From 32613f3862477bc73841cca02960f737cb4a44fe Mon Sep 17 00:00:00 2001 From: Herman Sletmoen Date: Thu, 25 Apr 2024 17:10:46 +0200 Subject: [PATCH 0001/2176] Fix structural simplification of non-1-indexed variable arrays --- Project.toml | 1 + src/structural_transformation/symbolics_tearing.jl | 6 +++++- test/odesystem.jl | 10 ++++++++++ 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 6a7a0bcecf..09f984444b 100644 --- a/Project.toml +++ b/Project.toml @@ -32,6 +32,7 @@ Libdl = "8f399da3-3557-5675-b5ff-fb832c97cbdb" LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" MLStyle = "d8e11817-5142-5d16-987a-aa16d5891078" NaNMath = "77ba4419-2d1f-58cd-9bb1-8ffee604a2e3" +OffsetArrays = "6fe1bfb0-de20-5000-8ca7-80f57d26f881" OrdinaryDiffEq = "1dea7af3-3e70-54e6-95c3-0bf5283fa5ed" PrecompileTools = "aea7be01-6a6a-4083-8856-8a6e6704d82a" RecursiveArrayTools = "731186ca-8d62-57ce-b412-fbd966d074cd" diff --git a/src/structural_transformation/symbolics_tearing.jl b/src/structural_transformation/symbolics_tearing.jl index f2f27e8606..0764e16c3e 100644 --- a/src/structural_transformation/symbolics_tearing.jl +++ b/src/structural_transformation/symbolics_tearing.jl @@ -1,3 +1,5 @@ +using OffsetArrays: Origin + # N.B. assumes `slist` and `dlist` are unique function substitution_graph(graph, slist, dlist, var_eq_matching) ns = length(slist) @@ -573,7 +575,9 @@ function tearing_reassemble(state::TearingState, var_eq_matching, Symbolics.shape(lhs) !== Symbolics.Unknown() || continue arg1 = arguments(lhs)[1] haskey(obs_arr_subs, arg1) && continue - obs_arr_subs[arg1] = [arg1[i] for i in eachindex(arg1)] + obs_arr_subs[arg1] = [arg1[i] for i in eachindex(arg1)] # e.g. p => [p[1], p[2]] + index_first = eachindex(arg1)[1] + obs_arr_subs[arg1] = Origin(index_first)(obs_arr_subs[arg1]) # respect non-1-indexed arrays end for i in eachindex(neweqs) neweqs[i] = fast_substitute(neweqs[i], obs_arr_subs; operator = Symbolics.Operator) diff --git a/test/odesystem.jl b/test/odesystem.jl index f11ab62a06..bca7f7f99a 100644 --- a/test/odesystem.jl +++ b/test/odesystem.jl @@ -1159,3 +1159,13 @@ for sys in [sys1, sys2] @test variable_index(sys, x[i]) == variable_index(sys, x)[i] end end + +@testset "Non-1-indexed variable array (issue #2670)" begin + @variables x(t)[0:1] # 0-indexed variable array + sys = ODESystem([ + x[0] ~ 0.0 + D(x[1]) ~ x[0] + ], t, [x], []; name=:sys) + @test_nowarn sys = structural_simplify(sys) + @test equations(sys) == [D(x[1]) ~ 0.0] +end \ No newline at end of file From c06abfe86ad6815694732f81e5b757b4fa58ed7e Mon Sep 17 00:00:00 2001 From: Herman Sletmoen Date: Thu, 25 Apr 2024 17:21:08 +0200 Subject: [PATCH 0002/2176] Format code --- src/structural_transformation/symbolics_tearing.jl | 2 +- test/odesystem.jl | 7 ++----- 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/src/structural_transformation/symbolics_tearing.jl b/src/structural_transformation/symbolics_tearing.jl index 0764e16c3e..d1d86cac1a 100644 --- a/src/structural_transformation/symbolics_tearing.jl +++ b/src/structural_transformation/symbolics_tearing.jl @@ -576,7 +576,7 @@ function tearing_reassemble(state::TearingState, var_eq_matching, arg1 = arguments(lhs)[1] haskey(obs_arr_subs, arg1) && continue obs_arr_subs[arg1] = [arg1[i] for i in eachindex(arg1)] # e.g. p => [p[1], p[2]] - index_first = eachindex(arg1)[1] + index_first = eachindex(arg1)[1] obs_arr_subs[arg1] = Origin(index_first)(obs_arr_subs[arg1]) # respect non-1-indexed arrays end for i in eachindex(neweqs) diff --git a/test/odesystem.jl b/test/odesystem.jl index bca7f7f99a..cbef18473f 100644 --- a/test/odesystem.jl +++ b/test/odesystem.jl @@ -1162,10 +1162,7 @@ end @testset "Non-1-indexed variable array (issue #2670)" begin @variables x(t)[0:1] # 0-indexed variable array - sys = ODESystem([ - x[0] ~ 0.0 - D(x[1]) ~ x[0] - ], t, [x], []; name=:sys) + @named sys = ODESystem([x[0] ~ 0.0, D(x[1]) ~ x[0]], t, [x], []) @test_nowarn sys = structural_simplify(sys) @test equations(sys) == [D(x[1]) ~ 0.0] -end \ No newline at end of file +end From c8c2fc55b212ec51d379902d62bad1876dbc1031 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 11 Sep 2024 17:52:32 +0530 Subject: [PATCH 0003/2176] feat: handle case of indexed array variable in SII impl for IndexCache --- src/systems/index_cache.jl | 48 +++++++++++++++++++++----------------- 1 file changed, 26 insertions(+), 22 deletions(-) diff --git a/src/systems/index_cache.jl b/src/systems/index_cache.jl index 3f2b4ddebe..64e9c134f6 100644 --- a/src/systems/index_cache.jl +++ b/src/systems/index_cache.jl @@ -288,11 +288,7 @@ function IndexCache(sys::AbstractSystem) end function SymbolicIndexingInterface.is_variable(ic::IndexCache, sym) - if sym isa Symbol - sym = get(ic.symbol_to_variable, sym, nothing) - sym === nothing && return false - end - return check_index_map(ic.unknown_idx, sym) !== nothing + variable_index(ic, sym) !== nothing end function SymbolicIndexingInterface.variable_index(ic::IndexCache, sym) @@ -300,18 +296,17 @@ function SymbolicIndexingInterface.variable_index(ic::IndexCache, sym) sym = get(ic.symbol_to_variable, sym, nothing) sym === nothing && return nothing end - return check_index_map(ic.unknown_idx, sym) + idx = check_index_map(ic.unknown_idx, sym) + idx === nothing || return idx + iscall(sym) && operation(sym) == getindex || return nothing + args = arguments(sym) + idx = variable_index(ic, args[1]) + idx === nothing && return nothing + return idx[args[2:end]...] end function SymbolicIndexingInterface.is_parameter(ic::IndexCache, sym) - if sym isa Symbol - sym = get(ic.symbol_to_variable, sym, nothing) - sym === nothing && return false - end - return check_index_map(ic.tunable_idx, sym) !== nothing || - check_index_map(ic.discrete_idx, sym) !== nothing || - check_index_map(ic.constant_idx, sym) !== nothing || - check_index_map(ic.nonnumeric_idx, sym) !== nothing + parameter_index(ic, sym) !== nothing end function SymbolicIndexingInterface.parameter_index(ic::IndexCache, sym) @@ -331,17 +326,21 @@ function SymbolicIndexingInterface.parameter_index(ic::IndexCache, sym) ParameterIndex(SciMLStructures.Constants(), idx, validate_size) elseif (idx = check_index_map(ic.nonnumeric_idx, sym)) !== nothing ParameterIndex(NONNUMERIC_PORTION, idx, validate_size) - else - nothing + elseif iscall(sym) && operation(sym) == getindex + args = arguments(sym) + pidx = parameter_index(ic, args[1]) + pidx === nothing && return nothing + if pidx.portion == SciMLStructures.Tunable() + ParameterIndex(pidx.portion, reshape(pidx.idx, size(args[1]))[args[2:end]...], + pidx.validate_size) + else + ParameterIndex(pidx.portion, (pidx.idx..., args[2:end]...), pidx.validate_size) + end end end function SymbolicIndexingInterface.is_timeseries_parameter(ic::IndexCache, sym) - if sym isa Symbol - sym = get(ic.symbol_to_variable, sym, nothing) - sym === nothing && return false - end - return check_index_map(ic.discrete_idx, sym) !== nothing + timeseries_parameter_index(ic, sym) !== nothing end function SymbolicIndexingInterface.timeseries_parameter_index(ic::IndexCache, sym) @@ -350,8 +349,13 @@ function SymbolicIndexingInterface.timeseries_parameter_index(ic::IndexCache, sy sym === nothing && return nothing end idx = check_index_map(ic.discrete_idx, sym) + idx === nothing || + return ParameterTimeseriesIndex(idx.clock_idx, (idx.buffer_idx, idx.idx_in_clock)) + iscall(sym) && operation(sym) == getindex || return nothing + args = arguments(sym) + idx = timeseries_parameter_index(ic, args[1]) idx === nothing && return nothing - return ParameterTimeseriesIndex(idx.clock_idx, (idx.buffer_idx, idx.idx_in_clock)) + ParameterIndex(idx.portion, (idx.idx..., args[2:end]...), idx.validate_size) end function check_index_map(idxmap, sym) From 58ae68b67ad162b1d0d690c055191059a59e315d Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 11 Sep 2024 17:53:38 +0530 Subject: [PATCH 0004/2176] feat: make parameter type validation error more descriptive --- src/systems/parameter_buffer.jl | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/src/systems/parameter_buffer.jl b/src/systems/parameter_buffer.jl index 95ca13adad..c2705bab3f 100644 --- a/src/systems/parameter_buffer.jl +++ b/src/systems/parameter_buffer.jl @@ -336,14 +336,15 @@ function Base.copy(p::MTKParameters) end function SymbolicIndexingInterface.parameter_values(p::MTKParameters, pind::ParameterIndex) + _ducktyped_parameter_values(p, pind) +end +function _ducktyped_parameter_values(p, pind::ParameterIndex) @unpack portion, idx = pind if portion isa SciMLStructures.Tunable return idx isa Int ? p.tunable[idx] : view(p.tunable, idx) end i, j, k... = idx - if portion isa SciMLStructures.Tunable - return isempty(k) ? p.tunable[i][j] : p.tunable[i][j][k...] - elseif portion isa SciMLStructures.Discrete + if portion isa SciMLStructures.Discrete return isempty(k) ? p.discrete[i][j] : p.discrete[i][j][k...] elseif portion isa SciMLStructures.Constants return isempty(k) ? p.constant[i][j] : p.constant[i][j][k...] @@ -444,11 +445,13 @@ function validate_parameter_type(ic::IndexCache, stype, sz, sym, index, val) # Nonnumeric parameters have to match the type if portion === NONNUMERIC_PORTION val isa stype && return nothing - throw(ParameterTypeException(:validate_parameter_type, sym, stype, val)) + throw(ParameterTypeException( + :validate_parameter_type, sym === nothing ? index : sym, stype, val)) end # Array parameters need array values... if stype <: AbstractArray && !isa(val, AbstractArray) - throw(ParameterTypeException(:validate_parameter_type, sym, stype, val)) + throw(ParameterTypeException( + :validate_parameter_type, sym === nothing ? index : sym, stype, val)) end # ... and must match sizes if stype <: AbstractArray && sz != Symbolics.Unknown() && size(val) != sz @@ -465,7 +468,7 @@ function validate_parameter_type(ic::IndexCache, stype, sz, sym, index, val) # This is for duals and other complicated number types etype = SciMLBase.parameterless_type(etype) eltype(val) <: etype || throw(ParameterTypeException( - :validate_parameter_type, sym, AbstractArray{etype}, val)) + :validate_parameter_type, sym === nothing ? index : sym, AbstractArray{etype}, val)) else # Real check if stype <: Real @@ -473,7 +476,8 @@ function validate_parameter_type(ic::IndexCache, stype, sz, sym, index, val) end stype = SciMLBase.parameterless_type(stype) val isa stype || - throw(ParameterTypeException(:validate_parameter_type, sym, stype, val)) + throw(ParameterTypeException( + :validate_parameter_type, sym === nothing ? index : sym, stype, val)) end end From a246d5595d410fc672db486e3540563c786d56ff Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 11 Sep 2024 17:54:04 +0530 Subject: [PATCH 0005/2176] feat: add adjoint for `remake_buffer` --- ext/MTKChainRulesCoreExt.jl | 90 ++++++++++++++++++++++++++++++++- src/systems/parameter_buffer.jl | 9 +++- test/extensions/Project.toml | 2 + test/extensions/ad.jl | 46 +++++++++++++++++ 4 files changed, 145 insertions(+), 2 deletions(-) diff --git a/ext/MTKChainRulesCoreExt.jl b/ext/MTKChainRulesCoreExt.jl index e7019e25df..a3a7f10dad 100644 --- a/ext/MTKChainRulesCoreExt.jl +++ b/ext/MTKChainRulesCoreExt.jl @@ -2,14 +2,102 @@ module MTKChainRulesCoreExt import ModelingToolkit as MTK import ChainRulesCore -import ChainRulesCore: NoTangent +import ChainRulesCore: Tangent, ZeroTangent, NoTangent, zero_tangent, unthunk function ChainRulesCore.rrule(::Type{MTK.MTKParameters}, tunables, args...) function mtp_pullback(dt) + dt = unthunk(dt) (NoTangent(), dt.tunable[1:length(tunables)], ntuple(_ -> NoTangent(), length(args))...) end MTK.MTKParameters(tunables, args...), mtp_pullback end +notangent_or_else(::NoTangent, _, x) = x +notangent_or_else(_, x, _) = x +notangent_fallback(x, y) = notangent_or_else(x, x, y) +reduce_to_notangent(x, y) = notangent_or_else(x, y, x) + +function subset_idxs(idxs, portion, template) + ntuple(Val(length(template))) do subi + [Base.tail(idx.idx) for idx in idxs if idx.portion == portion && idx.idx[1] == subi] + end +end + +selected_tangents(::NoTangent, _) = () +selected_tangents(::ZeroTangent, _) = ZeroTangent() +function selected_tangents( + tangents::AbstractArray{T}, idxs::Vector{Tuple{Int}}) where {T <: Number} + selected_tangents(tangents, map(only, idxs)) +end +function selected_tangents(tangents::AbstractArray{T}, idxs...) where {T <: Number} + newtangents = copy(tangents) + view(newtangents, idxs...) .= zero(T) + newtangents +end +function selected_tangents( + tangents::AbstractVector{T}, idxs) where {S <: Number, T <: AbstractArray{S}} + newtangents = copy(tangents) + for i in idxs + j, k... = i + if k == () + newtangents[j] = zero(newtangents[j]) + else + newtangents[j] = selected_tangents(newtangents[j], k...) + end + end + newtangents +end +function selected_tangents(tangents::AbstractVector{T}, idxs) where {T <: AbstractArray} + newtangents = similar(tangents, Union{T, NoTangent}) + copyto!(newtangents, tangents) + for i in idxs + j, k... = i + if k == () + newtangents[j] = NoTangent() + else + newtangents[j] = selected_tangents(newtangents[j], k...) + end + end + newtangents +end +function selected_tangents( + tangents::Union{Tangent{<:Tuple}, Tangent{T, <:Tuple}}, idxs) where {T} + ntuple(Val(length(tangents))) do i + selected_tangents(tangents[i], idxs[i]) + end +end + +function ChainRulesCore.rrule( + ::typeof(MTK.remake_buffer), indp, oldbuf::MTK.MTKParameters, idxs, vals) + if idxs isa AbstractSet + idxs = collect(idxs) + end + idxs = map(idxs) do i + i isa MTK.ParameterIndex ? i : MTK.parameter_index(indp, i) + end + newbuf = MTK.remake_buffer(indp, oldbuf, idxs, vals) + tunable_idxs = reduce( + vcat, (idx.idx for idx in idxs if idx.portion isa MTK.SciMLStructures.Tunable)) + disc_idxs = subset_idxs(idxs, MTK.SciMLStructures.Discrete(), oldbuf.discrete) + const_idxs = subset_idxs(idxs, MTK.SciMLStructures.Constants(), oldbuf.constant) + nn_idxs = subset_idxs(idxs, MTK.NONNUMERIC_PORTION, oldbuf.nonnumeric) + + function remake_buffer_pullback(buf′) + buf′ = unthunk(buf′) + f′ = NoTangent() + indp′ = NoTangent() + + tunable = selected_tangents(buf′.tunable, tunable_idxs) + discrete = selected_tangents(buf′.discrete, disc_idxs) + constant = selected_tangents(buf′.constant, const_idxs) + nonnumeric = selected_tangents(buf′.nonnumeric, nn_idxs) + oldbuf′ = Tangent{typeof(oldbuf)}(; tunable, discrete, constant, nonnumeric) + idxs′ = NoTangent() + vals′ = map(i -> MTK._ducktyped_parameter_values(buf′, i), idxs) + return f′, indp′, oldbuf′, idxs′, vals′ + end + newbuf, remake_buffer_pullback +end + end diff --git a/src/systems/parameter_buffer.jl b/src/systems/parameter_buffer.jl index c2705bab3f..ad001b32dc 100644 --- a/src/systems/parameter_buffer.jl +++ b/src/systems/parameter_buffer.jl @@ -436,8 +436,12 @@ function validate_parameter_type(ic::IndexCache, p, idx::ParameterIndex, val) end function validate_parameter_type(ic::IndexCache, idx::ParameterIndex, val) + stype = get_buffer_template(ic, idx).type + if idx.portion == SciMLStructures.Tunable() && !(idx.idx isa Int) + stype = AbstractArray{<:stype} + end validate_parameter_type( - ic, get_buffer_template(ic, idx).type, Symbolics.Unknown(), nothing, idx, val) + ic, stype, Symbolics.Unknown(), nothing, idx, val) end function validate_parameter_type(ic::IndexCache, stype, sz, sym, index, val) @@ -489,6 +493,9 @@ function indp_to_system(indp) end function SymbolicIndexingInterface.remake_buffer(indp, oldbuf::MTKParameters, idxs, vals) + _remake_buffer(indp, oldbuf, idxs, vals) +end +function _remake_buffer(indp, oldbuf::MTKParameters, idxs, vals; validate = true) newbuf = @set oldbuf.tunable = similar(oldbuf.tunable, Any) @set! newbuf.discrete = Tuple(similar(buf, Any) for buf in newbuf.discrete) @set! newbuf.constant = Tuple(similar(buf, Any) for buf in newbuf.constant) diff --git a/test/extensions/Project.toml b/test/extensions/Project.toml index d8d3d64605..81097d98d2 100644 --- a/test/extensions/Project.toml +++ b/test/extensions/Project.toml @@ -1,5 +1,7 @@ [deps] BifurcationKit = "0f109fa4-8a5d-4b75-95aa-f515264e7665" +ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4" +ChainRulesTestUtils = "cdddcdb0-9152-4a09-a978-84456f9df70a" ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210" LabelledArrays = "2ee39098-c373-598a-b85f-a56591580800" ModelingToolkit = "961ee093-0014-501f-94e3-6117800e7a78" diff --git a/test/extensions/ad.jl b/test/extensions/ad.jl index 1946db5277..d0db435ecb 100644 --- a/test/extensions/ad.jl +++ b/test/extensions/ad.jl @@ -6,6 +6,9 @@ using SciMLStructures using OrdinaryDiffEq using SciMLSensitivity using ForwardDiff +using ChainRulesCore +using ChainRulesCore: NoTangent +using ChainRulesTestUtils: test_rrule @variables x(t)[1:3] y(t) @parameters p[1:3, 1:3] q @@ -51,3 +54,46 @@ end @test ForwardDiff.gradient(x_at_0, [0.3, 0.7]) == zeros(2) end + +@parameters a b[1:3] c(t) d::Integer e[1:3] f[1:3, 1:3]::Int g::Vector{AbstractFloat} h::String +@named sys = ODESystem( + Equation[], t, [], [a, b, c, d, e, f, g, h], + continuous_events = [[a ~ 0] => [c ~ 0]]) +sys = complete(sys) + +ivs = Dict(c => 3a, b => ones(3), a => 1.0, d => 4, e => [5.0, 6.0, 7.0], + f => ones(Int, 3, 3), g => [0.1, 0.2, 0.3], h => "foo") + +ps = MTKParameters(sys, ivs) + +varmap = Dict(a => 1.0f0, b => 3ones(Float32, 3), c => 2.0, + e => Float32[0.4, 0.5, 0.6], g => ones(Float32, 4)) +get_values = getp(sys, [a, b..., c, e...]) +get_g = getp(sys, g) +for (_idxs, vals) in [ + # all portions + (collect(keys(varmap)), collect(values(varmap))), + # non-arrays + (keys(varmap), values(varmap)), + # tunable only + ([a], [varmap[a]]), + ([a, b], (varmap[a], varmap[b])), + ([a, b[2]], (varmap[a], varmap[b][2])) +] + for idxs in [_idxs, map(i -> parameter_index(sys, i), collect(_idxs))] + loss = function (p) + newps = remake_buffer(sys, ps, idxs, p) + return sum(get_values(newps)) + sum(get_g(newps)) + end + + grad = Zygote.gradient(loss, vals)[1] + for (val, g) in zip(vals, grad) + @test eltype(val) == eltype(g) + if val isa Number + @test isone(g) + else + @test all(isone, g) + end + end + end +end From 55e3452e9fb82ee4af681ed99e6e9dea65fc804b Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 16 Sep 2024 12:25:31 +0530 Subject: [PATCH 0006/2176] feat: improve type-stability of adjoint --- ext/MTKChainRulesCoreExt.jl | 33 +++++++++++++++------------------ test/extensions/ad.jl | 8 +++++++- 2 files changed, 22 insertions(+), 19 deletions(-) diff --git a/ext/MTKChainRulesCoreExt.jl b/ext/MTKChainRulesCoreExt.jl index a3a7f10dad..f84690e23f 100644 --- a/ext/MTKChainRulesCoreExt.jl +++ b/ext/MTKChainRulesCoreExt.jl @@ -13,11 +13,6 @@ function ChainRulesCore.rrule(::Type{MTK.MTKParameters}, tunables, args...) MTK.MTKParameters(tunables, args...), mtp_pullback end -notangent_or_else(::NoTangent, _, x) = x -notangent_or_else(_, x, _) = x -notangent_fallback(x, y) = notangent_or_else(x, x, y) -reduce_to_notangent(x, y) = notangent_or_else(x, y, x) - function subset_idxs(idxs, portion, template) ntuple(Val(length(template))) do subi [Base.tail(idx.idx) for idx in idxs if idx.portion == portion && idx.idx[1] == subi] @@ -83,21 +78,23 @@ function ChainRulesCore.rrule( const_idxs = subset_idxs(idxs, MTK.SciMLStructures.Constants(), oldbuf.constant) nn_idxs = subset_idxs(idxs, MTK.NONNUMERIC_PORTION, oldbuf.nonnumeric) - function remake_buffer_pullback(buf′) - buf′ = unthunk(buf′) - f′ = NoTangent() - indp′ = NoTangent() + pullback = let idxs = idxs + function remake_buffer_pullback(buf′) + buf′ = unthunk(buf′) + f′ = NoTangent() + indp′ = NoTangent() - tunable = selected_tangents(buf′.tunable, tunable_idxs) - discrete = selected_tangents(buf′.discrete, disc_idxs) - constant = selected_tangents(buf′.constant, const_idxs) - nonnumeric = selected_tangents(buf′.nonnumeric, nn_idxs) - oldbuf′ = Tangent{typeof(oldbuf)}(; tunable, discrete, constant, nonnumeric) - idxs′ = NoTangent() - vals′ = map(i -> MTK._ducktyped_parameter_values(buf′, i), idxs) - return f′, indp′, oldbuf′, idxs′, vals′ + tunable = selected_tangents(buf′.tunable, tunable_idxs) + discrete = selected_tangents(buf′.discrete, disc_idxs) + constant = selected_tangents(buf′.constant, const_idxs) + nonnumeric = selected_tangents(buf′.nonnumeric, nn_idxs) + oldbuf′ = Tangent{typeof(oldbuf)}(; tunable, discrete, constant, nonnumeric) + idxs′ = NoTangent() + vals′ = map(i -> MTK._ducktyped_parameter_values(buf′, i), idxs) + return f′, indp′, oldbuf′, idxs′, vals′ + end end - newbuf, remake_buffer_pullback + newbuf, pullback end end diff --git a/test/extensions/ad.jl b/test/extensions/ad.jl index d0db435ecb..954b868a1e 100644 --- a/test/extensions/ad.jl +++ b/test/extensions/ad.jl @@ -8,7 +8,7 @@ using SciMLSensitivity using ForwardDiff using ChainRulesCore using ChainRulesCore: NoTangent -using ChainRulesTestUtils: test_rrule +using ChainRulesTestUtils: test_rrule, rand_tangent @variables x(t)[1:3] y(t) @parameters p[1:3, 1:3] q @@ -97,3 +97,9 @@ for (_idxs, vals) in [ end end end + +idxs = (parameter_index(sys, a), parameter_index(sys, b)) +vals = (1.0f0, 3ones(Float32, 3)) +tangent = rand_tangent(ps) +fwd, back = ChainRulesCore.rrule(remake_buffer, sys, ps, idxs, vals) +@inferred back(tangent) From ea01bae44debf1eefec3fde5b77e9de0023e5d21 Mon Sep 17 00:00:00 2001 From: Herman Sletmoen Date: Mon, 16 Sep 2024 15:14:33 +0200 Subject: [PATCH 0007/2176] Merge system guesses before dummy derivative substitution --- src/systems/nonlinear/initializesystem.jl | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/systems/nonlinear/initializesystem.jl b/src/systems/nonlinear/initializesystem.jl index aa1ecd9a8b..aa7ad8bd69 100644 --- a/src/systems/nonlinear/initializesystem.jl +++ b/src/systems/nonlinear/initializesystem.jl @@ -29,7 +29,7 @@ function generate_initializesystem(sys::ODESystem; full_states = unique([sts; getfield.((observed(sys)), :lhs)]) set_full_states = Set(full_states) - guesses = todict(guesses) + guesses = merge(get_guesses(sys), todict(guesses)) schedule = getfield(sys, :schedule) if schedule !== nothing @@ -69,7 +69,6 @@ function generate_initializesystem(sys::ODESystem; end defs = merge(defaults(sys), filtered_u0) - guesses = merge(get_guesses(sys), todict(guesses), dd_guess) for st in full_states if st ∈ keys(defs) From a3bee38dce1f89db07d9d74600d8d0d671aab21c Mon Sep 17 00:00:00 2001 From: Herman Sletmoen Date: Mon, 16 Sep 2024 15:14:52 +0200 Subject: [PATCH 0008/2176] Test derivative guesses --- test/initializationsystem.jl | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/test/initializationsystem.jl b/test/initializationsystem.jl index 08c66e85e6..eda57c11b0 100644 --- a/test/initializationsystem.jl +++ b/test/initializationsystem.jl @@ -510,7 +510,7 @@ end end # https://github.com/SciML/ModelingToolkit.jl/issues/3029 -@testset "Derivatives in Initialization Equations" begin +@testset "Derivatives in initialization equations" begin @variables x(t) sys = ODESystem( [D(D(x)) ~ 0], t; initialization_eqs = [x ~ 0, D(x) ~ 1], name = :sys) |> @@ -523,6 +523,20 @@ end @test_nowarn ODEProblem(sys, [D(x) => 1.0], (0.0, 1.0), []) end +# https://github.com/SciML/ModelingToolkit.jl/issues/3049 +@testset "Derivatives in initialization guesses" begin + for sign in [-1.0, +1.0] + @variables x(t) + sys = ODESystem( + [D(D(x)) ~ 0], t; + initialization_eqs = [D(x)^2 ~ 1, x ~ 0], guesses = [D(x) => sign], name = :sys + ) |> structural_simplify + prob = ODEProblem(sys, [], (0.0, 1.0), []) + sol = solve(prob, Tsit5()) + @test sol(1.0, idxs=sys.x) ≈ sign # system with D(x(0)) = ±1 should solve to x(1) = ±1 + end +end + # https://github.com/SciML/ModelingToolkit.jl/issues/2619 @parameters k1 k2 ω @variables X(t) Y(t) From 556d09e5652ba0fcd88a9360ad1c1d92923d6b6e Mon Sep 17 00:00:00 2001 From: Herman Sletmoen Date: Mon, 16 Sep 2024 15:47:04 +0200 Subject: [PATCH 0009/2176] Format --- test/initializationsystem.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/initializationsystem.jl b/test/initializationsystem.jl index eda57c11b0..82d129b901 100644 --- a/test/initializationsystem.jl +++ b/test/initializationsystem.jl @@ -533,7 +533,7 @@ end ) |> structural_simplify prob = ODEProblem(sys, [], (0.0, 1.0), []) sol = solve(prob, Tsit5()) - @test sol(1.0, idxs=sys.x) ≈ sign # system with D(x(0)) = ±1 should solve to x(1) = ±1 + @test sol(1.0, idxs = sys.x) ≈ sign # system with D(x(0)) = ±1 should solve to x(1) = ±1 end end From 9aadc7172647fe1d1bb6941a6cacad50ebf7d7d9 Mon Sep 17 00:00:00 2001 From: Christopher Rackauckas Date: Mon, 16 Sep 2024 23:19:34 +0200 Subject: [PATCH 0010/2176] Update Project.toml --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 39f94e545b..6bec6f5a22 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "ModelingToolkit" uuid = "961ee093-0014-501f-94e3-6117800e7a78" authors = ["Yingbo Ma ", "Chris Rackauckas and contributors"] -version = "9.39.1" +version = "9.40.0" [deps] AbstractTrees = "1520ce14-60c1-5f80-bbc7-55ef81b5835c" From 9671deb046ac66e0b6b64d98ffbfe521630925fc Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 28 Aug 2024 13:21:48 +0530 Subject: [PATCH 0011/2176] feat: support callable parameters --- Project.toml | 6 ++++- src/ModelingToolkit.jl | 4 ++- src/parameters.jl | 10 ++++++++ src/systems/index_cache.jl | 43 +++++++++++++++++++++---------- src/systems/parameter_buffer.jl | 3 +++ src/systems/systemstructure.jl | 3 ++- src/utils.jl | 12 +++++++-- test/split_parameters.jl | 45 +++++++++++++++++++++++++++++++++ 8 files changed, 107 insertions(+), 19 deletions(-) diff --git a/Project.toml b/Project.toml index 6bec6f5a22..b73fa95e6f 100644 --- a/Project.toml +++ b/Project.toml @@ -24,6 +24,7 @@ ExprTools = "e2ba6199-217a-4e67-a87a-7c52f15ade04" Expronicon = "6b7a57c9-7cc1-4fdf-b7f5-e857abae3636" FindFirstFunctions = "64ca27bc-2ba2-4a57-88aa-44e436879224" ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210" +FunctionWrappers = "069b7b12-0de2-55c6-9aab-29f3d0a68a2e" FunctionWrappersWrappers = "77dc65aa-8811-40c2-897b-53d922fa7daf" Graphs = "86223c79-3864-5bf0-83f7-82e725a168b6" InteractiveUtils = "b77e0a4c-d291-57a0-90e8-8db25a27a240" @@ -76,6 +77,7 @@ ChainRulesCore = "1" Combinatorics = "1" Compat = "3.42, 4" ConstructionBase = "1" +DataInterpolations = "6.4" DataStructures = "0.17, 0.18" DeepDiffs = "1" DiffEqBase = "6.103.0" @@ -91,6 +93,7 @@ ExprTools = "0.1.10" Expronicon = "0.8" FindFirstFunctions = "1" ForwardDiff = "0.10.3" +FunctionWrappers = "1.1" FunctionWrappersWrappers = "0.1" Graphs = "1.5.2" InteractiveUtils = "1" @@ -129,6 +132,7 @@ julia = "1.9" AmplNLWriter = "7c4d4715-977e-5154-bfe0-e096adeac482" BenchmarkTools = "6e4b80f9-dd63-53aa-95a3-0cdb28fa8baf" ControlSystemsBase = "aaaaaaaa-a6ca-5380-bf3e-84a91bcd477e" +DataInterpolations = "82cc6244-b520-54b8-b5a6-8a565e85f1d0" DeepDiffs = "ab62b9b5-e342-54a8-a765-a90f495de1a6" DelayDiffEq = "bcd4f6db-9728-5f36-b5f7-82caef46ccdb" ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210" @@ -154,4 +158,4 @@ Sundials = "c3572dad-4567-51f8-b174-8c6c989267f4" Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" [targets] -test = ["AmplNLWriter", "BenchmarkTools", "ControlSystemsBase", "DelayDiffEq", "NonlinearSolve", "ForwardDiff", "Ipopt", "Ipopt_jll", "ModelingToolkitStandardLibrary", "Optimization", "OptimizationOptimJL", "OptimizationMOI", "OrdinaryDiffEq", "Random", "ReferenceTests", "SafeTestsets", "StableRNGs", "Statistics", "SteadyStateDiffEq", "Test", "StochasticDiffEq", "Sundials", "StochasticDelayDiffEq", "Pkg", "JET"] +test = ["AmplNLWriter", "BenchmarkTools", "ControlSystemsBase", "DataInterpolations", "DelayDiffEq", "NonlinearSolve", "ForwardDiff", "Ipopt", "Ipopt_jll", "ModelingToolkitStandardLibrary", "Optimization", "OptimizationOptimJL", "OptimizationMOI", "OrdinaryDiffEq", "Random", "ReferenceTests", "SafeTestsets", "StableRNGs", "Statistics", "SteadyStateDiffEq", "Test", "StochasticDiffEq", "Sundials", "StochasticDelayDiffEq", "Pkg", "JET"] diff --git a/src/ModelingToolkit.jl b/src/ModelingToolkit.jl index 13b29831e5..9a4b025d07 100644 --- a/src/ModelingToolkit.jl +++ b/src/ModelingToolkit.jl @@ -38,6 +38,7 @@ using Base: RefValue using Combinatorics import Distributions import FunctionWrappersWrappers +import FunctionWrappers: FunctionWrapper using URIs: URI using SciMLStructures using Compat @@ -63,7 +64,8 @@ using Symbolics: _parse_vars, value, @derivatives, get_variables, VariableSource, getname, variable, Connection, connect, NAMESPACE_SEPARATOR, set_scalar_metadata, setdefaultval, initial_state, transition, activeState, entry, hasnode, - ticksInState, timeInState, fixpoint_sub, fast_substitute + ticksInState, timeInState, fixpoint_sub, fast_substitute, + CallWithMetadata, CallWithParent const NAMESPACE_SEPARATOR_SYMBOL = Symbol(NAMESPACE_SEPARATOR) import Symbolics: rename, get_variables!, _solve, hessian_sparsity, jacobian_sparsity, isaffine, islinear, _iszero, _isone, diff --git a/src/parameters.jl b/src/parameters.jl index fb6b0b4d6e..ced7767718 100644 --- a/src/parameters.jl +++ b/src/parameters.jl @@ -26,6 +26,16 @@ function isparameter(x) end end +function iscalledparameter(x) + x = unwrap(x) + return isparameter(getmetadata(x, CallWithParent, nothing)) +end + +function getcalledparameter(x) + x = unwrap(x) + return getmetadata(x, CallWithParent) +end + """ toparam(s) diff --git a/src/systems/index_cache.jl b/src/systems/index_cache.jl index 64e9c134f6..82a17d01e9 100644 --- a/src/systems/index_cache.jl +++ b/src/systems/index_cache.jl @@ -32,6 +32,8 @@ struct DiscreteIndex end const ParamIndexMap = Dict{BasicSymbolic, Tuple{Int, Int}} +const NonnumericMap = Dict{ + Union{BasicSymbolic, Symbolics.CallWithMetadata}, Tuple{Int, Int}} const UnknownIndexMap = Dict{ BasicSymbolic, Union{Int, UnitRange{Int}, AbstractArray{Int}}} const TunableIndexMap = Dict{BasicSymbolic, @@ -45,20 +47,20 @@ struct IndexCache callback_to_clocks::Dict{Any, Vector{Int}} tunable_idx::TunableIndexMap constant_idx::ParamIndexMap - nonnumeric_idx::ParamIndexMap + nonnumeric_idx::NonnumericMap observed_syms::Set{BasicSymbolic} dependent_pars::Set{BasicSymbolic} discrete_buffer_sizes::Vector{Vector{BufferTemplate}} tunable_buffer_size::BufferTemplate constant_buffer_sizes::Vector{BufferTemplate} nonnumeric_buffer_sizes::Vector{BufferTemplate} - symbol_to_variable::Dict{Symbol, BasicSymbolic} + symbol_to_variable::Dict{Symbol, Union{BasicSymbolic, CallWithMetadata}} end function IndexCache(sys::AbstractSystem) unks = solved_unknowns(sys) unk_idxs = UnknownIndexMap() - symbol_to_variable = Dict{Symbol, BasicSymbolic}() + symbol_to_variable = Dict{Symbol, Union{BasicSymbolic, CallWithMetadata}}() let idx = 1 for sym in unks @@ -105,12 +107,11 @@ function IndexCache(sys::AbstractSystem) tunable_buffers = Dict{Any, Set{BasicSymbolic}}() constant_buffers = Dict{Any, Set{BasicSymbolic}}() - nonnumeric_buffers = Dict{Any, Set{BasicSymbolic}}() + nonnumeric_buffers = Dict{Any, Set{Union{BasicSymbolic, CallWithMetadata}}}() - function insert_by_type!(buffers::Dict{Any, Set{BasicSymbolic}}, sym) + function insert_by_type!(buffers::Dict{Any, S}, sym, ctype) where {S} sym = unwrap(sym) - ctype = symtype(sym) - buf = get!(buffers, ctype, Set{BasicSymbolic}()) + buf = get!(buffers, ctype, S()) push!(buf, sym) end @@ -142,7 +143,7 @@ function IndexCache(sys::AbstractSystem) clocks = get!(() -> Set{Int}(), disc_param_callbacks, sym) push!(clocks, i) else - insert_by_type!(constant_buffers, sym) + insert_by_type!(constant_buffers, sym, symtype(sym)) end end end @@ -197,6 +198,9 @@ function IndexCache(sys::AbstractSystem) for p in parameters(sys) p = unwrap(p) ctype = symtype(p) + if ctype <: FnType + ctype = fntype_to_function_type(ctype) + end haskey(disc_idxs, p) && continue haskey(constant_buffers, ctype) && p in constant_buffers[ctype] && continue insert_by_type!( @@ -212,12 +216,13 @@ function IndexCache(sys::AbstractSystem) else nonnumeric_buffers end, - p + p, + ctype ) end - function get_buffer_sizes_and_idxs(buffers::Dict{Any, Set{BasicSymbolic}}) - idxs = ParamIndexMap() + function get_buffer_sizes_and_idxs(T, buffers::Dict) + idxs = T() buffer_sizes = BufferTemplate[] for (i, (T, buf)) in enumerate(buffers) for (j, p) in enumerate(buf) @@ -229,13 +234,18 @@ function IndexCache(sys::AbstractSystem) idxs[rp] = (i, j) idxs[rttp] = (i, j) end + if T <: Symbolics.FnType + T = Any + end push!(buffer_sizes, BufferTemplate(T, length(buf))) end return idxs, buffer_sizes end - const_idxs, const_buffer_sizes = get_buffer_sizes_and_idxs(constant_buffers) - nonnumeric_idxs, nonnumeric_buffer_sizes = get_buffer_sizes_and_idxs(nonnumeric_buffers) + const_idxs, const_buffer_sizes = get_buffer_sizes_and_idxs( + ParamIndexMap, constant_buffers) + nonnumeric_idxs, nonnumeric_buffer_sizes = get_buffer_sizes_and_idxs( + NonnumericMap, nonnumeric_buffers) tunable_idxs = TunableIndexMap() tunable_buffer_size = 0 @@ -401,7 +411,8 @@ function reorder_parameters(ic::IndexCache, ps; drop_missing = false) for temp in ic.discrete_buffer_sizes) const_buf = Tuple(BasicSymbolic[unwrap(variable(:DEF)) for _ in 1:(temp.length)] for temp in ic.constant_buffer_sizes) - nonnumeric_buf = Tuple(BasicSymbolic[unwrap(variable(:DEF)) for _ in 1:(temp.length)] + nonnumeric_buf = Tuple(Union{BasicSymbolic, CallWithMetadata}[unwrap(variable(:DEF)) + for _ in 1:(temp.length)] for temp in ic.nonnumeric_buffer_sizes) for p in ps p = unwrap(p) @@ -481,3 +492,7 @@ function get_buffer_template(ic::IndexCache, pidx::ParameterIndex) error("Unhandled portion $portion") end end + +fntype_to_function_type(::Type{FnType{A, R, T}}) where {A, R, T} = T +fntype_to_function_type(::Type{FnType{A, R, Nothing}}) where {A, R} = FunctionWrapper{R, A} +fntype_to_function_type(::Type{FnType{A, R}}) where {A, R} = FunctionWrapper{R, A} diff --git a/src/systems/parameter_buffer.jl b/src/systems/parameter_buffer.jl index ad001b32dc..20ba3d731a 100644 --- a/src/systems/parameter_buffer.jl +++ b/src/systems/parameter_buffer.jl @@ -152,6 +152,9 @@ function MTKParameters( if symbolic_type(val) !== NotSymbolic() error("Could not evaluate value of parameter $sym. Missing values for variables in expression $val.") end + if ctype <: FnType + ctype = fntype_to_function_type(ctype) + end val = symconvert(ctype, val) done = set_value(sym, val) if !done && Symbolics.isarraysymbolic(sym) diff --git a/src/systems/systemstructure.jl b/src/systems/systemstructure.jl index 1b0b58d09a..71bdf6fe30 100644 --- a/src/systems/systemstructure.jl +++ b/src/systems/systemstructure.jl @@ -661,7 +661,8 @@ function structural_simplify!(state::TearingState, io = nothing; simplify = fals @set! sys.defaults = merge(ModelingToolkit.defaults(sys), Dict(v => 0.0 for v in Iterators.flatten(inputs))) end - ps = [setmetadata(sym, VariableTimeDomain, get(time_domains, sym, Continuous)) + ps = [sym isa CallWithMetadata ? sym : + setmetadata(sym, VariableTimeDomain, get(time_domains, sym, Continuous)) for sym in get_ps(sys)] @set! sys.ps = ps else diff --git a/src/utils.jl b/src/utils.jl index 095da9e247..4426c8a799 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -371,7 +371,11 @@ end vars(exprs::Num; op = Differential) = vars(unwrap(exprs); op) vars(exprs::Symbolics.Arr; op = Differential) = vars(unwrap(exprs); op) function vars(exprs; op = Differential) - foldl((x, y) -> vars!(x, unwrap(y); op = op), exprs; init = Set()) + if hasmethod(iterate, Tuple{typeof(exprs)}) + foldl((x, y) -> vars!(x, unwrap(y); op = op), exprs; init = Set()) + else + vars!(Set(), unwrap(exprs); op) + end end vars(eq::Equation; op = Differential) = vars!(Set(), eq; op = op) function vars!(vars, eq::Equation; op = Differential) @@ -479,7 +483,11 @@ end function collect_var!(unknowns, parameters, var, iv) isequal(var, iv) && return nothing - if isparameter(var) || (iscall(var) && isparameter(operation(var))) + if iscalledparameter(var) + callable = getcalledparameter(var) + push!(parameters, callable) + collect_vars!(unknowns, parameters, arguments(var), iv) + elseif isparameter(var) || (iscall(var) && isparameter(operation(var))) push!(parameters, var) elseif !isconstant(var) push!(unknowns, var) diff --git a/test/split_parameters.jl b/test/split_parameters.jl index acd98972db..9f124ef45d 100644 --- a/test/split_parameters.jl +++ b/test/split_parameters.jl @@ -1,11 +1,13 @@ using ModelingToolkit, Test using ModelingToolkitStandardLibrary.Blocks using OrdinaryDiffEq +using DataInterpolations using BlockArrays: BlockedArray using ModelingToolkit: t_nounits as t, D_nounits as D using ModelingToolkit: MTKParameters, ParameterIndex, NONNUMERIC_PORTION using SciMLStructures: Tunable, Discrete, Constants using StaticArrays: SizedVector +using SymbolicIndexingInterface: is_parameter, getp x = [1, 2.0, false, [1, 2, 3], Parameter(1.0)] @@ -219,3 +221,46 @@ S = get_sensitivity(closed_loop, :u) @test ps[ParameterIndex(Tunable(), 1:8)] == collect(1.0:8.0) .+ 0.5 @test ps[ParameterIndex(Discrete(), (2, 1, 2, 2))] == 5 end + +@testset "Callable parameters" begin + @testset "As FunctionWrapper" begin + _f1(x) = 2x + struct Foo end + (::Foo)(x) = 3x + @variables x(t) + @parameters fn(::Real) = _f1 + @mtkbuild sys = ODESystem(D(x) ~ fn(t), t) + @test is_parameter(sys, fn) + @test ModelingToolkit.defaults(sys)[fn] == _f1 + + getter = getp(sys, fn) + prob = ODEProblem(sys, [x => 1.0], (0.0, 1.0)) + @inferred getter(prob) + # cannot be inferred better since `FunctionWrapper` is only known to return `Real` + @inferred Vector{<:Real} prob.f(prob.u0, prob.p, prob.tspan[1]) + sol = solve(prob, Tsit5(); abstol = 1e-10, reltol = 1e-10) + @test sol.u[end][] ≈ 2.0 + + prob = ODEProblem(sys, [x => 1.0], (0.0, 1.0), [fn => Foo()]) + @inferred getter(prob) + @inferred Vector{<:Real} prob.f(prob.u0, prob.p, prob.tspan[1]) + sol = solve(prob; abstol = 1e-10, reltol = 1e-10) + @test sol.u[end][] ≈ 2.5 + end + + @testset "Concrete function type" begin + ts = 0.0:0.1:1.0 + interp = LinearInterpolation(ts .^ 2, ts; extrapolate = true) + @variables x(t) + @parameters (fn::typeof(interp))(..) + @mtkbuild sys = ODESystem(D(x) ~ fn(x), t) + @test is_parameter(sys, fn) + getter = getp(sys, fn) + prob = ODEProblem(sys, [x => 1.0], (0.0, 1.0), [fn => interp]) + @inferred getter(prob) + @inferred prob.f(prob.u0, prob.p, prob.tspan[1]) + @test_nowarn sol = solve(prob, Tsit5()) + @test_nowarn prob.ps[fn] = LinearInterpolation(ts .^ 3, ts; extrapolate = true) + @test_nowarn sol = solve(prob) + end +end From 2bed81c5bd4cece557241b099e94f32ed9f9acf8 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 19 Sep 2024 10:37:05 +0530 Subject: [PATCH 0012/2176] build: bump Symbolics, SymbolicUtils compat --- Project.toml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Project.toml b/Project.toml index b73fa95e6f..248ed16e6d 100644 --- a/Project.toml +++ b/Project.toml @@ -121,8 +121,8 @@ SparseArrays = "1" SpecialFunctions = "0.7, 0.8, 0.9, 0.10, 1.0, 2" StaticArrays = "0.10, 0.11, 0.12, 1.0" SymbolicIndexingInterface = "0.3.29" -SymbolicUtils = "3.2" -Symbolics = "6.3" +SymbolicUtils = "3.7" +Symbolics = "6.12" URIs = "1" UnPack = "0.1, 1.0" Unitful = "1.1" From 7579312a9b505df442ecce146c2a169a725d7931 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 19 Sep 2024 16:07:48 +0530 Subject: [PATCH 0013/2176] test: fix odesystem callable parameter tests --- test/odesystem.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/odesystem.jl b/test/odesystem.jl index e39649c1c2..9191ffc785 100644 --- a/test/odesystem.jl +++ b/test/odesystem.jl @@ -137,7 +137,7 @@ eqs = [D(x) ~ σ(t - 1) * (y - x), D(y) ~ x * (ρ - z) - y, D(z) ~ x * y - β * z * κ] @named de = ODESystem(eqs, t) -test_diffeq_inference("single internal iv-varying", de, t, (x, y, z), (σ(t - 1), ρ, β)) +test_diffeq_inference("single internal iv-varying", de, t, (x, y, z), (σ, ρ, β)) f = eval(generate_function(de, [x, y, z], [σ, ρ, β])[2]) du = [0.0, 0.0, 0.0] f(du, [1.0, 2.0, 3.0], [x -> x + 7, 2, 3], 5.0) @@ -145,7 +145,7 @@ f(du, [1.0, 2.0, 3.0], [x -> x + 7, 2, 3], 5.0) eqs = [D(x) ~ x + 10σ(t - 1) + 100σ(t - 2) + 1000σ(t^2)] @named de = ODESystem(eqs, t) -test_diffeq_inference("many internal iv-varying", de, t, (x,), (σ(t - 2), σ(t^2), σ(t - 1))) +test_diffeq_inference("many internal iv-varying", de, t, (x,), (σ,)) f = eval(generate_function(de, [x], [σ])[2]) du = [0.0] f(du, [1.0], [t -> t + 2], 5.0) From e85503548a8fda03a69d75591f518efd6bba02da Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 19 Sep 2024 15:57:26 +0530 Subject: [PATCH 0014/2176] fix: handle `nothing` passed as `u0` to `ODEProblem` fix: handle non-vector non-symbolic array u0 for ODEProblem --- src/systems/diffeqs/abstractodesystem.jl | 5 +++-- test/odesystem.jl | 7 +++++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/systems/diffeqs/abstractodesystem.jl b/src/systems/diffeqs/abstractodesystem.jl index d8085d7e33..f7f94504e4 100644 --- a/src/systems/diffeqs/abstractodesystem.jl +++ b/src/systems/diffeqs/abstractodesystem.jl @@ -817,11 +817,12 @@ function process_DEProblem(constructor, sys::AbstractODESystem, u0map, parammap; ModelingToolkit.get_tearing_state(sys) !== nothing) || !isempty(initialization_equations(sys))) && t !== nothing if eltype(u0map) <: Number - u0map = unknowns(sys) .=> u0map + u0map = unknowns(sys) .=> vec(u0map) end - if isempty(u0map) + if u0map === nothing || isempty(u0map) u0map = Dict() end + initializeprob = ModelingToolkit.InitializationProblem( sys, t, u0map, parammap; guesses, warn_initialize_determined, initialization_eqs, eval_expression, eval_module, fully_determined, check_units) diff --git a/test/odesystem.jl b/test/odesystem.jl index e39649c1c2..c229eebe28 100644 --- a/test/odesystem.jl +++ b/test/odesystem.jl @@ -1387,3 +1387,10 @@ end @test obsfn(ones(2), 2ones(2), 3ones(4), 4.0) == 6ones(2) end + +@testset "Passing `nothing` to `u0`" begin + @variables x(t) = 1 + @mtkbuild sys = ODEProblem(D(x) ~ t, t) + prob = @test_nowarn ODEProblem(sys, nothing, (0.0, 1.0)) + @test_nowarn solve(prob) +end From bbc8bf788d2d069faadb752a0e3e1ebda680fff0 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 19 Sep 2024 17:41:59 +0530 Subject: [PATCH 0015/2176] fix: handle non-symbolic and `nothing` u0 for `DiscreteProblem` --- src/systems/discrete_system/discrete_system.jl | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/systems/discrete_system/discrete_system.jl b/src/systems/discrete_system/discrete_system.jl index 4a2e9bca97..7103cfca80 100644 --- a/src/systems/discrete_system/discrete_system.jl +++ b/src/systems/discrete_system/discrete_system.jl @@ -247,6 +247,13 @@ function process_DiscreteProblem(constructor, sys::DiscreteSystem, u0map, paramm dvs = unknowns(sys) ps = parameters(sys) + if eltype(u0map) <: Number + u0map = unknowns(sys) .=> vec(u0map) + end + if u0map === nothing || isempty(u0map) + u0map = Dict() + end + trueu0map = Dict() for (k, v) in u0map k = unwrap(k) From f84e5715a366f05c7a096c99da3eb9009292ad17 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 19 Sep 2024 17:42:12 +0530 Subject: [PATCH 0016/2176] test: test passing `nothing` to u0 for all system types --- test/discrete_system.jl | 8 ++++++++ test/nonlinearsystem.jl | 7 +++++++ test/odesystem.jl | 2 +- test/optimizationsystem.jl | 7 +++++++ test/sdesystem.jl | 8 ++++++++ 5 files changed, 31 insertions(+), 1 deletion(-) diff --git a/test/discrete_system.jl b/test/discrete_system.jl index f8ed0a911f..7d8c0d9678 100644 --- a/test/discrete_system.jl +++ b/test/discrete_system.jl @@ -271,3 +271,11 @@ end k = ShiftIndex(t) @named sys = DiscreteSystem([x ~ x^2 + y^2, y ~ x(k - 1) + y(k - 1)], t) @test_throws ["algebraic equations", "not yet supported"] structural_simplify(sys) + +@testset "Passing `nothing` to `u0`" begin + @variables x(t) = 1 + k = ShiftIndex() + @mtkbuild sys = DiscreteSystem([x(k) ~ x(k - 1) + 1], t) + prob = @test_nowarn DiscreteProblem(sys, nothing, (0.0, 1.0)) + @test_nowarn solve(prob, FunctionMap()) +end diff --git a/test/nonlinearsystem.jl b/test/nonlinearsystem.jl index 841a026d0f..a71d34a880 100644 --- a/test/nonlinearsystem.jl +++ b/test/nonlinearsystem.jl @@ -318,3 +318,10 @@ sys = structural_simplify(ns; conservative = true) sol = solve(prob, NewtonRaphson()) @test sol[x] ≈ sol[y] ≈ sol[z] ≈ -3 end + +@testset "Passing `nothing` to `u0`" begin + @variables x = 1 + @mtkbuild sys = NonlinearSystem([0 ~ x^2 - x^3 + 3]) + prob = @test_nowarn NonlinearProblem(sys, nothing) + @test_nowarn solve(prob) +end diff --git a/test/odesystem.jl b/test/odesystem.jl index c229eebe28..0837573baa 100644 --- a/test/odesystem.jl +++ b/test/odesystem.jl @@ -1390,7 +1390,7 @@ end @testset "Passing `nothing` to `u0`" begin @variables x(t) = 1 - @mtkbuild sys = ODEProblem(D(x) ~ t, t) + @mtkbuild sys = ODESystem(D(x) ~ t, t) prob = @test_nowarn ODEProblem(sys, nothing, (0.0, 1.0)) @test_nowarn solve(prob) end diff --git a/test/optimizationsystem.jl b/test/optimizationsystem.jl index f182e3ef87..426d6d5de0 100644 --- a/test/optimizationsystem.jl +++ b/test/optimizationsystem.jl @@ -340,3 +340,10 @@ end prob.f.cons_h(H3, [1.0, 1.0], [1.0, 100.0]) @test prob.f.cons_h([1.0, 1.0], [1.0, 100.0]) == H3 end + +@testset "Passing `nothing` to `u0`" begin + @variables x = 1.0 + @mtkbuild sys = OptimizationSystem((x - 3)^2, [x], []) + prob = @test_nowarn OptimizationProblem(sys, nothing) + @test_nowarn solve(prob, NelderMead()) +end diff --git a/test/sdesystem.jl b/test/sdesystem.jl index 5628772041..cae9ec9376 100644 --- a/test/sdesystem.jl +++ b/test/sdesystem.jl @@ -776,3 +776,11 @@ end prob = SDEProblem(de, u0map, (0.0, 100.0), parammap) @test solve(prob, SOSRI()).retcode == ReturnCode.Success end + +@testset "Passing `nothing` to `u0`" begin + @variables x(t) = 1 + @brownian b + @mtkbuild sys = System([D(x) ~ x + b], t) + prob = @test_nowarn SDEProblem(sys, nothing, (0.0, 1.0)) + @test_nowarn solve(prob, ImplicitEM()) +end From a25b43a25dbd0507df3d72c9580998825d6fdb34 Mon Sep 17 00:00:00 2001 From: CompatHelper Julia Date: Sat, 21 Sep 2024 00:22:18 +0000 Subject: [PATCH 0017/2176] CompatHelper: bump compat for OptimizationOptimJL to 0.4 for package docs, (keep existing compat) --- docs/Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/Project.toml b/docs/Project.toml index 078df7d696..d2523f08ba 100644 --- a/docs/Project.toml +++ b/docs/Project.toml @@ -31,7 +31,7 @@ ModelingToolkit = "8.33, 9" NonlinearSolve = "3" Optim = "1.7" Optimization = "3.9" -OptimizationOptimJL = "0.1" +OptimizationOptimJL = "0.1, 0.4" OrdinaryDiffEq = "6.31" Plots = "1.36" SciMLStructures = "1.1" From d19de0f65839c36fe084bf40dd18a52b27f186af Mon Sep 17 00:00:00 2001 From: CompatHelper Julia Date: Sat, 21 Sep 2024 00:22:22 +0000 Subject: [PATCH 0018/2176] CompatHelper: bump compat for Optimization to 4 for package docs, (keep existing compat) --- docs/Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/Project.toml b/docs/Project.toml index 078df7d696..4029f6f293 100644 --- a/docs/Project.toml +++ b/docs/Project.toml @@ -30,7 +30,7 @@ DynamicQuantities = "^0.11.2, 0.12, 1" ModelingToolkit = "8.33, 9" NonlinearSolve = "3" Optim = "1.7" -Optimization = "3.9" +Optimization = "3.9, 4" OptimizationOptimJL = "0.1" OrdinaryDiffEq = "6.31" Plots = "1.36" From 52a1ebfd71e3ab8bded0c69f647502e4ee7b0446 Mon Sep 17 00:00:00 2001 From: Christopher Rackauckas Date: Sat, 21 Sep 2024 14:40:53 +0200 Subject: [PATCH 0019/2176] Update Project.toml --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 248ed16e6d..85b970f181 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "ModelingToolkit" uuid = "961ee093-0014-501f-94e3-6117800e7a78" authors = ["Yingbo Ma ", "Chris Rackauckas and contributors"] -version = "9.40.0" +version = "9.40.1" [deps] AbstractTrees = "1520ce14-60c1-5f80-bbc7-55ef81b5835c" From 00c56f218d4d7f06fc0c4e23be154fc48eed188d Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 23 Sep 2024 13:06:48 +0530 Subject: [PATCH 0020/2176] feat: reorder parameters in `complete(sys)` according to portions --- src/systems/abstractsystem.jl | 38 +++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index 9aa4bfc92d..e8ec8cc06f 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -882,6 +882,44 @@ namespace its subsystems or variables, i.e. `isequal(complete(sys).v.i, v.i)`. function complete(sys::AbstractSystem; split = true) if split && has_index_cache(sys) @set! sys.index_cache = IndexCache(sys) + all_ps = parameters(sys) + if !isempty(all_ps) + # reorder parameters by portions + ps_split = reorder_parameters(sys, all_ps) + # if there are no tunables, vcat them + if isempty(get_index_cache(sys).tunable_idx) + ordered_ps = reduce(vcat, ps_split) + else + # if there are tunables, they will all be in `ps_split[1]` + # and the arrays will have been scalarized + ordered_ps = eltype(all_ps)[] + i = 1 + # go through all the tunables + while i <= length(ps_split[1]) + sym = ps_split[1][i] + # if the sym is not a scalarized array symbolic OR it was already scalarized, + # just push it as-is + if !iscall(sym) || operation(sym) != getindex || + any(isequal(sym), all_ps) + push!(ordered_ps, sym) + i += 1 + continue + end + # the next `length(sym)` symbols should be scalarized versions of the same + # array symbolic + if !allequal(first(arguments(x)) + for x in view(ps_split[1], i:(i + length(sym) - 1))) + error("This should not be possible. Please open an issue in ModelingToolkit.jl with an MWE and stacktrace.") + end + arrsym = first(arguments(sym)) + push!(ordered_ps, arrsym) + i += length(arrsym) + end + ordered_ps = vcat( + ordered_ps, reduce(vcat, ps_split[2:end]; init = eltype(ordered_ps)[])) + end + @set! sys.ps = ordered_ps + end end if isdefined(sys, :initializesystem) && get_initializesystem(sys) !== nothing @set! sys.initializesystem = complete(get_initializesystem(sys); split) From 8f6828c3fcf22a493050b8b6358bb35d3e88003f Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 23 Sep 2024 13:07:56 +0530 Subject: [PATCH 0021/2176] test: ensure `tunable_parameters` are now correctly ordered --- src/variables.jl | 7 ++++++- test/index_cache.jl | 22 +++++++++++++++++++++- test/runtests.jl | 2 +- 3 files changed, 28 insertions(+), 3 deletions(-) diff --git a/src/variables.jl b/src/variables.jl index 767dec686b..4e5f860513 100644 --- a/src/variables.jl +++ b/src/variables.jl @@ -381,7 +381,12 @@ Create a tunable parameter by @parameters u [tunable=true] ``` -See also [`getbounds`](@ref), [`istunable`](@ref) +For systems created with `split = true` (the default) and `default = true` passed to this function, the order +of parameters returned is the order in which they are stored in the tunables portion of `MTKParameters`. Note +that array variables will not be scalarized. To obtain the flattened representation of the tunables portion, +call `Symbolics.scalarize(tunable_parameters(sys))` and concatenate the resulting arrays. + +See also [`getbounds`](@ref), [`istunable`](@ref), [`MTKParameters`](@ref), [`complete`](@ref) """ function tunable_parameters(sys, p = parameters(sys); default = true) filter(x -> istunable(x, default), p) diff --git a/test/index_cache.jl b/test/index_cache.jl index 3bee1db381..d1301c260b 100644 --- a/test/index_cache.jl +++ b/test/index_cache.jl @@ -1,4 +1,4 @@ -using ModelingToolkit, SymbolicIndexingInterface +using ModelingToolkit, SymbolicIndexingInterface, SciMLStructures using ModelingToolkit: t_nounits as t # Ensure indexes of array symbolics are cached appropriately @@ -43,3 +43,23 @@ ic = ModelingToolkit.get_index_cache(sys) @test isequal(ic.symbol_to_variable[:x], x) @test isequal(ic.symbol_to_variable[:y], y) @test isequal(ic.symbol_to_variable[:z], z) + +@testset "tunable_parameters is ordered" begin + @parameters p q[1:3] r[1:2, 1:2] s [tunable = false] + @named sys = ODESystem(Equation[], t, [], [p, q, r, s]) + sys = complete(sys) + @test all(splat(isequal), zip(tunable_parameters(sys), parameters(sys)[1:3])) + + offset = 1 + for par in tunable_parameters(sys) + idx = parameter_index(sys, par) + @test idx.portion isa SciMLStructures.Tunable + if Symbolics.isarraysymbolic(par) + @test vec(idx.idx) == offset:(offset + length(par) - 1) + else + @test idx.idx == offset + end + offset += length(par) + end +end + diff --git a/test/runtests.jl b/test/runtests.jl index e45e2dfe42..0a9cf3c4db 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -24,7 +24,6 @@ end @safetestset "Parsing Test" include("variable_parsing.jl") @safetestset "Simplify Test" include("simplify.jl") @safetestset "Direct Usage Test" include("direct.jl") - @safetestset "IndexCache Test" include("index_cache.jl") @safetestset "System Linearity Test" include("linearity.jl") @safetestset "Input Output Test" include("input_output_handling.jl") @safetestset "Clock Test" include("clock.jl") @@ -65,6 +64,7 @@ end if GROUP == "All" || GROUP == "InterfaceII" @testset "InterfaceII" begin + @safetestset "IndexCache Test" include("index_cache.jl") @safetestset "Variable Utils Test" include("variable_utils.jl") @safetestset "Variable Metadata Test" include("test_variable_metadata.jl") @safetestset "OptimizationSystem Test" include("optimizationsystem.jl") From f1b0036ca4d25031e6756e301264318dd13c8d05 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 23 Sep 2024 13:08:11 +0530 Subject: [PATCH 0022/2176] feat: add `reorder_dimension_by_tunables!` --- src/ModelingToolkit.jl | 2 +- src/systems/index_cache.jl | 52 ++++++++++++++++++++++++++++++++++++++ test/index_cache.jl | 29 +++++++++++++++++++++ 3 files changed, 82 insertions(+), 1 deletion(-) diff --git a/src/ModelingToolkit.jl b/src/ModelingToolkit.jl index 9a4b025d07..2237ba8952 100644 --- a/src/ModelingToolkit.jl +++ b/src/ModelingToolkit.jl @@ -278,6 +278,6 @@ export debug_system export Sample, Hold, Shift, ShiftIndex, sampletime, SampleTime export Clock, SolverStepClock, TimeDomain -export MTKParameters +export MTKParameters, reorder_dimension_by_tunables!, reorder_dimension_by_tunables end # module diff --git a/src/systems/index_cache.jl b/src/systems/index_cache.jl index 82a17d01e9..ce74fd6a88 100644 --- a/src/systems/index_cache.jl +++ b/src/systems/index_cache.jl @@ -496,3 +496,55 @@ end fntype_to_function_type(::Type{FnType{A, R, T}}) where {A, R, T} = T fntype_to_function_type(::Type{FnType{A, R, Nothing}}) where {A, R} = FunctionWrapper{R, A} fntype_to_function_type(::Type{FnType{A, R}}) where {A, R} = FunctionWrapper{R, A} + +""" + reorder_dimension_by_tunables!(dest::AbstractArray, sys::AbstractSystem, arr::AbstractArray, syms; dim = 1) + +Assuming the order of values in dimension `dim` of `arr` correspond to the order of tunable +parameters in the system, reorder them according to the order described in `syms`. `syms` must +be a permutation of `tunable_parameters(sys)`. The result is written to `dest`. The `size` of `dest` and +`arr` must be equal. Return `dest`. + +See also: [`MTKParameters`](@ref), [`tunable_parameters`](@ref), [`reorder_dimension_by_tunables`](@ref). +""" +function reorder_dimension_by_tunables!( + dest::AbstractArray, sys::AbstractSystem, arr::AbstractArray, syms; dim = 1) + if !iscomplete(sys) + throw(ArgumentError("A completed system is required. Call `complete` or `structural_simplify` on the system.")) + end + if !has_index_cache(sys) || (ic = get_index_cache(sys)) === nothing + throw(ArgumentError("The system does not have an index cache. Call `complete` or `structural_simplify` on the system with `split = true`.")) + end + if size(dest) != size(arr) + throw(ArgumentError("Source and destination arrays must have the same size. Got source array with size $(size(arr)) and destination with size $(size(dest)).")) + end + + dsti = 1 + for sym in syms + idx = parameter_index(ic, sym) + if !(idx.portion isa SciMLStructures.Tunable) + throw(ArgumentError("`syms` must be a permutation of `tunable_parameters(sys)`. Found $sym which is not a tunable parameter.")) + end + + dstidx = ntuple( + i -> i == dim ? (dsti:(dsti + length(sym) - 1)) : (:), Val(ndims(arr))) + destv = @view dest[dstidx...] + dsti += length(sym) + arridx = ntuple(i -> i == dim ? (idx.idx) : (:), Val(ndims(arr))) + srcv = @view arr[arridx...] + copyto!(destv, srcv) + end + return dest +end + +""" + reorder_dimension_by_tunables(sys::AbstractSystem, arr::AbstractArray, syms; dim = 1) + +Out-of-place version of [`reorder_dimension_by_tunables!`](@ref). +""" +function reorder_dimension_by_tunables( + sys::AbstractSystem, arr::AbstractArray, syms; dim = 1) + buffer = similar(arr) + reorder_dimension_by_tunables!(buffer, sys, arr, syms; dim) + return buffer +end diff --git a/test/index_cache.jl b/test/index_cache.jl index d1301c260b..c479075797 100644 --- a/test/index_cache.jl +++ b/test/index_cache.jl @@ -63,3 +63,32 @@ ic = ModelingToolkit.get_index_cache(sys) end end +@testset "reorder_dimension_by_tunables" begin + @parameters p q[1:3] r[1:2, 1:2] s [tunable = false] + @named sys = ODESystem(Equation[], t, [], [p, q, r, s]) + src = ones(8) + dst = zeros(8) + # system must be complete... + @test_throws ArgumentError reorder_dimension_by_tunables!(dst, sys, src, [p, q, r]) + @test_throws ArgumentError reorder_dimension_by_tunables(sys, src, [p, q, r]) + sys = complete(sys; split = false) + # with split = true... + @test_throws ArgumentError reorder_dimension_by_tunables!(dst, sys, src, [p, q, r]) + @test_throws ArgumentError reorder_dimension_by_tunables(sys, src, [p, q, r]) + sys = complete(sys) + # and the arrays must have matching size + @test_throws ArgumentError reorder_dimension_by_tunables!( + zeros(2, 4), sys, src, [p, q, r]) + + ps = MTKParameters(sys, [p => 1.0, q => 3ones(3), r => 4ones(2, 2), s => 0.0]) + src = ps.tunable + reorder_dimension_by_tunables!(dst, sys, src, [q, r, p]) + @test dst ≈ vcat(3ones(3), 4ones(4), 1.0) + @test reorder_dimension_by_tunables(sys, src, [r, p, q]) ≈ vcat(4ones(4), 1.0, 3ones(3)) + reorder_dimension_by_tunables!(dst, sys, src, [q[1], r[:, 1], q[2], r[:, 2], q[3], p]) + @test dst ≈ vcat(3.0, 4ones(2), 3.0, 4ones(2), 3.0, 1.0) + src = stack([copy(ps.tunable) for i in 1:5]; dims = 1) + dst = zeros(size(src)) + reorder_dimension_by_tunables!(dst, sys, src, [r, q, p]; dim = 2) + @test dst ≈ stack([vcat(4ones(4), 3ones(3), 1.0) for i in 1:5]; dims = 1) +end From af1f7c6d24909bacd76df51276914f06e01df3d8 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 23 Sep 2024 13:31:04 +0530 Subject: [PATCH 0023/2176] docs: add documentation for tunable parameters (re-)ordering --- docs/src/basics/FAQ.md | 59 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/docs/src/basics/FAQ.md b/docs/src/basics/FAQ.md index 44f97c2b25..bb3d01fc30 100644 --- a/docs/src/basics/FAQ.md +++ b/docs/src/basics/FAQ.md @@ -244,3 +244,62 @@ D = Differential(x) @variables y(x) @named sys = ODESystem([D(y) ~ x], x) ``` + +## Ordering of tunable parameters + +Tunable parameters are floating point parameters, not used in callbacks and not marked with `tunable = false` in their metadata. These are expected to be used with AD +and optimization libraries. As such, they are stored together in one `Vector{T}`. To obtain the ordering of tunable parameters in this buffer, use: + +```@docs +tunable_parameters +``` + +If you have an array in which a particular dimension is in the order of tunable parameters (e.g. the jacobian with respect to tunables) then that dimension of the +array can be reordered into the required permutation using the symbolic variables: + +```@docs +reorder_dimension_by_tunables! +reorder_dimension_by_tunables +``` + +For example: + +```@example reorder +using ModelingToolkit + +@parameters p q[1:3] r[1:2, 1:2] + +@named sys = ODESystem(Equation[], ModelingToolkit.t_nounits, [], [p, q, r]) +sys = complete(sys) +``` + +The canonicalized tunables portion of `MTKParameters` will be in the order of tunables: + +```@example reorder +using SciMLStructures: canonicalize, Tunable + +ps = MTKParameters(sys, [p => 1.0, q => [2.0, 3.0, 4.0], r => [5.0 6.0; 7.0 8.0]]) +arr = canonicalize(Tunable(), ps)[1] +``` + +We can reorder this to contain the value for `p`, then all values for `q`, then for `r` using: + +```@example reorder +reorder_dimension_by_tunables(sys, arr, [p, q, r]) +``` + +This also works with interleaved subarrays of symbolics: + +```@example reorder +reorder_dimension_by_tunables(sys, arr, [q[1], r[1, :], q[2], r[2, :], q[3], p]) +``` + +And arbitrary dimensions of higher dimensional arrays: + +```@example reorder +highdimarr = stack([i * arr for i in 1:5]; dims = 1) +``` + +```@example reorder +reorder_dimension_by_tunables(sys, highdimarr, [q[1:2], r[1, :], q[3], r[2, :], p]; dim = 2) +``` From efca396cacfb0791a5dacddb2c99395983c42876 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 23 Sep 2024 18:02:34 +0530 Subject: [PATCH 0024/2176] fix: allow accessing parameter dependencies via `Symbol` names --- src/systems/index_cache.jl | 11 ++++++++++- test/symbolic_indexing_interface.jl | 9 +++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/systems/index_cache.jl b/src/systems/index_cache.jl index 82a17d01e9..8e5235245c 100644 --- a/src/systems/index_cache.jl +++ b/src/systems/index_cache.jl @@ -277,7 +277,16 @@ function IndexCache(sys::AbstractSystem) dependent_pars = Set{BasicSymbolic}() for eq in parameter_dependencies(sys) - push!(dependent_pars, eq.lhs) + sym = eq.lhs + ttsym = default_toterm(sym) + rsym = renamespace(sys, sym) + rttsym = renamespace(sys, ttsym) + for s in [sym, ttsym, rsym, rttsym] + push!(dependent_pars, s) + if hasname(s) && (!iscall(s) || operation(s) != getindex) + symbol_to_variable[getname(s)] = sym + end + end end return IndexCache( diff --git a/test/symbolic_indexing_interface.jl b/test/symbolic_indexing_interface.jl index 2531f4eef4..0f42ed7d34 100644 --- a/test/symbolic_indexing_interface.jl +++ b/test/symbolic_indexing_interface.jl @@ -185,3 +185,12 @@ get_dep = @test_nowarn getu(prob, 2p1) @test getu(prob, p1)(prob) == getu(prob, :p1)(prob) @test getu(prob, p2)(prob) == getu(prob, :p2)(prob) end + +@testset "Parameter dependencies as symbols" begin + @variables x(t) = 1.0 + @parameters a=1 b + @named model = ODESystem(D(x) ~ x + a - b, t, parameter_dependencies = [b ~ a + 1]) + sys = complete(model) + prob = ODEProblem(sys, [], (0.0, 1.0)) + @test prob.ps[b] == prob.ps[:b] +end From da612c0c02c3304408101ce8ac443483505bd719 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 23 Sep 2024 15:15:51 +0530 Subject: [PATCH 0025/2176] test: make tests independent of parameter order --- test/discrete_system.jl | 2 +- test/labelledarrays.jl | 3 ++- test/model_parsing.jl | 6 +++--- test/precompile_test.jl | 2 +- 4 files changed, 7 insertions(+), 6 deletions(-) diff --git a/test/discrete_system.jl b/test/discrete_system.jl index 7d8c0d9678..b63f66d4e4 100644 --- a/test/discrete_system.jl +++ b/test/discrete_system.jl @@ -42,7 +42,7 @@ for df in [ # iip du = zeros(3) u = collect(1:3) - p = MTKParameters(syss, parameters(syss) .=> collect(1:5)) + p = MTKParameters(syss, [c, nsteps, δt, β, γ] .=> collect(1:5)) df.f(du, u, p, 0) @test du ≈ [0.01831563888873422, 0.9816849729159067, 4.999999388195359] diff --git a/test/labelledarrays.jl b/test/labelledarrays.jl index 0ec9e64f8d..c9ee7ee50b 100644 --- a/test/labelledarrays.jl +++ b/test/labelledarrays.jl @@ -87,5 +87,6 @@ u0 = @LArray [9998.0, 1.0, 1.0, 1.0] (:S, :I, :R, :C) problem = ODEProblem(SIR!, u0, tspan, p) sys = complete(modelingtoolkitize(problem)) -@test all(isequal.(parameters(sys), getproperty.(@variables(β, η, ω, φ, σ, μ), :val))) +@test all(any(isequal(x), parameters(sys)) +for x in ModelingToolkit.unwrap.(@variables(β, η, ω, φ, σ, μ))) @test all(isequal.(Symbol.(unknowns(sys)), Symbol.(@variables(S(t), I(t), R(t), C(t))))) diff --git a/test/model_parsing.jl b/test/model_parsing.jl index 40fb0a13f2..6332dc12a5 100644 --- a/test/model_parsing.jl +++ b/test/model_parsing.jl @@ -534,9 +534,9 @@ end @named else_in_sys = InsideTheBlock(flag = 3) else_in_sys = complete(else_in_sys) - @test getname.(parameters(if_in_sys)) == [:if_parameter, :eq] - @test getname.(parameters(elseif_in_sys)) == [:elseif_parameter, :eq] - @test getname.(parameters(else_in_sys)) == [:else_parameter, :eq] + @test sort(getname.(parameters(if_in_sys))) == [:eq, :if_parameter] + @test sort(getname.(parameters(elseif_in_sys))) == [:elseif_parameter, :eq] + @test sort(getname.(parameters(else_in_sys))) == [:else_parameter, :eq] @test getdefault(if_in_sys.if_parameter) == 100 @test getdefault(elseif_in_sys.elseif_parameter) == 101 diff --git a/test/precompile_test.jl b/test/precompile_test.jl index 6e31317de2..38051d9d49 100644 --- a/test/precompile_test.jl +++ b/test/precompile_test.jl @@ -10,7 +10,7 @@ using ODEPrecompileTest u = collect(1:3) p = ModelingToolkit.MTKParameters(ODEPrecompileTest.f_noeval_good.sys, - parameters(ODEPrecompileTest.f_noeval_good.sys) .=> collect(4:6)) + [:σ, :ρ, :β] .=> collect(4:6)) # These cases do not work, because they get defined in the ModelingToolkit's RGF cache. @test parentmodule(typeof(ODEPrecompileTest.f_bad.f.f_iip).parameters[2]) == ModelingToolkit From 083efb2f571ab9859b4e722fee7a8ce2ea073f5c Mon Sep 17 00:00:00 2001 From: Christopher Rackauckas Date: Tue, 24 Sep 2024 06:29:29 -0400 Subject: [PATCH 0026/2176] Update Project.toml --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 85b970f181..32a18bfdc0 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "ModelingToolkit" uuid = "961ee093-0014-501f-94e3-6117800e7a78" authors = ["Yingbo Ma ", "Chris Rackauckas and contributors"] -version = "9.40.1" +version = "9.41.0" [deps] AbstractTrees = "1520ce14-60c1-5f80-bbc7-55ef81b5835c" From 2bf2778f7448e7a944131ff929f3841d29a03cbc Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 25 Sep 2024 14:27:49 +0530 Subject: [PATCH 0027/2176] test: do not mark inversemodel tests as broken --- test/downstream/inversemodel.jl | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/test/downstream/inversemodel.jl b/test/downstream/inversemodel.jl index 56307aec31..8c2c6bebb9 100644 --- a/test/downstream/inversemodel.jl +++ b/test/downstream/inversemodel.jl @@ -151,13 +151,11 @@ x, _ = ModelingToolkit.get_u0_p(simplified_sys, op) p = ModelingToolkit.MTKParameters(simplified_sys, op) # If this somehow passes, mention it on # https://github.com/SciML/ModelingToolkit.jl/issues/2786 -@test_broken begin - matrices1 = Sf(x, p, 0) - matrices2, _ = Blocks.get_sensitivity(model, :y; op) # Test that we get the same result when calling the higher-level API - @test matrices1.f_x ≈ matrices2.A[1:7, 1:7] - nsys = get_named_sensitivity(model, :y; op) # Test that we get the same result when calling an even higher-level API - @test matrices2.A ≈ nsys.A -end +matrices1 = Sf(x, p, 0) +matrices2, _ = Blocks.get_sensitivity(model, :y; op) # Test that we get the same result when calling the higher-level API +@test_broken matrices1.f_x ≈ matrices2.A[1:7, 1:7] +nsys = get_named_sensitivity(model, :y; op) # Test that we get the same result when calling an even higher-level API +@test matrices2.A ≈ nsys.A # Test the same thing for comp sensitivities @@ -166,10 +164,8 @@ x, _ = ModelingToolkit.get_u0_p(simplified_sys, op) p = ModelingToolkit.MTKParameters(simplified_sys, op) # If this somehow passes, mention it on # https://github.com/SciML/ModelingToolkit.jl/issues/2786 -@test_broken begin - matrices1 = Sf(x, p, 0) - matrices2, _ = Blocks.get_comp_sensitivity(model, :y; op) # Test that we get the same result when calling the higher-level API - @test matrices1.f_x ≈ matrices2.A[1:7, 1:7] - nsys = get_named_comp_sensitivity(model, :y; op) # Test that we get the same result when calling an even higher-level API - @test matrices2.A ≈ nsys.A -end +matrices1 = Sf(x, p, 0) +matrices2, _ = Blocks.get_comp_sensitivity(model, :y; op) # Test that we get the same result when calling the higher-level API +@test_broken matrices1.f_x ≈ matrices2.A[1:7, 1:7] +nsys = get_named_comp_sensitivity(model, :y; op) # Test that we get the same result when calling an even higher-level API +@test matrices2.A ≈ nsys.A From 95e3d296aca95cced76419327d3b07ee87e5ec69 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 25 Sep 2024 12:39:03 +0530 Subject: [PATCH 0028/2176] fix: check scope when discovering variables from equations --- src/utils.jl | 1 + 1 file changed, 1 insertion(+) diff --git a/src/utils.jl b/src/utils.jl index 4426c8a799..25731f495a 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -483,6 +483,7 @@ end function collect_var!(unknowns, parameters, var, iv) isequal(var, iv) && return nothing + getmetadata(var, SymScope, LocalScope()) == LocalScope() || return nothing if iscalledparameter(var) callable = getcalledparameter(var) push!(parameters, callable) From 98889f8e75e1cf1b2f03a34fe4b0dcf4fa63ac4e Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 25 Sep 2024 12:44:19 +0530 Subject: [PATCH 0029/2176] test: test properly scoped discovery of parameters --- test/variable_scope.jl | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/test/variable_scope.jl b/test/variable_scope.jl index 8c6e358c23..853d5ce757 100644 --- a/test/variable_scope.jl +++ b/test/variable_scope.jl @@ -1,9 +1,8 @@ using ModelingToolkit -using ModelingToolkit: SymScope -using Symbolics: arguments, value +using ModelingToolkit: SymScope, t_nounits as t, D_nounits as D +using Symbolics: arguments, value, getname using Test -@independent_variables t @variables a b(t) c d e(t) b = ParentScope(b) @@ -52,7 +51,6 @@ end @test renamed([:foo :bar :baz], c) == Symbol("foo₊c") @test renamed([:foo :bar :baz], d) == :d -@independent_variables t @parameters a b c d e f p = [a ParentScope(b) @@ -84,3 +82,22 @@ arr1 = ODESystem(Equation[], t, [], []; name = :arr1) ∘ arr0 arr_ps = ModelingToolkit.getname.(parameters(arr1)) @test isequal(arr_ps[1], Symbol("xx")) @test isequal(arr_ps[2], Symbol("arr0₊xx")) + +function Foo(; name, p = 1) + @parameters p = p + @variables x(t) + return ODESystem(D(x) ~ p, t; name) +end +function Bar(; name, p = 2) + @parameters p = p + @variables x(t) + @named foo = Foo(; p) + return ODESystem(D(x) ~ p + t, t; systems = [foo], name) +end +@named bar = Bar() +bar = complete(bar) +@test length(parameters(bar)) == 2 +@test sort(getname.(parameters(bar))) == [:foo₊p, :p] +defs = ModelingToolkit.defaults(bar) +@test defs[bar.p] == 2 +@test isequal(defs[bar.foo.p], bar.p) From d0967f3b2442fc1c72f31d05425ef30794451652 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20St-Jean?= Date: Fri, 27 Sep 2024 10:22:30 +0900 Subject: [PATCH 0030/2176] Fix duplicate text --- docs/src/basics/InputOutput.md | 1 - 1 file changed, 1 deletion(-) diff --git a/docs/src/basics/InputOutput.md b/docs/src/basics/InputOutput.md index 4f42d9dcd7..4dc5a3d50f 100644 --- a/docs/src/basics/InputOutput.md +++ b/docs/src/basics/InputOutput.md @@ -55,7 +55,6 @@ We can inspect the state realization chosen by MTK x_sym ``` -as expected, `x` is chosen as the state variable. as expected, `x` is chosen as the state variable. ```@example inputoutput From 1c939fe51a00eed5036fe06a12e12d43ca89d739 Mon Sep 17 00:00:00 2001 From: Venkateshprasad <32921645+ven-k@users.noreply.github.com> Date: Wed, 18 Sep 2024 17:40:43 +0000 Subject: [PATCH 0031/2176] feat: allow users to set array length via args in `@mtkmodel` --- src/systems/model_parsing.jl | 79 ++++++++++++++++++++---------------- test/model_parsing.jl | 23 +++++++++++ 2 files changed, 66 insertions(+), 36 deletions(-) diff --git a/src/systems/model_parsing.jl b/src/systems/model_parsing.jl index fce2add6b7..df75965121 100644 --- a/src/systems/model_parsing.jl +++ b/src/systems/model_parsing.jl @@ -269,9 +269,12 @@ function parse_variable_def!(dict, mod, arg, varclass, kwargs, where_types; return var, def, Dict() end Expr(:ref, a, b...) => begin - indices = map(i -> UnitRange(i.args[2], i.args[end]), b) - parse_variable_def!(dict, mod, a, varclass, kwargs, where_types; - def, indices, type, meta) + if varclass == :parameters + var = :($a = $first(@parameters $a[$(b...)])) + else + var = :($a = $first(@variables $a[$(b...)])) + end + (:($a...), var), nothing, Dict() end _ => error("$arg cannot be parsed") end @@ -674,47 +677,51 @@ end function parse_variable_arg(dict, mod, arg, varclass, kwargs, where_types) vv, def, metadata_with_exprs = parse_variable_def!( dict, mod, arg, varclass, kwargs, where_types) - name = getname(vv) + if vv isa Tuple + return vv + else + name = getname(vv[1]) - varexpr = if haskey(metadata_with_exprs, VariableUnit) - unit = metadata_with_exprs[VariableUnit] - quote - $name = if $name === nothing - $setdefault($vv, $def) - else - try - $setdefault($vv, $convert_units($unit, $name)) - catch e - if isa(e, $(DynamicQuantities.DimensionError)) || - isa(e, $(Unitful.DimensionError)) - error("Unable to convert units for \'" * string(:($$vv)) * "\'") - elseif isa(e, MethodError) - error("No or invalid units provided for \'" * string(:($$vv)) * - "\'") - else - rethrow(e) + varexpr = if haskey(metadata_with_exprs, VariableUnit) + unit = metadata_with_exprs[VariableUnit] + quote + $name = if $name === nothing + $setdefault($vv, $def) + else + try + $setdefault($vv, $convert_units($unit, $name)) + catch e + if isa(e, $(DynamicQuantities.DimensionError)) || + isa(e, $(Unitful.DimensionError)) + error("Unable to convert units for \'" * string(:($$vv)) * "\'") + elseif isa(e, MethodError) + error("No or invalid units provided for \'" * string(:($$vv)) * + "\'") + else + rethrow(e) + end end end end - end - else - quote - $name = if $name === nothing - $setdefault($vv, $def) - else - $setdefault($vv, $name) + else + quote + $name = if $name === nothing + $setdefault($vv, $def) + else + $setdefault($vv, $name) + end end end - end - metadata_expr = Expr(:block) - for (k, v) in metadata_with_exprs - push!(metadata_expr.args, - :($name = $wrap($set_scalar_metadata($unwrap($name), $k, $v)))) - end + metadata_expr = Expr(:block) + for (k, v) in metadata_with_exprs + push!(metadata_expr.args, + :($name = $wrap($set_scalar_metadata($unwrap($name), $k, $v)))) + end - push!(varexpr.args, metadata_expr) - return vv isa Num ? name : :($name...), varexpr + push!(varexpr.args, metadata_expr) + return vv isa Num ? name : :($name...), varexpr + end end function handle_conditional_vars!( diff --git a/test/model_parsing.jl b/test/model_parsing.jl index 6332dc12a5..21cb4837ef 100644 --- a/test/model_parsing.jl +++ b/test/model_parsing.jl @@ -876,3 +876,26 @@ end end), false) end + +@testset "Array Length as an Input" begin + @mtkmodel VaryingLengthArray begin + @structural_parameters begin + N + M + end + @parameters begin + p1[1:N] + p2[1:N, 1:M] + end + @variables begin + v1(t)[1:N] + v2(t)[1:N, 1:M] + end + end + + @named model = VaryingLengthArray(N = 2, M = 3) + @test length(model.p1) == 2 + @test size(model.p2) == (2, 3) + @test length(model.v1) == 2 + @test size(model.v2) == (2, 3) +end From 51184bfc294ff7616af88ba8d9a8f9e6a84c41e3 Mon Sep 17 00:00:00 2001 From: Venkateshprasad <32921645+ven-k@users.noreply.github.com> Date: Mon, 23 Sep 2024 19:38:14 +0000 Subject: [PATCH 0032/2176] feat: support arbitrary length arrays with metadata and default --- src/systems/model_parsing.jl | 111 ++++++++++++++++++++++++++++++----- 1 file changed, 96 insertions(+), 15 deletions(-) diff --git a/src/systems/model_parsing.jl b/src/systems/model_parsing.jl index df75965121..ef89754dca 100644 --- a/src/systems/model_parsing.jl +++ b/src/systems/model_parsing.jl @@ -222,6 +222,96 @@ function parse_variable_def!(dict, mod, arg, varclass, kwargs, where_types; varclass, where_types, meta) return var, def, Dict() end + Expr(:tuple, Expr(:ref, a, b...), y) => begin + varname = Meta.isexpr(a, :call) ? a.args[1] : a + push!(kwargs, Expr(:kw, varname, nothing)) + if varclass == :parameters + var = :($varname = $first(@parameters $a[$(b...)] = ($varname, $y))) + else + var = :($varname = $first(@variables $a[$(b...)] = ($varname, $y))) + end + #TODO: update `dict` aka `Model.structure` with the metadata + (:($varname...), var), nothing, Dict() + end + Expr(:(=), Expr(:ref, a, b...), y) => begin + varname = Meta.isexpr(a, :call) ? a.args[1] : a + if Meta.isexpr(y, :tuple) + val, y = (y.args[1], y.args[2:end]) + push!(kwargs, Expr(:kw, varname, nothing)) + if varclass == :parameters + var = :($varname = $varname === nothing ? $val : $varname; + $varname = $first(@parameters $a[$(b...)] = ( + $varname, $(y...)))) + else + var = :($varname = $varname === nothing ? $val : $varname; + $varname = $first(@variables $a[$(b...)] = ( + $varname, $(y...)))) + end + else + push!(kwargs, Expr(:kw, varname, nothing)) + if varclass == :parameters + var = :($varname = $varname === nothing ? $y : $varname; $varname = $first(@parameters $a[$(b...)] = $varname)) + else + var = :($varname = $varname === nothing ? $y : $varname; $varname = $first(@variables $a[$(b...)] = $varname)) + end + end + #TODO: update `dict`` aka `Model.structure` with the metadata + (:($varname...), var), nothing, Dict() + end + Expr(:(=), Expr(:(::), Expr(:ref, a, b...), n), y) => begin + varname = Meta.isexpr(a, :call) ? a.args[1] : a + if Meta.isexpr(y, :tuple) + val, y = (y.args[1], y.args[2:end]) + push!(kwargs, Expr(:kw, varname, nothing)) + if varclass == :parameters + var = :( + $varname = $varname = $varname === nothing ? $val : $varname; + $varname = $first(@parameters $a[$(b...)]::$n = ($varname, $(y...))) + ) + else + var = :($varname = $varname === nothing ? $val : $varname; + $varname = $first(@variables $a[$(b...)]::$n = ( + $varname, $(y...)))) + end + else + push!(kwargs, Expr(:kw, varname, y)) + if varclass == :parameters + var = :($varname = $first(@parameters $a[$(b...)]::$n = $varname)) + else + var = :($varname = $first(@variables $a[$(b...)]::$n = $varname)) + end + end + #TODO: update `dict`` aka `Model.structure` with the metadata + (:($varname...), var), nothing, Dict() + end + Expr(:tuple, Expr(:(::), Expr(:ref, a, b...), n), y) => begin + varname = Meta.isexpr(a, :call) ? a.args[1] : a + push!(kwargs, Expr(:kw, varname, nothing)) + if varclass == :parameters + var = :($varname = $first(@parameters $a[$(b...)]::$n = ($varname, $y))) + else + var = :($varname = $first(@variables $a[$(b...)]::$n = ($varname, $y))) + end + #TODO: update `dict` aka `Model.structure` with the metadata + (:($varname...), var), nothing, Dict() + end + Expr(:ref, a, b...) => begin + varname = a isa Expr && a.head == :call ? a.args[1] : a + push!(kwargs, Expr(:kw, varname, nothing)) + if varclass == :parameters + var = :($varname = $first(@parameters $a[$(b...)] = $varname)) + elseif varclass == :variables + var = :($varname = $first(@variables $a[$(b...)] = $varname)) + else + throw("Symbolic array with arbitrary length is not handled for $varclass. + Please open an issue with an example.") + end + dict[varclass] = get!(dict, varclass) do + Dict{Symbol, Dict{Symbol, Any}}() + end + # dict[:kwargs][varname] = dict[varclass][varname] = Dict(:size => b) + (:($varname...), var), nothing, Dict() + end Expr(:(=), a, b) => begin Base.remove_linenums!(b) def, meta = parse_default(mod, b) @@ -268,14 +358,6 @@ function parse_variable_def!(dict, mod, arg, varclass, kwargs, where_types; end return var, def, Dict() end - Expr(:ref, a, b...) => begin - if varclass == :parameters - var = :($a = $first(@parameters $a[$(b...)])) - else - var = :($a = $first(@variables $a[$(b...)])) - end - (:($a...), var), nothing, Dict() - end _ => error("$arg cannot be parsed") end end @@ -677,11 +759,8 @@ end function parse_variable_arg(dict, mod, arg, varclass, kwargs, where_types) vv, def, metadata_with_exprs = parse_variable_def!( dict, mod, arg, varclass, kwargs, where_types) - if vv isa Tuple - return vv - else - name = getname(vv[1]) - + if !(vv isa Tuple) + name = getname(vv) varexpr = if haskey(metadata_with_exprs, VariableUnit) unit = metadata_with_exprs[VariableUnit] quote @@ -692,11 +771,11 @@ function parse_variable_arg(dict, mod, arg, varclass, kwargs, where_types) $setdefault($vv, $convert_units($unit, $name)) catch e if isa(e, $(DynamicQuantities.DimensionError)) || - isa(e, $(Unitful.DimensionError)) + isa(e, $(Unitful.DimensionError)) error("Unable to convert units for \'" * string(:($$vv)) * "\'") elseif isa(e, MethodError) error("No or invalid units provided for \'" * string(:($$vv)) * - "\'") + "\'") else rethrow(e) end @@ -721,6 +800,8 @@ function parse_variable_arg(dict, mod, arg, varclass, kwargs, where_types) push!(varexpr.args, metadata_expr) return vv isa Num ? name : :($name...), varexpr + else + return vv end end From 6cb3a28293a1848ea5b0d25b91475a2b2ad40be5 Mon Sep 17 00:00:00 2001 From: Venkateshprasad <32921645+ven-k@users.noreply.github.com> Date: Mon, 23 Sep 2024 19:39:06 +0000 Subject: [PATCH 0033/2176] test: mark broken tests These are related to: - Model.structure metadata related to variable/parameter array - Certain cases of symtypes (although neither case is unchanged by this PR) --- test/model_parsing.jl | 34 +++++++++++++++++++++++++++------- 1 file changed, 27 insertions(+), 7 deletions(-) diff --git a/test/model_parsing.jl b/test/model_parsing.jl index 21cb4837ef..c9868c8ebf 100644 --- a/test/model_parsing.jl +++ b/test/model_parsing.jl @@ -259,7 +259,8 @@ end @test all(collect(hasmetadata.(model.l, ModelingToolkit.VariableDescription))) @test all(lastindex.([model.a2, model.b2, model.d2, model.e2, model.h2]) .== 2) - @test size(model.l) == MockModel.structure[:parameters][:l][:size] == (2, 3) + @test size(model.l) == (2, 3) + @test_broken MockModel.structure[:parameters][:l][:size] == (2, 3) model = complete(model) @test getdefault(model.cval) == 1 @@ -302,8 +303,8 @@ end @test symtype(type_model.par2) == Int @test symtype(type_model.par3) == BigFloat @test symtype(type_model.par4) == Float64 - @test symtype(type_model.par5[1]) == BigFloat - @test symtype(type_model.par6[1]) == BigFloat + @test_broken symtype(type_model.par5[1]) == BigFloat + @test_broken symtype(type_model.par6[1]) == BigFloat @test symtype(type_model.par7[1, 1]) == BigFloat @test_throws TypeError TypeModel(; name = :throws, flag = 1) @@ -313,11 +314,10 @@ end @test_throws TypeError TypeModel(; name = :throws, par3 = true) @test_throws TypeError TypeModel(; name = :throws, par4 = true) # par7 should be an AbstractArray of BigFloat. - @test_throws MethodError TypeModel(; name = :throws, par7 = rand(Int, 3, 3)) # Test that array types are correctly added. @named type_model2 = TypeModel(; par5 = rand(BigFloat, 3)) - @test symtype(type_model2.par5[1]) == BigFloat + @test_broken symtype(type_model2.par5[1]) == BigFloat @named type_model3 = TypeModel(; par7 = rand(BigFloat, 3, 3)) @test symtype(type_model3.par7[1, 1]) == BigFloat @@ -474,7 +474,8 @@ using ModelingToolkit: getdefault, scalarize @named model_with_component_array = ModelWithComponentArray() - @test eval(ModelWithComponentArray.structure[:parameters][:r][:unit]) == eval(u"Ω") + @test_broken eval(ModelWithComponentArray.structure[:parameters][:r][:unit]) == + eval(u"Ω") @test lastindex(parameters(model_with_component_array)) == 3 # Test the constant `k`. Manually k's value should be kept in sync here @@ -892,10 +893,29 @@ end v2(t)[1:N, 1:M] end end - + @named model = VaryingLengthArray(N = 2, M = 3) @test length(model.p1) == 2 @test size(model.p2) == (2, 3) @test length(model.v1) == 2 @test size(model.v2) == (2, 3) + + @mtkmodel WithMetadata begin + @structural_parameters begin + N + end + @parameters begin + p_only_default[1:N] = 101 + p_only_metadata[1:N], [description = "this only has metadata"] + p_both_default_and_metadata[1:N] = 102, + [description = "this has both default value and metadata"] + end + end + + @named with_metadata = WithMetadata(N = 10) + @test getdefault(with_metadata.p_only_default) == 101 + @test getdescription(with_metadata.p_only_metadata) == "this only has metadata" + @test getdefault(with_metadata.p_both_default_and_metadata) == 102 + @test getdescription(with_metadata.p_both_default_and_metadata) == + "this has both default value and metadata" end From 98471b6b96f762b8f6c0b02074e8d16dbbc4ca4c Mon Sep 17 00:00:00 2001 From: Venkateshprasad <32921645+ven-k@users.noreply.github.com> Date: Mon, 23 Sep 2024 20:32:56 +0000 Subject: [PATCH 0034/2176] docs: use symbolic array with arbitray length in ModelC example --- docs/src/basics/MTKLanguage.md | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/docs/src/basics/MTKLanguage.md b/docs/src/basics/MTKLanguage.md index a2fb7d0870..44fe8bbc07 100644 --- a/docs/src/basics/MTKLanguage.md +++ b/docs/src/basics/MTKLanguage.md @@ -63,13 +63,14 @@ end @structural_parameters begin f = sin N = 2 + M = 3 end begin v_var = 1.0 end @variables begin v(t) = v_var - v_array(t)[1:2, 1:3] + v_array(t)[1:N, 1:M] v_for_defaults(t) end @extend ModelB(; p1) @@ -310,10 +311,10 @@ end - `:defaults`: Dictionary of variables and default values specified in the `@defaults`. - `:extend`: The list of extended unknowns, name given to the base system, and name of the base system. - `:structural_parameters`: Dictionary of structural parameters mapped to their metadata. - - `:parameters`: Dictionary of symbolic parameters mapped to their metadata. For - parameter arrays, length is added to the metadata as `:size`. - - `:variables`: Dictionary of symbolic variables mapped to their metadata. For - variable arrays, length is added to the metadata as `:size`. + - `:parameters`: Dictionary of symbolic parameters mapped to their metadata. Metadata of + the parameter arrays is, for now, omitted. + - `:variables`: Dictionary of symbolic variables mapped to their metadata. Metadata of + the variable arrays is, for now, omitted. - `:kwargs`: Dictionary of keyword arguments mapped to their metadata. - `:independent_variable`: Independent variable, which is added while generating the Model. - `:equations`: List of equations (represented as strings). @@ -324,10 +325,10 @@ For example, the structure of `ModelC` is: julia> ModelC.structure Dict{Symbol, Any} with 10 entries: :components => Any[Union{Expr, Symbol}[:model_a, :ModelA], Union{Expr, Symbol}[:model_array_a, :ModelA, :(1:N)], Union{Expr, Symbol}[:model_array_b, :ModelA, :(1:N)]] - :variables => Dict{Symbol, Dict{Symbol, Any}}(:v=>Dict(:default=>:v_var, :type=>Real), :v_array=>Dict(:type=>Real, :size=>(2, 3)), :v_for_defaults=>Dict(:type=>Real)) + :variables => Dict{Symbol, Dict{Symbol, Any}}(:v=>Dict(:default=>:v_var, :type=>Real), :v_for_defaults=>Dict(:type=>Real)) :icon => URI("https://github.com/SciML/SciMLDocs/blob/main/docs/src/assets/logo.png") - :kwargs => Dict{Symbol, Dict}(:f=>Dict(:value=>:sin), :N=>Dict(:value=>2), :v=>Dict{Symbol, Any}(:value=>:v_var, :type=>Real), :v_array=>Dict{Symbol, Union{Nothing, UnionAll}}(:value=>nothing, :type=>AbstractArray{Real}), :v_for_defaults=>Dict{Symbol, Union{Nothing, DataType}}(:value=>nothing, :type=>Real), :p1=>Dict(:value=>nothing)) - :structural_parameters => Dict{Symbol, Dict}(:f=>Dict(:value=>:sin), :N=>Dict(:value=>2)) + :kwargs => Dict{Symbol, Dict}(:f => Dict(:value => :sin), :N => Dict(:value => 2), :M => Dict(:value => 3), :v => Dict{Symbol, Any}(:value => :v_var, :type => Real), :v_for_defaults => Dict{Symbol, Union{Nothing, DataType}}(:value => nothing, :type => Real), :p1 => Dict(:value => nothing)), + :structural_parameters => Dict{Symbol, Dict}(:f => Dict(:value => :sin), :N => Dict(:value => 2), :M => Dict(:value => 3)) :independent_variable => t :constants => Dict{Symbol, Dict}(:c=>Dict{Symbol, Any}(:value=>1, :type=>Int64, :description=>"Example constant.")) :extend => Any[[:p2, :p1], Symbol("#mtkmodel__anonymous__ModelB"), :ModelB] From 687dc7fe653ba6312c5225e53ab90bd0a925e747 Mon Sep 17 00:00:00 2001 From: Venkateshprasad <32921645+ven-k@users.noreply.github.com> Date: Tue, 24 Sep 2024 17:32:39 +0000 Subject: [PATCH 0035/2176] feat(mtkmodel): handle unit conversion for array inputs --- src/systems/model_parsing.jl | 54 ++++++++++++++++++++++++++---------- 1 file changed, 39 insertions(+), 15 deletions(-) diff --git a/src/systems/model_parsing.jl b/src/systems/model_parsing.jl index ef89754dca..e5e1c54328 100644 --- a/src/systems/model_parsing.jl +++ b/src/systems/model_parsing.jl @@ -180,6 +180,16 @@ function update_kwargs_and_metadata!(dict, kwargs, a, def, indices, type, var, end end +function unit_handled_variable_value(mod, y, varname) + meta = parse_metadata(mod, y) + varval = if meta isa Nothing || get(meta, VariableUnit, nothing) isa Nothing + varname + else + :($convert_units($(meta[VariableUnit]), $varname)) + end + return varval +end + function parse_variable_def!(dict, mod, arg, varclass, kwargs, where_types; def = nothing, indices::Union{Vector{UnitRange{Int}}, Nothing} = nothing, type::Type = Real, meta = Dict{DataType, Expr}()) @@ -225,10 +235,11 @@ function parse_variable_def!(dict, mod, arg, varclass, kwargs, where_types; Expr(:tuple, Expr(:ref, a, b...), y) => begin varname = Meta.isexpr(a, :call) ? a.args[1] : a push!(kwargs, Expr(:kw, varname, nothing)) + varval = unit_handled_variable_value(mod, y, varname) if varclass == :parameters - var = :($varname = $first(@parameters $a[$(b...)] = ($varname, $y))) + var = :($varname = $first(@parameters $a[$(b...)] = ($varval, $y))) else - var = :($varname = $first(@variables $a[$(b...)] = ($varname, $y))) + var = :($varname = $first(@variables $a[$(b...)] = ($varval, $y))) end #TODO: update `dict` aka `Model.structure` with the metadata (:($varname...), var), nothing, Dict() @@ -236,16 +247,17 @@ function parse_variable_def!(dict, mod, arg, varclass, kwargs, where_types; Expr(:(=), Expr(:ref, a, b...), y) => begin varname = Meta.isexpr(a, :call) ? a.args[1] : a if Meta.isexpr(y, :tuple) + varval = unit_handled_variable_value(mod, y, varname) val, y = (y.args[1], y.args[2:end]) push!(kwargs, Expr(:kw, varname, nothing)) if varclass == :parameters var = :($varname = $varname === nothing ? $val : $varname; $varname = $first(@parameters $a[$(b...)] = ( - $varname, $(y...)))) + $varval, $(y...)))) else var = :($varname = $varname === nothing ? $val : $varname; $varname = $first(@variables $a[$(b...)] = ( - $varname, $(y...)))) + $varval, $(y...)))) end else push!(kwargs, Expr(:kw, varname, nothing)) @@ -260,25 +272,24 @@ function parse_variable_def!(dict, mod, arg, varclass, kwargs, where_types; end Expr(:(=), Expr(:(::), Expr(:ref, a, b...), n), y) => begin varname = Meta.isexpr(a, :call) ? a.args[1] : a + varval = unit_handled_variable_value(mod, y, varname) if Meta.isexpr(y, :tuple) val, y = (y.args[1], y.args[2:end]) push!(kwargs, Expr(:kw, varname, nothing)) if varclass == :parameters - var = :( - $varname = $varname = $varname === nothing ? $val : $varname; - $varname = $first(@parameters $a[$(b...)]::$n = ($varname, $(y...))) - ) + var = :($varname = $varname = $varname === nothing ? $val : $varname; + $varname = $first(@parameters $a[$(b...)]::$n = ($varval, $(y...)))) else var = :($varname = $varname === nothing ? $val : $varname; $varname = $first(@variables $a[$(b...)]::$n = ( - $varname, $(y...)))) + $varval, $(y...)))) end else push!(kwargs, Expr(:kw, varname, y)) if varclass == :parameters - var = :($varname = $first(@parameters $a[$(b...)]::$n = $varname)) + var = :($varname = $first(@parameters $a[$(b...)]::$n = $varval)) else - var = :($varname = $first(@variables $a[$(b...)]::$n = $varname)) + var = :($varname = $first(@variables $a[$(b...)]::$n = $varval)) end end #TODO: update `dict`` aka `Model.structure` with the metadata @@ -286,11 +297,12 @@ function parse_variable_def!(dict, mod, arg, varclass, kwargs, where_types; end Expr(:tuple, Expr(:(::), Expr(:ref, a, b...), n), y) => begin varname = Meta.isexpr(a, :call) ? a.args[1] : a + varval = unit_handled_variable_value(mod, y, varname) push!(kwargs, Expr(:kw, varname, nothing)) if varclass == :parameters - var = :($varname = $first(@parameters $a[$(b...)]::$n = ($varname, $y))) + var = :($varname = $first(@parameters $a[$(b...)]::$n = ($varval, $y))) else - var = :($varname = $first(@variables $a[$(b...)]::$n = ($varname, $y))) + var = :($varname = $first(@variables $a[$(b...)]::$n = ($varval, $y))) end #TODO: update `dict` aka `Model.structure` with the metadata (:($varname...), var), nothing, Dict() @@ -465,14 +477,24 @@ function parse_default(mod, a) end end -function parse_metadata(mod, a) +function parse_metadata(mod, a::Expr) + @info a typeof(a) MLStyle.@match a begin - Expr(:vect, eles...) => Dict(parse_metadata(mod, e) for e in eles) + Expr(:vect, b...) => Dict(parse_metadata(mod, m) for m in b) + Expr(:tuple, a, b...) => parse_metadata(mod, b) Expr(:(=), a, b) => Symbolics.option_to_metadata_type(Val(a)) => get_var(mod, b) _ => error("Cannot parse metadata $a") end end +function parse_metadata(mod, metadata::AbstractArray) + ret = Dict() + for m in metadata + merge!(ret, parse_metadata(mod, m)) + end + ret +end + function _set_var_metadata!(metadata_with_exprs, a, m, v::Expr) push!(metadata_with_exprs, m => v) a @@ -730,6 +752,7 @@ function parse_variable_arg!(exprs, vs, dict, mod, arg, varclass, kwargs, where_ end function convert_units(varunits::DynamicQuantities.Quantity, value) + value isa Nothing && return nothing DynamicQuantities.ustrip(DynamicQuantities.uconvert( DynamicQuantities.SymbolicUnits.as_quantity(varunits), value)) end @@ -741,6 +764,7 @@ function convert_units( end function convert_units(varunits::Unitful.FreeUnits, value) + value isa Nothing && return nothing Unitful.ustrip(varunits, value) end From ed5e0fbdb808fb1acda6ba775838f3dc31aec7c0 Mon Sep 17 00:00:00 2001 From: Venkateshprasad <32921645+ven-k@users.noreply.github.com> Date: Tue, 24 Sep 2024 19:20:12 +0000 Subject: [PATCH 0036/2176] refactor: parse typed and untyped arrays within same block --- src/systems/model_parsing.jl | 58 ++++++++---------------------------- test/model_parsing.jl | 2 +- 2 files changed, 13 insertions(+), 47 deletions(-) diff --git a/src/systems/model_parsing.jl b/src/systems/model_parsing.jl index e5e1c54328..6560f8260e 100644 --- a/src/systems/model_parsing.jl +++ b/src/systems/model_parsing.jl @@ -232,19 +232,21 @@ function parse_variable_def!(dict, mod, arg, varclass, kwargs, where_types; varclass, where_types, meta) return var, def, Dict() end - Expr(:tuple, Expr(:ref, a, b...), y) => begin + Expr(:tuple, Expr(:ref, a, b...), y) || Expr(:tuple, Expr(:(::), Expr(:ref, a, b...), n), y) => begin + isdefined(mod, :n) || (n = Real) varname = Meta.isexpr(a, :call) ? a.args[1] : a push!(kwargs, Expr(:kw, varname, nothing)) varval = unit_handled_variable_value(mod, y, varname) if varclass == :parameters - var = :($varname = $first(@parameters $a[$(b...)] = ($varval, $y))) + var = :($varname = $first(@parameters $a[$(b...)]::$n = ($varval, $y))) else - var = :($varname = $first(@variables $a[$(b...)] = ($varval, $y))) + var = :($varname = $first(@variables $a[$(b...)]::$n = ($varval, $y))) end #TODO: update `dict` aka `Model.structure` with the metadata (:($varname...), var), nothing, Dict() end - Expr(:(=), Expr(:ref, a, b...), y) => begin + Expr(:(=), Expr(:ref, a, b...), y) || Expr(:(=), Expr(:(::), Expr(:ref, a, b...), n), y) => begin + isdefined(mod, :n) || (n = Real) varname = Meta.isexpr(a, :call) ? a.args[1] : a if Meta.isexpr(y, :tuple) varval = unit_handled_variable_value(mod, y, varname) @@ -252,61 +254,26 @@ function parse_variable_def!(dict, mod, arg, varclass, kwargs, where_types; push!(kwargs, Expr(:kw, varname, nothing)) if varclass == :parameters var = :($varname = $varname === nothing ? $val : $varname; - $varname = $first(@parameters $a[$(b...)] = ( + $varname = $first(@parameters $a[$(b...)]::$n = ( $varval, $(y...)))) - else - var = :($varname = $varname === nothing ? $val : $varname; - $varname = $first(@variables $a[$(b...)] = ( - $varval, $(y...)))) - end - else - push!(kwargs, Expr(:kw, varname, nothing)) - if varclass == :parameters - var = :($varname = $varname === nothing ? $y : $varname; $varname = $first(@parameters $a[$(b...)] = $varname)) - else - var = :($varname = $varname === nothing ? $y : $varname; $varname = $first(@variables $a[$(b...)] = $varname)) - end - end - #TODO: update `dict`` aka `Model.structure` with the metadata - (:($varname...), var), nothing, Dict() - end - Expr(:(=), Expr(:(::), Expr(:ref, a, b...), n), y) => begin - varname = Meta.isexpr(a, :call) ? a.args[1] : a - varval = unit_handled_variable_value(mod, y, varname) - if Meta.isexpr(y, :tuple) - val, y = (y.args[1], y.args[2:end]) - push!(kwargs, Expr(:kw, varname, nothing)) - if varclass == :parameters - var = :($varname = $varname = $varname === nothing ? $val : $varname; - $varname = $first(@parameters $a[$(b...)]::$n = ($varval, $(y...)))) else var = :($varname = $varname === nothing ? $val : $varname; $varname = $first(@variables $a[$(b...)]::$n = ( $varval, $(y...)))) end else - push!(kwargs, Expr(:kw, varname, y)) + push!(kwargs, Expr(:kw, varname, nothing)) if varclass == :parameters - var = :($varname = $first(@parameters $a[$(b...)]::$n = $varval)) + var = :($varname = $varname === nothing ? $y : $varname; + $varname = $first(@parameters $a[$(b...)]::$n = $varname)) else - var = :($varname = $first(@variables $a[$(b...)]::$n = $varval)) + var = :($varname = $varname === nothing ? $y : $varname; + $varname = $first(@variables $a[$(b...)]::$n = $varname)) end end #TODO: update `dict`` aka `Model.structure` with the metadata (:($varname...), var), nothing, Dict() end - Expr(:tuple, Expr(:(::), Expr(:ref, a, b...), n), y) => begin - varname = Meta.isexpr(a, :call) ? a.args[1] : a - varval = unit_handled_variable_value(mod, y, varname) - push!(kwargs, Expr(:kw, varname, nothing)) - if varclass == :parameters - var = :($varname = $first(@parameters $a[$(b...)]::$n = ($varval, $y))) - else - var = :($varname = $first(@variables $a[$(b...)]::$n = ($varval, $y))) - end - #TODO: update `dict` aka `Model.structure` with the metadata - (:($varname...), var), nothing, Dict() - end Expr(:ref, a, b...) => begin varname = a isa Expr && a.head == :call ? a.args[1] : a push!(kwargs, Expr(:kw, varname, nothing)) @@ -478,7 +445,6 @@ function parse_default(mod, a) end function parse_metadata(mod, a::Expr) - @info a typeof(a) MLStyle.@match a begin Expr(:vect, b...) => Dict(parse_metadata(mod, m) for m in b) Expr(:tuple, a, b...) => parse_metadata(mod, b) diff --git a/test/model_parsing.jl b/test/model_parsing.jl index c9868c8ebf..471e1f4651 100644 --- a/test/model_parsing.jl +++ b/test/model_parsing.jl @@ -305,7 +305,7 @@ end @test symtype(type_model.par4) == Float64 @test_broken symtype(type_model.par5[1]) == BigFloat @test_broken symtype(type_model.par6[1]) == BigFloat - @test symtype(type_model.par7[1, 1]) == BigFloat + @test_broken symtype(type_model.par7[1, 1]) == BigFloat @test_throws TypeError TypeModel(; name = :throws, flag = 1) @test_throws TypeError TypeModel(; name = :throws, par0 = 1) From 563dedcb8a6d46c72f696aae4bf46214efb85c63 Mon Sep 17 00:00:00 2001 From: Venkateshprasad <32921645+ven-k@users.noreply.github.com> Date: Tue, 24 Sep 2024 19:45:12 +0000 Subject: [PATCH 0037/2176] fix: handle more typed array cases This also fixes more tests --- src/systems/model_parsing.jl | 27 ++++++++++++++------------- test/model_parsing.jl | 8 ++++---- 2 files changed, 18 insertions(+), 17 deletions(-) diff --git a/src/systems/model_parsing.jl b/src/systems/model_parsing.jl index 6560f8260e..09ef05c172 100644 --- a/src/systems/model_parsing.jl +++ b/src/systems/model_parsing.jl @@ -232,21 +232,21 @@ function parse_variable_def!(dict, mod, arg, varclass, kwargs, where_types; varclass, where_types, meta) return var, def, Dict() end - Expr(:tuple, Expr(:ref, a, b...), y) || Expr(:tuple, Expr(:(::), Expr(:ref, a, b...), n), y) => begin - isdefined(mod, :n) || (n = Real) + Expr(:tuple, Expr(:(::), Expr(:ref, a, b...), type), y) || Expr(:tuple, Expr(:ref, a, b...), y) => begin + (@isdefined type) || (type = Real) varname = Meta.isexpr(a, :call) ? a.args[1] : a push!(kwargs, Expr(:kw, varname, nothing)) varval = unit_handled_variable_value(mod, y, varname) if varclass == :parameters - var = :($varname = $first(@parameters $a[$(b...)]::$n = ($varval, $y))) + var = :($varname = $first(@parameters $a[$(b...)]::$type = ($varval, $y))) else - var = :($varname = $first(@variables $a[$(b...)]::$n = ($varval, $y))) + var = :($varname = $first(@variables $a[$(b...)]::$type = ($varval, $y))) end #TODO: update `dict` aka `Model.structure` with the metadata (:($varname...), var), nothing, Dict() end - Expr(:(=), Expr(:ref, a, b...), y) || Expr(:(=), Expr(:(::), Expr(:ref, a, b...), n), y) => begin - isdefined(mod, :n) || (n = Real) + Expr(:(=), Expr(:(::), Expr(:ref, a, b...), type), y) || Expr(:(=), Expr(:ref, a, b...), y) => begin + (@isdefined type) || (type = Real) varname = Meta.isexpr(a, :call) ? a.args[1] : a if Meta.isexpr(y, :tuple) varval = unit_handled_variable_value(mod, y, varname) @@ -254,33 +254,34 @@ function parse_variable_def!(dict, mod, arg, varclass, kwargs, where_types; push!(kwargs, Expr(:kw, varname, nothing)) if varclass == :parameters var = :($varname = $varname === nothing ? $val : $varname; - $varname = $first(@parameters $a[$(b...)]::$n = ( + $varname = $first(@parameters $a[$(b...)]::$type = ( $varval, $(y...)))) else var = :($varname = $varname === nothing ? $val : $varname; - $varname = $first(@variables $a[$(b...)]::$n = ( + $varname = $first(@variables $a[$(b...)]::$type = ( $varval, $(y...)))) end else push!(kwargs, Expr(:kw, varname, nothing)) if varclass == :parameters var = :($varname = $varname === nothing ? $y : $varname; - $varname = $first(@parameters $a[$(b...)]::$n = $varname)) + $varname = $first(@parameters $a[$(b...)]::$type = $varname)) else var = :($varname = $varname === nothing ? $y : $varname; - $varname = $first(@variables $a[$(b...)]::$n = $varname)) + $varname = $first(@variables $a[$(b...)]::$type = $varname)) end end #TODO: update `dict`` aka `Model.structure` with the metadata (:($varname...), var), nothing, Dict() end - Expr(:ref, a, b...) => begin + Expr(:(::), Expr(:ref, a, b...), type) || Expr(:ref, a, b...) => begin + (@isdefined type) || (type = Real) varname = a isa Expr && a.head == :call ? a.args[1] : a push!(kwargs, Expr(:kw, varname, nothing)) if varclass == :parameters - var = :($varname = $first(@parameters $a[$(b...)] = $varname)) + var = :($varname = $first(@parameters $a[$(b...)]::$type = $varname)) elseif varclass == :variables - var = :($varname = $first(@variables $a[$(b...)] = $varname)) + var = :($varname = $first(@variables $a[$(b...)]::$type = $varname)) else throw("Symbolic array with arbitrary length is not handled for $varclass. Please open an issue with an example.") diff --git a/test/model_parsing.jl b/test/model_parsing.jl index 471e1f4651..9d903842c2 100644 --- a/test/model_parsing.jl +++ b/test/model_parsing.jl @@ -303,9 +303,9 @@ end @test symtype(type_model.par2) == Int @test symtype(type_model.par3) == BigFloat @test symtype(type_model.par4) == Float64 - @test_broken symtype(type_model.par5[1]) == BigFloat - @test_broken symtype(type_model.par6[1]) == BigFloat - @test_broken symtype(type_model.par7[1, 1]) == BigFloat + @test symtype(type_model.par5[1]) == BigFloat + @test symtype(type_model.par6[1]) == BigFloat + @test symtype(type_model.par7[1, 1]) == BigFloat @test_throws TypeError TypeModel(; name = :throws, flag = 1) @test_throws TypeError TypeModel(; name = :throws, par0 = 1) @@ -317,7 +317,7 @@ end # Test that array types are correctly added. @named type_model2 = TypeModel(; par5 = rand(BigFloat, 3)) - @test_broken symtype(type_model2.par5[1]) == BigFloat + @test symtype(type_model2.par5[1]) == BigFloat @named type_model3 = TypeModel(; par7 = rand(BigFloat, 3, 3)) @test symtype(type_model3.par7[1, 1]) == BigFloat From 153a293ba25b6896b3a6beaaefb107b6c5f1ba4a Mon Sep 17 00:00:00 2001 From: CompatHelper Julia Date: Sat, 28 Sep 2024 00:22:47 +0000 Subject: [PATCH 0038/2176] CompatHelper: bump compat for DiffEqCallbacks to 4, (keep existing compat) --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 32a18bfdc0..ff17093ae9 100644 --- a/Project.toml +++ b/Project.toml @@ -81,7 +81,7 @@ DataInterpolations = "6.4" DataStructures = "0.17, 0.18" DeepDiffs = "1" DiffEqBase = "6.103.0" -DiffEqCallbacks = "2.16, 3" +DiffEqCallbacks = "2.16, 3, 4" DiffEqNoiseProcess = "5" DiffRules = "0.1, 1.0" Distributed = "1" From 39a63f9e7afa4fc6bccdc567a58f6099336a8e91 Mon Sep 17 00:00:00 2001 From: ArnoStrouwen Date: Tue, 1 Oct 2024 03:30:30 +0200 Subject: [PATCH 0039/2176] Start rewriting programmatically generating systems docs --- .../tutorials/programmatically_generating.md | 44 +++++++++++-------- 1 file changed, 26 insertions(+), 18 deletions(-) diff --git a/docs/src/tutorials/programmatically_generating.md b/docs/src/tutorials/programmatically_generating.md index 8b8b03027a..bd1d2359f1 100644 --- a/docs/src/tutorials/programmatically_generating.md +++ b/docs/src/tutorials/programmatically_generating.md @@ -1,42 +1,44 @@ # [Programmatically Generating and Scripting ODESystems](@id programmatically) -In the following tutorial we will discuss how to programmatically generate `ODESystem`s. -This is for cases where one is writing functions that generating `ODESystem`s, for example -if implementing a reader which parses some file format to generate an `ODESystem` (for example, -SBML), or for writing functions that transform an `ODESystem` (for example, if you write a -function that log-transforms a variable in an `ODESystem`). +In the following tutorial, we will discuss how to programmatically generate `ODESystem`s. +This is useful for functions that generate `ODESystem`s, for example +when you implement a reader that parses some file format, such as SBML, to generate an `ODESystem`. +It is also useful for functions that transform an `ODESystem`, for example +when you write a function that log-transforms a variable in an `ODESystem`. ## The Representation of a ModelingToolkit System ModelingToolkit is built on [Symbolics.jl](https://symbolics.juliasymbolics.org/dev/), a symbolic Computer Algebra System (CAS) developed in Julia. As such, all CAS functionality -is available on ModelingToolkit systems, such as symbolic differentiation, Groebner basis +is also available to be used on ModelingToolkit systems, such as symbolic differentiation, Groebner basis calculations, and whatever else you can think of. Under the hood, all ModelingToolkit variables and expressions are Symbolics.jl variables and expressions. Thus when scripting a ModelingToolkit system, one simply needs to generate Symbolics.jl variables and equations as demonstrated in the Symbolics.jl documentation. This looks like: ```@example scripting -using Symbolics -using ModelingToolkit: t_nounits as t, D_nounits as D - -@variables x(t) y(t) # Define variables +using ModelingToolkit # reexports Symbolics +@variables t x(t) y(t) # Define variables +D = Differential(t) eqs = [D(x) ~ y D(y) ~ x] # Define an array of equations ``` +However, ModelingToolkit has many higher-level features which will make scripting ModelingToolkit systems more convenient. +For example, as shown in the next section, defining your own independent variables and differentials is rarely needed. + ## The Non-DSL (non-`@mtkmodel`) Way of Defining an ODESystem -Using `@mtkmodel` is the preferred way of defining ODEs with MTK. However, let us -look at how we can define the same system without `@mtkmodel`. This is useful for -defining PDESystem etc. +Using `@mtkmodel`, like in the [getting started tutorial](@ref getting_started), +is the preferred way of defining ODEs with MTK. +However generating the contents of a `@mtkmodel` programmatically can be tedious. +Let us look at how we can define the same system without `@mtkmodel`. ```@example scripting using ModelingToolkit using ModelingToolkit: t_nounits as t, D_nounits as D - -@variables x(t) # independent and dependent variables -@parameters τ # parameters +@variables x(t) = 0.0 # independent and dependent variables +@parameters τ = 3.0 # parameters @constants h = 1 # constants eqs = [D(x) ~ (h - x) / τ] # create an array of equations @@ -45,10 +47,16 @@ eqs = [D(x) ~ (h - x) / τ] # create an array of equations # Perform the standard transformations and mark the model complete # Note: Complete models cannot be subsystems of other models! -fol_model = structural_simplify(model) +fol = structural_simplify(model) +prob = ODEProblem(fol, [], (0.0, 10.0), []) +using DifferentialEquations: solve +sol = solve(prob) + +using Plots +plot(sol) ``` -As you can see, generating an ODESystem is as simple as creating the array of equations +As you can see, generating an ODESystem is as simple as creating an array of equations and passing it to the `ODESystem` constructor. ## Understanding the Difference Between the Julia Variable and the Symbolic Variable From 928ee8ddb057e65d173214caab30b95be5b75f90 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 20 Sep 2024 15:45:57 +0530 Subject: [PATCH 0040/2176] feat: add `is_dde` flag for `ODESystem` and `SDESystem` --- src/systems/abstractsystem.jl | 1 + src/systems/diffeqs/abstractodesystem.jl | 23 +++++++++++++++++++++++ src/systems/diffeqs/odesystem.jl | 16 ++++++++++++---- src/systems/diffeqs/sdesystem.jl | 15 ++++++++++++--- test/dde.jl | 5 +++++ test/odesystem.jl | 8 ++++++++ 6 files changed, 61 insertions(+), 7 deletions(-) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index e8ec8cc06f..cd7bc52346 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -971,6 +971,7 @@ for prop in [:eqs :solved_unknowns :split_idxs :parent + :is_dde :index_cache :is_scalar_noise :isscheduled] diff --git a/src/systems/diffeqs/abstractodesystem.jl b/src/systems/diffeqs/abstractodesystem.jl index f7f94504e4..982cef0121 100644 --- a/src/systems/diffeqs/abstractodesystem.jl +++ b/src/systems/diffeqs/abstractodesystem.jl @@ -3,6 +3,29 @@ struct Schedule dummy_sub::Any end +""" + is_dde(sys::AbstractSystem) + +Return a boolean indicating whether a system represents a set of delay +differential equations. +""" +is_dde(sys::AbstractSystem) = has_is_dde(sys) && get_is_dde(sys) + +function _check_if_dde(eqs, iv, subsystems) + is_dde = any(ModelingToolkit.is_dde, subsystems) + if !is_dde + vs = Set() + for eq in eqs + vars!(vs, eq) + is_dde = any(vs) do sym + isdelay(unwrap(sym), iv) + end + is_dde && break + end + end + return is_dde +end + function filter_kwargs(kwargs) kwargs = Dict(kwargs) for key in keys(kwargs) diff --git a/src/systems/diffeqs/odesystem.jl b/src/systems/diffeqs/odesystem.jl index a1fb333092..d746318b09 100644 --- a/src/systems/diffeqs/odesystem.jl +++ b/src/systems/diffeqs/odesystem.jl @@ -141,6 +141,10 @@ struct ODESystem <: AbstractODESystem """ gui_metadata::Union{Nothing, GUIMetadata} """ + A boolean indicating if the given `ODESystem` represents a system of DDEs. + """ + is_dde::Bool + """ Cache for intermediate tearing state. """ tearing_state::Any @@ -178,7 +182,7 @@ struct ODESystem <: AbstractODESystem torn_matching, initializesystem, initialization_eqs, schedule, connector_type, preface, cevents, devents, parameter_dependencies, - metadata = nothing, gui_metadata = nothing, + metadata = nothing, gui_metadata = nothing, is_dde = false, tearing_state = nothing, substitutions = nothing, complete = false, index_cache = nothing, discrete_subsystems = nothing, solved_unknowns = nothing, @@ -198,7 +202,7 @@ struct ODESystem <: AbstractODESystem ctrl_jac, Wfact, Wfact_t, name, systems, defaults, guesses, torn_matching, initializesystem, initialization_eqs, schedule, connector_type, preface, cevents, devents, parameter_dependencies, metadata, - gui_metadata, tearing_state, substitutions, complete, index_cache, + gui_metadata, is_dde, tearing_state, substitutions, complete, index_cache, discrete_subsystems, solved_unknowns, split_idxs, parent) end end @@ -223,7 +227,8 @@ function ODESystem(deqs::AbstractVector{<:Equation}, iv, dvs, ps; parameter_dependencies = Equation[], checks = true, metadata = nothing, - gui_metadata = nothing) + gui_metadata = nothing, + is_dde = nothing) name === nothing && throw(ArgumentError("The `name` keyword must be provided. Please consider using the `@named` macro")) @assert all(control -> any(isequal.(control, ps)), controls) "All controls must also be parameters." @@ -266,12 +271,15 @@ function ODESystem(deqs::AbstractVector{<:Equation}, iv, dvs, ps; disc_callbacks = SymbolicDiscreteCallbacks(discrete_events) parameter_dependencies, ps′ = process_parameter_dependencies( parameter_dependencies, ps′) + if is_dde === nothing + is_dde = _check_if_dde(deqs, iv′, systems) + end ODESystem(Threads.atomic_add!(SYSTEM_COUNT, UInt(1)), deqs, iv′, dvs′, ps′, tspan, var_to_name, ctrl′, observed, tgrad, jac, ctrl_jac, Wfact, Wfact_t, name, systems, defaults, guesses, nothing, initializesystem, initialization_eqs, schedule, connector_type, preface, cont_callbacks, disc_callbacks, parameter_dependencies, - metadata, gui_metadata, checks = checks) + metadata, gui_metadata, is_dde, checks = checks) end function ODESystem(eqs, iv; kwargs...) diff --git a/src/systems/diffeqs/sdesystem.jl b/src/systems/diffeqs/sdesystem.jl index ee16728133..1b368f7df3 100644 --- a/src/systems/diffeqs/sdesystem.jl +++ b/src/systems/diffeqs/sdesystem.jl @@ -133,6 +133,10 @@ struct SDESystem <: AbstractODESystem be `true` when `noiseeqs isa Vector`. """ is_scalar_noise::Bool + """ + A boolean indicating if the given `ODESystem` represents a system of DDEs. + """ + is_dde::Bool isscheduled::Bool function SDESystem(tag, deqs, neqs, iv, dvs, ps, tspan, var_to_name, ctrls, observed, @@ -141,6 +145,7 @@ struct SDESystem <: AbstractODESystem ctrl_jac, Wfact, Wfact_t, name, systems, defaults, connector_type, cevents, devents, parameter_dependencies, metadata = nothing, gui_metadata = nothing, complete = false, index_cache = nothing, parent = nothing, is_scalar_noise = false, + is_dde = false, isscheduled = false; checks::Union{Bool, Int} = true) if checks == true || (checks & CheckComponents) > 0 @@ -165,7 +170,7 @@ struct SDESystem <: AbstractODESystem ctrl_jac, Wfact, Wfact_t, name, systems, defaults, connector_type, cevents, devents, parameter_dependencies, metadata, gui_metadata, complete, index_cache, parent, is_scalar_noise, - isscheduled) + is_dde, isscheduled) end end @@ -188,7 +193,8 @@ function SDESystem(deqs::AbstractVector{<:Equation}, neqs::AbstractArray, iv, dv complete = false, index_cache = nothing, parent = nothing, - is_scalar_noise = false) + is_scalar_noise = false, + is_dde = nothing) name === nothing && throw(ArgumentError("The `name` keyword must be provided. Please consider using the `@named` macro")) iv′ = value(iv) @@ -223,11 +229,14 @@ function SDESystem(deqs::AbstractVector{<:Equation}, neqs::AbstractArray, iv, dv disc_callbacks = SymbolicDiscreteCallbacks(discrete_events) parameter_dependencies, ps′ = process_parameter_dependencies( parameter_dependencies, ps′) + if is_dde === nothing + is_dde = _check_if_dde(deqs, iv′, systems) + end SDESystem(Threads.atomic_add!(SYSTEM_COUNT, UInt(1)), deqs, neqs, iv′, dvs′, ps′, tspan, var_to_name, ctrl′, observed, tgrad, jac, ctrl_jac, Wfact, Wfact_t, name, systems, defaults, connector_type, cont_callbacks, disc_callbacks, parameter_dependencies, metadata, gui_metadata, - complete, index_cache, parent, is_scalar_noise; checks = checks) + complete, index_cache, parent, is_scalar_noise, is_dde; checks = checks) end function SDESystem(sys::ODESystem, neqs; kwargs...) diff --git a/test/dde.jl b/test/dde.jl index 439db46fe9..90f02cd963 100644 --- a/test/dde.jl +++ b/test/dde.jl @@ -38,6 +38,7 @@ eqs = [D(x₀) ~ (v0 / (1 + beta0 * (x₂(t - tau)^2))) * (p0 - q0) * x₀ - d0 (v1 / (1 + beta1 * (x₂(t - tau)^2))) * (p1 - q1) * x₁ - d1 * x₁ D(x₂(t)) ~ (v1 / (1 + beta1 * (x₂(t - tau)^2))) * (1 - p1 + q1) * x₁ - d2 * x₂(t)] @mtkbuild sys = System(eqs, t) +@test ModelingToolkit.is_dde(sys) prob = DDEProblem(sys, [x₀ => 1.0, x₁ => 1.0, x₂(t) => 1.0], tspan, @@ -77,6 +78,7 @@ sol = solve(prob, RKMil()) τ = 1.0 eqs = [D(x(t)) ~ a * x(t) + b * x(t - τ) + c + (α * x(t) + γ) * η] @mtkbuild sys = System(eqs, t) +@test ModelingToolkit.is_dde(sys) @test equations(sys) == [D(x(t)) ~ a * x(t) + b * x(t - τ) + c] @test isequal(ModelingToolkit.get_noiseeqs(sys), [α * x(t) + γ]) prob_mtk = SDDEProblem(sys, [x(t) => 1.0 + t], tspan; constant_lags = (τ,)); @@ -100,8 +102,11 @@ end eqs = [osc1.jcn ~ osc2.delx, osc2.jcn ~ osc1.delx] @named coupledOsc = System(eqs, t) +@test ModelingToolkit.is_dde(coupledOsc) @named coupledOsc = compose(coupledOsc, systems) +@test ModelingToolkit.is_dde(coupledOsc) @named coupledOsc2 = System(eqs, t; systems) +@test ModelingToolkit.is_dde(coupledOsc2) for coupledOsc in [coupledOsc, coupledOsc2] local sys = structural_simplify(coupledOsc) @test length(equations(sys)) == 4 diff --git a/test/odesystem.jl b/test/odesystem.jl index fa0aa0acb5..748a6a4df5 100644 --- a/test/odesystem.jl +++ b/test/odesystem.jl @@ -1394,3 +1394,11 @@ end prob = @test_nowarn ODEProblem(sys, nothing, (0.0, 1.0)) @test_nowarn solve(prob) end + +@testset "ODEs are not DDEs" begin + @variables x(t) + @named sys = ODESystem(D(x) ~ x, t) + @test !ModelingToolkit.is_dde(sys) + @named sys2 = ODESystem(Equation[], t; systems = [sys]) + @test !ModelingToolkit.is_dde(sys) +end From ea26d5825b651df96669ec9b294eaf9622a52c1d Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 17 Sep 2024 14:10:30 +0530 Subject: [PATCH 0041/2176] feat: support observed function generation for DDEs --- src/systems/abstractsystem.jl | 4 ++++ src/systems/diffeqs/odesystem.jl | 30 ++++++++++++++++++++++++++++-- 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index cd7bc52346..9bf8d14b0e 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -805,6 +805,10 @@ function SymbolicIndexingInterface.observed( return let _fn = _fn fn1(u, p, t) = _fn(u, p, t) fn1(u, p::MTKParameters, t) = _fn(u, p..., t) + + # DDEs + fn1(u, histfn, p, t) = _fn(u, histfn, p, t) + fn1(u, histfn, p::MTKParameters, t) = _fn(u, histfn, p..., t) fn1 end else diff --git a/src/systems/diffeqs/odesystem.jl b/src/systems/diffeqs/odesystem.jl index d746318b09..a66e34b0c3 100644 --- a/src/systems/diffeqs/odesystem.jl +++ b/src/systems/diffeqs/odesystem.jl @@ -411,6 +411,9 @@ function build_explicit_observed_function(sys, ts; ts = [ts] end ts = unwrap.(ts) + if is_dde(sys) + ts = map(x -> delay_to_function(sys, x), ts) + end vars = Set() foreach(v -> vars!(vars, v; op), ts) @@ -483,8 +486,12 @@ function build_explicit_observed_function(sys, ts; end ts = map(t -> substitute(t, subs), ts) obsexprs = [] + for i in 1:maxidx eq = obs[i] + if is_dde(sys) + eq = delay_to_function(sys, eq) + end lhs = eq.lhs rhs = eq.rhs push!(obsexprs, lhs ← rhs) @@ -505,12 +512,17 @@ function build_explicit_observed_function(sys, ts; ps = (DestructuredArgs(unwrap.(ps), inbounds = !checkbounds),) end dvs = DestructuredArgs(unknowns(sys), inbounds = !checkbounds) + if is_dde(sys) + dvs = (dvs, DDE_HISTORY_FUN) + else + dvs = (dvs,) + end if inputs === nothing - args = param_only ? [ps..., ivs...] : [dvs, ps..., ivs...] + args = param_only ? [ps..., ivs...] : [dvs..., ps..., ivs...] else inputs = unwrap.(inputs) ipts = DestructuredArgs(inputs, inbounds = !checkbounds) - args = param_only ? [ipts, ps..., ivs...] : [dvs, ipts, ps..., ivs...] + args = param_only ? [ipts, ps..., ivs...] : [dvs..., ipts, ps..., ivs...] end pre = get_postprocess_fbody(sys) @@ -546,6 +558,20 @@ function build_explicit_observed_function(sys, ts; end end +function populate_delays(delays::Set, obsexprs, histfn, sys, sym) + _vars_util = vars(sym) + for v in _vars_util + v in delays && continue + iscall(v) && issym(operation(v)) && (args = arguments(v); length(args) == 1) && + iscall(only(args)) || continue + + idx = variable_index(sys, operation(v)(get_iv(sys))) + idx === nothing && error("Delay term $v is not an unknown in the system") + push!(delays, v) + push!(obsexprs, v ← histfn(only(args))[idx]) + end +end + function _eq_unordered(a, b) length(a) === length(b) || return false n = length(a) From 65777da8aa61d40d022554ea92dbfd3b445685d3 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 17 Sep 2024 15:07:09 +0530 Subject: [PATCH 0042/2176] test: test symbolic indexing of DDE solutions --- test/dde.jl | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/test/dde.jl b/test/dde.jl index 90f02cd963..7ab465e6f2 100644 --- a/test/dde.jl +++ b/test/dde.jl @@ -51,6 +51,8 @@ prob2 = DDEProblem(sys, constant_lags = [tau]) sol2_mtk = solve(prob2, alg, reltol = 1e-7, abstol = 1e-10) @test sol2_mtk.u[end] ≈ sol2.u[end] +@test_nowarn sol2_mtk[[x₀, x₁, x₂(t)]] +@test_nowarn sol2_mtk[[x₀, x₁, x₂(t - 0.1)]] using StochasticDelayDiffEq function hayes_modelf(du, u, h, p, t) @@ -102,7 +104,6 @@ end eqs = [osc1.jcn ~ osc2.delx, osc2.jcn ~ osc1.delx] @named coupledOsc = System(eqs, t) -@test ModelingToolkit.is_dde(coupledOsc) @named coupledOsc = compose(coupledOsc, systems) @test ModelingToolkit.is_dde(coupledOsc) @named coupledOsc2 = System(eqs, t; systems) @@ -112,3 +113,10 @@ for coupledOsc in [coupledOsc, coupledOsc2] @test length(equations(sys)) == 4 @test length(unknowns(sys)) == 4 end +sys = structural_simplify(coupledOsc) +prob = DDEProblem(sys, [], (0.0, 10.0); constant_lags = [sys.osc1.τ, sys.osc2.τ]) +sol = solve(prob, MethodOfSteps(Tsit5())) +obsfn = ModelingToolkit.build_explicit_observed_function( + sys, [sys.osc1.delx, sys.osc2.delx]) +@test_nowarn sol[[sys.osc1.delx, sys.osc2.delx]] +@test sol[sys.osc1.delx] ≈ sol(sol.t .- 0.01; idxs = sys.osc1.x) From 50148d4d8bef936c65fafaa286da8629e81c0f51 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 24 Sep 2024 11:25:15 +0530 Subject: [PATCH 0043/2176] fix: propagate `is_dde` through `flatten` and `compose` --- src/systems/abstractsystem.jl | 3 +++ src/systems/diffeqs/odesystem.jl | 1 + 2 files changed, 4 insertions(+) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index 9bf8d14b0e..35641ef77e 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -2960,6 +2960,9 @@ function compose(sys::AbstractSystem, systems::AbstractArray; name = nameof(sys) nsys == 0 && return sys @set! sys.name = name @set! sys.systems = [get_systems(sys); systems] + if has_is_dde(sys) + @set! sys.is_dde = _check_if_dde(equations(sys), get_iv(sys), get_systems(sys)) + end return sys end function compose(syss...; name = nameof(first(syss))) diff --git a/src/systems/diffeqs/odesystem.jl b/src/systems/diffeqs/odesystem.jl index a66e34b0c3..942c87afcc 100644 --- a/src/systems/diffeqs/odesystem.jl +++ b/src/systems/diffeqs/odesystem.jl @@ -382,6 +382,7 @@ function flatten(sys::ODESystem, noeqs = false) defaults = defaults(sys), name = nameof(sys), initialization_eqs = initialization_equations(sys), + is_dde = is_dde(sys), checks = false) end end From f8030f86a0ed7fe9faafbfb90ab231030f76d115 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 25 Sep 2024 14:06:19 +0530 Subject: [PATCH 0044/2176] refactor: allow changing name of parameter argument in `delay_to_function` --- src/systems/diffeqs/abstractodesystem.jl | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/src/systems/diffeqs/abstractodesystem.jl b/src/systems/diffeqs/abstractodesystem.jl index 982cef0121..06feaac688 100644 --- a/src/systems/diffeqs/abstractodesystem.jl +++ b/src/systems/diffeqs/abstractodesystem.jl @@ -242,29 +242,32 @@ function isdelay(var, iv) return false end const DDE_HISTORY_FUN = Sym{Symbolics.FnType{Tuple{Any, <:Real}, Vector{Real}}}(:___history___) -function delay_to_function(sys::AbstractODESystem, eqs = full_equations(sys)) +const DEFAULT_PARAMS_ARG = Sym{Any}(:ˍ₋arg3) +function delay_to_function( + sys::AbstractODESystem, eqs = full_equations(sys); history_arg = DEFAULT_PARAMS_ARG) delay_to_function(eqs, get_iv(sys), Dict{Any, Int}(operation(s) => i for (i, s) in enumerate(unknowns(sys))), parameters(sys), - DDE_HISTORY_FUN) + DDE_HISTORY_FUN; history_arg) end -function delay_to_function(eqs::Vector, iv, sts, ps, h) - delay_to_function.(eqs, (iv,), (sts,), (ps,), (h,)) +function delay_to_function(eqs::Vector, iv, sts, ps, h; history_arg = DEFAULT_PARAMS_ARG) + delay_to_function.(eqs, (iv,), (sts,), (ps,), (h,); history_arg) end -function delay_to_function(eq::Equation, iv, sts, ps, h) - delay_to_function(eq.lhs, iv, sts, ps, h) ~ delay_to_function(eq.rhs, iv, sts, ps, h) +function delay_to_function(eq::Equation, iv, sts, ps, h; history_arg = DEFAULT_PARAMS_ARG) + delay_to_function(eq.lhs, iv, sts, ps, h; history_arg) ~ delay_to_function( + eq.rhs, iv, sts, ps, h; history_arg) end -function delay_to_function(expr, iv, sts, ps, h) +function delay_to_function(expr, iv, sts, ps, h; history_arg = DEFAULT_PARAMS_ARG) if isdelay(expr, iv) v = operation(expr) time = arguments(expr)[1] idx = sts[v] - return term(getindex, h(Sym{Any}(:ˍ₋arg3), time), idx, type = Real) # BIG BIG HACK + return term(getindex, h(history_arg, time), idx, type = Real) # BIG BIG HACK elseif iscall(expr) return maketerm(typeof(expr), operation(expr), - map(x -> delay_to_function(x, iv, sts, ps, h), arguments(expr)), + map(x -> delay_to_function(x, iv, sts, ps, h; history_arg), arguments(expr)), metadata(expr)) else return expr From 0e50dcf68c844f559a9637315ea7acd6e1759692 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 25 Sep 2024 14:07:20 +0530 Subject: [PATCH 0045/2176] fix: fix and document `wrap_mtkparameters` --- src/systems/abstractsystem.jl | 66 ++++++++++++++++++++++++----------- 1 file changed, 46 insertions(+), 20 deletions(-) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index 35641ef77e..9b5a3ab6dd 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -398,50 +398,76 @@ function wrap_array_vars( end end -function wrap_mtkparameters(sys::AbstractSystem, isscalar::Bool) +const MTKPARAMETERS_ARG = Sym{Vector{Vector}}(:___mtkparameters___) + +""" + wrap_mtkparameters(sys::AbstractSystem, isscalar::Bool, p_start = 2) + +Return function(s) to be passed to the `wrap_code` keyword of `build_function` which +allow the compiled function to be called as `f(u, p, t)` where `p isa MTKParameters` +instead of `f(u, p..., t)`. `isscalar` denotes whether the function expression being +wrapped is for a scalar value. `p_start` is the index of the argument containing +the first parameter vector in the out-of-place version of the function. For example, +if a history function (DDEs) was passed before `p`, then the function before wrapping +would have the signature `f(u, h, p..., t)` and hence `p_start` would need to be `3`. + +The returned function is `identity` if the system does not have an `IndexCache`. +""" +function wrap_mtkparameters(sys::AbstractSystem, isscalar::Bool, p_start = 2) if has_index_cache(sys) && get_index_cache(sys) !== nothing offset = Int(is_time_dependent(sys)) if isscalar function (expr) - p = gensym(:p) + param_args = expr.args[p_start:(end - offset)] + param_buffer_idxs = findall(x -> x isa DestructuredArgs, param_args) + param_buffer_args = param_args[param_buffer_idxs] + destructured_mtkparams = DestructuredArgs( + [x.name for x in param_buffer_args], + MTKPARAMETERS_ARG; inds = param_buffer_idxs) Func( [ - expr.args[1], - DestructuredArgs( - [arg.name for arg in expr.args[2:(end - offset)]], p), - (isone(offset) ? (expr.args[end],) : ())... + expr.args[begin:(p_start - 1)]..., + destructured_mtkparams, + expr.args[(end - offset + 1):end]... ], [], - Let(expr.args[2:(end - offset)], expr.body, false) + Let(param_buffer_args, expr.body, false) ) end else function (expr) - p = gensym(:p) + param_args = expr.args[p_start:(end - offset)] + param_buffer_idxs = findall(x -> x isa DestructuredArgs, param_args) + param_buffer_args = param_args[param_buffer_idxs] + destructured_mtkparams = DestructuredArgs( + [x.name for x in param_buffer_args], + MTKPARAMETERS_ARG; inds = param_buffer_idxs) Func( [ - expr.args[1], - DestructuredArgs( - [arg.name for arg in expr.args[2:(end - offset)]], p), - (isone(offset) ? (expr.args[end],) : ())... + expr.args[begin:(p_start - 1)]..., + destructured_mtkparams, + expr.args[(end - offset + 1):end]... ], [], - Let(expr.args[2:(end - offset)], expr.body, false) + Let(param_buffer_args, expr.body, false) ) end, function (expr) - p = gensym(:p) + param_args = expr.args[(p_start + 1):(end - offset)] + param_buffer_idxs = findall(x -> x isa DestructuredArgs, param_args) + param_buffer_args = param_args[param_buffer_idxs] + destructured_mtkparams = DestructuredArgs( + [x.name for x in param_buffer_args], + MTKPARAMETERS_ARG; inds = param_buffer_idxs) Func( [ - expr.args[1], - expr.args[2], - DestructuredArgs( - [arg.name for arg in expr.args[3:(end - offset)]], p), - (isone(offset) ? (expr.args[end],) : ())... + expr.args[begin:p_start]..., + destructured_mtkparams, + expr.args[(end - offset + 1):end]... ], [], - Let(expr.args[3:(end - offset)], expr.body, false) + Let(param_buffer_args, expr.body, false) ) end end From ab639ebea58cef4ee1deea864e5749d7d4a7d397 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 25 Sep 2024 14:08:01 +0530 Subject: [PATCH 0046/2176] feat: use `wrap_mtkparameters` in `build_explicit_observed_function` --- src/systems/diffeqs/odesystem.jl | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/src/systems/diffeqs/odesystem.jl b/src/systems/diffeqs/odesystem.jl index 942c87afcc..a063b006fd 100644 --- a/src/systems/diffeqs/odesystem.jl +++ b/src/systems/diffeqs/odesystem.jl @@ -412,8 +412,16 @@ function build_explicit_observed_function(sys, ts; ts = [ts] end ts = unwrap.(ts) + issplit = has_index_cache(sys) && get_index_cache(sys) !== nothing if is_dde(sys) - ts = map(x -> delay_to_function(sys, x), ts) + if issplit + ts = map( + x -> delay_to_function( + sys, x; history_arg = issplit ? MTKPARAMETERS_ARG : DEFAULT_PARAMS_ARG), + ts) + else + ts = map(x -> delay_to_function(sys, x), ts) + end end vars = Set() @@ -491,7 +499,8 @@ function build_explicit_observed_function(sys, ts; for i in 1:maxidx eq = obs[i] if is_dde(sys) - eq = delay_to_function(sys, eq) + eq = delay_to_function( + sys, eq; history_arg = issplit ? MTKPARAMETERS_ARG : DEFAULT_PARAMS_ARG) end lhs = eq.lhs rhs = eq.rhs @@ -518,12 +527,14 @@ function build_explicit_observed_function(sys, ts; else dvs = (dvs,) end + p_start = param_only ? 1 : (length(dvs) + 1) if inputs === nothing args = param_only ? [ps..., ivs...] : [dvs..., ps..., ivs...] else inputs = unwrap.(inputs) ipts = DestructuredArgs(inputs, inbounds = !checkbounds) args = param_only ? [ipts, ps..., ivs...] : [dvs..., ipts, ps..., ivs...] + p_start += 1 end pre = get_postprocess_fbody(sys) @@ -534,19 +545,27 @@ function build_explicit_observed_function(sys, ts; wrap_array_vars(sys, ts; ps = _ps, inputs) .∘ wrap_parameter_dependencies(sys, isscalar) end + mtkparams_wrapper = wrap_mtkparameters(sys, isscalar, p_start) + if mtkparams_wrapper isa Tuple + oop_mtkp_wrapper = mtkparams_wrapper[1] + else + oop_mtkp_wrapper = mtkparams_wrapper + end + # Need to keep old method of building the function since it uses `output_type`, # which can't be provided to `build_function` oop_fn = Func(args, [], pre(Let(obsexprs, isscalar ? ts[1] : MakeArray(ts, output_type), - false))) |> array_wrapper[1] |> toexpr + false))) |> array_wrapper[1] |> oop_mtkp_wrapper |> toexpr oop_fn = expression ? oop_fn : eval_or_rgf(oop_fn; eval_expression, eval_module) if !isscalar iip_fn = build_function(ts, args...; postprocess_fbody = pre, - wrap_code = array_wrapper .∘ wrap_assignments(isscalar, obsexprs), + wrap_code = array_wrapper .∘ wrap_assignments(isscalar, obsexprs) .∘ + mtkparams_wrapper, expression = Val{true})[2] if !expression iip_fn = eval_or_rgf(iip_fn; eval_expression, eval_module) From d5912be9bc21c2e21a2612692b5b49d0306ce3ec Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 25 Sep 2024 14:09:02 +0530 Subject: [PATCH 0047/2176] refactor: don't splat `MTKParameters` into observed functions anymore --- src/systems/abstractsystem.jl | 50 +++++++++-------------------------- test/clock.jl | 2 +- test/input_output_handling.jl | 4 +-- test/odesystem.jl | 2 +- test/reduction.jl | 2 +- test/serialization.jl | 2 +- 6 files changed, 19 insertions(+), 43 deletions(-) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index 9b5a3ab6dd..a30ad818f6 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -695,25 +695,17 @@ function SymbolicIndexingInterface.parameter_observed(sys::AbstractSystem, sym) if rawobs isa Tuple if is_time_dependent(sys) obsfn = let oop = rawobs[1], iip = rawobs[2] - f1a(p::MTKParameters, t) = oop(p..., t) - f1a(out, p::MTKParameters, t) = iip(out, p..., t) + f1a(p, t) = oop(p, t) + f1a(out, p, t) = iip(out, p, t) end else obsfn = let oop = rawobs[1], iip = rawobs[2] - f1b(p::MTKParameters) = oop(p...) - f1b(out, p::MTKParameters) = iip(out, p...) + f1b(p) = oop(p) + f1b(out, p) = iip(out, p) end end else - if is_time_dependent(sys) - obsfn = let rawobs = rawobs - f2a(p::MTKParameters, t) = rawobs(p..., t) - end - else - obsfn = let rawobs = rawobs - f2b(p::MTKParameters) = rawobs(p...) - end - end + obsfn = rawobs end else obsfn = build_explicit_observed_function(sys, sym; param_only = true) @@ -828,21 +820,11 @@ function SymbolicIndexingInterface.observed( _fn = build_explicit_observed_function(sys, sym; eval_expression, eval_module) if is_time_dependent(sys) - return let _fn = _fn - fn1(u, p, t) = _fn(u, p, t) - fn1(u, p::MTKParameters, t) = _fn(u, p..., t) - - # DDEs - fn1(u, histfn, p, t) = _fn(u, histfn, p, t) - fn1(u, histfn, p::MTKParameters, t) = _fn(u, histfn, p..., t) - fn1 - end + return _fn else return let _fn = _fn fn2(u, p) = _fn(u, p) - fn2(u, p::MTKParameters) = _fn(u, p...) fn2(::Nothing, p) = _fn([], p) - fn2(::Nothing, p::MTKParameters) = _fn([], p...) fn2 end end @@ -2380,8 +2362,8 @@ function linearization_function(sys::AbstractSystem, inputs, u_getter = u_getter function (u, p, t) - p_setter!(oldps, p_getter(u, p..., t)) - newu = u_getter(u, p..., t) + p_setter!(oldps, p_getter(u, p, t)) + newu = u_getter(u, p, t) return newu, oldps end end @@ -2392,20 +2374,15 @@ function linearization_function(sys::AbstractSystem, inputs, function (u, p, t) state = ProblemState(; u, p, t) - return u_getter(state), p_getter(state) + return u_getter( + state_values(state), parameter_values(state), current_time(state)), + p_getter(state) end end end initfn = NonlinearFunction(initsys; eval_expression, eval_module) initprobmap = build_explicit_observed_function( initsys, unknowns(sys); eval_expression, eval_module) - if has_index_cache(sys) && get_index_cache(sys) !== nothing - initprobmap = let inner = initprobmap - fn(u, p::MTKParameters) = inner(u, p...) - fn(u, p) = inner(u, p) - fn - end - end ps = parameters(sys) h = build_explicit_observed_function(sys, outputs; eval_expression, eval_module) lin_fun = let diff_idxs = diff_idxs, @@ -2452,7 +2429,7 @@ function linearization_function(sys::AbstractSystem, inputs, fg_xz = ForwardDiff.jacobian(uf, u) h_xz = ForwardDiff.jacobian( let p = p, t = t - xz -> p isa MTKParameters ? h(xz, p..., t) : h(xz, p, t) + xz -> h(xz, p, t) end, u) pf = SciMLBase.ParamJacobianWrapper(fun, t, u) fg_u = jacobian_wrt_vars(pf, p, input_idxs, chunk) @@ -2464,7 +2441,6 @@ function linearization_function(sys::AbstractSystem, inputs, end hp = let u = u, t = t _hp(p) = h(u, p, t) - _hp(p::MTKParameters) = h(u, p..., t) _hp end h_u = jacobian_wrt_vars(hp, p, input_idxs, chunk) @@ -2517,7 +2493,7 @@ function linearize_symbolic(sys::AbstractSystem, inputs, dx = fun(sts, p..., t) h = build_explicit_observed_function(sys, outputs; eval_expression, eval_module) - y = h(sts, p..., t) + y = h(sts, p, t) fg_xz = Symbolics.jacobian(dx, sts) fg_u = Symbolics.jacobian(dx, inputs) diff --git a/test/clock.jl b/test/clock.jl index 5bf5e917aa..91aaa8248e 100644 --- a/test/clock.jl +++ b/test/clock.jl @@ -514,7 +514,7 @@ eqs = [yd ~ Sample(dt)(y) @test sol.prob.kwargs[:disc_saved_values][1].t == sol.t[1:2:end] # Test that the discrete-time system executed at every step of the continuous solver. The solver saves each time step twice, one state value before discrete affect and one after. @test_nowarn ModelingToolkit.build_explicit_observed_function( - model, model.counter.ud)(sol.u[1], prob.p..., sol.t[1]) + model, model.counter.ud)(sol.u[1], prob.p, sol.t[1]) @variables x(t)=1.0 y(t)=1.0 eqs = [D(y) ~ Hold(x) diff --git a/test/input_output_handling.jl b/test/input_output_handling.jl index 19078bc98c..07a26d2d1a 100644 --- a/test/input_output_handling.jl +++ b/test/input_output_handling.jl @@ -144,9 +144,9 @@ if VERSION >= v"1.8" # :opaque_closure not supported before drop_expr = identity) x = randn(size(A, 1)) u = randn(size(B, 2)) - p = getindex.( + p = (getindex.( Ref(ModelingToolkit.defaults_and_guesses(ssys)), - parameters(ssys)) + parameters(ssys)),) y1 = obsf(x, u, p, 0) y2 = C * x + D * u @test y1[] ≈ y2[] diff --git a/test/odesystem.jl b/test/odesystem.jl index 748a6a4df5..154f8eb9f3 100644 --- a/test/odesystem.jl +++ b/test/odesystem.jl @@ -548,7 +548,7 @@ prob = ODEProblem( @test_nowarn solve(prob, Tsit5()) obsfn = ModelingToolkit.build_explicit_observed_function( outersys, bar(3outersys.sys.ms, 3outersys.sys.p)) -@test_nowarn obsfn(sol.u[1], prob.p..., sol.t[1]) +@test_nowarn obsfn(sol.u[1], prob.p, sol.t[1]) # x/x @variables x(t) diff --git a/test/reduction.jl b/test/reduction.jl index 064a2efd81..2d9e950e95 100644 --- a/test/reduction.jl +++ b/test/reduction.jl @@ -119,7 +119,7 @@ prob1 = ODEProblem(reduced_system, u0, (0.0, 100.0), pp) solve(prob1, Rodas5()) prob2 = SteadyStateProblem(reduced_system, u0, pp) -@test prob2.f.observed(lorenz2.u, prob2.u0, pp) === 1.0 +@test prob2.f.observed(lorenz2.u, prob2.u0, prob2.p) === 1.0 # issue #724 and #716 let diff --git a/test/serialization.jl b/test/serialization.jl index 5e09055a92..e10de51299 100644 --- a/test/serialization.jl +++ b/test/serialization.jl @@ -50,7 +50,7 @@ for var in all_obs f = ModelingToolkit.build_explicit_observed_function(ss, var; expression = true) sym = ModelingToolkit.getname(var) |> string ex = :(if name == Symbol($sym) - return $f(u0, p..., t) + return $f(u0, p, t) end) push!(obs_exps, ex) end From eb50d7404fb7233c4e1820a958b20b401cc2e007 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 25 Sep 2024 17:18:07 +0530 Subject: [PATCH 0048/2176] feat: implement and test SII interface for non-markovian systems --- src/systems/abstractsystem.jl | 2 ++ test/dde.jl | 5 +++++ test/odesystem.jl | 2 ++ 3 files changed, 9 insertions(+) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index a30ad818f6..35fc0324d1 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -840,6 +840,8 @@ end SymbolicIndexingInterface.is_time_dependent(::AbstractTimeDependentSystem) = true SymbolicIndexingInterface.is_time_dependent(::AbstractTimeIndependentSystem) = false +SymbolicIndexingInterface.is_markovian(sys::AbstractSystem) = !is_dde(sys) + SymbolicIndexingInterface.constant_structure(::AbstractSystem) = true function SymbolicIndexingInterface.all_variable_symbols(sys::AbstractSystem) diff --git a/test/dde.jl b/test/dde.jl index 7ab465e6f2..67f9e4f599 100644 --- a/test/dde.jl +++ b/test/dde.jl @@ -1,4 +1,5 @@ using ModelingToolkit, DelayDiffEq, Test +using SymbolicIndexingInterface: is_markovian using ModelingToolkit: t_nounits as t, D_nounits as D p0 = 0.2; @@ -39,6 +40,7 @@ eqs = [D(x₀) ~ (v0 / (1 + beta0 * (x₂(t - tau)^2))) * (p0 - q0) * x₀ - d0 D(x₂(t)) ~ (v1 / (1 + beta1 * (x₂(t - tau)^2))) * (1 - p1 + q1) * x₁ - d2 * x₂(t)] @mtkbuild sys = System(eqs, t) @test ModelingToolkit.is_dde(sys) +@test !is_markovian(sys) prob = DDEProblem(sys, [x₀ => 1.0, x₁ => 1.0, x₂(t) => 1.0], tspan, @@ -81,6 +83,7 @@ sol = solve(prob, RKMil()) eqs = [D(x(t)) ~ a * x(t) + b * x(t - τ) + c + (α * x(t) + γ) * η] @mtkbuild sys = System(eqs, t) @test ModelingToolkit.is_dde(sys) +@test !is_markovian(sys) @test equations(sys) == [D(x(t)) ~ a * x(t) + b * x(t - τ) + c] @test isequal(ModelingToolkit.get_noiseeqs(sys), [α * x(t) + γ]) prob_mtk = SDDEProblem(sys, [x(t) => 1.0 + t], tspan; constant_lags = (τ,)); @@ -106,8 +109,10 @@ eqs = [osc1.jcn ~ osc2.delx, @named coupledOsc = System(eqs, t) @named coupledOsc = compose(coupledOsc, systems) @test ModelingToolkit.is_dde(coupledOsc) +@test !is_markovian(coupledOsc) @named coupledOsc2 = System(eqs, t; systems) @test ModelingToolkit.is_dde(coupledOsc2) +@test !is_markovian(coupledOsc2) for coupledOsc in [coupledOsc, coupledOsc2] local sys = structural_simplify(coupledOsc) @test length(equations(sys)) == 4 diff --git a/test/odesystem.jl b/test/odesystem.jl index 154f8eb9f3..47c8ad9661 100644 --- a/test/odesystem.jl +++ b/test/odesystem.jl @@ -1399,6 +1399,8 @@ end @variables x(t) @named sys = ODESystem(D(x) ~ x, t) @test !ModelingToolkit.is_dde(sys) + @test is_markovian(sys) @named sys2 = ODESystem(Equation[], t; systems = [sys]) @test !ModelingToolkit.is_dde(sys) + @test is_markovian(sys) end From 65e59d0bb24f23953e69bf43d6f5ef762040484e Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 26 Sep 2024 14:55:10 +0530 Subject: [PATCH 0049/2176] feat: update `wrap_array_vars` to handle history function --- src/systems/abstractsystem.jl | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index 35fc0324d1..958000ec3e 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -230,7 +230,8 @@ function wrap_parameter_dependencies(sys::AbstractSystem, isscalar) end function wrap_array_vars( - sys::AbstractSystem, exprs; dvs = unknowns(sys), ps = parameters(sys), inputs = nothing) + sys::AbstractSystem, exprs; dvs = unknowns(sys), ps = parameters(sys), + inputs = nothing, history = false) isscalar = !(exprs isa AbstractArray) array_vars = Dict{Any, AbstractArray{Int}}() if dvs !== nothing @@ -328,6 +329,19 @@ function wrap_array_vars( array_parameters[p] = (idxs, buffer_idx, sz) end end + + inputind = if history + uind + 2 + else + uind + 1 + end + params_offset = if history && hasinputs + uind + 2 + elseif history || hasinputs + uind + 1 + else + uind + end if isscalar function (expr) Func( @@ -336,10 +350,10 @@ function wrap_array_vars( Let( vcat( [k ← :(view($(expr.args[uind].name), $v)) for (k, v) in array_vars], - [k ← :(view($(expr.args[uind + hasinputs].name), $v)) + [k ← :(view($(expr.args[inputind].name), $v)) for (k, v) in input_vars], [k ← :(reshape( - view($(expr.args[uind + hasinputs + buffer_idx].name), $idxs), + view($(expr.args[params_offset + buffer_idx].name), $idxs), $sz)) for (k, (idxs, buffer_idx, sz)) in array_parameters], [k ← Code.MakeArray(v, symtype(k)) @@ -358,10 +372,10 @@ function wrap_array_vars( Let( vcat( [k ← :(view($(expr.args[uind].name), $v)) for (k, v) in array_vars], - [k ← :(view($(expr.args[uind + hasinputs].name), $v)) + [k ← :(view($(expr.args[inputind].name), $v)) for (k, v) in input_vars], [k ← :(reshape( - view($(expr.args[uind + hasinputs + buffer_idx].name), $idxs), + view($(expr.args[params_offset + buffer_idx].name), $idxs), $sz)) for (k, (idxs, buffer_idx, sz)) in array_parameters], [k ← Code.MakeArray(v, symtype(k)) @@ -380,10 +394,10 @@ function wrap_array_vars( vcat( [k ← :(view($(expr.args[uind + 1].name), $v)) for (k, v) in array_vars], - [k ← :(view($(expr.args[uind + hasinputs + 1].name), $v)) + [k ← :(view($(expr.args[inputind + 1].name), $v)) for (k, v) in input_vars], [k ← :(reshape( - view($(expr.args[uind + hasinputs + buffer_idx + 1].name), + view($(expr.args[params_offset + buffer_idx + 1].name), $idxs), $sz)) for (k, (idxs, buffer_idx, sz)) in array_parameters], From b237706ba0a817ca69bc285c44505166e00365f2 Mon Sep 17 00:00:00 2001 From: Herman Sletmoen Date: Mon, 16 Sep 2024 17:06:27 +0200 Subject: [PATCH 0050/2176] Clean up generate_initializesystem() --- src/systems/nonlinear/initializesystem.jl | 133 ++++++++++------------ 1 file changed, 58 insertions(+), 75 deletions(-) diff --git a/src/systems/nonlinear/initializesystem.jl b/src/systems/nonlinear/initializesystem.jl index aa7ad8bd69..bf3bbb227d 100644 --- a/src/systems/nonlinear/initializesystem.jl +++ b/src/systems/nonlinear/initializesystem.jl @@ -5,109 +5,92 @@ Generate `NonlinearSystem` which initializes an ODE problem from specified initi """ function generate_initializesystem(sys::ODESystem; u0map = Dict(), - name = nameof(sys), - guesses = Dict(), check_defguess = false, - default_dd_value = 0.0, - algebraic_only = false, initialization_eqs = [], - check_units = true, - kwargs...) - sts, eqs = unknowns(sys), equations(sys) + guesses = Dict(), + default_dd_guess = 0.0, + algebraic_only = false, + check_units = true, check_defguess = false, + name = nameof(sys), kwargs...) + vars = unique([unknowns(sys); getfield.((observed(sys)), :lhs)]) + vars_set = Set(vars) # for efficient in-lookup + + eqs = equations(sys) idxs_diff = isdiffeq.(eqs) idxs_alge = .!idxs_diff - num_alge = sum(idxs_alge) - - # Start the equations list with algebraic equations - eqs_ics = eqs[idxs_alge] - u0 = Vector{Pair}(undef, 0) + # prepare map for dummy derivative substitution eqs_diff = eqs[idxs_diff] - diffmap = Dict(getfield.(eqs_diff, :lhs) .=> getfield.(eqs_diff, :rhs)) - observed_diffmap = Dict(Differential(get_iv(sys)).(getfield.((observed(sys)), :lhs)) .=> - Differential(get_iv(sys)).(getfield.((observed(sys)), :rhs))) - full_diffmap = merge(diffmap, observed_diffmap) + D = Differential(get_iv(sys)) + diffmap = merge( + Dict(eq.lhs => eq.rhs for eq in eqs_diff), + Dict(D(eq.lhs) => D(eq.rhs) for eq in observed(sys)) + ) - full_states = unique([sts; getfield.((observed(sys)), :lhs)]) - set_full_states = Set(full_states) + # 1) process dummy derivatives and u0map into initialization system + eqs_ics = eqs[idxs_alge] # start equation list with algebraic equations + defs = copy(defaults(sys)) # copy so we don't modify sys.defaults guesses = merge(get_guesses(sys), todict(guesses)) schedule = getfield(sys, :schedule) - - if schedule !== nothing - guessmap = [x[1] => get(guesses, x[1], default_dd_value) - for x in schedule.dummy_sub] - dd_guess = Dict(filter(x -> !isnothing(x[1]), guessmap)) - if u0map === nothing || isempty(u0map) - filtered_u0 = u0map - else - filtered_u0 = Pair[] - for x in u0map - y = get(schedule.dummy_sub, x[1], x[1]) - y = ModelingToolkit.fixpoint_sub(y, full_diffmap) - - if y ∈ set_full_states - # defer initialization until defaults are merged below - push!(filtered_u0, y => x[2]) + if !isnothing(schedule) + for x in filter(x -> !isnothing(x[1]), schedule.dummy_sub) + # set dummy derivatives to default_dd_guess unless specified + push!(defs, x[1] => get(guesses, x[1], default_dd_guess)) + end + if !isnothing(u0map) + for (y, x) in u0map + y = get(schedule.dummy_sub, y, y) + y = fixpoint_sub(y, diffmap) + if y ∈ vars_set + # variables specified in u0 overrides defaults + push!(defs, y => x) elseif y isa Symbolics.Arr - # scalarize array # TODO: don't scalarize arrays - _y = collect(y) - for i in eachindex(_y) - push!(filtered_u0, _y[i] => x[2][i]) - end + # TODO: don't scalarize arrays + push!(defs, collect(y) .=> x) elseif y isa Symbolics.BasicSymbolic - # y is a derivative expression expanded - # add to the initialization equations - push!(eqs_ics, y ~ x[2]) + # y is a derivative expression expanded; add it to the initialization equations + push!(eqs_ics, y ~ x) else error("Initialization expression $y is currently not supported. If its a higher order derivative expression, then only the dummy derivative expressions are supported.") end end - filtered_u0 = todict(filtered_u0) end - else - dd_guess = Dict() - filtered_u0 = todict(u0map) end - defs = merge(defaults(sys), filtered_u0) - - for st in full_states - if st ∈ keys(defs) - def = defs[st] - + # 2) process other variables + for var in vars + if var ∈ keys(defs) + def = defs[var] if def isa Equation - st ∉ keys(guesses) && check_defguess && - error("Invalid setup: unknown $(st) has an initial condition equation with no guess.") + # TODO: this behavior is not tested! + var ∉ keys(guesses) && check_defguess && + error("Invalid setup: variable $(var) has an initial condition equation with no guess.") push!(eqs_ics, def) - push!(u0, st => guesses[st]) + push!(defs, var => guesses[var]) else - push!(eqs_ics, st ~ def) - push!(u0, st => def) + push!(eqs_ics, var ~ def) end - elseif st ∈ keys(guesses) - push!(u0, st => guesses[st]) + elseif var ∈ keys(guesses) + push!(defs, var => guesses[var]) elseif check_defguess - error("Invalid setup: unknown $(st) has no default value or initial guess") + error("Invalid setup: variable $(var) has no default value or initial guess") end end + # 3) process explicitly provided initialization equations if !algebraic_only - for eq in [get_initialization_eqs(sys); initialization_eqs] - _eq = ModelingToolkit.fixpoint_sub(eq, full_diffmap) - push!(eqs_ics, _eq) + initialization_eqs = [get_initialization_eqs(sys); initialization_eqs] + for eq in initialization_eqs + eq = fixpoint_sub(eq, diffmap) # expand dummy derivatives + push!(eqs_ics, eq) end end - pars = [parameters(sys); get_iv(sys)] - nleqs = [eqs_ics; observed(sys)] - - sys_nl = NonlinearSystem(nleqs, - full_states, - pars; - defaults = merge(ModelingToolkit.defaults(sys), todict(u0), dd_guess), - parameter_dependencies = parameter_dependencies(sys), + pars = [parameters(sys); get_iv(sys)] # include independent variable as pseudo-parameter + eqs_ics = [eqs_ics; observed(sys)] + return NonlinearSystem( + eqs_ics, vars, pars; + defaults = defs, parameter_dependencies = parameter_dependencies(sys), checks = check_units, - name, - kwargs...) - - return sys_nl + name, kwargs... + ) end From cc12bb99fc4700accd1d9a586fba96afc7688efc Mon Sep 17 00:00:00 2001 From: Herman Sletmoen Date: Mon, 16 Sep 2024 18:14:13 +0200 Subject: [PATCH 0051/2176] Remove unsupported/untested/unworking behavior where defaults map variables to equations --- src/systems/nonlinear/initializesystem.jl | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/src/systems/nonlinear/initializesystem.jl b/src/systems/nonlinear/initializesystem.jl index bf3bbb227d..6ce85ad1a2 100644 --- a/src/systems/nonlinear/initializesystem.jl +++ b/src/systems/nonlinear/initializesystem.jl @@ -59,16 +59,7 @@ function generate_initializesystem(sys::ODESystem; # 2) process other variables for var in vars if var ∈ keys(defs) - def = defs[var] - if def isa Equation - # TODO: this behavior is not tested! - var ∉ keys(guesses) && check_defguess && - error("Invalid setup: variable $(var) has an initial condition equation with no guess.") - push!(eqs_ics, def) - push!(defs, var => guesses[var]) - else - push!(eqs_ics, var ~ def) - end + push!(eqs_ics, var ~ defs[var]) elseif var ∈ keys(guesses) push!(defs, var => guesses[var]) elseif check_defguess From a61977a51525f97819239817f3d3456ebe7c6ba5 Mon Sep 17 00:00:00 2001 From: Herman Sletmoen Date: Mon, 16 Sep 2024 18:55:27 +0200 Subject: [PATCH 0052/2176] Splat scalarized array --- src/systems/nonlinear/initializesystem.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/systems/nonlinear/initializesystem.jl b/src/systems/nonlinear/initializesystem.jl index 6ce85ad1a2..e3d28719c8 100644 --- a/src/systems/nonlinear/initializesystem.jl +++ b/src/systems/nonlinear/initializesystem.jl @@ -45,7 +45,7 @@ function generate_initializesystem(sys::ODESystem; push!(defs, y => x) elseif y isa Symbolics.Arr # TODO: don't scalarize arrays - push!(defs, collect(y) .=> x) + push!(defs, (collect(y) .=> x)...) elseif y isa Symbolics.BasicSymbolic # y is a derivative expression expanded; add it to the initialization equations push!(eqs_ics, y ~ x) From 794a421bc86668605f1d7b4134d657fbc4aba37c Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 3 Oct 2024 12:55:39 +0530 Subject: [PATCH 0053/2176] fix: promote `resid_prototype` using tunables --- src/systems/nonlinear/nonlinearsystem.jl | 35 +++++++++++++++++++----- test/nonlinearsystem.jl | 19 +++++++++++++ 2 files changed, 47 insertions(+), 7 deletions(-) diff --git a/src/systems/nonlinear/nonlinearsystem.jl b/src/systems/nonlinear/nonlinearsystem.jl index 1f36d61601..498ad1e59b 100644 --- a/src/systems/nonlinear/nonlinearsystem.jl +++ b/src/systems/nonlinear/nonlinearsystem.jl @@ -295,7 +295,7 @@ function SciMLBase.NonlinearFunction(sys::NonlinearSystem, args...; kwargs...) end function SciMLBase.NonlinearFunction{iip}(sys::NonlinearSystem, dvs = unknowns(sys), - ps = parameters(sys), u0 = nothing; + ps = parameters(sys), u0 = nothing, p = nothing; version = nothing, jac = false, eval_expression = false, @@ -327,11 +327,22 @@ function SciMLBase.NonlinearFunction{iip}(sys::NonlinearSystem, dvs = unknowns(s observedfun = ObservedFunctionCache(sys; eval_expression, eval_module) + if length(dvs) == length(equations(sys)) + resid_prototype = nothing + else + u0ElType = u0 === nothing ? Float64 : eltype(u0) + if SciMLStructures.isscimlstructure(p) + u0ElType = promote_type( + eltype(SciMLStructures.canonicalize(SciMLStructures.Tunable(), p)[1]), + u0ElType) + end + resid_prototype = zeros(u0ElType, length(equations(sys))) + end + NonlinearFunction{iip}(f, sys = sys, jac = _jac === nothing ? nothing : _jac, - resid_prototype = length(dvs) == length(equations(sys)) ? nothing : - zeros(length(equations(sys))), + resid_prototype = resid_prototype, jac_prototype = sparse ? similar(calculate_jacobian(sys, sparse = sparse), Float64) : nothing, @@ -355,7 +366,7 @@ variable and parameter vectors, respectively. struct NonlinearFunctionExpr{iip} end function NonlinearFunctionExpr{iip}(sys::NonlinearSystem, dvs = unknowns(sys), - ps = parameters(sys), u0 = nothing; + ps = parameters(sys), u0 = nothing, p = nothing; version = nothing, tgrad = false, jac = false, linenumbers = false, @@ -376,8 +387,18 @@ function NonlinearFunctionExpr{iip}(sys::NonlinearSystem, dvs = unknowns(sys), end jp_expr = sparse ? :(similar($(get_jac(sys)[]), Float64)) : :nothing - resid_expr = length(dvs) == length(equations(sys)) ? :nothing : - :(zeros($(length(equations(sys))))) + if length(dvs) == length(equations(sys)) + resid_expr = :nothing + else + u0ElType = u0 === nothing ? Float64 : eltype(u0) + if SciMLStructures.isscimlstructure(p) + u0ElType = promote_type( + eltype(SciMLStructures.canonicalize(SciMLStructures.Tunable(), p)[1]), + u0ElType) + end + + resid_expr = :(zeros($u0ElType, $(length(equations(sys))))) + end ex = quote f = $f jac = $_jac @@ -412,7 +433,7 @@ function process_NonlinearProblem(constructor, sys::NonlinearSystem, u0map, para check_eqs_u0(eqs, dvs, u0; kwargs...) end - f = constructor(sys, dvs, ps, u0; jac = jac, checkbounds = checkbounds, + f = constructor(sys, dvs, ps, u0, p; jac = jac, checkbounds = checkbounds, linenumbers = linenumbers, parallel = parallel, simplify = simplify, sparse = sparse, eval_expression = eval_expression, eval_module = eval_module, kwargs...) diff --git a/test/nonlinearsystem.jl b/test/nonlinearsystem.jl index a71d34a880..400eb45e51 100644 --- a/test/nonlinearsystem.jl +++ b/test/nonlinearsystem.jl @@ -3,6 +3,7 @@ using ModelingToolkit: get_metadata using DiffEqBase, SparseArrays using Test using NonlinearSolve +using ForwardDiff using ModelingToolkit: value using ModelingToolkit: get_default_or_guess, MTKParameters @@ -325,3 +326,21 @@ end prob = @test_nowarn NonlinearProblem(sys, nothing) @test_nowarn solve(prob) end + +@testset "resid_prototype when system has no unknowns and an equation" begin + @variables x + @parameters p + @named sys = NonlinearSystem([x ~ 1, x^2 - p ~ 0]) + for sys in [ + structural_simplify(sys, fully_determined = false), + structural_simplify(sys, fully_determined = false, split = false) + ] + @test length(equations(sys)) == 1 + @test length(unknowns(sys)) == 0 + T = typeof(ForwardDiff.Dual(1.0)) + prob = NonlinearProblem(sys, [], [p => ForwardDiff.Dual(1.0)]; check_length = false) + @test prob.f(Float64[], prob.p) isa Vector{T} + @test prob.f.resid_prototype isa Vector{T} + @test_nowarn solve(prob) + end +end From 7de6abf9f9b2858fe47cd70f4c29dbce8d3daed4 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 26 Sep 2024 14:55:30 +0530 Subject: [PATCH 0054/2176] fix: fix DDE observed with array variables --- src/systems/diffeqs/odesystem.jl | 4 ++-- test/dde.jl | 37 ++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/src/systems/diffeqs/odesystem.jl b/src/systems/diffeqs/odesystem.jl index a063b006fd..784b071ef1 100644 --- a/src/systems/diffeqs/odesystem.jl +++ b/src/systems/diffeqs/odesystem.jl @@ -539,10 +539,10 @@ function build_explicit_observed_function(sys, ts; pre = get_postprocess_fbody(sys) array_wrapper = if param_only - wrap_array_vars(sys, ts; ps = _ps, dvs = nothing, inputs) .∘ + wrap_array_vars(sys, ts; ps = _ps, dvs = nothing, inputs, history = is_dde(sys)) .∘ wrap_parameter_dependencies(sys, isscalar) else - wrap_array_vars(sys, ts; ps = _ps, inputs) .∘ + wrap_array_vars(sys, ts; ps = _ps, inputs, history = is_dde(sys)) .∘ wrap_parameter_dependencies(sys, isscalar) end mtkparams_wrapper = wrap_mtkparameters(sys, isscalar, p_start) diff --git a/test/dde.jl b/test/dde.jl index 67f9e4f599..7a6f46f723 100644 --- a/test/dde.jl +++ b/test/dde.jl @@ -125,3 +125,40 @@ obsfn = ModelingToolkit.build_explicit_observed_function( sys, [sys.osc1.delx, sys.osc2.delx]) @test_nowarn sol[[sys.osc1.delx, sys.osc2.delx]] @test sol[sys.osc1.delx] ≈ sol(sol.t .- 0.01; idxs = sys.osc1.x) + +@testset "DDE observed with array variables" begin + @component function valve(; name) + @parameters begin + open(t)::Bool = false + Kp = 2 + Ksnap = 1.1 + τ = 0.1 + end + @variables begin + opening(..) + lag_opening(t) + snap_opening(t) + end + eqs = [D(opening(t)) ~ Kp * (open - opening(t)) + lag_opening ~ opening(t - τ) + snap_opening ~ clamp(Ksnap * lag_opening - 1 / Ksnap, 0, 1)] + return System(eqs, t; name = name) + end + + @component function veccy(; name) + @parameters dx[1:3] = ones(3) + @variables begin + x(t)[1:3] = zeros(3) + end + return System([D(x) ~ dx], t; name = name) + end + + @mtkbuild ssys = System( + Equation[], t; systems = [valve(name = :valve), veccy(name = :vvecs)]) + prob = DDEProblem(ssys, [ssys.valve.opening => 1.0], (0.0, 1.0)) + sol = solve(prob, MethodOfSteps(Tsit5())) + obsval = @test_nowarn sol[ssys.valve.lag_opening + sum(ssys.vvecs.x)] + @test obsval ≈ + sol(sol.t .- prob.ps[ssys.valve.τ]; idxs = ssys.valve.opening).u .+ + sum.(sol[ssys.vvecs.x]) +end From 14789d5c54ae3b971039639ccaca52db8867b139 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 2 Oct 2024 16:19:46 +0530 Subject: [PATCH 0055/2176] build: bump SciMLBase and SII compat --- Project.toml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Project.toml b/Project.toml index ff17093ae9..9a69a1467e 100644 --- a/Project.toml +++ b/Project.toml @@ -112,7 +112,7 @@ PrecompileTools = "1" RecursiveArrayTools = "3.26" Reexport = "0.2, 1" RuntimeGeneratedFunctions = "0.5.9" -SciMLBase = "2.52.1" +SciMLBase = "2.55" SciMLStructures = "1.0" Serialization = "1" Setfield = "0.7, 0.8, 1" @@ -120,7 +120,7 @@ SimpleNonlinearSolve = "0.1.0, 1" SparseArrays = "1" SpecialFunctions = "0.7, 0.8, 0.9, 0.10, 1.0, 2" StaticArrays = "0.10, 0.11, 0.12, 1.0" -SymbolicIndexingInterface = "0.3.29" +SymbolicIndexingInterface = "0.3.31" SymbolicUtils = "3.7" Symbolics = "6.12" URIs = "1" From b669cc9ce0e05401a85f6c0682c42180b8a0a70c Mon Sep 17 00:00:00 2001 From: Herman Sletmoen Date: Thu, 3 Oct 2024 15:07:01 +0200 Subject: [PATCH 0056/2176] Test vector in initial conditions --- test/initializationsystem.jl | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/test/initializationsystem.jl b/test/initializationsystem.jl index 82d129b901..466d3ea7e1 100644 --- a/test/initializationsystem.jl +++ b/test/initializationsystem.jl @@ -567,3 +567,12 @@ oprob_2nd_order_2 = ODEProblem(sys_2nd_order, u0_2nd_order_2, tspan, ps) sol = solve(oprob_2nd_order_2, Rosenbrock23()) # retcode: Success @test sol[Y][1] == 2.0 @test sol[D(Y)][1] == 0.5 + +@testset "Vector in initial conditions" begin + @variables x(t)[1:5] y(t)[1:5] + @named sys = ODESystem([D(x) ~ x, D(y) ~ y], t; initialization_eqs = [y ~ -x]) + sys = structural_simplify(sys) + prob = ODEProblem(sys, [sys.x => ones(5)], (0.0, 1.0), []) + sol = solve(prob, Tsit5(), reltol=1e-4) + @test all(sol(1.0, idxs=sys.x) .≈ +exp(1)) && all(sol(1.0, idxs=sys.y) .≈ -exp(1)) +end From 5cf19a907ab8c84fe018efc8100565e0e37d44e9 Mon Sep 17 00:00:00 2001 From: Herman Sletmoen Date: Thu, 3 Oct 2024 15:31:34 +0200 Subject: [PATCH 0057/2176] NonlinearSystem: put single equation input in vector, instead of scalarizing --- src/systems/nonlinear/nonlinearsystem.jl | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/src/systems/nonlinear/nonlinearsystem.jl b/src/systems/nonlinear/nonlinearsystem.jl index 1f36d61601..c5f79fdaf4 100644 --- a/src/systems/nonlinear/nonlinearsystem.jl +++ b/src/systems/nonlinear/nonlinearsystem.jl @@ -126,15 +126,13 @@ function NonlinearSystem(eqs, unknowns, ps; throw(ArgumentError("NonlinearSystem does not accept `continuous_events`, you provided $continuous_events")) discrete_events === nothing || isempty(discrete_events) || throw(ArgumentError("NonlinearSystem does not accept `discrete_events`, you provided $discrete_events")) - name === nothing && throw(ArgumentError("The `name` keyword must be provided. Please consider using the `@named` macro")) - # Move things over, but do not touch array expressions - # - # # we cannot scalarize in the loop because `eqs` itself might require - # scalarization - eqs = [x.lhs isa Union{Symbolic, Number} ? 0 ~ x.rhs - x.lhs : x - for x in scalarize(eqs)] + + # Accept a single (scalar/vector) equation, but make array for consistent internal handling + if !(eqs isa AbstractArray) + eqs = [eqs] + end if !(isempty(default_u0) && isempty(default_p)) Base.depwarn( From e9a06c56d85d96a76c4c0a0cbde90a86706a743b Mon Sep 17 00:00:00 2001 From: Herman Sletmoen Date: Thu, 3 Oct 2024 15:33:32 +0200 Subject: [PATCH 0058/2176] Scalarize initial conditions of vectors --- src/systems/nonlinear/initializesystem.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/systems/nonlinear/initializesystem.jl b/src/systems/nonlinear/initializesystem.jl index e3d28719c8..3cca51b1e9 100644 --- a/src/systems/nonlinear/initializesystem.jl +++ b/src/systems/nonlinear/initializesystem.jl @@ -45,7 +45,7 @@ function generate_initializesystem(sys::ODESystem; push!(defs, y => x) elseif y isa Symbolics.Arr # TODO: don't scalarize arrays - push!(defs, (collect(y) .=> x)...) + merge!(defs, Dict(scalarize(y .=> x))) elseif y isa Symbolics.BasicSymbolic # y is a derivative expression expanded; add it to the initialization equations push!(eqs_ics, y ~ x) From 7936f985551a1f6476f4de438ac6408dce0c4315 Mon Sep 17 00:00:00 2001 From: Herman Sletmoen Date: Thu, 3 Oct 2024 15:37:17 +0200 Subject: [PATCH 0059/2176] Do all checks at start of NonlinearSystem() --- src/systems/nonlinear/nonlinearsystem.jl | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/src/systems/nonlinear/nonlinearsystem.jl b/src/systems/nonlinear/nonlinearsystem.jl index c5f79fdaf4..3f082068e4 100644 --- a/src/systems/nonlinear/nonlinearsystem.jl +++ b/src/systems/nonlinear/nonlinearsystem.jl @@ -128,21 +128,18 @@ function NonlinearSystem(eqs, unknowns, ps; throw(ArgumentError("NonlinearSystem does not accept `discrete_events`, you provided $discrete_events")) name === nothing && throw(ArgumentError("The `name` keyword must be provided. Please consider using the `@named` macro")) + length(unique(nameof.(systems))) == length(systems) || + throw(ArgumentError("System names must be unique.")) + (isempty(default_u0) && isempty(default_p)) || + Base.depwarn( + "`default_u0` and `default_p` are deprecated. Use `defaults` instead.", + :NonlinearSystem, force = true) # Accept a single (scalar/vector) equation, but make array for consistent internal handling if !(eqs isa AbstractArray) eqs = [eqs] end - if !(isempty(default_u0) && isempty(default_p)) - Base.depwarn( - "`default_u0` and `default_p` are deprecated. Use `defaults` instead.", - :NonlinearSystem, force = true) - end - sysnames = nameof.(systems) - if length(unique(sysnames)) != length(sysnames) - throw(ArgumentError("System names must be unique.")) - end jac = RefValue{Any}(EMPTY_JAC) defaults = todict(defaults) defaults = Dict{Any, Any}(value(k) => value(v) From 4d1019bc79729d810395f0a836fb22eb7796f247 Mon Sep 17 00:00:00 2001 From: Herman Sletmoen Date: Thu, 3 Oct 2024 16:01:26 +0200 Subject: [PATCH 0060/2176] Remove unnecessary branch and just iterate over empty dictionary --- src/systems/nonlinear/initializesystem.jl | 30 +++++++++++------------ 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/src/systems/nonlinear/initializesystem.jl b/src/systems/nonlinear/initializesystem.jl index 3cca51b1e9..2097e2955e 100644 --- a/src/systems/nonlinear/initializesystem.jl +++ b/src/systems/nonlinear/initializesystem.jl @@ -36,22 +36,20 @@ function generate_initializesystem(sys::ODESystem; # set dummy derivatives to default_dd_guess unless specified push!(defs, x[1] => get(guesses, x[1], default_dd_guess)) end - if !isnothing(u0map) - for (y, x) in u0map - y = get(schedule.dummy_sub, y, y) - y = fixpoint_sub(y, diffmap) - if y ∈ vars_set - # variables specified in u0 overrides defaults - push!(defs, y => x) - elseif y isa Symbolics.Arr - # TODO: don't scalarize arrays - merge!(defs, Dict(scalarize(y .=> x))) - elseif y isa Symbolics.BasicSymbolic - # y is a derivative expression expanded; add it to the initialization equations - push!(eqs_ics, y ~ x) - else - error("Initialization expression $y is currently not supported. If its a higher order derivative expression, then only the dummy derivative expressions are supported.") - end + for (y, x) in u0map + y = get(schedule.dummy_sub, y, y) + y = fixpoint_sub(y, diffmap) + if y ∈ vars_set + # variables specified in u0 overrides defaults + push!(defs, y => x) + elseif y isa Symbolics.Arr + # TODO: don't scalarize arrays + merge!(defs, Dict(scalarize(y .=> x))) + elseif y isa Symbolics.BasicSymbolic + # y is a derivative expression expanded; add it to the initialization equations + push!(eqs_ics, y ~ x) + else + error("Initialization expression $y is currently not supported. If its a higher order derivative expression, then only the dummy derivative expressions are supported.") end end end From 28e9e5f47a5479f0b8fca6dfb5b60d9f7b762107 Mon Sep 17 00:00:00 2001 From: Herman Sletmoen Date: Thu, 3 Oct 2024 16:18:43 +0200 Subject: [PATCH 0061/2176] Format --- test/initializationsystem.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/initializationsystem.jl b/test/initializationsystem.jl index 466d3ea7e1..e0caf22ba4 100644 --- a/test/initializationsystem.jl +++ b/test/initializationsystem.jl @@ -573,6 +573,6 @@ sol = solve(oprob_2nd_order_2, Rosenbrock23()) # retcode: Success @named sys = ODESystem([D(x) ~ x, D(y) ~ y], t; initialization_eqs = [y ~ -x]) sys = structural_simplify(sys) prob = ODEProblem(sys, [sys.x => ones(5)], (0.0, 1.0), []) - sol = solve(prob, Tsit5(), reltol=1e-4) - @test all(sol(1.0, idxs=sys.x) .≈ +exp(1)) && all(sol(1.0, idxs=sys.y) .≈ -exp(1)) + sol = solve(prob, Tsit5(), reltol = 1e-4) + @test all(sol(1.0, idxs = sys.x) .≈ +exp(1)) && all(sol(1.0, idxs = sys.y) .≈ -exp(1)) end From 9182a7db83e66390e850866ea7933f1ca527189c Mon Sep 17 00:00:00 2001 From: Herman Sletmoen Date: Thu, 3 Oct 2024 16:48:16 +0200 Subject: [PATCH 0062/2176] Restore canonical equation form 0 ~ rhs - lhs --- src/systems/nonlinear/nonlinearsystem.jl | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/systems/nonlinear/nonlinearsystem.jl b/src/systems/nonlinear/nonlinearsystem.jl index 3f082068e4..050eea135c 100644 --- a/src/systems/nonlinear/nonlinearsystem.jl +++ b/src/systems/nonlinear/nonlinearsystem.jl @@ -140,6 +140,9 @@ function NonlinearSystem(eqs, unknowns, ps; eqs = [eqs] end + # Copy equations to canonical form, but do not touch array expressions + eqs = [wrap(eq.lhs) isa Symbolics.Arr ? eq : 0 ~ eq.rhs - eq.lhs for eq in eqs] + jac = RefValue{Any}(EMPTY_JAC) defaults = todict(defaults) defaults = Dict{Any, Any}(value(k) => value(v) From 21dc079af957b9a946a4b2c0bd2dd0fac2e654e7 Mon Sep 17 00:00:00 2001 From: Yingbo Ma Date: Thu, 3 Oct 2024 15:05:35 -0400 Subject: [PATCH 0063/2176] Revert "feat: allow users to set array length via args in `@mtkmodel`" --- docs/src/basics/MTKLanguage.md | 17 ++-- src/systems/model_parsing.jl | 161 +++++++++------------------------ test/model_parsing.jl | 49 +--------- 3 files changed, 52 insertions(+), 175 deletions(-) diff --git a/docs/src/basics/MTKLanguage.md b/docs/src/basics/MTKLanguage.md index 44fe8bbc07..a2fb7d0870 100644 --- a/docs/src/basics/MTKLanguage.md +++ b/docs/src/basics/MTKLanguage.md @@ -63,14 +63,13 @@ end @structural_parameters begin f = sin N = 2 - M = 3 end begin v_var = 1.0 end @variables begin v(t) = v_var - v_array(t)[1:N, 1:M] + v_array(t)[1:2, 1:3] v_for_defaults(t) end @extend ModelB(; p1) @@ -311,10 +310,10 @@ end - `:defaults`: Dictionary of variables and default values specified in the `@defaults`. - `:extend`: The list of extended unknowns, name given to the base system, and name of the base system. - `:structural_parameters`: Dictionary of structural parameters mapped to their metadata. - - `:parameters`: Dictionary of symbolic parameters mapped to their metadata. Metadata of - the parameter arrays is, for now, omitted. - - `:variables`: Dictionary of symbolic variables mapped to their metadata. Metadata of - the variable arrays is, for now, omitted. + - `:parameters`: Dictionary of symbolic parameters mapped to their metadata. For + parameter arrays, length is added to the metadata as `:size`. + - `:variables`: Dictionary of symbolic variables mapped to their metadata. For + variable arrays, length is added to the metadata as `:size`. - `:kwargs`: Dictionary of keyword arguments mapped to their metadata. - `:independent_variable`: Independent variable, which is added while generating the Model. - `:equations`: List of equations (represented as strings). @@ -325,10 +324,10 @@ For example, the structure of `ModelC` is: julia> ModelC.structure Dict{Symbol, Any} with 10 entries: :components => Any[Union{Expr, Symbol}[:model_a, :ModelA], Union{Expr, Symbol}[:model_array_a, :ModelA, :(1:N)], Union{Expr, Symbol}[:model_array_b, :ModelA, :(1:N)]] - :variables => Dict{Symbol, Dict{Symbol, Any}}(:v=>Dict(:default=>:v_var, :type=>Real), :v_for_defaults=>Dict(:type=>Real)) + :variables => Dict{Symbol, Dict{Symbol, Any}}(:v=>Dict(:default=>:v_var, :type=>Real), :v_array=>Dict(:type=>Real, :size=>(2, 3)), :v_for_defaults=>Dict(:type=>Real)) :icon => URI("https://github.com/SciML/SciMLDocs/blob/main/docs/src/assets/logo.png") - :kwargs => Dict{Symbol, Dict}(:f => Dict(:value => :sin), :N => Dict(:value => 2), :M => Dict(:value => 3), :v => Dict{Symbol, Any}(:value => :v_var, :type => Real), :v_for_defaults => Dict{Symbol, Union{Nothing, DataType}}(:value => nothing, :type => Real), :p1 => Dict(:value => nothing)), - :structural_parameters => Dict{Symbol, Dict}(:f => Dict(:value => :sin), :N => Dict(:value => 2), :M => Dict(:value => 3)) + :kwargs => Dict{Symbol, Dict}(:f=>Dict(:value=>:sin), :N=>Dict(:value=>2), :v=>Dict{Symbol, Any}(:value=>:v_var, :type=>Real), :v_array=>Dict{Symbol, Union{Nothing, UnionAll}}(:value=>nothing, :type=>AbstractArray{Real}), :v_for_defaults=>Dict{Symbol, Union{Nothing, DataType}}(:value=>nothing, :type=>Real), :p1=>Dict(:value=>nothing)) + :structural_parameters => Dict{Symbol, Dict}(:f=>Dict(:value=>:sin), :N=>Dict(:value=>2)) :independent_variable => t :constants => Dict{Symbol, Dict}(:c=>Dict{Symbol, Any}(:value=>1, :type=>Int64, :description=>"Example constant.")) :extend => Any[[:p2, :p1], Symbol("#mtkmodel__anonymous__ModelB"), :ModelB] diff --git a/src/systems/model_parsing.jl b/src/systems/model_parsing.jl index 09ef05c172..fce2add6b7 100644 --- a/src/systems/model_parsing.jl +++ b/src/systems/model_parsing.jl @@ -180,16 +180,6 @@ function update_kwargs_and_metadata!(dict, kwargs, a, def, indices, type, var, end end -function unit_handled_variable_value(mod, y, varname) - meta = parse_metadata(mod, y) - varval = if meta isa Nothing || get(meta, VariableUnit, nothing) isa Nothing - varname - else - :($convert_units($(meta[VariableUnit]), $varname)) - end - return varval -end - function parse_variable_def!(dict, mod, arg, varclass, kwargs, where_types; def = nothing, indices::Union{Vector{UnitRange{Int}}, Nothing} = nothing, type::Type = Real, meta = Dict{DataType, Expr}()) @@ -232,66 +222,6 @@ function parse_variable_def!(dict, mod, arg, varclass, kwargs, where_types; varclass, where_types, meta) return var, def, Dict() end - Expr(:tuple, Expr(:(::), Expr(:ref, a, b...), type), y) || Expr(:tuple, Expr(:ref, a, b...), y) => begin - (@isdefined type) || (type = Real) - varname = Meta.isexpr(a, :call) ? a.args[1] : a - push!(kwargs, Expr(:kw, varname, nothing)) - varval = unit_handled_variable_value(mod, y, varname) - if varclass == :parameters - var = :($varname = $first(@parameters $a[$(b...)]::$type = ($varval, $y))) - else - var = :($varname = $first(@variables $a[$(b...)]::$type = ($varval, $y))) - end - #TODO: update `dict` aka `Model.structure` with the metadata - (:($varname...), var), nothing, Dict() - end - Expr(:(=), Expr(:(::), Expr(:ref, a, b...), type), y) || Expr(:(=), Expr(:ref, a, b...), y) => begin - (@isdefined type) || (type = Real) - varname = Meta.isexpr(a, :call) ? a.args[1] : a - if Meta.isexpr(y, :tuple) - varval = unit_handled_variable_value(mod, y, varname) - val, y = (y.args[1], y.args[2:end]) - push!(kwargs, Expr(:kw, varname, nothing)) - if varclass == :parameters - var = :($varname = $varname === nothing ? $val : $varname; - $varname = $first(@parameters $a[$(b...)]::$type = ( - $varval, $(y...)))) - else - var = :($varname = $varname === nothing ? $val : $varname; - $varname = $first(@variables $a[$(b...)]::$type = ( - $varval, $(y...)))) - end - else - push!(kwargs, Expr(:kw, varname, nothing)) - if varclass == :parameters - var = :($varname = $varname === nothing ? $y : $varname; - $varname = $first(@parameters $a[$(b...)]::$type = $varname)) - else - var = :($varname = $varname === nothing ? $y : $varname; - $varname = $first(@variables $a[$(b...)]::$type = $varname)) - end - end - #TODO: update `dict`` aka `Model.structure` with the metadata - (:($varname...), var), nothing, Dict() - end - Expr(:(::), Expr(:ref, a, b...), type) || Expr(:ref, a, b...) => begin - (@isdefined type) || (type = Real) - varname = a isa Expr && a.head == :call ? a.args[1] : a - push!(kwargs, Expr(:kw, varname, nothing)) - if varclass == :parameters - var = :($varname = $first(@parameters $a[$(b...)]::$type = $varname)) - elseif varclass == :variables - var = :($varname = $first(@variables $a[$(b...)]::$type = $varname)) - else - throw("Symbolic array with arbitrary length is not handled for $varclass. - Please open an issue with an example.") - end - dict[varclass] = get!(dict, varclass) do - Dict{Symbol, Dict{Symbol, Any}}() - end - # dict[:kwargs][varname] = dict[varclass][varname] = Dict(:size => b) - (:($varname...), var), nothing, Dict() - end Expr(:(=), a, b) => begin Base.remove_linenums!(b) def, meta = parse_default(mod, b) @@ -338,6 +268,11 @@ function parse_variable_def!(dict, mod, arg, varclass, kwargs, where_types; end return var, def, Dict() end + Expr(:ref, a, b...) => begin + indices = map(i -> UnitRange(i.args[2], i.args[end]), b) + parse_variable_def!(dict, mod, a, varclass, kwargs, where_types; + def, indices, type, meta) + end _ => error("$arg cannot be parsed") end end @@ -445,23 +380,14 @@ function parse_default(mod, a) end end -function parse_metadata(mod, a::Expr) +function parse_metadata(mod, a) MLStyle.@match a begin - Expr(:vect, b...) => Dict(parse_metadata(mod, m) for m in b) - Expr(:tuple, a, b...) => parse_metadata(mod, b) + Expr(:vect, eles...) => Dict(parse_metadata(mod, e) for e in eles) Expr(:(=), a, b) => Symbolics.option_to_metadata_type(Val(a)) => get_var(mod, b) _ => error("Cannot parse metadata $a") end end -function parse_metadata(mod, metadata::AbstractArray) - ret = Dict() - for m in metadata - merge!(ret, parse_metadata(mod, m)) - end - ret -end - function _set_var_metadata!(metadata_with_exprs, a, m, v::Expr) push!(metadata_with_exprs, m => v) a @@ -719,7 +645,6 @@ function parse_variable_arg!(exprs, vs, dict, mod, arg, varclass, kwargs, where_ end function convert_units(varunits::DynamicQuantities.Quantity, value) - value isa Nothing && return nothing DynamicQuantities.ustrip(DynamicQuantities.uconvert( DynamicQuantities.SymbolicUnits.as_quantity(varunits), value)) end @@ -731,7 +656,6 @@ function convert_units( end function convert_units(varunits::Unitful.FreeUnits, value) - value isa Nothing && return nothing Unitful.ustrip(varunits, value) end @@ -750,50 +674,47 @@ end function parse_variable_arg(dict, mod, arg, varclass, kwargs, where_types) vv, def, metadata_with_exprs = parse_variable_def!( dict, mod, arg, varclass, kwargs, where_types) - if !(vv isa Tuple) - name = getname(vv) - varexpr = if haskey(metadata_with_exprs, VariableUnit) - unit = metadata_with_exprs[VariableUnit] - quote - $name = if $name === nothing - $setdefault($vv, $def) - else - try - $setdefault($vv, $convert_units($unit, $name)) - catch e - if isa(e, $(DynamicQuantities.DimensionError)) || - isa(e, $(Unitful.DimensionError)) - error("Unable to convert units for \'" * string(:($$vv)) * "\'") - elseif isa(e, MethodError) - error("No or invalid units provided for \'" * string(:($$vv)) * - "\'") - else - rethrow(e) - end + name = getname(vv) + + varexpr = if haskey(metadata_with_exprs, VariableUnit) + unit = metadata_with_exprs[VariableUnit] + quote + $name = if $name === nothing + $setdefault($vv, $def) + else + try + $setdefault($vv, $convert_units($unit, $name)) + catch e + if isa(e, $(DynamicQuantities.DimensionError)) || + isa(e, $(Unitful.DimensionError)) + error("Unable to convert units for \'" * string(:($$vv)) * "\'") + elseif isa(e, MethodError) + error("No or invalid units provided for \'" * string(:($$vv)) * + "\'") + else + rethrow(e) end end end - else - quote - $name = if $name === nothing - $setdefault($vv, $def) - else - $setdefault($vv, $name) - end - end end - - metadata_expr = Expr(:block) - for (k, v) in metadata_with_exprs - push!(metadata_expr.args, - :($name = $wrap($set_scalar_metadata($unwrap($name), $k, $v)))) + else + quote + $name = if $name === nothing + $setdefault($vv, $def) + else + $setdefault($vv, $name) + end end + end - push!(varexpr.args, metadata_expr) - return vv isa Num ? name : :($name...), varexpr - else - return vv + metadata_expr = Expr(:block) + for (k, v) in metadata_with_exprs + push!(metadata_expr.args, + :($name = $wrap($set_scalar_metadata($unwrap($name), $k, $v)))) end + + push!(varexpr.args, metadata_expr) + return vv isa Num ? name : :($name...), varexpr end function handle_conditional_vars!( diff --git a/test/model_parsing.jl b/test/model_parsing.jl index 9d903842c2..6332dc12a5 100644 --- a/test/model_parsing.jl +++ b/test/model_parsing.jl @@ -259,8 +259,7 @@ end @test all(collect(hasmetadata.(model.l, ModelingToolkit.VariableDescription))) @test all(lastindex.([model.a2, model.b2, model.d2, model.e2, model.h2]) .== 2) - @test size(model.l) == (2, 3) - @test_broken MockModel.structure[:parameters][:l][:size] == (2, 3) + @test size(model.l) == MockModel.structure[:parameters][:l][:size] == (2, 3) model = complete(model) @test getdefault(model.cval) == 1 @@ -314,6 +313,7 @@ end @test_throws TypeError TypeModel(; name = :throws, par3 = true) @test_throws TypeError TypeModel(; name = :throws, par4 = true) # par7 should be an AbstractArray of BigFloat. + @test_throws MethodError TypeModel(; name = :throws, par7 = rand(Int, 3, 3)) # Test that array types are correctly added. @named type_model2 = TypeModel(; par5 = rand(BigFloat, 3)) @@ -474,8 +474,7 @@ using ModelingToolkit: getdefault, scalarize @named model_with_component_array = ModelWithComponentArray() - @test_broken eval(ModelWithComponentArray.structure[:parameters][:r][:unit]) == - eval(u"Ω") + @test eval(ModelWithComponentArray.structure[:parameters][:r][:unit]) == eval(u"Ω") @test lastindex(parameters(model_with_component_array)) == 3 # Test the constant `k`. Manually k's value should be kept in sync here @@ -877,45 +876,3 @@ end end), false) end - -@testset "Array Length as an Input" begin - @mtkmodel VaryingLengthArray begin - @structural_parameters begin - N - M - end - @parameters begin - p1[1:N] - p2[1:N, 1:M] - end - @variables begin - v1(t)[1:N] - v2(t)[1:N, 1:M] - end - end - - @named model = VaryingLengthArray(N = 2, M = 3) - @test length(model.p1) == 2 - @test size(model.p2) == (2, 3) - @test length(model.v1) == 2 - @test size(model.v2) == (2, 3) - - @mtkmodel WithMetadata begin - @structural_parameters begin - N - end - @parameters begin - p_only_default[1:N] = 101 - p_only_metadata[1:N], [description = "this only has metadata"] - p_both_default_and_metadata[1:N] = 102, - [description = "this has both default value and metadata"] - end - end - - @named with_metadata = WithMetadata(N = 10) - @test getdefault(with_metadata.p_only_default) == 101 - @test getdescription(with_metadata.p_only_metadata) == "this only has metadata" - @test getdefault(with_metadata.p_both_default_and_metadata) == 102 - @test getdescription(with_metadata.p_both_default_and_metadata) == - "this has both default value and metadata" -end From 09fda2ff33c616b76ab369bd2eae171feb10e94e Mon Sep 17 00:00:00 2001 From: Herman Sletmoen Date: Thu, 3 Oct 2024 20:56:44 +0200 Subject: [PATCH 0064/2176] Test linear equation system with vector variable --- test/nonlinearsystem.jl | 14 ++++++++++++++ test/reduction.jl | 1 + 2 files changed, 15 insertions(+) diff --git a/test/nonlinearsystem.jl b/test/nonlinearsystem.jl index a71d34a880..c42eb6f87b 100644 --- a/test/nonlinearsystem.jl +++ b/test/nonlinearsystem.jl @@ -325,3 +325,17 @@ end prob = @test_nowarn NonlinearProblem(sys, nothing) @test_nowarn solve(prob) end + +@testset "System of linear equations with vector variable" begin + # 1st example in https://en.wikipedia.org/w/index.php?title=System_of_linear_equations&oldid=1247697953 + @variables x[1:3] + A = [3 2 -1 + 2 -2 4 + -1 1/2 -1] + b = [1, -2, 0] + @named sys = NonlinearSystem(A * x ~ b, [x], []) + sys = structural_simplify(sys) + prob = NonlinearProblem(sys, unknowns(sys) .=> 0.0) + sol = solve(prob) + @test all(sol[x] .≈ A \ b) +end diff --git a/test/reduction.jl b/test/reduction.jl index 064a2efd81..80969d5794 100644 --- a/test/reduction.jl +++ b/test/reduction.jl @@ -178,6 +178,7 @@ A = reshape(1:(N^2), N, N) eqs = xs ~ A * xs @named sys′ = NonlinearSystem(eqs, [xs], []) sys = structural_simplify(sys′) +@test length(equations(sys)) == 3 && length(observed(sys)) == 2 # issue 958 @parameters k₁ k₂ k₋₁ E₀ From 7d7316ee84b58de3fb1829d30138b5db3359f496 Mon Sep 17 00:00:00 2001 From: Christopher Rackauckas Date: Fri, 4 Oct 2024 12:39:13 -0400 Subject: [PATCH 0065/2176] Update Project.toml --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 9a69a1467e..a08da6f952 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "ModelingToolkit" uuid = "961ee093-0014-501f-94e3-6117800e7a78" authors = ["Yingbo Ma ", "Chris Rackauckas and contributors"] -version = "9.41.0" +version = "9.42.0" [deps] AbstractTrees = "1520ce14-60c1-5f80-bbc7-55ef81b5835c" From 8dea9b111f76efdc69b5c1e39997bd15e79f3e89 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 27 Sep 2024 18:33:09 +0530 Subject: [PATCH 0066/2176] feat: improve error message when system contains array equations --- src/systems/abstractsystem.jl | 9 +++++++ src/systems/diffeqs/abstractodesystem.jl | 1 + test/odesystem.jl | 31 ++++++++++++++++++++++++ 3 files changed, 41 insertions(+) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index 958000ec3e..d91659c0d7 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -2878,6 +2878,15 @@ function Base.eltype(::Type{<:TreeIterator{ModelingToolkit.AbstractSystem}}) ModelingToolkit.AbstractSystem end +function check_array_equations_unknowns(eqs, dvs) + if any(eq -> Symbolics.isarraysymbolic(eq.lhs), eqs) + throw(ArgumentError("The system has array equations. Call `structural_simplify` to handle such equations or scalarize them manually.")) + end + if any(x -> Symbolics.isarraysymbolic(x), dvs) + throw(ArgumentError("The system has array unknowns. Call `structural_simplify` to handle this or scalarize them manually.")) + end +end + function check_eqs_u0(eqs, dvs, u0; check_length = true, kwargs...) if u0 !== nothing if check_length diff --git a/src/systems/diffeqs/abstractodesystem.jl b/src/systems/diffeqs/abstractodesystem.jl index 06feaac688..a7ae411def 100644 --- a/src/systems/diffeqs/abstractodesystem.jl +++ b/src/systems/diffeqs/abstractodesystem.jl @@ -815,6 +815,7 @@ function process_DEProblem(constructor, sys::AbstractODESystem, u0map, parammap; ps = parameters(sys) iv = get_iv(sys) + check_array_equations_unknowns(eqs, dvs) # TODO: Pass already computed information to varmap_to_vars call # in process_u0? That would just be a small optimization varmap = u0map === nothing || isempty(u0map) || eltype(u0map) <: Number ? diff --git a/test/odesystem.jl b/test/odesystem.jl index 47c8ad9661..bdaf489a32 100644 --- a/test/odesystem.jl +++ b/test/odesystem.jl @@ -1404,3 +1404,34 @@ end @test !ModelingToolkit.is_dde(sys) @test is_markovian(sys) end + +@testset "Issue #2597" begin + @variables x(t)[1:2]=ones(2) y(t)=1.0 + + for eqs in [D(x) ~ x, collect(D(x) .~ x)] + for dvs in [[x], collect(x)] + @named sys = ODESystem(eqs, t, dvs, []) + sys = complete(sys) + if eqs isa Vector && length(eqs) == 2 && length(dvs) == 2 + @test_nowarn ODEProblem(sys, [], (0.0, 1.0)) + else + @test_throws [ + r"array (equations|unknowns)", "structural_simplify", "scalarize"] ODEProblem( + sys, [], (0.0, 1.0)) + end + end + end + for eqs in [[D(x) ~ x, D(y) ~ y], [collect(D(x) .~ x); D(y) ~ y]] + for dvs in [[x, y], [x..., y]] + @named sys = ODESystem(eqs, t, dvs, []) + sys = complete(sys) + if eqs isa Vector && length(eqs) == 3 && length(dvs) == 3 + @test_nowarn ODEProblem(sys, [], (0.0, 1.0)) + else + @test_throws [ + r"array (equations|unknowns)", "structural_simplify", "scalarize"] ODEProblem( + sys, [], (0.0, 1.0)) + end + end + end +end From e926d061706a9e098b412b2f993dfc8cccd7ca28 Mon Sep 17 00:00:00 2001 From: Christopher Rackauckas Date: Sat, 5 Oct 2024 10:14:15 -0400 Subject: [PATCH 0067/2176] Test master --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index d93a18e3f7..59ced159df 100644 --- a/README.md +++ b/README.md @@ -24,6 +24,7 @@ For information on using the package, [in-development documentation](https://docs.sciml.ai/ModelingToolkit/dev/) for the version of the documentation which contains the unreleased features. + ## Standard Library For a standard library of ModelingToolkit components and blocks, check out the From 90567a05bb7a52fb7f50bd0f08197dd75d497643 Mon Sep 17 00:00:00 2001 From: ArnoStrouwen Date: Sat, 5 Oct 2024 16:48:26 +0200 Subject: [PATCH 0068/2176] move symbolic details to Symbolics docs --- docs/pages.jl | 1 - docs/src/examples/parsing.md | 33 -------------- .../tutorials/programmatically_generating.md | 44 ++----------------- 3 files changed, 3 insertions(+), 75 deletions(-) delete mode 100644 docs/src/examples/parsing.md diff --git a/docs/pages.jl b/docs/pages.jl index f92f869def..d772c32471 100644 --- a/docs/pages.jl +++ b/docs/pages.jl @@ -17,7 +17,6 @@ pages = [ "Basic Examples" => Any["examples/higher_order.md", "examples/spring_mass.md", "examples/modelingtoolkitize_index_reduction.md", - "examples/parsing.md", "examples/remake.md"], "Advanced Examples" => Any["examples/tearing_parallelism.md", "examples/sparse_jacobians.md", diff --git a/docs/src/examples/parsing.md b/docs/src/examples/parsing.md deleted file mode 100644 index 66a4e4d82f..0000000000 --- a/docs/src/examples/parsing.md +++ /dev/null @@ -1,33 +0,0 @@ -# Parsing Expressions into Solvable Systems - -Many times when creating DSLs or creating ModelingToolkit extensions to read new file formats, -it can become imperative to parse expressions. In many cases, it can be easy to use `Base.parse` -to take things to standard Julia expressions, but how can you take a `Base.Expr` and generate -symbolic forms from that? For example, say we had the following system we wanted to solve: - -```@example parsing -ex = [:(y ~ x) - :(y ~ -2x + 3 / z) - :(z ~ 2)] -``` - -We can use the function `parse_expr_to_symbolic` from Symbolics.jl to generate the symbolic -form of the expression: - -```@example parsing -using Symbolics -eqs = parse_expr_to_symbolic.(ex, (Main,)) -``` - -From there, we can use ModelingToolkit to transform the symbolic equations into a numerical -nonlinear solve: - -```@example parsing -using ModelingToolkit, SymbolicIndexingInterface, NonlinearSolve -vars = union(ModelingToolkit.vars.(eqs)...) -@mtkbuild ns = NonlinearSystem(eqs, vars, []) - -varmap = Dict(SymbolicIndexingInterface.getname.(vars) .=> vars) -prob = NonlinearProblem(ns, [varmap[:x] => 1.0, varmap[:y] => 1.0, varmap[:z] => 1.0]) -sol = solve(prob, NewtonRaphson()) -``` diff --git a/docs/src/tutorials/programmatically_generating.md b/docs/src/tutorials/programmatically_generating.md index bd1d2359f1..76d12dba2a 100644 --- a/docs/src/tutorials/programmatically_generating.md +++ b/docs/src/tutorials/programmatically_generating.md @@ -59,51 +59,13 @@ plot(sol) As you can see, generating an ODESystem is as simple as creating an array of equations and passing it to the `ODESystem` constructor. -## Understanding the Difference Between the Julia Variable and the Symbolic Variable - -In the most basic usage of ModelingToolkit and Symbolics, the name of the Julia variable -and the symbolic variable are the same. For example, when we do: - -```@example scripting -@variables a -``` - -the name of the symbolic variable is `a` and same with the Julia variable. However, we can -de-couple these by setting `a` to a new symbolic variable, for example: - -```@example scripting -b = only(@variables(a)) -``` - -Now the Julia variable `b` refers to the variable named `a`. However, the downside of this current -approach is that it requires that the user writing the script knows the name `a` that they want to -place to the variable. But what if for example we needed to get the variable's name from a file? - -To do this, one can interpolate a symbol into the `@variables` macro using `$`. For example: - -```@example scripting -a = :c -b = only(@variables($a)) -``` - -In this example, `@variables($a)` created a variable named `c`, and set this variable to `b`. - -Variables are not the only thing with names. For example, when you build a system, it knows its name -that name is used in the namespacing. In the standard usage, again the Julia variable and the -symbolic name are made the same via: - -```@example scripting -@named fol_model = ODESystem(eqs, t) -``` - -However, one can decouple these two properties by noting that `@named` is simply shorthand for the -following: +`@named` automatically gives a name to the `ODESystem`, and is shorthand for ```@example scripting -fol_model = ODESystem(eqs, t; name = :fol_model) +fol_model = ODESystem(eqs, t; name = :fol_model) # @named fol_model = ODESystem(eqs, t) ``` -Thus if we had read a name from a file and wish to populate an `ODESystem` with said name, we could do: +Thus, if we had read a name from a file and wish to populate an `ODESystem` with said name, we could do: ```@example scripting namesym = :name_from_file From 54548346624df893e645c50577f436a06e692bd9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Miclu=C8=9Ba-C=C3=A2mpeanu?= Date: Fri, 27 Sep 2024 00:59:35 +0300 Subject: [PATCH 0069/2176] refactor: relax type constraints to allow callable parameters in pdeps --- src/systems/abstractsystem.jl | 3 ++- src/systems/index_cache.jl | 16 ++++++++++++---- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index d91659c0d7..aa02a5fb73 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -3090,7 +3090,7 @@ function process_parameter_dependencies(pdeps, ps) end for p in pdeps] end - lhss = BasicSymbolic[] + lhss = [] for p in pdeps if !isparameter(p.lhs) error("LHS of parameter dependency must be a single parameter. Found $(p.lhs).") @@ -3101,6 +3101,7 @@ function process_parameter_dependencies(pdeps, ps) end push!(lhss, p.lhs) end + lhss = map(identity, lhss) pdeps = topsort_equations(pdeps, union(ps, lhss)) ps = filter(ps) do p !any(isequal(p), lhss) diff --git a/src/systems/index_cache.jl b/src/systems/index_cache.jl index 90ca3eb781..52565493db 100644 --- a/src/systems/index_cache.jl +++ b/src/systems/index_cache.jl @@ -39,7 +39,7 @@ const UnknownIndexMap = Dict{ const TunableIndexMap = Dict{BasicSymbolic, Union{Int, UnitRange{Int}, Base.ReshapedArray{Int, N, UnitRange{Int}} where {N}}} -struct IndexCache +struct IndexCache{D} unknown_idx::UnknownIndexMap # sym => (bufferidx, idx_in_buffer) discrete_idx::Dict{BasicSymbolic, DiscreteIndex} @@ -49,7 +49,7 @@ struct IndexCache constant_idx::ParamIndexMap nonnumeric_idx::NonnumericMap observed_syms::Set{BasicSymbolic} - dependent_pars::Set{BasicSymbolic} + dependent_pars::Set{D} discrete_buffer_sizes::Vector{Vector{BufferTemplate}} tunable_buffer_size::BufferTemplate constant_buffer_sizes::Vector{BufferTemplate} @@ -275,7 +275,15 @@ function IndexCache(sys::AbstractSystem) end end - dependent_pars = Set{BasicSymbolic}() + pdeps = parameter_dependencies(sys) + + D = if isempty(pdeps) + BasicSymbolic + else + mapreduce(typeof, promote_type, getproperty.(pdeps, :lhs)) + end + dependent_pars = Set{D}() + for eq in parameter_dependencies(sys) sym = eq.lhs ttsym = default_toterm(sym) @@ -289,7 +297,7 @@ function IndexCache(sys::AbstractSystem) end end - return IndexCache( + return IndexCache{D}( unk_idxs, disc_idxs, callback_to_clocks, From 61d986c413787e58b525bed66dec0885be52f448 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Miclu=C8=9Ba-C=C3=A2mpeanu?= Date: Fri, 27 Sep 2024 16:57:57 +0300 Subject: [PATCH 0070/2176] refactor: avoid type parameters in IndexCache --- src/systems/index_cache.jl | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/src/systems/index_cache.jl b/src/systems/index_cache.jl index 52565493db..00f7837407 100644 --- a/src/systems/index_cache.jl +++ b/src/systems/index_cache.jl @@ -39,7 +39,7 @@ const UnknownIndexMap = Dict{ const TunableIndexMap = Dict{BasicSymbolic, Union{Int, UnitRange{Int}, Base.ReshapedArray{Int, N, UnitRange{Int}} where {N}}} -struct IndexCache{D} +struct IndexCache unknown_idx::UnknownIndexMap # sym => (bufferidx, idx_in_buffer) discrete_idx::Dict{BasicSymbolic, DiscreteIndex} @@ -49,7 +49,7 @@ struct IndexCache{D} constant_idx::ParamIndexMap nonnumeric_idx::NonnumericMap observed_syms::Set{BasicSymbolic} - dependent_pars::Set{D} + dependent_pars::Set{Union{BasicSymbolic, CallWithMetadata}} discrete_buffer_sizes::Vector{Vector{BufferTemplate}} tunable_buffer_size::BufferTemplate constant_buffer_sizes::Vector{BufferTemplate} @@ -275,14 +275,7 @@ function IndexCache(sys::AbstractSystem) end end - pdeps = parameter_dependencies(sys) - - D = if isempty(pdeps) - BasicSymbolic - else - mapreduce(typeof, promote_type, getproperty.(pdeps, :lhs)) - end - dependent_pars = Set{D}() + dependent_pars = Set{Union{BasicSymbolic, CallWithMetadata}}() for eq in parameter_dependencies(sys) sym = eq.lhs @@ -297,7 +290,7 @@ function IndexCache(sys::AbstractSystem) end end - return IndexCache{D}( + return IndexCache( unk_idxs, disc_idxs, callback_to_clocks, From 535fd5216b8c0d51e9e756f3f6abf6136a8e40e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Miclu=C8=9Ba-C=C3=A2mpeanu?= Date: Fri, 27 Sep 2024 18:16:59 +0300 Subject: [PATCH 0071/2176] test: add test for callable pdeps --- test/parameter_dependencies.jl | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/test/parameter_dependencies.jl b/test/parameter_dependencies.jl index 034c27041e..61ddab20ba 100644 --- a/test/parameter_dependencies.jl +++ b/test/parameter_dependencies.jl @@ -177,6 +177,27 @@ end @test SciMLBase.successful_retcode(sol) end +struct CallableFoo + p +end + +(f::CallableFoo)(x) = f.p+x + +@testset "callable parameters" begin + @variables y(t) = 1 + @parameters p = 2 (i::CallableFoo)(..) + + eqs = [D(y) ~ i(t)+p] + @named model = ODESystem(eqs, t, [y], [p, i]; + parameter_dependencies = [i ~ CallableFoo(p)]) + sys = structural_simplify(model) + + prob = ODEProblem(sys, [], (0.0, 1.0)) + sol = solve(prob, Tsit5()) + + @test SciMLBase.successful_retcode(sol) +end + @testset "Clock system" begin dt = 0.1 @variables x(t) y(t) u(t) yd(t) ud(t) r(t) z(t) From 6f96622c6c41ae0630f25448d4d242185c02905d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Miclu=C8=9Ba-C=C3=A2mpeanu?= Date: Fri, 4 Oct 2024 18:25:05 +0300 Subject: [PATCH 0072/2176] test: fix pdeps test Co-authored-by: Aayush Sabharwal --- test/parameter_dependencies.jl | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/test/parameter_dependencies.jl b/test/parameter_dependencies.jl index 61ddab20ba..9cfd4ca5c5 100644 --- a/test/parameter_dependencies.jl +++ b/test/parameter_dependencies.jl @@ -178,16 +178,18 @@ end end struct CallableFoo - p + p::Any end -(f::CallableFoo)(x) = f.p+x +@register_symbolic CallableFoo(x) + +(f::CallableFoo)(x) = f.p + x @testset "callable parameters" begin @variables y(t) = 1 - @parameters p = 2 (i::CallableFoo)(..) + @parameters p=2 (i::CallableFoo)(..) - eqs = [D(y) ~ i(t)+p] + eqs = [D(y) ~ i(t) + p] @named model = ODESystem(eqs, t, [y], [p, i]; parameter_dependencies = [i ~ CallableFoo(p)]) sys = structural_simplify(model) From eb8b6175725bc9025ce593da7daf00a91e8d550b Mon Sep 17 00:00:00 2001 From: Christopher Rackauckas Date: Sat, 5 Oct 2024 12:58:47 -0400 Subject: [PATCH 0073/2176] Update tearing.jl --- test/structural_transformation/tearing.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/structural_transformation/tearing.jl b/test/structural_transformation/tearing.jl index dea6af0b11..605e6885ed 100644 --- a/test/structural_transformation/tearing.jl +++ b/test/structural_transformation/tearing.jl @@ -169,7 +169,7 @@ infprob = ODEProblem(structural_simplify(sys), [x => 1.0], (0, 1.0), [p => 0.2]) infprob.f(du, infprob.u0, pr, tt) @test any(isnan, du) -sol1 = solve(prob, RosShamp4(), reltol = 8e-7) +sol1 = solve(prob, RosShamp4(), reltol = 1e-7) sol2 = solve(ODEProblem{false}((u, p, t) -> [-asin(u[1] - pr * t)], [1.0], (0, 1.0), From b57f7f3555f80ed0c189ca027c06211192cb116c Mon Sep 17 00:00:00 2001 From: Christopher Rackauckas Date: Sat, 5 Oct 2024 15:21:47 -0400 Subject: [PATCH 0074/2176] Update tearing.jl --- test/structural_transformation/tearing.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/structural_transformation/tearing.jl b/test/structural_transformation/tearing.jl index 605e6885ed..0ac37aed05 100644 --- a/test/structural_transformation/tearing.jl +++ b/test/structural_transformation/tearing.jl @@ -169,7 +169,7 @@ infprob = ODEProblem(structural_simplify(sys), [x => 1.0], (0, 1.0), [p => 0.2]) infprob.f(du, infprob.u0, pr, tt) @test any(isnan, du) -sol1 = solve(prob, RosShamp4(), reltol = 1e-7) +sol1 = solve(prob, RosShamp4(), reltol = 1e-7, dtmax = 0.1) sol2 = solve(ODEProblem{false}((u, p, t) -> [-asin(u[1] - pr * t)], [1.0], (0, 1.0), From 8f8cf61b9ef30819f2e996365703c0cf0ec45c2c Mon Sep 17 00:00:00 2001 From: Karl Wessel Date: Mon, 7 Oct 2024 09:43:46 +0200 Subject: [PATCH 0075/2176] fix first ODE perturbation example --- docs/src/examples/perturbation.md | 31 +++++++++++++++++++------------ 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/docs/src/examples/perturbation.md b/docs/src/examples/perturbation.md index f603178e37..fc3ec01aaf 100644 --- a/docs/src/examples/perturbation.md +++ b/docs/src/examples/perturbation.md @@ -2,13 +2,12 @@ ## Prelims -In the previous tutorial, [Mixed Symbolic-Numeric Perturbation Theory](https://symbolics.juliasymbolics.org/stable/examples/perturbation/), we discussed how to solve algebraic equations using **Symbolics.jl**. Here, our goal is to extend the method to differential equations. First, we import the following helper functions that were introduced in [Mixed Symbolic/Numerical Methods for Perturbation Theory - Algebraic Equations](@ref perturb_alg): +In the previous tutorial, [Mixed Symbolic-Numeric Perturbation Theory](https://symbolics.juliasymbolics.org/stable/examples/perturbation/), we discussed how to solve algebraic equations using **Symbolics.jl**. Here, our goal is to extend the method to differential equations. First, we import the following helper functions that were introduced in [Mixed Symbolic/Numerical Methods for Perturbation Theory - Algebraic Equations](https://symbolics.juliasymbolics.org/stable/examples/perturbation/): ```julia using Symbolics, SymbolicUtils -def_taylor(x, ps) = sum([a * x^i for (i, a) in enumerate(ps)]) -def_taylor(x, ps, p₀) = p₀ + def_taylor(x, ps) +def_taylor(x, ps) = sum([a * x^(i - 1) for (i, a) in enumerate(ps)]) function collect_powers(eq, x, ns; max_power = 100) eq = substitute(expand(eq), Dict(x^j => 0 for j in (last(ns) + 1):max_power)) @@ -16,7 +15,14 @@ function collect_powers(eq, x, ns; max_power = 100) eqs = [] for i in ns powers = Dict(x^j => (i == j ? 1 : 0) for j in 1:last(ns)) - push!(eqs, substitute(eq, powers)) + e = substitute(eq, powers) + + # manually remove zeroth order from higher orders + if 0 in ns && i != 0 + e = e - eqs[1] + end + + push!(eqs, e) end eqs end @@ -46,20 +52,21 @@ with the initial conditions $x(0) = 0$, and $\dot{x}(0) = 1$. Note that for $\ep ```julia using ModelingToolkit: t_nounits as t, D_nounits as D -n = 3 -@variables ϵ y[1:n](t) ∂∂y[1:n](t) +order = 2 +n = order + 1 +@variables ϵ (y(t))[1:n] (∂∂y(t))[1:n] ``` Next, we define $x$. ```julia -x = def_taylor(ϵ, y[3:end], y[2]) +x = def_taylor(ϵ, y) ``` We need the second derivative of `x`. It may seem that we can do this using `Differential(t)`; however, this operation needs to wait for a few steps because we need to manipulate the differentials as separate variables. Instead, we define dummy variables `∂∂y` as the placeholder for the second derivatives and define ```julia -∂∂x = def_taylor(ϵ, ∂∂y[3:end], ∂∂y[2]) +∂∂x = def_taylor(ϵ, ∂∂y) ``` as the second derivative of `x`. After rearrangement, our governing equation is $\ddot{x}(t)(1 + \epsilon x(t))^{-2} + 1 = 0$, or @@ -71,7 +78,7 @@ eq = ∂∂x * (1 + ϵ * x)^2 + 1 The next two steps are the same as the ones for algebraic equations (note that we pass `1:n` to `collect_powers` because the zeroth order term is needed here) ```julia -eqs = collect_powers(eq, ϵ, 1:n) +eqs = collect_powers(eq, ϵ, 0:order) ``` and, @@ -99,8 +106,8 @@ unknowns(sys) ```julia # the initial conditions # everything is zero except the initial velocity -u0 = zeros(2n + 2) -u0[3] = 1.0 # y₀ˍt +u0 = zeros(2order + 2) +u0[2] = 1.0 # yˍt₁ prob = ODEProblem(sys, u0, (0, 3.0)) sol = solve(prob; dtmax = 0.01) @@ -109,7 +116,7 @@ sol = solve(prob; dtmax = 0.01) Finally, we calculate the solution to the problem as a function of `ϵ` by substituting the solution to the ODE system back into the defining equation for `x`. Note that `𝜀` is a number, compared to `ϵ`, which is a symbolic variable. ```julia -X = 𝜀 -> sum([𝜀^(i - 1) * sol[y[i]] for i in eachindex(y)]) +X = 𝜀 -> sum([𝜀^(i - 1) * sol[yi] for (i, yi) in enumerate(y)]) ``` Using `X`, we can plot the trajectory for a range of $𝜀$s. From 163be441de8f7806b0245de80914dc2934d9a010 Mon Sep 17 00:00:00 2001 From: Herman Sletmoen Date: Mon, 7 Oct 2024 12:56:01 +0200 Subject: [PATCH 0076/2176] Ensure OffsetArrays dependency is removed when non-1-indexed array hack is removed --- src/structural_transformation/symbolics_tearing.jl | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/structural_transformation/symbolics_tearing.jl b/src/structural_transformation/symbolics_tearing.jl index f346e20b2a..84cee928cd 100644 --- a/src/structural_transformation/symbolics_tearing.jl +++ b/src/structural_transformation/symbolics_tearing.jl @@ -589,7 +589,10 @@ function tearing_reassemble(state::TearingState, var_eq_matching, haskey(obs_arr_subs, arg1) && continue obs_arr_subs[arg1] = [arg1[i] for i in eachindex(arg1)] # e.g. p => [p[1], p[2]] index_first = eachindex(arg1)[1] - obs_arr_subs[arg1] = Origin(index_first)(obs_arr_subs[arg1]) # respect non-1-indexed arrays + + # respect non-1-indexed arrays + # TODO: get rid of this hack together with the above hack, then remove OffsetArrays dependency + obs_arr_subs[arg1] = Origin(index_first)(obs_arr_subs[arg1]) end for i in eachindex(neweqs) neweqs[i] = fast_substitute(neweqs[i], obs_arr_subs; operator = Symbolics.Operator) From 91e5e70aa19791b958d41c1bc1f5bcdfbe974366 Mon Sep 17 00:00:00 2001 From: DhairyaLGandhi Date: Mon, 7 Oct 2024 19:16:48 +0530 Subject: [PATCH 0077/2176] chore: handle arrays in MTKParameters pullback --- ext/MTKChainRulesCoreExt.jl | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/ext/MTKChainRulesCoreExt.jl b/ext/MTKChainRulesCoreExt.jl index f84690e23f..635f9d12cf 100644 --- a/ext/MTKChainRulesCoreExt.jl +++ b/ext/MTKChainRulesCoreExt.jl @@ -7,7 +7,8 @@ import ChainRulesCore: Tangent, ZeroTangent, NoTangent, zero_tangent, unthunk function ChainRulesCore.rrule(::Type{MTK.MTKParameters}, tunables, args...) function mtp_pullback(dt) dt = unthunk(dt) - (NoTangent(), dt.tunable[1:length(tunables)], + dtunables = dt isa AbstractArray ? dt : dt.tunable + (NoTangent(), dtunables[1:length(tunables)], ntuple(_ -> NoTangent(), length(args))...) end MTK.MTKParameters(tunables, args...), mtp_pullback From 295edb61d5ca07ff3b78502fe2a5dad9699d8e02 Mon Sep 17 00:00:00 2001 From: Christopher Rackauckas Date: Mon, 7 Oct 2024 10:18:58 -0400 Subject: [PATCH 0078/2176] Update Project.toml --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 0843bb934d..3c21f3db31 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "ModelingToolkit" uuid = "961ee093-0014-501f-94e3-6117800e7a78" authors = ["Yingbo Ma ", "Chris Rackauckas and contributors"] -version = "9.42.0" +version = "9.43.0" [deps] AbstractTrees = "1520ce14-60c1-5f80-bbc7-55ef81b5835c" From a7adb5386da93044f424e3d078ff2cc51b9e3ebf Mon Sep 17 00:00:00 2001 From: Karl Royen Date: Mon, 7 Oct 2024 16:49:53 +0200 Subject: [PATCH 0079/2176] fix second part of example on perturbation theory for ODEs --- docs/src/examples/perturbation.md | 63 ++++++++++++++++--------------- 1 file changed, 32 insertions(+), 31 deletions(-) diff --git a/docs/src/examples/perturbation.md b/docs/src/examples/perturbation.md index fc3ec01aaf..6fb094f8af 100644 --- a/docs/src/examples/perturbation.md +++ b/docs/src/examples/perturbation.md @@ -4,8 +4,8 @@ In the previous tutorial, [Mixed Symbolic-Numeric Perturbation Theory](https://symbolics.juliasymbolics.org/stable/examples/perturbation/), we discussed how to solve algebraic equations using **Symbolics.jl**. Here, our goal is to extend the method to differential equations. First, we import the following helper functions that were introduced in [Mixed Symbolic/Numerical Methods for Perturbation Theory - Algebraic Equations](https://symbolics.juliasymbolics.org/stable/examples/perturbation/): -```julia -using Symbolics, SymbolicUtils +```@example perturbation +using Symbolics def_taylor(x, ps) = sum([a * x^(i - 1) for (i, a) in enumerate(ps)]) @@ -17,10 +17,10 @@ function collect_powers(eq, x, ns; max_power = 100) powers = Dict(x^j => (i == j ? 1 : 0) for j in 1:last(ns)) e = substitute(eq, powers) - # manually remove zeroth order from higher orders - if 0 in ns && i != 0 - e = e - eqs[1] - end + # manually remove zeroth order from higher orders + if 0 in ns && i != 0 + e = e - eqs[1] + end push!(eqs, e) end @@ -50,7 +50,7 @@ As the first ODE example, we have chosen a simple and well-behaved problem, whic with the initial conditions $x(0) = 0$, and $\dot{x}(0) = 1$. Note that for $\epsilon = 0$, this equation transforms back to the standard one. Let's start with defining the variables -```julia +```@example perturbation using ModelingToolkit: t_nounits as t, D_nounits as D order = 2 n = order + 1 @@ -59,69 +59,69 @@ n = order + 1 Next, we define $x$. -```julia +```@example perturbation x = def_taylor(ϵ, y) ``` We need the second derivative of `x`. It may seem that we can do this using `Differential(t)`; however, this operation needs to wait for a few steps because we need to manipulate the differentials as separate variables. Instead, we define dummy variables `∂∂y` as the placeholder for the second derivatives and define -```julia +```@example perturbation ∂∂x = def_taylor(ϵ, ∂∂y) ``` as the second derivative of `x`. After rearrangement, our governing equation is $\ddot{x}(t)(1 + \epsilon x(t))^{-2} + 1 = 0$, or -```julia +```@example perturbation eq = ∂∂x * (1 + ϵ * x)^2 + 1 ``` The next two steps are the same as the ones for algebraic equations (note that we pass `1:n` to `collect_powers` because the zeroth order term is needed here) -```julia +```@example perturbation eqs = collect_powers(eq, ϵ, 0:order) ``` and, -```julia +```@example perturbation vals = solve_coef(eqs, ∂∂y) ``` Our system of ODEs is forming. Now is the time to convert `∂∂`s to the correct **Symbolics.jl** form by substitution: -```julia +```@example perturbation subs = Dict(∂∂y[i] => D(D(y[i])) for i in eachindex(y)) eqs = [substitute(first(v), subs) ~ substitute(last(v), subs) for v in vals] ``` We are nearly there! From this point on, the rest is standard ODE solving procedures. Potentially, we can use a symbolic ODE solver to find a closed form solution to this problem. However, **Symbolics.jl** currently does not support this functionality. Instead, we solve the problem numerically. We form an `ODESystem`, lower the order (convert second derivatives to first), generate an `ODEProblem` (after passing the correct initial conditions), and, finally, solve it. -```julia +```@example perturbation using ModelingToolkit, DifferentialEquations @mtkbuild sys = ODESystem(eqs, t) unknowns(sys) ``` -```julia +```@example perturbation # the initial conditions # everything is zero except the initial velocity u0 = zeros(2order + 2) u0[2] = 1.0 # yˍt₁ prob = ODEProblem(sys, u0, (0, 3.0)) -sol = solve(prob; dtmax = 0.01) +sol = solve(prob; dtmax = 0.01); ``` Finally, we calculate the solution to the problem as a function of `ϵ` by substituting the solution to the ODE system back into the defining equation for `x`. Note that `𝜀` is a number, compared to `ϵ`, which is a symbolic variable. -```julia +```@example perturbation X = 𝜀 -> sum([𝜀^(i - 1) * sol[yi] for (i, yi) in enumerate(y)]) ``` Using `X`, we can plot the trajectory for a range of $𝜀$s. -```julia +```@example perturbation using Plots plot(sol.t, hcat([X(𝜀) for 𝜀 in 0.0:0.1:0.5]...)) @@ -135,17 +135,18 @@ For the next example, we have chosen a simple example from a very important clas The goal is to solve $\ddot{x} + 2\epsilon\dot{x} + x = 0$, where the dot signifies time-derivatives and the initial conditions are $x(0) = 0$ and $\dot{x}(0) = 1$. If $\epsilon = 0$, the problem reduces to the simple linear harmonic oscillator with the exact solution $x(t) = \sin(t)$. We follow the same steps as the previous example. -```julia -n = 3 -@variables ϵ t y[1:n](t) ∂y[1:n] ∂∂y[1:n] -x = def_taylor(ϵ, y[3:end], y[2]) -∂x = def_taylor(ϵ, ∂y[3:end], ∂y[2]) -∂∂x = def_taylor(ϵ, ∂∂y[3:end], ∂∂y[2]) +```@example perturbation +order = 2 +n = order + 1 +@variables ϵ (y(t))[1:n] (∂y)[1:n] (∂∂y)[1:n] +x = def_taylor(ϵ, y) +∂x = def_taylor(ϵ, ∂y) +∂∂x = def_taylor(ϵ, ∂∂y) ``` This time we also need the first derivative terms. Continuing, -```julia +```@example perturbation eq = ∂∂x + 2 * ϵ * ∂x + x eqs = collect_powers(eq, ϵ, 0:n) vals = solve_coef(eqs, ∂∂y) @@ -153,7 +154,7 @@ vals = solve_coef(eqs, ∂∂y) Next, we need to replace `∂`s and `∂∂`s with their **Symbolics.jl** counterparts: -```julia +```@example perturbation subs1 = Dict(∂y[i] => D(y[i]) for i in eachindex(y)) subs2 = Dict(∂∂y[i] => D(D(y[i])) for i in eachindex(y)) subs = subs1 ∪ subs2 @@ -162,19 +163,19 @@ eqs = [substitute(first(v), subs) ~ substitute(last(v), subs) for v in vals] We continue with converting 'eqs' to an `ODEProblem`, solving it, and finally plot the results against the exact solution to the original problem, which is $x(t, \epsilon) = (1 - \epsilon)^{-1/2} e^{-\epsilon t} \sin((1- \epsilon^2)^{1/2}t)$, -```julia +```@example perturbation @mtkbuild sys = ODESystem(eqs, t) ``` -```julia +```@example perturbation # the initial conditions -u0 = zeros(2n + 2) -u0[3] = 1.0 # y₀ˍt +u0 = zeros(2order + 2) +u0[1] = 1.0 # yˍt₁ prob = ODEProblem(sys, u0, (0, 50.0)) sol = solve(prob; dtmax = 0.01) -X = 𝜀 -> sum([𝜀^(i - 1) * sol[y[i]] for i in eachindex(y)]) +X = 𝜀 -> sum([𝜀^(i - 1) * sol[yi] for (i, yi) in enumerate(y)]) T = sol.t Y = 𝜀 -> exp.(-𝜀 * T) .* sin.(sqrt(1 - 𝜀^2) * T) / sqrt(1 - 𝜀^2) # exact solution From 87280efb223fe75495b80334f14f4347bd4b4fa6 Mon Sep 17 00:00:00 2001 From: romain veltz Date: Mon, 7 Oct 2024 20:51:08 +0200 Subject: [PATCH 0080/2176] towards BifurcationKit @0.4 --- Project.toml | 2 +- ext/MTKBifurcationKitExt.jl | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Project.toml b/Project.toml index 3c21f3db31..73c2fa4c8a 100644 --- a/Project.toml +++ b/Project.toml @@ -72,7 +72,7 @@ MTKLabelledArraysExt = "LabelledArrays" [compat] AbstractTrees = "0.3, 0.4" ArrayInterface = "6, 7" -BifurcationKit = "0.3" +BifurcationKit = "0.3, 0.4" BlockArrays = "1.1" ChainRulesCore = "1" Combinatorics = "1" diff --git a/ext/MTKBifurcationKitExt.jl b/ext/MTKBifurcationKitExt.jl index 6bc536958f..a0bd3bfc02 100644 --- a/ext/MTKBifurcationKitExt.jl +++ b/ext/MTKBifurcationKitExt.jl @@ -113,7 +113,7 @@ function BifurcationKit.BifurcationProblem(nsys::NonlinearSystem, # If the plot var is a normal state. if any(isequal(plot_var, var) for var in unknowns(nsys)) plot_idx = findfirst(isequal(plot_var), unknowns(nsys)) - record_from_solution = (x, p) -> x[plot_idx] + record_from_solution = (x, p; k...) -> x[plot_idx] # If the plot var is an observed state. elseif any(isequal(plot_var, eq.lhs) for eq in observed(nsys)) @@ -132,7 +132,7 @@ function BifurcationKit.BifurcationProblem(nsys::NonlinearSystem, return BifurcationKit.BifurcationProblem(F, u0_bif_vals, p_vals, - (@lens _[bif_idx]), + (BifurcationKit.@optic _[bif_idx]), args...; record_from_solution = record_from_solution, J = J, From 8eaa427c4c54639863b03d609a02c12ed64996ee Mon Sep 17 00:00:00 2001 From: Karl Wessel Date: Tue, 8 Oct 2024 06:59:47 +0200 Subject: [PATCH 0081/2176] use more robust definition of initial conditions for perturbation example --- docs/src/examples/perturbation.md | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/docs/src/examples/perturbation.md b/docs/src/examples/perturbation.md index 6fb094f8af..3ebdc3488a 100644 --- a/docs/src/examples/perturbation.md +++ b/docs/src/examples/perturbation.md @@ -106,8 +106,7 @@ unknowns(sys) ```@example perturbation # the initial conditions # everything is zero except the initial velocity -u0 = zeros(2order + 2) -u0[2] = 1.0 # yˍt₁ +u0 = Dict([unknowns(sys) .=> 0; D(y[1]) => 1]) prob = ODEProblem(sys, u0, (0, 3.0)) sol = solve(prob; dtmax = 0.01); @@ -169,8 +168,7 @@ We continue with converting 'eqs' to an `ODEProblem`, solving it, and finally pl ```@example perturbation # the initial conditions -u0 = zeros(2order + 2) -u0[1] = 1.0 # yˍt₁ +u0 = Dict([unknowns(sys) .=> 0; D(y[1]) => 1]) prob = ODEProblem(sys, u0, (0, 50.0)) sol = solve(prob; dtmax = 0.01) From ce64618e3c913a35613d999ae690b603b8383d90 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 8 Oct 2024 12:13:13 +0530 Subject: [PATCH 0082/2176] fix: fix test --- test/structural_transformation/tearing.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/structural_transformation/tearing.jl b/test/structural_transformation/tearing.jl index 0ac37aed05..32e839d802 100644 --- a/test/structural_transformation/tearing.jl +++ b/test/structural_transformation/tearing.jl @@ -169,7 +169,7 @@ infprob = ODEProblem(structural_simplify(sys), [x => 1.0], (0, 1.0), [p => 0.2]) infprob.f(du, infprob.u0, pr, tt) @test any(isnan, du) -sol1 = solve(prob, RosShamp4(), reltol = 1e-7, dtmax = 0.1) +sol1 = solve(prob, RosShamp4(), reltol = 9e-4) sol2 = solve(ODEProblem{false}((u, p, t) -> [-asin(u[1] - pr * t)], [1.0], (0, 1.0), From f63257691840b9ef2de353efb7095e2d22a3fe58 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 8 Oct 2024 12:13:19 +0530 Subject: [PATCH 0083/2176] refactor: format --- README.md | 1 - 1 file changed, 1 deletion(-) diff --git a/README.md b/README.md index 59ced159df..d93a18e3f7 100644 --- a/README.md +++ b/README.md @@ -24,7 +24,6 @@ For information on using the package, [in-development documentation](https://docs.sciml.ai/ModelingToolkit/dev/) for the version of the documentation which contains the unreleased features. - ## Standard Library For a standard library of ModelingToolkit components and blocks, check out the From b16031690acebc5d703c2eacfc640c41f6977d33 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 8 Oct 2024 13:03:22 +0530 Subject: [PATCH 0084/2176] test: fix depwarn in dde test --- test/dde.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/dde.jl b/test/dde.jl index 7a6f46f723..f5e72ee1bb 100644 --- a/test/dde.jl +++ b/test/dde.jl @@ -124,7 +124,7 @@ sol = solve(prob, MethodOfSteps(Tsit5())) obsfn = ModelingToolkit.build_explicit_observed_function( sys, [sys.osc1.delx, sys.osc2.delx]) @test_nowarn sol[[sys.osc1.delx, sys.osc2.delx]] -@test sol[sys.osc1.delx] ≈ sol(sol.t .- 0.01; idxs = sys.osc1.x) +@test sol[sys.osc1.delx] ≈ sol(sol.t .- 0.01; idxs = sys.osc1.x).u @testset "DDE observed with array variables" begin @component function valve(; name) From 7aac0fdd0cb413431c8ce21c4b744477a67b84fe Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 8 Oct 2024 14:35:06 +0530 Subject: [PATCH 0085/2176] test: decrease solve tolerance, increase test tolerance --- test/structural_transformation/tearing.jl | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/structural_transformation/tearing.jl b/test/structural_transformation/tearing.jl index 32e839d802..60edcb76c5 100644 --- a/test/structural_transformation/tearing.jl +++ b/test/structural_transformation/tearing.jl @@ -169,7 +169,7 @@ infprob = ODEProblem(structural_simplify(sys), [x => 1.0], (0, 1.0), [p => 0.2]) infprob.f(du, infprob.u0, pr, tt) @test any(isnan, du) -sol1 = solve(prob, RosShamp4(), reltol = 9e-4) +sol1 = solve(prob, RosShamp4(), reltol = 2e-4) sol2 = solve(ODEProblem{false}((u, p, t) -> [-asin(u[1] - pr * t)], [1.0], (0, 1.0), @@ -179,11 +179,11 @@ sol2 = solve(ODEProblem{false}((u, p, t) -> [-asin(u[1] - pr * t)], @test sol1[x] == first.(sol1.u) @test sol1[y] == first.(sol1.u) -@test sin.(sol1[z]) .+ sol1[y]≈pr[1] * sol1.t atol=5e-5 +@test sin.(sol1[z]) .+ sol1[y]≈pr[1] * sol1.t atol=8e-4 @test sol1[sin(z) + y]≈sin.(sol1[z]) .+ sol1[y] rtol=1e-12 @test sol1[y, :] == sol1[x, :] -@test (@. sin(sol1[z, :]) + sol1[y, :])≈pr * sol1.t atol=5e-5 +@test (@. sin(sol1[z, :]) + sol1[y, :])≈pr * sol1.t atol=8e-4 # 1426 function Translational_Mass(; name, m = 1.0) From 1f81661c7cc0cb260e495a17e62156fdcac201ca Mon Sep 17 00:00:00 2001 From: Christopher Rackauckas Date: Tue, 8 Oct 2024 06:44:03 -0400 Subject: [PATCH 0086/2176] Update Project.toml --- Project.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/Project.toml b/Project.toml index 3c21f3db31..bb13481cf4 100644 --- a/Project.toml +++ b/Project.toml @@ -107,6 +107,7 @@ LinearAlgebra = "1" MLStyle = "0.4.17" NaNMath = "0.3, 1" NonlinearSolve = "3.14" +OffsetArrays = "1" OrderedCollections = "1" OrdinaryDiffEq = "6.82.0" PrecompileTools = "1" From 03c3861feb63338abf949e703718228f698687cd Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 9 Oct 2024 11:24:07 +0530 Subject: [PATCH 0087/2176] test: add REPL as a test dependency --- Project.toml | 4 +++- test/runtests.jl | 2 ++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index a08da6f952..3e2b1e22dc 100644 --- a/Project.toml +++ b/Project.toml @@ -109,6 +109,7 @@ NonlinearSolve = "3.14" OrderedCollections = "1" OrdinaryDiffEq = "6.82.0" PrecompileTools = "1" +REPL = "1" RecursiveArrayTools = "3.26" Reexport = "0.2, 1" RuntimeGeneratedFunctions = "0.5.9" @@ -146,6 +147,7 @@ OptimizationMOI = "fd9f6733-72f4-499f-8506-86b2bdd0dea1" OptimizationOptimJL = "36348300-93cb-4f02-beb5-3c3902f8871e" OrdinaryDiffEq = "1dea7af3-3e70-54e6-95c3-0bf5283fa5ed" Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f" +REPL = "3fa0cd96-eef1-5676-8a61-b3b8758bbffb" Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c" ReferenceTests = "324d217c-45ce-50fc-942e-d289b448e8cf" SafeTestsets = "1bc83da4-3b8d-516f-aca4-4fe02f6d838f" @@ -158,4 +160,4 @@ Sundials = "c3572dad-4567-51f8-b174-8c6c989267f4" Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" [targets] -test = ["AmplNLWriter", "BenchmarkTools", "ControlSystemsBase", "DataInterpolations", "DelayDiffEq", "NonlinearSolve", "ForwardDiff", "Ipopt", "Ipopt_jll", "ModelingToolkitStandardLibrary", "Optimization", "OptimizationOptimJL", "OptimizationMOI", "OrdinaryDiffEq", "Random", "ReferenceTests", "SafeTestsets", "StableRNGs", "Statistics", "SteadyStateDiffEq", "Test", "StochasticDiffEq", "Sundials", "StochasticDelayDiffEq", "Pkg", "JET"] +test = ["AmplNLWriter", "BenchmarkTools", "ControlSystemsBase", "DataInterpolations", "DelayDiffEq", "NonlinearSolve", "ForwardDiff", "Ipopt", "Ipopt_jll", "ModelingToolkitStandardLibrary", "Optimization", "OptimizationOptimJL", "OptimizationMOI", "OrdinaryDiffEq", "REPL", "Random", "ReferenceTests", "SafeTestsets", "StableRNGs", "Statistics", "SteadyStateDiffEq", "Test", "StochasticDiffEq", "Sundials", "StochasticDelayDiffEq", "Pkg", "JET"] diff --git a/test/runtests.jl b/test/runtests.jl index 0a9cf3c4db..1c48956458 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1,4 +1,6 @@ using SafeTestsets, Pkg, Test +# https://github.com/JuliaLang/julia/issues/54664 +import REPL const GROUP = get(ENV, "GROUP", "All") From 422230bf30f1a2223c4ace11a9b4fd8a22ab469d Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 9 Oct 2024 14:50:02 +0530 Subject: [PATCH 0088/2176] test: remove ODAEProblem test --- test/structural_transformation/tearing.jl | 16 ---------------- 1 file changed, 16 deletions(-) diff --git a/test/structural_transformation/tearing.jl b/test/structural_transformation/tearing.jl index 60edcb76c5..a0ccc7355d 100644 --- a/test/structural_transformation/tearing.jl +++ b/test/structural_transformation/tearing.jl @@ -169,22 +169,6 @@ infprob = ODEProblem(structural_simplify(sys), [x => 1.0], (0, 1.0), [p => 0.2]) infprob.f(du, infprob.u0, pr, tt) @test any(isnan, du) -sol1 = solve(prob, RosShamp4(), reltol = 2e-4) -sol2 = solve(ODEProblem{false}((u, p, t) -> [-asin(u[1] - pr * t)], - [1.0], - (0, 1.0), - 0.2), - Tsit5(), tstops = sol1.t, adaptive = false) -@test Array(sol1[x])≈Array(sol2[1, :]) atol=1e-5 - -@test sol1[x] == first.(sol1.u) -@test sol1[y] == first.(sol1.u) -@test sin.(sol1[z]) .+ sol1[y]≈pr[1] * sol1.t atol=8e-4 -@test sol1[sin(z) + y]≈sin.(sol1[z]) .+ sol1[y] rtol=1e-12 - -@test sol1[y, :] == sol1[x, :] -@test (@. sin(sol1[z, :]) + sol1[y, :])≈pr * sol1.t atol=8e-4 - # 1426 function Translational_Mass(; name, m = 1.0) sts = @variables s(t) v(t) a(t) From 8ce64bf34899e5beb5aebbf577128f60880bc9a2 Mon Sep 17 00:00:00 2001 From: Christopher Rackauckas Date: Wed, 9 Oct 2024 08:09:20 -0400 Subject: [PATCH 0089/2176] Update Project.toml --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 16b71b9c91..1947ea7ce6 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "ModelingToolkit" uuid = "961ee093-0014-501f-94e3-6117800e7a78" authors = ["Yingbo Ma ", "Chris Rackauckas and contributors"] -version = "9.43.0" +version = "9.44.0" [deps] AbstractTrees = "1520ce14-60c1-5f80-bbc7-55ef81b5835c" From b2dbf919fa7a38bda16b45b0e1b98345e3596c49 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 6 Aug 2024 10:57:25 +0530 Subject: [PATCH 0090/2176] feat: allow parameters to be unknowns in the initialization system --- src/systems/abstractsystem.jl | 9 ++ src/systems/diffeqs/abstractodesystem.jl | 102 +++++++++++++++++++-- src/systems/nonlinear/initializesystem.jl | 106 ++++++++++++++++++++-- src/systems/parameter_buffer.jl | 3 + src/utils.jl | 4 +- 5 files changed, 207 insertions(+), 17 deletions(-) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index aa02a5fb73..661a5fd4b4 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -736,6 +736,15 @@ function has_observed_with_lhs(sys, sym) end end +function has_parameter_dependency_with_lhs(sys, sym) + has_parameter_dependencies(sys) || return false + if has_index_cache(sys) && (ic = get_index_cache(sys)) !== nothing + return any(isequal(sym), ic.dependent_pars) + else + return any(isequal(sym), [eq.lhs for eq in parameter_dependencies(sys)]) + end +end + function _all_ts_idxs!(ts_idxs, ::NotSymbolic, sys, sym) if is_variable(sys, sym) || is_independent_variable(sys, sym) push!(ts_idxs, ContinuousTimeseries()) diff --git a/src/systems/diffeqs/abstractodesystem.jl b/src/systems/diffeqs/abstractodesystem.jl index a7ae411def..623b2b47af 100644 --- a/src/systems/diffeqs/abstractodesystem.jl +++ b/src/systems/diffeqs/abstractodesystem.jl @@ -357,7 +357,9 @@ function DiffEqBase.ODEFunction{iip, specialize}(sys::AbstractODESystem, analytic = nothing, split_idxs = nothing, initializeprob = nothing, + update_initializeprob! = nothing, initializeprobmap = nothing, + initializeprobpmap = nothing, kwargs...) where {iip, specialize} if !iscomplete(sys) error("A completed system is required. Call `complete` or `structural_simplify` on the system before creating an `ODEFunction`") @@ -459,7 +461,9 @@ function DiffEqBase.ODEFunction{iip, specialize}(sys::AbstractODESystem, sparsity = sparsity ? jacobian_sparsity(sys) : nothing, analytic = analytic, initializeprob = initializeprob, - initializeprobmap = initializeprobmap) + update_initializeprob! = update_initializeprob!, + initializeprobmap = initializeprobmap, + initializeprobpmap = initializeprobpmap) end """ @@ -789,6 +793,45 @@ function get_u0( return u0, defs end +struct GetUpdatedMTKParameters{G, S} + # `getu` functor which gets parameters that are unknowns during initialization + getpunknowns::G + # `setu` functor which returns a modified MTKParameters using those parameters + setpunknowns::S +end + +function (f::GetUpdatedMTKParameters)(prob, initializesol) + mtkp = copy(parameter_values(prob)) + f.setpunknowns(mtkp, f.getpunknowns(initializesol)) + mtkp +end + +struct UpdateInitializeprob{G, S} + # `getu` functor which gets all values from prob + getvals::G + # `setu` functor which updates initializeprob with values + setvals::S +end + +function (f::UpdateInitializeprob)(initializeprob, prob) + f.setvals(initializeprob, f.getvals(prob)) +end + +function get_temporary_value(p) + stype = symtype(unwrap(p)) + return if stype == Real + zero(Float64) + elseif stype <: AbstractArray{Real} + zeros(Float64, size(p)) + elseif stype <: Real + zero(stype) + elseif stype <: AbstractArray + zeros(eltype(stype), size(p)) + else + error("Nonnumeric parameter $p with symtype $stype cannot be solved for during initialization") + end +end + function process_DEProblem(constructor, sys::AbstractODESystem, u0map, parammap; implicit_dae = false, du0map = nothing, version = nothing, tgrad = false, @@ -829,18 +872,38 @@ function process_DEProblem(constructor, sys::AbstractODESystem, u0map, parammap; end if eltype(parammap) <: Pair - parammap = Dict(unwrap(k) => v for (k, v) in todict(parammap)) + parammap = Dict{Any, Any}(unwrap(k) => v for (k, v) in parammap) elseif parammap isa AbstractArray if isempty(parammap) parammap = SciMLBase.NullParameters() else - parammap = Dict(unwrap.(parameters(sys)) .=> parammap) + parammap = Dict{Any, Any}(unwrap.(parameters(sys)) .=> parammap) end end - + defs = defaults(sys) + if has_guesses(sys) + guesses = merge( + ModelingToolkit.guesses(sys), isempty(guesses) ? Dict() : todict(guesses)) + solvablepars = [p + for p in parameters(sys) + if is_parameter_solvable(p, parammap, defs, guesses)] + + pvarmap = if parammap === nothing || parammap == SciMLBase.NullParameters() || !(eltype(parammap) <: Pair) && isempty(parammap) + defs + else + merge(defs, todict(parammap)) + end + setparobserved = filter(keys(pvarmap)) do var + has_parameter_dependency_with_lhs(sys, var) + end + else + solvablepars = () + setparobserved = () + end # ModelingToolkit.get_tearing_state(sys) !== nothing => Requires structural_simplify first if sys isa ODESystem && build_initializeprob && - (((implicit_dae || !isempty(missingvars) || !isempty(setobserved)) && + (((implicit_dae || !isempty(missingvars) || !isempty(solvablepars) || + !isempty(setobserved) || !isempty(setparobserved)) && ModelingToolkit.get_tearing_state(sys) !== nothing) || !isempty(initialization_equations(sys))) && t !== nothing if eltype(u0map) <: Number @@ -854,14 +917,32 @@ function process_DEProblem(constructor, sys::AbstractODESystem, u0map, parammap; sys, t, u0map, parammap; guesses, warn_initialize_determined, initialization_eqs, eval_expression, eval_module, fully_determined, check_units) initializeprobmap = getu(initializeprob, unknowns(sys)) + punknowns = [p + for p in all_variable_symbols(initializeprob) if is_parameter(sys, p)] + getpunknowns = getu(initializeprob, punknowns) + setpunknowns = setp(sys, punknowns) + initializeprobpmap = GetUpdatedMTKParameters(getpunknowns, setpunknowns) + reqd_syms = parameter_symbols(initializeprob) + update_initializeprob! = UpdateInitializeprob( + getu(sys, reqd_syms), setu(initializeprob, reqd_syms)) zerovars = Dict(setdiff(unknowns(sys), keys(defaults(sys))) .=> 0.0) + if parammap isa SciMLBase.NullParameters + parammap = Dict() + end + for p in punknowns + p = unwrap(p) + stype = symtype(p) + parammap[p] = get_temporary_value(p) + end trueinit = collect(merge(zerovars, eltype(u0map) <: Pair ? todict(u0map) : u0map)) u0map isa StaticArraysCore.StaticArray && (trueinit = SVector{length(trueinit)}(trueinit)) else initializeprob = nothing + update_initializeprob! = nothing initializeprobmap = nothing + initializeprobpmap = nothing trueinit = u0map end @@ -909,7 +990,9 @@ function process_DEProblem(constructor, sys::AbstractODESystem, u0map, parammap; sparse = sparse, eval_expression = eval_expression, eval_module = eval_module, initializeprob = initializeprob, + update_initializeprob! = update_initializeprob!, initializeprobmap = initializeprobmap, + initializeprobpmap = initializeprobpmap, kwargs...) implicit_dae ? (f, du0, u0, p) : (f, u0, p) end @@ -1471,10 +1554,12 @@ function InitializationProblem{iip, specialize}(sys::AbstractODESystem, isys = get_initializesystem(sys; initialization_eqs, check_units) elseif isempty(u0map) && get_initializesystem(sys) === nothing isys = structural_simplify( - generate_initializesystem(sys; initialization_eqs, check_units); fully_determined) + generate_initializesystem( + sys; initialization_eqs, check_units, pmap = parammap); fully_determined) else isys = structural_simplify( - generate_initializesystem(sys; u0map, initialization_eqs, check_units); fully_determined) + generate_initializesystem( + sys; u0map, initialization_eqs, check_units, pmap = parammap); fully_determined) end uninit = setdiff(unknowns(sys), [unknowns(isys); getfield.(observed(isys), :lhs)]) @@ -1498,6 +1583,7 @@ function InitializationProblem{iip, specialize}(sys::AbstractODESystem, parammap = parammap isa DiffEqBase.NullParameters || isempty(parammap) ? [get_iv(sys) => t] : merge(todict(parammap), Dict(get_iv(sys) => t)) + parammap = Dict(k => v for (k, v) in parammap if v !== missing) if isempty(u0map) u0map = Dict() end @@ -1505,7 +1591,7 @@ function InitializationProblem{iip, specialize}(sys::AbstractODESystem, guesses = Dict() end - u0map = merge(todict(guesses), todict(u0map)) + u0map = merge(ModelingToolkit.guesses(sys), todict(guesses), todict(u0map)) if neqs == nunknown NonlinearProblem(isys, u0map, parammap; kwargs...) else diff --git a/src/systems/nonlinear/initializesystem.jl b/src/systems/nonlinear/initializesystem.jl index 2097e2955e..d4253afda5 100644 --- a/src/systems/nonlinear/initializesystem.jl +++ b/src/systems/nonlinear/initializesystem.jl @@ -5,6 +5,7 @@ Generate `NonlinearSystem` which initializes an ODE problem from specified initi """ function generate_initializesystem(sys::ODESystem; u0map = Dict(), + pmap = Dict(), initialization_eqs = [], guesses = Dict(), default_dd_guess = 0.0, @@ -74,12 +75,103 @@ function generate_initializesystem(sys::ODESystem; end end - pars = [parameters(sys); get_iv(sys)] # include independent variable as pseudo-parameter - eqs_ics = [eqs_ics; observed(sys)] - return NonlinearSystem( - eqs_ics, vars, pars; - defaults = defs, parameter_dependencies = parameter_dependencies(sys), - checks = check_units, - name, kwargs... + # 4) process parameters as initialization unknowns + paramsubs = Dict() + if pmap isa SciMLBase.NullParameters + pmap = Dict() + end + pmap = todict(pmap) + for p in parameters(sys) + if is_parameter_solvable(p, pmap, defs, guesses) + # If either of them are `missing` the parameter is an unknown + # But if the parameter is passed a value, use that as an additional + # equation in the system + _val1 = get(pmap, p, nothing) + _val2 = get(defs, p, nothing) + _val3 = get(guesses, p, nothing) + varp = tovar(p) + paramsubs[p] = varp + # Has a default of `missing`, and (either an equation using the value passed to `ODEProblem` or a guess) + if _val2 === missing + if _val1 !== nothing && _val1 !== missing + push!(eqs_ics, varp ~ _val1) + push!(defs, varp => _val1) + elseif _val3 !== nothing + # assuming an equation exists (either via algebraic equations or initialization_eqs) + push!(defs, varp => _val3) + elseif check_defguess + error("Invalid setup: parameter $(p) has no default value, initial value, or guess") + end + # `missing` passed to `ODEProblem`, and (either an equation using default or a guess) + elseif _val1 === missing + if _val2 !== nothing && _val2 !== missing + push!(eqs_ics, varp ~ _val2) + push!(defs, varp => _val2) + elseif _val3 !== nothing + push!(defs, varp => _val3) + elseif check_defguess + error("Invalid setup: parameter $(p) has no default value, initial value, or guess") + end + # given a symbolic value to ODEProblem + elseif symbolic_type(_val1) != NotSymbolic() + push!(eqs_ics, varp ~ _val1) + push!(defs, varp => _val3) + # No value passed to `ODEProblem`, but a default and a guess are present + # _val2 !== missing is implied by it falling this far in the elseif chain + elseif _val1 === nothing && _val2 !== nothing + push!(eqs_ics, varp ~ _val2) + push!(defs, varp => _val3) + else + # _val1 !== missing and _val1 !== nothing, so a value was provided to ODEProblem + # This would mean `is_parameter_solvable` returned `false`, so we never end up + # here + error("This should never be reached") + end + end + end + + # 5) parameter dependencies become equations, their LHS become unknowns + for eq in parameter_dependencies(sys) + varp = tovar(eq.lhs) + paramsubs[eq.lhs] = varp + push!(eqs_ics, eq) + guessval = get(guesses, eq.lhs, eq.rhs) + push!(defs, varp => guessval) + end + + # 6) handle values provided for dependent parameters similar to values for observed variables + for (k, v) in merge(defaults(sys), pmap) + if is_variable_floatingpoint(k) && has_parameter_dependency_with_lhs(sys, k) + push!(eqs_ics, paramsubs[k] ~ v) + end + end + + # parameters do not include ones that became initialization unknowns + pars = vcat( + [get_iv(sys)], # include independent variable as pseudo-parameter + [p for p in parameters(sys) if !haskey(paramsubs, p)] ) + + eqs_ics = Symbolics.substitute.([eqs_ics; observed(sys)], (paramsubs,)) + vars = [vars; collect(values(paramsubs))] + for k in keys(defs) + defs[k] = substitute(defs[k], paramsubs) + end + return NonlinearSystem(eqs_ics, + vars, + pars; + defaults = defs, + checks = check_units, + name, + kwargs...) +end + +function is_parameter_solvable(p, pmap, defs, guesses) + _val1 = pmap isa AbstractDict ? get(pmap, p, nothing) : nothing + _val2 = get(defs, p, nothing) + _val3 = get(guesses, p, nothing) + # either (missing is a default or was passed to the ODEProblem) or (nothing was passed to + # the ODEProblem and it has a default and a guess) + return ((_val1 === missing || _val2 === missing) || + (_val1 === nothing && _val2 !== nothing)) && _val3 !== nothing end diff --git a/src/systems/parameter_buffer.jl b/src/systems/parameter_buffer.jl index 20ba3d731a..67b26ea50c 100644 --- a/src/systems/parameter_buffer.jl +++ b/src/systems/parameter_buffer.jl @@ -522,6 +522,9 @@ function _remake_buffer(indp, oldbuf::MTKParameters, idxs, vals; validate = true ic = get_index_cache(indp_to_system(indp)) for (idx, val) in zip(idxs, vals) sym = nothing + if val === missing + val = get_temporary_value(idx) + end if symbolic_type(idx) == ScalarSymbolic() sym = idx idx = parameter_index(ic, sym) diff --git a/src/utils.jl b/src/utils.jl index 25731f495a..a3f99731af 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -494,8 +494,8 @@ function collect_var!(unknowns, parameters, var, iv) push!(unknowns, var) end # Add also any parameters that appear only as defaults in the var - if hasdefault(var) - collect_vars!(unknowns, parameters, getdefault(var), iv) + if hasdefault(var) && (def = getdefault(var)) !== missing + collect_vars!(unknowns, parameters, def, iv) end return nothing end From 8ec6d3e467dede8db4c5c97fefb994b38c6a3af0 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 21 Aug 2024 12:36:44 +0530 Subject: [PATCH 0091/2176] feat: collect guesses from parameter metadata in ODESystem --- src/systems/diffeqs/odesystem.jl | 15 ++++++++++----- src/variables.jl | 10 ++++++++++ 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/src/systems/diffeqs/odesystem.jl b/src/systems/diffeqs/odesystem.jl index 784b071ef1..48c00a933b 100644 --- a/src/systems/diffeqs/odesystem.jl +++ b/src/systems/diffeqs/odesystem.jl @@ -249,11 +249,16 @@ function ODESystem(deqs::AbstractVector{<:Equation}, iv, dvs, ps; process_variables!(var_to_name, defaults, dvs′) process_variables!(var_to_name, defaults, ps′) - sysguesses = [ModelingToolkit.getguess(st) for st in dvs′] - hasaguess = findall(!isnothing, sysguesses) - var_guesses = dvs′[hasaguess] .=> sysguesses[hasaguess] - sysguesses = isempty(var_guesses) ? Dict() : todict(var_guesses) - guesses = merge(sysguesses, todict(guesses)) + sysdvsguesses = [ModelingToolkit.getguess(st) for st in dvs′] + hasaguess = findall(!isnothing, sysdvsguesses) + var_guesses = dvs′[hasaguess] .=> sysdvsguesses[hasaguess] + sysdvsguesses = isempty(var_guesses) ? Dict() : todict(var_guesses) + syspsguesses = [ModelingToolkit.getguess(st) for st in ps′] + hasaguess = findall(!isnothing, syspsguesses) + ps_guesses = ps′[hasaguess] .=> syspsguesses[hasaguess] + syspsguesses = isempty(ps_guesses) ? Dict() : todict(ps_guesses) + + guesses = merge(sysdvsguesses, syspsguesses, todict(guesses)) guesses = Dict{Any, Any}(value(k) => value(v) for (k, v) in pairs(guesses)) isempty(observed) || collect_var_to_name!(var_to_name, (eq.lhs for eq in observed)) diff --git a/src/variables.jl b/src/variables.jl index 4e5f860513..1c22540c63 100644 --- a/src/variables.jl +++ b/src/variables.jl @@ -491,6 +491,16 @@ function getguess(x) Symbolics.getmetadata(x, VariableGuess, nothing) end +""" + setguess(x, v) + +Set the guess for the initial value associated with symbolic variable `x` to `v`. +See also [`hasguess`](@ref). +""" +function setguess(x, v) + Symbolics.setmetadata(x, VariableGuess, v) +end + """ hasguess(x) From 6d63ffdfef27fef051390106f3bdabf09a002bb2 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 6 Aug 2024 16:19:26 +0530 Subject: [PATCH 0092/2176] test: test parameters as initialization unknowns --- test/initializationsystem.jl | 130 +++++++++++++++++++++++++++++++++ test/parameter_dependencies.jl | 4 +- 2 files changed, 132 insertions(+), 2 deletions(-) diff --git a/test/initializationsystem.jl b/test/initializationsystem.jl index e0caf22ba4..e6192144c8 100644 --- a/test/initializationsystem.jl +++ b/test/initializationsystem.jl @@ -1,4 +1,5 @@ using ModelingToolkit, OrdinaryDiffEq, NonlinearSolve, Test +using SymbolicIndexingInterface using ModelingToolkit: t_nounits as t, D_nounits as D @parameters g @@ -576,3 +577,132 @@ sol = solve(oprob_2nd_order_2, Rosenbrock23()) # retcode: Success sol = solve(prob, Tsit5(), reltol = 1e-4) @test all(sol(1.0, idxs = sys.x) .≈ +exp(1)) && all(sol(1.0, idxs = sys.y) .≈ -exp(1)) end + +@testset "Initialization of parameters" begin + function test_parameter(prob, sym, val, initialval = zero(val)) + @test prob.ps[sym] ≈ initialval + @test init(prob, Tsit5()).ps[sym] ≈ val + @test solve(prob, Tsit5()).ps[sym] ≈ val + end + function test_initializesystem(sys, u0map, pmap, p, equation) + isys = ModelingToolkit.generate_initializesystem( + sys; u0map, pmap, guesses = ModelingToolkit.guesses(sys)) + @test is_variable(isys, p) + @test equation in equations(isys) || (0 ~ -equation.rhs) in equations(isys) + end + @variables x(t) y(t) + @parameters p q + u0map = Dict(x => 1.0, y => 1.0) + pmap = Dict() + pmap[q] = 1.0 + # `missing` default, equation from ODEProblem + @mtkbuild sys = ODESystem( + [D(x) ~ x * q, D(y) ~ y * p], t; defaults = [p => missing], guesses = [p => 1.0]) + pmap[p] = 2q + prob = ODEProblem(sys, u0map, (0.0, 1.0), pmap) + test_parameter(prob, p, 2.0) + # `missing` default, provided guess + @mtkbuild sys = ODESystem( + [D(x) ~ x, p ~ x + y], t; defaults = [p => missing], guesses = [p => 0.0]) + prob = ODEProblem(sys, u0map, (0.0, 1.0)) + test_parameter(prob, p, 2.0) + test_initializesystem(sys, u0map, pmap, p, 0 ~ p - x - y) + + # `missing` to ODEProblem, equation from default + @mtkbuild sys = ODESystem( + [D(x) ~ x * q, D(y) ~ y * p], t; defaults = [p => 2q], guesses = [p => 1.0]) + pmap[p] = missing + prob = ODEProblem(sys, u0map, (0.0, 1.0), pmap) + test_parameter(prob, p, 2.0) + test_initializesystem(sys, u0map, pmap, p, 0 ~ 2q - p) + test_parameter(prob2, p, 2.0) + # `missing` to ODEProblem, provided guess + @mtkbuild sys = ODESystem( + [D(x) ~ x, p ~ x + y], t; guesses = [p => 0.0]) + prob = ODEProblem(sys, u0map, (0.0, 1.0), pmap) + test_parameter(prob, p, 2.0) + test_initializesystem(sys, u0map, pmap, p, 0 ~ x + y - p) + + # No `missing`, default and guess + @mtkbuild sys = ODESystem( + [D(x) ~ x * q, D(y) ~ y * p], t; defaults = [p => 2q], guesses = [p => 0.0]) + delete!(pmap, p) + prob = ODEProblem(sys, u0map, (0.0, 1.0), pmap) + test_parameter(prob, p, 2.0) + test_initializesystem(sys, u0map, pmap, p, 0 ~ 2q - p) + + # Should not be solved for: + + # ODEProblem value with guess, no `missing` + @mtkbuild sys = ODESystem([D(x) ~ x * q, D(y) ~ y * p], t; guesses = [p => 0.0]) + _pmap = merge(pmap, Dict(p => 3q)) + prob = ODEProblem(sys, u0map, (0.0, 1.0), _pmap) + @test prob.ps[p] ≈ 3.0 + @test prob.f.initializeprob === nothing + # Default overridden by ODEProblem, guess provided + @mtkbuild sys = ODESystem( + [D(x) ~ q * x, D(y) ~ y * p], t; defaults = [p => 2q], guesses = [p => 1.0]) + prob = ODEProblem(sys, u0map, (0.0, 1.0), _pmap) + @test prob.ps[p] ≈ 3.0 + @test prob.f.initializeprob === nothing + + @mtkbuild sys = ODESystem([D(x) ~ x, p ~ x + y], t; guesses = [p => 0.0]) + @test_throws ModelingToolkit.MissingParametersError ODEProblem( + sys, [x => 1.0, y => 1.0], (0.0, 1.0)) + + @testset "Null system" begin + @variables x(t) y(t) s(t) + @parameters x0 y0 + @mtkbuild sys = ODESystem([x ~ x0, y ~ y0, s ~ x + y], t; guesses = [y0 => 0.0]) + prob = ODEProblem(sys, [s => 1.0], (0.0, 1.0), [x0 => 0.3, y0 => missing]) + test_parameter(prob, y0, 0.7) + end + + using ModelingToolkitStandardLibrary.Mechanical.TranslationalModelica: Fixed, Mass, + Spring, Force, + Damper + using ModelingToolkitStandardLibrary.Mechanical: TranslationalModelica as TM + using ModelingToolkitStandardLibrary.Blocks: Constant + + @named mass = TM.Mass(; m = 1.0, s = 1.0, v = 0.0, a = 0.0) + @named fixed = Fixed(; s0 = 0.0) + @named spring = Spring(; c = 2.0, s_rel0 = nothing) + @named gravity = Force() + @named constant = Constant(; k = 9.81) + @named damper = TM.Damper(; d = 0.1) + @mtkbuild sys = ODESystem( + [connect(fixed.flange, spring.flange_a), connect(spring.flange_b, mass.flange_a), + connect(mass.flange_a, gravity.flange), connect(constant.output, gravity.f), + connect(fixed.flange, damper.flange_a), connect(damper.flange_b, mass.flange_a)], + t; + systems = [fixed, spring, mass, gravity, constant, damper], + guesses = [spring.s_rel0 => 1.0]) + prob = ODEProblem(sys, [], (0.0, 1.0), [spring.s_rel0 => missing]) + test_parameter(prob, spring.s_rel0, -3.905) +end + +@testset "Update initializeprob parameters" begin + @variables x(t) y(t) + @parameters p q + @mtkbuild sys = ODESystem( + [D(x) ~ x, p ~ x + y], t; guesses = [x => 0.0, p => 0.0]) + prob = ODEProblem(sys, [y => 1.0], (0.0, 1.0), [p => 3.0]) + @test prob.f.initializeprob.ps[p] ≈ 3.0 + @test init(prob, Tsit5())[x] ≈ 2.0 + prob.ps[p] = 2.0 + @test prob.f.initializeprob.ps[p] ≈ 3.0 + @test init(prob, Tsit5())[x] ≈ 1.0 + ModelingToolkit.defaults(prob.f.sys)[p] = missing +end + +@testset "Equations for dependent parameters" begin + @variables x(t) + @parameters p q=5 r + @mtkbuild sys = ODESystem( + D(x) ~ 2x + r, t; parameter_dependencies = [r ~ p + 2q, q ~ p + 3], + guesses = [p => 1.0]) + prob = ODEProblem(sys, [x => 1.0], (0.0, 1.0), [p => missing]) + @test length(equations(ModelingToolkit.get_parent(prob.f.initializeprob.f.sys))) == 4 + integ = init(prob, Tsit5()) + @test integ.ps[p] ≈ 2 +end diff --git a/test/parameter_dependencies.jl b/test/parameter_dependencies.jl index 9cfd4ca5c5..1f1aab9953 100644 --- a/test/parameter_dependencies.jl +++ b/test/parameter_dependencies.jl @@ -10,7 +10,7 @@ using SymbolicIndexingInterface using NonlinearSolve @testset "ODESystem with callbacks" begin - @parameters p1=1.0 p2=1.0 + @parameters p1=1.0 p2 @variables x(t) cb1 = [x ~ 2.0] => [p1 ~ 2.0] # triggers at t=-2+√6 function affect1!(integ, u, p, ctx) @@ -31,7 +31,7 @@ using NonlinearSolve prob = ODEProblem(sys, [x => 1.0], (0.0, 1.5), jac = true) @test prob.ps[p1] == 1.0 @test prob.ps[p2] == 2.0 - @test_nowarn solve(prob, Tsit5()) + @test SciMLBase.successful_retcode(solve(prob, Tsit5())) prob = ODEProblem(sys, [x => 1.0], (0.0, 1.5), [p1 => 1.0], jac = true) @test prob.ps[p1] == 1.0 @test prob.ps[p2] == 2.0 From 4682f8d2d968d2ed7c44a1fc87d3a864783ce5a7 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 6 Aug 2024 17:53:15 +0530 Subject: [PATCH 0093/2176] fix: check symbolic_type in `namespace_expr` --- src/systems/abstractsystem.jl | 1 + 1 file changed, 1 insertion(+) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index 661a5fd4b4..bad83133a1 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -1356,6 +1356,7 @@ end function namespace_expr( O, sys, n = nameof(sys); ivs = independent_variables(sys)) O = unwrap(O) + symbolic_type(O) == NotSymbolic() && return O if any(isequal(O), ivs) return O elseif iscall(O) From 8425bb83060d9d57dfe61fc5860438c7d233b6bc Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 6 Aug 2024 17:53:31 +0530 Subject: [PATCH 0094/2176] refactor: allow parameter default to be `missing` in `@mtkmodel` --- src/systems/model_parsing.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/systems/model_parsing.jl b/src/systems/model_parsing.jl index fce2add6b7..70fab5a7cc 100644 --- a/src/systems/model_parsing.jl +++ b/src/systems/model_parsing.jl @@ -154,9 +154,9 @@ function update_kwargs_and_metadata!(dict, kwargs, a, def, indices, type, var, if !isnothing(meta) && haskey(meta, VariableUnit) uvar = gensym() push!(where_types, uvar) - push!(kwargs, Expr(:kw, :($a::Union{Nothing, $uvar}), nothing)) + push!(kwargs, Expr(:kw, :($a::Union{Nothing, Missing, $uvar}), nothing)) else - push!(kwargs, Expr(:kw, :($a::Union{Nothing, $type}), nothing)) + push!(kwargs, Expr(:kw, :($a::Union{Nothing, Missing, $type}), nothing)) end dict[:kwargs][getname(var)] = Dict(:value => def, :type => type) else From 5a538c3a7807f1744abeebcd60d98c1b6cd354e3 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 5 Jul 2024 19:56:35 +0530 Subject: [PATCH 0095/2176] feat: support `SciMLBase.remake_initializeprob` --- src/systems/nonlinear/initializesystem.jl | 53 +++++++++++++++++++++++ test/initializationsystem.jl | 5 +++ 2 files changed, 58 insertions(+) diff --git a/src/systems/nonlinear/initializesystem.jl b/src/systems/nonlinear/initializesystem.jl index d4253afda5..a4a29f223b 100644 --- a/src/systems/nonlinear/initializesystem.jl +++ b/src/systems/nonlinear/initializesystem.jl @@ -175,3 +175,56 @@ function is_parameter_solvable(p, pmap, defs, guesses) return ((_val1 === missing || _val2 === missing) || (_val1 === nothing && _val2 !== nothing)) && _val3 !== nothing end + +function SciMLBase.remake_initializeprob(sys::ODESystem, odefn, u0, t0, p) + if (u0 === missing || !(eltype(u0) <: Pair) || isempty(u0)) && + (p === missing || !(eltype(p) <: Pair) || isempty(p)) + return odefn.initializeprob, odefn.update_initializeprob!, odefn.initializeprobmap, + odefn.initializeprobpmap + end + if u0 === missing || isempty(u0) + u0 = Dict() + elseif !(eltype(u0) <: Pair) + u0 = Dict(unknowns(sys) .=> u0) + end + if p === missing + p = Dict() + end + if t0 === nothing + t0 = 0.0 + end + u0 = todict(u0) + defs = defaults(sys) + varmap = merge(defs, u0) + varmap = canonicalize_varmap(varmap) + missingvars = setdiff(unknowns(sys), collect(keys(varmap))) + setobserved = filter(keys(varmap)) do var + has_observed_with_lhs(sys, var) || has_observed_with_lhs(sys, default_toterm(var)) + end + p = todict(p) + guesses = ModelingToolkit.guesses(sys) + solvablepars = [par + for par in parameters(sys) + if is_parameter_solvable(par, p, defs, guesses)] + pvarmap = merge(defs, p) + setparobserved = filter(keys(pvarmap)) do var + has_parameter_dependency_with_lhs(sys, var) + end + if (((!isempty(missingvars) || !isempty(solvablepars) || + !isempty(setobserved) || !isempty(setparobserved)) && + ModelingToolkit.get_tearing_state(sys) !== nothing) || + !isempty(initialization_equations(sys))) + initprob = InitializationProblem(sys, t0, u0, p) + initprobmap = getu(initprob, unknowns(sys)) + punknowns = [p for p in all_variable_symbols(initprob) if is_parameter(sys, p)] + getpunknowns = getu(initprob, punknowns) + setpunknowns = setp(sys, punknowns) + initprobpmap = GetUpdatedMTKParameters(getpunknowns, setpunknowns) + reqd_syms = parameter_symbols(initprob) + update_initializeprob! = UpdateInitializeprob( + getu(sys, reqd_syms), setu(initprob, reqd_syms)) + return initprob, update_initializeprob!, initprobmap, initprobpmap + else + return nothing, nothing, nothing, nothing + end +end diff --git a/test/initializationsystem.jl b/test/initializationsystem.jl index e6192144c8..353eaa24e2 100644 --- a/test/initializationsystem.jl +++ b/test/initializationsystem.jl @@ -693,6 +693,11 @@ end @test prob.f.initializeprob.ps[p] ≈ 3.0 @test init(prob, Tsit5())[x] ≈ 1.0 ModelingToolkit.defaults(prob.f.sys)[p] = missing + prob2 = remake(prob; u0 = [y => 1.0], p = [p => 3x]) + @test !is_variable(prob2.f.initializeprob, p) && + !is_parameter(prob2.f.initializeprob, p) + @test init(prob2, Tsit5())[x] ≈ 0.5 + @test_nowarn solve(prob2, Tsit5()) end @testset "Equations for dependent parameters" begin From f783cb105f9e88117bf637151551f004e42a6c5a Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 21 Aug 2024 14:19:08 +0530 Subject: [PATCH 0096/2176] test: test remaking of initialization problem --- test/initializationsystem.jl | 35 +++++++++++++++++++++++++++++++++++ test/mtkparameters.jl | 2 +- test/split_parameters.jl | 5 +++-- 3 files changed, 39 insertions(+), 3 deletions(-) diff --git a/test/initializationsystem.jl b/test/initializationsystem.jl index 353eaa24e2..d9757ffc5b 100644 --- a/test/initializationsystem.jl +++ b/test/initializationsystem.jl @@ -601,12 +601,18 @@ end pmap[p] = 2q prob = ODEProblem(sys, u0map, (0.0, 1.0), pmap) test_parameter(prob, p, 2.0) + prob2 = remake(prob; u0 = u0map, p = pmap) + prob2.ps[p] = 0.0 + test_parameter(prob2, p, 2.0) # `missing` default, provided guess @mtkbuild sys = ODESystem( [D(x) ~ x, p ~ x + y], t; defaults = [p => missing], guesses = [p => 0.0]) prob = ODEProblem(sys, u0map, (0.0, 1.0)) test_parameter(prob, p, 2.0) test_initializesystem(sys, u0map, pmap, p, 0 ~ p - x - y) + prob2 = remake(prob; u0 = u0map) + prob2.ps[p] = 0.0 + test_parameter(prob2, p, 2.0) # `missing` to ODEProblem, equation from default @mtkbuild sys = ODESystem( @@ -615,6 +621,8 @@ end prob = ODEProblem(sys, u0map, (0.0, 1.0), pmap) test_parameter(prob, p, 2.0) test_initializesystem(sys, u0map, pmap, p, 0 ~ 2q - p) + prob2 = remake(prob; u0 = u0map, p = pmap) + prob2.ps[p] = 0.0 test_parameter(prob2, p, 2.0) # `missing` to ODEProblem, provided guess @mtkbuild sys = ODESystem( @@ -622,6 +630,9 @@ end prob = ODEProblem(sys, u0map, (0.0, 1.0), pmap) test_parameter(prob, p, 2.0) test_initializesystem(sys, u0map, pmap, p, 0 ~ x + y - p) + prob2 = remake(prob; u0 = u0map, p = pmap) + prob2.ps[p] = 0.0 + test_parameter(prob2, p, 2.0) # No `missing`, default and guess @mtkbuild sys = ODESystem( @@ -630,6 +641,9 @@ end prob = ODEProblem(sys, u0map, (0.0, 1.0), pmap) test_parameter(prob, p, 2.0) test_initializesystem(sys, u0map, pmap, p, 0 ~ 2q - p) + prob2 = remake(prob; u0 = u0map, p = pmap) + prob2.ps[p] = 0.0 + test_parameter(prob2, p, 2.0) # Should not be solved for: @@ -711,3 +725,24 @@ end integ = init(prob, Tsit5()) @test integ.ps[p] ≈ 2 end + +@testset "Re-creating initialization problem on remake" begin + @variables x(t) y(t) + @parameters p q + @mtkbuild sys = ODESystem( + [D(x) ~ x, p ~ x + y], t; defaults = [p => missing], guesses = [x => 0.0, p => 0.0]) + prob = ODEProblem(sys, [x => 1.0, y => 1.0], (0.0, 1.0)) + @test init(prob, Tsit5()).ps[p] ≈ 2.0 + # nonsensical value for y just to test that equations work + prob2 = remake(prob; u0 = [x => 1.0, y => 2x + exp(t)]) + @test init(prob2, Tsit5()).ps[p] ≈ 4.0 + # solve for `x` given `p` and `y` + prob3 = remake(prob; u0 = [x => nothing, y => 1.0], p = [p => 2x + exp(t)]) + @test init(prob3, Tsit5())[x] ≈ 0.0 + @test_logs (:warn, r"overdetermined") remake( + prob; u0 = [x => 1.0, y => 2.0], p = [p => 4.0]) + prob4 = remake(prob; u0 = [x => 1.0, y => 2.0], p = [p => 4.0]) + @test solve(prob4, Tsit5()).retcode == ReturnCode.InitialFailure + prob5 = remake(prob) + @test init(prob, Tsit5()).ps[p] ≈ 2.0 +end diff --git a/test/mtkparameters.jl b/test/mtkparameters.jl index e0aee8c289..b4fc1a9677 100644 --- a/test/mtkparameters.jl +++ b/test/mtkparameters.jl @@ -293,7 +293,7 @@ end ps_vec = [k => [2.0, 3.0, 4.0, 5.0]] ps_scal = [k[1] => 1.0, k[2] => 2.0, k[3] => 3.0, k[4] => 4.0] oprob_scal_scal = ODEProblem(osys_scal, u0, 1.0, ps_scal) - newoprob = remake(oprob_scal_scal; p = ps_vec) + newoprob = remake(oprob_scal_scal; p = ps_vec, build_initializeprob = false) @test newoprob.ps[k] == [2.0, 3.0, 4.0, 5.0] end diff --git a/test/split_parameters.jl b/test/split_parameters.jl index 9f124ef45d..b8651238ea 100644 --- a/test/split_parameters.jl +++ b/test/split_parameters.jl @@ -114,7 +114,7 @@ eqs = [D(y) ~ dy * a sys = structural_simplify(model; split = false) tspan = (0.0, t_end) -prob = ODEProblem(sys, [], tspan, []) +prob = ODEProblem(sys, [], tspan, []; build_initializeprob = false) @test prob.p isa Vector{Float64} sol = solve(prob, ImplicitEuler()); @@ -122,7 +122,8 @@ sol = solve(prob, ImplicitEuler()); # ------------------------ Mixed Type Conserved -prob = ODEProblem(sys, [], tspan, []; tofloat = false, use_union = true) +prob = ODEProblem( + sys, [], tspan, []; tofloat = false, use_union = true, build_initializeprob = false) @test prob.p isa Tuple{Vector{Float64}, Vector{Int64}} sol = solve(prob, ImplicitEuler()); From 26c3f3acc61516be334109f4e882dc0431f97837 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 4 Sep 2024 19:27:52 +0530 Subject: [PATCH 0097/2176] fix: handle edge cases in namespacing --- src/systems/abstractsystem.jl | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index bad83133a1..9908d6f1e3 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -1353,10 +1353,21 @@ function namespace_assignment(eq::Assignment, sys) Assignment(_lhs, _rhs) end +function is_array_of_symbolics(x) + symbolic_type(x) == ArraySymbolic() && return true + symbolic_type(x) == ScalarSymbolic() && return false + x isa AbstractArray && + any(y -> symbolic_type(y) != NotSymbolic() || is_array_of_symbolics(y), x) +end + function namespace_expr( O, sys, n = nameof(sys); ivs = independent_variables(sys)) O = unwrap(O) - symbolic_type(O) == NotSymbolic() && return O + # Exceptions for arrays of symbolic and Ref of a symbolic, the latter + # of which shows up in broadcasts + if symbolic_type(O) == NotSymbolic() && !(O isa AbstractArray) && !(O isa Ref) + return O + end if any(isequal(O), ivs) return O elseif iscall(O) @@ -1378,7 +1389,7 @@ function namespace_expr( end elseif isvariable(O) renamespace(n, O) - elseif O isa Array + elseif O isa AbstractArray && is_array_of_symbolics(O) let sys = sys, n = n map(o -> namespace_expr(o, sys, n; ivs), O) end From de94dc1a724225c53fba2073f7874d6b9a484f1d Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 6 Sep 2024 17:01:45 +0530 Subject: [PATCH 0098/2176] fix: allow passing `nothing` to override default for `@mtkmodel` models --- src/systems/model_parsing.jl | 18 ++++++++++++------ test/model_parsing.jl | 2 +- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/src/systems/model_parsing.jl b/src/systems/model_parsing.jl index 70fab5a7cc..7b6705bbd5 100644 --- a/src/systems/model_parsing.jl +++ b/src/systems/model_parsing.jl @@ -148,15 +148,20 @@ end pop_structure_dict!(dict, key) = length(dict[key]) == 0 && pop!(dict, key) +struct NoValue end +const NO_VALUE = NoValue() + function update_kwargs_and_metadata!(dict, kwargs, a, def, indices, type, var, varclass, where_types, meta) if indices isa Nothing if !isnothing(meta) && haskey(meta, VariableUnit) uvar = gensym() push!(where_types, uvar) - push!(kwargs, Expr(:kw, :($a::Union{Nothing, Missing, $uvar}), nothing)) + push!(kwargs, + Expr(:kw, :($a::Union{Nothing, Missing, $NoValue, $uvar}), NO_VALUE)) else - push!(kwargs, Expr(:kw, :($a::Union{Nothing, Missing, $type}), nothing)) + push!(kwargs, + Expr(:kw, :($a::Union{Nothing, Missing, $NoValue, $type}), NO_VALUE)) end dict[:kwargs][getname(var)] = Dict(:value => def, :type => type) else @@ -164,8 +169,9 @@ function update_kwargs_and_metadata!(dict, kwargs, a, def, indices, type, var, push!(kwargs, Expr(:kw, Expr(:(::), a, - Expr(:curly, :Union, :Nothing, Expr(:curly, :AbstractArray, vartype))), - nothing)) + Expr(:curly, :Union, :Nothing, :Missing, NoValue, + Expr(:curly, :AbstractArray, vartype))), + NO_VALUE)) if !isnothing(meta) && haskey(meta, VariableUnit) push!(where_types, vartype) else @@ -679,7 +685,7 @@ function parse_variable_arg(dict, mod, arg, varclass, kwargs, where_types) varexpr = if haskey(metadata_with_exprs, VariableUnit) unit = metadata_with_exprs[VariableUnit] quote - $name = if $name === nothing + $name = if $name === $NO_VALUE $setdefault($vv, $def) else try @@ -699,7 +705,7 @@ function parse_variable_arg(dict, mod, arg, varclass, kwargs, where_types) end else quote - $name = if $name === nothing + $name = if $name === $NO_VALUE $setdefault($vv, $def) else $setdefault($vv, $name) diff --git a/test/model_parsing.jl b/test/model_parsing.jl index 6332dc12a5..f80dc77dc4 100644 --- a/test/model_parsing.jl +++ b/test/model_parsing.jl @@ -840,7 +840,7 @@ end n[1:3] = if flag [2, 2, 2] else - 1 + [1, 1, 1] end end end From 5874f97ed519a8bb18c83d9ec9b8bb8f794e4f74 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 6 Sep 2024 17:02:02 +0530 Subject: [PATCH 0099/2176] fix: fix `nothing` default overrides being ignored --- src/systems/diffeqs/odesystem.jl | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/systems/diffeqs/odesystem.jl b/src/systems/diffeqs/odesystem.jl index 48c00a933b..539fd1a975 100644 --- a/src/systems/diffeqs/odesystem.jl +++ b/src/systems/diffeqs/odesystem.jl @@ -242,12 +242,12 @@ function ODESystem(deqs::AbstractVector{<:Equation}, iv, dvs, ps; "`default_u0` and `default_p` are deprecated. Use `defaults` instead.", :ODESystem, force = true) end - defaults = todict(defaults) - defaults = Dict{Any, Any}(value(k) => value(v) - for (k, v) in pairs(defaults) if value(v) !== nothing) + defaults = Dict{Any, Any}(todict(defaults)) var_to_name = Dict() process_variables!(var_to_name, defaults, dvs′) process_variables!(var_to_name, defaults, ps′) + defaults = Dict{Any, Any}(value(k) => value(v) + for (k, v) in pairs(defaults) if v !== nothing) sysdvsguesses = [ModelingToolkit.getguess(st) for st in dvs′] hasaguess = findall(!isnothing, sysdvsguesses) From d7755bfb9464469cd983f437fc9ab045c4398b08 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 26 Sep 2024 19:24:33 +0530 Subject: [PATCH 0100/2176] feat: track parameter dependency defaults and guesses --- src/systems/diffeqs/odesystem.jl | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/src/systems/diffeqs/odesystem.jl b/src/systems/diffeqs/odesystem.jl index 539fd1a975..bda5690a67 100644 --- a/src/systems/diffeqs/odesystem.jl +++ b/src/systems/diffeqs/odesystem.jl @@ -237,6 +237,8 @@ function ODESystem(deqs::AbstractVector{<:Equation}, iv, dvs, ps; ctrl′ = value.(controls) dvs′ = value.(dvs) dvs′ = filter(x -> !isdelay(x, iv), dvs′) + parameter_dependencies, ps′ = process_parameter_dependencies( + parameter_dependencies, ps′) if !(isempty(default_u0) && isempty(default_p)) Base.depwarn( "`default_u0` and `default_p` are deprecated. Use `defaults` instead.", @@ -246,6 +248,8 @@ function ODESystem(deqs::AbstractVector{<:Equation}, iv, dvs, ps; var_to_name = Dict() process_variables!(var_to_name, defaults, dvs′) process_variables!(var_to_name, defaults, ps′) + process_variables!(var_to_name, defaults, [eq.lhs for eq in parameter_dependencies]) + process_variables!(var_to_name, defaults, [eq.rhs for eq in parameter_dependencies]) defaults = Dict{Any, Any}(value(k) => value(v) for (k, v) in pairs(defaults) if v !== nothing) @@ -257,9 +261,15 @@ function ODESystem(deqs::AbstractVector{<:Equation}, iv, dvs, ps; hasaguess = findall(!isnothing, syspsguesses) ps_guesses = ps′[hasaguess] .=> syspsguesses[hasaguess] syspsguesses = isempty(ps_guesses) ? Dict() : todict(ps_guesses) + syspdepguesses = [ModelingToolkit.getguess(eq.lhs) for eq in parameter_dependencies] + hasaguess = findall(!isnothing, syspdepguesses) + pdep_guesses = [eq.lhs for eq in parameter_dependencies][hasaguess] .=> + syspdepguesses[hasaguess] + syspdepguesses = isempty(pdep_guesses) ? Dict() : todict(pdep_guesses) - guesses = merge(sysdvsguesses, syspsguesses, todict(guesses)) - guesses = Dict{Any, Any}(value(k) => value(v) for (k, v) in pairs(guesses)) + guesses = merge(sysdvsguesses, syspsguesses, syspdepguesses, todict(guesses)) + guesses = Dict{Any, Any}(value(k) => value(v) + for (k, v) in pairs(guesses) if v !== nothing) isempty(observed) || collect_var_to_name!(var_to_name, (eq.lhs for eq in observed)) @@ -274,8 +284,7 @@ function ODESystem(deqs::AbstractVector{<:Equation}, iv, dvs, ps; end cont_callbacks = SymbolicContinuousCallbacks(continuous_events) disc_callbacks = SymbolicDiscreteCallbacks(discrete_events) - parameter_dependencies, ps′ = process_parameter_dependencies( - parameter_dependencies, ps′) + if is_dde === nothing is_dde = _check_if_dde(deqs, iv′, systems) end From 96faa983161c677c33e351361238163b65a75dd8 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 1 Oct 2024 15:08:21 +0530 Subject: [PATCH 0101/2176] fix: handle type promotion in `InitializationProblem` with observed --- src/systems/diffeqs/abstractodesystem.jl | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/systems/diffeqs/abstractodesystem.jl b/src/systems/diffeqs/abstractodesystem.jl index 623b2b47af..8ebe6d93d0 100644 --- a/src/systems/diffeqs/abstractodesystem.jl +++ b/src/systems/diffeqs/abstractodesystem.jl @@ -888,7 +888,8 @@ function process_DEProblem(constructor, sys::AbstractODESystem, u0map, parammap; for p in parameters(sys) if is_parameter_solvable(p, parammap, defs, guesses)] - pvarmap = if parammap === nothing || parammap == SciMLBase.NullParameters() || !(eltype(parammap) <: Pair) && isempty(parammap) + pvarmap = if parammap === nothing || parammap == SciMLBase.NullParameters() || + !(eltype(parammap) <: Pair) && isempty(parammap) defs else merge(defs, todict(parammap)) @@ -1592,6 +1593,22 @@ function InitializationProblem{iip, specialize}(sys::AbstractODESystem, end u0map = merge(ModelingToolkit.guesses(sys), todict(guesses), todict(u0map)) + fullmap = merge(u0map, parammap) + u0T = Union{} + for sym in unknowns(isys) + haskey(fullmap, sym) || continue + symbolic_type(fullmap[sym]) == NotSymbolic() || continue + u0T = promote_type(u0T, typeof(fullmap[sym])) + end + for eq in observed(isys) + haskey(fullmap, eq.lhs) || continue + symbolic_type(fullmap[eq.lhs]) == NotSymbolic() || continue + u0T = promote_type(u0T, typeof(fullmap[eq.lhs])) + end + if u0T != Union{} + u0map = Dict(k => symbolic_type(v) == NotSymbolic() ? u0T(v) : v + for (k, v) in u0map) + end if neqs == nunknown NonlinearProblem(isys, u0map, parammap; kwargs...) else From 8836abec485bd0d2d7504c9960b37bfa73fee3d5 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 1 Oct 2024 15:08:44 +0530 Subject: [PATCH 0102/2176] fix: improve type promotion in `remake_initializeprob` --- src/systems/nonlinear/initializesystem.jl | 45 +++++++++++++++++++++-- test/initializationsystem.jl | 36 +++++++++++++++++- 2 files changed, 77 insertions(+), 4 deletions(-) diff --git a/src/systems/nonlinear/initializesystem.jl b/src/systems/nonlinear/initializesystem.jl index a4a29f223b..5b465a677c 100644 --- a/src/systems/nonlinear/initializesystem.jl +++ b/src/systems/nonlinear/initializesystem.jl @@ -173,15 +173,54 @@ function is_parameter_solvable(p, pmap, defs, guesses) # either (missing is a default or was passed to the ODEProblem) or (nothing was passed to # the ODEProblem and it has a default and a guess) return ((_val1 === missing || _val2 === missing) || - (_val1 === nothing && _val2 !== nothing)) && _val3 !== nothing + (_val1 === nothing && _val2 !== nothing)) && _val3 !== nothing end function SciMLBase.remake_initializeprob(sys::ODESystem, odefn, u0, t0, p) - if (u0 === missing || !(eltype(u0) <: Pair) || isempty(u0)) && - (p === missing || !(eltype(p) <: Pair) || isempty(p)) + if u0 === missing && p === missing return odefn.initializeprob, odefn.update_initializeprob!, odefn.initializeprobmap, odefn.initializeprobpmap end + if !(eltype(u0) <: Pair) && !(eltype(p) <: Pair) + oldinitprob = odefn.initializeprob + if oldinitprob === nothing || !SciMLBase.has_sys(oldinitprob.f) || + !(oldinitprob.f.sys isa NonlinearSystem) + return oldinitprob, odefn.update_initializeprob!, odefn.initializeprobmap, + odefn.initializeprobpmap + end + pidxs = ParameterIndex[] + pvals = [] + u0idxs = Int[] + u0vals = [] + for sym in variable_symbols(oldinitprob) + if is_variable(sys, sym) + u0 !== missing || continue + idx = variable_index(oldinitprob, sym) + push!(u0idxs, idx) + push!(u0vals, eltype(u0)(state_values(oldinitprob, idx))) + else + p !== missing || continue + idx = variable_index(oldinitprob, sym) + push!(u0idxs, idx) + push!(u0vals, typeof(getp(sys, sym)(p))(state_values(oldinitprob, idx))) + end + end + if p !== missing + for sym in parameter_symbols(oldinitprob) + push!(pidxs, parameter_index(oldinitprob, sym)) + if isequal(sym, get_iv(sys)) + push!(pvals, t0) + else + push!(pvals, getp(sys, sym)(p)) + end + end + end + newu0 = remake_buffer(oldinitprob.f.sys, state_values(oldinitprob), u0idxs, u0vals) + newp = remake_buffer(oldinitprob.f.sys, parameter_values(oldinitprob), pidxs, pvals) + initprob = remake(oldinitprob; u0 = newu0, p = newp) + return initprob, odefn.update_initializeprob!, odefn.initializeprobmap, + odefn.initializeprobpmap + end if u0 === missing || isempty(u0) u0 = Dict() elseif !(eltype(u0) <: Pair) diff --git a/test/initializationsystem.jl b/test/initializationsystem.jl index d9757ffc5b..f30dc5fa3a 100644 --- a/test/initializationsystem.jl +++ b/test/initializationsystem.jl @@ -1,5 +1,7 @@ using ModelingToolkit, OrdinaryDiffEq, NonlinearSolve, Test -using SymbolicIndexingInterface +using ForwardDiff +using SymbolicIndexingInterface, SciMLStructures +using SciMLStructures: Tunable using ModelingToolkit: t_nounits as t, D_nounits as D @parameters g @@ -746,3 +748,35 @@ end prob5 = remake(prob) @test init(prob, Tsit5()).ps[p] ≈ 2.0 end + +@testset "`remake` changes initialization problem types" begin + @variables x(t) y(t) z(t) + @parameters p q + @mtkbuild sys = ODESystem( + [D(x) ~ x * p + y * q, y^2 * q + q^2 * x ~ 0, z * p - p^2 * x * z ~ 0], + t; guesses = [x => 0.0, y => 0.0, z => 0.0, p => 0.0, q => 0.0]) + prob = ODEProblem(sys, [x => 1.0], (0.0, 1.0), [p => 1.0, q => missing]) + @test is_variable(prob.f.initializeprob, q) + ps = prob.p + newps = SciMLStructures.replace(Tunable(), ps, ForwardDiff.Dual.(ps.tunable)) + prob2 = remake(prob; p = newps) + @test eltype(prob2.f.initializeprob.u0) <: ForwardDiff.Dual + @test eltype(prob2.f.initializeprob.p.tunable) <: ForwardDiff.Dual + @test prob2.f.initializeprob.u0 ≈ prob.f.initializeprob.u0 + + prob2 = remake(prob; u0 = ForwardDiff.Dual.(prob.u0)) + @test eltype(prob2.f.initializeprob.u0) <: ForwardDiff.Dual + @test eltype(prob2.f.initializeprob.p.tunable) <: Float64 + @test prob2.f.initializeprob.u0 ≈ prob.f.initializeprob.u0 + + prob2 = remake(prob; u0 = ForwardDiff.Dual.(prob.u0), p = newps) + @test eltype(prob2.f.initializeprob.u0) <: ForwardDiff.Dual + @test eltype(prob2.f.initializeprob.p.tunable) <: ForwardDiff.Dual + @test prob2.f.initializeprob.u0 ≈ prob.f.initializeprob.u0 + + prob2 = remake(prob; u0 = [x => ForwardDiff.Dual(1.0)], + p = [p => ForwardDiff.Dual(1.0), q => missing]) + @test eltype(prob2.f.initializeprob.u0) <: ForwardDiff.Dual + @test eltype(prob2.f.initializeprob.p.tunable) <: ForwardDiff.Dual + @test prob2.f.initializeprob.u0 ≈ prob.f.initializeprob.u0 +end From 015ce60b2d528f69dad83317100647985a817b02 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 1 Oct 2024 17:30:51 +0530 Subject: [PATCH 0103/2176] feat: preserve the `u0map` passed to `ODEProblem` and use it in `remake_initializeprob` --- src/systems/nonlinear/initializesystem.jl | 16 ++++++++++++++++ test/initializationsystem.jl | 16 ++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/src/systems/nonlinear/initializesystem.jl b/src/systems/nonlinear/initializesystem.jl index 5b465a677c..f0a07bfc25 100644 --- a/src/systems/nonlinear/initializesystem.jl +++ b/src/systems/nonlinear/initializesystem.jl @@ -157,15 +157,22 @@ function generate_initializesystem(sys::ODESystem; for k in keys(defs) defs[k] = substitute(defs[k], paramsubs) end + meta = InitializationSystemMetadata(Dict{Any, Any}(u0map), Dict{Any, Any}(pmap)) return NonlinearSystem(eqs_ics, vars, pars; defaults = defs, checks = check_units, name, + metadata = meta, kwargs...) end +struct InitializationSystemMetadata + u0map::Dict{Any, Any} + pmap::Dict{Any, Any} +end + function is_parameter_solvable(p, pmap, defs, guesses) _val1 = pmap isa AbstractDict ? get(pmap, p, nothing) : nothing _val2 = get(defs, p, nothing) @@ -253,6 +260,15 @@ function SciMLBase.remake_initializeprob(sys::ODESystem, odefn, u0, t0, p) !isempty(setobserved) || !isempty(setparobserved)) && ModelingToolkit.get_tearing_state(sys) !== nothing) || !isempty(initialization_equations(sys))) + if SciMLBase.has_initializeprob(odefn) + oldsys = odefn.initializeprob.f.sys + meta = get_metadata(oldsys) + if meta isa InitializationSystemMetadata + u0 = merge(meta.u0map, u0) + p = merge(meta.pmap, p) + end + end + initprob = InitializationProblem(sys, t0, u0, p) initprobmap = getu(initprob, unknowns(sys)) punknowns = [p for p in all_variable_symbols(initprob) if is_parameter(sys, p)] diff --git a/test/initializationsystem.jl b/test/initializationsystem.jl index f30dc5fa3a..77c015144e 100644 --- a/test/initializationsystem.jl +++ b/test/initializationsystem.jl @@ -780,3 +780,19 @@ end @test eltype(prob2.f.initializeprob.p.tunable) <: ForwardDiff.Dual @test prob2.f.initializeprob.u0 ≈ prob.f.initializeprob.u0 end + +@testset "`remake` preserves old u0map and pmap" begin + @variables x(t) y(t) + @parameters p + @mtkbuild sys = ODESystem( + [D(x) ~ x + p * y, y^2 + 4y * p^2 ~ x], t; guesses = [y => 1.0, p => 1.0]) + prob = ODEProblem(sys, [x => 1.0], (0.0, 1.0), [p => 1.0]) + @test is_variable(prob.f.initializeprob, y) + prob2 = @test_nowarn remake(prob; p = [p => 3.0]) # ensure no over/under-determined warning + @test is_variable(prob.f.initializeprob, y) + + prob = ODEProblem(sys, [y => 1.0, x => 2.0], (0.0, 1.0), [p => missing]) + @test is_variable(prob.f.initializeprob, p) + prob2 = @test_nowarn remake(prob; u0 = [y => 0.5]) + @test is_variable(prob.f.initializeprob, p) +end From cab11eb594002aea5ece2f5948c21a103878ccf1 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 7 Oct 2024 18:42:32 +0530 Subject: [PATCH 0104/2176] feat: validate parameter type and allow dependent initial values in param init --- src/systems/nonlinear/initializesystem.jl | 5 +++- src/utils.jl | 7 ++++++ test/initializationsystem.jl | 30 ++++++++++++++++++----- 3 files changed, 35 insertions(+), 7 deletions(-) diff --git a/src/systems/nonlinear/initializesystem.jl b/src/systems/nonlinear/initializesystem.jl index f0a07bfc25..6a21e9a6f0 100644 --- a/src/systems/nonlinear/initializesystem.jl +++ b/src/systems/nonlinear/initializesystem.jl @@ -174,13 +174,16 @@ struct InitializationSystemMetadata end function is_parameter_solvable(p, pmap, defs, guesses) + p = unwrap(p) + is_variable_floatingpoint(p) || return false _val1 = pmap isa AbstractDict ? get(pmap, p, nothing) : nothing _val2 = get(defs, p, nothing) _val3 = get(guesses, p, nothing) # either (missing is a default or was passed to the ODEProblem) or (nothing was passed to # the ODEProblem and it has a default and a guess) return ((_val1 === missing || _val2 === missing) || - (_val1 === nothing && _val2 !== nothing)) && _val3 !== nothing + (symbolic_type(_val1) != NotSymbolic() || + _val1 === nothing && _val2 !== nothing)) && _val3 !== nothing end function SciMLBase.remake_initializeprob(sys::ODESystem, odefn, u0, t0, p) diff --git a/src/utils.jl b/src/utils.jl index a3f99731af..29e61ede69 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -885,3 +885,10 @@ end diff2term_with_unit(x, t) = _with_unit(diff2term, x, t) lower_varname_with_unit(var, iv, order) = _with_unit(lower_varname, var, iv, iv, order) + +function is_variable_floatingpoint(sym) + sym = unwrap(sym) + T = symtype(sym) + return T == Real || T <: AbstractFloat || T <: AbstractArray{Real} || + T <: AbstractArray{<:AbstractFloat} +end diff --git a/test/initializationsystem.jl b/test/initializationsystem.jl index 77c015144e..08b653c22e 100644 --- a/test/initializationsystem.jl +++ b/test/initializationsystem.jl @@ -647,19 +647,37 @@ end prob2.ps[p] = 0.0 test_parameter(prob2, p, 2.0) - # Should not be solved for: + # Default overridden by ODEProblem, guess provided + @mtkbuild sys = ODESystem( + [D(x) ~ q * x, D(y) ~ y * p], t; defaults = [p => 2q], guesses = [p => 1.0]) + _pmap = merge(pmap, Dict(p => q)) + prob = ODEProblem(sys, u0map, (0.0, 1.0), _pmap) + test_parameter(prob, p, _pmap[q]) + test_initializesystem(sys, u0map, _pmap, p, 0 ~ q - p) - # ODEProblem value with guess, no `missing` + # ODEProblem dependent value with guess, no `missing` @mtkbuild sys = ODESystem([D(x) ~ x * q, D(y) ~ y * p], t; guesses = [p => 0.0]) _pmap = merge(pmap, Dict(p => 3q)) prob = ODEProblem(sys, u0map, (0.0, 1.0), _pmap) - @test prob.ps[p] ≈ 3.0 - @test prob.f.initializeprob === nothing - # Default overridden by ODEProblem, guess provided + test_parameter(prob, p, 3pmap[q]) + + # Should not be solved for: + + # Override dependent default with direct value @mtkbuild sys = ODESystem( [D(x) ~ q * x, D(y) ~ y * p], t; defaults = [p => 2q], guesses = [p => 1.0]) + _pmap = merge(pmap, Dict(p => 1.0)) prob = ODEProblem(sys, u0map, (0.0, 1.0), _pmap) - @test prob.ps[p] ≈ 3.0 + @test prob.ps[p] ≈ 1.0 + @test prob.f.initializeprob === nothing + + # Non-floating point + @parameters r::Int s::Int + @mtkbuild sys = ODESystem( + [D(x) ~ s * x, D(y) ~ y * r], t; defaults = [s => 2r], guesses = [s => 1.0]) + prob = ODEProblem(sys, u0map, (0.0, 1.0), [r => 1]) + @test prob.ps[r] == 1 + @test prob.ps[s] == 2 @test prob.f.initializeprob === nothing @mtkbuild sys = ODESystem([D(x) ~ x, p ~ x + y], t; guesses = [p => 0.0]) From 62c2f09d11c72dfeaf212270640fc7ff84000455 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 7 Oct 2024 22:14:57 +0530 Subject: [PATCH 0105/2176] fix: only allow numeric pdeps to be initialization equations --- src/systems/nonlinear/initializesystem.jl | 1 + 1 file changed, 1 insertion(+) diff --git a/src/systems/nonlinear/initializesystem.jl b/src/systems/nonlinear/initializesystem.jl index 6a21e9a6f0..1641d66dc0 100644 --- a/src/systems/nonlinear/initializesystem.jl +++ b/src/systems/nonlinear/initializesystem.jl @@ -132,6 +132,7 @@ function generate_initializesystem(sys::ODESystem; # 5) parameter dependencies become equations, their LHS become unknowns for eq in parameter_dependencies(sys) + is_variable_floatingpoint(eq.lhs) || continue varp = tovar(eq.lhs) paramsubs[eq.lhs] = varp push!(eqs_ics, eq) From ba82445d0de255f6340348c019c675a47a65609f Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 7 Oct 2024 22:15:11 +0530 Subject: [PATCH 0106/2176] feat: allow `nothing` to override retained values in `remake_initializeprob` --- src/systems/nonlinear/initializesystem.jl | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/systems/nonlinear/initializesystem.jl b/src/systems/nonlinear/initializesystem.jl index 1641d66dc0..38a3095b5c 100644 --- a/src/systems/nonlinear/initializesystem.jl +++ b/src/systems/nonlinear/initializesystem.jl @@ -246,6 +246,11 @@ function SciMLBase.remake_initializeprob(sys::ODESystem, odefn, u0, t0, p) u0 = todict(u0) defs = defaults(sys) varmap = merge(defs, u0) + for k in collect(keys(varmap)) + if varmap[k] === nothing + delete!(varmap, k) + end + end varmap = canonicalize_varmap(varmap) missingvars = setdiff(unknowns(sys), collect(keys(varmap))) setobserved = filter(keys(varmap)) do var @@ -272,6 +277,16 @@ function SciMLBase.remake_initializeprob(sys::ODESystem, odefn, u0, t0, p) p = merge(meta.pmap, p) end end + for k in collect(keys(u0)) + if u0[k] === nothing + delete!(u0, k) + end + end + for k in collect(keys(p)) + if p[k] === nothing + delete!(p, k) + end + end initprob = InitializationProblem(sys, t0, u0, p) initprobmap = getu(initprob, unknowns(sys)) From ec75ebee36aff1aefcd8dcc2bfc0ac6d3e196649 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 6 Aug 2024 18:45:41 +0530 Subject: [PATCH 0107/2176] docs: document parameter initialization in the tutorial --- docs/src/tutorials/initialization.md | 134 +++++++++++++++++++++++++++ 1 file changed, 134 insertions(+) diff --git a/docs/src/tutorials/initialization.md b/docs/src/tutorials/initialization.md index f40c28991f..6451832dbc 100644 --- a/docs/src/tutorials/initialization.md +++ b/docs/src/tutorials/initialization.md @@ -201,6 +201,87 @@ long enough you will see that `λ = 0` is required for this equation, but since problem constructor. Additionally, any warning about not being fully determined can be suppressed via passing `warn_initialize_determined = false`. +## Initialization of parameters + +Parameters may also be treated as unknowns in the initialization system. Doing so works +almost identically to the standard case. For a parameter to be an initialization unknown +(henceforth referred to as "solved parameter") it must represent a floating point number +(have a `symtype` of `Real` or `<:AbstractFloat`) or an array of such numbers. Additionally, +it must have a guess and one of the following conditions must be satisfied: + + 1. The value of the parameter as passed to `ODEProblem` is an expression involving other + variables/parameters. For example, if `[p => 2q + x]` is passed to `ODEProblem`. In + this case, `p ~ 2q + x` is used as an equation during initialization. + 2. The parameter has a default (and no value for it is given to `ODEProblem`, since + that is condition 1). The default will be used as an equation during initialization. + 3. The parameter has a default of `missing`. If `ODEProblem` is given a value for this + parameter, it is used as an equation during initialization (whether the value is an + expression or not). + 4. `ODEProblem` is given a value of `missing` for the parameter. If the parameter has a + default, it will be used as an equation during initialization. + +All parameter dependencies (where the dependent parameter is a floating point number or +array thereof) also become equations during initialization, and the dependent parameters +become unknowns. + +`remake` will reconstruct the initialization system and problem, given the new +constraints provided to it. The new values will be combined with the original +variable-value mapping provided to `ODEProblem` and used to construct the initialization +problem. + +### Parameter initialization by example + +Consider the following system, where the sum of two unknowns is a constant parameter +`total`. + +```@example paraminit +using ModelingToolkit, OrdinaryDiffEq # hidden +using ModelingToolkit: t_nounits as t, D_nounits as D # hidden + +@variables x(t) y(t) +@parameters total +@mtkbuild sys = ODESystem([D(x) ~ -x, total ~ x + y], t; + defaults = [total => missing], guesses = [total => 1.0]) +``` + +Given any two of `x`, `y` and `total` we can determine the remaining variable. + +```@example paraminit +prob = ODEProblem(sys, [x => 1.0, y => 2.0], (0.0, 1.0)) +integ = init(prob, Tsit5()) +@assert integ.ps[total] ≈ 3.0 # hide +integ.ps[total] +``` + +Suppose we want to re-create this problem, but now solve for `x` given `total` and `y`: + +```@example paraminit +prob2 = remake(prob; u0 = [y => 1.0], p = [total => 4.0]) +initsys = prob2.f.initializeprob.f.sys +``` + +The system is now overdetermined. In fact: + +```@example paraminit +[equations(initsys); observed(initsys)] +``` + +The system can never be satisfied and will always lead to an `InitialFailure`. This is +due to the aforementioned behavior of retaining the original variable-value mapping +provided to `ODEProblem`. To fix this, we pass `x => nothing` to `remake` to remove its +retained value. + +```@example paraminit +prob2 = remake(prob; u0 = [y => 1.0, x => nothing], p = [total => 4.0]) +initsys = prob2.f.initializeprob.f.sys +``` + +The system is fully determined, and the equations are solvable. + +```@example +[equations(initsys); observed(initsys)] +``` + ## Diving Deeper: Constructing the Initialization System To get a better sense of the initialization system and to help debug it, you can construct @@ -383,3 +464,56 @@ sol[α * x - β * x * y] ```@example init plot(sol) ``` + +## Solving for parameters during initialization + +Sometimes, it is necessary to solve for a parameter during initialization. For example, +given a spring-mass system we want to find the un-stretched length of the spring given +that the initial condition of the system is its steady state. + +```@example init +using ModelingToolkitStandardLibrary.Mechanical.TranslationalModelica: Fixed, Mass, Spring, + Force, Damper +using ModelingToolkitStandardLibrary.Blocks: Constant + +@named mass = Mass(; m = 1.0, s = 1.0, v = 0.0, a = 0.0) +@named fixed = Fixed(; s0 = 0.0) +@named spring = Spring(; c = 2.0, s_rel0 = missing) +@named gravity = Force() +@named constant = Constant(; k = 9.81) +@named damper = Damper(; d = 0.1) +@mtkbuild sys = ODESystem( + [connect(fixed.flange, spring.flange_a), connect(spring.flange_b, mass.flange_a), + connect(mass.flange_a, gravity.flange), connect(constant.output, gravity.f), + connect(fixed.flange, damper.flange_a), connect(damper.flange_b, mass.flange_a)], + t; + systems = [fixed, spring, mass, gravity, constant, damper], + guesses = [spring.s_rel0 => 1.0]) +``` + +Note that we explicitly provide `s_rel0 = missing` to the spring. Parameters are only +solved for during initialization if their value (either default, or explicitly passed +to the `ODEProblem` constructor) is `missing`. We also need to provide a guess for the +parameter. + +If a parameter is not given a value of `missing`, and does not have a default or initial +value, the `ODEProblem` constructor will throw an error. If the parameter _does_ have a +value of `missing`, it must be given a guess. + +```@example init +prob = ODEProblem(sys, [], (0.0, 1.0)) +prob.ps[spring.s_rel0] +``` + +Note that the value of the parameter in the problem is zero, similar to unknowns that +are solved for during initialization. + +```@example init +integ = init(prob) +integ.ps[spring.s_rel0] +``` + +The un-stretched length of the spring is now correctly calculated. The same result can be +achieved if `s_rel0 = missing` is omitted when constructing `spring`, and instead +`spring.s_rel0 => missing` is passed to the `ODEProblem` constructor along with values +of other parameters. From 84a1f2e233624a4fd2a60420d03f6349e3c74aa6 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 9 Oct 2024 17:54:17 +0530 Subject: [PATCH 0108/2176] build: bump compats for SciMLBase, DiffEqBase, OrdinaryDiffEqCore --- Project.toml | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/Project.toml b/Project.toml index 1947ea7ce6..cfd424ef8b 100644 --- a/Project.toml +++ b/Project.toml @@ -81,7 +81,7 @@ ConstructionBase = "1" DataInterpolations = "6.4" DataStructures = "0.17, 0.18" DeepDiffs = "1" -DiffEqBase = "6.103.0" +DiffEqBase = "6.157" DiffEqCallbacks = "2.16, 3, 4" DiffEqNoiseProcess = "5" DiffRules = "0.1, 1.0" @@ -110,12 +110,13 @@ NonlinearSolve = "3.14" OffsetArrays = "1" OrderedCollections = "1" OrdinaryDiffEq = "6.82.0" +OrdinaryDiffEqCore = "1.7.0" PrecompileTools = "1" REPL = "1" RecursiveArrayTools = "3.26" Reexport = "0.2, 1" RuntimeGeneratedFunctions = "0.5.9" -SciMLBase = "2.55" +SciMLBase = "2.56.1" SciMLStructures = "1.0" Serialization = "1" Setfield = "0.7, 0.8, 1" @@ -148,6 +149,7 @@ Optimization = "7f7a1694-90dd-40f0-9382-eb1efda571ba" OptimizationMOI = "fd9f6733-72f4-499f-8506-86b2bdd0dea1" OptimizationOptimJL = "36348300-93cb-4f02-beb5-3c3902f8871e" OrdinaryDiffEq = "1dea7af3-3e70-54e6-95c3-0bf5283fa5ed" +OrdinaryDiffEqCore = "bbf590c4-e513-4bbe-9b18-05decba2e5d8" Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f" REPL = "3fa0cd96-eef1-5676-8a61-b3b8758bbffb" Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c" @@ -162,4 +164,4 @@ Sundials = "c3572dad-4567-51f8-b174-8c6c989267f4" Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" [targets] -test = ["AmplNLWriter", "BenchmarkTools", "ControlSystemsBase", "DataInterpolations", "DelayDiffEq", "NonlinearSolve", "ForwardDiff", "Ipopt", "Ipopt_jll", "ModelingToolkitStandardLibrary", "Optimization", "OptimizationOptimJL", "OptimizationMOI", "OrdinaryDiffEq", "REPL", "Random", "ReferenceTests", "SafeTestsets", "StableRNGs", "Statistics", "SteadyStateDiffEq", "Test", "StochasticDiffEq", "Sundials", "StochasticDelayDiffEq", "Pkg", "JET"] +test = ["AmplNLWriter", "BenchmarkTools", "ControlSystemsBase", "DataInterpolations", "DelayDiffEq", "NonlinearSolve", "ForwardDiff", "Ipopt", "Ipopt_jll", "ModelingToolkitStandardLibrary", "Optimization", "OptimizationOptimJL", "OptimizationMOI", "OrdinaryDiffEq", "OrdinaryDiffEqCore", "REPL", "Random", "ReferenceTests", "SafeTestsets", "StableRNGs", "Statistics", "SteadyStateDiffEq", "Test", "StochasticDiffEq", "Sundials", "StochasticDelayDiffEq", "Pkg", "JET"] From 816fde74ed572cbb0201e67f42abd32afc4594d0 Mon Sep 17 00:00:00 2001 From: Christopher Rackauckas Date: Thu, 10 Oct 2024 04:02:36 -0400 Subject: [PATCH 0109/2176] Update Project.toml --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index cfd424ef8b..23ae9f8d13 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "ModelingToolkit" uuid = "961ee093-0014-501f-94e3-6117800e7a78" authors = ["Yingbo Ma ", "Chris Rackauckas and contributors"] -version = "9.44.0" +version = "9.45.0" [deps] AbstractTrees = "1520ce14-60c1-5f80-bbc7-55ef81b5835c" From ae22c7127fef1210f2d4c17210d45d60a5d58933 Mon Sep 17 00:00:00 2001 From: Venkateshprasad <32921645+ven-k@users.noreply.github.com> Date: Tue, 8 Oct 2024 14:06:52 +0000 Subject: [PATCH 0110/2176] feat: add helpers to update symbolic array metadata --- src/systems/model_parsing.jl | 64 +++++++++++++++++++++++++++++++++--- test/model_parsing.jl | 6 ++-- 2 files changed, 64 insertions(+), 6 deletions(-) diff --git a/src/systems/model_parsing.jl b/src/systems/model_parsing.jl index 7b6705bbd5..8a0f5165e4 100644 --- a/src/systems/model_parsing.jl +++ b/src/systems/model_parsing.jl @@ -163,7 +163,7 @@ function update_kwargs_and_metadata!(dict, kwargs, a, def, indices, type, var, push!(kwargs, Expr(:kw, :($a::Union{Nothing, Missing, $NoValue, $type}), NO_VALUE)) end - dict[:kwargs][getname(var)] = Dict(:value => def, :type => type) + dict[:kwargs][a] = Dict(:value => def, :type => type) else vartype = gensym(:T) push!(kwargs, @@ -177,15 +177,71 @@ function update_kwargs_and_metadata!(dict, kwargs, a, def, indices, type, var, else push!(where_types, :($vartype <: $type)) end - dict[:kwargs][getname(var)] = Dict(:value => def, :type => AbstractArray{type}) + dict[:kwargs][a] = Dict(:value => def, :type => AbstractArray{type}) end if dict[varclass] isa Vector - dict[varclass][1][getname(var)][:type] = AbstractArray{type} + dict[varclass][1][a][:type] = AbstractArray{type} else - dict[varclass][getname(var)][:type] = type + dict[varclass][a][:type] = type end end +function update_readable_metadata!(varclass_dict, meta::Dict, varname) + metatypes = [(:connection_type, VariableConnectType), + (:description, VariableDescription), + (:unit, VariableUnit), + (:bounds, VariableBounds), + (:noise, VariableNoiseType), + (:input, VariableInput), + (:output, VariableOutput), + (:irreducible, VariableIrreducible), + (:state_priority, VariableStatePriority), + (:misc, VariableMisc), + (:disturbance, VariableDisturbance), + (:tunable, VariableTunable), + (:dist, VariableDistribution)] + + var_dict = get!(varclass_dict, varname) do + Dict{Symbol, Any}() + end + + for (type, key) in metatypes + if (mt = get(meta, key, nothing)) !== nothing + key == VariableConnectType && (mt = nameof(mt)) + var_dict[type] = mt + end + end +end + +function push_array_kwargs_and_metadata!( + dict, indices, meta, type, varclass, varname, varval) + dict[varclass] = get!(dict, varclass) do + Dict{Symbol, Dict{Symbol, Any}}() + end + varclass_dict = dict[varclass] isa Vector ? Ref(dict[varclass][1]) : Ref(dict[varclass]) + + merge!(varclass_dict[], + Dict(varname => Dict( + :size => tuple([index_arg.args[end] for index_arg in indices]...), + :value => varval, + :type => type + ))) + + # Useful keys for kwargs entry are: value, type and size. + dict[:kwargs][varname] = varclass_dict[][varname] + + meta !== nothing && update_readable_metadata!(varclass_dict[], meta, varname) +end + +function unit_handled_variable_value(meta, varname) + varval = if meta isa Nothing || get(meta, VariableUnit, nothing) isa Nothing + varname + else + :($convert_units($(meta[VariableUnit]), $varname)) + end + return varval +end + function parse_variable_def!(dict, mod, arg, varclass, kwargs, where_types; def = nothing, indices::Union{Vector{UnitRange{Int}}, Nothing} = nothing, type::Type = Real, meta = Dict{DataType, Expr}()) diff --git a/test/model_parsing.jl b/test/model_parsing.jl index f80dc77dc4..727954a00d 100644 --- a/test/model_parsing.jl +++ b/test/model_parsing.jl @@ -259,7 +259,8 @@ end @test all(collect(hasmetadata.(model.l, ModelingToolkit.VariableDescription))) @test all(lastindex.([model.a2, model.b2, model.d2, model.e2, model.h2]) .== 2) - @test size(model.l) == MockModel.structure[:parameters][:l][:size] == (2, 3) + @test size(model.l) == (2, 3) + @test MockModel.structure[:parameters][:l][:size] == (2, 3) model = complete(model) @test getdefault(model.cval) == 1 @@ -474,7 +475,8 @@ using ModelingToolkit: getdefault, scalarize @named model_with_component_array = ModelWithComponentArray() - @test eval(ModelWithComponentArray.structure[:parameters][:r][:unit]) == eval(u"Ω") + @test eval(ModelWithComponentArray.structure[:parameters][:r][:unit]) == + eval(u"Ω") @test lastindex(parameters(model_with_component_array)) == 3 # Test the constant `k`. Manually k's value should be kept in sync here From b0ab722aa5e9412a615b3e73d78bd2048a9755a2 Mon Sep 17 00:00:00 2001 From: Venkateshprasad <32921645+ven-k@users.noreply.github.com> Date: Tue, 8 Oct 2024 14:09:58 +0000 Subject: [PATCH 0111/2176] feat: support arbitrary length arrays with metadata and default Update metadata dict and kwargs for symbolic arrays --- src/systems/model_parsing.jl | 207 +++++++++++++++++++++-------------- 1 file changed, 127 insertions(+), 80 deletions(-) diff --git a/src/systems/model_parsing.jl b/src/systems/model_parsing.jl index 8a0f5165e4..04b133ba87 100644 --- a/src/systems/model_parsing.jl +++ b/src/systems/model_parsing.jl @@ -245,20 +245,6 @@ end function parse_variable_def!(dict, mod, arg, varclass, kwargs, where_types; def = nothing, indices::Union{Vector{UnitRange{Int}}, Nothing} = nothing, type::Type = Real, meta = Dict{DataType, Expr}()) - metatypes = [(:connection_type, VariableConnectType), - (:description, VariableDescription), - (:unit, VariableUnit), - (:bounds, VariableBounds), - (:noise, VariableNoiseType), - (:input, VariableInput), - (:output, VariableOutput), - (:irreducible, VariableIrreducible), - (:state_priority, VariableStatePriority), - (:misc, VariableMisc), - (:disturbance, VariableDisturbance), - (:tunable, VariableTunable), - (:dist, VariableDistribution)] - arg isa LineNumberNode && return MLStyle.@match arg begin a::Symbol => begin @@ -284,27 +270,86 @@ function parse_variable_def!(dict, mod, arg, varclass, kwargs, where_types; varclass, where_types, meta) return var, def, Dict() end + Expr(:tuple, Expr(:(::), Expr(:ref, a, indices...), type), meta_val) || + Expr(:tuple, Expr(:ref, a, indices...), meta_val) => begin + (@isdefined type) || (type = Real) + varname = Meta.isexpr(a, :call) ? a.args[1] : a + push!(kwargs, Expr(:kw, varname, nothing)) + meta = parse_metadata(mod, meta_val) + varval = (@isdefined default_val) ? default_val : + unit_handled_variable_value(meta, varname) + if varclass == :parameters + var = :($varname = $first(@parameters ($a[$(indices...)]::$type = $varval), + $meta_val)) + else + var = :($varname = $first(@variables ($a[$(indices)]::$type = $varval), + $meta_val)) + end + push_array_kwargs_and_metadata!( + dict, indices, meta, type, varclass, varname, varval) + (:($varname...), var), nothing, Dict() + end + Expr(:(=), Expr(:(::), Expr(:ref, a, indices...), type), def_n_meta) || + Expr(:(=), Expr(:ref, a, indices...), def_n_meta) => begin + (@isdefined type) || (type = Real) + varname = Meta.isexpr(a, :call) ? a.args[1] : a + if Meta.isexpr(def_n_meta, :tuple) + meta = parse_metadata(mod, def_n_meta) + varval = unit_handled_variable_value(meta, varname) + val, def_n_meta = (def_n_meta.args[1], def_n_meta.args[2:end]) + push!(kwargs, Expr(:kw, varname, nothing)) + if varclass == :parameters + var = :($varname = $varname === nothing ? $val : $varname; + $varname = $first(@parameters ($a[$(indices...)]::$type = $varval), + $(def_n_meta...))) + else + var = :($varname = $varname === nothing ? $val : $varname; + $varname = $first(@variables $a[$(indices...)]::$type = ( + $varval), + $(def_n_meta...))) + end + else + push!(kwargs, Expr(:kw, varname, nothing)) + if varclass == :parameters + var = :($varname = $varname === nothing ? $def_n_meta : $varname; + $varname = $first(@parameters $a[$(indices...)]::$type = $varname)) + else + var = :($varname = $varname === nothing ? $def_n_meta : $varname; + $varname = $first(@variables $a[$(indices...)]::$type = $varname)) + end + varval, meta = def_n_meta, nothing + end + push_array_kwargs_and_metadata!( + dict, indices, meta, type, varclass, varname, varval) + (:($varname...), var), nothing, Dict() + end + Expr(:(::), Expr(:ref, a, indices...), type) || + Expr(:ref, a, indices...) => begin + (@isdefined type) || (type = Real) + varname = a isa Expr && a.head == :call ? a.args[1] : a + push!(kwargs, Expr(:kw, varname, nothing)) + if varclass == :parameters + var = :($varname = $first(@parameters $a[$(indices...)]::$type = $varname)) + elseif varclass == :variables + var = :($varname = $first(@variables $a[$(indices...)]::$type = $varname)) + else + throw("Symbolic array with arbitrary length is not handled for $varclass. + Please open an issue with an example.") + end + push_array_kwargs_and_metadata!( + dict, indices, nothing, type, varclass, varname, nothing) + (:($varname...), var), nothing, Dict() + end Expr(:(=), a, b) => begin Base.remove_linenums!(b) def, meta = parse_default(mod, b) var, def, _ = parse_variable_def!( dict, mod, a, varclass, kwargs, where_types; def, type, meta) - if dict[varclass] isa Vector - dict[varclass][1][getname(var)][:default] = def - else - dict[varclass][getname(var)][:default] = def - end + varclass_dict = dict[varclass] isa Vector ? Ref(dict[varclass][1]) : + Ref(dict[varclass]) + varclass_dict[][getname(var)][:default] = def if meta !== nothing - for (type, key) in metatypes - if (mt = get(meta, key, nothing)) !== nothing - key == VariableConnectType && (mt = nameof(mt)) - if dict[varclass] isa Vector - dict[varclass][1][getname(var)][type] = mt - else - dict[varclass][getname(var)][type] = mt - end - end - end + update_readable_metadata!(varclass_dict[], meta, getname(var)) var, metadata_with_exprs = set_var_metadata(var, meta) return var, def, metadata_with_exprs end @@ -314,27 +359,15 @@ function parse_variable_def!(dict, mod, arg, varclass, kwargs, where_types; meta = parse_metadata(mod, b) var, def, _ = parse_variable_def!( dict, mod, a, varclass, kwargs, where_types; type, meta) + varclass_dict = dict[varclass] isa Vector ? Ref(dict[varclass][1]) : + Ref(dict[varclass]) if meta !== nothing - for (type, key) in metatypes - if (mt = get(meta, key, nothing)) !== nothing - key == VariableConnectType && (mt = nameof(mt)) - if dict[varclass] isa Vector - dict[varclass][1][getname(var)][type] = mt - else - dict[varclass][getname(var)][type] = mt - end - end - end + update_readable_metadata!(varclass_dict[], meta, getname(var)) var, metadata_with_exprs = set_var_metadata(var, meta) return var, def, metadata_with_exprs end return var, def, Dict() end - Expr(:ref, a, b...) => begin - indices = map(i -> UnitRange(i.args[2], i.args[end]), b) - parse_variable_def!(dict, mod, a, varclass, kwargs, where_types; - def, indices, type, meta) - end _ => error("$arg cannot be parsed") end end @@ -442,14 +475,23 @@ function parse_default(mod, a) end end -function parse_metadata(mod, a) +function parse_metadata(mod, a::Expr) MLStyle.@match a begin - Expr(:vect, eles...) => Dict(parse_metadata(mod, e) for e in eles) + Expr(:vect, b...) => Dict(parse_metadata(mod, m) for m in b) + Expr(:tuple, a, b...) => parse_metadata(mod, b) Expr(:(=), a, b) => Symbolics.option_to_metadata_type(Val(a)) => get_var(mod, b) _ => error("Cannot parse metadata $a") end end +function parse_metadata(mod, metadata::AbstractArray) + ret = Dict() + for m in metadata + merge!(ret, parse_metadata(mod, m)) + end + ret +end + function _set_var_metadata!(metadata_with_exprs, a, m, v::Expr) push!(metadata_with_exprs, m => v) a @@ -707,6 +749,7 @@ function parse_variable_arg!(exprs, vs, dict, mod, arg, varclass, kwargs, where_ end function convert_units(varunits::DynamicQuantities.Quantity, value) + value isa Nothing && return nothing DynamicQuantities.ustrip(DynamicQuantities.uconvert( DynamicQuantities.SymbolicUnits.as_quantity(varunits), value)) end @@ -718,6 +761,7 @@ function convert_units( end function convert_units(varunits::Unitful.FreeUnits, value) + value isa Nothing && return nothing Unitful.ustrip(varunits, value) end @@ -736,47 +780,50 @@ end function parse_variable_arg(dict, mod, arg, varclass, kwargs, where_types) vv, def, metadata_with_exprs = parse_variable_def!( dict, mod, arg, varclass, kwargs, where_types) - name = getname(vv) - - varexpr = if haskey(metadata_with_exprs, VariableUnit) - unit = metadata_with_exprs[VariableUnit] - quote - $name = if $name === $NO_VALUE - $setdefault($vv, $def) - else - try - $setdefault($vv, $convert_units($unit, $name)) - catch e - if isa(e, $(DynamicQuantities.DimensionError)) || - isa(e, $(Unitful.DimensionError)) - error("Unable to convert units for \'" * string(:($$vv)) * "\'") - elseif isa(e, MethodError) - error("No or invalid units provided for \'" * string(:($$vv)) * - "\'") - else - rethrow(e) + if !(vv isa Tuple) + name = getname(vv) + varexpr = if haskey(metadata_with_exprs, VariableUnit) + unit = metadata_with_exprs[VariableUnit] + quote + $name = if $name === $NO_VALUE + $setdefault($vv, $def) + else + try + $setdefault($vv, $convert_units($unit, $name)) + catch e + if isa(e, $(DynamicQuantities.DimensionError)) || + isa(e, $(Unitful.DimensionError)) + error("Unable to convert units for \'" * string(:($$vv)) * "\'") + elseif isa(e, MethodError) + error("No or invalid units provided for \'" * string(:($$vv)) * + "\'") + else + rethrow(e) + end end end end - end - else - quote - $name = if $name === $NO_VALUE - $setdefault($vv, $def) - else - $setdefault($vv, $name) + else + quote + $name = if $name === $NO_VALUE + $setdefault($vv, $def) + else + $setdefault($vv, $name) + end end end - end - metadata_expr = Expr(:block) - for (k, v) in metadata_with_exprs - push!(metadata_expr.args, - :($name = $wrap($set_scalar_metadata($unwrap($name), $k, $v)))) - end + metadata_expr = Expr(:block) + for (k, v) in metadata_with_exprs + push!(metadata_expr.args, + :($name = $wrap($set_scalar_metadata($unwrap($name), $k, $v)))) + end - push!(varexpr.args, metadata_expr) - return vv isa Num ? name : :($name...), varexpr + push!(varexpr.args, metadata_expr) + return vv isa Num ? name : :($name...), varexpr + else + return vv + end end function handle_conditional_vars!( From a403880cc92493fd3bd980fc1c72bba6e50eb1a5 Mon Sep 17 00:00:00 2001 From: Venkateshprasad <32921645+ven-k@users.noreply.github.com> Date: Mon, 23 Sep 2024 20:32:56 +0000 Subject: [PATCH 0112/2176] docs: use symbolic array with arbitray length in ModelC example --- docs/src/basics/MTKLanguage.md | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/docs/src/basics/MTKLanguage.md b/docs/src/basics/MTKLanguage.md index a2fb7d0870..a656b24096 100644 --- a/docs/src/basics/MTKLanguage.md +++ b/docs/src/basics/MTKLanguage.md @@ -63,13 +63,14 @@ end @structural_parameters begin f = sin N = 2 + M = 3 end begin v_var = 1.0 end @variables begin v(t) = v_var - v_array(t)[1:2, 1:3] + v_array(t)[1:N, 1:M] v_for_defaults(t) end @extend ModelB(; p1) @@ -324,10 +325,10 @@ For example, the structure of `ModelC` is: julia> ModelC.structure Dict{Symbol, Any} with 10 entries: :components => Any[Union{Expr, Symbol}[:model_a, :ModelA], Union{Expr, Symbol}[:model_array_a, :ModelA, :(1:N)], Union{Expr, Symbol}[:model_array_b, :ModelA, :(1:N)]] - :variables => Dict{Symbol, Dict{Symbol, Any}}(:v=>Dict(:default=>:v_var, :type=>Real), :v_array=>Dict(:type=>Real, :size=>(2, 3)), :v_for_defaults=>Dict(:type=>Real)) + :variables => Dict{Symbol, Dict{Symbol, Any}}(:v=>Dict(:default=>:v_var, :type=>Real), :v_for_defaults=>Dict(:type=>Real)) :icon => URI("https://github.com/SciML/SciMLDocs/blob/main/docs/src/assets/logo.png") - :kwargs => Dict{Symbol, Dict}(:f=>Dict(:value=>:sin), :N=>Dict(:value=>2), :v=>Dict{Symbol, Any}(:value=>:v_var, :type=>Real), :v_array=>Dict{Symbol, Union{Nothing, UnionAll}}(:value=>nothing, :type=>AbstractArray{Real}), :v_for_defaults=>Dict{Symbol, Union{Nothing, DataType}}(:value=>nothing, :type=>Real), :p1=>Dict(:value=>nothing)) - :structural_parameters => Dict{Symbol, Dict}(:f=>Dict(:value=>:sin), :N=>Dict(:value=>2)) + :kwargs => Dict{Symbol, Dict}(:f => Dict(:value => :sin), :N => Dict(:value => 2), :M => Dict(:value => 3), :v => Dict{Symbol, Any}(:value => :v_var, :type => Real), :v_for_defaults => Dict{Symbol, Union{Nothing, DataType}}(:value => nothing, :type => Real), :p1 => Dict(:value => nothing)), + :structural_parameters => Dict{Symbol, Dict}(:f => Dict(:value => :sin), :N => Dict(:value => 2), :M => Dict(:value => 3)) :independent_variable => t :constants => Dict{Symbol, Dict}(:c=>Dict{Symbol, Any}(:value=>1, :type=>Int64, :description=>"Example constant.")) :extend => Any[[:p2, :p1], Symbol("#mtkmodel__anonymous__ModelB"), :ModelB] From 9f4e7b8f92d2efa50c2c359d41dac5e95ce8c860 Mon Sep 17 00:00:00 2001 From: Venkateshprasad <32921645+ven-k@users.noreply.github.com> Date: Tue, 8 Oct 2024 14:12:20 +0000 Subject: [PATCH 0113/2176] feat: parity in symbolic-array definition with vanilla `@variables` --- src/systems/model_parsing.jl | 2 ++ test/model_parsing.jl | 15 +++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/src/systems/model_parsing.jl b/src/systems/model_parsing.jl index 04b133ba87..3e011b044a 100644 --- a/src/systems/model_parsing.jl +++ b/src/systems/model_parsing.jl @@ -270,6 +270,8 @@ function parse_variable_def!(dict, mod, arg, varclass, kwargs, where_types; varclass, where_types, meta) return var, def, Dict() end + Expr(:tuple, Expr(:(=), Expr(:ref, a, indices...), default_val), meta_val) || + Expr(:tuple, Expr(:(=), Expr(:(::), Expr(:ref, a, indices...), type), default_val), meta_val) || Expr(:tuple, Expr(:(::), Expr(:ref, a, indices...), type), meta_val) || Expr(:tuple, Expr(:ref, a, indices...), meta_val) => begin (@isdefined type) || (type = Real) diff --git a/test/model_parsing.jl b/test/model_parsing.jl index 727954a00d..c108b88625 100644 --- a/test/model_parsing.jl +++ b/test/model_parsing.jl @@ -279,6 +279,21 @@ end @test MockModel.structure[:defaults] == Dict(:n => 1.0, :n2 => "g()") end +@testset "Arrays using vanilla-@variable syntax" begin + @mtkmodel TupleInArrayDef begin + @parameters begin + (l(t)[1:2, 1:3] = 1), [description = "l is more than 1D"] + (l2(t)[1:3] = 2), [description = "l2 is 1D"] + (l3(t)[1:3]::Int = 3), [description = "l3 is 1D and has a type"] + end + end + + @named arr = TupleInArrayDef() + @test getdefault(arr.l) == 1 + @test getdefault(arr.l2) == 2 + @test getdefault(arr.l3) == 3 +end + @testset "Type annotation" begin @mtkmodel TypeModel begin @structural_parameters begin From d7d82bf1ce82626de29be9c2bf1aae1349c184eb Mon Sep 17 00:00:00 2001 From: Venkateshprasad <32921645+ven-k@users.noreply.github.com> Date: Tue, 8 Oct 2024 14:46:08 +0000 Subject: [PATCH 0114/2176] feat: check independent-var usage in symbolic vars - Ensure `@variables` are variables of an independent var. - In both `@variables` and `@parameters` case, ensure a single indpendent var is used --- src/systems/model_parsing.jl | 34 ++++++++++++++++++++++++++++++---- 1 file changed, 30 insertions(+), 4 deletions(-) diff --git a/src/systems/model_parsing.jl b/src/systems/model_parsing.jl index 3e011b044a..94469308e4 100644 --- a/src/systems/model_parsing.jl +++ b/src/systems/model_parsing.jl @@ -281,9 +281,13 @@ function parse_variable_def!(dict, mod, arg, varclass, kwargs, where_types; varval = (@isdefined default_val) ? default_val : unit_handled_variable_value(meta, varname) if varclass == :parameters + Meta.isexpr(a, :call) && assert_unique_independent_var(dict, a.args[end]) var = :($varname = $first(@parameters ($a[$(indices...)]::$type = $varval), $meta_val)) else + Meta.isexpr(a, :call) || + throw("$a is not a variable of the independent variable") + assert_unique_independent_var(dict, a.args[end]) var = :($varname = $first(@variables ($a[$(indices)]::$type = $varval), $meta_val)) end @@ -301,10 +305,15 @@ function parse_variable_def!(dict, mod, arg, varclass, kwargs, where_types; val, def_n_meta = (def_n_meta.args[1], def_n_meta.args[2:end]) push!(kwargs, Expr(:kw, varname, nothing)) if varclass == :parameters + Meta.isexpr(a, :call) && + assert_unique_independent_var(dict, a.args[end]) var = :($varname = $varname === nothing ? $val : $varname; $varname = $first(@parameters ($a[$(indices...)]::$type = $varval), $(def_n_meta...))) else + Meta.isexpr(a, :call) || + throw("$a is not a variable of the independent variable") + assert_unique_independent_var(dict, a.args[end]) var = :($varname = $varname === nothing ? $val : $varname; $varname = $first(@variables $a[$(indices...)]::$type = ( $varval), @@ -313,9 +322,14 @@ function parse_variable_def!(dict, mod, arg, varclass, kwargs, where_types; else push!(kwargs, Expr(:kw, varname, nothing)) if varclass == :parameters + Meta.isexpr(a, :call) && + assert_unique_independent_var(dict, a.args[end]) var = :($varname = $varname === nothing ? $def_n_meta : $varname; $varname = $first(@parameters $a[$(indices...)]::$type = $varname)) else + Meta.isexpr(a, :call) || + throw("$a is not a variable of the independent variable") + assert_unique_independent_var(dict, a.args[end]) var = :($varname = $varname === nothing ? $def_n_meta : $varname; $varname = $first(@variables $a[$(indices...)]::$type = $varname)) end @@ -331,8 +345,12 @@ function parse_variable_def!(dict, mod, arg, varclass, kwargs, where_types; varname = a isa Expr && a.head == :call ? a.args[1] : a push!(kwargs, Expr(:kw, varname, nothing)) if varclass == :parameters + Meta.isexpr(a, :call) && assert_unique_independent_var(dict, a.args[end]) var = :($varname = $first(@parameters $a[$(indices...)]::$type = $varname)) elseif varclass == :variables + Meta.isexpr(a, :call) || + throw("$a is not a variable of the independent variable") + assert_unique_independent_var(dict, a.args[end]) var = :($varname = $first(@variables $a[$(indices...)]::$type = $varname)) else throw("Symbolic array with arbitrary length is not handled for $varclass. @@ -411,14 +429,22 @@ function generate_var!(dict, a, varclass; generate_var(a, varclass; indices, type) end +function assert_unique_independent_var(dict, iv::Num) + assert_unique_independent_var(dict, nameof(iv)) +end +function assert_unique_independent_var(dict, iv) + prev_iv = get!(dict, :independent_variable) do + iv + end + prev_iv isa Num && (prev_iv = nameof(prev_iv)) + @assert isequal(iv, prev_iv) "Multiple independent variables are used in the model $(typeof(iv)) $(typeof(prev_iv))" +end + function generate_var!(dict, a, b, varclass, mod; indices::Union{Vector{UnitRange{Int}}, Nothing} = nothing, type = Real) iv = b == :t ? get_t(mod, b) : generate_var(b, :independent_variables) - prev_iv = get!(dict, :independent_variable) do - iv - end - @assert isequal(iv, prev_iv) "Multiple independent variables are used in the model" + assert_unique_independent_var(dict, iv) check_name_uniqueness(dict, a, varclass) vd = get!(dict, varclass) do Dict{Symbol, Dict{Symbol, Any}}() From 84bf015a4541037ea141c8dc861c529b1e057a80 Mon Sep 17 00:00:00 2001 From: Venkateshprasad <32921645+ven-k@users.noreply.github.com> Date: Tue, 8 Oct 2024 15:08:53 +0000 Subject: [PATCH 0115/2176] docs: add a dedicated section to showcase symbolic-array definition With many ways to define, this feature demands a dedicated section. --- docs/src/basics/MTKLanguage.md | 42 +++++++++++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/docs/src/basics/MTKLanguage.md b/docs/src/basics/MTKLanguage.md index a656b24096..685c549429 100644 --- a/docs/src/basics/MTKLanguage.md +++ b/docs/src/basics/MTKLanguage.md @@ -302,7 +302,7 @@ end For more examples of usage, checkout [ModelingToolkitStandardLibrary.jl](https://github.com/SciML/ModelingToolkitStandardLibrary.jl/) -## More on `Model.structure` +## [More on `Model.structure`](@id model_structure) `structure` stores metadata that describes composition of a model. It includes: @@ -336,6 +336,46 @@ Dict{Symbol, Any} with 10 entries: :equations => Any["model_a.k ~ f(v)"] ``` +### Different ways to define symbolics arrays: + +`@mtkmodel` supports symbolics arrays in both `@parameters` and `@variables`. +Using a structural parameters, symbolic arrays of arbitrary lengths can be defined. +Refer the following example for different ways to define symbolic arrays. + +```@example mtkmodel-example +@mtkmodel ModelWithArrays begin + @structural_parameters begin + N = 2 + M = 3 + end + @parameters begin + p1[1:4] + p2[1:N] + p3[1:N, 1:M] = 10, + [description = "A multi-dimensional array of arbitrary length with description"] + (p4[1:N, 1:M] = 10), + [description = "An alternate syntax for p3 to match the syntax of vanilla parameters macro"] + end + @variables begin + v1(t)[1:2] = 10, [description = "An array of variable `v1`"] + v2(t)[1:3] = [1, 2, 3] + end +end +``` + +The size of symbolic array can be accessed via `:size` key, along with other metadata (refer [More on `Model.structure`](@ref model_structure)) +of the symbolic variable. + +```julia +julia> ModelWithArrays.structure +Dict{Symbol, Any} with 5 entries: + :variables => Dict{Symbol, Dict{Symbol, Any}}(:v2 => Dict(:value => :([1, 2, 3]), :type => Real, :size => (3,)), :v1 => Dict(:value => :v1, :type => Real, :description => "An array of variable `v1`", :size => (2,))) + :kwargs => Dict{Symbol, Dict}(:p2 => Dict{Symbol, Any}(:value => nothing, :type => Real, :size => (:N,)), :v1 => Dict{Symbol, Any}(:value => :v1, :type => Real, :description => "An array of variable `v1`", :size => (2,)), :N => Dict(:value => 2), :M => Dict(:value => 3), :p4 => Dict{Symbol, Any}(:value => 10, :type => Real, :description => "An alternate syntax for p3 to match the syntax of vanilla parameters macro", :size => (:N, :M)), :v2 => Dict{Symbol, Any}(:value => :([1, 2, 3]), :type => Real, :size => (3,)), :p1 => Dict{Symbol, Any}(:value => nothing, :type => Real, :size => (4,)), :p3 => Dict{Symbol, Any}(:value => :p3, :type => Real, :description => "A multi-dimensional array of arbitrary length with description", :size => (:N, :M))) + :structural_parameters => Dict{Symbol, Dict}(:N => Dict(:value => 2), :M => Dict(:value => 3)) + :independent_variable => :t + :parameters => Dict{Symbol, Dict{Symbol, Any}}(:p2 => Dict(:value => nothing, :type => Real, :size => (:N,)), :p4 => Dict(:value => 10, :type => Real, :description => "An alternate syntax for p3 to match the syntax of vanilla parameters macro", :size => (:N, :M)), :p1 => Dict(:value => nothing, :type => Real, :size => (4,)), :p3 => Dict(:value => :p3, :type => Real, :description => "A multi-dimensional array of arbitrary length with description", :size => (:N, :M)))), false) +``` + ### Using conditional statements #### Conditional elements of the system From 7b9a234c7a850424f44b784e80b59744554588cf Mon Sep 17 00:00:00 2001 From: Venkateshprasad <32921645+ven-k@users.noreply.github.com> Date: Tue, 8 Oct 2024 18:15:49 +0000 Subject: [PATCH 0116/2176] feat: update the vartype of kwargs rel. to symbolic-array While here, removes passing `indices` around to generate_var, update_kwargs_and_metadata! and parse_variable_def! as these no longer handles symbolic-arrays. --- src/systems/model_parsing.jl | 84 ++++++++++++++++-------------------- 1 file changed, 36 insertions(+), 48 deletions(-) diff --git a/src/systems/model_parsing.jl b/src/systems/model_parsing.jl index 94469308e4..7e8eed9900 100644 --- a/src/systems/model_parsing.jl +++ b/src/systems/model_parsing.jl @@ -151,34 +151,18 @@ pop_structure_dict!(dict, key) = length(dict[key]) == 0 && pop!(dict, key) struct NoValue end const NO_VALUE = NoValue() -function update_kwargs_and_metadata!(dict, kwargs, a, def, indices, type, var, +function update_kwargs_and_metadata!(dict, kwargs, a, def, type, varclass, where_types, meta) - if indices isa Nothing - if !isnothing(meta) && haskey(meta, VariableUnit) - uvar = gensym() - push!(where_types, uvar) - push!(kwargs, - Expr(:kw, :($a::Union{Nothing, Missing, $NoValue, $uvar}), NO_VALUE)) - else - push!(kwargs, - Expr(:kw, :($a::Union{Nothing, Missing, $NoValue, $type}), NO_VALUE)) - end - dict[:kwargs][a] = Dict(:value => def, :type => type) + if !isnothing(meta) && haskey(meta, VariableUnit) + uvar = gensym() + push!(where_types, uvar) + push!(kwargs, + Expr(:kw, :($a::Union{Nothing, Missing, $NoValue, $uvar}), NO_VALUE)) else - vartype = gensym(:T) push!(kwargs, - Expr(:kw, - Expr(:(::), a, - Expr(:curly, :Union, :Nothing, :Missing, NoValue, - Expr(:curly, :AbstractArray, vartype))), - NO_VALUE)) - if !isnothing(meta) && haskey(meta, VariableUnit) - push!(where_types, vartype) - else - push!(where_types, :($vartype <: $type)) - end - dict[:kwargs][a] = Dict(:value => def, :type => AbstractArray{type}) + Expr(:kw, :($a::Union{Nothing, Missing, $NoValue, $type}), NO_VALUE)) end + dict[:kwargs][a] = Dict(:value => def, :type => type) if dict[varclass] isa Vector dict[varclass][1][a][:type] = AbstractArray{type} else @@ -213,8 +197,8 @@ function update_readable_metadata!(varclass_dict, meta::Dict, varname) end end -function push_array_kwargs_and_metadata!( - dict, indices, meta, type, varclass, varname, varval) +function update_array_kwargs_and_metadata!( + dict, indices, kwargs, meta, type, varclass, varname, varval, where_types) dict[varclass] = get!(dict, varclass) do Dict{Symbol, Dict{Symbol, Any}}() end @@ -227,6 +211,18 @@ function push_array_kwargs_and_metadata!( :type => type ))) + vartype = gensym(:T) + push!(kwargs, + Expr(:kw, + Expr(:(::), varname, + Expr(:curly, :Union, :Nothing, Expr(:curly, :AbstractArray, vartype))), + nothing)) + if !isnothing(meta) && haskey(meta, VariableUnit) + push!(where_types, vartype) + else + push!(where_types, :($vartype <: $type)) + end + # Useful keys for kwargs entry are: value, type and size. dict[:kwargs][varname] = varclass_dict[][varname] @@ -243,13 +239,12 @@ function unit_handled_variable_value(meta, varname) end function parse_variable_def!(dict, mod, arg, varclass, kwargs, where_types; - def = nothing, indices::Union{Vector{UnitRange{Int}}, Nothing} = nothing, - type::Type = Real, meta = Dict{DataType, Expr}()) + def = nothing, type::Type = Real, meta = Dict{DataType, Expr}()) arg isa LineNumberNode && return MLStyle.@match arg begin a::Symbol => begin - var = generate_var!(dict, a, varclass; indices, type) - update_kwargs_and_metadata!(dict, kwargs, a, def, indices, type, var, + var = generate_var!(dict, a, varclass; type) + update_kwargs_and_metadata!(dict, kwargs, a, def, type, varclass, where_types, meta) return var, def, Dict() end @@ -265,8 +260,8 @@ function parse_variable_def!(dict, mod, arg, varclass, kwargs, where_types; dict, mod, a, varclass, kwargs, where_types; def, type, meta) end Expr(:call, a, b) => begin - var = generate_var!(dict, a, b, varclass, mod; indices, type) - update_kwargs_and_metadata!(dict, kwargs, a, def, indices, type, var, + var = generate_var!(dict, a, b, varclass, mod; type) + update_kwargs_and_metadata!(dict, kwargs, a, def, type, varclass, where_types, meta) return var, def, Dict() end @@ -276,7 +271,6 @@ function parse_variable_def!(dict, mod, arg, varclass, kwargs, where_types; Expr(:tuple, Expr(:ref, a, indices...), meta_val) => begin (@isdefined type) || (type = Real) varname = Meta.isexpr(a, :call) ? a.args[1] : a - push!(kwargs, Expr(:kw, varname, nothing)) meta = parse_metadata(mod, meta_val) varval = (@isdefined default_val) ? default_val : unit_handled_variable_value(meta, varname) @@ -291,8 +285,8 @@ function parse_variable_def!(dict, mod, arg, varclass, kwargs, where_types; var = :($varname = $first(@variables ($a[$(indices)]::$type = $varval), $meta_val)) end - push_array_kwargs_and_metadata!( - dict, indices, meta, type, varclass, varname, varval) + update_array_kwargs_and_metadata!( + dict, indices, kwargs, meta, type, varclass, varname, varval, where_types) (:($varname...), var), nothing, Dict() end Expr(:(=), Expr(:(::), Expr(:ref, a, indices...), type), def_n_meta) || @@ -303,7 +297,6 @@ function parse_variable_def!(dict, mod, arg, varclass, kwargs, where_types; meta = parse_metadata(mod, def_n_meta) varval = unit_handled_variable_value(meta, varname) val, def_n_meta = (def_n_meta.args[1], def_n_meta.args[2:end]) - push!(kwargs, Expr(:kw, varname, nothing)) if varclass == :parameters Meta.isexpr(a, :call) && assert_unique_independent_var(dict, a.args[end]) @@ -320,7 +313,6 @@ function parse_variable_def!(dict, mod, arg, varclass, kwargs, where_types; $(def_n_meta...))) end else - push!(kwargs, Expr(:kw, varname, nothing)) if varclass == :parameters Meta.isexpr(a, :call) && assert_unique_independent_var(dict, a.args[end]) @@ -335,15 +327,14 @@ function parse_variable_def!(dict, mod, arg, varclass, kwargs, where_types; end varval, meta = def_n_meta, nothing end - push_array_kwargs_and_metadata!( - dict, indices, meta, type, varclass, varname, varval) + update_array_kwargs_and_metadata!( + dict, indices, kwargs, meta, type, varclass, varname, varval, where_types) (:($varname...), var), nothing, Dict() end Expr(:(::), Expr(:ref, a, indices...), type) || Expr(:ref, a, indices...) => begin (@isdefined type) || (type = Real) varname = a isa Expr && a.head == :call ? a.args[1] : a - push!(kwargs, Expr(:kw, varname, nothing)) if varclass == :parameters Meta.isexpr(a, :call) && assert_unique_independent_var(dict, a.args[end]) var = :($varname = $first(@parameters $a[$(indices...)]::$type = $varname)) @@ -356,8 +347,8 @@ function parse_variable_def!(dict, mod, arg, varclass, kwargs, where_types; throw("Symbolic array with arbitrary length is not handled for $varclass. Please open an issue with an example.") end - push_array_kwargs_and_metadata!( - dict, indices, nothing, type, varclass, varname, nothing) + update_array_kwargs_and_metadata!( + dict, indices, kwargs, nothing, type, varclass, varname, nothing, where_types) (:($varname...), var), nothing, Dict() end Expr(:(=), a, b) => begin @@ -392,11 +383,8 @@ function parse_variable_def!(dict, mod, arg, varclass, kwargs, where_types; end end -function generate_var(a, varclass; - indices::Union{Vector{UnitRange{Int}}, Nothing} = nothing, - type = Real) - var = indices === nothing ? Symbolics.variable(a; T = type) : - first(@variables $a[indices...]::type) +function generate_var(a, varclass; type = Real) + var = Symbolics.variable(a; T = type) if varclass == :parameters var = toparam(var) elseif varclass == :independent_variables @@ -426,7 +414,7 @@ function generate_var!(dict, a, varclass; vd isa Vector && (vd = first(vd)) vd[a] = Dict{Symbol, Any}() indices !== nothing && (vd[a][:size] = Tuple(lastindex.(indices))) - generate_var(a, varclass; indices, type) + generate_var(a, varclass; type) end function assert_unique_independent_var(dict, iv::Num) From da4429283b657e107fd631e3b1ba6346e6319032 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 11 Oct 2024 12:52:55 +0530 Subject: [PATCH 0117/2176] feat: better discover scoped parameters in parent systems --- src/systems/abstractsystem.jl | 17 +++++++++ src/systems/diffeqs/odesystem.jl | 15 +++++--- src/utils.jl | 61 +++++++++++++++++++++++++++++--- test/variable_scope.jl | 38 ++++++++++++++++++++ 4 files changed, 121 insertions(+), 10 deletions(-) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index 9908d6f1e3..00a003e06d 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -917,6 +917,15 @@ Mark a system as completed. If a system is complete, the system will no longer namespace its subsystems or variables, i.e. `isequal(complete(sys).v.i, v.i)`. """ function complete(sys::AbstractSystem; split = true) + if !(sys isa JumpSystem) + newunknowns = OrderedSet() + newparams = OrderedSet() + iv = has_iv(sys) ? get_iv(sys) : nothing + collect_scoped_vars!(newunknowns, newparams, sys, iv; depth = -1) + # don't update unknowns to not disturb `structural_simplify` order + # `GlobalScope`d unknowns will be picked up and added there + @set! sys.ps = unique!(vcat(get_ps(sys), collect(newparams))) + end if split && has_index_cache(sys) @set! sys.index_cache = IndexCache(sys) all_ps = parameters(sys) @@ -3011,6 +3020,14 @@ function compose(sys::AbstractSystem, systems::AbstractArray; name = nameof(sys) if has_is_dde(sys) @set! sys.is_dde = _check_if_dde(equations(sys), get_iv(sys), get_systems(sys)) end + newunknowns = OrderedSet() + newparams = OrderedSet() + iv = has_iv(sys) ? get_iv(sys) : nothing + for ssys in systems + collect_scoped_vars!(newunknowns, newparams, ssys, iv) + end + @set! sys.unknowns = unique!(vcat(get_unknowns(sys), collect(newunknowns))) + @set! sys.ps = unique!(vcat(get_ps(sys), collect(newparams))) return sys end function compose(syss...; name = nameof(first(syss))) diff --git a/src/systems/diffeqs/odesystem.jl b/src/systems/diffeqs/odesystem.jl index bda5690a67..68ea0b48e3 100644 --- a/src/systems/diffeqs/odesystem.jl +++ b/src/systems/diffeqs/odesystem.jl @@ -323,11 +323,13 @@ function ODESystem(eqs, iv; kwargs...) collect_vars!(allunknowns, ps, eq.rhs, iv) if isdiffeq(eq) diffvar, _ = var_from_nested_derivative(eq.lhs) - isequal(iv, iv_from_nested_derivative(eq.lhs)) || - throw(ArgumentError("An ODESystem can only have one independent variable.")) - diffvar in diffvars && - throw(ArgumentError("The differential variable $diffvar is not unique in the system of equations.")) - push!(diffvars, diffvar) + if check_scope_depth(getmetadata(diffvar, SymScope, LocalScope()), 0) + isequal(iv, iv_from_nested_derivative(eq.lhs)) || + throw(ArgumentError("An ODESystem can only have one independent variable.")) + diffvar in diffvars && + throw(ArgumentError("The differential variable $diffvar is not unique in the system of equations.")) + push!(diffvars, diffvar) + end push!(diffeq, eq) else push!(algeeq, eq) @@ -342,6 +344,9 @@ function ODESystem(eqs, iv; kwargs...) collect_vars!(allunknowns, ps, eq.rhs, iv) end end + for ssys in get(kwargs, :systems, ODESystem[]) + collect_scoped_vars!(allunknowns, ps, ssys, iv) + end for v in allunknowns isdelay(v, iv) || continue collect_vars!(allunknowns, ps, arguments(v)[1], iv) diff --git a/src/utils.jl b/src/utils.jl index 29e61ede69..3e347bd405 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -467,23 +467,56 @@ function find_derivatives!(vars, expr, f) return vars end -function collect_vars!(unknowns, parameters, expr, iv; op = Differential) +""" + $(TYPEDSIGNATURES) + +Search through equations and parameter dependencies of `sys`, where sys is at a depth of +`depth` from the root system, looking for variables scoped to the root system. Also +recursively searches through all subsystems of `sys`, increasing the depth if it is not +`-1`. A depth of `-1` indicates searching for variables with `GlobalScope`. +""" +function collect_scoped_vars!(unknowns, parameters, sys, iv; depth = 1, op = Differential) + if has_eqs(sys) + for eq in get_eqs(sys) + eq.lhs isa Union{Symbolic, Number} || continue + collect_vars!(unknowns, parameters, eq.lhs, iv; depth, op) + collect_vars!(unknowns, parameters, eq.rhs, iv; depth, op) + end + end + if has_parameter_dependencies(sys) + for eq in get_parameter_dependencies(sys) + if eq isa Pair + collect_vars!(unknowns, parameters, eq[1], iv; depth, op) + collect_vars!(unknowns, parameters, eq[2], iv; depth, op) + else + collect_vars!(unknowns, parameters, eq.lhs, iv; depth, op) + collect_vars!(unknowns, parameters, eq.rhs, iv; depth, op) + end + end + end + newdepth = depth == -1 ? depth : depth + 1 + for ssys in get_systems(sys) + collect_scoped_vars!(unknowns, parameters, ssys, iv; depth = newdepth, op) + end +end + +function collect_vars!(unknowns, parameters, expr, iv; depth = 0, op = Differential) if issym(expr) - collect_var!(unknowns, parameters, expr, iv) + collect_var!(unknowns, parameters, expr, iv; depth) else for var in vars(expr; op) if iscall(var) && operation(var) isa Differential var, _ = var_from_nested_derivative(var) end - collect_var!(unknowns, parameters, var, iv) + collect_var!(unknowns, parameters, var, iv; depth) end end return nothing end -function collect_var!(unknowns, parameters, var, iv) +function collect_var!(unknowns, parameters, var, iv; depth = 0) isequal(var, iv) && return nothing - getmetadata(var, SymScope, LocalScope()) == LocalScope() || return nothing + check_scope_depth(getmetadata(var, SymScope, LocalScope()), depth) || return nothing if iscalledparameter(var) callable = getcalledparameter(var) push!(parameters, callable) @@ -500,6 +533,24 @@ function collect_var!(unknowns, parameters, var, iv) return nothing end +""" + $(TYPEDSIGNATURES) + +Check if the given `scope` is at a depth of `depth` from the root system. Only +returns `true` for `scope::GlobalScope` if `depth == -1`. +""" +function check_scope_depth(scope, depth) + if scope isa LocalScope + return depth == 0 + elseif scope isa ParentScope + return depth > 0 && check_scope_depth(scope.parent, depth - 1) + elseif scope isa DelayParentScope + return depth >= scope.N && check_scope_depth(scope.parent, depth - scope.N) + elseif scope isa GlobalScope + return depth == -1 + end +end + """ Find all the symbolic constants of some equations or terms and return them as a vector. """ diff --git a/test/variable_scope.jl b/test/variable_scope.jl index 853d5ce757..e90f7e6fbf 100644 --- a/test/variable_scope.jl +++ b/test/variable_scope.jl @@ -101,3 +101,41 @@ bar = complete(bar) defs = ModelingToolkit.defaults(bar) @test defs[bar.p] == 2 @test isequal(defs[bar.foo.p], bar.p) + +# Issue#3101 +@variables x1(t) x2(t) x3(t) x4(t) x5(t) +x2 = ParentScope(x2) +x3 = ParentScope(ParentScope(x3)) +x4 = DelayParentScope(x4, 2) +x5 = GlobalScope(x5) +@parameters p1 p2 p3 p4 p5 +p2 = ParentScope(p2) +p3 = ParentScope(ParentScope(p3)) +p4 = DelayParentScope(p4, 2) +p5 = GlobalScope(p5) + +@named sys1 = ODESystem([D(x1) ~ p1, D(x2) ~ p2, D(x3) ~ p3, D(x4) ~ p4, D(x5) ~ p5], t) +@test isequal(x1, only(unknowns(sys1))) +@test isequal(p1, only(parameters(sys1))) +@named sys2 = ODESystem(Equation[], t; systems = [sys1]) +@test length(unknowns(sys2)) == 2 +@test any(isequal(x2), unknowns(sys2)) +@test length(parameters(sys2)) == 2 +@test any(isequal(p2), parameters(sys2)) +@named sys3 = ODESystem(Equation[], t) +sys3 = sys3 ∘ sys2 +@test length(unknowns(sys3)) == 4 +@test any(isequal(x3), unknowns(sys3)) +@test any(isequal(x4), unknowns(sys3)) +@test length(parameters(sys3)) == 4 +@test any(isequal(p3), parameters(sys3)) +@test any(isequal(p4), parameters(sys3)) +sys4 = complete(sys3) +@test length(unknowns(sys3)) == 4 +@test length(parameters(sys4)) == 5 +@test any(isequal(p5), parameters(sys4)) +sys5 = structural_simplify(sys3) +@test length(unknowns(sys5)) == 5 +@test any(isequal(x5), unknowns(sys5)) +@test length(parameters(sys5)) == 5 +@test any(isequal(p5), parameters(sys5)) From 1290985bfdce26b2c8010e45314a1cf110fde273 Mon Sep 17 00:00:00 2001 From: Venkateshprasad <32921645+ven-k@users.noreply.github.com> Date: Fri, 11 Oct 2024 09:04:51 +0000 Subject: [PATCH 0118/2176] refactor: set default value of the symbolic-array-kwargs to NO_VALUE --- src/systems/model_parsing.jl | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/src/systems/model_parsing.jl b/src/systems/model_parsing.jl index 7e8eed9900..bf70d06b79 100644 --- a/src/systems/model_parsing.jl +++ b/src/systems/model_parsing.jl @@ -215,8 +215,9 @@ function update_array_kwargs_and_metadata!( push!(kwargs, Expr(:kw, Expr(:(::), varname, - Expr(:curly, :Union, :Nothing, Expr(:curly, :AbstractArray, vartype))), - nothing)) + Expr(:curly, :Union, :Nothing, :Missing, NoValue, + Expr(:curly, :AbstractArray, vartype))), + NO_VALUE)) if !isnothing(meta) && haskey(meta, VariableUnit) push!(where_types, vartype) else @@ -300,14 +301,14 @@ function parse_variable_def!(dict, mod, arg, varclass, kwargs, where_types; if varclass == :parameters Meta.isexpr(a, :call) && assert_unique_independent_var(dict, a.args[end]) - var = :($varname = $varname === nothing ? $val : $varname; + var = :($varname = $varname === $NO_VALUE ? $val : $varname; $varname = $first(@parameters ($a[$(indices...)]::$type = $varval), $(def_n_meta...))) else Meta.isexpr(a, :call) || throw("$a is not a variable of the independent variable") assert_unique_independent_var(dict, a.args[end]) - var = :($varname = $varname === nothing ? $val : $varname; + var = :($varname = $varname === $NO_VALUE ? $val : $varname; $varname = $first(@variables $a[$(indices...)]::$type = ( $varval), $(def_n_meta...))) @@ -316,13 +317,13 @@ function parse_variable_def!(dict, mod, arg, varclass, kwargs, where_types; if varclass == :parameters Meta.isexpr(a, :call) && assert_unique_independent_var(dict, a.args[end]) - var = :($varname = $varname === nothing ? $def_n_meta : $varname; + var = :($varname = $varname === $NO_VALUE ? $def_n_meta : $varname; $varname = $first(@parameters $a[$(indices...)]::$type = $varname)) else Meta.isexpr(a, :call) || throw("$a is not a variable of the independent variable") assert_unique_independent_var(dict, a.args[end]) - var = :($varname = $varname === nothing ? $def_n_meta : $varname; + var = :($varname = $varname === $NO_VALUE ? $def_n_meta : $varname; $varname = $first(@variables $a[$(indices...)]::$type = $varname)) end varval, meta = def_n_meta, nothing @@ -765,11 +766,12 @@ function parse_variable_arg!(exprs, vs, dict, mod, arg, varclass, kwargs, where_ end function convert_units(varunits::DynamicQuantities.Quantity, value) - value isa Nothing && return nothing DynamicQuantities.ustrip(DynamicQuantities.uconvert( DynamicQuantities.SymbolicUnits.as_quantity(varunits), value)) end +convert_units(::DynamicQuantities.Quantity, value::NoValue) = NO_VALUE + function convert_units( varunits::DynamicQuantities.Quantity, value::AbstractArray{T}) where {T} DynamicQuantities.ustrip.(DynamicQuantities.uconvert.( @@ -777,21 +779,18 @@ function convert_units( end function convert_units(varunits::Unitful.FreeUnits, value) - value isa Nothing && return nothing Unitful.ustrip(varunits, value) end +convert_units(::Unitful.FreeUnits, value::NoValue) = NO_VALUE + function convert_units(varunits::Unitful.FreeUnits, value::AbstractArray{T}) where {T} Unitful.ustrip.(varunits, value) end -function convert_units(varunits::Unitful.FreeUnits, value::Num) - value -end +convert_units(::Unitful.FreeUnits, value::Num) = value -function convert_units(varunits::DynamicQuantities.Quantity, value::Num) - value -end +convert_units(::DynamicQuantities.Quantity, value::Num) = value function parse_variable_arg(dict, mod, arg, varclass, kwargs, where_types) vv, def, metadata_with_exprs = parse_variable_def!( From b83a251a33434dd2ccdd6aea73a69959119720b1 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 11 Oct 2024 14:39:56 +0530 Subject: [PATCH 0119/2176] fix: make `vars` search through arrays of symbolics --- src/utils.jl | 6 ++++++ test/variable_utils.jl | 10 +++++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/utils.jl b/src/utils.jl index 29e61ede69..71b7bc3cd4 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -385,6 +385,12 @@ function vars!(vars, O; op = Differential) if isvariable(O) return push!(vars, O) end + if symbolic_type(O) == NotSymbolic() && O isa AbstractArray + for arg in O + vars!(vars, arg; op) + end + return vars + end !iscall(O) && return vars operation(O) isa op && return push!(vars, O) diff --git a/test/variable_utils.jl b/test/variable_utils.jl index 8f3178f453..d76f2f1209 100644 --- a/test/variable_utils.jl +++ b/test/variable_utils.jl @@ -1,5 +1,5 @@ using ModelingToolkit, Test -using ModelingToolkit: value +using ModelingToolkit: value, vars using SymbolicUtils: <ₑ @parameters α β δ expr = (((1 / β - 1) + δ) / α)^(1 / (α - 1)) @@ -33,3 +33,11 @@ aov = ModelingToolkit.collect_applied_operators(eq, Differential) ts = collect_ivs([eq]) @test ts == Set([t]) + +@testset "vars searching through array of symbolics" begin + fn(x, y) = sum(x) + y + @register_symbolic fn(x::AbstractArray, y) + @variables x y z + res = vars(fn([x, y], z)) + @test length(res) == 3 +end From 5d7d97daf58ca4d7beeb3ba496e4c492b9a6f8b4 Mon Sep 17 00:00:00 2001 From: Christopher Rackauckas Date: Fri, 11 Oct 2024 08:05:40 -0400 Subject: [PATCH 0120/2176] Update abstractsystem.jl --- src/systems/abstractsystem.jl | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index 00a003e06d..efec8b7fd8 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -913,7 +913,12 @@ end """ $(TYPEDSIGNATURES) -Mark a system as completed. If a system is complete, the system will no longer +Mark a system as completed. A completed system is a system which is done being +defined/modified and is ready for structural analysis or other transformations. +This allows for analyses and optimizations to be performed which require knowing +the global structure of the system. + +One property to note is that if a system is complete, the system will no longer namespace its subsystems or variables, i.e. `isequal(complete(sys).v.i, v.i)`. """ function complete(sys::AbstractSystem; split = true) From 09de19020c99b92d51ba577d13b4bae6b3202d1c Mon Sep 17 00:00:00 2001 From: Venkateshprasad <32921645+ven-k@users.noreply.github.com> Date: Fri, 11 Oct 2024 14:44:05 +0000 Subject: [PATCH 0121/2176] test: for vanilla-`@variable` syntax with arbitrary length --- test/model_parsing.jl | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/test/model_parsing.jl b/test/model_parsing.jl index c108b88625..3e2bd2045c 100644 --- a/test/model_parsing.jl +++ b/test/model_parsing.jl @@ -281,17 +281,34 @@ end @testset "Arrays using vanilla-@variable syntax" begin @mtkmodel TupleInArrayDef begin + @structural_parameters begin + N + M + end @parameters begin (l(t)[1:2, 1:3] = 1), [description = "l is more than 1D"] - (l2(t)[1:3] = 2), [description = "l2 is 1D"] - (l3(t)[1:3]::Int = 3), [description = "l3 is 1D and has a type"] + (l2(t)[1:N, 1:M] = 2), + [description = "l is more than 1D, with arbitrary length"] + (l3(t)[1:3] = 3), [description = "l2 is 1D"] + (l4(t)[1:N] = 4), [description = "l2 is 1D, with arbitrary length"] + (l5(t)[1:3]::Int = 5), [description = "l3 is 1D and has a type"] + (l6(t)[1:N]::Int = 6), + [description = "l3 is 1D and has a type, with arbitrary length"] end end - @named arr = TupleInArrayDef() + N, M = 4, 5 + @named arr = TupleInArrayDef(; N, M) @test getdefault(arr.l) == 1 @test getdefault(arr.l2) == 2 @test getdefault(arr.l3) == 3 + @test getdefault(arr.l4) == 4 + @test getdefault(arr.l5) == 5 + @test getdefault(arr.l6) == 6 + + @test size(arr.l2) == (N, M) + @test size(arr.l4) == (N,) + @test size(arr.l6) == (N,) end @testset "Type annotation" begin From 0d4c97d6a015fa76c1e80bda64a21bd1ab6a9974 Mon Sep 17 00:00:00 2001 From: Venkateshprasad <32921645+ven-k@users.noreply.github.com> Date: Fri, 11 Oct 2024 16:10:46 +0000 Subject: [PATCH 0122/2176] fix: remove an unused parsing block This is handled in two steps and this block is never used --- src/systems/model_parsing.jl | 6 ------ 1 file changed, 6 deletions(-) diff --git a/src/systems/model_parsing.jl b/src/systems/model_parsing.jl index bf70d06b79..51b7c6eb69 100644 --- a/src/systems/model_parsing.jl +++ b/src/systems/model_parsing.jl @@ -254,12 +254,6 @@ function parse_variable_def!(dict, mod, arg, varclass, kwargs, where_types; parse_variable_def!( dict, mod, a, varclass, kwargs, where_types; def, type, meta) end - Expr(:(::), Expr(:call, a, b), type) => begin - type = getfield(mod, type) - def = _type_check!(def, a, type, varclass) - parse_variable_def!( - dict, mod, a, varclass, kwargs, where_types; def, type, meta) - end Expr(:call, a, b) => begin var = generate_var!(dict, a, b, varclass, mod; type) update_kwargs_and_metadata!(dict, kwargs, a, def, type, From 4c0b1edee4f4cae0b05eec9f206e491f372c6955 Mon Sep 17 00:00:00 2001 From: Venkateshprasad <32921645+ven-k@users.noreply.github.com> Date: Fri, 11 Oct 2024 16:12:24 +0000 Subject: [PATCH 0123/2176] docs: document the syntax parsed by a particular block --- src/systems/model_parsing.jl | 62 ++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/src/systems/model_parsing.jl b/src/systems/model_parsing.jl index 51b7c6eb69..1298d72506 100644 --- a/src/systems/model_parsing.jl +++ b/src/systems/model_parsing.jl @@ -239,27 +239,65 @@ function unit_handled_variable_value(meta, varname) return varval end +# This function parses various variable/parameter definitions. +# +# The comments indicate the syntax matched by a block; either when parsed directly +# when it is called recursively for parsing a part of an expression. +# These variable definitions are part of test suite in `test/model_parsing.jl` function parse_variable_def!(dict, mod, arg, varclass, kwargs, where_types; def = nothing, type::Type = Real, meta = Dict{DataType, Expr}()) arg isa LineNumberNode && return MLStyle.@match arg begin + # Parses: `a` + # Recursively called by: `c(t) = cval + jval` + # Recursively called by: `d = 2` + # Recursively called by: `e, [description = "e"]` + # Recursively called by: `f = 3, [description = "f"]` + # Recursively called by: `k = kval, [description = "k"]` + # Recursively called by: `par0::Bool = true` a::Symbol => begin var = generate_var!(dict, a, varclass; type) update_kwargs_and_metadata!(dict, kwargs, a, def, type, varclass, where_types, meta) return var, def, Dict() end + # Parses: `par5[1:3]::BigFloat` + # Parses: `par6(t)[1:3]::BigFloat` + # Recursively called by: `par2(t)::Int` + # Recursively called by: `par3(t)::BigFloat = 1.0` Expr(:(::), a, type) => begin type = getfield(mod, type) parse_variable_def!( dict, mod, a, varclass, kwargs, where_types; def, type, meta) end + # Recursively called by: `i(t) = 4, [description = "i(t)"]` + # Recursively called by: `h(t), [description = "h(t)"]` + # Recursively called by: `j(t) = jval, [description = "j(t)"]` + # Recursively called by: `par2(t)::Int` + # Recursively called by: `par3(t)::BigFloat = 1.0` Expr(:call, a, b) => begin var = generate_var!(dict, a, b, varclass, mod; type) update_kwargs_and_metadata!(dict, kwargs, a, def, type, varclass, where_types, meta) return var, def, Dict() end + # Condition 1 parses: + # `(l(t)[1:2, 1:3] = 1), [description = "l is more than 1D"]` + # `(l2(t)[1:N, 1:M] = 2), [description = "l is more than 1D, with arbitrary length"]` + # `(l3(t)[1:3] = 3), [description = "l2 is 1D"]` + # `(l4(t)[1:N] = 4), [description = "l2 is 1D, with arbitrary length"]` + # + # Condition 2 parses: + # `(l5(t)[1:3]::Int = 5), [description = "l3 is 1D and has a type"]` + # `(l6(t)[1:N]::Int = 6), [description = "l3 is 1D and has a type, with arbitrary length"]` + # + # Condition 3 parses: + # `e2[1:2]::Int, [description = "e2"]` + # `h2(t)[1:2]::Int, [description = "h2(t)"]` + # + # Condition 4 parses: + # `e2[1:2], [description = "e2"]` + # `h2(t)[1:2], [description = "h2(t)"]` Expr(:tuple, Expr(:(=), Expr(:ref, a, indices...), default_val), meta_val) || Expr(:tuple, Expr(:(=), Expr(:(::), Expr(:ref, a, indices...), type), default_val), meta_val) || Expr(:tuple, Expr(:(::), Expr(:ref, a, indices...), type), meta_val) || @@ -284,6 +322,12 @@ function parse_variable_def!(dict, mod, arg, varclass, kwargs, where_types; dict, indices, kwargs, meta, type, varclass, varname, varval, where_types) (:($varname...), var), nothing, Dict() end + # Condition 1 parses: + # `par7(t)[1:3, 1:3]::BigFloat = 1.0, [description = "with description"]` + # + # Condition 2 parses: + # `d2[1:2] = 2` + # `l(t)[1:2, 1:3] = 2, [description = "l is more than 1D"]` Expr(:(=), Expr(:(::), Expr(:ref, a, indices...), type), def_n_meta) || Expr(:(=), Expr(:ref, a, indices...), def_n_meta) => begin (@isdefined type) || (type = Real) @@ -326,6 +370,13 @@ function parse_variable_def!(dict, mod, arg, varclass, kwargs, where_types; dict, indices, kwargs, meta, type, varclass, varname, varval, where_types) (:($varname...), var), nothing, Dict() end + # Condition 1 is recursively called by: + # `par5[1:3]::BigFloat` + # `par6(t)[1:3]::BigFloat` + # + # Condition 2 parses: + # `b2(t)[1:2]` + # `a2[1:2]` Expr(:(::), Expr(:ref, a, indices...), type) || Expr(:ref, a, indices...) => begin (@isdefined type) || (type = Real) @@ -346,6 +397,14 @@ function parse_variable_def!(dict, mod, arg, varclass, kwargs, where_types; dict, indices, kwargs, nothing, type, varclass, varname, nothing, where_types) (:($varname...), var), nothing, Dict() end + # Parses: `c(t) = cval + jval` + # Parses: `d = 2` + # Parses: `f = 3, [description = "f"]` + # Parses: `i(t) = 4, [description = "i(t)"]` + # Parses: `j(t) = jval, [description = "j(t)"]` + # Parses: `k = kval, [description = "k"]` + # Parses: `par0::Bool = true` + # Parses: `par3(t)::BigFloat = 1.0` Expr(:(=), a, b) => begin Base.remove_linenums!(b) def, meta = parse_default(mod, b) @@ -361,6 +420,9 @@ function parse_variable_def!(dict, mod, arg, varclass, kwargs, where_types; end return var, def, Dict() end + # Parses: `e, [description = "e"]` + # Parses: `h(t), [description = "h(t)"]` + # Parses: `par2(t)::Int` Expr(:tuple, a, b) => begin meta = parse_metadata(mod, b) var, def, _ = parse_variable_def!( From 31efe04cd025c5a0a67ae7331c2f8964407428e2 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 8 Oct 2024 11:33:57 +0530 Subject: [PATCH 0124/2176] fix: handle unknowns inside callable parameters when topsorting equations --- src/utils.jl | 8 ++++++++ test/structural_transformation/utils.jl | 8 ++++++++ 2 files changed, 16 insertions(+) diff --git a/src/utils.jl b/src/utils.jl index dadf31ebcc..d38ab822d4 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -383,6 +383,14 @@ function vars!(vars, eq::Equation; op = Differential) end function vars!(vars, O; op = Differential) if isvariable(O) + if iscalledparameter(O) + f = getcalledparameter(O) + push!(vars, f) + for arg in arguments(O) + vars!(vars, arg; op) + end + return vars + end return push!(vars, O) end if symbolic_type(O) == NotSymbolic() && O isa AbstractArray diff --git a/test/structural_transformation/utils.jl b/test/structural_transformation/utils.jl index 6d4a5a7506..8644d96945 100644 --- a/test/structural_transformation/utils.jl +++ b/test/structural_transformation/utils.jl @@ -32,3 +32,11 @@ se = collect(StructuralTransformations.edges(graph)) @test se == mapreduce(vcat, enumerate(graph.fadjlist)) do (s, d) StructuralTransformations.BipartiteEdge.(s, d) end + +@testset "observed2graph handles unknowns inside callable parameters" begin + @variables x(t) y(t) + @parameters p(..) + g, _ = ModelingToolkit.observed2graph([y ~ p(x), x ~ 0], [y, x]) + @test ModelingToolkit.𝑠neighbors(g, 1) == [2] + @test ModelingToolkit.𝑑neighbors(g, 2) == [1] +end From c3deaf96c6d2e4e5c4529ebdc4fc0258ad64fae6 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Sat, 12 Oct 2024 17:42:49 +0530 Subject: [PATCH 0125/2176] build: bump Symbolics compat --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 23ae9f8d13..5025ce3017 100644 --- a/Project.toml +++ b/Project.toml @@ -126,7 +126,7 @@ SpecialFunctions = "0.7, 0.8, 0.9, 0.10, 1.0, 2" StaticArrays = "0.10, 0.11, 0.12, 1.0" SymbolicIndexingInterface = "0.3.31" SymbolicUtils = "3.7" -Symbolics = "6.12" +Symbolics = "6.14" URIs = "1" UnPack = "0.1, 1.0" Unitful = "1.1" From 84ae858e33da996622f28dfcdda852e1910df81b Mon Sep 17 00:00:00 2001 From: Christopher Rackauckas Date: Sat, 12 Oct 2024 10:16:54 -0400 Subject: [PATCH 0126/2176] Update Project.toml --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 5025ce3017..0c152b1d6f 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "ModelingToolkit" uuid = "961ee093-0014-501f-94e3-6117800e7a78" authors = ["Yingbo Ma ", "Chris Rackauckas and contributors"] -version = "9.45.0" +version = "9.46.0" [deps] AbstractTrees = "1520ce14-60c1-5f80-bbc7-55ef81b5835c" From 375a78d4339e58fe879e446c9e0274feca3e86c8 Mon Sep 17 00:00:00 2001 From: ArnoStrouwen Date: Sun, 29 Sep 2024 19:08:07 +0200 Subject: [PATCH 0127/2176] Rewrite of SDE tutorial --- docs/src/tutorials/stochastic_diffeq.md | 87 ++++++++++++++++++------- 1 file changed, 64 insertions(+), 23 deletions(-) diff --git a/docs/src/tutorials/stochastic_diffeq.md b/docs/src/tutorials/stochastic_diffeq.md index fd03d51810..c53c74b8b3 100644 --- a/docs/src/tutorials/stochastic_diffeq.md +++ b/docs/src/tutorials/stochastic_diffeq.md @@ -1,38 +1,79 @@ # Modeling with Stochasticity -All models with `ODESystem` are deterministic. `SDESystem` adds another element -to the model: randomness. This is a +All previous differential equations tutorials deal with deterministic `ODESystem`s. +In this tutorial, we add randomness. +In particular, we show how to represent a [stochastic differential equation](https://en.wikipedia.org/wiki/Stochastic_differential_equation) -which has a deterministic (drift) component and a stochastic (diffusion) -component. Let's take the Lorenz equation from the first tutorial and extend -it to have multiplicative noise by creating `@brownian` variables in the equations. +as a `SDESystem`. + +!!! note + + The high level `@mtkmodel` macro used in the + [getting started tutorial](@ref getting_started) + is not yet compatible with `SDESystem`. + We thus have to use a lower level interface to define stochastic differential equations. + For an introduction to this interface, read the + [programmatically generating ODESystems tutorial](@ref programmatically). + +Let's take the Lorenz equation and add noise to each of the states. +To show the flexibility of ModelingToolkit, +we do not use homogeneous noise, with constant variance, +but instead use heterogeneous noise, +where the magnitude of the noise scales with (0.1 times) the magnitude of each of the states: + +```math +\begin{aligned} +dx &= (\sigma (y-x))dt &+ 0.1xdB \\ +dy &= (x(\rho-z) - y)dt &+ 0.1ydB \\ +dz &= (xy - \beta z)dt &+ 0.1zdB \\ +\end{aligned} +``` + +Where $B$, is standard Brownian motion, also called the +[Wiener process](https://en.wikipedia.org/wiki/Wiener_process). +In ModelingToolkit this can be done by `@brownian` variables. ```@example SDE using ModelingToolkit, StochasticDiffEq using ModelingToolkit: t_nounits as t, D_nounits as D +using Plots -# Define some variables -@parameters σ ρ β -@variables x(t) y(t) z(t) -@brownian a -eqs = [D(x) ~ σ * (y - x) + 0.1a * x, - D(y) ~ x * (ρ - z) - y + 0.1a * y, - D(z) ~ x * y - β * z + 0.1a * z] +@parameters σ=10.0 ρ=2.33 β=26.0 +@variables x(t)=5.0 y(t)=5.0 z(t)=1.0 +@brownian B +eqs = [D(x) ~ σ * (y - x) + 0.1B * x, + D(y) ~ x * (ρ - z) - y + 0.1B * y, + D(z) ~ x * y - β * z + 0.1B * z] @mtkbuild de = System(eqs, t) +``` + +Even though we did not explicitly use `SDESystem`, ModelingToolkit can still infer this from the equations. -u0map = [ - x => 1.0, - y => 0.0, - z => 0.0 -] +```@example SDE +typeof(de) +``` -parammap = [ - σ => 10.0, - β => 26.0, - ρ => 2.33 -] +We continue by solving and plotting the SDE. -prob = SDEProblem(de, u0map, (0.0, 100.0), parammap) +```@example SDE +prob = SDEProblem(de, [], (0.0, 100.0), []) sol = solve(prob, LambaEulerHeun()) +plot(sol, idxs = [(1, 2, 3)]) +``` + +The noise present in all 3 equations is correlated, as can be seen on the below figure. +If you want uncorrelated noise for each equation, +multiple `@brownian` variables have to be declared. + +```@example SDE +@brownian Bx By Bz +``` + +The figure also shows the multiplicative nature of the noise. +Because states `x` and `y` generally take on larger values, +the noise also takes on a more pronounced effect on these states compared to the state `z`. + +```@example SDE +plot(sol) ``` From b144e15611ee5a354b1d1c43122a6a9f33b614ee Mon Sep 17 00:00:00 2001 From: Christopher Rackauckas Date: Sat, 12 Oct 2024 11:28:42 -0400 Subject: [PATCH 0128/2176] Update Project.toml --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 73c2fa4c8a..381354b493 100644 --- a/Project.toml +++ b/Project.toml @@ -72,7 +72,7 @@ MTKLabelledArraysExt = "LabelledArrays" [compat] AbstractTrees = "0.3, 0.4" ArrayInterface = "6, 7" -BifurcationKit = "0.3, 0.4" +BifurcationKit = "0.4" BlockArrays = "1.1" ChainRulesCore = "1" Combinatorics = "1" From 87cbf0e72592fc91331a004dfa12deab1c0f5f79 Mon Sep 17 00:00:00 2001 From: ArnoStrouwen Date: Sat, 12 Oct 2024 17:49:12 +0200 Subject: [PATCH 0129/2176] Finish rewrite of SDE tutorial --- docs/src/tutorials/stochastic_diffeq.md | 45 ++++++++++++++++--------- 1 file changed, 30 insertions(+), 15 deletions(-) diff --git a/docs/src/tutorials/stochastic_diffeq.md b/docs/src/tutorials/stochastic_diffeq.md index c53c74b8b3..a6543dc977 100644 --- a/docs/src/tutorials/stochastic_diffeq.md +++ b/docs/src/tutorials/stochastic_diffeq.md @@ -19,19 +19,27 @@ Let's take the Lorenz equation and add noise to each of the states. To show the flexibility of ModelingToolkit, we do not use homogeneous noise, with constant variance, but instead use heterogeneous noise, -where the magnitude of the noise scales with (0.1 times) the magnitude of each of the states: +where the magnitude of the noise scales with (0.3 times) the magnitude of each of the states: ```math \begin{aligned} -dx &= (\sigma (y-x))dt &+ 0.1xdB \\ -dy &= (x(\rho-z) - y)dt &+ 0.1ydB \\ -dz &= (xy - \beta z)dt &+ 0.1zdB \\ +\frac{dx}{dt} &= (\sigma (y-x)) &+ 0.1x\frac{dB}{dt} \\ +\frac{dy}{dt} &= (x(\rho-z) - y) &+ 0.1y\frac{dB}{dt} \\ +\frac{dz}{dt} &= (xy - \beta z) &+ 0.1z\frac{dB}{dt} \\ \end{aligned} ``` Where $B$, is standard Brownian motion, also called the [Wiener process](https://en.wikipedia.org/wiki/Wiener_process). -In ModelingToolkit this can be done by `@brownian` variables. +We use notation similar to the +[Langevin equation](https://en.wikipedia.org/wiki/Stochastic_differential_equation#Use_in_physics), +often used in physics. +By "multiplying" the equations by $dt$, the notation used in +[probability theory](https://en.wikipedia.org/wiki/Stochastic_differential_equation#Use_in_probability_and_mathematical_finance) +can be recovered. + +We use this Langevin-like notation because it allows us to extend MTK modeling capacity from ODEs to SDEs, +using only a single new concept, `@brownian` variables, which represent $\frac{dB}{dt}$ in the above equation. ```@example SDE using ModelingToolkit, StochasticDiffEq @@ -41,9 +49,9 @@ using Plots @parameters σ=10.0 ρ=2.33 β=26.0 @variables x(t)=5.0 y(t)=5.0 z(t)=1.0 @brownian B -eqs = [D(x) ~ σ * (y - x) + 0.1B * x, - D(y) ~ x * (ρ - z) - y + 0.1B * y, - D(z) ~ x * y - β * z + 0.1B * z] +eqs = [D(x) ~ σ * (y - x) + 0.3x * B, + D(y) ~ x * (ρ - z) - y + 0.3y * B, + D(z) ~ x * y - β * z + 0.3z * B] @mtkbuild de = System(eqs, t) ``` @@ -58,22 +66,29 @@ We continue by solving and plotting the SDE. ```@example SDE prob = SDEProblem(de, [], (0.0, 100.0), []) -sol = solve(prob, LambaEulerHeun()) +sol = solve(prob, SRIW1()) plot(sol, idxs = [(1, 2, 3)]) ``` The noise present in all 3 equations is correlated, as can be seen on the below figure. -If you want uncorrelated noise for each equation, -multiple `@brownian` variables have to be declared. +The figure also shows the multiplicative nature of the noise. +Because states `x` and `y` generally take on larger values, +the noise also takes on a more pronounced effect on these states compared to the state `z`. ```@example SDE -@brownian Bx By Bz +plot(sol) ``` -The figure also shows the multiplicative nature of the noise. -Because states `x` and `y` generally take on larger values, -the noise also takes on a more pronounced effect on these states compared to the state `z`. +If you want uncorrelated noise for each equation, +multiple `@brownian` variables have to be declared. ```@example SDE +@brownian Bx By Bz +eqs = [D(x) ~ σ * (y - x) + 0.3x * Bx, + D(y) ~ x * (ρ - z) - y + 0.3y * By, + D(z) ~ x * y - β * z + 0.3z * Bz] +@mtkbuild de = System(eqs, t) +prob = SDEProblem(de, [], (0.0, 100.0), []) +sol = solve(prob, SRIW1()) plot(sol) ``` From e0d5e15736b079c5e0120fd177422059cd181204 Mon Sep 17 00:00:00 2001 From: Christopher Rackauckas Date: Sat, 12 Oct 2024 14:37:32 -0400 Subject: [PATCH 0130/2176] Update bifurcationkit.jl --- test/extensions/bifurcationkit.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/extensions/bifurcationkit.jl b/test/extensions/bifurcationkit.jl index 698dd085c8..85ac28765f 100644 --- a/test/extensions/bifurcationkit.jl +++ b/test/extensions/bifurcationkit.jl @@ -1,4 +1,4 @@ -using BifurcationKit, ModelingToolkit, Test +using BifurcationKit, ModelingToolkit, SetField, Test using ModelingToolkit: t_nounits as t, D_nounits as D # Simple pitchfork diagram, compares solution to native BifurcationKit, checks they are identical. # Checks using `jac=false` option. @@ -36,7 +36,7 @@ let bprob_BK = BifurcationProblem(f_BK, [1.0, 1.0], [-1.0, 1.0], - (@lens _[1]); + (SetField.@lens _[1]); record_from_solution = (x, p) -> x[1]) bif_dia_BK = bifurcationdiagram(bprob_BK, PALC(), From 37e8063f503b0d4095bded50ecc0261c4828ff51 Mon Sep 17 00:00:00 2001 From: Christopher Rackauckas Date: Sat, 12 Oct 2024 14:43:24 -0400 Subject: [PATCH 0131/2176] Fix initialization.md --- docs/src/tutorials/initialization.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/src/tutorials/initialization.md b/docs/src/tutorials/initialization.md index 6451832dbc..0cdc2d312b 100644 --- a/docs/src/tutorials/initialization.md +++ b/docs/src/tutorials/initialization.md @@ -278,7 +278,7 @@ initsys = prob2.f.initializeprob.f.sys The system is fully determined, and the equations are solvable. -```@example +```@example paraminit [equations(initsys); observed(initsys)] ``` From 0a479785f7366cff5386111caa0d8b994bbf1d12 Mon Sep 17 00:00:00 2001 From: Christopher Rackauckas Date: Sat, 12 Oct 2024 14:49:26 -0400 Subject: [PATCH 0132/2176] Update Project.toml --- test/extensions/Project.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/test/extensions/Project.toml b/test/extensions/Project.toml index 81097d98d2..3e9ad1557e 100644 --- a/test/extensions/Project.toml +++ b/test/extensions/Project.toml @@ -8,5 +8,6 @@ ModelingToolkit = "961ee093-0014-501f-94e3-6117800e7a78" OrdinaryDiffEq = "1dea7af3-3e70-54e6-95c3-0bf5283fa5ed" SciMLSensitivity = "1ed8b502-d754-442c-8d5d-10ac956f44a1" SciMLStructures = "53ae85a6-f571-4167-b2af-e1d143709226" +SetField = "efcf1570-3423-57d1-acb7-fd33fddbac46" SymbolicIndexingInterface = "2efcf032-c050-4f8e-a9bb-153293bab1f5" Zygote = "e88e6eb3-aa80-5325-afca-941959d7151f" From d52828ebc89cc66ad4ca8903191f2e346be57036 Mon Sep 17 00:00:00 2001 From: Christopher Rackauckas Date: Sat, 12 Oct 2024 14:58:56 -0400 Subject: [PATCH 0133/2176] Update test/extensions/Project.toml --- test/extensions/Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/extensions/Project.toml b/test/extensions/Project.toml index 3e9ad1557e..0e6ba6bc4a 100644 --- a/test/extensions/Project.toml +++ b/test/extensions/Project.toml @@ -8,6 +8,6 @@ ModelingToolkit = "961ee093-0014-501f-94e3-6117800e7a78" OrdinaryDiffEq = "1dea7af3-3e70-54e6-95c3-0bf5283fa5ed" SciMLSensitivity = "1ed8b502-d754-442c-8d5d-10ac956f44a1" SciMLStructures = "53ae85a6-f571-4167-b2af-e1d143709226" -SetField = "efcf1570-3423-57d1-acb7-fd33fddbac46" +Setfield = "efcf1570-3423-57d1-acb7-fd33fddbac46" SymbolicIndexingInterface = "2efcf032-c050-4f8e-a9bb-153293bab1f5" Zygote = "e88e6eb3-aa80-5325-afca-941959d7151f" From 0437dd5449e1cc43920eb205b4dffc0164a25da5 Mon Sep 17 00:00:00 2001 From: Christopher Rackauckas Date: Sat, 12 Oct 2024 15:12:18 -0400 Subject: [PATCH 0134/2176] Update test/extensions/bifurcationkit.jl --- test/extensions/bifurcationkit.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/extensions/bifurcationkit.jl b/test/extensions/bifurcationkit.jl index 85ac28765f..a81a754e0b 100644 --- a/test/extensions/bifurcationkit.jl +++ b/test/extensions/bifurcationkit.jl @@ -36,7 +36,7 @@ let bprob_BK = BifurcationProblem(f_BK, [1.0, 1.0], [-1.0, 1.0], - (SetField.@lens _[1]); + (Setfield.@lens _[1]); record_from_solution = (x, p) -> x[1]) bif_dia_BK = bifurcationdiagram(bprob_BK, PALC(), From fed60d9796c7db9746f61c13c604ebac6366ef01 Mon Sep 17 00:00:00 2001 From: Christopher Rackauckas Date: Sat, 12 Oct 2024 15:12:23 -0400 Subject: [PATCH 0135/2176] Update test/extensions/bifurcationkit.jl --- test/extensions/bifurcationkit.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/extensions/bifurcationkit.jl b/test/extensions/bifurcationkit.jl index a81a754e0b..724a3ebe64 100644 --- a/test/extensions/bifurcationkit.jl +++ b/test/extensions/bifurcationkit.jl @@ -1,4 +1,4 @@ -using BifurcationKit, ModelingToolkit, SetField, Test +using BifurcationKit, ModelingToolkit, Setfield, Test using ModelingToolkit: t_nounits as t, D_nounits as D # Simple pitchfork diagram, compares solution to native BifurcationKit, checks they are identical. # Checks using `jac=false` option. From f288c470abf7116333974c08d1be4b1ae28c93a0 Mon Sep 17 00:00:00 2001 From: Christopher Rackauckas Date: Sat, 12 Oct 2024 16:08:32 -0400 Subject: [PATCH 0136/2176] Remove extra initial condition Fixes https://github.com/SciML/ModelingToolkit.jl/issues/3088 --- docs/src/tutorials/acausal_components.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/docs/src/tutorials/acausal_components.md b/docs/src/tutorials/acausal_components.md index 982629595c..c91ba29670 100644 --- a/docs/src/tutorials/acausal_components.md +++ b/docs/src/tutorials/acausal_components.md @@ -311,8 +311,7 @@ DAE solver](https://docs.sciml.ai/DiffEqDocs/stable/solvers/dae_solve/#OrdinaryD This is done as follows: ```@example acausal -u0 = [rc_model.capacitor.v => 0.0 - rc_model.capacitor.p.i => 0.0] +u0 = [rc_model.capacitor.v => 0.0] prob = ODEProblem(rc_model, u0, (0, 10.0)) sol = solve(prob) From 8380d6b13887cc4af7287291aee515230e156e4e Mon Sep 17 00:00:00 2001 From: Christopher Rackauckas Date: Sun, 13 Oct 2024 02:29:19 -0400 Subject: [PATCH 0137/2176] Backwards compatibility of scope detection for Catalyst Attempt to fix https://github.com/SciML/Catalyst.jl/issues/1075 --- src/utils.jl | 1 + 1 file changed, 1 insertion(+) diff --git a/src/utils.jl b/src/utils.jl index d38ab822d4..39d5d18ffd 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -492,6 +492,7 @@ recursively searches through all subsystems of `sys`, increasing the depth if it function collect_scoped_vars!(unknowns, parameters, sys, iv; depth = 1, op = Differential) if has_eqs(sys) for eq in get_eqs(sys) + hasfield(eq, :lhs) || continue eq.lhs isa Union{Symbolic, Number} || continue collect_vars!(unknowns, parameters, eq.lhs, iv; depth, op) collect_vars!(unknowns, parameters, eq.rhs, iv; depth, op) From d0014f62d6776b760e29a50ffc19582613127487 Mon Sep 17 00:00:00 2001 From: Christopher Rackauckas Date: Sun, 13 Oct 2024 03:33:32 -0400 Subject: [PATCH 0138/2176] Update src/utils.jl Co-authored-by: Sam Isaacson --- src/utils.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utils.jl b/src/utils.jl index 39d5d18ffd..e8ed131d78 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -492,7 +492,7 @@ recursively searches through all subsystems of `sys`, increasing the depth if it function collect_scoped_vars!(unknowns, parameters, sys, iv; depth = 1, op = Differential) if has_eqs(sys) for eq in get_eqs(sys) - hasfield(eq, :lhs) || continue + eq isa Equation || continue eq.lhs isa Union{Symbolic, Number} || continue collect_vars!(unknowns, parameters, eq.lhs, iv; depth, op) collect_vars!(unknowns, parameters, eq.rhs, iv; depth, op) From 1e41add2e0439da2c304fb063b654e4c8ea4478e Mon Sep 17 00:00:00 2001 From: Christopher Rackauckas Date: Sun, 13 Oct 2024 04:35:17 -0400 Subject: [PATCH 0139/2176] Update Project.toml --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 0c152b1d6f..808d68af06 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "ModelingToolkit" uuid = "961ee093-0014-501f-94e3-6117800e7a78" authors = ["Yingbo Ma ", "Chris Rackauckas and contributors"] -version = "9.46.0" +version = "9.46.1" [deps] AbstractTrees = "1520ce14-60c1-5f80-bbc7-55ef81b5835c" From 281b82312379b1941a4c7b27a6eabb1982348086 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 14 Oct 2024 10:15:41 +0530 Subject: [PATCH 0140/2176] docs: remove redundant parameter initialization section --- docs/src/tutorials/initialization.md | 53 ---------------------------- 1 file changed, 53 deletions(-) diff --git a/docs/src/tutorials/initialization.md b/docs/src/tutorials/initialization.md index 0cdc2d312b..c3dbf8bbb0 100644 --- a/docs/src/tutorials/initialization.md +++ b/docs/src/tutorials/initialization.md @@ -464,56 +464,3 @@ sol[α * x - β * x * y] ```@example init plot(sol) ``` - -## Solving for parameters during initialization - -Sometimes, it is necessary to solve for a parameter during initialization. For example, -given a spring-mass system we want to find the un-stretched length of the spring given -that the initial condition of the system is its steady state. - -```@example init -using ModelingToolkitStandardLibrary.Mechanical.TranslationalModelica: Fixed, Mass, Spring, - Force, Damper -using ModelingToolkitStandardLibrary.Blocks: Constant - -@named mass = Mass(; m = 1.0, s = 1.0, v = 0.0, a = 0.0) -@named fixed = Fixed(; s0 = 0.0) -@named spring = Spring(; c = 2.0, s_rel0 = missing) -@named gravity = Force() -@named constant = Constant(; k = 9.81) -@named damper = Damper(; d = 0.1) -@mtkbuild sys = ODESystem( - [connect(fixed.flange, spring.flange_a), connect(spring.flange_b, mass.flange_a), - connect(mass.flange_a, gravity.flange), connect(constant.output, gravity.f), - connect(fixed.flange, damper.flange_a), connect(damper.flange_b, mass.flange_a)], - t; - systems = [fixed, spring, mass, gravity, constant, damper], - guesses = [spring.s_rel0 => 1.0]) -``` - -Note that we explicitly provide `s_rel0 = missing` to the spring. Parameters are only -solved for during initialization if their value (either default, or explicitly passed -to the `ODEProblem` constructor) is `missing`. We also need to provide a guess for the -parameter. - -If a parameter is not given a value of `missing`, and does not have a default or initial -value, the `ODEProblem` constructor will throw an error. If the parameter _does_ have a -value of `missing`, it must be given a guess. - -```@example init -prob = ODEProblem(sys, [], (0.0, 1.0)) -prob.ps[spring.s_rel0] -``` - -Note that the value of the parameter in the problem is zero, similar to unknowns that -are solved for during initialization. - -```@example init -integ = init(prob) -integ.ps[spring.s_rel0] -``` - -The un-stretched length of the spring is now correctly calculated. The same result can be -achieved if `s_rel0 = missing` is omitted when constructing `spring`, and instead -`spring.s_rel0 => missing` is passed to the `ODEProblem` constructor along with values -of other parameters. From 82d815c4c2eaaf6ec28f2a257c2cf512723bb094 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 2 Oct 2024 16:14:56 +0530 Subject: [PATCH 0141/2176] refactor: major cleanup of `*Problem` construction --- src/ModelingToolkit.jl | 1 + src/systems/diffeqs/abstractodesystem.jl | 222 +------- src/systems/diffeqs/sdesystem.jl | 5 +- .../discrete_system/discrete_system.jl | 60 +-- src/systems/nonlinear/nonlinearsystem.jl | 40 +- src/systems/problem_utils.jl | 503 ++++++++++++++++++ src/variables.jl | 2 +- test/initializationsystem.jl | 3 +- test/split_parameters.jl | 2 +- 9 files changed, 541 insertions(+), 297 deletions(-) create mode 100644 src/systems/problem_utils.jl diff --git a/src/ModelingToolkit.jl b/src/ModelingToolkit.jl index 2237ba8952..2f57bb1765 100644 --- a/src/ModelingToolkit.jl +++ b/src/ModelingToolkit.jl @@ -144,6 +144,7 @@ include("systems/abstractsystem.jl") include("systems/model_parsing.jl") include("systems/connectors.jl") include("systems/callbacks.jl") +include("systems/problem_utils.jl") include("systems/nonlinear/nonlinearsystem.jl") include("systems/diffeqs/odesystem.jl") diff --git a/src/systems/diffeqs/abstractodesystem.jl b/src/systems/diffeqs/abstractodesystem.jl index 8ebe6d93d0..48770e7a52 100644 --- a/src/systems/diffeqs/abstractodesystem.jl +++ b/src/systems/diffeqs/abstractodesystem.jl @@ -793,211 +793,6 @@ function get_u0( return u0, defs end -struct GetUpdatedMTKParameters{G, S} - # `getu` functor which gets parameters that are unknowns during initialization - getpunknowns::G - # `setu` functor which returns a modified MTKParameters using those parameters - setpunknowns::S -end - -function (f::GetUpdatedMTKParameters)(prob, initializesol) - mtkp = copy(parameter_values(prob)) - f.setpunknowns(mtkp, f.getpunknowns(initializesol)) - mtkp -end - -struct UpdateInitializeprob{G, S} - # `getu` functor which gets all values from prob - getvals::G - # `setu` functor which updates initializeprob with values - setvals::S -end - -function (f::UpdateInitializeprob)(initializeprob, prob) - f.setvals(initializeprob, f.getvals(prob)) -end - -function get_temporary_value(p) - stype = symtype(unwrap(p)) - return if stype == Real - zero(Float64) - elseif stype <: AbstractArray{Real} - zeros(Float64, size(p)) - elseif stype <: Real - zero(stype) - elseif stype <: AbstractArray - zeros(eltype(stype), size(p)) - else - error("Nonnumeric parameter $p with symtype $stype cannot be solved for during initialization") - end -end - -function process_DEProblem(constructor, sys::AbstractODESystem, u0map, parammap; - implicit_dae = false, du0map = nothing, - version = nothing, tgrad = false, - jac = false, - checkbounds = false, sparse = false, - simplify = false, - linenumbers = true, parallel = SerialForm(), - eval_expression = false, - eval_module = @__MODULE__, - use_union = false, - tofloat = true, - symbolic_u0 = false, - u0_constructor = identity, - guesses = Dict(), - t = nothing, - warn_initialize_determined = true, - build_initializeprob = true, - initialization_eqs = [], - fully_determined = false, - check_units = true, - kwargs...) - eqs = equations(sys) - dvs = unknowns(sys) - ps = parameters(sys) - iv = get_iv(sys) - - check_array_equations_unknowns(eqs, dvs) - # TODO: Pass already computed information to varmap_to_vars call - # in process_u0? That would just be a small optimization - varmap = u0map === nothing || isempty(u0map) || eltype(u0map) <: Number ? - defaults(sys) : - merge(defaults(sys), todict(u0map)) - varmap = canonicalize_varmap(varmap) - varlist = collect(map(unwrap, dvs)) - missingvars = setdiff(varlist, collect(keys(varmap))) - setobserved = filter(keys(varmap)) do var - has_observed_with_lhs(sys, var) || has_observed_with_lhs(sys, default_toterm(var)) - end - - if eltype(parammap) <: Pair - parammap = Dict{Any, Any}(unwrap(k) => v for (k, v) in parammap) - elseif parammap isa AbstractArray - if isempty(parammap) - parammap = SciMLBase.NullParameters() - else - parammap = Dict{Any, Any}(unwrap.(parameters(sys)) .=> parammap) - end - end - defs = defaults(sys) - if has_guesses(sys) - guesses = merge( - ModelingToolkit.guesses(sys), isempty(guesses) ? Dict() : todict(guesses)) - solvablepars = [p - for p in parameters(sys) - if is_parameter_solvable(p, parammap, defs, guesses)] - - pvarmap = if parammap === nothing || parammap == SciMLBase.NullParameters() || - !(eltype(parammap) <: Pair) && isempty(parammap) - defs - else - merge(defs, todict(parammap)) - end - setparobserved = filter(keys(pvarmap)) do var - has_parameter_dependency_with_lhs(sys, var) - end - else - solvablepars = () - setparobserved = () - end - # ModelingToolkit.get_tearing_state(sys) !== nothing => Requires structural_simplify first - if sys isa ODESystem && build_initializeprob && - (((implicit_dae || !isempty(missingvars) || !isempty(solvablepars) || - !isempty(setobserved) || !isempty(setparobserved)) && - ModelingToolkit.get_tearing_state(sys) !== nothing) || - !isempty(initialization_equations(sys))) && t !== nothing - if eltype(u0map) <: Number - u0map = unknowns(sys) .=> vec(u0map) - end - if u0map === nothing || isempty(u0map) - u0map = Dict() - end - - initializeprob = ModelingToolkit.InitializationProblem( - sys, t, u0map, parammap; guesses, warn_initialize_determined, - initialization_eqs, eval_expression, eval_module, fully_determined, check_units) - initializeprobmap = getu(initializeprob, unknowns(sys)) - punknowns = [p - for p in all_variable_symbols(initializeprob) if is_parameter(sys, p)] - getpunknowns = getu(initializeprob, punknowns) - setpunknowns = setp(sys, punknowns) - initializeprobpmap = GetUpdatedMTKParameters(getpunknowns, setpunknowns) - reqd_syms = parameter_symbols(initializeprob) - update_initializeprob! = UpdateInitializeprob( - getu(sys, reqd_syms), setu(initializeprob, reqd_syms)) - - zerovars = Dict(setdiff(unknowns(sys), keys(defaults(sys))) .=> 0.0) - if parammap isa SciMLBase.NullParameters - parammap = Dict() - end - for p in punknowns - p = unwrap(p) - stype = symtype(p) - parammap[p] = get_temporary_value(p) - end - trueinit = collect(merge(zerovars, eltype(u0map) <: Pair ? todict(u0map) : u0map)) - u0map isa StaticArraysCore.StaticArray && - (trueinit = SVector{length(trueinit)}(trueinit)) - else - initializeprob = nothing - update_initializeprob! = nothing - initializeprobmap = nothing - initializeprobpmap = nothing - trueinit = u0map - end - - if has_index_cache(sys) && get_index_cache(sys) !== nothing - u0, defs = get_u0(sys, trueinit, parammap; symbolic_u0, - t0 = constructor <: Union{DDEFunction, SDDEFunction} ? nothing : t, use_union) - check_eqs_u0(eqs, dvs, u0; kwargs...) - p = if parammap === nothing || - parammap == SciMLBase.NullParameters() && isempty(defs) - nothing - else - MTKParameters(sys, parammap, trueinit; t0 = t) - end - else - u0, p, defs = get_u0_p(sys, - trueinit, - parammap; - tofloat, - use_union, - t0 = constructor <: Union{DDEFunction, SDDEFunction} ? nothing : t, - symbolic_u0) - p, split_idxs = split_parameters_by_type(p) - if p isa Tuple - ps = Base.Fix1(getindex, parameters(sys)).(split_idxs) - ps = (ps...,) #if p is Tuple, ps should be Tuple - end - end - if u0 !== nothing - u0 = u0_constructor(u0) - end - - if implicit_dae && du0map !== nothing - ddvs = map(Differential(iv), dvs) - defs = mergedefaults(defs, du0map, ddvs) - du0 = varmap_to_vars(du0map, ddvs; defaults = defs, toterm = identity, - tofloat = true) - else - du0 = nothing - ddvs = nothing - end - check_eqs_u0(eqs, dvs, u0; kwargs...) - f = constructor(sys, dvs, ps, u0; ddvs = ddvs, tgrad = tgrad, jac = jac, - checkbounds = checkbounds, p = p, - linenumbers = linenumbers, parallel = parallel, simplify = simplify, - sparse = sparse, eval_expression = eval_expression, - eval_module = eval_module, - initializeprob = initializeprob, - update_initializeprob! = update_initializeprob!, - initializeprobmap = initializeprobmap, - initializeprobpmap = initializeprobpmap, - kwargs...) - implicit_dae ? (f, du0, u0, p) : (f, u0, p) -end - function ODEFunctionExpr(sys::AbstractODESystem, args...; kwargs...) ODEFunctionExpr{true}(sys, args...; kwargs...) end @@ -1104,7 +899,7 @@ function DiffEqBase.ODEProblem{iip, specialize}(sys::AbstractODESystem, u0map = if !iscomplete(sys) error("A completed system is required. Call `complete` or `structural_simplify` on the system before creating an `ODEProblem`") end - f, u0, p = process_DEProblem(ODEFunction{iip, specialize}, sys, u0map, parammap; + f, u0, p = process_SciMLProblem(ODEFunction{iip, specialize}, sys, u0map, parammap; t = tspan !== nothing ? tspan[1] : tspan, check_length, warn_initialize_determined, eval_expression, eval_module, kwargs...) cbs = process_events(sys; callback, eval_expression, eval_module, kwargs...) @@ -1147,7 +942,7 @@ function DiffEqBase.DAEProblem{iip}(sys::AbstractODESystem, du0map, u0map, tspan if !iscomplete(sys) error("A completed system is required. Call `complete` or `structural_simplify` on the system before creating a `DAEProblem`") end - f, du0, u0, p = process_DEProblem(DAEFunction{iip}, sys, u0map, parammap; + f, du0, u0, p = process_SciMLProblem(DAEFunction{iip}, sys, u0map, parammap; implicit_dae = true, du0map = du0map, check_length, t = tspan !== nothing ? tspan[1] : tspan, warn_initialize_determined, kwargs...) @@ -1179,7 +974,7 @@ function DiffEqBase.DDEProblem{iip}(sys::AbstractODESystem, u0map = [], if !iscomplete(sys) error("A completed system is required. Call `complete` or `structural_simplify` on the system before creating a `DDEProblem`") end - f, u0, p = process_DEProblem(DDEFunction{iip}, sys, u0map, parammap; + f, u0, p = process_SciMLProblem(DDEFunction{iip}, sys, u0map, parammap; t = tspan !== nothing ? tspan[1] : tspan, symbolic_u0 = true, check_length, eval_expression, eval_module, kwargs...) @@ -1214,7 +1009,7 @@ function DiffEqBase.SDDEProblem{iip}(sys::AbstractODESystem, u0map = [], if !iscomplete(sys) error("A completed system is required. Call `complete` or `structural_simplify` on the system before creating a `SDDEProblem`") end - f, u0, p = process_DEProblem(SDDEFunction{iip}, sys, u0map, parammap; + f, u0, p = process_SciMLProblem(SDDEFunction{iip}, sys, u0map, parammap; t = tspan !== nothing ? tspan[1] : tspan, symbolic_u0 = true, eval_expression, eval_module, check_length, kwargs...) @@ -1274,7 +1069,8 @@ function ODEProblemExpr{iip}(sys::AbstractODESystem, u0map, tspan, if !iscomplete(sys) error("A completed system is required. Call `complete` or `structural_simplify` on the system before creating a `ODEProblemExpr`") end - f, u0, p = process_DEProblem(ODEFunctionExpr{iip}, sys, u0map, parammap; check_length, + f, u0, p = process_SciMLProblem( + ODEFunctionExpr{iip}, sys, u0map, parammap; check_length, t = tspan !== nothing ? tspan[1] : tspan, kwargs...) linenumbers = get(kwargs, :linenumbers, true) @@ -1320,7 +1116,7 @@ function DAEProblemExpr{iip}(sys::AbstractODESystem, du0map, u0map, tspan, if !iscomplete(sys) error("A completed system is required. Call `complete` or `structural_simplify` on the system before creating a `DAEProblemExpr`") end - f, du0, u0, p = process_DEProblem(DAEFunctionExpr{iip}, sys, u0map, parammap; + f, du0, u0, p = process_SciMLProblem(DAEFunctionExpr{iip}, sys, u0map, parammap; t = tspan !== nothing ? tspan[1] : tspan, implicit_dae = true, du0map = du0map, check_length, kwargs...) @@ -1372,7 +1168,7 @@ function DiffEqBase.SteadyStateProblem{iip}(sys::AbstractODESystem, u0map, if !iscomplete(sys) error("A completed system is required. Call `complete` or `structural_simplify` on the system before creating a `SteadyStateProblem`") end - f, u0, p = process_DEProblem(ODEFunction{iip}, sys, u0map, parammap; + f, u0, p = process_SciMLProblem(ODEFunction{iip}, sys, u0map, parammap; steady_state = true, check_length, kwargs...) kwargs = filter_kwargs(kwargs) @@ -1404,7 +1200,7 @@ function SteadyStateProblemExpr{iip}(sys::AbstractODESystem, u0map, if !iscomplete(sys) error("A completed system is required. Call `complete` or `structural_simplify` on the system before creating a `SteadyStateProblemExpr`") end - f, u0, p = process_DEProblem(ODEFunctionExpr{iip}, sys, u0map, parammap; + f, u0, p = process_SciMLProblem(ODEFunctionExpr{iip}, sys, u0map, parammap; steady_state = true, check_length, kwargs...) linenumbers = get(kwargs, :linenumbers, true) diff --git a/src/systems/diffeqs/sdesystem.jl b/src/systems/diffeqs/sdesystem.jl index 1b368f7df3..0d73aaf313 100644 --- a/src/systems/diffeqs/sdesystem.jl +++ b/src/systems/diffeqs/sdesystem.jl @@ -659,7 +659,7 @@ function DiffEqBase.SDEProblem{iip, specialize}( if !iscomplete(sys) error("A completed `SDESystem` is required. Call `complete` or `structural_simplify` on the system before creating an `SDEProblem`") end - f, u0, p = process_DEProblem( + f, u0, p = process_SciMLProblem( SDEFunction{iip, specialize}, sys, u0map, parammap; check_length, kwargs...) cbs = process_events(sys; callback, kwargs...) @@ -745,7 +745,8 @@ function SDEProblemExpr{iip}(sys::SDESystem, u0map, tspan, if !iscomplete(sys) error("A completed `SDESystem` is required. Call `complete` or `structural_simplify` on the system before creating an `SDEProblemExpr`") end - f, u0, p = process_DEProblem(SDEFunctionExpr{iip}, sys, u0map, parammap; check_length, + f, u0, p = process_SciMLProblem( + SDEFunctionExpr{iip}, sys, u0map, parammap; check_length, kwargs...) linenumbers = get(kwargs, :linenumbers, true) sparsenoise === nothing && (sparsenoise = get(kwargs, :sparse, false)) diff --git a/src/systems/discrete_system/discrete_system.jl b/src/systems/discrete_system/discrete_system.jl index 7103cfca80..bf7879be62 100644 --- a/src/systems/discrete_system/discrete_system.jl +++ b/src/systems/discrete_system/discrete_system.jl @@ -236,55 +236,25 @@ function generate_function( generate_custom_function(sys, exprs, dvs, ps; wrap_code, kwargs...) end -function process_DiscreteProblem(constructor, sys::DiscreteSystem, u0map, parammap; - linenumbers = true, parallel = SerialForm(), - use_union = false, - tofloat = !use_union, - eval_expression = false, eval_module = @__MODULE__, - kwargs...) +function shift_u0map_forward(sys::DiscreteSystem, u0map, defs) iv = get_iv(sys) - eqs = equations(sys) - dvs = unknowns(sys) - ps = parameters(sys) - - if eltype(u0map) <: Number - u0map = unknowns(sys) .=> vec(u0map) - end - if u0map === nothing || isempty(u0map) - u0map = Dict() - end - - trueu0map = Dict() - for (k, v) in u0map - k = unwrap(k) + updated = AnyDict() + for k in collect(keys(u0map)) + v = u0map[k] if !((op = operation(k)) isa Shift) error("Initial conditions must be for the past state of the unknowns. Instead of providing the condition for $k, provide the condition for $(Shift(iv, -1)(k)).") end - trueu0map[Shift(iv, op.steps + 1)(arguments(k)[1])] = v - end - defs = ModelingToolkit.get_defaults(sys) - for var in dvs - if (op = operation(var)) isa Shift && !haskey(trueu0map, var) - root = arguments(var)[1] - haskey(defs, root) || error("Initial condition for $var not provided.") - trueu0map[var] = defs[root] - end + updated[Shift(iv, op.steps + 1)(arguments(k)[1])] = v end - if has_index_cache(sys) && get_index_cache(sys) !== nothing - u0, defs = get_u0(sys, trueu0map, parammap) - p = MTKParameters(sys, parammap, trueu0map) - else - u0, p, defs = get_u0_p(sys, trueu0map, parammap; tofloat, use_union) + for var in unknowns(sys) + op = operation(var) + op isa Shift || continue + haskey(updated, var) && continue + root = first(arguments(var)) + haskey(defs, root) || error("Initial condition for $var not provided.") + updated[var] = defs[root] end - - check_eqs_u0(eqs, dvs, u0; kwargs...) - - f = constructor(sys, dvs, ps, u0; - linenumbers = linenumbers, parallel = parallel, - syms = Symbol.(dvs), paramsyms = Symbol.(ps), - eval_expression = eval_expression, eval_module = eval_module, - kwargs...) - return f, u0, p + return updated end """ @@ -307,7 +277,9 @@ function SciMLBase.DiscreteProblem( eqs = equations(sys) iv = get_iv(sys) - f, u0, p = process_DiscreteProblem( + u0map = to_varmap(u0map, dvs) + u0map = shift_u0map_forward(sys, u0map, defaults(sys)) + f, u0, p = process_SciMLProblem( DiscreteFunction, sys, u0map, parammap; eval_expression, eval_module) u0 = f(u0, p, tspan[1]) DiscreteProblem(f, u0, tspan, p; kwargs...) diff --git a/src/systems/nonlinear/nonlinearsystem.jl b/src/systems/nonlinear/nonlinearsystem.jl index 46bf032d6f..a0cd636753 100644 --- a/src/systems/nonlinear/nonlinearsystem.jl +++ b/src/systems/nonlinear/nonlinearsystem.jl @@ -293,7 +293,7 @@ function SciMLBase.NonlinearFunction(sys::NonlinearSystem, args...; kwargs...) end function SciMLBase.NonlinearFunction{iip}(sys::NonlinearSystem, dvs = unknowns(sys), - ps = parameters(sys), u0 = nothing, p = nothing; + ps = parameters(sys), u0 = nothing; p = nothing, version = nothing, jac = false, eval_expression = false, @@ -408,36 +408,6 @@ function NonlinearFunctionExpr{iip}(sys::NonlinearSystem, dvs = unknowns(sys), !linenumbers ? Base.remove_linenums!(ex) : ex end -function process_NonlinearProblem(constructor, sys::NonlinearSystem, u0map, parammap; - version = nothing, - jac = false, - checkbounds = false, sparse = false, - simplify = false, - linenumbers = true, parallel = SerialForm(), - eval_expression = false, - eval_module = @__MODULE__, - use_union = false, - tofloat = !use_union, - kwargs...) - eqs = equations(sys) - dvs = unknowns(sys) - ps = parameters(sys) - if has_index_cache(sys) && get_index_cache(sys) !== nothing - u0, defs = get_u0(sys, u0map, parammap) - check_eqs_u0(eqs, dvs, u0; kwargs...) - p = MTKParameters(sys, parammap, u0map) - else - u0, p, defs = get_u0_p(sys, u0map, parammap; tofloat, use_union) - check_eqs_u0(eqs, dvs, u0; kwargs...) - end - - f = constructor(sys, dvs, ps, u0, p; jac = jac, checkbounds = checkbounds, - linenumbers = linenumbers, parallel = parallel, simplify = simplify, - sparse = sparse, eval_expression = eval_expression, eval_module = eval_module, - kwargs...) - return f, u0, p -end - """ ```julia DiffEqBase.NonlinearProblem{iip}(sys::NonlinearSystem, u0map, @@ -461,7 +431,7 @@ function DiffEqBase.NonlinearProblem{iip}(sys::NonlinearSystem, u0map, if !iscomplete(sys) error("A completed `NonlinearSystem` is required. Call `complete` or `structural_simplify` on the system before creating a `NonlinearProblem`") end - f, u0, p = process_NonlinearProblem(NonlinearFunction{iip}, sys, u0map, parammap; + f, u0, p = process_SciMLProblem(NonlinearFunction{iip}, sys, u0map, parammap; check_length, kwargs...) pt = something(get_metadata(sys), StandardNonlinearProblem()) NonlinearProblem{iip}(f, u0, p, pt; filter_kwargs(kwargs)...) @@ -490,7 +460,7 @@ function DiffEqBase.NonlinearLeastSquaresProblem{iip}(sys::NonlinearSystem, u0ma if !iscomplete(sys) error("A completed `NonlinearSystem` is required. Call `complete` or `structural_simplify` on the system before creating a `NonlinearLeastSquaresProblem`") end - f, u0, p = process_NonlinearProblem(NonlinearFunction{iip}, sys, u0map, parammap; + f, u0, p = process_SciMLProblem(NonlinearFunction{iip}, sys, u0map, parammap; check_length, kwargs...) pt = something(get_metadata(sys), StandardNonlinearProblem()) NonlinearLeastSquaresProblem{iip}(f, u0, p; filter_kwargs(kwargs)...) @@ -523,7 +493,7 @@ function NonlinearProblemExpr{iip}(sys::NonlinearSystem, u0map, if !iscomplete(sys) error("A completed `NonlinearSystem` is required. Call `complete` or `structural_simplify` on the system before creating a `NonlinearProblemExpr`") end - f, u0, p = process_NonlinearProblem(NonlinearFunctionExpr{iip}, sys, u0map, parammap; + f, u0, p = process_SciMLProblem(NonlinearFunctionExpr{iip}, sys, u0map, parammap; check_length, kwargs...) linenumbers = get(kwargs, :linenumbers, true) @@ -563,7 +533,7 @@ function NonlinearLeastSquaresProblemExpr{iip}(sys::NonlinearSystem, u0map, if !iscomplete(sys) error("A completed `NonlinearSystem` is required. Call `complete` or `structural_simplify` on the system before creating a `NonlinearProblemExpr`") end - f, u0, p = process_NonlinearProblem(NonlinearFunctionExpr{iip}, sys, u0map, parammap; + f, u0, p = process_SciMLProblem(NonlinearFunctionExpr{iip}, sys, u0map, parammap; check_length, kwargs...) linenumbers = get(kwargs, :linenumbers, true) diff --git a/src/systems/problem_utils.jl b/src/systems/problem_utils.jl new file mode 100644 index 0000000000..b5de7b64d6 --- /dev/null +++ b/src/systems/problem_utils.jl @@ -0,0 +1,503 @@ +const AnyDict = Dict{Any, Any} + +""" + $(TYPEDSIGNATURES) + +If called without arguments, return `Dict{Any, Any}`. Otherwise, interpret the input +as a symbolic map and turn it into a `Dict{Any, Any}`. Handles `SciMLBase.NullParameters` +and `nothing`. +""" +anydict() = AnyDict() +anydict(::SciMLBase.NullParameters) = AnyDict() +anydict(::Nothing) = AnyDict() +anydict(x::AnyDict) = x +anydict(x) = AnyDict(x) + +""" + $(TYPEDSIGNATURES) + +Check if `x` is a symbolic with known size. Assumes `Symbolics.shape(unwrap(x))` +is a valid operation. +""" +is_sized_array_symbolic(x) = Symbolics.shape(unwrap(x)) != Symbolics.Unknown() + +""" + $(TYPEDSIGNATURES) + +Check if the system is in split form (has an `IndexCache`). +""" +is_split(sys::AbstractSystem) = has_index_cache(sys) && get_index_cache(sys) !== nothing + +""" + $(TYPEDSIGNATURES) + +Given a variable-value mapping, add mappings for the `toterm` of each of the keys. +""" +function add_toterms!(varmap::AbstractDict; toterm = default_toterm) + for k in collect(keys(varmap)) + varmap[toterm(k)] = varmap[k] + end + return nothing +end + +""" + $(TYPEDSIGNATURES) + +Out-of-place version of [`add_toterms!`](@ref). +""" +function add_toterms(varmap::AbstractDict; toterm = default_toterm) + cp = copy(varmap) + add_toterms!(cp; toterm) + return cp +end + +""" + $(TYPEDSIGNATURES) + +Ensure `varmap` contains entries for all variables in `vars` by using values from +`fallbacks` if they don't already exist in `varmap`. Return the set of all variables in +`vars` not present in `varmap` or `fallbacks`. If an array variable in `vars` does not +exist in `varmap` or `fallbacks`, each of its scalarized elements will be searched for. +In case none of the scalarized elements exist, the array variable will be reported as +missing. In case some of the scalarized elements exist, the missing elements will be +reported as missing. If `fallbacks` contains both the scalarized and non-scalarized forms, +the latter will take priority. + +Variables as they are specified in `vars` will take priority over their `toterm` forms. +""" +function add_fallbacks!( + varmap::AnyDict, vars::Vector, fallbacks::Dict; toterm = default_toterm) + missingvars = Set() + for var in vars + haskey(varmap, var) && continue + ttvar = toterm(var) + haskey(varmap, ttvar) && continue + + # array symbolics with a defined size may be present in the scalarized form + if Symbolics.isarraysymbolic(var) && is_sized_array_symbolic(var) + val = map(eachindex(var)) do idx + # @something is lazy and saves from writing a massive if-elseif-else + @something(get(varmap, var[idx], nothing), + get(varmap, ttvar[idx], nothing), get(fallbacks, var, nothing)[idx], + get(fallbacks, ttvar, nothing)[idx], get(fallbacks, var[idx], nothing), + get(fallbacks, ttvar[idx], nothing), Some(nothing)) + end + # only push the missing entries + mask = map(x -> x === nothing, val) + if all(mask) + push!(missingvars, var) + elseif any(mask) + for i in eachindex(var) + if mask[i] + push!(missingvars, var) + else + varmap[var[i]] = val[i] + end + end + else + varmap[var] = val + end + else + if iscall(var) && operation(var) == getindex + args = arguments(var) + arrvar = args[1] + ttarrvar = toterm(arrvar) + idxs = args[2:end] + val = @something get(varmap, arrvar, nothing) get(varmap, ttarrvar, nothing) get( + fallbacks, arrvar, nothing) get(fallbacks, ttarrvar, nothing) Some(nothing) + if val !== nothing + val = val[idxs...] + end + else + val = nothing + end + val = @something val get(fallbacks, var, nothing) get(fallbacks, ttvar, nothing) Some(nothing) + if val === nothing + push!(missingvars, var) + else + varmap[var] = val + end + end + end + + return missingvars +end + +""" + $(TYPEDSIGNATURES) + +Return the list of variables in `varlist` not present in `varmap`. Uses the same criteria +for missing array variables and `toterm` forms as [`add_fallbacks!`](@ref). +""" +function missingvars( + varmap::AbstractDict, varlist::Vector; toterm = default_toterm) + missingvars = Set() + for var in varlist + haskey(varmap, var) && continue + ttsym = toterm(var) + haskey(varmap, ttsym) && continue + + if Symbolics.isarraysymbolic(var) && is_sized_array_symbolic(var) + mask = map(eachindex(var)) do idx + !haskey(varmap, var[idx]) && !haskey(varmap, ttsym[idx]) + end + if all(mask) + push!(missingvars, var) + else + for i in eachindex(var) + mask[i] && push!(missingvars, var[i]) + end + end + else + push!(missingvars, var) + end + end + return missingvars +end + +""" + $(TYPEDSIGNATURES) + +Attempt to interpret `vals` as a symbolic map of variables in `varlist` to values. Return +the result as a `Dict{Any, Any}`. In case `vals` is already an iterable of pairs, convert +it to a `Dict{Any, Any}` and return. If `vals` is an array (whose `eltype` is not `Pair`) +with the same length as `varlist`, assume the `i`th element of `varlist` is mapped to the +`i`th element of `vals`. Automatically `unwrap`s all keys and values in the mapping. Also +handles `SciMLBase.NullParameters` and `nothing`, both of which are interpreted as empty +maps. +""" +function to_varmap(vals, varlist::Vector) + if vals isa AbstractArray && !(eltype(vals) <: Pair) && !isempty(vals) + check_eqs_u0(varlist, varlist, vals) + vals = vec(varlist) .=> vec(vals) + end + return anydict(unwrap(k) => unwrap(v) for (k, v) in anydict(vals)) +end + +""" + $(TYPEDSIGNATURES) + +Return the appropriate zero value for a symbolic variable representing a number or array of +numbers. Sized array symbolics return a zero-filled array of matching size. Unsized array +symbolics return an empty array of the appropriate `eltype`. +""" +function zero_var(x::Symbolic{T}) where {V <: Number, T <: Union{V, AbstractArray{V}}} + if Symbolics.isarraysymbolic(x) + if is_sized_array_symbolic(x) + return zeros(T, size(x)) + else + return T[] + end + else + return zero(T) + end +end + +""" + $(TYPEDSIGNATURES) + +Add equations `eqs` to `varmap`. Assumes each element in `eqs` maps a single symbolic +variable to an expression representing its value. In case `varmap` already contains an +entry for `eq.lhs`, insert the reverse mapping if `eq.rhs` is not a number. +""" +function add_observed_equations!(varmap::AbstractDict, eqs) + for eq in eqs + if haskey(varmap, eq.lhs) + eq.rhs isa Number && continue + haskey(varmap, eq.rhs) && continue + !iscall(eq.rhs) || issym(operation(eq.rhs)) || continue + varmap[eq.rhs] = eq.lhs + else + varmap[eq.lhs] = eq.rhs + end + end +end + +""" + $(TYPEDSIGNATURES) + +Add all equations in `observed(sys)` to `varmap` using [`add_observed_equations!`](@ref). +""" +function add_observed!(sys::AbstractSystem, varmap::AbstractDict) + add_observed_equations!(varmap, observed(sys)) +end + +""" + $(TYPEDSIGNATURES) + +Add all equations in `parameter_dependencies(sys)` to `varmap` using +[`add_observed_equations!`](@ref). +""" +function add_parameter_dependencies!(sys::AbstractSystem, varmap::AbstractDict) + has_parameter_dependencies(sys) || return nothing + add_observed_equations!(varmap, parameter_dependencies(sys)) +end + +""" + $(TYPEDSIGNATURES) + +Return an array of values where the `i`th element corresponds to the value of `vars[i]` +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`. +- `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`, + `promotetoconcrete` is set to `false`. +""" +function better_varmap_to_vars(varmap::AbstractDict, vars::Vector; + tofloat = true, use_union = true, container_type = Array, + toterm = default_toterm, promotetoconcrete = nothing, check = true, allow_symbolic = false) + isempty(vars) && return nothing + + if check + missing_vars = missingvars(varmap, vars; toterm) + isempty(missing_vars) || throw(MissingVariablesError(missing_vars)) + end + vals = map(x -> varmap[x], vars) + + if container_type <: Union{AbstractDict, Tuple, Nothing} + container_type = Array + end + + promotetoconcrete === nothing && (promotetoconcrete = container_type <: AbstractArray) + if promotetoconcrete && !allow_symbolic + vals = promote_to_concrete(vals; tofloat = tofloat, use_union = use_union) + end + + if isempty(vals) + return nothing + elseif container_type <: Tuple + return (vals...,) + else + return SymbolicUtils.Code.create_array(container_type, eltype(vals), Val{1}(), + Val(length(vals)), vals...) + end +end + +""" + $(TYPEDSIGNATURES) + +Performs symbolic substitution on the values in `varmap`, using `varmap` itself as the +set of substitution rules. +""" +function evaluate_varmap!(varmap::AbstractDict) + for (k, v) in varmap + varmap[k] = fixpoint_sub(v, varmap) + end +end + +struct GetUpdatedMTKParameters{G, S} + # `getu` functor which gets parameters that are unknowns during initialization + getpunknowns::G + # `setu` functor which returns a modified MTKParameters using those parameters + setpunknowns::S +end + +function (f::GetUpdatedMTKParameters)(prob, initializesol) + mtkp = copy(parameter_values(prob)) + f.setpunknowns(mtkp, f.getpunknowns(initializesol)) + mtkp +end + +struct UpdateInitializeprob{G, S} + # `getu` functor which gets all values from prob + getvals::G + # `setu` functor which updates initializeprob with values + setvals::S +end + +function (f::UpdateInitializeprob)(initializeprob, prob) + f.setvals(initializeprob, f.getvals(prob)) +end + +function get_temporary_value(p) + stype = symtype(unwrap(p)) + return if stype == Real + zero(Float64) + elseif stype <: AbstractArray{Real} + zeros(Float64, size(p)) + elseif stype <: Real + zero(stype) + elseif stype <: AbstractArray + zeros(eltype(stype), size(p)) + else + error("Nonnumeric parameter $p with symtype $stype cannot be solved for during initialization") + end +end + +""" + $(TYPEDSIGNATURES) + +Return the SciMLFunction created via calling `constructor`, the initial conditions `u0` +and parameter object `p` given the system `sys`, and user-provided initial values `u0map` +and `pmap`. `u0map` and `pmap` are converted into variable maps via [`to_varmap`](@ref). + +The order of unknowns is determined by `unknowns(sys)`. If the system is split +[`is_split`](@ref) create an [`MTKParameters`](@ref) object. Otherwise, a parameter vector. +Initial values provided in terms of other variables will be symbolically evaluated using +[`evaluate_varmap!`](@ref). The type of `u0map` and `pmap` will be used to determine the +type of the containers (if parameters are not in an `MTKParameters` object). `Dict`s will be +turned into `Array`s. + +If `sys isa ODESystem`, this will also build the initialization problem and related objects +and pass them to the SciMLFunction as keyword arguments. + +Keyword arguments: +- `build_initializeprob`: If `false`, avoids building the initialization problem for an + `ODESystem`. +- `t`: The initial time of the `ODEProblem`. If this is not provided, the initialization + problem cannot be built. +- `implicit_dae`: Also build a mapping of derivatives of states to values for implicit DAEs, + using `du0map`. Changes the return value of this function to `(f, du0, u0, p)` instead of + `(f, u0, p)`. +- `guesses`: The guesses for variables in the system, used as initial values for the + initialization problem. +- `warn_initialize_determined`: Warn if the initialization system is under/over-determined. +- `initialization_eqs`: Extra equations to use in the initialization problem. +- `eval_expression`: Whether to compile any functions via `eval` or `RuntimeGeneratedFunctions`. +- `eval_module`: If `eval_expression == true`, the module to `eval` into. Otherwise, the module + in which to generate the `RuntimeGeneratedFunction`. +- `fully_determined`: Override whether the initialization system is fully determined. +- `check_units`: Enable or disable unit checks. +- `tofloat`, `use_union`: 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`. +- `check_length`: Whether to check the number of equations along with number of unknowns and + length of `u0` vector for consistency. If `false`, do not check with equations. This is + forwarded to `check_eqs_u0` +- `symbolic_u0` allows the returned `u0` to be an array of symbolics. + +All other keyword arguments are passed as-is to `constructor`. +""" +function process_SciMLProblem( + constructor, sys::AbstractSystem, u0map, pmap; build_initializeprob = true, + implicit_dae = false, t = nothing, guesses = AnyDict(), + warn_initialize_determined = true, initialization_eqs = [], + eval_expression = false, eval_module = @__MODULE__, fully_determined = false, + check_units = true, tofloat = true, use_union = false, + u0_constructor = identity, du0map = nothing, check_length = true, symbolic_u0 = false, kwargs...) + dvs = unknowns(sys) + ps = parameters(sys) + iv = has_iv(sys) ? get_iv(sys) : nothing + eqs = equations(sys) + + check_array_equations_unknowns(eqs, dvs) + + u0Type = typeof(u0map) + pType = typeof(pmap) + _u0map = u0map + u0map = to_varmap(u0map, dvs) + _pmap = pmap + pmap = to_varmap(pmap, ps) + defs = add_toterms(defaults(sys)) + cmap, cs = get_cmap(sys) + kwargs = NamedTuple(kwargs) + + op = add_toterms(u0map) + missing_unknowns = add_fallbacks!(op, dvs, defs) + for (k, v) in defs + haskey(op, k) && continue + op[k] = v + end + merge!(op, pmap) + missing_pars = add_fallbacks!(op, ps, defs) + for eq in cmap + op[eq.lhs] = eq.rhs + end + if sys isa ODESystem + guesses = merge(ModelingToolkit.guesses(sys), todict(guesses)) + has_observed_u0s = any( + k -> has_observed_with_lhs(sys, k) || has_parameter_dependency_with_lhs(sys, k), + keys(op)) + solvablepars = [p + for p in parameters(sys) + if is_parameter_solvable(p, pmap, defs, guesses)] + if build_initializeprob && + (((implicit_dae || has_observed_u0s || !isempty(missing_unknowns) || + !isempty(solvablepars)) && + get_tearing_state(sys) !== nothing) || + !isempty(initialization_equations(sys))) && t !== nothing + initializeprob = ModelingToolkit.InitializationProblem( + sys, t, u0map, pmap; guesses, warn_initialize_determined, + initialization_eqs, eval_expression, eval_module, fully_determined, check_units) + initializeprobmap = getu(initializeprob, unknowns(sys)) + + punknowns = [p + for p in all_variable_symbols(initializeprob) + if is_parameter(sys, p)] + getpunknowns = getu(initializeprob, punknowns) + setpunknowns = setp(sys, punknowns) + initializeprobpmap = GetUpdatedMTKParameters(getpunknowns, setpunknowns) + + reqd_syms = parameter_symbols(initializeprob) + update_initializeprob! = UpdateInitializeprob( + getu(sys, reqd_syms), setu(initializeprob, reqd_syms)) + for p in punknowns + p = unwrap(p) + stype = symtype(p) + op[p] = get_temporary_value(p) + delete!(missing_pars, p) + end + + for v in missing_unknowns + op[v] = zero_var(v) + end + empty!(missing_unknowns) + kwargs = merge(kwargs, + (; initializeprob, initializeprobmap, + initializeprobpmap, update_initializeprob!)) + end + end + + if t !== nothing && !(constructor <: Union{DDEFunction, SDDEFunction}) + op[iv] = t + end + + add_observed!(sys, op) + add_parameter_dependencies!(sys, op) + + evaluate_varmap!(op) + + u0 = better_varmap_to_vars( + op, dvs; tofloat = true, use_union = false, + container_type = u0Type, allow_symbolic = symbolic_u0) + + if u0 !== nothing + u0 = u0_constructor(u0) + end + + check_eqs_u0(eqs, dvs, u0; check_length, kwargs...) + + if is_split(sys) + p = MTKParameters(sys, op) + else + p = better_varmap_to_vars(op, ps; tofloat, use_union, container_type = pType) + end + + if implicit_dae && du0map !== nothing + ddvs = map(Differential(iv), dvs) + du0map = to_varmap(du0map, ddvs) + merge!(op, du0map) + + du0 = varmap_to_vars(du0map, ddvs; toterm = identity, + tofloat = true) + kwargs = merge(kwargs, (; ddvs)) + else + du0 = nothing + end + + f = constructor(sys, dvs, ps, u0; p = p, + eval_expression = eval_expression, + eval_module = eval_module, + kwargs...) + implicit_dae ? (f, du0, u0, p) : (f, u0, p) +end diff --git a/src/variables.jl b/src/variables.jl index 1c22540c63..c0c875450c 100644 --- a/src/variables.jl +++ b/src/variables.jl @@ -140,7 +140,7 @@ function varmap_to_vars(varmap, varlist; defaults = Dict(), check = true, if is_incomplete_initialization || isempty(varmap) if isempty(defaults) if !is_incomplete_initialization && check - isempty(varlist) || throw_missingvars(varlist) + isempty(varlist) || throw(MissingVariablesError(varlist)) end return nothing else diff --git a/test/initializationsystem.jl b/test/initializationsystem.jl index 08b653c22e..e819980752 100644 --- a/test/initializationsystem.jl +++ b/test/initializationsystem.jl @@ -487,7 +487,8 @@ sys = extend(sysx, sysy) @variables x(t) y(t) @named sys = ODESystem([x^2 + y^2 ~ 25, D(x) ~ 1], t) ssys = structural_simplify(sys) - @test_throws ArgumentError ODEProblem(ssys, [x => 3], (0, 1), []) # y should have a guess + @test_throws ModelingToolkit.MissingVariablesError ODEProblem( + ssys, [x => 3], (0, 1), []) # y should have a guess end # https://github.com/SciML/ModelingToolkit.jl/issues/3025 diff --git a/test/split_parameters.jl b/test/split_parameters.jl index b8651238ea..0c317cb154 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, use_union = true, build_initializeprob = false) -@test prob.p isa Tuple{Vector{Float64}, Vector{Int64}} +@test prob.p isa Union{Float64, Int64} sol = solve(prob, ImplicitEuler()); @test sol.retcode == ReturnCode.Success From 3c43431e6f2688e8c87bbe59ce86adb2a1bb7cb5 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 2 Oct 2024 16:17:07 +0530 Subject: [PATCH 0142/2176] refactor: move `get_u0_p` and `get_u0` to `problem_utils.jl` --- src/systems/diffeqs/abstractodesystem.jl | 91 ----------------------- src/systems/problem_utils.jl | 95 ++++++++++++++++++++++++ 2 files changed, 95 insertions(+), 91 deletions(-) diff --git a/src/systems/diffeqs/abstractodesystem.jl b/src/systems/diffeqs/abstractodesystem.jl index 48770e7a52..e0d4c72f2b 100644 --- a/src/systems/diffeqs/abstractodesystem.jl +++ b/src/systems/diffeqs/abstractodesystem.jl @@ -702,97 +702,6 @@ function ODEFunctionExpr{iip}(sys::AbstractODESystem, dvs = unknowns(sys), !linenumbers ? Base.remove_linenums!(ex) : ex end -""" - 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. -""" -function get_u0_p(sys, - u0map, - parammap = nothing; - t0 = nothing, - use_union = true, - tofloat = true, - symbolic_u0 = false) - dvs = unknowns(sys) - ps = parameters(sys) - - defs = defaults(sys) - if t0 !== nothing - defs[get_iv(sys)] = t0 - end - if parammap !== nothing - defs = mergedefaults(defs, parammap, ps) - end - if u0map isa Vector && eltype(u0map) <: Pair - u0map = Dict(u0map) - end - if u0map isa Dict - allobs = Set(getproperty.(observed(sys), :lhs)) - if any(in(allobs), keys(u0map)) - u0s_in_obs = filter(in(allobs), keys(u0map)) - @warn "Observed variables cannot be assigned initial values. Initial values for $u0s_in_obs will be ignored." - end - end - obs = filter!(x -> !(x[1] isa Number), map(x -> x.rhs => x.lhs, observed(sys))) - observedmap = isempty(obs) ? Dict() : todict(obs) - defs = mergedefaults(defs, observedmap, u0map, dvs) - for (k, v) in defs - if Symbolics.isarraysymbolic(k) - ks = scalarize(k) - length(ks) == length(v) || error("$k has default value $v with unmatched size") - for (kk, vv) in zip(ks, v) - if !haskey(defs, kk) - defs[kk] = vv - end - end - end - end - - 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) - end - 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 -end - -function get_u0( - sys, u0map, parammap = nothing; symbolic_u0 = false, - toterm = default_toterm, t0 = nothing, use_union = true) - dvs = unknowns(sys) - ps = parameters(sys) - defs = defaults(sys) - if t0 !== nothing - defs[get_iv(sys)] = t0 - end - if parammap !== nothing - defs = mergedefaults(defs, parammap, ps) - end - - # Convert observed equations "lhs ~ rhs" into defaults. - # Use the order "lhs => rhs" by default, but flip it to "rhs => lhs" - # if "lhs" is known by other means (parameter, another default, ...) - # TODO: Is there a better way to determine which equations to flip? - obs = map(x -> x.lhs => x.rhs, observed(sys)) - obs = map(x -> x[1] in keys(defs) ? reverse(x) : x, obs) - obs = filter!(x -> !(x[1] isa Number), obs) # exclude e.g. "0 => x^2 + y^2 - 25" - obsmap = isempty(obs) ? Dict() : todict(obs) - - defs = mergedefaults(defs, obsmap, u0map, dvs) - if symbolic_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) - end - t0 !== nothing && delete!(defs, get_iv(sys)) - return u0, defs -end - function ODEFunctionExpr(sys::AbstractODESystem, args...; kwargs...) ODEFunctionExpr{true}(sys, args...; kwargs...) end diff --git a/src/systems/problem_utils.jl b/src/systems/problem_utils.jl index b5de7b64d6..2b894eb7ee 100644 --- a/src/systems/problem_utils.jl +++ b/src/systems/problem_utils.jl @@ -501,3 +501,98 @@ function process_SciMLProblem( kwargs...) implicit_dae ? (f, du0, u0, p) : (f, u0, p) end + +############## +# Legacy functions for backward compatibility +############## + +""" + 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. +""" +function get_u0_p(sys, + u0map, + parammap = nothing; + t0 = nothing, + use_union = true, + tofloat = true, + symbolic_u0 = false) + dvs = unknowns(sys) + ps = parameters(sys) + + defs = defaults(sys) + if t0 !== nothing + defs[get_iv(sys)] = t0 + end + if parammap !== nothing + defs = mergedefaults(defs, parammap, ps) + end + if u0map isa Vector && eltype(u0map) <: Pair + u0map = Dict(u0map) + end + if u0map isa Dict + allobs = Set(getproperty.(observed(sys), :lhs)) + if any(in(allobs), keys(u0map)) + u0s_in_obs = filter(in(allobs), keys(u0map)) + @warn "Observed variables cannot be assigned initial values. Initial values for $u0s_in_obs will be ignored." + end + end + obs = filter!(x -> !(x[1] isa Number), map(x -> x.rhs => x.lhs, observed(sys))) + observedmap = isempty(obs) ? Dict() : todict(obs) + defs = mergedefaults(defs, observedmap, u0map, dvs) + for (k, v) in defs + if Symbolics.isarraysymbolic(k) + ks = scalarize(k) + length(ks) == length(v) || error("$k has default value $v with unmatched size") + for (kk, vv) in zip(ks, v) + if !haskey(defs, kk) + defs[kk] = vv + end + end + end + end + + 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) + end + 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 +end + +function get_u0( + sys, u0map, parammap = nothing; symbolic_u0 = false, + toterm = default_toterm, t0 = nothing, use_union = true) + dvs = unknowns(sys) + ps = parameters(sys) + defs = defaults(sys) + if t0 !== nothing + defs[get_iv(sys)] = t0 + end + if parammap !== nothing + defs = mergedefaults(defs, parammap, ps) + end + + # Convert observed equations "lhs ~ rhs" into defaults. + # Use the order "lhs => rhs" by default, but flip it to "rhs => lhs" + # if "lhs" is known by other means (parameter, another default, ...) + # TODO: Is there a better way to determine which equations to flip? + obs = map(x -> x.lhs => x.rhs, observed(sys)) + obs = map(x -> x[1] in keys(defs) ? reverse(x) : x, obs) + obs = filter!(x -> !(x[1] isa Number), obs) # exclude e.g. "0 => x^2 + y^2 - 25" + obsmap = isempty(obs) ? Dict() : todict(obs) + + defs = mergedefaults(defs, obsmap, u0map, dvs) + if symbolic_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) + end + t0 !== nothing && delete!(defs, get_iv(sys)) + return u0, defs +end From b0f2132ea2b0eccff1a7b8335a86204d48703220 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 14 Oct 2024 20:32:54 +0530 Subject: [PATCH 0143/2176] fix: handle array dummy derivatives in generate_initializesystem --- src/systems/nonlinear/initializesystem.jl | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/systems/nonlinear/initializesystem.jl b/src/systems/nonlinear/initializesystem.jl index 38a3095b5c..99e4e19d09 100644 --- a/src/systems/nonlinear/initializesystem.jl +++ b/src/systems/nonlinear/initializesystem.jl @@ -37,7 +37,7 @@ function generate_initializesystem(sys::ODESystem; # set dummy derivatives to default_dd_guess unless specified push!(defs, x[1] => get(guesses, x[1], default_dd_guess)) end - for (y, x) in u0map + function process_u0map_with_dummysubs(y, x) y = get(schedule.dummy_sub, y, y) y = fixpoint_sub(y, diffmap) if y ∈ vars_set @@ -53,6 +53,13 @@ function generate_initializesystem(sys::ODESystem; error("Initialization expression $y is currently not supported. If its a higher order derivative expression, then only the dummy derivative expressions are supported.") end end + for (y, x) in u0map + if Symbolics.isarraysymbolic(y) + process_u0map_with_dummysubs.(collect(y), collect(x)) + else + process_u0map_with_dummysubs(y, x) + end + end end # 2) process other variables From 49bcc8263586997bd3083321ad5a5e9112666a2e Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 14 Oct 2024 20:40:32 +0530 Subject: [PATCH 0144/2176] test: test initial values dependent on constants --- test/constants.jl | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/test/constants.jl b/test/constants.jl index bfdc83bafc..f2c4fdaa86 100644 --- a/test/constants.jl +++ b/test/constants.jl @@ -37,3 +37,16 @@ eqs = [D(x) ~ β] simp = structural_simplify(sys) @test isempty(MT.collect_constants(nothing)) + +@testset "Issue#3044" begin + @constants h = 1 + @parameters τ = 0.5 * h + @variables x(MT.t_nounits) = h + eqs = [MT.D_nounits(x) ~ (h - x) / τ] + + @mtkbuild fol_model = ODESystem(eqs, MT.t_nounits) + + prob = ODEProblem(fol_model, [], (0.0, 10.0)) + @test prob[x] ≈ 1 + @test prob.ps[τ] ≈ 0.5 +end From e67763ace865ac33b562086178bb361a40a501d6 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 15 Oct 2024 16:55:43 +0530 Subject: [PATCH 0145/2176] test: remove test for creating incomplete `SDEProblem` --- test/sdesystem.jl | 4 ---- 1 file changed, 4 deletions(-) diff --git a/test/sdesystem.jl b/test/sdesystem.jl index cae9ec9376..c258a4142b 100644 --- a/test/sdesystem.jl +++ b/test/sdesystem.jl @@ -36,10 +36,6 @@ solexpr = solve(eval(probexpr), SRIW1(), seed = 1) @test all(x -> x == 0, Array(sol - solexpr)) -# Test no error -@test_nowarn SDEProblem(de, nothing, (0, 10.0)) -@test SDEProblem(de, nothing).tspan == (0.0, 10.0) - noiseeqs_nd = [0.01*x 0.01*x*y 0.02*x*z σ 0.01*y 0.02*x*z ρ β 0.01*z] From 03e294f46c5fd7bca6ea9175ac57d54d96d1dc92 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 15 Oct 2024 16:55:57 +0530 Subject: [PATCH 0146/2176] test: refactor test for old parameter splitting --- 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 0c317cb154..22c90edf7a 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, use_union = true, build_initializeprob = false) -@test prob.p isa Union{Float64, Int64} +@test prob.p isa Vector{Union{Float64, Int64}} sol = solve(prob, ImplicitEuler()); @test sol.retcode == ReturnCode.Success From c69d28e0d829e0f9a21a56ea8c2364c052f0a367 Mon Sep 17 00:00:00 2001 From: Sam Isaacson Date: Tue, 15 Oct 2024 10:59:51 -0400 Subject: [PATCH 0147/2176] add collect_vars! equation dispatch --- src/systems/diffeqs/odesystem.jl | 9 +++---- .../discrete_system/discrete_system.jl | 9 +++---- src/systems/nonlinear/nonlinearsystem.jl | 9 +++---- src/utils.jl | 24 +++++++++++++------ 4 files changed, 26 insertions(+), 25 deletions(-) diff --git a/src/systems/diffeqs/odesystem.jl b/src/systems/diffeqs/odesystem.jl index 68ea0b48e3..d1cd01ce7b 100644 --- a/src/systems/diffeqs/odesystem.jl +++ b/src/systems/diffeqs/odesystem.jl @@ -319,8 +319,7 @@ function ODESystem(eqs, iv; kwargs...) compressed_eqs = Equation[] # equations that need to be expanded later, like `connect(a, b)` for eq in eqs eq.lhs isa Union{Symbolic, Number} || (push!(compressed_eqs, eq); continue) - collect_vars!(allunknowns, ps, eq.lhs, iv) - collect_vars!(allunknowns, ps, eq.rhs, iv) + collect_vars!(allunknowns, ps, eq, iv) if isdiffeq(eq) diffvar, _ = var_from_nested_derivative(eq.lhs) if check_scope_depth(getmetadata(diffvar, SymScope, LocalScope()), 0) @@ -337,11 +336,9 @@ function ODESystem(eqs, iv; kwargs...) end for eq in get(kwargs, :parameter_dependencies, Equation[]) if eq isa Pair - collect_vars!(allunknowns, ps, eq[1], iv) - collect_vars!(allunknowns, ps, eq[2], iv) + collect_vars!(allunknowns, ps, eq, iv) else - collect_vars!(allunknowns, ps, eq.lhs, iv) - collect_vars!(allunknowns, ps, eq.rhs, iv) + collect_vars!(allunknowns, ps, eq, iv) end end for ssys in get(kwargs, :systems, ODESystem[]) diff --git a/src/systems/discrete_system/discrete_system.jl b/src/systems/discrete_system/discrete_system.jl index 7103cfca80..99f76a8ce9 100644 --- a/src/systems/discrete_system/discrete_system.jl +++ b/src/systems/discrete_system/discrete_system.jl @@ -175,8 +175,7 @@ function DiscreteSystem(eqs, iv; kwargs...) ps = OrderedSet() iv = value(iv) for eq in eqs - collect_vars!(allunknowns, ps, eq.lhs, iv; op = Shift) - collect_vars!(allunknowns, ps, eq.rhs, iv; op = Shift) + collect_vars!(allunknowns, ps, eq, iv; op = Shift) if iscall(eq.lhs) && operation(eq.lhs) isa Shift isequal(iv, operation(eq.lhs).t) || throw(ArgumentError("A DiscreteSystem can only have one independent variable.")) @@ -187,11 +186,9 @@ function DiscreteSystem(eqs, iv; kwargs...) end for eq in get(kwargs, :parameter_dependencies, Equation[]) if eq isa Pair - collect_vars!(allunknowns, ps, eq[1], iv) - collect_vars!(allunknowns, ps, eq[2], iv) + collect_vars!(allunknowns, ps, eq, iv) else - collect_vars!(allunknowns, ps, eq.lhs, iv) - collect_vars!(allunknowns, ps, eq.rhs, iv) + collect_vars!(allunknowns, ps, eq, iv) end end new_ps = OrderedSet() diff --git a/src/systems/nonlinear/nonlinearsystem.jl b/src/systems/nonlinear/nonlinearsystem.jl index 46bf032d6f..c649b9b287 100644 --- a/src/systems/nonlinear/nonlinearsystem.jl +++ b/src/systems/nonlinear/nonlinearsystem.jl @@ -166,16 +166,13 @@ function NonlinearSystem(eqs; kwargs...) allunknowns = OrderedSet() ps = OrderedSet() for eq in eqs - collect_vars!(allunknowns, ps, eq.lhs, nothing) - collect_vars!(allunknowns, ps, eq.rhs, nothing) + collect_vars!(allunknowns, ps, eq, nothing) end for eq in get(kwargs, :parameter_dependencies, Equation[]) if eq isa Pair - collect_vars!(allunknowns, ps, eq[1], nothing) - collect_vars!(allunknowns, ps, eq[2], nothing) + collect_vars!(allunknowns, ps, eq, nothing) else - collect_vars!(allunknowns, ps, eq.lhs, nothing) - collect_vars!(allunknowns, ps, eq.rhs, nothing) + collect_vars!(allunknowns, ps, eq, nothing) end end new_ps = OrderedSet() diff --git a/src/utils.jl b/src/utils.jl index e8ed131d78..62445d5814 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -493,19 +493,16 @@ function collect_scoped_vars!(unknowns, parameters, sys, iv; depth = 1, op = Dif if has_eqs(sys) for eq in get_eqs(sys) eq isa Equation || continue - eq.lhs isa Union{Symbolic, Number} || continue - collect_vars!(unknowns, parameters, eq.lhs, iv; depth, op) - collect_vars!(unknowns, parameters, eq.rhs, iv; depth, op) + (eq isa Equation && eq.lhs isa Union{Symbolic, Number}) || continue + collect_vars!(unknowns, parameters, eq, iv; depth, op) end end if has_parameter_dependencies(sys) for eq in get_parameter_dependencies(sys) if eq isa Pair - collect_vars!(unknowns, parameters, eq[1], iv; depth, op) - collect_vars!(unknowns, parameters, eq[2], iv; depth, op) + collect_vars!(unknowns, parameters, eq, iv; depth, op) else - collect_vars!(unknowns, parameters, eq.lhs, iv; depth, op) - collect_vars!(unknowns, parameters, eq.rhs, iv; depth, op) + collect_vars!(unknowns, parameters, eq, iv; depth, op) end end end @@ -529,6 +526,19 @@ function collect_vars!(unknowns, parameters, expr, iv; depth = 0, op = Different return nothing end +function collect_vars!(unknowns, parameters, eq::Equation, iv; + depth = 0, op = Differential) + collect_vars!(unknowns, parameters, eq.lhs, iv; depth, op) + collect_vars!(unknowns, parameters, eq.rhs, iv; depth, op) + return nothing +end + +function collect_vars!(unknowns, parameters, p::Pair, iv; depth = 0, op = Differential) + collect_vars!(unknowns, parameters, p[1], iv; depth, op) + collect_vars!(unknowns, parameters, p[2], iv; depth, op) + return nothing +end + function collect_var!(unknowns, parameters, var, iv; depth = 0) isequal(var, iv) && return nothing check_scope_depth(getmetadata(var, SymScope, LocalScope()), depth) || return nothing From dbe211d80c4008726329d15ed029c8f21d4ec45e Mon Sep 17 00:00:00 2001 From: Sam Isaacson Date: Tue, 15 Oct 2024 11:07:17 -0400 Subject: [PATCH 0148/2176] add collect_vars equation dispatch --- src/utils.jl | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/utils.jl b/src/utils.jl index 62445d5814..1eaa2fa8eb 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -492,7 +492,7 @@ recursively searches through all subsystems of `sys`, increasing the depth if it function collect_scoped_vars!(unknowns, parameters, sys, iv; depth = 1, op = Differential) if has_eqs(sys) for eq in get_eqs(sys) - eq isa Equation || continue + eqtype_supports_collect_vars(eq) || continue (eq isa Equation && eq.lhs isa Union{Symbolic, Number}) || continue collect_vars!(unknowns, parameters, eq, iv; depth, op) end @@ -526,6 +526,16 @@ function collect_vars!(unknowns, parameters, expr, iv; depth = 0, op = Different return nothing end +""" + $(TYPEDSIGNATURES) + +Indicate whether the given equation type (Equation, Pair, etc) supports `collect_vars!`. Can +be dispatched by higher-level libraries to indicate support. +""" +eqtype_supports_collect_vars(eq) = false +eqtype_supports_collect_vars(eq::Equation) = true +eqtype_supports_collect_vars(eq::Pair) = true + function collect_vars!(unknowns, parameters, eq::Equation, iv; depth = 0, op = Differential) collect_vars!(unknowns, parameters, eq.lhs, iv; depth, op) From 1209b1e0fd0d9678520a5ee2c4a2b605ecd8179a Mon Sep 17 00:00:00 2001 From: Sam Isaacson Date: Tue, 15 Oct 2024 11:14:37 -0400 Subject: [PATCH 0149/2176] comment tweak --- src/utils.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/utils.jl b/src/utils.jl index 1eaa2fa8eb..5eb411a1e5 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -529,8 +529,8 @@ end """ $(TYPEDSIGNATURES) -Indicate whether the given equation type (Equation, Pair, etc) supports `collect_vars!`. Can -be dispatched by higher-level libraries to indicate support. +Indicate whether the given equation type (Equation, Pair, etc) supports `collect_vars!`. +Can be dispatched by higher-level libraries to indicate support. """ eqtype_supports_collect_vars(eq) = false eqtype_supports_collect_vars(eq::Equation) = true From 9cd326bc9b64654a2b7a39f73dedde1d259b896c Mon Sep 17 00:00:00 2001 From: Sam Isaacson Date: Tue, 15 Oct 2024 11:19:44 -0400 Subject: [PATCH 0150/2176] format --- src/utils.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utils.jl b/src/utils.jl index 5eb411a1e5..1072c37353 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -536,7 +536,7 @@ eqtype_supports_collect_vars(eq) = false eqtype_supports_collect_vars(eq::Equation) = true eqtype_supports_collect_vars(eq::Pair) = true -function collect_vars!(unknowns, parameters, eq::Equation, iv; +function collect_vars!(unknowns, parameters, eq::Equation, iv; depth = 0, op = Differential) collect_vars!(unknowns, parameters, eq.lhs, iv; depth, op) collect_vars!(unknowns, parameters, eq.rhs, iv; depth, op) From 557e20d194af7970142e98951887bcbf711cbb8b Mon Sep 17 00:00:00 2001 From: Sam Isaacson Date: Tue, 15 Oct 2024 11:24:11 -0400 Subject: [PATCH 0151/2176] fix check --- src/utils.jl | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/utils.jl b/src/utils.jl index 1072c37353..67074246b9 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -493,7 +493,9 @@ function collect_scoped_vars!(unknowns, parameters, sys, iv; depth = 1, op = Dif if has_eqs(sys) for eq in get_eqs(sys) eqtype_supports_collect_vars(eq) || continue - (eq isa Equation && eq.lhs isa Union{Symbolic, Number}) || continue + if eq isa Equation + eq.lhs isa Union{Symbolic, Number} || continue + end collect_vars!(unknowns, parameters, eq, iv; depth, op) end end From a8c09306c5bb4a2198a6936d461d2b4cc157faaa Mon Sep 17 00:00:00 2001 From: Sam Isaacson Date: Tue, 15 Oct 2024 12:15:25 -0400 Subject: [PATCH 0152/2176] format --- src/utils.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utils.jl b/src/utils.jl index 67074246b9..c13d8a480a 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -493,7 +493,7 @@ function collect_scoped_vars!(unknowns, parameters, sys, iv; depth = 1, op = Dif if has_eqs(sys) for eq in get_eqs(sys) eqtype_supports_collect_vars(eq) || continue - if eq isa Equation + if eq isa Equation eq.lhs isa Union{Symbolic, Number} || continue end collect_vars!(unknowns, parameters, eq, iv; depth, op) From 18617272fab3c7c022870c7d0a077124b96471bc Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 16 Oct 2024 11:25:41 +0530 Subject: [PATCH 0153/2176] refactor: use `process_SciMLProblem` in `jumpsystem.jl` --- src/systems/abstractsystem.jl | 2 +- src/systems/jumps/jumpsystem.jl | 41 +++++---------------------------- src/systems/problem_utils.jl | 12 ++++++++++ src/utils.jl | 11 ++++++++- 4 files changed, 29 insertions(+), 37 deletions(-) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index efec8b7fd8..b6e2a79c3e 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -2914,7 +2914,7 @@ function Base.eltype(::Type{<:TreeIterator{ModelingToolkit.AbstractSystem}}) end function check_array_equations_unknowns(eqs, dvs) - if any(eq -> Symbolics.isarraysymbolic(eq.lhs), eqs) + if any(eq -> eq isa Equation && Symbolics.isarraysymbolic(eq.lhs), eqs) throw(ArgumentError("The system has array equations. Call `structural_simplify` to handle such equations or scalarize them manually.")) end if any(x -> Symbolics.isarraysymbolic(x), dvs) diff --git a/src/systems/jumps/jumpsystem.jl b/src/systems/jumps/jumpsystem.jl index 60b061b1da..a714d4b364 100644 --- a/src/systems/jumps/jumpsystem.jl +++ b/src/systems/jumps/jumpsystem.jl @@ -348,20 +348,8 @@ function DiffEqBase.DiscreteProblem(sys::JumpSystem, u0map, tspan::Union{Tuple, if !iscomplete(sys) error("A completed `JumpSystem` is required. Call `complete` or `structural_simplify` on the system before creating a `DiscreteProblem`") end - dvs = unknowns(sys) - ps = parameters(sys) - - defs = defaults(sys) - defs = mergedefaults(defs, parammap, ps) - defs = mergedefaults(defs, u0map, dvs) - - u0 = varmap_to_vars(u0map, dvs; defaults = defs, tofloat = false) - 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) - end - + _, u0, p = process_SciMLProblem(EmptySciMLFunction, sys, u0map, parammap; + t = tspan === nothing ? nothing : tspan[1], use_union, tofloat = false, check_length = false) f = DiffEqBase.DISCRETE_INPLACE_DEFAULT observedfun = ObservedFunctionCache(sys; eval_expression, eval_module) @@ -399,16 +387,9 @@ function DiscreteProblemExpr{iip}(sys::JumpSystem, u0map, tspan::Union{Tuple, No if !iscomplete(sys) error("A completed `JumpSystem` is required. Call `complete` or `structural_simplify` on the system before creating a `DiscreteProblemExpr`") end - dvs = unknowns(sys) - ps = parameters(sys) - defs = defaults(sys) - u0 = varmap_to_vars(u0map, dvs; defaults = defs, tofloat = false) - 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) - end + _, u0, p = process_SciMLProblem(EmptySciMLFunction, sys, u0map, parammap; + t = tspan === nothing ? nothing : tspan[1], use_union, tofloat = false, check_length = false) # identity function to make syms works quote f = DiffEqBase.DISCRETE_INPLACE_DEFAULT @@ -454,19 +435,9 @@ function DiffEqBase.ODEProblem(sys::JumpSystem, u0map, tspan::Union{Tuple, Nothi if !iscomplete(sys) error("A completed `JumpSystem` is required. Call `complete` or `structural_simplify` on the system before creating a `DiscreteProblem`") end - dvs = unknowns(sys) - ps = parameters(sys) - - defs = defaults(sys) - defs = mergedefaults(defs, parammap, ps) - defs = mergedefaults(defs, u0map, dvs) - u0 = varmap_to_vars(u0map, dvs; defaults = defs, tofloat = false) - 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) - end + _, u0, p = process_SciMLProblem(EmptySciMLFunction, sys, u0map, parammap; + t = tspan === nothing ? nothing : tspan[1], use_union, tofloat = false, check_length = false) observedfun = ObservedFunctionCache(sys; eval_expression, eval_module) diff --git a/src/systems/problem_utils.jl b/src/systems/problem_utils.jl index 2b894eb7ee..e530e62eed 100644 --- a/src/systems/problem_utils.jl +++ b/src/systems/problem_utils.jl @@ -332,6 +332,18 @@ function get_temporary_value(p) end end +""" + $(TYPEDEF) + +A simple utility meant to be used as the `constructor` passed to `process_SciMLProblem` in +case constructing a SciMLFunction is not required. +""" +struct EmptySciMLFunction end + +function EmptySciMLFunction(args...; kwargs...) + return nothing +end + """ $(TYPEDSIGNATURES) diff --git a/src/utils.jl b/src/utils.jl index e8ed131d78..1e54e7047b 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -610,6 +610,15 @@ function collect_constants!(constants, expr::Symbolic) end end +function collect_constants!(constants, expr::Union{ConstantRateJump, VariableRateJump}) + collect_constants!(constants, expr.rate) + collect_constants!(constants, expr.affect!) +end + +function collect_constants!(constants, ::MassActionJump) + return constants +end + """ Replace symbolic constants with their literal values """ @@ -667,7 +676,7 @@ end function get_cmap(sys, exprs = nothing) #Inject substitutions for constants => values - cs = collect_constants([get_eqs(sys); get_observed(sys)]) #ctrls? what else? + cs = collect_constants([collect(get_eqs(sys)); get_observed(sys)]) #ctrls? what else? if !empty_substitutions(sys) cs = [cs; collect_constants(get_substitutions(sys).subs)] end From 713c1cb3588af44e10081988413050a01c0e4b80 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 16 Oct 2024 19:34:51 +0530 Subject: [PATCH 0154/2176] fix: handle parameter dependencies with constant RHS --- src/systems/diffeqs/odesystem.jl | 6 +----- src/utils.jl | 2 ++ test/odesystem.jl | 5 +++++ 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/src/systems/diffeqs/odesystem.jl b/src/systems/diffeqs/odesystem.jl index d1cd01ce7b..ec2c5f8157 100644 --- a/src/systems/diffeqs/odesystem.jl +++ b/src/systems/diffeqs/odesystem.jl @@ -335,11 +335,7 @@ function ODESystem(eqs, iv; kwargs...) end end for eq in get(kwargs, :parameter_dependencies, Equation[]) - if eq isa Pair - collect_vars!(allunknowns, ps, eq, iv) - else - collect_vars!(allunknowns, ps, eq, iv) - end + collect_vars!(allunknowns, ps, eq, iv) end for ssys in get(kwargs, :systems, ODESystem[]) collect_scoped_vars!(allunknowns, ps, ssys, iv) diff --git a/src/utils.jl b/src/utils.jl index 2ff6af2231..d2e8a3ea38 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -252,6 +252,7 @@ end function collect_defaults!(defs, vars) for v in vars + symbolic_type(v) == NotSymbolic() && continue if haskey(defs, v) || !hasdefault(unwrap(v)) || (def = getdefault(v)) === nothing continue end @@ -262,6 +263,7 @@ end function collect_var_to_name!(vars, xs) for x in xs + symbolic_type(x) == NotSymbolic() && continue x = unwrap(x) if hasmetadata(x, Symbolics.GetindexParent) xarr = getmetadata(x, Symbolics.GetindexParent) diff --git a/test/odesystem.jl b/test/odesystem.jl index d00d9228a5..9446d105e0 100644 --- a/test/odesystem.jl +++ b/test/odesystem.jl @@ -1442,3 +1442,8 @@ end end end end + +@testset "Parameter dependencies with constant RHS" begin + @parameters p + @test_nowarn ODESystem(Equation[], t; parameter_dependencies = [p ~ 1.0], name = :a) +end From da67c967947b79c1faaab7278f75fd69940e299b Mon Sep 17 00:00:00 2001 From: CompatHelper Julia Date: Thu, 17 Oct 2024 00:22:58 +0000 Subject: [PATCH 0155/2176] CompatHelper: bump compat for JuliaFormatter to 2, (keep existing compat) --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 808d68af06..143c72ba25 100644 --- a/Project.toml +++ b/Project.toml @@ -98,7 +98,7 @@ FunctionWrappers = "1.1" FunctionWrappersWrappers = "0.1" Graphs = "1.5.2" InteractiveUtils = "1" -JuliaFormatter = "1.0.47" +JuliaFormatter = "1.0.47, 2" JumpProcesses = "9.13.1" LabelledArrays = "1.3" Latexify = "0.11, 0.12, 0.13, 0.14, 0.15, 0.16" From 51af9ae121f62c21b670ba9d1c470d6e1e518fe7 Mon Sep 17 00:00:00 2001 From: Anant Thazhemadam Date: Thu, 17 Oct 2024 10:34:15 +0530 Subject: [PATCH 0156/2176] ci: test with `1`, `lts` and `pre` versions of julia --- .github/workflows/Tests.yml | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/.github/workflows/Tests.yml b/.github/workflows/Tests.yml index 93db3ac518..609ab2fb3d 100644 --- a/.github/workflows/Tests.yml +++ b/.github/workflows/Tests.yml @@ -25,6 +25,10 @@ jobs: strategy: fail-fast: false matrix: + version: + - "1" + - "lts" + - "pre" group: - InterfaceI - InterfaceII @@ -35,5 +39,6 @@ jobs: - RegressionI uses: "SciML/.github/.github/workflows/tests.yml@v1" with: + julia-version: "${{ matrix.version }}" group: "${{ matrix.group }}" secrets: "inherit" From 101d4dfc9d122b777d61974b9701d96d5f39f9d0 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 17 Oct 2024 17:42:25 +0530 Subject: [PATCH 0157/2176] docs: add tutorial for callable parameters --- docs/pages.jl | 3 +- docs/src/tutorials/callable_params.md | 91 +++++++++++++++++++++++++++ 2 files changed, 93 insertions(+), 1 deletion(-) create mode 100644 docs/src/tutorials/callable_params.md diff --git a/docs/pages.jl b/docs/pages.jl index d772c32471..2af487adf8 100644 --- a/docs/pages.jl +++ b/docs/pages.jl @@ -12,7 +12,8 @@ pages = [ "tutorials/parameter_identifiability.md", "tutorials/bifurcation_diagram_computation.md", "tutorials/SampledData.md", - "tutorials/domain_connections.md"], + "tutorials/domain_connections.md", + "tutorials/callable_params.md"], "Examples" => Any[ "Basic Examples" => Any["examples/higher_order.md", "examples/spring_mass.md", diff --git a/docs/src/tutorials/callable_params.md b/docs/src/tutorials/callable_params.md new file mode 100644 index 0000000000..74e7ea87fa --- /dev/null +++ b/docs/src/tutorials/callable_params.md @@ -0,0 +1,91 @@ +# Callable parameters and interpolating data + +ModelingToolkit.jl allows creating parameters that represent functions to be called. This +is especially useful for including interpolants and/or lookup tables inside ODEs. In this +tutorial we will create an `ODESystem` which employs callable parameters to interpolate data +inside an ODE and go over the various syntax options and their implications. + +## Callable parameter syntax + +The syntax for callable parameters declared via `@parameters` must be one of the following + + 1. `(fn::FType)(..)` + 2. `fn(::argType1, ::argType2, ...)` + +In the first case, the parameter is callable with any number/combination of arguments, and +has a type of `FType` (the callable must be a subtype of `FType`). In the second case, +the parameter is callable with as many arguments as declared, and all must match the +declared types. + +By default, the return type of the callable symbolic is inferred to be `Real`. To change +this, a `::retType` annotation can be added at the end. + +To declare a function that returns an array of values, the same array syntax can be used +as for normal variables: + +```julia +@parameters (foo::FType)(..)[1:3]::retType +@parameters foo(::argType1, ::argType2)[1:3]::retType +``` + +`retType` here is the `eltype` of the returned array. + +## Storage of callable parameters + +Callable parameters declared with the `::FType` syntax will be stored in a `Vector{FType}`. +Thus, if `FType` is non-concrete, the buffer will also be non-concrete. This is sometimes +necessary to allow the value of the callable to be switched out for a different type without +rebuilding the model. Typically this syntax is preferable when `FType` is concrete or +a small union. + +Callable parameters declared with the `::argType1, ...` syntax will be stored in a +`Vector{FunctionWrappers.FunctionWrapper{retType, Tuple{argType1, ...}}}`. This suffers +the small overhead of a `FunctionWrapper` and restricts the signature of the callable, +symbolic, but allows storing the parameter in a type-stable manner and swapping it out. +This is preferable when the values that the callable can take do not share a common +subtype. For example, when a callable can represent the activation of a neural network +and can be `tanh`, `sigmoid`, etc. which have a common ancestor of `Function`. + +If both `::FType` and `::argType`s are specified, `::FType` takes priority. For example, + +```julia +@parameters (p::LinearInterpolation)(::Real) +``` + +`p` will be stored in a `Vector{LinearInterpolation}`. If `::LinearInterpolation` was not +specified, it would be stored in a `Vector{FunctionWrapper{Real, Tuple{Real}}}`. + +## Example using interpolations + +```@example callable +using ModelingToolkit +using OrdinaryDiffEq +using DataInterpolations +using ModelingToolkit: t_nounits as t, D_nounits as D + +ts = collect(0.0:0.1:10.0) +spline = LinearInterpolation(ts .^ 2, ts) +Tspline = typeof(spline) +@variables x(t) +@parameters (interp::Tspline)(..) + +@mtkbuild sys = ODESystem(D(x) ~ interp(t), t) +``` + +The derivative of `x` is obtained via an interpolation from DataInterpolations.jl. Note +the parameter syntax. The `(..)` marks the parameter as callable. `(interp::Tspline)` +indicates that the parameter is of type `Tspline`. + +```@example callable +prob = ODEProblem(sys, [x => 0.0], (0.0, 1.0), [interp => spline]) +solve(prob) +``` + +Note that the the following will not work: + +```julia +ODEProblem( + sys; [x => 0.0], (0.0, 1.0), [interp => LinearInterpolation(0.0:0.1:1.0, 0.0:0.1:1.0)]) +``` + +Since the type of the spline doesn't match. From ce739406c461ee55aa939da18f6377ce0cce82d0 Mon Sep 17 00:00:00 2001 From: Venkateshprasad <32921645+ven-k@users.noreply.github.com> Date: Tue, 15 Oct 2024 12:00:17 +0000 Subject: [PATCH 0158/2176] feat: extend all arguments of a base sys to sys --- docs/src/basics/MTKLanguage.md | 26 ++++++++------- src/systems/model_parsing.jl | 59 +++++++++++++++++++++++++--------- test/model_parsing.jl | 30 ++++++++++++++--- 3 files changed, 84 insertions(+), 31 deletions(-) diff --git a/docs/src/basics/MTKLanguage.md b/docs/src/basics/MTKLanguage.md index 685c549429..537e1b0349 100644 --- a/docs/src/basics/MTKLanguage.md +++ b/docs/src/basics/MTKLanguage.md @@ -73,7 +73,7 @@ end v_array(t)[1:N, 1:M] v_for_defaults(t) end - @extend ModelB(; p1) + @extend ModelB(p1 = 1) @components begin model_a = ModelA(; k_array) model_array_a = [ModelA(; k = i) for i in 1:N] @@ -149,14 +149,18 @@ julia> ModelingToolkit.getdefault(model_c1.v) #### `@extend` begin block - - Partial systems can be extended in a higher system as `@extend PartialSystem(; kwargs)`. - - Keyword arguments pf partial system in the `@extend` definition are added as the keyword arguments of the base system. - - Note that in above example, `p1` is promoted as an argument of `ModelC`. Users can set the value of `p1`. However, as `p2` isn't listed in the model definition, its initial guess can't be specified while creating an instance of `ModelC`. +Partial systems can be extended in a higher system in two ways: -```julia -julia> @mtkbuild model_c2 = ModelC(; p1 = 2.0) + - `@extend PartialSystem(var1 = value1)` + + + This is the recommended way of extending a base system. + + The default values for the arguments of the base system can be declared in the `@extend` statement. + + Note that all keyword arguments of the base system are added as the keyword arguments of the main system. -``` + - `@extend var_to_unpack1, var_to_unpack2 = partial_sys = PartialSystem(var1 = value1)` + + + In this method: explicitly list the variables that should be unpacked, provide a name for the partial system and declare the base system. + + Note that only the arguments listed out in the declaration of the base system (here: `var1`) are added as the keyword arguments of the higher system. #### `@components` begin block @@ -325,11 +329,11 @@ For example, the structure of `ModelC` is: julia> ModelC.structure Dict{Symbol, Any} with 10 entries: :components => Any[Union{Expr, Symbol}[:model_a, :ModelA], Union{Expr, Symbol}[:model_array_a, :ModelA, :(1:N)], Union{Expr, Symbol}[:model_array_b, :ModelA, :(1:N)]] - :variables => Dict{Symbol, Dict{Symbol, Any}}(:v=>Dict(:default=>:v_var, :type=>Real), :v_for_defaults=>Dict(:type=>Real)) + :variables => Dict{Symbol, Dict{Symbol, Any}}(:v=>Dict(:default=>:v_var, :type=>Real), :v_array=>Dict(:value=>nothing, :type=>Real, :size=>(:N, :M)), :v_for_defaults=>Dict(:type=>Real)) :icon => URI("https://github.com/SciML/SciMLDocs/blob/main/docs/src/assets/logo.png") - :kwargs => Dict{Symbol, Dict}(:f => Dict(:value => :sin), :N => Dict(:value => 2), :M => Dict(:value => 3), :v => Dict{Symbol, Any}(:value => :v_var, :type => Real), :v_for_defaults => Dict{Symbol, Union{Nothing, DataType}}(:value => nothing, :type => Real), :p1 => Dict(:value => nothing)), - :structural_parameters => Dict{Symbol, Dict}(:f => Dict(:value => :sin), :N => Dict(:value => 2), :M => Dict(:value => 3)) - :independent_variable => t + :kwargs => Dict{Symbol, Dict}(:f=>Dict(:value=>:sin), :p2=>Dict(:value=>NoValue()), :N=>Dict(:value=>2), :M=>Dict(:value=>3), :v=>Dict{Symbol, Any}(:value=>:v_var, :type=>Real), :v_array=>Dict{Symbol, Any}(:value=>nothing, :type=>Real, :size=>(:N, :M)), :v_for_defaults=>Dict{Symbol, Union{Nothing, DataType}}(:value=>nothing, :type=>Real), :p1=>Dict(:value=>1)) + :structural_parameters => Dict{Symbol, Dict}(:f=>Dict(:value=>:sin), :N=>Dict(:value=>2), :M=>Dict(:value=>3)) + :independent_variable => :t :constants => Dict{Symbol, Dict}(:c=>Dict{Symbol, Any}(:value=>1, :type=>Int64, :description=>"Example constant.")) :extend => Any[[:p2, :p1], Symbol("#mtkmodel__anonymous__ModelB"), :ModelB] :defaults => Dict{Symbol, Any}(:v_for_defaults=>2.0) diff --git a/src/systems/model_parsing.jl b/src/systems/model_parsing.jl index 1298d72506..ab70646824 100644 --- a/src/systems/model_parsing.jl +++ b/src/systems/model_parsing.jl @@ -717,7 +717,7 @@ function parse_structural_parameters!(exprs, sps, dict, mod, body, kwargs) end end -function extend_args!(a, b, dict, expr, kwargs, varexpr, has_param = false) +function extend_args!(a, b, dict, expr, kwargs, has_param = false) # Whenever `b` is a function call, skip the first arg aka the function name. # Whenever it is a kwargs list, include it. start = b.head == :call ? 2 : 1 @@ -738,18 +738,18 @@ function extend_args!(a, b, dict, expr, kwargs, varexpr, has_param = false) dict[:kwargs][x] = Dict(:value => nothing) end Expr(:kw, x) => begin + b.args[i] = Expr(:kw, x, x) push!(kwargs, Expr(:kw, x, nothing)) dict[:kwargs][x] = Dict(:value => nothing) end Expr(:kw, x, y) => begin b.args[i] = Expr(:kw, x, x) - push!(varexpr.args, :($x = $x === nothing ? $y : $x)) - push!(kwargs, Expr(:kw, x, nothing)) - dict[:kwargs][x] = Dict(:value => nothing) + push!(kwargs, Expr(:kw, x, y)) + dict[:kwargs][x] = Dict(:value => y) end Expr(:parameters, x...) => begin has_param = true - extend_args!(a, arg, dict, expr, kwargs, varexpr, has_param) + extend_args!(a, arg, dict, expr, kwargs, has_param) end _ => error("Could not parse $arg of component $a") end @@ -758,17 +758,40 @@ end const EMPTY_DICT = Dict() const EMPTY_VoVoSYMBOL = Vector{Symbol}[] +const EMPTY_VoVoVoSYMBOL = Vector{Symbol}[[]] -function Base.names(model::Model) +function _arguments(model::Model) vars = keys(get(model.structure, :variables, EMPTY_DICT)) vars = union(vars, keys(get(model.structure, :parameters, EMPTY_DICT))) - vars = union(vars, - map(first, get(model.structure, :components, EMPTY_VoVoSYMBOL))) + vars = union(vars, first(get(model.structure, :extend, EMPTY_VoVoVoSYMBOL))) collect(vars) end -function _parse_extend!(ext, a, b, dict, expr, kwargs, varexpr, vars) - extend_args!(a, b, dict, expr, kwargs, varexpr) +function Base.names(model::Model) + collect(union(_arguments(model), + map(first, get(model.structure, :components, EMPTY_VoVoSYMBOL)))) +end + +function _parse_extend!(ext, a, b, dict, expr, kwargs, vars, additional_args) + extend_args!(a, b, dict, expr, kwargs) + + # `additional_args` doubles as a flag to check the mode of `@extend`. It is + # `nothing` for explicit destructuring. + # The following block modifies the arguments of both base and higher systems + # for the implicit extend statements. + if additional_args !== nothing + b.args = [b.args[1]] + allvars = [additional_args.args..., vars.args...] + push!(b.args, Expr(:parameters)) + for var in allvars + push!(b.args[end].args, var) + if !haskey(dict[:kwargs], var) + push!(dict[:kwargs], var => Dict(:value => NO_VALUE)) + push!(kwargs, Expr(:kw, var, NO_VALUE)) + end + end + end + ext[] = a push!(b.args, Expr(:kw, :name, Meta.quot(a))) push!(expr.args, :($a = $b)) @@ -780,8 +803,6 @@ end function parse_extend!(exprs, ext, dict, mod, body, kwargs) expr = Expr(:block) - varexpr = Expr(:block) - push!(exprs, varexpr) push!(exprs, expr) body = deepcopy(body) MLStyle.@match body begin @@ -792,7 +813,9 @@ function parse_extend!(exprs, ext, dict, mod, body, kwargs) error("`@extend` destructuring only takes an tuple as LHS. Got $body") end a, b = b.args - _parse_extend!(ext, a, b, dict, expr, kwargs, varexpr, vars) + # This doubles as a flag to identify the mode of `@extend` + additional_args = nothing + _parse_extend!(ext, a, b, dict, expr, kwargs, vars, additional_args) else error("When explicitly destructing in `@extend` please use the syntax: `@extend a, b = oneport = OnePort()`.") end @@ -802,8 +825,11 @@ function parse_extend!(exprs, ext, dict, mod, body, kwargs) b = body if (model = getproperty(mod, b.args[1])) isa Model vars = Expr(:tuple) - append!(vars.args, names(model)) - _parse_extend!(ext, a, b, dict, expr, kwargs, varexpr, vars) + append!(vars.args, _arguments(model)) + additional_args = Expr(:tuple) + append!(additional_args.args, + keys(get(model.structure, :structural_parameters, EMPTY_DICT))) + _parse_extend!(ext, a, b, dict, expr, kwargs, vars, additional_args) else error("Cannot infer the exact `Model` that `@extend $(body)` refers." * " Please specify the names that it brings into scope by:" * @@ -1104,7 +1130,7 @@ function parse_icon!(body::String, dict, icon, mod) icon_dir = get(ENV, "MTK_ICONS_DIR", joinpath(DEPOT_PATH[1], "mtk_icons")) dict[:icon] = icon[] = if isfile(body) URI("file:///" * abspath(body)) - elseif (iconpath = joinpath(icon_dir, body); isfile(iconpath)) + elseif (iconpath = abspath(joinpath(icon_dir, body)); isfile(iconpath)) URI("file:///" * abspath(iconpath)) elseif try Base.isvalid(URI(body)) @@ -1115,6 +1141,7 @@ function parse_icon!(body::String, dict, icon, mod) elseif (_body = lstrip(body); startswith(_body, r"<\?xml| Dict(:type => Real)) @test A.structure[:extend] == [[:e], :extended_e, :E] @test A.structure[:equations] == ["e ~ 0"] - @test A.structure[:kwargs] == - Dict{Symbol, Dict}(:p => Dict(:value => nothing, :type => Real), - :v => Dict(:value => nothing, :type => Real)) + @test A.structure[:kwargs] == Dict{Symbol, Dict}( + :p => Dict{Symbol, Union{Nothing, DataType}}(:value => nothing, :type => Real), + :v => Dict{Symbol, Union{Nothing, DataType}}(:value => nothing, :type => Real)) @test A.structure[:components] == [[:cc, :C]] end @@ -910,3 +910,25 @@ end end), false) end + +@mtkmodel BaseSys begin + @parameters begin + p1 + p2 + end + @variables begin + v1(t) + end +end + +@testset "Arguments of base system" begin + @mtkmodel MainSys begin + @extend BaseSys(p1 = 1) + end + + @test names(MainSys) == [:p2, :p1, :v1] + @named main_sys = MainSys(p1 = 11, p2 = 12, v1 = 13) + @test getdefault(main_sys.p1) == 11 + @test getdefault(main_sys.p2) == 12 + @test getdefault(main_sys.v1) == 13 +end From a6f781d45cbbc885edf9167695682124cd9655bb Mon Sep 17 00:00:00 2001 From: Ben Chung Date: Thu, 1 Aug 2024 20:36:50 -0700 Subject: [PATCH 0159/2176] First pass at MutatingFunctionalAffect --- Project.toml | 1 + src/ModelingToolkit.jl | 1 + src/systems/callbacks.jl | 144 +++++++++++++++++++++++++++++-- src/systems/diffeqs/odesystem.jl | 27 +++++- test/symbolic_events.jl | 49 +++++++++++ 5 files changed, 215 insertions(+), 7 deletions(-) diff --git a/Project.toml b/Project.toml index 808d68af06..729966fde0 100644 --- a/Project.toml +++ b/Project.toml @@ -9,6 +9,7 @@ ArrayInterface = "4fba245c-0d91-5ea0-9b3e-6abc04ee57a9" BlockArrays = "8e7c35d0-a365-5155-bbbb-fb81a777f24e" Combinatorics = "861a8166-3701-5b0c-9a16-15d98fcdc6aa" Compat = "34da2185-b29b-5c13-b0c7-acf172513d20" +ComponentArrays = "b0b7db55-cfe3-40fc-9ded-d10e2dbeff66" ConstructionBase = "187b0558-2788-49d3-abe0-74a17ed4e7c9" DataStructures = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8" DiffEqBase = "2b5f629d-d688-5b77-993f-72d75c75574e" diff --git a/src/ModelingToolkit.jl b/src/ModelingToolkit.jl index 2f57bb1765..f5262a1526 100644 --- a/src/ModelingToolkit.jl +++ b/src/ModelingToolkit.jl @@ -54,6 +54,7 @@ using Reexport using RecursiveArrayTools import Graphs: SimpleDiGraph, add_edge!, incidence_matrix import BlockArrays: BlockedArray, Block, blocksize, blocksizes +import ComponentArrays using RuntimeGeneratedFunctions using RuntimeGeneratedFunctions: drop_expr diff --git a/src/systems/callbacks.jl b/src/systems/callbacks.jl index 86cab57634..f7d4baa4cb 100644 --- a/src/systems/callbacks.jl +++ b/src/systems/callbacks.jl @@ -60,8 +60,6 @@ function Base.hash(a::FunctionalAffect, s::UInt) hash(a.ctx, s) end -has_functional_affect(cb) = affects(cb) isa FunctionalAffect - namespace_affect(affect, s) = namespace_equation(affect, s) function namespace_affect(affect::FunctionalAffect, s) FunctionalAffect(func(affect), @@ -73,6 +71,67 @@ function namespace_affect(affect::FunctionalAffect, s) context(affect)) end +""" +`MutatingFunctionalAffect` differs from `FunctionalAffect` in two key ways: +* First, insetad of the `u` vector passed to `f` being a vector of indices into `integ.u` it's instead the result of evaluating `obs` at the current state, named as specified in `obs_syms`. This allows affects to easily access observed states and decouples affect inputs from the system structure. +* Second, it abstracts the assignment back to system states away. Instead of writing `integ.u[u.myvar] = [whatever]`, you instead declare in `mod_params` that you want to modify `myvar` and then either (out of place) return a named tuple with `myvar` or (in place) modify the associated element in the ComponentArray that's given. +Initially, we only support "flat" states in `modified`; these states will be marked as irreducible in the overarching system and they will simply be bulk assigned at mutation. In the future, this will be extended to perform a nonlinear solve to further decouple the affect from the system structure. +""" +@kwdef struct MutatingFunctionalAffect + f::Any + obs::Vector + obs_syms::Vector{Symbol} + modified::Vector + mod_syms::Vector{Symbol} + ctx::Any +end + +MutatingFunctionalAffect(f::Function; + observed::NamedTuple = NamedTuple{()}(()), + modified::NamedTuple = NamedTuple{()}(()), + ctx=nothing) = MutatingFunctionalAffect(f, collect(values(observed)), collect(keys(observed)), collect(values(modified)), collect(keys(modified)), ctx) +MutatingFunctionalAffect(f::Function, observed::NamedTuple; modified::NamedTuple = NamedTuple{()}(()), ctx=nothing) = + MutatingFunctionalAffect(f, observed=observed, modified=modified, ctx=ctx) +MutatingFunctionalAffect(f::Function, observed::NamedTuple, modified::NamedTuple; ctx=nothing) = + MutatingFunctionalAffect(f, observed=observed, modified=modified, ctx=ctx) +MutatingFunctionalAffect(f::Function, observed::NamedTuple, modified::NamedTuple, ctx) = + MutatingFunctionalAffect(f, observed=observed, modified=modified, ctx=ctx) + +func(f::MutatingFunctionalAffect) = f.f +context(a::MutatingFunctionalAffect) = a.ctx +observed(a::MutatingFunctionalAffect) = a.obs +observed_syms(a::MutatingFunctionalAffect) = a.obs_syms +discretes(a::MutatingFunctionalAffect) = filter(ModelingToolkit.isparameter, a.modified) +modified(a::MutatingFunctionalAffect) = a.modified +modified_syms(a::MutatingFunctionalAffect) = a.mod_syms + +function Base.:(==)(a1::MutatingFunctionalAffect, a2::MutatingFunctionalAffect) + isequal(a1.f, a2.f) && isequal(a1.obs, a2.obs) && isequal(a1.modified, a2.modified) && + isequal(a1.obs_syms, a2.obs_syms) && isequal(a1.mod_syms, a2.mod_syms)&& isequal(a1.ctx, a2.ctx) +end + +function Base.hash(a::MutatingFunctionalAffect, s::UInt) + s = hash(a.f, s) + s = hash(a.obs, s) + s = hash(a.obs_syms, s) + s = hash(a.modified, s) + s = hash(a.mod_syms, s) + hash(a.ctx, s) +end + +function namespace_affect(affect::MutatingFunctionalAffect, s) + MutatingFunctionalAffect(func(affect), + renamespace.((s,), observed(affect)), + observed_syms(affect), + renamespace.((s,), modified(affect)), + modified_syms(affect), + context(affect)) +end + +function has_functional_affect(cb) + (affects(cb) isa FunctionalAffect || affects(cb) isa MutatingFunctionalAffect) +end + #################################### continuous events ##################################### const NULL_AFFECT = Equation[] @@ -109,8 +168,8 @@ Affects (i.e. `affect` and `affect_neg`) can be specified as either: """ struct SymbolicContinuousCallback eqs::Vector{Equation} - affect::Union{Vector{Equation}, FunctionalAffect} - affect_neg::Union{Vector{Equation}, FunctionalAffect, Nothing} + affect::Union{Vector{Equation}, FunctionalAffect, MutatingFunctionalAffect} + affect_neg::Union{Vector{Equation}, FunctionalAffect, MutatingFunctionalAffect, Nothing} rootfind::SciMLBase.RootfindOpt function SymbolicContinuousCallback(; eqs::Vector{Equation}, affect = NULL_AFFECT, affect_neg = affect, rootfind = SciMLBase.LeftRootFind) @@ -250,6 +309,7 @@ scalarize_affects(affects) = scalarize(affects) scalarize_affects(affects::Tuple) = FunctionalAffect(affects...) scalarize_affects(affects::NamedTuple) = FunctionalAffect(; affects...) scalarize_affects(affects::FunctionalAffect) = affects +scalarize_affects(affects::MutatingFunctionalAffect) = affects SymbolicDiscreteCallback(p::Pair) = SymbolicDiscreteCallback(p[1], p[2]) SymbolicDiscreteCallback(cb::SymbolicDiscreteCallback) = cb # passthrough @@ -257,7 +317,7 @@ SymbolicDiscreteCallback(cb::SymbolicDiscreteCallback) = cb # passthrough function Base.show(io::IO, db::SymbolicDiscreteCallback) println(io, "condition: ", db.condition) println(io, "affects:") - if db.affects isa FunctionalAffect + if db.affects isa FunctionalAffect || db.affects isa MutatingFunctionalAffect # TODO println(io, " ", db.affects) else @@ -749,6 +809,80 @@ function compile_user_affect(affect::FunctionalAffect, cb, sys, dvs, ps; kwargs. end end +invalid_variables(sys, expr) = filter(x -> !any(isequal(x), all_symbols(sys)), vars(expr)) +function unassignable_variables(sys, expr) + assignable_syms = vcat(unknowns(sys), parameters(sys)) + return filter(x -> !any(isequal(x), assignable_syms), vars(expr)) +end + +function compile_user_affect(affect::MutatingFunctionalAffect, sys, dvs, ps; kwargs...) + #= + Implementation sketch: + generate observed function (oop), should save to a component array under obs_syms + do the same stuff as the normal FA for pars_syms + call the affect method - test if it's OOP or IP using applicable + unpack and apply the resulting values + =# + obs_exprs = observed(affect) + for oexpr in obs_exprs + invalid_vars = invalid_variables(sys, oexpr) + if length(invalid_vars) > 0 + error("Observed equation $(oexpr) in affect refers to missing variable(s) $(invalid_vars); the variables may not have been added (e.g. if a component is missing).") + end + end + obs_syms = observed_syms(affect) + obs_size = size.(obs_exprs) # we will generate a work buffer of a ComponentArray that maps obs_syms to arrays of size obs_size + + mod_exprs = modified(affect) + for mexpr in mod_exprs + if !is_observed(sys, mexpr) && parameter_index(sys, mexpr) === nothing + error("Expression $mexpr cannot be assigned to; currently only unknowns and parameters may be updated by an affect.") + end + invalid_vars = unassignable_variables(sys, mexpr) + if length(invalid_vars) > 0 + error("Observed equation $(mexpr) in affect refers to missing variable(s) $(invalid_vars); the variables may not have been added (e.g. if a component is missing) or they may have been reduced away.") + end + end + mod_syms = modified_syms(affect) + _, mod_og_val_fun = build_explicit_observed_function(sys, mod_exprs; return_inplace=true) + + # sanity checks done! now build the data and update function for observed values + mkzero(sz) = if sz === () 0.0 else zeros(sz) end + _, obs_fun = build_explicit_observed_function(sys, reduce(vcat, Symbolics.scalarize.(obs_exprs); init = []); return_inplace=true) + obs_component_array = ComponentArrays.ComponentArray(NamedTuple{(obs_syms..., )}(mkzero.(obs_size))) + + # okay so now to generate the stuff to assign it back into the system + # note that we reorder the componentarray to make the views coherent wrt the base array + mod_pairs = mod_exprs .=> mod_syms + mod_param_pairs = filter(v -> is_parameter(sys, v[1]), mod_pairs) + mod_unk_pairs = filter(v -> !is_parameter(sys, v[1]), mod_pairs) + _, mod_og_val_fun = build_explicit_observed_function(sys, reduce(vcat, [first.(mod_param_pairs); first.(mod_unk_pairs)]; init = []); return_inplace=true) + upd_params_fun = setu(sys, reduce(vcat, Symbolics.scalarize.(first.(mod_param_pairs)); init = [])) + upd_unk_fun = setu(sys, reduce(vcat, Symbolics.scalarize.(first.(mod_unk_pairs)); init = [])) + + upd_component_array = ComponentArrays.ComponentArray(NamedTuple{([last.(mod_param_pairs); last.(mod_unk_pairs)]...,)}( + [collect(mkzero(size(e)) for e in first.(mod_param_pairs)); + collect(mkzero(size(e)) for e in first.(mod_unk_pairs))])) + upd_params_view = view(upd_component_array, last.(mod_param_pairs)) + upd_unks_view = view(upd_component_array, last.(mod_unk_pairs)) + let user_affect = func(affect), ctx = context(affect) + function (integ) + # update the to-be-mutated values; this ensures that if you do a no-op then nothing happens + mod_og_val_fun(upd_component_array, integ.u, integ.p..., integ.t) + + # update the observed values + obs_fun(obs_component_array, integ.u, integ.p..., integ.t) + + # let the user do their thing + user_affect(upd_component_array, obs_component_array, integ, ctx) + + # write the new values back to the integrator + upd_params_fun(integ, upd_params_view) + upd_unk_fun(integ, upd_unks_view) + end + end +end + function compile_affect(affect::FunctionalAffect, cb, sys, dvs, ps; kwargs...) compile_user_affect(affect, cb, sys, dvs, ps; kwargs...) end diff --git a/src/systems/diffeqs/odesystem.jl b/src/systems/diffeqs/odesystem.jl index ec2c5f8157..daa4321ed0 100644 --- a/src/systems/diffeqs/odesystem.jl +++ b/src/systems/diffeqs/odesystem.jl @@ -404,8 +404,31 @@ ODESystem(eq::Equation, args...; kwargs...) = ODESystem([eq], args...; kwargs... """ $(SIGNATURES) -Build the observed function assuming the observed equations are all explicit, -i.e. there are no cycles. +Generates a function that computes the observed value(s) `ts` in the system `sys` assuming that there are no cycles in the equations. + +The return value will be either: +* a single function if the input is a scalar or if the input is a Vector but `return_inplace` is false +* the out of place and in-place functions `(ip, oop)` if `return_inplace` is true and the input is a `Vector` + +The function(s) will be: +* `RuntimeGeneratedFunction`s by default, +* A Julia `Expr` if `expression` is true, +* A directly evaluated Julia function in the module `eval_module` if `eval_expression` is true + +The signatures will be of the form `g(...)` with arguments: +* `output` for in-place functions +* `unknowns` if `params_only` is `false` +* `inputs` if `inputs` is an array of symbolic inputs that should be available in `ts` +* `p...` unconditionally; note that in the case of `MTKParameters` more than one parameters argument may be present, so it must be splatted +* `t` if the system is time-dependent; for example `NonlinearSystem` will not have `t` +For example, a function `g(op, unknowns, p, inputs, t)` will be the in-place function generated if `return_inplace` is true, `ts` is a vector, an array of inputs `inputs` is given, and `params_only` is false for a time-dependent system. + +Options not otherwise specified are: +* `output_type = Array` the type of the array generated by the out-of-place vector-valued function +* `checkbounds = true` checks bounds if true when destructuring parameters +* `op = Operator` sets the recursion terminator for the walk done by `vars` to identify the variables that appear in `ts`. See the documentation for `vars` for more detail. +* `throw = true` if true, throw an error when generating a function for `ts` that reference variables that do not exist +* `drop_expr` is deprecated. """ function build_explicit_observed_function(sys, ts; inputs = nothing, diff --git a/test/symbolic_events.jl b/test/symbolic_events.jl index e1d12814ef..351f8111ee 100644 --- a/test/symbolic_events.jl +++ b/test/symbolic_events.jl @@ -887,3 +887,52 @@ end @test sol[b] == [2.0, 5.0, 5.0] @test sol[c] == [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0] end +@testset "Heater" begin + @variables temp(t) + params = @parameters furnace_on_threshold=0.5 furnace_off_threshold=0.7 furnace_power=1.0 leakage=0.1 furnace_on::Bool=false + eqs = [ + D(temp) ~ furnace_on * furnace_power - temp^2 * leakage + ] + + furnace_off = ModelingToolkit.SymbolicContinuousCallback([temp ~ furnace_off_threshold], + ModelingToolkit.MutatingFunctionalAffect(modified=(; furnace_on)) do x, o, i, c + x.furnace_on = false + end) + furnace_enable = ModelingToolkit.SymbolicContinuousCallback([temp ~ furnace_on_threshold], + ModelingToolkit.MutatingFunctionalAffect(modified=(; furnace_on)) do x, o, i, c + x.furnace_on = true + end) + + @named sys = ODESystem(eqs, t, [temp], params; continuous_events = [furnace_off, furnace_enable]) + ss = structural_simplify(sys) + prob = ODEProblem(ss, [temp => 0.0, furnace_on => true], (0.0, 100.0)) + sol = solve(prob, Tsit5(); dtmax=0.01) + @test all(sol[temp][sol.t .> 1.0] .<= 0.79) && all(sol[temp][sol.t .> 1.0] .>= 0.49) +end + +@testset "Quadrature" begin + @variables theta(t) omega(t) + params = @parameters qA=0 qB=0 + eqs = [ + D(theta) ~ omega + omega ~ sin(0.5*t) + ] + qAevt = ModelingToolkit.SymbolicContinuousCallback([cos(1000 * theta) ~ 0], + ModelingToolkit.MutatingFunctionalAffect(modified=(; qA)) do x, o, i, c + x.qA = 1 + end, + affect_neg = ModelingToolkit.MutatingFunctionalAffect(modified=(; qA)) do x, o, i, c + x.qA = 0 + end) + qBevt = ModelingToolkit.SymbolicContinuousCallback([cos(1000 * theta + π/2) ~ 0], + ModelingToolkit.MutatingFunctionalAffect(modified=(; qB)) do x, o, i, c + x.qB = 1 + end, + affect_neg = ModelingToolkit.MutatingFunctionalAffect(modified=(; qB)) do x, o, i, c + x.qB = 0 + end) + @named sys = ODESystem(eqs, t, [theta, omega], params; continuous_events = [qAevt, qBevt]) + ss = structural_simplify(sys) + prob = ODEProblem(ss, [theta => 0.0], (0.0, 1.0)) + sol = solve(prob, Tsit5(); dtmax=0.01) +end From f151e429cd9edb33b55ec019be069af12cb9d108 Mon Sep 17 00:00:00 2001 From: Ben Chung Date: Fri, 2 Aug 2024 16:19:51 -0700 Subject: [PATCH 0160/2176] Clarify documentation for SCC --- src/systems/callbacks.jl | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/src/systems/callbacks.jl b/src/systems/callbacks.jl index f7d4baa4cb..a57d5c006d 100644 --- a/src/systems/callbacks.jl +++ b/src/systems/callbacks.jl @@ -145,17 +145,26 @@ By default `affect_neg = affect`; to only get rising edges specify `affect_neg = Assume without loss of generality that the equation is of the form `c(u,p,t) ~ 0`; we denote the integrator state as `i.u`. For compactness, we define `prev_sign = sign(c(u[t-1], p[t-1], t-1))` and `cur_sign = sign(c(u[t], p[t], t))`. A condition edge will be detected and the callback will be invoked iff `prev_sign * cur_sign <= 0`. +The positive edge `affect` will be triggered iff an edge is detected and if `prev_sign < 0`; similarly, `affect_neg` will be +triggered iff an edge is detected and `prev_sign > 0`. + Inter-sample condition activation is not guaranteed; for example if we use the dirac delta function as `c` to insert a sharp discontinuity between integrator steps (which in this example would not normally be identified by adaptivity) then the condition is not guaranteed to be triggered. Once detected the integrator will "wind back" through a root-finding process to identify the point when the condition became active; the method used -is specified by `rootfind` from [`SciMLBase.RootfindOpt`](@ref). Multiple callbacks in the same system with different `rootfind` operations will be resolved -into separate VectorContinuousCallbacks in the enumeration order of `SciMLBase.RootfindOpt`, which may cause some callbacks to not fire if several become -active at the same instant. See the `SciMLBase` documentation for more information on the semantic rules. - -The positive edge `affect` will be triggered iff an edge is detected and if `prev_sign < 0`; similarly, `affect_neg` will be -triggered iff an edge is detected `prev_sign > 0`. +is specified by `rootfind` from [`SciMLBase.RootfindOpt`](@ref). If we denote the time when the condition becomes active at tc, +the value in the integrator after windback will be: +* `u[tc-epsilon], p[tc-epsilon], tc` if `LeftRootFind` is used, +* `u[tc+epsilon], p[tc+epsilon], tc` if `RightRootFind` is used, +* or `u[t], p[t], t` if `NoRootFind` is used. +For example, if we want to detect when an unknown variable `x` satisfies `x > 0` using the condition `x ~ 0` on a positive edge (that is, `D(x) > 0`), +then left root finding will get us `x=-epsilon`, right root finding `x=epsilon` and no root finding whatever the next step of the integrator was after +it passed through 0. + +Multiple callbacks in the same system with different `rootfind` operations will be grouped +by their `rootfind` value into separate VectorContinuousCallbacks in the enumeration order of `SciMLBase.RootfindOpt`. This may cause some callbacks to not fire if several become +active at the same instant. See the `SciMLBase` documentation for more information on the semantic rules. Affects (i.e. `affect` and `affect_neg`) can be specified as either: * A list of equations that should be applied when the callback is triggered (e.g. `x ~ 3, y ~ 7`) which must be of the form `unknown ~ observed value` where each `unknown` appears only once. Equations will be applied in the order that they appear in the vector; parameters and state updates will become immediately visible to following equations. From 49d48b833645a17f18f7581009f2a15a9c708161 Mon Sep 17 00:00:00 2001 From: Ben Chung Date: Fri, 2 Aug 2024 16:20:05 -0700 Subject: [PATCH 0161/2176] MutatingFunctionalAffect test cases --- test/symbolic_events.jl | 160 +++++++++++++++++++++++++++++++++++++--- 1 file changed, 149 insertions(+), 11 deletions(-) diff --git a/test/symbolic_events.jl b/test/symbolic_events.jl index 351f8111ee..ca9f0ad9c3 100644 --- a/test/symbolic_events.jl +++ b/test/symbolic_events.jl @@ -227,6 +227,117 @@ affect_neg = [x ~ 1] @test e[].affect == affect end +@testset "MutatingFunctionalAffect constructors" begin + fmfa(o, x, i, c) = nothing + m = ModelingToolkit.MutatingFunctionalAffect(fmfa) + @test m isa ModelingToolkit.MutatingFunctionalAffect + @test m.f == fmfa + @test m.obs == [] + @test m.obs_syms == [] + @test m.modified == [] + @test m.mod_syms == [] + @test m.ctx === nothing + + m = ModelingToolkit.MutatingFunctionalAffect(fmfa, (;)) + @test m isa ModelingToolkit.MutatingFunctionalAffect + @test m.f == fmfa + @test m.obs == [] + @test m.obs_syms == [] + @test m.modified == [] + @test m.mod_syms == [] + @test m.ctx === nothing + + m = ModelingToolkit.MutatingFunctionalAffect(fmfa, (; x)) + @test m isa ModelingToolkit.MutatingFunctionalAffect + @test m.f == fmfa + @test isequal(m.obs, [x]) + @test m.obs_syms == [:x] + @test m.modified == [] + @test m.mod_syms == [] + @test m.ctx === nothing + + m = ModelingToolkit.MutatingFunctionalAffect(fmfa, (; y=x)) + @test m isa ModelingToolkit.MutatingFunctionalAffect + @test m.f == fmfa + @test isequal(m.obs, [x]) + @test m.obs_syms == [:y] + @test m.modified == [] + @test m.mod_syms == [] + @test m.ctx === nothing + + m = ModelingToolkit.MutatingFunctionalAffect(fmfa; observed=(; y=x)) + @test m isa ModelingToolkit.MutatingFunctionalAffect + @test m.f == fmfa + @test isequal(m.obs, [x]) + @test m.obs_syms == [:y] + @test m.modified == [] + @test m.mod_syms == [] + @test m.ctx === nothing + + m = ModelingToolkit.MutatingFunctionalAffect(fmfa; modified=(; x)) + @test m isa ModelingToolkit.MutatingFunctionalAffect + @test m.f == fmfa + @test isequal(m.obs, []) + @test m.obs_syms == [] + @test isequal(m.modified, [x]) + @test m.mod_syms == [:x] + @test m.ctx === nothing + + m = ModelingToolkit.MutatingFunctionalAffect(fmfa; modified=(; y=x)) + @test m isa ModelingToolkit.MutatingFunctionalAffect + @test m.f == fmfa + @test isequal(m.obs, []) + @test m.obs_syms == [] + @test isequal(m.modified, [x]) + @test m.mod_syms == [:y] + @test m.ctx === nothing + + m = ModelingToolkit.MutatingFunctionalAffect(fmfa, (; x), (; x)) + @test m isa ModelingToolkit.MutatingFunctionalAffect + @test m.f == fmfa + @test isequal(m.obs, [x]) + @test m.obs_syms == [:x] + @test isequal(m.modified, [x]) + @test m.mod_syms == [:x] + @test m.ctx === nothing + + m = ModelingToolkit.MutatingFunctionalAffect(fmfa, (; y=x), (; y=x)) + @test m isa ModelingToolkit.MutatingFunctionalAffect + @test m.f == fmfa + @test isequal(m.obs, [x]) + @test m.obs_syms == [:y] + @test isequal(m.modified, [x]) + @test m.mod_syms == [:y] + @test m.ctx === nothing + + m = ModelingToolkit.MutatingFunctionalAffect(fmfa; modified=(; y=x), observed=(; y=x)) + @test m isa ModelingToolkit.MutatingFunctionalAffect + @test m.f == fmfa + @test isequal(m.obs, [x]) + @test m.obs_syms == [:y] + @test isequal(m.modified, [x]) + @test m.mod_syms == [:y] + @test m.ctx === nothing + + m = ModelingToolkit.MutatingFunctionalAffect(fmfa; modified=(; y=x), observed=(; y=x), ctx=3) + @test m isa ModelingToolkit.MutatingFunctionalAffect + @test m.f == fmfa + @test isequal(m.obs, [x]) + @test m.obs_syms == [:y] + @test isequal(m.modified, [x]) + @test m.mod_syms == [:y] + @test m.ctx === 3 + + m = ModelingToolkit.MutatingFunctionalAffect(fmfa, (; x), (; x), 3) + @test m isa ModelingToolkit.MutatingFunctionalAffect + @test m.f == fmfa + @test isequal(m.obs, [x]) + @test m.obs_syms == [:x] + @test isequal(m.modified, [x]) + @test m.mod_syms == [:x] + @test m.ctx === 3 +end + ## @named sys = ODESystem(eqs, t, continuous_events = [x ~ 1]) @@ -912,27 +1023,54 @@ end @testset "Quadrature" begin @variables theta(t) omega(t) - params = @parameters qA=0 qB=0 + params = @parameters qA=0 qB=0 hA=0 hB=0 cnt=0 eqs = [ D(theta) ~ omega - omega ~ sin(0.5*t) + omega ~ 1.0 ] - qAevt = ModelingToolkit.SymbolicContinuousCallback([cos(1000 * theta) ~ 0], - ModelingToolkit.MutatingFunctionalAffect(modified=(; qA)) do x, o, i, c + function decoder(oldA, oldB, newA, newB) + state = (oldA, oldB, newA, newB) + if state == (0, 0, 1, 0) || state == (1, 0, 1, 1) || state == (1, 1, 0, 1) || state == (0, 1, 0, 0) + return 1 + elseif state == (0, 0, 0, 1) || state == (0, 1, 1, 1) || state == (1, 1, 1, 0) || state == (1, 0, 0, 0) + return -1 + elseif state == (0, 0, 0, 0) || state == (0, 1, 0, 1) || state == (1, 0, 1, 0) || state == (1, 1, 1, 1) + return 0 + else + return 0 # err is interpreted as no movement + end + end + # todo: warn about dups + # todo: warn if a variable appears in both observed and modified + qAevt = ModelingToolkit.SymbolicContinuousCallback([cos(100 * theta) ~ 0], + ModelingToolkit.MutatingFunctionalAffect((; qB), (; qA, hA, hB, cnt)) do x, o, i, c + x.hA = x.qA + x.hB = o.qB x.qA = 1 + x.cnt += decoder(x.hA, x.hB, x.qA, o.qB) end, - affect_neg = ModelingToolkit.MutatingFunctionalAffect(modified=(; qA)) do x, o, i, c + affect_neg = ModelingToolkit.MutatingFunctionalAffect((; qB), (; qA, hA, hB, cnt)) do x, o, i, c + x.hA = x.qA + x.hB = o.qB x.qA = 0 - end) - qBevt = ModelingToolkit.SymbolicContinuousCallback([cos(1000 * theta + π/2) ~ 0], - ModelingToolkit.MutatingFunctionalAffect(modified=(; qB)) do x, o, i, c + x.cnt += decoder(x.hA, x.hB, x.qA, o.qB) + end; rootfind=SciMLBase.RightRootFind) + qBevt = ModelingToolkit.SymbolicContinuousCallback([cos(100 * theta - π/2) ~ 0], + ModelingToolkit.MutatingFunctionalAffect((; qA), (; qB, hA, hB, cnt)) do x, o, i, c + x.hA = o.qA + x.hB = x.qB x.qB = 1 + x.cnt += decoder(x.hA, x.hB, o.qA, x.qB) end, - affect_neg = ModelingToolkit.MutatingFunctionalAffect(modified=(; qB)) do x, o, i, c + affect_neg = ModelingToolkit.MutatingFunctionalAffect((; qA), (; qB, hA, hB, cnt)) do x, o, i, c + x.hA = o.qA + x.hB = x.qB x.qB = 0 - end) + x.cnt += decoder(x.hA, x.hB, o.qA, x.qB) + end; rootfind=SciMLBase.RightRootFind) @named sys = ODESystem(eqs, t, [theta, omega], params; continuous_events = [qAevt, qBevt]) ss = structural_simplify(sys) - prob = ODEProblem(ss, [theta => 0.0], (0.0, 1.0)) + prob = ODEProblem(ss, [theta => 0.0], (0.0, pi)) sol = solve(prob, Tsit5(); dtmax=0.01) + @test sol[cnt] == 197 # we get 2 pulses per phase cycle (cos 0 crossing) and we go to 100 cycles; we miss a few due to the initial state end From b96cd2eb273d640bf39d3a7f3c01a5e994cb7cc9 Mon Sep 17 00:00:00 2001 From: Ben Chung Date: Fri, 2 Aug 2024 16:42:55 -0700 Subject: [PATCH 0162/2176] More sanity checking --- src/systems/callbacks.jl | 28 ++++++++++++++++++++++++--- test/symbolic_events.jl | 42 ++++++++++++++++++++++++++++++++++++++-- 2 files changed, 65 insertions(+), 5 deletions(-) diff --git a/src/systems/callbacks.jl b/src/systems/callbacks.jl index a57d5c006d..612d80536c 100644 --- a/src/systems/callbacks.jl +++ b/src/systems/callbacks.jl @@ -818,10 +818,10 @@ function compile_user_affect(affect::FunctionalAffect, cb, sys, dvs, ps; kwargs. end end -invalid_variables(sys, expr) = filter(x -> !any(isequal(x), all_symbols(sys)), vars(expr)) +invalid_variables(sys, expr) = filter(x -> !any(isequal(x), all_symbols(sys)), reduce(vcat, vars(expr); init=[])) function unassignable_variables(sys, expr) assignable_syms = vcat(unknowns(sys), parameters(sys)) - return filter(x -> !any(isequal(x), assignable_syms), vars(expr)) + return filter(x -> !any(isequal(x), assignable_syms), reduce(vcat, vars(expr); init=[])) end function compile_user_affect(affect::MutatingFunctionalAffect, sys, dvs, ps; kwargs...) @@ -832,6 +832,21 @@ function compile_user_affect(affect::MutatingFunctionalAffect, sys, dvs, ps; kwa call the affect method - test if it's OOP or IP using applicable unpack and apply the resulting values =# + function check_dups(syms, exprs) # = (syms_dedup, exprs_dedup) + seen = Set{Symbol}() + syms_dedup = []; exprs_dedup = [] + for (sym, exp) in Iterators.zip(syms, exprs) + if !in(sym, seen) + push!(syms_dedup, sym) + push!(exprs_dedup, exp) + push!(seen, sym) + else + @warn "Expression $(expr) is aliased as $sym, which has already been used. The first definition will be used." + end + end + return (syms_dedup, exprs_dedup) + end + obs_exprs = observed(affect) for oexpr in obs_exprs invalid_vars = invalid_variables(sys, oexpr) @@ -840,6 +855,7 @@ function compile_user_affect(affect::MutatingFunctionalAffect, sys, dvs, ps; kwa end end obs_syms = observed_syms(affect) + obs_syms, obs_exprs = check_dups(obs_syms, obs_exprs) obs_size = size.(obs_exprs) # we will generate a work buffer of a ComponentArray that maps obs_syms to arrays of size obs_size mod_exprs = modified(affect) @@ -849,12 +865,18 @@ function compile_user_affect(affect::MutatingFunctionalAffect, sys, dvs, ps; kwa end invalid_vars = unassignable_variables(sys, mexpr) if length(invalid_vars) > 0 - error("Observed equation $(mexpr) in affect refers to missing variable(s) $(invalid_vars); the variables may not have been added (e.g. if a component is missing) or they may have been reduced away.") + error("Modified equation $(mexpr) in affect refers to missing variable(s) $(invalid_vars); the variables may not have been added (e.g. if a component is missing) or they may have been reduced away.") end end mod_syms = modified_syms(affect) + mod_syms, mod_exprs = check_dups(mod_syms, mod_exprs) _, mod_og_val_fun = build_explicit_observed_function(sys, mod_exprs; return_inplace=true) + overlapping_syms = intersect(mod_syms, obs_syms) + if length(overlapping_syms) > 0 + @warn "The symbols $overlapping_syms are declared as both observed and modified; this is a code smell because it becomes easy to confuse them and assign/not assign a value." + end + # sanity checks done! now build the data and update function for observed values mkzero(sz) = if sz === () 0.0 else zeros(sz) end _, obs_fun = build_explicit_observed_function(sys, reduce(vcat, Symbolics.scalarize.(obs_exprs); init = []); return_inplace=true) diff --git a/test/symbolic_events.jl b/test/symbolic_events.jl index ca9f0ad9c3..d24b590970 100644 --- a/test/symbolic_events.jl +++ b/test/symbolic_events.jl @@ -1021,6 +1021,46 @@ end @test all(sol[temp][sol.t .> 1.0] .<= 0.79) && all(sol[temp][sol.t .> 1.0] .>= 0.49) end +@testset "MutatingFunctionalAffect errors and warnings" begin + @variables temp(t) + params = @parameters furnace_on_threshold=0.5 furnace_off_threshold=0.7 furnace_power=1.0 leakage=0.1 furnace_on::Bool=false + eqs = [ + D(temp) ~ furnace_on * furnace_power - temp^2 * leakage + ] + + furnace_off = ModelingToolkit.SymbolicContinuousCallback([temp ~ furnace_off_threshold], + ModelingToolkit.MutatingFunctionalAffect(modified=(; furnace_on), observed=(; furnace_on)) do x, o, i, c + x.furnace_on = false + end) + @named sys = ODESystem(eqs, t, [temp], params; continuous_events = [furnace_off]) + ss = structural_simplify(sys) + @test_logs (:warn, "The symbols Any[:furnace_on] are declared as both observed and modified; this is a code smell because it becomes easy to confuse them and assign/not assign a value.") prob = ODEProblem(ss, [temp => 0.0, furnace_on => true], (0.0, 100.0)) + + @variables tempsq(t) # trivially eliminated + eqs = [ + tempsq ~ temp^2 + D(temp) ~ furnace_on * furnace_power - temp^2 * leakage + ] + + furnace_off = ModelingToolkit.SymbolicContinuousCallback([temp ~ furnace_off_threshold], + ModelingToolkit.MutatingFunctionalAffect(modified=(; furnace_on, tempsq), observed=(; furnace_on)) do x, o, i, c + x.furnace_on = false + end) + @named sys = ODESystem(eqs, t, [temp, tempsq], params; continuous_events = [furnace_off]) + ss = structural_simplify(sys) + @test_throws "refers to missing variable(s)" prob = ODEProblem(ss, [temp => 0.0, furnace_on => true], (0.0, 100.0)) + + + @parameters not_actually_here + furnace_off = ModelingToolkit.SymbolicContinuousCallback([temp ~ furnace_off_threshold], + ModelingToolkit.MutatingFunctionalAffect(modified=(; furnace_on), observed=(; furnace_on, not_actually_here)) do x, o, i, c + x.furnace_on = false + end) + @named sys = ODESystem(eqs, t, [temp, tempsq], params; continuous_events = [furnace_off]) + ss = structural_simplify(sys) + @test_throws "refers to missing variable(s)" prob = ODEProblem(ss, [temp => 0.0, furnace_on => true], (0.0, 100.0)) +end + @testset "Quadrature" begin @variables theta(t) omega(t) params = @parameters qA=0 qB=0 hA=0 hB=0 cnt=0 @@ -1040,8 +1080,6 @@ end return 0 # err is interpreted as no movement end end - # todo: warn about dups - # todo: warn if a variable appears in both observed and modified qAevt = ModelingToolkit.SymbolicContinuousCallback([cos(100 * theta) ~ 0], ModelingToolkit.MutatingFunctionalAffect((; qB), (; qA, hA, hB, cnt)) do x, o, i, c x.hA = x.qA From eec24cfb95546fb5b8a2415a8cd3a2be0d60a6a8 Mon Sep 17 00:00:00 2001 From: Ben Chung Date: Fri, 2 Aug 2024 16:56:56 -0700 Subject: [PATCH 0163/2176] Document MutatingFunctionalAffect --- src/systems/callbacks.jl | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/src/systems/callbacks.jl b/src/systems/callbacks.jl index 612d80536c..fefff94442 100644 --- a/src/systems/callbacks.jl +++ b/src/systems/callbacks.jl @@ -72,10 +72,29 @@ function namespace_affect(affect::FunctionalAffect, s) end """ -`MutatingFunctionalAffect` differs from `FunctionalAffect` in two key ways: -* First, insetad of the `u` vector passed to `f` being a vector of indices into `integ.u` it's instead the result of evaluating `obs` at the current state, named as specified in `obs_syms`. This allows affects to easily access observed states and decouples affect inputs from the system structure. -* Second, it abstracts the assignment back to system states away. Instead of writing `integ.u[u.myvar] = [whatever]`, you instead declare in `mod_params` that you want to modify `myvar` and then either (out of place) return a named tuple with `myvar` or (in place) modify the associated element in the ComponentArray that's given. -Initially, we only support "flat" states in `modified`; these states will be marked as irreducible in the overarching system and they will simply be bulk assigned at mutation. In the future, this will be extended to perform a nonlinear solve to further decouple the affect from the system structure. + MutatingFunctionalAffect(f::Function; observed::NamedTuple, modified::NamedTuple, ctx) + +`MutatingFunctionalAffect` is a helper for writing affect functions that will compute observed values and +ensure that modified values are correctly written back into the system. The affect function `f` needs to have +one of three signatures: +* `f(observed::ComponentArray)` if the function only reads observed values back from the system, +* `f(observed::ComponentArray, modified::ComponentArray)` if the function also writes values (unknowns or parameters) into the system, +* `f(observed::ComponentArray, modified::ComponentArray, ctx)` if the function needs the user-defined context, +* `f(observed::ComponentArray, modified::ComponentArray, ctx, integrator)` if the function needs the low-level integrator. + +The function `f` will be called with `observed` and `modified` `ComponentArray`s that are derived from their respective `NamedTuple` definitions. +Each `NamedTuple` should map an expression to a symbol; for example if we pass `observed=(; x = a + b)` this will alias the result of executing `a+b` in the system as `x` +so the value of `a + b` will be accessible as `observed.x` in `f`. `modified` currently restricts symbolic expressions to only bare variables, so only tuples of the form +`(; x = y)` or `(; x)` (which aliases `x` as itself) are allowed. + +Both `observed` and `modified` will be automatically populated with the current values of their corresponding expressions on function entry. +The values in `modified` will be written back to the system after `f` returns. For example, if we want to update the value of `x` to be the result of `x + y` we could write + + MutatingFunctionalAffect(observed=(; x_plus_y = x + y), modified=(; x)) do o, m + m.x = o.x_plus_y + end + +The affect function updates the value at `x` in `modified` to be the result of evaluating `x + y` as passed in the observed values. """ @kwdef struct MutatingFunctionalAffect f::Any @@ -174,6 +193,7 @@ Affects (i.e. `affect` and `affect_neg`) can be specified as either: + `read_parameters` is a vector of the parameters that are *used* by `f!`. Their indices are passed to `f` in `p` similarly to the indices of `unknowns` passed in `u`. + `modified_parameters` is a vector of the parameters that are *modified* by `f!`. Note that a parameter will not appear in `p` if it only appears in `modified_parameters`; it must appear in both `parameters` and `modified_parameters` if it is used in the affect definition. + `ctx` is a user-defined context object passed to `f!` when invoked. This value is aliased for each problem. +* A [`MutatingFunctionalAffect`](@ref); refer to its documentation for details. """ struct SymbolicContinuousCallback eqs::Vector{Equation} From fd0125d36715693c0b7e2c5d5fe130e402c137ae Mon Sep 17 00:00:00 2001 From: Ben Chung Date: Fri, 2 Aug 2024 17:15:38 -0700 Subject: [PATCH 0164/2176] Flip modified and observed order; write docstring --- src/systems/callbacks.jl | 34 +++++++++++++------ test/symbolic_events.jl | 73 +++++++++++++++++++++++++++++++--------- 2 files changed, 80 insertions(+), 27 deletions(-) diff --git a/src/systems/callbacks.jl b/src/systems/callbacks.jl index fefff94442..715b0e9a91 100644 --- a/src/systems/callbacks.jl +++ b/src/systems/callbacks.jl @@ -72,15 +72,16 @@ function namespace_affect(affect::FunctionalAffect, s) end """ - MutatingFunctionalAffect(f::Function; observed::NamedTuple, modified::NamedTuple, ctx) + MutatingFunctionalAffect(f::Function; modified::NamedTuple, observed::NamedTuple, ctx) `MutatingFunctionalAffect` is a helper for writing affect functions that will compute observed values and ensure that modified values are correctly written back into the system. The affect function `f` needs to have -one of three signatures: -* `f(observed::ComponentArray)` if the function only reads observed values back from the system, -* `f(observed::ComponentArray, modified::ComponentArray)` if the function also writes values (unknowns or parameters) into the system, -* `f(observed::ComponentArray, modified::ComponentArray, ctx)` if the function needs the user-defined context, -* `f(observed::ComponentArray, modified::ComponentArray, ctx, integrator)` if the function needs the low-level integrator. +one of four signatures: +* `f(modified::ComponentArray)` if the function only writes values (unknowns or parameters) to the system, +* `f(modified::ComponentArray, observed::ComponentArray)` if the function also reads observed values from the system, +* `f(modified::ComponentArray, observed::ComponentArray, ctx)` if the function needs the user-defined context, +* `f(modified::ComponentArray, observed::ComponentArray, ctx, integrator)` if the function needs the low-level integrator. +These will be checked in reverse order (that is, the four-argument version first, than the 3, etc). The function `f` will be called with `observed` and `modified` `ComponentArray`s that are derived from their respective `NamedTuple` definitions. Each `NamedTuple` should map an expression to a symbol; for example if we pass `observed=(; x = a + b)` this will alias the result of executing `a+b` in the system as `x` @@ -90,7 +91,7 @@ so the value of `a + b` will be accessible as `observed.x` in `f`. `modified` cu Both `observed` and `modified` will be automatically populated with the current values of their corresponding expressions on function entry. The values in `modified` will be written back to the system after `f` returns. For example, if we want to update the value of `x` to be the result of `x + y` we could write - MutatingFunctionalAffect(observed=(; x_plus_y = x + y), modified=(; x)) do o, m + MutatingFunctionalAffect(observed=(; x_plus_y = x + y), modified=(; x)) do m, o m.x = o.x_plus_y end @@ -109,11 +110,11 @@ MutatingFunctionalAffect(f::Function; observed::NamedTuple = NamedTuple{()}(()), modified::NamedTuple = NamedTuple{()}(()), ctx=nothing) = MutatingFunctionalAffect(f, collect(values(observed)), collect(keys(observed)), collect(values(modified)), collect(keys(modified)), ctx) -MutatingFunctionalAffect(f::Function, observed::NamedTuple; modified::NamedTuple = NamedTuple{()}(()), ctx=nothing) = +MutatingFunctionalAffect(f::Function, modified::NamedTuple; observed::NamedTuple = NamedTuple{()}(()), ctx=nothing) = MutatingFunctionalAffect(f, observed=observed, modified=modified, ctx=ctx) -MutatingFunctionalAffect(f::Function, observed::NamedTuple, modified::NamedTuple; ctx=nothing) = +MutatingFunctionalAffect(f::Function, modified::NamedTuple, observed::NamedTuple; ctx=nothing) = MutatingFunctionalAffect(f, observed=observed, modified=modified, ctx=ctx) -MutatingFunctionalAffect(f::Function, observed::NamedTuple, modified::NamedTuple, ctx) = +MutatingFunctionalAffect(f::Function, modified::NamedTuple, observed::NamedTuple, ctx) = MutatingFunctionalAffect(f, observed=observed, modified=modified, ctx=ctx) func(f::MutatingFunctionalAffect) = f.f @@ -925,7 +926,18 @@ function compile_user_affect(affect::MutatingFunctionalAffect, sys, dvs, ps; kwa obs_fun(obs_component_array, integ.u, integ.p..., integ.t) # let the user do their thing - user_affect(upd_component_array, obs_component_array, integ, ctx) + if applicable(user_affect, upd_component_array, obs_component_array, ctx, integ) + user_affect(upd_component_array, obs_component_array, ctx, integ) + elseif applicable(user_affect, upd_component_array, obs_component_array, ctx) + user_affect(upd_component_array, obs_component_array, ctx) + elseif applicable(user_affect, upd_component_array, obs_component_array) + user_affect(upd_component_array, obs_component_array) + elseif applicable(user_affect, upd_component_array) + user_affect(upd_component_array) + else + @error "User affect function $user_affect needs to implement one of the supported MutatingFunctionalAffect callback forms; see the MutatingFunctionalAffect docstring for more details" + user_affect(upd_component_array, obs_component_array, integ, ctx) # this WILL error but it'll give a more sensible message + end # write the new values back to the integrator upd_params_fun(integ, upd_params_view) diff --git a/test/symbolic_events.jl b/test/symbolic_events.jl index d24b590970..779f41471a 100644 --- a/test/symbolic_events.jl +++ b/test/symbolic_events.jl @@ -250,19 +250,19 @@ end m = ModelingToolkit.MutatingFunctionalAffect(fmfa, (; x)) @test m isa ModelingToolkit.MutatingFunctionalAffect @test m.f == fmfa - @test isequal(m.obs, [x]) - @test m.obs_syms == [:x] - @test m.modified == [] - @test m.mod_syms == [] + @test isequal(m.obs, []) + @test m.obs_syms == [] + @test isequal(m.modified, [x]) + @test m.mod_syms == [:x] @test m.ctx === nothing m = ModelingToolkit.MutatingFunctionalAffect(fmfa, (; y=x)) @test m isa ModelingToolkit.MutatingFunctionalAffect @test m.f == fmfa - @test isequal(m.obs, [x]) - @test m.obs_syms == [:y] - @test m.modified == [] - @test m.mod_syms == [] + @test isequal(m.obs, []) + @test m.obs_syms == [] + @test isequal(m.modified, [x]) + @test m.mod_syms == [:y] @test m.ctx === nothing m = ModelingToolkit.MutatingFunctionalAffect(fmfa; observed=(; y=x)) @@ -1013,7 +1013,48 @@ end ModelingToolkit.MutatingFunctionalAffect(modified=(; furnace_on)) do x, o, i, c x.furnace_on = true end) - + @named sys = ODESystem(eqs, t, [temp], params; continuous_events = [furnace_off, furnace_enable]) + ss = structural_simplify(sys) + prob = ODEProblem(ss, [temp => 0.0, furnace_on => true], (0.0, 100.0)) + sol = solve(prob, Tsit5(); dtmax=0.01) + @test all(sol[temp][sol.t .> 1.0] .<= 0.79) && all(sol[temp][sol.t .> 1.0] .>= 0.49) + + furnace_off = ModelingToolkit.SymbolicContinuousCallback([temp ~ furnace_off_threshold], + ModelingToolkit.MutatingFunctionalAffect(modified=(; furnace_on)) do x, o, i + x.furnace_on = false + end) + furnace_enable = ModelingToolkit.SymbolicContinuousCallback([temp ~ furnace_on_threshold], + ModelingToolkit.MutatingFunctionalAffect(modified=(; furnace_on)) do x, o, i + x.furnace_on = true + end) + @named sys = ODESystem(eqs, t, [temp], params; continuous_events = [furnace_off, furnace_enable]) + ss = structural_simplify(sys) + prob = ODEProblem(ss, [temp => 0.0, furnace_on => true], (0.0, 100.0)) + sol = solve(prob, Tsit5(); dtmax=0.01) + @test all(sol[temp][sol.t .> 1.0] .<= 0.79) && all(sol[temp][sol.t .> 1.0] .>= 0.49) + + furnace_off = ModelingToolkit.SymbolicContinuousCallback([temp ~ furnace_off_threshold], + ModelingToolkit.MutatingFunctionalAffect(modified=(; furnace_on)) do x, o + x.furnace_on = false + end) + furnace_enable = ModelingToolkit.SymbolicContinuousCallback([temp ~ furnace_on_threshold], + ModelingToolkit.MutatingFunctionalAffect(modified=(; furnace_on)) do x, o + x.furnace_on = true + end) + @named sys = ODESystem(eqs, t, [temp], params; continuous_events = [furnace_off, furnace_enable]) + ss = structural_simplify(sys) + prob = ODEProblem(ss, [temp => 0.0, furnace_on => true], (0.0, 100.0)) + sol = solve(prob, Tsit5(); dtmax=0.01) + @test all(sol[temp][sol.t .> 1.0] .<= 0.79) && all(sol[temp][sol.t .> 1.0] .>= 0.49) + + furnace_off = ModelingToolkit.SymbolicContinuousCallback([temp ~ furnace_off_threshold], + ModelingToolkit.MutatingFunctionalAffect(modified=(; furnace_on)) do x + x.furnace_on = false + end) + furnace_enable = ModelingToolkit.SymbolicContinuousCallback([temp ~ furnace_on_threshold], + ModelingToolkit.MutatingFunctionalAffect(modified=(; furnace_on)) do x + x.furnace_on = true + end) @named sys = ODESystem(eqs, t, [temp], params; continuous_events = [furnace_off, furnace_enable]) ss = structural_simplify(sys) prob = ODEProblem(ss, [temp => 0.0, furnace_on => true], (0.0, 100.0)) @@ -1029,7 +1070,7 @@ end ] furnace_off = ModelingToolkit.SymbolicContinuousCallback([temp ~ furnace_off_threshold], - ModelingToolkit.MutatingFunctionalAffect(modified=(; furnace_on), observed=(; furnace_on)) do x, o, i, c + ModelingToolkit.MutatingFunctionalAffect(modified=(; furnace_on), observed=(; furnace_on)) do x, o, c, i x.furnace_on = false end) @named sys = ODESystem(eqs, t, [temp], params; continuous_events = [furnace_off]) @@ -1043,7 +1084,7 @@ end ] furnace_off = ModelingToolkit.SymbolicContinuousCallback([temp ~ furnace_off_threshold], - ModelingToolkit.MutatingFunctionalAffect(modified=(; furnace_on, tempsq), observed=(; furnace_on)) do x, o, i, c + ModelingToolkit.MutatingFunctionalAffect(modified=(; furnace_on, tempsq), observed=(; furnace_on)) do x, o, c, i x.furnace_on = false end) @named sys = ODESystem(eqs, t, [temp, tempsq], params; continuous_events = [furnace_off]) @@ -1053,7 +1094,7 @@ end @parameters not_actually_here furnace_off = ModelingToolkit.SymbolicContinuousCallback([temp ~ furnace_off_threshold], - ModelingToolkit.MutatingFunctionalAffect(modified=(; furnace_on), observed=(; furnace_on, not_actually_here)) do x, o, i, c + ModelingToolkit.MutatingFunctionalAffect(modified=(; furnace_on), observed=(; furnace_on, not_actually_here)) do x, o, c, i x.furnace_on = false end) @named sys = ODESystem(eqs, t, [temp, tempsq], params; continuous_events = [furnace_off]) @@ -1081,26 +1122,26 @@ end end end qAevt = ModelingToolkit.SymbolicContinuousCallback([cos(100 * theta) ~ 0], - ModelingToolkit.MutatingFunctionalAffect((; qB), (; qA, hA, hB, cnt)) do x, o, i, c + ModelingToolkit.MutatingFunctionalAffect((; qA, hA, hB, cnt), (; qB)) do x, o, i, c x.hA = x.qA x.hB = o.qB x.qA = 1 x.cnt += decoder(x.hA, x.hB, x.qA, o.qB) end, - affect_neg = ModelingToolkit.MutatingFunctionalAffect((; qB), (; qA, hA, hB, cnt)) do x, o, i, c + affect_neg = ModelingToolkit.MutatingFunctionalAffect((; qA, hA, hB, cnt), (; qB)) do x, o, c, i x.hA = x.qA x.hB = o.qB x.qA = 0 x.cnt += decoder(x.hA, x.hB, x.qA, o.qB) end; rootfind=SciMLBase.RightRootFind) qBevt = ModelingToolkit.SymbolicContinuousCallback([cos(100 * theta - π/2) ~ 0], - ModelingToolkit.MutatingFunctionalAffect((; qA), (; qB, hA, hB, cnt)) do x, o, i, c + ModelingToolkit.MutatingFunctionalAffect((; qB, hA, hB, cnt), (; qA)) do x, o, i, c x.hA = o.qA x.hB = x.qB x.qB = 1 x.cnt += decoder(x.hA, x.hB, o.qA, x.qB) end, - affect_neg = ModelingToolkit.MutatingFunctionalAffect((; qA), (; qB, hA, hB, cnt)) do x, o, i, c + affect_neg = ModelingToolkit.MutatingFunctionalAffect((; qB, hA, hB, cnt), (; qA)) do x, o, c, i x.hA = o.qA x.hB = x.qB x.qB = 0 From 9948de076b491a114b395f8a9168eaaebe0959b1 Mon Sep 17 00:00:00 2001 From: Ben Chung Date: Fri, 2 Aug 2024 17:17:12 -0700 Subject: [PATCH 0165/2176] Run formatter --- src/systems/callbacks.jl | 87 ++++++++++++------- test/symbolic_events.jl | 175 ++++++++++++++++++++++----------------- 2 files changed, 158 insertions(+), 104 deletions(-) diff --git a/src/systems/callbacks.jl b/src/systems/callbacks.jl index 715b0e9a91..5c6f9b2801 100644 --- a/src/systems/callbacks.jl +++ b/src/systems/callbacks.jl @@ -106,16 +106,25 @@ The affect function updates the value at `x` in `modified` to be the result of e ctx::Any end -MutatingFunctionalAffect(f::Function; - observed::NamedTuple = NamedTuple{()}(()), - modified::NamedTuple = NamedTuple{()}(()), - ctx=nothing) = MutatingFunctionalAffect(f, collect(values(observed)), collect(keys(observed)), collect(values(modified)), collect(keys(modified)), ctx) -MutatingFunctionalAffect(f::Function, modified::NamedTuple; observed::NamedTuple = NamedTuple{()}(()), ctx=nothing) = - MutatingFunctionalAffect(f, observed=observed, modified=modified, ctx=ctx) -MutatingFunctionalAffect(f::Function, modified::NamedTuple, observed::NamedTuple; ctx=nothing) = - MutatingFunctionalAffect(f, observed=observed, modified=modified, ctx=ctx) -MutatingFunctionalAffect(f::Function, modified::NamedTuple, observed::NamedTuple, ctx) = - MutatingFunctionalAffect(f, observed=observed, modified=modified, ctx=ctx) +function MutatingFunctionalAffect(f::Function; + observed::NamedTuple = NamedTuple{()}(()), + modified::NamedTuple = NamedTuple{()}(()), + ctx = nothing) + MutatingFunctionalAffect(f, collect(values(observed)), collect(keys(observed)), + collect(values(modified)), collect(keys(modified)), ctx) +end +function MutatingFunctionalAffect(f::Function, modified::NamedTuple; + observed::NamedTuple = NamedTuple{()}(()), ctx = nothing) + MutatingFunctionalAffect(f, observed = observed, modified = modified, ctx = ctx) +end +function MutatingFunctionalAffect( + f::Function, modified::NamedTuple, observed::NamedTuple; ctx = nothing) + MutatingFunctionalAffect(f, observed = observed, modified = modified, ctx = ctx) +end +function MutatingFunctionalAffect( + f::Function, modified::NamedTuple, observed::NamedTuple, ctx) + MutatingFunctionalAffect(f, observed = observed, modified = modified, ctx = ctx) +end func(f::MutatingFunctionalAffect) = f.f context(a::MutatingFunctionalAffect) = a.ctx @@ -126,8 +135,9 @@ modified(a::MutatingFunctionalAffect) = a.modified modified_syms(a::MutatingFunctionalAffect) = a.mod_syms function Base.:(==)(a1::MutatingFunctionalAffect, a2::MutatingFunctionalAffect) - isequal(a1.f, a2.f) && isequal(a1.obs, a2.obs) && isequal(a1.modified, a2.modified) && - isequal(a1.obs_syms, a2.obs_syms) && isequal(a1.mod_syms, a2.mod_syms)&& isequal(a1.ctx, a2.ctx) + isequal(a1.f, a2.f) && isequal(a1.obs, a2.obs) && isequal(a1.modified, a2.modified) && + isequal(a1.obs_syms, a2.obs_syms) && isequal(a1.mod_syms, a2.mod_syms) && + isequal(a1.ctx, a2.ctx) end function Base.hash(a::MutatingFunctionalAffect, s::UInt) @@ -839,10 +849,13 @@ function compile_user_affect(affect::FunctionalAffect, cb, sys, dvs, ps; kwargs. end end -invalid_variables(sys, expr) = filter(x -> !any(isequal(x), all_symbols(sys)), reduce(vcat, vars(expr); init=[])) -function unassignable_variables(sys, expr) +function invalid_variables(sys, expr) + filter(x -> !any(isequal(x), all_symbols(sys)), reduce(vcat, vars(expr); init = [])) +end +function unassignable_variables(sys, expr) assignable_syms = vcat(unknowns(sys), parameters(sys)) - return filter(x -> !any(isequal(x), assignable_syms), reduce(vcat, vars(expr); init=[])) + return filter( + x -> !any(isequal(x), assignable_syms), reduce(vcat, vars(expr); init = [])) end function compile_user_affect(affect::MutatingFunctionalAffect, sys, dvs, ps; kwargs...) @@ -855,7 +868,8 @@ function compile_user_affect(affect::MutatingFunctionalAffect, sys, dvs, ps; kwa =# function check_dups(syms, exprs) # = (syms_dedup, exprs_dedup) seen = Set{Symbol}() - syms_dedup = []; exprs_dedup = [] + syms_dedup = [] + exprs_dedup = [] for (sym, exp) in Iterators.zip(syms, exprs) if !in(sym, seen) push!(syms_dedup, sym) @@ -869,7 +883,7 @@ function compile_user_affect(affect::MutatingFunctionalAffect, sys, dvs, ps; kwa end obs_exprs = observed(affect) - for oexpr in obs_exprs + for oexpr in obs_exprs invalid_vars = invalid_variables(sys, oexpr) if length(invalid_vars) > 0 error("Observed equation $(oexpr) in affect refers to missing variable(s) $(invalid_vars); the variables may not have been added (e.g. if a component is missing).") @@ -880,7 +894,7 @@ function compile_user_affect(affect::MutatingFunctionalAffect, sys, dvs, ps; kwa obs_size = size.(obs_exprs) # we will generate a work buffer of a ComponentArray that maps obs_syms to arrays of size obs_size mod_exprs = modified(affect) - for mexpr in mod_exprs + for mexpr in mod_exprs if !is_observed(sys, mexpr) && parameter_index(sys, mexpr) === nothing error("Expression $mexpr cannot be assigned to; currently only unknowns and parameters may be updated by an affect.") end @@ -891,7 +905,8 @@ function compile_user_affect(affect::MutatingFunctionalAffect, sys, dvs, ps; kwa end mod_syms = modified_syms(affect) mod_syms, mod_exprs = check_dups(mod_syms, mod_exprs) - _, mod_og_val_fun = build_explicit_observed_function(sys, mod_exprs; return_inplace=true) + _, mod_og_val_fun = build_explicit_observed_function( + sys, mod_exprs; return_inplace = true) overlapping_syms = intersect(mod_syms, obs_syms) if length(overlapping_syms) > 0 @@ -899,21 +914,33 @@ function compile_user_affect(affect::MutatingFunctionalAffect, sys, dvs, ps; kwa end # sanity checks done! now build the data and update function for observed values - mkzero(sz) = if sz === () 0.0 else zeros(sz) end - _, obs_fun = build_explicit_observed_function(sys, reduce(vcat, Symbolics.scalarize.(obs_exprs); init = []); return_inplace=true) - obs_component_array = ComponentArrays.ComponentArray(NamedTuple{(obs_syms..., )}(mkzero.(obs_size))) + mkzero(sz) = + if sz === () + 0.0 + else + zeros(sz) + end + _, obs_fun = build_explicit_observed_function( + sys, reduce(vcat, Symbolics.scalarize.(obs_exprs); init = []); + return_inplace = true) + obs_component_array = ComponentArrays.ComponentArray(NamedTuple{(obs_syms...,)}(mkzero.(obs_size))) # okay so now to generate the stuff to assign it back into the system # note that we reorder the componentarray to make the views coherent wrt the base array mod_pairs = mod_exprs .=> mod_syms mod_param_pairs = filter(v -> is_parameter(sys, v[1]), mod_pairs) mod_unk_pairs = filter(v -> !is_parameter(sys, v[1]), mod_pairs) - _, mod_og_val_fun = build_explicit_observed_function(sys, reduce(vcat, [first.(mod_param_pairs); first.(mod_unk_pairs)]; init = []); return_inplace=true) - upd_params_fun = setu(sys, reduce(vcat, Symbolics.scalarize.(first.(mod_param_pairs)); init = [])) - upd_unk_fun = setu(sys, reduce(vcat, Symbolics.scalarize.(first.(mod_unk_pairs)); init = [])) - - upd_component_array = ComponentArrays.ComponentArray(NamedTuple{([last.(mod_param_pairs); last.(mod_unk_pairs)]...,)}( - [collect(mkzero(size(e)) for e in first.(mod_param_pairs)); + _, mod_og_val_fun = build_explicit_observed_function( + sys, reduce(vcat, [first.(mod_param_pairs); first.(mod_unk_pairs)]; init = []); + return_inplace = true) + upd_params_fun = setu( + sys, reduce(vcat, Symbolics.scalarize.(first.(mod_param_pairs)); init = [])) + upd_unk_fun = setu( + sys, reduce(vcat, Symbolics.scalarize.(first.(mod_unk_pairs)); init = [])) + + upd_component_array = ComponentArrays.ComponentArray(NamedTuple{([last.(mod_param_pairs); + last.(mod_unk_pairs)]...,)}( + [collect(mkzero(size(e)) for e in first.(mod_param_pairs)); collect(mkzero(size(e)) for e in first.(mod_unk_pairs))])) upd_params_view = view(upd_component_array, last.(mod_param_pairs)) upd_unks_view = view(upd_component_array, last.(mod_unk_pairs)) @@ -921,7 +948,7 @@ function compile_user_affect(affect::MutatingFunctionalAffect, sys, dvs, ps; kwa function (integ) # update the to-be-mutated values; this ensures that if you do a no-op then nothing happens mod_og_val_fun(upd_component_array, integ.u, integ.p..., integ.t) - + # update the observed values obs_fun(obs_component_array, integ.u, integ.p..., integ.t) @@ -934,7 +961,7 @@ function compile_user_affect(affect::MutatingFunctionalAffect, sys, dvs, ps; kwa user_affect(upd_component_array, obs_component_array) elseif applicable(user_affect, upd_component_array) user_affect(upd_component_array) - else + else @error "User affect function $user_affect needs to implement one of the supported MutatingFunctionalAffect callback forms; see the MutatingFunctionalAffect docstring for more details" user_affect(upd_component_array, obs_component_array, integ, ctx) # this WILL error but it'll give a more sensible message end diff --git a/test/symbolic_events.jl b/test/symbolic_events.jl index 779f41471a..7aba69dc7f 100644 --- a/test/symbolic_events.jl +++ b/test/symbolic_events.jl @@ -237,7 +237,7 @@ end @test m.modified == [] @test m.mod_syms == [] @test m.ctx === nothing - + m = ModelingToolkit.MutatingFunctionalAffect(fmfa, (;)) @test m isa ModelingToolkit.MutatingFunctionalAffect @test m.f == fmfa @@ -246,7 +246,7 @@ end @test m.modified == [] @test m.mod_syms == [] @test m.ctx === nothing - + m = ModelingToolkit.MutatingFunctionalAffect(fmfa, (; x)) @test m isa ModelingToolkit.MutatingFunctionalAffect @test m.f == fmfa @@ -255,8 +255,8 @@ end @test isequal(m.modified, [x]) @test m.mod_syms == [:x] @test m.ctx === nothing - - m = ModelingToolkit.MutatingFunctionalAffect(fmfa, (; y=x)) + + m = ModelingToolkit.MutatingFunctionalAffect(fmfa, (; y = x)) @test m isa ModelingToolkit.MutatingFunctionalAffect @test m.f == fmfa @test isequal(m.obs, []) @@ -264,8 +264,8 @@ end @test isequal(m.modified, [x]) @test m.mod_syms == [:y] @test m.ctx === nothing - - m = ModelingToolkit.MutatingFunctionalAffect(fmfa; observed=(; y=x)) + + m = ModelingToolkit.MutatingFunctionalAffect(fmfa; observed = (; y = x)) @test m isa ModelingToolkit.MutatingFunctionalAffect @test m.f == fmfa @test isequal(m.obs, [x]) @@ -273,8 +273,8 @@ end @test m.modified == [] @test m.mod_syms == [] @test m.ctx === nothing - - m = ModelingToolkit.MutatingFunctionalAffect(fmfa; modified=(; x)) + + m = ModelingToolkit.MutatingFunctionalAffect(fmfa; modified = (; x)) @test m isa ModelingToolkit.MutatingFunctionalAffect @test m.f == fmfa @test isequal(m.obs, []) @@ -283,7 +283,7 @@ end @test m.mod_syms == [:x] @test m.ctx === nothing - m = ModelingToolkit.MutatingFunctionalAffect(fmfa; modified=(; y=x)) + m = ModelingToolkit.MutatingFunctionalAffect(fmfa; modified = (; y = x)) @test m isa ModelingToolkit.MutatingFunctionalAffect @test m.f == fmfa @test isequal(m.obs, []) @@ -291,7 +291,7 @@ end @test isequal(m.modified, [x]) @test m.mod_syms == [:y] @test m.ctx === nothing - + m = ModelingToolkit.MutatingFunctionalAffect(fmfa, (; x), (; x)) @test m isa ModelingToolkit.MutatingFunctionalAffect @test m.f == fmfa @@ -300,8 +300,8 @@ end @test isequal(m.modified, [x]) @test m.mod_syms == [:x] @test m.ctx === nothing - - m = ModelingToolkit.MutatingFunctionalAffect(fmfa, (; y=x), (; y=x)) + + m = ModelingToolkit.MutatingFunctionalAffect(fmfa, (; y = x), (; y = x)) @test m isa ModelingToolkit.MutatingFunctionalAffect @test m.f == fmfa @test isequal(m.obs, [x]) @@ -309,8 +309,9 @@ end @test isequal(m.modified, [x]) @test m.mod_syms == [:y] @test m.ctx === nothing - - m = ModelingToolkit.MutatingFunctionalAffect(fmfa; modified=(; y=x), observed=(; y=x)) + + m = ModelingToolkit.MutatingFunctionalAffect( + fmfa; modified = (; y = x), observed = (; y = x)) @test m isa ModelingToolkit.MutatingFunctionalAffect @test m.f == fmfa @test isequal(m.obs, [x]) @@ -318,8 +319,9 @@ end @test isequal(m.modified, [x]) @test m.mod_syms == [:y] @test m.ctx === nothing - - m = ModelingToolkit.MutatingFunctionalAffect(fmfa; modified=(; y=x), observed=(; y=x), ctx=3) + + m = ModelingToolkit.MutatingFunctionalAffect( + fmfa; modified = (; y = x), observed = (; y = x), ctx = 3) @test m isa ModelingToolkit.MutatingFunctionalAffect @test m.f == fmfa @test isequal(m.obs, [x]) @@ -327,7 +329,7 @@ end @test isequal(m.modified, [x]) @test m.mod_syms == [:y] @test m.ctx === 3 - + m = ModelingToolkit.MutatingFunctionalAffect(fmfa, (; x), (; x), 3) @test m isa ModelingToolkit.MutatingFunctionalAffect @test m.f == fmfa @@ -1005,151 +1007,176 @@ end D(temp) ~ furnace_on * furnace_power - temp^2 * leakage ] - furnace_off = ModelingToolkit.SymbolicContinuousCallback([temp ~ furnace_off_threshold], - ModelingToolkit.MutatingFunctionalAffect(modified=(; furnace_on)) do x, o, i, c + furnace_off = ModelingToolkit.SymbolicContinuousCallback( + [temp ~ furnace_off_threshold], + ModelingToolkit.MutatingFunctionalAffect(modified = (; furnace_on)) do x, o, i, c x.furnace_on = false end) - furnace_enable = ModelingToolkit.SymbolicContinuousCallback([temp ~ furnace_on_threshold], - ModelingToolkit.MutatingFunctionalAffect(modified=(; furnace_on)) do x, o, i, c + furnace_enable = ModelingToolkit.SymbolicContinuousCallback( + [temp ~ furnace_on_threshold], + ModelingToolkit.MutatingFunctionalAffect(modified = (; furnace_on)) do x, o, i, c x.furnace_on = true end) - @named sys = ODESystem(eqs, t, [temp], params; continuous_events = [furnace_off, furnace_enable]) + @named sys = ODESystem( + eqs, t, [temp], params; continuous_events = [furnace_off, furnace_enable]) ss = structural_simplify(sys) prob = ODEProblem(ss, [temp => 0.0, furnace_on => true], (0.0, 100.0)) - sol = solve(prob, Tsit5(); dtmax=0.01) + sol = solve(prob, Tsit5(); dtmax = 0.01) @test all(sol[temp][sol.t .> 1.0] .<= 0.79) && all(sol[temp][sol.t .> 1.0] .>= 0.49) - furnace_off = ModelingToolkit.SymbolicContinuousCallback([temp ~ furnace_off_threshold], - ModelingToolkit.MutatingFunctionalAffect(modified=(; furnace_on)) do x, o, i + furnace_off = ModelingToolkit.SymbolicContinuousCallback( + [temp ~ furnace_off_threshold], + ModelingToolkit.MutatingFunctionalAffect(modified = (; furnace_on)) do x, o, i x.furnace_on = false end) - furnace_enable = ModelingToolkit.SymbolicContinuousCallback([temp ~ furnace_on_threshold], - ModelingToolkit.MutatingFunctionalAffect(modified=(; furnace_on)) do x, o, i + furnace_enable = ModelingToolkit.SymbolicContinuousCallback( + [temp ~ furnace_on_threshold], + ModelingToolkit.MutatingFunctionalAffect(modified = (; furnace_on)) do x, o, i x.furnace_on = true end) - @named sys = ODESystem(eqs, t, [temp], params; continuous_events = [furnace_off, furnace_enable]) + @named sys = ODESystem( + eqs, t, [temp], params; continuous_events = [furnace_off, furnace_enable]) ss = structural_simplify(sys) prob = ODEProblem(ss, [temp => 0.0, furnace_on => true], (0.0, 100.0)) - sol = solve(prob, Tsit5(); dtmax=0.01) + sol = solve(prob, Tsit5(); dtmax = 0.01) @test all(sol[temp][sol.t .> 1.0] .<= 0.79) && all(sol[temp][sol.t .> 1.0] .>= 0.49) - furnace_off = ModelingToolkit.SymbolicContinuousCallback([temp ~ furnace_off_threshold], - ModelingToolkit.MutatingFunctionalAffect(modified=(; furnace_on)) do x, o + furnace_off = ModelingToolkit.SymbolicContinuousCallback( + [temp ~ furnace_off_threshold], + ModelingToolkit.MutatingFunctionalAffect(modified = (; furnace_on)) do x, o x.furnace_on = false end) - furnace_enable = ModelingToolkit.SymbolicContinuousCallback([temp ~ furnace_on_threshold], - ModelingToolkit.MutatingFunctionalAffect(modified=(; furnace_on)) do x, o + furnace_enable = ModelingToolkit.SymbolicContinuousCallback( + [temp ~ furnace_on_threshold], + ModelingToolkit.MutatingFunctionalAffect(modified = (; furnace_on)) do x, o x.furnace_on = true end) - @named sys = ODESystem(eqs, t, [temp], params; continuous_events = [furnace_off, furnace_enable]) + @named sys = ODESystem( + eqs, t, [temp], params; continuous_events = [furnace_off, furnace_enable]) ss = structural_simplify(sys) prob = ODEProblem(ss, [temp => 0.0, furnace_on => true], (0.0, 100.0)) - sol = solve(prob, Tsit5(); dtmax=0.01) + sol = solve(prob, Tsit5(); dtmax = 0.01) @test all(sol[temp][sol.t .> 1.0] .<= 0.79) && all(sol[temp][sol.t .> 1.0] .>= 0.49) - furnace_off = ModelingToolkit.SymbolicContinuousCallback([temp ~ furnace_off_threshold], - ModelingToolkit.MutatingFunctionalAffect(modified=(; furnace_on)) do x + furnace_off = ModelingToolkit.SymbolicContinuousCallback( + [temp ~ furnace_off_threshold], + ModelingToolkit.MutatingFunctionalAffect(modified = (; furnace_on)) do x x.furnace_on = false end) - furnace_enable = ModelingToolkit.SymbolicContinuousCallback([temp ~ furnace_on_threshold], - ModelingToolkit.MutatingFunctionalAffect(modified=(; furnace_on)) do x + furnace_enable = ModelingToolkit.SymbolicContinuousCallback( + [temp ~ furnace_on_threshold], + ModelingToolkit.MutatingFunctionalAffect(modified = (; furnace_on)) do x x.furnace_on = true end) - @named sys = ODESystem(eqs, t, [temp], params; continuous_events = [furnace_off, furnace_enable]) + @named sys = ODESystem( + eqs, t, [temp], params; continuous_events = [furnace_off, furnace_enable]) ss = structural_simplify(sys) prob = ODEProblem(ss, [temp => 0.0, furnace_on => true], (0.0, 100.0)) - sol = solve(prob, Tsit5(); dtmax=0.01) + sol = solve(prob, Tsit5(); dtmax = 0.01) @test all(sol[temp][sol.t .> 1.0] .<= 0.79) && all(sol[temp][sol.t .> 1.0] .>= 0.49) end -@testset "MutatingFunctionalAffect errors and warnings" begin +@testset "MutatingFunctionalAffect errors and warnings" begin @variables temp(t) params = @parameters furnace_on_threshold=0.5 furnace_off_threshold=0.7 furnace_power=1.0 leakage=0.1 furnace_on::Bool=false eqs = [ D(temp) ~ furnace_on * furnace_power - temp^2 * leakage ] - furnace_off = ModelingToolkit.SymbolicContinuousCallback([temp ~ furnace_off_threshold], - ModelingToolkit.MutatingFunctionalAffect(modified=(; furnace_on), observed=(; furnace_on)) do x, o, c, i + furnace_off = ModelingToolkit.SymbolicContinuousCallback( + [temp ~ furnace_off_threshold], + ModelingToolkit.MutatingFunctionalAffect( + modified = (; furnace_on), observed = (; furnace_on)) do x, o, c, i x.furnace_on = false end) @named sys = ODESystem(eqs, t, [temp], params; continuous_events = [furnace_off]) ss = structural_simplify(sys) - @test_logs (:warn, "The symbols Any[:furnace_on] are declared as both observed and modified; this is a code smell because it becomes easy to confuse them and assign/not assign a value.") prob = ODEProblem(ss, [temp => 0.0, furnace_on => true], (0.0, 100.0)) + @test_logs (:warn, + "The symbols Any[:furnace_on] are declared as both observed and modified; this is a code smell because it becomes easy to confuse them and assign/not assign a value.") prob=ODEProblem( + ss, [temp => 0.0, furnace_on => true], (0.0, 100.0)) @variables tempsq(t) # trivially eliminated - eqs = [ - tempsq ~ temp^2 - D(temp) ~ furnace_on * furnace_power - temp^2 * leakage - ] + eqs = [tempsq ~ temp^2 + D(temp) ~ furnace_on * furnace_power - temp^2 * leakage] - furnace_off = ModelingToolkit.SymbolicContinuousCallback([temp ~ furnace_off_threshold], - ModelingToolkit.MutatingFunctionalAffect(modified=(; furnace_on, tempsq), observed=(; furnace_on)) do x, o, c, i + furnace_off = ModelingToolkit.SymbolicContinuousCallback( + [temp ~ furnace_off_threshold], + ModelingToolkit.MutatingFunctionalAffect( + modified = (; furnace_on, tempsq), observed = (; furnace_on)) do x, o, c, i x.furnace_on = false end) - @named sys = ODESystem(eqs, t, [temp, tempsq], params; continuous_events = [furnace_off]) + @named sys = ODESystem( + eqs, t, [temp, tempsq], params; continuous_events = [furnace_off]) ss = structural_simplify(sys) - @test_throws "refers to missing variable(s)" prob = ODEProblem(ss, [temp => 0.0, furnace_on => true], (0.0, 100.0)) + @test_throws "refers to missing variable(s)" prob=ODEProblem( + ss, [temp => 0.0, furnace_on => true], (0.0, 100.0)) - @parameters not_actually_here - furnace_off = ModelingToolkit.SymbolicContinuousCallback([temp ~ furnace_off_threshold], - ModelingToolkit.MutatingFunctionalAffect(modified=(; furnace_on), observed=(; furnace_on, not_actually_here)) do x, o, c, i + furnace_off = ModelingToolkit.SymbolicContinuousCallback( + [temp ~ furnace_off_threshold], + ModelingToolkit.MutatingFunctionalAffect(modified = (; furnace_on), + observed = (; furnace_on, not_actually_here)) do x, o, c, i x.furnace_on = false end) - @named sys = ODESystem(eqs, t, [temp, tempsq], params; continuous_events = [furnace_off]) + @named sys = ODESystem( + eqs, t, [temp, tempsq], params; continuous_events = [furnace_off]) ss = structural_simplify(sys) - @test_throws "refers to missing variable(s)" prob = ODEProblem(ss, [temp => 0.0, furnace_on => true], (0.0, 100.0)) + @test_throws "refers to missing variable(s)" prob=ODEProblem( + ss, [temp => 0.0, furnace_on => true], (0.0, 100.0)) end -@testset "Quadrature" begin +@testset "Quadrature" begin @variables theta(t) omega(t) params = @parameters qA=0 qB=0 hA=0 hB=0 cnt=0 - eqs = [ - D(theta) ~ omega - omega ~ 1.0 - ] + eqs = [D(theta) ~ omega + omega ~ 1.0] function decoder(oldA, oldB, newA, newB) state = (oldA, oldB, newA, newB) - if state == (0, 0, 1, 0) || state == (1, 0, 1, 1) || state == (1, 1, 0, 1) || state == (0, 1, 0, 0) + if state == (0, 0, 1, 0) || state == (1, 0, 1, 1) || state == (1, 1, 0, 1) || + state == (0, 1, 0, 0) return 1 - elseif state == (0, 0, 0, 1) || state == (0, 1, 1, 1) || state == (1, 1, 1, 0) || state == (1, 0, 0, 0) + elseif state == (0, 0, 0, 1) || state == (0, 1, 1, 1) || state == (1, 1, 1, 0) || + state == (1, 0, 0, 0) return -1 - elseif state == (0, 0, 0, 0) || state == (0, 1, 0, 1) || state == (1, 0, 1, 0) || state == (1, 1, 1, 1) + elseif state == (0, 0, 0, 0) || state == (0, 1, 0, 1) || state == (1, 0, 1, 0) || + state == (1, 1, 1, 1) return 0 else return 0 # err is interpreted as no movement end end - qAevt = ModelingToolkit.SymbolicContinuousCallback([cos(100 * theta) ~ 0], + qAevt = ModelingToolkit.SymbolicContinuousCallback([cos(100 * theta) ~ 0], ModelingToolkit.MutatingFunctionalAffect((; qA, hA, hB, cnt), (; qB)) do x, o, i, c x.hA = x.qA x.hB = o.qB x.qA = 1 x.cnt += decoder(x.hA, x.hB, x.qA, o.qB) end, - affect_neg = ModelingToolkit.MutatingFunctionalAffect((; qA, hA, hB, cnt), (; qB)) do x, o, c, i + affect_neg = ModelingToolkit.MutatingFunctionalAffect( + (; qA, hA, hB, cnt), (; qB)) do x, o, c, i x.hA = x.qA x.hB = o.qB x.qA = 0 x.cnt += decoder(x.hA, x.hB, x.qA, o.qB) - end; rootfind=SciMLBase.RightRootFind) - qBevt = ModelingToolkit.SymbolicContinuousCallback([cos(100 * theta - π/2) ~ 0], + end; rootfind = SciMLBase.RightRootFind) + qBevt = ModelingToolkit.SymbolicContinuousCallback([cos(100 * theta - π / 2) ~ 0], ModelingToolkit.MutatingFunctionalAffect((; qB, hA, hB, cnt), (; qA)) do x, o, i, c x.hA = o.qA x.hB = x.qB x.qB = 1 x.cnt += decoder(x.hA, x.hB, o.qA, x.qB) end, - affect_neg = ModelingToolkit.MutatingFunctionalAffect((; qB, hA, hB, cnt), (; qA)) do x, o, c, i + affect_neg = ModelingToolkit.MutatingFunctionalAffect( + (; qB, hA, hB, cnt), (; qA)) do x, o, c, i x.hA = o.qA x.hB = x.qB x.qB = 0 x.cnt += decoder(x.hA, x.hB, o.qA, x.qB) - end; rootfind=SciMLBase.RightRootFind) - @named sys = ODESystem(eqs, t, [theta, omega], params; continuous_events = [qAevt, qBevt]) + end; rootfind = SciMLBase.RightRootFind) + @named sys = ODESystem( + eqs, t, [theta, omega], params; continuous_events = [qAevt, qBevt]) ss = structural_simplify(sys) prob = ODEProblem(ss, [theta => 0.0], (0.0, pi)) - sol = solve(prob, Tsit5(); dtmax=0.01) + sol = solve(prob, Tsit5(); dtmax = 0.01) @test sol[cnt] == 197 # we get 2 pulses per phase cycle (cos 0 crossing) and we go to 100 cycles; we miss a few due to the initial state end From 61cf6762c43e595448ed6f37f37bd889db663b10 Mon Sep 17 00:00:00 2001 From: Ben Chung Date: Fri, 2 Aug 2024 18:44:18 -0700 Subject: [PATCH 0166/2176] FIx SCC reconstruction --- src/systems/callbacks.jl | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/systems/callbacks.jl b/src/systems/callbacks.jl index 5c6f9b2801..8ea7d9f506 100644 --- a/src/systems/callbacks.jl +++ b/src/systems/callbacks.jl @@ -284,13 +284,15 @@ end namespace_affects(af::Vector, s) = Equation[namespace_affect(a, s) for a in af] namespace_affects(af::FunctionalAffect, s) = namespace_affect(af, s) +namespace_affects(af::MutatingFunctionalAffect, s) = namespace_affect(af, s) namespace_affects(::Nothing, s) = nothing function namespace_callback(cb::SymbolicContinuousCallback, s)::SymbolicContinuousCallback - SymbolicContinuousCallback( - namespace_equation.(equations(cb), (s,)), - namespace_affects(affects(cb), s); - affect_neg = namespace_affects(affect_negs(cb), s)) + SymbolicContinuousCallback(; + eqs = namespace_equation.(equations(cb), (s,)), + affect = namespace_affects(affects(cb), s), + affect_neg = namespace_affects(affect_negs(cb), s), + rootfind = cb.rootfind) end """ From 8edef14b63637a4132e5139540d3ec89fa9d39fd Mon Sep 17 00:00:00 2001 From: Benjamin Chung Date: Mon, 19 Aug 2024 07:49:33 -0700 Subject: [PATCH 0167/2176] Implement initialize and finalize affects for symbolic callbacks --- src/systems/callbacks.jl | 206 +++++++++++++++++++++++++++++---------- 1 file changed, 154 insertions(+), 52 deletions(-) diff --git a/src/systems/callbacks.jl b/src/systems/callbacks.jl index 8ea7d9f506..d14fd50691 100644 --- a/src/systems/callbacks.jl +++ b/src/systems/callbacks.jl @@ -104,28 +104,38 @@ The affect function updates the value at `x` in `modified` to be the result of e modified::Vector mod_syms::Vector{Symbol} ctx::Any + skip_checks::Bool end function MutatingFunctionalAffect(f::Function; observed::NamedTuple = NamedTuple{()}(()), modified::NamedTuple = NamedTuple{()}(()), - ctx = nothing) - MutatingFunctionalAffect(f, collect(values(observed)), collect(keys(observed)), - collect(values(modified)), collect(keys(modified)), ctx) + ctx = nothing, + skip_checks = false) + MutatingFunctionalAffect(f, + collect(values(observed)), collect(keys(observed)), + collect(values(modified)), collect(keys(modified)), + ctx, skip_checks) end function MutatingFunctionalAffect(f::Function, modified::NamedTuple; - observed::NamedTuple = NamedTuple{()}(()), ctx = nothing) - MutatingFunctionalAffect(f, observed = observed, modified = modified, ctx = ctx) + observed::NamedTuple = NamedTuple{()}(()), ctx = nothing, skip_checks=false) + MutatingFunctionalAffect(f, observed = observed, modified = modified, ctx = ctx, skip_checks = skip_checks) end function MutatingFunctionalAffect( - f::Function, modified::NamedTuple, observed::NamedTuple; ctx = nothing) - MutatingFunctionalAffect(f, observed = observed, modified = modified, ctx = ctx) + f::Function, modified::NamedTuple, observed::NamedTuple; ctx = nothing, skip_checks=false) + MutatingFunctionalAffect(f, observed = observed, modified = modified, ctx = ctx, skip_checks = skip_checks) end function MutatingFunctionalAffect( - f::Function, modified::NamedTuple, observed::NamedTuple, ctx) - MutatingFunctionalAffect(f, observed = observed, modified = modified, ctx = ctx) + f::Function, modified::NamedTuple, observed::NamedTuple, ctx; skip_checks=false) + MutatingFunctionalAffect(f, observed = observed, modified = modified, ctx = ctx, skip_checks = skip_checks) end +function Base.show(io::IO, mfa::MutatingFunctionalAffect) + obs_vals = join(map((ob,nm) -> "$ob => $nm", mfa.obs, mfa.obs_syms), ", ") + mod_vals = join(map((md,nm) -> "$md => $nm", mfa.modified, mfa.mod_syms), ", ") + affect = mfa.f + print(io, "MutatingFunctionalAffect(observed: [$obs_vals], modified: [$mod_vals], affect:$affect)") +end func(f::MutatingFunctionalAffect) = f.f context(a::MutatingFunctionalAffect) = a.ctx observed(a::MutatingFunctionalAffect) = a.obs @@ -208,12 +218,19 @@ Affects (i.e. `affect` and `affect_neg`) can be specified as either: """ struct SymbolicContinuousCallback eqs::Vector{Equation} + initialize::Union{Vector{Equation}, FunctionalAffect, MutatingFunctionalAffect} + finalize::Union{Vector{Equation}, FunctionalAffect, MutatingFunctionalAffect} affect::Union{Vector{Equation}, FunctionalAffect, MutatingFunctionalAffect} affect_neg::Union{Vector{Equation}, FunctionalAffect, MutatingFunctionalAffect, Nothing} rootfind::SciMLBase.RootfindOpt - function SymbolicContinuousCallback(; eqs::Vector{Equation}, affect = NULL_AFFECT, - affect_neg = affect, rootfind = SciMLBase.LeftRootFind) - new(eqs, make_affect(affect), make_affect(affect_neg), rootfind) + function SymbolicContinuousCallback(; + eqs::Vector{Equation}, + affect = NULL_AFFECT, + affect_neg = affect, + rootfind = SciMLBase.LeftRootFind, + initialize=NULL_AFFECT, + finalize=NULL_AFFECT) + new(eqs, initialize, finalize, make_affect(affect), make_affect(affect_neg), rootfind) end # Default affect to nothing end make_affect(affect) = affect @@ -221,18 +238,81 @@ make_affect(affect::Tuple) = FunctionalAffect(affect...) make_affect(affect::NamedTuple) = FunctionalAffect(; affect...) function Base.:(==)(e1::SymbolicContinuousCallback, e2::SymbolicContinuousCallback) - isequal(e1.eqs, e2.eqs) && isequal(e1.affect, e2.affect) && + isequal(e1.eqs, e2.eqs) && isequal(e1.affect, e2.affect) && + isequal(e1.initialize, e2.initialize) && isequal(e1.finalize, e2.finalize) && isequal(e1.affect_neg, e2.affect_neg) && isequal(e1.rootfind, e2.rootfind) end Base.isempty(cb::SymbolicContinuousCallback) = isempty(cb.eqs) function Base.hash(cb::SymbolicContinuousCallback, s::UInt) + hash_affect(affect::AbstractVector, s) = foldr(hash, affect, init = s) + hash_affect(affect, s) = hash(cb.affect, s) s = foldr(hash, cb.eqs, init = s) - s = cb.affect isa AbstractVector ? foldr(hash, cb.affect, init = s) : hash(cb.affect, s) - s = cb.affect_neg isa AbstractVector ? foldr(hash, cb.affect_neg, init = s) : - hash(cb.affect_neg, s) + s = hash_affect(cb.affect, s) + s = hash_affect(cb.affect_neg, s) + s = hash_affect(cb.initialize, s) + s = hash_affect(cb.finalize, s) hash(cb.rootfind, s) end + +function Base.show(io::IO, cb::SymbolicContinuousCallback) + indent = get(io, :indent, 0) + iio = IOContext(io, :indent => indent+1) + print(io, "SymbolicContinuousCallback(") + print(iio, "Equations:") + show(iio, equations(cb)) + print(iio, "; ") + if affects(cb) != NULL_AFFECT + print(iio, "Affect:") + show(iio, affects(cb)) + print(iio, ", ") + end + if affect_negs(cb) != NULL_AFFECT + print(iio, "Negative-edge affect:") + show(iio, affect_negs(cb)) + print(iio, ", ") + end + if initialize_affects(cb) != NULL_AFFECT + print(iio, "Initialization affect:") + show(iio, initialize_affects(cb)) + print(iio, ", ") + end + if finalize_affects(cb) != NULL_AFFECT + print(iio, "Finalization affect:") + show(iio, finalize_affects(cb)) + end + print(iio, ")") +end + +function Base.show(io::IO, mime::MIME"text/plain", cb::SymbolicContinuousCallback) + indent = get(io, :indent, 0) + iio = IOContext(io, :indent => indent+1) + println(io, "SymbolicContinuousCallback:") + println(iio, "Equations:") + show(iio, mime, equations(cb)) + print(iio, "\n") + if affects(cb) != NULL_AFFECT + println(iio, "Affect:") + show(iio, mime, affects(cb)) + print(iio, "\n") + end + if affect_negs(cb) != NULL_AFFECT + println(iio, "Negative-edge affect:") + show(iio, mime, affect_negs(cb)) + print(iio, "\n") + end + if initialize_affects(cb) != NULL_AFFECT + println(iio, "Initialization affect:") + show(iio, mime, initialize_affects(cb)) + print(iio, "\n") + end + if finalize_affects(cb) != NULL_AFFECT + println(iio, "Finalization affect:") + show(iio, mime, finalize_affects(cb)) + print(iio, "\n") + end +end + to_equation_vector(eq::Equation) = [eq] to_equation_vector(eqs::Vector{Equation}) = eqs function to_equation_vector(eqs::Vector{Any}) @@ -246,14 +326,14 @@ end # wrap eq in vector SymbolicContinuousCallback(p::Pair) = SymbolicContinuousCallback(p[1], p[2]) SymbolicContinuousCallback(cb::SymbolicContinuousCallback) = cb # passthrough function SymbolicContinuousCallback(eqs::Equation, affect = NULL_AFFECT; - affect_neg = affect, rootfind = SciMLBase.LeftRootFind) + affect_neg = affect, rootfind = SciMLBase.LeftRootFind, initialize = NULL_AFFECT, finalize = NULL_AFFECT) SymbolicContinuousCallback( - eqs = [eqs], affect = affect, affect_neg = affect_neg, rootfind = rootfind) + eqs = [eqs], affect = affect, affect_neg = affect_neg, rootfind = rootfind, initialize=initialize, finalize=finalize) end function SymbolicContinuousCallback(eqs::Vector{Equation}, affect = NULL_AFFECT; - affect_neg = affect, rootfind = SciMLBase.LeftRootFind) + affect_neg = affect, rootfind = SciMLBase.LeftRootFind, initialize = NULL_AFFECT, finalize = NULL_AFFECT) SymbolicContinuousCallback( - eqs = eqs, affect = affect, affect_neg = affect_neg, rootfind = rootfind) + eqs = eqs, affect = affect, affect_neg = affect_neg, rootfind = rootfind, initialize=initialize, finalize=finalize) end SymbolicContinuousCallbacks(cb::SymbolicContinuousCallback) = [cb] @@ -282,6 +362,16 @@ function affect_negs(cbs::Vector{SymbolicContinuousCallback}) mapreduce(affect_negs, vcat, cbs, init = Equation[]) end +initialize_affects(cb::SymbolicContinuousCallback) = cb.initialize +function initialize_affects(cbs::Vector{SymbolicContinuousCallback}) + mapreduce(initialize_affects, vcat, cbs, init = Equation[]) +end + +finalize_affects(cb::SymbolicContinuousCallback) = cb.initialize +function finalize_affects(cbs::Vector{SymbolicContinuousCallback}) + mapreduce(finalize_affects, vcat, cbs, init = Equation[]) +end + namespace_affects(af::Vector, s) = Equation[namespace_affect(a, s) for a in af] namespace_affects(af::FunctionalAffect, s) = namespace_affect(af, s) namespace_affects(af::MutatingFunctionalAffect, s) = namespace_affect(af, s) @@ -292,6 +382,8 @@ function namespace_callback(cb::SymbolicContinuousCallback, s)::SymbolicContinuo eqs = namespace_equation.(equations(cb), (s,)), affect = namespace_affects(affects(cb), s), affect_neg = namespace_affects(affect_negs(cb), s), + initialize = namespace_affects(initialize_affects(cb), s), + finalize = namespace_affects(finalize_affects(cb), s), rootfind = cb.rootfind) end @@ -681,8 +773,9 @@ function generate_single_rootfinding_callback( initfn = SciMLBase.INITIALIZE_DEFAULT end return ContinuousCallback( - cond, affect_function.affect, affect_function.affect_neg, - rootfind = cb.rootfind, initialize = initfn) + cond, affect_function.affect, affect_function.affect_neg, rootfind = cb.rootfind, + initialize = isnothing(affect_function.initialize) ? SciMLBase.INITIALIZE_DEFAULT : (c, u, t, i) -> affect_function.initialize(i), + finalize = isnothing(affect_function.finalize) ? SciMLBase.FINALIZE_DEFAULT : (c, u, t, i) -> affect_function.finalize(i)) end function generate_vector_rootfinding_callback( @@ -702,13 +795,12 @@ function generate_vector_rootfinding_callback( _, rf_ip = generate_custom_function( sys, rhss, dvs, ps; expression = Val{false}, kwargs...) - affect_functions = @NamedTuple{affect::Function, affect_neg::Union{Function, Nothing}}[compile_affect_fn( - cb, - sys, - dvs, - ps, - kwargs) - for cb in cbs] + affect_functions = @NamedTuple{ + affect::Function, + affect_neg::Union{Function, Nothing}, + initialize::Union{Function, Nothing}, + finalize::Union{Function, Nothing}}[ + compile_affect_fn(cb, sys, dvs, ps, kwargs) for cb in cbs] cond = function (out, u, t, integ) rf_ip(out, u, parameter_values(integ), t) end @@ -734,25 +826,27 @@ function generate_vector_rootfinding_callback( affect_neg(integ) end end - if has_index_cache(sys) && (ic = get_index_cache(sys)) !== nothing - save_idxs = mapreduce( - cb -> get(ic.callback_to_clocks, cb, Int[]), vcat, cbs; init = Int[]) - initfn = if isempty(save_idxs) - SciMLBase.INITIALIZE_DEFAULT + function handle_optional_setup_fn(funs, default) + if all(isnothing, funs) + return default else - let save_idxs = save_idxs - function (cb, u, t, integrator) - for idx in save_idxs - SciMLBase.save_discretes!(integrator, idx) + return let funs = funs + function (cb, u, t, integ) + for func in funs + if isnothing(func) + continue + else + func(integ) + end end end end end - else - initfn = SciMLBase.INITIALIZE_DEFAULT end + initialize = handle_optional_setup_fn(map(fn -> fn.initialize, affect_functions), SciMLBase.INITIALIZE_DEFAULT) + finalize = handle_optional_setup_fn(map(fn -> fn.finalize, affect_functions), SciMLBase.FINALIZE_DEFAULT) return VectorContinuousCallback( - cond, affect, affect_neg, length(eqs), rootfind = rootfind, initialize = initfn) + cond, affect, affect_neg, length(eqs), rootfind = rootfind, initialize = initialize, finalize = finalize) end """ @@ -762,15 +856,23 @@ function compile_affect_fn(cb, sys::AbstractODESystem, dvs, ps, kwargs) eq_aff = affects(cb) eq_neg_aff = affect_negs(cb) affect = compile_affect(eq_aff, cb, sys, dvs, ps; expression = Val{false}, kwargs...) + function compile_optional_affect(aff) + if isnothing(aff) + return nothing + else + affspr = compile_affect(aff, cb, sys, dvs, ps; expression = Val{true}, kwargs...) + @show affspr + return compile_affect(aff, cb, sys, dvs, ps; expression = Val{false}, kwargs...) + end + end if eq_neg_aff === eq_aff affect_neg = affect - elseif isnothing(eq_neg_aff) - affect_neg = nothing else - affect_neg = compile_affect( - eq_neg_aff, cb, sys, dvs, ps; expression = Val{false}, kwargs...) + affect_neg = compile_optional_affect(eq_neg_aff) end - (affect = affect, affect_neg = affect_neg) + initialize = compile_optional_affect(initialize_affects(cb)) + finalize = compile_optional_affect(finalize_affects(cb)) + (affect = affect, affect_neg = affect_neg, initialize = initialize, finalize = finalize) end function generate_rootfinding_callback(cbs, sys::AbstractODESystem, dvs = unknowns(sys), @@ -877,7 +979,7 @@ function compile_user_affect(affect::MutatingFunctionalAffect, sys, dvs, ps; kwa push!(syms_dedup, sym) push!(exprs_dedup, exp) push!(seen, sym) - else + elseif !affect.skip_checks @warn "Expression $(expr) is aliased as $sym, which has already been used. The first definition will be used." end end @@ -887,7 +989,7 @@ function compile_user_affect(affect::MutatingFunctionalAffect, sys, dvs, ps; kwa obs_exprs = observed(affect) for oexpr in obs_exprs invalid_vars = invalid_variables(sys, oexpr) - if length(invalid_vars) > 0 + if length(invalid_vars) > 0 && !affect.skip_checks error("Observed equation $(oexpr) in affect refers to missing variable(s) $(invalid_vars); the variables may not have been added (e.g. if a component is missing).") end end @@ -897,11 +999,11 @@ function compile_user_affect(affect::MutatingFunctionalAffect, sys, dvs, ps; kwa mod_exprs = modified(affect) for mexpr in mod_exprs - if !is_observed(sys, mexpr) && parameter_index(sys, mexpr) === nothing - error("Expression $mexpr cannot be assigned to; currently only unknowns and parameters may be updated by an affect.") + if !is_observed(sys, mexpr) && parameter_index(sys, mexpr) === nothing && !affect.skip_checks + @warn ("Expression $mexpr cannot be assigned to; currently only unknowns and parameters may be updated by an affect.") end invalid_vars = unassignable_variables(sys, mexpr) - if length(invalid_vars) > 0 + if length(invalid_vars) > 0 && !affect.skip_checks error("Modified equation $(mexpr) in affect refers to missing variable(s) $(invalid_vars); the variables may not have been added (e.g. if a component is missing) or they may have been reduced away.") end end @@ -911,7 +1013,7 @@ function compile_user_affect(affect::MutatingFunctionalAffect, sys, dvs, ps; kwa sys, mod_exprs; return_inplace = true) overlapping_syms = intersect(mod_syms, obs_syms) - if length(overlapping_syms) > 0 + if length(overlapping_syms) > 0 && !affect.skip_checks @warn "The symbols $overlapping_syms are declared as both observed and modified; this is a code smell because it becomes easy to confuse them and assign/not assign a value." end From 3c7afd8c40b526dd6f139aa2d119095ab6debeb7 Mon Sep 17 00:00:00 2001 From: Benjamin Chung Date: Mon, 19 Aug 2024 08:08:13 -0700 Subject: [PATCH 0168/2176] Test some simple initialization affects --- test/symbolic_events.jl | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/test/symbolic_events.jl b/test/symbolic_events.jl index 7aba69dc7f..b0f230d75e 100644 --- a/test/symbolic_events.jl +++ b/test/symbolic_events.jl @@ -1074,6 +1074,26 @@ end prob = ODEProblem(ss, [temp => 0.0, furnace_on => true], (0.0, 100.0)) sol = solve(prob, Tsit5(); dtmax = 0.01) @test all(sol[temp][sol.t .> 1.0] .<= 0.79) && all(sol[temp][sol.t .> 1.0] .>= 0.49) + + furnace_off = ModelingToolkit.SymbolicContinuousCallback( + [temp ~ furnace_off_threshold], + ModelingToolkit.MutatingFunctionalAffect(modified = (; furnace_on)) do x + x.furnace_on = false + end; initialize = ModelingToolkit.MutatingFunctionalAffect(modified = (; temp)) do x + x.temp = 0.2 + end) + furnace_enable = ModelingToolkit.SymbolicContinuousCallback( + [temp ~ furnace_on_threshold], + ModelingToolkit.MutatingFunctionalAffect(modified = (; furnace_on)) do x, o, c, i + x.furnace_on = true + end) + @named sys = ODESystem( + eqs, t, [temp], params; continuous_events = [furnace_off, furnace_enable]) + ss = structural_simplify(sys) + prob = ODEProblem(ss, [temp => 0.0, furnace_on => true], (0.0, 100.0)) + sol = solve(prob, Tsit5(); dtmax = 0.01) + @test all(sol[temp][sol.t .> 1.0] .<= 0.79) && all(sol[temp][sol.t .> 1.0] .>= 0.49) + @test all(sol[temp][sol.t .!= 0.0] .<= 0.79) && all(sol[temp][sol.t .!= 0.0] .>= 0.2) end @testset "MutatingFunctionalAffect errors and warnings" begin From 95956f363eaa8bbc5b492452946c6c2a21e35bbd Mon Sep 17 00:00:00 2001 From: Benjamin Chung Date: Thu, 22 Aug 2024 07:17:50 -0700 Subject: [PATCH 0169/2176] Properly pass skip_checks through --- src/systems/callbacks.jl | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/systems/callbacks.jl b/src/systems/callbacks.jl index d14fd50691..ca0a64b452 100644 --- a/src/systems/callbacks.jl +++ b/src/systems/callbacks.jl @@ -165,7 +165,8 @@ function namespace_affect(affect::MutatingFunctionalAffect, s) observed_syms(affect), renamespace.((s,), modified(affect)), modified_syms(affect), - context(affect)) + context(affect), + affect.skip_checks) end function has_functional_affect(cb) From 4f928ae57e33b2d8bb5e52faed229941b5e589af Mon Sep 17 00:00:00 2001 From: Benjamin Chung Date: Thu, 22 Aug 2024 07:35:38 -0700 Subject: [PATCH 0170/2176] Support time-indexed parameters --- src/systems/callbacks.jl | 16 ++++++++++++++-- src/systems/index_cache.jl | 2 +- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/src/systems/callbacks.jl b/src/systems/callbacks.jl index ca0a64b452..279d31483e 100644 --- a/src/systems/callbacks.jl +++ b/src/systems/callbacks.jl @@ -963,7 +963,7 @@ function unassignable_variables(sys, expr) x -> !any(isequal(x), assignable_syms), reduce(vcat, vars(expr); init = [])) end -function compile_user_affect(affect::MutatingFunctionalAffect, sys, dvs, ps; kwargs...) +function compile_user_affect(affect::MutatingFunctionalAffect, cb, sys, dvs, ps; kwargs...) #= Implementation sketch: generate observed function (oop), should save to a component array under obs_syms @@ -1049,6 +1049,13 @@ function compile_user_affect(affect::MutatingFunctionalAffect, sys, dvs, ps; kwa collect(mkzero(size(e)) for e in first.(mod_unk_pairs))])) upd_params_view = view(upd_component_array, last.(mod_param_pairs)) upd_unks_view = view(upd_component_array, last.(mod_unk_pairs)) + + if has_index_cache(sys) && (ic = get_index_cache(sys)) !== nothing + save_idxs = get(ic.callback_to_clocks, cb, Int[]) + else + save_idxs = Int[] + end + let user_affect = func(affect), ctx = context(affect) function (integ) # update the to-be-mutated values; this ensures that if you do a no-op then nothing happens @@ -1074,11 +1081,16 @@ function compile_user_affect(affect::MutatingFunctionalAffect, sys, dvs, ps; kwa # write the new values back to the integrator upd_params_fun(integ, upd_params_view) upd_unk_fun(integ, upd_unks_view) + + + for idx in save_idxs + SciMLBase.save_discretes!(integ, idx) + end end end end -function compile_affect(affect::FunctionalAffect, cb, sys, dvs, ps; kwargs...) +function compile_affect(affect::Union{FunctionalAffect, MutatingFunctionalAffect}, cb, sys, dvs, ps; kwargs...) compile_user_affect(affect, cb, sys, dvs, ps; kwargs...) end diff --git a/src/systems/index_cache.jl b/src/systems/index_cache.jl index 00f7837407..55d819990b 100644 --- a/src/systems/index_cache.jl +++ b/src/systems/index_cache.jl @@ -126,7 +126,7 @@ function IndexCache(sys::AbstractSystem) for affect in affs if affect isa Equation is_parameter(sys, affect.lhs) && push!(discs, affect.lhs) - elseif affect isa FunctionalAffect + elseif affect isa FunctionalAffect || affect isa MutatingFunctionalAffect union!(discs, unwrap.(discretes(affect))) else error("Unhandled affect type $(typeof(affect))") From 3eca9b96567008469ee6963745a6a4f2ea64ed50 Mon Sep 17 00:00:00 2001 From: Benjamin Chung Date: Tue, 27 Aug 2024 09:28:17 -0700 Subject: [PATCH 0171/2176] Fix bugs relating to array arguments to callbacks --- src/systems/callbacks.jl | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/systems/callbacks.jl b/src/systems/callbacks.jl index 279d31483e..884d1d87f3 100644 --- a/src/systems/callbacks.jl +++ b/src/systems/callbacks.jl @@ -958,9 +958,10 @@ function invalid_variables(sys, expr) filter(x -> !any(isequal(x), all_symbols(sys)), reduce(vcat, vars(expr); init = [])) end function unassignable_variables(sys, expr) - assignable_syms = vcat(unknowns(sys), parameters(sys)) + assignable_syms = reduce(vcat, Symbolics.scalarize.(vcat(unknowns(sys), parameters(sys))); init=[]) + written = reduce(vcat, Symbolics.scalarize.(vars(expr)); init = []) return filter( - x -> !any(isequal(x), assignable_syms), reduce(vcat, vars(expr); init = [])) + x -> !any(isequal(x), assignable_syms), written) end function compile_user_affect(affect::MutatingFunctionalAffect, cb, sys, dvs, ps; kwargs...) @@ -1000,7 +1001,10 @@ function compile_user_affect(affect::MutatingFunctionalAffect, cb, sys, dvs, ps; mod_exprs = modified(affect) for mexpr in mod_exprs - if !is_observed(sys, mexpr) && parameter_index(sys, mexpr) === nothing && !affect.skip_checks + if affect.skip_checks + continue + end + if !is_variable(sys, mexpr) && parameter_index(sys, mexpr) === nothing && !affect.skip_checks @warn ("Expression $mexpr cannot be assigned to; currently only unknowns and parameters may be updated by an affect.") end invalid_vars = unassignable_variables(sys, mexpr) @@ -1036,7 +1040,7 @@ function compile_user_affect(affect::MutatingFunctionalAffect, cb, sys, dvs, ps; mod_param_pairs = filter(v -> is_parameter(sys, v[1]), mod_pairs) mod_unk_pairs = filter(v -> !is_parameter(sys, v[1]), mod_pairs) _, mod_og_val_fun = build_explicit_observed_function( - sys, reduce(vcat, [first.(mod_param_pairs); first.(mod_unk_pairs)]; init = []); + sys, reduce(vcat, Symbolics.scalarize.([first.(mod_param_pairs); first.(mod_unk_pairs)]); init = []); return_inplace = true) upd_params_fun = setu( sys, reduce(vcat, Symbolics.scalarize.(first.(mod_param_pairs)); init = [])) From c41e7d4677077f69024c0f4f0a8e9ef6f83b2d38 Mon Sep 17 00:00:00 2001 From: Benjamin Chung Date: Tue, 27 Aug 2024 18:27:22 -0700 Subject: [PATCH 0172/2176] Remove debug logging --- src/systems/callbacks.jl | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/systems/callbacks.jl b/src/systems/callbacks.jl index 884d1d87f3..a44b5093c8 100644 --- a/src/systems/callbacks.jl +++ b/src/systems/callbacks.jl @@ -861,8 +861,6 @@ function compile_affect_fn(cb, sys::AbstractODESystem, dvs, ps, kwargs) if isnothing(aff) return nothing else - affspr = compile_affect(aff, cb, sys, dvs, ps; expression = Val{true}, kwargs...) - @show affspr return compile_affect(aff, cb, sys, dvs, ps; expression = Val{false}, kwargs...) end end From 95fa1ee554c0cf449c3c2ac9abdcff85546c80d4 Mon Sep 17 00:00:00 2001 From: Benjamin Chung Date: Mon, 9 Sep 2024 20:06:45 -0700 Subject: [PATCH 0173/2176] Fix the namespace operation used while namespacing MutatingFunctionalAffects --- src/systems/callbacks.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/systems/callbacks.jl b/src/systems/callbacks.jl index a44b5093c8..a7834d918e 100644 --- a/src/systems/callbacks.jl +++ b/src/systems/callbacks.jl @@ -161,7 +161,7 @@ end function namespace_affect(affect::MutatingFunctionalAffect, s) MutatingFunctionalAffect(func(affect), - renamespace.((s,), observed(affect)), + namespace_expr.(observed(affect), (s,)), observed_syms(affect), renamespace.((s,), modified(affect)), modified_syms(affect), From f57215ab42f5c4c3747744827b8793403c2e6761 Mon Sep 17 00:00:00 2001 From: Benjamin Chung Date: Tue, 10 Sep 2024 12:45:14 -0700 Subject: [PATCH 0174/2176] Add support for the initializealg argument in SciMLBase callbacks --- src/systems/callbacks.jl | 41 ++++++++++++++++++++++++++++++---------- test/symbolic_events.jl | 6 +++--- 2 files changed, 34 insertions(+), 13 deletions(-) diff --git a/src/systems/callbacks.jl b/src/systems/callbacks.jl index a7834d918e..d2657d2ced 100644 --- a/src/systems/callbacks.jl +++ b/src/systems/callbacks.jl @@ -216,6 +216,11 @@ Affects (i.e. `affect` and `affect_neg`) can be specified as either: + `modified_parameters` is a vector of the parameters that are *modified* by `f!`. Note that a parameter will not appear in `p` if it only appears in `modified_parameters`; it must appear in both `parameters` and `modified_parameters` if it is used in the affect definition. + `ctx` is a user-defined context object passed to `f!` when invoked. This value is aliased for each problem. * A [`MutatingFunctionalAffect`](@ref); refer to its documentation for details. + +Callbacks that impact a DAE are applied, then the DAE is reinitialized using `reinitializealg` (which defaults to `SciMLBase.CheckInit`). +This reinitialization algorithm ensures that the DAE is satisfied after the callback runs. The default value of `CheckInit` will simply validate +that the newly-assigned values indeed satisfy the algebraic system; see the documentation on DAE initialization for a more detailed discussion of +initialization. """ struct SymbolicContinuousCallback eqs::Vector{Equation} @@ -224,14 +229,16 @@ struct SymbolicContinuousCallback affect::Union{Vector{Equation}, FunctionalAffect, MutatingFunctionalAffect} affect_neg::Union{Vector{Equation}, FunctionalAffect, MutatingFunctionalAffect, Nothing} rootfind::SciMLBase.RootfindOpt + reinitializealg::SciMLBase.DAEInitializationAlgorithm function SymbolicContinuousCallback(; eqs::Vector{Equation}, affect = NULL_AFFECT, affect_neg = affect, rootfind = SciMLBase.LeftRootFind, initialize=NULL_AFFECT, - finalize=NULL_AFFECT) - new(eqs, initialize, finalize, make_affect(affect), make_affect(affect_neg), rootfind) + finalize=NULL_AFFECT, + reinitializealg=SciMLBase.CheckInit()) + new(eqs, initialize, finalize, make_affect(affect), make_affect(affect_neg), rootfind, reinitializealg) end # Default affect to nothing end make_affect(affect) = affect @@ -373,6 +380,10 @@ function finalize_affects(cbs::Vector{SymbolicContinuousCallback}) mapreduce(finalize_affects, vcat, cbs, init = Equation[]) end +reinitialization_alg(cb::SymbolicContinuousCallback) = cb.reinitializealg +reinitialization_algs(cbs::Vector{SymbolicContinuousCallback}) = + mapreduce(reinitialization_alg, vcat, cbs, init = SciMLBase.DAEInitializationAlgorithm[]) + namespace_affects(af::Vector, s) = Equation[namespace_affect(a, s) for a in af] namespace_affects(af::FunctionalAffect, s) = namespace_affect(af, s) namespace_affects(af::MutatingFunctionalAffect, s) = namespace_affect(af, s) @@ -419,11 +430,12 @@ struct SymbolicDiscreteCallback # TODO: Iterative condition::Any affects::Any + reinitializealg::SciMLBase.DAEInitializationAlgorithm - function SymbolicDiscreteCallback(condition, affects = NULL_AFFECT) + function SymbolicDiscreteCallback(condition, affects = NULL_AFFECT, reinitializealg=SciMLBase.CheckInit()) c = scalarize_condition(condition) a = scalarize_affects(affects) - new(c, a) + new(c, a, reinitializealg) end # Default affect to nothing end @@ -481,6 +493,10 @@ function affects(cbs::Vector{SymbolicDiscreteCallback}) reduce(vcat, affects(cb) for cb in cbs; init = []) end +reinitialization_alg(cb::SymbolicDiscreteCallback) = cb.reinitializealg +reinitialization_algs(cbs::Vector{SymbolicDiscreteCallback}) = + mapreduce(reinitialization_alg, vcat, cbs, init = SciMLBase.DAEInitializationAlgorithm[]) + function namespace_callback(cb::SymbolicDiscreteCallback, s)::SymbolicDiscreteCallback af = affects(cb) af = af isa AbstractVector ? namespace_affect.(af, Ref(s)) : namespace_affect(af, s) @@ -776,12 +792,13 @@ function generate_single_rootfinding_callback( return ContinuousCallback( cond, affect_function.affect, affect_function.affect_neg, rootfind = cb.rootfind, initialize = isnothing(affect_function.initialize) ? SciMLBase.INITIALIZE_DEFAULT : (c, u, t, i) -> affect_function.initialize(i), - finalize = isnothing(affect_function.finalize) ? SciMLBase.FINALIZE_DEFAULT : (c, u, t, i) -> affect_function.finalize(i)) + finalize = isnothing(affect_function.finalize) ? SciMLBase.FINALIZE_DEFAULT : (c, u, t, i) -> affect_function.finalize(i), + initializealg = reinitialization_alg(cb)) end function generate_vector_rootfinding_callback( cbs, sys::AbstractODESystem, dvs = unknowns(sys), - ps = parameters(sys); rootfind = SciMLBase.RightRootFind, kwargs...) + ps = parameters(sys); rootfind = SciMLBase.RightRootFind, reinitialization = SciMLBase.CheckInit(), kwargs...) eqs = map(cb -> flatten_equations(cb.eqs), cbs) num_eqs = length.(eqs) # fuse equations to create VectorContinuousCallback @@ -847,7 +864,7 @@ function generate_vector_rootfinding_callback( initialize = handle_optional_setup_fn(map(fn -> fn.initialize, affect_functions), SciMLBase.INITIALIZE_DEFAULT) finalize = handle_optional_setup_fn(map(fn -> fn.finalize, affect_functions), SciMLBase.FINALIZE_DEFAULT) return VectorContinuousCallback( - cond, affect, affect_neg, length(eqs), rootfind = rootfind, initialize = initialize, finalize = finalize) + cond, affect, affect_neg, length(eqs), rootfind = rootfind, initialize = initialize, finalize = finalize, initializealg = reinitialization) end """ @@ -893,10 +910,14 @@ function generate_rootfinding_callback(cbs, sys::AbstractODESystem, dvs = unknow # group the cbs by what rootfind op they use # groupby would be very useful here, but alas cb_classes = Dict{ - @NamedTuple{rootfind::SciMLBase.RootfindOpt}, Vector{SymbolicContinuousCallback}}() + @NamedTuple{ + rootfind::SciMLBase.RootfindOpt, + reinitialization::SciMLBase.DAEInitializationAlgorithm}, Vector{SymbolicContinuousCallback}}() for cb in cbs push!( - get!(() -> SymbolicContinuousCallback[], cb_classes, (rootfind = cb.rootfind,)), + get!(() -> SymbolicContinuousCallback[], cb_classes, ( + rootfind = cb.rootfind, + reinitialization = reinitialization_alg(cb))), cb) end @@ -904,7 +925,7 @@ function generate_rootfinding_callback(cbs, sys::AbstractODESystem, dvs = unknow compiled_callbacks = map(collect(pairs(sort!( OrderedDict(cb_classes); by = p -> p.rootfind)))) do (equiv_class, cbs_in_class) return generate_vector_rootfinding_callback( - cbs_in_class, sys, dvs, ps; rootfind = equiv_class.rootfind, kwargs...) + cbs_in_class, sys, dvs, ps; rootfind = equiv_class.rootfind, reinitialization=equiv_class.reinitialization, kwargs...) end if length(compiled_callbacks) == 1 return compiled_callbacks[] diff --git a/test/symbolic_events.jl b/test/symbolic_events.jl index b0f230d75e..dd60b99003 100644 --- a/test/symbolic_events.jl +++ b/test/symbolic_events.jl @@ -996,8 +996,8 @@ end @test sort(canonicalize(Discrete(), prob.p)[1]) == [0.0, 1.0, 2.0] sol = solve(prob, Tsit5()) - @test sol[a] == [1.0, -1.0] - @test sol[b] == [2.0, 5.0, 5.0] + @test sol[a] == [-1.0] + @test sol[b] == [5.0, 5.0] @test sol[c] == [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0] end @testset "Heater" begin @@ -1198,5 +1198,5 @@ end ss = structural_simplify(sys) prob = ODEProblem(ss, [theta => 0.0], (0.0, pi)) sol = solve(prob, Tsit5(); dtmax = 0.01) - @test sol[cnt] == 197 # we get 2 pulses per phase cycle (cos 0 crossing) and we go to 100 cycles; we miss a few due to the initial state + @test getp(sol, cnt)(sol) == 197 # we get 2 pulses per phase cycle (cos 0 crossing) and we go to 100 cycles; we miss a few due to the initial state end From d79d49de426ece41ab43df618c7960d88611b6c5 Mon Sep 17 00:00:00 2001 From: Benjamin Chung Date: Tue, 10 Sep 2024 16:23:58 -0700 Subject: [PATCH 0175/2176] Switch MutatingFunctionalAffect from using ComponentArrays to using NamedTuples for heterotyped operation support. --- src/systems/callbacks.jl | 78 +++++++++++++++--------------- src/systems/diffeqs/odesystem.jl | 10 ++-- test/symbolic_events.jl | 81 ++++++++++++++++++++------------ 3 files changed, 94 insertions(+), 75 deletions(-) diff --git a/src/systems/callbacks.jl b/src/systems/callbacks.jl index d2657d2ced..34acb5b2ae 100644 --- a/src/systems/callbacks.jl +++ b/src/systems/callbacks.jl @@ -77,25 +77,29 @@ end `MutatingFunctionalAffect` is a helper for writing affect functions that will compute observed values and ensure that modified values are correctly written back into the system. The affect function `f` needs to have one of four signatures: -* `f(modified::ComponentArray)` if the function only writes values (unknowns or parameters) to the system, -* `f(modified::ComponentArray, observed::ComponentArray)` if the function also reads observed values from the system, -* `f(modified::ComponentArray, observed::ComponentArray, ctx)` if the function needs the user-defined context, -* `f(modified::ComponentArray, observed::ComponentArray, ctx, integrator)` if the function needs the low-level integrator. +* `f(modified::NamedTuple)::NamedTuple` if the function only writes values (unknowns or parameters) to the system, +* `f(modified::NamedTuple, observed::NamedTuple)::NamedTuple` if the function also reads observed values from the system, +* `f(modified::NamedTuple, observed::NamedTuple, ctx)::NamedTuple` if the function needs the user-defined context, +* `f(modified::NamedTuple, observed::NamedTuple, ctx, integrator)::NamedTuple` if the function needs the low-level integrator. These will be checked in reverse order (that is, the four-argument version first, than the 3, etc). -The function `f` will be called with `observed` and `modified` `ComponentArray`s that are derived from their respective `NamedTuple` definitions. -Each `NamedTuple` should map an expression to a symbol; for example if we pass `observed=(; x = a + b)` this will alias the result of executing `a+b` in the system as `x` +The function `f` will be called with `observed` and `modified` `NamedTuple`s that are derived from their respective `NamedTuple` definitions. +Each declaration`NamedTuple` should map an expression to a symbol; for example if we pass `observed=(; x = a + b)` this will alias the result of executing `a+b` in the system as `x` so the value of `a + b` will be accessible as `observed.x` in `f`. `modified` currently restricts symbolic expressions to only bare variables, so only tuples of the form `(; x = y)` or `(; x)` (which aliases `x` as itself) are allowed. -Both `observed` and `modified` will be automatically populated with the current values of their corresponding expressions on function entry. -The values in `modified` will be written back to the system after `f` returns. For example, if we want to update the value of `x` to be the result of `x + y` we could write +The argument NamedTuples (for instance `(;x=y)`) will be populated with the declared values on function entry; if we require `(;x=y)` in `observed` and `y=2`, for example, +then the NamedTuple `(;x=2)` will be passed as `observed` to the affect function `f`. + +The NamedTuple returned from `f` includes the values to be written back to the system after `f` returns. For example, if we want to update the value of `x` to be the result of `x + y` we could write MutatingFunctionalAffect(observed=(; x_plus_y = x + y), modified=(; x)) do m, o - m.x = o.x_plus_y + @set! m.x = o.x_plus_y end -The affect function updates the value at `x` in `modified` to be the result of evaluating `x + y` as passed in the observed values. +Where we use Setfield to copy the tuple `m` with a new value for `x`, then return the modified value of `m`. All values updated by the tuple must have names originally declared in +`modified`; a runtime error will be produced if a value is written that does not appear in `modified`. The user can dynamically decide not to write a value back by not including it +in the returned tuple, in which case the associated field will not be updated. """ @kwdef struct MutatingFunctionalAffect f::Any @@ -983,6 +987,18 @@ function unassignable_variables(sys, expr) x -> !any(isequal(x), assignable_syms), written) end +@generated function _generated_writeback(integ, setters::NamedTuple{NS1,<:Tuple}, values::NamedTuple{NS2, <:Tuple}) where {NS1, NS2} + setter_exprs = [] + for name in NS2 + if !(name in NS1) + missing_name = "Tried to write back to $name from affect; only declared states ($NS1) may be written to." + error(missing_name) + end + push!(setter_exprs, :(setters.$name(integ, values.$name))) + end + return :(begin $(setter_exprs...) end) +end + function compile_user_affect(affect::MutatingFunctionalAffect, cb, sys, dvs, ps; kwargs...) #= Implementation sketch: @@ -1016,7 +1032,6 @@ function compile_user_affect(affect::MutatingFunctionalAffect, cb, sys, dvs, ps; end obs_syms = observed_syms(affect) obs_syms, obs_exprs = check_dups(obs_syms, obs_exprs) - obs_size = size.(obs_exprs) # we will generate a work buffer of a ComponentArray that maps obs_syms to arrays of size obs_size mod_exprs = modified(affect) for mexpr in mod_exprs @@ -1033,8 +1048,6 @@ function compile_user_affect(affect::MutatingFunctionalAffect, cb, sys, dvs, ps; end mod_syms = modified_syms(affect) mod_syms, mod_exprs = check_dups(mod_syms, mod_exprs) - _, mod_og_val_fun = build_explicit_observed_function( - sys, mod_exprs; return_inplace = true) overlapping_syms = intersect(mod_syms, obs_syms) if length(overlapping_syms) > 0 && !affect.skip_checks @@ -1048,31 +1061,20 @@ function compile_user_affect(affect::MutatingFunctionalAffect, cb, sys, dvs, ps; else zeros(sz) end - _, obs_fun = build_explicit_observed_function( + obs_fun = build_explicit_observed_function( sys, reduce(vcat, Symbolics.scalarize.(obs_exprs); init = []); - return_inplace = true) - obs_component_array = ComponentArrays.ComponentArray(NamedTuple{(obs_syms...,)}(mkzero.(obs_size))) + array_type = :tuple) + obs_sym_tuple = (obs_syms...,) # okay so now to generate the stuff to assign it back into the system - # note that we reorder the componentarray to make the views coherent wrt the base array mod_pairs = mod_exprs .=> mod_syms - mod_param_pairs = filter(v -> is_parameter(sys, v[1]), mod_pairs) - mod_unk_pairs = filter(v -> !is_parameter(sys, v[1]), mod_pairs) - _, mod_og_val_fun = build_explicit_observed_function( - sys, reduce(vcat, Symbolics.scalarize.([first.(mod_param_pairs); first.(mod_unk_pairs)]); init = []); - return_inplace = true) - upd_params_fun = setu( - sys, reduce(vcat, Symbolics.scalarize.(first.(mod_param_pairs)); init = [])) - upd_unk_fun = setu( - sys, reduce(vcat, Symbolics.scalarize.(first.(mod_unk_pairs)); init = [])) - - upd_component_array = ComponentArrays.ComponentArray(NamedTuple{([last.(mod_param_pairs); - last.(mod_unk_pairs)]...,)}( - [collect(mkzero(size(e)) for e in first.(mod_param_pairs)); - collect(mkzero(size(e)) for e in first.(mod_unk_pairs))])) - upd_params_view = view(upd_component_array, last.(mod_param_pairs)) - upd_unks_view = view(upd_component_array, last.(mod_unk_pairs)) + mod_names = (mod_syms..., ) + mod_og_val_fun = build_explicit_observed_function( + sys, reduce(vcat, Symbolics.scalarize.(first.(mod_pairs)); init = []); + array_type = :tuple) + upd_funs = NamedTuple{mod_names}((setu.((sys,), first.(mod_pairs))...,)) + if has_index_cache(sys) && (ic = get_index_cache(sys)) !== nothing save_idxs = get(ic.callback_to_clocks, cb, Int[]) else @@ -1082,13 +1084,13 @@ function compile_user_affect(affect::MutatingFunctionalAffect, cb, sys, dvs, ps; let user_affect = func(affect), ctx = context(affect) function (integ) # update the to-be-mutated values; this ensures that if you do a no-op then nothing happens - mod_og_val_fun(upd_component_array, integ.u, integ.p..., integ.t) + upd_component_array = NamedTuple{mod_names}(mod_og_val_fun(integ.u, integ.p..., integ.t)) # update the observed values - obs_fun(obs_component_array, integ.u, integ.p..., integ.t) + obs_component_array = NamedTuple{obs_sym_tuple}(obs_fun(integ.u, integ.p..., integ.t)) # let the user do their thing - if applicable(user_affect, upd_component_array, obs_component_array, ctx, integ) + modvals = if applicable(user_affect, upd_component_array, obs_component_array, ctx, integ) user_affect(upd_component_array, obs_component_array, ctx, integ) elseif applicable(user_affect, upd_component_array, obs_component_array, ctx) user_affect(upd_component_array, obs_component_array, ctx) @@ -1102,9 +1104,7 @@ function compile_user_affect(affect::MutatingFunctionalAffect, cb, sys, dvs, ps; end # write the new values back to the integrator - upd_params_fun(integ, upd_params_view) - upd_unk_fun(integ, upd_unks_view) - + _generated_writeback(integ, upd_funs, modvals) for idx in save_idxs SciMLBase.save_discretes!(integ, idx) diff --git a/src/systems/diffeqs/odesystem.jl b/src/systems/diffeqs/odesystem.jl index daa4321ed0..e99911eec6 100644 --- a/src/systems/diffeqs/odesystem.jl +++ b/src/systems/diffeqs/odesystem.jl @@ -429,6 +429,7 @@ Options not otherwise specified are: * `op = Operator` sets the recursion terminator for the walk done by `vars` to identify the variables that appear in `ts`. See the documentation for `vars` for more detail. * `throw = true` if true, throw an error when generating a function for `ts` that reference variables that do not exist * `drop_expr` is deprecated. +* `array_type`; only used if the output is an array (that is, `!isscalar(ts)`). If `:array`, then it will generate an array, if `:tuple` then it will generate a tuple. """ function build_explicit_observed_function(sys, ts; inputs = nothing, @@ -442,7 +443,8 @@ function build_explicit_observed_function(sys, ts; return_inplace = false, param_only = false, op = Operator, - throw = true) + throw = true, + array_type=:array) if (isscalar = symbolic_type(ts) !== NotSymbolic()) ts = [ts] end @@ -587,12 +589,10 @@ function build_explicit_observed_function(sys, ts; oop_mtkp_wrapper = mtkparams_wrapper end + output_expr = isscalar ? ts[1] : (array_type == :array ? MakeArray(ts, output_type) : MakeTuple(ts)) # Need to keep old method of building the function since it uses `output_type`, # which can't be provided to `build_function` - oop_fn = Func(args, [], - pre(Let(obsexprs, - isscalar ? ts[1] : MakeArray(ts, output_type), - false))) |> array_wrapper[1] |> oop_mtkp_wrapper |> toexpr + oop_fn = Func(args, [], pre(Let(obsexprs, output_expr, false))) |> array_wrapper[1] |> oop_mtkp_wrapper |> toexpr oop_fn = expression ? oop_fn : eval_or_rgf(oop_fn; eval_expression, eval_module) if !isscalar diff --git a/test/symbolic_events.jl b/test/symbolic_events.jl index dd60b99003..bc455ec06e 100644 --- a/test/symbolic_events.jl +++ b/test/symbolic_events.jl @@ -8,6 +8,7 @@ using ModelingToolkit: SymbolicContinuousCallback, using StableRNGs import SciMLBase using SymbolicIndexingInterface +using Setfield rng = StableRNG(12345) @variables x(t) = 0 @@ -1010,12 +1011,12 @@ end furnace_off = ModelingToolkit.SymbolicContinuousCallback( [temp ~ furnace_off_threshold], ModelingToolkit.MutatingFunctionalAffect(modified = (; furnace_on)) do x, o, i, c - x.furnace_on = false + @set! x.furnace_on = false end) furnace_enable = ModelingToolkit.SymbolicContinuousCallback( [temp ~ furnace_on_threshold], ModelingToolkit.MutatingFunctionalAffect(modified = (; furnace_on)) do x, o, i, c - x.furnace_on = true + @set! x.furnace_on = true end) @named sys = ODESystem( eqs, t, [temp], params; continuous_events = [furnace_off, furnace_enable]) @@ -1027,12 +1028,12 @@ end furnace_off = ModelingToolkit.SymbolicContinuousCallback( [temp ~ furnace_off_threshold], ModelingToolkit.MutatingFunctionalAffect(modified = (; furnace_on)) do x, o, i - x.furnace_on = false + @set! x.furnace_on = false end) furnace_enable = ModelingToolkit.SymbolicContinuousCallback( [temp ~ furnace_on_threshold], ModelingToolkit.MutatingFunctionalAffect(modified = (; furnace_on)) do x, o, i - x.furnace_on = true + @set! x.furnace_on = true end) @named sys = ODESystem( eqs, t, [temp], params; continuous_events = [furnace_off, furnace_enable]) @@ -1044,12 +1045,12 @@ end furnace_off = ModelingToolkit.SymbolicContinuousCallback( [temp ~ furnace_off_threshold], ModelingToolkit.MutatingFunctionalAffect(modified = (; furnace_on)) do x, o - x.furnace_on = false + @set! x.furnace_on = false end) furnace_enable = ModelingToolkit.SymbolicContinuousCallback( [temp ~ furnace_on_threshold], ModelingToolkit.MutatingFunctionalAffect(modified = (; furnace_on)) do x, o - x.furnace_on = true + @set! x.furnace_on = true end) @named sys = ODESystem( eqs, t, [temp], params; continuous_events = [furnace_off, furnace_enable]) @@ -1061,12 +1062,12 @@ end furnace_off = ModelingToolkit.SymbolicContinuousCallback( [temp ~ furnace_off_threshold], ModelingToolkit.MutatingFunctionalAffect(modified = (; furnace_on)) do x - x.furnace_on = false + @set! x.furnace_on = false end) furnace_enable = ModelingToolkit.SymbolicContinuousCallback( [temp ~ furnace_on_threshold], ModelingToolkit.MutatingFunctionalAffect(modified = (; furnace_on)) do x - x.furnace_on = true + @set! x.furnace_on = true end) @named sys = ODESystem( eqs, t, [temp], params; continuous_events = [furnace_off, furnace_enable]) @@ -1078,14 +1079,14 @@ end furnace_off = ModelingToolkit.SymbolicContinuousCallback( [temp ~ furnace_off_threshold], ModelingToolkit.MutatingFunctionalAffect(modified = (; furnace_on)) do x - x.furnace_on = false + @set! x.furnace_on = false end; initialize = ModelingToolkit.MutatingFunctionalAffect(modified = (; temp)) do x - x.temp = 0.2 + @set! x.temp = 0.2 end) furnace_enable = ModelingToolkit.SymbolicContinuousCallback( [temp ~ furnace_on_threshold], ModelingToolkit.MutatingFunctionalAffect(modified = (; furnace_on)) do x, o, c, i - x.furnace_on = true + @set! x.furnace_on = true end) @named sys = ODESystem( eqs, t, [temp], params; continuous_events = [furnace_off, furnace_enable]) @@ -1107,7 +1108,7 @@ end [temp ~ furnace_off_threshold], ModelingToolkit.MutatingFunctionalAffect( modified = (; furnace_on), observed = (; furnace_on)) do x, o, c, i - x.furnace_on = false + @set! x.furnace_on = false end) @named sys = ODESystem(eqs, t, [temp], params; continuous_events = [furnace_off]) ss = structural_simplify(sys) @@ -1123,7 +1124,7 @@ end [temp ~ furnace_off_threshold], ModelingToolkit.MutatingFunctionalAffect( modified = (; furnace_on, tempsq), observed = (; furnace_on)) do x, o, c, i - x.furnace_on = false + @set! x.furnace_on = false end) @named sys = ODESystem( eqs, t, [temp, tempsq], params; continuous_events = [furnace_off]) @@ -1136,18 +1137,32 @@ end [temp ~ furnace_off_threshold], ModelingToolkit.MutatingFunctionalAffect(modified = (; furnace_on), observed = (; furnace_on, not_actually_here)) do x, o, c, i - x.furnace_on = false + @set! x.furnace_on = false end) @named sys = ODESystem( eqs, t, [temp, tempsq], params; continuous_events = [furnace_off]) ss = structural_simplify(sys) @test_throws "refers to missing variable(s)" prob=ODEProblem( ss, [temp => 0.0, furnace_on => true], (0.0, 100.0)) + + + furnace_off = ModelingToolkit.SymbolicContinuousCallback( + [temp ~ furnace_off_threshold], + ModelingToolkit.MutatingFunctionalAffect(modified = (; furnace_on), + observed = (; furnace_on)) do x, o, c, i + return (;fictional2 = false) + end) + @named sys = ODESystem( + eqs, t, [temp, tempsq], params; continuous_events = [furnace_off]) + ss = structural_simplify(sys) + prob=ODEProblem( + ss, [temp => 0.0, furnace_on => true], (0.0, 100.0)) + @test_throws "Tried to write back to" solve(prob, Tsit5()) end @testset "Quadrature" begin @variables theta(t) omega(t) - params = @parameters qA=0 qB=0 hA=0 hB=0 cnt=0 + params = @parameters qA=0 qB=0 hA=0 hB=0 cnt::Int=0 eqs = [D(theta) ~ omega omega ~ 1.0] function decoder(oldA, oldB, newA, newB) @@ -1167,31 +1182,35 @@ end end qAevt = ModelingToolkit.SymbolicContinuousCallback([cos(100 * theta) ~ 0], ModelingToolkit.MutatingFunctionalAffect((; qA, hA, hB, cnt), (; qB)) do x, o, i, c - x.hA = x.qA - x.hB = o.qB - x.qA = 1 - x.cnt += decoder(x.hA, x.hB, x.qA, o.qB) + @set! x.hA = x.qA + @set! x.hB = o.qB + @set! x.qA = 1 + @set! x.cnt += decoder(x.hA, x.hB, x.qA, o.qB) + x end, affect_neg = ModelingToolkit.MutatingFunctionalAffect( (; qA, hA, hB, cnt), (; qB)) do x, o, c, i - x.hA = x.qA - x.hB = o.qB - x.qA = 0 - x.cnt += decoder(x.hA, x.hB, x.qA, o.qB) + @set! x.hA = x.qA + @set! x.hB = o.qB + @set! x.qA = 0 + @set! x.cnt += decoder(x.hA, x.hB, x.qA, o.qB) + x end; rootfind = SciMLBase.RightRootFind) qBevt = ModelingToolkit.SymbolicContinuousCallback([cos(100 * theta - π / 2) ~ 0], ModelingToolkit.MutatingFunctionalAffect((; qB, hA, hB, cnt), (; qA)) do x, o, i, c - x.hA = o.qA - x.hB = x.qB - x.qB = 1 - x.cnt += decoder(x.hA, x.hB, o.qA, x.qB) + @set! x.hA = o.qA + @set! x.hB = x.qB + @set! x.qB = 1 + @set! x.cnt += decoder(x.hA, x.hB, o.qA, x.qB) + x end, affect_neg = ModelingToolkit.MutatingFunctionalAffect( (; qB, hA, hB, cnt), (; qA)) do x, o, c, i - x.hA = o.qA - x.hB = x.qB - x.qB = 0 - x.cnt += decoder(x.hA, x.hB, o.qA, x.qB) + @set! x.hA = o.qA + @set! x.hB = x.qB + @set! x.qB = 0 + @set! x.cnt += decoder(x.hA, x.hB, o.qA, x.qB) + x end; rootfind = SciMLBase.RightRootFind) @named sys = ODESystem( eqs, t, [theta, omega], params; continuous_events = [qAevt, qBevt]) From c940e5ed27eeed1cafdb3d2a408a245b31950c3e Mon Sep 17 00:00:00 2001 From: Benjamin Chung Date: Fri, 13 Sep 2024 09:10:20 -0700 Subject: [PATCH 0176/2176] Fix support for array forms in the NamedTuple version of MutatingFunctionalAffect --- src/systems/callbacks.jl | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/systems/callbacks.jl b/src/systems/callbacks.jl index 34acb5b2ae..ad41807ab8 100644 --- a/src/systems/callbacks.jl +++ b/src/systems/callbacks.jl @@ -1062,7 +1062,7 @@ function compile_user_affect(affect::MutatingFunctionalAffect, cb, sys, dvs, ps; zeros(sz) end obs_fun = build_explicit_observed_function( - sys, reduce(vcat, Symbolics.scalarize.(obs_exprs); init = []); + sys, Symbolics.scalarize.(obs_exprs); array_type = :tuple) obs_sym_tuple = (obs_syms...,) @@ -1070,7 +1070,7 @@ function compile_user_affect(affect::MutatingFunctionalAffect, cb, sys, dvs, ps; mod_pairs = mod_exprs .=> mod_syms mod_names = (mod_syms..., ) mod_og_val_fun = build_explicit_observed_function( - sys, reduce(vcat, Symbolics.scalarize.(first.(mod_pairs)); init = []); + sys, Symbolics.scalarize.(first.(mod_pairs)); array_type = :tuple) upd_funs = NamedTuple{mod_names}((setu.((sys,), first.(mod_pairs))...,)) @@ -1084,7 +1084,8 @@ function compile_user_affect(affect::MutatingFunctionalAffect, cb, sys, dvs, ps; let user_affect = func(affect), ctx = context(affect) function (integ) # update the to-be-mutated values; this ensures that if you do a no-op then nothing happens - upd_component_array = NamedTuple{mod_names}(mod_og_val_fun(integ.u, integ.p..., integ.t)) + modvals = mod_og_val_fun(integ.u, integ.p..., integ.t) + upd_component_array = NamedTuple{mod_names}(modvals) # update the observed values obs_component_array = NamedTuple{obs_sym_tuple}(obs_fun(integ.u, integ.p..., integ.t)) From 2206425cae06f57c2985607c1c18d740bb7d439c Mon Sep 17 00:00:00 2001 From: Benjamin Chung Date: Mon, 23 Sep 2024 08:18:16 -0700 Subject: [PATCH 0177/2176] Remove ComponentArrays dep, cleanup handling of skip_checks --- Project.toml | 1 - src/systems/callbacks.jl | 29 +++++++++++++++-------------- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/Project.toml b/Project.toml index 729966fde0..808d68af06 100644 --- a/Project.toml +++ b/Project.toml @@ -9,7 +9,6 @@ ArrayInterface = "4fba245c-0d91-5ea0-9b3e-6abc04ee57a9" BlockArrays = "8e7c35d0-a365-5155-bbbb-fb81a777f24e" Combinatorics = "861a8166-3701-5b0c-9a16-15d98fcdc6aa" Compat = "34da2185-b29b-5c13-b0c7-acf172513d20" -ComponentArrays = "b0b7db55-cfe3-40fc-9ded-d10e2dbeff66" ConstructionBase = "187b0558-2788-49d3-abe0-74a17ed4e7c9" DataStructures = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8" DiffEqBase = "2b5f629d-d688-5b77-993f-72d75c75574e" diff --git a/src/systems/callbacks.jl b/src/systems/callbacks.jl index ad41807ab8..cf12b9078b 100644 --- a/src/systems/callbacks.jl +++ b/src/systems/callbacks.jl @@ -1024,26 +1024,27 @@ function compile_user_affect(affect::MutatingFunctionalAffect, cb, sys, dvs, ps; end obs_exprs = observed(affect) - for oexpr in obs_exprs - invalid_vars = invalid_variables(sys, oexpr) - if length(invalid_vars) > 0 && !affect.skip_checks - error("Observed equation $(oexpr) in affect refers to missing variable(s) $(invalid_vars); the variables may not have been added (e.g. if a component is missing).") + if !affect.skip_checks + for oexpr in obs_exprs + invalid_vars = invalid_variables(sys, oexpr) + if length(invalid_vars) > 0 + error("Observed equation $(oexpr) in affect refers to missing variable(s) $(invalid_vars); the variables may not have been added (e.g. if a component is missing).") + end end end obs_syms = observed_syms(affect) obs_syms, obs_exprs = check_dups(obs_syms, obs_exprs) mod_exprs = modified(affect) - for mexpr in mod_exprs - if affect.skip_checks - continue - end - if !is_variable(sys, mexpr) && parameter_index(sys, mexpr) === nothing && !affect.skip_checks - @warn ("Expression $mexpr cannot be assigned to; currently only unknowns and parameters may be updated by an affect.") - end - invalid_vars = unassignable_variables(sys, mexpr) - if length(invalid_vars) > 0 && !affect.skip_checks - error("Modified equation $(mexpr) in affect refers to missing variable(s) $(invalid_vars); the variables may not have been added (e.g. if a component is missing) or they may have been reduced away.") + if !affect.skip_checks + for mexpr in mod_exprs + if !is_variable(sys, mexpr) && parameter_index(sys, mexpr) === nothing + @warn ("Expression $mexpr cannot be assigned to; currently only unknowns and parameters may be updated by an affect.") + end + invalid_vars = unassignable_variables(sys, mexpr) + if length(invalid_vars) > 0 + error("Modified equation $(mexpr) in affect refers to missing variable(s) $(invalid_vars); the variables may not have been added (e.g. if a component is missing) or they may have been reduced away.") + end end end mod_syms = modified_syms(affect) From 98dcd4ee1ff90a572c273977f780c4cd23f4ce0d Mon Sep 17 00:00:00 2001 From: Benjamin Chung Date: Mon, 23 Sep 2024 09:59:43 -0700 Subject: [PATCH 0178/2176] Improve detection of writeback values --- src/systems/callbacks.jl | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/systems/callbacks.jl b/src/systems/callbacks.jl index cf12b9078b..23b7ccac2c 100644 --- a/src/systems/callbacks.jl +++ b/src/systems/callbacks.jl @@ -999,6 +999,18 @@ end return :(begin $(setter_exprs...) end) end +function check_assignable(sys, sym) + if symbolic_type(sym) == ScalarSymbolic() + is_variable(sys, sym) || is_parameter(sys, sym) + elseif symbolic_type(sym) == ArraySymbolic() + is_variable(sys, sym) || is_parameter(sys, sym) || all(x -> check_assignable(sys, x), collect(sym)) + elseif sym isa Union{AbstractArray, Tuple} + all(x -> check_assignable(sys, x), sym) + else + false + end +end + function compile_user_affect(affect::MutatingFunctionalAffect, cb, sys, dvs, ps; kwargs...) #= Implementation sketch: @@ -1038,7 +1050,7 @@ function compile_user_affect(affect::MutatingFunctionalAffect, cb, sys, dvs, ps; mod_exprs = modified(affect) if !affect.skip_checks for mexpr in mod_exprs - if !is_variable(sys, mexpr) && parameter_index(sys, mexpr) === nothing + if !check_assignable(sys, mexpr) @warn ("Expression $mexpr cannot be assigned to; currently only unknowns and parameters may be updated by an affect.") end invalid_vars = unassignable_variables(sys, mexpr) From 3fd4462d31d0c10b77fae3a0c6678d36f9fe0046 Mon Sep 17 00:00:00 2001 From: Benjamin Chung Date: Mon, 23 Sep 2024 10:06:16 -0700 Subject: [PATCH 0179/2176] Remove ComponentArrays dep --- src/ModelingToolkit.jl | 1 - 1 file changed, 1 deletion(-) diff --git a/src/ModelingToolkit.jl b/src/ModelingToolkit.jl index f5262a1526..2f57bb1765 100644 --- a/src/ModelingToolkit.jl +++ b/src/ModelingToolkit.jl @@ -54,7 +54,6 @@ using Reexport using RecursiveArrayTools import Graphs: SimpleDiGraph, add_edge!, incidence_matrix import BlockArrays: BlockedArray, Block, blocksize, blocksizes -import ComponentArrays using RuntimeGeneratedFunctions using RuntimeGeneratedFunctions: drop_expr From e6ce6ab65fe9e62b5c34f8adc546ceafa91f475f Mon Sep 17 00:00:00 2001 From: Benjamin Chung Date: Thu, 17 Oct 2024 12:00:44 -0700 Subject: [PATCH 0180/2176] Rename MutatingFunctionalAffect to ImperativeAffect --- src/systems/callbacks.jl | 74 +++++++++++++++---------------- src/systems/index_cache.jl | 2 +- test/symbolic_events.jl | 90 +++++++++++++++++++------------------- 3 files changed, 83 insertions(+), 83 deletions(-) diff --git a/src/systems/callbacks.jl b/src/systems/callbacks.jl index 23b7ccac2c..73f2a7a6cf 100644 --- a/src/systems/callbacks.jl +++ b/src/systems/callbacks.jl @@ -72,9 +72,9 @@ function namespace_affect(affect::FunctionalAffect, s) end """ - MutatingFunctionalAffect(f::Function; modified::NamedTuple, observed::NamedTuple, ctx) + ImperativeAffect(f::Function; modified::NamedTuple, observed::NamedTuple, ctx) -`MutatingFunctionalAffect` is a helper for writing affect functions that will compute observed values and +`ImperativeAffect` is a helper for writing affect functions that will compute observed values and ensure that modified values are correctly written back into the system. The affect function `f` needs to have one of four signatures: * `f(modified::NamedTuple)::NamedTuple` if the function only writes values (unknowns or parameters) to the system, @@ -93,7 +93,7 @@ then the NamedTuple `(;x=2)` will be passed as `observed` to the affect function The NamedTuple returned from `f` includes the values to be written back to the system after `f` returns. For example, if we want to update the value of `x` to be the result of `x + y` we could write - MutatingFunctionalAffect(observed=(; x_plus_y = x + y), modified=(; x)) do m, o + ImperativeAffect(observed=(; x_plus_y = x + y), modified=(; x)) do m, o @set! m.x = o.x_plus_y end @@ -101,7 +101,7 @@ Where we use Setfield to copy the tuple `m` with a new value for `x`, then retur `modified`; a runtime error will be produced if a value is written that does not appear in `modified`. The user can dynamically decide not to write a value back by not including it in the returned tuple, in which case the associated field will not be updated. """ -@kwdef struct MutatingFunctionalAffect +@kwdef struct ImperativeAffect f::Any obs::Vector obs_syms::Vector{Symbol} @@ -111,50 +111,50 @@ in the returned tuple, in which case the associated field will not be updated. skip_checks::Bool end -function MutatingFunctionalAffect(f::Function; +function ImperativeAffect(f::Function; observed::NamedTuple = NamedTuple{()}(()), modified::NamedTuple = NamedTuple{()}(()), ctx = nothing, skip_checks = false) - MutatingFunctionalAffect(f, + ImperativeAffect(f, collect(values(observed)), collect(keys(observed)), collect(values(modified)), collect(keys(modified)), ctx, skip_checks) end -function MutatingFunctionalAffect(f::Function, modified::NamedTuple; +function ImperativeAffect(f::Function, modified::NamedTuple; observed::NamedTuple = NamedTuple{()}(()), ctx = nothing, skip_checks=false) - MutatingFunctionalAffect(f, observed = observed, modified = modified, ctx = ctx, skip_checks = skip_checks) + ImperativeAffect(f, observed = observed, modified = modified, ctx = ctx, skip_checks = skip_checks) end -function MutatingFunctionalAffect( +function ImperativeAffect( f::Function, modified::NamedTuple, observed::NamedTuple; ctx = nothing, skip_checks=false) - MutatingFunctionalAffect(f, observed = observed, modified = modified, ctx = ctx, skip_checks = skip_checks) + ImperativeAffect(f, observed = observed, modified = modified, ctx = ctx, skip_checks = skip_checks) end -function MutatingFunctionalAffect( +function ImperativeAffect( f::Function, modified::NamedTuple, observed::NamedTuple, ctx; skip_checks=false) - MutatingFunctionalAffect(f, observed = observed, modified = modified, ctx = ctx, skip_checks = skip_checks) + ImperativeAffect(f, observed = observed, modified = modified, ctx = ctx, skip_checks = skip_checks) end -function Base.show(io::IO, mfa::MutatingFunctionalAffect) +function Base.show(io::IO, mfa::ImperativeAffect) obs_vals = join(map((ob,nm) -> "$ob => $nm", mfa.obs, mfa.obs_syms), ", ") mod_vals = join(map((md,nm) -> "$md => $nm", mfa.modified, mfa.mod_syms), ", ") affect = mfa.f - print(io, "MutatingFunctionalAffect(observed: [$obs_vals], modified: [$mod_vals], affect:$affect)") + print(io, "ImperativeAffect(observed: [$obs_vals], modified: [$mod_vals], affect:$affect)") end -func(f::MutatingFunctionalAffect) = f.f -context(a::MutatingFunctionalAffect) = a.ctx -observed(a::MutatingFunctionalAffect) = a.obs -observed_syms(a::MutatingFunctionalAffect) = a.obs_syms -discretes(a::MutatingFunctionalAffect) = filter(ModelingToolkit.isparameter, a.modified) -modified(a::MutatingFunctionalAffect) = a.modified -modified_syms(a::MutatingFunctionalAffect) = a.mod_syms +func(f::ImperativeAffect) = f.f +context(a::ImperativeAffect) = a.ctx +observed(a::ImperativeAffect) = a.obs +observed_syms(a::ImperativeAffect) = a.obs_syms +discretes(a::ImperativeAffect) = filter(ModelingToolkit.isparameter, a.modified) +modified(a::ImperativeAffect) = a.modified +modified_syms(a::ImperativeAffect) = a.mod_syms -function Base.:(==)(a1::MutatingFunctionalAffect, a2::MutatingFunctionalAffect) +function Base.:(==)(a1::ImperativeAffect, a2::ImperativeAffect) isequal(a1.f, a2.f) && isequal(a1.obs, a2.obs) && isequal(a1.modified, a2.modified) && isequal(a1.obs_syms, a2.obs_syms) && isequal(a1.mod_syms, a2.mod_syms) && isequal(a1.ctx, a2.ctx) end -function Base.hash(a::MutatingFunctionalAffect, s::UInt) +function Base.hash(a::ImperativeAffect, s::UInt) s = hash(a.f, s) s = hash(a.obs, s) s = hash(a.obs_syms, s) @@ -163,8 +163,8 @@ function Base.hash(a::MutatingFunctionalAffect, s::UInt) hash(a.ctx, s) end -function namespace_affect(affect::MutatingFunctionalAffect, s) - MutatingFunctionalAffect(func(affect), +function namespace_affect(affect::ImperativeAffect, s) + ImperativeAffect(func(affect), namespace_expr.(observed(affect), (s,)), observed_syms(affect), renamespace.((s,), modified(affect)), @@ -174,7 +174,7 @@ function namespace_affect(affect::MutatingFunctionalAffect, s) end function has_functional_affect(cb) - (affects(cb) isa FunctionalAffect || affects(cb) isa MutatingFunctionalAffect) + (affects(cb) isa FunctionalAffect || affects(cb) isa ImperativeAffect) end #################################### continuous events ##################################### @@ -219,7 +219,7 @@ Affects (i.e. `affect` and `affect_neg`) can be specified as either: + `read_parameters` is a vector of the parameters that are *used* by `f!`. Their indices are passed to `f` in `p` similarly to the indices of `unknowns` passed in `u`. + `modified_parameters` is a vector of the parameters that are *modified* by `f!`. Note that a parameter will not appear in `p` if it only appears in `modified_parameters`; it must appear in both `parameters` and `modified_parameters` if it is used in the affect definition. + `ctx` is a user-defined context object passed to `f!` when invoked. This value is aliased for each problem. -* A [`MutatingFunctionalAffect`](@ref); refer to its documentation for details. +* A [`ImperativeAffect`](@ref); refer to its documentation for details. Callbacks that impact a DAE are applied, then the DAE is reinitialized using `reinitializealg` (which defaults to `SciMLBase.CheckInit`). This reinitialization algorithm ensures that the DAE is satisfied after the callback runs. The default value of `CheckInit` will simply validate @@ -228,10 +228,10 @@ initialization. """ struct SymbolicContinuousCallback eqs::Vector{Equation} - initialize::Union{Vector{Equation}, FunctionalAffect, MutatingFunctionalAffect} - finalize::Union{Vector{Equation}, FunctionalAffect, MutatingFunctionalAffect} - affect::Union{Vector{Equation}, FunctionalAffect, MutatingFunctionalAffect} - affect_neg::Union{Vector{Equation}, FunctionalAffect, MutatingFunctionalAffect, Nothing} + initialize::Union{Vector{Equation}, FunctionalAffect, ImperativeAffect} + finalize::Union{Vector{Equation}, FunctionalAffect, ImperativeAffect} + affect::Union{Vector{Equation}, FunctionalAffect, ImperativeAffect} + affect_neg::Union{Vector{Equation}, FunctionalAffect, ImperativeAffect, Nothing} rootfind::SciMLBase.RootfindOpt reinitializealg::SciMLBase.DAEInitializationAlgorithm function SymbolicContinuousCallback(; @@ -390,7 +390,7 @@ reinitialization_algs(cbs::Vector{SymbolicContinuousCallback}) = namespace_affects(af::Vector, s) = Equation[namespace_affect(a, s) for a in af] namespace_affects(af::FunctionalAffect, s) = namespace_affect(af, s) -namespace_affects(af::MutatingFunctionalAffect, s) = namespace_affect(af, s) +namespace_affects(af::ImperativeAffect, s) = namespace_affect(af, s) namespace_affects(::Nothing, s) = nothing function namespace_callback(cb::SymbolicContinuousCallback, s)::SymbolicContinuousCallback @@ -460,7 +460,7 @@ scalarize_affects(affects) = scalarize(affects) scalarize_affects(affects::Tuple) = FunctionalAffect(affects...) scalarize_affects(affects::NamedTuple) = FunctionalAffect(; affects...) scalarize_affects(affects::FunctionalAffect) = affects -scalarize_affects(affects::MutatingFunctionalAffect) = affects +scalarize_affects(affects::ImperativeAffect) = affects SymbolicDiscreteCallback(p::Pair) = SymbolicDiscreteCallback(p[1], p[2]) SymbolicDiscreteCallback(cb::SymbolicDiscreteCallback) = cb # passthrough @@ -468,7 +468,7 @@ SymbolicDiscreteCallback(cb::SymbolicDiscreteCallback) = cb # passthrough function Base.show(io::IO, db::SymbolicDiscreteCallback) println(io, "condition: ", db.condition) println(io, "affects:") - if db.affects isa FunctionalAffect || db.affects isa MutatingFunctionalAffect + if db.affects isa FunctionalAffect || db.affects isa ImperativeAffect # TODO println(io, " ", db.affects) else @@ -1011,7 +1011,7 @@ function check_assignable(sys, sym) end end -function compile_user_affect(affect::MutatingFunctionalAffect, cb, sys, dvs, ps; kwargs...) +function compile_user_affect(affect::ImperativeAffect, cb, sys, dvs, ps; kwargs...) #= Implementation sketch: generate observed function (oop), should save to a component array under obs_syms @@ -1113,7 +1113,7 @@ function compile_user_affect(affect::MutatingFunctionalAffect, cb, sys, dvs, ps; elseif applicable(user_affect, upd_component_array) user_affect(upd_component_array) else - @error "User affect function $user_affect needs to implement one of the supported MutatingFunctionalAffect callback forms; see the MutatingFunctionalAffect docstring for more details" + @error "User affect function $user_affect needs to implement one of the supported ImperativeAffect callback forms; see the ImperativeAffect docstring for more details" user_affect(upd_component_array, obs_component_array, integ, ctx) # this WILL error but it'll give a more sensible message end @@ -1127,7 +1127,7 @@ function compile_user_affect(affect::MutatingFunctionalAffect, cb, sys, dvs, ps; end end -function compile_affect(affect::Union{FunctionalAffect, MutatingFunctionalAffect}, cb, sys, dvs, ps; kwargs...) +function compile_affect(affect::Union{FunctionalAffect, ImperativeAffect}, cb, sys, dvs, ps; kwargs...) compile_user_affect(affect, cb, sys, dvs, ps; kwargs...) end diff --git a/src/systems/index_cache.jl b/src/systems/index_cache.jl index 55d819990b..bd295055fa 100644 --- a/src/systems/index_cache.jl +++ b/src/systems/index_cache.jl @@ -126,7 +126,7 @@ function IndexCache(sys::AbstractSystem) for affect in affs if affect isa Equation is_parameter(sys, affect.lhs) && push!(discs, affect.lhs) - elseif affect isa FunctionalAffect || affect isa MutatingFunctionalAffect + elseif affect isa FunctionalAffect || affect isa ImperativeAffect union!(discs, unwrap.(discretes(affect))) else error("Unhandled affect type $(typeof(affect))") diff --git a/test/symbolic_events.jl b/test/symbolic_events.jl index bc455ec06e..b301b65650 100644 --- a/test/symbolic_events.jl +++ b/test/symbolic_events.jl @@ -228,10 +228,10 @@ affect_neg = [x ~ 1] @test e[].affect == affect end -@testset "MutatingFunctionalAffect constructors" begin +@testset "ImperativeAffect constructors" begin fmfa(o, x, i, c) = nothing - m = ModelingToolkit.MutatingFunctionalAffect(fmfa) - @test m isa ModelingToolkit.MutatingFunctionalAffect + m = ModelingToolkit.ImperativeAffect(fmfa) + @test m isa ModelingToolkit.ImperativeAffect @test m.f == fmfa @test m.obs == [] @test m.obs_syms == [] @@ -239,8 +239,8 @@ end @test m.mod_syms == [] @test m.ctx === nothing - m = ModelingToolkit.MutatingFunctionalAffect(fmfa, (;)) - @test m isa ModelingToolkit.MutatingFunctionalAffect + m = ModelingToolkit.ImperativeAffect(fmfa, (;)) + @test m isa ModelingToolkit.ImperativeAffect @test m.f == fmfa @test m.obs == [] @test m.obs_syms == [] @@ -248,8 +248,8 @@ end @test m.mod_syms == [] @test m.ctx === nothing - m = ModelingToolkit.MutatingFunctionalAffect(fmfa, (; x)) - @test m isa ModelingToolkit.MutatingFunctionalAffect + m = ModelingToolkit.ImperativeAffect(fmfa, (; x)) + @test m isa ModelingToolkit.ImperativeAffect @test m.f == fmfa @test isequal(m.obs, []) @test m.obs_syms == [] @@ -257,8 +257,8 @@ end @test m.mod_syms == [:x] @test m.ctx === nothing - m = ModelingToolkit.MutatingFunctionalAffect(fmfa, (; y = x)) - @test m isa ModelingToolkit.MutatingFunctionalAffect + m = ModelingToolkit.ImperativeAffect(fmfa, (; y = x)) + @test m isa ModelingToolkit.ImperativeAffect @test m.f == fmfa @test isequal(m.obs, []) @test m.obs_syms == [] @@ -266,8 +266,8 @@ end @test m.mod_syms == [:y] @test m.ctx === nothing - m = ModelingToolkit.MutatingFunctionalAffect(fmfa; observed = (; y = x)) - @test m isa ModelingToolkit.MutatingFunctionalAffect + m = ModelingToolkit.ImperativeAffect(fmfa; observed = (; y = x)) + @test m isa ModelingToolkit.ImperativeAffect @test m.f == fmfa @test isequal(m.obs, [x]) @test m.obs_syms == [:y] @@ -275,8 +275,8 @@ end @test m.mod_syms == [] @test m.ctx === nothing - m = ModelingToolkit.MutatingFunctionalAffect(fmfa; modified = (; x)) - @test m isa ModelingToolkit.MutatingFunctionalAffect + m = ModelingToolkit.ImperativeAffect(fmfa; modified = (; x)) + @test m isa ModelingToolkit.ImperativeAffect @test m.f == fmfa @test isequal(m.obs, []) @test m.obs_syms == [] @@ -284,8 +284,8 @@ end @test m.mod_syms == [:x] @test m.ctx === nothing - m = ModelingToolkit.MutatingFunctionalAffect(fmfa; modified = (; y = x)) - @test m isa ModelingToolkit.MutatingFunctionalAffect + m = ModelingToolkit.ImperativeAffect(fmfa; modified = (; y = x)) + @test m isa ModelingToolkit.ImperativeAffect @test m.f == fmfa @test isequal(m.obs, []) @test m.obs_syms == [] @@ -293,8 +293,8 @@ end @test m.mod_syms == [:y] @test m.ctx === nothing - m = ModelingToolkit.MutatingFunctionalAffect(fmfa, (; x), (; x)) - @test m isa ModelingToolkit.MutatingFunctionalAffect + m = ModelingToolkit.ImperativeAffect(fmfa, (; x), (; x)) + @test m isa ModelingToolkit.ImperativeAffect @test m.f == fmfa @test isequal(m.obs, [x]) @test m.obs_syms == [:x] @@ -302,8 +302,8 @@ end @test m.mod_syms == [:x] @test m.ctx === nothing - m = ModelingToolkit.MutatingFunctionalAffect(fmfa, (; y = x), (; y = x)) - @test m isa ModelingToolkit.MutatingFunctionalAffect + m = ModelingToolkit.ImperativeAffect(fmfa, (; y = x), (; y = x)) + @test m isa ModelingToolkit.ImperativeAffect @test m.f == fmfa @test isequal(m.obs, [x]) @test m.obs_syms == [:y] @@ -311,9 +311,9 @@ end @test m.mod_syms == [:y] @test m.ctx === nothing - m = ModelingToolkit.MutatingFunctionalAffect( + m = ModelingToolkit.ImperativeAffect( fmfa; modified = (; y = x), observed = (; y = x)) - @test m isa ModelingToolkit.MutatingFunctionalAffect + @test m isa ModelingToolkit.ImperativeAffect @test m.f == fmfa @test isequal(m.obs, [x]) @test m.obs_syms == [:y] @@ -321,9 +321,9 @@ end @test m.mod_syms == [:y] @test m.ctx === nothing - m = ModelingToolkit.MutatingFunctionalAffect( + m = ModelingToolkit.ImperativeAffect( fmfa; modified = (; y = x), observed = (; y = x), ctx = 3) - @test m isa ModelingToolkit.MutatingFunctionalAffect + @test m isa ModelingToolkit.ImperativeAffect @test m.f == fmfa @test isequal(m.obs, [x]) @test m.obs_syms == [:y] @@ -331,8 +331,8 @@ end @test m.mod_syms == [:y] @test m.ctx === 3 - m = ModelingToolkit.MutatingFunctionalAffect(fmfa, (; x), (; x), 3) - @test m isa ModelingToolkit.MutatingFunctionalAffect + m = ModelingToolkit.ImperativeAffect(fmfa, (; x), (; x), 3) + @test m isa ModelingToolkit.ImperativeAffect @test m.f == fmfa @test isequal(m.obs, [x]) @test m.obs_syms == [:x] @@ -1010,12 +1010,12 @@ end furnace_off = ModelingToolkit.SymbolicContinuousCallback( [temp ~ furnace_off_threshold], - ModelingToolkit.MutatingFunctionalAffect(modified = (; furnace_on)) do x, o, i, c + ModelingToolkit.ImperativeAffect(modified = (; furnace_on)) do x, o, i, c @set! x.furnace_on = false end) furnace_enable = ModelingToolkit.SymbolicContinuousCallback( [temp ~ furnace_on_threshold], - ModelingToolkit.MutatingFunctionalAffect(modified = (; furnace_on)) do x, o, i, c + ModelingToolkit.ImperativeAffect(modified = (; furnace_on)) do x, o, i, c @set! x.furnace_on = true end) @named sys = ODESystem( @@ -1027,12 +1027,12 @@ end furnace_off = ModelingToolkit.SymbolicContinuousCallback( [temp ~ furnace_off_threshold], - ModelingToolkit.MutatingFunctionalAffect(modified = (; furnace_on)) do x, o, i + ModelingToolkit.ImperativeAffect(modified = (; furnace_on)) do x, o, i @set! x.furnace_on = false end) furnace_enable = ModelingToolkit.SymbolicContinuousCallback( [temp ~ furnace_on_threshold], - ModelingToolkit.MutatingFunctionalAffect(modified = (; furnace_on)) do x, o, i + ModelingToolkit.ImperativeAffect(modified = (; furnace_on)) do x, o, i @set! x.furnace_on = true end) @named sys = ODESystem( @@ -1044,12 +1044,12 @@ end furnace_off = ModelingToolkit.SymbolicContinuousCallback( [temp ~ furnace_off_threshold], - ModelingToolkit.MutatingFunctionalAffect(modified = (; furnace_on)) do x, o + ModelingToolkit.ImperativeAffect(modified = (; furnace_on)) do x, o @set! x.furnace_on = false end) furnace_enable = ModelingToolkit.SymbolicContinuousCallback( [temp ~ furnace_on_threshold], - ModelingToolkit.MutatingFunctionalAffect(modified = (; furnace_on)) do x, o + ModelingToolkit.ImperativeAffect(modified = (; furnace_on)) do x, o @set! x.furnace_on = true end) @named sys = ODESystem( @@ -1061,12 +1061,12 @@ end furnace_off = ModelingToolkit.SymbolicContinuousCallback( [temp ~ furnace_off_threshold], - ModelingToolkit.MutatingFunctionalAffect(modified = (; furnace_on)) do x + ModelingToolkit.ImperativeAffect(modified = (; furnace_on)) do x @set! x.furnace_on = false end) furnace_enable = ModelingToolkit.SymbolicContinuousCallback( [temp ~ furnace_on_threshold], - ModelingToolkit.MutatingFunctionalAffect(modified = (; furnace_on)) do x + ModelingToolkit.ImperativeAffect(modified = (; furnace_on)) do x @set! x.furnace_on = true end) @named sys = ODESystem( @@ -1078,14 +1078,14 @@ end furnace_off = ModelingToolkit.SymbolicContinuousCallback( [temp ~ furnace_off_threshold], - ModelingToolkit.MutatingFunctionalAffect(modified = (; furnace_on)) do x + ModelingToolkit.ImperativeAffect(modified = (; furnace_on)) do x @set! x.furnace_on = false - end; initialize = ModelingToolkit.MutatingFunctionalAffect(modified = (; temp)) do x + end; initialize = ModelingToolkit.ImperativeAffect(modified = (; temp)) do x @set! x.temp = 0.2 end) furnace_enable = ModelingToolkit.SymbolicContinuousCallback( [temp ~ furnace_on_threshold], - ModelingToolkit.MutatingFunctionalAffect(modified = (; furnace_on)) do x, o, c, i + ModelingToolkit.ImperativeAffect(modified = (; furnace_on)) do x, o, c, i @set! x.furnace_on = true end) @named sys = ODESystem( @@ -1097,7 +1097,7 @@ end @test all(sol[temp][sol.t .!= 0.0] .<= 0.79) && all(sol[temp][sol.t .!= 0.0] .>= 0.2) end -@testset "MutatingFunctionalAffect errors and warnings" begin +@testset "ImperativeAffect errors and warnings" begin @variables temp(t) params = @parameters furnace_on_threshold=0.5 furnace_off_threshold=0.7 furnace_power=1.0 leakage=0.1 furnace_on::Bool=false eqs = [ @@ -1106,7 +1106,7 @@ end furnace_off = ModelingToolkit.SymbolicContinuousCallback( [temp ~ furnace_off_threshold], - ModelingToolkit.MutatingFunctionalAffect( + ModelingToolkit.ImperativeAffect( modified = (; furnace_on), observed = (; furnace_on)) do x, o, c, i @set! x.furnace_on = false end) @@ -1122,7 +1122,7 @@ end furnace_off = ModelingToolkit.SymbolicContinuousCallback( [temp ~ furnace_off_threshold], - ModelingToolkit.MutatingFunctionalAffect( + ModelingToolkit.ImperativeAffect( modified = (; furnace_on, tempsq), observed = (; furnace_on)) do x, o, c, i @set! x.furnace_on = false end) @@ -1135,7 +1135,7 @@ end @parameters not_actually_here furnace_off = ModelingToolkit.SymbolicContinuousCallback( [temp ~ furnace_off_threshold], - ModelingToolkit.MutatingFunctionalAffect(modified = (; furnace_on), + ModelingToolkit.ImperativeAffect(modified = (; furnace_on), observed = (; furnace_on, not_actually_here)) do x, o, c, i @set! x.furnace_on = false end) @@ -1148,7 +1148,7 @@ end furnace_off = ModelingToolkit.SymbolicContinuousCallback( [temp ~ furnace_off_threshold], - ModelingToolkit.MutatingFunctionalAffect(modified = (; furnace_on), + ModelingToolkit.ImperativeAffect(modified = (; furnace_on), observed = (; furnace_on)) do x, o, c, i return (;fictional2 = false) end) @@ -1181,14 +1181,14 @@ end end end qAevt = ModelingToolkit.SymbolicContinuousCallback([cos(100 * theta) ~ 0], - ModelingToolkit.MutatingFunctionalAffect((; qA, hA, hB, cnt), (; qB)) do x, o, i, c + ModelingToolkit.ImperativeAffect((; qA, hA, hB, cnt), (; qB)) do x, o, i, c @set! x.hA = x.qA @set! x.hB = o.qB @set! x.qA = 1 @set! x.cnt += decoder(x.hA, x.hB, x.qA, o.qB) x end, - affect_neg = ModelingToolkit.MutatingFunctionalAffect( + affect_neg = ModelingToolkit.ImperativeAffect( (; qA, hA, hB, cnt), (; qB)) do x, o, c, i @set! x.hA = x.qA @set! x.hB = o.qB @@ -1197,14 +1197,14 @@ end x end; rootfind = SciMLBase.RightRootFind) qBevt = ModelingToolkit.SymbolicContinuousCallback([cos(100 * theta - π / 2) ~ 0], - ModelingToolkit.MutatingFunctionalAffect((; qB, hA, hB, cnt), (; qA)) do x, o, i, c + ModelingToolkit.ImperativeAffect((; qB, hA, hB, cnt), (; qA)) do x, o, i, c @set! x.hA = o.qA @set! x.hB = x.qB @set! x.qB = 1 @set! x.cnt += decoder(x.hA, x.hB, o.qA, x.qB) x end, - affect_neg = ModelingToolkit.MutatingFunctionalAffect( + affect_neg = ModelingToolkit.ImperativeAffect( (; qB, hA, hB, cnt), (; qA)) do x, o, c, i @set! x.hA = o.qA @set! x.hB = x.qB From 54bb95af05f6212375ba3a9e2717147b45246ad9 Mon Sep 17 00:00:00 2001 From: Benjamin Chung Date: Thu, 17 Oct 2024 17:02:04 -0700 Subject: [PATCH 0181/2176] Fix tests --- src/systems/callbacks.jl | 19 +++++++++++-------- test/symbolic_events.jl | 14 ++++++++++++++ 2 files changed, 25 insertions(+), 8 deletions(-) diff --git a/src/systems/callbacks.jl b/src/systems/callbacks.jl index 73f2a7a6cf..96758ccf04 100644 --- a/src/systems/callbacks.jl +++ b/src/systems/callbacks.jl @@ -781,21 +781,24 @@ function generate_single_rootfinding_callback( end end + user_initfun = isnothing(affect_function.initialize) ? SciMLBase.INITIALIZE_DEFAULT : (c, u, t, i) -> affect_function.initialize(i) if has_index_cache(sys) && (ic = get_index_cache(sys)) !== nothing && (save_idxs = get(ic.callback_to_clocks, cb, nothing)) !== nothing initfn = let save_idxs = save_idxs function (cb, u, t, integrator) + user_initfun(cb, u, t, integrator) for idx in save_idxs SciMLBase.save_discretes!(integrator, idx) end end end else - initfn = SciMLBase.INITIALIZE_DEFAULT + initfn = user_initfun end + return ContinuousCallback( cond, affect_function.affect, affect_function.affect_neg, rootfind = cb.rootfind, - initialize = isnothing(affect_function.initialize) ? SciMLBase.INITIALIZE_DEFAULT : (c, u, t, i) -> affect_function.initialize(i), + initialize = initfn, finalize = isnothing(affect_function.finalize) ? SciMLBase.FINALIZE_DEFAULT : (c, u, t, i) -> affect_function.finalize(i), initializealg = reinitialization_alg(cb)) end @@ -878,8 +881,8 @@ function compile_affect_fn(cb, sys::AbstractODESystem, dvs, ps, kwargs) eq_aff = affects(cb) eq_neg_aff = affect_negs(cb) affect = compile_affect(eq_aff, cb, sys, dvs, ps; expression = Val{false}, kwargs...) - function compile_optional_affect(aff) - if isnothing(aff) + function compile_optional_affect(aff, default=nothing) + if isnothing(aff) || aff==default return nothing else return compile_affect(aff, cb, sys, dvs, ps; expression = Val{false}, kwargs...) @@ -890,8 +893,8 @@ function compile_affect_fn(cb, sys::AbstractODESystem, dvs, ps, kwargs) else affect_neg = compile_optional_affect(eq_neg_aff) end - initialize = compile_optional_affect(initialize_affects(cb)) - finalize = compile_optional_affect(finalize_affects(cb)) + initialize = compile_optional_affect(initialize_affects(cb), NULL_AFFECT) + finalize = compile_optional_affect(finalize_affects(cb), NULL_AFFECT) (affect = affect, affect_neg = affect_neg, initialize = initialize, finalize = finalize) end @@ -1097,11 +1100,11 @@ function compile_user_affect(affect::ImperativeAffect, cb, sys, dvs, ps; kwargs. let user_affect = func(affect), ctx = context(affect) function (integ) # update the to-be-mutated values; this ensures that if you do a no-op then nothing happens - modvals = mod_og_val_fun(integ.u, integ.p..., integ.t) + modvals = mod_og_val_fun(integ.u, integ.p, integ.t) upd_component_array = NamedTuple{mod_names}(modvals) # update the observed values - obs_component_array = NamedTuple{obs_sym_tuple}(obs_fun(integ.u, integ.p..., integ.t)) + obs_component_array = NamedTuple{obs_sym_tuple}(obs_fun(integ.u, integ.p, integ.t)) # let the user do their thing modvals = if applicable(user_affect, upd_component_array, obs_component_array, ctx, integ) diff --git a/test/symbolic_events.jl b/test/symbolic_events.jl index b301b65650..c2c26aae7f 100644 --- a/test/symbolic_events.jl +++ b/test/symbolic_events.jl @@ -1219,3 +1219,17 @@ end sol = solve(prob, Tsit5(); dtmax = 0.01) @test getp(sol, cnt)(sol) == 197 # we get 2 pulses per phase cycle (cos 0 crossing) and we go to 100 cycles; we miss a few due to the initial state end + + + +import RuntimeGeneratedFunctions +function (f::RuntimeGeneratedFunctions.RuntimeGeneratedFunction{argnames, cache_tag, context_tag, id})(args::Vararg{Any, N}) where {N, argnames, cache_tag, context_tag, id} + try + RuntimeGeneratedFunctions.generated_callfunc(f, args...) + catch e + @error "Caught error in RuntimeGeneratedFunction; source code follows" + func_expr = Expr(:->, Expr(:tuple, argnames...), RuntimeGeneratedFunctions._lookup_body(cache_tag, id)) + @show func_expr + rethrow(e) + end +end From 05e89abc0abfedfd4f30e696439b78253d81dc96 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Sat, 12 Oct 2024 13:32:36 +0530 Subject: [PATCH 0182/2176] feat: initial implementation of HomotopyContinuation interface --- Project.toml | 5 + ext/MTKHomotopyContinuationExt.jl | 123 +++++++++++++++++++++++ src/ModelingToolkit.jl | 3 + src/systems/nonlinear/nonlinearsystem.jl | 30 ++++++ 4 files changed, 161 insertions(+) create mode 100644 ext/MTKHomotopyContinuationExt.jl diff --git a/Project.toml b/Project.toml index 808d68af06..c12cae9f23 100644 --- a/Project.toml +++ b/Project.toml @@ -8,6 +8,7 @@ AbstractTrees = "1520ce14-60c1-5f80-bbc7-55ef81b5835c" ArrayInterface = "4fba245c-0d91-5ea0-9b3e-6abc04ee57a9" BlockArrays = "8e7c35d0-a365-5155-bbbb-fb81a777f24e" Combinatorics = "861a8166-3701-5b0c-9a16-15d98fcdc6aa" +CommonSolve = "38540f10-b2f7-11e9-35d8-d573e4eb0ff2" Compat = "34da2185-b29b-5c13-b0c7-acf172513d20" ConstructionBase = "187b0558-2788-49d3-abe0-74a17ed4e7c9" DataStructures = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8" @@ -61,12 +62,14 @@ Unitful = "1986cc42-f94f-5a68-af5c-568840ba703d" BifurcationKit = "0f109fa4-8a5d-4b75-95aa-f515264e7665" ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4" DeepDiffs = "ab62b9b5-e342-54a8-a765-a90f495de1a6" +HomotopyContinuation = "f213a82b-91d6-5c5d-acf7-10f1c761b327" LabelledArrays = "2ee39098-c373-598a-b85f-a56591580800" [extensions] MTKBifurcationKitExt = "BifurcationKit" MTKChainRulesCoreExt = "ChainRulesCore" MTKDeepDiffsExt = "DeepDiffs" +MTKHomotopyContinuationExt = "HomotopyContinuation" MTKLabelledArraysExt = "LabelledArrays" [compat] @@ -76,6 +79,7 @@ BifurcationKit = "0.3" BlockArrays = "1.1" ChainRulesCore = "1" Combinatorics = "1" +CommonSolve = "0.2.4" Compat = "3.42, 4" ConstructionBase = "1" DataInterpolations = "6.4" @@ -97,6 +101,7 @@ ForwardDiff = "0.10.3" FunctionWrappers = "1.1" FunctionWrappersWrappers = "0.1" Graphs = "1.5.2" +HomotopyContinuation = "2.11" InteractiveUtils = "1" JuliaFormatter = "1.0.47" JumpProcesses = "9.13.1" diff --git a/ext/MTKHomotopyContinuationExt.jl b/ext/MTKHomotopyContinuationExt.jl new file mode 100644 index 0000000000..a36e5aef8d --- /dev/null +++ b/ext/MTKHomotopyContinuationExt.jl @@ -0,0 +1,123 @@ +module MTKHomotopyContinuationExt + +using ModelingToolkit +using ModelingToolkit.SciMLBase +using ModelingToolkit.Symbolics: unwrap +using ModelingToolkit.SymbolicIndexingInterface +using HomotopyContinuation +using ModelingToolkit: iscomplete, parameters, has_index_cache, get_index_cache, get_u0, + get_u0_p, check_eqs_u0, CommonSolve + +const MTK = ModelingToolkit + +function contains_variable(x, wrt) + any(isequal(x), wrt) && return true + iscall(x) || return false + return any(y -> contains_variable(y, wrt), arguments(x)) +end + +function is_polynomial(x, wrt) + x = unwrap(x) + symbolic_type(x) == NotSymbolic() && return true + iscall(x) || return true + contains_variable(x, wrt) || return true + any(isequal(x), wrt) && return true + + if operation(x) in (*, +, -) + return all(y -> is_polynomial(y, wrt), arguments(x)) + end + if operation(x) == (^) + b, p = arguments(x) + return is_polynomial(b, wrt) && !contains_variable(p, wrt) + end + return false +end + +function symbolics_to_hc(expr) + if iscall(expr) + if operation(expr) == getindex + args = arguments(expr) + return ModelKit.Variable(getname(args[1]), args[2:end]...) + else + return operation(expr)(symbolics_to_hc.(arguments(expr))...) + end + elseif symbolic_type(expr) == NotSymbolic() + return expr + else + return ModelKit.Variable(getname(expr)) + end +end + +struct MTKHomotopySystem{F, P, J, V} <: HomotopyContinuation.AbstractSystem + f::F + p::P + jac::J + vars::V + nexprs::Int +end + +Base.size(sys::MTKHomotopySystem) = (sys.nexprs, length(sys.vars)) +ModelKit.variables(sys::MTKHomotopySystem) = sys.vars + +function (sys::MTKHomotopySystem)(x, p = nothing) + sys.f(x, sys.p) +end + +function ModelKit.evaluate!(u, sys::MTKHomotopySystem, x, p = nothing) + sys.f(u, x, sys.p) +end + +function ModelKit.evaluate_and_jacobian!(u, U, sys::MTKHomotopySystem, x, p = nothing) + sys.f(u, x, sys.p) + sys.jac(U, x, sys.p) +end + +SymbolicIndexingInterface.parameter_values(s::MTKHomotopySystem) = s.p + +function MTK.HomotopyContinuationProblem( + sys::NonlinearSystem, u0map, parammap; compile = :all, eval_expression = false, eval_module = ModelingToolkit, kwargs...) + if !iscomplete(sys) + error("A completed `NonlinearSystem` is required. Call `complete` or `structural_simplify` on the system before creating a `HomotopyContinuationProblem`") + end + + dvs = unknowns(sys) + eqs = equations(sys) + + for eq in eqs + if !is_polynomial(eq.lhs, dvs) || !is_polynomial(eq.rhs, dvs) + error("Equation $eq is not a polynomial in the unknowns") + end + end + + nlfn, u0, p = MTK.process_SciMLProblem(NonlinearFunction{true}, sys, u0map, parammap; + jac = true, eval_expression, eval_module) + + hvars = symbolics_to_hc.(dvs) + mtkhsys = MTKHomotopySystem(nlfn.f, p, nlfn.jac, hvars, length(eqs)) + + obsfn = MTK.ObservedFunctionCache(sys; eval_expression, eval_module) + + return MTK.HomotopyContinuationProblem(u0, mtkhsys, sys, obsfn) +end + +function CommonSolve.solve(prob::MTK.HomotopyContinuationProblem; kwargs...) + sol = HomotopyContinuation.solve(prob.homotopy_continuation_system; kwargs...) + realsols = HomotopyContinuation.results(sol; only_real = true) + if isempty(realsols) + u = state_values(prob) + resid = prob.homotopy_continuation_system(u) + retcode = SciMLBase.ReturnCode.ConvergenceFailure + else + distance, idx = findmin(realsols) do result + norm(result.solution - state_values(prob)) + end + u = real.(realsols[idx].solution) + resid = prob.homotopy_continuation_system(u) + retcode = SciMLBase.ReturnCode.Success + end + + return SciMLBase.build_solution( + prob, :HomotopyContinuation, u, resid; retcode, original = sol) +end + +end diff --git a/src/ModelingToolkit.jl b/src/ModelingToolkit.jl index 2f57bb1765..7ea8cb247b 100644 --- a/src/ModelingToolkit.jl +++ b/src/ModelingToolkit.jl @@ -54,6 +54,7 @@ using Reexport using RecursiveArrayTools import Graphs: SimpleDiGraph, add_edge!, incidence_matrix import BlockArrays: BlockedArray, Block, blocksize, blocksizes +import CommonSolve using RuntimeGeneratedFunctions using RuntimeGeneratedFunctions: drop_expr @@ -281,4 +282,6 @@ export Clock, SolverStepClock, TimeDomain export MTKParameters, reorder_dimension_by_tunables!, reorder_dimension_by_tunables +export HomotopyContinuationProblem + end # module diff --git a/src/systems/nonlinear/nonlinearsystem.jl b/src/systems/nonlinear/nonlinearsystem.jl index d9ad826a2c..d048fc59c7 100644 --- a/src/systems/nonlinear/nonlinearsystem.jl +++ b/src/systems/nonlinear/nonlinearsystem.jl @@ -565,3 +565,33 @@ function Base.:(==)(sys1::NonlinearSystem, sys2::NonlinearSystem) _eq_unordered(get_ps(sys1), get_ps(sys2)) && all(s1 == s2 for (s1, s2) in zip(get_systems(sys1), get_systems(sys2))) end + +struct HomotopyContinuationProblem{uType, H, O} <: SciMLBase.AbstractNonlinearProblem{uType, true} + u0::uType + homotopy_continuation_system::H + sys::NonlinearSystem + obsfn::O +end + +function HomotopyContinuationProblem(args...; kwargs...) + error("Requires HomotopyContinuationExt") +end + +SymbolicIndexingInterface.symbolic_container(p::HomotopyContinuationProblem) = p.sys +SymbolicIndexingInterface.state_values(p::HomotopyContinuationProblem) = p.u0 +function SymbolicIndexingInterface.set_state!(p::HomotopyContinuationProblem, args...) + set_state!(p.u0, args...) +end +function SymbolicIndexingInterface.parameter_values(p::HomotopyContinuationProblem) + parameter_values(p.homotopy_continuation_system) +end +function SymbolicIndexingInterface.set_parameter!(p::HomotopyContinuationProblem, args...) + set_parameter!(parameter_values(p), args...) +end +function SymbolicIndexingInterface.observed(p::HomotopyContinuationProblem, sym) + if p.obsfn !== nothing + return p.obsfn(sym) + else + return SymbolicIndexingInterface.observed(p.sys, sym) + end +end From 6667457656881cf43db88944ffc16e0d8f0fc4a1 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Sat, 12 Oct 2024 20:08:08 +0530 Subject: [PATCH 0183/2176] test: add tests for HomotopyContinuation.jl interface --- test/extensions/Project.toml | 2 + test/extensions/homotopy_continuation.jl | 62 ++++++++++++++++++++++++ test/runtests.jl | 1 + 3 files changed, 65 insertions(+) create mode 100644 test/extensions/homotopy_continuation.jl diff --git a/test/extensions/Project.toml b/test/extensions/Project.toml index 81097d98d2..d11b88a6e9 100644 --- a/test/extensions/Project.toml +++ b/test/extensions/Project.toml @@ -3,10 +3,12 @@ BifurcationKit = "0f109fa4-8a5d-4b75-95aa-f515264e7665" ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4" ChainRulesTestUtils = "cdddcdb0-9152-4a09-a978-84456f9df70a" ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210" +HomotopyContinuation = "f213a82b-91d6-5c5d-acf7-10f1c761b327" LabelledArrays = "2ee39098-c373-598a-b85f-a56591580800" ModelingToolkit = "961ee093-0014-501f-94e3-6117800e7a78" OrdinaryDiffEq = "1dea7af3-3e70-54e6-95c3-0bf5283fa5ed" SciMLSensitivity = "1ed8b502-d754-442c-8d5d-10ac956f44a1" SciMLStructures = "53ae85a6-f571-4167-b2af-e1d143709226" SymbolicIndexingInterface = "2efcf032-c050-4f8e-a9bb-153293bab1f5" +Symbolics = "0c5d862f-8b57-4792-8d23-62f2024744c7" Zygote = "e88e6eb3-aa80-5325-afca-941959d7151f" diff --git a/test/extensions/homotopy_continuation.jl b/test/extensions/homotopy_continuation.jl new file mode 100644 index 0000000000..51114bfb3b --- /dev/null +++ b/test/extensions/homotopy_continuation.jl @@ -0,0 +1,62 @@ +using ModelingToolkit, NonlinearSolve, SymbolicIndexingInterface +using LinearAlgebra +using Test +import HomotopyContinuation + +@testset "No parameters" begin + @variables x y z + eqs = [0 ~ x^2 + y^2 + 2x * y + 0 ~ x^2 + 4x + 4 + 0 ~ y * z + 4x^2] + @mtkbuild sys = NonlinearSystem(eqs) + prob = HomotopyContinuationProblem(sys, [x => 1.0, y => 1.0, z => 1.0], []) + @test prob[x] == prob[y] == prob[z] == 1.0 + @test prob[x + y] == 2.0 + sol = solve(prob; threading = false) + @test SciMLBase.successful_retcode(sol) + @test norm(sol.resid)≈0.0 atol=1e-10 +end + +struct Wrapper + x::Matrix{Float64} +end + +@testset "Parameters" begin + wrapper(w::Wrapper) = det(w.x) + @register_symbolic wrapper(w::Wrapper) + + @variables x y z + @parameters p q::Int r::Wrapper + + eqs = [0 ~ x^2 + y^2 + p * x * y + 0 ~ x^2 + 4x + q + 0 ~ y * z + 4x^2 + wrapper(r)] + + @mtkbuild sys = NonlinearSystem(eqs) + prob = HomotopyContinuationProblem(sys, [x => 1.0, y => 1.0, z => 1.0], + [p => 2.0, q => 4, r => Wrapper([1.0 1.0; 0.0 0.0])]) + @test prob.ps[p] == 2.0 + @test prob.ps[q] == 4 + @test prob.ps[r].x == [1.0 1.0; 0.0 0.0] + @test prob.ps[p * q] == 8.0 + sol = solve(prob; threading = false) + @test SciMLBase.successful_retcode(sol) + @test norm(sol.resid)≈0.0 atol=1e-10 +end + +@testset "Array variables" begin + @variables x[1:3] + @parameters p[1:3] + _x = collect(x) + eqs = collect(0 .~ vec(sum(_x * _x'; dims = 2)) + collect(p)) + @mtkbuild sys = NonlinearSystem(eqs) + prob = HomotopyContinuationProblem(sys, [x => ones(3)], [p => 1:3]) + @test prob[x] == ones(3) + @test prob[p + x] == [2, 3, 4] + prob[x] = 2ones(3) + @test prob[x] == 2ones(3) + prob.ps[p] = [2, 3, 4] + @test prob.ps[p] == [2, 3, 4] + sol = @test_nowarn solve(prob; threading = false) + @test sol.retcode == ReturnCode.ConvergenceFailure +end diff --git a/test/runtests.jl b/test/runtests.jl index 1c48956458..c7edbfaaf5 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -110,6 +110,7 @@ end if GROUP == "All" || GROUP == "Extensions" activate_extensions_env() @safetestset "BifurcationKit Extension Test" include("extensions/bifurcationkit.jl") + @safetestset "HomotopyContinuation Extension Test" include("extensions/homotopy_continuation.jl") @safetestset "Auto Differentiation Test" include("extensions/ad.jl") @safetestset "LabelledArrays Test" include("labelledarrays.jl") end From 07aa8245a53ff50ef8eefd0ba29fa1d2b95bf28e Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 17 Oct 2024 12:59:45 +0530 Subject: [PATCH 0184/2176] docs: add documentation for HomotopyContinuation interface --- ext/MTKHomotopyContinuationExt.jl | 64 +++++++++++++++++++++--- src/systems/nonlinear/nonlinearsystem.jl | 28 +++++++++-- 2 files changed, 83 insertions(+), 9 deletions(-) diff --git a/ext/MTKHomotopyContinuationExt.jl b/ext/MTKHomotopyContinuationExt.jl index a36e5aef8d..51cb67e7b0 100644 --- a/ext/MTKHomotopyContinuationExt.jl +++ b/ext/MTKHomotopyContinuationExt.jl @@ -4,6 +4,7 @@ using ModelingToolkit using ModelingToolkit.SciMLBase using ModelingToolkit.Symbolics: unwrap using ModelingToolkit.SymbolicIndexingInterface +using ModelingToolkit.DocStringExtensions using HomotopyContinuation using ModelingToolkit: iscomplete, parameters, has_index_cache, get_index_cache, get_u0, get_u0_p, check_eqs_u0, CommonSolve @@ -11,11 +12,14 @@ using ModelingToolkit: iscomplete, parameters, has_index_cache, get_index_cache, const MTK = ModelingToolkit function contains_variable(x, wrt) - any(isequal(x), wrt) && return true - iscall(x) || return false - return any(y -> contains_variable(y, wrt), arguments(x)) + any(y -> occursin(y, x), wrt) end +""" +$(TYPEDSIGNATURES) + +Check if `x` is polynomial with respect to the variables in `wrt`. +""" function is_polynomial(x, wrt) x = unwrap(x) symbolic_type(x) == NotSymbolic() && return true @@ -33,6 +37,11 @@ function is_polynomial(x, wrt) return false end +""" +$(TYPEDSIGNATURES) + +Convert `expr` from a symbolics expression to one that uses `HomotopyContinuation.ModelKit`. +""" function symbolics_to_hc(expr) if iscall(expr) if operation(expr) == getindex @@ -48,11 +57,32 @@ function symbolics_to_hc(expr) end end +""" +$(TYPEDEF) + +A subtype of `HomotopyContinuation.AbstractSystem` used to solve `HomotopyContinuationProblem`s. +""" struct MTKHomotopySystem{F, P, J, V} <: HomotopyContinuation.AbstractSystem + """ + The generated function for the residual of the polynomial system. In-place. + """ f::F + """ + The parameter object. + """ p::P + """ + The generated function for the jacobian of the polynomial system. In-place. + """ jac::J + """ + The `HomotopyContinuation.ModelKit.Variable` representation of the unknowns of + the system. + """ vars::V + """ + The number of polynomials in the system. Must also be equal to `length(vars)`. + """ nexprs::Int end @@ -74,8 +104,17 @@ end SymbolicIndexingInterface.parameter_values(s::MTKHomotopySystem) = s.p +""" + $(TYPEDSIGNATURES) + +Create a `HomotopyContinuationProblem` from a `NonlinearSystem` with polynomial equations. +The problem will be solved by HomotopyContinuation.jl. The resultant `NonlinearSolution` +will contain the polynomial root closest to the point specified by `u0map` (if real roots +exist for the system). +""" function MTK.HomotopyContinuationProblem( - sys::NonlinearSystem, u0map, parammap; compile = :all, eval_expression = false, eval_module = ModelingToolkit, kwargs...) + sys::NonlinearSystem, u0map, parammap = nothing; eval_expression = false, + eval_module = ModelingToolkit, kwargs...) if !iscomplete(sys) error("A completed `NonlinearSystem` is required. Call `complete` or `structural_simplify` on the system before creating a `HomotopyContinuationProblem`") end @@ -100,8 +139,21 @@ function MTK.HomotopyContinuationProblem( return MTK.HomotopyContinuationProblem(u0, mtkhsys, sys, obsfn) end -function CommonSolve.solve(prob::MTK.HomotopyContinuationProblem; kwargs...) - sol = HomotopyContinuation.solve(prob.homotopy_continuation_system; kwargs...) +""" +$(TYPEDSIGNATURES) + +Solve a `HomotopyContinuationProblem`. Ignores the algorithm passed to it, and always +uses `HomotopyContinuation.jl`. All keyword arguments are forwarded to +`HomotopyContinuation.solve`. The original solution as returned by `HomotopyContinuation.jl` +will be available in the `.original` field of the returned `NonlinearSolution`. + +All keyword arguments have their default values in HomotopyContinuation.jl, except +`show_progress` which defaults to `false`. +""" +function CommonSolve.solve(prob::MTK.HomotopyContinuationProblem, + alg = nothing; show_progress = false, kwargs...) + sol = HomotopyContinuation.solve( + prob.homotopy_continuation_system; show_progress, kwargs...) realsols = HomotopyContinuation.results(sol; only_real = true) if isempty(realsols) u = state_values(prob) diff --git a/src/systems/nonlinear/nonlinearsystem.jl b/src/systems/nonlinear/nonlinearsystem.jl index d048fc59c7..01a250d46a 100644 --- a/src/systems/nonlinear/nonlinearsystem.jl +++ b/src/systems/nonlinear/nonlinearsystem.jl @@ -566,15 +566,37 @@ function Base.:(==)(sys1::NonlinearSystem, sys2::NonlinearSystem) all(s1 == s2 for (s1, s2) in zip(get_systems(sys1), get_systems(sys2))) end -struct HomotopyContinuationProblem{uType, H, O} <: SciMLBase.AbstractNonlinearProblem{uType, true} +""" +$(TYPEDEF) + +A type of Nonlinear problem which specializes on polynomial systems and uses +HomotopyContinuation.jl to solve the system. Requires importing HomotopyContinuation.jl to +create and solve. +""" +struct HomotopyContinuationProblem{uType, H, O} <: + SciMLBase.AbstractNonlinearProblem{uType, true} + """ + The initial values of states in the system. If there are multiple real roots of + the system, the one closest to this point is returned. + """ u0::uType + """ + A subtype of `HomotopyContinuation.AbstractSystem` to solve. Also contains the + parameter object. + """ homotopy_continuation_system::H + """ + The `NonlinearSystem` used to create this problem. Used for symbolic indexing. + """ sys::NonlinearSystem + """ + A function which generates and returns observed expressions for the given system. + """ obsfn::O end -function HomotopyContinuationProblem(args...; kwargs...) - error("Requires HomotopyContinuationExt") +function HomotopyContinuationProblem(::AbstractSystem, _u0, _p; kwargs...) + error("HomotopyContinuation.jl is required to create and solve `HomotopyContinuationProblem`s. Please run `Pkg.add(\"HomotopyContinuation\")` to continue.") end SymbolicIndexingInterface.symbolic_container(p::HomotopyContinuationProblem) = p.sys From f358fed206f233d06ccb8920d814a9d89885e21a Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Sat, 19 Oct 2024 15:59:17 +0530 Subject: [PATCH 0185/2176] test: fix linearize tests --- test/downstream/linearize.jl | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test/downstream/linearize.jl b/test/downstream/linearize.jl index 49b4a45629..98abdb0318 100644 --- a/test/downstream/linearize.jl +++ b/test/downstream/linearize.jl @@ -121,13 +121,13 @@ lsys = ModelingToolkit.reorder_unknowns(lsys0, unknowns(ssys), desired_order) lsyss, _ = ModelingToolkit.linearize_symbolic(pid, [reference.u, measurement.u], [ctr_output.u]) -@test substitute( +@test ModelingToolkit.fixpoint_sub( lsyss.A, ModelingToolkit.defaults_and_guesses(pid)) == lsys.A -@test substitute( +@test ModelingToolkit.fixpoint_sub( lsyss.B, ModelingToolkit.defaults_and_guesses(pid)) == lsys.B -@test substitute( +@test ModelingToolkit.fixpoint_sub( lsyss.C, ModelingToolkit.defaults_and_guesses(pid)) == lsys.C -@test substitute( +@test ModelingToolkit.fixpoint_sub( lsyss.D, ModelingToolkit.defaults_and_guesses(pid)) == lsys.D # Test with the reverse desired unknown order as well to verify that similarity transform and reoreder_unknowns really works From ecf01b3a3f1145da7b0d73469b8f5dc76eb65625 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Sat, 19 Oct 2024 16:57:06 +0530 Subject: [PATCH 0186/2176] feat: better detect and report non-polynomial systems --- ext/MTKHomotopyContinuationExt.jl | 23 ++++++++++++++++++--- test/extensions/homotopy_continuation.jl | 26 ++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 3 deletions(-) diff --git a/ext/MTKHomotopyContinuationExt.jl b/ext/MTKHomotopyContinuationExt.jl index 51cb67e7b0..81d9a840c9 100644 --- a/ext/MTKHomotopyContinuationExt.jl +++ b/ext/MTKHomotopyContinuationExt.jl @@ -2,7 +2,7 @@ module MTKHomotopyContinuationExt using ModelingToolkit using ModelingToolkit.SciMLBase -using ModelingToolkit.Symbolics: unwrap +using ModelingToolkit.Symbolics: unwrap, symtype using ModelingToolkit.SymbolicIndexingInterface using ModelingToolkit.DocStringExtensions using HomotopyContinuation @@ -32,8 +32,25 @@ function is_polynomial(x, wrt) end if operation(x) == (^) b, p = arguments(x) - return is_polynomial(b, wrt) && !contains_variable(p, wrt) + is_pow_integer = symtype(p) <: Integer + if !is_pow_integer + if symbolic_type(p) == NotSymbolic() + @warn "In $x: Exponent $p is not an integer" + else + @warn "In $x: Exponent $p is not an integer. Use `@parameters p::Integer` to declare integer parameters." + end + end + exponent_has_unknowns = contains_variable(p, wrt) + if exponent_has_unknowns + @warn "In $x: Exponent $p cannot contain unknowns of the system." + end + base_polynomial = is_polynomial(b, wrt) + if !base_polynomial + @warn "In $x: Base is not a polynomial" + end + return base_polynomial && !exponent_has_unknowns && is_pow_integer end + @warn "In $x: Unrecognized operation $(operation(x)). Allowed polynomial operations are `*, +, -, ^`" return false end @@ -124,7 +141,7 @@ function MTK.HomotopyContinuationProblem( for eq in eqs if !is_polynomial(eq.lhs, dvs) || !is_polynomial(eq.rhs, dvs) - error("Equation $eq is not a polynomial in the unknowns") + error("Equation $eq is not a polynomial in the unknowns. See warnings for further details.") end end diff --git a/test/extensions/homotopy_continuation.jl b/test/extensions/homotopy_continuation.jl index 51114bfb3b..ceabb6b6e3 100644 --- a/test/extensions/homotopy_continuation.jl +++ b/test/extensions/homotopy_continuation.jl @@ -60,3 +60,29 @@ end sol = @test_nowarn solve(prob; threading = false) @test sol.retcode == ReturnCode.ConvergenceFailure end + +@testset "Parametric exponent" begin + @variables x = 1.0 + @parameters n::Integer = 4 + @mtkbuild sys = NonlinearSystem([x^n + x^2 - 1 ~ 0]) + prob = HomotopyContinuationProblem(sys, []) + sol = solve(prob; threading = false) + @test SciMLBase.successful_retcode(sol) +end + +@testset "Polynomial check and warnings" begin + @variables x = 1.0 + @parameters n = 4 + @mtkbuild sys = NonlinearSystem([x^n + x^2 - 1 ~ 0]) + @test_warn ["Exponent", "not an integer", "@parameters"] @test_throws "not a polynomial" HomotopyContinuationProblem( + sys, []) + @mtkbuild sys = NonlinearSystem([x^1.5 + x^2 - 1 ~ 0]) + @test_warn ["Exponent", "not an integer"] @test_throws "not a polynomial" HomotopyContinuationProblem( + sys, []) + @mtkbuild sys = NonlinearSystem([x^x - x ~ 0]) + @test_warn ["Exponent", "unknowns"] @test_throws "not a polynomial" HomotopyContinuationProblem( + sys, []) + @mtkbuild sys = NonlinearSystem([((x^2) / (x + 3))^2 + x ~ 0]) + @test_warn ["Base", "not a polynomial", "Unrecognized operation", "/"] @test_throws "not a polynomial" HomotopyContinuationProblem( + sys, []) +end From 72ffd1e941a8ed7c94b0aadd1ca0305d8c29f8f8 Mon Sep 17 00:00:00 2001 From: Herman Sletmoen Date: Thu, 17 Oct 2024 14:12:42 +0200 Subject: [PATCH 0187/2176] Refactor show() --- src/systems/abstractsystem.jl | 115 +++++++++++++--------------------- 1 file changed, 44 insertions(+), 71 deletions(-) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index b6e2a79c3e..c9d0116434 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -1883,84 +1883,57 @@ function n_extra_equations(sys::AbstractSystem) nextras = n_outer_stream_variables + length(ceqs) end -function Base.show(io::IO, mime::MIME"text/plain", sys::AbstractSystem) +function Base.show(io::IO, mime::MIME"text/plain", sys::AbstractSystem; bold = true) + limit = get(io, :limit, false) # if output should be limited, + rows = first(displaysize(io)) ÷ 5 # then allocate ≈1/5 of display height to each list + + # Print name + printstyled(io, "Model $(nameof(sys))"; bold) + + # Print equations eqs = equations(sys) - vars = unknowns(sys) - nvars = length(vars) if eqs isa AbstractArray && eltype(eqs) <: Equation neqs = count(eq -> !(eq.lhs isa Connection), eqs) - Base.printstyled(io, "Model $(nameof(sys)) with $neqs "; bold = true) - nextras = n_extra_equations(sys) - if nextras > 0 - Base.printstyled(io, "("; bold = true) - Base.printstyled(io, neqs + nextras; bold = true, color = :magenta) - Base.printstyled(io, ") "; bold = true) - end - Base.printstyled(io, "equations\n"; bold = true) - else - Base.printstyled(io, "Model $(nameof(sys))\n"; bold = true) - end - # The reduced equations are usually very long. It's not that useful to print - # them. - #Base.print_matrix(io, eqs) - #println(io) - - rows = first(displaysize(io)) ÷ 5 - limit = get(io, :limit, false) - - Base.printstyled(io, "Unknowns ($nvars):"; bold = true) - nrows = min(nvars, limit ? rows : nvars) - limited = nrows < length(vars) - defs = has_defaults(sys) ? defaults(sys) : nothing - for i in 1:nrows - s = vars[i] - print(io, "\n ", s) - - if defs !== nothing - val = get(defs, s, nothing) - if val !== nothing - print(io, " [defaults to ") - show( - IOContext(io, :compact => true, :limit => true, - :displaysize => (1, displaysize(io)[2])), - val) - print(io, "]") - end - description = getdescription(s) - if description !== nothing && description != "" - print(io, ": ", description) - end - end - end - limited && print(io, "\n⋮") - println(io) - - vars = parameters(sys) - nvars = length(vars) - Base.printstyled(io, "Parameters ($nvars):"; bold = true) - nrows = min(nvars, limit ? rows : nvars) - limited = nrows < length(vars) - for i in 1:nrows - s = vars[i] - print(io, "\n ", s) - - if defs !== nothing - val = get(defs, s, nothing) - if val !== nothing - print(io, " [defaults to ") - show( - IOContext(io, :compact => true, :limit => true, - :displaysize => (1, displaysize(io)[2])), - val) - print(io, "]") + next = n_extra_equations(sys) + ntot = neqs + next + ntot > 0 && printstyled(io, "\nEquations ($ntot):"; bold) + neqs > 0 && print(io, "\n $neqs solvable … see equations(sys) for all") + next > 0 && print(io, "\n $next extra") + #Base.print_matrix(io, eqs) # usually too long and not useful to print all equations + end + + # Print variables + for varfunc in [unknowns, parameters] + vars = varfunc(sys) + nvars = length(vars) + nvars == 0 && continue # skip + header = titlecase(String(nameof(varfunc))) # e.g. "Unknowns" + printstyled(io, "\n$header ($nvars):"; bold) + nrows = min(nvars, limit ? rows : nvars) + defs = has_defaults(sys) ? defaults(sys) : nothing + for i in 1:nrows + s = vars[i] + print(io, "\n ", s) + if !isnothing(defs) + val = get(defs, s, nothing) + if !isnothing(val) + print(io, " [defaults to ") + show( + IOContext(io, :compact => true, :limit => true, + :displaysize => (1, displaysize(io)[2])), + val) + print(io, "]") + end + desc = getdescription(s) end - description = getdescription(s) - if description !== nothing && description != "" - print(io, ": ", description) + if !isnothing(desc) && desc != "" + print(io, ": ", desc) end end + limited = nrows < nvars + limited && print(io, "\n ⋮") # too many variables to print end - limited && print(io, "\n⋮") + return nothing end From c23cd6234da57e37d7235cc7e764017366279240 Mon Sep 17 00:00:00 2001 From: Herman Sletmoen Date: Thu, 17 Oct 2024 14:29:21 +0200 Subject: [PATCH 0188/2176] Include observed equations --- src/systems/abstractsystem.jl | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index c9d0116434..2712611c82 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -1894,10 +1894,12 @@ function Base.show(io::IO, mime::MIME"text/plain", sys::AbstractSystem; bold = t eqs = equations(sys) if eqs isa AbstractArray && eltype(eqs) <: Equation neqs = count(eq -> !(eq.lhs isa Connection), eqs) + nobs = has_observed(sys) ? length(observed(sys)) : 0 next = n_extra_equations(sys) - ntot = neqs + next + ntot = neqs + nobs + next ntot > 0 && printstyled(io, "\nEquations ($ntot):"; bold) - neqs > 0 && print(io, "\n $neqs solvable … see equations(sys) for all") + neqs > 0 && print(io, "\n $neqs solvable") + nobs > 0 && print(io, "\n $nobs observed") next > 0 && print(io, "\n $next extra") #Base.print_matrix(io, eqs) # usually too long and not useful to print all equations end From 8d7a65269c8e63d254b05be253fc6112ea4d6aa9 Mon Sep 17 00:00:00 2001 From: Herman Sletmoen Date: Thu, 17 Oct 2024 15:18:09 +0200 Subject: [PATCH 0189/2176] Show subsystem names --- src/systems/abstractsystem.jl | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index 2712611c82..117c37a70d 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -1890,6 +1890,14 @@ function Base.show(io::IO, mime::MIME"text/plain", sys::AbstractSystem; bold = t # Print name printstyled(io, "Model $(nameof(sys))"; bold) + # Print subsystems # TODO: limit + subs = nameof.(ModelingToolkit.get_systems(sys)) + nsubs = length(subs) + nsubs > 0 && printstyled(io, "\nSubsystems ($(nsubs)):"; bold) + for sub in subs + print(io, "\n ", sub) + end + # Print equations eqs = equations(sys) if eqs isa AbstractArray && eltype(eqs) <: Equation From ffc43d2a923d8654b17910bba0f55c94dc91171a Mon Sep 17 00:00:00 2001 From: Herman Sletmoen Date: Thu, 17 Oct 2024 16:18:57 +0200 Subject: [PATCH 0190/2176] Add description to all systems --- src/systems/abstractsystem.jl | 1 + src/systems/diffeqs/odesystem.jl | 11 ++++++++--- src/systems/diffeqs/sdesystem.jl | 15 ++++++++++----- src/systems/discrete_system/discrete_system.jl | 12 +++++++++--- src/systems/jumps/jumpsystem.jl | 9 ++++++--- src/systems/nonlinear/nonlinearsystem.jl | 12 +++++++++--- src/systems/optimization/constraints_system.jl | 11 ++++++++--- src/systems/optimization/optimizationsystem.jl | 9 ++++++--- src/systems/pde/pdesystem.jl | 7 ++++++- 9 files changed, 63 insertions(+), 24 deletions(-) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index 117c37a70d..2bc3335b81 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -986,6 +986,7 @@ for prop in [:eqs :ps :tspan :name + :description :var_to_name :ctrls :defaults diff --git a/src/systems/diffeqs/odesystem.jl b/src/systems/diffeqs/odesystem.jl index ec2c5f8157..b59f08878c 100644 --- a/src/systems/diffeqs/odesystem.jl +++ b/src/systems/diffeqs/odesystem.jl @@ -79,6 +79,10 @@ struct ODESystem <: AbstractODESystem """ name::Symbol """ + A description of the system. + """ + description::String + """ The internal systems. These are required to have unique names. """ systems::Vector{ODESystem} @@ -178,7 +182,7 @@ struct ODESystem <: AbstractODESystem parent::Any function ODESystem(tag, deqs, iv, dvs, ps, tspan, var_to_name, ctrls, observed, tgrad, - jac, ctrl_jac, Wfact, Wfact_t, name, systems, defaults, guesses, + jac, ctrl_jac, Wfact, Wfact_t, name, description, systems, defaults, guesses, torn_matching, initializesystem, initialization_eqs, schedule, connector_type, preface, cevents, devents, parameter_dependencies, @@ -199,7 +203,7 @@ struct ODESystem <: AbstractODESystem check_units(u, deqs) end new(tag, deqs, iv, dvs, ps, tspan, var_to_name, ctrls, observed, tgrad, jac, - ctrl_jac, Wfact, Wfact_t, name, systems, defaults, guesses, torn_matching, + ctrl_jac, Wfact, Wfact_t, name, description, systems, defaults, guesses, torn_matching, initializesystem, initialization_eqs, schedule, connector_type, preface, cevents, devents, parameter_dependencies, metadata, gui_metadata, is_dde, tearing_state, substitutions, complete, index_cache, @@ -213,6 +217,7 @@ function ODESystem(deqs::AbstractVector{<:Equation}, iv, dvs, ps; systems = ODESystem[], tspan = nothing, name = nothing, + description = "", default_u0 = Dict(), default_p = Dict(), defaults = _merge(Dict(default_u0), Dict(default_p)), @@ -290,7 +295,7 @@ function ODESystem(deqs::AbstractVector{<:Equation}, iv, dvs, ps; end ODESystem(Threads.atomic_add!(SYSTEM_COUNT, UInt(1)), deqs, iv′, dvs′, ps′, tspan, var_to_name, ctrl′, observed, tgrad, jac, - ctrl_jac, Wfact, Wfact_t, name, systems, defaults, guesses, nothing, initializesystem, + ctrl_jac, Wfact, Wfact_t, name, description, systems, defaults, guesses, nothing, initializesystem, initialization_eqs, schedule, connector_type, preface, cont_callbacks, disc_callbacks, parameter_dependencies, metadata, gui_metadata, is_dde, checks = checks) diff --git a/src/systems/diffeqs/sdesystem.jl b/src/systems/diffeqs/sdesystem.jl index 0d73aaf313..8343f35e17 100644 --- a/src/systems/diffeqs/sdesystem.jl +++ b/src/systems/diffeqs/sdesystem.jl @@ -80,6 +80,10 @@ struct SDESystem <: AbstractODESystem """ name::Symbol """ + A description of the system. + """ + description::String + """ The internal systems. These are required to have unique names. """ systems::Vector{SDESystem} @@ -142,7 +146,7 @@ struct SDESystem <: AbstractODESystem function SDESystem(tag, deqs, neqs, iv, dvs, ps, tspan, var_to_name, ctrls, observed, tgrad, jac, - ctrl_jac, Wfact, Wfact_t, name, systems, defaults, connector_type, + ctrl_jac, Wfact, Wfact_t, name, description, systems, defaults, connector_type, cevents, devents, parameter_dependencies, metadata = nothing, gui_metadata = nothing, complete = false, index_cache = nothing, parent = nothing, is_scalar_noise = false, is_dde = false, @@ -168,7 +172,7 @@ struct SDESystem <: AbstractODESystem end new(tag, deqs, neqs, iv, dvs, ps, tspan, var_to_name, ctrls, observed, tgrad, jac, ctrl_jac, - Wfact, Wfact_t, name, systems, defaults, connector_type, cevents, devents, + Wfact, Wfact_t, name, description, systems, defaults, connector_type, cevents, devents, parameter_dependencies, metadata, gui_metadata, complete, index_cache, parent, is_scalar_noise, is_dde, isscheduled) end @@ -183,6 +187,7 @@ function SDESystem(deqs::AbstractVector{<:Equation}, neqs::AbstractArray, iv, dv default_p = Dict(), defaults = _merge(Dict(default_u0), Dict(default_p)), name = nothing, + description = "", connector_type = nothing, checks = true, continuous_events = nothing, @@ -234,7 +239,7 @@ function SDESystem(deqs::AbstractVector{<:Equation}, neqs::AbstractArray, iv, dv end SDESystem(Threads.atomic_add!(SYSTEM_COUNT, UInt(1)), deqs, neqs, iv′, dvs′, ps′, tspan, var_to_name, ctrl′, observed, tgrad, jac, - ctrl_jac, Wfact, Wfact_t, name, systems, defaults, connector_type, + ctrl_jac, Wfact, Wfact_t, name, description, systems, defaults, connector_type, cont_callbacks, disc_callbacks, parameter_dependencies, metadata, gui_metadata, complete, index_cache, parent, is_scalar_noise, is_dde; checks = checks) end @@ -349,7 +354,7 @@ function stochastic_integral_transform(sys::SDESystem, correction_factor) end SDESystem(deqs, get_noiseeqs(sys), get_iv(sys), unknowns(sys), parameters(sys), - name = name, parameter_dependencies = parameter_dependencies(sys), checks = false) + name = name, description = get_description(sys), parameter_dependencies = parameter_dependencies(sys), checks = false) end """ @@ -457,7 +462,7 @@ function Girsanov_transform(sys::SDESystem, u; θ0 = 1.0) # return modified SDE System SDESystem(deqs, noiseeqs, get_iv(sys), unknown_vars, parameters(sys); defaults = Dict(θ => θ0), observed = [weight ~ θ / θ0], - name = name, parameter_dependencies = parameter_dependencies(sys), + name = name, description = get_description(sys), parameter_dependencies = parameter_dependencies(sys), checks = false) end diff --git a/src/systems/discrete_system/discrete_system.jl b/src/systems/discrete_system/discrete_system.jl index b09ecf5c4c..5e7ae46a08 100644 --- a/src/systems/discrete_system/discrete_system.jl +++ b/src/systems/discrete_system/discrete_system.jl @@ -42,6 +42,10 @@ struct DiscreteSystem <: AbstractTimeDependentSystem """ name::Symbol """ + A description of the system. + """ + description::String + """ The internal systems. These are required to have unique names. """ systems::Vector{DiscreteSystem} @@ -95,7 +99,7 @@ struct DiscreteSystem <: AbstractTimeDependentSystem function DiscreteSystem(tag, discreteEqs, iv, dvs, ps, tspan, var_to_name, observed, - name, + name, description, systems, defaults, preface, connector_type, parameter_dependencies = Equation[], metadata = nothing, gui_metadata = nothing, tearing_state = nothing, substitutions = nothing, @@ -111,7 +115,7 @@ struct DiscreteSystem <: AbstractTimeDependentSystem u = __get_unit_type(dvs, ps, iv) check_units(u, discreteEqs) end - new(tag, discreteEqs, iv, dvs, ps, tspan, var_to_name, observed, name, + new(tag, discreteEqs, iv, dvs, ps, tspan, var_to_name, observed, name, description, systems, defaults, preface, connector_type, parameter_dependencies, metadata, gui_metadata, @@ -128,6 +132,7 @@ function DiscreteSystem(eqs::AbstractVector{<:Equation}, iv, dvs, ps; systems = DiscreteSystem[], tspan = nothing, name = nothing, + description = "", default_u0 = Dict(), default_p = Dict(), defaults = _merge(Dict(default_u0), Dict(default_p)), @@ -164,7 +169,7 @@ function DiscreteSystem(eqs::AbstractVector{<:Equation}, iv, dvs, ps; throw(ArgumentError("System names must be unique.")) end DiscreteSystem(Threads.atomic_add!(SYSTEM_COUNT, UInt(1)), - eqs, iv′, dvs′, ps′, tspan, var_to_name, observed, name, systems, + eqs, iv′, dvs′, ps′, tspan, var_to_name, observed, name, description, systems, defaults, preface, connector_type, parameter_dependencies, metadata, gui_metadata, kwargs...) end @@ -221,6 +226,7 @@ function flatten(sys::DiscreteSystem, noeqs = false) observed = observed(sys), defaults = defaults(sys), name = nameof(sys), + description = get_description(sys), checks = false) end end diff --git a/src/systems/jumps/jumpsystem.jl b/src/systems/jumps/jumpsystem.jl index a714d4b364..98253569d2 100644 --- a/src/systems/jumps/jumpsystem.jl +++ b/src/systems/jumps/jumpsystem.jl @@ -74,6 +74,8 @@ struct JumpSystem{U <: ArrayPartition} <: AbstractTimeDependentSystem observed::Vector{Equation} """The name of the system.""" name::Symbol + """A description of the system.""" + description::String """The internal systems. These are required to have unique names.""" systems::Vector{JumpSystem} """ @@ -116,7 +118,7 @@ struct JumpSystem{U <: ArrayPartition} <: AbstractTimeDependentSystem index_cache::Union{Nothing, IndexCache} isscheduled::Bool - function JumpSystem{U}(tag, ap::U, iv, unknowns, ps, var_to_name, observed, name, + function JumpSystem{U}(tag, ap::U, iv, unknowns, ps, var_to_name, observed, name, description, systems, defaults, connector_type, devents, parameter_dependencies, metadata = nothing, gui_metadata = nothing, @@ -131,7 +133,7 @@ struct JumpSystem{U <: ArrayPartition} <: AbstractTimeDependentSystem u = __get_unit_type(unknowns, ps, iv) check_units(u, ap, iv) end - new{U}(tag, ap, iv, unknowns, ps, var_to_name, observed, name, systems, defaults, + new{U}(tag, ap, iv, unknowns, ps, var_to_name, observed, name, description, systems, defaults, connector_type, devents, parameter_dependencies, metadata, gui_metadata, complete, index_cache, isscheduled) end @@ -147,6 +149,7 @@ function JumpSystem(eqs, iv, unknowns, ps; default_p = Dict(), defaults = _merge(Dict(default_u0), Dict(default_p)), name = nothing, + description = "", connector_type = nothing, checks = true, continuous_events = nothing, @@ -193,7 +196,7 @@ function JumpSystem(eqs, iv, unknowns, ps; disc_callbacks = SymbolicDiscreteCallbacks(discrete_events) parameter_dependencies, ps = process_parameter_dependencies(parameter_dependencies, ps) JumpSystem{typeof(ap)}(Threads.atomic_add!(SYSTEM_COUNT, UInt(1)), - ap, value(iv), unknowns, ps, var_to_name, observed, name, systems, + ap, value(iv), unknowns, ps, var_to_name, observed, name, description, systems, defaults, connector_type, disc_callbacks, parameter_dependencies, metadata, gui_metadata, checks = checks) end diff --git a/src/systems/nonlinear/nonlinearsystem.jl b/src/systems/nonlinear/nonlinearsystem.jl index d9ad826a2c..07c73798f4 100644 --- a/src/systems/nonlinear/nonlinearsystem.jl +++ b/src/systems/nonlinear/nonlinearsystem.jl @@ -44,6 +44,10 @@ struct NonlinearSystem <: AbstractTimeIndependentSystem """ name::Symbol """ + A description of the system. + """ + description::String + """ The internal systems. These are required to have unique names. """ systems::Vector{NonlinearSystem} @@ -91,7 +95,7 @@ struct NonlinearSystem <: AbstractTimeIndependentSystem parent::Any isscheduled::Bool - function NonlinearSystem(tag, eqs, unknowns, ps, var_to_name, observed, jac, name, + function NonlinearSystem(tag, eqs, unknowns, ps, var_to_name, observed, jac, name, description, systems, defaults, connector_type, parameter_dependencies = Equation[], metadata = nothing, gui_metadata = nothing, @@ -102,7 +106,7 @@ struct NonlinearSystem <: AbstractTimeIndependentSystem u = __get_unit_type(unknowns, ps) check_units(u, eqs) end - new(tag, eqs, unknowns, ps, var_to_name, observed, jac, name, systems, defaults, + new(tag, eqs, unknowns, ps, var_to_name, observed, jac, name, description, systems, defaults, connector_type, parameter_dependencies, metadata, gui_metadata, tearing_state, substitutions, complete, index_cache, parent, isscheduled) end @@ -111,6 +115,7 @@ end function NonlinearSystem(eqs, unknowns, ps; observed = [], name = nothing, + description = "", default_u0 = Dict(), default_p = Dict(), defaults = _merge(Dict(default_u0), Dict(default_p)), @@ -157,7 +162,7 @@ function NonlinearSystem(eqs, unknowns, ps; parameter_dependencies, ps = process_parameter_dependencies( parameter_dependencies, ps) NonlinearSystem(Threads.atomic_add!(SYSTEM_COUNT, UInt(1)), - eqs, unknowns, ps, var_to_name, observed, jac, name, systems, defaults, + eqs, unknowns, ps, var_to_name, observed, jac, name, description, systems, defaults, connector_type, parameter_dependencies, metadata, gui_metadata, checks = checks) end @@ -554,6 +559,7 @@ function flatten(sys::NonlinearSystem, noeqs = false) observed = observed(sys), defaults = defaults(sys), name = nameof(sys), + description = get_description(sys), checks = false) end end diff --git a/src/systems/optimization/constraints_system.jl b/src/systems/optimization/constraints_system.jl index f9176df3a2..054244e005 100644 --- a/src/systems/optimization/constraints_system.jl +++ b/src/systems/optimization/constraints_system.jl @@ -45,6 +45,10 @@ struct ConstraintsSystem <: AbstractTimeIndependentSystem """ name::Symbol """ + A description of the system. + """ + description::String + """ The internal systems. These are required to have unique names. """ systems::Vector{ConstraintsSystem} @@ -79,7 +83,7 @@ struct ConstraintsSystem <: AbstractTimeIndependentSystem index_cache::Union{Nothing, IndexCache} function ConstraintsSystem(tag, constraints, unknowns, ps, var_to_name, observed, jac, - name, + name, description, systems, defaults, connector_type, metadata = nothing, tearing_state = nothing, substitutions = nothing, @@ -89,7 +93,7 @@ struct ConstraintsSystem <: AbstractTimeIndependentSystem u = __get_unit_type(unknowns, ps) check_units(u, constraints) end - new(tag, constraints, unknowns, ps, var_to_name, observed, jac, name, systems, + new(tag, constraints, unknowns, ps, var_to_name, observed, jac, name, description, systems, defaults, connector_type, metadata, tearing_state, substitutions, complete, index_cache) end @@ -100,6 +104,7 @@ equations(sys::ConstraintsSystem) = constraints(sys) # needed for Base.show function ConstraintsSystem(constraints, unknowns, ps; observed = [], name = nothing, + description = "", default_u0 = Dict(), default_p = Dict(), defaults = _merge(Dict(default_u0), Dict(default_p)), @@ -142,7 +147,7 @@ function ConstraintsSystem(constraints, unknowns, ps; isempty(observed) || collect_var_to_name!(var_to_name, (eq.lhs for eq in observed)) ConstraintsSystem(Threads.atomic_add!(SYSTEM_COUNT, UInt(1)), - cstr, unknowns, ps, var_to_name, observed, jac, name, systems, + cstr, unknowns, ps, var_to_name, observed, jac, name, description, systems, defaults, connector_type, metadata, checks = checks) end diff --git a/src/systems/optimization/optimizationsystem.jl b/src/systems/optimization/optimizationsystem.jl index 22488e8bad..1f543dc8d1 100644 --- a/src/systems/optimization/optimizationsystem.jl +++ b/src/systems/optimization/optimizationsystem.jl @@ -37,6 +37,8 @@ struct OptimizationSystem <: AbstractOptimizationSystem constraints::Vector{Union{Equation, Inequality}} """The name of the system.""" name::Symbol + """A description of the system.""" + description::String """The internal systems. These are required to have unique names.""" systems::Vector{OptimizationSystem} """ @@ -67,7 +69,7 @@ struct OptimizationSystem <: AbstractOptimizationSystem isscheduled::Bool function OptimizationSystem(tag, op, unknowns, ps, var_to_name, observed, - constraints, name, systems, defaults, metadata = nothing, + constraints, name, description, systems, defaults, metadata = nothing, gui_metadata = nothing, complete = false, index_cache = nothing, parent = nothing, isscheduled = false; checks::Union{Bool, Int} = true) @@ -78,7 +80,7 @@ struct OptimizationSystem <: AbstractOptimizationSystem check_units(u, constraints) end new(tag, op, unknowns, ps, var_to_name, observed, - constraints, name, systems, defaults, metadata, gui_metadata, complete, + constraints, name, description, systems, defaults, metadata, gui_metadata, complete, index_cache, parent, isscheduled) end end @@ -92,6 +94,7 @@ function OptimizationSystem(op, unknowns, ps; default_p = Dict(), defaults = _merge(Dict(default_u0), Dict(default_p)), name = nothing, + description = "", systems = OptimizationSystem[], checks = true, metadata = nothing, @@ -125,7 +128,7 @@ function OptimizationSystem(op, unknowns, ps; op′, unknowns′, ps′, var_to_name, observed, constraints, - name, systems, defaults, metadata, gui_metadata; + name, description, systems, defaults, metadata, gui_metadata; checks = checks) end diff --git a/src/systems/pde/pdesystem.jl b/src/systems/pde/pdesystem.jl index 7aa3f29191..eac540e401 100644 --- a/src/systems/pde/pdesystem.jl +++ b/src/systems/pde/pdesystem.jl @@ -78,6 +78,10 @@ struct PDESystem <: ModelingToolkit.AbstractMultivariateSystem """ name::Symbol """ + A description of the system. + """ + description::String + """ Metadata for the system, to be used by downstream packages. """ metadata::Any @@ -96,6 +100,7 @@ struct PDESystem <: ModelingToolkit.AbstractMultivariateSystem gui_metadata = nothing, eval_module = @__MODULE__, checks::Union{Bool, Int} = true, + description = "", name) if checks == true || (checks & CheckUnits) > 0 u = __get_unit_type(dvs, ivs, ps) @@ -127,7 +132,7 @@ struct PDESystem <: ModelingToolkit.AbstractMultivariateSystem end new(eqs, bcs, domain, ivs, dvs, ps, defaults, connector_type, systems, analytic, - analytic_func, name, metadata, gui_metadata) + analytic_func, name, description, metadata, gui_metadata) end end From 428bc8d0a760392b5e9c686322d4139541fe69ed Mon Sep 17 00:00:00 2001 From: Herman Sletmoen Date: Thu, 17 Oct 2024 16:23:02 +0200 Subject: [PATCH 0191/2176] Extend system descriptions --- src/systems/abstractsystem.jl | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index 2bc3335b81..0dbe67b2a5 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -2972,12 +2972,13 @@ function extend(sys::AbstractSystem, basesys::AbstractSystem; name::Symbol = nam cevs = union(get_continuous_events(basesys), get_continuous_events(sys)) devs = union(get_discrete_events(basesys), get_discrete_events(sys)) defs = merge(get_defaults(basesys), get_defaults(sys)) # prefer `sys` + desc = join(filter(desc -> !isempty(desc), get_description.([sys, basesys])), " ") # concatenate non-empty descriptions with space meta = union_nothing(get_metadata(basesys), get_metadata(sys)) syss = union(get_systems(basesys), get_systems(sys)) args = length(ivs) == 0 ? (eqs, sts, ps) : (eqs, ivs[1], sts, ps) kwargs = (parameter_dependencies = dep_ps, observed = obs, continuous_events = cevs, discrete_events = devs, defaults = defs, systems = syss, metadata = meta, - name = name, gui_metadata = gui_metadata) + name = name, description = desc, gui_metadata = gui_metadata) # collect fields specific to some system types if basesys isa ODESystem From 59e23d4ea47b7172e9c5f2b4806c2a800b664e54 Mon Sep 17 00:00:00 2001 From: Herman Sletmoen Date: Thu, 17 Oct 2024 16:32:33 +0200 Subject: [PATCH 0192/2176] Show system and subsystem descriptions --- src/systems/abstractsystem.jl | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index 0dbe67b2a5..1d8b085afe 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -1888,15 +1888,26 @@ function Base.show(io::IO, mime::MIME"text/plain", sys::AbstractSystem; bold = t limit = get(io, :limit, false) # if output should be limited, rows = first(displaysize(io)) ÷ 5 # then allocate ≈1/5 of display height to each list - # Print name - printstyled(io, "Model $(nameof(sys))"; bold) + # Print name and description + desc = get_description(sys) + printstyled(io, "Model ", nameof(sys), ":"; bold) + !isempty(desc) && print(io, " ", desc) - # Print subsystems # TODO: limit - subs = nameof.(ModelingToolkit.get_systems(sys)) + # Print subsystems + subs = get_systems(sys) nsubs = length(subs) nsubs > 0 && printstyled(io, "\nSubsystems ($(nsubs)):"; bold) for sub in subs - print(io, "\n ", sub) + name = String(nameof(sub)) + print(io, "\n ", name) + desc = get_description(sub) + if !isempty(desc) + maxlen = displaysize(io)[2] - length(name) - 6 # remaining length of line + if limit && length(desc) > maxlen + desc = chop(desc, tail = length(desc) - maxlen) * "…" # too long + end + print(io, ": ", desc) + end end # Print equations From cc926f9c9665062c951230c6b1c83a32195999f6 Mon Sep 17 00:00:00 2001 From: Herman Sletmoen Date: Thu, 17 Oct 2024 18:31:31 +0200 Subject: [PATCH 0193/2176] Limit number of subsystems shown --- src/systems/abstractsystem.jl | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index 1d8b085afe..f98cc316f7 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -1896,8 +1896,10 @@ function Base.show(io::IO, mime::MIME"text/plain", sys::AbstractSystem; bold = t # Print subsystems subs = get_systems(sys) nsubs = length(subs) - nsubs > 0 && printstyled(io, "\nSubsystems ($(nsubs)):"; bold) - for sub in subs + nrows = min(nsubs, limit ? rows : nsubs) + nrows > 0 && printstyled(io, "\nSubsystems ($(nsubs)):"; bold) + for i in 1:nrows + sub = subs[i] name = String(nameof(sub)) print(io, "\n ", name) desc = get_description(sub) @@ -1909,6 +1911,8 @@ function Base.show(io::IO, mime::MIME"text/plain", sys::AbstractSystem; bold = t print(io, ": ", desc) end end + limited = nrows < nsubs + limited && print(io, "\n ⋮") # too many variables to print # Print equations eqs = equations(sys) From b02abba138af18f2d01bc6d28a8878429dca33d0 Mon Sep 17 00:00:00 2001 From: Herman Sletmoen Date: Thu, 17 Oct 2024 19:06:29 +0200 Subject: [PATCH 0194/2176] Pass description through flatten() --- src/systems/diffeqs/odesystem.jl | 1 + 1 file changed, 1 insertion(+) diff --git a/src/systems/diffeqs/odesystem.jl b/src/systems/diffeqs/odesystem.jl index b59f08878c..155ee211db 100644 --- a/src/systems/diffeqs/odesystem.jl +++ b/src/systems/diffeqs/odesystem.jl @@ -398,6 +398,7 @@ function flatten(sys::ODESystem, noeqs = false) discrete_events = discrete_events(sys), defaults = defaults(sys), name = nameof(sys), + description = get_description(sys), initialization_eqs = initialization_equations(sys), is_dde = is_dde(sys), checks = false) From db9348c9e67d2168576a73fda956c5d4f3f19468 Mon Sep 17 00:00:00 2001 From: Herman Sletmoen Date: Thu, 17 Oct 2024 19:50:47 +0200 Subject: [PATCH 0195/2176] Hint to inspection functions when only parts of a system is shown --- src/systems/abstractsystem.jl | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index f98cc316f7..c79f805819 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -1884,7 +1884,7 @@ function n_extra_equations(sys::AbstractSystem) nextras = n_outer_stream_variables + length(ceqs) end -function Base.show(io::IO, mime::MIME"text/plain", sys::AbstractSystem; bold = true) +function Base.show(io::IO, mime::MIME"text/plain", sys::AbstractSystem; hint = true, bold = true) limit = get(io, :limit, false) # if output should be limited, rows = first(displaysize(io)) ÷ 5 # then allocate ≈1/5 of display height to each list @@ -1898,6 +1898,7 @@ function Base.show(io::IO, mime::MIME"text/plain", sys::AbstractSystem; bold = t nsubs = length(subs) nrows = min(nsubs, limit ? rows : nsubs) nrows > 0 && printstyled(io, "\nSubsystems ($(nsubs)):"; bold) + nrows > 0 && hint && print(io, " see ModelingToolkit.get_systems(sys)") for i in 1:nrows sub = subs[i] name = String(nameof(sub)) @@ -1912,7 +1913,7 @@ function Base.show(io::IO, mime::MIME"text/plain", sys::AbstractSystem; bold = t end end limited = nrows < nsubs - limited && print(io, "\n ⋮") # too many variables to print + limited && print(io, "\n ⋮") # too many to print # Print equations eqs = equations(sys) @@ -1922,6 +1923,7 @@ function Base.show(io::IO, mime::MIME"text/plain", sys::AbstractSystem; bold = t next = n_extra_equations(sys) ntot = neqs + nobs + next ntot > 0 && printstyled(io, "\nEquations ($ntot):"; bold) + ntot > 0 && hint && print(io, " see equations(sys)") neqs > 0 && print(io, "\n $neqs solvable") nobs > 0 && print(io, "\n $nobs observed") next > 0 && print(io, "\n $next extra") @@ -1935,6 +1937,7 @@ function Base.show(io::IO, mime::MIME"text/plain", sys::AbstractSystem; bold = t nvars == 0 && continue # skip header = titlecase(String(nameof(varfunc))) # e.g. "Unknowns" printstyled(io, "\n$header ($nvars):"; bold) + hint && print(io, " see $(nameof(varfunc))(sys)") nrows = min(nvars, limit ? rows : nvars) defs = has_defaults(sys) ? defaults(sys) : nothing for i in 1:nrows @@ -1957,7 +1960,7 @@ function Base.show(io::IO, mime::MIME"text/plain", sys::AbstractSystem; bold = t end end limited = nrows < nvars - limited && print(io, "\n ⋮") # too many variables to print + limited && printstyled(io, "\n ⋮") # too many variables to print end return nothing From d473a73b28ca7eb13b8be058995ddde8f31d2377 Mon Sep 17 00:00:00 2001 From: Herman Sletmoen Date: Fri, 18 Oct 2024 11:20:37 +0200 Subject: [PATCH 0196/2176] Export hierarchy(sys) to print subsystem hierarchy --- src/ModelingToolkit.jl | 2 +- src/systems/abstractsystem.jl | 18 ++++++++++++++---- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/src/ModelingToolkit.jl b/src/ModelingToolkit.jl index 2f57bb1765..7687122a61 100644 --- a/src/ModelingToolkit.jl +++ b/src/ModelingToolkit.jl @@ -243,7 +243,7 @@ export Equation, ConstrainedEquation export Term, Sym export SymScope, LocalScope, ParentScope, DelayParentScope, GlobalScope export independent_variable, equations, controls, observed, full_equations -export initialization_equations, guesses, defaults, parameter_dependencies +export initialization_equations, guesses, defaults, parameter_dependencies, hierarchy export structural_simplify, expand_connections, linearize, linearization_function export calculate_jacobian, generate_jacobian, generate_function, generate_custom_function diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index c79f805819..d8ee0337c8 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -1898,7 +1898,7 @@ function Base.show(io::IO, mime::MIME"text/plain", sys::AbstractSystem; hint = t nsubs = length(subs) nrows = min(nsubs, limit ? rows : nsubs) nrows > 0 && printstyled(io, "\nSubsystems ($(nsubs)):"; bold) - nrows > 0 && hint && print(io, " see ModelingToolkit.get_systems(sys)") + nrows > 0 && hint && print(io, " see hierarchy(sys)") for i in 1:nrows sub = subs[i] name = String(nameof(sub)) @@ -2902,12 +2902,22 @@ function Base.showerror(io::IO, e::HybridSystemNotSupportedException) print(io, "HybridSystemNotSupportedException: ", e.msg) end -function AbstractTrees.children(sys::ModelingToolkit.AbstractSystem) +function AbstractTrees.children(sys::AbstractSystem) ModelingToolkit.get_systems(sys) end -function AbstractTrees.printnode(io::IO, sys::ModelingToolkit.AbstractSystem) - print(io, nameof(sys)) +function AbstractTrees.printnode(io::IO, sys::AbstractSystem; describe = false, bold = false) + printstyled(io, nameof(sys); bold) + describe && !isempty(get_description(sys)) && print(io, ": ", get_description(sys)) end +""" + hierarchy(sys::AbstractSystem; describe = false, bold = describe, kwargs...) + +Print a tree of a system's hierarchy of subsystems. +""" +function hierarchy(sys::AbstractSystem; describe = false, bold = describe, kwargs...) + print_tree(sys; printnode_kw = (describe = describe, bold = bold), kwargs...) +end + function Base.IteratorEltype(::Type{<:TreeIterator{ModelingToolkit.AbstractSystem}}) Base.HasEltype() end From b3f3faf2b941bb9112b19c757bef8f173a1e77c9 Mon Sep 17 00:00:00 2001 From: Herman Sletmoen Date: Fri, 18 Oct 2024 17:02:24 +0200 Subject: [PATCH 0197/2176] Fix format --- src/systems/abstractsystem.jl | 3 ++- src/systems/diffeqs/odesystem.jl | 3 ++- src/systems/diffeqs/sdesystem.jl | 9 ++++++--- src/systems/jumps/jumpsystem.jl | 6 ++++-- src/systems/nonlinear/nonlinearsystem.jl | 6 ++++-- src/systems/optimization/constraints_system.jl | 3 ++- 6 files changed, 20 insertions(+), 10 deletions(-) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index d8ee0337c8..f1646861ee 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -2905,7 +2905,8 @@ end function AbstractTrees.children(sys::AbstractSystem) ModelingToolkit.get_systems(sys) end -function AbstractTrees.printnode(io::IO, sys::AbstractSystem; describe = false, bold = false) +function AbstractTrees.printnode( + io::IO, sys::AbstractSystem; describe = false, bold = false) printstyled(io, nameof(sys); bold) describe && !isempty(get_description(sys)) && print(io, ": ", get_description(sys)) end diff --git a/src/systems/diffeqs/odesystem.jl b/src/systems/diffeqs/odesystem.jl index 155ee211db..b901275121 100644 --- a/src/systems/diffeqs/odesystem.jl +++ b/src/systems/diffeqs/odesystem.jl @@ -295,7 +295,8 @@ function ODESystem(deqs::AbstractVector{<:Equation}, iv, dvs, ps; end ODESystem(Threads.atomic_add!(SYSTEM_COUNT, UInt(1)), deqs, iv′, dvs′, ps′, tspan, var_to_name, ctrl′, observed, tgrad, jac, - ctrl_jac, Wfact, Wfact_t, name, description, systems, defaults, guesses, nothing, initializesystem, + ctrl_jac, Wfact, Wfact_t, name, description, systems, + defaults, guesses, nothing, initializesystem, initialization_eqs, schedule, connector_type, preface, cont_callbacks, disc_callbacks, parameter_dependencies, metadata, gui_metadata, is_dde, checks = checks) diff --git a/src/systems/diffeqs/sdesystem.jl b/src/systems/diffeqs/sdesystem.jl index 8343f35e17..e8a72b81cb 100644 --- a/src/systems/diffeqs/sdesystem.jl +++ b/src/systems/diffeqs/sdesystem.jl @@ -172,7 +172,8 @@ struct SDESystem <: AbstractODESystem end new(tag, deqs, neqs, iv, dvs, ps, tspan, var_to_name, ctrls, observed, tgrad, jac, ctrl_jac, - Wfact, Wfact_t, name, description, systems, defaults, connector_type, cevents, devents, + Wfact, Wfact_t, name, description, systems, + defaults, connector_type, cevents, devents, parameter_dependencies, metadata, gui_metadata, complete, index_cache, parent, is_scalar_noise, is_dde, isscheduled) end @@ -354,7 +355,8 @@ function stochastic_integral_transform(sys::SDESystem, correction_factor) end SDESystem(deqs, get_noiseeqs(sys), get_iv(sys), unknowns(sys), parameters(sys), - name = name, description = get_description(sys), parameter_dependencies = parameter_dependencies(sys), checks = false) + name = name, description = get_description(sys), + parameter_dependencies = parameter_dependencies(sys), checks = false) end """ @@ -462,7 +464,8 @@ function Girsanov_transform(sys::SDESystem, u; θ0 = 1.0) # return modified SDE System SDESystem(deqs, noiseeqs, get_iv(sys), unknown_vars, parameters(sys); defaults = Dict(θ => θ0), observed = [weight ~ θ / θ0], - name = name, description = get_description(sys), parameter_dependencies = parameter_dependencies(sys), + name = name, description = get_description(sys), + parameter_dependencies = parameter_dependencies(sys), checks = false) end diff --git a/src/systems/jumps/jumpsystem.jl b/src/systems/jumps/jumpsystem.jl index 98253569d2..6409609a10 100644 --- a/src/systems/jumps/jumpsystem.jl +++ b/src/systems/jumps/jumpsystem.jl @@ -118,7 +118,8 @@ struct JumpSystem{U <: ArrayPartition} <: AbstractTimeDependentSystem index_cache::Union{Nothing, IndexCache} isscheduled::Bool - function JumpSystem{U}(tag, ap::U, iv, unknowns, ps, var_to_name, observed, name, description, + function JumpSystem{U}( + tag, ap::U, iv, unknowns, ps, var_to_name, observed, name, description, systems, defaults, connector_type, devents, parameter_dependencies, metadata = nothing, gui_metadata = nothing, @@ -133,7 +134,8 @@ struct JumpSystem{U <: ArrayPartition} <: AbstractTimeDependentSystem u = __get_unit_type(unknowns, ps, iv) check_units(u, ap, iv) end - new{U}(tag, ap, iv, unknowns, ps, var_to_name, observed, name, description, systems, defaults, + new{U}(tag, ap, iv, unknowns, ps, var_to_name, + observed, name, description, systems, defaults, connector_type, devents, parameter_dependencies, metadata, gui_metadata, complete, index_cache, isscheduled) end diff --git a/src/systems/nonlinear/nonlinearsystem.jl b/src/systems/nonlinear/nonlinearsystem.jl index 07c73798f4..f1b438f0bb 100644 --- a/src/systems/nonlinear/nonlinearsystem.jl +++ b/src/systems/nonlinear/nonlinearsystem.jl @@ -95,7 +95,8 @@ struct NonlinearSystem <: AbstractTimeIndependentSystem parent::Any isscheduled::Bool - function NonlinearSystem(tag, eqs, unknowns, ps, var_to_name, observed, jac, name, description, + function NonlinearSystem( + tag, eqs, unknowns, ps, var_to_name, observed, jac, name, description, systems, defaults, connector_type, parameter_dependencies = Equation[], metadata = nothing, gui_metadata = nothing, @@ -106,7 +107,8 @@ struct NonlinearSystem <: AbstractTimeIndependentSystem u = __get_unit_type(unknowns, ps) check_units(u, eqs) end - new(tag, eqs, unknowns, ps, var_to_name, observed, jac, name, description, systems, defaults, + new(tag, eqs, unknowns, ps, var_to_name, observed, + jac, name, description, systems, defaults, connector_type, parameter_dependencies, metadata, gui_metadata, tearing_state, substitutions, complete, index_cache, parent, isscheduled) end diff --git a/src/systems/optimization/constraints_system.jl b/src/systems/optimization/constraints_system.jl index 054244e005..a2756994ac 100644 --- a/src/systems/optimization/constraints_system.jl +++ b/src/systems/optimization/constraints_system.jl @@ -93,7 +93,8 @@ struct ConstraintsSystem <: AbstractTimeIndependentSystem u = __get_unit_type(unknowns, ps) check_units(u, constraints) end - new(tag, constraints, unknowns, ps, var_to_name, observed, jac, name, description, systems, + new(tag, constraints, unknowns, ps, var_to_name, + observed, jac, name, description, systems, defaults, connector_type, metadata, tearing_state, substitutions, complete, index_cache) end From b5b1a8a1665c9ccdb2f2d7e12cf49735d1c51e63 Mon Sep 17 00:00:00 2001 From: Herman Sletmoen Date: Sun, 20 Oct 2024 18:49:02 +0200 Subject: [PATCH 0198/2176] Dispatch on ODESystem to print initialization equation count --- src/systems/diffeqs/odesystem.jl | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/systems/diffeqs/odesystem.jl b/src/systems/diffeqs/odesystem.jl index b901275121..7055da3ac9 100644 --- a/src/systems/diffeqs/odesystem.jl +++ b/src/systems/diffeqs/odesystem.jl @@ -704,3 +704,15 @@ function add_accumulations(sys::ODESystem, vars::Vector{<:Pair}) @set! sys.unknowns = [get_unknowns(sys); avars] @set! sys.defaults = merge(get_defaults(sys), Dict(a => 0.0 for a in avars)) end + +function Base.show(io::IO, mime::MIME"text/plain", sys::ODESystem; hint = true, bold = true) + # Print general AbstractSystem information + invoke(Base.show, Tuple{typeof(io), typeof(mime), AbstractSystem}, io, mime, sys; hint, bold) + + # Print initialization equations (unique to ODESystems) + nini = length(initialization_equations(sys)) + nini > 0 && printstyled(io, "\nInitialization equations ($nini):"; bold) + nini > 0 && hint && print(io, " see initialization_equations(sys)") + + return nothing +end From 2bc4b161a83a16c347a264fc4a83a6bad3a6d00e Mon Sep 17 00:00:00 2001 From: Herman Sletmoen Date: Sun, 20 Oct 2024 19:31:37 +0200 Subject: [PATCH 0199/2176] Distinguish standard and connecting equations; rename and document n_extra_equations() --- src/systems/abstractsystem.jl | 21 +++++++++++++-------- test/components.jl | 2 +- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index f1646861ee..a84beb05da 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -1856,8 +1856,14 @@ function get_or_construct_tearing_state(sys) state end -# TODO: what about inputs? -function n_extra_equations(sys::AbstractSystem) +""" + n_expanded_connection_equations(sys::AbstractSystem) + +Returns the number of equations that the connections in `sys` expands to. +Equivalent to `length(equations(expand_connections(sys))) - length(filter(eq -> !(eq.lhs isa Connection), equations(sys)))`. +""" +function n_expanded_connection_equations(sys::AbstractSystem) + # TODO: what about inputs? isconnector(sys) && return length(get_unknowns(sys)) sys, (csets, _) = generate_connection_set(sys) ceqs, instream_csets = generate_connection_equations_and_stream_connections(csets) @@ -1919,14 +1925,13 @@ function Base.show(io::IO, mime::MIME"text/plain", sys::AbstractSystem; hint = t eqs = equations(sys) if eqs isa AbstractArray && eltype(eqs) <: Equation neqs = count(eq -> !(eq.lhs isa Connection), eqs) + next = n_expanded_connection_equations(sys) nobs = has_observed(sys) ? length(observed(sys)) : 0 - next = n_extra_equations(sys) - ntot = neqs + nobs + next + ntot = neqs + next + nobs ntot > 0 && printstyled(io, "\nEquations ($ntot):"; bold) - ntot > 0 && hint && print(io, " see equations(sys)") - neqs > 0 && print(io, "\n $neqs solvable") - nobs > 0 && print(io, "\n $nobs observed") - next > 0 && print(io, "\n $next extra") + neqs > 0 && print(io, "\n $neqs standard", hint ? ": see equations(sys)" : "") + next > 0 && print(io, "\n $next connecting", hint ? ": see equations(expand_connections(sys))" : "") + nobs > 0 && print(io, "\n $nobs observed", hint ? ": see observed(sys)" : "") #Base.print_matrix(io, eqs) # usually too long and not useful to print all equations end diff --git a/test/components.jl b/test/components.jl index 965578e0c5..ea63e3e758 100644 --- a/test/components.jl +++ b/test/components.jl @@ -41,7 +41,7 @@ end completed_rc_model = complete(rc_model) @test isequal(completed_rc_model.resistor.n.i, resistor.n.i) -@test ModelingToolkit.n_extra_equations(capacitor) == 2 +@test ModelingToolkit.n_expanded_connection_equations(capacitor) == 2 @test length(equations(structural_simplify(rc_model, allow_parameter = false))) == 2 sys = structural_simplify(rc_model) @test_throws ModelingToolkit.RepeatedStructuralSimplificationError structural_simplify(sys) From f7e956f45547e3cd500cd070ab6dce1c6d77f150 Mon Sep 17 00:00:00 2001 From: Herman Sletmoen Date: Sun, 20 Oct 2024 19:34:59 +0200 Subject: [PATCH 0200/2176] Add description to AbstractSystem documentation --- docs/src/basics/AbstractSystem.md | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/src/basics/AbstractSystem.md b/docs/src/basics/AbstractSystem.md index 2161613353..e68a7bb94e 100644 --- a/docs/src/basics/AbstractSystem.md +++ b/docs/src/basics/AbstractSystem.md @@ -63,6 +63,7 @@ Optionally, a system could have: - `get_defaults(sys)`: A `Dict` that maps variables into their default values for the current-level system. - `get_noiseeqs(sys)`: Noise equations of the current-level system. + - `get_description(sys)`: A string that describes what a system represents. - `get_metadata(sys)`: Any metadata about the system or its origin to be used by downstream packages. Note that if you know a system is an `AbstractTimeDependentSystem` you could use `get_iv` to get the From f57535113531300b6bc474646e7b6b57b1158020 Mon Sep 17 00:00:00 2001 From: Herman Sletmoen Date: Sun, 20 Oct 2024 19:52:24 +0200 Subject: [PATCH 0201/2176] Always check if AbstractSystems have a description --- src/systems/abstractsystem.jl | 9 +++++---- src/systems/diffeqs/odesystem.jl | 2 +- src/systems/diffeqs/sdesystem.jl | 4 ++-- src/systems/discrete_system/discrete_system.jl | 2 +- src/systems/nonlinear/nonlinearsystem.jl | 2 +- 5 files changed, 10 insertions(+), 9 deletions(-) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index a84beb05da..1a3c725166 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -498,6 +498,7 @@ end Substitutions(subs, deps) = Substitutions(subs, deps, nothing) Base.nameof(sys::AbstractSystem) = getfield(sys, :name) +description(sys::AbstractSystem) = has_description(sys) ? get_description(sys) : "" #Deprecated function independent_variable(sys::AbstractSystem) @@ -1895,7 +1896,7 @@ function Base.show(io::IO, mime::MIME"text/plain", sys::AbstractSystem; hint = t rows = first(displaysize(io)) ÷ 5 # then allocate ≈1/5 of display height to each list # Print name and description - desc = get_description(sys) + desc = description(sys) printstyled(io, "Model ", nameof(sys), ":"; bold) !isempty(desc) && print(io, " ", desc) @@ -1909,7 +1910,7 @@ function Base.show(io::IO, mime::MIME"text/plain", sys::AbstractSystem; hint = t sub = subs[i] name = String(nameof(sub)) print(io, "\n ", name) - desc = get_description(sub) + desc = description(sub) if !isempty(desc) maxlen = displaysize(io)[2] - length(name) - 6 # remaining length of line if limit && length(desc) > maxlen @@ -2913,7 +2914,7 @@ end function AbstractTrees.printnode( io::IO, sys::AbstractSystem; describe = false, bold = false) printstyled(io, nameof(sys); bold) - describe && !isempty(get_description(sys)) && print(io, ": ", get_description(sys)) + describe && !isempty(description(sys)) && print(io, ": ", description(sys)) end """ hierarchy(sys::AbstractSystem; describe = false, bold = describe, kwargs...) @@ -3006,7 +3007,7 @@ function extend(sys::AbstractSystem, basesys::AbstractSystem; name::Symbol = nam cevs = union(get_continuous_events(basesys), get_continuous_events(sys)) devs = union(get_discrete_events(basesys), get_discrete_events(sys)) defs = merge(get_defaults(basesys), get_defaults(sys)) # prefer `sys` - desc = join(filter(desc -> !isempty(desc), get_description.([sys, basesys])), " ") # concatenate non-empty descriptions with space + desc = join(filter(desc -> !isempty(desc), description.([sys, basesys])), " ") # concatenate non-empty descriptions with space meta = union_nothing(get_metadata(basesys), get_metadata(sys)) syss = union(get_systems(basesys), get_systems(sys)) args = length(ivs) == 0 ? (eqs, sts, ps) : (eqs, ivs[1], sts, ps) diff --git a/src/systems/diffeqs/odesystem.jl b/src/systems/diffeqs/odesystem.jl index 7055da3ac9..2341927ff1 100644 --- a/src/systems/diffeqs/odesystem.jl +++ b/src/systems/diffeqs/odesystem.jl @@ -399,7 +399,7 @@ function flatten(sys::ODESystem, noeqs = false) discrete_events = discrete_events(sys), defaults = defaults(sys), name = nameof(sys), - description = get_description(sys), + description = description(sys), initialization_eqs = initialization_equations(sys), is_dde = is_dde(sys), checks = false) diff --git a/src/systems/diffeqs/sdesystem.jl b/src/systems/diffeqs/sdesystem.jl index e8a72b81cb..9f3814774f 100644 --- a/src/systems/diffeqs/sdesystem.jl +++ b/src/systems/diffeqs/sdesystem.jl @@ -355,7 +355,7 @@ function stochastic_integral_transform(sys::SDESystem, correction_factor) end SDESystem(deqs, get_noiseeqs(sys), get_iv(sys), unknowns(sys), parameters(sys), - name = name, description = get_description(sys), + name = name, description = description(sys), parameter_dependencies = parameter_dependencies(sys), checks = false) end @@ -464,7 +464,7 @@ function Girsanov_transform(sys::SDESystem, u; θ0 = 1.0) # return modified SDE System SDESystem(deqs, noiseeqs, get_iv(sys), unknown_vars, parameters(sys); defaults = Dict(θ => θ0), observed = [weight ~ θ / θ0], - name = name, description = get_description(sys), + name = name, description = description(sys), parameter_dependencies = parameter_dependencies(sys), checks = false) end diff --git a/src/systems/discrete_system/discrete_system.jl b/src/systems/discrete_system/discrete_system.jl index 5e7ae46a08..3e220998cb 100644 --- a/src/systems/discrete_system/discrete_system.jl +++ b/src/systems/discrete_system/discrete_system.jl @@ -226,7 +226,7 @@ function flatten(sys::DiscreteSystem, noeqs = false) observed = observed(sys), defaults = defaults(sys), name = nameof(sys), - description = get_description(sys), + description = description(sys), checks = false) end end diff --git a/src/systems/nonlinear/nonlinearsystem.jl b/src/systems/nonlinear/nonlinearsystem.jl index f1b438f0bb..3b0e4b7e92 100644 --- a/src/systems/nonlinear/nonlinearsystem.jl +++ b/src/systems/nonlinear/nonlinearsystem.jl @@ -561,7 +561,7 @@ function flatten(sys::NonlinearSystem, noeqs = false) observed = observed(sys), defaults = defaults(sys), name = nameof(sys), - description = get_description(sys), + description = description(sys), checks = false) end end From 668bffae6b1744bf31cd44e9f75a88778ace515c Mon Sep 17 00:00:00 2001 From: Herman Sletmoen Date: Sun, 20 Oct 2024 20:44:09 +0200 Subject: [PATCH 0202/2176] Show separate Observed: category instead of mashing it in Equations: --- src/systems/abstractsystem.jl | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index 1a3c725166..ddaeb172f3 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -1927,12 +1927,10 @@ function Base.show(io::IO, mime::MIME"text/plain", sys::AbstractSystem; hint = t if eqs isa AbstractArray && eltype(eqs) <: Equation neqs = count(eq -> !(eq.lhs isa Connection), eqs) next = n_expanded_connection_equations(sys) - nobs = has_observed(sys) ? length(observed(sys)) : 0 - ntot = neqs + next + nobs + ntot = neqs + next ntot > 0 && printstyled(io, "\nEquations ($ntot):"; bold) neqs > 0 && print(io, "\n $neqs standard", hint ? ": see equations(sys)" : "") next > 0 && print(io, "\n $next connecting", hint ? ": see equations(expand_connections(sys))" : "") - nobs > 0 && print(io, "\n $nobs observed", hint ? ": see observed(sys)" : "") #Base.print_matrix(io, eqs) # usually too long and not useful to print all equations end @@ -1969,6 +1967,11 @@ function Base.show(io::IO, mime::MIME"text/plain", sys::AbstractSystem; hint = t limited && printstyled(io, "\n ⋮") # too many variables to print end + # Print observed + nobs = has_observed(sys) ? length(observed(sys)) : 0 + nobs > 0 && printstyled(io, "\nObserved ($nobs):"; bold) + nobs > 0 && hint && print(io, " see observed(sys)") + return nothing end From efad630bd65ed1ba74c0a640f1a5045a6a27af2a Mon Sep 17 00:00:00 2001 From: Herman Sletmoen Date: Sun, 20 Oct 2024 23:58:12 +0200 Subject: [PATCH 0203/2176] Format --- src/systems/abstractsystem.jl | 6 ++++-- src/systems/diffeqs/odesystem.jl | 3 ++- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index ddaeb172f3..226c4ff55d 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -1891,7 +1891,8 @@ function n_expanded_connection_equations(sys::AbstractSystem) nextras = n_outer_stream_variables + length(ceqs) end -function Base.show(io::IO, mime::MIME"text/plain", sys::AbstractSystem; hint = true, bold = true) +function Base.show( + io::IO, mime::MIME"text/plain", sys::AbstractSystem; hint = true, bold = true) limit = get(io, :limit, false) # if output should be limited, rows = first(displaysize(io)) ÷ 5 # then allocate ≈1/5 of display height to each list @@ -1930,7 +1931,8 @@ function Base.show(io::IO, mime::MIME"text/plain", sys::AbstractSystem; hint = t ntot = neqs + next ntot > 0 && printstyled(io, "\nEquations ($ntot):"; bold) neqs > 0 && print(io, "\n $neqs standard", hint ? ": see equations(sys)" : "") - next > 0 && print(io, "\n $next connecting", hint ? ": see equations(expand_connections(sys))" : "") + next > 0 && print(io, "\n $next connecting", + hint ? ": see equations(expand_connections(sys))" : "") #Base.print_matrix(io, eqs) # usually too long and not useful to print all equations end diff --git a/src/systems/diffeqs/odesystem.jl b/src/systems/diffeqs/odesystem.jl index 2341927ff1..2176e53d55 100644 --- a/src/systems/diffeqs/odesystem.jl +++ b/src/systems/diffeqs/odesystem.jl @@ -707,7 +707,8 @@ end function Base.show(io::IO, mime::MIME"text/plain", sys::ODESystem; hint = true, bold = true) # Print general AbstractSystem information - invoke(Base.show, Tuple{typeof(io), typeof(mime), AbstractSystem}, io, mime, sys; hint, bold) + invoke(Base.show, Tuple{typeof(io), typeof(mime), AbstractSystem}, + io, mime, sys; hint, bold) # Print initialization equations (unique to ODESystems) nini = length(initialization_equations(sys)) From e5e8d83f30baeda27187999685d5453cc65c7706 Mon Sep 17 00:00:00 2001 From: Christopher Rackauckas Date: Mon, 21 Oct 2024 05:06:36 -0400 Subject: [PATCH 0204/2176] Update Project.toml --- test/extensions/Project.toml | 1 - 1 file changed, 1 deletion(-) diff --git a/test/extensions/Project.toml b/test/extensions/Project.toml index 0e6ba6bc4a..81097d98d2 100644 --- a/test/extensions/Project.toml +++ b/test/extensions/Project.toml @@ -8,6 +8,5 @@ ModelingToolkit = "961ee093-0014-501f-94e3-6117800e7a78" OrdinaryDiffEq = "1dea7af3-3e70-54e6-95c3-0bf5283fa5ed" SciMLSensitivity = "1ed8b502-d754-442c-8d5d-10ac956f44a1" SciMLStructures = "53ae85a6-f571-4167-b2af-e1d143709226" -Setfield = "efcf1570-3423-57d1-acb7-fd33fddbac46" SymbolicIndexingInterface = "2efcf032-c050-4f8e-a9bb-153293bab1f5" Zygote = "e88e6eb3-aa80-5325-afca-941959d7151f" From 82735205d8fadab13baa2dd33c0dd42887109074 Mon Sep 17 00:00:00 2001 From: Christopher Rackauckas Date: Mon, 21 Oct 2024 05:07:08 -0400 Subject: [PATCH 0205/2176] Update bifurcationkit.jl --- test/extensions/bifurcationkit.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/extensions/bifurcationkit.jl b/test/extensions/bifurcationkit.jl index 724a3ebe64..698dd085c8 100644 --- a/test/extensions/bifurcationkit.jl +++ b/test/extensions/bifurcationkit.jl @@ -1,4 +1,4 @@ -using BifurcationKit, ModelingToolkit, Setfield, Test +using BifurcationKit, ModelingToolkit, Test using ModelingToolkit: t_nounits as t, D_nounits as D # Simple pitchfork diagram, compares solution to native BifurcationKit, checks they are identical. # Checks using `jac=false` option. @@ -36,7 +36,7 @@ let bprob_BK = BifurcationProblem(f_BK, [1.0, 1.0], [-1.0, 1.0], - (Setfield.@lens _[1]); + (@lens _[1]); record_from_solution = (x, p) -> x[1]) bif_dia_BK = bifurcationdiagram(bprob_BK, PALC(), From f74f184d95498281cd248ee6fcbb8fe6a3f502b4 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 21 Oct 2024 15:53:39 +0530 Subject: [PATCH 0206/2176] fix: retain nonnumeric parameter dependencies in initialization system --- src/systems/nonlinear/initializesystem.jl | 8 +++++++- test/initializationsystem.jl | 21 +++++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/src/systems/nonlinear/initializesystem.jl b/src/systems/nonlinear/initializesystem.jl index 99e4e19d09..a79b44888e 100644 --- a/src/systems/nonlinear/initializesystem.jl +++ b/src/systems/nonlinear/initializesystem.jl @@ -138,8 +138,13 @@ function generate_initializesystem(sys::ODESystem; end # 5) parameter dependencies become equations, their LHS become unknowns + # non-numeric dependent parameters stay as parameter dependencies + new_parameter_deps = Equation[] for eq in parameter_dependencies(sys) - is_variable_floatingpoint(eq.lhs) || continue + if !is_variable_floatingpoint(eq.lhs) + push!(new_parameter_deps, eq) + continue + end varp = tovar(eq.lhs) paramsubs[eq.lhs] = varp push!(eqs_ics, eq) @@ -171,6 +176,7 @@ function generate_initializesystem(sys::ODESystem; pars; defaults = defs, checks = check_units, + parameter_dependencies = new_parameter_deps, name, metadata = meta, kwargs...) diff --git a/test/initializationsystem.jl b/test/initializationsystem.jl index e819980752..f6ec724b14 100644 --- a/test/initializationsystem.jl +++ b/test/initializationsystem.jl @@ -815,3 +815,24 @@ end prob2 = @test_nowarn remake(prob; u0 = [y => 0.5]) @test is_variable(prob.f.initializeprob, p) end + +struct Multiplier{T} + a::T + b::T +end + +function (m::Multiplier)(x, y) + m.a * x + m.b * y +end + +@register_symbolic Multiplier(x::Real, y::Real) + +@testset "Nonnumeric parameter dependencies are retained" begin + @variables x(t) y(t) + @parameters foo(::Real, ::Real) p + @mtkbuild sys = ODESystem([D(x) ~ t, 0 ~ foo(x, y)], t; + parameter_dependencies = [foo ~ Multiplier(p, 2p)], guesses = [y => -1.0]) + prob = ODEProblem(sys, [x => 1.0], (0.0, 1.0), [p => 1.0]) + integ = init(prob, Rosenbrock23()) + @test integ[y] ≈ -0.5 +end From 50075a6d250d878e19c6bbcc4ffad73165f48fea Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 21 Oct 2024 19:02:24 +0530 Subject: [PATCH 0207/2176] test: fix downstream linearization test --- test/downstream/linearize.jl | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test/downstream/linearize.jl b/test/downstream/linearize.jl index 49b4a45629..d12d1ffa81 100644 --- a/test/downstream/linearize.jl +++ b/test/downstream/linearize.jl @@ -95,10 +95,10 @@ lsys = ModelingToolkit.reorder_unknowns(lsys0, unknowns(ssys), desired_order) ## Symbolic linearization lsyss, _ = ModelingToolkit.linearize_symbolic(cl, [f.u], [p.x]) -@test substitute(lsyss.A, ModelingToolkit.defaults(cl)) == lsys.A -@test substitute(lsyss.B, ModelingToolkit.defaults(cl)) == lsys.B -@test substitute(lsyss.C, ModelingToolkit.defaults(cl)) == lsys.C -@test substitute(lsyss.D, ModelingToolkit.defaults(cl)) == lsys.D +@test ModelingToolkit.fixpoint_sub(lsyss.A, ModelingToolkit.defaults(cl)) == lsys.A +@test ModelingToolkit.fixpoint_sub(lsyss.B, ModelingToolkit.defaults(cl)) == lsys.B +@test ModelingToolkit.fixpoint_sub(lsyss.C, ModelingToolkit.defaults(cl)) == lsys.C +@test ModelingToolkit.fixpoint_sub(lsyss.D, ModelingToolkit.defaults(cl)) == lsys.D ## using ModelingToolkitStandardLibrary.Blocks: LimPID k = 400 From 170b09413a97bae6cea70d5c339f9f9aa6fced40 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 18 Oct 2024 20:40:00 +0530 Subject: [PATCH 0208/2176] fix: handle array guesses in `InitializationProblem` --- src/systems/diffeqs/abstractodesystem.jl | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/systems/diffeqs/abstractodesystem.jl b/src/systems/diffeqs/abstractodesystem.jl index e0d4c72f2b..94650d43a8 100644 --- a/src/systems/diffeqs/abstractodesystem.jl +++ b/src/systems/diffeqs/abstractodesystem.jl @@ -1311,7 +1311,11 @@ function InitializationProblem{iip, specialize}(sys::AbstractODESystem, u0T = promote_type(u0T, typeof(fullmap[eq.lhs])) end if u0T != Union{} - u0map = Dict(k => symbolic_type(v) == NotSymbolic() ? u0T(v) : v + u0map = Dict(k => if symbolic_type(v) == NotSymbolic() && !is_array_of_symbolics(v) + v isa AbstractArray ? u0T.(v) : u0T(v) + else + v + end for (k, v) in u0map) end if neqs == nunknown From f531800d04665d96fd6e6c7fe05f7f8305853cee Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 21 Oct 2024 17:52:24 +0530 Subject: [PATCH 0209/2176] build: bump Symbolics compat --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 808d68af06..f57da969ff 100644 --- a/Project.toml +++ b/Project.toml @@ -126,7 +126,7 @@ SpecialFunctions = "0.7, 0.8, 0.9, 0.10, 1.0, 2" StaticArrays = "0.10, 0.11, 0.12, 1.0" SymbolicIndexingInterface = "0.3.31" SymbolicUtils = "3.7" -Symbolics = "6.14" +Symbolics = "6.15.2" URIs = "1" UnPack = "0.1, 1.0" Unitful = "1.1" From e563aaa2e7c6e1454e4ffe1afd98ade58e9d655c Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 6 Aug 2024 18:55:58 +0530 Subject: [PATCH 0210/2176] fix: better handle empty parameter dependencies --- src/systems/parameter_buffer.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/systems/parameter_buffer.jl b/src/systems/parameter_buffer.jl index 67b26ea50c..1bd90762bd 100644 --- a/src/systems/parameter_buffer.jl +++ b/src/systems/parameter_buffer.jl @@ -59,7 +59,7 @@ function MTKParameters( end p = Dict() missing_params = Set() - pdeps = has_parameter_dependencies(sys) ? parameter_dependencies(sys) : nothing + pdeps = has_parameter_dependencies(sys) ? parameter_dependencies(sys) : [] for sym in all_ps ttsym = default_toterm(sym) @@ -92,7 +92,7 @@ function MTKParameters( delete!(missing_params, ttsym) end - if pdeps !== nothing + if !isempty(pdeps) for eq in pdeps sym = eq.lhs expr = eq.rhs From f658073de0ab8a6933177a9d41016e17424ccd50 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 6 Aug 2024 18:57:09 +0530 Subject: [PATCH 0211/2176] feat: implement `size` and `IndexStyle` for `MTKParameters` --- src/systems/parameter_buffer.jl | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/systems/parameter_buffer.jl b/src/systems/parameter_buffer.jl index 1bd90762bd..8b5a303c29 100644 --- a/src/systems/parameter_buffer.jl +++ b/src/systems/parameter_buffer.jl @@ -678,6 +678,10 @@ end return len end +Base.size(ps::MTKParameters) = (length(ps),) + +Base.IndexStyle(::Type{T}) where {T <: MTKParameters} = IndexLinear() + Base.getindex(p::MTKParameters, pind::ParameterIndex) = parameter_values(p, pind) Base.setindex!(p::MTKParameters, val, pind::ParameterIndex) = set_parameter!(p, val, pind) From 2811b1d0d1226727134bd15199dd661a9c0ff64c Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 9 Aug 2024 14:02:54 +0530 Subject: [PATCH 0212/2176] refactor: make `repack` call `replace` --- src/systems/parameter_buffer.jl | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/src/systems/parameter_buffer.jl b/src/systems/parameter_buffer.jl index 8b5a303c29..72af9f594d 100644 --- a/src/systems/parameter_buffer.jl +++ b/src/systems/parameter_buffer.jl @@ -279,10 +279,7 @@ function SciMLStructures.canonicalize(::SciMLStructures.Tunable, p::MTKParameter arr = p.tunable repack = let p = p function (new_val) - if new_val !== p.tunable - copyto!(p.tunable, new_val) - end - return p + return SciMLStructures.replace(SciMLStructures.Tunable(), p, new_val) end end return arr, repack, true @@ -303,12 +300,9 @@ for (Portion, field, recurse) in [(SciMLStructures.Discrete, :discrete, 1) (Nonnumeric, :nonnumeric, 1)] @eval function SciMLStructures.canonicalize(::$Portion, p::MTKParameters) as_vector = buffer_to_arraypartition(p.$field) - repack = let as_vector = as_vector, p = p + repack = let p = p function (new_val) - if new_val !== as_vector - update_tuple_of_buffers(new_val, p.$field) - end - p + return SciMLStructures.replace(($Portion)(), p, new_val) end end return as_vector, repack, true From 94f1c3d354c95ed839adb11a61d06617d342d8a2 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 22 Oct 2024 16:38:35 +0530 Subject: [PATCH 0213/2176] test: fix repack test --- test/mtkparameters.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/mtkparameters.jl b/test/mtkparameters.jl index b4fc1a9677..603426aaf7 100644 --- a/test/mtkparameters.jl +++ b/test/mtkparameters.jl @@ -53,7 +53,7 @@ for (portion, values) in [(Tunable(), [1.0, 5.0, 6.0, 7.0]) SciMLStructures.replace!(portion, ps, ones(length(buffer))) # make sure it is in-place @test all(isone, canonicalize(portion, ps)[1]) - repack(zeros(length(buffer))) + global ps = repack(zeros(length(buffer))) @test all(iszero, canonicalize(portion, ps)[1]) end From cd2518e948febc3e1b699b6edf1a175c489b464d Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 22 Oct 2024 15:19:13 +0530 Subject: [PATCH 0214/2176] feat: use observed equations for guesses of observed variables --- src/systems/nonlinear/initializesystem.jl | 11 ++++++++++- test/initializationsystem.jl | 8 ++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/systems/nonlinear/initializesystem.jl b/src/systems/nonlinear/initializesystem.jl index a79b44888e..becace8ec5 100644 --- a/src/systems/nonlinear/initializesystem.jl +++ b/src/systems/nonlinear/initializesystem.jl @@ -165,7 +165,16 @@ function generate_initializesystem(sys::ODESystem; [p for p in parameters(sys) if !haskey(paramsubs, p)] ) - eqs_ics = Symbolics.substitute.([eqs_ics; observed(sys)], (paramsubs,)) + # 7) use observed equations for guesses of observed variables if not provided + obseqs = observed(sys) + for eq in obseqs + haskey(defs, eq.lhs) && continue + any(x -> isequal(default_toterm(x), eq.lhs), keys(defs)) && continue + + defs[eq.lhs] = eq.rhs + end + + eqs_ics = Symbolics.substitute.([eqs_ics; obseqs], (paramsubs,)) vars = [vars; collect(values(paramsubs))] for k in keys(defs) defs[k] = substitute(defs[k], paramsubs) diff --git a/test/initializationsystem.jl b/test/initializationsystem.jl index f6ec724b14..de48821cff 100644 --- a/test/initializationsystem.jl +++ b/test/initializationsystem.jl @@ -836,3 +836,11 @@ end integ = init(prob, Rosenbrock23()) @test integ[y] ≈ -0.5 end + +@testset "Use observed equations for guesses of observed variables" begin + @variables x(t) y(t) [state_priority = 100] + @mtkbuild sys = ODESystem( + [D(x) ~ x + t, y ~ 2x + 1], t; initialization_eqs = [x^3 + y^3 ~ 1]) + isys = ModelingToolkit.generate_initializesystem(sys) + @test isequal(defaults(isys)[y], 2x + 1) +end From 0c52a6e321dff668c671ff94f6261b42522f2282 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 22 Oct 2024 19:22:35 +0530 Subject: [PATCH 0215/2176] docs: add DataInterpolations to docs environment --- docs/Project.toml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/docs/Project.toml b/docs/Project.toml index 078df7d696..0aefe3d701 100644 --- a/docs/Project.toml +++ b/docs/Project.toml @@ -1,6 +1,7 @@ [deps] BenchmarkTools = "6e4b80f9-dd63-53aa-95a3-0cdb28fa8baf" BifurcationKit = "0f109fa4-8a5d-4b75-95aa-f515264e7665" +DataInterpolations = "82cc6244-b520-54b8-b5a6-8a565e85f1d0" DifferentialEquations = "0c46a032-eb83-5123-abaf-570d42b7fbaa" Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f" Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4" @@ -23,6 +24,7 @@ Unitful = "1986cc42-f94f-5a68-af5c-568840ba703d" [compat] BenchmarkTools = "1.3" BifurcationKit = "0.3" +DataInterpolations = "6.5" DifferentialEquations = "7.6" Distributions = "0.25" Documenter = "1" From cfeaf23694a6b8b596cb42c1c957cbf5cd4b103f Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 22 Oct 2024 18:15:16 +0530 Subject: [PATCH 0216/2176] fix: propagate bounds information to variable metadata in `modelingtoolkitize` --- src/systems/optimization/modelingtoolkitize.jl | 9 +++++++++ test/modelingtoolkitize.jl | 10 ++++++++++ 2 files changed, 19 insertions(+) diff --git a/src/systems/optimization/modelingtoolkitize.jl b/src/systems/optimization/modelingtoolkitize.jl index 1ceea795c5..b66f113f6c 100644 --- a/src/systems/optimization/modelingtoolkitize.jl +++ b/src/systems/optimization/modelingtoolkitize.jl @@ -33,6 +33,15 @@ function modelingtoolkitize(prob::DiffEqBase.OptimizationProblem; end _vars = reshape(_vars, size(prob.u0)) vars = ArrayInterface.restructure(prob.u0, _vars) + if prob.ub !== nothing # lb is also !== nothing + vars = map(vars, prob.lb, prob.ub) do sym, lb, ub + if iszero(lb) && iszero(ub) || isinf(lb) && lb < 0 && isinf(ub) && ub > 0 + sym + else + Symbolics.setmetadata(sym, VariableBounds, (lb, ub)) + end + end + end params = if has_p if p_names === nothing && SciMLBase.has_sys(prob.f) p_names = Dict(parameter_index(prob.f.sys, sym) => sym diff --git a/test/modelingtoolkitize.jl b/test/modelingtoolkitize.jl index 32a9720f47..621bf530b7 100644 --- a/test/modelingtoolkitize.jl +++ b/test/modelingtoolkitize.jl @@ -67,6 +67,16 @@ sol = solve(prob, BFGS()) sol = solve(prob, Newton()) @test sol.objective < 1e-8 +prob = OptimizationProblem(ones(3); lb = [-Inf, 0.0, 1.0], ub = [Inf, 0.0, 2.0]) do u, p + sum(abs2, u) +end + +sys = complete(modelingtoolkitize(prob)) +@test !ModelingToolkit.hasbounds(unknowns(sys)[1]) +@test !ModelingToolkit.hasbounds(unknowns(sys)[2]) +@test ModelingToolkit.hasbounds(unknowns(sys)[3]) +@test ModelingToolkit.getbounds(unknowns(sys)[3]) == (1.0, 2.0) + ## SIR System Regression Test β = 0.01# infection rate From 2a3e0230a81706136443c9017e17686d7a9dc83a Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 22 Oct 2024 19:17:11 +0530 Subject: [PATCH 0217/2176] fix: mark bounded optimization unknowns as irreducible --- src/systems/optimization/optimizationsystem.jl | 14 +++++++++++++- src/variables.jl | 1 + test/optimizationsystem.jl | 14 +++++++++++++- 3 files changed, 27 insertions(+), 2 deletions(-) diff --git a/src/systems/optimization/optimizationsystem.jl b/src/systems/optimization/optimizationsystem.jl index 22488e8bad..271e0073ce 100644 --- a/src/systems/optimization/optimizationsystem.jl +++ b/src/systems/optimization/optimizationsystem.jl @@ -103,6 +103,17 @@ function OptimizationSystem(op, unknowns, ps; ps′ = value.(ps) op′ = value(scalarize(op)) + irreducible_subs = Dict() + for i in eachindex(unknowns′) + var = unknowns′[i] + if hasbounds(var) + irreducible_subs[var] = irrvar = setirreducible(var, true) + unknowns′[i] = irrvar + end + end + op′ = substitute(op′, irreducible_subs) + constraints = substitute.(constraints, (irreducible_subs,)) + if !(isempty(default_u0) && isempty(default_p)) Base.depwarn( "`default_u0` and `default_p` are deprecated. Use `defaults` instead.", @@ -113,7 +124,8 @@ function OptimizationSystem(op, unknowns, ps; throw(ArgumentError("System names must be unique.")) end defaults = todict(defaults) - defaults = Dict(value(k) => value(v) + defaults = Dict(substitute(value(k), irreducible_subs) => substitute( + value(v), irreducible_subs) for (k, v) in pairs(defaults) if value(v) !== nothing) var_to_name = Dict() diff --git a/src/variables.jl b/src/variables.jl index c0c875450c..fcfc7f9b1e 100644 --- a/src/variables.jl +++ b/src/variables.jl @@ -106,6 +106,7 @@ isoutput(x) = isvarkind(VariableOutput, x) # Before the solvability check, we already have handled IO variables, so # irreducibility is independent from IO. isirreducible(x) = isvarkind(VariableIrreducible, x) +setirreducible(x, v) = setmetadata(x, VariableIrreducible, v) state_priority(x) = convert(Float64, getmetadata(x, VariableStatePriority, 0.0))::Float64 function default_toterm(x) diff --git a/test/optimizationsystem.jl b/test/optimizationsystem.jl index 426d6d5de0..dfa11fca37 100644 --- a/test/optimizationsystem.jl +++ b/test/optimizationsystem.jl @@ -1,5 +1,5 @@ using ModelingToolkit, SparseArrays, Test, Optimization, OptimizationOptimJL, - OptimizationMOI, Ipopt, AmplNLWriter, Ipopt_jll + OptimizationMOI, Ipopt, AmplNLWriter, Ipopt_jll, SymbolicIndexingInterface using ModelingToolkit: get_metadata @testset "basic" begin @@ -347,3 +347,15 @@ end prob = @test_nowarn OptimizationProblem(sys, nothing) @test_nowarn solve(prob, NelderMead()) end + +@testset "Bounded unknowns are irreducible" begin + @variables x + @variables y [bounds = (-Inf, Inf)] + @variables z [bounds = (1.0, 2.0)] + obj = x^2 + y^2 + z^2 + cons = [y ~ 2x + z ~ 2y] + @mtkbuild sys = OptimizationSystem(obj, [x, y, z], []; constraints = cons) + @test is_variable(sys, z) + @test !is_variable(sys, y) +end From 065490971ad5bb91718f0cd2a61463060b962794 Mon Sep 17 00:00:00 2001 From: Benjamin Chung Date: Tue, 22 Oct 2024 18:36:18 -0700 Subject: [PATCH 0218/2176] Document ImperativeEffect and the SymbolicContinousCallback changes --- docs/Project.toml | 1 + docs/src/basics/Events.md | 198 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 199 insertions(+) diff --git a/docs/Project.toml b/docs/Project.toml index 078df7d696..15f1a6a7f0 100644 --- a/docs/Project.toml +++ b/docs/Project.toml @@ -14,6 +14,7 @@ OptimizationOptimJL = "36348300-93cb-4f02-beb5-3c3902f8871e" OrdinaryDiffEq = "1dea7af3-3e70-54e6-95c3-0bf5283fa5ed" Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80" SciMLStructures = "53ae85a6-f571-4167-b2af-e1d143709226" +Setfield = "efcf1570-3423-57d1-acb7-fd33fddbac46" StochasticDiffEq = "789caeaf-c7a9-5a7d-9973-96adeb23e2a0" SymbolicIndexingInterface = "2efcf032-c050-4f8e-a9bb-153293bab1f5" SymbolicUtils = "d1185830-fcd6-423d-90d6-eec64667417b" diff --git a/docs/src/basics/Events.md b/docs/src/basics/Events.md index f425fdce5b..9d3ba30780 100644 --- a/docs/src/basics/Events.md +++ b/docs/src/basics/Events.md @@ -378,3 +378,201 @@ sol.ps[c] # sol[c] will error, since `c` is not a timeseries value ``` It can be seen that the timeseries for `c` is not saved. + + +## [(Experimental) Imperative affects](@id imp_affects) +The `ImperativeAffect` can be used as an alternative to the aforementioned functional affect form. Note +that `ImperativeAffect` is still experimental; to emphasize this, we do not export it and it should be +included as `ModelingToolkit.ImperativeAffect`. It abstracts over how values are written back to the +system, simplifying the definitions and (in the future) allowing assignments back to observed values +by solving the nonlinear reinitialization problem afterwards. + +We will use two examples to describe `ImperativeAffect`: a simple heater and a quadrature encoder. +These examples will also demonstrate advanced usage of `ModelingToolkit.SymbolicContinousCallback`, +the low-level interface that the aforementioned tuple form converts into and allows control over the +exact SciMLCallbacks event that is generated for a continous event. + +### [Heater](@id heater_events) +Bang-bang control of a heater connected to a leaky plant requires hysteresis in order to prevent control oscillation. + +```@example events +@variables temp(t) +params = @parameters furnace_on_threshold=0.5 furnace_off_threshold=0.7 furnace_power=1.0 leakage=0.1 furnace_on(t)::Bool=false +eqs = [ + D(temp) ~ furnace_on * furnace_power - temp^2 * leakage +] +``` +Our plant is simple. We have a heater that's turned on and off by the clocked parameter `furnace_on` +which adds `furnace_power` forcing to the system when enabled. We then leak heat porportional to `leakage` +as a function of the square of the current temperature. + +We need a controller with hysteresis to conol the plant. We wish the furnace to turn on when the temperature +is below `furnace_on_threshold` and off when above `furnace_off_threshold`, while maintaining its current state +in between. To do this, we create two continous callbacks: +```@example events +using Setfield +furnace_disable = ModelingToolkit.SymbolicContinuousCallback( + [temp ~ furnace_off_threshold], + ModelingToolkit.ImperativeAffect(modified = (; furnace_on)) do x, o, i, c + @set! x.furnace_on = false + end) +furnace_enable = ModelingToolkit.SymbolicContinuousCallback( + [temp ~ furnace_on_threshold], + ModelingToolkit.ImperativeAffect(modified = (; furnace_on)) do x, o, i, c + @set! x.furnace_on = true + end) +``` +We're using the explicit form of `SymbolicContinuousCallback` here, though +so far we aren't using anything that's not possible with the implicit interface. +You can also write +```julia +[temp ~ furnace_off_threshold] => ModelingToolkit.ImperativeAffect(modified = (; furnace_on)) do x, o, i, c + @set! x.furnace_on = false +end +``` +and it would work the same. + +The `ImperativeAffect` is the larger change in this example. `ImperativeAffect` has the constructor signature +```julia + ImperativeAffect(f::Function; modified::NamedTuple, observed::NamedTuple, ctx) +``` +that accepts the function to call, a named tuple of both the names of and symbolic values representing +values in the system to be modified, a named tuple of the values that are merely observed (that is, used from +the system but not modified), and a context that's passed to the affect function. + +In our example, each event merely changes whether the furnace is on or off. Accordingly, we pass a `modified` tuple +`(; furnace_on)` (creating a `NamedTuple` equivalent to `(furnace_on = furnace_on)`). `ImperativeAffect` will then +evaluate this before calling our function to fill out all of the numerical values, then apply them back to the system +once our affect function returns. Furthermore, it will check that it is possible to do this assignment. + +The function given to `ImperativeAffect` needs to have one of four signatures, checked in this order: +* `f(modified::NamedTuple, observed::NamedTuple, ctx, integrator)::NamedTuple` if the function needs the low-level integrator, +* `f(modified::NamedTuple, observed::NamedTuple, ctx)::NamedTuple` if the function needs the user-defined context, +* `f(modified::NamedTuple, observed::NamedTuple)::NamedTuple` if the function also reads observed values from the system, +* `f(modified::NamedTuple)::NamedTuple` if the function only writes values (unknowns or parameters) to the system. +The `do` block in the example implicitly constructs said function inline. For exposition, we use the full version (e.g. `x, o, i, c`) but this could be simplified to merely `x`. + +The function `f` will be called with `observed` and `modified` `NamedTuple`s that are derived from their respective `NamedTuple` definitions. +In our example, if `furnace_on` is `false`, then the value of the `x` that's passed in as `modified` will be `(furnace_on = false)`. +The modified values should be passed out in the same format: to set `furnace_on` to `true` we need to return a tuple `(furnace_on = true)`. +We use Setfield to do this in the example, recreating the result tuple before returning it. + +Accordingly, we can now interpret the `ImperativeAffect` definitions to mean that when `temp = furnace_off_threshold` we +will write `furnace_on = false` back to the system, and when `temp = furnace_on_threshold` we will write `furnace_on = true` back +to the system. + +```@example events +@named sys = ODESystem( + eqs, t, [temp], params; continuous_events = [furnace_disable, furnace_enable]) +ss = structural_simplify(sys) +prob = ODEProblem(ss, [temp => 0.0, furnace_on => true], (0.0, 10.0)) +sol = solve(prob, Tsit5()) +plot(sol) +hline!([sol.ps[furnace_off_threshold], sol.ps[furnace_on_threshold]], l = (:black, 1), primary = false) +``` + +Here we see exactly the desired hysteresis. The heater starts on until the temperature hits +`furnace_off_threshold`. The temperature then bleeds away until `furnace_on_threshold` at which +point the furnace turns on again until `furnace_off_threshold` and so on and so forth. The controller +is effectively regulating the temperature of the plant. + +### [Quadrature Encoder](@id quadrature) +For a more complex application we'll look at modeling a quadrature encoder attached to a shaft spinning at a constant speed. +Traditionally, a quadrature encoder is built out of a code wheel that interrupts the sensors at constant intervals and two sensors slightly out of phase with one another. +A state machine can take the pattern of pulses produced by the two sensors and determine the number of steps that the shaft has spun. The state machine takes the new value +from each sensor and the old values and decodes them into the direction that the wheel has spun in this step. + +```@example events + @variables theta(t) omega(t) + params = @parameters qA=0 qB=0 hA=0 hB=0 cnt::Int=0 + eqs = [D(theta) ~ omega + omega ~ 1.0] +``` +Our continous-time system is extremely simple. We have two states, `theta` for the angle of the shaft +and `omega` for the rate at which it's spinning. We then have parameters for the state machine `qA, qB, hA, hB` +and a step count `cnt`. + +We'll then implement the decoder as a simple Julia function. +```@example events + function decoder(oldA, oldB, newA, newB) + state = (oldA, oldB, newA, newB) + if state == (0, 0, 1, 0) || state == (1, 0, 1, 1) || state == (1, 1, 0, 1) || + state == (0, 1, 0, 0) + return 1 + elseif state == (0, 0, 0, 1) || state == (0, 1, 1, 1) || state == (1, 1, 1, 0) || + state == (1, 0, 0, 0) + return -1 + elseif state == (0, 0, 0, 0) || state == (0, 1, 0, 1) || state == (1, 0, 1, 0) || + state == (1, 1, 1, 1) + return 0 + else + return 0 # err is interpreted as no movement + end + end +``` +Based on the current and old state, this function will return 1 if the wheel spun in the positive direction, +-1 if in the negative, and 0 otherwise. + +The encoder state advances when the occlusion begins or ends. We model the +code wheel as simply detecting when `cos(100*theta)` is 0; if we're at a positive +edge of the 0 crossing, then we interpret that as occlusion (so the discrete `qA` goes to 1). Otherwise, if `cos` is +going negative, we interpret that as lack of occlusion (so the discrete goes to 0). The decoder function is +then invoked to update the count with this new information. + +We can implement this in one of two ways: using edge sign detection or right root finding. For exposition, we +will implement each sensor differently. + +For sensor A, we're using the edge detction method. By providing a different affect to `SymbolicContinuousCallback`'s +`affect_neg` argument, we can specify different behaviour for the negative crossing vs. the positive crossing of the root. +In our encoder, we interpret this as occlusion or nonocclusion of the sensor, update the internal state, and tick the decoder. +```@example events + qAevt = ModelingToolkit.SymbolicContinuousCallback([cos(100 * theta) ~ 0], + ModelingToolkit.ImperativeAffect((; qA, hA, hB, cnt), (; qB)) do x, o, i, c + @set! x.hA = x.qA + @set! x.hB = o.qB + @set! x.qA = 1 + @set! x.cnt += decoder(x.hA, x.hB, x.qA, o.qB) + x + end, + affect_neg = ModelingToolkit.ImperativeAffect( + (; qA, hA, hB, cnt), (; qB)) do x, o, c, i + @set! x.hA = x.qA + @set! x.hB = o.qB + @set! x.qA = 0 + @set! x.cnt += decoder(x.hA, x.hB, x.qA, o.qB) + x + end) +``` + +The other way we can implement a sensor is by changing the root find. +Normally, we use left root finding; the affect will be invoked instantaneously before +the root is crossed. This makes it trickier to figure out what the new state is. +Instead, we can use right root finding: + +```@example events + qBevt = ModelingToolkit.SymbolicContinuousCallback([cos(100 * theta - π / 2) ~ 0], + ModelingToolkit.ImperativeAffect((; qB, hA, hB, cnt), (; qA, theta)) do x, o, i, c + @set! x.hA = o.qA + @set! x.hB = x.qB + @set! x.qB = clamp(sign(cos(100 * o.theta - π / 2)), 0.0, 1.0) + @set! x.cnt += decoder(x.hA, x.hB, o.qA, x.qB) + x + end; rootfind = SciMLBase.RightRootFind) +``` +Here, sensor B is located `π / 2` behind sensor A in angular space, so we're adjusting our +trigger function accordingly. We here ask for right root finding on the callback, so we know +that the value of said function will have the "new" sign rather than the old one. Thus, we can +determine the new state of the sensor from the sign of the indicator function evaluated at the +affect activation point, with -1 mapped to 0. + +We can now simulate the encoder. +```@example events + @named sys = ODESystem( + eqs, t, [theta, omega], params; continuous_events = [qAevt, qBevt]) + ss = structural_simplify(sys) + prob = ODEProblem(ss, [theta => 0.0], (0.0, pi)) + sol = solve(prob, Tsit5(); dtmax = 0.01) + sol.ps[cnt] +``` +`cos(100*theta)` will have 200 crossings in the half rotation we've gone through, so the encoder would notionally count 200 steps. +Our encoder counts 198 steps (it loses one step to initialization and one step due to the final state falling squarely on an edge). \ No newline at end of file From aecd59bfea213d5e0650057135afe7eb6262b988 Mon Sep 17 00:00:00 2001 From: Benjamin Chung Date: Tue, 22 Oct 2024 18:40:34 -0700 Subject: [PATCH 0219/2176] Formatter --- src/systems/callbacks.jl | 164 +++++++++++++++++++++++---------------- test/symbolic_events.jl | 20 ++--- 2 files changed, 107 insertions(+), 77 deletions(-) diff --git a/src/systems/callbacks.jl b/src/systems/callbacks.jl index 96758ccf04..e7198817b1 100644 --- a/src/systems/callbacks.jl +++ b/src/systems/callbacks.jl @@ -116,29 +116,33 @@ function ImperativeAffect(f::Function; modified::NamedTuple = NamedTuple{()}(()), ctx = nothing, skip_checks = false) - ImperativeAffect(f, + ImperativeAffect(f, collect(values(observed)), collect(keys(observed)), - collect(values(modified)), collect(keys(modified)), + collect(values(modified)), collect(keys(modified)), ctx, skip_checks) end function ImperativeAffect(f::Function, modified::NamedTuple; - observed::NamedTuple = NamedTuple{()}(()), ctx = nothing, skip_checks=false) - ImperativeAffect(f, observed = observed, modified = modified, ctx = ctx, skip_checks = skip_checks) + observed::NamedTuple = NamedTuple{()}(()), ctx = nothing, skip_checks = false) + ImperativeAffect( + f, observed = observed, modified = modified, ctx = ctx, skip_checks = skip_checks) end function ImperativeAffect( - f::Function, modified::NamedTuple, observed::NamedTuple; ctx = nothing, skip_checks=false) - ImperativeAffect(f, observed = observed, modified = modified, ctx = ctx, skip_checks = skip_checks) + f::Function, modified::NamedTuple, observed::NamedTuple; ctx = nothing, skip_checks = false) + ImperativeAffect( + f, observed = observed, modified = modified, ctx = ctx, skip_checks = skip_checks) end function ImperativeAffect( - f::Function, modified::NamedTuple, observed::NamedTuple, ctx; skip_checks=false) - ImperativeAffect(f, observed = observed, modified = modified, ctx = ctx, skip_checks = skip_checks) + f::Function, modified::NamedTuple, observed::NamedTuple, ctx; skip_checks = false) + ImperativeAffect( + f, observed = observed, modified = modified, ctx = ctx, skip_checks = skip_checks) end -function Base.show(io::IO, mfa::ImperativeAffect) - obs_vals = join(map((ob,nm) -> "$ob => $nm", mfa.obs, mfa.obs_syms), ", ") - mod_vals = join(map((md,nm) -> "$md => $nm", mfa.modified, mfa.mod_syms), ", ") +function Base.show(io::IO, mfa::ImperativeAffect) + obs_vals = join(map((ob, nm) -> "$ob => $nm", mfa.obs, mfa.obs_syms), ", ") + mod_vals = join(map((md, nm) -> "$md => $nm", mfa.modified, mfa.mod_syms), ", ") affect = mfa.f - print(io, "ImperativeAffect(observed: [$obs_vals], modified: [$mod_vals], affect:$affect)") + print(io, + "ImperativeAffect(observed: [$obs_vals], modified: [$mod_vals], affect:$affect)") end func(f::ImperativeAffect) = f.f context(a::ImperativeAffect) = a.ctx @@ -234,15 +238,16 @@ struct SymbolicContinuousCallback affect_neg::Union{Vector{Equation}, FunctionalAffect, ImperativeAffect, Nothing} rootfind::SciMLBase.RootfindOpt reinitializealg::SciMLBase.DAEInitializationAlgorithm - function SymbolicContinuousCallback(; - eqs::Vector{Equation}, - affect = NULL_AFFECT, - affect_neg = affect, - rootfind = SciMLBase.LeftRootFind, - initialize=NULL_AFFECT, - finalize=NULL_AFFECT, - reinitializealg=SciMLBase.CheckInit()) - new(eqs, initialize, finalize, make_affect(affect), make_affect(affect_neg), rootfind, reinitializealg) + function SymbolicContinuousCallback(; + eqs::Vector{Equation}, + affect = NULL_AFFECT, + affect_neg = affect, + rootfind = SciMLBase.LeftRootFind, + initialize = NULL_AFFECT, + finalize = NULL_AFFECT, + reinitializealg = SciMLBase.CheckInit()) + new(eqs, initialize, finalize, make_affect(affect), + make_affect(affect_neg), rootfind, reinitializealg) end # Default affect to nothing end make_affect(affect) = affect @@ -250,8 +255,8 @@ make_affect(affect::Tuple) = FunctionalAffect(affect...) make_affect(affect::NamedTuple) = FunctionalAffect(; affect...) function Base.:(==)(e1::SymbolicContinuousCallback, e2::SymbolicContinuousCallback) - isequal(e1.eqs, e2.eqs) && isequal(e1.affect, e2.affect) && - isequal(e1.initialize, e2.initialize) && isequal(e1.finalize, e2.finalize) && + isequal(e1.eqs, e2.eqs) && isequal(e1.affect, e2.affect) && + isequal(e1.initialize, e2.initialize) && isequal(e1.finalize, e2.finalize) && isequal(e1.affect_neg, e2.affect_neg) && isequal(e1.rootfind, e2.rootfind) end Base.isempty(cb::SymbolicContinuousCallback) = isempty(cb.eqs) @@ -266,10 +271,9 @@ function Base.hash(cb::SymbolicContinuousCallback, s::UInt) hash(cb.rootfind, s) end - function Base.show(io::IO, cb::SymbolicContinuousCallback) indent = get(io, :indent, 0) - iio = IOContext(io, :indent => indent+1) + iio = IOContext(io, :indent => indent + 1) print(io, "SymbolicContinuousCallback(") print(iio, "Equations:") show(iio, equations(cb)) @@ -298,7 +302,7 @@ end function Base.show(io::IO, mime::MIME"text/plain", cb::SymbolicContinuousCallback) indent = get(io, :indent, 0) - iio = IOContext(io, :indent => indent+1) + iio = IOContext(io, :indent => indent + 1) println(io, "SymbolicContinuousCallback:") println(iio, "Equations:") show(iio, mime, equations(cb)) @@ -338,14 +342,18 @@ end # wrap eq in vector SymbolicContinuousCallback(p::Pair) = SymbolicContinuousCallback(p[1], p[2]) SymbolicContinuousCallback(cb::SymbolicContinuousCallback) = cb # passthrough function SymbolicContinuousCallback(eqs::Equation, affect = NULL_AFFECT; - affect_neg = affect, rootfind = SciMLBase.LeftRootFind, initialize = NULL_AFFECT, finalize = NULL_AFFECT) + affect_neg = affect, rootfind = SciMLBase.LeftRootFind, + initialize = NULL_AFFECT, finalize = NULL_AFFECT) SymbolicContinuousCallback( - eqs = [eqs], affect = affect, affect_neg = affect_neg, rootfind = rootfind, initialize=initialize, finalize=finalize) + eqs = [eqs], affect = affect, affect_neg = affect_neg, rootfind = rootfind, + initialize = initialize, finalize = finalize) end function SymbolicContinuousCallback(eqs::Vector{Equation}, affect = NULL_AFFECT; - affect_neg = affect, rootfind = SciMLBase.LeftRootFind, initialize = NULL_AFFECT, finalize = NULL_AFFECT) + affect_neg = affect, rootfind = SciMLBase.LeftRootFind, + initialize = NULL_AFFECT, finalize = NULL_AFFECT) SymbolicContinuousCallback( - eqs = eqs, affect = affect, affect_neg = affect_neg, rootfind = rootfind, initialize=initialize, finalize=finalize) + eqs = eqs, affect = affect, affect_neg = affect_neg, rootfind = rootfind, + initialize = initialize, finalize = finalize) end SymbolicContinuousCallbacks(cb::SymbolicContinuousCallback) = [cb] @@ -385,8 +393,10 @@ function finalize_affects(cbs::Vector{SymbolicContinuousCallback}) end reinitialization_alg(cb::SymbolicContinuousCallback) = cb.reinitializealg -reinitialization_algs(cbs::Vector{SymbolicContinuousCallback}) = - mapreduce(reinitialization_alg, vcat, cbs, init = SciMLBase.DAEInitializationAlgorithm[]) +function reinitialization_algs(cbs::Vector{SymbolicContinuousCallback}) + mapreduce( + reinitialization_alg, vcat, cbs, init = SciMLBase.DAEInitializationAlgorithm[]) +end namespace_affects(af::Vector, s) = Equation[namespace_affect(a, s) for a in af] namespace_affects(af::FunctionalAffect, s) = namespace_affect(af, s) @@ -436,7 +446,8 @@ struct SymbolicDiscreteCallback affects::Any reinitializealg::SciMLBase.DAEInitializationAlgorithm - function SymbolicDiscreteCallback(condition, affects = NULL_AFFECT, reinitializealg=SciMLBase.CheckInit()) + function SymbolicDiscreteCallback( + condition, affects = NULL_AFFECT, reinitializealg = SciMLBase.CheckInit()) c = scalarize_condition(condition) a = scalarize_affects(affects) new(c, a, reinitializealg) @@ -498,8 +509,10 @@ function affects(cbs::Vector{SymbolicDiscreteCallback}) end reinitialization_alg(cb::SymbolicDiscreteCallback) = cb.reinitializealg -reinitialization_algs(cbs::Vector{SymbolicDiscreteCallback}) = - mapreduce(reinitialization_alg, vcat, cbs, init = SciMLBase.DAEInitializationAlgorithm[]) +function reinitialization_algs(cbs::Vector{SymbolicDiscreteCallback}) + mapreduce( + reinitialization_alg, vcat, cbs, init = SciMLBase.DAEInitializationAlgorithm[]) +end function namespace_callback(cb::SymbolicDiscreteCallback, s)::SymbolicDiscreteCallback af = affects(cb) @@ -781,7 +794,8 @@ function generate_single_rootfinding_callback( end end - user_initfun = isnothing(affect_function.initialize) ? SciMLBase.INITIALIZE_DEFAULT : (c, u, t, i) -> affect_function.initialize(i) + user_initfun = isnothing(affect_function.initialize) ? SciMLBase.INITIALIZE_DEFAULT : + (c, u, t, i) -> affect_function.initialize(i) if has_index_cache(sys) && (ic = get_index_cache(sys)) !== nothing && (save_idxs = get(ic.callback_to_clocks, cb, nothing)) !== nothing initfn = let save_idxs = save_idxs @@ -795,17 +809,19 @@ function generate_single_rootfinding_callback( else initfn = user_initfun end - + return ContinuousCallback( - cond, affect_function.affect, affect_function.affect_neg, rootfind = cb.rootfind, - initialize = initfn, - finalize = isnothing(affect_function.finalize) ? SciMLBase.FINALIZE_DEFAULT : (c, u, t, i) -> affect_function.finalize(i), + cond, affect_function.affect, affect_function.affect_neg, rootfind = cb.rootfind, + initialize = initfn, + finalize = isnothing(affect_function.finalize) ? SciMLBase.FINALIZE_DEFAULT : + (c, u, t, i) -> affect_function.finalize(i), initializealg = reinitialization_alg(cb)) end function generate_vector_rootfinding_callback( cbs, sys::AbstractODESystem, dvs = unknowns(sys), - ps = parameters(sys); rootfind = SciMLBase.RightRootFind, reinitialization = SciMLBase.CheckInit(), kwargs...) + ps = parameters(sys); rootfind = SciMLBase.RightRootFind, + reinitialization = SciMLBase.CheckInit(), kwargs...) eqs = map(cb -> flatten_equations(cb.eqs), cbs) num_eqs = length.(eqs) # fuse equations to create VectorContinuousCallback @@ -821,11 +837,12 @@ function generate_vector_rootfinding_callback( sys, rhss, dvs, ps; expression = Val{false}, kwargs...) affect_functions = @NamedTuple{ - affect::Function, - affect_neg::Union{Function, Nothing}, - initialize::Union{Function, Nothing}, + affect::Function, + affect_neg::Union{Function, Nothing}, + initialize::Union{Function, Nothing}, finalize::Union{Function, Nothing}}[ - compile_affect_fn(cb, sys, dvs, ps, kwargs) for cb in cbs] + compile_affect_fn(cb, sys, dvs, ps, kwargs) + for cb in cbs] cond = function (out, u, t, integ) rf_ip(out, u, parameter_values(integ), t) end @@ -861,17 +878,20 @@ function generate_vector_rootfinding_callback( if isnothing(func) continue else - func(integ) + func(integ) end end end end end end - initialize = handle_optional_setup_fn(map(fn -> fn.initialize, affect_functions), SciMLBase.INITIALIZE_DEFAULT) - finalize = handle_optional_setup_fn(map(fn -> fn.finalize, affect_functions), SciMLBase.FINALIZE_DEFAULT) + initialize = handle_optional_setup_fn( + map(fn -> fn.initialize, affect_functions), SciMLBase.INITIALIZE_DEFAULT) + finalize = handle_optional_setup_fn( + map(fn -> fn.finalize, affect_functions), SciMLBase.FINALIZE_DEFAULT) return VectorContinuousCallback( - cond, affect, affect_neg, length(eqs), rootfind = rootfind, initialize = initialize, finalize = finalize, initializealg = reinitialization) + cond, affect, affect_neg, length(eqs), rootfind = rootfind, initialize = initialize, + finalize = finalize, initializealg = reinitialization) end """ @@ -881,8 +901,8 @@ function compile_affect_fn(cb, sys::AbstractODESystem, dvs, ps, kwargs) eq_aff = affects(cb) eq_neg_aff = affect_negs(cb) affect = compile_affect(eq_aff, cb, sys, dvs, ps; expression = Val{false}, kwargs...) - function compile_optional_affect(aff, default=nothing) - if isnothing(aff) || aff==default + function compile_optional_affect(aff, default = nothing) + if isnothing(aff) || aff == default return nothing else return compile_affect(aff, cb, sys, dvs, ps; expression = Val{false}, kwargs...) @@ -918,13 +938,14 @@ function generate_rootfinding_callback(cbs, sys::AbstractODESystem, dvs = unknow # groupby would be very useful here, but alas cb_classes = Dict{ @NamedTuple{ - rootfind::SciMLBase.RootfindOpt, + rootfind::SciMLBase.RootfindOpt, reinitialization::SciMLBase.DAEInitializationAlgorithm}, Vector{SymbolicContinuousCallback}}() for cb in cbs push!( - get!(() -> SymbolicContinuousCallback[], cb_classes, ( - rootfind = cb.rootfind, - reinitialization = reinitialization_alg(cb))), + get!(() -> SymbolicContinuousCallback[], cb_classes, + ( + rootfind = cb.rootfind, + reinitialization = reinitialization_alg(cb))), cb) end @@ -932,7 +953,8 @@ function generate_rootfinding_callback(cbs, sys::AbstractODESystem, dvs = unknow compiled_callbacks = map(collect(pairs(sort!( OrderedDict(cb_classes); by = p -> p.rootfind)))) do (equiv_class, cbs_in_class) return generate_vector_rootfinding_callback( - cbs_in_class, sys, dvs, ps; rootfind = equiv_class.rootfind, reinitialization=equiv_class.reinitialization, kwargs...) + cbs_in_class, sys, dvs, ps; rootfind = equiv_class.rootfind, + reinitialization = equiv_class.reinitialization, kwargs...) end if length(compiled_callbacks) == 1 return compiled_callbacks[] @@ -984,29 +1006,34 @@ function invalid_variables(sys, expr) filter(x -> !any(isequal(x), all_symbols(sys)), reduce(vcat, vars(expr); init = [])) end function unassignable_variables(sys, expr) - assignable_syms = reduce(vcat, Symbolics.scalarize.(vcat(unknowns(sys), parameters(sys))); init=[]) + assignable_syms = reduce( + vcat, Symbolics.scalarize.(vcat(unknowns(sys), parameters(sys))); init = []) written = reduce(vcat, Symbolics.scalarize.(vars(expr)); init = []) return filter( x -> !any(isequal(x), assignable_syms), written) end -@generated function _generated_writeback(integ, setters::NamedTuple{NS1,<:Tuple}, values::NamedTuple{NS2, <:Tuple}) where {NS1, NS2} +@generated function _generated_writeback(integ, setters::NamedTuple{NS1, <:Tuple}, + values::NamedTuple{NS2, <:Tuple}) where {NS1, NS2} setter_exprs = [] - for name in NS2 + for name in NS2 if !(name in NS1) missing_name = "Tried to write back to $name from affect; only declared states ($NS1) may be written to." error(missing_name) end push!(setter_exprs, :(setters.$name(integ, values.$name))) end - return :(begin $(setter_exprs...) end) + return :(begin + $(setter_exprs...) + end) end function check_assignable(sys, sym) if symbolic_type(sym) == ScalarSymbolic() is_variable(sys, sym) || is_parameter(sys, sym) elseif symbolic_type(sym) == ArraySymbolic() - is_variable(sys, sym) || is_parameter(sys, sym) || all(x -> check_assignable(sys, x), collect(sym)) + is_variable(sys, sym) || is_parameter(sys, sym) || + all(x -> check_assignable(sys, x), collect(sym)) elseif sym isa Union{AbstractArray, Tuple} all(x -> check_assignable(sys, x), sym) else @@ -1084,13 +1111,13 @@ function compile_user_affect(affect::ImperativeAffect, cb, sys, dvs, ps; kwargs. # okay so now to generate the stuff to assign it back into the system mod_pairs = mod_exprs .=> mod_syms - mod_names = (mod_syms..., ) + mod_names = (mod_syms...,) mod_og_val_fun = build_explicit_observed_function( sys, Symbolics.scalarize.(first.(mod_pairs)); array_type = :tuple) upd_funs = NamedTuple{mod_names}((setu.((sys,), first.(mod_pairs))...,)) - + if has_index_cache(sys) && (ic = get_index_cache(sys)) !== nothing save_idxs = get(ic.callback_to_clocks, cb, Int[]) else @@ -1104,10 +1131,12 @@ function compile_user_affect(affect::ImperativeAffect, cb, sys, dvs, ps; kwargs. upd_component_array = NamedTuple{mod_names}(modvals) # update the observed values - obs_component_array = NamedTuple{obs_sym_tuple}(obs_fun(integ.u, integ.p, integ.t)) + obs_component_array = NamedTuple{obs_sym_tuple}(obs_fun( + integ.u, integ.p, integ.t)) # let the user do their thing - modvals = if applicable(user_affect, upd_component_array, obs_component_array, ctx, integ) + modvals = if applicable( + user_affect, upd_component_array, obs_component_array, ctx, integ) user_affect(upd_component_array, obs_component_array, ctx, integ) elseif applicable(user_affect, upd_component_array, obs_component_array, ctx) user_affect(upd_component_array, obs_component_array, ctx) @@ -1122,7 +1151,7 @@ function compile_user_affect(affect::ImperativeAffect, cb, sys, dvs, ps; kwargs. # write the new values back to the integrator _generated_writeback(integ, upd_funs, modvals) - + for idx in save_idxs SciMLBase.save_discretes!(integ, idx) end @@ -1130,7 +1159,8 @@ function compile_user_affect(affect::ImperativeAffect, cb, sys, dvs, ps; kwargs. end end -function compile_affect(affect::Union{FunctionalAffect, ImperativeAffect}, cb, sys, dvs, ps; kwargs...) +function compile_affect( + affect::Union{FunctionalAffect, ImperativeAffect}, cb, sys, dvs, ps; kwargs...) compile_user_affect(affect, cb, sys, dvs, ps; kwargs...) end diff --git a/test/symbolic_events.jl b/test/symbolic_events.jl index c2c26aae7f..f4f97fb2b9 100644 --- a/test/symbolic_events.jl +++ b/test/symbolic_events.jl @@ -1001,7 +1001,7 @@ end @test sol[b] == [5.0, 5.0] @test sol[c] == [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0] end -@testset "Heater" begin +@testset "Heater" begin @variables temp(t) params = @parameters furnace_on_threshold=0.5 furnace_off_threshold=0.7 furnace_power=1.0 leakage=0.1 furnace_on::Bool=false eqs = [ @@ -1080,7 +1080,7 @@ end [temp ~ furnace_off_threshold], ModelingToolkit.ImperativeAffect(modified = (; furnace_on)) do x @set! x.furnace_on = false - end; initialize = ModelingToolkit.ImperativeAffect(modified = (; temp)) do x + end; initialize = ModelingToolkit.ImperativeAffect(modified = (; temp)) do x @set! x.temp = 0.2 end) furnace_enable = ModelingToolkit.SymbolicContinuousCallback( @@ -1145,17 +1145,16 @@ end @test_throws "refers to missing variable(s)" prob=ODEProblem( ss, [temp => 0.0, furnace_on => true], (0.0, 100.0)) - furnace_off = ModelingToolkit.SymbolicContinuousCallback( [temp ~ furnace_off_threshold], ModelingToolkit.ImperativeAffect(modified = (; furnace_on), observed = (; furnace_on)) do x, o, c, i - return (;fictional2 = false) + return (; fictional2 = false) end) @named sys = ODESystem( eqs, t, [temp, tempsq], params; continuous_events = [furnace_off]) ss = structural_simplify(sys) - prob=ODEProblem( + prob = ODEProblem( ss, [temp => 0.0, furnace_on => true], (0.0, 100.0)) @test_throws "Tried to write back to" solve(prob, Tsit5()) end @@ -1220,15 +1219,16 @@ end @test getp(sol, cnt)(sol) == 197 # we get 2 pulses per phase cycle (cos 0 crossing) and we go to 100 cycles; we miss a few due to the initial state end - - import RuntimeGeneratedFunctions -function (f::RuntimeGeneratedFunctions.RuntimeGeneratedFunction{argnames, cache_tag, context_tag, id})(args::Vararg{Any, N}) where {N, argnames, cache_tag, context_tag, id} +function (f::RuntimeGeneratedFunctions.RuntimeGeneratedFunction{ + argnames, cache_tag, context_tag, + id})(args::Vararg{Any, N}) where {N, argnames, cache_tag, context_tag, id} try RuntimeGeneratedFunctions.generated_callfunc(f, args...) - catch e + catch e @error "Caught error in RuntimeGeneratedFunction; source code follows" - func_expr = Expr(:->, Expr(:tuple, argnames...), RuntimeGeneratedFunctions._lookup_body(cache_tag, id)) + func_expr = Expr(:->, Expr(:tuple, argnames...), + RuntimeGeneratedFunctions._lookup_body(cache_tag, id)) @show func_expr rethrow(e) end From 89954e46437a7a6fd3536d699d2e3d1846020a48 Mon Sep 17 00:00:00 2001 From: Benjamin Chung Date: Tue, 22 Oct 2024 18:43:31 -0700 Subject: [PATCH 0220/2176] Formatter (2) --- docs/src/basics/Events.md | 161 +++++++++++++++++-------------- src/systems/diffeqs/odesystem.jl | 8 +- 2 files changed, 95 insertions(+), 74 deletions(-) diff --git a/docs/src/basics/Events.md b/docs/src/basics/Events.md index 9d3ba30780..4a59149308 100644 --- a/docs/src/basics/Events.md +++ b/docs/src/basics/Events.md @@ -379,36 +379,39 @@ sol.ps[c] # sol[c] will error, since `c` is not a timeseries value It can be seen that the timeseries for `c` is not saved. - ## [(Experimental) Imperative affects](@id imp_affects) + The `ImperativeAffect` can be used as an alternative to the aforementioned functional affect form. Note that `ImperativeAffect` is still experimental; to emphasize this, we do not export it and it should be -included as `ModelingToolkit.ImperativeAffect`. It abstracts over how values are written back to the +included as `ModelingToolkit.ImperativeAffect`. It abstracts over how values are written back to the system, simplifying the definitions and (in the future) allowing assignments back to observed values by solving the nonlinear reinitialization problem afterwards. -We will use two examples to describe `ImperativeAffect`: a simple heater and a quadrature encoder. +We will use two examples to describe `ImperativeAffect`: a simple heater and a quadrature encoder. These examples will also demonstrate advanced usage of `ModelingToolkit.SymbolicContinousCallback`, the low-level interface that the aforementioned tuple form converts into and allows control over the exact SciMLCallbacks event that is generated for a continous event. ### [Heater](@id heater_events) + Bang-bang control of a heater connected to a leaky plant requires hysteresis in order to prevent control oscillation. -```@example events +```@example events @variables temp(t) params = @parameters furnace_on_threshold=0.5 furnace_off_threshold=0.7 furnace_power=1.0 leakage=0.1 furnace_on(t)::Bool=false eqs = [ D(temp) ~ furnace_on * furnace_power - temp^2 * leakage ] ``` + Our plant is simple. We have a heater that's turned on and off by the clocked parameter `furnace_on` which adds `furnace_power` forcing to the system when enabled. We then leak heat porportional to `leakage` -as a function of the square of the current temperature. +as a function of the square of the current temperature. We need a controller with hysteresis to conol the plant. We wish the furnace to turn on when the temperature is below `furnace_on_threshold` and off when above `furnace_off_threshold`, while maintaining its current state in between. To do this, we create two continous callbacks: + ```@example events using Setfield furnace_disable = ModelingToolkit.SymbolicContinuousCallback( @@ -422,42 +425,49 @@ furnace_enable = ModelingToolkit.SymbolicContinuousCallback( @set! x.furnace_on = true end) ``` + We're using the explicit form of `SymbolicContinuousCallback` here, though so far we aren't using anything that's not possible with the implicit interface. -You can also write +You can also write + ```julia -[temp ~ furnace_off_threshold] => ModelingToolkit.ImperativeAffect(modified = (; furnace_on)) do x, o, i, c +[temp ~ furnace_off_threshold] => ModelingToolkit.ImperativeAffect(modified = (; + furnace_on)) do x, o, i, c @set! x.furnace_on = false end ``` + and it would work the same. The `ImperativeAffect` is the larger change in this example. `ImperativeAffect` has the constructor signature + ```julia - ImperativeAffect(f::Function; modified::NamedTuple, observed::NamedTuple, ctx) +ImperativeAffect(f::Function; modified::NamedTuple, observed::NamedTuple, ctx) ``` + that accepts the function to call, a named tuple of both the names of and symbolic values representing values in the system to be modified, a named tuple of the values that are merely observed (that is, used from the system but not modified), and a context that's passed to the affect function. In our example, each event merely changes whether the furnace is on or off. Accordingly, we pass a `modified` tuple -`(; furnace_on)` (creating a `NamedTuple` equivalent to `(furnace_on = furnace_on)`). `ImperativeAffect` will then +`(; furnace_on)` (creating a `NamedTuple` equivalent to `(furnace_on = furnace_on)`). `ImperativeAffect` will then evaluate this before calling our function to fill out all of the numerical values, then apply them back to the system once our affect function returns. Furthermore, it will check that it is possible to do this assignment. The function given to `ImperativeAffect` needs to have one of four signatures, checked in this order: -* `f(modified::NamedTuple, observed::NamedTuple, ctx, integrator)::NamedTuple` if the function needs the low-level integrator, -* `f(modified::NamedTuple, observed::NamedTuple, ctx)::NamedTuple` if the function needs the user-defined context, -* `f(modified::NamedTuple, observed::NamedTuple)::NamedTuple` if the function also reads observed values from the system, -* `f(modified::NamedTuple)::NamedTuple` if the function only writes values (unknowns or parameters) to the system. -The `do` block in the example implicitly constructs said function inline. For exposition, we use the full version (e.g. `x, o, i, c`) but this could be simplified to merely `x`. + + - `f(modified::NamedTuple, observed::NamedTuple, ctx, integrator)::NamedTuple` if the function needs the low-level integrator, + - `f(modified::NamedTuple, observed::NamedTuple, ctx)::NamedTuple` if the function needs the user-defined context, + - `f(modified::NamedTuple, observed::NamedTuple)::NamedTuple` if the function also reads observed values from the system, + - `f(modified::NamedTuple)::NamedTuple` if the function only writes values (unknowns or parameters) to the system. + The `do` block in the example implicitly constructs said function inline. For exposition, we use the full version (e.g. `x, o, i, c`) but this could be simplified to merely `x`. The function `f` will be called with `observed` and `modified` `NamedTuple`s that are derived from their respective `NamedTuple` definitions. -In our example, if `furnace_on` is `false`, then the value of the `x` that's passed in as `modified` will be `(furnace_on = false)`. +In our example, if `furnace_on` is `false`, then the value of the `x` that's passed in as `modified` will be `(furnace_on = false)`. The modified values should be passed out in the same format: to set `furnace_on` to `true` we need to return a tuple `(furnace_on = true)`. We use Setfield to do this in the example, recreating the result tuple before returning it. -Accordingly, we can now interpret the `ImperativeAffect` definitions to mean that when `temp = furnace_off_threshold` we +Accordingly, we can now interpret the `ImperativeAffect` definitions to mean that when `temp = furnace_off_threshold` we will write `furnace_on = false` back to the system, and when `temp = furnace_on_threshold` we will write `furnace_on = true` back to the system. @@ -468,7 +478,8 @@ ss = structural_simplify(sys) prob = ODEProblem(ss, [temp => 0.0, furnace_on => true], (0.0, 10.0)) sol = solve(prob, Tsit5()) plot(sol) -hline!([sol.ps[furnace_off_threshold], sol.ps[furnace_on_threshold]], l = (:black, 1), primary = false) +hline!([sol.ps[furnace_off_threshold], sol.ps[furnace_on_threshold]], + l = (:black, 1), primary = false) ``` Here we see exactly the desired hysteresis. The heater starts on until the temperature hits @@ -477,71 +488,76 @@ point the furnace turns on again until `furnace_off_threshold` and so on and so is effectively regulating the temperature of the plant. ### [Quadrature Encoder](@id quadrature) + For a more complex application we'll look at modeling a quadrature encoder attached to a shaft spinning at a constant speed. Traditionally, a quadrature encoder is built out of a code wheel that interrupts the sensors at constant intervals and two sensors slightly out of phase with one another. A state machine can take the pattern of pulses produced by the two sensors and determine the number of steps that the shaft has spun. The state machine takes the new value from each sensor and the old values and decodes them into the direction that the wheel has spun in this step. ```@example events - @variables theta(t) omega(t) - params = @parameters qA=0 qB=0 hA=0 hB=0 cnt::Int=0 - eqs = [D(theta) ~ omega - omega ~ 1.0] +@variables theta(t) omega(t) +params = @parameters qA=0 qB=0 hA=0 hB=0 cnt::Int=0 +eqs = [D(theta) ~ omega + omega ~ 1.0] ``` + Our continous-time system is extremely simple. We have two states, `theta` for the angle of the shaft -and `omega` for the rate at which it's spinning. We then have parameters for the state machine `qA, qB, hA, hB` +and `omega` for the rate at which it's spinning. We then have parameters for the state machine `qA, qB, hA, hB` and a step count `cnt`. We'll then implement the decoder as a simple Julia function. + ```@example events - function decoder(oldA, oldB, newA, newB) - state = (oldA, oldB, newA, newB) - if state == (0, 0, 1, 0) || state == (1, 0, 1, 1) || state == (1, 1, 0, 1) || - state == (0, 1, 0, 0) - return 1 - elseif state == (0, 0, 0, 1) || state == (0, 1, 1, 1) || state == (1, 1, 1, 0) || - state == (1, 0, 0, 0) - return -1 - elseif state == (0, 0, 0, 0) || state == (0, 1, 0, 1) || state == (1, 0, 1, 0) || - state == (1, 1, 1, 1) - return 0 - else - return 0 # err is interpreted as no movement - end +function decoder(oldA, oldB, newA, newB) + state = (oldA, oldB, newA, newB) + if state == (0, 0, 1, 0) || state == (1, 0, 1, 1) || state == (1, 1, 0, 1) || + state == (0, 1, 0, 0) + return 1 + elseif state == (0, 0, 0, 1) || state == (0, 1, 1, 1) || state == (1, 1, 1, 0) || + state == (1, 0, 0, 0) + return -1 + elseif state == (0, 0, 0, 0) || state == (0, 1, 0, 1) || state == (1, 0, 1, 0) || + state == (1, 1, 1, 1) + return 0 + else + return 0 # err is interpreted as no movement end +end ``` + Based on the current and old state, this function will return 1 if the wheel spun in the positive direction, -1 if in the negative, and 0 otherwise. -The encoder state advances when the occlusion begins or ends. We model the +The encoder state advances when the occlusion begins or ends. We model the code wheel as simply detecting when `cos(100*theta)` is 0; if we're at a positive edge of the 0 crossing, then we interpret that as occlusion (so the discrete `qA` goes to 1). Otherwise, if `cos` is going negative, we interpret that as lack of occlusion (so the discrete goes to 0). The decoder function is then invoked to update the count with this new information. We can implement this in one of two ways: using edge sign detection or right root finding. For exposition, we -will implement each sensor differently. +will implement each sensor differently. For sensor A, we're using the edge detction method. By providing a different affect to `SymbolicContinuousCallback`'s `affect_neg` argument, we can specify different behaviour for the negative crossing vs. the positive crossing of the root. In our encoder, we interpret this as occlusion or nonocclusion of the sensor, update the internal state, and tick the decoder. + ```@example events - qAevt = ModelingToolkit.SymbolicContinuousCallback([cos(100 * theta) ~ 0], - ModelingToolkit.ImperativeAffect((; qA, hA, hB, cnt), (; qB)) do x, o, i, c - @set! x.hA = x.qA - @set! x.hB = o.qB - @set! x.qA = 1 - @set! x.cnt += decoder(x.hA, x.hB, x.qA, o.qB) - x - end, - affect_neg = ModelingToolkit.ImperativeAffect( - (; qA, hA, hB, cnt), (; qB)) do x, o, c, i - @set! x.hA = x.qA - @set! x.hB = o.qB - @set! x.qA = 0 - @set! x.cnt += decoder(x.hA, x.hB, x.qA, o.qB) - x - end) +qAevt = ModelingToolkit.SymbolicContinuousCallback([cos(100 * theta) ~ 0], + ModelingToolkit.ImperativeAffect((; qA, hA, hB, cnt), (; qB)) do x, o, i, c + @set! x.hA = x.qA + @set! x.hB = o.qB + @set! x.qA = 1 + @set! x.cnt += decoder(x.hA, x.hB, x.qA, o.qB) + x + end, + affect_neg = ModelingToolkit.ImperativeAffect( + (; qA, hA, hB, cnt), (; qB)) do x, o, c, i + @set! x.hA = x.qA + @set! x.hB = o.qB + @set! x.qA = 0 + @set! x.cnt += decoder(x.hA, x.hB, x.qA, o.qB) + x + end) ``` The other way we can implement a sensor is by changing the root find. @@ -550,29 +566,32 @@ the root is crossed. This makes it trickier to figure out what the new state is. Instead, we can use right root finding: ```@example events - qBevt = ModelingToolkit.SymbolicContinuousCallback([cos(100 * theta - π / 2) ~ 0], - ModelingToolkit.ImperativeAffect((; qB, hA, hB, cnt), (; qA, theta)) do x, o, i, c - @set! x.hA = o.qA - @set! x.hB = x.qB - @set! x.qB = clamp(sign(cos(100 * o.theta - π / 2)), 0.0, 1.0) - @set! x.cnt += decoder(x.hA, x.hB, o.qA, x.qB) - x - end; rootfind = SciMLBase.RightRootFind) +qBevt = ModelingToolkit.SymbolicContinuousCallback([cos(100 * theta - π / 2) ~ 0], + ModelingToolkit.ImperativeAffect((; qB, hA, hB, cnt), (; qA, theta)) do x, o, i, c + @set! x.hA = o.qA + @set! x.hB = x.qB + @set! x.qB = clamp(sign(cos(100 * o.theta - π / 2)), 0.0, 1.0) + @set! x.cnt += decoder(x.hA, x.hB, o.qA, x.qB) + x + end; rootfind = SciMLBase.RightRootFind) ``` -Here, sensor B is located `π / 2` behind sensor A in angular space, so we're adjusting our + +Here, sensor B is located `π / 2` behind sensor A in angular space, so we're adjusting our trigger function accordingly. We here ask for right root finding on the callback, so we know -that the value of said function will have the "new" sign rather than the old one. Thus, we can +that the value of said function will have the "new" sign rather than the old one. Thus, we can determine the new state of the sensor from the sign of the indicator function evaluated at the affect activation point, with -1 mapped to 0. We can now simulate the encoder. + ```@example events - @named sys = ODESystem( - eqs, t, [theta, omega], params; continuous_events = [qAevt, qBevt]) - ss = structural_simplify(sys) - prob = ODEProblem(ss, [theta => 0.0], (0.0, pi)) - sol = solve(prob, Tsit5(); dtmax = 0.01) - sol.ps[cnt] +@named sys = ODESystem( + eqs, t, [theta, omega], params; continuous_events = [qAevt, qBevt]) +ss = structural_simplify(sys) +prob = ODEProblem(ss, [theta => 0.0], (0.0, pi)) +sol = solve(prob, Tsit5(); dtmax = 0.01) +sol.ps[cnt] ``` + `cos(100*theta)` will have 200 crossings in the half rotation we've gone through, so the encoder would notionally count 200 steps. -Our encoder counts 198 steps (it loses one step to initialization and one step due to the final state falling squarely on an edge). \ No newline at end of file +Our encoder counts 198 steps (it loses one step to initialization and one step due to the final state falling squarely on an edge). diff --git a/src/systems/diffeqs/odesystem.jl b/src/systems/diffeqs/odesystem.jl index e99911eec6..1cc8273b4d 100644 --- a/src/systems/diffeqs/odesystem.jl +++ b/src/systems/diffeqs/odesystem.jl @@ -444,7 +444,7 @@ function build_explicit_observed_function(sys, ts; param_only = false, op = Operator, throw = true, - array_type=:array) + array_type = :array) if (isscalar = symbolic_type(ts) !== NotSymbolic()) ts = [ts] end @@ -589,10 +589,12 @@ function build_explicit_observed_function(sys, ts; oop_mtkp_wrapper = mtkparams_wrapper end - output_expr = isscalar ? ts[1] : (array_type == :array ? MakeArray(ts, output_type) : MakeTuple(ts)) + output_expr = isscalar ? ts[1] : + (array_type == :array ? MakeArray(ts, output_type) : MakeTuple(ts)) # Need to keep old method of building the function since it uses `output_type`, # which can't be provided to `build_function` - oop_fn = Func(args, [], pre(Let(obsexprs, output_expr, false))) |> array_wrapper[1] |> oop_mtkp_wrapper |> toexpr + oop_fn = Func(args, [], pre(Let(obsexprs, output_expr, false))) |> array_wrapper[1] |> + oop_mtkp_wrapper |> toexpr oop_fn = expression ? oop_fn : eval_or_rgf(oop_fn; eval_expression, eval_module) if !isscalar From 711fb8c654be38ce7377b85f3025ee7934cd5de8 Mon Sep 17 00:00:00 2001 From: Benjamin Chung Date: Tue, 22 Oct 2024 18:44:45 -0700 Subject: [PATCH 0221/2176] Spelling --- docs/src/basics/Events.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/docs/src/basics/Events.md b/docs/src/basics/Events.md index 4a59149308..8088e0f52a 100644 --- a/docs/src/basics/Events.md +++ b/docs/src/basics/Events.md @@ -388,9 +388,9 @@ system, simplifying the definitions and (in the future) allowing assignments bac by solving the nonlinear reinitialization problem afterwards. We will use two examples to describe `ImperativeAffect`: a simple heater and a quadrature encoder. -These examples will also demonstrate advanced usage of `ModelingToolkit.SymbolicContinousCallback`, +These examples will also demonstrate advanced usage of `ModelingToolkit.SymbolicContinuousCallback`, the low-level interface that the aforementioned tuple form converts into and allows control over the -exact SciMLCallbacks event that is generated for a continous event. +exact SciMLCallbacks event that is generated for a continuous event. ### [Heater](@id heater_events) @@ -405,7 +405,7 @@ eqs = [ ``` Our plant is simple. We have a heater that's turned on and off by the clocked parameter `furnace_on` -which adds `furnace_power` forcing to the system when enabled. We then leak heat porportional to `leakage` +which adds `furnace_power` forcing to the system when enabled. We then leak heat proportional to `leakage` as a function of the square of the current temperature. We need a controller with hysteresis to conol the plant. We wish the furnace to turn on when the temperature @@ -537,7 +537,7 @@ then invoked to update the count with this new information. We can implement this in one of two ways: using edge sign detection or right root finding. For exposition, we will implement each sensor differently. -For sensor A, we're using the edge detction method. By providing a different affect to `SymbolicContinuousCallback`'s +For sensor A, we're using the edge detection method. By providing a different affect to `SymbolicContinuousCallback`'s `affect_neg` argument, we can specify different behaviour for the negative crossing vs. the positive crossing of the root. In our encoder, we interpret this as occlusion or nonocclusion of the sensor, update the internal state, and tick the decoder. From a8ea3698e71afe91dd9040a6fb80bab22c7d6c95 Mon Sep 17 00:00:00 2001 From: Benjamin Chung Date: Tue, 22 Oct 2024 18:46:54 -0700 Subject: [PATCH 0222/2176] Spelling (2) --- docs/src/basics/Events.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/src/basics/Events.md b/docs/src/basics/Events.md index 8088e0f52a..e583af8d9e 100644 --- a/docs/src/basics/Events.md +++ b/docs/src/basics/Events.md @@ -410,7 +410,7 @@ as a function of the square of the current temperature. We need a controller with hysteresis to conol the plant. We wish the furnace to turn on when the temperature is below `furnace_on_threshold` and off when above `furnace_off_threshold`, while maintaining its current state -in between. To do this, we create two continous callbacks: +in between. To do this, we create two continuous callbacks: ```@example events using Setfield @@ -501,7 +501,7 @@ eqs = [D(theta) ~ omega omega ~ 1.0] ``` -Our continous-time system is extremely simple. We have two states, `theta` for the angle of the shaft +Our continuous-time system is extremely simple. We have two states, `theta` for the angle of the shaft and `omega` for the rate at which it's spinning. We then have parameters for the state machine `qA, qB, hA, hB` and a step count `cnt`. From 6a304e7d7f8960d7f2cc63d1be93d8f41f6db824 Mon Sep 17 00:00:00 2001 From: Benjamin Chung Date: Tue, 22 Oct 2024 18:48:15 -0700 Subject: [PATCH 0223/2176] Remove debug RGF shim --- test/symbolic_events.jl | 17 +---------------- 1 file changed, 1 insertion(+), 16 deletions(-) diff --git a/test/symbolic_events.jl b/test/symbolic_events.jl index f4f97fb2b9..96a57ec40e 100644 --- a/test/symbolic_events.jl +++ b/test/symbolic_events.jl @@ -1217,19 +1217,4 @@ end prob = ODEProblem(ss, [theta => 0.0], (0.0, pi)) sol = solve(prob, Tsit5(); dtmax = 0.01) @test getp(sol, cnt)(sol) == 197 # we get 2 pulses per phase cycle (cos 0 crossing) and we go to 100 cycles; we miss a few due to the initial state -end - -import RuntimeGeneratedFunctions -function (f::RuntimeGeneratedFunctions.RuntimeGeneratedFunction{ - argnames, cache_tag, context_tag, - id})(args::Vararg{Any, N}) where {N, argnames, cache_tag, context_tag, id} - try - RuntimeGeneratedFunctions.generated_callfunc(f, args...) - catch e - @error "Caught error in RuntimeGeneratedFunction; source code follows" - func_expr = Expr(:->, Expr(:tuple, argnames...), - RuntimeGeneratedFunctions._lookup_body(cache_tag, id)) - @show func_expr - rethrow(e) - end -end +end \ No newline at end of file From 35ec1c59b31ab3d0c6a421482d7e48459a00111c Mon Sep 17 00:00:00 2001 From: Benjamin Chung Date: Tue, 22 Oct 2024 18:50:32 -0700 Subject: [PATCH 0224/2176] Formatter (3) --- test/symbolic_events.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/symbolic_events.jl b/test/symbolic_events.jl index 96a57ec40e..c021f99eea 100644 --- a/test/symbolic_events.jl +++ b/test/symbolic_events.jl @@ -1217,4 +1217,4 @@ end prob = ODEProblem(ss, [theta => 0.0], (0.0, pi)) sol = solve(prob, Tsit5(); dtmax = 0.01) @test getp(sol, cnt)(sol) == 197 # we get 2 pulses per phase cycle (cos 0 crossing) and we go to 100 cycles; we miss a few due to the initial state -end \ No newline at end of file +end From 4c3d6fcc1eaedbec33cfd3f979a958110ff4eace Mon Sep 17 00:00:00 2001 From: Christopher Rackauckas Date: Wed, 23 Oct 2024 06:21:05 +0000 Subject: [PATCH 0225/2176] Update docs/src/basics/Events.md Co-authored-by: Fredrik Bagge Carlson --- docs/src/basics/Events.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/src/basics/Events.md b/docs/src/basics/Events.md index e583af8d9e..41fa96d619 100644 --- a/docs/src/basics/Events.md +++ b/docs/src/basics/Events.md @@ -394,7 +394,7 @@ exact SciMLCallbacks event that is generated for a continuous event. ### [Heater](@id heater_events) -Bang-bang control of a heater connected to a leaky plant requires hysteresis in order to prevent control oscillation. +Bang-bang control of a heater connected to a leaky plant requires hysteresis in order to prevent rapid control oscillation. ```@example events @variables temp(t) From cdddeb9f62ffcd12357dc96648167fe8514993be Mon Sep 17 00:00:00 2001 From: Christopher Rackauckas Date: Wed, 23 Oct 2024 06:21:34 +0000 Subject: [PATCH 0226/2176] Update docs/src/basics/Events.md Co-authored-by: Fredrik Bagge Carlson --- docs/src/basics/Events.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/src/basics/Events.md b/docs/src/basics/Events.md index 41fa96d619..add870b7f3 100644 --- a/docs/src/basics/Events.md +++ b/docs/src/basics/Events.md @@ -408,7 +408,7 @@ Our plant is simple. We have a heater that's turned on and off by the clocked pa which adds `furnace_power` forcing to the system when enabled. We then leak heat proportional to `leakage` as a function of the square of the current temperature. -We need a controller with hysteresis to conol the plant. We wish the furnace to turn on when the temperature +We need a controller with hysteresis to control the plant. We wish the furnace to turn on when the temperature is below `furnace_on_threshold` and off when above `furnace_off_threshold`, while maintaining its current state in between. To do this, we create two continuous callbacks: From 2ac2d230664916fdd0151cb6257adbf9dd7b2ccb Mon Sep 17 00:00:00 2001 From: Christopher Rackauckas Date: Wed, 23 Oct 2024 06:21:54 +0000 Subject: [PATCH 0227/2176] Update docs/src/basics/Events.md Co-authored-by: Fredrik Bagge Carlson --- docs/src/basics/Events.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/src/basics/Events.md b/docs/src/basics/Events.md index add870b7f3..0d9149e23c 100644 --- a/docs/src/basics/Events.md +++ b/docs/src/basics/Events.md @@ -561,7 +561,7 @@ qAevt = ModelingToolkit.SymbolicContinuousCallback([cos(100 * theta) ~ 0], ``` The other way we can implement a sensor is by changing the root find. -Normally, we use left root finding; the affect will be invoked instantaneously before +Normally, we use left root finding; the affect will be invoked instantaneously _before_ the root is crossed. This makes it trickier to figure out what the new state is. Instead, we can use right root finding: From d11d98d7f6788f771cb4a580df742073950ef35c Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 23 Oct 2024 16:13:57 +0530 Subject: [PATCH 0228/2176] fix: fix `linearization_function` not handling dependent parameter mapping --- src/systems/abstractsystem.jl | 1 + test/downstream/linearize.jl | 6 ++++++ 2 files changed, 7 insertions(+) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index b6e2a79c3e..ada8a2a056 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -2458,6 +2458,7 @@ function linearization_function(sys::AbstractSystem, inputs, newps = deepcopy(sys_ps) for (k, v) in p if is_parameter(sys, k) + v = fixpoint_sub(v, p) setp(sys, k)(newps, v) end end diff --git a/test/downstream/linearize.jl b/test/downstream/linearize.jl index d12d1ffa81..dde56d9bb1 100644 --- a/test/downstream/linearize.jl +++ b/test/downstream/linearize.jl @@ -307,6 +307,12 @@ matrices = linfun([1.0], Dict(p => 3.0), 1.0) # this would be 1 if the parameter value isn't respected @test matrices.f_u[] == 3.0 +@testset "linearization_function handles dependent values" begin + @parameters q + matrices = @test_nowarn linfun([1.0], Dict(p => 3q, q => 1.0), 1.0) + @test matrices.f_u[] == 3.0 +end + @testset "Issue #2941" begin @variables x(t) y(t) @parameters p From bbda4123e450eb2c9a9c4e4cf17b48a479d0c920 Mon Sep 17 00:00:00 2001 From: Benjamin Chung Date: Tue, 10 Sep 2024 12:45:14 -0700 Subject: [PATCH 0229/2176] Add support for the initializealg argument in SciMLBase callbacks --- src/systems/callbacks.jl | 54 ++++++++++++----- test/symbolic_events.jl | 121 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 160 insertions(+), 15 deletions(-) diff --git a/src/systems/callbacks.jl b/src/systems/callbacks.jl index 86cab57634..617475d536 100644 --- a/src/systems/callbacks.jl +++ b/src/systems/callbacks.jl @@ -106,15 +106,25 @@ Affects (i.e. `affect` and `affect_neg`) can be specified as either: + `read_parameters` is a vector of the parameters that are *used* by `f!`. Their indices are passed to `f` in `p` similarly to the indices of `unknowns` passed in `u`. + `modified_parameters` is a vector of the parameters that are *modified* by `f!`. Note that a parameter will not appear in `p` if it only appears in `modified_parameters`; it must appear in both `parameters` and `modified_parameters` if it is used in the affect definition. + `ctx` is a user-defined context object passed to `f!` when invoked. This value is aliased for each problem. + +Callbacks that impact a DAE are applied, then the DAE is reinitialized using `reinitializealg` (which defaults to `SciMLBase.CheckInit`). +This reinitialization algorithm ensures that the DAE is satisfied after the callback runs. The default value of `CheckInit` will simply validate +that the newly-assigned values indeed satisfy the algebraic system; see the documentation on DAE initialization for a more detailed discussion of +initialization. """ struct SymbolicContinuousCallback eqs::Vector{Equation} affect::Union{Vector{Equation}, FunctionalAffect} affect_neg::Union{Vector{Equation}, FunctionalAffect, Nothing} rootfind::SciMLBase.RootfindOpt - function SymbolicContinuousCallback(; eqs::Vector{Equation}, affect = NULL_AFFECT, - affect_neg = affect, rootfind = SciMLBase.LeftRootFind) - new(eqs, make_affect(affect), make_affect(affect_neg), rootfind) + reinitializealg::SciMLBase.DAEInitializationAlgorithm + function SymbolicContinuousCallback(; + eqs::Vector{Equation}, + affect = NULL_AFFECT, + affect_neg = affect, + rootfind = SciMLBase.LeftRootFind, + reinitializealg=SciMLBase.CheckInit()) + new(eqs, make_affect(affect), make_affect(affect_neg), rootfind, reinitializealg) end # Default affect to nothing end make_affect(affect) = affect @@ -183,6 +193,10 @@ function affect_negs(cbs::Vector{SymbolicContinuousCallback}) mapreduce(affect_negs, vcat, cbs, init = Equation[]) end +reinitialization_alg(cb::SymbolicContinuousCallback) = cb.reinitializealg +reinitialization_algs(cbs::Vector{SymbolicContinuousCallback}) = + mapreduce(reinitialization_alg, vcat, cbs, init = SciMLBase.DAEInitializationAlgorithm[]) + namespace_affects(af::Vector, s) = Equation[namespace_affect(a, s) for a in af] namespace_affects(af::FunctionalAffect, s) = namespace_affect(af, s) namespace_affects(::Nothing, s) = nothing @@ -225,11 +239,12 @@ struct SymbolicDiscreteCallback # TODO: Iterative condition::Any affects::Any + reinitializealg::SciMLBase.DAEInitializationAlgorithm - function SymbolicDiscreteCallback(condition, affects = NULL_AFFECT) + function SymbolicDiscreteCallback(condition, affects = NULL_AFFECT, reinitializealg=SciMLBase.CheckInit()) c = scalarize_condition(condition) a = scalarize_affects(affects) - new(c, a) + new(c, a, reinitializealg) end # Default affect to nothing end @@ -286,6 +301,10 @@ function affects(cbs::Vector{SymbolicDiscreteCallback}) reduce(vcat, affects(cb) for cb in cbs; init = []) end +reinitialization_alg(cb::SymbolicDiscreteCallback) = cb.reinitializealg +reinitialization_algs(cbs::Vector{SymbolicDiscreteCallback}) = + mapreduce(reinitialization_alg, vcat, cbs, init = SciMLBase.DAEInitializationAlgorithm[]) + function namespace_callback(cb::SymbolicDiscreteCallback, s)::SymbolicDiscreteCallback af = affects(cb) af = af isa AbstractVector ? namespace_affect.(af, Ref(s)) : namespace_affect(af, s) @@ -579,13 +598,14 @@ function generate_single_rootfinding_callback( initfn = SciMLBase.INITIALIZE_DEFAULT end return ContinuousCallback( - cond, affect_function.affect, affect_function.affect_neg, - rootfind = cb.rootfind, initialize = initfn) + cond, affect_function.affect, affect_function.affect_neg, rootfind = cb.rootfind, + initialize = initfn, + initializealg = reinitialization_alg(cb)) end function generate_vector_rootfinding_callback( cbs, sys::AbstractODESystem, dvs = unknowns(sys), - ps = parameters(sys); rootfind = SciMLBase.RightRootFind, kwargs...) + ps = parameters(sys); rootfind = SciMLBase.RightRootFind, reinitialization = SciMLBase.CheckInit(), kwargs...) eqs = map(cb -> flatten_equations(cb.eqs), cbs) num_eqs = length.(eqs) # fuse equations to create VectorContinuousCallback @@ -650,7 +670,7 @@ function generate_vector_rootfinding_callback( initfn = SciMLBase.INITIALIZE_DEFAULT end return VectorContinuousCallback( - cond, affect, affect_neg, length(eqs), rootfind = rootfind, initialize = initfn) + cond, affect, affect_neg, length(eqs), rootfind = rootfind, initialize = initfn, initializealg = reinitialization) end """ @@ -690,10 +710,14 @@ function generate_rootfinding_callback(cbs, sys::AbstractODESystem, dvs = unknow # group the cbs by what rootfind op they use # groupby would be very useful here, but alas cb_classes = Dict{ - @NamedTuple{rootfind::SciMLBase.RootfindOpt}, Vector{SymbolicContinuousCallback}}() + @NamedTuple{ + rootfind::SciMLBase.RootfindOpt, + reinitialization::SciMLBase.DAEInitializationAlgorithm}, Vector{SymbolicContinuousCallback}}() for cb in cbs push!( - get!(() -> SymbolicContinuousCallback[], cb_classes, (rootfind = cb.rootfind,)), + get!(() -> SymbolicContinuousCallback[], cb_classes, ( + rootfind = cb.rootfind, + reinitialization = reinitialization_alg(cb))), cb) end @@ -701,7 +725,7 @@ function generate_rootfinding_callback(cbs, sys::AbstractODESystem, dvs = unknow compiled_callbacks = map(collect(pairs(sort!( OrderedDict(cb_classes); by = p -> p.rootfind)))) do (equiv_class, cbs_in_class) return generate_vector_rootfinding_callback( - cbs_in_class, sys, dvs, ps; rootfind = equiv_class.rootfind, kwargs...) + cbs_in_class, sys, dvs, ps; rootfind = equiv_class.rootfind, reinitialization=equiv_class.reinitialization, kwargs...) end if length(compiled_callbacks) == 1 return compiled_callbacks[] @@ -772,10 +796,10 @@ function generate_timed_callback(cb, sys, dvs, ps; postprocess_affect_expr! = no end if cond isa AbstractVector # Preset Time - return PresetTimeCallback(cond, as; initialize = initfn) + return PresetTimeCallback(cond, as; initialize = initfn, initializealg=reinitialization_alg(cb)) else # Periodic - return PeriodicCallback(as, cond; initialize = initfn) + return PeriodicCallback(as, cond; initialize = initfn, initializealg=reinitialization_alg(cb)) end end @@ -800,7 +824,7 @@ function generate_discrete_callback(cb, sys, dvs, ps; postprocess_affect_expr! = else initfn = SciMLBase.INITIALIZE_DEFAULT end - return DiscreteCallback(c, as; initialize = initfn) + return DiscreteCallback(c, as; initialize = initfn, initializealg=reinitialization_alg(cb)) end end diff --git a/test/symbolic_events.jl b/test/symbolic_events.jl index e1d12814ef..7084ab5b96 100644 --- a/test/symbolic_events.jl +++ b/test/symbolic_events.jl @@ -867,6 +867,108 @@ end @test sign.(cos.(3 * (required_crossings_c2 .+ 1e-6))) == sign.(last.(cr2)) end +@testset "Discrete variable timeseries" begin + @variables x(t) + @parameters a(t) b(t) c(t) + cb1 = [x ~ 1.0] => [a ~ -a] + function save_affect!(integ, u, p, ctx) + integ.ps[p.b] = 5.0 + end + cb2 = [x ~ 0.5] => (save_affect!, [], [b], [b], nothing) + cb3 = 1.0 => [c ~ t] + + @mtkbuild sys = ODESystem(D(x) ~ cos(t), t, [x], [a, b, c]; + continuous_events = [cb1, cb2], discrete_events = [cb3]) + prob = ODEProblem(sys, [x => 1.0], (0.0, 2pi), [a => 1.0, b => 2.0, c => 0.0]) + @test sort(canonicalize(Discrete(), prob.p)[1]) == [0.0, 1.0, 2.0] + sol = solve(prob, Tsit5()) + + @test sol[a] == [-1.0] + @test sol[b] == [5.0, 5.0] + @test sol[c] == [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0] +end + +@testset "Discrete event reinitialization (#3142)" begin + @connector LiquidPort begin + p(t)::Float64, [ description = "Set pressure in bar", + guess = 1.01325] + Vdot(t)::Float64, [ description = "Volume flow rate in L/min", + guess = 0.0, + connect = Flow] + end + + @mtkmodel PressureSource begin + @components begin + port = LiquidPort() + end + @parameters begin + p_set::Float64 = 1.01325, [description = "Set pressure in bar"] + end + @equations begin + port.p ~ p_set + end + end + + @mtkmodel BinaryValve begin + @constants begin + p_ref::Float64 = 1.0, [description = "Reference pressure drop in bar"] + ρ_ref::Float64 = 1000.0, [description = "Reference density in kg/m^3"] + end + @components begin + port_in = LiquidPort() + port_out = LiquidPort() + end + @parameters begin + k_V::Float64 = 1.0, [description = "Valve coefficient in L/min/bar"] + k_leakage::Float64 = 1e-08, [description = "Leakage coefficient in L/min/bar"] + ρ::Float64 = 1000.0, [description = "Density in kg/m^3"] + end + @variables begin + S(t)::Float64, [description = "Valve state", guess = 1.0, irreducible = true] + Δp(t)::Float64, [description = "Pressure difference in bar", guess = 1.0] + Vdot(t)::Float64, [description = "Volume flow rate in L/min", guess = 1.0] + end + @equations begin + # Port handling + port_in.Vdot ~ -Vdot + port_out.Vdot ~ Vdot + Δp ~ port_in.p - port_out.p + # System behavior + D(S) ~ 0.0 + Vdot ~ S*k_V*sign(Δp)*sqrt(abs(Δp)/p_ref * ρ_ref/ρ) + k_leakage*Δp # softplus alpha function to avoid negative values under the sqrt + end + end + + # Test System + @mtkmodel TestSystem begin + @components begin + pressure_source_1 = PressureSource(p_set = 2.0) + binary_valve_1 = BinaryValve(S = 1.0, k_leakage=0.0) + binary_valve_2 = BinaryValve(S = 1.0, k_leakage=0.0) + pressure_source_2 = PressureSource(p_set = 1.0) + end + @equations begin + connect(pressure_source_1.port, binary_valve_1.port_in) + connect(binary_valve_1.port_out, binary_valve_2.port_in) + connect(binary_valve_2.port_out, pressure_source_2.port) + end + @discrete_events begin + [30] => [binary_valve_1.S ~ 0.0, binary_valve_2.Δp ~ 0.0 ] + [60] => [binary_valve_1.S ~ 1.0, binary_valve_2.S ~ 0.0, binary_valve_2.Δp ~ 1.0 ] + [120] => [binary_valve_1.S ~ 0.0, binary_valve_2.Δp ~ 0.0 ] + end + end + + # Test Simulation + @mtkbuild sys = TestSystem() + + # Test Simulation + prob = ODEProblem(sys, [], (0.0, 150.0)) + sol = solve(prob) + @test sol[end] == [0.0, 0.0, 0.0] +end + + @testset "Discrete variable timeseries" begin @variables x(t) @parameters a(t) b(t) c(t) @@ -887,3 +989,22 @@ end @test sol[b] == [2.0, 5.0, 5.0] @test sol[c] == [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0] end + +@testset "Bump" begin + @variables x(t) [irreducible=true] y(t) [irreducible=true] + eqs = [x ~ y, D(x) ~ -1] + cb = [x ~ 0.0] => [x ~ 0, y ~ 1] + @mtkbuild pend = ODESystem(eqs, t;continuous_events = [cb]) + prob = ODEProblem(pend, [x => 1], (0.0, 3.0), guesses = [y => x]) + @test_throws "initialization not satisifed" solve(prob, Rodas5()) + + cb = [x ~ 0.0] => [y ~ 1] + @mtkbuild pend = ODESystem(eqs, t;continuous_events = [cb]) + prob = ODEProblem(pend, [x => 1], (0.0, 3.0), guesses = [y => x]) + @test_broken !SciMLBase.successful_retcode(solve(prob, Rodas5())) + + cb = [x ~ 0.0] => [x ~ 1, y ~ 1] + @mtkbuild pend = ODESystem(eqs, t;continuous_events = [cb]) + prob = ODEProblem(pend, [x => 1], (0.0, 3.0), guesses = [y => x]) + @test all(≈(0.0; atol=1e-9), solve(prob, Rodas5())[[x,y]][end]) +end \ No newline at end of file From afbf1884399cd2921e3a7551131125a5b6b20362 Mon Sep 17 00:00:00 2001 From: Benjamin Chung Date: Wed, 23 Oct 2024 03:49:31 -0700 Subject: [PATCH 0230/2176] Run formatter --- src/systems/callbacks.jl | 56 ++++++++++++++++++++++++---------------- test/symbolic_events.jl | 51 ++++++++++++++++++------------------ 2 files changed, 60 insertions(+), 47 deletions(-) diff --git a/src/systems/callbacks.jl b/src/systems/callbacks.jl index 617475d536..bca98c5c83 100644 --- a/src/systems/callbacks.jl +++ b/src/systems/callbacks.jl @@ -118,12 +118,12 @@ struct SymbolicContinuousCallback affect_neg::Union{Vector{Equation}, FunctionalAffect, Nothing} rootfind::SciMLBase.RootfindOpt reinitializealg::SciMLBase.DAEInitializationAlgorithm - function SymbolicContinuousCallback(; - eqs::Vector{Equation}, - affect = NULL_AFFECT, - affect_neg = affect, - rootfind = SciMLBase.LeftRootFind, - reinitializealg=SciMLBase.CheckInit()) + function SymbolicContinuousCallback(; + eqs::Vector{Equation}, + affect = NULL_AFFECT, + affect_neg = affect, + rootfind = SciMLBase.LeftRootFind, + reinitializealg = SciMLBase.CheckInit()) new(eqs, make_affect(affect), make_affect(affect_neg), rootfind, reinitializealg) end # Default affect to nothing end @@ -194,8 +194,10 @@ function affect_negs(cbs::Vector{SymbolicContinuousCallback}) end reinitialization_alg(cb::SymbolicContinuousCallback) = cb.reinitializealg -reinitialization_algs(cbs::Vector{SymbolicContinuousCallback}) = - mapreduce(reinitialization_alg, vcat, cbs, init = SciMLBase.DAEInitializationAlgorithm[]) +function reinitialization_algs(cbs::Vector{SymbolicContinuousCallback}) + mapreduce( + reinitialization_alg, vcat, cbs, init = SciMLBase.DAEInitializationAlgorithm[]) +end namespace_affects(af::Vector, s) = Equation[namespace_affect(a, s) for a in af] namespace_affects(af::FunctionalAffect, s) = namespace_affect(af, s) @@ -241,7 +243,8 @@ struct SymbolicDiscreteCallback affects::Any reinitializealg::SciMLBase.DAEInitializationAlgorithm - function SymbolicDiscreteCallback(condition, affects = NULL_AFFECT, reinitializealg=SciMLBase.CheckInit()) + function SymbolicDiscreteCallback( + condition, affects = NULL_AFFECT, reinitializealg = SciMLBase.CheckInit()) c = scalarize_condition(condition) a = scalarize_affects(affects) new(c, a, reinitializealg) @@ -302,8 +305,10 @@ function affects(cbs::Vector{SymbolicDiscreteCallback}) end reinitialization_alg(cb::SymbolicDiscreteCallback) = cb.reinitializealg -reinitialization_algs(cbs::Vector{SymbolicDiscreteCallback}) = - mapreduce(reinitialization_alg, vcat, cbs, init = SciMLBase.DAEInitializationAlgorithm[]) +function reinitialization_algs(cbs::Vector{SymbolicDiscreteCallback}) + mapreduce( + reinitialization_alg, vcat, cbs, init = SciMLBase.DAEInitializationAlgorithm[]) +end function namespace_callback(cb::SymbolicDiscreteCallback, s)::SymbolicDiscreteCallback af = affects(cb) @@ -598,14 +603,15 @@ function generate_single_rootfinding_callback( initfn = SciMLBase.INITIALIZE_DEFAULT end return ContinuousCallback( - cond, affect_function.affect, affect_function.affect_neg, rootfind = cb.rootfind, + cond, affect_function.affect, affect_function.affect_neg, rootfind = cb.rootfind, initialize = initfn, initializealg = reinitialization_alg(cb)) end function generate_vector_rootfinding_callback( cbs, sys::AbstractODESystem, dvs = unknowns(sys), - ps = parameters(sys); rootfind = SciMLBase.RightRootFind, reinitialization = SciMLBase.CheckInit(), kwargs...) + ps = parameters(sys); rootfind = SciMLBase.RightRootFind, + reinitialization = SciMLBase.CheckInit(), kwargs...) eqs = map(cb -> flatten_equations(cb.eqs), cbs) num_eqs = length.(eqs) # fuse equations to create VectorContinuousCallback @@ -670,7 +676,8 @@ function generate_vector_rootfinding_callback( initfn = SciMLBase.INITIALIZE_DEFAULT end return VectorContinuousCallback( - cond, affect, affect_neg, length(eqs), rootfind = rootfind, initialize = initfn, initializealg = reinitialization) + cond, affect, affect_neg, length(eqs), rootfind = rootfind, + initialize = initfn, initializealg = reinitialization) end """ @@ -711,13 +718,14 @@ function generate_rootfinding_callback(cbs, sys::AbstractODESystem, dvs = unknow # groupby would be very useful here, but alas cb_classes = Dict{ @NamedTuple{ - rootfind::SciMLBase.RootfindOpt, + rootfind::SciMLBase.RootfindOpt, reinitialization::SciMLBase.DAEInitializationAlgorithm}, Vector{SymbolicContinuousCallback}}() for cb in cbs push!( - get!(() -> SymbolicContinuousCallback[], cb_classes, ( - rootfind = cb.rootfind, - reinitialization = reinitialization_alg(cb))), + get!(() -> SymbolicContinuousCallback[], cb_classes, + ( + rootfind = cb.rootfind, + reinitialization = reinitialization_alg(cb))), cb) end @@ -725,7 +733,8 @@ function generate_rootfinding_callback(cbs, sys::AbstractODESystem, dvs = unknow compiled_callbacks = map(collect(pairs(sort!( OrderedDict(cb_classes); by = p -> p.rootfind)))) do (equiv_class, cbs_in_class) return generate_vector_rootfinding_callback( - cbs_in_class, sys, dvs, ps; rootfind = equiv_class.rootfind, reinitialization=equiv_class.reinitialization, kwargs...) + cbs_in_class, sys, dvs, ps; rootfind = equiv_class.rootfind, + reinitialization = equiv_class.reinitialization, kwargs...) end if length(compiled_callbacks) == 1 return compiled_callbacks[] @@ -796,10 +805,12 @@ function generate_timed_callback(cb, sys, dvs, ps; postprocess_affect_expr! = no end if cond isa AbstractVector # Preset Time - return PresetTimeCallback(cond, as; initialize = initfn, initializealg=reinitialization_alg(cb)) + return PresetTimeCallback( + cond, as; initialize = initfn, initializealg = reinitialization_alg(cb)) else # Periodic - return PeriodicCallback(as, cond; initialize = initfn, initializealg=reinitialization_alg(cb)) + return PeriodicCallback( + as, cond; initialize = initfn, initializealg = reinitialization_alg(cb)) end end @@ -824,7 +835,8 @@ function generate_discrete_callback(cb, sys, dvs, ps; postprocess_affect_expr! = else initfn = SciMLBase.INITIALIZE_DEFAULT end - return DiscreteCallback(c, as; initialize = initfn, initializealg=reinitialization_alg(cb)) + return DiscreteCallback( + c, as; initialize = initfn, initializealg = reinitialization_alg(cb)) end end diff --git a/test/symbolic_events.jl b/test/symbolic_events.jl index 7084ab5b96..998786fa67 100644 --- a/test/symbolic_events.jl +++ b/test/symbolic_events.jl @@ -890,13 +890,14 @@ end @testset "Discrete event reinitialization (#3142)" begin @connector LiquidPort begin - p(t)::Float64, [ description = "Set pressure in bar", - guess = 1.01325] - Vdot(t)::Float64, [ description = "Volume flow rate in L/min", - guess = 0.0, - connect = Flow] + p(t)::Float64, [description = "Set pressure in bar", + guess = 1.01325] + Vdot(t)::Float64, + [description = "Volume flow rate in L/min", + guess = 0.0, + connect = Flow] end - + @mtkmodel PressureSource begin @components begin port = LiquidPort() @@ -908,7 +909,7 @@ end port.p ~ p_set end end - + @mtkmodel BinaryValve begin @constants begin p_ref::Float64 = 1.0, [description = "Reference pressure drop in bar"] @@ -918,7 +919,7 @@ end port_in = LiquidPort() port_out = LiquidPort() end - @parameters begin + @parameters begin k_V::Float64 = 1.0, [description = "Valve coefficient in L/min/bar"] k_leakage::Float64 = 1e-08, [description = "Leakage coefficient in L/min/bar"] ρ::Float64 = 1000.0, [description = "Density in kg/m^3"] @@ -935,16 +936,16 @@ end Δp ~ port_in.p - port_out.p # System behavior D(S) ~ 0.0 - Vdot ~ S*k_V*sign(Δp)*sqrt(abs(Δp)/p_ref * ρ_ref/ρ) + k_leakage*Δp # softplus alpha function to avoid negative values under the sqrt + Vdot ~ S * k_V * sign(Δp) * sqrt(abs(Δp) / p_ref * ρ_ref / ρ) + k_leakage * Δp # softplus alpha function to avoid negative values under the sqrt end end - + # Test System @mtkmodel TestSystem begin @components begin pressure_source_1 = PressureSource(p_set = 2.0) - binary_valve_1 = BinaryValve(S = 1.0, k_leakage=0.0) - binary_valve_2 = BinaryValve(S = 1.0, k_leakage=0.0) + binary_valve_1 = BinaryValve(S = 1.0, k_leakage = 0.0) + binary_valve_2 = BinaryValve(S = 1.0, k_leakage = 0.0) pressure_source_2 = PressureSource(p_set = 1.0) end @equations begin @@ -953,22 +954,22 @@ end connect(binary_valve_2.port_out, pressure_source_2.port) end @discrete_events begin - [30] => [binary_valve_1.S ~ 0.0, binary_valve_2.Δp ~ 0.0 ] - [60] => [binary_valve_1.S ~ 1.0, binary_valve_2.S ~ 0.0, binary_valve_2.Δp ~ 1.0 ] - [120] => [binary_valve_1.S ~ 0.0, binary_valve_2.Δp ~ 0.0 ] + [30] => [binary_valve_1.S ~ 0.0, binary_valve_2.Δp ~ 0.0] + [60] => [ + binary_valve_1.S ~ 1.0, binary_valve_2.S ~ 0.0, binary_valve_2.Δp ~ 1.0] + [120] => [binary_valve_1.S ~ 0.0, binary_valve_2.Δp ~ 0.0] end end - + # Test Simulation @mtkbuild sys = TestSystem() - + # Test Simulation prob = ODEProblem(sys, [], (0.0, 150.0)) sol = solve(prob) @test sol[end] == [0.0, 0.0, 0.0] end - @testset "Discrete variable timeseries" begin @variables x(t) @parameters a(t) b(t) c(t) @@ -991,20 +992,20 @@ end end @testset "Bump" begin - @variables x(t) [irreducible=true] y(t) [irreducible=true] + @variables x(t) [irreducible = true] y(t) [irreducible = true] eqs = [x ~ y, D(x) ~ -1] cb = [x ~ 0.0] => [x ~ 0, y ~ 1] - @mtkbuild pend = ODESystem(eqs, t;continuous_events = [cb]) + @mtkbuild pend = ODESystem(eqs, t; continuous_events = [cb]) prob = ODEProblem(pend, [x => 1], (0.0, 3.0), guesses = [y => x]) @test_throws "initialization not satisifed" solve(prob, Rodas5()) - + cb = [x ~ 0.0] => [y ~ 1] - @mtkbuild pend = ODESystem(eqs, t;continuous_events = [cb]) + @mtkbuild pend = ODESystem(eqs, t; continuous_events = [cb]) prob = ODEProblem(pend, [x => 1], (0.0, 3.0), guesses = [y => x]) @test_broken !SciMLBase.successful_retcode(solve(prob, Rodas5())) cb = [x ~ 0.0] => [x ~ 1, y ~ 1] - @mtkbuild pend = ODESystem(eqs, t;continuous_events = [cb]) + @mtkbuild pend = ODESystem(eqs, t; continuous_events = [cb]) prob = ODEProblem(pend, [x => 1], (0.0, 3.0), guesses = [y => x]) - @test all(≈(0.0; atol=1e-9), solve(prob, Rodas5())[[x,y]][end]) -end \ No newline at end of file + @test all(≈(0.0; atol = 1e-9), solve(prob, Rodas5())[[x, y]][end]) +end From 2c49ef2def541d45755751faa89ff8c4fa44bbba Mon Sep 17 00:00:00 2001 From: Benjamin Chung Date: Wed, 23 Oct 2024 03:53:08 -0700 Subject: [PATCH 0231/2176] Sidestep typo --- test/symbolic_events.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/symbolic_events.jl b/test/symbolic_events.jl index 998786fa67..670eea76fe 100644 --- a/test/symbolic_events.jl +++ b/test/symbolic_events.jl @@ -997,7 +997,7 @@ end cb = [x ~ 0.0] => [x ~ 0, y ~ 1] @mtkbuild pend = ODESystem(eqs, t; continuous_events = [cb]) prob = ODEProblem(pend, [x => 1], (0.0, 3.0), guesses = [y => x]) - @test_throws "initialization not satisifed" solve(prob, Rodas5()) + @test_throws "CheckInit specified but initialization" solve(prob, Rodas5()) cb = [x ~ 0.0] => [y ~ 1] @mtkbuild pend = ODESystem(eqs, t; continuous_events = [cb]) From f89450029514546579f8cb1a27c5be60a4c100af Mon Sep 17 00:00:00 2001 From: Benjamin Chung Date: Wed, 23 Oct 2024 04:16:29 -0700 Subject: [PATCH 0232/2176] Fix test dup --- test/symbolic_events.jl | 21 --------------------- 1 file changed, 21 deletions(-) diff --git a/test/symbolic_events.jl b/test/symbolic_events.jl index 670eea76fe..26593f980c 100644 --- a/test/symbolic_events.jl +++ b/test/symbolic_events.jl @@ -867,27 +867,6 @@ end @test sign.(cos.(3 * (required_crossings_c2 .+ 1e-6))) == sign.(last.(cr2)) end -@testset "Discrete variable timeseries" begin - @variables x(t) - @parameters a(t) b(t) c(t) - cb1 = [x ~ 1.0] => [a ~ -a] - function save_affect!(integ, u, p, ctx) - integ.ps[p.b] = 5.0 - end - cb2 = [x ~ 0.5] => (save_affect!, [], [b], [b], nothing) - cb3 = 1.0 => [c ~ t] - - @mtkbuild sys = ODESystem(D(x) ~ cos(t), t, [x], [a, b, c]; - continuous_events = [cb1, cb2], discrete_events = [cb3]) - prob = ODEProblem(sys, [x => 1.0], (0.0, 2pi), [a => 1.0, b => 2.0, c => 0.0]) - @test sort(canonicalize(Discrete(), prob.p)[1]) == [0.0, 1.0, 2.0] - sol = solve(prob, Tsit5()) - - @test sol[a] == [-1.0] - @test sol[b] == [5.0, 5.0] - @test sol[c] == [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0] -end - @testset "Discrete event reinitialization (#3142)" begin @connector LiquidPort begin p(t)::Float64, [description = "Set pressure in bar", From 47f84fe14cfd17f681d3ec5ae29221672815eb3a Mon Sep 17 00:00:00 2001 From: Benjamin Chung Date: Wed, 23 Oct 2024 04:17:51 -0700 Subject: [PATCH 0233/2176] Fix language bug --- src/systems/callbacks.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/systems/callbacks.jl b/src/systems/callbacks.jl index bca98c5c83..c427ae02a1 100644 --- a/src/systems/callbacks.jl +++ b/src/systems/callbacks.jl @@ -107,7 +107,7 @@ Affects (i.e. `affect` and `affect_neg`) can be specified as either: + `modified_parameters` is a vector of the parameters that are *modified* by `f!`. Note that a parameter will not appear in `p` if it only appears in `modified_parameters`; it must appear in both `parameters` and `modified_parameters` if it is used in the affect definition. + `ctx` is a user-defined context object passed to `f!` when invoked. This value is aliased for each problem. -Callbacks that impact a DAE are applied, then the DAE is reinitialized using `reinitializealg` (which defaults to `SciMLBase.CheckInit`). +DAEs will be reinitialized using `reinitializealg` (which defaults to `SciMLBase.CheckInit`) after callbacks are applied. This reinitialization algorithm ensures that the DAE is satisfied after the callback runs. The default value of `CheckInit` will simply validate that the newly-assigned values indeed satisfy the algebraic system; see the documentation on DAE initialization for a more detailed discussion of initialization. From 9bf734dcbc03123b72fb179a47370d9aa484869f Mon Sep 17 00:00:00 2001 From: Benjamin Chung Date: Wed, 23 Oct 2024 15:28:56 -0700 Subject: [PATCH 0234/2176] Simplify callback interface, fix references --- docs/src/basics/Events.md | 22 +++++++-------- src/systems/callbacks.jl | 31 ++++++-------------- test/symbolic_events.jl | 59 +++------------------------------------ 3 files changed, 23 insertions(+), 89 deletions(-) diff --git a/docs/src/basics/Events.md b/docs/src/basics/Events.md index 0d9149e23c..de1c1000ce 100644 --- a/docs/src/basics/Events.md +++ b/docs/src/basics/Events.md @@ -416,12 +416,12 @@ in between. To do this, we create two continuous callbacks: using Setfield furnace_disable = ModelingToolkit.SymbolicContinuousCallback( [temp ~ furnace_off_threshold], - ModelingToolkit.ImperativeAffect(modified = (; furnace_on)) do x, o, i, c + ModelingToolkit.ImperativeAffect(modified = (; furnace_on)) do x, o, c, i @set! x.furnace_on = false end) furnace_enable = ModelingToolkit.SymbolicContinuousCallback( [temp ~ furnace_on_threshold], - ModelingToolkit.ImperativeAffect(modified = (; furnace_on)) do x, o, i, c + ModelingToolkit.ImperativeAffect(modified = (; furnace_on)) do x, o, c, i @set! x.furnace_on = true end) ``` @@ -454,18 +454,16 @@ In our example, each event merely changes whether the furnace is on or off. Acco evaluate this before calling our function to fill out all of the numerical values, then apply them back to the system once our affect function returns. Furthermore, it will check that it is possible to do this assignment. -The function given to `ImperativeAffect` needs to have one of four signatures, checked in this order: - - - `f(modified::NamedTuple, observed::NamedTuple, ctx, integrator)::NamedTuple` if the function needs the low-level integrator, - - `f(modified::NamedTuple, observed::NamedTuple, ctx)::NamedTuple` if the function needs the user-defined context, - - `f(modified::NamedTuple, observed::NamedTuple)::NamedTuple` if the function also reads observed values from the system, - - `f(modified::NamedTuple)::NamedTuple` if the function only writes values (unknowns or parameters) to the system. - The `do` block in the example implicitly constructs said function inline. For exposition, we use the full version (e.g. `x, o, i, c`) but this could be simplified to merely `x`. +The function given to `ImperativeAffect` needs to have the signature: +```julia + f(modified::NamedTuple, observed::NamedTuple, ctx, integrator)::NamedTuple +``` The function `f` will be called with `observed` and `modified` `NamedTuple`s that are derived from their respective `NamedTuple` definitions. In our example, if `furnace_on` is `false`, then the value of the `x` that's passed in as `modified` will be `(furnace_on = false)`. The modified values should be passed out in the same format: to set `furnace_on` to `true` we need to return a tuple `(furnace_on = true)`. -We use Setfield to do this in the example, recreating the result tuple before returning it. +The examples does this with Setfield, recreating the result tuple before returning it; the returned tuple may optionally be missing values as +well, in which case those values will not be written back to the problem. Accordingly, we can now interpret the `ImperativeAffect` definitions to mean that when `temp = furnace_off_threshold` we will write `furnace_on = false` back to the system, and when `temp = furnace_on_threshold` we will write `furnace_on = true` back @@ -543,7 +541,7 @@ In our encoder, we interpret this as occlusion or nonocclusion of the sensor, up ```@example events qAevt = ModelingToolkit.SymbolicContinuousCallback([cos(100 * theta) ~ 0], - ModelingToolkit.ImperativeAffect((; qA, hA, hB, cnt), (; qB)) do x, o, i, c + ModelingToolkit.ImperativeAffect((; qA, hA, hB, cnt), (; qB)) do x, o, c, i @set! x.hA = x.qA @set! x.hB = o.qB @set! x.qA = 1 @@ -567,7 +565,7 @@ Instead, we can use right root finding: ```@example events qBevt = ModelingToolkit.SymbolicContinuousCallback([cos(100 * theta - π / 2) ~ 0], - ModelingToolkit.ImperativeAffect((; qB, hA, hB, cnt), (; qA, theta)) do x, o, i, c + ModelingToolkit.ImperativeAffect((; qB, hA, hB, cnt), (; qA, theta)) do x, o, c, i @set! x.hA = o.qA @set! x.hB = x.qB @set! x.qB = clamp(sign(cos(100 * o.theta - π / 2)), 0.0, 1.0) diff --git a/src/systems/callbacks.jl b/src/systems/callbacks.jl index e7198817b1..618a1f3a83 100644 --- a/src/systems/callbacks.jl +++ b/src/systems/callbacks.jl @@ -76,12 +76,11 @@ end `ImperativeAffect` is a helper for writing affect functions that will compute observed values and ensure that modified values are correctly written back into the system. The affect function `f` needs to have -one of four signatures: -* `f(modified::NamedTuple)::NamedTuple` if the function only writes values (unknowns or parameters) to the system, -* `f(modified::NamedTuple, observed::NamedTuple)::NamedTuple` if the function also reads observed values from the system, -* `f(modified::NamedTuple, observed::NamedTuple, ctx)::NamedTuple` if the function needs the user-defined context, -* `f(modified::NamedTuple, observed::NamedTuple, ctx, integrator)::NamedTuple` if the function needs the low-level integrator. -These will be checked in reverse order (that is, the four-argument version first, than the 3, etc). +the signature + +``` + f(modified::NamedTuple, observed::NamedTuple, ctx, integrator)::NamedTuple +``` The function `f` will be called with `observed` and `modified` `NamedTuple`s that are derived from their respective `NamedTuple` definitions. Each declaration`NamedTuple` should map an expression to a symbol; for example if we pass `observed=(; x = a + b)` this will alias the result of executing `a+b` in the system as `x` @@ -1046,7 +1045,7 @@ function compile_user_affect(affect::ImperativeAffect, cb, sys, dvs, ps; kwargs. Implementation sketch: generate observed function (oop), should save to a component array under obs_syms do the same stuff as the normal FA for pars_syms - call the affect method - test if it's OOP or IP using applicable + call the affect method unpack and apply the resulting values =# function check_dups(syms, exprs) # = (syms_dedup, exprs_dedup) @@ -1135,22 +1134,10 @@ function compile_user_affect(affect::ImperativeAffect, cb, sys, dvs, ps; kwargs. integ.u, integ.p, integ.t)) # let the user do their thing - modvals = if applicable( - user_affect, upd_component_array, obs_component_array, ctx, integ) - user_affect(upd_component_array, obs_component_array, ctx, integ) - elseif applicable(user_affect, upd_component_array, obs_component_array, ctx) - user_affect(upd_component_array, obs_component_array, ctx) - elseif applicable(user_affect, upd_component_array, obs_component_array) - user_affect(upd_component_array, obs_component_array) - elseif applicable(user_affect, upd_component_array) - user_affect(upd_component_array) - else - @error "User affect function $user_affect needs to implement one of the supported ImperativeAffect callback forms; see the ImperativeAffect docstring for more details" - user_affect(upd_component_array, obs_component_array, integ, ctx) # this WILL error but it'll give a more sensible message - end - + upd_vals = user_affect(upd_component_array, obs_component_array, ctx, integ) + # write the new values back to the integrator - _generated_writeback(integ, upd_funs, modvals) + _generated_writeback(integ, upd_funs, upd_vals) for idx in save_idxs SciMLBase.save_discretes!(integ, idx) diff --git a/test/symbolic_events.jl b/test/symbolic_events.jl index c021f99eea..3c3e9168e7 100644 --- a/test/symbolic_events.jl +++ b/test/symbolic_events.jl @@ -1027,60 +1027,9 @@ end furnace_off = ModelingToolkit.SymbolicContinuousCallback( [temp ~ furnace_off_threshold], - ModelingToolkit.ImperativeAffect(modified = (; furnace_on)) do x, o, i - @set! x.furnace_on = false - end) - furnace_enable = ModelingToolkit.SymbolicContinuousCallback( - [temp ~ furnace_on_threshold], - ModelingToolkit.ImperativeAffect(modified = (; furnace_on)) do x, o, i - @set! x.furnace_on = true - end) - @named sys = ODESystem( - eqs, t, [temp], params; continuous_events = [furnace_off, furnace_enable]) - ss = structural_simplify(sys) - prob = ODEProblem(ss, [temp => 0.0, furnace_on => true], (0.0, 100.0)) - sol = solve(prob, Tsit5(); dtmax = 0.01) - @test all(sol[temp][sol.t .> 1.0] .<= 0.79) && all(sol[temp][sol.t .> 1.0] .>= 0.49) - - furnace_off = ModelingToolkit.SymbolicContinuousCallback( - [temp ~ furnace_off_threshold], - ModelingToolkit.ImperativeAffect(modified = (; furnace_on)) do x, o - @set! x.furnace_on = false - end) - furnace_enable = ModelingToolkit.SymbolicContinuousCallback( - [temp ~ furnace_on_threshold], - ModelingToolkit.ImperativeAffect(modified = (; furnace_on)) do x, o - @set! x.furnace_on = true - end) - @named sys = ODESystem( - eqs, t, [temp], params; continuous_events = [furnace_off, furnace_enable]) - ss = structural_simplify(sys) - prob = ODEProblem(ss, [temp => 0.0, furnace_on => true], (0.0, 100.0)) - sol = solve(prob, Tsit5(); dtmax = 0.01) - @test all(sol[temp][sol.t .> 1.0] .<= 0.79) && all(sol[temp][sol.t .> 1.0] .>= 0.49) - - furnace_off = ModelingToolkit.SymbolicContinuousCallback( - [temp ~ furnace_off_threshold], - ModelingToolkit.ImperativeAffect(modified = (; furnace_on)) do x - @set! x.furnace_on = false - end) - furnace_enable = ModelingToolkit.SymbolicContinuousCallback( - [temp ~ furnace_on_threshold], - ModelingToolkit.ImperativeAffect(modified = (; furnace_on)) do x - @set! x.furnace_on = true - end) - @named sys = ODESystem( - eqs, t, [temp], params; continuous_events = [furnace_off, furnace_enable]) - ss = structural_simplify(sys) - prob = ODEProblem(ss, [temp => 0.0, furnace_on => true], (0.0, 100.0)) - sol = solve(prob, Tsit5(); dtmax = 0.01) - @test all(sol[temp][sol.t .> 1.0] .<= 0.79) && all(sol[temp][sol.t .> 1.0] .>= 0.49) - - furnace_off = ModelingToolkit.SymbolicContinuousCallback( - [temp ~ furnace_off_threshold], - ModelingToolkit.ImperativeAffect(modified = (; furnace_on)) do x + ModelingToolkit.ImperativeAffect(modified = (; furnace_on)) do x, o, c, i @set! x.furnace_on = false - end; initialize = ModelingToolkit.ImperativeAffect(modified = (; temp)) do x + end; initialize = ModelingToolkit.ImperativeAffect(modified = (; temp)) do x, o, c, i @set! x.temp = 0.2 end) furnace_enable = ModelingToolkit.SymbolicContinuousCallback( @@ -1180,7 +1129,7 @@ end end end qAevt = ModelingToolkit.SymbolicContinuousCallback([cos(100 * theta) ~ 0], - ModelingToolkit.ImperativeAffect((; qA, hA, hB, cnt), (; qB)) do x, o, i, c + ModelingToolkit.ImperativeAffect((; qA, hA, hB, cnt), (; qB)) do x, o, c, i @set! x.hA = x.qA @set! x.hB = o.qB @set! x.qA = 1 @@ -1196,7 +1145,7 @@ end x end; rootfind = SciMLBase.RightRootFind) qBevt = ModelingToolkit.SymbolicContinuousCallback([cos(100 * theta - π / 2) ~ 0], - ModelingToolkit.ImperativeAffect((; qB, hA, hB, cnt), (; qA)) do x, o, i, c + ModelingToolkit.ImperativeAffect((; qB, hA, hB, cnt), (; qA)) do x, o, c, i @set! x.hA = o.qA @set! x.hB = x.qB @set! x.qB = 1 From 1a3f7d4e4916c12d26a4659334a859a9c3d5ca8b Mon Sep 17 00:00:00 2001 From: Benjamin Chung Date: Wed, 23 Oct 2024 15:33:20 -0700 Subject: [PATCH 0235/2176] Make array_type an actual type, though only in a limited sense --- src/systems/callbacks.jl | 4 ++-- src/systems/diffeqs/odesystem.jl | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/systems/callbacks.jl b/src/systems/callbacks.jl index 618a1f3a83..c34681a20c 100644 --- a/src/systems/callbacks.jl +++ b/src/systems/callbacks.jl @@ -1105,7 +1105,7 @@ function compile_user_affect(affect::ImperativeAffect, cb, sys, dvs, ps; kwargs. end obs_fun = build_explicit_observed_function( sys, Symbolics.scalarize.(obs_exprs); - array_type = :tuple) + array_type = Tuple) obs_sym_tuple = (obs_syms...,) # okay so now to generate the stuff to assign it back into the system @@ -1113,7 +1113,7 @@ function compile_user_affect(affect::ImperativeAffect, cb, sys, dvs, ps; kwargs. mod_names = (mod_syms...,) mod_og_val_fun = build_explicit_observed_function( sys, Symbolics.scalarize.(first.(mod_pairs)); - array_type = :tuple) + array_type = Tuple) upd_funs = NamedTuple{mod_names}((setu.((sys,), first.(mod_pairs))...,)) diff --git a/src/systems/diffeqs/odesystem.jl b/src/systems/diffeqs/odesystem.jl index 1cc8273b4d..374cbab5f8 100644 --- a/src/systems/diffeqs/odesystem.jl +++ b/src/systems/diffeqs/odesystem.jl @@ -429,7 +429,7 @@ Options not otherwise specified are: * `op = Operator` sets the recursion terminator for the walk done by `vars` to identify the variables that appear in `ts`. See the documentation for `vars` for more detail. * `throw = true` if true, throw an error when generating a function for `ts` that reference variables that do not exist * `drop_expr` is deprecated. -* `array_type`; only used if the output is an array (that is, `!isscalar(ts)`). If `:array`, then it will generate an array, if `:tuple` then it will generate a tuple. +* `array_type`; only used if the output is an array (that is, `!isscalar(ts)`). If it is `Vector`, then it will generate an array, if `Tuple` then it will generate a tuple. """ function build_explicit_observed_function(sys, ts; inputs = nothing, @@ -444,7 +444,7 @@ function build_explicit_observed_function(sys, ts; param_only = false, op = Operator, throw = true, - array_type = :array) + array_type = Vector) if (isscalar = symbolic_type(ts) !== NotSymbolic()) ts = [ts] end @@ -590,7 +590,7 @@ function build_explicit_observed_function(sys, ts; end output_expr = isscalar ? ts[1] : - (array_type == :array ? MakeArray(ts, output_type) : MakeTuple(ts)) + (array_type <: Vector ? MakeArray(ts, output_type) : MakeTuple(ts)) # Need to keep old method of building the function since it uses `output_type`, # which can't be provided to `build_function` oop_fn = Func(args, [], pre(Let(obsexprs, output_expr, false))) |> array_wrapper[1] |> From 6c7e4b84ae40e0bf1cbe1c61f6e4c41202ee449c Mon Sep 17 00:00:00 2001 From: Benjamin Chung Date: Wed, 23 Oct 2024 15:34:59 -0700 Subject: [PATCH 0236/2176] Clean up language indocs --- docs/src/basics/Events.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/src/basics/Events.md b/docs/src/basics/Events.md index de1c1000ce..902be98fd4 100644 --- a/docs/src/basics/Events.md +++ b/docs/src/basics/Events.md @@ -404,7 +404,7 @@ eqs = [ ] ``` -Our plant is simple. We have a heater that's turned on and off by the clocked parameter `furnace_on` +Our plant is simple. We have a heater that's turned on and off by the time-indexed parameter `furnace_on` which adds `furnace_power` forcing to the system when enabled. We then leak heat proportional to `leakage` as a function of the square of the current temperature. @@ -499,9 +499,9 @@ eqs = [D(theta) ~ omega omega ~ 1.0] ``` -Our continuous-time system is extremely simple. We have two states, `theta` for the angle of the shaft +Our continuous-time system is extremely simple. We have two unknown variables `theta` for the angle of the shaft and `omega` for the rate at which it's spinning. We then have parameters for the state machine `qA, qB, hA, hB` -and a step count `cnt`. +(corresponding to the current quadrature of the A/B sensors and the historical ones) and a step count `cnt`. We'll then implement the decoder as a simple Julia function. From 06ecd2ee541d94bcf92c39543583f9bc0646286d Mon Sep 17 00:00:00 2001 From: Benjamin Chung Date: Wed, 23 Oct 2024 21:24:46 -0700 Subject: [PATCH 0237/2176] Format --- docs/src/basics/Events.md | 7 ++++--- src/systems/callbacks.jl | 2 +- test/symbolic_events.jl | 3 ++- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/docs/src/basics/Events.md b/docs/src/basics/Events.md index 902be98fd4..4c9803ef57 100644 --- a/docs/src/basics/Events.md +++ b/docs/src/basics/Events.md @@ -456,13 +456,14 @@ once our affect function returns. Furthermore, it will check that it is possible The function given to `ImperativeAffect` needs to have the signature: -```julia - f(modified::NamedTuple, observed::NamedTuple, ctx, integrator)::NamedTuple +```julia +f(modified::NamedTuple, observed::NamedTuple, ctx, integrator)::NamedTuple ``` + The function `f` will be called with `observed` and `modified` `NamedTuple`s that are derived from their respective `NamedTuple` definitions. In our example, if `furnace_on` is `false`, then the value of the `x` that's passed in as `modified` will be `(furnace_on = false)`. The modified values should be passed out in the same format: to set `furnace_on` to `true` we need to return a tuple `(furnace_on = true)`. -The examples does this with Setfield, recreating the result tuple before returning it; the returned tuple may optionally be missing values as +The examples does this with Setfield, recreating the result tuple before returning it; the returned tuple may optionally be missing values as well, in which case those values will not be written back to the problem. Accordingly, we can now interpret the `ImperativeAffect` definitions to mean that when `temp = furnace_off_threshold` we diff --git a/src/systems/callbacks.jl b/src/systems/callbacks.jl index c34681a20c..1061139a8a 100644 --- a/src/systems/callbacks.jl +++ b/src/systems/callbacks.jl @@ -1135,7 +1135,7 @@ function compile_user_affect(affect::ImperativeAffect, cb, sys, dvs, ps; kwargs. # let the user do their thing upd_vals = user_affect(upd_component_array, obs_component_array, ctx, integ) - + # write the new values back to the integrator _generated_writeback(integ, upd_funs, upd_vals) diff --git a/test/symbolic_events.jl b/test/symbolic_events.jl index 3c3e9168e7..77961cb01f 100644 --- a/test/symbolic_events.jl +++ b/test/symbolic_events.jl @@ -1029,7 +1029,8 @@ end [temp ~ furnace_off_threshold], ModelingToolkit.ImperativeAffect(modified = (; furnace_on)) do x, o, c, i @set! x.furnace_on = false - end; initialize = ModelingToolkit.ImperativeAffect(modified = (; temp)) do x, o, c, i + end; initialize = ModelingToolkit.ImperativeAffect(modified = (; + temp)) do x, o, c, i @set! x.temp = 0.2 end) furnace_enable = ModelingToolkit.SymbolicContinuousCallback( From 8f2a9e4ba76f55939b71b93ca293740cc523e2cc Mon Sep 17 00:00:00 2001 From: Christopher Rackauckas Date: Thu, 24 Oct 2024 05:06:30 +0000 Subject: [PATCH 0238/2176] Update Project.toml --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index f2564304a8..6650abee74 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "ModelingToolkit" uuid = "961ee093-0014-501f-94e3-6117800e7a78" authors = ["Yingbo Ma ", "Chris Rackauckas and contributors"] -version = "9.46.1" +version = "9.47.0" [deps] AbstractTrees = "1520ce14-60c1-5f80-bbc7-55ef81b5835c" From 1e3618d99cf2daca3e985e0b7e7a822b81332752 Mon Sep 17 00:00:00 2001 From: Christopher Rackauckas Date: Thu, 24 Oct 2024 05:20:03 +0000 Subject: [PATCH 0239/2176] Update bifurcationkit.jl --- test/extensions/bifurcationkit.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/extensions/bifurcationkit.jl b/test/extensions/bifurcationkit.jl index 698dd085c8..c2dc5685aa 100644 --- a/test/extensions/bifurcationkit.jl +++ b/test/extensions/bifurcationkit.jl @@ -36,7 +36,7 @@ let bprob_BK = BifurcationProblem(f_BK, [1.0, 1.0], [-1.0, 1.0], - (@lens _[1]); + (BifurcationKit.@optic _[1]); record_from_solution = (x, p) -> x[1]) bif_dia_BK = bifurcationdiagram(bprob_BK, PALC(), From a03f061f049b8cab15843b91acc6f2530f1a9c7d Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 16 Oct 2024 18:52:33 +0530 Subject: [PATCH 0240/2176] fix: fix variable discovery in arrays of `Num` passed to callable params --- src/utils.jl | 10 ++++++++-- test/odesystem.jl | 8 ++++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/utils.jl b/src/utils.jl index d2e8a3ea38..830ec98e44 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -389,7 +389,13 @@ function vars!(vars, O; op = Differential) f = getcalledparameter(O) push!(vars, f) for arg in arguments(O) - vars!(vars, arg; op) + if symbolic_type(arg) == NotSymbolic() && arg isa AbstractArray + for el in arg + vars!(vars, unwrap(el); op) + end + else + vars!(vars, arg; op) + end end return vars end @@ -397,7 +403,7 @@ function vars!(vars, O; op = Differential) end if symbolic_type(O) == NotSymbolic() && O isa AbstractArray for arg in O - vars!(vars, arg; op) + vars!(vars, unwrap(arg); op) end return vars end diff --git a/test/odesystem.jl b/test/odesystem.jl index 9446d105e0..cbc24300cd 100644 --- a/test/odesystem.jl +++ b/test/odesystem.jl @@ -1447,3 +1447,11 @@ end @parameters p @test_nowarn ODESystem(Equation[], t; parameter_dependencies = [p ~ 1.0], name = :a) end + +@testset "Variable discovery in arrays of `Num` inside callable symbolic" begin + @variables x(t) y(t) + @parameters foo(::AbstractVector) + sys = @test_nowarn ODESystem(D(x) ~ foo([x, 2y]), t; name = :sys) + @test length(unknowns(sys)) == 2 + @test any(isequal(y), unknowns(sys)) +end From 5c2edf4ff43796be3284a42843fc73d75967bc58 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 16 Oct 2024 18:53:17 +0530 Subject: [PATCH 0241/2176] fix: improve hack supporting unscalarized usage of array observed variables --- .../symbolics_tearing.jl | 98 +++++++++++++------ test/structural_transformation/utils.jl | 14 +++ 2 files changed, 83 insertions(+), 29 deletions(-) diff --git a/src/structural_transformation/symbolics_tearing.jl b/src/structural_transformation/symbolics_tearing.jl index 84cee928cd..889b75c611 100644 --- a/src/structural_transformation/symbolics_tearing.jl +++ b/src/structural_transformation/symbolics_tearing.jl @@ -574,48 +574,78 @@ function tearing_reassemble(state::TearingState, var_eq_matching, # TODO: compute the dependency correctly so that we don't have to do this obs = [fast_substitute(observed(sys), obs_sub); subeqs] - # HACK: Substitute non-scalarized symbolic arrays of observed variables - # E.g. if `p[1] ~ (...)` and `p[2] ~ (...)` then substitute `p => [p[1], p[2]]` in all equations - # ideally, we want to support equations such as `p ~ [p[1], p[2]]` which will then be handled - # by the topological sorting and dependency identification pieces - obs_arr_subs = Dict() + unknowns = Any[v + for (i, v) in enumerate(fullvars) + if diff_to_var[i] === nothing && ispresent(i)] + if !isempty(extra_vars) + for v in extra_vars + push!(unknowns, old_fullvars[v]) + end + end + @set! sys.unknowns = unknowns + # HACK: Add equations for array observed variables. If `p[i] ~ (...)` + # are equations, add an equation `p ~ [p[1], p[2], ...]` + # allow topsort to reorder them + # only add the new equation if all `p[i]` are present and the unscalarized + # form is used in any equation (observed or not) + # we first count the number of times the scalarized form of each observed + # variable occurs in observed equations (and unknowns if it's split). + + # map of array observed variable (unscalarized) to number of its + # scalarized terms that appear in observed equations + arr_obs_occurrences = Dict() + # to check if array variables occur in unscalarized form anywhere + all_vars = Set() for eq in obs + vars!(all_vars, eq.rhs) lhs = eq.lhs iscall(lhs) || continue operation(lhs) === getindex || continue - Symbolics.shape(lhs) !== Symbolics.Unknown() || continue + Symbolics.shape(lhs) != Symbolics.Unknown() || continue arg1 = arguments(lhs)[1] - haskey(obs_arr_subs, arg1) && continue - obs_arr_subs[arg1] = [arg1[i] for i in eachindex(arg1)] # e.g. p => [p[1], p[2]] - index_first = eachindex(arg1)[1] - + cnt = get(arr_obs_occurrences, arg1, 0) + arr_obs_occurrences[arg1] = cnt + 1 + continue + end + # count variables in unknowns if they are scalarized forms of variables + # also present as observed. e.g. if `x[1]` is an unknown and `x[2] ~ (..)` + # is an observed equation. + for sym in unknowns + iscall(sym) || continue + operation(sym) === getindex || continue + Symbolics.shape(sym) != Symbolics.Unknown() || continue + arg1 = arguments(sym)[1] + cnt = get(arr_obs_occurrences, arg1, 0) + cnt == 0 && continue + arr_obs_occurrences[arg1] = cnt + 1 + end + for eq in neweqs + vars!(all_vars, eq.rhs) + end + obs_arr_eqs = Equation[] + for (arrvar, cnt) in arr_obs_occurrences + cnt == length(arrvar) || continue + arrvar in all_vars || continue + # firstindex returns 1 for multidimensional array symbolics + firstind = first(eachindex(arrvar)) + scal = [arrvar[i] for i in eachindex(arrvar)] # respect non-1-indexed arrays # TODO: get rid of this hack together with the above hack, then remove OffsetArrays dependency - obs_arr_subs[arg1] = Origin(index_first)(obs_arr_subs[arg1]) - end - for i in eachindex(neweqs) - neweqs[i] = fast_substitute(neweqs[i], obs_arr_subs; operator = Symbolics.Operator) - end - for i in eachindex(obs) - obs[i] = fast_substitute(obs[i], obs_arr_subs; operator = Symbolics.Operator) - end - for i in eachindex(subeqs) - subeqs[i] = fast_substitute(subeqs[i], obs_arr_subs; operator = Symbolics.Operator) + # `change_origin` is required because `Origin(firstind)(scal)` makes codegen + # try to `create_array(OffsetArray{...}, ...)` which errors. + # `term(Origin(firstind), scal)` doesn't retain the `symtype` and `size` + # of `scal`. + push!(obs_arr_eqs, arrvar ~ change_origin(Origin(firstind), scal)) end + append!(obs, obs_arr_eqs) + append!(subeqs, obs_arr_eqs) + # need to re-sort subeqs + subeqs = ModelingToolkit.topsort_equations(subeqs, [eq.lhs for eq in subeqs]) @set! sys.eqs = neweqs @set! sys.observed = obs - unknowns = Any[v - for (i, v) in enumerate(fullvars) - if diff_to_var[i] === nothing && ispresent(i)] - if !isempty(extra_vars) - for v in extra_vars - push!(unknowns, old_fullvars[v]) - end - end - @set! sys.unknowns = unknowns @set! sys.substitutions = Substitutions(subeqs, deps) # Only makes sense for time-dependent @@ -629,6 +659,16 @@ function tearing_reassemble(state::TearingState, var_eq_matching, return invalidate_cache!(sys) end +function change_origin(origin, arr) + return origin(arr) +end + +@register_array_symbolic change_origin(origin::Origin, arr::AbstractArray) begin + size = size(arr) + eltype = eltype(arr) + ndims = ndims(arr) +end + function tearing(state::TearingState; kwargs...) state.structure.solvable_graph === nothing && find_solvables!(state; kwargs...) complete!(state.structure) diff --git a/test/structural_transformation/utils.jl b/test/structural_transformation/utils.jl index 8644d96945..c7146bab65 100644 --- a/test/structural_transformation/utils.jl +++ b/test/structural_transformation/utils.jl @@ -40,3 +40,17 @@ end @test ModelingToolkit.𝑠neighbors(g, 1) == [2] @test ModelingToolkit.𝑑neighbors(g, 2) == [1] end + +@testset "array observed used unscalarized in another observed" begin + @variables x(t) y(t)[1:2] z(t)[1:2] + @parameters foo(::AbstractVector)[1:2] + _tmp_fn(x) = 2x + @mtkbuild sys = ODESystem( + [D(x) ~ z[1] + z[2] + foo(z)[1], y[1] ~ 2t, y[2] ~ 3t, z ~ foo(y)], t) + @test length(equations(sys)) == 1 + @test length(observed(sys)) == 6 + @test any(eq -> isequal(eq.lhs, y), observed(sys)) + @test any(eq -> isequal(eq.lhs, z), observed(sys)) + prob = ODEProblem(sys, [x => 1.0], (0.0, 1.0), [foo => _tmp_fn]) + @test_nowarn prob.f(prob.u0, prob.p, 0.0) +end From 6c3576f0f18918fb5643fdedc4d9842eede18bc2 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 18 Oct 2024 20:33:59 +0530 Subject: [PATCH 0242/2176] feat: add simple CSE for array scalarization case --- .../symbolics_tearing.jl | 54 ++++++++++++++++++- test/structural_transformation/utils.jl | 18 ++++++- 2 files changed, 69 insertions(+), 3 deletions(-) diff --git a/src/structural_transformation/symbolics_tearing.jl b/src/structural_transformation/symbolics_tearing.jl index 889b75c611..d7bec888b5 100644 --- a/src/structural_transformation/symbolics_tearing.jl +++ b/src/structural_transformation/symbolics_tearing.jl @@ -584,6 +584,16 @@ function tearing_reassemble(state::TearingState, var_eq_matching, end @set! sys.unknowns = unknowns + # HACK: Since we don't support array equations, any equation of the sort + # `x[1:n] ~ f(...)[1:n]` gets turned into `x[1] ~ f(...)[1], x[2] ~ f(...)[2]`. Repeatedly + # calling `f` gets _very_ expensive. this hack performs a limited form of CSE specifically + # for this case to avoid the unnecessary cost. + # This and the below hack are implemented simultaneously + + # mapping of rhs to temporary CSE variable + # `f(...) => tmpvar` in above example + rhs_to_tempvar = Dict() + # HACK: Add equations for array observed variables. If `p[i] ~ (...)` # are equations, add an equation `p ~ [p[1], p[2], ...]` # allow topsort to reorder them @@ -597,9 +607,42 @@ function tearing_reassemble(state::TearingState, var_eq_matching, arr_obs_occurrences = Dict() # to check if array variables occur in unscalarized form anywhere all_vars = Set() - for eq in obs - vars!(all_vars, eq.rhs) + for (i, eq) in enumerate(obs) lhs = eq.lhs + rhs = eq.rhs + vars!(all_vars, rhs) + + # HACK 1 + if (!ModelingToolkit.isvariable(rhs) || ModelingToolkit.iscalledparameter(rhs)) && + iscall(rhs) && operation(rhs) === getindex && + Symbolics.shape(rhs) != Symbolics.Unknown() + rhs_arr = arguments(rhs)[1] + if !haskey(rhs_to_tempvar, rhs_arr) + tempvar = gensym(Symbol(lhs)) + N = length(rhs_arr) + tempvar = unwrap(Symbolics.variable( + tempvar; T = Symbolics.symtype(rhs_arr))) + tempvar = setmetadata( + tempvar, Symbolics.ArrayShapeCtx, Symbolics.shape(rhs_arr)) + tempeq = tempvar ~ rhs_arr + rhs_to_tempvar[rhs_arr] = tempvar + push!(obs, tempeq) + push!(subeqs, tempeq) + end + + # getindex_wrapper is used because `observed2graph` treats `x` and `x[i]` as different, + # so it doesn't find a dependency between this equation and `tempvar ~ rhs_arr` + # which fails the topological sort + neweq = lhs ~ getindex_wrapper( + rhs_to_tempvar[rhs_arr], Tuple(arguments(rhs)[2:end])) + obs[i] = neweq + subeqi = findfirst(isequal(eq), subeqs) + if subeqi !== nothing + subeqs[subeqi] = neweq + end + end + # end HACK 1 + iscall(lhs) || continue operation(lhs) === getindex || continue Symbolics.shape(lhs) != Symbolics.Unknown() || continue @@ -640,6 +683,7 @@ function tearing_reassemble(state::TearingState, var_eq_matching, end append!(obs, obs_arr_eqs) append!(subeqs, obs_arr_eqs) + # need to re-sort subeqs subeqs = ModelingToolkit.topsort_equations(subeqs, [eq.lhs for eq in subeqs]) @@ -659,6 +703,12 @@ function tearing_reassemble(state::TearingState, var_eq_matching, return invalidate_cache!(sys) end +# PART OF HACK 1 +getindex_wrapper(x, i) = x[i...] + +@register_symbolic getindex_wrapper(x::AbstractArray, i::Tuple{Vararg{Int}}) + +# PART OF HACK 2 function change_origin(origin, arr) return origin(arr) end diff --git a/test/structural_transformation/utils.jl b/test/structural_transformation/utils.jl index c7146bab65..dded8333f2 100644 --- a/test/structural_transformation/utils.jl +++ b/test/structural_transformation/utils.jl @@ -48,9 +48,25 @@ end @mtkbuild sys = ODESystem( [D(x) ~ z[1] + z[2] + foo(z)[1], y[1] ~ 2t, y[2] ~ 3t, z ~ foo(y)], t) @test length(equations(sys)) == 1 - @test length(observed(sys)) == 6 + @test length(observed(sys)) == 7 @test any(eq -> isequal(eq.lhs, y), observed(sys)) @test any(eq -> isequal(eq.lhs, z), observed(sys)) prob = ODEProblem(sys, [x => 1.0], (0.0, 1.0), [foo => _tmp_fn]) @test_nowarn prob.f(prob.u0, prob.p, 0.0) end + +@testset "scalarized array observed calling same function multiple times" begin + @variables x(t) y(t)[1:2] + @parameters foo(::Real)[1:2] + val = Ref(0) + function _tmp_fn2(x) + val[] += 1 + return [x, 2x] + end + @mtkbuild sys = ODESystem([D(x) ~ y[1] + y[2], y ~ foo(x)], t) + @test length(equations(sys)) == 1 + @test length(observed(sys)) == 3 + prob = ODEProblem(sys, [x => 1.0], (0.0, 1.0), [foo => _tmp_fn2]) + @test_nowarn prob.f(prob.u0, prob.p, 0.0) + @test val[] == 1 +end From c023e7ebba82d5d83b7f6448ad04a67f37fccefa Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 23 Oct 2024 20:17:18 +0530 Subject: [PATCH 0243/2176] fix: undo the hack in `generate_initializesystem` --- src/systems/nonlinear/initializesystem.jl | 45 ++++++++++++++++++++--- test/structural_transformation/utils.jl | 16 ++++++++ 2 files changed, 56 insertions(+), 5 deletions(-) diff --git a/src/systems/nonlinear/initializesystem.jl b/src/systems/nonlinear/initializesystem.jl index becace8ec5..0bdb770c54 100644 --- a/src/systems/nonlinear/initializesystem.jl +++ b/src/systems/nonlinear/initializesystem.jl @@ -12,7 +12,9 @@ function generate_initializesystem(sys::ODESystem; algebraic_only = false, check_units = true, check_defguess = false, name = nameof(sys), kwargs...) - vars = unique([unknowns(sys); getfield.((observed(sys)), :lhs)]) + trueobs = unhack_observed(observed(sys)) + @show trueobs + vars = unique([unknowns(sys); getfield.(trueobs, :lhs)]) vars_set = Set(vars) # for efficient in-lookup eqs = equations(sys) @@ -24,7 +26,7 @@ function generate_initializesystem(sys::ODESystem; D = Differential(get_iv(sys)) diffmap = merge( Dict(eq.lhs => eq.rhs for eq in eqs_diff), - Dict(D(eq.lhs) => D(eq.rhs) for eq in observed(sys)) + Dict(D(eq.lhs) => D(eq.rhs) for eq in trueobs) ) # 1) process dummy derivatives and u0map into initialization system @@ -166,15 +168,14 @@ function generate_initializesystem(sys::ODESystem; ) # 7) use observed equations for guesses of observed variables if not provided - obseqs = observed(sys) - for eq in obseqs + for eq in trueobs haskey(defs, eq.lhs) && continue any(x -> isequal(default_toterm(x), eq.lhs), keys(defs)) && continue defs[eq.lhs] = eq.rhs end - eqs_ics = Symbolics.substitute.([eqs_ics; obseqs], (paramsubs,)) + eqs_ics = Symbolics.substitute.([eqs_ics; trueobs], (paramsubs,)) vars = [vars; collect(values(paramsubs))] for k in keys(defs) defs[k] = substitute(defs[k], paramsubs) @@ -324,3 +325,37 @@ function SciMLBase.remake_initializeprob(sys::ODESystem, odefn, u0, t0, p) return nothing, nothing, nothing, nothing end end + +""" +Counteracts the CSE/array variable hacks in `symbolics_tearing.jl` so it works with +initialization. +""" +function unhack_observed(eqs::Vector{Equation}) + subs = Dict() + tempvars = Set() + rm_idxs = Int[] + for (i, eq) in enumerate(eqs) + iscall(eq.rhs) || continue + if operation(eq.rhs) == StructuralTransformations.change_origin + push!(rm_idxs, i) + continue + end + if operation(eq.rhs) == StructuralTransformations.getindex_wrapper + var, idxs = arguments(eq.rhs) + subs[eq.rhs] = var[idxs...] + push!(tempvars, var) + end + end + + for (i, eq) in enumerate(eqs) + if eq.lhs in tempvars + subs[eq.lhs] = eq.rhs + push!(rm_idxs, i) + end + end + + eqs = eqs[setdiff(eachindex(eqs), rm_idxs)] + return map(eqs) do eq + fixpoint_sub(eq.lhs, subs) ~ fixpoint_sub(eq.rhs, subs) + end +end diff --git a/test/structural_transformation/utils.jl b/test/structural_transformation/utils.jl index dded8333f2..ea3552ff0d 100644 --- a/test/structural_transformation/utils.jl +++ b/test/structural_transformation/utils.jl @@ -53,6 +53,14 @@ end @test any(eq -> isequal(eq.lhs, z), observed(sys)) prob = ODEProblem(sys, [x => 1.0], (0.0, 1.0), [foo => _tmp_fn]) @test_nowarn prob.f(prob.u0, prob.p, 0.0) + + isys = ModelingToolkit.generate_initializesystem(sys) + @test length(unknowns(isys)) == 5 + @test length(equations(isys)) == 4 + @test !any(equations(isys)) do eq + iscall(eq.rhs) && operation(eq.rhs) in [StructuralTransformations.getindex_wrapper, + StructuralTransformations.change_origin] + end end @testset "scalarized array observed calling same function multiple times" begin @@ -69,4 +77,12 @@ end prob = ODEProblem(sys, [x => 1.0], (0.0, 1.0), [foo => _tmp_fn2]) @test_nowarn prob.f(prob.u0, prob.p, 0.0) @test val[] == 1 + + isys = ModelingToolkit.generate_initializesystem(sys) + @test length(unknowns(isys)) == 3 + @test length(equations(isys)) == 2 + @test !any(equations(isys)) do eq + iscall(eq.rhs) && operation(eq.rhs) in [StructuralTransformations.getindex_wrapper, + StructuralTransformations.change_origin] + end end From 79a7fc933b858e51412abc486ce1018fb8484638 Mon Sep 17 00:00:00 2001 From: Christopher Rackauckas Date: Thu, 24 Oct 2024 04:51:51 +0000 Subject: [PATCH 0244/2176] Update src/systems/nonlinear/initializesystem.jl --- src/systems/nonlinear/initializesystem.jl | 1 - 1 file changed, 1 deletion(-) diff --git a/src/systems/nonlinear/initializesystem.jl b/src/systems/nonlinear/initializesystem.jl index 0bdb770c54..6c7457e49b 100644 --- a/src/systems/nonlinear/initializesystem.jl +++ b/src/systems/nonlinear/initializesystem.jl @@ -13,7 +13,6 @@ function generate_initializesystem(sys::ODESystem; check_units = true, check_defguess = false, name = nameof(sys), kwargs...) trueobs = unhack_observed(observed(sys)) - @show trueobs vars = unique([unknowns(sys); getfield.(trueobs, :lhs)]) vars_set = Set(vars) # for efficient in-lookup From d2fe0eb1a482ec7dc28b39bc21c7367bc1b60f8e Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 24 Oct 2024 11:43:38 +0530 Subject: [PATCH 0245/2176] feat: allow CSE and array hacks to be disabled --- .../symbolics_tearing.jl | 79 +++++++++++-------- test/structural_transformation/utils.jl | 44 +++++++++++ 2 files changed, 90 insertions(+), 33 deletions(-) diff --git a/src/structural_transformation/symbolics_tearing.jl b/src/structural_transformation/symbolics_tearing.jl index d7bec888b5..cabf0415ea 100644 --- a/src/structural_transformation/symbolics_tearing.jl +++ b/src/structural_transformation/symbolics_tearing.jl @@ -230,7 +230,7 @@ end =# function tearing_reassemble(state::TearingState, var_eq_matching, - full_var_eq_matching = nothing; simplify = false, mm = nothing) + full_var_eq_matching = nothing; simplify = false, mm = nothing, cse_hack = true, array_hack = true) @unpack fullvars, sys, structure = state @unpack solvable_graph, var_to_diff, eq_to_diff, graph = structure extra_vars = Int[] @@ -584,24 +584,48 @@ function tearing_reassemble(state::TearingState, var_eq_matching, end @set! sys.unknowns = unknowns - # HACK: Since we don't support array equations, any equation of the sort - # `x[1:n] ~ f(...)[1:n]` gets turned into `x[1] ~ f(...)[1], x[2] ~ f(...)[2]`. Repeatedly - # calling `f` gets _very_ expensive. this hack performs a limited form of CSE specifically - # for this case to avoid the unnecessary cost. - # This and the below hack are implemented simultaneously + obs, subeqs = cse_and_array_hacks( + obs, subeqs, unknowns, neweqs; cse = cse_hack, array = array_hack) + @set! sys.eqs = neweqs + @set! sys.observed = obs + + @set! sys.substitutions = Substitutions(subeqs, deps) + + # Only makes sense for time-dependent + # TODO: generalize to SDE + if sys isa ODESystem + @set! sys.schedule = Schedule(var_eq_matching, dummy_sub) + end + sys = schedule(sys) + @set! state.sys = sys + @set! sys.tearing_state = state + return invalidate_cache!(sys) +end + +""" +# HACK 1 + +Since we don't support array equations, any equation of the sort `x[1:n] ~ f(...)[1:n]` +gets turned into `x[1] ~ f(...)[1], x[2] ~ f(...)[2]`. Repeatedly calling `f` gets +_very_ expensive. this hack performs a limited form of CSE specifically for this case to +avoid the unnecessary cost. This and the below hack are implemented simultaneously + +# HACK 2 + +Add equations for array observed variables. If `p[i] ~ (...)` are equations, add an +equation `p ~ [p[1], p[2], ...]` allow topsort to reorder them only add the new equation +if all `p[i]` are present and the unscalarized form is used in any equation (observed or +not) we first count the number of times the scalarized form of each observed variable +occurs in observed equations (and unknowns if it's split). +""" +function cse_and_array_hacks(obs, subeqs, unknowns, neweqs; cse = true, array = true) + # HACK 1 # mapping of rhs to temporary CSE variable # `f(...) => tmpvar` in above example rhs_to_tempvar = Dict() - # HACK: Add equations for array observed variables. If `p[i] ~ (...)` - # are equations, add an equation `p ~ [p[1], p[2], ...]` - # allow topsort to reorder them - # only add the new equation if all `p[i]` are present and the unscalarized - # form is used in any equation (observed or not) - # we first count the number of times the scalarized form of each observed - # variable occurs in observed equations (and unknowns if it's split). - + # HACK 2 # map of array observed variable (unscalarized) to number of its # scalarized terms that appear in observed equations arr_obs_occurrences = Dict() @@ -613,7 +637,8 @@ function tearing_reassemble(state::TearingState, var_eq_matching, vars!(all_vars, rhs) # HACK 1 - if (!ModelingToolkit.isvariable(rhs) || ModelingToolkit.iscalledparameter(rhs)) && + if cse && + (!ModelingToolkit.isvariable(rhs) || ModelingToolkit.iscalledparameter(rhs)) && iscall(rhs) && operation(rhs) === getindex && Symbolics.shape(rhs) != Symbolics.Unknown() rhs_arr = arguments(rhs)[1] @@ -643,6 +668,7 @@ function tearing_reassemble(state::TearingState, var_eq_matching, end # end HACK 1 + array || continue iscall(lhs) || continue operation(lhs) === getindex || continue Symbolics.shape(lhs) != Symbolics.Unknown() || continue @@ -687,20 +713,7 @@ function tearing_reassemble(state::TearingState, var_eq_matching, # need to re-sort subeqs subeqs = ModelingToolkit.topsort_equations(subeqs, [eq.lhs for eq in subeqs]) - @set! sys.eqs = neweqs - @set! sys.observed = obs - - @set! sys.substitutions = Substitutions(subeqs, deps) - - # Only makes sense for time-dependent - # TODO: generalize to SDE - if sys isa ODESystem - @set! sys.schedule = Schedule(var_eq_matching, dummy_sub) - end - sys = schedule(sys) - @set! state.sys = sys - @set! sys.tearing_state = state - return invalidate_cache!(sys) + return obs, subeqs end # PART OF HACK 1 @@ -733,10 +746,10 @@ new residual equations after tearing. End users are encouraged to call [`structu instead, which calls this function internally. """ function tearing(sys::AbstractSystem, state = TearingState(sys); mm = nothing, - simplify = false, kwargs...) + simplify = false, cse_hack = true, array_hack = true, kwargs...) var_eq_matching, full_var_eq_matching = tearing(state) invalidate_cache!(tearing_reassemble( - state, var_eq_matching, full_var_eq_matching; mm, simplify)) + state, var_eq_matching, full_var_eq_matching; mm, simplify, cse_hack, array_hack)) end """ @@ -758,7 +771,7 @@ Perform index reduction and use the dummy derivative technique to ensure that the system is balanced. """ function dummy_derivative(sys, state = TearingState(sys); simplify = false, - mm = nothing, kwargs...) + mm = nothing, cse_hack = true, array_hack = true, kwargs...) jac = let state = state (eqs, vars) -> begin symeqs = EquationsView(state)[eqs] @@ -782,5 +795,5 @@ function dummy_derivative(sys, state = TearingState(sys); simplify = false, end var_eq_matching = dummy_derivative_graph!(state, jac; state_priority, kwargs...) - tearing_reassemble(state, var_eq_matching; simplify, mm) + tearing_reassemble(state, var_eq_matching; simplify, mm, cse_hack, array_hack) end diff --git a/test/structural_transformation/utils.jl b/test/structural_transformation/utils.jl index ea3552ff0d..04600a7a6b 100644 --- a/test/structural_transformation/utils.jl +++ b/test/structural_transformation/utils.jl @@ -86,3 +86,47 @@ end StructuralTransformations.change_origin] end end + +@testset "array and cse hacks can be disabled" begin + @testset "fully_determined = true" begin + @variables x(t) y(t)[1:2] z(t)[1:2] + @parameters foo(::AbstractVector)[1:2] + _tmp_fn(x) = 2x + @named sys = ODESystem( + [D(x) ~ z[1] + z[2] + foo(z)[1], y[1] ~ 2t, y[2] ~ 3t, z ~ foo(y)], t) + + sys1 = structural_simplify(sys; cse_hack = false) + @test length(observed(sys1)) == 6 + @test !any(observed(sys1)) do eq + iscall(eq.rhs) && + operation(eq.rhs) == StructuralTransformations.getindex_wrapper + end + + sys2 = structural_simplify(sys; array_hack = false) + @test length(observed(sys2)) == 5 + @test !any(observed(sys2)) do eq + iscall(eq.rhs) && operation(eq.rhs) == StructuralTransformations.change_origin + end + end + + @testset "fully_determined = false" begin + @variables x(t) y(t)[1:2] z(t)[1:2] w(t) + @parameters foo(::AbstractVector)[1:2] + _tmp_fn(x) = 2x + @named sys = ODESystem( + [D(x) ~ z[1] + z[2] + foo(z)[1] + w, y[1] ~ 2t, y[2] ~ 3t, z ~ foo(y)], t) + + sys1 = structural_simplify(sys; cse_hack = false, fully_determined = false) + @test length(observed(sys1)) == 6 + @test !any(observed(sys1)) do eq + iscall(eq.rhs) && + operation(eq.rhs) == StructuralTransformations.getindex_wrapper + end + + sys2 = structural_simplify(sys; array_hack = false, fully_determined = false) + @test length(observed(sys2)) == 5 + @test !any(observed(sys2)) do eq + iscall(eq.rhs) && operation(eq.rhs) == StructuralTransformations.change_origin + end + end +end From 877cf03449738dc68f631c664a37e2b67bc49dca Mon Sep 17 00:00:00 2001 From: Christopher Rackauckas Date: Thu, 24 Oct 2024 07:34:08 +0000 Subject: [PATCH 0246/2176] Update test/extensions/bifurcationkit.jl --- test/extensions/bifurcationkit.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/extensions/bifurcationkit.jl b/test/extensions/bifurcationkit.jl index c2dc5685aa..b8c95dca99 100644 --- a/test/extensions/bifurcationkit.jl +++ b/test/extensions/bifurcationkit.jl @@ -37,7 +37,7 @@ let [1.0, 1.0], [-1.0, 1.0], (BifurcationKit.@optic _[1]); - record_from_solution = (x, p) -> x[1]) + record_from_solution = (x, p; k...) -> x[1]) bif_dia_BK = bifurcationdiagram(bprob_BK, PALC(), 2, From 31d8273c60496f5f1b28ecf5b91455d911f4226e Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 24 Oct 2024 13:19:29 +0530 Subject: [PATCH 0247/2176] fix: fix in-place observed function generation --- src/systems/diffeqs/odesystem.jl | 4 ++-- test/odesystem.jl | 12 ++++++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/systems/diffeqs/odesystem.jl b/src/systems/diffeqs/odesystem.jl index ec2c5f8157..6788c64c8e 100644 --- a/src/systems/diffeqs/odesystem.jl +++ b/src/systems/diffeqs/odesystem.jl @@ -576,8 +576,8 @@ function build_explicit_observed_function(sys, ts; iip_fn = build_function(ts, args...; postprocess_fbody = pre, - wrap_code = array_wrapper .∘ wrap_assignments(isscalar, obsexprs) .∘ - mtkparams_wrapper, + wrap_code = mtkparams_wrapper .∘ array_wrapper .∘ + wrap_assignments(isscalar, obsexprs), expression = Val{true})[2] if !expression iip_fn = eval_or_rgf(iip_fn; eval_expression, eval_module) diff --git a/test/odesystem.jl b/test/odesystem.jl index 9446d105e0..27ceafd210 100644 --- a/test/odesystem.jl +++ b/test/odesystem.jl @@ -1447,3 +1447,15 @@ end @parameters p @test_nowarn ODESystem(Equation[], t; parameter_dependencies = [p ~ 1.0], name = :a) end + +@testset "Inplace observed" begin + @variables x(t) + @parameters p[1:2] q + @mtkbuild sys = ODESystem(D(x) ~ sum(p) * x + q * t, t) + prob = ODEProblem(sys, [x => 1.0], (0.0, 1.0), [p => ones(2), q => 2]) + obsfn = ModelingToolkit.build_explicit_observed_function( + sys, [p..., q], return_inplace = true)[2] + buf = zeros(3) + obsfn(buf, prob.u0, prob.p, 0.0) + @test buf ≈ [1.0, 1.0, 2.0] +end From 21d1ce7e3707c890633d363bba21301d3a69bdbd Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 24 Oct 2024 15:05:28 +0530 Subject: [PATCH 0248/2176] fix: construct `initializeprob` if initial value is symbolic --- src/systems/problem_utils.jl | 7 ++++++- test/initializationsystem.jl | 16 ++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/systems/problem_utils.jl b/src/systems/problem_utils.jl index e530e62eed..cc2e959ccd 100644 --- a/src/systems/problem_utils.jl +++ b/src/systems/problem_utils.jl @@ -433,9 +433,14 @@ function process_SciMLProblem( solvablepars = [p for p in parameters(sys) if is_parameter_solvable(p, pmap, defs, guesses)] + has_dependent_unknowns = any(unknowns(sys)) do sym + val = get(op, sym, nothing) + val === nothing && return false + return symbolic_type(val) != NotSymbolic() || is_array_of_symbolics(val) + end if build_initializeprob && (((implicit_dae || has_observed_u0s || !isempty(missing_unknowns) || - !isempty(solvablepars)) && + !isempty(solvablepars) || has_dependent_unknowns) && get_tearing_state(sys) !== nothing) || !isempty(initialization_equations(sys))) && t !== nothing initializeprob = ModelingToolkit.InitializationProblem( diff --git a/test/initializationsystem.jl b/test/initializationsystem.jl index de48821cff..3eddba2b78 100644 --- a/test/initializationsystem.jl +++ b/test/initializationsystem.jl @@ -844,3 +844,19 @@ end isys = ModelingToolkit.generate_initializesystem(sys) @test isequal(defaults(isys)[y], 2x + 1) end + +@testset "Create initializeprob when unknown has dependent value" begin + @variables x(t) y(t) + @mtkbuild sys = ODESystem([D(x) ~ x, D(y) ~ t * y], t; defaults = [x => 2y]) + prob = ODEProblem(sys, [y => 1.0], (0.0, 1.0)) + @test prob.f.initializeprob !== nothing + integ = init(prob) + @test integ[x] ≈ 2.0 + + @variables x(t)[1:2] y(t) + @mtkbuild sys = ODESystem([D(x) ~ x, D(y) ~ t], t; defaults = [x => [y, 3.0]]) + prob = ODEProblem(sys, [y => 1.0], (0.0, 1.0)) + @test prob.f.initializeprob !== nothing + integ = init(prob) + @test integ[x] ≈ [1.0, 3.0] +end From 6081a502fdd102d311ba296a94c4963ea1a61c65 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 24 Oct 2024 15:27:58 +0530 Subject: [PATCH 0249/2176] fix: recursively unwrap arrays of symbolics in `process_SciMLProblem` --- src/systems/problem_utils.jl | 21 +++++++++++++++++++-- test/initial_values.jl | 9 +++++++++ 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/src/systems/problem_utils.jl b/src/systems/problem_utils.jl index cc2e959ccd..ce46f2762a 100644 --- a/src/systems/problem_utils.jl +++ b/src/systems/problem_utils.jl @@ -171,7 +171,24 @@ function to_varmap(vals, varlist::Vector) check_eqs_u0(varlist, varlist, vals) vals = vec(varlist) .=> vec(vals) end - return anydict(unwrap(k) => unwrap(v) for (k, v) in anydict(vals)) + return recursive_unwrap(anydict(vals)) +end + +""" + $(TYPEDSIGNATURES) + +Recursively call `Symbolics.unwrap` on `x`. Useful when `x` is an array of (potentially) +symbolic values, all of which need to be unwrapped. Specializes when `x isa AbstractDict` +to unwrap keys and values, returning an `AnyDict`. +""" +function recursive_unwrap(x::AbstractArray) + symbolic_type(x) == ArraySymbolic() ? unwrap(x) : recursive_unwrap.(x) +end + +recursive_unwrap(x) = unwrap(x) + +function recursive_unwrap(x::AbstractDict) + return anydict(unwrap(k) => recursive_unwrap(v) for (k, v) in x) end """ @@ -410,7 +427,7 @@ function process_SciMLProblem( u0map = to_varmap(u0map, dvs) _pmap = pmap pmap = to_varmap(pmap, ps) - defs = add_toterms(defaults(sys)) + defs = add_toterms(recursive_unwrap(defaults(sys))) cmap, cs = get_cmap(sys) kwargs = NamedTuple(kwargs) diff --git a/test/initial_values.jl b/test/initial_values.jl index 2ff1b0c2a3..12fb7633e9 100644 --- a/test/initial_values.jl +++ b/test/initial_values.jl @@ -119,3 +119,12 @@ end prob = ODEProblem(sys, [], (1.0, 2.0), []) @test prob[x] == 1.0 @test prob.ps[p] == 2.0 + +@testset "Array of symbolics is unwrapped" begin + @variables x(t)[1:2] y(t) + @mtkbuild sys = ODESystem([D(x) ~ x, D(y) ~ t], t; defaults = [x => [y, 3.0]]) + prob = ODEProblem(sys, [y => 1.0], (0.0, 1.0)) + @test eltype(prob.u0) <: Float64 + prob = ODEProblem(sys, [x => [y, 4.0], y => 2.0], (0.0, 1.0)) + @test eltype(prob.u0) <: Float64 +end From 2184598d1d88f0a3f1b1f760d4040d5651f2e5b3 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 24 Oct 2024 15:42:51 +0530 Subject: [PATCH 0250/2176] fix: handle all parameter values from defaults in `split = false` systems --- src/systems/problem_utils.jl | 2 +- test/initial_values.jl | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/systems/problem_utils.jl b/src/systems/problem_utils.jl index ce46f2762a..daf1c164d8 100644 --- a/src/systems/problem_utils.jl +++ b/src/systems/problem_utils.jl @@ -279,7 +279,7 @@ function better_varmap_to_vars(varmap::AbstractDict, vars::Vector; end vals = map(x -> varmap[x], vars) - if container_type <: Union{AbstractDict, Tuple, Nothing} + if container_type <: Union{AbstractDict, Tuple, Nothing, SciMLBase.NullParameters} container_type = Array end diff --git a/test/initial_values.jl b/test/initial_values.jl index 12fb7633e9..9911bab7f4 100644 --- a/test/initial_values.jl +++ b/test/initial_values.jl @@ -128,3 +128,11 @@ prob = ODEProblem(sys, [], (1.0, 2.0), []) prob = ODEProblem(sys, [x => [y, 4.0], y => 2.0], (0.0, 1.0)) @test eltype(prob.u0) <: Float64 end + +@testset "split=false systems with all parameter defaults" begin + @variables x(t) = 1.0 + @parameters p=1.0 q=2.0 r=3.0 + @mtkbuild sys=ODESystem(D(x) ~ p * x + q * t + r, t) split=false + prob = @test_nowarn ODEProblem(sys, [], (0.0, 1.0)) + @test prob.p isa Vector{Float64} +end From 4b1fa7a30dc5ff9b6f11042ac8eadd038fc26e9a Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 24 Oct 2024 15:48:15 +0530 Subject: [PATCH 0251/2176] docs: bump docs compat for `BifurcationKit` --- docs/Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/Project.toml b/docs/Project.toml index 0aefe3d701..16fc8b4b07 100644 --- a/docs/Project.toml +++ b/docs/Project.toml @@ -23,7 +23,7 @@ Unitful = "1986cc42-f94f-5a68-af5c-568840ba703d" [compat] BenchmarkTools = "1.3" -BifurcationKit = "0.3" +BifurcationKit = "0.4" DataInterpolations = "6.5" DifferentialEquations = "7.6" Distributions = "0.25" From 10253638d71ad9101f113b0dcaf1828e43615541 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 24 Oct 2024 20:16:14 +0530 Subject: [PATCH 0252/2176] feat: extend CSE hack to non-observed equations --- .../symbolics_tearing.jl | 45 ++++++++++++++++--- src/systems/nonlinear/initializesystem.jl | 24 +++++++--- test/structural_transformation/utils.jl | 22 +++++++++ 3 files changed, 79 insertions(+), 12 deletions(-) diff --git a/src/structural_transformation/symbolics_tearing.jl b/src/structural_transformation/symbolics_tearing.jl index cabf0415ea..a854acb9b1 100644 --- a/src/structural_transformation/symbolics_tearing.jl +++ b/src/structural_transformation/symbolics_tearing.jl @@ -584,7 +584,7 @@ function tearing_reassemble(state::TearingState, var_eq_matching, end @set! sys.unknowns = unknowns - obs, subeqs = cse_and_array_hacks( + obs, subeqs, deps = cse_and_array_hacks( obs, subeqs, unknowns, neweqs; cse = cse_hack, array = array_hack) @set! sys.eqs = neweqs @@ -637,10 +637,7 @@ function cse_and_array_hacks(obs, subeqs, unknowns, neweqs; cse = true, array = vars!(all_vars, rhs) # HACK 1 - if cse && - (!ModelingToolkit.isvariable(rhs) || ModelingToolkit.iscalledparameter(rhs)) && - iscall(rhs) && operation(rhs) === getindex && - Symbolics.shape(rhs) != Symbolics.Unknown() + if cse && is_getindexed_array(rhs) rhs_arr = arguments(rhs)[1] if !haskey(rhs_to_tempvar, rhs_arr) tempvar = gensym(Symbol(lhs)) @@ -677,6 +674,33 @@ function cse_and_array_hacks(obs, subeqs, unknowns, neweqs; cse = true, array = arr_obs_occurrences[arg1] = cnt + 1 continue end + + # Also do CSE for `equations(sys)` + if cse + for (i, eq) in enumerate(neweqs) + (; lhs, rhs) = eq + is_getindexed_array(rhs) || continue + rhs_arr = arguments(rhs)[1] + if !haskey(rhs_to_tempvar, rhs_arr) + tempvar = gensym(Symbol(lhs)) + N = length(rhs_arr) + tempvar = unwrap(Symbolics.variable( + tempvar; T = Symbolics.symtype(rhs_arr))) + tempvar = setmetadata( + tempvar, Symbolics.ArrayShapeCtx, Symbolics.shape(rhs_arr)) + tempeq = tempvar ~ rhs_arr + rhs_to_tempvar[rhs_arr] = tempvar + push!(obs, tempeq) + push!(subeqs, tempeq) + end + # don't need getindex_wrapper, but do it anyway to know that this + # hack took place + neweq = lhs ~ getindex_wrapper( + rhs_to_tempvar[rhs_arr], Tuple(arguments(rhs)[2:end])) + neweqs[i] = neweq + end + end + # count variables in unknowns if they are scalarized forms of variables # also present as observed. e.g. if `x[1]` is an unknown and `x[2] ~ (..)` # is an observed equation. @@ -713,7 +737,16 @@ function cse_and_array_hacks(obs, subeqs, unknowns, neweqs; cse = true, array = # need to re-sort subeqs subeqs = ModelingToolkit.topsort_equations(subeqs, [eq.lhs for eq in subeqs]) - return obs, subeqs + deps = Vector{Int}[i == 1 ? Int[] : collect(1:(i - 1)) + for i in 1:length(subeqs)] + + return obs, subeqs, deps +end + +function is_getindexed_array(rhs) + (!ModelingToolkit.isvariable(rhs) || ModelingToolkit.iscalledparameter(rhs)) && + iscall(rhs) && operation(rhs) === getindex && + Symbolics.shape(rhs) != Symbolics.Unknown() end # PART OF HACK 1 diff --git a/src/systems/nonlinear/initializesystem.jl b/src/systems/nonlinear/initializesystem.jl index 6c7457e49b..eefe393acc 100644 --- a/src/systems/nonlinear/initializesystem.jl +++ b/src/systems/nonlinear/initializesystem.jl @@ -12,11 +12,10 @@ function generate_initializesystem(sys::ODESystem; algebraic_only = false, check_units = true, check_defguess = false, name = nameof(sys), kwargs...) - trueobs = unhack_observed(observed(sys)) + trueobs, eqs = unhack_observed(observed(sys), equations(sys)) vars = unique([unknowns(sys); getfield.(trueobs, :lhs)]) vars_set = Set(vars) # for efficient in-lookup - eqs = equations(sys) idxs_diff = isdiffeq.(eqs) idxs_alge = .!idxs_diff @@ -329,11 +328,11 @@ end Counteracts the CSE/array variable hacks in `symbolics_tearing.jl` so it works with initialization. """ -function unhack_observed(eqs::Vector{Equation}) +function unhack_observed(obseqs::Vector{Equation}, eqs::Vector{Equation}) subs = Dict() tempvars = Set() rm_idxs = Int[] - for (i, eq) in enumerate(eqs) + for (i, eq) in enumerate(obseqs) iscall(eq.rhs) || continue if operation(eq.rhs) == StructuralTransformations.change_origin push!(rm_idxs, i) @@ -347,14 +346,27 @@ function unhack_observed(eqs::Vector{Equation}) end for (i, eq) in enumerate(eqs) + iscall(eq.rhs) || continue + if operation(eq.rhs) == StructuralTransformations.getindex_wrapper + var, idxs = arguments(eq.rhs) + subs[eq.rhs] = var[idxs...] + push!(tempvars, var) + end + end + + for (i, eq) in enumerate(obseqs) if eq.lhs in tempvars subs[eq.lhs] = eq.rhs push!(rm_idxs, i) end end - eqs = eqs[setdiff(eachindex(eqs), rm_idxs)] - return map(eqs) do eq + obseqs = obseqs[setdiff(eachindex(obseqs), rm_idxs)] + obseqs = map(obseqs) do eq + fixpoint_sub(eq.lhs, subs) ~ fixpoint_sub(eq.rhs, subs) + end + eqs = map(eqs) do eq fixpoint_sub(eq.lhs, subs) ~ fixpoint_sub(eq.rhs, subs) end + return obseqs, eqs end diff --git a/test/structural_transformation/utils.jl b/test/structural_transformation/utils.jl index 04600a7a6b..2704559f72 100644 --- a/test/structural_transformation/utils.jl +++ b/test/structural_transformation/utils.jl @@ -85,6 +85,28 @@ end iscall(eq.rhs) && operation(eq.rhs) in [StructuralTransformations.getindex_wrapper, StructuralTransformations.change_origin] end + + @testset "CSE hack in equations(sys)" begin + val[] = 0 + @variables z(t)[1:2] + @mtkbuild sys = ODESystem( + [D(y) ~ foo(x), D(x) ~ sum(y), zeros(2) ~ foo(prod(z))], t) + @test length(equations(sys)) == 5 + @test length(observed(sys)) == 2 + prob = ODEProblem( + sys, [y => ones(2), z => 2ones(2), x => 3.0], (0.0, 1.0), [foo => _tmp_fn2]) + @test_nowarn prob.f(prob.u0, prob.p, 0.0) + @test val[] == 2 + + isys = ModelingToolkit.generate_initializesystem(sys) + @test length(unknowns(isys)) == 5 + @test length(equations(isys)) == 2 + @test !any(equations(isys)) do eq + iscall(eq.rhs) && + operation(eq.rhs) in [StructuralTransformations.getindex_wrapper, + StructuralTransformations.change_origin] + end + end end @testset "array and cse hacks can be disabled" begin From 93c1e8fadf792f663a052adca749c082df61f25e Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 25 Oct 2024 13:37:59 +0530 Subject: [PATCH 0253/2176] fix: fix `u0_constructor` for `DDEProblem`/`SDDEProblem` --- src/systems/diffeqs/abstractodesystem.jl | 8 ++++++++ test/dde.jl | 10 +++++++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/systems/diffeqs/abstractodesystem.jl b/src/systems/diffeqs/abstractodesystem.jl index 94650d43a8..8d9e0b5381 100644 --- a/src/systems/diffeqs/abstractodesystem.jl +++ b/src/systems/diffeqs/abstractodesystem.jl @@ -879,6 +879,7 @@ function DiffEqBase.DDEProblem{iip}(sys::AbstractODESystem, u0map = [], check_length = true, eval_expression = false, eval_module = @__MODULE__, + u0_constructor = identity, kwargs...) where {iip} if !iscomplete(sys) error("A completed system is required. Call `complete` or `structural_simplify` on the system before creating a `DDEProblem`") @@ -892,6 +893,9 @@ function DiffEqBase.DDEProblem{iip}(sys::AbstractODESystem, u0map = [], h(p, t) = h_oop(p, t) h(p::MTKParameters, t) = h_oop(p..., t) u0 = h(p, tspan[1]) + if u0 !== nothing + u0 = u0_constructor(u0) + end cbs = process_events(sys; callback, eval_expression, eval_module, kwargs...) kwargs = filter_kwargs(kwargs) @@ -914,6 +918,7 @@ function DiffEqBase.SDDEProblem{iip}(sys::AbstractODESystem, u0map = [], sparsenoise = nothing, eval_expression = false, eval_module = @__MODULE__, + u0_constructor = identity, kwargs...) where {iip} if !iscomplete(sys) error("A completed system is required. Call `complete` or `structural_simplify` on the system before creating a `SDDEProblem`") @@ -929,6 +934,9 @@ function DiffEqBase.SDDEProblem{iip}(sys::AbstractODESystem, u0map = [], h(p::MTKParameters, t) = h_oop(p..., t) h(out, p::MTKParameters, t) = h_iip(out, p..., t) u0 = h(p, tspan[1]) + if u0 !== nothing + u0 = u0_constructor(u0) + end cbs = process_events(sys; callback, eval_expression, eval_module, kwargs...) kwargs = filter_kwargs(kwargs) diff --git a/test/dde.jl b/test/dde.jl index f5e72ee1bb..ec076fba53 100644 --- a/test/dde.jl +++ b/test/dde.jl @@ -1,4 +1,4 @@ -using ModelingToolkit, DelayDiffEq, Test +using ModelingToolkit, DelayDiffEq, StaticArrays, Test using SymbolicIndexingInterface: is_markovian using ModelingToolkit: t_nounits as t, D_nounits as D @@ -89,6 +89,10 @@ eqs = [D(x(t)) ~ a * x(t) + b * x(t - τ) + c + (α * x(t) + γ) * η] prob_mtk = SDDEProblem(sys, [x(t) => 1.0 + t], tspan; constant_lags = (τ,)); @test_nowarn sol_mtk = solve(prob_mtk, RKMil()) +prob_sa = SDDEProblem( + sys, [x(t) => 1.0 + t], tspan; constant_lags = (τ,), u0_constructor = SVector{1}) +@test prob_sa.u0 isa SVector{4, Float64} + @parameters x(..) a function oscillator(; name, k = 1.0, τ = 0.01) @@ -126,6 +130,10 @@ obsfn = ModelingToolkit.build_explicit_observed_function( @test_nowarn sol[[sys.osc1.delx, sys.osc2.delx]] @test sol[sys.osc1.delx] ≈ sol(sol.t .- 0.01; idxs = sys.osc1.x).u +prob_sa = DDEProblem(sys, [], (0.0, 10.0); constant_lags = [sys.osc1.τ, sys.osc2.τ], + u0_constructor = SVector{4}) +@test prob_sa.u0 isa SVector{4, Float64} + @testset "DDE observed with array variables" begin @component function valve(; name) @parameters begin From 8c1793109dc6a7e4e423ddb08abb8a51f9806ca2 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 25 Oct 2024 15:56:31 +0530 Subject: [PATCH 0254/2176] test: fix `u0_constructor` test for `SDDEProblem` --- test/dde.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/dde.jl b/test/dde.jl index ec076fba53..fb099ab8c6 100644 --- a/test/dde.jl +++ b/test/dde.jl @@ -91,7 +91,7 @@ prob_mtk = SDDEProblem(sys, [x(t) => 1.0 + t], tspan; constant_lags = (τ,)); prob_sa = SDDEProblem( sys, [x(t) => 1.0 + t], tspan; constant_lags = (τ,), u0_constructor = SVector{1}) -@test prob_sa.u0 isa SVector{4, Float64} +@test prob_sa.u0 isa SVector{1, Float64} @parameters x(..) a From 7f7f65cc3e9dde8fec99037f4df27221088ed818 Mon Sep 17 00:00:00 2001 From: Benjamin Chung Date: Fri, 25 Oct 2024 10:17:26 -0700 Subject: [PATCH 0255/2176] Clear up some of the documentation language --- docs/src/basics/Events.md | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/docs/src/basics/Events.md b/docs/src/basics/Events.md index 4c9803ef57..90b1b4036d 100644 --- a/docs/src/basics/Events.md +++ b/docs/src/basics/Events.md @@ -383,14 +383,13 @@ It can be seen that the timeseries for `c` is not saved. The `ImperativeAffect` can be used as an alternative to the aforementioned functional affect form. Note that `ImperativeAffect` is still experimental; to emphasize this, we do not export it and it should be -included as `ModelingToolkit.ImperativeAffect`. It abstracts over how values are written back to the -system, simplifying the definitions and (in the future) allowing assignments back to observed values -by solving the nonlinear reinitialization problem afterwards. +included as `ModelingToolkit.ImperativeAffect`. `ImperativeAffect` aims to simplify the manipulation of +system state. We will use two examples to describe `ImperativeAffect`: a simple heater and a quadrature encoder. These examples will also demonstrate advanced usage of `ModelingToolkit.SymbolicContinuousCallback`, -the low-level interface that the aforementioned tuple form converts into and allows control over the -exact SciMLCallbacks event that is generated for a continuous event. +the low-level interface of the tuple form converts into that allows control over the SciMLBase-level +event that is generated for a continuous event. ### [Heater](@id heater_events) From e750219fcb2d746b5b3bf2c52bcf22c19c48955c Mon Sep 17 00:00:00 2001 From: Benjamin Chung Date: Fri, 25 Oct 2024 10:49:42 -0700 Subject: [PATCH 0256/2176] Format --- docs/src/basics/Events.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/src/basics/Events.md b/docs/src/basics/Events.md index 90b1b4036d..23e1e6d7d1 100644 --- a/docs/src/basics/Events.md +++ b/docs/src/basics/Events.md @@ -383,12 +383,12 @@ It can be seen that the timeseries for `c` is not saved. The `ImperativeAffect` can be used as an alternative to the aforementioned functional affect form. Note that `ImperativeAffect` is still experimental; to emphasize this, we do not export it and it should be -included as `ModelingToolkit.ImperativeAffect`. `ImperativeAffect` aims to simplify the manipulation of +included as `ModelingToolkit.ImperativeAffect`. `ImperativeAffect` aims to simplify the manipulation of system state. We will use two examples to describe `ImperativeAffect`: a simple heater and a quadrature encoder. These examples will also demonstrate advanced usage of `ModelingToolkit.SymbolicContinuousCallback`, -the low-level interface of the tuple form converts into that allows control over the SciMLBase-level +the low-level interface of the tuple form converts into that allows control over the SciMLBase-level event that is generated for a continuous event. ### [Heater](@id heater_events) From 0af572f64648b3885f5491fa6a66050586e2a002 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Sat, 26 Oct 2024 13:56:14 +0530 Subject: [PATCH 0257/2176] build: bump minor version --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index fbf612261d..9dfd9ed0d3 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "ModelingToolkit" uuid = "961ee093-0014-501f-94e3-6117800e7a78" authors = ["Yingbo Ma ", "Chris Rackauckas and contributors"] -version = "9.47.0" +version = "9.48.0" [deps] AbstractTrees = "1520ce14-60c1-5f80-bbc7-55ef81b5835c" From d95a3f7252b8d250b053540bee61a2c3eb5a948e Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 25 Oct 2024 21:48:36 +0530 Subject: [PATCH 0258/2176] feat: support rational functions in `HomotopyContinuationProblem` --- ext/MTKHomotopyContinuationExt.jl | 100 ++++++++++++++++++++--- src/systems/nonlinear/nonlinearsystem.jl | 8 +- test/extensions/homotopy_continuation.jl | 44 +++++++++- 3 files changed, 136 insertions(+), 16 deletions(-) diff --git a/ext/MTKHomotopyContinuationExt.jl b/ext/MTKHomotopyContinuationExt.jl index 81d9a840c9..e4a9243ff9 100644 --- a/ext/MTKHomotopyContinuationExt.jl +++ b/ext/MTKHomotopyContinuationExt.jl @@ -2,7 +2,7 @@ module MTKHomotopyContinuationExt using ModelingToolkit using ModelingToolkit.SciMLBase -using ModelingToolkit.Symbolics: unwrap, symtype +using ModelingToolkit.Symbolics: unwrap, symtype, BasicSymbolic, simplify_fractions using ModelingToolkit.SymbolicIndexingInterface using ModelingToolkit.DocStringExtensions using HomotopyContinuation @@ -27,7 +27,7 @@ function is_polynomial(x, wrt) contains_variable(x, wrt) || return true any(isequal(x), wrt) && return true - if operation(x) in (*, +, -) + if operation(x) in (*, +, -, /) return all(y -> is_polynomial(y, wrt), arguments(x)) end if operation(x) == (^) @@ -57,6 +57,57 @@ end """ $(TYPEDSIGNATURES) +Given a `x`, a polynomial in variables in `wrt` which may contain rational functions, +express `x` as a single rational function with polynomial `num` and denominator `den`. +Return `(num, den)`. +""" +function handle_rational_polynomials(x, wrt) + x = unwrap(x) + symbolic_type(x) == NotSymbolic() && return x, 1 + iscall(x) || return x, 1 + contains_variable(x, wrt) || return x, 1 + any(isequal(x), wrt) && return x, 1 + + # simplify_fractions cancels out some common factors + # and expands (a / b)^c to a^c / b^c, so we only need + # to handle these cases + x = simplify_fractions(x) + op = operation(x) + args = arguments(x) + + if op == / + # numerator and denominator are trivial + num, den = args + # but also search for rational functions in numerator + n, d = handle_rational_polynomials(num, wrt) + num, den = n, den * d + elseif op == + + num = 0 + den = 1 + + # we don't need to do common denominator + # because we don't care about cases where denominator + # is zero. The expression is zero when all the numerators + # are zero. + for arg in args + n, d = handle_rational_polynomials(arg, wrt) + num += n + den *= d + end + else + return x, 1 + end + # if the denominator isn't a polynomial in `wrt`, better to not include it + # to reduce the size of the gcd polynomial + if !contains_variable(den, wrt) + return num / den, 1 + end + return num, den +end + +""" +$(TYPEDSIGNATURES) + Convert `expr` from a symbolics expression to one that uses `HomotopyContinuation.ModelKit`. """ function symbolics_to_hc(expr) @@ -139,51 +190,74 @@ function MTK.HomotopyContinuationProblem( dvs = unknowns(sys) eqs = equations(sys) - for eq in eqs + denoms = [] + eqs2 = map(eqs) do eq if !is_polynomial(eq.lhs, dvs) || !is_polynomial(eq.rhs, dvs) error("Equation $eq is not a polynomial in the unknowns. See warnings for further details.") end + num, den = handle_rational_polynomials(eq.rhs - eq.lhs, dvs) + push!(denoms, den) + return 0 ~ num end - nlfn, u0, p = MTK.process_SciMLProblem(NonlinearFunction{true}, sys, u0map, parammap; + sys2 = MTK.@set sys.eqs = eqs2 + + nlfn, u0, p = MTK.process_SciMLProblem(NonlinearFunction{true}, sys2, u0map, parammap; jac = true, eval_expression, eval_module) + denominator = MTK.build_explicit_observed_function(sys, denoms) + hvars = symbolics_to_hc.(dvs) mtkhsys = MTKHomotopySystem(nlfn.f, p, nlfn.jac, hvars, length(eqs)) obsfn = MTK.ObservedFunctionCache(sys; eval_expression, eval_module) - return MTK.HomotopyContinuationProblem(u0, mtkhsys, sys, obsfn) + return MTK.HomotopyContinuationProblem(u0, mtkhsys, denominator, sys, obsfn) end """ $(TYPEDSIGNATURES) Solve a `HomotopyContinuationProblem`. Ignores the algorithm passed to it, and always -uses `HomotopyContinuation.jl`. All keyword arguments are forwarded to -`HomotopyContinuation.solve`. The original solution as returned by `HomotopyContinuation.jl` -will be available in the `.original` field of the returned `NonlinearSolution`. +uses `HomotopyContinuation.jl`. All keyword arguments except the ones listed below are +forwarded to `HomotopyContinuation.solve`. The original solution as returned by +`HomotopyContinuation.jl` will be available in the `.original` field of the returned +`NonlinearSolution`. All keyword arguments have their default values in HomotopyContinuation.jl, except `show_progress` which defaults to `false`. + +Extra keyword arguments: +- `denominator_abstol`: In case `prob` is solving a rational function, roots which cause + the denominator to be below `denominator_abstol` will be discarded. """ function CommonSolve.solve(prob::MTK.HomotopyContinuationProblem, - alg = nothing; show_progress = false, kwargs...) + alg = nothing; show_progress = false, denominator_abstol = 1e-8, kwargs...) sol = HomotopyContinuation.solve( prob.homotopy_continuation_system; show_progress, kwargs...) realsols = HomotopyContinuation.results(sol; only_real = true) if isempty(realsols) u = state_values(prob) - resid = prob.homotopy_continuation_system(u) retcode = SciMLBase.ReturnCode.ConvergenceFailure else + T = eltype(state_values(prob)) distance, idx = findmin(realsols) do result + if any(<=(denominator_abstol), + prob.denominator(real.(result.solution), parameter_values(prob))) + return T(Inf) + end norm(result.solution - state_values(prob)) end - u = real.(realsols[idx].solution) - resid = prob.homotopy_continuation_system(u) - retcode = SciMLBase.ReturnCode.Success + # all roots cause denominator to be zero + if isinf(distance) + u = state_values(prob) + retcode = SciMLBase.ReturnCode.Infeasible + else + u = real.(realsols[idx].solution) + retcode = SciMLBase.ReturnCode.Success + end end + resid = prob.homotopy_continuation_system(u) return SciMLBase.build_solution( prob, :HomotopyContinuation, u, resid; retcode, original = sol) diff --git a/src/systems/nonlinear/nonlinearsystem.jl b/src/systems/nonlinear/nonlinearsystem.jl index 01a250d46a..bdad40d52f 100644 --- a/src/systems/nonlinear/nonlinearsystem.jl +++ b/src/systems/nonlinear/nonlinearsystem.jl @@ -573,7 +573,7 @@ A type of Nonlinear problem which specializes on polynomial systems and uses HomotopyContinuation.jl to solve the system. Requires importing HomotopyContinuation.jl to create and solve. """ -struct HomotopyContinuationProblem{uType, H, O} <: +struct HomotopyContinuationProblem{uType, H, D, O} <: SciMLBase.AbstractNonlinearProblem{uType, true} """ The initial values of states in the system. If there are multiple real roots of @@ -586,6 +586,12 @@ struct HomotopyContinuationProblem{uType, H, O} <: """ homotopy_continuation_system::H """ + A function with signature `(u, p) -> resid`. In case of rational functions, this + is used to rule out roots of the system which would cause the denominator to be + zero. + """ + denominator::D + """ The `NonlinearSystem` used to create this problem. Used for symbolic indexing. """ sys::NonlinearSystem diff --git a/test/extensions/homotopy_continuation.jl b/test/extensions/homotopy_continuation.jl index ceabb6b6e3..6d7279899f 100644 --- a/test/extensions/homotopy_continuation.jl +++ b/test/extensions/homotopy_continuation.jl @@ -82,7 +82,47 @@ end @mtkbuild sys = NonlinearSystem([x^x - x ~ 0]) @test_warn ["Exponent", "unknowns"] @test_throws "not a polynomial" HomotopyContinuationProblem( sys, []) - @mtkbuild sys = NonlinearSystem([((x^2) / (x + 3))^2 + x ~ 0]) - @test_warn ["Base", "not a polynomial", "Unrecognized operation", "/"] @test_throws "not a polynomial" HomotopyContinuationProblem( + @mtkbuild sys = NonlinearSystem([((x^2) / sin(x))^2 + x ~ 0]) + @test_warn ["Unrecognized", "sin"] @test_throws "not a polynomial" HomotopyContinuationProblem( sys, []) end + +@testset "Rational functions" begin + @variables x=2.0 y=2.0 + @parameters n = 4 + @mtkbuild sys = NonlinearSystem([ + 0 ~ (x^2 - n * x + n) * (x - 1) / (x - 2) / (x - 3) + ]) + prob = HomotopyContinuationProblem(sys, []) + sol = solve(prob; threading = false) + @test sol[x] ≈ 1.0 + p = parameter_values(prob) + for invalid in [2.0, 3.0] + @test prob.denominator([invalid], p)[1] <= 1e-8 + end + + @named sys = NonlinearSystem( + [ + 0 ~ (x - 2) / (x - 4) + ((x - 3) / (y - 7)) / ((x^2 - 4x + y) / (x - 2.5)), + 0 ~ ((y - 3) / (y - 4)) * (n / (y - 5)) + ((x - 1.5) / (x - 5.5))^2 + ], + [x, y], + [n]) + sys = complete(sys) + prob = HomotopyContinuationProblem(sys, []) + sol = solve(prob; threading = false) + disallowed_x = [4, 5.5] + disallowed_y = [7, 5, 4] + @test all(!isapprox(sol[x]; atol = 1e-8), disallowed_x) + @test all(!isapprox(sol[y]; atol = 1e-8), disallowed_y) + @test sol[x^2 - 4x + y] >= 1e-8 + + p = parameter_values(prob) + for val in disallowed_x + @test any(<=(1e-8), prob.denominator([val, 2.0], p)) + end + for val in disallowed_y + @test any(<=(1e-8), prob.denominator([2.0, val], p)) + end + @test prob.denominator([2.0, 4.0], p)[1] <= 1e-8 +end From 7aae63db19b3bf6168d20881875a36fc65ec8bd9 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Sat, 26 Oct 2024 13:58:35 +0530 Subject: [PATCH 0259/2176] build: bump SciMLBase compat --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 9dfd9ed0d3..958772cb05 100644 --- a/Project.toml +++ b/Project.toml @@ -121,7 +121,7 @@ REPL = "1" RecursiveArrayTools = "3.26" Reexport = "0.2, 1" RuntimeGeneratedFunctions = "0.5.9" -SciMLBase = "2.56.1" +SciMLBase = "2.57.1" SciMLStructures = "1.0" Serialization = "1" Setfield = "0.7, 0.8, 1" From 5a94e137d10673b8cec2a35c664892745fe66370 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 25 Oct 2024 15:08:21 +0530 Subject: [PATCH 0260/2176] fix: fix `getproperty`, `hasproperty` for simplified hierarchical systems --- src/systems/abstractsystem.jl | 41 +++++++++++------- src/systems/systems.jl | 2 +- test/components.jl | 36 +++++++++++++++ test/model_parsing.jl | 20 ++++----- test/runtests.jl | 82 +++++++++++++++++------------------ 5 files changed, 113 insertions(+), 68 deletions(-) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index ada8a2a056..f114bc3fa7 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -921,7 +921,7 @@ the global structure of the system. One property to note is that if a system is complete, the system will no longer namespace its subsystems or variables, i.e. `isequal(complete(sys).v.i, v.i)`. """ -function complete(sys::AbstractSystem; split = true) +function complete(sys::AbstractSystem; split = true, flatten = true) if !(sys isa JumpSystem) newunknowns = OrderedSet() newparams = OrderedSet() @@ -931,9 +931,21 @@ function complete(sys::AbstractSystem; split = true) # `GlobalScope`d unknowns will be picked up and added there @set! sys.ps = unique!(vcat(get_ps(sys), collect(newparams))) end + if flatten + if sys isa Union{OptimizationSystem, ConstraintsSystem, JumpSystem} + newsys = sys + else + newsys = expand_connections(sys) + end + newsys = ModelingToolkit.flatten(newsys) + if has_parent(newsys) && get_parent(sys) === nothing + @set! newsys.parent = complete(sys; split = false, flatten = false) + end + sys = newsys + end if split && has_index_cache(sys) @set! sys.index_cache = IndexCache(sys) - all_ps = parameters(sys) + all_ps = get_ps(sys) if !isempty(all_ps) # reorder parameters by portions ps_split = reorder_parameters(sys, all_ps) @@ -1102,6 +1114,9 @@ function Base.propertynames(sys::AbstractSystem; private = false) if private return fieldnames(typeof(sys)) else + if has_parent(sys) && (parent = get_parent(sys); parent !== nothing) + sys = parent + end names = Symbol[] for s in get_systems(sys) push!(names, getname(s)) @@ -1144,20 +1159,6 @@ function getvar(sys::AbstractSystem, name::Symbol; namespace = !iscomplete(sys)) avs = get_var_to_name(sys) v = get(avs, name, nothing) v === nothing || return namespace ? renamespace(sys, v) : v - else - sts = get_unknowns(sys) - i = findfirst(x -> getname(x) == name, sts) - if i !== nothing - return namespace ? renamespace(sys, sts[i]) : sts[i] - end - - if has_ps(sys) - ps = get_ps(sys) - i = findfirst(x -> getname(x) == name, ps) - if i !== nothing - return namespace ? renamespace(sys, ps[i]) : ps[i] - end - end end sts = get_unknowns(sys) @@ -1166,6 +1167,14 @@ function getvar(sys::AbstractSystem, name::Symbol; namespace = !iscomplete(sys)) return namespace ? renamespace(sys, sts[i]) : sts[i] end + if has_ps(sys) + ps = get_ps(sys) + i = findfirst(x -> getname(x) == name, ps) + if i !== nothing + return namespace ? renamespace(sys, ps[i]) : ps[i] + end + end + if has_observed(sys) obs = get_observed(sys) i = findfirst(x -> getname(x.lhs) == name, obs) diff --git a/src/systems/systems.jl b/src/systems/systems.jl index 78cc78989f..848980605f 100644 --- a/src/systems/systems.jl +++ b/src/systems/systems.jl @@ -47,7 +47,7 @@ function structural_simplify( """) end if newsys isa ODESystem || has_parent(newsys) - @set! newsys.parent = complete(sys; split) + @set! newsys.parent = complete(sys; split, flatten = false) end newsys = complete(newsys; split) if has_defaults(newsys) && (defs = get_defaults(newsys)) !== nothing diff --git a/test/components.jl b/test/components.jl index 965578e0c5..3daf1b9100 100644 --- a/test/components.jl +++ b/test/components.jl @@ -314,3 +314,39 @@ sol = solve(prob, Tsit5()) end @test string(Base.doc(Pin2)) == "Hey there, Pin2!\n" end + +@testset "Issue#3016 Hierarchical indexing" begin + @mtkmodel Inner begin + @parameters begin + p + end + end + @mtkmodel Outer begin + @components begin + inner = Inner() + end + @variables begin + x(t) + end + @equations begin + x ~ inner.p + end + end + + @named outer = Outer() + simp = structural_simplify(outer) + + @test sort(propertynames(outer)) == [:inner, :t, :x] + @test propertynames(simp) == propertynames(outer) + @test sort(propertynames(outer.inner)) == [:p, :t] + @test propertynames(simp.inner) == propertynames(outer.inner) + + for sym in (:t, :x) + @test_nowarn getproperty(simp, sym) + @test_nowarn getproperty(outer, sym) + end + @test_nowarn simp.inner.p + @test_nowarn outer.inner.p + @test_throws ArgumentError simp.inner₊p + @test_throws ArgumentError outer.inner₊p +end diff --git a/test/model_parsing.jl b/test/model_parsing.jl index 5126bf5e66..2dcb8e3f11 100644 --- a/test/model_parsing.jl +++ b/test/model_parsing.jl @@ -562,11 +562,11 @@ end end @named if_in_sys = InsideTheBlock() - if_in_sys = complete(if_in_sys) + if_in_sys = complete(if_in_sys; flatten = false) @named elseif_in_sys = InsideTheBlock(flag = 2) - elseif_in_sys = complete(elseif_in_sys) + elseif_in_sys = complete(elseif_in_sys; flatten = false) @named else_in_sys = InsideTheBlock(flag = 3) - else_in_sys = complete(else_in_sys) + else_in_sys = complete(else_in_sys; flatten = false) @test sort(getname.(parameters(if_in_sys))) == [:eq, :if_parameter] @test sort(getname.(parameters(elseif_in_sys))) == [:elseif_parameter, :eq] @@ -653,13 +653,13 @@ end end @named if_out_sys = OutsideTheBlock(condition = 1) - if_out_sys = complete(if_out_sys) + if_out_sys = complete(if_out_sys; flatten = false) @named elseif_out_sys = OutsideTheBlock(condition = 2) - elseif_out_sys = complete(elseif_out_sys) + elseif_out_sys = complete(elseif_out_sys; flatten = false) @named else_out_sys = OutsideTheBlock(condition = 10) - else_out_sys = complete(else_out_sys) + else_out_sys = complete(else_out_sys; flatten = false) @named ternary_out_sys = OutsideTheBlock(condition = 4) - else_out_sys = complete(else_out_sys) + else_out_sys = complete(else_out_sys; flatten = false) @test getname.(parameters(if_out_sys)) == [:if_parameter, :default_parameter] @test getname.(parameters(elseif_out_sys)) == [:elseif_parameter, :default_parameter] @@ -708,10 +708,10 @@ end end @named ternary_true = TernaryBranchingOutsideTheBlock() - ternary_true = complete(ternary_true) + ternary_true = complete(ternary_true; flatten = false) @named ternary_false = TernaryBranchingOutsideTheBlock(condition = false) - ternary_false = complete(ternary_false) + ternary_false = complete(ternary_false; flatten = false) @test getname.(parameters(ternary_true)) == [:ternary_parameter_true] @test getname.(parameters(ternary_false)) == [:ternary_parameter_false] @@ -758,7 +758,7 @@ end end @named component = Component() - component = complete(component) + component = complete(component; flatten = false) @test nameof.(ModelingToolkit.get_systems(component)) == [ :comprehension_1, diff --git a/test/runtests.jl b/test/runtests.jl index c7edbfaaf5..5db3308540 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -19,48 +19,48 @@ end @time begin if GROUP == "All" || GROUP == "InterfaceI" @testset "InterfaceI" begin - @safetestset "Linear Algebra Test" include("linalg.jl") - @safetestset "AbstractSystem Test" include("abstractsystem.jl") - @safetestset "Variable Scope Tests" include("variable_scope.jl") - @safetestset "Symbolic Parameters Test" include("symbolic_parameters.jl") - @safetestset "Parsing Test" include("variable_parsing.jl") - @safetestset "Simplify Test" include("simplify.jl") - @safetestset "Direct Usage Test" include("direct.jl") - @safetestset "System Linearity Test" include("linearity.jl") - @safetestset "Input Output Test" include("input_output_handling.jl") - @safetestset "Clock Test" include("clock.jl") - @safetestset "ODESystem Test" include("odesystem.jl") - @safetestset "Dynamic Quantities Test" include("dq_units.jl") - @safetestset "Unitful Quantities Test" include("units.jl") - @safetestset "Mass Matrix Test" include("mass_matrix.jl") - @safetestset "InitializationSystem Test" include("initializationsystem.jl") - @safetestset "Guess Propagation" include("guess_propagation.jl") - @safetestset "Hierarchical Initialization Equations" include("hierarchical_initialization_eqs.jl") - @safetestset "Reduction Test" include("reduction.jl") - @safetestset "Split Parameters Test" include("split_parameters.jl") - @safetestset "StaticArrays Test" include("static_arrays.jl") - @safetestset "Components Test" include("components.jl") - @safetestset "Model Parsing Test" include("model_parsing.jl") - @safetestset "Error Handling" include("error_handling.jl") - @safetestset "StructuralTransformations" include("structural_transformation/runtests.jl") - @safetestset "State Selection Test" include("state_selection.jl") - @safetestset "Symbolic Event Test" include("symbolic_events.jl") - @safetestset "Stream Connect Test" include("stream_connectors.jl") - @safetestset "Domain Connect Test" include("domain_connectors.jl") - @safetestset "Lowering Integration Test" include("lowering_solving.jl") - @safetestset "Dependency Graph Test" include("dep_graphs.jl") - @safetestset "Function Registration Test" include("function_registration.jl") - @safetestset "Precompiled Modules Test" include("precompile_test.jl") - @safetestset "DAE Jacobians Test" include("dae_jacobian.jl") - @safetestset "Jacobian Sparsity" include("jacobiansparsity.jl") + # @safetestset "Linear Algebra Test" include("linalg.jl") + # @safetestset "AbstractSystem Test" include("abstractsystem.jl") + # @safetestset "Variable Scope Tests" include("variable_scope.jl") + # @safetestset "Symbolic Parameters Test" include("symbolic_parameters.jl") + # @safetestset "Parsing Test" include("variable_parsing.jl") + # @safetestset "Simplify Test" include("simplify.jl") + # @safetestset "Direct Usage Test" include("direct.jl") + # @safetestset "System Linearity Test" include("linearity.jl") + # @safetestset "Input Output Test" include("input_output_handling.jl") + # @safetestset "Clock Test" include("clock.jl") + # @safetestset "ODESystem Test" include("odesystem.jl") + # @safetestset "Dynamic Quantities Test" include("dq_units.jl") + # @safetestset "Unitful Quantities Test" include("units.jl") + # @safetestset "Mass Matrix Test" include("mass_matrix.jl") + # @safetestset "InitializationSystem Test" include("initializationsystem.jl") + # @safetestset "Guess Propagation" include("guess_propagation.jl") + # @safetestset "Hierarchical Initialization Equations" include("hierarchical_initialization_eqs.jl") + # @safetestset "Reduction Test" include("reduction.jl") + # @safetestset "Split Parameters Test" include("split_parameters.jl") + # @safetestset "StaticArrays Test" include("static_arrays.jl") + # @safetestset "Components Test" include("components.jl") + # @safetestset "Model Parsing Test" include("model_parsing.jl") + # @safetestset "Error Handling" include("error_handling.jl") + # @safetestset "StructuralTransformations" include("structural_transformation/runtests.jl") + # @safetestset "State Selection Test" include("state_selection.jl") + # @safetestset "Symbolic Event Test" include("symbolic_events.jl") + # @safetestset "Stream Connect Test" include("stream_connectors.jl") + # @safetestset "Domain Connect Test" include("domain_connectors.jl") + # @safetestset "Lowering Integration Test" include("lowering_solving.jl") + # @safetestset "Dependency Graph Test" include("dep_graphs.jl") + # @safetestset "Function Registration Test" include("function_registration.jl") + # @safetestset "Precompiled Modules Test" include("precompile_test.jl") + # @safetestset "DAE Jacobians Test" include("dae_jacobian.jl") + # @safetestset "Jacobian Sparsity" include("jacobiansparsity.jl") @safetestset "Modelingtoolkitize Test" include("modelingtoolkitize.jl") - @safetestset "FuncAffect Test" include("funcaffect.jl") - @safetestset "Constants Test" include("constants.jl") - @safetestset "Parameter Dependency Test" include("parameter_dependencies.jl") - @safetestset "Generate Custom Function Test" include("generate_custom_function.jl") - @safetestset "Initial Values Test" include("initial_values.jl") - @safetestset "Equation Type Accessors Test" include("equation_type_accessors.jl") - @safetestset "Equations with complex values" include("complex.jl") + # @safetestset "FuncAffect Test" include("funcaffect.jl") + # @safetestset "Constants Test" include("constants.jl") + # @safetestset "Parameter Dependency Test" include("parameter_dependencies.jl") + # @safetestset "Generate Custom Function Test" include("generate_custom_function.jl") + # @safetestset "Initial Values Test" include("initial_values.jl") + # @safetestset "Equation Type Accessors Test" include("equation_type_accessors.jl") + # @safetestset "Equations with complex values" include("complex.jl") end end From f9ff0a20000feb454dc4ea6408254413d6d43b0f Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Sat, 26 Oct 2024 23:31:23 +0530 Subject: [PATCH 0261/2176] fix: add `flatten` for `OptimizationSystem` --- src/systems/optimization/optimizationsystem.jl | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/systems/optimization/optimizationsystem.jl b/src/systems/optimization/optimizationsystem.jl index 271e0073ce..2e796867ae 100644 --- a/src/systems/optimization/optimizationsystem.jl +++ b/src/systems/optimization/optimizationsystem.jl @@ -141,6 +141,22 @@ function OptimizationSystem(op, unknowns, ps; checks = checks) end +function flatten(sys::OptimizationSystem) + systems = get_systems(sys) + isempty(systems) && return sys + + return OptimizationSystem( + objective(sys), + unknowns(sys), + parameters(sys); + observed = observed(sys), + constraints = constraints(sys), + defaults = defaults(sys), + name = nameof(sys), + checks = false + ) +end + function calculate_gradient(sys::OptimizationSystem) expand_derivatives.(gradient(objective(sys), unknowns(sys))) end From d8e47b18880df6f76a42055882290e43d0e0c7ec Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 28 Oct 2024 11:26:20 +0530 Subject: [PATCH 0262/2176] fix: fix flattening in `complete` --- src/systems/abstractsystem.jl | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index f114bc3fa7..216a83f85f 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -932,10 +932,11 @@ function complete(sys::AbstractSystem; split = true, flatten = true) @set! sys.ps = unique!(vcat(get_ps(sys), collect(newparams))) end if flatten - if sys isa Union{OptimizationSystem, ConstraintsSystem, JumpSystem} - newsys = sys - else + if (eqs = equations(sys)) isa Vector && + any(eq -> eq isa Equation && isconnection(eq.lhs), eqs) newsys = expand_connections(sys) + else + newsys = sys end newsys = ModelingToolkit.flatten(newsys) if has_parent(newsys) && get_parent(sys) === nothing From 21efe808cc75970f8ec5b5feca1046644d69aace Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 28 Oct 2024 11:26:25 +0530 Subject: [PATCH 0263/2176] test: uncomment runtests --- test/runtests.jl | 82 ++++++++++++++++++++++++------------------------ 1 file changed, 41 insertions(+), 41 deletions(-) diff --git a/test/runtests.jl b/test/runtests.jl index 5db3308540..c7edbfaaf5 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -19,48 +19,48 @@ end @time begin if GROUP == "All" || GROUP == "InterfaceI" @testset "InterfaceI" begin - # @safetestset "Linear Algebra Test" include("linalg.jl") - # @safetestset "AbstractSystem Test" include("abstractsystem.jl") - # @safetestset "Variable Scope Tests" include("variable_scope.jl") - # @safetestset "Symbolic Parameters Test" include("symbolic_parameters.jl") - # @safetestset "Parsing Test" include("variable_parsing.jl") - # @safetestset "Simplify Test" include("simplify.jl") - # @safetestset "Direct Usage Test" include("direct.jl") - # @safetestset "System Linearity Test" include("linearity.jl") - # @safetestset "Input Output Test" include("input_output_handling.jl") - # @safetestset "Clock Test" include("clock.jl") - # @safetestset "ODESystem Test" include("odesystem.jl") - # @safetestset "Dynamic Quantities Test" include("dq_units.jl") - # @safetestset "Unitful Quantities Test" include("units.jl") - # @safetestset "Mass Matrix Test" include("mass_matrix.jl") - # @safetestset "InitializationSystem Test" include("initializationsystem.jl") - # @safetestset "Guess Propagation" include("guess_propagation.jl") - # @safetestset "Hierarchical Initialization Equations" include("hierarchical_initialization_eqs.jl") - # @safetestset "Reduction Test" include("reduction.jl") - # @safetestset "Split Parameters Test" include("split_parameters.jl") - # @safetestset "StaticArrays Test" include("static_arrays.jl") - # @safetestset "Components Test" include("components.jl") - # @safetestset "Model Parsing Test" include("model_parsing.jl") - # @safetestset "Error Handling" include("error_handling.jl") - # @safetestset "StructuralTransformations" include("structural_transformation/runtests.jl") - # @safetestset "State Selection Test" include("state_selection.jl") - # @safetestset "Symbolic Event Test" include("symbolic_events.jl") - # @safetestset "Stream Connect Test" include("stream_connectors.jl") - # @safetestset "Domain Connect Test" include("domain_connectors.jl") - # @safetestset "Lowering Integration Test" include("lowering_solving.jl") - # @safetestset "Dependency Graph Test" include("dep_graphs.jl") - # @safetestset "Function Registration Test" include("function_registration.jl") - # @safetestset "Precompiled Modules Test" include("precompile_test.jl") - # @safetestset "DAE Jacobians Test" include("dae_jacobian.jl") - # @safetestset "Jacobian Sparsity" include("jacobiansparsity.jl") + @safetestset "Linear Algebra Test" include("linalg.jl") + @safetestset "AbstractSystem Test" include("abstractsystem.jl") + @safetestset "Variable Scope Tests" include("variable_scope.jl") + @safetestset "Symbolic Parameters Test" include("symbolic_parameters.jl") + @safetestset "Parsing Test" include("variable_parsing.jl") + @safetestset "Simplify Test" include("simplify.jl") + @safetestset "Direct Usage Test" include("direct.jl") + @safetestset "System Linearity Test" include("linearity.jl") + @safetestset "Input Output Test" include("input_output_handling.jl") + @safetestset "Clock Test" include("clock.jl") + @safetestset "ODESystem Test" include("odesystem.jl") + @safetestset "Dynamic Quantities Test" include("dq_units.jl") + @safetestset "Unitful Quantities Test" include("units.jl") + @safetestset "Mass Matrix Test" include("mass_matrix.jl") + @safetestset "InitializationSystem Test" include("initializationsystem.jl") + @safetestset "Guess Propagation" include("guess_propagation.jl") + @safetestset "Hierarchical Initialization Equations" include("hierarchical_initialization_eqs.jl") + @safetestset "Reduction Test" include("reduction.jl") + @safetestset "Split Parameters Test" include("split_parameters.jl") + @safetestset "StaticArrays Test" include("static_arrays.jl") + @safetestset "Components Test" include("components.jl") + @safetestset "Model Parsing Test" include("model_parsing.jl") + @safetestset "Error Handling" include("error_handling.jl") + @safetestset "StructuralTransformations" include("structural_transformation/runtests.jl") + @safetestset "State Selection Test" include("state_selection.jl") + @safetestset "Symbolic Event Test" include("symbolic_events.jl") + @safetestset "Stream Connect Test" include("stream_connectors.jl") + @safetestset "Domain Connect Test" include("domain_connectors.jl") + @safetestset "Lowering Integration Test" include("lowering_solving.jl") + @safetestset "Dependency Graph Test" include("dep_graphs.jl") + @safetestset "Function Registration Test" include("function_registration.jl") + @safetestset "Precompiled Modules Test" include("precompile_test.jl") + @safetestset "DAE Jacobians Test" include("dae_jacobian.jl") + @safetestset "Jacobian Sparsity" include("jacobiansparsity.jl") @safetestset "Modelingtoolkitize Test" include("modelingtoolkitize.jl") - # @safetestset "FuncAffect Test" include("funcaffect.jl") - # @safetestset "Constants Test" include("constants.jl") - # @safetestset "Parameter Dependency Test" include("parameter_dependencies.jl") - # @safetestset "Generate Custom Function Test" include("generate_custom_function.jl") - # @safetestset "Initial Values Test" include("initial_values.jl") - # @safetestset "Equation Type Accessors Test" include("equation_type_accessors.jl") - # @safetestset "Equations with complex values" include("complex.jl") + @safetestset "FuncAffect Test" include("funcaffect.jl") + @safetestset "Constants Test" include("constants.jl") + @safetestset "Parameter Dependency Test" include("parameter_dependencies.jl") + @safetestset "Generate Custom Function Test" include("generate_custom_function.jl") + @safetestset "Initial Values Test" include("initial_values.jl") + @safetestset "Equation Type Accessors Test" include("equation_type_accessors.jl") + @safetestset "Equations with complex values" include("complex.jl") end end From 3055c6345878877396a5a8d6a407b4e42c332716 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 28 Oct 2024 11:38:09 +0530 Subject: [PATCH 0264/2176] fix: only evaluate required keys of varmap in `process_SciMLProblem` --- src/systems/problem_utils.jl | 17 ++++++++++------- test/initial_values.jl | 13 +++++++++++++ 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/src/systems/problem_utils.jl b/src/systems/problem_utils.jl index daf1c164d8..bcec03040f 100644 --- a/src/systems/problem_utils.jl +++ b/src/systems/problem_utils.jl @@ -301,12 +301,14 @@ end """ $(TYPEDSIGNATURES) -Performs symbolic substitution on the values in `varmap`, using `varmap` itself as the -set of substitution rules. -""" -function evaluate_varmap!(varmap::AbstractDict) - for (k, v) in varmap - varmap[k] = fixpoint_sub(v, varmap) +Performs symbolic substitution on the values in `varmap` for the keys in `vars`, using +`varmap` itself as the set of substitution rules. If an entry is `vars` is not a key +in `varmap`, it is ignored. +""" +function evaluate_varmap!(varmap::AbstractDict, vars) + for k in vars + haskey(varmap, k) || continue + varmap[k] = fixpoint_sub(varmap[k], varmap) end end @@ -499,7 +501,7 @@ function process_SciMLProblem( add_observed!(sys, op) add_parameter_dependencies!(sys, op) - evaluate_varmap!(op) + evaluate_varmap!(op, dvs) u0 = better_varmap_to_vars( op, dvs; tofloat = true, use_union = false, @@ -511,6 +513,7 @@ function process_SciMLProblem( check_eqs_u0(eqs, dvs, u0; check_length, kwargs...) + evaluate_varmap!(op, ps) if is_split(sys) p = MTKParameters(sys, op) else diff --git a/test/initial_values.jl b/test/initial_values.jl index 9911bab7f4..fc386242a6 100644 --- a/test/initial_values.jl +++ b/test/initial_values.jl @@ -136,3 +136,16 @@ end prob = @test_nowarn ODEProblem(sys, [], (0.0, 1.0)) @test prob.p isa Vector{Float64} end + +@testset "Issue#3153" begin + @variables x(t) y(t) + @parameters c1 c2 c3 + eqs = [D(x) ~ y, + y ~ ifelse(t < c1, 0.0, (-c1 + t)^(c3))] + sps = [x, y] + ps = [c1, c2, c3] + @mtkbuild osys = ODESystem(eqs, t, sps, ps) + u0map = [x => 1.0] + pmap = [c1 => 5.0, c2 => 1.0, c3 => 1.2] + oprob = ODEProblem(osys, u0map, (0.0, 10.0), pmap) +end From b2decf70152faf90b381cd564043a30f7633946f Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 28 Oct 2024 11:38:15 +0530 Subject: [PATCH 0265/2176] test: uncomment runtests --- test/runtests.jl | 82 ++++++++++++++++++++++++------------------------ 1 file changed, 41 insertions(+), 41 deletions(-) diff --git a/test/runtests.jl b/test/runtests.jl index 5db3308540..c7edbfaaf5 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -19,48 +19,48 @@ end @time begin if GROUP == "All" || GROUP == "InterfaceI" @testset "InterfaceI" begin - # @safetestset "Linear Algebra Test" include("linalg.jl") - # @safetestset "AbstractSystem Test" include("abstractsystem.jl") - # @safetestset "Variable Scope Tests" include("variable_scope.jl") - # @safetestset "Symbolic Parameters Test" include("symbolic_parameters.jl") - # @safetestset "Parsing Test" include("variable_parsing.jl") - # @safetestset "Simplify Test" include("simplify.jl") - # @safetestset "Direct Usage Test" include("direct.jl") - # @safetestset "System Linearity Test" include("linearity.jl") - # @safetestset "Input Output Test" include("input_output_handling.jl") - # @safetestset "Clock Test" include("clock.jl") - # @safetestset "ODESystem Test" include("odesystem.jl") - # @safetestset "Dynamic Quantities Test" include("dq_units.jl") - # @safetestset "Unitful Quantities Test" include("units.jl") - # @safetestset "Mass Matrix Test" include("mass_matrix.jl") - # @safetestset "InitializationSystem Test" include("initializationsystem.jl") - # @safetestset "Guess Propagation" include("guess_propagation.jl") - # @safetestset "Hierarchical Initialization Equations" include("hierarchical_initialization_eqs.jl") - # @safetestset "Reduction Test" include("reduction.jl") - # @safetestset "Split Parameters Test" include("split_parameters.jl") - # @safetestset "StaticArrays Test" include("static_arrays.jl") - # @safetestset "Components Test" include("components.jl") - # @safetestset "Model Parsing Test" include("model_parsing.jl") - # @safetestset "Error Handling" include("error_handling.jl") - # @safetestset "StructuralTransformations" include("structural_transformation/runtests.jl") - # @safetestset "State Selection Test" include("state_selection.jl") - # @safetestset "Symbolic Event Test" include("symbolic_events.jl") - # @safetestset "Stream Connect Test" include("stream_connectors.jl") - # @safetestset "Domain Connect Test" include("domain_connectors.jl") - # @safetestset "Lowering Integration Test" include("lowering_solving.jl") - # @safetestset "Dependency Graph Test" include("dep_graphs.jl") - # @safetestset "Function Registration Test" include("function_registration.jl") - # @safetestset "Precompiled Modules Test" include("precompile_test.jl") - # @safetestset "DAE Jacobians Test" include("dae_jacobian.jl") - # @safetestset "Jacobian Sparsity" include("jacobiansparsity.jl") + @safetestset "Linear Algebra Test" include("linalg.jl") + @safetestset "AbstractSystem Test" include("abstractsystem.jl") + @safetestset "Variable Scope Tests" include("variable_scope.jl") + @safetestset "Symbolic Parameters Test" include("symbolic_parameters.jl") + @safetestset "Parsing Test" include("variable_parsing.jl") + @safetestset "Simplify Test" include("simplify.jl") + @safetestset "Direct Usage Test" include("direct.jl") + @safetestset "System Linearity Test" include("linearity.jl") + @safetestset "Input Output Test" include("input_output_handling.jl") + @safetestset "Clock Test" include("clock.jl") + @safetestset "ODESystem Test" include("odesystem.jl") + @safetestset "Dynamic Quantities Test" include("dq_units.jl") + @safetestset "Unitful Quantities Test" include("units.jl") + @safetestset "Mass Matrix Test" include("mass_matrix.jl") + @safetestset "InitializationSystem Test" include("initializationsystem.jl") + @safetestset "Guess Propagation" include("guess_propagation.jl") + @safetestset "Hierarchical Initialization Equations" include("hierarchical_initialization_eqs.jl") + @safetestset "Reduction Test" include("reduction.jl") + @safetestset "Split Parameters Test" include("split_parameters.jl") + @safetestset "StaticArrays Test" include("static_arrays.jl") + @safetestset "Components Test" include("components.jl") + @safetestset "Model Parsing Test" include("model_parsing.jl") + @safetestset "Error Handling" include("error_handling.jl") + @safetestset "StructuralTransformations" include("structural_transformation/runtests.jl") + @safetestset "State Selection Test" include("state_selection.jl") + @safetestset "Symbolic Event Test" include("symbolic_events.jl") + @safetestset "Stream Connect Test" include("stream_connectors.jl") + @safetestset "Domain Connect Test" include("domain_connectors.jl") + @safetestset "Lowering Integration Test" include("lowering_solving.jl") + @safetestset "Dependency Graph Test" include("dep_graphs.jl") + @safetestset "Function Registration Test" include("function_registration.jl") + @safetestset "Precompiled Modules Test" include("precompile_test.jl") + @safetestset "DAE Jacobians Test" include("dae_jacobian.jl") + @safetestset "Jacobian Sparsity" include("jacobiansparsity.jl") @safetestset "Modelingtoolkitize Test" include("modelingtoolkitize.jl") - # @safetestset "FuncAffect Test" include("funcaffect.jl") - # @safetestset "Constants Test" include("constants.jl") - # @safetestset "Parameter Dependency Test" include("parameter_dependencies.jl") - # @safetestset "Generate Custom Function Test" include("generate_custom_function.jl") - # @safetestset "Initial Values Test" include("initial_values.jl") - # @safetestset "Equation Type Accessors Test" include("equation_type_accessors.jl") - # @safetestset "Equations with complex values" include("complex.jl") + @safetestset "FuncAffect Test" include("funcaffect.jl") + @safetestset "Constants Test" include("constants.jl") + @safetestset "Parameter Dependency Test" include("parameter_dependencies.jl") + @safetestset "Generate Custom Function Test" include("generate_custom_function.jl") + @safetestset "Initial Values Test" include("initial_values.jl") + @safetestset "Equation Type Accessors Test" include("equation_type_accessors.jl") + @safetestset "Equations with complex values" include("complex.jl") end end From 7aba2fc7b5196d713ce46c053e49efb71b403bc3 Mon Sep 17 00:00:00 2001 From: Christopher Rackauckas Date: Mon, 28 Oct 2024 06:13:19 -0100 Subject: [PATCH 0266/2176] Update src/systems/problem_utils.jl --- 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 bcec03040f..3e50cc41cc 100644 --- a/src/systems/problem_utils.jl +++ b/src/systems/problem_utils.jl @@ -302,7 +302,7 @@ end $(TYPEDSIGNATURES) Performs symbolic substitution on the values in `varmap` for the keys in `vars`, using -`varmap` itself as the set of substitution rules. If an entry is `vars` is not a key +`varmap` itself as the set of substitution rules. If an entry in `vars` is not a key in `varmap`, it is ignored. """ function evaluate_varmap!(varmap::AbstractDict, vars) From 108d055dbb8b7f894f4095d08a271b5612e282e0 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 28 Oct 2024 13:09:28 +0530 Subject: [PATCH 0267/2176] feat: add support for `IntervalNonlinearProblem` --- src/ModelingToolkit.jl | 2 + src/systems/nonlinear/nonlinearsystem.jl | 121 +++++++++++++++++++++-- test/nonlinearsystem.jl | 22 +++++ 3 files changed, 139 insertions(+), 6 deletions(-) diff --git a/src/ModelingToolkit.jl b/src/ModelingToolkit.jl index 46177e902e..bdb9168fa0 100644 --- a/src/ModelingToolkit.jl +++ b/src/ModelingToolkit.jl @@ -226,6 +226,8 @@ export JumpSystem export ODEProblem, SDEProblem export NonlinearFunction, NonlinearFunctionExpr export NonlinearProblem, NonlinearProblemExpr +export IntervalNonlinearFunction, IntervalNonlinearFunctionExpr +export IntervalNonlinearProblem, IntervalNonlinearProblemExpr export OptimizationProblem, OptimizationProblemExpr, constraints export SteadyStateProblem, SteadyStateProblemExpr export JumpProblem diff --git a/src/systems/nonlinear/nonlinearsystem.jl b/src/systems/nonlinear/nonlinearsystem.jl index e7adb549a3..44e77d5a0f 100644 --- a/src/systems/nonlinear/nonlinearsystem.jl +++ b/src/systems/nonlinear/nonlinearsystem.jl @@ -258,13 +258,18 @@ end function generate_function( sys::NonlinearSystem, dvs = unknowns(sys), ps = parameters(sys); - wrap_code = identity, kwargs...) + wrap_code = identity, scalar = false, kwargs...) rhss = [deq.rhs for deq in equations(sys)] + dvs′ = value.(dvs) + if scalar + rhss = only(rhss) + dvs′ = only(dvs) + end pre, sol_states = get_substitutions_and_solved_unknowns(sys) wrap_code = wrap_code .∘ wrap_array_vars(sys, rhss; dvs, ps) .∘ - wrap_parameter_dependencies(sys, false) + wrap_parameter_dependencies(sys, scalar) p = reorder_parameters(sys, value.(ps)) - return build_function(rhss, value.(dvs), p...; postprocess_fbody = pre, + return build_function(rhss, dvs′, p...; postprocess_fbody = pre, states = sol_states, wrap_code, kwargs...) end @@ -288,7 +293,7 @@ SciMLBase.NonlinearFunction{iip}(sys::NonlinearSystem, dvs = unknowns(sys), kwargs...) where {iip} ``` -Create an `NonlinearFunction` from the [`NonlinearSystem`](@ref). The arguments +Create a `NonlinearFunction` from the [`NonlinearSystem`](@ref). The arguments `dvs` and `ps` are used to set the order of the dependent variable and parameter vectors, respectively. """ @@ -351,6 +356,34 @@ function SciMLBase.NonlinearFunction{iip}(sys::NonlinearSystem, dvs = unknowns(s observed = observedfun) end +""" +$(TYPEDSIGNATURES) + +Create an `IntervalNonlinearFunction` from the [`NonlinearSystem`](@ref). The arguments +`dvs` and `ps` are used to set the order of the dependent variable and parameter vectors, +respectively. +""" +function SciMLBase.IntervalNonlinearFunction( + sys::NonlinearSystem, dvs = unknowns(sys), ps = parameters(sys), u0 = nothing; + p = nothing, eval_expression = false, eval_module = @__MODULE__, kwargs...) + if !iscomplete(sys) + error("A completed `NonlinearSystem` is required. Call `complete` or `structural_simplify` on the system before creating a `IntervalNonlinearFunction`") + end + if !isone(length(dvs)) || !isone(length(equations(sys))) + error("`IntervalNonlinearFunction` only supports systems with a single equation and a single unknown.") + end + + f_gen = generate_function( + sys, dvs, ps; expression = Val{true}, scalar = true, kwargs...) + f_oop = eval_or_rgf(f_gen; eval_expression, eval_module) + f(u, p) = f_oop(u, p) + f(u, p::MTKParameters) = f_oop(u, p...) + + observedfun = ObservedFunctionCache(sys; eval_expression, eval_module) + + IntervalNonlinearFunction{false}(f; observed = observedfun, sys = sys) +end + """ ```julia SciMLBase.NonlinearFunctionExpr{iip}(sys::NonlinearSystem, dvs = unknowns(sys), @@ -361,14 +394,14 @@ SciMLBase.NonlinearFunctionExpr{iip}(sys::NonlinearSystem, dvs = unknowns(sys), kwargs...) where {iip} ``` -Create a Julia expression for an `ODEFunction` from the [`ODESystem`](@ref). +Create a Julia expression for a `NonlinearFunction` from the [`NonlinearSystem`](@ref). The arguments `dvs` and `ps` are used to set the order of the dependent variable and parameter vectors, respectively. """ struct NonlinearFunctionExpr{iip} end function NonlinearFunctionExpr{iip}(sys::NonlinearSystem, dvs = unknowns(sys), - ps = parameters(sys), u0 = nothing, p = nothing; + ps = parameters(sys), u0 = nothing; p = nothing, version = nothing, tgrad = false, jac = false, linenumbers = false, @@ -412,6 +445,34 @@ function NonlinearFunctionExpr{iip}(sys::NonlinearSystem, dvs = unknowns(sys), !linenumbers ? Base.remove_linenums!(ex) : ex end +""" +$(TYPEDSIGNATURES) + +Create a Julia expression for an `IntervalNonlinearFunction` from the +[`NonlinearSystem`](@ref). The arguments `dvs` and `ps` are used to set the order of the +dependent variable and parameter vectors, respectively. +""" +function IntervalNonlinearFunctionExpr( + sys::NonlinearSystem, dvs = unknowns(sys), ps = parameters(sys), + u0 = nothing; p = nothing, linenumbers = false, kwargs...) + if !iscomplete(sys) + error("A completed `NonlinearSystem` is required. Call `complete` or `structural_simplify` on the system before creating a `IntervalNonlinearFunctionExpr`") + end + if !isone(length(dvs)) || !isone(length(equations(sys))) + error("`IntervalNonlinearFunctionExpr` only supports systems with a single equation and a single unknown.") + end + + f = generate_function(sys, dvs, ps; expression = Val{true}, scalar = true, kwargs...) + + IntervalNonlinearFunction{false}(f; sys = sys) + + ex = quote + f = $f + NonlinearFunction{false}(f) + end + !linenumbers ? Base.remove_linenums!(ex) : ex +end + """ ```julia DiffEqBase.NonlinearProblem{iip}(sys::NonlinearSystem, u0map, @@ -470,6 +531,26 @@ function DiffEqBase.NonlinearLeastSquaresProblem{iip}(sys::NonlinearSystem, u0ma NonlinearLeastSquaresProblem{iip}(f, u0, p; filter_kwargs(kwargs)...) end +""" +$(TYPEDSIGNATURES) + +Generate an `IntervalNonlinearProblem` from a `NonlinearSystem` and allow for automatically +symbolically calculating numerical enhancements. +""" +function DiffEqBase.IntervalNonlinearProblem(sys::NonlinearSystem, uspan::NTuple{2}, + parammap = SciMLBase.NullParameters(); kwargs...) + if !iscomplete(sys) + error("A completed `NonlinearSystem` is required. Call `complete` or `structural_simplify` on the system before creating a `IntervalNonlinearProblem`") + end + if !isone(length(unknowns(sys))) || !isone(length(equations(sys))) + error("`IntervalNonlinearProblem` only supports with a single equation and a single unknown.") + end + f, u0, p = process_SciMLProblem( + IntervalNonlinearFunction, sys, unknowns(sys) .=> uspan[1], parammap; kwargs...) + + return IntervalNonlinearProblem(f, uspan, p; filter_kwargs(kwargs)...) +end + """ ```julia DiffEqBase.NonlinearProblemExpr{iip}(sys::NonlinearSystem, u0map, @@ -550,6 +631,34 @@ function NonlinearLeastSquaresProblemExpr{iip}(sys::NonlinearSystem, u0map, !linenumbers ? Base.remove_linenums!(ex) : ex end +""" +$(TYPEDSIGNATURES) + +Generates a Julia expression for an IntervalNonlinearProblem from a +NonlinearSystem and allows for automatically symbolically calculating +numerical enhancements. +""" +function IntervalNonlinearProblemExpr(sys::NonlinearSystem, uspan::NTuple{2}, + parammap = SciMLBase.NullParameters(); kwargs...) + if !iscomplete(sys) + error("A completed `NonlinearSystem` is required. Call `complete` or `structural_simplify` on the system before creating a `IntervalNonlinearProblemExpr`") + end + if !isone(length(unknowns(sys))) || !isone(length(equations(sys))) + error("`IntervalNonlinearProblemExpr` only supports with a single equation and a single unknown.") + end + f, u0, p = process_SciMLProblem( + IntervalNonlinearFunctionExpr, sys, unknowns(sys) .=> uspan[1], parammap; kwargs...) + linenumbers = get(kwargs, :linenumbers, true) + + ex = quote + f = $f + uspan = $uspan + p = $p + IntervalNonlinearProblem(f, uspan, p; $(filter_kwargs(kwargs)...)) + end + !linenumbers ? Base.remove_linenums!(ex) : ex +end + function flatten(sys::NonlinearSystem, noeqs = false) systems = get_systems(sys) if isempty(systems) diff --git a/test/nonlinearsystem.jl b/test/nonlinearsystem.jl index c50f32e463..f766ca0131 100644 --- a/test/nonlinearsystem.jl +++ b/test/nonlinearsystem.jl @@ -358,3 +358,25 @@ end @test_nowarn solve(prob) end end + +@testset "IntervalNonlinearProblem" begin + @variables x + @parameters p + @named nlsys = NonlinearSystem([0 ~ x * x - p]) + + for sys in [complete(nlsys), complete(nlsys; split = false)] + prob = IntervalNonlinearProblem(sys, (0.0, 2.0), [p => 1.0]) + sol = @test_nowarn solve(prob, ITP()) + @test SciMLBase.successful_retcode(sol) + @test_nowarn IntervalNonlinearProblemExpr(sys, (0.0, 2.0), [p => 1.0]) + end + + @variables y + @mtkbuild sys = NonlinearSystem([0 ~ x * x - p * x + p, 0 ~ x * y + p]) + @test_throws ["single equation", "unknown"] IntervalNonlinearProblem(sys, (0.0, 1.0)) + @test_throws ["single equation", "unknown"] IntervalNonlinearFunction(sys, (0.0, 1.0)) + @test_throws ["single equation", "unknown"] IntervalNonlinearProblemExpr( + sys, (0.0, 1.0)) + @test_throws ["single equation", "unknown"] IntervalNonlinearFunctionExpr( + sys, (0.0, 1.0)) +end From f7bf33ac394c5379cc229592e0974777f53badff Mon Sep 17 00:00:00 2001 From: Christopher Tessum Date: Sun, 13 Oct 2024 14:10:41 -0500 Subject: [PATCH 0268/2176] Don't check units during initialization ...because initialization problems don't correctly handle units and units have already been checked during system initialization. --- src/systems/problem_utils.jl | 4 ++-- test/initializationsystem.jl | 17 +++++++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/systems/problem_utils.jl b/src/systems/problem_utils.jl index 3e50cc41cc..695e489db0 100644 --- a/src/systems/problem_utils.jl +++ b/src/systems/problem_utils.jl @@ -396,7 +396,7 @@ Keyword arguments: - `eval_module`: If `eval_expression == true`, the module to `eval` into. Otherwise, the module in which to generate the `RuntimeGeneratedFunction`. - `fully_determined`: Override whether the initialization system is fully determined. -- `check_units`: Enable or disable unit checks. +- `check_units`: Enable or disable unit checks when constructing the initialization problem. - `tofloat`, `use_union`: 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` @@ -414,7 +414,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 = false, - check_units = true, tofloat = true, use_union = false, + check_units = false, tofloat = true, use_union = false, u0_constructor = identity, du0map = nothing, check_length = true, symbolic_u0 = false, kwargs...) dvs = unknowns(sys) ps = parameters(sys) diff --git a/test/initializationsystem.jl b/test/initializationsystem.jl index 3eddba2b78..c9783c9659 100644 --- a/test/initializationsystem.jl +++ b/test/initializationsystem.jl @@ -3,6 +3,7 @@ using ForwardDiff using SymbolicIndexingInterface, SciMLStructures using SciMLStructures: Tunable using ModelingToolkit: t_nounits as t, D_nounits as D +using DynamicQuantities @parameters g @variables x(t) y(t) [state_priority = 10] λ(t) @@ -860,3 +861,19 @@ end integ = init(prob) @test integ[x] ≈ [1.0, 3.0] end + +@testset "units" begin + t = ModelingToolkit.t + D = ModelingToolkit.D + @parameters g [unit = u"m/s^2"] L=1 [unit = u"m^2"] + @variables x(t) [unit = u"m"] y(t) [unit = u"m" state_priority = 10] λ(t) [unit = u"s^-2"] + eqs = [D(D(x)) ~ λ * x + D(D(y)) ~ λ * y - g + x^2 + y^2 ~ L] + @mtkbuild pend = ODESystem(eqs, t) + + prob = ODEProblem(pend, [x => 1, y => 0], (0.0, 1.5), [g => 1], + guesses = ModelingToolkit.missing_variable_defaults(pend)) + sol = solve(prob, Rodas5P()) + @test SciMLBase.successful_retcode(sol) +end From 0f5cf1a9970b266a4f8c4ff963cd79e0b9a883c3 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 28 Oct 2024 14:49:00 +0530 Subject: [PATCH 0269/2176] refactor: rename `check_units` to `check_initialization_units` --- src/systems/problem_utils.jl | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/systems/problem_utils.jl b/src/systems/problem_utils.jl index 695e489db0..f7f13b9e19 100644 --- a/src/systems/problem_utils.jl +++ b/src/systems/problem_utils.jl @@ -396,7 +396,8 @@ Keyword arguments: - `eval_module`: If `eval_expression == true`, the module to `eval` into. Otherwise, the module in which to generate the `RuntimeGeneratedFunction`. - `fully_determined`: Override whether the initialization system is fully determined. -- `check_units`: Enable or disable unit checks when constructing the initialization problem. +- `check_initialization_units`: Enable or disable unit checks when constructing the + initialization problem. - `tofloat`, `use_union`: 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` @@ -414,7 +415,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 = false, - check_units = false, tofloat = true, use_union = false, + check_initialization_units = false, tofloat = true, use_union = false, u0_constructor = identity, du0map = nothing, check_length = true, symbolic_u0 = false, kwargs...) dvs = unknowns(sys) ps = parameters(sys) @@ -464,7 +465,7 @@ function process_SciMLProblem( !isempty(initialization_equations(sys))) && t !== nothing initializeprob = ModelingToolkit.InitializationProblem( sys, t, u0map, pmap; guesses, warn_initialize_determined, - initialization_eqs, eval_expression, eval_module, fully_determined, check_units) + initialization_eqs, eval_expression, eval_module, fully_determined, check_units = check_initialization_units) initializeprobmap = getu(initializeprob, unknowns(sys)) punknowns = [p From 616e5d1f3eae00bedb12071b979af99e685bdccc Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 28 Oct 2024 15:03:25 +0530 Subject: [PATCH 0270/2176] refactor: format --- src/systems/problem_utils.jl | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/systems/problem_utils.jl b/src/systems/problem_utils.jl index f7f13b9e19..977f637d47 100644 --- a/src/systems/problem_utils.jl +++ b/src/systems/problem_utils.jl @@ -465,7 +465,8 @@ function process_SciMLProblem( !isempty(initialization_equations(sys))) && t !== nothing initializeprob = ModelingToolkit.InitializationProblem( sys, t, u0map, pmap; guesses, warn_initialize_determined, - initialization_eqs, eval_expression, eval_module, fully_determined, check_units = check_initialization_units) + initialization_eqs, eval_expression, eval_module, fully_determined, + check_units = check_initialization_units) initializeprobmap = getu(initializeprob, unknowns(sys)) punknowns = [p From 26074bd51b6b35744fa4850b4673dfcc4e8cbb24 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 28 Oct 2024 15:54:42 +0530 Subject: [PATCH 0271/2176] fix: fix denominator checking in `HomotopyContinuationExt` --- ext/MTKHomotopyContinuationExt.jl | 15 +++++++++++++-- test/extensions/homotopy_continuation.jl | 14 ++++++++++---- 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/ext/MTKHomotopyContinuationExt.jl b/ext/MTKHomotopyContinuationExt.jl index e4a9243ff9..ebd8afbfc1 100644 --- a/ext/MTKHomotopyContinuationExt.jl +++ b/ext/MTKHomotopyContinuationExt.jl @@ -196,7 +196,18 @@ function MTK.HomotopyContinuationProblem( error("Equation $eq is not a polynomial in the unknowns. See warnings for further details.") end num, den = handle_rational_polynomials(eq.rhs - eq.lhs, dvs) - push!(denoms, den) + + # make factors different elements, otherwise the nonzero factors artificially + # inflate the error of the zero factor. + if iscall(den) && operation(den) == * + for arg in arguments(den) + # ignore constant factors + symbolic_type(arg) == NotSymbolic() && continue + push!(denoms, abs(arg)) + end + elseif symbolic_type(den) != NotSymbolic() + push!(denoms, abs(den)) + end return 0 ~ num end @@ -232,7 +243,7 @@ Extra keyword arguments: the denominator to be below `denominator_abstol` will be discarded. """ function CommonSolve.solve(prob::MTK.HomotopyContinuationProblem, - alg = nothing; show_progress = false, denominator_abstol = 1e-8, kwargs...) + alg = nothing; show_progress = false, denominator_abstol = 1e-7, kwargs...) sol = HomotopyContinuation.solve( prob.homotopy_continuation_system; show_progress, kwargs...) realsols = HomotopyContinuation.results(sol; only_real = true) diff --git a/test/extensions/homotopy_continuation.jl b/test/extensions/homotopy_continuation.jl index 6d7279899f..9bd64fa7fb 100644 --- a/test/extensions/homotopy_continuation.jl +++ b/test/extensions/homotopy_continuation.jl @@ -98,7 +98,9 @@ end @test sol[x] ≈ 1.0 p = parameter_values(prob) for invalid in [2.0, 3.0] - @test prob.denominator([invalid], p)[1] <= 1e-8 + for err in [-9e-8, 0, 9e-8] + @test any(<=(1e-7), prob.denominator([invalid + err, 2.0], p)) + end end @named sys = NonlinearSystem( @@ -115,14 +117,18 @@ end disallowed_y = [7, 5, 4] @test all(!isapprox(sol[x]; atol = 1e-8), disallowed_x) @test all(!isapprox(sol[y]; atol = 1e-8), disallowed_y) - @test sol[x^2 - 4x + y] >= 1e-8 + @test abs(sol[x^2 - 4x + y]) >= 1e-8 p = parameter_values(prob) for val in disallowed_x - @test any(<=(1e-8), prob.denominator([val, 2.0], p)) + for err in [-9e-8, 0, 9e-8] + @test any(<=(1e-7), prob.denominator([val + err, 2.0], p)) + end end for val in disallowed_y - @test any(<=(1e-8), prob.denominator([2.0, val], p)) + for err in [-9e-8, 0, 9e-8] + @test any(<=(1e-7), prob.denominator([2.0, val + err], p)) + end end @test prob.denominator([2.0, 4.0], p)[1] <= 1e-8 end From add7059c8a52df7401aee9af3f7e6d8833b59db4 Mon Sep 17 00:00:00 2001 From: vyudu Date: Mon, 28 Oct 2024 17:14:30 -0400 Subject: [PATCH 0272/2176] make Observable definition comply with new record_from_solution definition --- ext/MTKBifurcationKitExt.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ext/MTKBifurcationKitExt.jl b/ext/MTKBifurcationKitExt.jl index a0bd3bfc02..e5b57efc5a 100644 --- a/ext/MTKBifurcationKitExt.jl +++ b/ext/MTKBifurcationKitExt.jl @@ -59,7 +59,7 @@ struct ObservableRecordFromSolution{S, T} end end # Functor function that computes the value. -function (orfs::ObservableRecordFromSolution)(x, p) +function (orfs::ObservableRecordFromSolution)(x, p; k...) # Updates the state values (in subs_vals). for state_idx in 1:(orfs.state_end_idxs) orfs.subs_vals[state_idx] = orfs.subs_vals[state_idx][1] => x[state_idx] From eb681c10403031e3a1ee28c96f9d4b2defc3e389 Mon Sep 17 00:00:00 2001 From: vyudu Date: Mon, 28 Oct 2024 17:26:25 -0400 Subject: [PATCH 0273/2176] add test --- test/extensions/bifurcationkit.jl | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/test/extensions/bifurcationkit.jl b/test/extensions/bifurcationkit.jl index b8c95dca99..629edf46a6 100644 --- a/test/extensions/bifurcationkit.jl +++ b/test/extensions/bifurcationkit.jl @@ -162,4 +162,12 @@ let bf = bifurcationdiagram(bp, PALC(), 2, opts_br) @test bf.γ.specialpoint[1].param≈0.1 atol=1e-4 rtol=1e-4 + + # Test with plot variable as observable + pvar = ModelingToolkit.get_var_to_name(fol)[:RHS] + bp = BifurcationProblem(fol, u0, par, bif_par; plot_var = pvar) + opts_br = ContinuationPar(p_min = -1.0, + p_max = 1.0) + bf = bifurcationdiagram(bp, PALC(), 2, opts_br) + @test bf.γ.specialpoint[1].param≈0.1 atol=1e-4 rtol=1e-4 end From d3c8933b7d005fffad8ebb37513f14a0dbba1af4 Mon Sep 17 00:00:00 2001 From: Christopher Rackauckas Date: Tue, 29 Oct 2024 08:52:53 -0100 Subject: [PATCH 0274/2176] Update Documentation.yml to build docs with LTS Should fix https://github.com/SciML/ModelingToolkit.jl/issues/3163 --- .github/workflows/Documentation.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/Documentation.yml b/.github/workflows/Documentation.yml index ca24f79d0b..dceee5f1d2 100644 --- a/.github/workflows/Documentation.yml +++ b/.github/workflows/Documentation.yml @@ -20,7 +20,7 @@ jobs: - uses: actions/checkout@v4 - uses: julia-actions/setup-julia@latest with: - version: '1' + version: 'lts' - run: sudo apt-get update && sudo apt-get install -y xorg-dev mesa-utils xvfb libgl1 freeglut3-dev libxrandr-dev libxinerama-dev libxcursor-dev libxi-dev libxext-dev - name: Install dependencies run: DISPLAY=:0 xvfb-run -s '-screen 0 1024x768x24' julia --project=docs/ -e 'using Pkg; Pkg.develop(PackageSpec(path=pwd())); Pkg.instantiate()' From 91eabc77fde9cc47d73bb1f482f1e73bd663a5b5 Mon Sep 17 00:00:00 2001 From: Christopher Rackauckas Date: Tue, 29 Oct 2024 08:53:48 -0100 Subject: [PATCH 0275/2176] Update Project.toml --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 958772cb05..656fef6173 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "ModelingToolkit" uuid = "961ee093-0014-501f-94e3-6117800e7a78" authors = ["Yingbo Ma ", "Chris Rackauckas and contributors"] -version = "9.48.0" +version = "9.49.0" [deps] AbstractTrees = "1520ce14-60c1-5f80-bbc7-55ef81b5835c" From d48dc57ff2a15372ce4e6cd939e62ef333a3b746 Mon Sep 17 00:00:00 2001 From: vyudu Date: Mon, 28 Oct 2024 17:14:30 -0400 Subject: [PATCH 0276/2176] make Observable definition comply with new record_from_solution definition --- ext/MTKBifurcationKitExt.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ext/MTKBifurcationKitExt.jl b/ext/MTKBifurcationKitExt.jl index a0bd3bfc02..e5b57efc5a 100644 --- a/ext/MTKBifurcationKitExt.jl +++ b/ext/MTKBifurcationKitExt.jl @@ -59,7 +59,7 @@ struct ObservableRecordFromSolution{S, T} end end # Functor function that computes the value. -function (orfs::ObservableRecordFromSolution)(x, p) +function (orfs::ObservableRecordFromSolution)(x, p; k...) # Updates the state values (in subs_vals). for state_idx in 1:(orfs.state_end_idxs) orfs.subs_vals[state_idx] = orfs.subs_vals[state_idx][1] => x[state_idx] From e27da938be5ff7f69578eac71bd8c61e621e0f32 Mon Sep 17 00:00:00 2001 From: vyudu Date: Mon, 28 Oct 2024 17:26:25 -0400 Subject: [PATCH 0277/2176] add test --- test/extensions/bifurcationkit.jl | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/test/extensions/bifurcationkit.jl b/test/extensions/bifurcationkit.jl index b8c95dca99..629edf46a6 100644 --- a/test/extensions/bifurcationkit.jl +++ b/test/extensions/bifurcationkit.jl @@ -162,4 +162,12 @@ let bf = bifurcationdiagram(bp, PALC(), 2, opts_br) @test bf.γ.specialpoint[1].param≈0.1 atol=1e-4 rtol=1e-4 + + # Test with plot variable as observable + pvar = ModelingToolkit.get_var_to_name(fol)[:RHS] + bp = BifurcationProblem(fol, u0, par, bif_par; plot_var = pvar) + opts_br = ContinuationPar(p_min = -1.0, + p_max = 1.0) + bf = bifurcationdiagram(bp, PALC(), 2, opts_br) + @test bf.γ.specialpoint[1].param≈0.1 atol=1e-4 rtol=1e-4 end From 4f89f08b7474469434ea38e3a1bcc5d7f5643d3e Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 30 Oct 2024 12:05:24 +0530 Subject: [PATCH 0278/2176] fix: fix `complete` not properly expanding systems --- src/systems/abstractsystem.jl | 4 ++-- test/odesystem.jl | 28 ++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index de389ab3b8..c53c900d27 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -933,8 +933,8 @@ function complete(sys::AbstractSystem; split = true, flatten = true) @set! sys.ps = unique!(vcat(get_ps(sys), collect(newparams))) end if flatten - if (eqs = equations(sys)) isa Vector && - any(eq -> eq isa Equation && isconnection(eq.lhs), eqs) + eqs = equations(sys) + if eqs isa AbstractArray && eltype(eqs) <: Equation newsys = expand_connections(sys) else newsys = sys diff --git a/test/odesystem.jl b/test/odesystem.jl index 9286fb3c01..dfc755da98 100644 --- a/test/odesystem.jl +++ b/test/odesystem.jl @@ -1467,3 +1467,31 @@ end obsfn(buf, prob.u0, prob.p, 0.0) @test buf ≈ [1.0, 1.0, 2.0] end + +@testset "`complete` expands connections" begin + using ModelingToolkitStandardLibrary.Electrical + @mtkmodel RC begin + @parameters begin + R = 1.0 + C = 1.0 + V = 1.0 + end + @components begin + resistor = Resistor(R = R) + capacitor = Capacitor(C = C, v = 0.0) + source = Voltage() + constant = Constant(k = V) + ground = Ground() + end + @equations begin + connect(constant.output, source.V) + connect(source.p, resistor.p) + connect(resistor.n, capacitor.p) + connect(capacitor.n, source.n, ground.g) + end + end + @named sys = RC() + total_eqs = length(equations(expand_connections(sys))) + sys2 = complete(sys) + @test length(equations(sys2)) == total_eqs +end From 2d1554a778d659a645237a90342c37b37a223539 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 30 Oct 2024 12:50:37 +0530 Subject: [PATCH 0279/2176] fix: support non-tunable parameters with DDEs --- src/systems/diffeqs/abstractodesystem.jl | 11 +++++++---- test/dde.jl | 17 +++++++++++++++++ 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/src/systems/diffeqs/abstractodesystem.jl b/src/systems/diffeqs/abstractodesystem.jl index 8d9e0b5381..eeac9c7626 100644 --- a/src/systems/diffeqs/abstractodesystem.jl +++ b/src/systems/diffeqs/abstractodesystem.jl @@ -189,7 +189,9 @@ function generate_function(sys::AbstractODESystem, dvs = unknowns(sys), wrap_code = identity, kwargs...) if isdde - eqs = delay_to_function(sys) + issplit = has_index_cache(sys) && get_index_cache(sys) !== nothing + eqs = delay_to_function( + sys; history_arg = issplit ? MTKPARAMETERS_ARG : DEFAULT_PARAMS_ARG) else eqs = [eq for eq in equations(sys)] end @@ -211,7 +213,10 @@ function generate_function(sys::AbstractODESystem, dvs = unknowns(sys), t = get_iv(sys) if isdde - build_function(rhss, u, DDE_HISTORY_FUN, p..., t; kwargs...) + build_function(rhss, u, DDE_HISTORY_FUN, p..., t; kwargs..., + wrap_code = wrap_code .∘ wrap_mtkparameters(sys, false, 3) .∘ + wrap_array_vars(sys, rhss; dvs, ps) .∘ + wrap_parameter_dependencies(sys, false)) else pre, sol_states = get_substitutions_and_solved_unknowns(sys) @@ -570,9 +575,7 @@ function DiffEqBase.DDEFunction{iip}(sys::AbstractODESystem, dvs = unknowns(sys) kwargs...) f_oop, f_iip = eval_or_rgf.(f_gen; eval_expression, eval_module) f(u, h, p, t) = f_oop(u, h, p, t) - f(u, h, p::MTKParameters, t) = f_oop(u, h, p..., t) f(du, u, h, p, t) = f_iip(du, u, h, p, t) - f(du, u, h, p::MTKParameters, t) = f_iip(du, u, h, p..., t) DDEFunction{iip}(f, sys = sys) end diff --git a/test/dde.jl b/test/dde.jl index fb099ab8c6..c6e64fc1a5 100644 --- a/test/dde.jl +++ b/test/dde.jl @@ -170,3 +170,20 @@ prob_sa = DDEProblem(sys, [], (0.0, 10.0); constant_lags = [sys.osc1.τ, sys.osc sol(sol.t .- prob.ps[ssys.valve.τ]; idxs = ssys.valve.opening).u .+ sum.(sol[ssys.vvecs.x]) end + +@testset "Issue#3165 DDEs with non-tunables" begin + @variables x(..) = 1.0 + @parameters w=1.0 [tunable = false] τ=0.5 + eqs = [D(x(t)) ~ -w * x(t - τ)] + + @named sys = System(eqs, t) + sys = structural_simplify(sys) + + prob = DDEProblem(sys, + [], + (0.0, 10.0), + constant_lags = [τ]) + + alg = MethodOfSteps(Vern7()) + @test_nowarn solve(prob, alg) +end From d1289974104a59b7e3661d1aef3aca52be652b22 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 30 Oct 2024 15:37:09 +0530 Subject: [PATCH 0280/2176] fix: support non-tunable parameters with SDDEs --- src/systems/diffeqs/abstractodesystem.jl | 11 ++++------- src/systems/diffeqs/sdesystem.jl | 6 +++++- test/dde.jl | 11 +++++++++++ 3 files changed, 20 insertions(+), 8 deletions(-) diff --git a/src/systems/diffeqs/abstractodesystem.jl b/src/systems/diffeqs/abstractodesystem.jl index eeac9c7626..518b48e929 100644 --- a/src/systems/diffeqs/abstractodesystem.jl +++ b/src/systems/diffeqs/abstractodesystem.jl @@ -215,7 +215,7 @@ function generate_function(sys::AbstractODESystem, dvs = unknowns(sys), if isdde build_function(rhss, u, DDE_HISTORY_FUN, p..., t; kwargs..., wrap_code = wrap_code .∘ wrap_mtkparameters(sys, false, 3) .∘ - wrap_array_vars(sys, rhss; dvs, ps) .∘ + wrap_array_vars(sys, rhss; dvs, ps, history = true) .∘ wrap_parameter_dependencies(sys, false)) else pre, sol_states = get_substitutions_and_solved_unknowns(sys) @@ -598,17 +598,14 @@ function DiffEqBase.SDDEFunction{iip}(sys::AbstractODESystem, dvs = unknowns(sys expression_module = eval_module, checkbounds = checkbounds, kwargs...) f_oop, f_iip = eval_or_rgf.(f_gen; eval_expression, eval_module) + f(u, h, p, t) = f_oop(u, h, p, t) + f(du, u, h, p, t) = f_iip(du, u, h, p, t) + g_gen = generate_diffusion_function(sys, dvs, ps; expression = Val{true}, isdde = true, kwargs...) g_oop, g_iip = eval_or_rgf.(g_gen; eval_expression, eval_module) - f(u, h, p, t) = f_oop(u, h, p, t) - f(u, h, p::MTKParameters, t) = f_oop(u, h, p..., t) - f(du, u, h, p, t) = f_iip(du, u, h, p, t) - f(du, u, h, p::MTKParameters, t) = f_iip(du, u, h, p..., t) g(u, h, p, t) = g_oop(u, h, p, t) - g(u, h, p::MTKParameters, t) = g_oop(u, h, p..., t) g(du, u, h, p, t) = g_iip(du, u, h, p, t) - g(du, u, h, p::MTKParameters, t) = g_iip(du, u, h, p..., t) SDDEFunction{iip}(f, g, sys = sys) end diff --git a/src/systems/diffeqs/sdesystem.jl b/src/systems/diffeqs/sdesystem.jl index 9f3814774f..011ba6e216 100644 --- a/src/systems/diffeqs/sdesystem.jl +++ b/src/systems/diffeqs/sdesystem.jl @@ -302,7 +302,11 @@ function generate_diffusion_function(sys::SDESystem, dvs = unknowns(sys), (map(x -> time_varying_as_func(value(x), sys), ps),) end if isdde - return build_function(eqs, u, DDE_HISTORY_FUN, p..., get_iv(sys); kwargs...) + return build_function(eqs, u, DDE_HISTORY_FUN, p..., get_iv(sys); kwargs..., + wrap_code = get(kwargs, :wrap_code, identity) .∘ + wrap_mtkparameters(sys, false, 3) .∘ + wrap_array_vars(sys, eqs; dvs, ps, history = true) .∘ + wrap_parameter_dependencies(sys, false)) else return build_function(eqs, u, p..., get_iv(sys); kwargs...) end diff --git a/test/dde.jl b/test/dde.jl index c6e64fc1a5..aa4536e604 100644 --- a/test/dde.jl +++ b/test/dde.jl @@ -186,4 +186,15 @@ end alg = MethodOfSteps(Vern7()) @test_nowarn solve(prob, alg) + + @brownian r + eqs = [D(x(t)) ~ -w * x(t - τ) + r] + @named sys = System(eqs, t) + sys = structural_simplify(sys) + prob = SDDEProblem(sys, + [], + (0.0, 10.0), + constant_lags = [τ]) + + @test_nowarn solve(prob, RKMil()) end From dddd69884244be31e9f8d9d3a83b9b126eba16ce Mon Sep 17 00:00:00 2001 From: Venkateshprasad <32921645+ven-k@users.noreply.github.com> Date: Wed, 30 Oct 2024 18:27:34 +0000 Subject: [PATCH 0281/2176] fix: unpack components of the base sys in implicit extend --- src/systems/model_parsing.jl | 22 +++++++++++----------- test/model_parsing.jl | 26 ++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 11 deletions(-) diff --git a/src/systems/model_parsing.jl b/src/systems/model_parsing.jl index ab70646824..e8955b5b84 100644 --- a/src/systems/model_parsing.jl +++ b/src/systems/model_parsing.jl @@ -772,18 +772,17 @@ function Base.names(model::Model) map(first, get(model.structure, :components, EMPTY_VoVoSYMBOL)))) end -function _parse_extend!(ext, a, b, dict, expr, kwargs, vars, additional_args) +function _parse_extend!(ext, a, b, dict, expr, kwargs, vars, implicit_arglist) extend_args!(a, b, dict, expr, kwargs) - # `additional_args` doubles as a flag to check the mode of `@extend`. It is + # `implicit_arglist` doubles as a flag to check the mode of `@extend`. It is # `nothing` for explicit destructuring. # The following block modifies the arguments of both base and higher systems # for the implicit extend statements. - if additional_args !== nothing + if implicit_arglist !== nothing b.args = [b.args[1]] - allvars = [additional_args.args..., vars.args...] push!(b.args, Expr(:parameters)) - for var in allvars + for var in implicit_arglist.args push!(b.args[end].args, var) if !haskey(dict[:kwargs], var) push!(dict[:kwargs], var => Dict(:value => NO_VALUE)) @@ -814,8 +813,8 @@ function parse_extend!(exprs, ext, dict, mod, body, kwargs) end a, b = b.args # This doubles as a flag to identify the mode of `@extend` - additional_args = nothing - _parse_extend!(ext, a, b, dict, expr, kwargs, vars, additional_args) + implicit_arglist = nothing + _parse_extend!(ext, a, b, dict, expr, kwargs, vars, implicit_arglist) else error("When explicitly destructing in `@extend` please use the syntax: `@extend a, b = oneport = OnePort()`.") end @@ -825,11 +824,12 @@ function parse_extend!(exprs, ext, dict, mod, body, kwargs) b = body if (model = getproperty(mod, b.args[1])) isa Model vars = Expr(:tuple) - append!(vars.args, _arguments(model)) - additional_args = Expr(:tuple) - append!(additional_args.args, + append!(vars.args, names(model)) + implicit_arglist = Expr(:tuple) + append!(implicit_arglist.args, _arguments(model)) + append!(implicit_arglist.args, keys(get(model.structure, :structural_parameters, EMPTY_DICT))) - _parse_extend!(ext, a, b, dict, expr, kwargs, vars, additional_args) + _parse_extend!(ext, a, b, dict, expr, kwargs, vars, implicit_arglist) else error("Cannot infer the exact `Model` that `@extend $(body)` refers." * " Please specify the names that it brings into scope by:" * diff --git a/test/model_parsing.jl b/test/model_parsing.jl index 2dcb8e3f11..9cdd8712d4 100644 --- a/test/model_parsing.jl +++ b/test/model_parsing.jl @@ -932,3 +932,29 @@ end @test getdefault(main_sys.p2) == 12 @test getdefault(main_sys.v1) == 13 end + +@mtkmodel InnerModel begin + @parameters begin + p + end +end + +@mtkmodel MidModel begin + @components begin + inmodel = InnerModel() + end +end + +@mtkmodel OuterModel begin + @extend MidModel() + @equations begin + inmodel.p ~ 0 + end +end + +# The base system is fetched from the module while extending implicitly. This +# way of defining fails when defined inside the `@testset`. So, it is moved out. +@testset "Test unpacking of components in implicit extend" begin + @named out = OuterModel() + @test OuterModel.structure[:extend][1] == [:inmodel] +end From 2c2480937ee6cdc8e49e94695c11d6889a25f029 Mon Sep 17 00:00:00 2001 From: Sam Isaacson Date: Sun, 3 Nov 2024 14:29:20 -0500 Subject: [PATCH 0282/2176] cleanup JumpSystem constructor to match ODESystem better --- src/systems/jumps/jumpsystem.jl | 81 +++++++++++++++++++++++---------- 1 file changed, 57 insertions(+), 24 deletions(-) diff --git a/src/systems/jumps/jumpsystem.jl b/src/systems/jumps/jumpsystem.jl index 6409609a10..6a74c6018b 100644 --- a/src/systems/jumps/jumpsystem.jl +++ b/src/systems/jumps/jumpsystem.jl @@ -96,6 +96,11 @@ struct JumpSystem{U <: ArrayPartition} <: AbstractTimeDependentSystem """ discrete_events::Vector{SymbolicDiscreteCallback} """ + A `Vector{SymbolicContinuousCallback}` that model events. + The integrator will use root finding to guarantee that it steps at each zero crossing. + """ + continuous_events::Vector{SymbolicContinuousCallback} + """ Topologically sorted parameter dependency equations, where all symbols are parameters and the LHS is a single parameter. """ @@ -160,13 +165,31 @@ function JumpSystem(eqs, iv, unknowns, ps; metadata = nothing, gui_metadata = nothing, kwargs...) + + # variable processing, similar to ODESystem name === nothing && throw(ArgumentError("The `name` keyword must be provided. Please consider using the `@named` macro")) - eqs = scalarize.(eqs) - sysnames = nameof.(systems) - if length(unique(sysnames)) != length(sysnames) - throw(ArgumentError("System names must be unique.")) + iv′ = value(iv) + us′ = value.(unknowns) + ps′ = value.(ps) + parameter_dependencies, ps = process_parameter_dependencies(parameter_dependencies, ps′) + if !(isempty(default_u0) && isempty(default_p)) + Base.depwarn( + "`default_u0` and `default_p` are deprecated. Use `defaults` instead.", + :JumpSystem, force = true) end + defaults = todict(defaults) + var_to_name = Dict() + process_variables!(var_to_name, defaults, us′) + process_variables!(var_to_name, defaults, ps′) + process_variables!(var_to_name, defaults, [eq.lhs for eq in parameter_dependencies]) + process_variables!(var_to_name, defaults, [eq.rhs for eq in parameter_dependencies]) + defaults = Dict{Any, Any}(value(k) => value(v) for (k, v) in pairs(defaults) + if value(v) !== nothing) + isempty(observed) || collect_var_to_name!(var_to_name, (eq.lhs for eq in observed)) + + # equation processing + eqs = scalarize.(eqs) ap = ArrayPartition(MassActionJump[], ConstantRateJump[], VariableRateJump[]) for eq in eqs if eq isa MassActionJump @@ -179,30 +202,42 @@ function JumpSystem(eqs, iv, unknowns, ps; error("JumpSystem equations must contain MassActionJumps, ConstantRateJumps, or VariableRateJumps.") end end - if !(isempty(default_u0) && isempty(default_p)) - Base.depwarn( - "`default_u0` and `default_p` are deprecated. Use `defaults` instead.", - :JumpSystem, force = true) + + sysnames = nameof.(systems) + if length(unique(sysnames)) != length(sysnames) + throw(ArgumentError("System names must be unique.")) end - defaults = todict(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() - process_variables!(var_to_name, defaults, unknowns) - process_variables!(var_to_name, defaults, ps) - isempty(observed) || collect_var_to_name!(var_to_name, (eq.lhs for eq in observed)) (continuous_events === nothing) || error("JumpSystems currently only support discrete events.") disc_callbacks = SymbolicDiscreteCallbacks(discrete_events) - parameter_dependencies, ps = process_parameter_dependencies(parameter_dependencies, ps) + JumpSystem{typeof(ap)}(Threads.atomic_add!(SYSTEM_COUNT, UInt(1)), - ap, value(iv), unknowns, ps, var_to_name, observed, name, description, systems, + ap, iv′, us′, ps′, var_to_name, observed, name, description, systems, defaults, connector_type, disc_callbacks, parameter_dependencies, metadata, gui_metadata, checks = checks) end +##### MTK dispatches for JumpSystems ##### +function collect_vars!(unknowns, parameters, j::MassActionJump, iv; depth = 0, + op = Differential) + for field in (j.scaled_rates, j.reactant_stoch, j.net_stoch) + collect_vars!(unknowns, parameters, field, iv; depth, op) + end + return nothing +end + +function collect_vars!(unknowns, parameters, j::Union{ConstantRateJump,VariableRateJump}, + iv; depth = 0, op = Differential) + collect_vars!(unknowns, parameters, j.condition, iv; depth, op) + for eq in j.affect + (eq isa Equation) && collect_vars!(unknowns, parameters, eq, iv; depth, op) + end + return nothing +end + +########################################## + has_massactionjumps(js::JumpSystem) = !isempty(equations(js).x[1]) has_constantratejumps(js::JumpSystem) = !isempty(equations(js).x[2]) has_variableratejumps(js::JumpSystem) = !isempty(equations(js).x[3]) @@ -240,9 +275,8 @@ function assemble_vrj( outputvars = (value(affect.lhs) for affect in vrj.affect!) outputidxs = [unknowntoid[var] for var in outputvars] - affect = eval_or_rgf( - generate_affect_function(js, vrj.affect!, - outputidxs); eval_expression, eval_module) + affect = eval_or_rgf(generate_affect_function(js, vrj.affect!, outputidxs); + eval_expression, eval_module) VariableRateJump(rate, affect) end @@ -269,9 +303,8 @@ function assemble_crj( outputvars = (value(affect.lhs) for affect in crj.affect!) outputidxs = [unknowntoid[var] for var in outputvars] - affect = eval_or_rgf( - generate_affect_function(js, crj.affect!, - outputidxs); eval_expression, eval_module) + affect = eval_or_rgf(generate_affect_function(js, crj.affect!, outputidxs); + eval_expression, eval_module) ConstantRateJump(rate, affect) end From dfcbe0ff21cca35c3d9347d42651ad20d3900cb7 Mon Sep 17 00:00:00 2001 From: CompatHelper Julia Date: Mon, 4 Nov 2024 00:24:44 +0000 Subject: [PATCH 0283/2176] CompatHelper: bump compat for SimpleNonlinearSolve to 2, (keep existing compat) --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 656fef6173..1c821c46db 100644 --- a/Project.toml +++ b/Project.toml @@ -125,7 +125,7 @@ SciMLBase = "2.57.1" SciMLStructures = "1.0" Serialization = "1" Setfield = "0.7, 0.8, 1" -SimpleNonlinearSolve = "0.1.0, 1" +SimpleNonlinearSolve = "0.1.0, 1, 2" SparseArrays = "1" SpecialFunctions = "0.7, 0.8, 0.9, 0.10, 1.0, 2" StaticArrays = "0.10, 0.11, 0.12, 1.0" From 6468f207589667e365f428065b8bdb85fc430978 Mon Sep 17 00:00:00 2001 From: CompatHelper Julia Date: Mon, 4 Nov 2024 00:24:48 +0000 Subject: [PATCH 0284/2176] CompatHelper: bump compat for NonlinearSolve to 4, (keep existing compat) --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 656fef6173..ef8fce84c2 100644 --- a/Project.toml +++ b/Project.toml @@ -111,7 +111,7 @@ Libdl = "1" LinearAlgebra = "1" MLStyle = "0.4.17" NaNMath = "0.3, 1" -NonlinearSolve = "3.14" +NonlinearSolve = "3.14, 4" OffsetArrays = "1" OrderedCollections = "1" OrdinaryDiffEq = "6.82.0" From 4af92778f8fc72701887e0bf379783a55553a5c7 Mon Sep 17 00:00:00 2001 From: CompatHelper Julia Date: Mon, 4 Nov 2024 00:25:21 +0000 Subject: [PATCH 0285/2176] CompatHelper: bump compat for NonlinearSolve to 4 for package docs, (keep existing compat) --- docs/Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/Project.toml b/docs/Project.toml index 16fc8b4b07..edc956c3d2 100644 --- a/docs/Project.toml +++ b/docs/Project.toml @@ -30,7 +30,7 @@ Distributions = "0.25" Documenter = "1" DynamicQuantities = "^0.11.2, 0.12, 1" ModelingToolkit = "8.33, 9" -NonlinearSolve = "3" +NonlinearSolve = "3, 4" Optim = "1.7" Optimization = "3.9" OptimizationOptimJL = "0.1" From 9e04b036ed2250d29134647684b9bdff45cc625e Mon Sep 17 00:00:00 2001 From: Herman Sletmoen Date: Mon, 4 Nov 2024 14:02:20 +0100 Subject: [PATCH 0286/2176] Extend description and name in the same way --- src/systems/abstractsystem.jl | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index c53c900d27..10401d91c5 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -2995,13 +2995,14 @@ end """ $(TYPEDSIGNATURES) -Extend the `basesys` with `sys`, the resulting system would inherit `sys`'s name -by default. +Extend `basesys` with `sys`. +By default, the resulting system inherits `sys`'s name and description. See also [`compose`](@ref). """ -function extend(sys::AbstractSystem, basesys::AbstractSystem; name::Symbol = nameof(sys), - gui_metadata = get_gui_metadata(sys)) +function extend(sys::AbstractSystem, basesys::AbstractSystem; + name::Symbol = nameof(sys), description = description(sys), + gui_metadata = get_gui_metadata(sys)) T = SciMLBase.parameterless_type(basesys) ivs = independent_variables(basesys) if !(sys isa T) @@ -3023,13 +3024,12 @@ function extend(sys::AbstractSystem, basesys::AbstractSystem; name::Symbol = nam cevs = union(get_continuous_events(basesys), get_continuous_events(sys)) devs = union(get_discrete_events(basesys), get_discrete_events(sys)) defs = merge(get_defaults(basesys), get_defaults(sys)) # prefer `sys` - desc = join(filter(desc -> !isempty(desc), description.([sys, basesys])), " ") # concatenate non-empty descriptions with space meta = union_nothing(get_metadata(basesys), get_metadata(sys)) syss = union(get_systems(basesys), get_systems(sys)) args = length(ivs) == 0 ? (eqs, sts, ps) : (eqs, ivs[1], sts, ps) kwargs = (parameter_dependencies = dep_ps, observed = obs, continuous_events = cevs, discrete_events = devs, defaults = defs, systems = syss, metadata = meta, - name = name, description = desc, gui_metadata = gui_metadata) + name = name, description = description, gui_metadata = gui_metadata) # collect fields specific to some system types if basesys isa ODESystem @@ -3041,8 +3041,8 @@ function extend(sys::AbstractSystem, basesys::AbstractSystem; name::Symbol = nam return T(args...; kwargs...) end -function Base.:(&)(sys::AbstractSystem, basesys::AbstractSystem; name::Symbol = nameof(sys)) - extend(sys, basesys; name = name) +function Base.:(&)(sys::AbstractSystem, basesys::AbstractSystem; kwargs...) + extend(sys, basesys; kwargs...) end """ From 413d258c5e90f146657b588e898a8d342f430b9e Mon Sep 17 00:00:00 2001 From: Sam Isaacson Date: Mon, 4 Nov 2024 10:01:44 -0500 Subject: [PATCH 0287/2176] get tests workings --- src/systems/jumps/jumpsystem.jl | 32 ++++++++++++++--------------- test/jumpsystem.jl | 36 +++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 16 deletions(-) diff --git a/src/systems/jumps/jumpsystem.jl b/src/systems/jumps/jumpsystem.jl index 6a74c6018b..d59a2df5ce 100644 --- a/src/systems/jumps/jumpsystem.jl +++ b/src/systems/jumps/jumpsystem.jl @@ -96,11 +96,6 @@ struct JumpSystem{U <: ArrayPartition} <: AbstractTimeDependentSystem """ discrete_events::Vector{SymbolicDiscreteCallback} """ - A `Vector{SymbolicContinuousCallback}` that model events. - The integrator will use root finding to guarantee that it steps at each zero crossing. - """ - continuous_events::Vector{SymbolicContinuousCallback} - """ Topologically sorted parameter dependency equations, where all symbols are parameters and the LHS is a single parameter. """ @@ -172,13 +167,14 @@ function JumpSystem(eqs, iv, unknowns, ps; iv′ = value(iv) us′ = value.(unknowns) ps′ = value.(ps) - parameter_dependencies, ps = process_parameter_dependencies(parameter_dependencies, ps′) + parameter_dependencies, ps′ = process_parameter_dependencies( + parameter_dependencies, ps′) if !(isempty(default_u0) && isempty(default_p)) Base.depwarn( "`default_u0` and `default_p` are deprecated. Use `defaults` instead.", :JumpSystem, force = true) end - defaults = todict(defaults) + defaults = Dict{Any,Any}(todict(defaults)) var_to_name = Dict() process_variables!(var_to_name, defaults, us′) process_variables!(var_to_name, defaults, ps′) @@ -188,7 +184,14 @@ function JumpSystem(eqs, iv, unknowns, ps; if value(v) !== nothing) isempty(observed) || collect_var_to_name!(var_to_name, (eq.lhs for eq in observed)) + sysnames = nameof.(systems) + if length(unique(sysnames)) != length(sysnames) + throw(ArgumentError("System names must be unique.")) + end + # equation processing + # this and the treatment of continuous events are the only part + # unique to JumpSystems eqs = scalarize.(eqs) ap = ArrayPartition(MassActionJump[], ConstantRateJump[], VariableRateJump[]) for eq in eqs @@ -203,11 +206,6 @@ function JumpSystem(eqs, iv, unknowns, ps; end end - sysnames = nameof.(systems) - if length(unique(sysnames)) != length(sysnames) - throw(ArgumentError("System names must be unique.")) - end - (continuous_events === nothing) || error("JumpSystems currently only support discrete events.") disc_callbacks = SymbolicDiscreteCallbacks(discrete_events) @@ -220,17 +218,19 @@ end ##### MTK dispatches for JumpSystems ##### function collect_vars!(unknowns, parameters, j::MassActionJump, iv; depth = 0, - op = Differential) + op = Differential) for field in (j.scaled_rates, j.reactant_stoch, j.net_stoch) - collect_vars!(unknowns, parameters, field, iv; depth, op) + for el in field + collect_vars!(unknowns, parameters, el, iv; depth, op) + end end return nothing end function collect_vars!(unknowns, parameters, j::Union{ConstantRateJump,VariableRateJump}, iv; depth = 0, op = Differential) - collect_vars!(unknowns, parameters, j.condition, iv; depth, op) - for eq in j.affect + collect_vars!(unknowns, parameters, j.rate, iv; depth, op) + for eq in j.affect! (eq isa Equation) && collect_vars!(unknowns, parameters, eq, iv; depth, op) end return nothing diff --git a/test/jumpsystem.jl b/test/jumpsystem.jl index 24b9abbc8a..16181d1938 100644 --- a/test/jumpsystem.jl +++ b/test/jumpsystem.jl @@ -340,3 +340,39 @@ let @test all(abs.(cmean .- cmean2) .<= 0.05 .* cmean) end + + +# collect_vars! tests for jumps +let + @variables x1(t) x2(t) x3(t) x4(t) x5(t) + @parameters p1 p2 p3 p4 p5 + j1 = ConstantRateJump(p1, [x1 ~ x1 + 1]) + j2 = MassActionJump(p2, [x2 => 1], [x3 => -1]) + j3 = VariableRateJump(p3, [x3 ~ x3 + 1, x4 ~ x4 + 1]) + j4 = MassActionJump(p4*p5, [x1 => 1, x5 => 1], [x1 => -1, x5 => -1, x2 => 1]) + us = Set() + ps = Set() + iv = t + + MT.collect_vars!(us, ps, j1, iv) + @test issetequal(us, [x1]) + @test issetequal(ps, [p1]) + + empty!(us) + empty!(ps) + MT.collect_vars!(us, ps, j2, iv) + @test issetequal(us, [x2, x3]) + @test issetequal(ps, [p2]) + + empty!(us) + empty!(ps) + MT.collect_vars!(us, ps, j3, iv) + @test issetequal(us, [x3, x4]) + @test issetequal(ps, [p3]) + + empty!(us) + empty!(ps) + MT.collect_vars!(us, ps, j4, iv) + @test issetequal(us, [x1, x5, x2]) + @test issetequal(ps, [p4, p5]) +end From 4daf8846d9b9555b3db071bde36c708d4ec2ce30 Mon Sep 17 00:00:00 2001 From: Sam Isaacson Date: Mon, 4 Nov 2024 10:10:08 -0500 Subject: [PATCH 0288/2176] tweak collect_vars --- src/systems/jumps/jumpsystem.jl | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/systems/jumps/jumpsystem.jl b/src/systems/jumps/jumpsystem.jl index d59a2df5ce..f444a2480e 100644 --- a/src/systems/jumps/jumpsystem.jl +++ b/src/systems/jumps/jumpsystem.jl @@ -219,7 +219,8 @@ end ##### MTK dispatches for JumpSystems ##### function collect_vars!(unknowns, parameters, j::MassActionJump, iv; depth = 0, op = Differential) - for field in (j.scaled_rates, j.reactant_stoch, j.net_stoch) + collect_vars!(unknowns, parameters, j.scaled_rates, iv; depth, op) + for field in (j.reactant_stoch, j.net_stoch) for el in field collect_vars!(unknowns, parameters, el, iv; depth, op) end From 75f4ec6121b1c99eab4fef12a084b1df6c3067a0 Mon Sep 17 00:00:00 2001 From: Sam Isaacson Date: Mon, 4 Nov 2024 11:38:26 -0500 Subject: [PATCH 0289/2176] update collecting vars with scoping --- src/systems/abstractsystem.jl | 17 ++++++------ src/systems/jumps/jumpsystem.jl | 2 ++ test/jumpsystem.jl | 46 +++++++++++++++++++++++++++++++++ 3 files changed, 56 insertions(+), 9 deletions(-) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index c53c900d27..829310927f 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -923,15 +923,14 @@ One property to note is that if a system is complete, the system will no longer namespace its subsystems or variables, i.e. `isequal(complete(sys).v.i, v.i)`. """ function complete(sys::AbstractSystem; split = true, flatten = true) - if !(sys isa JumpSystem) - newunknowns = OrderedSet() - newparams = OrderedSet() - iv = has_iv(sys) ? get_iv(sys) : nothing - collect_scoped_vars!(newunknowns, newparams, sys, iv; depth = -1) - # don't update unknowns to not disturb `structural_simplify` order - # `GlobalScope`d unknowns will be picked up and added there - @set! sys.ps = unique!(vcat(get_ps(sys), collect(newparams))) - end + newunknowns = OrderedSet() + newparams = OrderedSet() + iv = has_iv(sys) ? get_iv(sys) : nothing + collect_scoped_vars!(newunknowns, newparams, sys, iv; depth = -1) + # don't update unknowns to not disturb `structural_simplify` order + # `GlobalScope`d unknowns will be picked up and added there + @set! sys.ps = unique!(vcat(get_ps(sys), collect(newparams))) + if flatten eqs = equations(sys) if eqs isa AbstractArray && eltype(eqs) <: Equation diff --git a/src/systems/jumps/jumpsystem.jl b/src/systems/jumps/jumpsystem.jl index f444a2480e..2fb92db62f 100644 --- a/src/systems/jumps/jumpsystem.jl +++ b/src/systems/jumps/jumpsystem.jl @@ -217,6 +217,7 @@ function JumpSystem(eqs, iv, unknowns, ps; end ##### MTK dispatches for JumpSystems ##### +eqtype_supports_collect_vars(j::MassActionJump) = true function collect_vars!(unknowns, parameters, j::MassActionJump, iv; depth = 0, op = Differential) collect_vars!(unknowns, parameters, j.scaled_rates, iv; depth, op) @@ -228,6 +229,7 @@ function collect_vars!(unknowns, parameters, j::MassActionJump, iv; depth = 0, return nothing end +eqtype_supports_collect_vars(j::Union{ConstantRateJump,VariableRateJump}) = true function collect_vars!(unknowns, parameters, j::Union{ConstantRateJump,VariableRateJump}, iv; depth = 0, op = Differential) collect_vars!(unknowns, parameters, j.rate, iv; depth, op) diff --git a/test/jumpsystem.jl b/test/jumpsystem.jl index 16181d1938..693cd39ddd 100644 --- a/test/jumpsystem.jl +++ b/test/jumpsystem.jl @@ -376,3 +376,49 @@ let @test issetequal(us, [x1, x5, x2]) @test issetequal(ps, [p4, p5]) end + +# scoping tests +let + @variables x1(t) x2(t) x3(t) x4(t) x5(t) + x2 = ParentScope(x2) + x3 = ParentScope(ParentScope(x3)) + x4 = DelayParentScope(x4, 2) + x5 = GlobalScope(x5) + @parameters p1 p2 p3 p4 p5 + p2 = ParentScope(p2) + p3 = ParentScope(ParentScope(p3)) + p4 = DelayParentScope(p4, 2) + p5 = GlobalScope(p5) + + j1 = ConstantRateJump(p1, [x1 ~ x1 + 1]) + j2 = MassActionJump(p2, [x2 => 1], [x3 => -1]) + j3 = VariableRateJump(p3, [x3 ~ x3 + 1, x4 ~ x4 + 1]) + j4 = MassActionJump(p4*p5, [x1 => 1, x5 => 1], [x1 => -1, x5 => -1, x2 => 1]) + @named js = JumpSystem([j1, j2, j3, j4], t, [x1, x2, x3, x4, x5], [p1, p2, p3, p4, p5]) + + us = Set(); ps = Set() + iv = t + MT.collect_scoped_vars!(us, ps, js, iv) + @test issetequal(us, [x2]) + @test issetequal(ps, [p2]) + + empty!.((us,ps)) + MT.collect_scoped_vars!(us, ps, js, iv; depth = 0) + @test issetequal(us, [x1]) + @test issetequal(ps, [p1]) + + empty!.((us,ps)) + MT.collect_scoped_vars!(us, ps, js, iv; depth = 1) + @test issetequal(us, [x2]) + @test issetequal(ps, [p2]) + + empty!.((us,ps)) + MT.collect_scoped_vars!(us, ps, js, iv; depth = 2) + @test issetequal(us, [x3, x4]) + @test issetequal(ps, [p3, p4]) + + empty!.((us,ps)) + MT.collect_scoped_vars!(us, ps, js, iv; depth = -1) + @test issetequal(us, [x5]) + @test issetequal(ps, [p5]) +end From e6ebcb9b5ca21e796670d731ff6f4913ce21a1ac Mon Sep 17 00:00:00 2001 From: Sam Isaacson Date: Mon, 4 Nov 2024 13:09:34 -0500 Subject: [PATCH 0290/2176] format --- src/systems/abstractsystem.jl | 2 +- src/systems/jumps/jumpsystem.jl | 19 ++++++++++--------- test/jumpsystem.jl | 20 ++++++++++---------- 3 files changed, 21 insertions(+), 20 deletions(-) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index 829310927f..e565bb27e1 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -930,7 +930,7 @@ function complete(sys::AbstractSystem; split = true, flatten = true) # don't update unknowns to not disturb `structural_simplify` order # `GlobalScope`d unknowns will be picked up and added there @set! sys.ps = unique!(vcat(get_ps(sys), collect(newparams))) - + if flatten eqs = equations(sys) if eqs isa AbstractArray && eltype(eqs) <: Equation diff --git a/src/systems/jumps/jumpsystem.jl b/src/systems/jumps/jumpsystem.jl index 2fb92db62f..9fa073b4b0 100644 --- a/src/systems/jumps/jumpsystem.jl +++ b/src/systems/jumps/jumpsystem.jl @@ -174,14 +174,15 @@ function JumpSystem(eqs, iv, unknowns, ps; "`default_u0` and `default_p` are deprecated. Use `defaults` instead.", :JumpSystem, force = true) end - defaults = Dict{Any,Any}(todict(defaults)) + defaults = Dict{Any, Any}(todict(defaults)) var_to_name = Dict() process_variables!(var_to_name, defaults, us′) process_variables!(var_to_name, defaults, ps′) process_variables!(var_to_name, defaults, [eq.lhs for eq in parameter_dependencies]) process_variables!(var_to_name, defaults, [eq.rhs for eq in parameter_dependencies]) - defaults = Dict{Any, Any}(value(k) => value(v) for (k, v) in pairs(defaults) - if value(v) !== nothing) + #! format: off + defaults = Dict{Any, Any}(value(k) => value(v) for (k, v) in pairs(defaults) if value(v) !== nothing) + #! format: on isempty(observed) || collect_var_to_name!(var_to_name, (eq.lhs for eq in observed)) sysnames = nameof.(systems) @@ -218,8 +219,8 @@ end ##### MTK dispatches for JumpSystems ##### eqtype_supports_collect_vars(j::MassActionJump) = true -function collect_vars!(unknowns, parameters, j::MassActionJump, iv; depth = 0, - op = Differential) +function collect_vars!(unknowns, parameters, j::MassActionJump, iv; depth = 0, + op = Differential) collect_vars!(unknowns, parameters, j.scaled_rates, iv; depth, op) for field in (j.reactant_stoch, j.net_stoch) for el in field @@ -229,8 +230,8 @@ function collect_vars!(unknowns, parameters, j::MassActionJump, iv; depth = 0, return nothing end -eqtype_supports_collect_vars(j::Union{ConstantRateJump,VariableRateJump}) = true -function collect_vars!(unknowns, parameters, j::Union{ConstantRateJump,VariableRateJump}, +eqtype_supports_collect_vars(j::Union{ConstantRateJump, VariableRateJump}) = true +function collect_vars!(unknowns, parameters, j::Union{ConstantRateJump, VariableRateJump}, iv; depth = 0, op = Differential) collect_vars!(unknowns, parameters, j.rate, iv; depth, op) for eq in j.affect! @@ -278,7 +279,7 @@ function assemble_vrj( outputvars = (value(affect.lhs) for affect in vrj.affect!) outputidxs = [unknowntoid[var] for var in outputvars] - affect = eval_or_rgf(generate_affect_function(js, vrj.affect!, outputidxs); + affect = eval_or_rgf(generate_affect_function(js, vrj.affect!, outputidxs); eval_expression, eval_module) VariableRateJump(rate, affect) end @@ -306,7 +307,7 @@ function assemble_crj( outputvars = (value(affect.lhs) for affect in crj.affect!) outputidxs = [unknowntoid[var] for var in outputvars] - affect = eval_or_rgf(generate_affect_function(js, crj.affect!, outputidxs); + affect = eval_or_rgf(generate_affect_function(js, crj.affect!, outputidxs); eval_expression, eval_module) ConstantRateJump(rate, affect) end diff --git a/test/jumpsystem.jl b/test/jumpsystem.jl index 693cd39ddd..30cb636226 100644 --- a/test/jumpsystem.jl +++ b/test/jumpsystem.jl @@ -341,15 +341,14 @@ let @test all(abs.(cmean .- cmean2) .<= 0.05 .* cmean) end - # collect_vars! tests for jumps -let +let @variables x1(t) x2(t) x3(t) x4(t) x5(t) @parameters p1 p2 p3 p4 p5 j1 = ConstantRateJump(p1, [x1 ~ x1 + 1]) j2 = MassActionJump(p2, [x2 => 1], [x3 => -1]) j3 = VariableRateJump(p3, [x3 ~ x3 + 1, x4 ~ x4 + 1]) - j4 = MassActionJump(p4*p5, [x1 => 1, x5 => 1], [x1 => -1, x5 => -1, x2 => 1]) + j4 = MassActionJump(p4 * p5, [x1 => 1, x5 => 1], [x1 => -1, x5 => -1, x2 => 1]) us = Set() ps = Set() iv = t @@ -389,35 +388,36 @@ let p3 = ParentScope(ParentScope(p3)) p4 = DelayParentScope(p4, 2) p5 = GlobalScope(p5) - + j1 = ConstantRateJump(p1, [x1 ~ x1 + 1]) j2 = MassActionJump(p2, [x2 => 1], [x3 => -1]) j3 = VariableRateJump(p3, [x3 ~ x3 + 1, x4 ~ x4 + 1]) - j4 = MassActionJump(p4*p5, [x1 => 1, x5 => 1], [x1 => -1, x5 => -1, x2 => 1]) + j4 = MassActionJump(p4 * p5, [x1 => 1, x5 => 1], [x1 => -1, x5 => -1, x2 => 1]) @named js = JumpSystem([j1, j2, j3, j4], t, [x1, x2, x3, x4, x5], [p1, p2, p3, p4, p5]) - us = Set(); ps = Set() + us = Set() + ps = Set() iv = t MT.collect_scoped_vars!(us, ps, js, iv) @test issetequal(us, [x2]) @test issetequal(ps, [p2]) - empty!.((us,ps)) + empty!.((us, ps)) MT.collect_scoped_vars!(us, ps, js, iv; depth = 0) @test issetequal(us, [x1]) @test issetequal(ps, [p1]) - empty!.((us,ps)) + empty!.((us, ps)) MT.collect_scoped_vars!(us, ps, js, iv; depth = 1) @test issetequal(us, [x2]) @test issetequal(ps, [p2]) - empty!.((us,ps)) + empty!.((us, ps)) MT.collect_scoped_vars!(us, ps, js, iv; depth = 2) @test issetequal(us, [x3, x4]) @test issetequal(ps, [p3, p4]) - empty!.((us,ps)) + empty!.((us, ps)) MT.collect_scoped_vars!(us, ps, js, iv; depth = -1) @test issetequal(us, [x5]) @test issetequal(ps, [p5]) From 29ffd992535ddcd5427d69905b6905719c747442 Mon Sep 17 00:00:00 2001 From: Sam Isaacson Date: Mon, 4 Nov 2024 14:17:22 -0500 Subject: [PATCH 0291/2176] add equations --- src/systems/jumps/jumpsystem.jl | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/systems/jumps/jumpsystem.jl b/src/systems/jumps/jumpsystem.jl index 9fa073b4b0..8b2e8e5ded 100644 --- a/src/systems/jumps/jumpsystem.jl +++ b/src/systems/jumps/jumpsystem.jl @@ -194,7 +194,8 @@ function JumpSystem(eqs, iv, unknowns, ps; # this and the treatment of continuous events are the only part # unique to JumpSystems eqs = scalarize.(eqs) - ap = ArrayPartition(MassActionJump[], ConstantRateJump[], VariableRateJump[]) + ap = ArrayPartition( + MassActionJump[], ConstantRateJump[], VariableRateJump[], Equation[]) for eq in eqs if eq isa MassActionJump push!(ap.x[1], eq) @@ -202,8 +203,10 @@ function JumpSystem(eqs, iv, unknowns, ps; push!(ap.x[2], eq) elseif eq isa VariableRateJump push!(ap.x[3], eq) + elseif eq isa Equation + push!(ap.x[4], eq) else - error("JumpSystem equations must contain MassActionJumps, ConstantRateJumps, or VariableRateJumps.") + error("JumpSystem equations must contain MassActionJumps, ConstantRateJumps, VariableRateJumps, or Equations.") end end @@ -245,6 +248,7 @@ end has_massactionjumps(js::JumpSystem) = !isempty(equations(js).x[1]) has_constantratejumps(js::JumpSystem) = !isempty(equations(js).x[2]) has_variableratejumps(js::JumpSystem) = !isempty(equations(js).x[3]) +has_equations(js::JumpSystem) = !isempty(equations(js).x[4]) function generate_rate_function(js::JumpSystem, rate) consts = collect_constants(rate) @@ -390,6 +394,11 @@ function DiffEqBase.DiscreteProblem(sys::JumpSystem, u0map, tspan::Union{Tuple, if !iscomplete(sys) error("A completed `JumpSystem` is required. Call `complete` or `structural_simplify` on the system before creating a `DiscreteProblem`") end + + if has_equations(sys) + error("The passed in JumpSystem contains `Equations`, please use a problem type that supports equations such as such as ODEProblem.") + end + _, u0, p = process_SciMLProblem(EmptySciMLFunction, sys, u0map, parammap; t = tspan === nothing ? nothing : tspan[1], use_union, tofloat = false, check_length = false) f = DiffEqBase.DISCRETE_INPLACE_DEFAULT From 6a720421d8ebc628b8b9e97f868916954a66b36d Mon Sep 17 00:00:00 2001 From: Sam Isaacson Date: Mon, 4 Nov 2024 15:06:43 -0500 Subject: [PATCH 0292/2176] hybrid jump-ODEs --- src/systems/jumps/jumpsystem.jl | 32 ++++++++++++++++++++++---------- test/jumpsystem.jl | 22 ++++++++++++++++++++++ 2 files changed, 44 insertions(+), 10 deletions(-) diff --git a/src/systems/jumps/jumpsystem.jl b/src/systems/jumps/jumpsystem.jl index 8b2e8e5ded..7950d2cc98 100644 --- a/src/systems/jumps/jumpsystem.jl +++ b/src/systems/jumps/jumpsystem.jl @@ -482,19 +482,31 @@ function DiffEqBase.ODEProblem(sys::JumpSystem, u0map, tspan::Union{Tuple, Nothi use_union = false, eval_expression = false, eval_module = @__MODULE__, + check_length = false, kwargs...) if !iscomplete(sys) error("A completed `JumpSystem` is required. Call `complete` or `structural_simplify` on the system before creating a `DiscreteProblem`") end - _, u0, p = process_SciMLProblem(EmptySciMLFunction, sys, u0map, parammap; - t = tspan === nothing ? nothing : tspan[1], use_union, tofloat = false, check_length = false) - - observedfun = ObservedFunctionCache(sys; eval_expression, eval_module) - - f = (du, u, p, t) -> (du .= 0; nothing) - df = ODEFunction(f; sys, observed = observedfun) - ODEProblem(df, u0, tspan, p; kwargs...) + # forward everything to be an ODESystem but the jumps + if has_equations(sys) + osys = ODESystem(equations(sys).x[4], get_iv(sys), unknowns(sys), parameters(sys); + observed = observed(sys), name = nameof(sys), description = description(sys), + systems = get_systems(sys), defaults = defaults(sys), + discrete_events = discrete_events(sys), + parameter_dependencies = parameter_dependencies(sys), + metadata = get_metadata(sys), gui_metadata = get_gui_metadata(sys)) + osys = complete(osys) + return ODEProblem(osys, u0map, tspan, parammap; check_length, kwargs...) + else + _, u0, p = process_SciMLProblem(EmptySciMLFunction, sys, u0map, parammap; + t = tspan === nothing ? nothing : tspan[1], use_union, tofloat = false, + check_length = false) + f = (du, u, p, t) -> (du .= 0; nothing) + observedfun = ObservedFunctionCache(sys; eval_expression, eval_module) + df = ODEFunction(f; sys, observed = observedfun) + return ODEProblem(df, u0, tspan, p; check_length, kwargs...) + end end """ @@ -530,8 +542,8 @@ function JumpProcesses.JumpProblem(js::JumpSystem, prob, for j in eqs.x[2]] vrjs = VariableRateJump[assemble_vrj(js, j, unknowntoid; eval_expression, eval_module) for j in eqs.x[3]] - ((prob isa DiscreteProblem) && !isempty(vrjs)) && - error("Use continuous problems such as an ODEProblem or a SDEProblem with VariableRateJumps") + ((prob isa DiscreteProblem) && (!isempty(vrjs) || has_equations(js))) && + error("Use continuous problems such as an ODEProblem or a SDEProblem with VariableRateJumps and/or coupled differential equations.") jset = JumpSet(Tuple(vrjs), Tuple(crjs), nothing, majs) # dep graphs are only for constant rate jumps diff --git a/test/jumpsystem.jl b/test/jumpsystem.jl index 30cb636226..76d11f6205 100644 --- a/test/jumpsystem.jl +++ b/test/jumpsystem.jl @@ -422,3 +422,25 @@ let @test issetequal(us, [x5]) @test issetequal(ps, [p5]) end + +# PDMP test +let + @variables X(t) Y(t) + @parameters k1 k2 + rate1 = k1 * X + affect1! = [X ~ X - 1] + rate2 = k1 + affect2! = [Y ~ Y + 1] + eqs = [D(X) ~ k2, D(Y) ~ -k2/100*Y] + vrj1 = VariableRateJump(rate1, affect1!) + vrj2 = VariableRateJump(rate2, affect2!) + @named jsys = JumpSystem([vrj1, vrj2, eqs[1], eqs[2]], t, [X, Y], [k1, k2]) + jsys = complete(jsys) + u0 = [X => 0.0, Y => 0.0] + p = [k1 => 1.0, k2 => 20.0] + tspan = (0.0, 20.0) + oprob = ODEProblem(jsys, u0, tspan, p) + jprob = JumpProblem(jsys, oprob; rng) + sol = solve(jprob, Tsit5()) + +end \ No newline at end of file From 922800bf24c38bc3d63fc6c66547e2734084e288 Mon Sep 17 00:00:00 2001 From: Sam Isaacson Date: Mon, 4 Nov 2024 15:42:56 -0500 Subject: [PATCH 0293/2176] add continuous events --- src/systems/jumps/jumpsystem.jl | 32 +++++++++++++++++++++----------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/src/systems/jumps/jumpsystem.jl b/src/systems/jumps/jumpsystem.jl index 7950d2cc98..f046b7ed78 100644 --- a/src/systems/jumps/jumpsystem.jl +++ b/src/systems/jumps/jumpsystem.jl @@ -88,6 +88,11 @@ struct JumpSystem{U <: ArrayPartition} <: AbstractTimeDependentSystem """ connector_type::Any """ + A `Vector{SymbolicContinuousCallback}` that model events. + The integrator will use root finding to guarantee that it steps at each zero crossing. + """ + continuous_events::Vector{SymbolicContinuousCallback} + """ A `Vector{SymbolicDiscreteCallback}` that models events. Symbolic analog to `SciMLBase.DiscreteCallback` that executes an affect when a given condition is true at the end of an integration step. Note, one must make sure to call @@ -120,8 +125,7 @@ struct JumpSystem{U <: ArrayPartition} <: AbstractTimeDependentSystem function JumpSystem{U}( tag, ap::U, iv, unknowns, ps, var_to_name, observed, name, description, - systems, - defaults, connector_type, devents, parameter_dependencies, + systems, defaults, connector_type, cevents, devents, parameter_dependencies, metadata = nothing, gui_metadata = nothing, complete = false, index_cache = nothing, isscheduled = false; checks::Union{Bool, Int} = true) where {U <: ArrayPartition} @@ -136,8 +140,8 @@ struct JumpSystem{U <: ArrayPartition} <: AbstractTimeDependentSystem end new{U}(tag, ap, iv, unknowns, ps, var_to_name, observed, name, description, systems, defaults, - connector_type, devents, parameter_dependencies, metadata, gui_metadata, - complete, index_cache, isscheduled) + connector_type, cevents, devents, parameter_dependencies, metadata, + gui_metadata, complete, index_cache, isscheduled) end end function JumpSystem(tag, ap, iv, states, ps, var_to_name, args...; kwargs...) @@ -210,13 +214,12 @@ function JumpSystem(eqs, iv, unknowns, ps; end end - (continuous_events === nothing) || - error("JumpSystems currently only support discrete events.") + cont_callbacks = SymbolicContinuousCallbacks(continuous_events) disc_callbacks = SymbolicDiscreteCallbacks(discrete_events) JumpSystem{typeof(ap)}(Threads.atomic_add!(SYSTEM_COUNT, UInt(1)), ap, iv′, us′, ps′, var_to_name, observed, name, description, systems, - defaults, connector_type, disc_callbacks, parameter_dependencies, + defaults, connector_type, cont_callbacks, disc_callbacks, parameter_dependencies, metadata, gui_metadata, checks = checks) end @@ -399,6 +402,10 @@ function DiffEqBase.DiscreteProblem(sys::JumpSystem, u0map, tspan::Union{Tuple, error("The passed in JumpSystem contains `Equations`, please use a problem type that supports equations such as such as ODEProblem.") end + if !isempty(continuous_events(sys)) + error("The passed in JumpSystem contains `ContinuousEvents`, please use a problem type that supports continuous events such as such as ODEProblem.") + end + _, u0, p = process_SciMLProblem(EmptySciMLFunction, sys, u0map, parammap; t = tspan === nothing ? nothing : tspan[1], use_union, tofloat = false, check_length = false) f = DiffEqBase.DISCRETE_INPLACE_DEFAULT @@ -488,12 +495,12 @@ function DiffEqBase.ODEProblem(sys::JumpSystem, u0map, tspan::Union{Tuple, Nothi error("A completed `JumpSystem` is required. Call `complete` or `structural_simplify` on the system before creating a `DiscreteProblem`") end - # forward everything to be an ODESystem but the jumps + # forward everything to be an ODESystem but the jumps and discrete events if has_equations(sys) osys = ODESystem(equations(sys).x[4], get_iv(sys), unknowns(sys), parameters(sys); observed = observed(sys), name = nameof(sys), description = description(sys), systems = get_systems(sys), defaults = defaults(sys), - discrete_events = discrete_events(sys), + continuous_events = continuous_events(sys), parameter_dependencies = parameter_dependencies(sys), metadata = get_metadata(sys), gui_metadata = get_gui_metadata(sys)) osys = complete(osys) @@ -542,8 +549,11 @@ function JumpProcesses.JumpProblem(js::JumpSystem, prob, for j in eqs.x[2]] vrjs = VariableRateJump[assemble_vrj(js, j, unknowntoid; eval_expression, eval_module) for j in eqs.x[3]] - ((prob isa DiscreteProblem) && (!isempty(vrjs) || has_equations(js))) && - error("Use continuous problems such as an ODEProblem or a SDEProblem with VariableRateJumps and/or coupled differential equations.") + if prob isa DiscreteProblem + if (!isempty(vrjs) || has_equations(js) || !isempty(continous_events(js))) + error("Use continuous problems such as an ODEProblem or a SDEProblem with VariableRateJumps, coupled differential equations, or continuous events.") + end + end jset = JumpSet(Tuple(vrjs), Tuple(crjs), nothing, majs) # dep graphs are only for constant rate jumps From a6c5c6cf9366686ad078084c2ce92d8a8e58cde2 Mon Sep 17 00:00:00 2001 From: Sam Isaacson Date: Mon, 4 Nov 2024 15:45:58 -0500 Subject: [PATCH 0294/2176] typos --- src/systems/jumps/jumpsystem.jl | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/systems/jumps/jumpsystem.jl b/src/systems/jumps/jumpsystem.jl index f046b7ed78..4cf82e4bec 100644 --- a/src/systems/jumps/jumpsystem.jl +++ b/src/systems/jumps/jumpsystem.jl @@ -398,12 +398,8 @@ function DiffEqBase.DiscreteProblem(sys::JumpSystem, u0map, tspan::Union{Tuple, error("A completed `JumpSystem` is required. Call `complete` or `structural_simplify` on the system before creating a `DiscreteProblem`") end - if has_equations(sys) - error("The passed in JumpSystem contains `Equations`, please use a problem type that supports equations such as such as ODEProblem.") - end - - if !isempty(continuous_events(sys)) - error("The passed in JumpSystem contains `ContinuousEvents`, please use a problem type that supports continuous events such as such as ODEProblem.") + if has_equations(sys) || (!isempty(continuous_events(sys))) + error("The passed in JumpSystem contains `Equation`s or continuous events, please use a problem type that supports these features, such as ODEProblem.") end _, u0, p = process_SciMLProblem(EmptySciMLFunction, sys, u0map, parammap; From 4f1cd262144f152d67fc9ebbaefeee9d59c7751d Mon Sep 17 00:00:00 2001 From: Sam Isaacson Date: Mon, 4 Nov 2024 17:04:07 -0500 Subject: [PATCH 0295/2176] spelling --- src/systems/jumps/jumpsystem.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/systems/jumps/jumpsystem.jl b/src/systems/jumps/jumpsystem.jl index 4cf82e4bec..94cf2b67e8 100644 --- a/src/systems/jumps/jumpsystem.jl +++ b/src/systems/jumps/jumpsystem.jl @@ -546,7 +546,7 @@ function JumpProcesses.JumpProblem(js::JumpSystem, prob, vrjs = VariableRateJump[assemble_vrj(js, j, unknowntoid; eval_expression, eval_module) for j in eqs.x[3]] if prob isa DiscreteProblem - if (!isempty(vrjs) || has_equations(js) || !isempty(continous_events(js))) + if (!isempty(vrjs) || has_equations(js) || !isempty(continuous_events(js))) error("Use continuous problems such as an ODEProblem or a SDEProblem with VariableRateJumps, coupled differential equations, or continuous events.") end end From eda30b9c5e165798bf64f6170cce148ebce9526e Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 5 Nov 2024 14:20:47 +0530 Subject: [PATCH 0296/2176] fix: `complete` with `split = false` removes the index cache --- src/systems/abstractsystem.jl | 2 ++ test/odesystem.jl | 9 +++++++++ 2 files changed, 11 insertions(+) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index 10401d91c5..9a2b2369bd 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -985,6 +985,8 @@ function complete(sys::AbstractSystem; split = true, flatten = true) end @set! sys.ps = ordered_ps end + elseif has_index_cache(sys) + @set! sys.index_cache = nothing end if isdefined(sys, :initializesystem) && get_initializesystem(sys) !== nothing @set! sys.initializesystem = complete(get_initializesystem(sys); split) diff --git a/test/odesystem.jl b/test/odesystem.jl index dfc755da98..d7a2658f63 100644 --- a/test/odesystem.jl +++ b/test/odesystem.jl @@ -1495,3 +1495,12 @@ end sys2 = complete(sys) @test length(equations(sys2)) == total_eqs end + +@testset "`complete` with `split = false` removes the index cache" begin + @variables x(t) + @parameters p + @mtkbuild sys = ODESystem(D(x) ~ p * t, t) + @test ModelingToolkit.get_index_cache(sys) !== nothing + sys2 = complete(sys; split = false) + @test ModelingToolkit.get_index_cache(sys2) === nothing +end From eaf5edc646b41d41f30878cb0b368756ba0c017c Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 5 Nov 2024 14:20:51 +0530 Subject: [PATCH 0297/2176] refactor: format --- src/systems/abstractsystem.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index 9a2b2369bd..80bc3d44d4 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -3003,8 +3003,8 @@ By default, the resulting system inherits `sys`'s name and description. See also [`compose`](@ref). """ function extend(sys::AbstractSystem, basesys::AbstractSystem; - name::Symbol = nameof(sys), description = description(sys), - gui_metadata = get_gui_metadata(sys)) + name::Symbol = nameof(sys), description = description(sys), + gui_metadata = get_gui_metadata(sys)) T = SciMLBase.parameterless_type(basesys) ivs = independent_variables(basesys) if !(sys isa T) From 022af77d450f68fd611885afb77cb8de5820f065 Mon Sep 17 00:00:00 2001 From: Sam Isaacson Date: Tue, 5 Nov 2024 14:42:41 -0500 Subject: [PATCH 0298/2176] Add continuous event support --- src/systems/callbacks.jl | 16 ++++++++-------- src/systems/jumps/jumpsystem.jl | 6 ++---- 2 files changed, 10 insertions(+), 12 deletions(-) diff --git a/src/systems/callbacks.jl b/src/systems/callbacks.jl index c427ae02a1..9ca69bf151 100644 --- a/src/systems/callbacks.jl +++ b/src/systems/callbacks.jl @@ -1,6 +1,6 @@ #################################### system operations ##################################### get_continuous_events(sys::AbstractSystem) = SymbolicContinuousCallback[] -get_continuous_events(sys::AbstractODESystem) = getfield(sys, :continuous_events) +get_continuous_events(sys::AbstractTimeDependentSystem) = getfield(sys, :continuous_events) has_continuous_events(sys::AbstractSystem) = isdefined(sys, :continuous_events) has_discrete_events(sys::AbstractSystem) = isdefined(sys, :discrete_events) @@ -560,7 +560,7 @@ function compile_affect(eqs::Vector{Equation}, cb, sys, dvs, ps; outputidxs = no end end -function generate_rootfinding_callback(sys::AbstractODESystem, dvs = unknowns(sys), +function generate_rootfinding_callback(sys::AbstractTimeDependentSystem, dvs = unknowns(sys), ps = parameters(sys); kwargs...) cbs = continuous_events(sys) isempty(cbs) && return nothing @@ -571,7 +571,7 @@ Generate a single rootfinding callback; this happens if there is only one equati generate_rootfinding_callback and thus we can produce a ContinuousCallback instead of a VectorContinuousCallback. """ function generate_single_rootfinding_callback( - eq, cb, sys::AbstractODESystem, dvs = unknowns(sys), + eq, cb, sys::AbstractTimeDependentSystem, dvs = unknowns(sys), ps = parameters(sys); kwargs...) if !isequal(eq.lhs, 0) eq = 0 ~ eq.lhs - eq.rhs @@ -609,7 +609,7 @@ function generate_single_rootfinding_callback( end function generate_vector_rootfinding_callback( - cbs, sys::AbstractODESystem, dvs = unknowns(sys), + cbs, sys::AbstractTimeDependentSystem, dvs = unknowns(sys), ps = parameters(sys); rootfind = SciMLBase.RightRootFind, reinitialization = SciMLBase.CheckInit(), kwargs...) eqs = map(cb -> flatten_equations(cb.eqs), cbs) @@ -683,7 +683,7 @@ end """ Compile a single continuous callback affect function(s). """ -function compile_affect_fn(cb, sys::AbstractODESystem, dvs, ps, kwargs) +function compile_affect_fn(cb, sys::AbstractTimeDependentSystem, dvs, ps, kwargs) eq_aff = affects(cb) eq_neg_aff = affect_negs(cb) affect = compile_affect(eq_aff, cb, sys, dvs, ps; expression = Val{false}, kwargs...) @@ -698,7 +698,7 @@ function compile_affect_fn(cb, sys::AbstractODESystem, dvs, ps, kwargs) (affect = affect, affect_neg = affect_neg) end -function generate_rootfinding_callback(cbs, sys::AbstractODESystem, dvs = unknowns(sys), +function generate_rootfinding_callback(cbs, sys::AbstractTimeDependentSystem, dvs = unknowns(sys), ps = parameters(sys); kwargs...) eqs = map(cb -> flatten_equations(cb.eqs), cbs) num_eqs = length.(eqs) @@ -859,12 +859,12 @@ merge_cb(x, ::Nothing) = x merge_cb(x, y) = CallbackSet(x, y) function process_events(sys; callback = nothing, kwargs...) - if has_continuous_events(sys) + if has_continuous_events(sys) && !isempty(continuous_events(sys)) contin_cb = generate_rootfinding_callback(sys; kwargs...) else contin_cb = nothing end - if has_discrete_events(sys) + if has_discrete_events(sys) && !isempty(discrete_events(sys)) discrete_cb = generate_discrete_callbacks(sys; kwargs...) else discrete_cb = nothing diff --git a/src/systems/jumps/jumpsystem.jl b/src/systems/jumps/jumpsystem.jl index 94cf2b67e8..e497d86141 100644 --- a/src/systems/jumps/jumpsystem.jl +++ b/src/systems/jumps/jumpsystem.jl @@ -485,7 +485,6 @@ function DiffEqBase.ODEProblem(sys::JumpSystem, u0map, tspan::Union{Tuple, Nothi use_union = false, eval_expression = false, eval_module = @__MODULE__, - check_length = false, kwargs...) if !iscomplete(sys) error("A completed `JumpSystem` is required. Call `complete` or `structural_simplify` on the system before creating a `DiscreteProblem`") @@ -496,11 +495,10 @@ function DiffEqBase.ODEProblem(sys::JumpSystem, u0map, tspan::Union{Tuple, Nothi osys = ODESystem(equations(sys).x[4], get_iv(sys), unknowns(sys), parameters(sys); observed = observed(sys), name = nameof(sys), description = description(sys), systems = get_systems(sys), defaults = defaults(sys), - continuous_events = continuous_events(sys), parameter_dependencies = parameter_dependencies(sys), metadata = get_metadata(sys), gui_metadata = get_gui_metadata(sys)) osys = complete(osys) - return ODEProblem(osys, u0map, tspan, parammap; check_length, kwargs...) + return ODEProblem(osys, u0map, tspan, parammap; check_length = false, kwargs...) else _, u0, p = process_SciMLProblem(EmptySciMLFunction, sys, u0map, parammap; t = tspan === nothing ? nothing : tspan[1], use_union, tofloat = false, @@ -508,7 +506,7 @@ function DiffEqBase.ODEProblem(sys::JumpSystem, u0map, tspan::Union{Tuple, Nothi f = (du, u, p, t) -> (du .= 0; nothing) observedfun = ObservedFunctionCache(sys; eval_expression, eval_module) df = ODEFunction(f; sys, observed = observedfun) - return ODEProblem(df, u0, tspan, p; check_length, kwargs...) + return ODEProblem(df, u0, tspan, p; kwargs...) end end From a77fcd4e184133ff766f4ba4fc8ccb4f72f8e569 Mon Sep 17 00:00:00 2001 From: Herman Sletmoen Date: Tue, 5 Nov 2024 21:47:21 +0100 Subject: [PATCH 0299/2176] Update perturbation example with Symbolics' new Taylor series functions --- docs/src/examples/perturbation.md | 195 ++++++++---------------------- 1 file changed, 50 insertions(+), 145 deletions(-) diff --git a/docs/src/examples/perturbation.md b/docs/src/examples/perturbation.md index 3ebdc3488a..b2ee4bef32 100644 --- a/docs/src/examples/perturbation.md +++ b/docs/src/examples/perturbation.md @@ -1,183 +1,88 @@ # [Symbolic-Numeric Perturbation Theory for ODEs](@id perturb_diff) -## Prelims +In the [Mixed Symbolic-Numeric Perturbation Theory tutorial](https://symbolics.juliasymbolics.org/stable/tutorials/perturbation/), we discussed how to solve algebraic equations using **Symbolics.jl**. Here we extend the method to differential equations. The procedure is similar, but the Taylor series coefficients now become functions of an independent variable (usually time). -In the previous tutorial, [Mixed Symbolic-Numeric Perturbation Theory](https://symbolics.juliasymbolics.org/stable/examples/perturbation/), we discussed how to solve algebraic equations using **Symbolics.jl**. Here, our goal is to extend the method to differential equations. First, we import the following helper functions that were introduced in [Mixed Symbolic/Numerical Methods for Perturbation Theory - Algebraic Equations](https://symbolics.juliasymbolics.org/stable/examples/perturbation/): - -```@example perturbation -using Symbolics - -def_taylor(x, ps) = sum([a * x^(i - 1) for (i, a) in enumerate(ps)]) - -function collect_powers(eq, x, ns; max_power = 100) - eq = substitute(expand(eq), Dict(x^j => 0 for j in (last(ns) + 1):max_power)) - - eqs = [] - for i in ns - powers = Dict(x^j => (i == j ? 1 : 0) for j in 1:last(ns)) - e = substitute(eq, powers) - - # manually remove zeroth order from higher orders - if 0 in ns && i != 0 - e = e - eqs[1] - end - - push!(eqs, e) - end - eqs -end - -function solve_coef(eqs, ps) - vals = Dict() - - for i in 1:length(ps) - eq = substitute(eqs[i], vals) - vals[ps[i]] = Symbolics.symbolic_linear_solve(eq ~ 0, ps[i]) - end - vals -end -``` - -## The Trajectory of a Ball! - -In the first two examples, we applied the perturbation method to algebraic problems. However, the main power of the perturbation method is to solve differential equations (usually ODEs, but also occasionally PDEs). Surprisingly, the main procedure developed to solve algebraic problems works well for differential equations. In fact, we will use the same two helper functions, `collect_powers` and `solve_coef`. The main difference is in the way we expand the dependent variables. For algebraic problems, the coefficients of $\epsilon$ are constants; whereas, for differential equations, they are functions of the dependent variable (usually time). - -As the first ODE example, we have chosen a simple and well-behaved problem, which is a variation of a standard first-year physics problem: what is the trajectory of an object (say, a ball, or a rocket) thrown vertically at velocity $v$ from the surface of a planet? Assuming a constant acceleration of gravity, $g$, every burgeoning physicist knows the answer: $x(t) = x(0) + vt - \frac{1}{2}gt^2$. However, what happens if $g$ is not constant? Specifically, $g$ is inversely proportional to the distant from the center of the planet. If $v$ is large and the projectile travels a large fraction of the radius of the planet, the assumption of constant gravity does not hold anymore. However, unless $v$ is large compared to the escape velocity, the correction is usually small. After simplifications and change of variables to dimensionless, the problem becomes +## Free fall in a varying gravitational field +Our first ODE example is a well-known physics problem: what is the altitude $x(t)$ of an object (say, a ball or a rocket) thrown vertically with initial velocity $ẋ(0)$ from the surface of a planet with mass $M$ and radius $R$? According to Newton's second law and law of gravity, it is the solution of the ODE ```math - \ddot{x}(t) = -\frac{1}{(1 + \epsilon x(t))^2} +ẍ = -\frac{GM}{(R+x)^2} = -\frac{GM}{R^2} \frac{1}{\left(1+ϵ\frac{x}{R}\right)^2}. ``` +In the last equality, we introduced a perturbative expansion parameter $ϵ$. When $ϵ=1$, we recover the original problem. When $ϵ=0$, the problem reduces to the trivial problem $ẍ = -g$ with constant gravitational acceleration $g = GM/R^2$ and solution $x(t) = x(0) + ẋ(0) t - \frac{1}{2} g t^2$. This is a good setup for perturbation theory. -with the initial conditions $x(0) = 0$, and $\dot{x}(0) = 1$. Note that for $\epsilon = 0$, this equation transforms back to the standard one. Let's start with defining the variables - +To make the problem dimensionless, we redefine $x \leftarrow x / R$ and $t \leftarrow t / \sqrt{R^3/GM}$. Then the ODE becomes ```@example perturbation +using ModelingToolkit using ModelingToolkit: t_nounits as t, D_nounits as D -order = 2 -n = order + 1 -@variables ϵ (y(t))[1:n] (∂∂y(t))[1:n] -``` - -Next, we define $x$. - -```@example perturbation -x = def_taylor(ϵ, y) -``` - -We need the second derivative of `x`. It may seem that we can do this using `Differential(t)`; however, this operation needs to wait for a few steps because we need to manipulate the differentials as separate variables. Instead, we define dummy variables `∂∂y` as the placeholder for the second derivatives and define - -```@example perturbation -∂∂x = def_taylor(ϵ, ∂∂y) -``` - -as the second derivative of `x`. After rearrangement, our governing equation is $\ddot{x}(t)(1 + \epsilon x(t))^{-2} + 1 = 0$, or - -```@example perturbation -eq = ∂∂x * (1 + ϵ * x)^2 + 1 -``` - -The next two steps are the same as the ones for algebraic equations (note that we pass `1:n` to `collect_powers` because the zeroth order term is needed here) - -```@example perturbation -eqs = collect_powers(eq, ϵ, 0:order) -``` - -and, - -```@example perturbation -vals = solve_coef(eqs, ∂∂y) +@variables ϵ x(t) +eq = D(D(x)) ~ -(1 + ϵ*x)^(-2) ``` -Our system of ODEs is forming. Now is the time to convert `∂∂`s to the correct **Symbolics.jl** form by substitution: - +Next, expand $x(t)$ in a series up to second order in $ϵ$: ```@example perturbation -subs = Dict(∂∂y[i] => D(D(y[i])) for i in eachindex(y)) -eqs = [substitute(first(v), subs) ~ substitute(last(v), subs) for v in vals] +using Symbolics +@variables y(t)[0:2] # coefficients +x_series = series(y, ϵ) ``` -We are nearly there! From this point on, the rest is standard ODE solving procedures. Potentially, we can use a symbolic ODE solver to find a closed form solution to this problem. However, **Symbolics.jl** currently does not support this functionality. Instead, we solve the problem numerically. We form an `ODESystem`, lower the order (convert second derivatives to first), generate an `ODEProblem` (after passing the correct initial conditions), and, finally, solve it. - +Insert this into the equation and collect perturbed equations to each order: ```@example perturbation -using ModelingToolkit, DifferentialEquations - -@mtkbuild sys = ODESystem(eqs, t) -unknowns(sys) +eq_pert = substitute(eq, x => x_series) +eqs_pert = taylor_coeff(eq_pert, ϵ, 0:2) ``` - +!!! note + The 0-th order equation can be solved analytically, but ModelingToolkit does currently not feature automatic analytical solution of ODEs, so we proceed with solving it numerically. +These are the ODEs we want to solve. Now construct an `ODESystem`, which automatically inserts dummy derivatives for the velocities: ```@example perturbation -# the initial conditions -# everything is zero except the initial velocity -u0 = Dict([unknowns(sys) .=> 0; D(y[1]) => 1]) - -prob = ODEProblem(sys, u0, (0, 3.0)) -sol = solve(prob; dtmax = 0.01); +@mtkbuild sys = ODESystem(eqs_pert, t) ``` -Finally, we calculate the solution to the problem as a function of `ϵ` by substituting the solution to the ODE system back into the defining equation for `x`. Note that `𝜀` is a number, compared to `ϵ`, which is a symbolic variable. - +To solve the `ODESystem`, we generate an `ODEProblem` with initial conditions $x(0) = 0$, and $ẋ(0) = 1$, and solve it: ```@example perturbation -X = 𝜀 -> sum([𝜀^(i - 1) * sol[yi] for (i, yi) in enumerate(y)]) +using DifferentialEquations +u0 = Dict([unknowns(sys) .=> 0.0; D(y[0]) => 1.0]) # nonzero initial velocity +prob = ODEProblem(sys, u0, (0.0, 3.0)) +sol = solve(prob) ``` - -Using `X`, we can plot the trajectory for a range of $𝜀$s. - +This is the solution for the coefficients in the series for $x(t)$ and their derivatives. Finally, we calculate the solution to the original problem by summing the series for different $ϵ$: ```@example perturbation using Plots - -plot(sol.t, hcat([X(𝜀) for 𝜀 in 0.0:0.1:0.5]...)) -``` - -As expected, as `𝜀` becomes larger (meaning the gravity is less with altitude), the object goes higher and stays up for a longer duration. Of course, we could have solved the problem directly using as ODE solver. One of the benefits of the perturbation method is that we need to run the ODE solver only once and then can just calculate the answer for different values of `𝜀`; whereas, if we had used the direct method, we would need to run the solver once for each value of `𝜀`. - -## A Weakly Nonlinear Oscillator - -For the next example, we have chosen a simple example from a very important class of problems, the nonlinear oscillators. As we will see, perturbation theory has difficulty providing a good solution to this problem, but the process is instructive. This example closely follows the chapter 7.6 of *Nonlinear Dynamics and Chaos* by Steven Strogatz. - -The goal is to solve $\ddot{x} + 2\epsilon\dot{x} + x = 0$, where the dot signifies time-derivatives and the initial conditions are $x(0) = 0$ and $\dot{x}(0) = 1$. If $\epsilon = 0$, the problem reduces to the simple linear harmonic oscillator with the exact solution $x(t) = \sin(t)$. We follow the same steps as the previous example. - -```@example perturbation -order = 2 -n = order + 1 -@variables ϵ (y(t))[1:n] (∂y)[1:n] (∂∂y)[1:n] -x = def_taylor(ϵ, y) -∂x = def_taylor(ϵ, ∂y) -∂∂x = def_taylor(ϵ, ∂∂y) +p = plot() +for ϵᵢ in 0.0:0.1:1.0 + plot!(p, sol, idxs = substitute(x_series, ϵ => ϵᵢ), label = "ϵ = $ϵᵢ") +end +p ``` +This makes sense: for larger $ϵ$, gravity weakens with altitude, and the trajectory goes higher for a fixed initial velocity. -This time we also need the first derivative terms. Continuing, +An advantage of the perturbative method is that we run the ODE solver only once and calculate trajectories for several $ϵ$ for free. Had we solved the full unperturbed ODE directly, we would need to do repeat it for every $ϵ$. -```@example perturbation -eq = ∂∂x + 2 * ϵ * ∂x + x -eqs = collect_powers(eq, ϵ, 0:n) -vals = solve_coef(eqs, ∂∂y) -``` +## Weakly nonlinear oscillator -Next, we need to replace `∂`s and `∂∂`s with their **Symbolics.jl** counterparts: +Our second example applies perturbation theory to nonlinear oscillators -- a very important class of problems. As we will see, perturbation theory has difficulty providing a good solution to this problem, but the process is nevertheless instructive. This example closely follows chapter 7.6 of *Nonlinear Dynamics and Chaos* by Steven Strogatz. +The goal is to solve the ODE ```@example perturbation -subs1 = Dict(∂y[i] => D(y[i]) for i in eachindex(y)) -subs2 = Dict(∂∂y[i] => D(D(y[i])) for i in eachindex(y)) -subs = subs1 ∪ subs2 -eqs = [substitute(first(v), subs) ~ substitute(last(v), subs) for v in vals] +eq = D(D(x)) + 2*ϵ*D(x) + x ~ 0 ``` +with initial conditions $x(0) = 0$ and $ẋ(0) = 1$. With $ϵ = 0$, the problem reduces to the simple linear harmonic oscillator with the exact solution $x(t) = \sin(t)$. -We continue with converting 'eqs' to an `ODEProblem`, solving it, and finally plot the results against the exact solution to the original problem, which is $x(t, \epsilon) = (1 - \epsilon)^{-1/2} e^{-\epsilon t} \sin((1- \epsilon^2)^{1/2}t)$, - +We follow the same steps as in the previous example to construct the `ODESystem`: ```@example perturbation -@mtkbuild sys = ODESystem(eqs, t) +eq_pert = substitute(eq, x => x_series) +eqs_pert = taylor_coeff(eq_pert, ϵ, 0:2) +@mtkbuild sys = ODESystem(eqs_pert, t) ``` +We solve and plot it as in the previous example, and compare the solution with $ϵ=0.1$ to the exact solution $x(t, ϵ) = e^{-ϵ t} \sin(\sqrt{(1-ϵ^2)}\,t) / \sqrt{1-ϵ^2}$ of the unperturbed equation: ```@example perturbation -# the initial conditions -u0 = Dict([unknowns(sys) .=> 0; D(y[1]) => 1]) - -prob = ODEProblem(sys, u0, (0, 50.0)) -sol = solve(prob; dtmax = 0.01) - -X = 𝜀 -> sum([𝜀^(i - 1) * sol[yi] for (i, yi) in enumerate(y)]) -T = sol.t -Y = 𝜀 -> exp.(-𝜀 * T) .* sin.(sqrt(1 - 𝜀^2) * T) / sqrt(1 - 𝜀^2) # exact solution +u0 = Dict([unknowns(sys) .=> 0.0; D(y[0]) => 1.0]) # nonzero initial velocity +prob = ODEProblem(sys, u0, (0.0, 50.0)) +sol = solve(prob) +plot(sol, idxs = substitute(x_series, ϵ => 0.1); label = "Perturbative (ϵ=0.1)") -plot(sol.t, [Y(0.1), X(0.1)]) +x_exact(t, ϵ) = exp(-ϵ * t) * sin(√(1 - ϵ^2) * t) / √(1 - ϵ^2) +plot!(sol.t, x_exact.(sol.t, 0.1); label = "Exact (ϵ=0.1)") ``` -The figure is similar to Figure 7.6.2 in *Nonlinear Dynamics and Chaos*. The two curves fit well for the first couple of cycles, but then the perturbation method curve diverges from the true solution. The main reason is that the problem has two or more time-scales that introduce secular terms in the solution. One solution is to explicitly account for the two time scales and use an analytic method called *two-timing*. +This is similar to Figure 7.6.2 in *Nonlinear Dynamics and Chaos*. The two curves fit well for the first couple of cycles, but then the perturbative solution diverges from the exact solution. The main reason is that the problem has two or more time-scales that introduce secular terms in the solution. One solution is to explicitly account for the two time scales and use an analytic method called *two-timing*, but this is outside the scope of this example. From feac63a6c123c1658b35fd04c76ac02cbffad0dc Mon Sep 17 00:00:00 2001 From: Sam Isaacson Date: Tue, 5 Nov 2024 17:27:11 -0500 Subject: [PATCH 0300/2176] update get_continuous_events to only apply to systems that have them --- src/systems/callbacks.jl | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/systems/callbacks.jl b/src/systems/callbacks.jl index 9ca69bf151..9dd78af65e 100644 --- a/src/systems/callbacks.jl +++ b/src/systems/callbacks.jl @@ -1,7 +1,9 @@ #################################### system operations ##################################### -get_continuous_events(sys::AbstractSystem) = SymbolicContinuousCallback[] -get_continuous_events(sys::AbstractTimeDependentSystem) = getfield(sys, :continuous_events) has_continuous_events(sys::AbstractSystem) = isdefined(sys, :continuous_events) +function get_continuous_events(sys::AbstractSystem) + has_continuous_events(sys) || return SymbolicContinuousCallback[] + getfield(sys, :discrete_events) +end has_discrete_events(sys::AbstractSystem) = isdefined(sys, :discrete_events) function get_discrete_events(sys::AbstractSystem) From e4ed1367908e3a40b8a344c4612b1517b1f948ef Mon Sep 17 00:00:00 2001 From: Sam Isaacson Date: Tue, 5 Nov 2024 17:27:36 -0500 Subject: [PATCH 0301/2176] typo --- src/systems/callbacks.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/systems/callbacks.jl b/src/systems/callbacks.jl index 9dd78af65e..0b6b69e272 100644 --- a/src/systems/callbacks.jl +++ b/src/systems/callbacks.jl @@ -2,7 +2,7 @@ has_continuous_events(sys::AbstractSystem) = isdefined(sys, :continuous_events) function get_continuous_events(sys::AbstractSystem) has_continuous_events(sys) || return SymbolicContinuousCallback[] - getfield(sys, :discrete_events) + getfield(sys, :continuous_events) end has_discrete_events(sys::AbstractSystem) = isdefined(sys, :discrete_events) From fec5b00ce854e58f014997caf7bcf336c82708f8 Mon Sep 17 00:00:00 2001 From: Fredrik Bagge Carlson Date: Thu, 7 Nov 2024 05:18:10 +0100 Subject: [PATCH 0302/2176] add name of sys in show method --- src/systems/abstractsystem.jl | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index c53c900d27..5c0348c060 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -1908,7 +1908,8 @@ function Base.show( # Print name and description desc = description(sys) - printstyled(io, "Model ", nameof(sys), ":"; bold) + name = nameof(sys) + printstyled(io, "Model ", name, ":"; bold) !isempty(desc) && print(io, " ", desc) # Print subsystems @@ -1916,7 +1917,7 @@ function Base.show( nsubs = length(subs) nrows = min(nsubs, limit ? rows : nsubs) nrows > 0 && printstyled(io, "\nSubsystems ($(nsubs)):"; bold) - nrows > 0 && hint && print(io, " see hierarchy(sys)") + nrows > 0 && hint && print(io, " see hierarchy($name)") for i in 1:nrows sub = subs[i] name = String(nameof(sub)) @@ -1940,9 +1941,9 @@ function Base.show( next = n_expanded_connection_equations(sys) ntot = neqs + next ntot > 0 && printstyled(io, "\nEquations ($ntot):"; bold) - neqs > 0 && print(io, "\n $neqs standard", hint ? ": see equations(sys)" : "") + neqs > 0 && print(io, "\n $neqs standard", hint ? ": see equations($name)" : "") next > 0 && print(io, "\n $next connecting", - hint ? ": see equations(expand_connections(sys))" : "") + hint ? ": see equations(expand_connections($name))" : "") #Base.print_matrix(io, eqs) # usually too long and not useful to print all equations end @@ -1953,7 +1954,7 @@ function Base.show( nvars == 0 && continue # skip header = titlecase(String(nameof(varfunc))) # e.g. "Unknowns" printstyled(io, "\n$header ($nvars):"; bold) - hint && print(io, " see $(nameof(varfunc))(sys)") + hint && print(io, " see $(nameof(varfunc))($name)") nrows = min(nvars, limit ? rows : nvars) defs = has_defaults(sys) ? defaults(sys) : nothing for i in 1:nrows @@ -1982,7 +1983,7 @@ function Base.show( # Print observed nobs = has_observed(sys) ? length(observed(sys)) : 0 nobs > 0 && printstyled(io, "\nObserved ($nobs):"; bold) - nobs > 0 && hint && print(io, " see observed(sys)") + nobs > 0 && hint && print(io, " see observed($name)") return nothing end From a1b249f6fd9864c51364b4ebe571a04faec87fb8 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 6 Nov 2024 16:56:48 +0530 Subject: [PATCH 0303/2176] feat: add circular dependency checking, substitution limit --- src/systems/problem_utils.jl | 127 +++++++++++++++++++++++++++++++++-- test/initial_values.jl | 25 +++++++ 2 files changed, 146 insertions(+), 6 deletions(-) diff --git a/src/systems/problem_utils.jl b/src/systems/problem_utils.jl index 977f637d47..142d2f6d71 100644 --- a/src/systems/problem_utils.jl +++ b/src/systems/problem_utils.jl @@ -250,6 +250,25 @@ function add_parameter_dependencies!(sys::AbstractSystem, varmap::AbstractDict) add_observed_equations!(varmap, parameter_dependencies(sys)) end +struct UnexpectedSymbolicValueInVarmap <: Exception + sym::Any + val::Any +end + +function Base.showerror(io::IO, err::UnexpectedSymbolicValueInVarmap) + println(io, + """ + Found symbolic value $(err.val) for variable $(err.sym). You may be missing an \ + initial condition or have cyclic initial conditions. If this is intended, pass \ + `symbolic_u0 = true`. In case the initial conditions are not cyclic but \ + require more substitutions to resolve, increase `substitution_limit`. To report \ + cycles in initial conditions of unknowns/parameters, pass \ + `warn_cyclic_dependency = true`. If the cycles are still not reported, you \ + may need to pass a larger value for `circular_dependency_max_cycle_length` \ + or `circular_dependency_max_cycles`. + """) +end + """ $(TYPEDSIGNATURES) @@ -278,6 +297,12 @@ function better_varmap_to_vars(varmap::AbstractDict, vars::Vector; isempty(missing_vars) || throw(MissingVariablesError(missing_vars)) end vals = map(x -> varmap[x], vars) + if !allow_symbolic + for (sym, val) in zip(vars, vals) + symbolic_type(val) == NotSymbolic() && continue + throw(UnexpectedSymbolicValueInVarmap(sym, val)) + end + end if container_type <: Union{AbstractDict, Tuple, Nothing, SciMLBase.NullParameters} container_type = Array @@ -298,6 +323,57 @@ function better_varmap_to_vars(varmap::AbstractDict, vars::Vector; end end +""" + $(TYPEDSIGNATURES) + +Check if any of the substitution rules in `varmap` lead to cycles involving +variables in `vars`. Return a vector of vectors containing all the variables +in each cycle. + +Keyword arguments: +- `max_cycle_length`: The maximum length (number of variables) of detected cycles. +- `max_cycles`: The maximum number of cycles to report. +""" +function check_substitution_cycles( + varmap::AbstractDict, vars; max_cycle_length = length(varmap), max_cycles = 10) + # ordered set so that `vars` are the first `k` in the list + allvars = OrderedSet{Any}(vars) + union!(allvars, keys(varmap)) + allvars = collect(allvars) + var_to_idx = Dict(allvars .=> eachindex(allvars)) + graph = SimpleDiGraph(length(allvars)) + + buffer = Set() + for (k, v) in varmap + kidx = var_to_idx[k] + if symbolic_type(v) != NotSymbolic() + vars!(buffer, v) + for var in buffer + haskey(var_to_idx, var) || continue + add_edge!(graph, kidx, var_to_idx[var]) + end + elseif v isa AbstractArray + for val in v + vars!(buffer, val) + end + for var in buffer + haskey(var_to_idx, var) || continue + add_edge!(graph, kidx, var_to_idx[var]) + end + end + empty!(buffer) + end + + # detect at most 100 cycles involving at most `length(varmap)` vertices + cycles = Graphs.simplecycles_limited_length(graph, max_cycle_length, max_cycles) + # only count those which contain variables in `vars` + filter!(Base.Fix1(any, <=(length(vars))), cycles) + + map(cycles) do cycle + map(Base.Fix1(getindex, allvars), cycle) + end +end + """ $(TYPEDSIGNATURES) @@ -305,10 +381,10 @@ Performs symbolic substitution on the values in `varmap` for the keys in `vars`, `varmap` itself as the set of substitution rules. If an entry in `vars` is not a key in `varmap`, it is ignored. """ -function evaluate_varmap!(varmap::AbstractDict, vars) +function evaluate_varmap!(varmap::AbstractDict, vars; limit = 100) for k in vars haskey(varmap, k) || continue - varmap[k] = fixpoint_sub(varmap[k], varmap) + varmap[k] = fixpoint_sub(varmap[k], varmap; maxiters = limit) end end @@ -407,6 +483,14 @@ Keyword arguments: length of `u0` vector for consistency. If `false`, do not check with equations. This is forwarded to `check_eqs_u0` - `symbolic_u0` allows the returned `u0` to be an array of symbolics. +- `warn_cyclic_dependency`: Whether to emit a warning listing out cycles in initial + conditions provided for unknowns and parameters. +- `circular_dependency_max_cycle_length`: Maximum length of cycle to check for. + Only applicable if `warn_cyclic_dependency == true`. +- `circular_dependency_max_cycles`: Maximum number of cycles to check for. + Only applicable if `warn_cyclic_dependency == true`. +- `substitution_limit`: The number times to substitute initial conditions into each + other to attempt to arrive at a numeric value. All other keyword arguments are passed as-is to `constructor`. """ @@ -416,7 +500,11 @@ function process_SciMLProblem( warn_initialize_determined = true, initialization_eqs = [], eval_expression = false, eval_module = @__MODULE__, fully_determined = false, check_initialization_units = false, tofloat = true, use_union = false, - u0_constructor = identity, du0map = nothing, check_length = true, symbolic_u0 = false, kwargs...) + u0_constructor = identity, du0map = nothing, check_length = true, + symbolic_u0 = false, warn_cyclic_dependency = false, + circular_dependency_max_cycle_length = length(all_symbols(sys)), + circular_dependency_max_cycles = 10, + substitution_limit = 100, kwargs...) dvs = unknowns(sys) ps = parameters(sys) iv = has_iv(sys) ? get_iv(sys) : nothing @@ -466,7 +554,8 @@ function process_SciMLProblem( initializeprob = ModelingToolkit.InitializationProblem( sys, t, u0map, pmap; guesses, warn_initialize_determined, initialization_eqs, eval_expression, eval_module, fully_determined, - check_units = check_initialization_units) + warn_cyclic_dependency, check_units = check_initialization_units, + circular_dependency_max_cycle_length, circular_dependency_max_cycles) initializeprobmap = getu(initializeprob, unknowns(sys)) punknowns = [p @@ -503,7 +592,20 @@ function process_SciMLProblem( add_observed!(sys, op) add_parameter_dependencies!(sys, op) - evaluate_varmap!(op, dvs) + if warn_cyclic_dependency + cycles = check_substitution_cycles( + op, dvs; max_cycle_length = circular_dependency_max_cycle_length, + max_cycles = circular_dependency_max_cycles) + if !isempty(cycles) + buffer = IOBuffer() + for cycle in cycles + println(buffer, cycle) + end + msg = String(take!(buffer)) + @warn "Cycles in unknowns:\n$msg" + end + end + evaluate_varmap!(op, dvs; limit = substitution_limit) u0 = better_varmap_to_vars( op, dvs; tofloat = true, use_union = false, @@ -515,7 +617,20 @@ function process_SciMLProblem( check_eqs_u0(eqs, dvs, u0; check_length, kwargs...) - evaluate_varmap!(op, ps) + if warn_cyclic_dependency + cycles = check_substitution_cycles( + op, ps; max_cycle_length = circular_dependency_max_cycle_length, + max_cycles = circular_dependency_max_cycles) + if !isempty(cycles) + buffer = IOBuffer() + for cycle in cycles + println(buffer, cycle) + end + msg = String(take!(buffer)) + @warn "Cycles in parameters:\n$msg" + end + end + evaluate_varmap!(op, ps; limit = substitution_limit) if is_split(sys) p = MTKParameters(sys, op) else diff --git a/test/initial_values.jl b/test/initial_values.jl index fc386242a6..a2931e22b6 100644 --- a/test/initial_values.jl +++ b/test/initial_values.jl @@ -149,3 +149,28 @@ end pmap = [c1 => 5.0, c2 => 1.0, c3 => 1.2] oprob = ODEProblem(osys, u0map, (0.0, 10.0), pmap) end + +@testset "Cyclic dependency checking and substitution limits" begin + @variables x(t) y(t) + @mtkbuild sys = ODESystem( + [D(x) ~ x, D(y) ~ y], t; initialization_eqs = [x ~ 2y + 3, y ~ 2x], + guesses = [x => 2y, y => 2x]) + @test_warn ["Cycle", "unknowns", "x", "y"] try + ODEProblem(sys, [], (0.0, 1.0), warn_cyclic_dependency = true) + catch + end + @test_throws ModelingToolkit.UnexpectedSymbolicValueInVarmap ODEProblem( + sys, [x => 2y + 1, y => 2x], (0.0, 1.0); build_initializeprob = false) + + @parameters p q + @mtkbuild sys = ODESystem( + [D(x) ~ x * p, D(y) ~ y * q], t; guesses = [p => 1.0, q => 2.0]) + # "unknowns" because they are initialization unknowns + @test_warn ["Cycle", "unknowns", "p", "q"] try + ODEProblem(sys, [x => 1, y => 2], (0.0, 1.0), + [p => 2q, q => 3p]; warn_cyclic_dependency = true) + catch + end + @test_throws ModelingToolkit.UnexpectedSymbolicValueInVarmap ODEProblem( + sys, [x => 1, y => 2], (0.0, 1.0), [p => 2q, q => 3p]) +end From 5a6da3cdf29104aa7e81528d5fe6299c4f432def Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 6 Nov 2024 16:56:55 +0530 Subject: [PATCH 0304/2176] build: bump Symbolics compat `maxiters` keyword to `fixpoint_sub` is required --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 656fef6173..67401c601a 100644 --- a/Project.toml +++ b/Project.toml @@ -131,7 +131,7 @@ SpecialFunctions = "0.7, 0.8, 0.9, 0.10, 1.0, 2" StaticArrays = "0.10, 0.11, 0.12, 1.0" SymbolicIndexingInterface = "0.3.31" SymbolicUtils = "3.7" -Symbolics = "6.15.2" +Symbolics = "6.15.4" URIs = "1" UnPack = "0.1, 1.0" Unitful = "1.1" From 7ca38cb306269df20388ed9f47eb5ea24e10ff52 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 6 Nov 2024 16:57:22 +0530 Subject: [PATCH 0305/2176] refactor: format --- docs/src/examples/perturbation.md | 22 +++++++++++++++++++--- src/systems/abstractsystem.jl | 4 ++-- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/docs/src/examples/perturbation.md b/docs/src/examples/perturbation.md index b2ee4bef32..cd1e843f55 100644 --- a/docs/src/examples/perturbation.md +++ b/docs/src/examples/perturbation.md @@ -5,20 +5,24 @@ In the [Mixed Symbolic-Numeric Perturbation Theory tutorial](https://symbolics.j ## Free fall in a varying gravitational field Our first ODE example is a well-known physics problem: what is the altitude $x(t)$ of an object (say, a ball or a rocket) thrown vertically with initial velocity $ẋ(0)$ from the surface of a planet with mass $M$ and radius $R$? According to Newton's second law and law of gravity, it is the solution of the ODE + ```math ẍ = -\frac{GM}{(R+x)^2} = -\frac{GM}{R^2} \frac{1}{\left(1+ϵ\frac{x}{R}\right)^2}. ``` + In the last equality, we introduced a perturbative expansion parameter $ϵ$. When $ϵ=1$, we recover the original problem. When $ϵ=0$, the problem reduces to the trivial problem $ẍ = -g$ with constant gravitational acceleration $g = GM/R^2$ and solution $x(t) = x(0) + ẋ(0) t - \frac{1}{2} g t^2$. This is a good setup for perturbation theory. To make the problem dimensionless, we redefine $x \leftarrow x / R$ and $t \leftarrow t / \sqrt{R^3/GM}$. Then the ODE becomes + ```@example perturbation using ModelingToolkit using ModelingToolkit: t_nounits as t, D_nounits as D @variables ϵ x(t) -eq = D(D(x)) ~ -(1 + ϵ*x)^(-2) +eq = D(D(x)) ~ -(1 + ϵ * x)^(-2) ``` Next, expand $x(t)$ in a series up to second order in $ϵ$: + ```@example perturbation using Symbolics @variables y(t)[0:2] # coefficients @@ -26,25 +30,32 @@ x_series = series(y, ϵ) ``` Insert this into the equation and collect perturbed equations to each order: + ```@example perturbation eq_pert = substitute(eq, x => x_series) eqs_pert = taylor_coeff(eq_pert, ϵ, 0:2) ``` + !!! note + The 0-th order equation can be solved analytically, but ModelingToolkit does currently not feature automatic analytical solution of ODEs, so we proceed with solving it numerically. -These are the ODEs we want to solve. Now construct an `ODESystem`, which automatically inserts dummy derivatives for the velocities: + These are the ODEs we want to solve. Now construct an `ODESystem`, which automatically inserts dummy derivatives for the velocities: + ```@example perturbation @mtkbuild sys = ODESystem(eqs_pert, t) ``` To solve the `ODESystem`, we generate an `ODEProblem` with initial conditions $x(0) = 0$, and $ẋ(0) = 1$, and solve it: + ```@example perturbation using DifferentialEquations u0 = Dict([unknowns(sys) .=> 0.0; D(y[0]) => 1.0]) # nonzero initial velocity prob = ODEProblem(sys, u0, (0.0, 3.0)) sol = solve(prob) ``` + This is the solution for the coefficients in the series for $x(t)$ and their derivatives. Finally, we calculate the solution to the original problem by summing the series for different $ϵ$: + ```@example perturbation using Plots p = plot() @@ -53,6 +64,7 @@ for ϵᵢ in 0.0:0.1:1.0 end p ``` + This makes sense: for larger $ϵ$, gravity weakens with altitude, and the trajectory goes higher for a fixed initial velocity. An advantage of the perturbative method is that we run the ODE solver only once and calculate trajectories for several $ϵ$ for free. Had we solved the full unperturbed ODE directly, we would need to do repeat it for every $ϵ$. @@ -62,12 +74,15 @@ An advantage of the perturbative method is that we run the ODE solver only once Our second example applies perturbation theory to nonlinear oscillators -- a very important class of problems. As we will see, perturbation theory has difficulty providing a good solution to this problem, but the process is nevertheless instructive. This example closely follows chapter 7.6 of *Nonlinear Dynamics and Chaos* by Steven Strogatz. The goal is to solve the ODE + ```@example perturbation -eq = D(D(x)) + 2*ϵ*D(x) + x ~ 0 +eq = D(D(x)) + 2 * ϵ * D(x) + x ~ 0 ``` + with initial conditions $x(0) = 0$ and $ẋ(0) = 1$. With $ϵ = 0$, the problem reduces to the simple linear harmonic oscillator with the exact solution $x(t) = \sin(t)$. We follow the same steps as in the previous example to construct the `ODESystem`: + ```@example perturbation eq_pert = substitute(eq, x => x_series) eqs_pert = taylor_coeff(eq_pert, ϵ, 0:2) @@ -75,6 +90,7 @@ eqs_pert = taylor_coeff(eq_pert, ϵ, 0:2) ``` We solve and plot it as in the previous example, and compare the solution with $ϵ=0.1$ to the exact solution $x(t, ϵ) = e^{-ϵ t} \sin(\sqrt{(1-ϵ^2)}\,t) / \sqrt{1-ϵ^2}$ of the unperturbed equation: + ```@example perturbation u0 = Dict([unknowns(sys) .=> 0.0; D(y[0]) => 1.0]) # nonzero initial velocity prob = ODEProblem(sys, u0, (0.0, 50.0)) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index 10401d91c5..9dda345f14 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -3001,8 +3001,8 @@ By default, the resulting system inherits `sys`'s name and description. See also [`compose`](@ref). """ function extend(sys::AbstractSystem, basesys::AbstractSystem; - name::Symbol = nameof(sys), description = description(sys), - gui_metadata = get_gui_metadata(sys)) + name::Symbol = nameof(sys), description = description(sys), + gui_metadata = get_gui_metadata(sys)) T = SciMLBase.parameterless_type(basesys) ivs = independent_variables(basesys) if !(sys isa T) From a023f7e694f2cb52674fcafbf1fa7bd4483c6bc8 Mon Sep 17 00:00:00 2001 From: Benjamin Chung Date: Sun, 27 Oct 2024 14:05:55 -0700 Subject: [PATCH 0306/2176] Add support for initialize and finalize callbacks --- src/systems/callbacks.jl | 177 ++++++++++++++++++++++++++++++--------- 1 file changed, 138 insertions(+), 39 deletions(-) diff --git a/src/systems/callbacks.jl b/src/systems/callbacks.jl index c427ae02a1..99d001146f 100644 --- a/src/systems/callbacks.jl +++ b/src/systems/callbacks.jl @@ -114,6 +114,8 @@ initialization. """ struct SymbolicContinuousCallback eqs::Vector{Equation} + initialize::Union{Vector{Equation}, FunctionalAffect} + finalize::Union{Vector{Equation}, FunctionalAffect} affect::Union{Vector{Equation}, FunctionalAffect} affect_neg::Union{Vector{Equation}, FunctionalAffect, Nothing} rootfind::SciMLBase.RootfindOpt @@ -122,9 +124,12 @@ struct SymbolicContinuousCallback eqs::Vector{Equation}, affect = NULL_AFFECT, affect_neg = affect, + initialize = NULL_AFFECT, + finalize = NULL_AFFECT, rootfind = SciMLBase.LeftRootFind, reinitializealg = SciMLBase.CheckInit()) - new(eqs, make_affect(affect), make_affect(affect_neg), rootfind, reinitializealg) + new(eqs, initialize, finalize, make_affect(affect), + make_affect(affect_neg), rootfind, reinitializealg) end # Default affect to nothing end make_affect(affect) = affect @@ -133,17 +138,80 @@ make_affect(affect::NamedTuple) = FunctionalAffect(; affect...) function Base.:(==)(e1::SymbolicContinuousCallback, e2::SymbolicContinuousCallback) isequal(e1.eqs, e2.eqs) && isequal(e1.affect, e2.affect) && + isequal(e1.initialize, e2.initialize) && isequal(e1.finalize, e2.finalize) && isequal(e1.affect_neg, e2.affect_neg) && isequal(e1.rootfind, e2.rootfind) end Base.isempty(cb::SymbolicContinuousCallback) = isempty(cb.eqs) function Base.hash(cb::SymbolicContinuousCallback, s::UInt) + hash_affect(affect::AbstractVector, s) = foldr(hash, affect, init = s) + hash_affect(affect, s) = hash(affect, s) s = foldr(hash, cb.eqs, init = s) - s = cb.affect isa AbstractVector ? foldr(hash, cb.affect, init = s) : hash(cb.affect, s) - s = cb.affect_neg isa AbstractVector ? foldr(hash, cb.affect_neg, init = s) : - hash(cb.affect_neg, s) + s = hash_affect(cb.affect, s) + s = hash_affect(cb.affect_neg, s) + s = hash_affect(cb.initialize, s) + s = hash_affect(cb.finalize, s) + s = hash(cb.reinitializealg, s) hash(cb.rootfind, s) end +function Base.show(io::IO, cb::SymbolicContinuousCallback) + indent = get(io, :indent, 0) + iio = IOContext(io, :indent => indent + 1) + print(io, "SymbolicContinuousCallback(") + print(iio, "Equations:") + show(iio, equations(cb)) + print(iio, "; ") + if affects(cb) != NULL_AFFECT + print(iio, "Affect:") + show(iio, affects(cb)) + print(iio, ", ") + end + if affect_negs(cb) != NULL_AFFECT + print(iio, "Negative-edge affect:") + show(iio, affect_negs(cb)) + print(iio, ", ") + end + if initialize_affects(cb) != NULL_AFFECT + print(iio, "Initialization affect:") + show(iio, initialize_affects(cb)) + print(iio, ", ") + end + if finalize_affects(cb) != NULL_AFFECT + print(iio, "Finalization affect:") + show(iio, finalize_affects(cb)) + end + print(iio, ")") +end + +function Base.show(io::IO, mime::MIME"text/plain", cb::SymbolicContinuousCallback) + indent = get(io, :indent, 0) + iio = IOContext(io, :indent => indent + 1) + println(io, "SymbolicContinuousCallback:") + println(iio, "Equations:") + show(iio, mime, equations(cb)) + print(iio, "\n") + if affects(cb) != NULL_AFFECT + println(iio, "Affect:") + show(iio, mime, affects(cb)) + print(iio, "\n") + end + if affect_negs(cb) != NULL_AFFECT + println(iio, "Negative-edge affect:") + show(iio, mime, affect_negs(cb)) + print(iio, "\n") + end + if initialize_affects(cb) != NULL_AFFECT + println(iio, "Initialization affect:") + show(iio, mime, initialize_affects(cb)) + print(iio, "\n") + end + if finalize_affects(cb) != NULL_AFFECT + println(iio, "Finalization affect:") + show(iio, mime, finalize_affects(cb)) + print(iio, "\n") + end +end + to_equation_vector(eq::Equation) = [eq] to_equation_vector(eqs::Vector{Equation}) = eqs function to_equation_vector(eqs::Vector{Any}) @@ -156,15 +224,18 @@ function SymbolicContinuousCallback(args...) end # wrap eq in vector SymbolicContinuousCallback(p::Pair) = SymbolicContinuousCallback(p[1], p[2]) SymbolicContinuousCallback(cb::SymbolicContinuousCallback) = cb # passthrough -function SymbolicContinuousCallback(eqs::Equation, affect = NULL_AFFECT; - affect_neg = affect, rootfind = SciMLBase.LeftRootFind) +function SymbolicContinuousCallback(eqs::Equation, affect = NULL_AFFECT; + initialize=NULL_AFFECT, finalize=NULL_AFFECT, + affect_neg = affect, rootfind = SciMLBase.LeftRootFind) SymbolicContinuousCallback( - eqs = [eqs], affect = affect, affect_neg = affect_neg, rootfind = rootfind) + eqs = [eqs], affect = affect, affect_neg = affect_neg, + initialize=initialize, finalize=finalize, rootfind = rootfind) end function SymbolicContinuousCallback(eqs::Vector{Equation}, affect = NULL_AFFECT; - affect_neg = affect, rootfind = SciMLBase.LeftRootFind) + affect_neg = affect, initialize=NULL_AFFECT, finalize=NULL_AFFECT, + rootfind = SciMLBase.LeftRootFind) SymbolicContinuousCallback( - eqs = eqs, affect = affect, affect_neg = affect_neg, rootfind = rootfind) + eqs = eqs, affect = affect, affect_neg = affect_neg, initialize=initialize, finalize=finalize, rootfind = rootfind) end SymbolicContinuousCallbacks(cb::SymbolicContinuousCallback) = [cb] @@ -199,15 +270,28 @@ function reinitialization_algs(cbs::Vector{SymbolicContinuousCallback}) reinitialization_alg, vcat, cbs, init = SciMLBase.DAEInitializationAlgorithm[]) end +initialize_affects(cb::SymbolicContinuousCallback) = cb.initialize +function initialize_affects(cbs::Vector{SymbolicContinuousCallback}) + mapreduce(initialize_affects, vcat, cbs, init = Equation[]) +end + +finalize_affects(cb::SymbolicContinuousCallback) = cb.initialize +function finalize_affects(cbs::Vector{SymbolicContinuousCallback}) + mapreduce(finalize_affects, vcat, cbs, init = Equation[]) +end + namespace_affects(af::Vector, s) = Equation[namespace_affect(a, s) for a in af] namespace_affects(af::FunctionalAffect, s) = namespace_affect(af, s) namespace_affects(::Nothing, s) = nothing function namespace_callback(cb::SymbolicContinuousCallback, s)::SymbolicContinuousCallback - SymbolicContinuousCallback( - namespace_equation.(equations(cb), (s,)), - namespace_affects(affects(cb), s); - affect_neg = namespace_affects(affect_negs(cb), s)) + SymbolicContinuousCallback(; + eqs = namespace_equation.(equations(cb), (s,)), + affect = namespace_affects(affects(cb), s), + affect_neg = namespace_affects(affect_negs(cb), s), + initialize = namespace_affects(initialize_affects(cb), s), + finalize = namespace_affects(finalize_affects(cb), s), + rootfind = cb.rootfind) end """ @@ -589,22 +673,25 @@ function generate_single_rootfinding_callback( rf_oop(u, parameter_values(integ), t) end end - + user_initfun = (affect_function.initialize == NULL_AFFECT) ? SciMLBase.INITIALIZE_DEFAULT : + (c, u, t, i) -> affect_function.initialize(i) if has_index_cache(sys) && (ic = get_index_cache(sys)) !== nothing && (save_idxs = get(ic.callback_to_clocks, cb, nothing)) !== nothing initfn = let save_idxs = save_idxs function (cb, u, t, integrator) + user_initfun(cb, u, t, integrator) for idx in save_idxs SciMLBase.save_discretes!(integrator, idx) end end end else - initfn = SciMLBase.INITIALIZE_DEFAULT + initfn = user_initfun end return ContinuousCallback( cond, affect_function.affect, affect_function.affect_neg, rootfind = cb.rootfind, initialize = initfn, + finalize = (affect_function.finalize == NULL_AFFECT) ? SciMLBase.FINALIZE_DEFAULT : (c, u, t, i) -> affect_function.finalize(i), initializealg = reinitialization_alg(cb)) end @@ -626,13 +713,14 @@ function generate_vector_rootfinding_callback( _, rf_ip = generate_custom_function( sys, rhss, dvs, ps; expression = Val{false}, kwargs...) - affect_functions = @NamedTuple{affect::Function, affect_neg::Union{Function, Nothing}}[compile_affect_fn( - cb, - sys, - dvs, - ps, - kwargs) - for cb in cbs] + affect_functions = @NamedTuple{ + affect::Function, + affect_neg::Union{Function, Nothing}, + initialize::Union{Function, Nothing}, + finalize::Union{Function, Nothing}}[ + compile_affect_fn(cb, sys, dvs, ps, kwargs) + for cb in cbs] + cond = function (out, u, t, integ) rf_ip(out, u, parameter_values(integ), t) end @@ -658,26 +746,31 @@ function generate_vector_rootfinding_callback( affect_neg(integ) end end - if has_index_cache(sys) && (ic = get_index_cache(sys)) !== nothing - save_idxs = mapreduce( - cb -> get(ic.callback_to_clocks, cb, Int[]), vcat, cbs; init = Int[]) - initfn = if isempty(save_idxs) - SciMLBase.INITIALIZE_DEFAULT + function handle_optional_setup_fn(funs, default) + if all(isnothing, funs) + return default else - let save_idxs = save_idxs - function (cb, u, t, integrator) - for idx in save_idxs - SciMLBase.save_discretes!(integrator, idx) + return let funs = funs + function (cb, u, t, integ) + for func in funs + if isnothing(func) + continue + else + func(integ) + end end end end end - else - initfn = SciMLBase.INITIALIZE_DEFAULT end + + initialize = handle_optional_setup_fn( + map(fn -> fn.initialize, affect_functions), SciMLBase.INITIALIZE_DEFAULT) + finalize = handle_optional_setup_fn( + map(fn -> fn.finalize, affect_functions), SciMLBase.FINALIZE_DEFAULT) return VectorContinuousCallback( cond, affect, affect_neg, length(eqs), rootfind = rootfind, - initialize = initfn, initializealg = reinitialization) + initialize = initialize, finalize = finalize, initializealg = reinitialization) end """ @@ -687,15 +780,21 @@ function compile_affect_fn(cb, sys::AbstractODESystem, dvs, ps, kwargs) eq_aff = affects(cb) eq_neg_aff = affect_negs(cb) affect = compile_affect(eq_aff, cb, sys, dvs, ps; expression = Val{false}, kwargs...) + function compile_optional_affect(aff, default = nothing) + if isnothing(aff) || aff == default + return nothing + else + return compile_affect(aff, cb, sys, dvs, ps; expression = Val{false}, kwargs...) + end + end if eq_neg_aff === eq_aff affect_neg = affect - elseif isnothing(eq_neg_aff) - affect_neg = nothing else - affect_neg = compile_affect( - eq_neg_aff, cb, sys, dvs, ps; expression = Val{false}, kwargs...) + affect_neg = compile_optional_affect(eq_neg_aff) end - (affect = affect, affect_neg = affect_neg) + initialize = compile_optional_affect(initialize_affects(cb), NULL_AFFECT) + finalize = compile_optional_affect(finalize_affects(cb), NULL_AFFECT) + (affect = affect, affect_neg = affect_neg, initialize = initialize, finalize = finalize) end function generate_rootfinding_callback(cbs, sys::AbstractODESystem, dvs = unknowns(sys), From b4893a3ef5b767eafc2d34409781241397de9509 Mon Sep 17 00:00:00 2001 From: Benjamin Chung Date: Mon, 28 Oct 2024 01:59:41 -0700 Subject: [PATCH 0307/2176] Fix initial condition --- src/systems/callbacks.jl | 36 +++++++++++++++++++++++++++++++----- 1 file changed, 31 insertions(+), 5 deletions(-) diff --git a/src/systems/callbacks.jl b/src/systems/callbacks.jl index 99d001146f..fe2dd8c14f 100644 --- a/src/systems/callbacks.jl +++ b/src/systems/callbacks.jl @@ -673,7 +673,7 @@ function generate_single_rootfinding_callback( rf_oop(u, parameter_values(integ), t) end end - user_initfun = (affect_function.initialize == NULL_AFFECT) ? SciMLBase.INITIALIZE_DEFAULT : + user_initfun = isnothing(affect_function.initialize) ? SciMLBase.INITIALIZE_DEFAULT : (c, u, t, i) -> affect_function.initialize(i) if has_index_cache(sys) && (ic = get_index_cache(sys)) !== nothing && (save_idxs = get(ic.callback_to_clocks, cb, nothing)) !== nothing @@ -691,7 +691,7 @@ function generate_single_rootfinding_callback( return ContinuousCallback( cond, affect_function.affect, affect_function.affect_neg, rootfind = cb.rootfind, initialize = initfn, - finalize = (affect_function.finalize == NULL_AFFECT) ? SciMLBase.FINALIZE_DEFAULT : (c, u, t, i) -> affect_function.finalize(i), + finalize = isnothing(affect_function.finalize) ? SciMLBase.FINALIZE_DEFAULT : (c, u, t, i) -> affect_function.finalize(i), initializealg = reinitialization_alg(cb)) end @@ -763,9 +763,35 @@ function generate_vector_rootfinding_callback( end end end - - initialize = handle_optional_setup_fn( - map(fn -> fn.initialize, affect_functions), SciMLBase.INITIALIZE_DEFAULT) + initialize = nothing + if has_index_cache(sys) && (ic = get_index_cache(sys)) !== nothing + initialize = handle_optional_setup_fn(map((cb, fn) -> begin + if (save_idxs = get(ic.callback_to_clocks, cb, nothing)) !== nothing + let save_idxs = save_idxs + if !isnothing(fn.initialize) + (i) -> begin + for idx in save_idxs + SciMLBase.save_discretes!(i, idx) + end + fn.initialize(i) + end + else + (i) -> begin + for idx in save_idxs + SciMLBase.save_discretes!(i, idx) + end + end + end + end + else + fn.initialize + end + end, cbs, affect_functions), SciMLBase.INITIALIZE_DEFAULT) + + else + initialize = handle_optional_setup_fn(map(fn -> fn.initialize, affect_functions), SciMLBase.INITIALIZE_DEFAULT) + end + finalize = handle_optional_setup_fn( map(fn -> fn.finalize, affect_functions), SciMLBase.FINALIZE_DEFAULT) return VectorContinuousCallback( From aa0ab70d75f33b97a13a42e740a59107add84e3e Mon Sep 17 00:00:00 2001 From: Benjamin Chung Date: Mon, 28 Oct 2024 02:15:10 -0700 Subject: [PATCH 0308/2176] Fix finalization --- src/systems/callbacks.jl | 2 +- test/symbolic_events.jl | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/systems/callbacks.jl b/src/systems/callbacks.jl index fe2dd8c14f..1729796a14 100644 --- a/src/systems/callbacks.jl +++ b/src/systems/callbacks.jl @@ -275,7 +275,7 @@ function initialize_affects(cbs::Vector{SymbolicContinuousCallback}) mapreduce(initialize_affects, vcat, cbs, init = Equation[]) end -finalize_affects(cb::SymbolicContinuousCallback) = cb.initialize +finalize_affects(cb::SymbolicContinuousCallback) = cb.finalize function finalize_affects(cbs::Vector{SymbolicContinuousCallback}) mapreduce(finalize_affects, vcat, cbs, init = Equation[]) end diff --git a/test/symbolic_events.jl b/test/symbolic_events.jl index 26593f980c..1e9321770e 100644 --- a/test/symbolic_events.jl +++ b/test/symbolic_events.jl @@ -970,6 +970,20 @@ end @test sol[c] == [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0] end + +@testset "Initialization" begin + @variables x(t) + seen = false + f = ModelingToolkit.FunctionalAffect(f=(i,u,p,c)->seen=true, sts=[], pars=[], discretes=[]) + cb1 = ModelingToolkit.SymbolicContinuousCallback([x ~ 0], Equation[], initialize=[x~1.5], finalize=f) + @mtkbuild sys = ODESystem(D(x) ~ -1, t, [x], []; continuous_events = [cb1]) + prob = ODEProblem(sys, [x => 1.0], (0.0, 2), []) + sol = solve(prob, Tsit5(); dtmax=0.01) + @test sol[x][1] ≈ 1.0 + @test sol[x][2] ≈ 1.5 # the initialize affect has been applied + @test seen == true +end + @testset "Bump" begin @variables x(t) [irreducible = true] y(t) [irreducible = true] eqs = [x ~ y, D(x) ~ -1] From 3e5cbde5ae297a69b554334b05ce3dd92114e6f4 Mon Sep 17 00:00:00 2001 From: Benjamin Chung Date: Mon, 28 Oct 2024 02:17:35 -0700 Subject: [PATCH 0309/2176] Add a note to the docstring about initialize and finalize --- src/systems/callbacks.jl | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/systems/callbacks.jl b/src/systems/callbacks.jl index 1729796a14..03869e9779 100644 --- a/src/systems/callbacks.jl +++ b/src/systems/callbacks.jl @@ -111,6 +111,9 @@ DAEs will be reinitialized using `reinitializealg` (which defaults to `SciMLBase This reinitialization algorithm ensures that the DAE is satisfied after the callback runs. The default value of `CheckInit` will simply validate that the newly-assigned values indeed satisfy the algebraic system; see the documentation on DAE initialization for a more detailed discussion of initialization. + +Initial and final affects can also be specified with SCC, which are specified identically to positive and negative edge affects. Initialization affects +will run as soon as the solver starts, while finalization affects will be executed after termination. """ struct SymbolicContinuousCallback eqs::Vector{Equation} From 9e638232e4fa7d24f412111656814af62b7c0d27 Mon Sep 17 00:00:00 2001 From: Benjamin Chung Date: Mon, 28 Oct 2024 02:20:36 -0700 Subject: [PATCH 0310/2176] Test VCC initialization/finalization --- test/symbolic_events.jl | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/test/symbolic_events.jl b/test/symbolic_events.jl index 1e9321770e..727e190c72 100644 --- a/test/symbolic_events.jl +++ b/test/symbolic_events.jl @@ -982,6 +982,25 @@ end @test sol[x][1] ≈ 1.0 @test sol[x][2] ≈ 1.5 # the initialize affect has been applied @test seen == true + + + @variables x(t) + seen = false + f = ModelingToolkit.FunctionalAffect(f=(i,u,p,c)->seen=true, sts=[], pars=[], discretes=[]) + cb1 = ModelingToolkit.SymbolicContinuousCallback([x ~ 0], Equation[], initialize=[x~1.5], finalize=f) + inited = false + finaled = false + a = ModelingToolkit.FunctionalAffect(f=(i,u,p,c)->inited=true, sts=[], pars=[], discretes=[]) + b = ModelingToolkit.FunctionalAffect(f=(i,u,p,c)->finaled=true, sts=[], pars=[], discretes=[]) + cb2= ModelingToolkit.SymbolicContinuousCallback([x ~ 0.1], Equation[], initialize=a, finalize=b) + @mtkbuild sys = ODESystem(D(x) ~ -1, t, [x], []; continuous_events = [cb1, cb2]) + prob = ODEProblem(sys, [x => 1.0], (0.0, 2), []) + sol = solve(prob, Tsit5()) + @test sol[x][1] ≈ 1.0 + @test sol[x][2] ≈ 1.5 # the initialize affect has been applied + @test seen == true + @test inited == true + @test finaled == true end @testset "Bump" begin From 6424ca9da6597a7fc8c1a6da1dee233535f6e602 Mon Sep 17 00:00:00 2001 From: Benjamin Chung Date: Mon, 28 Oct 2024 18:28:21 -0700 Subject: [PATCH 0311/2176] Initialize and finalize for discrete callbacks --- src/systems/callbacks.jl | 88 ++++++++++++++++++++++++++++------------ test/symbolic_events.jl | 48 ++++++++++++++++++++++ 2 files changed, 111 insertions(+), 25 deletions(-) diff --git a/src/systems/callbacks.jl b/src/systems/callbacks.jl index 03869e9779..18497a0d1e 100644 --- a/src/systems/callbacks.jl +++ b/src/systems/callbacks.jl @@ -328,13 +328,16 @@ struct SymbolicDiscreteCallback # TODO: Iterative condition::Any affects::Any + initialize::Any + finalize::Any reinitializealg::SciMLBase.DAEInitializationAlgorithm function SymbolicDiscreteCallback( - condition, affects = NULL_AFFECT, reinitializealg = SciMLBase.CheckInit()) + condition, affects = NULL_AFFECT; reinitializealg = SciMLBase.CheckInit(), + initialize=NULL_AFFECT, finalize=NULL_AFFECT) c = scalarize_condition(condition) a = scalarize_affects(affects) - new(c, a, reinitializealg) + new(c, a, scalarize_affects(initialize), scalarize_affects(finalize), reinitializealg) end # Default affect to nothing end @@ -373,11 +376,16 @@ function Base.show(io::IO, db::SymbolicDiscreteCallback) end function Base.:(==)(e1::SymbolicDiscreteCallback, e2::SymbolicDiscreteCallback) - isequal(e1.condition, e2.condition) && isequal(e1.affects, e2.affects) + isequal(e1.condition, e2.condition) && isequal(e1.affects, e2.affects) && + isequal(e1.initialize, e2.initialize) && isequal(e1.finalize, e2.finalize) end function Base.hash(cb::SymbolicDiscreteCallback, s::UInt) s = hash(cb.condition, s) - cb.affects isa AbstractVector ? foldr(hash, cb.affects, init = s) : hash(cb.affects, s) + s = cb.affects isa AbstractVector ? foldr(hash, cb.affects, init = s) : hash(cb.affects, s) + s = cb.initialize isa AbstractVector ? foldr(hash, cb.initialize, init = s) : hash(cb.initialize, s) + s = cb.finalize isa AbstractVector ? foldr(hash, cb.finalize, init = s) : hash(cb.finalize, s) + s = hash(cb.reinitializealg, s) + return s end condition(cb::SymbolicDiscreteCallback) = cb.condition @@ -397,10 +405,23 @@ function reinitialization_algs(cbs::Vector{SymbolicDiscreteCallback}) reinitialization_alg, vcat, cbs, init = SciMLBase.DAEInitializationAlgorithm[]) end + +initialize_affects(cb::SymbolicDiscreteCallback) = cb.initialize +function initialize_affects(cbs::Vector{SymbolicDiscreteCallback}) + mapreduce(initialize_affects, vcat, cbs, init = Equation[]) +end + +finalize_affects(cb::SymbolicDiscreteCallback) = cb.finalize +function finalize_affects(cbs::Vector{SymbolicDiscreteCallback}) + mapreduce(finalize_affects, vcat, cbs, init = Equation[]) +end + function namespace_callback(cb::SymbolicDiscreteCallback, s)::SymbolicDiscreteCallback - af = affects(cb) - af = af isa AbstractVector ? namespace_affect.(af, Ref(s)) : namespace_affect(af, s) - SymbolicDiscreteCallback(namespace_condition(condition(cb), s), af) + function namespace_affects(af) + return af isa AbstractVector ? namespace_affect.(af, Ref(s)) : namespace_affect(af, s) + end + SymbolicDiscreteCallback(namespace_condition(condition(cb), s), namespace_affects(affects(cb)), + reinitializealg=cb.reinitializealg, initialize=namespace_affects(initialize_affects(cb)), finalize=namespace_affects(finalize_affects(cb))) end SymbolicDiscreteCallbacks(cb::Pair) = SymbolicDiscreteCallback[SymbolicDiscreteCallback(cb)] @@ -773,10 +794,10 @@ function generate_vector_rootfinding_callback( let save_idxs = save_idxs if !isnothing(fn.initialize) (i) -> begin + fn.initialize(i) for idx in save_idxs SciMLBase.save_discretes!(i, idx) end - fn.initialize(i) end else (i) -> begin @@ -809,20 +830,13 @@ function compile_affect_fn(cb, sys::AbstractODESystem, dvs, ps, kwargs) eq_aff = affects(cb) eq_neg_aff = affect_negs(cb) affect = compile_affect(eq_aff, cb, sys, dvs, ps; expression = Val{false}, kwargs...) - function compile_optional_affect(aff, default = nothing) - if isnothing(aff) || aff == default - return nothing - else - return compile_affect(aff, cb, sys, dvs, ps; expression = Val{false}, kwargs...) - end - end if eq_neg_aff === eq_aff affect_neg = affect else - affect_neg = compile_optional_affect(eq_neg_aff) + affect_neg = _compile_optional_affect(NULL_AFFECT, eq_neg_aff, cb, sys, dvs, ps; kwargs...) end - initialize = compile_optional_affect(initialize_affects(cb), NULL_AFFECT) - finalize = compile_optional_affect(finalize_affects(cb), NULL_AFFECT) + initialize = _compile_optional_affect(NULL_AFFECT, initialize_affects(cb), cb, sys, dvs, ps; kwargs...) + finalize = _compile_optional_affect(NULL_AFFECT, finalize_affects(cb), cb, sys, dvs, ps; kwargs...) (affect = affect, affect_neg = affect_neg, initialize = initialize, finalize = finalize) end @@ -914,31 +928,48 @@ function compile_affect(affect::FunctionalAffect, cb, sys, dvs, ps; kwargs...) compile_user_affect(affect, cb, sys, dvs, ps; kwargs...) end + +function _compile_optional_affect(default, aff, cb, sys, dvs, ps; kwargs...) + if isnothing(aff) || aff == default + return nothing + else + return compile_affect(aff, cb, sys, dvs, ps; expression = Val{false}, kwargs...) + end +end function generate_timed_callback(cb, sys, dvs, ps; postprocess_affect_expr! = nothing, kwargs...) cond = condition(cb) as = compile_affect(affects(cb), cb, sys, dvs, ps; expression = Val{false}, postprocess_affect_expr!, kwargs...) + + user_initfun = _compile_optional_affect(NULL_AFFECT, initialize_affects(cb), cb, sys, dvs, ps; kwargs...) + user_finfun = _compile_optional_affect(NULL_AFFECT, finalize_affects(cb), cb, sys, dvs, ps; kwargs...) if has_index_cache(sys) && (ic = get_index_cache(sys)) !== nothing && (save_idxs = get(ic.callback_to_clocks, cb, nothing)) !== nothing - initfn = let save_idxs = save_idxs + initfn = let + save_idxs = save_idxs + initfun=user_initfun function (cb, u, t, integrator) + if !isnothing(initfun) + initfun(integrator) + end for idx in save_idxs SciMLBase.save_discretes!(integrator, idx) end end end else - initfn = SciMLBase.INITIALIZE_DEFAULT + initfn = isnothing(user_initfun) ? SciMLBase.INITIALIZE_DEFAULT : (_,_,_,i) -> user_initfun(i) end + finfun = isnothing(user_finfun) ? SciMLBase.FINALIZE_DEFAULT : (_,_,_,i) -> user_finfun(i) if cond isa AbstractVector # Preset Time return PresetTimeCallback( - cond, as; initialize = initfn, initializealg = reinitialization_alg(cb)) + cond, as; initialize = initfn, finalize=finfun, initializealg = reinitialization_alg(cb)) else # Periodic return PeriodicCallback( - as, cond; initialize = initfn, initializealg = reinitialization_alg(cb)) + as, cond; initialize = initfn, finalize=finfun, initializealg = reinitialization_alg(cb)) end end @@ -951,20 +982,27 @@ function generate_discrete_callback(cb, sys, dvs, ps; postprocess_affect_expr! = c = compile_condition(cb, sys, dvs, ps; expression = Val{false}, kwargs...) as = compile_affect(affects(cb), cb, sys, dvs, ps; expression = Val{false}, postprocess_affect_expr!, kwargs...) + + user_initfun = _compile_optional_affect(NULL_AFFECT, initialize_affects(cb), cb, sys, dvs, ps; kwargs...) + user_finfun = _compile_optional_affect(NULL_AFFECT, finalize_affects(cb), cb, sys, dvs, ps; kwargs...) if has_index_cache(sys) && (ic = get_index_cache(sys)) !== nothing && (save_idxs = get(ic.callback_to_clocks, cb, nothing)) !== nothing - initfn = let save_idxs = save_idxs + initfn = let save_idxs = save_idxs, initfun = user_initfun function (cb, u, t, integrator) + if !isnothing(initfun) + initfun(integrator) + end for idx in save_idxs SciMLBase.save_discretes!(integrator, idx) end end end else - initfn = SciMLBase.INITIALIZE_DEFAULT + initfn = isnothing(user_initfun) ? SciMLBase.INITIALIZE_DEFAULT : (_,_,_,i) -> user_initfun(i) end + finfun = isnothing(user_finfun) ? SciMLBase.FINALIZE_DEFAULT : (_,_,_,i) -> user_finfun(i) return DiscreteCallback( - c, as; initialize = initfn, initializealg = reinitialization_alg(cb)) + c, as; initialize = initfn, finalize = finfun, initializealg = reinitialization_alg(cb)) end end diff --git a/test/symbolic_events.jl b/test/symbolic_events.jl index 727e190c72..54180ad7c6 100644 --- a/test/symbolic_events.jl +++ b/test/symbolic_events.jl @@ -1001,6 +1001,54 @@ end @test seen == true @test inited == true @test finaled == true + + #periodic + inited = false + finaled = false + cb3 = ModelingToolkit.SymbolicDiscreteCallback(1.0, [x ~ 2], initialize=a, finalize=b) + @mtkbuild sys = ODESystem(D(x) ~ -1, t, [x], []; discrete_events = [cb3]) + prob = ODEProblem(sys, [x => 1.0], (0.0, 2), []) + sol = solve(prob, Tsit5()) + @test inited == true + @test finaled == true + @test isapprox(sol[x][3], 0.0, atol=1e-9) + @test sol[x][4] ≈ 2.0 + @test sol[x][5] ≈ 1.0 + + + seen = false + inited = false + finaled = false + cb3 = ModelingToolkit.SymbolicDiscreteCallback(1.0, f, initialize=a, finalize=b) + @mtkbuild sys = ODESystem(D(x) ~ -1, t, [x], []; discrete_events = [cb3]) + prob = ODEProblem(sys, [x => 1.0], (0.0, 2), []) + sol = solve(prob, Tsit5()) + @test seen == true + @test inited == true + + #preset + seen = false + inited = false + finaled = false + cb3 = ModelingToolkit.SymbolicDiscreteCallback([1.0], f, initialize=a, finalize=b) + @mtkbuild sys = ODESystem(D(x) ~ -1, t, [x], []; discrete_events = [cb3]) + prob = ODEProblem(sys, [x => 1.0], (0.0, 2), []) + sol = solve(prob, Tsit5()) + @test seen == true + @test inited == true + @test finaled == true + + #equational + seen = false + inited = false + finaled = false + cb3 = ModelingToolkit.SymbolicDiscreteCallback(t == 1.0, f, initialize=a, finalize=b) + @mtkbuild sys = ODESystem(D(x) ~ -1, t, [x], []; discrete_events = [cb3]) + prob = ODEProblem(sys, [x => 1.0], (0.0, 2), []) + sol = solve(prob, Tsit5(); tstops=1.0) + @test seen == true + @test inited == true + @test finaled == true end @testset "Bump" begin From e0e0e460a61b5ccc9c20841745b275338995f090 Mon Sep 17 00:00:00 2001 From: Benjamin Chung Date: Mon, 28 Oct 2024 18:29:20 -0700 Subject: [PATCH 0312/2176] Format --- src/systems/callbacks.jl | 149 +++++++++++++++++++++++---------------- test/symbolic_events.jl | 60 +++++++++------- 2 files changed, 121 insertions(+), 88 deletions(-) diff --git a/src/systems/callbacks.jl b/src/systems/callbacks.jl index 18497a0d1e..0b758a6e86 100644 --- a/src/systems/callbacks.jl +++ b/src/systems/callbacks.jl @@ -131,7 +131,7 @@ struct SymbolicContinuousCallback finalize = NULL_AFFECT, rootfind = SciMLBase.LeftRootFind, reinitializealg = SciMLBase.CheckInit()) - new(eqs, initialize, finalize, make_affect(affect), + new(eqs, initialize, finalize, make_affect(affect), make_affect(affect_neg), rootfind, reinitializealg) end # Default affect to nothing end @@ -227,18 +227,19 @@ function SymbolicContinuousCallback(args...) end # wrap eq in vector SymbolicContinuousCallback(p::Pair) = SymbolicContinuousCallback(p[1], p[2]) SymbolicContinuousCallback(cb::SymbolicContinuousCallback) = cb # passthrough -function SymbolicContinuousCallback(eqs::Equation, affect = NULL_AFFECT; - initialize=NULL_AFFECT, finalize=NULL_AFFECT, - affect_neg = affect, rootfind = SciMLBase.LeftRootFind) +function SymbolicContinuousCallback(eqs::Equation, affect = NULL_AFFECT; + initialize = NULL_AFFECT, finalize = NULL_AFFECT, + affect_neg = affect, rootfind = SciMLBase.LeftRootFind) SymbolicContinuousCallback( - eqs = [eqs], affect = affect, affect_neg = affect_neg, - initialize=initialize, finalize=finalize, rootfind = rootfind) + eqs = [eqs], affect = affect, affect_neg = affect_neg, + initialize = initialize, finalize = finalize, rootfind = rootfind) end function SymbolicContinuousCallback(eqs::Vector{Equation}, affect = NULL_AFFECT; - affect_neg = affect, initialize=NULL_AFFECT, finalize=NULL_AFFECT, + affect_neg = affect, initialize = NULL_AFFECT, finalize = NULL_AFFECT, rootfind = SciMLBase.LeftRootFind) SymbolicContinuousCallback( - eqs = eqs, affect = affect, affect_neg = affect_neg, initialize=initialize, finalize=finalize, rootfind = rootfind) + eqs = eqs, affect = affect, affect_neg = affect_neg, + initialize = initialize, finalize = finalize, rootfind = rootfind) end SymbolicContinuousCallbacks(cb::SymbolicContinuousCallback) = [cb] @@ -334,10 +335,11 @@ struct SymbolicDiscreteCallback function SymbolicDiscreteCallback( condition, affects = NULL_AFFECT; reinitializealg = SciMLBase.CheckInit(), - initialize=NULL_AFFECT, finalize=NULL_AFFECT) + initialize = NULL_AFFECT, finalize = NULL_AFFECT) c = scalarize_condition(condition) a = scalarize_affects(affects) - new(c, a, scalarize_affects(initialize), scalarize_affects(finalize), reinitializealg) + new(c, a, scalarize_affects(initialize), + scalarize_affects(finalize), reinitializealg) end # Default affect to nothing end @@ -376,14 +378,17 @@ function Base.show(io::IO, db::SymbolicDiscreteCallback) end function Base.:(==)(e1::SymbolicDiscreteCallback, e2::SymbolicDiscreteCallback) - isequal(e1.condition, e2.condition) && isequal(e1.affects, e2.affects) && - isequal(e1.initialize, e2.initialize) && isequal(e1.finalize, e2.finalize) + isequal(e1.condition, e2.condition) && isequal(e1.affects, e2.affects) && + isequal(e1.initialize, e2.initialize) && isequal(e1.finalize, e2.finalize) end function Base.hash(cb::SymbolicDiscreteCallback, s::UInt) s = hash(cb.condition, s) - s = cb.affects isa AbstractVector ? foldr(hash, cb.affects, init = s) : hash(cb.affects, s) - s = cb.initialize isa AbstractVector ? foldr(hash, cb.initialize, init = s) : hash(cb.initialize, s) - s = cb.finalize isa AbstractVector ? foldr(hash, cb.finalize, init = s) : hash(cb.finalize, s) + s = cb.affects isa AbstractVector ? foldr(hash, cb.affects, init = s) : + hash(cb.affects, s) + s = cb.initialize isa AbstractVector ? foldr(hash, cb.initialize, init = s) : + hash(cb.initialize, s) + s = cb.finalize isa AbstractVector ? foldr(hash, cb.finalize, init = s) : + hash(cb.finalize, s) s = hash(cb.reinitializealg, s) return s end @@ -405,7 +410,6 @@ function reinitialization_algs(cbs::Vector{SymbolicDiscreteCallback}) reinitialization_alg, vcat, cbs, init = SciMLBase.DAEInitializationAlgorithm[]) end - initialize_affects(cb::SymbolicDiscreteCallback) = cb.initialize function initialize_affects(cbs::Vector{SymbolicDiscreteCallback}) mapreduce(initialize_affects, vcat, cbs, init = Equation[]) @@ -418,10 +422,13 @@ end function namespace_callback(cb::SymbolicDiscreteCallback, s)::SymbolicDiscreteCallback function namespace_affects(af) - return af isa AbstractVector ? namespace_affect.(af, Ref(s)) : namespace_affect(af, s) + return af isa AbstractVector ? namespace_affect.(af, Ref(s)) : + namespace_affect(af, s) end - SymbolicDiscreteCallback(namespace_condition(condition(cb), s), namespace_affects(affects(cb)), - reinitializealg=cb.reinitializealg, initialize=namespace_affects(initialize_affects(cb)), finalize=namespace_affects(finalize_affects(cb))) + SymbolicDiscreteCallback( + namespace_condition(condition(cb), s), namespace_affects(affects(cb)), + reinitializealg = cb.reinitializealg, initialize = namespace_affects(initialize_affects(cb)), + finalize = namespace_affects(finalize_affects(cb))) end SymbolicDiscreteCallbacks(cb::Pair) = SymbolicDiscreteCallback[SymbolicDiscreteCallback(cb)] @@ -698,7 +705,7 @@ function generate_single_rootfinding_callback( end end user_initfun = isnothing(affect_function.initialize) ? SciMLBase.INITIALIZE_DEFAULT : - (c, u, t, i) -> affect_function.initialize(i) + (c, u, t, i) -> affect_function.initialize(i) if has_index_cache(sys) && (ic = get_index_cache(sys)) !== nothing && (save_idxs = get(ic.callback_to_clocks, cb, nothing)) !== nothing initfn = let save_idxs = save_idxs @@ -715,7 +722,8 @@ function generate_single_rootfinding_callback( return ContinuousCallback( cond, affect_function.affect, affect_function.affect_neg, rootfind = cb.rootfind, initialize = initfn, - finalize = isnothing(affect_function.finalize) ? SciMLBase.FINALIZE_DEFAULT : (c, u, t, i) -> affect_function.finalize(i), + finalize = isnothing(affect_function.finalize) ? SciMLBase.FINALIZE_DEFAULT : + (c, u, t, i) -> affect_function.finalize(i), initializealg = reinitialization_alg(cb)) end @@ -742,8 +750,8 @@ function generate_vector_rootfinding_callback( affect_neg::Union{Function, Nothing}, initialize::Union{Function, Nothing}, finalize::Union{Function, Nothing}}[ - compile_affect_fn(cb, sys, dvs, ps, kwargs) - for cb in cbs] + compile_affect_fn(cb, sys, dvs, ps, kwargs) + for cb in cbs] cond = function (out, u, t, integ) rf_ip(out, u, parameter_values(integ), t) @@ -789,31 +797,37 @@ function generate_vector_rootfinding_callback( end initialize = nothing if has_index_cache(sys) && (ic = get_index_cache(sys)) !== nothing - initialize = handle_optional_setup_fn(map((cb, fn) -> begin - if (save_idxs = get(ic.callback_to_clocks, cb, nothing)) !== nothing - let save_idxs = save_idxs - if !isnothing(fn.initialize) - (i) -> begin - fn.initialize(i) - for idx in save_idxs - SciMLBase.save_discretes!(i, idx) - end - end - else - (i) -> begin - for idx in save_idxs - SciMLBase.save_discretes!(i, idx) + initialize = handle_optional_setup_fn( + map( + (cb, fn) -> begin + if (save_idxs = get(ic.callback_to_clocks, cb, nothing)) !== nothing + let save_idxs = save_idxs + if !isnothing(fn.initialize) + (i) -> begin + fn.initialize(i) + for idx in save_idxs + SciMLBase.save_discretes!(i, idx) + end + end + else + (i) -> begin + for idx in save_idxs + SciMLBase.save_discretes!(i, idx) + end + end end end + else + fn.initialize end - end - else - fn.initialize - end - end, cbs, affect_functions), SciMLBase.INITIALIZE_DEFAULT) - + end, + cbs, + affect_functions), + SciMLBase.INITIALIZE_DEFAULT) + else - initialize = handle_optional_setup_fn(map(fn -> fn.initialize, affect_functions), SciMLBase.INITIALIZE_DEFAULT) + initialize = handle_optional_setup_fn( + map(fn -> fn.initialize, affect_functions), SciMLBase.INITIALIZE_DEFAULT) end finalize = handle_optional_setup_fn( @@ -833,10 +847,13 @@ function compile_affect_fn(cb, sys::AbstractODESystem, dvs, ps, kwargs) if eq_neg_aff === eq_aff affect_neg = affect else - affect_neg = _compile_optional_affect(NULL_AFFECT, eq_neg_aff, cb, sys, dvs, ps; kwargs...) + affect_neg = _compile_optional_affect( + NULL_AFFECT, eq_neg_aff, cb, sys, dvs, ps; kwargs...) end - initialize = _compile_optional_affect(NULL_AFFECT, initialize_affects(cb), cb, sys, dvs, ps; kwargs...) - finalize = _compile_optional_affect(NULL_AFFECT, finalize_affects(cb), cb, sys, dvs, ps; kwargs...) + initialize = _compile_optional_affect( + NULL_AFFECT, initialize_affects(cb), cb, sys, dvs, ps; kwargs...) + finalize = _compile_optional_affect( + NULL_AFFECT, finalize_affects(cb), cb, sys, dvs, ps; kwargs...) (affect = affect, affect_neg = affect_neg, initialize = initialize, finalize = finalize) end @@ -928,7 +945,6 @@ function compile_affect(affect::FunctionalAffect, cb, sys, dvs, ps; kwargs...) compile_user_affect(affect, cb, sys, dvs, ps; kwargs...) end - function _compile_optional_affect(default, aff, cb, sys, dvs, ps; kwargs...) if isnothing(aff) || aff == default return nothing @@ -942,13 +958,15 @@ function generate_timed_callback(cb, sys, dvs, ps; postprocess_affect_expr! = no as = compile_affect(affects(cb), cb, sys, dvs, ps; expression = Val{false}, postprocess_affect_expr!, kwargs...) - user_initfun = _compile_optional_affect(NULL_AFFECT, initialize_affects(cb), cb, sys, dvs, ps; kwargs...) - user_finfun = _compile_optional_affect(NULL_AFFECT, finalize_affects(cb), cb, sys, dvs, ps; kwargs...) + user_initfun = _compile_optional_affect( + NULL_AFFECT, initialize_affects(cb), cb, sys, dvs, ps; kwargs...) + user_finfun = _compile_optional_affect( + NULL_AFFECT, finalize_affects(cb), cb, sys, dvs, ps; kwargs...) if has_index_cache(sys) && (ic = get_index_cache(sys)) !== nothing && (save_idxs = get(ic.callback_to_clocks, cb, nothing)) !== nothing - initfn = let + initfn = let save_idxs = save_idxs - initfun=user_initfun + initfun = user_initfun function (cb, u, t, integrator) if !isnothing(initfun) initfun(integrator) @@ -959,17 +977,21 @@ function generate_timed_callback(cb, sys, dvs, ps; postprocess_affect_expr! = no end end else - initfn = isnothing(user_initfun) ? SciMLBase.INITIALIZE_DEFAULT : (_,_,_,i) -> user_initfun(i) + initfn = isnothing(user_initfun) ? SciMLBase.INITIALIZE_DEFAULT : + (_, _, _, i) -> user_initfun(i) end - finfun = isnothing(user_finfun) ? SciMLBase.FINALIZE_DEFAULT : (_,_,_,i) -> user_finfun(i) + finfun = isnothing(user_finfun) ? SciMLBase.FINALIZE_DEFAULT : + (_, _, _, i) -> user_finfun(i) if cond isa AbstractVector # Preset Time return PresetTimeCallback( - cond, as; initialize = initfn, finalize=finfun, initializealg = reinitialization_alg(cb)) + cond, as; initialize = initfn, finalize = finfun, + initializealg = reinitialization_alg(cb)) else # Periodic return PeriodicCallback( - as, cond; initialize = initfn, finalize=finfun, initializealg = reinitialization_alg(cb)) + as, cond; initialize = initfn, finalize = finfun, + initializealg = reinitialization_alg(cb)) end end @@ -983,8 +1005,10 @@ function generate_discrete_callback(cb, sys, dvs, ps; postprocess_affect_expr! = as = compile_affect(affects(cb), cb, sys, dvs, ps; expression = Val{false}, postprocess_affect_expr!, kwargs...) - user_initfun = _compile_optional_affect(NULL_AFFECT, initialize_affects(cb), cb, sys, dvs, ps; kwargs...) - user_finfun = _compile_optional_affect(NULL_AFFECT, finalize_affects(cb), cb, sys, dvs, ps; kwargs...) + user_initfun = _compile_optional_affect( + NULL_AFFECT, initialize_affects(cb), cb, sys, dvs, ps; kwargs...) + user_finfun = _compile_optional_affect( + NULL_AFFECT, finalize_affects(cb), cb, sys, dvs, ps; kwargs...) if has_index_cache(sys) && (ic = get_index_cache(sys)) !== nothing && (save_idxs = get(ic.callback_to_clocks, cb, nothing)) !== nothing initfn = let save_idxs = save_idxs, initfun = user_initfun @@ -998,11 +1022,14 @@ function generate_discrete_callback(cb, sys, dvs, ps; postprocess_affect_expr! = end end else - initfn = isnothing(user_initfun) ? SciMLBase.INITIALIZE_DEFAULT : (_,_,_,i) -> user_initfun(i) + initfn = isnothing(user_initfun) ? SciMLBase.INITIALIZE_DEFAULT : + (_, _, _, i) -> user_initfun(i) end - finfun = isnothing(user_finfun) ? SciMLBase.FINALIZE_DEFAULT : (_,_,_,i) -> user_finfun(i) + finfun = isnothing(user_finfun) ? SciMLBase.FINALIZE_DEFAULT : + (_, _, _, i) -> user_finfun(i) return DiscreteCallback( - c, as; initialize = initfn, finalize = finfun, initializealg = reinitialization_alg(cb)) + c, as; initialize = initfn, finalize = finfun, + initializealg = reinitialization_alg(cb)) end end diff --git a/test/symbolic_events.jl b/test/symbolic_events.jl index 54180ad7c6..b58d5911f4 100644 --- a/test/symbolic_events.jl +++ b/test/symbolic_events.jl @@ -970,84 +970,90 @@ end @test sol[c] == [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0] end - @testset "Initialization" begin @variables x(t) seen = false - f = ModelingToolkit.FunctionalAffect(f=(i,u,p,c)->seen=true, sts=[], pars=[], discretes=[]) - cb1 = ModelingToolkit.SymbolicContinuousCallback([x ~ 0], Equation[], initialize=[x~1.5], finalize=f) + f = ModelingToolkit.FunctionalAffect( + f = (i, u, p, c) -> seen = true, sts = [], pars = [], discretes = []) + cb1 = ModelingToolkit.SymbolicContinuousCallback( + [x ~ 0], Equation[], initialize = [x ~ 1.5], finalize = f) @mtkbuild sys = ODESystem(D(x) ~ -1, t, [x], []; continuous_events = [cb1]) prob = ODEProblem(sys, [x => 1.0], (0.0, 2), []) - sol = solve(prob, Tsit5(); dtmax=0.01) + sol = solve(prob, Tsit5(); dtmax = 0.01) @test sol[x][1] ≈ 1.0 @test sol[x][2] ≈ 1.5 # the initialize affect has been applied @test seen == true - @variables x(t) seen = false - f = ModelingToolkit.FunctionalAffect(f=(i,u,p,c)->seen=true, sts=[], pars=[], discretes=[]) - cb1 = ModelingToolkit.SymbolicContinuousCallback([x ~ 0], Equation[], initialize=[x~1.5], finalize=f) - inited = false + f = ModelingToolkit.FunctionalAffect( + f = (i, u, p, c) -> seen = true, sts = [], pars = [], discretes = []) + cb1 = ModelingToolkit.SymbolicContinuousCallback( + [x ~ 0], Equation[], initialize = [x ~ 1.5], finalize = f) + inited = false finaled = false - a = ModelingToolkit.FunctionalAffect(f=(i,u,p,c)->inited=true, sts=[], pars=[], discretes=[]) - b = ModelingToolkit.FunctionalAffect(f=(i,u,p,c)->finaled=true, sts=[], pars=[], discretes=[]) - cb2= ModelingToolkit.SymbolicContinuousCallback([x ~ 0.1], Equation[], initialize=a, finalize=b) + a = ModelingToolkit.FunctionalAffect( + f = (i, u, p, c) -> inited = true, sts = [], pars = [], discretes = []) + b = ModelingToolkit.FunctionalAffect( + f = (i, u, p, c) -> finaled = true, sts = [], pars = [], discretes = []) + cb2 = ModelingToolkit.SymbolicContinuousCallback( + [x ~ 0.1], Equation[], initialize = a, finalize = b) @mtkbuild sys = ODESystem(D(x) ~ -1, t, [x], []; continuous_events = [cb1, cb2]) prob = ODEProblem(sys, [x => 1.0], (0.0, 2), []) sol = solve(prob, Tsit5()) @test sol[x][1] ≈ 1.0 @test sol[x][2] ≈ 1.5 # the initialize affect has been applied @test seen == true - @test inited == true + @test inited == true @test finaled == true #periodic - inited = false + inited = false finaled = false - cb3 = ModelingToolkit.SymbolicDiscreteCallback(1.0, [x ~ 2], initialize=a, finalize=b) + cb3 = ModelingToolkit.SymbolicDiscreteCallback( + 1.0, [x ~ 2], initialize = a, finalize = b) @mtkbuild sys = ODESystem(D(x) ~ -1, t, [x], []; discrete_events = [cb3]) prob = ODEProblem(sys, [x => 1.0], (0.0, 2), []) sol = solve(prob, Tsit5()) - @test inited == true + @test inited == true @test finaled == true - @test isapprox(sol[x][3], 0.0, atol=1e-9) + @test isapprox(sol[x][3], 0.0, atol = 1e-9) @test sol[x][4] ≈ 2.0 @test sol[x][5] ≈ 1.0 - seen = false - inited = false + inited = false finaled = false - cb3 = ModelingToolkit.SymbolicDiscreteCallback(1.0, f, initialize=a, finalize=b) + cb3 = ModelingToolkit.SymbolicDiscreteCallback(1.0, f, initialize = a, finalize = b) @mtkbuild sys = ODESystem(D(x) ~ -1, t, [x], []; discrete_events = [cb3]) prob = ODEProblem(sys, [x => 1.0], (0.0, 2), []) sol = solve(prob, Tsit5()) @test seen == true - @test inited == true + @test inited == true #preset seen = false - inited = false + inited = false finaled = false - cb3 = ModelingToolkit.SymbolicDiscreteCallback([1.0], f, initialize=a, finalize=b) + cb3 = ModelingToolkit.SymbolicDiscreteCallback([1.0], f, initialize = a, finalize = b) @mtkbuild sys = ODESystem(D(x) ~ -1, t, [x], []; discrete_events = [cb3]) prob = ODEProblem(sys, [x => 1.0], (0.0, 2), []) sol = solve(prob, Tsit5()) @test seen == true - @test inited == true + @test inited == true @test finaled == true #equational seen = false - inited = false + inited = false finaled = false - cb3 = ModelingToolkit.SymbolicDiscreteCallback(t == 1.0, f, initialize=a, finalize=b) + cb3 = ModelingToolkit.SymbolicDiscreteCallback( + t == 1.0, f, initialize = a, finalize = b) @mtkbuild sys = ODESystem(D(x) ~ -1, t, [x], []; discrete_events = [cb3]) prob = ODEProblem(sys, [x => 1.0], (0.0, 2), []) - sol = solve(prob, Tsit5(); tstops=1.0) + sol = solve(prob, Tsit5(); tstops = 1.0) @test seen == true - @test inited == true + @test inited == true @test finaled == true end From 5a127174bf71bdcff2e075d11df34ce3d4779999 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 7 Nov 2024 15:40:21 +0530 Subject: [PATCH 0313/2176] build: bump version --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 67401c601a..f2c1209b93 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "ModelingToolkit" uuid = "961ee093-0014-501f-94e3-6117800e7a78" authors = ["Yingbo Ma ", "Chris Rackauckas and contributors"] -version = "9.49.0" +version = "9.50.0" [deps] AbstractTrees = "1520ce14-60c1-5f80-bbc7-55ef81b5835c" From 32aeac69c6d083602777ee6fd331f115d8e3d7de Mon Sep 17 00:00:00 2001 From: Sam Isaacson Date: Thu, 7 Nov 2024 07:00:01 -0500 Subject: [PATCH 0314/2176] update master --- test/jumpsystem.jl | 34 ++++++++++++++++++++++++---------- 1 file changed, 24 insertions(+), 10 deletions(-) diff --git a/test/jumpsystem.jl b/test/jumpsystem.jl index 76d11f6205..8a3c2adccf 100644 --- a/test/jumpsystem.jl +++ b/test/jumpsystem.jl @@ -301,7 +301,7 @@ end # basic VariableRateJump test let N = 1000 # number of simulations for testing solve accuracy - Random.seed!(rng, 1111) + Random.seed!(rng, 1111) @variables A(t) B(t) C(t) @parameters k vrj = VariableRateJump(k * (sin(t) + 1), [A ~ A + 1, C ~ C + 2]) @@ -427,20 +427,34 @@ end let @variables X(t) Y(t) @parameters k1 k2 - rate1 = k1 * X - affect1! = [X ~ X - 1] - rate2 = k1 - affect2! = [Y ~ Y + 1] + vrj1 = VariableRateJump(k1 * X, [X ~ X - 1]) + vrj2 = VariableRateJump(k2, [Y ~ Y + 1]) eqs = [D(X) ~ k2, D(Y) ~ -k2/100*Y] - vrj1 = VariableRateJump(rate1, affect1!) - vrj2 = VariableRateJump(rate2, affect2!) @named jsys = JumpSystem([vrj1, vrj2, eqs[1], eqs[2]], t, [X, Y], [k1, k2]) jsys = complete(jsys) - u0 = [X => 0.0, Y => 0.0] - p = [k1 => 1.0, k2 => 20.0] + X0 = 0.0; Y0 = 0.0 + u0 = [X => X0, Y => Y0] + k1val = 1.0; k2val = 20.0 + p = [k1 => k1val, k2 => k2val] tspan = (0.0, 20.0) oprob = ODEProblem(jsys, u0, tspan, p) jprob = JumpProblem(jsys, oprob; rng) - sol = solve(jprob, Tsit5()) + + times = range(0.0, 20.0, length = 100) + Nsims = 2000 + X = zeros(length(times)) + Y = similar(X) + for n in 1:Nsims + sol = solve(jprob, Tsit5()) + X .+= Array(sol(times; idxs = X)) + Y .+= Array(sol(times; idxs = Y)) + end + X ./= Nsims; Y ./= Nsims; + + Xact(t) = X0 * exp(-k1val * t) + (k2val / k1val) * (1 - exp(-k1val * t)) + Yact(t) = Y0 * exp(-k2val/100 * t) + (k1 / (k2val/100)) * (1 - exp(-k2val/100 * t)) + + @test all(abs.(X .- Xact.(times)) .<= 0.05 .* X) + @test all(abs.(Y .- Yact.(times)) .<= 0.05 .* Y) end \ No newline at end of file From 551cf0d9803f1a500f1d1933d13ffd3a9994a3fe Mon Sep 17 00:00:00 2001 From: Venkateshprasad <32921645+ven-k@users.noreply.github.com> Date: Thu, 7 Nov 2024 17:19:41 +0000 Subject: [PATCH 0315/2176] feat: support multiple extend statements in MTKModel --- docs/src/basics/MTKLanguage.md | 7 ++++--- src/systems/abstractsystem.jl | 13 +++++++++++-- src/systems/model_parsing.jl | 22 ++++++++++++++-------- test/model_parsing.jl | 21 +++++++++++++++++++++ 4 files changed, 50 insertions(+), 13 deletions(-) diff --git a/docs/src/basics/MTKLanguage.md b/docs/src/basics/MTKLanguage.md index 537e1b0349..06250f90b4 100644 --- a/docs/src/basics/MTKLanguage.md +++ b/docs/src/basics/MTKLanguage.md @@ -147,9 +147,9 @@ julia> ModelingToolkit.getdefault(model_c1.v) 2.0 ``` -#### `@extend` begin block +#### `@extend` statement -Partial systems can be extended in a higher system in two ways: +One or more partial systems can be extended in a higher system with `@extend` statements. This can be done in two ways: - `@extend PartialSystem(var1 = value1)` @@ -313,7 +313,8 @@ end - `:components`: The list of sub-components in the form of [[name, sub_component_name],...]. - `:constants`: Dictionary of constants mapped to its metadata. - `:defaults`: Dictionary of variables and default values specified in the `@defaults`. - - `:extend`: The list of extended unknowns, name given to the base system, and name of the base system. + - `:extend`: The list of extended unknowns, parameters and components, name given to the base system, and name of the base system. + When multiple extend statements are present, latter two are returned as lists. - `:structural_parameters`: Dictionary of structural parameters mapped to their metadata. - `:parameters`: Dictionary of symbolic parameters mapped to their metadata. For parameter arrays, length is added to the metadata as `:size`. diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index 21a3600749..e715dc6eea 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -918,7 +918,7 @@ Mark a system as completed. A completed system is a system which is done being defined/modified and is ready for structural analysis or other transformations. This allows for analyses and optimizations to be performed which require knowing the global structure of the system. - + One property to note is that if a system is complete, the system will no longer namespace its subsystems or variables, i.e. `isequal(complete(sys).v.i, v.i)`. """ @@ -1933,7 +1933,7 @@ function Base.show( end end limited = nrows < nsubs - limited && print(io, "\n ⋮") # too many to print + limited && print(io, "\n ⋮") # too many to print # Print equations eqs = equations(sys) @@ -3043,10 +3043,19 @@ function extend(sys::AbstractSystem, basesys::AbstractSystem; return T(args...; kwargs...) end +function extend(sys, basesys::Vector{T}) where {T <: AbstractSystem} + foldl(extend, basesys, init = sys) +end + function Base.:(&)(sys::AbstractSystem, basesys::AbstractSystem; kwargs...) extend(sys, basesys; kwargs...) end +function Base.:(&)( + sys::AbstractSystem, basesys::Vector{T}; kwargs...) where {T <: AbstractSystem} + extend(sys, basesys; kwargs...) +end + """ $(SIGNATURES) diff --git a/src/systems/model_parsing.jl b/src/systems/model_parsing.jl index e8955b5b84..5b79eaa91b 100644 --- a/src/systems/model_parsing.jl +++ b/src/systems/model_parsing.jl @@ -50,7 +50,7 @@ function _model_macro(mod, name, expr, isconnector) :structural_parameters => Dict{Symbol, Dict}() ) comps = Union{Symbol, Expr}[] - ext = Ref{Any}(nothing) + ext = [] eqs = Expr[] icon = Ref{Union{String, URI}}() ps, sps, vs, = [], [], [] @@ -115,10 +115,10 @@ function _model_macro(mod, name, expr, isconnector) sys = :($ODESystem($(flatten_equations)(equations), $iv, variables, parameters; name, systems, gui_metadata = $gui_metadata, defaults)) - if ext[] === nothing + if length(ext) == 0 push!(exprs.args, :(var"#___sys___" = $sys)) else - push!(exprs.args, :(var"#___sys___" = $extend($sys, $(ext[])))) + push!(exprs.args, :(var"#___sys___" = $extend($sys, [$(ext...)]))) end isconnector && push!(exprs.args, @@ -240,7 +240,7 @@ function unit_handled_variable_value(meta, varname) end # This function parses various variable/parameter definitions. -# +# # The comments indicate the syntax matched by a block; either when parsed directly # when it is called recursively for parsing a part of an expression. # These variable definitions are part of test suite in `test/model_parsing.jl` @@ -286,7 +286,7 @@ function parse_variable_def!(dict, mod, arg, varclass, kwargs, where_types; # `(l2(t)[1:N, 1:M] = 2), [description = "l is more than 1D, with arbitrary length"]` # `(l3(t)[1:3] = 3), [description = "l2 is 1D"]` # `(l4(t)[1:N] = 4), [description = "l2 is 1D, with arbitrary length"]` - # + # # Condition 2 parses: # `(l5(t)[1:3]::Int = 5), [description = "l3 is 1D and has a type"]` # `(l6(t)[1:N]::Int = 6), [description = "l3 is 1D and has a type, with arbitrary length"]` @@ -373,7 +373,7 @@ function parse_variable_def!(dict, mod, arg, varclass, kwargs, where_types; # Condition 1 is recursively called by: # `par5[1:3]::BigFloat` # `par6(t)[1:3]::BigFloat` - # + # # Condition 2 parses: # `b2(t)[1:2]` # `a2[1:2]` @@ -791,11 +791,17 @@ function _parse_extend!(ext, a, b, dict, expr, kwargs, vars, implicit_arglist) end end - ext[] = a + push!(ext, a) push!(b.args, Expr(:kw, :name, Meta.quot(a))) push!(expr.args, :($a = $b)) - dict[:extend] = [Symbol.(vars.args), a, b.args[1]] + if !haskey(dict, :extend) + dict[:extend] = [Symbol.(vars.args), a, b.args[1]] + else + push!(dict[:extend][1], Symbol.(vars.args)...) + dict[:extend][2] = vcat(dict[:extend][2], a) + dict[:extend][3] = vcat(dict[:extend][3], b.args[1]) + end push!(expr.args, :(@unpack $vars = $a)) end diff --git a/test/model_parsing.jl b/test/model_parsing.jl index 9cdd8712d4..fd223800e3 100644 --- a/test/model_parsing.jl +++ b/test/model_parsing.jl @@ -945,6 +945,15 @@ end end end +@mtkmodel MidModelB begin + @parameters begin + b + end + @components begin + inmodel_b = InnerModel() + end +end + @mtkmodel OuterModel begin @extend MidModel() @equations begin @@ -958,3 +967,15 @@ end @named out = OuterModel() @test OuterModel.structure[:extend][1] == [:inmodel] end + +@mtkmodel MultipleExtend begin + @extend MidModel() + @extend MidModelB() +end + +@testset "Multiple extend statements" begin + @named multiple_extend = MultipleExtend() + @test collect(nameof.(multiple_extend.systems)) == [:inmodel_b, :inmodel] + @test MultipleExtend.structure[:extend][1] == [:inmodel, :b, :inmodel_b] + @test tosymbol.(parameters(multiple_extend)) == [:b, :inmodel_b₊p, :inmodel₊p] +end From 8eab77a1377052dd5c49a783d2da4b6cf7110e59 Mon Sep 17 00:00:00 2001 From: Sam Isaacson Date: Thu, 7 Nov 2024 14:46:58 -0500 Subject: [PATCH 0316/2176] test passes --- src/systems/jumps/jumpsystem.jl | 2 +- test/jumpsystem.jl | 30 ++++++++++++++---------------- 2 files changed, 15 insertions(+), 17 deletions(-) diff --git a/src/systems/jumps/jumpsystem.jl b/src/systems/jumps/jumpsystem.jl index e497d86141..e745d0c627 100644 --- a/src/systems/jumps/jumpsystem.jl +++ b/src/systems/jumps/jumpsystem.jl @@ -288,7 +288,7 @@ function assemble_vrj( outputidxs = [unknowntoid[var] for var in outputvars] affect = eval_or_rgf(generate_affect_function(js, vrj.affect!, outputidxs); eval_expression, eval_module) - VariableRateJump(rate, affect) + VariableRateJump(rate, affect; save_positions = vrj.save_positions) end function assemble_vrj_expr(js, vrj, unknowntoid) diff --git a/test/jumpsystem.jl b/test/jumpsystem.jl index 8a3c2adccf..75bac26011 100644 --- a/test/jumpsystem.jl +++ b/test/jumpsystem.jl @@ -427,8 +427,8 @@ end let @variables X(t) Y(t) @parameters k1 k2 - vrj1 = VariableRateJump(k1 * X, [X ~ X - 1]) - vrj2 = VariableRateJump(k2, [Y ~ Y + 1]) + vrj1 = VariableRateJump(k1 * X, [X ~ X - 1]; save_positions = (false, false)) + vrj2 = VariableRateJump(k1, [Y ~ Y + 1]; save_positions = (false, false)) eqs = [D(X) ~ k2, D(Y) ~ -k2/100*Y] @named jsys = JumpSystem([vrj1, vrj2, eqs[1], eqs[2]], t, [X, Y], [k1, k2]) jsys = complete(jsys) @@ -436,25 +436,23 @@ let u0 = [X => X0, Y => Y0] k1val = 1.0; k2val = 20.0 p = [k1 => k1val, k2 => k2val] - tspan = (0.0, 20.0) + tspan = (0.0, 10.0) oprob = ODEProblem(jsys, u0, tspan, p) - jprob = JumpProblem(jsys, oprob; rng) + jprob = JumpProblem(jsys, oprob; rng, save_positions = (false, false)) - times = range(0.0, 20.0, length = 100) + times = range(0.0, tspan[2], length = 100) Nsims = 2000 - X = zeros(length(times)) - Y = similar(X) + Xv = zeros(length(times)) + Yv = copy(Xv) for n in 1:Nsims - sol = solve(jprob, Tsit5()) - X .+= Array(sol(times; idxs = X)) - Y .+= Array(sol(times; idxs = Y)) + sol = solve(jprob, Tsit5(); saveat = times) + Xv .+= sol[1,:] #sol(times; idxs = X) + Yv .+= sol[2,:] #sol(times; idxs = Y) end - X ./= Nsims; Y ./= Nsims; + Xv ./= Nsims; Yv ./= Nsims; Xact(t) = X0 * exp(-k1val * t) + (k2val / k1val) * (1 - exp(-k1val * t)) - Yact(t) = Y0 * exp(-k2val/100 * t) + (k1 / (k2val/100)) * (1 - exp(-k2val/100 * t)) - - @test all(abs.(X .- Xact.(times)) .<= 0.05 .* X) - @test all(abs.(Y .- Yact.(times)) .<= 0.05 .* Y) - + Yact(t) = Y0 * exp(-k2val/100 * t) + (k1val / (k2val/100)) * (1 - exp(-k2val/100 * t)) + @test all(abs.(Xv .- Xact.(times)) .<= 0.05 .* Xv) + @test all(abs.(Yv .- Yact.(times)) .<= 0.05 .* Yv) end \ No newline at end of file From 79344744e5a4e38771f49509245356b94e4a9ecf Mon Sep 17 00:00:00 2001 From: Sam Isaacson Date: Thu, 7 Nov 2024 14:47:08 -0500 Subject: [PATCH 0317/2176] updates --- test/jumpsystem.jl | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/test/jumpsystem.jl b/test/jumpsystem.jl index 75bac26011..7f049b0aa9 100644 --- a/test/jumpsystem.jl +++ b/test/jumpsystem.jl @@ -446,8 +446,10 @@ let Yv = copy(Xv) for n in 1:Nsims sol = solve(jprob, Tsit5(); saveat = times) - Xv .+= sol[1,:] #sol(times; idxs = X) - Yv .+= sol[2,:] #sol(times; idxs = Y) + + # use direct indexing as much faster than symbolic indexing + Xv .+= sol[1,:] + Yv .+= sol[2,:] end Xv ./= Nsims; Yv ./= Nsims; From 0a223169fa74f22b033c90e88e4b64874b46aaa4 Mon Sep 17 00:00:00 2001 From: Sam Isaacson Date: Thu, 7 Nov 2024 17:50:13 -0500 Subject: [PATCH 0318/2176] more sampled --- test/jumpsystem.jl | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/test/jumpsystem.jl b/test/jumpsystem.jl index 7f049b0aa9..204f4f2fb5 100644 --- a/test/jumpsystem.jl +++ b/test/jumpsystem.jl @@ -441,13 +441,11 @@ let jprob = JumpProblem(jsys, oprob; rng, save_positions = (false, false)) times = range(0.0, tspan[2], length = 100) - Nsims = 2000 + Nsims = 4000 Xv = zeros(length(times)) Yv = copy(Xv) for n in 1:Nsims sol = solve(jprob, Tsit5(); saveat = times) - - # use direct indexing as much faster than symbolic indexing Xv .+= sol[1,:] Yv .+= sol[2,:] end From caa236ae4141d1c3ef3f9b5b43bf23061ff5452b Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 7 Nov 2024 13:12:25 +0530 Subject: [PATCH 0319/2176] fix: fix discrete callback condition with array variable --- src/systems/callbacks.jl | 5 +++-- test/symbolic_events.jl | 21 +++++++++++++++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/src/systems/callbacks.jl b/src/systems/callbacks.jl index 0b758a6e86..ef1b2e3cd6 100644 --- a/src/systems/callbacks.jl +++ b/src/systems/callbacks.jl @@ -550,7 +550,7 @@ end """ compile_condition(cb::SymbolicDiscreteCallback, sys, dvs, ps; expression, kwargs...) -Returns a function `condition(u,p,t)` returning the `condition(cb)`. +Returns a function `condition(u,t,integrator)` returning the `condition(cb)`. Notes @@ -571,7 +571,8 @@ function compile_condition(cb::SymbolicDiscreteCallback, sys, dvs, ps; end expr = build_function( condit, u, t, p...; expression = Val{true}, - wrap_code = condition_header(sys) .∘ wrap_array_vars(sys, condit; dvs, ps) .∘ + wrap_code = condition_header(sys) .∘ + wrap_array_vars(sys, condit; dvs, ps, inputs = true) .∘ wrap_parameter_dependencies(sys, !(condit isa AbstractArray)), kwargs...) if expression == Val{true} diff --git a/test/symbolic_events.jl b/test/symbolic_events.jl index b58d5911f4..61690acdce 100644 --- a/test/symbolic_events.jl +++ b/test/symbolic_events.jl @@ -1075,3 +1075,24 @@ end prob = ODEProblem(pend, [x => 1], (0.0, 3.0), guesses = [y => x]) @test all(≈(0.0; atol = 1e-9), solve(prob, Rodas5())[[x, y]][end]) end + +@testset "Issue#3154 Array variable in discrete condition" begin + @mtkmodel DECAY begin + @parameters begin + unrelated[1:2] = zeros(2) + k = 0.0 + end + @variables begin + x(t) = 10.0 + end + @equations begin + D(x) ~ -k * x + end + @discrete_events begin + (t == 1.0) => [k ~ 1.0] + end + end + @mtkbuild decay = DECAY() + prob = ODEProblem(decay, [], (0.0, 10.0), []) + @test_nowarn solve(prob, Tsit5(), tstops = [1.0]) +end From 094d736fe1951fc46f8209739a22905ef34a64b8 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 8 Nov 2024 13:01:30 +0530 Subject: [PATCH 0320/2176] test: specify seed for SDDE solve --- test/dde.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/dde.jl b/test/dde.jl index aa4536e604..2030a90d06 100644 --- a/test/dde.jl +++ b/test/dde.jl @@ -74,7 +74,7 @@ pmul = [1.0, prob = SDDEProblem(hayes_modelf, hayes_modelg, [1.0], h, tspan, pmul; constant_lags = (pmul[1],)); -sol = solve(prob, RKMil()) +sol = solve(prob, RKMil(), seed = 100) @variables x(..) @parameters a=-4.0 b=-2.0 c=10.0 α=-1.3 β=-1.2 γ=1.1 @@ -87,7 +87,7 @@ eqs = [D(x(t)) ~ a * x(t) + b * x(t - τ) + c + (α * x(t) + γ) * η] @test equations(sys) == [D(x(t)) ~ a * x(t) + b * x(t - τ) + c] @test isequal(ModelingToolkit.get_noiseeqs(sys), [α * x(t) + γ]) prob_mtk = SDDEProblem(sys, [x(t) => 1.0 + t], tspan; constant_lags = (τ,)); -@test_nowarn sol_mtk = solve(prob_mtk, RKMil()) +@test_nowarn sol_mtk = solve(prob_mtk, RKMil(), seed = 100) prob_sa = SDDEProblem( sys, [x(t) => 1.0 + t], tspan; constant_lags = (τ,), u0_constructor = SVector{1}) From de628fa336af97fec346338dc4720d206d54ee20 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 8 Nov 2024 17:19:06 +0530 Subject: [PATCH 0321/2176] feat: cache start system and solver in HomotopyContinuation interface --- ext/MTKHomotopyContinuationExt.jl | 36 +++++++++++++++--------- src/systems/nonlinear/nonlinearsystem.jl | 7 ++++- test/extensions/homotopy_continuation.jl | 13 ++------- 3 files changed, 30 insertions(+), 26 deletions(-) diff --git a/ext/MTKHomotopyContinuationExt.jl b/ext/MTKHomotopyContinuationExt.jl index ebd8afbfc1..96adf2a959 100644 --- a/ext/MTKHomotopyContinuationExt.jl +++ b/ext/MTKHomotopyContinuationExt.jl @@ -32,14 +32,16 @@ function is_polynomial(x, wrt) end if operation(x) == (^) b, p = arguments(x) - is_pow_integer = symtype(p) <: Integer - if !is_pow_integer - if symbolic_type(p) == NotSymbolic() - @warn "In $x: Exponent $p is not an integer" - else - @warn "In $x: Exponent $p is not an integer. Use `@parameters p::Integer` to declare integer parameters." - end + if symbolic_type(p) != NotSymbolic() + @warn "In $x: Exponent $p cannot be symbolic" + is_pow_integer = false + elseif !(p isa Integer) + @warn "In $x: Exponent $p is not an integer" + is_pow_integer = false + else + is_pow_integer = true end + exponent_has_unknowns = contains_variable(p, wrt) if exponent_has_unknowns @warn "In $x: Exponent $p cannot contain unknowns of the system." @@ -179,6 +181,8 @@ Create a `HomotopyContinuationProblem` from a `NonlinearSystem` with polynomial The problem will be solved by HomotopyContinuation.jl. The resultant `NonlinearSolution` will contain the polynomial root closest to the point specified by `u0map` (if real roots exist for the system). + +Keyword arguments are forwarded to `HomotopyContinuation.solver_startsystems`. """ function MTK.HomotopyContinuationProblem( sys::NonlinearSystem, u0map, parammap = nothing; eval_expression = false, @@ -223,20 +227,24 @@ function MTK.HomotopyContinuationProblem( obsfn = MTK.ObservedFunctionCache(sys; eval_expression, eval_module) - return MTK.HomotopyContinuationProblem(u0, mtkhsys, denominator, sys, obsfn) + solver_and_starts = HomotopyContinuation.solver_startsolutions(mtkhsys; kwargs...) + return MTK.HomotopyContinuationProblem( + u0, mtkhsys, denominator, sys, obsfn, solver_and_starts) end """ $(TYPEDSIGNATURES) Solve a `HomotopyContinuationProblem`. Ignores the algorithm passed to it, and always -uses `HomotopyContinuation.jl`. All keyword arguments except the ones listed below are -forwarded to `HomotopyContinuation.solve`. The original solution as returned by +uses `HomotopyContinuation.jl`. The original solution as returned by `HomotopyContinuation.jl` will be available in the `.original` field of the returned `NonlinearSolution`. -All keyword arguments have their default values in HomotopyContinuation.jl, except -`show_progress` which defaults to `false`. +All keyword arguments except the ones listed below are forwarded to +`HomotopyContinuation.solve`. Note that the solver and start solutions are precomputed, +and only keyword arguments related to the solve process are valid. All keyword +arguments have their default values in HomotopyContinuation.jl, except `show_progress` +which defaults to `false`. Extra keyword arguments: - `denominator_abstol`: In case `prob` is solving a rational function, roots which cause @@ -244,8 +252,8 @@ Extra keyword arguments: """ function CommonSolve.solve(prob::MTK.HomotopyContinuationProblem, alg = nothing; show_progress = false, denominator_abstol = 1e-7, kwargs...) - sol = HomotopyContinuation.solve( - prob.homotopy_continuation_system; show_progress, kwargs...) + solver, starts = prob.solver_and_starts + sol = HomotopyContinuation.solve(solver, starts; show_progress, kwargs...) realsols = HomotopyContinuation.results(sol; only_real = true) if isempty(realsols) u = state_values(prob) diff --git a/src/systems/nonlinear/nonlinearsystem.jl b/src/systems/nonlinear/nonlinearsystem.jl index 44e77d5a0f..c6b2610ae7 100644 --- a/src/systems/nonlinear/nonlinearsystem.jl +++ b/src/systems/nonlinear/nonlinearsystem.jl @@ -690,7 +690,7 @@ A type of Nonlinear problem which specializes on polynomial systems and uses HomotopyContinuation.jl to solve the system. Requires importing HomotopyContinuation.jl to create and solve. """ -struct HomotopyContinuationProblem{uType, H, D, O} <: +struct HomotopyContinuationProblem{uType, H, D, O, SS} <: SciMLBase.AbstractNonlinearProblem{uType, true} """ The initial values of states in the system. If there are multiple real roots of @@ -716,6 +716,11 @@ struct HomotopyContinuationProblem{uType, H, D, O} <: A function which generates and returns observed expressions for the given system. """ obsfn::O + """ + The HomotopyContinuation.jl solver and start system, obtained through + `HomotopyContinuation.solver_startsystems`. + """ + solver_and_starts::SS end function HomotopyContinuationProblem(::AbstractSystem, _u0, _p; kwargs...) diff --git a/test/extensions/homotopy_continuation.jl b/test/extensions/homotopy_continuation.jl index 9bd64fa7fb..056700b805 100644 --- a/test/extensions/homotopy_continuation.jl +++ b/test/extensions/homotopy_continuation.jl @@ -61,20 +61,11 @@ end @test sol.retcode == ReturnCode.ConvergenceFailure end -@testset "Parametric exponent" begin - @variables x = 1.0 - @parameters n::Integer = 4 - @mtkbuild sys = NonlinearSystem([x^n + x^2 - 1 ~ 0]) - prob = HomotopyContinuationProblem(sys, []) - sol = solve(prob; threading = false) - @test SciMLBase.successful_retcode(sol) -end - @testset "Polynomial check and warnings" begin @variables x = 1.0 - @parameters n = 4 + @parameters n::Integer = 4 @mtkbuild sys = NonlinearSystem([x^n + x^2 - 1 ~ 0]) - @test_warn ["Exponent", "not an integer", "@parameters"] @test_throws "not a polynomial" HomotopyContinuationProblem( + @test_warn ["Exponent", "cannot be symbolic"] @test_throws "not a polynomial" HomotopyContinuationProblem( sys, []) @mtkbuild sys = NonlinearSystem([x^1.5 + x^2 - 1 ~ 0]) @test_warn ["Exponent", "not an integer"] @test_throws "not a polynomial" HomotopyContinuationProblem( From db7386bd51e8181f17553f41251dfb69f001434b Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 8 Nov 2024 20:04:20 +0530 Subject: [PATCH 0322/2176] fix: use `full_equations` when generating `HomotopyContinuationProblem` --- ext/MTKHomotopyContinuationExt.jl | 10 +++++++++- test/extensions/homotopy_continuation.jl | 22 ++++++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/ext/MTKHomotopyContinuationExt.jl b/ext/MTKHomotopyContinuationExt.jl index 96adf2a959..d714f3cbfb 100644 --- a/ext/MTKHomotopyContinuationExt.jl +++ b/ext/MTKHomotopyContinuationExt.jl @@ -192,7 +192,12 @@ function MTK.HomotopyContinuationProblem( end dvs = unknowns(sys) - eqs = equations(sys) + # we need to consider `full_equations` because observed also should be + # polynomials (if used in equations) and we don't know if observed is used + # in denominator. + # This is not the most efficient, and would be improved significantly with + # CSE/hashconsing. + eqs = full_equations(sys) denoms = [] eqs2 = map(eqs) do eq @@ -216,6 +221,9 @@ function MTK.HomotopyContinuationProblem( end sys2 = MTK.@set sys.eqs = eqs2 + # remove observed equations to avoid adding them in codegen + MTK.@set! sys2.observed = Equation[] + MTK.@set! sys2.substitutions = nothing nlfn, u0, p = MTK.process_SciMLProblem(NonlinearFunction{true}, sys2, u0map, parammap; jac = true, eval_expression, eval_module) diff --git a/test/extensions/homotopy_continuation.jl b/test/extensions/homotopy_continuation.jl index 056700b805..5c4470befa 100644 --- a/test/extensions/homotopy_continuation.jl +++ b/test/extensions/homotopy_continuation.jl @@ -76,6 +76,11 @@ end @mtkbuild sys = NonlinearSystem([((x^2) / sin(x))^2 + x ~ 0]) @test_warn ["Unrecognized", "sin"] @test_throws "not a polynomial" HomotopyContinuationProblem( sys, []) + + @variables y = 2.0 + @mtkbuild sys = NonlinearSystem([x^2 + y^2 + 2 ~ 0, y ~ sin(x)]) + @test_warn ["Unrecognized", "sin"] @test_throws "not a polynomial" HomotopyContinuationProblem( + sys, []) end @testset "Rational functions" begin @@ -122,4 +127,21 @@ end end end @test prob.denominator([2.0, 4.0], p)[1] <= 1e-8 + + @testset "Rational function in observed" begin + @variables x=1 y=1 + @mtkbuild sys = NonlinearSystem([x^2 + y^2 - 2x - 2 ~ 0, y ~ (x - 1) / (x - 2)]) + prob = HomotopyContinuationProblem(sys, []) + @test any(prob.denominator([2.0], parameter_values(prob)) .≈ 0.0) + @test_nowarn solve(prob; threading = false) + end +end + +@test "Non-polynomial observed not used in equations" begin + @variables x=1 y + @mtkbuild sys = NonlinearSystem([x^2 - 2 ~ 0, y ~ sin(x)]) + prob = HomotopyContinuationProblem(sys, []) + sol = @test_nowarn solve(prob; threading = false) + @test sol[x] ≈ √2.0 + @test sol[y] ≈ sin(√2.0) end From 2c95e1941cd53cc080ccaa877c15f6dc9813d972 Mon Sep 17 00:00:00 2001 From: Sam Isaacson Date: Fri, 8 Nov 2024 10:26:48 -0500 Subject: [PATCH 0323/2176] losen test tolerance --- test/jumpsystem.jl | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/test/jumpsystem.jl b/test/jumpsystem.jl index 204f4f2fb5..9569208474 100644 --- a/test/jumpsystem.jl +++ b/test/jumpsystem.jl @@ -425,11 +425,12 @@ end # PDMP test let + Random.seed!(rng, 1111) @variables X(t) Y(t) @parameters k1 k2 vrj1 = VariableRateJump(k1 * X, [X ~ X - 1]; save_positions = (false, false)) vrj2 = VariableRateJump(k1, [Y ~ Y + 1]; save_positions = (false, false)) - eqs = [D(X) ~ k2, D(Y) ~ -k2/100*Y] + eqs = [D(X) ~ k2, D(Y) ~ -k2/10*Y] @named jsys = JumpSystem([vrj1, vrj2, eqs[1], eqs[2]], t, [X, Y], [k1, k2]) jsys = complete(jsys) X0 = 0.0; Y0 = 0.0 @@ -443,7 +444,7 @@ let times = range(0.0, tspan[2], length = 100) Nsims = 4000 Xv = zeros(length(times)) - Yv = copy(Xv) + Yv = zeros(length(times)) for n in 1:Nsims sol = solve(jprob, Tsit5(); saveat = times) Xv .+= sol[1,:] @@ -452,7 +453,7 @@ let Xv ./= Nsims; Yv ./= Nsims; Xact(t) = X0 * exp(-k1val * t) + (k2val / k1val) * (1 - exp(-k1val * t)) - Yact(t) = Y0 * exp(-k2val/100 * t) + (k1val / (k2val/100)) * (1 - exp(-k2val/100 * t)) + Yact(t) = Y0 * exp(-k2val/10 * t) + (k1val / (k2val/10)) * (1 - exp(-k2val/10 * t)) @test all(abs.(Xv .- Xact.(times)) .<= 0.05 .* Xv) - @test all(abs.(Yv .- Yact.(times)) .<= 0.05 .* Yv) + @test all(abs.(Yv .- Yact.(times)) .<= 0.1 .* Yv) end \ No newline at end of file From 03fd5fad23be1f02b1aeee4bd48dcc8e9811d818 Mon Sep 17 00:00:00 2001 From: Alex Hirzel Date: Fri, 8 Nov 2024 21:15:40 -0500 Subject: [PATCH 0324/2176] Update stochastic_diffeq.md - fix Brownian coefficient this changes the coefficient from 0.1 to 0.3 to match the rest of the text and code of this example. --- docs/src/tutorials/stochastic_diffeq.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/src/tutorials/stochastic_diffeq.md b/docs/src/tutorials/stochastic_diffeq.md index a6543dc977..79c71a2e8d 100644 --- a/docs/src/tutorials/stochastic_diffeq.md +++ b/docs/src/tutorials/stochastic_diffeq.md @@ -23,9 +23,9 @@ where the magnitude of the noise scales with (0.3 times) the magnitude of each o ```math \begin{aligned} -\frac{dx}{dt} &= (\sigma (y-x)) &+ 0.1x\frac{dB}{dt} \\ -\frac{dy}{dt} &= (x(\rho-z) - y) &+ 0.1y\frac{dB}{dt} \\ -\frac{dz}{dt} &= (xy - \beta z) &+ 0.1z\frac{dB}{dt} \\ +\frac{dx}{dt} &= (\sigma (y-x)) &+ 0.3x\frac{dB}{dt} \\ +\frac{dy}{dt} &= (x(\rho-z) - y) &+ 0.3y\frac{dB}{dt} \\ +\frac{dz}{dt} &= (xy - \beta z) &+ 0.3z\frac{dB}{dt} \\ \end{aligned} ``` From d326ab494b5026b90f12f6d5648905d894818bfd Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Sat, 9 Nov 2024 21:31:10 +0530 Subject: [PATCH 0325/2176] refactor: allow parametric exponents --- Project.toml | 2 + ext/MTKHomotopyContinuationExt.jl | 129 +++++++++++++++++++---- src/ModelingToolkit.jl | 1 + test/extensions/homotopy_continuation.jl | 23 ++-- 4 files changed, 126 insertions(+), 29 deletions(-) diff --git a/Project.toml b/Project.toml index ea380ef6b1..0e804e1f63 100644 --- a/Project.toml +++ b/Project.toml @@ -21,6 +21,7 @@ Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f" DocStringExtensions = "ffbed154-4ef7-542d-bbb7-c09d3a79fcae" DomainSets = "5b8099bc-c8ec-5219-889f-1d9e522a28bf" DynamicQuantities = "06fc5a27-2a28-4c7c-a15d-362465fb6821" +EnumX = "4e289a0a-7415-4d19-859d-a7e5c4648b56" ExprTools = "e2ba6199-217a-4e67-a87a-7c52f15ade04" Expronicon = "6b7a57c9-7cc1-4fdf-b7f5-e857abae3636" FindFirstFunctions = "64ca27bc-2ba2-4a57-88aa-44e436879224" @@ -94,6 +95,7 @@ Distributions = "0.23, 0.24, 0.25" DocStringExtensions = "0.7, 0.8, 0.9" DomainSets = "0.6, 0.7" DynamicQuantities = "^0.11.2, 0.12, 0.13, 1" +EnumX = "1.0.4" ExprTools = "0.1.10" Expronicon = "0.8" FindFirstFunctions = "1" diff --git a/ext/MTKHomotopyContinuationExt.jl b/ext/MTKHomotopyContinuationExt.jl index d714f3cbfb..19c21483c3 100644 --- a/ext/MTKHomotopyContinuationExt.jl +++ b/ext/MTKHomotopyContinuationExt.jl @@ -15,12 +15,70 @@ function contains_variable(x, wrt) any(y -> occursin(y, x), wrt) end +""" +Possible reasons why a term is not polynomial +""" +MTK.EnumX.@enumx NonPolynomialReason begin + NonIntegerExponent + ExponentContainsUnknowns + BaseNotPolynomial + UnrecognizedOperation +end + +function display_reason(reason::NonPolynomialReason.T, sym) + if reason == NonPolynomialReason.NonIntegerExponent + pow = arguments(sym)[2] + "In $sym: Exponent $pow is not an integer" + elseif reason == NonPolynomialReason.ExponentContainsUnknowns + pow = arguments(sym)[2] + "In $sym: Exponent $pow contains unknowns of the system" + elseif reason == NonPolynomialReason.BaseNotPolynomial + base = arguments(sym)[1] + "In $sym: Base $base is not a polynomial in the unknowns" + elseif reason == NonPolynomialReason.UnrecognizedOperation + op = operation(sym) + """ + In $sym: Operation $op is not recognized. Allowed polynomial operations are \ + `*, /, +, -, ^`. + """ + else + error("This should never happen. Please open an issue in ModelingToolkit.jl.") + end +end + +mutable struct PolynomialData + non_polynomial_terms::Vector{BasicSymbolic} + reasons::Vector{NonPolynomialReason.T} + has_parametric_exponent::Bool +end + +PolynomialData() = PolynomialData(BasicSymbolic[], NonPolynomialReason.T[], false) + +struct NotPolynomialError <: Exception + eq::Equation + data::PolynomialData +end + +function Base.showerror(io::IO, err::NotPolynomialError) + println(io, + "Equation $(err.eq) is not a polynomial in the unknowns for the following reasons:") + for (term, reason) in zip(err.data.non_polynomial_terms, err.data.reasons) + println(io, display_reason(reason, term)) + end +end + +function is_polynomial!(data, y, wrt) + process_polynomial!(data, y, wrt) + isempty(data.reasons) +end + """ $(TYPEDSIGNATURES) -Check if `x` is polynomial with respect to the variables in `wrt`. +Return information about the polynmial `x` with respect to variables in `wrt`, +writing said information to `data`. """ -function is_polynomial(x, wrt) +function process_polynomial!(data::PolynomialData, x, wrt) x = unwrap(x) symbolic_type(x) == NotSymbolic() && return true iscall(x) || return true @@ -28,31 +86,33 @@ function is_polynomial(x, wrt) any(isequal(x), wrt) && return true if operation(x) in (*, +, -, /) - return all(y -> is_polynomial(y, wrt), arguments(x)) + return all(y -> is_polynomial!(data, y, wrt), arguments(x)) end if operation(x) == (^) b, p = arguments(x) + is_pow_integer = symtype(p) <: Integer + if !is_pow_integer + push!(data.non_polynomial_terms, x) + push!(data.reasons, NonPolynomialReason.NonIntegerExponent) + end if symbolic_type(p) != NotSymbolic() - @warn "In $x: Exponent $p cannot be symbolic" - is_pow_integer = false - elseif !(p isa Integer) - @warn "In $x: Exponent $p is not an integer" - is_pow_integer = false - else - is_pow_integer = true + data.has_parametric_exponent = true end exponent_has_unknowns = contains_variable(p, wrt) if exponent_has_unknowns - @warn "In $x: Exponent $p cannot contain unknowns of the system." + push!(data.non_polynomial_terms, x) + push!(data.reasons, NonPolynomialReason.ExponentContainsUnknowns) end - base_polynomial = is_polynomial(b, wrt) + base_polynomial = is_polynomial!(data, b, wrt) if !base_polynomial - @warn "In $x: Base is not a polynomial" + push!(data.non_polynomial_terms, x) + push!(data.reasons, NonPolynomialReason.BaseNotPolynomial) end return base_polynomial && !exponent_has_unknowns && is_pow_integer end - @warn "In $x: Unrecognized operation $(operation(x)). Allowed polynomial operations are `*, +, -, ^`" + push!(data.non_polynomial_terms, x) + push!(data.reasons, NonPolynomialReason.UnrecognizedOperation) return false end @@ -182,11 +242,17 @@ The problem will be solved by HomotopyContinuation.jl. The resultant `NonlinearS will contain the polynomial root closest to the point specified by `u0map` (if real roots exist for the system). -Keyword arguments are forwarded to `HomotopyContinuation.solver_startsystems`. +Keyword arguments: +- `eval_expression`: Whether to `eval` the generated functions or use a `RuntimeGeneratedFunction`. +- `eval_module`: The module to use for `eval`/`@RuntimeGeneratedFunction` +- `warn_parametric_exponent`: Whether to warn if the system contains a parametric + exponent preventing the homotopy from being cached. + +All other keyword arguments are forwarded to `HomotopyContinuation.solver_startsystems`. """ function MTK.HomotopyContinuationProblem( sys::NonlinearSystem, u0map, parammap = nothing; eval_expression = false, - eval_module = ModelingToolkit, kwargs...) + eval_module = ModelingToolkit, warn_parametric_exponent = true, kwargs...) if !iscomplete(sys) error("A completed `NonlinearSystem` is required. Call `complete` or `structural_simplify` on the system before creating a `HomotopyContinuationProblem`") end @@ -200,9 +266,14 @@ function MTK.HomotopyContinuationProblem( eqs = full_equations(sys) denoms = [] + has_parametric_exponents = false eqs2 = map(eqs) do eq - if !is_polynomial(eq.lhs, dvs) || !is_polynomial(eq.rhs, dvs) - error("Equation $eq is not a polynomial in the unknowns. See warnings for further details.") + data = PolynomialData() + process_polynomial!(data, eq.lhs, dvs) + process_polynomial!(data, eq.rhs, dvs) + has_parametric_exponents |= data.has_parametric_exponent + if !isempty(data.non_polynomial_terms) + throw(NotPolynomialError(eq, data)) end num, den = handle_rational_polynomials(eq.rhs - eq.lhs, dvs) @@ -235,7 +306,18 @@ function MTK.HomotopyContinuationProblem( obsfn = MTK.ObservedFunctionCache(sys; eval_expression, eval_module) - solver_and_starts = HomotopyContinuation.solver_startsolutions(mtkhsys; kwargs...) + if has_parametric_exponents + if warn_parametric_exponent + @warn """ + The system has parametric exponents, preventing caching of the homotopy. \ + This will cause `solve` to be slower. Pass `warn_parametric_exponent \ + = false` to turn off this warning + """ + end + solver_and_starts = nothing + else + solver_and_starts = HomotopyContinuation.solver_startsolutions(mtkhsys; kwargs...) + end return MTK.HomotopyContinuationProblem( u0, mtkhsys, denominator, sys, obsfn, solver_and_starts) end @@ -260,8 +342,13 @@ Extra keyword arguments: """ function CommonSolve.solve(prob::MTK.HomotopyContinuationProblem, alg = nothing; show_progress = false, denominator_abstol = 1e-7, kwargs...) - solver, starts = prob.solver_and_starts - sol = HomotopyContinuation.solve(solver, starts; show_progress, kwargs...) + if prob.solver_and_starts === nothing + sol = HomotopyContinuation.solve( + prob.homotopy_continuation_system; show_progress, kwargs...) + else + solver, starts = prob.solver_and_starts + sol = HomotopyContinuation.solve(solver, starts; show_progress, kwargs...) + end realsols = HomotopyContinuation.results(sol; only_real = true) if isempty(realsols) u = state_values(prob) diff --git a/src/ModelingToolkit.jl b/src/ModelingToolkit.jl index bdb9168fa0..de8e69c41f 100644 --- a/src/ModelingToolkit.jl +++ b/src/ModelingToolkit.jl @@ -55,6 +55,7 @@ using RecursiveArrayTools import Graphs: SimpleDiGraph, add_edge!, incidence_matrix import BlockArrays: BlockedArray, Block, blocksize, blocksizes import CommonSolve +import EnumX using RuntimeGeneratedFunctions using RuntimeGeneratedFunctions: drop_expr diff --git a/test/extensions/homotopy_continuation.jl b/test/extensions/homotopy_continuation.jl index 5c4470befa..172ee58890 100644 --- a/test/extensions/homotopy_continuation.jl +++ b/test/extensions/homotopy_continuation.jl @@ -61,25 +61,32 @@ end @test sol.retcode == ReturnCode.ConvergenceFailure end -@testset "Polynomial check and warnings" begin +@testset "Parametric exponents" begin @variables x = 1.0 @parameters n::Integer = 4 @mtkbuild sys = NonlinearSystem([x^n + x^2 - 1 ~ 0]) - @test_warn ["Exponent", "cannot be symbolic"] @test_throws "not a polynomial" HomotopyContinuationProblem( - sys, []) + prob = @test_warn ["parametric", "exponent"] HomotopyContinuationProblem(sys, []) + @test prob.solver_and_starts === nothing + @test_nowarn HomotopyContinuationProblem(sys, []; warn_parametric_exponent = false) + sol = solve(prob; threading = false) + @test SciMLBase.successful_retcode(sol) +end + +@testset "Polynomial check and warnings" begin + @variables x = 1.0 @mtkbuild sys = NonlinearSystem([x^1.5 + x^2 - 1 ~ 0]) - @test_warn ["Exponent", "not an integer"] @test_throws "not a polynomial" HomotopyContinuationProblem( + @test_throws ["Exponent", "not an integer", "not a polynomial"] HomotopyContinuationProblem( sys, []) @mtkbuild sys = NonlinearSystem([x^x - x ~ 0]) - @test_warn ["Exponent", "unknowns"] @test_throws "not a polynomial" HomotopyContinuationProblem( + @test_throws ["Exponent", "unknowns", "not a polynomial"] HomotopyContinuationProblem( sys, []) @mtkbuild sys = NonlinearSystem([((x^2) / sin(x))^2 + x ~ 0]) - @test_warn ["Unrecognized", "sin"] @test_throws "not a polynomial" HomotopyContinuationProblem( + @test_throws ["recognized", "sin", "not a polynomial"] HomotopyContinuationProblem( sys, []) @variables y = 2.0 @mtkbuild sys = NonlinearSystem([x^2 + y^2 + 2 ~ 0, y ~ sin(x)]) - @test_warn ["Unrecognized", "sin"] @test_throws "not a polynomial" HomotopyContinuationProblem( + @test_throws ["recognized", "sin", "not a polynomial"] HomotopyContinuationProblem( sys, []) end @@ -137,7 +144,7 @@ end end end -@test "Non-polynomial observed not used in equations" begin +@testset "Non-polynomial observed not used in equations" begin @variables x=1 y @mtkbuild sys = NonlinearSystem([x^2 - 2 ~ 0, y ~ sin(x)]) prob = HomotopyContinuationProblem(sys, []) From d2849786277bbb03232672542d10565437f1e18a Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Sun, 10 Nov 2024 01:35:19 +0530 Subject: [PATCH 0326/2176] test: move BifurcationKit to the end of the group --- test/runtests.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/runtests.jl b/test/runtests.jl index c7edbfaaf5..192ead935c 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -109,9 +109,9 @@ end if GROUP == "All" || GROUP == "Extensions" activate_extensions_env() - @safetestset "BifurcationKit Extension Test" include("extensions/bifurcationkit.jl") @safetestset "HomotopyContinuation Extension Test" include("extensions/homotopy_continuation.jl") @safetestset "Auto Differentiation Test" include("extensions/ad.jl") @safetestset "LabelledArrays Test" include("labelledarrays.jl") + @safetestset "BifurcationKit Extension Test" include("extensions/bifurcationkit.jl") end end From 5f0a4eacbe7b46e727e2f0d165b0460ede788e98 Mon Sep 17 00:00:00 2001 From: Sam Isaacson Date: Mon, 11 Nov 2024 12:13:36 -0500 Subject: [PATCH 0327/2176] finish tests --- test/jumpsystem.jl | 68 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 67 insertions(+), 1 deletion(-) diff --git a/test/jumpsystem.jl b/test/jumpsystem.jl index 9569208474..7de84ea5a3 100644 --- a/test/jumpsystem.jl +++ b/test/jumpsystem.jl @@ -1,5 +1,5 @@ using ModelingToolkit, DiffEqBase, JumpProcesses, Test, LinearAlgebra -using Random, StableRNGs +using Random, StableRNGs, NonlinearSolve using OrdinaryDiffEq using ModelingToolkit: t_nounits as t, D_nounits as D MT = ModelingToolkit @@ -456,4 +456,70 @@ let Yact(t) = Y0 * exp(-k2val/10 * t) + (k1val / (k2val/10)) * (1 - exp(-k2val/10 * t)) @test all(abs.(Xv .- Xact.(times)) .<= 0.05 .* Xv) @test all(abs.(Yv .- Yact.(times)) .<= 0.1 .* Yv) +end + +# that mixes ODEs and jump types, and then contin events +let + Random.seed!(rng, 1111) + @variables X(t) Y(t) + @parameters α β + vrj = VariableRateJump(β * X, [X ~ X - 1]; save_positions = (false, false)) + crj = ConstantRateJump(β*Y, [Y ~ Y - 1]) + maj = MassActionJump(α, [0 => 1], [Y => 1]) + eqs = [D(X) ~ α*(1 + Y)] + @named jsys = JumpSystem([maj, crj, vrj, eqs[1]], t, [X, Y], [α, β]) + jsys = complete(jsys) + + p = (α = 6.0, β = 2.0, X₀ = 2.0, Y₀ = 1.0) + u0map = [X => p.X₀, Y => p.Y₀] + pmap = [α => p.α, β => p.β] + tspan = (0.0, 20.0) + oprob = ODEProblem(jsys, u0map, tspan, pmap) + jprob = JumpProblem(jsys, oprob; rng, save_positions = (false, false)) + + times = range(0.0, tspan[2], length = 100) + Nsims = 4000 + Xv = zeros(length(times)) + Yv = zeros(length(times)) + for n in 1:Nsims + sol = solve(jprob, Tsit5(); saveat = times) + Xv .+= sol[1,:] + Yv .+= sol[2,:] + end + Xv ./= Nsims; Yv ./= Nsims; + function Yf(t, p) + @unpack α, β, Y₀ = p + return (α / β) + (Y₀ - α / β) * exp(-β * t) + end + function Xf(t, p) + @unpack α, β, X₀, Y₀ = p + return (α / β) + (α^2 / β^2) + α * (Y₀ - α / β) * t * exp(-β * t) + (X₀ - α / β - α^2 / β^2) * exp(-β * t) + end + Xact = [Xf(t,p) for t in times] + Yact = [Yf(t,p) for t in times] + @test all(abs.(Xv .- Xact) .<= 0.05 .* Xv) + @test all(abs.(Yv .- Yact) .<= 0.05 .* Yv) + + Xss = (p.α / p.β) + (p.α^2 / p.β^2) + function affect!(integ, u, p, ctx) + savevalues!(integ, true) + terminate!(integ) + nothing + end + cevents = [t ~ .2] => (affect!, [], [], [], nothing) + @named jsys = JumpSystem([maj, crj, vrj, eqs[1]], t, [X, Y], [α, β]; + continuous_events = cevents) + jsys = complete(jsys) + tspan = (0.0, 200.0) + oprob = ODEProblem(jsys, u0map, tspan, pmap) + jprob = JumpProblem(jsys, oprob; rng, save_positions = (false, false)) + Xsamp = 0.0 + Nsims = 4000 + for n in 1:Nsims + sol = solve(jprob, Tsit5(), saveat = tspan[2]) + @test sol.retcode == ReturnCode.Terminated + Xsamp += sol[1,end] + end + Xsamp /= Nsims + @test abs(Xsamp - Xf(.2,p) < .05 * Xf(.2,p)) end \ No newline at end of file From 350f9df1540cf888efefb48b9ca940e5841c81af Mon Sep 17 00:00:00 2001 From: Sam Isaacson Date: Mon, 11 Nov 2024 12:26:57 -0500 Subject: [PATCH 0328/2176] bug fix --- test/jumpsystem.jl | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/test/jumpsystem.jl b/test/jumpsystem.jl index 7de84ea5a3..7841e3b4ff 100644 --- a/test/jumpsystem.jl +++ b/test/jumpsystem.jl @@ -469,14 +469,12 @@ let eqs = [D(X) ~ α*(1 + Y)] @named jsys = JumpSystem([maj, crj, vrj, eqs[1]], t, [X, Y], [α, β]) jsys = complete(jsys) - p = (α = 6.0, β = 2.0, X₀ = 2.0, Y₀ = 1.0) u0map = [X => p.X₀, Y => p.Y₀] pmap = [α => p.α, β => p.β] tspan = (0.0, 20.0) oprob = ODEProblem(jsys, u0map, tspan, pmap) jprob = JumpProblem(jsys, oprob; rng, save_positions = (false, false)) - times = range(0.0, tspan[2], length = 100) Nsims = 4000 Xv = zeros(length(times)) @@ -487,20 +485,20 @@ let Yv .+= sol[2,:] end Xv ./= Nsims; Yv ./= Nsims; + function Yf(t, p) - @unpack α, β, Y₀ = p + local α, β, X₀, Y₀ = p return (α / β) + (Y₀ - α / β) * exp(-β * t) end function Xf(t, p) - @unpack α, β, X₀, Y₀ = p + local α, β, X₀, Y₀ = p return (α / β) + (α^2 / β^2) + α * (Y₀ - α / β) * t * exp(-β * t) + (X₀ - α / β - α^2 / β^2) * exp(-β * t) end Xact = [Xf(t,p) for t in times] Yact = [Yf(t,p) for t in times] @test all(abs.(Xv .- Xact) .<= 0.05 .* Xv) @test all(abs.(Yv .- Yact) .<= 0.05 .* Yv) - - Xss = (p.α / p.β) + (p.α^2 / p.β^2) + function affect!(integ, u, p, ctx) savevalues!(integ, true) terminate!(integ) From 4c6dea1d1f16977ba47753fe5fad64d7fff98ee5 Mon Sep 17 00:00:00 2001 From: Sam Isaacson Date: Mon, 11 Nov 2024 13:10:12 -0500 Subject: [PATCH 0329/2176] format --- src/systems/callbacks.jl | 8 ++--- src/systems/jumps/jumpsystem.jl | 8 ++--- test/jumpsystem.jl | 63 ++++++++++++++++++--------------- 3 files changed, 43 insertions(+), 36 deletions(-) diff --git a/src/systems/callbacks.jl b/src/systems/callbacks.jl index dcc48286af..e5db9a71b6 100644 --- a/src/systems/callbacks.jl +++ b/src/systems/callbacks.jl @@ -678,8 +678,8 @@ function compile_affect(eqs::Vector{Equation}, cb, sys, dvs, ps; outputidxs = no end end -function generate_rootfinding_callback(sys::AbstractTimeDependentSystem, dvs = unknowns(sys), - ps = parameters(sys); kwargs...) +function generate_rootfinding_callback(sys::AbstractTimeDependentSystem, + dvs = unknowns(sys), ps = parameters(sys); kwargs...) cbs = continuous_events(sys) isempty(cbs) && return nothing generate_rootfinding_callback(cbs, sys, dvs, ps; kwargs...) @@ -860,8 +860,8 @@ function compile_affect_fn(cb, sys::AbstractTimeDependentSystem, dvs, ps, kwargs (affect = affect, affect_neg = affect_neg, initialize = initialize, finalize = finalize) end -function generate_rootfinding_callback(cbs, sys::AbstractTimeDependentSystem, dvs = unknowns(sys), - ps = parameters(sys); kwargs...) +function generate_rootfinding_callback(cbs, sys::AbstractTimeDependentSystem, + dvs = unknowns(sys), ps = parameters(sys); kwargs...) eqs = map(cb -> flatten_equations(cb.eqs), cbs) num_eqs = length.(eqs) total_eqs = sum(num_eqs) diff --git a/src/systems/jumps/jumpsystem.jl b/src/systems/jumps/jumpsystem.jl index e745d0c627..e5e17fb5f9 100644 --- a/src/systems/jumps/jumpsystem.jl +++ b/src/systems/jumps/jumpsystem.jl @@ -140,7 +140,7 @@ struct JumpSystem{U <: ArrayPartition} <: AbstractTimeDependentSystem end new{U}(tag, ap, iv, unknowns, ps, var_to_name, observed, name, description, systems, defaults, - connector_type, cevents, devents, parameter_dependencies, metadata, + connector_type, cevents, devents, parameter_dependencies, metadata, gui_metadata, complete, index_cache, isscheduled) end end @@ -494,17 +494,17 @@ function DiffEqBase.ODEProblem(sys::JumpSystem, u0map, tspan::Union{Tuple, Nothi if has_equations(sys) osys = ODESystem(equations(sys).x[4], get_iv(sys), unknowns(sys), parameters(sys); observed = observed(sys), name = nameof(sys), description = description(sys), - systems = get_systems(sys), defaults = defaults(sys), + systems = get_systems(sys), defaults = defaults(sys), parameter_dependencies = parameter_dependencies(sys), metadata = get_metadata(sys), gui_metadata = get_gui_metadata(sys)) osys = complete(osys) return ODEProblem(osys, u0map, tspan, parammap; check_length = 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], use_union, tofloat = false, check_length = false) f = (du, u, p, t) -> (du .= 0; nothing) - observedfun = ObservedFunctionCache(sys; eval_expression, eval_module) + observedfun = ObservedFunctionCache(sys; eval_expression, eval_module) df = ODEFunction(f; sys, observed = observedfun) return ODEProblem(df, u0, tspan, p; kwargs...) end diff --git a/test/jumpsystem.jl b/test/jumpsystem.jl index 7841e3b4ff..d27a54598b 100644 --- a/test/jumpsystem.jl +++ b/test/jumpsystem.jl @@ -301,7 +301,7 @@ end # basic VariableRateJump test let N = 1000 # number of simulations for testing solve accuracy - Random.seed!(rng, 1111) + Random.seed!(rng, 1111) @variables A(t) B(t) C(t) @parameters k vrj = VariableRateJump(k * (sin(t) + 1), [A ~ A + 1, C ~ C + 2]) @@ -425,17 +425,19 @@ end # PDMP test let - Random.seed!(rng, 1111) + Random.seed!(rng, 1111) @variables X(t) Y(t) @parameters k1 k2 vrj1 = VariableRateJump(k1 * X, [X ~ X - 1]; save_positions = (false, false)) vrj2 = VariableRateJump(k1, [Y ~ Y + 1]; save_positions = (false, false)) - eqs = [D(X) ~ k2, D(Y) ~ -k2/10*Y] + eqs = [D(X) ~ k2, D(Y) ~ -k2 / 10 * Y] @named jsys = JumpSystem([vrj1, vrj2, eqs[1], eqs[2]], t, [X, Y], [k1, k2]) jsys = complete(jsys) - X0 = 0.0; Y0 = 0.0 + X0 = 0.0 + Y0 = 0.0 u0 = [X => X0, Y => Y0] - k1val = 1.0; k2val = 20.0 + k1val = 1.0 + k2val = 20.0 p = [k1 => k1val, k2 => k2val] tspan = (0.0, 10.0) oprob = ODEProblem(jsys, u0, tspan, p) @@ -447,27 +449,30 @@ let Yv = zeros(length(times)) for n in 1:Nsims sol = solve(jprob, Tsit5(); saveat = times) - Xv .+= sol[1,:] - Yv .+= sol[2,:] + Xv .+= sol[1, :] + Yv .+= sol[2, :] end - Xv ./= Nsims; Yv ./= Nsims; + Xv ./= Nsims + Yv ./= Nsims Xact(t) = X0 * exp(-k1val * t) + (k2val / k1val) * (1 - exp(-k1val * t)) - Yact(t) = Y0 * exp(-k2val/10 * t) + (k1val / (k2val/10)) * (1 - exp(-k2val/10 * t)) + function Yact(t) + Y0 * exp(-k2val / 10 * t) + (k1val / (k2val / 10)) * (1 - exp(-k2val / 10 * t)) + end @test all(abs.(Xv .- Xact.(times)) .<= 0.05 .* Xv) - @test all(abs.(Yv .- Yact.(times)) .<= 0.1 .* Yv) + @test all(abs.(Yv .- Yact.(times)) .<= 0.1 .* Yv) end # that mixes ODEs and jump types, and then contin events let - Random.seed!(rng, 1111) + Random.seed!(rng, 1111) @variables X(t) Y(t) @parameters α β vrj = VariableRateJump(β * X, [X ~ X - 1]; save_positions = (false, false)) - crj = ConstantRateJump(β*Y, [Y ~ Y - 1]) + crj = ConstantRateJump(β * Y, [Y ~ Y - 1]) maj = MassActionJump(α, [0 => 1], [Y => 1]) - eqs = [D(X) ~ α*(1 + Y)] - @named jsys = JumpSystem([maj, crj, vrj, eqs[1]], t, [X, Y], [α, β]) + eqs = [D(X) ~ α * (1 + Y)] + @named jsys = JumpSystem([maj, crj, vrj, eqs[1]], t, [X, Y], [α, β]) jsys = complete(jsys) p = (α = 6.0, β = 2.0, X₀ = 2.0, Y₀ = 1.0) u0map = [X => p.X₀, Y => p.Y₀] @@ -481,10 +486,11 @@ let Yv = zeros(length(times)) for n in 1:Nsims sol = solve(jprob, Tsit5(); saveat = times) - Xv .+= sol[1,:] - Yv .+= sol[2,:] + Xv .+= sol[1, :] + Yv .+= sol[2, :] end - Xv ./= Nsims; Yv ./= Nsims; + Xv ./= Nsims + Yv ./= Nsims function Yf(t, p) local α, β, X₀, Y₀ = p @@ -492,21 +498,22 @@ let end function Xf(t, p) local α, β, X₀, Y₀ = p - return (α / β) + (α^2 / β^2) + α * (Y₀ - α / β) * t * exp(-β * t) + (X₀ - α / β - α^2 / β^2) * exp(-β * t) + return (α / β) + (α^2 / β^2) + α * (Y₀ - α / β) * t * exp(-β * t) + + (X₀ - α / β - α^2 / β^2) * exp(-β * t) end - Xact = [Xf(t,p) for t in times] - Yact = [Yf(t,p) for t in times] + Xact = [Xf(t, p) for t in times] + Yact = [Yf(t, p) for t in times] @test all(abs.(Xv .- Xact) .<= 0.05 .* Xv) - @test all(abs.(Yv .- Yact) .<= 0.05 .* Yv) - + @test all(abs.(Yv .- Yact) .<= 0.05 .* Yv) + function affect!(integ, u, p, ctx) savevalues!(integ, true) terminate!(integ) nothing end - cevents = [t ~ .2] => (affect!, [], [], [], nothing) - @named jsys = JumpSystem([maj, crj, vrj, eqs[1]], t, [X, Y], [α, β]; - continuous_events = cevents) + cevents = [t ~ 0.2] => (affect!, [], [], [], nothing) + @named jsys = JumpSystem([maj, crj, vrj, eqs[1]], t, [X, Y], [α, β]; + continuous_events = cevents) jsys = complete(jsys) tspan = (0.0, 200.0) oprob = ODEProblem(jsys, u0map, tspan, pmap) @@ -516,8 +523,8 @@ let for n in 1:Nsims sol = solve(jprob, Tsit5(), saveat = tspan[2]) @test sol.retcode == ReturnCode.Terminated - Xsamp += sol[1,end] + Xsamp += sol[1, end] end Xsamp /= Nsims - @test abs(Xsamp - Xf(.2,p) < .05 * Xf(.2,p)) -end \ No newline at end of file + @test abs(Xsamp - Xf(0.2, p) < 0.05 * Xf(0.2, p)) +end From efee2f9207cb5bbf808e7a3e3370ca45b85029e1 Mon Sep 17 00:00:00 2001 From: Sam Isaacson Date: Mon, 11 Nov 2024 17:23:43 -0500 Subject: [PATCH 0330/2176] try tweaking test --- test/jumpsystem.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/jumpsystem.jl b/test/jumpsystem.jl index d27a54598b..adeb751899 100644 --- a/test/jumpsystem.jl +++ b/test/jumpsystem.jl @@ -434,7 +434,7 @@ let @named jsys = JumpSystem([vrj1, vrj2, eqs[1], eqs[2]], t, [X, Y], [k1, k2]) jsys = complete(jsys) X0 = 0.0 - Y0 = 0.0 + Y0 = 3.0 u0 = [X => X0, Y => Y0] k1val = 1.0 k2val = 20.0 From ddf0612b2815d4d179b3bff50f8930794e01589f Mon Sep 17 00:00:00 2001 From: Sam Isaacson Date: Mon, 11 Nov 2024 17:33:11 -0500 Subject: [PATCH 0331/2176] seed default rng too --- test/jumpsystem.jl | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/jumpsystem.jl b/test/jumpsystem.jl index adeb751899..b187a7a7ae 100644 --- a/test/jumpsystem.jl +++ b/test/jumpsystem.jl @@ -426,6 +426,7 @@ end # PDMP test let Random.seed!(rng, 1111) + Random.seed!(Random.default_rng(), 1111) @variables X(t) Y(t) @parameters k1 k2 vrj1 = VariableRateJump(k1 * X, [X ~ X - 1]; save_positions = (false, false)) @@ -466,6 +467,7 @@ end # that mixes ODEs and jump types, and then contin events let Random.seed!(rng, 1111) + Random.seed!(Random.default_rng(), 1111) @variables X(t) Y(t) @parameters α β vrj = VariableRateJump(β * X, [X ~ X - 1]; save_positions = (false, false)) From 08decab292d1bb596d70c42d618f43cdf91cc586 Mon Sep 17 00:00:00 2001 From: Sam Isaacson Date: Mon, 11 Nov 2024 20:44:26 -0500 Subject: [PATCH 0332/2176] try to make tests deterministic --- test/jumpsystem.jl | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/test/jumpsystem.jl b/test/jumpsystem.jl index b187a7a7ae..c63d4e3d15 100644 --- a/test/jumpsystem.jl +++ b/test/jumpsystem.jl @@ -425,8 +425,8 @@ end # PDMP test let - Random.seed!(rng, 1111) - Random.seed!(Random.default_rng(), 1111) + seed = 1111 + Random.seed!(rng, seed) @variables X(t) Y(t) @parameters k1 k2 vrj1 = VariableRateJump(k1 * X, [X ~ X - 1]; save_positions = (false, false)) @@ -449,9 +449,10 @@ let Xv = zeros(length(times)) Yv = zeros(length(times)) for n in 1:Nsims - sol = solve(jprob, Tsit5(); saveat = times) + sol = solve(jprob, Tsit5(); saveat = times, seed) Xv .+= sol[1, :] Yv .+= sol[2, :] + seed += 1 end Xv ./= Nsims Yv ./= Nsims @@ -466,8 +467,8 @@ end # that mixes ODEs and jump types, and then contin events let - Random.seed!(rng, 1111) - Random.seed!(Random.default_rng(), 1111) + seed = 1111 + Random.seed!(rng, seed) @variables X(t) Y(t) @parameters α β vrj = VariableRateJump(β * X, [X ~ X - 1]; save_positions = (false, false)) @@ -487,9 +488,10 @@ let Xv = zeros(length(times)) Yv = zeros(length(times)) for n in 1:Nsims - sol = solve(jprob, Tsit5(); saveat = times) + sol = solve(jprob, Tsit5(); saveat = times, seed) Xv .+= sol[1, :] Yv .+= sol[2, :] + seed += 1 end Xv ./= Nsims Yv ./= Nsims @@ -523,9 +525,10 @@ let Xsamp = 0.0 Nsims = 4000 for n in 1:Nsims - sol = solve(jprob, Tsit5(), saveat = tspan[2]) + sol = solve(jprob, Tsit5(), saveat = tspan[2], seed) @test sol.retcode == ReturnCode.Terminated Xsamp += sol[1, end] + seed += 1 end Xsamp /= Nsims @test abs(Xsamp - Xf(0.2, p) < 0.05 * Xf(0.2, p)) From 456eb885a6b04dceef9e4059587a298595db3128 Mon Sep 17 00:00:00 2001 From: Herman Sletmoen Date: Tue, 12 Nov 2024 11:27:17 +0100 Subject: [PATCH 0333/2176] Update perturbation.md --- docs/src/examples/perturbation.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/docs/src/examples/perturbation.md b/docs/src/examples/perturbation.md index cd1e843f55..407248709d 100644 --- a/docs/src/examples/perturbation.md +++ b/docs/src/examples/perturbation.md @@ -39,7 +39,8 @@ eqs_pert = taylor_coeff(eq_pert, ϵ, 0:2) !!! note The 0-th order equation can be solved analytically, but ModelingToolkit does currently not feature automatic analytical solution of ODEs, so we proceed with solving it numerically. - These are the ODEs we want to solve. Now construct an `ODESystem`, which automatically inserts dummy derivatives for the velocities: + +These are the ODEs we want to solve. Now construct an `ODESystem`, which automatically inserts dummy derivatives for the velocities: ```@example perturbation @mtkbuild sys = ODESystem(eqs_pert, t) From 0ec1fa39e15ae81c569ee2c93e7731ffe2825265 Mon Sep 17 00:00:00 2001 From: Sam Isaacson Date: Tue, 12 Nov 2024 05:39:01 -0500 Subject: [PATCH 0334/2176] Update test/jumpsystem.jl --- test/jumpsystem.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/jumpsystem.jl b/test/jumpsystem.jl index c63d4e3d15..0fd6dc4af0 100644 --- a/test/jumpsystem.jl +++ b/test/jumpsystem.jl @@ -525,7 +525,7 @@ let Xsamp = 0.0 Nsims = 4000 for n in 1:Nsims - sol = solve(jprob, Tsit5(), saveat = tspan[2], seed) + sol = solve(jprob, Tsit5(); saveat = tspan[2], seed) @test sol.retcode == ReturnCode.Terminated Xsamp += sol[1, end] seed += 1 From 3bc5d236053cebf4b08315f64379c3666d5c9e84 Mon Sep 17 00:00:00 2001 From: Benjamin Chung Date: Tue, 12 Nov 2024 16:29:47 -0800 Subject: [PATCH 0335/2176] Fix merge issues --- src/systems/callbacks.jl | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/src/systems/callbacks.jl b/src/systems/callbacks.jl index c2c895626c..fab573cffb 100644 --- a/src/systems/callbacks.jl +++ b/src/systems/callbacks.jl @@ -247,8 +247,6 @@ struct SymbolicContinuousCallback initialize = NULL_AFFECT, finalize = NULL_AFFECT, rootfind = SciMLBase.LeftRootFind, - initialize = NULL_AFFECT, - finalize = NULL_AFFECT, reinitializealg = SciMLBase.CheckInit()) new(eqs, initialize, finalize, make_affect(affect), make_affect(affect_neg), rootfind, reinitializealg) @@ -387,16 +385,6 @@ function affect_negs(cbs::Vector{SymbolicContinuousCallback}) mapreduce(affect_negs, vcat, cbs, init = Equation[]) end -initialize_affects(cb::SymbolicContinuousCallback) = cb.initialize -function initialize_affects(cbs::Vector{SymbolicContinuousCallback}) - mapreduce(initialize_affects, vcat, cbs, init = Equation[]) -end - -finalize_affects(cb::SymbolicContinuousCallback) = cb.initialize -function finalize_affects(cbs::Vector{SymbolicContinuousCallback}) - mapreduce(finalize_affects, vcat, cbs, init = Equation[]) -end - reinitialization_alg(cb::SymbolicContinuousCallback) = cb.reinitializealg function reinitialization_algs(cbs::Vector{SymbolicContinuousCallback}) mapreduce( From e69ef194e685fb94016541255030b75764acbbb5 Mon Sep 17 00:00:00 2001 From: Benjamin Chung Date: Tue, 12 Nov 2024 23:45:49 -0800 Subject: [PATCH 0336/2176] Clean up usage of custom init --- src/systems/callbacks.jl | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/src/systems/callbacks.jl b/src/systems/callbacks.jl index fab573cffb..9c8187f6b7 100644 --- a/src/systems/callbacks.jl +++ b/src/systems/callbacks.jl @@ -922,18 +922,11 @@ function generate_vector_rootfinding_callback( (cb, fn) -> begin if (save_idxs = get(ic.callback_to_clocks, cb, nothing)) !== nothing let save_idxs = save_idxs - if !isnothing(fn.initialize) - (i) -> begin - fn.initialize(i) - for idx in save_idxs - SciMLBase.save_discretes!(i, idx) - end - end - else - (i) -> begin - for idx in save_idxs - SciMLBase.save_discretes!(i, idx) - end + custom_init = fn.initialize + (i) -> begin + isnothing(custom_init) && custom_init(i) + for idx in save_idxs + SciMLBase.save_discretes!(i, idx) end end end From 3c6b37ed3852d3590afe9b29f9690b0824f84a09 Mon Sep 17 00:00:00 2001 From: Benjamin Chung Date: Tue, 12 Nov 2024 23:46:52 -0800 Subject: [PATCH 0337/2176] Simplify setup function construction --- src/systems/callbacks.jl | 27 ++++++++++++--------------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/src/systems/callbacks.jl b/src/systems/callbacks.jl index 9c8187f6b7..b9423b8f07 100644 --- a/src/systems/callbacks.jl +++ b/src/systems/callbacks.jl @@ -918,24 +918,21 @@ function generate_vector_rootfinding_callback( initialize = nothing if has_index_cache(sys) && (ic = get_index_cache(sys)) !== nothing initialize = handle_optional_setup_fn( - map( - (cb, fn) -> begin - if (save_idxs = get(ic.callback_to_clocks, cb, nothing)) !== nothing - let save_idxs = save_idxs - custom_init = fn.initialize - (i) -> begin - isnothing(custom_init) && custom_init(i) - for idx in save_idxs - SciMLBase.save_discretes!(i, idx) - end + map(cbs, affect_functions) do cb, fn + if (save_idxs = get(ic.callback_to_clocks, cb, nothing)) !== nothing + let save_idxs = save_idxs + custom_init = fn.initialize + (i) -> begin + isnothing(custom_init) && custom_init(i) + for idx in save_idxs + SciMLBase.save_discretes!(i, idx) end end - else - fn.initialize end - end, - cbs, - affect_functions), + else + fn.initialize + end + end, SciMLBase.INITIALIZE_DEFAULT) else From 8edc6b0927737608d7d510e6ce0bd99ed2a153c3 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 13 Nov 2024 14:07:55 +0530 Subject: [PATCH 0338/2176] feat: support directly generating observed functions for tuples --- Project.toml | 2 +- src/systems/abstractsystem.jl | 5 ++++- src/systems/diffeqs/odesystem.jl | 13 ++++++++++++- test/symbolic_indexing_interface.jl | 19 +++++++++++++++++++ 4 files changed, 36 insertions(+), 3 deletions(-) diff --git a/Project.toml b/Project.toml index 0e804e1f63..7e64764e63 100644 --- a/Project.toml +++ b/Project.toml @@ -131,7 +131,7 @@ SimpleNonlinearSolve = "0.1.0, 1, 2" SparseArrays = "1" SpecialFunctions = "0.7, 0.8, 0.9, 0.10, 1.0, 2" StaticArrays = "0.10, 0.11, 0.12, 1.0" -SymbolicIndexingInterface = "0.3.31" +SymbolicIndexingInterface = "0.3.35" SymbolicUtils = "3.7" Symbolics = "6.15.4" URIs = "1" diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index e715dc6eea..44681e1db6 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -818,6 +818,8 @@ function SymbolicIndexingInterface.is_observed(sys::AbstractSystem, sym) !is_independent_variable(sys, sym) && symbolic_type(sym) != NotSymbolic() end +SymbolicIndexingInterface.supports_tuple_observed(::AbstractSystem) = true + function SymbolicIndexingInterface.observed( sys::AbstractSystem, sym; eval_expression = false, eval_module = @__MODULE__) if has_index_cache(sys) && (ic = get_index_cache(sys)) !== nothing @@ -827,7 +829,8 @@ function SymbolicIndexingInterface.observed( throw(ArgumentError("Symbol $sym does not exist in the system")) end sym = _sym - elseif sym isa AbstractArray && symbolic_type(sym) isa NotSymbolic && + elseif (sym isa Tuple || + (sym isa AbstractArray && symbolic_type(sym) isa NotSymbolic)) && any(x -> x isa Symbol, sym) sym = map(sym) do s if s isa Symbol diff --git a/src/systems/diffeqs/odesystem.jl b/src/systems/diffeqs/odesystem.jl index 4d37339518..29423baf5e 100644 --- a/src/systems/diffeqs/odesystem.jl +++ b/src/systems/diffeqs/odesystem.jl @@ -427,6 +427,10 @@ function build_explicit_observed_function(sys, ts; param_only = false, op = Operator, throw = true) + is_tuple = ts isa Tuple + if is_tuple + ts = collect(ts) + end if (isscalar = symbolic_type(ts) !== NotSymbolic()) ts = [ts] end @@ -573,9 +577,16 @@ function build_explicit_observed_function(sys, ts; # Need to keep old method of building the function since it uses `output_type`, # which can't be provided to `build_function` + return_value = if isscalar + ts[1] + elseif is_tuple + MakeTuple(Tuple(ts)) + else + MakeArray(ts, output_type) + end oop_fn = Func(args, [], pre(Let(obsexprs, - isscalar ? ts[1] : MakeArray(ts, output_type), + return_value, false))) |> array_wrapper[1] |> oop_mtkp_wrapper |> toexpr oop_fn = expression ? oop_fn : eval_or_rgf(oop_fn; eval_expression, eval_module) diff --git a/test/symbolic_indexing_interface.jl b/test/symbolic_indexing_interface.jl index 0f42ed7d34..161d926cb2 100644 --- a/test/symbolic_indexing_interface.jl +++ b/test/symbolic_indexing_interface.jl @@ -8,6 +8,7 @@ using SciMLStructures: Tunable eqs = [D(x) ~ a * y + t, D(y) ~ b * t] @named odesys = ODESystem(eqs, t, [x, y], [a, b]; observed = [xy ~ x + y]) odesys = complete(odesys) + @test SymbolicIndexingInterface.supports_tuple_observed(odesys) @test all(is_variable.((odesys,), [x, y, 1, 2, :x, :y])) @test all(.!is_variable.((odesys,), [a, b, t, 3, 0, :a, :b])) @test variable_index.((odesys,), [x, y, a, b, t, 1, 2, :x, :y, :a, :b]) == @@ -33,6 +34,14 @@ using SciMLStructures: Tunable @test default_values(odesys)[y] == 2.0 @test isequal(default_values(odesys)[xy], x + y) + prob = ODEProblem(odesys, [], (0.0, 1.0), [a => 1.0, b => 2.0]) + getter = getu(odesys, (x + 1, x + 2)) + @test getter(prob) isa Tuple + @test_nowarn @inferred getter(prob) + getter = getp(odesys, (a + 1, a + 2)) + @test getter(prob) isa Tuple + @test_nowarn @inferred getter(prob) + @named odesys = ODESystem( eqs, t, [x, y], [a, b]; defaults = [xy => 3.0], observed = [xy ~ x + y]) odesys = complete(odesys) @@ -99,6 +108,7 @@ end 0 ~ x * y - β * z] @named ns = NonlinearSystem(eqs, [x, y, z], [σ, ρ, β]) ns = complete(ns) + @test SymbolicIndexingInterface.supports_tuple_observed(ns) @test !is_time_dependent(ns) ps = ModelingToolkit.MTKParameters(ns, [σ => 1.0, ρ => 2.0, β => 3.0]) pobs = parameter_observed(ns, σ + ρ) @@ -107,6 +117,15 @@ end pobs = parameter_observed(ns, [σ + ρ, ρ + β]) @test isempty(get_all_timeseries_indexes(ns, [σ + ρ, ρ + β])) @test pobs(ps) == [3.0, 5.0] + + prob = NonlinearProblem( + ns, [x => 1.0, y => 2.0, z => 3.0], [σ => 1.0, ρ => 2.0, β => 3.0]) + getter = getu(ns, (x + 1, x + 2)) + @test getter(prob) isa Tuple + @test_nowarn @inferred getter(prob) + getter = getp(ns, (σ + 1, σ + 2)) + @test getter(prob) isa Tuple + @test_nowarn @inferred getter(prob) end @testset "PDESystem" begin From 82d55dc3f38ca924a5e2870a96a53c0863072595 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 14 Nov 2024 11:48:42 +0530 Subject: [PATCH 0339/2176] fix: fix `getproperty` syntax on `structural_simplify(complete(sys))` --- src/systems/abstractsystem.jl | 4 ++-- test/components.jl | 20 ++++++++++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index 44681e1db6..f396a9830a 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -1122,7 +1122,7 @@ function Base.propertynames(sys::AbstractSystem; private = false) return fieldnames(typeof(sys)) else if has_parent(sys) && (parent = get_parent(sys); parent !== nothing) - sys = parent + return propertynames(parent; private) end names = Symbol[] for s in get_systems(sys) @@ -1144,7 +1144,7 @@ end function Base.getproperty(sys::AbstractSystem, name::Symbol; namespace = !iscomplete(sys)) if has_parent(sys) && (parent = get_parent(sys); parent !== nothing) - sys = parent + return getproperty(parent, name; namespace) end wrap(getvar(sys, name; namespace = namespace)) end diff --git a/test/components.jl b/test/components.jl index a284c9b0c8..298f9fceb9 100644 --- a/test/components.jl +++ b/test/components.jl @@ -350,3 +350,23 @@ end @test_throws ArgumentError simp.inner₊p @test_throws ArgumentError outer.inner₊p end + +@testset "`getproperty` on `structural_simplify(complete(sys))`" begin + @mtkmodel Foo begin + @variables begin + x(t) + end + end + @mtkmodel Bar begin + @components begin + foo = Foo() + end + @equations begin + D(foo.x) ~ foo.x + end + end + @named bar = Bar() + cbar = complete(bar) + ss = structural_simplify(cbar) + @test isequal(cbar.foo.x, ss.foo.x) +end From 1431f9013b0228141f3ab557faaf17176a0bc3ad Mon Sep 17 00:00:00 2001 From: Benjamin Chung Date: Tue, 12 Nov 2024 16:44:06 -0800 Subject: [PATCH 0340/2176] Document build_explicit_observed_function and allow user-defined array construction --- src/systems/diffeqs/odesystem.jl | 33 ++++++++++++++++++++++++++++---- 1 file changed, 29 insertions(+), 4 deletions(-) diff --git a/src/systems/diffeqs/odesystem.jl b/src/systems/diffeqs/odesystem.jl index 29423baf5e..ba3a4977ba 100644 --- a/src/systems/diffeqs/odesystem.jl +++ b/src/systems/diffeqs/odesystem.jl @@ -411,8 +411,32 @@ ODESystem(eq::Equation, args...; kwargs...) = ODESystem([eq], args...; kwargs... """ $(SIGNATURES) -Build the observed function assuming the observed equations are all explicit, -i.e. there are no cycles. +Generates a function that computes the observed value(s) `ts` in the system `sys` assuming that there are no cycles in the equations. + +The return value will be either: +* a single function if the input is a scalar or if the input is a Vector but `return_inplace` is false +* the out of place and in-place functions `(ip, oop)` if `return_inplace` is true and the input is a `Vector` + +The function(s) will be: +* `RuntimeGeneratedFunction`s by default, +* A Julia `Expr` if `expression` is true, +* A directly evaluated Julia function in the module `eval_module` if `eval_expression` is true + +The signatures will be of the form `g(...)` with arguments: +* `output` for in-place functions +* `unknowns` if `params_only` is `false` +* `inputs` if `inputs` is an array of symbolic inputs that should be available in `ts` +* `p...` unconditionally; note that in the case of `MTKParameters` more than one parameters argument may be present, so it must be splatted +* `t` if the system is time-dependent; for example `NonlinearSystem` will not have `t` +For example, a function `g(op, unknowns, p, inputs, t)` will be the in-place function generated if `return_inplace` is true, `ts` is a vector, an array of inputs `inputs` is given, and `params_only` is false for a time-dependent system. + +Options not otherwise specified are: +* `output_type = Array` the type of the array generated by the out-of-place vector-valued function +* `checkbounds = true` checks bounds if true when destructuring parameters +* `op = Operator` sets the recursion terminator for the walk done by `vars` to identify the variables that appear in `ts`. See the documentation for `vars` for more detail. +* `throw = true` if true, throw an error when generating a function for `ts` that reference variables that do not exist +* `drop_expr` is deprecated. +* `mkarray`; only used if the output is an array (that is, `!isscalar(ts)`). Called as `mkarray(ts, output_type)` where `ts` are the expressions to put in the array and `output_type` is the argument of the same name passed to build_explicit_observed_function. """ function build_explicit_observed_function(sys, ts; inputs = nothing, @@ -426,7 +450,8 @@ function build_explicit_observed_function(sys, ts; return_inplace = false, param_only = false, op = Operator, - throw = true) + throw = true, + mkarray = MakeArray) is_tuple = ts isa Tuple if is_tuple ts = collect(ts) @@ -582,7 +607,7 @@ function build_explicit_observed_function(sys, ts; elseif is_tuple MakeTuple(Tuple(ts)) else - MakeArray(ts, output_type) + mkarray(ts, output_type) end oop_fn = Func(args, [], pre(Let(obsexprs, From dbf06cd421c29ce21185df2cd1668952cb88fc96 Mon Sep 17 00:00:00 2001 From: Benjamin Chung Date: Thu, 14 Nov 2024 17:17:11 -0800 Subject: [PATCH 0341/2176] Remove deprecated argument to build_explicit_observed_function --- src/systems/diffeqs/odesystem.jl | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/systems/diffeqs/odesystem.jl b/src/systems/diffeqs/odesystem.jl index ba3a4977ba..1f333b5e91 100644 --- a/src/systems/diffeqs/odesystem.jl +++ b/src/systems/diffeqs/odesystem.jl @@ -445,8 +445,7 @@ function build_explicit_observed_function(sys, ts; eval_module = @__MODULE__, output_type = Array, checkbounds = true, - drop_expr = drop_expr, - ps = parameters(sys), + ps = parameters(sys), return_inplace = false, param_only = false, op = Operator, From 7b73213151436cb3c02f687a4c9d35eaaba9fa7f Mon Sep 17 00:00:00 2001 From: Benjamin Chung Date: Thu, 14 Nov 2024 17:17:29 -0800 Subject: [PATCH 0342/2176] Reformat docstring --- src/systems/diffeqs/odesystem.jl | 58 ++++++++++++++++++++------------ 1 file changed, 36 insertions(+), 22 deletions(-) diff --git a/src/systems/diffeqs/odesystem.jl b/src/systems/diffeqs/odesystem.jl index 1f333b5e91..b60220aeea 100644 --- a/src/systems/diffeqs/odesystem.jl +++ b/src/systems/diffeqs/odesystem.jl @@ -409,34 +409,48 @@ end ODESystem(eq::Equation, args...; kwargs...) = ODESystem([eq], args...; kwargs...) """ -$(SIGNATURES) + build_explicit_observed_function(sys, ts; kwargs...) -> Function(s) + +Generates a function that computes the observed value(s) `ts` in the system `sys`, while making the assumption that there are no cycles in the equations. + +## Arguments +- `sys`: The system for which to generate the function +- `ts`: The symbolic observed values whose value should be computed + +## Keywords +- `return_inplace = false`: If true and the observed value is a vector, then return both the in place and out of place methods. +- `expression = false`: Generates a Julia `Expr`` computing the observed value if `expression` is true +- `eval_expression = false`: If true and `expression = false`, evaluates the returned function in the module `eval_module` +- `output_type = Array` the type of the array generated by a out-of-place vector-valued function +- `param_only = false` if true, only allow the generated function to access system parameters +- `inputs = nothing` additinoal symbolic variables that should be provided to the generated function +- `checkbounds = true` checks bounds if true when destructuring parameters +- `op = Operator` sets the recursion terminator for the walk done by `vars` to identify the variables that appear in `ts`. See the documentation for `vars` for more detail. +- `throw = true` if true, throw an error when generating a function for `ts` that reference variables that do not exist. +- `mkarray`; only used if the output is an array (that is, `!isscalar(ts)`). Called as `mkarray(ts, output_type)` where `ts` are the expressions to put in +the array and `output_type` is the argument of the same name passed to build_explicit_observed_function. + +## Returns -Generates a function that computes the observed value(s) `ts` in the system `sys` assuming that there are no cycles in the equations. - The return value will be either: -* a single function if the input is a scalar or if the input is a Vector but `return_inplace` is false -* the out of place and in-place functions `(ip, oop)` if `return_inplace` is true and the input is a `Vector` +* a single function `f_oop` if the input is a scalar or if the input is a Vector but `return_inplace` is false +* the out of place and in-place functions `(f_ip, f_oop)` if `return_inplace` is true and the input is a `Vector` -The function(s) will be: +The function(s) `f_oop` (and potentially `f_ip`) will be: * `RuntimeGeneratedFunction`s by default, * A Julia `Expr` if `expression` is true, -* A directly evaluated Julia function in the module `eval_module` if `eval_expression` is true +* A directly evaluated Julia function in the module `eval_module` if `eval_expression` is true and `expression` is false. The signatures will be of the form `g(...)` with arguments: -* `output` for in-place functions -* `unknowns` if `params_only` is `false` -* `inputs` if `inputs` is an array of symbolic inputs that should be available in `ts` -* `p...` unconditionally; note that in the case of `MTKParameters` more than one parameters argument may be present, so it must be splatted -* `t` if the system is time-dependent; for example `NonlinearSystem` will not have `t` -For example, a function `g(op, unknowns, p, inputs, t)` will be the in-place function generated if `return_inplace` is true, `ts` is a vector, an array of inputs `inputs` is given, and `params_only` is false for a time-dependent system. - -Options not otherwise specified are: -* `output_type = Array` the type of the array generated by the out-of-place vector-valued function -* `checkbounds = true` checks bounds if true when destructuring parameters -* `op = Operator` sets the recursion terminator for the walk done by `vars` to identify the variables that appear in `ts`. See the documentation for `vars` for more detail. -* `throw = true` if true, throw an error when generating a function for `ts` that reference variables that do not exist -* `drop_expr` is deprecated. -* `mkarray`; only used if the output is an array (that is, `!isscalar(ts)`). Called as `mkarray(ts, output_type)` where `ts` are the expressions to put in the array and `output_type` is the argument of the same name passed to build_explicit_observed_function. + +- `output` for in-place functions +- `unknowns` if `param_only` is `false` +- `inputs` if `inputs` is an array of symbolic inputs that should be available in `ts` +- `p...` unconditionally; note that in the case of `MTKParameters` more than one parameters argument may be present, so it must be splatted +- `t` if the system is time-dependent; for example `NonlinearSystem` will not have `t` + +For example, a function `g(op, unknowns, p..., inputs, t)` will be the in-place function generated if `return_inplace` is true, `ts` is a vector, +an array of inputs `inputs` is given, and `param_only` is false for a time-dependent system. """ function build_explicit_observed_function(sys, ts; inputs = nothing, @@ -445,7 +459,7 @@ function build_explicit_observed_function(sys, ts; eval_module = @__MODULE__, output_type = Array, checkbounds = true, - ps = parameters(sys), + ps = parameters(sys), return_inplace = false, param_only = false, op = Operator, From 14996d7db8065c3716e8ad54333bbe5cbbf7af45 Mon Sep 17 00:00:00 2001 From: Benjamin Chung Date: Thu, 14 Nov 2024 17:59:02 -0800 Subject: [PATCH 0343/2176] Fix docstring to account for tuples --- src/systems/diffeqs/odesystem.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/systems/diffeqs/odesystem.jl b/src/systems/diffeqs/odesystem.jl index b60220aeea..5ad275906d 100644 --- a/src/systems/diffeqs/odesystem.jl +++ b/src/systems/diffeqs/odesystem.jl @@ -427,7 +427,7 @@ Generates a function that computes the observed value(s) `ts` in the system `sys - `checkbounds = true` checks bounds if true when destructuring parameters - `op = Operator` sets the recursion terminator for the walk done by `vars` to identify the variables that appear in `ts`. See the documentation for `vars` for more detail. - `throw = true` if true, throw an error when generating a function for `ts` that reference variables that do not exist. -- `mkarray`; only used if the output is an array (that is, `!isscalar(ts)`). Called as `mkarray(ts, output_type)` where `ts` are the expressions to put in +- `mkarray`; only used if the output is an array (that is, `!isscalar(ts)` and `ts` is not a tuple, in which case the result will always be a tuple). Called as `mkarray(ts, output_type)` where `ts` are the expressions to put in the array and `output_type` is the argument of the same name passed to build_explicit_observed_function. ## Returns From 1e9181ff128dde4faf9f1915cc1d6434d062d2ae Mon Sep 17 00:00:00 2001 From: Benjamin Chung Date: Thu, 14 Nov 2024 20:24:59 -0800 Subject: [PATCH 0344/2176] Update input_output_handling.jl to remove use of drop_expr --- test/input_output_handling.jl | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/test/input_output_handling.jl b/test/input_output_handling.jl index 07a26d2d1a..c3a0a63f6c 100644 --- a/test/input_output_handling.jl +++ b/test/input_output_handling.jl @@ -140,8 +140,7 @@ if VERSION >= v"1.8" # :opaque_closure not supported before A, B, C, D = matrices obsf = ModelingToolkit.build_explicit_observed_function(ssys, [y], - inputs = [torque.tau.u], - drop_expr = identity) + inputs = [torque.tau.u]) x = randn(size(A, 1)) u = randn(size(B, 2)) p = (getindex.( From eaf72ac5505851839f37112326cb80f8206fc28d Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 15 Nov 2024 11:54:58 +0530 Subject: [PATCH 0345/2176] test: remove use of deprecated kwarg --- test/input_output_handling.jl | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/test/input_output_handling.jl b/test/input_output_handling.jl index 07a26d2d1a..c3a0a63f6c 100644 --- a/test/input_output_handling.jl +++ b/test/input_output_handling.jl @@ -140,8 +140,7 @@ if VERSION >= v"1.8" # :opaque_closure not supported before A, B, C, D = matrices obsf = ModelingToolkit.build_explicit_observed_function(ssys, [y], - inputs = [torque.tau.u], - drop_expr = identity) + inputs = [torque.tau.u]) x = randn(size(A, 1)) u = randn(size(B, 2)) p = (getindex.( From 3bd65e095447058aaac756fb81312408c5caa84e Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 15 Nov 2024 12:58:40 +0530 Subject: [PATCH 0346/2176] fix: handle observed in `remake_initializeprob` --- src/systems/nonlinear/initializesystem.jl | 16 +++++- test/initializationsystem.jl | 70 +++++++++++++++++++++++ 2 files changed, 83 insertions(+), 3 deletions(-) diff --git a/src/systems/nonlinear/initializesystem.jl b/src/systems/nonlinear/initializesystem.jl index eefe393acc..eff19afb07 100644 --- a/src/systems/nonlinear/initializesystem.jl +++ b/src/systems/nonlinear/initializesystem.jl @@ -225,7 +225,7 @@ function SciMLBase.remake_initializeprob(sys::ODESystem, odefn, u0, t0, p) u0idxs = Int[] u0vals = [] for sym in variable_symbols(oldinitprob) - if is_variable(sys, sym) + if is_variable(sys, sym) || has_observed_with_lhs(sys, sym) u0 !== missing || continue idx = variable_index(oldinitprob, sym) push!(u0idxs, idx) @@ -247,8 +247,18 @@ function SciMLBase.remake_initializeprob(sys::ODESystem, odefn, u0, t0, p) end end end - newu0 = remake_buffer(oldinitprob.f.sys, state_values(oldinitprob), u0idxs, u0vals) - newp = remake_buffer(oldinitprob.f.sys, parameter_values(oldinitprob), pidxs, pvals) + if isempty(u0idxs) + newu0 = state_values(oldinitprob) + else + newu0 = remake_buffer( + oldinitprob.f.sys, state_values(oldinitprob), u0idxs, u0vals) + end + if isempty(pidxs) + newp = parameter_values(oldinitprob) + else + newp = remake_buffer( + oldinitprob.f.sys, parameter_values(oldinitprob), pidxs, pvals) + end initprob = remake(oldinitprob; u0 = newu0, p = newp) return initprob, odefn.update_initializeprob!, odefn.initializeprobmap, odefn.initializeprobpmap diff --git a/test/initializationsystem.jl b/test/initializationsystem.jl index c9783c9659..f39ec3c2f2 100644 --- a/test/initializationsystem.jl +++ b/test/initializationsystem.jl @@ -877,3 +877,73 @@ end sol = solve(prob, Rodas5P()) @test SciMLBase.successful_retcode(sol) end + +@testset "Issue#3205" begin + using ModelingToolkitStandardLibrary.Electrical + import ModelingToolkitStandardLibrary.Mechanical.Rotational as MR + using ModelingToolkitStandardLibrary.Blocks + using SciMLBase + + function dc_motor(R1 = 0.5) + R = R1 # [Ohm] armature resistance + L = 4.5e-3 # [H] armature inductance + k = 0.5 # [N.m/A] motor constant + J = 0.02 # [kg.m²] inertia + f = 0.01 # [N.m.s/rad] friction factor + tau_L_step = -0.3 # [N.m] amplitude of the load torque step + + @named ground = Ground() + @named source = Voltage() + @named ref = Blocks.Step(height = 0.2, start_time = 0) + @named pi_controller = Blocks.LimPI(k = 1.1, T = 0.035, u_max = 10, Ta = 0.035) + @named feedback = Blocks.Feedback() + @named R1 = Resistor(R = R) + @named L1 = Inductor(L = L) + @named emf = EMF(k = k) + @named fixed = MR.Fixed() + @named load = MR.Torque() + @named load_step = Blocks.Step(height = tau_L_step, start_time = 3) + @named inertia = MR.Inertia(J = J) + @named friction = MR.Damper(d = f) + @named speed_sensor = MR.SpeedSensor() + + connections = [connect(fixed.flange, emf.support, friction.flange_b) + connect(emf.flange, friction.flange_a, inertia.flange_a) + connect(inertia.flange_b, load.flange) + connect(inertia.flange_b, speed_sensor.flange) + connect(load_step.output, load.tau) + connect(ref.output, feedback.input1) + connect(speed_sensor.w, :y, feedback.input2) + connect(feedback.output, pi_controller.err_input) + connect(pi_controller.ctr_output, :u, source.V) + connect(source.p, R1.p) + connect(R1.n, L1.p) + connect(L1.n, emf.p) + connect(emf.n, source.n, ground.g)] + + @named model = ODESystem(connections, t, + systems = [ + ground, + ref, + pi_controller, + feedback, + source, + R1, + L1, + emf, + fixed, + load, + load_step, + inertia, + friction, + speed_sensor + ]) + end + + model = dc_motor() + sys = structural_simplify(model) + + prob = ODEProblem(sys, [sys.L1.i => 0.0], (0, 6.0)) + + @test_nowarn remake(prob, p = prob.p) +end From 894c135f797acb8384408bc3e4b9b23231adef90 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 8 Oct 2024 15:14:04 +0530 Subject: [PATCH 0347/2176] fix: detect observed variables and dependent parameters dependent on discrete parameters --- src/systems/abstractsystem.jl | 13 ++++-- src/systems/diffeqs/odesystem.jl | 11 ++++- src/systems/index_cache.jl | 76 ++++++++++++++++++++------------ test/odesystem.jl | 11 +++++ 4 files changed, 77 insertions(+), 34 deletions(-) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index f396a9830a..15fc54e9e9 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -731,7 +731,7 @@ end function has_observed_with_lhs(sys, sym) has_observed(sys) || return false if has_index_cache(sys) && (ic = get_index_cache(sys)) !== nothing - return any(isequal(sym), ic.observed_syms) + return haskey(ic.observed_syms_to_timeseries, sym) else return any(isequal(sym), [eq.lhs for eq in observed(sys)]) end @@ -740,7 +740,7 @@ end function has_parameter_dependency_with_lhs(sys, sym) has_parameter_dependencies(sys) || return false if has_index_cache(sys) && (ic = get_index_cache(sys)) !== nothing - return any(isequal(sym), ic.dependent_pars) + return haskey(ic.dependent_pars_to_timeseries, unwrap(sym)) else return any(isequal(sym), [eq.lhs for eq in parameter_dependencies(sys)]) end @@ -762,11 +762,16 @@ for traitT in [ allsyms = vars(sym; op = Symbolics.Operator) for s in allsyms s = unwrap(s) - if is_variable(sys, s) || is_independent_variable(sys, s) || - has_observed_with_lhs(sys, s) + if is_variable(sys, s) || is_independent_variable(sys, s) push!(ts_idxs, ContinuousTimeseries()) elseif is_timeseries_parameter(sys, s) push!(ts_idxs, timeseries_parameter_index(sys, s).timeseries_idx) + elseif has_index_cache(sys) && (ic = get_index_cache(sys)) !== nothing + if (ts = get(ic.observed_syms_to_timeseries, s, nothing)) !== nothing + union!(ts_idxs, ts) + elseif (ts = get(ic.dependent_pars_to_timeseries, s, nothing)) !== nothing + union!(ts_idxs, ts) + end end end end diff --git a/src/systems/diffeqs/odesystem.jl b/src/systems/diffeqs/odesystem.jl index 5ad275906d..b8dee2bac7 100644 --- a/src/systems/diffeqs/odesystem.jl +++ b/src/systems/diffeqs/odesystem.jl @@ -490,7 +490,16 @@ function build_explicit_observed_function(sys, ts; ivs = independent_variables(sys) dep_vars = scalarize(setdiff(vars, ivs)) - obs = param_only ? Equation[] : observed(sys) + obs = observed(sys) + if param_only + if has_index_cache(sys) && (ic = get_index_cache(sys)) !== nothing + obs = filter(obs) do eq + !(ContinuousTimeseries() in ic.observed_syms_to_timeseries[eq.lhs]) + end + else + obs = Equation[] + end + end cs = collect_constants(obs) if !isempty(cs) > 0 diff --git a/src/systems/index_cache.jl b/src/systems/index_cache.jl index 00f7837407..7a376aa1f2 100644 --- a/src/systems/index_cache.jl +++ b/src/systems/index_cache.jl @@ -38,6 +38,7 @@ const UnknownIndexMap = Dict{ BasicSymbolic, Union{Int, UnitRange{Int}, AbstractArray{Int}}} const TunableIndexMap = Dict{BasicSymbolic, Union{Int, UnitRange{Int}, Base.ReshapedArray{Int, N, UnitRange{Int}} where {N}}} +const TimeseriesSetType = Set{Union{ContinuousTimeseries, Int}} struct IndexCache unknown_idx::UnknownIndexMap @@ -48,8 +49,9 @@ struct IndexCache tunable_idx::TunableIndexMap constant_idx::ParamIndexMap nonnumeric_idx::NonnumericMap - observed_syms::Set{BasicSymbolic} - dependent_pars::Set{Union{BasicSymbolic, CallWithMetadata}} + observed_syms_to_timeseries::Dict{BasicSymbolic, TimeseriesSetType} + dependent_pars_to_timeseries::Dict{ + Union{BasicSymbolic, CallWithMetadata}, TimeseriesSetType} discrete_buffer_sizes::Vector{Vector{BufferTemplate}} tunable_buffer_size::BufferTemplate constant_buffer_sizes::Vector{BufferTemplate} @@ -91,20 +93,6 @@ function IndexCache(sys::AbstractSystem) end end - observed_syms = Set{BasicSymbolic}() - for eq in observed(sys) - if symbolic_type(eq.lhs) != NotSymbolic() - sym = eq.lhs - ttsym = default_toterm(sym) - rsym = renamespace(sys, sym) - rttsym = renamespace(sys, ttsym) - push!(observed_syms, sym) - push!(observed_syms, ttsym) - push!(observed_syms, rsym) - push!(observed_syms, rttsym) - end - end - tunable_buffers = Dict{Any, Set{BasicSymbolic}}() constant_buffers = Dict{Any, Set{BasicSymbolic}}() nonnumeric_buffers = Dict{Any, Set{Union{BasicSymbolic, CallWithMetadata}}}() @@ -267,29 +255,59 @@ function IndexCache(sys::AbstractSystem) end end - for sym in Iterators.flatten((keys(unk_idxs), keys(disc_idxs), keys(tunable_idxs), - keys(const_idxs), keys(nonnumeric_idxs), - observed_syms, independent_variable_symbols(sys))) - if hasname(sym) && (!iscall(sym) || operation(sym) !== getindex) - symbol_to_variable[getname(sym)] = sym - end - end - - dependent_pars = Set{Union{BasicSymbolic, CallWithMetadata}}() + dependent_pars_to_timeseries = Dict{ + Union{BasicSymbolic, CallWithMetadata}, TimeseriesSetType}() for eq in parameter_dependencies(sys) sym = eq.lhs + vs = vars(eq.rhs) + timeseries = TimeseriesSetType() + for v in vs + if (idx = get(disc_idxs, v, nothing)) !== nothing + push!(timeseries, idx.clock_idx) + end + end ttsym = default_toterm(sym) rsym = renamespace(sys, sym) rttsym = renamespace(sys, ttsym) - for s in [sym, ttsym, rsym, rttsym] - push!(dependent_pars, s) + for s in (sym, ttsym, rsym, rttsym) + dependent_pars_to_timeseries[s] = timeseries if hasname(s) && (!iscall(s) || operation(s) != getindex) symbol_to_variable[getname(s)] = sym end end end + observed_syms_to_timeseries = Dict{BasicSymbolic, TimeseriesSetType}() + for eq in observed(sys) + if symbolic_type(eq.lhs) != NotSymbolic() + sym = eq.lhs + vs = vars(eq.rhs) + timeseries = TimeseriesSetType() + for v in vs + if (idx = get(disc_idxs, v, nothing)) !== nothing + push!(timeseries, idx.clock_idx) + elseif haskey(unk_idxs, v) || haskey(observed_syms_to_timeseries, v) + push!(timeseries, ContinuousTimeseries()) + end + end + ttsym = default_toterm(sym) + rsym = renamespace(sys, sym) + rttsym = renamespace(sys, ttsym) + for s in (sym, ttsym, rsym, rttsym) + observed_syms_to_timeseries[s] = timeseries + end + end + end + + for sym in Iterators.flatten((keys(unk_idxs), keys(disc_idxs), keys(tunable_idxs), + keys(const_idxs), keys(nonnumeric_idxs), + keys(observed_syms_to_timeseries), independent_variable_symbols(sys))) + if hasname(sym) && (!iscall(sym) || operation(sym) !== getindex) + symbol_to_variable[getname(sym)] = sym + end + end + return IndexCache( unk_idxs, disc_idxs, @@ -297,8 +315,8 @@ function IndexCache(sys::AbstractSystem) tunable_idxs, const_idxs, nonnumeric_idxs, - observed_syms, - dependent_pars, + observed_syms_to_timeseries, + dependent_pars_to_timeseries, disc_buffer_templates, BufferTemplate(Real, tunable_buffer_size), const_buffer_sizes, diff --git a/test/odesystem.jl b/test/odesystem.jl index d7a2658f63..fb185fdcb0 100644 --- a/test/odesystem.jl +++ b/test/odesystem.jl @@ -1504,3 +1504,14 @@ end sys2 = complete(sys; split = false) @test ModelingToolkit.get_index_cache(sys2) === nothing end + +# https://github.com/SciML/SciMLBase.jl/issues/786 +@testset "Observed variables dependent on discrete parameters" begin + @variables x(t) obs(t) + @parameters c(t) + @mtkbuild sys = ODESystem( + [D(x) ~ c * cos(x), obs ~ c], t, [x], [c]; discrete_events = [1.0 => [c ~ c + 1]]) + prob = ODEProblem(sys, [x => 0.0], (0.0, 2pi), [c => 1.0]) + sol = solve(prob, Tsit5()) + @test sol[obs] ≈ 1:7 +end From bcd0edb1de1790c63200cd6f4229ebf0c183fdac Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 28 Oct 2024 18:04:21 +0530 Subject: [PATCH 0348/2176] fix: fix timeseries detection for shifted variables in DDEs --- src/systems/abstractsystem.jl | 4 ++++ src/systems/index_cache.jl | 27 +++++++++++++++++++-------- 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index 15fc54e9e9..db2fcb4f10 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -766,6 +766,10 @@ for traitT in [ push!(ts_idxs, ContinuousTimeseries()) elseif is_timeseries_parameter(sys, s) push!(ts_idxs, timeseries_parameter_index(sys, s).timeseries_idx) + elseif is_time_dependent(sys) && iscall(s) && issym(operation(s)) && + is_variable(sys, operation(s)(get_iv(sys))) + # DDEs case, to detect x(t - k) + push!(ts_idxs, ContinuousTimeseries()) elseif has_index_cache(sys) && (ic = get_index_cache(sys)) !== nothing if (ts = get(ic.observed_syms_to_timeseries, s, nothing)) !== nothing union!(ts_idxs, ts) diff --git a/src/systems/index_cache.jl b/src/systems/index_cache.jl index 7a376aa1f2..307ac71a56 100644 --- a/src/systems/index_cache.jl +++ b/src/systems/index_cache.jl @@ -262,9 +262,11 @@ function IndexCache(sys::AbstractSystem) sym = eq.lhs vs = vars(eq.rhs) timeseries = TimeseriesSetType() - for v in vs - if (idx = get(disc_idxs, v, nothing)) !== nothing - push!(timeseries, idx.clock_idx) + if is_time_dependent(sys) + for v in vs + if (idx = get(disc_idxs, v, nothing)) !== nothing + push!(timeseries, idx.clock_idx) + end end end ttsym = default_toterm(sym) @@ -284,11 +286,20 @@ function IndexCache(sys::AbstractSystem) sym = eq.lhs vs = vars(eq.rhs) timeseries = TimeseriesSetType() - for v in vs - if (idx = get(disc_idxs, v, nothing)) !== nothing - push!(timeseries, idx.clock_idx) - elseif haskey(unk_idxs, v) || haskey(observed_syms_to_timeseries, v) - push!(timeseries, ContinuousTimeseries()) + if is_time_dependent(sys) + for v in vs + if (idx = get(disc_idxs, v, nothing)) !== nothing + push!(timeseries, idx.clock_idx) + elseif haskey(unk_idxs, v) + push!(timeseries, ContinuousTimeseries()) + elseif haskey(observed_syms_to_timeseries, v) + union!(timeseries, observed_syms_to_timeseries[v]) + elseif haskey(dependent_pars_to_timeseries, v) + union!(timeseries, dependent_pars_to_timeseries[v]) + elseif iscall(v) && issym(operation(v)) && + is_variable(sys, operation(v)(get_iv(sys))) + push!(timeseries, ContinuousTimeseries()) + end end end ttsym = default_toterm(sym) From 87c16598cf244f55c5ea4d483ae6ec553ef7da87 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 29 Oct 2024 15:36:54 +0530 Subject: [PATCH 0349/2176] fix: handle timeseries detection for constant observed --- src/systems/index_cache.jl | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/systems/index_cache.jl b/src/systems/index_cache.jl index 307ac71a56..1e3490ee69 100644 --- a/src/systems/index_cache.jl +++ b/src/systems/index_cache.jl @@ -284,23 +284,21 @@ function IndexCache(sys::AbstractSystem) for eq in observed(sys) if symbolic_type(eq.lhs) != NotSymbolic() sym = eq.lhs - vs = vars(eq.rhs) + vs = vars(eq.rhs; op = Nothing) timeseries = TimeseriesSetType() if is_time_dependent(sys) for v in vs if (idx = get(disc_idxs, v, nothing)) !== nothing push!(timeseries, idx.clock_idx) - elseif haskey(unk_idxs, v) - push!(timeseries, ContinuousTimeseries()) elseif haskey(observed_syms_to_timeseries, v) union!(timeseries, observed_syms_to_timeseries[v]) elseif haskey(dependent_pars_to_timeseries, v) union!(timeseries, dependent_pars_to_timeseries[v]) - elseif iscall(v) && issym(operation(v)) && - is_variable(sys, operation(v)(get_iv(sys))) - push!(timeseries, ContinuousTimeseries()) end end + if isempty(timeseries) + push!(timeseries, ContinuousTimeseries()) + end end ttsym = default_toterm(sym) rsym = renamespace(sys, sym) From 55c52175830b91a83d70939e95941d359b70ed03 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 15 Nov 2024 15:46:53 +0530 Subject: [PATCH 0350/2176] ci: prevent codecov from failing CI --- .github/workflows/Downstream.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/Downstream.yml b/.github/workflows/Downstream.yml index 7a7556efa3..a6d5e84656 100644 --- a/.github/workflows/Downstream.yml +++ b/.github/workflows/Downstream.yml @@ -72,4 +72,4 @@ jobs: with: file: lcov.info token: ${{ secrets.CODECOV_TOKEN }} - fail_ci_if_error: true + fail_ci_if_error: false From 085358b7573c66a8c9ba2a651a70514681eee6d0 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 14 Nov 2024 17:48:34 +0530 Subject: [PATCH 0351/2176] feat: add support for bounds of array variables --- src/variables.jl | 34 +++++++++++++++++++++++----- test/test_variable_metadata.jl | 41 ++++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+), 5 deletions(-) diff --git a/src/variables.jl b/src/variables.jl index fcfc7f9b1e..d11c6c1834 100644 --- a/src/variables.jl +++ b/src/variables.jl @@ -254,7 +254,6 @@ end ## Bounds ====================================================================== struct VariableBounds end Symbolics.option_to_metadata_type(::Val{:bounds}) = VariableBounds -getbounds(x::Num) = getbounds(Symbolics.unwrap(x)) """ getbounds(x) @@ -266,10 +265,35 @@ Create parameters with bounds like this @parameters p [bounds=(-1, 1)] ``` """ -function getbounds(x) +function getbounds(x::Union{Num, Symbolics.Arr, SymbolicUtils.Symbolic}) + x = unwrap(x) p = Symbolics.getparent(x, nothing) - p === nothing || (x = p) - Symbolics.getmetadata(x, VariableBounds, (-Inf, Inf)) + if p === nothing + bounds = Symbolics.getmetadata(x, VariableBounds, (-Inf, Inf)) + if symbolic_type(x) == ArraySymbolic() && Symbolics.shape(x) != Symbolics.Unknown() + bounds = map(bounds) do b + b isa AbstractArray && return b + return fill(b, size(x)) + end + end + else + # if we reached here, `x` is the result of calling `getindex` + bounds = @something Symbolics.getmetadata(x, VariableBounds, nothing) getbounds(p) + idxs = arguments(x)[2:end] + bounds = map(bounds) do b + if b isa AbstractArray + if Symbolics.shape(p) != Symbolics.Unknown() && size(p) != size(b) + throw(DimensionMismatch("Expected array variable $p with shape $(size(p)) to have bounds of identical size. Found $bounds of size $(size(bounds)).")) + end + return b[idxs...] + elseif symbolic_type(x) == ArraySymbolic() + return fill(b, size(x)) + else + return b + end + end + end + return bounds end """ @@ -280,7 +304,7 @@ See also [`getbounds`](@ref). """ function hasbounds(x) b = getbounds(x) - isfinite(b[1]) || isfinite(b[2]) + any(isfinite.(b[1]) .|| isfinite.(b[2])) end ## Disturbance ================================================================= diff --git a/test/test_variable_metadata.jl b/test/test_variable_metadata.jl index 7f9799edb3..7b093be366 100644 --- a/test/test_variable_metadata.jl +++ b/test/test_variable_metadata.jl @@ -10,6 +10,47 @@ using ModelingToolkit @test !hasbounds(y) @test !haskey(ModelingToolkit.dump_variable_metadata(y), :bounds) +@variables y[1:3] +@test !hasbounds(y) +@test getbounds(y)[1] == [-Inf, -Inf, -Inf] +@test getbounds(y)[2] == [Inf, Inf, Inf] +for i in eachindex(y) + @test !hasbounds(y[i]) + b = getbounds(y[i]) + @test b[1] == -Inf && b[2] == Inf +end + +@variables y[1:3] [bounds = (-1, 1)] +@test hasbounds(y) +@test getbounds(y)[1] == -ones(3) +@test getbounds(y)[2] == ones(3) +for i in eachindex(y) + @test hasbounds(y[i]) + b = getbounds(y[i]) + @test b[1] == -1.0 && b[2] == 1.0 +end +@test getbounds(y[1:2])[1] == -ones(2) +@test getbounds(y[1:2])[2] == ones(2) + +@variables y[1:2, 1:2] [bounds = (-1, [1.0 Inf; 2.0 3.0])] +@test hasbounds(y) +@test getbounds(y)[1] == [-1 -1; -1 -1] +@test getbounds(y)[2] == [1.0 Inf; 2.0 3.0] +for i in eachindex(y) + @test hasbounds(y[i]) + b = getbounds(y[i]) + @test b[1] == -1 && b[2] == [1.0 Inf; 2.0 3.0][i] +end + +@variables y[1:2] [bounds = (-Inf, [1.0, Inf])] +@test hasbounds(y) +@test getbounds(y)[1] == [-Inf, -Inf] +@test getbounds(y)[2] == [1.0, Inf] +@test hasbounds(y[1]) +@test getbounds(y[1]) == (-Inf, 1.0) +@test !hasbounds(y[2]) +@test getbounds(y[2]) == (-Inf, Inf) + # Guess @variables y [guess = 0] @test getguess(y) === 0 From 9017fbf95081ec485b32f5d44bef32ca5ff7fe86 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 15 Nov 2024 11:11:02 +0530 Subject: [PATCH 0352/2176] docs: update documentation for bounds metadata --- docs/src/basics/Variable_metadata.md | 38 ++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/docs/src/basics/Variable_metadata.md b/docs/src/basics/Variable_metadata.md index 6462c3a469..e9bdabfe38 100644 --- a/docs/src/basics/Variable_metadata.md +++ b/docs/src/basics/Variable_metadata.md @@ -86,6 +86,44 @@ hasbounds(u) getbounds(u) ``` +Bounds can also be specified for array variables. A scalar array bound is applied to each +element of the array. A bound may also be specified as an array, in which case the size of +the array must match the size of the symbolic variable. + +```@example metadata +@variables x[1:2, 1:2] [bounds = (-1, 1)] +hasbounds(x) +``` + +```@example metadata +getbounds(x) +``` + +```@example metadata +getbounds(x[1, 1]) +``` + +```@example metadata +getbounds(x[1:2, 1]) +``` + +```@example metadata +@variables x[1:2] [bounds = (-Inf, [1.0, Inf])] +hasbounds(x) +``` + +```@example metadata +getbounds(x) +``` + +```@example metadata +getbounds(x[2]) +``` + +```@example metadata +hasbounds(x[2]) +``` + ## Guess Specify an initial guess for custom initial conditions of an `ODESystem`. From 6dbc375e3d65309ba85b7cf046b92da0e202d649 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 15 Nov 2024 16:03:17 +0530 Subject: [PATCH 0353/2176] feat: respect array variable bounds in `OptimizationSystem` --- src/systems/optimization/optimizationsystem.jl | 4 ++-- test/optimizationsystem.jl | 9 +++++++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/src/systems/optimization/optimizationsystem.jl b/src/systems/optimization/optimizationsystem.jl index a0fa07be47..39c5c1e7a7 100644 --- a/src/systems/optimization/optimizationsystem.jl +++ b/src/systems/optimization/optimizationsystem.jl @@ -101,8 +101,8 @@ function OptimizationSystem(op, unknowns, ps; gui_metadata = nothing) name === nothing && throw(ArgumentError("The `name` keyword must be provided. Please consider using the `@named` macro")) - constraints = value.(scalarize(constraints)) - unknowns′ = value.(scalarize(unknowns)) + constraints = value.(reduce(vcat, scalarize(constraints); init = [])) + unknowns′ = value.(reduce(vcat, scalarize(unknowns); init = [])) ps′ = value.(ps) op′ = value(scalarize(op)) diff --git a/test/optimizationsystem.jl b/test/optimizationsystem.jl index dfa11fca37..a8e2be936d 100644 --- a/test/optimizationsystem.jl +++ b/test/optimizationsystem.jl @@ -358,4 +358,13 @@ end @mtkbuild sys = OptimizationSystem(obj, [x, y, z], []; constraints = cons) @test is_variable(sys, z) @test !is_variable(sys, y) + + @variables x[1:3] [bounds = ([-Inf, -1.0, -2.0], [Inf, 1.0, 2.0])] + obj = x[1]^2 + x[2]^2 + x[3]^2 + cons = [x[2] ~ 2x[1] + 3, x[3] ~ x[1] + x[2]] + @mtkbuild sys = OptimizationSystem(obj, [x], []; constraints = cons) + @test length(unknowns(sys)) == 2 + @test !is_variable(sys, x[1]) + @test is_variable(sys, x[2]) + @test is_variable(sys, x[3]) end From b0e4c7aeb3ac5ad6472f87af92d30bc52c7fe78f Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 15 Nov 2024 19:26:43 +0530 Subject: [PATCH 0354/2176] fix: enable using `MTKParameters` with `generate_control_function` --- src/inputoutput.jl | 10 ++++++---- test/input_output_handling.jl | 24 ++++++++++++++++++------ 2 files changed, 24 insertions(+), 10 deletions(-) diff --git a/src/inputoutput.jl b/src/inputoutput.jl index 9ed0984050..4a99ec11f5 100644 --- a/src/inputoutput.jl +++ b/src/inputoutput.jl @@ -235,18 +235,20 @@ function generate_control_function(sys::AbstractODESystem, inputs = unbound_inpu # TODO: add an optional check on the ordering of observed equations u = map(x -> time_varying_as_func(value(x), sys), dvs) p = map(x -> time_varying_as_func(value(x), sys), ps) + p = reorder_parameters(sys, p) t = get_iv(sys) # pre = has_difference ? (ex -> ex) : get_postprocess_fbody(sys) - args = (u, inputs, p, t) + args = (u, inputs, p..., t) if implicit_dae ddvs = map(Differential(get_iv(sys)), dvs) args = (ddvs, args...) end process = get_postprocess_fbody(sys) f = build_function(rhss, args...; postprocess_fbody = process, - expression = Val{true}, wrap_code = wrap_array_vars(sys, rhss; dvs, ps) .∘ + expression = Val{true}, wrap_code = wrap_mtkparameters(sys, false, 3) .∘ + wrap_array_vars(sys, rhss; dvs, ps, inputs) .∘ wrap_parameter_dependencies(sys, false), kwargs...) f = eval_or_rgf.(f; eval_expression, eval_module) @@ -426,7 +428,7 @@ function add_input_disturbance(sys, dist::DisturbanceModel, inputs = nothing; kw augmented_sys = ODESystem(eqs, t, systems = [dsys], name = gensym(:outer)) augmented_sys = extend(augmented_sys, sys) - (f_oop, f_ip), dvs, p = generate_control_function(augmented_sys, all_inputs, + (f_oop, f_ip), dvs, p, io_sys = generate_control_function(augmented_sys, all_inputs, [d]; kwargs...) - (f_oop, f_ip), augmented_sys, dvs, p + (f_oop, f_ip), augmented_sys, dvs, p, io_sys end diff --git a/test/input_output_handling.jl b/test/input_output_handling.jl index c3a0a63f6c..9550a87f31 100644 --- a/test/input_output_handling.jl +++ b/test/input_output_handling.jl @@ -160,12 +160,12 @@ eqs = [ ] @named sys = ODESystem(eqs, t) -f, dvs, ps = ModelingToolkit.generate_control_function(sys, simplify = true) +f, dvs, ps, io_sys = ModelingToolkit.generate_control_function(sys, simplify = true) @test isequal(dvs[], x) @test isempty(ps) -p = [] +p = nothing x = [rand()] u = [rand()] @test f[1](x, u, p, 1) == -x + u @@ -221,10 +221,10 @@ eqs = [connect_sd(sd, mass1, mass2) @named _model = ODESystem(eqs, t) @named model = compose(_model, mass1, mass2, sd); -f, dvs, ps = ModelingToolkit.generate_control_function(model, simplify = true) +f, dvs, ps, io_sys = ModelingToolkit.generate_control_function(model, simplify = true) @test length(dvs) == 4 @test length(ps) == length(parameters(model)) -p = ModelingToolkit.varmap_to_vars(ModelingToolkit.defaults(model), ps) +p = MTKParameters(io_sys, [io_sys.u => NaN]) x = ModelingToolkit.varmap_to_vars( merge(ModelingToolkit.defaults(model), Dict(D.(unknowns(model)) .=> 0.0)), dvs) @@ -288,7 +288,7 @@ model_outputs = [model.inertia1.w, model.inertia2.w, model.inertia1.phi, model.i @named dmodel = Blocks.StateSpace([0.0], [1.0], [1.0], [0.0]) # An integrating disturbance @named dist = ModelingToolkit.DisturbanceModel(model.torque.tau.u, dmodel) -(f_oop, f_ip), outersys, dvs, p = ModelingToolkit.add_input_disturbance(model, dist) +(f_oop, f_ip), outersys, dvs, p, io_sys = ModelingToolkit.add_input_disturbance(model, dist) @unpack u, d = outersys matrices, ssys = linearize(outersys, [u, d], model_outputs) @@ -302,7 +302,7 @@ x_add = ModelingToolkit.varmap_to_vars(merge(Dict(dvs .=> 0), Dict(dstate => 1)) x0 = randn(5) x1 = copy(x0) + x_add # add disturbance state perturbation u = randn(1) -pn = ModelingToolkit.varmap_to_vars(def, p) +pn = MTKParameters(io_sys, []) xp0 = f_oop(x0, u, pn, 0) xp1 = f_oop(x1, u, pn, 0) @@ -401,3 +401,15 @@ end f, dvs, ps = ModelingToolkit.generate_control_function(sys, simplify = true) @test f[1]([0.5], nothing, nothing, 0.0) == [1.0] end + +@testset "With callable symbolic" begin + @variables x(t)=0 u(t)=0 [input = true] + @parameters p(::Real) = (x -> 2x) + eqs = [D(x) ~ -x + p(u)] + @named sys = ODESystem(eqs, t) + f, dvs, ps, io_sys = ModelingToolkit.generate_control_function(sys, simplify = true) + p = MTKParameters(io_sys, []) + u = [1.0] + x = [1.0] + @test_nowarn f[1](x, u, p, 0.0) +end From 47e26794ecfebdf82a3f5bc05bdb1d730fed1dcb Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Sun, 17 Nov 2024 22:59:47 +0530 Subject: [PATCH 0355/2176] feat: support polynomials of invertible functions in HomotopyContinuationExt --- ext/MTKHomotopyContinuationExt.jl | 207 ++++++++++++++++++++--- src/systems/nonlinear/nonlinearsystem.jl | 8 +- test/extensions/homotopy_continuation.jl | 48 +++++- 3 files changed, 230 insertions(+), 33 deletions(-) diff --git a/ext/MTKHomotopyContinuationExt.jl b/ext/MTKHomotopyContinuationExt.jl index 19c21483c3..fa5d1bfcd4 100644 --- a/ext/MTKHomotopyContinuationExt.jl +++ b/ext/MTKHomotopyContinuationExt.jl @@ -54,16 +54,73 @@ end PolynomialData() = PolynomialData(BasicSymbolic[], NonPolynomialReason.T[], false) +abstract type PolynomialTransformationError <: Exception end + +struct MultivarTerm <: PolynomialTransformationError + term::Any + vars::Any +end + +function Base.showerror(io::IO, err::MultivarTerm) + println(io, + "Cannot convert system to polynomial: Found term $(err.term) which is a function of multiple unknowns $(err.vars).") +end + +struct MultipleTermsOfSameVar <: PolynomialTransformationError + terms::Any + var::Any +end + +function Base.showerror(io::IO, err::MultipleTermsOfSameVar) + println(io, + "Cannot convert system to polynomial: Found multiple non-polynomial terms $(err.terms) involving the same unknown $(err.var).") +end + +struct SymbolicSolveFailure <: PolynomialTransformationError + term::Any + var::Any +end + +function Base.showerror(io::IO, err::SymbolicSolveFailure) + println(io, + "Cannot convert system to polynomial: Unable to symbolically solve $(err.term) for $(err.var).") +end + +struct NemoNotLoaded <: PolynomialTransformationError end + +function Base.showerror(io::IO, err::NemoNotLoaded) + println(io, + "ModelingToolkit may be able to solve this system as a polynomial system if `Nemo` is loaded. Run `import Nemo` and try again.") +end + +struct VariablesAsPolyAndNonPoly <: PolynomialTransformationError + vars::Any +end + +function Base.showerror(io::IO, err::VariablesAsPolyAndNonPoly) + println(io, + "Cannot convert convert system to polynomial: Variables $(err.vars) occur in both polynomial and non-polynomial terms in the system.") +end + struct NotPolynomialError <: Exception - eq::Equation - data::PolynomialData + transformation_err::Union{PolynomialTransformationError, Nothing} + eq::Vector{Equation} + data::Vector{PolynomialData} end function Base.showerror(io::IO, err::NotPolynomialError) - println(io, - "Equation $(err.eq) is not a polynomial in the unknowns for the following reasons:") - for (term, reason) in zip(err.data.non_polynomial_terms, err.data.reasons) - println(io, display_reason(reason, term)) + if err.transformation_err !== nothing + Base.showerror(io, err.transformation_err) + end + for (eq, data) in zip(err.eq, err.data) + if isempty(data.non_polynomial_terms) + continue + end + println(io, + "Equation $(eq) is not a polynomial in the unknowns for the following reasons:") + for (term, reason) in zip(data.non_polynomial_terms, data.reasons) + println(io, display_reason(reason, term)) + end end end @@ -86,7 +143,9 @@ function process_polynomial!(data::PolynomialData, x, wrt) any(isequal(x), wrt) && return true if operation(x) in (*, +, -, /) - return all(y -> is_polynomial!(data, y, wrt), arguments(x)) + # `map` because `all` will early exit, but we want to search + # through everything to get all the non-polynomial terms + return all(map(y -> is_polynomial!(data, y, wrt), arguments(x))) end if operation(x) == (^) b, p = arguments(x) @@ -105,10 +164,6 @@ function process_polynomial!(data::PolynomialData, x, wrt) push!(data.reasons, NonPolynomialReason.ExponentContainsUnknowns) end base_polynomial = is_polynomial!(data, b, wrt) - if !base_polynomial - push!(data.non_polynomial_terms, x) - push!(data.reasons, NonPolynomialReason.BaseNotPolynomial) - end return base_polynomial && !exponent_has_unknowns && is_pow_integer end push!(data.non_polynomial_terms, x) @@ -234,6 +289,12 @@ end SymbolicIndexingInterface.parameter_values(s::MTKHomotopySystem) = s.p +struct PolynomialTransformationData + new_var::BasicSymbolic + term::BasicSymbolic + inv_term::Vector +end + """ $(TYPEDSIGNATURES) @@ -265,18 +326,95 @@ function MTK.HomotopyContinuationProblem( # CSE/hashconsing. eqs = full_equations(sys) - denoms = [] - has_parametric_exponents = false - eqs2 = map(eqs) do eq + polydata = map(eqs) do eq data = PolynomialData() process_polynomial!(data, eq.lhs, dvs) process_polynomial!(data, eq.rhs, dvs) - has_parametric_exponents |= data.has_parametric_exponent - if !isempty(data.non_polynomial_terms) - throw(NotPolynomialError(eq, data)) + data + end + + has_parametric_exponents = any(d -> d.has_parametric_exponent, polydata) + + all_non_poly_terms = mapreduce(d -> d.non_polynomial_terms, vcat, polydata) + unique!(all_non_poly_terms) + + var_to_nonpoly = Dict{BasicSymbolic, PolynomialTransformationData}() + + is_poly = true + transformation_err = nothing + for t in all_non_poly_terms + # if the term involves multiple unknowns, we can't invert it + dvs_in_term = map(x -> occursin(x, t), dvs) + if count(dvs_in_term) > 1 + transformation_err = MultivarTerm(t, dvs[dvs_in_term]) + is_poly = false + break + end + # we already have a substitution solving for `var` + var = dvs[findfirst(dvs_in_term)] + if haskey(var_to_nonpoly, var) && !isequal(var_to_nonpoly[var].term, t) + transformation_err = MultipleTermsOfSameVar([t, var_to_nonpoly[var].term], var) + is_poly = false + break + end + # we want to solve `term - new_var` for `var` + new_var = gensym(Symbol(var)) + new_var = unwrap(only(@variables $new_var)) + invterm = Symbolics.ia_solve( + t - new_var, var; complex_roots = false, periodic_roots = false, warns = false) + # if we can't invert it, quit + if invterm === nothing || isempty(invterm) + transformation_err = SymbolicSolveFailure(t, var) + is_poly = false + break + end + # `ia_solve` returns lazy terms i.e. `asin(1.0)` instead of `pi/2` + # this just evaluates the constant expressions + invterm = Symbolics.substitute.(invterm, (Dict(),)) + # RootsOf implies Symbolics couldn't solve the inner polynomial because + # `Nemo` wasn't loaded. + if any(x -> MTK.iscall(x) && MTK.operation(x) == Symbolics.RootsOf, invterm) + transformation_err = NemoNotLoaded() + is_poly = false + break end - num, den = handle_rational_polynomials(eq.rhs - eq.lhs, dvs) + var_to_nonpoly[var] = PolynomialTransformationData(new_var, t, invterm) + end + + if !is_poly + throw(NotPolynomialError(transformation_err, eqs, polydata)) + end + + subrules = Dict() + combinations = Vector[] + new_dvs = [] + for x in dvs + if haskey(var_to_nonpoly, x) + _data = var_to_nonpoly[x] + subrules[_data.term] = _data.new_var + push!(combinations, _data.inv_term) + push!(new_dvs, _data.new_var) + else + push!(combinations, [x]) + push!(new_dvs, x) + end + end + all_solutions = collect.(collect(Iterators.product(combinations...))) + denoms = [] + eqs2 = map(eqs) do eq + t = eq.rhs - eq.lhs + t = Symbolics.fixpoint_sub(t, subrules; maxiters = length(dvs)) + # the substituted variable occurs outside the substituted term + poly_and_nonpoly = map(dvs) do x + haskey(var_to_nonpoly, x) && occursin(x, t) + end + if any(poly_and_nonpoly) + throw(NotPolynomialError( + VariablesAsPolyAndNonPoly(dvs[poly_and_nonpoly]), eqs, polydata)) + end + + num, den = handle_rational_polynomials(t, new_dvs) # make factors different elements, otherwise the nonzero factors artificially # inflate the error of the zero factor. if iscall(den) && operation(den) == * @@ -292,16 +430,19 @@ function MTK.HomotopyContinuationProblem( end sys2 = MTK.@set sys.eqs = eqs2 + MTK.@set! sys2.unknowns = new_dvs # remove observed equations to avoid adding them in codegen MTK.@set! sys2.observed = Equation[] MTK.@set! sys2.substitutions = nothing - nlfn, u0, p = MTK.process_SciMLProblem(NonlinearFunction{true}, sys2, u0map, parammap; - jac = true, eval_expression, eval_module) + _, u0, p = MTK.process_SciMLProblem( + MTK.EmptySciMLFunction, sys, u0map, parammap; eval_expression, eval_module) + nlfn = NonlinearFunction{true}(sys2; jac = true, eval_expression, eval_module) - denominator = MTK.build_explicit_observed_function(sys, denoms) + denominator = MTK.build_explicit_observed_function(sys2, denoms) + unpack_solution = MTK.build_explicit_observed_function(sys2, all_solutions) - hvars = symbolics_to_hc.(dvs) + hvars = symbolics_to_hc.(new_dvs) mtkhsys = MTKHomotopySystem(nlfn.f, p, nlfn.jac, hvars, length(eqs)) obsfn = MTK.ObservedFunctionCache(sys; eval_expression, eval_module) @@ -319,7 +460,7 @@ function MTK.HomotopyContinuationProblem( solver_and_starts = HomotopyContinuation.solver_startsolutions(mtkhsys; kwargs...) end return MTK.HomotopyContinuationProblem( - u0, mtkhsys, denominator, sys, obsfn, solver_and_starts) + u0, mtkhsys, denominator, sys, obsfn, solver_and_starts, unpack_solution) end """ @@ -353,25 +494,35 @@ function CommonSolve.solve(prob::MTK.HomotopyContinuationProblem, if isempty(realsols) u = state_values(prob) retcode = SciMLBase.ReturnCode.ConvergenceFailure + resid = prob.homotopy_continuation_system(u) else T = eltype(state_values(prob)) - distance, idx = findmin(realsols) do result + distance = T(Inf) + u = state_values(prob) + resid = nothing + for result in realsols if any(<=(denominator_abstol), prob.denominator(real.(result.solution), parameter_values(prob))) - return T(Inf) + continue + end + for truesol in prob.unpack_solution(result.solution, parameter_values(prob)) + dist = norm(truesol - state_values(prob)) + if dist < distance + distance = dist + u = T.(real.(truesol)) + resid = T.(real.(prob.homotopy_continuation_system(result.solution))) + end end - norm(result.solution - state_values(prob)) end # all roots cause denominator to be zero if isinf(distance) u = state_values(prob) + resid = prob.homotopy_continuation_system(u) retcode = SciMLBase.ReturnCode.Infeasible else - u = real.(realsols[idx].solution) retcode = SciMLBase.ReturnCode.Success end end - resid = prob.homotopy_continuation_system(u) return SciMLBase.build_solution( prob, :HomotopyContinuation, u, resid; retcode, original = sol) diff --git a/src/systems/nonlinear/nonlinearsystem.jl b/src/systems/nonlinear/nonlinearsystem.jl index c6b2610ae7..94b44e0a6f 100644 --- a/src/systems/nonlinear/nonlinearsystem.jl +++ b/src/systems/nonlinear/nonlinearsystem.jl @@ -690,7 +690,7 @@ A type of Nonlinear problem which specializes on polynomial systems and uses HomotopyContinuation.jl to solve the system. Requires importing HomotopyContinuation.jl to create and solve. """ -struct HomotopyContinuationProblem{uType, H, D, O, SS} <: +struct HomotopyContinuationProblem{uType, H, D, O, SS, U} <: SciMLBase.AbstractNonlinearProblem{uType, true} """ The initial values of states in the system. If there are multiple real roots of @@ -721,6 +721,12 @@ struct HomotopyContinuationProblem{uType, H, D, O, SS} <: `HomotopyContinuation.solver_startsystems`. """ solver_and_starts::SS + """ + A function which takes a solution of the transformed system, and returns a vector + of solutions for the original system. This is utilized when converting systems + to polynomials. + """ + unpack_solution::U end function HomotopyContinuationProblem(::AbstractSystem, _u0, _p; kwargs...) diff --git a/test/extensions/homotopy_continuation.jl b/test/extensions/homotopy_continuation.jl index 172ee58890..81c252c84a 100644 --- a/test/extensions/homotopy_continuation.jl +++ b/test/extensions/homotopy_continuation.jl @@ -75,19 +75,59 @@ end @testset "Polynomial check and warnings" begin @variables x = 1.0 @mtkbuild sys = NonlinearSystem([x^1.5 + x^2 - 1 ~ 0]) - @test_throws ["Exponent", "not an integer", "not a polynomial"] HomotopyContinuationProblem( + @test_throws ["Cannot convert", "Unable", "symbolically solve", + "Exponent", "not an integer", "not a polynomial"] HomotopyContinuationProblem( sys, []) @mtkbuild sys = NonlinearSystem([x^x - x ~ 0]) - @test_throws ["Exponent", "unknowns", "not a polynomial"] HomotopyContinuationProblem( + @test_throws ["Cannot convert", "Unable", "symbolically solve", + "Exponent", "unknowns", "not a polynomial"] HomotopyContinuationProblem( sys, []) @mtkbuild sys = NonlinearSystem([((x^2) / sin(x))^2 + x ~ 0]) - @test_throws ["recognized", "sin", "not a polynomial"] HomotopyContinuationProblem( + @test_throws ["Cannot convert", "both polynomial", "non-polynomial", + "recognized", "sin", "not a polynomial"] HomotopyContinuationProblem( sys, []) @variables y = 2.0 @mtkbuild sys = NonlinearSystem([x^2 + y^2 + 2 ~ 0, y ~ sin(x)]) - @test_throws ["recognized", "sin", "not a polynomial"] HomotopyContinuationProblem( + @test_throws ["Cannot convert", "recognized", "sin", "not a polynomial"] HomotopyContinuationProblem( sys, []) + + @mtkbuild sys = NonlinearSystem([x^2 + y^2 - 2 ~ 0, sin(x + y) ~ 0]) + @test_throws ["Cannot convert", "function of multiple unknowns"] HomotopyContinuationProblem( + sys, []) + + @mtkbuild sys = NonlinearSystem([sin(x)^2 + 1 ~ 0, cos(y) - cos(x) - 1 ~ 0]) + @test_throws ["Cannot convert", "multiple non-polynomial terms", "same unknown"] HomotopyContinuationProblem( + sys, []) + + @mtkbuild sys = NonlinearSystem([sin(x^2)^2 + sin(x^2) - 1 ~ 0]) + @test_throws ["import Nemo"] HomotopyContinuationProblem(sys, []) +end + +import Nemo + +@testset "With Nemo" begin + @variables x = 2.0 + @mtkbuild sys = NonlinearSystem([sin(x^2)^2 + sin(x^2) - 1 ~ 0]) + prob = HomotopyContinuationProblem(sys, []) + @test prob[1] ≈ 2.0 + sol = solve(prob; threading = false) + _x = sol[1] + @test sin(_x^2)^2 + sin(_x^2) - 1≈0.0 atol=1e-12 +end + +@testset "Function of polynomial" begin + @variables x=0.25 y=0.125 + a = sin(x^2 - 4x + 1) + b = cos(3log(y) + 4) + @mtkbuild sys = NonlinearSystem([(a^2 - 4a * b + 4b^2) / (a - 0.25) ~ 0 + (a^2 - 0.75a + 0.125) ~ 0]) + prob = HomotopyContinuationProblem(sys, []) + @test prob[x] ≈ 0.25 + @test prob[y] ≈ 0.125 + sol = solve(prob; threading = false) + @test sol[a]≈0.5 atol=1e-6 + @test sol[b]≈0.25 atol=1e-6 end @testset "Rational functions" begin From daa3fee063055687c5ee5766344f50c73dea3fd0 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 18 Nov 2024 14:03:01 +0530 Subject: [PATCH 0356/2176] fix: fix `DAEProblem` with array parameters --- src/systems/diffeqs/abstractodesystem.jl | 4 +++- src/systems/problem_utils.jl | 3 +-- test/odesystem.jl | 9 +++++++++ 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/src/systems/diffeqs/abstractodesystem.jl b/src/systems/diffeqs/abstractodesystem.jl index 518b48e929..eeb3838f99 100644 --- a/src/systems/diffeqs/abstractodesystem.jl +++ b/src/systems/diffeqs/abstractodesystem.jl @@ -221,9 +221,11 @@ function generate_function(sys::AbstractODESystem, dvs = unknowns(sys), pre, sol_states = get_substitutions_and_solved_unknowns(sys) if implicit_dae + # inputs = [] makes `wrap_array_vars` offset by 1 since there is an extra + # argument build_function(rhss, ddvs, u, p..., t; postprocess_fbody = pre, states = sol_states, - wrap_code = wrap_code .∘ wrap_array_vars(sys, rhss; dvs, ps) .∘ + wrap_code = wrap_code .∘ wrap_array_vars(sys, rhss; dvs, ps, inputs = []) .∘ wrap_parameter_dependencies(sys, false), kwargs...) else diff --git a/src/systems/problem_utils.jl b/src/systems/problem_utils.jl index 142d2f6d71..ff1d69e2bc 100644 --- a/src/systems/problem_utils.jl +++ b/src/systems/problem_utils.jl @@ -641,8 +641,7 @@ function process_SciMLProblem( ddvs = map(Differential(iv), dvs) du0map = to_varmap(du0map, ddvs) merge!(op, du0map) - - du0 = varmap_to_vars(du0map, ddvs; toterm = identity, + du0 = varmap_to_vars(op, ddvs; toterm = identity, tofloat = true) kwargs = merge(kwargs, (; ddvs)) else diff --git a/test/odesystem.jl b/test/odesystem.jl index fb185fdcb0..c36c10f4f8 100644 --- a/test/odesystem.jl +++ b/test/odesystem.jl @@ -1515,3 +1515,12 @@ end sol = solve(prob, Tsit5()) @test sol[obs] ≈ 1:7 end + +@testset "DAEProblem with array parameters" begin + @variables x(t)=1.0 y(t) [guess = 1.0] + @parameters p[1:2] = [1.0, 2.0] + @mtkbuild sys = ODESystem([D(x) ~ x, y^2 ~ x + sum(p)], t) + prob = DAEProblem(sys, [D(x) => x, D(y) => D(x) / 2y], [], (0.0, 1.0)) + sol = solve(prob, DImplicitEuler()) + @test sol[x]≈sol[y^2 - sum(p)] atol=1e-5 +end From f83ff6c90d223b09134593bdfb75b0e5c81a950c Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 18 Nov 2024 14:03:56 +0530 Subject: [PATCH 0357/2176] refactor: remove unused `DiscreteSaveAffect` --- src/systems/diffeqs/abstractodesystem.jl | 6 ------ 1 file changed, 6 deletions(-) diff --git a/src/systems/diffeqs/abstractodesystem.jl b/src/systems/diffeqs/abstractodesystem.jl index eeb3838f99..ca69c038d9 100644 --- a/src/systems/diffeqs/abstractodesystem.jl +++ b/src/systems/diffeqs/abstractodesystem.jl @@ -792,12 +792,6 @@ function DiffEqBase.ODEProblem{false}(sys::AbstractODESystem, args...; kwargs... ODEProblem{false, SciMLBase.FullSpecialize}(sys, args...; kwargs...) end -struct DiscreteSaveAffect{F, S} <: Function - f::F - s::S -end -(d::DiscreteSaveAffect)(args...) = d.f(args..., d.s) - function DiffEqBase.ODEProblem{iip, specialize}(sys::AbstractODESystem, u0map = [], tspan = get_tspan(sys), parammap = DiffEqBase.NullParameters(); From e0b22f885b1623f1ac9147ec6493405d3f2c63b7 Mon Sep 17 00:00:00 2001 From: Christopher Rackauckas Date: Mon, 18 Nov 2024 07:52:45 -0100 Subject: [PATCH 0358/2176] Update Project.toml --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 7e64764e63..7bac4f96fc 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "ModelingToolkit" uuid = "961ee093-0014-501f-94e3-6117800e7a78" authors = ["Yingbo Ma ", "Chris Rackauckas and contributors"] -version = "9.50.0" +version = "9.51.0" [deps] AbstractTrees = "1520ce14-60c1-5f80-bbc7-55ef81b5835c" From 898810c1e99477a9eaac1ddd694655604821fbd8 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 18 Nov 2024 00:12:16 +0530 Subject: [PATCH 0359/2176] test: add Nemo to extension deps --- test/extensions/Project.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/test/extensions/Project.toml b/test/extensions/Project.toml index d11b88a6e9..37614d9bfb 100644 --- a/test/extensions/Project.toml +++ b/test/extensions/Project.toml @@ -6,6 +6,7 @@ ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210" HomotopyContinuation = "f213a82b-91d6-5c5d-acf7-10f1c761b327" LabelledArrays = "2ee39098-c373-598a-b85f-a56591580800" ModelingToolkit = "961ee093-0014-501f-94e3-6117800e7a78" +Nemo = "2edaba10-b0f1-5616-af89-8c11ac63239a" OrdinaryDiffEq = "1dea7af3-3e70-54e6-95c3-0bf5283fa5ed" SciMLSensitivity = "1ed8b502-d754-442c-8d5d-10ac956f44a1" SciMLStructures = "53ae85a6-f571-4167-b2af-e1d143709226" From 13500f22fc62913b7f12a4170585b08047936cd7 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 18 Nov 2024 16:01:34 +0530 Subject: [PATCH 0360/2176] build: bump Symbolics compat required for HCExtension, using `ia_solve` without `Nemo` --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 7e64764e63..ff6d2ffa38 100644 --- a/Project.toml +++ b/Project.toml @@ -133,7 +133,7 @@ SpecialFunctions = "0.7, 0.8, 0.9, 0.10, 1.0, 2" StaticArrays = "0.10, 0.11, 0.12, 1.0" SymbolicIndexingInterface = "0.3.35" SymbolicUtils = "3.7" -Symbolics = "6.15.4" +Symbolics = "6.19" URIs = "1" UnPack = "0.1, 1.0" Unitful = "1.1" From f6d581c9fdf6f14096cf46073e9923f540a59f09 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Mon, 18 Nov 2024 11:35:06 +0000 Subject: [PATCH 0361/2176] build(deps): bump codecov/codecov-action from 4 to 5 Bumps [codecov/codecov-action](https://github.com/codecov/codecov-action) from 4 to 5. - [Release notes](https://github.com/codecov/codecov-action/releases) - [Changelog](https://github.com/codecov/codecov-action/blob/main/CHANGELOG.md) - [Commits](https://github.com/codecov/codecov-action/compare/v4...v5) --- updated-dependencies: - dependency-name: codecov/codecov-action dependency-type: direct:production update-type: version-update:semver-major ... Signed-off-by: dependabot[bot] --- .github/workflows/Documentation.yml | 2 +- .github/workflows/Downstream.yml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/Documentation.yml b/.github/workflows/Documentation.yml index dceee5f1d2..f21acd9d25 100644 --- a/.github/workflows/Documentation.yml +++ b/.github/workflows/Documentation.yml @@ -31,7 +31,7 @@ jobs: JULIA_DEBUG: "Documenter" run: DISPLAY=:0 xvfb-run -s '-screen 0 1024x768x24' julia --project=docs/ --code-coverage=user docs/make.jl - uses: julia-actions/julia-processcoverage@v1 - - uses: codecov/codecov-action@v4 + - uses: codecov/codecov-action@v5 with: file: lcov.info token: ${{ secrets.CODECOV_TOKEN }} diff --git a/.github/workflows/Downstream.yml b/.github/workflows/Downstream.yml index a6d5e84656..e407da6129 100644 --- a/.github/workflows/Downstream.yml +++ b/.github/workflows/Downstream.yml @@ -68,7 +68,7 @@ jobs: exit(0) # Exit immediately, as a success end - uses: julia-actions/julia-processcoverage@v1 - - uses: codecov/codecov-action@v4 + - uses: codecov/codecov-action@v5 with: file: lcov.info token: ${{ secrets.CODECOV_TOKEN }} From 061f3c848157935bb3b747105185eee03894b277 Mon Sep 17 00:00:00 2001 From: Christopher Rackauckas Date: Mon, 18 Nov 2024 10:37:54 -0100 Subject: [PATCH 0362/2176] Update .github/workflows/Downstream.yml --- .github/workflows/Downstream.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/Downstream.yml b/.github/workflows/Downstream.yml index e407da6129..f0aefa87f6 100644 --- a/.github/workflows/Downstream.yml +++ b/.github/workflows/Downstream.yml @@ -70,6 +70,6 @@ jobs: - uses: julia-actions/julia-processcoverage@v1 - uses: codecov/codecov-action@v5 with: - file: lcov.info + files: lcov.info token: ${{ secrets.CODECOV_TOKEN }} fail_ci_if_error: false From 720b848d24d4f80ac646d43a699c5435f155cfc0 Mon Sep 17 00:00:00 2001 From: Christopher Rackauckas Date: Mon, 18 Nov 2024 10:38:00 -0100 Subject: [PATCH 0363/2176] Update .github/workflows/Documentation.yml --- .github/workflows/Documentation.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/Documentation.yml b/.github/workflows/Documentation.yml index f21acd9d25..14bea532b3 100644 --- a/.github/workflows/Documentation.yml +++ b/.github/workflows/Documentation.yml @@ -33,6 +33,6 @@ jobs: - uses: julia-actions/julia-processcoverage@v1 - uses: codecov/codecov-action@v5 with: - file: lcov.info + files: lcov.info token: ${{ secrets.CODECOV_TOKEN }} fail_ci_if_error: true From 8dccf9db87d6353406f5663f99371e0c23666d2a Mon Sep 17 00:00:00 2001 From: Christopher Rackauckas Date: Mon, 18 Nov 2024 18:11:06 -0100 Subject: [PATCH 0364/2176] Update odesystem.jl --- test/odesystem.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/odesystem.jl b/test/odesystem.jl index c36c10f4f8..90d1c4c578 100644 --- a/test/odesystem.jl +++ b/test/odesystem.jl @@ -1521,6 +1521,6 @@ end @parameters p[1:2] = [1.0, 2.0] @mtkbuild sys = ODESystem([D(x) ~ x, y^2 ~ x + sum(p)], t) prob = DAEProblem(sys, [D(x) => x, D(y) => D(x) / 2y], [], (0.0, 1.0)) - sol = solve(prob, DImplicitEuler()) + sol = solve(prob, DFBDF(), abstol=1e-8, reltol=1e-8) @test sol[x]≈sol[y^2 - sum(p)] atol=1e-5 end From 85ca1a9f8cb8a426cab4302e0517e484951d19df Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 18 Nov 2024 14:04:14 +0530 Subject: [PATCH 0365/2176] feat: add symbolic tstops support to `ODESystem` --- src/systems/abstractsystem.jl | 17 ++++++++ src/systems/diffeqs/abstractodesystem.jl | 49 +++++++++++++++++++++++- src/systems/diffeqs/odesystem.jl | 15 ++++++-- test/odesystem.jl | 28 ++++++++++++++ 4 files changed, 103 insertions(+), 6 deletions(-) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index db2fcb4f10..8f021c9a1d 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -1051,6 +1051,7 @@ for prop in [:eqs :split_idxs :parent :is_dde + :tstops :index_cache :is_scalar_noise :isscheduled] @@ -1377,6 +1378,14 @@ function namespace_initialization_equations( map(eq -> namespace_equation(eq, sys; ivs), eqs) end +function namespace_tstops(sys::AbstractSystem) + tstops = symbolic_tstops(sys) + isempty(tstops) && return tstops + map(tstops) do val + namespace_expr(val, sys) + end +end + function namespace_equation(eq::Equation, sys, n = nameof(sys); @@ -1632,6 +1641,14 @@ function initialization_equations(sys::AbstractSystem) end end +function symbolic_tstops(sys::AbstractSystem) + tstops = get_tstops(sys) + systems = get_systems(sys) + isempty(systems) && return tstops + tstops = [tstops; reduce(vcat, namespace_tstops.(get_systems(sys)); init = [])] + return tstops +end + function preface(sys::AbstractSystem) has_preface(sys) || return nothing pre = get_preface(sys) diff --git a/src/systems/diffeqs/abstractodesystem.jl b/src/systems/diffeqs/abstractodesystem.jl index ca69c038d9..64a24b8ba3 100644 --- a/src/systems/diffeqs/abstractodesystem.jl +++ b/src/systems/diffeqs/abstractodesystem.jl @@ -758,6 +758,39 @@ function DAEFunctionExpr(sys::AbstractODESystem, args...; kwargs...) DAEFunctionExpr{true}(sys, args...; kwargs...) end +struct SymbolicTstops{F} + fn::F +end + +function (st::SymbolicTstops)(p, tspan) + unique!(sort!(reduce(vcat, st.fn(p..., tspan...)))) +end + +function SymbolicTstops( + sys::AbstractSystem; eval_expression = false, eval_module = @__MODULE__) + tstops = symbolic_tstops(sys) + isempty(tstops) && return nothing + t0 = gensym(:t0) + t1 = gensym(:t1) + tstops = map(tstops) do val + if is_array_of_symbolics(val) || val isa AbstractArray + collect(val) + else + term(:, t0, unwrap(val), t1; type = AbstractArray{Real}) + end + end + rps = reorder_parameters(sys, parameters(sys)) + tstops, _ = build_function(tstops, + rps..., + t0, + t1; + expression = Val{true}, + wrap_code = wrap_array_vars(sys, tstops; dvs = nothing) .∘ + wrap_parameter_dependencies(sys, false)) + tstops = eval_or_rgf(tstops; eval_expression, eval_module) + return SymbolicTstops(tstops) +end + """ ```julia DiffEqBase.ODEProblem{iip}(sys::AbstractODESystem, u0map, tspan, @@ -817,6 +850,11 @@ function DiffEqBase.ODEProblem{iip, specialize}(sys::AbstractODESystem, u0map = kwargs1 = merge(kwargs1, (callback = cbs,)) end + tstops = SymbolicTstops(sys; eval_expression, eval_module) + if tstops !== nothing + kwargs1 = merge(kwargs1, (; tstops)) + end + return ODEProblem{iip}(f, u0, tspan, p, pt; kwargs1..., kwargs...) end get_callback(prob::ODEProblem) = prob.kwargs[:callback] @@ -843,7 +881,7 @@ end function DiffEqBase.DAEProblem{iip}(sys::AbstractODESystem, du0map, u0map, tspan, parammap = DiffEqBase.NullParameters(); warn_initialize_determined = true, - check_length = true, kwargs...) where {iip} + check_length = true, eval_expression = false, eval_module = @__MODULE__, kwargs...) where {iip} if !iscomplete(sys) error("A completed system is required. Call `complete` or `structural_simplify` on the system before creating a `DAEProblem`") end @@ -856,8 +894,15 @@ function DiffEqBase.DAEProblem{iip}(sys::AbstractODESystem, du0map, u0map, tspan differential_vars = map(Base.Fix2(in, diffvars), sts) kwargs = filter_kwargs(kwargs) + kwargs1 = (;) + + tstops = SymbolicTstops(sys; eval_expression, eval_module) + if tstops !== nothing + kwargs1 = merge(kwargs1, (; tstops)) + end + DAEProblem{iip}(f, du0, u0, tspan, p; differential_vars = differential_vars, - kwargs...) + kwargs..., kwargs1...) end function generate_history(sys::AbstractODESystem, u0; expression = Val{false}, kwargs...) diff --git a/src/systems/diffeqs/odesystem.jl b/src/systems/diffeqs/odesystem.jl index b8dee2bac7..2b0bd8c8d7 100644 --- a/src/systems/diffeqs/odesystem.jl +++ b/src/systems/diffeqs/odesystem.jl @@ -149,6 +149,11 @@ struct ODESystem <: AbstractODESystem """ is_dde::Bool """ + A list of points to provide to the solver as tstops. Uses the same syntax as discrete + events. + """ + tstops::Vector{Any} + """ Cache for intermediate tearing state. """ tearing_state::Any @@ -187,7 +192,7 @@ struct ODESystem <: AbstractODESystem connector_type, preface, cevents, devents, parameter_dependencies, metadata = nothing, gui_metadata = nothing, is_dde = false, - tearing_state = nothing, + tstops = [], tearing_state = nothing, substitutions = nothing, complete = false, index_cache = nothing, discrete_subsystems = nothing, solved_unknowns = nothing, split_idxs = nothing, parent = nothing; checks::Union{Bool, Int} = true) @@ -206,7 +211,7 @@ struct ODESystem <: AbstractODESystem ctrl_jac, Wfact, Wfact_t, name, description, systems, defaults, guesses, torn_matching, initializesystem, initialization_eqs, schedule, connector_type, preface, cevents, devents, parameter_dependencies, metadata, - gui_metadata, is_dde, tearing_state, substitutions, complete, index_cache, + gui_metadata, is_dde, tstops, tearing_state, substitutions, complete, index_cache, discrete_subsystems, solved_unknowns, split_idxs, parent) end end @@ -233,7 +238,8 @@ function ODESystem(deqs::AbstractVector{<:Equation}, iv, dvs, ps; checks = true, metadata = nothing, gui_metadata = nothing, - is_dde = nothing) + is_dde = nothing, + tstops = []) name === nothing && throw(ArgumentError("The `name` keyword must be provided. Please consider using the `@named` macro")) @assert all(control -> any(isequal.(control, ps)), controls) "All controls must also be parameters." @@ -299,7 +305,7 @@ function ODESystem(deqs::AbstractVector{<:Equation}, iv, dvs, ps; defaults, guesses, nothing, initializesystem, initialization_eqs, schedule, connector_type, preface, cont_callbacks, disc_callbacks, parameter_dependencies, - metadata, gui_metadata, is_dde, checks = checks) + metadata, gui_metadata, is_dde, tstops, checks = checks) end function ODESystem(eqs, iv; kwargs...) @@ -402,6 +408,7 @@ function flatten(sys::ODESystem, noeqs = false) description = description(sys), initialization_eqs = initialization_equations(sys), is_dde = is_dde(sys), + tstops = symbolic_tstops(sys), checks = false) end end diff --git a/test/odesystem.jl b/test/odesystem.jl index 90d1c4c578..7f292798df 100644 --- a/test/odesystem.jl +++ b/test/odesystem.jl @@ -1524,3 +1524,31 @@ end sol = solve(prob, DFBDF(), abstol=1e-8, reltol=1e-8) @test sol[x]≈sol[y^2 - sum(p)] atol=1e-5 end + +@testset "Symbolic tstops" begin + @variables x(t) = 1.0 + @parameters p=0.15 q=0.25 r[1:2]=[0.35, 0.45] + @mtkbuild sys = ODESystem( + [D(x) ~ p * x + q * t + sum(r)], t; tstops = [0.5p, [0.1, 0.2], [p + 2q], r]) + prob = ODEProblem(sys, [], (0.0, 5.0)) + sol = solve(prob) + expected_tstops = unique!(sort!(vcat(0.0:0.075:5.0, 0.1, 0.2, 0.65, 0.35, 0.45))) + @test all(x -> any(isapprox(x, atol = 1e-6), sol.t), expected_tstops) + prob2 = remake(prob; tspan = (0.0, 10.0)) + sol2 = solve(prob2) + expected_tstops = unique!(sort!(vcat(0.0:0.075:10.0, 0.1, 0.2, 0.65, 0.35, 0.45))) + @test all(x -> any(isapprox(x, atol = 1e-6), sol2.t), expected_tstops) + + @variables y(t) [guess = 1.0] + @mtkbuild sys = ODESystem([D(x) ~ p * x + q * t + sum(r), y^3 ~ 2x + 1], + t; tstops = [0.5p, [0.1, 0.2], [p + 2q], r]) + prob = DAEProblem( + sys, [D(y) => 2D(x) / 3y^2, D(x) => p * x + q * t + sum(r)], [], (0.0, 5.0)) + sol = solve(prob, DImplicitEuler()) + expected_tstops = unique!(sort!(vcat(0.0:0.075:5.0, 0.1, 0.2, 0.65, 0.35, 0.45))) + @test all(x -> any(isapprox(x, atol = 1e-6), sol.t), expected_tstops) + prob2 = remake(prob; tspan = (0.0, 10.0)) + sol2 = solve(prob2, DImplicitEuler()) + expected_tstops = unique!(sort!(vcat(0.0:0.075:10.0, 0.1, 0.2, 0.65, 0.35, 0.45))) + @test all(x -> any(isapprox(x, atol = 1e-6), sol2.t), expected_tstops) +end From c4a5616971f1bf25891aa264b508d3fea7c3d6f9 Mon Sep 17 00:00:00 2001 From: Fredrik Bagge Carlson Date: Tue, 19 Nov 2024 09:27:50 +0100 Subject: [PATCH 0366/2176] add capability to trace MTK dynamics with InfiniteOpt --- Project.toml | 3 + ext/MTKInfiniteOptExt.jl | 19 ++++++ test/extensions/Project.toml | 3 + test/extensions/test_infiniteopt.jl | 102 ++++++++++++++++++++++++++++ test/runtests.jl | 1 + 5 files changed, 128 insertions(+) create mode 100644 ext/MTKInfiniteOptExt.jl create mode 100644 test/extensions/test_infiniteopt.jl diff --git a/Project.toml b/Project.toml index 18b4d55992..5848298418 100644 --- a/Project.toml +++ b/Project.toml @@ -64,6 +64,7 @@ BifurcationKit = "0f109fa4-8a5d-4b75-95aa-f515264e7665" ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4" DeepDiffs = "ab62b9b5-e342-54a8-a765-a90f495de1a6" HomotopyContinuation = "f213a82b-91d6-5c5d-acf7-10f1c761b327" +InfiniteOpt = "20393b10-9daf-11e9-18c9-8db751c92c57" LabelledArrays = "2ee39098-c373-598a-b85f-a56591580800" [extensions] @@ -72,6 +73,7 @@ MTKChainRulesCoreExt = "ChainRulesCore" MTKDeepDiffsExt = "DeepDiffs" MTKHomotopyContinuationExt = "HomotopyContinuation" MTKLabelledArraysExt = "LabelledArrays" +MTKInfiniteOptExt = "InfiniteOpt" [compat] AbstractTrees = "0.3, 0.4" @@ -104,6 +106,7 @@ FunctionWrappers = "1.1" FunctionWrappersWrappers = "0.1" Graphs = "1.5.2" HomotopyContinuation = "2.11" +InfiniteOpt = "0.5" InteractiveUtils = "1" JuliaFormatter = "1.0.47" JumpProcesses = "9.13.1" diff --git a/ext/MTKInfiniteOptExt.jl b/ext/MTKInfiniteOptExt.jl new file mode 100644 index 0000000000..88bf724d14 --- /dev/null +++ b/ext/MTKInfiniteOptExt.jl @@ -0,0 +1,19 @@ +module MTKInfiniteOptExt +import ModelingToolkit +import SymbolicUtils +import NaNMath +import InfiniteOpt + +# This file contains method definitions to make it possible to trace through functions generated by MTK using JuMP variables + +for ff in [acos, log1p, acosh, log2, asin, lgamma, tan, atanh, cos, log, sin, log10, sqrt] + f = nameof(ff) + # These need to be defined so that JuMP can trace through functions built by Symbolics + @eval NaNMath.$f(x::GeneralVariableRef) = Base.$f(x) +end + +# JuMP variables and Symbolics variables never compare equal. When tracing through dynamics, a function argument can be either a JuMP variable or A Symbolics variable, it can never be both. +Base.isequal(::SymbolicUtils.BasicSymbolic{Real}, ::InfiniteOpt.GeneralVariableRef) = false +Base.isequal(::InfiniteOpt.GeneralVariableRef, ::SymbolicUtils.BasicSymbolic{Real}) = false + +end diff --git a/test/extensions/Project.toml b/test/extensions/Project.toml index 37614d9bfb..5f7afe222a 100644 --- a/test/extensions/Project.toml +++ b/test/extensions/Project.toml @@ -4,6 +4,9 @@ ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4" ChainRulesTestUtils = "cdddcdb0-9152-4a09-a978-84456f9df70a" ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210" HomotopyContinuation = "f213a82b-91d6-5c5d-acf7-10f1c761b327" +InfiniteOpt = "20393b10-9daf-11e9-18c9-8db751c92c57" +Ipopt = "b6b21f68-93f8-5de0-b562-5493be1d77c9" +JuMP = "4076af6c-e467-56ae-b986-b466b2749572" LabelledArrays = "2ee39098-c373-598a-b85f-a56591580800" ModelingToolkit = "961ee093-0014-501f-94e3-6117800e7a78" Nemo = "2edaba10-b0f1-5616-af89-8c11ac63239a" diff --git a/test/extensions/test_infiniteopt.jl b/test/extensions/test_infiniteopt.jl new file mode 100644 index 0000000000..e45aa0f2fd --- /dev/null +++ b/test/extensions/test_infiniteopt.jl @@ -0,0 +1,102 @@ +using ModelingToolkit, InfiniteOpt, JuMP, Ipopt +using ModelingToolkit: D_nounits as D, t_nounits as t, varmap_to_vars + +@mtkmodel Pendulum begin + @parameters begin + g = 9.8 + L = 0.4 + K = 1.2 + m = 0.3 + end + @variables begin + θ(t) # state + ω(t) # state + τ(t) = 0 # input + y(t) # output + end + @equations begin + D(θ) ~ ω + D(ω) ~ -g / L * sin(θ) - K / m * ω + τ / m / L^2 + y ~ θ * 180 / π + end +end +@named model = Pendulum() +model = complete(model) + +inputs = [model.τ] +(f_oop, f_ip), dvs, psym, io_sys = ModelingToolkit.generate_control_function( + model, inputs, split = false) + +outputs = [model.y] +f_obs = ModelingToolkit.build_explicit_observed_function(io_sys, outputs; inputs = inputs) + +expected_state_order = [model.θ, model.ω] +permutation = [findfirst(isequal(x), expected_state_order) for x in dvs] # This maps our expected state order to the actual state order + +## + +ub = varmap_to_vars([model.θ => 2pi, model.ω => 10], dvs) +lb = varmap_to_vars([model.θ => -2pi, model.ω => -10], dvs) +xf = varmap_to_vars([model.θ => pi, model.ω => 0], dvs) +nx = length(dvs) +nu = length(inputs) +ny = length(outputs) + +## +m = InfiniteModel(optimizer_with_attributes(Ipopt.Optimizer, + "print_level" => 0, "acceptable_tol" => 1e-3, "constr_viol_tol" => 1e-5, "max_iter" => 1000, + "tol" => 1e-5, "mu_strategy" => "monotone", "nlp_scaling_method" => "gradient-based", + "alpha_for_y" => "safer-min-dual-infeas", "bound_mult_init_method" => "mu-based", "print_user_options" => "yes")); + +@infinite_parameter(m, τ in [0, 1], num_supports=51, + derivative_method=OrthogonalCollocation(4)) # Time variable +guess_xs = [t -> pi, t -> 0.1][permutation] +guess_us = [t -> 0.1] +InfiniteOpt.@variables(m, + begin + # state variables + (lb[i] <= x[i = 1:nx] <= ub[i], Infinite(τ), start = guess_xs[i]) # state variables + -10 <= u[i = 1:nu] <= 10, Infinite(τ), (start = guess_us[i]) # control variables + 0 <= tf <= 10, (start = 5) # Final time + 0.2 <= L <= 0.6, (start = 0.4) # Length parameter + end) + +# Trace the dynamics +x0, p = ModelingToolkit.get_u0_p(io_sys, [model.θ => 0, model.ω => 0], [model.L => L]) + +xp = f_oop(x, u, p, τ) +cp = f_obs(x, u, p, τ) # Test that it's possible to trace through an observed function + +@objective(m, Min, tf) +@constraint(m, [i = 1:nx], x[i](0)==x0[i]) # Initial condition +@constraint(m, [i = 1:nx], x[i](1)==xf[i]) # Terminal state + +x_scale = varmap_to_vars([model.θ => 1 + model.ω => 1], dvs) + +# Add dynamics constraints +@constraint(m, [i = 1:nx], (∂(x[i], τ) - tf * xp[i]) / x_scale[i]==0) + +optimize!(m) + +# Extract the optimal solution +opt_tf = value(tf) +opt_time = opt_tf * value(τ) +opt_x = [value(x[i]) for i in permutation] +opt_u = [value(u[i]) for i in 1:nu] +opt_L = value(L) + +# Plot the results +# using Plots +# plot(opt_time, opt_x[1], label = "θ", xlabel = "Time [s]", layout=3) +# plot!(opt_time, opt_x[2], label = "ω", sp=2) +# plot!(opt_time, opt_u[1], label = "τ", sp=3) + +using Test +@test opt_x[1][end]≈pi atol=1e-3 +@test opt_x[2][end]≈0 atol=1e-3 + +@test opt_x[1][1]≈0 atol=1e-3 +@test opt_x[2][1]≈0 atol=1e-3 + +@test opt_L≈0.2 atol=1e-3 # Smallest permissible length is optimal diff --git a/test/runtests.jl b/test/runtests.jl index 192ead935c..25b692f678 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -113,5 +113,6 @@ end @safetestset "Auto Differentiation Test" include("extensions/ad.jl") @safetestset "LabelledArrays Test" include("labelledarrays.jl") @safetestset "BifurcationKit Extension Test" include("extensions/bifurcationkit.jl") + @safetestset "InfiniteOpt Extension Test" include("extensions/test_infiniteopt.jl") end end From d4f7e8a1392b4aa8b6c3507c706d91ac968eb9d6 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 19 Nov 2024 13:30:37 +0530 Subject: [PATCH 0367/2176] fix: handle observeds in `get_all_timeseries_indexes` for `split = false` systems --- src/systems/abstractsystem.jl | 18 +++++++++++++----- test/symbolic_indexing_interface.jl | 11 +++++++++++ 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index db2fcb4f10..6d59c5b8ea 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -770,11 +770,19 @@ for traitT in [ is_variable(sys, operation(s)(get_iv(sys))) # DDEs case, to detect x(t - k) push!(ts_idxs, ContinuousTimeseries()) - elseif has_index_cache(sys) && (ic = get_index_cache(sys)) !== nothing - if (ts = get(ic.observed_syms_to_timeseries, s, nothing)) !== nothing - union!(ts_idxs, ts) - elseif (ts = get(ic.dependent_pars_to_timeseries, s, nothing)) !== nothing - union!(ts_idxs, ts) + else + if has_index_cache(sys) && (ic = get_index_cache(sys)) !== nothing + if (ts = get(ic.observed_syms_to_timeseries, s, nothing)) !== nothing + union!(ts_idxs, ts) + elseif (ts = get(ic.dependent_pars_to_timeseries, s, nothing)) !== + nothing + union!(ts_idxs, ts) + end + else + # for split=false systems + if has_observed_with_lhs(sys, sym) + push!(ts_idxs, ContinuousTimeseries()) + end end end end diff --git a/test/symbolic_indexing_interface.jl b/test/symbolic_indexing_interface.jl index 161d926cb2..e46c197dde 100644 --- a/test/symbolic_indexing_interface.jl +++ b/test/symbolic_indexing_interface.jl @@ -213,3 +213,14 @@ end prob = ODEProblem(sys, [], (0.0, 1.0)) @test prob.ps[b] == prob.ps[:b] end + +@testset "`get_all_timeseries_indexes` with non-split systems" begin + @variables x(t) y(t) z(t) + @parameters a + @named sys = ODESystem([D(x) ~ a * x, y ~ 2x, z ~ 0.0], t) + sys = structural_simplify(sys, split = false) + for sym in [x, y, z, x + y, x + a, y / x] + @test only(get_all_timeseries_indexes(sys, sym)) == ContinuousTimeseries() + end + @test isempty(get_all_timeseries_indexes(sys, a)) +end From 0b9d51dbac41d7bf3138fa3f6c5710a79043330a Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 19 Nov 2024 14:39:40 +0530 Subject: [PATCH 0368/2176] refactor: format --- test/odesystem.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/odesystem.jl b/test/odesystem.jl index 90d1c4c578..02eb80ef85 100644 --- a/test/odesystem.jl +++ b/test/odesystem.jl @@ -1521,6 +1521,6 @@ end @parameters p[1:2] = [1.0, 2.0] @mtkbuild sys = ODESystem([D(x) ~ x, y^2 ~ x + sum(p)], t) prob = DAEProblem(sys, [D(x) => x, D(y) => D(x) / 2y], [], (0.0, 1.0)) - sol = solve(prob, DFBDF(), abstol=1e-8, reltol=1e-8) + sol = solve(prob, DFBDF(), abstol = 1e-8, reltol = 1e-8) @test sol[x]≈sol[y^2 - sum(p)] atol=1e-5 end From a631481e026c5ee39219dc8868f1f3e34a2269f0 Mon Sep 17 00:00:00 2001 From: David Widmann Date: Tue, 19 Nov 2024 10:51:51 +0100 Subject: [PATCH 0369/2176] Remove unused variables --- src/systems/diffeqs/abstractodesystem.jl | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/systems/diffeqs/abstractodesystem.jl b/src/systems/diffeqs/abstractodesystem.jl index ca69c038d9..3d8df99f0a 100644 --- a/src/systems/diffeqs/abstractodesystem.jl +++ b/src/systems/diffeqs/abstractodesystem.jl @@ -71,8 +71,6 @@ function calculate_jacobian(sys::AbstractODESystem; rhs = [eq.rhs - eq.lhs for eq in full_equations(sys)] #need du terms on rhs for differentiating wrt du - iv = get_iv(sys) - if sparse jac = sparsejacobian(rhs, dvs, simplify = simplify) else @@ -94,8 +92,6 @@ function calculate_control_jacobian(sys::AbstractODESystem; end rhs = [eq.rhs for eq in full_equations(sys)] - - iv = get_iv(sys) ctrls = controls(sys) if sparse From 00bfd1d539977588416cfb6de8cdcbcbf8ce2c24 Mon Sep 17 00:00:00 2001 From: Christopher Rackauckas Date: Tue, 19 Nov 2024 09:36:52 -0100 Subject: [PATCH 0370/2176] Update MTKInfiniteOptExt.jl --- ext/MTKInfiniteOptExt.jl | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/ext/MTKInfiniteOptExt.jl b/ext/MTKInfiniteOptExt.jl index 88bf724d14..79463f7e71 100644 --- a/ext/MTKInfiniteOptExt.jl +++ b/ext/MTKInfiniteOptExt.jl @@ -15,5 +15,6 @@ end # JuMP variables and Symbolics variables never compare equal. When tracing through dynamics, a function argument can be either a JuMP variable or A Symbolics variable, it can never be both. Base.isequal(::SymbolicUtils.BasicSymbolic{Real}, ::InfiniteOpt.GeneralVariableRef) = false Base.isequal(::InfiniteOpt.GeneralVariableRef, ::SymbolicUtils.BasicSymbolic{Real}) = false - +Base.isequal(::SymbolicUtils.Symbolic, ::Union{JuMP.GenericAffExpr, JuMP.GenericQuadExpr, InfiniteOpt.AbstractInfOptExpr}) = false +Base.isequal(::Union{JuMP.GenericAffExpr, JuMP.GenericQuadExpr, InfiniteOpt.AbstractInfOptExpr}, ::SymbolicUtils.Symbolic) = false end From a40177226295769049f51d926bb6fca3b23ec800 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 19 Nov 2024 16:13:35 +0530 Subject: [PATCH 0371/2176] fix: set defaults in `modelingtoolkitize(::SDEProblem)` --- src/systems/diffeqs/modelingtoolkitize.jl | 16 ++++++++++++- test/modelingtoolkitize.jl | 28 +++++++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/src/systems/diffeqs/modelingtoolkitize.jl b/src/systems/diffeqs/modelingtoolkitize.jl index 68a970d8b5..2e0623bea1 100644 --- a/src/systems/diffeqs/modelingtoolkitize.jl +++ b/src/systems/diffeqs/modelingtoolkitize.jl @@ -277,10 +277,24 @@ function modelingtoolkitize(prob::DiffEqBase.SDEProblem; kwargs...) else Vector(vec(params)) end + sts = Vector(vec(vars)) + default_u0 = Dict(sts .=> vec(collect(prob.u0))) + default_p = if has_p + if prob.p isa AbstractDict + Dict(v => prob.p[k] for (k, v) in pairs(_params)) + elseif prob.p isa MTKParameters + Dict(params .=> reduce(vcat, prob.p)) + else + Dict(params .=> vec(collect(prob.p))) + end + else + Dict() + end - de = SDESystem(deqs, neqs, t, Vector(vec(vars)), params; + de = SDESystem(deqs, neqs, t, sts, params; name = gensym(:MTKizedSDE), tspan = prob.tspan, + defaults = merge(default_u0, default_p), kwargs...) de diff --git a/test/modelingtoolkitize.jl b/test/modelingtoolkitize.jl index 621bf530b7..3bcfdecc8b 100644 --- a/test/modelingtoolkitize.jl +++ b/test/modelingtoolkitize.jl @@ -441,3 +441,31 @@ prob = NonlinearLeastSquaresProblem( sys = modelingtoolkitize(prob) @test length(equations(sys)) == 3 @test length(equations(structural_simplify(sys; fully_determined = false))) == 0 + +@testset "`modelingtoolkitize(::SDEProblem)` sets defaults" begin + function sdeg!(du, u, p, t) + du[1] = 0.3 * u[1] + du[2] = 0.3 * u[2] + du[3] = 0.3 * u[3] + end + function sdef!(du, u, p, t) + x, y, z = u + sigma, rho, beta = p + du[1] = sigma * (y - x) + du[2] = x * (rho - z) - y + du[3] = x * y - beta * z + end + u0 = [1.0, 0.0, 0.0] + tspan = (0.0, 100.0) + p = [10.0, 28.0, 2.66] + sprob = SDEProblem(sdef!, sdeg!, u0, tspan, p) + sys = complete(modelingtoolkitize(sprob)) + @test length(ModelingToolkit.defaults(sys)) == 6 + sprob2 = SDEProblem(sys, [], tspan) + + truevals = similar(u0) + sprob.f(truevals, u0, p, tspan[1]) + mtkvals = similar(u0) + sprob2.f(mtkvals, sprob2.u0, sprob2.p, tspan[1]) + @test mtkvals ≈ truevals +end From 73daad1904da5c6d44af94d6272ba6e2416777d1 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 19 Nov 2024 19:09:41 +0530 Subject: [PATCH 0372/2176] feat: add function to parse variable from string --- src/systems/abstractsystem.jl | 73 +++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index db2fcb4f10..9c427a2a12 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -3285,6 +3285,79 @@ function dump_unknowns(sys::AbstractSystem) end end +# syntax: +# varname = "D(" varname ")" | arrvar | maybe_dummy_var +# arrvar = maybe_dummy_var "[idxs...]" +# idxs = int | int "," idxs +# maybe_dummy_var = namespacedvar | namespacedvar "(t)" | +# namespacedvar "(t)" "ˍ" ts | namespacedvar "ˍ" ts | +# namespacedvar "ˍ" ts "(t)" +# ts = "t" | "t" ts +# namespacedvar = ident "₊" namespacedvar | ident "." namespacedvar | ident +# +# I'd write a regex to validate this, but https://xkcd.com/1171/ +function parse_variable(sys::AbstractSystem, str::AbstractString) + str = strip(str) + derivative_level = 0 + while startswith(str, "D(") && endswith(str, ")") + derivative_level += 1 + str = _string_view_inner(str, 2, 1) + end + + arr_idxs = nothing + if endswith(str, ']') + open_idx = only(findfirst('[', str)) + idxs_range = nextind(str, open_idx):prevind(str, lastindex(str)) + idxs_str = view(str, idxs_range) + str = view(str, firstindex(str):prevind(str, open_idx)) + arr_idxs = map(Base.Fix1(parse, Int), eachsplit(idxs_str, ",")) + end + + if endswith(str, "(t)") + str = _string_view_inner(str, 0, 3) + end + + dummyderivative_level = 0 + if (dd_idx = findfirst('ˍ', str)) !== nothing + t_idx = nextind(str, dd_idx) + while checkbounds(Bool, str, t_idx) + if str[t_idx] != 't' + throw(ArgumentError("Dummy derivative must be 'ˍ' followed by one or more 't'.")) + end + dummyderivative_level += 1 + t_idx = nextind(str, t_idx) + end + str = view(str, firstindex(str):prevind(str, dd_idx)) + end + + if endswith(str, "(t)") + str = _string_view_inner(str, 0, 3) + end + + cur = sys + for ident in eachsplit(str, ('.', NAMESPACE_SEPARATOR)) + ident = Symbol(ident) + hasproperty(cur, ident) || + throw(ArgumentError("System $(nameof(cur)) does not have a subsystem/variable named $(ident)")) + cur = getproperty(cur, ident) + end + + if arr_idxs !== nothing + cur = cur[arr_idxs...] + end + + for i in 1:(derivative_level + dummyderivative_level) + cur = Differential(get_iv(sys))(cur) + end + + return cur +end + +function _string_view_inner(str, startoffset, endoffset) + view(str, + nextind(str, firstindex(str), startoffset):prevind(str, lastindex(str), endoffset)) +end + ### Functions for accessing algebraic/differential equations in systems ### """ From fc13ecd8e726694042be7ca823a04941b3f6236a Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 19 Nov 2024 19:23:54 +0530 Subject: [PATCH 0373/2176] test: add tests for `parse_variable` --- test/variable_utils.jl | 99 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 98 insertions(+), 1 deletion(-) diff --git a/test/variable_utils.jl b/test/variable_utils.jl index d76f2f1209..100e2bc1ad 100644 --- a/test/variable_utils.jl +++ b/test/variable_utils.jl @@ -1,6 +1,7 @@ using ModelingToolkit, Test -using ModelingToolkit: value, vars +using ModelingToolkit: value, vars, parse_variable using SymbolicUtils: <ₑ + @parameters α β δ expr = (((1 / β - 1) + δ) / α)^(1 / (α - 1)) ref = sort([β, δ, α], lt = <ₑ) @@ -41,3 +42,99 @@ ts = collect_ivs([eq]) res = vars(fn([x, y], z)) @test length(res) == 3 end + +@testset "parse_variable" begin + @mtkmodel Lorenz begin + @variables begin + x(t) + y(t) + z(t) + end + @parameters begin + σ + ρ + β + end + @equations begin + D(D(x)) ~ σ * (y - x) + D(y) ~ x * (ρ - z) - y + D(z) ~ x * y - β * z + end + end + @mtkmodel ArrSys begin + @variables begin + x(t)[1:2] + end + @parameters begin + p[1:2, 1:2] + end + @equations begin + D(D(x)) ~ p * x + end + end + @mtkmodel Outer begin + @components begin + 😄 = Lorenz() + arr = ArrSys() + end + end + + @mtkbuild sys = Outer() + for (str, var) in [ + # unicode system, scalar variable + ("😄.x", sys.😄.x), + ("😄.x(t)", sys.😄.x), + ("😄₊x", sys.😄.x), + ("😄₊x(t)", sys.😄.x), + # derivative + ("D(😄.x)", D(sys.😄.x)), + ("D(😄.x(t))", D(sys.😄.x)), + ("D(😄₊x)", D(sys.😄.x)), + ("D(😄₊x(t))", D(sys.😄.x)), + # other derivative + ("😄.xˍt", D(sys.😄.x)), + ("😄.x(t)ˍt", D(sys.😄.x)), + ("😄₊xˍt", D(sys.😄.x)), + ("😄₊x(t)ˍt", D(sys.😄.x)), + # scalar parameter + ("😄.σ", sys.😄.σ), + ("😄₊σ", sys.😄.σ), + # array variable + ("arr.x", sys.arr.x), + ("arr₊x", sys.arr.x), + ("arr.x(t)", sys.arr.x), + ("arr₊x(t)", sys.arr.x), + # getindex + ("arr.x[1]", sys.arr.x[1]), + ("arr₊x[1]", sys.arr.x[1]), + ("arr.x(t)[1]", sys.arr.x[1]), + ("arr₊x(t)[1]", sys.arr.x[1]), + # derivative + ("D(arr.x(t))", D(sys.arr.x)), + ("D(arr₊x(t))", D(sys.arr.x)), + ("D(arr.x[1])", D(sys.arr.x[1])), + ("D(arr₊x[1])", D(sys.arr.x[1])), + ("D(arr.x(t)[1])", D(sys.arr.x[1])), + ("D(arr₊x(t)[1])", D(sys.arr.x[1])), + # other derivative + ("arr.xˍt", D(sys.arr.x)), + ("arr₊xˍt", D(sys.arr.x)), + ("arr.xˍt(t)", D(sys.arr.x)), + ("arr₊xˍt(t)", D(sys.arr.x)), + ("arr.xˍt[1]", D(sys.arr.x[1])), + ("arr₊xˍt[1]", D(sys.arr.x[1])), + ("arr.xˍt(t)[1]", D(sys.arr.x[1])), + ("arr₊xˍt(t)[1]", D(sys.arr.x[1])), + ("arr.x(t)ˍt", D(sys.arr.x)), + ("arr₊x(t)ˍt", D(sys.arr.x)), + ("arr.x(t)ˍt[1]", D(sys.arr.x[1])), + ("arr₊x(t)ˍt[1]", D(sys.arr.x[1])), + # array parameter + ("arr.p", sys.arr.p), + ("arr₊p", sys.arr.p), + ("arr.p[1, 2]", sys.arr.p[1, 2]), + ("arr₊p[1, 2]", sys.arr.p[1, 2]) + ] + isequal(parse_variable(sys, str), var) + end +end From 8712f991b7096d8715e131a9edf1c72fdf9b8ac2 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 19 Nov 2024 19:26:30 +0530 Subject: [PATCH 0374/2176] docs: add docstring for `parse_variable` --- src/systems/abstractsystem.jl | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index 9c427a2a12..ba78a40a58 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -3285,18 +3285,25 @@ function dump_unknowns(sys::AbstractSystem) end end -# syntax: -# varname = "D(" varname ")" | arrvar | maybe_dummy_var -# arrvar = maybe_dummy_var "[idxs...]" -# idxs = int | int "," idxs -# maybe_dummy_var = namespacedvar | namespacedvar "(t)" | -# namespacedvar "(t)" "ˍ" ts | namespacedvar "ˍ" ts | -# namespacedvar "ˍ" ts "(t)" -# ts = "t" | "t" ts -# namespacedvar = ident "₊" namespacedvar | ident "." namespacedvar | ident -# -# I'd write a regex to validate this, but https://xkcd.com/1171/ +""" + $(TYPEDSIGNATURES) + +Return the variable in `sys` referred to by its string representation `str`. +Roughly supports the following CFG: + +``` +varname = "D(" varname ")" | arrvar | maybe_dummy_var +arrvar = maybe_dummy_var "[idxs...]" +idxs = int | int "," idxs +maybe_dummy_var = namespacedvar | namespacedvar "(t)" | + namespacedvar "(t)" "ˍ" ts | namespacedvar "ˍ" ts | + namespacedvar "ˍ" ts "(t)" +ts = "t" | "t" ts +namespacedvar = ident "₊" namespacedvar | ident "." namespacedvar | ident +``` +""" function parse_variable(sys::AbstractSystem, str::AbstractString) + # I'd write a regex to validate `str`, but https://xkcd.com/1171/ str = strip(str) derivative_level = 0 while startswith(str, "D(") && endswith(str, ")") From dddcd2c9c568dfe22e49a8b81afda21b3bee2d58 Mon Sep 17 00:00:00 2001 From: Christopher Rackauckas Date: Tue, 19 Nov 2024 17:46:53 -0100 Subject: [PATCH 0375/2176] Update Project.toml --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 18b4d55992..e1bcbef173 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "ModelingToolkit" uuid = "961ee093-0014-501f-94e3-6117800e7a78" authors = ["Yingbo Ma ", "Chris Rackauckas and contributors"] -version = "9.51.0" +version = "9.52.0" [deps] AbstractTrees = "1520ce14-60c1-5f80-bbc7-55ef81b5835c" From 3c728a399b8edf733d7be568750c32b68d828d09 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 20 Nov 2024 11:53:25 +0530 Subject: [PATCH 0376/2176] docs: add `parse_variable` to documentation --- docs/src/basics/FAQ.md | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/docs/src/basics/FAQ.md b/docs/src/basics/FAQ.md index bb3d01fc30..10671299c6 100644 --- a/docs/src/basics/FAQ.md +++ b/docs/src/basics/FAQ.md @@ -82,6 +82,16 @@ parameter_index(sys, sym) Note that while the variable index will be an integer, the parameter index is a struct of type `ParameterIndex` whose internals should not be relied upon. +## Can I index with strings? + +Strings are not considered symbolic variables, and thus cannot directly be used for symbolic +indexing. However, ModelingToolkit does provide a method to parse the string representation of +a variable, given the system in which that variable exists. + +```@docs +ModelingToolkit.parse_variable +``` + ## Transforming value maps to arrays ModelingToolkit.jl allows (and recommends) input maps like `[x => 2.0, y => 3.0]` From afd55cbe3f197836a438163fba084e06bc9cc623 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 20 Nov 2024 11:53:57 +0530 Subject: [PATCH 0377/2176] feat: support arbitrary independent variables in `parse_variable` --- src/systems/abstractsystem.jl | 31 ++++++------ test/variable_utils.jl | 95 +++++++++++++++++------------------ 2 files changed, 62 insertions(+), 64 deletions(-) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index ba78a40a58..36bd5ac800 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -3295,14 +3295,18 @@ Roughly supports the following CFG: varname = "D(" varname ")" | arrvar | maybe_dummy_var arrvar = maybe_dummy_var "[idxs...]" idxs = int | int "," idxs -maybe_dummy_var = namespacedvar | namespacedvar "(t)" | - namespacedvar "(t)" "ˍ" ts | namespacedvar "ˍ" ts | - namespacedvar "ˍ" ts "(t)" -ts = "t" | "t" ts +maybe_dummy_var = namespacedvar | namespacedvar "(" iv ")" | + namespacedvar "(" iv ")" "ˍ" ts | namespacedvar "ˍ" ts | + namespacedvar "ˍ" ts "(" iv ")" +ts = iv | iv ts namespacedvar = ident "₊" namespacedvar | ident "." namespacedvar | ident ``` + +Where `iv` is the independent variable, `int` is an integer and `ident` is an identifier. """ function parse_variable(sys::AbstractSystem, str::AbstractString) + iv = has_iv(sys) ? string(getname(get_iv(sys))) : nothing + # I'd write a regex to validate `str`, but https://xkcd.com/1171/ str = strip(str) derivative_level = 0 @@ -3320,25 +3324,22 @@ function parse_variable(sys::AbstractSystem, str::AbstractString) arr_idxs = map(Base.Fix1(parse, Int), eachsplit(idxs_str, ",")) end - if endswith(str, "(t)") - str = _string_view_inner(str, 0, 3) + if iv !== nothing && endswith(str, "($iv)") + str = _string_view_inner(str, 0, 2 + length(iv)) end dummyderivative_level = 0 - if (dd_idx = findfirst('ˍ', str)) !== nothing - t_idx = nextind(str, dd_idx) - while checkbounds(Bool, str, t_idx) - if str[t_idx] != 't' - throw(ArgumentError("Dummy derivative must be 'ˍ' followed by one or more 't'.")) - end + if iv !== nothing && (dd_idx = findfirst('ˍ', str)) !== nothing + t_idx = findnext(iv, str, dd_idx) + while t_idx !== nothing dummyderivative_level += 1 - t_idx = nextind(str, t_idx) + t_idx = findnext(iv, str, nextind(str, last(t_idx))) end str = view(str, firstindex(str):prevind(str, dd_idx)) end - if endswith(str, "(t)") - str = _string_view_inner(str, 0, 3) + if iv !== nothing && endswith(str, "($iv)") + str = _string_view_inner(str, 0, 2 + length(iv)) end cur = sys diff --git a/test/variable_utils.jl b/test/variable_utils.jl index 100e2bc1ad..ebbc3c0d3b 100644 --- a/test/variable_utils.jl +++ b/test/variable_utils.jl @@ -43,98 +43,95 @@ ts = collect_ivs([eq]) @test length(res) == 3 end -@testset "parse_variable" begin - @mtkmodel Lorenz begin +@testset "parse_variable with iv: $iv" for iv in [t, only(@independent_variables tt)] + D = Differential(iv) + function Lorenz(; name) @variables begin - x(t) - y(t) - z(t) + x(iv) + y(iv) + z(iv) end @parameters begin σ ρ β end - @equations begin - D(D(x)) ~ σ * (y - x) - D(y) ~ x * (ρ - z) - y - D(z) ~ x * y - β * z - end + sys = ODESystem( + [D(D(x)) ~ σ * (y - x) + D(y) ~ x * (ρ - z) - y + D(z) ~ x * y - β * z], iv; name) end - @mtkmodel ArrSys begin + function ArrSys(; name) @variables begin - x(t)[1:2] + x(iv)[1:2] end @parameters begin p[1:2, 1:2] end - @equations begin - D(D(x)) ~ p * x - end + sys = ODESystem([D(D(x)) ~ p * x], iv; name) end - @mtkmodel Outer begin - @components begin - 😄 = Lorenz() - arr = ArrSys() - end + function Outer(; name) + @named 😄 = Lorenz() + @named arr = ArrSys() + sys = ODESystem(Equation[], iv; name, systems = [😄, arr]) end @mtkbuild sys = Outer() for (str, var) in [ # unicode system, scalar variable ("😄.x", sys.😄.x), - ("😄.x(t)", sys.😄.x), + ("😄.x($iv)", sys.😄.x), ("😄₊x", sys.😄.x), - ("😄₊x(t)", sys.😄.x), + ("😄₊x($iv)", sys.😄.x), # derivative ("D(😄.x)", D(sys.😄.x)), - ("D(😄.x(t))", D(sys.😄.x)), + ("D(😄.x($iv))", D(sys.😄.x)), ("D(😄₊x)", D(sys.😄.x)), - ("D(😄₊x(t))", D(sys.😄.x)), + ("D(😄₊x($iv))", D(sys.😄.x)), # other derivative - ("😄.xˍt", D(sys.😄.x)), - ("😄.x(t)ˍt", D(sys.😄.x)), - ("😄₊xˍt", D(sys.😄.x)), - ("😄₊x(t)ˍt", D(sys.😄.x)), + ("😄.xˍ$iv", D(sys.😄.x)), + ("😄.x($iv)ˍ$iv", D(sys.😄.x)), + ("😄₊xˍ$iv", D(sys.😄.x)), + ("😄₊x($iv)ˍ$iv", D(sys.😄.x)), # scalar parameter ("😄.σ", sys.😄.σ), ("😄₊σ", sys.😄.σ), # array variable ("arr.x", sys.arr.x), ("arr₊x", sys.arr.x), - ("arr.x(t)", sys.arr.x), - ("arr₊x(t)", sys.arr.x), + ("arr.x($iv)", sys.arr.x), + ("arr₊x($iv)", sys.arr.x), # getindex ("arr.x[1]", sys.arr.x[1]), ("arr₊x[1]", sys.arr.x[1]), - ("arr.x(t)[1]", sys.arr.x[1]), - ("arr₊x(t)[1]", sys.arr.x[1]), + ("arr.x($iv)[1]", sys.arr.x[1]), + ("arr₊x($iv)[1]", sys.arr.x[1]), # derivative - ("D(arr.x(t))", D(sys.arr.x)), - ("D(arr₊x(t))", D(sys.arr.x)), + ("D(arr.x($iv))", D(sys.arr.x)), + ("D(arr₊x($iv))", D(sys.arr.x)), ("D(arr.x[1])", D(sys.arr.x[1])), ("D(arr₊x[1])", D(sys.arr.x[1])), - ("D(arr.x(t)[1])", D(sys.arr.x[1])), - ("D(arr₊x(t)[1])", D(sys.arr.x[1])), + ("D(arr.x($iv)[1])", D(sys.arr.x[1])), + ("D(arr₊x($iv)[1])", D(sys.arr.x[1])), # other derivative - ("arr.xˍt", D(sys.arr.x)), - ("arr₊xˍt", D(sys.arr.x)), - ("arr.xˍt(t)", D(sys.arr.x)), - ("arr₊xˍt(t)", D(sys.arr.x)), - ("arr.xˍt[1]", D(sys.arr.x[1])), - ("arr₊xˍt[1]", D(sys.arr.x[1])), - ("arr.xˍt(t)[1]", D(sys.arr.x[1])), - ("arr₊xˍt(t)[1]", D(sys.arr.x[1])), - ("arr.x(t)ˍt", D(sys.arr.x)), - ("arr₊x(t)ˍt", D(sys.arr.x)), - ("arr.x(t)ˍt[1]", D(sys.arr.x[1])), - ("arr₊x(t)ˍt[1]", D(sys.arr.x[1])), + ("arr.xˍ$iv", D(sys.arr.x)), + ("arr₊xˍ$iv", D(sys.arr.x)), + ("arr.xˍ$iv($iv)", D(sys.arr.x)), + ("arr₊xˍ$iv($iv)", D(sys.arr.x)), + ("arr.xˍ$iv[1]", D(sys.arr.x[1])), + ("arr₊xˍ$iv[1]", D(sys.arr.x[1])), + ("arr.xˍ$iv($iv)[1]", D(sys.arr.x[1])), + ("arr₊xˍ$iv($iv)[1]", D(sys.arr.x[1])), + ("arr.x($iv)ˍ$iv", D(sys.arr.x)), + ("arr₊x($iv)ˍ$iv", D(sys.arr.x)), + ("arr.x($iv)ˍ$iv[1]", D(sys.arr.x[1])), + ("arr₊x($iv)ˍ$iv[1]", D(sys.arr.x[1])), # array parameter ("arr.p", sys.arr.p), ("arr₊p", sys.arr.p), ("arr.p[1, 2]", sys.arr.p[1, 2]), ("arr₊p[1, 2]", sys.arr.p[1, 2]) ] - isequal(parse_variable(sys, str), var) + @test isequal(parse_variable(sys, str), var) end end From 2186298df1c35bffe38ea34be5ebcd097441486f Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 20 Nov 2024 18:24:42 +0530 Subject: [PATCH 0378/2176] feat: support `Differential(t)` syntax in `parse_variable` --- src/systems/abstractsystem.jl | 15 ++++++++++++--- test/variable_utils.jl | 10 ++++++++++ 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index 36bd5ac800..37a7a74f2d 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -3292,7 +3292,7 @@ Return the variable in `sys` referred to by its string representation `str`. Roughly supports the following CFG: ``` -varname = "D(" varname ")" | arrvar | maybe_dummy_var +varname = "D(" varname ")" | "Differential(" iv ")(" varname ")" | arrvar | maybe_dummy_var arrvar = maybe_dummy_var "[idxs...]" idxs = int | int "," idxs maybe_dummy_var = namespacedvar | namespacedvar "(" iv ")" | @@ -3310,9 +3310,18 @@ function parse_variable(sys::AbstractSystem, str::AbstractString) # I'd write a regex to validate `str`, but https://xkcd.com/1171/ str = strip(str) derivative_level = 0 - while startswith(str, "D(") && endswith(str, ")") + while ((cond1 = startswith(str, "D(")) || startswith(str, "Differential(")) && endswith(str, ")") + if cond1 + derivative_level += 1 + str = _string_view_inner(str, 2, 1) + continue + end + _tmpstr = _string_view_inner(str, 13, 1) + if !startswith(_tmpstr, "$iv)(") + throw(ArgumentError("Expected differential with respect to independent variable $iv in $str")) + end derivative_level += 1 - str = _string_view_inner(str, 2, 1) + str = _string_view_inner(_tmpstr, length(iv) + 2, 0) end arr_idxs = nothing diff --git a/test/variable_utils.jl b/test/variable_utils.jl index ebbc3c0d3b..ecc2421955 100644 --- a/test/variable_utils.jl +++ b/test/variable_utils.jl @@ -88,6 +88,10 @@ end ("D(😄.x($iv))", D(sys.😄.x)), ("D(😄₊x)", D(sys.😄.x)), ("D(😄₊x($iv))", D(sys.😄.x)), + ("Differential($iv)(😄.x)", D(sys.😄.x)), + ("Differential($iv)(😄.x($iv))", D(sys.😄.x)), + ("Differential($iv)(😄₊x)", D(sys.😄.x)), + ("Differential($iv)(😄₊x($iv))", D(sys.😄.x)), # other derivative ("😄.xˍ$iv", D(sys.😄.x)), ("😄.x($iv)ˍ$iv", D(sys.😄.x)), @@ -113,6 +117,12 @@ end ("D(arr₊x[1])", D(sys.arr.x[1])), ("D(arr.x($iv)[1])", D(sys.arr.x[1])), ("D(arr₊x($iv)[1])", D(sys.arr.x[1])), + ("Differential($iv)(arr.x($iv))", D(sys.arr.x)), + ("Differential($iv)(arr₊x($iv))", D(sys.arr.x)), + ("Differential($iv)(arr.x[1])", D(sys.arr.x[1])), + ("Differential($iv)(arr₊x[1])", D(sys.arr.x[1])), + ("Differential($iv)(arr.x($iv)[1])", D(sys.arr.x[1])), + ("Differential($iv)(arr₊x($iv)[1])", D(sys.arr.x[1])), # other derivative ("arr.xˍ$iv", D(sys.arr.x)), ("arr₊xˍ$iv", D(sys.arr.x)), From 72009993b81bc0e7908e35c20775b0c03589beb1 Mon Sep 17 00:00:00 2001 From: Fredrik Bagge Carlson Date: Wed, 20 Nov 2024 18:39:08 +0100 Subject: [PATCH 0379/2176] rm lgamma --- ext/MTKInfiniteOptExt.jl | 16 +++++++++++----- test/odesystem.jl | 2 +- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/ext/MTKInfiniteOptExt.jl b/ext/MTKInfiniteOptExt.jl index 79463f7e71..3b482a1f03 100644 --- a/ext/MTKInfiniteOptExt.jl +++ b/ext/MTKInfiniteOptExt.jl @@ -3,18 +3,24 @@ import ModelingToolkit import SymbolicUtils import NaNMath import InfiniteOpt +import InfiniteOpt: JuMP, GeneralVariableRef # This file contains method definitions to make it possible to trace through functions generated by MTK using JuMP variables -for ff in [acos, log1p, acosh, log2, asin, lgamma, tan, atanh, cos, log, sin, log10, sqrt] +for ff in [acos, log1p, acosh, log2, asin, tan, atanh, cos, log, sin, log10, sqrt] f = nameof(ff) # These need to be defined so that JuMP can trace through functions built by Symbolics @eval NaNMath.$f(x::GeneralVariableRef) = Base.$f(x) end # JuMP variables and Symbolics variables never compare equal. When tracing through dynamics, a function argument can be either a JuMP variable or A Symbolics variable, it can never be both. -Base.isequal(::SymbolicUtils.BasicSymbolic{Real}, ::InfiniteOpt.GeneralVariableRef) = false -Base.isequal(::InfiniteOpt.GeneralVariableRef, ::SymbolicUtils.BasicSymbolic{Real}) = false -Base.isequal(::SymbolicUtils.Symbolic, ::Union{JuMP.GenericAffExpr, JuMP.GenericQuadExpr, InfiniteOpt.AbstractInfOptExpr}) = false -Base.isequal(::Union{JuMP.GenericAffExpr, JuMP.GenericQuadExpr, InfiniteOpt.AbstractInfOptExpr}, ::SymbolicUtils.Symbolic) = false +function Base.isequal(::SymbolicUtils.Symbolic, + ::Union{JuMP.GenericAffExpr, JuMP.GenericQuadExpr, InfiniteOpt.AbstractInfOptExpr}) + false +end +function Base.isequal( + ::Union{JuMP.GenericAffExpr, JuMP.GenericQuadExpr, InfiniteOpt.AbstractInfOptExpr}, + ::SymbolicUtils.Symbolic) + false +end end diff --git a/test/odesystem.jl b/test/odesystem.jl index 90d1c4c578..02eb80ef85 100644 --- a/test/odesystem.jl +++ b/test/odesystem.jl @@ -1521,6 +1521,6 @@ end @parameters p[1:2] = [1.0, 2.0] @mtkbuild sys = ODESystem([D(x) ~ x, y^2 ~ x + sum(p)], t) prob = DAEProblem(sys, [D(x) => x, D(y) => D(x) / 2y], [], (0.0, 1.0)) - sol = solve(prob, DFBDF(), abstol=1e-8, reltol=1e-8) + sol = solve(prob, DFBDF(), abstol = 1e-8, reltol = 1e-8) @test sol[x]≈sol[y^2 - sum(p)] atol=1e-5 end From e3e20cfeacce1adb67c24a5240df741b813823b6 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 22 Nov 2024 14:26:33 +0530 Subject: [PATCH 0380/2176] build: bump minor version --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index e1bcbef173..dcad7c5e4d 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "ModelingToolkit" uuid = "961ee093-0014-501f-94e3-6117800e7a78" authors = ["Yingbo Ma ", "Chris Rackauckas and contributors"] -version = "9.52.0" +version = "9.53.0" [deps] AbstractTrees = "1520ce14-60c1-5f80-bbc7-55ef81b5835c" From 9733460668a662b41fd4cf87102d08521c1e4f92 Mon Sep 17 00:00:00 2001 From: vyudu Date: Fri, 22 Nov 2024 13:25:30 -0500 Subject: [PATCH 0381/2176] init --- src/systems/diffeqs/abstractodesystem.jl | 110 +++++++++++++++++++++++ 1 file changed, 110 insertions(+) diff --git a/src/systems/diffeqs/abstractodesystem.jl b/src/systems/diffeqs/abstractodesystem.jl index 8d9e0b5381..2579ebafcf 100644 --- a/src/systems/diffeqs/abstractodesystem.jl +++ b/src/systems/diffeqs/abstractodesystem.jl @@ -466,6 +466,116 @@ function DiffEqBase.ODEFunction{iip, specialize}(sys::AbstractODESystem, initializeprobpmap = initializeprobpmap) end +""" +```julia +SciMLBase.BVProblem{iip}(sys::AbstractODESystem, u0map, tspan, + parammap = DiffEqBase.NullParameters(); + version = nothing, tgrad = false, + jac = true, sparse = true, + simplify = false, + kwargs...) where {iip} +``` + +Create a `BVProblem` from the [`ODESystem`](@ref). The arguments `dvs` and +`ps` are used to set the order of the dependent variable and parameter vectors, +respectively. +""" +function SciMLBase.BVProblem(sys::AbstractODESystem, args...; kwargs...) + BVProblem{true}(sys, args...; kwargs...) +end + +function SciMLBase.BVProblem(sys::AbstractODESystem, + u0map::StaticArray, + args...; + kwargs...) + BVProblem{false, SciMLBase.FullSpecialize}(sys, u0map, args...; kwargs...) +end + +function SciMLBase.BVProblem{true}(sys::AbstractODESystem, args...; kwargs...) + BVProblem{true, SciMLBase.AutoSpecialize}(sys, args...; kwargs...) +end + +function SciMLBase.BVProblem{false}(sys::AbstractODESystem, args...; kwargs...) + BVProblem{false, SciMLBase.FullSpecialize}(sys, args...; kwargs...) +end + +function SciMLBase.BVProblem{iip, specialize}(sys::AbstractODESystem, u0map = [], + tspan = get_tspan(sys), + parammap = DiffEqBase.NullParameters(); + version = nothing, tgrad = false, + jac = true, sparse = true, + sparsity = true, + callback = nothing, + check_length = true, + warn_initialize_determined = true, + eval_expression = false, + eval_module = @__MODULE__, + kwargs...) where {iip, specialize} + + if !iscomplete(sys) + error("A completed system is required. Call `complete` or `structural_simplify` on the system before creating an `BVProblem`") + end + + f, u0, p = process_SciMLProblem(ODEFunction{iip, specialize}, sys, u0map, parammap; + t = tspan !== nothing ? tspan[1] : tspan, + check_length, warn_initialize_determined, eval_expression, eval_module, jac, sparse, sparsity, kwargs...) + + # if jac + # jac_gen = generate_jacobian(sys, dvs, ps; + # simplify = simplify, sparse = sparse, + # expression = Val{true}, + # expression_module = eval_module, + # checkbounds = checkbounds, kwargs...) + # jac_oop, jac_iip = eval_or_rgf.(jac_gen; eval_expression, eval_module) + + # _jac(u, p, t) = jac_oop(u, p, t) + # _jac(J, u, p, t) = jac_iip(J, u, p, t) + # _jac(u, p::Tuple{Vararg{Number}}, t) = jac_oop(u, p, t) + # _jac(J, u, p::Tuple{Vararg{Number}}, t) = jac_iip(J, u, p, t) + # _jac(u, p::Tuple, t) = jac_oop(u, p..., t) + # _jac(J, u, p::Tuple, t) = jac_iip(J, u, p..., t) + # _jac(u, p::MTKParameters, t) = jac_oop(u, p..., t) + # _jac(J, u, p::MTKParameters, t) = jac_iip(J, u, p..., t) + # else + # _jac = nothing + # end + + # jac_prototype = if sparse + # uElType = u0 === nothing ? Float64 : eltype(u0) + # if jac + # similar(calculate_jacobian(sys, sparse = sparse), uElType) + # else + # similar(jacobian_sparsity(sys), uElType) + # end + # else + # nothing + # end + + # f.jac = _jac + # f.jac_prototype = jac_prototype + # f.sparsity = jac ? jacobian_sparsity(sys) : nothing + + cbs = process_events(sys; callback, eval_expression, eval_module, kwargs...) + + kwargs = filter_kwargs(kwargs) + + kwargs1 = (;) + if cbs !== nothing + kwargs1 = merge(kwargs1, (callback = cbs,)) + end + + # Define the boundary conditions + bc = if iip + (residual, u, p, t) -> (residual = u[1] - u0) + else + (u, p, t) -> (u[1] - u0) + end + + return BVProblem{iip}(f, bc, u0, tspan, p; kwargs1..., kwargs...) +end + +get_callback(prob::BVProblem) = prob.kwargs[:callback] + """ ```julia DiffEqBase.DAEFunction{iip}(sys::AbstractODESystem, dvs = unknowns(sys), From d0748e6467285627531071e4377f43c9489cebfa Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 25 Nov 2024 13:04:00 +0530 Subject: [PATCH 0382/2176] fix: fix nonnumeric parameters in `@mtkmodel` --- src/systems/model_parsing.jl | 2 +- test/model_parsing.jl | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/systems/model_parsing.jl b/src/systems/model_parsing.jl index 5b79eaa91b..5402659b1c 100644 --- a/src/systems/model_parsing.jl +++ b/src/systems/model_parsing.jl @@ -923,7 +923,7 @@ function parse_variable_arg(dict, mod, arg, varclass, kwargs, where_types) end push!(varexpr.args, metadata_expr) - return vv isa Num ? name : :($name...), varexpr + return symbolic_type(vv) == ScalarSymbolic() ? name : :($name...), varexpr else return vv end diff --git a/test/model_parsing.jl b/test/model_parsing.jl index fd223800e3..6ab7636b32 100644 --- a/test/model_parsing.jl +++ b/test/model_parsing.jl @@ -979,3 +979,14 @@ end @test MultipleExtend.structure[:extend][1] == [:inmodel, :b, :inmodel_b] @test tosymbol.(parameters(multiple_extend)) == [:b, :inmodel_b₊p, :inmodel₊p] end + +struct CustomStruct end +@testset "Nonnumeric parameters" begin + @mtkmodel MyModel begin + @parameters begin + p::CustomStruct + end + end + @named sys = MyModel(p = CustomStruct()) + @test ModelingToolkit.defaults(sys)[@nonamespace sys.p] == CustomStruct() +end From 472921dc215bd2ecf8785c4ca5a630e9095e42c5 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 25 Nov 2024 14:31:12 +0530 Subject: [PATCH 0383/2176] fix: support callable parameters provided to discretes list of callback --- src/systems/index_cache.jl | 29 +++++++++++++++++++---------- test/index_cache.jl | 26 ++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 10 deletions(-) diff --git a/src/systems/index_cache.jl b/src/systems/index_cache.jl index 1e3490ee69..ab0dd08764 100644 --- a/src/systems/index_cache.jl +++ b/src/systems/index_cache.jl @@ -40,10 +40,12 @@ const TunableIndexMap = Dict{BasicSymbolic, Union{Int, UnitRange{Int}, Base.ReshapedArray{Int, N, UnitRange{Int}} where {N}}} const TimeseriesSetType = Set{Union{ContinuousTimeseries, Int}} +const SymbolicParam = Union{BasicSymbolic, CallWithMetadata} + struct IndexCache unknown_idx::UnknownIndexMap # sym => (bufferidx, idx_in_buffer) - discrete_idx::Dict{BasicSymbolic, DiscreteIndex} + discrete_idx::Dict{SymbolicParam, DiscreteIndex} # sym => (clockidx, idx_in_clockbuffer) callback_to_clocks::Dict{Any, Vector{Int}} tunable_idx::TunableIndexMap @@ -56,13 +58,13 @@ struct IndexCache tunable_buffer_size::BufferTemplate constant_buffer_sizes::Vector{BufferTemplate} nonnumeric_buffer_sizes::Vector{BufferTemplate} - symbol_to_variable::Dict{Symbol, Union{BasicSymbolic, CallWithMetadata}} + symbol_to_variable::Dict{Symbol, SymbolicParam} end function IndexCache(sys::AbstractSystem) unks = solved_unknowns(sys) unk_idxs = UnknownIndexMap() - symbol_to_variable = Dict{Symbol, Union{BasicSymbolic, CallWithMetadata}}() + symbol_to_variable = Dict{Symbol, SymbolicParam}() let idx = 1 for sym in unks @@ -95,7 +97,7 @@ function IndexCache(sys::AbstractSystem) tunable_buffers = Dict{Any, Set{BasicSymbolic}}() constant_buffers = Dict{Any, Set{BasicSymbolic}}() - nonnumeric_buffers = Dict{Any, Set{Union{BasicSymbolic, CallWithMetadata}}}() + nonnumeric_buffers = Dict{Any, Set{SymbolicParam}}() function insert_by_type!(buffers::Dict{Any, S}, sym, ctype) where {S} sym = unwrap(sym) @@ -103,10 +105,10 @@ function IndexCache(sys::AbstractSystem) push!(buf, sym) end - disc_param_callbacks = Dict{BasicSymbolic, Set{Int}}() + disc_param_callbacks = Dict{SymbolicParam, Set{Int}}() events = vcat(continuous_events(sys), discrete_events(sys)) for (i, event) in enumerate(events) - discs = Set{BasicSymbolic}() + discs = Set{SymbolicParam}() affs = affects(event) if !(affs isa AbstractArray) affs = [affs] @@ -130,26 +132,32 @@ function IndexCache(sys::AbstractSystem) isequal(only(arguments(sym)), get_iv(sys)) clocks = get!(() -> Set{Int}(), disc_param_callbacks, sym) push!(clocks, i) - else + elseif is_variable_floatingpoint(sym) insert_by_type!(constant_buffers, sym, symtype(sym)) + else + stype = symtype(sym) + if stype <: FnType + stype = fntype_to_function_type(stype) + end + insert_by_type!(nonnumeric_buffers, sym, stype) end end end clock_partitions = unique(collect(values(disc_param_callbacks))) disc_symtypes = unique(symtype.(keys(disc_param_callbacks))) disc_symtype_idx = Dict(disc_symtypes .=> eachindex(disc_symtypes)) - disc_syms_by_symtype = [BasicSymbolic[] for _ in disc_symtypes] + disc_syms_by_symtype = [SymbolicParam[] for _ in disc_symtypes] for sym in keys(disc_param_callbacks) push!(disc_syms_by_symtype[disc_symtype_idx[symtype(sym)]], sym) end - disc_syms_by_symtype_by_partition = [Vector{BasicSymbolic}[] for _ in disc_symtypes] + disc_syms_by_symtype_by_partition = [Vector{SymbolicParam}[] for _ in disc_symtypes] for (i, buffer) in enumerate(disc_syms_by_symtype) for partition in clock_partitions push!(disc_syms_by_symtype_by_partition[i], [sym for sym in buffer if disc_param_callbacks[sym] == partition]) end end - disc_idxs = Dict{BasicSymbolic, DiscreteIndex}() + disc_idxs = Dict{SymbolicParam, DiscreteIndex}() callback_to_clocks = Dict{ Union{SymbolicContinuousCallback, SymbolicDiscreteCallback}, Set{Int}}() for (typei, disc_syms_by_partition) in enumerate(disc_syms_by_symtype_by_partition) @@ -191,6 +199,7 @@ function IndexCache(sys::AbstractSystem) end haskey(disc_idxs, p) && continue haskey(constant_buffers, ctype) && p in constant_buffers[ctype] && continue + haskey(nonnumeric_buffers, ctype) && p in nonnumeric_buffers[ctype] && continue insert_by_type!( if ctype <: Real || ctype <: AbstractArray{<:Real} if istunable(p, true) && Symbolics.shape(p) != Symbolics.Unknown() && diff --git a/test/index_cache.jl b/test/index_cache.jl index c479075797..455203d759 100644 --- a/test/index_cache.jl +++ b/test/index_cache.jl @@ -92,3 +92,29 @@ end reorder_dimension_by_tunables!(dst, sys, src, [r, q, p]; dim = 2) @test dst ≈ stack([vcat(4ones(4), 3ones(3), 1.0) for i in 1:5]; dims = 1) end + +mutable struct ParamTest + y::Any +end +(pt::ParamTest)(x) = pt.y - x +@testset "Issue#3215: Callable discrete parameter" begin + function update_affect!(integ, u, p, ctx) + integ.p[p.p_1].y = integ.t + end + + tp1 = typeof(ParamTest(1)) + @parameters (p_1::tp1)(..) = ParamTest(1) + @variables x(ModelingToolkit.t_nounits) = 0 + + event1 = [1.0, 2, 3] => (update_affect!, [], [p_1], [p_1], nothing) + + @named sys = ODESystem([ + ModelingToolkit.D_nounits(x) ~ p_1(x) + ], + ModelingToolkit.t_nounits; + discrete_events = [event1] + ) + ss = @test_nowarn complete(sys) + @test length(parameters(ss)) == 1 + @test !is_timeseries_parameter(ss, p_1) +end From 585de359502db53e50609d656e97d3696e2d6300 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 25 Nov 2024 15:05:31 +0530 Subject: [PATCH 0384/2176] fix: fix SDEs with noise dependent on observed variables --- .../symbolics_tearing.jl | 8 ++++++++ src/systems/systems.jl | 2 ++ test/sdesystem.jl | 19 +++++++++++++++++++ 3 files changed, 29 insertions(+) diff --git a/src/structural_transformation/symbolics_tearing.jl b/src/structural_transformation/symbolics_tearing.jl index a854acb9b1..8161a9572b 100644 --- a/src/structural_transformation/symbolics_tearing.jl +++ b/src/structural_transformation/symbolics_tearing.jl @@ -88,6 +88,14 @@ function tearing_sub(expr, dict, s) s ? simplify(expr) : expr end +function tearing_substitute_expr(sys::AbstractSystem, expr; simplify = false) + empty_substitutions(sys) && return expr + substitutions = get_substitutions(sys) + @unpack subs = substitutions + solved = Dict(eq.lhs => eq.rhs for eq in subs) + return tearing_sub(expr, solved, simplify) +end + """ $(TYPEDSIGNATURES) diff --git a/src/systems/systems.jl b/src/systems/systems.jl index 848980605f..4205a4d207 100644 --- a/src/systems/systems.jl +++ b/src/systems/systems.jl @@ -152,6 +152,8 @@ function __structural_simplify(sys::AbstractSystem, io = nothing; simplify = fal noise_eqs = sorted_g_rows is_scalar_noise = false end + + noise_eqs = StructuralTransformations.tearing_substitute_expr(ode_sys, noise_eqs) return SDESystem(full_equations(ode_sys), noise_eqs, get_iv(ode_sys), unknowns(ode_sys), parameters(ode_sys); name = nameof(ode_sys), is_scalar_noise) diff --git a/test/sdesystem.jl b/test/sdesystem.jl index c258a4142b..78d7a0418b 100644 --- a/test/sdesystem.jl +++ b/test/sdesystem.jl @@ -780,3 +780,22 @@ end prob = @test_nowarn SDEProblem(sys, nothing, (0.0, 1.0)) @test_nowarn solve(prob, ImplicitEM()) end + +@testset "Issue#3212: Noise dependent on observed" begin + sts = @variables begin + x(t) = 1.0 + input(t) + [input = true] + end + ps = @parameters a = 2 + @brownian η + + eqs = [D(x) ~ -a * x + (input + 1) * η + input ~ 0.0] + + sys = System(eqs, t, sts, ps; name = :name) + sys = structural_simplify(sys) + @test ModelingToolkit.get_noiseeqs(sys) ≈ [1.0] + prob = SDEProblem(sys, [], (0.0, 1.0), []) + @test_nowarn solve(prob, RKMil()) +end From 398849613e4fd4bc7f9bcdc0960d54c2bb2c270e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Miclu=C8=9Ba-C=C3=A2mpeanu?= Date: Mon, 25 Nov 2024 13:06:07 +0200 Subject: [PATCH 0385/2176] fix: fix InitializationProblems that have vectors in u0map --- src/systems/diffeqs/abstractodesystem.jl | 1 + 1 file changed, 1 insertion(+) diff --git a/src/systems/diffeqs/abstractodesystem.jl b/src/systems/diffeqs/abstractodesystem.jl index 1dc2198cc2..18aac952f4 100644 --- a/src/systems/diffeqs/abstractodesystem.jl +++ b/src/systems/diffeqs/abstractodesystem.jl @@ -1356,6 +1356,7 @@ function InitializationProblem{iip, specialize}(sys::AbstractODESystem, u0T = promote_type(u0T, typeof(fullmap[eq.lhs])) end if u0T != Union{} + u0T = eltype(u0T) u0map = Dict(k => if symbolic_type(v) == NotSymbolic() && !is_array_of_symbolics(v) v isa AbstractArray ? u0T.(v) : u0T(v) else From d4e0d0f1634d6b322ef0cfa4ee110e0f9d7cd693 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 25 Nov 2024 12:37:39 +0530 Subject: [PATCH 0386/2176] feat: enable `structural_simplify` to automatically handle non-fully-determined systems Uses the result of `check_consistency` --- src/structural_transformation/utils.jl | 58 +++++++++++++++++++------- src/systems/systemstructure.jl | 9 +++- 2 files changed, 49 insertions(+), 18 deletions(-) diff --git a/src/structural_transformation/utils.jl b/src/structural_transformation/utils.jl index 8b2ac1f69c..405a3ebed3 100644 --- a/src/structural_transformation/utils.jl +++ b/src/structural_transformation/utils.jl @@ -58,7 +58,41 @@ end ### ### Structural check ### -function check_consistency(state::TransformationState, orig_inputs) + +""" + $(TYPEDSIGNATURES) + +Check if the `state` represents a singular system, and return the unmatched variables. +""" +function singular_check(state::TransformationState) + @unpack graph, var_to_diff = state.structure + fullvars = get_fullvars(state) + # This is defined to check if Pantelides algorithm terminates. For more + # details, check the equation (15) of the original paper. + extended_graph = (@set graph.fadjlist = Vector{Int}[graph.fadjlist; + map(collect, edges(var_to_diff))]) + extended_var_eq_matching = maximal_matching(extended_graph) + + nvars = ndsts(graph) + unassigned_var = [] + for (vj, eq) in enumerate(extended_var_eq_matching) + vj > nvars && break + if eq === unassigned && !isempty(𝑑neighbors(graph, vj)) + push!(unassigned_var, fullvars[vj]) + end + end + return unassigned_var +end + +""" + $(TYPEDSIGNATURES) + +Check the consistency of `state`, given the inputs `orig_inputs`. If `nothrow == false`, +throws an error if the system is under-/over-determined or singular. In this case, if the +function returns it will return `true`. If `nothrow == true`, it will return `false` +instead of throwing an error. The singular case will print a warning. +""" +function check_consistency(state::TransformationState, orig_inputs; nothrow = false) fullvars = get_fullvars(state) neqs = n_concrete_eqs(state) @unpack graph, var_to_diff = state.structure @@ -72,6 +106,7 @@ function check_consistency(state::TransformationState, orig_inputs) is_balanced = n_highest_vars == neqs if neqs > 0 && !is_balanced + nothrow && return false varwhitelist = var_to_diff .== nothing var_eq_matching = maximal_matching(graph, eq -> true, v -> varwhitelist[v]) # not assigned # Just use `error_reporting` to do conditional @@ -85,20 +120,7 @@ function check_consistency(state::TransformationState, orig_inputs) error_reporting(state, bad_idxs, n_highest_vars, iseqs, orig_inputs) end - # This is defined to check if Pantelides algorithm terminates. For more - # details, check the equation (15) of the original paper. - extended_graph = (@set graph.fadjlist = Vector{Int}[graph.fadjlist; - map(collect, edges(var_to_diff))]) - extended_var_eq_matching = maximal_matching(extended_graph) - - nvars = ndsts(graph) - unassigned_var = [] - for (vj, eq) in enumerate(extended_var_eq_matching) - vj > nvars && break - if eq === unassigned && !isempty(𝑑neighbors(graph, vj)) - push!(unassigned_var, fullvars[vj]) - end - end + unassigned_var = singular_check(state) if !isempty(unassigned_var) || !is_balanced io = IOBuffer() @@ -107,10 +129,14 @@ function check_consistency(state::TransformationState, orig_inputs) errmsg = "The system is structurally singular! " * "Here are the problematic variables: \n" * unassigned_var_str + if nothrow + @warn errmsg + return false + end throw(InvalidSystemException(errmsg)) end - return nothing + return true end ### diff --git a/src/systems/systemstructure.jl b/src/systems/systemstructure.jl index 71bdf6fe30..1c0ad8b8ae 100644 --- a/src/systems/systemstructure.jl +++ b/src/systems/systemstructure.jl @@ -677,7 +677,11 @@ function _structural_simplify!(state::TearingState, io; simplify = false, check_consistency = true, fully_determined = true, warn_initialize_determined = false, dummy_derivative = true, kwargs...) - check_consistency &= fully_determined + if fully_determined isa Bool + check_consistency &= fully_determined + else + check_consistency = true + end has_io = io !== nothing orig_inputs = Set() if has_io @@ -690,7 +694,8 @@ function _structural_simplify!(state::TearingState, io; simplify = false, end sys, mm = ModelingToolkit.alias_elimination!(state; kwargs...) if check_consistency - ModelingToolkit.check_consistency(state, orig_inputs) + fully_determined = ModelingToolkit.check_consistency( + state, orig_inputs; nothrow = fully_determined === nothing) end if fully_determined && dummy_derivative sys = ModelingToolkit.dummy_derivative( From beb16ec624e1f68a834a0aff7e7731c489e6fabc Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 25 Nov 2024 12:38:02 +0530 Subject: [PATCH 0387/2176] feat: simplify initialization system with `fully_determined=nothing` by default --- src/systems/diffeqs/abstractodesystem.jl | 6 +++++- src/systems/problem_utils.jl | 2 +- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/systems/diffeqs/abstractodesystem.jl b/src/systems/diffeqs/abstractodesystem.jl index 1dc2198cc2..4019a868d8 100644 --- a/src/systems/diffeqs/abstractodesystem.jl +++ b/src/systems/diffeqs/abstractodesystem.jl @@ -1295,7 +1295,7 @@ function InitializationProblem{iip, specialize}(sys::AbstractODESystem, check_length = true, warn_initialize_determined = true, initialization_eqs = [], - fully_determined = false, + fully_determined = nothing, check_units = true, kwargs...) where {iip, specialize} if !iscomplete(sys) @@ -1313,6 +1313,10 @@ function InitializationProblem{iip, specialize}(sys::AbstractODESystem, sys; u0map, initialization_eqs, check_units, pmap = parammap); fully_determined) end + if !isempty(StructuralTransformations.singular_check(get_tearing_state(isys))) + @warn "Since the initialization system is singular, the guess values may significantly affect the initial values of the ODE" + end + uninit = setdiff(unknowns(sys), [unknowns(isys); getfield.(observed(isys), :lhs)]) # TODO: throw on uninitialized arrays diff --git a/src/systems/problem_utils.jl b/src/systems/problem_utils.jl index ff1d69e2bc..1cf110df13 100644 --- a/src/systems/problem_utils.jl +++ b/src/systems/problem_utils.jl @@ -498,7 +498,7 @@ function process_SciMLProblem( constructor, sys::AbstractSystem, u0map, pmap; build_initializeprob = true, implicit_dae = false, t = nothing, guesses = AnyDict(), warn_initialize_determined = true, initialization_eqs = [], - eval_expression = false, eval_module = @__MODULE__, fully_determined = false, + eval_expression = false, eval_module = @__MODULE__, fully_determined = nothing, check_initialization_units = false, tofloat = true, use_union = false, u0_constructor = identity, du0map = nothing, check_length = true, symbolic_u0 = false, warn_cyclic_dependency = false, From 6632a2995ad99e129622da4c4910e0a36d2c93da Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 25 Nov 2024 12:38:13 +0530 Subject: [PATCH 0388/2176] test: test that singular initialization systems throw a warning --- test/initializationsystem.jl | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/test/initializationsystem.jl b/test/initializationsystem.jl index f39ec3c2f2..6d3e677c1e 100644 --- a/test/initializationsystem.jl +++ b/test/initializationsystem.jl @@ -947,3 +947,14 @@ end @test_nowarn remake(prob, p = prob.p) end + +@testset "Singular initialization prints a warning" begin + @parameters g + @variables x(t) y(t) [state_priority = 10] λ(t) + eqs = [D(D(x)) ~ λ * x + D(D(y)) ~ λ * y - g + x^2 + y^2 ~ 1] + @mtkbuild pend = ODESystem(eqs, t) + @test_warn ["structurally singular", "initialization", "guess"] ODEProblem( + pend, [x => 1, y => 0], (0.0, 1.5), [g => 1], guesses = [λ => 1]) +end From 1892f0a10900e124587ab034e5842a1f4ec565da Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 25 Nov 2024 12:43:28 +0530 Subject: [PATCH 0389/2176] refactor: format --- src/systems/abstractsystem.jl | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index 497b5e6476..3a9bbd33e1 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -3335,7 +3335,8 @@ function parse_variable(sys::AbstractSystem, str::AbstractString) # I'd write a regex to validate `str`, but https://xkcd.com/1171/ str = strip(str) derivative_level = 0 - while ((cond1 = startswith(str, "D(")) || startswith(str, "Differential(")) && endswith(str, ")") + while ((cond1 = startswith(str, "D(")) || startswith(str, "Differential(")) && + endswith(str, ")") if cond1 derivative_level += 1 str = _string_view_inner(str, 2, 1) From 2b7a6b695a33a09634a6039f27eabf414f2c7530 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 25 Nov 2024 18:03:16 +0530 Subject: [PATCH 0390/2176] refactor: only display singular warning if `warn_initialize_determined` --- src/structural_transformation/utils.jl | 7 +++---- src/systems/diffeqs/abstractodesystem.jl | 13 +++++++++++-- test/initializationsystem.jl | 2 +- 3 files changed, 15 insertions(+), 7 deletions(-) diff --git a/src/structural_transformation/utils.jl b/src/structural_transformation/utils.jl index 405a3ebed3..bd24a1d017 100644 --- a/src/structural_transformation/utils.jl +++ b/src/structural_transformation/utils.jl @@ -123,16 +123,15 @@ function check_consistency(state::TransformationState, orig_inputs; nothrow = fa unassigned_var = singular_check(state) if !isempty(unassigned_var) || !is_balanced + if nothrow + return false + end io = IOBuffer() Base.print_array(io, unassigned_var) unassigned_var_str = String(take!(io)) errmsg = "The system is structurally singular! " * "Here are the problematic variables: \n" * unassigned_var_str - if nothrow - @warn errmsg - return false - end throw(InvalidSystemException(errmsg)) end diff --git a/src/systems/diffeqs/abstractodesystem.jl b/src/systems/diffeqs/abstractodesystem.jl index 4019a868d8..5c13572aef 100644 --- a/src/systems/diffeqs/abstractodesystem.jl +++ b/src/systems/diffeqs/abstractodesystem.jl @@ -1313,8 +1313,17 @@ function InitializationProblem{iip, specialize}(sys::AbstractODESystem, sys; u0map, initialization_eqs, check_units, pmap = parammap); fully_determined) end - if !isempty(StructuralTransformations.singular_check(get_tearing_state(isys))) - @warn "Since the initialization system is singular, the guess values may significantly affect the initial values of the ODE" + ts = get_tearing_state(isys) + if warn_initialize_determined && + (unassigned_vars = StructuralTransformations.singular_check(ts); !isempty(unassigned_vars)) + errmsg = """ + The initialization system is structurally singular. Guess values may \ + significantly affect the initial values of the ODE. The problematic variables \ + are $unassigned_vars. + + Note that the identification of problematic variables is a best-effort heuristic. + """ + @warn errmsg end uninit = setdiff(unknowns(sys), [unknowns(isys); getfield.(observed(isys), :lhs)]) diff --git a/test/initializationsystem.jl b/test/initializationsystem.jl index 6d3e677c1e..920a73a723 100644 --- a/test/initializationsystem.jl +++ b/test/initializationsystem.jl @@ -955,6 +955,6 @@ end D(D(y)) ~ λ * y - g x^2 + y^2 ~ 1] @mtkbuild pend = ODESystem(eqs, t) - @test_warn ["structurally singular", "initialization", "guess"] ODEProblem( + @test_warn ["structurally singular", "initialization", "Guess", "heuristic"] ODEProblem( pend, [x => 1, y => 0], (0.0, 1.5), [g => 1], guesses = [λ => 1]) end From 9c7fbc7fda33b687553f51549af2e66f536cc407 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 22 Nov 2024 14:23:43 +0530 Subject: [PATCH 0391/2176] test: make initializaton tests runnable from OrdinaryDiffEq --- test/runtests.jl | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/test/runtests.jl b/test/runtests.jl index 192ead935c..0ba23fc4f7 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -33,7 +33,6 @@ end @safetestset "Dynamic Quantities Test" include("dq_units.jl") @safetestset "Unitful Quantities Test" include("units.jl") @safetestset "Mass Matrix Test" include("mass_matrix.jl") - @safetestset "InitializationSystem Test" include("initializationsystem.jl") @safetestset "Guess Propagation" include("guess_propagation.jl") @safetestset "Hierarchical Initialization Equations" include("hierarchical_initialization_eqs.jl") @safetestset "Reduction Test" include("reduction.jl") @@ -64,6 +63,10 @@ end end end + if GROUP == "All" || GROUP == "InterfaceI" || GROUP == "Initialization" + @safetestset "InitializationSystem Test" include("initializationsystem.jl") + end + if GROUP == "All" || GROUP == "InterfaceII" @testset "InterfaceII" begin @safetestset "IndexCache Test" include("index_cache.jl") From 7eba3438d96d3a0a1c275edd3f61c47e49b2788f Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 26 Nov 2024 17:56:51 +0530 Subject: [PATCH 0392/2176] test: also move guess propagation, hierarchical initialization equations and initial values testsets --- test/runtests.jl | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/runtests.jl b/test/runtests.jl index 0ba23fc4f7..4f6c20e04b 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -33,8 +33,6 @@ end @safetestset "Dynamic Quantities Test" include("dq_units.jl") @safetestset "Unitful Quantities Test" include("units.jl") @safetestset "Mass Matrix Test" include("mass_matrix.jl") - @safetestset "Guess Propagation" include("guess_propagation.jl") - @safetestset "Hierarchical Initialization Equations" include("hierarchical_initialization_eqs.jl") @safetestset "Reduction Test" include("reduction.jl") @safetestset "Split Parameters Test" include("split_parameters.jl") @safetestset "StaticArrays Test" include("static_arrays.jl") @@ -57,14 +55,16 @@ end @safetestset "Constants Test" include("constants.jl") @safetestset "Parameter Dependency Test" include("parameter_dependencies.jl") @safetestset "Generate Custom Function Test" include("generate_custom_function.jl") - @safetestset "Initial Values Test" include("initial_values.jl") @safetestset "Equation Type Accessors Test" include("equation_type_accessors.jl") @safetestset "Equations with complex values" include("complex.jl") end end if GROUP == "All" || GROUP == "InterfaceI" || GROUP == "Initialization" + @safetestset "Guess Propagation" include("guess_propagation.jl") + @safetestset "Hierarchical Initialization Equations" include("hierarchical_initialization_eqs.jl") @safetestset "InitializationSystem Test" include("initializationsystem.jl") + @safetestset "Initial Values Test" include("initial_values.jl") end if GROUP == "All" || GROUP == "InterfaceII" From d73342d458fe6ca0b85fbfb6c6f8cb274dfd5ba0 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 27 Nov 2024 11:27:37 +0530 Subject: [PATCH 0393/2176] test: run `Initialization` as its own test group --- .github/workflows/Tests.yml | 1 + test/runtests.jl | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/workflows/Tests.yml b/.github/workflows/Tests.yml index 609ab2fb3d..a95f19a085 100644 --- a/.github/workflows/Tests.yml +++ b/.github/workflows/Tests.yml @@ -32,6 +32,7 @@ jobs: group: - InterfaceI - InterfaceII + - Initialization - SymbolicIndexingInterface - Extended - Extensions diff --git a/test/runtests.jl b/test/runtests.jl index 4f6c20e04b..06a167bdd9 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -60,7 +60,7 @@ end end end - if GROUP == "All" || GROUP == "InterfaceI" || GROUP == "Initialization" + if GROUP == "All" || GROUP == "Initialization" @safetestset "Guess Propagation" include("guess_propagation.jl") @safetestset "Hierarchical Initialization Equations" include("hierarchical_initialization_eqs.jl") @safetestset "InitializationSystem Test" include("initializationsystem.jl") From 237cb82ef42252c6619179befa918ad580647ee9 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 27 Nov 2024 13:10:31 +0530 Subject: [PATCH 0394/2176] refactor: format --- test/runtests.jl | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test/runtests.jl b/test/runtests.jl index 06a167bdd9..244393d351 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -61,10 +61,10 @@ end end if GROUP == "All" || GROUP == "Initialization" - @safetestset "Guess Propagation" include("guess_propagation.jl") - @safetestset "Hierarchical Initialization Equations" include("hierarchical_initialization_eqs.jl") - @safetestset "InitializationSystem Test" include("initializationsystem.jl") - @safetestset "Initial Values Test" include("initial_values.jl") + @safetestset "Guess Propagation" include("guess_propagation.jl") + @safetestset "Hierarchical Initialization Equations" include("hierarchical_initialization_eqs.jl") + @safetestset "InitializationSystem Test" include("initializationsystem.jl") + @safetestset "Initial Values Test" include("initial_values.jl") end if GROUP == "All" || GROUP == "InterfaceII" From 5b702cab0620e79b92637929bb773ef2a2805a21 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 21 Nov 2024 17:37:24 +0530 Subject: [PATCH 0395/2176] fix: propagate `initializeprobpmap` and `update_initializeprob!` to `DAEFunction` --- src/systems/diffeqs/abstractodesystem.jl | 6 +++++- test/initializationsystem.jl | 17 +++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/systems/diffeqs/abstractodesystem.jl b/src/systems/diffeqs/abstractodesystem.jl index b6d750c621..cbf835f48d 100644 --- a/src/systems/diffeqs/abstractodesystem.jl +++ b/src/systems/diffeqs/abstractodesystem.jl @@ -498,6 +498,8 @@ function DiffEqBase.DAEFunction{iip}(sys::AbstractODESystem, dvs = unknowns(sys) checkbounds = false, initializeprob = nothing, initializeprobmap = nothing, + initializeprobpmap = nothing, + update_initializeprob! = nothing, kwargs...) where {iip} if !iscomplete(sys) error("A completed system is required. Call `complete` or `structural_simplify` on the system before creating a `DAEFunction`") @@ -551,7 +553,9 @@ function DiffEqBase.DAEFunction{iip}(sys::AbstractODESystem, dvs = unknowns(sys) jac_prototype = jac_prototype, observed = observedfun, initializeprob = initializeprob, - initializeprobmap = initializeprobmap) + initializeprobmap = initializeprobmap, + initializeprobpmap = initializeprobpmap, + update_initializeprob! = update_initializeprob!) end function DiffEqBase.DDEFunction(sys::AbstractODESystem, args...; kwargs...) diff --git a/test/initializationsystem.jl b/test/initializationsystem.jl index 920a73a723..770f9f12bd 100644 --- a/test/initializationsystem.jl +++ b/test/initializationsystem.jl @@ -958,3 +958,20 @@ end @test_warn ["structurally singular", "initialization", "Guess", "heuristic"] ODEProblem( pend, [x => 1, y => 0], (0.0, 1.5), [g => 1], guesses = [λ => 1]) end + +@testset "DAEProblem initialization" begin + @variables x(t) [guess = 1.0] y(t) [guess = 1.0] + @parameters p=missing [guess = 1.0] q=missing [guess = 1.0] + @mtkbuild sys = ODESystem( + [D(x) ~ p * y + q * t, x^3 + y^3 ~ 5], t; initialization_eqs = [p^2 + q^3 ~ 3]) + + # FIXME: solve for du0 + prob = DAEProblem( + sys, [D(x) => cbrt(4), D(y) => -1 / cbrt(4)], [x => 1.0], (0.0, 1.0), [p => 1.0]) + + integ = init(prob, DImplicitEuler()) + @test integ[x] ≈ 1.0 + @test integ[y]≈cbrt(4) rtol=1e-6 + @test integ.ps[p] ≈ 1.0 + @test integ.ps[q]≈cbrt(2) rtol=1e-6 +end From 57ea5fc3ffae436d516624a285b928b0d226be51 Mon Sep 17 00:00:00 2001 From: Andrew Leonard Date: Wed, 27 Nov 2024 08:47:15 -0500 Subject: [PATCH 0396/2176] add checks kwarg to OptimizationProblem, use in ConstraintSystem --- src/systems/optimization/optimizationsystem.jl | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/systems/optimization/optimizationsystem.jl b/src/systems/optimization/optimizationsystem.jl index 39c5c1e7a7..176a2bdfed 100644 --- a/src/systems/optimization/optimizationsystem.jl +++ b/src/systems/optimization/optimizationsystem.jl @@ -284,6 +284,7 @@ function DiffEqBase.OptimizationProblem{iip}(sys::OptimizationSystem, u0map, linenumbers = true, parallel = SerialForm(), eval_expression = false, eval_module = @__MODULE__, use_union = false, + checks = true, kwargs...) where {iip} if !iscomplete(sys) error("A completed `OptimizationSystem` is required. Call `complete` or `structural_simplify` on the system before creating a `OptimizationProblem`") @@ -393,7 +394,7 @@ function DiffEqBase.OptimizationProblem{iip}(sys::OptimizationSystem, u0map, observedfun = ObservedFunctionCache(sys; eval_expression, eval_module) if length(cstr) > 0 - @named cons_sys = ConstraintsSystem(cstr, dvs, ps) + @named cons_sys = ConstraintsSystem(cstr, dvs, ps; checks) cons_sys = complete(cons_sys) cons, lcons_, ucons_ = generate_function(cons_sys, checkbounds = checkbounds, linenumbers = linenumbers, From d9943261b68954456de1de36865dc05d39722cb7 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 27 Nov 2024 20:05:45 +0530 Subject: [PATCH 0397/2176] fix: fix constraint function not wrapped in `OptimizationProblem` --- src/systems/optimization/optimizationsystem.jl | 9 +++++++-- test/optimizationsystem.jl | 8 ++++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/src/systems/optimization/optimizationsystem.jl b/src/systems/optimization/optimizationsystem.jl index 176a2bdfed..801e7b05f3 100644 --- a/src/systems/optimization/optimizationsystem.jl +++ b/src/systems/optimization/optimizationsystem.jl @@ -399,7 +399,12 @@ function DiffEqBase.OptimizationProblem{iip}(sys::OptimizationSystem, u0map, cons, lcons_, ucons_ = generate_function(cons_sys, checkbounds = checkbounds, linenumbers = linenumbers, expression = Val{true}) - cons = eval_or_rgf.(cons; eval_expression, eval_module) + cons = let (cons_oop, cons_iip) = eval_or_rgf.(cons; eval_expression, eval_module) + _cons(u, p) = cons_oop(u, p) + _cons(resid, u, p) = cons_iip(resid, u, p) + _cons(u, p::MTKParameters) = cons_oop(u, p...) + _cons(resid, u, p::MTKParameters) = cons_iip(resid, u, p...) + end if cons_j _cons_j = let (cons_jac_oop, cons_jac_iip) = eval_or_rgf.( generate_jacobian(cons_sys; @@ -465,7 +470,7 @@ function DiffEqBase.OptimizationProblem{iip}(sys::OptimizationSystem, u0map, grad = _grad, hess = _hess, hess_prototype = hess_prototype, - cons = cons[2], + cons = cons, cons_j = _cons_j, cons_h = _cons_h, cons_jac_prototype = cons_jac_prototype, diff --git a/test/optimizationsystem.jl b/test/optimizationsystem.jl index a8e2be936d..23d07dcb85 100644 --- a/test/optimizationsystem.jl +++ b/test/optimizationsystem.jl @@ -368,3 +368,11 @@ end @test is_variable(sys, x[2]) @test is_variable(sys, x[3]) end + +@testset "Constraints work with nonnumeric parameters" begin + @variables x + @parameters p f(::Real) + @mtkbuild sys = OptimizationSystem(x^2 + f(x) * p, [x], [f, p]; constraints = [2.0 ≲ f(x) + p]) + prob = OptimizationProblem(sys, [x => 1.0], [p => 1.0, f => (x -> 2x)]) + @test abs(prob.f.cons(prob.u0, prob.p)[1]) ≈ 1.0 +end From d6add0eefe9f4d1232762e975cd023464c49cc01 Mon Sep 17 00:00:00 2001 From: Andrew Leonard Date: Wed, 27 Nov 2024 10:45:30 -0500 Subject: [PATCH 0398/2176] formatting --- test/optimizationsystem.jl | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/optimizationsystem.jl b/test/optimizationsystem.jl index 23d07dcb85..bb59fb09d9 100644 --- a/test/optimizationsystem.jl +++ b/test/optimizationsystem.jl @@ -372,7 +372,8 @@ end @testset "Constraints work with nonnumeric parameters" begin @variables x @parameters p f(::Real) - @mtkbuild sys = OptimizationSystem(x^2 + f(x) * p, [x], [f, p]; constraints = [2.0 ≲ f(x) + p]) + @mtkbuild sys = OptimizationSystem( + x^2 + f(x) * p, [x], [f, p]; constraints = [2.0 ≲ f(x) + p]) prob = OptimizationProblem(sys, [x => 1.0], [p => 1.0, f => (x -> 2x)]) @test abs(prob.f.cons(prob.u0, prob.p)[1]) ≈ 1.0 end From f20c7941f18b88cd4b7d50754a83e414bfba6eff Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 28 Nov 2024 12:41:19 +0530 Subject: [PATCH 0399/2176] build: bump minor version --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index dcad7c5e4d..1cb204a41b 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "ModelingToolkit" uuid = "961ee093-0014-501f-94e3-6117800e7a78" authors = ["Yingbo Ma ", "Chris Rackauckas and contributors"] -version = "9.53.0" +version = "9.54.0" [deps] AbstractTrees = "1520ce14-60c1-5f80-bbc7-55ef81b5835c" From 28858155db1b668ea82547fc505da68131854f67 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 28 Nov 2024 13:20:27 +0530 Subject: [PATCH 0400/2176] feat: add automatic variable discovery for `OptimizationSystem` --- .../optimization/optimizationsystem.jl | 27 +++++++++++++++++++ src/utils.jl | 13 ++++++++- test/optimizationsystem.jl | 11 ++++++++ 3 files changed, 50 insertions(+), 1 deletion(-) diff --git a/src/systems/optimization/optimizationsystem.jl b/src/systems/optimization/optimizationsystem.jl index 801e7b05f3..3425216b5f 100644 --- a/src/systems/optimization/optimizationsystem.jl +++ b/src/systems/optimization/optimizationsystem.jl @@ -144,6 +144,33 @@ function OptimizationSystem(op, unknowns, ps; checks = checks) end +function OptimizationSystem(objective; constraints = [], kwargs...) + allunknowns = OrderedSet() + ps = OrderedSet() + collect_vars!(allunknowns, ps, objective, nothing) + for cons in constraints + collect_vars!(allunknowns, ps, cons, nothing) + end + for ssys in get(kwargs, :systems, OptimizationSystem[]) + collect_scoped_vars!(allunknowns, ps, ssys, nothing) + end + new_ps = OrderedSet() + for p in ps + if iscall(p) && operation(p) === getindex + par = arguments(p)[begin] + if Symbolics.shape(Symbolics.unwrap(par)) !== Symbolics.Unknown() && + all(par[i] in ps for i in eachindex(par)) + push!(new_ps, par) + else + push!(new_ps, p) + end + else + push!(new_ps, p) + end + end + return OptimizationSystem(objective, collect(allunknowns), collect(new_ps); constraints, kwargs...) +end + function flatten(sys::OptimizationSystem) systems = get_systems(sys) isempty(systems) && return sys diff --git a/src/utils.jl b/src/utils.jl index 830ec98e44..1555cd624e 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -516,6 +516,15 @@ function collect_scoped_vars!(unknowns, parameters, sys, iv; depth = 1, op = Dif end end end + if has_constraints(sys) + for eq in get_constraints(sys) + eqtype_supports_collect_vars(eq) || continue + collect_vars!(unknowns, parameters, eq, iv; depth, op) + end + end + if has_op(sys) + collect_vars!(unknowns, parameters, get_op(sys), iv; depth, op) + end newdepth = depth == -1 ? depth : depth + 1 for ssys in get_systems(sys) collect_scoped_vars!(unknowns, parameters, ssys, iv; depth = newdepth, op) @@ -544,9 +553,10 @@ Can be dispatched by higher-level libraries to indicate support. """ eqtype_supports_collect_vars(eq) = false eqtype_supports_collect_vars(eq::Equation) = true +eqtype_supports_collect_vars(eq::Inequality) = true eqtype_supports_collect_vars(eq::Pair) = true -function collect_vars!(unknowns, parameters, eq::Equation, iv; +function collect_vars!(unknowns, parameters, eq::Union{Equation, Inequality}, iv; depth = 0, op = Differential) collect_vars!(unknowns, parameters, eq.lhs, iv; depth, op) collect_vars!(unknowns, parameters, eq.rhs, iv; depth, op) @@ -559,6 +569,7 @@ function collect_vars!(unknowns, parameters, p::Pair, iv; depth = 0, op = Differ return nothing end + function collect_var!(unknowns, parameters, var, iv; depth = 0) isequal(var, iv) && return nothing check_scope_depth(getmetadata(var, SymScope, LocalScope()), depth) || return nothing diff --git a/test/optimizationsystem.jl b/test/optimizationsystem.jl index bb59fb09d9..c20613441a 100644 --- a/test/optimizationsystem.jl +++ b/test/optimizationsystem.jl @@ -377,3 +377,14 @@ end prob = OptimizationProblem(sys, [x => 1.0], [p => 1.0, f => (x -> 2x)]) @test abs(prob.f.cons(prob.u0, prob.p)[1]) ≈ 1.0 end + +@testset "Variable discovery" begin + @variables x1 x2 + @parameters p1 p2 + @named sys1 = OptimizationSystem(x1^2; constraints = [p1 * x1 ≲ 2.0]) + @named sys2 = OptimizationSystem(x2^2; constraints = [p2 * x2 ≲ 2.0], systems = [sys1]) + @test isequal(only(unknowns(sys1)), x1) + @test isequal(only(parameters(sys1)), p1) + @test all(y -> any(x -> isequal(x, y), unknowns(sys2)), [x2, sys1.x1]) + @test all(y -> any(x -> isequal(x, y), parameters(sys2)), [p2, sys1.p1]) +end From 6729cdaa202e272e922465f2d44f6dbca18870c3 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 28 Nov 2024 15:16:17 +0530 Subject: [PATCH 0401/2176] fix: retain observed equations after `structural_simplify` of `SDESystem` --- src/systems/systems.jl | 2 +- test/sdesystem.jl | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/systems/systems.jl b/src/systems/systems.jl index 4205a4d207..a54206d1dd 100644 --- a/src/systems/systems.jl +++ b/src/systems/systems.jl @@ -156,6 +156,6 @@ function __structural_simplify(sys::AbstractSystem, io = nothing; simplify = fal noise_eqs = StructuralTransformations.tearing_substitute_expr(ode_sys, noise_eqs) return SDESystem(full_equations(ode_sys), noise_eqs, get_iv(ode_sys), unknowns(ode_sys), parameters(ode_sys); - name = nameof(ode_sys), is_scalar_noise) + name = nameof(ode_sys), is_scalar_noise, observed = observed(ode_sys)) end end diff --git a/test/sdesystem.jl b/test/sdesystem.jl index 78d7a0418b..749aca86a7 100644 --- a/test/sdesystem.jl +++ b/test/sdesystem.jl @@ -799,3 +799,13 @@ end prob = SDEProblem(sys, [], (0.0, 1.0), []) @test_nowarn solve(prob, RKMil()) end + +@testset "Observed variables retained after `structural_simplify`" begin + @variables x(t) y(t) z(t) + @brownian a + @mtkbuild sys = System([D(x) ~ x + a, D(y) ~ y + a, z ~ x + y], t) + @test sys isa SDESystem + @test length(observed(sys)) == 1 + prob = SDEProblem(sys, [x => 1.0, y => 1.0], (0.0, 1.0)) + @test prob[z] ≈ 2.0 +end From 9589a1f347673cb85b639fbccdab508201d64402 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 21 Nov 2024 12:50:57 +0530 Subject: [PATCH 0402/2176] feat: store args and kwargs in `EmptySciMLFunction` --- src/systems/problem_utils.jl | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/systems/problem_utils.jl b/src/systems/problem_utils.jl index 1cf110df13..1cfb8eb40a 100644 --- a/src/systems/problem_utils.jl +++ b/src/systems/problem_utils.jl @@ -431,12 +431,16 @@ end $(TYPEDEF) A simple utility meant to be used as the `constructor` passed to `process_SciMLProblem` in -case constructing a SciMLFunction is not required. +case constructing a SciMLFunction is not required. The arguments passed to it are available +in the `args` field, and the keyword arguments in the `kwargs` field. """ -struct EmptySciMLFunction end +struct EmptySciMLFunction{A, K} + args::A + kwargs::K +end function EmptySciMLFunction(args...; kwargs...) - return nothing + return EmptySciMLFunction{typeof(args), typeof(kwargs)}(args, kwargs) end """ From 8d4f5423e424279ba0855bf79e008a25e6544a77 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 20 Nov 2024 12:54:33 +0530 Subject: [PATCH 0403/2176] feat: propagate `ODEProblem` guesses to `remake` --- src/systems/diffeqs/abstractodesystem.jl | 4 +- src/systems/nonlinear/initializesystem.jl | 90 ++++++----------------- src/systems/problem_utils.jl | 14 +++- test/initializationsystem.jl | 23 ++++++ 4 files changed, 60 insertions(+), 71 deletions(-) diff --git a/src/systems/diffeqs/abstractodesystem.jl b/src/systems/diffeqs/abstractodesystem.jl index cbf835f48d..af96c4fcfe 100644 --- a/src/systems/diffeqs/abstractodesystem.jl +++ b/src/systems/diffeqs/abstractodesystem.jl @@ -1310,11 +1310,11 @@ function InitializationProblem{iip, specialize}(sys::AbstractODESystem, elseif isempty(u0map) && get_initializesystem(sys) === nothing isys = structural_simplify( generate_initializesystem( - sys; initialization_eqs, check_units, pmap = parammap); fully_determined) + sys; initialization_eqs, check_units, pmap = parammap, guesses); fully_determined) else isys = structural_simplify( generate_initializesystem( - sys; u0map, initialization_eqs, check_units, pmap = parammap); fully_determined) + sys; u0map, initialization_eqs, check_units, pmap = parammap, guesses); fully_determined) end ts = get_tearing_state(isys) diff --git a/src/systems/nonlinear/initializesystem.jl b/src/systems/nonlinear/initializesystem.jl index eff19afb07..fe41e44d84 100644 --- a/src/systems/nonlinear/initializesystem.jl +++ b/src/systems/nonlinear/initializesystem.jl @@ -30,7 +30,8 @@ function generate_initializesystem(sys::ODESystem; # 1) process dummy derivatives and u0map into initialization system eqs_ics = eqs[idxs_alge] # start equation list with algebraic equations defs = copy(defaults(sys)) # copy so we don't modify sys.defaults - guesses = merge(get_guesses(sys), todict(guesses)) + additional_guesses = anydict(guesses) + guesses = merge(get_guesses(sys), additional_guesses) schedule = getfield(sys, :schedule) if !isnothing(schedule) for x in filter(x -> !isnothing(x[1]), schedule.dummy_sub) @@ -178,7 +179,7 @@ function generate_initializesystem(sys::ODESystem; for k in keys(defs) defs[k] = substitute(defs[k], paramsubs) end - meta = InitializationSystemMetadata(Dict{Any, Any}(u0map), Dict{Any, Any}(pmap)) + meta = InitializationSystemMetadata(anydict(u0map), anydict(pmap), additional_guesses) return NonlinearSystem(eqs_ics, vars, pars; @@ -193,6 +194,7 @@ end struct InitializationSystemMetadata u0map::Dict{Any, Any} pmap::Dict{Any, Any} + additional_guesses::Dict{Any, Any} end function is_parameter_solvable(p, pmap, defs, guesses) @@ -263,75 +265,29 @@ function SciMLBase.remake_initializeprob(sys::ODESystem, odefn, u0, t0, p) return initprob, odefn.update_initializeprob!, odefn.initializeprobmap, odefn.initializeprobpmap end - if u0 === missing || isempty(u0) - u0 = Dict() - elseif !(eltype(u0) <: Pair) - u0 = Dict(unknowns(sys) .=> u0) - end - if p === missing - p = Dict() + dvs = unknowns(sys) + ps = parameters(sys) + u0map = to_varmap(u0, dvs) + pmap = to_varmap(p, ps) + guesses = Dict() + if SciMLBase.has_initializeprob(odefn) + oldsys = odefn.initializeprob.f.sys + meta = get_metadata(oldsys) + if meta isa InitializationSystemMetadata + u0map = merge(meta.u0map, u0map) + pmap = merge(meta.pmap, pmap) + merge!(guesses, meta.additional_guesses) + end end if t0 === nothing t0 = 0.0 end - u0 = todict(u0) - defs = defaults(sys) - varmap = merge(defs, u0) - for k in collect(keys(varmap)) - if varmap[k] === nothing - delete!(varmap, k) - end - end - varmap = canonicalize_varmap(varmap) - missingvars = setdiff(unknowns(sys), collect(keys(varmap))) - setobserved = filter(keys(varmap)) do var - has_observed_with_lhs(sys, var) || has_observed_with_lhs(sys, default_toterm(var)) - end - p = todict(p) - guesses = ModelingToolkit.guesses(sys) - solvablepars = [par - for par in parameters(sys) - if is_parameter_solvable(par, p, defs, guesses)] - pvarmap = merge(defs, p) - setparobserved = filter(keys(pvarmap)) do var - has_parameter_dependency_with_lhs(sys, var) - end - if (((!isempty(missingvars) || !isempty(solvablepars) || - !isempty(setobserved) || !isempty(setparobserved)) && - ModelingToolkit.get_tearing_state(sys) !== nothing) || - !isempty(initialization_equations(sys))) - if SciMLBase.has_initializeprob(odefn) - oldsys = odefn.initializeprob.f.sys - meta = get_metadata(oldsys) - if meta isa InitializationSystemMetadata - u0 = merge(meta.u0map, u0) - p = merge(meta.pmap, p) - end - end - for k in collect(keys(u0)) - if u0[k] === nothing - delete!(u0, k) - end - end - for k in collect(keys(p)) - if p[k] === nothing - delete!(p, k) - end - end - - initprob = InitializationProblem(sys, t0, u0, p) - initprobmap = getu(initprob, unknowns(sys)) - punknowns = [p for p in all_variable_symbols(initprob) if is_parameter(sys, p)] - getpunknowns = getu(initprob, punknowns) - setpunknowns = setp(sys, punknowns) - initprobpmap = GetUpdatedMTKParameters(getpunknowns, setpunknowns) - reqd_syms = parameter_symbols(initprob) - update_initializeprob! = UpdateInitializeprob( - getu(sys, reqd_syms), setu(initprob, reqd_syms)) - return initprob, update_initializeprob!, initprobmap, initprobpmap - else - return nothing, nothing, nothing, nothing - end + filter_missing_values!(u0map) + filter_missing_values!(pmap) + f, _ = process_SciMLProblem(EmptySciMLFunction, sys, u0map, pmap; guesses, t = t0) + kws = f.kwargs + return get(kws, :initializeprob, nothing), get(kws, :update_initializeprob!, nothing), get(kws, :initializeprobmap, nothing), + get(kws, :initializeprobpmap, nothing) end """ diff --git a/src/systems/problem_utils.jl b/src/systems/problem_utils.jl index 1cfb8eb40a..9521df7872 100644 --- a/src/systems/problem_utils.jl +++ b/src/systems/problem_utils.jl @@ -4,12 +4,13 @@ const AnyDict = Dict{Any, Any} $(TYPEDSIGNATURES) If called without arguments, return `Dict{Any, Any}`. Otherwise, interpret the input -as a symbolic map and turn it into a `Dict{Any, Any}`. Handles `SciMLBase.NullParameters` -and `nothing`. +as a symbolic map and turn it into a `Dict{Any, Any}`. Handles `SciMLBase.NullParameters`, +`missing` and `nothing`. """ anydict() = AnyDict() anydict(::SciMLBase.NullParameters) = AnyDict() anydict(::Nothing) = AnyDict() +anydict(::Missing) = AnyDict() anydict(x::AnyDict) = x anydict(x) = AnyDict(x) @@ -388,6 +389,15 @@ function evaluate_varmap!(varmap::AbstractDict, vars; limit = 100) end end +""" + $(TYPEDSIGNATURES) + +Remove keys in `varmap` whose values are `nothing`. +""" +function filter_missing_values!(varmap::AbstractDict) + filter!(kvp -> kvp[2] !== nothing, varmap) +end + struct GetUpdatedMTKParameters{G, S} # `getu` functor which gets parameters that are unknowns during initialization getpunknowns::G diff --git a/test/initializationsystem.jl b/test/initializationsystem.jl index 770f9f12bd..edb4199356 100644 --- a/test/initializationsystem.jl +++ b/test/initializationsystem.jl @@ -975,3 +975,26 @@ end @test integ.ps[p] ≈ 1.0 @test integ.ps[q]≈cbrt(2) rtol=1e-6 end + +@testset "Guesses provided to `ODEProblem` are used in `remake`" begin + @variables x(t) y(t)=2x + @parameters p q=3x + @mtkbuild sys = ODESystem([D(x) ~ x * p + q, x^3 + y^3 ~ 3], t) + prob = ODEProblem( + sys, [], (0.0, 1.0), [p => 1.0]; guesses = [x => 1.0, y => 1.0, q => 1.0]) + @test prob[x] == 0.0 + @test prob[y] == 0.0 + @test prob.ps[p] == 1.0 + @test prob.ps[q] == 0.0 + integ = init(prob) + @test integ[x] ≈ 1 / cbrt(3) + @test integ[y] ≈ 2 / cbrt(3) + @test integ.ps[p] == 1.0 + @test integ.ps[q] ≈ 3 / cbrt(3) + prob2 = remake(prob; u0 = [y => 3x], p = [q => 2x]) + integ2 = init(prob2) + @test integ2[x] ≈ cbrt(3 / 28) + @test integ2[y] ≈ 3cbrt(3 / 28) + @test integ2.ps[p] == 1.0 + @test integ2.ps[q] ≈ 2cbrt(3 / 28) +end From 2641fd8a97cfc785d340e2fa7cea80a3a3815182 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 22 Nov 2024 14:04:13 +0530 Subject: [PATCH 0404/2176] fix: handle initial values passed as `Symbol`s --- src/systems/problem_utils.jl | 38 ++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/src/systems/problem_utils.jl b/src/systems/problem_utils.jl index 9521df7872..b837eef98e 100644 --- a/src/systems/problem_utils.jl +++ b/src/systems/problem_utils.jl @@ -52,6 +52,42 @@ function add_toterms(varmap::AbstractDict; toterm = default_toterm) return cp end +""" + $(TYPEDSIGNATURES) + +Turn any `Symbol` keys in `varmap` to the appropriate symbolic variables in `sys`. Any +symbols that cannot be converted are ignored. +""" +function symbols_to_symbolics!(sys::AbstractSystem, varmap::AbstractDict) + if is_split(sys) + ic = get_index_cache(sys) + for k in collect(keys(varmap)) + k isa Symbol || continue + newk = get(ic.symbol_to_variable, k, nothing) + newk === nothing && continue + varmap[newk] = varmap[k] + delete!(varmap, k) + end + else + syms = all_symbols(sys) + for k in collect(keys(varmap)) + k isa Symbol || continue + idx = findfirst(syms) do sym + hasname(sym) || return false + name = getname(sym) + return name == k + end + idx === nothing && continue + newk = syms[idx] + if iscall(newk) && operation(newk) === getindex + newk = arguments(newk)[1] + end + varmap[newk] = varmap[k] + delete!(varmap, k) + end + end +end + """ $(TYPEDSIGNATURES) @@ -530,8 +566,10 @@ function process_SciMLProblem( pType = typeof(pmap) _u0map = u0map u0map = to_varmap(u0map, dvs) + symbols_to_symbolics!(sys, u0map) _pmap = pmap pmap = to_varmap(pmap, ps) + symbols_to_symbolics!(sys, pmap) defs = add_toterms(recursive_unwrap(defaults(sys))) cmap, cs = get_cmap(sys) kwargs = NamedTuple(kwargs) From 26980151eacd8d3b1977f544f4a284022ce71ad9 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 22 Nov 2024 14:04:34 +0530 Subject: [PATCH 0405/2176] fix: handle `remake` with no pre-existing initializeprob --- src/systems/nonlinear/initializesystem.jl | 58 ++++++++++++++++++----- 1 file changed, 47 insertions(+), 11 deletions(-) diff --git a/src/systems/nonlinear/initializesystem.jl b/src/systems/nonlinear/initializesystem.jl index fe41e44d84..4abb345822 100644 --- a/src/systems/nonlinear/initializesystem.jl +++ b/src/systems/nonlinear/initializesystem.jl @@ -210,17 +210,16 @@ function is_parameter_solvable(p, pmap, defs, guesses) _val1 === nothing && _val2 !== nothing)) && _val3 !== nothing end -function SciMLBase.remake_initializeprob(sys::ODESystem, odefn, u0, t0, p) +function SciMLBase.remake_initialization_data(sys::ODESystem, odefn, u0, t0, p, newu0, newp) if u0 === missing && p === missing - return odefn.initializeprob, odefn.update_initializeprob!, odefn.initializeprobmap, - odefn.initializeprobpmap + return odefn.initialization_data end if !(eltype(u0) <: Pair) && !(eltype(p) <: Pair) oldinitprob = odefn.initializeprob - if oldinitprob === nothing || !SciMLBase.has_sys(oldinitprob.f) || - !(oldinitprob.f.sys isa NonlinearSystem) - return oldinitprob, odefn.update_initializeprob!, odefn.initializeprobmap, - odefn.initializeprobpmap + oldinitprob === nothing && return nothing + if !SciMLBase.has_sys(oldinitprob.f) || !(oldinitprob.f.sys isa NonlinearSystem) + return SciMLBase.OverrideInitData(oldinitprob, odefn.update_initializeprob!, + odefn.initializeprobmap, odefn.initializeprobpmap) end pidxs = ParameterIndex[] pvals = [] @@ -262,14 +261,17 @@ function SciMLBase.remake_initializeprob(sys::ODESystem, odefn, u0, t0, p) oldinitprob.f.sys, parameter_values(oldinitprob), pidxs, pvals) end initprob = remake(oldinitprob; u0 = newu0, p = newp) - return initprob, odefn.update_initializeprob!, odefn.initializeprobmap, - odefn.initializeprobpmap + return SciMLBase.OverrideInitData(initprob, odefn.update_initializeprob!, + odefn.initializeprobmap, odefn.initializeprobpmap) end dvs = unknowns(sys) ps = parameters(sys) u0map = to_varmap(u0, dvs) + symbols_to_symbolics!(sys, u0map) pmap = to_varmap(p, ps) + symbols_to_symbolics!(sys, pmap) guesses = Dict() + defs = defaults(sys) if SciMLBase.has_initializeprob(odefn) oldsys = odefn.initializeprob.f.sys meta = get_metadata(oldsys) @@ -278,6 +280,35 @@ function SciMLBase.remake_initializeprob(sys::ODESystem, odefn, u0, t0, p) pmap = merge(meta.pmap, pmap) merge!(guesses, meta.additional_guesses) end + else + # there is no initializeprob, so the original problem construction + # had no solvable parameters and had the differential variables + # specified in `u0map`. + if u0 === missing + # the user didn't pass `u0` to `remake`, so they want to retain + # existing values. Fill the differential variables in `u0map`, + # initialization will either be elided or solve for the algebraic + # variables + diff_idxs = isdiffeq.(equations(sys)) + for i in eachindex(dvs) + diff_idxs[i] || continue + u0map[dvs[i]] = newu0[i] + end + end + if p === missing + # the user didn't pass `p` to `remake`, so they want to retain + # existing values. Fill all parameters in `pmap` so that none of + # them are solvable. + for p in ps + pmap[p] = getp(sys, p)(newp) + end + end + # all non-solvable parameters need values regardless + for p in ps + haskey(pmap, p) && continue + is_parameter_solvable(p, pmap, defs, guesses) && continue + pmap[p] = getp(sys, p)(newp) + end end if t0 === nothing t0 = 0.0 @@ -286,8 +317,13 @@ function SciMLBase.remake_initializeprob(sys::ODESystem, odefn, u0, t0, p) filter_missing_values!(pmap) f, _ = process_SciMLProblem(EmptySciMLFunction, sys, u0map, pmap; guesses, t = t0) kws = f.kwargs - return get(kws, :initializeprob, nothing), get(kws, :update_initializeprob!, nothing), get(kws, :initializeprobmap, nothing), - get(kws, :initializeprobpmap, nothing) + initprob = get(kws, :initializeprob, nothing) + if initprob === nothing + return nothing + end + return SciMLBase.OverrideInitData(initprob, get(kws, :update_initializeprob!, nothing), + get(kws, :initializeprobmap, nothing), + get(kws, :initializeprobpmap, nothing)) end """ From 3383bcede7c841530a9d82f601ff70e40f32deca Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 22 Nov 2024 14:05:11 +0530 Subject: [PATCH 0406/2176] test: test `remake` without initializeprob and `Symbol` values --- test/initializationsystem.jl | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/test/initializationsystem.jl b/test/initializationsystem.jl index edb4199356..0b0dc42c1e 100644 --- a/test/initializationsystem.jl +++ b/test/initializationsystem.jl @@ -998,3 +998,37 @@ end @test integ2.ps[p] == 1.0 @test integ2.ps[q] ≈ 2cbrt(3 / 28) end + +@testset "Remake problem with no initializeprob" begin + @variables x(t) [guess = 1.0] y(t) [guess = 1.0] + @parameters p [guess = 1.0] q [guess = 1.0] + @mtkbuild sys = ODESystem( + [D(x) ~ p * x + q * y, y ~ 2x], t; parameter_dependencies = [q ~ 2p]) + prob = ODEProblem(sys, [x => 1.0], (0.0, 1.0), [p => 1.0]) + @test prob.f.initialization_data === nothing + prob2 = remake(prob; u0 = [x => 2.0]) + @test prob2[x] == 2.0 + @test prob2.f.initialization_data === nothing + prob3 = remake(prob; u0 = [y => 2.0]) + @test prob3.f.initialization_data !== nothing + @test init(prob3)[x] ≈ 1.0 + prob4 = remake(prob; p = [p => 1.0]) + @test prob4.f.initialization_data === nothing + prob5 = remake(prob; p = [p => missing, q => 2.0]) + @test prob5.f.initialization_data !== nothing + @test init(prob5).ps[p] ≈ 1.0 +end + +@testset "Variables provided as symbols" begin + @variables x(t) [guess = 1.0] y(t) [guess = 1.0] + @parameters p [guess = 1.0] q [guess = 1.0] + @mtkbuild sys = ODESystem( + [D(x) ~ p * x + q * y, y ~ 2x], t; parameter_dependencies = [q ~ 2p]) + prob = ODEProblem(sys, [:x => 1.0], (0.0, 1.0), [p => 1.0]) + @test prob.f.initialization_data === nothing + prob2 = remake(prob; u0 = [:x => 2.0]) + @test prob2.f.initialization_data === nothing + prob3 = remake(prob; u0 = [:y => 1.0]) + @test prob3.f.initialization_data !== nothing + @test init(prob3)[x] ≈ 0.5 +end From 83ed891cd7cf2319ffbef0c5bb870424de5ab6ff Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Sat, 30 Nov 2024 23:37:12 +0530 Subject: [PATCH 0407/2176] build: bump SciMLBase compat --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 093c632c42..1621669c2c 100644 --- a/Project.toml +++ b/Project.toml @@ -126,7 +126,7 @@ REPL = "1" RecursiveArrayTools = "3.26" Reexport = "0.2, 1" RuntimeGeneratedFunctions = "0.5.9" -SciMLBase = "2.57.1" +SciMLBase = "2.64" SciMLStructures = "1.0" Serialization = "1" Setfield = "0.7, 0.8, 1" From 10cc9c16854fc277b88967bbcd709bf42a3b14d2 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Sat, 30 Nov 2024 23:40:22 +0530 Subject: [PATCH 0408/2176] refactor: format --- src/systems/optimization/optimizationsystem.jl | 3 ++- src/utils.jl | 1 - 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/systems/optimization/optimizationsystem.jl b/src/systems/optimization/optimizationsystem.jl index 3425216b5f..43e9294dd3 100644 --- a/src/systems/optimization/optimizationsystem.jl +++ b/src/systems/optimization/optimizationsystem.jl @@ -168,7 +168,8 @@ function OptimizationSystem(objective; constraints = [], kwargs...) push!(new_ps, p) end end - return OptimizationSystem(objective, collect(allunknowns), collect(new_ps); constraints, kwargs...) + return OptimizationSystem( + objective, collect(allunknowns), collect(new_ps); constraints, kwargs...) end function flatten(sys::OptimizationSystem) diff --git a/src/utils.jl b/src/utils.jl index 1555cd624e..416efd8f2c 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -569,7 +569,6 @@ function collect_vars!(unknowns, parameters, p::Pair, iv; depth = 0, op = Differ return nothing end - function collect_var!(unknowns, parameters, var, iv; depth = 0) isequal(var, iv) && return nothing check_scope_depth(getmetadata(var, SymScope, LocalScope()), depth) || return nothing From 12b02c0bc5881873c5bfc27c141895c1a006b7e2 Mon Sep 17 00:00:00 2001 From: Christopher Rackauckas Date: Sun, 1 Dec 2024 12:28:48 -0800 Subject: [PATCH 0409/2176] Fix initialization_eqs example It was trivially wrong, fixes https://github.com/SciML/ModelingToolkit.jl/issues/3245 --- docs/src/tutorials/initialization.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/src/tutorials/initialization.md b/docs/src/tutorials/initialization.md index c3dbf8bbb0..c01310eb06 100644 --- a/docs/src/tutorials/initialization.md +++ b/docs/src/tutorials/initialization.md @@ -113,7 +113,7 @@ the `initialization_eqs` keyword argument, for example: ```@example init prob = ODEProblem(pend, [x => 1], (0.0, 1.5), [g => 1], guesses = [λ => 0, y => 1], - initialization_eqs = [y ~ 1]) + initialization_eqs = [y ~ 0]) sol = solve(prob, Rodas5P()) plot(sol, idxs = (x, y)) ``` From b3da8137a9288b9c3eeff209a541dd41a5f97524 Mon Sep 17 00:00:00 2001 From: vyudu Date: Sun, 1 Dec 2024 16:18:18 -0500 Subject: [PATCH 0410/2176] up --- src/systems/diffeqs/abstractodesystem.jl | 64 +++++++++--------------- 1 file changed, 23 insertions(+), 41 deletions(-) diff --git a/src/systems/diffeqs/abstractodesystem.jl b/src/systems/diffeqs/abstractodesystem.jl index 7a8bf56562..bb4cbbb6c3 100644 --- a/src/systems/diffeqs/abstractodesystem.jl +++ b/src/systems/diffeqs/abstractodesystem.jl @@ -469,6 +469,17 @@ function DiffEqBase.ODEFunction{iip, specialize}(sys::AbstractODESystem, initializeprobpmap = initializeprobpmap) end +""" +```julia +SciMLBase.BVPFunction{iip}(sys::AbstractODESystem, u0map, tspan, + parammap = DiffEqBase.NullParameters(); + version = nothing, tgrad = false, + jac = true, sparse = true, + simplify = false, + kwargs...) where {iip} +``` +""" + """ ```julia SciMLBase.BVProblem{iip}(sys::AbstractODESystem, u0map, tspan, @@ -481,7 +492,7 @@ SciMLBase.BVProblem{iip}(sys::AbstractODESystem, u0map, tspan, Create a `BVProblem` from the [`ODESystem`](@ref). The arguments `dvs` and `ps` are used to set the order of the dependent variable and parameter vectors, -respectively. +respectively. `u0` should be either the initial condition, a vector of values `u(t_i)` for collocation methods, or a function returning one or the other. """ function SciMLBase.BVProblem(sys::AbstractODESystem, args...; kwargs...) BVProblem{true}(sys, args...; kwargs...) @@ -502,12 +513,13 @@ function SciMLBase.BVProblem{false}(sys::AbstractODESystem, args...; kwargs...) BVProblem{false, SciMLBase.FullSpecialize}(sys, args...; kwargs...) end +# figure out what's going on when we try to set `sparse`? + function SciMLBase.BVProblem{iip, specialize}(sys::AbstractODESystem, u0map = [], tspan = get_tspan(sys), parammap = DiffEqBase.NullParameters(); version = nothing, tgrad = false, jac = true, sparse = true, - sparsity = true, callback = nothing, check_length = true, warn_initialize_determined = true, @@ -521,57 +533,27 @@ function SciMLBase.BVProblem{iip, specialize}(sys::AbstractODESystem, u0map = [] f, u0, p = process_SciMLProblem(ODEFunction{iip, specialize}, sys, u0map, parammap; t = tspan !== nothing ? tspan[1] : tspan, - check_length, warn_initialize_determined, eval_expression, eval_module, jac, sparse, sparsity, kwargs...) - - # if jac - # jac_gen = generate_jacobian(sys, dvs, ps; - # simplify = simplify, sparse = sparse, - # expression = Val{true}, - # expression_module = eval_module, - # checkbounds = checkbounds, kwargs...) - # jac_oop, jac_iip = eval_or_rgf.(jac_gen; eval_expression, eval_module) - - # _jac(u, p, t) = jac_oop(u, p, t) - # _jac(J, u, p, t) = jac_iip(J, u, p, t) - # _jac(u, p::Tuple{Vararg{Number}}, t) = jac_oop(u, p, t) - # _jac(J, u, p::Tuple{Vararg{Number}}, t) = jac_iip(J, u, p, t) - # _jac(u, p::Tuple, t) = jac_oop(u, p..., t) - # _jac(J, u, p::Tuple, t) = jac_iip(J, u, p..., t) - # _jac(u, p::MTKParameters, t) = jac_oop(u, p..., t) - # _jac(J, u, p::MTKParameters, t) = jac_iip(J, u, p..., t) - # else - # _jac = nothing - # end - - # jac_prototype = if sparse - # uElType = u0 === nothing ? Float64 : eltype(u0) - # if jac - # similar(calculate_jacobian(sys, sparse = sparse), uElType) - # else - # similar(jacobian_sparsity(sys), uElType) - # end - # else - # nothing - # end - - # f.jac = _jac - # f.jac_prototype = jac_prototype - # f.sparsity = jac ? jacobian_sparsity(sys) : nothing + check_length, warn_initialize_determined, eval_expression, eval_module, jac, kwargs...) cbs = process_events(sys; callback, eval_expression, eval_module, kwargs...) - kwargs = filter_kwargs(kwargs) kwargs1 = (;) if cbs !== nothing kwargs1 = merge(kwargs1, (callback = cbs,)) end + + # Construct initial conditions + _u0 = prepare_initial_state(u0) + __u0 = if _u0 isa Function + _u0(t_i) + end # Define the boundary conditions bc = if iip - (residual, u, p, t) -> (residual = u[1] - u0) + (residual, u, p, t) -> (residual = u[1] - __u0) else - (u, p, t) -> (u[1] - u0) + (u, p, t) -> (u[1] - __u0) end return BVProblem{iip}(f, bc, u0, tspan, p; kwargs1..., kwargs...) From 4affeac4b340a06c770373b498ec6b0e94c25a06 Mon Sep 17 00:00:00 2001 From: vyudu Date: Sun, 1 Dec 2024 17:35:05 -0500 Subject: [PATCH 0411/2176] up --- src/systems/diffeqs/abstractodesystem.jl | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/systems/diffeqs/abstractodesystem.jl b/src/systems/diffeqs/abstractodesystem.jl index 263a75d153..33bddece30 100644 --- a/src/systems/diffeqs/abstractodesystem.jl +++ b/src/systems/diffeqs/abstractodesystem.jl @@ -545,9 +545,7 @@ function SciMLBase.BVProblem{iip, specialize}(sys::AbstractODESystem, u0map = [] # Construct initial conditions _u0 = prepare_initial_state(u0) - __u0 = if _u0 isa Function - _u0(t_i) - end + __u0 = _u0 isa Function ? _u0(tspan[1]) : _u0 # Define the boundary conditions bc = if iip From a3429ea2b7d9e67898023eac1599e71c3d1b7bec Mon Sep 17 00:00:00 2001 From: vyudu Date: Sun, 1 Dec 2024 17:42:39 -0500 Subject: [PATCH 0412/2176] up --- src/systems/diffeqs/abstractodesystem.jl | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/src/systems/diffeqs/abstractodesystem.jl b/src/systems/diffeqs/abstractodesystem.jl index 33bddece30..84f40a01f7 100644 --- a/src/systems/diffeqs/abstractodesystem.jl +++ b/src/systems/diffeqs/abstractodesystem.jl @@ -513,8 +513,6 @@ function SciMLBase.BVProblem{false}(sys::AbstractODESystem, args...; kwargs...) BVProblem{false, SciMLBase.FullSpecialize}(sys, args...; kwargs...) end -# figure out what's going on when we try to set `sparse`? - function SciMLBase.BVProblem{iip, specialize}(sys::AbstractODESystem, u0map = [], tspan = get_tspan(sys), parammap = DiffEqBase.NullParameters(); @@ -544,14 +542,13 @@ function SciMLBase.BVProblem{iip, specialize}(sys::AbstractODESystem, u0map = [] end # Construct initial conditions - _u0 = prepare_initial_state(u0) - __u0 = _u0 isa Function ? _u0(tspan[1]) : _u0 + _u0 = u0 isa Function ? u0(tspan[1]) : u0 # Define the boundary conditions bc = if iip - (residual, u, p, t) -> (residual = u[1] - __u0) + (residual, u, p, t) -> (residual = u[1] - _u0) else - (u, p, t) -> (u[1] - __u0) + (u, p, t) -> (u[1] - _u0) end return BVProblem{iip}(f, bc, u0, tspan, p; kwargs1..., kwargs...) From f751fbb51c49e0b35ee84a12c54ec2de919660a5 Mon Sep 17 00:00:00 2001 From: vyudu Date: Sun, 1 Dec 2024 21:22:08 -0500 Subject: [PATCH 0413/2176] up --- src/systems/diffeqs/abstractodesystem.jl | 18 ++------ test/bvproblem.jl | 56 ++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 15 deletions(-) create mode 100644 test/bvproblem.jl diff --git a/src/systems/diffeqs/abstractodesystem.jl b/src/systems/diffeqs/abstractodesystem.jl index 84f40a01f7..112419dea8 100644 --- a/src/systems/diffeqs/abstractodesystem.jl +++ b/src/systems/diffeqs/abstractodesystem.jl @@ -469,17 +469,6 @@ function DiffEqBase.ODEFunction{iip, specialize}(sys::AbstractODESystem, initializeprobpmap = initializeprobpmap) end -""" -```julia -SciMLBase.BVPFunction{iip}(sys::AbstractODESystem, u0map, tspan, - parammap = DiffEqBase.NullParameters(); - version = nothing, tgrad = false, - jac = true, sparse = true, - simplify = false, - kwargs...) where {iip} -``` -""" - """ ```julia SciMLBase.BVProblem{iip}(sys::AbstractODESystem, u0map, tspan, @@ -492,7 +481,7 @@ SciMLBase.BVProblem{iip}(sys::AbstractODESystem, u0map, tspan, Create a `BVProblem` from the [`ODESystem`](@ref). The arguments `dvs` and `ps` are used to set the order of the dependent variable and parameter vectors, -respectively. `u0` should be either the initial condition, a vector of values `u(t_i)` for collocation methods, or a function returning one or the other. +respectively. `u0map` should be used to specify the initial condition, or be a function returning an initial condition. """ function SciMLBase.BVProblem(sys::AbstractODESystem, args...; kwargs...) BVProblem{true}(sys, args...; kwargs...) @@ -517,7 +506,6 @@ function SciMLBase.BVProblem{iip, specialize}(sys::AbstractODESystem, u0map = [] tspan = get_tspan(sys), parammap = DiffEqBase.NullParameters(); version = nothing, tgrad = false, - jac = true, sparse = true, callback = nothing, check_length = true, warn_initialize_determined = true, @@ -531,7 +519,7 @@ function SciMLBase.BVProblem{iip, specialize}(sys::AbstractODESystem, u0map = [] f, u0, p = process_SciMLProblem(ODEFunction{iip, specialize}, sys, u0map, parammap; t = tspan !== nothing ? tspan[1] : tspan, - check_length, warn_initialize_determined, eval_expression, eval_module, jac, kwargs...) + check_length, warn_initialize_determined, eval_expression, eval_module, kwargs...) cbs = process_events(sys; callback, eval_expression, eval_module, kwargs...) kwargs = filter_kwargs(kwargs) @@ -546,7 +534,7 @@ function SciMLBase.BVProblem{iip, specialize}(sys::AbstractODESystem, u0map = [] # Define the boundary conditions bc = if iip - (residual, u, p, t) -> (residual = u[1] - _u0) + (residual, u, p, t) -> residual .= u[1] - _u0 else (u, p, t) -> (u[1] - _u0) end diff --git a/test/bvproblem.jl b/test/bvproblem.jl new file mode 100644 index 0000000000..90d41a96ad --- /dev/null +++ b/test/bvproblem.jl @@ -0,0 +1,56 @@ +using BoundaryValueDiffEq, OrdinaryDiffEq +using ModelingToolkit +using ModelingToolkit: t_nounits as t, D_nounits as D + +@parameters σ = 10. ρ = 28 β = 8/3 +@variables x(t) = 1 y(t) = 0 z(t) = 0 + +eqs = [D(x) ~ σ*(y-x), + D(y) ~ x*(ρ-z)-y, + D(z) ~ x*y - β*z] + +u0map = [:x => 1., :y => 0., :z => 0.] +parammap = [:ρ => 28., :β => 8/3, :σ => 10.] +tspan = (0., 10.) + +@mtkbuild lorenz = ODESystem(eqs, t) + +bvp = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}(lorenz, u0map, tspan, parammap) +sol = solve(bvp, MIRK4(), dt = 0.1); + +bvp2 = SciMLBase.BVProblem{false, SciMLBase.AutoSpecialize}(lorenz, u0map, tspan, parammap) +sol2 = solve(bvp, MIRK4(), dt = 0.1); + +op = ODEProblem(lorenz, u0map, tspan, parammap) +osol = solve(op) + +@test sol.u[end] ≈ osol.u[end] +@test sol2.u[end] ≈ osol.u[end] +@test sol.u[1] == [1., 0., 0.] +@test sol2.u[1] == [1., 0., 0.] + +### Testing on pendulum + +@parameters g = 9.81 L = 1. +@variables θ(t) = π/2 + +eqs = [D(D(θ)) ~ -(g / L) * sin(θ)] + +@mtkbuild pend = ODESystem(eqs, t) + +u0map = [θ => π/2, D(θ) => π/2] +parammap = [:L => 2.] +tspan = (0., 10.) + +bvp = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}(pend, u0map, tspan, parammap) +sol = solve(bvp, MIRK4(), dt = 0.05); + +bvp2 = SciMLBase.BVProblem{false, SciMLBase.AutoSpecialize}(pend, u0map, tspan, parammap) +sol2 = solve(bvp2, MIRK4(), dt = 0.05); + +osol = solve(pend) + +@test sol.u[end] ≈ osol.u[end] +@test sol.u[1] == [π/2, π/2] +@test sol2.u[end] ≈ osol.u[end] +@test sol2.u[1] == [π/2, π/2] From b063e6b7cc12747b6ffb7a474d5bbe06165867d5 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 2 Dec 2024 13:18:11 +0530 Subject: [PATCH 0414/2176] refactor: separate out `resid_prototype` calculation --- src/systems/nonlinear/nonlinearsystem.jl | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/src/systems/nonlinear/nonlinearsystem.jl b/src/systems/nonlinear/nonlinearsystem.jl index 94b44e0a6f..1289388197 100644 --- a/src/systems/nonlinear/nonlinearsystem.jl +++ b/src/systems/nonlinear/nonlinearsystem.jl @@ -283,6 +283,16 @@ function hessian_sparsity(sys::NonlinearSystem) unknowns(sys)) for eq in equations(sys)] end +function calculate_resid_prototype(N, u0, p) + u0ElType = u0 === nothing ? Float64 : eltype(u0) + if SciMLStructures.isscimlstructure(p) + u0ElType = promote_type( + eltype(SciMLStructures.canonicalize(SciMLStructures.Tunable(), p)[1]), + u0ElType) + end + return zeros(u0ElType, N) +end + """ ```julia SciMLBase.NonlinearFunction{iip}(sys::NonlinearSystem, dvs = unknowns(sys), @@ -337,13 +347,7 @@ function SciMLBase.NonlinearFunction{iip}(sys::NonlinearSystem, dvs = unknowns(s if length(dvs) == length(equations(sys)) resid_prototype = nothing else - u0ElType = u0 === nothing ? Float64 : eltype(u0) - if SciMLStructures.isscimlstructure(p) - u0ElType = promote_type( - eltype(SciMLStructures.canonicalize(SciMLStructures.Tunable(), p)[1]), - u0ElType) - end - resid_prototype = zeros(u0ElType, length(equations(sys))) + resid_prototype = calculate_resid_prototype(length(equations(sys)), u0, p) end NonlinearFunction{iip}(f, From c578da182393e8ba8efac181b796e9838d093680 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 2 Dec 2024 13:18:33 +0530 Subject: [PATCH 0415/2176] fix: recalculate `resid_prototype` in `remake_initialization_data` --- src/systems/nonlinear/initializesystem.jl | 11 ++++++++++- test/initializationsystem.jl | 17 +++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/src/systems/nonlinear/initializesystem.jl b/src/systems/nonlinear/initializesystem.jl index 4abb345822..726d171bd0 100644 --- a/src/systems/nonlinear/initializesystem.jl +++ b/src/systems/nonlinear/initializesystem.jl @@ -260,7 +260,16 @@ function SciMLBase.remake_initialization_data(sys::ODESystem, odefn, u0, t0, p, newp = remake_buffer( oldinitprob.f.sys, parameter_values(oldinitprob), pidxs, pvals) end - initprob = remake(oldinitprob; u0 = newu0, p = newp) + if oldinitprob.f.resid_prototype === nothing + newf = oldinitprob.f + else + newf = NonlinearFunction{ + SciMLBase.isinplace(oldinitprob.f), SciMLBase.specialization(oldinitprob.f)}( + oldinitprob.f; + resid_prototype = calculate_resid_prototype( + length(oldinitprob.f.resid_prototype), newu0, newp)) + end + initprob = remake(oldinitprob; f = newf, u0 = newu0, p = newp) return SciMLBase.OverrideInitData(initprob, odefn.update_initializeprob!, odefn.initializeprobmap, odefn.initializeprobpmap) end diff --git a/test/initializationsystem.jl b/test/initializationsystem.jl index 0b0dc42c1e..f3015f7db0 100644 --- a/test/initializationsystem.jl +++ b/test/initializationsystem.jl @@ -1032,3 +1032,20 @@ end @test prob3.f.initialization_data !== nothing @test init(prob3)[x] ≈ 0.5 end + +@testset "Issue#3246: type promotion with parameter dependent initialization_eqs" begin + @variables x(t)=1 y(t)=1 + @parameters a = 1 + @named sys = ODESystem([D(x) ~ 0, D(y) ~ x + a], t; initialization_eqs = [y ~ a]) + + ssys = structural_simplify(sys) + prob = ODEProblem(ssys, [], (0, 1), []) + + @test SciMLBase.successful_retcode(solve(prob)) + + seta = setsym_oop(prob, [a]) + (newu0, newp) = seta(prob, ForwardDiff.Dual{ForwardDiff.Tag{:tag, Float64}}.([1.0], 1)) + newprob = remake(prob, u0 = newu0, p = newp) + + @test SciMLBase.successful_retcode(solve(newprob)) +end From 44a60c5c171f312f7ee0afe7bc3acad62ad0714a Mon Sep 17 00:00:00 2001 From: ArnoStrouwen Date: Tue, 3 Dec 2024 01:09:00 +0100 Subject: [PATCH 0416/2176] update higher order documentation to modern MTK --- docs/src/examples/higher_order.md | 55 ++++++++++++++++--------------- 1 file changed, 28 insertions(+), 27 deletions(-) diff --git a/docs/src/examples/higher_order.md b/docs/src/examples/higher_order.md index 7dafe758dc..fac707525f 100644 --- a/docs/src/examples/higher_order.md +++ b/docs/src/examples/higher_order.md @@ -3,7 +3,7 @@ ModelingToolkit has a system for transformations of mathematical systems. These transformations allow for symbolically changing the representation of the model to problems that are easier to -numerically solve. One simple to demonstrate transformation is the +numerically solve. One simple to demonstrate transformation, is `structural_simplify`, which does a lot of tricks, one being the transformation that turns an Nth order ODE into N coupled 1st order ODEs. @@ -15,16 +15,28 @@ We utilize the derivative operator twice here to define the second order: using ModelingToolkit, OrdinaryDiffEq using ModelingToolkit: t_nounits as t, D_nounits as D -@parameters σ ρ β -@variables x(t) y(t) z(t) - -eqs = [D(D(x)) ~ σ * (y - x), - D(y) ~ x * (ρ - z) - y, - D(z) ~ x * y - β * z] - -@named sys = ODESystem(eqs, t) +@mtkmodel SECOND_ORDER begin + @parameters begin + σ = 28.0 + ρ = 10.0 + β = 8 / 3 + end + @variables begin + x(t) = 1.0 + y(t) = 0.0 + z(t) = 0.0 + end + @equations begin + D(D(x)) ~ σ * (y - x) + D(y) ~ x * (ρ - z) - y + D(z) ~ x * y - β * z + end +end +@mtkbuild sys = SECOND_ORDER() ``` +The second order ODE has been automatically transformed to two first order ODEs. + Note that we could've used an alternative syntax for 2nd order, i.e. `D = Differential(t)^2` and then `D(x)` would be the second derivative, and this syntax extends to `N`-th order. Also, we can use `*` or `∘` to compose @@ -33,28 +45,17 @@ and this syntax extends to `N`-th order. Also, we can use `*` or `∘` to compos Now let's transform this into the `ODESystem` of first order components. We do this by calling `structural_simplify`: -```@example orderlowering -sys = structural_simplify(sys) -``` - Now we can directly numerically solve the lowered system. Note that, following the original problem, the solution requires knowing the -initial condition for `x'`, and thus we include that in our input -specification: +initial condition for both `x` and `D(x)`. +The former already got assigned a default value in the `@mtkmodel`, +but we still have to provide a value for the latter. ```@example orderlowering -u0 = [D(x) => 2.0, - x => 1.0, - y => 0.0, - z => 0.0] - -p = [σ => 28.0, - ρ => 10.0, - β => 8 / 3] - +u0 = [D(sys.x) => 2.0] tspan = (0.0, 100.0) -prob = ODEProblem(sys, u0, tspan, p, jac = true) +prob = ODEProblem(sys, u0, tspan, [], jac = true) sol = solve(prob, Tsit5()) -using Plots; -plot(sol, idxs = (x, y)); +using Plots +plot(sol, idxs = (sys.x, sys.y)) ``` From a9fdfd6a3a114c4df28e4781c6b667990c01bafc Mon Sep 17 00:00:00 2001 From: vyudu Date: Tue, 3 Dec 2024 05:04:45 -0500 Subject: [PATCH 0417/2176] up --- src/systems/diffeqs/abstractodesystem.jl | 6 ++-- test/bvproblem.jl | 44 ++++++++++++------------ 2 files changed, 25 insertions(+), 25 deletions(-) diff --git a/src/systems/diffeqs/abstractodesystem.jl b/src/systems/diffeqs/abstractodesystem.jl index 112419dea8..b795bb981d 100644 --- a/src/systems/diffeqs/abstractodesystem.jl +++ b/src/systems/diffeqs/abstractodesystem.jl @@ -529,12 +529,12 @@ function SciMLBase.BVProblem{iip, specialize}(sys::AbstractODESystem, u0map = [] kwargs1 = merge(kwargs1, (callback = cbs,)) end - # Construct initial conditions + # Construct initial conditions. _u0 = u0 isa Function ? u0(tspan[1]) : u0 - # Define the boundary conditions + # Define the boundary conditions. bc = if iip - (residual, u, p, t) -> residual .= u[1] - _u0 + (residual, u, p, t) -> (residual .= u[1] - _u0) else (u, p, t) -> (u[1] - _u0) end diff --git a/test/bvproblem.jl b/test/bvproblem.jl index 90d41a96ad..4865c867b3 100644 --- a/test/bvproblem.jl +++ b/test/bvproblem.jl @@ -2,32 +2,31 @@ using BoundaryValueDiffEq, OrdinaryDiffEq using ModelingToolkit using ModelingToolkit: t_nounits as t, D_nounits as D -@parameters σ = 10. ρ = 28 β = 8/3 -@variables x(t) = 1 y(t) = 0 z(t) = 0 +@parameters α = 7.5 β = 4. γ = 8. δ = 5. +@variables x(t) = 1. y(t) = 2. -eqs = [D(x) ~ σ*(y-x), - D(y) ~ x*(ρ-z)-y, - D(z) ~ x*y - β*z] +eqs = [D(x) ~ α*x - β*x*y, + D(y) ~ -γ*y + δ*x*y] -u0map = [:x => 1., :y => 0., :z => 0.] -parammap = [:ρ => 28., :β => 8/3, :σ => 10.] +u0map = [:x => 1., :y => 2.] +parammap = [:α => 7.5, :β => 4, :γ => 8., :δ => 5.] tspan = (0., 10.) -@mtkbuild lorenz = ODESystem(eqs, t) +@mtkbuild lotkavolterra = ODESystem(eqs, t) -bvp = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}(lorenz, u0map, tspan, parammap) -sol = solve(bvp, MIRK4(), dt = 0.1); +bvp = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}(lotkavolterra, u0map, tspan, parammap) +sol = solve(bvp, MIRK4(), dt = 0.01); -bvp2 = SciMLBase.BVProblem{false, SciMLBase.AutoSpecialize}(lorenz, u0map, tspan, parammap) -sol2 = solve(bvp, MIRK4(), dt = 0.1); +bvp2 = SciMLBase.BVProblem{false, SciMLBase.AutoSpecialize}(lotkavolterra, u0map, tspan, parammap) +sol2 = solve(bvp, MIRK4(), dt = 0.01); -op = ODEProblem(lorenz, u0map, tspan, parammap) -osol = solve(op) +op = ODEProblem(lotkavolterra, u0map, tspan, parammap) +osol = solve(op, Vern9()) -@test sol.u[end] ≈ osol.u[end] -@test sol2.u[end] ≈ osol.u[end] -@test sol.u[1] == [1., 0., 0.] -@test sol2.u[1] == [1., 0., 0.] +@test isapprox(sol.u[end],osol.u[end]; atol = 0.001) +@test isapprox(sol2.u[end],osol.u[end]; atol = 0.001) +@test sol.u[1] == [1., 2.] +@test sol2.u[1] == [1., 2.] ### Testing on pendulum @@ -39,16 +38,17 @@ eqs = [D(D(θ)) ~ -(g / L) * sin(θ)] @mtkbuild pend = ODESystem(eqs, t) u0map = [θ => π/2, D(θ) => π/2] -parammap = [:L => 2.] +parammap = [:L => 2., :g => 9.81] tspan = (0., 10.) bvp = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}(pend, u0map, tspan, parammap) -sol = solve(bvp, MIRK4(), dt = 0.05); +sol = solve(bvp, MIRK4(), dt = 0.01); bvp2 = SciMLBase.BVProblem{false, SciMLBase.AutoSpecialize}(pend, u0map, tspan, parammap) -sol2 = solve(bvp2, MIRK4(), dt = 0.05); +sol2 = solve(bvp2, MIRK4(), dt = 0.01); -osol = solve(pend) +op = ODEProblem(pend, u0map, tspan, parammap) +osol = solve(op, Vern9()) @test sol.u[end] ≈ osol.u[end] @test sol.u[1] == [π/2, π/2] From a9f210691c1f38ef0f575eb89a939763b81c9c1b Mon Sep 17 00:00:00 2001 From: vyudu Date: Tue, 3 Dec 2024 19:11:35 -0500 Subject: [PATCH 0418/2176] up --- src/systems/diffeqs/abstractodesystem.jl | 4 ++-- test/bvproblem.jl | 8 ++++---- test/runtests.jl | 1 + 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/src/systems/diffeqs/abstractodesystem.jl b/src/systems/diffeqs/abstractodesystem.jl index b795bb981d..8dac19f296 100644 --- a/src/systems/diffeqs/abstractodesystem.jl +++ b/src/systems/diffeqs/abstractodesystem.jl @@ -534,7 +534,7 @@ function SciMLBase.BVProblem{iip, specialize}(sys::AbstractODESystem, u0map = [] # Define the boundary conditions. bc = if iip - (residual, u, p, t) -> (residual .= u[1] - _u0) + (residual, u, p, t) -> (residual .= u[1] .- _u0) else (u, p, t) -> (u[1] - _u0) end @@ -542,7 +542,7 @@ function SciMLBase.BVProblem{iip, specialize}(sys::AbstractODESystem, u0map = [] return BVProblem{iip}(f, bc, u0, tspan, p; kwargs1..., kwargs...) end -get_callback(prob::BVProblem) = prob.kwargs[:callback] +get_callback(prob::BVProblem) = error("BVP solvers do not support callbacks.") """ ```julia diff --git a/test/bvproblem.jl b/test/bvproblem.jl index 4865c867b3..7235638cf0 100644 --- a/test/bvproblem.jl +++ b/test/bvproblem.jl @@ -23,8 +23,8 @@ sol2 = solve(bvp, MIRK4(), dt = 0.01); op = ODEProblem(lotkavolterra, u0map, tspan, parammap) osol = solve(op, Vern9()) -@test isapprox(sol.u[end],osol.u[end]; atol = 0.001) -@test isapprox(sol2.u[end],osol.u[end]; atol = 0.001) +@test isapprox(sol.u[end],osol.u[end]; atol = 0.01) +@test isapprox(sol2.u[end],osol.u[end]; atol = 0.01) @test sol.u[1] == [1., 2.] @test sol2.u[1] == [1., 2.] @@ -50,7 +50,7 @@ sol2 = solve(bvp2, MIRK4(), dt = 0.01); op = ODEProblem(pend, u0map, tspan, parammap) osol = solve(op, Vern9()) -@test sol.u[end] ≈ osol.u[end] +@test isapprox(sol.u[end], osol.u[end]; atol = 0.01) @test sol.u[1] == [π/2, π/2] -@test sol2.u[end] ≈ osol.u[end] +@test isapprox(sol2.u[end], osol.u[end]; atol = 0.01) @test sol2.u[1] == [π/2, π/2] diff --git a/test/runtests.jl b/test/runtests.jl index 44846eed57..eaa87e6407 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -80,6 +80,7 @@ end @safetestset "NonlinearSystem Test" include("nonlinearsystem.jl") @safetestset "PDE Construction Test" include("pde.jl") @safetestset "JumpSystem Test" include("jumpsystem.jl") + @safetestset "BVProblem Test" include("bvproblem.jl") @safetestset "print_tree" include("print_tree.jl") @safetestset "Constraints Test" include("constraints.jl") end From c11e76a6bdeee93ffbfacd144356026ee6589eb9 Mon Sep 17 00:00:00 2001 From: ArnoStrouwen Date: Wed, 4 Dec 2024 01:32:46 +0100 Subject: [PATCH 0419/2176] add Lagrangian explanation to DAE reduction tutorial. --- docs/src/examples/modelingtoolkitize_index_reduction.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/docs/src/examples/modelingtoolkitize_index_reduction.md b/docs/src/examples/modelingtoolkitize_index_reduction.md index 415d5b85ff..8686fd60d4 100644 --- a/docs/src/examples/modelingtoolkitize_index_reduction.md +++ b/docs/src/examples/modelingtoolkitize_index_reduction.md @@ -51,6 +51,14 @@ In this tutorial, we will look at the pendulum system: \end{aligned} ``` +These equations can be derived using the [Lagrangian equation of the first kind.](https://en.wikipedia.org/wiki/Lagrangian_mechanics#Lagrangian) +Specifically, for a pendulum with unit mass and length $L$, which thus has +kinetic energy $\frac{1}{2}(v_x^2 + v_y^2)$, +potential energy $gy$, +and holonomic constraint $x^2 + y^2 - L^2 = 0$. +The Lagrange multiplier related to this constraint is equal to half of $T$, +and represents the tension in the rope of the pendulum. + As a good DifferentialEquations.jl user, one would follow [the mass matrix DAE tutorial](https://docs.sciml.ai/DiffEqDocs/stable/tutorials/dae_example/#Mass-Matrix-Differential-Algebraic-Equations-(DAEs)) to arrive at code for simulating the model: From 56a9bb33b582aff924d8b1a30c140f2f3382ca40 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 2 Dec 2024 17:18:07 +0530 Subject: [PATCH 0420/2176] fix: retain system data on `structural_simplify` of `SDESystem` --- src/systems/systems.jl | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/systems/systems.jl b/src/systems/systems.jl index a54206d1dd..862718968d 100644 --- a/src/systems/systems.jl +++ b/src/systems/systems.jl @@ -154,8 +154,9 @@ function __structural_simplify(sys::AbstractSystem, io = nothing; simplify = fal end noise_eqs = StructuralTransformations.tearing_substitute_expr(ode_sys, noise_eqs) - return SDESystem(full_equations(ode_sys), noise_eqs, + return SDESystem(Vector{Equation}(full_equations(ode_sys)), noise_eqs, get_iv(ode_sys), unknowns(ode_sys), parameters(ode_sys); - name = nameof(ode_sys), is_scalar_noise, observed = observed(ode_sys)) + name = nameof(ode_sys), is_scalar_noise, observed = observed(ode_sys), defaults = defaults(sys), + parameter_dependencies = parameter_dependencies(sys)) end end From 3b63f826b4b69838cff4e12d0a6146c32e614f40 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 4 Dec 2024 13:18:01 +0530 Subject: [PATCH 0421/2176] test: test observed equations are retained after simplifying `SDESystem` --- test/dde.jl | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/test/dde.jl b/test/dde.jl index 2030a90d06..c7561e6c24 100644 --- a/test/dde.jl +++ b/test/dde.jl @@ -76,12 +76,13 @@ prob = SDDEProblem(hayes_modelf, hayes_modelg, [1.0], h, tspan, pmul; constant_lags = (pmul[1],)); sol = solve(prob, RKMil(), seed = 100) -@variables x(..) +@variables x(..) delx(t) @parameters a=-4.0 b=-2.0 c=10.0 α=-1.3 β=-1.2 γ=1.1 @brownian η τ = 1.0 -eqs = [D(x(t)) ~ a * x(t) + b * x(t - τ) + c + (α * x(t) + γ) * η] +eqs = [D(x(t)) ~ a * x(t) + b * x(t - τ) + c + (α * x(t) + γ) * η, delx ~ x(t - τ)] @mtkbuild sys = System(eqs, t) +@test ModelingToolkit.has_observed_with_lhs(sys, delx) @test ModelingToolkit.is_dde(sys) @test !is_markovian(sys) @test equations(sys) == [D(x(t)) ~ a * x(t) + b * x(t - τ) + c] From 9c1bed9f22aeb0c1e490871509cc5e3361e5b1f7 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 4 Dec 2024 14:17:55 +0530 Subject: [PATCH 0422/2176] refactor: format --- docs/src/examples/modelingtoolkitize_index_reduction.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/src/examples/modelingtoolkitize_index_reduction.md b/docs/src/examples/modelingtoolkitize_index_reduction.md index 8686fd60d4..b19ea46701 100644 --- a/docs/src/examples/modelingtoolkitize_index_reduction.md +++ b/docs/src/examples/modelingtoolkitize_index_reduction.md @@ -56,7 +56,7 @@ Specifically, for a pendulum with unit mass and length $L$, which thus has kinetic energy $\frac{1}{2}(v_x^2 + v_y^2)$, potential energy $gy$, and holonomic constraint $x^2 + y^2 - L^2 = 0$. -The Lagrange multiplier related to this constraint is equal to half of $T$, +The Lagrange multiplier related to this constraint is equal to half of $T$, and represents the tension in the rope of the pendulum. As a good DifferentialEquations.jl user, one would follow From 565f02ada5ec6ee30bdf49f104067e2a44a52c51 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 4 Dec 2024 15:43:33 +0530 Subject: [PATCH 0423/2176] build: bump minor version --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 1621669c2c..b1f5701d13 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "ModelingToolkit" uuid = "961ee093-0014-501f-94e3-6117800e7a78" authors = ["Yingbo Ma ", "Chris Rackauckas and contributors"] -version = "9.54.0" +version = "9.55.0" [deps] AbstractTrees = "1520ce14-60c1-5f80-bbc7-55ef81b5835c" From 17359ce72a9c690ce53b809c041888038cd3f468 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 15 Nov 2024 18:49:53 +0530 Subject: [PATCH 0424/2176] feat: initial implementation of `SCCNonlinearProblem` codegen --- .../bipartite_tearing/modia_tearing.jl | 14 +- src/systems/abstractsystem.jl | 190 ++++++------------ src/systems/nonlinear/nonlinearsystem.jl | 111 ++++++++++ src/systems/parameter_buffer.jl | 35 +++- src/utils.jl | 47 +++++ 5 files changed, 250 insertions(+), 147 deletions(-) diff --git a/src/structural_transformation/bipartite_tearing/modia_tearing.jl b/src/structural_transformation/bipartite_tearing/modia_tearing.jl index cef2f5f6d7..5da873afdf 100644 --- a/src/structural_transformation/bipartite_tearing/modia_tearing.jl +++ b/src/structural_transformation/bipartite_tearing/modia_tearing.jl @@ -62,6 +62,15 @@ function tear_graph_block_modia!(var_eq_matching, ict, solvable_graph, eqs, vars return nothing end +function build_var_eq_matching(structure::SystemStructure, ::Type{U} = Unassigned; + varfilter::F2 = v -> true, eqfilter::F3 = eq -> true) where {U, F2, F3} + @unpack graph, solvable_graph = structure + var_eq_matching = maximal_matching(graph, eqfilter, varfilter, U) + matching_len = max(length(var_eq_matching), + maximum(x -> x isa Int ? x : 0, var_eq_matching, init = 0)) + return complete(var_eq_matching, matching_len), matching_len +end + function tear_graph_modia(structure::SystemStructure, isder::F = nothing, ::Type{U} = Unassigned; varfilter::F2 = v -> true, @@ -78,10 +87,7 @@ function tear_graph_modia(structure::SystemStructure, isder::F = nothing, # find them here [TODO: It would be good to have an explicit example of this.] @unpack graph, solvable_graph = structure - var_eq_matching = maximal_matching(graph, eqfilter, varfilter, U) - matching_len = max(length(var_eq_matching), - maximum(x -> x isa Int ? x : 0, var_eq_matching, init = 0)) - var_eq_matching = complete(var_eq_matching, matching_len) + var_eq_matching, matching_len = build_var_eq_matching(structure, U; varfilter, eqfilter) full_var_eq_matching = copy(var_eq_matching) var_sccs = find_var_sccs(graph, var_eq_matching) vargraph = DiCMOBiGraph{true}(graph, 0, Matching(matching_len)) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index 3a9bbd33e1..60ed2fa1ce 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -162,11 +162,12 @@ object. """ function generate_custom_function(sys::AbstractSystem, exprs, dvs = unknowns(sys), ps = parameters(sys); wrap_code = nothing, postprocess_fbody = nothing, states = nothing, - expression = Val{true}, eval_expression = false, eval_module = @__MODULE__, kwargs...) + expression = Val{true}, eval_expression = false, eval_module = @__MODULE__, + cachesyms::Tuple = (), kwargs...) if !iscomplete(sys) error("A completed system is required. Call `complete` or `structural_simplify` on the system.") end - p = reorder_parameters(sys, unwrap.(ps)) + p = (reorder_parameters(sys, unwrap.(ps))..., cachesyms...) isscalar = !(exprs isa AbstractArray) if wrap_code === nothing wrap_code = isscalar ? identity : (identity, identity) @@ -187,7 +188,7 @@ function generate_custom_function(sys::AbstractSystem, exprs, dvs = unknowns(sys postprocess_fbody, states, wrap_code = wrap_code .∘ wrap_mtkparameters(sys, isscalar) .∘ - wrap_array_vars(sys, exprs; dvs) .∘ + wrap_array_vars(sys, exprs; dvs, cachesyms) .∘ wrap_parameter_dependencies(sys, isscalar), expression = Val{true} ) @@ -199,7 +200,7 @@ function generate_custom_function(sys::AbstractSystem, exprs, dvs = unknowns(sys postprocess_fbody, states, wrap_code = wrap_code .∘ wrap_mtkparameters(sys, isscalar) .∘ - wrap_array_vars(sys, exprs; dvs) .∘ + wrap_array_vars(sys, exprs; dvs, cachesyms) .∘ wrap_parameter_dependencies(sys, isscalar), expression = Val{true} ) @@ -231,116 +232,51 @@ end function wrap_array_vars( sys::AbstractSystem, exprs; dvs = unknowns(sys), ps = parameters(sys), - inputs = nothing, history = false) + inputs = nothing, history = false, cachesyms::Tuple = ()) isscalar = !(exprs isa AbstractArray) - array_vars = Dict{Any, AbstractArray{Int}}() - if dvs !== nothing - for (j, x) in enumerate(dvs) - if iscall(x) && operation(x) == getindex - arg = arguments(x)[1] - inds = get!(() -> Int[], array_vars, arg) - push!(inds, j) - end - end - for (k, inds) in array_vars - if inds == (inds′ = inds[1]:inds[end]) - array_vars[k] = inds′ - end - end + var_to_arridxs = Dict() - uind = 1 - else + if dvs === nothing uind = 0 - end - # values are (indexes, index of buffer, size of parameter) - array_parameters = Dict{Any, Tuple{AbstractArray{Int}, Int, Tuple{Vararg{Int}}}}() - # If for some reason different elements of an array parameter are in different buffers - other_array_parameters = Dict{Any, Any}() - - hasinputs = inputs !== nothing - input_vars = Dict{Any, AbstractArray{Int}}() - if hasinputs - for (j, x) in enumerate(inputs) - if iscall(x) && operation(x) == getindex - arg = arguments(x)[1] - inds = get!(() -> Int[], input_vars, arg) - push!(inds, j) - end - end - for (k, inds) in input_vars - if inds == (inds′ = inds[1]:inds[end]) - input_vars[k] = inds′ - end - end - end - if has_index_cache(sys) - ic = get_index_cache(sys) else - ic = nothing - end - if ps isa Tuple && eltype(ps) <: AbstractArray - ps = Iterators.flatten(ps) - end - for p in ps - p = unwrap(p) - if iscall(p) && operation(p) == getindex - p = arguments(p)[1] - end - symtype(p) <: AbstractArray && Symbolics.shape(p) != Symbolics.Unknown() || continue - scal = collect(p) - # all scalarized variables are in `ps` - any(isequal(p), ps) || all(x -> any(isequal(x), ps), scal) || continue - (haskey(array_parameters, p) || haskey(other_array_parameters, p)) && continue - - idx = parameter_index(sys, p) - idx isa Int && continue - if idx isa ParameterIndex - if idx.portion != SciMLStructures.Tunable() + uind = 1 + for (i, x) in enumerate(dvs) + iscall(x) && operation(x) == getindex || continue + arg = arguments(x)[1] + inds = get!(() -> [], var_to_arridxs, arg) + push!(inds, (uind, i)) + end + end + p_start = uind + 1 + (inputs !== nothing) + history + input_ind = inputs === nothing ? -1 : (p_start - 1) + rps = (reorder_parameters(sys, ps)..., cachesyms...) + for sym in reduce(vcat, rps; init = []) + iscall(sym) && operation(sym) == getindex || continue + arg = arguments(sym)[1] + if inputs !== nothing + idx = findfirst(isequal(sym), inputs) + if idx !== nothing + inds = get!(() -> [], var_to_arridxs, arg) + push!(inds, (input_ind, idx)) continue end - array_parameters[p] = (vec(idx.idx), 1, size(idx.idx)) - else - # idx === nothing - idxs = map(Base.Fix1(parameter_index, sys), scal) - if first(idxs) isa ParameterIndex - buffer_idxs = map(Base.Fix1(iterated_buffer_index, ic), idxs) - if allequal(buffer_idxs) - buffer_idx = first(buffer_idxs) - if first(idxs).portion == SciMLStructures.Tunable() - idxs = map(x -> x.idx, idxs) - else - idxs = map(x -> x.idx[end], idxs) - end - else - other_array_parameters[p] = scal - continue - end - else - buffer_idx = 1 - end - - sz = size(idxs) - if vec(idxs) == idxs[begin]:idxs[end] - idxs = idxs[begin]:idxs[end] - elseif vec(idxs) == idxs[begin]:-1:idxs[end] - idxs = idxs[begin]:-1:idxs[end] - end - idxs = vec(idxs) - array_parameters[p] = (idxs, buffer_idx, sz) end + bufferidx = findfirst(buf -> any(isequal(sym), buf), rps) + idxinbuffer = findfirst(isequal(sym), rps[bufferidx]) + inds = get!(() -> [], var_to_arridxs, arg) + push!(inds, (p_start + bufferidx - 1, idxinbuffer)) end - inputind = if history - uind + 2 - else - uind + 1 - end - params_offset = if history && hasinputs - uind + 2 - elseif history || hasinputs - uind + 1 - else - uind + viewsyms = Dict() + splitsyms = Dict() + for (arrsym, idxs) in var_to_arridxs + length(idxs) == length(arrsym) || continue + # allequal(first, idxs) is a 1.11 feature + if allequal(Iterators.map(first, idxs)) + viewsyms[arrsym] = (first(first(idxs)), reshape(last.(idxs), size(arrsym))) + else + splitsyms[arrsym] = reshape(idxs, size(arrsym)) + end end if isscalar function (expr) @@ -349,15 +285,11 @@ function wrap_array_vars( [], Let( vcat( - [k ← :(view($(expr.args[uind].name), $v)) for (k, v) in array_vars], - [k ← :(view($(expr.args[inputind].name), $v)) - for (k, v) in input_vars], - [k ← :(reshape( - view($(expr.args[params_offset + buffer_idx].name), $idxs), - $sz)) - for (k, (idxs, buffer_idx, sz)) in array_parameters], - [k ← Code.MakeArray(v, symtype(k)) - for (k, v) in other_array_parameters] + [sym ← :(view($(expr.args[i].name), $idxs)) + for (sym, (i, idxs)) in viewsyms], + [sym ← + MakeArray([expr.args[bufi].elems[vali] for (bufi, vali) in idxs], + expr.args[idxs[1][1]]) for (sym, idxs) in splitsyms] ), expr.body, false @@ -371,15 +303,11 @@ function wrap_array_vars( [], Let( vcat( - [k ← :(view($(expr.args[uind].name), $v)) for (k, v) in array_vars], - [k ← :(view($(expr.args[inputind].name), $v)) - for (k, v) in input_vars], - [k ← :(reshape( - view($(expr.args[params_offset + buffer_idx].name), $idxs), - $sz)) - for (k, (idxs, buffer_idx, sz)) in array_parameters], - [k ← Code.MakeArray(v, symtype(k)) - for (k, v) in other_array_parameters] + [sym ← :(view($(expr.args[i].name), $idxs)) + for (sym, (i, idxs)) in viewsyms], + [sym ← + MakeArray([expr.args[bufi].elems[vali] for (bufi, vali) in idxs], + expr.args[idxs[1][1]]) for (sym, idxs) in splitsyms] ), expr.body, false @@ -392,17 +320,11 @@ function wrap_array_vars( [], Let( vcat( - [k ← :(view($(expr.args[uind + 1].name), $v)) - for (k, v) in array_vars], - [k ← :(view($(expr.args[inputind + 1].name), $v)) - for (k, v) in input_vars], - [k ← :(reshape( - view($(expr.args[params_offset + buffer_idx + 1].name), - $idxs), - $sz)) - for (k, (idxs, buffer_idx, sz)) in array_parameters], - [k ← Code.MakeArray(v, symtype(k)) - for (k, v) in other_array_parameters] + [sym ← :(view($(expr.args[i + 1].name), $idxs)) + for (sym, (i, idxs)) in viewsyms], + [sym ← MakeArray( + [expr.args[bufi + 1].elems[vali] for (bufi, vali) in idxs], + expr.args[idxs[1][1] + 1]) for (sym, idxs) in splitsyms] ), expr.body, false diff --git a/src/systems/nonlinear/nonlinearsystem.jl b/src/systems/nonlinear/nonlinearsystem.jl index 1289388197..a7028b3a1e 100644 --- a/src/systems/nonlinear/nonlinearsystem.jl +++ b/src/systems/nonlinear/nonlinearsystem.jl @@ -535,6 +535,117 @@ function DiffEqBase.NonlinearLeastSquaresProblem{iip}(sys::NonlinearSystem, u0ma NonlinearLeastSquaresProblem{iip}(f, u0, p; filter_kwargs(kwargs)...) end +struct CacheWriter{F} + fn::F +end + +function (cw::CacheWriter)(p, sols) + cw.fn(p.caches[1], sols, p...) +end + +function CacheWriter(sys::AbstractSystem, exprs, solsyms; + eval_expression = false, eval_module = @__MODULE__) + ps = parameters(sys) + rps = reorder_parameters(sys, ps) + fn = Func( + [:out, DestructuredArgs(DestructuredArgs.(solsyms)), + DestructuredArgs.(rps)...], + [], + SetArray(true, :out, exprs) + ) |> wrap_parameter_dependencies(sys, false)[2] |> + wrap_array_vars(sys, exprs; dvs = nothing)[2] |> toexpr + return CacheWriter(eval_or_rgf(fn; eval_expression, eval_module)) +end + +struct SCCNonlinearFunction{iip} end + +function SCCNonlinearFunction{iip}( + sys::NonlinearSystem, vscc, escc, cachesyms; eval_expression = false, + eval_module = @__MODULE__, kwargs...) where {iip} + dvs = unknowns(sys) + ps = parameters(sys) + rps = reorder_parameters(sys, ps) + eqs = equations(sys) + obs = observed(sys) + + _dvs = dvs[vscc] + _eqs = eqs[escc] + obsidxs = observed_equations_used_by(sys, _eqs) + _obs = obs[obsidxs] + + cmap, cs = get_cmap(sys) + assignments = [eq.lhs ← eq.rhs for eq in cmap] + rhss = [eq.rhs - eq.lhs for eq in _eqs] + wrap_code = wrap_assignments(false, assignments) .∘ + (wrap_array_vars(sys, rhss; dvs = _dvs, cachesyms)) .∘ + wrap_parameter_dependencies(sys, false) + @show _dvs + f_gen = build_function( + rhss, _dvs, ps..., cachesyms...; wrap_code, expression = Val{true}) + f_oop, f_iip = eval_or_rgf.(f_gen; eval_expression, eval_module) + + f(u, p) = f_oop(u, p) + f(u, p::MTKParameters) = f_oop(u, p...) + f(resid, u, p) = f_iip(resid, u, p) + f(resid, u, p::MTKParameters) = f_iip(resid, u, p...) + + return NonlinearFunction{iip}(f) +end + +function SciMLBase.SCCNonlinearProblem(sys::NonlinearSystem, args...; kwargs...) + SCCNonlinearProblem{true}(sys, args...; kwargs...) +end + +function SciMLBase.SCCNonlinearProblem{iip}(sys::NonlinearSystem, u0map, + parammap = SciMLBase.NullParameters(); eval_expression = false, eval_module = @__MODULE__, kwargs...) where {iip} + if !iscomplete(sys) || get_tearing_state(sys) === nothing + error("A simplified `NonlinearSystem` is required. Call `structural_simplify` on the system before creating an `SCCNonlinearProblem`.") + end + + if !is_split(sys) + error("The system has been simplified with `split = false`. `SCCNonlinearProblem` is not compatible with this system. Pass `split = true` to `structural_simplify` to use `SCCNonlinearProblem`.") + end + + ts = get_tearing_state(sys) + var_eq_matching, var_sccs = StructuralTransformations.algebraic_variables_scc(ts) + condensed_graph = StructuralTransformations.MatchedCondensationGraph( + StructuralTransformations.DiCMOBiGraph{true}(ts.structure.graph, var_eq_matching), + var_sccs) + toporder = topological_sort_by_dfs(condensed_graph) + var_sccs = var_sccs[toporder] + eq_sccs = map(Base.Fix1(getindex, var_eq_matching), var_sccs) + + dvs = unknowns(sys) + ps = parameters(sys) + eqs = equations(sys) + obs = observed(sys) + + _, u0, p = process_SciMLProblem( + EmptySciMLFunction, sys, u0map, parammap; eval_expression, eval_module, kwargs...) + p = rebuild_with_caches(p, BufferTemplate(eltype(u0), length(u0))) + + subprobs = [] + explicitfuns = [] + for (i, (escc, vscc)) in enumerate(zip(eq_sccs, var_sccs)) + oldvars = dvs[reduce(vcat, view(var_sccs, 1:(i - 1)); init = Int[])] + if isempty(oldvars) + push!(explicitfuns, (_...) -> nothing) + else + solsyms = getindex.((dvs,), view(var_sccs, 1:(i - 1))) + push!(explicitfuns, + CacheWriter(sys, oldvars, solsyms; eval_expression, eval_module)) + end + prob = NonlinearProblem( + SCCNonlinearFunction{iip}( + sys, vscc, escc, (oldvars,); eval_expression, eval_module, kwargs...), + u0[vscc], + p) + push!(subprobs, prob) + end + + return SCCNonlinearProblem(subprobs, explicitfuns) +end + """ $(TYPEDSIGNATURES) diff --git a/src/systems/parameter_buffer.jl b/src/systems/parameter_buffer.jl index 72af9f594d..7d64054acc 100644 --- a/src/systems/parameter_buffer.jl +++ b/src/systems/parameter_buffer.jl @@ -3,11 +3,12 @@ symconvert(::Type{T}, x) where {T} = convert(T, x) symconvert(::Type{Real}, x::Integer) = convert(Float64, x) symconvert(::Type{V}, x) where {V <: AbstractArray} = convert(V, symconvert.(eltype(V), x)) -struct MTKParameters{T, D, C, N} +struct MTKParameters{T, D, C, N, H} tunable::T discrete::D constant::C nonnumeric::N + caches::H end """ @@ -181,11 +182,18 @@ function MTKParameters( mtkps = MTKParameters{ typeof(tunable_buffer), typeof(disc_buffer), typeof(const_buffer), - typeof(nonnumeric_buffer)}(tunable_buffer, disc_buffer, const_buffer, - nonnumeric_buffer) + typeof(nonnumeric_buffer), typeof(())}(tunable_buffer, + disc_buffer, const_buffer, nonnumeric_buffer, ()) return mtkps end +function rebuild_with_caches(p::MTKParameters, cache_templates::BufferTemplate...) + buffers = map(cache_templates) do template + Vector{template.type}(undef, template.length) + end + @set p.caches = buffers +end + function narrow_buffer_type(buffer::AbstractArray) type = Union{} for x in buffer @@ -297,7 +305,8 @@ end for (Portion, field, recurse) in [(SciMLStructures.Discrete, :discrete, 1) (SciMLStructures.Constants, :constant, 1) - (Nonnumeric, :nonnumeric, 1)] + (Nonnumeric, :nonnumeric, 1) + (SciMLStructures.Caches, :caches, 1)] @eval function SciMLStructures.canonicalize(::$Portion, p::MTKParameters) as_vector = buffer_to_arraypartition(p.$field) repack = let p = p @@ -324,11 +333,13 @@ function Base.copy(p::MTKParameters) discrete = Tuple(eltype(buf) <: Real ? copy(buf) : copy.(buf) for buf in p.discrete) constant = Tuple(eltype(buf) <: Real ? copy(buf) : copy.(buf) for buf in p.constant) nonnumeric = copy.(p.nonnumeric) + caches = copy.(p.caches) return MTKParameters( tunable, discrete, constant, - nonnumeric + nonnumeric, + caches ) end @@ -640,7 +651,7 @@ end # getindex indexes the vectors, setindex! linearly indexes values # it's inconsistent, but we need it to be this way @generated function Base.getindex( - ps::MTKParameters{T, D, C, N}, idx::Int) where {T, D, C, N} + ps::MTKParameters{T, D, C, N, H}, idx::Int) where {T, D, C, N, H} paths = [] if !(T <: SizedVector{0, Float64}) push!(paths, :(ps.tunable)) @@ -654,6 +665,9 @@ end for i in 1:fieldcount(N) push!(paths, :(ps.nonnumeric[$i])) end + for i in 1:fieldcount(H) + push!(paths, :(ps.caches[$i])) + end expr = Expr(:if, :(idx == 1), :(return $(paths[1]))) curexpr = expr for i in 2:length(paths) @@ -663,12 +677,12 @@ end return Expr(:block, expr, :(throw(BoundsError(ps, idx)))) end -@generated function Base.length(ps::MTKParameters{T, D, C, N}) where {T, D, C, N} +@generated function Base.length(ps::MTKParameters{T, D, C, N, H}) where {T, D, C, N, H} len = 0 if !(T <: SizedVector{0, Float64}) len += 1 end - len += fieldcount(D) + fieldcount(C) + fieldcount(N) + len += fieldcount(D) + fieldcount(C) + fieldcount(N) + fieldcount(H) return len end @@ -691,7 +705,10 @@ end function Base.:(==)(a::MTKParameters, b::MTKParameters) return a.tunable == b.tunable && a.discrete == b.discrete && - a.constant == b.constant && a.nonnumeric == b.nonnumeric + a.constant == b.constant && a.nonnumeric == b.nonnumeric && + all(Iterators.map(a.caches, b.caches) do acache, bcache + eltype(acache) == eltype(bcache) && length(acache) == length(bcache) + end) end # to support linearize/linearization_function diff --git a/src/utils.jl b/src/utils.jl index 416efd8f2c..7be247429c 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -1007,3 +1007,50 @@ function is_variable_floatingpoint(sym) return T == Real || T <: AbstractFloat || T <: AbstractArray{Real} || T <: AbstractArray{<:AbstractFloat} end + +function observed_dependency_graph(eqs::Vector{Equation}) + for eq in eqs + if symbolic_type(eq.lhs) == NotSymbolic() + error("All equations must be observed equations of the form `var ~ expr`. Got $eq") + end + end + + idxmap = Dict(eq.lhs => i for (i, eq) in enumerate(eqs)) + g = SimpleDiGraph(length(eqs)) + + syms = Set() + for (i, eq) in enumerate(eqs) + vars!(syms, eq) + for sym in syms + idx = get(idxmap, sym, nothing) + idx === nothing && continue + add_edge!(g, i, idx) + end + end + + return g +end + +function observed_equations_used_by(sys::AbstractSystem, exprs) + obs = observed(sys) + + obsvars = getproperty.(obs, :lhs) + graph = observed_dependency_graph(obs) + + syms = vars(exprs) + + obsidxs = BitSet() + for sym in syms + idx = findfirst(isequal(sym), obsvars) + idx === nothing && continue + parents = dfs_parents(graph, idx) + for i in eachindex(parents) + parents[i] == 0 && continue + push!(obsidxs, i) + end + end + + obsidxs = collect(obsidxs) + sort!(obsidxs) + return obsidxs +end From 7d3b3f41dc7cd9cd746b2cdef7ca24896ba2e6ed Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 18 Nov 2024 15:35:49 +0530 Subject: [PATCH 0425/2176] refactor: no need to re-sort SCCs --- src/systems/nonlinear/nonlinearsystem.jl | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/src/systems/nonlinear/nonlinearsystem.jl b/src/systems/nonlinear/nonlinearsystem.jl index a7028b3a1e..68758de169 100644 --- a/src/systems/nonlinear/nonlinearsystem.jl +++ b/src/systems/nonlinear/nonlinearsystem.jl @@ -579,9 +579,8 @@ function SCCNonlinearFunction{iip}( wrap_code = wrap_assignments(false, assignments) .∘ (wrap_array_vars(sys, rhss; dvs = _dvs, cachesyms)) .∘ wrap_parameter_dependencies(sys, false) - @show _dvs f_gen = build_function( - rhss, _dvs, ps..., cachesyms...; wrap_code, expression = Val{true}) + rhss, _dvs, rps..., cachesyms...; wrap_code, expression = Val{true}) f_oop, f_iip = eval_or_rgf.(f_gen; eval_expression, eval_module) f(u, p) = f_oop(u, p) @@ -608,11 +607,9 @@ function SciMLBase.SCCNonlinearProblem{iip}(sys::NonlinearSystem, u0map, ts = get_tearing_state(sys) var_eq_matching, var_sccs = StructuralTransformations.algebraic_variables_scc(ts) - condensed_graph = StructuralTransformations.MatchedCondensationGraph( - StructuralTransformations.DiCMOBiGraph{true}(ts.structure.graph, var_eq_matching), - var_sccs) - toporder = topological_sort_by_dfs(condensed_graph) - var_sccs = var_sccs[toporder] + # The system is simplified, so SCCs are already in sorted order. We just need to get them and sort + # according to index in unknowns(sys) + sort!(var_sccs) eq_sccs = map(Base.Fix1(getindex, var_eq_matching), var_sccs) dvs = unknowns(sys) From 3e4a648488e9bd1a1fc276798a5b1274d8463eac Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 18 Nov 2024 19:27:58 +0530 Subject: [PATCH 0426/2176] fix: minor bug fix --- src/systems/nonlinear/nonlinearsystem.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/systems/nonlinear/nonlinearsystem.jl b/src/systems/nonlinear/nonlinearsystem.jl index 68758de169..1742e83371 100644 --- a/src/systems/nonlinear/nonlinearsystem.jl +++ b/src/systems/nonlinear/nonlinearsystem.jl @@ -553,7 +553,7 @@ function CacheWriter(sys::AbstractSystem, exprs, solsyms; [], SetArray(true, :out, exprs) ) |> wrap_parameter_dependencies(sys, false)[2] |> - wrap_array_vars(sys, exprs; dvs = nothing)[2] |> toexpr + wrap_array_vars(sys, exprs; dvs = nothing, inputs = [])[2] |> toexpr return CacheWriter(eval_or_rgf(fn; eval_expression, eval_module)) end From b99ab7d12c897333d28c4fe3bd01c34103069065 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 18 Nov 2024 21:51:43 +0530 Subject: [PATCH 0427/2176] fix: fix observed equations not being generated --- src/systems/nonlinear/nonlinearsystem.jl | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/systems/nonlinear/nonlinearsystem.jl b/src/systems/nonlinear/nonlinearsystem.jl index 1742e83371..9e6208ed10 100644 --- a/src/systems/nonlinear/nonlinearsystem.jl +++ b/src/systems/nonlinear/nonlinearsystem.jl @@ -572,13 +572,15 @@ function SCCNonlinearFunction{iip}( _eqs = eqs[escc] obsidxs = observed_equations_used_by(sys, _eqs) _obs = obs[obsidxs] + obs_assignments = [eq.lhs ← eq.rhs for eq in _obs] cmap, cs = get_cmap(sys) - assignments = [eq.lhs ← eq.rhs for eq in cmap] + cmap_assignments = [eq.lhs ← eq.rhs for eq in cmap] rhss = [eq.rhs - eq.lhs for eq in _eqs] - wrap_code = wrap_assignments(false, assignments) .∘ + wrap_code = wrap_assignments(false, cmap_assignments) .∘ (wrap_array_vars(sys, rhss; dvs = _dvs, cachesyms)) .∘ - wrap_parameter_dependencies(sys, false) + wrap_parameter_dependencies(sys, false) .∘ + wrap_assignments(false, obs_assignments) f_gen = build_function( rhss, _dvs, rps..., cachesyms...; wrap_code, expression = Val{true}) f_oop, f_iip = eval_or_rgf.(f_gen; eval_expression, eval_module) From 04c0cf883fe5714d32d3b04ecdc69c503318292b Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 18 Nov 2024 21:52:02 +0530 Subject: [PATCH 0428/2176] test: add tests for `SCCNonlinearProblem` codegen --- test/runtests.jl | 1 + test/scc_nonlinear_problem.jl | 145 ++++++++++++++++++++++++++++++++++ 2 files changed, 146 insertions(+) create mode 100644 test/scc_nonlinear_problem.jl diff --git a/test/runtests.jl b/test/runtests.jl index 44846eed57..677f40c717 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -78,6 +78,7 @@ end @safetestset "SDESystem Test" include("sdesystem.jl") @safetestset "DDESystem Test" include("dde.jl") @safetestset "NonlinearSystem Test" include("nonlinearsystem.jl") + @safetestset "SCCNonlinearProblem Test" include("scc_nonlinear_problem.jl") @safetestset "PDE Construction Test" include("pde.jl") @safetestset "JumpSystem Test" include("jumpsystem.jl") @safetestset "print_tree" include("print_tree.jl") diff --git a/test/scc_nonlinear_problem.jl b/test/scc_nonlinear_problem.jl new file mode 100644 index 0000000000..c4368bf0b9 --- /dev/null +++ b/test/scc_nonlinear_problem.jl @@ -0,0 +1,145 @@ +using ModelingToolkit +using NonlinearSolve, SCCNonlinearSolve +using OrdinaryDiffEq +using SciMLBase, Symbolics +using LinearAlgebra, Test + +@testset "Trivial case" begin + function f!(du, u, p) + du[1] = cos(u[2]) - u[1] + du[2] = sin(u[1] + u[2]) + u[2] + du[3] = 2u[4] + u[3] + 1.0 + du[4] = u[5]^2 + u[4] + du[5] = u[3]^2 + u[5] + du[6] = u[1] + u[2] + u[3] + u[4] + u[5] + 2.0u[6] + 2.5u[7] + 1.5u[8] + du[7] = u[1] + u[2] + u[3] + 2.0u[4] + u[5] + 4.0u[6] - 1.5u[7] + 1.5u[8] + du[8] = u[1] + 2.0u[2] + 3.0u[3] + 5.0u[4] + 6.0u[5] + u[6] - u[7] - u[8] + end + @variables u[1:8] [irreducible = true] + eqs = Any[0 for _ in 1:8] + f!(eqs, u, nothing) + eqs = 0 .~ eqs + @named model = NonlinearSystem(eqs) + @test_throws ["simplified", "required"] SCCNonlinearProblem(model, []) + _model = structural_simplify(model; split = false) + @test_throws ["not compatible"] SCCNonlinearProblem(_model, []) + model = structural_simplify(model) + prob = NonlinearProblem(model, [u => zeros(8)]) + sccprob = SCCNonlinearProblem(model, [u => zeros(8)]) + sol1 = solve(prob, NewtonRaphson()) + sol2 = solve(sccprob, NewtonRaphson()) + @test SciMLBase.successful_retcode(sol1) + @test SciMLBase.successful_retcode(sol2) + @test sol1.u ≈ sol2.u +end + +@testset "With parameters" begin + function f!(du, u, (p1, p2), t) + x = (*)(p1[4], u[1]) + y = (*)(p1[4], (+)(0.1016, (*)(-1, u[1]))) + z1 = ifelse((<)(p2[1], 0), + (*)((*)(457896.07999999996, p1[2]), sqrt((*)(1.1686468413521012e-5, p1[3]))), + 0) + z2 = ifelse((>)(p2[1], 0), + (*)((*)((*)(0.58, p1[2]), sqrt((*)(1 // 86100, p1[3]))), u[4]), + 0) + z3 = ifelse((>)(p2[1], 0), + (*)((*)(457896.07999999996, p1[2]), sqrt((*)(1.1686468413521012e-5, p1[3]))), + 0) + z4 = ifelse((<)(p2[1], 0), + (*)((*)((*)(0.58, p1[2]), sqrt((*)(1 // 86100, p1[3]))), u[5]), + 0) + du[1] = p2[1] + du[2] = (+)(z1, (*)(-1, z2)) + du[3] = (+)(z3, (*)(-1, z4)) + du[4] = (+)((*)(-1, u[2]), (*)((*)(1 // 86100, y), u[4])) + du[5] = (+)((*)(-1, u[3]), (*)((*)(1 // 86100, x), u[5])) + end + p = ( + [0.04864391799335977, 7.853981633974484e-5, 1.4034843205574914, + 0.018241469247509915, 300237.05, 9.226186337232914], + [0.0508]) + u0 = [0.0, 0.0, 0.0, 789476.0, 101325.0] + tspan = (0.0, 1.0) + mass_matrix = [1.0 0.0 0.0 0.0 0.0; 0.0 1.0 0.0 0.0 0.0; 0.0 0.0 1.0 0.0 0.0; + 0.0 0.0 0.0 0.0 0.0; 0.0 0.0 0.0 0.0 0.0] + dt = 1e-3 + function nlf(u1, (u0, p)) + resid = Any[0 for _ in u0] + f!(resid, u1, p, 0.0) + return mass_matrix * (u1 - u0) - dt * resid + end + + prob = NonlinearProblem(nlf, u0, (u0, p)) + @test_throws Exception solve(prob, SimpleNewtonRaphson(), abstol = 1e-9) + sol = solve(prob, TrustRegion(); abstol = 1e-9) + + @variables u[1:5] [irreducible = true] + @parameters p1[1:6] p2 + eqs = 0 .~ collect(nlf(u, (u0, (p1, p2)))) + @mtkbuild sys = NonlinearSystem(eqs, [u], [p1, p2]) + sccprob = SCCNonlinearProblem(sys, [u => u0], [p1 => p[1], p2 => p[2][]]) + sccsol = solve(sccprob, SimpleNewtonRaphson(); abstol = 1e-9) + @test SciMLBase.successful_retcode(sccsol) + @test norm(sccsol.resid) < norm(sol.resid) +end + +@testset "Transistor amplifier" begin + C = [k * 1e-6 for k in 1:5] + Ub = 6 + UF = 0.026 + α = 0.99 + β = 1e-6 + R0 = 1000 + R = 9000 + Ue(t) = 0.1 * sin(200 * π * t) + + function transamp(out, du, u, p, t) + g(x) = 1e-6 * (exp(x / 0.026) - 1) + y1, y2, y3, y4, y5, y6, y7, y8 = u + out[1] = -Ue(t) / R0 + y1 / R0 + C[1] * du[1] - C[1] * du[2] + out[2] = -Ub / R + y2 * 2 / R - (α - 1) * g(y2 - y3) - C[1] * du[1] + C[1] * du[2] + out[3] = -g(y2 - y3) + y3 / R + C[2] * du[3] + out[4] = -Ub / R + y4 / R + α * g(y2 - y3) + C[3] * du[4] - C[3] * du[5] + out[5] = -Ub / R + y5 * 2 / R - (α - 1) * g(y5 - y6) - C[3] * du[4] + C[3] * du[5] + out[6] = -g(y5 - y6) + y6 / R + C[4] * du[6] + out[7] = -Ub / R + y7 / R + α * g(y5 - y6) + C[5] * du[7] - C[5] * du[8] + out[8] = y8 / R - C[5] * du[7] + C[5] * du[8] + end + + u0 = [0, Ub / 2, Ub / 2, Ub, Ub / 2, Ub / 2, Ub, 0] + du0 = [ + 51.338775, + 51.338775, + -Ub / (2 * (C[2] * R)), + -24.9757667, + -24.9757667, + -Ub / (2 * (C[4] * R)), + -10.00564453, + -10.00564453 + ] + daeprob = DAEProblem(transamp, du0, u0, (0.0, 0.1)) + daesol = solve(daeprob, DImplicitEuler()) + + t0 = daesol.t[5] + t1 = daesol.t[6] + u0 = daesol.u[5] + u1 = daesol.u[6] + dt = t1 - t0 + + @variables y(t)[1:8] + eqs = Any[0 for _ in 1:8] + transamp(eqs, collect(D(y)), y, nothing, t) + eqs = 0 .~ eqs + subrules = Dict(Symbolics.unwrap(D(y[i])) => ((y[i] - u0[i]) / dt) for i in 1:8) + eqs = substitute.(eqs, (subrules,)) + @mtkbuild sys = NonlinearSystem(eqs) + prob = NonlinearProblem(sys, [y => u0], [t => t0]) + sol = solve(prob, NewtonRaphson(); abstol = 1e-12) + + sccprob = SCCNonlinearProblem(sys, [y => u0], [t => t0]) + sccsol = solve(sccprob, NewtonRaphson(); abstol = 1e-12) + + @test sol.u≈sccsol.u atol=1e-10 +end + From 5639bd11c390974a37ead01cca46d4959680dee8 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 19 Nov 2024 14:37:05 +0530 Subject: [PATCH 0429/2176] feat: pre-compute observed equations of previous SCCs --- src/systems/nonlinear/nonlinearsystem.jl | 53 +++++++++++++++--------- 1 file changed, 34 insertions(+), 19 deletions(-) diff --git a/src/systems/nonlinear/nonlinearsystem.jl b/src/systems/nonlinear/nonlinearsystem.jl index 9e6208ed10..b608fd642a 100644 --- a/src/systems/nonlinear/nonlinearsystem.jl +++ b/src/systems/nonlinear/nonlinearsystem.jl @@ -560,18 +560,11 @@ end struct SCCNonlinearFunction{iip} end function SCCNonlinearFunction{iip}( - sys::NonlinearSystem, vscc, escc, cachesyms; eval_expression = false, + sys::NonlinearSystem, _eqs, _dvs, _obs, cachesyms; eval_expression = false, eval_module = @__MODULE__, kwargs...) where {iip} - dvs = unknowns(sys) ps = parameters(sys) rps = reorder_parameters(sys, ps) - eqs = equations(sys) - obs = observed(sys) - _dvs = dvs[vscc] - _eqs = eqs[escc] - obsidxs = observed_equations_used_by(sys, _eqs) - _obs = obs[obsidxs] obs_assignments = [eq.lhs ← eq.rhs for eq in _obs] cmap, cs = get_cmap(sys) @@ -621,24 +614,46 @@ function SciMLBase.SCCNonlinearProblem{iip}(sys::NonlinearSystem, u0map, _, u0, p = process_SciMLProblem( EmptySciMLFunction, sys, u0map, parammap; eval_expression, eval_module, kwargs...) - p = rebuild_with_caches(p, BufferTemplate(eltype(u0), length(u0))) - subprobs = [] explicitfuns = [] + nlfuns = [] + prevobsidxs = Int[] + cachevars = [] + cacheexprs = [] for (i, (escc, vscc)) in enumerate(zip(eq_sccs, var_sccs)) - oldvars = dvs[reduce(vcat, view(var_sccs, 1:(i - 1)); init = Int[])] - if isempty(oldvars) - push!(explicitfuns, (_...) -> nothing) + # subset unknowns and equations + _dvs = dvs[vscc] + _eqs = eqs[escc] + # get observed equations required by this SCC + obsidxs = observed_equations_used_by(sys, _eqs) + # the ones used by previous SCCs can be precomputed into the cache + setdiff!(obsidxs, prevobsidxs) + _obs = obs[obsidxs] + + if isempty(cachevars) + push!(explicitfuns, Returns(nothing)) else solsyms = getindex.((dvs,), view(var_sccs, 1:(i - 1))) push!(explicitfuns, - CacheWriter(sys, oldvars, solsyms; eval_expression, eval_module)) + CacheWriter(sys, cacheexprs, solsyms; eval_expression, eval_module)) + end + f = SCCNonlinearFunction{iip}( + sys, _eqs, _dvs, _obs, (cachevars,); eval_expression, eval_module, kwargs...) + push!(nlfuns, f) + append!(cachevars, _dvs) + append!(cacheexprs, _dvs) + for i in obsidxs + push!(cachevars, obs[i].lhs) + push!(cacheexprs, obs[i].rhs) end - prob = NonlinearProblem( - SCCNonlinearFunction{iip}( - sys, vscc, escc, (oldvars,); eval_expression, eval_module, kwargs...), - u0[vscc], - p) + append!(prevobsidxs, obsidxs) + end + + p = rebuild_with_caches(p, BufferTemplate(eltype(u0), length(cachevars))) + + subprobs = [] + for (f, vscc) in zip(nlfuns, var_sccs) + prob = NonlinearProblem(f, u0[vscc], p) push!(subprobs, prob) end From 26269d974ebddd971e918b840a0d2d7fdf4f39b2 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 19 Nov 2024 14:37:22 +0530 Subject: [PATCH 0430/2176] feat: subset system and pass to SCC problems --- src/systems/index_cache.jl | 25 ++++++++++++++++++++++++ src/systems/nonlinear/nonlinearsystem.jl | 8 +++++++- 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/src/systems/index_cache.jl b/src/systems/index_cache.jl index ab0dd08764..aca6a0547d 100644 --- a/src/systems/index_cache.jl +++ b/src/systems/index_cache.jl @@ -594,3 +594,28 @@ function reorder_dimension_by_tunables( reorder_dimension_by_tunables!(buffer, sys, arr, syms; dim) return buffer end + +function subset_unknowns_observed( + ic::IndexCache, sys::AbstractSystem, newunknowns, newobsvars) + unknown_idx = copy(ic.unknown_idx) + empty!(unknown_idx) + for (i, sym) in enumerate(newunknowns) + ttsym = default_toterm(sym) + rsym = renamespace(sys, sym) + rttsym = renamespace(sys, ttsym) + unknown_idx[sym] = unknown_idx[ttsym] = unknown_idx[rsym] = unknown_idx[rttsym] = i + end + observed_syms_to_timeseries = copy(ic.observed_syms_to_timeseries) + empty!(observed_syms_to_timeseries) + for sym in newobsvars + ttsym = default_toterm(sym) + rsym = renamespace(sys, sym) + rttsym = renamespace(sys, ttsym) + for s in (sym, ttsym, rsym, rttsym) + observed_syms_to_timeseries[s] = ic.observed_syms_to_timeseries[sym] + end + end + ic = @set ic.unknown_idx = unknown_idx + @set! ic.observed_syms_to_timeseries = observed_syms_to_timeseries + return ic +end diff --git a/src/systems/nonlinear/nonlinearsystem.jl b/src/systems/nonlinear/nonlinearsystem.jl index b608fd642a..40e883222d 100644 --- a/src/systems/nonlinear/nonlinearsystem.jl +++ b/src/systems/nonlinear/nonlinearsystem.jl @@ -583,7 +583,13 @@ function SCCNonlinearFunction{iip}( f(resid, u, p) = f_iip(resid, u, p) f(resid, u, p::MTKParameters) = f_iip(resid, u, p...) - return NonlinearFunction{iip}(f) + subsys = NonlinearSystem(_eqs, _dvs, ps; observed = _obs, parameter_dependencies = parameter_dependencies(sys), name = nameof(sys)) + if get_index_cache(sys) !== nothing + @set! subsys.index_cache = subset_unknowns_observed(get_index_cache(sys), sys, _dvs, getproperty.(_obs, (:lhs,))) + @set! subsys.complete = true + end + + return NonlinearFunction{iip}(f; sys = subsys) end function SciMLBase.SCCNonlinearProblem(sys::NonlinearSystem, args...; kwargs...) From beb7070234a6cb24e9601c1fb7bedc89de847922 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 20 Nov 2024 18:12:24 +0530 Subject: [PATCH 0431/2176] refactor: improve `observed_dependency_graph` and add docstrings --- src/utils.jl | 28 +++++++++++++--------------- 1 file changed, 13 insertions(+), 15 deletions(-) diff --git a/src/utils.jl b/src/utils.jl index 7be247429c..d223c835e2 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -1008,29 +1008,27 @@ function is_variable_floatingpoint(sym) T <: AbstractArray{<:AbstractFloat} end +""" + $(TYPEDSIGNATURES) + +Return the `DiCMOBiGraph` denoting the dependencies between observed equations `eqs`. +""" function observed_dependency_graph(eqs::Vector{Equation}) for eq in eqs if symbolic_type(eq.lhs) == NotSymbolic() error("All equations must be observed equations of the form `var ~ expr`. Got $eq") end end - - idxmap = Dict(eq.lhs => i for (i, eq) in enumerate(eqs)) - g = SimpleDiGraph(length(eqs)) - - syms = Set() - for (i, eq) in enumerate(eqs) - vars!(syms, eq) - for sym in syms - idx = get(idxmap, sym, nothing) - idx === nothing && continue - add_edge!(g, i, idx) - end - end - - return g + graph, assigns = observed2graph(eqs, getproperty.(eqs, (:lhs,))) + matching = complete(Matching(Vector{Union{Unassigned, Int}}(assigns))) + return DiCMOBiGraph{false}(graph, matching) end +""" + $(TYPEDSIGNATURES) + +Return the indexes of observed equations of `sys` used by expression `exprs`. +""" function observed_equations_used_by(sys::AbstractSystem, exprs) obs = observed(sys) From 27d7f706dfb56eb995e1d7c686fbb16ce14c9d95 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 20 Nov 2024 18:14:15 +0530 Subject: [PATCH 0432/2176] feat: cache subexpressions dependent only on previous SCCs --- src/systems/nonlinear/nonlinearsystem.jl | 39 +++++++++-- src/utils.jl | 89 ++++++++++++++++++++++++ test/scc_nonlinear_problem.jl | 17 +++++ 3 files changed, 140 insertions(+), 5 deletions(-) diff --git a/src/systems/nonlinear/nonlinearsystem.jl b/src/systems/nonlinear/nonlinearsystem.jl index 40e883222d..84cfe58189 100644 --- a/src/systems/nonlinear/nonlinearsystem.jl +++ b/src/systems/nonlinear/nonlinearsystem.jl @@ -583,9 +583,11 @@ function SCCNonlinearFunction{iip}( f(resid, u, p) = f_iip(resid, u, p) f(resid, u, p::MTKParameters) = f_iip(resid, u, p...) - subsys = NonlinearSystem(_eqs, _dvs, ps; observed = _obs, parameter_dependencies = parameter_dependencies(sys), name = nameof(sys)) + subsys = NonlinearSystem(_eqs, _dvs, ps; observed = _obs, + parameter_dependencies = parameter_dependencies(sys), name = nameof(sys)) if get_index_cache(sys) !== nothing - @set! subsys.index_cache = subset_unknowns_observed(get_index_cache(sys), sys, _dvs, getproperty.(_obs, (:lhs,))) + @set! subsys.index_cache = subset_unknowns_observed( + get_index_cache(sys), sys, _dvs, getproperty.(_obs, (:lhs,))) @set! subsys.complete = true end @@ -624,8 +626,7 @@ function SciMLBase.SCCNonlinearProblem{iip}(sys::NonlinearSystem, u0map, explicitfuns = [] nlfuns = [] prevobsidxs = Int[] - cachevars = [] - cacheexprs = [] + cachesize = 0 for (i, (escc, vscc)) in enumerate(zip(eq_sccs, var_sccs)) # subset unknowns and equations _dvs = dvs[vscc] @@ -636,6 +637,32 @@ function SciMLBase.SCCNonlinearProblem{iip}(sys::NonlinearSystem, u0map, setdiff!(obsidxs, prevobsidxs) _obs = obs[obsidxs] + # get all subexpressions in the RHS which we can precompute in the cache + banned_vars = Set{Any}(vcat(_dvs, getproperty.(_obs, (:lhs,)))) + for var in banned_vars + iscall(var) || continue + operation(var) === getindex || continue + push!(banned_vars, arguments(var)[1]) + end + state = Dict() + for i in eachindex(_obs) + _obs[i] = _obs[i].lhs ~ subexpressions_not_involving_vars!( + _obs[i].rhs, banned_vars, state) + end + for i in eachindex(_eqs) + _eqs[i] = _eqs[i].lhs ~ subexpressions_not_involving_vars!( + _eqs[i].rhs, banned_vars, state) + end + + # cached variables and their corresponding expressions + cachevars = Any[obs[i].lhs for i in prevobsidxs] + cacheexprs = Any[obs[i].rhs for i in prevobsidxs] + for (k, v) in state + push!(cachevars, unwrap(v)) + push!(cacheexprs, unwrap(k)) + end + cachesize = max(cachesize, length(cachevars)) + if isempty(cachevars) push!(explicitfuns, Returns(nothing)) else @@ -655,7 +682,9 @@ function SciMLBase.SCCNonlinearProblem{iip}(sys::NonlinearSystem, u0map, append!(prevobsidxs, obsidxs) end - p = rebuild_with_caches(p, BufferTemplate(eltype(u0), length(cachevars))) + if cachesize != 0 + p = rebuild_with_caches(p, BufferTemplate(eltype(u0), cachesize)) + end subprobs = [] for (f, vscc) in zip(nlfuns, var_sccs) diff --git a/src/utils.jl b/src/utils.jl index d223c835e2..dd7113cbe6 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -1001,6 +1001,11 @@ end diff2term_with_unit(x, t) = _with_unit(diff2term, x, t) lower_varname_with_unit(var, iv, order) = _with_unit(lower_varname, var, iv, iv, order) +""" + $(TYPEDSIGNATURES) + +Check if `sym` represents a symbolic floating point number or array of such numbers. +""" function is_variable_floatingpoint(sym) sym = unwrap(sym) T = symtype(sym) @@ -1052,3 +1057,87 @@ function observed_equations_used_by(sys::AbstractSystem, exprs) sort!(obsidxs) return obsidxs end + +""" + $(TYPEDSIGNATURES) + +Given an expression `expr`, return a dictionary mapping subexpressions of `expr` that do +not involve variables in `vars` to anonymous symbolic variables. Also return the modified +`expr` with the substitutions indicated by the dictionary. If `expr` is a function +of only `vars`, then all of the returned subexpressions can be precomputed. + +Note that this will only process subexpressions floating point value. Additionally, +array variables must be passed in both scalarized and non-scalarized forms in `vars`. +""" +function subexpressions_not_involving_vars(expr, vars) + expr = unwrap(expr) + vars = map(unwrap, vars) + state = Dict() + newexpr = subexpressions_not_involving_vars!(expr, vars, state) + return state, newexpr +end + +""" + $(TYPEDSIGNATURES) + +Mutating version of `subexpressions_not_involving_vars` which writes to `state`. Only +returns the modified `expr`. +""" +function subexpressions_not_involving_vars!(expr, vars, state::Dict{Any, Any}) + expr = unwrap(expr) + symbolic_type(expr) == NotSymbolic() && return expr + iscall(expr) || return expr + is_variable_floatingpoint(expr) || return expr + symtype(expr) <: Union{Real, AbstractArray{<:Real}} || return expr + Symbolics.shape(expr) == Symbolics.Unknown() && return expr + haskey(state, expr) && return state[expr] + vs = ModelingToolkit.vars(expr) + intersect!(vs, vars) + if isempty(vs) + sym = gensym(:subexpr) + stype = symtype(expr) + var = similar_variable(expr, sym) + state[expr] = var + return var + end + op = operation(expr) + args = arguments(expr) + if (op == (+) || op == (*)) && symbolic_type(expr) !== ArraySymbolic() + indep_args = [] + dep_args = [] + for arg in args + _vs = ModelingToolkit.vars(arg) + intersect!(_vs, vars) + if !isempty(_vs) + push!(dep_args, subexpressions_not_involving_vars!(arg, vars, state)) + else + push!(indep_args, arg) + end + end + indep_term = reduce(op, indep_args; init = Int(op == (*))) + indep_term = subexpressions_not_involving_vars!(indep_term, vars, state) + dep_term = reduce(op, dep_args; init = Int(op == (*))) + return op(indep_term, dep_term) + end + newargs = map(args) do arg + symbolic_type(arg) != NotSymbolic() || is_array_of_symbolics(arg) || return arg + subexpressions_not_involving_vars!(arg, vars, state) + end + return maketerm(typeof(expr), op, newargs, metadata(expr)) +end + +""" + $(TYPEDSIGNATURES) + +Create an anonymous symbolic variable of the same shape, size and symtype as `var`, with +name `gensym(name)`. Does not support unsized array symbolics. +""" +function similar_variable(var::BasicSymbolic, name = :anon) + name = gensym(name) + stype = symtype(var) + sym = Symbolics.variable(name; T = stype) + if size(var) !== () + sym = setmetadata(sym, Symbolics.ArrayShapeCtx, map(Base.OneTo, size(var))) + end + return sym +end diff --git a/test/scc_nonlinear_problem.jl b/test/scc_nonlinear_problem.jl index c4368bf0b9..0c4bfe61d0 100644 --- a/test/scc_nonlinear_problem.jl +++ b/test/scc_nonlinear_problem.jl @@ -143,3 +143,20 @@ end @test sol.u≈sccsol.u atol=1e-10 end +@testset "Expression caching" begin + @variables x[1:4] = rand(4) + val = Ref(0) + function func(x, y) + val[] += 1 + x + y + end + @register_symbolic func(x, y) + @mtkbuild sys = NonlinearSystem([0 ~ x[1]^3 + x[2]^3 - 5 + 0 ~ sin(x[1] - x[2]) - 0.5 + 0 ~ func(x[1], x[2]) * exp(x[3]) - x[4]^3 - 5 + 0 ~ func(x[1], x[2]) * exp(x[4]) - x[3]^3 - 4]) + sccprob = SCCNonlinearProblem(sys, []) + sccsol = solve(sccprob, NewtonRaphson()) + @test SciMLBase.successful_retcode(sccsol) + @test val[] == 1 +end From 691747aa1053a6d18eb0ce8fdc80e063eaa3eaed Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 26 Nov 2024 12:16:31 +0530 Subject: [PATCH 0433/2176] feat: use SCCNonlinearProblem for initialization --- Project.toml | 4 +++- src/ModelingToolkit.jl | 1 + src/systems/diffeqs/abstractodesystem.jl | 13 ++++++++----- src/systems/nonlinear/nonlinearsystem.jl | 2 +- src/systems/problem_utils.jl | 6 ++++-- 5 files changed, 17 insertions(+), 9 deletions(-) diff --git a/Project.toml b/Project.toml index b1f5701d13..b520fa3e0c 100644 --- a/Project.toml +++ b/Project.toml @@ -44,6 +44,7 @@ PrecompileTools = "aea7be01-6a6a-4083-8856-8a6e6704d82a" RecursiveArrayTools = "731186ca-8d62-57ce-b412-fbd966d074cd" Reexport = "189a3867-3050-52da-a836-e630ba90ab69" RuntimeGeneratedFunctions = "7e49a35a-f44a-4d26-94aa-eba1b4ca6b47" +SCCNonlinearSolve = "9dfe8606-65a1-4bb3-9748-cb89d1561431" SciMLBase = "0bca4576-84f4-4d90-8ffe-ffa030f20462" SciMLStructures = "53ae85a6-f571-4167-b2af-e1d143709226" Serialization = "9e88b42a-f829-5b0c-bbe9-9e923198166b" @@ -126,7 +127,8 @@ REPL = "1" RecursiveArrayTools = "3.26" Reexport = "0.2, 1" RuntimeGeneratedFunctions = "0.5.9" -SciMLBase = "2.64" +SCCNonlinearSolve = "1.0.0" +SciMLBase = "2.65" SciMLStructures = "1.0" Serialization = "1" Setfield = "0.7, 0.8, 1" diff --git a/src/ModelingToolkit.jl b/src/ModelingToolkit.jl index de8e69c41f..59be358349 100644 --- a/src/ModelingToolkit.jl +++ b/src/ModelingToolkit.jl @@ -50,6 +50,7 @@ using Distributed import JuliaFormatter using MLStyle using NonlinearSolve +import SCCNonlinearSolve using Reexport using RecursiveArrayTools import Graphs: SimpleDiGraph, add_edge!, incidence_matrix diff --git a/src/systems/diffeqs/abstractodesystem.jl b/src/systems/diffeqs/abstractodesystem.jl index af96c4fcfe..ac8870fcaa 100644 --- a/src/systems/diffeqs/abstractodesystem.jl +++ b/src/systems/diffeqs/abstractodesystem.jl @@ -1301,6 +1301,7 @@ function InitializationProblem{iip, specialize}(sys::AbstractODESystem, initialization_eqs = [], fully_determined = nothing, check_units = true, + use_scc = true, kwargs...) where {iip, specialize} if !iscomplete(sys) error("A completed system is required. Call `complete` or `structural_simplify` on the system before creating an `ODEProblem`") @@ -1318,8 +1319,8 @@ function InitializationProblem{iip, specialize}(sys::AbstractODESystem, end ts = get_tearing_state(isys) - if warn_initialize_determined && - (unassigned_vars = StructuralTransformations.singular_check(ts); !isempty(unassigned_vars)) + unassigned_vars = StructuralTransformations.singular_check(ts) + if warn_initialize_determined && !isempty(unassigned_vars) errmsg = """ The initialization system is structurally singular. Guess values may \ significantly affect the initial values of the ODE. The problematic variables \ @@ -1381,9 +1382,11 @@ function InitializationProblem{iip, specialize}(sys::AbstractODESystem, end for (k, v) in u0map) end - if neqs == nunknown - NonlinearProblem(isys, u0map, parammap; kwargs...) + + TProb = if neqs == nunknown && isempty(unassigned_vars) + use_scc && neqs > 0 && is_split(isys) ? SCCNonlinearProblem : NonlinearProblem else - NonlinearLeastSquaresProblem(isys, u0map, parammap; kwargs...) + NonlinearLeastSquaresProblem end + TProb(isys, u0map, parammap; kwargs...) end diff --git a/src/systems/nonlinear/nonlinearsystem.jl b/src/systems/nonlinear/nonlinearsystem.jl index 84cfe58189..4299eba62d 100644 --- a/src/systems/nonlinear/nonlinearsystem.jl +++ b/src/systems/nonlinear/nonlinearsystem.jl @@ -692,7 +692,7 @@ function SciMLBase.SCCNonlinearProblem{iip}(sys::NonlinearSystem, u0map, push!(subprobs, prob) end - return SCCNonlinearProblem(subprobs, explicitfuns) + return SCCNonlinearProblem(subprobs, explicitfuns, sys, p) end """ diff --git a/src/systems/problem_utils.jl b/src/systems/problem_utils.jl index b837eef98e..ebc5a5bd75 100644 --- a/src/systems/problem_utils.jl +++ b/src/systems/problem_utils.jl @@ -541,6 +541,8 @@ Keyword arguments: Only applicable if `warn_cyclic_dependency == true`. - `substitution_limit`: The number times to substitute initial conditions into each other to attempt to arrive at a numeric value. +- `use_scc`: Whether to use `SCCNonlinearProblem` for initialization if the system is fully + determined. All other keyword arguments are passed as-is to `constructor`. """ @@ -554,7 +556,7 @@ function process_SciMLProblem( symbolic_u0 = false, warn_cyclic_dependency = false, circular_dependency_max_cycle_length = length(all_symbols(sys)), circular_dependency_max_cycles = 10, - substitution_limit = 100, kwargs...) + substitution_limit = 100, use_scc = true, kwargs...) dvs = unknowns(sys) ps = parameters(sys) iv = has_iv(sys) ? get_iv(sys) : nothing @@ -607,7 +609,7 @@ function process_SciMLProblem( sys, t, u0map, pmap; guesses, warn_initialize_determined, initialization_eqs, eval_expression, eval_module, fully_determined, warn_cyclic_dependency, check_units = check_initialization_units, - circular_dependency_max_cycle_length, circular_dependency_max_cycles) + circular_dependency_max_cycle_length, circular_dependency_max_cycles, use_scc) initializeprobmap = getu(initializeprob, unknowns(sys)) punknowns = [p From b5fc3ed1e19b9a0d38ba22c67afdf9dcc43751c1 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 26 Nov 2024 16:57:58 +0530 Subject: [PATCH 0434/2176] refactor: better handle inputs in `wrap_array_vars` --- src/systems/abstractsystem.jl | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index 60ed2fa1ce..e44f250a7f 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -247,20 +247,15 @@ function wrap_array_vars( push!(inds, (uind, i)) end end - p_start = uind + 1 + (inputs !== nothing) + history - input_ind = inputs === nothing ? -1 : (p_start - 1) + p_start = uind + 1 + history rps = (reorder_parameters(sys, ps)..., cachesyms...) + if inputs !== nothing + rps = (inputs, rps...) + end for sym in reduce(vcat, rps; init = []) iscall(sym) && operation(sym) == getindex || continue arg = arguments(sym)[1] - if inputs !== nothing - idx = findfirst(isequal(sym), inputs) - if idx !== nothing - inds = get!(() -> [], var_to_arridxs, arg) - push!(inds, (input_ind, idx)) - continue - end - end + bufferidx = findfirst(buf -> any(isequal(sym), buf), rps) idxinbuffer = findfirst(isequal(sym), rps[bufferidx]) inds = get!(() -> [], var_to_arridxs, arg) From e79dc902fc3377a8ff5adb5623d1bb26a969ecbe Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 26 Nov 2024 16:58:22 +0530 Subject: [PATCH 0435/2176] fix: properly sort SCCs --- src/systems/nonlinear/nonlinearsystem.jl | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/systems/nonlinear/nonlinearsystem.jl b/src/systems/nonlinear/nonlinearsystem.jl index 4299eba62d..bb37dadb8c 100644 --- a/src/systems/nonlinear/nonlinearsystem.jl +++ b/src/systems/nonlinear/nonlinearsystem.jl @@ -610,9 +610,17 @@ function SciMLBase.SCCNonlinearProblem{iip}(sys::NonlinearSystem, u0map, ts = get_tearing_state(sys) var_eq_matching, var_sccs = StructuralTransformations.algebraic_variables_scc(ts) - # The system is simplified, so SCCs are already in sorted order. We just need to get them and sort - # according to index in unknowns(sys) - sort!(var_sccs) + + if length(var_sccs) == 1 + return NonlinearProblem{iip}(sys, u0map, parammap; eval_expression, eval_module, kwargs...) + end + + condensed_graph = MatchedCondensationGraph( + DiCMOBiGraph{true}(complete(ts.structure.graph), + complete(var_eq_matching)), + var_sccs) + toporder = topological_sort_by_dfs(condensed_graph) + var_sccs = var_sccs[toporder] eq_sccs = map(Base.Fix1(getindex, var_eq_matching), var_sccs) dvs = unknowns(sys) From c1e1523cae45dc2e5a1ecb6d8aa8a914cb36aa3b Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 26 Nov 2024 16:58:38 +0530 Subject: [PATCH 0436/2176] feat: better handle observed variables, constants in SCCNonlinearProblem --- src/systems/nonlinear/nonlinearsystem.jl | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/src/systems/nonlinear/nonlinearsystem.jl b/src/systems/nonlinear/nonlinearsystem.jl index bb37dadb8c..636c1202f7 100644 --- a/src/systems/nonlinear/nonlinearsystem.jl +++ b/src/systems/nonlinear/nonlinearsystem.jl @@ -543,17 +543,22 @@ function (cw::CacheWriter)(p, sols) cw.fn(p.caches[1], sols, p...) end -function CacheWriter(sys::AbstractSystem, exprs, solsyms; +function CacheWriter(sys::AbstractSystem, exprs, solsyms, obseqs::Vector{Equation}; eval_expression = false, eval_module = @__MODULE__) ps = parameters(sys) rps = reorder_parameters(sys, ps) + obs_assigns = [eq.lhs ← eq.rhs for eq in obseqs] + cmap, cs = get_cmap(sys) + cmap_assigns = [eq.lhs ← eq.rhs for eq in cmap] fn = Func( [:out, DestructuredArgs(DestructuredArgs.(solsyms)), DestructuredArgs.(rps)...], [], SetArray(true, :out, exprs) - ) |> wrap_parameter_dependencies(sys, false)[2] |> - wrap_array_vars(sys, exprs; dvs = nothing, inputs = [])[2] |> toexpr + ) |> wrap_assignments(false, obs_assigns)[2] |> + wrap_parameter_dependencies(sys, false)[2] |> + wrap_array_vars(sys, exprs; dvs = nothing, inputs = [])[2] |> + wrap_assignments(false, cmap_assigns)[2] |> toexpr return CacheWriter(eval_or_rgf(fn; eval_expression, eval_module)) end @@ -612,7 +617,8 @@ function SciMLBase.SCCNonlinearProblem{iip}(sys::NonlinearSystem, u0map, var_eq_matching, var_sccs = StructuralTransformations.algebraic_variables_scc(ts) if length(var_sccs) == 1 - return NonlinearProblem{iip}(sys, u0map, parammap; eval_expression, eval_module, kwargs...) + return NonlinearProblem{iip}( + sys, u0map, parammap; eval_expression, eval_module, kwargs...) end condensed_graph = MatchedCondensationGraph( @@ -664,7 +670,7 @@ function SciMLBase.SCCNonlinearProblem{iip}(sys::NonlinearSystem, u0map, # cached variables and their corresponding expressions cachevars = Any[obs[i].lhs for i in prevobsidxs] - cacheexprs = Any[obs[i].rhs for i in prevobsidxs] + cacheexprs = Any[obs[i].lhs for i in prevobsidxs] for (k, v) in state push!(cachevars, unwrap(v)) push!(cacheexprs, unwrap(k)) @@ -676,7 +682,8 @@ function SciMLBase.SCCNonlinearProblem{iip}(sys::NonlinearSystem, u0map, else solsyms = getindex.((dvs,), view(var_sccs, 1:(i - 1))) push!(explicitfuns, - CacheWriter(sys, cacheexprs, solsyms; eval_expression, eval_module)) + CacheWriter(sys, cacheexprs, solsyms, obs[prevobsidxs]; + eval_expression, eval_module)) end f = SCCNonlinearFunction{iip}( sys, _eqs, _dvs, _obs, (cachevars,); eval_expression, eval_module, kwargs...) From 205d76a2f6b64820f59c17a2fdf8e2253f914cf4 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 26 Nov 2024 16:59:17 +0530 Subject: [PATCH 0437/2176] test: fix tests --- test/initializationsystem.jl | 2 +- test/mtkparameters.jl | 4 ++-- test/scc_nonlinear_problem.jl | 3 ++- test/split_parameters.jl | 2 +- 4 files changed, 6 insertions(+), 5 deletions(-) diff --git a/test/initializationsystem.jl b/test/initializationsystem.jl index f3015f7db0..9c06dd2030 100644 --- a/test/initializationsystem.jl +++ b/test/initializationsystem.jl @@ -28,7 +28,7 @@ sol = solve(initprob) initprob = ModelingToolkit.InitializationProblem(pend, 0.0, [x => 1, y => 0], [g => 1]; guesses = ModelingToolkit.missing_variable_defaults(pend)) -@test initprob isa NonlinearProblem +@test initprob isa NonlinearLeastSquaresProblem sol = solve(initprob) @test SciMLBase.successful_retcode(sol) @test sol.u == [0.0, 0.0, 0.0, 0.0] diff --git a/test/mtkparameters.jl b/test/mtkparameters.jl index 603426aaf7..ce524fdb76 100644 --- a/test/mtkparameters.jl +++ b/test/mtkparameters.jl @@ -299,7 +299,7 @@ end # Parameter timeseries ps = MTKParameters(([1.0, 1.0],), (BlockedArray(zeros(4), [2, 2]),), - (), ()) + (), (), ()) ps2 = SciMLStructures.replace(Discrete(), ps, ones(4)) @test typeof(ps2.discrete) == typeof(ps.discrete) with_updated_parameter_timeseries_values( @@ -316,7 +316,7 @@ with_updated_parameter_timeseries_values( ps = MTKParameters( (), (BlockedArray([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], [3, 3]), BlockedArray(falses(1), [1, 0])), - (), ()) + (), (), ()) @test SciMLBase.get_saveable_values(sys, ps, 1).x isa Tuple{Vector{Float64}, Vector{Bool}} tsidx1 = 1 tsidx2 = 2 diff --git a/test/scc_nonlinear_problem.jl b/test/scc_nonlinear_problem.jl index 0c4bfe61d0..fdf1646343 100644 --- a/test/scc_nonlinear_problem.jl +++ b/test/scc_nonlinear_problem.jl @@ -3,6 +3,7 @@ using NonlinearSolve, SCCNonlinearSolve using OrdinaryDiffEq using SciMLBase, Symbolics using LinearAlgebra, Test +using ModelingToolkit: t_nounits as t, D_nounits as D @testset "Trivial case" begin function f!(du, u, p) @@ -30,7 +31,7 @@ using LinearAlgebra, Test sol2 = solve(sccprob, NewtonRaphson()) @test SciMLBase.successful_retcode(sol1) @test SciMLBase.successful_retcode(sol2) - @test sol1.u ≈ sol2.u + @test sol1[u] ≈ sol2[u] end @testset "With parameters" begin diff --git a/test/split_parameters.jl b/test/split_parameters.jl index 22c90edf7a..2f8667faa8 100644 --- a/test/split_parameters.jl +++ b/test/split_parameters.jl @@ -206,7 +206,7 @@ S = get_sensitivity(closed_loop, :u) BlockedArray([[1 2; 3 4], [2 4; 6 8]], [1, 1])), # (BlockedArray([[true, false], [false, true]]), BlockedArray([[[1 2; 3 4]], [[2 4; 6 8]]])), ([5, 6],), - (["hi", "bye"], [:lie, :die])) + (["hi", "bye"], [:lie, :die]), ()) @test ps[ParameterIndex(Tunable(), 1)] == 1.0 @test ps[ParameterIndex(Tunable(), 2:4)] == collect(2.0:4.0) @test ps[ParameterIndex(Tunable(), reshape(4:7, 2, 2))] == reshape(4.0:7.0, 2, 2) From ed9cdf380488fdbd1e43753fb689f76e9474437e Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 26 Nov 2024 23:54:58 +0530 Subject: [PATCH 0438/2176] fix: reorder system in `SCCNonlinearProblem` --- src/systems/nonlinear/nonlinearsystem.jl | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/systems/nonlinear/nonlinearsystem.jl b/src/systems/nonlinear/nonlinearsystem.jl index 636c1202f7..24680fead4 100644 --- a/src/systems/nonlinear/nonlinearsystem.jl +++ b/src/systems/nonlinear/nonlinearsystem.jl @@ -707,6 +707,11 @@ function SciMLBase.SCCNonlinearProblem{iip}(sys::NonlinearSystem, u0map, push!(subprobs, prob) end + new_dvs = dvs[reduce(vcat, var_sccs)] + new_eqs = eqs[reduce(vcat, eq_sccs)] + @set! sys.unknowns = new_dvs + @set! sys.eqs = new_eqs + sys = complete(sys) return SCCNonlinearProblem(subprobs, explicitfuns, sys, p) end From a3819300c07a9e8c1a70ccff93b71574a832bc75 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Sat, 30 Nov 2024 23:28:06 +0530 Subject: [PATCH 0439/2176] build: bump OrdinaryDiffEqCore, OrdinaryDiffEqNonlinearSolve compats --- Project.toml | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Project.toml b/Project.toml index b520fa3e0c..7ac6f940be 100644 --- a/Project.toml +++ b/Project.toml @@ -121,7 +121,8 @@ NonlinearSolve = "3.14, 4" OffsetArrays = "1" OrderedCollections = "1" OrdinaryDiffEq = "6.82.0" -OrdinaryDiffEqCore = "1.7.0" +OrdinaryDiffEqCore = "1.13.0" +OrdinaryDiffEqNonlinearSolve = "1.3.0" PrecompileTools = "1" REPL = "1" RecursiveArrayTools = "3.26" @@ -162,6 +163,7 @@ OptimizationMOI = "fd9f6733-72f4-499f-8506-86b2bdd0dea1" OptimizationOptimJL = "36348300-93cb-4f02-beb5-3c3902f8871e" OrdinaryDiffEq = "1dea7af3-3e70-54e6-95c3-0bf5283fa5ed" OrdinaryDiffEqCore = "bbf590c4-e513-4bbe-9b18-05decba2e5d8" +OrdinaryDiffEqNonlinearSolve = "127b3ac7-2247-4354-8eb6-78cf4e7c58e8" Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f" REPL = "3fa0cd96-eef1-5676-8a61-b3b8758bbffb" Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c" @@ -176,4 +178,4 @@ Sundials = "c3572dad-4567-51f8-b174-8c6c989267f4" Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" [targets] -test = ["AmplNLWriter", "BenchmarkTools", "ControlSystemsBase", "DataInterpolations", "DelayDiffEq", "NonlinearSolve", "ForwardDiff", "Ipopt", "Ipopt_jll", "ModelingToolkitStandardLibrary", "Optimization", "OptimizationOptimJL", "OptimizationMOI", "OrdinaryDiffEq", "OrdinaryDiffEqCore", "REPL", "Random", "ReferenceTests", "SafeTestsets", "StableRNGs", "Statistics", "SteadyStateDiffEq", "Test", "StochasticDiffEq", "Sundials", "StochasticDelayDiffEq", "Pkg", "JET"] +test = ["AmplNLWriter", "BenchmarkTools", "ControlSystemsBase", "DataInterpolations", "DelayDiffEq", "NonlinearSolve", "ForwardDiff", "Ipopt", "Ipopt_jll", "ModelingToolkitStandardLibrary", "Optimization", "OptimizationOptimJL", "OptimizationMOI", "OrdinaryDiffEq", "OrdinaryDiffEqCore", "REPL", "Random", "ReferenceTests", "SafeTestsets", "StableRNGs", "Statistics", "SteadyStateDiffEq", "Test", "StochasticDiffEq", "Sundials", "StochasticDelayDiffEq", "Pkg", "JET", "OrdinaryDiffEqNonlinearSolve"] From 52eba502e7e85670095ee4dafe7b131370c5d14f Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 4 Dec 2024 14:14:27 +0530 Subject: [PATCH 0440/2176] refactor: update to new `SCCNonlinearProblem` constructor --- Project.toml | 2 +- src/systems/nonlinear/nonlinearsystem.jl | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Project.toml b/Project.toml index 7ac6f940be..d81b3d4ef9 100644 --- a/Project.toml +++ b/Project.toml @@ -129,7 +129,7 @@ RecursiveArrayTools = "3.26" Reexport = "0.2, 1" RuntimeGeneratedFunctions = "0.5.9" SCCNonlinearSolve = "1.0.0" -SciMLBase = "2.65" +SciMLBase = "2.66" SciMLStructures = "1.0" Serialization = "1" Setfield = "0.7, 0.8, 1" diff --git a/src/systems/nonlinear/nonlinearsystem.jl b/src/systems/nonlinear/nonlinearsystem.jl index 24680fead4..7826e06f76 100644 --- a/src/systems/nonlinear/nonlinearsystem.jl +++ b/src/systems/nonlinear/nonlinearsystem.jl @@ -712,7 +712,7 @@ function SciMLBase.SCCNonlinearProblem{iip}(sys::NonlinearSystem, u0map, @set! sys.unknowns = new_dvs @set! sys.eqs = new_eqs sys = complete(sys) - return SCCNonlinearProblem(subprobs, explicitfuns, sys, p) + return SCCNonlinearProblem(subprobs, explicitfuns, p, true; sys) end """ From 24d39af42431ac0fff321bb785e7ff6dac864120 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 4 Dec 2024 14:14:41 +0530 Subject: [PATCH 0441/2176] test: fix test to new SciMLBase error message --- test/symbolic_events.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/symbolic_events.jl b/test/symbolic_events.jl index 61690acdce..9d3aac4074 100644 --- a/test/symbolic_events.jl +++ b/test/symbolic_events.jl @@ -1063,7 +1063,7 @@ end cb = [x ~ 0.0] => [x ~ 0, y ~ 1] @mtkbuild pend = ODESystem(eqs, t; continuous_events = [cb]) prob = ODEProblem(pend, [x => 1], (0.0, 3.0), guesses = [y => x]) - @test_throws "CheckInit specified but initialization" solve(prob, Rodas5()) + @test_throws "DAE initialization failed" solve(prob, Rodas5()) cb = [x ~ 0.0] => [y ~ 1] @mtkbuild pend = ODESystem(eqs, t; continuous_events = [cb]) From 1320fc0bd6bd11e819405512bb48ab6737094fa8 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 5 Dec 2024 13:06:04 +0530 Subject: [PATCH 0442/2176] refactor: separate out operating point and initializeprob construction --- src/systems/problem_utils.jl | 154 +++++++++++++++++++++-------------- 1 file changed, 94 insertions(+), 60 deletions(-) diff --git a/src/systems/problem_utils.jl b/src/systems/problem_utils.jl index ebc5a5bd75..6b75fb2c6d 100644 --- a/src/systems/problem_utils.jl +++ b/src/systems/problem_utils.jl @@ -489,6 +489,89 @@ function EmptySciMLFunction(args...; kwargs...) return EmptySciMLFunction{typeof(args), typeof(kwargs)}(args, kwargs) end +""" + $(TYPEDSIGNATURES) + +Construct the operating point of the system from the user-provided `u0map` and `pmap`, system +defaults `defs`, constant equations `cmap` (from `get_cmap(sys)`), unknowns `dvs` and +parameters `ps`. Return the operating point as a dictionary, the list of unknowns for which +no values can be determined, and the list of parameters for which no values can be determined. +""" +function build_operating_point( + u0map::AbstractDict, pmap::AbstractDict, defs::AbstractDict, cmap, dvs, ps) + op = add_toterms(u0map) + missing_unknowns = add_fallbacks!(op, dvs, defs) + for (k, v) in defs + haskey(op, k) && continue + op[k] = v + end + merge!(op, pmap) + missing_pars = add_fallbacks!(op, ps, defs) + for eq in cmap + op[eq.lhs] = eq.rhs + end + return op, missing_unknowns, missing_pars +end + +""" + $(TYPEDSIGNATURES) + +Build and return the initialization problem and associated data as a `NamedTuple` to be passed +to the `SciMLFunction` constructor. Requires the system `sys`, operating point `op`, +user-provided `u0map` and `pmap`, initial time `t`, system defaults `defs`, user-provided +`guesses`, and list of unknowns which don't have a value in `op`. The keyword `implicit_dae` +denotes whether the `SciMLProblem` being constructed is in implicit DAE form (`DAEProblem`). +All other keyword arguments are forwarded to `InitializationProblem`. +""" +function maybe_build_initialization_problem( + sys::AbstractSystem, op::AbstractDict, u0map, pmap, t, defs, + guesses, missing_unknowns; implicit_dae = false, kwargs...) + guesses = merge(ModelingToolkit.guesses(sys), todict(guesses)) + has_observed_u0s = any( + k -> has_observed_with_lhs(sys, k) || has_parameter_dependency_with_lhs(sys, k), + keys(op)) + solvablepars = [p + for p in parameters(sys) + if is_parameter_solvable(p, pmap, defs, guesses)] + has_dependent_unknowns = any(unknowns(sys)) do sym + val = get(op, sym, nothing) + val === nothing && return false + return symbolic_type(val) != NotSymbolic() || is_array_of_symbolics(val) + end + if (((implicit_dae || has_observed_u0s || !isempty(missing_unknowns) || + !isempty(solvablepars) || has_dependent_unknowns) && + get_tearing_state(sys) !== nothing) || + !isempty(initialization_equations(sys))) && t !== nothing + initializeprob = ModelingToolkit.InitializationProblem( + sys, t, u0map, pmap; guesses, kwargs...) + initializeprobmap = getu(initializeprob, unknowns(sys)) + + punknowns = [p + for p in all_variable_symbols(initializeprob) + if is_parameter(sys, p)] + getpunknowns = getu(initializeprob, punknowns) + setpunknowns = setp(sys, punknowns) + initializeprobpmap = GetUpdatedMTKParameters(getpunknowns, setpunknowns) + + reqd_syms = parameter_symbols(initializeprob) + update_initializeprob! = UpdateInitializeprob( + getu(sys, reqd_syms), setu(initializeprob, reqd_syms)) + for p in punknowns + p = unwrap(p) + stype = symtype(p) + op[p] = get_temporary_value(p) + end + + for v in missing_unknowns + op[v] = zero_var(v) + end + empty!(missing_unknowns) + return (; + initializeprob, initializeprobmap, initializeprobpmap, update_initializeprob!) + end + return (;) +end + """ $(TYPEDSIGNATURES) @@ -576,67 +659,18 @@ function process_SciMLProblem( cmap, cs = get_cmap(sys) kwargs = NamedTuple(kwargs) - op = add_toterms(u0map) - missing_unknowns = add_fallbacks!(op, dvs, defs) - for (k, v) in defs - haskey(op, k) && continue - op[k] = v - end - merge!(op, pmap) - missing_pars = add_fallbacks!(op, ps, defs) - for eq in cmap - op[eq.lhs] = eq.rhs - end - if sys isa ODESystem - guesses = merge(ModelingToolkit.guesses(sys), todict(guesses)) - has_observed_u0s = any( - k -> has_observed_with_lhs(sys, k) || has_parameter_dependency_with_lhs(sys, k), - keys(op)) - solvablepars = [p - for p in parameters(sys) - if is_parameter_solvable(p, pmap, defs, guesses)] - has_dependent_unknowns = any(unknowns(sys)) do sym - val = get(op, sym, nothing) - val === nothing && return false - return symbolic_type(val) != NotSymbolic() || is_array_of_symbolics(val) - end - if build_initializeprob && - (((implicit_dae || has_observed_u0s || !isempty(missing_unknowns) || - !isempty(solvablepars) || has_dependent_unknowns) && - get_tearing_state(sys) !== nothing) || - !isempty(initialization_equations(sys))) && t !== nothing - initializeprob = ModelingToolkit.InitializationProblem( - sys, t, u0map, pmap; guesses, warn_initialize_determined, - initialization_eqs, eval_expression, eval_module, fully_determined, - warn_cyclic_dependency, check_units = check_initialization_units, - circular_dependency_max_cycle_length, circular_dependency_max_cycles, use_scc) - initializeprobmap = getu(initializeprob, unknowns(sys)) - - punknowns = [p - for p in all_variable_symbols(initializeprob) - if is_parameter(sys, p)] - getpunknowns = getu(initializeprob, punknowns) - setpunknowns = setp(sys, punknowns) - initializeprobpmap = GetUpdatedMTKParameters(getpunknowns, setpunknowns) - - reqd_syms = parameter_symbols(initializeprob) - update_initializeprob! = UpdateInitializeprob( - getu(sys, reqd_syms), setu(initializeprob, reqd_syms)) - for p in punknowns - p = unwrap(p) - stype = symtype(p) - op[p] = get_temporary_value(p) - delete!(missing_pars, p) - end + op, missing_unknowns, missing_pars = build_operating_point( + u0map, pmap, defs, cmap, dvs, ps) - for v in missing_unknowns - op[v] = zero_var(v) - end - empty!(missing_unknowns) - kwargs = merge(kwargs, - (; initializeprob, initializeprobmap, - initializeprobpmap, update_initializeprob!)) - end + if sys isa ODESystem && build_initializeprob + kws = maybe_build_initialization_problem( + sys, op, u0map, pmap, t, defs, guesses, missing_unknowns; + implicit_dae, warn_initialize_determined, initialization_eqs, + eval_expression, eval_module, fully_determined, + warn_cyclic_dependency, check_units = check_initialization_units, + circular_dependency_max_cycle_length, circular_dependency_max_cycles, use_scc) + + kwargs = merge(kwargs, kws) end if t !== nothing && !(constructor <: Union{DDEFunction, SDDEFunction}) From 35b407c728ba9a8590f1567140822cba9c57e924 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 5 Dec 2024 03:12:26 +0530 Subject: [PATCH 0443/2176] fix: fix `remake_initialization_data` on problems with no initprob --- src/systems/nonlinear/initializesystem.jl | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/systems/nonlinear/initializesystem.jl b/src/systems/nonlinear/initializesystem.jl index 726d171bd0..4544f0fa96 100644 --- a/src/systems/nonlinear/initializesystem.jl +++ b/src/systems/nonlinear/initializesystem.jl @@ -281,6 +281,8 @@ function SciMLBase.remake_initialization_data(sys::ODESystem, odefn, u0, t0, p, symbols_to_symbolics!(sys, pmap) guesses = Dict() defs = defaults(sys) + cmap, cs = get_cmap(sys) + if SciMLBase.has_initializeprob(odefn) oldsys = odefn.initializeprob.f.sys meta = get_metadata(oldsys) @@ -324,8 +326,9 @@ function SciMLBase.remake_initialization_data(sys::ODESystem, odefn, u0, t0, p, end filter_missing_values!(u0map) filter_missing_values!(pmap) - f, _ = process_SciMLProblem(EmptySciMLFunction, sys, u0map, pmap; guesses, t = t0) - kws = f.kwargs + + op, missing_unknowns, missing_pars = build_operating_point(u0map, pmap, defs, cmap, dvs, ps) + kws = maybe_build_initialization_problem(sys, op, u0map, pmap, t0, defs, guesses, missing_unknowns; use_scc = true) initprob = get(kws, :initializeprob, nothing) if initprob === nothing return nothing From 99435326bd009f38bdd9545801dfcfd2ac1e28b6 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 5 Dec 2024 13:11:17 +0530 Subject: [PATCH 0444/2176] refactor: add better warnings when SCC initialization cannot be used --- src/systems/diffeqs/abstractodesystem.jl | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/src/systems/diffeqs/abstractodesystem.jl b/src/systems/diffeqs/abstractodesystem.jl index ac8870fcaa..8b1476e582 100644 --- a/src/systems/diffeqs/abstractodesystem.jl +++ b/src/systems/diffeqs/abstractodesystem.jl @@ -1342,11 +1342,17 @@ function InitializationProblem{iip, specialize}(sys::AbstractODESystem, neqs = length(equations(isys)) nunknown = length(unknowns(isys)) + if use_scc + scc_message = "`SCCNonlinearProblem` can only be used for initialization of fully determined systems and hence will not be used here. " + else + scc_message = "" + end + if warn_initialize_determined && neqs > nunknown - @warn "Initialization system is overdetermined. $neqs equations for $nunknown unknowns. Initialization will default to using least squares. To suppress this warning pass warn_initialize_determined = false. To make this warning into an error, pass fully_determined = true" + @warn "Initialization system is overdetermined. $neqs equations for $nunknown unknowns. Initialization will default to using least squares. $(scc_message)To suppress this warning pass warn_initialize_determined = false. To make this warning into an error, pass fully_determined = true" end if warn_initialize_determined && neqs < nunknown - @warn "Initialization system is underdetermined. $neqs equations for $nunknown unknowns. Initialization will default to using least squares. To suppress this warning pass warn_initialize_determined = false. To make this warning into an error, pass fully_determined = true" + @warn "Initialization system is underdetermined. $neqs equations for $nunknown unknowns. Initialization will default to using least squares. $(scc_message)To suppress this warning pass warn_initialize_determined = false. To make this warning into an error, pass fully_determined = true" end parammap = parammap isa DiffEqBase.NullParameters || isempty(parammap) ? @@ -1384,7 +1390,16 @@ function InitializationProblem{iip, specialize}(sys::AbstractODESystem, end TProb = if neqs == nunknown && isempty(unassigned_vars) - use_scc && neqs > 0 && is_split(isys) ? SCCNonlinearProblem : NonlinearProblem + if use_scc && neqs > 0 + if is_split(isys) + SCCNonlinearProblem + else + @warn "`SCCNonlinearProblem` can only be used with `split = true` systems. Simplify your `ODESystem` with `split = true` or pass `use_scc = false` to disable this warning" + NonlinearProblem + end + else + NonlinearProblem + end else NonlinearLeastSquaresProblem end From 0537715df6c5a0ed03c5d47b26a0768b42b2b184 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 5 Dec 2024 13:22:29 +0530 Subject: [PATCH 0445/2176] refactor: propagate `use_scc` to `remake_initialization_data` --- src/systems/diffeqs/abstractodesystem.jl | 6 ++++-- src/systems/nonlinear/initializesystem.jl | 14 ++++++++++---- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/src/systems/diffeqs/abstractodesystem.jl b/src/systems/diffeqs/abstractodesystem.jl index 8b1476e582..f4e29346ff 100644 --- a/src/systems/diffeqs/abstractodesystem.jl +++ b/src/systems/diffeqs/abstractodesystem.jl @@ -1311,11 +1311,13 @@ function InitializationProblem{iip, specialize}(sys::AbstractODESystem, elseif isempty(u0map) && get_initializesystem(sys) === nothing isys = structural_simplify( generate_initializesystem( - sys; initialization_eqs, check_units, pmap = parammap, guesses); fully_determined) + sys; initialization_eqs, check_units, pmap = parammap, + guesses, extra_metadata = (; use_scc)); fully_determined) else isys = structural_simplify( generate_initializesystem( - sys; u0map, initialization_eqs, check_units, pmap = parammap, guesses); fully_determined) + sys; u0map, initialization_eqs, check_units, + pmap = parammap, guesses, extra_metadata = (; use_scc)); fully_determined) end ts = get_tearing_state(isys) diff --git a/src/systems/nonlinear/initializesystem.jl b/src/systems/nonlinear/initializesystem.jl index 4544f0fa96..2344727920 100644 --- a/src/systems/nonlinear/initializesystem.jl +++ b/src/systems/nonlinear/initializesystem.jl @@ -11,7 +11,7 @@ function generate_initializesystem(sys::ODESystem; default_dd_guess = 0.0, algebraic_only = false, check_units = true, check_defguess = false, - name = nameof(sys), kwargs...) + name = nameof(sys), extra_metadata = (;), kwargs...) trueobs, eqs = unhack_observed(observed(sys), equations(sys)) vars = unique([unknowns(sys); getfield.(trueobs, :lhs)]) vars_set = Set(vars) # for efficient in-lookup @@ -179,7 +179,8 @@ function generate_initializesystem(sys::ODESystem; for k in keys(defs) defs[k] = substitute(defs[k], paramsubs) end - meta = InitializationSystemMetadata(anydict(u0map), anydict(pmap), additional_guesses) + meta = InitializationSystemMetadata( + anydict(u0map), anydict(pmap), additional_guesses, extra_metadata) return NonlinearSystem(eqs_ics, vars, pars; @@ -195,6 +196,7 @@ struct InitializationSystemMetadata u0map::Dict{Any, Any} pmap::Dict{Any, Any} additional_guesses::Dict{Any, Any} + extra_metadata::NamedTuple end function is_parameter_solvable(p, pmap, defs, guesses) @@ -282,6 +284,7 @@ function SciMLBase.remake_initialization_data(sys::ODESystem, odefn, u0, t0, p, guesses = Dict() defs = defaults(sys) cmap, cs = get_cmap(sys) + use_scc = true if SciMLBase.has_initializeprob(odefn) oldsys = odefn.initializeprob.f.sys @@ -290,6 +293,7 @@ function SciMLBase.remake_initialization_data(sys::ODESystem, odefn, u0, t0, p, u0map = merge(meta.u0map, u0map) pmap = merge(meta.pmap, pmap) merge!(guesses, meta.additional_guesses) + use_scc = get(meta.extra_metadata, :use_scc, true) end else # there is no initializeprob, so the original problem construction @@ -327,8 +331,10 @@ function SciMLBase.remake_initialization_data(sys::ODESystem, odefn, u0, t0, p, filter_missing_values!(u0map) filter_missing_values!(pmap) - op, missing_unknowns, missing_pars = build_operating_point(u0map, pmap, defs, cmap, dvs, ps) - kws = maybe_build_initialization_problem(sys, op, u0map, pmap, t0, defs, guesses, missing_unknowns; use_scc = true) + op, missing_unknowns, missing_pars = build_operating_point( + u0map, pmap, defs, cmap, dvs, ps) + kws = maybe_build_initialization_problem( + sys, op, u0map, pmap, t0, defs, guesses, missing_unknowns; use_scc) initprob = get(kws, :initializeprob, nothing) if initprob === nothing return nothing From ab5747f208a9cf5c2924fa251c739053cccf859d Mon Sep 17 00:00:00 2001 From: Christopher Rackauckas Date: Thu, 5 Dec 2024 07:30:41 -0700 Subject: [PATCH 0446/2176] Update Project.toml --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index d81b3d4ef9..fe3a9e88e5 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "ModelingToolkit" uuid = "961ee093-0014-501f-94e3-6117800e7a78" authors = ["Yingbo Ma ", "Chris Rackauckas and contributors"] -version = "9.55.0" +version = "9.56.0" [deps] AbstractTrees = "1520ce14-60c1-5f80-bbc7-55ef81b5835c" From 82198b7f49cb707b31b06af6d49dd0a7ecd33160 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Sat, 30 Nov 2024 14:09:11 +0530 Subject: [PATCH 0447/2176] refactor: move HomtopyContinuation-related code to its own file --- src/ModelingToolkit.jl | 1 + .../nonlinear/homotopy_continuation.jl | 68 ++++++++++++++++++ src/systems/nonlinear/nonlinearsystem.jl | 69 ------------------- 3 files changed, 69 insertions(+), 69 deletions(-) create mode 100644 src/systems/nonlinear/homotopy_continuation.jl diff --git a/src/ModelingToolkit.jl b/src/ModelingToolkit.jl index 59be358349..de9a06d774 100644 --- a/src/ModelingToolkit.jl +++ b/src/ModelingToolkit.jl @@ -150,6 +150,7 @@ include("systems/callbacks.jl") include("systems/problem_utils.jl") include("systems/nonlinear/nonlinearsystem.jl") +include("systems/nonlinear/homotopy_continuation.jl") include("systems/diffeqs/odesystem.jl") include("systems/diffeqs/sdesystem.jl") include("systems/diffeqs/abstractodesystem.jl") diff --git a/src/systems/nonlinear/homotopy_continuation.jl b/src/systems/nonlinear/homotopy_continuation.jl new file mode 100644 index 0000000000..e41a3f0ce7 --- /dev/null +++ b/src/systems/nonlinear/homotopy_continuation.jl @@ -0,0 +1,68 @@ +""" +$(TYPEDEF) + +A type of Nonlinear problem which specializes on polynomial systems and uses +HomotopyContinuation.jl to solve the system. Requires importing HomotopyContinuation.jl to +create and solve. +""" +struct HomotopyContinuationProblem{uType, H, D, O, SS, U} <: + SciMLBase.AbstractNonlinearProblem{uType, true} + """ + The initial values of states in the system. If there are multiple real roots of + the system, the one closest to this point is returned. + """ + u0::uType + """ + A subtype of `HomotopyContinuation.AbstractSystem` to solve. Also contains the + parameter object. + """ + homotopy_continuation_system::H + """ + A function with signature `(u, p) -> resid`. In case of rational functions, this + is used to rule out roots of the system which would cause the denominator to be + zero. + """ + denominator::D + """ + The `NonlinearSystem` used to create this problem. Used for symbolic indexing. + """ + sys::NonlinearSystem + """ + A function which generates and returns observed expressions for the given system. + """ + obsfn::O + """ + The HomotopyContinuation.jl solver and start system, obtained through + `HomotopyContinuation.solver_startsystems`. + """ + solver_and_starts::SS + """ + A function which takes a solution of the transformed system, and returns a vector + of solutions for the original system. This is utilized when converting systems + to polynomials. + """ + unpack_solution::U +end + +function HomotopyContinuationProblem(::AbstractSystem, _u0, _p; kwargs...) + error("HomotopyContinuation.jl is required to create and solve `HomotopyContinuationProblem`s. Please run `Pkg.add(\"HomotopyContinuation\")` to continue.") +end + +SymbolicIndexingInterface.symbolic_container(p::HomotopyContinuationProblem) = p.sys +SymbolicIndexingInterface.state_values(p::HomotopyContinuationProblem) = p.u0 +function SymbolicIndexingInterface.set_state!(p::HomotopyContinuationProblem, args...) + set_state!(p.u0, args...) +end +function SymbolicIndexingInterface.parameter_values(p::HomotopyContinuationProblem) + parameter_values(p.homotopy_continuation_system) +end +function SymbolicIndexingInterface.set_parameter!(p::HomotopyContinuationProblem, args...) + set_parameter!(parameter_values(p), args...) +end +function SymbolicIndexingInterface.observed(p::HomotopyContinuationProblem, sym) + if p.obsfn !== nothing + return p.obsfn(sym) + else + return SymbolicIndexingInterface.observed(p.sys, sym) + end +end diff --git a/src/systems/nonlinear/nonlinearsystem.jl b/src/systems/nonlinear/nonlinearsystem.jl index 7826e06f76..ff4a7d0eb2 100644 --- a/src/systems/nonlinear/nonlinearsystem.jl +++ b/src/systems/nonlinear/nonlinearsystem.jl @@ -866,72 +866,3 @@ function Base.:(==)(sys1::NonlinearSystem, sys2::NonlinearSystem) _eq_unordered(get_ps(sys1), get_ps(sys2)) && all(s1 == s2 for (s1, s2) in zip(get_systems(sys1), get_systems(sys2))) end - -""" -$(TYPEDEF) - -A type of Nonlinear problem which specializes on polynomial systems and uses -HomotopyContinuation.jl to solve the system. Requires importing HomotopyContinuation.jl to -create and solve. -""" -struct HomotopyContinuationProblem{uType, H, D, O, SS, U} <: - SciMLBase.AbstractNonlinearProblem{uType, true} - """ - The initial values of states in the system. If there are multiple real roots of - the system, the one closest to this point is returned. - """ - u0::uType - """ - A subtype of `HomotopyContinuation.AbstractSystem` to solve. Also contains the - parameter object. - """ - homotopy_continuation_system::H - """ - A function with signature `(u, p) -> resid`. In case of rational functions, this - is used to rule out roots of the system which would cause the denominator to be - zero. - """ - denominator::D - """ - The `NonlinearSystem` used to create this problem. Used for symbolic indexing. - """ - sys::NonlinearSystem - """ - A function which generates and returns observed expressions for the given system. - """ - obsfn::O - """ - The HomotopyContinuation.jl solver and start system, obtained through - `HomotopyContinuation.solver_startsystems`. - """ - solver_and_starts::SS - """ - A function which takes a solution of the transformed system, and returns a vector - of solutions for the original system. This is utilized when converting systems - to polynomials. - """ - unpack_solution::U -end - -function HomotopyContinuationProblem(::AbstractSystem, _u0, _p; kwargs...) - error("HomotopyContinuation.jl is required to create and solve `HomotopyContinuationProblem`s. Please run `Pkg.add(\"HomotopyContinuation\")` to continue.") -end - -SymbolicIndexingInterface.symbolic_container(p::HomotopyContinuationProblem) = p.sys -SymbolicIndexingInterface.state_values(p::HomotopyContinuationProblem) = p.u0 -function SymbolicIndexingInterface.set_state!(p::HomotopyContinuationProblem, args...) - set_state!(p.u0, args...) -end -function SymbolicIndexingInterface.parameter_values(p::HomotopyContinuationProblem) - parameter_values(p.homotopy_continuation_system) -end -function SymbolicIndexingInterface.set_parameter!(p::HomotopyContinuationProblem, args...) - set_parameter!(parameter_values(p), args...) -end -function SymbolicIndexingInterface.observed(p::HomotopyContinuationProblem, sym) - if p.obsfn !== nothing - return p.obsfn(sym) - else - return SymbolicIndexingInterface.observed(p.sys, sym) - end -end From 2401aa22fc2ef382589efb57e75bede4a8ed14a3 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Sat, 30 Nov 2024 16:08:38 +0530 Subject: [PATCH 0448/2176] refactor: move HomotopyContinuation internals to MTK --- ext/MTKHomotopyContinuationExt.jl | 355 +------------- .../nonlinear/homotopy_continuation.jl | 437 ++++++++++++++++++ 2 files changed, 458 insertions(+), 334 deletions(-) diff --git a/ext/MTKHomotopyContinuationExt.jl b/ext/MTKHomotopyContinuationExt.jl index fa5d1bfcd4..586d608156 100644 --- a/ext/MTKHomotopyContinuationExt.jl +++ b/ext/MTKHomotopyContinuationExt.jl @@ -11,217 +11,6 @@ using ModelingToolkit: iscomplete, parameters, has_index_cache, get_index_cache, const MTK = ModelingToolkit -function contains_variable(x, wrt) - any(y -> occursin(y, x), wrt) -end - -""" -Possible reasons why a term is not polynomial -""" -MTK.EnumX.@enumx NonPolynomialReason begin - NonIntegerExponent - ExponentContainsUnknowns - BaseNotPolynomial - UnrecognizedOperation -end - -function display_reason(reason::NonPolynomialReason.T, sym) - if reason == NonPolynomialReason.NonIntegerExponent - pow = arguments(sym)[2] - "In $sym: Exponent $pow is not an integer" - elseif reason == NonPolynomialReason.ExponentContainsUnknowns - pow = arguments(sym)[2] - "In $sym: Exponent $pow contains unknowns of the system" - elseif reason == NonPolynomialReason.BaseNotPolynomial - base = arguments(sym)[1] - "In $sym: Base $base is not a polynomial in the unknowns" - elseif reason == NonPolynomialReason.UnrecognizedOperation - op = operation(sym) - """ - In $sym: Operation $op is not recognized. Allowed polynomial operations are \ - `*, /, +, -, ^`. - """ - else - error("This should never happen. Please open an issue in ModelingToolkit.jl.") - end -end - -mutable struct PolynomialData - non_polynomial_terms::Vector{BasicSymbolic} - reasons::Vector{NonPolynomialReason.T} - has_parametric_exponent::Bool -end - -PolynomialData() = PolynomialData(BasicSymbolic[], NonPolynomialReason.T[], false) - -abstract type PolynomialTransformationError <: Exception end - -struct MultivarTerm <: PolynomialTransformationError - term::Any - vars::Any -end - -function Base.showerror(io::IO, err::MultivarTerm) - println(io, - "Cannot convert system to polynomial: Found term $(err.term) which is a function of multiple unknowns $(err.vars).") -end - -struct MultipleTermsOfSameVar <: PolynomialTransformationError - terms::Any - var::Any -end - -function Base.showerror(io::IO, err::MultipleTermsOfSameVar) - println(io, - "Cannot convert system to polynomial: Found multiple non-polynomial terms $(err.terms) involving the same unknown $(err.var).") -end - -struct SymbolicSolveFailure <: PolynomialTransformationError - term::Any - var::Any -end - -function Base.showerror(io::IO, err::SymbolicSolveFailure) - println(io, - "Cannot convert system to polynomial: Unable to symbolically solve $(err.term) for $(err.var).") -end - -struct NemoNotLoaded <: PolynomialTransformationError end - -function Base.showerror(io::IO, err::NemoNotLoaded) - println(io, - "ModelingToolkit may be able to solve this system as a polynomial system if `Nemo` is loaded. Run `import Nemo` and try again.") -end - -struct VariablesAsPolyAndNonPoly <: PolynomialTransformationError - vars::Any -end - -function Base.showerror(io::IO, err::VariablesAsPolyAndNonPoly) - println(io, - "Cannot convert convert system to polynomial: Variables $(err.vars) occur in both polynomial and non-polynomial terms in the system.") -end - -struct NotPolynomialError <: Exception - transformation_err::Union{PolynomialTransformationError, Nothing} - eq::Vector{Equation} - data::Vector{PolynomialData} -end - -function Base.showerror(io::IO, err::NotPolynomialError) - if err.transformation_err !== nothing - Base.showerror(io, err.transformation_err) - end - for (eq, data) in zip(err.eq, err.data) - if isempty(data.non_polynomial_terms) - continue - end - println(io, - "Equation $(eq) is not a polynomial in the unknowns for the following reasons:") - for (term, reason) in zip(data.non_polynomial_terms, data.reasons) - println(io, display_reason(reason, term)) - end - end -end - -function is_polynomial!(data, y, wrt) - process_polynomial!(data, y, wrt) - isempty(data.reasons) -end - -""" -$(TYPEDSIGNATURES) - -Return information about the polynmial `x` with respect to variables in `wrt`, -writing said information to `data`. -""" -function process_polynomial!(data::PolynomialData, x, wrt) - x = unwrap(x) - symbolic_type(x) == NotSymbolic() && return true - iscall(x) || return true - contains_variable(x, wrt) || return true - any(isequal(x), wrt) && return true - - if operation(x) in (*, +, -, /) - # `map` because `all` will early exit, but we want to search - # through everything to get all the non-polynomial terms - return all(map(y -> is_polynomial!(data, y, wrt), arguments(x))) - end - if operation(x) == (^) - b, p = arguments(x) - is_pow_integer = symtype(p) <: Integer - if !is_pow_integer - push!(data.non_polynomial_terms, x) - push!(data.reasons, NonPolynomialReason.NonIntegerExponent) - end - if symbolic_type(p) != NotSymbolic() - data.has_parametric_exponent = true - end - - exponent_has_unknowns = contains_variable(p, wrt) - if exponent_has_unknowns - push!(data.non_polynomial_terms, x) - push!(data.reasons, NonPolynomialReason.ExponentContainsUnknowns) - end - base_polynomial = is_polynomial!(data, b, wrt) - return base_polynomial && !exponent_has_unknowns && is_pow_integer - end - push!(data.non_polynomial_terms, x) - push!(data.reasons, NonPolynomialReason.UnrecognizedOperation) - return false -end - -""" -$(TYPEDSIGNATURES) - -Given a `x`, a polynomial in variables in `wrt` which may contain rational functions, -express `x` as a single rational function with polynomial `num` and denominator `den`. -Return `(num, den)`. -""" -function handle_rational_polynomials(x, wrt) - x = unwrap(x) - symbolic_type(x) == NotSymbolic() && return x, 1 - iscall(x) || return x, 1 - contains_variable(x, wrt) || return x, 1 - any(isequal(x), wrt) && return x, 1 - - # simplify_fractions cancels out some common factors - # and expands (a / b)^c to a^c / b^c, so we only need - # to handle these cases - x = simplify_fractions(x) - op = operation(x) - args = arguments(x) - - if op == / - # numerator and denominator are trivial - num, den = args - # but also search for rational functions in numerator - n, d = handle_rational_polynomials(num, wrt) - num, den = n, den * d - elseif op == + - num = 0 - den = 1 - - # we don't need to do common denominator - # because we don't care about cases where denominator - # is zero. The expression is zero when all the numerators - # are zero. - for arg in args - n, d = handle_rational_polynomials(arg, wrt) - num += n - den *= d - end - else - return x, 1 - end - # if the denominator isn't a polynomial in `wrt`, better to not include it - # to reduce the size of the gcd polynomial - if !contains_variable(den, wrt) - return num / den, 1 - end - return num, den -end - """ $(TYPEDSIGNATURES) @@ -289,12 +78,6 @@ end SymbolicIndexingInterface.parameter_values(s::MTKHomotopySystem) = s.p -struct PolynomialTransformationData - new_var::BasicSymbolic - term::BasicSymbolic - inv_term::Vector -end - """ $(TYPEDSIGNATURES) @@ -312,128 +95,31 @@ Keyword arguments: All other keyword arguments are forwarded to `HomotopyContinuation.solver_startsystems`. """ function MTK.HomotopyContinuationProblem( - sys::NonlinearSystem, u0map, parammap = nothing; eval_expression = false, - eval_module = ModelingToolkit, warn_parametric_exponent = true, kwargs...) + sys::NonlinearSystem, u0map, parammap = nothing; kwargs...) if !iscomplete(sys) error("A completed `NonlinearSystem` is required. Call `complete` or `structural_simplify` on the system before creating a `HomotopyContinuationProblem`") end - - dvs = unknowns(sys) - # we need to consider `full_equations` because observed also should be - # polynomials (if used in equations) and we don't know if observed is used - # in denominator. - # This is not the most efficient, and would be improved significantly with - # CSE/hashconsing. - eqs = full_equations(sys) - - polydata = map(eqs) do eq - data = PolynomialData() - process_polynomial!(data, eq.lhs, dvs) - process_polynomial!(data, eq.rhs, dvs) - data - end - - has_parametric_exponents = any(d -> d.has_parametric_exponent, polydata) - - all_non_poly_terms = mapreduce(d -> d.non_polynomial_terms, vcat, polydata) - unique!(all_non_poly_terms) - - var_to_nonpoly = Dict{BasicSymbolic, PolynomialTransformationData}() - - is_poly = true - transformation_err = nothing - for t in all_non_poly_terms - # if the term involves multiple unknowns, we can't invert it - dvs_in_term = map(x -> occursin(x, t), dvs) - if count(dvs_in_term) > 1 - transformation_err = MultivarTerm(t, dvs[dvs_in_term]) - is_poly = false - break - end - # we already have a substitution solving for `var` - var = dvs[findfirst(dvs_in_term)] - if haskey(var_to_nonpoly, var) && !isequal(var_to_nonpoly[var].term, t) - transformation_err = MultipleTermsOfSameVar([t, var_to_nonpoly[var].term], var) - is_poly = false - break - end - # we want to solve `term - new_var` for `var` - new_var = gensym(Symbol(var)) - new_var = unwrap(only(@variables $new_var)) - invterm = Symbolics.ia_solve( - t - new_var, var; complex_roots = false, periodic_roots = false, warns = false) - # if we can't invert it, quit - if invterm === nothing || isempty(invterm) - transformation_err = SymbolicSolveFailure(t, var) - is_poly = false - break - end - # `ia_solve` returns lazy terms i.e. `asin(1.0)` instead of `pi/2` - # this just evaluates the constant expressions - invterm = Symbolics.substitute.(invterm, (Dict(),)) - # RootsOf implies Symbolics couldn't solve the inner polynomial because - # `Nemo` wasn't loaded. - if any(x -> MTK.iscall(x) && MTK.operation(x) == Symbolics.RootsOf, invterm) - transformation_err = NemoNotLoaded() - is_poly = false - break - end - var_to_nonpoly[var] = PolynomialTransformationData(new_var, t, invterm) + transformation = MTK.PolynomialTransformation(sys) + if transformation isa MTK.NotPolynomialError + throw(transformation) end - - if !is_poly - throw(NotPolynomialError(transformation_err, eqs, polydata)) - end - - subrules = Dict() - combinations = Vector[] - new_dvs = [] - for x in dvs - if haskey(var_to_nonpoly, x) - _data = var_to_nonpoly[x] - subrules[_data.term] = _data.new_var - push!(combinations, _data.inv_term) - push!(new_dvs, _data.new_var) - else - push!(combinations, [x]) - push!(new_dvs, x) - end - end - all_solutions = collect.(collect(Iterators.product(combinations...))) - - denoms = [] - eqs2 = map(eqs) do eq - t = eq.rhs - eq.lhs - t = Symbolics.fixpoint_sub(t, subrules; maxiters = length(dvs)) - # the substituted variable occurs outside the substituted term - poly_and_nonpoly = map(dvs) do x - haskey(var_to_nonpoly, x) && occursin(x, t) - end - if any(poly_and_nonpoly) - throw(NotPolynomialError( - VariablesAsPolyAndNonPoly(dvs[poly_and_nonpoly]), eqs, polydata)) - end - - num, den = handle_rational_polynomials(t, new_dvs) - # make factors different elements, otherwise the nonzero factors artificially - # inflate the error of the zero factor. - if iscall(den) && operation(den) == * - for arg in arguments(den) - # ignore constant factors - symbolic_type(arg) == NotSymbolic() && continue - push!(denoms, abs(arg)) - end - elseif symbolic_type(den) != NotSymbolic() - push!(denoms, abs(den)) - end - return 0 ~ num + result = MTK.transform_system(sys, transformation) + if result isa MTK.NotPolynomialError + throw(result) end + MTK.HomotopyContinuationProblem(sys, transformation, result, u0map, parammap; kwargs...) +end - sys2 = MTK.@set sys.eqs = eqs2 - MTK.@set! sys2.unknowns = new_dvs - # remove observed equations to avoid adding them in codegen - MTK.@set! sys2.observed = Equation[] - MTK.@set! sys2.substitutions = nothing +function MTK.HomotopyContinuationProblem( + sys::MTK.NonlinearSystem, transformation::MTK.PolynomialTransformation, + result::MTK.PolynomialTransformationResult, u0map, + parammap = nothing; eval_expression = false, + eval_module = ModelingToolkit, warn_parametric_exponent = true, kwargs...) + sys2 = result.sys + denoms = result.denominators + polydata = transformation.polydata + new_dvs = transformation.new_dvs + all_solutions = transformation.all_solutions _, u0, p = MTK.process_SciMLProblem( MTK.EmptySciMLFunction, sys, u0map, parammap; eval_expression, eval_module) @@ -443,10 +129,11 @@ function MTK.HomotopyContinuationProblem( unpack_solution = MTK.build_explicit_observed_function(sys2, all_solutions) hvars = symbolics_to_hc.(new_dvs) - mtkhsys = MTKHomotopySystem(nlfn.f, p, nlfn.jac, hvars, length(eqs)) + mtkhsys = MTKHomotopySystem(nlfn.f, p, nlfn.jac, hvars, length(new_dvs)) obsfn = MTK.ObservedFunctionCache(sys; eval_expression, eval_module) + has_parametric_exponents = any(d -> d.has_parametric_exponent, polydata) if has_parametric_exponents if warn_parametric_exponent @warn """ diff --git a/src/systems/nonlinear/homotopy_continuation.jl b/src/systems/nonlinear/homotopy_continuation.jl index e41a3f0ce7..044f44f70f 100644 --- a/src/systems/nonlinear/homotopy_continuation.jl +++ b/src/systems/nonlinear/homotopy_continuation.jl @@ -66,3 +66,440 @@ function SymbolicIndexingInterface.observed(p::HomotopyContinuationProblem, sym) return SymbolicIndexingInterface.observed(p.sys, sym) end end + +function contains_variable(x, wrt) + any(y -> occursin(y, x), wrt) +end + +""" +Possible reasons why a term is not polynomial +""" +EnumX.@enumx NonPolynomialReason begin + NonIntegerExponent + ExponentContainsUnknowns + BaseNotPolynomial + UnrecognizedOperation +end + +function display_reason(reason::NonPolynomialReason.T, sym) + if reason == NonPolynomialReason.NonIntegerExponent + pow = arguments(sym)[2] + "In $sym: Exponent $pow is not an integer" + elseif reason == NonPolynomialReason.ExponentContainsUnknowns + pow = arguments(sym)[2] + "In $sym: Exponent $pow contains unknowns of the system" + elseif reason == NonPolynomialReason.BaseNotPolynomial + base = arguments(sym)[1] + "In $sym: Base $base is not a polynomial in the unknowns" + elseif reason == NonPolynomialReason.UnrecognizedOperation + op = operation(sym) + """ + In $sym: Operation $op is not recognized. Allowed polynomial operations are \ + `*, /, +, -, ^`. + """ + else + error("This should never happen. Please open an issue in ModelingToolkit.jl.") + end +end + +""" + $(TYPEDEF) + +Information about an expression about its polynomial nature. +""" +mutable struct PolynomialData + """ + A list of all non-polynomial terms in the expression. + """ + non_polynomial_terms::Vector{BasicSymbolic} + """ + Corresponding to `non_polynomial_terms`, a list of reasons why they are + not polynomial. + """ + reasons::Vector{NonPolynomialReason.T} + """ + Whether the polynomial contains parametric exponents of unknowns. + """ + has_parametric_exponent::Bool +end + +PolynomialData() = PolynomialData(BasicSymbolic[], NonPolynomialReason.T[], false) + +abstract type PolynomialTransformationError <: Exception end + +struct MultivarTerm <: PolynomialTransformationError + term::Any + vars::Any +end + +function Base.showerror(io::IO, err::MultivarTerm) + println(io, + "Cannot convert system to polynomial: Found term $(err.term) which is a function of multiple unknowns $(err.vars).") +end + +struct MultipleTermsOfSameVar <: PolynomialTransformationError + terms::Any + var::Any +end + +function Base.showerror(io::IO, err::MultipleTermsOfSameVar) + println(io, + "Cannot convert system to polynomial: Found multiple non-polynomial terms $(err.terms) involving the same unknown $(err.var).") +end + +struct SymbolicSolveFailure <: PolynomialTransformationError + term::Any + var::Any +end + +function Base.showerror(io::IO, err::SymbolicSolveFailure) + println(io, + "Cannot convert system to polynomial: Unable to symbolically solve $(err.term) for $(err.var).") +end + +struct NemoNotLoaded <: PolynomialTransformationError end + +function Base.showerror(io::IO, err::NemoNotLoaded) + println(io, + "ModelingToolkit may be able to solve this system as a polynomial system if `Nemo` is loaded. Run `import Nemo` and try again.") +end + +struct VariablesAsPolyAndNonPoly <: PolynomialTransformationError + vars::Any +end + +function Base.showerror(io::IO, err::VariablesAsPolyAndNonPoly) + println(io, + "Cannot convert convert system to polynomial: Variables $(err.vars) occur in both polynomial and non-polynomial terms in the system.") +end + +struct NotPolynomialError <: Exception + transformation_err::Union{PolynomialTransformationError, Nothing} + eq::Vector{Equation} + data::Vector{PolynomialData} +end + +function Base.showerror(io::IO, err::NotPolynomialError) + if err.transformation_err !== nothing + Base.showerror(io, err.transformation_err) + end + for (eq, data) in zip(err.eq, err.data) + if isempty(data.non_polynomial_terms) + continue + end + println(io, + "Equation $(eq) is not a polynomial in the unknowns for the following reasons:") + for (term, reason) in zip(data.non_polynomial_terms, data.reasons) + println(io, display_reason(reason, term)) + end + end +end + +function is_polynomial!(data, y, wrt) + process_polynomial!(data, y, wrt) + isempty(data.reasons) +end + +""" +$(TYPEDSIGNATURES) + +Return information about the polynmial `x` with respect to variables in `wrt`, +writing said information to `data`. +""" +function process_polynomial!(data::PolynomialData, x, wrt) + x = unwrap(x) + symbolic_type(x) == NotSymbolic() && return true + iscall(x) || return true + contains_variable(x, wrt) || return true + any(isequal(x), wrt) && return true + + if operation(x) in (*, +, -, /) + # `map` because `all` will early exit, but we want to search + # through everything to get all the non-polynomial terms + return all(map(y -> is_polynomial!(data, y, wrt), arguments(x))) + end + if operation(x) == (^) + b, p = arguments(x) + is_pow_integer = symtype(p) <: Integer + if !is_pow_integer + push!(data.non_polynomial_terms, x) + push!(data.reasons, NonPolynomialReason.NonIntegerExponent) + end + if symbolic_type(p) != NotSymbolic() + data.has_parametric_exponent = true + end + + exponent_has_unknowns = contains_variable(p, wrt) + if exponent_has_unknowns + push!(data.non_polynomial_terms, x) + push!(data.reasons, NonPolynomialReason.ExponentContainsUnknowns) + end + base_polynomial = is_polynomial!(data, b, wrt) + return base_polynomial && !exponent_has_unknowns && is_pow_integer + end + push!(data.non_polynomial_terms, x) + push!(data.reasons, NonPolynomialReason.UnrecognizedOperation) + return false +end + +""" + $(TYPEDEF) + +Information about how an unknown in the system is substituted for a non-polynomial +expression to turn the system into a polynomial. Used in `PolynomialTransformation`. +""" +struct PolynomialTransformationData + """ + The new variable to use as an unknown of the transformed system. + """ + new_var::BasicSymbolic + """ + The non-polynomial expression being substituted. + """ + term::BasicSymbolic + """ + A vector of expressions corresponding to the solutions of + the non-polynomial expression `term` in terms of the new unknown `new_var`, + used to backsolve for the original unknown of the system. + """ + inv_term::Vector{BasicSymbolic} +end + +""" + $(TYPEDEF) + +Information representing how to transform a `NonlinearSystem` into a polynomial +system. +""" +struct PolynomialTransformation + """ + Substitutions mapping non-polynomial terms to temporary unknowns. The system + is a polynomial in the new unknowns. Currently, each non-polynomial term is a + function of a single unknown of the original system. + """ + substitution_rules::Dict{BasicSymbolic, BasicSymbolic} + """ + A vector of expressions involving unknowns of the transformed system, mapping + back to solutions of the original system. + """ + all_solutions::Vector{Vector{BasicSymbolic}} + """ + The new unknowns of the transformed system. + """ + new_dvs::Vector{BasicSymbolic} + """ + The polynomial data for each equation. + """ + polydata::Vector{PolynomialData} +end + +function PolynomialTransformation(sys::NonlinearSystem) + # we need to consider `full_equations` because observed also should be + # polynomials (if used in equations) and we don't know if observed is used + # in denominator. + # This is not the most efficient, and would be improved significantly with + # CSE/hashconsing. + eqs = full_equations(sys) + dvs = unknowns(sys) + + # Collect polynomial information about all equations + polydata = map(eqs) do eq + data = PolynomialData() + process_polynomial!(data, eq.lhs, dvs) + process_polynomial!(data, eq.rhs, dvs) + data + end + + # Get all unique non-polynomial terms + # NOTE: + # Is there a better way to check for uniqueness? `simplify` is relatively slow + # (maybe use the threaded version?) and `expand` can blow up expression size. + # Could metatheory help? + all_non_poly_terms = mapreduce(d -> d.non_polynomial_terms, vcat, polydata) + unique!(all_non_poly_terms) + + # each variable can only be replaced by one non-polynomial expression involving + # that variable. Keep track of this mapping. + var_to_nonpoly = Dict{BasicSymbolic, PolynomialTransformationData}() + + is_poly = true + transformation_err = nothing + for t in all_non_poly_terms + # if the term involves multiple unknowns, we can't invert it + dvs_in_term = map(x -> occursin(x, t), dvs) + if count(dvs_in_term) > 1 + transformation_err = MultivarTerm(t, dvs[dvs_in_term]) + is_poly = false + break + end + # we already have a substitution solving for `var` + var = dvs[findfirst(dvs_in_term)] + if haskey(var_to_nonpoly, var) && !isequal(var_to_nonpoly[var].term, t) + transformation_err = MultipleTermsOfSameVar([t, var_to_nonpoly[var].term], var) + is_poly = false + break + end + # we want to solve `term - new_var` for `var` + new_var = gensym(Symbol(var)) + new_var = unwrap(only(@variables $new_var)) + invterm = Symbolics.ia_solve( + t - new_var, var; complex_roots = false, periodic_roots = false, warns = false) + # if we can't invert it, quit + if invterm === nothing || isempty(invterm) + transformation_err = SymbolicSolveFailure(t, var) + is_poly = false + break + end + # `ia_solve` returns lazy terms i.e. `asin(1.0)` instead of `pi/2` + # this just evaluates the constant expressions + invterm = Symbolics.substitute.(invterm, (Dict(),)) + # RootsOf implies Symbolics couldn't solve the inner polynomial because + # `Nemo` wasn't loaded. + if any(x -> iscall(x) && operation(x) == Symbolics.RootsOf, invterm) + transformation_err = NemoNotLoaded() + is_poly = false + break + end + var_to_nonpoly[var] = PolynomialTransformationData(new_var, t, invterm) + end + + # return the error instead of throwing it, so the user can choose what to do + # without having to catch the exception + if !is_poly + return NotPolynomialError(transformation_err, eqs, polydata) + end + + subrules = Dict{BasicSymbolic, BasicSymbolic}() + # corresponding to each unknown in `dvs`, the list of its possible solutions + # in terms of the new unknown. + combinations = Vector{BasicSymbolic}[] + new_dvs = BasicSymbolic[] + for x in dvs + if haskey(var_to_nonpoly, x) + _data = var_to_nonpoly[x] + # map term to new unknown + subrules[_data.term] = _data.new_var + push!(combinations, _data.inv_term) + push!(new_dvs, _data.new_var) + else + push!(combinations, BasicSymbolic[x]) + push!(new_dvs, x) + end + end + all_solutions = vec(collect.(collect(Iterators.product(combinations...)))) + return PolynomialTransformation(subrules, all_solutions, new_dvs, polydata) +end + +""" + $(TYPEDEF) + +A struct containing the result of transforming a system into a polynomial system +using the appropriate `PolynomialTransformation`. Also contains the denominators +in the equations, to rule out invalid roots. +""" +struct PolynomialTransformationResult + sys::NonlinearSystem + denominators::Vector{BasicSymbolic} +end + +""" + $(TYPEDSIGNATURES) + +Transform the system `sys` with `transformation` and return a +`PolynomialTransformationResult`, or a `NotPolynomialError` if the system cannot +be transformed. +""" +function transform_system(sys::NonlinearSystem, transformation::PolynomialTransformation) + subrules = transformation.substitution_rules + dvs = unknowns(sys) + eqs = full_equations(sys) + polydata = transformation.polydata + new_dvs = transformation.new_dvs + all_solutions = transformation.all_solutions + + eqs2 = Equation[] + denoms = BasicSymbolic[] + for eq in eqs + t = eq.rhs - eq.lhs + t = Symbolics.fixpoint_sub(t, subrules; maxiters = length(dvs)) + # the substituted variable occurs outside the substituted term + poly_and_nonpoly = map(dvs) do x + all(!isequal(x), new_dvs) && occursin(x, t) + end + if any(poly_and_nonpoly) + return NotPolynomialError( + VariablesAsPolyAndNonPoly(dvs[poly_and_nonpoly]), eqs, polydata) + end + num, den = handle_rational_polynomials(t, new_dvs) + # make factors different elements, otherwise the nonzero factors artificially + # inflate the error of the zero factor. + if iscall(den) && operation(den) == * + for arg in arguments(den) + # ignore constant factors + symbolic_type(arg) == NotSymbolic() && continue + push!(denoms, abs(arg)) + end + elseif symbolic_type(den) != NotSymbolic() + push!(denoms, abs(den)) + end + push!(eqs2, 0 ~ num) + end + + sys2 = @set sys.eqs = eqs2 + @set! sys2.unknowns = new_dvs + # remove observed equations to avoid adding them in codegen + @set! sys2.observed = Equation[] + @set! sys2.substitutions = nothing + return PolynomialTransformationResult(sys2, denoms) +end + +""" +$(TYPEDSIGNATURES) + +Given a `x`, a polynomial in variables in `wrt` which may contain rational functions, +express `x` as a single rational function with polynomial `num` and denominator `den`. +Return `(num, den)`. +""" +function handle_rational_polynomials(x, wrt) + x = unwrap(x) + symbolic_type(x) == NotSymbolic() && return x, 1 + iscall(x) || return x, 1 + contains_variable(x, wrt) || return x, 1 + any(isequal(x), wrt) && return x, 1 + + # simplify_fractions cancels out some common factors + # and expands (a / b)^c to a^c / b^c, so we only need + # to handle these cases + x = simplify_fractions(x) + op = operation(x) + args = arguments(x) + + if op == / + # numerator and denominator are trivial + num, den = args + # but also search for rational functions in numerator + n, d = handle_rational_polynomials(num, wrt) + num, den = n, den * d + elseif op == + + num = 0 + den = 1 + + # we don't need to do common denominator + # because we don't care about cases where denominator + # is zero. The expression is zero when all the numerators + # are zero. + for arg in args + n, d = handle_rational_polynomials(arg, wrt) + num += n + den *= d + end + else + return x, 1 + end + # if the denominator isn't a polynomial in `wrt`, better to not include it + # to reduce the size of the gcd polynomial + if !contains_variable(den, wrt) + return num / den, 1 + end + return num, den +end From 669bc84e41897d8c3a0a270adf941829be1cdd10 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Sat, 30 Nov 2024 19:53:48 +0530 Subject: [PATCH 0449/2176] feat: add `safe_HomotopyContinuationProblem` --- ext/MTKHomotopyContinuationExt.jl | 10 +++++++-- .../nonlinear/homotopy_continuation.jl | 21 +++++++++++++++++++ test/extensions/homotopy_continuation.jl | 20 ++++++++++++++++++ 3 files changed, 49 insertions(+), 2 deletions(-) diff --git a/ext/MTKHomotopyContinuationExt.jl b/ext/MTKHomotopyContinuationExt.jl index 586d608156..c4a090d9a8 100644 --- a/ext/MTKHomotopyContinuationExt.jl +++ b/ext/MTKHomotopyContinuationExt.jl @@ -96,16 +96,22 @@ All other keyword arguments are forwarded to `HomotopyContinuation.solver_starts """ function MTK.HomotopyContinuationProblem( sys::NonlinearSystem, u0map, parammap = nothing; kwargs...) + prob = MTK._safe_HomotopyContinuationProblem(sys, u0map, parammap; kwargs...) + prob isa MTK.HomotopyContinuationProblem || throw(prob) + return prob +end + +function MTK._safe_HomotopyContinuationProblem(sys, u0map, parammap = nothing; kwargs...) if !iscomplete(sys) error("A completed `NonlinearSystem` is required. Call `complete` or `structural_simplify` on the system before creating a `HomotopyContinuationProblem`") end transformation = MTK.PolynomialTransformation(sys) if transformation isa MTK.NotPolynomialError - throw(transformation) + return transformation end result = MTK.transform_system(sys, transformation) if result isa MTK.NotPolynomialError - throw(result) + return result end MTK.HomotopyContinuationProblem(sys, transformation, result, u0map, parammap; kwargs...) end diff --git a/src/systems/nonlinear/homotopy_continuation.jl b/src/systems/nonlinear/homotopy_continuation.jl index 044f44f70f..ed69db1aee 100644 --- a/src/systems/nonlinear/homotopy_continuation.jl +++ b/src/systems/nonlinear/homotopy_continuation.jl @@ -48,6 +48,27 @@ function HomotopyContinuationProblem(::AbstractSystem, _u0, _p; kwargs...) error("HomotopyContinuation.jl is required to create and solve `HomotopyContinuationProblem`s. Please run `Pkg.add(\"HomotopyContinuation\")` to continue.") end +""" + $(TYPEDSIGNATURES) + +Utility function for `safe_HomotopyContinuationProblem`, implemented in the extension. +""" +function _safe_HomotopyContinuationProblem end + +""" + $(TYPEDSIGNATURES) + +Return a `HomotopyContinuationProblem` if the extension is loaded and the system is +polynomial. If the extension is not loaded, return `nothing`. If the system is not +polynomial, return the appropriate `NotPolynomialError`. +""" +function safe_HomotopyContinuationProblem(sys::NonlinearSystem, args...; kwargs...) + if Base.get_extension(ModelingToolkit, :MTKHomotopyContinuationExt) === nothing + return nothing + end + return _safe_HomotopyContinuationProblem(sys, args...; kwargs...) +end + SymbolicIndexingInterface.symbolic_container(p::HomotopyContinuationProblem) = p.sys SymbolicIndexingInterface.state_values(p::HomotopyContinuationProblem) = p.u0 function SymbolicIndexingInterface.set_state!(p::HomotopyContinuationProblem, args...) diff --git a/test/extensions/homotopy_continuation.jl b/test/extensions/homotopy_continuation.jl index 81c252c84a..3bdfa06a5d 100644 --- a/test/extensions/homotopy_continuation.jl +++ b/test/extensions/homotopy_continuation.jl @@ -1,6 +1,19 @@ using ModelingToolkit, NonlinearSolve, SymbolicIndexingInterface +import ModelingToolkit as MTK using LinearAlgebra using Test + +@testset "Safe HCProblem" begin + @variables x y z + eqs = [0 ~ x^2 + y^2 + 2x * y + 0 ~ x^2 + 4x + 4 + 0 ~ y * z + 4x^2] + @mtkbuild sys = NonlinearSystem(eqs) + prob = MTK.safe_HomotopyContinuationProblem(sys, [x => 1.0, y => 1.0, z => 1.0], []) + @test prob === nothing +end + + import HomotopyContinuation @testset "No parameters" begin @@ -78,30 +91,37 @@ end @test_throws ["Cannot convert", "Unable", "symbolically solve", "Exponent", "not an integer", "not a polynomial"] HomotopyContinuationProblem( sys, []) + @test MTK.safe_HomotopyContinuationProblem(sys, []) isa MTK.NotPolynomialError @mtkbuild sys = NonlinearSystem([x^x - x ~ 0]) @test_throws ["Cannot convert", "Unable", "symbolically solve", "Exponent", "unknowns", "not a polynomial"] HomotopyContinuationProblem( sys, []) + @test MTK.safe_HomotopyContinuationProblem(sys, []) isa MTK.NotPolynomialError @mtkbuild sys = NonlinearSystem([((x^2) / sin(x))^2 + x ~ 0]) @test_throws ["Cannot convert", "both polynomial", "non-polynomial", "recognized", "sin", "not a polynomial"] HomotopyContinuationProblem( sys, []) + @test MTK.safe_HomotopyContinuationProblem(sys, []) isa MTK.NotPolynomialError @variables y = 2.0 @mtkbuild sys = NonlinearSystem([x^2 + y^2 + 2 ~ 0, y ~ sin(x)]) @test_throws ["Cannot convert", "recognized", "sin", "not a polynomial"] HomotopyContinuationProblem( sys, []) + @test MTK.safe_HomotopyContinuationProblem(sys, []) isa MTK.NotPolynomialError @mtkbuild sys = NonlinearSystem([x^2 + y^2 - 2 ~ 0, sin(x + y) ~ 0]) @test_throws ["Cannot convert", "function of multiple unknowns"] HomotopyContinuationProblem( sys, []) + @test MTK.safe_HomotopyContinuationProblem(sys, []) isa MTK.NotPolynomialError @mtkbuild sys = NonlinearSystem([sin(x)^2 + 1 ~ 0, cos(y) - cos(x) - 1 ~ 0]) @test_throws ["Cannot convert", "multiple non-polynomial terms", "same unknown"] HomotopyContinuationProblem( sys, []) + @test MTK.safe_HomotopyContinuationProblem(sys, []) isa MTK.NotPolynomialError @mtkbuild sys = NonlinearSystem([sin(x^2)^2 + sin(x^2) - 1 ~ 0]) @test_throws ["import Nemo"] HomotopyContinuationProblem(sys, []) + @test MTK.safe_HomotopyContinuationProblem(sys, []) isa MTK.NotPolynomialError end import Nemo From 1ef12170e0206694ac627639b28589871e2128a4 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Sat, 30 Nov 2024 19:58:00 +0530 Subject: [PATCH 0450/2176] feat: use `HomotopyContinuationProblem` in `NonlinearProblem` if possible --- src/systems/nonlinear/nonlinearsystem.jl | 6 +++++- test/extensions/homotopy_continuation.jl | 18 ++++++++++++++++-- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/src/systems/nonlinear/nonlinearsystem.jl b/src/systems/nonlinear/nonlinearsystem.jl index ff4a7d0eb2..3cb68853aa 100644 --- a/src/systems/nonlinear/nonlinearsystem.jl +++ b/src/systems/nonlinear/nonlinearsystem.jl @@ -496,10 +496,14 @@ end function DiffEqBase.NonlinearProblem{iip}(sys::NonlinearSystem, u0map, parammap = DiffEqBase.NullParameters(); - check_length = true, kwargs...) where {iip} + check_length = true, use_homotopy_continuation = true, kwargs...) where {iip} if !iscomplete(sys) error("A completed `NonlinearSystem` is required. Call `complete` or `structural_simplify` on the system before creating a `NonlinearProblem`") end + prob = safe_HomotopyContinuationProblem(sys, u0map, parammap; check_length, kwargs...) + if prob isa HomotopyContinuationProblem + return prob + end f, u0, p = process_SciMLProblem(NonlinearFunction{iip}, sys, u0map, parammap; check_length, kwargs...) pt = something(get_metadata(sys), StandardNonlinearProblem()) diff --git a/test/extensions/homotopy_continuation.jl b/test/extensions/homotopy_continuation.jl index 3bdfa06a5d..e57cd0b2f7 100644 --- a/test/extensions/homotopy_continuation.jl +++ b/test/extensions/homotopy_continuation.jl @@ -13,7 +13,6 @@ using Test @test prob === nothing end - import HomotopyContinuation @testset "No parameters" begin @@ -22,12 +21,19 @@ import HomotopyContinuation 0 ~ x^2 + 4x + 4 0 ~ y * z + 4x^2] @mtkbuild sys = NonlinearSystem(eqs) - prob = HomotopyContinuationProblem(sys, [x => 1.0, y => 1.0, z => 1.0], []) + u0 = [x => 1.0, y => 1.0, z => 1.0] + prob = HomotopyContinuationProblem(sys, u0) @test prob[x] == prob[y] == prob[z] == 1.0 @test prob[x + y] == 2.0 sol = solve(prob; threading = false) @test SciMLBase.successful_retcode(sol) @test norm(sol.resid)≈0.0 atol=1e-10 + + prob2 = NonlinearProblem(sys, u0) + @test prob2 isa HomotopyContinuationProblem + sol = solve(prob2; threading = false) + @test SciMLBase.successful_retcode(sol) + @test norm(sol.resid)≈0.0 atol=1e-10 end struct Wrapper @@ -92,36 +98,44 @@ end "Exponent", "not an integer", "not a polynomial"] HomotopyContinuationProblem( sys, []) @test MTK.safe_HomotopyContinuationProblem(sys, []) isa MTK.NotPolynomialError + @test NonlinearProblem(sys, []) isa NonlinearProblem + @mtkbuild sys = NonlinearSystem([x^x - x ~ 0]) @test_throws ["Cannot convert", "Unable", "symbolically solve", "Exponent", "unknowns", "not a polynomial"] HomotopyContinuationProblem( sys, []) @test MTK.safe_HomotopyContinuationProblem(sys, []) isa MTK.NotPolynomialError + @test NonlinearProblem(sys, []) isa NonlinearProblem @mtkbuild sys = NonlinearSystem([((x^2) / sin(x))^2 + x ~ 0]) @test_throws ["Cannot convert", "both polynomial", "non-polynomial", "recognized", "sin", "not a polynomial"] HomotopyContinuationProblem( sys, []) @test MTK.safe_HomotopyContinuationProblem(sys, []) isa MTK.NotPolynomialError + @test NonlinearProblem(sys, []) isa NonlinearProblem @variables y = 2.0 @mtkbuild sys = NonlinearSystem([x^2 + y^2 + 2 ~ 0, y ~ sin(x)]) @test_throws ["Cannot convert", "recognized", "sin", "not a polynomial"] HomotopyContinuationProblem( sys, []) @test MTK.safe_HomotopyContinuationProblem(sys, []) isa MTK.NotPolynomialError + @test NonlinearProblem(sys, []) isa NonlinearProblem @mtkbuild sys = NonlinearSystem([x^2 + y^2 - 2 ~ 0, sin(x + y) ~ 0]) @test_throws ["Cannot convert", "function of multiple unknowns"] HomotopyContinuationProblem( sys, []) @test MTK.safe_HomotopyContinuationProblem(sys, []) isa MTK.NotPolynomialError + @test NonlinearProblem(sys, []) isa NonlinearProblem @mtkbuild sys = NonlinearSystem([sin(x)^2 + 1 ~ 0, cos(y) - cos(x) - 1 ~ 0]) @test_throws ["Cannot convert", "multiple non-polynomial terms", "same unknown"] HomotopyContinuationProblem( sys, []) @test MTK.safe_HomotopyContinuationProblem(sys, []) isa MTK.NotPolynomialError + @test NonlinearProblem(sys, []) isa NonlinearProblem @mtkbuild sys = NonlinearSystem([sin(x^2)^2 + sin(x^2) - 1 ~ 0]) @test_throws ["import Nemo"] HomotopyContinuationProblem(sys, []) @test MTK.safe_HomotopyContinuationProblem(sys, []) isa MTK.NotPolynomialError + @test NonlinearProblem(sys, []) isa NonlinearProblem end import Nemo From aa0e08ad700d5c9f52c0a6fd258ee11c3a5add3e Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Sat, 30 Nov 2024 23:16:17 +0530 Subject: [PATCH 0451/2176] docs: add docstrings to `NonPolynomialReason` enum variants --- src/systems/nonlinear/homotopy_continuation.jl | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/systems/nonlinear/homotopy_continuation.jl b/src/systems/nonlinear/homotopy_continuation.jl index ed69db1aee..03aeed1edf 100644 --- a/src/systems/nonlinear/homotopy_continuation.jl +++ b/src/systems/nonlinear/homotopy_continuation.jl @@ -96,9 +96,21 @@ end Possible reasons why a term is not polynomial """ EnumX.@enumx NonPolynomialReason begin + """ + Exponent of an expression involving unknowns is not an integer. + """ NonIntegerExponent + """ + Exponent is an expression containing unknowns. + """ ExponentContainsUnknowns + """ + The base of an exponent is not a polynomial in the unknowns. + """ BaseNotPolynomial + """ + An expression involves a non-polynomial operation involving unknowns. + """ UnrecognizedOperation end From b9b396ddb80537b71d9c06dca20df22e44eb6bc3 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Sun, 8 Dec 2024 10:56:17 +0530 Subject: [PATCH 0452/2176] test: add logs to debug tests --- test/extensions/homotopy_continuation.jl | 3 +++ 1 file changed, 3 insertions(+) diff --git a/test/extensions/homotopy_continuation.jl b/test/extensions/homotopy_continuation.jl index e57cd0b2f7..f9724692e7 100644 --- a/test/extensions/homotopy_continuation.jl +++ b/test/extensions/homotopy_continuation.jl @@ -160,6 +160,9 @@ end @test prob[x] ≈ 0.25 @test prob[y] ≈ 0.125 sol = solve(prob; threading = false) + # can't replicate the solve failure locally, so CI logs might help + @show sol.u HomotopyContinuation.real_solutions(sol.original) + @test SciMLBase.successful_retcode(sol) @test sol[a]≈0.5 atol=1e-6 @test sol[b]≈0.25 atol=1e-6 end From f428df4bc0dbc298a0f15280bb980679d7f895ef Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 9 Dec 2024 12:41:19 +0530 Subject: [PATCH 0453/2176] fixup! test: add logs to debug tests --- test/extensions/homotopy_continuation.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/extensions/homotopy_continuation.jl b/test/extensions/homotopy_continuation.jl index f9724692e7..554f9e1e1d 100644 --- a/test/extensions/homotopy_continuation.jl +++ b/test/extensions/homotopy_continuation.jl @@ -161,7 +161,7 @@ end @test prob[y] ≈ 0.125 sol = solve(prob; threading = false) # can't replicate the solve failure locally, so CI logs might help - @show sol.u HomotopyContinuation.real_solutions(sol.original) + @show sol.u sol.original.path_results @test SciMLBase.successful_retcode(sol) @test sol[a]≈0.5 atol=1e-6 @test sol[b]≈0.25 atol=1e-6 From 16d3f5c7a57bffd698a6c69da3ea89e0089b2d54 Mon Sep 17 00:00:00 2001 From: Benjamin Chung Date: Mon, 9 Dec 2024 02:52:15 -0800 Subject: [PATCH 0454/2176] Refactor ImperativeAffect into its own file --- src/ModelingToolkit.jl | 1 + src/systems/callbacks.jl | 221 +------------------------------ src/systems/diffeqs/odesystem.jl | 2 - src/systems/imperative_affect.jl | 220 ++++++++++++++++++++++++++++++ 4 files changed, 224 insertions(+), 220 deletions(-) create mode 100644 src/systems/imperative_affect.jl diff --git a/src/ModelingToolkit.jl b/src/ModelingToolkit.jl index de8e69c41f..ccdaa15e99 100644 --- a/src/ModelingToolkit.jl +++ b/src/ModelingToolkit.jl @@ -145,6 +145,7 @@ include("systems/parameter_buffer.jl") include("systems/abstractsystem.jl") include("systems/model_parsing.jl") include("systems/connectors.jl") +include("systems/imperative_affect.jl") include("systems/callbacks.jl") include("systems/problem_utils.jl") diff --git a/src/systems/callbacks.jl b/src/systems/callbacks.jl index 0cb7f16c9f..4533534029 100644 --- a/src/systems/callbacks.jl +++ b/src/systems/callbacks.jl @@ -73,111 +73,6 @@ function namespace_affect(affect::FunctionalAffect, s) context(affect)) end -""" - ImperativeAffect(f::Function; modified::NamedTuple, observed::NamedTuple, ctx) - -`ImperativeAffect` is a helper for writing affect functions that will compute observed values and -ensure that modified values are correctly written back into the system. The affect function `f` needs to have -the signature - -``` - f(modified::NamedTuple, observed::NamedTuple, ctx, integrator)::NamedTuple -``` - -The function `f` will be called with `observed` and `modified` `NamedTuple`s that are derived from their respective `NamedTuple` definitions. -Each declaration`NamedTuple` should map an expression to a symbol; for example if we pass `observed=(; x = a + b)` this will alias the result of executing `a+b` in the system as `x` -so the value of `a + b` will be accessible as `observed.x` in `f`. `modified` currently restricts symbolic expressions to only bare variables, so only tuples of the form -`(; x = y)` or `(; x)` (which aliases `x` as itself) are allowed. - -The argument NamedTuples (for instance `(;x=y)`) will be populated with the declared values on function entry; if we require `(;x=y)` in `observed` and `y=2`, for example, -then the NamedTuple `(;x=2)` will be passed as `observed` to the affect function `f`. - -The NamedTuple returned from `f` includes the values to be written back to the system after `f` returns. For example, if we want to update the value of `x` to be the result of `x + y` we could write - - ImperativeAffect(observed=(; x_plus_y = x + y), modified=(; x)) do m, o - @set! m.x = o.x_plus_y - end - -Where we use Setfield to copy the tuple `m` with a new value for `x`, then return the modified value of `m`. All values updated by the tuple must have names originally declared in -`modified`; a runtime error will be produced if a value is written that does not appear in `modified`. The user can dynamically decide not to write a value back by not including it -in the returned tuple, in which case the associated field will not be updated. -""" -@kwdef struct ImperativeAffect - f::Any - obs::Vector - obs_syms::Vector{Symbol} - modified::Vector - mod_syms::Vector{Symbol} - ctx::Any - skip_checks::Bool -end - -function ImperativeAffect(f::Function; - observed::NamedTuple = NamedTuple{()}(()), - modified::NamedTuple = NamedTuple{()}(()), - ctx = nothing, - skip_checks = false) - ImperativeAffect(f, - collect(values(observed)), collect(keys(observed)), - collect(values(modified)), collect(keys(modified)), - ctx, skip_checks) -end -function ImperativeAffect(f::Function, modified::NamedTuple; - observed::NamedTuple = NamedTuple{()}(()), ctx = nothing, skip_checks = false) - ImperativeAffect( - f, observed = observed, modified = modified, ctx = ctx, skip_checks = skip_checks) -end -function ImperativeAffect( - f::Function, modified::NamedTuple, observed::NamedTuple; ctx = nothing, skip_checks = false) - ImperativeAffect( - f, observed = observed, modified = modified, ctx = ctx, skip_checks = skip_checks) -end -function ImperativeAffect( - f::Function, modified::NamedTuple, observed::NamedTuple, ctx; skip_checks = false) - ImperativeAffect( - f, observed = observed, modified = modified, ctx = ctx, skip_checks = skip_checks) -end - -function Base.show(io::IO, mfa::ImperativeAffect) - obs_vals = join(map((ob, nm) -> "$ob => $nm", mfa.obs, mfa.obs_syms), ", ") - mod_vals = join(map((md, nm) -> "$md => $nm", mfa.modified, mfa.mod_syms), ", ") - affect = mfa.f - print(io, - "ImperativeAffect(observed: [$obs_vals], modified: [$mod_vals], affect:$affect)") -end -func(f::ImperativeAffect) = f.f -context(a::ImperativeAffect) = a.ctx -observed(a::ImperativeAffect) = a.obs -observed_syms(a::ImperativeAffect) = a.obs_syms -discretes(a::ImperativeAffect) = filter(ModelingToolkit.isparameter, a.modified) -modified(a::ImperativeAffect) = a.modified -modified_syms(a::ImperativeAffect) = a.mod_syms - -function Base.:(==)(a1::ImperativeAffect, a2::ImperativeAffect) - isequal(a1.f, a2.f) && isequal(a1.obs, a2.obs) && isequal(a1.modified, a2.modified) && - isequal(a1.obs_syms, a2.obs_syms) && isequal(a1.mod_syms, a2.mod_syms) && - isequal(a1.ctx, a2.ctx) -end - -function Base.hash(a::ImperativeAffect, s::UInt) - s = hash(a.f, s) - s = hash(a.obs, s) - s = hash(a.obs_syms, s) - s = hash(a.modified, s) - s = hash(a.mod_syms, s) - hash(a.ctx, s) -end - -function namespace_affect(affect::ImperativeAffect, s) - ImperativeAffect(func(affect), - namespace_expr.(observed(affect), (s,)), - observed_syms(affect), - renamespace.((s,), modified(affect)), - modified_syms(affect), - context(affect), - affect.skip_checks) -end - function has_functional_affect(cb) (affects(cb) isa FunctionalAffect || affects(cb) isa ImperativeAffect) end @@ -203,13 +98,13 @@ sharp discontinuity between integrator steps (which in this example would not no guaranteed to be triggered. Once detected the integrator will "wind back" through a root-finding process to identify the point when the condition became active; the method used -is specified by `rootfind` from [`SciMLBase.RootfindOpt`](@ref). If we denote the time when the condition becomes active at tc, +is specified by `rootfind` from [`SciMLBase.RootfindOpt`](@ref). If we denote the time when the condition becomes active as `tc``, the value in the integrator after windback will be: * `u[tc-epsilon], p[tc-epsilon], tc` if `LeftRootFind` is used, * `u[tc+epsilon], p[tc+epsilon], tc` if `RightRootFind` is used, * or `u[t], p[t], t` if `NoRootFind` is used. For example, if we want to detect when an unknown variable `x` satisfies `x > 0` using the condition `x ~ 0` on a positive edge (that is, `D(x) > 0`), -then left root finding will get us `x=-epsilon`, right root finding `x=epsilon` and no root finding whatever the next step of the integrator was after +then left root finding will get us `x=-epsilon`, right root finding `x=epsilon` and no root finding will produce whatever the next step of the integrator was after it passed through 0. Multiple callbacks in the same system with different `rootfind` operations will be grouped @@ -405,7 +300,6 @@ end namespace_affects(af::Vector, s) = Equation[namespace_affect(a, s) for a in af] namespace_affects(af::FunctionalAffect, s) = namespace_affect(af, s) -namespace_affects(af::ImperativeAffect, s) = namespace_affect(af, s) namespace_affects(::Nothing, s) = nothing function namespace_callback(cb::SymbolicContinuousCallback, s)::SymbolicContinuousCallback @@ -480,7 +374,6 @@ scalarize_affects(affects) = scalarize(affects) scalarize_affects(affects::Tuple) = FunctionalAffect(affects...) scalarize_affects(affects::NamedTuple) = FunctionalAffect(; affects...) scalarize_affects(affects::FunctionalAffect) = affects -scalarize_affects(affects::ImperativeAffect) = affects SymbolicDiscreteCallback(p::Pair) = SymbolicDiscreteCallback(p[1], p[2]) SymbolicDiscreteCallback(cb::SymbolicDiscreteCallback) = cb # passthrough @@ -1099,117 +992,9 @@ function check_assignable(sys, sym) end end -function compile_user_affect(affect::ImperativeAffect, cb, sys, dvs, ps; kwargs...) - #= - Implementation sketch: - generate observed function (oop), should save to a component array under obs_syms - do the same stuff as the normal FA for pars_syms - call the affect method - unpack and apply the resulting values - =# - function check_dups(syms, exprs) # = (syms_dedup, exprs_dedup) - seen = Set{Symbol}() - syms_dedup = [] - exprs_dedup = [] - for (sym, exp) in Iterators.zip(syms, exprs) - if !in(sym, seen) - push!(syms_dedup, sym) - push!(exprs_dedup, exp) - push!(seen, sym) - elseif !affect.skip_checks - @warn "Expression $(expr) is aliased as $sym, which has already been used. The first definition will be used." - end - end - return (syms_dedup, exprs_dedup) - end - - obs_exprs = observed(affect) - if !affect.skip_checks - for oexpr in obs_exprs - invalid_vars = invalid_variables(sys, oexpr) - if length(invalid_vars) > 0 - error("Observed equation $(oexpr) in affect refers to missing variable(s) $(invalid_vars); the variables may not have been added (e.g. if a component is missing).") - end - end - end - obs_syms = observed_syms(affect) - obs_syms, obs_exprs = check_dups(obs_syms, obs_exprs) - - mod_exprs = modified(affect) - if !affect.skip_checks - for mexpr in mod_exprs - if !check_assignable(sys, mexpr) - @warn ("Expression $mexpr cannot be assigned to; currently only unknowns and parameters may be updated by an affect.") - end - invalid_vars = unassignable_variables(sys, mexpr) - if length(invalid_vars) > 0 - error("Modified equation $(mexpr) in affect refers to missing variable(s) $(invalid_vars); the variables may not have been added (e.g. if a component is missing) or they may have been reduced away.") - end - end - end - mod_syms = modified_syms(affect) - mod_syms, mod_exprs = check_dups(mod_syms, mod_exprs) - - overlapping_syms = intersect(mod_syms, obs_syms) - if length(overlapping_syms) > 0 && !affect.skip_checks - @warn "The symbols $overlapping_syms are declared as both observed and modified; this is a code smell because it becomes easy to confuse them and assign/not assign a value." - end - - # sanity checks done! now build the data and update function for observed values - mkzero(sz) = - if sz === () - 0.0 - else - zeros(sz) - end - obs_fun = build_explicit_observed_function( - sys, Symbolics.scalarize.(obs_exprs); - array_type = Tuple) - obs_sym_tuple = (obs_syms...,) - - # okay so now to generate the stuff to assign it back into the system - mod_pairs = mod_exprs .=> mod_syms - mod_names = (mod_syms...,) - mod_og_val_fun = build_explicit_observed_function( - sys, Symbolics.scalarize.(first.(mod_pairs)); - array_type = Tuple) - - upd_funs = NamedTuple{mod_names}((setu.((sys,), first.(mod_pairs))...,)) - - if has_index_cache(sys) && (ic = get_index_cache(sys)) !== nothing - save_idxs = get(ic.callback_to_clocks, cb, Int[]) - else - save_idxs = Int[] - end - - let user_affect = func(affect), ctx = context(affect) - function (integ) - # update the to-be-mutated values; this ensures that if you do a no-op then nothing happens - modvals = mod_og_val_fun(integ.u, integ.p, integ.t) - upd_component_array = NamedTuple{mod_names}(modvals) - - # update the observed values - obs_component_array = NamedTuple{obs_sym_tuple}(obs_fun( - integ.u, integ.p, integ.t)) - - # let the user do their thing - upd_vals = user_affect(upd_component_array, obs_component_array, ctx, integ) - - # write the new values back to the integrator - _generated_writeback(integ, upd_funs, upd_vals) - - for idx in save_idxs - SciMLBase.save_discretes!(integ, idx) - end - end - end -end - -function compile_affect( - affect::Union{FunctionalAffect, ImperativeAffect}, cb, sys, dvs, ps; kwargs...) +function compile_affect(affect::FunctionalAffect, cb, sys, dvs, ps; kwargs...) compile_user_affect(affect, cb, sys, dvs, ps; kwargs...) end - function _compile_optional_affect(default, aff, cb, sys, dvs, ps; kwargs...) if isnothing(aff) || aff == default return nothing diff --git a/src/systems/diffeqs/odesystem.jl b/src/systems/diffeqs/odesystem.jl index 4040b7a646..2b0bd8c8d7 100644 --- a/src/systems/diffeqs/odesystem.jl +++ b/src/systems/diffeqs/odesystem.jl @@ -629,8 +629,6 @@ function build_explicit_observed_function(sys, ts; oop_mtkp_wrapper = mtkparams_wrapper end - output_expr = isscalar ? ts[1] : - (array_type <: Vector ? MakeArray(ts, output_type) : MakeTuple(ts)) # Need to keep old method of building the function since it uses `output_type`, # which can't be provided to `build_function` return_value = if isscalar diff --git a/src/systems/imperative_affect.jl b/src/systems/imperative_affect.jl new file mode 100644 index 0000000000..f20ad941da --- /dev/null +++ b/src/systems/imperative_affect.jl @@ -0,0 +1,220 @@ + +""" + ImperativeAffect(f::Function; modified::NamedTuple, observed::NamedTuple, ctx) + +`ImperativeAffect` is a helper for writing affect functions that will compute observed values and +ensure that modified values are correctly written back into the system. The affect function `f` needs to have +the signature + +``` + f(modified::NamedTuple, observed::NamedTuple, ctx, integrator)::NamedTuple +``` + +The function `f` will be called with `observed` and `modified` `NamedTuple`s that are derived from their respective `NamedTuple` definitions. +Each declaration`NamedTuple` should map an expression to a symbol; for example if we pass `observed=(; x = a + b)` this will alias the result of executing `a+b` in the system as `x` +so the value of `a + b` will be accessible as `observed.x` in `f`. `modified` currently restricts symbolic expressions to only bare variables, so only tuples of the form +`(; x = y)` or `(; x)` (which aliases `x` as itself) are allowed. + +The argument NamedTuples (for instance `(;x=y)`) will be populated with the declared values on function entry; if we require `(;x=y)` in `observed` and `y=2`, for example, +then the NamedTuple `(;x=2)` will be passed as `observed` to the affect function `f`. + +The NamedTuple returned from `f` includes the values to be written back to the system after `f` returns. For example, if we want to update the value of `x` to be the result of `x + y` we could write + + ImperativeAffect(observed=(; x_plus_y = x + y), modified=(; x)) do m, o + @set! m.x = o.x_plus_y + end + +Where we use Setfield to copy the tuple `m` with a new value for `x`, then return the modified value of `m`. All values updated by the tuple must have names originally declared in +`modified`; a runtime error will be produced if a value is written that does not appear in `modified`. The user can dynamically decide not to write a value back by not including it +in the returned tuple, in which case the associated field will not be updated. +""" +@kwdef struct ImperativeAffect + f::Any + obs::Vector + obs_syms::Vector{Symbol} + modified::Vector + mod_syms::Vector{Symbol} + ctx::Any + skip_checks::Bool +end + +function ImperativeAffect(f::Function; + observed::NamedTuple = NamedTuple{()}(()), + modified::NamedTuple = NamedTuple{()}(()), + ctx = nothing, + skip_checks = false) + ImperativeAffect(f, + collect(values(observed)), collect(keys(observed)), + collect(values(modified)), collect(keys(modified)), + ctx, skip_checks) +end +function ImperativeAffect(f::Function, modified::NamedTuple; + observed::NamedTuple = NamedTuple{()}(()), ctx = nothing, skip_checks = false) + ImperativeAffect( + f, observed = observed, modified = modified, ctx = ctx, skip_checks = skip_checks) +end +function ImperativeAffect( + f::Function, modified::NamedTuple, observed::NamedTuple; ctx = nothing, skip_checks = false) + ImperativeAffect( + f, observed = observed, modified = modified, ctx = ctx, skip_checks = skip_checks) +end +function ImperativeAffect( + f::Function, modified::NamedTuple, observed::NamedTuple, ctx; skip_checks = false) + ImperativeAffect( + f, observed = observed, modified = modified, ctx = ctx, skip_checks = skip_checks) +end + +function Base.show(io::IO, mfa::ImperativeAffect) + obs_vals = join(map((ob, nm) -> "$ob => $nm", mfa.obs, mfa.obs_syms), ", ") + mod_vals = join(map((md, nm) -> "$md => $nm", mfa.modified, mfa.mod_syms), ", ") + affect = mfa.f + print(io, + "ImperativeAffect(observed: [$obs_vals], modified: [$mod_vals], affect:$affect)") +end +func(f::ImperativeAffect) = f.f +context(a::ImperativeAffect) = a.ctx +observed(a::ImperativeAffect) = a.obs +observed_syms(a::ImperativeAffect) = a.obs_syms +discretes(a::ImperativeAffect) = filter(ModelingToolkit.isparameter, a.modified) +modified(a::ImperativeAffect) = a.modified +modified_syms(a::ImperativeAffect) = a.mod_syms + +function Base.:(==)(a1::ImperativeAffect, a2::ImperativeAffect) + isequal(a1.f, a2.f) && isequal(a1.obs, a2.obs) && isequal(a1.modified, a2.modified) && + isequal(a1.obs_syms, a2.obs_syms) && isequal(a1.mod_syms, a2.mod_syms) && + isequal(a1.ctx, a2.ctx) +end + +function Base.hash(a::ImperativeAffect, s::UInt) + s = hash(a.f, s) + s = hash(a.obs, s) + s = hash(a.obs_syms, s) + s = hash(a.modified, s) + s = hash(a.mod_syms, s) + hash(a.ctx, s) +end + + +namespace_affects(af::ImperativeAffect, s) = namespace_affect(af, s) +function namespace_affect(affect::ImperativeAffect, s) + ImperativeAffect(func(affect), + namespace_expr.(observed(affect), (s,)), + observed_syms(affect), + renamespace.((s,), modified(affect)), + modified_syms(affect), + context(affect), + affect.skip_checks) +end + +function compile_affect(affect::ImperativeAffect, cb, sys, dvs, ps; kwargs...) + compile_user_affect(affect, cb, sys, dvs, ps; kwargs...) +end + +function compile_user_affect(affect::ImperativeAffect, cb, sys, dvs, ps; kwargs...) + #= + Implementation sketch: + generate observed function (oop), should save to a component array under obs_syms + do the same stuff as the normal FA for pars_syms + call the affect method + unpack and apply the resulting values + =# + function check_dups(syms, exprs) # = (syms_dedup, exprs_dedup) + seen = Set{Symbol}() + syms_dedup = [] + exprs_dedup = [] + for (sym, exp) in Iterators.zip(syms, exprs) + if !in(sym, seen) + push!(syms_dedup, sym) + push!(exprs_dedup, exp) + push!(seen, sym) + elseif !affect.skip_checks + @warn "Expression $(expr) is aliased as $sym, which has already been used. The first definition will be used." + end + end + return (syms_dedup, exprs_dedup) + end + + obs_exprs = observed(affect) + if !affect.skip_checks + for oexpr in obs_exprs + invalid_vars = invalid_variables(sys, oexpr) + if length(invalid_vars) > 0 + error("Observed equation $(oexpr) in affect refers to missing variable(s) $(invalid_vars); the variables may not have been added (e.g. if a component is missing).") + end + end + end + obs_syms = observed_syms(affect) + obs_syms, obs_exprs = check_dups(obs_syms, obs_exprs) + + mod_exprs = modified(affect) + if !affect.skip_checks + for mexpr in mod_exprs + if !check_assignable(sys, mexpr) + @warn ("Expression $mexpr cannot be assigned to; currently only unknowns and parameters may be updated by an affect.") + end + invalid_vars = unassignable_variables(sys, mexpr) + if length(invalid_vars) > 0 + error("Modified equation $(mexpr) in affect refers to missing variable(s) $(invalid_vars); the variables may not have been added (e.g. if a component is missing) or they may have been reduced away.") + end + end + end + mod_syms = modified_syms(affect) + mod_syms, mod_exprs = check_dups(mod_syms, mod_exprs) + + overlapping_syms = intersect(mod_syms, obs_syms) + if length(overlapping_syms) > 0 && !affect.skip_checks + @warn "The symbols $overlapping_syms are declared as both observed and modified; this is a code smell because it becomes easy to confuse them and assign/not assign a value." + end + + # sanity checks done! now build the data and update function for observed values + mkzero(sz) = + if sz === () + 0.0 + else + zeros(sz) + end + obs_fun = build_explicit_observed_function( + sys, Symbolics.scalarize.(obs_exprs); + array_type = Tuple) + obs_sym_tuple = (obs_syms...,) + + # okay so now to generate the stuff to assign it back into the system + mod_pairs = mod_exprs .=> mod_syms + mod_names = (mod_syms...,) + mod_og_val_fun = build_explicit_observed_function( + sys, Symbolics.scalarize.(first.(mod_pairs)); + array_type = Tuple) + + upd_funs = NamedTuple{mod_names}((setu.((sys,), first.(mod_pairs))...,)) + + if has_index_cache(sys) && (ic = get_index_cache(sys)) !== nothing + save_idxs = get(ic.callback_to_clocks, cb, Int[]) + else + save_idxs = Int[] + end + + let user_affect = func(affect), ctx = context(affect) + function (integ) + # update the to-be-mutated values; this ensures that if you do a no-op then nothing happens + modvals = mod_og_val_fun(integ.u, integ.p, integ.t) + upd_component_array = NamedTuple{mod_names}(modvals) + + # update the observed values + obs_component_array = NamedTuple{obs_sym_tuple}(obs_fun( + integ.u, integ.p, integ.t)) + + # let the user do their thing + upd_vals = user_affect(upd_component_array, obs_component_array, ctx, integ) + + # write the new values back to the integrator + _generated_writeback(integ, upd_funs, upd_vals) + + for idx in save_idxs + SciMLBase.save_discretes!(integ, idx) + end + end + end +end + + +scalarize_affects(affects::ImperativeAffect) = affects From 1c78dee591ac5623a240084005545dbca04e2941 Mon Sep 17 00:00:00 2001 From: Benjamin Chung Date: Mon, 9 Dec 2024 03:16:24 -0800 Subject: [PATCH 0455/2176] Fix tests & update API usage --- src/systems/callbacks.jl | 2 +- src/systems/imperative_affect.jl | 4 ++-- test/symbolic_events.jl | 8 ++++---- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/systems/callbacks.jl b/src/systems/callbacks.jl index 4533534029..57db5e097c 100644 --- a/src/systems/callbacks.jl +++ b/src/systems/callbacks.jl @@ -818,7 +818,7 @@ function generate_vector_rootfinding_callback( let save_idxs = save_idxs custom_init = fn.initialize (i) -> begin - isnothing(custom_init) && custom_init(i) + !isnothing(custom_init) && custom_init(i) for idx in save_idxs SciMLBase.save_discretes!(i, idx) end diff --git a/src/systems/imperative_affect.jl b/src/systems/imperative_affect.jl index f20ad941da..19f7f7590d 100644 --- a/src/systems/imperative_affect.jl +++ b/src/systems/imperative_affect.jl @@ -175,7 +175,7 @@ function compile_user_affect(affect::ImperativeAffect, cb, sys, dvs, ps; kwargs. end obs_fun = build_explicit_observed_function( sys, Symbolics.scalarize.(obs_exprs); - array_type = Tuple) + mkarray = (es,_) -> MakeTuple(es)) obs_sym_tuple = (obs_syms...,) # okay so now to generate the stuff to assign it back into the system @@ -183,7 +183,7 @@ function compile_user_affect(affect::ImperativeAffect, cb, sys, dvs, ps; kwargs. mod_names = (mod_syms...,) mod_og_val_fun = build_explicit_observed_function( sys, Symbolics.scalarize.(first.(mod_pairs)); - array_type = Tuple) + mkarray = (es,_) -> MakeTuple(es)) upd_funs = NamedTuple{mod_names}((setu.((sys,), first.(mod_pairs))...,)) diff --git a/test/symbolic_events.jl b/test/symbolic_events.jl index 717d2438ac..858a30f4fd 100644 --- a/test/symbolic_events.jl +++ b/test/symbolic_events.jl @@ -936,7 +936,7 @@ end @named trigsys = ODESystem(eqs, t; continuous_events = [evt1, evt2]) trigsys_ss = structural_simplify(trigsys) prob = ODEProblem(trigsys_ss, [], (0.0, 2π)) - sol = solve(prob, Tsit5()) + sol = solve(prob, Tsit5(); dtmax=0.01) required_crossings_c1 = [π / 2, 3 * π / 2] required_crossings_c2 = [π / 6, π / 2, 5 * π / 6, 7 * π / 6, 3 * π / 2, 11 * π / 6] @test maximum(abs.(first.(cr1) .- required_crossings_c1)) < 1e-4 @@ -1079,8 +1079,8 @@ end @test sort(canonicalize(Discrete(), prob.p)[1]) == [0.0, 1.0, 2.0] sol = solve(prob, Tsit5()) - @test sol[a] == [-1.0] - @test sol[b] == [5.0, 5.0] + @test sol[a] == [1.0,-1.0] + @test sol[b] == [2.0,5.0, 5.0] @test sol[c] == [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0] end @testset "Heater" begin @@ -1248,7 +1248,7 @@ end ss = structural_simplify(sys) prob = ODEProblem(ss, [theta => 0.0], (0.0, pi)) sol = solve(prob, Tsit5(); dtmax = 0.01) - @test getp(sol, cnt)(sol) == 197 # we get 2 pulses per phase cycle (cos 0 crossing) and we go to 100 cycles; we miss a few due to the initial state + @test getp(sol, cnt)(sol) == 198 # we get 2 pulses per phase cycle (cos 0 crossing) and we go to 100 cycles; we miss a few due to the initial state end @testset "Initialization" begin From aa556d612a12f2cc297b7af7282a959aa8674775 Mon Sep 17 00:00:00 2001 From: Benjamin Chung Date: Mon, 9 Dec 2024 03:59:31 -0800 Subject: [PATCH 0456/2176] Adjust quadrature test forward a little to avoid numerical issues --- test/symbolic_events.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/symbolic_events.jl b/test/symbolic_events.jl index 858a30f4fd..1138b7c96f 100644 --- a/test/symbolic_events.jl +++ b/test/symbolic_events.jl @@ -1246,7 +1246,7 @@ end @named sys = ODESystem( eqs, t, [theta, omega], params; continuous_events = [qAevt, qBevt]) ss = structural_simplify(sys) - prob = ODEProblem(ss, [theta => 0.0], (0.0, pi)) + prob = ODEProblem(ss, [theta => 1e-5], (0.0, pi)) sol = solve(prob, Tsit5(); dtmax = 0.01) @test getp(sol, cnt)(sol) == 198 # we get 2 pulses per phase cycle (cos 0 crossing) and we go to 100 cycles; we miss a few due to the initial state end From c169b9e97629c3031bc9f0e82f3c0899e6e17f71 Mon Sep 17 00:00:00 2001 From: Benjamin Chung Date: Mon, 9 Dec 2024 04:01:00 -0800 Subject: [PATCH 0457/2176] Formatter --- src/systems/imperative_affect.jl | 6 ++---- test/symbolic_events.jl | 6 +++--- 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/src/systems/imperative_affect.jl b/src/systems/imperative_affect.jl index 19f7f7590d..2f489913ad 100644 --- a/src/systems/imperative_affect.jl +++ b/src/systems/imperative_affect.jl @@ -94,7 +94,6 @@ function Base.hash(a::ImperativeAffect, s::UInt) hash(a.ctx, s) end - namespace_affects(af::ImperativeAffect, s) = namespace_affect(af, s) function namespace_affect(affect::ImperativeAffect, s) ImperativeAffect(func(affect), @@ -175,7 +174,7 @@ function compile_user_affect(affect::ImperativeAffect, cb, sys, dvs, ps; kwargs. end obs_fun = build_explicit_observed_function( sys, Symbolics.scalarize.(obs_exprs); - mkarray = (es,_) -> MakeTuple(es)) + mkarray = (es, _) -> MakeTuple(es)) obs_sym_tuple = (obs_syms...,) # okay so now to generate the stuff to assign it back into the system @@ -183,7 +182,7 @@ function compile_user_affect(affect::ImperativeAffect, cb, sys, dvs, ps; kwargs. mod_names = (mod_syms...,) mod_og_val_fun = build_explicit_observed_function( sys, Symbolics.scalarize.(first.(mod_pairs)); - mkarray = (es,_) -> MakeTuple(es)) + mkarray = (es, _) -> MakeTuple(es)) upd_funs = NamedTuple{mod_names}((setu.((sys,), first.(mod_pairs))...,)) @@ -216,5 +215,4 @@ function compile_user_affect(affect::ImperativeAffect, cb, sys, dvs, ps; kwargs. end end - scalarize_affects(affects::ImperativeAffect) = affects diff --git a/test/symbolic_events.jl b/test/symbolic_events.jl index 1138b7c96f..ccf0a17a40 100644 --- a/test/symbolic_events.jl +++ b/test/symbolic_events.jl @@ -936,7 +936,7 @@ end @named trigsys = ODESystem(eqs, t; continuous_events = [evt1, evt2]) trigsys_ss = structural_simplify(trigsys) prob = ODEProblem(trigsys_ss, [], (0.0, 2π)) - sol = solve(prob, Tsit5(); dtmax=0.01) + sol = solve(prob, Tsit5(); dtmax = 0.01) required_crossings_c1 = [π / 2, 3 * π / 2] required_crossings_c2 = [π / 6, π / 2, 5 * π / 6, 7 * π / 6, 3 * π / 2, 11 * π / 6] @test maximum(abs.(first.(cr1) .- required_crossings_c1)) < 1e-4 @@ -1079,8 +1079,8 @@ end @test sort(canonicalize(Discrete(), prob.p)[1]) == [0.0, 1.0, 2.0] sol = solve(prob, Tsit5()) - @test sol[a] == [1.0,-1.0] - @test sol[b] == [2.0,5.0, 5.0] + @test sol[a] == [1.0, -1.0] + @test sol[b] == [2.0, 5.0, 5.0] @test sol[c] == [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0] end @testset "Heater" begin From c8a207bb10803ae3248c61245fdf6d7f05e0366e Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 10 Dec 2024 16:45:01 +0530 Subject: [PATCH 0458/2176] build: bump version --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index fe3a9e88e5..534b0bd32b 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "ModelingToolkit" uuid = "961ee093-0014-501f-94e3-6117800e7a78" authors = ["Yingbo Ma ", "Chris Rackauckas and contributors"] -version = "9.56.0" +version = "9.57.0" [deps] AbstractTrees = "1520ce14-60c1-5f80-bbc7-55ef81b5835c" From b24e5256cd9d3e15b25e9b9468ee2996af6febdc Mon Sep 17 00:00:00 2001 From: Benjamin Chung Date: Tue, 10 Dec 2024 17:23:24 -0800 Subject: [PATCH 0459/2176] Update src/systems/callbacks.jl Co-authored-by: Aayush Sabharwal --- src/systems/callbacks.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/systems/callbacks.jl b/src/systems/callbacks.jl index 57db5e097c..eaf31a9c5f 100644 --- a/src/systems/callbacks.jl +++ b/src/systems/callbacks.jl @@ -98,7 +98,7 @@ sharp discontinuity between integrator steps (which in this example would not no guaranteed to be triggered. Once detected the integrator will "wind back" through a root-finding process to identify the point when the condition became active; the method used -is specified by `rootfind` from [`SciMLBase.RootfindOpt`](@ref). If we denote the time when the condition becomes active as `tc``, +is specified by `rootfind` from [`SciMLBase.RootfindOpt`](@ref). If we denote the time when the condition becomes active as `tc`, the value in the integrator after windback will be: * `u[tc-epsilon], p[tc-epsilon], tc` if `LeftRootFind` is used, * `u[tc+epsilon], p[tc+epsilon], tc` if `RightRootFind` is used, From e5b5f61f8dcd4d61c5ae450a82c869ac51462dd8 Mon Sep 17 00:00:00 2001 From: Christopher Rackauckas Date: Wed, 11 Dec 2024 01:43:34 -0100 Subject: [PATCH 0460/2176] Update Project.toml --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 534b0bd32b..0b56e53805 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "ModelingToolkit" uuid = "961ee093-0014-501f-94e3-6117800e7a78" authors = ["Yingbo Ma ", "Chris Rackauckas and contributors"] -version = "9.57.0" +version = "9.58.0" [deps] AbstractTrees = "1520ce14-60c1-5f80-bbc7-55ef81b5835c" From 6d6383abf73d16b6c3ecb86bfed3500d70d25257 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 11 Dec 2024 17:11:17 +0530 Subject: [PATCH 0461/2176] fix: allow interpolating names in `@brownian` --- src/variables.jl | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/variables.jl b/src/variables.jl index d11c6c1834..3b0d64e7ea 100644 --- a/src/variables.jl +++ b/src/variables.jl @@ -489,7 +489,9 @@ $(SIGNATURES) Define one or more Brownian variables. """ macro brownian(xs...) - all(x -> x isa Symbol || Meta.isexpr(x, :call) && x.args[1] == :$, xs) || + all( + x -> x isa Symbol || Meta.isexpr(x, :call) && x.args[1] == :$ || Meta.isexpr(x, :$), + xs) || error("@brownian only takes scalar expressions!") Symbolics._parse_vars(:brownian, Real, From a2acbe59eebeec40b6ddb4355636aedb8ec5134f Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 11 Dec 2024 17:25:26 +0530 Subject: [PATCH 0462/2176] feat: add ability to convert `SDESystem` to equivalent `ODESystem` --- src/systems/diffeqs/sdesystem.jl | 41 ++++++++++++++++++++++++++ test/sdesystem.jl | 49 ++++++++++++++++++++++++++++++++ 2 files changed, 90 insertions(+) diff --git a/src/systems/diffeqs/sdesystem.jl b/src/systems/diffeqs/sdesystem.jl index 011ba6e216..ac47f4c45c 100644 --- a/src/systems/diffeqs/sdesystem.jl +++ b/src/systems/diffeqs/sdesystem.jl @@ -263,6 +263,47 @@ function Base.:(==)(sys1::SDESystem, sys2::SDESystem) all(s1 == s2 for (s1, s2) in zip(get_systems(sys1), get_systems(sys2))) end +""" + function ODESystem(sys::SDESystem) + +Convert an `SDESystem` to the equivalent `ODESystem` using `@brownian` variables instead +of noise equations. The returned system will not be `iscomplete` and will not have an +index cache, regardless of `iscomplete(sys)`. +""" +function ODESystem(sys::SDESystem) + neqs = get_noiseeqs(sys) + eqs = equations(sys) + is_scalar_noise = get_is_scalar_noise(sys) + nbrownian = if is_scalar_noise + length(neqs) + else + size(neqs, 2) + end + brownvars = map(1:nbrownian) do i + name = gensym(Symbol(:brown_, i)) + only(@brownian $name) + end + if is_scalar_noise + brownterms = reduce(+, neqs .* brownvars; init = 0) + neweqs = map(eqs) do eq + eq.lhs ~ eq.rhs + brownterms + end + else + if neqs isa AbstractVector + neqs = reshape(neqs, (length(neqs), 1)) + end + brownterms = neqs * brownvars + neweqs = map(eqs, brownterms) do eq, brown + eq.lhs ~ eq.rhs + brown + end + end + newsys = ODESystem(neweqs, get_iv(sys), unknowns(sys), parameters(sys); + parameter_dependencies = parameter_dependencies(sys), defaults = defaults(sys), + continuous_events = continuous_events(sys), discrete_events = discrete_events(sys), + name = nameof(sys), description = description(sys), metadata = get_metadata(sys)) + @set newsys.parent = sys +end + function __num_isdiag_noise(mat) for i in axes(mat, 1) nnz = 0 diff --git a/test/sdesystem.jl b/test/sdesystem.jl index 749aca86a7..036c94b868 100644 --- a/test/sdesystem.jl +++ b/test/sdesystem.jl @@ -809,3 +809,52 @@ end prob = SDEProblem(sys, [x => 1.0, y => 1.0], (0.0, 1.0)) @test prob[z] ≈ 2.0 end + +@testset "SDESystem to ODESystem" begin + @variables x(t) y(t) z(t) + @testset "Scalar noise" begin + @named sys = SDESystem([D(x) ~ x, D(y) ~ y, z ~ x + y], [x, y, 3], + t, [x, y, z], [], is_scalar_noise = true) + odesys = ODESystem(sys) + @test odesys isa ODESystem + vs = ModelingToolkit.vars(equations(odesys)) + nbrownian = count( + v -> ModelingToolkit.getvariabletype(v) == ModelingToolkit.BROWNIAN, vs) + @test nbrownian == 3 + for eq in equations(odesys) + ModelingToolkit.isdiffeq(eq) || continue + @test length(arguments(eq.rhs)) == 4 + end + end + + @testset "Non-scalar vector noise" begin + @named sys = SDESystem([D(x) ~ x, D(y) ~ y, z ~ x + y], [x, y, 0], + t, [x, y, z], [], is_scalar_noise = false) + odesys = ODESystem(sys) + @test odesys isa ODESystem + vs = ModelingToolkit.vars(equations(odesys)) + nbrownian = count( + v -> ModelingToolkit.getvariabletype(v) == ModelingToolkit.BROWNIAN, vs) + @test nbrownian == 1 + for eq in equations(odesys) + ModelingToolkit.isdiffeq(eq) || continue + @test length(arguments(eq.rhs)) == 2 + end + end + + @testset "Matrix noise" begin + noiseeqs = [x+y y+z z+x + 2y 2z 2x + z+1 x+1 y+1] + @named sys = SDESystem([D(x) ~ x, D(y) ~ y, D(z) ~ z], noiseeqs, t, [x, y, z], []) + odesys = ODESystem(sys) + @test odesys isa ODESystem + vs = ModelingToolkit.vars(equations(odesys)) + nbrownian = count( + v -> ModelingToolkit.getvariabletype(v) == ModelingToolkit.BROWNIAN, vs) + @test nbrownian == 3 + for eq in equations(odesys) + @test length(arguments(eq.rhs)) == 4 + end + end +end From ea9b6bd02f683f7c75ce858187e92293f4c4e72a Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 11 Dec 2024 17:25:38 +0530 Subject: [PATCH 0463/2176] feat: enable `structural_simplify(::SDESystem)` --- src/systems/systems.jl | 4 ++++ test/sdesystem.jl | 10 ++++++++++ 2 files changed, 14 insertions(+) diff --git a/src/systems/systems.jl b/src/systems/systems.jl index 862718968d..97d22cf4cf 100644 --- a/src/systems/systems.jl +++ b/src/systems/systems.jl @@ -72,6 +72,10 @@ function __structural_simplify(sys::JumpSystem, args...; kwargs...) return sys end +function __structural_simplify(sys::SDESystem, args...; kwargs...) + return __structural_simplify(ODESystem(sys), args...; kwargs...) +end + function __structural_simplify(sys::AbstractSystem, io = nothing; simplify = false, kwargs...) sys = expand_connections(sys) diff --git a/test/sdesystem.jl b/test/sdesystem.jl index 036c94b868..8069581dcc 100644 --- a/test/sdesystem.jl +++ b/test/sdesystem.jl @@ -858,3 +858,13 @@ end end end end + +@testset "`structural_simplify(::SDESystem)`" begin + @variables x(t) y(t) + @mtkbuild sys = SDESystem( + [D(x) ~ x, y ~ 2x], [x, 0], t, [x, y], []; is_scalar_noise = true) + @test sys isa SDESystem + @test length(equations(sys)) == 1 + @test length(ModelingToolkit.get_noiseeqs(sys)) == 1 + @test length(observed(sys)) == 1 +end From 22d70938e2ac432c29c4787aa57c687bf45b76f3 Mon Sep 17 00:00:00 2001 From: CompatHelper Julia Date: Thu, 12 Dec 2024 00:25:48 +0000 Subject: [PATCH 0464/2176] CompatHelper: add new compat entry for Setfield at version 1 for package docs, (keep existing compat) --- docs/Project.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/Project.toml b/docs/Project.toml index 24f00b4db2..a358455503 100644 --- a/docs/Project.toml +++ b/docs/Project.toml @@ -38,6 +38,7 @@ OptimizationOptimJL = "0.1, 0.4" OrdinaryDiffEq = "6.31" Plots = "1.36" SciMLStructures = "1.1" +Setfield = "1" StochasticDiffEq = "6" SymbolicIndexingInterface = "0.3.1" SymbolicUtils = "3" From 552b0393f683fb16935c90779296347454cae9c3 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 11 Dec 2024 12:14:27 +0530 Subject: [PATCH 0465/2176] fix: respect `use_homotopy_continuation` in `NonlinearProblem` and default it to `false` --- src/systems/nonlinear/nonlinearsystem.jl | 10 ++++++---- test/extensions/homotopy_continuation.jl | 2 +- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/src/systems/nonlinear/nonlinearsystem.jl b/src/systems/nonlinear/nonlinearsystem.jl index 3cb68853aa..2c788688bd 100644 --- a/src/systems/nonlinear/nonlinearsystem.jl +++ b/src/systems/nonlinear/nonlinearsystem.jl @@ -496,13 +496,15 @@ end function DiffEqBase.NonlinearProblem{iip}(sys::NonlinearSystem, u0map, parammap = DiffEqBase.NullParameters(); - check_length = true, use_homotopy_continuation = true, kwargs...) where {iip} + check_length = true, use_homotopy_continuation = false, kwargs...) where {iip} if !iscomplete(sys) error("A completed `NonlinearSystem` is required. Call `complete` or `structural_simplify` on the system before creating a `NonlinearProblem`") end - prob = safe_HomotopyContinuationProblem(sys, u0map, parammap; check_length, kwargs...) - if prob isa HomotopyContinuationProblem - return prob + if use_homotopy_continuation + prob = safe_HomotopyContinuationProblem(sys, u0map, parammap; check_length, kwargs...) + if prob isa HomotopyContinuationProblem + return prob + end end f, u0, p = process_SciMLProblem(NonlinearFunction{iip}, sys, u0map, parammap; check_length, kwargs...) diff --git a/test/extensions/homotopy_continuation.jl b/test/extensions/homotopy_continuation.jl index 554f9e1e1d..3f4a3fbc71 100644 --- a/test/extensions/homotopy_continuation.jl +++ b/test/extensions/homotopy_continuation.jl @@ -29,7 +29,7 @@ import HomotopyContinuation @test SciMLBase.successful_retcode(sol) @test norm(sol.resid)≈0.0 atol=1e-10 - prob2 = NonlinearProblem(sys, u0) + prob2 = NonlinearProblem(sys, u0; use_homotopy_continuation = true) @test prob2 isa HomotopyContinuationProblem sol = solve(prob2; threading = false) @test SciMLBase.successful_retcode(sol) From e75d06fb2f680ccda550f6ad37cc875497119736 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 11 Dec 2024 14:35:16 +0530 Subject: [PATCH 0466/2176] fix: properly handle rational functions in HomotopyContinuation --- ext/MTKHomotopyContinuationExt.jl | 5 +- .../nonlinear/homotopy_continuation.jl | 63 +++++++++++++------ src/systems/nonlinear/nonlinearsystem.jl | 3 +- test/extensions/homotopy_continuation.jl | 31 ++++++++- 4 files changed, 79 insertions(+), 23 deletions(-) diff --git a/ext/MTKHomotopyContinuationExt.jl b/ext/MTKHomotopyContinuationExt.jl index c4a090d9a8..8f17c05b18 100644 --- a/ext/MTKHomotopyContinuationExt.jl +++ b/ext/MTKHomotopyContinuationExt.jl @@ -101,7 +101,8 @@ function MTK.HomotopyContinuationProblem( return prob end -function MTK._safe_HomotopyContinuationProblem(sys, u0map, parammap = nothing; kwargs...) +function MTK._safe_HomotopyContinuationProblem(sys, u0map, parammap = nothing; + fraction_cancel_fn = SymbolicUtils.simplify_fractions, kwargs...) if !iscomplete(sys) error("A completed `NonlinearSystem` is required. Call `complete` or `structural_simplify` on the system before creating a `HomotopyContinuationProblem`") end @@ -109,7 +110,7 @@ function MTK._safe_HomotopyContinuationProblem(sys, u0map, parammap = nothing; k if transformation isa MTK.NotPolynomialError return transformation end - result = MTK.transform_system(sys, transformation) + result = MTK.transform_system(sys, transformation; fraction_cancel_fn) if result isa MTK.NotPolynomialError return result end diff --git a/src/systems/nonlinear/homotopy_continuation.jl b/src/systems/nonlinear/homotopy_continuation.jl index 03aeed1edf..00c6c7f1b0 100644 --- a/src/systems/nonlinear/homotopy_continuation.jl +++ b/src/systems/nonlinear/homotopy_continuation.jl @@ -442,7 +442,8 @@ Transform the system `sys` with `transformation` and return a `PolynomialTransformationResult`, or a `NotPolynomialError` if the system cannot be transformed. """ -function transform_system(sys::NonlinearSystem, transformation::PolynomialTransformation) +function transform_system(sys::NonlinearSystem, transformation::PolynomialTransformation; + fraction_cancel_fn = simplify_fractions) subrules = transformation.substitution_rules dvs = unknowns(sys) eqs = full_equations(sys) @@ -463,7 +464,7 @@ function transform_system(sys::NonlinearSystem, transformation::PolynomialTransf return NotPolynomialError( VariablesAsPolyAndNonPoly(dvs[poly_and_nonpoly]), eqs, polydata) end - num, den = handle_rational_polynomials(t, new_dvs) + num, den = handle_rational_polynomials(t, new_dvs; fraction_cancel_fn) # make factors different elements, otherwise the nonzero factors artificially # inflate the error of the zero factor. if iscall(den) && operation(den) == * @@ -492,43 +493,67 @@ $(TYPEDSIGNATURES) Given a `x`, a polynomial in variables in `wrt` which may contain rational functions, express `x` as a single rational function with polynomial `num` and denominator `den`. Return `(num, den)`. + +Keyword arguments: +- `fraction_cancel_fn`: A function which takes a fraction (`operation(expr) == /`) and returns + a simplified symbolic quantity with common factors in the numerator and denominator are + cancelled. Defaults to `SymbolicUtils.simplify_fractions`, but can be changed to + `nothing` to improve performance on large polynomials at the cost of avoiding non-trivial + cancellation. """ -function handle_rational_polynomials(x, wrt) +function handle_rational_polynomials(x, wrt; fraction_cancel_fn = simplify_fractions) x = unwrap(x) symbolic_type(x) == NotSymbolic() && return x, 1 iscall(x) || return x, 1 contains_variable(x, wrt) || return x, 1 any(isequal(x), wrt) && return x, 1 - # simplify_fractions cancels out some common factors - # and expands (a / b)^c to a^c / b^c, so we only need - # to handle these cases - x = simplify_fractions(x) op = operation(x) args = arguments(x) if op == / # numerator and denominator are trivial num, den = args - # but also search for rational functions in numerator - n, d = handle_rational_polynomials(num, wrt) - num, den = n, den * d - elseif op == + + n1, d1 = handle_rational_polynomials(num, wrt; fraction_cancel_fn) + n2, d2 = handle_rational_polynomials(den, wrt; fraction_cancel_fn) + num, den = n1 * d2, d1 * n2 + elseif (op == +) || (op == -) num = 0 den = 1 - - # we don't need to do common denominator - # because we don't care about cases where denominator - # is zero. The expression is zero when all the numerators - # are zero. + if op == - + args[2] = -args[2] + end + for arg in args + n, d = handle_rational_polynomials(arg, wrt; fraction_cancel_fn) + num = num * d + n * den + den *= d + end + elseif op == ^ + base, pow = args + num, den = handle_rational_polynomials(base, wrt; fraction_cancel_fn) + num ^= pow + den ^= pow + elseif op == * + num = 1 + den = 1 for arg in args - n, d = handle_rational_polynomials(arg, wrt) - num += n + n, d = handle_rational_polynomials(arg, wrt; fraction_cancel_fn) + num *= n den *= d end else - return x, 1 + error("Unhandled operation in `handle_rational_polynomials`. This should never happen. Please open an issue in ModelingToolkit.jl with an MWE.") + end + + if fraction_cancel_fn !== nothing + expr = fraction_cancel_fn(num / den) + if iscall(expr) && operation(expr) == / + num, den = arguments(expr) + else + num, den = expr, 1 + end end + # if the denominator isn't a polynomial in `wrt`, better to not include it # to reduce the size of the gcd polynomial if !contains_variable(den, wrt) diff --git a/src/systems/nonlinear/nonlinearsystem.jl b/src/systems/nonlinear/nonlinearsystem.jl index 2c788688bd..2751f4f5cd 100644 --- a/src/systems/nonlinear/nonlinearsystem.jl +++ b/src/systems/nonlinear/nonlinearsystem.jl @@ -501,7 +501,8 @@ function DiffEqBase.NonlinearProblem{iip}(sys::NonlinearSystem, u0map, error("A completed `NonlinearSystem` is required. Call `complete` or `structural_simplify` on the system before creating a `NonlinearProblem`") end if use_homotopy_continuation - prob = safe_HomotopyContinuationProblem(sys, u0map, parammap; check_length, kwargs...) + prob = safe_HomotopyContinuationProblem( + sys, u0map, parammap; check_length, kwargs...) if prob isa HomotopyContinuationProblem return prob end diff --git a/test/extensions/homotopy_continuation.jl b/test/extensions/homotopy_continuation.jl index 3f4a3fbc71..9e15ea857e 100644 --- a/test/extensions/homotopy_continuation.jl +++ b/test/extensions/homotopy_continuation.jl @@ -1,4 +1,5 @@ using ModelingToolkit, NonlinearSolve, SymbolicIndexingInterface +using SymbolicUtils import ModelingToolkit as MTK using LinearAlgebra using Test @@ -34,6 +35,8 @@ import HomotopyContinuation sol = solve(prob2; threading = false) @test SciMLBase.successful_retcode(sol) @test norm(sol.resid)≈0.0 atol=1e-10 + + @test NonlinearProblem(sys, u0; use_homotopy_continuation = false) isa NonlinearProblem end struct Wrapper @@ -217,7 +220,17 @@ end @mtkbuild sys = NonlinearSystem([x^2 + y^2 - 2x - 2 ~ 0, y ~ (x - 1) / (x - 2)]) prob = HomotopyContinuationProblem(sys, []) @test any(prob.denominator([2.0], parameter_values(prob)) .≈ 0.0) - @test_nowarn solve(prob; threading = false) + @test SciMLBase.successful_retcode(solve(prob; threading = false)) + end + + @testset "Rational function forced to common denominators" begin + @variables x = 1 + @mtkbuild sys = NonlinearSystem([0 ~ 1 / (1 + x) - x]) + prob = HomotopyContinuationProblem(sys, []) + @test any(prob.denominator([-1.0], parameter_values(prob)) .≈ 0.0) + sol = solve(prob; threading = false) + @test SciMLBase.successful_retcode(sol) + @test 1 / (1 + sol.u[1]) - sol.u[1]≈0.0 atol=1e-10 end end @@ -229,3 +242,19 @@ end @test sol[x] ≈ √2.0 @test sol[y] ≈ sin(√2.0) end + +@testset "`fraction_cancel_fn`" begin + @variables x = 1 + @named sys = NonlinearSystem([0 ~ ((x^2 - 5x + 6) / (x - 2) - 1) * (x^2 - 7x + 12) / + (x - 4)^3]) + sys = complete(sys) + + @testset "`simplify_fractions`" begin + prob = HomotopyContinuationProblem(sys, []) + @test prob.denominator([0.0], parameter_values(prob)) ≈ [4.0] + end + @testset "`nothing`" begin + prob = HomotopyContinuationProblem(sys, []; fraction_cancel_fn = nothing) + @test sort(prob.denominator([0.0], parameter_values(prob))) ≈ [2.0, 4.0^3] + end +end From 41600b14e7ddea22cf17e529f005eb85aa98ce31 Mon Sep 17 00:00:00 2001 From: Benjamin Chung Date: Tue, 12 Nov 2024 23:53:53 -0800 Subject: [PATCH 0467/2176] Add a simple mechanism to add passes to structural simplify --- src/systems/systems.jl | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/systems/systems.jl b/src/systems/systems.jl index 97d22cf4cf..29cc96d28f 100644 --- a/src/systems/systems.jl +++ b/src/systems/systems.jl @@ -26,7 +26,7 @@ topological sort of the observed equations in `sys`. + `fully_determined=true` controls whether or not an error will be thrown if the number of equations don't match the number of inputs, outputs, and equations. """ function structural_simplify( - sys::AbstractSystem, io = nothing; simplify = false, split = true, + sys::AbstractSystem, io = nothing; additional_passes = [], simplify = false, split = true, allow_symbolic = false, allow_parameter = true, conservative = false, fully_determined = true, kwargs...) isscheduled(sys) && throw(RepeatedStructuralSimplificationError()) @@ -49,6 +49,9 @@ function structural_simplify( if newsys isa ODESystem || has_parent(newsys) @set! newsys.parent = complete(sys; split, flatten = false) end + for pass in additional_passes + newsys = pass(newsys) + end newsys = complete(newsys; split) if has_defaults(newsys) && (defs = get_defaults(newsys)) !== nothing ks = collect(keys(defs)) # take copy to avoid mutating defs while iterating. From c0abc56decc7691b25254af716f20cc9979f73cf Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 12 Dec 2024 14:59:55 +0530 Subject: [PATCH 0468/2176] fix: run additional passes before setting the parent of the system --- src/systems/systems.jl | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/systems/systems.jl b/src/systems/systems.jl index 29cc96d28f..47acd81a82 100644 --- a/src/systems/systems.jl +++ b/src/systems/systems.jl @@ -46,12 +46,12 @@ function structural_simplify( not yet supported. """) end + for pass in additional_passes + newsys = pass(newsys) + end if newsys isa ODESystem || has_parent(newsys) @set! newsys.parent = complete(sys; split, flatten = false) end - for pass in additional_passes - newsys = pass(newsys) - end newsys = complete(newsys; split) if has_defaults(newsys) && (defs = get_defaults(newsys)) !== nothing ks = collect(keys(defs)) # take copy to avoid mutating defs while iterating. From daf93edfde0a60ec6fb460f83e977c248cfeb849 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 12 Dec 2024 15:00:08 +0530 Subject: [PATCH 0469/2176] test: test additional passes mechanism --- test/structural_transformation/utils.jl | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/test/structural_transformation/utils.jl b/test/structural_transformation/utils.jl index 2704559f72..863e091aad 100644 --- a/test/structural_transformation/utils.jl +++ b/test/structural_transformation/utils.jl @@ -152,3 +152,12 @@ end end end end + +@testset "additional passes" begin + @variables x(t) y(t) + @named sys = ODESystem([D(x) ~ x, y ~ x + t], t) + value = Ref(0) + pass(sys; kwargs...) = (value[] += 1; return sys) + structural_simplify(sys; additional_passes = [pass]) + @test value[] == 1 +end From 18fdd5f79702627e30ce0acc971677a5cd1d303f Mon Sep 17 00:00:00 2001 From: vyudu Date: Fri, 13 Dec 2024 10:04:59 +0900 Subject: [PATCH 0470/2176] up --- Project.toml | 6 ++++-- test/bvproblem.jl | 2 +- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/Project.toml b/Project.toml index 093c632c42..19063a0342 100644 --- a/Project.toml +++ b/Project.toml @@ -72,14 +72,15 @@ MTKBifurcationKitExt = "BifurcationKit" MTKChainRulesCoreExt = "ChainRulesCore" MTKDeepDiffsExt = "DeepDiffs" MTKHomotopyContinuationExt = "HomotopyContinuation" -MTKLabelledArraysExt = "LabelledArrays" MTKInfiniteOptExt = "InfiniteOpt" +MTKLabelledArraysExt = "LabelledArrays" [compat] AbstractTrees = "0.3, 0.4" ArrayInterface = "6, 7" BifurcationKit = "0.4" BlockArrays = "1.1" +BoundaryValueDiffEq = "5.12.0" ChainRulesCore = "1" Combinatorics = "1" CommonSolve = "0.2.4" @@ -145,6 +146,7 @@ julia = "1.9" [extras] AmplNLWriter = "7c4d4715-977e-5154-bfe0-e096adeac482" BenchmarkTools = "6e4b80f9-dd63-53aa-95a3-0cdb28fa8baf" +BoundaryValueDiffEq = "764a87c0-6b3e-53db-9096-fe964310641d" ControlSystemsBase = "aaaaaaaa-a6ca-5380-bf3e-84a91bcd477e" DataInterpolations = "82cc6244-b520-54b8-b5a6-8a565e85f1d0" DeepDiffs = "ab62b9b5-e342-54a8-a765-a90f495de1a6" @@ -174,4 +176,4 @@ Sundials = "c3572dad-4567-51f8-b174-8c6c989267f4" Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" [targets] -test = ["AmplNLWriter", "BenchmarkTools", "ControlSystemsBase", "DataInterpolations", "DelayDiffEq", "NonlinearSolve", "ForwardDiff", "Ipopt", "Ipopt_jll", "ModelingToolkitStandardLibrary", "Optimization", "OptimizationOptimJL", "OptimizationMOI", "OrdinaryDiffEq", "OrdinaryDiffEqCore", "REPL", "Random", "ReferenceTests", "SafeTestsets", "StableRNGs", "Statistics", "SteadyStateDiffEq", "Test", "StochasticDiffEq", "Sundials", "StochasticDelayDiffEq", "Pkg", "JET"] +test = ["AmplNLWriter", "BenchmarkTools", "BoundaryValueDiffEq", "ControlSystemsBase", "DataInterpolations", "DelayDiffEq", "NonlinearSolve", "ForwardDiff", "Ipopt", "Ipopt_jll", "ModelingToolkitStandardLibrary", "Optimization", "OptimizationOptimJL", "OptimizationMOI", "OrdinaryDiffEq", "OrdinaryDiffEqCore", "REPL", "Random", "ReferenceTests", "SafeTestsets", "StableRNGs", "Statistics", "SteadyStateDiffEq", "Test", "StochasticDiffEq", "Sundials", "StochasticDelayDiffEq", "Pkg", "JET"] diff --git a/test/bvproblem.jl b/test/bvproblem.jl index 7235638cf0..7787bd9c3e 100644 --- a/test/bvproblem.jl +++ b/test/bvproblem.jl @@ -44,7 +44,7 @@ tspan = (0., 10.) bvp = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}(pend, u0map, tspan, parammap) sol = solve(bvp, MIRK4(), dt = 0.01); -bvp2 = SciMLBase.BVProblem{false, SciMLBase.AutoSpecialize}(pend, u0map, tspan, parammap) +bvp2 = SciMLBase.BVProblem{false, SciMLBase.FullSpecialize}(pend, u0map, tspan, parammap) sol2 = solve(bvp2, MIRK4(), dt = 0.01); op = ODEProblem(pend, u0map, tspan, parammap) From 61b79a849cc643982aa036158d7b5fd5b7e11c7b Mon Sep 17 00:00:00 2001 From: Fredrik Bagge Carlson Date: Mon, 16 Dec 2024 10:41:04 +0100 Subject: [PATCH 0471/2176] add option to include disturbance arguments in `generate_control_function` --- src/inputoutput.jl | 21 +++++++++++++------- test/input_output_handling.jl | 37 +++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 7 deletions(-) diff --git a/src/inputoutput.jl b/src/inputoutput.jl index 4a99ec11f5..89db016795 100644 --- a/src/inputoutput.jl +++ b/src/inputoutput.jl @@ -160,7 +160,7 @@ has_var(ex, x) = x ∈ Set(get_variables(ex)) # Build control function """ - (f_oop, f_ip), x_sym, p, io_sys = generate_control_function( + (f_oop, f_ip), x_sym, p_sym, io_sys = generate_control_function( sys::AbstractODESystem, inputs = unbound_inputs(sys), disturbance_inputs = nothing; @@ -177,8 +177,7 @@ f_ip : (xout,x,u,p,t) -> nothing The return values also include the chosen state-realization (the remaining unknowns) `x_sym` and parameters, in the order they appear as arguments to `f`. -If `disturbance_inputs` is an array of variables, the generated dynamics function will preserve any state and dynamics associated with disturbance inputs, but the disturbance inputs themselves will not be included as inputs to the generated function. The use case for this is to generate dynamics for state observers that estimate the influence of unmeasured disturbances, and thus require unknown variables for the disturbance model, but without disturbance inputs since the disturbances are not available for measurement. -See [`add_input_disturbance`](@ref) for a higher-level interface to this functionality. +If `disturbance_inputs` is an array of variables, the generated dynamics function will preserve any state and dynamics associated with disturbance inputs, but the disturbance inputs themselves will (by default) not be included as inputs to the generated function. The use case for this is to generate dynamics for state observers that estimate the influence of unmeasured disturbances, and thus require unknown variables for the disturbance model, but without disturbance inputs since the disturbances are not available for measurement. To add an input argument corresponding to the disturbance inputs, either include the disturbance inputs among the control inputs, or set `disturbance_argument=true`, in which case an additional input argument `w` is added to the generated function `(x,u,p,t,w)->rhs`. !!! note "Un-simplified system" This function expects `sys` to be un-simplified, i.e., `structural_simplify` or `@mtkbuild` should not be called on the system before passing it into this function. `generate_control_function` calls a special version of `structural_simplify` internally. @@ -196,6 +195,7 @@ f[1](x, inputs, p, t) """ function generate_control_function(sys::AbstractODESystem, inputs = unbound_inputs(sys), disturbance_inputs = disturbances(sys); + disturbance_argument = false, implicit_dae = false, simplify = false, eval_expression = false, @@ -219,10 +219,11 @@ function generate_control_function(sys::AbstractODESystem, inputs = unbound_inpu # ps = [ps; disturbance_inputs] end inputs = map(x -> time_varying_as_func(value(x), sys), inputs) + disturbance_inputs = map(x -> time_varying_as_func(value(x), sys), disturbance_inputs) eqs = [eq for eq in full_equations(sys)] eqs = map(subs_constants, eqs) - if disturbance_inputs !== nothing + if disturbance_inputs !== nothing && !disturbance_argument # Set all disturbance *inputs* to zero (we just want to keep the disturbance state) subs = Dict(disturbance_inputs .=> 0) eqs = [eq.lhs ~ substitute(eq.rhs, subs) for eq in eqs] @@ -239,16 +240,22 @@ function generate_control_function(sys::AbstractODESystem, inputs = unbound_inpu t = get_iv(sys) # pre = has_difference ? (ex -> ex) : get_postprocess_fbody(sys) - - args = (u, inputs, p..., t) + if disturbance_argument + args = (u, inputs, p..., t, disturbance_inputs) + else + args = (u, inputs, p..., t) + end if implicit_dae ddvs = map(Differential(get_iv(sys)), dvs) args = (ddvs, args...) end process = get_postprocess_fbody(sys) + wrapped_arrays_vars = disturbance_argument ? + wrap_array_vars(sys, rhss; dvs, ps, inputs, disturbance_inputs) : + wrap_array_vars(sys, rhss; dvs, ps, inputs) f = build_function(rhss, args...; postprocess_fbody = process, expression = Val{true}, wrap_code = wrap_mtkparameters(sys, false, 3) .∘ - wrap_array_vars(sys, rhss; dvs, ps, inputs) .∘ + wrapped_arrays_vars .∘ wrap_parameter_dependencies(sys, false), kwargs...) f = eval_or_rgf.(f; eval_expression, eval_module) diff --git a/test/input_output_handling.jl b/test/input_output_handling.jl index 9550a87f31..25434e3dfb 100644 --- a/test/input_output_handling.jl +++ b/test/input_output_handling.jl @@ -170,6 +170,43 @@ x = [rand()] u = [rand()] @test f[1](x, u, p, 1) == -x + u +# With disturbance inputs +@variables x(t)=0 u(t)=0 [input = true] d(t)=0 +eqs = [ + D(x) ~ -x + u + d^2 +] + +@named sys = ODESystem(eqs, t) +f, dvs, ps, io_sys = ModelingToolkit.generate_control_function( + sys, [u], [d], simplify = true) + +@test isequal(dvs[], x) +@test isempty(ps) + +p = nothing +x = [rand()] +u = [rand()] +@test f[1](x, u, p, 1) == -x + u + +# With added d argument +@variables x(t)=0 u(t)=0 [input = true] d(t)=0 +eqs = [ + D(x) ~ -x + u + d^2 +] + +@named sys = ODESystem(eqs, t) +f, dvs, ps, io_sys = ModelingToolkit.generate_control_function( + sys, [u], [d], simplify = true, disturbance_argument = true) + +@test isequal(dvs[], x) +@test isempty(ps) + +p = nothing +x = [rand()] +u = [rand()] +d = [rand()] +@test f[1](x, u, p, 1, d) == -x + u + [d[]^2] + # more complicated system @variables u(t) [input = true] From b7a4cdf71bbca02bb4f4a6f62a6c0d65964188d1 Mon Sep 17 00:00:00 2001 From: Fredrik Bagge Carlson Date: Mon, 16 Dec 2024 11:25:51 +0100 Subject: [PATCH 0472/2176] Apply suggestions from code review Co-authored-by: Aayush Sabharwal --- src/inputoutput.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/inputoutput.jl b/src/inputoutput.jl index 89db016795..f1ea0bc749 100644 --- a/src/inputoutput.jl +++ b/src/inputoutput.jl @@ -219,7 +219,7 @@ function generate_control_function(sys::AbstractODESystem, inputs = unbound_inpu # ps = [ps; disturbance_inputs] end inputs = map(x -> time_varying_as_func(value(x), sys), inputs) - disturbance_inputs = map(x -> time_varying_as_func(value(x), sys), disturbance_inputs) + disturbance_inputs = unwrap.(disturbance_inputs) eqs = [eq for eq in full_equations(sys)] eqs = map(subs_constants, eqs) @@ -251,7 +251,7 @@ function generate_control_function(sys::AbstractODESystem, inputs = unbound_inpu end process = get_postprocess_fbody(sys) wrapped_arrays_vars = disturbance_argument ? - wrap_array_vars(sys, rhss; dvs, ps, inputs, disturbance_inputs) : + wrap_array_vars(sys, rhss; dvs, ps, inputs, cachesyms = (disturbance_inputs,)) : wrap_array_vars(sys, rhss; dvs, ps, inputs) f = build_function(rhss, args...; postprocess_fbody = process, expression = Val{true}, wrap_code = wrap_mtkparameters(sys, false, 3) .∘ From d75009ba5fde932da03095edb5b811d3b03d38a6 Mon Sep 17 00:00:00 2001 From: Fredrik Bagge Carlson Date: Mon, 16 Dec 2024 12:08:12 +0100 Subject: [PATCH 0473/2176] Apply suggestions from code review Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- src/inputoutput.jl | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/inputoutput.jl b/src/inputoutput.jl index f1ea0bc749..1be1fbb8dd 100644 --- a/src/inputoutput.jl +++ b/src/inputoutput.jl @@ -251,7 +251,8 @@ function generate_control_function(sys::AbstractODESystem, inputs = unbound_inpu end process = get_postprocess_fbody(sys) wrapped_arrays_vars = disturbance_argument ? - wrap_array_vars(sys, rhss; dvs, ps, inputs, cachesyms = (disturbance_inputs,)) : + wrap_array_vars( + sys, rhss; dvs, ps, inputs, cachesyms = (disturbance_inputs,)) : wrap_array_vars(sys, rhss; dvs, ps, inputs) f = build_function(rhss, args...; postprocess_fbody = process, expression = Val{true}, wrap_code = wrap_mtkparameters(sys, false, 3) .∘ From a3789ae17a9f67df87b98179483e044c33e1fe44 Mon Sep 17 00:00:00 2001 From: Fredrik Bagge Carlson Date: Mon, 16 Dec 2024 14:20:27 +0100 Subject: [PATCH 0474/2176] test with split true and false --- test/input_output_handling.jl | 113 ++++++++++++++++++---------------- 1 file changed, 59 insertions(+), 54 deletions(-) diff --git a/test/input_output_handling.jl b/test/input_output_handling.jl index 25434e3dfb..de6fc92b5c 100644 --- a/test/input_output_handling.jl +++ b/test/input_output_handling.jl @@ -153,61 +153,66 @@ if VERSION >= v"1.8" # :opaque_closure not supported before end ## Code generation with unbound inputs +@testset "generate_control_function with disturbance inputs" begin + for split in [true, false] + simplify = true + + @variables x(t)=0 u(t)=0 [input = true] + eqs = [ + D(x) ~ -x + u + ] + + @named sys = ODESystem(eqs, t) + f, dvs, ps, io_sys = ModelingToolkit.generate_control_function(sys; simplify, split) + + @test isequal(dvs[], x) + @test isempty(ps) + + p = nothing + x = [rand()] + u = [rand()] + @test f[1](x, u, p, 1) == -x + u + + # With disturbance inputs + @variables x(t)=0 u(t)=0 [input = true] d(t)=0 + eqs = [ + D(x) ~ -x + u + d^2 + ] + + @named sys = ODESystem(eqs, t) + f, dvs, ps, io_sys = ModelingToolkit.generate_control_function( + sys, [u], [d]; simplify, split) + + @test isequal(dvs[], x) + @test isempty(ps) + + p = nothing + x = [rand()] + u = [rand()] + @test f[1](x, u, p, 1) == -x + u + + ## With added d argument + @variables x(t)=0 u(t)=0 [input = true] d(t)=0 + eqs = [ + D(x) ~ -x + u + d^2 + ] + + @named sys = ODESystem(eqs, t) + f, dvs, ps, io_sys = ModelingToolkit.generate_control_function( + sys, [u], [d]; simplify, split, disturbance_argument = true) + + @test isequal(dvs[], x) + @test isempty(ps) + + p = nothing + x = [rand()] + u = [rand()] + d = [rand()] + @test f[1](x, u, p, t, d) == -x + u + [d[]^2] + end +end -@variables x(t)=0 u(t)=0 [input = true] -eqs = [ - D(x) ~ -x + u -] - -@named sys = ODESystem(eqs, t) -f, dvs, ps, io_sys = ModelingToolkit.generate_control_function(sys, simplify = true) - -@test isequal(dvs[], x) -@test isempty(ps) - -p = nothing -x = [rand()] -u = [rand()] -@test f[1](x, u, p, 1) == -x + u - -# With disturbance inputs -@variables x(t)=0 u(t)=0 [input = true] d(t)=0 -eqs = [ - D(x) ~ -x + u + d^2 -] - -@named sys = ODESystem(eqs, t) -f, dvs, ps, io_sys = ModelingToolkit.generate_control_function( - sys, [u], [d], simplify = true) - -@test isequal(dvs[], x) -@test isempty(ps) - -p = nothing -x = [rand()] -u = [rand()] -@test f[1](x, u, p, 1) == -x + u - -# With added d argument -@variables x(t)=0 u(t)=0 [input = true] d(t)=0 -eqs = [ - D(x) ~ -x + u + d^2 -] - -@named sys = ODESystem(eqs, t) -f, dvs, ps, io_sys = ModelingToolkit.generate_control_function( - sys, [u], [d], simplify = true, disturbance_argument = true) - -@test isequal(dvs[], x) -@test isempty(ps) - -p = nothing -x = [rand()] -u = [rand()] -d = [rand()] -@test f[1](x, u, p, 1, d) == -x + u + [d[]^2] - -# more complicated system +## more complicated system @variables u(t) [input = true] From cb6ca4ce4cfd8547117317d23e5b32558d995a68 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 16 Dec 2024 19:25:25 +0530 Subject: [PATCH 0475/2176] feat: add support for `extra_args` in `wrap_array_vars` --- src/systems/abstractsystem.jl | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index e44f250a7f..6a20ee17dc 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -230,9 +230,33 @@ function wrap_parameter_dependencies(sys::AbstractSystem, isscalar) wrap_assignments(isscalar, [eq.lhs ← eq.rhs for eq in parameter_dependencies(sys)]) end +""" + $(TYPEDSIGNATURES) + +Add the necessary assignment statements to allow use of unscalarized array variables +in the generated code. `expr` is the expression returned by the function. `dvs` and +`ps` are the unknowns and parameters of the system `sys` to use in the generated code. +`inputs` can be specified as an array of symbolics if the generated function has inputs. +If `history == true`, the generated function accepts a history function. `cachesyms` are +extra variables (arrays of variables) stored in the cache array(s) of the parameter +object. `extra_args` are extra arguments appended to the end of the argument list. + +The function is assumed to have the signature `f(du, u, h, x, p, cache_syms..., t, extra_args...)` +Where: +- `du` is the optional buffer to write to for in-place functions. +- `u` is the list of unknowns. This argument is not present if `dvs === nothing`. +- `h` is the optional history function, present if `history == true`. +- `x` is the array of inputs, present only if `inputs !== nothing`. Values are assumed + to be in the order of variables passed to `inputs`. +- `p` is the parameter object. +- `cache_syms` are the cache variables. These are part of the splatted parameter object. +- `t` is time, present only if the system is time dependent. +- `extra_args` are the extra arguments passed to the function, present only if + `extra_args` is non-empty. +""" function wrap_array_vars( sys::AbstractSystem, exprs; dvs = unknowns(sys), ps = parameters(sys), - inputs = nothing, history = false, cachesyms::Tuple = ()) + inputs = nothing, history = false, cachesyms::Tuple = (), extra_args::Tuple = ()) isscalar = !(exprs isa AbstractArray) var_to_arridxs = Dict() @@ -252,6 +276,10 @@ function wrap_array_vars( if inputs !== nothing rps = (inputs, rps...) end + if has_iv(sys) + rps = (rps..., get_iv(sys)) + end + rps = (rps..., extra_args...) for sym in reduce(vcat, rps; init = []) iscall(sym) && operation(sym) == getindex || continue arg = arguments(sym)[1] From 266630dacfa864b79fae6e2eb534b6c4be99001d Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 16 Dec 2024 19:44:06 +0530 Subject: [PATCH 0476/2176] feat: handle additional arguments in `wrap_mtkparameters` --- src/systems/abstractsystem.jl | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index 6a20ee17dc..168260ae69 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -360,7 +360,7 @@ end const MTKPARAMETERS_ARG = Sym{Vector{Vector}}(:___mtkparameters___) """ - wrap_mtkparameters(sys::AbstractSystem, isscalar::Bool, p_start = 2) + wrap_mtkparameters(sys::AbstractSystem, isscalar::Bool, p_start = 2, offset = Int(is_time_dependent(sys))) Return function(s) to be passed to the `wrap_code` keyword of `build_function` which allow the compiled function to be called as `f(u, p, t)` where `p isa MTKParameters` @@ -370,12 +370,14 @@ the first parameter vector in the out-of-place version of the function. For exam if a history function (DDEs) was passed before `p`, then the function before wrapping would have the signature `f(u, h, p..., t)` and hence `p_start` would need to be `3`. +`offset` is the number of arguments at the end of the argument list to ignore. Defaults +to 1 if the system is time-dependent (to ignore `t`) and 0 otherwise. + The returned function is `identity` if the system does not have an `IndexCache`. """ -function wrap_mtkparameters(sys::AbstractSystem, isscalar::Bool, p_start = 2) +function wrap_mtkparameters(sys::AbstractSystem, isscalar::Bool, p_start = 2, + offset = Int(is_time_dependent(sys))) if has_index_cache(sys) && get_index_cache(sys) !== nothing - offset = Int(is_time_dependent(sys)) - if isscalar function (expr) param_args = expr.args[p_start:(end - offset)] From a79f4c2918b309cf4653f5cb110812cddaa214db Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 16 Dec 2024 19:25:42 +0530 Subject: [PATCH 0477/2176] fix: use `extra_args` in `generate_control_function` --- src/inputoutput.jl | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/inputoutput.jl b/src/inputoutput.jl index 1be1fbb8dd..6bdcac6dd4 100644 --- a/src/inputoutput.jl +++ b/src/inputoutput.jl @@ -252,10 +252,11 @@ function generate_control_function(sys::AbstractODESystem, inputs = unbound_inpu process = get_postprocess_fbody(sys) wrapped_arrays_vars = disturbance_argument ? wrap_array_vars( - sys, rhss; dvs, ps, inputs, cachesyms = (disturbance_inputs,)) : + sys, rhss; dvs, ps, inputs, extra_args = (disturbance_inputs,)) : wrap_array_vars(sys, rhss; dvs, ps, inputs) f = build_function(rhss, args...; postprocess_fbody = process, - expression = Val{true}, wrap_code = wrap_mtkparameters(sys, false, 3) .∘ + expression = Val{true}, wrap_code = wrap_mtkparameters( + sys, false, 3, Int(disturbance_argument) + 1) .∘ wrapped_arrays_vars .∘ wrap_parameter_dependencies(sys, false), kwargs...) From 9d65a3345ade530654a64ace2069ff24e8c67875 Mon Sep 17 00:00:00 2001 From: vyudu Date: Mon, 16 Dec 2024 23:43:35 +0800 Subject: [PATCH 0478/2176] fixing create_array --- Project.toml | 3 ++ src/systems/diffeqs/abstractodesystem.jl | 10 ++++- test/bvproblem.jl | 53 +++++++++++++++--------- 3 files changed, 45 insertions(+), 21 deletions(-) diff --git a/Project.toml b/Project.toml index 19063a0342..ab854b80bd 100644 --- a/Project.toml +++ b/Project.toml @@ -7,6 +7,9 @@ version = "9.54.0" AbstractTrees = "1520ce14-60c1-5f80-bbc7-55ef81b5835c" ArrayInterface = "4fba245c-0d91-5ea0-9b3e-6abc04ee57a9" BlockArrays = "8e7c35d0-a365-5155-bbbb-fb81a777f24e" +BoundaryValueDiffEq = "764a87c0-6b3e-53db-9096-fe964310641d" +BoundaryValueDiffEqCore = "56b672f2-a5fe-4263-ab2d-da677488eb3a" +BoundaryValueDiffEqMIRK = "1a22d4ce-7765-49ea-b6f2-13c8438986a6" Combinatorics = "861a8166-3701-5b0c-9a16-15d98fcdc6aa" CommonSolve = "38540f10-b2f7-11e9-35d8-d573e4eb0ff2" Compat = "34da2185-b29b-5c13-b0c7-acf172513d20" diff --git a/src/systems/diffeqs/abstractodesystem.jl b/src/systems/diffeqs/abstractodesystem.jl index 8dac19f296..3a502d3183 100644 --- a/src/systems/diffeqs/abstractodesystem.jl +++ b/src/systems/diffeqs/abstractodesystem.jl @@ -539,11 +539,19 @@ function SciMLBase.BVProblem{iip, specialize}(sys::AbstractODESystem, u0map = [] (u, p, t) -> (u[1] - _u0) end - return BVProblem{iip}(f, bc, u0, tspan, p; kwargs1..., kwargs...) + return BVProblem{iip}(f, bc, _u0, tspan, p; kwargs1..., kwargs...) end get_callback(prob::BVProblem) = error("BVP solvers do not support callbacks.") +@inline function create_array(::Type{Base.ReinterpretArray}, ::Nothing, ::Val{1}, ::Val{dims}, elems...) where dims + [elems...] +end + +@inline function create_array(::Type{Base.ReinterpretArray}, T, ::Val{1}, ::Val{dims}, elems...) where dims + T[elems...] +end + """ ```julia DiffEqBase.DAEFunction{iip}(sys::AbstractODESystem, dvs = unknowns(sys), diff --git a/test/bvproblem.jl b/test/bvproblem.jl index 7787bd9c3e..7cdb7e1837 100644 --- a/test/bvproblem.jl +++ b/test/bvproblem.jl @@ -2,6 +2,8 @@ using BoundaryValueDiffEq, OrdinaryDiffEq using ModelingToolkit using ModelingToolkit: t_nounits as t, D_nounits as D +solvers = [MIRK4, RadauIIa5, LobattoIIIa3] + @parameters α = 7.5 β = 4. γ = 8. δ = 5. @variables x(t) = 1. y(t) = 2. @@ -13,20 +15,26 @@ parammap = [:α => 7.5, :β => 4, :γ => 8., :δ => 5.] tspan = (0., 10.) @mtkbuild lotkavolterra = ODESystem(eqs, t) +op = ODEProblem(lotkavolterra, u0map, tspan, parammap) +osol = solve(op, Vern9()) -bvp = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}(lotkavolterra, u0map, tspan, parammap) -sol = solve(bvp, MIRK4(), dt = 0.01); +bvp = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}(lotkavolterra, u0map, tspan, parammap; eval_expression = true) -bvp2 = SciMLBase.BVProblem{false, SciMLBase.AutoSpecialize}(lotkavolterra, u0map, tspan, parammap) -sol2 = solve(bvp, MIRK4(), dt = 0.01); +for solver in solvers + println("$solver") + sol = solve(bvp, solver(), dt = 0.01) + @test isapprox(sol.u[end], osol.u[end]; atol = 0.01) + @test sol.u[1] == [1., 2.] +end -op = ODEProblem(lotkavolterra, u0map, tspan, parammap) -osol = solve(op, Vern9()) +# Test out of place +bvp2 = SciMLBase.BVProblem{false, SciMLBase.AutoSpecialize}(lotkavolterra, u0map, tspan, parammap; eval_expression = true) -@test isapprox(sol.u[end],osol.u[end]; atol = 0.01) -@test isapprox(sol2.u[end],osol.u[end]; atol = 0.01) -@test sol.u[1] == [1., 2.] -@test sol2.u[1] == [1., 2.] +for solver in solvers + sol = solve(bvp2, solver(), dt = 0.01) + @test isapprox(sol.u[end],osol.u[end]; atol = 0.01) + @test sol.u[1] == [1., 2.] +end ### Testing on pendulum @@ -38,19 +46,24 @@ eqs = [D(D(θ)) ~ -(g / L) * sin(θ)] @mtkbuild pend = ODESystem(eqs, t) u0map = [θ => π/2, D(θ) => π/2] -parammap = [:L => 2., :g => 9.81] +parammap = [:L => 1., :g => 9.81] tspan = (0., 10.) +op = ODEProblem(pend, u0map, tspan, parammap) +osol = solve(op, Vern9()) + bvp = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}(pend, u0map, tspan, parammap) -sol = solve(bvp, MIRK4(), dt = 0.01); +for solver in solvers + sol = solve(bvp2, solver(), dt = 0.01) + @test isapprox(sol.u[end],osol.u[end]; atol = 0.01) + @test sol.u[1] == [π/2, π/2] +end +# Test out-of-place bvp2 = SciMLBase.BVProblem{false, SciMLBase.FullSpecialize}(pend, u0map, tspan, parammap) -sol2 = solve(bvp2, MIRK4(), dt = 0.01); - -op = ODEProblem(pend, u0map, tspan, parammap) -osol = solve(op, Vern9()) -@test isapprox(sol.u[end], osol.u[end]; atol = 0.01) -@test sol.u[1] == [π/2, π/2] -@test isapprox(sol2.u[end], osol.u[end]; atol = 0.01) -@test sol2.u[1] == [π/2, π/2] +for solver in solvers + sol = solve(bvp2, solver(), dt = 0.01) + @test isapprox(sol.u[end],osol.u[end]; atol = 0.01) + @test sol.u[1] == [π/2, π/2] +end From 999ec308d58e32c51941743a6ba5a8f096c56ac2 Mon Sep 17 00:00:00 2001 From: vyudu Date: Mon, 16 Dec 2024 23:44:24 +0800 Subject: [PATCH 0479/2176] revert Project.toml --- Project.toml | 3 --- 1 file changed, 3 deletions(-) diff --git a/Project.toml b/Project.toml index ab854b80bd..19063a0342 100644 --- a/Project.toml +++ b/Project.toml @@ -7,9 +7,6 @@ version = "9.54.0" AbstractTrees = "1520ce14-60c1-5f80-bbc7-55ef81b5835c" ArrayInterface = "4fba245c-0d91-5ea0-9b3e-6abc04ee57a9" BlockArrays = "8e7c35d0-a365-5155-bbbb-fb81a777f24e" -BoundaryValueDiffEq = "764a87c0-6b3e-53db-9096-fe964310641d" -BoundaryValueDiffEqCore = "56b672f2-a5fe-4263-ab2d-da677488eb3a" -BoundaryValueDiffEqMIRK = "1a22d4ce-7765-49ea-b6f2-13c8438986a6" Combinatorics = "861a8166-3701-5b0c-9a16-15d98fcdc6aa" CommonSolve = "38540f10-b2f7-11e9-35d8-d573e4eb0ff2" Compat = "34da2185-b29b-5c13-b0c7-acf172513d20" From 9226ad687ff06cbc1dbbdcaa16ba3df85ef32d4e Mon Sep 17 00:00:00 2001 From: vyudu Date: Mon, 16 Dec 2024 23:56:04 +0800 Subject: [PATCH 0480/2176] Up --- test/bvproblem.jl | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/test/bvproblem.jl b/test/bvproblem.jl index 7cdb7e1837..86e3722eec 100644 --- a/test/bvproblem.jl +++ b/test/bvproblem.jl @@ -21,7 +21,6 @@ osol = solve(op, Vern9()) bvp = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}(lotkavolterra, u0map, tspan, parammap; eval_expression = true) for solver in solvers - println("$solver") sol = solve(bvp, solver(), dt = 0.01) @test isapprox(sol.u[end], osol.u[end]; atol = 0.01) @test sol.u[1] == [1., 2.] @@ -47,15 +46,15 @@ eqs = [D(D(θ)) ~ -(g / L) * sin(θ)] u0map = [θ => π/2, D(θ) => π/2] parammap = [:L => 1., :g => 9.81] -tspan = (0., 10.) +tspan = (0., 6.) op = ODEProblem(pend, u0map, tspan, parammap) osol = solve(op, Vern9()) bvp = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}(pend, u0map, tspan, parammap) for solver in solvers - sol = solve(bvp2, solver(), dt = 0.01) - @test isapprox(sol.u[end],osol.u[end]; atol = 0.01) + sol = solve(bvp, solver(), dt = 0.01) + @test isapprox(sol.u[end], osol.u[end]; atol = 0.01) @test sol.u[1] == [π/2, π/2] end From 67d8164c591b74a9b985ffbcaf1ef248fb0efaaa Mon Sep 17 00:00:00 2001 From: vyudu Date: Tue, 17 Dec 2024 00:09:52 +0800 Subject: [PATCH 0481/2176] formatting --- src/systems/diffeqs/abstractodesystem.jl | 11 ++++--- test/bvproblem.jl | 42 +++++++++++++----------- 2 files changed, 28 insertions(+), 25 deletions(-) diff --git a/src/systems/diffeqs/abstractodesystem.jl b/src/systems/diffeqs/abstractodesystem.jl index 87ee6e823d..06c83073bf 100644 --- a/src/systems/diffeqs/abstractodesystem.jl +++ b/src/systems/diffeqs/abstractodesystem.jl @@ -512,7 +512,6 @@ function SciMLBase.BVProblem{iip, specialize}(sys::AbstractODESystem, u0map = [] eval_expression = false, eval_module = @__MODULE__, kwargs...) where {iip, specialize} - if !iscomplete(sys) error("A completed system is required. Call `complete` or `structural_simplify` on the system before creating an `BVProblem`") end @@ -528,12 +527,12 @@ function SciMLBase.BVProblem{iip, specialize}(sys::AbstractODESystem, u0map = [] if cbs !== nothing kwargs1 = merge(kwargs1, (callback = cbs,)) end - + # Construct initial conditions. _u0 = u0 isa Function ? u0(tspan[1]) : u0 # Define the boundary conditions. - bc = if iip + bc = if iip (residual, u, p, t) -> (residual .= u[1] .- _u0) else (u, p, t) -> (u[1] - _u0) @@ -544,11 +543,13 @@ end get_callback(prob::BVProblem) = error("BVP solvers do not support callbacks.") -@inline function create_array(::Type{Base.ReinterpretArray}, ::Nothing, ::Val{1}, ::Val{dims}, elems...) where dims +@inline function create_array(::Type{Base.ReinterpretArray}, ::Nothing, + ::Val{1}, ::Val{dims}, elems...) where {dims} [elems...] end -@inline function create_array(::Type{Base.ReinterpretArray}, T, ::Val{1}, ::Val{dims}, elems...) where dims +@inline function create_array( + ::Type{Base.ReinterpretArray}, T, ::Val{1}, ::Val{dims}, elems...) where {dims} T[elems...] end diff --git a/test/bvproblem.jl b/test/bvproblem.jl index 86e3722eec..1072874917 100644 --- a/test/bvproblem.jl +++ b/test/bvproblem.jl @@ -4,49 +4,51 @@ using ModelingToolkit: t_nounits as t, D_nounits as D solvers = [MIRK4, RadauIIa5, LobattoIIIa3] -@parameters α = 7.5 β = 4. γ = 8. δ = 5. -@variables x(t) = 1. y(t) = 2. +@parameters α=7.5 β=4.0 γ=8.0 δ=5.0 +@variables x(t)=1.0 y(t)=2.0 -eqs = [D(x) ~ α*x - β*x*y, - D(y) ~ -γ*y + δ*x*y] +eqs = [D(x) ~ α * x - β * x * y, + D(y) ~ -γ * y + δ * x * y] -u0map = [:x => 1., :y => 2.] -parammap = [:α => 7.5, :β => 4, :γ => 8., :δ => 5.] -tspan = (0., 10.) +u0map = [:x => 1.0, :y => 2.0] +parammap = [:α => 7.5, :β => 4, :γ => 8.0, :δ => 5.0] +tspan = (0.0, 10.0) @mtkbuild lotkavolterra = ODESystem(eqs, t) op = ODEProblem(lotkavolterra, u0map, tspan, parammap) osol = solve(op, Vern9()) -bvp = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}(lotkavolterra, u0map, tspan, parammap; eval_expression = true) +bvp = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}( + lotkavolterra, u0map, tspan, parammap; eval_expression = true) for solver in solvers sol = solve(bvp, solver(), dt = 0.01) @test isapprox(sol.u[end], osol.u[end]; atol = 0.01) - @test sol.u[1] == [1., 2.] + @test sol.u[1] == [1.0, 2.0] end # Test out of place -bvp2 = SciMLBase.BVProblem{false, SciMLBase.AutoSpecialize}(lotkavolterra, u0map, tspan, parammap; eval_expression = true) +bvp2 = SciMLBase.BVProblem{false, SciMLBase.AutoSpecialize}( + lotkavolterra, u0map, tspan, parammap; eval_expression = true) for solver in solvers sol = solve(bvp2, solver(), dt = 0.01) - @test isapprox(sol.u[end],osol.u[end]; atol = 0.01) - @test sol.u[1] == [1., 2.] + @test isapprox(sol.u[end], osol.u[end]; atol = 0.01) + @test sol.u[1] == [1.0, 2.0] end ### Testing on pendulum -@parameters g = 9.81 L = 1. -@variables θ(t) = π/2 +@parameters g=9.81 L=1.0 +@variables θ(t) = π / 2 eqs = [D(D(θ)) ~ -(g / L) * sin(θ)] @mtkbuild pend = ODESystem(eqs, t) -u0map = [θ => π/2, D(θ) => π/2] -parammap = [:L => 1., :g => 9.81] -tspan = (0., 6.) +u0map = [θ => π / 2, D(θ) => π / 2] +parammap = [:L => 1.0, :g => 9.81] +tspan = (0.0, 6.0) op = ODEProblem(pend, u0map, tspan, parammap) osol = solve(op, Vern9()) @@ -55,7 +57,7 @@ bvp = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}(pend, u0map, tspan, pa for solver in solvers sol = solve(bvp, solver(), dt = 0.01) @test isapprox(sol.u[end], osol.u[end]; atol = 0.01) - @test sol.u[1] == [π/2, π/2] + @test sol.u[1] == [π / 2, π / 2] end # Test out-of-place @@ -63,6 +65,6 @@ bvp2 = SciMLBase.BVProblem{false, SciMLBase.FullSpecialize}(pend, u0map, tspan, for solver in solvers sol = solve(bvp2, solver(), dt = 0.01) - @test isapprox(sol.u[end],osol.u[end]; atol = 0.01) - @test sol.u[1] == [π/2, π/2] + @test isapprox(sol.u[end], osol.u[end]; atol = 0.01) + @test sol.u[1] == [π / 2, π / 2] end From 25988f3bc6b6d66b008b6a40341f75e4547e574a Mon Sep 17 00:00:00 2001 From: vyudu Date: Tue, 17 Dec 2024 10:52:37 +0800 Subject: [PATCH 0482/2176] up --- src/systems/diffeqs/abstractodesystem.jl | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/systems/diffeqs/abstractodesystem.jl b/src/systems/diffeqs/abstractodesystem.jl index 06c83073bf..c84f5ff5be 100644 --- a/src/systems/diffeqs/abstractodesystem.jl +++ b/src/systems/diffeqs/abstractodesystem.jl @@ -543,13 +543,13 @@ end get_callback(prob::BVProblem) = error("BVP solvers do not support callbacks.") -@inline function create_array(::Type{Base.ReinterpretArray}, ::Nothing, +@inline function SciML.Code.create_array(::Type{<:Base.ReinterpretArray}, ::Nothing, ::Val{1}, ::Val{dims}, elems...) where {dims} [elems...] end -@inline function create_array( - ::Type{Base.ReinterpretArray}, T, ::Val{1}, ::Val{dims}, elems...) where {dims} +@inline function SciML.Code.create_array( + ::Type{<:Base.ReinterpretArray}, T, ::Val{1}, ::Val{dims}, elems...) where {dims} T[elems...] end From bb28d4fe2a0d753221104186fe42808c7657f5d6 Mon Sep 17 00:00:00 2001 From: vyudu Date: Tue, 17 Dec 2024 10:53:22 +0800 Subject: [PATCH 0483/2176] up --- src/systems/diffeqs/abstractodesystem.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/systems/diffeqs/abstractodesystem.jl b/src/systems/diffeqs/abstractodesystem.jl index c84f5ff5be..eac2df16aa 100644 --- a/src/systems/diffeqs/abstractodesystem.jl +++ b/src/systems/diffeqs/abstractodesystem.jl @@ -543,12 +543,12 @@ end get_callback(prob::BVProblem) = error("BVP solvers do not support callbacks.") -@inline function SciML.Code.create_array(::Type{<:Base.ReinterpretArray}, ::Nothing, +@inline function SciMLBase.Code.create_array(::Type{<:Base.ReinterpretArray}, ::Nothing, ::Val{1}, ::Val{dims}, elems...) where {dims} [elems...] end -@inline function SciML.Code.create_array( +@inline function SciMLBase.Code.create_array( ::Type{<:Base.ReinterpretArray}, T, ::Val{1}, ::Val{dims}, elems...) where {dims} T[elems...] end From b2bf7c05532bdf68d93e566d07c5e7444473dd7a Mon Sep 17 00:00:00 2001 From: vyudu Date: Tue, 17 Dec 2024 11:00:30 +0800 Subject: [PATCH 0484/2176] fix --- src/systems/diffeqs/abstractodesystem.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/systems/diffeqs/abstractodesystem.jl b/src/systems/diffeqs/abstractodesystem.jl index eac2df16aa..3d143d49fb 100644 --- a/src/systems/diffeqs/abstractodesystem.jl +++ b/src/systems/diffeqs/abstractodesystem.jl @@ -543,12 +543,12 @@ end get_callback(prob::BVProblem) = error("BVP solvers do not support callbacks.") -@inline function SciMLBase.Code.create_array(::Type{<:Base.ReinterpretArray}, ::Nothing, +@inline function SymbolicUtils.Code.create_array(::Type{<:Base.ReinterpretArray}, ::Nothing, ::Val{1}, ::Val{dims}, elems...) where {dims} [elems...] end -@inline function SciMLBase.Code.create_array( +@inline function SymbolicUtils.Code.create_array( ::Type{<:Base.ReinterpretArray}, T, ::Val{1}, ::Val{dims}, elems...) where {dims} T[elems...] end From 21a2a33aa6d505da57c30c8e8326b40201106aae Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 13 Dec 2024 18:21:18 +0530 Subject: [PATCH 0485/2176] feat: allow passing pre-computed `vars` to `observed_equations_used_by` --- src/utils.jl | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/utils.jl b/src/utils.jl index dd7113cbe6..e9ddad3a07 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -1033,19 +1033,24 @@ end $(TYPEDSIGNATURES) Return the indexes of observed equations of `sys` used by expression `exprs`. + +Keyword arguments: +- `involved_vars`: A collection of the variables involved in `exprs`. This is the set of + variables which will be explored to find dependencies on observed equations. Typically, + providing this keyword is not necessary and is only useful to avoid repeatedly calling + `vars(exprs)` """ -function observed_equations_used_by(sys::AbstractSystem, exprs) +function observed_equations_used_by(sys::AbstractSystem, exprs; involved_vars = vars(exprs)) obs = observed(sys) obsvars = getproperty.(obs, :lhs) graph = observed_dependency_graph(obs) - syms = vars(exprs) - obsidxs = BitSet() - for sym in syms + for sym in involved_vars idx = findfirst(isequal(sym), obsvars) idx === nothing && continue + idx in obsidxs && continue parents = dfs_parents(graph, idx) for i in eachindex(parents) parents[i] == 0 && continue From 7deb72ba09091a1324c8efb75dc8b55a5c06906e Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 13 Dec 2024 18:21:33 +0530 Subject: [PATCH 0486/2176] feat: implement `IfLifting` structural simplification pass Co-authored-by: Benjamin Chung --- src/ModelingToolkit.jl | 1 + src/systems/if_lifting.jl | 511 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 512 insertions(+) create mode 100644 src/systems/if_lifting.jl diff --git a/src/ModelingToolkit.jl b/src/ModelingToolkit.jl index 20b2ada8fa..10aba4d8a9 100644 --- a/src/ModelingToolkit.jl +++ b/src/ModelingToolkit.jl @@ -181,6 +181,7 @@ include("discretedomain.jl") include("systems/systemstructure.jl") include("systems/clock_inference.jl") include("systems/systems.jl") +include("systems/if_lifting.jl") include("debugging.jl") include("systems/alias_elimination.jl") diff --git a/src/systems/if_lifting.jl b/src/systems/if_lifting.jl new file mode 100644 index 0000000000..53e1ca4957 --- /dev/null +++ b/src/systems/if_lifting.jl @@ -0,0 +1,511 @@ +""" + struct CondRewriter + +Callable struct used to transform symbolic conditions into conditions involving discrete +variables. +""" +struct CondRewriter + """ + The independent variable which the discrete variables depend on. + """ + iv::BasicSymbolic + """ + A mapping from a discrete variables to a `NamedTuple` containing the condition + determining whether the discrete variable needs to be evaluated and the symbolic + expression the discrete variable represents. The expression is used as a rootfinding + function, and zero-crossings trigger re-evaluation of the condition (if `dependency` + is `true`). `expression < 0` is evaluated on an up-crossing and `expression <= 0` is + evaluated on a down-crossing to get the updated value of the condition variable. + """ + conditions::Dict{Any, @NamedTuple{dependency, expression}} +end + +function CondRewriter(iv) + return CondRewriter(iv, Dict()) +end + +""" + $(TYPEDSIGNATURES) + +Given a symbolic condition `expr` and the condition `dep` it depends on, update the +mapping in `cw` and generate a new discrete variable if necessary. +""" +function new_cond_sym(cw::CondRewriter, expr, dep) + if !iscall(expr) || operation(expr) != Base.:(<) || !iszero(arguments(expr)[2]) + throw(ArgumentError("`expr` passed to `new_cond_sym` must be of the form `f(args...) < 0`. Got $expr.")) + end + # check if the same expression exists in the mapping + existing_var = findfirst(p -> isequal(p.expression, expr), cw.conditions) + if existing_var !== nothing + # cache hit + (existing_dep, _) = cw.conditions[existing_var] + # update the dependency condition + cw.conditions[existing_var] = (dependency = (dep | existing_dep), expression = expr) + return existing_var + end + # generate a new condition variable + cvar = gensym("cond") + st = symtype(expr) + iv = cw.iv + cv = unwrap(first(@parameters $(cvar)(iv)::st = true)) # TODO: real init + cw.conditions[cv] = (dependency = dep, expression = expr) + return cv +end + +""" +Utility function for boolean implication. +""" +implies(a, b) = !a & b + +""" + $(TYPEDSIGNATURES) + +Recursively rewrite conditions into discrete variables. `expr` is the condition to rewrite, +`dep` is a boolean expression/value which determines when the `expr` is to be evaluated. For +example, if `expr = expr1 | expr2` and `dep = dep1`, then `expr` should only be evaluated if +`dep1` evaluates to `true`. Recursively, `expr1` should only be evaluated if `dep1` is `true`, +and `expr2` should only be evaluated if `dep & !expr1`. + +Returns a 3-tuple of the substituted expression, a condition describing when `expr` evaluates +to `true`, and a condition describing when `expr` evaluates to `false`. + +This expects that all expressions with discontinuities or with discontinuous derivatives have +been rewritten into the form of `ifelse(rootfunc(args...) < 0, left(args...), right(args...))`. +The transformation is performed via `discontinuities_to_ifelse` using `Symbolics.rootfunction` +and family. +""" +function (cw::CondRewriter)(expr, dep) + # single variable, trivial case + if issym(expr) || iscall(expr) && issym(operation(expr)) + return (expr, expr, !expr) + # literal boolean or integer + elseif expr isa Bool + return (expr, expr, !expr) + elseif expr isa Int + return (expr, true, true) + # other singleton symbolic variables + elseif !iscall(expr) + @warn "Automatic conversion of if statments to events requires use of a limited conditional grammar; see the documentation. Skipping due to $expr" + return (expr, true, true) # error case => conservative assumption is that both true and false have to be evaluated + elseif operation(expr) == Base.:(|) # OR of two conditions + a, b = arguments(expr) + (rw_conda, truea, falsea) = cw(a, dep) + # only evaluate second if first is false + (rw_condb, trueb, falseb) = cw(b, dep & falsea) + return (rw_conda | rw_condb, truea | trueb, falsea & falseb) + + elseif operation(expr) == Base.:(&) # AND of two conditions + a, b = arguments(expr) + (rw_conda, truea, falsea) = cw(a, dep) + # only evaluate second if first is true + (rw_condb, trueb, falseb) = cw(b, dep & truea) + return (rw_conda & rw_condb, truea & trueb, falsea | falseb) + elseif operation(expr) == ifelse + c, a, b = arguments(expr) + (rw_cond, ctrue, cfalse) = cw(c, dep) + # only evaluate if condition is true + (rw_conda, truea, falsea) = cw(a, dep & ctrue) + # only evaluate if condition is false + (rw_condb, trueb, falseb) = cw(b, dep & cfalse) + # expression is true if condition is true and THEN branch is true, or condition is false + # and ELSE branch is true + # similarly for expression being false + return (ifelse(rw_cond, rw_conda, rw_condb), + implies(ctrue, truea) | implies(cfalse, trueb), + implies(ctrue, falsea) | implies(cfalse, falseb)) + elseif operation(expr) == Base.:(!) # NOT of expression + (a,) = arguments(expr) + (rw, ctrue, cfalse) = cw(a, dep) + return (!rw, cfalse, ctrue) + elseif operation(expr) == Base.:(<) + if !isequal(arguments(expr)[2], 0) + throw(ArgumentError("Expected comparison to be written as `f(args...) < 0`. Found $expr.")) + end + + # if the comparison does not include time-dependent variables, + # don't create a callback for it + + # Calling `expression_is_time_dependent` is `O(d)` where `d` is the depth of the + # expression tree. We only call this in this here to avoid turning this into + # an `O(d^2)` time complexity recursion, which would happen if it were called + # at the beginning of the function. Now, it only happens near the leaves of + # the recursive tree. + if !expression_is_time_dependent(expr, cw.iv) + return (expr, expr, !expr) + end + cv = new_cond_sym(cw, expr, dep) + return (cv, cv, !cv) + elseif operation(expr) == (==) + # we don't touch equality since it's a point discontinuity. It's basically always + # false for continuous variables. In case it's an equality between discrete + # quantities, we don't need to transform it. + return (expr, expr, !expr) + elseif !expression_is_time_dependent(expr, cw.iv) + return (expr, expr, !expr) + end + error(""" + Unsupported expression form in decision variable computation $expr. If the expression + involves a registered function, declare the discontinuity using + `Symbolics.@register_discontinuity`. If this is not meant to be transformed via + `IfLifting`, wrap the parent expression in `ModelingToolkit.no_if_lift`. + """) +end + +""" + $(TYPEDSIGNATURES) + +Acts as the identity function, and prevents transformation of conditional expressions inside it. Useful +if specific `ifelse` or other functions with discontinuous derivatives shouldn't be transformed into +callbacks. +""" +no_if_lift(s) = s +@register_symbolic no_if_lift(s) + +""" + $(TYPEDEF) + +A utility struct to search through an expression specifically for `ifelse` terms, and find +all variables used in the condition of such terms. The variables are stored in a field of +the struct. +""" +struct VarsUsedInCondition + """ + Stores variables used in conditions of `ifelse` statements in the expression. + """ + vars::Set{Any} +end + +VarsUsedInCondition() = VarsUsedInCondition(Set()) + +function (v::VarsUsedInCondition)(expr) + expr = Symbolics.unwrap(expr) + if symbolic_type(expr) == NotSymbolic() + is_array_of_symbolics(expr) || return + foreach(v, expr) + return + end + iscall(expr) || return + op = operation(expr) + + # do not search inside no_if_lift to avoid discovering + # redundant variables + op == no_if_lift && return + + args = arguments(expr) + if op == ifelse + cond, branch_a, branch_b = arguments(expr) + vars!(v.vars, cond) + v(branch_a) + v(branch_b) + end + foreach(v, args) + return +end + +""" + $(TYPEDSIGNATURES) + +Check if `expr` depends on the independent variable `iv`. Return `true` if `iv` is present +in the expression, `Differential(iv)` is in the expression, or a dependent variable such +as `@variables x(iv)` is in the expression. +""" +function expression_is_time_dependent(expr, iv) + any(vars(expr)) do sym + sym = unwrap(sym) + isequal(sym, iv) && return true + iscall(sym) || return false + op = operation(sym) + args = arguments(sym) + op isa Differential && op == Differential(iv) || + issym(op) && length(args) == 1 && expression_is_time_dependent(args[1], iv) + end +end + +""" + $(TYPEDSIGNATURES) + +Given an expression `expr` which is to be evaluated if `dep` evaluates to `true`, transform +the conditions of all all `ifelse` statements in `expr` into functions of new discrete +variables. `cw` is used to store the information relevant to these newly introduced variables. +""" +function rewrite_ifs(cw::CondRewriter, expr, dep) + expr = unwrap(expr) + if symbolic_type(expr) == NotSymbolic() + # non-symbolic expression might still be an array of symbolic expressions + is_array_of_symbolics(expr) || return expr + return map(ex -> rewrite_ifs(cw, ex, dep), expr) + end + + iscall(expr) || return expr + op = operation(expr) + args = arguments(expr) + # do not search into `no_if_lift` + op == no_if_lift && return expr + + # transform `ifelse` + if op == ifelse + cond, iftrue, iffalse = args + + (rw_cond, deptrue, depfalse) = cw(cond, dep) + rw_iftrue = rewrite_ifs(cw, iftrue, deptrue) + rw_iffalse = rewrite_ifs(cw, iffalse, depfalse) + return maketerm( + typeof(expr), ifelse, [unwrap(rw_cond), rw_iftrue, rw_iffalse], metadata(expr)) + end + + # recurse into the rest of the cases + args = map(ex -> rewrite_ifs(cw, ex, dep), args) + return maketerm(typeof(expr), op, args, metadata(expr)) +end + +""" + $(TYPEDSIGNATURES) + +Return a modified `expr` where functions with known discontinuities or discontinuous +derivatives are transformed into `ifelse` statements. Utilizes the discontinuity API +in Symbolics. See [`Symbolics.rootfunction`](@ref), +[`Symbolics.left_continuous_function`](@ref), [`Symbolics.right_continuous_function`](@ref). + +`iv` is the independent variable of the system. Only subexpressions of `expr` which +depend on `iv` are transformed. +""" +function discontinuities_to_ifelse(expr, iv) + expr = unwrap(expr) + if symbolic_type(expr) == NotSymbolic() + # non-symbolic expression might still be an array of symbolic expressions + is_array_of_symbolics(expr) || return expr + return map(ex -> discontinuities_to_ifelse(ex, iv), expr) + end + + iscall(expr) || return expr + op = operation(expr) + args = arguments(expr) + # do not search into `no_if_lift` + op == no_if_lift && return expr + + # Case I: the operation is symbolic. + # We don't actually care if this is a callable parameter or not. + # If it is, we want to search inside and perform if-lifting there. + # If it isn't, either it's `x(t)` in which case this recursion is + # effectively a no-op OR it's `x(f(t))` for DDEs and we want to + # perform if-lifting inside. + # + # Case II: the operation is not symbolic. + # We anyway want to recursively apply the transformation. + # + # Thus, we can do this here regardless of the subsequent checks + args = map(ex -> discontinuities_to_ifelse(ex, iv), args) + + # if the operation is a known discontinuity + if hasmethod(Symbolics.rootfunction, Tuple{typeof(op)}) + rootfn = Symbolics.rootfunction(op) + leftfn = Symbolics.left_continuous_function(op) + rightfn = Symbolics.right_continuous_function(op) + rootexpr = rootfn(args...) < 0 + leftexpr = leftfn(args...) + rightexpr = rightfn(args...) + return maketerm( + typeof(expr), ifelse, [rootexpr, leftexpr, rightexpr], metadata(expr)) + end + + return maketerm(typeof(expr), op, args, metadata(expr)) +end + +""" + $(TYPEDSIGNATURES) + +Generate the symbolic condition for discrete variable `sym`, which represents the condition +of an `ifelse` statement created through [`IfLifting`](@ref). This condition is used to +trigger a callback which updates the value of the condition appropriately. +""" +function generate_condition(cw::CondRewriter, sym) + (dep, expr) = cw.conditions[sym] + + # expr is `f(args...) < 0`, `f(args...)` is the zero-crossing expression + zero_crossing = arguments(expr)[1] + + # if we're meant to evaluate the condition, evaluate it. Otherwise, return `NaN`. + # the solvers don't treat the transition from a number to NaN or back as a zero-crossing, + # so it can be used to effectively disable the affect when the condition is not meant to + # be evaluated. + return ifelse(dep, zero_crossing, NaN) ~ 0 +end + +""" + $(TYPEDSIGNATURES) + +Generate the upcrossing and downcrossing affect functions for discrete variable `sym` involved +in `ifelse` statements that are lifted to callbacks using [`IfLifting`](@ref). `syms` is a +condition variable introduced by `cw`, and is thus a key in `cw.conditions`. `new_cond_vars` +is the list of all such new condition variables, corresponding to the order of vertices in +`new_cond_vars_graph`. `new_cond_vars_graph` is a directed graph where edges denote the +condition variables involved in the dependency expression of the source vertex. +""" +function generate_affects(cw::CondRewriter, sym, new_cond_vars, new_cond_vars_graph) + sym_idx = findfirst(isequal(sym), new_cond_vars) + if sym_idx === nothing + throw(ArgumentError("Expected variable $sym to be a condition variable in $new_cond_vars.")) + end + # use reverse direction of edges because instead of finding the variables it depends + # on, we want the variables that depend on it + parents = bfs_parents(new_cond_vars_graph, sym_idx; dir = :in) + cond_vars_to_update = [new_cond_vars[i] + for i in eachindex(parents) if !iszero(parents[i])] + update_syms = Symbol.(cond_vars_to_update) + modified = NamedTuple{(update_syms...,)}(cond_vars_to_update) + + upcrossing_update_exprs = [arguments(last(cw.conditions[sym]))[1] < 0 + for sym in cond_vars_to_update] + upcrossing = ImperativeAffect( + modified, observed = NamedTuple{(update_syms...,)}(upcrossing_update_exprs), + skip_checks = true) do x, o, c, i + return o + end + downcrossing_update_exprs = [arguments(last(cw.conditions[sym]))[1] <= 0 + for sym in cond_vars_to_update] + downcrossing = ImperativeAffect( + modified, observed = NamedTuple{(update_syms...,)}(downcrossing_update_exprs), + skip_checks = true) do x, o, c, i + return o + end + + return upcrossing, downcrossing +end + +const CONDITION_SIMPLIFIER = Rewriters.Fixpoint(Rewriters.Postwalk(Rewriters.Chain([ + # simple boolean laws + (@rule (!!(~x)) => (~x)) + (@rule ((~x) & true) => (~x)) + (@rule ((~x) & false) => false) + (@rule ((~x) | true) => true) + (@rule ((~x) | false) => (~x)) + (@rule ((~x) & !(~x)) => false) + (@rule ((~x) | !(~x)) => true) + # reversed order of the above, because it matters and `@acrule` refuses to do its job + (@rule (true & (~x)) => (~x)) + (@rule (false & (~x)) => false) + (@rule (true | (~x)) => true) + (@rule (false | (~x)) => (~x)) + (@rule (!(~x) & (~x)) => false) + (@rule (!(~x) | (~x)) => true) + # idempotent + (@rule ((~x) & (~x)) => (~x)) + (@rule ((~x) | (~x)) => (~x)) + # ifelse with determined branches + (@rule ifelse((~x), true, false) => (~x)) + (@rule ifelse((~x), false, true) => !(~x)) + # ifelse with identical branches + (@rule ifelse((~x), (~y), (~y)) => (~y)) + (@rule ifelse((~x), (~y), !(~y)) => ((~x) & + (~y))) + (@rule ifelse((~x), !(~y), (~y)) => ((~x) & + !(~y))) + # ifelse with determined condition + (@rule ifelse(true, (~x), (~y)) => (~x)) + (@rule ifelse(false, (~x), (~y)) => (~y))]))) + +""" +If lifting converts (nested) if statements into a series of continous events + a logically equivalent if statement + parameters. + +Lifting proceeds through the following process: +* rewrite comparisons to be of the form eqn [op] 0; subtract the RHS from the LHS +* replace comparisons with generated parameters; for each comparison eqn [op] 0, generate an event (dependent on op) that sets the parameter +""" +function IfLifting(sys::ODESystem) + cw = CondRewriter(get_iv(sys)) + + eqs = copy(equations(sys)) + obs = copy(observed(sys)) + + # get variables used by `eqs` + syms = vars(eqs) + # get observed equations used by `eqs` + obs_idxs = observed_equations_used_by(sys, eqs; involved_vars = syms) + # and the variables used in those equations + for i in obs_idxs + vars!(syms, obs[i]) + end + + # get all integral variables used in conditions + # this is used when performing the transformation on observed equations + # since they are transformed differently depending on whether they are + # discrete variables involved in a condition or not + condition_vars = Set() + # searcher struct + # we can use the same one since it avoids iterating over duplicates + vars_in_condition! = VarsUsedInCondition() + for i in eachindex(eqs) + eq = eqs[i] + vars_in_condition!(eq.rhs) + # also transform the equation + eqs[i] = eq.lhs ~ rewrite_ifs(cw, discontinuities_to_ifelse(eq.rhs, cw.iv), true) + end + # also search through relevant observed equations + for i in obs_idxs + vars_in_condition!(obs[i].rhs) + end + # add to `condition_vars` after filtering out differential, parameter, independent and + # non-integral variables + for v in vars_in_condition!.vars + v = unwrap(v) + stype = symtype(v) + if isdifferential(v) || isparameter(v) || isequal(v, get_iv(sys)) + continue + end + stype <: Union{Integer, AbstractArray{Integer}} && push!(condition_vars, v) + end + # transform observed equations + for i in obs_idxs + obs[i] = if obs[i].lhs in condition_vars + obs[i].lhs ~ first(cw(discontinuities_to_ifelse(obs[i].rhs, cw.iv), true)) + else + obs[i].lhs ~ rewrite_ifs(cw, discontinuities_to_ifelse(obs[i].rhs, cw.iv), true) + end + end + + # `rewrite_ifs` and calling `cw` generate a lot of redundant code, simplify it + eqs = map(eqs) do eq + eq.lhs ~ CONDITION_SIMPLIFIER(eq.rhs) + end + obs = map(obs) do eq + eq.lhs ~ CONDITION_SIMPLIFIER(eq.rhs) + end + # also simplify dependencies + for (k, v) in cw.conditions + cw.conditions[k] = map(CONDITION_SIMPLIFIER ∘ unwrap, v) + end + + # get directed graph where nodes are the new condition variables and edges from each + # node denote the condition variables used in it's dependency expression + + # so we have an ordering for the vertices + new_cond_vars = collect(keys(cw.conditions)) + # "observed" equations + new_cond_dep_eqs = [v ~ cw.conditions[v] for v in new_cond_vars] + # construct the graph as a `DiCMOBiGraph` + new_cond_vars_graph = observed_dependency_graph(new_cond_dep_eqs) + + new_callbacks = continuous_events(sys) + new_defaults = defaults(sys) + new_ps = Vector{SymbolicParam}(parameters(sys)) + + for var in new_cond_vars + condition = generate_condition(cw, var) + up_affect, down_affect = generate_affects( + cw, var, new_cond_vars, new_cond_vars_graph) + cb = SymbolicContinuousCallback([condition], up_affect; affect_neg = down_affect, + initialize = up_affect, rootfind = SciMLBase.RightRootFind) + + push!(new_callbacks, cb) + new_defaults[var] = getdefault(var) + push!(new_ps, var) + end + + @set! sys.defaults = new_defaults + @set! sys.eqs = eqs + # do not need to topsort because we didn't modify the order + @set! sys.observed = obs + @set! sys.continuous_events = new_callbacks + @set! sys.ps = new_ps + return sys +end From 838ad8034b83bd7e935b64c31e034ecd94bd0fff Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 17 Dec 2024 22:15:26 +0530 Subject: [PATCH 0487/2176] test: add tests for if-lifting --- test/if_lifting.jl | 110 +++++++++++++++++++++++++++++++++++++++++++++ test/runtests.jl | 1 + 2 files changed, 111 insertions(+) create mode 100644 test/if_lifting.jl diff --git a/test/if_lifting.jl b/test/if_lifting.jl new file mode 100644 index 0000000000..d702355506 --- /dev/null +++ b/test/if_lifting.jl @@ -0,0 +1,110 @@ +using ModelingToolkit, OrdinaryDiffEq +using ModelingToolkit: t_nounits as t, D_nounits as D, IfLifting, no_if_lift + +@testset "Simple `abs(x)`" begin + @mtkmodel SimpleAbs begin + @variables begin + x(t) + y(t) + end + @equations begin + D(x) ~ abs(y) + y ~ sin(t) + end + end + @named sys = SimpleAbs() + ss1 = structural_simplify(sys) + @test length(equations(ss1)) == 1 + ss2 = structural_simplify(sys, additional_passes = [IfLifting]) + @test length(equations(ss2)) == 1 + @test length(parameters(ss2)) == 1 + @test operation(only(equations(ss2)).rhs) === ifelse + + discvar = only(parameters(ss2)) + prob2 = ODEProblem(ss2, [x => 0.0], (0.0, 5.0)) + sol2 = solve(prob2, Tsit5()) + @test count(isapprox(pi), sol2.t) == 2 + @test any(isapprox(pi), sol2.discretes[1].t) + @test !sol2[discvar][1] + @test sol2[discvar][end] + + _t = pi + 1.0 + # x(t) = 1 - cos(t) in [0, pi) + # x(t) = 3 + cos(t) in [pi, 2pi) + _trueval = 3 + cos(_t) + @test !isapprox(sol1(_t)[1], _trueval; rtol = 1e-3) + @test isapprox(sol2(_t)[1], _trueval; rtol = 1e-3) +end + +@testset "Big test case" begin + @mtkmodel BigModel begin + @variables begin + x(t) + y(t) + z(t) + c(t)::Bool + w(t) + q(t) + r(t) + end + @parameters begin + p + end + @equations begin + # ifelse, max, min + D(x) ~ ifelse(c, max(x, y), min(x, y)) + # discrete observed + c ~ x <= y + # observed should also get if-lifting + y ~ abs(sin(t)) + # should be ignored + D(z) ~ no_if_lift(ifelse(x < y, x, y)) + # ignore time-independent ifelse + D(w) ~ ifelse(p < 3, 1.0, 2.0) + # all the boolean operators + D(q) ~ ifelse((x < 1) & ((y < 0.5) | ifelse(y > 0.8, c, !c)), 1.0, 2.0) + # don't touch time-independent condition, but modify time-dependent branches + D(r) ~ ifelse(p < 2, abs(x), max(y, 0.9)) + end + end + + @named sys = BigModel() + ss = structural_simplify(sys, additional_passes = [IfLifting]) + + ps = parameters(ss) + @test length(ps) == 9 + eqs = equations(ss) + obs = observed(ss) + + @testset "no_if_lift is untouched" begin + idx = findfirst(eq -> isequal(eq.lhs, D(ss.z)), eqs) + eq = eqs[idx] + @test isequal(eq.rhs, no_if_lift(ifelse(ss.x < ss.y, ss.x, ss.y))) + end + @testset "time-independent ifelse is untouched" begin + idx = findfirst(eq -> isequal(eq.lhs, D(ss.w)), eqs) + eq = eqs[idx] + @test operation(arguments(eq.rhs)[1]) === Base.:< + end + @testset "time-dependent branch of time-independent condition is modified" begin + idx = findfirst(eq -> isequal(eq.lhs, D(ss.r)), eqs) + eq = eqs[idx] + @test operation(eq.rhs) === ifelse + args = arguments(eq.rhs) + @test operation(args[1]) == Base.:< + @test operation(args[2]) === ifelse + condvars = ModelingToolkit.vars(arguments(args[2])[1]) + @test length(condvars) == 1 && any(isequal(only(condvars)), ps) + @test operation(args[3]) === ifelse + condvars = ModelingToolkit.vars(arguments(args[3])[1]) + @test length(condvars) == 1 && any(isequal(only(condvars)), ps) + end + @testset "Observed variables are modified" begin + idx = findfirst(eq -> isequal(eq.lhs, ss.c), obs) + eq = obs[idx] + @test operation(eq.rhs) === Base.:! && any(isequal(only(arguments(eq.rhs))), ps) + idx = findfirst(eq -> isequal(eq.lhs, ss.y), obs) + eq = obs[idx] + @test operation(eq.rhs) === ifelse + end +end diff --git a/test/runtests.jl b/test/runtests.jl index 677f40c717..4a2ec80e6a 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -83,6 +83,7 @@ end @safetestset "JumpSystem Test" include("jumpsystem.jl") @safetestset "print_tree" include("print_tree.jl") @safetestset "Constraints Test" include("constraints.jl") + @safetestset "IfLifting Test" include("if_lifting.jl") end end From e4d5710e74edb0e20b11190f16fcde79d8df97ed Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 18 Dec 2024 11:32:27 +0530 Subject: [PATCH 0488/2176] fix: retain system metadata when calling `flatten` --- src/systems/diffeqs/odesystem.jl | 1 + .../discrete_system/discrete_system.jl | 1 + src/systems/nonlinear/nonlinearsystem.jl | 1 + .../optimization/optimizationsystem.jl | 1 + test/components.jl | 36 +++++++++++++++++++ 5 files changed, 40 insertions(+) diff --git a/src/systems/diffeqs/odesystem.jl b/src/systems/diffeqs/odesystem.jl index 2b0bd8c8d7..34003f40c2 100644 --- a/src/systems/diffeqs/odesystem.jl +++ b/src/systems/diffeqs/odesystem.jl @@ -409,6 +409,7 @@ function flatten(sys::ODESystem, noeqs = false) initialization_eqs = initialization_equations(sys), is_dde = is_dde(sys), tstops = symbolic_tstops(sys), + metadata = get_metadata(sys), checks = false) end end diff --git a/src/systems/discrete_system/discrete_system.jl b/src/systems/discrete_system/discrete_system.jl index 3e220998cb..01fca30235 100644 --- a/src/systems/discrete_system/discrete_system.jl +++ b/src/systems/discrete_system/discrete_system.jl @@ -227,6 +227,7 @@ function flatten(sys::DiscreteSystem, noeqs = false) defaults = defaults(sys), name = nameof(sys), description = description(sys), + metadata = get_metadata(sys), checks = false) end end diff --git a/src/systems/nonlinear/nonlinearsystem.jl b/src/systems/nonlinear/nonlinearsystem.jl index 3cb68853aa..b2abac5184 100644 --- a/src/systems/nonlinear/nonlinearsystem.jl +++ b/src/systems/nonlinear/nonlinearsystem.jl @@ -859,6 +859,7 @@ function flatten(sys::NonlinearSystem, noeqs = false) defaults = defaults(sys), name = nameof(sys), description = description(sys), + metadata = get_metadata(sys), checks = false) end end diff --git a/src/systems/optimization/optimizationsystem.jl b/src/systems/optimization/optimizationsystem.jl index 43e9294dd3..0398c892eb 100644 --- a/src/systems/optimization/optimizationsystem.jl +++ b/src/systems/optimization/optimizationsystem.jl @@ -184,6 +184,7 @@ function flatten(sys::OptimizationSystem) constraints = constraints(sys), defaults = defaults(sys), name = nameof(sys), + metadata = get_metadata(sys), checks = false ) end diff --git a/test/components.jl b/test/components.jl index 298f9fceb9..8ac40f6fbb 100644 --- a/test/components.jl +++ b/test/components.jl @@ -370,3 +370,39 @@ end ss = structural_simplify(cbar) @test isequal(cbar.foo.x, ss.foo.x) end + +@testset "Issue#3275: Metadata retained on `complete`" begin + @variables x(t) y(t) + @testset "ODESystem" begin + @named inner = ODESystem(D(x) ~ x, t) + @named outer = ODESystem(D(y) ~ y, t; systems = [inner], metadata = "test") + @test ModelingToolkit.get_metadata(outer) == "test" + sys = complete(outer) + @test ModelingToolkit.get_metadata(sys) == "test" + end + @testset "NonlinearSystem" begin + @named inner = NonlinearSystem([0 ~ x^2 + 4x + 4], [x], []) + @named outer = NonlinearSystem( + [0 ~ x^3 - y^3], [x, y], []; systems = [inner], metadata = "test") + @test ModelingToolkit.get_metadata(outer) == "test" + sys = complete(outer) + @test ModelingToolkit.get_metadata(sys) == "test" + end + k = ShiftIndex(t) + @testset "DiscreteSystem" begin + @named inner = DiscreteSystem([x(k) ~ x(k - 1) + x(k - 2)], t, [x], []) + @named outer = DiscreteSystem([y(k) ~ y(k - 1) + y(k - 2)], t, [x, y], + []; systems = [inner], metadata = "test") + @test ModelingToolkit.get_metadata(outer) == "test" + sys = complete(outer) + @test ModelingToolkit.get_metadata(sys) == "test" + end + @testset "OptimizationSystem" begin + @named inner = OptimizationSystem(x^2 + y^2 - 3, [x, y], []) + @named outer = OptimizationSystem( + x^3 - y, [x, y], []; systems = [inner], metadata = "test") + @test ModelingToolkit.get_metadata(outer) == "test" + sys = complete(outer) + @test ModelingToolkit.get_metadata(sys) == "test" + end +end From 4792360ad15eff9bc1a77fc5245bfaaec46c96a0 Mon Sep 17 00:00:00 2001 From: Christopher Rackauckas Date: Wed, 18 Dec 2024 14:49:50 -0100 Subject: [PATCH 0489/2176] Update Project.toml --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 0b56e53805..3c6126b7c0 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "ModelingToolkit" uuid = "961ee093-0014-501f-94e3-6117800e7a78" authors = ["Yingbo Ma ", "Chris Rackauckas and contributors"] -version = "9.58.0" +version = "9.59.0" [deps] AbstractTrees = "1520ce14-60c1-5f80-bbc7-55ef81b5835c" From 3751c2a92c282593ef52b67b727c7db635ea677e Mon Sep 17 00:00:00 2001 From: vyudu Date: Fri, 20 Dec 2024 23:58:28 +0900 Subject: [PATCH 0490/2176] up --- src/systems/diffeqs/abstractodesystem.jl | 165 +++++++++++------------ test/bvproblem.jl | 4 +- 2 files changed, 83 insertions(+), 86 deletions(-) diff --git a/src/systems/diffeqs/abstractodesystem.jl b/src/systems/diffeqs/abstractodesystem.jl index 3d143d49fb..d8ff71c324 100644 --- a/src/systems/diffeqs/abstractodesystem.jl +++ b/src/systems/diffeqs/abstractodesystem.jl @@ -469,90 +469,6 @@ function DiffEqBase.ODEFunction{iip, specialize}(sys::AbstractODESystem, initializeprobpmap = initializeprobpmap) end -""" -```julia -SciMLBase.BVProblem{iip}(sys::AbstractODESystem, u0map, tspan, - parammap = DiffEqBase.NullParameters(); - version = nothing, tgrad = false, - jac = true, sparse = true, - simplify = false, - kwargs...) where {iip} -``` - -Create a `BVProblem` from the [`ODESystem`](@ref). The arguments `dvs` and -`ps` are used to set the order of the dependent variable and parameter vectors, -respectively. `u0map` should be used to specify the initial condition, or be a function returning an initial condition. -""" -function SciMLBase.BVProblem(sys::AbstractODESystem, args...; kwargs...) - BVProblem{true}(sys, args...; kwargs...) -end - -function SciMLBase.BVProblem(sys::AbstractODESystem, - u0map::StaticArray, - args...; - kwargs...) - BVProblem{false, SciMLBase.FullSpecialize}(sys, u0map, args...; kwargs...) -end - -function SciMLBase.BVProblem{true}(sys::AbstractODESystem, args...; kwargs...) - BVProblem{true, SciMLBase.AutoSpecialize}(sys, args...; kwargs...) -end - -function SciMLBase.BVProblem{false}(sys::AbstractODESystem, args...; kwargs...) - BVProblem{false, SciMLBase.FullSpecialize}(sys, args...; kwargs...) -end - -function SciMLBase.BVProblem{iip, specialize}(sys::AbstractODESystem, u0map = [], - tspan = get_tspan(sys), - parammap = DiffEqBase.NullParameters(); - version = nothing, tgrad = false, - callback = nothing, - check_length = true, - warn_initialize_determined = true, - eval_expression = false, - eval_module = @__MODULE__, - kwargs...) where {iip, specialize} - if !iscomplete(sys) - error("A completed system is required. Call `complete` or `structural_simplify` on the system before creating an `BVProblem`") - end - - f, u0, p = process_SciMLProblem(ODEFunction{iip, specialize}, sys, u0map, parammap; - t = tspan !== nothing ? tspan[1] : tspan, - check_length, warn_initialize_determined, eval_expression, eval_module, kwargs...) - - cbs = process_events(sys; callback, eval_expression, eval_module, kwargs...) - kwargs = filter_kwargs(kwargs) - - kwargs1 = (;) - if cbs !== nothing - kwargs1 = merge(kwargs1, (callback = cbs,)) - end - - # Construct initial conditions. - _u0 = u0 isa Function ? u0(tspan[1]) : u0 - - # Define the boundary conditions. - bc = if iip - (residual, u, p, t) -> (residual .= u[1] .- _u0) - else - (u, p, t) -> (u[1] - _u0) - end - - return BVProblem{iip}(f, bc, _u0, tspan, p; kwargs1..., kwargs...) -end - -get_callback(prob::BVProblem) = error("BVP solvers do not support callbacks.") - -@inline function SymbolicUtils.Code.create_array(::Type{<:Base.ReinterpretArray}, ::Nothing, - ::Val{1}, ::Val{dims}, elems...) where {dims} - [elems...] -end - -@inline function SymbolicUtils.Code.create_array( - ::Type{<:Base.ReinterpretArray}, T, ::Val{1}, ::Val{dims}, elems...) where {dims} - T[elems...] -end - """ ```julia DiffEqBase.DAEFunction{iip}(sys::AbstractODESystem, dvs = unknowns(sys), @@ -943,6 +859,87 @@ function DiffEqBase.ODEProblem{iip, specialize}(sys::AbstractODESystem, u0map = end get_callback(prob::ODEProblem) = prob.kwargs[:callback] +""" +```julia +SciMLBase.BVProblem{iip}(sys::AbstractODESystem, u0map, tspan, + parammap = DiffEqBase.NullParameters(); + version = nothing, tgrad = false, + jac = true, sparse = true, + simplify = false, + kwargs...) where {iip} +``` + +Create a `BVProblem` from the [`ODESystem`](@ref). The arguments `dvs` and +`ps` are used to set the order of the dependent variable and parameter vectors, +respectively. `u0map` should be used to specify the initial condition. +""" +function SciMLBase.BVProblem(sys::AbstractODESystem, args...; kwargs...) + BVProblem{true}(sys, args...; kwargs...) +end + +function SciMLBase.BVProblem(sys::AbstractODESystem, + u0map::StaticArray, + args...; + kwargs...) + BVProblem{false, SciMLBase.FullSpecialize}(sys, u0map, args...; kwargs...) +end + +function SciMLBase.BVProblem{true}(sys::AbstractODESystem, args...; kwargs...) + BVProblem{true, SciMLBase.AutoSpecialize}(sys, args...; kwargs...) +end + +function SciMLBase.BVProblem{false}(sys::AbstractODESystem, args...; kwargs...) + BVProblem{false, SciMLBase.FullSpecialize}(sys, args...; kwargs...) +end + +function SciMLBase.BVProblem{iip, specialize}(sys::AbstractODESystem, u0map = [], + tspan = get_tspan(sys), + parammap = DiffEqBase.NullParameters(); + version = nothing, tgrad = false, + callback = nothing, + check_length = true, + warn_initialize_determined = true, + eval_expression = false, + eval_module = @__MODULE__, + kwargs...) where {iip, specialize} + if !iscomplete(sys) + error("A completed system is required. Call `complete` or `structural_simplify` on the system before creating an `BVProblem`") + end + + f, u0, p = process_SciMLProblem(ODEFunction{iip, specialize}, sys, u0map, parammap; + t = tspan !== nothing ? tspan[1] : tspan, + check_length, warn_initialize_determined, eval_expression, eval_module, kwargs...) + + cbs = process_events(sys; callback, eval_expression, eval_module, kwargs...) + kwargs = filter_kwargs(kwargs) + + kwargs1 = (;) + if cbs !== nothing + kwargs1 = merge(kwargs1, (callback = cbs,)) + end + + # Define the boundary conditions. + bc = if iip + (residual, u, p, t) -> (residual .= u[1] .- u0) + else + (u, p, t) -> (u[1] - u0) + end + + return BVProblem{iip}(f, bc, u0, tspan, p; kwargs1..., kwargs...) +end + +get_callback(prob::BVProblem) = error("BVP solvers do not support callbacks.") + +@inline function SymbolicUtils.Code.create_array(::Type{<:Base.ReinterpretArray}, ::Nothing, + ::Val{1}, ::Val{dims}, elems...) where {dims} + [elems...] +end + +@inline function SymbolicUtils.Code.create_array( + ::Type{<:Base.ReinterpretArray}, T, ::Val{1}, ::Val{dims}, elems...) where {dims} + T[elems...] +end + """ ```julia DiffEqBase.DAEProblem{iip}(sys::AbstractODESystem, du0map, u0map, tspan, diff --git a/test/bvproblem.jl b/test/bvproblem.jl index 1072874917..c5a302147d 100644 --- a/test/bvproblem.jl +++ b/test/bvproblem.jl @@ -10,8 +10,8 @@ solvers = [MIRK4, RadauIIa5, LobattoIIIa3] eqs = [D(x) ~ α * x - β * x * y, D(y) ~ -γ * y + δ * x * y] -u0map = [:x => 1.0, :y => 2.0] -parammap = [:α => 7.5, :β => 4, :γ => 8.0, :δ => 5.0] +u0map = [x => 1.0, y => 2.0] +parammap = [α => 7.5, β => 4, γ => 8.0, δ => 5.0] tspan = (0.0, 10.0) @mtkbuild lotkavolterra = ODESystem(eqs, t) From 219aee396ed4d29606fcb3fa6922bb140091a0e6 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Sun, 1 Dec 2024 13:07:15 +0530 Subject: [PATCH 0491/2176] refactor: add guesses to `SDESystem`, `NonlinearSystem`, `JumpSystem` --- src/systems/diffeqs/odesystem.jl | 27 +++------ src/systems/diffeqs/sdesystem.jl | 52 ++++++++++++----- .../discrete_system/discrete_system.jl | 47 +++++++++++---- src/systems/jumps/jumpsystem.jl | 43 ++++++++++---- src/systems/nonlinear/nonlinearsystem.jl | 57 ++++++++++++++----- .../optimization/constraints_system.jl | 4 +- .../optimization/optimizationsystem.jl | 4 +- src/utils.jl | 27 +++++++++ 8 files changed, 187 insertions(+), 74 deletions(-) diff --git a/src/systems/diffeqs/odesystem.jl b/src/systems/diffeqs/odesystem.jl index 34003f40c2..61f16fd926 100644 --- a/src/systems/diffeqs/odesystem.jl +++ b/src/systems/diffeqs/odesystem.jl @@ -256,29 +256,16 @@ function ODESystem(deqs::AbstractVector{<:Equation}, iv, dvs, ps; :ODESystem, force = true) end defaults = Dict{Any, Any}(todict(defaults)) + guesses = Dict{Any, Any}(todict(guesses)) var_to_name = Dict() - process_variables!(var_to_name, defaults, dvs′) - process_variables!(var_to_name, defaults, ps′) - process_variables!(var_to_name, defaults, [eq.lhs for eq in parameter_dependencies]) - process_variables!(var_to_name, defaults, [eq.rhs for eq in parameter_dependencies]) + process_variables!(var_to_name, defaults, guesses, dvs′) + process_variables!(var_to_name, defaults, guesses, ps′) + process_variables!( + var_to_name, defaults, guesses, [eq.lhs for eq in parameter_dependencies]) + process_variables!( + var_to_name, defaults, guesses, [eq.rhs for eq in parameter_dependencies]) defaults = Dict{Any, Any}(value(k) => value(v) for (k, v) in pairs(defaults) if v !== nothing) - - sysdvsguesses = [ModelingToolkit.getguess(st) for st in dvs′] - hasaguess = findall(!isnothing, sysdvsguesses) - var_guesses = dvs′[hasaguess] .=> sysdvsguesses[hasaguess] - sysdvsguesses = isempty(var_guesses) ? Dict() : todict(var_guesses) - syspsguesses = [ModelingToolkit.getguess(st) for st in ps′] - hasaguess = findall(!isnothing, syspsguesses) - ps_guesses = ps′[hasaguess] .=> syspsguesses[hasaguess] - syspsguesses = isempty(ps_guesses) ? Dict() : todict(ps_guesses) - syspdepguesses = [ModelingToolkit.getguess(eq.lhs) for eq in parameter_dependencies] - hasaguess = findall(!isnothing, syspdepguesses) - pdep_guesses = [eq.lhs for eq in parameter_dependencies][hasaguess] .=> - syspdepguesses[hasaguess] - syspdepguesses = isempty(pdep_guesses) ? Dict() : todict(pdep_guesses) - - guesses = merge(sysdvsguesses, syspsguesses, syspdepguesses, todict(guesses)) guesses = Dict{Any, Any}(value(k) => value(v) for (k, v) in pairs(guesses) if v !== nothing) diff --git a/src/systems/diffeqs/sdesystem.jl b/src/systems/diffeqs/sdesystem.jl index ac47f4c45c..d604863024 100644 --- a/src/systems/diffeqs/sdesystem.jl +++ b/src/systems/diffeqs/sdesystem.jl @@ -93,6 +93,19 @@ struct SDESystem <: AbstractODESystem """ defaults::Dict """ + The guesses to use as the initial conditions for the + initialization system. + """ + guesses::Dict + """ + The system for performing the initialization. + """ + initializesystem::Union{Nothing, NonlinearSystem} + """ + Extra equations to be enforced during the initialization sequence. + """ + initialization_eqs::Vector{Equation} + """ Type of the system. """ connector_type::Any @@ -144,9 +157,8 @@ struct SDESystem <: AbstractODESystem isscheduled::Bool function SDESystem(tag, deqs, neqs, iv, dvs, ps, tspan, var_to_name, ctrls, observed, - tgrad, - jac, - ctrl_jac, Wfact, Wfact_t, name, description, systems, defaults, connector_type, + tgrad, jac, ctrl_jac, Wfact, Wfact_t, name, description, systems, defaults, + guesses, initializesystem, initialization_eqs, connector_type, cevents, devents, parameter_dependencies, metadata = nothing, gui_metadata = nothing, complete = false, index_cache = nothing, parent = nothing, is_scalar_noise = false, is_dde = false, @@ -171,9 +183,9 @@ struct SDESystem <: AbstractODESystem check_units(u, deqs, neqs) end new(tag, deqs, neqs, iv, dvs, ps, tspan, var_to_name, ctrls, observed, tgrad, jac, - ctrl_jac, - Wfact, Wfact_t, name, description, systems, - defaults, connector_type, cevents, devents, + ctrl_jac, Wfact, Wfact_t, name, description, systems, + defaults, guesses, initializesystem, initialization_eqs, connector_type, cevents, + devents, parameter_dependencies, metadata, gui_metadata, complete, index_cache, parent, is_scalar_noise, is_dde, isscheduled) end @@ -187,6 +199,9 @@ function SDESystem(deqs::AbstractVector{<:Equation}, neqs::AbstractArray, iv, dv default_u0 = Dict(), default_p = Dict(), defaults = _merge(Dict(default_u0), Dict(default_p)), + guesses = Dict(), + initializesystem = nothing, + initialization_eqs = Equation[], name = nothing, description = "", connector_type = nothing, @@ -207,6 +222,8 @@ function SDESystem(deqs::AbstractVector{<:Equation}, neqs::AbstractArray, iv, dv dvs′ = value.(dvs) ps′ = value.(ps) ctrl′ = value.(controls) + parameter_dependencies, ps′ = process_parameter_dependencies( + parameter_dependencies, ps′) sysnames = nameof.(systems) if length(unique(sysnames)) != length(sysnames) @@ -217,13 +234,21 @@ function SDESystem(deqs::AbstractVector{<:Equation}, neqs::AbstractArray, iv, dv "`default_u0` and `default_p` are deprecated. Use `defaults` instead.", :SDESystem, force = true) end - defaults = todict(defaults) - defaults = Dict(value(k) => value(v) - for (k, v) in pairs(defaults) if value(v) !== nothing) + defaults = Dict{Any, Any}(todict(defaults)) + guesses = Dict{Any, Any}(todict(guesses)) var_to_name = Dict() - process_variables!(var_to_name, defaults, dvs′) - process_variables!(var_to_name, defaults, ps′) + process_variables!(var_to_name, defaults, guesses, dvs′) + process_variables!(var_to_name, defaults, guesses, ps′) + process_variables!( + var_to_name, defaults, guesses, [eq.lhs for eq in parameter_dependencies]) + process_variables!( + var_to_name, defaults, guesses, [eq.rhs for eq in parameter_dependencies]) + defaults = Dict{Any, Any}(value(k) => value(v) + for (k, v) in pairs(defaults) if v !== nothing) + guesses = Dict{Any, Any}(value(k) => value(v) + for (k, v) in pairs(guesses) if v !== nothing) + isempty(observed) || collect_var_to_name!(var_to_name, (eq.lhs for eq in observed)) tgrad = RefValue(EMPTY_TGRAD) @@ -233,14 +258,13 @@ function SDESystem(deqs::AbstractVector{<:Equation}, neqs::AbstractArray, iv, dv Wfact_t = RefValue(EMPTY_JAC) cont_callbacks = SymbolicContinuousCallbacks(continuous_events) disc_callbacks = SymbolicDiscreteCallbacks(discrete_events) - parameter_dependencies, ps′ = process_parameter_dependencies( - parameter_dependencies, ps′) if is_dde === nothing is_dde = _check_if_dde(deqs, iv′, systems) end SDESystem(Threads.atomic_add!(SYSTEM_COUNT, UInt(1)), deqs, neqs, iv′, dvs′, ps′, tspan, var_to_name, ctrl′, observed, tgrad, jac, - ctrl_jac, Wfact, Wfact_t, name, description, systems, defaults, connector_type, + ctrl_jac, Wfact, Wfact_t, name, description, systems, defaults, guesses, + initializesystem, initialization_eqs, connector_type, cont_callbacks, disc_callbacks, parameter_dependencies, metadata, gui_metadata, complete, index_cache, parent, is_scalar_noise, is_dde; checks = checks) end diff --git a/src/systems/discrete_system/discrete_system.jl b/src/systems/discrete_system/discrete_system.jl index 01fca30235..7458237333 100644 --- a/src/systems/discrete_system/discrete_system.jl +++ b/src/systems/discrete_system/discrete_system.jl @@ -55,6 +55,19 @@ struct DiscreteSystem <: AbstractTimeDependentSystem """ defaults::Dict """ + The guesses to use as the initial conditions for the + initialization system. + """ + guesses::Dict + """ + The system for performing the initialization. + """ + initializesystem::Union{Nothing, NonlinearSystem} + """ + Extra equations to be enforced during the initialization sequence. + """ + initialization_eqs::Vector{Equation} + """ Inject assignment statements before the evaluation of the RHS function. """ preface::Any @@ -98,9 +111,8 @@ struct DiscreteSystem <: AbstractTimeDependentSystem isscheduled::Bool function DiscreteSystem(tag, discreteEqs, iv, dvs, ps, tspan, var_to_name, - observed, - name, description, - systems, defaults, preface, connector_type, parameter_dependencies = Equation[], + observed, name, description, systems, defaults, guesses, initializesystem, + initialization_eqs, preface, connector_type, parameter_dependencies = Equation[], metadata = nothing, gui_metadata = nothing, tearing_state = nothing, substitutions = nothing, complete = false, index_cache = nothing, parent = nothing, @@ -116,8 +128,7 @@ struct DiscreteSystem <: AbstractTimeDependentSystem check_units(u, discreteEqs) end new(tag, discreteEqs, iv, dvs, ps, tspan, var_to_name, observed, name, description, - systems, - defaults, + systems, defaults, guesses, initializesystem, initialization_eqs, preface, connector_type, parameter_dependencies, metadata, gui_metadata, tearing_state, substitutions, complete, index_cache, parent, isscheduled) end @@ -135,6 +146,9 @@ function DiscreteSystem(eqs::AbstractVector{<:Equation}, iv, dvs, ps; description = "", default_u0 = Dict(), default_p = Dict(), + guesses = Dict(), + initializesystem = nothing, + initialization_eqs = Equation[], defaults = _merge(Dict(default_u0), Dict(default_p)), preface = nothing, connector_type = nothing, @@ -155,13 +169,21 @@ function DiscreteSystem(eqs::AbstractVector{<:Equation}, iv, dvs, ps; "`default_u0` and `default_p` are deprecated. Use `defaults` instead.", :DiscreteSystem, force = true) end - defaults = todict(defaults) - defaults = Dict(value(k) => value(v) - for (k, v) in pairs(defaults) if value(v) !== nothing) + defaults = Dict{Any, Any}(todict(defaults)) + guesses = Dict{Any, Any}(todict(guesses)) var_to_name = Dict() - process_variables!(var_to_name, defaults, dvs′) - process_variables!(var_to_name, defaults, ps′) + process_variables!(var_to_name, defaults, guesses, dvs′) + process_variables!(var_to_name, defaults, guesses, ps′) + process_variables!( + var_to_name, defaults, guesses, [eq.lhs for eq in parameter_dependencies]) + process_variables!( + var_to_name, defaults, guesses, [eq.rhs for eq in parameter_dependencies]) + defaults = Dict{Any, Any}(value(k) => value(v) + for (k, v) in pairs(defaults) if v !== nothing) + guesses = Dict{Any, Any}(value(k) => value(v) + for (k, v) in pairs(guesses) if v !== nothing) + isempty(observed) || collect_var_to_name!(var_to_name, (eq.lhs for eq in observed)) sysnames = nameof.(systems) @@ -170,7 +192,8 @@ function DiscreteSystem(eqs::AbstractVector{<:Equation}, iv, dvs, ps; end DiscreteSystem(Threads.atomic_add!(SYSTEM_COUNT, UInt(1)), eqs, iv′, dvs′, ps′, tspan, var_to_name, observed, name, description, systems, - defaults, preface, connector_type, parameter_dependencies, metadata, gui_metadata, kwargs...) + defaults, guesses, initializesystem, initialization_eqs, preface, connector_type, + parameter_dependencies, metadata, gui_metadata, kwargs...) end function DiscreteSystem(eqs, iv; kwargs...) @@ -225,6 +248,8 @@ function flatten(sys::DiscreteSystem, noeqs = false) parameters(sys), observed = observed(sys), defaults = defaults(sys), + guesses = guesses(sys), + initialization_eqs = initialization_equations(sys), name = nameof(sys), description = description(sys), metadata = get_metadata(sys), diff --git a/src/systems/jumps/jumpsystem.jl b/src/systems/jumps/jumpsystem.jl index e5e17fb5f9..efc5a9be7d 100644 --- a/src/systems/jumps/jumpsystem.jl +++ b/src/systems/jumps/jumpsystem.jl @@ -84,6 +84,19 @@ struct JumpSystem{U <: ArrayPartition} <: AbstractTimeDependentSystem """ defaults::Dict """ + The guesses to use as the initial conditions for the + initialization system. + """ + guesses::Dict + """ + The system for performing the initialization. + """ + initializesystem::Union{Nothing, NonlinearSystem} + """ + Extra equations to be enforced during the initialization sequence. + """ + initialization_eqs::Vector{Equation} + """ Type of the system. """ connector_type::Any @@ -125,8 +138,9 @@ struct JumpSystem{U <: ArrayPartition} <: AbstractTimeDependentSystem function JumpSystem{U}( tag, ap::U, iv, unknowns, ps, var_to_name, observed, name, description, - systems, defaults, connector_type, cevents, devents, parameter_dependencies, - metadata = nothing, gui_metadata = nothing, + systems, defaults, guesses, initializesystem, initialization_eqs, connector_type, + cevents, devents, + parameter_dependencies, metadata = nothing, gui_metadata = nothing, complete = false, index_cache = nothing, isscheduled = false; checks::Union{Bool, Int} = true) where {U <: ArrayPartition} if checks == true || (checks & CheckComponents) > 0 @@ -139,7 +153,8 @@ struct JumpSystem{U <: ArrayPartition} <: AbstractTimeDependentSystem check_units(u, ap, iv) end new{U}(tag, ap, iv, unknowns, ps, var_to_name, - observed, name, description, systems, defaults, + observed, name, description, systems, defaults, guesses, initializesystem, + initialization_eqs, connector_type, cevents, devents, parameter_dependencies, metadata, gui_metadata, complete, index_cache, isscheduled) end @@ -154,6 +169,9 @@ function JumpSystem(eqs, iv, unknowns, ps; default_u0 = Dict(), default_p = Dict(), defaults = _merge(Dict(default_u0), Dict(default_p)), + guesses = Dict(), + initializesystem = nothing, + initialization_eqs = Equation[], name = nothing, description = "", connector_type = nothing, @@ -179,13 +197,17 @@ function JumpSystem(eqs, iv, unknowns, ps; :JumpSystem, force = true) end defaults = Dict{Any, Any}(todict(defaults)) + guesses = Dict{Any, Any}(todict(guesses)) var_to_name = Dict() - process_variables!(var_to_name, defaults, us′) - process_variables!(var_to_name, defaults, ps′) - process_variables!(var_to_name, defaults, [eq.lhs for eq in parameter_dependencies]) - process_variables!(var_to_name, defaults, [eq.rhs for eq in parameter_dependencies]) + process_variables!(var_to_name, defaults, guesses, us′) + process_variables!(var_to_name, defaults, guesses, ps′) + process_variables!( + var_to_name, defaults, guesses, [eq.lhs for eq in parameter_dependencies]) + process_variables!( + var_to_name, defaults, guesses, [eq.rhs for eq in parameter_dependencies]) #! format: off defaults = Dict{Any, Any}(value(k) => value(v) for (k, v) in pairs(defaults) if value(v) !== nothing) + guesses = Dict{Any, Any}(value(k) => value(v) for (k, v) in pairs(guesses) if v !== nothing) #! format: on isempty(observed) || collect_var_to_name!(var_to_name, (eq.lhs for eq in observed)) @@ -219,8 +241,9 @@ function JumpSystem(eqs, iv, unknowns, ps; JumpSystem{typeof(ap)}(Threads.atomic_add!(SYSTEM_COUNT, UInt(1)), ap, iv′, us′, ps′, var_to_name, observed, name, description, systems, - defaults, connector_type, cont_callbacks, disc_callbacks, parameter_dependencies, - metadata, gui_metadata, checks = checks) + defaults, guesses, initializesystem, initialization_eqs, connector_type, + cont_callbacks, disc_callbacks, + parameter_dependencies, metadata, gui_metadata, checks = checks) end ##### MTK dispatches for JumpSystems ##### @@ -494,7 +517,7 @@ function DiffEqBase.ODEProblem(sys::JumpSystem, u0map, tspan::Union{Tuple, Nothi if has_equations(sys) osys = ODESystem(equations(sys).x[4], get_iv(sys), unknowns(sys), parameters(sys); observed = observed(sys), name = nameof(sys), description = description(sys), - systems = get_systems(sys), defaults = defaults(sys), + systems = get_systems(sys), defaults = defaults(sys), guesses = guesses(sys), parameter_dependencies = parameter_dependencies(sys), metadata = get_metadata(sys), gui_metadata = get_gui_metadata(sys)) osys = complete(osys) diff --git a/src/systems/nonlinear/nonlinearsystem.jl b/src/systems/nonlinear/nonlinearsystem.jl index b2abac5184..46d39822bf 100644 --- a/src/systems/nonlinear/nonlinearsystem.jl +++ b/src/systems/nonlinear/nonlinearsystem.jl @@ -57,6 +57,19 @@ struct NonlinearSystem <: AbstractTimeIndependentSystem """ defaults::Dict """ + The guesses to use as the initial conditions for the + initialization system. + """ + guesses::Dict + """ + The system for performing the initialization. + """ + initializesystem::Union{Nothing, NonlinearSystem} + """ + Extra equations to be enforced during the initialization sequence. + """ + initialization_eqs::Vector{Equation} + """ Type of the system. """ connector_type::Any @@ -97,9 +110,8 @@ struct NonlinearSystem <: AbstractTimeIndependentSystem function NonlinearSystem( tag, eqs, unknowns, ps, var_to_name, observed, jac, name, description, - systems, - defaults, connector_type, parameter_dependencies = Equation[], metadata = nothing, - gui_metadata = nothing, + systems, defaults, guesses, initializesystem, initialization_eqs, connector_type, + parameter_dependencies = Equation[], metadata = nothing, gui_metadata = nothing, tearing_state = nothing, substitutions = nothing, complete = false, index_cache = nothing, parent = nothing, isscheduled = false; checks::Union{Bool, Int} = true) @@ -107,8 +119,8 @@ struct NonlinearSystem <: AbstractTimeIndependentSystem u = __get_unit_type(unknowns, ps) check_units(u, eqs) end - new(tag, eqs, unknowns, ps, var_to_name, observed, - jac, name, description, systems, defaults, + new(tag, eqs, unknowns, ps, var_to_name, observed, jac, name, description, + systems, defaults, guesses, initializesystem, initialization_eqs, connector_type, parameter_dependencies, metadata, gui_metadata, tearing_state, substitutions, complete, index_cache, parent, isscheduled) end @@ -121,6 +133,9 @@ function NonlinearSystem(eqs, unknowns, ps; default_u0 = Dict(), default_p = Dict(), defaults = _merge(Dict(default_u0), Dict(default_p)), + guesses = Dict(), + initializesystem = nothing, + initialization_eqs = Equation[], systems = NonlinearSystem[], connector_type = nothing, continuous_events = nothing, # this argument is only required for ODESystems, but is added here for the constructor to accept it without error @@ -151,21 +166,32 @@ function NonlinearSystem(eqs, unknowns, ps; eqs = [wrap(eq.lhs) isa Symbolics.Arr ? eq : 0 ~ eq.rhs - eq.lhs for eq in eqs] jac = RefValue{Any}(EMPTY_JAC) - defaults = todict(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) + ps′ = value.(ps) + dvs′ = value.(unknowns) + parameter_dependencies, ps′ = process_parameter_dependencies( + parameter_dependencies, ps′) + + defaults = Dict{Any, Any}(todict(defaults)) + guesses = Dict{Any, Any}(todict(guesses)) var_to_name = Dict() - process_variables!(var_to_name, defaults, unknowns) - process_variables!(var_to_name, defaults, ps) + process_variables!(var_to_name, defaults, guesses, dvs′) + process_variables!(var_to_name, defaults, guesses, ps′) + process_variables!( + var_to_name, defaults, guesses, [eq.lhs for eq in parameter_dependencies]) + process_variables!( + var_to_name, defaults, guesses, [eq.rhs for eq in parameter_dependencies]) + defaults = Dict{Any, Any}(value(k) => value(v) + for (k, v) in pairs(defaults) if v !== nothing) + guesses = Dict{Any, Any}(value(k) => value(v) + for (k, v) in pairs(guesses) if v !== nothing) + isempty(observed) || collect_var_to_name!(var_to_name, (eq.lhs for eq in observed)) - parameter_dependencies, ps = process_parameter_dependencies( - parameter_dependencies, ps) NonlinearSystem(Threads.atomic_add!(SYSTEM_COUNT, UInt(1)), - eqs, unknowns, ps, var_to_name, observed, jac, name, description, systems, defaults, - connector_type, parameter_dependencies, metadata, gui_metadata, checks = checks) + eqs, dvs′, ps′, var_to_name, observed, jac, name, description, systems, defaults, + guesses, initializesystem, initialization_eqs, connector_type, parameter_dependencies, + metadata, gui_metadata, checks = checks) end function NonlinearSystem(eqs; kwargs...) @@ -857,6 +883,7 @@ function flatten(sys::NonlinearSystem, noeqs = false) parameters(sys), observed = observed(sys), defaults = defaults(sys), + guesses = guesses(sys), name = nameof(sys), description = description(sys), metadata = get_metadata(sys), diff --git a/src/systems/optimization/constraints_system.jl b/src/systems/optimization/constraints_system.jl index a2756994ac..03225fc900 100644 --- a/src/systems/optimization/constraints_system.jl +++ b/src/systems/optimization/constraints_system.jl @@ -143,8 +143,8 @@ function ConstraintsSystem(constraints, unknowns, ps; for (k, v) in pairs(defaults) if value(v) !== nothing) var_to_name = Dict() - process_variables!(var_to_name, defaults, unknowns′) - process_variables!(var_to_name, defaults, ps′) + process_variables!(var_to_name, defaults, Dict(), unknowns′) + process_variables!(var_to_name, defaults, Dict(), ps′) isempty(observed) || collect_var_to_name!(var_to_name, (eq.lhs for eq in observed)) ConstraintsSystem(Threads.atomic_add!(SYSTEM_COUNT, UInt(1)), diff --git a/src/systems/optimization/optimizationsystem.jl b/src/systems/optimization/optimizationsystem.jl index 0398c892eb..0b20fdef79 100644 --- a/src/systems/optimization/optimizationsystem.jl +++ b/src/systems/optimization/optimizationsystem.jl @@ -132,8 +132,8 @@ function OptimizationSystem(op, unknowns, ps; for (k, v) in pairs(defaults) if value(v) !== nothing) var_to_name = Dict() - process_variables!(var_to_name, defaults, unknowns′) - process_variables!(var_to_name, defaults, ps′) + process_variables!(var_to_name, defaults, Dict(), unknowns′) + process_variables!(var_to_name, defaults, Dict(), ps′) isempty(observed) || collect_var_to_name!(var_to_name, (eq.lhs for eq in observed)) OptimizationSystem(Threads.atomic_add!(SYSTEM_COUNT, UInt(1)), diff --git a/src/utils.jl b/src/utils.jl index e9ddad3a07..c3011c2a79 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -244,6 +244,13 @@ function setdefault(v, val) val === nothing ? v : wrap(setdefaultval(unwrap(v), value(val))) end +function process_variables!(var_to_name, defs, guesses, vars) + collect_defaults!(defs, vars) + collect_guesses!(guesses, vars) + collect_var_to_name!(var_to_name, vars) + return nothing +end + function process_variables!(var_to_name, defs, vars) collect_defaults!(defs, vars) collect_var_to_name!(var_to_name, vars) @@ -261,6 +268,17 @@ function collect_defaults!(defs, vars) return defs end +function collect_guesses!(guesses, vars) + for v in vars + symbolic_type(v) == NotSymbolic() && continue + if haskey(guesses, v) || !hasguess(unwrap(v)) || (def = getguess(v)) === nothing + continue + end + guesses[v] = getguess(v) + end + return guesses +end + function collect_var_to_name!(vars, xs) for x in xs symbolic_type(x) == NotSymbolic() && continue @@ -1146,3 +1164,12 @@ function similar_variable(var::BasicSymbolic, name = :anon) end return sym end + +function guesses_from_metadata!(guesses, vars) + varguesses = [getguess(v) for v in vars] + hasaguess = findall(!isnothing, varguesses) + for i in hasaguess + haskey(guesses, vars[i]) && continue + guesses[vars[i]] = varguesses[i] + end +end From af8cd679571d6c2d078fbebdd9219536b270f335 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Sun, 1 Dec 2024 13:07:33 +0530 Subject: [PATCH 0492/2176] feat: support arbitrary systems in `generate_initializesystem` --- src/systems/nonlinear/initializesystem.jl | 65 +++++++++++++---------- 1 file changed, 38 insertions(+), 27 deletions(-) diff --git a/src/systems/nonlinear/initializesystem.jl b/src/systems/nonlinear/initializesystem.jl index 2344727920..229d462911 100644 --- a/src/systems/nonlinear/initializesystem.jl +++ b/src/systems/nonlinear/initializesystem.jl @@ -3,7 +3,7 @@ $(TYPEDSIGNATURES) Generate `NonlinearSystem` which initializes an ODE problem from specified initial conditions of an `ODESystem`. """ -function generate_initializesystem(sys::ODESystem; +function generate_initializesystem(sys::AbstractSystem; u0map = Dict(), pmap = Dict(), initialization_eqs = [], @@ -12,28 +12,36 @@ function generate_initializesystem(sys::ODESystem; algebraic_only = false, check_units = true, check_defguess = false, name = nameof(sys), extra_metadata = (;), kwargs...) - trueobs, eqs = unhack_observed(observed(sys), equations(sys)) + eqs = equations(sys) + eqs = filter(x -> x isa Equation, eqs) + trueobs, eqs = unhack_observed(observed(sys), eqs) vars = unique([unknowns(sys); getfield.(trueobs, :lhs)]) vars_set = Set(vars) # for efficient in-lookup - idxs_diff = isdiffeq.(eqs) - idxs_alge = .!idxs_diff - - # prepare map for dummy derivative substitution - eqs_diff = eqs[idxs_diff] - D = Differential(get_iv(sys)) - diffmap = merge( - Dict(eq.lhs => eq.rhs for eq in eqs_diff), - Dict(D(eq.lhs) => D(eq.rhs) for eq in trueobs) - ) - - # 1) process dummy derivatives and u0map into initialization system - eqs_ics = eqs[idxs_alge] # start equation list with algebraic equations + eqs_ics = Equation[] defs = copy(defaults(sys)) # copy so we don't modify sys.defaults additional_guesses = anydict(guesses) guesses = merge(get_guesses(sys), additional_guesses) - schedule = getfield(sys, :schedule) - if !isnothing(schedule) + idxs_diff = isdiffeq.(eqs) + + # 1) Use algebraic equations of time-dependent systems as initialization constraints + if has_iv(sys) + idxs_alge = .!idxs_diff + append!(eqs_ics, eqs[idxs_alge]) # start equation list with algebraic equations + + eqs_diff = eqs[idxs_diff] + D = Differential(get_iv(sys)) + diffmap = merge( + Dict(eq.lhs => eq.rhs for eq in eqs_diff), + Dict(D(eq.lhs) => D(eq.rhs) for eq in trueobs) + ) + else + diffmap = Dict() + end + + if has_schedule(sys) && (schedule = get_schedule(sys); !isnothing(schedule)) + # 2) process dummy derivatives and u0map into initialization system + # prepare map for dummy derivative substitution for x in filter(x -> !isnothing(x[1]), schedule.dummy_sub) # set dummy derivatives to default_dd_guess unless specified push!(defs, x[1] => get(guesses, x[1], default_dd_guess)) @@ -61,9 +69,14 @@ function generate_initializesystem(sys::ODESystem; process_u0map_with_dummysubs(y, x) end end + else + # 2) System doesn't have a schedule, so dummy derivatives don't exist/aren't handled (SDESystem) + for (k, v) in u0map + defs[k] = v + end end - # 2) process other variables + # 3) process other variables for var in vars if var ∈ keys(defs) push!(eqs_ics, var ~ defs[var]) @@ -74,7 +87,7 @@ function generate_initializesystem(sys::ODESystem; end end - # 3) process explicitly provided initialization equations + # 4) process explicitly provided initialization equations if !algebraic_only initialization_eqs = [get_initialization_eqs(sys); initialization_eqs] for eq in initialization_eqs @@ -83,7 +96,7 @@ function generate_initializesystem(sys::ODESystem; end end - # 4) process parameters as initialization unknowns + # 5) process parameters as initialization unknowns paramsubs = Dict() if pmap isa SciMLBase.NullParameters pmap = Dict() @@ -138,7 +151,7 @@ function generate_initializesystem(sys::ODESystem; end end - # 5) parameter dependencies become equations, their LHS become unknowns + # 6) parameter dependencies become equations, their LHS become unknowns # non-numeric dependent parameters stay as parameter dependencies new_parameter_deps = Equation[] for eq in parameter_dependencies(sys) @@ -153,7 +166,7 @@ function generate_initializesystem(sys::ODESystem; push!(defs, varp => guessval) end - # 6) handle values provided for dependent parameters similar to values for observed variables + # 7) handle values provided for dependent parameters similar to values for observed variables for (k, v) in merge(defaults(sys), pmap) if is_variable_floatingpoint(k) && has_parameter_dependency_with_lhs(sys, k) push!(eqs_ics, paramsubs[k] ~ v) @@ -161,12 +174,10 @@ function generate_initializesystem(sys::ODESystem; end # parameters do not include ones that became initialization unknowns - pars = vcat( - [get_iv(sys)], # include independent variable as pseudo-parameter - [p for p in parameters(sys) if !haskey(paramsubs, p)] - ) + pars = Vector{SymbolicParam}(filter(p -> !haskey(paramsubs, p), parameters(sys))) + is_time_dependent(sys) && push!(pars, get_iv(sys)) - # 7) use observed equations for guesses of observed variables if not provided + # 8) use observed equations for guesses of observed variables if not provided for eq in trueobs haskey(defs, eq.lhs) && continue any(x -> isequal(default_toterm(x), eq.lhs), keys(defs)) && continue From 39281945c17108c2b4a3434d89a4e57911905927 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 2 Dec 2024 11:32:03 +0530 Subject: [PATCH 0493/2176] refactor: use `initialization_data` in SciMLFunction constructors --- src/systems/diffeqs/abstractodesystem.jl | 28 ++++++++--------------- src/systems/diffeqs/sdesystem.jl | 6 ++--- src/systems/nonlinear/initializesystem.jl | 8 +------ src/systems/nonlinear/nonlinearsystem.jl | 12 ++++++---- 4 files changed, 21 insertions(+), 33 deletions(-) diff --git a/src/systems/diffeqs/abstractodesystem.jl b/src/systems/diffeqs/abstractodesystem.jl index f4e29346ff..7d9369d441 100644 --- a/src/systems/diffeqs/abstractodesystem.jl +++ b/src/systems/diffeqs/abstractodesystem.jl @@ -359,10 +359,7 @@ function DiffEqBase.ODEFunction{iip, specialize}(sys::AbstractODESystem, sparsity = false, analytic = nothing, split_idxs = nothing, - initializeprob = nothing, - update_initializeprob! = nothing, - initializeprobmap = nothing, - initializeprobpmap = nothing, + initialization_data = nothing, kwargs...) where {iip, specialize} if !iscomplete(sys) error("A completed system is required. Call `complete` or `structural_simplify` on the system before creating an `ODEFunction`") @@ -463,10 +460,7 @@ function DiffEqBase.ODEFunction{iip, specialize}(sys::AbstractODESystem, observed = observedfun, sparsity = sparsity ? jacobian_sparsity(sys) : nothing, analytic = analytic, - initializeprob = initializeprob, - update_initializeprob! = update_initializeprob!, - initializeprobmap = initializeprobmap, - initializeprobpmap = initializeprobpmap) + initialization_data) end """ @@ -496,10 +490,7 @@ function DiffEqBase.DAEFunction{iip}(sys::AbstractODESystem, dvs = unknowns(sys) sparse = false, simplify = false, eval_module = @__MODULE__, checkbounds = false, - initializeprob = nothing, - initializeprobmap = nothing, - initializeprobpmap = nothing, - update_initializeprob! = nothing, + initialization_data = nothing, kwargs...) where {iip} if !iscomplete(sys) error("A completed system is required. Call `complete` or `structural_simplify` on the system before creating a `DAEFunction`") @@ -547,15 +538,12 @@ function DiffEqBase.DAEFunction{iip}(sys::AbstractODESystem, dvs = unknowns(sys) nothing end - DAEFunction{iip}(f, + DAEFunction{iip}(f; sys = sys, jac = _jac === nothing ? nothing : _jac, jac_prototype = jac_prototype, observed = observedfun, - initializeprob = initializeprob, - initializeprobmap = initializeprobmap, - initializeprobpmap = initializeprobpmap, - update_initializeprob! = update_initializeprob!) + initialization_data) end function DiffEqBase.DDEFunction(sys::AbstractODESystem, args...; kwargs...) @@ -567,6 +555,7 @@ function DiffEqBase.DDEFunction{iip}(sys::AbstractODESystem, dvs = unknowns(sys) eval_expression = false, eval_module = @__MODULE__, checkbounds = false, + initialization_data = nothing, kwargs...) where {iip} if !iscomplete(sys) error("A completed system is required. Call `complete` or `structural_simplify` on the system before creating an `DDEFunction`") @@ -579,7 +568,7 @@ function DiffEqBase.DDEFunction{iip}(sys::AbstractODESystem, dvs = unknowns(sys) f(u, h, p, t) = f_oop(u, h, p, t) f(du, u, h, p, t) = f_iip(du, u, h, p, t) - DDEFunction{iip}(f, sys = sys) + DDEFunction{iip}(f; sys = sys, initialization_data) end function DiffEqBase.SDDEFunction(sys::AbstractODESystem, args...; kwargs...) @@ -591,6 +580,7 @@ function DiffEqBase.SDDEFunction{iip}(sys::AbstractODESystem, dvs = unknowns(sys eval_expression = false, eval_module = @__MODULE__, checkbounds = false, + initialization_data = nothing, kwargs...) where {iip} if !iscomplete(sys) error("A completed system is required. Call `complete` or `structural_simplify` on the system before creating an `SDDEFunction`") @@ -609,7 +599,7 @@ function DiffEqBase.SDDEFunction{iip}(sys::AbstractODESystem, dvs = unknowns(sys g(u, h, p, t) = g_oop(u, h, p, t) g(du, u, h, p, t) = g_iip(du, u, h, p, t) - SDDEFunction{iip}(f, g, sys = sys) + SDDEFunction{iip}(f, g; sys = sys, initialization_data) end """ diff --git a/src/systems/diffeqs/sdesystem.jl b/src/systems/diffeqs/sdesystem.jl index d604863024..37e743218a 100644 --- a/src/systems/diffeqs/sdesystem.jl +++ b/src/systems/diffeqs/sdesystem.jl @@ -544,7 +544,7 @@ function DiffEqBase.SDEFunction{iip, specialize}(sys::SDESystem, dvs = unknowns( version = nothing, tgrad = false, sparse = false, jac = false, Wfact = false, eval_expression = false, eval_module = @__MODULE__, - checkbounds = false, + checkbounds = false, initialization_data = nothing, kwargs...) where {iip, specialize} if !iscomplete(sys) error("A completed `SDESystem` is required. Call `complete` or `structural_simplify` on the system before creating an `SDEFunction`") @@ -615,13 +615,13 @@ function DiffEqBase.SDEFunction{iip, specialize}(sys::SDESystem, dvs = unknowns( observedfun = ObservedFunctionCache(sys; eval_expression, eval_module) - SDEFunction{iip, specialize}(f, g, + SDEFunction{iip, specialize}(f, g; sys = sys, jac = _jac === nothing ? nothing : _jac, tgrad = _tgrad === nothing ? nothing : _tgrad, Wfact = _Wfact === nothing ? nothing : _Wfact, Wfact_t = _Wfact_t === nothing ? nothing : _Wfact_t, - mass_matrix = _M, + mass_matrix = _M, initialization_data, observed = observedfun) end diff --git a/src/systems/nonlinear/initializesystem.jl b/src/systems/nonlinear/initializesystem.jl index 229d462911..624ee1bd71 100644 --- a/src/systems/nonlinear/initializesystem.jl +++ b/src/systems/nonlinear/initializesystem.jl @@ -346,13 +346,7 @@ function SciMLBase.remake_initialization_data(sys::ODESystem, odefn, u0, t0, p, u0map, pmap, defs, cmap, dvs, ps) kws = maybe_build_initialization_problem( sys, op, u0map, pmap, t0, defs, guesses, missing_unknowns; use_scc) - initprob = get(kws, :initializeprob, nothing) - if initprob === nothing - return nothing - end - return SciMLBase.OverrideInitData(initprob, get(kws, :update_initializeprob!, nothing), - get(kws, :initializeprobmap, nothing), - get(kws, :initializeprobpmap, nothing)) + return get(kws, :initialization_data, nothing) end """ diff --git a/src/systems/nonlinear/nonlinearsystem.jl b/src/systems/nonlinear/nonlinearsystem.jl index 46d39822bf..8cd8175668 100644 --- a/src/systems/nonlinear/nonlinearsystem.jl +++ b/src/systems/nonlinear/nonlinearsystem.jl @@ -344,6 +344,7 @@ function SciMLBase.NonlinearFunction{iip}(sys::NonlinearSystem, dvs = unknowns(s eval_expression = false, eval_module = @__MODULE__, sparse = false, simplify = false, + initialization_data = nothing, kwargs...) where {iip} if !iscomplete(sys) error("A completed `NonlinearSystem` is required. Call `complete` or `structural_simplify` on the system before creating a `NonlinearFunction`") @@ -376,14 +377,14 @@ function SciMLBase.NonlinearFunction{iip}(sys::NonlinearSystem, dvs = unknowns(s resid_prototype = calculate_resid_prototype(length(equations(sys)), u0, p) end - NonlinearFunction{iip}(f, + NonlinearFunction{iip}(f; sys = sys, jac = _jac === nothing ? nothing : _jac, resid_prototype = resid_prototype, jac_prototype = sparse ? similar(calculate_jacobian(sys, sparse = sparse), Float64) : nothing, - observed = observedfun) + observed = observedfun, initialization_data) end """ @@ -395,7 +396,8 @@ respectively. """ function SciMLBase.IntervalNonlinearFunction( sys::NonlinearSystem, dvs = unknowns(sys), ps = parameters(sys), u0 = nothing; - p = nothing, eval_expression = false, eval_module = @__MODULE__, kwargs...) + p = nothing, eval_expression = false, eval_module = @__MODULE__, + initialization_data = nothing, kwargs...) if !iscomplete(sys) error("A completed `NonlinearSystem` is required. Call `complete` or `structural_simplify` on the system before creating a `IntervalNonlinearFunction`") end @@ -411,7 +413,8 @@ function SciMLBase.IntervalNonlinearFunction( observedfun = ObservedFunctionCache(sys; eval_expression, eval_module) - IntervalNonlinearFunction{false}(f; observed = observedfun, sys = sys) + IntervalNonlinearFunction{false}( + f; observed = observedfun, sys = sys, initialization_data) end """ @@ -884,6 +887,7 @@ function flatten(sys::NonlinearSystem, noeqs = false) observed = observed(sys), defaults = defaults(sys), guesses = guesses(sys), + initialization_eqs = initialization_equations(sys), name = nameof(sys), description = description(sys), metadata = get_metadata(sys), From 4044317b17b3f239f0a4fa6a87f16f368f8f28aa Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 2 Dec 2024 11:33:14 +0530 Subject: [PATCH 0494/2176] fix: don't build initializeprob for initializeprob --- src/systems/diffeqs/abstractodesystem.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/systems/diffeqs/abstractodesystem.jl b/src/systems/diffeqs/abstractodesystem.jl index 7d9369d441..b3d4903191 100644 --- a/src/systems/diffeqs/abstractodesystem.jl +++ b/src/systems/diffeqs/abstractodesystem.jl @@ -1395,5 +1395,5 @@ function InitializationProblem{iip, specialize}(sys::AbstractODESystem, else NonlinearLeastSquaresProblem end - TProb(isys, u0map, parammap; kwargs...) + TProb(isys, u0map, parammap; kwargs..., build_initializeprob = false) end From 180b97801da7d134754c16becc9daebbfb688912 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 2 Dec 2024 11:34:02 +0530 Subject: [PATCH 0495/2176] feat: build initialization system for all system types in `process_SciMLProblem` --- src/systems/problem_utils.jl | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/systems/problem_utils.jl b/src/systems/problem_utils.jl index 6b75fb2c6d..6ce5704daa 100644 --- a/src/systems/problem_utils.jl +++ b/src/systems/problem_utils.jl @@ -540,8 +540,9 @@ function maybe_build_initialization_problem( end if (((implicit_dae || has_observed_u0s || !isempty(missing_unknowns) || !isempty(solvablepars) || has_dependent_unknowns) && - get_tearing_state(sys) !== nothing) || - !isempty(initialization_equations(sys))) && t !== nothing + (!has_tearing_state(sys) || get_tearing_state(sys) !== nothing)) || + !isempty(initialization_equations(sys))) && + (!is_time_dependent(sys) || t !== nothing) initializeprob = ModelingToolkit.InitializationProblem( sys, t, u0map, pmap; guesses, kwargs...) initializeprobmap = getu(initializeprob, unknowns(sys)) @@ -567,7 +568,9 @@ function maybe_build_initialization_problem( end empty!(missing_unknowns) return (; - initializeprob, initializeprobmap, initializeprobpmap, update_initializeprob!) + initialization_data = SciMLBase.OverrideInitData( + initializeprob, update_initializeprob!, initializeprobmap, + initializeprobpmap)) end return (;) end @@ -662,7 +665,7 @@ function process_SciMLProblem( op, missing_unknowns, missing_pars = build_operating_point( u0map, pmap, defs, cmap, dvs, ps) - if sys isa ODESystem && build_initializeprob + if build_initializeprob kws = maybe_build_initialization_problem( sys, op, u0map, pmap, t, defs, guesses, missing_unknowns; implicit_dae, warn_initialize_determined, initialization_eqs, From c9c613f314cddb9ade459920cbb102a3b57b22da Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 2 Dec 2024 17:18:07 +0530 Subject: [PATCH 0496/2176] fix: retain system data on `structural_simplify` of `SDESystem` --- src/systems/systems.jl | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/systems/systems.jl b/src/systems/systems.jl index 47acd81a82..04c50bc766 100644 --- a/src/systems/systems.jl +++ b/src/systems/systems.jl @@ -164,6 +164,7 @@ function __structural_simplify(sys::AbstractSystem, io = nothing; simplify = fal return SDESystem(Vector{Equation}(full_equations(ode_sys)), noise_eqs, get_iv(ode_sys), unknowns(ode_sys), parameters(ode_sys); name = nameof(ode_sys), is_scalar_noise, observed = observed(ode_sys), defaults = defaults(sys), - parameter_dependencies = parameter_dependencies(sys)) + parameter_dependencies = parameter_dependencies(sys), + guesses = guesses(sys), initialization_eqs = initialization_equations(sys)) end end From 4d5daa3caeaad6ee4ed6f9698194aa467d1c4ea4 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 3 Dec 2024 16:32:11 +0530 Subject: [PATCH 0497/2176] fix: pass `t` to `process_SciMLProblem` in `SDEProblem` --- src/systems/diffeqs/sdesystem.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/systems/diffeqs/sdesystem.jl b/src/systems/diffeqs/sdesystem.jl index 37e743218a..5e0d0e3208 100644 --- a/src/systems/diffeqs/sdesystem.jl +++ b/src/systems/diffeqs/sdesystem.jl @@ -738,7 +738,7 @@ function DiffEqBase.SDEProblem{iip, specialize}( end f, u0, p = process_SciMLProblem( SDEFunction{iip, specialize}, sys, u0map, parammap; check_length, - kwargs...) + t = tspan === nothing ? nothing : tspan[1], kwargs...) cbs = process_events(sys; callback, kwargs...) sparsenoise === nothing && (sparsenoise = get(kwargs, :sparse, false)) From 8d09409a011a4d91dfe6d22d81b255f9a0da753f Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 3 Dec 2024 16:33:03 +0530 Subject: [PATCH 0498/2176] feat: support arbitrary systems in `remake_initialization_data` --- src/systems/nonlinear/initializesystem.jl | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/src/systems/nonlinear/initializesystem.jl b/src/systems/nonlinear/initializesystem.jl index 624ee1bd71..e32f967f4a 100644 --- a/src/systems/nonlinear/initializesystem.jl +++ b/src/systems/nonlinear/initializesystem.jl @@ -223,16 +223,19 @@ function is_parameter_solvable(p, pmap, defs, guesses) _val1 === nothing && _val2 !== nothing)) && _val3 !== nothing end -function SciMLBase.remake_initialization_data(sys::ODESystem, odefn, u0, t0, p, newu0, newp) +function SciMLBase.remake_initialization_data( + sys::AbstractSystem, odefn, u0, t0, p, newu0, newp) if u0 === missing && p === missing return odefn.initialization_data end if !(eltype(u0) <: Pair) && !(eltype(p) <: Pair) - oldinitprob = odefn.initializeprob + oldinitdata = odefn.initialization_data + oldinitdata === nothing && return nothing + + oldinitprob = oldinitdata.initializeprob oldinitprob === nothing && return nothing if !SciMLBase.has_sys(oldinitprob.f) || !(oldinitprob.f.sys isa NonlinearSystem) - return SciMLBase.OverrideInitData(oldinitprob, odefn.update_initializeprob!, - odefn.initializeprobmap, odefn.initializeprobpmap) + return oldinitdata end pidxs = ParameterIndex[] pvals = [] @@ -254,7 +257,7 @@ function SciMLBase.remake_initialization_data(sys::ODESystem, odefn, u0, t0, p, if p !== missing for sym in parameter_symbols(oldinitprob) push!(pidxs, parameter_index(oldinitprob, sym)) - if isequal(sym, get_iv(sys)) + if is_time_dependent(sys) && isequal(sym, get_iv(sys)) push!(pvals, t0) else push!(pvals, getp(sys, sym)(p)) @@ -283,8 +286,8 @@ function SciMLBase.remake_initialization_data(sys::ODESystem, odefn, u0, t0, p, length(oldinitprob.f.resid_prototype), newu0, newp)) end initprob = remake(oldinitprob; f = newf, u0 = newu0, p = newp) - return SciMLBase.OverrideInitData(initprob, odefn.update_initializeprob!, - odefn.initializeprobmap, odefn.initializeprobpmap) + return SciMLBase.OverrideInitData(initprob, oldinitdata.update_initializeprob!, + oldinitdata.initializeprobmap, oldinitdata.initializeprobpmap) end dvs = unknowns(sys) ps = parameters(sys) @@ -298,7 +301,7 @@ function SciMLBase.remake_initialization_data(sys::ODESystem, odefn, u0, t0, p, use_scc = true if SciMLBase.has_initializeprob(odefn) - oldsys = odefn.initializeprob.f.sys + oldsys = odefn.initialization_data.initializeprob.f.sys meta = get_metadata(oldsys) if meta isa InitializationSystemMetadata u0map = merge(meta.u0map, u0map) @@ -336,7 +339,7 @@ function SciMLBase.remake_initialization_data(sys::ODESystem, odefn, u0, t0, p, pmap[p] = getp(sys, p)(newp) end end - if t0 === nothing + if t0 === nothing && is_time_dependent(sys) t0 = 0.0 end filter_missing_values!(u0map) From def207b92a83baf5363ebaded009c5b95706d645 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 3 Dec 2024 16:33:13 +0530 Subject: [PATCH 0499/2176] fix: fix type promotion bug in `remake_buffer` --- src/systems/parameter_buffer.jl | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/systems/parameter_buffer.jl b/src/systems/parameter_buffer.jl index 7d64054acc..bc4e62a773 100644 --- a/src/systems/parameter_buffer.jl +++ b/src/systems/parameter_buffer.jl @@ -574,6 +574,10 @@ function _remake_buffer(indp, oldbuf::MTKParameters, idxs, vals; validate = true @set! newbuf.tunable = narrow_buffer_type_and_fallback_undefs( oldbuf.tunable, newbuf.tunable) + if eltype(newbuf.tunable) <: Integer + T = promote_type(eltype(newbuf.tunable), Float64) + @set! newbuf.tunable = T.(newbuf.tunable) + end @set! newbuf.discrete = narrow_buffer_type_and_fallback_undefs.( oldbuf.discrete, newbuf.discrete) @set! newbuf.constant = narrow_buffer_type_and_fallback_undefs.( From d971b185ad5d4d9f1714448131b2bb02aff58b2d Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 3 Dec 2024 16:33:51 +0530 Subject: [PATCH 0500/2176] test: test initialization on `SDEProblem`, `DDEProblem`, `SDDEProblem` --- test/initializationsystem.jl | 428 ++++++++++++++++++++--------------- 1 file changed, 247 insertions(+), 181 deletions(-) diff --git a/test/initializationsystem.jl b/test/initializationsystem.jl index 9c06dd2030..045b7d2d2c 100644 --- a/test/initializationsystem.jl +++ b/test/initializationsystem.jl @@ -1,4 +1,5 @@ using ModelingToolkit, OrdinaryDiffEq, NonlinearSolve, Test +using StochasticDiffEq, DelayDiffEq, StochasticDelayDiffEq using ForwardDiff using SymbolicIndexingInterface, SciMLStructures using SciMLStructures: Tunable @@ -583,108 +584,123 @@ sol = solve(oprob_2nd_order_2, Rosenbrock23()) # retcode: Success end @testset "Initialization of parameters" begin - function test_parameter(prob, sym, val, initialval = zero(val)) - @test prob.ps[sym] ≈ initialval - @test init(prob, Tsit5()).ps[sym] ≈ val - @test solve(prob, Tsit5()).ps[sym] ≈ val - end - function test_initializesystem(sys, u0map, pmap, p, equation) - isys = ModelingToolkit.generate_initializesystem( - sys; u0map, pmap, guesses = ModelingToolkit.guesses(sys)) - @test is_variable(isys, p) - @test equation in equations(isys) || (0 ~ -equation.rhs) in equations(isys) - end - @variables x(t) y(t) + @variables _x(..) y(t) @parameters p q - u0map = Dict(x => 1.0, y => 1.0) - pmap = Dict() - pmap[q] = 1.0 - # `missing` default, equation from ODEProblem - @mtkbuild sys = ODESystem( - [D(x) ~ x * q, D(y) ~ y * p], t; defaults = [p => missing], guesses = [p => 1.0]) - pmap[p] = 2q - prob = ODEProblem(sys, u0map, (0.0, 1.0), pmap) - test_parameter(prob, p, 2.0) - prob2 = remake(prob; u0 = u0map, p = pmap) - prob2.ps[p] = 0.0 - test_parameter(prob2, p, 2.0) - # `missing` default, provided guess - @mtkbuild sys = ODESystem( - [D(x) ~ x, p ~ x + y], t; defaults = [p => missing], guesses = [p => 0.0]) - prob = ODEProblem(sys, u0map, (0.0, 1.0)) - test_parameter(prob, p, 2.0) - test_initializesystem(sys, u0map, pmap, p, 0 ~ p - x - y) - prob2 = remake(prob; u0 = u0map) - prob2.ps[p] = 0.0 - test_parameter(prob2, p, 2.0) - - # `missing` to ODEProblem, equation from default - @mtkbuild sys = ODESystem( - [D(x) ~ x * q, D(y) ~ y * p], t; defaults = [p => 2q], guesses = [p => 1.0]) - pmap[p] = missing - prob = ODEProblem(sys, u0map, (0.0, 1.0), pmap) - test_parameter(prob, p, 2.0) - test_initializesystem(sys, u0map, pmap, p, 0 ~ 2q - p) - prob2 = remake(prob; u0 = u0map, p = pmap) - prob2.ps[p] = 0.0 - test_parameter(prob2, p, 2.0) - # `missing` to ODEProblem, provided guess - @mtkbuild sys = ODESystem( - [D(x) ~ x, p ~ x + y], t; guesses = [p => 0.0]) - prob = ODEProblem(sys, u0map, (0.0, 1.0), pmap) - test_parameter(prob, p, 2.0) - test_initializesystem(sys, u0map, pmap, p, 0 ~ x + y - p) - prob2 = remake(prob; u0 = u0map, p = pmap) - prob2.ps[p] = 0.0 - test_parameter(prob2, p, 2.0) - - # No `missing`, default and guess - @mtkbuild sys = ODESystem( - [D(x) ~ x * q, D(y) ~ y * p], t; defaults = [p => 2q], guesses = [p => 0.0]) - delete!(pmap, p) - prob = ODEProblem(sys, u0map, (0.0, 1.0), pmap) - test_parameter(prob, p, 2.0) - test_initializesystem(sys, u0map, pmap, p, 0 ~ 2q - p) - prob2 = remake(prob; u0 = u0map, p = pmap) - prob2.ps[p] = 0.0 - test_parameter(prob2, p, 2.0) - - # Default overridden by ODEProblem, guess provided - @mtkbuild sys = ODESystem( - [D(x) ~ q * x, D(y) ~ y * p], t; defaults = [p => 2q], guesses = [p => 1.0]) - _pmap = merge(pmap, Dict(p => q)) - prob = ODEProblem(sys, u0map, (0.0, 1.0), _pmap) - test_parameter(prob, p, _pmap[q]) - test_initializesystem(sys, u0map, _pmap, p, 0 ~ q - p) - - # ODEProblem dependent value with guess, no `missing` - @mtkbuild sys = ODESystem([D(x) ~ x * q, D(y) ~ y * p], t; guesses = [p => 0.0]) - _pmap = merge(pmap, Dict(p => 3q)) - prob = ODEProblem(sys, u0map, (0.0, 1.0), _pmap) - test_parameter(prob, p, 3pmap[q]) - - # Should not be solved for: - - # Override dependent default with direct value - @mtkbuild sys = ODESystem( - [D(x) ~ q * x, D(y) ~ y * p], t; defaults = [p => 2q], guesses = [p => 1.0]) - _pmap = merge(pmap, Dict(p => 1.0)) - prob = ODEProblem(sys, u0map, (0.0, 1.0), _pmap) - @test prob.ps[p] ≈ 1.0 - @test prob.f.initializeprob === nothing - - # Non-floating point - @parameters r::Int s::Int - @mtkbuild sys = ODESystem( - [D(x) ~ s * x, D(y) ~ y * r], t; defaults = [s => 2r], guesses = [s => 1.0]) - prob = ODEProblem(sys, u0map, (0.0, 1.0), [r => 1]) - @test prob.ps[r] == 1 - @test prob.ps[s] == 2 - @test prob.f.initializeprob === nothing - - @mtkbuild sys = ODESystem([D(x) ~ x, p ~ x + y], t; guesses = [p => 0.0]) - @test_throws ModelingToolkit.MissingParametersError ODEProblem( - sys, [x => 1.0, y => 1.0], (0.0, 1.0)) + @brownian a b + x = _x(t) + + # `System` constructor creates appropriate type with mtkbuild + # `Problem` and `alg` create the problem to test and allow calling `init` with + # the correct solver. + # `rhss` allows adding terms to the end of equations (only 2 equations allowed) to influence + # the system type (brownian vars to turn it into an SDE). + @testset "$Problem" for (Problem, alg, rhss) in [ + (ODEProblem, Tsit5(), zeros(2)), (SDEProblem, ImplicitEM(), [a, b]), + (DDEProblem, MethodOfSteps(Tsit5()), [_x(t - 0.1), 0.0]), + (SDDEProblem, ImplicitEM(), [_x(t - 0.1) + a, b])] + function test_parameter(prob, sym, val, initialval = zero(val)) + @test prob.ps[sym] ≈ initialval + @test init(prob, alg).ps[sym] ≈ val + @test solve(prob, alg).ps[sym] ≈ val + end + function test_initializesystem(sys, u0map, pmap, p, equation) + isys = ModelingToolkit.generate_initializesystem( + sys; u0map, pmap, guesses = ModelingToolkit.guesses(sys)) + @test is_variable(isys, p) + @test equation in equations(isys) || (0 ~ -equation.rhs) in equations(isys) + end + u0map = Dict(x => 1.0, y => 1.0) + pmap = Dict() + pmap[q] = 1.0 + # `missing` default, equation from Problem + @mtkbuild sys = System( + [D(x) ~ x * q + rhss[1], D(y) ~ y * p + rhss[2]], t; defaults = [p => missing], guesses = [p => 1.0]) + pmap[p] = 2q + prob = Problem(sys, u0map, (0.0, 1.0), pmap) + test_parameter(prob, p, 2.0) + prob2 = remake(prob; u0 = u0map, p = pmap) + prob2.ps[p] = 0.0 + test_parameter(prob2, p, 2.0) + # `missing` default, provided guess + @mtkbuild sys = System( + [D(x) ~ x + rhss[1], p ~ x + y + rhss[2]], t; defaults = [p => missing], guesses = [p => 0.0]) + prob = Problem(sys, u0map, (0.0, 1.0)) + test_parameter(prob, p, 2.0) + test_initializesystem(sys, u0map, pmap, p, 0 ~ p - x - y) + prob2 = remake(prob; u0 = u0map) + prob2.ps[p] = 0.0 + test_parameter(prob2, p, 2.0) + + # `missing` to Problem, equation from default + @mtkbuild sys = System( + [D(x) ~ x * q + rhss[1], D(y) ~ y * p + rhss[2]], t; defaults = [p => 2q], guesses = [p => 1.0]) + pmap[p] = missing + prob = Problem(sys, u0map, (0.0, 1.0), pmap) + test_parameter(prob, p, 2.0) + test_initializesystem(sys, u0map, pmap, p, 0 ~ 2q - p) + prob2 = remake(prob; u0 = u0map, p = pmap) + prob2.ps[p] = 0.0 + test_parameter(prob2, p, 2.0) + # `missing` to Problem, provided guess + @mtkbuild sys = System( + [D(x) ~ x + rhss[1], p ~ x + y + rhss[2]], t; guesses = [p => 0.0]) + prob = Problem(sys, u0map, (0.0, 1.0), pmap) + test_parameter(prob, p, 2.0) + test_initializesystem(sys, u0map, pmap, p, 0 ~ x + y - p) + prob2 = remake(prob; u0 = u0map, p = pmap) + prob2.ps[p] = 0.0 + test_parameter(prob2, p, 2.0) + + # No `missing`, default and guess + @mtkbuild sys = System( + [D(x) ~ x * q + rhss[1], D(y) ~ y * p + rhss[2]], t; defaults = [p => 2q], guesses = [p => 0.0]) + delete!(pmap, p) + prob = Problem(sys, u0map, (0.0, 1.0), pmap) + test_parameter(prob, p, 2.0) + test_initializesystem(sys, u0map, pmap, p, 0 ~ 2q - p) + prob2 = remake(prob; u0 = u0map, p = pmap) + prob2.ps[p] = 0.0 + test_parameter(prob2, p, 2.0) + + # Default overridden by Problem, guess provided + @mtkbuild sys = System( + [D(x) ~ q * x + rhss[1], D(y) ~ y * p + rhss[2]], t; defaults = [p => 2q], guesses = [p => 1.0]) + _pmap = merge(pmap, Dict(p => q)) + prob = Problem(sys, u0map, (0.0, 1.0), _pmap) + test_parameter(prob, p, _pmap[q]) + test_initializesystem(sys, u0map, _pmap, p, 0 ~ q - p) + + # Problem dependent value with guess, no `missing` + @mtkbuild sys = System( + [D(x) ~ x * q + rhss[1], D(y) ~ y * p + rhss[2]], t; guesses = [p => 0.0]) + _pmap = merge(pmap, Dict(p => 3q)) + prob = Problem(sys, u0map, (0.0, 1.0), _pmap) + test_parameter(prob, p, 3pmap[q]) + + # Should not be solved for: + + # Override dependent default with direct value + @mtkbuild sys = System( + [D(x) ~ q * x + rhss[1], D(y) ~ y * p + rhss[2]], t; defaults = [p => 2q], guesses = [p => 1.0]) + _pmap = merge(pmap, Dict(p => 1.0)) + prob = Problem(sys, u0map, (0.0, 1.0), _pmap) + @test prob.ps[p] ≈ 1.0 + @test prob.f.initialization_data === nothing + + # Non-floating point + @parameters r::Int s::Int + @mtkbuild sys = System( + [D(x) ~ s * x + rhss[1], D(y) ~ y * r + rhss[2]], t; defaults = [s => 2r], guesses = [s => 1.0]) + prob = Problem(sys, u0map, (0.0, 1.0), [r => 1]) + @test prob.ps[r] == 1 + @test prob.ps[s] == 2 + @test prob.f.initialization_data === nothing + + @mtkbuild sys = System( + [D(x) ~ x + rhss[1], p ~ x + y + rhss[2]], t; guesses = [p => 0.0]) + @test_throws ModelingToolkit.MissingParametersError Problem( + sys, [x => 1.0, y => 1.0], (0.0, 1.0)) + end @testset "Null system" begin @variables x(t) y(t) s(t) @@ -718,103 +734,153 @@ end end @testset "Update initializeprob parameters" begin - @variables x(t) y(t) + @variables _x(..) y(t) @parameters p q - @mtkbuild sys = ODESystem( - [D(x) ~ x, p ~ x + y], t; guesses = [x => 0.0, p => 0.0]) - prob = ODEProblem(sys, [y => 1.0], (0.0, 1.0), [p => 3.0]) - @test prob.f.initializeprob.ps[p] ≈ 3.0 - @test init(prob, Tsit5())[x] ≈ 2.0 - prob.ps[p] = 2.0 - @test prob.f.initializeprob.ps[p] ≈ 3.0 - @test init(prob, Tsit5())[x] ≈ 1.0 - ModelingToolkit.defaults(prob.f.sys)[p] = missing - prob2 = remake(prob; u0 = [y => 1.0], p = [p => 3x]) - @test !is_variable(prob2.f.initializeprob, p) && - !is_parameter(prob2.f.initializeprob, p) - @test init(prob2, Tsit5())[x] ≈ 0.5 - @test_nowarn solve(prob2, Tsit5()) + @brownian a b + x = _x(t) + + @testset "$Problem" for (Problem, alg, rhss) in [ + (ODEProblem, Tsit5(), zeros(2)), (SDEProblem, ImplicitEM(), [a, b]), + (DDEProblem, MethodOfSteps(Tsit5()), [_x(t - 0.1), 0.0]), + (SDDEProblem, ImplicitEM(), [_x(t - 0.1) + a, b])] + @mtkbuild sys = System( + [D(x) ~ x + rhss[1], p ~ x + y + rhss[2]], t; guesses = [x => 0.0, p => 0.0]) + prob = Problem(sys, [y => 1.0], (0.0, 1.0), [p => 3.0]) + @test prob.f.initialization_data.initializeprob.ps[p] ≈ 3.0 + @test init(prob, alg)[x] ≈ 2.0 + prob.ps[p] = 2.0 + @test prob.f.initialization_data.initializeprob.ps[p] ≈ 3.0 + @test init(prob, alg)[x] ≈ 1.0 + ModelingToolkit.defaults(prob.f.sys)[p] = missing + prob2 = remake(prob; u0 = [y => 1.0], p = [p => 3x]) + @test !is_variable(prob2.f.initialization_data.initializeprob, p) && + !is_parameter(prob2.f.initialization_data.initializeprob, p) + @test init(prob2, alg)[x] ≈ 0.5 + @test_nowarn solve(prob2, alg) + end end @testset "Equations for dependent parameters" begin - @variables x(t) + @variables _x(..) @parameters p q=5 r - @mtkbuild sys = ODESystem( - D(x) ~ 2x + r, t; parameter_dependencies = [r ~ p + 2q, q ~ p + 3], - guesses = [p => 1.0]) - prob = ODEProblem(sys, [x => 1.0], (0.0, 1.0), [p => missing]) - @test length(equations(ModelingToolkit.get_parent(prob.f.initializeprob.f.sys))) == 4 - integ = init(prob, Tsit5()) - @test integ.ps[p] ≈ 2 + @brownian a + x = _x(t) + + @testset "$Problem" for (Problem, alg, rhss) in [ + (ODEProblem, Tsit5(), 0), (SDEProblem, ImplicitEM(), a), + (DDEProblem, MethodOfSteps(Tsit5()), _x(t - 0.1)), + (SDDEProblem, ImplicitEM(), _x(t - 0.1) + a)] + @mtkbuild sys = System( + [D(x) ~ 2x + r + rhss], t; parameter_dependencies = [r ~ p + 2q, q ~ p + 3], + guesses = [p => 1.0]) + prob = Problem(sys, [x => 1.0], (0.0, 1.0), [p => missing]) + @test length(equations(ModelingToolkit.get_parent(prob.f.initialization_data.initializeprob.f.sys))) == + 4 + integ = init(prob, alg) + @test integ.ps[p] ≈ 2 + end end @testset "Re-creating initialization problem on remake" begin - @variables x(t) y(t) + @variables _x(..) y(t) @parameters p q - @mtkbuild sys = ODESystem( - [D(x) ~ x, p ~ x + y], t; defaults = [p => missing], guesses = [x => 0.0, p => 0.0]) - prob = ODEProblem(sys, [x => 1.0, y => 1.0], (0.0, 1.0)) - @test init(prob, Tsit5()).ps[p] ≈ 2.0 - # nonsensical value for y just to test that equations work - prob2 = remake(prob; u0 = [x => 1.0, y => 2x + exp(t)]) - @test init(prob2, Tsit5()).ps[p] ≈ 4.0 - # solve for `x` given `p` and `y` - prob3 = remake(prob; u0 = [x => nothing, y => 1.0], p = [p => 2x + exp(t)]) - @test init(prob3, Tsit5())[x] ≈ 0.0 - @test_logs (:warn, r"overdetermined") remake( - prob; u0 = [x => 1.0, y => 2.0], p = [p => 4.0]) - prob4 = remake(prob; u0 = [x => 1.0, y => 2.0], p = [p => 4.0]) - @test solve(prob4, Tsit5()).retcode == ReturnCode.InitialFailure - prob5 = remake(prob) - @test init(prob, Tsit5()).ps[p] ≈ 2.0 + @brownian a b + x = _x(t) + + @testset "$Problem" for (Problem, alg, rhss) in [ + (ODEProblem, Tsit5(), zeros(2)), (SDEProblem, ImplicitEM(), [a, b]), + (DDEProblem, MethodOfSteps(Tsit5()), [_x(t - 0.1), 0.0]), + (SDDEProblem, ImplicitEM(), [_x(t - 0.1) + a, b])] + @mtkbuild sys = System( + [D(x) ~ x + rhss[1], p ~ x + y + rhss[2]], t; defaults = [p => missing], guesses = [ + x => 0.0, p => 0.0]) + prob = Problem(sys, [x => 1.0, y => 1.0], (0.0, 1.0)) + @test init(prob, alg).ps[p] ≈ 2.0 + # nonsensical value for y just to test that equations work + prob2 = remake(prob; u0 = [x => 1.0, y => 2x + exp(t)]) + @test init(prob2, alg).ps[p] ≈ 4.0 + # solve for `x` given `p` and `y` + prob3 = remake(prob; u0 = [x => nothing, y => 1.0], p = [p => 2x + exp(t)]) + @test init(prob3, alg)[x] ≈ 0.0 + @test_logs (:warn, r"overdetermined") remake( + prob; u0 = [x => 1.0, y => 2.0], p = [p => 4.0]) + prob4 = remake(prob; u0 = [x => 1.0, y => 2.0], p = [p => 4.0]) + @test solve(prob4, alg).retcode == ReturnCode.InitialFailure + prob5 = remake(prob) + @test init(prob, alg).ps[p] ≈ 2.0 + end end @testset "`remake` changes initialization problem types" begin - @variables x(t) y(t) z(t) + @variables _x(..) y(t) z(t) @parameters p q - @mtkbuild sys = ODESystem( - [D(x) ~ x * p + y * q, y^2 * q + q^2 * x ~ 0, z * p - p^2 * x * z ~ 0], - t; guesses = [x => 0.0, y => 0.0, z => 0.0, p => 0.0, q => 0.0]) - prob = ODEProblem(sys, [x => 1.0], (0.0, 1.0), [p => 1.0, q => missing]) - @test is_variable(prob.f.initializeprob, q) - ps = prob.p - newps = SciMLStructures.replace(Tunable(), ps, ForwardDiff.Dual.(ps.tunable)) - prob2 = remake(prob; p = newps) - @test eltype(prob2.f.initializeprob.u0) <: ForwardDiff.Dual - @test eltype(prob2.f.initializeprob.p.tunable) <: ForwardDiff.Dual - @test prob2.f.initializeprob.u0 ≈ prob.f.initializeprob.u0 - - prob2 = remake(prob; u0 = ForwardDiff.Dual.(prob.u0)) - @test eltype(prob2.f.initializeprob.u0) <: ForwardDiff.Dual - @test eltype(prob2.f.initializeprob.p.tunable) <: Float64 - @test prob2.f.initializeprob.u0 ≈ prob.f.initializeprob.u0 - - prob2 = remake(prob; u0 = ForwardDiff.Dual.(prob.u0), p = newps) - @test eltype(prob2.f.initializeprob.u0) <: ForwardDiff.Dual - @test eltype(prob2.f.initializeprob.p.tunable) <: ForwardDiff.Dual - @test prob2.f.initializeprob.u0 ≈ prob.f.initializeprob.u0 - - prob2 = remake(prob; u0 = [x => ForwardDiff.Dual(1.0)], - p = [p => ForwardDiff.Dual(1.0), q => missing]) - @test eltype(prob2.f.initializeprob.u0) <: ForwardDiff.Dual - @test eltype(prob2.f.initializeprob.p.tunable) <: ForwardDiff.Dual - @test prob2.f.initializeprob.u0 ≈ prob.f.initializeprob.u0 + @brownian a + x = _x(t) + + @testset "$Problem" for (Problem, alg, rhss) in [ + (ODEProblem, Tsit5(), 0), (SDEProblem, ImplicitEM(), a), + (DDEProblem, MethodOfSteps(Tsit5()), _x(t - 0.1)), + (SDDEProblem, ImplicitEM(), _x(t - 0.1) + a)] + @mtkbuild sys = System( + [D(x) ~ x * p + y * q + rhss, y^2 * q + q^2 * x ~ 0, z * p - p^2 * x * z ~ 0], + t; guesses = [x => 0.0, y => 0.0, z => 0.0, p => 0.0, q => 0.0]) + prob = Problem(sys, [x => 1.0], (0.0, 1.0), [p => 1.0, q => missing]) + @test is_variable(prob.f.initialization_data.initializeprob, q) + ps = prob.p + newps = SciMLStructures.replace(Tunable(), ps, ForwardDiff.Dual.(ps.tunable)) + prob2 = remake(prob; p = newps) + @test eltype(prob2.f.initialization_data.initializeprob.u0) <: ForwardDiff.Dual + @test eltype(prob2.f.initialization_data.initializeprob.p.tunable) <: + ForwardDiff.Dual + @test prob2.f.initialization_data.initializeprob.u0 ≈ + prob.f.initialization_data.initializeprob.u0 + + prob2 = remake(prob; u0 = ForwardDiff.Dual.(prob.u0)) + @test eltype(prob2.f.initialization_data.initializeprob.u0) <: ForwardDiff.Dual + @test eltype(prob2.f.initialization_data.initializeprob.p.tunable) <: Float64 + @test prob2.f.initialization_data.initializeprob.u0 ≈ + prob.f.initialization_data.initializeprob.u0 + + prob2 = remake(prob; u0 = ForwardDiff.Dual.(prob.u0), p = newps) + @test eltype(prob2.f.initialization_data.initializeprob.u0) <: ForwardDiff.Dual + @test eltype(prob2.f.initialization_data.initializeprob.p.tunable) <: + ForwardDiff.Dual + @test prob2.f.initialization_data.initializeprob.u0 ≈ + prob.f.initialization_data.initializeprob.u0 + + prob2 = remake(prob; u0 = [x => ForwardDiff.Dual(1.0)], + p = [p => ForwardDiff.Dual(1.0), q => missing]) + @test eltype(prob2.f.initialization_data.initializeprob.u0) <: ForwardDiff.Dual + @test eltype(prob2.f.initialization_data.initializeprob.p.tunable) <: + ForwardDiff.Dual + @test prob2.f.initialization_data.initializeprob.u0 ≈ + prob.f.initialization_data.initializeprob.u0 + end end @testset "`remake` preserves old u0map and pmap" begin - @variables x(t) y(t) + @variables _x(..) y(t) @parameters p - @mtkbuild sys = ODESystem( - [D(x) ~ x + p * y, y^2 + 4y * p^2 ~ x], t; guesses = [y => 1.0, p => 1.0]) - prob = ODEProblem(sys, [x => 1.0], (0.0, 1.0), [p => 1.0]) - @test is_variable(prob.f.initializeprob, y) - prob2 = @test_nowarn remake(prob; p = [p => 3.0]) # ensure no over/under-determined warning - @test is_variable(prob.f.initializeprob, y) - - prob = ODEProblem(sys, [y => 1.0, x => 2.0], (0.0, 1.0), [p => missing]) - @test is_variable(prob.f.initializeprob, p) - prob2 = @test_nowarn remake(prob; u0 = [y => 0.5]) - @test is_variable(prob.f.initializeprob, p) + @brownian a + x = _x(t) + + @testset "$Problem" for (Problem, alg, rhss) in [ + (ODEProblem, Tsit5(), 0), (SDEProblem, ImplicitEM(), a), + (DDEProblem, MethodOfSteps(Tsit5()), _x(t - 0.1)), + (SDDEProblem, ImplicitEM(), _x(t - 0.1) + a)] + @mtkbuild sys = System( + [D(x) ~ x + p * y + rhss, y^2 + 4y * p^2 ~ x], t; guesses = [ + y => 1.0, p => 1.0]) + prob = Problem(sys, [x => 1.0], (0.0, 1.0), [p => 1.0]) + @test is_variable(prob.f.initialization_data.initializeprob, y) + prob2 = @test_nowarn remake(prob; p = [p => 3.0]) # ensure no over/under-determined warning + @test is_variable(prob.f.initialization_data.initializeprob, y) + + prob = Problem(sys, [y => 1.0, x => 2.0], (0.0, 1.0), [p => missing]) + @test is_variable(prob.f.initialization_data.initializeprob, p) + prob2 = @test_nowarn remake(prob; u0 = [y => 0.5]) + @test is_variable(prob.f.initialization_data.initializeprob, p) + end end struct Multiplier{T} From 39bb59c3ccfcd8efee174829c58b2d546864cea9 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 6 Dec 2024 14:12:35 +0530 Subject: [PATCH 0501/2176] fix: handle integer `u0` in `DDEProblem` --- src/systems/diffeqs/abstractodesystem.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/systems/diffeqs/abstractodesystem.jl b/src/systems/diffeqs/abstractodesystem.jl index b3d4903191..40c44b7bae 100644 --- a/src/systems/diffeqs/abstractodesystem.jl +++ b/src/systems/diffeqs/abstractodesystem.jl @@ -923,7 +923,7 @@ function DiffEqBase.DDEProblem{iip}(sys::AbstractODESystem, u0map = [], h_oop, h_iip = eval_or_rgf.(h_gen; eval_expression, eval_module) h(p, t) = h_oop(p, t) h(p::MTKParameters, t) = h_oop(p..., t) - u0 = h(p, tspan[1]) + u0 = float.(h(p, tspan[1])) if u0 !== nothing u0 = u0_constructor(u0) end From 2f2e62524f42d521e69b692b2801d5ca5e90cfc1 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 6 Dec 2024 14:13:00 +0530 Subject: [PATCH 0502/2176] feat: enable creating `InitializationProblem` for non-`AbstractODESystem`s --- src/systems/diffeqs/abstractodesystem.jl | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/systems/diffeqs/abstractodesystem.jl b/src/systems/diffeqs/abstractodesystem.jl index 40c44b7bae..a6607e0c55 100644 --- a/src/systems/diffeqs/abstractodesystem.jl +++ b/src/systems/diffeqs/abstractodesystem.jl @@ -1247,11 +1247,11 @@ Generates a NonlinearProblem or NonlinearLeastSquaresProblem from an ODESystem which represents the initialization, i.e. the calculation of the consistent initial conditions for the given DAE. """ -function InitializationProblem(sys::AbstractODESystem, args...; kwargs...) +function InitializationProblem(sys::AbstractSystem, args...; kwargs...) InitializationProblem{true}(sys, args...; kwargs...) end -function InitializationProblem(sys::AbstractODESystem, t, +function InitializationProblem(sys::AbstractSystem, t, u0map::StaticArray, args...; kwargs...) @@ -1259,11 +1259,11 @@ function InitializationProblem(sys::AbstractODESystem, t, sys, t, u0map, args...; kwargs...) end -function InitializationProblem{true}(sys::AbstractODESystem, args...; kwargs...) +function InitializationProblem{true}(sys::AbstractSystem, args...; kwargs...) InitializationProblem{true, SciMLBase.AutoSpecialize}(sys, args...; kwargs...) end -function InitializationProblem{false}(sys::AbstractODESystem, args...; kwargs...) +function InitializationProblem{false}(sys::AbstractSystem, args...; kwargs...) InitializationProblem{false, SciMLBase.FullSpecialize}(sys, args...; kwargs...) end @@ -1282,8 +1282,8 @@ function Base.showerror(io::IO, e::IncompleteInitializationError) println(io, e.uninit) end -function InitializationProblem{iip, specialize}(sys::AbstractODESystem, - t::Number, u0map = [], +function InitializationProblem{iip, specialize}(sys::AbstractSystem, + t, u0map = [], parammap = DiffEqBase.NullParameters(); guesses = [], check_length = true, @@ -1347,13 +1347,13 @@ function InitializationProblem{iip, specialize}(sys::AbstractODESystem, @warn "Initialization system is underdetermined. $neqs equations for $nunknown unknowns. Initialization will default to using least squares. $(scc_message)To suppress this warning pass warn_initialize_determined = false. To make this warning into an error, pass fully_determined = true" end - parammap = parammap isa DiffEqBase.NullParameters || isempty(parammap) ? - [get_iv(sys) => t] : - merge(todict(parammap), Dict(get_iv(sys) => t)) - parammap = Dict(k => v for (k, v) in parammap if v !== missing) - if isempty(u0map) - u0map = Dict() + parammap = recursive_unwrap(anydict(parammap)) + if t !== nothing + parammap[get_iv(sys)] = t end + filter!(kvp -> kvp[2] !== missing, parammap) + + u0map = to_varmap(u0map, unknowns(sys)) if isempty(guesses) guesses = Dict() end From 671b93feea590a1a7756fd469f9426cc6105766d Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 6 Dec 2024 14:13:10 +0530 Subject: [PATCH 0503/2176] fix: filter kwargs in `SDEProblem` --- src/systems/diffeqs/sdesystem.jl | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/systems/diffeqs/sdesystem.jl b/src/systems/diffeqs/sdesystem.jl index 5e0d0e3208..0d600cfb5f 100644 --- a/src/systems/diffeqs/sdesystem.jl +++ b/src/systems/diffeqs/sdesystem.jl @@ -760,6 +760,8 @@ function DiffEqBase.SDEProblem{iip, specialize}( noise = nothing end + kwargs = filter_kwargs(kwargs) + SDEProblem{iip}(f, u0, tspan, p; callback = cbs, noise, noise_rate_prototype = noise_rate_prototype, kwargs...) end From 9987da0b19e5e2c41aed9af214a215e7dc9b714a Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 6 Dec 2024 14:15:05 +0530 Subject: [PATCH 0504/2176] test: test initialization on `NonlinearProblem` and `NonlinearLeastSquaresProblem` --- test/initializationsystem.jl | 269 ++++++++++++++++++++++++++++------- 1 file changed, 218 insertions(+), 51 deletions(-) diff --git a/test/initializationsystem.jl b/test/initializationsystem.jl index 045b7d2d2c..2745b9d81f 100644 --- a/test/initializationsystem.jl +++ b/test/initializationsystem.jl @@ -583,6 +583,14 @@ sol = solve(oprob_2nd_order_2, Rosenbrock23()) # retcode: Success @test all(sol(1.0, idxs = sys.x) .≈ +exp(1)) && all(sol(1.0, idxs = sys.y) .≈ -exp(1)) end +NonlinearSystemWrapper(eqs, t; kws...) = NonlinearSystem(eqs; kws...) +function NonlinearProblemWrapper(sys, u0, tspan, args...; kwargs...) + NonlinearProblem(sys, u0, args...; kwargs...) +end +function NLLSProblemWrapper(sys, u0, tspan, args...; kwargs...) + NonlinearLeastSquaresProblem(sys, u0, args...; kwargs...) +end + @testset "Initialization of parameters" begin @variables _x(..) y(t) @parameters p q @@ -594,13 +602,37 @@ end # the correct solver. # `rhss` allows adding terms to the end of equations (only 2 equations allowed) to influence # the system type (brownian vars to turn it into an SDE). - @testset "$Problem" for (Problem, alg, rhss) in [ - (ODEProblem, Tsit5(), zeros(2)), (SDEProblem, ImplicitEM(), [a, b]), - (DDEProblem, MethodOfSteps(Tsit5()), [_x(t - 0.1), 0.0]), - (SDDEProblem, ImplicitEM(), [_x(t - 0.1) + a, b])] + @testset "$Problem with $(SciMLBase.parameterless_type(alg))" for (System, Problem, alg, rhss) in [ + (ModelingToolkit.System, ODEProblem, Tsit5(), zeros(2)), + (ModelingToolkit.System, SDEProblem, ImplicitEM(), [a, b]), + (ModelingToolkit.System, DDEProblem, MethodOfSteps(Tsit5()), [_x(t - 0.1), 0.0]), + (ModelingToolkit.System, SDDEProblem, ImplicitEM(), [_x(t - 0.1) + a, b]), + # polyalg cache + (NonlinearSystemWrapper, NonlinearProblemWrapper, + FastShortcutNonlinearPolyalg(), zeros(2)), + # generalized first order cache + (NonlinearSystemWrapper, NonlinearProblemWrapper, NewtonRaphson(), zeros(2)), + # quasi newton cache + (NonlinearSystemWrapper, NonlinearProblemWrapper, Klement(), zeros(2)), + # noinit cache + (NonlinearSystemWrapper, NonlinearProblemWrapper, SimpleNewtonRaphson(), zeros(2)), + # DFSane cache + (NonlinearSystemWrapper, NonlinearProblemWrapper, DFSane(), zeros(2)), + # Least squares + # polyalg cache + (NonlinearSystemWrapper, NLLSProblemWrapper, FastShortcutNLLSPolyalg(), zeros(2)), + # generalized first order cache + (NonlinearSystemWrapper, NLLSProblemWrapper, LevenbergMarquardt(), zeros(2)), + # noinit cache + (NonlinearSystemWrapper, NLLSProblemWrapper, SimpleGaussNewton(), zeros(2)) + ] + is_nlsolve = alg isa SciMLBase.AbstractNonlinearAlgorithm + function test_parameter(prob, sym, val, initialval = zero(val)) @test prob.ps[sym] ≈ initialval - @test init(prob, alg).ps[sym] ≈ val + if !is_nlsolve || prob.u0 !== nothing + @test init(prob, alg).ps[sym] ≈ val + end @test solve(prob, alg).ps[sym] ≈ val end function test_initializesystem(sys, u0map, pmap, p, equation) @@ -609,6 +641,8 @@ end @test is_variable(isys, p) @test equation in equations(isys) || (0 ~ -equation.rhs) in equations(isys) end + D = is_nlsolve ? v -> v^3 : Differential(t) + u0map = Dict(x => 1.0, y => 1.0) pmap = Dict() pmap[q] = 1.0 @@ -669,16 +703,14 @@ end prob = Problem(sys, u0map, (0.0, 1.0), _pmap) test_parameter(prob, p, _pmap[q]) test_initializesystem(sys, u0map, _pmap, p, 0 ~ q - p) - # Problem dependent value with guess, no `missing` @mtkbuild sys = System( - [D(x) ~ x * q + rhss[1], D(y) ~ y * p + rhss[2]], t; guesses = [p => 0.0]) + [D(x) ~ y * q + p + rhss[1], D(y) ~ x * p + q + rhss[2]], t; guesses = [p => 0.0]) _pmap = merge(pmap, Dict(p => 3q)) prob = Problem(sys, u0map, (0.0, 1.0), _pmap) test_parameter(prob, p, 3pmap[q]) # Should not be solved for: - # Override dependent default with direct value @mtkbuild sys = System( [D(x) ~ q * x + rhss[1], D(y) ~ y * p + rhss[2]], t; defaults = [p => 2q], guesses = [p => 1.0]) @@ -700,6 +732,14 @@ end [D(x) ~ x + rhss[1], p ~ x + y + rhss[2]], t; guesses = [p => 0.0]) @test_throws ModelingToolkit.MissingParametersError Problem( sys, [x => 1.0, y => 1.0], (0.0, 1.0)) + + # Unsatisfiable initialization + prob = Problem(sys, [x => 1.0, y => 1.0], (0.0, 1.0), + [p => 2.0]; initialization_eqs = [x^2 + y^2 ~ 3]) + @test prob.f.initialization_data !== nothing + @test solve(prob, alg).retcode == ReturnCode.InitialFailure + cache = init(prob, alg) + @test solve!(cache).retcode == ReturnCode.InitialFailure end @testset "Null system" begin @@ -707,7 +747,9 @@ end @parameters x0 y0 @mtkbuild sys = ODESystem([x ~ x0, y ~ y0, s ~ x + y], t; guesses = [y0 => 0.0]) prob = ODEProblem(sys, [s => 1.0], (0.0, 1.0), [x0 => 0.3, y0 => missing]) - test_parameter(prob, y0, 0.7) + @test prob.ps[y0] ≈ 0.0 + @test init(prob, Tsit5()).ps[y0] ≈ 0.7 + @test solve(prob, Tsit5()).ps[y0] ≈ 0.7 end using ModelingToolkitStandardLibrary.Mechanical.TranslationalModelica: Fixed, Mass, @@ -730,7 +772,9 @@ end systems = [fixed, spring, mass, gravity, constant, damper], guesses = [spring.s_rel0 => 1.0]) prob = ODEProblem(sys, [], (0.0, 1.0), [spring.s_rel0 => missing]) - test_parameter(prob, spring.s_rel0, -3.905) + @test prob.ps[spring.s_rel0] ≈ 0.0 + @test init(prob, Tsit5()).ps[spring.s_rel0] ≈ -3.905 + @test solve(prob, Tsit5()).ps[spring.s_rel0] ≈ -3.905 end @testset "Update initializeprob parameters" begin @@ -739,10 +783,33 @@ end @brownian a b x = _x(t) - @testset "$Problem" for (Problem, alg, rhss) in [ - (ODEProblem, Tsit5(), zeros(2)), (SDEProblem, ImplicitEM(), [a, b]), - (DDEProblem, MethodOfSteps(Tsit5()), [_x(t - 0.1), 0.0]), - (SDDEProblem, ImplicitEM(), [_x(t - 0.1) + a, b])] + @testset "$Problem with $(SciMLBase.parameterless_type(typeof(alg)))" for (System, Problem, alg, rhss) in [ + (ModelingToolkit.System, ODEProblem, Tsit5(), zeros(2)), + (ModelingToolkit.System, SDEProblem, ImplicitEM(), [a, b]), + (ModelingToolkit.System, DDEProblem, MethodOfSteps(Tsit5()), [_x(t - 0.1), 0.0]), + (ModelingToolkit.System, SDDEProblem, ImplicitEM(), [_x(t - 0.1) + a, b]), + # polyalg cache + (NonlinearSystemWrapper, NonlinearProblemWrapper, + FastShortcutNonlinearPolyalg(), zeros(2)), + # generalized first order cache + (NonlinearSystemWrapper, NonlinearProblemWrapper, NewtonRaphson(), zeros(2)), + # quasi newton cache + (NonlinearSystemWrapper, NonlinearProblemWrapper, Klement(), zeros(2)), + # noinit cache + (NonlinearSystemWrapper, NonlinearProblemWrapper, SimpleNewtonRaphson(), zeros(2)), + # DFSane cache + (NonlinearSystemWrapper, NonlinearProblemWrapper, DFSane(), zeros(2)), + # Least squares + # polyalg cache + (NonlinearSystemWrapper, NLLSProblemWrapper, FastShortcutNLLSPolyalg(), zeros(2)), + # generalized first order cache + (NonlinearSystemWrapper, NLLSProblemWrapper, LevenbergMarquardt(), zeros(2)), + # noinit cache + (NonlinearSystemWrapper, NLLSProblemWrapper, SimpleGaussNewton(), zeros(2)) + ] + is_nlsolve = alg isa SciMLBase.AbstractNonlinearAlgorithm + D = is_nlsolve ? v -> v^3 : Differential(t) + @mtkbuild sys = System( [D(x) ~ x + rhss[1], p ~ x + y + rhss[2]], t; guesses = [x => 0.0, p => 0.0]) prob = Problem(sys, [y => 1.0], (0.0, 1.0), [p => 3.0]) @@ -766,10 +833,33 @@ end @brownian a x = _x(t) - @testset "$Problem" for (Problem, alg, rhss) in [ - (ODEProblem, Tsit5(), 0), (SDEProblem, ImplicitEM(), a), - (DDEProblem, MethodOfSteps(Tsit5()), _x(t - 0.1)), - (SDDEProblem, ImplicitEM(), _x(t - 0.1) + a)] + @testset "$Problem with $(SciMLBase.parameterless_type(typeof(alg)))" for (System, Problem, alg, rhss) in [ + (ModelingToolkit.System, ODEProblem, Tsit5(), 0), + (ModelingToolkit.System, SDEProblem, ImplicitEM(), a), + (ModelingToolkit.System, DDEProblem, MethodOfSteps(Tsit5()), _x(t - 0.1)), + (ModelingToolkit.System, SDDEProblem, ImplicitEM(), _x(t - 0.1) + a), + # polyalg cache + (NonlinearSystemWrapper, NonlinearProblemWrapper, + FastShortcutNonlinearPolyalg(), 0), + # generalized first order cache + (NonlinearSystemWrapper, NonlinearProblemWrapper, NewtonRaphson(), 0), + # quasi newton cache + (NonlinearSystemWrapper, NonlinearProblemWrapper, Klement(), 0), + # noinit cache + (NonlinearSystemWrapper, NonlinearProblemWrapper, SimpleNewtonRaphson(), 0), + # DFSane cache + (NonlinearSystemWrapper, NonlinearProblemWrapper, DFSane(), 0), + # Least squares + # polyalg cache + (NonlinearSystemWrapper, NLLSProblemWrapper, FastShortcutNLLSPolyalg(), 0), + # generalized first order cache + (NonlinearSystemWrapper, NLLSProblemWrapper, LevenbergMarquardt(), 0), + # noinit cache + (NonlinearSystemWrapper, NLLSProblemWrapper, SimpleGaussNewton(), 0) + ] + is_nlsolve = alg isa SciMLBase.AbstractNonlinearAlgorithm + D = is_nlsolve ? v -> v^3 : Differential(t) + @mtkbuild sys = System( [D(x) ~ 2x + r + rhss], t; parameter_dependencies = [r ~ p + 2q, q ~ p + 3], guesses = [p => 1.0]) @@ -787,21 +877,44 @@ end @brownian a b x = _x(t) - @testset "$Problem" for (Problem, alg, rhss) in [ - (ODEProblem, Tsit5(), zeros(2)), (SDEProblem, ImplicitEM(), [a, b]), - (DDEProblem, MethodOfSteps(Tsit5()), [_x(t - 0.1), 0.0]), - (SDDEProblem, ImplicitEM(), [_x(t - 0.1) + a, b])] + @testset "$Problem with $(SciMLBase.parameterless_type(typeof(alg)))" for (System, Problem, alg, rhss) in [ + (ModelingToolkit.System, ODEProblem, Tsit5(), zeros(2)), + (ModelingToolkit.System, SDEProblem, ImplicitEM(), [a, b]), + (ModelingToolkit.System, DDEProblem, MethodOfSteps(Tsit5()), [_x(t - 0.1), 0.0]), + (ModelingToolkit.System, SDDEProblem, ImplicitEM(), [_x(t - 0.1) + a, b]), + # polyalg cache + (NonlinearSystemWrapper, NonlinearProblemWrapper, + FastShortcutNonlinearPolyalg(), zeros(2)), + # generalized first order cache + (NonlinearSystemWrapper, NonlinearProblemWrapper, NewtonRaphson(), zeros(2)), + # quasi newton cache + (NonlinearSystemWrapper, NonlinearProblemWrapper, Klement(), zeros(2)), + # noinit cache + (NonlinearSystemWrapper, NonlinearProblemWrapper, SimpleNewtonRaphson(), zeros(2)), + # DFSane cache + (NonlinearSystemWrapper, NonlinearProblemWrapper, DFSane(), zeros(2)), + # Least squares + # polyalg cache + (NonlinearSystemWrapper, NLLSProblemWrapper, FastShortcutNLLSPolyalg(), zeros(2)), + # generalized first order cache + (NonlinearSystemWrapper, NLLSProblemWrapper, LevenbergMarquardt(), zeros(2)), + # noinit cache + (NonlinearSystemWrapper, NLLSProblemWrapper, SimpleGaussNewton(), zeros(2)) + ] + is_nlsolve = alg isa SciMLBase.AbstractNonlinearAlgorithm + D = is_nlsolve ? v -> v^3 : Differential(t) + @mtkbuild sys = System( [D(x) ~ x + rhss[1], p ~ x + y + rhss[2]], t; defaults = [p => missing], guesses = [ x => 0.0, p => 0.0]) prob = Problem(sys, [x => 1.0, y => 1.0], (0.0, 1.0)) @test init(prob, alg).ps[p] ≈ 2.0 # nonsensical value for y just to test that equations work - prob2 = remake(prob; u0 = [x => 1.0, y => 2x + exp(t)]) - @test init(prob2, alg).ps[p] ≈ 4.0 + prob2 = remake(prob; u0 = [x => 1.0, y => 2x + exp(x)]) + @test init(prob2, alg).ps[p] ≈ 3 + exp(1) # solve for `x` given `p` and `y` - prob3 = remake(prob; u0 = [x => nothing, y => 1.0], p = [p => 2x + exp(t)]) - @test init(prob3, alg)[x] ≈ 0.0 + prob3 = remake(prob; u0 = [x => nothing, y => 1.0], p = [p => 2x + exp(y)]) + @test init(prob3, alg)[x] ≈ 1 - exp(1) @test_logs (:warn, r"overdetermined") remake( prob; u0 = [x => 1.0, y => 2.0], p = [p => 4.0]) prob4 = remake(prob; u0 = [x => 1.0, y => 2.0], p = [p => 4.0]) @@ -817,44 +930,73 @@ end @brownian a x = _x(t) - @testset "$Problem" for (Problem, alg, rhss) in [ - (ODEProblem, Tsit5(), 0), (SDEProblem, ImplicitEM(), a), - (DDEProblem, MethodOfSteps(Tsit5()), _x(t - 0.1)), - (SDDEProblem, ImplicitEM(), _x(t - 0.1) + a)] + @testset "$Problem with $(SciMLBase.parameterless_type(typeof(alg)))" for (System, Problem, alg, rhss) in [ + (ModelingToolkit.System, ODEProblem, Tsit5(), 0), + (ModelingToolkit.System, SDEProblem, ImplicitEM(), a), + (ModelingToolkit.System, DDEProblem, MethodOfSteps(Tsit5()), _x(t - 0.1)), + (ModelingToolkit.System, SDDEProblem, ImplicitEM(), _x(t - 0.1) + a), + # polyalg cache + (NonlinearSystemWrapper, NonlinearProblemWrapper, + FastShortcutNonlinearPolyalg(), 0), + # generalized first order cache + (NonlinearSystemWrapper, NonlinearProblemWrapper, NewtonRaphson(), 0), + # quasi newton cache + (NonlinearSystemWrapper, NonlinearProblemWrapper, Klement(), 0), + # noinit cache + (NonlinearSystemWrapper, NonlinearProblemWrapper, SimpleNewtonRaphson(), 0), + # DFSane cache + (NonlinearSystemWrapper, NonlinearProblemWrapper, DFSane(), 0), + # Least squares + # polyalg cache + (NonlinearSystemWrapper, NLLSProblemWrapper, FastShortcutNLLSPolyalg(), 0), + # generalized first order cache + (NonlinearSystemWrapper, NLLSProblemWrapper, LevenbergMarquardt(), 0), + # noinit cache + (NonlinearSystemWrapper, NLLSProblemWrapper, SimpleGaussNewton(), 0) + ] + is_nlsolve = alg isa SciMLBase.AbstractNonlinearAlgorithm + D = is_nlsolve ? v -> v^3 : Differential(t) + alge_eqs = [y^2 * q + q^2 * x ~ 0, z * p - p^2 * x * z ~ 0] + @mtkbuild sys = System( - [D(x) ~ x * p + y * q + rhss, y^2 * q + q^2 * x ~ 0, z * p - p^2 * x * z ~ 0], + [D(x) ~ x * p + y^2 * q + rhss; alge_eqs], t; guesses = [x => 0.0, y => 0.0, z => 0.0, p => 0.0, q => 0.0]) - prob = Problem(sys, [x => 1.0], (0.0, 1.0), [p => 1.0, q => missing]) + prob = Problem(sys, [x => 1.0], (0.0, 1.0), [p => 1.0, q => missing]; + initialization_eqs = is_nlsolve ? alge_eqs : []) @test is_variable(prob.f.initialization_data.initializeprob, q) ps = prob.p newps = SciMLStructures.replace(Tunable(), ps, ForwardDiff.Dual.(ps.tunable)) prob2 = remake(prob; p = newps) - @test eltype(prob2.f.initialization_data.initializeprob.u0) <: ForwardDiff.Dual + @test eltype(state_values(prob2.f.initialization_data.initializeprob)) <: + ForwardDiff.Dual @test eltype(prob2.f.initialization_data.initializeprob.p.tunable) <: ForwardDiff.Dual - @test prob2.f.initialization_data.initializeprob.u0 ≈ - prob.f.initialization_data.initializeprob.u0 + @test state_values(prob2.f.initialization_data.initializeprob) ≈ + state_values(prob.f.initialization_data.initializeprob) prob2 = remake(prob; u0 = ForwardDiff.Dual.(prob.u0)) - @test eltype(prob2.f.initialization_data.initializeprob.u0) <: ForwardDiff.Dual + @test eltype(state_values(prob2.f.initialization_data.initializeprob)) <: + ForwardDiff.Dual @test eltype(prob2.f.initialization_data.initializeprob.p.tunable) <: Float64 - @test prob2.f.initialization_data.initializeprob.u0 ≈ - prob.f.initialization_data.initializeprob.u0 + @test state_values(prob2.f.initialization_data.initializeprob) ≈ + state_values(prob.f.initialization_data.initializeprob) prob2 = remake(prob; u0 = ForwardDiff.Dual.(prob.u0), p = newps) - @test eltype(prob2.f.initialization_data.initializeprob.u0) <: ForwardDiff.Dual + @test eltype(state_values(prob2.f.initialization_data.initializeprob)) <: + ForwardDiff.Dual @test eltype(prob2.f.initialization_data.initializeprob.p.tunable) <: ForwardDiff.Dual - @test prob2.f.initialization_data.initializeprob.u0 ≈ - prob.f.initialization_data.initializeprob.u0 + @test state_values(prob2.f.initialization_data.initializeprob) ≈ + state_values(prob.f.initialization_data.initializeprob) prob2 = remake(prob; u0 = [x => ForwardDiff.Dual(1.0)], p = [p => ForwardDiff.Dual(1.0), q => missing]) - @test eltype(prob2.f.initialization_data.initializeprob.u0) <: ForwardDiff.Dual + @test eltype(state_values(prob2.f.initialization_data.initializeprob)) <: + ForwardDiff.Dual @test eltype(prob2.f.initialization_data.initializeprob.p.tunable) <: ForwardDiff.Dual - @test prob2.f.initialization_data.initializeprob.u0 ≈ - prob.f.initialization_data.initializeprob.u0 + @test state_values(prob2.f.initialization_data.initializeprob) ≈ + state_values(prob.f.initialization_data.initializeprob) end end @@ -864,19 +1006,44 @@ end @brownian a x = _x(t) - @testset "$Problem" for (Problem, alg, rhss) in [ - (ODEProblem, Tsit5(), 0), (SDEProblem, ImplicitEM(), a), - (DDEProblem, MethodOfSteps(Tsit5()), _x(t - 0.1)), - (SDDEProblem, ImplicitEM(), _x(t - 0.1) + a)] + @testset "$Problem with $(SciMLBase.parameterless_type(typeof(alg)))" for (System, Problem, alg, rhss) in [ + (ModelingToolkit.System, ODEProblem, Tsit5(), 0), + (ModelingToolkit.System, SDEProblem, ImplicitEM(), a), + (ModelingToolkit.System, DDEProblem, MethodOfSteps(Tsit5()), _x(t - 0.1)), + (ModelingToolkit.System, SDDEProblem, ImplicitEM(), _x(t - 0.1) + a), + # polyalg cache + (NonlinearSystemWrapper, NonlinearProblemWrapper, + FastShortcutNonlinearPolyalg(), 0), + # generalized first order cache + (NonlinearSystemWrapper, NonlinearProblemWrapper, NewtonRaphson(), 0), + # quasi newton cache + (NonlinearSystemWrapper, NonlinearProblemWrapper, Klement(), 0), + # noinit cache + (NonlinearSystemWrapper, NonlinearProblemWrapper, SimpleNewtonRaphson(), 0), + # DFSane cache + (NonlinearSystemWrapper, NonlinearProblemWrapper, DFSane(), 0), + # Least squares + # polyalg cache + (NonlinearSystemWrapper, NLLSProblemWrapper, FastShortcutNLLSPolyalg(), 0), + # generalized first order cache + (NonlinearSystemWrapper, NLLSProblemWrapper, LevenbergMarquardt(), 0), + # noinit cache + (NonlinearSystemWrapper, NLLSProblemWrapper, SimpleGaussNewton(), 0) + ] + is_nlsolve = alg isa SciMLBase.AbstractNonlinearAlgorithm + D = is_nlsolve ? v -> v^3 : Differential(t) + alge_eqs = [y^2 + 4y * p^2 ~ x^3] @mtkbuild sys = System( - [D(x) ~ x + p * y + rhss, y^2 + 4y * p^2 ~ x], t; guesses = [ + [D(x) ~ x + p * y^2 + rhss; alge_eqs], t; guesses = [ y => 1.0, p => 1.0]) - prob = Problem(sys, [x => 1.0], (0.0, 1.0), [p => 1.0]) + prob = Problem(sys, [x => 1.0], (0.0, 1.0), [p => 1.0]; + initialization_eqs = is_nlsolve ? alge_eqs : []) @test is_variable(prob.f.initialization_data.initializeprob, y) prob2 = @test_nowarn remake(prob; p = [p => 3.0]) # ensure no over/under-determined warning @test is_variable(prob.f.initialization_data.initializeprob, y) - prob = Problem(sys, [y => 1.0, x => 2.0], (0.0, 1.0), [p => missing]) + prob = Problem(sys, [y => 1.0, x => 2.0], (0.0, 1.0), [p => missing]; + initialization_eqs = is_nlsolve ? alge_eqs : []) @test is_variable(prob.f.initialization_data.initializeprob, p) prob2 = @test_nowarn remake(prob; u0 = [y => 0.5]) @test is_variable(prob.f.initialization_data.initializeprob, p) From 51eeeeb27ee5094424bd92eb589f8b99f843c592 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 6 Dec 2024 17:43:57 +0530 Subject: [PATCH 0505/2176] fix: store and propagate `initialization_eqs` provided to Problem --- src/systems/nonlinear/initializesystem.jl | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/systems/nonlinear/initializesystem.jl b/src/systems/nonlinear/initializesystem.jl index e32f967f4a..cf6c6d4d42 100644 --- a/src/systems/nonlinear/initializesystem.jl +++ b/src/systems/nonlinear/initializesystem.jl @@ -21,6 +21,7 @@ function generate_initializesystem(sys::AbstractSystem; eqs_ics = Equation[] defs = copy(defaults(sys)) # copy so we don't modify sys.defaults additional_guesses = anydict(guesses) + additional_initialization_eqs = Vector{Equation}(initialization_eqs) guesses = merge(get_guesses(sys), additional_guesses) idxs_diff = isdiffeq.(eqs) @@ -191,7 +192,7 @@ function generate_initializesystem(sys::AbstractSystem; defs[k] = substitute(defs[k], paramsubs) end meta = InitializationSystemMetadata( - anydict(u0map), anydict(pmap), additional_guesses, extra_metadata) + anydict(u0map), anydict(pmap), additional_guesses, additional_initialization_eqs, extra_metadata) return NonlinearSystem(eqs_ics, vars, pars; @@ -207,6 +208,7 @@ struct InitializationSystemMetadata u0map::Dict{Any, Any} pmap::Dict{Any, Any} additional_guesses::Dict{Any, Any} + additional_initialization_eqs::Vector{Equation} extra_metadata::NamedTuple end @@ -299,6 +301,7 @@ function SciMLBase.remake_initialization_data( defs = defaults(sys) cmap, cs = get_cmap(sys) use_scc = true + initialization_eqs = Equation[] if SciMLBase.has_initializeprob(odefn) oldsys = odefn.initialization_data.initializeprob.f.sys @@ -308,6 +311,7 @@ function SciMLBase.remake_initialization_data( pmap = merge(meta.pmap, pmap) merge!(guesses, meta.additional_guesses) use_scc = get(meta.extra_metadata, :use_scc, true) + initialization_eqs = meta.additional_initialization_eqs end else # there is no initializeprob, so the original problem construction @@ -348,7 +352,7 @@ function SciMLBase.remake_initialization_data( op, missing_unknowns, missing_pars = build_operating_point( u0map, pmap, defs, cmap, dvs, ps) kws = maybe_build_initialization_problem( - sys, op, u0map, pmap, t0, defs, guesses, missing_unknowns; use_scc) + sys, op, u0map, pmap, t0, defs, guesses, missing_unknowns; use_scc, initialization_eqs) return get(kws, :initialization_data, nothing) end From 96f8d5de87813b300b61e3d8359b7231ec661529 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Sat, 14 Dec 2024 13:18:38 +0530 Subject: [PATCH 0506/2176] build: bump compats --- Project.toml | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/Project.toml b/Project.toml index 3c6126b7c0..d672c007c2 100644 --- a/Project.toml +++ b/Project.toml @@ -89,6 +89,7 @@ ConstructionBase = "1" DataInterpolations = "6.4" DataStructures = "0.17, 0.18" DeepDiffs = "1" +DelayDiffEq = "5.50" DiffEqBase = "6.157" DiffEqCallbacks = "2.16, 3, 4" DiffEqNoiseProcess = "5" @@ -117,7 +118,7 @@ Libdl = "1" LinearAlgebra = "1" MLStyle = "0.4.17" NaNMath = "0.3, 1" -NonlinearSolve = "3.14, 4" +NonlinearSolve = "4.3" OffsetArrays = "1" OrderedCollections = "1" OrdinaryDiffEq = "6.82.0" @@ -129,7 +130,7 @@ RecursiveArrayTools = "3.26" Reexport = "0.2, 1" RuntimeGeneratedFunctions = "0.5.9" SCCNonlinearSolve = "1.0.0" -SciMLBase = "2.66" +SciMLBase = "2.68.1" SciMLStructures = "1.0" Serialization = "1" Setfield = "0.7, 0.8, 1" @@ -137,7 +138,9 @@ SimpleNonlinearSolve = "0.1.0, 1, 2" SparseArrays = "1" SpecialFunctions = "0.7, 0.8, 0.9, 0.10, 1.0, 2" StaticArrays = "0.10, 0.11, 0.12, 1.0" -SymbolicIndexingInterface = "0.3.35" +StochasticDiffEq = "6.72.1" +StochasticDelayDiffEq = "1.8.1" +SymbolicIndexingInterface = "0.3.36" SymbolicUtils = "3.7" Symbolics = "6.19" URIs = "1" From 0a881e71d5f1b5fe77e170713e78b5a5ab3ec023 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 16 Dec 2024 16:22:15 +0530 Subject: [PATCH 0507/2176] fix: better handle reconstructing initializeprob with new types --- src/systems/diffeqs/abstractodesystem.jl | 5 ++ src/systems/nonlinear/initializesystem.jl | 69 ++++++++++------------- 2 files changed, 34 insertions(+), 40 deletions(-) diff --git a/src/systems/diffeqs/abstractodesystem.jl b/src/systems/diffeqs/abstractodesystem.jl index a6607e0c55..dd4165cb57 100644 --- a/src/systems/diffeqs/abstractodesystem.jl +++ b/src/systems/diffeqs/abstractodesystem.jl @@ -1310,6 +1310,11 @@ function InitializationProblem{iip, specialize}(sys::AbstractSystem, pmap = parammap, guesses, extra_metadata = (; use_scc)); fully_determined) end + meta = get_metadata(isys) + if meta isa InitializationSystemMetadata + @set! isys.metadata.oop_reconstruct_u0_p = ReconstructInitializeprob(sys, isys) + end + ts = get_tearing_state(isys) unassigned_vars = StructuralTransformations.singular_check(ts) if warn_initialize_determined && !isempty(unassigned_vars) diff --git a/src/systems/nonlinear/initializesystem.jl b/src/systems/nonlinear/initializesystem.jl index cf6c6d4d42..602fc7cac9 100644 --- a/src/systems/nonlinear/initializesystem.jl +++ b/src/systems/nonlinear/initializesystem.jl @@ -192,7 +192,8 @@ function generate_initializesystem(sys::AbstractSystem; defs[k] = substitute(defs[k], paramsubs) end meta = InitializationSystemMetadata( - anydict(u0map), anydict(pmap), additional_guesses, additional_initialization_eqs, extra_metadata) + anydict(u0map), anydict(pmap), additional_guesses, + additional_initialization_eqs, extra_metadata, nothing) return NonlinearSystem(eqs_ics, vars, pars; @@ -204,12 +205,30 @@ function generate_initializesystem(sys::AbstractSystem; kwargs...) end +struct ReconstructInitializeprob + getter::Any + setter::Any +end + +function ReconstructInitializeprob(srcsys::AbstractSystem, dstsys::AbstractSystem) + syms = [unknowns(dstsys); + reduce(vcat, reorder_parameters(dstsys, parameters(dstsys)); init = [])] + getter = getu(srcsys, syms) + setter = setsym_oop(dstsys, syms) + return ReconstructInitializeprob(getter, setter) +end + +function (rip::ReconstructInitializeprob)(srcvalp, dstvalp) + rip.setter(dstvalp, rip.getter(srcvalp)) +end + struct InitializationSystemMetadata u0map::Dict{Any, Any} pmap::Dict{Any, Any} additional_guesses::Dict{Any, Any} additional_initialization_eqs::Vector{Equation} extra_metadata::NamedTuple + oop_reconstruct_u0_p::Union{Nothing, ReconstructInitializeprob} end function is_parameter_solvable(p, pmap, defs, guesses) @@ -239,45 +258,15 @@ function SciMLBase.remake_initialization_data( if !SciMLBase.has_sys(oldinitprob.f) || !(oldinitprob.f.sys isa NonlinearSystem) return oldinitdata end - pidxs = ParameterIndex[] - pvals = [] - u0idxs = Int[] - u0vals = [] - for sym in variable_symbols(oldinitprob) - if is_variable(sys, sym) || has_observed_with_lhs(sys, sym) - u0 !== missing || continue - idx = variable_index(oldinitprob, sym) - push!(u0idxs, idx) - push!(u0vals, eltype(u0)(state_values(oldinitprob, idx))) - else - p !== missing || continue - idx = variable_index(oldinitprob, sym) - push!(u0idxs, idx) - push!(u0vals, typeof(getp(sys, sym)(p))(state_values(oldinitprob, idx))) - end - end - if p !== missing - for sym in parameter_symbols(oldinitprob) - push!(pidxs, parameter_index(oldinitprob, sym)) - if is_time_dependent(sys) && isequal(sym, get_iv(sys)) - push!(pvals, t0) - else - push!(pvals, getp(sys, sym)(p)) - end - end - end - if isempty(u0idxs) - newu0 = state_values(oldinitprob) - else - newu0 = remake_buffer( - oldinitprob.f.sys, state_values(oldinitprob), u0idxs, u0vals) - end - if isempty(pidxs) - newp = parameter_values(oldinitprob) + oldinitsys = oldinitprob.f.sys + meta = get_metadata(oldinitsys) + if meta isa InitializationSystemMetadata && meta.oop_reconstruct_u0_p !== nothing + reconstruct_fn = meta.oop_reconstruct_u0_p else - newp = remake_buffer( - oldinitprob.f.sys, parameter_values(oldinitprob), pidxs, pvals) + reconstruct_fn = ReconstructInitializeprob(sys, oldinitsys) end + new_initu0, new_initp = reconstruct_fn( + ProblemState(; u = newu0, p = newp, t = t0), oldinitprob) if oldinitprob.f.resid_prototype === nothing newf = oldinitprob.f else @@ -285,9 +274,9 @@ function SciMLBase.remake_initialization_data( SciMLBase.isinplace(oldinitprob.f), SciMLBase.specialization(oldinitprob.f)}( oldinitprob.f; resid_prototype = calculate_resid_prototype( - length(oldinitprob.f.resid_prototype), newu0, newp)) + length(oldinitprob.f.resid_prototype), new_initu0, new_initp)) end - initprob = remake(oldinitprob; f = newf, u0 = newu0, p = newp) + initprob = remake(oldinitprob; f = newf, u0 = new_initu0, p = new_initp) return SciMLBase.OverrideInitData(initprob, oldinitdata.update_initializeprob!, oldinitdata.initializeprobmap, oldinitdata.initializeprobpmap) end From 2e07200e03af428b127814af85f48fdeccdaf574 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 16 Dec 2024 16:22:51 +0530 Subject: [PATCH 0508/2176] test: fix incorrect initial values in tests --- test/nonlinearsystem.jl | 2 +- test/reduction.jl | 4 +--- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/test/nonlinearsystem.jl b/test/nonlinearsystem.jl index f766ca0131..cbd95a50d3 100644 --- a/test/nonlinearsystem.jl +++ b/test/nonlinearsystem.jl @@ -293,7 +293,7 @@ sys = structural_simplify(ns; conservative = true) eqs = [0 ~ σ * (y - x) 0 ~ x * (ρ - z) - y 0 ~ x * y - β * z] - guesses = [x => 1.0, y => 0.0, z => 0.0] + guesses = [x => 1.0, z => 0.0] ps = [σ => 10.0, ρ => 26.0, β => 8 / 3] @mtkbuild ns = NonlinearSystem(eqs) diff --git a/test/reduction.jl b/test/reduction.jl index 6d7a05b99e..fa9029a652 100644 --- a/test/reduction.jl +++ b/test/reduction.jl @@ -158,9 +158,7 @@ eqs = [u1 ~ u2 reducedsys = structural_simplify(sys) @test length(observed(reducedsys)) == 2 -u0 = [u1 => 1 - u2 => 1 - u3 => 0.3] +u0 = [u2 => 1] pp = [2] nlprob = NonlinearProblem(reducedsys, u0, [p => pp[1]]) reducedsol = solve(nlprob, NewtonRaphson()) From 878122305e616c0d0b4a40df493d58deb15d8227 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 24 Dec 2024 15:33:10 +0530 Subject: [PATCH 0509/2176] fix: fix `flatten_equations` for higher-dimension array equations --- src/systems/diffeqs/abstractodesystem.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/systems/diffeqs/abstractodesystem.jl b/src/systems/diffeqs/abstractodesystem.jl index f4e29346ff..8e7b2ac97e 100644 --- a/src/systems/diffeqs/abstractodesystem.jl +++ b/src/systems/diffeqs/abstractodesystem.jl @@ -1230,7 +1230,7 @@ function flatten_equations(eqs) error("LHS ($(eq.lhs)) and RHS ($(eq.rhs)) must either both be array expressions or both scalar") size(eq.lhs) == size(eq.rhs) || error("Size of LHS ($(eq.lhs)) and RHS ($(eq.rhs)) must match: got $(size(eq.lhs)) and $(size(eq.rhs))") - return collect(eq.lhs) .~ collect(eq.rhs) + return vec(collect(eq.lhs) .~ collect(eq.rhs)) else eq end From 994b2db06507c26826e4950d3f70e19b69d6e09c Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 24 Dec 2024 15:34:38 +0530 Subject: [PATCH 0510/2176] fix: fix `timeseries_parameter_index` for array symbolics --- src/systems/index_cache.jl | 4 +++- test/symbolic_indexing_interface.jl | 11 +++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/systems/index_cache.jl b/src/systems/index_cache.jl index 4fc2f18bfd..623623da42 100644 --- a/src/systems/index_cache.jl +++ b/src/systems/index_cache.jl @@ -404,6 +404,7 @@ function SymbolicIndexingInterface.timeseries_parameter_index(ic::IndexCache, sy sym = get(ic.symbol_to_variable, sym, nothing) sym === nothing && return nothing end + sym = unwrap(sym) idx = check_index_map(ic.discrete_idx, sym) idx === nothing || return ParameterTimeseriesIndex(idx.clock_idx, (idx.buffer_idx, idx.idx_in_clock)) @@ -411,7 +412,8 @@ function SymbolicIndexingInterface.timeseries_parameter_index(ic::IndexCache, sy args = arguments(sym) idx = timeseries_parameter_index(ic, args[1]) idx === nothing && return nothing - ParameterIndex(idx.portion, (idx.idx..., args[2:end]...), idx.validate_size) + return ParameterTimeseriesIndex( + idx.timeseries_idx, (idx.parameter_idx..., args[2:end]...)) end function check_index_map(idxmap, sym) diff --git a/test/symbolic_indexing_interface.jl b/test/symbolic_indexing_interface.jl index e46c197dde..4a7cd926b4 100644 --- a/test/symbolic_indexing_interface.jl +++ b/test/symbolic_indexing_interface.jl @@ -224,3 +224,14 @@ end end @test isempty(get_all_timeseries_indexes(sys, a)) end + +@testset "`timeseries_parameter_index` on unwrapped scalarized timeseries parameter" begin + @variables x(t)[1:2] + @parameters p(t)[1:2, 1:2] + ev = [x[1] ~ 2.0] => [p ~ -ones(2, 2)] + @mtkbuild sys = ODESystem(D(x) ~ p * x, t; continuous_events = [ev]) + p = ModelingToolkit.unwrap(p) + @test timeseries_parameter_index(sys, p) === ParameterTimeseriesIndex(1, (1, 1)) + @test timeseries_parameter_index(sys, p[1, 1]) === + ParameterTimeseriesIndex(1, (1, 1, 1, 1)) +end From 1835a56c04e138a2b4afd07bf3f25994bfb19e4c Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 24 Dec 2024 16:10:52 +0530 Subject: [PATCH 0511/2176] test: fix `IfLifting` test --- test/if_lifting.jl | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/test/if_lifting.jl b/test/if_lifting.jl index d702355506..b72b468bf1 100644 --- a/test/if_lifting.jl +++ b/test/if_lifting.jl @@ -21,7 +21,9 @@ using ModelingToolkit: t_nounits as t, D_nounits as D, IfLifting, no_if_lift @test operation(only(equations(ss2)).rhs) === ifelse discvar = only(parameters(ss2)) - prob2 = ODEProblem(ss2, [x => 0.0], (0.0, 5.0)) + prob1 = ODEProblem(ss1, [ss1.x => 0.0], (0.0, 5.0)) + sol1 = solve(prob1, Tsit5()) + prob2 = ODEProblem(ss2, [ss2.x => 0.0], (0.0, 5.0)) sol2 = solve(prob2, Tsit5()) @test count(isapprox(pi), sol2.t) == 2 @test any(isapprox(pi), sol2.discretes[1].t) From 127cf3c36831595d57e423ff729fe600b367bcea Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 2 Jan 2025 18:44:38 +0530 Subject: [PATCH 0512/2176] build: bump OrdinaryDiffEqDefault compat --- Project.toml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Project.toml b/Project.toml index d672c007c2..b3ffc46164 100644 --- a/Project.toml +++ b/Project.toml @@ -123,6 +123,7 @@ OffsetArrays = "1" OrderedCollections = "1" OrdinaryDiffEq = "6.82.0" OrdinaryDiffEqCore = "1.13.0" +OrdinaryDiffEqDefault = "1.2" OrdinaryDiffEqNonlinearSolve = "1.3.0" PrecompileTools = "1" REPL = "1" @@ -166,6 +167,7 @@ OptimizationMOI = "fd9f6733-72f4-499f-8506-86b2bdd0dea1" OptimizationOptimJL = "36348300-93cb-4f02-beb5-3c3902f8871e" OrdinaryDiffEq = "1dea7af3-3e70-54e6-95c3-0bf5283fa5ed" OrdinaryDiffEqCore = "bbf590c4-e513-4bbe-9b18-05decba2e5d8" +OrdinaryDiffEqDefault = "50262376-6c5a-4cf5-baba-aaf4f84d72d7" OrdinaryDiffEqNonlinearSolve = "127b3ac7-2247-4354-8eb6-78cf4e7c58e8" Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f" REPL = "3fa0cd96-eef1-5676-8a61-b3b8758bbffb" From da03e00bc5499d6b58417fbcae0c7c4229dc8227 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 2 Jan 2025 20:24:57 +0530 Subject: [PATCH 0513/2176] test: add `OrdinaryDiffEqDefault` to the test environment --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index b3ffc46164..3d14a2dc65 100644 --- a/Project.toml +++ b/Project.toml @@ -183,4 +183,4 @@ Sundials = "c3572dad-4567-51f8-b174-8c6c989267f4" Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" [targets] -test = ["AmplNLWriter", "BenchmarkTools", "ControlSystemsBase", "DataInterpolations", "DelayDiffEq", "NonlinearSolve", "ForwardDiff", "Ipopt", "Ipopt_jll", "ModelingToolkitStandardLibrary", "Optimization", "OptimizationOptimJL", "OptimizationMOI", "OrdinaryDiffEq", "OrdinaryDiffEqCore", "REPL", "Random", "ReferenceTests", "SafeTestsets", "StableRNGs", "Statistics", "SteadyStateDiffEq", "Test", "StochasticDiffEq", "Sundials", "StochasticDelayDiffEq", "Pkg", "JET", "OrdinaryDiffEqNonlinearSolve"] +test = ["AmplNLWriter", "BenchmarkTools", "ControlSystemsBase", "DataInterpolations", "DelayDiffEq", "NonlinearSolve", "ForwardDiff", "Ipopt", "Ipopt_jll", "ModelingToolkitStandardLibrary", "Optimization", "OptimizationOptimJL", "OptimizationMOI", "OrdinaryDiffEq", "OrdinaryDiffEqCore", "OrdinaryDiffEqDefault", "REPL", "Random", "ReferenceTests", "SafeTestsets", "StableRNGs", "Statistics", "SteadyStateDiffEq", "Test", "StochasticDiffEq", "Sundials", "StochasticDelayDiffEq", "Pkg", "JET", "OrdinaryDiffEqNonlinearSolve"] From 59a41b1581a86d407fcdb8489510af8807335cea Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Sun, 5 Jan 2025 22:41:36 +0530 Subject: [PATCH 0514/2176] build: bump minor version --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 3d14a2dc65..7b0997823c 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "ModelingToolkit" uuid = "961ee093-0014-501f-94e3-6117800e7a78" authors = ["Yingbo Ma ", "Chris Rackauckas and contributors"] -version = "9.59.0" +version = "9.60.0" [deps] AbstractTrees = "1520ce14-60c1-5f80-bbc7-55ef81b5835c" From 080af2c536d411d06bf498cdedcc1c498e3e86fe Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 6 Jan 2025 15:27:53 +0530 Subject: [PATCH 0515/2176] fix: propagate `checkbounds` information to observed function generation --- src/systems/abstractsystem.jl | 18 ++++++++++++------ src/systems/diffeqs/abstractodesystem.jl | 6 ++++-- src/systems/diffeqs/sdesystem.jl | 3 ++- src/systems/discrete_system/discrete_system.jl | 3 ++- src/systems/jumps/jumpsystem.jl | 6 ++++-- src/systems/nonlinear/nonlinearsystem.jl | 6 ++++-- src/systems/optimization/optimizationsystem.jl | 2 +- 7 files changed, 29 insertions(+), 15 deletions(-) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index 168260ae69..64b75e80d0 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -785,7 +785,7 @@ end SymbolicIndexingInterface.supports_tuple_observed(::AbstractSystem) = true function SymbolicIndexingInterface.observed( - sys::AbstractSystem, sym; eval_expression = false, eval_module = @__MODULE__) + sys::AbstractSystem, sym; eval_expression = false, eval_module = @__MODULE__, checkbounds = true) if has_index_cache(sys) && (ic = get_index_cache(sys)) !== nothing if sym isa Symbol _sym = get(ic.symbol_to_variable, sym, nothing) @@ -808,7 +808,8 @@ function SymbolicIndexingInterface.observed( end end end - _fn = build_explicit_observed_function(sys, sym; eval_expression, eval_module) + _fn = build_explicit_observed_function( + sys, sym; eval_expression, eval_module, checkbounds) if is_time_dependent(sys) return _fn @@ -1671,11 +1672,14 @@ struct ObservedFunctionCache{S} steady_state::Bool eval_expression::Bool eval_module::Module + checkbounds::Bool end function ObservedFunctionCache( - sys; steady_state = false, eval_expression = false, eval_module = @__MODULE__) - return ObservedFunctionCache(sys, Dict(), steady_state, eval_expression, eval_module) + sys; steady_state = false, eval_expression = false, + eval_module = @__MODULE__, checkbounds = true) + return ObservedFunctionCache( + sys, Dict(), steady_state, eval_expression, eval_module, checkbounds) end # This is hit because ensemble problems do a deepcopy @@ -1685,7 +1689,9 @@ function Base.deepcopy_internal(ofc::ObservedFunctionCache, stackdict::IdDict) steady_state = ofc.steady_state eval_expression = ofc.eval_expression eval_module = ofc.eval_module - newofc = ObservedFunctionCache(sys, dict, steady_state, eval_expression, eval_module) + checkbounds = ofc.checkbounds + newofc = ObservedFunctionCache( + sys, dict, steady_state, eval_expression, eval_module, checkbounds) stackdict[ofc] = newofc return newofc end @@ -1694,7 +1700,7 @@ function (ofc::ObservedFunctionCache)(obsvar, args...) obs = get!(ofc.dict, value(obsvar)) do SymbolicIndexingInterface.observed( ofc.sys, obsvar; eval_expression = ofc.eval_expression, - eval_module = ofc.eval_module) + eval_module = ofc.eval_module, checkbounds = ofc.checkbounds) end if ofc.steady_state obs = let fn = obs diff --git a/src/systems/diffeqs/abstractodesystem.jl b/src/systems/diffeqs/abstractodesystem.jl index 20293068a9..b012bc73d4 100644 --- a/src/systems/diffeqs/abstractodesystem.jl +++ b/src/systems/diffeqs/abstractodesystem.jl @@ -436,7 +436,8 @@ function DiffEqBase.ODEFunction{iip, specialize}(sys::AbstractODESystem, ArrayInterface.restructure(u0 .* u0', M) end - observedfun = ObservedFunctionCache(sys; steady_state, eval_expression, eval_module) + observedfun = ObservedFunctionCache( + sys; steady_state, eval_expression, eval_module, checkbounds) jac_prototype = if sparse uElType = u0 === nothing ? Float64 : eltype(u0) @@ -522,7 +523,8 @@ function DiffEqBase.DAEFunction{iip}(sys::AbstractODESystem, dvs = unknowns(sys) _jac = nothing end - observedfun = ObservedFunctionCache(sys; eval_expression, eval_module) + observedfun = ObservedFunctionCache( + sys; eval_expression, eval_module, checkbounds = get(kwargs, :checkbounds, false)) jac_prototype = if sparse uElType = u0 === nothing ? Float64 : eltype(u0) diff --git a/src/systems/diffeqs/sdesystem.jl b/src/systems/diffeqs/sdesystem.jl index 0d600cfb5f..366e8a6d09 100644 --- a/src/systems/diffeqs/sdesystem.jl +++ b/src/systems/diffeqs/sdesystem.jl @@ -613,7 +613,8 @@ function DiffEqBase.SDEFunction{iip, specialize}(sys::SDESystem, dvs = unknowns( M = calculate_massmatrix(sys) _M = (u0 === nothing || M == I) ? M : ArrayInterface.restructure(u0 .* u0', M) - observedfun = ObservedFunctionCache(sys; eval_expression, eval_module) + observedfun = ObservedFunctionCache( + sys; eval_expression, eval_module, checkbounds = get(kwargs, :checkbounds, false)) SDEFunction{iip, specialize}(f, g; sys = sys, diff --git a/src/systems/discrete_system/discrete_system.jl b/src/systems/discrete_system/discrete_system.jl index 7458237333..bd5c72eec7 100644 --- a/src/systems/discrete_system/discrete_system.jl +++ b/src/systems/discrete_system/discrete_system.jl @@ -353,7 +353,8 @@ function SciMLBase.DiscreteFunction{iip, specialize}( f = SciMLBase.wrapfun_iip(f, (u0, u0, p, t)) end - observedfun = ObservedFunctionCache(sys) + observedfun = ObservedFunctionCache( + sys; eval_expression, eval_module, checkbounds = get(kwargs, :checkbounds, false)) DiscreteFunction{iip, specialize}(f; sys = sys, diff --git a/src/systems/jumps/jumpsystem.jl b/src/systems/jumps/jumpsystem.jl index efc5a9be7d..5c0a61771a 100644 --- a/src/systems/jumps/jumpsystem.jl +++ b/src/systems/jumps/jumpsystem.jl @@ -429,7 +429,8 @@ function DiffEqBase.DiscreteProblem(sys::JumpSystem, u0map, tspan::Union{Tuple, t = tspan === nothing ? nothing : tspan[1], use_union, tofloat = false, check_length = false) f = DiffEqBase.DISCRETE_INPLACE_DEFAULT - observedfun = ObservedFunctionCache(sys; eval_expression, eval_module) + observedfun = ObservedFunctionCache( + sys; eval_expression, eval_module, checkbounds = get(kwargs, :checkbounds, false)) df = DiscreteFunction{true, true}(f; sys = sys, observed = observedfun) DiscreteProblem(df, u0, tspan, p; kwargs...) @@ -527,7 +528,8 @@ function DiffEqBase.ODEProblem(sys::JumpSystem, u0map, tspan::Union{Tuple, Nothi t = tspan === nothing ? nothing : tspan[1], use_union, tofloat = false, check_length = false) f = (du, u, p, t) -> (du .= 0; nothing) - observedfun = ObservedFunctionCache(sys; eval_expression, eval_module) + observedfun = ObservedFunctionCache(sys; eval_expression, eval_module, + checkbounds = get(kwargs, :checkbounds, false)) df = ODEFunction(f; sys, observed = observedfun) return ODEProblem(df, u0, tspan, p; kwargs...) end diff --git a/src/systems/nonlinear/nonlinearsystem.jl b/src/systems/nonlinear/nonlinearsystem.jl index 8cd8175668..9ed830fc62 100644 --- a/src/systems/nonlinear/nonlinearsystem.jl +++ b/src/systems/nonlinear/nonlinearsystem.jl @@ -369,7 +369,8 @@ function SciMLBase.NonlinearFunction{iip}(sys::NonlinearSystem, dvs = unknowns(s _jac = nothing end - observedfun = ObservedFunctionCache(sys; eval_expression, eval_module) + observedfun = ObservedFunctionCache( + sys; eval_expression, eval_module, checkbounds = get(kwargs, :checkbounds, false)) if length(dvs) == length(equations(sys)) resid_prototype = nothing @@ -411,7 +412,8 @@ function SciMLBase.IntervalNonlinearFunction( f(u, p) = f_oop(u, p) f(u, p::MTKParameters) = f_oop(u, p...) - observedfun = ObservedFunctionCache(sys; eval_expression, eval_module) + observedfun = ObservedFunctionCache( + sys; eval_expression, eval_module, checkbounds = get(kwargs, :checkbounds, false)) IntervalNonlinearFunction{false}( f; observed = observedfun, sys = sys, initialization_data) diff --git a/src/systems/optimization/optimizationsystem.jl b/src/systems/optimization/optimizationsystem.jl index 0b20fdef79..860e063e35 100644 --- a/src/systems/optimization/optimizationsystem.jl +++ b/src/systems/optimization/optimizationsystem.jl @@ -420,7 +420,7 @@ function DiffEqBase.OptimizationProblem{iip}(sys::OptimizationSystem, u0map, hess_prototype = nothing end - observedfun = ObservedFunctionCache(sys; eval_expression, eval_module) + observedfun = ObservedFunctionCache(sys; eval_expression, eval_module, checkbounds) if length(cstr) > 0 @named cons_sys = ConstraintsSystem(cstr, dvs, ps; checks) From f9c833730b378ec8478a12a15d609b3e4a12af41 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 6 Jan 2025 15:29:17 +0530 Subject: [PATCH 0516/2176] build: bump Symbolics compat --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 7b0997823c..d1bde6371f 100644 --- a/Project.toml +++ b/Project.toml @@ -143,7 +143,7 @@ StochasticDiffEq = "6.72.1" StochasticDelayDiffEq = "1.8.1" SymbolicIndexingInterface = "0.3.36" SymbolicUtils = "3.7" -Symbolics = "6.19" +Symbolics = "6.22.1" URIs = "1" UnPack = "0.1, 1.0" Unitful = "1.1" From bc2c1287c24fd80ce12aa5068c6924c3395665c7 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 19 Dec 2024 13:37:32 +0530 Subject: [PATCH 0517/2176] feat: add analysis points --- src/ModelingToolkit.jl | 4 +- src/systems/analysis_points.jl | 649 +++++++++++++++++++++++++++++++++ src/systems/connectors.jl | 1 + 3 files changed, 653 insertions(+), 1 deletion(-) create mode 100644 src/systems/analysis_points.jl diff --git a/src/ModelingToolkit.jl b/src/ModelingToolkit.jl index 10aba4d8a9..486ef63460 100644 --- a/src/ModelingToolkit.jl +++ b/src/ModelingToolkit.jl @@ -145,6 +145,7 @@ include("systems/index_cache.jl") include("systems/parameter_buffer.jl") include("systems/abstractsystem.jl") include("systems/model_parsing.jl") +include("systems/analysis_points.jl") include("systems/connectors.jl") include("systems/imperative_affect.jl") include("systems/callbacks.jl") @@ -238,7 +239,8 @@ export SteadyStateProblem, SteadyStateProblemExpr export JumpProblem export NonlinearSystem, OptimizationSystem, ConstraintsSystem export alias_elimination, flatten -export connect, domain_connect, @connector, Connection, Flow, Stream, instream +export connect, domain_connect, @connector, Connection, AnalysisPoint, Flow, Stream, + instream export initial_state, transition, activeState, entry, ticksInState, timeInState export @component, @mtkmodel, @mtkbuild export isinput, isoutput, getbounds, hasbounds, getguess, hasguess, isdisturbance, diff --git a/src/systems/analysis_points.jl b/src/systems/analysis_points.jl new file mode 100644 index 0000000000..605eb32749 --- /dev/null +++ b/src/systems/analysis_points.jl @@ -0,0 +1,649 @@ +""" + $(TYPEDEF) + $(TYPEDSIGNATURES) + +Create an AnalysisPoint for linear analysis. Analysis points can be created by calling + +``` +connect(in, :ap_name, out...) +``` + +Where `in` is the input to the connection, and `out...` are the outputs. In the context of +ModelingToolkitStandardLibrary.jl, `in` is a `RealOutput` connector and `out...` are all +`RealInput` connectors. All involved connectors are required to either have an unknown named +`u` or a single unknown, all of which should have the same size. +""" +struct AnalysisPoint + input::Any + name::Symbol + outputs::Union{Nothing, Vector{Any}} +end + +AnalysisPoint() = AnalysisPoint(nothing, Symbol(), nothing) +AnalysisPoint(name::Symbol) = AnalysisPoint(nothing, name, nothing) + +Base.nameof(ap::AnalysisPoint) = ap.name + +Base.show(io::IO, ap::AnalysisPoint) = show(io, MIME"text/plain"(), ap) +function Base.show(io::IO, ::MIME"text/plain", ap::AnalysisPoint) + if ap.input === nothing + print(io, "0") + return + end + if get(io, :compact, false) + print(io, + "AnalysisPoint($(ap_var(ap.input)), $(ap_var.(ap.outputs)); name=$(ap.name))") + else + print(io, "AnalysisPoint(") + printstyled(io, ap.name, color = :cyan) + if ap.input !== nothing && ap.outputs !== nothing + print(io, " from ") + printstyled(io, ap_var(ap.input), color = :green) + print(io, " to ") + if length(ap.outputs) == 1 + printstyled(io, ap_var(ap.outputs[1]), color = :blue) + else + printstyled(io, "[", join(ap_var.(ap.outputs), ", "), "]", color = :blue) + end + end + print(io, ")") + end +end + +""" + $(TYPEDSIGNATURES) + +Convert an `AnalysisPoint` to a standard connection. +""" +function to_connection(ap::AnalysisPoint) + return connect(ap.input, ap.outputs...) +end + +""" + $(TYPEDSIGNATURES) + +Namespace an `AnalysisPoint` by namespacing the involved systems and the name of the point. +""" +function renamespace(sys, ap::AnalysisPoint) + return AnalysisPoint( + ap.input === nothing ? nothing : renamespace(sys, ap.input), + renamespace(sys, ap.name), + ap.outputs === nothing ? nothing : map(Base.Fix1(renamespace, sys), ap.outputs) + ) +end + +# create analysis points via `connect` +function Symbolics.connect(in, ap::AnalysisPoint, outs...) + return AnalysisPoint() ~ AnalysisPoint(in, ap.name, collect(outs)) +end + +""" + $(TYPEDSIGNATURES) + +Create an `AnalysisPoint` connection connecting `in` to `outs...`. +""" +function Symbolics.connect(in::AbstractSystem, name::Symbol, out, outs...) + return AnalysisPoint() ~ AnalysisPoint(in, name, [out; collect(outs)]) +end + +""" + $(TYPEDSIGNATURES) + +Return all the namespaces in `name`. Namespaces should be separated by `.` or +`$NAMESPACE_SEPARATOR`. +""" +namespace_hierarchy(name::Symbol) = map( + Symbol, split(string(name), ('.', NAMESPACE_SEPARATOR))) + +""" + $(TYPEDSIGNATURES) + +Remove all `AnalysisPoint`s in `sys` and any of its subsystems, replacing them by equivalent connections. +""" +function remove_analysis_points(sys::AbstractSystem) + eqs = map(get_eqs(sys)) do eq + eq.lhs isa AnalysisPoint ? to_connection(eq.rhs) : eq + end + @set! sys.eqs = eqs + @set! sys.systems = map(remove_analysis_points, get_systems(sys)) + + return sys +end + +""" + $(TYPEDSIGNATURES) + +Given a system involved in an `AnalysisPoint`, get the variable to be used in the +connection. This is the variable named `u` if present, and otherwise the only +variable in the system. If the system does not have a variable named `u` and +contains multiple variables, throw an error. +""" +function ap_var(sys) + if hasproperty(sys, :u) + return sys.u + end + x = unknowns(sys) + length(x) == 1 && return renamespace(sys, x[1]) + error("Could not determine the analysis-point variable in system $(nameof(sys)). To use an analysis point, apply it to a connection between causal blocks which have a variable named `u` or a single unknown of the same size.") +end + +""" + $(TYPEDEF) + +The supertype of all transformations that can be applied to an `AnalysisPoint`. All +concrete subtypes must implement `apply_transformation`. +""" +abstract type AnalysisPointTransformation end + +""" + apply_transformation(tf::AnalysisPointTransformation, sys::AbstractSystem) + +Apply the given analysis point transformation `tf` to the system `sys`. Throw an error if +any analysis points referred to in `tf` are not present in `sys`. Return a tuple +containing the modified system as the first element, and a tuple of the additional +variables added by the transformation as the second element. +""" +function apply_transformation end + +""" + $(TYPEDSIGNATURES) + +Given a namespaced subsystem `target` of root system `root`, return a modified copy of +`root` with `target` modified according to `fn` alongside any extra variables added +by `fn`. + +`fn` is a function which takes the instance of `target` present in the hierarchy of +`root`, and returns a 2-tuple consisting of the modified version of `target` and a tuple +of the extra variables added. +""" +modify_nested_subsystem(fn, root::AbstractSystem, target::AbstractSystem) = modify_nested_subsystem( + fn, root, nameof(target)) +""" + $(TYPEDSIGNATURES) + +Apply the modification to the system containing the namespaced analysis point `target`. +""" +modify_nested_subsystem(fn, root::AbstractSystem, target::AnalysisPoint) = modify_nested_subsystem( + fn, root, @view namespace_hierarchy(nameof(target))[1:(end - 1)]) +""" + $(TYPEDSIGNATURES) + +Apply the modification to the nested subsystem of `root` whose namespaced name matches +the provided name `target`. The namespace separator in `target` should be `.` or +`$NAMESPACE_SEPARATOR`. The `target` may include `nameof(root)` as the first namespace. +""" +modify_nested_subsystem(fn, root::AbstractSystem, target::Symbol) = modify_nested_subsystem( + fn, root, namespace_hierarchy(target)) + +""" + $(TYPEDSIGNATURES) + +Apply the modification to the nested subsystem of `root` where the name of the subsystem at +each level in the hierarchy is given by elements of `hierarchy`. For example, if +`hierarchy = [:a, :b, :c]`, the system being searched for will be `root.a.b.c`. Note that +the hierarchy may include the name of the root system, in which the first element will be +ignored. For example, `hierarchy = [:root, :a, :b, :c]` also searches for `root.a.b.c`. +An empty `hierarchy` will apply the modification to `root`. +""" +function modify_nested_subsystem( + fn, root::AbstractSystem, hierarchy::AbstractVector{Symbol}) + # no hierarchy, so just apply to the root + if isempty(hierarchy) + return fn(root) + end + # ignore the name of the root + if nameof(root) == hierarchy[1] + hierarchy = @view hierarchy[2:end] + end + + # recursive helper function which does the searching and modification + function _helper(sys::AbstractSystem, i::Int) + # we reached past the end, so everything matched and + # `sys` is the system to modify. + if i > length(hierarchy) + sys, vars = fn(sys) + else + cur = hierarchy[i] + idx = findfirst(subsys -> nameof(subsys) == cur, get_systems(sys)) + idx === nothing && + error("System $(join([nameof(root); hierarchy[1:i-1]], '.')) does not have a subsystem named $cur.") + + newsys, vars = _helper(get_systems(sys)[idx], i + 1) + @set! sys.systems[idx] = newsys + end + if i != 1 + vars = ntuple(Val(length(vars))) do i + renamespace(sys, vars[i]) + end + end + return sys, vars + end + + return _helper(root, 1) +end + +""" + $(TYPEDSIGNATURES) + +Given a system `sys` and analysis point `ap`, return the index in `get_eqs(sys)` +containing an equation which has as it's RHS an analysis point with name `nameof(ap)`. +""" +analysis_point_index(sys::AbstractSystem, ap::AnalysisPoint) = analysis_point_index( + sys, nameof(ap)) +""" + $(TYPEDSIGNATURES) + +Search for the analysis point with the given `name` in `get_eqs(sys)`. +""" +function analysis_point_index(sys::AbstractSystem, name::Symbol) + name = namespace_hierarchy(name)[end] + findfirst(get_eqs(sys)) do eq + eq.lhs isa AnalysisPoint && nameof(eq.rhs) == name + end +end + +""" + $(TYPEDSIGNATURES) + +Create a new variable of the same `symtype` and size as `var`, using `name` as the base +name for the new variable. `iv` denotes the independent variable of the system. Prefix +`d_` to the name of the new variable if `perturb == true`. Return the new symbolic +variable and the appropriate zero value for it. +""" +function get_analysis_variable(var, name, iv; perturb = true) + var = unwrap(var) + if perturb + name = Symbol(:d_, name) + end + if Symbolics.isarraysymbolic(var) + T = Array{eltype(symtype(var)), ndims(var)} + pvar = unwrap(only(@variables $name(iv)::T)) + pvar = setmetadata(pvar, Symbolics.ArrayShapeCtx, Symbolics.shape(var)) + default = zeros(symtype(var), size(var)) + else + T = symtype(var) + pvar = unwrap(only(@variables $name(iv)::T)) + default = zero(T) + end + return pvar, default +end + +#### PRIMITIVE TRANSFORMATIONS + +""" + $(TYPEDEF) + +A transformation which breaks the connection referred to by `ap`. If `add_input == true`, +it will add a new input variable which connects to the outputs of the analysis point. +`apply_transformation` returns the new input variable (if added) as the auxiliary +information. The new input variable will have the name `Symbol(:d_, nameof(ap))`. + +Note that this transformation will remove `ap`, causing any subsequent transformations +referring to it to fail. + +The added variable, if present, will have a default of zero, of the appropriate type and +size. + +## Fields + +$(TYPEDFIELDS) +""" +struct Break <: AnalysisPointTransformation + """ + The analysis point to break. + """ + ap::AnalysisPoint + """ + Whether to add a new input variable connected to all the outputs of `ap`. + """ + add_input::Bool +end + +""" + $(TYPEDSIGNATURES) + +`Break` the given analysis point `ap` without adding an input. +""" +Break(ap::AnalysisPoint) = Break(ap, false) + +function apply_transformation(tf::Break, sys::AbstractSystem) + modify_nested_subsystem(sys, tf.ap) do breaksys + # get analysis point + ap_idx = analysis_point_index(breaksys, tf.ap) + ap_idx === nothing && + error("Analysis point $(nameof(tf.ap)) not found in system $(nameof(sys)).") + breaksys_eqs = copy(get_eqs(breaksys)) + @set! breaksys.eqs = breaksys_eqs + + ap = breaksys_eqs[ap_idx].rhs + deleteat!(breaksys_eqs, ap_idx) + + tf.add_input || return sys, () + + ap_ivar = ap_var(ap.input) + new_var, new_def = get_analysis_variable(ap_ivar, nameof(ap), get_iv(sys)) + for outsys in ap.outputs + push!(breaksys_eqs, ap_var(outsys) ~ new_var) + end + defs = copy(get_defaults(breaksys)) + defs[new_var] = new_def + @set! breaksys.defaults = defs + unks = copy(get_unknowns(breaksys)) + push!(unks, new_var) + @set! breaksys.unknowns = unks + + return breaksys, (new_var,) + end +end + +""" + $(TYPEDEF) + +A transformation which returns the variable corresponding to the input of the analysis +point. + +`apply_transformation` returns the variable as auxiliary information. + +## Fields + +$(TYPEDFIELDS) +""" +struct GetInput <: AnalysisPointTransformation + """ + The analysis point to add the output to. + """ + ap::AnalysisPoint +end + +function apply_transformation(tf::GetInput, sys::AbstractSystem) + modify_nested_subsystem(sys, tf.ap) do ap_sys + # get the analysis point + ap_idx = analysis_point_index(ap_sys, tf.ap) + ap_idx === nothing && + error("Analysis point $(nameof(tf.ap)) not found in system $(nameof(sys)).") + # get the anlysis point + ap_sys_eqs = copy(get_eqs(ap_sys)) + ap = ap_sys_eqs[ap_idx].rhs + + # input variable + ap_ivar = ap_var(ap.input) + return ap_sys, (ap_ivar,) + end +end + +""" + $(TYPEDEF) + +A transformation that creates a new input variable which is added to the input of +the analysis point before connecting to the outputs. The new variable will have the name +`Symbol(:d_, nameof(ap))`. + +If `with_output == true`, also creates an additional new variable which has the value +provided to the outputs after the above modification. This new variable has the same name +as the analysis point. + +`apply_transformation` returns a 1-tuple of the perturbation variable if +`with_output == false` and a 2-tuple of the perturbation variable and output variable if +`with_output == true`. + +Removes the analysis point `ap`, so any subsequent transformations requiring it will fail. + +The added variable(s) will have a default of zero, of the appropriate type and size. + +## Fields + +$(TYPEDFIELDS) +""" +struct PerturbOutput <: AnalysisPointTransformation + """ + The analysis point to modify + """ + ap::AnalysisPoint + """ + Whether to add an additional output variable. + """ + with_output::Bool +end + +""" + $(TYPEDSIGNATURES) + +Add an input without an additional output variable. +""" +PerturbOutput(ap::AnalysisPoint) = PerturbOutput(ap, false) + +function apply_transformation(tf::PerturbOutput, sys::AbstractSystem) + modify_nested_subsystem(sys, tf.ap) do ap_sys + # get analysis point + ap_idx = analysis_point_index(ap_sys, tf.ap) + ap_idx === nothing && + error("Analysis point $(nameof(tf.ap)) not found in system $(nameof(sys)).") + # modified quations + ap_sys_eqs = copy(get_eqs(ap_sys)) + @set! ap_sys.eqs = ap_sys_eqs + ap = ap_sys_eqs[ap_idx].rhs + # remove analysis point + deleteat!(ap_sys_eqs, ap_idx) + + # add equations involving new variable + ap_ivar = ap_var(ap.input) + new_var, new_def = get_analysis_variable(ap_ivar, nameof(ap), get_iv(sys)) + for outsys in ap.outputs + push!(ap_sys_eqs, ap_var(outsys) ~ ap_ivar + new_var) + end + # add variable + unks = copy(get_unknowns(ap_sys)) + push!(unks, new_var) + @set! ap_sys.unknowns = unks + # add default + defs = copy(get_defaults(ap_sys)) + defs[new_var] = new_def + @set! ap_sys.defaults = defs + + tf.with_output || return ap_sys, (new_var,) + + # add output variable, equation, default + out_var, out_def = get_analysis_variable( + ap_ivar, nameof(ap), get_iv(sys); perturb = false) + defs[out_var] = out_def + push!(ap_sys_eqs, out_var ~ ap_ivar + new_var) + push!(unks, out_var) + + return ap_sys, (new_var, out_var) + end +end + +""" + $(TYPEDSIGNATURES) + +A transformation which adds a variable named `name` to the system containing the analysis +point `ap`. The added variable has the same type and size as the input of the analysis +point. +""" +struct AddVariable <: AnalysisPointTransformation + ap::AnalysisPoint + name::Symbol +end + +AddVariable(ap::AnalysisPoint) = AddVariable(ap, nameof(ap)) + +function apply_transformation(tf::AddVariable, sys::AbstractSystem) + modify_nested_subsystem(sys, tf.ap) do ap_sys + # get analysis point + ap_idx = analysis_point_index(ap_sys, tf.ap) + ap_idx === nothing && + error("Analysis point $(nameof(tf.ap)) not found in system $(nameof(sys)).") + ap_sys_eqs = copy(get_eqs(ap_sys)) + ap = ap_sys_eqs[ap_idx].rhs + + # add equations involving new variable + ap_ivar = ap_var(ap.input) + new_var, new_def = get_analysis_variable( + ap_ivar, tf.name, get_iv(sys); perturb = false) + # add variable + unks = copy(get_unknowns(ap_sys)) + push!(unks, new_var) + @set! ap_sys.unknowns = unks + return ap_sys, (new_var,) + end +end + +#### DERIVED TRANSFORMATIONS + +""" + $(TYPEDSIGNATURES) + +A transformation enable calculating the sensitivity function about the analysis point `ap`. +`apply_transformation` returns a 2-tuple `du, u` as auxiliary information. + +Removes the analysis point `ap`, so any subsequent transformations requiring it will fail. + +The added variables will have a default of zero, of the appropriate type and size. +""" +SensitivityTransform(ap::AnalysisPoint) = PerturbOutput(ap, true) + +""" + $(TYPEDEF) + +A transformation to enable calculating the complementary sensitivity function about the +analysis point `ap`. `apply_transformation` returns a 2-tuple `du, u` as auxiliary +information. + +Removes the analysis point `ap`, so any subsequent transformations requiring it will fail. + +The added variables will have a default of zero, of the appropriate type and size. +""" +struct ComplementarySensitivityTransform <: AnalysisPointTransformation + ap::AnalysisPoint +end + +function apply_transformation(cst::ComplementarySensitivityTransform, sys::AbstractSystem) + sys, (u,) = apply_transformation(GetInput(cst.ap), sys) + sys, (du,) = apply_transformation(AddVariable(cst.ap, Symbol(:comp_sens_du)), sys) + sys, (_du,) = apply_transformation(PerturbOutput(cst.ap), sys) + + # `PerturbOutput` adds the equation `input + _du ~ output` + # but comp sensitivity wants `output + du ~ input`. Thus, `du ~ -_du`. + eqs = copy(get_eqs(sys)) + @set! sys.eqs = eqs + push!(eqs, du ~ -_du) + + defs = copy(get_defaults(sys)) + @set! sys.defaults = defs + defs[du] = -_du + return sys, (du, u) +end + +struct LoopTransferTransform <: AnalysisPointTransformation + ap::AnalysisPoint +end + +function apply_transformation(tf::LoopTransferTransform, sys::AbstractSystem) + sys, (u,) = apply_transformation(GetInput(tf.ap), sys) + sys, (du,) = apply_transformation(Break(tf.ap, true), sys) + return sys, (du, u) +end + +### TODO: Move these + +canonicalize_ap(ap::Symbol) = [AnalysisPoint(ap)] +canonicalize_ap(ap::AnalysisPoint) = [ap] +canonicalize_ap(ap) = [ap] +function canonicalize_ap(aps::Vector) + mapreduce(canonicalize_ap, vcat, aps; init = []) +end + +function handle_loop_openings(sys::AbstractSystem, aps) + for ap in canonicalize_ap(aps) + sys, (outvar,) = apply_transformation(Break(ap, true), sys) + if Symbolics.isarraysymbolic(outvar) + push!(get_eqs(sys), outvar ~ zeros(size(outvar))) + else + push!(get_eqs(sys), outvar ~ 0) + end + end + return sys +end + +function get_sensitivity_function( + sys::AbstractSystem, aps; system_modifier = identity, loop_openings = [], kwargs...) + sys = handle_loop_openings(sys, loop_openings) + aps = canonicalize_ap(aps) + dus = [] + us = [] + for ap in aps + sys, (du, u) = apply_transformation(SensitivityTransform(ap), sys) + push!(dus, du) + push!(us, u) + end + linearization_function(system_modifier(sys), dus, us; kwargs...) +end + +function get_comp_sensitivity_function( + sys::AbstractSystem, aps; system_modifier = identity, loop_openings = [], kwargs...) + sys = handle_loop_openings(sys, loop_openings) + aps = canonicalize_ap(aps) + dus = [] + us = [] + for ap in aps + sys, (du, u) = apply_transformation(ComplementarySensitivityTransform(ap), sys) + push!(dus, du) + push!(us, u) + end + linearization_function(system_modifier(sys), dus, us; kwargs...) +end + +function get_looptransfer_function( + sys, aps; system_modifier = identity, loop_openings = [], kwargs...) + sys = handle_loop_openings(sys, loop_openings) + aps = canonicalize_ap(aps) + dus = [] + us = [] + for ap in aps + sys, (du, u) = apply_transformation(LoopTransferTransform(ap), sys) + push!(dus, du) + push!(us, u) + end + linearization_function(system_modifier(sys), dus, us; kwargs...) +end + +for f in [:get_sensitivity, :get_comp_sensitivity, :get_looptransfer] + @eval function $f(sys, ap, args...; kwargs...) + lin_fun, ssys = $(Symbol(f, :_function))(sys, ap, args...; kwargs...) + ModelingToolkit.linearize(ssys, lin_fun; kwargs...), ssys + end +end + +function open_loop(sys, ap::Union{Symbol, AnalysisPoint}; kwargs...) + if ap isa Symbol + ap = AnalysisPoint(ap) + end + tf = LoopTransferTransform(ap) + return apply_transformation(tf, sys) +end + +function linearization_function(sys::AbstractSystem, + inputs::Union{Symbol, Vector{Symbol}, AnalysisPoint, Vector{AnalysisPoint}}, + outputs; loop_openings = [], system_modifier = identity, kwargs...) + sys = handle_loop_openings(sys, loop_openings) + + inputs = canonicalize_ap(inputs) + outputs = canonicalize_ap(outputs) + + input_vars = [] + for input in inputs + sys, (input_var,) = apply_transformation(PerturbOutput(input), sys) + push!(input_vars, input_var) + end + output_vars = [] + for output in outputs + if output isa AnalysisPoint + sys, (output_var,) = apply_transformation(GetInput(output), sys) + push!(output_vars, output_var) + else + push!(output_vars, output) + end + end + + return linearization_function(system_modifier(sys), input_vars, output_vars; kwargs...) +end diff --git a/src/systems/connectors.jl b/src/systems/connectors.jl index 227b4624bf..f0f49acb85 100644 --- a/src/systems/connectors.jl +++ b/src/systems/connectors.jl @@ -481,6 +481,7 @@ end function expand_connections(sys::AbstractSystem, find = nothing, replace = nothing; debug = false, tol = 1e-10, scalarize = true) + sys = remove_analysis_points(sys) sys, (csets, domain_csets) = generate_connection_set(sys, find, replace; scalarize) ceqs, instream_csets = generate_connection_equations_and_stream_connections(csets) _sys = expand_instream(instream_csets, sys; debug = debug, tol = tol) From c43b3e9cd0f94658bf29f8cd01490a8e40082e6b Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 19 Dec 2024 13:37:40 +0530 Subject: [PATCH 0518/2176] test: test analysis points --- test/analysis_points.jl | 23 +++++++++++++++++++++++ test/runtests.jl | 1 + 2 files changed, 24 insertions(+) create mode 100644 test/analysis_points.jl diff --git a/test/analysis_points.jl b/test/analysis_points.jl new file mode 100644 index 0000000000..b6a9c8c21f --- /dev/null +++ b/test/analysis_points.jl @@ -0,0 +1,23 @@ +using ModelingToolkit, ModelingToolkitStandardLibrary +using Test +using ModelingToolkit: t_nounits as t, D_nounits as D + +@testset "AnalysisPoint is lowered to `connect`" begin + @named P = FirstOrder(k = 1, T = 1) + @named C = Gain(; k = -1) + + ap = AnalysisPoint(:plant_input) + eqs = [connect(P.output, C.input) + connect(C.output, ap, P.input)] + sys_ap = ODESystem(eqs, t, systems = [P, C], name = :hej) + sys_ap2 = @test_nowarn expand_connections(sys) + + @test all(eq -> !(eq.lhs isa AnalysisPoint), equations(sys_ap2)) + + eqs = [connect(P.output, C.input) + connect(C.output, P.input)] + sys_normal = ODESystem(eqs, t, systems = [P, C], name = :hej) + sys_normal2 = @test_nowarn expand_connections(sys) + + @test isequal(sys_ap2, sys_normal2) +end diff --git a/test/runtests.jl b/test/runtests.jl index 4a2ec80e6a..af83ffccd8 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -84,6 +84,7 @@ end @safetestset "print_tree" include("print_tree.jl") @safetestset "Constraints Test" include("constraints.jl") @safetestset "IfLifting Test" include("if_lifting.jl") + @safetestset "Analysis Points Test" include("analysis_points.jl") end end From eba5dc77d855bca64e4c94e0280b1535009b1f64 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 24 Dec 2024 11:24:54 +0530 Subject: [PATCH 0519/2176] refactor: format Project.toml --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 7b0997823c..e4eba3cfad 100644 --- a/Project.toml +++ b/Project.toml @@ -73,8 +73,8 @@ MTKBifurcationKitExt = "BifurcationKit" MTKChainRulesCoreExt = "ChainRulesCore" MTKDeepDiffsExt = "DeepDiffs" MTKHomotopyContinuationExt = "HomotopyContinuation" -MTKLabelledArraysExt = "LabelledArrays" MTKInfiniteOptExt = "InfiniteOpt" +MTKLabelledArraysExt = "LabelledArrays" [compat] AbstractTrees = "0.3, 0.4" From f0bab4a58daff1d4b6e984173631a1ba75c72fa3 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 30 Dec 2024 00:41:06 +0530 Subject: [PATCH 0520/2176] feat: allow accessing `AnalysisPoint` via `getproperty` syntax --- src/systems/abstractsystem.jl | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index 168260ae69..9b5ff7183d 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -1162,6 +1162,14 @@ function getvar(sys::AbstractSystem, name::Symbol; namespace = !iscomplete(sys)) end end + if has_eqs(sys) + for eq in get_eqs(sys) + if eq.lhs isa AnalysisPoint && nameof(eq.rhs) == name + return namespace ? renamespace(sys, eq.rhs) : eq.rhs + end + end + end + throw(ArgumentError("System $(nameof(sys)): variable $name does not exist")) end From 04ca326ca985bbc28a66b02f0f1ed89143919a77 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 30 Dec 2024 00:43:03 +0530 Subject: [PATCH 0521/2176] test: add tests from MTKStdlib --- test/analysis_points.jl | 242 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 239 insertions(+), 3 deletions(-) diff --git a/test/analysis_points.jl b/test/analysis_points.jl index b6a9c8c21f..6eae30314e 100644 --- a/test/analysis_points.jl +++ b/test/analysis_points.jl @@ -1,11 +1,14 @@ -using ModelingToolkit, ModelingToolkitStandardLibrary +using ModelingToolkit, ModelingToolkitStandardLibrary.Blocks +using OrdinaryDiffEq, LinearAlgebra using Test -using ModelingToolkit: t_nounits as t, D_nounits as D +using ModelingToolkit: t_nounits as t, D_nounits as D, AnalysisPoint, get_sensitivity, + get_comp_sensitivity, get_looptransfer +using Symbolics: NAMESPACE_SEPARATOR @testset "AnalysisPoint is lowered to `connect`" begin @named P = FirstOrder(k = 1, T = 1) @named C = Gain(; k = -1) - + ap = AnalysisPoint(:plant_input) eqs = [connect(P.output, C.input) connect(C.output, ap, P.input)] @@ -21,3 +24,236 @@ using ModelingToolkit: t_nounits as t, D_nounits as D @test isequal(sys_ap2, sys_normal2) end + +# also tests `connect(input, name::Symbol, outputs...)` syntax +@testset "AnalysisPoint is accessible via `getproperty`" begin + @named P = FirstOrder(k = 1, T = 1) + @named C = Gain(; k = -1) + + eqs = [connect(P.output, C.input), connect(C.output, :plant_input, P.input)] + sys_ap = ODESystem(eqs, t, systems = [P, C], name = :hej) + ap2 = @test_nowarn sys_ap.plant_input + @test nameof(ap2) == Symbol(join(["hej", "plant_input"], NAMESPACE_SEPARATOR)) + @named sys = ODESystem(Equation[], t; systems = [sys_ap]) + ap3 = @test_nowarn sys.hej.plant_input + @test nameof(ap3) == Symbol(join(["sys", "hej", "plant_input"], NAMESPACE_SEPARATOR)) + sys = complete(sys) + ap4 = sys.hej.plant_input + @test nameof(ap4) == Symbol(join(["hej", "plant_input"], NAMESPACE_SEPARATOR)) +end + +### Ported from MTKStdlib + +@named P = FirstOrder(k = 1, T = 1) +@named C = Gain(; k = -1) + +ap = AnalysisPoint(:plant_input) +eqs = [connect(P.output, C.input), connect(C.output, ap, P.input)] +sys = ODESystem(eqs, t, systems = [P, C], name = :hej) +@named nested_sys = ODESystem(Equation[], t; systems = [sys]) + +@testset "simplifies and solves" begin + ssys = structural_simplify(sys) + prob = ODEProblem(ssys, [P.x => 1], (0, 10)) + sol = solve(prob, Rodas5()) + @test norm(sol.u[1]) >= 1 + @test norm(sol.u[end]) < 1e-6 # This fails without the feedback through C +end + +@testset "get_sensitivity - $name" for (name, sys, ap) in [ + ("inner", sys, sys.plant_input), + ("nested", nested_sys, nested_sys.hej.plant_input), + ("inner - nonamespace", sys, :plant_input), + ("inner - Symbol", sys, nameof(sys.plant_input)), + ("nested - Symbol", nested_sys, nameof(nested_sys.hej.plant_input)) +] + matrices, _ = get_sensitivity(sys, ap) + @test matrices.A[] == -2 + @test matrices.B[] * matrices.C[] == -1 # either one negative + @test matrices.D[] == 1 +end + +@testset "get_comp_sensitivity - $name" for (name, sys, ap) in [ + ("inner", sys, sys.plant_input), + ("nested", nested_sys, nested_sys.hej.plant_input), + ("inner - nonamespace", sys, :plant_input), + ("inner - Symbol", sys, nameof(sys.plant_input)), + ("nested - Symbol", nested_sys, nameof(nested_sys.hej.plant_input)) +] + matrices, _ = get_comp_sensitivity(sys, ap) + @test matrices.A[] == -2 + @test matrices.B[] * matrices.C[] == 1 # both positive or negative + @test matrices.D[] == 0 +end + +#= +# Equivalent code using ControlSystems. This can be used to verify the expected results tested for above. +using ControlSystemsBase +P = tf(1.0, [1, 1]) +C = 1 # Negative feedback assumed in ControlSystems +S = sensitivity(P, C) # or feedback(1, P*C) +T = comp_sensitivity(P, C) # or feedback(P*C) +=# + +@testset "get_looptransfer - $name" for (name, sys, ap) in [ + ("inner", sys, sys.plant_input), + ("nested", nested_sys, nested_sys.hej.plant_input), + ("inner - nonamespace", sys, :plant_input), + ("inner - Symbol", sys, nameof(sys.plant_input)), + ("nested - Symbol", nested_sys, nameof(nested_sys.hej.plant_input)) +] + matrices, _ = get_looptransfer(sys, ap) + @test matrices.A[] == -1 + @test matrices.B[] * matrices.C[] == -1 # either one negative + @test matrices.D[] == 0 +end + +#= +# Equivalent code using ControlSystems. This can be used to verify the expected results tested for above. +using ControlSystemsBase +P = tf(1.0, [1, 1]) +C = -1 +L = P*C +=# + +@testset "open_loop - $name" for (name, sys, ap) in [ + ("inner", sys, sys.plant_input), + ("nested", nested_sys, nested_sys.hej.plant_input), + ("inner - nonamespace", sys, :plant_input), + ("inner - Symbol", sys, nameof(sys.plant_input)), + ("nested - Symbol", nested_sys, nameof(nested_sys.hej.plant_input)) +] + open_sys, (du, u) = open_loop(sys, ap) + matrices, _ = linearize(open_sys, [du], [u]) + @test matrices.A[] == -1 + @test matrices.B[] * matrices.C[] == -1 # either one negative + @test matrices.D[] == 0 +end + +# Multiple analysis points + +eqs = [connect(P.output, :plant_output, C.input) + connect(C.output, :plant_input, P.input)] +sys = ODESystem(eqs, t, systems = [P, C], name = :hej) +@named nested_sys = ODESystem(Equation[], t; systems = [sys]) + +@testset "get_sensitivity - $name" for (name, sys, ap) in [ + ("inner", sys, sys.plant_input), + ("nested", nested_sys, nested_sys.hej.plant_input), + ("inner - nonamespace", sys, :plant_input), + ("inner - Symbol", sys, nameof(sys.plant_input)), + ("nested - Symbol", nested_sys, nameof(nested_sys.hej.plant_input)) +] + matrices, _ = get_sensitivity(sys, ap) + @test matrices.A[] == -2 + @test matrices.B[] * matrices.C[] == -1 # either one negative + @test matrices.D[] == 1 +end + +@testset "linearize - $name" for (name, sys, inputap, outputap) in [ + ("inner", sys, sys.plant_input, sys.plant_output), + ("nested", nested_sys, nested_sys.hej.plant_input, nested_sys.hej.plant_output) +] + @testset "input - $(typeof(input)), output - $(typeof(output))" for (input, output) in [ + (inputap, outputap), + (nameof(inputap), outputap), + (inputap, nameof(outputap)), + (nameof(inputap), nameof(outputap)), + (inputap, [outputap]), + (nameof(inputap), [outputap]), + (inputap, [nameof(outputap)]), + (nameof(inputap), [nameof(outputap)]) + ] + if input isa Symbol + # broken because MTKStdlib defines the method for + # `input::Union{Symbol, Vector{Symbol}}` which we can't directly call + @test_broken linearize(sys, input, output) + linfun, ssys = @invoke linearization_function(sys::AbstractSystem, + input::Union{Symbol, Vector{Symbol}, AnalysisPoint, Vector{AnalysisPoint}}, + output::Any) + matrices = linearize(ssys, linfun) + else + matrices, _ = linearize(sys, input, output) + end + # Result should be the same as feedpack(P, 1), i.e., the closed-loop transfer function from plant input to plant output + @test matrices.A[] == -2 + @test matrices.B[] * matrices.C[] == 1 # both positive + @test matrices.D[] == 0 + end +end + +@testset "linearize - variable output - $name" for (name, sys, input, output) in [ + ("inner", sys, sys.plant_input, P.output.u), + ("nested", nested_sys, nested_sys.hej.plant_input, sys.P.output.u) +] + matrices, _ = linearize(sys, input, [output]) + @test matrices.A[] == -2 + @test matrices.B[] * matrices.C[] == 1 # both positive + @test matrices.D[] == 0 +end + +using ModelingToolkit, OrdinaryDiffEq, LinearAlgebra +using ModelingToolkitStandardLibrary.Mechanical.Rotational +using ModelingToolkitStandardLibrary.Blocks: Sine, PID, SecondOrder, Step, RealOutput +using ModelingToolkit: connect + +@testset "Complicated model" begin + # Parameters + m1 = 1 + m2 = 1 + k = 1000 # Spring stiffness + c = 10 # Damping coefficient + @named inertia1 = Inertia(; J = m1) + @named inertia2 = Inertia(; J = m2) + @named spring = Spring(; c = k) + @named damper = Damper(; d = c) + @named torque = Torque() + + function SystemModel(u = nothing; name = :model) + eqs = [connect(torque.flange, inertia1.flange_a) + connect(inertia1.flange_b, spring.flange_a, damper.flange_a) + connect(inertia2.flange_a, spring.flange_b, damper.flange_b)] + if u !== nothing + push!(eqs, connect(torque.tau, u.output)) + return ODESystem(eqs, t; + systems = [ + torque, + inertia1, + inertia2, + spring, + damper, + u + ], + name) + end + ODESystem(eqs, t; systems = [torque, inertia1, inertia2, spring, damper], name) + end + + @named r = Step(start_time = 0) + model = SystemModel() + @named pid = PID(k = 100, Ti = 0.5, Td = 1) + @named filt = SecondOrder(d = 0.9, w = 10) + @named sensor = AngleSensor() + @named er = Add(k2 = -1) + + connections = [connect(r.output, :r, filt.input) + connect(filt.output, er.input1) + connect(pid.ctr_output, :u, model.torque.tau) + connect(model.inertia2.flange_b, sensor.flange) + connect(sensor.phi, :y, er.input2) + connect(er.output, :e, pid.err_input)] + + closed_loop = ODESystem(connections, t, systems = [model, pid, filt, sensor, r, er], + name = :closed_loop, defaults = [ + model.inertia1.phi => 0.0, + model.inertia2.phi => 0.0, + model.inertia1.w => 0.0, + model.inertia2.w => 0.0, + filt.x => 0.0, + filt.xd => 0.0 + ]) + + sys = structural_simplify(closed_loop) + prob = ODEProblem(sys, unknowns(sys) .=> 0.0, (0.0, 4.0)) + sol = solve(prob, Rodas5P(), reltol = 1e-6, abstol = 1e-9) +end From 8a4ca21724b6631d271450b458d4a041db2ab5de Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 30 Dec 2024 00:43:23 +0530 Subject: [PATCH 0522/2176] feat: export `AnalysisPoint`-related tools --- src/ModelingToolkit.jl | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/ModelingToolkit.jl b/src/ModelingToolkit.jl index 486ef63460..29288a5742 100644 --- a/src/ModelingToolkit.jl +++ b/src/ModelingToolkit.jl @@ -293,4 +293,8 @@ export MTKParameters, reorder_dimension_by_tunables!, reorder_dimension_by_tunab export HomotopyContinuationProblem +export AnalysisPoint, Break, PerturbOutput, SampleInput, SensitivityTransform, + ComplementarySensitivityTransform, LoopTransferTransform, apply_transformation, + get_sensitivity_function, get_comp_sensitivity_function, get_looptransfer_function, + get_sensitivity, get_comp_sensitivity, get_looptransfer, open_loop end # module From 0750e62f415e7e66cc4abc1fd9b85a3a2b4a764f Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 30 Dec 2024 00:43:47 +0530 Subject: [PATCH 0523/2176] test: add downstream analysis points tests --- test/downstream/analysis_points.jl | 233 +++++++++++++++++++++++++++++ test/runtests.jl | 1 + 2 files changed, 234 insertions(+) create mode 100644 test/downstream/analysis_points.jl diff --git a/test/downstream/analysis_points.jl b/test/downstream/analysis_points.jl new file mode 100644 index 0000000000..5dc2b8957c --- /dev/null +++ b/test/downstream/analysis_points.jl @@ -0,0 +1,233 @@ +using ModelingToolkit, OrdinaryDiffEq, LinearAlgebra, ControlSystemsBase +using ModelingToolkitStandardLibrary.Mechanical.Rotational +using ModelingToolkitStandardLibrary.Blocks +using ModelingToolkit: connect, AnalysisPoint, t_nounits as t, D_nounits as D +import ControlSystemsBase as CS + +@testset "Complicated model" begin + # Parameters + m1 = 1 + m2 = 1 + k = 1000 # Spring stiffness + c = 10 # Damping coefficient + @named inertia1 = Inertia(; J = m1) + @named inertia2 = Inertia(; J = m2) + @named spring = Spring(; c = k) + @named damper = Damper(; d = c) + @named torque = Torque() + + function SystemModel(u = nothing; name = :model) + eqs = [connect(torque.flange, inertia1.flange_a) + connect(inertia1.flange_b, spring.flange_a, damper.flange_a) + connect(inertia2.flange_a, spring.flange_b, damper.flange_b)] + if u !== nothing + push!(eqs, connect(torque.tau, u.output)) + return ODESystem(eqs, t; + systems = [ + torque, + inertia1, + inertia2, + spring, + damper, + u + ], + name) + end + ODESystem(eqs, t; systems = [torque, inertia1, inertia2, spring, damper], name) + end + + @named r = Step(start_time = 0) + model = SystemModel() + @named pid = PID(k = 100, Ti = 0.5, Td = 1) + @named filt = SecondOrder(d = 0.9, w = 10) + @named sensor = AngleSensor() + @named er = Add(k2 = -1) + + connections = [connect(r.output, :r, filt.input) + connect(filt.output, er.input1) + connect(pid.ctr_output, :u, model.torque.tau) + connect(model.inertia2.flange_b, sensor.flange) + connect(sensor.phi, :y, er.input2) + connect(er.output, :e, pid.err_input)] + + closed_loop = ODESystem(connections, t, systems = [model, pid, filt, sensor, r, er], + name = :closed_loop, defaults = [ + model.inertia1.phi => 0.0, + model.inertia2.phi => 0.0, + model.inertia1.w => 0.0, + model.inertia2.w => 0.0, + filt.x => 0.0, + filt.xd => 0.0 + ]) + + sys = structural_simplify(closed_loop) + prob = ODEProblem(sys, unknowns(sys) .=> 0.0, (0.0, 4.0)) + sol = solve(prob, Rodas5P(), reltol = 1e-6, abstol = 1e-9) + + matrices, ssys = linearize(closed_loop, AnalysisPoint(:r), AnalysisPoint(:y)) + lsys = ss(matrices...) |> sminreal + @test lsys.nx == 8 + + stepres = ControlSystemsBase.step(c2d(lsys, 0.001), 4) + @test Array(stepres.y[:])≈Array(sol(0:0.001:4, idxs = model.inertia2.phi)) rtol=1e-4 + + matrices, ssys = get_sensitivity(closed_loop, :y) + So = ss(matrices...) + + matrices, ssys = get_sensitivity(closed_loop, :u) + Si = ss(matrices...) + + @test tf(So) ≈ tf(Si) +end + +@testset "Analysis points with subsystems" begin + @named P = FirstOrder(k = 1, T = 1) + @named C = Gain(; k = 1) + @named add = Blocks.Add(k2 = -1) + + eqs = [connect(P.output, :plant_output, add.input2) + connect(add.output, C.input) + connect(C.output, :plant_input, P.input)] + + # eqs = [connect(P.output, add.input2) + # connect(add.output, C.input) + # connect(C.output, P.input)] + + sys_inner = ODESystem(eqs, t, systems = [P, C, add], name = :inner) + + @named r = Constant(k = 1) + @named F = FirstOrder(k = 1, T = 3) + + eqs = [connect(r.output, F.input) + connect(F.output, sys_inner.add.input1)] + sys_outer = ODESystem(eqs, t, systems = [F, sys_inner, r], name = :outer) + + # test first that the structural_simplify works correctly + ssys = structural_simplify(sys_outer) + prob = ODEProblem(ssys, Pair[], (0, 10)) + @test_nowarn solve(prob, Rodas5()) + + matrices, _ = get_sensitivity(sys_outer, sys_outer.inner.plant_input) + lsys = sminreal(ss(matrices...)) + @test lsys.A[] == -2 + @test lsys.B[] * lsys.C[] == -1 # either one negative + @test lsys.D[] == 1 + + matrices_So, _ = get_sensitivity(sys_outer, sys_outer.inner.plant_output) + lsyso = sminreal(ss(matrices_So...)) + @test lsys == lsyso || lsys == -1 * lsyso * (-1) # Output and input sensitivities are equal for SISO systems +end + +@testset "multilevel system with loop openings" begin + @named P_inner = FirstOrder(k = 1, T = 1) + @named feedback = Feedback() + @named ref = Step() + @named sys_inner = ODESystem( + [connect(P_inner.output, :y, feedback.input2) + connect(feedback.output, :u, P_inner.input) + connect(ref.output, :r, feedback.input1)], + t, + systems = [P_inner, feedback, ref]) + + P_not_broken, _ = linearize(sys_inner, AnalysisPoint(:u), AnalysisPoint(:y)) + @test P_not_broken.A[] == -2 + P_broken, _ = linearize(sys_inner, AnalysisPoint(:u), AnalysisPoint(:y), + loop_openings = [AnalysisPoint(:u)]) + @test P_broken.A[] == -1 + P_broken, _ = linearize(sys_inner, AnalysisPoint(:u), AnalysisPoint(:y), + loop_openings = [AnalysisPoint(:y)]) + @test P_broken.A[] == -1 + + Sinner = sminreal(ss(get_sensitivity(sys_inner, :u)[1]...)) + + @named sys_inner = ODESystem( + [connect(P_inner.output, :y, feedback.input2) + connect(feedback.output, :u, P_inner.input)], + t, + systems = [P_inner, feedback]) + + @named P_outer = FirstOrder(k = rand(), T = rand()) + + @named sys_outer = ODESystem( + [connect(sys_inner.P_inner.output, :y2, P_outer.input) + connect(P_outer.output, :u2, sys_inner.feedback.input1)], + t, + systems = [P_outer, sys_inner]) + + Souter = sminreal(ss(get_sensitivity(sys_outer, :sys_inner_u)[1]...)) + + Sinner2 = sminreal(ss(get_sensitivity( + sys_outer, :sys_inner_u, loop_openings = [:y2])[1]...)) + + @test Sinner.nx == 1 + @test Sinner == Sinner2 + @test Souter.nx == 2 +end + +@testset "sensitivities in multivariate signals" begin + A = [-0.994 -0.0794; -0.006242 -0.0134] + B = [-0.181 -0.389; 1.1 1.12] + C = [1.74 0.72; -0.33 0.33] + D = [0.0 0.0; 0.0 0.0] + @named P = Blocks.StateSpace(A, B, C, D) + Pss = CS.ss(A, B, C, D) + + A = [-0.097;;] + B = [-0.138 -1.02] + C = [-0.076; 0.09;;] + D = [0.0 0.0; 0.0 0.0] + @named K = Blocks.StateSpace(A, B, C, D) + Kss = CS.ss(A, B, C, D) + + eqs = [connect(P.output, :plant_output, K.input) + connect(K.output, :plant_input, P.input)] + sys = ODESystem(eqs, t, systems = [P, K], name = :hej) + + matrices, _ = Blocks.get_sensitivity(sys, :plant_input) + S = CS.feedback(I(2), Kss * Pss, pos_feedback = true) + + @test CS.tf(CS.ss(matrices...)) ≈ CS.tf(S) + + matrices, _ = Blocks.get_comp_sensitivity(sys, :plant_input) + T = -CS.feedback(Kss * Pss, I(2), pos_feedback = true) + + # bodeplot([ss(matrices...), T]) + @test CS.tf(CS.ss(matrices...)) ≈ CS.tf(T) + + matrices, _ = Blocks.get_looptransfer( + sys, :plant_input) + L = Kss * Pss + @test CS.tf(CS.ss(matrices...)) ≈ CS.tf(L) + + matrices, _ = linearize(sys, :plant_input, :plant_output) + G = CS.feedback(Pss, Kss, pos_feedback = true) + @test CS.tf(CS.ss(matrices...)) ≈ CS.tf(G) +end + +@testset "multiple analysis points" begin + @named P = FirstOrder(k = 1, T = 1) + @named C = Gain(; k = 1) + @named add = Blocks.Add(k2 = -1) + + eqs = [connect(P.output, :plant_output, add.input2) + connect(add.output, C.input) + connect(C.output, :plant_input, P.input)] + + sys_inner = ODESystem(eqs, t, systems = [P, C, add], name = :inner) + + @named r = Constant(k = 1) + @named F = FirstOrder(k = 1, T = 3) + + eqs = [connect(r.output, F.input) + connect(F.output, sys_inner.add.input1)] + sys_outer = ODESystem(eqs, t, systems = [F, sys_inner, r], name = :outer) + + matrices, _ = get_sensitivity(sys_outer, [:, :inner_plant_output]) + + Ps = tf(1, [1, 1]) |> ss + Cs = tf(1) |> ss + + G = CS.ss(matrices...) |> sminreal + Si = CS.feedback(1, Cs * Ps) + @test tf(G[1, 1]) ≈ tf(Si) +end diff --git a/test/runtests.jl b/test/runtests.jl index af83ffccd8..d38aa86bb2 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -111,6 +111,7 @@ end @safetestset "Linearization Tests" include("downstream/linearize.jl") @safetestset "Linearization Dummy Derivative Tests" include("downstream/linearization_dd.jl") @safetestset "Inverse Models Test" include("downstream/inversemodel.jl") + @safetestset "Analysis Points Test" include("downstream/analysis_points.jl") end if GROUP == "All" || GROUP == "Extensions" From 469eda8837e7f7db533763e108e578fb9938bfe9 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 2 Jan 2025 23:33:41 +0530 Subject: [PATCH 0524/2176] feat: handle array variables as inputs/outputs of `linearization_function` --- src/systems/abstractsystem.jl | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index 9b5ff7183d..3790cb8eb0 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -2374,6 +2374,12 @@ function linearization_function(sys::AbstractSystem, inputs, op = Dict(op) inputs isa AbstractVector || (inputs = [inputs]) outputs isa AbstractVector || (outputs = [outputs]) + inputs = mapreduce(vcat, inputs; init = []) do var + symbolic_type(var) == ArraySymbolic() ? collect(var) : [var] + end + outputs = mapreduce(vcat, outputs; init = []) do var + symbolic_type(var) == ArraySymbolic() ? collect(var) : [var] + end ssys, diff_idxs, alge_idxs, input_idxs = io_preprocessing(sys, inputs, outputs; simplify, kwargs...) From ccfed451bfb8ba124fc4564eb27d3fd5111642f7 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 2 Jan 2025 23:34:02 +0530 Subject: [PATCH 0525/2176] fix: fix array variable handling in `get_analysis_variable` --- src/systems/analysis_points.jl | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/systems/analysis_points.jl b/src/systems/analysis_points.jl index 605eb32749..ef20377147 100644 --- a/src/systems/analysis_points.jl +++ b/src/systems/analysis_points.jl @@ -255,11 +255,11 @@ function get_analysis_variable(var, name, iv; perturb = true) if perturb name = Symbol(:d_, name) end - if Symbolics.isarraysymbolic(var) + if symbolic_type(var) == ArraySymbolic() T = Array{eltype(symtype(var)), ndims(var)} pvar = unwrap(only(@variables $name(iv)::T)) pvar = setmetadata(pvar, Symbolics.ArrayShapeCtx, Symbolics.shape(var)) - default = zeros(symtype(var), size(var)) + default = zeros(eltype(symtype(var)), size(var)) else T = symtype(var) pvar = unwrap(only(@variables $name(iv)::T)) @@ -429,7 +429,7 @@ function apply_transformation(tf::PerturbOutput, sys::AbstractSystem) ap_ivar = ap_var(ap.input) new_var, new_def = get_analysis_variable(ap_ivar, nameof(ap), get_iv(sys)) for outsys in ap.outputs - push!(ap_sys_eqs, ap_var(outsys) ~ ap_ivar + new_var) + push!(ap_sys_eqs, ap_var(outsys) ~ ap_ivar + wrap(new_var)) end # add variable unks = copy(get_unknowns(ap_sys)) @@ -446,7 +446,7 @@ function apply_transformation(tf::PerturbOutput, sys::AbstractSystem) out_var, out_def = get_analysis_variable( ap_ivar, nameof(ap), get_iv(sys); perturb = false) defs[out_var] = out_def - push!(ap_sys_eqs, out_var ~ ap_ivar + new_var) + push!(ap_sys_eqs, out_var ~ ap_ivar + wrap(new_var)) push!(unks, out_var) return ap_sys, (new_var, out_var) @@ -526,11 +526,11 @@ function apply_transformation(cst::ComplementarySensitivityTransform, sys::Abstr # but comp sensitivity wants `output + du ~ input`. Thus, `du ~ -_du`. eqs = copy(get_eqs(sys)) @set! sys.eqs = eqs - push!(eqs, du ~ -_du) + push!(eqs, du ~ -wrap(_du)) defs = copy(get_defaults(sys)) @set! sys.defaults = defs - defs[du] = -_du + defs[du] = -wrap(_du) return sys, (du, u) end From f9b62e2b3b16cf019fc355bbeec231e951e54409 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 2 Jan 2025 23:34:59 +0530 Subject: [PATCH 0526/2176] fix: fix naming of new variable in `ComplementarySensitivityTransform` --- src/systems/analysis_points.jl | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/systems/analysis_points.jl b/src/systems/analysis_points.jl index ef20377147..e71e67c691 100644 --- a/src/systems/analysis_points.jl +++ b/src/systems/analysis_points.jl @@ -519,7 +519,10 @@ end function apply_transformation(cst::ComplementarySensitivityTransform, sys::AbstractSystem) sys, (u,) = apply_transformation(GetInput(cst.ap), sys) - sys, (du,) = apply_transformation(AddVariable(cst.ap, Symbol(:comp_sens_du)), sys) + sys, (du,) = apply_transformation( + AddVariable( + cst.ap, Symbol(namespace_hierarchy(nameof(cst.ap))[end], :_comp_sens_du)), + sys) sys, (_du,) = apply_transformation(PerturbOutput(cst.ap), sys) # `PerturbOutput` adds the equation `input + _du ~ output` From ed5fa70d33f4b3c8e950a85bcddd71b63f3695ed Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 2 Jan 2025 23:35:41 +0530 Subject: [PATCH 0527/2176] fix: handle `loop_openings`, `system_modifier` kwargs in `get_*` linear analysis functions --- src/systems/analysis_points.jl | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/systems/analysis_points.jl b/src/systems/analysis_points.jl index e71e67c691..b8ec48eb68 100644 --- a/src/systems/analysis_points.jl +++ b/src/systems/analysis_points.jl @@ -611,8 +611,10 @@ function get_looptransfer_function( end for f in [:get_sensitivity, :get_comp_sensitivity, :get_looptransfer] - @eval function $f(sys, ap, args...; kwargs...) - lin_fun, ssys = $(Symbol(f, :_function))(sys, ap, args...; kwargs...) + @eval function $f( + sys, ap, args...; loop_openings = [], system_modifier = identity, kwargs...) + lin_fun, ssys = $(Symbol(f, :_function))( + sys, ap, args...; loop_openings, system_modifier, kwargs...) ModelingToolkit.linearize(ssys, lin_fun; kwargs...), ssys end end From c8a4127b6580c089f12e51a435268f5b62fb869f Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 2 Jan 2025 23:36:09 +0530 Subject: [PATCH 0528/2176] fix: fix handling of `loop_openings` in `linearization_function` --- src/systems/analysis_points.jl | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/src/systems/analysis_points.jl b/src/systems/analysis_points.jl index b8ec48eb68..180b730a98 100644 --- a/src/systems/analysis_points.jl +++ b/src/systems/analysis_points.jl @@ -630,25 +630,33 @@ end function linearization_function(sys::AbstractSystem, inputs::Union{Symbol, Vector{Symbol}, AnalysisPoint, Vector{AnalysisPoint}}, outputs; loop_openings = [], system_modifier = identity, kwargs...) - sys = handle_loop_openings(sys, loop_openings) - + loop_openings = Set(map(nameof, canonicalize_ap(loop_openings))) inputs = canonicalize_ap(inputs) outputs = canonicalize_ap(outputs) input_vars = [] for input in inputs - sys, (input_var,) = apply_transformation(PerturbOutput(input), sys) + if nameof(input) in loop_openings + delete!(loop_openings, nameof(input)) + sys, (input_var,) = apply_transformation(Break(input, true), sys) + else + sys, (input_var,) = apply_transformation(PerturbOutput(input), sys) + end push!(input_vars, input_var) end output_vars = [] for output in outputs if output isa AnalysisPoint - sys, (output_var,) = apply_transformation(GetInput(output), sys) - push!(output_vars, output_var) + sys, (output_var,) = apply_transformation(AddVariable(output), sys) + sys, (input_var,) = apply_transformation(GetInput(output), sys) + push!(get_eqs(sys), output_var ~ input_var) else - push!(output_vars, output) + output_var = output end + push!(output_vars, output_var) end + sys = handle_loop_openings(sys, collect(loop_openings)) + return linearization_function(system_modifier(sys), input_vars, output_vars; kwargs...) end From 0610d67ce61fc08f9fbb9bdb4f9151826144da82 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 2 Jan 2025 23:36:35 +0530 Subject: [PATCH 0529/2176] test: fix analysis point tests --- test/analysis_points.jl | 72 ++--------------------------------------- 1 file changed, 3 insertions(+), 69 deletions(-) diff --git a/test/analysis_points.jl b/test/analysis_points.jl index 6eae30314e..c888261db2 100644 --- a/test/analysis_points.jl +++ b/test/analysis_points.jl @@ -2,7 +2,7 @@ using ModelingToolkit, ModelingToolkitStandardLibrary.Blocks using OrdinaryDiffEq, LinearAlgebra using Test using ModelingToolkit: t_nounits as t, D_nounits as D, AnalysisPoint, get_sensitivity, - get_comp_sensitivity, get_looptransfer + get_comp_sensitivity, get_looptransfer, open_loop, AbstractSystem using Symbolics: NAMESPACE_SEPARATOR @testset "AnalysisPoint is lowered to `connect`" begin @@ -13,14 +13,14 @@ using Symbolics: NAMESPACE_SEPARATOR eqs = [connect(P.output, C.input) connect(C.output, ap, P.input)] sys_ap = ODESystem(eqs, t, systems = [P, C], name = :hej) - sys_ap2 = @test_nowarn expand_connections(sys) + sys_ap2 = @test_nowarn expand_connections(sys_ap) @test all(eq -> !(eq.lhs isa AnalysisPoint), equations(sys_ap2)) eqs = [connect(P.output, C.input) connect(C.output, P.input)] sys_normal = ODESystem(eqs, t, systems = [P, C], name = :hej) - sys_normal2 = @test_nowarn expand_connections(sys) + sys_normal2 = @test_nowarn expand_connections(sys_normal) @test isequal(sys_ap2, sys_normal2) end @@ -191,69 +191,3 @@ end @test matrices.B[] * matrices.C[] == 1 # both positive @test matrices.D[] == 0 end - -using ModelingToolkit, OrdinaryDiffEq, LinearAlgebra -using ModelingToolkitStandardLibrary.Mechanical.Rotational -using ModelingToolkitStandardLibrary.Blocks: Sine, PID, SecondOrder, Step, RealOutput -using ModelingToolkit: connect - -@testset "Complicated model" begin - # Parameters - m1 = 1 - m2 = 1 - k = 1000 # Spring stiffness - c = 10 # Damping coefficient - @named inertia1 = Inertia(; J = m1) - @named inertia2 = Inertia(; J = m2) - @named spring = Spring(; c = k) - @named damper = Damper(; d = c) - @named torque = Torque() - - function SystemModel(u = nothing; name = :model) - eqs = [connect(torque.flange, inertia1.flange_a) - connect(inertia1.flange_b, spring.flange_a, damper.flange_a) - connect(inertia2.flange_a, spring.flange_b, damper.flange_b)] - if u !== nothing - push!(eqs, connect(torque.tau, u.output)) - return ODESystem(eqs, t; - systems = [ - torque, - inertia1, - inertia2, - spring, - damper, - u - ], - name) - end - ODESystem(eqs, t; systems = [torque, inertia1, inertia2, spring, damper], name) - end - - @named r = Step(start_time = 0) - model = SystemModel() - @named pid = PID(k = 100, Ti = 0.5, Td = 1) - @named filt = SecondOrder(d = 0.9, w = 10) - @named sensor = AngleSensor() - @named er = Add(k2 = -1) - - connections = [connect(r.output, :r, filt.input) - connect(filt.output, er.input1) - connect(pid.ctr_output, :u, model.torque.tau) - connect(model.inertia2.flange_b, sensor.flange) - connect(sensor.phi, :y, er.input2) - connect(er.output, :e, pid.err_input)] - - closed_loop = ODESystem(connections, t, systems = [model, pid, filt, sensor, r, er], - name = :closed_loop, defaults = [ - model.inertia1.phi => 0.0, - model.inertia2.phi => 0.0, - model.inertia1.w => 0.0, - model.inertia2.w => 0.0, - filt.x => 0.0, - filt.xd => 0.0 - ]) - - sys = structural_simplify(closed_loop) - prob = ODEProblem(sys, unknowns(sys) .=> 0.0, (0.0, 4.0)) - sol = solve(prob, Rodas5P(), reltol = 1e-6, abstol = 1e-9) -end From bb23e6216360b625314e5491e28196220ffdc900 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 2 Jan 2025 23:36:48 +0530 Subject: [PATCH 0530/2176] test: fix and port over remaining analysis point downstream tests --- test/downstream/analysis_points.jl | 58 +++++++++++++++++++++++++----- 1 file changed, 50 insertions(+), 8 deletions(-) diff --git a/test/downstream/analysis_points.jl b/test/downstream/analysis_points.jl index 5dc2b8957c..85d271a547 100644 --- a/test/downstream/analysis_points.jl +++ b/test/downstream/analysis_points.jl @@ -1,7 +1,8 @@ using ModelingToolkit, OrdinaryDiffEq, LinearAlgebra, ControlSystemsBase using ModelingToolkitStandardLibrary.Mechanical.Rotational using ModelingToolkitStandardLibrary.Blocks -using ModelingToolkit: connect, AnalysisPoint, t_nounits as t, D_nounits as D +using ModelingToolkit: connect, AnalysisPoint, t_nounits as t, D_nounits as D, + get_sensitivity, get_comp_sensitivity, get_looptransfer, open_loop import ControlSystemsBase as CS @testset "Complicated model" begin @@ -154,10 +155,10 @@ end t, systems = [P_outer, sys_inner]) - Souter = sminreal(ss(get_sensitivity(sys_outer, :sys_inner_u)[1]...)) + Souter = sminreal(ss(get_sensitivity(sys_outer, sys_inner.u)[1]...)) Sinner2 = sminreal(ss(get_sensitivity( - sys_outer, :sys_inner_u, loop_openings = [:y2])[1]...)) + sys_outer, sys_inner.u, loop_openings = [:y2])[1]...)) @test Sinner.nx == 1 @test Sinner == Sinner2 @@ -183,23 +184,23 @@ end connect(K.output, :plant_input, P.input)] sys = ODESystem(eqs, t, systems = [P, K], name = :hej) - matrices, _ = Blocks.get_sensitivity(sys, :plant_input) + matrices, _ = get_sensitivity(sys, :plant_input) S = CS.feedback(I(2), Kss * Pss, pos_feedback = true) @test CS.tf(CS.ss(matrices...)) ≈ CS.tf(S) - matrices, _ = Blocks.get_comp_sensitivity(sys, :plant_input) + matrices, _ = get_comp_sensitivity(sys, :plant_input) T = -CS.feedback(Kss * Pss, I(2), pos_feedback = true) # bodeplot([ss(matrices...), T]) @test CS.tf(CS.ss(matrices...)) ≈ CS.tf(T) - matrices, _ = Blocks.get_looptransfer( + matrices, _ = get_looptransfer( sys, :plant_input) L = Kss * Pss @test CS.tf(CS.ss(matrices...)) ≈ CS.tf(L) - matrices, _ = linearize(sys, :plant_input, :plant_output) + matrices, _ = linearize(sys, AnalysisPoint(:plant_input), :plant_output) G = CS.feedback(Pss, Kss, pos_feedback = true) @test CS.tf(CS.ss(matrices...)) ≈ CS.tf(G) end @@ -222,7 +223,8 @@ end connect(F.output, sys_inner.add.input1)] sys_outer = ODESystem(eqs, t, systems = [F, sys_inner, r], name = :outer) - matrices, _ = get_sensitivity(sys_outer, [:, :inner_plant_output]) + matrices, _ = get_sensitivity( + sys_outer, [sys_outer.inner.plant_input, sys_outer.inner.plant_output]) Ps = tf(1, [1, 1]) |> ss Cs = tf(1) |> ss @@ -230,4 +232,44 @@ end G = CS.ss(matrices...) |> sminreal Si = CS.feedback(1, Cs * Ps) @test tf(G[1, 1]) ≈ tf(Si) + + So = CS.feedback(1, Ps * Cs) + @test tf(G[2, 2]) ≈ tf(So) + @test tf(G[1, 2]) ≈ tf(-CS.feedback(Cs, Ps)) + @test tf(G[2, 1]) ≈ tf(CS.feedback(Ps, Cs)) + + matrices, _ = get_comp_sensitivity( + sys_outer, [sys_outer.inner.plant_input, sys_outer.inner.plant_output]) + + G = CS.ss(matrices...) |> sminreal + Ti = CS.feedback(Cs * Ps) + @test tf(G[1, 1]) ≈ tf(Ti) + + To = CS.feedback(Ps * Cs) + @test tf(G[2, 2]) ≈ tf(To) + @test tf(G[1, 2]) ≈ tf(CS.feedback(Cs, Ps)) # The negative sign appears in a confusing place due to negative feedback not happening through Ps + @test tf(G[2, 1]) ≈ tf(-CS.feedback(Ps, Cs)) + + # matrices, _ = get_looptransfer(sys_outer, [:inner_plant_input, :inner_plant_output]) + matrices, _ = get_looptransfer(sys_outer, sys_outer.inner.plant_input) + L = CS.ss(matrices...) |> sminreal + @test tf(L) ≈ -tf(Cs * Ps) + + matrices, _ = get_looptransfer(sys_outer, sys_outer.inner.plant_output) + L = CS.ss(matrices...) |> sminreal + @test tf(L[1, 1]) ≈ -tf(Ps * Cs) + + # Calling looptransfer like below is not the intended way, but we can work out what it should return if we did so it remains a valid test + matrices, _ = get_looptransfer( + sys_outer, [sys_outer.inner.plant_input, sys_outer.inner.plant_output]) + L = CS.ss(matrices...) |> sminreal + @test tf(L[1, 1]) ≈ tf(0) + @test tf(L[2, 2]) ≈ tf(0) + @test sminreal(L[1, 2]) ≈ ss(-1) + @test tf(L[2, 1]) ≈ tf(Ps) + + matrices, _ = linearize( + sys_outer, [sys_outer.inner.plant_input], [sys_inner.plant_output]) + G = CS.ss(matrices...) |> sminreal + @test tf(G) ≈ tf(CS.feedback(Ps, Cs)) end From 89ed0cf57e6187c0a68c41a68f899aebf5eee516 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 3 Jan 2025 00:27:01 +0530 Subject: [PATCH 0531/2176] docs: add and update docstrings for analysis points and transformations --- src/systems/analysis_points.jl | 378 +++++++++++++++++++++++++++------ 1 file changed, 313 insertions(+), 65 deletions(-) diff --git a/src/systems/analysis_points.jl b/src/systems/analysis_points.jl index 180b730a98..9ff06109e5 100644 --- a/src/systems/analysis_points.jl +++ b/src/systems/analysis_points.jl @@ -1,25 +1,85 @@ """ $(TYPEDEF) - $(TYPEDSIGNATURES) + AnalysisPoint(input, name::Symbol, outputs::Vector) Create an AnalysisPoint for linear analysis. Analysis points can be created by calling ``` -connect(in, :ap_name, out...) +connect(out, :ap_name, in...) ``` -Where `in` is the input to the connection, and `out...` are the outputs. In the context of -ModelingToolkitStandardLibrary.jl, `in` is a `RealOutput` connector and `out...` are all -`RealInput` connectors. All involved connectors are required to either have an unknown named +Where `out` is the output being connected to the inputs `in...`. All involved +connectors (input and outputs) are required to either have an unknown named `u` or a single unknown, all of which should have the same size. + +See also [`get_sensitivity`](@ref), [`get_comp_sensitivity`](@ref), [`get_looptransfer`](@ref), [`open_loop`](@ref) + +# Fields + +$(TYPEDFIELDS) + +# Example + +```julia +using ModelingToolkit +using ModelingToolkitStandardLibrary.Blocks +using ModelingToolkit: t_nounits as t + +@named P = FirstOrder(k = 1, T = 1) +@named C = Gain(; k = -1) +t = ModelingToolkit.get_iv(P) + +eqs = [connect(P.output, C.input) + connect(C.output, :plant_input, P.input)] +sys = ODESystem(eqs, t, systems = [P, C], name = :feedback_system) + +matrices_S, _ = get_sensitivity(sys, :plant_input) # Compute the matrices of a state-space representation of the (input) sensitivity function. +matrices_T, _ = get_comp_sensitivity(sys, :plant_input) +``` + +Continued linear analysis and design can be performed using ControlSystemsBase.jl. +Create `ControlSystemsBase.StateSpace` objects using + +```julia +using ControlSystemsBase, Plots +S = ss(matrices_S...) +T = ss(matrices_T...) +bodeplot([S, T], lab = ["S" "T"]) +``` + +The sensitivity functions obtained this way should be equivalent to the ones obtained with the code below + +```julia +using ControlSystemsBase +P = tf(1.0, [1, 1]) +C = 1 # Negative feedback assumed in ControlSystems +S = sensitivity(P, C) # or feedback(1, P*C) +T = comp_sensitivity(P, C) # or feedback(P*C) +``` """ struct AnalysisPoint + """ + The input to the connection. In the context of ModelingToolkitStandardLibrary.jl, + this is a `RealOutput` connector. + """ input::Any + """ + The name of the analysis point. + """ name::Symbol + """ + The outputs of the connection. In the context of ModelingToolkitStandardLibrary.jl, + these are all `RealInput` connectors. + """ outputs::Union{Nothing, Vector{Any}} end AnalysisPoint() = AnalysisPoint(nothing, Symbol(), nothing) +""" + $(TYPEDSIGNATURES) + +Create an `AnalysisPoint` with the given name, with no input or outputs specified. +""" AnalysisPoint(name::Symbol) = AnalysisPoint(nothing, name, nothing) Base.nameof(ap::AnalysisPoint) = ap.name @@ -78,9 +138,35 @@ function Symbolics.connect(in, ap::AnalysisPoint, outs...) end """ - $(TYPEDSIGNATURES) + connect(output_connector, ap_name::Symbol, input_connector; verbose = true) + connect(output_connector, ap::AnalysisPoint, input_connector; verbose = true) + +Connect `output_connector` and `input_connector` with an [`AnalysisPoint`](@ref) inbetween. +The incoming connection `output_connector` is expected to be an output connector (for +example, `ModelingToolkitStandardLibrary.Blocks.RealOutput`), and vice versa. + +*PLEASE NOTE*: The connection is assumed to be *causal*, meaning that -Create an `AnalysisPoint` connection connecting `in` to `outs...`. +```julia +@named P = FirstOrder(k = 1, T = 1) +@named C = Gain(; k = -1) +connect(C.output, :plant_input, P.input) +``` + +is correct, whereas + +```julia +connect(P.input, :plant_input, C.output) +``` + +typically is not (unless the model is an inverse model). + +# Arguments: + + - `output_connector`: An output connector + - `input_connector`: An input connector + - `ap`: An explicitly created [`AnalysisPoint`](@ref) + - `ap_name`: If a name is given, an [`AnalysisPoint`](@ref) with the given name will be created automatically. """ function Symbolics.connect(in::AbstractSystem, name::Symbol, out, outs...) return AnalysisPoint() ~ AnalysisPoint(in, name, [out; collect(outs)]) @@ -198,19 +284,23 @@ function modify_nested_subsystem( # recursive helper function which does the searching and modification function _helper(sys::AbstractSystem, i::Int) - # we reached past the end, so everything matched and - # `sys` is the system to modify. if i > length(hierarchy) + # we reached past the end, so everything matched and + # `sys` is the system to modify. sys, vars = fn(sys) else + # find the subsystem with the given name and error otherwise cur = hierarchy[i] idx = findfirst(subsys -> nameof(subsys) == cur, get_systems(sys)) idx === nothing && error("System $(join([nameof(root); hierarchy[1:i-1]], '.')) does not have a subsystem named $cur.") + # recurse into new subsystem newsys, vars = _helper(get_systems(sys)[idx], i + 1) + # update this system with modified subsystem @set! sys.systems[idx] = newsys end + # only namespace variables from inner systems if i != 1 vars = ntuple(Val(length(vars))) do i renamespace(sys, vars[i]) @@ -270,6 +360,15 @@ end #### PRIMITIVE TRANSFORMATIONS +const DOC_WILL_REMOVE_AP = """ + Note that this transformation will remove `ap`, causing any subsequent transformations \ + referring to it to fail.\ + """ + +const DOC_ADDED_VARIABLE = """ + The added variable(s) will have a default of zero, of the appropriate type and size.\ + """ + """ $(TYPEDEF) @@ -278,11 +377,9 @@ it will add a new input variable which connects to the outputs of the analysis p `apply_transformation` returns the new input variable (if added) as the auxiliary information. The new input variable will have the name `Symbol(:d_, nameof(ap))`. -Note that this transformation will remove `ap`, causing any subsequent transformations -referring to it to fail. +$DOC_WILL_REMOVE_AP -The added variable, if present, will have a default of zero, of the appropriate type and -size. +$DOC_ADDED_VARIABLE ## Fields @@ -340,9 +437,7 @@ end $(TYPEDEF) A transformation which returns the variable corresponding to the input of the analysis -point. - -`apply_transformation` returns the variable as auxiliary information. +point. Does not modify the system. ## Fields @@ -350,7 +445,7 @@ $(TYPEDFIELDS) """ struct GetInput <: AnalysisPointTransformation """ - The analysis point to add the output to. + The analysis point to get the input of. """ ap::AnalysisPoint end @@ -380,15 +475,12 @@ the analysis point before connecting to the outputs. The new variable will have If `with_output == true`, also creates an additional new variable which has the value provided to the outputs after the above modification. This new variable has the same name -as the analysis point. +as the analysis point and will be the second variable in the tuple of new variables returned +from `apply_transformation`. -`apply_transformation` returns a 1-tuple of the perturbation variable if -`with_output == false` and a 2-tuple of the perturbation variable and output variable if -`with_output == true`. +$DOC_WILL_REMOVE_AP -Removes the analysis point `ap`, so any subsequent transformations requiring it will fail. - -The added variable(s) will have a default of zero, of the appropriate type and size. +$DOC_ADDED_VARIABLE ## Fields @@ -454,17 +546,33 @@ function apply_transformation(tf::PerturbOutput, sys::AbstractSystem) end """ - $(TYPEDSIGNATURES) + $(TYPEDEF) A transformation which adds a variable named `name` to the system containing the analysis -point `ap`. The added variable has the same type and size as the input of the analysis -point. +point `ap`. $DOC_ADDED_VARIABLE + +# Fields + +$(TYPEDFIELDS) """ struct AddVariable <: AnalysisPointTransformation + """ + The analysis point in the system to modify, and whose input should be used as the + template for the new variable. + """ ap::AnalysisPoint + """ + The name of the added variable. + """ name::Symbol end +""" + $(TYPEDSIGNATURES) + +Add a new variable to the system containing analysis point `ap` with the same name as the +analysis point. +""" AddVariable(ap::AnalysisPoint) = AddVariable(ap, nameof(ap)) function apply_transformation(tf::AddVariable, sys::AbstractSystem) @@ -494,11 +602,12 @@ end $(TYPEDSIGNATURES) A transformation enable calculating the sensitivity function about the analysis point `ap`. -`apply_transformation` returns a 2-tuple `du, u` as auxiliary information. +The returned added variables are `(du, u)` where `du` is the perturbation added to the +input, and `u` is the output after perturbation. -Removes the analysis point `ap`, so any subsequent transformations requiring it will fail. +$DOC_WILL_REMOVE_AP -The added variables will have a default of zero, of the appropriate type and size. +$DOC_ADDED_VARIABLE """ SensitivityTransform(ap::AnalysisPoint) = PerturbOutput(ap, true) @@ -506,14 +615,21 @@ SensitivityTransform(ap::AnalysisPoint) = PerturbOutput(ap, true) $(TYPEDEF) A transformation to enable calculating the complementary sensitivity function about the -analysis point `ap`. `apply_transformation` returns a 2-tuple `du, u` as auxiliary -information. +analysis point `ap`. The returned added variables are `(du, u)` where `du` is the +perturbation added to the outputs and `u` is the input to the analysis point. + +$DOC_WILL_REMOVE_AP + +$DOC_ADDED_VARIABLE -Removes the analysis point `ap`, so any subsequent transformations requiring it will fail. +# Fields -The added variables will have a default of zero, of the appropriate type and size. +$(TYPEDFIELDS) """ struct ComplementarySensitivityTransform <: AnalysisPointTransformation + """ + The analysis point to modify. + """ ap::AnalysisPoint end @@ -537,7 +653,25 @@ function apply_transformation(cst::ComplementarySensitivityTransform, sys::Abstr return sys, (du, u) end +""" + $(TYPEDEF) + +A transformation to enable calculating the loop transfer function about the analysis point +`ap`. The returned added variables are `(du, u)` where `du` feeds into the outputs of `ap` +and `u` is the input of `ap`. + +$DOC_WILL_REMOVE_AP + +$DOC_ADDED_VARIABLE + +# Fields + +$(TYPEDFIELDS) +""" struct LoopTransferTransform <: AnalysisPointTransformation + """ + The analysis point to modify. + """ ap::AnalysisPoint end @@ -547,8 +681,13 @@ function apply_transformation(tf::LoopTransferTransform, sys::AbstractSystem) return sys, (du, u) end -### TODO: Move these +""" + $(TYPEDSIGNATURES) +A utility function to get the "canonical" form of a list of analysis points. Always returns +a list of values. Any value that cannot be turned into an `AnalysisPoint` (i.e. isn't +already an `AnalysisPoint` or `Symbol`) is simply wrapped in an array. +""" canonicalize_ap(ap::Symbol) = [AnalysisPoint(ap)] canonicalize_ap(ap::AnalysisPoint) = [ap] canonicalize_ap(ap) = [ap] @@ -556,6 +695,11 @@ function canonicalize_ap(aps::Vector) mapreduce(canonicalize_ap, vcat, aps; init = []) end +""" + $(TYPEDSIGNATURES) + +Given a list of analysis points, break the connection for each and set the output to zero. +""" function handle_loop_openings(sys::AbstractSystem, aps) for ap in canonicalize_ap(aps) sys, (outvar,) = apply_transformation(Break(ap, true), sys) @@ -568,63 +712,124 @@ function handle_loop_openings(sys::AbstractSystem, aps) return sys end -function get_sensitivity_function( - sys::AbstractSystem, aps; system_modifier = identity, loop_openings = [], kwargs...) +const DOC_LOOP_OPENINGS = """ + - `loop_openings`: A list of analysis points whose connections should be removed and + the outputs set to zero as a part of the linear analysis. +""" + +const DOC_SYS_MODIFIER = """ + - `system_modifier`: A function taking the transformed system and applying any + additional transformations, returning the modified system. The modified system + is passed to `linearization_function`. +""" +""" + $(TYPEDSIGNATURES) + +Utility function for linear analyses that apply a transformation `transform`, which +returns the added variables `(du, u)`, to each of the analysis points in `aps` and then +calls `linearization_function` with all the `du`s as inputs and `u`s as outputs. Returns +the linearization function and modified, simplified system. + +# Keyword arguments + +$DOC_LOOP_OPENINGS +$DOC_SYS_MODIFIER + +All other keyword arguments are forwarded to `linearization_function`. +""" +function get_linear_analysis_function( + sys::AbstractSystem, transform, aps; system_modifier = identity, loop_openings = [], kwargs...) sys = handle_loop_openings(sys, loop_openings) aps = canonicalize_ap(aps) dus = [] us = [] for ap in aps - sys, (du, u) = apply_transformation(SensitivityTransform(ap), sys) + sys, (du, u) = apply_transformation(transform(ap), sys) push!(dus, du) push!(us, u) end linearization_function(system_modifier(sys), dus, us; kwargs...) end -function get_comp_sensitivity_function( - sys::AbstractSystem, aps; system_modifier = identity, loop_openings = [], kwargs...) - sys = handle_loop_openings(sys, loop_openings) - aps = canonicalize_ap(aps) - dus = [] - us = [] - for ap in aps - sys, (du, u) = apply_transformation(ComplementarySensitivityTransform(ap), sys) - push!(dus, du) - push!(us, u) - end - linearization_function(system_modifier(sys), dus, us; kwargs...) +""" + $(TYPEDSIGNATURES) + +Return the sensitivity function for the analysis point(s) `aps`, and the modified system +simplified with the appropriate inputs and outputs. + +# Keyword Arguments + +$DOC_LOOP_OPENINGS +$DOC_SYS_MODIFIER + +All other keyword arguments are forwarded to `linearization_function`. +""" +function get_sensitivity_function(sys::AbstractSystem, aps; kwargs...) + get_linear_analysis_function(sys, SensitivityTransform, aps; kwargs...) end -function get_looptransfer_function( - sys, aps; system_modifier = identity, loop_openings = [], kwargs...) - sys = handle_loop_openings(sys, loop_openings) - aps = canonicalize_ap(aps) - dus = [] - us = [] - for ap in aps - sys, (du, u) = apply_transformation(LoopTransferTransform(ap), sys) - push!(dus, du) - push!(us, u) - end - linearization_function(system_modifier(sys), dus, us; kwargs...) +""" + $(TYPEDSIGNATURES) + +Return the complementary sensitivity function for the analysis point(s) `aps`, and the +modified system simplified with the appropriate inputs and outputs. + +# Keyword Arguments + +$DOC_LOOP_OPENINGS +$DOC_SYS_MODIFIER + +All other keyword arguments are forwarded to `linearization_function`. +""" +function get_comp_sensitivity_function(sys::AbstractSystem, aps; kwargs...) + get_linear_analysis_function(sys, ComplementarySensitivityTransform, aps; kwargs...) +end + +""" + $(TYPEDSIGNATURES) + +Return the loop-transfer function for the analysis point(s) `aps`, and the modified +system simplified with the appropriate inputs and outputs. + +# Keyword Arguments + +$DOC_LOOP_OPENINGS +$DOC_SYS_MODIFIER + +All other keyword arguments are forwarded to `linearization_function`. +""" +function get_looptransfer_function(sys::AbstractSystem, aps; kwargs...) + get_linear_analysis_function(sys, LoopTransferTransform, aps; kwargs...) end for f in [:get_sensitivity, :get_comp_sensitivity, :get_looptransfer] + utility_fun = Symbol(f, :_function) @eval function $f( sys, ap, args...; loop_openings = [], system_modifier = identity, kwargs...) - lin_fun, ssys = $(Symbol(f, :_function))( + lin_fun, ssys = $(utility_fun)( sys, ap, args...; loop_openings, system_modifier, kwargs...) ModelingToolkit.linearize(ssys, lin_fun; kwargs...), ssys end end -function open_loop(sys, ap::Union{Symbol, AnalysisPoint}; kwargs...) +""" + $(TYPEDSIGNATURES) + +Apply `LoopTransferTransform` to the analysis point `ap` and return the +result of `apply_transformation`. + +# Keyword Arguments + +- `system_modifier`: a function which takes the modified system and returns a new system + with any required further modifications peformed. +""" +function open_loop(sys, ap::Union{Symbol, AnalysisPoint}; system_modifier = identity) if ap isa Symbol ap = AnalysisPoint(ap) end tf = LoopTransferTransform(ap) - return apply_transformation(tf, sys) + sys, vars = apply_transformation(tf, sys) + return system_modifier(sys), vars end function linearization_function(sys::AbstractSystem, @@ -660,3 +865,46 @@ function linearization_function(sys::AbstractSystem, return linearization_function(system_modifier(sys), input_vars, output_vars; kwargs...) end + +@doc """ + get_sensitivity(sys, ap::AnalysisPoint; kwargs) + get_sensitivity(sys, ap_name::Symbol; kwargs) + +Compute the sensitivity function in analysis point `ap`. The sensitivity function is obtained by introducing an infinitesimal perturbation `d` at the input of `ap`, linearizing the system and computing the transfer function between `d` and the output of `ap`. + +# Arguments: + + - `kwargs`: Are sent to `ModelingToolkit.linearize` + +See also [`get_comp_sensitivity`](@ref), [`get_looptransfer`](@ref). +""" get_sensitivity + +@doc """ + get_comp_sensitivity(sys, ap::AnalysisPoint; kwargs) + get_comp_sensitivity(sys, ap_name::Symbol; kwargs) + +Compute the complementary sensitivity function in analysis point `ap`. The complementary sensitivity function is obtained by introducing an infinitesimal perturbation `d` at the output of `ap`, linearizing the system and computing the transfer function between `d` and the input of `ap`. + +# Arguments: + + - `kwargs`: Are sent to `ModelingToolkit.linearize` + +See also [`get_sensitivity`](@ref), [`get_looptransfer`](@ref). +""" get_comp_sensitivity + +@doc """ + get_looptransfer(sys, ap::AnalysisPoint; kwargs) + get_looptransfer(sys, ap_name::Symbol; kwargs) + +Compute the (linearized) loop-transfer function in analysis point `ap`, from `ap.out` to `ap.in`. + +!!! info "Negative feedback" + + Feedback loops often use negative feedback, and the computed loop-transfer function will in this case have the negative feedback included. Standard analysis tools often assume a loop-transfer function without the negative gain built in, and the result of this function may thus need negation before use. + +# Arguments: + + - `kwargs`: Are sent to `ModelingToolkit.linearize` + +See also [`get_sensitivity`](@ref), [`get_comp_sensitivity`](@ref), [`open_loop`](@ref). +""" get_looptransfer From ddceb6b6127c89e78308d04275c0e652b7bc87b3 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 3 Jan 2025 17:29:22 +0530 Subject: [PATCH 0532/2176] refactor: use version of Stdlib which disables old `AnalysisPoint` --- Project.toml | 1 + test/analysis_points.jl | 15 ++------------- test/downstream/Project.toml | 3 +++ test/downstream/analysis_points.jl | 3 +-- 4 files changed, 7 insertions(+), 15 deletions(-) diff --git a/Project.toml b/Project.toml index e4eba3cfad..256b1de6fc 100644 --- a/Project.toml +++ b/Project.toml @@ -117,6 +117,7 @@ Latexify = "0.11, 0.12, 0.13, 0.14, 0.15, 0.16" Libdl = "1" LinearAlgebra = "1" MLStyle = "0.4.17" +ModelingToolkitStandardLibrary = "2.19" NaNMath = "0.3, 1" NonlinearSolve = "4.3" OffsetArrays = "1" diff --git a/test/analysis_points.jl b/test/analysis_points.jl index c888261db2..9361b5a686 100644 --- a/test/analysis_points.jl +++ b/test/analysis_points.jl @@ -1,8 +1,7 @@ using ModelingToolkit, ModelingToolkitStandardLibrary.Blocks using OrdinaryDiffEq, LinearAlgebra using Test -using ModelingToolkit: t_nounits as t, D_nounits as D, AnalysisPoint, get_sensitivity, - get_comp_sensitivity, get_looptransfer, open_loop, AbstractSystem +using ModelingToolkit: t_nounits as t, D_nounits as D, AnalysisPoint, AbstractSystem using Symbolics: NAMESPACE_SEPARATOR @testset "AnalysisPoint is lowered to `connect`" begin @@ -164,17 +163,7 @@ end (inputap, [nameof(outputap)]), (nameof(inputap), [nameof(outputap)]) ] - if input isa Symbol - # broken because MTKStdlib defines the method for - # `input::Union{Symbol, Vector{Symbol}}` which we can't directly call - @test_broken linearize(sys, input, output) - linfun, ssys = @invoke linearization_function(sys::AbstractSystem, - input::Union{Symbol, Vector{Symbol}, AnalysisPoint, Vector{AnalysisPoint}}, - output::Any) - matrices = linearize(ssys, linfun) - else - matrices, _ = linearize(sys, input, output) - end + matrices, _ = linearize(sys, input, output) # Result should be the same as feedpack(P, 1), i.e., the closed-loop transfer function from plant input to plant output @test matrices.A[] == -2 @test matrices.B[] * matrices.C[] == 1 # both positive diff --git a/test/downstream/Project.toml b/test/downstream/Project.toml index b8776e1e4f..ade09e797b 100644 --- a/test/downstream/Project.toml +++ b/test/downstream/Project.toml @@ -5,3 +5,6 @@ ModelingToolkit = "961ee093-0014-501f-94e3-6117800e7a78" ModelingToolkitStandardLibrary = "16a59e39-deab-5bd0-87e4-056b12336739" OrdinaryDiffEq = "1dea7af3-3e70-54e6-95c3-0bf5283fa5ed" SymbolicIndexingInterface = "2efcf032-c050-4f8e-a9bb-153293bab1f5" + +[compat] +ModelingToolkitStandardLibrary = "2.19" diff --git a/test/downstream/analysis_points.jl b/test/downstream/analysis_points.jl index 85d271a547..fabca0f493 100644 --- a/test/downstream/analysis_points.jl +++ b/test/downstream/analysis_points.jl @@ -1,8 +1,7 @@ using ModelingToolkit, OrdinaryDiffEq, LinearAlgebra, ControlSystemsBase using ModelingToolkitStandardLibrary.Mechanical.Rotational using ModelingToolkitStandardLibrary.Blocks -using ModelingToolkit: connect, AnalysisPoint, t_nounits as t, D_nounits as D, - get_sensitivity, get_comp_sensitivity, get_looptransfer, open_loop +using ModelingToolkit: connect, t_nounits as t, D_nounits as D import ControlSystemsBase as CS @testset "Complicated model" begin From de17b878e35ca7fb05c5af2a262f1c9ef799c51e Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 6 Jan 2025 12:31:29 +0530 Subject: [PATCH 0533/2176] feat: validate causality of analysis points --- src/systems/analysis_points.jl | 54 ++++++++++++++++++++++++++++------ test/analysis_points.jl | 10 +++++++ 2 files changed, 55 insertions(+), 9 deletions(-) diff --git a/src/systems/analysis_points.jl b/src/systems/analysis_points.jl index 9ff06109e5..a2b7fb7ea8 100644 --- a/src/systems/analysis_points.jl +++ b/src/systems/analysis_points.jl @@ -72,6 +72,36 @@ struct AnalysisPoint these are all `RealInput` connectors. """ outputs::Union{Nothing, Vector{Any}} + + function AnalysisPoint(input, name::Symbol, outputs; verbose = true) + # input to analysis point should be an output variable + if verbose && input !== nothing + var = ap_var(input) + isoutput(var) || ap_warning(1, name, true) + end + # outputs of analysis points should be input variables + if verbose && outputs !== nothing + for (i, output) in enumerate(outputs) + var = ap_var(output) + isinput(var) || ap_warning(2 + i, name, false) + end + end + + return new(input, name, outputs) + end +end + +function ap_warning(arg::Int, name::Symbol, should_be_output) + causality = should_be_output ? "output" : "input" + @warn """ + The $(arg)-th argument to analysis point $(name) was not a $causality. This is supported in \ + order to handle inverse models, but may not be what you intended. + + If you are building a forward mode (causal), you may want to swap this argument with \ + one on the opposite side of the name of the analysis point provided to `connect`. \ + Learn more about the causality of analysis points in the docstring for `AnalysisPoint`. \ + Silence this message using `connect(out, :name, in...; warn = false)`. + """ end AnalysisPoint() = AnalysisPoint(nothing, Symbol(), nothing) @@ -133,8 +163,8 @@ function renamespace(sys, ap::AnalysisPoint) end # create analysis points via `connect` -function Symbolics.connect(in, ap::AnalysisPoint, outs...) - return AnalysisPoint() ~ AnalysisPoint(in, ap.name, collect(outs)) +function Symbolics.connect(in, ap::AnalysisPoint, outs...; verbose = true) + return AnalysisPoint() ~ AnalysisPoint(in, ap.name, collect(outs); verbose) end """ @@ -161,15 +191,21 @@ connect(P.input, :plant_input, C.output) typically is not (unless the model is an inverse model). -# Arguments: +# Arguments + +- `output_connector`: An output connector +- `input_connector`: An input connector +- `ap`: An explicitly created [`AnalysisPoint`](@ref) +- `ap_name`: If a name is given, an [`AnalysisPoint`](@ref) with the given name will be + created automatically. + +# Keyword arguments - - `output_connector`: An output connector - - `input_connector`: An input connector - - `ap`: An explicitly created [`AnalysisPoint`](@ref) - - `ap_name`: If a name is given, an [`AnalysisPoint`](@ref) with the given name will be created automatically. +- `verbose`: Warn if an input is connected to an output (reverse causality). Silence this + warning if you are analyzing an inverse model. """ -function Symbolics.connect(in::AbstractSystem, name::Symbol, out, outs...) - return AnalysisPoint() ~ AnalysisPoint(in, name, [out; collect(outs)]) +function Symbolics.connect(in::AbstractSystem, name::Symbol, out, outs...; verbose = true) + return AnalysisPoint() ~ AnalysisPoint(in, name, [out; collect(outs)]; verbose) end """ diff --git a/test/analysis_points.jl b/test/analysis_points.jl index 9361b5a686..8c93e919b9 100644 --- a/test/analysis_points.jl +++ b/test/analysis_points.jl @@ -24,6 +24,16 @@ using Symbolics: NAMESPACE_SEPARATOR @test isequal(sys_ap2, sys_normal2) end +@testset "Inverse causality throws a warning" begin + @named P = FirstOrder(k = 1, T = 1) + @named C = Gain(; k = -1) + + ap = AnalysisPoint(:plant_input) + @test_warn ["1-th argument", "plant_input", "not a output"] connect( + P.input, ap, C.output) + @test_nowarn connect(P.input, ap, C.output; verbose = false) +end + # also tests `connect(input, name::Symbol, outputs...)` syntax @testset "AnalysisPoint is accessible via `getproperty`" begin @named P = FirstOrder(k = 1, T = 1) From 50973353bb7da69dc35d838f90177f5ae1b461d1 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 6 Jan 2025 14:06:29 +0530 Subject: [PATCH 0534/2176] fix: fix `isvarkind` for `Symbolics.Arr` --- src/variables.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/variables.jl b/src/variables.jl index 3b0d64e7ea..c45c4b0f00 100644 --- a/src/variables.jl +++ b/src/variables.jl @@ -90,7 +90,7 @@ struct Equality <: AbstractConnectType end # Equality connection struct Flow <: AbstractConnectType end # sum to 0 struct Stream <: AbstractConnectType end # special stream connector -isvarkind(m, x::Num) = isvarkind(m, value(x)) +isvarkind(m, x::Union{Num, Symbolics.Arr}) = isvarkind(m, value(x)) function isvarkind(m, x) iskind = getmetadata(x, m, nothing) iskind !== nothing && return iskind From 2b1f525cb036dadec10bb8c34503e480240e4d5c Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 6 Jan 2025 14:55:13 +0530 Subject: [PATCH 0535/2176] fix: remove ambiguities in namespacing of analysis points --- src/systems/analysis_points.jl | 34 ++++++++--------- test/analysis_points.jl | 59 ++++++++++++------------------ test/downstream/analysis_points.jl | 16 ++++---- 3 files changed, 48 insertions(+), 61 deletions(-) diff --git a/src/systems/analysis_points.jl b/src/systems/analysis_points.jl index a2b7fb7ea8..87f278d4dc 100644 --- a/src/systems/analysis_points.jl +++ b/src/systems/analysis_points.jl @@ -314,9 +314,10 @@ function modify_nested_subsystem( return fn(root) end # ignore the name of the root - if nameof(root) == hierarchy[1] - hierarchy = @view hierarchy[2:end] + if nameof(root) != hierarchy[1] + error("The name of the root system $(nameof(root)) must be included in the name passed to `modify_nested_subsystem`") end + hierarchy = @view hierarchy[2:end] # recursive helper function which does the searching and modification function _helper(sys::AbstractSystem, i::Int) @@ -722,13 +723,14 @@ end A utility function to get the "canonical" form of a list of analysis points. Always returns a list of values. Any value that cannot be turned into an `AnalysisPoint` (i.e. isn't -already an `AnalysisPoint` or `Symbol`) is simply wrapped in an array. +already an `AnalysisPoint` or `Symbol`) is simply wrapped in an array. `Symbol` names of +`AnalysisPoint`s are namespaced with `sys`. """ -canonicalize_ap(ap::Symbol) = [AnalysisPoint(ap)] -canonicalize_ap(ap::AnalysisPoint) = [ap] -canonicalize_ap(ap) = [ap] -function canonicalize_ap(aps::Vector) - mapreduce(canonicalize_ap, vcat, aps; init = []) +canonicalize_ap(sys::AbstractSystem, ap::Symbol) = [AnalysisPoint(renamespace(sys, ap))] +canonicalize_ap(sys::AbstractSystem, ap::AnalysisPoint) = [ap] +canonicalize_ap(sys::AbstractSystem, ap) = [ap] +function canonicalize_ap(sys::AbstractSystem, aps::Vector) + mapreduce(Base.Fix1(canonicalize_ap, sys), vcat, aps; init = []) end """ @@ -737,7 +739,7 @@ end Given a list of analysis points, break the connection for each and set the output to zero. """ function handle_loop_openings(sys::AbstractSystem, aps) - for ap in canonicalize_ap(aps) + for ap in canonicalize_ap(sys, aps) sys, (outvar,) = apply_transformation(Break(ap, true), sys) if Symbolics.isarraysymbolic(outvar) push!(get_eqs(sys), outvar ~ zeros(size(outvar))) @@ -776,7 +778,7 @@ All other keyword arguments are forwarded to `linearization_function`. function get_linear_analysis_function( sys::AbstractSystem, transform, aps; system_modifier = identity, loop_openings = [], kwargs...) sys = handle_loop_openings(sys, loop_openings) - aps = canonicalize_ap(aps) + aps = canonicalize_ap(sys, aps) dus = [] us = [] for ap in aps @@ -860,9 +862,7 @@ result of `apply_transformation`. with any required further modifications peformed. """ function open_loop(sys, ap::Union{Symbol, AnalysisPoint}; system_modifier = identity) - if ap isa Symbol - ap = AnalysisPoint(ap) - end + ap = only(canonicalize_ap(sys, ap)) tf = LoopTransferTransform(ap) sys, vars = apply_transformation(tf, sys) return system_modifier(sys), vars @@ -871,9 +871,9 @@ end function linearization_function(sys::AbstractSystem, inputs::Union{Symbol, Vector{Symbol}, AnalysisPoint, Vector{AnalysisPoint}}, outputs; loop_openings = [], system_modifier = identity, kwargs...) - loop_openings = Set(map(nameof, canonicalize_ap(loop_openings))) - inputs = canonicalize_ap(inputs) - outputs = canonicalize_ap(outputs) + loop_openings = Set(map(nameof, canonicalize_ap(sys, loop_openings))) + inputs = canonicalize_ap(sys, inputs) + outputs = canonicalize_ap(sys, outputs) input_vars = [] for input in inputs @@ -897,7 +897,7 @@ function linearization_function(sys::AbstractSystem, push!(output_vars, output_var) end - sys = handle_loop_openings(sys, collect(loop_openings)) + sys = handle_loop_openings(sys, map(AnalysisPoint, collect(loop_openings))) return linearization_function(system_modifier(sys), input_vars, output_vars; kwargs...) end diff --git a/test/analysis_points.jl b/test/analysis_points.jl index 8c93e919b9..9e8e3fb455 100644 --- a/test/analysis_points.jl +++ b/test/analysis_points.jl @@ -2,6 +2,7 @@ using ModelingToolkit, ModelingToolkitStandardLibrary.Blocks using OrdinaryDiffEq, LinearAlgebra using Test using ModelingToolkit: t_nounits as t, D_nounits as D, AnalysisPoint, AbstractSystem +import ModelingToolkit as MTK using Symbolics: NAMESPACE_SEPARATOR @testset "AnalysisPoint is lowered to `connect`" begin @@ -69,26 +70,21 @@ sys = ODESystem(eqs, t, systems = [P, C], name = :hej) @test norm(sol.u[end]) < 1e-6 # This fails without the feedback through C end -@testset "get_sensitivity - $name" for (name, sys, ap) in [ +test_cases = [ ("inner", sys, sys.plant_input), ("nested", nested_sys, nested_sys.hej.plant_input), - ("inner - nonamespace", sys, :plant_input), - ("inner - Symbol", sys, nameof(sys.plant_input)), - ("nested - Symbol", nested_sys, nameof(nested_sys.hej.plant_input)) + ("inner - Symbol", sys, :plant_input), + ("nested - Symbol", nested_sys, nameof(sys.plant_input)) ] + +@testset "get_sensitivity - $name" for (name, sys, ap) in test_cases matrices, _ = get_sensitivity(sys, ap) @test matrices.A[] == -2 @test matrices.B[] * matrices.C[] == -1 # either one negative @test matrices.D[] == 1 end -@testset "get_comp_sensitivity - $name" for (name, sys, ap) in [ - ("inner", sys, sys.plant_input), - ("nested", nested_sys, nested_sys.hej.plant_input), - ("inner - nonamespace", sys, :plant_input), - ("inner - Symbol", sys, nameof(sys.plant_input)), - ("nested - Symbol", nested_sys, nameof(nested_sys.hej.plant_input)) -] +@testset "get_comp_sensitivity - $name" for (name, sys, ap) in test_cases matrices, _ = get_comp_sensitivity(sys, ap) @test matrices.A[] == -2 @test matrices.B[] * matrices.C[] == 1 # both positive or negative @@ -104,13 +100,7 @@ S = sensitivity(P, C) # or feedback(1, P*C) T = comp_sensitivity(P, C) # or feedback(P*C) =# -@testset "get_looptransfer - $name" for (name, sys, ap) in [ - ("inner", sys, sys.plant_input), - ("nested", nested_sys, nested_sys.hej.plant_input), - ("inner - nonamespace", sys, :plant_input), - ("inner - Symbol", sys, nameof(sys.plant_input)), - ("nested - Symbol", nested_sys, nameof(nested_sys.hej.plant_input)) -] +@testset "get_looptransfer - $name" for (name, sys, ap) in test_cases matrices, _ = get_looptransfer(sys, ap) @test matrices.A[] == -1 @test matrices.B[] * matrices.C[] == -1 # either one negative @@ -125,13 +115,7 @@ C = -1 L = P*C =# -@testset "open_loop - $name" for (name, sys, ap) in [ - ("inner", sys, sys.plant_input), - ("nested", nested_sys, nested_sys.hej.plant_input), - ("inner - nonamespace", sys, :plant_input), - ("inner - Symbol", sys, nameof(sys.plant_input)), - ("nested - Symbol", nested_sys, nameof(nested_sys.hej.plant_input)) -] +@testset "open_loop - $name" for (name, sys, ap) in test_cases open_sys, (du, u) = open_loop(sys, ap) matrices, _ = linearize(open_sys, [du], [u]) @test matrices.A[] == -1 @@ -146,13 +130,14 @@ eqs = [connect(P.output, :plant_output, C.input) sys = ODESystem(eqs, t, systems = [P, C], name = :hej) @named nested_sys = ODESystem(Equation[], t; systems = [sys]) -@testset "get_sensitivity - $name" for (name, sys, ap) in [ +test_cases = [ ("inner", sys, sys.plant_input), ("nested", nested_sys, nested_sys.hej.plant_input), - ("inner - nonamespace", sys, :plant_input), - ("inner - Symbol", sys, nameof(sys.plant_input)), - ("nested - Symbol", nested_sys, nameof(nested_sys.hej.plant_input)) + ("inner - Symbol", sys, :plant_input), + ("nested - Symbol", nested_sys, nameof(sys.plant_input)) ] + +@testset "get_sensitivity - $name" for (name, sys, ap) in test_cases matrices, _ = get_sensitivity(sys, ap) @test matrices.A[] == -2 @test matrices.B[] * matrices.C[] == -1 # either one negative @@ -163,15 +148,19 @@ end ("inner", sys, sys.plant_input, sys.plant_output), ("nested", nested_sys, nested_sys.hej.plant_input, nested_sys.hej.plant_output) ] + inputname = Symbol(join( + MTK.namespace_hierarchy(nameof(inputap))[2:end], NAMESPACE_SEPARATOR)) + outputname = Symbol(join( + MTK.namespace_hierarchy(nameof(outputap))[2:end], NAMESPACE_SEPARATOR)) @testset "input - $(typeof(input)), output - $(typeof(output))" for (input, output) in [ (inputap, outputap), - (nameof(inputap), outputap), - (inputap, nameof(outputap)), - (nameof(inputap), nameof(outputap)), + (inputname, outputap), + (inputap, outputname), + (inputname, outputname), (inputap, [outputap]), - (nameof(inputap), [outputap]), - (inputap, [nameof(outputap)]), - (nameof(inputap), [nameof(outputap)]) + (inputname, [outputap]), + (inputap, [outputname]), + (inputname, [outputname]) ] matrices, _ = linearize(sys, input, output) # Result should be the same as feedpack(P, 1), i.e., the closed-loop transfer function from plant input to plant output diff --git a/test/downstream/analysis_points.jl b/test/downstream/analysis_points.jl index fabca0f493..7a433cb504 100644 --- a/test/downstream/analysis_points.jl +++ b/test/downstream/analysis_points.jl @@ -64,7 +64,7 @@ import ControlSystemsBase as CS prob = ODEProblem(sys, unknowns(sys) .=> 0.0, (0.0, 4.0)) sol = solve(prob, Rodas5P(), reltol = 1e-6, abstol = 1e-9) - matrices, ssys = linearize(closed_loop, AnalysisPoint(:r), AnalysisPoint(:y)) + matrices, ssys = linearize(closed_loop, :r, :y) lsys = ss(matrices...) |> sminreal @test lsys.nx == 8 @@ -129,13 +129,11 @@ end t, systems = [P_inner, feedback, ref]) - P_not_broken, _ = linearize(sys_inner, AnalysisPoint(:u), AnalysisPoint(:y)) + P_not_broken, _ = linearize(sys_inner, :u, :y) @test P_not_broken.A[] == -2 - P_broken, _ = linearize(sys_inner, AnalysisPoint(:u), AnalysisPoint(:y), - loop_openings = [AnalysisPoint(:u)]) + P_broken, _ = linearize(sys_inner, :u, :y, loop_openings = [:u]) @test P_broken.A[] == -1 - P_broken, _ = linearize(sys_inner, AnalysisPoint(:u), AnalysisPoint(:y), - loop_openings = [AnalysisPoint(:y)]) + P_broken, _ = linearize(sys_inner, :u, :y, loop_openings = [:y]) @test P_broken.A[] == -1 Sinner = sminreal(ss(get_sensitivity(sys_inner, :u)[1]...)) @@ -154,10 +152,10 @@ end t, systems = [P_outer, sys_inner]) - Souter = sminreal(ss(get_sensitivity(sys_outer, sys_inner.u)[1]...)) + Souter = sminreal(ss(get_sensitivity(sys_outer, sys_outer.sys_inner.u)[1]...)) Sinner2 = sminreal(ss(get_sensitivity( - sys_outer, sys_inner.u, loop_openings = [:y2])[1]...)) + sys_outer, sys_outer.sys_inner.u, loop_openings = [:y2])[1]...)) @test Sinner.nx == 1 @test Sinner == Sinner2 @@ -268,7 +266,7 @@ end @test tf(L[2, 1]) ≈ tf(Ps) matrices, _ = linearize( - sys_outer, [sys_outer.inner.plant_input], [sys_inner.plant_output]) + sys_outer, [sys_outer.inner.plant_input], [nameof(sys_inner.plant_output)]) G = CS.ss(matrices...) |> sminreal @test tf(G) ≈ tf(CS.feedback(Ps, Cs)) end From c93df8fb418af2db96ca215028657ae8f9f73e5f Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 6 Jan 2025 15:13:50 +0530 Subject: [PATCH 0536/2176] refactor: do not export individual analysis point transformations --- src/ModelingToolkit.jl | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/ModelingToolkit.jl b/src/ModelingToolkit.jl index 29288a5742..7462122998 100644 --- a/src/ModelingToolkit.jl +++ b/src/ModelingToolkit.jl @@ -293,8 +293,7 @@ export MTKParameters, reorder_dimension_by_tunables!, reorder_dimension_by_tunab export HomotopyContinuationProblem -export AnalysisPoint, Break, PerturbOutput, SampleInput, SensitivityTransform, - ComplementarySensitivityTransform, LoopTransferTransform, apply_transformation, - get_sensitivity_function, get_comp_sensitivity_function, get_looptransfer_function, - get_sensitivity, get_comp_sensitivity, get_looptransfer, open_loop +export AnalysisPoint, get_sensitivity_function, get_comp_sensitivity_function, + get_looptransfer_function, get_sensitivity, get_comp_sensitivity, get_looptransfer, + open_loop end # module From a7650737cbcfdd99def579c193b59c4dedec04d1 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 6 Jan 2025 15:14:22 +0530 Subject: [PATCH 0537/2176] docs: port over linear analysis tutorial from MTKStdlib --- docs/Project.toml | 3 + docs/pages.jl | 3 +- docs/src/tutorials/linear_analysis.md | 152 ++++++++++++++++++++++++++ 3 files changed, 157 insertions(+), 1 deletion(-) create mode 100644 docs/src/tutorials/linear_analysis.md diff --git a/docs/Project.toml b/docs/Project.toml index a358455503..be80250603 100644 --- a/docs/Project.toml +++ b/docs/Project.toml @@ -1,6 +1,7 @@ [deps] BenchmarkTools = "6e4b80f9-dd63-53aa-95a3-0cdb28fa8baf" BifurcationKit = "0f109fa4-8a5d-4b75-95aa-f515264e7665" +ControlSystemsBase = "aaaaaaaa-a6ca-5380-bf3e-84a91bcd477e" DataInterpolations = "82cc6244-b520-54b8-b5a6-8a565e85f1d0" DifferentialEquations = "0c46a032-eb83-5123-abaf-570d42b7fbaa" Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f" @@ -8,6 +9,7 @@ Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4" DynamicQuantities = "06fc5a27-2a28-4c7c-a15d-362465fb6821" LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" ModelingToolkit = "961ee093-0014-501f-94e3-6117800e7a78" +ModelingToolkitStandardLibrary = "16a59e39-deab-5bd0-87e4-056b12336739" NonlinearSolve = "8913a72c-1f9b-4ce2-8d82-65094dcecaec" Optim = "429524aa-4258-5aef-a3af-852621145aeb" Optimization = "7f7a1694-90dd-40f0-9382-eb1efda571ba" @@ -31,6 +33,7 @@ Distributions = "0.25" Documenter = "1" DynamicQuantities = "^0.11.2, 0.12, 1" ModelingToolkit = "8.33, 9" +ModelingToolkitStandardLibrary = "2.19" NonlinearSolve = "3, 4" Optim = "1.7" Optimization = "3.9, 4" diff --git a/docs/pages.jl b/docs/pages.jl index 2af487adf8..8a95de4ac5 100644 --- a/docs/pages.jl +++ b/docs/pages.jl @@ -13,7 +13,8 @@ pages = [ "tutorials/bifurcation_diagram_computation.md", "tutorials/SampledData.md", "tutorials/domain_connections.md", - "tutorials/callable_params.md"], + "tutorials/callable_params.md", + "tutorials/linear_analysis.md"], "Examples" => Any[ "Basic Examples" => Any["examples/higher_order.md", "examples/spring_mass.md", diff --git a/docs/src/tutorials/linear_analysis.md b/docs/src/tutorials/linear_analysis.md new file mode 100644 index 0000000000..7fcb67f9fb --- /dev/null +++ b/docs/src/tutorials/linear_analysis.md @@ -0,0 +1,152 @@ +# Linear Analysis + +Linear analysis refers to the process of linearizing a nonlinear model and analysing the resulting linear dynamical system. To facilitate linear analysis, ModelingToolkit provides the concept of an [`AnalysisPoint`](@ref), which can be inserted in-between two causal blocks (such as those from `ModelingToolkitStandardLibrary.Blocks` sub module). Once a model containing analysis points is built, several operations are available: + + - [`get_sensitivity`](@ref) get the [sensitivity function (wiki)](https://en.wikipedia.org/wiki/Sensitivity_(control_systems)), $S(s)$, as defined in the field of control theory. + - [`get_comp_sensitivity`](@ref) get the complementary sensitivity function $T(s) : S(s)+T(s)=1$. + - [`get_looptransfer`](@ref) get the (open) loop-transfer function where the loop starts and ends in the analysis point. For a typical simple feedback connection with a plant $P(s)$ and a controller $C(s)$, the loop-transfer function at the plant output is $P(s)C(s)$. + - [`linearize`](@ref) can be called with two analysis points denoting the input and output of the linearized system. + - [`open_loop`](@ref) return a new (nonlinear) system where the loop has been broken in the analysis point, i.e., the connection the analysis point usually implies has been removed. + +An analysis point can be created explicitly using the constructor [`AnalysisPoint`](@ref), or automatically when connecting two causal components using `connect`: + +```julia +connect(comp1.output, :analysis_point_name, comp2.input) +``` + +A single output can also be connected to multiple inputs: + +```julia +connect(comp1.output, :analysis_point_name, comp2.input, comp3.input, comp4.input) +``` + +!!! warning "Causality" + + Analysis points are *causal*, i.e., they imply a directionality for the flow of information. The order of the connections in the connect statement is thus important, i.e., `connect(out, :name, in)` is different from `connect(in, :name, out)`. + +The directionality of an analysis point can be thought of as an arrow in a block diagram, where the name of the analysis point applies to the arrow itself. + +``` +┌─────┐ ┌─────┐ +│ │ name │ │ +│ out├────────►│in │ +│ │ │ │ +└─────┘ └─────┘ +``` + +This is signified by the name being the middle argument to `connect`. + +Of the above mentioned functions, all except for [`open_loop`](@ref) return the output of [`ModelingToolkit.linearize`](@ref), which is + +```julia +matrices, simplified_sys = linearize(...) +# matrices = (; A, B, C, D) +``` + +i.e., `matrices` is a named tuple containing the matrices of a linear state-space system on the form + +```math +\begin{aligned} +\dot x &= Ax + Bu\\ +y &= Cx + Du +\end{aligned} +``` + +## Example + +The following example builds a simple closed-loop system with a plant $P$ and a controller $C$. Two analysis points are inserted, one before and one after $P$. We then derive a number of sensitivity functions and show the corresponding code using the package ControlSystemBase.jl + +```@example LINEAR_ANALYSIS +using ModelingToolkitStandardLibrary.Blocks, ModelingToolkit +@named P = FirstOrder(k = 1, T = 1) # A first-order system with pole in -1 +@named C = Gain(-1) # A P controller +t = ModelingToolkit.get_iv(P) +eqs = [connect(P.output, :plant_output, C.input) # Connect with an automatically created analysis point called :plant_output + connect(C.output, :plant_input, P.input)] +sys = ODESystem(eqs, t, systems = [P, C], name = :feedback_system) + +matrices_S = get_sensitivity(sys, :plant_input)[1] # Compute the matrices of a state-space representation of the (input)sensitivity function. +matrices_T = get_comp_sensitivity(sys, :plant_input)[1] +``` + +Continued linear analysis and design can be performed using ControlSystemsBase.jl. +We create `ControlSystemsBase.StateSpace` objects using + +```@example LINEAR_ANALYSIS +using ControlSystemsBase, Plots +S = ss(matrices_S...) +T = ss(matrices_T...) +bodeplot([S, T], lab = ["S" "" "T" ""], plot_title = "Bode plot of sensitivity functions", + margin = 5Plots.mm) +``` + +The sensitivity functions obtained this way should be equivalent to the ones obtained with the code below + +```@example LINEAR_ANALYSIS_CS +using ControlSystemsBase +P = tf(1.0, [1, 1]) |> ss +C = 1 # Negative feedback assumed in ControlSystems +S = sensitivity(P, C) # or feedback(1, P*C) +T = comp_sensitivity(P, C) # or feedback(P*C) +``` + +We may also derive the loop-transfer function $L(s) = P(s)C(s)$ using + +```@example LINEAR_ANALYSIS +matrices_L = get_looptransfer(sys, :plant_output)[1] +L = ss(matrices_L...) +``` + +which is equivalent to the following with ControlSystems + +```@example LINEAR_ANALYSIS_CS +L = P * (-C) # Add the minus sign to build the negative feedback into the controller +``` + +To obtain the transfer function between two analysis points, we call `linearize` + +```@example LINEAR_ANALYSIS +using ModelingToolkit # hide +matrices_PS = linearize(sys, :plant_input, :plant_output)[1] +``` + +this particular transfer function should be equivalent to the linear system `P(s)S(s)`, i.e., equivalent to + +```@example LINEAR_ANALYSIS_CS +feedback(P, C) +``` + +### Obtaining transfer functions + +A statespace system from [ControlSystemsBase](https://juliacontrol.github.io/ControlSystems.jl/stable/man/creating_systems/) can be converted to a transfer function using the function `tf`: + +```@example LINEAR_ANALYSIS_CS +tf(S) +``` + +## Gain and phase margins + +Further linear analysis can be performed using the [analysis methods from ControlSystemsBase](https://juliacontrol.github.io/ControlSystems.jl/stable/lib/analysis/). For example, calculating the gain and phase margins of a system can be done using + +```@example LINEAR_ANALYSIS_CS +margin(P) +``` + +(they are infinite for this system). A Nyquist plot can be produced using + +```@example LINEAR_ANALYSIS_CS +nyquistplot(P) +``` + +## Index + +```@index +Pages = ["linear_analysis.md"] +``` + +```@autodocs +Modules = [ModelingToolkit] +Pages = ["systems/analysis_points.jl"] +Order = [:function, :type] +Private = false +``` From 520daf49d589b49c05c172d8a29e353170bf6c05 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 6 Jan 2025 18:08:13 +0530 Subject: [PATCH 0538/2176] docs: remove DifferentialEquations.jl from docs environment --- docs/Project.toml | 2 -- docs/src/basics/Composition.md | 2 +- docs/src/examples/perturbation.md | 2 +- docs/src/examples/sparse_jacobians.md | 2 +- docs/src/examples/spring_mass.md | 2 +- docs/src/tutorials/acausal_components.md | 2 +- docs/src/tutorials/modelingtoolkitize.md | 4 ++-- docs/src/tutorials/ode_modeling.md | 6 +++--- docs/src/tutorials/programmatically_generating.md | 2 +- 9 files changed, 11 insertions(+), 13 deletions(-) diff --git a/docs/Project.toml b/docs/Project.toml index be80250603..40fcb16581 100644 --- a/docs/Project.toml +++ b/docs/Project.toml @@ -3,7 +3,6 @@ BenchmarkTools = "6e4b80f9-dd63-53aa-95a3-0cdb28fa8baf" BifurcationKit = "0f109fa4-8a5d-4b75-95aa-f515264e7665" ControlSystemsBase = "aaaaaaaa-a6ca-5380-bf3e-84a91bcd477e" DataInterpolations = "82cc6244-b520-54b8-b5a6-8a565e85f1d0" -DifferentialEquations = "0c46a032-eb83-5123-abaf-570d42b7fbaa" Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f" Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4" DynamicQuantities = "06fc5a27-2a28-4c7c-a15d-362465fb6821" @@ -28,7 +27,6 @@ Unitful = "1986cc42-f94f-5a68-af5c-568840ba703d" BenchmarkTools = "1.3" BifurcationKit = "0.4" DataInterpolations = "6.5" -DifferentialEquations = "7.6" Distributions = "0.25" Documenter = "1" DynamicQuantities = "^0.11.2, 0.12, 1" diff --git a/docs/src/basics/Composition.md b/docs/src/basics/Composition.md index 78dec8445b..2e5d4be831 100644 --- a/docs/src/basics/Composition.md +++ b/docs/src/basics/Composition.md @@ -56,7 +56,7 @@ x0 = [decay1.x => 1.0 p = [decay1.a => 0.1 decay2.a => 0.2] -using DifferentialEquations +using OrdinaryDiffEq prob = ODEProblem(simplified_sys, x0, (0.0, 100.0), p) sol = solve(prob, Tsit5()) sol[decay2.f] diff --git a/docs/src/examples/perturbation.md b/docs/src/examples/perturbation.md index 407248709d..0d1a493cb4 100644 --- a/docs/src/examples/perturbation.md +++ b/docs/src/examples/perturbation.md @@ -49,7 +49,7 @@ These are the ODEs we want to solve. Now construct an `ODESystem`, which automat To solve the `ODESystem`, we generate an `ODEProblem` with initial conditions $x(0) = 0$, and $ẋ(0) = 1$, and solve it: ```@example perturbation -using DifferentialEquations +using OrdinaryDiffEq u0 = Dict([unknowns(sys) .=> 0.0; D(y[0]) => 1.0]) # nonzero initial velocity prob = ODEProblem(sys, u0, (0.0, 3.0)) sol = solve(prob) diff --git a/docs/src/examples/sparse_jacobians.md b/docs/src/examples/sparse_jacobians.md index 1ed3b1733c..03bd80d432 100644 --- a/docs/src/examples/sparse_jacobians.md +++ b/docs/src/examples/sparse_jacobians.md @@ -11,7 +11,7 @@ First, let's start out with an implementation of the 2-dimensional Brusselator partial differential equation discretized using finite differences: ```@example sparsejac -using DifferentialEquations, ModelingToolkit +using OrdinaryDiffEq, ModelingToolkit const N = 32 const xyd_brusselator = range(0, stop = 1, length = N) diff --git a/docs/src/examples/spring_mass.md b/docs/src/examples/spring_mass.md index e733b11724..355e5c20b2 100644 --- a/docs/src/examples/spring_mass.md +++ b/docs/src/examples/spring_mass.md @@ -5,7 +5,7 @@ In this tutorial, we will build a simple component-based model of a spring-mass ## Copy-Paste Example ```@example component -using ModelingToolkit, Plots, DifferentialEquations, LinearAlgebra +using ModelingToolkit, Plots, OrdinaryDiffEq, LinearAlgebra using ModelingToolkit: t_nounits as t, D_nounits as D using Symbolics: scalarize diff --git a/docs/src/tutorials/acausal_components.md b/docs/src/tutorials/acausal_components.md index c91ba29670..096b576d29 100644 --- a/docs/src/tutorials/acausal_components.md +++ b/docs/src/tutorials/acausal_components.md @@ -20,7 +20,7 @@ equalities before solving. Let's see this in action. ## Copy-Paste Example ```@example acausal -using ModelingToolkit, Plots, DifferentialEquations +using ModelingToolkit, Plots, OrdinaryDiffEq using ModelingToolkit: t_nounits as t, D_nounits as D @connector Pin begin diff --git a/docs/src/tutorials/modelingtoolkitize.md b/docs/src/tutorials/modelingtoolkitize.md index e272a2237c..545879f842 100644 --- a/docs/src/tutorials/modelingtoolkitize.md +++ b/docs/src/tutorials/modelingtoolkitize.md @@ -33,10 +33,10 @@ to improve a simulation code before it's passed to the solver. ## Example Usage: Generating an Analytical Jacobian Expression for an ODE Code Take, for example, the Robertson ODE -defined as an `ODEProblem` for DifferentialEquations.jl: +defined as an `ODEProblem` for OrdinaryDiffEq.jl: ```@example mtkize -using DifferentialEquations, ModelingToolkit +using OrdinaryDiffEq, ModelingToolkit function rober(du, u, p, t) y₁, y₂, y₃ = u k₁, k₂, k₃ = p diff --git a/docs/src/tutorials/ode_modeling.md b/docs/src/tutorials/ode_modeling.md index 755e70ef46..0a4cd80803 100644 --- a/docs/src/tutorials/ode_modeling.md +++ b/docs/src/tutorials/ode_modeling.md @@ -34,7 +34,7 @@ using ModelingToolkit: t_nounits as t, D_nounits as D end end -using DifferentialEquations: solve +using OrdinaryDiffEq @mtkbuild fol = FOL() prob = ODEProblem(fol, [], (0.0, 10.0), []) sol = solve(prob) @@ -84,10 +84,10 @@ Note that equations in MTK use the tilde character (`~`) as equality sign. `@mtkbuild` creates an instance of `FOL` named as `fol`. -After construction of the ODE, you can solve it using [DifferentialEquations.jl](https://docs.sciml.ai/DiffEqDocs/stable/): +After construction of the ODE, you can solve it using [OrdinaryDiffEq.jl](https://docs.sciml.ai/DiffEqDocs/stable/): ```@example ode2 -using DifferentialEquations +using OrdinaryDiffEq using Plots prob = ODEProblem(fol, [], (0.0, 10.0), []) diff --git a/docs/src/tutorials/programmatically_generating.md b/docs/src/tutorials/programmatically_generating.md index 76d12dba2a..9fc1db1834 100644 --- a/docs/src/tutorials/programmatically_generating.md +++ b/docs/src/tutorials/programmatically_generating.md @@ -49,7 +49,7 @@ eqs = [D(x) ~ (h - x) / τ] # create an array of equations # Note: Complete models cannot be subsystems of other models! fol = structural_simplify(model) prob = ODEProblem(fol, [], (0.0, 10.0), []) -using DifferentialEquations: solve +using OrdinaryDiffEq sol = solve(prob) using Plots From 53f32e29c519a8bc48d61fd3b80a733d99bac049 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 18 Nov 2024 16:28:03 +0530 Subject: [PATCH 0539/2176] fix: `@mtkmodel` no longer makes `@variables` callable --- src/systems/model_parsing.jl | 2 +- test/model_parsing.jl | 21 ++++++++++++++++++++- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/src/systems/model_parsing.jl b/src/systems/model_parsing.jl index 5402659b1c..52e46597bf 100644 --- a/src/systems/model_parsing.jl +++ b/src/systems/model_parsing.jl @@ -497,7 +497,7 @@ function generate_var!(dict, a, b, varclass, mod; vd isa Vector && (vd = first(vd)) vd[a] = Dict{Symbol, Any}() var = if indices === nothing - Symbolics.variable(a, T = SymbolicUtils.FnType{Tuple{Any}, type})(iv) + first(@variables $a(iv)::type) else vd[a][:size] = Tuple(lastindex.(indices)) first(@variables $a(iv)[indices...]::type) diff --git a/test/model_parsing.jl b/test/model_parsing.jl index 6ab7636b32..baaa4651bf 100644 --- a/test/model_parsing.jl +++ b/test/model_parsing.jl @@ -1,4 +1,4 @@ -using ModelingToolkit, Test +using ModelingToolkit, Symbolics, Test using ModelingToolkit: get_connector_type, get_defaults, get_gui_metadata, get_systems, get_ps, getdefault, getname, readable_code, scalarize, symtype, VariableDescription, RegularConnector, @@ -990,3 +990,22 @@ struct CustomStruct end @named sys = MyModel(p = CustomStruct()) @test ModelingToolkit.defaults(sys)[@nonamespace sys.p] == CustomStruct() end + +@testset "Variables are not callable symbolics" begin + @mtkmodel Example begin + @variables begin + x(t) + y(t) + end + @equations begin + x ~ y + end + end + @named ex = Example() + vars = Symbolics.get_variables(only(equations(ex))) + @test length(vars) == 2 + for u in Symbolics.unwrap.(unknowns(ex)) + @test !Symbolics.hasmetadata(u, Symbolics.CallWithParent) + @test any(isequal(u), vars) + end +end From 25fd631c599b5c885d1faadddbceff5787c23533 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 20 Nov 2024 21:47:38 +0530 Subject: [PATCH 0540/2176] build: bump Symbolics compat --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 7b0997823c..2e4812c50f 100644 --- a/Project.toml +++ b/Project.toml @@ -143,7 +143,7 @@ StochasticDiffEq = "6.72.1" StochasticDelayDiffEq = "1.8.1" SymbolicIndexingInterface = "0.3.36" SymbolicUtils = "3.7" -Symbolics = "6.19" +Symbolics = "6.22" URIs = "1" UnPack = "0.1, 1.0" Unitful = "1.1" From 69e05f5b5031acc3902acf9edd5430dad75efa73 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 7 Jan 2025 12:35:57 +0530 Subject: [PATCH 0541/2176] docs: ignore Scholarpedia link in Documenter linkcheck --- docs/make.jl | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/docs/make.jl b/docs/make.jl index f380867e6c..36a27bf598 100644 --- a/docs/make.jl +++ b/docs/make.jl @@ -25,7 +25,12 @@ makedocs(sitename = "ModelingToolkit.jl", modules = [ModelingToolkit], clean = true, doctest = false, linkcheck = true, warnonly = [:docs_block, :missing_docs, :cross_references], - linkcheck_ignore = ["https://epubs.siam.org/doi/10.1137/0903023"], + linkcheck_ignore = [ + "https://epubs.siam.org/doi/10.1137/0903023", + # this link tends to fail linkcheck stochastically and often takes much longer to succeed + # even in the browser it takes ages + "http://www.scholarpedia.org/article/Differential-algebraic_equations" + ], format = Documenter.HTML(; assets = ["assets/favicon.ico"], mathengine, From da923ca2f583ad9f8dcffdcabe486d7f60d3d1b5 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 8 Jan 2025 11:33:01 +0530 Subject: [PATCH 0542/2176] fix: minor bug fix in `PolynomialTransformation` --- src/systems/nonlinear/homotopy_continuation.jl | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/systems/nonlinear/homotopy_continuation.jl b/src/systems/nonlinear/homotopy_continuation.jl index 00c6c7f1b0..09e11199e8 100644 --- a/src/systems/nonlinear/homotopy_continuation.jl +++ b/src/systems/nonlinear/homotopy_continuation.jl @@ -348,7 +348,8 @@ function PolynomialTransformation(sys::NonlinearSystem) # Is there a better way to check for uniqueness? `simplify` is relatively slow # (maybe use the threaded version?) and `expand` can blow up expression size. # Could metatheory help? - all_non_poly_terms = mapreduce(d -> d.non_polynomial_terms, vcat, polydata) + all_non_poly_terms = mapreduce( + d -> d.non_polynomial_terms, vcat, polydata; init = BasicSymbolic[]) unique!(all_non_poly_terms) # each variable can only be replaced by one non-polynomial expression involving From cb79bf269ad39aae8bf916b2c7060e6fa4176c51 Mon Sep 17 00:00:00 2001 From: Herman Sletmoen Date: Wed, 8 Jan 2025 15:59:18 +0100 Subject: [PATCH 0543/2176] Handle any error with logged functions --- src/debugging.jl | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/src/debugging.jl b/src/debugging.jl index 06e3edf0d8..80bf3a8f10 100644 --- a/src/debugging.jl +++ b/src/debugging.jl @@ -1,23 +1,23 @@ const LOGGED_FUN = Set([log, sqrt, (^), /, inv]) -is_legal(::typeof(/), a, b) = is_legal(inv, b) -is_legal(::typeof(inv), a) = !iszero(a) -is_legal(::Union{typeof(log), typeof(sqrt)}, a) = a isa Complex || a >= zero(a) -is_legal(::typeof(^), a, b) = a isa Complex || b isa Complex || isinteger(b) || a >= zero(a) +struct LoggedFunctionException <: Exception + msg::String +end struct LoggedFun{F} f::F args::Any end +Base.showerror(io::IO, err::LoggedFunctionException) = print(io, err.msg) Base.nameof(lf::LoggedFun) = nameof(lf.f) SymbolicUtils.promote_symtype(::LoggedFun, Ts...) = Real function (lf::LoggedFun)(args...) - f = lf.f - symbolic_args = lf.args - if is_legal(f, args...) - f(args...) - else - args_str = join(string.(symbolic_args .=> args), ", ", ", and ") - throw(DomainError(args, "$(lf.f) errors with input(s): $args_str")) + try + return lf.f(args...) # try to call with numerical input, as usual + catch err + throw(LoggedFunctionException( + "Function $(lf.f)($(join(lf.args, ", "))) errors with input" * + join("\n " .* string.(lf.args .=> args)) # one line for each "var => val" for readability + )) # Julia automatically attaches original error message end end From 075b90898239f5b0819885b4a8a167644679dd9c Mon Sep 17 00:00:00 2001 From: Herman Sletmoen Date: Wed, 8 Jan 2025 16:01:38 +0100 Subject: [PATCH 0544/2176] Update test (remove legacy branch) --- test/odesystem.jl | 25 +++++++++---------------- 1 file changed, 9 insertions(+), 16 deletions(-) diff --git a/test/odesystem.jl b/test/odesystem.jl index 85d135b338..c07594f726 100644 --- a/test/odesystem.jl +++ b/test/odesystem.jl @@ -931,22 +931,15 @@ testdict = Dict([:name => "test"]) @named sys = ODESystem(eqs, t, metadata = testdict) @test get_metadata(sys) == testdict -@variables P(t)=0 Q(t)=2 -∂t = D - -eqs = [∂t(Q) ~ 1 / sin(P) - ∂t(P) ~ log(-cos(Q))] -@named sys = ODESystem(eqs, t, [P, Q], []) -sys = complete(debug_system(sys)); -prob = ODEProblem(sys, [], (0, 1.0)); -du = zero(prob.u0); -if VERSION < v"1.8" - @test_throws DomainError prob.f(du, [1, 0], prob.p, 0.0) - @test_throws DomainError prob.f(du, [0, 2], prob.p, 0.0) -else - @test_throws "-cos(Q(t))" prob.f(du, [1, 0], prob.p, 0.0) - @test_throws "sin(P(t))" prob.f(du, [0, 2], prob.p, 0.0) -end +@variables P(t) = NaN Q(t) = NaN +@named sys = ODESystem([ + D(Q) ~ 1 / sin(P) + D(P) ~ log(-cos(Q)) +], t, [P, Q], []) +sys = complete(debug_system(sys)) +prob = ODEProblem(sys, [], (0.0, 1.0)) +@test_throws "log(-cos(Q(t))) errors" prob.f([1, 0], prob.p, 0.0) +@test prob.f([0, 2], prob.p, 0.0)[1] == 1 / 0 let @variables x(t) = 1 From e1cc46ed9333a1b978a1fbc11b40b6768d459db7 Mon Sep 17 00:00:00 2001 From: Herman Sletmoen Date: Wed, 8 Jan 2025 16:13:41 +0100 Subject: [PATCH 0545/2176] Update debug_system() docstring --- src/systems/abstractsystem.jl | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index 168260ae69..9aac35dad6 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -2266,19 +2266,17 @@ Replace functions with singularities with a function that errors with symbolic information. E.g. ```julia-repl -julia> sys = debug_system(sys); +julia> sys = debug_system(sys) -julia> sys = complete(sys); +julia> sys = complete(sys) -julia> prob = ODEProblem(sys, [], (0, 1.0)); +julia> prob = ODEProblem(sys, [1.0, 0.0], (0.0, 1.0)) -julia> du = zero(prob.u0); - -julia> prob.f(du, prob.u0, prob.p, 0.0) -ERROR: DomainError with (-1.0,): -log errors with input(s): -cos(Q(t)) => -1.0 +julia> prob.f(prob.u0, prob.p, 0.0) +ERROR: Function log(-cos(Q(t))) errors with input + -cos(Q(t)) => -1.0 Stacktrace: - [1] (::ModelingToolkit.LoggedFun{typeof(log)})(args::Float64) + [1] (::ModelingToolkit.LoggedFun{typeof(log)})(num_args::Float64) ... ``` """ From f2c94b5d70d52085ed49b694e18ec89818876aea Mon Sep 17 00:00:00 2001 From: Herman Sletmoen Date: Wed, 8 Jan 2025 16:13:54 +0100 Subject: [PATCH 0546/2176] Hint at flattening system in error message --- src/systems/abstractsystem.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index 9aac35dad6..1205a3d370 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -2282,7 +2282,7 @@ Stacktrace: """ function debug_system(sys::AbstractSystem) if has_systems(sys) && !isempty(get_systems(sys)) - error("debug_system only works on systems with no sub-systems!") + error("debug_system(sys) only works on systems with no sub-systems! Consider flattening it with flatten(sys) or structural_simplify(sys) first.") end if has_eqs(sys) @set! sys.eqs = debug_sub.(equations(sys)) From dcfbe2d00f1ef2d501e1948492e5dfd89ec9dead Mon Sep 17 00:00:00 2001 From: Herman Sletmoen Date: Wed, 8 Jan 2025 16:46:15 +0100 Subject: [PATCH 0547/2176] Error on non-finite values when debugging systems --- src/debugging.jl | 17 +++++++++++------ src/systems/abstractsystem.jl | 14 +++++--------- test/odesystem.jl | 2 +- 3 files changed, 17 insertions(+), 16 deletions(-) diff --git a/src/debugging.jl b/src/debugging.jl index 80bf3a8f10..830d32afce 100644 --- a/src/debugging.jl +++ b/src/debugging.jl @@ -7,18 +7,23 @@ struct LoggedFun{F} f::F args::Any end +LoggedFunctionException(lf::LoggedFun, args, msg) = LoggedFunctionException( + "Function $(lf.f)($(join(lf.args, ", "))) " * msg * " with input" * + join("\n " .* string.(lf.args .=> args)) # one line for each "var => val" for readability +) Base.showerror(io::IO, err::LoggedFunctionException) = print(io, err.msg) Base.nameof(lf::LoggedFun) = nameof(lf.f) SymbolicUtils.promote_symtype(::LoggedFun, Ts...) = Real function (lf::LoggedFun)(args...) - try - return lf.f(args...) # try to call with numerical input, as usual + val = try + lf.f(args...) # try to call with numerical input, as usual catch err - throw(LoggedFunctionException( - "Function $(lf.f)($(join(lf.args, ", "))) errors with input" * - join("\n " .* string.(lf.args .=> args)) # one line for each "var => val" for readability - )) # Julia automatically attaches original error message + throw(LoggedFunctionException(lf, args, "errors")) # Julia automatically attaches original error message end + if !isfinite(val) + throw(LoggedFunctionException(lf, args, "output non-finite value $val")) + end + return val end function logged_fun(f, args...) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index 1205a3d370..492ded28ee 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -2266,18 +2266,14 @@ Replace functions with singularities with a function that errors with symbolic information. E.g. ```julia-repl -julia> sys = debug_system(sys) +julia> sys = debug_system(complete(sys)) -julia> sys = complete(sys) - -julia> prob = ODEProblem(sys, [1.0, 0.0], (0.0, 1.0)) +julia> prob = ODEProblem(sys, [0.0, 2.0], (0.0, 1.0)) julia> prob.f(prob.u0, prob.p, 0.0) -ERROR: Function log(-cos(Q(t))) errors with input - -cos(Q(t)) => -1.0 -Stacktrace: - [1] (::ModelingToolkit.LoggedFun{typeof(log)})(num_args::Float64) - ... +ERROR: Function /(1, sin(P(t))) output non-finite value Inf with input + 1 => 1 + sin(P(t)) => 0.0 ``` """ function debug_system(sys::AbstractSystem) diff --git a/test/odesystem.jl b/test/odesystem.jl index c07594f726..cc2fe89ff3 100644 --- a/test/odesystem.jl +++ b/test/odesystem.jl @@ -939,7 +939,7 @@ testdict = Dict([:name => "test"]) sys = complete(debug_system(sys)) prob = ODEProblem(sys, [], (0.0, 1.0)) @test_throws "log(-cos(Q(t))) errors" prob.f([1, 0], prob.p, 0.0) -@test prob.f([0, 2], prob.p, 0.0)[1] == 1 / 0 +@test_throws "/(1, sin(P(t))) output non-finite value" prob.f([0, 2], prob.p, 0.0) let @variables x(t) = 1 From f5562dc215102895a6c0296877be4a886321067f Mon Sep 17 00:00:00 2001 From: Herman Sletmoen Date: Wed, 8 Jan 2025 17:07:35 +0100 Subject: [PATCH 0548/2176] Toggle extra non-finite value error with keyword argument --- src/debugging.jl | 13 +++++++------ src/systems/abstractsystem.jl | 12 +++++++----- 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/src/debugging.jl b/src/debugging.jl index 830d32afce..c8b7372c5d 100644 --- a/src/debugging.jl +++ b/src/debugging.jl @@ -6,6 +6,7 @@ end struct LoggedFun{F} f::F args::Any + error_nonfinite::Bool end LoggedFunctionException(lf::LoggedFun, args, msg) = LoggedFunctionException( "Function $(lf.f)($(join(lf.args, ", "))) " * msg * " with input" * @@ -20,22 +21,22 @@ function (lf::LoggedFun)(args...) catch err throw(LoggedFunctionException(lf, args, "errors")) # Julia automatically attaches original error message end - if !isfinite(val) + if lf.error_nonfinite && !isfinite(val) throw(LoggedFunctionException(lf, args, "output non-finite value $val")) end return val end -function logged_fun(f, args...) +function logged_fun(f, args...; error_nonfinite = true) # remember to update error_nonfinite in debug_system() docstring # Currently we don't really support complex numbers - term(LoggedFun(f, args), args..., type = Real) + term(LoggedFun(f, args, error_nonfinite), args..., type = Real) end -debug_sub(eq::Equation) = debug_sub(eq.lhs) ~ debug_sub(eq.rhs) -function debug_sub(ex) +debug_sub(eq::Equation; kw...) = debug_sub(eq.lhs; kw...) ~ debug_sub(eq.rhs; kw...) +function debug_sub(ex; kw...) iscall(ex) || return ex f = operation(ex) args = map(debug_sub, arguments(ex)) - f in LOGGED_FUN ? logged_fun(f, args...) : + f in LOGGED_FUN ? logged_fun(f, args...; kw...) : maketerm(typeof(ex), f, args, metadata(ex)) end diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index 492ded28ee..c3525c542b 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -2260,10 +2260,12 @@ macro mtkbuild(exprs...) end """ -$(SIGNATURES) + debug_system(sys::AbstractSystem; error_nonfinite = true) Replace functions with singularities with a function that errors with symbolic -information. E.g. +information. If `error_nonfinite`, debugged functions that output nonfinite values +(like `Inf` or `NaN`) also display errors, even though the raw function itself +does not throw an exception (like `1/0`). For example: ```julia-repl julia> sys = debug_system(complete(sys)) @@ -2276,15 +2278,15 @@ ERROR: Function /(1, sin(P(t))) output non-finite value Inf with input sin(P(t)) => 0.0 ``` """ -function debug_system(sys::AbstractSystem) +function debug_system(sys::AbstractSystem; kw...) if has_systems(sys) && !isempty(get_systems(sys)) error("debug_system(sys) only works on systems with no sub-systems! Consider flattening it with flatten(sys) or structural_simplify(sys) first.") end if has_eqs(sys) - @set! sys.eqs = debug_sub.(equations(sys)) + @set! sys.eqs = debug_sub.(equations(sys); kw...) end if has_observed(sys) - @set! sys.observed = debug_sub.(observed(sys)) + @set! sys.observed = debug_sub.(observed(sys); kw...) end return sys end From 94576dbec700682eef80054dce76f1dcf6c78518 Mon Sep 17 00:00:00 2001 From: Herman Sletmoen Date: Wed, 8 Jan 2025 17:27:32 +0100 Subject: [PATCH 0549/2176] Let user pass list of functions that should be debugged --- src/debugging.jl | 10 ++++------ src/systems/abstractsystem.jl | 17 ++++++++++------- 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/src/debugging.jl b/src/debugging.jl index c8b7372c5d..f6652183cb 100644 --- a/src/debugging.jl +++ b/src/debugging.jl @@ -1,5 +1,3 @@ -const LOGGED_FUN = Set([log, sqrt, (^), /, inv]) - struct LoggedFunctionException <: Exception msg::String end @@ -32,11 +30,11 @@ function logged_fun(f, args...; error_nonfinite = true) # remember to update err term(LoggedFun(f, args, error_nonfinite), args..., type = Real) end -debug_sub(eq::Equation; kw...) = debug_sub(eq.lhs; kw...) ~ debug_sub(eq.rhs; kw...) -function debug_sub(ex; kw...) +debug_sub(eq::Equation, funcs; kw...) = debug_sub(eq.lhs, funcs; kw...) ~ debug_sub(eq.rhs, funcs; kw...) +function debug_sub(ex, funcs; kw...) iscall(ex) || return ex f = operation(ex) - args = map(debug_sub, arguments(ex)) - f in LOGGED_FUN ? logged_fun(f, args...; kw...) : + args = map(ex -> debug_sub(ex, funcs; kw...), arguments(ex)) + f in funcs ? logged_fun(f, args...; kw...) : maketerm(typeof(ex), f, args, metadata(ex)) end diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index c3525c542b..796ff73eba 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -2260,11 +2260,11 @@ macro mtkbuild(exprs...) end """ - debug_system(sys::AbstractSystem; error_nonfinite = true) + debug_system(sys::AbstractSystem; functions = [log, sqrt, (^), /, inv, asin, acos], error_nonfinite = true) -Replace functions with singularities with a function that errors with symbolic -information. If `error_nonfinite`, debugged functions that output nonfinite values -(like `Inf` or `NaN`) also display errors, even though the raw function itself +Wrap `functions` in `sys` so any error thrown in them shows helpful symbolic-numeric +information about its input. If `error_nonfinite`, functions that output nonfinite +values (like `Inf` or `NaN`) also display errors, even though the raw function itself does not throw an exception (like `1/0`). For example: ```julia-repl @@ -2278,15 +2278,18 @@ ERROR: Function /(1, sin(P(t))) output non-finite value Inf with input sin(P(t)) => 0.0 ``` """ -function debug_system(sys::AbstractSystem; kw...) +function debug_system(sys::AbstractSystem; functions = [log, sqrt, (^), /, inv, asin, acos], kw...) + if !(functions isa Set) + functions = Set(functions) # more efficient "in" lookup + end if has_systems(sys) && !isempty(get_systems(sys)) error("debug_system(sys) only works on systems with no sub-systems! Consider flattening it with flatten(sys) or structural_simplify(sys) first.") end if has_eqs(sys) - @set! sys.eqs = debug_sub.(equations(sys); kw...) + @set! sys.eqs = debug_sub.(equations(sys), Ref(functions); kw...) end if has_observed(sys) - @set! sys.observed = debug_sub.(observed(sys); kw...) + @set! sys.observed = debug_sub.(observed(sys), Ref(functions); kw...) end return sys end From ca788528b7046163100b1821f16907721c98c9f5 Mon Sep 17 00:00:00 2001 From: Herman Sletmoen Date: Wed, 8 Jan 2025 17:39:46 +0100 Subject: [PATCH 0550/2176] Format --- src/debugging.jl | 14 +++++++++----- src/systems/abstractsystem.jl | 3 ++- test/odesystem.jl | 8 +++----- 3 files changed, 14 insertions(+), 11 deletions(-) diff --git a/src/debugging.jl b/src/debugging.jl index f6652183cb..a1a168d8dd 100644 --- a/src/debugging.jl +++ b/src/debugging.jl @@ -6,10 +6,12 @@ struct LoggedFun{F} args::Any error_nonfinite::Bool end -LoggedFunctionException(lf::LoggedFun, args, msg) = LoggedFunctionException( - "Function $(lf.f)($(join(lf.args, ", "))) " * msg * " with input" * - join("\n " .* string.(lf.args .=> args)) # one line for each "var => val" for readability -) +function LoggedFunctionException(lf::LoggedFun, args, msg) + LoggedFunctionException( + "Function $(lf.f)($(join(lf.args, ", "))) " * msg * " with input" * + join("\n " .* string.(lf.args .=> args)) # one line for each "var => val" for readability + ) +end Base.showerror(io::IO, err::LoggedFunctionException) = print(io, err.msg) Base.nameof(lf::LoggedFun) = nameof(lf.f) SymbolicUtils.promote_symtype(::LoggedFun, Ts...) = Real @@ -30,7 +32,9 @@ function logged_fun(f, args...; error_nonfinite = true) # remember to update err term(LoggedFun(f, args, error_nonfinite), args..., type = Real) end -debug_sub(eq::Equation, funcs; kw...) = debug_sub(eq.lhs, funcs; kw...) ~ debug_sub(eq.rhs, funcs; kw...) +function debug_sub(eq::Equation, funcs; kw...) + debug_sub(eq.lhs, funcs; kw...) ~ debug_sub(eq.rhs, funcs; kw...) +end function debug_sub(ex, funcs; kw...) iscall(ex) || return ex f = operation(ex) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index 796ff73eba..e6c07bcf9c 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -2278,7 +2278,8 @@ ERROR: Function /(1, sin(P(t))) output non-finite value Inf with input sin(P(t)) => 0.0 ``` """ -function debug_system(sys::AbstractSystem; functions = [log, sqrt, (^), /, inv, asin, acos], kw...) +function debug_system( + sys::AbstractSystem; functions = [log, sqrt, (^), /, inv, asin, acos], kw...) if !(functions isa Set) functions = Set(functions) # more efficient "in" lookup end diff --git a/test/odesystem.jl b/test/odesystem.jl index cc2fe89ff3..94f676461b 100644 --- a/test/odesystem.jl +++ b/test/odesystem.jl @@ -931,11 +931,9 @@ testdict = Dict([:name => "test"]) @named sys = ODESystem(eqs, t, metadata = testdict) @test get_metadata(sys) == testdict -@variables P(t) = NaN Q(t) = NaN -@named sys = ODESystem([ - D(Q) ~ 1 / sin(P) - D(P) ~ log(-cos(Q)) -], t, [P, Q], []) +@variables P(t)=NaN Q(t)=NaN +eqs = [D(Q) ~ 1 / sin(P), D(P) ~ log(-cos(Q))] +@named sys = ODESystem(eqs, t, [P, Q], []) sys = complete(debug_system(sys)) prob = ODEProblem(sys, [], (0.0, 1.0)) @test_throws "log(-cos(Q(t))) errors" prob.f([1, 0], prob.p, 0.0) From ef1f089cbd493272e2b9297c2189aa2a6029d72f Mon Sep 17 00:00:00 2001 From: vyudu Date: Wed, 8 Jan 2025 15:12:12 -0500 Subject: [PATCH 0551/2176] up --- src/systems/diffeqs/abstractodesystem.jl | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/src/systems/diffeqs/abstractodesystem.jl b/src/systems/diffeqs/abstractodesystem.jl index d8ff71c324..87ab83f10b 100644 --- a/src/systems/diffeqs/abstractodesystem.jl +++ b/src/systems/diffeqs/abstractodesystem.jl @@ -930,16 +930,6 @@ end get_callback(prob::BVProblem) = error("BVP solvers do not support callbacks.") -@inline function SymbolicUtils.Code.create_array(::Type{<:Base.ReinterpretArray}, ::Nothing, - ::Val{1}, ::Val{dims}, elems...) where {dims} - [elems...] -end - -@inline function SymbolicUtils.Code.create_array( - ::Type{<:Base.ReinterpretArray}, T, ::Val{1}, ::Val{dims}, elems...) where {dims} - T[elems...] -end - """ ```julia DiffEqBase.DAEProblem{iip}(sys::AbstractODESystem, du0map, u0map, tspan, From 9921abe9fce3250ffa2d2535e61c31fcffeb701a Mon Sep 17 00:00:00 2001 From: Torkel Loman Date: Thu, 9 Jan 2025 11:24:58 +0000 Subject: [PATCH 0552/2176] DifferentialEquations -> OrdinaryDiffEqDefault --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index d93a18e3f7..12570c35a3 100644 --- a/README.md +++ b/README.md @@ -36,7 +36,7 @@ lower it to a first order system, symbolically generate the Jacobian function for the numerical integrator, and solve it. ```julia -using DifferentialEquations, ModelingToolkit +using OrdinaryDiffEqDefault, ModelingToolkit using ModelingToolkit: t_nounits as t, D_nounits as D @parameters σ ρ β From 2a25200bcb6a1ad57c4329b6383ba844eb73c2f7 Mon Sep 17 00:00:00 2001 From: vyudu Date: Thu, 9 Jan 2025 17:36:38 -0500 Subject: [PATCH 0553/2176] extend BVProblem for constraint equations --- src/systems/diffeqs/abstractodesystem.jl | 74 +++++++++++++++++-- test/bvproblem.jl | 90 ++++++++++++++++++++++++ 2 files changed, 160 insertions(+), 4 deletions(-) diff --git a/src/systems/diffeqs/abstractodesystem.jl b/src/systems/diffeqs/abstractodesystem.jl index f5c1c288d7..74b1bf7596 100644 --- a/src/systems/diffeqs/abstractodesystem.jl +++ b/src/systems/diffeqs/abstractodesystem.jl @@ -873,7 +873,7 @@ function SciMLBase.BVProblem(sys::AbstractODESystem, kwargs...) BVProblem{false, SciMLBase.FullSpecialize}(sys, u0map, args...; kwargs...) end - +o function SciMLBase.BVProblem{true}(sys::AbstractODESystem, args...; kwargs...) BVProblem{true, SciMLBase.AutoSpecialize}(sys, args...; kwargs...) end @@ -908,11 +908,32 @@ function SciMLBase.BVProblem{iip, specialize}(sys::AbstractODESystem, u0map = [] kwargs1 = merge(kwargs1, (callback = cbs,)) end + # Handle algebraic equations + stidxmap = Dict([v => i for (i, v) in enumerate(unknowns(sys))]) + pidxmap = Dict([v => i for (i, v) in enumerate(parameters(sys))]) + ns = length(stmap) + ne = length(get_alg_eqs(sys)) + # Define the boundary conditions. - bc = if iip - (residual, u, p, t) -> (residual .= u[1] .- u0) + bc = if has_alg_eqs(sys) + if iip + (residual,u,p,t) -> begin + residual[1:ns] .= u[1] .- u0 + residual[ns+1:ns+ne] .= sub_u_p_into_symeq.(get_alg_eqs(sys)) + end + else + (u,p,t) -> begin + resid = vcat(u[1] - u0, sub_u_p_into_symeq.(get_alg_eqs(sys))) + end + end else - (u, p, t) -> (u[1] - u0) + if iip + (residual,u,p,t) -> begin + residual .= u[1] .- u0 + end + else + (u,p,t) -> (u[1] - u0) + end end return BVProblem{iip}(f, bc, u0, tspan, p; kwargs1..., kwargs...) @@ -920,6 +941,51 @@ end get_callback(prob::BVProblem) = error("BVP solvers do not support callbacks.") +# Helper to create the dictionary that will substitute numeric values for u, p into the algebraic equations in the ODESystem. Used to construct the boundary condition function. +# Take a system with variables x,y, parameters g +# +# 1 + x + y → 1 + u[1][1] + u[1][2] +# x(0.5) → u(0.5)[1] +# x(0.5)*g(0.5) → u(0.5)[1]*p[1] + +function sub_u_p_into_symeq(eq, u, p, stidxmap, pidxmap) + iv = ModelingToolkit.get_iv(sys) + eq = Symbolics.unwrap(eq) + + stmap = Dict([st => u[1][i] for st => i in stidxmap]) + pmap = Dict([pa => p[i] for pa => i in pidxmap]) + eq = Symbolics.substitute(eq, merge(stmap, pmap)) + + csyms = [] + # Find most nested calls, substitute those first. + while !isempty(find_callable_syms!(csyms, eq)) + for sym in csyms + t = arguments(sym)[1] + x = operation(sym) + + if isparameter(x) + eq = Symbolics.substitute(eq, Dict(x(t) => p[pidxmap[x(iv)]])) + elseif isvariable(x) + eq = Symbolics.substitute(eq, Dict(x(t) => u(val)[stidxmap[x(iv)]])) + end + end + empty!(csyms) + end + eq +end + +function find_callable_syms!(csyms, ex) + ex = Symbolics.unwrap(ex) + + if iscall(ex) + operation(ex) isa Symbolic && (arguments(ex)[1] isa Symbolic) && push!(csyms, ex) # only add leaf nodes + for arg in arguments(ex) + find_callable_syms!(csyms, arg) + end + end + csyms +end + """ ```julia DiffEqBase.DAEProblem{iip}(sys::AbstractODESystem, du0map, u0map, tspan, diff --git a/test/bvproblem.jl b/test/bvproblem.jl index c5a302147d..2d5535325a 100644 --- a/test/bvproblem.jl +++ b/test/bvproblem.jl @@ -2,6 +2,7 @@ using BoundaryValueDiffEq, OrdinaryDiffEq using ModelingToolkit using ModelingToolkit: t_nounits as t, D_nounits as D +### Test Collocation solvers on simple problems solvers = [MIRK4, RadauIIa5, LobattoIIIa3] @parameters α=7.5 β=4.0 γ=8.0 δ=5.0 @@ -68,3 +69,92 @@ for solver in solvers @test isapprox(sol.u[end], osol.u[end]; atol = 0.01) @test sol.u[1] == [π / 2, π / 2] end + +################################################### +### TESTING ODESystem with Constraint Equations ### +################################################### + +# Cartesian pendulum from the docs. Testing that initialization is satisfied. +let + @parameters g + @variables x(t) y(t) [state_priority = 10] λ(t) + eqs = [D(D(x)) ~ λ * x + D(D(y)) ~ λ * y - g + x^2 + y^2 ~ 1] + @mtkbuild pend = ODESystem(eqs, t) + + tspan = (0.0, 1.5) + u0map = [x => 1, y => 0] + parammap = [g => 1] + guesses = [λ => 1] + + prob = ODEProblem(pend, u0map, tspan, pmap; guesses) + sol = solve(prob, Rodas5P()) + + bvp = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}(pend, u0map, tspan, parammap; guesses) + + for solver in solvers + sol = solve(bvp, solver(), dt = 0.01) + @test isapprox(sol.u[end], osol.u[end]; atol = 0.01) + conditions = getfield.(equations(pend)[3:end], :rhs) + @test [sol[conditions][1]; sol[x][1] - 1; sol[y][1]] ≈ 0 + end + + bvp2 = SciMLBase.BVProblem{false, SciMLBase.FullSpecialize}(pend, u0map, tspan, parammap) + for solver in solvers + sol = solve(bvp, solver(), dt = 0.01) + @test isapprox(sol.u[end], osol.u[end]; atol = 0.01) + conditions = getfield.(equations(pend)[3:end], :rhs) + @test [sol[conditions][1]; sol[x][1] - 1; sol[y][1]] ≈ 0 + end +end + +# Adding a midpoint boundary condition. +let + @parameters g + @variables x(..) y(t) [state_priority = 10] λ(t) + eqs = [D(D(x(t))) ~ λ * x(t) + D(D(y)) ~ λ * y - g + x(t)^2 + y^2 ~ 1 + x(0.5) ~ 1] + @mtkbuild pend = ODESystem(eqs, t) + + tspan = (0.0, 1.5) + u0map = [x(t) => 0.6, y => 0.8] + parammap = [g => 1] + guesses = [λ => 1] + + prob = ODEProblem(pend, u0map, tspan, pmap; guesses, check_length = false) + sol = solve(prob, Rodas5P()) + + bvp = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}(pend, u0map, tspan, parammap; guesses = guesses, check_length = false) + + for solver in solvers + sol = solve(bvp, solver(), dt = 0.01) + @test isapprox(sol.u[end], osol.u[end]; atol = 0.01) + conditions = getfield.(equations(pend)[3:end], :rhs) + @test [sol[conditions][1]; sol[x][1] - 1; sol[y][1]] ≈ 0 + @test sol.u[1] == [π / 2, π / 2] + end + + bvp = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}(pend, u0map, tspan, parammap; guesses) + + for solver in solvers + sol = solve(bvp, solver(), dt = 0.01) + @test isapprox(sol.u[end], osol.u[end]; atol = 0.01) + conditions = getfield.(equations(pend)[3:end], :rhs) + @test [sol[conditions][1]; sol[x][1] - 1; sol[y][1]] ≈ 0 + end + + bvp2 = SciMLBase.BVProblem{false, SciMLBase.FullSpecialize}(pend, u0map, tspan, parammap) + for solver in solvers + sol = solve(bvp, solver(), dt = 0.01) + @test isapprox(sol.u[end], osol.u[end]; atol = 0.01) + conditions = getfield.(equations(pend)[3:end], :rhs) + @test [sol[conditions][1]; sol[x][1] - 1; sol[y][1]] ≈ 0 + end +end + +# Testing a more complicated case with multiple constraints. +let +end From 761f5ea75a39e4264c765d6d00b37b302581cf47 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 9 Jan 2025 13:01:27 +0530 Subject: [PATCH 0554/2176] feat: allow remade initialization systems to be incomplete --- src/systems/diffeqs/abstractodesystem.jl | 9 ++++++++- src/systems/nonlinear/initializesystem.jl | 9 ++++++++- src/systems/problem_utils.jl | 5 ++++- test/initializationsystem.jl | 24 +++++++++++++++++++++++ 4 files changed, 44 insertions(+), 3 deletions(-) diff --git a/src/systems/diffeqs/abstractodesystem.jl b/src/systems/diffeqs/abstractodesystem.jl index b012bc73d4..3da5b6a2d0 100644 --- a/src/systems/diffeqs/abstractodesystem.jl +++ b/src/systems/diffeqs/abstractodesystem.jl @@ -1294,6 +1294,7 @@ function InitializationProblem{iip, specialize}(sys::AbstractSystem, fully_determined = nothing, check_units = true, use_scc = true, + allow_incomplete = false, kwargs...) where {iip, specialize} if !iscomplete(sys) error("A completed system is required. Call `complete` or `structural_simplify` on the system before creating an `ODEProblem`") @@ -1335,7 +1336,13 @@ function InitializationProblem{iip, specialize}(sys::AbstractSystem, # TODO: throw on uninitialized arrays filter!(x -> !(x isa Symbolics.Arr), uninit) if !isempty(uninit) - throw(IncompleteInitializationError(uninit)) + allow_incomplete || throw(IncompleteInitializationError(uninit)) + # for incomplete initialization, we will add the missing variables as parameters. + # they will be updated by `update_initializeprob!` and `initializeprobmap` will + # use them to construct the new `u0`. + newparams = map(toparam, uninit) + append!(get_ps(isys), newparams) + isys = complete(isys) end neqs = length(equations(isys)) diff --git a/src/systems/nonlinear/initializesystem.jl b/src/systems/nonlinear/initializesystem.jl index 602fc7cac9..c3bbd92317 100644 --- a/src/systems/nonlinear/initializesystem.jl +++ b/src/systems/nonlinear/initializesystem.jl @@ -317,6 +317,12 @@ function SciMLBase.remake_initialization_data( u0map[dvs[i]] = newu0[i] end end + # ensure all unknowns have guesses in case they weren't given one + # and become solvable + for i in eachindex(dvs) + haskey(guesses, dvs[i]) && continue + guesses[dvs[i]] = newu0[i] + end if p === missing # the user didn't pass `p` to `remake`, so they want to retain # existing values. Fill all parameters in `pmap` so that none of @@ -341,7 +347,8 @@ function SciMLBase.remake_initialization_data( op, missing_unknowns, missing_pars = build_operating_point( u0map, pmap, defs, cmap, dvs, ps) kws = maybe_build_initialization_problem( - sys, op, u0map, pmap, t0, defs, guesses, missing_unknowns; use_scc, initialization_eqs) + sys, op, u0map, pmap, t0, defs, guesses, missing_unknowns; + use_scc, initialization_eqs, allow_incomplete = true) return get(kws, :initialization_data, nothing) end diff --git a/src/systems/problem_utils.jl b/src/systems/problem_utils.jl index 6ce5704daa..4cbf495d87 100644 --- a/src/systems/problem_utils.jl +++ b/src/systems/problem_utils.jl @@ -545,7 +545,10 @@ function maybe_build_initialization_problem( (!is_time_dependent(sys) || t !== nothing) initializeprob = ModelingToolkit.InitializationProblem( sys, t, u0map, pmap; guesses, kwargs...) - initializeprobmap = getu(initializeprob, unknowns(sys)) + + all_init_syms = Set(all_symbols(initializeprob)) + solved_unknowns = filter(var -> var in all_init_syms, unknowns(sys)) + initializeprobmap = getu(initializeprob, solved_unknowns) punknowns = [p for p in all_variable_symbols(initializeprob) diff --git a/test/initializationsystem.jl b/test/initializationsystem.jl index 2745b9d81f..ecf6899624 100644 --- a/test/initializationsystem.jl +++ b/test/initializationsystem.jl @@ -1282,3 +1282,27 @@ end @test SciMLBase.successful_retcode(solve(newprob)) end + +@testset "Issue#3295: Incomplete initialization of pure-ODE systems" begin + @variables X(t) Y(t) + @parameters p d + eqs = [ + D(X) ~ p - d * X, + D(Y) ~ p - d * Y + ] + @mtkbuild osys = ODESystem(eqs, t) + + # Make problem. + u0_vals = [X => 4, Y => 5.0] + tspan = (0.0, 10.0) + p_vals = [p => 1.0, d => 0.1] + oprob = ODEProblem(osys, u0_vals, tspan, p_vals) + integ = init(oprob) + @test integ[X] ≈ 4.0 + @test integ[Y] ≈ 5.0 + # Attempt to `remake`. + rp = remake(oprob; u0 = [Y => 7]) + integ = init(rp) + @test integ[X] ≈ 4.0 + @test integ[Y] ≈ 7.0 +end From fa0df9d7e215376967acc2f00e06feaea7e8d730 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 10 Jan 2025 14:22:38 +0530 Subject: [PATCH 0555/2176] test: fix initialization test --- test/initializationsystem.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/initializationsystem.jl b/test/initializationsystem.jl index ecf6899624..6f8c961b45 100644 --- a/test/initializationsystem.jl +++ b/test/initializationsystem.jl @@ -116,7 +116,7 @@ end x′ end @variables begin - p(t) = p′ + p(t) x(t) = x′ dm(t) = 0 f(t) = p′ * A From 755b3b6eb5d7ce31b94e1912e8ceeb1504129c6e Mon Sep 17 00:00:00 2001 From: Herman Sletmoen Date: Fri, 10 Jan 2025 15:23:21 +0100 Subject: [PATCH 0556/2176] Add documentation page for debugging --- docs/pages.jl | 1 + docs/src/basics/Debugging.md | 44 ++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 docs/src/basics/Debugging.md diff --git a/docs/pages.jl b/docs/pages.jl index 2af487adf8..5dd869625c 100644 --- a/docs/pages.jl +++ b/docs/pages.jl @@ -31,6 +31,7 @@ pages = [ "basics/InputOutput.md", "basics/MTKLanguage.md", "basics/Validation.md", + "basics/Debugging.md", "basics/DependencyGraphs.md", "basics/Precompilation.md", "basics/FAQ.md"], diff --git a/docs/src/basics/Debugging.md b/docs/src/basics/Debugging.md new file mode 100644 index 0000000000..29fdc5f215 --- /dev/null +++ b/docs/src/basics/Debugging.md @@ -0,0 +1,44 @@ +# Debugging + +Every (mortal) modeler writes models that contain mistakes or are susceptible to numerical errors in their hunt for the perfect model. +Debugging such errors is part of the modeling process, and ModelingToolkit includes some functionality that helps with this. + +For example, consider an ODE model with "dangerous" functions (here `√`): + +```@example debug +using ModelingToolkit, OrdinaryDiffEq +using ModelingToolkit: t_nounits as t, D_nounits as D + +@variables u1(t) u2(t) u3(t) +eqs = [D(u1) ~ -√(u1), D(u2) ~ -√(u2), D(u3) ~ -√(u3)] +defaults = [u1 => 1.0, u2 => 2.0, u3 => 3.0] +@named sys = ODESystem(eqs, t; defaults) +sys = structural_simplify(sys) +``` + +This problem causes the ODE solver to crash: + +```@example debug +prob = ODEProblem(sys, [], (0.0, 10.0), []) +sol = solve(prob, Tsit5()) +``` + +This suggests *that* something went wrong, but not exactly *what* went wrong and *where* it did. +In such situations, the `debug_system` function is helpful: + +```@example debug +try # workaround to show Documenter.jl error (https://github.com/JuliaDocs/Documenter.jl/issues/1420#issuecomment-770539595) # hide +dsys = debug_system(sys; functions = [sqrt]) +dprob = ODEProblem(dsys, [], (0.0, 10.0), []) +dsol = solve(dprob, Tsit5()) +catch err # hide +showerror(stderr, err) # hide +end # hide +``` + +Now we see that it crashed because `u1` decreased so much that it became negative and outside the domain of the `√` function. +We could have figured that out ourselves, but it is not always so obvious for more complex models. + +```@docs +debug_system +``` From 3bc5e155ff6260d536e48cea872d55de4a7b2b62 Mon Sep 17 00:00:00 2001 From: Herman Sletmoen Date: Fri, 10 Jan 2025 16:03:46 +0100 Subject: [PATCH 0557/2176] Use @repl block instead of @example to show errors in documentation without workarounds --- docs/src/basics/Debugging.md | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/docs/src/basics/Debugging.md b/docs/src/basics/Debugging.md index 29fdc5f215..d5c51ec0c1 100644 --- a/docs/src/basics/Debugging.md +++ b/docs/src/basics/Debugging.md @@ -18,22 +18,18 @@ sys = structural_simplify(sys) This problem causes the ODE solver to crash: -```@example debug -prob = ODEProblem(sys, [], (0.0, 10.0), []) -sol = solve(prob, Tsit5()) +```@repl debug +prob = ODEProblem(sys, [], (0.0, 10.0), []); +sol = solve(prob, Tsit5()); ``` This suggests *that* something went wrong, but not exactly *what* went wrong and *where* it did. In such situations, the `debug_system` function is helpful: -```@example debug -try # workaround to show Documenter.jl error (https://github.com/JuliaDocs/Documenter.jl/issues/1420#issuecomment-770539595) # hide -dsys = debug_system(sys; functions = [sqrt]) -dprob = ODEProblem(dsys, [], (0.0, 10.0), []) -dsol = solve(dprob, Tsit5()) -catch err # hide -showerror(stderr, err) # hide -end # hide +```@repl debug +dsys = debug_system(sys; functions = [sqrt]); +dprob = ODEProblem(dsys, [], (0.0, 10.0), []); +dsol = solve(dprob, Tsit5()); ``` Now we see that it crashed because `u1` decreased so much that it became negative and outside the domain of the `√` function. From 50504abbcf0149d50bd6e858ae1c5f368f8d2835 Mon Sep 17 00:00:00 2001 From: vyudu Date: Sat, 11 Jan 2025 13:57:59 -0500 Subject: [PATCH 0558/2176] adding tests --- Project.toml | 6 +- src/systems/diffeqs/abstractodesystem.jl | 140 ++++++++++--- test/bvproblem.jl | 242 ++++++++++++++--------- 3 files changed, 261 insertions(+), 127 deletions(-) diff --git a/Project.toml b/Project.toml index 98fd119f0c..b0bd4381b6 100644 --- a/Project.toml +++ b/Project.toml @@ -82,6 +82,7 @@ ArrayInterface = "6, 7" BifurcationKit = "0.4" BlockArrays = "1.1" BoundaryValueDiffEq = "5.12.0" +BoundaryValueDiffEqAscher = "1.1.0" ChainRulesCore = "1" Combinatorics = "1" CommonSolve = "0.2.4" @@ -140,8 +141,8 @@ SimpleNonlinearSolve = "0.1.0, 1, 2" SparseArrays = "1" SpecialFunctions = "0.7, 0.8, 0.9, 0.10, 1.0, 2" StaticArrays = "0.10, 0.11, 0.12, 1.0" -StochasticDiffEq = "6.72.1" StochasticDelayDiffEq = "1.8.1" +StochasticDiffEq = "6.72.1" SymbolicIndexingInterface = "0.3.36" SymbolicUtils = "3.7" Symbolics = "6.19" @@ -154,6 +155,7 @@ julia = "1.9" AmplNLWriter = "7c4d4715-977e-5154-bfe0-e096adeac482" BenchmarkTools = "6e4b80f9-dd63-53aa-95a3-0cdb28fa8baf" BoundaryValueDiffEq = "764a87c0-6b3e-53db-9096-fe964310641d" +BoundaryValueDiffEqAscher = "7227322d-7511-4e07-9247-ad6ff830280e" ControlSystemsBase = "aaaaaaaa-a6ca-5380-bf3e-84a91bcd477e" DataInterpolations = "82cc6244-b520-54b8-b5a6-8a565e85f1d0" DeepDiffs = "ab62b9b5-e342-54a8-a765-a90f495de1a6" @@ -185,4 +187,4 @@ Sundials = "c3572dad-4567-51f8-b174-8c6c989267f4" Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" [targets] -test = ["AmplNLWriter", "BenchmarkTools", "ControlSystemsBase", "DataInterpolations", "DelayDiffEq", "NonlinearSolve", "ForwardDiff", "Ipopt", "Ipopt_jll", "ModelingToolkitStandardLibrary", "Optimization", "OptimizationOptimJL", "OptimizationMOI", "OrdinaryDiffEq", "OrdinaryDiffEqCore", "OrdinaryDiffEqDefault", "REPL", "Random", "ReferenceTests", "SafeTestsets", "StableRNGs", "Statistics", "SteadyStateDiffEq", "Test", "StochasticDiffEq", "Sundials", "StochasticDelayDiffEq", "Pkg", "JET", "OrdinaryDiffEqNonlinearSolve"] +test = ["AmplNLWriter", "BenchmarkTools", "BoundaryValueDiffEq", "BoundaryValueDiffEqAscher", "ControlSystemsBase", "DataInterpolations", "DelayDiffEq", "NonlinearSolve", "ForwardDiff", "Ipopt", "Ipopt_jll", "ModelingToolkitStandardLibrary", "Optimization", "OptimizationOptimJL", "OptimizationMOI", "OrdinaryDiffEq", "OrdinaryDiffEqCore", "OrdinaryDiffEqDefault", "REPL", "Random", "ReferenceTests", "SafeTestsets", "StableRNGs", "Statistics", "SteadyStateDiffEq", "Test", "StochasticDiffEq", "Sundials", "StochasticDelayDiffEq", "Pkg", "JET", "OrdinaryDiffEqNonlinearSolve"] diff --git a/src/systems/diffeqs/abstractodesystem.jl b/src/systems/diffeqs/abstractodesystem.jl index 74b1bf7596..3c45b1b2f8 100644 --- a/src/systems/diffeqs/abstractodesystem.jl +++ b/src/systems/diffeqs/abstractodesystem.jl @@ -853,15 +853,47 @@ get_callback(prob::ODEProblem) = prob.kwargs[:callback] ```julia SciMLBase.BVProblem{iip}(sys::AbstractODESystem, u0map, tspan, parammap = DiffEqBase.NullParameters(); + constraints = nothing, guesses = nothing, version = nothing, tgrad = false, jac = true, sparse = true, simplify = false, kwargs...) where {iip} ``` -Create a `BVProblem` from the [`ODESystem`](@ref). The arguments `dvs` and +Create a boundary value problem from the [`ODESystem`](@ref). The arguments `dvs` and `ps` are used to set the order of the dependent variable and parameter vectors, -respectively. `u0map` should be used to specify the initial condition. +respectively. `u0map` is used to specify fixed initial values for the states. + +Every variable must have either an initial guess supplied using `guesses` or +a fixed initial value specified using `u0map`. + +`constraints` are used to specify boundary conditions to the ODESystem in the +form of equations. These values should specify values that state variables should +take at specific points, as in `x(0.5) ~ 1`). More general constraints that +should hold over the entire solution, such as `x(t)^2 + y(t)^2`, should be +specified as one of the equations used to build the `ODESystem`. Below is an example. + +```julia + @parameters g + @variables x(..) y(t) [state_priority = 10] λ(t) + eqs = [D(D(x(t))) ~ λ * x(t) + D(D(y)) ~ λ * y - g + x(t)^2 + y^2 ~ 1] + @mtkbuild pend = ODESystem(eqs, t) + + tspan = (0.0, 1.5) + u0map = [x(t) => 0.6, y => 0.8] + parammap = [g => 1] + guesses = [λ => 1] + constraints = [x(0.5) ~ 1] + + bvp = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}(pend, u0map, tspan, parammap; constraints, guesses, check_length = false) +``` + +If no `constraints` are specified, the problem will be treated as an initial value problem. + +If the `ODESystem` has algebraic equations like `x(t)^2 + y(t)^2`, the resulting +`BVProblem` must be solved using BVDAE solvers, such as Ascher. """ function SciMLBase.BVProblem(sys::AbstractODESystem, args...; kwargs...) BVProblem{true}(sys, args...; kwargs...) @@ -873,7 +905,7 @@ function SciMLBase.BVProblem(sys::AbstractODESystem, kwargs...) BVProblem{false, SciMLBase.FullSpecialize}(sys, u0map, args...; kwargs...) end -o + function SciMLBase.BVProblem{true}(sys::AbstractODESystem, args...; kwargs...) BVProblem{true, SciMLBase.AutoSpecialize}(sys, args...; kwargs...) end @@ -885,6 +917,7 @@ end function SciMLBase.BVProblem{iip, specialize}(sys::AbstractODESystem, u0map = [], tspan = get_tspan(sys), parammap = DiffEqBase.NullParameters(); + constraints = nothing, guesses = nothing, version = nothing, tgrad = false, callback = nothing, check_length = true, @@ -892,38 +925,63 @@ function SciMLBase.BVProblem{iip, specialize}(sys::AbstractODESystem, u0map = [] eval_expression = false, eval_module = @__MODULE__, kwargs...) where {iip, specialize} + if !iscomplete(sys) error("A completed system is required. Call `complete` or `structural_simplify` on the system before creating an `BVProblem`") end + !isnothing(callbacks) && error("BVP solvers do not support callbacks.") - f, u0, p = process_SciMLProblem(ODEFunction{iip, specialize}, sys, u0map, parammap; - t = tspan !== nothing ? tspan[1] : tspan, - check_length, warn_initialize_determined, eval_expression, eval_module, kwargs...) + iv = get_iv(sys) + constraintsts = nothing + constraintps = nothing + sts = unknowns(sys) + ps = parameters(sys) - cbs = process_events(sys; callback, eval_expression, eval_module, kwargs...) - kwargs = filter_kwargs(kwargs) + if !isnothing(constraints) + constraints isa Equation || + constraints isa Vector{Equation} || + error("Constraints must be specified as an equation or a vector of equations.") - kwargs1 = (;) - if cbs !== nothing - kwargs1 = merge(kwargs1, (callback = cbs,)) + (length(constraints) + length(u0map) > length(sts)) && + error("The BVProblem is overdetermined. The total number of conditions (# constraints + # fixed initial values given by u0map) cannot exceed the total number of states.") + + constraintsts = OrderedSet() + constraintps = OrderedSet() + + for eq in constraints + collect_vars!(constraintsts, constraintps, eq, iv) + validate_constraint_syms(eq, constraintsts, constraintps, Set(sts), Set(ps), iv) + empty!(constraintsts) + empty!(constraintps) + end end - # Handle algebraic equations - stidxmap = Dict([v => i for (i, v) in enumerate(unknowns(sys))]) - pidxmap = Dict([v => i for (i, v) in enumerate(parameters(sys))]) - ns = length(stmap) - ne = length(get_alg_eqs(sys)) + f, u0, p = process_SciMLProblem(ODEFunction{iip, specialize}, sys, u0map, parammap; + t = tspan !== nothing ? tspan[1] : tspan, guesses, + check_length, warn_initialize_determined, eval_expression, eval_module, kwargs...) + + stidxmap = Dict([v => i for (i, v) in enumerate(sts)]) + pidxmap = Dict([v => i for (i, v) in enumerate(ps)]) + + # Indices of states that have initial constraints. + u0i = has_alg_eqs(sys) ? collect(1:length(sts)) : [stidxmap[k] for k in keys(u0map)] + ni = length(u0i) - # Define the boundary conditions. - bc = if has_alg_eqs(sys) + bc = if !isnothing(constraints) + ne = length(constraints) if iip (residual,u,p,t) -> begin - residual[1:ns] .= u[1] .- u0 - residual[ns+1:ns+ne] .= sub_u_p_into_symeq.(get_alg_eqs(sys)) + residual[1:ni] .= u[1][u0i] .- u0[u0i] + residual[ni+1:ni+ne] .= map(constraints) do cons + sub_u_p_into_symeq(cons.rhs - cons.lhs, u, p, stidxmap, pidxmap, iv, tspan) + end end else (u,p,t) -> begin - resid = vcat(u[1] - u0, sub_u_p_into_symeq.(get_alg_eqs(sys))) + consresid = map(constraints) do cons + sub_u_p_into_symeq(cons.rhs-cons.lhs, u, p, stidxmap, pidxmap, iv, tspan) + end + resid = vcat(u[1][u0i] - u0[u0i], consresid) end end else @@ -941,32 +999,54 @@ end get_callback(prob::BVProblem) = error("BVP solvers do not support callbacks.") -# Helper to create the dictionary that will substitute numeric values for u, p into the algebraic equations in the ODESystem. Used to construct the boundary condition function. +# Validate that all the variables in the BVP constraints are well-formed states or parameters. +function validate_constraint_syms(eq, constraintsts, constraintps, sts, ps, iv) + ModelingToolkit.check_variables(constraintsts) + ModelingToolkit.check_parameters(constraintps) + + for var in constraintsts + if arguments(var) == iv + var ∈ sts || error("Constraint equation $eq contains a variable $var that is not a variable of the ODESystem.") + error("Constraint equation $eq contains a variable $var that does not have a specified argument. Such equations should be specified as algebraic equations to the ODESystem rather than a boundary constraints.") + else + operation(var)(iv) ∈ sts || error("Constraint equation $eq contains a variable $(operation(var)) that is not a variable of the ODESystem.") + end + end + + for var in constraintps + if !iscall(var) + var ∈ ps || error("Constraint equation $eq contains a parameter $var that is not a parameter of the ODESystem.") + else + operation(var) ∈ ps || error("Constraint equations contain a parameter $var that is not a parameter of the ODESystem.") + end + end +end + +# Helper to substitute numeric values for u, p into the algebraic equations in the ODESystem. Used to construct the boundary condition function. # Take a system with variables x,y, parameters g # -# 1 + x + y → 1 + u[1][1] + u[1][2] +# 1 + x(0) + y(0) → 1 + u[1][1] + u[1][2] # x(0.5) → u(0.5)[1] # x(0.5)*g(0.5) → u(0.5)[1]*p[1] - -function sub_u_p_into_symeq(eq, u, p, stidxmap, pidxmap) - iv = ModelingToolkit.get_iv(sys) +function sub_u_p_into_symeq(eq, u, p, stidxmap, pidxmap, iv, tspan) eq = Symbolics.unwrap(eq) - stmap = Dict([st => u[1][i] for st => i in stidxmap]) - pmap = Dict([pa => p[i] for pa => i in pidxmap]) + stmap = Dict([st => u[1][i] for (st, i) in stidxmap]) + pmap = Dict([pa => p[i] for (pa, i) in pidxmap]) eq = Symbolics.substitute(eq, merge(stmap, pmap)) csyms = [] # Find most nested calls, substitute those first. while !isempty(find_callable_syms!(csyms, eq)) for sym in csyms - t = arguments(sym)[1] x = operation(sym) + t = arguments(sym)[1] + prog = (tspan[2] - tspan[1])/(t - tspan[1]) # 1 / the % of the timespan elapsed if isparameter(x) eq = Symbolics.substitute(eq, Dict(x(t) => p[pidxmap[x(iv)]])) elseif isvariable(x) - eq = Symbolics.substitute(eq, Dict(x(t) => u(val)[stidxmap[x(iv)]])) + eq = Symbolics.substitute(eq, Dict(x(t) => u[Int(end ÷ prog)][stidxmap[x(iv)]])) end end empty!(csyms) diff --git a/test/bvproblem.jl b/test/bvproblem.jl index 2d5535325a..6432c5ae02 100644 --- a/test/bvproblem.jl +++ b/test/bvproblem.jl @@ -1,80 +1,85 @@ -using BoundaryValueDiffEq, OrdinaryDiffEq +using BoundaryValueDiffEq, OrdinaryDiffEq, BoundaryValueDiffEqAscher using ModelingToolkit using ModelingToolkit: t_nounits as t, D_nounits as D ### Test Collocation solvers on simple problems solvers = [MIRK4, RadauIIa5, LobattoIIIa3] +daesolvers = [Ascher2, Ascher4, Ascher6] -@parameters α=7.5 β=4.0 γ=8.0 δ=5.0 -@variables x(t)=1.0 y(t)=2.0 - -eqs = [D(x) ~ α * x - β * x * y, - D(y) ~ -γ * y + δ * x * y] - -u0map = [x => 1.0, y => 2.0] -parammap = [α => 7.5, β => 4, γ => 8.0, δ => 5.0] -tspan = (0.0, 10.0) - -@mtkbuild lotkavolterra = ODESystem(eqs, t) -op = ODEProblem(lotkavolterra, u0map, tspan, parammap) -osol = solve(op, Vern9()) - -bvp = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}( - lotkavolterra, u0map, tspan, parammap; eval_expression = true) - -for solver in solvers - sol = solve(bvp, solver(), dt = 0.01) - @test isapprox(sol.u[end], osol.u[end]; atol = 0.01) - @test sol.u[1] == [1.0, 2.0] -end - -# Test out of place -bvp2 = SciMLBase.BVProblem{false, SciMLBase.AutoSpecialize}( - lotkavolterra, u0map, tspan, parammap; eval_expression = true) - -for solver in solvers - sol = solve(bvp2, solver(), dt = 0.01) - @test isapprox(sol.u[end], osol.u[end]; atol = 0.01) - @test sol.u[1] == [1.0, 2.0] +let + @parameters α=7.5 β=4.0 γ=8.0 δ=5.0 + @variables x(t)=1.0 y(t)=2.0 + + eqs = [D(x) ~ α * x - β * x * y, + D(y) ~ -γ * y + δ * x * y] + + u0map = [x => 1.0, y => 2.0] + parammap = [α => 7.5, β => 4, γ => 8.0, δ => 5.0] + tspan = (0.0, 10.0) + + @mtkbuild lotkavolterra = ODESystem(eqs, t) + op = ODEProblem(lotkavolterra, u0map, tspan, parammap) + osol = solve(op, Vern9()) + + bvp = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}( + lotkavolterra, u0map, tspan, parammap; eval_expression = true) + + for solver in solvers + sol = solve(bvp, solver(), dt = 0.01) + @test isapprox(sol.u[end], osol.u[end]; atol = 0.01) + @test sol.u[1] == [1.0, 2.0] + end + + # Test out of place + bvp2 = SciMLBase.BVProblem{false, SciMLBase.AutoSpecialize}( + lotkavolterra, u0map, tspan, parammap; eval_expression = true) + + for solver in solvers + sol = solve(bvp2, solver(), dt = 0.01) + @test isapprox(sol.u[end], osol.u[end]; atol = 0.01) + @test sol.u[1] == [1.0, 2.0] + end end ### Testing on pendulum - -@parameters g=9.81 L=1.0 -@variables θ(t) = π / 2 - -eqs = [D(D(θ)) ~ -(g / L) * sin(θ)] - -@mtkbuild pend = ODESystem(eqs, t) - -u0map = [θ => π / 2, D(θ) => π / 2] -parammap = [:L => 1.0, :g => 9.81] -tspan = (0.0, 6.0) - -op = ODEProblem(pend, u0map, tspan, parammap) -osol = solve(op, Vern9()) - -bvp = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}(pend, u0map, tspan, parammap) -for solver in solvers - sol = solve(bvp, solver(), dt = 0.01) - @test isapprox(sol.u[end], osol.u[end]; atol = 0.01) - @test sol.u[1] == [π / 2, π / 2] -end - -# Test out-of-place -bvp2 = SciMLBase.BVProblem{false, SciMLBase.FullSpecialize}(pend, u0map, tspan, parammap) - -for solver in solvers - sol = solve(bvp2, solver(), dt = 0.01) - @test isapprox(sol.u[end], osol.u[end]; atol = 0.01) - @test sol.u[1] == [π / 2, π / 2] +let + @parameters g=9.81 L=1.0 + @variables θ(t) = π / 2 + + eqs = [D(D(θ)) ~ -(g / L) * sin(θ)] + + @mtkbuild pend = ODESystem(eqs, t) + + u0map = [θ => π / 2, D(θ) => π / 2] + parammap = [:L => 1.0, :g => 9.81] + tspan = (0.0, 6.0) + + op = ODEProblem(pend, u0map, tspan, parammap) + osol = solve(op, Vern9()) + + bvp = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}(pend, u0map, tspan, parammap) + for solver in solvers + sol = solve(bvp, solver(), dt = 0.01) + @test isapprox(sol.u[end], osol.u[end]; atol = 0.01) + @test sol.u[1] == [π / 2, π / 2] + end + + # Test out-of-place + bvp2 = SciMLBase.BVProblem{false, SciMLBase.FullSpecialize}(pend, u0map, tspan, parammap) + + for solver in solvers + sol = solve(bvp2, solver(), dt = 0.01) + @test isapprox(sol.u[end], osol.u[end]; atol = 0.01) + @test sol.u[1] == [π / 2, π / 2] + end end ################################################### -### TESTING ODESystem with Constraint Equations ### +### ODESystem with Constraint Equations, DAEs with constraints ### ################################################### -# Cartesian pendulum from the docs. Testing that initialization is satisfied. +# Cartesian pendulum from the docs. +# DAE IVP solved using BoundaryValueDiffEq solvers. let @parameters g @variables x(t) y(t) [state_priority = 10] λ(t) @@ -109,14 +114,74 @@ let end end -# Adding a midpoint boundary condition. +function test_solvers(solvers, prob, u0map, constraints, equations = []; dt = 0.01) + for solver in solvers + sol = solve(bvp, solver(); dt) + + for (k, v) in u0map + @test sol[k][1] == v + end + + for cons in constraints + @test sol[cons.rhs - cons.lhs] ≈ 0 + end + + for eq in equations + @test sol[eq] ≈ 0 + end + end +end + +# Simple ODESystem with BVP constraints. +let + @parameters α=7.5 β=4.0 γ=8.0 δ=5.0 + @variables x(..) y(t) + + eqs = [D(x) ~ α * x - β * x * y, + D(y) ~ -γ * y + δ * x * y] + + u0map = [y => 2.0] + parammap = [α => 7.5, β => 4, γ => 8.0, δ => 5.0] + tspan = (0.0, 10.0) + guesses = [x => 1.0] + + @mtkbuild lotkavolterra = ODESystem(eqs, t) + op = ODEProblem(lotkavolterra, u0map, tspan, parammap, guesses = guesses) + + constraints = [x(6.) ~ 3] + bvp = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}(pend, u0map, tspan, parammap; guesses, constraints) + test_solvers(solvers, bvp, u0map, constraints) + + # Testing that more complicated constraints give correct solutions. + constraints = [y(2.) + x(8.) ~ 12] + bvp = SciMLBase.BVProblem{false, SciMLBase.FullSpecialize}(pend, u0map, tspan, parammap; guesses, constraints) + test_solvers(solvers, bvp, u0map, constraints) + + constraints = [α * β - x(6.) ~ 24] + bvp = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}(pend, u0map, tspan, parammap; guesses, constraints) + test_solvers(solvers, bvp, u0map, constraints) + + # Testing that errors are properly thrown when malformed constraints are given. + @variables bad(..) + constraints = [x(1.) + bad(3.) ~ 10] + @test_throws Exception bvp = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}(pend, u0map, tspan, parammap; guesses, constraints) + + constraints = [x(t) + y(t) ~ 3] + @test_throws Exception bvp = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}(pend, u0map, tspan, parammap; guesses, constraints) + + @parameters bad2 + constraints = [bad2 + x(0.) ~ 3] + @test_throws Exception bvp = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}(pend, u0map, tspan, parammap; guesses, constraints) +end + +# Adding a midpoint boundary constraint. +# Solve using BVDAE solvers. let @parameters g @variables x(..) y(t) [state_priority = 10] λ(t) eqs = [D(D(x(t))) ~ λ * x(t) D(D(y)) ~ λ * y - g - x(t)^2 + y^2 ~ 1 - x(0.5) ~ 1] + x(t)^2 + y^2 ~ 1] @mtkbuild pend = ODESystem(eqs, t) tspan = (0.0, 1.5) @@ -124,37 +189,24 @@ let parammap = [g => 1] guesses = [λ => 1] - prob = ODEProblem(pend, u0map, tspan, pmap; guesses, check_length = false) - sol = solve(prob, Rodas5P()) + constraints = [x(0.5) ~ 1] + bvp = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}(pend, u0map, tspan, parammap; constraints, guesses, check_length = false) + test_solvers(daesolvers, bvp, u0map, constraints) - bvp = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}(pend, u0map, tspan, parammap; guesses = guesses, check_length = false) - - for solver in solvers - sol = solve(bvp, solver(), dt = 0.01) - @test isapprox(sol.u[end], osol.u[end]; atol = 0.01) - conditions = getfield.(equations(pend)[3:end], :rhs) - @test [sol[conditions][1]; sol[x][1] - 1; sol[y][1]] ≈ 0 - @test sol.u[1] == [π / 2, π / 2] - end + bvp2 = SciMLBase.BVProblem{false, SciMLBase.FullSpecialize}(pend, u0map, tspan, parammap) + test_solvers(daesolvers, bvp2, u0map, constraints, get_alg_eqs(pend)) - bvp = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}(pend, u0map, tspan, parammap; guesses) - - for solver in solvers - sol = solve(bvp, solver(), dt = 0.01) - @test isapprox(sol.u[end], osol.u[end]; atol = 0.01) - conditions = getfield.(equations(pend)[3:end], :rhs) - @test [sol[conditions][1]; sol[x][1] - 1; sol[y][1]] ≈ 0 - end + # More complicated constraints. + u0map = [x(t) => 0.6] + guesses = [λ => 1, y(t) => 0.8] - bvp2 = SciMLBase.BVProblem{false, SciMLBase.FullSpecialize}(pend, u0map, tspan, parammap) - for solver in solvers - sol = solve(bvp, solver(), dt = 0.01) - @test isapprox(sol.u[end], osol.u[end]; atol = 0.01) - conditions = getfield.(equations(pend)[3:end], :rhs) - @test [sol[conditions][1]; sol[x][1] - 1; sol[y][1]] ≈ 0 - end -end + constraints = [x(0.5) ~ 1, + x(0.3)^3 + y(0.6)^2 ~ 0.5] + bvp = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}(pend, u0map, tspan, parammap; constraints, guesses, check_length = false) + test_solvers(daesolvers, bvp, u0map, constraints, get_alg_eqs(pend)) -# Testing a more complicated case with multiple constraints. -let + constraints = [x(0.4) * g ~ y(0.2), + y(0.7) ~ 0.3] + bvp = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}(pend, u0map, tspan, parammap; constraints, guesses, check_length = false) + test_solvers(daesolvers, bvp, u0map, constraints, get_alg_eqs(pend)) end From 5d082ab05dc674b005ef1a724aa7273a712c2b66 Mon Sep 17 00:00:00 2001 From: vyudu Date: Sat, 11 Jan 2025 16:20:55 -0500 Subject: [PATCH 0559/2176] up --- src/systems/diffeqs/abstractodesystem.jl | 8 +++---- test/bvproblem.jl | 27 ++++++++++++------------ 2 files changed, 18 insertions(+), 17 deletions(-) diff --git a/src/systems/diffeqs/abstractodesystem.jl b/src/systems/diffeqs/abstractodesystem.jl index 3c45b1b2f8..99b34ec1a8 100644 --- a/src/systems/diffeqs/abstractodesystem.jl +++ b/src/systems/diffeqs/abstractodesystem.jl @@ -917,7 +917,7 @@ end function SciMLBase.BVProblem{iip, specialize}(sys::AbstractODESystem, u0map = [], tspan = get_tspan(sys), parammap = DiffEqBase.NullParameters(); - constraints = nothing, guesses = nothing, + constraints = nothing, guesses = Dict(), version = nothing, tgrad = false, callback = nothing, check_length = true, @@ -929,7 +929,7 @@ function SciMLBase.BVProblem{iip, specialize}(sys::AbstractODESystem, u0map = [] if !iscomplete(sys) error("A completed system is required. Call `complete` or `structural_simplify` on the system before creating an `BVProblem`") end - !isnothing(callbacks) && error("BVP solvers do not support callbacks.") + !isnothing(callback) && error("BVP solvers do not support callbacks.") iv = get_iv(sys) constraintsts = nothing @@ -964,7 +964,7 @@ function SciMLBase.BVProblem{iip, specialize}(sys::AbstractODESystem, u0map = [] pidxmap = Dict([v => i for (i, v) in enumerate(ps)]) # Indices of states that have initial constraints. - u0i = has_alg_eqs(sys) ? collect(1:length(sts)) : [stidxmap[k] for k in keys(u0map)] + u0i = has_alg_eqs(sys) ? collect(1:length(sts)) : [stidxmap[k] for (k,v) in u0map] ni = length(u0i) bc = if !isnothing(constraints) @@ -994,7 +994,7 @@ function SciMLBase.BVProblem{iip, specialize}(sys::AbstractODESystem, u0map = [] end end - return BVProblem{iip}(f, bc, u0, tspan, p; kwargs1..., kwargs...) + return BVProblem{iip}(f, bc, u0, tspan, p; kwargs...) end get_callback(prob::BVProblem) = error("BVP solvers do not support callbacks.") diff --git a/test/bvproblem.jl b/test/bvproblem.jl index 6432c5ae02..e864433f3c 100644 --- a/test/bvproblem.jl +++ b/test/bvproblem.jl @@ -44,13 +44,14 @@ end ### Testing on pendulum let @parameters g=9.81 L=1.0 - @variables θ(t) = π / 2 + @variables θ(t) = π / 2 θ_t(t) - eqs = [D(D(θ)) ~ -(g / L) * sin(θ)] + eqs = [D(θ) ~ θ_t + D(θ_t) ~ -(g / L) * sin(θ)] @mtkbuild pend = ODESystem(eqs, t) - u0map = [θ => π / 2, D(θ) => π / 2] + u0map = [θ => π / 2, θ_t => π / 2] parammap = [:L => 1.0, :g => 9.81] tspan = (0.0, 6.0) @@ -74,9 +75,9 @@ let end end -################################################### -### ODESystem with Constraint Equations, DAEs with constraints ### -################################################### +################################################################## +### ODESystem with constraint equations, DAEs with constraints ### +################################################################## # Cartesian pendulum from the docs. # DAE IVP solved using BoundaryValueDiffEq solvers. @@ -90,19 +91,19 @@ let tspan = (0.0, 1.5) u0map = [x => 1, y => 0] - parammap = [g => 1] - guesses = [λ => 1] + pmap = [g => 1] + guess = [λ => 1] - prob = ODEProblem(pend, u0map, tspan, pmap; guesses) - sol = solve(prob, Rodas5P()) + prob = ODEProblem(pend, u0map, tspan, pmap; guesses = guess) + osol = solve(prob, Rodas5P()) - bvp = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}(pend, u0map, tspan, parammap; guesses) + bvp = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}(pend, u0map, tspan, parammap; guesses = guess) for solver in solvers - sol = solve(bvp, solver(), dt = 0.01) + sol = solve(bvp, solver(), dt = 0.001) @test isapprox(sol.u[end], osol.u[end]; atol = 0.01) conditions = getfield.(equations(pend)[3:end], :rhs) - @test [sol[conditions][1]; sol[x][1] - 1; sol[y][1]] ≈ 0 + @test isapprox([sol[conditions][1]; sol[x][1] - 1; sol[y][1]], zeros(5), atol = 0.001) end bvp2 = SciMLBase.BVProblem{false, SciMLBase.FullSpecialize}(pend, u0map, tspan, parammap) From 31afd3ce4a0f86b14ef1d8f2633d381ed1dfba6a Mon Sep 17 00:00:00 2001 From: sivasathyaseeelan Date: Sun, 12 Jan 2025 17:53:22 +0530 Subject: [PATCH 0560/2176] added tests for SDEFunctionExpr Signed-off-by: sivasathyaseeelan --- test/sdesystem.jl | 52 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/test/sdesystem.jl b/test/sdesystem.jl index 8069581dcc..d25f9b3111 100644 --- a/test/sdesystem.jl +++ b/test/sdesystem.jl @@ -868,3 +868,55 @@ end @test length(ModelingToolkit.get_noiseeqs(sys)) == 1 @test length(observed(sys)) == 1 end + +@testset "SDEFunctionExpr" begin + @parameters σ ρ β + @variables x(tt) y(tt) z(tt) + + eqs = [D(x) ~ σ * (y - x), + D(y) ~ x * (ρ - z) - y, + D(z) ~ x * y - β * z] + + noiseeqs = [0.1 * x, + 0.1 * y, + 0.1 * z] + + @named sys = ODESystem(eqs, tt, [x, y, z], [σ, ρ, β]) + + @named de = SDESystem(eqs, noiseeqs, tt, [x, y, z], [σ, ρ, β], tspan = (0.0, 10.0)) + de = complete(de) + + f = SDEFunctionExpr(de) + @test f isa Expr + + @testset "Configuration Tests" begin + # Test with `tgrad` + f_tgrad = SDEFunctionExpr(de; tgrad = true) + @test f_tgrad isa Expr + + # Test with `jac` + f_jac = SDEFunctionExpr(de; jac = true) + @test f_jac isa Expr + + # Test with sparse Jacobian + f_sparse = SDEFunctionExpr(de; sparse = true) + @test f_sparse isa Expr + end + + @testset "Mass Matrix Handling" begin + # Test with default mass matrix + @test eval(SDEFunctionExpr(de).args[7]) == UniformScaling{Bool}(true) + + # Test with non-trivial mass matrix + u0 = [1.0, 2.0, 3.0] + f_mass = SDEFunctionExpr(de, u0 = u0) + @test f_mass isa Expr + end + + @testset "Ordering Tests" begin + dvs = [z, y, x] + ps = [β, ρ, σ] + f_order = SDEFunctionExpr(de, dvs, ps) + @test f_order isa Expr + end +end \ No newline at end of file From 0a4fca7025004218018481818fc29c9e17808251 Mon Sep 17 00:00:00 2001 From: vyudu Date: Mon, 13 Jan 2025 11:21:15 -0500 Subject: [PATCH 0561/2176] add error --- src/systems/diffeqs/sdesystem.jl | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/systems/diffeqs/sdesystem.jl b/src/systems/diffeqs/sdesystem.jl index 366e8a6d09..95b5a4cc59 100644 --- a/src/systems/diffeqs/sdesystem.jl +++ b/src/systems/diffeqs/sdesystem.jl @@ -737,6 +737,7 @@ function DiffEqBase.SDEProblem{iip, specialize}( if !iscomplete(sys) error("A completed `SDESystem` is required. Call `complete` or `structural_simplify` on the system before creating an `SDEProblem`") end + f, u0, p = process_SciMLProblem( SDEFunction{iip, specialize}, sys, u0map, parammap; check_length, t = tspan === nothing ? nothing : tspan[1], kwargs...) @@ -767,6 +768,19 @@ function DiffEqBase.SDEProblem{iip, specialize}( noise_rate_prototype = noise_rate_prototype, kwargs...) end +function DiffEqBase.SDEProblem{iip, specialize}( + sys::ODESystem, u0map = [], tspan = get_tspan(sys), + parammap = DiffEqBase.NullParameters(); + sparsenoise = nothing, check_length = true, + callback = nothing, kwargs...) where {iip, specialize} + + if any(ModelingToolkit.isbrownian, unknowns(sys)) + error("SDESystem constructed by defining Brownian variables with @brownian must be simplified by calling `structural_simplify` before a SDEProblem can be constructed.") + else + error("Cannot construct SDEProblem from an ODESystem.") + end +end + """ ```julia DiffEqBase.SDEProblem{iip}(sys::SDESystem, u0map, tspan, p = parammap; From 9eee7fd0c895612558b997be37e985f61e3f0f6c Mon Sep 17 00:00:00 2001 From: vyudu Date: Mon, 13 Jan 2025 11:26:56 -0500 Subject: [PATCH 0562/2176] up --- src/systems/diffeqs/sdesystem.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/systems/diffeqs/sdesystem.jl b/src/systems/diffeqs/sdesystem.jl index 95b5a4cc59..26f26b4263 100644 --- a/src/systems/diffeqs/sdesystem.jl +++ b/src/systems/diffeqs/sdesystem.jl @@ -777,7 +777,7 @@ function DiffEqBase.SDEProblem{iip, specialize}( if any(ModelingToolkit.isbrownian, unknowns(sys)) error("SDESystem constructed by defining Brownian variables with @brownian must be simplified by calling `structural_simplify` before a SDEProblem can be constructed.") else - error("Cannot construct SDEProblem from an ODESystem.") + error("Cannot construct SDEProblem from a normal ODESystem.") end end From 65b9912a470f298cd92f542a36c4835134e72d92 Mon Sep 17 00:00:00 2001 From: vyudu Date: Mon, 13 Jan 2025 12:06:02 -0500 Subject: [PATCH 0563/2176] init --- src/systems/diffeqs/odesystem.jl | 2 ++ src/systems/diffeqs/sdesystem.jl | 6 ++++-- test/odesystem.jl | 26 ++++++++++++++++++++++++++ test/sdesystem.jl | 26 ++++++++++++++++++++++++++ 4 files changed, 58 insertions(+), 2 deletions(-) diff --git a/src/systems/diffeqs/odesystem.jl b/src/systems/diffeqs/odesystem.jl index 61f16fd926..aafd1c254a 100644 --- a/src/systems/diffeqs/odesystem.jl +++ b/src/systems/diffeqs/odesystem.jl @@ -373,6 +373,8 @@ function Base.:(==)(sys1::ODESystem, sys2::ODESystem) _eq_unordered(get_eqs(sys1), get_eqs(sys2)) && _eq_unordered(get_unknowns(sys1), get_unknowns(sys2)) && _eq_unordered(get_ps(sys1), get_ps(sys2)) && + _eq_unordered(continuous_events(sys1), continuous_events(sys2)) && + _eq_unordered(discrete_events(sys1), discrete_events(sys2)) && all(s1 == s2 for (s1, s2) in zip(get_systems(sys1), get_systems(sys2))) end diff --git a/src/systems/diffeqs/sdesystem.jl b/src/systems/diffeqs/sdesystem.jl index 366e8a6d09..5e25941333 100644 --- a/src/systems/diffeqs/sdesystem.jl +++ b/src/systems/diffeqs/sdesystem.jl @@ -279,11 +279,13 @@ function Base.:(==)(sys1::SDESystem, sys2::SDESystem) iv2 = get_iv(sys2) isequal(iv1, iv2) && isequal(nameof(sys1), nameof(sys2)) && - isequal(get_eqs(sys1), get_eqs(sys2)) && - isequal(get_noiseeqs(sys1), get_noiseeqs(sys2)) && + _eq_unordered(get_eqs(sys1), get_eqs(sys2)) && + _eq_unordered(get_noiseeqs(sys1), get_noiseeqs(sys2)) && isequal(get_is_scalar_noise(sys1), get_is_scalar_noise(sys2)) && _eq_unordered(get_unknowns(sys1), get_unknowns(sys2)) && _eq_unordered(get_ps(sys1), get_ps(sys2)) && + _eq_unordered(continuous_events(sys1), continuous_events(sys2)) && + _eq_unordered(discrete_events(sys1), discrete_events(sys2)) && all(s1 == s2 for (s1, s2) in zip(get_systems(sys1), get_systems(sys2))) end diff --git a/test/odesystem.jl b/test/odesystem.jl index 85d135b338..e230c5e9a3 100644 --- a/test/odesystem.jl +++ b/test/odesystem.jl @@ -1552,3 +1552,29 @@ end expected_tstops = unique!(sort!(vcat(0.0:0.075:10.0, 0.1, 0.2, 0.65, 0.35, 0.45))) @test all(x -> any(isapprox(x, atol = 1e-6), sol2.t), expected_tstops) end + +# Test `isequal` +let + @variables X(t) + @parameters p d + eq = D(X) ~ p - d*X + + osys1 = complete(ODESystem([eq], t; name = :osys)) + osys2 = complete(ODESystem([eq], t; name = :osys)) + @test osys1 == osys2 # true + + continuous_events = [[X ~ 1.0] => [X ~ X + 5.0]] + discrete_events = [5.0 => [d ~ d / 2.0]] + + osys1 = complete(ODESystem([eq], t; name = :osys, continuous_events)) + osys2 = complete(ODESystem([eq], t; name = :osys)) + @test osys1 !== osys2 + + osys1 = complete(ODESystem([eq], t; name = :osys, discrete_events)) + osys2 = complete(ODESystem([eq], t; name = :osys)) + @test osys1 !== osys2 + + osys1 = complete(ODESystem([eq], t; name = :osys, continuous_events)) + osys2 = complete(ODESystem([eq], t; name = :osys, discrete_events)) + @test osys1 !== osys2 +end diff --git a/test/sdesystem.jl b/test/sdesystem.jl index 8069581dcc..7ff7d5e7c8 100644 --- a/test/sdesystem.jl +++ b/test/sdesystem.jl @@ -868,3 +868,29 @@ end @test length(ModelingToolkit.get_noiseeqs(sys)) == 1 @test length(observed(sys)) == 1 end + +# Test `isequal` handles events +let + @variables X(t) + @parameters p d + @brownian a + seq = D(X) ~ p - d*X + a + @mtkbuild ssys1 = System([eq], t; name = :ssys) + @mtkbuild ssys2 = System([eq], t; name = :ssys) + @test ssys1 == ssys2 # true + + continuous_events = [[X ~ 1.0] => [X ~ X + 5.0]] + discrete_events = [5.0 => [d ~ d / 2.0]] + + @mtkbuild ssys1 = System([eq], t; name = :ssys, continuous_events) + @mtkbuild ssys2 = System([eq], t; name = :ssys) + @test ssys1 !== ssys2 + + @mtkbuild ssys1 = System([eq], t; name = :ssys, discrete_events) + @mtkbuild ssys2 = System([eq], t; name = :ssys) + @test ssys1 !== ssys2 + + @mtkbuild ssys1 = System([eq], t; name = :ssys, continuous_events) + @mtkbuild ssys2 = System([eq], t; name = :ssys, discrete_events) + @test ssys1 !== ssys2 +end From fe704edc86b39d5e72df3091140b22f6b5bca38d Mon Sep 17 00:00:00 2001 From: sivasathyaseeelan Date: Tue, 14 Jan 2025 03:24:34 +0530 Subject: [PATCH 0564/2176] modified test Signed-off-by: sivasathyaseeelan --- test/sdesystem.jl | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/test/sdesystem.jl b/test/sdesystem.jl index d25f9b3111..1593458b63 100644 --- a/test/sdesystem.jl +++ b/test/sdesystem.jl @@ -903,16 +903,6 @@ end @test f_sparse isa Expr end - @testset "Mass Matrix Handling" begin - # Test with default mass matrix - @test eval(SDEFunctionExpr(de).args[7]) == UniformScaling{Bool}(true) - - # Test with non-trivial mass matrix - u0 = [1.0, 2.0, 3.0] - f_mass = SDEFunctionExpr(de, u0 = u0) - @test f_mass isa Expr - end - @testset "Ordering Tests" begin dvs = [z, y, x] ps = [β, ρ, σ] From b83e003babe1347439d05ea87a8b90995f1dcc82 Mon Sep 17 00:00:00 2001 From: vyudu Date: Tue, 14 Jan 2025 00:59:31 -0500 Subject: [PATCH 0565/2176] refactor the bc creation function --- src/systems/diffeqs/abstractodesystem.jl | 189 ++++++++++--------- test/bvproblem.jl | 230 ++++++++++++++++------- 2 files changed, 264 insertions(+), 155 deletions(-) diff --git a/src/systems/diffeqs/abstractodesystem.jl b/src/systems/diffeqs/abstractodesystem.jl index 99b34ec1a8..25347a17cd 100644 --- a/src/systems/diffeqs/abstractodesystem.jl +++ b/src/systems/diffeqs/abstractodesystem.jl @@ -931,68 +931,60 @@ function SciMLBase.BVProblem{iip, specialize}(sys::AbstractODESystem, u0map = [] end !isnothing(callback) && error("BVP solvers do not support callbacks.") - iv = get_iv(sys) + has_alg_eqs(sys) && error("The BVProblem currently does not support ODESystems with algebraic equations.") # Remove this when the BVDAE solvers get updated, the codegen should work when it does. + constraintsts = nothing constraintps = nothing sts = unknowns(sys) ps = parameters(sys) - if !isnothing(constraints) + # Constraint validation + f_cons = if !isnothing(constraints) constraints isa Equation || constraints isa Vector{Equation} || error("Constraints must be specified as an equation or a vector of equations.") (length(constraints) + length(u0map) > length(sts)) && - error("The BVProblem is overdetermined. The total number of conditions (# constraints + # fixed initial values given by u0map) cannot exceed the total number of states.") - - constraintsts = OrderedSet() - constraintps = OrderedSet() - - for eq in constraints - collect_vars!(constraintsts, constraintps, eq, iv) - validate_constraint_syms(eq, constraintsts, constraintps, Set(sts), Set(ps), iv) - empty!(constraintsts) - empty!(constraintps) - end + error("The BVProblem is overdetermined. The total number of conditions (# constraints + # fixed initial values given by u0map) cannot exceed the total number of states.") end - f, u0, p = process_SciMLProblem(ODEFunction{iip, specialize}, sys, u0map, parammap; + # ODESystems without algebraic equations should use both fixed values + guesses + # for initialization. + _u0map = has_alg_eqs(sys) ? u0map : merge(Dict(u0map), Dict(guesses)) + f, u0, p = process_SciMLProblem(ODEFunction{iip, specialize}, sys, _u0map, parammap; t = tspan !== nothing ? tspan[1] : tspan, guesses, check_length, warn_initialize_determined, eval_expression, eval_module, kwargs...) stidxmap = Dict([v => i for (i, v) in enumerate(sts)]) - pidxmap = Dict([v => i for (i, v) in enumerate(ps)]) - - # Indices of states that have initial constraints. - u0i = has_alg_eqs(sys) ? collect(1:length(sts)) : [stidxmap[k] for (k,v) in u0map] - ni = length(u0i) - - bc = if !isnothing(constraints) - ne = length(constraints) - if iip - (residual,u,p,t) -> begin - residual[1:ni] .= u[1][u0i] .- u0[u0i] - residual[ni+1:ni+ne] .= map(constraints) do cons - sub_u_p_into_symeq(cons.rhs - cons.lhs, u, p, stidxmap, pidxmap, iv, tspan) - end - end - else - (u,p,t) -> begin - consresid = map(constraints) do cons - sub_u_p_into_symeq(cons.rhs-cons.lhs, u, p, stidxmap, pidxmap, iv, tspan) - end - resid = vcat(u[1][u0i] - u0[u0i], consresid) - end - end - else - if iip - (residual,u,p,t) -> begin - residual .= u[1] .- u0 - end - else - (u,p,t) -> (u[1] - u0) - end - end + u0_idxs = has_alg_eqs(sys) ? collect(1:length(sts)) : [stidxmap[k] for (k,v) in u0map] + + # bc = if !isnothing(constraints) && iip + # (residual,u,p,t) -> begin + # println(u(0.5)) + # residual[1:ni] .= u[1][u0i] .- u0[u0i] + # for (i, cons) in enumerate(constraints) + # residual[ni+i] = eval_symbolic_residual(cons, u, p, stidxmap, pidxmap, iv, tspan) + # end + # end + + # elseif !isnothing(constraints) && !iip + # (u,p,t) -> begin + # consresid = map(constraints) do cons + # eval_symbolic_residual(cons, u, p, stidxmap, pidxmap, iv, tspan) + # end + # resid = vcat(u[1][u0i] - u0[u0i], consresid) + # end + + # elseif iip + # (residual,u,p,t) -> begin + # println(u(0.5)) + # residual .= u[1] .- u0 + # end + + # else + # (u,p,t) -> (u[1] - u0) + # end + bc = process_constraints(sys, constraints, u0, u0_idxs, tspan, iip) return BVProblem{iip}(f, bc, u0, tspan, p; kwargs...) end @@ -1001,11 +993,10 @@ get_callback(prob::BVProblem) = error("BVP solvers do not support callbacks.") # Validate that all the variables in the BVP constraints are well-formed states or parameters. function validate_constraint_syms(eq, constraintsts, constraintps, sts, ps, iv) - ModelingToolkit.check_variables(constraintsts) - ModelingToolkit.check_parameters(constraintps) - for var in constraintsts - if arguments(var) == iv + if length(arguments(var)) > 1 + error("Too many arguments for variable $var.") + elseif arguments(var) == iv var ∈ sts || error("Constraint equation $eq contains a variable $var that is not a variable of the ODESystem.") error("Constraint equation $eq contains a variable $var that does not have a specified argument. Such equations should be specified as algebraic equations to the ODESystem rather than a boundary constraints.") else @@ -1017,53 +1008,81 @@ function validate_constraint_syms(eq, constraintsts, constraintps, sts, ps, iv) if !iscall(var) var ∈ ps || error("Constraint equation $eq contains a parameter $var that is not a parameter of the ODESystem.") else + length(arguments(var)) > 1 && error("Too many arguments for parameter $var.") operation(var) ∈ ps || error("Constraint equations contain a parameter $var that is not a parameter of the ODESystem.") end end end -# Helper to substitute numeric values for u, p into the algebraic equations in the ODESystem. Used to construct the boundary condition function. -# Take a system with variables x,y, parameters g -# -# 1 + x(0) + y(0) → 1 + u[1][1] + u[1][2] -# x(0.5) → u(0.5)[1] -# x(0.5)*g(0.5) → u(0.5)[1]*p[1] -function sub_u_p_into_symeq(eq, u, p, stidxmap, pidxmap, iv, tspan) - eq = Symbolics.unwrap(eq) - - stmap = Dict([st => u[1][i] for (st, i) in stidxmap]) - pmap = Dict([pa => p[i] for (pa, i) in pidxmap]) - eq = Symbolics.substitute(eq, merge(stmap, pmap)) - - csyms = [] - # Find most nested calls, substitute those first. - while !isempty(find_callable_syms!(csyms, eq)) - for sym in csyms - x = operation(sym) - t = arguments(sym)[1] - prog = (tspan[2] - tspan[1])/(t - tspan[1]) # 1 / the % of the timespan elapsed - - if isparameter(x) - eq = Symbolics.substitute(eq, Dict(x(t) => p[pidxmap[x(iv)]])) - elseif isvariable(x) - eq = Symbolics.substitute(eq, Dict(x(t) => u[Int(end ÷ prog)][stidxmap[x(iv)]])) +""" + process_constraints(sys, constraints, u0, tspan, iip) + + Given an ODESystem with some constraints, generate the boundary condition function. +""" +function process_constraints(sys::ODESystem, constraints, u0, u0_idxs, tspan, iip) + + iv = get_iv(sys) + sts = get_unknowns(sys) + ps = get_ps(sys) + np = length(ps) + ns = length(sts) + + stidxmap = Dict([v => i for (i, v) in enumerate(sts)]) + pidxmap = Dict([v => i for (i, v) in enumerate(ps)]) + + @variables sol(..)[1:ns] p[1:np] + exprs = Any[] + + constraintsts = OrderedSet() + constraintps = OrderedSet() + + !isnothing(constraints) && for cons in constraints + collect_vars!(constraintsts, constraintps, cons, iv) + validate_constraint_syms(cons, constraintsts, constraintps, Set(sts), Set(ps), iv) + expr = cons.rhs - cons.lhs + + for st in constraintsts + x = operation(st) + t = arguments(st)[1] + idx = stidxmap[x(iv)] + + expr = Symbolics.substitute(expr, Dict(x(t) => sol(t)[idx])) + end + + for var in constraintps + if iscall(var) + x = operation(var) + t = arguments(var)[1] + idx = pidxmap[x] + + expr = Symbolics.substitute(expr, Dict(x(t) => p[idx])) + else + idx = pidxmap[var] + expr = Symbolics.substitute(expr, Dict(var => p[idx])) end end - empty!(csyms) + + empty!(constraintsts) + empty!(constraintps) + push!(exprs, expr) end - eq -end -function find_callable_syms!(csyms, ex) - ex = Symbolics.unwrap(ex) + init_cond_exprs = Any[] - if iscall(ex) - operation(ex) isa Symbolic && (arguments(ex)[1] isa Symbolic) && push!(csyms, ex) # only add leaf nodes - for arg in arguments(ex) - find_callable_syms!(csyms, arg) + for i in u0_idxs + expr = sol(tspan[1])[i] - u0[i] + push!(init_cond_exprs, expr) + end + + exprs = vcat(init_cond_exprs, exprs) + bcs = Symbolics.build_function(exprs, sol, p, expression = Val{false}) + if iip + return (resid, u, p, t) -> begin + bcs[2](resid, u, p) end + else + return (u, p, t) -> bcs[1](u, p) end - csyms end """ diff --git a/test/bvproblem.jl b/test/bvproblem.jl index e864433f3c..7e0d45b128 100644 --- a/test/bvproblem.jl +++ b/test/bvproblem.jl @@ -1,3 +1,5 @@ +### TODO: update when BoundaryValueDiffEqAscher is updated to use the normal boundary condition conventions + using BoundaryValueDiffEq, OrdinaryDiffEq, BoundaryValueDiffEqAscher using ModelingToolkit using ModelingToolkit: t_nounits as t, D_nounits as D @@ -81,51 +83,140 @@ end # Cartesian pendulum from the docs. # DAE IVP solved using BoundaryValueDiffEq solvers. +# let +# @parameters g +# @variables x(t) y(t) [state_priority = 10] λ(t) +# eqs = [D(D(x)) ~ λ * x +# D(D(y)) ~ λ * y - g +# x^2 + y^2 ~ 1] +# @mtkbuild pend = ODESystem(eqs, t) +# +# tspan = (0.0, 1.5) +# u0map = [x => 1, y => 0] +# pmap = [g => 1] +# guess = [λ => 1] +# +# prob = ODEProblem(pend, u0map, tspan, pmap; guesses = guess) +# osol = solve(prob, Rodas5P()) +# +# zeta = [0., 0., 0., 0., 0.] +# bvp = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}(pend, u0map, tspan, parammap; guesses = guess) +# +# for solver in solvers +# sol = solve(bvp, solver(zeta), dt = 0.001) +# @test isapprox(sol.u[end], osol.u[end]; atol = 0.01) +# conditions = getfield.(equations(pend)[3:end], :rhs) +# @test isapprox([sol[conditions][1]; sol[x][1] - 1; sol[y][1]], zeros(5), atol = 0.001) +# end +# +# bvp2 = SciMLBase.BVProblem{false, SciMLBase.FullSpecialize}(pend, u0map, tspan, parammap) +# for solver in solvers +# sol = solve(bvp, solver(zeta), dt = 0.01) +# @test isapprox(sol.u[end], osol.u[end]; atol = 0.01) +# conditions = getfield.(equations(pend)[3:end], :rhs) +# @test [sol[conditions][1]; sol[x][1] - 1; sol[y][1]] ≈ 0 +# end +# end + +# Test generation of boundary condition function. let - @parameters g - @variables x(t) y(t) [state_priority = 10] λ(t) - eqs = [D(D(x)) ~ λ * x - D(D(y)) ~ λ * y - g - x^2 + y^2 ~ 1] - @mtkbuild pend = ODESystem(eqs, t) - - tspan = (0.0, 1.5) - u0map = [x => 1, y => 0] - pmap = [g => 1] - guess = [λ => 1] - - prob = ODEProblem(pend, u0map, tspan, pmap; guesses = guess) - osol = solve(prob, Rodas5P()) - - bvp = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}(pend, u0map, tspan, parammap; guesses = guess) + @parameters α=7.5 β=4.0 γ=8.0 δ=5.0 + @variables x(..) y(t) + eqs = [D(x(t)) ~ α * x(t) - β * x(t) * y, + D(y) ~ -γ * y + δ * x(t) * y] - for solver in solvers - sol = solve(bvp, solver(), dt = 0.001) - @test isapprox(sol.u[end], osol.u[end]; atol = 0.01) - conditions = getfield.(equations(pend)[3:end], :rhs) - @test isapprox([sol[conditions][1]; sol[x][1] - 1; sol[y][1]], zeros(5), atol = 0.001) + tspan = (0., 10.) + @mtkbuild lksys = ODESystem(eqs, t) + + function lotkavolterra!(du, u, p, t) + du[1] = p[1]*u[1] - p[2]*u[1]*u[2] + du[2] = -p[3]*u[2] + p[4]*u[1]*u[2] end - bvp2 = SciMLBase.BVProblem{false, SciMLBase.FullSpecialize}(pend, u0map, tspan, parammap) - for solver in solvers - sol = solve(bvp, solver(), dt = 0.01) - @test isapprox(sol.u[end], osol.u[end]; atol = 0.01) - conditions = getfield.(equations(pend)[3:end], :rhs) - @test [sol[conditions][1]; sol[x][1] - 1; sol[y][1]] ≈ 0 + function lotkavolterra(u, p, t) + [p[1]*u[1] - p[2]*u[1]*u[2], -p[3]*u[2] + p[4]*u[1]*u[2]] + end + # Compare the built bc function to the actual constructed one. + function bc!(resid, u, p, t) + resid[1] = u[1][1] - 1. + resid[2] = u[1][2] - 2. + nothing + end + function bc(u, p, t) + [u[1][1] - 1., u[1][2] - 2.] + end + + constraints = nothing + u0 = [1., 2.]; p = [7.5, 4., 8., 5.] + genbc_iip = ModelingToolkit.process_constraints(lksys, constraints, u0, [1, 2], tspan, true) + genbc_oop = ModelingToolkit.process_constraints(lksys, constraints, u0, [1, 2], tspan, false) + + bvpi1 = SciMLBase.BVProblem(lotkavolterra!, bc!, [1,2], tspan, p) + bvpi2 = SciMLBase.BVProblem(lotkavolterra!, genbc_iip, [1,2], tspan, p) + + sol1 = solve(bvpi1, MIRK4(), dt = 0.01) + sol2 = solve(bvpi2, MIRK4(), dt = 0.01) + @test sol1 ≈ sol2 + + bvpo1 = BVProblem(lotkavolterra, bc, [1,2], tspan, p) + bvpo2 = BVProblem(lotkavolterra, genbc_oop, [1,2], tspan, p) + + sol1 = solve(bvpo1, MIRK4(), dt = 0.01) + sol2 = solve(bvpo2, MIRK4(), dt = 0.01) + @test sol1 ≈ sol2 + + # Test with a constraint. + constraints = [x(0.5) ~ 1.] + + function bc!(resid, u, p, t) + resid[1] = u[1][2] - 2. + resid[2] = u(0.5)[1] - 1. + end + function bc(u, p, t) + [u[1][2] - 2., u(0.5)[1] - 1.] end + + u0 = [1., 2.] + genbc_iip = ModelingToolkit.process_constraints(lksys, constraints, u0, [2], tspan, true) + genbc_oop = ModelingToolkit.process_constraints(lksys, constraints, u0, [2], tspan, false) + + bvpi1 = SciMLBase.BVProblem(lotkavolterra!, bc!, [1,2], tspan, p) + bvpi2 = SciMLBase.BVProblem(lotkavolterra!, genbc_iip, [1,2], tspan, p) + + sol1 = solve(bvpi1, MIRK4(), dt = 0.01) + sol2 = solve(bvpi2, MIRK4(), dt = 0.01) + @test sol1 ≈ sol2 # don't get true equality here, not sure why + + bvpo1 = BVProblem(lotkavolterra, bc, [1,2], tspan, p) + bvpo2 = BVProblem(lotkavolterra, genbc_oop, [1,2], tspan, p) + + sol1 = solve(bvpo1, MIRK4(), dt = 0.01) + sol2 = solve(bvpo2, MIRK4(), dt = 0.01) + @test sol1 ≈ sol2 end function test_solvers(solvers, prob, u0map, constraints, equations = []; dt = 0.01) for solver in solvers - sol = solve(bvp, solver(); dt) + sol = solve(prob, solver(); dt) + @test successful_retcode(sol.retcode) + p = prob.p; t = sol.t; bc = prob.f.bc + ns = length(prob.u0) + + if isinplace(bvp.f) + resid = zeros(ns) + bc!(resid, sol, p, t) + @test isapprox(zeros(ns), resid) + else + @test isapprox(zeros(ns), bc(sol, p, t)) + end for (k, v) in u0map @test sol[k][1] == v end - for cons in constraints - @test sol[cons.rhs - cons.lhs] ≈ 0 - end + # for cons in constraints + # @test sol[cons.rhs - cons.lhs] ≈ 0 + # end for eq in equations @test sol[eq] ≈ 0 @@ -138,19 +229,18 @@ let @parameters α=7.5 β=4.0 γ=8.0 δ=5.0 @variables x(..) y(t) - eqs = [D(x) ~ α * x - β * x * y, - D(y) ~ -γ * y + δ * x * y] + eqs = [D(x(t)) ~ α * x(t) - β * x(t) * y, + D(y) ~ -γ * y + δ * x(t) * y] u0map = [y => 2.0] parammap = [α => 7.5, β => 4, γ => 8.0, δ => 5.0] tspan = (0.0, 10.0) - guesses = [x => 1.0] + guesses = [x(t) => 1.0] @mtkbuild lotkavolterra = ODESystem(eqs, t) - op = ODEProblem(lotkavolterra, u0map, tspan, parammap, guesses = guesses) constraints = [x(6.) ~ 3] - bvp = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}(pend, u0map, tspan, parammap; guesses, constraints) + bvp = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}(lotkavolterra, u0map, tspan, parammap; guesses, constraints) test_solvers(solvers, bvp, u0map, constraints) # Testing that more complicated constraints give correct solutions. @@ -177,37 +267,37 @@ end # Adding a midpoint boundary constraint. # Solve using BVDAE solvers. -let - @parameters g - @variables x(..) y(t) [state_priority = 10] λ(t) - eqs = [D(D(x(t))) ~ λ * x(t) - D(D(y)) ~ λ * y - g - x(t)^2 + y^2 ~ 1] - @mtkbuild pend = ODESystem(eqs, t) - - tspan = (0.0, 1.5) - u0map = [x(t) => 0.6, y => 0.8] - parammap = [g => 1] - guesses = [λ => 1] - - constraints = [x(0.5) ~ 1] - bvp = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}(pend, u0map, tspan, parammap; constraints, guesses, check_length = false) - test_solvers(daesolvers, bvp, u0map, constraints) - - bvp2 = SciMLBase.BVProblem{false, SciMLBase.FullSpecialize}(pend, u0map, tspan, parammap) - test_solvers(daesolvers, bvp2, u0map, constraints, get_alg_eqs(pend)) - - # More complicated constraints. - u0map = [x(t) => 0.6] - guesses = [λ => 1, y(t) => 0.8] - - constraints = [x(0.5) ~ 1, - x(0.3)^3 + y(0.6)^2 ~ 0.5] - bvp = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}(pend, u0map, tspan, parammap; constraints, guesses, check_length = false) - test_solvers(daesolvers, bvp, u0map, constraints, get_alg_eqs(pend)) - - constraints = [x(0.4) * g ~ y(0.2), - y(0.7) ~ 0.3] - bvp = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}(pend, u0map, tspan, parammap; constraints, guesses, check_length = false) - test_solvers(daesolvers, bvp, u0map, constraints, get_alg_eqs(pend)) -end +# let +# @parameters g +# @variables x(..) y(t) [state_priority = 10] λ(t) +# eqs = [D(D(x(t))) ~ λ * x(t) +# D(D(y)) ~ λ * y - g +# x(t)^2 + y^2 ~ 1] +# @mtkbuild pend = ODESystem(eqs, t) +# +# tspan = (0.0, 1.5) +# u0map = [x(t) => 0.6, y => 0.8] +# parammap = [g => 1] +# guesses = [λ => 1] +# +# constraints = [x(0.5) ~ 1] +# bvp = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}(pend, u0map, tspan, parammap; constraints, guesses, check_length = false) +# test_solvers(daesolvers, bvp, u0map, constraints) +# +# bvp2 = SciMLBase.BVProblem{false, SciMLBase.FullSpecialize}(pend, u0map, tspan, parammap) +# test_solvers(daesolvers, bvp2, u0map, constraints, get_alg_eqs(pend)) +# +# # More complicated constraints. +# u0map = [x(t) => 0.6] +# guesses = [λ => 1, y(t) => 0.8] +# +# constraints = [x(0.5) ~ 1, +# x(0.3)^3 + y(0.6)^2 ~ 0.5] +# bvp = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}(pend, u0map, tspan, parammap; constraints, guesses, check_length = false) +# test_solvers(daesolvers, bvp, u0map, constraints, get_alg_eqs(pend)) +# +# constraints = [x(0.4) * g ~ y(0.2), +# y(0.7) ~ 0.3] +# bvp = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}(pend, u0map, tspan, parammap; constraints, guesses, check_length = false) +# test_solvers(daesolvers, bvp, u0map, constraints, get_alg_eqs(pend)) +# end From db5eb66ea29533e51aefd6ea49ea04e0257f3201 Mon Sep 17 00:00:00 2001 From: vyudu Date: Tue, 14 Jan 2025 13:46:31 -0500 Subject: [PATCH 0566/2176] up --- src/systems/diffeqs/abstractodesystem.jl | 33 +----- test/bvproblem.jl | 144 ++++++++++++----------- 2 files changed, 76 insertions(+), 101 deletions(-) diff --git a/src/systems/diffeqs/abstractodesystem.jl b/src/systems/diffeqs/abstractodesystem.jl index 25347a17cd..0e8435942c 100644 --- a/src/systems/diffeqs/abstractodesystem.jl +++ b/src/systems/diffeqs/abstractodesystem.jl @@ -939,7 +939,7 @@ function SciMLBase.BVProblem{iip, specialize}(sys::AbstractODESystem, u0map = [] ps = parameters(sys) # Constraint validation - f_cons = if !isnothing(constraints) + if !isnothing(constraints) constraints isa Equation || constraints isa Vector{Equation} || error("Constraints must be specified as an equation or a vector of equations.") @@ -958,32 +958,6 @@ function SciMLBase.BVProblem{iip, specialize}(sys::AbstractODESystem, u0map = [] stidxmap = Dict([v => i for (i, v) in enumerate(sts)]) u0_idxs = has_alg_eqs(sys) ? collect(1:length(sts)) : [stidxmap[k] for (k,v) in u0map] - # bc = if !isnothing(constraints) && iip - # (residual,u,p,t) -> begin - # println(u(0.5)) - # residual[1:ni] .= u[1][u0i] .- u0[u0i] - # for (i, cons) in enumerate(constraints) - # residual[ni+i] = eval_symbolic_residual(cons, u, p, stidxmap, pidxmap, iv, tspan) - # end - # end - - # elseif !isnothing(constraints) && !iip - # (u,p,t) -> begin - # consresid = map(constraints) do cons - # eval_symbolic_residual(cons, u, p, stidxmap, pidxmap, iv, tspan) - # end - # resid = vcat(u[1][u0i] - u0[u0i], consresid) - # end - - # elseif iip - # (residual,u,p,t) -> begin - # println(u(0.5)) - # residual .= u[1] .- u0 - # end - - # else - # (u,p,t) -> (u[1] - u0) - # end bc = process_constraints(sys, constraints, u0, u0_idxs, tspan, iip) return BVProblem{iip}(f, bc, u0, tspan, p; kwargs...) @@ -1075,11 +1049,10 @@ function process_constraints(sys::ODESystem, constraints, u0, u0_idxs, tspan, ii end exprs = vcat(init_cond_exprs, exprs) + @show exprs bcs = Symbolics.build_function(exprs, sol, p, expression = Val{false}) if iip - return (resid, u, p, t) -> begin - bcs[2](resid, u, p) - end + return (resid, u, p, t) -> bcs[2](resid, u, p) else return (u, p, t) -> bcs[1](u, p) end diff --git a/test/bvproblem.jl b/test/bvproblem.jl index 7e0d45b128..42762b8a3f 100644 --- a/test/bvproblem.jl +++ b/test/bvproblem.jl @@ -2,7 +2,9 @@ using BoundaryValueDiffEq, OrdinaryDiffEq, BoundaryValueDiffEqAscher using ModelingToolkit +using SciMLBase using ModelingToolkit: t_nounits as t, D_nounits as D +import ModelingToolkit: process_constraints ### Test Collocation solvers on simple problems solvers = [MIRK4, RadauIIa5, LobattoIIIa3] @@ -81,46 +83,9 @@ end ### ODESystem with constraint equations, DAEs with constraints ### ################################################################## -# Cartesian pendulum from the docs. -# DAE IVP solved using BoundaryValueDiffEq solvers. -# let -# @parameters g -# @variables x(t) y(t) [state_priority = 10] λ(t) -# eqs = [D(D(x)) ~ λ * x -# D(D(y)) ~ λ * y - g -# x^2 + y^2 ~ 1] -# @mtkbuild pend = ODESystem(eqs, t) -# -# tspan = (0.0, 1.5) -# u0map = [x => 1, y => 0] -# pmap = [g => 1] -# guess = [λ => 1] -# -# prob = ODEProblem(pend, u0map, tspan, pmap; guesses = guess) -# osol = solve(prob, Rodas5P()) -# -# zeta = [0., 0., 0., 0., 0.] -# bvp = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}(pend, u0map, tspan, parammap; guesses = guess) -# -# for solver in solvers -# sol = solve(bvp, solver(zeta), dt = 0.001) -# @test isapprox(sol.u[end], osol.u[end]; atol = 0.01) -# conditions = getfield.(equations(pend)[3:end], :rhs) -# @test isapprox([sol[conditions][1]; sol[x][1] - 1; sol[y][1]], zeros(5), atol = 0.001) -# end -# -# bvp2 = SciMLBase.BVProblem{false, SciMLBase.FullSpecialize}(pend, u0map, tspan, parammap) -# for solver in solvers -# sol = solve(bvp, solver(zeta), dt = 0.01) -# @test isapprox(sol.u[end], osol.u[end]; atol = 0.01) -# conditions = getfield.(equations(pend)[3:end], :rhs) -# @test [sol[conditions][1]; sol[x][1] - 1; sol[y][1]] ≈ 0 -# end -# end - # Test generation of boundary condition function. let - @parameters α=7.5 β=4.0 γ=8.0 δ=5.0 + @parameters α=1.5 β=1.0 γ=3.0 δ=1.0 @variables x(..) y(t) eqs = [D(x(t)) ~ α * x(t) - β * x(t) * y, D(y) ~ -γ * y + δ * x(t) * y] @@ -130,11 +95,11 @@ let function lotkavolterra!(du, u, p, t) du[1] = p[1]*u[1] - p[2]*u[1]*u[2] - du[2] = -p[3]*u[2] + p[4]*u[1]*u[2] + du[2] = -p[4]*u[2] + p[3]*u[1]*u[2] end function lotkavolterra(u, p, t) - [p[1]*u[1] - p[2]*u[1]*u[2], -p[3]*u[2] + p[4]*u[1]*u[2]] + [p[1]*u[1] - p[2]*u[1]*u[2], -p[4]*u[2] + p[3]*u[1]*u[2]] end # Compare the built bc function to the actual constructed one. function bc!(resid, u, p, t) @@ -146,23 +111,22 @@ let [u[1][1] - 1., u[1][2] - 2.] end - constraints = nothing - u0 = [1., 2.]; p = [7.5, 4., 8., 5.] - genbc_iip = ModelingToolkit.process_constraints(lksys, constraints, u0, [1, 2], tspan, true) - genbc_oop = ModelingToolkit.process_constraints(lksys, constraints, u0, [1, 2], tspan, false) + u0 = [1., 2.]; p = [1.5, 1., 3., 1.] + genbc_iip = ModelingToolkit.process_constraints(lksys, nothing, u0, [1, 2], tspan, true) + genbc_oop = ModelingToolkit.process_constraints(lksys, nothing, u0, [1, 2], tspan, false) - bvpi1 = SciMLBase.BVProblem(lotkavolterra!, bc!, [1,2], tspan, p) - bvpi2 = SciMLBase.BVProblem(lotkavolterra!, genbc_iip, [1,2], tspan, p) + bvpi1 = SciMLBase.BVProblem(lotkavolterra!, bc!, [1.,2.], tspan, p) + bvpi2 = SciMLBase.BVProblem(lotkavolterra!, genbc_iip, [1.,2.], tspan, p) - sol1 = solve(bvpi1, MIRK4(), dt = 0.01) - sol2 = solve(bvpi2, MIRK4(), dt = 0.01) + sol1 = solve(bvpi1, MIRK4(), dt = 0.05) + sol2 = solve(bvpi2, MIRK4(), dt = 0.05) @test sol1 ≈ sol2 bvpo1 = BVProblem(lotkavolterra, bc, [1,2], tspan, p) bvpo2 = BVProblem(lotkavolterra, genbc_oop, [1,2], tspan, p) - sol1 = solve(bvpo1, MIRK4(), dt = 0.01) - sol2 = solve(bvpo2, MIRK4(), dt = 0.01) + sol1 = solve(bvpo1, MIRK4(), dt = 0.05) + sol2 = solve(bvpo2, MIRK4(), dt = 0.05) @test sol1 ≈ sol2 # Test with a constraint. @@ -180,28 +144,28 @@ let genbc_iip = ModelingToolkit.process_constraints(lksys, constraints, u0, [2], tspan, true) genbc_oop = ModelingToolkit.process_constraints(lksys, constraints, u0, [2], tspan, false) - bvpi1 = SciMLBase.BVProblem(lotkavolterra!, bc!, [1,2], tspan, p) - bvpi2 = SciMLBase.BVProblem(lotkavolterra!, genbc_iip, [1,2], tspan, p) - - sol1 = solve(bvpi1, MIRK4(), dt = 0.01) - sol2 = solve(bvpi2, MIRK4(), dt = 0.01) + bvpi1 = SciMLBase.BVProblem(lotkavolterra!, bc!, [1.,2.], tspan, p) + bvpi2 = SciMLBase.BVProblem(lotkavolterra!, genbc_iip, [1.,2.], tspan, p) + bvpi3 = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}(lksys, u0map, tspan, parammap; guesses, constraints) + + sol1 = solve(bvpi1, MIRK4(), dt = 0.05) + sol2 = solve(bvpi2, MIRK4(), dt = 0.05) @test sol1 ≈ sol2 # don't get true equality here, not sure why bvpo1 = BVProblem(lotkavolterra, bc, [1,2], tspan, p) bvpo2 = BVProblem(lotkavolterra, genbc_oop, [1,2], tspan, p) - sol1 = solve(bvpo1, MIRK4(), dt = 0.01) - sol2 = solve(bvpo2, MIRK4(), dt = 0.01) + sol1 = solve(bvpo1, MIRK4(), dt = 0.05) + sol2 = solve(bvpo2, MIRK4(), dt = 0.05) @test sol1 ≈ sol2 end -function test_solvers(solvers, prob, u0map, constraints, equations = []; dt = 0.01) +function test_solvers(solvers, prob, u0map, constraints, equations = []; dt = 0.05) for solver in solvers sol = solve(prob, solver(); dt) - @test successful_retcode(sol.retcode) + @test SciMLBase.successful_retcode(sol.retcode) p = prob.p; t = sol.t; bc = prob.f.bc ns = length(prob.u0) - if isinplace(bvp.f) resid = zeros(ns) bc!(resid, sol, p, t) @@ -226,45 +190,83 @@ end # Simple ODESystem with BVP constraints. let - @parameters α=7.5 β=4.0 γ=8.0 δ=5.0 + t = ModelingToolkit.t_nounits; D = ModelingToolkit.D_nounits + @parameters α=1.5 β=1.0 γ=3.0 δ=1.0 @variables x(..) y(t) eqs = [D(x(t)) ~ α * x(t) - β * x(t) * y, D(y) ~ -γ * y + δ * x(t) * y] - u0map = [y => 2.0] + u0map = [] parammap = [α => 7.5, β => 4, γ => 8.0, δ => 5.0] tspan = (0.0, 10.0) - guesses = [x(t) => 1.0] + guesses = [x(t) => 1.0, y => 2.] @mtkbuild lotkavolterra = ODESystem(eqs, t) - constraints = [x(6.) ~ 3] + constraints = [x(6.) ~ 1.5] bvp = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}(lotkavolterra, u0map, tspan, parammap; guesses, constraints) test_solvers(solvers, bvp, u0map, constraints) # Testing that more complicated constraints give correct solutions. - constraints = [y(2.) + x(8.) ~ 12] - bvp = SciMLBase.BVProblem{false, SciMLBase.FullSpecialize}(pend, u0map, tspan, parammap; guesses, constraints) + constraints = [y(2.) + x(8.) ~ 2.] + bvp = SciMLBase.BVProblem{false, SciMLBase.FullSpecialize}(lotkavolterra, u0map, tspan, parammap; guesses, constraints) test_solvers(solvers, bvp, u0map, constraints) - constraints = [α * β - x(6.) ~ 24] - bvp = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}(pend, u0map, tspan, parammap; guesses, constraints) + constraints = [α * β - x(6.) ~ 0.5] + bvp = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}(lotkavolterra, u0map, tspan, parammap; guesses, constraints) test_solvers(solvers, bvp, u0map, constraints) # Testing that errors are properly thrown when malformed constraints are given. @variables bad(..) constraints = [x(1.) + bad(3.) ~ 10] - @test_throws Exception bvp = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}(pend, u0map, tspan, parammap; guesses, constraints) + @test_throws Exception bvp = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}(lotkavolterra, u0map, tspan, parammap; guesses, constraints) constraints = [x(t) + y(t) ~ 3] - @test_throws Exception bvp = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}(pend, u0map, tspan, parammap; guesses, constraints) + @test_throws Exception bvp = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}(lotkavolterra, u0map, tspan, parammap; guesses, constraints) @parameters bad2 constraints = [bad2 + x(0.) ~ 3] - @test_throws Exception bvp = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}(pend, u0map, tspan, parammap; guesses, constraints) + @test_throws Exception bvp = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}(lotkavolterra, u0map, tspan, parammap; guesses, constraints) end +# Cartesian pendulum from the docs. +# DAE IVP solved using BoundaryValueDiffEq solvers. +# let +# @parameters g +# @variables x(t) y(t) [state_priority = 10] λ(t) +# eqs = [D(D(x)) ~ λ * x +# D(D(y)) ~ λ * y - g +# x^2 + y^2 ~ 1] +# @mtkbuild pend = ODESystem(eqs, t) +# +# tspan = (0.0, 1.5) +# u0map = [x => 1, y => 0] +# pmap = [g => 1] +# guess = [λ => 1] +# +# prob = ODEProblem(pend, u0map, tspan, pmap; guesses = guess) +# osol = solve(prob, Rodas5P()) +# +# zeta = [0., 0., 0., 0., 0.] +# bvp = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}(pend, u0map, tspan, parammap; guesses = guess) +# +# for solver in solvers +# sol = solve(bvp, solver(zeta), dt = 0.001) +# @test isapprox(sol.u[end], osol.u[end]; atol = 0.01) +# conditions = getfield.(equations(pend)[3:end], :rhs) +# @test isapprox([sol[conditions][1]; sol[x][1] - 1; sol[y][1]], zeros(5), atol = 0.001) +# end +# +# bvp2 = SciMLBase.BVProblem{false, SciMLBase.FullSpecialize}(pend, u0map, tspan, parammap) +# for solver in solvers +# sol = solve(bvp, solver(zeta), dt = 0.01) +# @test isapprox(sol.u[end], osol.u[end]; atol = 0.01) +# conditions = getfield.(equations(pend)[3:end], :rhs) +# @test [sol[conditions][1]; sol[x][1] - 1; sol[y][1]] ≈ 0 +# end +# end + # Adding a midpoint boundary constraint. # Solve using BVDAE solvers. # let From ea15cc7519dc2451879b63e71d6241f1a9aef1fc Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 15 Jan 2025 15:11:43 +0530 Subject: [PATCH 0567/2176] build: bump SymbolicUtils compat --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 91a3c37109..93d535d085 100644 --- a/Project.toml +++ b/Project.toml @@ -143,7 +143,7 @@ StaticArrays = "0.10, 0.11, 0.12, 1.0" StochasticDiffEq = "6.72.1" StochasticDelayDiffEq = "1.8.1" SymbolicIndexingInterface = "0.3.36" -SymbolicUtils = "3.7" +SymbolicUtils = "3.10" Symbolics = "6.22.1" URIs = "1" UnPack = "0.1, 1.0" From e591cd0526a87c0706379722a9b343253f8abee6 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 9 Jan 2025 16:56:02 +0530 Subject: [PATCH 0568/2176] feat: add support for causal connections of variables --- src/systems/abstractsystem.jl | 9 ++- src/systems/analysis_points.jl | 19 ++++- src/systems/connectors.jl | 123 +++++++++++++++++++++++++++++++++ 3 files changed, 149 insertions(+), 2 deletions(-) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index ccc749baeb..3dc9cda3ba 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -1875,6 +1875,13 @@ Equivalent to `length(equations(expand_connections(sys))) - length(filter(eq -> function n_expanded_connection_equations(sys::AbstractSystem) # TODO: what about inputs? isconnector(sys) && return length(get_unknowns(sys)) + sys = remove_analysis_points(sys) + n_variable_connect_eqs = 0 + for eq in equations(sys) + is_causal_variable_connection(eq.rhs) || continue + n_variable_connect_eqs += length(get_systems(eq.rhs)) - 1 + end + sys, (csets, _) = generate_connection_set(sys) ceqs, instream_csets = generate_connection_equations_and_stream_connections(csets) n_outer_stream_variables = 0 @@ -1897,7 +1904,7 @@ function n_expanded_connection_equations(sys::AbstractSystem) # n_toplevel_unused_flows += count(x->get_connection_type(x) === Flow && !(x in toplevel_flows), get_unknowns(m)) #end - nextras = n_outer_stream_variables + length(ceqs) + nextras = n_outer_stream_variables + length(ceqs) + n_variable_connect_eqs end function Base.show( diff --git a/src/systems/analysis_points.jl b/src/systems/analysis_points.jl index 87f278d4dc..9ce5fe2bc1 100644 --- a/src/systems/analysis_points.jl +++ b/src/systems/analysis_points.jl @@ -208,6 +208,14 @@ function Symbolics.connect(in::AbstractSystem, name::Symbol, out, outs...; verbo return AnalysisPoint() ~ AnalysisPoint(in, name, [out; collect(outs)]; verbose) end +function Symbolics.connect( + in::ConnectableSymbolicT, name::Symbol, out::ConnectableSymbolicT, + outs::ConnectableSymbolicT...; verbose = true) + allvars = (in, out, outs...) + validate_causal_variables_connection(allvars) + return AnalysisPoint() ~ AnalysisPoint(in, name, [out; collect(outs)]; verbose) +end + """ $(TYPEDSIGNATURES) @@ -240,7 +248,7 @@ connection. This is the variable named `u` if present, and otherwise the only variable in the system. If the system does not have a variable named `u` and contains multiple variables, throw an error. """ -function ap_var(sys) +function ap_var(sys::AbstractSystem) if hasproperty(sys, :u) return sys.u end @@ -249,6 +257,15 @@ function ap_var(sys) error("Could not determine the analysis-point variable in system $(nameof(sys)). To use an analysis point, apply it to a connection between causal blocks which have a variable named `u` or a single unknown of the same size.") end +""" + $(TYPEDSIGNATURES) + +For an `AnalysisPoint` involving causal variables. Simply return the variable. +""" +function ap_var(var::ConnectableSymbolicT) + return var +end + """ $(TYPEDEF) diff --git a/src/systems/connectors.jl b/src/systems/connectors.jl index f0f49acb85..db3349f546 100644 --- a/src/systems/connectors.jl +++ b/src/systems/connectors.jl @@ -68,6 +68,93 @@ SymbolicUtils.promote_symtype(::typeof(instream), _) = Real isconnector(s::AbstractSystem) = has_connector_type(s) && get_connector_type(s) !== nothing +""" + $(TYPEDEF) + +Utility struct which wraps a symbolic variable used in a `Connection` to enable `Base.show` +to work. +""" +struct SymbolicWithNameof + var::Any +end + +function Base.nameof(x::SymbolicWithNameof) + return Symbol(x.var) +end + +is_causal_variable_connection(c) = false +function is_causal_variable_connection(c::Connection) + all(x -> x isa SymbolicWithNameof, get_systems(c)) +end + +const ConnectableSymbolicT = Union{BasicSymbolic, Num, Symbolics.Arr} + +const CAUSAL_CONNECTION_ERR = """ +Only causal variables can be used in a `connect` statement. The first argument must \ +be a single output variable and all subsequent variables must be input variables. +""" + +function VariableNotOutputError(var) + ArgumentError(""" + $CAUSAL_CONNECTION_ERR Expected $var to be marked as an output with `[output = true]` \ + in the variable metadata. + """) +end + +function VariableNotInputError(var) + ArgumentError(""" + $CAUSAL_CONNECTION_ERR Expected $var to be marked an input with `[input = true]` \ + in the variable metadata. + """) +end + +""" + $(TYPEDSIGNATURES) + +Perform validation for a connect statement involving causal variables. +""" +function validate_causal_variables_connection(allvars) + var1 = allvars[1] + var2 = allvars[2] + vars = Base.tail(Base.tail(allvars)) + for var in allvars + vtype = getvariabletype(var) + vtype === VARIABLE || + throw(ArgumentError("Expected $var to be of kind `$VARIABLE`. Got `$vtype`.")) + end + if length(unique(allvars)) !== length(allvars) + throw(ArgumentError("Expected all connection variables to be unique. Got variables $allvars which contains duplicate entries.")) + end + allsizes = map(size, allvars) + if !allequal(allsizes) + throw(ArgumentError("Expected all connection variables to have the same size. Got variables $allvars with sizes $allsizes respectively.")) + end + isoutput(var1) || throw(VariableNotOutputError(var1)) + isinput(var2) || throw(VariableNotInputError(var2)) + for var in vars + isinput(var) || throw(VariableNotInputError(var)) + end +end + +""" + $(TYPEDSIGNATURES) + +Connect multiple causal variables. The first variable must be an output, and all subsequent +variables must be inputs. The statement `connect(var1, var2, var3, ...)` expands to: + +```julia +var1 ~ var2 +var1 ~ var3 +# ... +``` +""" +function Symbolics.connect(var1::ConnectableSymbolicT, var2::ConnectableSymbolicT, + vars::ConnectableSymbolicT...) + allvars = (var1, var2, vars...) + validate_causal_variables_connection(allvars) + return Equation(Connection(), Connection(map(SymbolicWithNameof, allvars))) +end + function flowvar(sys::AbstractSystem) sts = get_unknowns(sys) for s in sts @@ -329,6 +416,10 @@ function generate_connection_set!(connectionsets, domain_csets, for eq in eqs′ lhs = eq.lhs rhs = eq.rhs + + # causal variable connections will be expanded before we get here, + # but this guard is useful for `n_expanded_connection_equations`. + is_causal_variable_connection(rhs) && continue if find !== nothing && find(rhs, _getname(namespace)) neweq, extra_unknown = replace(rhs, _getname(namespace)) if extra_unknown isa AbstractArray @@ -479,9 +570,41 @@ function domain_defaults(sys, domain_csets) def end +""" + $(TYPEDSIGNATURES) + +Recursively descend through the hierarchy of `sys` and expand all connection equations +of causal variables. Return the modified system. +""" +function expand_variable_connections(sys::AbstractSystem) + eqs = copy(get_eqs(sys)) + valid_idxs = trues(length(eqs)) + additional_eqs = Equation[] + + for (i, eq) in enumerate(eqs) + eq.lhs isa Connection || continue + connection = eq.rhs + elements = connection.systems + is_causal_variable_connection(connection) || continue + + valid_idxs[i] = false + elements = map(x -> x.var, elements) + outvar = first(elements) + for invar in Iterators.drop(elements, 1) + push!(additional_eqs, outvar ~ invar) + end + end + eqs = [eqs[valid_idxs]; additional_eqs] + subsystems = map(expand_variable_connections, get_systems(sys)) + @set! sys.eqs = eqs + @set! sys.systems = subsystems + return sys +end + function expand_connections(sys::AbstractSystem, find = nothing, replace = nothing; debug = false, tol = 1e-10, scalarize = true) sys = remove_analysis_points(sys) + sys = expand_variable_connections(sys) sys, (csets, domain_csets) = generate_connection_set(sys, find, replace; scalarize) ceqs, instream_csets = generate_connection_equations_and_stream_connections(csets) _sys = expand_instream(instream_csets, sys; debug = debug, tol = tol) From 0df55eb2414cecd4bae7fc30a7b57c4c4ed7309a Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 9 Jan 2025 16:56:11 +0530 Subject: [PATCH 0569/2176] test: test causal connections of variables --- test/causal_variables_connection.jl | 98 +++++++++++++++++++++++++++++ test/runtests.jl | 1 + 2 files changed, 99 insertions(+) create mode 100644 test/causal_variables_connection.jl diff --git a/test/causal_variables_connection.jl b/test/causal_variables_connection.jl new file mode 100644 index 0000000000..c22e8319d4 --- /dev/null +++ b/test/causal_variables_connection.jl @@ -0,0 +1,98 @@ +using ModelingToolkit, ModelingToolkitStandardLibrary.Blocks +using ModelingToolkit: t_nounits as t, D_nounits as D + +@testset "Error checking" begin + @variables begin + x(t) + y(t), [input = true] + z(t), [output = true] + w(t) + v(t), [input = true] + u(t), [output = true] + xarr(t)[1:4], [output = true] + yarr(t)[1:2, 1:2], [input = true] + end + @parameters begin + p, [input = true] + q, [output = true] + end + + @test_throws ["p", "kind", "VARIABLE", "PARAMETER"] connect(z, p) + @test_throws ["q", "kind", "VARIABLE", "PARAMETER"] connect(q, y) + @test_throws ["p", "kind", "VARIABLE", "PARAMETER"] connect(z, y, p) + + @test_throws ["unique"] connect(z, y, y) + + @test_throws ["same size"] connect(xarr, yarr) + + @test_throws ["Expected", "x", "output = true", "metadata"] connect(x, y) + @test_throws ["Expected", "y", "output = true", "metadata"] connect(y, v) + + @test_throws ["Expected", "x", "input = true", "metadata"] connect(z, x) + @test_throws ["Expected", "x", "input = true", "metadata"] connect(z, y, x) + @test_throws ["Expected", "u", "input = true", "metadata"] connect(z, u) + @test_throws ["Expected", "u", "input = true", "metadata"] connect(z, y, u) +end + +@testset "Connection expansion" begin + @named P = FirstOrder(k = 1, T = 1) + @named C = Gain(; k = -1) + + eqs = [connect(P.output.u, C.input.u) + connect(C.output.u, P.input.u)] + sys1 = ODESystem(eqs, t, systems = [P, C], name = :hej) + sys = expand_connections(sys1) + @test any(isequal(P.output.u ~ C.input.u), equations(sys)) + @test any(isequal(C.output.u ~ P.input.u), equations(sys)) + + @named sysouter = ODESystem(Equation[], t; systems = [sys1]) + sys = expand_connections(sysouter) + @test any(isequal(sys1.P.output.u ~ sys1.C.input.u), equations(sys)) + @test any(isequal(sys1.C.output.u ~ sys1.P.input.u), equations(sys)) +end + +@testset "With Analysis Points" begin + @named P = FirstOrder(k = 1, T = 1) + @named C = Gain(; k = -1) + + ap = AnalysisPoint(:plant_input) + eqs = [connect(P.output, C.input), connect(C.output.u, ap, P.input.u)] + sys = ODESystem(eqs, t, systems = [P, C], name = :hej) + @named nested_sys = ODESystem(Equation[], t; systems = [sys]) + + test_cases = [ + ("inner", sys, sys.plant_input), + ("nested", nested_sys, nested_sys.hej.plant_input), + ("inner - Symbol", sys, :plant_input), + ("nested - Symbol", nested_sys, nameof(sys.plant_input)) + ] + + @testset "get_sensitivity - $name" for (name, sys, ap) in test_cases + matrices, _ = get_sensitivity(sys, ap) + @test matrices.A[] == -2 + @test matrices.B[] * matrices.C[] == -1 # either one negative + @test matrices.D[] == 1 + end + + @testset "get_comp_sensitivity - $name" for (name, sys, ap) in test_cases + matrices, _ = get_comp_sensitivity(sys, ap) + @test matrices.A[] == -2 + @test matrices.B[] * matrices.C[] == 1 # both positive or negative + @test matrices.D[] == 0 + end + + @testset "get_looptransfer - $name" for (name, sys, ap) in test_cases + matrices, _ = get_looptransfer(sys, ap) + @test matrices.A[] == -1 + @test matrices.B[] * matrices.C[] == -1 # either one negative + @test matrices.D[] == 0 + end + + @testset "open_loop - $name" for (name, sys, ap) in test_cases + open_sys, (du, u) = open_loop(sys, ap) + matrices, _ = linearize(open_sys, [du], [u]) + @test matrices.A[] == -1 + @test matrices.B[] * matrices.C[] == -1 # either one negative + @test matrices.D[] == 0 + end +end diff --git a/test/runtests.jl b/test/runtests.jl index d38aa86bb2..95154e550e 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -85,6 +85,7 @@ end @safetestset "Constraints Test" include("constraints.jl") @safetestset "IfLifting Test" include("if_lifting.jl") @safetestset "Analysis Points Test" include("analysis_points.jl") + @safetestset "Causal Variables Connection Test" include("causal_variables_connection.jl") end end From b2b57eabd21a4bf66c76df11f336ba2ab78c0496 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 10 Jan 2025 12:05:42 +0530 Subject: [PATCH 0570/2176] refactor: rearrange includes --- src/ModelingToolkit.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ModelingToolkit.jl b/src/ModelingToolkit.jl index 7462122998..b63112bde1 100644 --- a/src/ModelingToolkit.jl +++ b/src/ModelingToolkit.jl @@ -145,8 +145,8 @@ include("systems/index_cache.jl") include("systems/parameter_buffer.jl") include("systems/abstractsystem.jl") include("systems/model_parsing.jl") -include("systems/analysis_points.jl") include("systems/connectors.jl") +include("systems/analysis_points.jl") include("systems/imperative_affect.jl") include("systems/callbacks.jl") include("systems/problem_utils.jl") From f6728dd21adc31b74d399ef622b72ea1f6ab3e0d Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 15 Jan 2025 15:12:34 +0530 Subject: [PATCH 0571/2176] build: bump SymbolicUtils compat --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 91a3c37109..93d535d085 100644 --- a/Project.toml +++ b/Project.toml @@ -143,7 +143,7 @@ StaticArrays = "0.10, 0.11, 0.12, 1.0" StochasticDiffEq = "6.72.1" StochasticDelayDiffEq = "1.8.1" SymbolicIndexingInterface = "0.3.36" -SymbolicUtils = "3.7" +SymbolicUtils = "3.10" Symbolics = "6.22.1" URIs = "1" UnPack = "0.1, 1.0" From 3133177381cd3b5d0a44ea7e8df559cfdc648167 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 15 Jan 2025 15:20:51 +0530 Subject: [PATCH 0572/2176] fix: fix printing of `AbstractSystem` with subsystems --- src/systems/abstractsystem.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index ccc749baeb..2bafe55396 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -1919,7 +1919,7 @@ function Base.show( nrows > 0 && hint && print(io, " see hierarchy($name)") for i in 1:nrows sub = subs[i] - name = String(nameof(sub)) + local name = String(nameof(sub)) print(io, "\n ", name) desc = description(sub) if !isempty(desc) From eff59072a629c2e5901715db9943d3ae9da4b604 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 15 Jan 2025 15:21:38 +0530 Subject: [PATCH 0573/2176] fix: handle all symtypes in `subexpressions_not_involving_vars!` --- src/utils.jl | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/utils.jl b/src/utils.jl index c3011c2a79..46b7ad2de4 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -1108,10 +1108,15 @@ returns the modified `expr`. """ function subexpressions_not_involving_vars!(expr, vars, state::Dict{Any, Any}) expr = unwrap(expr) - symbolic_type(expr) == NotSymbolic() && return expr + if symbolic_type(expr) == NotSymbolic() + if is_array_of_symbolics(expr) + return map(expr) do el + subexpressions_not_involving_vars!(el, vars, state) + end + end + return expr + end iscall(expr) || return expr - is_variable_floatingpoint(expr) || return expr - symtype(expr) <: Union{Real, AbstractArray{<:Real}} || return expr Symbolics.shape(expr) == Symbolics.Unknown() && return expr haskey(state, expr) && return state[expr] vs = ModelingToolkit.vars(expr) @@ -1143,7 +1148,6 @@ function subexpressions_not_involving_vars!(expr, vars, state::Dict{Any, Any}) return op(indep_term, dep_term) end newargs = map(args) do arg - symbolic_type(arg) != NotSymbolic() || is_array_of_symbolics(arg) || return arg subexpressions_not_involving_vars!(arg, vars, state) end return maketerm(typeof(expr), op, newargs, metadata(expr)) From 5c9e14862cae7ed381fe194bf709f7de97c809f3 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 15 Jan 2025 15:21:58 +0530 Subject: [PATCH 0574/2176] feat: support caching of different types of subexpressions in `SCCNonlinearProblem` --- src/ModelingToolkit.jl | 3 +- src/systems/nonlinear/nonlinearsystem.jl | 113 ++++++++++++++++++----- 2 files changed, 91 insertions(+), 25 deletions(-) diff --git a/src/ModelingToolkit.jl b/src/ModelingToolkit.jl index 7462122998..d88120afa0 100644 --- a/src/ModelingToolkit.jl +++ b/src/ModelingToolkit.jl @@ -54,7 +54,8 @@ import SCCNonlinearSolve using Reexport using RecursiveArrayTools import Graphs: SimpleDiGraph, add_edge!, incidence_matrix -import BlockArrays: BlockedArray, Block, blocksize, blocksizes +import BlockArrays: BlockArray, BlockedArray, Block, blocksize, blocksizes, blockpush!, + undef_blocks, blocks import CommonSolve import EnumX diff --git a/src/systems/nonlinear/nonlinearsystem.jl b/src/systems/nonlinear/nonlinearsystem.jl index fe18d0de35..bc3c1451e1 100644 --- a/src/systems/nonlinear/nonlinearsystem.jl +++ b/src/systems/nonlinear/nonlinearsystem.jl @@ -573,29 +573,37 @@ function DiffEqBase.NonlinearLeastSquaresProblem{iip}(sys::NonlinearSystem, u0ma NonlinearLeastSquaresProblem{iip}(f, u0, p; filter_kwargs(kwargs)...) end +const TypeT = Union{DataType, UnionAll} + struct CacheWriter{F} fn::F end function (cw::CacheWriter)(p, sols) - cw.fn(p.caches[1], sols, p...) + cw.fn(p.caches..., sols, p...) end -function CacheWriter(sys::AbstractSystem, exprs, solsyms, obseqs::Vector{Equation}; +function CacheWriter(sys::AbstractSystem, buffer_types::Vector{TypeT}, + exprs::Dict{TypeT, Vector{Any}}, solsyms, obseqs::Vector{Equation}; eval_expression = false, eval_module = @__MODULE__) ps = parameters(sys) rps = reorder_parameters(sys, ps) obs_assigns = [eq.lhs ← eq.rhs for eq in obseqs] cmap, cs = get_cmap(sys) cmap_assigns = [eq.lhs ← eq.rhs for eq in cmap] + + outsyms = [Symbol(:out, i) for i in eachindex(buffer_types)] + body = map(eachindex(buffer_types), buffer_types) do i, T + Symbol(:tmp, i) ← SetArray(true, outsyms[i], get(exprs, T, [])) + end fn = Func( - [:out, DestructuredArgs(DestructuredArgs.(solsyms)), + [outsyms..., DestructuredArgs(DestructuredArgs.(solsyms)), DestructuredArgs.(rps)...], [], - SetArray(true, :out, exprs) + Let(body, :()) ) |> wrap_assignments(false, obs_assigns)[2] |> wrap_parameter_dependencies(sys, false)[2] |> - wrap_array_vars(sys, exprs; dvs = nothing, inputs = [])[2] |> + wrap_array_vars(sys, []; dvs = nothing, inputs = [])[2] |> wrap_assignments(false, cmap_assigns)[2] |> toexpr return CacheWriter(eval_or_rgf(fn; eval_expression, eval_module)) end @@ -677,8 +685,16 @@ function SciMLBase.SCCNonlinearProblem{iip}(sys::NonlinearSystem, u0map, explicitfuns = [] nlfuns = [] - prevobsidxs = Int[] - cachesize = 0 + prevobsidxs = BlockArray(undef_blocks, Vector{Int}, Int[]) + # Cache buffer types and corresponding sizes. Stored as a pair of arrays instead of a + # dict to maintain a consistent order of buffers across SCCs + cachetypes = TypeT[] + cachesizes = Int[] + # explicitfun! related information for each SCC + # We need to compute buffer sizes before doing any codegen + scc_cachevars = Dict{TypeT, Vector{Any}}[] + scc_cacheexprs = Dict{TypeT, Vector{Any}}[] + scc_eqs = Vector{Equation}[] for (i, (escc, vscc)) in enumerate(zip(eq_sccs, var_sccs)) # subset unknowns and equations _dvs = dvs[vscc] @@ -690,6 +706,7 @@ function SciMLBase.SCCNonlinearProblem{iip}(sys::NonlinearSystem, u0map, _obs = obs[obsidxs] # get all subexpressions in the RHS which we can precompute in the cache + # precomputed subexpressions should not contain `banned_vars` banned_vars = Set{Any}(vcat(_dvs, getproperty.(_obs, (:lhs,)))) for var in banned_vars iscall(var) || continue @@ -706,37 +723,85 @@ function SciMLBase.SCCNonlinearProblem{iip}(sys::NonlinearSystem, u0map, _eqs[i].rhs, banned_vars, state) end - # cached variables and their corresponding expressions - cachevars = Any[obs[i].lhs for i in prevobsidxs] - cacheexprs = Any[obs[i].lhs for i in prevobsidxs] + # map from symtype to cached variables and their expressions + cachevars = Dict{Union{DataType, UnionAll}, Vector{Any}}() + cacheexprs = Dict{Union{DataType, UnionAll}, Vector{Any}}() + # observed of previous SCCs are in the cache + # NOTE: When we get proper CSE, we can substitute these + # and then use `subexpressions_not_involving_vars!` + for i in prevobsidxs + T = symtype(obs[i].lhs) + buf = get!(() -> Any[], cachevars, T) + push!(buf, obs[i].lhs) + + buf = get!(() -> Any[], cacheexprs, T) + push!(buf, obs[i].lhs) + end + for (k, v) in state - push!(cachevars, unwrap(v)) - push!(cacheexprs, unwrap(k)) + k = unwrap(k) + v = unwrap(v) + T = symtype(k) + buf = get!(() -> Any[], cachevars, T) + push!(buf, v) + buf = get!(() -> Any[], cacheexprs, T) + push!(buf, k) end - cachesize = max(cachesize, length(cachevars)) + + # update the sizes of cache buffers + for (T, buf) in cachevars + idx = findfirst(isequal(T), cachetypes) + if idx === nothing + push!(cachetypes, T) + push!(cachesizes, 0) + idx = lastindex(cachetypes) + end + cachesizes[idx] = max(cachesizes[idx], length(buf)) + end + + push!(scc_cachevars, cachevars) + push!(scc_cacheexprs, cacheexprs) + push!(scc_eqs, _eqs) + blockpush!(prevobsidxs, obsidxs) + end + + for (i, (escc, vscc)) in enumerate(zip(eq_sccs, var_sccs)) + _dvs = dvs[vscc] + _eqs = scc_eqs[i] + obsidxs = prevobsidxs[Block(i)] + _prevobsidxs = reduce(vcat, blocks(prevobsidxs)[1:(i - 1)]; init = Int[]) + _obs = obs[obsidxs] + cachevars = scc_cachevars[i] + cacheexprs = scc_cacheexprs[i] if isempty(cachevars) push!(explicitfuns, Returns(nothing)) else solsyms = getindex.((dvs,), view(var_sccs, 1:(i - 1))) push!(explicitfuns, - CacheWriter(sys, cacheexprs, solsyms, obs[prevobsidxs]; + CacheWriter(sys, cachetypes, cacheexprs, solsyms, obs[_prevobsidxs]; eval_expression, eval_module)) end + + cachebufsyms = Tuple(map(cachetypes) do T + get(cachevars, T, []) + end) f = SCCNonlinearFunction{iip}( - sys, _eqs, _dvs, _obs, (cachevars,); eval_expression, eval_module, kwargs...) + sys, _eqs, _dvs, _obs, cachebufsyms; eval_expression, eval_module, kwargs...) push!(nlfuns, f) - append!(cachevars, _dvs) - append!(cacheexprs, _dvs) - for i in obsidxs - push!(cachevars, obs[i].lhs) - push!(cacheexprs, obs[i].rhs) - end - append!(prevobsidxs, obsidxs) end - if cachesize != 0 - p = rebuild_with_caches(p, BufferTemplate(eltype(u0), cachesize)) + if !isempty(cachetypes) + templates = map(cachetypes, cachesizes) do T, n + # Real refers to `eltype(u0)` + if T == Real + T = eltype(u0) + elseif T <: Array && eltype(T) == Real + T = Array{eltype(u0), ndims(T)} + end + BufferTemplate(T, n) + end + p = rebuild_with_caches(p, templates...) end subprobs = [] From 2ef4392c29c3376fd965141f50211ad3f4c38242 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 15 Jan 2025 15:22:07 +0530 Subject: [PATCH 0575/2176] test: test caching of different symtypes in `SCCNonlinearProblem` --- test/scc_nonlinear_problem.jl | 92 +++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) diff --git a/test/scc_nonlinear_problem.jl b/test/scc_nonlinear_problem.jl index fdf1646343..57f3d72fb7 100644 --- a/test/scc_nonlinear_problem.jl +++ b/test/scc_nonlinear_problem.jl @@ -161,3 +161,95 @@ end @test SciMLBase.successful_retcode(sccsol) @test val[] == 1 end + +import ModelingToolkitStandardLibrary.Blocks as B +import ModelingToolkitStandardLibrary.Mechanical.Translational as T +import ModelingToolkitStandardLibrary.Hydraulic.IsothermalCompressible as IC + +@testset "Caching of subexpressions of different types" begin + liquid_pressure(rho, rho_0, bulk) = (rho / rho_0 - 1) * bulk + gas_pressure(rho, rho_0, p_gas, rho_gas) = rho * ((0 - p_gas) / (rho_0 - rho_gas)) + full_pressure(rho, rho_0, bulk, p_gas, rho_gas) = ifelse( + rho >= rho_0, liquid_pressure(rho, rho_0, bulk), + gas_pressure(rho, rho_0, p_gas, rho_gas)) + + @component function Volume(; + #parameters + area, + direction = +1, + x_int, + name) + pars = @parameters begin + area = area + x_int = x_int + rho_0 = 1000 + bulk = 1e9 + p_gas = -1000 + rho_gas = 1 + end + + vars = @variables begin + x(t) = x_int + dx(t), [guess = 0] + p(t), [guess = 0] + f(t), [guess = 0] + rho(t), [guess = 0] + m(t), [guess = 0] + dm(t), [guess = 0] + end + + systems = @named begin + port = IC.HydraulicPort() + flange = T.MechanicalPort() + end + + eqs = [ + # connectors + port.p ~ p + port.dm ~ dm + flange.v * direction ~ dx + flange.f * direction ~ -f + + # differentials + D(x) ~ dx + D(m) ~ dm + + # physics + p ~ full_pressure(rho, rho_0, bulk, p_gas, rho_gas) + f ~ p * area + m ~ rho * x * area] + + return ODESystem(eqs, t, vars, pars; name, systems) + end + + systems = @named begin + fluid = IC.HydraulicFluid(; bulk_modulus = 1e9) + + src1 = IC.Pressure(;) + src2 = IC.Pressure(;) + + vol1 = Volume(; area = 0.01, direction = +1, x_int = 0.1) + vol2 = Volume(; area = 0.01, direction = +1, x_int = 0.1) + + mass = T.Mass(; m = 10) + + sin1 = B.Sine(; frequency = 0.5, amplitude = +0.5e5, offset = 10e5) + sin2 = B.Sine(; frequency = 0.5, amplitude = -0.5e5, offset = 10e5) + end + + eqs = [connect(fluid, src1.port) + connect(fluid, src2.port) + connect(src1.port, vol1.port) + connect(src2.port, vol2.port) + connect(vol1.flange, mass.flange, vol2.flange) + connect(src1.p, sin1.output) + connect(src2.p, sin2.output)] + + initialization_eqs = [mass.s ~ 0.0 + mass.v ~ 0.0] + + @mtkbuild sys = ODESystem(eqs, t, [], []; systems, initialization_eqs) + prob = ODEProblem(sys, [], (0, 5)) + sol = solve(prob) + @test SciMLBase.successful_retcode(sol) +end From 562b77a6b13bc60889278da2de1b7a5422dbb461 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 15 Jan 2025 17:35:41 +0530 Subject: [PATCH 0576/2176] fix: fix `CacheWriter` codegen with array symbolics --- src/systems/nonlinear/nonlinearsystem.jl | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/systems/nonlinear/nonlinearsystem.jl b/src/systems/nonlinear/nonlinearsystem.jl index bc3c1451e1..3059da4cb4 100644 --- a/src/systems/nonlinear/nonlinearsystem.jl +++ b/src/systems/nonlinear/nonlinearsystem.jl @@ -580,7 +580,7 @@ struct CacheWriter{F} end function (cw::CacheWriter)(p, sols) - cw.fn(p.caches..., sols, p...) + cw.fn(p.caches, sols, p...) end function CacheWriter(sys::AbstractSystem, buffer_types::Vector{TypeT}, @@ -594,10 +594,10 @@ function CacheWriter(sys::AbstractSystem, buffer_types::Vector{TypeT}, outsyms = [Symbol(:out, i) for i in eachindex(buffer_types)] body = map(eachindex(buffer_types), buffer_types) do i, T - Symbol(:tmp, i) ← SetArray(true, outsyms[i], get(exprs, T, [])) + Symbol(:tmp, i) ← SetArray(true, :(out[$i]), get(exprs, T, [])) end fn = Func( - [outsyms..., DestructuredArgs(DestructuredArgs.(solsyms)), + [:out, DestructuredArgs(DestructuredArgs.(solsyms)), DestructuredArgs.(rps)...], [], Let(body, :()) From 1febdae62742d4694f46597a8414d089d954682e Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 15 Jan 2025 17:36:06 +0530 Subject: [PATCH 0577/2176] fix: fix handling of scalarized array symbolics in `subexpressions_not_involving_vars!` --- src/utils.jl | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/src/utils.jl b/src/utils.jl index 46b7ad2de4..5e4f0b52d2 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -1116,20 +1116,25 @@ function subexpressions_not_involving_vars!(expr, vars, state::Dict{Any, Any}) end return expr end + any(isequal(expr), vars) && return expr iscall(expr) || return expr Symbolics.shape(expr) == Symbolics.Unknown() && return expr haskey(state, expr) && return state[expr] - vs = ModelingToolkit.vars(expr) - intersect!(vs, vars) - if isempty(vs) + op = operation(expr) + args = arguments(expr) + # if this is a `getindex` and the getindex-ed value is a `Sym` + # or it is not a called parameter + # OR + # none of `vars` are involved in `expr` + if op === getindex && (issym(args[1]) || !iscalledparameter(args[1])) || + (vs = ModelingToolkit.vars(expr); intersect!(vs, vars); isempty(vs)) sym = gensym(:subexpr) stype = symtype(expr) var = similar_variable(expr, sym) state[expr] = var return var end - op = operation(expr) - args = arguments(expr) + if (op == (+) || op == (*)) && symbolic_type(expr) !== ArraySymbolic() indep_args = [] dep_args = [] From 6ce9e85b2b8f902f913533d29d9f9f4337e194a8 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 15 Jan 2025 17:36:19 +0530 Subject: [PATCH 0578/2176] fix: fix handling of SCC observed equations in `SCCNonlinearProblem` --- src/systems/nonlinear/nonlinearsystem.jl | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/systems/nonlinear/nonlinearsystem.jl b/src/systems/nonlinear/nonlinearsystem.jl index 3059da4cb4..6b0b0cc759 100644 --- a/src/systems/nonlinear/nonlinearsystem.jl +++ b/src/systems/nonlinear/nonlinearsystem.jl @@ -695,6 +695,7 @@ function SciMLBase.SCCNonlinearProblem{iip}(sys::NonlinearSystem, u0map, scc_cachevars = Dict{TypeT, Vector{Any}}[] scc_cacheexprs = Dict{TypeT, Vector{Any}}[] scc_eqs = Vector{Equation}[] + scc_obs = Vector{Equation}[] for (i, (escc, vscc)) in enumerate(zip(eq_sccs, var_sccs)) # subset unknowns and equations _dvs = dvs[vscc] @@ -708,10 +709,8 @@ function SciMLBase.SCCNonlinearProblem{iip}(sys::NonlinearSystem, u0map, # get all subexpressions in the RHS which we can precompute in the cache # precomputed subexpressions should not contain `banned_vars` banned_vars = Set{Any}(vcat(_dvs, getproperty.(_obs, (:lhs,)))) - for var in banned_vars - iscall(var) || continue - operation(var) === getindex || continue - push!(banned_vars, arguments(var)[1]) + filter!(banned_vars) do var + symbolic_type(var) != ArraySymbolic() || all(x -> var[i] in banned_vars, eachindex(var)) end state = Dict() for i in eachindex(_obs) @@ -762,15 +761,15 @@ function SciMLBase.SCCNonlinearProblem{iip}(sys::NonlinearSystem, u0map, push!(scc_cachevars, cachevars) push!(scc_cacheexprs, cacheexprs) push!(scc_eqs, _eqs) + push!(scc_obs, _obs) blockpush!(prevobsidxs, obsidxs) end for (i, (escc, vscc)) in enumerate(zip(eq_sccs, var_sccs)) _dvs = dvs[vscc] _eqs = scc_eqs[i] - obsidxs = prevobsidxs[Block(i)] _prevobsidxs = reduce(vcat, blocks(prevobsidxs)[1:(i - 1)]; init = Int[]) - _obs = obs[obsidxs] + _obs = scc_obs[i] cachevars = scc_cachevars[i] cacheexprs = scc_cacheexprs[i] From c7977aec93e28c47a89e75fff6cf1beb1cf41ad2 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 15 Jan 2025 18:11:12 +0530 Subject: [PATCH 0579/2176] test: refactor initialization test --- test/initializationsystem.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/initializationsystem.jl b/test/initializationsystem.jl index 6f8c961b45..987c2e091a 100644 --- a/test/initializationsystem.jl +++ b/test/initializationsystem.jl @@ -32,7 +32,7 @@ initprob = ModelingToolkit.InitializationProblem(pend, 0.0, [x => 1, y => 0], [g @test initprob isa NonlinearLeastSquaresProblem sol = solve(initprob) @test SciMLBase.successful_retcode(sol) -@test sol.u == [0.0, 0.0, 0.0, 0.0] +@test all(iszero, sol.u) @test maximum(abs.(sol[conditions])) < 1e-14 initprob = ModelingToolkit.InitializationProblem( From e802946e597d8f7fa7932ed328cb3acb1599e2c3 Mon Sep 17 00:00:00 2001 From: vyudu Date: Wed, 15 Jan 2025 16:10:55 -0500 Subject: [PATCH 0580/2176] test update --- src/systems/diffeqs/abstractodesystem.jl | 10 ++-- test/bvproblem.jl | 76 +++++++++++++----------- 2 files changed, 44 insertions(+), 42 deletions(-) diff --git a/src/systems/diffeqs/abstractodesystem.jl b/src/systems/diffeqs/abstractodesystem.jl index 0e8435942c..d935a94eb8 100644 --- a/src/systems/diffeqs/abstractodesystem.jl +++ b/src/systems/diffeqs/abstractodesystem.jl @@ -860,12 +860,11 @@ SciMLBase.BVProblem{iip}(sys::AbstractODESystem, u0map, tspan, kwargs...) where {iip} ``` -Create a boundary value problem from the [`ODESystem`](@ref). The arguments `dvs` and -`ps` are used to set the order of the dependent variable and parameter vectors, -respectively. `u0map` is used to specify fixed initial values for the states. +Create a boundary value problem from the [`ODESystem`](@ref). -Every variable must have either an initial guess supplied using `guesses` or -a fixed initial value specified using `u0map`. +`u0map` is used to specify fixed initial values for the states. Every variable +must have either an initial guess supplied using `guesses` or a fixed initial +value specified using `u0map`. `constraints` are used to specify boundary conditions to the ODESystem in the form of equations. These values should specify values that state variables should @@ -1049,7 +1048,6 @@ function process_constraints(sys::ODESystem, constraints, u0, u0_idxs, tspan, ii end exprs = vcat(init_cond_exprs, exprs) - @show exprs bcs = Symbolics.build_function(exprs, sol, p, expression = Val{false}) if iip return (resid, u, p, t) -> bcs[2](resid, u, p) diff --git a/test/bvproblem.jl b/test/bvproblem.jl index 42762b8a3f..e6645c060d 100644 --- a/test/bvproblem.jl +++ b/test/bvproblem.jl @@ -83,14 +83,14 @@ end ### ODESystem with constraint equations, DAEs with constraints ### ################################################################## -# Test generation of boundary condition function. +# Test generation of boundary condition function using `process_constraints`. Compare solutions to manually written boundary conditions let @parameters α=1.5 β=1.0 γ=3.0 δ=1.0 - @variables x(..) y(t) - eqs = [D(x(t)) ~ α * x(t) - β * x(t) * y, - D(y) ~ -γ * y + δ * x(t) * y] + @variables x(..) y(..) + eqs = [D(x(t)) ~ α * x(t) - β * x(t) * y(t), + D(y(t)) ~ -γ * y(t) + δ * x(t) * y(t)] - tspan = (0., 10.) + tspan = (0., 1.) @mtkbuild lksys = ODESystem(eqs, t) function lotkavolterra!(du, u, p, t) @@ -111,7 +111,7 @@ let [u[1][1] - 1., u[1][2] - 2.] end - u0 = [1., 2.]; p = [1.5, 1., 3., 1.] + u0 = [1., 2.]; p = [1.5, 1., 1., 3.] genbc_iip = ModelingToolkit.process_constraints(lksys, nothing, u0, [1, 2], tspan, true) genbc_oop = ModelingToolkit.process_constraints(lksys, nothing, u0, [1, 2], tspan, false) @@ -130,48 +130,54 @@ let @test sol1 ≈ sol2 # Test with a constraint. - constraints = [x(0.5) ~ 1.] + constraints = [y(0.5) ~ 2.] function bc!(resid, u, p, t) - resid[1] = u[1][2] - 2. - resid[2] = u(0.5)[1] - 1. + resid[1] = u(0.0)[1] - 1. + resid[2] = u(0.5)[2] - 2. end function bc(u, p, t) - [u[1][2] - 2., u(0.5)[1] - 1.] + [u(0.0)[1] - 1., u(0.5)[2] - 2.] end - u0 = [1., 2.] - genbc_iip = ModelingToolkit.process_constraints(lksys, constraints, u0, [2], tspan, true) - genbc_oop = ModelingToolkit.process_constraints(lksys, constraints, u0, [2], tspan, false) + u0 = [1, 1.] + genbc_iip = ModelingToolkit.process_constraints(lksys, constraints, u0, [1], tspan, true) + genbc_oop = ModelingToolkit.process_constraints(lksys, constraints, u0, [1], tspan, false) - bvpi1 = SciMLBase.BVProblem(lotkavolterra!, bc!, [1.,2.], tspan, p) - bvpi2 = SciMLBase.BVProblem(lotkavolterra!, genbc_iip, [1.,2.], tspan, p) - bvpi3 = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}(lksys, u0map, tspan, parammap; guesses, constraints) + bvpi1 = SciMLBase.BVProblem(lotkavolterra!, bc!, u0, tspan, p) + bvpi2 = SciMLBase.BVProblem(lotkavolterra!, genbc_iip, u0, tspan, p) + bvpi3 = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}(lksys, [x(t) => 1.], tspan; guesses = [y(t) => 1.], constraints) + bvpi4 = SciMLBase.BVProblem{true, SciMLBase.FullSpecialize}(lksys, [x(t) => 1.], tspan; guesses = [y(t) => 1.], constraints) - sol1 = solve(bvpi1, MIRK4(), dt = 0.05) - sol2 = solve(bvpi2, MIRK4(), dt = 0.05) - @test sol1 ≈ sol2 # don't get true equality here, not sure why + @btime sol1 = solve(bvpi1, MIRK4(), dt = 0.01) + @btime sol2 = solve(bvpi2, MIRK4(), dt = 0.01) + @btime sol3 = solve(bvpi3, MIRK4(), dt = 0.01) + @btime sol4 = solve(bvpi4, MIRK4(), dt = 0.01) + @test sol1 ≈ sol2 ≈ sol3 ≈ sol4 # don't get true equality here, not sure why bvpo1 = BVProblem(lotkavolterra, bc, [1,2], tspan, p) bvpo2 = BVProblem(lotkavolterra, genbc_oop, [1,2], tspan, p) + bvpo3 = SciMLBase.BVProblem{false, SciMLBase.FullSpecialize}(lksys, [x(t) => 1.], tspan, parammap; guesses = [y(t) => 1.], constraints) - sol1 = solve(bvpo1, MIRK4(), dt = 0.05) - sol2 = solve(bvpo2, MIRK4(), dt = 0.05) - @test sol1 ≈ sol2 + @btime sol1 = solve(bvpo1, MIRK4(), dt = 0.05) + @btime sol2 = solve(bvpo2, MIRK4(), dt = 0.05) + @btime sol3 = solve(bvpo3, MIRK4(), dt = 0.05) + @test sol1 ≈ sol2 ≈ sol3 end -function test_solvers(solvers, prob, u0map, constraints, equations = []; dt = 0.05) +function test_solvers(solvers, prob, u0map, constraints, equations = []; dt = 0.05, atol = 1e-4) for solver in solvers - sol = solve(prob, solver(); dt) + println("Solver: $solver") + @btime sol = solve(prob, solver(), dt = dt, abstol = atol) @test SciMLBase.successful_retcode(sol.retcode) p = prob.p; t = sol.t; bc = prob.f.bc ns = length(prob.u0) if isinplace(bvp.f) resid = zeros(ns) bc!(resid, sol, p, t) - @test isapprox(zeros(ns), resid) + @test isapprox(zeros(ns), resid; atol) else - @test isapprox(zeros(ns), bc(sol, p, t)) + @test isapprox(zeros(ns), bc(sol, p, t); atol) end for (k, v) in u0map @@ -190,23 +196,21 @@ end # Simple ODESystem with BVP constraints. let - t = ModelingToolkit.t_nounits; D = ModelingToolkit.D_nounits @parameters α=1.5 β=1.0 γ=3.0 δ=1.0 - @variables x(..) y(t) + @variables x(..) y(..) - eqs = [D(x(t)) ~ α * x(t) - β * x(t) * y, - D(y) ~ -γ * y + δ * x(t) * y] + eqs = [D(x(t)) ~ α * x(t) - β * x(t) * y(t), + D(y(t)) ~ -γ * y(t) + δ * x(t) * y(t)] u0map = [] - parammap = [α => 7.5, β => 4, γ => 8.0, δ => 5.0] tspan = (0.0, 10.0) - guesses = [x(t) => 1.0, y => 2.] + guesses = [x(t) => 4.0, y(t) => 2.] - @mtkbuild lotkavolterra = ODESystem(eqs, t) + @mtkbuild lksys = ODESystem(eqs, t) - constraints = [x(6.) ~ 1.5] - bvp = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}(lotkavolterra, u0map, tspan, parammap; guesses, constraints) - test_solvers(solvers, bvp, u0map, constraints) + constraints = [x(6.) ~ 3.5, x(3.) ~ 7.] + bvp = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}(lksys, u0map, tspan; guesses, constraints) + test_solvers(solvers, bvp, u0map, constraints; dt = 0.1) # Testing that more complicated constraints give correct solutions. constraints = [y(2.) + x(8.) ~ 2.] From e74e047bf333fc197ecd0b5390f22aa2651a4431 Mon Sep 17 00:00:00 2001 From: vyudu Date: Wed, 15 Jan 2025 17:20:43 -0500 Subject: [PATCH 0581/2176] fix --- test/bvproblem.jl | 44 ++++++++++++++++++++++---------------------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/test/bvproblem.jl b/test/bvproblem.jl index e6645c060d..a8de6af44d 100644 --- a/test/bvproblem.jl +++ b/test/bvproblem.jl @@ -7,7 +7,7 @@ using ModelingToolkit: t_nounits as t, D_nounits as D import ModelingToolkit: process_constraints ### Test Collocation solvers on simple problems -solvers = [MIRK4, RadauIIa5, LobattoIIIa3] +solvers = [MIRK4, RadauIIa5] daesolvers = [Ascher2, Ascher4, Ascher6] let @@ -149,32 +149,32 @@ let bvpi3 = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}(lksys, [x(t) => 1.], tspan; guesses = [y(t) => 1.], constraints) bvpi4 = SciMLBase.BVProblem{true, SciMLBase.FullSpecialize}(lksys, [x(t) => 1.], tspan; guesses = [y(t) => 1.], constraints) - @btime sol1 = solve(bvpi1, MIRK4(), dt = 0.01) - @btime sol2 = solve(bvpi2, MIRK4(), dt = 0.01) - @btime sol3 = solve(bvpi3, MIRK4(), dt = 0.01) - @btime sol4 = solve(bvpi4, MIRK4(), dt = 0.01) + sol1 = @btime solve($bvpi1, MIRK4(), dt = 0.01) + sol2 = @btime solve($bvpi2, MIRK4(), dt = 0.01) + sol3 = @btime solve($bvpi3, MIRK4(), dt = 0.01) + sol4 = @btime solve($bvpi4, MIRK4(), dt = 0.01) @test sol1 ≈ sol2 ≈ sol3 ≈ sol4 # don't get true equality here, not sure why bvpo1 = BVProblem(lotkavolterra, bc, [1,2], tspan, p) bvpo2 = BVProblem(lotkavolterra, genbc_oop, [1,2], tspan, p) - bvpo3 = SciMLBase.BVProblem{false, SciMLBase.FullSpecialize}(lksys, [x(t) => 1.], tspan, parammap; guesses = [y(t) => 1.], constraints) + bvpo3 = SciMLBase.BVProblem{false, SciMLBase.FullSpecialize}(lksys, [x(t) => 1.], tspan; guesses = [y(t) => 1.], constraints) - @btime sol1 = solve(bvpo1, MIRK4(), dt = 0.05) - @btime sol2 = solve(bvpo2, MIRK4(), dt = 0.05) - @btime sol3 = solve(bvpo3, MIRK4(), dt = 0.05) + sol1 = @btime solve($bvpo1, MIRK4(), dt = 0.05) + sol2 = @btime solve($bvpo2, MIRK4(), dt = 0.05) + sol3 = @btime solve($bvpo3, MIRK4(), dt = 0.05) @test sol1 ≈ sol2 ≈ sol3 end function test_solvers(solvers, prob, u0map, constraints, equations = []; dt = 0.05, atol = 1e-4) for solver in solvers println("Solver: $solver") - @btime sol = solve(prob, solver(), dt = dt, abstol = atol) + sol = @btime solve($prob, $solver(), dt = $dt, abstol = $atol) @test SciMLBase.successful_retcode(sol.retcode) p = prob.p; t = sol.t; bc = prob.f.bc ns = length(prob.u0) if isinplace(bvp.f) resid = zeros(ns) - bc!(resid, sol, p, t) + bc(resid, sol, p, t) @test isapprox(zeros(ns), resid; atol) else @test isapprox(zeros(ns), bc(sol, p, t); atol) @@ -203,35 +203,35 @@ let D(y(t)) ~ -γ * y(t) + δ * x(t) * y(t)] u0map = [] - tspan = (0.0, 10.0) + tspan = (0.0, 1.0) guesses = [x(t) => 4.0, y(t) => 2.] @mtkbuild lksys = ODESystem(eqs, t) - constraints = [x(6.) ~ 3.5, x(3.) ~ 7.] + constraints = [x(.6) ~ 3.5, x(.3) ~ 7.] bvp = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}(lksys, u0map, tspan; guesses, constraints) - test_solvers(solvers, bvp, u0map, constraints; dt = 0.1) + test_solvers(solvers, bvp, u0map, constraints; dt = 0.05) # Testing that more complicated constraints give correct solutions. - constraints = [y(2.) + x(8.) ~ 2.] - bvp = SciMLBase.BVProblem{false, SciMLBase.FullSpecialize}(lotkavolterra, u0map, tspan, parammap; guesses, constraints) - test_solvers(solvers, bvp, u0map, constraints) + constraints = [y(.2) + x(.8) ~ 3., y(.3) + x(.5) ~ 5.] + bvp = SciMLBase.BVProblem{false, SciMLBase.FullSpecialize}(lksys, u0map, tspan; guesses, constraints, jac = true) + test_solvers(solvers, bvp, u0map, constraints; dt = 0.05) - constraints = [α * β - x(6.) ~ 0.5] - bvp = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}(lotkavolterra, u0map, tspan, parammap; guesses, constraints) + constraints = [α * β - x(.6) ~ 0.0, y(.2) ~ 3.] + bvp = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}(lksys, u0map, tspan; guesses, constraints) test_solvers(solvers, bvp, u0map, constraints) # Testing that errors are properly thrown when malformed constraints are given. @variables bad(..) constraints = [x(1.) + bad(3.) ~ 10] - @test_throws Exception bvp = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}(lotkavolterra, u0map, tspan, parammap; guesses, constraints) + @test_throws Exception bvp = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}(lksys, u0map, tspan; guesses, constraints) constraints = [x(t) + y(t) ~ 3] - @test_throws Exception bvp = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}(lotkavolterra, u0map, tspan, parammap; guesses, constraints) + @test_throws Exception bvp = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}(lksys, u0map, tspan; guesses, constraints) @parameters bad2 constraints = [bad2 + x(0.) ~ 3] - @test_throws Exception bvp = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}(lotkavolterra, u0map, tspan, parammap; guesses, constraints) + @test_throws Exception bvp = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}(lksys, u0map, tspan; guesses, constraints) end # Cartesian pendulum from the docs. From 143d5c3c0c04b9713644ae989f6b691a427b7d86 Mon Sep 17 00:00:00 2001 From: vyudu Date: Thu, 16 Jan 2025 11:05:21 -0500 Subject: [PATCH 0582/2176] init --- src/systems/abstractsystem.jl | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index cbd07ee9bf..fe2978e28b 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -1010,7 +1010,9 @@ for prop in [:eqs :tstops :index_cache :is_scalar_noise - :isscheduled] + :isscheduled + :input + :misc] fname_get = Symbol(:get_, prop) fname_has = Symbol(:has_, prop) @eval begin From 80b25491f749bf4de1837f784ce0a6f62911beed Mon Sep 17 00:00:00 2001 From: vyudu Date: Thu, 16 Jan 2025 14:51:47 -0500 Subject: [PATCH 0583/2176] add getters for noise, unit, connect, misc --- src/ModelingToolkit.jl | 4 +- src/variables.jl | 93 +++++++++++++++++++++++++++++----- test/test_variable_metadata.jl | 44 ++++++++++++++++ 3 files changed, 128 insertions(+), 13 deletions(-) diff --git a/src/ModelingToolkit.jl b/src/ModelingToolkit.jl index 743dc8ce76..cf3ca5ada0 100644 --- a/src/ModelingToolkit.jl +++ b/src/ModelingToolkit.jl @@ -246,7 +246,9 @@ export initial_state, transition, activeState, entry, ticksInState, timeInState export @component, @mtkmodel, @mtkbuild export isinput, isoutput, getbounds, hasbounds, getguess, hasguess, isdisturbance, istunable, getdist, hasdist, - tunable_parameters, isirreducible, getdescription, hasdescription + tunable_parameters, isirreducible, getdescription, hasdescription, + hasnoise, getnoise, hasunit, getunit, hasconnect, getconnect, + hasmisc, getmisc export ode_order_lowering, dae_order_lowering, liouville_transform export PDESystem export Differential, expand_derivatives, @derivatives diff --git a/src/variables.jl b/src/variables.jl index c45c4b0f00..dab24dbbd1 100644 --- a/src/variables.jl +++ b/src/variables.jl @@ -29,7 +29,7 @@ ModelingToolkit.dump_variable_metadata(p) """ function dump_variable_metadata(var) uvar = unwrap(var) - vartype, name = get(uvar.metadata, VariableSource, (:unknown, :unknown)) + vartype, name = Symbolics.getmetadata(uvar, VariableSource, (:unknown, :unknown)) type = symtype(uvar) if type <: AbstractArray shape = Symbolics.shape(var) @@ -39,14 +39,14 @@ function dump_variable_metadata(var) else shape = nothing end - unit = get(uvar.metadata, VariableUnit, nothing) - connect = get(uvar.metadata, VariableConnectType, nothing) - noise = get(uvar.metadata, VariableNoiseType, nothing) + unit = getunit(uvar) + connect = getconnect(uvar) + noise = getnoise(uvar) input = isinput(uvar) || nothing output = isoutput(uvar) || nothing - irreducible = get(uvar.metadata, VariableIrreducible, nothing) - state_priority = get(uvar.metadata, VariableStatePriority, nothing) - misc = get(uvar.metadata, VariableMisc, nothing) + irreducible = isirreducible(var) + state_priority = Symbolics.getmetadata(uvar, VariableStatePriority, nothing) + misc = getmisc(uvar) bounds = hasbounds(uvar) ? getbounds(uvar) : nothing desc = getdescription(var) if desc == "" @@ -57,12 +57,13 @@ function dump_variable_metadata(var) disturbance = isdisturbance(uvar) || nothing tunable = istunable(uvar, isparameter(uvar)) dist = getdist(uvar) - type = symtype(uvar) + variable_type = getvariabletype(uvar) meta = ( var = var, vartype, name, + variable_type, shape, unit, connect, @@ -85,11 +86,28 @@ function dump_variable_metadata(var) return NamedTuple(k => v for (k, v) in pairs(meta) if v !== nothing) end +### Connect abstract type AbstractConnectType end struct Equality <: AbstractConnectType end # Equality connection struct Flow <: AbstractConnectType end # sum to 0 struct Stream <: AbstractConnectType end # special stream connector +""" + getconnect(x) + +Get the connect type of x. See also [`hasconnect`](@ref). +""" +getconnect(x) = getconnect(unwrap(x)) +getconnect(x::Symbolic) = Symbolics.getmetadata(x, VariableConnectType, nothing) +""" + hasconnect(x) + +Determine whether variable `x` has a connect type. See also [`getconnect`](@ref). +""" +hasconnect(x) = getconnect(x) !== nothing +setconnect(x, t::Type{T}) where T <: AbstractConnectType = setmetadata(x, VariableConnectType, t) + +### Input, Output, Irreducible isvarkind(m, x::Union{Num, Symbolics.Arr}) = isvarkind(m, value(x)) function isvarkind(m, x) iskind = getmetadata(x, m, nothing) @@ -98,15 +116,17 @@ function isvarkind(m, x) getmetadata(x, m, false) end -setinput(x, v) = setmetadata(x, VariableInput, v) -setoutput(x, v) = setmetadata(x, VariableOutput, v) -setio(x, i, o) = setoutput(setinput(x, i), o) +setinput(x, v::Bool) = setmetadata(x, VariableInput, v) +setoutput(x, v::Bool) = setmetadata(x, VariableOutput, v) +setio(x, i::Bool, o::Bool) = setoutput(setinput(x, i), o) + isinput(x) = isvarkind(VariableInput, x) isoutput(x) = isvarkind(VariableOutput, x) + # Before the solvability check, we already have handled IO variables, so # irreducibility is independent from IO. isirreducible(x) = isvarkind(VariableIrreducible, x) -setirreducible(x, v) = setmetadata(x, VariableIrreducible, v) +setirreducible(x, v::Bool) = setmetadata(x, VariableIrreducible, v) state_priority(x) = convert(Float64, getmetadata(x, VariableStatePriority, 0.0))::Float64 function default_toterm(x) @@ -545,3 +565,52 @@ function get_default_or_guess(x) return getguess(x) end end + +## Miscellaneous metadata ====================================================================== +""" + getmisc(x) + +Fetch any miscellaneous data associated with symbolic variable `x`. +See also [`hasmisc(x)`](@ref). +""" +getmisc(x) = getmisc(unwrap(x)) +getmisc(x::Symbolic) = Symbolics.getmetadata(x, VariableMisc, nothing) +""" + hasmisc(x) + +Determine whether a symbolic variable `x` has misc +metadata associated with it. + +See also [`getmisc(x)`](@ref). +""" +hasmisc(x) = getmisc(x) !== nothing +setmisc(x, miscdata) = setmetadata(x, VariableMisc, miscdata) + +## Units ====================================================================== +""" + getunit(x) + +Alias for [`get_unit(x)`](@ref). +""" +getunit(x) = get_unit(x) +""" + hasunit(x) + +Check if the variable `x` has a unit. +""" +hasunit(x) = getunit(x) !== nothing + +## Noise ====================================================================== +""" + getnoise(x) + +Get the noise type of variable `x`. +""" +getnoise(x) = getnoise(unwrap(x)) +getnoise(x::Symbolic) = Symbolics.getmetadata(x, VariableNoiseType, nothing) +""" + hasnoise(x) + +Determine if variable `x` has a noise type. +""" +hasnoise(x) = getnoise(x) !== nothing diff --git a/test/test_variable_metadata.jl b/test/test_variable_metadata.jl index 7b093be366..106bce1166 100644 --- a/test/test_variable_metadata.jl +++ b/test/test_variable_metadata.jl @@ -1,4 +1,5 @@ using ModelingToolkit +using DynamicQuantities # Bounds @variables u [bounds = (-1, 1)] @@ -185,3 +186,46 @@ params_meta = ModelingToolkit.dump_parameters(sys) params_meta = Dict([ModelingToolkit.getname(meta.var) => meta for meta in params_meta]) @test params_meta[:p].default == 3.0 @test isequal(params_meta[:q].dependency, 2p) + +# Noise +@variables x [noise = 1] +@test hasnoise(x) +@test getnoise(x) == 1 +@test ModelingToolkit.dump_variable_metadata(x).noise == 1 + +# Connect +@variables x [connect = Flow] +@test hasconnect(x) +@test getconnect(x) == Flow +@test ModelingToolkit.dump_variable_metadata(x).connect == Flow +x = ModelingToolkit.setconnect(x, ModelingToolkit.Stream) +@test getconnect(x) == ModelingToolkit.Stream + +struct BadConnect end +@test_throws Exception ModelingToolkit.setconnect(x, BadConnect) + +# Unit +@variables x [unit = u"s"] +@test hasunit(x) +@test getunit(x) == u"s" +@test ModelingToolkit.dump_variable_metadata(x).unit == u"s" + +# Misc data +@variables x [misc = [:good]] +@test hasmisc(x) +@test getmisc(x) == [:good] +x = ModelingToolkit.setmisc(x, "okay") +@test getmisc(x) == "okay" + +# Variable Type +@variables x +@test ModelingToolkit.getvariabletype(x) == ModelingToolkit.VARIABLE +@test ModelingToolkit.dump_variable_metadata(x).variable_type == ModelingToolkit.VARIABLE +x = ModelingToolkit.toparam(x) +@test ModelingToolkit.getvariabletype(x) == ModelingToolkit.PARAMETER + +@parameters y +@test ModelingToolkit.getvariabletype(y) == ModelingToolkit.PARAMETER + +@brownian z +@test ModelingToolkit.getvariabletype(z) == ModelingToolkit.BROWNIAN From a5db689b7892043e4492240f340fc07bb81bf2bb Mon Sep 17 00:00:00 2001 From: vyudu Date: Thu, 16 Jan 2025 14:52:44 -0500 Subject: [PATCH 0584/2176] up --- src/systems/abstractsystem.jl | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index fe2978e28b..cbd07ee9bf 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -1010,9 +1010,7 @@ for prop in [:eqs :tstops :index_cache :is_scalar_noise - :isscheduled - :input - :misc] + :isscheduled] fname_get = Symbol(:get_, prop) fname_has = Symbol(:has_, prop) @eval begin From 8e4cb3ca3be7f89247762109a73c44678f29e82d Mon Sep 17 00:00:00 2001 From: sivasathyaseeelan Date: Fri, 17 Jan 2025 01:51:37 +0530 Subject: [PATCH 0585/2176] added test for dae_order_lowering Signed-off-by: sivasathyaseeelan --- test/odesystem.jl | 57 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/test/odesystem.jl b/test/odesystem.jl index 85d135b338..76c47a8f1d 100644 --- a/test/odesystem.jl +++ b/test/odesystem.jl @@ -1552,3 +1552,60 @@ end expected_tstops = unique!(sort!(vcat(0.0:0.075:10.0, 0.1, 0.2, 0.65, 0.35, 0.45))) @test all(x -> any(isapprox(x, atol = 1e-6), sol2.t), expected_tstops) end + +@testset "dae_order_lowering basic test" begin + @parameters a + @variables x(t) y(t) z(t) + @named dae_sys = ODESystem([ + D(x) ~ y, + 0 ~ x + z, + 0 ~ x - y + z + ], t, [z, y, x], []) + + lowered_dae_sys = dae_order_lowering(dae_sys) + @variables x1(t) y1(t) z1(t) + expected_eqs = [ + 0 ~ x + z, + 0 ~ x - y + z, + Differential(t)(x) ~ y + ] + lowered_eqs = equations(lowered_dae_sys) + sorted_lowered_eqs = sort(lowered_eqs, by=string) + sorted_expected_eqs = sort(expected_eqs, by=string) + @test sorted_lowered_eqs == sorted_expected_eqs + + expected_vars = Set([z, y, x]) + lowered_vars = Set(unknowns(lowered_dae_sys)) + @test lowered_vars == expected_vars +end + +@testset "dae_order_lowering test with structural_simplify" begin + @variables x(t) y(t) z(t) + @parameters M b k + eqs = [ + D(D(x)) ~ -b / M * D(x) - k / M * x, + 0 ~ y - D(x), + 0 ~ z - x + ] + ps = [M, b, k] + default_u0 = [ + D(x) => 0.0, x => 10.0, y => 0.0, z => 10.0 + ] + default_p = [M => 1.0, b => 1.0, k => 1.0] + @named dae_sys = ODESystem(eqs, t, [x, y, z], ps; defaults = [default_u0; default_p]) + + simplified_dae_sys = structural_simplify(dae_sys) + + lowered_dae_sys = dae_order_lowering(simplified_dae_sys) + lowered_dae_sys = complete(lowered_dae_sys) + + tspan = (0.0, 10.0) + prob = ODEProblem(lowered_dae_sys, nothing, tspan) + sol = solve(prob, Tsit5()) + + @test sol.t[end] == tspan[end] + @test sum(abs, sol.u[end]) < 1 + + prob = ODEProblem{false}(lowered_dae_sys; u0_constructor = x -> SVector(x...)) + @test prob.u0 isa SVector +end \ No newline at end of file From 55e607bc8bb3742137a0c950782d74b6465f9706 Mon Sep 17 00:00:00 2001 From: Herman Sletmoen Date: Fri, 17 Jan 2025 13:27:05 +0100 Subject: [PATCH 0586/2176] Substitute dummy derivatives in initialization problem --- src/systems/diffeqs/abstractodesystem.jl | 1 + 1 file changed, 1 insertion(+) diff --git a/src/systems/diffeqs/abstractodesystem.jl b/src/systems/diffeqs/abstractodesystem.jl index b012bc73d4..c4da03e916 100644 --- a/src/systems/diffeqs/abstractodesystem.jl +++ b/src/systems/diffeqs/abstractodesystem.jl @@ -1366,6 +1366,7 @@ function InitializationProblem{iip, specialize}(sys::AbstractSystem, end u0map = merge(ModelingToolkit.guesses(sys), todict(guesses), todict(u0map)) + u0map = Dict(diff2term(var) => val for (var, val) in u0map) # replace D(x) -> x_t etc. fullmap = merge(u0map, parammap) u0T = Union{} for sym in unknowns(isys) From 94ff612da58463ae09586aa052c755e7c9724f9a Mon Sep 17 00:00:00 2001 From: Herman Sletmoen Date: Fri, 17 Jan 2025 14:13:20 +0100 Subject: [PATCH 0587/2176] Add test --- test/extensions/ad.jl | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/test/extensions/ad.jl b/test/extensions/ad.jl index 954b868a1e..75ec3ef753 100644 --- a/test/extensions/ad.jl +++ b/test/extensions/ad.jl @@ -4,6 +4,7 @@ using Zygote using SymbolicIndexingInterface using SciMLStructures using OrdinaryDiffEq +using NonlinearSolve using SciMLSensitivity using ForwardDiff using ChainRulesCore @@ -103,3 +104,22 @@ vals = (1.0f0, 3ones(Float32, 3)) tangent = rand_tangent(ps) fwd, back = ChainRulesCore.rrule(remake_buffer, sys, ps, idxs, vals) @inferred back(tangent) + +@testset "Dual type promotion in remake with dummy derivatives" begin # https://github.com/SciML/ModelingToolkit.jl/issues/3336 + # Throw ball straight up into the air + @variables y(t) + eqs = [D(D(y)) ~ -9.81] + initialization_eqs = [y^2 ~ 0] # initialize y = 0 in a way that builds an initialization problem + @named sys = ODESystem(eqs, t; initialization_eqs) + sys = structural_simplify(sys) + + # Find initial throw velocity that reaches exactly 10 m after 1 s + dprob0 = ODEProblem(sys, [D(y) => NaN], (0.0, 1.0), []; guesses = [y => 0.0]) + nprob = NonlinearProblem((ics, _) -> begin + dprob = remake(dprob0, u0 = Dict(D(y) => ics[1])) + dsol = solve(dprob, Tsit5()) + return [dsol[y][end] - 10.0] + end, [1.0]) + nsol = solve(nprob, NewtonRaphson()) + @test nsol[1] ≈ 10.0/1.0 + 9.81*1.0/2 # anal free fall solution is y = v0*t - g*t^2/2 -> v0 = y/t + g*t/2 +end From 6bd9ad4cd7a4befd1f54b56da1b83707b77b89b6 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 16 Jan 2025 13:28:20 +0530 Subject: [PATCH 0588/2176] fix: fix `generate_initializesystem(::JumpSystem)` --- src/systems/nonlinear/initializesystem.jl | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/systems/nonlinear/initializesystem.jl b/src/systems/nonlinear/initializesystem.jl index c3bbd92317..0b556eef68 100644 --- a/src/systems/nonlinear/initializesystem.jl +++ b/src/systems/nonlinear/initializesystem.jl @@ -13,7 +13,9 @@ function generate_initializesystem(sys::AbstractSystem; check_units = true, check_defguess = false, name = nameof(sys), extra_metadata = (;), kwargs...) eqs = equations(sys) - eqs = filter(x -> x isa Equation, eqs) + if !(eqs isa Vector{Equation}) + eqs = Equation[x for x in eqs if x isa Equation] + end trueobs, eqs = unhack_observed(observed(sys), eqs) vars = unique([unknowns(sys); getfield.(trueobs, :lhs)]) vars_set = Set(vars) # for efficient in-lookup From 3ea956cd2d6e144a2574a2de5d9d22de90498c62 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 16 Jan 2025 13:28:49 +0530 Subject: [PATCH 0589/2176] fix: retain `initialization_data` in `DiscreteProblem` --- src/systems/jumps/jumpsystem.jl | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/systems/jumps/jumpsystem.jl b/src/systems/jumps/jumpsystem.jl index 5c0a61771a..f39b5fa46a 100644 --- a/src/systems/jumps/jumpsystem.jl +++ b/src/systems/jumps/jumpsystem.jl @@ -425,14 +425,15 @@ function DiffEqBase.DiscreteProblem(sys::JumpSystem, u0map, tspan::Union{Tuple, error("The passed in JumpSystem contains `Equation`s or continuous events, please use a problem type that supports these features, such as ODEProblem.") end - _, u0, p = process_SciMLProblem(EmptySciMLFunction, sys, u0map, parammap; + _f, u0, p = process_SciMLProblem(EmptySciMLFunction, sys, u0map, parammap; t = tspan === nothing ? nothing : tspan[1], use_union, tofloat = false, check_length = false) f = DiffEqBase.DISCRETE_INPLACE_DEFAULT observedfun = ObservedFunctionCache( sys; eval_expression, eval_module, checkbounds = get(kwargs, :checkbounds, false)) - df = DiscreteFunction{true, true}(f; sys = sys, observed = observedfun) + df = DiscreteFunction{true, true}(f; sys = sys, observed = observedfun, + initialization_data = get(_f.kwargs, :initialization_data, nothing)) DiscreteProblem(df, u0, tspan, p; kwargs...) end From 9a78fe67cdf74564247a215fd5ccbbe8b834e8c0 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 16 Jan 2025 13:29:04 +0530 Subject: [PATCH 0590/2176] test: test initialization of `DiscreteProblem(::JumpSystem)` --- test/initializationsystem.jl | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/test/initializationsystem.jl b/test/initializationsystem.jl index 987c2e091a..7d2f2ed22a 100644 --- a/test/initializationsystem.jl +++ b/test/initializationsystem.jl @@ -1306,3 +1306,24 @@ end @test integ[X] ≈ 4.0 @test integ[Y] ≈ 7.0 end + +@testset "Issue#3297: `generate_initializesystem(::JumpSystem)`" begin + @parameters β γ S0 + @variables S(t)=S0 I(t) R(t) + rate₁ = β * S * I + affect₁ = [S ~ S - 1, I ~ I + 1] + rate₂ = γ * I + affect₂ = [I ~ I - 1, R ~ R + 1] + j₁ = ConstantRateJump(rate₁, affect₁) + j₂ = ConstantRateJump(rate₂, affect₂) + j₃ = MassActionJump(2 * β + γ, [R => 1], [S => 1, R => -1]) + @mtkbuild js = JumpSystem([j₁, j₂, j₃], t, [S, I, R], [β, γ, S0]) + + u0s = [I => 1, R => 0] + ps = [S0 => 999, β => 0.01, γ => 0.001] + dprob = DiscreteProblem(js, u0s, (0.0, 10.0), ps) + @test dprob.f.initialization_data !== nothing + sol = solve(dprob, FunctionMap()) + @test sol[S, 1] ≈ 999 + @test SciMLBase.successful_retcode(sol) +end From c5b7a639ff5c127241d2444407b2c7d7429b6196 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 16 Jan 2025 17:33:51 +0530 Subject: [PATCH 0591/2176] test: add `JumpProblem` to initialization test --- test/initializationsystem.jl | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/test/initializationsystem.jl b/test/initializationsystem.jl index 7d2f2ed22a..62b6bba707 100644 --- a/test/initializationsystem.jl +++ b/test/initializationsystem.jl @@ -1,5 +1,5 @@ using ModelingToolkit, OrdinaryDiffEq, NonlinearSolve, Test -using StochasticDiffEq, DelayDiffEq, StochasticDelayDiffEq +using StochasticDiffEq, DelayDiffEq, StochasticDelayDiffEq, JumpProcesses using ForwardDiff using SymbolicIndexingInterface, SciMLStructures using SciMLStructures: Tunable @@ -1326,4 +1326,9 @@ end sol = solve(dprob, FunctionMap()) @test sol[S, 1] ≈ 999 @test SciMLBase.successful_retcode(sol) + + jprob = JumpProblem(js, dprob) + sol = solve(jprob, SSAStepper()) + @test sol[S, 1] ≈ 999 + @test SciMLBase.successful_retcode(sol) end From 29d1a18bacdd03708c2b3e33e37d22fdaf5a6cce Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 17 Jan 2025 16:42:43 +0530 Subject: [PATCH 0592/2176] build: bump SciMLBase and OrdinaryDiffEqCore compats --- Project.toml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Project.toml b/Project.toml index 93d535d085..7ccb4d2110 100644 --- a/Project.toml +++ b/Project.toml @@ -123,7 +123,7 @@ NonlinearSolve = "4.3" OffsetArrays = "1" OrderedCollections = "1" OrdinaryDiffEq = "6.82.0" -OrdinaryDiffEqCore = "1.13.0" +OrdinaryDiffEqCore = "1.15.0" OrdinaryDiffEqDefault = "1.2" OrdinaryDiffEqNonlinearSolve = "1.3.0" PrecompileTools = "1" @@ -132,7 +132,7 @@ RecursiveArrayTools = "3.26" Reexport = "0.2, 1" RuntimeGeneratedFunctions = "0.5.9" SCCNonlinearSolve = "1.0.0" -SciMLBase = "2.68.1" +SciMLBase = "2.71" SciMLStructures = "1.0" Serialization = "1" Setfield = "0.7, 0.8, 1" From fa0494683bc00b2b4346d2f6cfe5976841116597 Mon Sep 17 00:00:00 2001 From: vyudu Date: Fri, 17 Jan 2025 10:49:59 -0500 Subject: [PATCH 0593/2176] remove noise, document new getters/setters --- docs/src/basics/Variable_metadata.md | 33 +++++++++++++++++++++++++++- src/variables.jl | 22 ++----------------- test/test_variable_metadata.jl | 2 ++ 3 files changed, 36 insertions(+), 21 deletions(-) diff --git a/docs/src/basics/Variable_metadata.md b/docs/src/basics/Variable_metadata.md index e9bdabfe38..ecb94ea878 100644 --- a/docs/src/basics/Variable_metadata.md +++ b/docs/src/basics/Variable_metadata.md @@ -54,6 +54,10 @@ using ModelingToolkit: t_nounits as t, D_nounits as D @variables i(t) [connect = Flow] @variables k(t) [connect = Stream] +hasconnect(i) +``` +```@example connect +getconnect(k) ``` ## Input or output @@ -177,8 +181,35 @@ A variable can be marked `irreducible` to prevent it from being moved to an `observed` state. This forces the variable to be computed during solving so that it can be accessed in [callbacks](@ref events) -```julia +```@example metadata @variable important_value [irreducible = true] +isirreducible(important_value) +``` + +## Units + +Units for variables can be designated using symbolic metadata. For more information, please see the [model validation and units](@ref units) section of the docs. Note that `getunit` is equivalent to `get_unit`. + +```@example metadata +@variable speed [unit=u"m/s"] +hasunit(speed) +``` +```@example metadata +getunit(speed) +``` + +## Miscellaneous metadata + +User-defined metadata can be added using the `misc` metadata. This can be queried +using the `hasmisc` and `getmisc` functions. + +```@example metadata +@variables u [misc = :conserved_parameter] y [misc = [2, 4, 6]] +hasmisc(u) +``` + +```@example metadata +getmisc(y) ``` ## Additional functions diff --git a/src/variables.jl b/src/variables.jl index dab24dbbd1..9cd7e72c79 100644 --- a/src/variables.jl +++ b/src/variables.jl @@ -8,7 +8,6 @@ struct VariableStatePriority end struct VariableMisc end Symbolics.option_to_metadata_type(::Val{:unit}) = VariableUnit Symbolics.option_to_metadata_type(::Val{:connect}) = VariableConnectType -Symbolics.option_to_metadata_type(::Val{:noise}) = VariableNoiseType Symbolics.option_to_metadata_type(::Val{:input}) = VariableInput Symbolics.option_to_metadata_type(::Val{:output}) = VariableOutput Symbolics.option_to_metadata_type(::Val{:irreducible}) = VariableIrreducible @@ -29,7 +28,7 @@ ModelingToolkit.dump_variable_metadata(p) """ function dump_variable_metadata(var) uvar = unwrap(var) - vartype, name = Symbolics.getmetadata(uvar, VariableSource, (:unknown, :unknown)) + variable_source, name = Symbolics.getmetadata(uvar, VariableSource, (:unknown, :unknown)) type = symtype(uvar) if type <: AbstractArray shape = Symbolics.shape(var) @@ -41,7 +40,6 @@ function dump_variable_metadata(var) end unit = getunit(uvar) connect = getconnect(uvar) - noise = getnoise(uvar) input = isinput(uvar) || nothing output = isoutput(uvar) || nothing irreducible = isirreducible(var) @@ -61,13 +59,12 @@ function dump_variable_metadata(var) meta = ( var = var, - vartype, + variable_source, name, variable_type, shape, unit, connect, - noise, input, output, irreducible, @@ -599,18 +596,3 @@ getunit(x) = get_unit(x) Check if the variable `x` has a unit. """ hasunit(x) = getunit(x) !== nothing - -## Noise ====================================================================== -""" - getnoise(x) - -Get the noise type of variable `x`. -""" -getnoise(x) = getnoise(unwrap(x)) -getnoise(x::Symbolic) = Symbolics.getmetadata(x, VariableNoiseType, nothing) -""" - hasnoise(x) - -Determine if variable `x` has a noise type. -""" -hasnoise(x) = getnoise(x) !== nothing diff --git a/test/test_variable_metadata.jl b/test/test_variable_metadata.jl index 106bce1166..b6e05ce4e0 100644 --- a/test/test_variable_metadata.jl +++ b/test/test_variable_metadata.jl @@ -221,8 +221,10 @@ x = ModelingToolkit.setmisc(x, "okay") @variables x @test ModelingToolkit.getvariabletype(x) == ModelingToolkit.VARIABLE @test ModelingToolkit.dump_variable_metadata(x).variable_type == ModelingToolkit.VARIABLE +@test ModelingToolkit.dump_variable_metadata(x).variable_source == :variables x = ModelingToolkit.toparam(x) @test ModelingToolkit.getvariabletype(x) == ModelingToolkit.PARAMETER +@test ModelingToolkit.dump_variable_metadata(x).variable_source == :variables @parameters y @test ModelingToolkit.getvariabletype(y) == ModelingToolkit.PARAMETER From baea3814840a525200d50a9f84f8e96c7cccb24a Mon Sep 17 00:00:00 2001 From: vyudu Date: Fri, 17 Jan 2025 11:19:14 -0500 Subject: [PATCH 0594/2176] add check --- src/systems/diffeqs/odesystem.jl | 1 + src/systems/diffeqs/sdesystem.jl | 1 + src/systems/discrete_system/discrete_system.jl | 1 + src/systems/jumps/jumpsystem.jl | 1 + src/systems/nonlinear/nonlinearsystem.jl | 3 +++ src/systems/optimization/optimizationsystem.jl | 3 +++ src/systems/pde/pdesystem.jl | 3 +++ src/utils.jl | 9 +++++++++ 8 files changed, 22 insertions(+) diff --git a/src/systems/diffeqs/odesystem.jl b/src/systems/diffeqs/odesystem.jl index 61f16fd926..b31f39ec22 100644 --- a/src/systems/diffeqs/odesystem.jl +++ b/src/systems/diffeqs/odesystem.jl @@ -202,6 +202,7 @@ struct ODESystem <: AbstractODESystem check_parameters(ps, iv) check_equations(deqs, iv) check_equations(equations(cevents), iv) + check_var_types(ODESystem, dvs) end if checks == true || (checks & CheckUnits) > 0 u = __get_unit_type(dvs, ps, iv) diff --git a/src/systems/diffeqs/sdesystem.jl b/src/systems/diffeqs/sdesystem.jl index 366e8a6d09..eeedce1d46 100644 --- a/src/systems/diffeqs/sdesystem.jl +++ b/src/systems/diffeqs/sdesystem.jl @@ -170,6 +170,7 @@ struct SDESystem <: AbstractODESystem check_parameters(ps, iv) check_equations(deqs, iv) check_equations(neqs, dvs) + check_var_types(SDESystem, dvs) if size(neqs, 1) != length(deqs) throw(ArgumentError("Noise equations ill-formed. Number of rows must match number of drift equations. size(neqs,1) = $(size(neqs,1)) != length(deqs) = $(length(deqs))")) end diff --git a/src/systems/discrete_system/discrete_system.jl b/src/systems/discrete_system/discrete_system.jl index bd5c72eec7..fb6dfe0236 100644 --- a/src/systems/discrete_system/discrete_system.jl +++ b/src/systems/discrete_system/discrete_system.jl @@ -122,6 +122,7 @@ struct DiscreteSystem <: AbstractTimeDependentSystem check_independent_variables([iv]) check_variables(dvs, iv) check_parameters(ps, iv) + check_var_types(DiscreteSystem, dvs) end if checks == true || (checks & CheckUnits) > 0 u = __get_unit_type(dvs, ps, iv) diff --git a/src/systems/jumps/jumpsystem.jl b/src/systems/jumps/jumpsystem.jl index 5c0a61771a..e5dbb185e2 100644 --- a/src/systems/jumps/jumpsystem.jl +++ b/src/systems/jumps/jumpsystem.jl @@ -147,6 +147,7 @@ struct JumpSystem{U <: ArrayPartition} <: AbstractTimeDependentSystem check_independent_variables([iv]) check_variables(unknowns, iv) check_parameters(ps, iv) + check_var_types(JumpSystem, unknowns) end if checks == true || (checks & CheckUnits) > 0 u = __get_unit_type(unknowns, ps, iv) diff --git a/src/systems/nonlinear/nonlinearsystem.jl b/src/systems/nonlinear/nonlinearsystem.jl index 6b0b0cc759..687d55b9bb 100644 --- a/src/systems/nonlinear/nonlinearsystem.jl +++ b/src/systems/nonlinear/nonlinearsystem.jl @@ -115,6 +115,9 @@ struct NonlinearSystem <: AbstractTimeIndependentSystem tearing_state = nothing, substitutions = nothing, complete = false, index_cache = nothing, parent = nothing, isscheduled = false; checks::Union{Bool, Int} = true) + if checks == true || (checks & CheckComponents) > 0 + check_var_types(NonlinearSystem, unknowns) + end if checks == true || (checks & CheckUnits) > 0 u = __get_unit_type(unknowns, ps) check_units(u, eqs) diff --git a/src/systems/optimization/optimizationsystem.jl b/src/systems/optimization/optimizationsystem.jl index 860e063e35..de4696bfa2 100644 --- a/src/systems/optimization/optimizationsystem.jl +++ b/src/systems/optimization/optimizationsystem.jl @@ -73,6 +73,9 @@ struct OptimizationSystem <: AbstractOptimizationSystem gui_metadata = nothing, complete = false, index_cache = nothing, parent = nothing, isscheduled = false; checks::Union{Bool, Int} = true) + if checks == true || (checks & CheckComponents) > 0 + check_var_types(OptimizationSystem, unknowns) + end if checks == true || (checks & CheckUnits) > 0 u = __get_unit_type(unknowns, ps) unwrap(op) isa Symbolic && check_units(u, op) diff --git a/src/systems/pde/pdesystem.jl b/src/systems/pde/pdesystem.jl index eac540e401..e067f0e149 100644 --- a/src/systems/pde/pdesystem.jl +++ b/src/systems/pde/pdesystem.jl @@ -102,6 +102,9 @@ struct PDESystem <: ModelingToolkit.AbstractMultivariateSystem checks::Union{Bool, Int} = true, description = "", name) + if checks == true || (checks & CheckComponents) > 0 + check_var_types(PDESystem, dvs) + end if checks == true || (checks & CheckUnits) > 0 u = __get_unit_type(dvs, ivs, ps) check_units(u, eqs) diff --git a/src/utils.jl b/src/utils.jl index 5e4f0b52d2..7324a07115 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -155,6 +155,14 @@ function check_variables(dvs, iv) end end +function check_var_types(sys_type::Type{T}, dvs) where T <: AbstractSystem + any(u -> !(symtype(u) <: Number), dvs) && error("The type of unknown variables must be a numeric type.") + if sys_type == ODESystem || sys_type == SDESystem || sys_type == PDESystem + any(u -> !(symtype(u) <: Float), dvs) && error("The type of unknown variables for an SDESystem, PDESystem, or ODESystem must be a float.") + end + nothing +end + function check_lhs(eq::Equation, op, dvs::Set) v = unwrap(eq.lhs) _iszero(v) && return @@ -1182,3 +1190,4 @@ function guesses_from_metadata!(guesses, vars) guesses[vars[i]] = varguesses[i] end end + From 42f36c5876d27e6947f7ea5d94cfa21d279c80d2 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 17 Jan 2025 22:12:38 +0530 Subject: [PATCH 0595/2176] fix: respect `checkbounds` in `build_explicit_observed_function` --- src/systems/diffeqs/odesystem.jl | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/src/systems/diffeqs/odesystem.jl b/src/systems/diffeqs/odesystem.jl index 61f16fd926..d9e126baba 100644 --- a/src/systems/diffeqs/odesystem.jl +++ b/src/systems/diffeqs/odesystem.jl @@ -629,7 +629,15 @@ function build_explicit_observed_function(sys, ts; oop_fn = Func(args, [], pre(Let(obsexprs, return_value, - false))) |> array_wrapper[1] |> oop_mtkp_wrapper |> toexpr + false)), [Expr(:meta, :propagate_inbounds)]) |> array_wrapper[1] |> oop_mtkp_wrapper |> toexpr + + if !checkbounds + oop_fn.args[end] = quote + @inbounds begin + $(oop_fn.args[end]) + end + end + end oop_fn = expression ? oop_fn : eval_or_rgf(oop_fn; eval_expression, eval_module) if !isscalar @@ -639,6 +647,18 @@ function build_explicit_observed_function(sys, ts; wrap_code = mtkparams_wrapper .∘ array_wrapper .∘ wrap_assignments(isscalar, obsexprs), expression = Val{true})[2] + if !checkbounds + iip_fn.args[end] = quote + @inbounds begin + $(iip_fn.args[end]) + end + end + end + iip_fn.args[end] = quote + $(Expr(:meta, :propagate_inbounds)) + $(iip_fn.args[end]) + end + if !expression iip_fn = eval_or_rgf(iip_fn; eval_expression, eval_module) end From 74e9d56d083530dfefe86055cca3e5be2f6ab235 Mon Sep 17 00:00:00 2001 From: vyudu Date: Fri, 17 Jan 2025 11:44:57 -0500 Subject: [PATCH 0596/2176] up --- src/utils.jl | 2 +- test/odesystem.jl | 7 +++++++ test/sdesystem.jl | 9 +++++++++ 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/utils.jl b/src/utils.jl index 7324a07115..f66055a65c 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -158,7 +158,7 @@ end function check_var_types(sys_type::Type{T}, dvs) where T <: AbstractSystem any(u -> !(symtype(u) <: Number), dvs) && error("The type of unknown variables must be a numeric type.") if sys_type == ODESystem || sys_type == SDESystem || sys_type == PDESystem - any(u -> !(symtype(u) <: Float), dvs) && error("The type of unknown variables for an SDESystem, PDESystem, or ODESystem must be a float.") + any(u -> !(symtype(u) <: AbstractFloat), dvs) && error("The type of unknown variables for an SDESystem, PDESystem, or ODESystem must be a float.") end nothing end diff --git a/test/odesystem.jl b/test/odesystem.jl index 85d135b338..c354399c3a 100644 --- a/test/odesystem.jl +++ b/test/odesystem.jl @@ -1552,3 +1552,10 @@ end expected_tstops = unique!(sort!(vcat(0.0:0.075:10.0, 0.1, 0.2, 0.65, 0.35, 0.45))) @test all(x -> any(isapprox(x, atol = 1e-6), sol2.t), expected_tstops) end + +let + @parameters p d + @variables X(t)::Int64 + eq = D(X) ~ p - d*X + @test_throws Exception @mtkbuild osys = ODESystem([eq], t) +end diff --git a/test/sdesystem.jl b/test/sdesystem.jl index 8069581dcc..95a5358db6 100644 --- a/test/sdesystem.jl +++ b/test/sdesystem.jl @@ -868,3 +868,12 @@ end @test length(ModelingToolkit.get_noiseeqs(sys)) == 1 @test length(observed(sys)) == 1 end + +# Test validating types of states +let + @parameters p d + @variables X(t)::Int64 + @brownian z + eq2 = D(X) ~ p - d*X + z + @test_throws Exception @mtkbuild ssys = System([eq2], t) +end From 5ff099a89e6b8ae5129f0ca2e8a38fb76908de11 Mon Sep 17 00:00:00 2001 From: vyudu Date: Fri, 17 Jan 2025 11:58:05 -0500 Subject: [PATCH 0597/2176] document state priority --- docs/src/basics/Variable_metadata.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/docs/src/basics/Variable_metadata.md b/docs/src/basics/Variable_metadata.md index ecb94ea878..d8e0bfc2c5 100644 --- a/docs/src/basics/Variable_metadata.md +++ b/docs/src/basics/Variable_metadata.md @@ -186,6 +186,15 @@ it can be accessed in [callbacks](@ref events) isirreducible(important_value) ``` +## State Priority + +When a model is structurally simplified, the algorithm will try to ensure that the variables with higher state priority become states of the system. A variable's state priority is a number set using the `state_priority` metadata. + +```@example metadata +@variable important_dof [state_priority = 10] unimportant_dof [state_priority = 2] +state_priority(important_dof) +``` + ## Units Units for variables can be designated using symbolic metadata. For more information, please see the [model validation and units](@ref units) section of the docs. Note that `getunit` is equivalent to `get_unit`. From cb068e8fcd9e79fea502a9443acdf96d5d3b6c23 Mon Sep 17 00:00:00 2001 From: vyudu Date: Fri, 17 Jan 2025 12:16:34 -0500 Subject: [PATCH 0598/2176] remove noise --- src/ModelingToolkit.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ModelingToolkit.jl b/src/ModelingToolkit.jl index cf3ca5ada0..b4533f72a0 100644 --- a/src/ModelingToolkit.jl +++ b/src/ModelingToolkit.jl @@ -247,7 +247,7 @@ export @component, @mtkmodel, @mtkbuild export isinput, isoutput, getbounds, hasbounds, getguess, hasguess, isdisturbance, istunable, getdist, hasdist, tunable_parameters, isirreducible, getdescription, hasdescription, - hasnoise, getnoise, hasunit, getunit, hasconnect, getconnect, + hasunit, getunit, hasconnect, getconnect, hasmisc, getmisc export ode_order_lowering, dae_order_lowering, liouville_transform export PDESystem From 0d64d282d1c56bfff888e08cf1171f3dcafab4cb Mon Sep 17 00:00:00 2001 From: vyudu Date: Fri, 17 Jan 2025 12:24:42 -0500 Subject: [PATCH 0599/2176] up --- docs/src/basics/Variable_metadata.md | 2 +- src/variables.jl | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/src/basics/Variable_metadata.md b/docs/src/basics/Variable_metadata.md index d8e0bfc2c5..d36c91697e 100644 --- a/docs/src/basics/Variable_metadata.md +++ b/docs/src/basics/Variable_metadata.md @@ -197,7 +197,7 @@ state_priority(important_dof) ## Units -Units for variables can be designated using symbolic metadata. For more information, please see the [model validation and units](@ref units) section of the docs. Note that `getunit` is equivalent to `get_unit`. +Units for variables can be designated using symbolic metadata. For more information, please see the [model validation and units](@ref units) section of the docs. Note that `getunit` is not equivalent to `get_unit` - the former is a metadata getter for individual variables (and is provided so the same interface function for `unit` exists like other metadata), while the latter is used to handle more general symbolic expressions. ```@example metadata @variable speed [unit=u"m/s"] diff --git a/src/variables.jl b/src/variables.jl index 9cd7e72c79..2f33e2eb29 100644 --- a/src/variables.jl +++ b/src/variables.jl @@ -587,9 +587,9 @@ setmisc(x, miscdata) = setmetadata(x, VariableMisc, miscdata) """ getunit(x) -Alias for [`get_unit(x)`](@ref). +Fetch the unit associated with variable `x`. This function is a metadata getter for an individual variable, while `get_unit` is used for unit inference on more complicated sdymbolic expressions. """ -getunit(x) = get_unit(x) +getunit(x) = Symbolics.getmetadata(x, VariableUnit, nothing) """ hasunit(x) From f5937aeaeb40a1e15be7d08c8640f9a9a6dc3a53 Mon Sep 17 00:00:00 2001 From: Herman Sletmoen Date: Fri, 17 Jan 2025 14:58:18 +0100 Subject: [PATCH 0600/2176] Use schedule to replace dummy derivatives --- src/systems/diffeqs/abstractodesystem.jl | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/systems/diffeqs/abstractodesystem.jl b/src/systems/diffeqs/abstractodesystem.jl index c4da03e916..1417080587 100644 --- a/src/systems/diffeqs/abstractodesystem.jl +++ b/src/systems/diffeqs/abstractodesystem.jl @@ -1366,7 +1366,21 @@ function InitializationProblem{iip, specialize}(sys::AbstractSystem, end u0map = merge(ModelingToolkit.guesses(sys), todict(guesses), todict(u0map)) - u0map = Dict(diff2term(var) => val for (var, val) in u0map) # replace D(x) -> x_t etc. + + # Replace dummy derivatives in u0map: D(x) -> x_t etc. + if has_schedule(sys) + schedule = get_schedule(sys) + if !isnothing(schedule) + for (var, val) in u0map + dvar = get(schedule.dummy_sub, var, var) # with dummy derivatives + if dvar !== var # then replace it + delete!(u0map, var) + push!(u0map, dvar => val) + end + end + end + end + fullmap = merge(u0map, parammap) u0T = Union{} for sym in unknowns(isys) From 60e4723c5ea9102738e03527d2ac0109fee79ab3 Mon Sep 17 00:00:00 2001 From: Herman Sletmoen Date: Fri, 17 Jan 2025 15:01:31 +0100 Subject: [PATCH 0601/2176] Format --- test/extensions/ad.jl | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/test/extensions/ad.jl b/test/extensions/ad.jl index 75ec3ef753..7b5c939b0a 100644 --- a/test/extensions/ad.jl +++ b/test/extensions/ad.jl @@ -115,11 +115,12 @@ fwd, back = ChainRulesCore.rrule(remake_buffer, sys, ps, idxs, vals) # Find initial throw velocity that reaches exactly 10 m after 1 s dprob0 = ODEProblem(sys, [D(y) => NaN], (0.0, 1.0), []; guesses = [y => 0.0]) - nprob = NonlinearProblem((ics, _) -> begin + function f(ics, _) dprob = remake(dprob0, u0 = Dict(D(y) => ics[1])) dsol = solve(dprob, Tsit5()) return [dsol[y][end] - 10.0] - end, [1.0]) + end + nprob = NonlinearProblem(f, [1.0]) nsol = solve(nprob, NewtonRaphson()) - @test nsol[1] ≈ 10.0/1.0 + 9.81*1.0/2 # anal free fall solution is y = v0*t - g*t^2/2 -> v0 = y/t + g*t/2 + @test nsol[1] ≈ 10.0 / 1.0 + 9.81 * 1.0 / 2 # anal free fall solution is y = v0*t - g*t^2/2 -> v0 = y/t + g*t/2 end From 25f9ed84d0480298a8c9e60bff06922141bf661a Mon Sep 17 00:00:00 2001 From: vyudu Date: Fri, 17 Jan 2025 14:28:19 -0500 Subject: [PATCH 0602/2176] add test, fix --- src/systems/diffeqs/sdesystem.jl | 6 +----- test/sdesystem.jl | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/src/systems/diffeqs/sdesystem.jl b/src/systems/diffeqs/sdesystem.jl index 26f26b4263..fb2eb1d804 100644 --- a/src/systems/diffeqs/sdesystem.jl +++ b/src/systems/diffeqs/sdesystem.jl @@ -768,11 +768,7 @@ function DiffEqBase.SDEProblem{iip, specialize}( noise_rate_prototype = noise_rate_prototype, kwargs...) end -function DiffEqBase.SDEProblem{iip, specialize}( - sys::ODESystem, u0map = [], tspan = get_tspan(sys), - parammap = DiffEqBase.NullParameters(); - sparsenoise = nothing, check_length = true, - callback = nothing, kwargs...) where {iip, specialize} +function DiffEqBase.SDEProblem(sys::ODESystem, args...; kwargs...) if any(ModelingToolkit.isbrownian, unknowns(sys)) error("SDESystem constructed by defining Brownian variables with @brownian must be simplified by calling `structural_simplify` before a SDEProblem can be constructed.") diff --git a/test/sdesystem.jl b/test/sdesystem.jl index 8069581dcc..1a22a3501d 100644 --- a/test/sdesystem.jl +++ b/test/sdesystem.jl @@ -868,3 +868,24 @@ end @test length(ModelingToolkit.get_noiseeqs(sys)) == 1 @test length(observed(sys)) == 1 end +using ModelingToolkit, StochasticDiffEq +using ModelingToolkit: t_nounits as t, D_nounits as D + +@testset "Error when constructing SDESystem without `structural_simplify`" begin + @parameters σ ρ β + @variables x(t) y(t) z(t) + @brownian a + eqs = [D(x) ~ σ * (y - x) + 0.1a * x, + D(y) ~ x * (ρ - z) - y + 0.1a * y, + D(z) ~ x * y - β * z + 0.1a * z] + + @named de = System(eqs, t) + de = complete(de) + + u0map = [x => 1.0, y => 0.0, z => 0.0] + parammap = [σ => 10.0, β => 26.0, ρ => 2.33] + + @test_throws ErrorException("SDESystem constructed by defining Brownian variables with @brownian must be simplified by calling `structural_simplify` before a SDEProblem can be constructed.") SDEProblem(de, u0map, (0.0, 100.0), parammap) + de = structural_simplify(de) + @test SDEProblem(de, u0map, (0.0, 100.0), parammap) isa SDEProblem +end From 557b01fc6539b0ca09c8a3ffcad271ba74760287 Mon Sep 17 00:00:00 2001 From: vyudu Date: Fri, 17 Jan 2025 14:50:56 -0500 Subject: [PATCH 0603/2176] fix --- src/utils.jl | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/utils.jl b/src/utils.jl index f66055a65c..4742ed819f 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -156,9 +156,12 @@ function check_variables(dvs, iv) end function check_var_types(sys_type::Type{T}, dvs) where T <: AbstractSystem - any(u -> !(symtype(u) <: Number), dvs) && error("The type of unknown variables must be a numeric type.") - if sys_type == ODESystem || sys_type == SDESystem || sys_type == PDESystem - any(u -> !(symtype(u) <: AbstractFloat), dvs) && error("The type of unknown variables for an SDESystem, PDESystem, or ODESystem must be a float.") + if any(u -> !(symtype(u) <: Number), dvs) + error("The type of unknown variables must be a numeric type.") + elseif any(u -> (symtype(u) !== symtype(dvs[1])), dvs) + error("The type of all the unknown variables in a system must all be the same.") + elseif sys_type == ODESystem || sys_type == SDESystem || sys_type == PDESystem + any(u -> !(symtype(u) == Real), dvs) && error("The type of unknown variables for an SDESystem, PDESystem, or ODESystem should not be a concrete numeric type.") end nothing end From e12313e4201f878aa09b502d1dee2c79a9bb8130 Mon Sep 17 00:00:00 2001 From: vyudu Date: Fri, 17 Jan 2025 15:13:46 -0500 Subject: [PATCH 0604/2176] handle arr --- src/utils.jl | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/utils.jl b/src/utils.jl index 4742ed819f..736a4cc03e 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -156,12 +156,12 @@ function check_variables(dvs, iv) end function check_var_types(sys_type::Type{T}, dvs) where T <: AbstractSystem - if any(u -> !(symtype(u) <: Number), dvs) + if any(u -> !(symtype(u) <: Number || eltype(symtype(u)) <: Number), dvs) error("The type of unknown variables must be a numeric type.") - elseif any(u -> (symtype(u) !== symtype(dvs[1])), dvs) + elseif any(u -> (symtype(u) !== symtype(dvs[1])), dvs) error("The type of all the unknown variables in a system must all be the same.") elseif sys_type == ODESystem || sys_type == SDESystem || sys_type == PDESystem - any(u -> !(symtype(u) == Real), dvs) && error("The type of unknown variables for an SDESystem, PDESystem, or ODESystem should not be a concrete numeric type.") + any(u -> !(symtype(u) == Real || eltype(symtype(u)) == Real), dvs) && error("The type of unknown variables for an SDESystem, PDESystem, or ODESystem should not be a concrete numeric type.") end nothing end From 483c97267dbfa89165bec31982df66c3146c538d Mon Sep 17 00:00:00 2001 From: vyudu Date: Fri, 17 Jan 2025 15:18:40 -0500 Subject: [PATCH 0605/2176] fix test --- test/test_variable_metadata.jl | 6 ------ 1 file changed, 6 deletions(-) diff --git a/test/test_variable_metadata.jl b/test/test_variable_metadata.jl index b6e05ce4e0..8fca96a23d 100644 --- a/test/test_variable_metadata.jl +++ b/test/test_variable_metadata.jl @@ -187,12 +187,6 @@ params_meta = Dict([ModelingToolkit.getname(meta.var) => meta for meta in params @test params_meta[:p].default == 3.0 @test isequal(params_meta[:q].dependency, 2p) -# Noise -@variables x [noise = 1] -@test hasnoise(x) -@test getnoise(x) == 1 -@test ModelingToolkit.dump_variable_metadata(x).noise == 1 - # Connect @variables x [connect = Flow] @test hasconnect(x) From 86d4144d910c0ba56ec5057f7a380ccca5caf879 Mon Sep 17 00:00:00 2001 From: vyudu Date: Fri, 17 Jan 2025 16:49:58 -0500 Subject: [PATCH 0606/2176] test more solvers: --- src/systems/diffeqs/abstractodesystem.jl | 2 +- test/bvproblem.jl | 333 ++++++++++++----------- 2 files changed, 172 insertions(+), 163 deletions(-) diff --git a/src/systems/diffeqs/abstractodesystem.jl b/src/systems/diffeqs/abstractodesystem.jl index d935a94eb8..cf6e0962fd 100644 --- a/src/systems/diffeqs/abstractodesystem.jl +++ b/src/systems/diffeqs/abstractodesystem.jl @@ -969,7 +969,7 @@ function validate_constraint_syms(eq, constraintsts, constraintps, sts, ps, iv) for var in constraintsts if length(arguments(var)) > 1 error("Too many arguments for variable $var.") - elseif arguments(var) == iv + elseif isequal(arguments(var)[1], iv) var ∈ sts || error("Constraint equation $eq contains a variable $var that is not a variable of the ODESystem.") error("Constraint equation $eq contains a variable $var that does not have a specified argument. Such equations should be specified as algebraic equations to the ODESystem rather than a boundary constraints.") else diff --git a/test/bvproblem.jl b/test/bvproblem.jl index a8de6af44d..2f278e6135 100644 --- a/test/bvproblem.jl +++ b/test/bvproblem.jl @@ -1,183 +1,186 @@ ### TODO: update when BoundaryValueDiffEqAscher is updated to use the normal boundary condition conventions using BoundaryValueDiffEq, OrdinaryDiffEq, BoundaryValueDiffEqAscher +using BenchmarkTools using ModelingToolkit using SciMLBase using ModelingToolkit: t_nounits as t, D_nounits as D import ModelingToolkit: process_constraints ### Test Collocation solvers on simple problems -solvers = [MIRK4, RadauIIa5] +solvers = [MIRK4, RadauIIa5, LobattoIIIa3] daesolvers = [Ascher2, Ascher4, Ascher6] -let - @parameters α=7.5 β=4.0 γ=8.0 δ=5.0 - @variables x(t)=1.0 y(t)=2.0 - - eqs = [D(x) ~ α * x - β * x * y, - D(y) ~ -γ * y + δ * x * y] - - u0map = [x => 1.0, y => 2.0] - parammap = [α => 7.5, β => 4, γ => 8.0, δ => 5.0] - tspan = (0.0, 10.0) - - @mtkbuild lotkavolterra = ODESystem(eqs, t) - op = ODEProblem(lotkavolterra, u0map, tspan, parammap) - osol = solve(op, Vern9()) - - bvp = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}( - lotkavolterra, u0map, tspan, parammap; eval_expression = true) - - for solver in solvers - sol = solve(bvp, solver(), dt = 0.01) - @test isapprox(sol.u[end], osol.u[end]; atol = 0.01) - @test sol.u[1] == [1.0, 2.0] - end - - # Test out of place - bvp2 = SciMLBase.BVProblem{false, SciMLBase.AutoSpecialize}( - lotkavolterra, u0map, tspan, parammap; eval_expression = true) - - for solver in solvers - sol = solve(bvp2, solver(), dt = 0.01) - @test isapprox(sol.u[end], osol.u[end]; atol = 0.01) - @test sol.u[1] == [1.0, 2.0] - end -end - -### Testing on pendulum -let - @parameters g=9.81 L=1.0 - @variables θ(t) = π / 2 θ_t(t) - - eqs = [D(θ) ~ θ_t - D(θ_t) ~ -(g / L) * sin(θ)] - - @mtkbuild pend = ODESystem(eqs, t) - - u0map = [θ => π / 2, θ_t => π / 2] - parammap = [:L => 1.0, :g => 9.81] - tspan = (0.0, 6.0) - - op = ODEProblem(pend, u0map, tspan, parammap) - osol = solve(op, Vern9()) - - bvp = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}(pend, u0map, tspan, parammap) - for solver in solvers - sol = solve(bvp, solver(), dt = 0.01) - @test isapprox(sol.u[end], osol.u[end]; atol = 0.01) - @test sol.u[1] == [π / 2, π / 2] - end - - # Test out-of-place - bvp2 = SciMLBase.BVProblem{false, SciMLBase.FullSpecialize}(pend, u0map, tspan, parammap) - - for solver in solvers - sol = solve(bvp2, solver(), dt = 0.01) - @test isapprox(sol.u[end], osol.u[end]; atol = 0.01) - @test sol.u[1] == [π / 2, π / 2] - end -end - -################################################################## -### ODESystem with constraint equations, DAEs with constraints ### -################################################################## - -# Test generation of boundary condition function using `process_constraints`. Compare solutions to manually written boundary conditions -let - @parameters α=1.5 β=1.0 γ=3.0 δ=1.0 - @variables x(..) y(..) - eqs = [D(x(t)) ~ α * x(t) - β * x(t) * y(t), - D(y(t)) ~ -γ * y(t) + δ * x(t) * y(t)] - - tspan = (0., 1.) - @mtkbuild lksys = ODESystem(eqs, t) - - function lotkavolterra!(du, u, p, t) - du[1] = p[1]*u[1] - p[2]*u[1]*u[2] - du[2] = -p[4]*u[2] + p[3]*u[1]*u[2] - end - - function lotkavolterra(u, p, t) - [p[1]*u[1] - p[2]*u[1]*u[2], -p[4]*u[2] + p[3]*u[1]*u[2]] - end - # Compare the built bc function to the actual constructed one. - function bc!(resid, u, p, t) - resid[1] = u[1][1] - 1. - resid[2] = u[1][2] - 2. - nothing - end - function bc(u, p, t) - [u[1][1] - 1., u[1][2] - 2.] - end - - u0 = [1., 2.]; p = [1.5, 1., 1., 3.] - genbc_iip = ModelingToolkit.process_constraints(lksys, nothing, u0, [1, 2], tspan, true) - genbc_oop = ModelingToolkit.process_constraints(lksys, nothing, u0, [1, 2], tspan, false) - - bvpi1 = SciMLBase.BVProblem(lotkavolterra!, bc!, [1.,2.], tspan, p) - bvpi2 = SciMLBase.BVProblem(lotkavolterra!, genbc_iip, [1.,2.], tspan, p) - - sol1 = solve(bvpi1, MIRK4(), dt = 0.05) - sol2 = solve(bvpi2, MIRK4(), dt = 0.05) - @test sol1 ≈ sol2 - - bvpo1 = BVProblem(lotkavolterra, bc, [1,2], tspan, p) - bvpo2 = BVProblem(lotkavolterra, genbc_oop, [1,2], tspan, p) - - sol1 = solve(bvpo1, MIRK4(), dt = 0.05) - sol2 = solve(bvpo2, MIRK4(), dt = 0.05) - @test sol1 ≈ sol2 - - # Test with a constraint. - constraints = [y(0.5) ~ 2.] - - function bc!(resid, u, p, t) - resid[1] = u(0.0)[1] - 1. - resid[2] = u(0.5)[2] - 2. - end - function bc(u, p, t) - [u(0.0)[1] - 1., u(0.5)[2] - 2.] - end - - u0 = [1, 1.] - genbc_iip = ModelingToolkit.process_constraints(lksys, constraints, u0, [1], tspan, true) - genbc_oop = ModelingToolkit.process_constraints(lksys, constraints, u0, [1], tspan, false) - - bvpi1 = SciMLBase.BVProblem(lotkavolterra!, bc!, u0, tspan, p) - bvpi2 = SciMLBase.BVProblem(lotkavolterra!, genbc_iip, u0, tspan, p) - bvpi3 = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}(lksys, [x(t) => 1.], tspan; guesses = [y(t) => 1.], constraints) - bvpi4 = SciMLBase.BVProblem{true, SciMLBase.FullSpecialize}(lksys, [x(t) => 1.], tspan; guesses = [y(t) => 1.], constraints) - - sol1 = @btime solve($bvpi1, MIRK4(), dt = 0.01) - sol2 = @btime solve($bvpi2, MIRK4(), dt = 0.01) - sol3 = @btime solve($bvpi3, MIRK4(), dt = 0.01) - sol4 = @btime solve($bvpi4, MIRK4(), dt = 0.01) - @test sol1 ≈ sol2 ≈ sol3 ≈ sol4 # don't get true equality here, not sure why - - bvpo1 = BVProblem(lotkavolterra, bc, [1,2], tspan, p) - bvpo2 = BVProblem(lotkavolterra, genbc_oop, [1,2], tspan, p) - bvpo3 = SciMLBase.BVProblem{false, SciMLBase.FullSpecialize}(lksys, [x(t) => 1.], tspan; guesses = [y(t) => 1.], constraints) - - sol1 = @btime solve($bvpo1, MIRK4(), dt = 0.05) - sol2 = @btime solve($bvpo2, MIRK4(), dt = 0.05) - sol3 = @btime solve($bvpo3, MIRK4(), dt = 0.05) - @test sol1 ≈ sol2 ≈ sol3 -end +# let +# @parameters α=7.5 β=4.0 γ=8.0 δ=5.0 +# @variables x(t)=1.0 y(t)=2.0 +# +# eqs = [D(x) ~ α * x - β * x * y, +# D(y) ~ -γ * y + δ * x * y] +# +# u0map = [x => 1.0, y => 2.0] +# parammap = [α => 7.5, β => 4, γ => 8.0, δ => 5.0] +# tspan = (0.0, 10.0) +# +# @mtkbuild lotkavolterra = ODESystem(eqs, t) +# op = ODEProblem(lotkavolterra, u0map, tspan, parammap) +# osol = solve(op, Vern9()) +# +# bvp = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}( +# lotkavolterra, u0map, tspan, parammap; eval_expression = true) +# +# for solver in solvers +# sol = solve(bvp, solver(), dt = 0.01) +# @test isapprox(sol.u[end], osol.u[end]; atol = 0.01) +# @test sol.u[1] == [1.0, 2.0] +# end +# +# # Test out of place +# bvp2 = SciMLBase.BVProblem{false, SciMLBase.AutoSpecialize}( +# lotkavolterra, u0map, tspan, parammap; eval_expression = true) +# +# for solver in solvers +# sol = solve(bvp2, solver(), dt = 0.01) +# @test isapprox(sol.u[end], osol.u[end]; atol = 0.01) +# @test sol.u[1] == [1.0, 2.0] +# end +# end +# +# ### Testing on pendulum +# let +# @parameters g=9.81 L=1.0 +# @variables θ(t) = π / 2 θ_t(t) +# +# eqs = [D(θ) ~ θ_t +# D(θ_t) ~ -(g / L) * sin(θ)] +# +# @mtkbuild pend = ODESystem(eqs, t) +# +# u0map = [θ => π / 2, θ_t => π / 2] +# parammap = [:L => 1.0, :g => 9.81] +# tspan = (0.0, 6.0) +# +# op = ODEProblem(pend, u0map, tspan, parammap) +# osol = solve(op, Vern9()) +# +# bvp = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}(pend, u0map, tspan, parammap) +# for solver in solvers +# sol = solve(bvp, solver(), dt = 0.01) +# @test isapprox(sol.u[end], osol.u[end]; atol = 0.01) +# @test sol.u[1] == [π / 2, π / 2] +# end +# +# # Test out-of-place +# bvp2 = SciMLBase.BVProblem{false, SciMLBase.FullSpecialize}(pend, u0map, tspan, parammap) +# +# for solver in solvers +# sol = solve(bvp2, solver(), dt = 0.01) +# @test isapprox(sol.u[end], osol.u[end]; atol = 0.01) +# @test sol.u[1] == [π / 2, π / 2] +# end +# end +# +# ################################################################## +# ### ODESystem with constraint equations, DAEs with constraints ### +# ################################################################## +# +# # Test generation of boundary condition function using `process_constraints`. Compare solutions to manually written boundary conditions +# let +# @parameters α=1.5 β=1.0 γ=3.0 δ=1.0 +# @variables x(..) y(..) +# eqs = [D(x(t)) ~ α * x(t) - β * x(t) * y(t), +# D(y(t)) ~ -γ * y(t) + δ * x(t) * y(t)] +# +# tspan = (0., 1.) +# @mtkbuild lksys = ODESystem(eqs, t) +# +# function lotkavolterra!(du, u, p, t) +# du[1] = p[1]*u[1] - p[2]*u[1]*u[2] +# du[2] = -p[4]*u[2] + p[3]*u[1]*u[2] +# end +# +# function lotkavolterra(u, p, t) +# [p[1]*u[1] - p[2]*u[1]*u[2], -p[4]*u[2] + p[3]*u[1]*u[2]] +# end +# # Compare the built bc function to the actual constructed one. +# function bc!(resid, u, p, t) +# resid[1] = u[1][1] - 1. +# resid[2] = u[1][2] - 2. +# nothing +# end +# function bc(u, p, t) +# [u[1][1] - 1., u[1][2] - 2.] +# end +# +# u0 = [1., 2.]; p = [1.5, 1., 1., 3.] +# genbc_iip = ModelingToolkit.process_constraints(lksys, nothing, u0, [1, 2], tspan, true) +# genbc_oop = ModelingToolkit.process_constraints(lksys, nothing, u0, [1, 2], tspan, false) +# +# bvpi1 = SciMLBase.BVProblem(lotkavolterra!, bc!, [1.,2.], tspan, p) +# bvpi2 = SciMLBase.BVProblem(lotkavolterra!, genbc_iip, [1.,2.], tspan, p) +# +# sol1 = solve(bvpi1, MIRK4(), dt = 0.05) +# sol2 = solve(bvpi2, MIRK4(), dt = 0.05) +# @test sol1 ≈ sol2 +# +# bvpo1 = BVProblem(lotkavolterra, bc, [1,2], tspan, p) +# bvpo2 = BVProblem(lotkavolterra, genbc_oop, [1,2], tspan, p) +# +# sol1 = solve(bvpo1, MIRK4(), dt = 0.05) +# sol2 = solve(bvpo2, MIRK4(), dt = 0.05) +# @test sol1 ≈ sol2 +# +# # Test with a constraint. +# constraints = [y(0.5) ~ 2.] +# +# function bc!(resid, u, p, t) +# resid[1] = u(0.0)[1] - 1. +# resid[2] = u(0.5)[2] - 2. +# end +# function bc(u, p, t) +# [u(0.0)[1] - 1., u(0.5)[2] - 2.] +# end +# +# u0 = [1, 1.] +# genbc_iip = ModelingToolkit.process_constraints(lksys, constraints, u0, [1], tspan, true) +# genbc_oop = ModelingToolkit.process_constraints(lksys, constraints, u0, [1], tspan, false) +# +# bvpi1 = SciMLBase.BVProblem(lotkavolterra!, bc!, u0, tspan, p) +# bvpi2 = SciMLBase.BVProblem(lotkavolterra!, genbc_iip, u0, tspan, p) +# bvpi3 = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}(lksys, [x(t) => 1.], tspan; guesses = [y(t) => 1.], constraints) +# bvpi4 = SciMLBase.BVProblem{true, SciMLBase.FullSpecialize}(lksys, [x(t) => 1.], tspan; guesses = [y(t) => 1.], constraints) +# +# sol1 = @btime solve($bvpi1, MIRK4(), dt = 0.01) +# sol2 = @btime solve($bvpi2, MIRK4(), dt = 0.01) +# sol3 = @btime solve($bvpi3, MIRK4(), dt = 0.01) +# sol4 = @btime solve($bvpi4, MIRK4(), dt = 0.01) +# @test sol1 ≈ sol2 ≈ sol3 ≈ sol4 # don't get true equality here, not sure why +# +# bvpo1 = BVProblem(lotkavolterra, bc, u0, tspan, p) +# bvpo2 = BVProblem(lotkavolterra, genbc_oop, u0, tspan, p) +# bvpo3 = SciMLBase.BVProblem{false, SciMLBase.FullSpecialize}(lksys, [x(t) => 1.], tspan; guesses = [y(t) => 1.], constraints) +# +# sol1 = @btime solve($bvpo1, MIRK4(), dt = 0.05) +# sol2 = @btime solve($bvpo2, MIRK4(), dt = 0.05) +# sol3 = @btime solve($bvpo3, MIRK4(), dt = 0.05) +# @test sol1 ≈ sol2 ≈ sol3 +# end -function test_solvers(solvers, prob, u0map, constraints, equations = []; dt = 0.05, atol = 1e-4) +function test_solvers(solvers, prob, u0map, constraints, equations = []; dt = 0.05, atol = 1e-3) for solver in solvers println("Solver: $solver") sol = @btime solve($prob, $solver(), dt = $dt, abstol = $atol) @test SciMLBase.successful_retcode(sol.retcode) p = prob.p; t = sol.t; bc = prob.f.bc ns = length(prob.u0) - if isinplace(bvp.f) + if isinplace(prob.f) resid = zeros(ns) bc(resid, sol, p, t) @test isapprox(zeros(ns), resid; atol) + @show resid else @test isapprox(zeros(ns), bc(sol, p, t); atol) + @show bc(sol, p, t) end for (k, v) in u0map @@ -194,6 +197,12 @@ function test_solvers(solvers, prob, u0map, constraints, equations = []; dt = 0. end end +solvers = [RadauIIa3, RadauIIa5, RadauIIa7, + LobattoIIIa2, LobattoIIIa4, LobattoIIIa5, + LobattoIIIb2, LobattoIIIb3, LobattoIIIb4, LobattoIIIb5, + LobattoIIIc2, LobattoIIIc3, LobattoIIIc4, LobattoIIIc5] +weird = [MIRK2, MIRK5, RadauIIa2] +daesolvers = [] # Simple ODESystem with BVP constraints. let @parameters α=1.5 β=1.0 γ=3.0 δ=1.0 @@ -213,8 +222,8 @@ let test_solvers(solvers, bvp, u0map, constraints; dt = 0.05) # Testing that more complicated constraints give correct solutions. - constraints = [y(.2) + x(.8) ~ 3., y(.3) + x(.5) ~ 5.] - bvp = SciMLBase.BVProblem{false, SciMLBase.FullSpecialize}(lksys, u0map, tspan; guesses, constraints, jac = true) + constraints = [y(.2) + x(.8) ~ 3., y(.3) ~ 2.] + bvp = SciMLBase.BVProblem{false, SciMLBase.FullSpecialize}(lksys, u0map, tspan; guesses, constraints) test_solvers(solvers, bvp, u0map, constraints; dt = 0.05) constraints = [α * β - x(.6) ~ 0.0, y(.2) ~ 3.] @@ -224,14 +233,14 @@ let # Testing that errors are properly thrown when malformed constraints are given. @variables bad(..) constraints = [x(1.) + bad(3.) ~ 10] - @test_throws Exception bvp = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}(lksys, u0map, tspan; guesses, constraints) + @test_throws ErrorException bvp = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}(lksys, u0map, tspan; guesses, constraints) constraints = [x(t) + y(t) ~ 3] - @test_throws Exception bvp = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}(lksys, u0map, tspan; guesses, constraints) + @test_throws ErrorException bvp = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}(lksys, u0map, tspan; guesses, constraints) @parameters bad2 constraints = [bad2 + x(0.) ~ 3] - @test_throws Exception bvp = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}(lksys, u0map, tspan; guesses, constraints) + @test_throws ErrorException bvp = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}(lksys, u0map, tspan; guesses, constraints) end # Cartesian pendulum from the docs. From df0ee8ae6804ad85b85885e3e5de8540bdd7e097 Mon Sep 17 00:00:00 2001 From: vyudu Date: Fri, 17 Jan 2025 16:51:19 -0500 Subject: [PATCH 0607/2176] fix unit --- src/variables.jl | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/variables.jl b/src/variables.jl index 2f33e2eb29..049dac790b 100644 --- a/src/variables.jl +++ b/src/variables.jl @@ -589,7 +589,8 @@ setmisc(x, miscdata) = setmetadata(x, VariableMisc, miscdata) Fetch the unit associated with variable `x`. This function is a metadata getter for an individual variable, while `get_unit` is used for unit inference on more complicated sdymbolic expressions. """ -getunit(x) = Symbolics.getmetadata(x, VariableUnit, nothing) +getunit(x) = getunit(unwrap(x)) +getunit(x::Symbolic) = Symbolics.getmetadata(x, VariableUnit, nothing) """ hasunit(x) From b4d9a627b333236e80d238dea9389f469e76acc8 Mon Sep 17 00:00:00 2001 From: vyudu Date: Fri, 17 Jan 2025 16:55:47 -0500 Subject: [PATCH 0608/2176] fix SDE test --- test/sdesystem.jl | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/test/sdesystem.jl b/test/sdesystem.jl index 7ff7d5e7c8..53466768f0 100644 --- a/test/sdesystem.jl +++ b/test/sdesystem.jl @@ -875,22 +875,22 @@ let @parameters p d @brownian a seq = D(X) ~ p - d*X + a - @mtkbuild ssys1 = System([eq], t; name = :ssys) - @mtkbuild ssys2 = System([eq], t; name = :ssys) + @mtkbuild ssys1 = System([seq], t; name = :ssys) + @mtkbuild ssys2 = System([seq], t; name = :ssys) @test ssys1 == ssys2 # true continuous_events = [[X ~ 1.0] => [X ~ X + 5.0]] discrete_events = [5.0 => [d ~ d / 2.0]] - @mtkbuild ssys1 = System([eq], t; name = :ssys, continuous_events) - @mtkbuild ssys2 = System([eq], t; name = :ssys) + @mtkbuild ssys1 = System([seq], t; name = :ssys, continuous_events) + @mtkbuild ssys2 = System([seq], t; name = :ssys) @test ssys1 !== ssys2 - @mtkbuild ssys1 = System([eq], t; name = :ssys, discrete_events) - @mtkbuild ssys2 = System([eq], t; name = :ssys) + @mtkbuild ssys1 = System([seq], t; name = :ssys, discrete_events) + @mtkbuild ssys2 = System([seq], t; name = :ssys) @test ssys1 !== ssys2 - @mtkbuild ssys1 = System([eq], t; name = :ssys, continuous_events) - @mtkbuild ssys2 = System([eq], t; name = :ssys, discrete_events) + @mtkbuild ssys1 = System([seq], t; name = :ssys, continuous_events) + @mtkbuild ssys2 = System([seq], t; name = :ssys, discrete_events) @test ssys1 !== ssys2 end From ea95965838ae66eda3623d4e7a83dd511db5cc74 Mon Sep 17 00:00:00 2001 From: Christopher Rackauckas Date: Sat, 18 Jan 2025 09:20:18 +0100 Subject: [PATCH 0609/2176] Update test/sdesystem.jl --- test/sdesystem.jl | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/test/sdesystem.jl b/test/sdesystem.jl index 1a22a3501d..94f3e9cbfc 100644 --- a/test/sdesystem.jl +++ b/test/sdesystem.jl @@ -868,12 +868,10 @@ end @test length(ModelingToolkit.get_noiseeqs(sys)) == 1 @test length(observed(sys)) == 1 end -using ModelingToolkit, StochasticDiffEq -using ModelingToolkit: t_nounits as t, D_nounits as D @testset "Error when constructing SDESystem without `structural_simplify`" begin @parameters σ ρ β - @variables x(t) y(t) z(t) + @variables x(tt) y(tt) z(tt) @brownian a eqs = [D(x) ~ σ * (y - x) + 0.1a * x, D(y) ~ x * (ρ - z) - y + 0.1a * y, From 39f8271ea334c3c8659499fd788c3e3c54c60dac Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Sun, 19 Jan 2025 14:00:36 +0530 Subject: [PATCH 0610/2176] fix: only solve parameter initialization for `NonlinearSystem` --- src/systems/diffeqs/abstractodesystem.jl | 2 +- src/systems/nonlinear/initializesystem.jl | 106 +++++----- src/systems/problem_utils.jl | 38 +++- test/initializationsystem.jl | 239 +++++++++------------- 4 files changed, 180 insertions(+), 205 deletions(-) diff --git a/src/systems/diffeqs/abstractodesystem.jl b/src/systems/diffeqs/abstractodesystem.jl index 00b3c11ec3..ac28d41dd4 100644 --- a/src/systems/diffeqs/abstractodesystem.jl +++ b/src/systems/diffeqs/abstractodesystem.jl @@ -1335,7 +1335,7 @@ function InitializationProblem{iip, specialize}(sys::AbstractSystem, # TODO: throw on uninitialized arrays filter!(x -> !(x isa Symbolics.Arr), uninit) - if !isempty(uninit) + if is_time_dependent(sys) && !isempty(uninit) allow_incomplete || throw(IncompleteInitializationError(uninit)) # for incomplete initialization, we will add the missing variables as parameters. # they will be updated by `update_initializeprob!` and `initializeprobmap` will diff --git a/src/systems/nonlinear/initializesystem.jl b/src/systems/nonlinear/initializesystem.jl index 0b556eef68..a7f98d79b1 100644 --- a/src/systems/nonlinear/initializesystem.jl +++ b/src/systems/nonlinear/initializesystem.jl @@ -42,51 +42,53 @@ function generate_initializesystem(sys::AbstractSystem; diffmap = Dict() end - if has_schedule(sys) && (schedule = get_schedule(sys); !isnothing(schedule)) - # 2) process dummy derivatives and u0map into initialization system - # prepare map for dummy derivative substitution - for x in filter(x -> !isnothing(x[1]), schedule.dummy_sub) - # set dummy derivatives to default_dd_guess unless specified - push!(defs, x[1] => get(guesses, x[1], default_dd_guess)) - end - function process_u0map_with_dummysubs(y, x) - y = get(schedule.dummy_sub, y, y) - y = fixpoint_sub(y, diffmap) - if y ∈ vars_set - # variables specified in u0 overrides defaults - push!(defs, y => x) - elseif y isa Symbolics.Arr - # TODO: don't scalarize arrays - merge!(defs, Dict(scalarize(y .=> x))) - elseif y isa Symbolics.BasicSymbolic - # y is a derivative expression expanded; add it to the initialization equations - push!(eqs_ics, y ~ x) - else - error("Initialization expression $y is currently not supported. If its a higher order derivative expression, then only the dummy derivative expressions are supported.") + if is_time_dependent(sys) + if has_schedule(sys) && (schedule = get_schedule(sys); !isnothing(schedule)) + # 2) process dummy derivatives and u0map into initialization system + # prepare map for dummy derivative substitution + for x in filter(x -> !isnothing(x[1]), schedule.dummy_sub) + # set dummy derivatives to default_dd_guess unless specified + push!(defs, x[1] => get(guesses, x[1], default_dd_guess)) end - end - for (y, x) in u0map - if Symbolics.isarraysymbolic(y) - process_u0map_with_dummysubs.(collect(y), collect(x)) - else - process_u0map_with_dummysubs(y, x) + function process_u0map_with_dummysubs(y, x) + y = get(schedule.dummy_sub, y, y) + y = fixpoint_sub(y, diffmap) + if y ∈ vars_set + # variables specified in u0 overrides defaults + push!(defs, y => x) + elseif y isa Symbolics.Arr + # TODO: don't scalarize arrays + merge!(defs, Dict(scalarize(y .=> x))) + elseif y isa Symbolics.BasicSymbolic + # y is a derivative expression expanded; add it to the initialization equations + push!(eqs_ics, y ~ x) + else + error("Initialization expression $y is currently not supported. If its a higher order derivative expression, then only the dummy derivative expressions are supported.") + end + end + for (y, x) in u0map + if Symbolics.isarraysymbolic(y) + process_u0map_with_dummysubs.(collect(y), collect(x)) + else + process_u0map_with_dummysubs(y, x) + end + end + else + # 2) System doesn't have a schedule, so dummy derivatives don't exist/aren't handled (SDESystem) + for (k, v) in u0map + defs[k] = v end end - else - # 2) System doesn't have a schedule, so dummy derivatives don't exist/aren't handled (SDESystem) - for (k, v) in u0map - defs[k] = v - end - end - # 3) process other variables - for var in vars - if var ∈ keys(defs) - push!(eqs_ics, var ~ defs[var]) - elseif var ∈ keys(guesses) - push!(defs, var => guesses[var]) - elseif check_defguess - error("Invalid setup: variable $(var) has no default value or initial guess") + # 3) process other variables + for var in vars + if var ∈ keys(defs) + push!(eqs_ics, var ~ defs[var]) + elseif var ∈ keys(guesses) + push!(defs, var => guesses[var]) + elseif check_defguess + error("Invalid setup: variable $(var) has no default value or initial guess") + end end end @@ -180,16 +182,24 @@ function generate_initializesystem(sys::AbstractSystem; pars = Vector{SymbolicParam}(filter(p -> !haskey(paramsubs, p), parameters(sys))) is_time_dependent(sys) && push!(pars, get_iv(sys)) - # 8) use observed equations for guesses of observed variables if not provided - for eq in trueobs - haskey(defs, eq.lhs) && continue - any(x -> isequal(default_toterm(x), eq.lhs), keys(defs)) && continue + if is_time_dependent(sys) + # 8) use observed equations for guesses of observed variables if not provided + for eq in trueobs + haskey(defs, eq.lhs) && continue + any(x -> isequal(default_toterm(x), eq.lhs), keys(defs)) && continue - defs[eq.lhs] = eq.rhs + defs[eq.lhs] = eq.rhs + end + append!(eqs_ics, trueobs) + end + + eqs_ics = Symbolics.substitute.(eqs_ics, (paramsubs,)) + if is_time_dependent(sys) + vars = [vars; collect(values(paramsubs))] + else + vars = collect(values(paramsubs)) end - eqs_ics = Symbolics.substitute.([eqs_ics; trueobs], (paramsubs,)) - vars = [vars; collect(values(paramsubs))] for k in keys(defs) defs[k] = substitute(defs[k], paramsubs) end diff --git a/src/systems/problem_utils.jl b/src/systems/problem_utils.jl index 4cbf495d87..a8d443349b 100644 --- a/src/systems/problem_utils.jl +++ b/src/systems/problem_utils.jl @@ -546,30 +546,46 @@ function maybe_build_initialization_problem( initializeprob = ModelingToolkit.InitializationProblem( sys, t, u0map, pmap; guesses, kwargs...) - all_init_syms = Set(all_symbols(initializeprob)) - solved_unknowns = filter(var -> var in all_init_syms, unknowns(sys)) - initializeprobmap = getu(initializeprob, solved_unknowns) + if is_time_dependent(sys) + all_init_syms = Set(all_symbols(initializeprob)) + solved_unknowns = filter(var -> var in all_init_syms, unknowns(sys)) + initializeprobmap = getu(initializeprob, solved_unknowns) + else + initializeprobmap = nothing + end punknowns = [p for p in all_variable_symbols(initializeprob) if is_parameter(sys, p)] - getpunknowns = getu(initializeprob, punknowns) - setpunknowns = setp(sys, punknowns) - initializeprobpmap = GetUpdatedMTKParameters(getpunknowns, setpunknowns) + if isempty(punknowns) + initializeprobpmap = nothing + else + getpunknowns = getu(initializeprob, punknowns) + setpunknowns = setp(sys, punknowns) + initializeprobpmap = GetUpdatedMTKParameters(getpunknowns, setpunknowns) + end reqd_syms = parameter_symbols(initializeprob) - update_initializeprob! = UpdateInitializeprob( - getu(sys, reqd_syms), setu(initializeprob, reqd_syms)) + # we still want the `initialization_data` because it helps with `remake` + if initializeprobmap === nothing && initializeprobpmap === nothing + update_initializeprob! = nothing + else + update_initializeprob! = UpdateInitializeprob( + getu(sys, reqd_syms), setu(initializeprob, reqd_syms)) + end + for p in punknowns p = unwrap(p) stype = symtype(p) op[p] = get_temporary_value(p) end - for v in missing_unknowns - op[v] = zero_var(v) + if is_time_dependent(sys) + for v in missing_unknowns + op[v] = zero_var(v) + end + empty!(missing_unknowns) end - empty!(missing_unknowns) return (; initialization_data = SciMLBase.OverrideInitData( initializeprob, update_initializeprob!, initializeprobmap, diff --git a/test/initializationsystem.jl b/test/initializationsystem.jl index 62b6bba707..0a4ef18038 100644 --- a/test/initializationsystem.jl +++ b/test/initializationsystem.jl @@ -583,14 +583,6 @@ sol = solve(oprob_2nd_order_2, Rosenbrock23()) # retcode: Success @test all(sol(1.0, idxs = sys.x) .≈ +exp(1)) && all(sol(1.0, idxs = sys.y) .≈ -exp(1)) end -NonlinearSystemWrapper(eqs, t; kws...) = NonlinearSystem(eqs; kws...) -function NonlinearProblemWrapper(sys, u0, tspan, args...; kwargs...) - NonlinearProblem(sys, u0, args...; kwargs...) -end -function NLLSProblemWrapper(sys, u0, tspan, args...; kwargs...) - NonlinearLeastSquaresProblem(sys, u0, args...; kwargs...) -end - @testset "Initialization of parameters" begin @variables _x(..) y(t) @parameters p q @@ -606,31 +598,11 @@ end (ModelingToolkit.System, ODEProblem, Tsit5(), zeros(2)), (ModelingToolkit.System, SDEProblem, ImplicitEM(), [a, b]), (ModelingToolkit.System, DDEProblem, MethodOfSteps(Tsit5()), [_x(t - 0.1), 0.0]), - (ModelingToolkit.System, SDDEProblem, ImplicitEM(), [_x(t - 0.1) + a, b]), - # polyalg cache - (NonlinearSystemWrapper, NonlinearProblemWrapper, - FastShortcutNonlinearPolyalg(), zeros(2)), - # generalized first order cache - (NonlinearSystemWrapper, NonlinearProblemWrapper, NewtonRaphson(), zeros(2)), - # quasi newton cache - (NonlinearSystemWrapper, NonlinearProblemWrapper, Klement(), zeros(2)), - # noinit cache - (NonlinearSystemWrapper, NonlinearProblemWrapper, SimpleNewtonRaphson(), zeros(2)), - # DFSane cache - (NonlinearSystemWrapper, NonlinearProblemWrapper, DFSane(), zeros(2)), - # Least squares - # polyalg cache - (NonlinearSystemWrapper, NLLSProblemWrapper, FastShortcutNLLSPolyalg(), zeros(2)), - # generalized first order cache - (NonlinearSystemWrapper, NLLSProblemWrapper, LevenbergMarquardt(), zeros(2)), - # noinit cache - (NonlinearSystemWrapper, NLLSProblemWrapper, SimpleGaussNewton(), zeros(2)) + (ModelingToolkit.System, SDDEProblem, ImplicitEM(), [_x(t - 0.1) + a, b]) ] - is_nlsolve = alg isa SciMLBase.AbstractNonlinearAlgorithm - function test_parameter(prob, sym, val, initialval = zero(val)) @test prob.ps[sym] ≈ initialval - if !is_nlsolve || prob.u0 !== nothing + if prob.u0 !== nothing @test init(prob, alg).ps[sym] ≈ val end @test solve(prob, alg).ps[sym] ≈ val @@ -641,7 +613,6 @@ end @test is_variable(isys, p) @test equation in equations(isys) || (0 ~ -equation.rhs) in equations(isys) end - D = is_nlsolve ? v -> v^3 : Differential(t) u0map = Dict(x => 1.0, y => 1.0) pmap = Dict() @@ -777,6 +748,90 @@ end @test solve(prob, Tsit5()).ps[spring.s_rel0] ≈ -3.905 end +@testset "NonlinearSystem initialization" begin + nl_algs = [FastShortcutNonlinearPolyalg(), NewtonRaphson(), + Klement(), SimpleNewtonRaphson(), DFSane()] + nlls_algs = [FastShortcutNLLSPolyalg(), LevenbergMarquardt(), SimpleGaussNewton()] + + @testset "No initialization for variables" begin + @variables x=1.0 y=0.0 z=0.0 + @parameters σ=10.0 ρ=26.0 β=8 / 3 + + eqs = [0 ~ σ * (y - x), + 0 ~ x * (ρ - z) - y, + 0 ~ x * y - β * z] + @mtkbuild ns = NonlinearSystem(eqs, [x, y, z], [σ, ρ, β]) + + prob = NonlinearProblem(ns, []) + @test prob.f.initialization_data.update_initializeprob! === nothing + @test prob.f.initialization_data.initializeprobmap === nothing + @test prob.f.initialization_data.initializeprobpmap === nothing + for alg in nl_algs + @test SciMLBase.successful_retcode(solve(prob, alg)) + end + + prob = NonlinearLeastSquaresProblem(ns, []) + @test prob.f.initialization_data.update_initializeprob! === nothing + @test prob.f.initialization_data.initializeprobmap === nothing + @test prob.f.initialization_data.initializeprobpmap === nothing + for alg in nlls_algs + @test SciMLBase.successful_retcode(solve(prob, alg)) + end + end + + prob_alg_combinations = zip( + [NonlinearProblem, NonlinearLeastSquaresProblem], [nl_algs, nlls_algs]) + @testset "Parameter initialization" begin + function test_parameter(prob, alg, param, val) + integ = init(prob, alg) + @test integ.ps[param]≈val rtol=1e-6 + sol = solve(prob, alg) + @test sol.ps[param]≈val rtol=1e-6 + @test SciMLBase.successful_retcode(sol) + end + @variables x=1.0 y=3.0 + @parameters p q + + @mtkbuild sys = NonlinearSystem( + [(x - p)^2 + (y - q)^3 ~ 0, x - q ~ 0]; defaults = [q => missing], + guesses = [q => 1.0], initialization_eqs = [p^2 + q^2 + 2p * q ~ 0]) + + for (probT, algs) in prob_alg_combinations + prob = probT(sys, [], [p => 2.0]) + @test prob.f.initialization_data !== nothing + @test prob.f.initialization_data.initializeprobmap === nothing + for alg in algs + test_parameter(prob, alg, q, -2.0) + end + + # `update_initializeprob!` works + prob.ps[p] = -2.0 + for alg in algs + test_parameter(prob, alg, q, 2.0) + end + prob.ps[p] = 2.0 + + # `remake` works + prob2 = remake(prob; p = [p => -2.0]) + @test prob2.f.initialization_data !== nothing + @test prob2.f.initialization_data.initializeprobmap === nothing + for alg in algs + test_parameter(prob2, alg, q, 2.0) + end + + # changing types works + ps = parameter_values(prob) + newps = SciMLStructures.replace(Tunable(), ps, ForwardDiff.Dual.(ps.tunable)) + prob3 = remake(prob; p = newps) + @test prob3.f.initialization_data !== nothing + @test eltype(state_values(prob3.f.initialization_data.initializeprob)) <: + ForwardDiff.Dual + @test eltype(prob3.f.initialization_data.initializeprob.p.tunable) <: + ForwardDiff.Dual + end + end +end + @testset "Update initializeprob parameters" begin @variables _x(..) y(t) @parameters p q @@ -787,29 +842,8 @@ end (ModelingToolkit.System, ODEProblem, Tsit5(), zeros(2)), (ModelingToolkit.System, SDEProblem, ImplicitEM(), [a, b]), (ModelingToolkit.System, DDEProblem, MethodOfSteps(Tsit5()), [_x(t - 0.1), 0.0]), - (ModelingToolkit.System, SDDEProblem, ImplicitEM(), [_x(t - 0.1) + a, b]), - # polyalg cache - (NonlinearSystemWrapper, NonlinearProblemWrapper, - FastShortcutNonlinearPolyalg(), zeros(2)), - # generalized first order cache - (NonlinearSystemWrapper, NonlinearProblemWrapper, NewtonRaphson(), zeros(2)), - # quasi newton cache - (NonlinearSystemWrapper, NonlinearProblemWrapper, Klement(), zeros(2)), - # noinit cache - (NonlinearSystemWrapper, NonlinearProblemWrapper, SimpleNewtonRaphson(), zeros(2)), - # DFSane cache - (NonlinearSystemWrapper, NonlinearProblemWrapper, DFSane(), zeros(2)), - # Least squares - # polyalg cache - (NonlinearSystemWrapper, NLLSProblemWrapper, FastShortcutNLLSPolyalg(), zeros(2)), - # generalized first order cache - (NonlinearSystemWrapper, NLLSProblemWrapper, LevenbergMarquardt(), zeros(2)), - # noinit cache - (NonlinearSystemWrapper, NLLSProblemWrapper, SimpleGaussNewton(), zeros(2)) + (ModelingToolkit.System, SDDEProblem, ImplicitEM(), [_x(t - 0.1) + a, b]) ] - is_nlsolve = alg isa SciMLBase.AbstractNonlinearAlgorithm - D = is_nlsolve ? v -> v^3 : Differential(t) - @mtkbuild sys = System( [D(x) ~ x + rhss[1], p ~ x + y + rhss[2]], t; guesses = [x => 0.0, p => 0.0]) prob = Problem(sys, [y => 1.0], (0.0, 1.0), [p => 3.0]) @@ -837,29 +871,8 @@ end (ModelingToolkit.System, ODEProblem, Tsit5(), 0), (ModelingToolkit.System, SDEProblem, ImplicitEM(), a), (ModelingToolkit.System, DDEProblem, MethodOfSteps(Tsit5()), _x(t - 0.1)), - (ModelingToolkit.System, SDDEProblem, ImplicitEM(), _x(t - 0.1) + a), - # polyalg cache - (NonlinearSystemWrapper, NonlinearProblemWrapper, - FastShortcutNonlinearPolyalg(), 0), - # generalized first order cache - (NonlinearSystemWrapper, NonlinearProblemWrapper, NewtonRaphson(), 0), - # quasi newton cache - (NonlinearSystemWrapper, NonlinearProblemWrapper, Klement(), 0), - # noinit cache - (NonlinearSystemWrapper, NonlinearProblemWrapper, SimpleNewtonRaphson(), 0), - # DFSane cache - (NonlinearSystemWrapper, NonlinearProblemWrapper, DFSane(), 0), - # Least squares - # polyalg cache - (NonlinearSystemWrapper, NLLSProblemWrapper, FastShortcutNLLSPolyalg(), 0), - # generalized first order cache - (NonlinearSystemWrapper, NLLSProblemWrapper, LevenbergMarquardt(), 0), - # noinit cache - (NonlinearSystemWrapper, NLLSProblemWrapper, SimpleGaussNewton(), 0) + (ModelingToolkit.System, SDDEProblem, ImplicitEM(), _x(t - 0.1) + a) ] - is_nlsolve = alg isa SciMLBase.AbstractNonlinearAlgorithm - D = is_nlsolve ? v -> v^3 : Differential(t) - @mtkbuild sys = System( [D(x) ~ 2x + r + rhss], t; parameter_dependencies = [r ~ p + 2q, q ~ p + 3], guesses = [p => 1.0]) @@ -881,29 +894,8 @@ end (ModelingToolkit.System, ODEProblem, Tsit5(), zeros(2)), (ModelingToolkit.System, SDEProblem, ImplicitEM(), [a, b]), (ModelingToolkit.System, DDEProblem, MethodOfSteps(Tsit5()), [_x(t - 0.1), 0.0]), - (ModelingToolkit.System, SDDEProblem, ImplicitEM(), [_x(t - 0.1) + a, b]), - # polyalg cache - (NonlinearSystemWrapper, NonlinearProblemWrapper, - FastShortcutNonlinearPolyalg(), zeros(2)), - # generalized first order cache - (NonlinearSystemWrapper, NonlinearProblemWrapper, NewtonRaphson(), zeros(2)), - # quasi newton cache - (NonlinearSystemWrapper, NonlinearProblemWrapper, Klement(), zeros(2)), - # noinit cache - (NonlinearSystemWrapper, NonlinearProblemWrapper, SimpleNewtonRaphson(), zeros(2)), - # DFSane cache - (NonlinearSystemWrapper, NonlinearProblemWrapper, DFSane(), zeros(2)), - # Least squares - # polyalg cache - (NonlinearSystemWrapper, NLLSProblemWrapper, FastShortcutNLLSPolyalg(), zeros(2)), - # generalized first order cache - (NonlinearSystemWrapper, NLLSProblemWrapper, LevenbergMarquardt(), zeros(2)), - # noinit cache - (NonlinearSystemWrapper, NLLSProblemWrapper, SimpleGaussNewton(), zeros(2)) + (ModelingToolkit.System, SDDEProblem, ImplicitEM(), [_x(t - 0.1) + a, b]) ] - is_nlsolve = alg isa SciMLBase.AbstractNonlinearAlgorithm - D = is_nlsolve ? v -> v^3 : Differential(t) - @mtkbuild sys = System( [D(x) ~ x + rhss[1], p ~ x + y + rhss[2]], t; defaults = [p => missing], guesses = [ x => 0.0, p => 0.0]) @@ -934,35 +926,14 @@ end (ModelingToolkit.System, ODEProblem, Tsit5(), 0), (ModelingToolkit.System, SDEProblem, ImplicitEM(), a), (ModelingToolkit.System, DDEProblem, MethodOfSteps(Tsit5()), _x(t - 0.1)), - (ModelingToolkit.System, SDDEProblem, ImplicitEM(), _x(t - 0.1) + a), - # polyalg cache - (NonlinearSystemWrapper, NonlinearProblemWrapper, - FastShortcutNonlinearPolyalg(), 0), - # generalized first order cache - (NonlinearSystemWrapper, NonlinearProblemWrapper, NewtonRaphson(), 0), - # quasi newton cache - (NonlinearSystemWrapper, NonlinearProblemWrapper, Klement(), 0), - # noinit cache - (NonlinearSystemWrapper, NonlinearProblemWrapper, SimpleNewtonRaphson(), 0), - # DFSane cache - (NonlinearSystemWrapper, NonlinearProblemWrapper, DFSane(), 0), - # Least squares - # polyalg cache - (NonlinearSystemWrapper, NLLSProblemWrapper, FastShortcutNLLSPolyalg(), 0), - # generalized first order cache - (NonlinearSystemWrapper, NLLSProblemWrapper, LevenbergMarquardt(), 0), - # noinit cache - (NonlinearSystemWrapper, NLLSProblemWrapper, SimpleGaussNewton(), 0) + (ModelingToolkit.System, SDDEProblem, ImplicitEM(), _x(t - 0.1) + a) ] - is_nlsolve = alg isa SciMLBase.AbstractNonlinearAlgorithm - D = is_nlsolve ? v -> v^3 : Differential(t) alge_eqs = [y^2 * q + q^2 * x ~ 0, z * p - p^2 * x * z ~ 0] @mtkbuild sys = System( [D(x) ~ x * p + y^2 * q + rhss; alge_eqs], t; guesses = [x => 0.0, y => 0.0, z => 0.0, p => 0.0, q => 0.0]) - prob = Problem(sys, [x => 1.0], (0.0, 1.0), [p => 1.0, q => missing]; - initialization_eqs = is_nlsolve ? alge_eqs : []) + prob = Problem(sys, [x => 1.0], (0.0, 1.0), [p => 1.0, q => missing]) @test is_variable(prob.f.initialization_data.initializeprob, q) ps = prob.p newps = SciMLStructures.replace(Tunable(), ps, ForwardDiff.Dual.(ps.tunable)) @@ -1010,40 +981,18 @@ end (ModelingToolkit.System, ODEProblem, Tsit5(), 0), (ModelingToolkit.System, SDEProblem, ImplicitEM(), a), (ModelingToolkit.System, DDEProblem, MethodOfSteps(Tsit5()), _x(t - 0.1)), - (ModelingToolkit.System, SDDEProblem, ImplicitEM(), _x(t - 0.1) + a), - # polyalg cache - (NonlinearSystemWrapper, NonlinearProblemWrapper, - FastShortcutNonlinearPolyalg(), 0), - # generalized first order cache - (NonlinearSystemWrapper, NonlinearProblemWrapper, NewtonRaphson(), 0), - # quasi newton cache - (NonlinearSystemWrapper, NonlinearProblemWrapper, Klement(), 0), - # noinit cache - (NonlinearSystemWrapper, NonlinearProblemWrapper, SimpleNewtonRaphson(), 0), - # DFSane cache - (NonlinearSystemWrapper, NonlinearProblemWrapper, DFSane(), 0), - # Least squares - # polyalg cache - (NonlinearSystemWrapper, NLLSProblemWrapper, FastShortcutNLLSPolyalg(), 0), - # generalized first order cache - (NonlinearSystemWrapper, NLLSProblemWrapper, LevenbergMarquardt(), 0), - # noinit cache - (NonlinearSystemWrapper, NLLSProblemWrapper, SimpleGaussNewton(), 0) + (ModelingToolkit.System, SDDEProblem, ImplicitEM(), _x(t - 0.1) + a) ] - is_nlsolve = alg isa SciMLBase.AbstractNonlinearAlgorithm - D = is_nlsolve ? v -> v^3 : Differential(t) alge_eqs = [y^2 + 4y * p^2 ~ x^3] @mtkbuild sys = System( [D(x) ~ x + p * y^2 + rhss; alge_eqs], t; guesses = [ y => 1.0, p => 1.0]) - prob = Problem(sys, [x => 1.0], (0.0, 1.0), [p => 1.0]; - initialization_eqs = is_nlsolve ? alge_eqs : []) + prob = Problem(sys, [x => 1.0], (0.0, 1.0), [p => 1.0]) @test is_variable(prob.f.initialization_data.initializeprob, y) prob2 = @test_nowarn remake(prob; p = [p => 3.0]) # ensure no over/under-determined warning @test is_variable(prob.f.initialization_data.initializeprob, y) - prob = Problem(sys, [y => 1.0, x => 2.0], (0.0, 1.0), [p => missing]; - initialization_eqs = is_nlsolve ? alge_eqs : []) + prob = Problem(sys, [y => 1.0, x => 2.0], (0.0, 1.0), [p => missing]) @test is_variable(prob.f.initialization_data.initializeprob, p) prob2 = @test_nowarn remake(prob; u0 = [y => 0.5]) @test is_variable(prob.f.initialization_data.initializeprob, p) From ccfc13b12ea766e5e99e0e12d59a97f07dce221c Mon Sep 17 00:00:00 2001 From: Christopher Rackauckas Date: Sun, 19 Jan 2025 11:25:21 +0100 Subject: [PATCH 0611/2176] Update Project.toml --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 7ccb4d2110..1f46a4771c 100644 --- a/Project.toml +++ b/Project.toml @@ -132,7 +132,7 @@ RecursiveArrayTools = "3.26" Reexport = "0.2, 1" RuntimeGeneratedFunctions = "0.5.9" SCCNonlinearSolve = "1.0.0" -SciMLBase = "2.71" +SciMLBase = "2.71.1" SciMLStructures = "1.0" Serialization = "1" Setfield = "0.7, 0.8, 1" From 40a85df0d809bbae7ac2707abf4518ba40b89c7d Mon Sep 17 00:00:00 2001 From: Christopher Rackauckas Date: Sun, 19 Jan 2025 15:31:14 +0100 Subject: [PATCH 0612/2176] Update Project.toml --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 1f46a4771c..0a6238685a 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "ModelingToolkit" uuid = "961ee093-0014-501f-94e3-6117800e7a78" authors = ["Yingbo Ma ", "Chris Rackauckas and contributors"] -version = "9.60.0" +version = "9.61.0" [deps] AbstractTrees = "1520ce14-60c1-5f80-bbc7-55ef81b5835c" From 3993abd12483b7387d3dc99d5f701a1a4c626532 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 24 Dec 2024 20:26:52 +0530 Subject: [PATCH 0613/2176] fix: fix `add_fallbacks!` --- src/systems/problem_utils.jl | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/src/systems/problem_utils.jl b/src/systems/problem_utils.jl index a8d443349b..bf3d72e1e5 100644 --- a/src/systems/problem_utils.jl +++ b/src/systems/problem_utils.jl @@ -88,6 +88,18 @@ function symbols_to_symbolics!(sys::AbstractSystem, varmap::AbstractDict) end end +""" + $(TYPEDSIGNATURES) + +Utility function to get the value `val` corresponding to key `var` in `varmap`, and +return `getindex(val, idx)` if it exists or `nothing` otherwise. +""" +function get_and_getindex(varmap, var, idx) + val = get(varmap, var, nothing) + val === nothing && return nothing + return val[idx] +end + """ $(TYPEDSIGNATURES) @@ -115,8 +127,9 @@ function add_fallbacks!( val = map(eachindex(var)) do idx # @something is lazy and saves from writing a massive if-elseif-else @something(get(varmap, var[idx], nothing), - get(varmap, ttvar[idx], nothing), get(fallbacks, var, nothing)[idx], - get(fallbacks, ttvar, nothing)[idx], get(fallbacks, var[idx], nothing), + get(varmap, ttvar[idx], nothing), get_and_getindex(fallbacks, var, idx), + get_and_getindex(fallbacks, ttvar, idx), get( + fallbacks, var[idx], nothing), get(fallbacks, ttvar[idx], nothing), Some(nothing)) end # only push the missing entries From d7f908eea35a2658397328afe25d52342fd23dc0 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 20 Jan 2025 11:29:04 +0530 Subject: [PATCH 0614/2176] test: test `add_fallbacks!` bug --- test/initial_values.jl | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/test/initial_values.jl b/test/initial_values.jl index a2931e22b6..c31eca33f8 100644 --- a/test/initial_values.jl +++ b/test/initial_values.jl @@ -174,3 +174,11 @@ end @test_throws ModelingToolkit.UnexpectedSymbolicValueInVarmap ODEProblem( sys, [x => 1, y => 2], (0.0, 1.0), [p => 2q, q => 3p]) end + +@testset "`add_fallbacks!` checks scalarized array parameters correctly" begin + @variables x(t)[1:2] + @parameters p[1:2, 1:2] + @mtkbuild sys = ODESystem(D(x) ~ p * x, t) + # used to throw a `MethodError` complaining about `getindex(::Nothing, ::CartesianIndex{2})` + @test_throws ModelingToolkit.MissingParametersError ODEProblem(sys, [x => ones(2)], (0.0, 1.0)) +end From 76e515c4c55af8b9ca3bf8a3318d1dd3339702a3 Mon Sep 17 00:00:00 2001 From: vyudu Date: Thu, 23 Jan 2025 12:33:15 -0500 Subject: [PATCH 0615/2176] Init --- .../discrete_system/implicitdiscretesystem.jl | 406 ++++++++++++++++++ 1 file changed, 406 insertions(+) create mode 100644 src/systems/discrete_system/implicitdiscretesystem.jl diff --git a/src/systems/discrete_system/implicitdiscretesystem.jl b/src/systems/discrete_system/implicitdiscretesystem.jl new file mode 100644 index 0000000000..404fa7ff49 --- /dev/null +++ b/src/systems/discrete_system/implicitdiscretesystem.jl @@ -0,0 +1,406 @@ +""" +$(TYPEDEF) +An implicit system of difference equations. +# Fields +$(FIELDS) +# Example +``` +using ModelingToolkit +using ModelingToolkit: t_nounits as t +@parameters σ=28.0 ρ=10.0 β=8/3 δt=0.1 +@variables x(t)=1.0 y(t)=0.0 z(t)=0.0 +k = ShiftIndex(t) +eqs = [x(k+1) ~ σ*(y-x), + y(k+1) ~ x*(ρ-z)-y, + z(k+1) ~ x*y - β*z] +@named de = ImplicitDiscreteSystem(eqs,t,[x,y,z],[σ,ρ,β]; tspan = (0, 1000.0)) # or +@named de = ImplicitDiscreteSystem(eqs) +``` +""" +struct ImplicitDiscreteSystem <: AbstractTimeDependentSystem + """ + A tag for the system. If two systems have the same tag, then they are + structurally identical. + """ + tag::UInt + """The differential equations defining the discrete system.""" + eqs::Vector{Equation} + """Independent variable.""" + iv::BasicSymbolic{Real} + """Dependent (state) variables. Must not contain the independent variable.""" + unknowns::Vector + """Parameter variables. Must not contain the independent variable.""" + ps::Vector + """Time span.""" + tspan::Union{NTuple{2, Any}, Nothing} + """Array variables.""" + var_to_name::Any + """Observed states.""" + observed::Vector{Equation} + """ + The name of the system + """ + name::Symbol + """ + A description of the system. + """ + description::String + """ + The internal systems. These are required to have unique names. + """ + systems::Vector{DiscreteSystem} + """ + The default values to use when initial conditions and/or + parameters are not supplied in `DiscreteProblem`. + """ + defaults::Dict + """ + The guesses to use as the initial conditions for the + initialization system. + """ + guesses::Dict + """ + The system for performing the initialization. + """ + initializesystem::Union{Nothing, NonlinearSystem} + """ + Extra equations to be enforced during the initialization sequence. + """ + initialization_eqs::Vector{Equation} + """ + Inject assignment statements before the evaluation of the RHS function. + """ + preface::Any + """ + Type of the system. + """ + connector_type::Any + """ + Topologically sorted parameter dependency equations, where all symbols are parameters and + the LHS is a single parameter. + """ + parameter_dependencies::Vector{Equation} + """ + Metadata for the system, to be used by downstream packages. + """ + metadata::Any + """ + Metadata for MTK GUI. + """ + gui_metadata::Union{Nothing, GUIMetadata} + """ + Cache for intermediate tearing state. + """ + tearing_state::Any + """ + Substitutions generated by tearing. + """ + substitutions::Any + """ + If a model `sys` is complete, then `sys.x` no longer performs namespacing. + """ + complete::Bool + """ + Cached data for fast symbolic indexing. + """ + index_cache::Union{Nothing, IndexCache} + """ + The hierarchical parent system before simplification. + """ + parent::Any + isscheduled::Bool + + function ImplicitDiscreteSystem(tag, discreteEqs, iv, dvs, ps, tspan, var_to_name, + observed, name, description, systems, defaults, guesses, initializesystem, + initialization_eqs, preface, connector_type, parameter_dependencies = Equation[], + metadata = nothing, gui_metadata = nothing, + tearing_state = nothing, substitutions = nothing, + complete = false, index_cache = nothing, parent = nothing, + isscheduled = false; + checks::Union{Bool, Int} = true) + if checks == true || (checks & CheckComponents) > 0 + check_independent_variables([iv]) + check_variables(dvs, iv) + check_parameters(ps, iv) + end + if checks == true || (checks & CheckUnits) > 0 + u = __get_unit_type(dvs, ps, iv) + check_units(u, discreteEqs) + end + new(tag, discreteEqs, iv, dvs, ps, tspan, var_to_name, observed, name, description, + systems, defaults, guesses, initializesystem, initialization_eqs, + preface, connector_type, parameter_dependencies, metadata, gui_metadata, + tearing_state, substitutions, complete, index_cache, parent, isscheduled) + end +end + +""" + $(TYPEDSIGNATURES) +Constructs a DiscreteSystem. +""" +function ImplicitDiscreteSystem(eqs::AbstractVector{<:Equation}, iv, dvs, ps; + observed = Num[], + systems = DiscreteSystem[], + tspan = nothing, + name = nothing, + description = "", + default_u0 = Dict(), + default_p = Dict(), + guesses = Dict(), + initializesystem = nothing, + initialization_eqs = Equation[], + defaults = _merge(Dict(default_u0), Dict(default_p)), + preface = nothing, + connector_type = nothing, + parameter_dependencies = Equation[], + metadata = nothing, + gui_metadata = nothing, + kwargs...) + name === nothing && + throw(ArgumentError("The `name` keyword must be provided. Please consider using the `@named` macro")) + iv′ = value(iv) + dvs′ = value.(dvs) + ps′ = value.(ps) + if any(hasderiv, eqs) || any(hashold, eqs) || any(hassample, eqs) || any(hasdiff, eqs) + error("Equations in a `DiscreteSystem` can only have `Shift` operators.") + end + if !(isempty(default_u0) && isempty(default_p)) + Base.depwarn( + "`default_u0` and `default_p` are deprecated. Use `defaults` instead.", + :DiscreteSystem, force = true) + end + + defaults = Dict{Any, Any}(todict(defaults)) + guesses = Dict{Any, Any}(todict(guesses)) + var_to_name = Dict() + process_variables!(var_to_name, defaults, guesses, dvs′) + process_variables!(var_to_name, defaults, guesses, ps′) + process_variables!( + var_to_name, defaults, guesses, [eq.lhs for eq in parameter_dependencies]) + process_variables!( + var_to_name, defaults, guesses, [eq.rhs for eq in parameter_dependencies]) + defaults = Dict{Any, Any}(value(k) => value(v) + for (k, v) in pairs(defaults) if v !== nothing) + guesses = Dict{Any, Any}(value(k) => value(v) + for (k, v) in pairs(guesses) if v !== nothing) + + isempty(observed) || collect_var_to_name!(var_to_name, (eq.lhs for eq in observed)) + + sysnames = nameof.(systems) + if length(unique(sysnames)) != length(sysnames) + throw(ArgumentError("System names must be unique.")) + end + DiscreteSystem(Threads.atomic_add!(SYSTEM_COUNT, UInt(1)), + eqs, iv′, dvs′, ps′, tspan, var_to_name, observed, name, description, systems, + defaults, guesses, initializesystem, initialization_eqs, preface, connector_type, + parameter_dependencies, metadata, gui_metadata, kwargs...) +end + +function ImplicitDiscreteSystem(eqs, iv; kwargs...) + eqs = collect(eqs) + diffvars = OrderedSet() + allunknowns = OrderedSet() + ps = OrderedSet() + iv = value(iv) + for eq in eqs + collect_vars!(allunknowns, ps, eq, iv; op = Shift) + if iscall(eq.lhs) && operation(eq.lhs) isa Shift + isequal(iv, operation(eq.lhs).t) || + throw(ArgumentError("A DiscreteSystem can only have one independent variable.")) + eq.lhs in diffvars && + throw(ArgumentError("The shift variable $(eq.lhs) is not unique in the system of equations.")) + push!(diffvars, eq.lhs) + end + end + for eq in get(kwargs, :parameter_dependencies, Equation[]) + if eq isa Pair + collect_vars!(allunknowns, ps, eq, iv) + else + collect_vars!(allunknowns, ps, eq, iv) + end + end + new_ps = OrderedSet() + for p in ps + if iscall(p) && operation(p) === getindex + par = arguments(p)[begin] + if Symbolics.shape(Symbolics.unwrap(par)) !== Symbolics.Unknown() && + all(par[i] in ps for i in eachindex(par)) + push!(new_ps, par) + else + push!(new_ps, p) + end + else + push!(new_ps, p) + end + end + return DiscreteSystem(eqs, iv, + collect(allunknowns), collect(new_ps); kwargs...) +end + +function flatten(sys::DiscreteSystem, noeqs = false) + systems = get_systems(sys) + if isempty(systems) + return sys + else + return DiscreteSystem(noeqs ? Equation[] : equations(sys), + get_iv(sys), + unknowns(sys), + parameters(sys), + observed = observed(sys), + defaults = defaults(sys), + guesses = guesses(sys), + initialization_eqs = initialization_equations(sys), + name = nameof(sys), + description = description(sys), + metadata = get_metadata(sys), + checks = false) + end +end + +function generate_function( + sys::DiscreteSystem, dvs = unknowns(sys), ps = parameters(sys); wrap_code = identity, kwargs...) + exprs = [eq.rhs for eq in equations(sys)] + wrap_code = wrap_code .∘ wrap_array_vars(sys, exprs) .∘ + wrap_parameter_dependencies(sys, false) + generate_custom_function(sys, exprs, dvs, ps; wrap_code, kwargs...) +end + +function shift_u0map_forward(sys::DiscreteSystem, u0map, defs) + iv = get_iv(sys) + updated = AnyDict() + for k in collect(keys(u0map)) + v = u0map[k] + if !((op = operation(k)) isa Shift) + error("Initial conditions must be for the past state of the unknowns. Instead of providing the condition for $k, provide the condition for $(Shift(iv, -1)(k)).") + end + updated[Shift(iv, op.steps + 1)(arguments(k)[1])] = v + end + for var in unknowns(sys) + op = operation(var) + op isa Shift || continue + haskey(updated, var) && continue + root = first(arguments(var)) + haskey(defs, root) || error("Initial condition for $var not provided.") + updated[var] = defs[root] + end + return updated +end + +""" + $(TYPEDSIGNATURES) +Generates an ImplicitDiscreteProblem from an ImplicitDiscreteSystem. +""" +function SciMLBase.ImplicitDiscreteProblem( + sys::DiscreteSystem, u0map = [], tspan = get_tspan(sys), + parammap = SciMLBase.NullParameters(); + eval_module = @__MODULE__, + eval_expression = false, + use_union = false, + kwargs... +) + if !iscomplete(sys) + error("A completed `ImplicitDiscreteSystem` is required. Call `complete` or `structural_simplify` on the system before creating a `ImplicitDiscreteProblem`") + end + dvs = unknowns(sys) + ps = parameters(sys) + eqs = equations(sys) + iv = get_iv(sys) + + u0map = to_varmap(u0map, dvs) + u0map = shift_u0map_forward(sys, u0map, defaults(sys)) + f, u0, p = process_SciMLProblem( + DiscreteFunction, sys, u0map, parammap; eval_expression, eval_module) + u0 = f(u0, p, tspan[1]) + ImplicitDiscreteProblem(f, u0, tspan, p; kwargs...) +end + +function SciMLBase.ImplicitDiscreteFunction(sys::ImplicitDiscreteSystem, args...; kwargs...) + ImplicitDiscreteFunction{true}(sys, args...; kwargs...) +end + +function SciMLBase.ImplicitDiscreteFunction{true}(sys::ImplicitDiscreteSystem, args...; kwargs...) + ImplicitDiscreteFunction{true, SciMLBase.AutoSpecialize}(sys, args...; kwargs...) +end + +function SciMLBase.ImplicitDiscreteFunction{false}(sys::ImplicitDiscreteSystem, args...; kwargs...) + ImplicitDiscreteFunction{false, SciMLBase.FullSpecialize}(sys, args...; kwargs...) +end +function SciMLBase.ImplicitDiscreteFunction{iip, specialize}( + sys::ImplicitDiscreteSystem, + dvs = unknowns(sys), + ps = parameters(sys), + u0 = nothing; + version = nothing, + p = nothing, + t = nothing, + eval_expression = false, + eval_module = @__MODULE__, + analytic = nothing, + kwargs...) where {iip, specialize} + if !iscomplete(sys) + error("A completed `ImplicitDiscreteSystem` is required. Call `complete` or `structural_simplify` on the system before creating a `ImplicitDiscreteProblem`") + end + f_gen = generate_function(sys, dvs, ps; expression = Val{true}, + expression_module = eval_module, kwargs...) + f_oop, f_iip = eval_or_rgf.(f_gen; eval_expression, eval_module) + f(u, p, t) = f_oop(u, p, t) + f(du, u, p, t) = f_iip(du, u, p, t) + + if specialize === SciMLBase.FunctionWrapperSpecialize && iip + if u0 === nothing || p === nothing || t === nothing + error("u0, p, and t must be specified for FunctionWrapperSpecialize on DiscreteFunction.") + end + f = SciMLBase.wrapfun_iip(f, (u0, u0, p, t)) + end + + observedfun = ObservedFunctionCache( + sys; eval_expression, eval_module, checkbounds = get(kwargs, :checkbounds, false)) + + ImplicitDiscreteFunction{iip, specialize}(f; + sys = sys, + observed = observedfun, + analytic = analytic) +end + +""" +```julia +ImplicitDiscreteFunctionExpr{iip}(sys::ImplicitDiscreteSystem, dvs = states(sys), + ps = parameters(sys); + version = nothing, + kwargs...) where {iip} +``` + +Create a Julia expression for an `ImplicitDiscreteFunction` from the [`ImplicitDiscreteSystem`](@ref). +The arguments `dvs` and `ps` are used to set the order of the dependent +variable and parameter vectors, respectively. +""" +struct ImplicitDiscreteFunctionExpr{iip} end +struct ImplicitDiscreteFunctionClosure{O, I} <: Function + f_oop::O + f_iip::I +end +(f::ImplicitDiscreteFunctionClosure)(u, p, t) = f.f_oop(u, p, t) +(f::ImplicitDiscreteFunctionClosure)(du, u, p, t) = f.f_iip(du, u, p, t) + +function ImplicitDiscreteFunctionExpr{iip}(sys::ImplicitDiscreteSystem, dvs = unknowns(sys), + ps = parameters(sys), u0 = nothing; + version = nothing, p = nothing, + linenumbers = false, + simplify = false, + kwargs...) where {iip} + f_oop, f_iip = generate_function(sys, dvs, ps; expression = Val{true}, kwargs...) + + fsym = gensym(:f) + _f = :($fsym = $ImplicitDiscreteFunctionClosure($f_oop, $f_iip)) + + ex = quote + $_f + DiscreteFunction{$iip}($fsym) + end + !linenumbers ? Base.remove_linenums!(ex) : ex +end + +function ImplicitDiscreteFunctionExpr(sys::ImplicitDiscreteSystem, args...; kwargs...) + ImplicitDiscreteFunctionExpr{true}(sys, args...; kwargs...) +end + From 45c2588e6e65025283b97dd473e0052c7ed6bb66 Mon Sep 17 00:00:00 2001 From: vyudu Date: Thu, 23 Jan 2025 13:40:11 -0500 Subject: [PATCH 0616/2176] fix pdesystem typecheck --- src/systems/pde/pdesystem.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/systems/pde/pdesystem.jl b/src/systems/pde/pdesystem.jl index e067f0e149..e2a5bb5734 100644 --- a/src/systems/pde/pdesystem.jl +++ b/src/systems/pde/pdesystem.jl @@ -103,7 +103,7 @@ struct PDESystem <: ModelingToolkit.AbstractMultivariateSystem description = "", name) if checks == true || (checks & CheckComponents) > 0 - check_var_types(PDESystem, dvs) + check_var_types(PDESystem, [dv(ivs...) for dv in dvs]) end if checks == true || (checks & CheckUnits) > 0 u = __get_unit_type(dvs, ivs, ps) From 9530a2e6bce78f9fdd49ed1841b9ed6120e04ad2 Mon Sep 17 00:00:00 2001 From: vyudu Date: Thu, 23 Jan 2025 16:31:25 -0500 Subject: [PATCH 0617/2176] init --- src/systems/parameter_buffer.jl | 6 +++-- src/systems/problem_utils.jl | 42 +++++++++++++++++++++++++++++++-- test/problem_validation.jl | 24 +++++++++++++++++++ 3 files changed, 68 insertions(+), 4 deletions(-) create mode 100644 test/problem_validation.jl diff --git a/src/systems/parameter_buffer.jl b/src/systems/parameter_buffer.jl index bc4e62a773..7a88489b48 100644 --- a/src/systems/parameter_buffer.jl +++ b/src/systems/parameter_buffer.jl @@ -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) @@ -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) diff --git a/src/systems/problem_utils.jl b/src/systems/problem_utils.jl index bf3d72e1e5..e2af471a15 100644 --- a/src/systems/problem_utils.jl +++ b/src/systems/problem_utils.jl @@ -684,12 +684,17 @@ function process_SciMLProblem( u0Type = typeof(u0map) pType = typeof(pmap) - _u0map = u0map + 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) @@ -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 ############## diff --git a/test/problem_validation.jl b/test/problem_validation.jl new file mode 100644 index 0000000000..fb724c55bd --- /dev/null +++ b/test/problem_validation.jl @@ -0,0 +1,24 @@ +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) + + 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 From a9dc112905ced2e1b1b16e1af0e179b0604f2563 Mon Sep 17 00:00:00 2001 From: vyudu Date: Thu, 23 Jan 2025 16:34:38 -0500 Subject: [PATCH 0618/2176] up --- test/problem_validation.jl | 1 + 1 file changed, 1 insertion(+) diff --git a/test/problem_validation.jl b/test/problem_validation.jl index fb724c55bd..f871327ae8 100644 --- a/test/problem_validation.jl +++ b/test/problem_validation.jl @@ -12,6 +12,7 @@ using ModelingToolkit: t_nounits as t, D_nounits as D 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) From 3c629ac227950886235d85307f3a82c7b3183ac7 Mon Sep 17 00:00:00 2001 From: vyudu Date: Thu, 23 Jan 2025 16:36:30 -0500 Subject: [PATCH 0619/2176] up --- src/systems/problem_utils.jl | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/systems/problem_utils.jl b/src/systems/problem_utils.jl index e2af471a15..8c6082e436 100644 --- a/src/systems/problem_utils.jl +++ b/src/systems/problem_utils.jl @@ -692,8 +692,6 @@ function process_SciMLProblem( 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) From 417b386a24a6c8c1ed8c49ee6a5c2581a562afae Mon Sep 17 00:00:00 2001 From: vyudu Date: Fri, 24 Jan 2025 08:48:29 -0500 Subject: [PATCH 0620/2176] just check not-symbolic --- src/systems/problem_utils.jl | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/systems/problem_utils.jl b/src/systems/problem_utils.jl index 8c6082e436..8541056272 100644 --- a/src/systems/problem_utils.jl +++ b/src/systems/problem_utils.jl @@ -788,10 +788,6 @@ function check_keys(sys, map) 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 From f1d2a0754ae33524a063d028b6d2515a9cabc06c Mon Sep 17 00:00:00 2001 From: vyudu Date: Fri, 24 Jan 2025 11:20:11 -0500 Subject: [PATCH 0621/2176] rename --- .../{implicitdiscretesystem.jl => implicit_discrete_system.jl} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename src/systems/discrete_system/{implicitdiscretesystem.jl => implicit_discrete_system.jl} (100%) diff --git a/src/systems/discrete_system/implicitdiscretesystem.jl b/src/systems/discrete_system/implicit_discrete_system.jl similarity index 100% rename from src/systems/discrete_system/implicitdiscretesystem.jl rename to src/systems/discrete_system/implicit_discrete_system.jl From 494df8f49bfabb5d495d50cd0d7f5b4ed39d634d Mon Sep 17 00:00:00 2001 From: Martijn Visser Date: Fri, 24 Jan 2025 21:18:01 +0100 Subject: [PATCH 0622/2176] Start using Moshi compatible syntax for singleton ADT variants In preparation for migrating from Expronicon to Moshi. --- src/clock.jl | 6 +++--- src/discretedomain.jl | 4 ++-- src/systems/clock_inference.jl | 6 +++--- src/systems/systemstructure.jl | 2 +- test/clock.jl | 30 +++++++++++++++--------------- 5 files changed, 24 insertions(+), 24 deletions(-) diff --git a/src/clock.jl b/src/clock.jl index 86b7296a6d..2cd00a24bc 100644 --- a/src/clock.jl +++ b/src/clock.jl @@ -29,7 +29,7 @@ true if `x` contains only continuous-domain signals. See also [`has_continuous_domain`](@ref) """ function is_continuous_domain(x) - issym(x) && return getmetadata(x, VariableTimeDomain, false) == Continuous + issym(x) && return getmetadata(x, VariableTimeDomain, false) == Continuous() !has_discrete_domain(x) && has_continuous_domain(x) end @@ -58,8 +58,8 @@ has_time_domain(x::Num) = has_time_domain(value(x)) has_time_domain(x) = false for op in [Differential] - @eval input_timedomain(::$op, arg = nothing) = Continuous - @eval output_timedomain(::$op, arg = nothing) = Continuous + @eval input_timedomain(::$op, arg = nothing) = Continuous() + @eval output_timedomain(::$op, arg = nothing) = Continuous() end """ diff --git a/src/discretedomain.jl b/src/discretedomain.jl index 95abe02a7a..c7f90007c5 100644 --- a/src/discretedomain.jl +++ b/src/discretedomain.jl @@ -247,7 +247,7 @@ function output_timedomain(s::Shift, arg = nothing) InferredDiscrete end -input_timedomain(::Sample, _ = nothing) = Continuous +input_timedomain(::Sample, _ = nothing) = Continuous() output_timedomain(s::Sample, _ = nothing) = s.clock function input_timedomain(h::Hold, arg = nothing) @@ -256,7 +256,7 @@ function input_timedomain(h::Hold, arg = nothing) end InferredDiscrete # the Hold accepts any discrete end -output_timedomain(::Hold, _ = nothing) = Continuous +output_timedomain(::Hold, _ = nothing) = Continuous() sampletime(op::Sample, _ = nothing) = sampletime(op.clock) sampletime(op::ShiftIndex, _ = nothing) = sampletime(op.clock) diff --git a/src/systems/clock_inference.jl b/src/systems/clock_inference.jl index a92b2aa67c..611f8e2fae 100644 --- a/src/systems/clock_inference.jl +++ b/src/systems/clock_inference.jl @@ -8,8 +8,8 @@ end function ClockInference(ts::TransformationState) @unpack structure = ts @unpack graph = structure - eq_domain = TimeDomain[Continuous for _ in 1:nsrcs(graph)] - var_domain = TimeDomain[Continuous for _ in 1:ndsts(graph)] + eq_domain = TimeDomain[Continuous() for _ in 1:nsrcs(graph)] + var_domain = TimeDomain[Continuous() for _ in 1:ndsts(graph)] inferred = BitSet() for (i, v) in enumerate(get_fullvars(ts)) d = get_time_domain(ts, v) @@ -151,7 +151,7 @@ function split_system(ci::ClockInference{S}) where {S} get!(clock_to_id, d) do cid = (cid_counter[] += 1) push!(id_to_clock, d) - if d == Continuous + if d == Continuous() continuous_id[] = cid end cid diff --git a/src/systems/systemstructure.jl b/src/systems/systemstructure.jl index 1c0ad8b8ae..1bdc11f06a 100644 --- a/src/systems/systemstructure.jl +++ b/src/systems/systemstructure.jl @@ -662,7 +662,7 @@ function structural_simplify!(state::TearingState, io = nothing; simplify = fals Dict(v => 0.0 for v in Iterators.flatten(inputs))) end ps = [sym isa CallWithMetadata ? sym : - setmetadata(sym, VariableTimeDomain, get(time_domains, sym, Continuous)) + setmetadata(sym, VariableTimeDomain, get(time_domains, sym, Continuous())) for sym in get_ps(sys)] @set! sys.ps = ps else diff --git a/test/clock.jl b/test/clock.jl index 91aaa8248e..67f469e1a6 100644 --- a/test/clock.jl +++ b/test/clock.jl @@ -77,19 +77,19 @@ k = ShiftIndex(d) d = Clock(dt) # Note that TearingState reorders the equations -@test eqmap[1] == Continuous +@test eqmap[1] == Continuous() @test eqmap[2] == d @test eqmap[3] == d @test eqmap[4] == d -@test eqmap[5] == Continuous -@test eqmap[6] == Continuous +@test eqmap[5] == Continuous() +@test eqmap[6] == Continuous() @test varmap[yd] == d @test varmap[ud] == d @test varmap[r] == d -@test varmap[x] == Continuous -@test varmap[y] == Continuous -@test varmap[u] == Continuous +@test varmap[x] == Continuous() +@test varmap[y] == Continuous() +@test varmap[u] == Continuous() @info "Testing shift normalization" dt = 0.1 @@ -192,10 +192,10 @@ eqs = [yd ~ Sample(dt)(y) @test varmap[ud1] == d @test varmap[yd2] == d2 @test varmap[ud2] == d2 - @test varmap[r] == Continuous - @test varmap[x] == Continuous - @test varmap[y] == Continuous - @test varmap[u] == Continuous + @test varmap[r] == Continuous() + @test varmap[x] == Continuous() + @test varmap[y] == Continuous() + @test varmap[u] == Continuous() @info "test composed systems" @@ -241,14 +241,14 @@ eqs = [yd ~ Sample(dt)(y) ci, varmap = infer_clocks(cl) @test varmap[f.x] == Clock(0.5) - @test varmap[p.x] == Continuous - @test varmap[p.y] == Continuous + @test varmap[p.x] == Continuous() + @test varmap[p.y] == Continuous() @test varmap[c.ud] == Clock(0.5) @test varmap[c.yd] == Clock(0.5) - @test varmap[c.y] == Continuous + @test varmap[c.y] == Continuous() @test varmap[f.y] == Clock(0.5) @test varmap[f.u] == Clock(0.5) - @test varmap[p.u] == Continuous + @test varmap[p.u] == Continuous() @test varmap[c.r] == Clock(0.5) ## Multiple clock rates @@ -474,7 +474,7 @@ eqs = [yd ~ Sample(dt)(y) ## Test continuous clock - c = ModelingToolkit.SolverStepClock + c = ModelingToolkit.SolverStepClock() k = ShiftIndex(c) @mtkmodel CounterSys begin From 2bd65e2702544b1ed561449665115247b65de121 Mon Sep 17 00:00:00 2001 From: vyudu Date: Fri, 24 Jan 2025 16:06:16 -0500 Subject: [PATCH 0623/2176] up --- src/systems/systems.jl | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/systems/systems.jl b/src/systems/systems.jl index 04c50bc766..ffbee75b33 100644 --- a/src/systems/systems.jl +++ b/src/systems/systems.jl @@ -39,13 +39,13 @@ function structural_simplify( else newsys = newsys′ end - if newsys isa DiscreteSystem && - any(eq -> symbolic_type(eq.lhs) == NotSymbolic(), equations(newsys)) - error(""" - Encountered algebraic equations when simplifying discrete system. This is \ - not yet supported. - """) - end + # if newsys isa DiscreteSystem && + # any(eq -> symbolic_type(eq.lhs) == NotSymbolic(), equations(newsys)) + # error(""" + # Encountered algebraic equations when simplifying discrete system. This is \ + # not yet supported. + # """) + # end for pass in additional_passes newsys = pass(newsys) end From 80d9bafca24006a2bb42ecd849b2b8e465f28ced Mon Sep 17 00:00:00 2001 From: Martijn Visser Date: Sat, 25 Jan 2025 20:51:57 +0100 Subject: [PATCH 0624/2176] Require latest SciMLBase --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 0a6238685a..164012d677 100644 --- a/Project.toml +++ b/Project.toml @@ -132,7 +132,7 @@ RecursiveArrayTools = "3.26" Reexport = "0.2, 1" RuntimeGeneratedFunctions = "0.5.9" SCCNonlinearSolve = "1.0.0" -SciMLBase = "2.71.1" +SciMLBase = "2.72.1" SciMLStructures = "1.0" Serialization = "1" Setfield = "0.7, 0.8, 1" From d2e31ac8553b91626b72e52dbdcf2ff083083bf3 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 20 Dec 2024 16:06:30 +0530 Subject: [PATCH 0625/2176] feat: add `FMUComponent` with support for v2 ME FMUs --- Project.toml | 3 + ext/MTKFMIExt.jl | 216 +++++++++++++++++++++++++++++++++++++++++ src/ModelingToolkit.jl | 2 + 3 files changed, 221 insertions(+) create mode 100644 ext/MTKFMIExt.jl diff --git a/Project.toml b/Project.toml index 0a6238685a..b25d2a0562 100644 --- a/Project.toml +++ b/Project.toml @@ -64,6 +64,7 @@ Unitful = "1986cc42-f94f-5a68-af5c-568840ba703d" BifurcationKit = "0f109fa4-8a5d-4b75-95aa-f515264e7665" ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4" DeepDiffs = "ab62b9b5-e342-54a8-a765-a90f495de1a6" +FMI = "14a09403-18e3-468f-ad8a-74f8dda2d9ac" HomotopyContinuation = "f213a82b-91d6-5c5d-acf7-10f1c761b327" InfiniteOpt = "20393b10-9daf-11e9-18c9-8db751c92c57" LabelledArrays = "2ee39098-c373-598a-b85f-a56591580800" @@ -72,6 +73,7 @@ LabelledArrays = "2ee39098-c373-598a-b85f-a56591580800" MTKBifurcationKitExt = "BifurcationKit" MTKChainRulesCoreExt = "ChainRulesCore" MTKDeepDiffsExt = "DeepDiffs" +MTKFMIExt = "FMI" MTKHomotopyContinuationExt = "HomotopyContinuation" MTKInfiniteOptExt = "InfiniteOpt" MTKLabelledArraysExt = "LabelledArrays" @@ -106,6 +108,7 @@ FindFirstFunctions = "1" ForwardDiff = "0.10.3" FunctionWrappers = "1.1" FunctionWrappersWrappers = "0.1" +FMI = "0.14" Graphs = "1.5.2" HomotopyContinuation = "2.11" InfiniteOpt = "0.5" diff --git a/ext/MTKFMIExt.jl b/ext/MTKFMIExt.jl new file mode 100644 index 0000000000..0812506df7 --- /dev/null +++ b/ext/MTKFMIExt.jl @@ -0,0 +1,216 @@ +module MTKFMIExt + +using ModelingToolkit +using ModelingToolkit: t_nounits as t, D_nounits as D +import ModelingToolkit as MTK +import FMI + +macro statuscheck(expr) + @assert Meta.isexpr(expr, :call) + fn = expr.args[1] + @assert Meta.isexpr(fn, :.) + @assert fn.args[1] == :FMI + fnname = fn.args[2] + + instance = expr.args[2] + + return quote + status = $expr + fnname = $fnname + if (status isa Tuple && status[1] == FMI.fmi2True) || + (!(status isa Tuple) && status != FMI.fmi2StatusOK && + status != FMI.fmi2StatusWarning) + if status != FMI.fmi2StatusFatal + FMI.fmi2Terminate(wrapper.instance) + end + FMI.fmi2FreeInstance!(wrapper.instance) + wrapper.instance = nothing + error("FMU Error: status $status") + end + end |> esc +end + +function MTK.FMIComponent(::Val{2}, ::Val{:ME}; fmu = nothing, tolerance = 1e-6, name) + value_references = Dict() + defs = Dict() + states = [] + diffvars = [] + observed = Equation[] + stateT = Float64 + for (valRef, snames) in FMI.getStateValueReferencesAndNames(fmu) + stateT = FMI.dataTypeForValueReference(fmu, valRef) + snames = map(parseFMIVariableName, snames) + vars = [MTK.unwrap(only(@variables $sname(t)::stateT)) for sname in snames] + for i in eachindex(vars) + if i == 1 + push!(diffvars, vars[i]) + else + push!(observed, vars[i] ~ vars[1]) + end + value_references[vars[i]] = valRef + end + append!(states, vars) + end + + inputs = [] + for (valRef, snames) in FMI.getInputValueReferencesAndNames(fmu) + snames = map(parseFMIVariableName, snames) + vars = [MTK.unwrap(only(@variables $sname(t)::stateT)) for sname in snames] + for i in eachindex(vars) + if i == 1 + push!(inputs, vars[i]) + else + push!(observed, vars[i] ~ vars[1]) + end + value_references[vars[i]] = valRef + end + append!(states, vars) + end + + outputs = [] + for (valRef, snames) in FMI.getOutputValueReferencesAndNames(fmu) + snames = map(parseFMIVariableName, snames) + vars = [MTK.unwrap(only(@variables $sname(t)::stateT)) for sname in snames] + for i in eachindex(vars) + if i == 1 + push!(outputs, vars[i]) + else + push!(observed, vars[i] ~ vars[1]) + end + value_references[vars[i]] = valRef + end + append!(states, vars) + end + + params = [] + parameter_dependencies = Equation[] + for (valRef, pnames) in FMI.getParameterValueReferencesAndNames(fmu) + defval = FMI.getStartValue(fmu, valRef) + T = FMI.dataTypeForValueReference(fmu, valRef) + pnames = map(parseFMIVariableName, pnames) + vars = [MTK.unwrap(only(@parameters $pname::T)) for pname in pnames] + for i in eachindex(vars) + if i == 1 + push!(params, vars[i]) + else + push!(parameter_dependencies, vars[i] ~ vars[1]) + end + value_references[vars[i]] = valRef + end + defs[vars[1]] = defval + end + + input_value_references = UInt32[value_references[var] for var in inputs] + param_value_references = UInt32[value_references[var] for var in params] + @parameters wrapper::FMI2InstanceWrapper = FMI2InstanceWrapper( + fmu, param_value_references, input_value_references, tolerance) + + output_value_references = UInt32[value_references[var] for var in outputs] + buffer_length = length(diffvars) + length(outputs) + _functor = FMI2MEFunctor(zeros(buffer_length), output_value_references) + @parameters (functor::(typeof(_functor)))(..)[1:buffer_length] = _functor + call_expr = functor(wrapper, copy(diffvars), copy(inputs), copy(params), t) + + diffeqs = Equation[] + for (i, var) in enumerate([D.(diffvars); outputs]) + push!(diffeqs, var ~ call_expr[i]) + end + + finalize_affect = MTK.FunctionalAffect(fmi2MEFinalize!, [], [wrapper], []) + step_affect = MTK.FunctionalAffect(fmi2MEStep!, [], [wrapper], []) + instance_management_callback = MTK.SymbolicDiscreteCallback( + (t != t - 1), step_affect; finalize = finalize_affect) + + push!(params, wrapper, functor) + eqs = [observed; diffeqs] + return ODESystem(eqs, t, states, params; parameter_dependencies, defaults = defs, + discrete_events = [instance_management_callback], name) +end + +function parseFMIVariableName(name::AbstractString) + return Symbol(replace(name, "." => "__")) +end + +mutable struct FMI2InstanceWrapper + const fmu::FMI.FMU2 + const param_value_references::Vector{UInt32} + const input_value_references::Vector{UInt32} + const tolerance::FMI.fmi2Real + instance::Union{FMI.FMU2Component, Nothing} +end + +function FMI2InstanceWrapper(fmu, params, inputs, tolerance) + FMI2InstanceWrapper(fmu, params, inputs, tolerance, nothing) +end + +function get_instance!(wrapper::FMI2InstanceWrapper, states, inputs, params, t) + if wrapper.instance === nothing + wrapper.instance = FMI.fmi2Instantiate!(wrapper.fmu)::FMI.FMU2Component + if !isempty(params) + @statuscheck FMI.fmi2SetReal(wrapper.instance, wrapper.param_value_references, + Csize_t(length(wrapper.param_value_references)), params) + end + @statuscheck FMI.fmi2SetupExperiment( + wrapper.instance, FMI.fmi2True, wrapper.tolerance, t, FMI.fmi2False, t) + @statuscheck FMI.fmi2EnterInitializationMode(wrapper.instance) + if !isempty(inputs) + @statuscheck FMI.fmi2SetReal(wrapper.instance, wrapper.input_value_references, + Csize_t(length(wrapper.param_value_references)), inputs) + end + @statuscheck FMI.fmi2ExitInitializationMode(wrapper.instance) + eventInfo = FMI.fmi2NewDiscreteStates(wrapper.instance) + @assert eventInfo.newDiscreteStatesNeeded == FMI.fmi2False + # TODO: Support FMU events + @statuscheck FMI.fmi2EnterContinuousTimeMode(wrapper.instance) + end + instance = wrapper.instance + @statuscheck FMI.fmi2SetTime(instance, t) + @statuscheck FMI.fmi2SetContinuousStates(instance, states) + if !isempty(inputs) + @statuscheck FMI.fmi2SetReal(instance, wrapper.input_value_references, + Csize_t(length(wrapper.param_value_references)), inputs) + end + + return instance +end + +function complete_step!(wrapper::FMI2InstanceWrapper) + wrapper.instance === nothing && return + @statuscheck FMI.fmi2CompletedIntegratorStep(wrapper.instance, FMI.fmi2True) +end + +function reset_instance!(wrapper::FMI2InstanceWrapper) + wrapper.instance === nothing && return + FMI.fmi2Terminate(wrapper.instance) + FMI.fmi2FreeInstance!(wrapper.instance) + wrapper.instance = nothing +end + +struct FMI2MEFunctor{T} + return_buffer::Vector{T} + output_value_references::Vector{UInt32} +end + +function (fn::FMI2MEFunctor)(wrapper::FMI2InstanceWrapper, states, inputs, params, t) + instance = get_instance!(wrapper, states, inputs, params, t) + + states_buffer = zeros(length(states)) + @statuscheck FMI.fmi2GetDerivatives!(instance, states_buffer) + outputs_buffer = zeros(length(fn.output_value_references)) + FMI.fmi2GetReal!(instance, fn.output_value_references, outputs_buffer) + return [states_buffer; outputs_buffer] +end + +function fmi2MEStep!(integrator, u, p, ctx) + wrapper_idx = p[1] + wrapper = integrator.ps[wrapper_idx] + complete_step!(wrapper) +end + +function fmi2MEFinalize!(integrator, u, p, ctx) + wrapper_idx = p[1] + wrapper = integrator.ps[wrapper_idx] + reset_instance!(wrapper) +end + +end # module diff --git a/src/ModelingToolkit.jl b/src/ModelingToolkit.jl index 743dc8ce76..2710d7d1e4 100644 --- a/src/ModelingToolkit.jl +++ b/src/ModelingToolkit.jl @@ -297,4 +297,6 @@ export HomotopyContinuationProblem export AnalysisPoint, get_sensitivity_function, get_comp_sensitivity_function, get_looptransfer_function, get_sensitivity, get_comp_sensitivity, get_looptransfer, open_loop +function FMIComponent end + end # module From 310c5f29d3d97257ec64b736ba8015caeb9ef7b7 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 20 Dec 2024 16:32:39 +0530 Subject: [PATCH 0626/2176] test: add tests for FMIComponent --- test/extensions/Project.toml | 2 ++ test/extensions/fmi.jl | 19 +++++++++++++++++++ test/runtests.jl | 1 + 3 files changed, 22 insertions(+) create mode 100644 test/extensions/fmi.jl diff --git a/test/extensions/Project.toml b/test/extensions/Project.toml index 5f7afe222a..72501554b5 100644 --- a/test/extensions/Project.toml +++ b/test/extensions/Project.toml @@ -2,6 +2,8 @@ BifurcationKit = "0f109fa4-8a5d-4b75-95aa-f515264e7665" ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4" ChainRulesTestUtils = "cdddcdb0-9152-4a09-a978-84456f9df70a" +FMI = "14a09403-18e3-468f-ad8a-74f8dda2d9ac" +FMIZoo = "724179cf-c260-40a9-bd27-cccc6fe2f195" ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210" HomotopyContinuation = "f213a82b-91d6-5c5d-acf7-10f1c761b327" InfiniteOpt = "20393b10-9daf-11e9-18c9-8db751c92c57" diff --git a/test/extensions/fmi.jl b/test/extensions/fmi.jl new file mode 100644 index 0000000000..b11c5f9a31 --- /dev/null +++ b/test/extensions/fmi.jl @@ -0,0 +1,19 @@ +using ModelingToolkit, FMI, FMIZoo, OrdinaryDiffEq +using ModelingToolkit: t_nounits as t, D_nounits as D +import ModelingToolkit as MTK + +@testset "Standalone pendulum model" begin + @testset "v2, ME" begin + fmu = loadFMU("SpringPendulum1D", "Dymola", "2022x"; type = :ME) + @mtkbuild sys = MTK.FMIComponent(Val(2), Val(:ME); fmu) + prob = ODEProblem{true, SciMLBase.FullSpecialize}( + sys, [sys.mass__s => 0.5, sys.mass__v => 0.0], (0.0, 8.0)) + sol = solve(prob, Tsit5(); reltol = 1e-8, abstol = 1e-8) + @test SciMLBase.successful_retcode(sol) + + truesol = FMI.simulate(fmu, (0.0, 8.0); saveat = 0.0:0.1:8.0) + @test sol(0.0:0.1:8.0).u≈truesol.states.u atol=1e-4 + # repeated solve works + @test_nowarn solve(prob, Tsit5()) + end +end diff --git a/test/runtests.jl b/test/runtests.jl index 95154e550e..bfb9e0b6f9 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -117,6 +117,7 @@ end if GROUP == "All" || GROUP == "Extensions" activate_extensions_env() + @safetestset "FMI Extension Test" include("extensions/fmi.jl") @safetestset "HomotopyContinuation Extension Test" include("extensions/homotopy_continuation.jl") @safetestset "Auto Differentiation Test" include("extensions/ad.jl") @safetestset "LabelledArrays Test" include("labelledarrays.jl") From 9bbcf7c7a2b5d02c96ba9ec8c22075f783a7b564 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 23 Dec 2024 17:26:23 +0530 Subject: [PATCH 0627/2176] fix: fix `getcalledparameter` namespacing issues --- src/parameters.jl | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/parameters.jl b/src/parameters.jl index ced7767718..91121b7cbb 100644 --- a/src/parameters.jl +++ b/src/parameters.jl @@ -33,7 +33,12 @@ end function getcalledparameter(x) x = unwrap(x) - return getmetadata(x, CallWithParent) + # `parent` is a `CallWithMetadata` with the correct metadata, + # but no namespacing. `operation(x)` has the correct namespacing, + # but is not a `CallWithMetadata` and doesn't have any metadata. + # This approach combines both. + parent = getmetadata(x, CallWithParent) + return CallWithMetadata(operation(x), metadata(parent)) end """ From 721430c913cdbf1f98ca913ce9d5f61334f94d19 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 23 Dec 2024 17:26:42 +0530 Subject: [PATCH 0628/2176] fix: handle scalarized called parameter in `vars!` --- src/utils.jl | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/utils.jl b/src/utils.jl index 5e4f0b52d2..b6544972b9 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -403,6 +403,9 @@ function vars!(vars, eq::Equation; op = Differential) end function vars!(vars, O; op = Differential) if isvariable(O) + if iscall(O) && operation(O) === getindex && iscalledparameter(first(arguments(O))) + O = first(arguments(O)) + end if iscalledparameter(O) f = getcalledparameter(O) push!(vars, f) From a20ba60f9daf536db41ba0d0a987a067d07df8f0 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 23 Dec 2024 17:27:08 +0530 Subject: [PATCH 0629/2176] fix: run array variables hack on equations added by CSE hack --- src/structural_transformation/symbolics_tearing.jl | 1 + 1 file changed, 1 insertion(+) diff --git a/src/structural_transformation/symbolics_tearing.jl b/src/structural_transformation/symbolics_tearing.jl index 8161a9572b..bd4868125e 100644 --- a/src/structural_transformation/symbolics_tearing.jl +++ b/src/structural_transformation/symbolics_tearing.jl @@ -696,6 +696,7 @@ function cse_and_array_hacks(obs, subeqs, unknowns, neweqs; cse = true, array = tempvar; T = Symbolics.symtype(rhs_arr))) tempvar = setmetadata( tempvar, Symbolics.ArrayShapeCtx, Symbolics.shape(rhs_arr)) + vars!(all_vars, rhs_arr) tempeq = tempvar ~ rhs_arr rhs_to_tempvar[rhs_arr] = tempvar push!(obs, tempeq) From af92fe8cbc718c66ed9d601b9b66d93cbce34efe Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 23 Dec 2024 17:27:31 +0530 Subject: [PATCH 0630/2176] fix: only use `OffsetArray` in array hack for non-standard `firstindex` --- src/structural_transformation/symbolics_tearing.jl | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/structural_transformation/symbolics_tearing.jl b/src/structural_transformation/symbolics_tearing.jl index bd4868125e..4382ad6284 100644 --- a/src/structural_transformation/symbolics_tearing.jl +++ b/src/structural_transformation/symbolics_tearing.jl @@ -738,7 +738,9 @@ function cse_and_array_hacks(obs, subeqs, unknowns, neweqs; cse = true, array = # try to `create_array(OffsetArray{...}, ...)` which errors. # `term(Origin(firstind), scal)` doesn't retain the `symtype` and `size` # of `scal`. - push!(obs_arr_eqs, arrvar ~ change_origin(Origin(firstind), scal)) + rhs = scal + rhs = change_origin(firstind, rhs) + push!(obs_arr_eqs, arrvar ~ rhs) end append!(obs, obs_arr_eqs) append!(subeqs, obs_arr_eqs) @@ -765,10 +767,10 @@ getindex_wrapper(x, i) = x[i...] # PART OF HACK 2 function change_origin(origin, arr) - return origin(arr) + return Origin(origin)(arr) end -@register_array_symbolic change_origin(origin::Origin, arr::AbstractArray) begin +@register_array_symbolic change_origin(origin::Any, arr::AbstractArray) begin size = size(arr) eltype = eltype(arr) ndims = ndims(arr) From 3a8a0e1c728f2172443dcc7f6fe949cf4d49f68e Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 23 Dec 2024 17:28:22 +0530 Subject: [PATCH 0631/2176] fix: handle usage of FMU in initialization --- ext/MTKFMIExt.jl | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/ext/MTKFMIExt.jl b/ext/MTKFMIExt.jl index 0812506df7..fc9171fab8 100644 --- a/ext/MTKFMIExt.jl +++ b/ext/MTKFMIExt.jl @@ -191,6 +191,14 @@ struct FMI2MEFunctor{T} output_value_references::Vector{UInt32} end +@register_array_symbolic (fn::FMI2MEFunctor)( + wrapper::FMI2InstanceWrapper, states::Vector{<:Real}, + inputs::Vector{<:Real}, params::Vector{<:Real}, t::Real) begin + size = (length(states) + length(fn.output_value_references),) + eltype = eltype(states) + ndims = 1 +end + function (fn::FMI2MEFunctor)(wrapper::FMI2InstanceWrapper, states, inputs, params, t) instance = get_instance!(wrapper, states, inputs, params, t) From b75a49fa8669663780c6f2bede1de6492ba753f0 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 23 Dec 2024 17:28:52 +0530 Subject: [PATCH 0632/2176] refactor: modularize code and enable array hacks --- ext/MTKFMIExt.jl | 107 +++++++++++++++++++++++------------------------ 1 file changed, 53 insertions(+), 54 deletions(-) diff --git a/ext/MTKFMIExt.jl b/ext/MTKFMIExt.jl index fc9171fab8..8b6f294601 100644 --- a/ext/MTKFMIExt.jl +++ b/ext/MTKFMIExt.jl @@ -36,68 +36,43 @@ function MTK.FMIComponent(::Val{2}, ::Val{:ME}; fmu = nothing, tolerance = 1e-6, states = [] diffvars = [] observed = Equation[] - stateT = Float64 - for (valRef, snames) in FMI.getStateValueReferencesAndNames(fmu) - stateT = FMI.dataTypeForValueReference(fmu, valRef) - snames = map(parseFMIVariableName, snames) - vars = [MTK.unwrap(only(@variables $sname(t)::stateT)) for sname in snames] - for i in eachindex(vars) - if i == 1 - push!(diffvars, vars[i]) - else - push!(observed, vars[i] ~ vars[1]) - end - value_references[vars[i]] = valRef - end - append!(states, vars) + fmi_variables_to_mtk_variables!(fmu, FMI.getStateValueReferencesAndNames(fmu), + value_references, diffvars, states, observed) + if isempty(diffvars) + __mtk_internal_u = [] + else + @variables __mtk_internal_u(t)[1:length(diffvars)] + push!(observed, __mtk_internal_u ~ copy(diffvars)) + push!(states, __mtk_internal_u) end inputs = [] - for (valRef, snames) in FMI.getInputValueReferencesAndNames(fmu) - snames = map(parseFMIVariableName, snames) - vars = [MTK.unwrap(only(@variables $sname(t)::stateT)) for sname in snames] - for i in eachindex(vars) - if i == 1 - push!(inputs, vars[i]) - else - push!(observed, vars[i] ~ vars[1]) - end - value_references[vars[i]] = valRef - end - append!(states, vars) + fmi_variables_to_mtk_variables!(fmu, FMI.getInputValueReferencesAndNames(fmu), + value_references, inputs, states, observed) + if isempty(inputs) + __mtk_internal_x = [] + else + @variables __mtk_internal_x(t)[1:length(inputs)] + push!(observed, __mtk_internal_x ~ copy(inputs)) + push!(states, __mtk_internal_x) end outputs = [] - for (valRef, snames) in FMI.getOutputValueReferencesAndNames(fmu) - snames = map(parseFMIVariableName, snames) - vars = [MTK.unwrap(only(@variables $sname(t)::stateT)) for sname in snames] - for i in eachindex(vars) - if i == 1 - push!(outputs, vars[i]) - else - push!(observed, vars[i] ~ vars[1]) - end - value_references[vars[i]] = valRef - end - append!(states, vars) - end + fmi_variables_to_mtk_variables!(fmu, FMI.getOutputValueReferencesAndNames(fmu), + value_references, outputs, states, observed) + # @variables __mtk_internal_o(t)[1:length(outputs)] + # push!(observed, __mtk_internal_o ~ outputs) params = [] parameter_dependencies = Equation[] - for (valRef, pnames) in FMI.getParameterValueReferencesAndNames(fmu) - defval = FMI.getStartValue(fmu, valRef) - T = FMI.dataTypeForValueReference(fmu, valRef) - pnames = map(parseFMIVariableName, pnames) - vars = [MTK.unwrap(only(@parameters $pname::T)) for pname in pnames] - for i in eachindex(vars) - if i == 1 - push!(params, vars[i]) - else - push!(parameter_dependencies, vars[i] ~ vars[1]) - end - value_references[vars[i]] = valRef - end - defs[vars[1]] = defval + fmi_variables_to_mtk_variables!( + fmu, FMI.getParameterValueReferencesAndNames(fmu), value_references, + params, [], parameter_dependencies, defs; parameters = true) + if isempty(params) + __mtk_internal_p = [] + else + @parameters __mtk_internal_p[1:length(params)] + push!(parameter_dependencies, __mtk_internal_p ~ copy(params)) end input_value_references = UInt32[value_references[var] for var in inputs] @@ -109,7 +84,7 @@ function MTK.FMIComponent(::Val{2}, ::Val{:ME}; fmu = nothing, tolerance = 1e-6, buffer_length = length(diffvars) + length(outputs) _functor = FMI2MEFunctor(zeros(buffer_length), output_value_references) @parameters (functor::(typeof(_functor)))(..)[1:buffer_length] = _functor - call_expr = functor(wrapper, copy(diffvars), copy(inputs), copy(params), t) + call_expr = functor(wrapper, __mtk_internal_u, __mtk_internal_x, __mtk_internal_p, t) diffeqs = Equation[] for (i, var) in enumerate([D.(diffvars); outputs]) @@ -127,6 +102,30 @@ function MTK.FMIComponent(::Val{2}, ::Val{:ME}; fmu = nothing, tolerance = 1e-6, discrete_events = [instance_management_callback], name) end +function fmi_variables_to_mtk_variables!(fmu, varmap, value_references, truevars, allvars, + obseqs, defs = Dict(); parameters = false) + for (valRef, snames) in varmap + stateT = FMI.dataTypeForValueReference(fmu, valRef) + snames = map(parseFMIVariableName, snames) + if parameters + vars = [MTK.unwrap(only(@parameters $sname::stateT)) for sname in snames] + else + vars = [MTK.unwrap(only(@variables $sname(t)::stateT)) for sname in snames] + end + for i in eachindex(vars) + if i == 1 + push!(truevars, vars[i]) + else + push!(obseqs, vars[i] ~ vars[1]) + end + value_references[vars[i]] = valRef + end + append!(allvars, vars) + defval = FMI.getStartValue(fmu, valRef) + defs[vars[1]] = defval + end +end + function parseFMIVariableName(name::AbstractString) return Symbol(replace(name, "." => "__")) end From 537b561a3b32b9f6c1ffa761ae92eebd7830c9ea Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 23 Dec 2024 17:29:19 +0530 Subject: [PATCH 0633/2176] test: add test for component FMU hooked up to MTK model, initialization --- test/extensions/fmi.jl | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/test/extensions/fmi.jl b/test/extensions/fmi.jl index b11c5f9a31..7e5e01f76a 100644 --- a/test/extensions/fmi.jl +++ b/test/extensions/fmi.jl @@ -17,3 +17,20 @@ import ModelingToolkit as MTK @test_nowarn solve(prob, Tsit5()) end end + +@testset "IO Model" begin + fmu = loadFMU("./fmus/SimpleAdder.fmu"; type = :ME) + @named adder = MTK.FMIComponent(Val(2), Val(:ME); fmu) + @variables a(t) b(t) c(t) [guess = 1.0] + @mtkbuild sys = ODESystem( + [adder.a ~ a, adder.b ~ b, D(a) ~ t, + D(b) ~ adder.out + adder.c, c^2 ~ adder.out + adder.value], + t; + systems = [adder]) + + # c will be solved for by initialization + # this tests that initialization also works with FMUs + prob = ODEProblem(sys, [sys.adder.c => 1.0, sys.a => 1.0, sys.b => 1.0], (0.0, 1.0)) + sol = solve(prob, Rodas5P(autodiff = false)) + @test SciMLBase.successful_retcode(sol) +end From 71762a8e30e4c3e81d8a37802f866894e9224dbe Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 23 Dec 2024 17:31:28 +0530 Subject: [PATCH 0634/2176] test: add `SimpleAdder.fmu` for testing --- test/extensions/fmus/SimpleAdder.fmu | Bin 0 -> 658043 bytes 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 test/extensions/fmus/SimpleAdder.fmu diff --git a/test/extensions/fmus/SimpleAdder.fmu b/test/extensions/fmus/SimpleAdder.fmu new file mode 100644 index 0000000000000000000000000000000000000000..87248ce050479793a8566b3c61b097de464a48f1 GIT binary patch literal 658043 zcmagFXE{m+$Poc`{A2me<`eDCQ! z+fQybo}cW!M2wAy?-7s#J~{r+?F}TocmE-C@$iF(EdM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SKX3ZPyJ0cZ5AzzPnHxW8&>11fbE(p%FGOeB3k`e$WKY?tM z_gag3i{l)MrL#Nw3>-H^$PEX%Cdq|8quS^)N-zSIk(NBMT)YcdHhzyQ@N^V0(9$g+ zB+HeCG6rw^K8l6P6*0E?j1YW7ZANh^dFg>`=cgE29Kppb+hFOc#GyA;WfaYqm{Z&r z;vR6Dxzjc1OP{^59RbJ`H%2_e*W-aDe*07Zht(D@zof$~8(CFbnrf^2IZgSknQR%K z>gTMJvNzD5#fbHw&BEi~(UpJdO+gwE2n84g;4eVk-;an#`CX@fNu&RtyZ?dJ{g*ZS zmxVw1|J_mZf71WRhhWKXc;A0<@ccgt{wM3dJ9_?4`XBiay!^Y`|MDREf3BRr<5~Y4 zMgNz4_zV38{rsPz|C5dX>R|f6*VVr|^#>;VmwX7KO91>ApxJ+R&g4q|smd`~TYb|I-yKNP~j?(FXJTllc38Rqg-h>Hh&&2RSGJ literal 0 HcmV?d00001 From ba2a8aec9e2fc607621972507d074bea702fec5e Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 3 Jan 2025 14:06:14 +0530 Subject: [PATCH 0649/2176] test: update `SimpleAdder.fmu` --- test/extensions/fmus/SimpleAdder.fmu | Bin 658043 -> 658163 bytes 1 file changed, 0 insertions(+), 0 deletions(-) diff --git a/test/extensions/fmus/SimpleAdder.fmu b/test/extensions/fmus/SimpleAdder.fmu index 87248ce050479793a8566b3c61b097de464a48f1..a1c3fd2728376ebed8f8dfd81819b0be51c84bef 100644 GIT binary patch delta 199526 zcmYg$XH?Tq(5{GpbPy2)0qIH=kX|Cvq)QX&2q^qPdWVpoB3-KVE={B>MS4Q-y+|kY z&=UwHkdR#8_ndp~-E;QS?1!Ca=IqS#>|j%#pi`evzt$$cLvibW)=#yJBx(+_Y#(RG ze>;7B?)H81|7Us4|NmQ9$p4)-vyKIIKTj$U{lAvqb1Xyv+mT?J(xV^ECd->$JxHeb z<|!U~eX^t)nb}54HeuD0=sqFm0+h|)$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@0?Yf--UG&AE{^1c(oW{o%O@sNjTM97B^I(Gb zQQqm#;pB@)g&O2C!t^ENbcgFcpYvk?<{S_bwoFbZDkE3Dho$OJpMAT6&;x}+iR_~# zgjQZ2^K(Bex|i+hG#2zDIcQ|nMyI=xhamDzJM@B3WEC(Blck3Z!|!pHkDox61JEJ9 zGk#&1O-aNl8Kerm{)9{6Hh9~|KLTtGXX;2>ep7?E;8V7~R&YVlfp-u5-FV4ThO0BU zE0W0W`VbHE#%(FAp3}zii>~)d%maUQw1nN7Af60G?SCaO$Uot3k-z7$Sc9xnw)FM$ z=XG7gSD^dwi5-l@VEDyfq&BQWeCn2=-t+zZgF^gx%qUkwD#{~n!?ojJ5BYOhXV8z5 z$bAaCfRgAdn1QYEFV=RD!@yPm3aIa<41kc9j3k1OQlRB1r32=||5JQa0uIxX>5550 t@ROEIQ_uzzC6d8tMcgETl6GC8L7Y3K4eYPd>}UUZ5{a>><_gS~;C}*K<=FrL From be249252145c7eb05068f8117e4e2b4bd9a763cc Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 3 Jan 2025 16:54:08 +0530 Subject: [PATCH 0650/2176] refactor: remove redundant fields from structs --- ext/MTKFMIExt.jl | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/ext/MTKFMIExt.jl b/ext/MTKFMIExt.jl index e0d59c4032..00ac91fb46 100644 --- a/ext/MTKFMIExt.jl +++ b/ext/MTKFMIExt.jl @@ -128,7 +128,7 @@ function MTK.FMIComponent(::Val{Ver}; fmu = nothing, tolerance = 1e-6, if type == :ME FunctorT = Ver == 2 ? FMI2MEFunctor : FMI3MEFunctor - _functor = FunctorT(zeros(buffer_length), output_value_references) + _functor = FunctorT(output_value_references) @parameters (functor::(typeof(_functor)))(..)[1:buffer_length] = _functor call_expr = functor( wrapper, __mtk_internal_u, __mtk_internal_x, __mtk_internal_p, t) @@ -147,11 +147,8 @@ function MTK.FMIComponent(::Val{Ver}; fmu = nothing, tolerance = 1e-6, push!(states, __mtk_internal_u) elseif type == :CS state_value_references = UInt32[value_references[var] for var in diffvars] - state_and_output_value_references = vcat( - state_value_references, output_value_references) _functor = if Ver == 2 - FMI2CSFunctor(state_and_output_value_references, - state_value_references, output_value_references) + FMI2CSFunctor(state_value_references, output_value_references) else FMI3CSFunctor(state_value_references, output_value_references) end @@ -351,9 +348,8 @@ function reset_instance!(wrapper::FMI3InstanceWrapper) wrapper.instance = nothing end -struct FMI2MEFunctor{T} - return_buffer::Vector{T} - output_value_references::Vector{UInt32} +struct FMI2MEFunctor + output_value_references::Vector{FMI.fmi2ValueReference} end @register_array_symbolic (fn::FMI2MEFunctor)( @@ -385,9 +381,8 @@ function (fn::FMI2MEFunctor)(wrapper::FMI2InstanceWrapper, states, inputs, param return [states_buffer; outputs_buffer] end -struct FMI3MEFunctor{T} - return_buffer::Vector{T} - output_value_references::Vector{UInt32} +struct FMI3MEFunctor + output_value_references::Vector{FMI.fmi3ValueReference} end @register_array_symbolic (fn::FMI3MEFunctor)( @@ -431,7 +426,6 @@ function fmiFinalize!(integrator, u, p, ctx) end struct FMI2CSFunctor - state_and_output_value_references::Vector{UInt32} state_value_references::Vector{UInt32} output_value_references::Vector{UInt32} end From 7b5e543e2e427be0596853fcaa3492980487557b Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 3 Jan 2025 16:54:24 +0530 Subject: [PATCH 0651/2176] docs: add documentation for all functions in FMIExt --- ext/MTKFMIExt.jl | 421 +++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 392 insertions(+), 29 deletions(-) diff --git a/ext/MTKFMIExt.jl b/ext/MTKFMIExt.jl index 00ac91fb46..4fea249dd9 100644 --- a/ext/MTKFMIExt.jl +++ b/ext/MTKFMIExt.jl @@ -3,10 +3,19 @@ module MTKFMIExt using ModelingToolkit using SymbolicIndexingInterface using ModelingToolkit: t_nounits as t, D_nounits as D +using DocStringExtensions import ModelingToolkit as MTK import SciMLBase import FMI +""" + $(TYPEDSIGNATURES) + +A utility macro for FMI.jl functions that return a status. Will terminate on +fatal statuses. Must be used as `@statuscheck FMI.fmiXFunction(...)` where +`X` should be `2` or `3`. Has an edge case for handling tuples for +`FMI.fmi2CompletedIntegratorStep`. +""" macro statuscheck(expr) @assert Meta.isexpr(expr, :call) fn = expr.args[1] @@ -40,6 +49,12 @@ macro statuscheck(expr) end @static if !hasmethod(FMI.getValueReferencesAndNames, Tuple{FMI.fmi3ModelDescription}) + """ + $(TYPEDSIGNATURES) + + This is type piracy, but FMI.jl is missing this implementation. It allows + `FMI.getStateValueReferencesAndNames` to work. + """ function FMI.getValueReferencesAndNames( md::FMI.fmi3ModelDescription; vrs = md.valueReferences) dict = Dict{FMI.fmi3ValueReference, Array{String}}() @@ -50,6 +65,29 @@ end end end +""" + $(TYPEDSIGNATURES) + +A component that wraps an FMU loaded via FMI.jl. The FMI version (2 or 3) should be +provided as a `Val` to the function. Supports Model Exchange and CoSimulation FMUs. +All inputs, continuous variables and outputs must be `FMI.fmi2Real` or `FMI.fmi3Float64`. +Does not support events or discrete variables in the FMU. Does not support automatic +differentiation. Parameters of the FMU will have defaults corresponding to their initial +values in the FMU specification. All other variables will not have a default. Hierarchical +names in the FMU of the form `namespace.variable` are transformed into symbolic variables +with the name `namespace__variable`. + +# Keyword Arguments + +- `fmu`: The FMU loaded via `FMI.loadFMU`. +- `tolerance`: The tolerance to provide to the FMU. Not used for v3 FMUs since it is not + supported by FMI.jl. +- `communication_step_size`: The periodic interval at which communication with CoSimulation + FMUs will occur. Must be provided for CoSimulation FMU components. +- `type`: Either `:ME` or `:CS` depending on whether `fmu` is a Model Exchange or + CoSimulation FMU respectively. +- `name`: The name of the system. +""" function MTK.FMIComponent(::Val{Ver}; fmu = nothing, tolerance = 1e-6, communication_step_size = nothing, type, name) where {Ver} if Ver != 2 && Ver != 3 @@ -58,26 +96,44 @@ function MTK.FMIComponent(::Val{Ver}; fmu = nothing, tolerance = 1e-6, if type == :CS && communication_step_size === nothing throw(ArgumentError("`communication_step_size` must be specified for Co-Simulation FMUs.")) end + # mapping from MTK variable to value reference value_references = Dict() + # defaults defs = Dict() + # unknowns of the system states = [] + # differential variables of the system + # this is a subset of `states` in the case where the FMU has multiple names for + # the same value reference. diffvars = [] + # observed equations observed = Equation[] + # parse states fmi_variables_to_mtk_variables!(fmu, FMI.getStateValueReferencesAndNames(fmu), value_references, diffvars, states, observed) + # create a symbolic variable __mtk_internal_u to pass to the relevant registered + # functions as the state vector if isempty(diffvars) + # no differential variables __mtk_internal_u = [] elseif type == :ME + # ME FMUs perform integration using the Julia solver, so unknowns of the FMU + # are unknowns of the `ODESystem` @variables __mtk_internal_u(t)[1:length(diffvars)] [guess = diffvars] push!(observed, __mtk_internal_u ~ copy(diffvars)) elseif type == :CS + # CS FMUs do their own independent integration in a periodic callback, so their + # unknowns are discrete variables in the `ODESystem`. A default of `missing` allows + # them to be solved for during initialization. @parameters __mtk_internal_u(t)[1:length(diffvars)]=missing [guess = diffvars] push!(observed, __mtk_internal_u ~ copy(diffvars)) end + # parse the inputs to the FMU inputs = [] fmi_variables_to_mtk_variables!(fmu, FMI.getInputValueReferencesAndNames(fmu), value_references, inputs, states, observed) + # create a symbolic variable for the input buffer if isempty(inputs) __mtk_internal_x = [] else @@ -86,9 +142,12 @@ function MTK.FMIComponent(::Val{Ver}; fmu = nothing, tolerance = 1e-6, push!(states, __mtk_internal_x) end + # parse the outputs of the FMU outputs = [] fmi_variables_to_mtk_variables!(fmu, FMI.getOutputValueReferencesAndNames(fmu), value_references, outputs, states, observed) + # create the output buffer. This is only required for CoSimulation to pass it to + # the callback affect if type == :CS if isempty(outputs) __mtk_internal_o = [] @@ -98,11 +157,14 @@ function MTK.FMIComponent(::Val{Ver}; fmu = nothing, tolerance = 1e-6, end end + # parse the parameters params = [] + # multiple names for the same parameter are treated as parameter dependencies. parameter_dependencies = Equation[] fmi_variables_to_mtk_variables!( fmu, FMI.getParameterValueReferencesAndNames(fmu), value_references, params, [], parameter_dependencies, defs; parameters = true) + # create a symbolic variable for the parameter buffer if isempty(params) __mtk_internal_p = [] else @@ -113,6 +175,8 @@ function MTK.FMIComponent(::Val{Ver}; fmu = nothing, tolerance = 1e-6, input_value_references = UInt32[value_references[var] for var in inputs] param_value_references = UInt32[value_references[var] for var in params] + # create a parameter for the instance wrapper + # this manages the creation and deallocation of FMU instances if Ver == 2 @parameters wrapper::FMI2InstanceWrapper = FMI2InstanceWrapper( fmu, param_value_references, input_value_references, tolerance) @@ -124,20 +188,28 @@ function MTK.FMIComponent(::Val{Ver}; fmu = nothing, tolerance = 1e-6, output_value_references = UInt32[value_references[var] for var in outputs] buffer_length = length(diffvars) + length(outputs) + # any additional initialization equations for the system initialization_eqs = Equation[] if type == :ME + # the functor is a callable struct which returns the state derivative and + # output values FunctorT = Ver == 2 ? FMI2MEFunctor : FMI3MEFunctor _functor = FunctorT(output_value_references) @parameters (functor::(typeof(_functor)))(..)[1:buffer_length] = _functor + + # symbolic expression for calling the functor call_expr = functor( wrapper, __mtk_internal_u, __mtk_internal_x, __mtk_internal_p, t) + # differential and observed equations diffeqs = Equation[] for (i, var) in enumerate([D.(diffvars); outputs]) push!(diffeqs, var ~ call_expr[i]) end + # instance management callback which deallocates the instance when + # necessary and notifies the FMU of completed integrator steps finalize_affect = MTK.FunctionalAffect(fmiFinalize!, [], [wrapper], []) step_affect = MTK.FunctionalAffect(fmiMEStep!, [], [wrapper], []) instance_management_callback = MTK.SymbolicDiscreteCallback( @@ -153,6 +225,8 @@ function MTK.FMIComponent(::Val{Ver}; fmu = nothing, tolerance = 1e-6, FMI3CSFunctor(state_value_references, output_value_references) end @parameters (functor::(typeof(_functor)))(..)[1:(length(__mtk_internal_u) + length(__mtk_internal_o))] = _functor + # for co-simulation, we need to ensure the output buffer is solved for + # during initialization for (i, x) in enumerate(collect(__mtk_internal_o)) push!(initialization_eqs, x ~ functor( @@ -161,18 +235,22 @@ function MTK.FMIComponent(::Val{Ver}; fmu = nothing, tolerance = 1e-6, diffeqs = Equation[] + # use `ImperativeAffect` for instance management here cb_observed = (; inputs = __mtk_internal_x, params = copy(params), t, wrapper, dt = communication_step_size) cb_modified = (;) + # modify the outputs if present if symbolic_type(__mtk_internal_o) != NotSymbolic() cb_modified = (cb_modified..., outputs = __mtk_internal_o) end + # modify the continuous state if present if symbolic_type(__mtk_internal_u) != NotSymbolic() cb_modified = (cb_modified..., states = __mtk_internal_u) end initialize_affect = MTK.ImperativeAffect(fmiCSInitialize!; observed = cb_observed, modified = cb_modified, ctx = _functor) finalize_affect = MTK.FunctionalAffect(fmiFinalize!, [], [wrapper], []) + # the callback affect performs the stepping step_affect = MTK.ImperativeAffect( fmiCSStep!; observed = cb_observed, modified = cb_modified, ctx = _functor) instance_management_callback = MTK.SymbolicDiscreteCallback( @@ -180,6 +258,7 @@ function MTK.FMIComponent(::Val{Ver}; fmu = nothing, tolerance = 1e-6, finalize = finalize_affect, reinitializealg = SciMLBase.NoInit() ) + # guarded in case there are no outputs/states and the variable is `[]`. symbolic_type(__mtk_internal_o) == NotSymbolic() || push!(params, __mtk_internal_o) symbolic_type(__mtk_internal_u) == NotSymbolic() || push!(params, __mtk_internal_u) @@ -191,7 +270,24 @@ function MTK.FMIComponent(::Val{Ver}; fmu = nothing, tolerance = 1e-6, discrete_events = [instance_management_callback], name, initialization_eqs) end -function fmi_variables_to_mtk_variables!(fmu, varmap, value_references, truevars, allvars, +""" + $(TYPEDSIGNATURES) + +A utility function which accepts an FMU `fmu` and a mapping from value reference to a +list of associated names `varmap`. A symbolic variable is created for each name. The +associated value reference is kept track of in `value_references`. In case there are +multiple names for a value reference, the symbolic variable for the first name is pushed +to `truevars`. All of the created symbolic variables are pushed to `allvars`. Observed +equations equating identical variables are pushed to `obseqs`. `defs` is a dictionary of +defaults. + +# Keyword Arguments +- `parameters`: A boolean indicating whether to use `@parameters` for the symbolic + variables instead of `@variables`. +""" +function fmi_variables_to_mtk_variables!( + fmu::Union{FMI.FMU2, FMI.FMU3}, varmap::AbstractDict, + value_references::AbstractDict, truevars, allvars, obseqs, defs = Dict(); parameters = false) for (valRef, snames) in varmap stateT = FMI.dataTypeForValueReference(fmu, valRef) @@ -215,23 +311,69 @@ function fmi_variables_to_mtk_variables!(fmu, varmap, value_references, truevars end end +""" + $(TYPEDSIGNATURES) + +Parse the string name of an FMI variable into a `Symbol` name for the corresponding +MTK vriable. +""" function parseFMIVariableName(name::AbstractString) return Symbol(replace(name, "." => "__")) end +""" + $(TYPEDEF) + +A struct which manages instance creation and deallocation for v2 FMUs. + +# Fields + +$(TYPEDFIELDS) +""" mutable struct FMI2InstanceWrapper + """ + The FMU from `FMI.loadFMU`. + """ const fmu::FMI.FMU2 - const param_value_references::Vector{UInt32} - const input_value_references::Vector{UInt32} + """ + The parameter value references. These should be in the same order as the parameter + vector passed to functions involving this wrapper. + """ + const param_value_references::Vector{FMI.fmi2ValueReference} + """ + The input value references. These should be in the same order as the inputs passed + to functions involving this wrapper. + """ + const input_value_references::Vector{FMI.fmi2ValueReference} + """ + The tolerance with which to setup the FMU instance. + """ const tolerance::FMI.fmi2Real - instance::Union{FMI.FMU2Component, Nothing} + """ + The FMU instance, if present, and `nothing` otherwise. + """ + instance::Union{FMI.FMU2Component{FMI.FMU2}, Nothing} end +""" + $(TYPEDSIGNATURES) + +Create an `FMI2InstanceWrapper` with no instance. +""" function FMI2InstanceWrapper(fmu, params, inputs, tolerance) FMI2InstanceWrapper(fmu, params, inputs, tolerance, nothing) end -function get_instance_common!(wrapper::FMI2InstanceWrapper, states, inputs, params, t) +""" + $(TYPEDSIGNATURES) + +Common functionality for creating an instance of a v2 FMU. Does not check if +`wrapper.instance` is already present, and overwrites the existing value with +a new instance. `inputs` should be in the order of `wrapper.input_value_references`. +`params` should be in the order of `wrapper.param_value_references`. `t` is the current +time. Returns the created instance, which is also stored in `wrapper.instance`. +""" +function get_instance_common!(wrapper::FMI2InstanceWrapper, inputs, params, t) wrapper.instance = FMI.fmi2Instantiate!(wrapper.fmu)::FMI.FMU2Component if !isempty(params) @statuscheck FMI.fmi2SetReal(wrapper.instance, wrapper.param_value_references, @@ -248,9 +390,17 @@ function get_instance_common!(wrapper::FMI2InstanceWrapper, states, inputs, para return wrapper.instance end -function get_instance_ME!(wrapper::FMI2InstanceWrapper, states, inputs, params, t) +""" + $(TYPEDSIGNATURES) + +Create an instance of a Model Exchange FMU. Use the existing instance in `wrapper` if +present and create a new one otherwise. Return the instance. + +See `get_instance_common!` for a description of the arguments. +""" +function get_instance_ME!(wrapper::FMI2InstanceWrapper, inputs, params, t) if wrapper.instance === nothing - get_instance_common!(wrapper, states, inputs, params, t) + get_instance_common!(wrapper, inputs, params, t) @statuscheck FMI.fmi2ExitInitializationMode(wrapper.instance) eventInfo = FMI.fmi2NewDiscreteStates(wrapper.instance) @assert eventInfo.newDiscreteStatesNeeded == FMI.fmi2False @@ -261,19 +411,39 @@ function get_instance_ME!(wrapper::FMI2InstanceWrapper, states, inputs, params, return wrapper.instance end -function get_instance_CS!(wrapper::FMI2InstanceWrapper, states, inputs, params, t) +""" + $(TYPEDSIGNATURES) + +Create an instance of a CoSimulation FMU. Use the existing instance in `wrapper` if +present and create a new one otherwise. Return the instance. + +See `get_instance_common!` for a description of the arguments. +""" +function get_instance_CS!(wrapper::FMI2InstanceWrapper, inputs, params, t) if wrapper.instance === nothing - get_instance_common!(wrapper, states, inputs, params, t) + get_instance_common!(wrapper, inputs, params, t) @statuscheck FMI.fmi2ExitInitializationMode(wrapper.instance) end return wrapper.instance end +""" + $(TYPEDSIGNATURES) + +If `wrapper.instance !== nothing`, tell the FMU that an integrator step has been accepted. +This is relevant only for ModelExchange FMUs. +""" function complete_step!(wrapper::FMI2InstanceWrapper) wrapper.instance === nothing && return @statuscheck FMI.fmi2CompletedIntegratorStep(wrapper.instance, FMI.fmi2True) end +""" + $(TYPEDSIGNATURES) + +If `wrapper.instance !== nothing`, terminate and free the instance. Also set +`wrapper.instance` to `nothing`. +""" function reset_instance!(wrapper::FMI2InstanceWrapper) wrapper.instance === nothing && return FMI.fmi2Terminate(wrapper.instance) @@ -281,18 +451,55 @@ function reset_instance!(wrapper::FMI2InstanceWrapper) wrapper.instance = nothing end +""" + $(TYPEDEF) + +A struct which manages instance creation and deallocation for v3 FMUs. + +# Fields + +$(TYPEDFIELDS) +""" mutable struct FMI3InstanceWrapper + """ + The FMU from `FMI.loadFMU`. + """ const fmu::FMI.FMU3 - const param_value_references::Vector{UInt32} - const input_value_references::Vector{UInt32} + """ + The parameter value references. These should be in the same order as the parameter + vector passed to functions involving this wrapper. + """ + const param_value_references::Vector{FMI.fmi3ValueReference} + """ + The input value references. These should be in the same order as the inputs passed + to functions involving this wrapper. + """ + const input_value_references::Vector{FMI.fmi3ValueReference} + """ + The FMU instance, if present, and `nothing` otherwise. + """ instance::Union{FMI.FMU3Instance{FMI.FMU3}, Nothing} end +""" + $(TYPEDSIGNATURES) + +Create an `FMI3InstanceWrapper` with no instance. +""" function FMI3InstanceWrapper(fmu, params, inputs) FMI3InstanceWrapper(fmu, params, inputs, nothing) end -function get_instance_common!(wrapper::FMI3InstanceWrapper, states, inputs, params, t) +""" + $(TYPEDSIGNATURES) + +Common functionality for creating an instance of a v3 FMU. Since v3 FMUs need to be +instantiated differently depending on the type, this assumes `wrapper.instance` is a +freshly instantiated FMU which needs to be initialized. `inputs` should be in the order +of `wrapper.input_value_references`. `params` should be in the order of +`wrapper.param_value_references`. `t` is the current time. Returns `wrapper.instance`. +""" +function get_instance_common!(wrapper::FMI3InstanceWrapper, inputs, params, t) if !isempty(params) @statuscheck FMI.fmi3SetFloat64(wrapper.instance, wrapper.param_value_references, params) @@ -307,10 +514,18 @@ function get_instance_common!(wrapper::FMI3InstanceWrapper, states, inputs, para return wrapper.instance end -function get_instance_ME!(wrapper::FMI3InstanceWrapper, states, inputs, params, t) +""" + $(TYPEDSIGNATURES) + +Create an instance of a Model Exchange FMU. Use the existing instance in `wrapper` if +present and create a new one otherwise. Return the instance. + +See `get_instance_common!` for a description of the arguments. +""" +function get_instance_ME!(wrapper::FMI3InstanceWrapper, inputs, params, t) if wrapper.instance === nothing wrapper.instance = FMI.fmi3InstantiateModelExchange!(wrapper.fmu)::FMI.FMU3Instance - get_instance_common!(wrapper, states, inputs, params, t) + get_instance_common!(wrapper, inputs, params, t) @statuscheck FMI.fmi3ExitInitializationMode(wrapper.instance) eventInfo = FMI.fmi3UpdateDiscreteStates(wrapper.instance) @assert eventInfo[1] == FMI.fmi2False @@ -321,16 +536,31 @@ function get_instance_ME!(wrapper::FMI3InstanceWrapper, states, inputs, params, return wrapper.instance end -function get_instance_CS!(wrapper::FMI3InstanceWrapper, states, inputs, params, t) +""" + $(TYPEDSIGNATURES) + +Create an instance of a CoSimulation FMU. Use the existing instance in `wrapper` if +present and create a new one otherwise. Return the instance. + +See `get_instance_common!` for a description of the arguments. +""" +function get_instance_CS!(wrapper::FMI3InstanceWrapper, inputs, params, t) if wrapper.instance === nothing wrapper.instance = FMI.fmi3InstantiateCoSimulation!( wrapper.fmu; eventModeUsed = false)::FMI.FMU3Instance - get_instance_common!(wrapper, states, inputs, params, t) + get_instance_common!(wrapper, inputs, params, t) @statuscheck FMI.fmi3ExitInitializationMode(wrapper.instance) end return wrapper.instance end +""" + $(TYPEDSIGNATURES) + +If `wrapper.instance !== nothing`, tell the FMU that an integrator step has been accepted. +This is relevant only for ModelExchange FMUs. Asserts that the simulation does not need +to be terminated and does not require entering event mode. +""" function complete_step!(wrapper::FMI3InstanceWrapper) wrapper.instance === nothing && return enterEventMode = Ref(FMI.fmi3False) @@ -341,6 +571,9 @@ function complete_step!(wrapper::FMI3InstanceWrapper) @assert terminateSimulation[] == FMI.fmi3False end +""" + $(TYPEDSIGNATURES) +""" function reset_instance!(wrapper::FMI3InstanceWrapper) wrapper.instance === nothing && return FMI.fmi3Terminate(wrapper.instance) @@ -348,7 +581,22 @@ function reset_instance!(wrapper::FMI3InstanceWrapper) wrapper.instance = nothing end +""" + $(TYPEDEF) + +A callable struct useful for simulating v2 Model Exchange FMUs. When called, updates the +internal state of the FMU and gets updated values for continuous state derivatives and +output variables. + +# Fields + +$(TYPEDFIELDS) +""" struct FMI2MEFunctor + """ + The value references for outputs of the FMU, in the order that the caller expects + them to be returned when calling `FMI2MEFunctor`. + """ output_value_references::Vector{FMI.fmi2ValueReference} end @@ -360,6 +608,11 @@ end ndims = 1 end +""" + $(TYPEDSIGNATURES) + +Update `wrapper.instance` with the new values of state, input and independent variables. +""" function update_instance_ME!(wrapper::FMI2InstanceWrapper, states, inputs, t) instance = wrapper.instance @statuscheck FMI.fmi2SetTime(instance, t) @@ -370,10 +623,19 @@ function update_instance_ME!(wrapper::FMI2InstanceWrapper, states, inputs, t) end end +""" + $(TYPEDSIGNATURES) + +Get the FMU instance (creating and initializing it if not present), update it +with the current values of variables, and return a vector of the state derivatives +and output variables. +""" function (fn::FMI2MEFunctor)(wrapper::FMI2InstanceWrapper, states, inputs, params, t) - instance = get_instance_ME!(wrapper, states, inputs, params, t) + instance = get_instance_ME!(wrapper, inputs, params, t) update_instance_ME!(wrapper, states, inputs, t) + # TODO: Find a way to do this without allocating. We can't pass a view to these + # functions. states_buffer = zeros(length(states)) @statuscheck FMI.fmi2GetDerivatives!(instance, states_buffer) outputs_buffer = zeros(length(fn.output_value_references)) @@ -381,7 +643,22 @@ function (fn::FMI2MEFunctor)(wrapper::FMI2InstanceWrapper, states, inputs, param return [states_buffer; outputs_buffer] end +""" + $(TYPEDEF) + +A callable struct useful for simulating v3 Model Exchange FMUs. When called, updates the +internal state of the FMU and gets updated values for continuous state derivatives and +output variables. + +# Fields + +$(TYPEDFIELDS) +""" struct FMI3MEFunctor + """ + The value references for outputs of the FMU, in the order that the caller expects + them to be returned when calling `FMI3MEFunctor`. + """ output_value_references::Vector{FMI.fmi3ValueReference} end @@ -393,6 +670,11 @@ end ndims = 1 end +""" + $(TYPEDSIGNATURES) + +Update `wrapper.instance` with the new values of state, input and independent variables. +""" function update_instance_ME!(wrapper::FMI3InstanceWrapper, states, inputs, t) instance = wrapper.instance @statuscheck FMI.fmi3SetTime(instance, t) @@ -402,10 +684,18 @@ function update_instance_ME!(wrapper::FMI3InstanceWrapper, states, inputs, t) end end +""" + $(TYPEDSIGNATURES) + +Get the FMU instance (creating and initializing it if not present), update it +with the current values of variables, and return a vector of the state derivatives +and output variables. +""" function (fn::FMI3MEFunctor)(wrapper::FMI3InstanceWrapper, states, inputs, params, t) - instance = get_instance_ME!(wrapper, states, inputs, params, t) + instance = get_instance_ME!(wrapper, inputs, params, t) update_instance_ME!(wrapper, states, inputs, t) + # TODO: Don't allocate states_buffer = zeros(length(states)) @statuscheck FMI.fmi3GetContinuousStateDerivatives!(instance, states_buffer) outputs_buffer = zeros(length(fn.output_value_references)) @@ -413,28 +703,60 @@ function (fn::FMI3MEFunctor)(wrapper::FMI3InstanceWrapper, states, inputs, param return [states_buffer; outputs_buffer] end +""" + $(TYPEDSIGNATURES) + +An affect function for use inside a `FunctionalAffect`. This should be triggered every +time an integrator step is accepted. Expects `p` to be a 1-length array containing +the index of the instance wrapper (`FMI2InstanceWrapper` or `FMI3InstanceWrapper`) in +the parameter object. +""" function fmiMEStep!(integrator, u, p, ctx) wrapper_idx = p[1] wrapper = integrator.ps[wrapper_idx] complete_step!(wrapper) end +""" + $(TYPEDSIGNATURES) + +An affect function for use inside a `FunctionalAffect`. This should be triggered at the +end of the solve, regardless of whether it succeeded or failed. Expects `p` to be a +1-length array containing the index of the instance wrapper (`FMI2InstanceWrapper` or +`FMI3InstanceWrapper`) in the parameter object. +""" function fmiFinalize!(integrator, u, p, ctx) wrapper_idx = p[1] wrapper = integrator.ps[wrapper_idx] reset_instance!(wrapper) end +""" + $(TYPEDEF) + +A callable struct useful for initializing v2 CoSimulation FMUs. When called, updates the +internal state of the FMU and gets updated values for output variables. + +# Fields + +$(TYPEDFIELDS) +""" struct FMI2CSFunctor - state_value_references::Vector{UInt32} - output_value_references::Vector{UInt32} + """ + The value references of state variables in the FMU. + """ + state_value_references::Vector{FMI.fmi2ValueReference} + """ + The value references of output variables in the FMU. + """ + output_value_references::Vector{FMI.fmi2ValueReference} end function (fn::FMI2CSFunctor)(wrapper::FMI2InstanceWrapper, states, inputs, params, t) states = states isa SubArray ? copy(states) : states inputs = inputs isa SubArray ? copy(inputs) : inputs params = params isa SubArray ? copy(params) : params - instance = get_instance_CS!(wrapper, states, inputs, params, t) + instance = get_instance_CS!(wrapper, inputs, params, t) if isempty(fn.output_value_references) return eltype(states)[] else @@ -450,6 +772,17 @@ end ndims = 1 end +""" + $(TYPEDSIGNATURES) + +An affect function designed for use with `ImperativeAffect`. Should be triggered during +callback initialization. `m` should contain the key `:states` with the value being the +state vector if the FMU has continuous states. `m` should contain the key `:outputs` with +the value being the output vector if the FMU has output variables. `o` should contain the +`:inputs`, `:params`, `:t` and `:wrapper` where the latter contains the `FMI2InstanceWrapper`. + +Initializes the FMU. Only for use with CoSimulation FMUs. +""" function fmiCSInitialize!(m, o, ctx::FMI2CSFunctor, integrator) states = isdefined(m, :states) ? m.states : () inputs = o.inputs @@ -460,8 +793,7 @@ function fmiCSInitialize!(m, o, ctx::FMI2CSFunctor, integrator) reset_instance!(wrapper) end - instance = get_instance_common!(wrapper, states, inputs, params, t) - @statuscheck FMI.fmi2ExitInitializationMode(instance) + instance = get_instance_CS!(wrapper, inputs, params, t) if isdefined(m, :states) @statuscheck FMI.fmi2GetReal!(instance, ctx.state_value_references, m.states) end @@ -472,6 +804,14 @@ function fmiCSInitialize!(m, o, ctx::FMI2CSFunctor, integrator) return m end +""" + $(TYPEDSIGNATURES) + +An affect function designed for use with `ImperativeAffect`. Should be triggered +periodically to communicte with the CoSimulation FMU. Has the same requirements as +`fmiCSInitialize!` for `m` and `o`, with the addition that `o` should have a key +`:dt` with the value being the communication step size. +""" function fmiCSStep!(m, o, ctx::FMI2CSFunctor, integrator) wrapper = o.wrapper states = isdefined(m, :states) ? m.states : () @@ -480,7 +820,7 @@ function fmiCSStep!(m, o, ctx::FMI2CSFunctor, integrator) t = o.t dt = o.dt - instance = get_instance_CS!(wrapper, states, inputs, params, integrator.t) + instance = get_instance_CS!(wrapper, inputs, params, integrator.t) @statuscheck FMI.fmi2DoStep(instance, integrator.t - dt, dt, FMI.fmi2True) if isdefined(m, :states) @@ -493,16 +833,33 @@ function fmiCSStep!(m, o, ctx::FMI2CSFunctor, integrator) return m end +""" + $(TYPEDEF) + +A callable struct useful for initializing v3 CoSimulation FMUs. When called, updates the +internal state of the FMU and gets updated values for output variables. + +# Fields + +$(TYPEDFIELDS) +""" struct FMI3CSFunctor - state_value_references::Vector{UInt32} - output_value_references::Vector{UInt32} + """ + The value references of state variables in the FMU. + """ + state_value_references::Vector{FMI.fmi3ValueReference} + """ + The value references of output variables in the FMU. + """ + output_value_references::Vector{FMI.fmi3ValueReference} end function (fn::FMI3CSFunctor)(wrapper::FMI3InstanceWrapper, states, inputs, params, t) states = states isa SubArray ? copy(states) : states inputs = inputs isa SubArray ? copy(inputs) : inputs params = params isa SubArray ? copy(params) : params - instance = get_instance_CS!(wrapper, states, inputs, params, t) + instance = get_instance_CS!(wrapper, inputs, params, t) + if isempty(fn.output_value_references) return eltype(states)[] else @@ -518,6 +875,9 @@ end ndims = 1 end +""" + $(TYPEDSIGNATURES) +""" function fmiCSInitialize!(m, o, ctx::FMI3CSFunctor, integrator) states = isdefined(m, :states) ? m.states : () inputs = o.inputs @@ -527,7 +887,7 @@ function fmiCSInitialize!(m, o, ctx::FMI3CSFunctor, integrator) if wrapper.instance !== nothing reset_instance!(wrapper) end - instance = get_instance_CS!(wrapper, states, inputs, params, t) + instance = get_instance_CS!(wrapper, inputs, params, t) if isdefined(m, :states) @statuscheck FMI.fmi3GetFloat64!(instance, ctx.state_value_references, m.states) end @@ -538,6 +898,9 @@ function fmiCSInitialize!(m, o, ctx::FMI3CSFunctor, integrator) return m end +""" + $(TYPEDSIGNATURES) +""" function fmiCSStep!(m, o, ctx::FMI3CSFunctor, integrator) wrapper = o.wrapper states = isdefined(m, :states) ? m.states : () @@ -546,7 +909,7 @@ function fmiCSStep!(m, o, ctx::FMI3CSFunctor, integrator) t = o.t dt = o.dt - instance = get_instance_CS!(wrapper, states, inputs, params, integrator.t) + instance = get_instance_CS!(wrapper, inputs, params, integrator.t) eventEncountered = Ref(FMI.fmi3False) terminateSimulation = Ref(FMI.fmi3False) earlyReturn = Ref(FMI.fmi3False) From 9614c0333c8b85a44dcdbbcaf3c244c6d8fbe1fc Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 7 Jan 2025 13:00:58 +0530 Subject: [PATCH 0652/2176] build: bump SymbolicIndexingInterface compat --- Project.toml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Project.toml b/Project.toml index b25d2a0562..13684a6a52 100644 --- a/Project.toml +++ b/Project.toml @@ -145,9 +145,9 @@ SpecialFunctions = "0.7, 0.8, 0.9, 0.10, 1.0, 2" StaticArrays = "0.10, 0.11, 0.12, 1.0" StochasticDiffEq = "6.72.1" StochasticDelayDiffEq = "1.8.1" -SymbolicIndexingInterface = "0.3.36" +SymbolicIndexingInterface = "0.3.37" SymbolicUtils = "3.10" -Symbolics = "6.22.1" +Symbolics = "6.23" URIs = "1" UnPack = "0.1, 1.0" Unitful = "1.1" From 1359630a1681ae8ee5045357a7d8a19e9289f654 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 7 Jan 2025 13:10:20 +0530 Subject: [PATCH 0653/2176] feat: mark inputs and outputs of FMU with appropriate metadata --- ext/MTKFMIExt.jl | 16 +++++++++++----- test/extensions/fmi.jl | 25 +++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 5 deletions(-) diff --git a/ext/MTKFMIExt.jl b/ext/MTKFMIExt.jl index 4fea249dd9..95e1c0ef26 100644 --- a/ext/MTKFMIExt.jl +++ b/ext/MTKFMIExt.jl @@ -132,7 +132,8 @@ function MTK.FMIComponent(::Val{Ver}; fmu = nothing, tolerance = 1e-6, # parse the inputs to the FMU inputs = [] fmi_variables_to_mtk_variables!(fmu, FMI.getInputValueReferencesAndNames(fmu), - value_references, inputs, states, observed) + value_references, inputs, states, observed; postprocess_variable = v -> MTK.setinput( + v, true)) # create a symbolic variable for the input buffer if isempty(inputs) __mtk_internal_x = [] @@ -145,7 +146,8 @@ function MTK.FMIComponent(::Val{Ver}; fmu = nothing, tolerance = 1e-6, # parse the outputs of the FMU outputs = [] fmi_variables_to_mtk_variables!(fmu, FMI.getOutputValueReferencesAndNames(fmu), - value_references, outputs, states, observed) + value_references, outputs, states, observed; postprocess_variable = v -> MTK.setoutput( + v, true)) # create the output buffer. This is only required for CoSimulation to pass it to # the callback affect if type == :CS @@ -284,18 +286,22 @@ defaults. # Keyword Arguments - `parameters`: A boolean indicating whether to use `@parameters` for the symbolic variables instead of `@variables`. +- `postprocess_variable`: A function applied to each created variable that should + return the updated variable. This is useful to add metadata to variables. """ function fmi_variables_to_mtk_variables!( fmu::Union{FMI.FMU2, FMI.FMU3}, varmap::AbstractDict, value_references::AbstractDict, truevars, allvars, - obseqs, defs = Dict(); parameters = false) + obseqs, defs = Dict(); parameters = false, postprocess_variable = identity) for (valRef, snames) in varmap stateT = FMI.dataTypeForValueReference(fmu, valRef) snames = map(parseFMIVariableName, snames) if parameters - vars = [MTK.unwrap(only(@parameters $sname::stateT)) for sname in snames] + vars = [postprocess_variable(MTK.unwrap(only(@parameters $sname::stateT))) + for sname in snames] else - vars = [MTK.unwrap(only(@variables $sname(t)::stateT)) for sname in snames] + vars = [postprocess_variable(MTK.unwrap(only(@variables $sname(t)::stateT))) + for sname in snames] end for i in eachindex(vars) if i == 1 diff --git a/test/extensions/fmi.jl b/test/extensions/fmi.jl index 868d60a9bb..4dc0fc9a67 100644 --- a/test/extensions/fmi.jl +++ b/test/extensions/fmi.jl @@ -9,9 +9,16 @@ const FMU_DIR = joinpath(@__DIR__, "fmus") truesol = FMI.simulate( fmu, (0.0, 8.0); saveat = 0.0:0.1:8.0, recordValues = ["mass.s", "mass.v"]) + function test_no_inputs_outputs(sys) + for var in unknowns(sys) + @test !MTK.isinput(var) + @test !MTK.isoutput(var) + end + end @testset "v2, ME" begin fmu = loadFMU("SpringPendulum1D", "Dymola", "2022x"; type = :ME) @mtkbuild sys = MTK.FMIComponent(Val(2); fmu, type = :ME) + test_no_inputs_outputs(sys) prob = ODEProblem{true, SciMLBase.FullSpecialize}( sys, [sys.mass__s => 0.5, sys.mass__v => 0.0], (0.0, 8.0)) sol = solve(prob, Tsit5(); reltol = 1e-8, abstol = 1e-8) @@ -28,6 +35,7 @@ const FMU_DIR = joinpath(@__DIR__, "fmus") Val(2); fmu, communication_step_size = 0.001, type = :CS) @variables x(t) = 1.0 @mtkbuild sys = ODESystem([D(x) ~ x], t; systems = [inner]) + test_no_inputs_outputs(sys) prob = ODEProblem{true, SciMLBase.FullSpecialize}( sys, [sys.inner.mass__s => 0.5, sys.inner.mass__v => 0.0], (0.0, 8.0)) @@ -44,6 +52,7 @@ const FMU_DIR = joinpath(@__DIR__, "fmus") @testset "v3, ME" begin fmu = loadFMU("SpringPendulum1D", "Dymola", "2023x", "3.0"; type = :ME) @mtkbuild sys = MTK.FMIComponent(Val(3); fmu, type = :ME) + test_no_inputs_outputs(sys) prob = ODEProblem{true, SciMLBase.FullSpecialize}( sys, [sys.mass__s => 0.5, sys.mass__v => 0.0], (0.0, 8.0)) sol = solve(prob, Tsit5(); reltol = 1e-8, abstol = 1e-8) @@ -60,6 +69,7 @@ const FMU_DIR = joinpath(@__DIR__, "fmus") Val(3); fmu, communication_step_size = 0.001, type = :CS) @variables x(t) = 1.0 @mtkbuild sys = ODESystem([D(x) ~ x], t; systems = [inner]) + test_no_inputs_outputs(sys) prob = ODEProblem{true, SciMLBase.FullSpecialize}( sys, [sys.inner.mass__s => 0.5, sys.inner.mass__v => 0.0], (0.0, 8.0)) @@ -75,6 +85,11 @@ end @testset "v2, ME" begin fmu = loadFMU(joinpath(FMU_DIR, "SimpleAdder.fmu"); type = :ME) @named adder = MTK.FMIComponent(Val(2); fmu, type = :ME) + @test MTK.isinput(adder.a) + @test MTK.isinput(adder.b) + @test MTK.isoutput(adder.out) + @test !MTK.isinput(adder.c) && !MTK.isoutput(adder.c) + @variables a(t) b(t) c(t) [guess = 1.0] @mtkbuild sys = ODESystem( [adder.a ~ a, adder.b ~ b, D(a) ~ t, @@ -92,6 +107,10 @@ end fmu = loadFMU(joinpath(FMU_DIR, "SimpleAdder.fmu"); type = :CS) @named adder = MTK.FMIComponent( Val(2); fmu, type = :CS, communication_step_size = 0.001) + @test MTK.isinput(adder.a) + @test MTK.isinput(adder.b) + @test MTK.isoutput(adder.out) + @test !MTK.isinput(adder.c) && !MTK.isoutput(adder.c) @variables a(t) b(t) c(t) [guess = 1.0] @mtkbuild sys = ODESystem( [adder.a ~ a, adder.b ~ b, D(a) ~ t, @@ -110,6 +129,9 @@ end @testset "v3, ME" begin fmu = loadFMU(joinpath(FMU_DIR, "StateSpace.fmu"); type = :ME) @named sspace = MTK.FMIComponent(Val(3); fmu, type = :ME) + @test MTK.isinput(sspace.u) + @test MTK.isoutput(sspace.y) + @test !MTK.isinput(sspace.x) && !MTK.isoutput(sspace.x) @variables u(t)=1.0 x(t)=1.0 y(t) [guess = 1.0] @mtkbuild sys = ODESystem( [sspace.u ~ u, D(u) ~ t, D(x) ~ sspace.x + sspace.y, y^2 ~ sspace.y + x], t; @@ -125,6 +147,9 @@ end fmu = loadFMU(joinpath(FMU_DIR, "StateSpace.fmu"); type = :CS) @named sspace = MTK.FMIComponent( Val(3); fmu, communication_step_size = 1e-3, type = :CS) + @test MTK.isinput(sspace.u) + @test MTK.isoutput(sspace.y) + @test !MTK.isinput(sspace.x) && !MTK.isoutput(sspace.x) @variables u(t)=1.0 x(t)=1.0 y(t) [guess = 1.0] @mtkbuild sys = ODESystem( [sspace.u ~ u, D(u) ~ t, D(x) ~ sspace.x + sspace.y, y^2 ~ sspace.y + x], t; From 66f2d75516e36484b61ef4d7f7ade1c65f9b9afa Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 8 Jan 2025 16:24:33 +0530 Subject: [PATCH 0654/2176] fix: fix array hack when looking through unknowns --- src/structural_transformation/symbolics_tearing.jl | 1 - 1 file changed, 1 deletion(-) diff --git a/src/structural_transformation/symbolics_tearing.jl b/src/structural_transformation/symbolics_tearing.jl index 173f430c04..f1a152b8e7 100644 --- a/src/structural_transformation/symbolics_tearing.jl +++ b/src/structural_transformation/symbolics_tearing.jl @@ -719,7 +719,6 @@ function cse_and_array_hacks(sys, obs, subeqs, unknowns, neweqs; cse = true, arr Symbolics.shape(sym) != Symbolics.Unknown() || continue arg1 = arguments(sym)[1] cnt = get(arr_obs_occurrences, arg1, 0) - cnt == 0 && continue arr_obs_occurrences[arg1] = cnt + 1 end for eq in neweqs From 7f1a7a167c582ea5d51efe01a32d4bfaea65aef6 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 8 Jan 2025 16:25:22 +0530 Subject: [PATCH 0655/2176] fix: fix several bugs causing incorrect results in FMU components --- ext/MTKFMIExt.jl | 364 +++++++++++++++++++++++------------------------ 1 file changed, 177 insertions(+), 187 deletions(-) diff --git a/ext/MTKFMIExt.jl b/ext/MTKFMIExt.jl index 95e1c0ef26..603ec8906c 100644 --- a/ext/MTKFMIExt.jl +++ b/ext/MTKFMIExt.jl @@ -84,12 +84,16 @@ with the name `namespace__variable`. supported by FMI.jl. - `communication_step_size`: The periodic interval at which communication with CoSimulation FMUs will occur. Must be provided for CoSimulation FMU components. +- `reinitializealg`: The DAE initialization algorithm to use for the callback managing the + FMU. For CoSimulation FMUs whose states/outputs are used in algebraic equations of the + system, this needs to be an algorithm that will solve for the new algebraic variables. + For example, `OrdinaryDiffEqCore.BrownFullBasicInit()`. - `type`: Either `:ME` or `:CS` depending on whether `fmu` is a Model Exchange or CoSimulation FMU respectively. - `name`: The name of the system. """ function MTK.FMIComponent(::Val{Ver}; fmu = nothing, tolerance = 1e-6, - communication_step_size = nothing, type, name) where {Ver} + communication_step_size = nothing, reinitializealg = SciMLBase.NoInit(), type, name) where {Ver} if Ver != 2 && Ver != 3 throw(ArgumentError("FMI Version must be `2` or `3`")) end @@ -106,8 +110,14 @@ function MTK.FMIComponent(::Val{Ver}; fmu = nothing, tolerance = 1e-6, # this is a subset of `states` in the case where the FMU has multiple names for # the same value reference. diffvars = [] + # variables that are derivatives of diffvars + dervars = [] # observed equations observed = Equation[] + # need to separate observed equations for duplicate derivative variables + # since they aren't included in CS FMUs + der_observed = Equation[] + # parse states fmi_variables_to_mtk_variables!(fmu, FMI.getStateValueReferencesAndNames(fmu), value_references, diffvars, states, observed) @@ -115,12 +125,14 @@ function MTK.FMIComponent(::Val{Ver}; fmu = nothing, tolerance = 1e-6, # functions as the state vector if isempty(diffvars) # no differential variables - __mtk_internal_u = [] + __mtk_internal_u = Float64[] elseif type == :ME - # ME FMUs perform integration using the Julia solver, so unknowns of the FMU - # are unknowns of the `ODESystem` - @variables __mtk_internal_u(t)[1:length(diffvars)] [guess = diffvars] - push!(observed, __mtk_internal_u ~ copy(diffvars)) + # to avoid running into `structural_simplify` warnings about array variables + # and some unfortunate circular dependency issues, ME FMUs use an array of + # symbolics instead. This is also not worse off in performance + # because the former approach would allocate anyway. + # TODO: Can we avoid an allocation here using static arrays? + __mtk_internal_u = copy(diffvars) elseif type == :CS # CS FMUs do their own independent integration in a periodic callback, so their # unknowns are discrete variables in the `ODESystem`. A default of `missing` allows @@ -129,18 +141,30 @@ function MTK.FMIComponent(::Val{Ver}; fmu = nothing, tolerance = 1e-6, push!(observed, __mtk_internal_u ~ copy(diffvars)) end + # parse derivatives of states + # the variables passed to `postprocess_variable` haven't been differentiated yet, so they + # should match one variable in states. That's the one this is the derivative of, and we + # keep track of this ordering + derivative_order = [] + function derivative_order_postprocess(var) + idx = findfirst(isequal(var), states) + idx === nothing || push!(derivative_order, states[idx]) + return var + end + fmi_variables_to_mtk_variables!( + fmu, FMI.getDerivateValueReferencesAndNames(fmu), value_references, dervars, + states, der_observed; postprocess_variable = derivative_order_postprocess) + @assert length(derivative_order) == length(dervars) + # parse the inputs to the FMU inputs = [] fmi_variables_to_mtk_variables!(fmu, FMI.getInputValueReferencesAndNames(fmu), value_references, inputs, states, observed; postprocess_variable = v -> MTK.setinput( v, true)) # create a symbolic variable for the input buffer - if isempty(inputs) - __mtk_internal_x = [] - else - @variables __mtk_internal_x(t)[1:length(inputs)] [guess = inputs] - push!(observed, __mtk_internal_x ~ copy(inputs)) - push!(states, __mtk_internal_x) + __mtk_internal_x = copy(inputs) + if isempty(__mtk_internal_x) + __mtk_internal_x = Float64[] end # parse the outputs of the FMU @@ -152,7 +176,7 @@ function MTK.FMIComponent(::Val{Ver}; fmu = nothing, tolerance = 1e-6, # the callback affect if type == :CS if isempty(outputs) - __mtk_internal_o = [] + __mtk_internal_o = Float64[] else @parameters __mtk_internal_o(t)[1:length(outputs)]=missing [guess = zeros(length(outputs))] push!(observed, __mtk_internal_o ~ outputs) @@ -167,60 +191,58 @@ function MTK.FMIComponent(::Val{Ver}; fmu = nothing, tolerance = 1e-6, fmu, FMI.getParameterValueReferencesAndNames(fmu), value_references, params, [], parameter_dependencies, defs; parameters = true) # create a symbolic variable for the parameter buffer - if isempty(params) - __mtk_internal_p = [] - else - @parameters __mtk_internal_p[1:length(params)] - push!(parameter_dependencies, __mtk_internal_p ~ copy(params)) + __mtk_internal_p = copy(params) + if isempty(__mtk_internal_p) + __mtk_internal_p = Float64[] end + derivative_value_references = UInt32[value_references[var] for var in dervars] + state_value_references = UInt32[value_references[var] for var in diffvars] + output_value_references = UInt32[value_references[var] for var in outputs] input_value_references = UInt32[value_references[var] for var in inputs] param_value_references = UInt32[value_references[var] for var in params] # create a parameter for the instance wrapper # this manages the creation and deallocation of FMU instances + buffer_length = length(diffvars) + length(outputs) if Ver == 2 - @parameters wrapper::FMI2InstanceWrapper = FMI2InstanceWrapper( - fmu, param_value_references, input_value_references, tolerance) + @parameters (wrapper::FMI2InstanceWrapper)(..)[1:buffer_length] = FMI2InstanceWrapper( + fmu, derivative_value_references, state_value_references, output_value_references, + param_value_references, input_value_references, tolerance) else - @parameters wrapper::FMI3InstanceWrapper = FMI3InstanceWrapper( - fmu, param_value_references, input_value_references) + @parameters (wrapper::FMI3InstanceWrapper)(..)[1:buffer_length] = FMI3InstanceWrapper( + fmu, derivative_value_references, state_value_references, + output_value_references, param_value_references, input_value_references) end - output_value_references = UInt32[value_references[var] for var in outputs] - buffer_length = length(diffvars) + length(outputs) - # any additional initialization equations for the system initialization_eqs = Equation[] if type == :ME - # the functor is a callable struct which returns the state derivative and + # the wrapper is a callable struct which returns the state derivative and # output values - FunctorT = Ver == 2 ? FMI2MEFunctor : FMI3MEFunctor - _functor = FunctorT(output_value_references) - @parameters (functor::(typeof(_functor)))(..)[1:buffer_length] = _functor - - # symbolic expression for calling the functor - call_expr = functor( - wrapper, __mtk_internal_u, __mtk_internal_x, __mtk_internal_p, t) + # symbolic expression for calling the wrapper + call_expr = wrapper(__mtk_internal_u, __mtk_internal_x, __mtk_internal_p, t) # differential and observed equations diffeqs = Equation[] - for (i, var) in enumerate([D.(diffvars); outputs]) + for (i, var) in enumerate([dervars; outputs]) push!(diffeqs, var ~ call_expr[i]) end + for (var, dervar) in zip(derivative_order, dervars) + push!(diffeqs, D(var) ~ dervar) + end # instance management callback which deallocates the instance when # necessary and notifies the FMU of completed integrator steps finalize_affect = MTK.FunctionalAffect(fmiFinalize!, [], [wrapper], []) - step_affect = MTK.FunctionalAffect(fmiMEStep!, [], [wrapper], []) + step_affect = MTK.FunctionalAffect(Returns(nothing), [], [], []) instance_management_callback = MTK.SymbolicDiscreteCallback( - (t != t - 1), step_affect; finalize = finalize_affect, reinitializealg = SciMLBase.NoInit()) + (t != t - 1), step_affect; finalize = finalize_affect, reinitializealg = reinitializealg) - push!(params, wrapper, functor) - push!(states, __mtk_internal_u) + push!(params, wrapper) + append!(observed, der_observed) elseif type == :CS - state_value_references = UInt32[value_references[var] for var in diffvars] _functor = if Ver == 2 FMI2CSFunctor(state_value_references, output_value_references) else @@ -257,7 +279,7 @@ function MTK.FMIComponent(::Val{Ver}; fmu = nothing, tolerance = 1e-6, fmiCSStep!; observed = cb_observed, modified = cb_modified, ctx = _functor) instance_management_callback = MTK.SymbolicDiscreteCallback( communication_step_size, step_affect; initialize = initialize_affect, - finalize = finalize_affect, reinitializealg = SciMLBase.NoInit() + finalize = finalize_affect, reinitializealg = reinitializealg ) # guarded in case there are no outputs/states and the variable is `[]`. @@ -293,9 +315,15 @@ function fmi_variables_to_mtk_variables!( fmu::Union{FMI.FMU2, FMI.FMU3}, varmap::AbstractDict, value_references::AbstractDict, truevars, allvars, obseqs, defs = Dict(); parameters = false, postprocess_variable = identity) - for (valRef, snames) in varmap + for (valRef, varnames) in varmap stateT = FMI.dataTypeForValueReference(fmu, valRef) - snames = map(parseFMIVariableName, snames) + snames = Symbol[] + ders = Int[] + for name in varnames + sname, der = parseFMIVariableName(name) + push!(snames, sname) + push!(ders, der) + end if parameters vars = [postprocess_variable(MTK.unwrap(only(@parameters $sname::stateT))) for sname in snames] @@ -303,6 +331,14 @@ function fmi_variables_to_mtk_variables!( vars = [postprocess_variable(MTK.unwrap(only(@variables $sname(t)::stateT))) for sname in snames] end + for i in eachindex(vars) + der = ders[i] + vars[i] = MTK.unwrap(vars[i]) + for j in 1:der + vars[i] = D(vars[i]) + end + vars[i] = MTK.default_toterm(vars[i]) + end for i in eachindex(vars) if i == 1 push!(truevars, vars[i]) @@ -321,10 +357,22 @@ end $(TYPEDSIGNATURES) Parse the string name of an FMI variable into a `Symbol` name for the corresponding -MTK vriable. +MTK variable. Return the `Symbol` name and the number of times it is differentiated. """ function parseFMIVariableName(name::AbstractString) - return Symbol(replace(name, "." => "__")) + name = replace(name, "." => "__") + der = 0 + if startswith(name, "der(") + idx = findfirst(',', name) + if idx === nothing + name = @view name[5:(end - 1)] + der = 1 + else + der = parse(Int, @view name[(idx + 1):(end - 1)]) + name = @view name[5:(idx - 1)] + end + end + return Symbol(name), der end """ @@ -342,6 +390,20 @@ mutable struct FMI2InstanceWrapper """ const fmu::FMI.FMU2 """ + The value references for derivatives of states of the FMU, in the order that the + caller expects them to be returned when calling this struct. + """ + const derivative_value_references::Vector{FMI.fmi2ValueReference} + """ + The value references for the states of the FMU. + """ + const state_value_references::Vector{FMI.fmi2ValueReference} + """ + The value references for outputs of the FMU, in the order that the caller expects + them to be returned when calling this struct. + """ + const output_value_references::Vector{FMI.fmi2ValueReference} + """ The parameter value references. These should be in the same order as the parameter vector passed to functions involving this wrapper. """ @@ -366,10 +428,12 @@ end Create an `FMI2InstanceWrapper` with no instance. """ -function FMI2InstanceWrapper(fmu, params, inputs, tolerance) - FMI2InstanceWrapper(fmu, params, inputs, tolerance, nothing) +function FMI2InstanceWrapper(fmu, ders, states, outputs, params, inputs, tolerance) + FMI2InstanceWrapper(fmu, ders, states, outputs, params, inputs, tolerance, nothing) end +Base.nameof(::FMI2InstanceWrapper) = :FMI2InstanceWrapper + """ $(TYPEDSIGNATURES) @@ -381,6 +445,10 @@ time. Returns the created instance, which is also stored in `wrapper.instance`. """ function get_instance_common!(wrapper::FMI2InstanceWrapper, inputs, params, t) wrapper.instance = FMI.fmi2Instantiate!(wrapper.fmu)::FMI.FMU2Component + if !isempty(inputs) + @statuscheck FMI.fmi2SetReal(wrapper.instance, wrapper.input_value_references, + Csize_t(length(wrapper.param_value_references)), inputs) + end if !isempty(params) @statuscheck FMI.fmi2SetReal(wrapper.instance, wrapper.param_value_references, Csize_t(length(wrapper.param_value_references)), params) @@ -388,11 +456,6 @@ function get_instance_common!(wrapper::FMI2InstanceWrapper, inputs, params, t) @statuscheck FMI.fmi2SetupExperiment( wrapper.instance, FMI.fmi2True, wrapper.tolerance, t, FMI.fmi2False, t) @statuscheck FMI.fmi2EnterInitializationMode(wrapper.instance) - if !isempty(inputs) - @statuscheck FMI.fmi2SetReal(wrapper.instance, wrapper.input_value_references, - Csize_t(length(wrapper.param_value_references)), inputs) - end - return wrapper.instance end @@ -425,9 +488,13 @@ present and create a new one otherwise. Return the instance. See `get_instance_common!` for a description of the arguments. """ -function get_instance_CS!(wrapper::FMI2InstanceWrapper, inputs, params, t) +function get_instance_CS!(wrapper::FMI2InstanceWrapper, states, inputs, params, t) if wrapper.instance === nothing get_instance_common!(wrapper, inputs, params, t) + if !isempty(states) + @statuscheck FMI.fmi2SetReal(wrapper.instance, wrapper.state_value_references, + Csize_t(length(wrapper.state_value_references)), states) + end @statuscheck FMI.fmi2ExitInitializationMode(wrapper.instance) end return wrapper.instance @@ -436,12 +503,10 @@ end """ $(TYPEDSIGNATURES) -If `wrapper.instance !== nothing`, tell the FMU that an integrator step has been accepted. -This is relevant only for ModelExchange FMUs. +Call `fmiXCompletedIntegratorStep` with `noSetFMUStatePriorToCurrentPoint` as false. """ -function complete_step!(wrapper::FMI2InstanceWrapper) - wrapper.instance === nothing && return - @statuscheck FMI.fmi2CompletedIntegratorStep(wrapper.instance, FMI.fmi2True) +function partiallyCompleteIntegratorStep(wrapper::FMI2InstanceWrapper) + @statuscheck FMI.fmi2CompletedIntegratorStep(wrapper.instance, FMI.fmi2False) end """ @@ -472,6 +537,17 @@ mutable struct FMI3InstanceWrapper """ const fmu::FMI.FMU3 """ + The value references for derivatives of states of the FMU, in the order that the + caller expects them to be returned when calling this struct. + """ + const derivative_value_references::Vector{FMI.fmi3ValueReference} + const state_value_references::Vector{FMI.fmi3ValueReference} + """ + The value references for outputs of the FMU, in the order that the caller expects + them to be returned when calling this struct. + """ + const output_value_references::Vector{FMI.fmi3ValueReference} + """ The parameter value references. These should be in the same order as the parameter vector passed to functions involving this wrapper. """ @@ -492,10 +568,12 @@ end Create an `FMI3InstanceWrapper` with no instance. """ -function FMI3InstanceWrapper(fmu, params, inputs) - FMI3InstanceWrapper(fmu, params, inputs, nothing) +function FMI3InstanceWrapper(fmu, ders, states, outputs, params, inputs) + FMI3InstanceWrapper(fmu, ders, states, outputs, params, inputs, nothing) end +Base.nameof(::FMI3InstanceWrapper) = :FMI3InstanceWrapper + """ $(TYPEDSIGNATURES) @@ -550,11 +628,15 @@ present and create a new one otherwise. Return the instance. See `get_instance_common!` for a description of the arguments. """ -function get_instance_CS!(wrapper::FMI3InstanceWrapper, inputs, params, t) +function get_instance_CS!(wrapper::FMI3InstanceWrapper, states, inputs, params, t) if wrapper.instance === nothing wrapper.instance = FMI.fmi3InstantiateCoSimulation!( wrapper.fmu; eventModeUsed = false)::FMI.FMU3Instance get_instance_common!(wrapper, inputs, params, t) + if !isempty(states) + @statuscheck FMI.fmi3SetFloat64( + wrapper.instance, wrapper.state_value_references, states) + end @statuscheck FMI.fmi3ExitInitializationMode(wrapper.instance) end return wrapper.instance @@ -562,17 +644,12 @@ end """ $(TYPEDSIGNATURES) - -If `wrapper.instance !== nothing`, tell the FMU that an integrator step has been accepted. -This is relevant only for ModelExchange FMUs. Asserts that the simulation does not need -to be terminated and does not require entering event mode. """ -function complete_step!(wrapper::FMI3InstanceWrapper) - wrapper.instance === nothing && return +function partiallyCompleteIntegratorStep(wrapper::FMI3InstanceWrapper) enterEventMode = Ref(FMI.fmi3False) terminateSimulation = Ref(FMI.fmi3False) @statuscheck FMI.fmi3CompletedIntegratorStep!( - wrapper.instance, FMI.fmi3True, enterEventMode, terminateSimulation) + wrapper.instance, FMI.fmi3False, enterEventMode, terminateSimulation) @assert enterEventMode[] == FMI.fmi3False @assert terminateSimulation[] == FMI.fmi3False end @@ -587,88 +664,14 @@ function reset_instance!(wrapper::FMI3InstanceWrapper) wrapper.instance = nothing end -""" - $(TYPEDEF) - -A callable struct useful for simulating v2 Model Exchange FMUs. When called, updates the -internal state of the FMU and gets updated values for continuous state derivatives and -output variables. - -# Fields - -$(TYPEDFIELDS) -""" -struct FMI2MEFunctor - """ - The value references for outputs of the FMU, in the order that the caller expects - them to be returned when calling `FMI2MEFunctor`. - """ - output_value_references::Vector{FMI.fmi2ValueReference} -end - -@register_array_symbolic (fn::FMI2MEFunctor)( - wrapper::FMI2InstanceWrapper, states::Vector{<:Real}, - inputs::Vector{<:Real}, params::Vector{<:Real}, t::Real) begin +@register_array_symbolic (fn::FMI2InstanceWrapper)( + states::Vector{<:Real}, inputs::Vector{<:Real}, params::Vector{<:Real}, t::Real) begin size = (length(states) + length(fn.output_value_references),) eltype = eltype(states) ndims = 1 end -""" - $(TYPEDSIGNATURES) - -Update `wrapper.instance` with the new values of state, input and independent variables. -""" -function update_instance_ME!(wrapper::FMI2InstanceWrapper, states, inputs, t) - instance = wrapper.instance - @statuscheck FMI.fmi2SetTime(instance, t) - @statuscheck FMI.fmi2SetContinuousStates(instance, states) - if !isempty(inputs) - @statuscheck FMI.fmi2SetReal(instance, wrapper.input_value_references, - Csize_t(length(wrapper.param_value_references)), inputs) - end -end - -""" - $(TYPEDSIGNATURES) - -Get the FMU instance (creating and initializing it if not present), update it -with the current values of variables, and return a vector of the state derivatives -and output variables. -""" -function (fn::FMI2MEFunctor)(wrapper::FMI2InstanceWrapper, states, inputs, params, t) - instance = get_instance_ME!(wrapper, inputs, params, t) - update_instance_ME!(wrapper, states, inputs, t) - - # TODO: Find a way to do this without allocating. We can't pass a view to these - # functions. - states_buffer = zeros(length(states)) - @statuscheck FMI.fmi2GetDerivatives!(instance, states_buffer) - outputs_buffer = zeros(length(fn.output_value_references)) - FMI.fmi2GetReal!(instance, fn.output_value_references, outputs_buffer) - return [states_buffer; outputs_buffer] -end - -""" - $(TYPEDEF) - -A callable struct useful for simulating v3 Model Exchange FMUs. When called, updates the -internal state of the FMU and gets updated values for continuous state derivatives and -output variables. - -# Fields - -$(TYPEDFIELDS) -""" -struct FMI3MEFunctor - """ - The value references for outputs of the FMU, in the order that the caller expects - them to be returned when calling `FMI3MEFunctor`. - """ - output_value_references::Vector{FMI.fmi3ValueReference} -end - -@register_array_symbolic (fn::FMI3MEFunctor)( +@register_array_symbolic (fn::FMI3InstanceWrapper)( wrapper::FMI3InstanceWrapper, states::Vector{<:Real}, inputs::Vector{<:Real}, params::Vector{<:Real}, t::Real) begin size = (length(states) + length(fn.output_value_references),) @@ -679,50 +682,30 @@ end """ $(TYPEDSIGNATURES) -Update `wrapper.instance` with the new values of state, input and independent variables. -""" -function update_instance_ME!(wrapper::FMI3InstanceWrapper, states, inputs, t) - instance = wrapper.instance - @statuscheck FMI.fmi3SetTime(instance, t) - @statuscheck FMI.fmi3SetContinuousStates(instance, states) - if !isempty(inputs) - @statuscheck FMI.fmi3SetFloat64(instance, wrapper.input_value_references, inputs) - end -end - -""" - $(TYPEDSIGNATURES) - -Get the FMU instance (creating and initializing it if not present), update it -with the current values of variables, and return a vector of the state derivatives -and output variables. +Update the internal state of the ME FMU and return a vector of updated values +for continuous state derivatives and output variables respectively. Needs to be a +callable struct to enable symbolic registration with an inferred return size. """ -function (fn::FMI3MEFunctor)(wrapper::FMI3InstanceWrapper, states, inputs, params, t) +function (wrapper::Union{FMI2InstanceWrapper, FMI3InstanceWrapper})( + states, inputs, params, t) instance = get_instance_ME!(wrapper, inputs, params, t) - update_instance_ME!(wrapper, states, inputs, t) - # TODO: Don't allocate + # TODO: Find a way to do this without allocating. We can't pass a view to these + # functions. states_buffer = zeros(length(states)) - @statuscheck FMI.fmi3GetContinuousStateDerivatives!(instance, states_buffer) - outputs_buffer = zeros(length(fn.output_value_references)) - FMI.fmi3GetFloat64!(instance, fn.output_value_references, outputs_buffer) + outputs_buffer = zeros(length(wrapper.output_value_references)) + # Defined in FMIBase.jl/src/eval.jl + # Doesn't seem to be documented, but somehow this is the only way to + # propagate inputs to the FMU consistently. I have no idea why. + instance(; x = states, u = inputs, u_refs = wrapper.input_value_references, + p = params, p_refs = wrapper.param_value_references, t = t) + # the spec requires completing the step before getting updated derivative/output values + partiallyCompleteIntegratorStep(wrapper) + instance(; dx = states_buffer, dx_refs = wrapper.derivative_value_references, + y = outputs_buffer, y_refs = wrapper.output_value_references) return [states_buffer; outputs_buffer] end -""" - $(TYPEDSIGNATURES) - -An affect function for use inside a `FunctionalAffect`. This should be triggered every -time an integrator step is accepted. Expects `p` to be a 1-length array containing -the index of the instance wrapper (`FMI2InstanceWrapper` or `FMI3InstanceWrapper`) in -the parameter object. -""" -function fmiMEStep!(integrator, u, p, ctx) - wrapper_idx = p[1] - wrapper = integrator.ps[wrapper_idx] - complete_step!(wrapper) -end - """ $(TYPEDSIGNATURES) @@ -762,7 +745,7 @@ function (fn::FMI2CSFunctor)(wrapper::FMI2InstanceWrapper, states, inputs, param states = states isa SubArray ? copy(states) : states inputs = inputs isa SubArray ? copy(inputs) : inputs params = params isa SubArray ? copy(params) : params - instance = get_instance_CS!(wrapper, inputs, params, t) + instance = get_instance_CS!(wrapper, states, inputs, params, t) if isempty(fn.output_value_references) return eltype(states)[] else @@ -799,7 +782,7 @@ function fmiCSInitialize!(m, o, ctx::FMI2CSFunctor, integrator) reset_instance!(wrapper) end - instance = get_instance_CS!(wrapper, inputs, params, t) + instance = get_instance_CS!(wrapper, states, inputs, params, t) if isdefined(m, :states) @statuscheck FMI.fmi2GetReal!(instance, ctx.state_value_references, m.states) end @@ -826,7 +809,11 @@ function fmiCSStep!(m, o, ctx::FMI2CSFunctor, integrator) t = o.t dt = o.dt - instance = get_instance_CS!(wrapper, inputs, params, integrator.t) + instance = get_instance_CS!(wrapper, states, inputs, params, integrator.t) + if !isempty(inputs) + FMI.fmi2SetReal( + instance, wrapper.input_value_references, Csize_t(length(inputs)), inputs) + end @statuscheck FMI.fmi2DoStep(instance, integrator.t - dt, dt, FMI.fmi2True) if isdefined(m, :states) @@ -864,7 +851,7 @@ function (fn::FMI3CSFunctor)(wrapper::FMI3InstanceWrapper, states, inputs, param states = states isa SubArray ? copy(states) : states inputs = inputs isa SubArray ? copy(inputs) : inputs params = params isa SubArray ? copy(params) : params - instance = get_instance_CS!(wrapper, inputs, params, t) + instance = get_instance_CS!(wrapper, states, inputs, params, t) if isempty(fn.output_value_references) return eltype(states)[] @@ -893,7 +880,7 @@ function fmiCSInitialize!(m, o, ctx::FMI3CSFunctor, integrator) if wrapper.instance !== nothing reset_instance!(wrapper) end - instance = get_instance_CS!(wrapper, inputs, params, t) + instance = get_instance_CS!(wrapper, states, inputs, params, t) if isdefined(m, :states) @statuscheck FMI.fmi3GetFloat64!(instance, ctx.state_value_references, m.states) end @@ -915,7 +902,10 @@ function fmiCSStep!(m, o, ctx::FMI3CSFunctor, integrator) t = o.t dt = o.dt - instance = get_instance_CS!(wrapper, inputs, params, integrator.t) + instance = get_instance_CS!(wrapper, states, inputs, params, integrator.t) + if !isempty(inputs) + FMI.fmi3SetFloat64(instance, wrapper.input_value_references, inputs) + end eventEncountered = Ref(FMI.fmi3False) terminateSimulation = Ref(FMI.fmi3False) earlyReturn = Ref(FMI.fmi3False) From 12d3454c8c848c0cf5f33eecda494296db6bdc1c Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 8 Jan 2025 16:25:41 +0530 Subject: [PATCH 0656/2176] test: update `SimpleAdder.fmu` --- test/extensions/fmus/SimpleAdder.fmu | Bin 658163 -> 659450 bytes 1 file changed, 0 insertions(+), 0 deletions(-) diff --git a/test/extensions/fmus/SimpleAdder.fmu b/test/extensions/fmus/SimpleAdder.fmu index a1c3fd2728376ebed8f8dfd81819b0be51c84bef..ed12961bfb0a2c03a9ccbe1c746028aa168fadb3 100644 GIT binary patch delta 210707 zcmYJZcQ9P<_Xi#%5u!#XT12!&38LEw;w?If&IS>pM(=JAEqVkIWfLVLI?>rBdR@J< zt9Of4c5VCR^Zos1es|{Hndi*h=a1LC&U0SR>l}23@D)Uu&QOQqI`y^xJrx>4mT7c6 zl)c-bPXFF4#yN7%>;D%tdhq`THz+yj{ueYT)r+#5AipMApY~G}`2YTK@(a9ujqEyS z@c{XCu796I8%k^DzDop!#zCyTghKTPZXLIpySHv>Y+QdSDVqDfL*)JTGQWMiki8~t zgL$F3ebT4){rRMt*AnCZ9oXDSQrRM1Y$**^9%Bgu+l~P++|l81j-2O8TegiBE`a{M zq40I$wy-ZN&|P*{plhn*_Dlx8yO4E(s(6P~A8qMxEA#^cvikDOoB7KDpKS(%f&GV} zWY5C8f_;Bsbs%_5$zbx(tmyrJE1w6aJgaW7i_yq~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ions(-) diff --git a/test/extensions/fmus/StateSpace.fmu b/test/extensions/fmus/StateSpace.fmu index c181081ca4222af0c306205c1d721c06905dd3a1..6138e1fdb265d3a94397dc6f8bdad51c46c12414 100644 GIT binary patch delta 20296 zcmaI7WmsEX@Gpu}yilyTyE_yJPO;)ziWe=#9kQX6;_ePDuEm{T#hp@$Lve?ofj|!L z`Ja34{dALO&CZ%>1Af5)WQOpyN1M?aB_4$$NV z+xGSwGKoV$mUdOv{ zxR|hze!UKYE7QI&(;Vj0?EUnri?8VmybSk_UCCT>)1t!i!F=HN*@hAaelNWx&r2=4 zdh5uSd`7*9kSGPay}X-^VKyp{hgHLntD z=1~PpcDG;}$nXY?O5Bo9YBvOa*p5e>u*n{@=n-{?h>_{2Q+3{YpP6UmR`auVo6IwT zXDhKNOda}rrPHvb+kS1Pm34ZBneDJ}Nu9jcW7X>1@2og!$XLweUlPhZ-&6R~^a(vc zxBp#OjaaT+pTMSVHgRRjtC`Q zE2^3|GY~`G^UJFo+ioW1Po!g%mJfUvWPQSHWa^+lm03Nbpy5UFpVRM-eJLPYjtF)whMs1KeH42AH`Xg@SVn4&X<)wiV|cFY2mEi0YOs{!d(6PuSs6kw;n4tM6~fA49v)G%N4 zMF!pKp-Lek>Ej|H{ZE1ZSC?#W{@()qZ;pjThh*jO+0xzNqX)0P9v%{=DI3_%@_9Tz zlh;rwP@hXW`2m#+=f5ickN^C9=96OnpK?#uL=*r2tMZc|98f6c5+qC>O+``ZeEYoj7?5)^L=yYtdu6}q-lUw2gxW%oer0)?^5Z;% zxqLjnwyt(9jyMb%Ryr>v8Z+~SugQ%3Q1OS^m4k4#Xqf|Xh4RL_NMmVn)<2Acc}r>T zE_Fah*X;JsS*J1NmpR9wvtN2~lMuh0MjIqsrZ)Qb4Qi!sR@jqVQz&ss36&S2eW-=) z{kSJX4Rfcf>>Ame!Wwy<_jK0Qb)B)s2O41Uab#P^Er|$WV#u2AYUznE|}SLHq+L1rE`al4}N9&3-r>4oEB%WoJbN+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W4d#&LrLx@QzJ0pt}yGT21q@tkS*inwl904euBY6#imH92(U$X52XB`7g z-fgsWZxlunD)I=Phlc&TFFBcWa~~^Z)tr19Nhr-DxMdeEx=kMQ@sUr}M2dc!JFZb7 zL;oO(ep`(A=F*w3TM#Y=ZR$TUR)))~kjQ}OFWZfI0|xhJuPc#yQsmg9V6P&?^_%qt zviJq0)Sw*7M;&7^{v&c(oBY!2SVcLnhbG51M9f8b3MITB>u50JZwuEWUhn6u zI*Sf^J`KP0sYneSd)a7g6cD-*ZfFHDcea1evgvb-50dynWD^I=cq_UD_+gl_qOeM% z=+DnbgCgq_1gwxk)>(H4#Alyayqv4EBAHuyGR_-XItZfOQ%>cR$;WY%iV~V&uSoJl zAz4F3ac*^rB!AUX>Lf0@H7;s;v@k`hmVv(fTF2$z=){%UTJD|1h(0yiDJ;J2KBf%k z?rp@spVB*?ZbAD%=xC25P))gY`Ln*p#iwrt?GlCpC_I^jdp>#s$VLAO)0rV#17kjU zq;E2FQWbbPLl|1jkv|P~NM`P-0y}iD=evWmaUQ#>#ZS5w@>#OC3)CVfamP}Oc!Jqf zUu@W+Dv<-wNKe^1T3GOOMEhUuvoNu^Gl|*F1~69(*G(f{UlCK0fLZtSd54v=HEX-|5h`RfrQybAjM1S)Uc)G6E`>-~tO64}d z7L8UjhIis}Y=4KWXt=j@q(5T~VY4Z4ceyw6r~S*{?zjJ)`|RL3b8s*Sw3n9-u+UI% zbi@y&vIQM&rhR&nlJVbErGL^D%F8%0d4W2)}~lAy{ih``VHQ8hZ9UxWogCyGe>nZV2h=cvdtrXw&b>18y Date: Wed, 8 Jan 2025 16:26:02 +0530 Subject: [PATCH 0658/2176] test: add more comprehensive tests for FMIComponent --- test/extensions/fmi.jl | 220 ++++++++++++++++++++++++++++++++++------- 1 file changed, 183 insertions(+), 37 deletions(-) diff --git a/test/extensions/fmi.jl b/test/extensions/fmi.jl index 4dc0fc9a67..145989ab29 100644 --- a/test/extensions/fmi.jl +++ b/test/extensions/fmi.jl @@ -81,83 +81,229 @@ const FMU_DIR = joinpath(@__DIR__, "fmus") end end -@testset "IO Model" begin - @testset "v2, ME" begin - fmu = loadFMU(joinpath(FMU_DIR, "SimpleAdder.fmu"); type = :ME) - @named adder = MTK.FMIComponent(Val(2); fmu, type = :ME) - @test MTK.isinput(adder.a) - @test MTK.isinput(adder.b) - @test MTK.isoutput(adder.out) - @test !MTK.isinput(adder.c) && !MTK.isoutput(adder.c) +@mtkmodel SimpleAdder begin + @variables begin + a(t) + b(t) + c(t) + out(t) + out2(t) + end + @parameters begin + value = 1.0 + end + @equations begin + out ~ a + b + value + D(c) ~ out + out2 ~ 2c + end +end + +@mtkmodel StateSpace begin + @variables begin + x(t) + y(t) + u(t) + end + @parameters begin + A = 1.0 + B = 1.0 + C = 1.0 + _D = 1.0 + end + @equations begin + D(x) ~ A * x + B * u + y ~ C * x + _D * u + end +end +@testset "IO Model" begin + function build_simple_adder(adder) @variables a(t) b(t) c(t) [guess = 1.0] @mtkbuild sys = ODESystem( [adder.a ~ a, adder.b ~ b, D(a) ~ t, D(b) ~ adder.out + adder.c, c^2 ~ adder.out + adder.value], t; systems = [adder]) - # c will be solved for by initialization # this tests that initialization also works with FMUs - prob = ODEProblem(sys, [sys.adder.c => 1.0, sys.a => 1.0, sys.b => 1.0], (0.0, 1.0)) - sol = solve(prob, Rodas5P(autodiff = false)) + prob = ODEProblem(sys, [sys.adder.c => 2.0, sys.a => 1.0, sys.b => 1.0], + (0.0, 1.0), [sys.adder.value => 2.0]) + return sys, prob + end + + @named adder = SimpleAdder() + truesys, trueprob = build_simple_adder(adder) + truesol = solve(trueprob, abstol = 1e-8, reltol = 1e-8) + @test SciMLBase.successful_retcode(truesol) + + @testset "v2, ME" begin + fmu = loadFMU(joinpath(FMU_DIR, "SimpleAdder.fmu"); type = :ME) + @named adder = MTK.FMIComponent(Val(2); fmu, type = :ME) + @test MTK.isinput(adder.a) + @test MTK.isinput(adder.b) + @test MTK.isoutput(adder.out) + @test MTK.isoutput(adder.out2) + @test !MTK.isinput(adder.c) && !MTK.isoutput(adder.c) + + sys, prob = build_simple_adder(adder) + sol = solve(prob, Rodas5P(autodiff = false), abstol = 1e-8, reltol = 1e-8) @test SciMLBase.successful_retcode(sol) + + @test truesol(sol.t; + idxs = [truesys.a, truesys.b, truesys.c, truesys.adder.c]).u≈sol[[ + sys.a, sys.b, sys.c, sys.adder.c]] rtol=1e-7 end @testset "v2, CS" begin fmu = loadFMU(joinpath(FMU_DIR, "SimpleAdder.fmu"); type = :CS) @named adder = MTK.FMIComponent( - Val(2); fmu, type = :CS, communication_step_size = 0.001) + Val(2); fmu, type = :CS, communication_step_size = 1e-3, + reinitializealg = BrownFullBasicInit()) @test MTK.isinput(adder.a) @test MTK.isinput(adder.b) @test MTK.isoutput(adder.out) + @test MTK.isoutput(adder.out2) @test !MTK.isinput(adder.c) && !MTK.isoutput(adder.c) - @variables a(t) b(t) c(t) [guess = 1.0] - @mtkbuild sys = ODESystem( - [adder.a ~ a, adder.b ~ b, D(a) ~ t, - D(b) ~ adder.out + adder.c, c^2 ~ adder.out + adder.value], - t; - systems = [adder]) - # c will be solved for by initialization - # this tests that initialization also works with FMUs - prob = ODEProblem(sys, [sys.adder.c => 1.0, sys.a => 1.0, sys.b => 1.0], - (0.0, 1.0); use_scc = false) - sol = solve(prob, Rodas5P(autodiff = false)) + sys, prob = build_simple_adder(adder) + sol = solve(prob, Rodas5P(autodiff = false), abstol = 1e-8, reltol = 1e-8) @test SciMLBase.successful_retcode(sol) + + @test truesol(sol.t; idxs = [truesys.a, truesys.b, truesys.c]).u≈sol.u rtol=1e-2 + # sys.adder.c is a discrete variable + @test truesol(sol.t; idxs = truesys.adder.c)≈sol(sol.t; idxs = sys.adder.c) rtol=1e-3 end + function build_sspace_model(sspace) + @variables u(t)=1.0 x(t)=1.0 y(t) [guess = 1.0] + @mtkbuild sys = ODESystem( + [sspace.u ~ u, D(u) ~ t, D(x) ~ sspace.x + sspace.y, y^2 ~ sspace.y + sspace.x], t; + systems = [sspace] + ) + + prob = ODEProblem( + sys, [sys.sspace.x => 1.0], (0.0, 1.0), [sys.sspace.A => 2.0]; use_scc = false) + return sys, prob + end + + @named sspace = StateSpace() + truesys, trueprob = build_sspace_model(sspace) + truesol = solve(trueprob, abstol = 1e-8, reltol = 1e-8) + @test SciMLBase.successful_retcode(truesol) + @testset "v3, ME" begin fmu = loadFMU(joinpath(FMU_DIR, "StateSpace.fmu"); type = :ME) @named sspace = MTK.FMIComponent(Val(3); fmu, type = :ME) @test MTK.isinput(sspace.u) @test MTK.isoutput(sspace.y) @test !MTK.isinput(sspace.x) && !MTK.isoutput(sspace.x) - @variables u(t)=1.0 x(t)=1.0 y(t) [guess = 1.0] - @mtkbuild sys = ODESystem( - [sspace.u ~ u, D(u) ~ t, D(x) ~ sspace.x + sspace.y, y^2 ~ sspace.y + x], t; - systems = [sspace] - ) - prob = ODEProblem(sys, [sys.sspace.x => 1.0], (0.0, 1.0); use_scc = false) - sol = solve(prob, Rodas5P(autodiff = false)) + sys, prob = build_sspace_model(sspace) + sol = solve(prob, Rodas5P(autodiff = false); abstol = 1e-8, reltol = 1e-8) @test SciMLBase.successful_retcode(sol) + + @test truesol(sol.t; + idxs = [truesys.u, truesys.x, truesys.y, truesys.sspace.x]).u≈sol[[ + sys.u, sys.x, sys.y, sys.sspace.x]] rtol=1e-7 end @testset "v3, CS" begin fmu = loadFMU(joinpath(FMU_DIR, "StateSpace.fmu"); type = :CS) @named sspace = MTK.FMIComponent( - Val(3); fmu, communication_step_size = 1e-3, type = :CS) + Val(3); fmu, communication_step_size = 1e-4, type = :CS, + reinitializealg = BrownFullBasicInit()) @test MTK.isinput(sspace.u) @test MTK.isoutput(sspace.y) @test !MTK.isinput(sspace.x) && !MTK.isoutput(sspace.x) - @variables u(t)=1.0 x(t)=1.0 y(t) [guess = 1.0] + + sys, prob = build_sspace_model(sspace) + sol = solve(prob, Rodas5P(autodiff = false); abstol = 1e-8, reltol = 1e-8) + @test SciMLBase.successful_retcode(sol) + + @test truesol( + sol.t; idxs = [truesys.u, truesys.x, truesys.y]).u≈sol[[sys.u, sys.x, sys.y]] rtol=1e-2 + @test truesol(sol.t; idxs = truesys.sspace.x).u≈sol(sol.t; idxs = sys.sspace.x).u rtol=1e-2 + end +end + +@testset "FMUs in a loop" begin + function build_looped_adders(adder1, adder2) + @variables x(t) = 1 @mtkbuild sys = ODESystem( - [sspace.u ~ u, D(u) ~ t, D(x) ~ sspace.x + sspace.y, y^2 ~ sspace.y + x], t; - systems = [sspace] - ) + [D(x) ~ x, adder1.a ~ adder2.out2, + adder2.a ~ adder1.out2, adder1.b ~ 1.0, adder2.b ~ 2.0], + t; + systems = [adder1, adder2]) + prob = ODEProblem( + sys, [adder1.c => 1.0, adder2.c => 1.0, adder1.a => 2.0], (0.0, 1.0)) + return sys, prob + end + @named adder1 = SimpleAdder() + @named adder2 = SimpleAdder() + truesys, trueprob = build_looped_adders(adder1, adder2) + truesol = solve(trueprob, Tsit5(), reltol = 1e-8) + @test SciMLBase.successful_retcode(truesol) + + @testset "v2, ME" begin + fmu = loadFMU(joinpath(FMU_DIR, "SimpleAdder.fmu"); type = :ME) + @named adder1 = MTK.FMIComponent(Val(2); fmu, type = :ME) + @named adder2 = MTK.FMIComponent(Val(2); fmu, type = :ME) + sys, prob = build_looped_adders(adder1, adder2) + sol = solve(prob, Rodas5P(autodiff = false); reltol = 1e-8) + @test SciMLBase.successful_retcode(sol) + @test truesol(sol.t; + idxs = [truesys.adder1.c, truesys.adder2.c]).u≈sol( + sol.t; idxs = [sys.adder1.c, sys.adder2.c]).u rtol=1e-7 + end + @testset "v2, CS" begin + fmu = loadFMU(joinpath(FMU_DIR, "SimpleAdder.fmu"); type = :CS) + @named adder1 = MTK.FMIComponent( + Val(2); fmu, type = :CS, communication_step_size = 1e-3) + @named adder2 = MTK.FMIComponent( + Val(2); fmu, type = :CS, communication_step_size = 1e-3) + sys, prob = build_looped_adders(adder1, adder2) + sol = solve(prob, Tsit5(); reltol = 1e-8) + @test truesol(sol.t; + idxs = [truesys.adder1.c, truesys.adder2.c]).u≈sol( + sol.t; idxs = [sys.adder1.c, sys.adder2.c]).u rtol=1e-3 + end + + function build_looped_sspace(sspace1, sspace2) + @variables x(t) = 1 + @mtkbuild sys = ODESystem([D(x) ~ x, sspace1.u ~ sspace2.x, sspace2.u ~ sspace1.y], + t; systems = [sspace1, sspace2]) + prob = ODEProblem(sys, [sspace1.x => 1.0, sspace2.x => 1.0], (0.0, 1.0)) + return sys, prob + end + @named sspace1 = StateSpace() + @named sspace2 = StateSpace() + truesys, trueprob = build_looped_sspace(sspace1, sspace2) + truesol = solve(trueprob, Rodas5P(), reltol = 1e-8) + @test SciMLBase.successful_retcode(truesol) + + @testset "v3, ME" begin + fmu = loadFMU(joinpath(FMU_DIR, "StateSpace.fmu"); type = :ME) + @named sspace1 = MTK.FMIComponent(Val(3); fmu, type = :ME) + @named sspace2 = MTK.FMIComponent(Val(3); fmu, type = :ME) + sys, prob = build_looped_sspace(sspace1, sspace2) + sol = solve(prob, Rodas5P(autodiff = false); reltol = 1e-8) + @test SciMLBase.successful_retcode(sol) + @test truesol(sol.t; + idxs = [truesys.sspace1.x, truesys.sspace2.x]).u≈sol( + sol.t; idxs = [sys.sspace1.x, sys.sspace2.x]).u rtol=1e-7 + end - prob = ODEProblem(sys, [sys.sspace.x => 1.0], (0.0, 1.0); use_scc = false) - sol = solve(prob, Rodas5P(autodiff = false)) + @testset "v3, CS" begin + fmu = loadFMU(joinpath(FMU_DIR, "StateSpace.fmu"); type = :CS) + @named sspace1 = MTK.FMIComponent( + Val(3); fmu, type = :CS, communication_step_size = 1e-4) + @named sspace2 = MTK.FMIComponent( + Val(3); fmu, type = :CS, communication_step_size = 1e-4) + sys, prob = build_looped_sspace(sspace1, sspace2) + sol = solve(prob, Rodas5P(autodiff = false); reltol = 1e-8) @test SciMLBase.successful_retcode(sol) + @test truesol(sol.t; + idxs = [truesys.sspace1.x, truesys.sspace2.x]).u≈sol( + sol.t; idxs = [sys.sspace1.x, sys.sspace2.x]).u rtol=1e-2 end end From a148354b6051de4ba2f277714a4986d88f958372 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 8 Jan 2025 16:29:37 +0530 Subject: [PATCH 0659/2176] test: add instructions for reproducibly building `SimpleAdder.fmu` --- test/extensions/fmus/SimpleAdder/README.md | 3 +++ test/extensions/fmus/SimpleAdder/SimpleAdder.mo | 12 ++++++++++++ test/extensions/fmus/SimpleAdder/buildFmu.mos | 12 ++++++++++++ 3 files changed, 27 insertions(+) create mode 100644 test/extensions/fmus/SimpleAdder/README.md create mode 100644 test/extensions/fmus/SimpleAdder/SimpleAdder.mo create mode 100644 test/extensions/fmus/SimpleAdder/buildFmu.mos diff --git a/test/extensions/fmus/SimpleAdder/README.md b/test/extensions/fmus/SimpleAdder/README.md new file mode 100644 index 0000000000..b26c247bce --- /dev/null +++ b/test/extensions/fmus/SimpleAdder/README.md @@ -0,0 +1,3 @@ +SimpleAdder.fmu is a v2 FMU built using OpenModelica. The file `SimpleAdder.mo` contains the +modelica source code for the model. The file `buildFMU.mos` is the OpenModelica script used +to build the FMU. diff --git a/test/extensions/fmus/SimpleAdder/SimpleAdder.mo b/test/extensions/fmus/SimpleAdder/SimpleAdder.mo new file mode 100644 index 0000000000..ce97f09a86 --- /dev/null +++ b/test/extensions/fmus/SimpleAdder/SimpleAdder.mo @@ -0,0 +1,12 @@ +block SimpleAdder + parameter Real value = 1.0; + input Real a; + input Real b; + Real c(start = 1.0, fixed = true); + output Real out; + output Real out2; +equation + der(c) = out; + out = a + b + value; + out2 = 2 * c; +end SimpleAdder; diff --git a/test/extensions/fmus/SimpleAdder/buildFmu.mos b/test/extensions/fmus/SimpleAdder/buildFmu.mos new file mode 100644 index 0000000000..df2c9bd58e --- /dev/null +++ b/test/extensions/fmus/SimpleAdder/buildFmu.mos @@ -0,0 +1,12 @@ +OpenModelica.Scripting.loadFile("SimpleAdder.mo"); getErrorString(); + +installPackage(Modelica); + +setCommandLineOptions("-d=newInst"); getErrorString(); +setCommandLineOptions("-d=initialization"); getErrorString(); +setCommandLineOptions("-d=-disableDirectionalDerivatives"); getErrorString(); + +cd("output"); getErrorString(); +buildModelFMU(SimpleAdder, version = "2.0", fmuType = "me_cs"); getErrorString(); +system("unzip -l SimpleAdder.fmu | egrep -v 'sources|files' | tail -n+3 | grep -o '[A-Za-z._0-9/]*$' > BB.log") + From 7e3540365f4b07597cbbe4271bddf87257a98328 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 8 Jan 2025 16:37:49 +0530 Subject: [PATCH 0660/2176] test: add instructions for reproducibly building StateSpace.fmu --- .../fmus/StateSpace/0001-tmp-commit.patch | 425 ++++++++++++++++++ test/extensions/fmus/StateSpace/README.md | 7 + 2 files changed, 432 insertions(+) create mode 100644 test/extensions/fmus/StateSpace/0001-tmp-commit.patch create mode 100644 test/extensions/fmus/StateSpace/README.md diff --git a/test/extensions/fmus/StateSpace/0001-tmp-commit.patch b/test/extensions/fmus/StateSpace/0001-tmp-commit.patch new file mode 100644 index 0000000000..951af2d4fd --- /dev/null +++ b/test/extensions/fmus/StateSpace/0001-tmp-commit.patch @@ -0,0 +1,425 @@ +From 10ac73996a7c09772ff31ccd6e030112d3bb5c43 Mon Sep 17 00:00:00 2001 +From: Aayush Sabharwal +Date: Wed, 8 Jan 2025 11:04:01 +0000 +Subject: tmp commit + +--- + StateSpace/FMI3.xml | 31 ++---- + StateSpace/config.h | 18 ++-- + StateSpace/model.c | 230 +++++++++++++------------------------------- + 3 files changed, 83 insertions(+), 196 deletions(-) + +diff --git a/StateSpace/FMI3.xml b/StateSpace/FMI3.xml +index 8d2e4d9..002fbab 100644 +--- a/StateSpace/FMI3.xml ++++ b/StateSpace/FMI3.xml +@@ -33,36 +33,23 @@ + + + +- +- +- ++ + +- +- +- ++ + +- +- +- ++ + +- +- +- ++ + +- +- ++ + +- +- ++ + +- +- ++ + +- +- ++ + +- +- ++ + + + +diff --git a/StateSpace/config.h b/StateSpace/config.h +index 707e500..8b422e2 100644 +--- a/StateSpace/config.h ++++ b/StateSpace/config.h +@@ -44,15 +44,15 @@ typedef struct { + uint64_t m; + uint64_t n; + uint64_t r; +- double A[M_MAX][N_MAX]; +- double B[M_MAX][N_MAX]; +- double C[M_MAX][N_MAX]; +- double D[M_MAX][N_MAX]; +- double x0[N_MAX]; +- double u[N_MAX]; +- double y[N_MAX]; +- double x[N_MAX]; +- double der_x[N_MAX]; ++ double A; ++ double B; ++ double C; ++ double D; ++ double x0; ++ double u; ++ double y; ++ double x; ++ double der_x; + } ModelData; + + #endif /* config_h */ +diff --git a/StateSpace/model.c b/StateSpace/model.c +index 8a47e74..9c170d8 100644 +--- a/StateSpace/model.c ++++ b/StateSpace/model.c +@@ -3,77 +3,29 @@ + + + void setStartValues(ModelInstance *comp) { +- +- M(m) = 3; +- M(n) = 3; +- M(r) = 3; +- + // identity matrix +- for (int i = 0; i < M_MAX; i++) +- for (int j = 0; j < N_MAX; j++) { +- M(A)[i][j] = i == j ? 1 : 0; +- M(B)[i][j] = i == j ? 1 : 0; +- M(C)[i][j] = i == j ? 1 : 0; +- M(D)[i][j] = i == j ? 1 : 0; +- } +- +- for (int i = 0; i < M_MAX; i++) { +- M(u)[i] = i + 1; +- } +- +- for (int i = 0; i < N_MAX; i++) { +- M(y)[i] = 0; +- } +- +- for (int i = 0; i < N_MAX; i++) { +- M(x)[i] = M(x0)[i]; +- M(x)[i] = 0; +- } +- ++ M(m) = 1; ++ M(n) = 1; ++ M(r) = 1; ++ ++ M(A) = 1; ++ M(B) = 1; ++ M(C) = 1; ++ M(D) = 1; ++ ++ M(u) = 1; ++ M(y) = 0; ++ M(x) = 0; ++ M(x0) = 0; + } + + Status calculateValues(ModelInstance *comp) { +- +- // der(x) = Ax + Bu +- for (size_t i = 0; i < M(n); i++) { +- +- M(der_x)[i] = 0; +- +- for (size_t j = 0; j < M(n); j++) { +- M(der_x)[i] += M(A)[i][j] * M(x)[j]; +- } +- } +- +- for (size_t i = 0; i < M(n); i++) { +- +- for (size_t j = 0; j < M(r); j++) { +- M(der_x)[i] += M(B)[i][j] * M(u)[j]; +- } +- } +- +- +- // y = Cx + Du +- for (size_t i = 0; i < M(r); i++) { +- +- M(y)[i] = 0; +- +- for (size_t j = 0; j < M(n); j++) { +- M(y)[i] += M(C)[i][j] * M(x)[j]; +- } +- } +- +- for (size_t i = 0; i < M(r); i++) { +- +- for (size_t j = 0; j < M(m); j++) { +- M(y)[i] += M(D)[i][j] * M(u)[j]; +- } +- } +- ++ M(der_x) = M(A) * M(x) + M(B) * M(u); ++ M(y) = M(C) * M(x) + M(D) * M(u); + return OK; + } + + Status getFloat64(ModelInstance* comp, ValueReference vr, double values[], size_t nValues, size_t* index) { +- + calculateValues(comp); + + switch (vr) { +@@ -82,66 +34,40 @@ Status getFloat64(ModelInstance* comp, ValueReference vr, double values[], size_ + values[(*index)++] = comp->time; + return OK; + case vr_A: +- ASSERT_NVALUES((size_t)(M(n) * M(n))); +- for (size_t i = 0; i < M(n); i++) { +- for (size_t j = 0; j < M(n); j++) { +- values[(*index)++] = M(A)[i][j]; +- } +- } ++ ASSERT_NVALUES(1); ++ values[(*index)++] = M(A); + return OK; + case vr_B: +- ASSERT_NVALUES((size_t)(M(m) * M(n))); +- for (size_t i = 0; i < M(m); i++) { +- for (size_t j = 0; j < M(n); j++) { +- values[(*index)++] = M(B)[i][j]; +- } +- } ++ ASSERT_NVALUES(1); ++ values[(*index)++] = M(B); + return OK; + case vr_C: +- ASSERT_NVALUES((size_t)(M(r) * M(n))); +- for (size_t i = 0; i < M(r); i++) { +- for (size_t j = 0; j < M(n); j++) { +- values[(*index)++] = M(C)[i][j]; +- } +- } ++ ASSERT_NVALUES(1); ++ values[(*index)++] = M(C); + return OK; + case vr_D: +- ASSERT_NVALUES((size_t)(M(r) * M(m))); +- for (size_t i = 0; i < M(r); i++) { +- for (size_t j = 0; j < M(m); j++) { +- values[(*index)++] = M(D)[i][j]; +- } +- } ++ ASSERT_NVALUES(1); ++ values[(*index)++] = M(D); + return OK; + case vr_x0: +- ASSERT_NVALUES((size_t)M(n)); +- for (size_t i = 0; i < M(n); i++) { +- values[(*index)++] = M(x0)[i]; +- } ++ ASSERT_NVALUES(1); ++ values[(*index)++] = M(x0); + return OK; + case vr_u: +- ASSERT_NVALUES((size_t)M(m)); +- for (size_t i = 0; i < M(m); i++) { +- values[(*index)++] = M(u)[i]; +- } ++ ASSERT_NVALUES(1); ++ values[(*index)++] = M(u); + return OK; + case vr_y: +- ASSERT_NVALUES((size_t)M(r)); +- for (size_t i = 0; i < M(r); i++) { +- values[(*index)++] = M(y)[i]; +- } ++ ASSERT_NVALUES(1); ++ values[(*index)++] = M(y); + return OK; + case vr_x: +- ASSERT_NVALUES((size_t)M(n)); +- for (size_t i = 0; i < M(n); i++) { +- values[(*index)++] = M(x)[i]; +- } ++ ASSERT_NVALUES(1); ++ values[(*index)++] = M(x); + return OK; + case vr_der_x: +- ASSERT_NVALUES((size_t)M(n)); +- for (size_t i = 0; i < M(n); i++) { +- values[(*index)++] = M(der_x)[i]; +- } ++ ASSERT_NVALUES(1); ++ values[(*index)++] = M(der_x); + return OK; + default: + logError(comp, "Get Float64 is not allowed for value reference %u.", vr); +@@ -153,58 +79,40 @@ Status setFloat64(ModelInstance* comp, ValueReference vr, const double values[], + + switch (vr) { + case vr_A: +- ASSERT_NVALUES((size_t)(M(n) * M(n))); +- for (size_t i = 0; i < M(n); i++) { +- for (size_t j = 0; j < M(n); j++) { +- M(A)[i][j] = values[(*index)++]; +- } +- } ++ ASSERT_NVALUES(1); ++ M(A) = values[(*index)++]; + break; + case vr_B: +- ASSERT_NVALUES((size_t)(M(n) * M(m))); +- for (size_t i = 0; i < M(n); i++) { +- for (size_t j = 0; j < M(m); j++) { +- M(B)[i][j] = values[(*index)++]; +- } +- } ++ ASSERT_NVALUES(1); ++ M(B) = values[(*index)++]; + break; + case vr_C: +- ASSERT_NVALUES((size_t)(M(r) * M(n))); +- for (size_t i = 0; i < M(r); i++) { +- for (size_t j = 0; j < M(n); j++) { +- M(C)[i][j] = values[(*index)++]; +- } +- } ++ ASSERT_NVALUES(1); ++ M(C) = values[(*index)++]; + break; + case vr_D: +- ASSERT_NVALUES((size_t)(M(r) * M(m))); +- for (size_t i = 0; i < M(r); i++) { +- for (size_t j = 0; j < M(m); j++) { +- M(D)[i][j] = values[(*index)++]; +- } +- } ++ ASSERT_NVALUES(1); ++ M(D) = values[(*index)++]; + break; + case vr_x0: +- ASSERT_NVALUES((size_t)M(n)); +- for (size_t i = 0; i < M(n); i++) { +- M(x0)[i] = values[(*index)++]; +- } ++ ASSERT_NVALUES(1); ++ M(x0) = values[(*index)++]; + break; + case vr_u: +- ASSERT_NVALUES((size_t)M(m)); +- for (size_t i = 0; i < M(m); i++) { +- M(u)[i] = values[(*index)++]; +- } ++ ASSERT_NVALUES(1); ++ M(u) = values[(*index)++]; ++ break; ++ case vr_y: ++ ASSERT_NVALUES(1); ++ M(y) = values[(*index)++]; + break; + case vr_x: +- if (comp->state != ContinuousTimeMode && comp->state != EventMode) { +- logError(comp, "Variable \"x\" can only be set in Continuous Time Mode and Event Mode."); +- return Error; +- } +- ASSERT_NVALUES((size_t)M(n)); +- for (size_t i = 0; i < M(n); i++) { +- M(x)[i] = values[(*index)++]; +- } ++ ASSERT_NVALUES(1); ++ M(x) = values[(*index)++]; ++ break; ++ case vr_der_x: ++ ASSERT_NVALUES(1); ++ M(der_x) = values[(*index)++]; + break; + default: + logError(comp, "Set Float64 is not allowed for value reference %u.", vr); +@@ -217,8 +125,6 @@ Status setFloat64(ModelInstance* comp, ValueReference vr, const double values[], + } + + Status getUInt64(ModelInstance* comp, ValueReference vr, uint64_t values[], size_t nValues, size_t* index) { +- +- + switch (vr) { + case vr_m: + ASSERT_NVALUES(1); +@@ -289,49 +195,43 @@ Status eventUpdate(ModelInstance *comp) { + } + + size_t getNumberOfContinuousStates(ModelInstance* comp) { +- return (size_t)M(n); ++ return M(n) == 1 ? M(n) : 1; + } + + Status getContinuousStates(ModelInstance* comp, double x[], size_t nx) { + +- if (nx != M(n)) { +- logError(comp, "Expected nx=%zu but was %zu.", M(n), nx); ++ if (nx != 1) { ++ logError(comp, "Expected nx=%zu but was %zu.", 1, nx); + return Error; + } + +- for (size_t i = 0; i < M(n); i++) { +- x[i] = M(x)[i]; +- } ++ x[0] = M(x); + + return OK; + } + + Status setContinuousStates(ModelInstance* comp, const double x[], size_t nx) { + +- if (nx != M(n)) { +- logError(comp, "Expected nx=%zu but was %zu.", M(n), nx); ++ if (nx != 1) { ++ logError(comp, "Expected nx=%zu but was %zu.", 1, nx); + return Error; + } + +- for (size_t i = 0; i < M(n); i++) { +- M(x)[i] = x[i]; +- } ++ M(x) = x[0]; + + return OK; + } + + Status getDerivatives(ModelInstance* comp, double dx[], size_t nx) { + +- if (nx != M(n)) { +- logError(comp, "Expected nx=%zu but was %zu.", M(n), nx); ++ if (nx != 1) { ++ logError(comp, "Expected nx=%zu but was %zu.", 1, nx); + return Error; + } + + calculateValues(comp); + +- for (size_t i = 0; i < M(n); i++) { +- dx[i] = M(der_x)[i]; +- } ++ dx[0] = M(der_x); + + return OK; + } +-- +2.34.1 + diff --git a/test/extensions/fmus/StateSpace/README.md b/test/extensions/fmus/StateSpace/README.md new file mode 100644 index 0000000000..108147443d --- /dev/null +++ b/test/extensions/fmus/StateSpace/README.md @@ -0,0 +1,7 @@ +StateSpace.fmu is a v3 FMU built using a modified version of the StateSpace FMU in Modelica's +Reference-FMUs. The link to the specific commit hash used is + +[https://github.com/modelica/Reference-FMUs/tree/0e1374cad0a7f0583a583ce189060ba7a67c27a7]() + +The git patch containing the changes is provided as `0001-tmp-commit.patch`. The FMU can be +built using the instructions provided in the repository's README. From 051f551917f5ecf36dd91b0c931fe92c23fb3551 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 16 Jan 2025 19:56:15 +0530 Subject: [PATCH 0661/2176] build: bump SymbolicUtils compat --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 13684a6a52..e8a8fea71a 100644 --- a/Project.toml +++ b/Project.toml @@ -146,7 +146,7 @@ StaticArrays = "0.10, 0.11, 0.12, 1.0" StochasticDiffEq = "6.72.1" StochasticDelayDiffEq = "1.8.1" SymbolicIndexingInterface = "0.3.37" -SymbolicUtils = "3.10" +SymbolicUtils = "3.10.1" Symbolics = "6.23" URIs = "1" UnPack = "0.1, 1.0" From fe2075bf4d9bddbab8b42e9e9ad3a7b7a9920569 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 20 Jan 2025 15:56:15 +0530 Subject: [PATCH 0662/2176] fix: fix type promotion in `InitializationProblem` --- src/systems/diffeqs/abstractodesystem.jl | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/systems/diffeqs/abstractodesystem.jl b/src/systems/diffeqs/abstractodesystem.jl index 921825dac8..6571aed9cc 100644 --- a/src/systems/diffeqs/abstractodesystem.jl +++ b/src/systems/diffeqs/abstractodesystem.jl @@ -1391,16 +1391,16 @@ function InitializationProblem{iip, specialize}(sys::AbstractSystem, fullmap = merge(u0map, parammap) u0T = Union{} for sym in unknowns(isys) - haskey(fullmap, sym) || continue - symbolic_type(fullmap[sym]) == NotSymbolic() || continue - is_array_of_symbolics(fullmap[sym]) && continue - u0T = promote_type(u0T, typeof(fullmap[sym])) + val = fixpoint_sub(sym, fullmap) + symbolic_type(val) == NotSymbolic() || continue + u0T = promote_type(u0T, typeof(val)) end for eq in observed(isys) - haskey(fullmap, eq.lhs) || continue - symbolic_type(fullmap[eq.lhs]) == NotSymbolic() || continue - is_array_of_symbolics(fullmap[eq.lhs]) && continue - u0T = promote_type(u0T, typeof(fullmap[eq.lhs])) + # ignore HACK-ed observed equations + symbolic_type(eq.lhs) == ArraySymbolic() && continue + val = fixpoint_sub(eq.lhs, fullmap) + symbolic_type(val) == NotSymbolic() || continue + u0T = promote_type(u0T, typeof(val)) end if u0T != Union{} u0T = eltype(u0T) From a7ff2041669457f819f1d25eb79a7ae226ef347b Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 20 Jan 2025 15:56:26 +0530 Subject: [PATCH 0663/2176] test: fix CSE hack test --- test/structural_transformation/utils.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/structural_transformation/utils.jl b/test/structural_transformation/utils.jl index 863e091aad..621aa8b8a8 100644 --- a/test/structural_transformation/utils.jl +++ b/test/structural_transformation/utils.jl @@ -92,7 +92,7 @@ end @mtkbuild sys = ODESystem( [D(y) ~ foo(x), D(x) ~ sum(y), zeros(2) ~ foo(prod(z))], t) @test length(equations(sys)) == 5 - @test length(observed(sys)) == 2 + @test length(observed(sys)) == 4 prob = ODEProblem( sys, [y => ones(2), z => 2ones(2), x => 3.0], (0.0, 1.0), [foo => _tmp_fn2]) @test_nowarn prob.f(prob.u0, prob.p, 0.0) From 06b4d1190950c349ea66866ba25afdec51603a71 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 21 Jan 2025 15:03:09 +0530 Subject: [PATCH 0664/2176] build: bump Symbolics compat --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index e8a8fea71a..c4feb9b593 100644 --- a/Project.toml +++ b/Project.toml @@ -147,7 +147,7 @@ StochasticDiffEq = "6.72.1" StochasticDelayDiffEq = "1.8.1" SymbolicIndexingInterface = "0.3.37" SymbolicUtils = "3.10.1" -Symbolics = "6.23" +Symbolics = "6.25" URIs = "1" UnPack = "0.1, 1.0" Unitful = "1.1" From 6c4ea64ccf1007b4796a99768a2f006c422838be Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 21 Jan 2025 17:17:20 +0530 Subject: [PATCH 0665/2176] test: move FMI tests to separate environment --- .github/workflows/Tests.yml | 1 + test/extensions/Project.toml | 2 -- test/fmi/Project.toml | 8 ++++++++ test/{extensions => fmi}/fmi.jl | 0 test/{extensions => fmi}/fmus/SimpleAdder.fmu | Bin test/{extensions => fmi}/fmus/SimpleAdder/README.md | 0 .../fmus/SimpleAdder/SimpleAdder.mo | 0 .../fmus/SimpleAdder/buildFmu.mos | 0 test/{extensions => fmi}/fmus/StateSpace.fmu | Bin .../fmus/StateSpace/0001-tmp-commit.patch | 0 test/{extensions => fmi}/fmus/StateSpace/README.md | 0 test/runtests.jl | 12 +++++++++++- 12 files changed, 20 insertions(+), 3 deletions(-) create mode 100644 test/fmi/Project.toml rename test/{extensions => fmi}/fmi.jl (100%) rename test/{extensions => fmi}/fmus/SimpleAdder.fmu (100%) rename test/{extensions => fmi}/fmus/SimpleAdder/README.md (100%) rename test/{extensions => fmi}/fmus/SimpleAdder/SimpleAdder.mo (100%) rename test/{extensions => fmi}/fmus/SimpleAdder/buildFmu.mos (100%) rename test/{extensions => fmi}/fmus/StateSpace.fmu (100%) rename test/{extensions => fmi}/fmus/StateSpace/0001-tmp-commit.patch (100%) rename test/{extensions => fmi}/fmus/StateSpace/README.md (100%) diff --git a/.github/workflows/Tests.yml b/.github/workflows/Tests.yml index a95f19a085..52c5482970 100644 --- a/.github/workflows/Tests.yml +++ b/.github/workflows/Tests.yml @@ -38,6 +38,7 @@ jobs: - Extensions - Downstream - RegressionI + - FMI uses: "SciML/.github/.github/workflows/tests.yml@v1" with: julia-version: "${{ matrix.version }}" diff --git a/test/extensions/Project.toml b/test/extensions/Project.toml index 72501554b5..5f7afe222a 100644 --- a/test/extensions/Project.toml +++ b/test/extensions/Project.toml @@ -2,8 +2,6 @@ BifurcationKit = "0f109fa4-8a5d-4b75-95aa-f515264e7665" ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4" ChainRulesTestUtils = "cdddcdb0-9152-4a09-a978-84456f9df70a" -FMI = "14a09403-18e3-468f-ad8a-74f8dda2d9ac" -FMIZoo = "724179cf-c260-40a9-bd27-cccc6fe2f195" ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210" HomotopyContinuation = "f213a82b-91d6-5c5d-acf7-10f1c761b327" InfiniteOpt = "20393b10-9daf-11e9-18c9-8db751c92c57" diff --git a/test/fmi/Project.toml b/test/fmi/Project.toml new file mode 100644 index 0000000000..59bd3c90da --- /dev/null +++ b/test/fmi/Project.toml @@ -0,0 +1,8 @@ +[deps] +FMI = "14a09403-18e3-468f-ad8a-74f8dda2d9ac" +FMIZoo = "724179cf-c260-40a9-bd27-cccc6fe2f195" +ModelingToolkit = "961ee093-0014-501f-94e3-6117800e7a78" +OrdinaryDiffEq = "1dea7af3-3e70-54e6-95c3-0bf5283fa5ed" + +[compat] +FMI = "0.14" diff --git a/test/extensions/fmi.jl b/test/fmi/fmi.jl similarity index 100% rename from test/extensions/fmi.jl rename to test/fmi/fmi.jl diff --git a/test/extensions/fmus/SimpleAdder.fmu b/test/fmi/fmus/SimpleAdder.fmu similarity index 100% rename from test/extensions/fmus/SimpleAdder.fmu rename to test/fmi/fmus/SimpleAdder.fmu diff --git a/test/extensions/fmus/SimpleAdder/README.md b/test/fmi/fmus/SimpleAdder/README.md similarity index 100% rename from test/extensions/fmus/SimpleAdder/README.md rename to test/fmi/fmus/SimpleAdder/README.md diff --git a/test/extensions/fmus/SimpleAdder/SimpleAdder.mo b/test/fmi/fmus/SimpleAdder/SimpleAdder.mo similarity index 100% rename from test/extensions/fmus/SimpleAdder/SimpleAdder.mo rename to test/fmi/fmus/SimpleAdder/SimpleAdder.mo diff --git a/test/extensions/fmus/SimpleAdder/buildFmu.mos b/test/fmi/fmus/SimpleAdder/buildFmu.mos similarity index 100% rename from test/extensions/fmus/SimpleAdder/buildFmu.mos rename to test/fmi/fmus/SimpleAdder/buildFmu.mos diff --git a/test/extensions/fmus/StateSpace.fmu b/test/fmi/fmus/StateSpace.fmu similarity index 100% rename from test/extensions/fmus/StateSpace.fmu rename to test/fmi/fmus/StateSpace.fmu diff --git a/test/extensions/fmus/StateSpace/0001-tmp-commit.patch b/test/fmi/fmus/StateSpace/0001-tmp-commit.patch similarity index 100% rename from test/extensions/fmus/StateSpace/0001-tmp-commit.patch rename to test/fmi/fmus/StateSpace/0001-tmp-commit.patch diff --git a/test/extensions/fmus/StateSpace/README.md b/test/fmi/fmus/StateSpace/README.md similarity index 100% rename from test/extensions/fmus/StateSpace/README.md rename to test/fmi/fmus/StateSpace/README.md diff --git a/test/runtests.jl b/test/runtests.jl index bfb9e0b6f9..52875fdae5 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -4,6 +4,12 @@ import REPL const GROUP = get(ENV, "GROUP", "All") +function activate_fmi_env() + Pkg.activate("fmi") + Pkg.develop(PackageSpec(path = dirname(@__DIR__))) + Pkg.instantiate() +end + function activate_extensions_env() Pkg.activate("extensions") Pkg.develop(PackageSpec(path = dirname(@__DIR__))) @@ -115,9 +121,13 @@ end @safetestset "Analysis Points Test" include("downstream/analysis_points.jl") end + if GROUP == "All" || GROUP == "FMI" + activate_fmi_env() + @safetestset "FMI Extension Test" include("fmi/fmi.jl") + end + if GROUP == "All" || GROUP == "Extensions" activate_extensions_env() - @safetestset "FMI Extension Test" include("extensions/fmi.jl") @safetestset "HomotopyContinuation Extension Test" include("extensions/homotopy_continuation.jl") @safetestset "Auto Differentiation Test" include("extensions/ad.jl") @safetestset "LabelledArrays Test" include("labelledarrays.jl") From 110e086ab0b99b48d422c58e7af914cdd726418c Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 22 Jan 2025 11:39:03 +0530 Subject: [PATCH 0666/2176] fix: fix equality comparison of `SDESystem` --- src/systems/diffeqs/odesystem.jl | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/systems/diffeqs/odesystem.jl b/src/systems/diffeqs/odesystem.jl index 431261f102..41bcaf1d01 100644 --- a/src/systems/diffeqs/odesystem.jl +++ b/src/systems/diffeqs/odesystem.jl @@ -687,13 +687,22 @@ function populate_delays(delays::Set, obsexprs, histfn, sys, sym) end function _eq_unordered(a, b) + # a and b may be multidimensional + # e.g. comparing noiseeqs of SDESystem + a = vec(a) + b = vec(b) length(a) === length(b) || return false n = length(a) idxs = Set(1:n) for x in a idx = findfirst(isequal(x), b) + # loop since there might be multiple identical entries in a/b + # and while we might have already matched the first there could + # be a second that is equal to x + while idx !== nothing && !(idx in idxs) + idx = findnext(isequal(x), b, idx + 1) + end idx === nothing && return false - idx ∈ idxs || return false delete!(idxs, idx) end return true From 2646b81ae8d393a7cd137de56c5ca3b955f07955 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 27 Jan 2025 12:49:46 +0530 Subject: [PATCH 0667/2176] fix: fix initialization of v2 CS FMUs --- ext/MTKFMIExt.jl | 3 +++ 1 file changed, 3 insertions(+) diff --git a/ext/MTKFMIExt.jl b/ext/MTKFMIExt.jl index 603ec8906c..5cfe9a82ef 100644 --- a/ext/MTKFMIExt.jl +++ b/ext/MTKFMIExt.jl @@ -745,6 +745,9 @@ function (fn::FMI2CSFunctor)(wrapper::FMI2InstanceWrapper, states, inputs, param states = states isa SubArray ? copy(states) : states inputs = inputs isa SubArray ? copy(inputs) : inputs params = params isa SubArray ? copy(params) : params + if wrapper.instance !== nothing + reset_instance!(wrapper) + end instance = get_instance_CS!(wrapper, states, inputs, params, t) if isempty(fn.output_value_references) return eltype(states)[] From c850df7bb2e037e01b007d4f032ca8f6a053b642 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 27 Jan 2025 12:50:02 +0530 Subject: [PATCH 0668/2176] fix: don't wrap arrays in `Origin` in array hack if not offset --- src/structural_transformation/symbolics_tearing.jl | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/structural_transformation/symbolics_tearing.jl b/src/structural_transformation/symbolics_tearing.jl index f1a152b8e7..d95951c41e 100644 --- a/src/structural_transformation/symbolics_tearing.jl +++ b/src/structural_transformation/symbolics_tearing.jl @@ -771,6 +771,9 @@ getindex_wrapper(x, i) = x[i...] # PART OF HACK 2 function change_origin(origin, arr) + if all(isone, Tuple(origin)) + return arr + end return Origin(origin)(arr) end From 2e1d3c59f36fa1d9e3dcbf19221c1f6d9472448a Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 27 Jan 2025 12:50:18 +0530 Subject: [PATCH 0669/2176] fix: fix SCCNonlinearProblem codegen --- src/systems/nonlinear/nonlinearsystem.jl | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/systems/nonlinear/nonlinearsystem.jl b/src/systems/nonlinear/nonlinearsystem.jl index 6b0b0cc759..89663f7dbc 100644 --- a/src/systems/nonlinear/nonlinearsystem.jl +++ b/src/systems/nonlinear/nonlinearsystem.jl @@ -710,7 +710,7 @@ function SciMLBase.SCCNonlinearProblem{iip}(sys::NonlinearSystem, u0map, # precomputed subexpressions should not contain `banned_vars` banned_vars = Set{Any}(vcat(_dvs, getproperty.(_obs, (:lhs,)))) filter!(banned_vars) do var - symbolic_type(var) != ArraySymbolic() || all(x -> var[i] in banned_vars, eachindex(var)) + symbolic_type(var) != ArraySymbolic() || all(j -> var[j] in banned_vars, eachindex(var)) end state = Dict() for i in eachindex(_obs) @@ -772,6 +772,7 @@ function SciMLBase.SCCNonlinearProblem{iip}(sys::NonlinearSystem, u0map, _obs = scc_obs[i] cachevars = scc_cachevars[i] cacheexprs = scc_cacheexprs[i] + _prevobsidxs = vcat(_prevobsidxs, observed_equations_used_by(sys, reduce(vcat, values(cacheexprs); init = []))) if isempty(cachevars) push!(explicitfuns, Returns(nothing)) From afdd072fb6553de6f3aeda68dbe59e1772df2e3d Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 27 Jan 2025 12:50:34 +0530 Subject: [PATCH 0670/2176] test: fix CS tests --- test/fmi/fmi.jl | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/fmi/fmi.jl b/test/fmi/fmi.jl index 145989ab29..bf5b60459f 100644 --- a/test/fmi/fmi.jl +++ b/test/fmi/fmi.jl @@ -1,4 +1,4 @@ -using ModelingToolkit, FMI, FMIZoo, OrdinaryDiffEq +using ModelingToolkit, FMI, FMIZoo, OrdinaryDiffEq, NonlinearSolve, SciMLBase using ModelingToolkit: t_nounits as t, D_nounits as D import ModelingToolkit as MTK @@ -171,7 +171,7 @@ end @test truesol(sol.t; idxs = [truesys.a, truesys.b, truesys.c]).u≈sol.u rtol=1e-2 # sys.adder.c is a discrete variable - @test truesol(sol.t; idxs = truesys.adder.c)≈sol(sol.t; idxs = sys.adder.c) rtol=1e-3 + @test truesol(sol.t; idxs = truesys.adder.c).u≈sol(sol.t; idxs = sys.adder.c).u rtol=1e-3 end function build_sspace_model(sspace) @@ -262,7 +262,7 @@ end @named adder2 = MTK.FMIComponent( Val(2); fmu, type = :CS, communication_step_size = 1e-3) sys, prob = build_looped_adders(adder1, adder2) - sol = solve(prob, Tsit5(); reltol = 1e-8) + sol = solve(prob, Tsit5(); reltol = 1e-8, initializealg = SciMLBase.OverrideInit(nlsolve = FastShortcutNLLSPolyalg(autodiff = AutoFiniteDiff()))) @test truesol(sol.t; idxs = [truesys.adder1.c, truesys.adder2.c]).u≈sol( sol.t; idxs = [sys.adder1.c, sys.adder2.c]).u rtol=1e-3 From 1f1dae7e744b924cfbc5d0afec0e71c17e5cc99e Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 27 Jan 2025 13:29:33 +0530 Subject: [PATCH 0671/2176] fix: fix `ReconstructInitializeprob` overriding guesses --- src/systems/nonlinear/initializesystem.jl | 24 +++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/src/systems/nonlinear/initializesystem.jl b/src/systems/nonlinear/initializesystem.jl index 87be4338b8..296078bf43 100644 --- a/src/systems/nonlinear/initializesystem.jl +++ b/src/systems/nonlinear/initializesystem.jl @@ -232,15 +232,31 @@ struct ReconstructInitializeprob end function ReconstructInitializeprob(srcsys::AbstractSystem, dstsys::AbstractSystem) - syms = [unknowns(dstsys); - reduce(vcat, reorder_parameters(dstsys, parameters(dstsys)); init = [])] + syms = reduce(vcat, reorder_parameters(dstsys, parameters(dstsys)); init = []) getter = getu(srcsys, syms) - setter = setsym_oop(dstsys, syms) + setter = setp_oop(dstsys, syms) return ReconstructInitializeprob(getter, setter) end function (rip::ReconstructInitializeprob)(srcvalp, dstvalp) - rip.setter(dstvalp, rip.getter(srcvalp)) + newp = rip.setter(dstvalp, rip.getter(srcvalp)) + if state_values(dstvalp) === nothing + return nothing, newp + end + T = eltype(state_values(srcvalp)) + if parameter_values(dstvalp) isa MTKParameters + if !isempty(newp.tunable) + T = promote_type(eltype(newp.tunable), T) + end + elseif !isempty(newp) + T = promote_type(eltype(newp), T) + end + if T == eltype(state_values(dstvalp)) + u0 = state_values(dstvalp) + else + u0 = T.(state_values(dstvalp)) + end + return u0, newp end struct InitializationSystemMetadata From 5ee8edb48bf9931cd02c6e87a66df8f122886bd9 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 27 Jan 2025 15:08:46 +0530 Subject: [PATCH 0672/2176] test: make `NonlinearSystem` parameter initializaiton test less temperamental --- test/initializationsystem.jl | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/initializationsystem.jl b/test/initializationsystem.jl index 0a4ef18038..def45a7abf 100644 --- a/test/initializationsystem.jl +++ b/test/initializationsystem.jl @@ -792,10 +792,10 @@ end @variables x=1.0 y=3.0 @parameters p q - @mtkbuild sys = NonlinearSystem( - [(x - p)^2 + (y - q)^3 ~ 0, x - q ~ 0]; defaults = [q => missing], + @named sys = NonlinearSystem( + [x + y - p ~ 0, x - q ~ 0]; defaults = [q => missing], guesses = [q => 1.0], initialization_eqs = [p^2 + q^2 + 2p * q ~ 0]) - + sys = complete(sys) for (probT, algs) in prob_alg_combinations prob = probT(sys, [], [p => 2.0]) @test prob.f.initialization_data !== nothing From ec386fe041595284c9ab44a0f54fe9a6c232155e Mon Sep 17 00:00:00 2001 From: vyudu Date: Mon, 27 Jan 2025 23:02:07 -0500 Subject: [PATCH 0673/2176] Refactor constraints --- src/ModelingToolkit.jl | 8 +- src/systems/abstractsystem.jl | 1 + src/systems/diffeqs/abstractodesystem.jl | 121 ++---- src/systems/diffeqs/bvpsystem.jl | 217 ++++++++++ src/systems/diffeqs/odesystem.jl | 86 +++- .../optimization/constraints_system.jl | 5 + test/bvproblem.jl | 373 +++++++++--------- test/odesystem.jl | 30 ++ 8 files changed, 568 insertions(+), 273 deletions(-) create mode 100644 src/systems/diffeqs/bvpsystem.jl diff --git a/src/ModelingToolkit.jl b/src/ModelingToolkit.jl index 10aba4d8a9..2d53c61401 100644 --- a/src/ModelingToolkit.jl +++ b/src/ModelingToolkit.jl @@ -150,6 +150,10 @@ include("systems/imperative_affect.jl") include("systems/callbacks.jl") include("systems/problem_utils.jl") +include("systems/optimization/constraints_system.jl") +include("systems/optimization/optimizationsystem.jl") +include("systems/optimization/modelingtoolkitize.jl") + include("systems/nonlinear/nonlinearsystem.jl") include("systems/nonlinear/homotopy_continuation.jl") include("systems/diffeqs/odesystem.jl") @@ -165,10 +169,6 @@ include("systems/discrete_system/discrete_system.jl") include("systems/jumps/jumpsystem.jl") -include("systems/optimization/constraints_system.jl") -include("systems/optimization/optimizationsystem.jl") -include("systems/optimization/modelingtoolkitize.jl") - include("systems/pde/pdesystem.jl") include("systems/sparsematrixclil.jl") diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index 168260ae69..7c0fe0285d 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -983,6 +983,7 @@ for prop in [:eqs :structure :op :constraints + :constraintsystem :controls :loss :bcs diff --git a/src/systems/diffeqs/abstractodesystem.jl b/src/systems/diffeqs/abstractodesystem.jl index cf6e0962fd..d0d6ed7937 100644 --- a/src/systems/diffeqs/abstractodesystem.jl +++ b/src/systems/diffeqs/abstractodesystem.jl @@ -827,6 +827,12 @@ function DiffEqBase.ODEProblem{iip, specialize}(sys::AbstractODESystem, u0map = if !iscomplete(sys) error("A completed system is required. Call `complete` or `structural_simplify` on the system before creating an `ODEProblem`") end + + if !isnothing(get_constraintsystem(sys)) + error("An ODESystem with constraints cannot be used to construct a regular ODEProblem. + Consider a BVProblem instead.") + end + f, u0, p = process_SciMLProblem(ODEFunction{iip, specialize}, sys, u0map, parammap; t = tspan !== nothing ? tspan[1] : tspan, check_length, warn_initialize_determined, eval_expression, eval_module, kwargs...) @@ -866,18 +872,23 @@ Create a boundary value problem from the [`ODESystem`](@ref). must have either an initial guess supplied using `guesses` or a fixed initial value specified using `u0map`. -`constraints` are used to specify boundary conditions to the ODESystem in the -form of equations. These values should specify values that state variables should +Boundary value conditions are supplied to ODESystems +in the form of a ConstraintsSystem. These equations +should specify values that state variables should take at specific points, as in `x(0.5) ~ 1`). More general constraints that should hold over the entire solution, such as `x(t)^2 + y(t)^2`, should be -specified as one of the equations used to build the `ODESystem`. Below is an example. +specified as one of the equations used to build the `ODESystem`. + +If an ODESystem without `constraints` is specified, it will be treated as an initial value problem. ```julia - @parameters g + @parameters g t_c = 0.5 @variables x(..) y(t) [state_priority = 10] λ(t) eqs = [D(D(x(t))) ~ λ * x(t) D(D(y)) ~ λ * y - g x(t)^2 + y^2 ~ 1] + cstr = [x(0.5) ~ 1] + @named cstrs = ConstraintsSystem(cstr, t) @mtkbuild pend = ODESystem(eqs, t) tspan = (0.0, 1.5) @@ -889,9 +900,7 @@ specified as one of the equations used to build the `ODESystem`. Below is an exa bvp = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}(pend, u0map, tspan, parammap; constraints, guesses, check_length = false) ``` -If no `constraints` are specified, the problem will be treated as an initial value problem. - -If the `ODESystem` has algebraic equations like `x(t)^2 + y(t)^2`, the resulting +If the `ODESystem` has algebraic equations, like `x(t)^2 + y(t)^2`, the resulting `BVProblem` must be solved using BVDAE solvers, such as Ascher. """ function SciMLBase.BVProblem(sys::AbstractODESystem, args...; kwargs...) @@ -916,7 +925,7 @@ end function SciMLBase.BVProblem{iip, specialize}(sys::AbstractODESystem, u0map = [], tspan = get_tspan(sys), parammap = DiffEqBase.NullParameters(); - constraints = nothing, guesses = Dict(), + guesses = Dict(), version = nothing, tgrad = false, callback = nothing, check_length = true, @@ -930,21 +939,14 @@ function SciMLBase.BVProblem{iip, specialize}(sys::AbstractODESystem, u0map = [] end !isnothing(callback) && error("BVP solvers do not support callbacks.") - has_alg_eqs(sys) && error("The BVProblem currently does not support ODESystems with algebraic equations.") # Remove this when the BVDAE solvers get updated, the codegen should work when it does. + has_alg_eqs(sys) && error("The BVProblem constructor currently does not support ODESystems with algebraic equations.") # Remove this when the BVDAE solvers get updated, the codegen should work when it does. - constraintsts = nothing - constraintps = nothing sts = unknowns(sys) ps = parameters(sys) - # Constraint validation if !isnothing(constraints) - constraints isa Equation || - constraints isa Vector{Equation} || - error("Constraints must be specified as an equation or a vector of equations.") - (length(constraints) + length(u0map) > length(sts)) && - error("The BVProblem is overdetermined. The total number of conditions (# constraints + # fixed initial values given by u0map) cannot exceed the total number of states.") + @warn "The BVProblem is overdetermined. The total number of conditions (# constraints + # fixed initial values given by u0map) exceeds the total number of states. The BVP solvers will default to doing a nonlinear least-squares optimization." end # ODESystems without algebraic equations should use both fixed values + guesses @@ -957,48 +959,25 @@ function SciMLBase.BVProblem{iip, specialize}(sys::AbstractODESystem, u0map = [] stidxmap = Dict([v => i for (i, v) in enumerate(sts)]) u0_idxs = has_alg_eqs(sys) ? collect(1:length(sts)) : [stidxmap[k] for (k,v) in u0map] - bc = process_constraints(sys, constraints, u0, u0_idxs, tspan, iip) - + bc = generate_function_bc(sys, u0, u0_idxs, tspan, iip) return BVProblem{iip}(f, bc, u0, tspan, p; kwargs...) end get_callback(prob::BVProblem) = error("BVP solvers do not support callbacks.") -# Validate that all the variables in the BVP constraints are well-formed states or parameters. -function validate_constraint_syms(eq, constraintsts, constraintps, sts, ps, iv) - for var in constraintsts - if length(arguments(var)) > 1 - error("Too many arguments for variable $var.") - elseif isequal(arguments(var)[1], iv) - var ∈ sts || error("Constraint equation $eq contains a variable $var that is not a variable of the ODESystem.") - error("Constraint equation $eq contains a variable $var that does not have a specified argument. Such equations should be specified as algebraic equations to the ODESystem rather than a boundary constraints.") - else - operation(var)(iv) ∈ sts || error("Constraint equation $eq contains a variable $(operation(var)) that is not a variable of the ODESystem.") - end - end - - for var in constraintps - if !iscall(var) - var ∈ ps || error("Constraint equation $eq contains a parameter $var that is not a parameter of the ODESystem.") - else - length(arguments(var)) > 1 && error("Too many arguments for parameter $var.") - operation(var) ∈ ps || error("Constraint equations contain a parameter $var that is not a parameter of the ODESystem.") - end - end -end - """ - process_constraints(sys, constraints, u0, tspan, iip) + generate_function_bc(sys::ODESystem, u0, u0_idxs, tspan, iip) - Given an ODESystem with some constraints, generate the boundary condition function. + Given an ODESystem with constraints, generate the boundary condition function to pass to boundary value problem solvers. """ -function process_constraints(sys::ODESystem, constraints, u0, u0_idxs, tspan, iip) - +function generate_function_bc(sys::ODESystem, u0, u0_idxs, tspan, iip) iv = get_iv(sys) sts = get_unknowns(sys) ps = get_ps(sys) np = length(ps) ns = length(sts) + conssys = get_constraintsystem(sys) + cons = constraints(conssys) stidxmap = Dict([v => i for (i, v) in enumerate(sts)]) pidxmap = Dict([v => i for (i, v) in enumerate(ps)]) @@ -1006,48 +985,34 @@ function process_constraints(sys::ODESystem, constraints, u0, u0_idxs, tspan, ii @variables sol(..)[1:ns] p[1:np] exprs = Any[] - constraintsts = OrderedSet() - constraintps = OrderedSet() + for st in get_unknowns(cons) + x = operation(st) + t = first(arguments(st)) + idx = stidxmap[x(iv)] - !isnothing(constraints) && for cons in constraints - collect_vars!(constraintsts, constraintps, cons, iv) - validate_constraint_syms(cons, constraintsts, constraintps, Set(sts), Set(ps), iv) - expr = cons.rhs - cons.lhs - - for st in constraintsts - x = operation(st) - t = arguments(st)[1] - idx = stidxmap[x(iv)] - - expr = Symbolics.substitute(expr, Dict(x(t) => sol(t)[idx])) - end + cons = Symbolics.substitute(cons, Dict(x(t) => sol(t)[idx])) + end - for var in constraintps - if iscall(var) - x = operation(var) - t = arguments(var)[1] - idx = pidxmap[x] + for var in get_parameters(cons) + if iscall(var) + x = operation(var) + t = arguments(var)[1] + idx = pidxmap[x] - expr = Symbolics.substitute(expr, Dict(x(t) => p[idx])) - else - idx = pidxmap[var] - expr = Symbolics.substitute(expr, Dict(var => p[idx])) - end + cons = Symbolics.substitute(cons, Dict(x(t) => p[idx])) + else + idx = pidxmap[var] + cons = Symbolics.substitute(cons, Dict(var => p[idx])) end - - empty!(constraintsts) - empty!(constraintps) - push!(exprs, expr) end - init_cond_exprs = Any[] - + init_conds = Any[] for i in u0_idxs expr = sol(tspan[1])[i] - u0[i] - push!(init_cond_exprs, expr) + push!(init_conds, expr) end - exprs = vcat(init_cond_exprs, exprs) + exprs = vcat(init_conds, cons) bcs = Symbolics.build_function(exprs, sol, p, expression = Val{false}) if iip return (resid, u, p, t) -> bcs[2](resid, u, p) diff --git a/src/systems/diffeqs/bvpsystem.jl b/src/systems/diffeqs/bvpsystem.jl new file mode 100644 index 0000000000..ae3029fa14 --- /dev/null +++ b/src/systems/diffeqs/bvpsystem.jl @@ -0,0 +1,217 @@ +""" +$(TYPEDEF) + +A system of ordinary differential equations. + +# Fields +$(FIELDS) + +# Example + +```julia +using ModelingToolkit +using ModelingToolkit: t_nounits as t, D_nounits as D + +@parameters σ ρ β +@variables x(t) y(t) z(t) + +eqs = [D(x) ~ σ*(y-x), + D(y) ~ x*(ρ-z)-y, + D(z) ~ x*y - β*z] + +@named de = ODESystem(eqs,t,[x,y,z],[σ,ρ,β],tspan=(0, 1000.0)) +``` +""" +struct ODESystem <: AbstractODESystem + """ + A tag for the system. If two systems have the same tag, then they are + structurally identical. + """ + tag::UInt + """The ODEs defining the system.""" + eqs::Vector{Equation} + """Independent variable.""" + iv::BasicSymbolic{Real} + """ + Dependent (unknown) variables. Must not contain the independent variable. + + N.B.: If `torn_matching !== nothing`, this includes all variables. Actual + ODE unknowns are determined by the `SelectedState()` entries in `torn_matching`. + """ + unknowns::Vector + """Parameter variables. Must not contain the independent variable.""" + ps::Vector + """Time span.""" + tspan::Union{NTuple{2, Any}, Nothing} + """Array variables.""" + var_to_name::Any + """Control parameters (some subset of `ps`).""" + ctrls::Vector + """Observed variables.""" + observed::Vector{Equation} + """ + Time-derivative matrix. Note: this field will not be defined until + [`calculate_tgrad`](@ref) is called on the system. + """ + tgrad::RefValue{Vector{Num}} + """ + Jacobian matrix. Note: this field will not be defined until + [`calculate_jacobian`](@ref) is called on the system. + """ + jac::RefValue{Any} + """ + Control Jacobian matrix. Note: this field will not be defined until + [`calculate_control_jacobian`](@ref) is called on the system. + """ + ctrl_jac::RefValue{Any} + """ + Note: this field will not be defined until + [`generate_factorized_W`](@ref) is called on the system. + """ + Wfact::RefValue{Matrix{Num}} + """ + Note: this field will not be defined until + [`generate_factorized_W`](@ref) is called on the system. + """ + Wfact_t::RefValue{Matrix{Num}} + """ + The name of the system. + """ + name::Symbol + """ + A description of the system. + """ + description::String + """ + The internal systems. These are required to have unique names. + """ + systems::Vector{ODESystem} + """ + The default values to use when initial conditions and/or + parameters are not supplied in `ODEProblem`. + """ + defaults::Dict + """ + The guesses to use as the initial conditions for the + initialization system. + """ + guesses::Dict + """ + Tearing result specifying how to solve the system. + """ + torn_matching::Union{Matching, Nothing} + """ + The system for performing the initialization. + """ + initializesystem::Union{Nothing, NonlinearSystem} + """ + Extra equations to be enforced during the initialization sequence. + """ + initialization_eqs::Vector{Equation} + """ + The schedule for the code generation process. + """ + schedule::Any + """ + Type of the system. + """ + connector_type::Any + """ + Inject assignment statements before the evaluation of the RHS function. + """ + preface::Any + """ + A `Vector{SymbolicContinuousCallback}` that model events. + The integrator will use root finding to guarantee that it steps at each zero crossing. + """ + continuous_events::Vector{SymbolicContinuousCallback} + """ + A `Vector{SymbolicDiscreteCallback}` that models events. Symbolic + analog to `SciMLBase.DiscreteCallback` that executes an affect when a given condition is + true at the end of an integration step. + """ + discrete_events::Vector{SymbolicDiscreteCallback} + """ + Topologically sorted parameter dependency equations, where all symbols are parameters and + the LHS is a single parameter. + """ + parameter_dependencies::Vector{Equation} + """ + Metadata for the system, to be used by downstream packages. + """ + metadata::Any + """ + Metadata for MTK GUI. + """ + gui_metadata::Union{Nothing, GUIMetadata} + """ + A boolean indicating if the given `ODESystem` represents a system of DDEs. + """ + is_dde::Bool + """ + A list of points to provide to the solver as tstops. Uses the same syntax as discrete + events. + """ + tstops::Vector{Any} + """ + Cache for intermediate tearing state. + """ + tearing_state::Any + """ + Substitutions generated by tearing. + """ + substitutions::Any + """ + If a model `sys` is complete, then `sys.x` no longer performs namespacing. + """ + complete::Bool + """ + Cached data for fast symbolic indexing. + """ + index_cache::Union{Nothing, IndexCache} + """ + A list of discrete subsystems. + """ + discrete_subsystems::Any + """ + A list of actual unknowns needed to be solved by solvers. + """ + solved_unknowns::Union{Nothing, Vector{Any}} + """ + A vector of vectors of indices for the split parameters. + """ + split_idxs::Union{Nothing, Vector{Vector{Int}}} + """ + The hierarchical parent system before simplification. + """ + parent::Any + + function ODESystem(tag, deqs, iv, dvs, ps, tspan, var_to_name, ctrls, observed, tgrad, + jac, ctrl_jac, Wfact, Wfact_t, name, description, systems, defaults, guesses, + torn_matching, initializesystem, initialization_eqs, schedule, + connector_type, preface, cevents, + devents, parameter_dependencies, + metadata = nothing, gui_metadata = nothing, is_dde = false, + tstops = [], tearing_state = nothing, + substitutions = nothing, complete = false, index_cache = nothing, + discrete_subsystems = nothing, solved_unknowns = nothing, + split_idxs = nothing, parent = nothing; checks::Union{Bool, Int} = true) + if checks == true || (checks & CheckComponents) > 0 + check_independent_variables([iv]) + check_variables(dvs, iv) + check_parameters(ps, iv) + check_equations(deqs, iv) + check_equations(equations(cevents), iv) + end + if checks == true || (checks & CheckUnits) > 0 + u = __get_unit_type(dvs, ps, iv) + check_units(u, deqs) + end + new(tag, deqs, iv, dvs, ps, tspan, var_to_name, ctrls, observed, tgrad, jac, + ctrl_jac, Wfact, Wfact_t, name, description, systems, defaults, guesses, torn_matching, + initializesystem, initialization_eqs, schedule, connector_type, preface, + cevents, devents, parameter_dependencies, metadata, + gui_metadata, is_dde, tstops, tearing_state, substitutions, complete, index_cache, + discrete_subsystems, solved_unknowns, split_idxs, parent) + end +end diff --git a/src/systems/diffeqs/odesystem.jl b/src/systems/diffeqs/odesystem.jl index 61f16fd926..6ca6cdf838 100644 --- a/src/systems/diffeqs/odesystem.jl +++ b/src/systems/diffeqs/odesystem.jl @@ -49,6 +49,8 @@ struct ODESystem <: AbstractODESystem ctrls::Vector """Observed variables.""" observed::Vector{Equation} + """System of constraints that must be satisfied by the solution to the system.""" + constraintsystem::Union{Nothing, ConstraintsSystem} """ Time-derivative matrix. Note: this field will not be defined until [`calculate_tgrad`](@ref) is called on the system. @@ -186,7 +188,7 @@ struct ODESystem <: AbstractODESystem """ parent::Any - function ODESystem(tag, deqs, iv, dvs, ps, tspan, var_to_name, ctrls, observed, tgrad, + function ODESystem(tag, deqs, iv, dvs, ps, tspan, var_to_name, ctrls, observed, constraints, tgrad, jac, ctrl_jac, Wfact, Wfact_t, name, description, systems, defaults, guesses, torn_matching, initializesystem, initialization_eqs, schedule, connector_type, preface, cevents, @@ -207,7 +209,7 @@ struct ODESystem <: AbstractODESystem u = __get_unit_type(dvs, ps, iv) check_units(u, deqs) end - new(tag, deqs, iv, dvs, ps, tspan, var_to_name, ctrls, observed, tgrad, jac, + new(tag, deqs, iv, dvs, ps, tspan, var_to_name, ctrls, observed, constraints, tgrad, jac, ctrl_jac, Wfact, Wfact_t, name, description, systems, defaults, guesses, torn_matching, initializesystem, initialization_eqs, schedule, connector_type, preface, cevents, devents, parameter_dependencies, metadata, @@ -219,6 +221,7 @@ end function ODESystem(deqs::AbstractVector{<:Equation}, iv, dvs, ps; controls = Num[], observed = Equation[], + constraints = Equation[], systems = ODESystem[], tspan = nothing, name = nothing, @@ -283,11 +286,26 @@ function ODESystem(deqs::AbstractVector{<:Equation}, iv, dvs, ps; cont_callbacks = SymbolicContinuousCallbacks(continuous_events) disc_callbacks = SymbolicDiscreteCallbacks(discrete_events) + constraintsys = nothing + if !isempty(constraints) + constraintsys = process_constraint_system(constraints, dvs′, ps′, iv, systems) + dvset = Set(dvs′) + pset = Set(ps′) + for st in get_unknowns(constraintsys) + iscall(st) ? + !in(operation(st)(iv), dvset) && push!(dvs′, st) : + !in(st, dvset) && push!(dvs′, st) + end + for p in parameters(constraintsys) + !in(p, pset) && push!(ps′, p) + end + end + if is_dde === nothing is_dde = _check_if_dde(deqs, iv′, systems) end ODESystem(Threads.atomic_add!(SYSTEM_COUNT, UInt(1)), - deqs, iv′, dvs′, ps′, tspan, var_to_name, ctrl′, observed, tgrad, jac, + deqs, iv′, dvs′, ps′, tspan, var_to_name, ctrl′, observed, constraintsys, tgrad, jac, ctrl_jac, Wfact, Wfact_t, name, description, systems, defaults, guesses, nothing, initializesystem, initialization_eqs, schedule, connector_type, preface, cont_callbacks, @@ -295,7 +313,7 @@ function ODESystem(deqs::AbstractVector{<:Equation}, iv, dvs, ps; metadata, gui_metadata, is_dde, tstops, checks = checks) end -function ODESystem(eqs, iv; kwargs...) +function ODESystem(eqs, iv; constraints = Equation[], kwargs...) eqs = collect(eqs) # NOTE: this assumes that the order of algebraic equations doesn't matter diffvars = OrderedSet() @@ -358,9 +376,10 @@ function ODESystem(eqs, iv; kwargs...) end end algevars = setdiff(allunknowns, diffvars) + # the orders here are very important! return ODESystem(Equation[diffeq; algeeq; compressed_eqs], iv, - collect(Iterators.flatten((diffvars, algevars))), collect(new_ps); kwargs...) + collect(Iterators.flatten((diffvars, algevars))), collect(new_ps); constraints, kwargs...) end # NOTE: equality does not check cached Jacobian @@ -770,3 +789,60 @@ function Base.show(io::IO, mime::MIME"text/plain", sys::ODESystem; hint = true, return nothing end + +# Validate that all the variables in the BVP constraints are well-formed states or parameters. +# - Any callable with multiple arguments will error. +# - Callable/delay variables (e.g. of the form x(0.6) should be unknowns of the system (and have one arg, etc.) +# - Callable/delay parameters should be parameters of the system (and have one arg, etc.) +function validate_constraint_syms(constraintsts, constraintps, sts, ps, iv) + for var in constraintsts + if !iscall(var) + occursin(iv, var) && var ∈ sts || throw(ArgumentError("Time-dependent variable $var is not an unknown of the system.")) + elseif length(arguments(var)) > 1 + throw(ArgumentError("Too many arguments for variable $var.")) + elseif length(arguments(var)) == 1 + arg = first(arguments(var)) + operation(var)(iv) ∈ sts || throw(ArgumentError("Variable $var is not a variable of the ODESystem. Called variables must be variables of the ODESystem.")) + + isequal(arg, iv) || + isparameter(arg) || + arg isa Integer || + arg isa AbstractFloat || + throw(ArgumentError("Invalid argument specified for variable $var. The argument of the variable should be either $iv, a parameter, or a value specifying the time that the constraint holds.")) + else + var ∈ sts && @warn "Variable $var has no argument. It will be interpreted as $var($iv), and the constraint will apply to the entire interval." + end + end + + for var in constraintps + !iscall(var) && continue + + if length(arguments(var)) > 1 + throw(ArgumentError("Too many arguments for parameter $var in equation $eq.")) + elseif length(arguments(var)) == 1 + arg = first(arguments(var)) + operation(var) ∈ ps || throw(ArgumentError("Parameter $var is not a parameter of the ODESystem. Called parameters must be parameters of the ODESystem.")) + + isequal(arg, iv) || + isparameter(arg) || + arg isa Integer || + arg isa AbstractFloat || + throw(ArgumentError("Invalid argument specified for callable parameter $var. The argument of the parameter should be either $iv, a parameter, or a value specifying the time that the constraint holds.")) + end + end +end + +function process_constraint_system(constraints::Vector{Equation}, sts, ps, iv, subsys::Vector{ODESystem}; name = :cons) + isempty(constraints) && return nothing + + constraintsts = OrderedSet() + constraintps = OrderedSet() + + for cons in constraints + syms = collect_vars!(constraintsts, constraintps, cons, iv) + end + validate_constraint_syms(constraintsts, constraintps, Set(sts), Set(ps), iv) + + constraint_subsys = get_constraintsystem.(subsys) + ConstraintsSystem(constraints, collect(constraintsts), collect(constraintps); systems = constraint_subsys, name) +end diff --git a/src/systems/optimization/constraints_system.jl b/src/systems/optimization/constraints_system.jl index 03225fc900..61e190e672 100644 --- a/src/systems/optimization/constraints_system.jl +++ b/src/systems/optimization/constraints_system.jl @@ -89,6 +89,10 @@ struct ConstraintsSystem <: AbstractTimeIndependentSystem tearing_state = nothing, substitutions = nothing, complete = false, index_cache = nothing; checks::Union{Bool, Int} = true) + + ##if checks == true || (checks & CheckComponents) > 0 + ## check_variables(unknowns, constraints) + ##end if checks == true || (checks & CheckUnits) > 0 u = __get_unit_type(unknowns, ps) check_units(u, constraints) @@ -253,3 +257,4 @@ function get_cmap(sys::ConstraintsSystem, exprs = nothing) cmap = map(x -> x ~ getdefault(x), cs) return cmap, cs end + diff --git a/test/bvproblem.jl b/test/bvproblem.jl index 2f278e6135..16c12d1be6 100644 --- a/test/bvproblem.jl +++ b/test/bvproblem.jl @@ -8,163 +8,163 @@ using ModelingToolkit: t_nounits as t, D_nounits as D import ModelingToolkit: process_constraints ### Test Collocation solvers on simple problems -solvers = [MIRK4, RadauIIa5, LobattoIIIa3] +solvers = [MIRK4] daesolvers = [Ascher2, Ascher4, Ascher6] -# let -# @parameters α=7.5 β=4.0 γ=8.0 δ=5.0 -# @variables x(t)=1.0 y(t)=2.0 -# -# eqs = [D(x) ~ α * x - β * x * y, -# D(y) ~ -γ * y + δ * x * y] -# -# u0map = [x => 1.0, y => 2.0] -# parammap = [α => 7.5, β => 4, γ => 8.0, δ => 5.0] -# tspan = (0.0, 10.0) -# -# @mtkbuild lotkavolterra = ODESystem(eqs, t) -# op = ODEProblem(lotkavolterra, u0map, tspan, parammap) -# osol = solve(op, Vern9()) -# -# bvp = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}( -# lotkavolterra, u0map, tspan, parammap; eval_expression = true) -# -# for solver in solvers -# sol = solve(bvp, solver(), dt = 0.01) -# @test isapprox(sol.u[end], osol.u[end]; atol = 0.01) -# @test sol.u[1] == [1.0, 2.0] -# end -# -# # Test out of place -# bvp2 = SciMLBase.BVProblem{false, SciMLBase.AutoSpecialize}( -# lotkavolterra, u0map, tspan, parammap; eval_expression = true) -# -# for solver in solvers -# sol = solve(bvp2, solver(), dt = 0.01) -# @test isapprox(sol.u[end], osol.u[end]; atol = 0.01) -# @test sol.u[1] == [1.0, 2.0] -# end -# end -# -# ### Testing on pendulum -# let -# @parameters g=9.81 L=1.0 -# @variables θ(t) = π / 2 θ_t(t) -# -# eqs = [D(θ) ~ θ_t -# D(θ_t) ~ -(g / L) * sin(θ)] -# -# @mtkbuild pend = ODESystem(eqs, t) -# -# u0map = [θ => π / 2, θ_t => π / 2] -# parammap = [:L => 1.0, :g => 9.81] -# tspan = (0.0, 6.0) -# -# op = ODEProblem(pend, u0map, tspan, parammap) -# osol = solve(op, Vern9()) -# -# bvp = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}(pend, u0map, tspan, parammap) -# for solver in solvers -# sol = solve(bvp, solver(), dt = 0.01) -# @test isapprox(sol.u[end], osol.u[end]; atol = 0.01) -# @test sol.u[1] == [π / 2, π / 2] -# end -# -# # Test out-of-place -# bvp2 = SciMLBase.BVProblem{false, SciMLBase.FullSpecialize}(pend, u0map, tspan, parammap) -# -# for solver in solvers -# sol = solve(bvp2, solver(), dt = 0.01) -# @test isapprox(sol.u[end], osol.u[end]; atol = 0.01) -# @test sol.u[1] == [π / 2, π / 2] -# end -# end -# -# ################################################################## -# ### ODESystem with constraint equations, DAEs with constraints ### -# ################################################################## -# -# # Test generation of boundary condition function using `process_constraints`. Compare solutions to manually written boundary conditions -# let -# @parameters α=1.5 β=1.0 γ=3.0 δ=1.0 -# @variables x(..) y(..) -# eqs = [D(x(t)) ~ α * x(t) - β * x(t) * y(t), -# D(y(t)) ~ -γ * y(t) + δ * x(t) * y(t)] -# -# tspan = (0., 1.) -# @mtkbuild lksys = ODESystem(eqs, t) -# -# function lotkavolterra!(du, u, p, t) -# du[1] = p[1]*u[1] - p[2]*u[1]*u[2] -# du[2] = -p[4]*u[2] + p[3]*u[1]*u[2] -# end -# -# function lotkavolterra(u, p, t) -# [p[1]*u[1] - p[2]*u[1]*u[2], -p[4]*u[2] + p[3]*u[1]*u[2]] -# end -# # Compare the built bc function to the actual constructed one. -# function bc!(resid, u, p, t) -# resid[1] = u[1][1] - 1. -# resid[2] = u[1][2] - 2. -# nothing -# end -# function bc(u, p, t) -# [u[1][1] - 1., u[1][2] - 2.] -# end -# -# u0 = [1., 2.]; p = [1.5, 1., 1., 3.] -# genbc_iip = ModelingToolkit.process_constraints(lksys, nothing, u0, [1, 2], tspan, true) -# genbc_oop = ModelingToolkit.process_constraints(lksys, nothing, u0, [1, 2], tspan, false) -# -# bvpi1 = SciMLBase.BVProblem(lotkavolterra!, bc!, [1.,2.], tspan, p) -# bvpi2 = SciMLBase.BVProblem(lotkavolterra!, genbc_iip, [1.,2.], tspan, p) -# -# sol1 = solve(bvpi1, MIRK4(), dt = 0.05) -# sol2 = solve(bvpi2, MIRK4(), dt = 0.05) -# @test sol1 ≈ sol2 -# -# bvpo1 = BVProblem(lotkavolterra, bc, [1,2], tspan, p) -# bvpo2 = BVProblem(lotkavolterra, genbc_oop, [1,2], tspan, p) -# -# sol1 = solve(bvpo1, MIRK4(), dt = 0.05) -# sol2 = solve(bvpo2, MIRK4(), dt = 0.05) -# @test sol1 ≈ sol2 -# -# # Test with a constraint. -# constraints = [y(0.5) ~ 2.] -# -# function bc!(resid, u, p, t) -# resid[1] = u(0.0)[1] - 1. -# resid[2] = u(0.5)[2] - 2. -# end -# function bc(u, p, t) -# [u(0.0)[1] - 1., u(0.5)[2] - 2.] -# end -# -# u0 = [1, 1.] -# genbc_iip = ModelingToolkit.process_constraints(lksys, constraints, u0, [1], tspan, true) -# genbc_oop = ModelingToolkit.process_constraints(lksys, constraints, u0, [1], tspan, false) -# -# bvpi1 = SciMLBase.BVProblem(lotkavolterra!, bc!, u0, tspan, p) -# bvpi2 = SciMLBase.BVProblem(lotkavolterra!, genbc_iip, u0, tspan, p) -# bvpi3 = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}(lksys, [x(t) => 1.], tspan; guesses = [y(t) => 1.], constraints) -# bvpi4 = SciMLBase.BVProblem{true, SciMLBase.FullSpecialize}(lksys, [x(t) => 1.], tspan; guesses = [y(t) => 1.], constraints) -# -# sol1 = @btime solve($bvpi1, MIRK4(), dt = 0.01) -# sol2 = @btime solve($bvpi2, MIRK4(), dt = 0.01) -# sol3 = @btime solve($bvpi3, MIRK4(), dt = 0.01) -# sol4 = @btime solve($bvpi4, MIRK4(), dt = 0.01) -# @test sol1 ≈ sol2 ≈ sol3 ≈ sol4 # don't get true equality here, not sure why -# -# bvpo1 = BVProblem(lotkavolterra, bc, u0, tspan, p) -# bvpo2 = BVProblem(lotkavolterra, genbc_oop, u0, tspan, p) -# bvpo3 = SciMLBase.BVProblem{false, SciMLBase.FullSpecialize}(lksys, [x(t) => 1.], tspan; guesses = [y(t) => 1.], constraints) -# -# sol1 = @btime solve($bvpo1, MIRK4(), dt = 0.05) -# sol2 = @btime solve($bvpo2, MIRK4(), dt = 0.05) -# sol3 = @btime solve($bvpo3, MIRK4(), dt = 0.05) -# @test sol1 ≈ sol2 ≈ sol3 -# end +let + @parameters α=7.5 β=4.0 γ=8.0 δ=5.0 + @variables x(t)=1.0 y(t)=2.0 + + eqs = [D(x) ~ α * x - β * x * y, + D(y) ~ -γ * y + δ * x * y] + + u0map = [x => 1.0, y => 2.0] + parammap = [α => 7.5, β => 4, γ => 8.0, δ => 5.0] + tspan = (0.0, 10.0) + + @mtkbuild lotkavolterra = ODESystem(eqs, t) + op = ODEProblem(lotkavolterra, u0map, tspan, parammap) + osol = solve(op, Vern9()) + + bvp = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}( + lotkavolterra, u0map, tspan, parammap; eval_expression = true) + + for solver in solvers + sol = solve(bvp, solver(), dt = 0.01) + @test isapprox(sol.u[end], osol.u[end]; atol = 0.01) + @test sol.u[1] == [1.0, 2.0] + end + + # Test out of place + bvp2 = SciMLBase.BVProblem{false, SciMLBase.AutoSpecialize}( + lotkavolterra, u0map, tspan, parammap; eval_expression = true) + + for solver in solvers + sol = solve(bvp2, solver(), dt = 0.01) + @test isapprox(sol.u[end], osol.u[end]; atol = 0.01) + @test sol.u[1] == [1.0, 2.0] + end +end + +### Testing on pendulum +let + @parameters g=9.81 L=1.0 + @variables θ(t) = π / 2 θ_t(t) + + eqs = [D(θ) ~ θ_t + D(θ_t) ~ -(g / L) * sin(θ)] + + @mtkbuild pend = ODESystem(eqs, t) + + u0map = [θ => π / 2, θ_t => π / 2] + parammap = [:L => 1.0, :g => 9.81] + tspan = (0.0, 6.0) + + op = ODEProblem(pend, u0map, tspan, parammap) + osol = solve(op, Vern9()) + + bvp = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}(pend, u0map, tspan, parammap) + for solver in solvers + sol = solve(bvp, solver(), dt = 0.01) + @test isapprox(sol.u[end], osol.u[end]; atol = 0.01) + @test sol.u[1] == [π / 2, π / 2] + end + + # Test out-of-place + bvp2 = SciMLBase.BVProblem{false, SciMLBase.FullSpecialize}(pend, u0map, tspan, parammap) + + for solver in solvers + sol = solve(bvp2, solver(), dt = 0.01) + @test isapprox(sol.u[end], osol.u[end]; atol = 0.01) + @test sol.u[1] == [π / 2, π / 2] + end +end + +################################################################## +### ODESystem with constraint equations, DAEs with constraints ### +################################################################## + +# Test generation of boundary condition function using `generate_function_bc`. Compare solutions to manually written boundary conditions +let + @parameters α=1.5 β=1.0 γ=3.0 δ=1.0 + @variables x(..) y(..) + eqs = [D(x(t)) ~ α * x(t) - β * x(t) * y(t), + D(y(t)) ~ -γ * y(t) + δ * x(t) * y(t)] + + tspan = (0., 1.) + @mtkbuild lksys = ODESystem(eqs, t) + + function lotkavolterra!(du, u, p, t) + du[1] = p[1]*u[1] - p[2]*u[1]*u[2] + du[2] = -p[4]*u[2] + p[3]*u[1]*u[2] + end + + function lotkavolterra(u, p, t) + [p[1]*u[1] - p[2]*u[1]*u[2], -p[4]*u[2] + p[3]*u[1]*u[2]] + end + # Compare the built bc function to the actual constructed one. + function bc!(resid, u, p, t) + resid[1] = u[1][1] - 1. + resid[2] = u[1][2] - 2. + nothing + end + function bc(u, p, t) + [u[1][1] - 1., u[1][2] - 2.] + end + + u0 = [1., 2.]; p = [1.5, 1., 1., 3.] + genbc_iip = ModelingToolkit.generate_function_bc(lksys, nothing, u0, [1, 2], tspan, true) + genbc_oop = ModelingToolkit.generate_function_bc(lksys, nothing, u0, [1, 2], tspan, false) + + bvpi1 = SciMLBase.BVProblem(lotkavolterra!, bc!, [1.,2.], tspan, p) + bvpi2 = SciMLBase.BVProblem(lotkavolterra!, genbc_iip, [1.,2.], tspan, p) + + sol1 = solve(bvpi1, MIRK4(), dt = 0.05) + sol2 = solve(bvpi2, MIRK4(), dt = 0.05) + @test sol1 ≈ sol2 + + bvpo1 = BVProblem(lotkavolterra, bc, [1,2], tspan, p) + bvpo2 = BVProblem(lotkavolterra, genbc_oop, [1,2], tspan, p) + + sol1 = solve(bvpo1, MIRK4(), dt = 0.05) + sol2 = solve(bvpo2, MIRK4(), dt = 0.05) + @test sol1 ≈ sol2 + + # Test with a constraint. + constraints = [y(0.5) ~ 2.] + + function bc!(resid, u, p, t) + resid[1] = u(0.0)[1] - 1. + resid[2] = u(0.5)[2] - 2. + end + function bc(u, p, t) + [u(0.0)[1] - 1., u(0.5)[2] - 2.] + end + + u0 = [1, 1.] + genbc_iip = ModelingToolkit.generate_function_bc(lksys, constraints, u0, [1], tspan, true) + genbc_oop = ModelingToolkit.generate_function_bc(lksys, constraints, u0, [1], tspan, false) + + bvpi1 = SciMLBase.BVProblem(lotkavolterra!, bc!, u0, tspan, p) + bvpi2 = SciMLBase.BVProblem(lotkavolterra!, genbc_iip, u0, tspan, p) + bvpi3 = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}(lksys, [x(t) => 1.], tspan; guesses = [y(t) => 1.], constraints) + bvpi4 = SciMLBase.BVProblem{true, SciMLBase.FullSpecialize}(lksys, [x(t) => 1.], tspan; guesses = [y(t) => 1.], constraints) + + sol1 = @btime solve($bvpi1, MIRK4(), dt = 0.01) + sol2 = @btime solve($bvpi2, MIRK4(), dt = 0.01) + sol3 = @btime solve($bvpi3, MIRK4(), dt = 0.01) + sol4 = @btime solve($bvpi4, MIRK4(), dt = 0.01) + @test sol1 ≈ sol2 ≈ sol3 ≈ sol4 # don't get true equality here, not sure why + + bvpo1 = BVProblem(lotkavolterra, bc, u0, tspan, p) + bvpo2 = BVProblem(lotkavolterra, genbc_oop, u0, tspan, p) + bvpo3 = SciMLBase.BVProblem{false, SciMLBase.FullSpecialize}(lksys, [x(t) => 1.], tspan; guesses = [y(t) => 1.], constraints) + + sol1 = @btime solve($bvpo1, MIRK4(), dt = 0.05) + sol2 = @btime solve($bvpo2, MIRK4(), dt = 0.05) + sol3 = @btime solve($bvpo3, MIRK4(), dt = 0.05) + @test sol1 ≈ sol2 ≈ sol3 +end function test_solvers(solvers, prob, u0map, constraints, equations = []; dt = 0.05, atol = 1e-3) for solver in solvers @@ -214,33 +214,32 @@ let u0map = [] tspan = (0.0, 1.0) guesses = [x(t) => 4.0, y(t) => 2.] + constr = [x(.6) ~ 3.5, x(.3) ~ 7.] + @mtkbuild lksys = ODESystem(eqs, t; constraints = constr) - @mtkbuild lksys = ODESystem(eqs, t) - - constraints = [x(.6) ~ 3.5, x(.3) ~ 7.] - bvp = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}(lksys, u0map, tspan; guesses, constraints) - test_solvers(solvers, bvp, u0map, constraints; dt = 0.05) + bvp = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}(lksys, u0map, tspan; guesses) + test_solvers(solvers, bvp, u0map, constr; dt = 0.05) - # Testing that more complicated constraints give correct solutions. - constraints = [y(.2) + x(.8) ~ 3., y(.3) ~ 2.] - bvp = SciMLBase.BVProblem{false, SciMLBase.FullSpecialize}(lksys, u0map, tspan; guesses, constraints) - test_solvers(solvers, bvp, u0map, constraints; dt = 0.05) + # Testing that more complicated constr give correct solutions. + constr = [y(.2) + x(.8) ~ 3., y(.3) ~ 2.] + bvp = SciMLBase.BVProblem{false, SciMLBase.FullSpecialize}(lksys, u0map, tspan; guesses) + test_solvers(solvers, bvp, u0map, constr; dt = 0.05) - constraints = [α * β - x(.6) ~ 0.0, y(.2) ~ 3.] - bvp = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}(lksys, u0map, tspan; guesses, constraints) - test_solvers(solvers, bvp, u0map, constraints) + constr = [α * β - x(.6) ~ 0.0, y(.2) ~ 3.] + bvp = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}(lksys, u0map, tspan; guesses) + test_solvers(solvers, bvp, u0map, constr) - # Testing that errors are properly thrown when malformed constraints are given. + # Testing that errors are properly thrown when malformed constr are given. @variables bad(..) - constraints = [x(1.) + bad(3.) ~ 10] - @test_throws ErrorException bvp = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}(lksys, u0map, tspan; guesses, constraints) + constr = [x(1.) + bad(3.) ~ 10] + @test_throws ErrorException lksys = ODESystem(eqs, t; constraints = constr) - constraints = [x(t) + y(t) ~ 3] - @test_throws ErrorException bvp = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}(lksys, u0map, tspan; guesses, constraints) + constr = [x(t) + y(t) ~ 3] + @test_throws ErrorException lksys = ODESystem(eqs, t; constraints = constr) @parameters bad2 - constraints = [bad2 + x(0.) ~ 3] - @test_throws ErrorException bvp = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}(lksys, u0map, tspan; guesses, constraints) + constr = [bad2 + x(0.) ~ 3] + @test_throws ErrorException lksys = ODESystem(eqs, t; constraints = constr) end # Cartesian pendulum from the docs. @@ -288,31 +287,33 @@ end # eqs = [D(D(x(t))) ~ λ * x(t) # D(D(y)) ~ λ * y - g # x(t)^2 + y^2 ~ 1] -# @mtkbuild pend = ODESystem(eqs, t) +# constr = [x(0.5) ~ 1] +# @mtkbuild pend = ODESystem(eqs, t; constr) # # tspan = (0.0, 1.5) # u0map = [x(t) => 0.6, y => 0.8] # parammap = [g => 1] # guesses = [λ => 1] # -# constraints = [x(0.5) ~ 1] -# bvp = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}(pend, u0map, tspan, parammap; constraints, guesses, check_length = false) -# test_solvers(daesolvers, bvp, u0map, constraints) +# bvp = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}(pend, u0map, tspan, parammap; guesses, check_length = false) +# test_solvers(daesolvers, bvp, u0map, constr) # # bvp2 = SciMLBase.BVProblem{false, SciMLBase.FullSpecialize}(pend, u0map, tspan, parammap) -# test_solvers(daesolvers, bvp2, u0map, constraints, get_alg_eqs(pend)) +# test_solvers(daesolvers, bvp2, u0map, constr, get_alg_eqs(pend)) # -# # More complicated constraints. +# # More complicated constr. # u0map = [x(t) => 0.6] # guesses = [λ => 1, y(t) => 0.8] # -# constraints = [x(0.5) ~ 1, +# constr = [x(0.5) ~ 1, # x(0.3)^3 + y(0.6)^2 ~ 0.5] -# bvp = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}(pend, u0map, tspan, parammap; constraints, guesses, check_length = false) -# test_solvers(daesolvers, bvp, u0map, constraints, get_alg_eqs(pend)) +# @mtkbuild pend = ODESystem(eqs, t; constr) +# bvp = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}(pend, u0map, tspan, parammap; guesses, check_length = false) +# test_solvers(daesolvers, bvp, u0map, constr, get_alg_eqs(pend)) # -# constraints = [x(0.4) * g ~ y(0.2), +# constr = [x(0.4) * g ~ y(0.2), # y(0.7) ~ 0.3] -# bvp = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}(pend, u0map, tspan, parammap; constraints, guesses, check_length = false) -# test_solvers(daesolvers, bvp, u0map, constraints, get_alg_eqs(pend)) +# @mtkbuild pend = ODESystem(eqs, t; constr) +# bvp = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}(pend, u0map, tspan, parammap; guesses, check_length = false) +# test_solvers(daesolvers, bvp, u0map, constr, get_alg_eqs(pend)) # end diff --git a/test/odesystem.jl b/test/odesystem.jl index 85d135b338..516fa7d415 100644 --- a/test/odesystem.jl +++ b/test/odesystem.jl @@ -1552,3 +1552,33 @@ end expected_tstops = unique!(sort!(vcat(0.0:0.075:10.0, 0.1, 0.2, 0.65, 0.35, 0.45))) @test all(x -> any(isapprox(x, atol = 1e-6), sol2.t), expected_tstops) end + +@testset "Constraint system construction" begin + @variables x(..) y(..) z(..) + @parameters a b c d e + eqs = [D(x(t)) ~ 3*a*y(t), D(y(t)) ~ x(t) - z(t), D(z(t)) ~ e*x(t)^2] + cons = [x(0.3) ~ c*d, y(0.7) ~ 3] + + # Test variables + parameters infer correctly. + @mtkbuild sys = ODESystem(eqs, t; constraints = cons) + @test issetequal(parameters(sys), [a, c, d, e]) + @test issetequal(unknowns(sys), [x(t), y(t)]) + + @parameters t_c + cons = [x(t_c) ~ 3] + @mtkbuild sys = ODESystem(eqs, t; constraints = cons) + @test_broken issetequal(parameters(sys), [a, e, t_c]) # TODO: unbreak this. + + # Test that bad constraints throw errors. + cons = [x(3, 4) ~ 3] + @test_throws ArgumentError @mtkbuild sys = ODESystem(eqs, t; constraints = cons) + + cons = [x(y(t)) ~ 2] + @test_throws ArgumentError @mtkbuild sys = ODESystem(eqs, t; constraints = cons) + + @variables u(t) v + cons = [x(t) * u ~ 3] + @test_throws ArgumentError @mtkbuild sys = ODESystem(eqs, t; constraints = cons) + cons = [x(t) * v ~ 3] + @test_nowarn @mtkbuild sys = ODESystem(eqs, t; constraints = cons) +end From 90ce80d658eabd599b99c9e5c9bbc5efc9d41470 Mon Sep 17 00:00:00 2001 From: vyudu Date: Tue, 28 Jan 2025 11:57:37 -0500 Subject: [PATCH 0674/2176] refactor tests --- src/systems/diffeqs/abstractodesystem.jl | 45 +++++++++++++----------- src/systems/diffeqs/odesystem.jl | 11 +++--- test/bvproblem.jl | 38 ++++++-------------- 3 files changed, 39 insertions(+), 55 deletions(-) diff --git a/src/systems/diffeqs/abstractodesystem.jl b/src/systems/diffeqs/abstractodesystem.jl index d0d6ed7937..47e5c3b88c 100644 --- a/src/systems/diffeqs/abstractodesystem.jl +++ b/src/systems/diffeqs/abstractodesystem.jl @@ -943,9 +943,10 @@ function SciMLBase.BVProblem{iip, specialize}(sys::AbstractODESystem, u0map = [] sts = unknowns(sys) ps = parameters(sys) + constraintsys = get_constraintsystem(sys) - if !isnothing(constraints) - (length(constraints) + length(u0map) > length(sts)) && + if !isnothing(constraintsys) + (length(constraints(constraintsys)) + length(u0map) > length(sts)) && @warn "The BVProblem is overdetermined. The total number of conditions (# constraints + # fixed initial values given by u0map) exceeds the total number of states. The BVP solvers will default to doing a nonlinear least-squares optimization." end @@ -976,33 +977,35 @@ function generate_function_bc(sys::ODESystem, u0, u0_idxs, tspan, iip) ps = get_ps(sys) np = length(ps) ns = length(sts) - conssys = get_constraintsystem(sys) - cons = constraints(conssys) - stidxmap = Dict([v => i for (i, v) in enumerate(sts)]) pidxmap = Dict([v => i for (i, v) in enumerate(ps)]) @variables sol(..)[1:ns] p[1:np] - exprs = Any[] - for st in get_unknowns(cons) - x = operation(st) - t = first(arguments(st)) - idx = stidxmap[x(iv)] + conssys = get_constraintsystem(sys) + cons = Any[] + if !isnothing(conssys) + cons = [con.lhs - con.rhs for con in constraints(conssys)] - cons = Symbolics.substitute(cons, Dict(x(t) => sol(t)[idx])) - end + for st in get_unknowns(conssys) + x = operation(st) + t = only(arguments(st)) + idx = stidxmap[x(iv)] - for var in get_parameters(cons) - if iscall(var) - x = operation(var) - t = arguments(var)[1] - idx = pidxmap[x] + cons = map(c -> Symbolics.substitute(c, Dict(x(t) => sol(t)[idx])), cons) + end - cons = Symbolics.substitute(cons, Dict(x(t) => p[idx])) - else - idx = pidxmap[var] - cons = Symbolics.substitute(cons, Dict(var => p[idx])) + for var in parameters(conssys) + if iscall(var) + x = operation(var) + t = only(arguments(var)) + idx = pidxmap[x] + + cons = map(c -> Symbolics.substitute(c, Dict(x(t) => p[idx])), cons) + else + idx = pidxmap[var] + cons = map(c -> Symbolics.substitute(c, Dict(var => p[idx])), cons) + end end end diff --git a/src/systems/diffeqs/odesystem.jl b/src/systems/diffeqs/odesystem.jl index 6ca6cdf838..4ef9f89c6a 100644 --- a/src/systems/diffeqs/odesystem.jl +++ b/src/systems/diffeqs/odesystem.jl @@ -802,13 +802,11 @@ function validate_constraint_syms(constraintsts, constraintps, sts, ps, iv) throw(ArgumentError("Too many arguments for variable $var.")) elseif length(arguments(var)) == 1 arg = first(arguments(var)) - operation(var)(iv) ∈ sts || throw(ArgumentError("Variable $var is not a variable of the ODESystem. Called variables must be variables of the ODESystem.")) + operation(var)(iv) ∈ sts || + throw(ArgumentError("Variable $var is not a variable of the ODESystem. Called variables must be variables of the ODESystem.")) - isequal(arg, iv) || - isparameter(arg) || - arg isa Integer || - arg isa AbstractFloat || - throw(ArgumentError("Invalid argument specified for variable $var. The argument of the variable should be either $iv, a parameter, or a value specifying the time that the constraint holds.")) + isequal(arg, iv) || isparameter(arg) || arg isa Integer || arg isa AbstractFloat || + throw(ArgumentError("Invalid argument specified for variable $var. The argument of the variable should be either $iv, a parameter, or a value specifying the time that the constraint holds.")) else var ∈ sts && @warn "Variable $var has no argument. It will be interpreted as $var($iv), and the constraint will apply to the entire interval." end @@ -824,7 +822,6 @@ function validate_constraint_syms(constraintsts, constraintps, sts, ps, iv) operation(var) ∈ ps || throw(ArgumentError("Parameter $var is not a parameter of the ODESystem. Called parameters must be parameters of the ODESystem.")) isequal(arg, iv) || - isparameter(arg) || arg isa Integer || arg isa AbstractFloat || throw(ArgumentError("Invalid argument specified for callable parameter $var. The argument of the parameter should be either $iv, a parameter, or a value specifying the time that the constraint holds.")) diff --git a/test/bvproblem.jl b/test/bvproblem.jl index 16c12d1be6..c6c124d09e 100644 --- a/test/bvproblem.jl +++ b/test/bvproblem.jl @@ -5,7 +5,6 @@ using BenchmarkTools using ModelingToolkit using SciMLBase using ModelingToolkit: t_nounits as t, D_nounits as D -import ModelingToolkit: process_constraints ### Test Collocation solvers on simple problems solvers = [MIRK4] @@ -113,8 +112,8 @@ let end u0 = [1., 2.]; p = [1.5, 1., 1., 3.] - genbc_iip = ModelingToolkit.generate_function_bc(lksys, nothing, u0, [1, 2], tspan, true) - genbc_oop = ModelingToolkit.generate_function_bc(lksys, nothing, u0, [1, 2], tspan, false) + genbc_iip = ModelingToolkit.generate_function_bc(lksys, u0, [1, 2], tspan, true) + genbc_oop = ModelingToolkit.generate_function_bc(lksys, u0, [1, 2], tspan, false) bvpi1 = SciMLBase.BVProblem(lotkavolterra!, bc!, [1.,2.], tspan, p) bvpi2 = SciMLBase.BVProblem(lotkavolterra!, genbc_iip, [1.,2.], tspan, p) @@ -131,7 +130,8 @@ let @test sol1 ≈ sol2 # Test with a constraint. - constraints = [y(0.5) ~ 2.] + constr = [y(0.5) ~ 2.] + @mtkbuild lksys = ODESystem(eqs, t; constraints = constr) function bc!(resid, u, p, t) resid[1] = u(0.0)[1] - 1. @@ -142,13 +142,13 @@ let end u0 = [1, 1.] - genbc_iip = ModelingToolkit.generate_function_bc(lksys, constraints, u0, [1], tspan, true) - genbc_oop = ModelingToolkit.generate_function_bc(lksys, constraints, u0, [1], tspan, false) + genbc_iip = ModelingToolkit.generate_function_bc(lksys, u0, [1], tspan, true) + genbc_oop = ModelingToolkit.generate_function_bc(lksys, u0, [1], tspan, false) bvpi1 = SciMLBase.BVProblem(lotkavolterra!, bc!, u0, tspan, p) bvpi2 = SciMLBase.BVProblem(lotkavolterra!, genbc_iip, u0, tspan, p) - bvpi3 = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}(lksys, [x(t) => 1.], tspan; guesses = [y(t) => 1.], constraints) - bvpi4 = SciMLBase.BVProblem{true, SciMLBase.FullSpecialize}(lksys, [x(t) => 1.], tspan; guesses = [y(t) => 1.], constraints) + bvpi3 = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}(lksys, [x(t) => 1.], tspan; guesses = [y(t) => 1.]) + bvpi4 = SciMLBase.BVProblem{true, SciMLBase.FullSpecialize}(lksys, [x(t) => 1.], tspan; guesses = [y(t) => 1.]) sol1 = @btime solve($bvpi1, MIRK4(), dt = 0.01) sol2 = @btime solve($bvpi2, MIRK4(), dt = 0.01) @@ -158,7 +158,7 @@ let bvpo1 = BVProblem(lotkavolterra, bc, u0, tspan, p) bvpo2 = BVProblem(lotkavolterra, genbc_oop, u0, tspan, p) - bvpo3 = SciMLBase.BVProblem{false, SciMLBase.FullSpecialize}(lksys, [x(t) => 1.], tspan; guesses = [y(t) => 1.], constraints) + bvpo3 = SciMLBase.BVProblem{false, SciMLBase.FullSpecialize}(lksys, [x(t) => 1.], tspan; guesses = [y(t) => 1.]) sol1 = @btime solve($bvpo1, MIRK4(), dt = 0.05) sol2 = @btime solve($bvpo2, MIRK4(), dt = 0.05) @@ -197,12 +197,6 @@ function test_solvers(solvers, prob, u0map, constraints, equations = []; dt = 0. end end -solvers = [RadauIIa3, RadauIIa5, RadauIIa7, - LobattoIIIa2, LobattoIIIa4, LobattoIIIa5, - LobattoIIIb2, LobattoIIIb3, LobattoIIIb4, LobattoIIIb5, - LobattoIIIc2, LobattoIIIc3, LobattoIIIc4, LobattoIIIc5] -weird = [MIRK2, MIRK5, RadauIIa2] -daesolvers = [] # Simple ODESystem with BVP constraints. let @parameters α=1.5 β=1.0 γ=3.0 δ=1.0 @@ -222,24 +216,14 @@ let # Testing that more complicated constr give correct solutions. constr = [y(.2) + x(.8) ~ 3., y(.3) ~ 2.] + @mtkbuild lksys = ODESystem(eqs, t; constraints = constr) bvp = SciMLBase.BVProblem{false, SciMLBase.FullSpecialize}(lksys, u0map, tspan; guesses) test_solvers(solvers, bvp, u0map, constr; dt = 0.05) constr = [α * β - x(.6) ~ 0.0, y(.2) ~ 3.] + @mtkbuild lksys = ODESystem(eqs, t; constraints = constr) bvp = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}(lksys, u0map, tspan; guesses) test_solvers(solvers, bvp, u0map, constr) - - # Testing that errors are properly thrown when malformed constr are given. - @variables bad(..) - constr = [x(1.) + bad(3.) ~ 10] - @test_throws ErrorException lksys = ODESystem(eqs, t; constraints = constr) - - constr = [x(t) + y(t) ~ 3] - @test_throws ErrorException lksys = ODESystem(eqs, t; constraints = constr) - - @parameters bad2 - constr = [bad2 + x(0.) ~ 3] - @test_throws ErrorException lksys = ODESystem(eqs, t; constraints = constr) end # Cartesian pendulum from the docs. From a15c67024d4630193afe0f662d7c4c4183d07037 Mon Sep 17 00:00:00 2001 From: vyudu Date: Tue, 28 Jan 2025 14:23:08 -0500 Subject: [PATCH 0675/2176] fix sym validation --- src/systems/diffeqs/odesystem.jl | 91 ++++++++++++++------------------ test/odesystem.jl | 15 ++++-- 2 files changed, 49 insertions(+), 57 deletions(-) diff --git a/src/systems/diffeqs/odesystem.jl b/src/systems/diffeqs/odesystem.jl index 4ef9f89c6a..0c8304e428 100644 --- a/src/systems/diffeqs/odesystem.jl +++ b/src/systems/diffeqs/odesystem.jl @@ -221,7 +221,7 @@ end function ODESystem(deqs::AbstractVector{<:Equation}, iv, dvs, ps; controls = Num[], observed = Equation[], - constraints = Equation[], + constraintsystem = nothing, systems = ODESystem[], tspan = nothing, name = nothing, @@ -286,26 +286,17 @@ function ODESystem(deqs::AbstractVector{<:Equation}, iv, dvs, ps; cont_callbacks = SymbolicContinuousCallbacks(continuous_events) disc_callbacks = SymbolicDiscreteCallbacks(discrete_events) - constraintsys = nothing - if !isempty(constraints) - constraintsys = process_constraint_system(constraints, dvs′, ps′, iv, systems) - dvset = Set(dvs′) - pset = Set(ps′) - for st in get_unknowns(constraintsys) - iscall(st) ? - !in(operation(st)(iv), dvset) && push!(dvs′, st) : - !in(st, dvset) && push!(dvs′, st) - end - for p in parameters(constraintsys) - !in(p, pset) && push!(ps′, p) - end - end - if is_dde === nothing is_dde = _check_if_dde(deqs, iv′, systems) end + + if !isempty(systems) + cons = get_constraintsystems.(systems) + @set! constraintsystem.systems = cons + end + ODESystem(Threads.atomic_add!(SYSTEM_COUNT, UInt(1)), - deqs, iv′, dvs′, ps′, tspan, var_to_name, ctrl′, observed, constraintsys, tgrad, jac, + deqs, iv′, dvs′, ps′, tspan, var_to_name, ctrl′, observed, constraintsystem, tgrad, jac, ctrl_jac, Wfact, Wfact_t, name, description, systems, defaults, guesses, nothing, initializesystem, initialization_eqs, schedule, connector_type, preface, cont_callbacks, @@ -377,9 +368,22 @@ function ODESystem(eqs, iv; constraints = Equation[], kwargs...) end algevars = setdiff(allunknowns, diffvars) + if !isempty(constraints) + consvars = OrderedSet() + constraintsystem = process_constraint_system(constraints, allunknowns, new_ps, iv) + for st in get_unknowns(constraintsystem) + iscall(st) ? + !in(operation(st)(iv), allunknowns) && push!(consvars, st) : + !in(st, allunknowns) && push!(consvars, st) + end + for p in parameters(constraintsystem) + !in(p, new_ps) && push!(new_ps, p) + end + end + # the orders here are very important! return ODESystem(Equation[diffeq; algeeq; compressed_eqs], iv, - collect(Iterators.flatten((diffvars, algevars))), collect(new_ps); constraints, kwargs...) + collect(Iterators.flatten((diffvars, algevars, consvars))), collect(new_ps); constraintsystem, kwargs...) end # NOTE: equality does not check cached Jacobian @@ -791,55 +795,38 @@ function Base.show(io::IO, mime::MIME"text/plain", sys::ODESystem; hint = true, end # Validate that all the variables in the BVP constraints are well-formed states or parameters. -# - Any callable with multiple arguments will error. # - Callable/delay variables (e.g. of the form x(0.6) should be unknowns of the system (and have one arg, etc.) # - Callable/delay parameters should be parameters of the system (and have one arg, etc.) -function validate_constraint_syms(constraintsts, constraintps, sts, ps, iv) +function process_constraint_system(constraints::Vector{Equation}, sts, ps, iv; consname = :cons) + isempty(constraints) && return nothing + + constraintsts = OrderedSet() + constraintps = OrderedSet() + + # Hack? to extract parameters from callable variables in constraints. + for cons in constraints + collect_vars!(constraintsts, constraintps, cons, iv) + end + + # Validate the states. for var in constraintsts if !iscall(var) - occursin(iv, var) && var ∈ sts || throw(ArgumentError("Time-dependent variable $var is not an unknown of the system.")) + occursin(iv, var) && (var ∈ sts || throw(ArgumentError("Time-dependent variable $var is not an unknown of the system."))) elseif length(arguments(var)) > 1 throw(ArgumentError("Too many arguments for variable $var.")) elseif length(arguments(var)) == 1 - arg = first(arguments(var)) + arg = only(arguments(var)) operation(var)(iv) ∈ sts || throw(ArgumentError("Variable $var is not a variable of the ODESystem. Called variables must be variables of the ODESystem.")) isequal(arg, iv) || isparameter(arg) || arg isa Integer || arg isa AbstractFloat || throw(ArgumentError("Invalid argument specified for variable $var. The argument of the variable should be either $iv, a parameter, or a value specifying the time that the constraint holds.")) + + isparameter(arg) && push!(constraintps, arg) else var ∈ sts && @warn "Variable $var has no argument. It will be interpreted as $var($iv), and the constraint will apply to the entire interval." end end - for var in constraintps - !iscall(var) && continue - - if length(arguments(var)) > 1 - throw(ArgumentError("Too many arguments for parameter $var in equation $eq.")) - elseif length(arguments(var)) == 1 - arg = first(arguments(var)) - operation(var) ∈ ps || throw(ArgumentError("Parameter $var is not a parameter of the ODESystem. Called parameters must be parameters of the ODESystem.")) - - isequal(arg, iv) || - arg isa Integer || - arg isa AbstractFloat || - throw(ArgumentError("Invalid argument specified for callable parameter $var. The argument of the parameter should be either $iv, a parameter, or a value specifying the time that the constraint holds.")) - end - end -end - -function process_constraint_system(constraints::Vector{Equation}, sts, ps, iv, subsys::Vector{ODESystem}; name = :cons) - isempty(constraints) && return nothing - - constraintsts = OrderedSet() - constraintps = OrderedSet() - - for cons in constraints - syms = collect_vars!(constraintsts, constraintps, cons, iv) - end - validate_constraint_syms(constraintsts, constraintps, Set(sts), Set(ps), iv) - - constraint_subsys = get_constraintsystem.(subsys) - ConstraintsSystem(constraints, collect(constraintsts), collect(constraintps); systems = constraint_subsys, name) + ConstraintsSystem(constraints, collect(constraintsts), collect(constraintps); name = consname) end diff --git a/test/odesystem.jl b/test/odesystem.jl index 516fa7d415..0276bb4b4f 100644 --- a/test/odesystem.jl +++ b/test/odesystem.jl @@ -1562,23 +1562,28 @@ end # Test variables + parameters infer correctly. @mtkbuild sys = ODESystem(eqs, t; constraints = cons) @test issetequal(parameters(sys), [a, c, d, e]) - @test issetequal(unknowns(sys), [x(t), y(t)]) + @test issetequal(unknowns(sys), [x(t), y(t), z(t)]) @parameters t_c cons = [x(t_c) ~ 3] @mtkbuild sys = ODESystem(eqs, t; constraints = cons) - @test_broken issetequal(parameters(sys), [a, e, t_c]) # TODO: unbreak this. + @test issetequal(parameters(sys), [a, e, t_c]) + + @parameters g(..) h i + cons = [g(h, i) * x(3) ~ c] + @mtkbuild sys = ODESystem(eqs, t; constraints = cons) + @test issetequal(parameters(sys), [g, h, i, a, e, c]) # Test that bad constraints throw errors. - cons = [x(3, 4) ~ 3] + cons = [x(3, 4) ~ 3] # unknowns cannot have multiple args. @test_throws ArgumentError @mtkbuild sys = ODESystem(eqs, t; constraints = cons) - cons = [x(y(t)) ~ 2] + cons = [x(y(t)) ~ 2] # unknown arg must be parameter, value, or t @test_throws ArgumentError @mtkbuild sys = ODESystem(eqs, t; constraints = cons) @variables u(t) v cons = [x(t) * u ~ 3] @test_throws ArgumentError @mtkbuild sys = ODESystem(eqs, t; constraints = cons) cons = [x(t) * v ~ 3] - @test_nowarn @mtkbuild sys = ODESystem(eqs, t; constraints = cons) + @test_throws ArgumentError @mtkbuild sys = ODESystem(eqs, t; constraints = cons) # Need time argument. end From c6ef04adac961b386f0f37c4750eee28273cff3f Mon Sep 17 00:00:00 2001 From: vyudu Date: Tue, 28 Jan 2025 14:24:38 -0500 Subject: [PATCH 0676/2176] remove file --- src/systems/diffeqs/bvpsystem.jl | 217 ------------------------------- 1 file changed, 217 deletions(-) delete mode 100644 src/systems/diffeqs/bvpsystem.jl diff --git a/src/systems/diffeqs/bvpsystem.jl b/src/systems/diffeqs/bvpsystem.jl deleted file mode 100644 index ae3029fa14..0000000000 --- a/src/systems/diffeqs/bvpsystem.jl +++ /dev/null @@ -1,217 +0,0 @@ -""" -$(TYPEDEF) - -A system of ordinary differential equations. - -# Fields -$(FIELDS) - -# Example - -```julia -using ModelingToolkit -using ModelingToolkit: t_nounits as t, D_nounits as D - -@parameters σ ρ β -@variables x(t) y(t) z(t) - -eqs = [D(x) ~ σ*(y-x), - D(y) ~ x*(ρ-z)-y, - D(z) ~ x*y - β*z] - -@named de = ODESystem(eqs,t,[x,y,z],[σ,ρ,β],tspan=(0, 1000.0)) -``` -""" -struct ODESystem <: AbstractODESystem - """ - A tag for the system. If two systems have the same tag, then they are - structurally identical. - """ - tag::UInt - """The ODEs defining the system.""" - eqs::Vector{Equation} - """Independent variable.""" - iv::BasicSymbolic{Real} - """ - Dependent (unknown) variables. Must not contain the independent variable. - - N.B.: If `torn_matching !== nothing`, this includes all variables. Actual - ODE unknowns are determined by the `SelectedState()` entries in `torn_matching`. - """ - unknowns::Vector - """Parameter variables. Must not contain the independent variable.""" - ps::Vector - """Time span.""" - tspan::Union{NTuple{2, Any}, Nothing} - """Array variables.""" - var_to_name::Any - """Control parameters (some subset of `ps`).""" - ctrls::Vector - """Observed variables.""" - observed::Vector{Equation} - """ - Time-derivative matrix. Note: this field will not be defined until - [`calculate_tgrad`](@ref) is called on the system. - """ - tgrad::RefValue{Vector{Num}} - """ - Jacobian matrix. Note: this field will not be defined until - [`calculate_jacobian`](@ref) is called on the system. - """ - jac::RefValue{Any} - """ - Control Jacobian matrix. Note: this field will not be defined until - [`calculate_control_jacobian`](@ref) is called on the system. - """ - ctrl_jac::RefValue{Any} - """ - Note: this field will not be defined until - [`generate_factorized_W`](@ref) is called on the system. - """ - Wfact::RefValue{Matrix{Num}} - """ - Note: this field will not be defined until - [`generate_factorized_W`](@ref) is called on the system. - """ - Wfact_t::RefValue{Matrix{Num}} - """ - The name of the system. - """ - name::Symbol - """ - A description of the system. - """ - description::String - """ - The internal systems. These are required to have unique names. - """ - systems::Vector{ODESystem} - """ - The default values to use when initial conditions and/or - parameters are not supplied in `ODEProblem`. - """ - defaults::Dict - """ - The guesses to use as the initial conditions for the - initialization system. - """ - guesses::Dict - """ - Tearing result specifying how to solve the system. - """ - torn_matching::Union{Matching, Nothing} - """ - The system for performing the initialization. - """ - initializesystem::Union{Nothing, NonlinearSystem} - """ - Extra equations to be enforced during the initialization sequence. - """ - initialization_eqs::Vector{Equation} - """ - The schedule for the code generation process. - """ - schedule::Any - """ - Type of the system. - """ - connector_type::Any - """ - Inject assignment statements before the evaluation of the RHS function. - """ - preface::Any - """ - A `Vector{SymbolicContinuousCallback}` that model events. - The integrator will use root finding to guarantee that it steps at each zero crossing. - """ - continuous_events::Vector{SymbolicContinuousCallback} - """ - A `Vector{SymbolicDiscreteCallback}` that models events. Symbolic - analog to `SciMLBase.DiscreteCallback` that executes an affect when a given condition is - true at the end of an integration step. - """ - discrete_events::Vector{SymbolicDiscreteCallback} - """ - Topologically sorted parameter dependency equations, where all symbols are parameters and - the LHS is a single parameter. - """ - parameter_dependencies::Vector{Equation} - """ - Metadata for the system, to be used by downstream packages. - """ - metadata::Any - """ - Metadata for MTK GUI. - """ - gui_metadata::Union{Nothing, GUIMetadata} - """ - A boolean indicating if the given `ODESystem` represents a system of DDEs. - """ - is_dde::Bool - """ - A list of points to provide to the solver as tstops. Uses the same syntax as discrete - events. - """ - tstops::Vector{Any} - """ - Cache for intermediate tearing state. - """ - tearing_state::Any - """ - Substitutions generated by tearing. - """ - substitutions::Any - """ - If a model `sys` is complete, then `sys.x` no longer performs namespacing. - """ - complete::Bool - """ - Cached data for fast symbolic indexing. - """ - index_cache::Union{Nothing, IndexCache} - """ - A list of discrete subsystems. - """ - discrete_subsystems::Any - """ - A list of actual unknowns needed to be solved by solvers. - """ - solved_unknowns::Union{Nothing, Vector{Any}} - """ - A vector of vectors of indices for the split parameters. - """ - split_idxs::Union{Nothing, Vector{Vector{Int}}} - """ - The hierarchical parent system before simplification. - """ - parent::Any - - function ODESystem(tag, deqs, iv, dvs, ps, tspan, var_to_name, ctrls, observed, tgrad, - jac, ctrl_jac, Wfact, Wfact_t, name, description, systems, defaults, guesses, - torn_matching, initializesystem, initialization_eqs, schedule, - connector_type, preface, cevents, - devents, parameter_dependencies, - metadata = nothing, gui_metadata = nothing, is_dde = false, - tstops = [], tearing_state = nothing, - substitutions = nothing, complete = false, index_cache = nothing, - discrete_subsystems = nothing, solved_unknowns = nothing, - split_idxs = nothing, parent = nothing; checks::Union{Bool, Int} = true) - if checks == true || (checks & CheckComponents) > 0 - check_independent_variables([iv]) - check_variables(dvs, iv) - check_parameters(ps, iv) - check_equations(deqs, iv) - check_equations(equations(cevents), iv) - end - if checks == true || (checks & CheckUnits) > 0 - u = __get_unit_type(dvs, ps, iv) - check_units(u, deqs) - end - new(tag, deqs, iv, dvs, ps, tspan, var_to_name, ctrls, observed, tgrad, jac, - ctrl_jac, Wfact, Wfact_t, name, description, systems, defaults, guesses, torn_matching, - initializesystem, initialization_eqs, schedule, connector_type, preface, - cevents, devents, parameter_dependencies, metadata, - gui_metadata, is_dde, tstops, tearing_state, substitutions, complete, index_cache, - discrete_subsystems, solved_unknowns, split_idxs, parent) - end -end From 78782256b5f3c3f82ae1869b2d6737ea7a4b7acc Mon Sep 17 00:00:00 2001 From: vyudu Date: Tue, 28 Jan 2025 14:26:12 -0500 Subject: [PATCH 0677/2176] up --- src/systems/optimization/constraints_system.jl | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/systems/optimization/constraints_system.jl b/src/systems/optimization/constraints_system.jl index 61e190e672..3c2aed192d 100644 --- a/src/systems/optimization/constraints_system.jl +++ b/src/systems/optimization/constraints_system.jl @@ -90,9 +90,6 @@ struct ConstraintsSystem <: AbstractTimeIndependentSystem complete = false, index_cache = nothing; checks::Union{Bool, Int} = true) - ##if checks == true || (checks & CheckComponents) > 0 - ## check_variables(unknowns, constraints) - ##end if checks == true || (checks & CheckUnits) > 0 u = __get_unit_type(unknowns, ps) check_units(u, constraints) From 0493b5d2b49e72872976caf2e9c57381e272557a Mon Sep 17 00:00:00 2001 From: vyudu Date: Tue, 28 Jan 2025 14:28:46 -0500 Subject: [PATCH 0678/2176] remove lines --- src/systems/optimization/constraints_system.jl | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/systems/optimization/constraints_system.jl b/src/systems/optimization/constraints_system.jl index 3c2aed192d..03225fc900 100644 --- a/src/systems/optimization/constraints_system.jl +++ b/src/systems/optimization/constraints_system.jl @@ -89,7 +89,6 @@ struct ConstraintsSystem <: AbstractTimeIndependentSystem tearing_state = nothing, substitutions = nothing, complete = false, index_cache = nothing; checks::Union{Bool, Int} = true) - if checks == true || (checks & CheckUnits) > 0 u = __get_unit_type(unknowns, ps) check_units(u, constraints) @@ -254,4 +253,3 @@ function get_cmap(sys::ConstraintsSystem, exprs = nothing) cmap = map(x -> x ~ getdefault(x), cs) return cmap, cs end - From 1d32b6ea8438bda25dc91e095458c5f377c801c0 Mon Sep 17 00:00:00 2001 From: vyudu Date: Tue, 28 Jan 2025 14:50:42 -0500 Subject: [PATCH 0679/2176] up --- src/systems/diffeqs/odesystem.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/systems/diffeqs/odesystem.jl b/src/systems/diffeqs/odesystem.jl index 8d0e09fce5..1154bcd4c8 100644 --- a/src/systems/diffeqs/odesystem.jl +++ b/src/systems/diffeqs/odesystem.jl @@ -368,8 +368,8 @@ function ODESystem(eqs, iv; constraints = Equation[], kwargs...) end algevars = setdiff(allunknowns, diffvars) + consvars = OrderedSet() if !isempty(constraints) - consvars = OrderedSet() constraintsystem = process_constraint_system(constraints, allunknowns, new_ps, iv) for st in get_unknowns(constraintsystem) iscall(st) ? From 2b3ca96d23f0010609867f6586449aa7dd9c6ddb Mon Sep 17 00:00:00 2001 From: vyudu Date: Tue, 28 Jan 2025 15:05:02 -0500 Subject: [PATCH 0680/2176] up --- src/systems/diffeqs/odesystem.jl | 1 + 1 file changed, 1 insertion(+) diff --git a/src/systems/diffeqs/odesystem.jl b/src/systems/diffeqs/odesystem.jl index 1154bcd4c8..1754079745 100644 --- a/src/systems/diffeqs/odesystem.jl +++ b/src/systems/diffeqs/odesystem.jl @@ -369,6 +369,7 @@ function ODESystem(eqs, iv; constraints = Equation[], kwargs...) algevars = setdiff(allunknowns, diffvars) consvars = OrderedSet() + constraintsystem = nothing if !isempty(constraints) constraintsystem = process_constraint_system(constraints, allunknowns, new_ps, iv) for st in get_unknowns(constraintsystem) From 0324522a419219c8f59f1b584c1ccd65454dc794 Mon Sep 17 00:00:00 2001 From: vyudu Date: Tue, 28 Jan 2025 15:05:57 -0500 Subject: [PATCH 0681/2176] fix typo --- src/systems/diffeqs/odesystem.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/systems/diffeqs/odesystem.jl b/src/systems/diffeqs/odesystem.jl index 1754079745..2ed7155f6c 100644 --- a/src/systems/diffeqs/odesystem.jl +++ b/src/systems/diffeqs/odesystem.jl @@ -291,7 +291,7 @@ function ODESystem(deqs::AbstractVector{<:Equation}, iv, dvs, ps; end if !isempty(systems) - cons = get_constraintsystems.(systems) + cons = get_constraintsystem.(systems) @set! constraintsystem.systems = cons end From 2a079becdd97c10431ad8a85f707adbb39519e59 Mon Sep 17 00:00:00 2001 From: vyudu Date: Tue, 28 Jan 2025 15:46:23 -0500 Subject: [PATCH 0682/2176] Fix setter --- src/systems/diffeqs/odesystem.jl | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/systems/diffeqs/odesystem.jl b/src/systems/diffeqs/odesystem.jl index 2ed7155f6c..dac0878d26 100644 --- a/src/systems/diffeqs/odesystem.jl +++ b/src/systems/diffeqs/odesystem.jl @@ -290,9 +290,14 @@ function ODESystem(deqs::AbstractVector{<:Equation}, iv, dvs, ps; is_dde = _check_if_dde(deqs, iv′, systems) end - if !isempty(systems) - cons = get_constraintsystem.(systems) - @set! constraintsystem.systems = cons + if !isempty(systems) && !isnothing(constraintsystem) + conssystems = ConstraintsSystem[] + for sys in systems + cons = get_constraintsystem(sys) + cons !== nothing && push!(conssystems, cons) + end + @show conssystems + @set! constraintsystem.systems = conssystems end ODESystem(Threads.atomic_add!(SYSTEM_COUNT, UInt(1)), From d70a470d546e9c79417fb4c0869956cd1800de00 Mon Sep 17 00:00:00 2001 From: vyudu Date: Tue, 28 Jan 2025 16:21:54 -0500 Subject: [PATCH 0683/2176] fix --- test/odesystem.jl | 1 + 1 file changed, 1 insertion(+) diff --git a/test/odesystem.jl b/test/odesystem.jl index 963847731c..53f75bf377 100644 --- a/test/odesystem.jl +++ b/test/odesystem.jl @@ -1660,3 +1660,4 @@ end @test_throws ArgumentError @mtkbuild sys = ODESystem(eqs, t; constraints = cons) cons = [x(t) * v ~ 3] @test_throws ArgumentError @mtkbuild sys = ODESystem(eqs, t; constraints = cons) # Need time argument. +end From 1b7261a05c98d65218fc748524b6cc443f9160a4 Mon Sep 17 00:00:00 2001 From: Christopher Rackauckas Date: Wed, 29 Jan 2025 05:42:54 -0500 Subject: [PATCH 0684/2176] Update Project.toml --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 38d2b7057a..e4b2238170 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "ModelingToolkit" uuid = "961ee093-0014-501f-94e3-6117800e7a78" authors = ["Yingbo Ma ", "Chris Rackauckas and contributors"] -version = "9.61.0" +version = "9.62.0" [deps] AbstractTrees = "1520ce14-60c1-5f80-bbc7-55ef81b5835c" From eea66d053b5b0ad52c180d9662165e0060a3345c Mon Sep 17 00:00:00 2001 From: vyudu Date: Wed, 29 Jan 2025 09:47:01 -0500 Subject: [PATCH 0685/2176] up --- src/utils.jl | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/utils.jl b/src/utils.jl index a83ba2fde7..f956c90a67 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -156,11 +156,11 @@ function check_variables(dvs, iv) end function check_var_types(sys_type::Type{T}, dvs) where T <: AbstractSystem - if any(u -> !(symtype(u) <: Number || eltype(symtype(u)) <: Number), dvs) - error("The type of unknown variables must be a numeric type.") - elseif any(u -> (eltype(symtype(u)) !== eltype(symtype(dvs[1]))), dvs) - error("The element type of all the unknown variables in a system must all be the same.") - elseif sys_type == ODESystem || sys_type == SDESystem || sys_type == PDESystem + # if any(u -> !(symtype(u) <: Number || eltype(symtype(u)) <: Number), dvs) + # error("The type of unknown variables must be a numeric type.") + # elseif any(u -> (eltype(symtype(u)) !== eltype(symtype(dvs[1]))), dvs) + # error("The element type of all the unknown variables in a system must all be the same.") + if sys_type == ODESystem || sys_type == SDESystem || sys_type == PDESystem any(u -> !(symtype(u) == Real || eltype(symtype(u)) == Real), dvs) && error("The type of unknown variables for an SDESystem, PDESystem, or ODESystem should not be a concrete numeric type.") end nothing From a9b7fa143b2b3dcfcab5b266d2131b479daf83ce Mon Sep 17 00:00:00 2001 From: vyudu Date: Wed, 29 Jan 2025 10:42:10 -0500 Subject: [PATCH 0686/2176] up --- src/systems/diffeqs/odesystem.jl | 2 ++ src/utils.jl | 5 +++-- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/systems/diffeqs/odesystem.jl b/src/systems/diffeqs/odesystem.jl index 8edbb46cf1..4a3a172d60 100644 --- a/src/systems/diffeqs/odesystem.jl +++ b/src/systems/diffeqs/odesystem.jl @@ -329,6 +329,8 @@ function ODESystem(eqs, iv; kwargs...) throw(ArgumentError("The differential variable $diffvar is not unique in the system of equations.")) push!(diffvars, diffvar) end + !(symtype(diffvar) === Real || eltype(symtype(var)) === Real) && throw(ArgumentError("Differential variable $var has type $(symtype(var)). Differential variables should not be concretely typed.")) + push!(diffeq, eq) else push!(algeeq, eq) diff --git a/src/utils.jl b/src/utils.jl index 8691111b01..34058818c8 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -161,9 +161,10 @@ function check_var_types(sys_type::Type{T}, dvs) where T <: AbstractSystem # elseif any(u -> (eltype(symtype(u)) !== eltype(symtype(dvs[1]))), dvs) # error("The element type of all the unknown variables in a system must all be the same.") if sys_type == ODESystem || sys_type == SDESystem || sys_type == PDESystem - any(u -> !(symtype(u) == Real || eltype(symtype(u)) == Real), dvs) && error("The type of unknown variables for an SDESystem, PDESystem, or ODESystem should not be a concrete numeric type.") + for var in dvs + !(symtype(var) === Real || eltype(symtype(var)) === Real) && throw(ArgumentError("Differential variable $var has type $(symtype(var)). The type of unknown variables for an SDESystem, PDESystem, or ODESystem should be Real.")) + end end - nothing end function check_lhs(eq::Equation, op, dvs::Set) From 68bb1b3a3543bc1c3b340cb0bd9d72941de7d9b7 Mon Sep 17 00:00:00 2001 From: vyudu Date: Wed, 29 Jan 2025 11:53:30 -0500 Subject: [PATCH 0687/2176] up --- src/systems/diffeqs/odesystem.jl | 1 - src/systems/diffeqs/sdesystem.jl | 1 - src/utils.jl | 12 ------------ 3 files changed, 14 deletions(-) diff --git a/src/systems/diffeqs/odesystem.jl b/src/systems/diffeqs/odesystem.jl index 4a3a172d60..32169c1ecd 100644 --- a/src/systems/diffeqs/odesystem.jl +++ b/src/systems/diffeqs/odesystem.jl @@ -202,7 +202,6 @@ struct ODESystem <: AbstractODESystem check_parameters(ps, iv) check_equations(deqs, iv) check_equations(equations(cevents), iv) - check_var_types(ODESystem, dvs) end if checks == true || (checks & CheckUnits) > 0 u = __get_unit_type(dvs, ps, iv) diff --git a/src/systems/diffeqs/sdesystem.jl b/src/systems/diffeqs/sdesystem.jl index 69e984d4ce..df950ee01c 100644 --- a/src/systems/diffeqs/sdesystem.jl +++ b/src/systems/diffeqs/sdesystem.jl @@ -170,7 +170,6 @@ struct SDESystem <: AbstractODESystem check_parameters(ps, iv) check_equations(deqs, iv) check_equations(neqs, dvs) - check_var_types(SDESystem, dvs) if size(neqs, 1) != length(deqs) throw(ArgumentError("Noise equations ill-formed. Number of rows must match number of drift equations. size(neqs,1) = $(size(neqs,1)) != length(deqs) = $(length(deqs))")) end diff --git a/src/utils.jl b/src/utils.jl index 34058818c8..29b9e424ac 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -155,18 +155,6 @@ function check_variables(dvs, iv) end end -function check_var_types(sys_type::Type{T}, dvs) where T <: AbstractSystem - # if any(u -> !(symtype(u) <: Number || eltype(symtype(u)) <: Number), dvs) - # error("The type of unknown variables must be a numeric type.") - # elseif any(u -> (eltype(symtype(u)) !== eltype(symtype(dvs[1]))), dvs) - # error("The element type of all the unknown variables in a system must all be the same.") - if sys_type == ODESystem || sys_type == SDESystem || sys_type == PDESystem - for var in dvs - !(symtype(var) === Real || eltype(symtype(var)) === Real) && throw(ArgumentError("Differential variable $var has type $(symtype(var)). The type of unknown variables for an SDESystem, PDESystem, or ODESystem should be Real.")) - end - end -end - function check_lhs(eq::Equation, op, dvs::Set) v = unwrap(eq.lhs) _iszero(v) && return From 5c1cebf0658d64328a353d1beb7913d535590480 Mon Sep 17 00:00:00 2001 From: vyudu Date: Wed, 29 Jan 2025 13:09:29 -0500 Subject: [PATCH 0688/2176] add constructor --- src/systems/diffeqs/odesystem.jl | 67 ++----------------------------- src/systems/diffeqs/sdesystem.jl | 15 +++++++ src/utils.jl | 69 ++++++++++++++++++++++++++++++++ 3 files changed, 87 insertions(+), 64 deletions(-) diff --git a/src/systems/diffeqs/odesystem.jl b/src/systems/diffeqs/odesystem.jl index 41bcaf1d01..efd7509572 100644 --- a/src/systems/diffeqs/odesystem.jl +++ b/src/systems/diffeqs/odesystem.jl @@ -296,71 +296,10 @@ function ODESystem(deqs::AbstractVector{<:Equation}, iv, dvs, ps; end function ODESystem(eqs, iv; kwargs...) - eqs = collect(eqs) - # NOTE: this assumes that the order of algebraic equations doesn't matter - diffvars = OrderedSet() - allunknowns = OrderedSet() - ps = OrderedSet() - # reorder equations such that it is in the form of `diffeq, algeeq` - diffeq = Equation[] - algeeq = Equation[] - # initial loop for finding `iv` - if iv === nothing - for eq in eqs - if !(eq.lhs isa Number) # assume eq.lhs is either Differential or Number - iv = iv_from_nested_derivative(eq.lhs) - break - end - end - end - iv = value(iv) - iv === nothing && throw(ArgumentError("Please pass in independent variables.")) - compressed_eqs = Equation[] # equations that need to be expanded later, like `connect(a, b)` - for eq in eqs - eq.lhs isa Union{Symbolic, Number} || (push!(compressed_eqs, eq); continue) - collect_vars!(allunknowns, ps, eq, iv) - if isdiffeq(eq) - diffvar, _ = var_from_nested_derivative(eq.lhs) - if check_scope_depth(getmetadata(diffvar, SymScope, LocalScope()), 0) - isequal(iv, iv_from_nested_derivative(eq.lhs)) || - throw(ArgumentError("An ODESystem can only have one independent variable.")) - diffvar in diffvars && - throw(ArgumentError("The differential variable $diffvar is not unique in the system of equations.")) - push!(diffvars, diffvar) - end - push!(diffeq, eq) - else - push!(algeeq, eq) - end - end - for eq in get(kwargs, :parameter_dependencies, Equation[]) - collect_vars!(allunknowns, ps, eq, iv) - end - for ssys in get(kwargs, :systems, ODESystem[]) - collect_scoped_vars!(allunknowns, ps, ssys, iv) - end - for v in allunknowns - isdelay(v, iv) || continue - collect_vars!(allunknowns, ps, arguments(v)[1], iv) - end - new_ps = OrderedSet() - for p in ps - if iscall(p) && operation(p) === getindex - par = arguments(p)[begin] - if Symbolics.shape(Symbolics.unwrap(par)) !== Symbolics.Unknown() && - all(par[i] in ps for i in eachindex(par)) - push!(new_ps, par) - else - push!(new_ps, p) - end - else - push!(new_ps, p) - end - end - algevars = setdiff(allunknowns, diffvars) + param_deps = get(kwargs, :parameter_dependencies, Equation[]) + eqs, dvs, ps = process_equations_DESystem(eqs, param_deps, iv) # the orders here are very important! - return ODESystem(Equation[diffeq; algeeq; compressed_eqs], iv, - collect(Iterators.flatten((diffvars, algevars))), collect(new_ps); kwargs...) + return ODESystem(eqs, iv, dvs, ps; kwargs...) end # NOTE: equality does not check cached Jacobian diff --git a/src/systems/diffeqs/sdesystem.jl b/src/systems/diffeqs/sdesystem.jl index df950ee01c..0462d4d12d 100644 --- a/src/systems/diffeqs/sdesystem.jl +++ b/src/systems/diffeqs/sdesystem.jl @@ -273,6 +273,21 @@ function SDESystem(sys::ODESystem, neqs; kwargs...) SDESystem(equations(sys), neqs, get_iv(sys), unknowns(sys), parameters(sys); kwargs...) end +function SDESystem(eqs, noiseeqs, iv; kwargs...) + param_deps = get(kwargs, :parameter_dependencies, Equation[]) + eqs, dvs, ps = process_equations_DESystem(eqs, param_deps, iv) + + # validate noise equations + noisedvs = OrderedSet() + noiseps = OrderedSet() + collect_vars!(noisedvs, noiseps, noiseeqs, iv) + for dv in noisedvs + var ∈ Set(dvs) || throw(ArgumentError("Variable $dv in noise equations is not an unknown of the system.")) + end + + return SDESystem(eqs, noiseeqs, iv, dvs, [ps; collect(noiseps)]; kwargs...) +end + function Base.:(==)(sys1::SDESystem, sys2::SDESystem) sys1 === sys2 && return true iv1 = get_iv(sys1) diff --git a/src/utils.jl b/src/utils.jl index b6544972b9..9cb109c1d3 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -1185,3 +1185,72 @@ function guesses_from_metadata!(guesses, vars) guesses[vars[i]] = varguesses[i] end end + +""" + $(TYPEDSIGNATURES) + +Find all the unknowns and parameters from the equations and parameter dependencies of an ODESystem or SDESystem. Return re-ordered equations, unknowns, and parameters. +""" +function process_equations_DESystem(eqs, param_deps, iv) + eqs = collect(eqs) + + diffvars = OrderedSet() + allunknowns = OrderedSet() + ps = OrderedSet() + + # NOTE: this assumes that the order of algebraic equations doesn't matter + # reorder equations such that it is in the form of `diffeq, algeeq` + diffeq = Equation[] + algeeq = Equation[] + # initial loop for finding `iv` + if iv === nothing + for eq in eqs + if !(eq.lhs isa Number) # assume eq.lhs is either Differential or Number + iv = iv_from_nested_derivative(eq.lhs) + break + end + end + end + iv = value(iv) + iv === nothing && throw(ArgumentError("Please pass in independent variables.")) + + compressed_eqs = Equation[] # equations that need to be expanded later, like `connect(a, b)` + for eq in eqs + eq.lhs isa Union{Symbolic, Number} || (push!(compressed_eqs, eq); continue) + collect_vars!(allunknowns, ps, eq, iv) + if isdiffeq(eq) + diffvar, _ = var_from_nested_derivative(eq.lhs) + if check_scope_depth(getmetadata(diffvar, SymScope, LocalScope()), 0) + isequal(iv, iv_from_nested_derivative(eq.lhs)) || + throw(ArgumentError("An ODESystem can only have one independent variable.")) + diffvar in diffvars && + throw(ArgumentError("The differential variable $diffvar is not unique in the system of equations.")) + push!(diffvars, diffvar) + end + push!(diffeq, eq) + else + push!(algeeq, eq) + end + end + for eq in param_deps + collect_vars!(allunknowns, ps, eq, iv) + end + + new_ps = OrderedSet() + for p in ps + if iscall(p) && operation(p) === getindex + par = arguments(p)[begin] + if Symbolics.shape(Symbolics.unwrap(par)) !== Symbolics.Unknown() && + all(par[i] in ps for i in eachindex(par)) + push!(new_ps, par) + else + push!(new_ps, p) + end + else + push!(new_ps, p) + end + end + algevars = setdiff(allunknowns, diffvars) + + Equation[diffeq; algeeq; compressed_eqs], collect(Iterators.flatten((diffvars, algevars))), collect(new_ps) +end From 3ea2dd389e425c9adb26545cd4ca3428fc48c3d4 Mon Sep 17 00:00:00 2001 From: vyudu Date: Wed, 29 Jan 2025 13:15:42 -0500 Subject: [PATCH 0689/2176] remove check_var_type --- src/systems/discrete_system/discrete_system.jl | 1 - src/systems/jumps/jumpsystem.jl | 1 - src/systems/nonlinear/nonlinearsystem.jl | 3 --- src/systems/optimization/optimizationsystem.jl | 3 --- src/systems/pde/pdesystem.jl | 3 --- 5 files changed, 11 deletions(-) diff --git a/src/systems/discrete_system/discrete_system.jl b/src/systems/discrete_system/discrete_system.jl index fb6dfe0236..bd5c72eec7 100644 --- a/src/systems/discrete_system/discrete_system.jl +++ b/src/systems/discrete_system/discrete_system.jl @@ -122,7 +122,6 @@ struct DiscreteSystem <: AbstractTimeDependentSystem check_independent_variables([iv]) check_variables(dvs, iv) check_parameters(ps, iv) - check_var_types(DiscreteSystem, dvs) end if checks == true || (checks & CheckUnits) > 0 u = __get_unit_type(dvs, ps, iv) diff --git a/src/systems/jumps/jumpsystem.jl b/src/systems/jumps/jumpsystem.jl index 4d77f4106d..f39b5fa46a 100644 --- a/src/systems/jumps/jumpsystem.jl +++ b/src/systems/jumps/jumpsystem.jl @@ -147,7 +147,6 @@ struct JumpSystem{U <: ArrayPartition} <: AbstractTimeDependentSystem check_independent_variables([iv]) check_variables(unknowns, iv) check_parameters(ps, iv) - check_var_types(JumpSystem, unknowns) end if checks == true || (checks & CheckUnits) > 0 u = __get_unit_type(unknowns, ps, iv) diff --git a/src/systems/nonlinear/nonlinearsystem.jl b/src/systems/nonlinear/nonlinearsystem.jl index 41aaf65d1a..89663f7dbc 100644 --- a/src/systems/nonlinear/nonlinearsystem.jl +++ b/src/systems/nonlinear/nonlinearsystem.jl @@ -115,9 +115,6 @@ struct NonlinearSystem <: AbstractTimeIndependentSystem tearing_state = nothing, substitutions = nothing, complete = false, index_cache = nothing, parent = nothing, isscheduled = false; checks::Union{Bool, Int} = true) - if checks == true || (checks & CheckComponents) > 0 - check_var_types(NonlinearSystem, unknowns) - end if checks == true || (checks & CheckUnits) > 0 u = __get_unit_type(unknowns, ps) check_units(u, eqs) diff --git a/src/systems/optimization/optimizationsystem.jl b/src/systems/optimization/optimizationsystem.jl index de4696bfa2..860e063e35 100644 --- a/src/systems/optimization/optimizationsystem.jl +++ b/src/systems/optimization/optimizationsystem.jl @@ -73,9 +73,6 @@ struct OptimizationSystem <: AbstractOptimizationSystem gui_metadata = nothing, complete = false, index_cache = nothing, parent = nothing, isscheduled = false; checks::Union{Bool, Int} = true) - if checks == true || (checks & CheckComponents) > 0 - check_var_types(OptimizationSystem, unknowns) - end if checks == true || (checks & CheckUnits) > 0 u = __get_unit_type(unknowns, ps) unwrap(op) isa Symbolic && check_units(u, op) diff --git a/src/systems/pde/pdesystem.jl b/src/systems/pde/pdesystem.jl index e2a5bb5734..eac540e401 100644 --- a/src/systems/pde/pdesystem.jl +++ b/src/systems/pde/pdesystem.jl @@ -102,9 +102,6 @@ struct PDESystem <: ModelingToolkit.AbstractMultivariateSystem checks::Union{Bool, Int} = true, description = "", name) - if checks == true || (checks & CheckComponents) > 0 - check_var_types(PDESystem, [dv(ivs...) for dv in dvs]) - end if checks == true || (checks & CheckUnits) > 0 u = __get_unit_type(dvs, ivs, ps) check_units(u, eqs) From 3329ce5e838177fcd4a369bf73e0e6dfd837aedf Mon Sep 17 00:00:00 2001 From: vyudu Date: Wed, 29 Jan 2025 13:29:36 -0500 Subject: [PATCH 0690/2176] up --- src/systems/diffeqs/sdesystem.jl | 5 ++++- src/utils.jl | 2 +- test/odesystem.jl | 5 ++++- test/sdesystem.jl | 4 +++- 4 files changed, 12 insertions(+), 4 deletions(-) diff --git a/src/systems/diffeqs/sdesystem.jl b/src/systems/diffeqs/sdesystem.jl index 0462d4d12d..747d8e844c 100644 --- a/src/systems/diffeqs/sdesystem.jl +++ b/src/systems/diffeqs/sdesystem.jl @@ -273,7 +273,7 @@ function SDESystem(sys::ODESystem, neqs; kwargs...) SDESystem(equations(sys), neqs, get_iv(sys), unknowns(sys), parameters(sys); kwargs...) end -function SDESystem(eqs, noiseeqs, iv; kwargs...) +function SDESystem(eqs::Vector{Equation}, noiseeqs::AbstractArray, iv; kwargs...) param_deps = get(kwargs, :parameter_dependencies, Equation[]) eqs, dvs, ps = process_equations_DESystem(eqs, param_deps, iv) @@ -288,6 +288,9 @@ function SDESystem(eqs, noiseeqs, iv; kwargs...) return SDESystem(eqs, noiseeqs, iv, dvs, [ps; collect(noiseps)]; kwargs...) end +SDESystem(eq::Equation, noiseeqs::AbstractArray, args...; kwargs...) = SDESystem([eq], noiseeqs, args...; kwargs...) +SDESystem(eq::Equation, noiseeq, args...; kwargs...) = SDESystem([eq], [noiseeq], args...; kwargs...) + function Base.:(==)(sys1::SDESystem, sys2::SDESystem) sys1 === sys2 && return true iv1 = get_iv(sys1) diff --git a/src/utils.jl b/src/utils.jl index f3828bea4f..a71e9ba799 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -1225,7 +1225,7 @@ function process_equations_DESystem(eqs, param_deps, iv) throw(ArgumentError("An ODESystem can only have one independent variable.")) diffvar in diffvars && throw(ArgumentError("The differential variable $diffvar is not unique in the system of equations.")) - !(symtype(diffvar) === Real || eltype(symtype(diffvar)) === Real) && throw(ArgumentError("Differential variable $var has type $(symtype(diffvar)). Differential variables should not be concretely typed.")) + !(symtype(diffvar) === Real || eltype(symtype(diffvar)) === Real) && throw(ArgumentError("Differential variable $diffvar has type $(symtype(diffvar)). Differential variables should not be concretely typed.")) push!(diffvars, diffvar) end push!(diffeq, eq) diff --git a/test/odesystem.jl b/test/odesystem.jl index 29076416fa..dce9275efe 100644 --- a/test/odesystem.jl +++ b/test/odesystem.jl @@ -1548,7 +1548,10 @@ end @parameters p d @variables X(t)::Int64 eq = D(X) ~ p - d*X - @test_throws Exception @mtkbuild osys = ODESystem([eq], t) + @test_throws ArgumentError @mtkbuild osys = ODESystem([eq], t) + @variables Y(t)[1:3]::String + eq = D(Y) ~ [p, p, p] + @test_throws ArgumentError @mtkbuild osys = ODESystem([eq], t) end # Test `isequal` diff --git a/test/sdesystem.jl b/test/sdesystem.jl index dee497f62c..7cac3645c4 100644 --- a/test/sdesystem.jl +++ b/test/sdesystem.jl @@ -875,7 +875,9 @@ end @variables X(t)::Int64 @brownian z eq2 = D(X) ~ p - d*X + z - @test_throws Exception @mtkbuild ssys = System([eq2], t) + @test_throws ArgumentError @mtkbuild ssys = System([eq2], t) + noiseeq = [1] + @test_throws ArgumentError @named ssys = SDESystem([eq2], [noiseeq], t) end @testset "SDEFunctionExpr" begin From 288a4a01030df20a9f106012c0bfcea52a1632fc Mon Sep 17 00:00:00 2001 From: vyudu Date: Wed, 29 Jan 2025 14:01:23 -0500 Subject: [PATCH 0691/2176] up --- src/systems/diffeqs/odesystem.jl | 35 ++++++++++++++++++++++++++++--- src/systems/diffeqs/sdesystem.jl | 36 ++++++++++++++++++++++++++++---- src/utils.jl | 25 +++------------------- 3 files changed, 67 insertions(+), 29 deletions(-) diff --git a/src/systems/diffeqs/odesystem.jl b/src/systems/diffeqs/odesystem.jl index 4aaf79f3f1..9953e28428 100644 --- a/src/systems/diffeqs/odesystem.jl +++ b/src/systems/diffeqs/odesystem.jl @@ -296,9 +296,38 @@ function ODESystem(deqs::AbstractVector{<:Equation}, iv, dvs, ps; end function ODESystem(eqs, iv; kwargs...) - param_deps = get(kwargs, :parameter_dependencies, Equation[]) - eqs, dvs, ps = process_equations_DESystem(eqs, param_deps, iv) - return ODESystem(eqs, iv, dvs, ps; kwargs...) + diffvars, allunknowns, ps, eqs = process_equations(eqs, iv) + + for eq in get(kwargs, :parameter_dependencies, Equation[]) + collect_vars!(allunknowns, ps, eq, iv) + end + + for ssys in get(kwargs, :systems, ODESystem[]) + collect_scoped_vars!(allunknowns, ps, ssys, iv) + end + + for v in allunknowns + isdelay(v, iv) || continue + collect_vars!(allunknowns, ps, arguments(v)[1], iv) + end + + new_ps = OrderedSet() + for p in ps + if iscall(p) && operation(p) === getindex + par = arguments(p)[begin] + if Symbolics.shape(Symbolics.unwrap(par)) !== Symbolics.Unknown() && + all(par[i] in ps for i in eachindex(par)) + push!(new_ps, par) + else + push!(new_ps, p) + end + else + push!(new_ps, p) + end + end + algevars = setdiff(allunknowns, diffvars) + + return ODESystem(eqs, iv, collect(Iterators.flatten((diffvars, algevars))), collect(new_ps); kwargs...) end # NOTE: equality does not check cached Jacobian diff --git a/src/systems/diffeqs/sdesystem.jl b/src/systems/diffeqs/sdesystem.jl index 747d8e844c..0d0a2ec0c7 100644 --- a/src/systems/diffeqs/sdesystem.jl +++ b/src/systems/diffeqs/sdesystem.jl @@ -274,18 +274,46 @@ function SDESystem(sys::ODESystem, neqs; kwargs...) end function SDESystem(eqs::Vector{Equation}, noiseeqs::AbstractArray, iv; kwargs...) - param_deps = get(kwargs, :parameter_dependencies, Equation[]) - eqs, dvs, ps = process_equations_DESystem(eqs, param_deps, iv) + diffvars, allunknowns, ps, eqs = process_equations(eqs, iv) + + for eq in get(kwargs, :parameter_dependencies, Equation[]) + collect_vars!(allunknowns, ps, eq, iv) + end + + for ssys in get(kwargs, :systems, ODESystem[]) + collect_scoped_vars!(allunknowns, ps, ssys, iv) + end + + for v in allunknowns + isdelay(v, iv) || continue + collect_vars!(allunknowns, ps, arguments(v)[1], iv) + end + + new_ps = OrderedSet() + for p in ps + if iscall(p) && operation(p) === getindex + par = arguments(p)[begin] + if Symbolics.shape(Symbolics.unwrap(par)) !== Symbolics.Unknown() && + all(par[i] in ps for i in eachindex(par)) + push!(new_ps, par) + else + push!(new_ps, p) + end + else + push!(new_ps, p) + end + end # validate noise equations noisedvs = OrderedSet() noiseps = OrderedSet() collect_vars!(noisedvs, noiseps, noiseeqs, iv) for dv in noisedvs - var ∈ Set(dvs) || throw(ArgumentError("Variable $dv in noise equations is not an unknown of the system.")) + var ∈ allunknowns || throw(ArgumentError("Variable $dv in noise equations is not an unknown of the system.")) end + algevars = setdiff(allunknowns, diffvars) - return SDESystem(eqs, noiseeqs, iv, dvs, [ps; collect(noiseps)]; kwargs...) + return SDESystem(eqs, noiseeqs, iv, Iterators.flatten((diffvars, algevars)), [ps; collect(noiseps)]; kwargs...) end SDESystem(eq::Equation, noiseeqs::AbstractArray, args...; kwargs...) = SDESystem([eq], noiseeqs, args...; kwargs...) diff --git a/src/utils.jl b/src/utils.jl index a71e9ba799..7bb1ebcbe9 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -1189,9 +1189,9 @@ end """ $(TYPEDSIGNATURES) -Find all the unknowns and parameters from the equations and parameter dependencies of an ODESystem or SDESystem. Return re-ordered equations, unknowns, and parameters. +Find all the unknowns and parameters from the equations of a SDESystem or ODESystem. Return re-ordered equations, differential variables, all variables, and parameters. """ -function process_equations_DESystem(eqs, param_deps, iv) +function process_equations(eqs, iv) eqs = collect(eqs) diffvars = OrderedSet() @@ -1233,25 +1233,6 @@ function process_equations_DESystem(eqs, param_deps, iv) push!(algeeq, eq) end end - for eq in param_deps - collect_vars!(allunknowns, ps, eq, iv) - end - - new_ps = OrderedSet() - for p in ps - if iscall(p) && operation(p) === getindex - par = arguments(p)[begin] - if Symbolics.shape(Symbolics.unwrap(par)) !== Symbolics.Unknown() && - all(par[i] in ps for i in eachindex(par)) - push!(new_ps, par) - else - push!(new_ps, p) - end - else - push!(new_ps, p) - end - end - algevars = setdiff(allunknowns, diffvars) - Equation[diffeq; algeeq; compressed_eqs], collect(Iterators.flatten((diffvars, algevars))), collect(new_ps) + diffvars, allunknowns, ps, Equation[diffeq; algeeq; compressed_eqs] end From 84759d821283c3ff51c34600350a03605bdbf7d9 Mon Sep 17 00:00:00 2001 From: vyudu Date: Wed, 29 Jan 2025 14:06:16 -0500 Subject: [PATCH 0692/2176] Fix typo --- src/systems/diffeqs/sdesystem.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/systems/diffeqs/sdesystem.jl b/src/systems/diffeqs/sdesystem.jl index 0d0a2ec0c7..f82bc5cadc 100644 --- a/src/systems/diffeqs/sdesystem.jl +++ b/src/systems/diffeqs/sdesystem.jl @@ -309,7 +309,7 @@ function SDESystem(eqs::Vector{Equation}, noiseeqs::AbstractArray, iv; kwargs... noiseps = OrderedSet() collect_vars!(noisedvs, noiseps, noiseeqs, iv) for dv in noisedvs - var ∈ allunknowns || throw(ArgumentError("Variable $dv in noise equations is not an unknown of the system.")) + dv ∈ allunknowns || throw(ArgumentError("Variable $dv in noise equations is not an unknown of the system.")) end algevars = setdiff(allunknowns, diffvars) From 37092f12c1fb6958b4d1ab6a917be99221371e61 Mon Sep 17 00:00:00 2001 From: vyudu Date: Wed, 29 Jan 2025 14:32:17 -0500 Subject: [PATCH 0693/2176] lower tol --- test/bvproblem.jl | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/bvproblem.jl b/test/bvproblem.jl index c6c124d09e..30fde44531 100644 --- a/test/bvproblem.jl +++ b/test/bvproblem.jl @@ -1,6 +1,6 @@ ### TODO: update when BoundaryValueDiffEqAscher is updated to use the normal boundary condition conventions -using BoundaryValueDiffEq, OrdinaryDiffEq, BoundaryValueDiffEqAscher +using BoundaryValueDiffEq, OrdinaryDiffEqDefault, BoundaryValueDiffEqAscher using BenchmarkTools using ModelingToolkit using SciMLBase @@ -166,7 +166,7 @@ let @test sol1 ≈ sol2 ≈ sol3 end -function test_solvers(solvers, prob, u0map, constraints, equations = []; dt = 0.05, atol = 1e-3) +function test_solvers(solvers, prob, u0map, constraints, equations = []; dt = 0.05, atol = 1e-2) for solver in solvers println("Solver: $solver") sol = @btime solve($prob, $solver(), dt = $dt, abstol = $atol) @@ -214,7 +214,7 @@ let bvp = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}(lksys, u0map, tspan; guesses) test_solvers(solvers, bvp, u0map, constr; dt = 0.05) - # Testing that more complicated constr give correct solutions. + # Testing that more complicated constraints give correct solutions. constr = [y(.2) + x(.8) ~ 3., y(.3) ~ 2.] @mtkbuild lksys = ODESystem(eqs, t; constraints = constr) bvp = SciMLBase.BVProblem{false, SciMLBase.FullSpecialize}(lksys, u0map, tspan; guesses) From 4142923e7e7e38e90c65952265c02e93fc081472 Mon Sep 17 00:00:00 2001 From: vyudu Date: Wed, 29 Jan 2025 15:04:39 -0500 Subject: [PATCH 0694/2176] up --- .../implicit_discrete_system.jl | 36 +++++++++---------- 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/src/systems/discrete_system/implicit_discrete_system.jl b/src/systems/discrete_system/implicit_discrete_system.jl index 404fa7ff49..56e7545c6a 100644 --- a/src/systems/discrete_system/implicit_discrete_system.jl +++ b/src/systems/discrete_system/implicit_discrete_system.jl @@ -23,7 +23,7 @@ struct ImplicitDiscreteSystem <: AbstractTimeDependentSystem structurally identical. """ tag::UInt - """The differential equations defining the discrete system.""" + """The difference equations defining the discrete system.""" eqs::Vector{Equation} """Independent variable.""" iv::BasicSymbolic{Real} @@ -48,10 +48,10 @@ struct ImplicitDiscreteSystem <: AbstractTimeDependentSystem """ The internal systems. These are required to have unique names. """ - systems::Vector{DiscreteSystem} + systems::Vector{ImplicitDiscreteSystem} """ The default values to use when initial conditions and/or - parameters are not supplied in `DiscreteProblem`. + parameters are not supplied in `ImplicitDiscreteProblem`. """ defaults::Dict """ @@ -136,11 +136,11 @@ end """ $(TYPEDSIGNATURES) -Constructs a DiscreteSystem. +Constructs a ImplicitDiscreteSystem. """ function ImplicitDiscreteSystem(eqs::AbstractVector{<:Equation}, iv, dvs, ps; observed = Num[], - systems = DiscreteSystem[], + systems = ImplicitDiscreteSystem[], tspan = nothing, name = nothing, description = "", @@ -162,12 +162,12 @@ function ImplicitDiscreteSystem(eqs::AbstractVector{<:Equation}, iv, dvs, ps; dvs′ = value.(dvs) ps′ = value.(ps) if any(hasderiv, eqs) || any(hashold, eqs) || any(hassample, eqs) || any(hasdiff, eqs) - error("Equations in a `DiscreteSystem` can only have `Shift` operators.") + error("Equations in a `ImplicitDiscreteSystem` can only have `Shift` operators.") end if !(isempty(default_u0) && isempty(default_p)) Base.depwarn( "`default_u0` and `default_p` are deprecated. Use `defaults` instead.", - :DiscreteSystem, force = true) + :ImplicitDiscreteSystem, force = true) end defaults = Dict{Any, Any}(todict(defaults)) @@ -190,7 +190,7 @@ function ImplicitDiscreteSystem(eqs::AbstractVector{<:Equation}, iv, dvs, ps; if length(unique(sysnames)) != length(sysnames) throw(ArgumentError("System names must be unique.")) end - DiscreteSystem(Threads.atomic_add!(SYSTEM_COUNT, UInt(1)), + ImplicitDiscreteSystem(Threads.atomic_add!(SYSTEM_COUNT, UInt(1)), eqs, iv′, dvs′, ps′, tspan, var_to_name, observed, name, description, systems, defaults, guesses, initializesystem, initialization_eqs, preface, connector_type, parameter_dependencies, metadata, gui_metadata, kwargs...) @@ -206,7 +206,7 @@ function ImplicitDiscreteSystem(eqs, iv; kwargs...) collect_vars!(allunknowns, ps, eq, iv; op = Shift) if iscall(eq.lhs) && operation(eq.lhs) isa Shift isequal(iv, operation(eq.lhs).t) || - throw(ArgumentError("A DiscreteSystem can only have one independent variable.")) + throw(ArgumentError("An ImplicitDiscreteSystem can only have one independent variable.")) eq.lhs in diffvars && throw(ArgumentError("The shift variable $(eq.lhs) is not unique in the system of equations.")) push!(diffvars, eq.lhs) @@ -233,16 +233,16 @@ function ImplicitDiscreteSystem(eqs, iv; kwargs...) push!(new_ps, p) end end - return DiscreteSystem(eqs, iv, + return ImplicitDiscreteSystem(eqs, iv, collect(allunknowns), collect(new_ps); kwargs...) end -function flatten(sys::DiscreteSystem, noeqs = false) +function flatten(sys::ImplicitDiscreteSystem, noeqs = false) systems = get_systems(sys) if isempty(systems) return sys else - return DiscreteSystem(noeqs ? Equation[] : equations(sys), + return ImplicitDiscreteSystem(noeqs ? Equation[] : equations(sys), get_iv(sys), unknowns(sys), parameters(sys), @@ -258,14 +258,14 @@ function flatten(sys::DiscreteSystem, noeqs = false) end function generate_function( - sys::DiscreteSystem, dvs = unknowns(sys), ps = parameters(sys); wrap_code = identity, kwargs...) + sys::ImplicitDiscreteSystem, dvs = unknowns(sys), ps = parameters(sys); wrap_code = identity, kwargs...) exprs = [eq.rhs for eq in equations(sys)] wrap_code = wrap_code .∘ wrap_array_vars(sys, exprs) .∘ wrap_parameter_dependencies(sys, false) generate_custom_function(sys, exprs, dvs, ps; wrap_code, kwargs...) end -function shift_u0map_forward(sys::DiscreteSystem, u0map, defs) +function shift_u0map_forward(sys::ImplicitDiscreteSystem, u0map, defs) iv = get_iv(sys) updated = AnyDict() for k in collect(keys(u0map)) @@ -291,7 +291,7 @@ end Generates an ImplicitDiscreteProblem from an ImplicitDiscreteSystem. """ function SciMLBase.ImplicitDiscreteProblem( - sys::DiscreteSystem, u0map = [], tspan = get_tspan(sys), + sys::ImplicitDiscreteSystem, u0map = [], tspan = get_tspan(sys), parammap = SciMLBase.NullParameters(); eval_module = @__MODULE__, eval_expression = false, @@ -309,7 +309,7 @@ function SciMLBase.ImplicitDiscreteProblem( u0map = to_varmap(u0map, dvs) u0map = shift_u0map_forward(sys, u0map, defaults(sys)) f, u0, p = process_SciMLProblem( - DiscreteFunction, sys, u0map, parammap; eval_expression, eval_module) + ImplicitDiscreteFunction, sys, u0map, parammap; eval_expression, eval_module) u0 = f(u0, p, tspan[1]) ImplicitDiscreteProblem(f, u0, tspan, p; kwargs...) end @@ -348,7 +348,7 @@ function SciMLBase.ImplicitDiscreteFunction{iip, specialize}( if specialize === SciMLBase.FunctionWrapperSpecialize && iip if u0 === nothing || p === nothing || t === nothing - error("u0, p, and t must be specified for FunctionWrapperSpecialize on DiscreteFunction.") + error("u0, p, and t must be specified for FunctionWrapperSpecialize on ImplicitDiscreteFunction.") end f = SciMLBase.wrapfun_iip(f, (u0, u0, p, t)) end @@ -395,7 +395,7 @@ function ImplicitDiscreteFunctionExpr{iip}(sys::ImplicitDiscreteSystem, dvs = un ex = quote $_f - DiscreteFunction{$iip}($fsym) + ImplicitDiscreteFunction{$iip}($fsym) end !linenumbers ? Base.remove_linenums!(ex) : ex end From e5eb8bd35a89ab8b0d5346c218ea0bdf6cb15f37 Mon Sep 17 00:00:00 2001 From: vyudu Date: Wed, 29 Jan 2025 15:05:35 -0500 Subject: [PATCH 0695/2176] fix Project.toml --- Project.toml | 1 - 1 file changed, 1 deletion(-) diff --git a/Project.toml b/Project.toml index 69ad24c8df..865b88658b 100644 --- a/Project.toml +++ b/Project.toml @@ -143,7 +143,6 @@ SparseArrays = "1" SpecialFunctions = "0.7, 0.8, 0.9, 0.10, 1.0, 2" StaticArrays = "0.10, 0.11, 0.12, 1.0" StochasticDelayDiffEq = "1.8.1" -StochasticDiffEq = "6.72.1" SymbolicIndexingInterface = "0.3.36" SymbolicUtils = "3.10" Symbolics = "6.22.1" From afc468996d65bff484d66327149f2f8ccabfa83e Mon Sep 17 00:00:00 2001 From: vyudu Date: Thu, 30 Jan 2025 11:03:18 -0500 Subject: [PATCH 0696/2176] add test file --- src/ModelingToolkit.jl | 2 + .../discrete_system/discrete_system.jl | 1 + .../implicit_discrete_system.jl | 48 +++++++++++++------ src/systems/systemstructure.jl | 4 +- test/implicit_discrete_system.jl | 2 + test/runtests.jl | 3 +- 6 files changed, 44 insertions(+), 16 deletions(-) create mode 100644 test/implicit_discrete_system.jl diff --git a/src/ModelingToolkit.jl b/src/ModelingToolkit.jl index 2710d7d1e4..4083f2609d 100644 --- a/src/ModelingToolkit.jl +++ b/src/ModelingToolkit.jl @@ -164,6 +164,7 @@ include("systems/diffeqs/modelingtoolkitize.jl") include("systems/diffeqs/basic_transformations.jl") include("systems/discrete_system/discrete_system.jl") +include("systems/discrete_system/implicit_discrete_system.jl") include("systems/jumps/jumpsystem.jl") @@ -229,6 +230,7 @@ export DAEFunctionExpr, DAEProblemExpr export SDESystem, SDEFunction, SDEFunctionExpr, SDEProblemExpr export SystemStructure export DiscreteSystem, DiscreteProblem, DiscreteFunction, DiscreteFunctionExpr +export ImplicitDiscreteSystem, ImplicitDiscreteProblem, ImplicitDiscreteFunction, ImplicitDiscreteFunctionExpr export JumpSystem export ODEProblem, SDEProblem export NonlinearFunction, NonlinearFunctionExpr diff --git a/src/systems/discrete_system/discrete_system.jl b/src/systems/discrete_system/discrete_system.jl index bd5c72eec7..0ad3317ea1 100644 --- a/src/systems/discrete_system/discrete_system.jl +++ b/src/systems/discrete_system/discrete_system.jl @@ -233,6 +233,7 @@ function DiscreteSystem(eqs, iv; kwargs...) push!(new_ps, p) end end + @show allunknowns return DiscreteSystem(eqs, iv, collect(allunknowns), collect(new_ps); kwargs...) end diff --git a/src/systems/discrete_system/implicit_discrete_system.jl b/src/systems/discrete_system/implicit_discrete_system.jl index 56e7545c6a..1e1b5d5f6f 100644 --- a/src/systems/discrete_system/implicit_discrete_system.jl +++ b/src/systems/discrete_system/implicit_discrete_system.jl @@ -10,11 +10,10 @@ using ModelingToolkit: t_nounits as t @parameters σ=28.0 ρ=10.0 β=8/3 δt=0.1 @variables x(t)=1.0 y(t)=0.0 z(t)=0.0 k = ShiftIndex(t) -eqs = [x(k+1) ~ σ*(y-x), - y(k+1) ~ x*(ρ-z)-y, - z(k+1) ~ x*y - β*z] -@named de = ImplicitDiscreteSystem(eqs,t,[x,y,z],[σ,ρ,β]; tspan = (0, 1000.0)) # or -@named de = ImplicitDiscreteSystem(eqs) +eqs = [x ~ σ*(y(k-1)-x), + y ~ x*(ρ-z(k-1))-y, + z ~ x(k-1)*y - β*z] +@named ide = ImplicitDiscreteSystem(eqs,t,[x,y,z],[σ,ρ,β]; tspan = (0, 1000.0)) ``` """ struct ImplicitDiscreteSystem <: AbstractTimeDependentSystem @@ -136,6 +135,7 @@ end """ $(TYPEDSIGNATURES) + Constructs a ImplicitDiscreteSystem. """ function ImplicitDiscreteSystem(eqs::AbstractVector{<:Equation}, iv, dvs, ps; @@ -170,6 +170,8 @@ function ImplicitDiscreteSystem(eqs::AbstractVector{<:Equation}, iv, dvs, ps; :ImplicitDiscreteSystem, force = true) end + # Copy equations to canonical form, but do not touch array expressions + eqs = [wrap(eq.lhs) isa Symbolics.Arr ? eq : 0 ~ eq.rhs - eq.lhs for eq in eqs] defaults = Dict{Any, Any}(todict(defaults)) guesses = Dict{Any, Any}(todict(guesses)) var_to_name = Dict() @@ -236,6 +238,8 @@ function ImplicitDiscreteSystem(eqs, iv; kwargs...) return ImplicitDiscreteSystem(eqs, iv, collect(allunknowns), collect(new_ps); kwargs...) end +# basically at every timestep it should build a nonlinear solve +# Previous timesteps should be treated as parameters? is this right? function flatten(sys::ImplicitDiscreteSystem, noeqs = false) systems = get_systems(sys) @@ -259,10 +263,25 @@ end function generate_function( sys::ImplicitDiscreteSystem, dvs = unknowns(sys), ps = parameters(sys); wrap_code = identity, kwargs...) - exprs = [eq.rhs for eq in equations(sys)] - wrap_code = wrap_code .∘ wrap_array_vars(sys, exprs) .∘ - wrap_parameter_dependencies(sys, false) - generate_custom_function(sys, exprs, dvs, ps; wrap_code, kwargs...) + if !iscomplete(sys) + error("A completed system is required. Call `complete` or `structural_simplify` on the system.") + end + p = (reorder_parameters(sys, unwrap.(ps))..., cachesyms...) + isscalar = !(exprs isa AbstractArray) + pre, sol_states = get_substitutions_and_solved_unknowns(sys, isscalar ? [exprs] : exprs) + if postprocess_fbody === nothing + postprocess_fbody = pre + end + if states === nothing + states = sol_states + end + exprs = [eq.lhs - eq.rhs for eq in equations(sys)] + u = map(Shift(iv, -1), dvs) + u_next = dvs + + wrap_code = wrap_code .∘ wrap_array_vars(sys, exprs) .∘ wrap_parameter_dependencies(sys, false) + + build_function(exprs, u_next, u, p..., get_iv(sys)) end function shift_u0map_forward(sys::ImplicitDiscreteSystem, u0map, defs) @@ -311,7 +330,7 @@ function SciMLBase.ImplicitDiscreteProblem( f, u0, p = process_SciMLProblem( ImplicitDiscreteFunction, sys, u0map, parammap; eval_expression, eval_module) u0 = f(u0, p, tspan[1]) - ImplicitDiscreteProblem(f, u0, tspan, p; kwargs...) + NonlinearProblem(f, u0, tspan, p; kwargs...) end function SciMLBase.ImplicitDiscreteFunction(sys::ImplicitDiscreteSystem, args...; kwargs...) @@ -337,14 +356,15 @@ function SciMLBase.ImplicitDiscreteFunction{iip, specialize}( eval_module = @__MODULE__, analytic = nothing, kwargs...) where {iip, specialize} + if !iscomplete(sys) error("A completed `ImplicitDiscreteSystem` is required. Call `complete` or `structural_simplify` on the system before creating a `ImplicitDiscreteProblem`") end f_gen = generate_function(sys, dvs, ps; expression = Val{true}, expression_module = eval_module, kwargs...) f_oop, f_iip = eval_or_rgf.(f_gen; eval_expression, eval_module) - f(u, p, t) = f_oop(u, p, t) - f(du, u, p, t) = f_iip(du, u, p, t) + f(u_next, u, p, t) = f_oop(u_next, u, p, t) + f(resid, u_next, u, p, t) = f_iip(resid, u_next, u, p, t) if specialize === SciMLBase.FunctionWrapperSpecialize && iip if u0 === nothing || p === nothing || t === nothing @@ -379,8 +399,8 @@ struct ImplicitDiscreteFunctionClosure{O, I} <: Function f_oop::O f_iip::I end -(f::ImplicitDiscreteFunctionClosure)(u, p, t) = f.f_oop(u, p, t) -(f::ImplicitDiscreteFunctionClosure)(du, u, p, t) = f.f_iip(du, u, p, t) +(f::ImplicitDiscreteFunctionClosure)(u_next, u, p, t) = f.f_oop(u_next, u, p, t) +(f::ImplicitDiscreteFunctionClosure)(resid, u_next, u, p, t) = f.f_iip(resid, u_next, u, p, t) function ImplicitDiscreteFunctionExpr{iip}(sys::ImplicitDiscreteSystem, dvs = unknowns(sys), ps = parameters(sys), u0 = nothing; diff --git a/src/systems/systemstructure.jl b/src/systems/systemstructure.jl index 1bdc11f06a..215d843c2e 100644 --- a/src/systems/systemstructure.jl +++ b/src/systems/systemstructure.jl @@ -432,7 +432,7 @@ function TearingState(sys; quick_cancel = false, check = true) SystemStructure(complete(var_to_diff), complete(eq_to_diff), complete(graph), nothing, var_types, sys isa DiscreteSystem), Any[]) - if sys isa DiscreteSystem + if sys isa DiscreteSystem || sys isa ImplicitDiscreteSystem ts = shift_discrete_system(ts) end return ts @@ -456,6 +456,8 @@ function lower_order_var(dervar, t) diffvar end +""" +""" function shift_discrete_system(ts::TearingState) @unpack fullvars, sys = ts discvars = OrderedSet() diff --git a/test/implicit_discrete_system.jl b/test/implicit_discrete_system.jl new file mode 100644 index 0000000000..adfb9d2fc1 --- /dev/null +++ b/test/implicit_discrete_system.jl @@ -0,0 +1,2 @@ + +#init diff --git a/test/runtests.jl b/test/runtests.jl index 52875fdae5..bd5eecacd0 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -79,7 +79,8 @@ end @safetestset "Variable Utils Test" include("variable_utils.jl") @safetestset "Variable Metadata Test" include("test_variable_metadata.jl") @safetestset "OptimizationSystem Test" include("optimizationsystem.jl") - @safetestset "Discrete System" include("discrete_system.jl") + @safetestset "DiscreteSystem Test" include("discrete_system.jl") + @safetestset "ImplicitDiscreteSystem Test" include("implicit_discrete_system.jl") @safetestset "SteadyStateSystem Test" include("steadystatesystems.jl") @safetestset "SDESystem Test" include("sdesystem.jl") @safetestset "DDESystem Test" include("dde.jl") From 397298fea4c78b4ccfbfc6f8d1c61fde5aabe16d Mon Sep 17 00:00:00 2001 From: vyudu Date: Thu, 30 Jan 2025 11:34:02 -0500 Subject: [PATCH 0697/2176] working on structural simplification --- src/ModelingToolkit.jl | 1 + src/systems/discrete_system/discrete_system.jl | 2 +- src/systems/discrete_system/implicit_discrete_system.jl | 8 ++++---- src/systems/systemstructure.jl | 4 ++-- 4 files changed, 8 insertions(+), 7 deletions(-) diff --git a/src/ModelingToolkit.jl b/src/ModelingToolkit.jl index 4083f2609d..2dbef8f272 100644 --- a/src/ModelingToolkit.jl +++ b/src/ModelingToolkit.jl @@ -123,6 +123,7 @@ abstract type AbstractTimeIndependentSystem <: AbstractSystem end abstract type AbstractODESystem <: AbstractTimeDependentSystem end abstract type AbstractMultivariateSystem <: AbstractSystem end abstract type AbstractOptimizationSystem <: AbstractTimeIndependentSystem end +abstract type AbstractDiscreteSystem <: AbstractTimeDependentSystem end function independent_variable end diff --git a/src/systems/discrete_system/discrete_system.jl b/src/systems/discrete_system/discrete_system.jl index 0ad3317ea1..f2d271dfe4 100644 --- a/src/systems/discrete_system/discrete_system.jl +++ b/src/systems/discrete_system/discrete_system.jl @@ -17,7 +17,7 @@ eqs = [x(k+1) ~ σ*(y-x), @named de = DiscreteSystem(eqs) ``` """ -struct DiscreteSystem <: AbstractTimeDependentSystem +struct DiscreteSystem <: AbstractDiscreteSystem """ A tag for the system. If two systems have the same tag, then they are structurally identical. diff --git a/src/systems/discrete_system/implicit_discrete_system.jl b/src/systems/discrete_system/implicit_discrete_system.jl index 1e1b5d5f6f..3a137eb8c1 100644 --- a/src/systems/discrete_system/implicit_discrete_system.jl +++ b/src/systems/discrete_system/implicit_discrete_system.jl @@ -10,13 +10,13 @@ using ModelingToolkit: t_nounits as t @parameters σ=28.0 ρ=10.0 β=8/3 δt=0.1 @variables x(t)=1.0 y(t)=0.0 z(t)=0.0 k = ShiftIndex(t) -eqs = [x ~ σ*(y(k-1)-x), - y ~ x*(ρ-z(k-1))-y, - z ~ x(k-1)*y - β*z] +eqs = [x ~ σ*(y-x), + y ~ x*(ρ-z)-y, + z ~ x*y - β*z] @named ide = ImplicitDiscreteSystem(eqs,t,[x,y,z],[σ,ρ,β]; tspan = (0, 1000.0)) ``` """ -struct ImplicitDiscreteSystem <: AbstractTimeDependentSystem +struct ImplicitDiscreteSystem <: AbstractDiscreteSystem """ A tag for the system. If two systems have the same tag, then they are structurally identical. diff --git a/src/systems/systemstructure.jl b/src/systems/systemstructure.jl index 215d843c2e..86d1702041 100644 --- a/src/systems/systemstructure.jl +++ b/src/systems/systemstructure.jl @@ -430,9 +430,9 @@ function TearingState(sys; quick_cancel = false, check = true) ts = TearingState(sys, fullvars, SystemStructure(complete(var_to_diff), complete(eq_to_diff), - complete(graph), nothing, var_types, sys isa DiscreteSystem), + complete(graph), nothing, var_types, sys isa AbstractDiscreteSystem), Any[]) - if sys isa DiscreteSystem || sys isa ImplicitDiscreteSystem + if sys isa AbstractDiscreteSystem ts = shift_discrete_system(ts) end return ts From 2ae79ae19941d5502a7a83d13770284cad7b2cc3 Mon Sep 17 00:00:00 2001 From: vyudu Date: Thu, 30 Jan 2025 11:34:36 -0500 Subject: [PATCH 0698/2176] revert to OrdinaryDiffEq --- test/bvproblem.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/bvproblem.jl b/test/bvproblem.jl index 30fde44531..069372aef7 100644 --- a/test/bvproblem.jl +++ b/test/bvproblem.jl @@ -1,6 +1,6 @@ ### TODO: update when BoundaryValueDiffEqAscher is updated to use the normal boundary condition conventions -using BoundaryValueDiffEq, OrdinaryDiffEqDefault, BoundaryValueDiffEqAscher +using BoundaryValueDiffEq, OrdinaryDiffEq, BoundaryValueDiffEqAscher using BenchmarkTools using ModelingToolkit using SciMLBase From be1b270298f11b23306cf4b5a9ea677a0ba26894 Mon Sep 17 00:00:00 2001 From: vyudu Date: Thu, 30 Jan 2025 14:26:04 -0500 Subject: [PATCH 0699/2176] change variable renaming --- src/structural_transformation/utils.jl | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/structural_transformation/utils.jl b/src/structural_transformation/utils.jl index bd24a1d017..29c0f66756 100644 --- a/src/structural_transformation/utils.jl +++ b/src/structural_transformation/utils.jl @@ -452,8 +452,15 @@ end function lower_varname_withshift(var, iv, order) order == 0 && return var if ModelingToolkit.isoperator(var, ModelingToolkit.Shift) - op = operation(var) - return Shift(op.t, order)(var) + O = only(arguments(var)) + oldop = operation(O) + ds = "$iv-$order" + d_separator = 'ˍ' + newname = Symbol(string(nameof(oldop)), d_separator, ds) + + newvar = maketerm(typeof(O), Symbolics.rename(oldop, newname), Symbolics.children(O), Symbolics.metadata(O)) + setmetadata(newvar, Symbolics.VariableSource, (:variables, newname)) + return ModelingToolkit._with_unit(identity, newvar, iv) end return lower_varname_with_unit(var, iv, order) end From cc23c888984a3e811d8be32f7f229b214dd7c7b0 Mon Sep 17 00:00:00 2001 From: vyudu Date: Thu, 30 Jan 2025 14:29:14 -0500 Subject: [PATCH 0700/2176] up --- src/systems/discrete_system/discrete_system.jl | 3 +-- src/systems/systems.jl | 14 +++++++------- src/systems/systemstructure.jl | 6 ++---- 3 files changed, 10 insertions(+), 13 deletions(-) diff --git a/src/systems/discrete_system/discrete_system.jl b/src/systems/discrete_system/discrete_system.jl index f2d271dfe4..bd5c72eec7 100644 --- a/src/systems/discrete_system/discrete_system.jl +++ b/src/systems/discrete_system/discrete_system.jl @@ -17,7 +17,7 @@ eqs = [x(k+1) ~ σ*(y-x), @named de = DiscreteSystem(eqs) ``` """ -struct DiscreteSystem <: AbstractDiscreteSystem +struct DiscreteSystem <: AbstractTimeDependentSystem """ A tag for the system. If two systems have the same tag, then they are structurally identical. @@ -233,7 +233,6 @@ function DiscreteSystem(eqs, iv; kwargs...) push!(new_ps, p) end end - @show allunknowns return DiscreteSystem(eqs, iv, collect(allunknowns), collect(new_ps); kwargs...) end diff --git a/src/systems/systems.jl b/src/systems/systems.jl index 5ff28eba26..9c8c272c5c 100644 --- a/src/systems/systems.jl +++ b/src/systems/systems.jl @@ -39,13 +39,13 @@ function structural_simplify( else newsys = newsys′ end - # if newsys isa DiscreteSystem && - # any(eq -> symbolic_type(eq.lhs) == NotSymbolic(), equations(newsys)) - # error(""" - # Encountered algebraic equations when simplifying discrete system. This is \ - # not yet supported. - # """) - # end + if newsys isa DiscreteSystem && + any(eq -> symbolic_type(eq.lhs) == NotSymbolic(), equations(newsys)) + error(""" + Encountered algebraic equations when simplifying discrete system. This is \ + not yet supported. + """) + end for pass in additional_passes newsys = pass(newsys) end diff --git a/src/systems/systemstructure.jl b/src/systems/systemstructure.jl index 86d1702041..1bdc11f06a 100644 --- a/src/systems/systemstructure.jl +++ b/src/systems/systemstructure.jl @@ -430,9 +430,9 @@ function TearingState(sys; quick_cancel = false, check = true) ts = TearingState(sys, fullvars, SystemStructure(complete(var_to_diff), complete(eq_to_diff), - complete(graph), nothing, var_types, sys isa AbstractDiscreteSystem), + complete(graph), nothing, var_types, sys isa DiscreteSystem), Any[]) - if sys isa AbstractDiscreteSystem + if sys isa DiscreteSystem ts = shift_discrete_system(ts) end return ts @@ -456,8 +456,6 @@ function lower_order_var(dervar, t) diffvar end -""" -""" function shift_discrete_system(ts::TearingState) @unpack fullvars, sys = ts discvars = OrderedSet() From f5e5aff51476b66e8fc152ed9b8c8fb4307e8f4c Mon Sep 17 00:00:00 2001 From: vyudu Date: Thu, 30 Jan 2025 14:30:08 -0500 Subject: [PATCH 0701/2176] revert all --- src/ModelingToolkit.jl | 3 - .../implicit_discrete_system.jl | 426 ------------------ test/implicit_discrete_system.jl | 2 - test/runtests.jl | 3 +- 4 files changed, 1 insertion(+), 433 deletions(-) delete mode 100644 src/systems/discrete_system/implicit_discrete_system.jl delete mode 100644 test/implicit_discrete_system.jl diff --git a/src/ModelingToolkit.jl b/src/ModelingToolkit.jl index 2dbef8f272..2710d7d1e4 100644 --- a/src/ModelingToolkit.jl +++ b/src/ModelingToolkit.jl @@ -123,7 +123,6 @@ abstract type AbstractTimeIndependentSystem <: AbstractSystem end abstract type AbstractODESystem <: AbstractTimeDependentSystem end abstract type AbstractMultivariateSystem <: AbstractSystem end abstract type AbstractOptimizationSystem <: AbstractTimeIndependentSystem end -abstract type AbstractDiscreteSystem <: AbstractTimeDependentSystem end function independent_variable end @@ -165,7 +164,6 @@ include("systems/diffeqs/modelingtoolkitize.jl") include("systems/diffeqs/basic_transformations.jl") include("systems/discrete_system/discrete_system.jl") -include("systems/discrete_system/implicit_discrete_system.jl") include("systems/jumps/jumpsystem.jl") @@ -231,7 +229,6 @@ export DAEFunctionExpr, DAEProblemExpr export SDESystem, SDEFunction, SDEFunctionExpr, SDEProblemExpr export SystemStructure export DiscreteSystem, DiscreteProblem, DiscreteFunction, DiscreteFunctionExpr -export ImplicitDiscreteSystem, ImplicitDiscreteProblem, ImplicitDiscreteFunction, ImplicitDiscreteFunctionExpr export JumpSystem export ODEProblem, SDEProblem export NonlinearFunction, NonlinearFunctionExpr diff --git a/src/systems/discrete_system/implicit_discrete_system.jl b/src/systems/discrete_system/implicit_discrete_system.jl deleted file mode 100644 index 3a137eb8c1..0000000000 --- a/src/systems/discrete_system/implicit_discrete_system.jl +++ /dev/null @@ -1,426 +0,0 @@ -""" -$(TYPEDEF) -An implicit system of difference equations. -# Fields -$(FIELDS) -# Example -``` -using ModelingToolkit -using ModelingToolkit: t_nounits as t -@parameters σ=28.0 ρ=10.0 β=8/3 δt=0.1 -@variables x(t)=1.0 y(t)=0.0 z(t)=0.0 -k = ShiftIndex(t) -eqs = [x ~ σ*(y-x), - y ~ x*(ρ-z)-y, - z ~ x*y - β*z] -@named ide = ImplicitDiscreteSystem(eqs,t,[x,y,z],[σ,ρ,β]; tspan = (0, 1000.0)) -``` -""" -struct ImplicitDiscreteSystem <: AbstractDiscreteSystem - """ - A tag for the system. If two systems have the same tag, then they are - structurally identical. - """ - tag::UInt - """The difference equations defining the discrete system.""" - eqs::Vector{Equation} - """Independent variable.""" - iv::BasicSymbolic{Real} - """Dependent (state) variables. Must not contain the independent variable.""" - unknowns::Vector - """Parameter variables. Must not contain the independent variable.""" - ps::Vector - """Time span.""" - tspan::Union{NTuple{2, Any}, Nothing} - """Array variables.""" - var_to_name::Any - """Observed states.""" - observed::Vector{Equation} - """ - The name of the system - """ - name::Symbol - """ - A description of the system. - """ - description::String - """ - The internal systems. These are required to have unique names. - """ - systems::Vector{ImplicitDiscreteSystem} - """ - The default values to use when initial conditions and/or - parameters are not supplied in `ImplicitDiscreteProblem`. - """ - defaults::Dict - """ - The guesses to use as the initial conditions for the - initialization system. - """ - guesses::Dict - """ - The system for performing the initialization. - """ - initializesystem::Union{Nothing, NonlinearSystem} - """ - Extra equations to be enforced during the initialization sequence. - """ - initialization_eqs::Vector{Equation} - """ - Inject assignment statements before the evaluation of the RHS function. - """ - preface::Any - """ - Type of the system. - """ - connector_type::Any - """ - Topologically sorted parameter dependency equations, where all symbols are parameters and - the LHS is a single parameter. - """ - parameter_dependencies::Vector{Equation} - """ - Metadata for the system, to be used by downstream packages. - """ - metadata::Any - """ - Metadata for MTK GUI. - """ - gui_metadata::Union{Nothing, GUIMetadata} - """ - Cache for intermediate tearing state. - """ - tearing_state::Any - """ - Substitutions generated by tearing. - """ - substitutions::Any - """ - If a model `sys` is complete, then `sys.x` no longer performs namespacing. - """ - complete::Bool - """ - Cached data for fast symbolic indexing. - """ - index_cache::Union{Nothing, IndexCache} - """ - The hierarchical parent system before simplification. - """ - parent::Any - isscheduled::Bool - - function ImplicitDiscreteSystem(tag, discreteEqs, iv, dvs, ps, tspan, var_to_name, - observed, name, description, systems, defaults, guesses, initializesystem, - initialization_eqs, preface, connector_type, parameter_dependencies = Equation[], - metadata = nothing, gui_metadata = nothing, - tearing_state = nothing, substitutions = nothing, - complete = false, index_cache = nothing, parent = nothing, - isscheduled = false; - checks::Union{Bool, Int} = true) - if checks == true || (checks & CheckComponents) > 0 - check_independent_variables([iv]) - check_variables(dvs, iv) - check_parameters(ps, iv) - end - if checks == true || (checks & CheckUnits) > 0 - u = __get_unit_type(dvs, ps, iv) - check_units(u, discreteEqs) - end - new(tag, discreteEqs, iv, dvs, ps, tspan, var_to_name, observed, name, description, - systems, defaults, guesses, initializesystem, initialization_eqs, - preface, connector_type, parameter_dependencies, metadata, gui_metadata, - tearing_state, substitutions, complete, index_cache, parent, isscheduled) - end -end - -""" - $(TYPEDSIGNATURES) - -Constructs a ImplicitDiscreteSystem. -""" -function ImplicitDiscreteSystem(eqs::AbstractVector{<:Equation}, iv, dvs, ps; - observed = Num[], - systems = ImplicitDiscreteSystem[], - tspan = nothing, - name = nothing, - description = "", - default_u0 = Dict(), - default_p = Dict(), - guesses = Dict(), - initializesystem = nothing, - initialization_eqs = Equation[], - defaults = _merge(Dict(default_u0), Dict(default_p)), - preface = nothing, - connector_type = nothing, - parameter_dependencies = Equation[], - metadata = nothing, - gui_metadata = nothing, - kwargs...) - name === nothing && - throw(ArgumentError("The `name` keyword must be provided. Please consider using the `@named` macro")) - iv′ = value(iv) - dvs′ = value.(dvs) - ps′ = value.(ps) - if any(hasderiv, eqs) || any(hashold, eqs) || any(hassample, eqs) || any(hasdiff, eqs) - error("Equations in a `ImplicitDiscreteSystem` can only have `Shift` operators.") - end - if !(isempty(default_u0) && isempty(default_p)) - Base.depwarn( - "`default_u0` and `default_p` are deprecated. Use `defaults` instead.", - :ImplicitDiscreteSystem, force = true) - end - - # Copy equations to canonical form, but do not touch array expressions - eqs = [wrap(eq.lhs) isa Symbolics.Arr ? eq : 0 ~ eq.rhs - eq.lhs for eq in eqs] - defaults = Dict{Any, Any}(todict(defaults)) - guesses = Dict{Any, Any}(todict(guesses)) - var_to_name = Dict() - process_variables!(var_to_name, defaults, guesses, dvs′) - process_variables!(var_to_name, defaults, guesses, ps′) - process_variables!( - var_to_name, defaults, guesses, [eq.lhs for eq in parameter_dependencies]) - process_variables!( - var_to_name, defaults, guesses, [eq.rhs for eq in parameter_dependencies]) - defaults = Dict{Any, Any}(value(k) => value(v) - for (k, v) in pairs(defaults) if v !== nothing) - guesses = Dict{Any, Any}(value(k) => value(v) - for (k, v) in pairs(guesses) if v !== nothing) - - isempty(observed) || collect_var_to_name!(var_to_name, (eq.lhs for eq in observed)) - - sysnames = nameof.(systems) - if length(unique(sysnames)) != length(sysnames) - throw(ArgumentError("System names must be unique.")) - end - ImplicitDiscreteSystem(Threads.atomic_add!(SYSTEM_COUNT, UInt(1)), - eqs, iv′, dvs′, ps′, tspan, var_to_name, observed, name, description, systems, - defaults, guesses, initializesystem, initialization_eqs, preface, connector_type, - parameter_dependencies, metadata, gui_metadata, kwargs...) -end - -function ImplicitDiscreteSystem(eqs, iv; kwargs...) - eqs = collect(eqs) - diffvars = OrderedSet() - allunknowns = OrderedSet() - ps = OrderedSet() - iv = value(iv) - for eq in eqs - collect_vars!(allunknowns, ps, eq, iv; op = Shift) - if iscall(eq.lhs) && operation(eq.lhs) isa Shift - isequal(iv, operation(eq.lhs).t) || - throw(ArgumentError("An ImplicitDiscreteSystem can only have one independent variable.")) - eq.lhs in diffvars && - throw(ArgumentError("The shift variable $(eq.lhs) is not unique in the system of equations.")) - push!(diffvars, eq.lhs) - end - end - for eq in get(kwargs, :parameter_dependencies, Equation[]) - if eq isa Pair - collect_vars!(allunknowns, ps, eq, iv) - else - collect_vars!(allunknowns, ps, eq, iv) - end - end - new_ps = OrderedSet() - for p in ps - if iscall(p) && operation(p) === getindex - par = arguments(p)[begin] - if Symbolics.shape(Symbolics.unwrap(par)) !== Symbolics.Unknown() && - all(par[i] in ps for i in eachindex(par)) - push!(new_ps, par) - else - push!(new_ps, p) - end - else - push!(new_ps, p) - end - end - return ImplicitDiscreteSystem(eqs, iv, - collect(allunknowns), collect(new_ps); kwargs...) -end -# basically at every timestep it should build a nonlinear solve -# Previous timesteps should be treated as parameters? is this right? - -function flatten(sys::ImplicitDiscreteSystem, noeqs = false) - systems = get_systems(sys) - if isempty(systems) - return sys - else - return ImplicitDiscreteSystem(noeqs ? Equation[] : equations(sys), - get_iv(sys), - unknowns(sys), - parameters(sys), - observed = observed(sys), - defaults = defaults(sys), - guesses = guesses(sys), - initialization_eqs = initialization_equations(sys), - name = nameof(sys), - description = description(sys), - metadata = get_metadata(sys), - checks = false) - end -end - -function generate_function( - sys::ImplicitDiscreteSystem, dvs = unknowns(sys), ps = parameters(sys); wrap_code = identity, kwargs...) - if !iscomplete(sys) - error("A completed system is required. Call `complete` or `structural_simplify` on the system.") - end - p = (reorder_parameters(sys, unwrap.(ps))..., cachesyms...) - isscalar = !(exprs isa AbstractArray) - pre, sol_states = get_substitutions_and_solved_unknowns(sys, isscalar ? [exprs] : exprs) - if postprocess_fbody === nothing - postprocess_fbody = pre - end - if states === nothing - states = sol_states - end - exprs = [eq.lhs - eq.rhs for eq in equations(sys)] - u = map(Shift(iv, -1), dvs) - u_next = dvs - - wrap_code = wrap_code .∘ wrap_array_vars(sys, exprs) .∘ wrap_parameter_dependencies(sys, false) - - build_function(exprs, u_next, u, p..., get_iv(sys)) -end - -function shift_u0map_forward(sys::ImplicitDiscreteSystem, u0map, defs) - iv = get_iv(sys) - updated = AnyDict() - for k in collect(keys(u0map)) - v = u0map[k] - if !((op = operation(k)) isa Shift) - error("Initial conditions must be for the past state of the unknowns. Instead of providing the condition for $k, provide the condition for $(Shift(iv, -1)(k)).") - end - updated[Shift(iv, op.steps + 1)(arguments(k)[1])] = v - end - for var in unknowns(sys) - op = operation(var) - op isa Shift || continue - haskey(updated, var) && continue - root = first(arguments(var)) - haskey(defs, root) || error("Initial condition for $var not provided.") - updated[var] = defs[root] - end - return updated -end - -""" - $(TYPEDSIGNATURES) -Generates an ImplicitDiscreteProblem from an ImplicitDiscreteSystem. -""" -function SciMLBase.ImplicitDiscreteProblem( - sys::ImplicitDiscreteSystem, u0map = [], tspan = get_tspan(sys), - parammap = SciMLBase.NullParameters(); - eval_module = @__MODULE__, - eval_expression = false, - use_union = false, - kwargs... -) - if !iscomplete(sys) - error("A completed `ImplicitDiscreteSystem` is required. Call `complete` or `structural_simplify` on the system before creating a `ImplicitDiscreteProblem`") - end - dvs = unknowns(sys) - ps = parameters(sys) - eqs = equations(sys) - iv = get_iv(sys) - - u0map = to_varmap(u0map, dvs) - u0map = shift_u0map_forward(sys, u0map, defaults(sys)) - f, u0, p = process_SciMLProblem( - ImplicitDiscreteFunction, sys, u0map, parammap; eval_expression, eval_module) - u0 = f(u0, p, tspan[1]) - NonlinearProblem(f, u0, tspan, p; kwargs...) -end - -function SciMLBase.ImplicitDiscreteFunction(sys::ImplicitDiscreteSystem, args...; kwargs...) - ImplicitDiscreteFunction{true}(sys, args...; kwargs...) -end - -function SciMLBase.ImplicitDiscreteFunction{true}(sys::ImplicitDiscreteSystem, args...; kwargs...) - ImplicitDiscreteFunction{true, SciMLBase.AutoSpecialize}(sys, args...; kwargs...) -end - -function SciMLBase.ImplicitDiscreteFunction{false}(sys::ImplicitDiscreteSystem, args...; kwargs...) - ImplicitDiscreteFunction{false, SciMLBase.FullSpecialize}(sys, args...; kwargs...) -end -function SciMLBase.ImplicitDiscreteFunction{iip, specialize}( - sys::ImplicitDiscreteSystem, - dvs = unknowns(sys), - ps = parameters(sys), - u0 = nothing; - version = nothing, - p = nothing, - t = nothing, - eval_expression = false, - eval_module = @__MODULE__, - analytic = nothing, - kwargs...) where {iip, specialize} - - if !iscomplete(sys) - error("A completed `ImplicitDiscreteSystem` is required. Call `complete` or `structural_simplify` on the system before creating a `ImplicitDiscreteProblem`") - end - f_gen = generate_function(sys, dvs, ps; expression = Val{true}, - expression_module = eval_module, kwargs...) - f_oop, f_iip = eval_or_rgf.(f_gen; eval_expression, eval_module) - f(u_next, u, p, t) = f_oop(u_next, u, p, t) - f(resid, u_next, u, p, t) = f_iip(resid, u_next, u, p, t) - - if specialize === SciMLBase.FunctionWrapperSpecialize && iip - if u0 === nothing || p === nothing || t === nothing - error("u0, p, and t must be specified for FunctionWrapperSpecialize on ImplicitDiscreteFunction.") - end - f = SciMLBase.wrapfun_iip(f, (u0, u0, p, t)) - end - - observedfun = ObservedFunctionCache( - sys; eval_expression, eval_module, checkbounds = get(kwargs, :checkbounds, false)) - - ImplicitDiscreteFunction{iip, specialize}(f; - sys = sys, - observed = observedfun, - analytic = analytic) -end - -""" -```julia -ImplicitDiscreteFunctionExpr{iip}(sys::ImplicitDiscreteSystem, dvs = states(sys), - ps = parameters(sys); - version = nothing, - kwargs...) where {iip} -``` - -Create a Julia expression for an `ImplicitDiscreteFunction` from the [`ImplicitDiscreteSystem`](@ref). -The arguments `dvs` and `ps` are used to set the order of the dependent -variable and parameter vectors, respectively. -""" -struct ImplicitDiscreteFunctionExpr{iip} end -struct ImplicitDiscreteFunctionClosure{O, I} <: Function - f_oop::O - f_iip::I -end -(f::ImplicitDiscreteFunctionClosure)(u_next, u, p, t) = f.f_oop(u_next, u, p, t) -(f::ImplicitDiscreteFunctionClosure)(resid, u_next, u, p, t) = f.f_iip(resid, u_next, u, p, t) - -function ImplicitDiscreteFunctionExpr{iip}(sys::ImplicitDiscreteSystem, dvs = unknowns(sys), - ps = parameters(sys), u0 = nothing; - version = nothing, p = nothing, - linenumbers = false, - simplify = false, - kwargs...) where {iip} - f_oop, f_iip = generate_function(sys, dvs, ps; expression = Val{true}, kwargs...) - - fsym = gensym(:f) - _f = :($fsym = $ImplicitDiscreteFunctionClosure($f_oop, $f_iip)) - - ex = quote - $_f - ImplicitDiscreteFunction{$iip}($fsym) - end - !linenumbers ? Base.remove_linenums!(ex) : ex -end - -function ImplicitDiscreteFunctionExpr(sys::ImplicitDiscreteSystem, args...; kwargs...) - ImplicitDiscreteFunctionExpr{true}(sys, args...; kwargs...) -end - diff --git a/test/implicit_discrete_system.jl b/test/implicit_discrete_system.jl deleted file mode 100644 index adfb9d2fc1..0000000000 --- a/test/implicit_discrete_system.jl +++ /dev/null @@ -1,2 +0,0 @@ - -#init diff --git a/test/runtests.jl b/test/runtests.jl index bd5eecacd0..52875fdae5 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -79,8 +79,7 @@ end @safetestset "Variable Utils Test" include("variable_utils.jl") @safetestset "Variable Metadata Test" include("test_variable_metadata.jl") @safetestset "OptimizationSystem Test" include("optimizationsystem.jl") - @safetestset "DiscreteSystem Test" include("discrete_system.jl") - @safetestset "ImplicitDiscreteSystem Test" include("implicit_discrete_system.jl") + @safetestset "Discrete System" include("discrete_system.jl") @safetestset "SteadyStateSystem Test" include("steadystatesystems.jl") @safetestset "SDESystem Test" include("sdesystem.jl") @safetestset "DDESystem Test" include("dde.jl") From 690a25522fa898b1b046947bd44a17c250aa6833 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 31 Jan 2025 18:03:40 +0530 Subject: [PATCH 0702/2176] refactor: format --- src/systems/diffeqs/odesystem.jl | 5 +++-- src/systems/diffeqs/sdesystem.jl | 3 +-- src/systems/nonlinear/nonlinearsystem.jl | 6 ++++-- test/fmi/fmi.jl | 5 ++++- test/initial_values.jl | 3 ++- test/odesystem.jl | 20 ++++++++++---------- test/sdesystem.jl | 15 ++++++++------- 7 files changed, 32 insertions(+), 25 deletions(-) diff --git a/src/systems/diffeqs/odesystem.jl b/src/systems/diffeqs/odesystem.jl index 41bcaf1d01..096ee30588 100644 --- a/src/systems/diffeqs/odesystem.jl +++ b/src/systems/diffeqs/odesystem.jl @@ -374,7 +374,7 @@ function Base.:(==)(sys1::ODESystem, sys2::ODESystem) _eq_unordered(get_unknowns(sys1), get_unknowns(sys2)) && _eq_unordered(get_ps(sys1), get_ps(sys2)) && _eq_unordered(continuous_events(sys1), continuous_events(sys2)) && - _eq_unordered(discrete_events(sys1), discrete_events(sys2)) && + _eq_unordered(discrete_events(sys1), discrete_events(sys2)) && all(s1 == s2 for (s1, s2) in zip(get_systems(sys1), get_systems(sys2))) end @@ -631,7 +631,8 @@ function build_explicit_observed_function(sys, ts; oop_fn = Func(args, [], pre(Let(obsexprs, return_value, - false)), [Expr(:meta, :propagate_inbounds)]) |> array_wrapper[1] |> oop_mtkp_wrapper |> toexpr + false)), [Expr(:meta, :propagate_inbounds)]) |> array_wrapper[1] |> + oop_mtkp_wrapper |> toexpr if !checkbounds oop_fn.args[end] = quote diff --git a/src/systems/diffeqs/sdesystem.jl b/src/systems/diffeqs/sdesystem.jl index df950ee01c..f677850bf5 100644 --- a/src/systems/diffeqs/sdesystem.jl +++ b/src/systems/diffeqs/sdesystem.jl @@ -285,7 +285,7 @@ function Base.:(==)(sys1::SDESystem, sys2::SDESystem) _eq_unordered(get_unknowns(sys1), get_unknowns(sys2)) && _eq_unordered(get_ps(sys1), get_ps(sys2)) && _eq_unordered(continuous_events(sys1), continuous_events(sys2)) && - _eq_unordered(discrete_events(sys1), discrete_events(sys2)) && + _eq_unordered(discrete_events(sys1), discrete_events(sys2)) && all(s1 == s2 for (s1, s2) in zip(get_systems(sys1), get_systems(sys2))) end @@ -771,7 +771,6 @@ function DiffEqBase.SDEProblem{iip, specialize}( end function DiffEqBase.SDEProblem(sys::ODESystem, args...; kwargs...) - if any(ModelingToolkit.isbrownian, unknowns(sys)) error("SDESystem constructed by defining Brownian variables with @brownian must be simplified by calling `structural_simplify` before a SDEProblem can be constructed.") else diff --git a/src/systems/nonlinear/nonlinearsystem.jl b/src/systems/nonlinear/nonlinearsystem.jl index 89663f7dbc..58f61d3b22 100644 --- a/src/systems/nonlinear/nonlinearsystem.jl +++ b/src/systems/nonlinear/nonlinearsystem.jl @@ -710,7 +710,8 @@ function SciMLBase.SCCNonlinearProblem{iip}(sys::NonlinearSystem, u0map, # precomputed subexpressions should not contain `banned_vars` banned_vars = Set{Any}(vcat(_dvs, getproperty.(_obs, (:lhs,)))) filter!(banned_vars) do var - symbolic_type(var) != ArraySymbolic() || all(j -> var[j] in banned_vars, eachindex(var)) + symbolic_type(var) != ArraySymbolic() || + all(j -> var[j] in banned_vars, eachindex(var)) end state = Dict() for i in eachindex(_obs) @@ -772,7 +773,8 @@ function SciMLBase.SCCNonlinearProblem{iip}(sys::NonlinearSystem, u0map, _obs = scc_obs[i] cachevars = scc_cachevars[i] cacheexprs = scc_cacheexprs[i] - _prevobsidxs = vcat(_prevobsidxs, observed_equations_used_by(sys, reduce(vcat, values(cacheexprs); init = []))) + _prevobsidxs = vcat(_prevobsidxs, + observed_equations_used_by(sys, reduce(vcat, values(cacheexprs); init = []))) if isempty(cachevars) push!(explicitfuns, Returns(nothing)) diff --git a/test/fmi/fmi.jl b/test/fmi/fmi.jl index bf5b60459f..39f624f616 100644 --- a/test/fmi/fmi.jl +++ b/test/fmi/fmi.jl @@ -262,7 +262,10 @@ end @named adder2 = MTK.FMIComponent( Val(2); fmu, type = :CS, communication_step_size = 1e-3) sys, prob = build_looped_adders(adder1, adder2) - sol = solve(prob, Tsit5(); reltol = 1e-8, initializealg = SciMLBase.OverrideInit(nlsolve = FastShortcutNLLSPolyalg(autodiff = AutoFiniteDiff()))) + sol = solve(prob, + Tsit5(); + reltol = 1e-8, + initializealg = SciMLBase.OverrideInit(nlsolve = FastShortcutNLLSPolyalg(autodiff = AutoFiniteDiff()))) @test truesol(sol.t; idxs = [truesys.adder1.c, truesys.adder2.c]).u≈sol( sol.t; idxs = [sys.adder1.c, sys.adder2.c]).u rtol=1e-3 diff --git a/test/initial_values.jl b/test/initial_values.jl index c31eca33f8..4543f3549e 100644 --- a/test/initial_values.jl +++ b/test/initial_values.jl @@ -180,5 +180,6 @@ end @parameters p[1:2, 1:2] @mtkbuild sys = ODESystem(D(x) ~ p * x, t) # used to throw a `MethodError` complaining about `getindex(::Nothing, ::CartesianIndex{2})` - @test_throws ModelingToolkit.MissingParametersError ODEProblem(sys, [x => ones(2)], (0.0, 1.0)) + @test_throws ModelingToolkit.MissingParametersError ODEProblem( + sys, [x => ones(2)], (0.0, 1.0)) end diff --git a/test/odesystem.jl b/test/odesystem.jl index a635c3dad9..eb766a1380 100644 --- a/test/odesystem.jl +++ b/test/odesystem.jl @@ -1548,7 +1548,7 @@ end @testset "`isequal`" begin @variables X(t) @parameters p d - eq = D(X) ~ p - d*X + eq = D(X) ~ p - d * X osys1 = complete(ODESystem([eq], t; name = :osys)) osys2 = complete(ODESystem([eq], t; name = :osys)) @@ -1559,7 +1559,7 @@ end osys1 = complete(ODESystem([eq], t; name = :osys, continuous_events)) osys2 = complete(ODESystem([eq], t; name = :osys)) - @test osys1 !== osys2 + @test osys1 !== osys2 osys1 = complete(ODESystem([eq], t; name = :osys, discrete_events)) osys2 = complete(ODESystem([eq], t; name = :osys)) @@ -1567,28 +1567,28 @@ end osys1 = complete(ODESystem([eq], t; name = :osys, continuous_events)) osys2 = complete(ODESystem([eq], t; name = :osys, discrete_events)) - @test osys1 !== osys2 + @test osys1 !== osys2 end @testset "dae_order_lowering basic test" begin @parameters a @variables x(t) y(t) z(t) @named dae_sys = ODESystem([ - D(x) ~ y, - 0 ~ x + z, - 0 ~ x - y + z - ], t, [z, y, x], []) + D(x) ~ y, + 0 ~ x + z, + 0 ~ x - y + z + ], t, [z, y, x], []) lowered_dae_sys = dae_order_lowering(dae_sys) - @variables x1(t) y1(t) z1(t) + @variables x1(t) y1(t) z1(t) expected_eqs = [ 0 ~ x + z, 0 ~ x - y + z, Differential(t)(x) ~ y ] lowered_eqs = equations(lowered_dae_sys) - sorted_lowered_eqs = sort(lowered_eqs, by=string) - sorted_expected_eqs = sort(expected_eqs, by=string) + sorted_lowered_eqs = sort(lowered_eqs, by = string) + sorted_expected_eqs = sort(expected_eqs, by = string) @test sorted_lowered_eqs == sorted_expected_eqs expected_vars = Set([z, y, x]) diff --git a/test/sdesystem.jl b/test/sdesystem.jl index ade91b60b9..40a0a52c26 100644 --- a/test/sdesystem.jl +++ b/test/sdesystem.jl @@ -882,10 +882,10 @@ end 0.1 * z] @named sys = ODESystem(eqs, tt, [x, y, z], [σ, ρ, β]) - + @named de = SDESystem(eqs, noiseeqs, tt, [x, y, z], [σ, ρ, β], tspan = (0.0, 10.0)) de = complete(de) - + f = SDEFunctionExpr(de) @test f isa Expr @@ -915,7 +915,7 @@ end @variables X(t) @parameters p d @brownian a - seq = D(X) ~ p - d*X + a + seq = D(X) ~ p - d * X + a @mtkbuild ssys1 = System([seq], t; name = :ssys) @mtkbuild ssys2 = System([seq], t; name = :ssys) @test ssys1 == ssys2 # true @@ -925,7 +925,7 @@ end @mtkbuild ssys1 = System([seq], t; name = :ssys, continuous_events) @mtkbuild ssys2 = System([seq], t; name = :ssys) - @test ssys1 !== ssys2 + @test ssys1 !== ssys2 @mtkbuild ssys1 = System([seq], t; name = :ssys, discrete_events) @mtkbuild ssys2 = System([seq], t; name = :ssys) @@ -933,7 +933,7 @@ end @mtkbuild ssys1 = System([seq], t; name = :ssys, continuous_events) @mtkbuild ssys2 = System([seq], t; name = :ssys, discrete_events) - @test ssys1 !== ssys2 + @test ssys1 !== ssys2 end @testset "Error when constructing SDESystem without `structural_simplify`" begin @@ -950,7 +950,8 @@ end u0map = [x => 1.0, y => 0.0, z => 0.0] parammap = [σ => 10.0, β => 26.0, ρ => 2.33] - @test_throws ErrorException("SDESystem constructed by defining Brownian variables with @brownian must be simplified by calling `structural_simplify` before a SDEProblem can be constructed.") SDEProblem(de, u0map, (0.0, 100.0), parammap) + @test_throws ErrorException("SDESystem constructed by defining Brownian variables with @brownian must be simplified by calling `structural_simplify` before a SDEProblem can be constructed.") SDEProblem( + de, u0map, (0.0, 100.0), parammap) de = structural_simplify(de) @test SDEProblem(de, u0map, (0.0, 100.0), parammap) isa SDEProblem -end \ No newline at end of file +end From 6b7d5dcd5eac5d021478371001166c2c9bd0a6bc Mon Sep 17 00:00:00 2001 From: Christopher Rackauckas Date: Fri, 31 Jan 2025 08:25:13 -0500 Subject: [PATCH 0703/2176] Update Downstream.yml --- .github/workflows/Downstream.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.github/workflows/Downstream.yml b/.github/workflows/Downstream.yml index f0aefa87f6..0debdbdc7c 100644 --- a/.github/workflows/Downstream.yml +++ b/.github/workflows/Downstream.yml @@ -37,7 +37,6 @@ jobs: - {user: SciML, repo: MethodOfLines.jl, group: Interface} - {user: SciML, repo: MethodOfLines.jl, group: 2D_Diffusion} - {user: SciML, repo: MethodOfLines.jl, group: DAE} - - {user: ai4energy, repo: Ai4EComponentLib.jl, group: Downstream} steps: - uses: actions/checkout@v4 - uses: julia-actions/setup-julia@v1 From 2ab8ee8fd0e5280937cd38481055bb041c7dd450 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 30 Jan 2025 17:52:04 +0530 Subject: [PATCH 0704/2176] fix: handle scalarized array parameters in initialization --- src/systems/nonlinear/initializesystem.jl | 34 +++++++++++++++++------ test/initializationsystem.jl | 11 ++++++++ 2 files changed, 37 insertions(+), 8 deletions(-) diff --git a/src/systems/nonlinear/initializesystem.jl b/src/systems/nonlinear/initializesystem.jl index 296078bf43..0ead9b3908 100644 --- a/src/systems/nonlinear/initializesystem.jl +++ b/src/systems/nonlinear/initializesystem.jl @@ -112,9 +112,9 @@ function generate_initializesystem(sys::AbstractSystem; # If either of them are `missing` the parameter is an unknown # But if the parameter is passed a value, use that as an additional # equation in the system - _val1 = get(pmap, p, nothing) - _val2 = get(defs, p, nothing) - _val3 = get(guesses, p, nothing) + _val1 = get_possibly_array_fallback_singletons(pmap, p) + _val2 = get_possibly_array_fallback_singletons(defs, p) + _val3 = get_possibly_array_fallback_singletons(guesses, p) varp = tovar(p) paramsubs[p] = varp # Has a default of `missing`, and (either an equation using the value passed to `ODEProblem` or a guess) @@ -139,7 +139,7 @@ function generate_initializesystem(sys::AbstractSystem; error("Invalid setup: parameter $(p) has no default value, initial value, or guess") end # given a symbolic value to ODEProblem - elseif symbolic_type(_val1) != NotSymbolic() + elseif symbolic_type(_val1) != NotSymbolic() || is_array_of_symbolics(_val1) push!(eqs_ics, varp ~ _val1) push!(defs, varp => _val3) # No value passed to `ODEProblem`, but a default and a guess are present @@ -268,16 +268,34 @@ struct InitializationSystemMetadata oop_reconstruct_u0_p::Union{Nothing, ReconstructInitializeprob} end +function get_possibly_array_fallback_singletons(varmap, p) + if haskey(varmap, p) + return varmap[p] + end + symbolic_type(p) == ArraySymbolic() || return nothing + scal = collect(p) + if all(x -> haskey(varmap, x), scal) + res = [varmap[x] for x in scal] + if any(x -> x === nothing, res) + return nothing + elseif any(x -> x === missing, res) + return missing + end + return res + end + return nothing +end + function is_parameter_solvable(p, pmap, defs, guesses) p = unwrap(p) is_variable_floatingpoint(p) || return false - _val1 = pmap isa AbstractDict ? get(pmap, p, nothing) : nothing - _val2 = get(defs, p, nothing) - _val3 = get(guesses, p, nothing) + _val1 = pmap isa AbstractDict ? get_possibly_array_fallback_singletons(pmap, p) : nothing + _val2 = get_possibly_array_fallback_singletons(defs, p) + _val3 = get_possibly_array_fallback_singletons(guesses, p) # either (missing is a default or was passed to the ODEProblem) or (nothing was passed to # the ODEProblem and it has a default and a guess) return ((_val1 === missing || _val2 === missing) || - (symbolic_type(_val1) != NotSymbolic() || + (symbolic_type(_val1) != NotSymbolic() || is_array_of_symbolics(_val1) || _val1 === nothing && _val2 !== nothing)) && _val3 !== nothing end diff --git a/test/initializationsystem.jl b/test/initializationsystem.jl index def45a7abf..ff6620a8b1 100644 --- a/test/initializationsystem.jl +++ b/test/initializationsystem.jl @@ -1281,3 +1281,14 @@ end @test sol[S, 1] ≈ 999 @test SciMLBase.successful_retcode(sol) end + +@testset "Solvable array parameters with scalarized guesses" begin + @variables x(t) + @parameters p[1:2] q + @mtkbuild sys = ODESystem(D(x) ~ p[1] + p[2] + q, t; defaults = [p[1] => q, p[2] => 2q], guesses = [p[1] => q, p[2] => 2q]) + @test ModelingToolkit.is_parameter_solvable(p, Dict(), defaults(sys), guesses(sys)) + prob = ODEProblem(sys, [x => 1.0], (0.0, 1.0), [q => 2.0]) + @test length(ModelingToolkit.observed(prob.f.initialization_data.initializeprob.f.sys)) == 3 + sol = solve(prob, Tsit5()) + @test sol.ps[p] ≈ [2.0, 4.0] +end From 47b636c14d2b97dfc9a514eae1758d59bebdf63b Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Sat, 1 Feb 2025 03:07:55 +0530 Subject: [PATCH 0705/2176] refactor: format --- src/systems/diffeqs/odesystem.jl | 3 ++- src/systems/diffeqs/sdesystem.jl | 16 +++++++++++----- src/systems/nonlinear/initializesystem.jl | 3 ++- src/utils.jl | 3 ++- test/initializationsystem.jl | 7 +++++-- test/odesystem.jl | 2 +- test/sdesystem.jl | 2 +- 7 files changed, 24 insertions(+), 12 deletions(-) diff --git a/src/systems/diffeqs/odesystem.jl b/src/systems/diffeqs/odesystem.jl index 1a1a2367b7..c95e9f19db 100644 --- a/src/systems/diffeqs/odesystem.jl +++ b/src/systems/diffeqs/odesystem.jl @@ -327,7 +327,8 @@ function ODESystem(eqs, iv; kwargs...) end algevars = setdiff(allunknowns, diffvars) - return ODESystem(eqs, iv, collect(Iterators.flatten((diffvars, algevars))), collect(new_ps); kwargs...) + return ODESystem(eqs, iv, collect(Iterators.flatten((diffvars, algevars))), + collect(new_ps); kwargs...) end # NOTE: equality does not check cached Jacobian diff --git a/src/systems/diffeqs/sdesystem.jl b/src/systems/diffeqs/sdesystem.jl index 12d860af7f..0426a77858 100644 --- a/src/systems/diffeqs/sdesystem.jl +++ b/src/systems/diffeqs/sdesystem.jl @@ -273,7 +273,7 @@ function SDESystem(sys::ODESystem, neqs; kwargs...) SDESystem(equations(sys), neqs, get_iv(sys), unknowns(sys), parameters(sys); kwargs...) end -function SDESystem(eqs::Vector{Equation}, noiseeqs::AbstractArray, iv; kwargs...) +function SDESystem(eqs::Vector{Equation}, noiseeqs::AbstractArray, iv; kwargs...) diffvars, allunknowns, ps, eqs = process_equations(eqs, iv) for eq in get(kwargs, :parameter_dependencies, Equation[]) @@ -309,15 +309,21 @@ function SDESystem(eqs::Vector{Equation}, noiseeqs::AbstractArray, iv; kwargs... noiseps = OrderedSet() collect_vars!(noisedvs, noiseps, noiseeqs, iv) for dv in noisedvs - dv ∈ allunknowns || throw(ArgumentError("Variable $dv in noise equations is not an unknown of the system.")) + dv ∈ allunknowns || + throw(ArgumentError("Variable $dv in noise equations is not an unknown of the system.")) end algevars = setdiff(allunknowns, diffvars) - return SDESystem(eqs, noiseeqs, iv, Iterators.flatten((diffvars, algevars)), [ps; collect(noiseps)]; kwargs...) + return SDESystem(eqs, noiseeqs, iv, Iterators.flatten((diffvars, algevars)), + [ps; collect(noiseps)]; kwargs...) end -SDESystem(eq::Equation, noiseeqs::AbstractArray, args...; kwargs...) = SDESystem([eq], noiseeqs, args...; kwargs...) -SDESystem(eq::Equation, noiseeq, args...; kwargs...) = SDESystem([eq], [noiseeq], args...; kwargs...) +function SDESystem(eq::Equation, noiseeqs::AbstractArray, args...; kwargs...) + SDESystem([eq], noiseeqs, args...; kwargs...) +end +function SDESystem(eq::Equation, noiseeq, args...; kwargs...) + SDESystem([eq], [noiseeq], args...; kwargs...) +end function Base.:(==)(sys1::SDESystem, sys2::SDESystem) sys1 === sys2 && return true diff --git a/src/systems/nonlinear/initializesystem.jl b/src/systems/nonlinear/initializesystem.jl index 0ead9b3908..68c9d62f62 100644 --- a/src/systems/nonlinear/initializesystem.jl +++ b/src/systems/nonlinear/initializesystem.jl @@ -289,7 +289,8 @@ end function is_parameter_solvable(p, pmap, defs, guesses) p = unwrap(p) is_variable_floatingpoint(p) || return false - _val1 = pmap isa AbstractDict ? get_possibly_array_fallback_singletons(pmap, p) : nothing + _val1 = pmap isa AbstractDict ? get_possibly_array_fallback_singletons(pmap, p) : + nothing _val2 = get_possibly_array_fallback_singletons(defs, p) _val3 = get_possibly_array_fallback_singletons(guesses, p) # either (missing is a default or was passed to the ODEProblem) or (nothing was passed to diff --git a/src/utils.jl b/src/utils.jl index 7bb1ebcbe9..5edd8fe792 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -1225,7 +1225,8 @@ function process_equations(eqs, iv) throw(ArgumentError("An ODESystem can only have one independent variable.")) diffvar in diffvars && throw(ArgumentError("The differential variable $diffvar is not unique in the system of equations.")) - !(symtype(diffvar) === Real || eltype(symtype(diffvar)) === Real) && throw(ArgumentError("Differential variable $diffvar has type $(symtype(diffvar)). Differential variables should not be concretely typed.")) + !(symtype(diffvar) === Real || eltype(symtype(diffvar)) === Real) && + throw(ArgumentError("Differential variable $diffvar has type $(symtype(diffvar)). Differential variables should not be concretely typed.")) push!(diffvars, diffvar) end push!(diffeq, eq) diff --git a/test/initializationsystem.jl b/test/initializationsystem.jl index ff6620a8b1..026fd2c43d 100644 --- a/test/initializationsystem.jl +++ b/test/initializationsystem.jl @@ -1285,10 +1285,13 @@ end @testset "Solvable array parameters with scalarized guesses" begin @variables x(t) @parameters p[1:2] q - @mtkbuild sys = ODESystem(D(x) ~ p[1] + p[2] + q, t; defaults = [p[1] => q, p[2] => 2q], guesses = [p[1] => q, p[2] => 2q]) + @mtkbuild sys = ODESystem( + D(x) ~ p[1] + p[2] + q, t; defaults = [p[1] => q, p[2] => 2q], + guesses = [p[1] => q, p[2] => 2q]) @test ModelingToolkit.is_parameter_solvable(p, Dict(), defaults(sys), guesses(sys)) prob = ODEProblem(sys, [x => 1.0], (0.0, 1.0), [q => 2.0]) - @test length(ModelingToolkit.observed(prob.f.initialization_data.initializeprob.f.sys)) == 3 + @test length(ModelingToolkit.observed(prob.f.initialization_data.initializeprob.f.sys)) == + 3 sol = solve(prob, Tsit5()) @test sol.ps[p] ≈ [2.0, 4.0] end diff --git a/test/odesystem.jl b/test/odesystem.jl index 40515fe570..30465d2975 100644 --- a/test/odesystem.jl +++ b/test/odesystem.jl @@ -1547,7 +1547,7 @@ end @testset "Validate input types" begin @parameters p d @variables X(t)::Int64 - eq = D(X) ~ p - d*X + eq = D(X) ~ p - d * X @test_throws ArgumentError @mtkbuild osys = ODESystem([eq], t) @variables Y(t)[1:3]::String eq = D(Y) ~ [p, p, p] diff --git a/test/sdesystem.jl b/test/sdesystem.jl index 24c74c89cb..40b8747d39 100644 --- a/test/sdesystem.jl +++ b/test/sdesystem.jl @@ -874,7 +874,7 @@ end @parameters p d @variables X(t)::Int64 @brownian z - eq2 = D(X) ~ p - d*X + z + eq2 = D(X) ~ p - d * X + z @test_throws ArgumentError @mtkbuild ssys = System([eq2], t) noiseeq = [1] @test_throws ArgumentError @named ssys = SDESystem([eq2], [noiseeq], t) From b107e129ccb99b26ee493131d45d175af294a753 Mon Sep 17 00:00:00 2001 From: Herman Sletmoen Date: Sun, 2 Feb 2025 11:56:16 +0100 Subject: [PATCH 0706/2176] Add @description to @mtkmodel --- docs/src/basics/MTKLanguage.md | 8 ++++++++ docs/src/tutorials/acausal_components.md | 2 ++ src/systems/model_parsing.jl | 16 ++++++++++++++-- test/model_parsing.jl | 3 ++- 4 files changed, 26 insertions(+), 3 deletions(-) diff --git a/docs/src/basics/MTKLanguage.md b/docs/src/basics/MTKLanguage.md index 06250f90b4..2bcec99b6a 100644 --- a/docs/src/basics/MTKLanguage.md +++ b/docs/src/basics/MTKLanguage.md @@ -23,6 +23,7 @@ equations. `@mtkmodel` definition contains begin blocks of + - `@description`: for describing the whole system with a human-readable string - `@components`: for listing sub-components of the system - `@constants`: for declaring constants - `@defaults`: for passing `defaults` to ODESystem @@ -42,6 +43,7 @@ using ModelingToolkit using ModelingToolkit: t @mtkmodel ModelA begin + @description "A component with parameters `k` and `k_array`." @parameters begin k k_array[1:2] @@ -49,6 +51,7 @@ using ModelingToolkit: t end @mtkmodel ModelB begin + @description "A component with parameters `p1` and `p2`." @parameters begin p1 = 1.0, [description = "Parameter of ModelB"] p2 = 1.0, [description = "Parameter of ModelB"] @@ -56,6 +59,7 @@ end end @mtkmodel ModelC begin + @description "A bigger system that contains many more things." @icon "https://github.com/SciML/SciMLDocs/blob/main/docs/src/assets/logo.png" @constants begin c::Int = 1, [description = "Example constant."] @@ -91,6 +95,10 @@ end end ``` +#### `@description` + +A documenting `String` that summarizes and explains what the model is. + #### `@icon` An icon can be embedded in 3 ways: diff --git a/docs/src/tutorials/acausal_components.md b/docs/src/tutorials/acausal_components.md index 096b576d29..b97500a3e9 100644 --- a/docs/src/tutorials/acausal_components.md +++ b/docs/src/tutorials/acausal_components.md @@ -84,6 +84,7 @@ end end @mtkmodel RCModel begin + @description "A circuit with a constant voltage source, resistor and capacitor connected in series." @components begin resistor = Resistor(R = 1.0) capacitor = Capacitor(C = 1.0) @@ -251,6 +252,7 @@ make all of our parameter values 1.0. As `resistor`, `capacitor`, `source` lists ```@example acausal @mtkmodel RCModel begin + @description "A circuit with a constant voltage source, resistor and capacitor connected in series." @components begin resistor = Resistor(R = 1.0) capacitor = Capacitor(C = 1.0) diff --git a/src/systems/model_parsing.jl b/src/systems/model_parsing.jl index 52e46597bf..1976e89aa2 100644 --- a/src/systems/model_parsing.jl +++ b/src/systems/model_parsing.jl @@ -109,11 +109,13 @@ function _model_macro(mod, name, expr, isconnector) gui_metadata = isassigned(icon) > 0 ? GUIMetadata(GlobalRef(mod, name), icon[]) : GUIMetadata(GlobalRef(mod, name)) + description = get(dict, :description, "") + @inline pop_structure_dict!.( Ref(dict), [:constants, :defaults, :kwargs, :structural_parameters]) sys = :($ODESystem($(flatten_equations)(equations), $iv, variables, parameters; - name, systems, gui_metadata = $gui_metadata, defaults)) + name, description = $description, systems, gui_metadata = $gui_metadata, defaults)) if length(ext) == 0 push!(exprs.args, :(var"#___sys___" = $sys)) @@ -598,7 +600,9 @@ function parse_model!(exprs, comps, ext, eqs, icon, vs, ps, sps, c_evts, d_evts, dict, mod, arg, kwargs, where_types) mname = arg.args[1] body = arg.args[end] - if mname == Symbol("@components") + if mname == Symbol("@description") + parse_description!(body, dict) + elseif mname == Symbol("@components") parse_components!(exprs, comps, dict, body, kwargs) elseif mname == Symbol("@extend") parse_extend!(exprs, ext, dict, mod, body, kwargs) @@ -1156,6 +1160,14 @@ function parse_icon!(body::Symbol, dict, icon, mod) parse_icon!(getfield(mod, body), dict, icon, mod) end +function parse_description!(body, dict) + if body isa String + dict[:description] = body + else + error("Invalid description string $body") + end +end + ### Parsing Components: function component_args!(a, b, varexpr, kwargs; index_name = nothing) diff --git a/test/model_parsing.jl b/test/model_parsing.jl index baaa4651bf..559992d7c5 100644 --- a/test/model_parsing.jl +++ b/test/model_parsing.jl @@ -127,6 +127,7 @@ end end @mtkmodel RC begin + @description "An RC circuit." @structural_parameters begin R_val = 10u"Ω" C_val = 10u"F" @@ -139,7 +140,6 @@ end constant = Constant(; k = k_val) ground = MyMockModule.Ground() end - @equations begin connect(constant.output, source.V) connect(source.p, resistor.p) @@ -157,6 +157,7 @@ sol = solve(prob) defs = ModelingToolkit.defaults(rc) @test sol[rc.capacitor.v, end] ≈ defs[rc.constant.k] resistor = getproperty(rc, :resistor; namespace = false) +@test ModelingToolkit.description(rc) == "An RC circuit." @test getname(rc.resistor) === getname(resistor) @test getname(rc.resistor.R) === getname(resistor.R) @test getname(rc.resistor.v) === getname(resistor.v) From d90dda3482a3747d46c5ae77a0432336c9a83b09 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 30 Jan 2025 11:55:21 +0530 Subject: [PATCH 0707/2176] feat: allow specifying observed equations to `observed_equations_used_by` --- src/utils.jl | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/utils.jl b/src/utils.jl index 5edd8fe792..1f573ca0e8 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -1060,10 +1060,9 @@ Keyword arguments: variables which will be explored to find dependencies on observed equations. Typically, providing this keyword is not necessary and is only useful to avoid repeatedly calling `vars(exprs)` +- `obs`: the list of observed equations. """ -function observed_equations_used_by(sys::AbstractSystem, exprs; involved_vars = vars(exprs)) - obs = observed(sys) - +function observed_equations_used_by(sys::AbstractSystem, exprs; involved_vars = vars(exprs), obs = observed(sys)) obsvars = getproperty.(obs, :lhs) graph = observed_dependency_graph(obs) From cf406210f78fd7f0ea1785e79f1fc059ac011c0e Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 30 Jan 2025 11:41:43 +0530 Subject: [PATCH 0708/2176] fix: fix `observed_equations_used_by` for `DiscreteSystem` --- src/utils.jl | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/utils.jl b/src/utils.jl index 1f573ca0e8..e7259cc898 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -1062,7 +1062,8 @@ Keyword arguments: `vars(exprs)` - `obs`: the list of observed equations. """ -function observed_equations_used_by(sys::AbstractSystem, exprs; involved_vars = vars(exprs), obs = observed(sys)) +function observed_equations_used_by(sys::AbstractSystem, exprs; + involved_vars = vars(exprs; op = Union{Shift, Differential}), obs = observed(sys)) obsvars = getproperty.(obs, :lhs) graph = observed_dependency_graph(obs) From b50cb27963e6e85277c1903e6cfbe73b51df01f9 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 30 Jan 2025 11:55:44 +0530 Subject: [PATCH 0709/2176] feat: add `build_function_wrapper` for centralized codegen --- src/ModelingToolkit.jl | 1 + src/systems/codegen_utils.jl | 88 ++++++++++++++++++++++++++++++++++++ 2 files changed, 89 insertions(+) create mode 100644 src/systems/codegen_utils.jl diff --git a/src/ModelingToolkit.jl b/src/ModelingToolkit.jl index 2710d7d1e4..107f98f0ab 100644 --- a/src/ModelingToolkit.jl +++ b/src/ModelingToolkit.jl @@ -150,6 +150,7 @@ include("systems/connectors.jl") include("systems/analysis_points.jl") include("systems/imperative_affect.jl") include("systems/callbacks.jl") +include("systems/codegen_utils.jl") include("systems/problem_utils.jl") include("systems/nonlinear/nonlinearsystem.jl") diff --git a/src/systems/codegen_utils.jl b/src/systems/codegen_utils.jl new file mode 100644 index 0000000000..35a7c579a2 --- /dev/null +++ b/src/systems/codegen_utils.jl @@ -0,0 +1,88 @@ +function generated_argument_name(i::Int) + return Symbol(:__mtk_arg_, i) +end + +function array_variable_assignments(args...) + var_to_arridxs = Dict{BasicSymbolic, Array{Tuple{Int, Int}}}() + for (i, arg) in enumerate(args) + symbolic_type(arg) == NotSymbolic() || continue + arg isa AbstractArray || continue + + for (j, var) in enumerate(arg) + var = unwrap(var) + iscall(var) || continue + operation(var) == getindex || continue + arrvar = arguments(var)[1] + idxbuffer = get!(() -> map(Returns((0, 0)), eachindex(arrvar)), var_to_arridxs, arrvar) + idxbuffer[arguments(var)[2:end]...] = (i, j) + end + end + + assignments = Assignment[] + for (arrvar, idxs) in var_to_arridxs + any(iszero ∘ first, idxs) && continue + + if allequal(Iterators.map(first, idxs)) + buffer_idx = first(first(idxs)) + idxs = map(last, idxs) + if first(idxs) < last(idxs) && vec(idxs) == first(idxs):last(idxs) + idxs = first(idxs):last(idxs) + elseif vec(idxs) == last(idxs):-1:first(idxs) + idxs = last(idxs):-1:first(idxs) + else + idxs = SArray{Tuple{size(idxs)...}}(idxs) + end + push!(assignments, arrvar ← term(reshape, term(view, generated_argument_name(buffer_idx), idxs), size(arrvar))) + else + elems = map(idxs) do idx + i, j = idx + term(getindex, generated_argument_name(i), j) + end + push!(assignments, arrvar ← MakeArray(elems, SArray)) + end + end + + return assignments +end + +function build_function_wrapper(sys::AbstractSystem, expr, args...; p_start = 2, p_end = is_time_dependent(sys) ? length(args) - 1 : length(args), wrap_delays = is_dde(sys), wrap_code = identity, kwargs...) + isscalar = !(expr isa AbstractArray || symbolic_type(expr) == ArraySymbolic()) + + obs = observed(sys) + pdeps = parameter_dependencies(sys) + + cmap, _ = get_cmap(sys) + obsidxs = observed_equations_used_by(sys, expr) + pdepidxs = observed_equations_used_by(sys, expr; obs = pdeps) + + assignments = array_variable_assignments(args...) + + for eq in Iterators.flatten((cmap, pdeps[pdepidxs], obs[obsidxs])) + push!(assignments, eq.lhs ← eq.rhs) + end + + args = ntuple(Val(length(args))) do i + arg = args[i] + if symbolic_type(arg) == NotSymbolic() && arg isa AbstractArray + DestructuredArgs(arg, generated_argument_name(i)) + else + arg + end + end + + if is_split(sys) + if p_start > p_end + args = (args[1:p_start-1]..., MTKPARAMETERS_ARG, args[p_end+1:end]...) + else + args = (args[1:p_start-1]..., DestructuredArgs(collect(args[p_start:p_end]), MTKPARAMETERS_ARG), args[p_end+1:end]...) + end + end + + wrap_code = wrap_code .∘ wrap_assignments(isscalar, assignments) + + if wrap_delays + expr = delay_to_function(sys, expr; history_arg = is_split(sys) ? MTKPARAMETERS_ARG : generated_argument_name(p_start)) + end + + return build_function(expr, args...; wrap_code, kwargs...) +end From 9166c74f0db371dd7bc9e1048e26e32954f32b4b Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 30 Jan 2025 11:56:11 +0530 Subject: [PATCH 0710/2176] refactor: use `build_function_wrapper` in `generate_tgrad` --- src/systems/diffeqs/abstractodesystem.jl | 17 +++-------------- 1 file changed, 3 insertions(+), 14 deletions(-) diff --git a/src/systems/diffeqs/abstractodesystem.jl b/src/systems/diffeqs/abstractodesystem.jl index 6571aed9cc..222a36b5ac 100644 --- a/src/systems/diffeqs/abstractodesystem.jl +++ b/src/systems/diffeqs/abstractodesystem.jl @@ -106,24 +106,13 @@ end function generate_tgrad( sys::AbstractODESystem, dvs = unknowns(sys), ps = parameters(sys); - simplify = false, wrap_code = identity, kwargs...) + simplify = false, kwargs...) tgrad = calculate_tgrad(sys, simplify = simplify) - pre = get_preprocess_constants(tgrad) - p = if has_index_cache(sys) && get_index_cache(sys) !== nothing - reorder_parameters(get_index_cache(sys), ps) - elseif ps isa Tuple - ps - else - (ps,) - end - wrap_code = wrap_code .∘ wrap_array_vars(sys, tgrad; dvs, ps) .∘ - wrap_parameter_dependencies(sys, !(tgrad isa AbstractArray)) - return build_function(tgrad, + p = reorder_parameters(sys, ps) + return build_function_wrapper(sys, tgrad, dvs, p..., get_iv(sys); - postprocess_fbody = pre, - wrap_code, kwargs...) end From 2887ca4bc17774637ac202b8cfb73c1504d7d3bd Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 30 Jan 2025 11:56:26 +0530 Subject: [PATCH 0711/2176] refactor: use `build_function` in `generate_jacobian(::AbstractODESystem)` --- src/systems/diffeqs/abstractodesystem.jl | 15 +++------------ 1 file changed, 3 insertions(+), 12 deletions(-) diff --git a/src/systems/diffeqs/abstractodesystem.jl b/src/systems/diffeqs/abstractodesystem.jl index 222a36b5ac..ea891e8557 100644 --- a/src/systems/diffeqs/abstractodesystem.jl +++ b/src/systems/diffeqs/abstractodesystem.jl @@ -118,22 +118,13 @@ end function generate_jacobian(sys::AbstractODESystem, dvs = unknowns(sys), ps = parameters(sys); - simplify = false, sparse = false, wrap_code = identity, kwargs...) + simplify = false, sparse = false, kwargs...) jac = calculate_jacobian(sys; simplify = simplify, sparse = sparse) - pre = get_preprocess_constants(jac) - p = if has_index_cache(sys) && get_index_cache(sys) !== nothing - reorder_parameters(get_index_cache(sys), ps) - else - (ps,) - end - wrap_code = wrap_code .∘ wrap_array_vars(sys, jac; dvs, ps) .∘ - wrap_parameter_dependencies(sys, false) - return build_function(jac, + p = reorder_parameters(sys, ps) + return build_function_wrapper(sys, jac, dvs, p..., get_iv(sys); - postprocess_fbody = pre, - wrap_code, kwargs...) end From 3574e3f45e9d5647b0418fb7595bfe5c35348b49 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 30 Jan 2025 11:57:11 +0530 Subject: [PATCH 0712/2176] refactor: use `build_function_wrapper` in `generate_control_jacobian` --- src/systems/diffeqs/abstractodesystem.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/systems/diffeqs/abstractodesystem.jl b/src/systems/diffeqs/abstractodesystem.jl index ea891e8557..4f36a3c100 100644 --- a/src/systems/diffeqs/abstractodesystem.jl +++ b/src/systems/diffeqs/abstractodesystem.jl @@ -133,7 +133,7 @@ function generate_control_jacobian(sys::AbstractODESystem, dvs = unknowns(sys), simplify = false, sparse = false, kwargs...) jac = calculate_control_jacobian(sys; simplify = simplify, sparse = sparse) p = reorder_parameters(sys, ps) - return build_function(jac, dvs, p..., get_iv(sys); kwargs...) + return build_function_wrapper(sys, jac, dvs, p..., get_iv(sys); kwargs...) end function generate_dae_jacobian(sys::AbstractODESystem, dvs = unknowns(sys), From f183e8ebfa763e2070e096ff6ea633318b0fba75 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 30 Jan 2025 11:57:25 +0530 Subject: [PATCH 0713/2176] refactor: use `build_function_wrapper` in `generate_dae_jacobian` --- src/systems/diffeqs/abstractodesystem.jl | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/src/systems/diffeqs/abstractodesystem.jl b/src/systems/diffeqs/abstractodesystem.jl index 4f36a3c100..9d57761af0 100644 --- a/src/systems/diffeqs/abstractodesystem.jl +++ b/src/systems/diffeqs/abstractodesystem.jl @@ -147,13 +147,9 @@ function generate_dae_jacobian(sys::AbstractODESystem, dvs = unknowns(sys), @variables ˍ₋gamma jac = ˍ₋gamma * jac_du + jac_u pre = get_preprocess_constants(jac) - p = if has_index_cache(sys) && get_index_cache(sys) !== nothing - reorder_parameters(get_index_cache(sys), ps) - else - (ps,) - end - return build_function(jac, derivatives, dvs, p..., ˍ₋gamma, get_iv(sys); - postprocess_fbody = pre, kwargs...) + p = reorder_parameters(sys, ps) + return build_function_wrapper(sys, jac, derivatives, dvs, p..., ˍ₋gamma, get_iv(sys); + p_start = 3, p_end = 2 + length(p), kwargs...) end function generate_function(sys::AbstractODESystem, dvs = unknowns(sys), From d162999bde58707eaf22f7d4da6afc53e533f13e Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 30 Jan 2025 11:57:59 +0530 Subject: [PATCH 0714/2176] refactor: use `build_function_wrapper` in `generate_function(::AbstractODESystem)` --- src/systems/diffeqs/abstractodesystem.jl | 42 ++++-------------------- 1 file changed, 6 insertions(+), 36 deletions(-) diff --git a/src/systems/diffeqs/abstractodesystem.jl b/src/systems/diffeqs/abstractodesystem.jl index 9d57761af0..e422404b44 100644 --- a/src/systems/diffeqs/abstractodesystem.jl +++ b/src/systems/diffeqs/abstractodesystem.jl @@ -158,55 +158,25 @@ function generate_function(sys::AbstractODESystem, dvs = unknowns(sys), ddvs = implicit_dae ? map(Differential(get_iv(sys)), dvs) : nothing, isdde = false, - wrap_code = identity, kwargs...) - if isdde - issplit = has_index_cache(sys) && get_index_cache(sys) !== nothing - eqs = delay_to_function( - sys; history_arg = issplit ? MTKPARAMETERS_ARG : DEFAULT_PARAMS_ARG) - else - eqs = [eq for eq in equations(sys)] - end + eqs = [eq for eq in equations(sys)] if !implicit_dae check_operator_variables(eqs, Differential) check_lhs(eqs, Differential, Set(dvs)) end - # substitute constants in - eqs = map(subs_constants, eqs) - - # substitute x(t) by just x rhss = implicit_dae ? [_iszero(eq.lhs) ? eq.rhs : eq.rhs - eq.lhs for eq in eqs] : [eq.rhs for eq in eqs] # TODO: add an optional check on the ordering of observed equations - u = map(x -> time_varying_as_func(value(x), sys), dvs) - p = map.(x -> time_varying_as_func(value(x), sys), reorder_parameters(sys, ps)) + u = dvs + p = reorder_parameters(sys, ps) t = get_iv(sys) - if isdde - build_function(rhss, u, DDE_HISTORY_FUN, p..., t; kwargs..., - wrap_code = wrap_code .∘ wrap_mtkparameters(sys, false, 3) .∘ - wrap_array_vars(sys, rhss; dvs, ps, history = true) .∘ - wrap_parameter_dependencies(sys, false)) + if implicit_dae + build_function_wrapper(sys, rhss, ddvs, u, p..., t; p_start = 3, kwargs...) else - pre, sol_states = get_substitutions_and_solved_unknowns(sys) - - if implicit_dae - # inputs = [] makes `wrap_array_vars` offset by 1 since there is an extra - # argument - build_function(rhss, ddvs, u, p..., t; postprocess_fbody = pre, - states = sol_states, - wrap_code = wrap_code .∘ wrap_array_vars(sys, rhss; dvs, ps, inputs = []) .∘ - wrap_parameter_dependencies(sys, false), - kwargs...) - else - build_function(rhss, u, p..., t; postprocess_fbody = pre, - states = sol_states, - wrap_code = wrap_code .∘ wrap_array_vars(sys, rhss; dvs, ps) .∘ - wrap_parameter_dependencies(sys, false), - kwargs...) - end + build_function_wrapper(sys, rhss, u, p..., t; kwargs...) end end From 7010d12d0cb2ede57830b677588eac518701433b Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 30 Jan 2025 11:58:19 +0530 Subject: [PATCH 0715/2176] refactor: use new codegen in `ODEFunction` --- src/systems/diffeqs/abstractodesystem.jl | 24 +++--------------------- 1 file changed, 3 insertions(+), 21 deletions(-) diff --git a/src/systems/diffeqs/abstractodesystem.jl b/src/systems/diffeqs/abstractodesystem.jl index e422404b44..86049ba303 100644 --- a/src/systems/diffeqs/abstractodesystem.jl +++ b/src/systems/diffeqs/abstractodesystem.jl @@ -317,12 +317,6 @@ function DiffEqBase.ODEFunction{iip, specialize}(sys::AbstractODESystem, f(u, p, t) = f_oop(u, p, t) f(du, u, p, t) = f_iip(du, u, p, t) - f(u, p::Tuple{Vararg{Number}}, t) = f_oop(u, p, t) - f(du, u, p::Tuple{Vararg{Number}}, t) = f_iip(du, u, p, t) - f(u, p::Tuple, t) = f_oop(u, p..., t) - f(du, u, p::Tuple, t) = f_iip(du, u, p..., t) - f(u, p::MTKParameters, t) = f_oop(u, p..., t) - f(du, u, p::MTKParameters, t) = f_iip(du, u, p..., t) if specialize === SciMLBase.FunctionWrapperSpecialize && iip if u0 === nothing || p === nothing || t === nothing @@ -339,15 +333,9 @@ function DiffEqBase.ODEFunction{iip, specialize}(sys::AbstractODESystem, checkbounds = checkbounds, kwargs...) tgrad_oop, tgrad_iip = eval_or_rgf.(tgrad_gen; eval_expression, eval_module) - if p isa Tuple - __tgrad(u, p, t) = tgrad_oop(u, p..., t) - __tgrad(J, u, p, t) = tgrad_iip(J, u, p..., t) - _tgrad = __tgrad - else - ___tgrad(u, p, t) = tgrad_oop(u, p, t) - ___tgrad(J, u, p, t) = tgrad_iip(J, u, p, t) - _tgrad = ___tgrad - end + ___tgrad(u, p, t) = tgrad_oop(u, p, t) + ___tgrad(J, u, p, t) = tgrad_iip(J, u, p, t) + _tgrad = ___tgrad else _tgrad = nothing end @@ -362,12 +350,6 @@ function DiffEqBase.ODEFunction{iip, specialize}(sys::AbstractODESystem, _jac(u, p, t) = jac_oop(u, p, t) _jac(J, u, p, t) = jac_iip(J, u, p, t) - _jac(u, p::Tuple{Vararg{Number}}, t) = jac_oop(u, p, t) - _jac(J, u, p::Tuple{Vararg{Number}}, t) = jac_iip(J, u, p, t) - _jac(u, p::Tuple, t) = jac_oop(u, p..., t) - _jac(J, u, p::Tuple, t) = jac_iip(J, u, p..., t) - _jac(u, p::MTKParameters, t) = jac_oop(u, p..., t) - _jac(J, u, p::MTKParameters, t) = jac_iip(J, u, p..., t) else _jac = nothing end From 92364b8afb1c6de301f5151bbc99809ee890ff9f Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 30 Jan 2025 11:58:46 +0530 Subject: [PATCH 0716/2176] refactor: use new codegen in `DAEFunction` --- src/systems/diffeqs/abstractodesystem.jl | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/systems/diffeqs/abstractodesystem.jl b/src/systems/diffeqs/abstractodesystem.jl index 86049ba303..faa78ef6ff 100644 --- a/src/systems/diffeqs/abstractodesystem.jl +++ b/src/systems/diffeqs/abstractodesystem.jl @@ -430,9 +430,7 @@ function DiffEqBase.DAEFunction{iip}(sys::AbstractODESystem, dvs = unknowns(sys) kwargs...) f_oop, f_iip = eval_or_rgf.(f_gen; eval_expression, eval_module) f(du, u, p, t) = f_oop(du, u, p, t) - f(du, u, p::MTKParameters, t) = f_oop(du, u, p..., t) f(out, du, u, p, t) = f_iip(out, du, u, p, t) - f(out, du, u, p::MTKParameters, t) = f_iip(out, du, u, p..., t) if jac jac_gen = generate_dae_jacobian(sys, dvs, ps; @@ -443,10 +441,7 @@ function DiffEqBase.DAEFunction{iip}(sys::AbstractODESystem, dvs = unknowns(sys) jac_oop, jac_iip = eval_or_rgf.(jac_gen; eval_expression, eval_module) _jac(du, u, p, ˍ₋gamma, t) = jac_oop(du, u, p, ˍ₋gamma, t) - _jac(du, u, p::MTKParameters, ˍ₋gamma, t) = jac_oop(du, u, p..., ˍ₋gamma, t) - _jac(J, du, u, p, ˍ₋gamma, t) = jac_iip(J, du, u, p, ˍ₋gamma, t) - _jac(J, du, u, p::MTKParameters, ˍ₋gamma, t) = jac_iip(J, du, u, p..., ˍ₋gamma, t) else _jac = nothing end From 141fcabd414dfebedddcaa1110f008f24686f85b Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 30 Jan 2025 11:58:59 +0530 Subject: [PATCH 0717/2176] refactor: use new codegen in `ODEFunctionClosure`, `DAEFunctionClosure` --- src/systems/diffeqs/abstractodesystem.jl | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/systems/diffeqs/abstractodesystem.jl b/src/systems/diffeqs/abstractodesystem.jl index faa78ef6ff..4861677fa8 100644 --- a/src/systems/diffeqs/abstractodesystem.jl +++ b/src/systems/diffeqs/abstractodesystem.jl @@ -549,8 +549,6 @@ struct ODEFunctionClosure{O, I} <: Function end (f::ODEFunctionClosure)(u, p, t) = f.f_oop(u, p, t) (f::ODEFunctionClosure)(du, u, p, t) = f.f_iip(du, u, p, t) -(f::ODEFunctionClosure)(u, p::MTKParameters, t) = f.f_oop(u, p..., t) -(f::ODEFunctionClosure)(du, u, p::MTKParameters, t) = f.f_iip(du, u, p..., t) function ODEFunctionExpr{iip}(sys::AbstractODESystem, dvs = unknowns(sys), ps = parameters(sys), u0 = nothing; @@ -645,8 +643,6 @@ struct DAEFunctionClosure{O, I} <: Function end (f::DAEFunctionClosure)(du, u, p, t) = f.f_oop(du, u, p, t) (f::DAEFunctionClosure)(out, du, u, p, t) = f.f_iip(out, du, u, p, t) -(f::DAEFunctionClosure)(du, u, p::MTKParameters, t) = f.f_oop(du, u, p..., t) -(f::DAEFunctionClosure)(out, du, u, p::MTKParameters, t) = f.f_iip(out, du, u, p..., t) function DAEFunctionExpr{iip}(sys::AbstractODESystem, dvs = unknowns(sys), ps = parameters(sys), u0 = nothing; From a3ff92f41a188a262428a7edaf1b7ce9831fd13b Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 30 Jan 2025 11:59:15 +0530 Subject: [PATCH 0718/2176] refactor: use `build_function_wrapper` in `generate_diffusion_function` --- src/systems/diffeqs/sdesystem.jl | 20 ++------------------ 1 file changed, 2 insertions(+), 18 deletions(-) diff --git a/src/systems/diffeqs/sdesystem.jl b/src/systems/diffeqs/sdesystem.jl index 0426a77858..a017ebc6b1 100644 --- a/src/systems/diffeqs/sdesystem.jl +++ b/src/systems/diffeqs/sdesystem.jl @@ -411,24 +411,8 @@ end function generate_diffusion_function(sys::SDESystem, dvs = unknowns(sys), ps = parameters(sys); isdde = false, kwargs...) eqs = get_noiseeqs(sys) - if isdde - eqs = delay_to_function(sys, eqs) - end - u = map(x -> time_varying_as_func(value(x), sys), dvs) - p = if has_index_cache(sys) && get_index_cache(sys) !== nothing - reorder_parameters(get_index_cache(sys), ps) - else - (map(x -> time_varying_as_func(value(x), sys), ps),) - end - if isdde - return build_function(eqs, u, DDE_HISTORY_FUN, p..., get_iv(sys); kwargs..., - wrap_code = get(kwargs, :wrap_code, identity) .∘ - wrap_mtkparameters(sys, false, 3) .∘ - wrap_array_vars(sys, eqs; dvs, ps, history = true) .∘ - wrap_parameter_dependencies(sys, false)) - else - return build_function(eqs, u, p..., get_iv(sys); kwargs...) - end + p = reorder_parameters(sys, ps) + return build_function_wrapper(sys, eqs, dvs, p..., get_iv(sys); kwargs...) end """ From b0fdacfabb63d527f5bba2c459bb981f21bb8945 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 30 Jan 2025 11:59:33 +0530 Subject: [PATCH 0719/2176] refactor: use new codegen in `SDEFunction` --- src/systems/diffeqs/sdesystem.jl | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/src/systems/diffeqs/sdesystem.jl b/src/systems/diffeqs/sdesystem.jl index a017ebc6b1..212e20d743 100644 --- a/src/systems/diffeqs/sdesystem.jl +++ b/src/systems/diffeqs/sdesystem.jl @@ -596,13 +596,9 @@ function DiffEqBase.SDEFunction{iip, specialize}(sys::SDESystem, dvs = unknowns( g_oop, g_iip = eval_or_rgf.(g_gen; eval_expression, eval_module) f(u, p, t) = f_oop(u, p, t) - f(u, p::MTKParameters, t) = f_oop(u, p..., t) f(du, u, p, t) = f_iip(du, u, p, t) - f(du, u, p::MTKParameters, t) = f_iip(du, u, p..., t) g(u, p, t) = g_oop(u, p, t) - g(u, p::MTKParameters, t) = g_oop(u, p..., t) g(du, u, p, t) = g_iip(du, u, p, t) - g(du, u, p::MTKParameters, t) = g_iip(du, u, p..., t) if tgrad tgrad_gen = generate_tgrad(sys, dvs, ps; expression = Val{true}, @@ -610,9 +606,7 @@ function DiffEqBase.SDEFunction{iip, specialize}(sys::SDESystem, dvs = unknowns( tgrad_oop, tgrad_iip = eval_or_rgf.(tgrad_gen; eval_expression, eval_module) _tgrad(u, p, t) = tgrad_oop(u, p, t) - _tgrad(u, p::MTKParameters, t) = tgrad_oop(u, p..., t) _tgrad(J, u, p, t) = tgrad_iip(J, u, p, t) - _tgrad(J, u, p::MTKParameters, t) = tgrad_iip(J, u, p..., t) else _tgrad = nothing end @@ -623,9 +617,7 @@ function DiffEqBase.SDEFunction{iip, specialize}(sys::SDESystem, dvs = unknowns( jac_oop, jac_iip = eval_or_rgf.(jac_gen; eval_expression, eval_module) _jac(u, p, t) = jac_oop(u, p, t) - _jac(u, p::MTKParameters, t) = jac_oop(u, p..., t) _jac(J, u, p, t) = jac_iip(J, u, p, t) - _jac(J, u, p::MTKParameters, t) = jac_iip(J, u, p..., t) else _jac = nothing end @@ -637,13 +629,9 @@ function DiffEqBase.SDEFunction{iip, specialize}(sys::SDESystem, dvs = unknowns( Wfact_oop_t, Wfact_iip_t = eval_or_rgf.(tmp_Wfact_t; eval_expression, eval_module) _Wfact(u, p, dtgamma, t) = Wfact_oop(u, p, dtgamma, t) - _Wfact(u, p::MTKParameters, dtgamma, t) = Wfact_oop(u, p..., dtgamma, t) _Wfact(W, u, p, dtgamma, t) = Wfact_iip(W, u, p, dtgamma, t) - _Wfact(W, u, p::MTKParameters, dtgamma, t) = Wfact_iip(W, u, p..., dtgamma, t) _Wfact_t(u, p, dtgamma, t) = Wfact_oop_t(u, p, dtgamma, t) - _Wfact_t(u, p::MTKParameters, dtgamma, t) = Wfact_oop_t(u, p..., dtgamma, t) _Wfact_t(W, u, p, dtgamma, t) = Wfact_iip_t(W, u, p, dtgamma, t) - _Wfact_t(W, u, p::MTKParameters, dtgamma, t) = Wfact_iip_t(W, u, p..., dtgamma, t) else _Wfact, _Wfact_t = nothing, nothing end From cff74a9f5b8a496bb53b0e4ef90b6989397eb13f Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 30 Jan 2025 11:59:51 +0530 Subject: [PATCH 0720/2176] refactor: use `build_function_wrapper` in `generate_jacobian(::NonlinearSystem)` --- src/systems/nonlinear/nonlinearsystem.jl | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/systems/nonlinear/nonlinearsystem.jl b/src/systems/nonlinear/nonlinearsystem.jl index 58f61d3b22..6ffac624b2 100644 --- a/src/systems/nonlinear/nonlinearsystem.jl +++ b/src/systems/nonlinear/nonlinearsystem.jl @@ -249,14 +249,10 @@ end function generate_jacobian( sys::NonlinearSystem, vs = unknowns(sys), ps = parameters(sys); - sparse = false, simplify = false, wrap_code = identity, kwargs...) + sparse = false, simplify = false, kwargs...) jac = calculate_jacobian(sys, sparse = sparse, simplify = simplify) - pre, sol_states = get_substitutions_and_solved_unknowns(sys) p = reorder_parameters(sys, ps) - wrap_code = wrap_code .∘ wrap_array_vars(sys, jac; dvs = vs, ps) .∘ - wrap_parameter_dependencies(sys, false) - return build_function( - jac, vs, p...; postprocess_fbody = pre, states = sol_states, wrap_code, kwargs...) + return build_function_wrapper(sys, jac, vs, p...; kwargs...) end function calculate_hessian(sys::NonlinearSystem; sparse = false, simplify = false) From 517c1a51f21fc9ecf7e60b2eed6be5530b672eaa Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 30 Jan 2025 12:00:08 +0530 Subject: [PATCH 0721/2176] refactor: use `build_function_wrapper` in `generate_hessian(::NonlinearSystem)` --- src/systems/nonlinear/nonlinearsystem.jl | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/systems/nonlinear/nonlinearsystem.jl b/src/systems/nonlinear/nonlinearsystem.jl index 6ffac624b2..7a0ba020ae 100644 --- a/src/systems/nonlinear/nonlinearsystem.jl +++ b/src/systems/nonlinear/nonlinearsystem.jl @@ -269,13 +269,10 @@ end function generate_hessian( sys::NonlinearSystem, vs = unknowns(sys), ps = parameters(sys); - sparse = false, simplify = false, wrap_code = identity, kwargs...) + sparse = false, simplify = false, kwargs...) hess = calculate_hessian(sys, sparse = sparse, simplify = simplify) - pre = get_preprocess_constants(hess) p = reorder_parameters(sys, ps) - wrap_code = wrap_code .∘ wrap_array_vars(sys, hess; dvs = vs, ps) .∘ - wrap_parameter_dependencies(sys, false) - return build_function(hess, vs, p...; postprocess_fbody = pre, wrap_code, kwargs...) + return build_function_wrapper(sys, hess, vs, p...; kwargs...) end function generate_function( From 4c7cffd4042e1d8b7bb1f06558f2ce08b0503c25 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 30 Jan 2025 12:00:33 +0530 Subject: [PATCH 0722/2176] refactor: use `build_function_wrapper` in `generate_function(::NonlinearSystem)` --- src/systems/nonlinear/nonlinearsystem.jl | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/systems/nonlinear/nonlinearsystem.jl b/src/systems/nonlinear/nonlinearsystem.jl index 7a0ba020ae..e7eeffb801 100644 --- a/src/systems/nonlinear/nonlinearsystem.jl +++ b/src/systems/nonlinear/nonlinearsystem.jl @@ -277,19 +277,15 @@ end function generate_function( sys::NonlinearSystem, dvs = unknowns(sys), ps = parameters(sys); - wrap_code = identity, scalar = false, kwargs...) + scalar = false, kwargs...) rhss = [deq.rhs for deq in equations(sys)] dvs′ = value.(dvs) if scalar rhss = only(rhss) dvs′ = only(dvs) end - pre, sol_states = get_substitutions_and_solved_unknowns(sys) - wrap_code = wrap_code .∘ wrap_array_vars(sys, rhss; dvs, ps) .∘ - wrap_parameter_dependencies(sys, scalar) p = reorder_parameters(sys, value.(ps)) - return build_function(rhss, dvs′, p...; postprocess_fbody = pre, - states = sol_states, wrap_code, kwargs...) + return build_function_wrapper(sys, rhss, dvs′, p...; kwargs...) end function jacobian_sparsity(sys::NonlinearSystem) From 0bc240d3994509606cbb875ea740e79384f3bcd4 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 30 Jan 2025 12:00:50 +0530 Subject: [PATCH 0723/2176] refactor: use new codegen in `NonlinearFunction`, `IntervalNonlinearFunction` --- src/systems/nonlinear/nonlinearsystem.jl | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/src/systems/nonlinear/nonlinearsystem.jl b/src/systems/nonlinear/nonlinearsystem.jl index e7eeffb801..08ede84033 100644 --- a/src/systems/nonlinear/nonlinearsystem.jl +++ b/src/systems/nonlinear/nonlinearsystem.jl @@ -341,9 +341,7 @@ function SciMLBase.NonlinearFunction{iip}(sys::NonlinearSystem, dvs = unknowns(s f_gen = generate_function(sys, dvs, ps; expression = Val{true}, kwargs...) f_oop, f_iip = eval_or_rgf.(f_gen; eval_expression, eval_module) f(u, p) = f_oop(u, p) - f(u, p::MTKParameters) = f_oop(u, p...) f(du, u, p) = f_iip(du, u, p) - f(du, u, p::MTKParameters) = f_iip(du, u, p...) if jac jac_gen = generate_jacobian(sys, dvs, ps; @@ -351,9 +349,7 @@ function SciMLBase.NonlinearFunction{iip}(sys::NonlinearSystem, dvs = unknowns(s expression = Val{true}, kwargs...) jac_oop, jac_iip = eval_or_rgf.(jac_gen; eval_expression, eval_module) _jac(u, p) = jac_oop(u, p) - _jac(u, p::MTKParameters) = jac_oop(u, p...) _jac(J, u, p) = jac_iip(J, u, p) - _jac(J, u, p::MTKParameters) = jac_iip(J, u, p...) else _jac = nothing end @@ -397,9 +393,7 @@ function SciMLBase.IntervalNonlinearFunction( f_gen = generate_function( sys, dvs, ps; expression = Val{true}, scalar = true, kwargs...) - f_oop = eval_or_rgf(f_gen; eval_expression, eval_module) - f(u, p) = f_oop(u, p) - f(u, p::MTKParameters) = f_oop(u, p...) + f = eval_or_rgf(f_gen; eval_expression, eval_module) observedfun = ObservedFunctionCache( sys; eval_expression, eval_module, checkbounds = get(kwargs, :checkbounds, false)) From de83f7056bd7ce04c72bca92f7fbb59f566eca05 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 30 Jan 2025 11:43:35 +0530 Subject: [PATCH 0724/2176] fix: handle extra constants in `build_function_wrapper` --- src/systems/codegen_utils.jl | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/systems/codegen_utils.jl b/src/systems/codegen_utils.jl index 35a7c579a2..6ccdc77034 100644 --- a/src/systems/codegen_utils.jl +++ b/src/systems/codegen_utils.jl @@ -52,7 +52,13 @@ function build_function_wrapper(sys::AbstractSystem, expr, args...; p_start = 2, pdeps = parameter_dependencies(sys) cmap, _ = get_cmap(sys) - obsidxs = observed_equations_used_by(sys, expr) + extra_constants = collect_constants(expr) + filter!(extra_constants) do c + !any(x -> isequal(c, x.lhs), cmap) + end + for c in extra_constants + push!(cmap, c ~ getdefault(c)) + end pdepidxs = observed_equations_used_by(sys, expr; obs = pdeps) assignments = array_variable_assignments(args...) From a845c2bd3a56e1ea1a7cc128cc512d2f25d50597 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 30 Jan 2025 11:45:12 +0530 Subject: [PATCH 0725/2176] feat: better observed handling, optional `DestructuredArgs` binding in `build_function_wrapper` --- src/systems/codegen_utils.jl | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/systems/codegen_utils.jl b/src/systems/codegen_utils.jl index 6ccdc77034..c06a6d1462 100644 --- a/src/systems/codegen_utils.jl +++ b/src/systems/codegen_utils.jl @@ -45,10 +45,10 @@ function array_variable_assignments(args...) return assignments end -function build_function_wrapper(sys::AbstractSystem, expr, args...; p_start = 2, p_end = is_time_dependent(sys) ? length(args) - 1 : length(args), wrap_delays = is_dde(sys), wrap_code = identity, kwargs...) +function build_function_wrapper(sys::AbstractSystem, expr, args...; p_start = 2, p_end = is_time_dependent(sys) ? length(args) - 1 : length(args), wrap_delays = is_dde(sys), wrap_code = identity, add_observed = true, filter_observed = Returns(true), create_bindings = true, kwargs...) isscalar = !(expr isa AbstractArray || symbolic_type(expr) == ArraySymbolic()) - obs = observed(sys) + obs = filter(filter_observed, observed(sys)) pdeps = parameter_dependencies(sys) cmap, _ = get_cmap(sys) @@ -59,6 +59,11 @@ function build_function_wrapper(sys::AbstractSystem, expr, args...; p_start = 2, for c in extra_constants push!(cmap, c ~ getdefault(c)) end + if add_observed + obsidxs = observed_equations_used_by(sys, expr) + else + obsidxs = Int[] + end pdepidxs = observed_equations_used_by(sys, expr; obs = pdeps) assignments = array_variable_assignments(args...) @@ -70,7 +75,7 @@ function build_function_wrapper(sys::AbstractSystem, expr, args...; p_start = 2, args = ntuple(Val(length(args))) do i arg = args[i] if symbolic_type(arg) == NotSymbolic() && arg isa AbstractArray - DestructuredArgs(arg, generated_argument_name(i)) + DestructuredArgs(arg, generated_argument_name(i); create_bindings) else arg end @@ -80,7 +85,10 @@ function build_function_wrapper(sys::AbstractSystem, expr, args...; p_start = 2, if p_start > p_end args = (args[1:p_start-1]..., MTKPARAMETERS_ARG, args[p_end+1:end]...) else - args = (args[1:p_start-1]..., DestructuredArgs(collect(args[p_start:p_end]), MTKPARAMETERS_ARG), args[p_end+1:end]...) + # cannot apply `create_bindings` here since it doesn't nest + args = (args[1:(p_start - 1)]..., + DestructuredArgs(collect(args[p_start:p_end]), MTKPARAMETERS_ARG), + args[(p_end + 1):end]...) end end From 60d501bb1995b11a29d83bc3ac5ee094499fab66 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 30 Jan 2025 11:45:30 +0530 Subject: [PATCH 0726/2176] fix: use `time_varying_as_func` in `build_function_wrapper` --- src/systems/codegen_utils.jl | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/systems/codegen_utils.jl b/src/systems/codegen_utils.jl index c06a6d1462..97ba818083 100644 --- a/src/systems/codegen_utils.jl +++ b/src/systems/codegen_utils.jl @@ -74,6 +74,14 @@ function build_function_wrapper(sys::AbstractSystem, expr, args...; p_start = 2, args = ntuple(Val(length(args))) do i arg = args[i] + if is_time_dependent(sys) + arg = if symbolic_type(arg) == NotSymbolic() + arg isa AbstractArray ? + map(x -> time_varying_as_func(unwrap(x), sys), arg) : arg + else + time_varying_as_func(unwrap(arg), sys) + end + end if symbolic_type(arg) == NotSymbolic() && arg isa AbstractArray DestructuredArgs(arg, generated_argument_name(i); create_bindings) else From 1cab1c319392a5f923455d2ba8cfc406c6afaa8a Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 30 Jan 2025 11:46:43 +0530 Subject: [PATCH 0727/2176] feat: better delay handling, preface handling in `build_function_wrapper` --- src/systems/codegen_utils.jl | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/src/systems/codegen_utils.jl b/src/systems/codegen_utils.jl index 97ba818083..35526c3c0e 100644 --- a/src/systems/codegen_utils.jl +++ b/src/systems/codegen_utils.jl @@ -49,6 +49,16 @@ function build_function_wrapper(sys::AbstractSystem, expr, args...; p_start = 2, isscalar = !(expr isa AbstractArray || symbolic_type(expr) == ArraySymbolic()) obs = filter(filter_observed, observed(sys)) + if wrap_delays + history_arg = is_split(sys) ? MTKPARAMETERS_ARG : generated_argument_name(p_start) + obs = map(obs) do eq + delay_to_function(sys, eq; history_arg) + end + expr = delay_to_function(sys, expr; history_arg) + args = (args[1:p_start-1]..., DDE_HISTORY_FUN, args[p_start:end]...) + p_start += 1 + p_end += 1 + end pdeps = parameter_dependencies(sys) cmap, _ = get_cmap(sys) @@ -100,11 +110,11 @@ function build_function_wrapper(sys::AbstractSystem, expr, args...; p_start = 2, end end - wrap_code = wrap_code .∘ wrap_assignments(isscalar, assignments) - - if wrap_delays - expr = delay_to_function(sys, expr; history_arg = is_split(sys) ? MTKPARAMETERS_ARG : generated_argument_name(p_start)) + if has_preface(sys) && (pref = preface(sys)) !== nothing + append!(assignments, pref) end + wrap_code = wrap_code .∘ wrap_assignments(isscalar, assignments) + return build_function(expr, args...; wrap_code, kwargs...) end From 106b05a356ee501ea6d40a0825474479e9a4b091 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 30 Jan 2025 11:49:14 +0530 Subject: [PATCH 0728/2176] refactor: use `build_function_wrapper` in callbacks --- src/systems/callbacks.jl | 88 ++++++++-------------------------------- 1 file changed, 17 insertions(+), 71 deletions(-) diff --git a/src/systems/callbacks.jl b/src/systems/callbacks.jl index 492b5dac8c..a58ff3f8ec 100644 --- a/src/systems/callbacks.jl +++ b/src/systems/callbacks.jl @@ -519,70 +519,19 @@ end # handles ensuring that affect! functions work with integrator arguments function add_integrator_header( sys::AbstractSystem, integrator = gensym(:MTKIntegrator), out = :u) - if has_index_cache(sys) && get_index_cache(sys) !== nothing - function (expr) - p = gensym(:p) - Func( - [ - DestructuredArgs([expr.args[1], p, expr.args[end]], - integrator, inds = [:u, :p, :t]) - ], - [], - Let( - [DestructuredArgs([arg.name for arg in expr.args[2:(end - 1)]], p), - expr.args[2:(end - 1)]...], - expr.body, - false) - ) - end, - function (expr) - p = gensym(:p) - Func( - [ - DestructuredArgs([expr.args[1], expr.args[2], p, expr.args[end]], - integrator, inds = [out, :u, :p, :t]) - ], - [], - Let( - [DestructuredArgs([arg.name for arg in expr.args[3:(end - 1)]], p), - expr.args[3:(end - 1)]...], - expr.body, - false) - ) - end - else - expr -> Func([DestructuredArgs(expr.args, integrator, inds = [:u, :p, :t])], [], - expr.body), - expr -> Func( - [DestructuredArgs(expr.args, integrator, inds = [out, :u, :p, :t])], [], - expr.body) - end + expr -> Func([DestructuredArgs(expr.args, integrator, inds = [:u, :p, :t])], [], + expr.body), + expr -> Func( + [DestructuredArgs(expr.args, integrator, inds = [out, :u, :p, :t])], [], + expr.body) end function condition_header(sys::AbstractSystem, integrator = gensym(:MTKIntegrator)) - if has_index_cache(sys) && get_index_cache(sys) !== nothing - function (expr) - p = gensym(:p) - res = Func( - [expr.args[1], expr.args[2], - DestructuredArgs([p], integrator, inds = [:p])], - [], - Let( - [ - DestructuredArgs([arg.name for arg in expr.args[3:end]], p), - expr.args[3:end]... - ], expr.body, false - ) - ) - return res - end - else - expr -> Func( - [expr.args[1], expr.args[2], - DestructuredArgs(expr.args[3:end], integrator, inds = [:p])], - [], - expr.body) - end + expr -> Func( + [expr.args[1], expr.args[2], + DestructuredArgs(expr.args[3:end], integrator, inds = [:p])], + [], + expr.body) end function callback_save_header(sys::AbstractSystem, cb) @@ -628,11 +577,10 @@ function compile_condition(cb::SymbolicDiscreteCallback, sys, dvs, ps; cmap = map(x -> x => getdefault(x), cs) condit = substitute(condit, cmap) end - expr = build_function( + expr = build_function_wrapper(sys, condit, u, t, p...; expression = Val{true}, - wrap_code = condition_header(sys) .∘ - wrap_array_vars(sys, condit; dvs, ps, inputs = true) .∘ - wrap_parameter_dependencies(sys, !(condit isa AbstractArray)), + p_start = 3, p_end = length(p) + 2, + wrap_code = condition_header(sys), kwargs...) if expression == Val{true} return expr @@ -715,14 +663,12 @@ function compile_affect(eqs::Vector{Equation}, cb, sys, dvs, ps; outputidxs = no end t = get_iv(sys) integ = gensym(:MTKIntegrator) - pre = get_preprocess_constants(rhss) - rf_oop, rf_ip = build_function(rhss, u, p..., t; expression = Val{true}, + rf_oop, rf_ip = build_function_wrapper( + sys, rhss, u, p..., t; expression = Val{true}, wrap_code = callback_save_header(sys, cb) .∘ - add_integrator_header(sys, integ, outvar) .∘ - wrap_array_vars(sys, rhss; dvs, ps = _ps) .∘ - wrap_parameter_dependencies(sys, false), + add_integrator_header(sys, integ, outvar), outputidxs = update_inds, - postprocess_fbody = pre, + create_bindings = false, kwargs...) # applied user-provided function to the generated expression if postprocess_affect_expr! !== nothing From d6607f91b3604566f0c96ea99940e4d0d4928e7c Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 30 Jan 2025 11:49:43 +0530 Subject: [PATCH 0729/2176] refactor: use `build_function_wrapper` in `generate_custom_function` --- src/systems/abstractsystem.jl | 26 +++----------------------- 1 file changed, 3 insertions(+), 23 deletions(-) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index eddcdc6266..e63364cbf0 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -161,7 +161,7 @@ time-independent systems. If `split=true` (the default) was passed to [`complete object. """ function generate_custom_function(sys::AbstractSystem, exprs, dvs = unknowns(sys), - ps = parameters(sys); wrap_code = nothing, postprocess_fbody = nothing, states = nothing, + ps = parameters(sys); expression = Val{true}, eval_expression = false, eval_module = @__MODULE__, cachesyms::Tuple = (), kwargs...) if !iscomplete(sys) @@ -169,39 +169,19 @@ function generate_custom_function(sys::AbstractSystem, exprs, dvs = unknowns(sys end p = (reorder_parameters(sys, unwrap.(ps))..., cachesyms...) isscalar = !(exprs isa AbstractArray) - if wrap_code === nothing - wrap_code = isscalar ? identity : (identity, identity) - end - pre, sol_states = get_substitutions_and_solved_unknowns(sys, isscalar ? [exprs] : exprs) - if postprocess_fbody === nothing - postprocess_fbody = pre - end - if states === nothing - states = sol_states - end fnexpr = if is_time_dependent(sys) - build_function(exprs, + build_function_wrapper(sys, exprs, dvs, p..., get_iv(sys); kwargs..., - postprocess_fbody, - states, - wrap_code = wrap_code .∘ wrap_mtkparameters(sys, isscalar) .∘ - wrap_array_vars(sys, exprs; dvs, cachesyms) .∘ - wrap_parameter_dependencies(sys, isscalar), expression = Val{true} ) else - build_function(exprs, + build_function_wrapper(sys, exprs, dvs, p...; kwargs..., - postprocess_fbody, - states, - wrap_code = wrap_code .∘ wrap_mtkparameters(sys, isscalar) .∘ - wrap_array_vars(sys, exprs; dvs, cachesyms) .∘ - wrap_parameter_dependencies(sys, isscalar), expression = Val{true} ) end From f9a50274dacb77dcbb9d5350d833811421607aef Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 30 Jan 2025 11:51:21 +0530 Subject: [PATCH 0730/2176] fix: don't consider parameters as delays --- src/systems/diffeqs/abstractodesystem.jl | 1 + 1 file changed, 1 insertion(+) diff --git a/src/systems/diffeqs/abstractodesystem.jl b/src/systems/diffeqs/abstractodesystem.jl index 4861677fa8..a80e5db5b2 100644 --- a/src/systems/diffeqs/abstractodesystem.jl +++ b/src/systems/diffeqs/abstractodesystem.jl @@ -183,6 +183,7 @@ end function isdelay(var, iv) iv === nothing && return false isvariable(var) || return false + isparameter(var) && return false if iscall(var) && !ModelingToolkit.isoperator(var, Symbolics.Operator) args = arguments(var) length(args) == 1 || return false From 5637a0cafb037e42023759f37c48caab152636f8 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 30 Jan 2025 11:51:48 +0530 Subject: [PATCH 0731/2176] refactor: use `build_function_wrapper` in `SymbolicTstops` --- src/systems/diffeqs/abstractodesystem.jl | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/systems/diffeqs/abstractodesystem.jl b/src/systems/diffeqs/abstractodesystem.jl index a80e5db5b2..749cef2766 100644 --- a/src/systems/diffeqs/abstractodesystem.jl +++ b/src/systems/diffeqs/abstractodesystem.jl @@ -675,7 +675,7 @@ struct SymbolicTstops{F} end function (st::SymbolicTstops)(p, tspan) - unique!(sort!(reduce(vcat, st.fn(p..., tspan...)))) + unique!(sort!(reduce(vcat, st.fn(p, tspan...)))) end function SymbolicTstops( @@ -692,13 +692,12 @@ function SymbolicTstops( end end rps = reorder_parameters(sys, parameters(sys)) - tstops, _ = build_function(tstops, + tstops, _ = build_function_wrapper(sys, tstops, rps..., t0, t1; expression = Val{true}, - wrap_code = wrap_array_vars(sys, tstops; dvs = nothing) .∘ - wrap_parameter_dependencies(sys, false)) + p_start = 1, p_end = length(rps), add_observed = false, force_SA = true) tstops = eval_or_rgf(tstops; eval_expression, eval_module) return SymbolicTstops(tstops) end From 6491c171e3dcd98ee1e186fee570e63bab226dc5 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 30 Jan 2025 11:52:08 +0530 Subject: [PATCH 0732/2176] refactor: use `build_function_wrapper` for history functions --- src/systems/diffeqs/abstractodesystem.jl | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/src/systems/diffeqs/abstractodesystem.jl b/src/systems/diffeqs/abstractodesystem.jl index 749cef2766..d9b8fb31ab 100644 --- a/src/systems/diffeqs/abstractodesystem.jl +++ b/src/systems/diffeqs/abstractodesystem.jl @@ -818,7 +818,9 @@ end function generate_history(sys::AbstractODESystem, u0; expression = Val{false}, kwargs...) p = reorder_parameters(sys, parameters(sys)) - build_function(u0, p..., get_iv(sys); expression, kwargs...) + build_function_wrapper( + sys, u0, p..., get_iv(sys); expression, p_start = 1, p_end = length(p), + similarto = typeof(u0), wrap_delays = false, kwargs...) end function DiffEqBase.DDEProblem(sys::AbstractODESystem, args...; kwargs...) @@ -842,8 +844,7 @@ function DiffEqBase.DDEProblem{iip}(sys::AbstractODESystem, u0map = [], check_length, eval_expression, eval_module, kwargs...) h_gen = generate_history(sys, u0; expression = Val{true}) h_oop, h_iip = eval_or_rgf.(h_gen; eval_expression, eval_module) - h(p, t) = h_oop(p, t) - h(p::MTKParameters, t) = h_oop(p..., t) + h = h_oop u0 = float.(h(p, tspan[1])) if u0 !== nothing u0 = u0_constructor(u0) @@ -881,10 +882,7 @@ function DiffEqBase.SDDEProblem{iip}(sys::AbstractODESystem, u0map = [], check_length, kwargs...) h_gen = generate_history(sys, u0; expression = Val{true}) h_oop, h_iip = eval_or_rgf.(h_gen; eval_expression, eval_module) - h(out, p, t) = h_iip(out, p, t) - h(p, t) = h_oop(p, t) - h(p::MTKParameters, t) = h_oop(p..., t) - h(out, p::MTKParameters, t) = h_iip(out, p..., t) + h = h_oop u0 = h(p, tspan[1]) if u0 !== nothing u0 = u0_constructor(u0) From ead141adca0250c35b2aea16f58cb605fa279984 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 30 Jan 2025 11:52:58 +0530 Subject: [PATCH 0733/2176] refactor: refactor `generate_function(::DiscreteSystem)` to use new codegen --- src/systems/discrete_system/discrete_system.jl | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/systems/discrete_system/discrete_system.jl b/src/systems/discrete_system/discrete_system.jl index bd5c72eec7..bd81255706 100644 --- a/src/systems/discrete_system/discrete_system.jl +++ b/src/systems/discrete_system/discrete_system.jl @@ -260,9 +260,7 @@ end function generate_function( sys::DiscreteSystem, dvs = unknowns(sys), ps = parameters(sys); wrap_code = identity, kwargs...) exprs = [eq.rhs for eq in equations(sys)] - wrap_code = wrap_code .∘ wrap_array_vars(sys, exprs) .∘ - wrap_parameter_dependencies(sys, false) - generate_custom_function(sys, exprs, dvs, ps; wrap_code, kwargs...) + generate_custom_function(sys, exprs, dvs, ps; kwargs...) end function shift_u0map_forward(sys::DiscreteSystem, u0map, defs) From dc099099827f3f03bbf4ca0717fd9c591daf5c56 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 30 Jan 2025 11:53:21 +0530 Subject: [PATCH 0734/2176] refactor: use `build_function_wrapper` in `JumpSystem` codegen --- src/systems/jumps/jumpsystem.jl | 20 +++++--------------- 1 file changed, 5 insertions(+), 15 deletions(-) diff --git a/src/systems/jumps/jumpsystem.jl b/src/systems/jumps/jumpsystem.jl index f39b5fa46a..beaa78e1e3 100644 --- a/src/systems/jumps/jumpsystem.jl +++ b/src/systems/jumps/jumpsystem.jl @@ -283,10 +283,8 @@ function generate_rate_function(js::JumpSystem, rate) rate = substitute(rate, csubs) end p = reorder_parameters(js, parameters(js)) - rf = build_function(rate, unknowns(js), p..., + rf = build_function_wrapper(js, rate, unknowns(js), p..., get_iv(js), - wrap_code = wrap_array_vars(js, rate; dvs = unknowns(js), ps = parameters(js)) .∘ - wrap_parameter_dependencies(js, !(rate isa AbstractArray)), expression = Val{true}) end @@ -303,9 +301,7 @@ end function assemble_vrj( js, vrj, unknowntoid; eval_expression = false, eval_module = @__MODULE__) - _rate = eval_or_rgf(generate_rate_function(js, vrj.rate); eval_expression, eval_module) - rate(u, p, t) = _rate(u, p, t) - rate(u, p::MTKParameters, t) = _rate(u, p..., t) + rate = eval_or_rgf(generate_rate_function(js, vrj.rate); eval_expression, eval_module) outputvars = (value(affect.lhs) for affect in vrj.affect!) outputidxs = [unknowntoid[var] for var in outputvars] @@ -320,9 +316,7 @@ function assemble_vrj_expr(js, vrj, unknowntoid) outputidxs = ((unknowntoid[var] for var in outputvars)...,) affect = generate_affect_function(js, vrj.affect!, outputidxs) quote - _rate = $rate - rate(u, p, t) = _rate(u, p, t) - rate(u, p::MTKParameters, t) = _rate(u, p..., t) + rate = $rate affect = $affect VariableRateJump(rate, affect) @@ -331,9 +325,7 @@ end function assemble_crj( js, crj, unknowntoid; eval_expression = false, eval_module = @__MODULE__) - _rate = eval_or_rgf(generate_rate_function(js, crj.rate); eval_expression, eval_module) - rate(u, p, t) = _rate(u, p, t) - rate(u, p::MTKParameters, t) = _rate(u, p..., t) + rate = eval_or_rgf(generate_rate_function(js, crj.rate); eval_expression, eval_module) outputvars = (value(affect.lhs) for affect in crj.affect!) outputidxs = [unknowntoid[var] for var in outputvars] @@ -348,9 +340,7 @@ function assemble_crj_expr(js, crj, unknowntoid) outputidxs = ((unknowntoid[var] for var in outputvars)...,) affect = generate_affect_function(js, crj.affect!, outputidxs) quote - _rate = $rate - rate(u, p, t) = _rate(u, p, t) - rate(u, p::MTKParameters, t) = _rate(u, p..., t) + rate = $rate affect = $affect ConstantRateJump(rate, affect) From 95a75ab21dfebfb5ed5e12f7cca91c522ffc6148 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 30 Jan 2025 11:53:37 +0530 Subject: [PATCH 0735/2176] refactor: fix `linearize_symbolic` to respect new codegen --- src/systems/abstractsystem.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index e63364cbf0..aaa87fc02d 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -2558,7 +2558,7 @@ function linearize_symbolic(sys::AbstractSystem, inputs, fun_expr = generate_function(sys, sts, ps; expression = Val{true})[1] fun = eval_or_rgf(fun_expr; eval_expression, eval_module) - dx = fun(sts, p..., t) + dx = fun(sts, p, t) h = build_explicit_observed_function(sys, outputs; eval_expression, eval_module) y = h(sts, p, t) From 16000134b9615927c5457811b354fc45734f9071 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 30 Jan 2025 11:53:49 +0530 Subject: [PATCH 0736/2176] fix: refactor `modelingtoolkitize` to use new codegen --- src/systems/diffeqs/modelingtoolkitize.jl | 10 +++++----- src/systems/nonlinear/modelingtoolkitize.jl | 2 +- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/systems/diffeqs/modelingtoolkitize.jl b/src/systems/diffeqs/modelingtoolkitize.jl index 2e0623bea1..7ab68e3bee 100644 --- a/src/systems/diffeqs/modelingtoolkitize.jl +++ b/src/systems/diffeqs/modelingtoolkitize.jl @@ -62,9 +62,9 @@ function modelingtoolkitize( fill!(rhs, 0) if prob.f isa ODEFunction && prob.f.f isa FunctionWrappersWrappers.FunctionWrappersWrapper - prob.f.f.fw[1].obj[](rhs, vars, params, t) + prob.f.f.fw[1].obj[](rhs, vars, p isa MTKParameters ? (params,) : params, t) else - prob.f(rhs, vars, params, t) + prob.f(rhs, vars, p isa MTKParameters ? (params,) : params, t) end else rhs = prob.f(vars, params, t) @@ -253,14 +253,14 @@ function modelingtoolkitize(prob::DiffEqBase.SDEProblem; kwargs...) if DiffEqBase.isinplace(prob) lhs = similar(vars, Any) - prob.f(lhs, vars, params, t) + prob.f(lhs, vars, p isa MTKParameters ? (params,) : params, t) if DiffEqBase.is_diagonal_noise(prob) neqs = similar(vars, Any) - prob.g(neqs, vars, params, t) + prob.g(neqs, vars, p isa MTKParameters ? (params,) : params, t) else neqs = similar(vars, Any, size(prob.noise_rate_prototype)) - prob.g(neqs, vars, params, t) + prob.g(neqs, vars, p isa MTKParameters ? (params,) : params, t) end else lhs = prob.f(vars, params, t) diff --git a/src/systems/nonlinear/modelingtoolkitize.jl b/src/systems/nonlinear/modelingtoolkitize.jl index 2f12157884..ec35318e3e 100644 --- a/src/systems/nonlinear/modelingtoolkitize.jl +++ b/src/systems/nonlinear/modelingtoolkitize.jl @@ -45,7 +45,7 @@ function modelingtoolkitize( eqs = vcat([0.0 ~ rhs[i] for i in 1:length(prob.f.resid_prototype)]...) else rhs = ArrayInterface.restructure(prob.u0, similar(vars, Num)) - prob.f(rhs, vars, params) + prob.f(rhs, vars, p isa MTKParameters ? (params,) : params) eqs = vcat([0.0 ~ rhs[i] for i in 1:length(rhs)]...) end From a42cec7a39dd5702cd2dd257879098e476b581a4 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 30 Jan 2025 11:54:03 +0530 Subject: [PATCH 0737/2176] test: use new codegen in index reduction test --- test/structural_transformation/index_reduction.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/structural_transformation/index_reduction.jl b/test/structural_transformation/index_reduction.jl index b94362bedc..d7f19e1fa2 100644 --- a/test/structural_transformation/index_reduction.jl +++ b/test/structural_transformation/index_reduction.jl @@ -62,7 +62,7 @@ first_order_idx1_pendulum = complete(ode_order_lowering(idx1_pendulum)) using OrdinaryDiffEq using LinearAlgebra -prob = ODEProblem(ODEFunction(first_order_idx1_pendulum), +prob = ODEProblem(first_order_idx1_pendulum, # [x, y, w, z, xˍt, yˍt, T] [1, 0, 0, 0, 0, 0, 0.0],# 0, 0, 0, 0], (0, 10.0), From 5e7b5838d93056ce0168ecff97faa843731ab985 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 30 Jan 2025 11:54:13 +0530 Subject: [PATCH 0738/2176] test: use new codegen in distributed test --- test/distributed.jl | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/test/distributed.jl b/test/distributed.jl index 3a53b9951e..0b75b8eeb3 100644 --- a/test/distributed.jl +++ b/test/distributed.jl @@ -15,12 +15,11 @@ addprocs(2) @everywhere @named de = ODESystem(eqs, t) @everywhere de = complete(de) -@everywhere ode_func = ODEFunction(de, [x, y, z], [σ, ρ, β]) @everywhere u0 = [19.0, 20.0, 50.0] @everywhere params = [16.0, 45.92, 4] -@everywhere ode_prob = ODEProblem(ode_func, u0, (0.0, 10.0), params) +@everywhere ode_prob = ODEProblem(de, u0, (0.0, 10.0), params) @everywhere begin using OrdinaryDiffEq From 8ce0eb2e4de2ab5decea8c02e458934691cd6821 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 30 Jan 2025 11:54:22 +0530 Subject: [PATCH 0739/2176] test: use new codegen in jumpsystem test --- test/jumpsystem.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/jumpsystem.jl b/test/jumpsystem.jl index 0fd6dc4af0..4cd5135939 100644 --- a/test/jumpsystem.jl +++ b/test/jumpsystem.jl @@ -104,8 +104,8 @@ sol = solve(jprob, SSAStepper(); saveat = 1.0) rate2(u, p, t) = 0.01u[2] jump2 = ConstantRateJump(rate2, affect2!) mtjumps = jprob.discrete_jump_aggregation -@test abs(mtjumps.rates[1](u, p, tf) - jump1.rate(u, p, tf)) < 10 * eps() -@test abs(mtjumps.rates[2](u, p, tf) - jump2.rate(u, p, tf)) < 10 * eps() +@test abs(mtjumps.rates[1](u, (p,), tf) - jump1.rate(u, p, tf)) < 10 * eps() +@test abs(mtjumps.rates[2](u, (p,), tf) - jump2.rate(u, p, tf)) < 10 * eps() mtjumps.affects![1](mtintegrator) jump1.affect!(integrator) @test all(integrator.u .== mtintegrator.u) From f9069b1e957347936c1e5aeb8431d14fd45443c7 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 30 Jan 2025 11:54:29 +0530 Subject: [PATCH 0740/2176] test: use new codegen in odesystem test --- test/odesystem.jl | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/odesystem.jl b/test/odesystem.jl index 30465d2975..87c192afc2 100644 --- a/test/odesystem.jl +++ b/test/odesystem.jl @@ -216,7 +216,7 @@ end prob = ODEProblem(ODEFunction{false}(lotka), [1.0, 1.0], (0.0, 1.0), [1.5, 1.0, 3.0, 1.0]) de = complete(modelingtoolkitize(prob)) -ODEFunction(de)(similar(prob.u0), prob.u0, prob.p, 0.1) +ODEFunction(de)(similar(prob.u0), prob.u0, (prob.p,), 0.1) function lotka(du, u, p, t) x = u[1] @@ -228,7 +228,7 @@ end prob = ODEProblem(lotka, [1.0, 1.0], (0.0, 1.0), [1.5, 1.0, 3.0, 1.0]) de = complete(modelingtoolkitize(prob)) -ODEFunction(de)(similar(prob.u0), prob.u0, prob.p, 0.1) +ODEFunction(de)(similar(prob.u0), prob.u0, (prob.p,), 0.1) # automatic unknown detection for DAEs @parameters k₁ k₂ k₃ @@ -1269,7 +1269,7 @@ end t, [u..., x..., o...], [p...]) sys1, = structural_simplify(sys, ([x...], [])) fn1, = ModelingToolkit.generate_function(sys1; expression = Val{false}) - @test_nowarn fn1(ones(4), 2ones(2), 3ones(2, 2), 4.0) + @test_nowarn fn1(ones(4), (2ones(2), 3ones(2, 2)), 4.0) sys2, = structural_simplify(sys, ([x...], []); split = false) fn2, = ModelingToolkit.generate_function(sys2; expression = Val{false}) @test_nowarn fn2(ones(4), 2ones(6), 4.0) From ddce985c692df4ac11620d9d823c5a17acfd11e6 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 30 Jan 2025 11:54:37 +0530 Subject: [PATCH 0741/2176] test: use new codegen in sdesystem test --- test/sdesystem.jl | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/test/sdesystem.jl b/test/sdesystem.jl index 40b8747d39..0a30982bac 100644 --- a/test/sdesystem.jl +++ b/test/sdesystem.jl @@ -24,14 +24,14 @@ noiseeqs = [0.1 * x, @named de = SDESystem(eqs, noiseeqs, tt, [x, y, z], [σ, ρ, β], tspan = (0.0, 10.0)) de = complete(de) f = eval(generate_diffusion_function(de)[1]) -@test f(ones(3), rand(3), nothing) == 0.1ones(3) +@test f(ones(3), (rand(3),), nothing) == 0.1ones(3) f = SDEFunction(de) -prob = SDEProblem(SDEFunction(de), [1.0, 0.0, 0.0], (0.0, 100.0), (10.0, 26.0, 2.33)) +prob = SDEProblem(de, [1.0, 0.0, 0.0], (0.0, 100.0), [10.0, 26.0, 2.33]) sol = solve(prob, SRIW1(), seed = 1) -probexpr = SDEProblem(SDEFunction(de), [1.0, 0.0, 0.0], (0.0, 100.0), - (10.0, 26.0, 2.33)) +probexpr = SDEProblem(de, [1.0, 0.0, 0.0], (0.0, 100.0), + [10.0, 26.0, 2.33]) solexpr = solve(eval(probexpr), SRIW1(), seed = 1) @test all(x -> x == 0, Array(sol - solexpr)) @@ -43,13 +43,13 @@ noiseeqs_nd = [0.01*x 0.01*x*y 0.02*x*z de = complete(de) f = eval(generate_diffusion_function(de)[1]) p = MTKParameters(de, [σ => 0.1, ρ => 0.2, β => 0.3]) -@test f([1, 2, 3.0], p..., nothing) == [0.01*1 0.01*1*2 0.02*1*3 +@test f([1, 2, 3.0], p, nothing) == [0.01*1 0.01*1*2 0.02*1*3 0.1 0.01*2 0.02*1*3 0.2 0.3 0.01*3] f = eval(generate_diffusion_function(de)[2]) du = ones(3, 3) -f(du, [1, 2, 3.0], p..., nothing) +f(du, [1, 2, 3.0], p, nothing) @test du == [0.01*1 0.01*1*2 0.02*1*3 0.1 0.01*2 0.02*1*3 0.2 0.3 0.01*3] @@ -86,7 +86,7 @@ function test_SDEFunction_no_eval() # Need to test within a function scope to trigger world age issues f = SDEFunction(de, eval_expression = false) p = MTKParameters(de, [σ => 10.0, ρ => 26.0, β => 2.33]) - @test f([1.0, 0.0, 0.0], p..., (0.0, 100.0)) ≈ [-10.0, 26.0, 0.0] + @test f([1.0, 0.0, 0.0], p, (0.0, 100.0)) ≈ [-10.0, 26.0, 0.0] end test_SDEFunction_no_eval() From b8d9de5fad889863b5e10a82872c9eeeabf0b5e7 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 30 Jan 2025 12:59:29 +0530 Subject: [PATCH 0742/2176] test: use new codegen in labelledarrays tests --- test/labelledarrays.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/labelledarrays.jl b/test/labelledarrays.jl index c9ee7ee50b..75d5de6d56 100644 --- a/test/labelledarrays.jl +++ b/test/labelledarrays.jl @@ -19,7 +19,7 @@ ff = ODEFunction(de, [x, y, z], [σ, ρ, β], jac = true) a = @SVector [1.0, 2.0, 3.0] b = SLVector(x = 1.0, y = 2.0, z = 3.0) c = [1.0, 2.0, 3.0] -p = SLVector(σ = 10.0, ρ = 26.0, β = 8 / 3) +p = (SLVector(σ = 10.0, ρ = 26.0, β = 8 / 3),) @test ff(a, p, 0.0) isa SVector @test typeof(ff(b, p, 0.0)) <: SLArray @test ff(c, p, 0.0) isa Vector From 5fb0effe815bd9b7b697957aadd829ad0e9c3563 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 31 Jan 2025 12:59:30 +0530 Subject: [PATCH 0743/2176] fix: add `collect_constants!` fallback for `Symbol` --- src/utils.jl | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/utils.jl b/src/utils.jl index e7259cc898..ec5323cba6 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -636,6 +636,8 @@ function collect_constants(x) return constants end +collect_constants!(::Any, ::Symbol) = nothing + function collect_constants!(constants, arr::AbstractArray) for el in arr collect_constants!(constants, el) From 3e33d96fa18d2b8e6b8b74d2b76b0fd1080eb196 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 31 Jan 2025 12:59:40 +0530 Subject: [PATCH 0744/2176] feat: support `get_cmap(::OptimizationSystem)` --- src/utils.jl | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/utils.jl b/src/utils.jl index ec5323cba6..9e90299a6c 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -739,7 +739,12 @@ end function get_cmap(sys, exprs = nothing) #Inject substitutions for constants => values - cs = collect_constants([collect(get_eqs(sys)); get_observed(sys)]) #ctrls? what else? + buffer = [] + has_eqs(sys) && append!(buffer, collect(get_eqs(sys))) + has_observed(sys) && append!(buffer, collect(get_observed(sys))) + has_op(sys) && push!(buffer, get_op(sys)) + has_constraints(sys) && append!(buffer, get_constraints(sys)) + cs = collect_constants(buffer) #ctrls? what else? if !empty_substitutions(sys) cs = [cs; collect_constants(get_substitutions(sys).subs)] end From 29ef316f9c97a0a64504996d89e327367eac30ae Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 31 Jan 2025 13:00:09 +0530 Subject: [PATCH 0745/2176] fix: handle array symbolics in `observed_equations_used_by` --- src/utils.jl | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/utils.jl b/src/utils.jl index 9e90299a6c..cf49d9f445 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -1076,7 +1076,8 @@ function observed_equations_used_by(sys::AbstractSystem, exprs; obsidxs = BitSet() for sym in involved_vars - idx = findfirst(isequal(sym), obsvars) + arrsym = iscall(sym) && operation(sym) === getindex ? arguments(sym)[1] : nothing + idx = findfirst(v -> isequal(v, sym) || isequal(v, arrsym), obsvars) idx === nothing && continue idx in obsidxs && continue parents = dfs_parents(graph, idx) From 322b5db4b31fcf7b44c1f75e1f1684f95f98b78a Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 31 Jan 2025 13:00:39 +0530 Subject: [PATCH 0746/2176] feat: add output type support to `build_function_wrapper` --- src/systems/codegen_utils.jl | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/src/systems/codegen_utils.jl b/src/systems/codegen_utils.jl index 35526c3c0e..dae9f07292 100644 --- a/src/systems/codegen_utils.jl +++ b/src/systems/codegen_utils.jl @@ -45,7 +45,7 @@ function array_variable_assignments(args...) return assignments end -function build_function_wrapper(sys::AbstractSystem, expr, args...; p_start = 2, p_end = is_time_dependent(sys) ? length(args) - 1 : length(args), wrap_delays = is_dde(sys), wrap_code = identity, add_observed = true, filter_observed = Returns(true), create_bindings = true, kwargs...) +function build_function_wrapper(sys::AbstractSystem, expr, args...; p_start = 2, p_end = is_time_dependent(sys) ? length(args) - 1 : length(args), wrap_delays = is_dde(sys), wrap_code = identity, add_observed = true, filter_observed = Returns(true), create_bindings = true, output_type = nothing, mkarray = nothing, kwargs...) isscalar = !(expr isa AbstractArray || symbolic_type(expr) == ArraySymbolic()) obs = filter(filter_observed, observed(sys)) @@ -116,5 +116,19 @@ function build_function_wrapper(sys::AbstractSystem, expr, args...; p_start = 2, wrap_code = wrap_code .∘ wrap_assignments(isscalar, assignments) - return build_function(expr, args...; wrap_code, kwargs...) + similarto = nothing + if output_type === Tuple + expr = MakeTuple(Tuple(expr)) + wrap_code = wrap_code[1] + elseif mkarray === nothing + similarto = output_type + else + expr = mkarray(expr, output_type) + wrap_code = wrap_code[2] + end + + if wrap_code isa Tuple && symbolic_type(expr) == ScalarSymbolic() + wrap_code = wrap_code[1] + end + return build_function(expr, args...; wrap_code, similarto, kwargs...) end From 07f82061c5212d186e56be47f6f4801eb4562c5a Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 31 Jan 2025 13:00:53 +0530 Subject: [PATCH 0747/2176] fix: fix `all_symbols` not returning dependent parameters --- src/systems/abstractsystem.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index aaa87fc02d..0444877432 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -824,7 +824,7 @@ end function SymbolicIndexingInterface.all_symbols(sys::AbstractSystem) syms = all_variable_symbols(sys) - for other in (parameter_symbols(sys), independent_variable_symbols(sys)) + for other in (full_parameters(sys), independent_variable_symbols(sys)) isempty(other) || (syms = vcat(syms, other)) end return syms From 0774860a624b882b0e3f543aa1b815aed4e5734b Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 31 Jan 2025 13:02:08 +0530 Subject: [PATCH 0748/2176] feat: use `build_function_wrapper` in `build_explicit_observed_function` --- src/systems/diffeqs/odesystem.jl | 245 ++++++++----------------------- 1 file changed, 62 insertions(+), 183 deletions(-) diff --git a/src/systems/diffeqs/odesystem.jl b/src/systems/diffeqs/odesystem.jl index c95e9f19db..c735b52c37 100644 --- a/src/systems/diffeqs/odesystem.jl +++ b/src/systems/diffeqs/odesystem.jl @@ -429,215 +429,94 @@ function build_explicit_observed_function(sys, ts; param_only = false, op = Operator, throw = true, - mkarray = MakeArray) + mkarray = nothing) is_tuple = ts isa Tuple if is_tuple ts = collect(ts) + output_type = Tuple end - if (isscalar = symbolic_type(ts) !== NotSymbolic()) - ts = [ts] - end - ts = unwrap.(ts) - issplit = has_index_cache(sys) && get_index_cache(sys) !== nothing - if is_dde(sys) - if issplit - ts = map( - x -> delay_to_function( - sys, x; history_arg = issplit ? MTKPARAMETERS_ARG : DEFAULT_PARAMS_ARG), - ts) - else - ts = map(x -> delay_to_function(sys, x), ts) - end - end - - vars = Set() - foreach(v -> vars!(vars, v; op), ts) - ivs = independent_variables(sys) - dep_vars = scalarize(setdiff(vars, ivs)) - obs = observed(sys) - if param_only - if has_index_cache(sys) && (ic = get_index_cache(sys)) !== nothing - obs = filter(obs) do eq - !(ContinuousTimeseries() in ic.observed_syms_to_timeseries[eq.lhs]) - end - else - obs = Equation[] + allsyms = all_symbols(sys) + function symbol_to_symbolic(sym) + sym isa Symbol || return sym + idx = findfirst(x -> (hasname(x) ? getname(x) : Symbol(x)) == sym, allsyms) + idx === nothing && return sym + sym = allsyms[idx] + if iscall(sym) && operation(sym) == getindex + sym = arguments(sym)[1] end + return sym end - - cs = collect_constants(obs) - if !isempty(cs) > 0 - cmap = map(x -> x => getdefault(x), cs) - obs = map(x -> x.lhs ~ substitute(x.rhs, cmap), obs) + if symbolic_type(ts) == NotSymbolic() && ts isa AbstractArray + ts = map(symbol_to_symbolic, ts) + else + ts = symbol_to_symbolic(ts) end - sts = param_only ? Set() : Set(unknowns(sys)) - sts = param_only ? Set() : - union(sts, - Set(arguments(st)[1] for st in sts if iscall(st) && operation(st) === getindex)) - - observed_idx = Dict(x.lhs => i for (i, x) in enumerate(obs)) - param_set = Set(full_parameters(sys)) - param_set = union(param_set, - Set(arguments(p)[1] for p in param_set if iscall(p) && operation(p) === getindex)) - param_set_ns = Set(unknowns(sys, p) for p in full_parameters(sys)) - param_set_ns = union(param_set_ns, - Set(arguments(p)[1] - for p in param_set_ns if iscall(p) && operation(p) === getindex)) - namespaced_to_obs = Dict(unknowns(sys, x.lhs) => x.lhs for x in obs) - namespaced_to_sts = param_only ? Dict() : - Dict(unknowns(sys, x) => x for x in unknowns(sys)) - - # FIXME: This is a rather rough estimate of dependencies. We assume - # the expression depends on everything before the `maxidx`. - subs = Dict() - maxidx = 0 - for s in dep_vars - if s in param_set || s in param_set_ns || - iscall(s) && - operation(s) === getindex && - (arguments(s)[1] in param_set || arguments(s)[1] in param_set_ns) - continue + vs = ModelingToolkit.vars(ts; op) + namespace_subs = Dict() + ns_map = Dict{Any, Any}(renamespace(sys, eq.lhs) => eq.lhs for eq in observed(sys)) + for sym in unknowns(sys) + ns_map[renamespace(sys, sym)] = sym + if iscall(sym) && operation(sym) === getindex + ns_map[renamespace(sys, arguments(sym)[1])] = arguments(sym)[1] end - idx = get(observed_idx, s, nothing) - if idx !== nothing - idx > maxidx && (maxidx = idx) - else - s′ = get(namespaced_to_obs, s, nothing) - if s′ !== nothing - subs[s] = s′ - s = s′ - idx = get(observed_idx, s, nothing) - end - if idx !== nothing - idx > maxidx && (maxidx = idx) - elseif !(s in sts) - s′ = get(namespaced_to_sts, s, nothing) - if s′ !== nothing - subs[s] = s′ - continue - end - if throw - Base.throw(ArgumentError("$s is neither an observed nor an unknown variable.")) - else - # TODO: return variables that don't exist in the system. - return nothing - end - end - continue + end + for sym in full_parameters(sys) + ns_map[renamespace(sys, sym)] = sym + if iscall(sym) && operation(sym) === getindex + ns_map[renamespace(sys, arguments(sym)[1])] = arguments(sym)[1] end end - ts = map(t -> substitute(t, subs), ts) - obsexprs = [] - - for i in 1:maxidx - eq = obs[i] - if is_dde(sys) - eq = delay_to_function( - sys, eq; history_arg = issplit ? MTKPARAMETERS_ARG : DEFAULT_PARAMS_ARG) + allsyms = Set(all_symbols(sys)) + for var in vs + var = unwrap(var) + newvar = get(ns_map, var, nothing) + if newvar !== nothing + namespace_subs[var] = newvar end - lhs = eq.lhs - rhs = eq.rhs - push!(obsexprs, lhs ← rhs) end + ts = fast_substitute(ts, namespace_subs) - if inputs !== nothing - ps = setdiff(ps, inputs) # Inputs have been converted to parameters by io_preprocessing, remove those from the parameter list - end - _ps = ps - if ps isa Tuple - ps = DestructuredArgs.(unwrap.(ps), inbounds = !checkbounds) - elseif has_index_cache(sys) && get_index_cache(sys) !== nothing - ps = DestructuredArgs.(reorder_parameters(get_index_cache(sys), unwrap.(ps))) - if isempty(ps) && inputs !== nothing - ps = (:EMPTY,) + obsfilter = if param_only + if is_split(sys) + let ic = get_index_cache(sys) + eq -> !(ContinuousTimeseries() in ic.observed_syms_to_timeseries[eq.lhs]) + end + else + Returns(false) end else - ps = (DestructuredArgs(unwrap.(ps), inbounds = !checkbounds),) + Returns(true) end - dvs = DestructuredArgs(unknowns(sys), inbounds = !checkbounds) - if is_dde(sys) - dvs = (dvs, DDE_HISTORY_FUN) + dvs = if param_only + () else - dvs = (dvs,) + (unknowns(sys),) end - p_start = param_only ? 1 : (length(dvs) + 1) if inputs === nothing - args = param_only ? [ps..., ivs...] : [dvs..., ps..., ivs...] + inputs = () else - inputs = unwrap.(inputs) - ipts = DestructuredArgs(inputs, inbounds = !checkbounds) - args = param_only ? [ipts, ps..., ivs...] : [dvs..., ipts, ps..., ivs...] - p_start += 1 - end - pre = get_postprocess_fbody(sys) - - array_wrapper = if param_only - wrap_array_vars(sys, ts; ps = _ps, dvs = nothing, inputs, history = is_dde(sys)) .∘ - wrap_parameter_dependencies(sys, isscalar) - else - wrap_array_vars(sys, ts; ps = _ps, inputs, history = is_dde(sys)) .∘ - wrap_parameter_dependencies(sys, isscalar) - end - mtkparams_wrapper = wrap_mtkparameters(sys, isscalar, p_start) - if mtkparams_wrapper isa Tuple - oop_mtkp_wrapper = mtkparams_wrapper[1] - else - oop_mtkp_wrapper = mtkparams_wrapper + ps = setdiff(ps, inputs) # Inputs have been converted to parameters by io_preprocessing, remove those from the parameter list + inputs = (inputs,) end - - # Need to keep old method of building the function since it uses `output_type`, - # which can't be provided to `build_function` - return_value = if isscalar - ts[1] - elseif is_tuple - MakeTuple(Tuple(ts)) + ps = reorder_parameters(sys, ps) + iv = if is_time_dependent(sys) + (get_iv(sys),) else - mkarray(ts, output_type) - end - oop_fn = Func(args, [], - pre(Let(obsexprs, - return_value, - false)), [Expr(:meta, :propagate_inbounds)]) |> array_wrapper[1] |> - oop_mtkp_wrapper |> toexpr - - if !checkbounds - oop_fn.args[end] = quote - @inbounds begin - $(oop_fn.args[end]) - end - end - end - oop_fn = expression ? oop_fn : eval_or_rgf(oop_fn; eval_expression, eval_module) - - if !isscalar - iip_fn = build_function(ts, - args...; - postprocess_fbody = pre, - wrap_code = mtkparams_wrapper .∘ array_wrapper .∘ - wrap_assignments(isscalar, obsexprs), - expression = Val{true})[2] - if !checkbounds - iip_fn.args[end] = quote - @inbounds begin - $(iip_fn.args[end]) - end - end - end - iip_fn.args[end] = quote - $(Expr(:meta, :propagate_inbounds)) - $(iip_fn.args[end]) - end - - if !expression - iip_fn = eval_or_rgf(iip_fn; eval_expression, eval_module) - end + () end - if isscalar || !return_inplace - return oop_fn + args = (dvs..., inputs..., ps..., iv...) + p_start = length(dvs) + length(inputs) + 1 + p_end = length(dvs) + length(inputs) + length(ps) + fns = build_function_wrapper( + sys, ts, args...; p_start, p_end, filter_observed = obsfilter, + output_type, mkarray, try_namespaced = true, expression = Val{true}) + if fns isa Tuple + oop, iip = eval_or_rgf.(fns; eval_expression, eval_module) + return return_inplace ? (oop, iip) : oop else - return oop_fn, iip_fn + return eval_or_rgf(fns; eval_expression, eval_module) end end From 14020bd0119cfc5425fa9e6ce2d8d8f73a14c709 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 31 Jan 2025 14:42:11 +0530 Subject: [PATCH 0749/2176] feat: allow opting out of destructuring `MTKParameters` in `build_function_wrapper` --- src/systems/codegen_utils.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/systems/codegen_utils.jl b/src/systems/codegen_utils.jl index dae9f07292..c0aa9b018b 100644 --- a/src/systems/codegen_utils.jl +++ b/src/systems/codegen_utils.jl @@ -45,7 +45,7 @@ function array_variable_assignments(args...) return assignments end -function build_function_wrapper(sys::AbstractSystem, expr, args...; p_start = 2, p_end = is_time_dependent(sys) ? length(args) - 1 : length(args), wrap_delays = is_dde(sys), wrap_code = identity, add_observed = true, filter_observed = Returns(true), create_bindings = true, output_type = nothing, mkarray = nothing, kwargs...) +function build_function_wrapper(sys::AbstractSystem, expr, args...; p_start = 2, p_end = is_time_dependent(sys) ? length(args) - 1 : length(args), wrap_delays = is_dde(sys), wrap_code = identity, add_observed = true, filter_observed = Returns(true), create_bindings = true, output_type = nothing, mkarray = nothing, wrap_mtkparameters = true, kwargs...) isscalar = !(expr isa AbstractArray || symbolic_type(expr) == ArraySymbolic()) obs = filter(filter_observed, observed(sys)) @@ -99,7 +99,7 @@ function build_function_wrapper(sys::AbstractSystem, expr, args...; p_start = 2, end end - if is_split(sys) + if is_split(sys) && wrap_mtkparameters if p_start > p_end args = (args[1:p_start-1]..., MTKPARAMETERS_ARG, args[p_end+1:end]...) else From 4686893b94509837937194081c62d5420ef3f8bc Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 31 Jan 2025 14:42:31 +0530 Subject: [PATCH 0750/2176] refactor: use `build_function_wrapper` in `generate_control_function` --- src/inputoutput.jl | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) diff --git a/src/inputoutput.jl b/src/inputoutput.jl index 6bdcac6dd4..7d2fa875b1 100644 --- a/src/inputoutput.jl +++ b/src/inputoutput.jl @@ -249,17 +249,8 @@ function generate_control_function(sys::AbstractODESystem, inputs = unbound_inpu ddvs = map(Differential(get_iv(sys)), dvs) args = (ddvs, args...) end - process = get_postprocess_fbody(sys) - wrapped_arrays_vars = disturbance_argument ? - wrap_array_vars( - sys, rhss; dvs, ps, inputs, extra_args = (disturbance_inputs,)) : - wrap_array_vars(sys, rhss; dvs, ps, inputs) - f = build_function(rhss, args...; postprocess_fbody = process, - expression = Val{true}, wrap_code = wrap_mtkparameters( - sys, false, 3, Int(disturbance_argument) + 1) .∘ - wrapped_arrays_vars .∘ - wrap_parameter_dependencies(sys, false), - kwargs...) + f = build_function_wrapper(sys, rhss, args...; p_start = 3 + implicit_dae, + p_end = length(p) + 2 + implicit_dae) f = eval_or_rgf.(f; eval_expression, eval_module) (; f, dvs, ps, io_sys = sys) end From f28df16df7ade0185c7b77fdbea52882b188de2c Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 31 Jan 2025 14:42:50 +0530 Subject: [PATCH 0751/2176] fix: refactor `modelingtoolkitize(::OptimizationProblem)` to use new codegen --- src/systems/optimization/modelingtoolkitize.jl | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/systems/optimization/modelingtoolkitize.jl b/src/systems/optimization/modelingtoolkitize.jl index b66f113f6c..0228a944cc 100644 --- a/src/systems/optimization/modelingtoolkitize.jl +++ b/src/systems/optimization/modelingtoolkitize.jl @@ -79,7 +79,11 @@ function modelingtoolkitize(prob::DiffEqBase.OptimizationProblem; if DiffEqBase.isinplace(prob) && !isnothing(prob.f.cons) lhs = Array{Num}(undef, num_cons) - prob.f.cons(lhs, vars, params) + if p isa MTKParameters + prob.f.cons(lhs, vars, params...) + else + prob.f.cons(lhs, vars, params) + end cons = Union{Equation, Inequality}[] if !isnothing(prob.lcons) @@ -108,7 +112,8 @@ function modelingtoolkitize(prob::DiffEqBase.OptimizationProblem; or pass the lower and upper bounds for inequality constraints.")) end elseif !isnothing(prob.f.cons) - cons = prob.f.cons(vars, params) + cons = p isa MTKParameters ? prob.f.cons(vars, params...) : + prob.f.cons(vars, params) else cons = [] end From cff02c9fd90e0ef659d1f547f65631101e726b5c Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 31 Jan 2025 14:43:24 +0530 Subject: [PATCH 0752/2176] refactor: use `build_function_wrapper` in `OptimizationProblem` codegen --- .../optimization/constraints_system.jl | 19 +++----- .../optimization/optimizationsystem.jl | 45 ++++++------------- 2 files changed, 19 insertions(+), 45 deletions(-) diff --git a/src/systems/optimization/constraints_system.jl b/src/systems/optimization/constraints_system.jl index 03225fc900..275079297a 100644 --- a/src/systems/optimization/constraints_system.jl +++ b/src/systems/optimization/constraints_system.jl @@ -172,12 +172,10 @@ end function generate_jacobian( sys::ConstraintsSystem, vs = unknowns(sys), ps = parameters(sys); - sparse = false, simplify = false, wrap_code = identity, kwargs...) + sparse = false, simplify = false, kwargs...) jac = calculate_jacobian(sys, sparse = sparse, simplify = simplify) p = reorder_parameters(sys, ps) - wrap_code = wrap_code .∘ wrap_array_vars(sys, jac; dvs = vs, ps) .∘ - wrap_parameter_dependencies(sys, false) - return build_function(jac, vs, p...; wrap_code, kwargs...) + return build_function_wrapper(sys, jac, vs, p...; kwargs...) end function calculate_hessian(sys::ConstraintsSystem; sparse = false, simplify = false) @@ -193,25 +191,18 @@ end function generate_hessian( sys::ConstraintsSystem, vs = unknowns(sys), ps = parameters(sys); - sparse = false, simplify = false, wrap_code = identity, kwargs...) + sparse = false, simplify = false, kwargs...) hess = calculate_hessian(sys, sparse = sparse, simplify = simplify) p = reorder_parameters(sys, ps) - wrap_code = wrap_code .∘ wrap_array_vars(sys, hess; dvs = vs, ps) .∘ - wrap_parameter_dependencies(sys, false) - return build_function(hess, vs, p...; wrap_code, kwargs...) + return build_function_wrapper(sys, hess, vs, p...; kwargs...) end function generate_function(sys::ConstraintsSystem, dvs = unknowns(sys), ps = parameters(sys); - wrap_code = identity, kwargs...) lhss = generate_canonical_form_lhss(sys) - pre, sol_states = get_substitutions_and_solved_unknowns(sys) p = reorder_parameters(sys, value.(ps)) - wrap_code = wrap_code .∘ wrap_array_vars(sys, lhss; dvs, ps) .∘ - wrap_parameter_dependencies(sys, false) - func = build_function(lhss, value.(dvs), p...; postprocess_fbody = pre, - states = sol_states, wrap_code, kwargs...) + func = build_function_wrapper(sys, lhss, value.(dvs), p...; kwargs...) cstr = constraints(sys) lcons = fill(-Inf, length(cstr)) diff --git a/src/systems/optimization/optimizationsystem.jl b/src/systems/optimization/optimizationsystem.jl index 860e063e35..3ae98be084 100644 --- a/src/systems/optimization/optimizationsystem.jl +++ b/src/systems/optimization/optimizationsystem.jl @@ -194,16 +194,10 @@ function calculate_gradient(sys::OptimizationSystem) end function generate_gradient(sys::OptimizationSystem, vs = unknowns(sys), - ps = parameters(sys); - wrap_code = identity, - kwargs...) + ps = parameters(sys); kwargs...) grad = calculate_gradient(sys) - pre = get_preprocess_constants(grad) p = reorder_parameters(sys, ps) - wrap_code = wrap_code .∘ wrap_array_vars(sys, grad; dvs = vs, ps) .∘ - wrap_parameter_dependencies(sys, !(grad isa AbstractArray)) - return build_function(grad, vs, p...; postprocess_fbody = pre, wrap_code, - kwargs...) + return build_function_wrapper(sys, grad, vs, p...; kwargs...) end function calculate_hessian(sys::OptimizationSystem) @@ -212,34 +206,22 @@ end function generate_hessian( sys::OptimizationSystem, vs = unknowns(sys), ps = parameters(sys); - sparse = false, wrap_code = identity, kwargs...) + sparse = false, kwargs...) if sparse hess = sparsehessian(objective(sys), unknowns(sys)) else hess = calculate_hessian(sys) end - pre = get_preprocess_constants(hess) p = reorder_parameters(sys, ps) - wrap_code = wrap_code .∘ wrap_array_vars(sys, hess; dvs = vs, ps) .∘ - wrap_parameter_dependencies(sys, false) - return build_function(hess, vs, p...; postprocess_fbody = pre, wrap_code, - kwargs...) + return build_function_wrapper(sys, hess, vs, p...; kwargs...) end function generate_function(sys::OptimizationSystem, vs = unknowns(sys), ps = parameters(sys); - wrap_code = identity, - kwargs...) - eqs = subs_constants(objective(sys)) - p = if has_index_cache(sys) - reorder_parameters(get_index_cache(sys), ps) - else - (ps,) - end - wrap_code = wrap_code .∘ wrap_array_vars(sys, eqs; dvs = vs, ps) .∘ - wrap_parameter_dependencies(sys, !(eqs isa AbstractArray)) - return build_function(eqs, vs, p...; wrap_code, kwargs...) + eqs = objective(sys) + p = reorder_parameters(sys, ps) + return build_function_wrapper(sys, eqs, vs, p...; kwargs...) end function namespace_objective(sys::AbstractSystem) @@ -368,7 +350,7 @@ function DiffEqBase.OptimizationProblem{iip}(sys::OptimizationSystem, u0map, f = let _f = eval_or_rgf( generate_function( sys, checkbounds = checkbounds, linenumbers = linenumbers, - expression = Val{true}); + expression = Val{true}, wrap_mtkparameters = false); eval_expression, eval_module) __f(u, p) = _f(u, p) @@ -382,7 +364,8 @@ function DiffEqBase.OptimizationProblem{iip}(sys::OptimizationSystem, u0map, generate_gradient( sys, checkbounds = checkbounds, linenumbers = linenumbers, - parallel = parallel, expression = Val{true}); + parallel = parallel, expression = Val{true}, + wrap_mtkparameters = false); eval_expression, eval_module) _grad(u, p) = grad_oop(u, p) @@ -401,7 +384,7 @@ function DiffEqBase.OptimizationProblem{iip}(sys::OptimizationSystem, u0map, sys, checkbounds = checkbounds, linenumbers = linenumbers, sparse = sparse, parallel = parallel, - expression = Val{true}); + expression = Val{true}, wrap_mtkparameters = false); eval_expression, eval_module) _hess(u, p) = hess_oop(u, p) @@ -427,7 +410,7 @@ function DiffEqBase.OptimizationProblem{iip}(sys::OptimizationSystem, u0map, cons_sys = complete(cons_sys) cons, lcons_, ucons_ = generate_function(cons_sys, checkbounds = checkbounds, linenumbers = linenumbers, - expression = Val{true}) + expression = Val{true}; wrap_mtkparameters = false) cons = let (cons_oop, cons_iip) = eval_or_rgf.(cons; eval_expression, eval_module) _cons(u, p) = cons_oop(u, p) _cons(resid, u, p) = cons_iip(resid, u, p) @@ -440,7 +423,7 @@ function DiffEqBase.OptimizationProblem{iip}(sys::OptimizationSystem, u0map, checkbounds = checkbounds, linenumbers = linenumbers, parallel = parallel, expression = Val{true}, - sparse = cons_sparse); + sparse = cons_sparse, wrap_mtkparameters = false); eval_expression, eval_module) _cons_j(u, p) = cons_jac_oop(u, p) @@ -458,7 +441,7 @@ function DiffEqBase.OptimizationProblem{iip}(sys::OptimizationSystem, u0map, cons_sys, checkbounds = checkbounds, linenumbers = linenumbers, sparse = cons_sparse, parallel = parallel, - expression = Val{true}); + expression = Val{true}, wrap_mtkparameters = false); eval_expression, eval_module) _cons_h(u, p) = cons_hess_oop(u, p) From 4274ec9bb9d741e5767f514c92014159546df6c3 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 31 Jan 2025 15:03:45 +0530 Subject: [PATCH 0753/2176] docs: add docstring for code generation utils --- src/systems/codegen_utils.jl | 92 +++++++++++++++++++++++++++++++++--- 1 file changed, 86 insertions(+), 6 deletions(-) diff --git a/src/systems/codegen_utils.jl b/src/systems/codegen_utils.jl index c0aa9b018b..1bf0830e7d 100644 --- a/src/systems/codegen_utils.jl +++ b/src/systems/codegen_utils.jl @@ -1,43 +1,71 @@ +""" + $(TYPEDSIGNATURES) + +Return the name for the `i`th argument in a function generated by `build_function_wrapper`. +""" function generated_argument_name(i::Int) return Symbol(:__mtk_arg_, i) end +""" + $(TYPEDSIGNATURES) + +Given the arguments to `build_function_wrapper`, return a list of assignments which +reconstruct array variables if they are present scalarized in `args`. +""" function array_variable_assignments(args...) + # map array symbolic to an identically sized array where each element is (buffer_idx, idx_in_buffer) var_to_arridxs = Dict{BasicSymbolic, Array{Tuple{Int, Int}}}() for (i, arg) in enumerate(args) + # filter out non-arrays + # any element of args which is not an array is assumed to not contain a + # scalarized array symbolic. This works because the only non-array element + # is the independent variable symbolic_type(arg) == NotSymbolic() || continue arg isa AbstractArray || continue + # go through symbolics for (j, var) in enumerate(arg) var = unwrap(var) + # filter out non-array-symbolics iscall(var) || continue operation(var) == getindex || continue arrvar = arguments(var)[1] - idxbuffer = get!(() -> map(Returns((0, 0)), eachindex(arrvar)), var_to_arridxs, arrvar) + # get and/or construct the buffer storing indexes + idxbuffer = get!( + () -> map(Returns((0, 0)), eachindex(arrvar)), var_to_arridxs, arrvar) idxbuffer[arguments(var)[2:end]...] = (i, j) end end assignments = Assignment[] for (arrvar, idxs) in var_to_arridxs + # all elements of the array need to be present in `args` to form the + # reconstructing assignment any(iszero ∘ first, idxs) && continue + # if they are all in the same buffer, we can take a shortcut and `view` into it if allequal(Iterators.map(first, idxs)) buffer_idx = first(first(idxs)) idxs = map(last, idxs) + # if all the elements are contiguous and ordered, turn the array of indexes into a range + # to help reduce allocations if first(idxs) < last(idxs) && vec(idxs) == first(idxs):last(idxs) idxs = first(idxs):last(idxs) elseif vec(idxs) == last(idxs):-1:first(idxs) idxs = last(idxs):-1:first(idxs) else + # Otherwise, turn the indexes into an `SArray` so they're stack-allocated idxs = SArray{Tuple{size(idxs)...}}(idxs) end + # view and reshape push!(assignments, arrvar ← term(reshape, term(view, generated_argument_name(buffer_idx), idxs), size(arrvar))) else elems = map(idxs) do idx i, j = idx term(getindex, generated_argument_name(i), j) end + # use `MakeArray` and generate a stack-allocated array push!(assignments, arrvar ← MakeArray(elems, SArray)) end end @@ -45,22 +73,63 @@ function array_variable_assignments(args...) return assignments end +""" + $(TYPEDSIGNATURES) + +A wrapper around `build_function` which performs the necessary transformations for +code generation of all types of systems. `expr` is the expression returned from the +generated functions, and `args` are the arguments. + +# Keyword Arguments + +- `p_start`, `p_end`: Denotes the indexes in `args` where the buffers of the splatted + `MTKParameters` object are present. These are collapsed into a single argument and + destructured inside the function. `p_start` must also be provided for non-split systems + since it is used by `wrap_delays`. +- `wrap_delays`: Whether to transform delayed unknowns of `sys` present in `expr` into + calls to a history function. The history function is added to the list of arguments + right before parameters, at the index `p_start`. +- `wrap_code`: Forwarded to `build_function`. +- `add_observed`: Whether to add assignment statements for observed equations in the + generated code. +- `filter_observed`: A predicate function to filter out observed equations which should + not be added to the generated code. +- `create_bindings`: Whether to explicitly destructure arrays of symbolics present in + `args` in the generated code. If `false`, all usages of the individual symbolics will + instead call `getindex` on the relevant argument. This is useful if the generated + function writes to one of its arguments and expects subsequent code to use the new + values. Note that the collapsed `MTKParameters` argument will always be explicitly + destructured regardless of this keyword argument. +- `output_type`: The type of the output buffer. If `mkarray` (see below) is `nothing`, + this will be passed to the `similarto` argument of `build_function`. If `output_type` + is `Tuple`, `expr` will be wrapped in `SymbolicUtils.Code.MakeTuple` (regardless of + whether it is scalar or an array). +- `mkarray`: A function which accepts `expr` and `output_type` and returns a code + generation object similar to `MakeArray` or `MakeTuple` to be used to generate + code for `expr`. +- `wrap_mtkparameters`: Whether to collapse parameter buffers for a split system into a + argument. + +All other keyword arguments are forwarded to `build_function`. +""" function build_function_wrapper(sys::AbstractSystem, expr, args...; p_start = 2, p_end = is_time_dependent(sys) ? length(args) - 1 : length(args), wrap_delays = is_dde(sys), wrap_code = identity, add_observed = true, filter_observed = Returns(true), create_bindings = true, output_type = nothing, mkarray = nothing, wrap_mtkparameters = true, kwargs...) isscalar = !(expr isa AbstractArray || symbolic_type(expr) == ArraySymbolic()) - + # filter observed equations obs = filter(filter_observed, observed(sys)) + # turn delayed unknowns into calls to the history function if wrap_delays history_arg = is_split(sys) ? MTKPARAMETERS_ARG : generated_argument_name(p_start) obs = map(obs) do eq delay_to_function(sys, eq; history_arg) end expr = delay_to_function(sys, expr; history_arg) - args = (args[1:p_start-1]..., DDE_HISTORY_FUN, args[p_start:end]...) + # add extra argument + args = (args[1:(p_start - 1)]..., DDE_HISTORY_FUN, args[p_start:end]...) p_start += 1 p_end += 1 end pdeps = parameter_dependencies(sys) - + # get the constants to add to the code cmap, _ = get_cmap(sys) extra_constants = collect_constants(expr) filter!(extra_constants) do c @@ -69,13 +138,15 @@ function build_function_wrapper(sys::AbstractSystem, expr, args...; p_start = 2, for c in extra_constants push!(cmap, c ~ getdefault(c)) end + # only get the necessary observed equations, avoiding extra computation if add_observed obsidxs = observed_equations_used_by(sys, expr) else obsidxs = Int[] end + # similarly for parameter dependency equations pdepidxs = observed_equations_used_by(sys, expr; obs = pdeps) - + # assignments for reconstructing scalarized array symbolics assignments = array_variable_assignments(args...) for eq in Iterators.flatten((cmap, pdeps[pdepidxs], obs[obsidxs])) @@ -84,6 +155,9 @@ function build_function_wrapper(sys::AbstractSystem, expr, args...; p_start = 2, args = ntuple(Val(length(args))) do i arg = args[i] + # for time-dependent systems, all arguments are passed through `time_varying_as_func` + # TODO: This is legacy behavior and a candidate for removal in v10 since we have callable + # parameters now. if is_time_dependent(sys) arg = if symbolic_type(arg) == NotSymbolic() arg isa AbstractArray ? @@ -92,6 +166,7 @@ function build_function_wrapper(sys::AbstractSystem, expr, args...; p_start = 2, time_varying_as_func(unwrap(arg), sys) end end + # Make sure to use the proper names for arguments if symbolic_type(arg) == NotSymbolic() && arg isa AbstractArray DestructuredArgs(arg, generated_argument_name(i); create_bindings) else @@ -99,9 +174,11 @@ function build_function_wrapper(sys::AbstractSystem, expr, args...; p_start = 2, end end + # wrap into a single MTKParameters argument if is_split(sys) && wrap_mtkparameters if p_start > p_end - args = (args[1:p_start-1]..., MTKPARAMETERS_ARG, args[p_end+1:end]...) + # In case there are no parameter buffers, still insert an argument + args = (args[1:(p_start - 1)]..., MTKPARAMETERS_ARG, args[(p_end + 1):end]...) else # cannot apply `create_bindings` here since it doesn't nest args = (args[1:(p_start - 1)]..., @@ -110,12 +187,14 @@ function build_function_wrapper(sys::AbstractSystem, expr, args...; p_start = 2, end end + # add preface assignments if has_preface(sys) && (pref = preface(sys)) !== nothing append!(assignments, pref) end wrap_code = wrap_code .∘ wrap_assignments(isscalar, assignments) + # handling of `output_type` and `mkarray` similarto = nothing if output_type === Tuple expr = MakeTuple(Tuple(expr)) @@ -127,6 +206,7 @@ function build_function_wrapper(sys::AbstractSystem, expr, args...; p_start = 2, wrap_code = wrap_code[2] end + # scalar `build_function` only accepts a single function for `wrap_code`. if wrap_code isa Tuple && symbolic_type(expr) == ScalarSymbolic() wrap_code = wrap_code[1] end From dfc49b469414aa3ab86abf50707f75d8d6bc81fd Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 31 Jan 2025 15:21:12 +0530 Subject: [PATCH 0754/2176] feat: add `extra_assignments` to `build_function_wrapper` --- src/systems/codegen_utils.jl | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/systems/codegen_utils.jl b/src/systems/codegen_utils.jl index 1bf0830e7d..3db890e0c3 100644 --- a/src/systems/codegen_utils.jl +++ b/src/systems/codegen_utils.jl @@ -59,7 +59,10 @@ function array_variable_assignments(args...) idxs = SArray{Tuple{size(idxs)...}}(idxs) end # view and reshape - push!(assignments, arrvar ← term(reshape, term(view, generated_argument_name(buffer_idx), idxs), size(arrvar))) + push!(assignments, + arrvar ← + term(reshape, term(view, generated_argument_name(buffer_idx), idxs), + size(arrvar))) else elems = map(idxs) do idx i, j = idx @@ -109,10 +112,17 @@ generated functions, and `args` are the arguments. code for `expr`. - `wrap_mtkparameters`: Whether to collapse parameter buffers for a split system into a argument. +- `extra_assignments`: Extra `Assignment` statements to prefix to `expr`, after all other + assignments. All other keyword arguments are forwarded to `build_function`. """ -function build_function_wrapper(sys::AbstractSystem, expr, args...; p_start = 2, p_end = is_time_dependent(sys) ? length(args) - 1 : length(args), wrap_delays = is_dde(sys), wrap_code = identity, add_observed = true, filter_observed = Returns(true), create_bindings = true, output_type = nothing, mkarray = nothing, wrap_mtkparameters = true, kwargs...) +function build_function_wrapper(sys::AbstractSystem, expr, args...; p_start = 2, + p_end = is_time_dependent(sys) ? length(args) - 1 : length(args), + wrap_delays = is_dde(sys), wrap_code = identity, + add_observed = true, filter_observed = Returns(true), + create_bindings = true, output_type = nothing, mkarray = nothing, + wrap_mtkparameters = true, extra_assignments = Assignment[], kwargs...) isscalar = !(expr isa AbstractArray || symbolic_type(expr) == ArraySymbolic()) # filter observed equations obs = filter(filter_observed, observed(sys)) @@ -152,6 +162,7 @@ function build_function_wrapper(sys::AbstractSystem, expr, args...; p_start = 2, for eq in Iterators.flatten((cmap, pdeps[pdepidxs], obs[obsidxs])) push!(assignments, eq.lhs ← eq.rhs) end + append!(assignments, extra_assignments) args = ntuple(Val(length(args))) do i arg = args[i] From 884a0fb2bf8911786bdde71a7546e7a07a7dcb6a Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 31 Jan 2025 15:21:23 +0530 Subject: [PATCH 0755/2176] refactor: use `build_function_wrapper` in `SCCNonlinearProblem` --- src/systems/nonlinear/nonlinearsystem.jl | 35 ++++++++---------------- 1 file changed, 11 insertions(+), 24 deletions(-) diff --git a/src/systems/nonlinear/nonlinearsystem.jl b/src/systems/nonlinear/nonlinearsystem.jl index 08ede84033..b5256ca4ca 100644 --- a/src/systems/nonlinear/nonlinearsystem.jl +++ b/src/systems/nonlinear/nonlinearsystem.jl @@ -563,7 +563,7 @@ struct CacheWriter{F} end function (cw::CacheWriter)(p, sols) - cw.fn(p.caches, sols, p...) + cw.fn(p.caches, sols, p) end function CacheWriter(sys::AbstractSystem, buffer_types::Vector{TypeT}, @@ -572,22 +572,15 @@ function CacheWriter(sys::AbstractSystem, buffer_types::Vector{TypeT}, ps = parameters(sys) rps = reorder_parameters(sys, ps) obs_assigns = [eq.lhs ← eq.rhs for eq in obseqs] - cmap, cs = get_cmap(sys) - cmap_assigns = [eq.lhs ← eq.rhs for eq in cmap] - - outsyms = [Symbol(:out, i) for i in eachindex(buffer_types)] body = map(eachindex(buffer_types), buffer_types) do i, T Symbol(:tmp, i) ← SetArray(true, :(out[$i]), get(exprs, T, [])) end - fn = Func( - [:out, DestructuredArgs(DestructuredArgs.(solsyms)), - DestructuredArgs.(rps)...], - [], - Let(body, :()) - ) |> wrap_assignments(false, obs_assigns)[2] |> - wrap_parameter_dependencies(sys, false)[2] |> - wrap_array_vars(sys, []; dvs = nothing, inputs = [])[2] |> - wrap_assignments(false, cmap_assigns)[2] |> toexpr + + fn = build_function_wrapper( + sys, nothing, :out, DestructuredArgs(DestructuredArgs.(solsyms)), + DestructuredArgs.(rps)...; p_start = 3, p_end = length(rps) + 2, + expression = Val{true}, add_observed = false, + extra_assignments = [obs_assigns; body]) return CacheWriter(eval_or_rgf(fn; eval_expression, eval_module)) end @@ -601,21 +594,15 @@ function SCCNonlinearFunction{iip}( obs_assignments = [eq.lhs ← eq.rhs for eq in _obs] - cmap, cs = get_cmap(sys) - cmap_assignments = [eq.lhs ← eq.rhs for eq in cmap] rhss = [eq.rhs - eq.lhs for eq in _eqs] - wrap_code = wrap_assignments(false, cmap_assignments) .∘ - (wrap_array_vars(sys, rhss; dvs = _dvs, cachesyms)) .∘ - wrap_parameter_dependencies(sys, false) .∘ - wrap_assignments(false, obs_assignments) - f_gen = build_function( - rhss, _dvs, rps..., cachesyms...; wrap_code, expression = Val{true}) + f_gen = build_function_wrapper(sys, + rhss, _dvs, rps..., cachesyms...; p_start = 2, + p_end = length(rps) + length(cachesyms) + 1, add_observed = false, + extra_assignments = obs_assignments, expression = Val{true}) f_oop, f_iip = eval_or_rgf.(f_gen; eval_expression, eval_module) f(u, p) = f_oop(u, p) - f(u, p::MTKParameters) = f_oop(u, p...) f(resid, u, p) = f_iip(resid, u, p) - f(resid, u, p::MTKParameters) = f_iip(resid, u, p...) subsys = NonlinearSystem(_eqs, _dvs, ps; observed = _obs, parameter_dependencies = parameter_dependencies(sys), name = nameof(sys)) From 4c39175fe33bd380e1b009e048cecaa0541a4d70 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 3 Feb 2025 12:21:33 +0530 Subject: [PATCH 0756/2176] fix: search through filtered observed equations in `build_function_wrapper` --- src/systems/codegen_utils.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/systems/codegen_utils.jl b/src/systems/codegen_utils.jl index 3db890e0c3..fd54ef01ca 100644 --- a/src/systems/codegen_utils.jl +++ b/src/systems/codegen_utils.jl @@ -149,8 +149,8 @@ function build_function_wrapper(sys::AbstractSystem, expr, args...; p_start = 2, push!(cmap, c ~ getdefault(c)) end # only get the necessary observed equations, avoiding extra computation - if add_observed - obsidxs = observed_equations_used_by(sys, expr) + if add_observed && !isempty(obs) + obsidxs = observed_equations_used_by(sys, expr; obs) else obsidxs = Int[] end From 1fc081f3ca7ff08c7e36e3cad34e1484e91e885e Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 3 Feb 2025 12:23:32 +0530 Subject: [PATCH 0757/2176] build: bump Symbolics compat --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index e4b2238170..c069b49b5a 100644 --- a/Project.toml +++ b/Project.toml @@ -147,7 +147,7 @@ StochasticDiffEq = "6.72.1" StochasticDelayDiffEq = "1.8.1" SymbolicIndexingInterface = "0.3.37" SymbolicUtils = "3.10.1" -Symbolics = "6.25" +Symbolics = "6.27" URIs = "1" UnPack = "0.1, 1.0" Unitful = "1.1" From b5305950778d256c70462e7858f4cb3b79ff835e Mon Sep 17 00:00:00 2001 From: vyudu Date: Mon, 3 Feb 2025 07:58:14 -0500 Subject: [PATCH 0758/2176] working simplification --- .../symbolics_tearing.jl | 58 +++++++++++++++++-- src/structural_transformation/utils.jl | 18 +++--- src/systems/systemstructure.jl | 30 +++++++--- 3 files changed, 88 insertions(+), 18 deletions(-) diff --git a/src/structural_transformation/symbolics_tearing.jl b/src/structural_transformation/symbolics_tearing.jl index d95951c41e..8c802d9765 100644 --- a/src/structural_transformation/symbolics_tearing.jl +++ b/src/structural_transformation/symbolics_tearing.jl @@ -240,7 +240,7 @@ end function tearing_reassemble(state::TearingState, var_eq_matching, full_var_eq_matching = nothing; simplify = false, mm = nothing, cse_hack = true, array_hack = true) @unpack fullvars, sys, structure = state - @unpack solvable_graph, var_to_diff, eq_to_diff, graph = structure + @unpack solvable_graph, var_to_diff, eq_to_diff, graph, lowest_shift = structure extra_vars = Int[] if full_var_eq_matching !== nothing for v in 𝑑vertices(state.structure.graph) @@ -279,6 +279,7 @@ function tearing_reassemble(state::TearingState, var_eq_matching, iv = D = nothing end diff_to_var = invview(var_to_diff) + dummy_sub = Dict() for var in 1:length(fullvars) dv = var_to_diff[var] @@ -310,7 +311,10 @@ function tearing_reassemble(state::TearingState, var_eq_matching, diff_to_var[dv] = nothing end end + @show neweqs + println("Post state selection.") + # `SelectedState` information is no longer needed past here. State selection # is done. All non-differentiated variables are algebraic variables, and all # variables that appear differentiated are differential variables. @@ -331,10 +335,28 @@ function tearing_reassemble(state::TearingState, var_eq_matching, order += 1 dv = dv′ end + println("Order") + @show fullvars[dv] + is_only_discrete(state.structure) && begin + var = fullvars[dv] + key = operation(var) isa Shift ? only(arguments(var)) : var + order = -get(lowest_shift, key, 0) - order + end order, dv end end + lower_name = is_only_discrete(state.structure) ? lower_varname_withshift : lower_varname_with_unit + # is_only_discrete(state.structure) && for v in 1:length(fullvars) + # var = fullvars[v] + # op = operation(var) + # if op isa Shift + # x = only(arguments(var)) + # lowest_shift_idxs[v] + # op.steps == lowest_shift[x] && (fullvars[v] = lower_varname_withshift(var, iv, -op.steps)) + # end + # end + #retear = BitSet() # There are three cases where we want to generate new variables to convert # the system into first order (semi-implicit) ODEs. @@ -384,9 +406,28 @@ function tearing_reassemble(state::TearingState, var_eq_matching, eq_var_matching = invview(var_eq_matching) linear_eqs = mm === nothing ? Dict{Int, Int}() : Dict(reverse(en) for en in enumerate(mm.nzrows)) + for v in 1:length(var_to_diff) - dv = var_to_diff[v] + println() + @show fullvars + @show diff_to_var + is_highest_discrete = begin + var = fullvars[v] + op = operation(var) + if (!is_only_discrete(state.structure) || op isa Shift) + false + elseif !haskey(lowest_shift, var) + false + else + low = lowest_shift[var] + idx = findfirst(x -> isequal(x, Shift(iv, low)(var)), fullvars) + true + end + end + dv = is_highest_discrete ? idx : var_to_diff[v] + @show (v, fullvars[v], dv) dv isa Int || continue + solved = var_eq_matching[dv] isa Int solved && continue # check if there's `D(x) = x_t` already @@ -404,17 +445,19 @@ function tearing_reassemble(state::TearingState, var_eq_matching, diff_to_var[v_t] === nothing) @assert dv in rvs dummy_eq = eq + @show "FOUND DUMMY EQ" @goto FOUND_DUMMY_EQ end end dx = fullvars[dv] # add `x_t` - order, lv = var_order(dv) - x_t = lower_varname_withshift(fullvars[lv], iv, order) + @show order, lv = var_order(dv) + x_t = lower_name(fullvars[lv], iv, order) push!(fullvars, simplify_shifts(x_t)) v_t = length(fullvars) v_t_idx = add_vertex!(var_to_diff) add_vertex!(graph, DST) + @show x_t, dx # TODO: do we care about solvable_graph? We don't use them after # `dummy_derivative_graph`. add_vertex!(solvable_graph, DST) @@ -433,10 +476,16 @@ function tearing_reassemble(state::TearingState, var_eq_matching, add_edge!(solvable_graph, dummy_eq, dv) @assert nsrcs(graph) == nsrcs(solvable_graph) == dummy_eq @label FOUND_DUMMY_EQ + @show is_highest_discrete + @show diff_to_var + @show v_t, dv + # If var = x with no shift, then + is_highest_discrete && (lowest_shift[x_t] = lowest_shift[fullvars[v]]) var_to_diff[v_t] = var_to_diff[dv] var_eq_matching[dv] = unassigned eq_var_matching[dummy_eq] = dv end + @show neweqs # Will reorder equations and unknowns to be: # [diffeqs; ...] @@ -537,6 +586,7 @@ function tearing_reassemble(state::TearingState, var_eq_matching, deps = Vector{Int}[i == 1 ? Int[] : collect(1:(i - 1)) for i in 1:length(solved_equations)] + # Contract the vertices in the structure graph to make the structure match # the new reality of the system we've just created. graph = contract_variables(graph, var_eq_matching, varsperm, eqsperm, diff --git a/src/structural_transformation/utils.jl b/src/structural_transformation/utils.jl index 29c0f66756..dd063c0b07 100644 --- a/src/structural_transformation/utils.jl +++ b/src/structural_transformation/utils.jl @@ -451,18 +451,22 @@ end function lower_varname_withshift(var, iv, order) order == 0 && return var + ds = "$iv-$order" + d_separator = 'ˍ' + if ModelingToolkit.isoperator(var, ModelingToolkit.Shift) O = only(arguments(var)) oldop = operation(O) - ds = "$iv-$order" - d_separator = 'ˍ' newname = Symbol(string(nameof(oldop)), d_separator, ds) - - newvar = maketerm(typeof(O), Symbolics.rename(oldop, newname), Symbolics.children(O), Symbolics.metadata(O)) - setmetadata(newvar, Symbolics.VariableSource, (:variables, newname)) - return ModelingToolkit._with_unit(identity, newvar, iv) + else + O = var + oldop = operation(var) + varname = split(string(nameof(oldop)), d_separator)[1] + newname = Symbol(varname, d_separator, ds) end - return lower_varname_with_unit(var, iv, order) + newvar = maketerm(typeof(O), Symbolics.rename(oldop, newname), Symbolics.children(O), Symbolics.metadata(O)) + setmetadata(newvar, Symbolics.VariableSource, (:variables, newname)) + return ModelingToolkit._with_unit(identity, newvar, iv) end function isdoubleshift(var) diff --git a/src/systems/systemstructure.jl b/src/systems/systemstructure.jl index 1bdc11f06a..44558ade6c 100644 --- a/src/systems/systemstructure.jl +++ b/src/systems/systemstructure.jl @@ -140,17 +140,21 @@ get_fullvars(ts::TransformationState) = ts.fullvars has_equations(::TransformationState) = true Base.@kwdef mutable struct SystemStructure - # Maps the (index of) a variable to the (index of) the variable describing - # its derivative. + """Maps the (index of) a variable to the (index of) the variable describing its derivative.""" var_to_diff::DiffGraph + """Maps the (index of) a """ eq_to_diff::DiffGraph # Can be access as # `graph` to automatically look at the bipartite graph # or as `torn` to assert that tearing has run. + """Incidence graph of the system of equations. An edge from equation x to variable y exists if variable y appears in equation x.""" graph::BipartiteGraph{Int, Nothing} + """.""" solvable_graph::Union{BipartiteGraph{Int, Nothing}, Nothing} var_types::Union{Vector{VariableType}, Nothing} + """Whether the system is discrete.""" only_discrete::Bool + lowest_shift::Union{Dict, Nothing} end function Base.copy(structure::SystemStructure) @@ -346,6 +350,8 @@ function TearingState(sys; quick_cancel = false, check = true) eqs[i] = eqs[i].lhs ~ rhs end end + + ### Handle discrete variables lowest_shift = Dict() for var in fullvars if ModelingToolkit.isoperator(var, ModelingToolkit.Shift) @@ -430,10 +436,10 @@ function TearingState(sys; quick_cancel = false, check = true) ts = TearingState(sys, fullvars, SystemStructure(complete(var_to_diff), complete(eq_to_diff), - complete(graph), nothing, var_types, sys isa DiscreteSystem), + complete(graph), nothing, var_types, sys isa DiscreteSystem, lowest_shift), Any[]) if sys isa DiscreteSystem - ts = shift_discrete_system(ts) + ts = shift_discrete_system(ts, lowest_shift) end return ts end @@ -456,17 +462,27 @@ function lower_order_var(dervar, t) diffvar end -function shift_discrete_system(ts::TearingState) +""" + Shift variable x by the largest shift s such that x(k-s) appears in the system of equations. + The lowest-shift term will have. +""" +function shift_discrete_system(ts::TearingState, lowest_shift) @unpack fullvars, sys = ts + return ts discvars = OrderedSet() eqs = equations(sys) + for eq in eqs vars!(discvars, eq; op = Union{Sample, Hold}) end iv = get_iv(sys) - discmap = Dict(k => StructuralTransformations.simplify_shifts(Shift(iv, 1)(k)) + discmap = Dict(k => StructuralTransformations.simplify_shifts(Shift(iv, -get(lowest_shift, k, 0))(k)) for k in discvars - if any(isequal(k), fullvars) && !isa(operation(k), Union{Sample, Hold})) + if any(isequal(k), fullvars) && !isa(operation(k), Union{Sample, Hold})) + + discmap = Dict(k => StructuralTransformations.simplify_shifts(Shift(iv, 1)(k)) for k in discvars + if any(isequal(k), fullvars) && !isa(operation(k), Union{Sample, Hold})) + for i in eachindex(fullvars) fullvars[i] = StructuralTransformations.simplify_shifts(fast_substitute( fullvars[i], discmap; operator = Union{Sample, Hold})) From fe372865a7dd79d1e576f19d50677b1977e70fd1 Mon Sep 17 00:00:00 2001 From: vyudu Date: Mon, 3 Feb 2025 14:21:03 -0500 Subject: [PATCH 0759/2176] solving equations --- .../symbolics_tearing.jl | 64 ++++++++++--------- 1 file changed, 34 insertions(+), 30 deletions(-) diff --git a/src/structural_transformation/symbolics_tearing.jl b/src/structural_transformation/symbolics_tearing.jl index 8c802d9765..599a75f4f7 100644 --- a/src/structural_transformation/symbolics_tearing.jl +++ b/src/structural_transformation/symbolics_tearing.jl @@ -237,6 +237,18 @@ function check_diff_graph(var_to_diff, fullvars) end =# +function state_selection() + +end + +function create_new_deriv_variables() + +end + +function solve_solvable_equations() + +end + function tearing_reassemble(state::TearingState, var_eq_matching, full_var_eq_matching = nothing; simplify = false, mm = nothing, cse_hack = true, array_hack = true) @unpack fullvars, sys, structure = state @@ -323,6 +335,11 @@ function tearing_reassemble(state::TearingState, var_eq_matching, is_solvable = let solvable_graph = solvable_graph (eq, iv) -> eq isa Int && iv isa Int && BipartiteEdge(eq, iv) in solvable_graph end + idx_to_lowest_shift = Dict{Int, Int}(var => 0 for var in 1:length(fullvars)) + for (i,var) in enumerate(fullvars) + key = operation(var) isa Shift ? only(arguments(var)) : var + idx_to_lowest_shift[i] = get(lowest_shift, key, 0) + end # if var is like D(x) isdervar = let diff_to_var = diff_to_var @@ -335,27 +352,11 @@ function tearing_reassemble(state::TearingState, var_eq_matching, order += 1 dv = dv′ end - println("Order") - @show fullvars[dv] - is_only_discrete(state.structure) && begin - var = fullvars[dv] - key = operation(var) isa Shift ? only(arguments(var)) : var - order = -get(lowest_shift, key, 0) - order - end + is_only_discrete(state.structure) && (order = -idx_to_lowest_shift[dv] - order - 1) order, dv end end - lower_name = is_only_discrete(state.structure) ? lower_varname_withshift : lower_varname_with_unit - # is_only_discrete(state.structure) && for v in 1:length(fullvars) - # var = fullvars[v] - # op = operation(var) - # if op isa Shift - # x = only(arguments(var)) - # lowest_shift_idxs[v] - # op.steps == lowest_shift[x] && (fullvars[v] = lower_varname_withshift(var, iv, -op.steps)) - # end - # end #retear = BitSet() # There are three cases where we want to generate new variables to convert @@ -408,9 +409,6 @@ function tearing_reassemble(state::TearingState, var_eq_matching, Dict(reverse(en) for en in enumerate(mm.nzrows)) for v in 1:length(var_to_diff) - println() - @show fullvars - @show diff_to_var is_highest_discrete = begin var = fullvars[v] op = operation(var) @@ -425,7 +423,6 @@ function tearing_reassemble(state::TearingState, var_eq_matching, end end dv = is_highest_discrete ? idx : var_to_diff[v] - @show (v, fullvars[v], dv) dv isa Int || continue solved = var_eq_matching[dv] isa Int @@ -445,19 +442,17 @@ function tearing_reassemble(state::TearingState, var_eq_matching, diff_to_var[v_t] === nothing) @assert dv in rvs dummy_eq = eq - @show "FOUND DUMMY EQ" @goto FOUND_DUMMY_EQ end end dx = fullvars[dv] # add `x_t` - @show order, lv = var_order(dv) + order, lv = var_order(dv) x_t = lower_name(fullvars[lv], iv, order) push!(fullvars, simplify_shifts(x_t)) v_t = length(fullvars) v_t_idx = add_vertex!(var_to_diff) add_vertex!(graph, DST) - @show x_t, dx # TODO: do we care about solvable_graph? We don't use them after # `dummy_derivative_graph`. add_vertex!(solvable_graph, DST) @@ -476,16 +471,12 @@ function tearing_reassemble(state::TearingState, var_eq_matching, add_edge!(solvable_graph, dummy_eq, dv) @assert nsrcs(graph) == nsrcs(solvable_graph) == dummy_eq @label FOUND_DUMMY_EQ - @show is_highest_discrete - @show diff_to_var - @show v_t, dv # If var = x with no shift, then - is_highest_discrete && (lowest_shift[x_t] = lowest_shift[fullvars[v]]) + is_only_discrete(state.structure) && (idx_to_lowest_shift[v_t] = idx_to_lowest_shift[dv]) var_to_diff[v_t] = var_to_diff[dv] var_eq_matching[dv] = unassigned eq_var_matching[dummy_eq] = dv end - @show neweqs # Will reorder equations and unknowns to be: # [diffeqs; ...] @@ -501,9 +492,16 @@ function tearing_reassemble(state::TearingState, var_eq_matching, subeqs = Equation[] solved_equations = Int[] solved_variables = Int[] + # Solve solvable equations + println() + println("SOLVING SOLVABLE EQUATIONS.") + @show eq_var_matching toporder = topological_sort(DiCMOBiGraph{false}(graph, var_eq_matching)) eqs = Iterators.reverse(toporder) + @show eqs + @show neweqs + @show fullvars total_sub = Dict() idep = iv for ieq in eqs @@ -516,12 +514,18 @@ function tearing_reassemble(state::TearingState, var_eq_matching, isnothing(D) && error("Differential found in a non-differential system. Likely this is a bug in the construction of an initialization system. Please report this issue with a reproducible example. Offending equation: $(equations(sys)[ieq])") order, lv = var_order(iv) - dx = D(simplify_shifts(lower_varname_withshift( + @show fullvars[iv] + @show (order, lv) + dx = D(simplify_shifts(lower_name( fullvars[lv], idep, order - 1))) + @show dx + @show neweqs[ieq] eq = dx ~ simplify_shifts(Symbolics.fixpoint_sub( Symbolics.symbolic_linear_solve(neweqs[ieq], fullvars[iv]), total_sub; operator = ModelingToolkit.Shift)) + @show total_sub + @show eq for e in 𝑑neighbors(graph, iv) e == ieq && continue for v in 𝑠neighbors(graph, e) From 13a242c35ed4ec7c19143ccde26f3a8e6edddf3f Mon Sep 17 00:00:00 2001 From: vyudu Date: Mon, 3 Feb 2025 15:34:05 -0500 Subject: [PATCH 0760/2176] update to use updated codegen --- src/systems/diffeqs/abstractodesystem.jl | 33 ++++++++---------------- test/bvproblem.jl | 11 ++++---- 2 files changed, 17 insertions(+), 27 deletions(-) diff --git a/src/systems/diffeqs/abstractodesystem.jl b/src/systems/diffeqs/abstractodesystem.jl index f017cb902f..95e966a461 100644 --- a/src/systems/diffeqs/abstractodesystem.jl +++ b/src/systems/diffeqs/abstractodesystem.jl @@ -881,18 +881,23 @@ function SciMLBase.BVProblem{iip, specialize}(sys::AbstractODESystem, u0map = [] stidxmap = Dict([v => i for (i, v) in enumerate(sts)]) u0_idxs = has_alg_eqs(sys) ? collect(1:length(sts)) : [stidxmap[k] for (k,v) in u0map] - bc = generate_function_bc(sys, u0, u0_idxs, tspan, iip) + fns = generate_function_bc(sys, u0, u0_idxs, tspan) + bc_oop, bc_iip = eval_or_rgf.(fns; eval_expression, eval_module) + # bc(sol, p, t) = bc_oop(sol, p, t) + bc(resid, u, p, t) = bc_iip(resid, u, p, t) + return BVProblem{iip}(f, bc, u0, tspan, p; kwargs...) end get_callback(prob::BVProblem) = error("BVP solvers do not support callbacks.") """ - generate_function_bc(sys::ODESystem, u0, u0_idxs, tspan, iip) + generate_function_bc(sys::ODESystem, u0, u0_idxs, tspan) Given an ODESystem with constraints, generate the boundary condition function to pass to boundary value problem solvers. + Expression uses the constraints and the provided initial conditions. """ -function generate_function_bc(sys::ODESystem, u0, u0_idxs, tspan, iip) +function generate_function_bc(sys::ODESystem, u0, u0_idxs, tspan; kwargs...) iv = get_iv(sys) sts = get_unknowns(sys) ps = get_ps(sys) @@ -915,19 +920,6 @@ function generate_function_bc(sys::ODESystem, u0, u0_idxs, tspan, iip) cons = map(c -> Symbolics.substitute(c, Dict(x(t) => sol(t)[idx])), cons) end - - for var in parameters(conssys) - if iscall(var) - x = operation(var) - t = only(arguments(var)) - idx = pidxmap[x] - - cons = map(c -> Symbolics.substitute(c, Dict(x(t) => p[idx])), cons) - else - idx = pidxmap[var] - cons = map(c -> Symbolics.substitute(c, Dict(var => p[idx])), cons) - end - end end init_conds = Any[] @@ -937,12 +929,9 @@ function generate_function_bc(sys::ODESystem, u0, u0_idxs, tspan, iip) end exprs = vcat(init_conds, cons) - bcs = Symbolics.build_function(exprs, sol, p, expression = Val{false}) - if iip - return (resid, u, p, t) -> bcs[2](resid, u, p) - else - return (u, p, t) -> bcs[1](u, p) - end + _p = reorder_parameters(sys, ps) + + build_function_wrapper(sys, exprs, sol, _p..., t; kwargs...) end """ diff --git a/test/bvproblem.jl b/test/bvproblem.jl index 069372aef7..cedd0eef8d 100644 --- a/test/bvproblem.jl +++ b/test/bvproblem.jl @@ -1,6 +1,7 @@ ### TODO: update when BoundaryValueDiffEqAscher is updated to use the normal boundary condition conventions -using BoundaryValueDiffEq, OrdinaryDiffEq, BoundaryValueDiffEqAscher +using OrdinaryDiffEqVerner +using BoundaryValueDiffEqMIRK, BoundaryValueDiffEqAscher using BenchmarkTools using ModelingToolkit using SciMLBase @@ -207,22 +208,22 @@ let u0map = [] tspan = (0.0, 1.0) - guesses = [x(t) => 4.0, y(t) => 2.] + guess = [x(t) => 4.0, y(t) => 2.0] constr = [x(.6) ~ 3.5, x(.3) ~ 7.] @mtkbuild lksys = ODESystem(eqs, t; constraints = constr) - bvp = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}(lksys, u0map, tspan; guesses) + bvp = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}(lksys, u0map, tspan; guesses = guess) test_solvers(solvers, bvp, u0map, constr; dt = 0.05) # Testing that more complicated constraints give correct solutions. constr = [y(.2) + x(.8) ~ 3., y(.3) ~ 2.] @mtkbuild lksys = ODESystem(eqs, t; constraints = constr) - bvp = SciMLBase.BVProblem{false, SciMLBase.FullSpecialize}(lksys, u0map, tspan; guesses) + bvp = SciMLBase.BVProblem{false, SciMLBase.FullSpecialize}(lksys, u0map, tspan; guesses = guess) test_solvers(solvers, bvp, u0map, constr; dt = 0.05) constr = [α * β - x(.6) ~ 0.0, y(.2) ~ 3.] @mtkbuild lksys = ODESystem(eqs, t; constraints = constr) - bvp = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}(lksys, u0map, tspan; guesses) + bvp = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}(lksys, u0map, tspan; guesses = guess) test_solvers(solvers, bvp, u0map, constr) end From 2fcb9c930c0e4d607d509f1f13a01cef11df6b12 Mon Sep 17 00:00:00 2001 From: vyudu Date: Mon, 3 Feb 2025 15:34:37 -0500 Subject: [PATCH 0761/2176] up --- src/systems/diffeqs/abstractodesystem.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/systems/diffeqs/abstractodesystem.jl b/src/systems/diffeqs/abstractodesystem.jl index 95e966a461..056ecd80e4 100644 --- a/src/systems/diffeqs/abstractodesystem.jl +++ b/src/systems/diffeqs/abstractodesystem.jl @@ -883,7 +883,7 @@ function SciMLBase.BVProblem{iip, specialize}(sys::AbstractODESystem, u0map = [] fns = generate_function_bc(sys, u0, u0_idxs, tspan) bc_oop, bc_iip = eval_or_rgf.(fns; eval_expression, eval_module) - # bc(sol, p, t) = bc_oop(sol, p, t) + bc(sol, p, t) = bc_oop(sol, p, t) bc(resid, u, p, t) = bc_iip(resid, u, p, t) return BVProblem{iip}(f, bc, u0, tspan, p; kwargs...) From 25b56d77859cb4afd8c33bf2c48771e81591859e Mon Sep 17 00:00:00 2001 From: vyudu Date: Mon, 3 Feb 2025 22:03:33 -0500 Subject: [PATCH 0762/2176] working codegen --- src/systems/diffeqs/abstractodesystem.jl | 8 ++++---- src/systems/diffeqs/odesystem.jl | 2 +- src/systems/optimization/constraints_system.jl | 2 +- test/odesystem.jl | 12 ++++++++++++ 4 files changed, 18 insertions(+), 6 deletions(-) diff --git a/src/systems/diffeqs/abstractodesystem.jl b/src/systems/diffeqs/abstractodesystem.jl index 056ecd80e4..8d8dd80d47 100644 --- a/src/systems/diffeqs/abstractodesystem.jl +++ b/src/systems/diffeqs/abstractodesystem.jl @@ -899,14 +899,14 @@ get_callback(prob::BVProblem) = error("BVP solvers do not support callbacks.") """ function generate_function_bc(sys::ODESystem, u0, u0_idxs, tspan; kwargs...) iv = get_iv(sys) - sts = get_unknowns(sys) - ps = get_ps(sys) + sts = unknowns(sys) + ps = parameters(sys) np = length(ps) ns = length(sts) stidxmap = Dict([v => i for (i, v) in enumerate(sts)]) pidxmap = Dict([v => i for (i, v) in enumerate(ps)]) - @variables sol(..)[1:ns] p[1:np] + @variables sol(..)[1:ns] conssys = get_constraintsystem(sys) cons = Any[] @@ -931,7 +931,7 @@ function generate_function_bc(sys::ODESystem, u0, u0_idxs, tspan; kwargs...) exprs = vcat(init_conds, cons) _p = reorder_parameters(sys, ps) - build_function_wrapper(sys, exprs, sol, _p..., t; kwargs...) + build_function_wrapper(sys, exprs, sol, _p..., t; output_type = Array, kwargs...) end """ diff --git a/src/systems/diffeqs/odesystem.jl b/src/systems/diffeqs/odesystem.jl index c38f20c235..44cc7df46b 100644 --- a/src/systems/diffeqs/odesystem.jl +++ b/src/systems/diffeqs/odesystem.jl @@ -687,7 +687,6 @@ function process_constraint_system(constraints::Vector{Equation}, sts, ps, iv; c constraintsts = OrderedSet() constraintps = OrderedSet() - # Hack? to extract parameters from callable variables in constraints. for cons in constraints collect_vars!(constraintsts, constraintps, cons, iv) end @@ -712,5 +711,6 @@ function process_constraint_system(constraints::Vector{Equation}, sts, ps, iv; c end end + @show constraints ConstraintsSystem(constraints, collect(constraintsts), collect(constraintps); name = consname) end diff --git a/src/systems/optimization/constraints_system.jl b/src/systems/optimization/constraints_system.jl index 275079297a..ecdbafa044 100644 --- a/src/systems/optimization/constraints_system.jl +++ b/src/systems/optimization/constraints_system.jl @@ -123,7 +123,7 @@ function ConstraintsSystem(constraints, unknowns, ps; name === nothing && throw(ArgumentError("The `name` keyword must be provided. Please consider using the `@named` macro")) - cstr = value.(Symbolics.canonical_form.(scalarize(constraints))) + cstr = value.(Symbolics.canonical_form.(vcat(scalarize(constraints)...))) unknowns′ = value.(scalarize(unknowns)) ps′ = value.(ps) diff --git a/test/odesystem.jl b/test/odesystem.jl index 98ad6dd87a..97073457b3 100644 --- a/test/odesystem.jl +++ b/test/odesystem.jl @@ -1670,4 +1670,16 @@ end @test_throws ArgumentError @mtkbuild sys = ODESystem(eqs, t; constraints = cons) cons = [x(t) * v ~ 3] @test_throws ArgumentError @mtkbuild sys = ODESystem(eqs, t; constraints = cons) # Need time argument. + + # Test array variables + @variables x(..)[1:5] + mat = [1 2 0 3 2 + 0 0 3 2 0 + 0 1 3 0 4 + 2 0 0 2 1 + 0 0 2 0 5] + eqs = D(x(t)) ~ mat * x(t) + cons = [x(3) ~ [2,3,3,5,4]] + @mtkbuild ode = ODESystem(D(x(t)) ~ mat * x(t), t; constraints = cons) + @test length(constraints(ModelingToolkit.get_constraintsystem(ode))) == 5 end From dd71d2349f0d8eca81b04fc212209045bfb18018 Mon Sep 17 00:00:00 2001 From: vyudu Date: Mon, 3 Feb 2025 22:10:22 -0500 Subject: [PATCH 0763/2176] add docs --- docs/src/systems/DiscreteSystem.md | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 docs/src/systems/DiscreteSystem.md diff --git a/docs/src/systems/DiscreteSystem.md b/docs/src/systems/DiscreteSystem.md new file mode 100644 index 0000000000..b6a8061e50 --- /dev/null +++ b/docs/src/systems/DiscreteSystem.md @@ -0,0 +1,28 @@ +# DiscreteSystem + +## System Constructors + +```@docs +DiscreteSystem +``` + +## Composition and Accessor Functions + + - `get_eqs(sys)` or `equations(sys)`: The equations that define the discrete system. + - `get_unknowns(sys)` or `unknowns(sys)`: The set of unknowns in the discrete system. + - `get_ps(sys)` or `parameters(sys)`: The parameters of the discrete system. + - `get_iv(sys)`: The independent variable of the discrete system. + - `discrete_events(sys)`: The set of discrete events in the discrete system. + +## Transformations + +```@docs; canonical=false +structural_simplify +``` + +## Problem Constructors + +```@docs; canonical=false +DiscreteProblem(sys::DiscreteSystem, u0map, tspan) +DiscreteFunction(sys::DiscreteSystem, u0map, tspan) +``` From 4475a5a6ca7b61a897f3c90041895ed23b3df5d4 Mon Sep 17 00:00:00 2001 From: vyudu Date: Mon, 3 Feb 2025 22:37:56 -0500 Subject: [PATCH 0764/2176] up --- .../symbolics_tearing.jl | 19 ++++--------------- 1 file changed, 4 insertions(+), 15 deletions(-) diff --git a/src/structural_transformation/symbolics_tearing.jl b/src/structural_transformation/symbolics_tearing.jl index 599a75f4f7..059c7ca9e4 100644 --- a/src/structural_transformation/symbolics_tearing.jl +++ b/src/structural_transformation/symbolics_tearing.jl @@ -237,18 +237,6 @@ function check_diff_graph(var_to_diff, fullvars) end =# -function state_selection() - -end - -function create_new_deriv_variables() - -end - -function solve_solvable_equations() - -end - function tearing_reassemble(state::TearingState, var_eq_matching, full_var_eq_matching = nothing; simplify = false, mm = nothing, cse_hack = true, array_hack = true) @unpack fullvars, sys, structure = state @@ -323,7 +311,7 @@ function tearing_reassemble(state::TearingState, var_eq_matching, diff_to_var[dv] = nothing end end - @show neweqs + @show var_eq_matching println("Post state selection.") @@ -337,7 +325,7 @@ function tearing_reassemble(state::TearingState, var_eq_matching, end idx_to_lowest_shift = Dict{Int, Int}(var => 0 for var in 1:length(fullvars)) for (i,var) in enumerate(fullvars) - key = operation(var) isa Shift ? only(arguments(var)) : var + key = (operation(var) isa Shift) ? only(arguments(var)) : var idx_to_lowest_shift[i] = get(lowest_shift, key, 0) end @@ -345,6 +333,8 @@ function tearing_reassemble(state::TearingState, var_eq_matching, isdervar = let diff_to_var = diff_to_var var -> diff_to_var[var] !== nothing end + # For discrete variables, we want the substitution to turn + # Shift(t, k)(x(t)) => x_t-k(t) var_order = let diff_to_var = diff_to_var dv -> begin order = 0 @@ -358,7 +348,6 @@ function tearing_reassemble(state::TearingState, var_eq_matching, end lower_name = is_only_discrete(state.structure) ? lower_varname_withshift : lower_varname_with_unit - #retear = BitSet() # There are three cases where we want to generate new variables to convert # the system into first order (semi-implicit) ODEs. # From c35b7974f2d96584c96cfbffa2de87a390d92d8d Mon Sep 17 00:00:00 2001 From: vyudu Date: Mon, 3 Feb 2025 22:43:39 -0500 Subject: [PATCH 0765/2176] revert to OrdinaryDiffEqDefault --- test/bvproblem.jl | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/bvproblem.jl b/test/bvproblem.jl index cedd0eef8d..311a17e172 100644 --- a/test/bvproblem.jl +++ b/test/bvproblem.jl @@ -1,6 +1,6 @@ ### TODO: update when BoundaryValueDiffEqAscher is updated to use the normal boundary condition conventions -using OrdinaryDiffEqVerner +using OrdinaryDiffEqDefault using BoundaryValueDiffEqMIRK, BoundaryValueDiffEqAscher using BenchmarkTools using ModelingToolkit @@ -24,7 +24,7 @@ let @mtkbuild lotkavolterra = ODESystem(eqs, t) op = ODEProblem(lotkavolterra, u0map, tspan, parammap) - osol = solve(op, Vern9()) + osol = solve(op) bvp = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}( lotkavolterra, u0map, tspan, parammap; eval_expression = true) @@ -61,7 +61,7 @@ let tspan = (0.0, 6.0) op = ODEProblem(pend, u0map, tspan, parammap) - osol = solve(op, Vern9()) + osol = solve(op) bvp = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}(pend, u0map, tspan, parammap) for solver in solvers From a964dd7e1660f599afdbf0e9b2c9ddbfa717ea83 Mon Sep 17 00:00:00 2001 From: vyudu Date: Mon, 3 Feb 2025 23:01:48 -0500 Subject: [PATCH 0766/2176] up --- src/structural_transformation/symbolics_tearing.jl | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/structural_transformation/symbolics_tearing.jl b/src/structural_transformation/symbolics_tearing.jl index 059c7ca9e4..a80bebd436 100644 --- a/src/structural_transformation/symbolics_tearing.jl +++ b/src/structural_transformation/symbolics_tearing.jl @@ -237,6 +237,10 @@ function check_diff_graph(var_to_diff, fullvars) end =# +function substitute_lower_order!(state::TearingState) + +end + function tearing_reassemble(state::TearingState, var_eq_matching, full_var_eq_matching = nothing; simplify = false, mm = nothing, cse_hack = true, array_hack = true) @unpack fullvars, sys, structure = state From 26a545dbaaf4ced4b59ae45cc02377523dec5f10 Mon Sep 17 00:00:00 2001 From: vyudu Date: Mon, 3 Feb 2025 23:07:07 -0500 Subject: [PATCH 0767/2176] up --- src/structural_transformation/symbolics_tearing.jl | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/structural_transformation/symbolics_tearing.jl b/src/structural_transformation/symbolics_tearing.jl index a80bebd436..e612369498 100644 --- a/src/structural_transformation/symbolics_tearing.jl +++ b/src/structural_transformation/symbolics_tearing.jl @@ -241,6 +241,7 @@ function substitute_lower_order!(state::TearingState) end +import ModelingToolkit: Shift function tearing_reassemble(state::TearingState, var_eq_matching, full_var_eq_matching = nothing; simplify = false, mm = nothing, cse_hack = true, array_hack = true) @unpack fullvars, sys, structure = state @@ -255,6 +256,8 @@ function tearing_reassemble(state::TearingState, var_eq_matching, end neweqs = collect(equations(state)) + lower_name = is_only_discrete(state.structure) ? lower_varname_withshift : lower_varname_with_unit + # Terminology and Definition: # # A general DAE is in the form of `F(u'(t), u(t), p, t) == 0`. We can @@ -350,7 +353,6 @@ function tearing_reassemble(state::TearingState, var_eq_matching, order, dv end end - lower_name = is_only_discrete(state.structure) ? lower_varname_withshift : lower_varname_with_unit # There are three cases where we want to generate new variables to convert # the system into first order (semi-implicit) ODEs. @@ -464,7 +466,6 @@ function tearing_reassemble(state::TearingState, var_eq_matching, add_edge!(solvable_graph, dummy_eq, dv) @assert nsrcs(graph) == nsrcs(solvable_graph) == dummy_eq @label FOUND_DUMMY_EQ - # If var = x with no shift, then is_only_discrete(state.structure) && (idx_to_lowest_shift[v_t] = idx_to_lowest_shift[dv]) var_to_diff[v_t] = var_to_diff[dv] var_eq_matching[dv] = unassigned From 25e84dbf39a92c569dd3a8f4027c9b33dd99f34e Mon Sep 17 00:00:00 2001 From: vyudu Date: Mon, 3 Feb 2025 23:08:12 -0500 Subject: [PATCH 0768/2176] use MIRK --- Project.toml | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/Project.toml b/Project.toml index c3646021ff..41c5ad0730 100644 --- a/Project.toml +++ b/Project.toml @@ -85,6 +85,7 @@ BifurcationKit = "0.4" BlockArrays = "1.1" BoundaryValueDiffEq = "5.12.0" BoundaryValueDiffEqAscher = "1.1.0" +BoundaryValueDiffEqMIRK = "1.4.0" ChainRulesCore = "1" Combinatorics = "1" CommonSolve = "0.2.4" @@ -106,11 +107,11 @@ DynamicQuantities = "^0.11.2, 0.12, 0.13, 1" EnumX = "1.0.4" ExprTools = "0.1.10" Expronicon = "0.8" +FMI = "0.14" FindFirstFunctions = "1" ForwardDiff = "0.10.3" FunctionWrappers = "1.1" FunctionWrappersWrappers = "0.1" -FMI = "0.14" Graphs = "1.5.2" HomotopyContinuation = "2.11" InfiniteOpt = "0.5" @@ -157,7 +158,7 @@ julia = "1.9" [extras] AmplNLWriter = "7c4d4715-977e-5154-bfe0-e096adeac482" BenchmarkTools = "6e4b80f9-dd63-53aa-95a3-0cdb28fa8baf" -BoundaryValueDiffEq = "764a87c0-6b3e-53db-9096-fe964310641d" +BoundaryValueDiffEqMIRK = "1a22d4ce-7765-49ea-b6f2-13c8438986a6" BoundaryValueDiffEqAscher = "7227322d-7511-4e07-9247-ad6ff830280e" ControlSystemsBase = "aaaaaaaa-a6ca-5380-bf3e-84a91bcd477e" DataInterpolations = "82cc6244-b520-54b8-b5a6-8a565e85f1d0" @@ -190,4 +191,4 @@ Sundials = "c3572dad-4567-51f8-b174-8c6c989267f4" Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" [targets] -test = ["AmplNLWriter", "BenchmarkTools", "BoundaryValueDiffEq", "BoundaryValueDiffEqAscher", "ControlSystemsBase", "DataInterpolations", "DelayDiffEq", "NonlinearSolve", "ForwardDiff", "Ipopt", "Ipopt_jll", "ModelingToolkitStandardLibrary", "Optimization", "OptimizationOptimJL", "OptimizationMOI", "OrdinaryDiffEq", "OrdinaryDiffEqCore", "OrdinaryDiffEqDefault", "REPL", "Random", "ReferenceTests", "SafeTestsets", "StableRNGs", "Statistics", "SteadyStateDiffEq", "Test", "StochasticDiffEq", "Sundials", "StochasticDelayDiffEq", "Pkg", "JET", "OrdinaryDiffEqNonlinearSolve"] +test = ["AmplNLWriter", "BenchmarkTools", "BoundaryValueDiffEqMIRK", "BoundaryValueDiffEqAscher", "ControlSystemsBase", "DataInterpolations", "DelayDiffEq", "NonlinearSolve", "ForwardDiff", "Ipopt", "Ipopt_jll", "ModelingToolkitStandardLibrary", "Optimization", "OptimizationOptimJL", "OptimizationMOI", "OrdinaryDiffEq", "OrdinaryDiffEqCore", "OrdinaryDiffEqDefault", "REPL", "Random", "ReferenceTests", "SafeTestsets", "StableRNGs", "Statistics", "SteadyStateDiffEq", "Test", "StochasticDiffEq", "Sundials", "StochasticDelayDiffEq", "Pkg", "JET", "OrdinaryDiffEqNonlinearSolve"] From e6a6932d192ef72bc46be8ed6124965372e87f26 Mon Sep 17 00:00:00 2001 From: vyudu Date: Mon, 3 Feb 2025 23:09:04 -0500 Subject: [PATCH 0769/2176] up --- Project.toml | 1 - 1 file changed, 1 deletion(-) diff --git a/Project.toml b/Project.toml index 41c5ad0730..cd2c2c4380 100644 --- a/Project.toml +++ b/Project.toml @@ -83,7 +83,6 @@ AbstractTrees = "0.3, 0.4" ArrayInterface = "6, 7" BifurcationKit = "0.4" BlockArrays = "1.1" -BoundaryValueDiffEq = "5.12.0" BoundaryValueDiffEqAscher = "1.1.0" BoundaryValueDiffEqMIRK = "1.4.0" ChainRulesCore = "1" From 5e5c24c071f00a8d50b6e7441a141ca9cbeb1681 Mon Sep 17 00:00:00 2001 From: vyudu Date: Mon, 3 Feb 2025 23:30:44 -0500 Subject: [PATCH 0770/2176] revert to OrdinaryDiffEq --- test/bvproblem.jl | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/test/bvproblem.jl b/test/bvproblem.jl index 311a17e172..1d7d4b3793 100644 --- a/test/bvproblem.jl +++ b/test/bvproblem.jl @@ -1,6 +1,6 @@ ### TODO: update when BoundaryValueDiffEqAscher is updated to use the normal boundary condition conventions -using OrdinaryDiffEqDefault +using OrdinaryDiffEq using BoundaryValueDiffEqMIRK, BoundaryValueDiffEqAscher using BenchmarkTools using ModelingToolkit @@ -24,10 +24,9 @@ let @mtkbuild lotkavolterra = ODESystem(eqs, t) op = ODEProblem(lotkavolterra, u0map, tspan, parammap) - osol = solve(op) + osol = solve(op, Vern9()) - bvp = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}( - lotkavolterra, u0map, tspan, parammap; eval_expression = true) + bvp = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}(lotkavolterra, u0map, tspan, parammap) for solver in solvers sol = solve(bvp, solver(), dt = 0.01) @@ -36,8 +35,7 @@ let end # Test out of place - bvp2 = SciMLBase.BVProblem{false, SciMLBase.AutoSpecialize}( - lotkavolterra, u0map, tspan, parammap; eval_expression = true) + bvp2 = SciMLBase.BVProblem{false, SciMLBase.AutoSpecialize}(lotkavolterra, u0map, tspan, parammap) for solver in solvers sol = solve(bvp2, solver(), dt = 0.01) From 5338d4f6b60704ebcc1df0c425a0a721f1a46138 Mon Sep 17 00:00:00 2001 From: vyudu Date: Tue, 4 Feb 2025 00:08:54 -0500 Subject: [PATCH 0771/2176] tests passing --- test/bvproblem.jl | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/test/bvproblem.jl b/test/bvproblem.jl index 1d7d4b3793..b4032e2927 100644 --- a/test/bvproblem.jl +++ b/test/bvproblem.jl @@ -59,7 +59,7 @@ let tspan = (0.0, 6.0) op = ODEProblem(pend, u0map, tspan, parammap) - osol = solve(op) + osol = solve(op, Vern9()) bvp = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}(pend, u0map, tspan, parammap) for solver in solvers @@ -111,8 +111,8 @@ let end u0 = [1., 2.]; p = [1.5, 1., 1., 3.] - genbc_iip = ModelingToolkit.generate_function_bc(lksys, u0, [1, 2], tspan, true) - genbc_oop = ModelingToolkit.generate_function_bc(lksys, u0, [1, 2], tspan, false) + fns = ModelingToolkit.generate_function_bc(lksys, u0, [1, 2], tspan) + genbc_oop, genbc_iip = ModelingToolkit.eval_or_rgf.(fns) bvpi1 = SciMLBase.BVProblem(lotkavolterra!, bc!, [1.,2.], tspan, p) bvpi2 = SciMLBase.BVProblem(lotkavolterra!, genbc_iip, [1.,2.], tspan, p) @@ -141,8 +141,8 @@ let end u0 = [1, 1.] - genbc_iip = ModelingToolkit.generate_function_bc(lksys, u0, [1], tspan, true) - genbc_oop = ModelingToolkit.generate_function_bc(lksys, u0, [1], tspan, false) + fns = ModelingToolkit.generate_function_bc(lksys, u0, [1], tspan) + genbc_oop, genbc_iip = ModelingToolkit.eval_or_rgf.(fns) bvpi1 = SciMLBase.BVProblem(lotkavolterra!, bc!, u0, tspan, p) bvpi2 = SciMLBase.BVProblem(lotkavolterra!, genbc_iip, u0, tspan, p) From 62766354d7fac2166d76e3e3e022f35129b3f7fd Mon Sep 17 00:00:00 2001 From: Christopher Rackauckas Date: Mon, 3 Feb 2025 21:14:59 -0800 Subject: [PATCH 0772/2176] Update docs/src/basics/Variable_metadata.md --- docs/src/basics/Variable_metadata.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/src/basics/Variable_metadata.md b/docs/src/basics/Variable_metadata.md index d36c91697e..bcfe90bab4 100644 --- a/docs/src/basics/Variable_metadata.md +++ b/docs/src/basics/Variable_metadata.md @@ -191,7 +191,7 @@ isirreducible(important_value) When a model is structurally simplified, the algorithm will try to ensure that the variables with higher state priority become states of the system. A variable's state priority is a number set using the `state_priority` metadata. ```@example metadata -@variable important_dof [state_priority = 10] unimportant_dof [state_priority = 2] +@variable important_dof [state_priority = 10] unimportant_dof [state_priority = -2] state_priority(important_dof) ``` From 79b07ceb12d88b212cd3dfb8e851b317eb216e24 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Szymon=20B=C4=99czkowski?= Date: Tue, 4 Feb 2025 08:43:50 +0100 Subject: [PATCH 0773/2176] latexify equation --- docs/src/tutorials/initialization.md | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/docs/src/tutorials/initialization.md b/docs/src/tutorials/initialization.md index c01310eb06..26dbb58899 100644 --- a/docs/src/tutorials/initialization.md +++ b/docs/src/tutorials/initialization.md @@ -14,8 +14,10 @@ principles of initialization of DAE systems. Take a DAE written in semi-explicit form: ```math -x' = f(x,y,t)\\ -0 = g(x,y,t) +\begin{aligned} + x^\prime &= f(x,y,t) \\ + 0 &= g(x,y,t) +\end{aligned} ``` where ``x`` are the differential variables and ``y`` are the algebraic variables. From 1656277f45e7a12229d8ed0545a979cb8bddb273 Mon Sep 17 00:00:00 2001 From: vyudu Date: Tue, 4 Feb 2025 02:49:11 -0500 Subject: [PATCH 0774/2176] maadd comments --- .../symbolics_tearing.jl | 68 ++++++++++++++----- src/structural_transformation/utils.jl | 3 +- src/systems/systemstructure.jl | 3 - 3 files changed, 52 insertions(+), 22 deletions(-) diff --git a/src/structural_transformation/symbolics_tearing.jl b/src/structural_transformation/symbolics_tearing.jl index e612369498..cf6736b419 100644 --- a/src/structural_transformation/symbolics_tearing.jl +++ b/src/structural_transformation/symbolics_tearing.jl @@ -241,6 +241,32 @@ function substitute_lower_order!(state::TearingState) end +# Documenting the differences to structural simplification for discrete systems: +# In discrete systems the lowest-order term is x_k-i, instead of x(t). In order +# to substitute dummy variables for x_k-1, x_k-2, ... instead you need to reverse +# the order. So for discrete systems `var_order` is defined a little differently. +# +# The orders will also be off by one. The reason this is is that the dynamics of +# the system should be given in terms of Shift(t, 1)(x(t), x(t-1), ...). But +# having the observables be indexed by the next time step is not so nice. So we +# handle the shifts in the renaming, rather than explicitly. +# +# The substitution should look like the following: +# x(t) -> Shift(t, 1)(x(t)) +# x(k-1) -> x(t) +# x(k-2) -> x_{t-1}(t) +# x(k-3) -> x_{t-2}(t) +# and so on... +# +# In the implicit discrete case this shouldn't happen. The simplification should +# look like a NonlinearSystem. +# +# For discrete systems Shift(t, 2)(x(t)) is not equivalent to Shift(t, 1)(Shift(t,1)(x(t)) +# This is different from the continuous case where D(D(x)) can be substituted for +# by iteratively substituting x_t ~ D(x), then x_tt ~ D(x_t). For this reason the +# total_sub dict is updated at the time that the renamed variables are written, +# inside the loop where new variables are generated. + import ModelingToolkit: Shift function tearing_reassemble(state::TearingState, var_eq_matching, full_var_eq_matching = nothing; simplify = false, mm = nothing, cse_hack = true, array_hack = true) @@ -318,9 +344,6 @@ function tearing_reassemble(state::TearingState, var_eq_matching, diff_to_var[dv] = nothing end end - @show var_eq_matching - - println("Post state selection.") # `SelectedState` information is no longer needed past here. State selection # is done. All non-differentiated variables are algebraic variables, and all @@ -330,18 +353,19 @@ function tearing_reassemble(state::TearingState, var_eq_matching, is_solvable = let solvable_graph = solvable_graph (eq, iv) -> eq isa Int && iv isa Int && BipartiteEdge(eq, iv) in solvable_graph end - idx_to_lowest_shift = Dict{Int, Int}(var => 0 for var in 1:length(fullvars)) - for (i,var) in enumerate(fullvars) - key = (operation(var) isa Shift) ? only(arguments(var)) : var - idx_to_lowest_shift[i] = get(lowest_shift, key, 0) + + if is_only_discrete(state.structure) + idx_to_lowest_shift = Dict{Int, Int}(var => 0 for var in 1:length(fullvars)) + for (i,var) in enumerate(fullvars) + key = (operation(var) isa Shift) ? only(arguments(var)) : var + idx_to_lowest_shift[i] = get(lowest_shift, key, 0) + end end # if var is like D(x) isdervar = let diff_to_var = diff_to_var var -> diff_to_var[var] !== nothing end - # For discrete variables, we want the substitution to turn - # Shift(t, k)(x(t)) => x_t-k(t) var_order = let diff_to_var = diff_to_var dv -> begin order = 0 @@ -349,7 +373,7 @@ function tearing_reassemble(state::TearingState, var_eq_matching, order += 1 dv = dv′ end - is_only_discrete(state.structure) && (order = -idx_to_lowest_shift[dv] - order - 1) + is_only_discrete(state.structure) && (order = -idx_to_lowest_shift[dv] - order) order, dv end end @@ -403,6 +427,7 @@ function tearing_reassemble(state::TearingState, var_eq_matching, linear_eqs = mm === nothing ? Dict{Int, Int}() : Dict(reverse(en) for en in enumerate(mm.nzrows)) + total_sub = Dict() for v in 1:length(var_to_diff) is_highest_discrete = begin var = fullvars[v] @@ -442,8 +467,13 @@ function tearing_reassemble(state::TearingState, var_eq_matching, end dx = fullvars[dv] # add `x_t` - order, lv = var_order(dv) + println() + @show order, lv = var_order(dv) x_t = lower_name(fullvars[lv], iv, order) + @show fullvars[v] + @show fullvars[dv] + @show fullvars[lv] + @show dx, x_t push!(fullvars, simplify_shifts(x_t)) v_t = length(fullvars) v_t_idx = add_vertex!(var_to_diff) @@ -466,11 +496,16 @@ function tearing_reassemble(state::TearingState, var_eq_matching, add_edge!(solvable_graph, dummy_eq, dv) @assert nsrcs(graph) == nsrcs(solvable_graph) == dummy_eq @label FOUND_DUMMY_EQ - is_only_discrete(state.structure) && (idx_to_lowest_shift[v_t] = idx_to_lowest_shift[dv]) + is_only_discrete(state.structure) && begin + idx_to_lowest_shift[v_t] = idx_to_lowest_shift[dv] + operation(dx) isa Shift && (total_sub[dx] = x_t) + order == 1 && (total_sub[x_t] = fullvars[var_to_diff[dv]]) + end var_to_diff[v_t] = var_to_diff[dv] var_eq_matching[dv] = unassigned eq_var_matching[dummy_eq] = dv end + @show total_sub # Will reorder equations and unknowns to be: # [diffeqs; ...] @@ -490,15 +525,13 @@ function tearing_reassemble(state::TearingState, var_eq_matching, # Solve solvable equations println() println("SOLVING SOLVABLE EQUATIONS.") - @show eq_var_matching toporder = topological_sort(DiCMOBiGraph{false}(graph, var_eq_matching)) eqs = Iterators.reverse(toporder) - @show eqs - @show neweqs - @show fullvars - total_sub = Dict() idep = iv + @show eq_var_matching + for ieq in eqs + println() iv = eq_var_matching[ieq] if is_solvable(ieq, iv) # We don't solve differential equations, but we will need to try to @@ -513,7 +546,6 @@ function tearing_reassemble(state::TearingState, var_eq_matching, dx = D(simplify_shifts(lower_name( fullvars[lv], idep, order - 1))) @show dx - @show neweqs[ieq] eq = dx ~ simplify_shifts(Symbolics.fixpoint_sub( Symbolics.symbolic_linear_solve(neweqs[ieq], fullvars[iv]), diff --git a/src/structural_transformation/utils.jl b/src/structural_transformation/utils.jl index dd063c0b07..707a57097b 100644 --- a/src/structural_transformation/utils.jl +++ b/src/structural_transformation/utils.jl @@ -451,7 +451,8 @@ end function lower_varname_withshift(var, iv, order) order == 0 && return var - ds = "$iv-$order" + #order == -1 && return Shift(iv, 1)(var) + ds = "$iv-$(order-1)" d_separator = 'ˍ' if ModelingToolkit.isoperator(var, ModelingToolkit.Shift) diff --git a/src/systems/systemstructure.jl b/src/systems/systemstructure.jl index 44558ade6c..89fcebf549 100644 --- a/src/systems/systemstructure.jl +++ b/src/systems/systemstructure.jl @@ -476,9 +476,6 @@ function shift_discrete_system(ts::TearingState, lowest_shift) vars!(discvars, eq; op = Union{Sample, Hold}) end iv = get_iv(sys) - discmap = Dict(k => StructuralTransformations.simplify_shifts(Shift(iv, -get(lowest_shift, k, 0))(k)) - for k in discvars - if any(isequal(k), fullvars) && !isa(operation(k), Union{Sample, Hold})) discmap = Dict(k => StructuralTransformations.simplify_shifts(Shift(iv, 1)(k)) for k in discvars if any(isequal(k), fullvars) && !isa(operation(k), Union{Sample, Hold})) From 810d4fa26af98e0231da9323bb61053f02fd13ce Mon Sep 17 00:00:00 2001 From: vyudu Date: Tue, 4 Feb 2025 02:58:11 -0500 Subject: [PATCH 0775/2176] remove problematic tests, codegen assumes MTKParameters --- test/bvproblem.jl | 44 ++------------------------------------------ 1 file changed, 2 insertions(+), 42 deletions(-) diff --git a/test/bvproblem.jl b/test/bvproblem.jl index b4032e2927..edc85b4cbd 100644 --- a/test/bvproblem.jl +++ b/test/bvproblem.jl @@ -100,33 +100,6 @@ let function lotkavolterra(u, p, t) [p[1]*u[1] - p[2]*u[1]*u[2], -p[4]*u[2] + p[3]*u[1]*u[2]] end - # Compare the built bc function to the actual constructed one. - function bc!(resid, u, p, t) - resid[1] = u[1][1] - 1. - resid[2] = u[1][2] - 2. - nothing - end - function bc(u, p, t) - [u[1][1] - 1., u[1][2] - 2.] - end - - u0 = [1., 2.]; p = [1.5, 1., 1., 3.] - fns = ModelingToolkit.generate_function_bc(lksys, u0, [1, 2], tspan) - genbc_oop, genbc_iip = ModelingToolkit.eval_or_rgf.(fns) - - bvpi1 = SciMLBase.BVProblem(lotkavolterra!, bc!, [1.,2.], tspan, p) - bvpi2 = SciMLBase.BVProblem(lotkavolterra!, genbc_iip, [1.,2.], tspan, p) - - sol1 = solve(bvpi1, MIRK4(), dt = 0.05) - sol2 = solve(bvpi2, MIRK4(), dt = 0.05) - @test sol1 ≈ sol2 - - bvpo1 = BVProblem(lotkavolterra, bc, [1,2], tspan, p) - bvpo2 = BVProblem(lotkavolterra, genbc_oop, [1,2], tspan, p) - - sol1 = solve(bvpo1, MIRK4(), dt = 0.05) - sol2 = solve(bvpo2, MIRK4(), dt = 0.05) - @test sol1 ≈ sol2 # Test with a constraint. constr = [y(0.5) ~ 2.] @@ -140,29 +113,16 @@ let [u(0.0)[1] - 1., u(0.5)[2] - 2.] end - u0 = [1, 1.] - fns = ModelingToolkit.generate_function_bc(lksys, u0, [1], tspan) - genbc_oop, genbc_iip = ModelingToolkit.eval_or_rgf.(fns) - bvpi1 = SciMLBase.BVProblem(lotkavolterra!, bc!, u0, tspan, p) - bvpi2 = SciMLBase.BVProblem(lotkavolterra!, genbc_iip, u0, tspan, p) + bvpi1 = SciMLBase.BVProblem(lotkavolterra, bc, u0, tspan, p) bvpi3 = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}(lksys, [x(t) => 1.], tspan; guesses = [y(t) => 1.]) - bvpi4 = SciMLBase.BVProblem{true, SciMLBase.FullSpecialize}(lksys, [x(t) => 1.], tspan; guesses = [y(t) => 1.]) + bvpi4 = SciMLBase.BVProblem{false, SciMLBase.FullSpecialize}(lksys, [x(t) => 1.], tspan; guesses = [y(t) => 1.]) sol1 = @btime solve($bvpi1, MIRK4(), dt = 0.01) sol2 = @btime solve($bvpi2, MIRK4(), dt = 0.01) sol3 = @btime solve($bvpi3, MIRK4(), dt = 0.01) sol4 = @btime solve($bvpi4, MIRK4(), dt = 0.01) @test sol1 ≈ sol2 ≈ sol3 ≈ sol4 # don't get true equality here, not sure why - - bvpo1 = BVProblem(lotkavolterra, bc, u0, tspan, p) - bvpo2 = BVProblem(lotkavolterra, genbc_oop, u0, tspan, p) - bvpo3 = SciMLBase.BVProblem{false, SciMLBase.FullSpecialize}(lksys, [x(t) => 1.], tspan; guesses = [y(t) => 1.]) - - sol1 = @btime solve($bvpo1, MIRK4(), dt = 0.05) - sol2 = @btime solve($bvpo2, MIRK4(), dt = 0.05) - sol3 = @btime solve($bvpo3, MIRK4(), dt = 0.05) - @test sol1 ≈ sol2 ≈ sol3 end function test_solvers(solvers, prob, u0map, constraints, equations = []; dt = 0.05, atol = 1e-2) From 16ea18bbde45a0c50db93d0922375a5f54312fe9 Mon Sep 17 00:00:00 2001 From: vyudu Date: Tue, 4 Feb 2025 03:27:27 -0500 Subject: [PATCH 0776/2176] begin refactor --- .../symbolics_tearing.jl | 129 +++++++++--------- 1 file changed, 66 insertions(+), 63 deletions(-) diff --git a/src/structural_transformation/symbolics_tearing.jl b/src/structural_transformation/symbolics_tearing.jl index cf6736b419..6340087003 100644 --- a/src/structural_transformation/symbolics_tearing.jl +++ b/src/structural_transformation/symbolics_tearing.jl @@ -237,8 +237,49 @@ function check_diff_graph(var_to_diff, fullvars) end =# -function substitute_lower_order!(state::TearingState) - + +function substitute_dummy_derivatives!(fullvars, var_to_diff, diff_to_var, var_eq_matching, neweqs) + # Dummy derivatives may determine that some differential variables are + # algebraic variables in disguise. The derivative of such variables are + # called dummy derivatives. + + # Step 1: + # Replace derivatives of non-selected unknown variables by dummy derivatives + + dummy_sub = Dict() + for var in 1:length(fullvars) + dv = var_to_diff[var] + dv === nothing && continue + if var_eq_matching[var] !== SelectedState() + dd = fullvars[dv] + v_t = setio(diff2term_with_unit(unwrap(dd), unwrap(iv)), false, false) + for eq in 𝑑neighbors(graph, dv) + dummy_sub[dd] = v_t + neweqs[eq] = fast_substitute(neweqs[eq], dd => v_t) + end + fullvars[dv] = v_t + # If we have: + # x -> D(x) -> D(D(x)) + # We need to to transform it to: + # x x_t -> D(x_t) + # update the structural information + dx = dv + x_t = v_t + while (ddx = var_to_diff[dx]) !== nothing + dx_t = D(x_t) + for eq in 𝑑neighbors(graph, ddx) + neweqs[eq] = fast_substitute(neweqs[eq], fullvars[ddx] => dx_t) + end + fullvars[ddx] = dx_t + dx = ddx + x_t = dx_t + end + diff_to_var[dv] = nothing + end + end +end + +function generate_derivative_variables!() end # Documenting the differences to structural simplification for discrete systems: @@ -281,88 +322,50 @@ function tearing_reassemble(state::TearingState, var_eq_matching, end end - neweqs = collect(equations(state)) - lower_name = is_only_discrete(state.structure) ? lower_varname_withshift : lower_varname_with_unit - - # Terminology and Definition: - # - # A general DAE is in the form of `F(u'(t), u(t), p, t) == 0`. We can - # characterize variables in `u(t)` into two classes: differential variables - # (denoted `v(t)`) and algebraic variables (denoted `z(t)`). Differential - # variables are marked as `SelectedState` and they are differentiated in the - # DAE system, i.e. `v'(t)` are all the variables in `u'(t)` that actually - # appear in the system. Algebraic variables are variables that are not - # differential variables. - # - # Dummy derivatives may determine that some differential variables are - # algebraic variables in disguise. The derivative of such variables are - # called dummy derivatives. - - # Step 1: - # Replace derivatives of non-selected unknown variables by dummy derivatives - if ModelingToolkit.has_iv(state.sys) iv = get_iv(state.sys) if is_only_discrete(state.structure) D = Shift(iv, 1) + lower_name = lower_varname_withshift + idx_to_lowest_shift = Dict{Int, Int}(var => 0 for var in 1:length(fullvars)) + for (i,var) in enumerate(fullvars) + key = (operation(var) isa Shift) ? only(arguments(var)) : var + idx_to_lowest_shift[i] = get(lowest_shift, key, 0) + end else D = Differential(iv) + lower_name = lower_varname_with_unit end else iv = D = nothing + lower_name = lower_varname_with_unit end + diff_to_var = invview(var_to_diff) + neweqs = collect(equations(state)) - dummy_sub = Dict() - for var in 1:length(fullvars) - dv = var_to_diff[var] - dv === nothing && continue - if var_eq_matching[var] !== SelectedState() - dd = fullvars[dv] - v_t = setio(diff2term_with_unit(unwrap(dd), unwrap(iv)), false, false) - for eq in 𝑑neighbors(graph, dv) - dummy_sub[dd] = v_t - neweqs[eq] = fast_substitute(neweqs[eq], dd => v_t) - end - fullvars[dv] = v_t - # If we have: - # x -> D(x) -> D(D(x)) - # We need to to transform it to: - # x x_t -> D(x_t) - # update the structural information - dx = dv - x_t = v_t - while (ddx = var_to_diff[dx]) !== nothing - dx_t = D(x_t) - for eq in 𝑑neighbors(graph, ddx) - neweqs[eq] = fast_substitute(neweqs[eq], fullvars[ddx] => dx_t) - end - fullvars[ddx] = dx_t - dx = ddx - x_t = dx_t - end - diff_to_var[dv] = nothing - end - end + # Terminology and Definition: + # + # A general DAE is in the form of `F(u'(t), u(t), p, t) == 0`. We can + # characterize variables in `u(t)` into two classes: differential variables + # (denoted `v(t)`) and algebraic variables (denoted `z(t)`). Differential + # variables are marked as `SelectedState` and they are differentiated in the + # DAE system, i.e. `v'(t)` are all the variables in `u'(t)` that actually + # appear in the system. Algebraic variables are variables that are not + # differential variables. + substitute_dummy_derivatives!(fullvars, var_to_diff, diff_to_var, var_eq_matching, neweqs) + # `SelectedState` information is no longer needed past here. State selection # is done. All non-differentiated variables are algebraic variables, and all # variables that appear differentiated are differential variables. - ### extract partition information + ### Extract partition information is_solvable = let solvable_graph = solvable_graph (eq, iv) -> eq isa Int && iv isa Int && BipartiteEdge(eq, iv) in solvable_graph end - if is_only_discrete(state.structure) - idx_to_lowest_shift = Dict{Int, Int}(var => 0 for var in 1:length(fullvars)) - for (i,var) in enumerate(fullvars) - key = (operation(var) isa Shift) ? only(arguments(var)) : var - idx_to_lowest_shift[i] = get(lowest_shift, key, 0) - end - end - - # if var is like D(x) + # if var is like D(x) or Shift(t, 1)(x) isdervar = let diff_to_var = diff_to_var var -> diff_to_var[var] !== nothing end From 6740b8c89b6992e3ddfc1b004f74fe632ef2295d Mon Sep 17 00:00:00 2001 From: vyudu Date: Tue, 4 Feb 2025 03:31:53 -0500 Subject: [PATCH 0777/2176] test fix --- test/bvproblem.jl | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/test/bvproblem.jl b/test/bvproblem.jl index edc85b4cbd..f05be90281 100644 --- a/test/bvproblem.jl +++ b/test/bvproblem.jl @@ -113,8 +113,11 @@ let [u(0.0)[1] - 1., u(0.5)[2] - 2.] end + u0 = [1., 1.] + tspan = (0., 1.) + p = [1.5, 1., 1., 3.] bvpi1 = SciMLBase.BVProblem(lotkavolterra!, bc!, u0, tspan, p) - bvpi1 = SciMLBase.BVProblem(lotkavolterra, bc, u0, tspan, p) + bvpi2 = SciMLBase.BVProblem(lotkavolterra, bc, u0, tspan, p) bvpi3 = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}(lksys, [x(t) => 1.], tspan; guesses = [y(t) => 1.]) bvpi4 = SciMLBase.BVProblem{false, SciMLBase.FullSpecialize}(lksys, [x(t) => 1.], tspan; guesses = [y(t) => 1.]) From 2c1997b864c068caaf8af6195e26655a317ed2bb Mon Sep 17 00:00:00 2001 From: Benjamin Chung Date: Tue, 4 Feb 2025 02:47:00 -0800 Subject: [PATCH 0778/2176] Fix parameter array handling in ImperativeAffect --- src/systems/imperative_affect.jl | 7 +++- test/symbolic_events.jl | 57 ++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+), 1 deletion(-) diff --git a/src/systems/imperative_affect.jl b/src/systems/imperative_affect.jl index aee95fd051..4c9ff3d248 100644 --- a/src/systems/imperative_affect.jl +++ b/src/systems/imperative_affect.jl @@ -75,7 +75,12 @@ func(f::ImperativeAffect) = f.f context(a::ImperativeAffect) = a.ctx observed(a::ImperativeAffect) = a.obs observed_syms(a::ImperativeAffect) = a.obs_syms -discretes(a::ImperativeAffect) = filter(ModelingToolkit.isparameter, a.modified) +function discretes(a::ImperativeAffect) + Iterators.filter(ModelingToolkit.isparameter, + Iterators.flatten(Iterators.map( + x -> symbolic_type(x) == NotSymbolic() && x isa AbstractArray ? x : [x], + a.modified))) +end modified(a::ImperativeAffect) = a.modified modified_syms(a::ImperativeAffect) = a.mod_syms diff --git a/test/symbolic_events.jl b/test/symbolic_events.jl index ccf0a17a40..3fb66081a2 100644 --- a/test/symbolic_events.jl +++ b/test/symbolic_events.jl @@ -1377,3 +1377,60 @@ end prob = ODEProblem(decay, [], (0.0, 10.0), []) @test_nowarn solve(prob, Tsit5(), tstops = [1.0]) end + +@testset "Array parameter updates in ImperativeEffect" begin + function weird1(max_time; name) + params = @parameters begin + θ(t) = 0.0 + end + vars = @variables begin + x(t) = 0.0 + end + eqs = reduce(vcat, Symbolics.scalarize.([ + D(x) ~ 1.0 + ])) + reset = ModelingToolkit.ImperativeAffect( + modified = (; x, θ)) do m, o, _, i + @set! m.θ = 0.0 + @set! m.x = 0.0 + return m + end + return ODESystem(eqs, t, vars, params; name = name, + continuous_events = [[x ~ max_time] => reset]) + end + + function weird2(max_time; name) + params = @parameters begin + θ(t) = 0.0 + end + vars = @variables begin + x(t) = 0.0 + end + eqs = reduce(vcat, Symbolics.scalarize.([ + D(x) ~ 1.0 + ])) + return ODESystem(eqs, t, vars, params; name = name) # note no event + end + + @named wd1 = weird1(0.021) + @named wd2 = weird2(0.021) + + sys1 = structural_simplify(ODESystem([], t; name = :parent, + discrete_events = [0.01 => ModelingToolkit.ImperativeAffect( + modified = (; θs = reduce(vcat, [[wd1.θ]])), ctx = [1]) do m, o, c, i + @set! m.θs[1] = c[] += 1 + end], + systems = [wd1])) + sys2 = structural_simplify(ODESystem([], t; name = :parent, + discrete_events = [0.01 => ModelingToolkit.ImperativeAffect( + modified = (; θs = reduce(vcat, [[wd2.θ]])), ctx = [1]) do m, o, c, i + @set! m.θs[1] = c[] += 1 + end], + systems = [wd2])) + + sol1 = solve(ODEProblem(sys1, [], (0.0, 1.0)), Tsit5()) + @test 100.0 ∈ sol1[sys1.wd1.θ] + + sol2 = solve(ODEProblem(sys2, [], (0.0, 1.0)), Tsit5()) + @test 100.0 ∈ sol2[sys2.wd2.θ] +end From bf3cd3394212acd34678e1927f79b958b5320bcd Mon Sep 17 00:00:00 2001 From: vyudu Date: Tue, 4 Feb 2025 14:52:43 -0500 Subject: [PATCH 0779/2176] reorganization into functions --- .../symbolics_tearing.jl | 376 ++++++++++-------- 1 file changed, 208 insertions(+), 168 deletions(-) diff --git a/src/structural_transformation/symbolics_tearing.jl b/src/structural_transformation/symbolics_tearing.jl index 6340087003..cf70914a51 100644 --- a/src/structural_transformation/symbolics_tearing.jl +++ b/src/structural_transformation/symbolics_tearing.jl @@ -237,16 +237,22 @@ function check_diff_graph(var_to_diff, fullvars) end =# +""" +Replace derivatives of non-selected unknown variables by dummy derivatives. -function substitute_dummy_derivatives!(fullvars, var_to_diff, diff_to_var, var_eq_matching, neweqs) - # Dummy derivatives may determine that some differential variables are - # algebraic variables in disguise. The derivative of such variables are - # called dummy derivatives. +State selection may determine that some differential variables are +algebraic variables in disguise. The derivative of such variables are +called dummy derivatives. - # Step 1: - # Replace derivatives of non-selected unknown variables by dummy derivatives +`SelectedState` information is no longer needed past here. State selection +is done. All non-differentiated variables are algebraic variables, and all +variables that appear differentiated are differential variables. +""" +function substitute_dummy_derivatives!(ts::TearingState, neweqs, dummy_sub, var_eq_matching) + @unpack fullvars, sys, structure = ts + @unpack solvable_graph, var_to_diff, eq_to_diff, graph, lowest_shift = structure + diff_to_var = invview(var_to_diff) - dummy_sub = Dict() for var in 1:length(fullvars) dv = var_to_diff[var] dv === nothing && continue @@ -279,163 +285,73 @@ function substitute_dummy_derivatives!(fullvars, var_to_diff, diff_to_var, var_e end end -function generate_derivative_variables!() -end +""" +Generate new derivative variables for the system. -# Documenting the differences to structural simplification for discrete systems: -# In discrete systems the lowest-order term is x_k-i, instead of x(t). In order -# to substitute dummy variables for x_k-1, x_k-2, ... instead you need to reverse -# the order. So for discrete systems `var_order` is defined a little differently. -# -# The orders will also be off by one. The reason this is is that the dynamics of -# the system should be given in terms of Shift(t, 1)(x(t), x(t-1), ...). But -# having the observables be indexed by the next time step is not so nice. So we -# handle the shifts in the renaming, rather than explicitly. -# -# The substitution should look like the following: -# x(t) -> Shift(t, 1)(x(t)) -# x(k-1) -> x(t) -# x(k-2) -> x_{t-1}(t) -# x(k-3) -> x_{t-2}(t) -# and so on... -# -# In the implicit discrete case this shouldn't happen. The simplification should -# look like a NonlinearSystem. -# -# For discrete systems Shift(t, 2)(x(t)) is not equivalent to Shift(t, 1)(Shift(t,1)(x(t)) -# This is different from the continuous case where D(D(x)) can be substituted for -# by iteratively substituting x_t ~ D(x), then x_tt ~ D(x_t). For this reason the -# total_sub dict is updated at the time that the renamed variables are written, -# inside the loop where new variables are generated. +There are three cases where we want to generate new variables to convert +the system into first order (semi-implicit) ODEs. + +1. To first order: +Whenever higher order differentiated variable like `D(D(D(x)))` appears, +we introduce new variables `x_t`, `x_tt`, and `x_ttt` and new equations +``` +D(x_tt) = x_ttt +D(x_t) = x_tt +D(x) = x_t +``` +and replace `D(x)` to `x_t`, `D(D(x))` to `x_tt`, and `D(D(D(x)))` to +`x_ttt`. + +2. To implicit to semi-implicit ODEs: +2.1: Unsolvable derivative: +If one derivative variable `D(x)` is unsolvable in all the equations it +appears in, then we introduce a new variable `x_t`, a new equation +``` +D(x) ~ x_t +``` +and replace all other `D(x)` to `x_t`. + +2.2: Solvable derivative: +If one derivative variable `D(x)` is solvable in at least one of the +equations it appears in, then we introduce a new variable `x_t`. One of +the solvable equations must be in the form of `0 ~ L(D(x), u...)` and +there exists a function `l` such that `D(x) ~ l(u...)`. We should replace +it to +``` +0 ~ x_t - l(u...) +D(x) ~ x_t +``` +and replace all other `D(x)` to `x_t`. + +Observe that we don't need to actually introduce a new variable `x_t`, as +the above equations can be lowered to +``` +x_t := l(u...) +D(x) ~ x_t +``` +where `:=` denotes assignment. + +As a final note, in all the above cases where we need to introduce new +variables and equations, don't add them when they already exist. +""" +function generate_derivative_variables!(ts::TearingState, neweqs, total_sub, var_eq_matching, var_order; + is_discrete = false, mm = nothing) -import ModelingToolkit: Shift -function tearing_reassemble(state::TearingState, var_eq_matching, - full_var_eq_matching = nothing; simplify = false, mm = nothing, cse_hack = true, array_hack = true) - @unpack fullvars, sys, structure = state + @unpack fullvars, sys, structure = ts @unpack solvable_graph, var_to_diff, eq_to_diff, graph, lowest_shift = structure - extra_vars = Int[] - if full_var_eq_matching !== nothing - for v in 𝑑vertices(state.structure.graph) - eq = full_var_eq_matching[v] - eq isa Int && continue - push!(extra_vars, v) - end - end - - if ModelingToolkit.has_iv(state.sys) - iv = get_iv(state.sys) - if is_only_discrete(state.structure) - D = Shift(iv, 1) - lower_name = lower_varname_withshift - idx_to_lowest_shift = Dict{Int, Int}(var => 0 for var in 1:length(fullvars)) - for (i,var) in enumerate(fullvars) - key = (operation(var) isa Shift) ? only(arguments(var)) : var - idx_to_lowest_shift[i] = get(lowest_shift, key, 0) - end - else - D = Differential(iv) - lower_name = lower_varname_with_unit - end - else - iv = D = nothing - lower_name = lower_varname_with_unit - end - + eq_var_matching = invview(var_eq_matching) diff_to_var = invview(var_to_diff) - neweqs = collect(equations(state)) - - # Terminology and Definition: - # - # A general DAE is in the form of `F(u'(t), u(t), p, t) == 0`. We can - # characterize variables in `u(t)` into two classes: differential variables - # (denoted `v(t)`) and algebraic variables (denoted `z(t)`). Differential - # variables are marked as `SelectedState` and they are differentiated in the - # DAE system, i.e. `v'(t)` are all the variables in `u'(t)` that actually - # appear in the system. Algebraic variables are variables that are not - # differential variables. - - substitute_dummy_derivatives!(fullvars, var_to_diff, diff_to_var, var_eq_matching, neweqs) - - # `SelectedState` information is no longer needed past here. State selection - # is done. All non-differentiated variables are algebraic variables, and all - # variables that appear differentiated are differential variables. - - ### Extract partition information - is_solvable = let solvable_graph = solvable_graph - (eq, iv) -> eq isa Int && iv isa Int && BipartiteEdge(eq, iv) in solvable_graph - end - - # if var is like D(x) or Shift(t, 1)(x) - isdervar = let diff_to_var = diff_to_var - var -> diff_to_var[var] !== nothing - end - var_order = let diff_to_var = diff_to_var - dv -> begin - order = 0 - while (dv′ = diff_to_var[dv]) !== nothing - order += 1 - dv = dv′ - end - is_only_discrete(state.structure) && (order = -idx_to_lowest_shift[dv] - order) - order, dv - end - end + iv = ModelingToolkit.has_iv(sys) ? ModelingToolkit.get_iv(sys) : nothing + lower_name = is_discrete ? lower_name_withshift : lower_name_with_unit - # There are three cases where we want to generate new variables to convert - # the system into first order (semi-implicit) ODEs. - # - # 1. To first order: - # Whenever higher order differentiated variable like `D(D(D(x)))` appears, - # we introduce new variables `x_t`, `x_tt`, and `x_ttt` and new equations - # ``` - # D(x_tt) = x_ttt - # D(x_t) = x_tt - # D(x) = x_t - # ``` - # and replace `D(x)` to `x_t`, `D(D(x))` to `x_tt`, and `D(D(D(x)))` to - # `x_ttt`. - # - # 2. To implicit to semi-implicit ODEs: - # 2.1: Unsolvable derivative: - # If one derivative variable `D(x)` is unsolvable in all the equations it - # appears in, then we introduce a new variable `x_t`, a new equation - # ``` - # D(x) ~ x_t - # ``` - # and replace all other `D(x)` to `x_t`. - # - # 2.2: Solvable derivative: - # If one derivative variable `D(x)` is solvable in at least one of the - # equations it appears in, then we introduce a new variable `x_t`. One of - # the solvable equations must be in the form of `0 ~ L(D(x), u...)` and - # there exists a function `l` such that `D(x) ~ l(u...)`. We should replace - # it to - # ``` - # 0 ~ x_t - l(u...) - # D(x) ~ x_t - # ``` - # and replace all other `D(x)` to `x_t`. - # - # Observe that we don't need to actually introduce a new variable `x_t`, as - # the above equations can be lowered to - # ``` - # x_t := l(u...) - # D(x) ~ x_t - # ``` - # where `:=` denotes assignment. - # - # As a final note, in all the above cases where we need to introduce new - # variables and equations, don't add them when they already exist. - eq_var_matching = invview(var_eq_matching) linear_eqs = mm === nothing ? Dict{Int, Int}() : Dict(reverse(en) for en in enumerate(mm.nzrows)) - total_sub = Dict() for v in 1:length(var_to_diff) is_highest_discrete = begin var = fullvars[v] op = operation(var) - if (!is_only_discrete(state.structure) || op isa Shift) + if (!is_discrete || op isa Shift) false elseif !haskey(lowest_shift, var) false @@ -450,7 +366,8 @@ function tearing_reassemble(state::TearingState, var_eq_matching, solved = var_eq_matching[dv] isa Int solved && continue - # check if there's `D(x) = x_t` already + + # Check if there's `D(x) = x_t` already local v_t, dummy_eq for eq in 𝑑neighbors(solvable_graph, dv) mi = get(linear_eqs, eq, 0) @@ -468,6 +385,7 @@ function tearing_reassemble(state::TearingState, var_eq_matching, @goto FOUND_DUMMY_EQ end end + dx = fullvars[dv] # add `x_t` println() @@ -499,7 +417,7 @@ function tearing_reassemble(state::TearingState, var_eq_matching, add_edge!(solvable_graph, dummy_eq, dv) @assert nsrcs(graph) == nsrcs(solvable_graph) == dummy_eq @label FOUND_DUMMY_EQ - is_only_discrete(state.structure) && begin + is_discrete && begin idx_to_lowest_shift[v_t] = idx_to_lowest_shift[dv] operation(dx) isa Shift && (total_sub[dx] = x_t) order == 1 && (total_sub[x_t] = fullvars[var_to_diff[dv]]) @@ -509,13 +427,51 @@ function tearing_reassemble(state::TearingState, var_eq_matching, eq_var_matching[dummy_eq] = dv end @show total_sub +end + +""" +Solve the solvable equations of the system and generate differential (or discrete) +equations in terms of the selected states. + +Will reorder equations and unknowns to be: + [diffeqs; ...] + [diffvars; ...] +such that the mass matrix is: + [I 0 + 0 0]. + +Update the state to account for the new ordering and equations. +""" +function solve_and_generate_equations!(state::TearingState, neweqs, total_sub, var_eq_matching, var_order) + @unpack fullvars, sys, structure = state + @unpack solvable_graph, var_to_diff, eq_to_diff, graph, lowest_shift = structure + eq_var_matching = invview(var_eq_matching) + diff_to_var = invview(var_to_diff) + + if ModelingToolkit.has_iv(sys) + iv = get_iv(sys) + if is_only_discrete(structure) + D = Shift(iv, 1) + lower_name = lower_varname_withshift + else + D = Differential(iv) + lower_name = lower_varname_with_unit + end + else + iv = D = nothing + lower_name = lower_varname_with_unit + end + + # if var is like D(x) or Shift(t, 1)(x) + isdervar = let diff_to_var = diff_to_var + var -> diff_to_var[var] !== nothing + end + + ### Extract partition information + is_solvable = let solvable_graph = solvable_graph + (eq, iv) -> eq isa Int && iv isa Int && BipartiteEdge(eq, iv) in solvable_graph + end - # Will reorder equations and unknowns to be: - # [diffeqs; ...] - # [diffvars; ...] - # such that the mass matrix is: - # [I 0 - # 0 0]. diffeq_idxs = Int[] algeeq_idxs = Int[] diff_eqs = Equation[] @@ -525,14 +481,11 @@ function tearing_reassemble(state::TearingState, var_eq_matching, solved_equations = Int[] solved_variables = Int[] - # Solve solvable equations - println() - println("SOLVING SOLVABLE EQUATIONS.") toporder = topological_sort(DiCMOBiGraph{false}(graph, var_eq_matching)) eqs = Iterators.reverse(toporder) - idep = iv - @show eq_var_matching + idep = ModelingToolkit.has_iv(sys) ? ModelingToolkit.get_iv(sys) : nothing + # Generate differential equations in terms of the new variables. for ieq in eqs println() iv = eq_var_matching[ieq] @@ -597,6 +550,7 @@ function tearing_reassemble(state::TearingState, var_eq_matching, push!(algeeq_idxs, ieq) end end + # TODO: BLT sorting neweqs = [diff_eqs; alge_eqs] inveqsperm = [diffeq_idxs; algeeq_idxs] @@ -625,7 +579,6 @@ function tearing_reassemble(state::TearingState, var_eq_matching, graph = contract_variables(graph, var_eq_matching, varsperm, eqsperm, length(solved_variables), length(solved_variables_set)) - # Update system new_var_to_diff = complete(DiffGraph(length(invvarsperm))) for (v, d) in enumerate(var_to_diff) v′ = varsperm[v] @@ -641,6 +594,92 @@ function tearing_reassemble(state::TearingState, var_eq_matching, new_eq_to_diff[v′] = d′ > 0 ? d′ : nothing end + fullvars[invvarsperm], new_var_to_diff, new_eq_to_diff, neweqs, subeqs +end + +# Documenting the differences to structural simplification for discrete systems: +# In discrete systems the lowest-order term is x_k-i, instead of x(t). In order +# to substitute dummy variables for x_k-1, x_k-2, ... instead you need to reverse +# the order. So for discrete systems `var_order` is defined a little differently. +# +# The orders will also be off by one. The reason this is is that the dynamics of +# the system should be given in terms of Shift(t, 1)(x(t), x(t-1), ...). But +# having the observables be indexed by the next time step is not so nice. So we +# handle the shifts in the renaming, rather than explicitly. +# +# The substitution should look like the following: +# x(t) -> Shift(t, 1)(x(t)) +# x(k-1) -> x(t) +# x(k-2) -> x_{t-1}(t) +# x(k-3) -> x_{t-2}(t) +# and so on... +# +# In the implicit discrete case this shouldn't happen. The simplification should +# look like a NonlinearSystem. +# +# For discrete systems Shift(t, 2)(x(t)) is not equivalent to Shift(t, 1)(Shift(t,1)(x(t)) +# This is different from the continuous case where D(D(x)) can be substituted for +# by iteratively substituting x_t ~ D(x), then x_tt ~ D(x_t). For this reason the +# total_sub dict is updated at the time that the renamed variables are written, +# inside the loop where new variables are generated. + + +# Terminology and Definition: +# +# A general DAE is in the form of `F(u'(t), u(t), p, t) == 0`. We can +# characterize variables in `u(t)` into two classes: differential variables +# (denoted `v(t)`) and algebraic variables (denoted `z(t)`). Differential +# variables are marked as `SelectedState` and they are differentiated in the +# DAE system, i.e. `v'(t)` are all the variables in `u'(t)` that actually +# appear in the system. Algebraic variables are variables that are not +# differential variables. + +import ModelingToolkit: Shift + +function tearing_reassemble(state::TearingState, var_eq_matching, + full_var_eq_matching = nothing; simplify = false, mm = nothing, cse_hack = true, array_hack = true) + @unpack fullvars, sys, structure = state + @unpack solvable_graph, var_to_diff, eq_to_diff, graph, lowest_shift = structure + extra_vars = Int[] + if full_var_eq_matching !== nothing + for v in 𝑑vertices(state.structure.graph) + eq = full_var_eq_matching[v] + eq isa Int && continue + push!(extra_vars, v) + end + end + neweqs = collect(equations(state)) + diff_to_var = invview(var_to_diff) + total_sub = Dict() + dummy_sub = Dict() + is_discrete = is_only_discrete(state.structure) + + if is_discrete + idx_to_lowest_shift = Dict{Int, Int}(var => 0 for var in 1:length(fullvars)) + for (i,var) in enumerate(fullvars) + key = (operation(var) isa Shift) ? only(arguments(var)) : var + idx_to_lowest_shift[i] = get(lowest_shift, key, 0) + end + end + var_order = let diff_to_var = diff_to_var + dv -> begin + order = 0 + while (dv′ = diff_to_var[dv]) !== nothing + order += 1 + dv = dv′ + end + is_discrete && (order = -idx_to_lowest_shift[dv] - order) + order, dv + end + end + + # Structural simplification + substitute_dummy_derivatives!(state, neweqs, dummy_sub, var_eq_matching) + generate_derivative_variables!(state, neweqs, total_sub, var_eq_matching, var_order; + is_discrete, mm) + new_fullvars, new_var_to_diff, new_eq_to_diff, neweqs, subeqs = solve_and_generate_equations!(state, neweqs, total_sub, var_eq_matching, var_order) + + # Update system var_to_diff = new_var_to_diff eq_to_diff = new_eq_to_diff diff_to_var = invview(var_to_diff) @@ -649,7 +688,7 @@ function tearing_reassemble(state::TearingState, var_eq_matching, @set! state.structure.graph = complete(graph) @set! state.structure.var_to_diff = var_to_diff @set! state.structure.eq_to_diff = eq_to_diff - @set! state.fullvars = fullvars = fullvars[invvarsperm] + @set! state.fullvars = fullvars = new_fullvars ispresent = let var_to_diff = var_to_diff, graph = graph i -> (!isempty(𝑑neighbors(graph, i)) || (var_to_diff[i] !== nothing && !isempty(𝑑neighbors(graph, var_to_diff[i])))) @@ -657,11 +696,13 @@ function tearing_reassemble(state::TearingState, var_eq_matching, sys = state.sys + @show dummy_sub obs_sub = dummy_sub for eq in neweqs isdiffeq(eq) || continue obs_sub[eq.lhs] = eq.rhs end + @show obs_sub # TODO: compute the dependency correctly so that we don't have to do this obs = [fast_substitute(observed(sys), obs_sub); subeqs] @@ -680,7 +721,6 @@ function tearing_reassemble(state::TearingState, var_eq_matching, @set! sys.eqs = neweqs @set! sys.observed = obs - @set! sys.substitutions = Substitutions(subeqs, deps) # Only makes sense for time-dependent From 7fa4324e41136d42b7e5b5f8818c4ab796ae55ad Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 5 Feb 2025 15:28:35 +0530 Subject: [PATCH 0780/2176] fix: fix `@mtkbuild` macro keyword parsing --- src/systems/abstractsystem.jl | 15 ++++++++++----- test/if_lifting.jl | 14 ++++++++++++++ 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index 0444877432..dc47457825 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -2248,15 +2248,20 @@ macro mtkbuild(exprs...) expr = exprs[1] named_expr = ModelingToolkit.named_expr(expr) name = named_expr.args[1] - kwargs = if length(exprs) > 1 - NamedTuple{Tuple(ex.args[1] for ex in Base.tail(exprs))}(Tuple(ex.args[2] - for ex in Base.tail(exprs))) + kwargs = Base.tail(exprs) + kwargs = map(kwargs) do ex + @assert ex.head == :(=) + Expr(:kw, ex.args[1], ex.args[2]) + end + if isempty(kwargs) + kwargs = () else - (;) + kwargs = (Expr(:parameters, kwargs...),) end + call_expr = Expr(:call, structural_simplify, kwargs..., name) esc(quote $named_expr - $name = $structural_simplify($name; $(kwargs)...) + $name = $call_expr end) end diff --git a/test/if_lifting.jl b/test/if_lifting.jl index b72b468bf1..9c58e676d0 100644 --- a/test/if_lifting.jl +++ b/test/if_lifting.jl @@ -110,3 +110,17 @@ end @test operation(eq.rhs) === ifelse end end + +@testset "`@mtkbuild` macro accepts `additional_passes`" begin + @mtkmodel SimpleAbs begin + @variables begin + x(t) + y(t) + end + @equations begin + D(x) ~ abs(y) + y ~ sin(t) + end + end + @test_nowarn @mtkbuild sys=SimpleAbs() additional_passes=[IfLifting] +end From 9012d9bd8f7baca0713fef47ee3894a34dc8c38b Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 5 Feb 2025 15:28:39 +0530 Subject: [PATCH 0781/2176] refactor: format --- docs/src/basics/Variable_metadata.md | 6 ++++-- src/variables.jl | 9 ++++++--- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/docs/src/basics/Variable_metadata.md b/docs/src/basics/Variable_metadata.md index bcfe90bab4..44dfb30327 100644 --- a/docs/src/basics/Variable_metadata.md +++ b/docs/src/basics/Variable_metadata.md @@ -56,6 +56,7 @@ using ModelingToolkit: t_nounits as t, D_nounits as D @variables k(t) [connect = Stream] hasconnect(i) ``` + ```@example connect getconnect(k) ``` @@ -197,12 +198,13 @@ state_priority(important_dof) ## Units -Units for variables can be designated using symbolic metadata. For more information, please see the [model validation and units](@ref units) section of the docs. Note that `getunit` is not equivalent to `get_unit` - the former is a metadata getter for individual variables (and is provided so the same interface function for `unit` exists like other metadata), while the latter is used to handle more general symbolic expressions. +Units for variables can be designated using symbolic metadata. For more information, please see the [model validation and units](@ref units) section of the docs. Note that `getunit` is not equivalent to `get_unit` - the former is a metadata getter for individual variables (and is provided so the same interface function for `unit` exists like other metadata), while the latter is used to handle more general symbolic expressions. ```@example metadata -@variable speed [unit=u"m/s"] +@variable speed [unit = u"m/s"] hasunit(speed) ``` + ```@example metadata getunit(speed) ``` diff --git a/src/variables.jl b/src/variables.jl index 049dac790b..536119f107 100644 --- a/src/variables.jl +++ b/src/variables.jl @@ -28,7 +28,8 @@ ModelingToolkit.dump_variable_metadata(p) """ function dump_variable_metadata(var) uvar = unwrap(var) - variable_source, name = Symbolics.getmetadata(uvar, VariableSource, (:unknown, :unknown)) + variable_source, name = Symbolics.getmetadata( + uvar, VariableSource, (:unknown, :unknown)) type = symtype(uvar) if type <: AbstractArray shape = Symbolics.shape(var) @@ -102,7 +103,9 @@ getconnect(x::Symbolic) = Symbolics.getmetadata(x, VariableConnectType, nothing) Determine whether variable `x` has a connect type. See also [`getconnect`](@ref). """ hasconnect(x) = getconnect(x) !== nothing -setconnect(x, t::Type{T}) where T <: AbstractConnectType = setmetadata(x, VariableConnectType, t) +function setconnect(x, t::Type{T}) where {T <: AbstractConnectType} + setmetadata(x, VariableConnectType, t) +end ### Input, Output, Irreducible isvarkind(m, x::Union{Num, Symbolics.Arr}) = isvarkind(m, value(x)) @@ -581,7 +584,7 @@ metadata associated with it. See also [`getmisc(x)`](@ref). """ hasmisc(x) = getmisc(x) !== nothing -setmisc(x, miscdata) = setmetadata(x, VariableMisc, miscdata) +setmisc(x, miscdata) = setmetadata(x, VariableMisc, miscdata) ## Units ====================================================================== """ From de6f54d3d41865acd8d01e11e140f3f08f06d4f5 Mon Sep 17 00:00:00 2001 From: David Widmann Date: Wed, 5 Feb 2025 12:16:17 +0100 Subject: [PATCH 0782/2176] Support specialization in `ODEFunctionExpr` --- src/systems/diffeqs/abstractodesystem.jl | 48 +++++++++++++----------- test/odesystem.jl | 19 ++++++++++ 2 files changed, 45 insertions(+), 22 deletions(-) diff --git a/src/systems/diffeqs/abstractodesystem.jl b/src/systems/diffeqs/abstractodesystem.jl index d9b8fb31ab..ef48efd8e9 100644 --- a/src/systems/diffeqs/abstractodesystem.jl +++ b/src/systems/diffeqs/abstractodesystem.jl @@ -542,7 +542,7 @@ Create a Julia expression for an `ODEFunction` from the [`ODESystem`](@ref). The arguments `dvs` and `ps` are used to set the order of the dependent variable and parameter vectors, respectively. """ -struct ODEFunctionExpr{iip} end +struct ODEFunctionExpr{iip,specialize} end struct ODEFunctionClosure{O, I} <: Function f_oop::O @@ -551,7 +551,7 @@ end (f::ODEFunctionClosure)(u, p, t) = f.f_oop(u, p, t) (f::ODEFunctionClosure)(du, u, p, t) = f.f_iip(du, u, p, t) -function ODEFunctionExpr{iip}(sys::AbstractODESystem, dvs = unknowns(sys), +function ODEFunctionExpr{iip,specialize}(sys::AbstractODESystem, dvs = unknowns(sys), ps = parameters(sys), u0 = nothing; version = nothing, tgrad = false, jac = false, p = nothing, @@ -560,14 +560,12 @@ function ODEFunctionExpr{iip}(sys::AbstractODESystem, dvs = unknowns(sys), steady_state = false, sparsity = false, observedfun_exp = nothing, - kwargs...) where {iip} + kwargs...) where {iip,specialize} if !iscomplete(sys) error("A completed system is required. Call `complete` or `structural_simplify` on the system before creating an `ODEFunctionExpr`") end f_oop, f_iip = generate_function(sys, dvs, ps; expression = Val{true}, kwargs...) - dict = Dict() - fsym = gensym(:f) _f = :($fsym = $ODEFunctionClosure($f_oop, $f_iip)) tgradsym = gensym(:tgrad) @@ -590,30 +588,28 @@ function ODEFunctionExpr{iip}(sys::AbstractODESystem, dvs = unknowns(sys), _jac = :($jacsym = nothing) end + Msym = gensym(:M) M = calculate_massmatrix(sys) - - _M = if sparse && !(u0 === nothing || M === I) - SparseArrays.sparse(M) + if sparse && !(u0 === nothing || M === I) + _M = :($Msym = $(SparseArrays.sparse(M))) elseif u0 === nothing || M === I - M + _M = :($Msym = $M) else - ArrayInterface.restructure(u0 .* u0', M) + _M = :($Msym = $(ArrayInterface.restructure(u0 .* u0', M))) end jp_expr = sparse ? :($similar($(get_jac(sys)[]), Float64)) : :nothing ex = quote - $_f - $_tgrad - $_jac - M = $_M - ODEFunction{$iip}($fsym, - sys = $sys, - jac = $jacsym, - tgrad = $tgradsym, - mass_matrix = M, - jac_prototype = $jp_expr, - sparsity = $(sparsity ? jacobian_sparsity(sys) : nothing), - observed = $observedfun_exp) + let $_f, $_tgrad, $_jac, $_M + ODEFunction{$iip,$specialize}($fsym, + sys = $sys, + jac = $jacsym, + tgrad = $tgradsym, + mass_matrix = $Msym, + jac_prototype = $jp_expr, + sparsity = $(sparsity ? jacobian_sparsity(sys) : nothing), + observed = $observedfun_exp) + end end !linenumbers ? Base.remove_linenums!(ex) : ex end @@ -622,6 +618,14 @@ function ODEFunctionExpr(sys::AbstractODESystem, args...; kwargs...) ODEFunctionExpr{true}(sys, args...; kwargs...) end +function ODEFunctionExpr{true}(sys::AbstractODESystem, args...; kwargs...) + return ODEFunctionExpr{true, SciMLBase.AutoSpecialize}(sys, args...; kwargs...) +end + +function ODEFunctionExpr{false}(sys::AbstractODESystem, args...; kwargs...) + return ODEFunctionExpr{false, SciMLBase.FullSpecialize}(sys, args...; kwargs...) +end + """ ```julia DAEFunctionExpr{iip}(sys::AbstractODESystem, dvs = unknowns(sys), diff --git a/test/odesystem.jl b/test/odesystem.jl index 87c192afc2..ea6f22f33b 100644 --- a/test/odesystem.jl +++ b/test/odesystem.jl @@ -97,6 +97,25 @@ f.f(du, u, p, 0.1) @test du == [4, 0, -16] @test_throws ArgumentError f.f(u, p, 0.1) +#check iip +f = eval(ODEFunctionExpr(de, [x, y, z], [σ, ρ, β])) +f2 = ODEFunction(de, [x, y, z], [σ, ρ, β]) +@test SciMLBase.isinplace(f) === SciMLBase.isinplace(f2) +@test SciMLBase.specialization(f) === SciMLBase.specialization(f2) +for iip in (true, false) + f = eval(ODEFunctionExpr{iip}(de, [x, y, z], [σ, ρ, β])) + f2 = ODEFunction{iip}(de, [x, y, z], [σ, ρ, β]) + @test SciMLBase.isinplace(f) === SciMLBase.isinplace(f2) === iip + @test SciMLBase.specialization(f) === SciMLBase.specialization(f2) + + for specialize in (SciMLBase.AutoSpecialize, SciMLBase.FullSpecialize) + f = eval(ODEFunctionExpr{iip,specialize}(de, [x, y, z], [σ, ρ, β])) + f2 = ODEFunction{iip,specialize}(de, [x, y, z], [σ, ρ, β]) + @test SciMLBase.isinplace(f) === SciMLBase.isinplace(f2) === iip + @test SciMLBase.specialization(f) === SciMLBase.specialization(f2) === specialize + end +end + #check sparsity f = eval(ODEFunctionExpr(de, [x, y, z], [σ, ρ, β], sparsity = true)) @test f.sparsity == ModelingToolkit.jacobian_sparsity(de) From 58dde099686862ca0dd3116667866b7a234c8162 Mon Sep 17 00:00:00 2001 From: David Widmann Date: Wed, 5 Feb 2025 12:28:22 +0100 Subject: [PATCH 0783/2176] Fix format Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- src/systems/diffeqs/abstractodesystem.jl | 8 ++++---- test/odesystem.jl | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/systems/diffeqs/abstractodesystem.jl b/src/systems/diffeqs/abstractodesystem.jl index ef48efd8e9..8d0720a1d2 100644 --- a/src/systems/diffeqs/abstractodesystem.jl +++ b/src/systems/diffeqs/abstractodesystem.jl @@ -542,7 +542,7 @@ Create a Julia expression for an `ODEFunction` from the [`ODESystem`](@ref). The arguments `dvs` and `ps` are used to set the order of the dependent variable and parameter vectors, respectively. """ -struct ODEFunctionExpr{iip,specialize} end +struct ODEFunctionExpr{iip, specialize} end struct ODEFunctionClosure{O, I} <: Function f_oop::O @@ -551,7 +551,7 @@ end (f::ODEFunctionClosure)(u, p, t) = f.f_oop(u, p, t) (f::ODEFunctionClosure)(du, u, p, t) = f.f_iip(du, u, p, t) -function ODEFunctionExpr{iip,specialize}(sys::AbstractODESystem, dvs = unknowns(sys), +function ODEFunctionExpr{iip, specialize}(sys::AbstractODESystem, dvs = unknowns(sys), ps = parameters(sys), u0 = nothing; version = nothing, tgrad = false, jac = false, p = nothing, @@ -560,7 +560,7 @@ function ODEFunctionExpr{iip,specialize}(sys::AbstractODESystem, dvs = unknowns( steady_state = false, sparsity = false, observedfun_exp = nothing, - kwargs...) where {iip,specialize} + kwargs...) where {iip, specialize} if !iscomplete(sys) error("A completed system is required. Call `complete` or `structural_simplify` on the system before creating an `ODEFunctionExpr`") end @@ -601,7 +601,7 @@ function ODEFunctionExpr{iip,specialize}(sys::AbstractODESystem, dvs = unknowns( jp_expr = sparse ? :($similar($(get_jac(sys)[]), Float64)) : :nothing ex = quote let $_f, $_tgrad, $_jac, $_M - ODEFunction{$iip,$specialize}($fsym, + ODEFunction{$iip, $specialize}($fsym, sys = $sys, jac = $jacsym, tgrad = $tgradsym, diff --git a/test/odesystem.jl b/test/odesystem.jl index ea6f22f33b..8281fafe93 100644 --- a/test/odesystem.jl +++ b/test/odesystem.jl @@ -109,8 +109,8 @@ for iip in (true, false) @test SciMLBase.specialization(f) === SciMLBase.specialization(f2) for specialize in (SciMLBase.AutoSpecialize, SciMLBase.FullSpecialize) - f = eval(ODEFunctionExpr{iip,specialize}(de, [x, y, z], [σ, ρ, β])) - f2 = ODEFunction{iip,specialize}(de, [x, y, z], [σ, ρ, β]) + f = eval(ODEFunctionExpr{iip, specialize}(de, [x, y, z], [σ, ρ, β])) + f2 = ODEFunction{iip, specialize}(de, [x, y, z], [σ, ρ, β]) @test SciMLBase.isinplace(f) === SciMLBase.isinplace(f2) === iip @test SciMLBase.specialization(f) === SciMLBase.specialization(f2) === specialize end From 5a77f4347b1b9259cb6da56128146af254b827a2 Mon Sep 17 00:00:00 2001 From: vyudu Date: Thu, 6 Feb 2025 17:22:49 -0500 Subject: [PATCH 0784/2176] explicit case --- .../symbolics_tearing.jl | 180 ++++++++++-------- src/structural_transformation/utils.jl | 7 +- src/systems/systems.jl | 8 +- 3 files changed, 104 insertions(+), 91 deletions(-) diff --git a/src/structural_transformation/symbolics_tearing.jl b/src/structural_transformation/symbolics_tearing.jl index cf70914a51..76bba2a303 100644 --- a/src/structural_transformation/symbolics_tearing.jl +++ b/src/structural_transformation/symbolics_tearing.jl @@ -333,6 +333,32 @@ where `:=` denotes assignment. As a final note, in all the above cases where we need to introduce new variables and equations, don't add them when they already exist. + +###### DISCRETE SYSTEMS ####### + +Documenting the differences to structural simplification for discrete systems: +In discrete systems the lowest-order term is x_k-i, instead of x(t). + +The orders will also be off by one. The reason this is is that the dynamics of +the system should be given in terms of Shift(t, 1)(x(t), x(t-1), ...). But +having the observables be indexed by the next time step is not so nice. So we +handle the shifts in the renaming, rather than explicitly. + +The substitution should look like the following: + x(t) -> Shift(t, 1)(x(t)) + Shift(t, -1)(x(t)) -> x(t) + Shift(t, -2)(x(t)) -> x_{t-1}(t) + Shift(t, -3)(x(t)) -> x_{t-2}(t) + and so on... + +In the implicit discrete case this shouldn't happen. The simplification should +look like a NonlinearSystem. + +For discrete systems Shift(t, 2)(x(t)) is not equivalent to Shift(t, 1)(Shift(t,1)(x(t)) +This is different from the continuous case where D(D(x)) can be substituted for +by iteratively substituting x_t ~ D(x), then x_tt ~ D(x_t). For this reason the +total_sub dict is updated at the time that the renamed variables are written, +inside the loop where new variables are generated. """ function generate_derivative_variables!(ts::TearingState, neweqs, total_sub, var_eq_matching, var_order; is_discrete = false, mm = nothing) @@ -342,26 +368,41 @@ function generate_derivative_variables!(ts::TearingState, neweqs, total_sub, var eq_var_matching = invview(var_eq_matching) diff_to_var = invview(var_to_diff) iv = ModelingToolkit.has_iv(sys) ? ModelingToolkit.get_iv(sys) : nothing - lower_name = is_discrete ? lower_name_withshift : lower_name_with_unit + lower_name = is_discrete ? lower_varname_withshift : lower_varname_with_unit + + # index v gets mapped to the lowest shift and the index of the unshifted variable + if is_discrete + idx_to_lowest_shift = Dict{Int, Tuple{Int, Int}}(var => (0,0) for var in 1:length(fullvars)) + var_to_unshiftedidx = Dict{Any, Int}(var => findfirst(x -> isequal(x, var), fullvars) for var in keys(lowest_shift)) + + for (i,var) in enumerate(fullvars) + key = (operation(var) isa Shift) ? only(arguments(var)) : var + idx_to_lowest_shift[i] = (get(lowest_shift, key, 0), get(var_to_unshiftedidx, key, i)) + end + end linear_eqs = mm === nothing ? Dict{Int, Int}() : Dict(reverse(en) for en in enumerate(mm.nzrows)) + # v is the index of the current variable, x = fullvars[v] + # dv is the index of the derivative dx = D(x), x_t is the substituted variable + # + # For ODESystems: lv is the index of the lowest-order variable (x(t)) + # For DiscreteSystems: + # - lv is the index of the lowest-order variable (Shift(t, k)(x(t))) + # - uv is the index of the highest-order variable (x(t)) for v in 1:length(var_to_diff) - is_highest_discrete = begin - var = fullvars[v] - op = operation(var) - if (!is_discrete || op isa Shift) - false - elseif !haskey(lowest_shift, var) - false - else - low = lowest_shift[var] - idx = findfirst(x -> isequal(x, Shift(iv, low)(var)), fullvars) - true + dv = var_to_diff[v] + if is_discrete + x = fullvars[v] + op = operation(x) + (low, uv) = idx_to_lowest_shift[v] + + # If v is unshifted (i.e. x(t)), then substitute the lowest-shift variable + if !(op isa Shift) && (low != 0) + dv = findfirst(_x -> isequal(_x, Shift(iv, low)(x)), fullvars) end end - dv = is_highest_discrete ? idx : var_to_diff[v] dv isa Int || continue solved = var_eq_matching[dv] isa Int @@ -388,13 +429,9 @@ function generate_derivative_variables!(ts::TearingState, neweqs, total_sub, var dx = fullvars[dv] # add `x_t` - println() - @show order, lv = var_order(dv) - x_t = lower_name(fullvars[lv], iv, order) - @show fullvars[v] - @show fullvars[dv] - @show fullvars[lv] - @show dx, x_t + order, lv = var_order(dv) + x_t = is_discrete ? lower_name(fullvars[lv], iv, -low-order-1; unshifted = fullvars[uv]) : + lower_name(fullvars[lv], iv, order) push!(fullvars, simplify_shifts(x_t)) v_t = length(fullvars) v_t_idx = add_vertex!(var_to_diff) @@ -402,11 +439,27 @@ function generate_derivative_variables!(ts::TearingState, neweqs, total_sub, var # TODO: do we care about solvable_graph? We don't use them after # `dummy_derivative_graph`. add_vertex!(solvable_graph, DST) - # var_eq_matching is a bit odd. - # length(var_eq_matching) == length(invview(var_eq_matching)) push!(var_eq_matching, unassigned) @assert v_t_idx == ndsts(graph) == ndsts(solvable_graph) == length(fullvars) == length(var_eq_matching) + + # Add the substitutions to total_sub directly. + is_discrete && begin + idx_to_lowest_shift[v_t] = idx_to_lowest_shift[dv] + @show dx + if operation(dx) isa Shift + total_sub[dx] = x_t + for e in 𝑑neighbors(graph, dv) + add_edge!(graph, e, v_t) + rem_edge!(graph, e, dv) + end + @show graph + !(operation(x) isa Shift) && begin + var_to_diff[v_t] = var_to_diff[dv] + continue + end + end + end # add `D(x) - x_t ~ 0` push!(neweqs, 0 ~ x_t - dx) add_vertex!(graph, SRC) @@ -417,16 +470,10 @@ function generate_derivative_variables!(ts::TearingState, neweqs, total_sub, var add_edge!(solvable_graph, dummy_eq, dv) @assert nsrcs(graph) == nsrcs(solvable_graph) == dummy_eq @label FOUND_DUMMY_EQ - is_discrete && begin - idx_to_lowest_shift[v_t] = idx_to_lowest_shift[dv] - operation(dx) isa Shift && (total_sub[dx] = x_t) - order == 1 && (total_sub[x_t] = fullvars[var_to_diff[dv]]) - end var_to_diff[v_t] = var_to_diff[dv] var_eq_matching[dv] = unassigned eq_var_matching[dummy_eq] = dv end - @show total_sub end """ @@ -442,7 +489,7 @@ such that the mass matrix is: Update the state to account for the new ordering and equations. """ -function solve_and_generate_equations!(state::TearingState, neweqs, total_sub, var_eq_matching, var_order) +function solve_and_generate_equations!(state::TearingState, neweqs, total_sub, var_eq_matching, var_order; simplify = false) @unpack fullvars, sys, structure = state @unpack solvable_graph, var_to_diff, eq_to_diff, graph, lowest_shift = structure eq_var_matching = invview(var_eq_matching) @@ -467,7 +514,7 @@ function solve_and_generate_equations!(state::TearingState, neweqs, total_sub, v var -> diff_to_var[var] !== nothing end - ### Extract partition information + # Extract partition information is_solvable = let solvable_graph = solvable_graph (eq, iv) -> eq isa Int && iv isa Int && BipartiteEdge(eq, iv) in solvable_graph end @@ -485,9 +532,12 @@ function solve_and_generate_equations!(state::TearingState, neweqs, total_sub, v eqs = Iterators.reverse(toporder) idep = ModelingToolkit.has_iv(sys) ? ModelingToolkit.get_iv(sys) : nothing - # Generate differential equations in terms of the new variables. + @show eq_var_matching + @show fullvars + @show neweqs + + # Equation ieq is solved for the RHS of iv for ieq in eqs - println() iv = eq_var_matching[ieq] if is_solvable(ieq, iv) # We don't solve differential equations, but we will need to try to @@ -497,23 +547,14 @@ function solve_and_generate_equations!(state::TearingState, neweqs, total_sub, v isnothing(D) && error("Differential found in a non-differential system. Likely this is a bug in the construction of an initialization system. Please report this issue with a reproducible example. Offending equation: $(equations(sys)[ieq])") order, lv = var_order(iv) - @show fullvars[iv] - @show (order, lv) - dx = D(simplify_shifts(lower_name( - fullvars[lv], idep, order - 1))) - @show dx + dx = D(fullvars[lv]) eq = dx ~ simplify_shifts(Symbolics.fixpoint_sub( Symbolics.symbolic_linear_solve(neweqs[ieq], fullvars[iv]), total_sub; operator = ModelingToolkit.Shift)) - @show total_sub - @show eq for e in 𝑑neighbors(graph, iv) - e == ieq && continue - for v in 𝑠neighbors(graph, e) - add_edge!(graph, e, v) - end rem_edge!(graph, e, iv) + add_edge!(graph, e, lv) end push!(diff_eqs, eq) total_sub[simplify_shifts(eq.lhs)] = eq.rhs @@ -594,38 +635,10 @@ function solve_and_generate_equations!(state::TearingState, neweqs, total_sub, v new_eq_to_diff[v′] = d′ > 0 ? d′ : nothing end - fullvars[invvarsperm], new_var_to_diff, new_eq_to_diff, neweqs, subeqs + fullvars[invvarsperm], new_var_to_diff, new_eq_to_diff, neweqs, subeqs, graph end -# Documenting the differences to structural simplification for discrete systems: -# In discrete systems the lowest-order term is x_k-i, instead of x(t). In order -# to substitute dummy variables for x_k-1, x_k-2, ... instead you need to reverse -# the order. So for discrete systems `var_order` is defined a little differently. -# -# The orders will also be off by one. The reason this is is that the dynamics of -# the system should be given in terms of Shift(t, 1)(x(t), x(t-1), ...). But -# having the observables be indexed by the next time step is not so nice. So we -# handle the shifts in the renaming, rather than explicitly. -# -# The substitution should look like the following: -# x(t) -> Shift(t, 1)(x(t)) -# x(k-1) -> x(t) -# x(k-2) -> x_{t-1}(t) -# x(k-3) -> x_{t-2}(t) -# and so on... -# -# In the implicit discrete case this shouldn't happen. The simplification should -# look like a NonlinearSystem. -# -# For discrete systems Shift(t, 2)(x(t)) is not equivalent to Shift(t, 1)(Shift(t,1)(x(t)) -# This is different from the continuous case where D(D(x)) can be substituted for -# by iteratively substituting x_t ~ D(x), then x_tt ~ D(x_t). For this reason the -# total_sub dict is updated at the time that the renamed variables are written, -# inside the loop where new variables are generated. - - # Terminology and Definition: -# # A general DAE is in the form of `F(u'(t), u(t), p, t) == 0`. We can # characterize variables in `u(t)` into two classes: differential variables # (denoted `v(t)`) and algebraic variables (denoted `z(t)`). Differential @@ -654,13 +667,6 @@ function tearing_reassemble(state::TearingState, var_eq_matching, dummy_sub = Dict() is_discrete = is_only_discrete(state.structure) - if is_discrete - idx_to_lowest_shift = Dict{Int, Int}(var => 0 for var in 1:length(fullvars)) - for (i,var) in enumerate(fullvars) - key = (operation(var) isa Shift) ? only(arguments(var)) : var - idx_to_lowest_shift[i] = get(lowest_shift, key, 0) - end - end var_order = let diff_to_var = diff_to_var dv -> begin order = 0 @@ -668,7 +674,6 @@ function tearing_reassemble(state::TearingState, var_eq_matching, order += 1 dv = dv′ end - is_discrete && (order = -idx_to_lowest_shift[dv] - order) order, dv end end @@ -677,7 +682,7 @@ function tearing_reassemble(state::TearingState, var_eq_matching, substitute_dummy_derivatives!(state, neweqs, dummy_sub, var_eq_matching) generate_derivative_variables!(state, neweqs, total_sub, var_eq_matching, var_order; is_discrete, mm) - new_fullvars, new_var_to_diff, new_eq_to_diff, neweqs, subeqs = solve_and_generate_equations!(state, neweqs, total_sub, var_eq_matching, var_order) + new_fullvars, new_var_to_diff, new_eq_to_diff, neweqs, subeqs, graph = solve_and_generate_equations!(state, neweqs, total_sub, var_eq_matching, var_order; simplify) # Update system var_to_diff = new_var_to_diff @@ -693,16 +698,25 @@ function tearing_reassemble(state::TearingState, var_eq_matching, i -> (!isempty(𝑑neighbors(graph, i)) || (var_to_diff[i] !== nothing && !isempty(𝑑neighbors(graph, var_to_diff[i])))) end + @show graph + println() + println("Shift test...") + @show neweqs + @show fullvars + @show 𝑑neighbors(graph, 5) sys = state.sys - - @show dummy_sub obs_sub = dummy_sub for eq in neweqs isdiffeq(eq) || continue obs_sub[eq.lhs] = eq.rhs end + is_discrete && for eq in subeqs + obs_sub[eq.rhs] = eq.lhs + end + @show obs_sub + @show observed(sys) # TODO: compute the dependency correctly so that we don't have to do this obs = [fast_substitute(observed(sys), obs_sub); subeqs] diff --git a/src/structural_transformation/utils.jl b/src/structural_transformation/utils.jl index 707a57097b..8df2d4177b 100644 --- a/src/structural_transformation/utils.jl +++ b/src/structural_transformation/utils.jl @@ -449,10 +449,9 @@ end ### Misc ### -function lower_varname_withshift(var, iv, order) - order == 0 && return var - #order == -1 && return Shift(iv, 1)(var) - ds = "$iv-$(order-1)" +function lower_varname_withshift(var, iv, backshift; unshifted = nothing) + backshift == 0 && return unshifted + ds = "$iv-$backshift" d_separator = 'ˍ' if ModelingToolkit.isoperator(var, ModelingToolkit.Shift) diff --git a/src/systems/systems.jl b/src/systems/systems.jl index 9c8c272c5c..0a7e4264e4 100644 --- a/src/systems/systems.jl +++ b/src/systems/systems.jl @@ -41,10 +41,10 @@ function structural_simplify( end if newsys isa DiscreteSystem && any(eq -> symbolic_type(eq.lhs) == NotSymbolic(), equations(newsys)) - error(""" - Encountered algebraic equations when simplifying discrete system. This is \ - not yet supported. - """) + # error(""" + # Encountered algebraic equations when simplifying discrete system. This is \ + # not yet supported. + # """) end for pass in additional_passes newsys = pass(newsys) From 91acf912e7651b1482a5e29552026774ed5fe5f0 Mon Sep 17 00:00:00 2001 From: vyudu Date: Thu, 6 Feb 2025 22:48:29 -0500 Subject: [PATCH 0785/2176] beginning implicit equation --- .../symbolics_tearing.jl | 76 +++++++++---------- src/structural_transformation/utils.jl | 1 + 2 files changed, 37 insertions(+), 40 deletions(-) diff --git a/src/structural_transformation/symbolics_tearing.jl b/src/structural_transformation/symbolics_tearing.jl index 76bba2a303..fb1765964e 100644 --- a/src/structural_transformation/symbolics_tearing.jl +++ b/src/structural_transformation/symbolics_tearing.jl @@ -360,9 +360,8 @@ by iteratively substituting x_t ~ D(x), then x_tt ~ D(x_t). For this reason the total_sub dict is updated at the time that the renamed variables are written, inside the loop where new variables are generated. """ -function generate_derivative_variables!(ts::TearingState, neweqs, total_sub, var_eq_matching, var_order; +function generate_derivative_variables!(ts::TearingState, neweqs, total_sub, var_eq_matching; is_discrete = false, mm = nothing) - @unpack fullvars, sys, structure = ts @unpack solvable_graph, var_to_diff, eq_to_diff, graph, lowest_shift = structure eq_var_matching = invview(var_eq_matching) @@ -386,13 +385,14 @@ function generate_derivative_variables!(ts::TearingState, neweqs, total_sub, var # v is the index of the current variable, x = fullvars[v] # dv is the index of the derivative dx = D(x), x_t is the substituted variable - # # For ODESystems: lv is the index of the lowest-order variable (x(t)) # For DiscreteSystems: # - lv is the index of the lowest-order variable (Shift(t, k)(x(t))) # - uv is the index of the highest-order variable (x(t)) for v in 1:length(var_to_diff) dv = var_to_diff[v] + println() + @show (v, dv) if is_discrete x = fullvars[v] op = operation(x) @@ -405,6 +405,9 @@ function generate_derivative_variables!(ts::TearingState, neweqs, total_sub, var end dv isa Int || continue + @show dv + @show var_eq_matching[dv] + @show fullvars solved = var_eq_matching[dv] isa Int solved && continue @@ -429,9 +432,10 @@ function generate_derivative_variables!(ts::TearingState, neweqs, total_sub, var dx = fullvars[dv] # add `x_t` - order, lv = var_order(dv) + order, lv = var_order(diff_to_var, dv) x_t = is_discrete ? lower_name(fullvars[lv], iv, -low-order-1; unshifted = fullvars[uv]) : lower_name(fullvars[lv], iv, order) + @show dx, x_t push!(fullvars, simplify_shifts(x_t)) v_t = length(fullvars) v_t_idx = add_vertex!(var_to_diff) @@ -443,23 +447,23 @@ function generate_derivative_variables!(ts::TearingState, neweqs, total_sub, var @assert v_t_idx == ndsts(graph) == ndsts(solvable_graph) == length(fullvars) == length(var_eq_matching) - # Add the substitutions to total_sub directly. + # Add discrete substitutions to total_sub directly. is_discrete && begin idx_to_lowest_shift[v_t] = idx_to_lowest_shift[dv] - @show dx if operation(dx) isa Shift total_sub[dx] = x_t for e in 𝑑neighbors(graph, dv) add_edge!(graph, e, v_t) rem_edge!(graph, e, dv) end - @show graph + # Do not add the lowest-order substitution as an equation, just substitute !(operation(x) isa Shift) && begin var_to_diff[v_t] = var_to_diff[dv] continue end end end + # add `D(x) - x_t ~ 0` push!(neweqs, 0 ~ x_t - dx) add_vertex!(graph, SRC) @@ -489,7 +493,7 @@ such that the mass matrix is: Update the state to account for the new ordering and equations. """ -function solve_and_generate_equations!(state::TearingState, neweqs, total_sub, var_eq_matching, var_order; simplify = false) +function solve_and_generate_equations!(state::TearingState, neweqs, total_sub, var_eq_matching; simplify = false) @unpack fullvars, sys, structure = state @unpack solvable_graph, var_to_diff, eq_to_diff, graph, lowest_shift = structure eq_var_matching = invview(var_eq_matching) @@ -530,13 +534,11 @@ function solve_and_generate_equations!(state::TearingState, neweqs, total_sub, v toporder = topological_sort(DiCMOBiGraph{false}(graph, var_eq_matching)) eqs = Iterators.reverse(toporder) - idep = ModelingToolkit.has_iv(sys) ? ModelingToolkit.get_iv(sys) : nothing - - @show eq_var_matching - @show fullvars - @show neweqs + idep = iv - # Equation ieq is solved for the RHS of iv + # Generate differential equations. + # fullvars[iv] is a differential variable of the form D^n(x), and neweqs[ieq] + # is solved to give the RHS. for ieq in eqs iv = eq_var_matching[ieq] if is_solvable(ieq, iv) @@ -546,7 +548,7 @@ function solve_and_generate_equations!(state::TearingState, neweqs, total_sub, v if isdervar(iv) isnothing(D) && error("Differential found in a non-differential system. Likely this is a bug in the construction of an initialization system. Please report this issue with a reproducible example. Offending equation: $(equations(sys)[ieq])") - order, lv = var_order(iv) + order, lv = var_order(diff_to_var, iv) dx = D(fullvars[lv]) eq = dx ~ simplify_shifts(Symbolics.fixpoint_sub( Symbolics.symbolic_linear_solve(neweqs[ieq], @@ -634,8 +636,9 @@ function solve_and_generate_equations!(state::TearingState, neweqs, total_sub, v d′ = eqsperm[d] new_eq_to_diff[v′] = d′ > 0 ? d′ : nothing end + new_fullvars = fullvars[invvarsperm] - fullvars[invvarsperm], new_var_to_diff, new_eq_to_diff, neweqs, subeqs, graph + new_fullvars, new_var_to_diff, new_eq_to_diff, neweqs, subeqs, graph end # Terminology and Definition: @@ -649,6 +652,16 @@ end import ModelingToolkit: Shift +# Give the order of the variable indexed by dv +function var_order(diff_to_var, dv) + order = 0 + while (dv′ = diff_to_var[dv]) !== nothing + order += 1 + dv = dv′ + end + order, dv +end + function tearing_reassemble(state::TearingState, var_eq_matching, full_var_eq_matching = nothing; simplify = false, mm = nothing, cse_hack = true, array_hack = true) @unpack fullvars, sys, structure = state @@ -667,22 +680,12 @@ function tearing_reassemble(state::TearingState, var_eq_matching, dummy_sub = Dict() is_discrete = is_only_discrete(state.structure) - var_order = let diff_to_var = diff_to_var - dv -> begin - order = 0 - while (dv′ = diff_to_var[dv]) !== nothing - order += 1 - dv = dv′ - end - order, dv - end - end - # Structural simplification substitute_dummy_derivatives!(state, neweqs, dummy_sub, var_eq_matching) - generate_derivative_variables!(state, neweqs, total_sub, var_eq_matching, var_order; + generate_derivative_variables!(state, neweqs, total_sub, var_eq_matching; is_discrete, mm) - new_fullvars, new_var_to_diff, new_eq_to_diff, neweqs, subeqs, graph = solve_and_generate_equations!(state, neweqs, total_sub, var_eq_matching, var_order; simplify) + new_fullvars, new_var_to_diff, new_eq_to_diff, neweqs, subeqs, graph = + solve_and_generate_equations!(state, neweqs, total_sub, var_eq_matching; simplify) # Update system var_to_diff = new_var_to_diff @@ -698,25 +701,18 @@ function tearing_reassemble(state::TearingState, var_eq_matching, i -> (!isempty(𝑑neighbors(graph, i)) || (var_to_diff[i] !== nothing && !isempty(𝑑neighbors(graph, var_to_diff[i])))) end - @show graph - println() - println("Shift test...") - @show neweqs - @show fullvars + @show ispresent.(collect(1:length(fullvars))) @show 𝑑neighbors(graph, 5) + @show var_to_diff[5] + @show neweqs + @show fullvars sys = state.sys obs_sub = dummy_sub for eq in neweqs isdiffeq(eq) || continue obs_sub[eq.lhs] = eq.rhs end - is_discrete && for eq in subeqs - obs_sub[eq.rhs] = eq.lhs - end - - @show obs_sub - @show observed(sys) # TODO: compute the dependency correctly so that we don't have to do this obs = [fast_substitute(observed(sys), obs_sub); subeqs] diff --git a/src/structural_transformation/utils.jl b/src/structural_transformation/utils.jl index 8df2d4177b..b2d67dc547 100644 --- a/src/structural_transformation/utils.jl +++ b/src/structural_transformation/utils.jl @@ -450,6 +450,7 @@ end ### function lower_varname_withshift(var, iv, backshift; unshifted = nothing) + backshift < 0 && return Shift(iv, -backshift)(var) backshift == 0 && return unshifted ds = "$iv-$backshift" d_separator = 'ˍ' From 56829f7c05e94a6048c55b482af8c1752e9cbad7 Mon Sep 17 00:00:00 2001 From: vyudu Date: Fri, 7 Feb 2025 02:26:13 -0500 Subject: [PATCH 0786/2176] up --- .../symbolics_tearing.jl | 112 ++++++++++-------- src/structural_transformation/utils.jl | 13 +- 2 files changed, 73 insertions(+), 52 deletions(-) diff --git a/src/structural_transformation/symbolics_tearing.jl b/src/structural_transformation/symbolics_tearing.jl index fb1765964e..020c85b05f 100644 --- a/src/structural_transformation/symbolics_tearing.jl +++ b/src/structural_transformation/symbolics_tearing.jl @@ -252,6 +252,7 @@ function substitute_dummy_derivatives!(ts::TearingState, neweqs, dummy_sub, var_ @unpack fullvars, sys, structure = ts @unpack solvable_graph, var_to_diff, eq_to_diff, graph, lowest_shift = structure diff_to_var = invview(var_to_diff) + iv = ModelingToolkit.has_iv(sys) ? ModelingToolkit.get_iv(sys) : nothing for var in 1:length(fullvars) dv = var_to_diff[var] @@ -337,16 +338,17 @@ variables and equations, don't add them when they already exist. ###### DISCRETE SYSTEMS ####### Documenting the differences to structural simplification for discrete systems: -In discrete systems the lowest-order term is x_k-i, instead of x(t). + +1. In discrete systems the lowest-order term is Shift(t, k)(x(t)), instead of x(t). We need to substitute the k-1 lowest order terms instead of the k-1 highest order terms. The orders will also be off by one. The reason this is is that the dynamics of the system should be given in terms of Shift(t, 1)(x(t), x(t-1), ...). But having the observables be indexed by the next time step is not so nice. So we -handle the shifts in the renaming, rather than explicitly. +handle the shifts in the renaming. The substitution should look like the following: x(t) -> Shift(t, 1)(x(t)) - Shift(t, -1)(x(t)) -> x(t) + Shift(t, -1)(x(t)) -> Shift(t, 0)(x(t)) Shift(t, -2)(x(t)) -> x_{t-1}(t) Shift(t, -3)(x(t)) -> x_{t-2}(t) and so on... @@ -354,14 +356,14 @@ The substitution should look like the following: In the implicit discrete case this shouldn't happen. The simplification should look like a NonlinearSystem. -For discrete systems Shift(t, 2)(x(t)) is not equivalent to Shift(t, 1)(Shift(t,1)(x(t)) +2. For discrete systems Shift(t, 2)(x(t)) cannot be substituted as Shift(t, 1)(Shift(t,1)(x(t)). This is different from the continuous case where D(D(x)) can be substituted for by iteratively substituting x_t ~ D(x), then x_tt ~ D(x_t). For this reason the -total_sub dict is updated at the time that the renamed variables are written, +shift_sub dict is updated at the time that the renamed variables are written, inside the loop where new variables are generated. """ -function generate_derivative_variables!(ts::TearingState, neweqs, total_sub, var_eq_matching; - is_discrete = false, mm = nothing) +function generate_derivative_variables!(ts::TearingState, neweqs, var_eq_matching; + is_discrete = false, mm = nothing, shift_sub = nothing) @unpack fullvars, sys, structure = ts @unpack solvable_graph, var_to_diff, eq_to_diff, graph, lowest_shift = structure eq_var_matching = invview(var_eq_matching) @@ -391,23 +393,24 @@ function generate_derivative_variables!(ts::TearingState, neweqs, total_sub, var # - uv is the index of the highest-order variable (x(t)) for v in 1:length(var_to_diff) dv = var_to_diff[v] - println() - @show (v, dv) + if is_discrete x = fullvars[v] op = operation(x) (low, uv) = idx_to_lowest_shift[v] # If v is unshifted (i.e. x(t)), then substitute the lowest-shift variable - if !(op isa Shift) && (low != 0) + if !(op isa Shift) dv = findfirst(_x -> isequal(_x, Shift(iv, low)(x)), fullvars) end + dx = fullvars[dv] + order, lv = var_order(diff_to_var, dv) + @show fullvars[uv] + x_t = lower_name(fullvars[lv], iv, -low-order-1; unshifted = fullvars[uv]) + shift_sub[dx] = x_t + (var_eq_matching[dv] isa Int) ? continue : @goto DISCRETE_VARIABLE end dv isa Int || continue - - @show dv - @show var_eq_matching[dv] - @show fullvars solved = var_eq_matching[dv] isa Int solved && continue @@ -430,13 +433,13 @@ function generate_derivative_variables!(ts::TearingState, neweqs, total_sub, var end end - dx = fullvars[dv] # add `x_t` + dx = fullvars[dv] order, lv = var_order(diff_to_var, dv) - x_t = is_discrete ? lower_name(fullvars[lv], iv, -low-order-1; unshifted = fullvars[uv]) : - lower_name(fullvars[lv], iv, order) - @show dx, x_t - push!(fullvars, simplify_shifts(x_t)) + x_t = lower_name(fullvars[lv], iv, order) + + @label DISCRETE_VARIABLE + push!(fullvars, x_t) v_t = length(fullvars) v_t_idx = add_vertex!(var_to_diff) add_vertex!(graph, DST) @@ -444,23 +447,18 @@ function generate_derivative_variables!(ts::TearingState, neweqs, total_sub, var # `dummy_derivative_graph`. add_vertex!(solvable_graph, DST) push!(var_eq_matching, unassigned) - @assert v_t_idx == ndsts(graph) == ndsts(solvable_graph) == length(fullvars) == - length(var_eq_matching) # Add discrete substitutions to total_sub directly. is_discrete && begin idx_to_lowest_shift[v_t] = idx_to_lowest_shift[dv] - if operation(dx) isa Shift - total_sub[dx] = x_t - for e in 𝑑neighbors(graph, dv) - add_edge!(graph, e, v_t) - rem_edge!(graph, e, dv) - end - # Do not add the lowest-order substitution as an equation, just substitute - !(operation(x) isa Shift) && begin - var_to_diff[v_t] = var_to_diff[dv] - continue - end + for e in 𝑑neighbors(graph, dv) + add_edge!(graph, e, v_t) + rem_edge!(graph, e, dv) + end + # Do not add the lowest-order substitution as an equation, just substitute + !(operation(x) isa Shift) && begin + var_to_diff[v_t] = var_to_diff[dv] + continue end end @@ -472,7 +470,7 @@ function generate_derivative_variables!(ts::TearingState, neweqs, total_sub, var add_edge!(graph, dummy_eq, v_t) add_vertex!(solvable_graph, SRC) add_edge!(solvable_graph, dummy_eq, dv) - @assert nsrcs(graph) == nsrcs(solvable_graph) == dummy_eq + @label FOUND_DUMMY_EQ var_to_diff[v_t] = var_to_diff[dv] var_eq_matching[dv] = unassigned @@ -480,6 +478,14 @@ function generate_derivative_variables!(ts::TearingState, neweqs, total_sub, var end end +function add_solvable_variable!() + +end + +function add_solvable_equation!() + +end + """ Solve the solvable equations of the system and generate differential (or discrete) equations in terms of the selected states. @@ -492,21 +498,27 @@ such that the mass matrix is: 0 0]. Update the state to account for the new ordering and equations. + +####### DISCRETE CASE +- only substitute Shift(t, -2) """ -function solve_and_generate_equations!(state::TearingState, neweqs, total_sub, var_eq_matching; simplify = false) +function solve_and_generate_equations!(state::TearingState, neweqs, var_eq_matching; simplify = false, shift_sub = Dict()) @unpack fullvars, sys, structure = state @unpack solvable_graph, var_to_diff, eq_to_diff, graph, lowest_shift = structure eq_var_matching = invview(var_eq_matching) diff_to_var = invview(var_to_diff) + dx_sub = Dict() if ModelingToolkit.has_iv(sys) iv = get_iv(sys) if is_only_discrete(structure) D = Shift(iv, 1) lower_name = lower_varname_withshift + total_sub = shift_sub else D = Differential(iv) lower_name = lower_varname_with_unit + total_sub = dx_sub end else iv = D = nothing @@ -540,6 +552,7 @@ function solve_and_generate_equations!(state::TearingState, neweqs, total_sub, v # fullvars[iv] is a differential variable of the form D^n(x), and neweqs[ieq] # is solved to give the RHS. for ieq in eqs + println() iv = eq_var_matching[ieq] if is_solvable(ieq, iv) # We don't solve differential equations, but we will need to try to @@ -549,7 +562,9 @@ function solve_and_generate_equations!(state::TearingState, neweqs, total_sub, v isnothing(D) && error("Differential found in a non-differential system. Likely this is a bug in the construction of an initialization system. Please report this issue with a reproducible example. Offending equation: $(equations(sys)[ieq])") order, lv = var_order(diff_to_var, iv) - dx = D(fullvars[lv]) + @show fullvars[lv] + @show simplify_shifts(fullvars[lv]) + dx = D(simplify_shifts(fullvars[lv])) eq = dx ~ simplify_shifts(Symbolics.fixpoint_sub( Symbolics.symbolic_linear_solve(neweqs[ieq], fullvars[iv]), @@ -560,6 +575,9 @@ function solve_and_generate_equations!(state::TearingState, neweqs, total_sub, v end push!(diff_eqs, eq) total_sub[simplify_shifts(eq.lhs)] = eq.rhs + dx_sub[simplify_shifts(eq.lhs)] = eq.rhs + @show total_sub + @show eq push!(diffeq_idxs, ieq) push!(diff_vars, diff_to_var[iv]) continue @@ -575,10 +593,10 @@ function solve_and_generate_equations!(state::TearingState, neweqs, total_sub, v @warn "Tearing: solving $eq for $var is singular!" else rhs = -b / a - neweq = var ~ Symbolics.fixpoint_sub( + neweq = var ~ simplify_shifts(Symbolics.fixpoint_sub( simplify ? Symbolics.simplify(rhs) : rhs, - total_sub; operator = ModelingToolkit.Shift) + dx_sub; operator = ModelingToolkit.Shift)) push!(subeqs, neweq) push!(solved_equations, ieq) push!(solved_variables, iv) @@ -589,7 +607,7 @@ function solve_and_generate_equations!(state::TearingState, neweqs, total_sub, v if !(eq.lhs isa Number && eq.lhs == 0) rhs = eq.rhs - eq.lhs end - push!(alge_eqs, 0 ~ Symbolics.fixpoint_sub(rhs, total_sub)) + push!(alge_eqs, 0 ~ simplify_shifts(Symbolics.fixpoint_sub(rhs, total_sub))) push!(algeeq_idxs, ieq) end end @@ -676,16 +694,19 @@ function tearing_reassemble(state::TearingState, var_eq_matching, end neweqs = collect(equations(state)) diff_to_var = invview(var_to_diff) - total_sub = Dict() - dummy_sub = Dict() is_discrete = is_only_discrete(state.structure) + shift_sub = Dict() + # Structural simplification + dummy_sub = Dict() substitute_dummy_derivatives!(state, neweqs, dummy_sub, var_eq_matching) - generate_derivative_variables!(state, neweqs, total_sub, var_eq_matching; - is_discrete, mm) + + generate_derivative_variables!(state, neweqs, var_eq_matching; + is_discrete, mm, shift_sub) + new_fullvars, new_var_to_diff, new_eq_to_diff, neweqs, subeqs, graph = - solve_and_generate_equations!(state, neweqs, total_sub, var_eq_matching; simplify) + solve_and_generate_equations!(state, neweqs, var_eq_matching; simplify, shift_sub) # Update system var_to_diff = new_var_to_diff @@ -701,12 +722,7 @@ function tearing_reassemble(state::TearingState, var_eq_matching, i -> (!isempty(𝑑neighbors(graph, i)) || (var_to_diff[i] !== nothing && !isempty(𝑑neighbors(graph, var_to_diff[i])))) end - @show ispresent.(collect(1:length(fullvars))) - @show 𝑑neighbors(graph, 5) - @show var_to_diff[5] - @show neweqs - @show fullvars sys = state.sys obs_sub = dummy_sub for eq in neweqs diff --git a/src/structural_transformation/utils.jl b/src/structural_transformation/utils.jl index b2d67dc547..c202556906 100644 --- a/src/structural_transformation/utils.jl +++ b/src/structural_transformation/utils.jl @@ -449,10 +449,9 @@ end ### Misc ### -function lower_varname_withshift(var, iv, backshift; unshifted = nothing) - backshift < 0 && return Shift(iv, -backshift)(var) - backshift == 0 && return unshifted - ds = "$iv-$backshift" +function lower_varname_withshift(var, iv, backshift; unshifted = nothing, allow_zero = true) + backshift <= 0 && return Shift(iv, -backshift)(unshifted, allow_zero) + ds = backshift > 0 ? "$iv-$backshift" : "$iv+$(-backshift)" d_separator = 'ˍ' if ModelingToolkit.isoperator(var, ModelingToolkit.Shift) @@ -475,9 +474,15 @@ function isdoubleshift(var) ModelingToolkit.isoperator(arguments(var)[1], ModelingToolkit.Shift) end +### Rules +# 1. x(t) -> x(t) +# 2. Shift(t, 0)(x(t)) -> x(t) +# 3. Shift(t, 1)(x + z) -> Shift(t, 1)(x) + Shift(t, 1)(z) + function simplify_shifts(var) ModelingToolkit.hasshift(var) || return var var isa Equation && return simplify_shifts(var.lhs) ~ simplify_shifts(var.rhs) + ((op = operation(var)) isa Shift) && op.steps == 0 && return simplify_shifts(arguments(var)[1]) if isdoubleshift(var) op1 = operation(var) vv1 = arguments(var)[1] From 1c578c3535fe0d7af1d82d08285d90fd244ce2af Mon Sep 17 00:00:00 2001 From: vyudu Date: Fri, 7 Feb 2025 14:46:09 -0500 Subject: [PATCH 0787/2176] rename functions --- .../symbolics_tearing.jl | 37 +++++++----- src/structural_transformation/utils.jl | 60 ++++++++++++++++++- test/structural_transformation/utils.jl | 17 ++++++ 3 files changed, 98 insertions(+), 16 deletions(-) diff --git a/src/structural_transformation/symbolics_tearing.jl b/src/structural_transformation/symbolics_tearing.jl index 020c85b05f..778473907e 100644 --- a/src/structural_transformation/symbolics_tearing.jl +++ b/src/structural_transformation/symbolics_tearing.jl @@ -254,7 +254,9 @@ function substitute_dummy_derivatives!(ts::TearingState, neweqs, dummy_sub, var_ diff_to_var = invview(var_to_diff) iv = ModelingToolkit.has_iv(sys) ? ModelingToolkit.get_iv(sys) : nothing + @show neweqs for var in 1:length(fullvars) + #@show neweqs dv = var_to_diff[var] dv === nothing && continue if var_eq_matching[var] !== SelectedState() @@ -286,9 +288,7 @@ function substitute_dummy_derivatives!(ts::TearingState, neweqs, dummy_sub, var_ end end -""" -Generate new derivative variables for the system. - +#= There are three cases where we want to generate new variables to convert the system into first order (semi-implicit) ODEs. @@ -361,6 +361,16 @@ This is different from the continuous case where D(D(x)) can be substituted for by iteratively substituting x_t ~ D(x), then x_tt ~ D(x_t). For this reason the shift_sub dict is updated at the time that the renamed variables are written, inside the loop where new variables are generated. +=# +""" +Generate new derivative variables for the system. + +Effects on the state: +- fullvars: add the new derivative variables x_t +- neweqs: add the identity equations for the new variables, D(x) ~ x_t +- graph: update graph with the new equations and variables, and their connections +- solvable_graph: +- var_eq_matching: solvable equations """ function generate_derivative_variables!(ts::TearingState, neweqs, var_eq_matching; is_discrete = false, mm = nothing, shift_sub = nothing) @@ -406,7 +416,7 @@ function generate_derivative_variables!(ts::TearingState, neweqs, var_eq_matchin dx = fullvars[dv] order, lv = var_order(diff_to_var, dv) @show fullvars[uv] - x_t = lower_name(fullvars[lv], iv, -low-order-1; unshifted = fullvars[uv]) + x_t = lower_name(fullvars[lv], iv, -low-order-1; unshifted = fullvars[uv], allow_zero = true) shift_sub[dx] = x_t (var_eq_matching[dv] isa Int) ? continue : @goto DISCRETE_VARIABLE end @@ -439,7 +449,7 @@ function generate_derivative_variables!(ts::TearingState, neweqs, var_eq_matchin x_t = lower_name(fullvars[lv], iv, order) @label DISCRETE_VARIABLE - push!(fullvars, x_t) + push!(fullvars, simplify_shifts(x_t)) v_t = length(fullvars) v_t_idx = add_vertex!(var_to_diff) add_vertex!(graph, DST) @@ -448,7 +458,6 @@ function generate_derivative_variables!(ts::TearingState, neweqs, var_eq_matchin add_vertex!(solvable_graph, DST) push!(var_eq_matching, unassigned) - # Add discrete substitutions to total_sub directly. is_discrete && begin idx_to_lowest_shift[v_t] = idx_to_lowest_shift[dv] for e in 𝑑neighbors(graph, dv) @@ -463,7 +472,7 @@ function generate_derivative_variables!(ts::TearingState, neweqs, var_eq_matchin end # add `D(x) - x_t ~ 0` - push!(neweqs, 0 ~ x_t - dx) + push!(neweqs, 0 ~ dx - x_t) add_vertex!(graph, SRC) dummy_eq = length(neweqs) add_edge!(graph, dummy_eq, dv) @@ -478,12 +487,11 @@ function generate_derivative_variables!(ts::TearingState, neweqs, var_eq_matchin end end -function add_solvable_variable!() +function add_solvable_variable!(state::TearingState) end -function add_solvable_equation!() - +function add_solvable_equation!(s::SystemStructure, neweqs, eq) end """ @@ -500,7 +508,8 @@ such that the mass matrix is: Update the state to account for the new ordering and equations. ####### DISCRETE CASE -- only substitute Shift(t, -2) +- Differential equations: substitute variables with everything shifted forward one timestep. +- Algebraic and observable equations: substitute variables with everything shifted back one timestep. """ function solve_and_generate_equations!(state::TearingState, neweqs, var_eq_matching; simplify = false, shift_sub = Dict()) @unpack fullvars, sys, structure = state @@ -562,8 +571,6 @@ function solve_and_generate_equations!(state::TearingState, neweqs, var_eq_match isnothing(D) && error("Differential found in a non-differential system. Likely this is a bug in the construction of an initialization system. Please report this issue with a reproducible example. Offending equation: $(equations(sys)[ieq])") order, lv = var_order(diff_to_var, iv) - @show fullvars[lv] - @show simplify_shifts(fullvars[lv]) dx = D(simplify_shifts(fullvars[lv])) eq = dx ~ simplify_shifts(Symbolics.fixpoint_sub( Symbolics.symbolic_linear_solve(neweqs[ieq], @@ -576,8 +583,6 @@ function solve_and_generate_equations!(state::TearingState, neweqs, var_eq_match push!(diff_eqs, eq) total_sub[simplify_shifts(eq.lhs)] = eq.rhs dx_sub[simplify_shifts(eq.lhs)] = eq.rhs - @show total_sub - @show eq push!(diffeq_idxs, ieq) push!(diff_vars, diff_to_var[iv]) continue @@ -611,6 +616,8 @@ function solve_and_generate_equations!(state::TearingState, neweqs, var_eq_match push!(algeeq_idxs, ieq) end end + @show neweqs + @show subeqs # TODO: BLT sorting neweqs = [diff_eqs; alge_eqs] diff --git a/src/structural_transformation/utils.jl b/src/structural_transformation/utils.jl index c202556906..b947fa4269 100644 --- a/src/structural_transformation/utils.jl +++ b/src/structural_transformation/utils.jl @@ -271,6 +271,8 @@ end function find_solvables!(state::TearingState; kwargs...) @assert state.structure.solvable_graph === nothing + println("in find_solvables") + @show eqs eqs = equations(state) graph = state.structure.graph state.structure.solvable_graph = BipartiteGraph(nsrcs(graph), ndsts(graph)) @@ -278,6 +280,7 @@ function find_solvables!(state::TearingState; kwargs...) for ieq in 1:length(eqs) find_eq_solvables!(state, ieq, to_rm; kwargs...) end + @show eqs return nothing end @@ -477,7 +480,7 @@ end ### Rules # 1. x(t) -> x(t) # 2. Shift(t, 0)(x(t)) -> x(t) -# 3. Shift(t, 1)(x + z) -> Shift(t, 1)(x) + Shift(t, 1)(z) +# 3. Shift(t, 3)(Shift(t, 2)(x(t)) -> Shift(t, 5)(x(t)) function simplify_shifts(var) ModelingToolkit.hasshift(var) || return var @@ -498,3 +501,58 @@ function simplify_shifts(var) unwrap(var).metadata) end end + +""" +Power expand the shifts. Used for substitution. + +Shift(t, -3)(x(t)) -> Shift(t, -1)(Shift(t, -1)(Shift(t, -1)(x))) +""" +function expand_shifts(var) + ModelingToolkit.hasshift(var) || return var + var = ModelingToolkit.value(var) + + var isa Equation && return expand_shifts(var.lhs) ~ expand_shifts(var.rhs) + op = operation(var) + s = sign(op.steps) + arg = only(arguments(var)) + + if ModelingToolkit.isvariable(arg) && (ModelingToolkit.getvariabletype(arg) === VARIABLE) && isequal(op.t, only(arguments(arg))) + out = arg + for i in 1:op.steps + out = Shift(op.t, s)(out) + end + return out + elseif iscall(arg) + return maketerm(typeof(var), operation(var), expand_shifts.(arguments(var)), + unwrap(var).metadata) + else + return arg + end +end + +""" +Shift(t, 1)(x + z) -> Shift(t, 1)(x) + Shift(t, 1)(z) +""" +function distribute_shift(var) + ModelingToolkit.hasshift(var) || return var + var isa Equation && return distribute_shift(var.lhs) ~ distribute_shift(var.rhs) + shift = operation(var) + expr = only(arguments(var)) + _distribute_shift(expr, shift) +end + +function _distribute_shift(expr, shift) + op = operation(expr) + args = arguments(expr) + + if length(args) == 1 + if ModelingToolkit.isvariable(only(args)) && isequal(op.t, only(args)) + return shift(only(args)) + else + return only(args) + end + else iscall(op) + return maketerm(typeof(expr), operation(expr), _distribute_shift.(args, shift), + unwrap(var).metadata) + end +end diff --git a/test/structural_transformation/utils.jl b/test/structural_transformation/utils.jl index 621aa8b8a8..98d986ebd4 100644 --- a/test/structural_transformation/utils.jl +++ b/test/structural_transformation/utils.jl @@ -4,6 +4,7 @@ using Graphs using SparseArrays using UnPack using ModelingToolkit: t_nounits as t, D_nounits as D +const ST = StructuralTransformations # Define some variables @parameters L g @@ -161,3 +162,19 @@ end structural_simplify(sys; additional_passes = [pass]) @test value[] == 1 end + +@testset "Shift simplification" begin + @variables x(t) y(t) z(t) + @parameters a b c + + # Expand shifts + @test isequal(ST.expand_shifts(Shift(t, -3)(x)), Shift(t, -1)(Shift(t, -1)(Shift(t, -1)(x)))) + expr = a * Shift(t, -2)(x) + Shift(t, 2)(y) + b + @test isequal(ST.expand_shifts(expr), + a * Shift(t, -1)(Shift(t, -1)(x)) + Shift(t, 1)(Shift(t, 1)(y)) + b) + @test isequal(ST.expand_shifts(Shift(t, 2)(Shift(t, 1)(a))), a) + + + # Distribute shifts + +end From 5f8cda3a59aa3333c9f1144562ff7642f82bd4b8 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 3 Feb 2025 16:27:12 +0530 Subject: [PATCH 0788/2176] feat: add `assertions` field to `ODESystem` --- src/systems/abstractsystem.jl | 24 ++++++++++++++++++++++++ src/systems/diffeqs/odesystem.jl | 14 +++++++++++--- 2 files changed, 35 insertions(+), 3 deletions(-) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index dc47457825..ad46e4529e 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -983,6 +983,7 @@ for prop in [:eqs :gui_metadata :discrete_subsystems :parameter_dependencies + :assertions :solved_unknowns :split_idxs :parent @@ -1468,6 +1469,24 @@ end """ $(TYPEDSIGNATURES) +Get the assertions for a system `sys` and its subsystems. +""" +function assertions(sys::AbstractSystem) + has_assertions(sys) || return Dict{BasicSymbolic, String}() + + asserts = get_assertions(sys) + systems = get_systems(sys) + namespaced_asserts = mapreduce( + merge!, systems; init = Dict{BasicSymbolic, String}()) do subsys + Dict{BasicSymbolic, String}(namespace_expr(k, subsys) => v + for (k, v) in assertions(subsys)) + end + return merge(asserts, namespaced_asserts) +end + +""" +$(TYPEDSIGNATURES) + Get the guesses for variables in the initialization system of the system `sys` and its subsystems. See also [`initialization_equations`](@ref) and [`ModelingToolkit.get_guesses`](@ref). @@ -3036,6 +3055,11 @@ function extend(sys::AbstractSystem, basesys::AbstractSystem; kwargs = merge(kwargs, (initialization_eqs = ieqs, guesses = guesses)) end + if has_assertions(basesys) + kwargs = merge( + kwargs, (; assertions = merge(get_assertions(basesys), get_assertions(sys)))) + end + return T(args...; kwargs...) end diff --git a/src/systems/diffeqs/odesystem.jl b/src/systems/diffeqs/odesystem.jl index c735b52c37..e32b43bd2a 100644 --- a/src/systems/diffeqs/odesystem.jl +++ b/src/systems/diffeqs/odesystem.jl @@ -137,6 +137,11 @@ struct ODESystem <: AbstractODESystem """ parameter_dependencies::Vector{Equation} """ + Mapping of conditions which should be true throughout the solve to corresponding error + messages. These will be added to the equations when calling `debug_system`. + """ + assertions::Dict{BasicSymbolic, String} + """ Metadata for the system, to be used by downstream packages. """ metadata::Any @@ -190,7 +195,7 @@ struct ODESystem <: AbstractODESystem jac, ctrl_jac, Wfact, Wfact_t, name, description, systems, defaults, guesses, torn_matching, initializesystem, initialization_eqs, schedule, connector_type, preface, cevents, - devents, parameter_dependencies, + devents, parameter_dependencies, assertions = Dict{BasicSymbolic, String}(), metadata = nothing, gui_metadata = nothing, is_dde = false, tstops = [], tearing_state = nothing, substitutions = nothing, complete = false, index_cache = nothing, @@ -210,7 +215,7 @@ struct ODESystem <: AbstractODESystem new(tag, deqs, iv, dvs, ps, tspan, var_to_name, ctrls, observed, tgrad, jac, ctrl_jac, Wfact, Wfact_t, name, description, systems, defaults, guesses, torn_matching, initializesystem, initialization_eqs, schedule, connector_type, preface, - cevents, devents, parameter_dependencies, metadata, + cevents, devents, parameter_dependencies, assertions, metadata, gui_metadata, is_dde, tstops, tearing_state, substitutions, complete, index_cache, discrete_subsystems, solved_unknowns, split_idxs, parent) end @@ -235,6 +240,7 @@ function ODESystem(deqs::AbstractVector{<:Equation}, iv, dvs, ps; continuous_events = nothing, discrete_events = nothing, parameter_dependencies = Equation[], + assertions = Dict(), checks = true, metadata = nothing, gui_metadata = nothing, @@ -286,12 +292,13 @@ function ODESystem(deqs::AbstractVector{<:Equation}, iv, dvs, ps; if is_dde === nothing is_dde = _check_if_dde(deqs, iv′, systems) end + assertions = Dict{BasicSymbolic, Any}(unwrap(k) => v for (k, v) in assertions) ODESystem(Threads.atomic_add!(SYSTEM_COUNT, UInt(1)), deqs, iv′, dvs′, ps′, tspan, var_to_name, ctrl′, observed, tgrad, jac, ctrl_jac, Wfact, Wfact_t, name, description, systems, defaults, guesses, nothing, initializesystem, initialization_eqs, schedule, connector_type, preface, cont_callbacks, - disc_callbacks, parameter_dependencies, + disc_callbacks, parameter_dependencies, assertions, metadata, gui_metadata, is_dde, tstops, checks = checks) end @@ -364,6 +371,7 @@ function flatten(sys::ODESystem, noeqs = false) name = nameof(sys), description = description(sys), initialization_eqs = initialization_equations(sys), + assertions = assertions(sys), is_dde = is_dde(sys), tstops = symbolic_tstops(sys), metadata = get_metadata(sys), From aa6f3402a5050f3d4c92eea45de8fbacdc98cc96 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 3 Feb 2025 16:27:39 +0530 Subject: [PATCH 0789/2176] feat: add assertions support to `debug_system` --- src/debugging.jl | 18 ++++++++++++++++++ src/systems/abstractsystem.jl | 10 +++++++++- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/src/debugging.jl b/src/debugging.jl index a1a168d8dd..b30a1ac2ac 100644 --- a/src/debugging.jl +++ b/src/debugging.jl @@ -42,3 +42,21 @@ function debug_sub(ex, funcs; kw...) f in funcs ? logged_fun(f, args...; kw...) : maketerm(typeof(ex), f, args, metadata(ex)) end + +function _debug_assertion(expr::Bool, message::String, log::Bool) + expr && return 0.0 + log && @error message + return NaN +end + +@register_symbolic _debug_assertion(expr::Bool, message::String, log::Bool) + +const ASSERTION_LOG_VARIABLE = only(@parameters __log_assertions_ₘₜₖ::Bool = false) + +function get_assertions_expr(assertions::Dict{BasicSymbolic, String}) + term = 0 + for (k, v) in assertions + term += _debug_assertion(k, "Assertion $k failed:\n$v", ASSERTION_LOG_VARIABLE) + end + return term +end diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index ad46e4529e..80b27e4e84 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -2312,7 +2312,15 @@ function debug_system( error("debug_system(sys) only works on systems with no sub-systems! Consider flattening it with flatten(sys) or structural_simplify(sys) first.") end if has_eqs(sys) - @set! sys.eqs = debug_sub.(equations(sys), Ref(functions); kw...) + eqs = debug_sub.(equations(sys), Ref(functions); kw...) + expr = get_assertions_expr(assertions(sys)) + eqs[end] = eqs[end].lhs ~ eqs[end].rhs + expr + @set! sys.eqs = eqs + @set! sys.ps = unique!([get_ps(sys); ASSERTION_LOG_VARIABLE]) + @set! sys.defaults = merge(get_defaults(sys), Dict(ASSERTION_LOG_VARIABLE => true)) + if iscomplete(sys) + sys = complete(sys; split = is_split(sys)) + end end if has_observed(sys) @set! sys.observed = debug_sub.(observed(sys), Ref(functions); kw...) From b5e6dd93f57c5e9bc51dfb1278c0411d8425724c Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 3 Feb 2025 17:13:22 +0530 Subject: [PATCH 0790/2176] feat: add `assertions` field to `SDESystem` --- src/systems/diffeqs/sdesystem.jl | 19 ++++++++++++++----- src/systems/systems.jl | 2 +- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/src/systems/diffeqs/sdesystem.jl b/src/systems/diffeqs/sdesystem.jl index 212e20d743..8b7bc8b0d8 100644 --- a/src/systems/diffeqs/sdesystem.jl +++ b/src/systems/diffeqs/sdesystem.jl @@ -126,6 +126,11 @@ struct SDESystem <: AbstractODESystem """ parameter_dependencies::Vector{Equation} """ + Mapping of conditions which should be true throughout the solve to corresponding error + messages. These will be added to the equations when calling `debug_system`. + """ + assertions::Dict{BasicSymbolic, String} + """ Metadata for the system, to be used by downstream packages. """ metadata::Any @@ -159,7 +164,9 @@ struct SDESystem <: AbstractODESystem function SDESystem(tag, deqs, neqs, iv, dvs, ps, tspan, var_to_name, ctrls, observed, tgrad, jac, ctrl_jac, Wfact, Wfact_t, name, description, systems, defaults, guesses, initializesystem, initialization_eqs, connector_type, - cevents, devents, parameter_dependencies, metadata = nothing, gui_metadata = nothing, + cevents, devents, parameter_dependencies, assertions = Dict{ + BasicSymbolic, Nothing}, + metadata = nothing, gui_metadata = nothing, complete = false, index_cache = nothing, parent = nothing, is_scalar_noise = false, is_dde = false, isscheduled = false; @@ -185,9 +192,8 @@ struct SDESystem <: AbstractODESystem new(tag, deqs, neqs, iv, dvs, ps, tspan, var_to_name, ctrls, observed, tgrad, jac, ctrl_jac, Wfact, Wfact_t, name, description, systems, defaults, guesses, initializesystem, initialization_eqs, connector_type, cevents, - devents, - parameter_dependencies, metadata, gui_metadata, complete, index_cache, parent, is_scalar_noise, - is_dde, isscheduled) + devents, parameter_dependencies, assertions, metadata, gui_metadata, complete, + index_cache, parent, is_scalar_noise, is_dde, isscheduled) end end @@ -209,6 +215,7 @@ function SDESystem(deqs::AbstractVector{<:Equation}, neqs::AbstractArray, iv, dv continuous_events = nothing, discrete_events = nothing, parameter_dependencies = Equation[], + assertions = Dict{BasicSymbolic, String}(), metadata = nothing, gui_metadata = nothing, complete = false, @@ -261,11 +268,12 @@ function SDESystem(deqs::AbstractVector{<:Equation}, neqs::AbstractArray, iv, dv if is_dde === nothing is_dde = _check_if_dde(deqs, iv′, systems) end + assertions = Dict{BasicSymbolic, Any}(unwrap(k) => v for (k, v) in assertions) SDESystem(Threads.atomic_add!(SYSTEM_COUNT, UInt(1)), deqs, neqs, iv′, dvs′, ps′, tspan, var_to_name, ctrl′, observed, tgrad, jac, ctrl_jac, Wfact, Wfact_t, name, description, systems, defaults, guesses, initializesystem, initialization_eqs, connector_type, - cont_callbacks, disc_callbacks, parameter_dependencies, metadata, gui_metadata, + cont_callbacks, disc_callbacks, parameter_dependencies, assertions, metadata, gui_metadata, complete, index_cache, parent, is_scalar_noise, is_dde; checks = checks) end @@ -378,6 +386,7 @@ function ODESystem(sys::SDESystem) newsys = ODESystem(neweqs, get_iv(sys), unknowns(sys), parameters(sys); parameter_dependencies = parameter_dependencies(sys), defaults = defaults(sys), continuous_events = continuous_events(sys), discrete_events = discrete_events(sys), + assertions = assertions(sys), name = nameof(sys), description = description(sys), metadata = get_metadata(sys)) @set newsys.parent = sys end diff --git a/src/systems/systems.jl b/src/systems/systems.jl index 9c8c272c5c..f8630f2d20 100644 --- a/src/systems/systems.jl +++ b/src/systems/systems.jl @@ -165,7 +165,7 @@ function __structural_simplify(sys::AbstractSystem, io = nothing; simplify = fal return SDESystem(Vector{Equation}(full_equations(ode_sys)), noise_eqs, get_iv(ode_sys), unknowns(ode_sys), parameters(ode_sys); name = nameof(ode_sys), is_scalar_noise, observed = observed(ode_sys), defaults = defaults(sys), - parameter_dependencies = parameter_dependencies(sys), + parameter_dependencies = parameter_dependencies(sys), assertions = assertions(sys), guesses = guesses(sys), initialization_eqs = initialization_equations(sys)) end end From 8e23967fd7c7e18a6d621252672f58753a07c06b Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 3 Feb 2025 17:13:31 +0530 Subject: [PATCH 0791/2176] test: test new assertions functionality --- test/debugging.jl | 39 +++++++++++++++++++++++++++++++++++++++ test/runtests.jl | 1 + 2 files changed, 40 insertions(+) create mode 100644 test/debugging.jl diff --git a/test/debugging.jl b/test/debugging.jl new file mode 100644 index 0000000000..f127df6cf8 --- /dev/null +++ b/test/debugging.jl @@ -0,0 +1,39 @@ +using ModelingToolkit, OrdinaryDiffEq, StochasticDiffEq, SymbolicIndexingInterface +using ModelingToolkit: t_nounits as t, D_nounits as D, ASSERTION_LOG_VARIABLE + +@variables x(t) +@brownian a +@named inner_ode = ODESystem(D(x) ~ -sqrt(x), t; assertions = [(x > 0) => "ohno"]) +@named inner_sde = System([D(x) ~ -sqrt(x) + a], t; assertions = [(x > 0) => "ohno"]) +sys_ode = structural_simplify(inner_ode) +sys_sde = structural_simplify(inner_sde) + +@testset "`debug_system` adds assertions" begin + @testset "$(typeof(sys))" for (Problem, sys, alg) in [ + (ODEProblem, sys_ode, Tsit5()), (SDEProblem, sys_sde, ImplicitEM())] + dsys = debug_system(sys; functions = []) + @test is_parameter(dsys, ASSERTION_LOG_VARIABLE) + prob = Problem(dsys, [x => 1.0], (0.0, 5.0)) + sol = solve(prob, alg) + @test !SciMLBase.successful_retcode(sol) + prob.ps[ASSERTION_LOG_VARIABLE] = true + sol = @test_logs (:error, r"ohno") match_mode=:any solve(prob, alg) + @test !SciMLBase.successful_retcode(sol) + end +end + +@testset "Hierarchical system" begin + @testset "$(typeof(inner))" for (ctor, Problem, inner, alg) in [ + (ODESystem, ODEProblem, inner_ode, Tsit5()), + (System, SDEProblem, inner_sde, ImplicitEM())] + @mtkbuild outer = ctor(Equation[], t; systems = [inner]) + dsys = debug_system(outer; functions = []) + @test is_parameter(dsys, ASSERTION_LOG_VARIABLE) + prob = Problem(dsys, [inner.x => 1.0], (0.0, 5.0)) + sol = solve(prob, alg) + @test !SciMLBase.successful_retcode(sol) + prob.ps[ASSERTION_LOG_VARIABLE] = true + sol = @test_logs (:error, r"ohno") match_mode=:any solve(prob, alg) + @test !SciMLBase.successful_retcode(sol) + end +end diff --git a/test/runtests.jl b/test/runtests.jl index 52875fdae5..9537b1b44e 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -92,6 +92,7 @@ end @safetestset "IfLifting Test" include("if_lifting.jl") @safetestset "Analysis Points Test" include("analysis_points.jl") @safetestset "Causal Variables Connection Test" include("causal_variables_connection.jl") + @safetestset "Debugging Test" include("debugging.jl") end end From 5d2e8ede6355369b7f064864eb83852fcb5aec2c Mon Sep 17 00:00:00 2001 From: Christopher Rackauckas Date: Mon, 3 Feb 2025 04:37:21 -0800 Subject: [PATCH 0792/2176] Update src/systems/diffeqs/odesystem.jl Co-authored-by: Fredrik Bagge Carlson --- src/systems/diffeqs/odesystem.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/systems/diffeqs/odesystem.jl b/src/systems/diffeqs/odesystem.jl index e32b43bd2a..96fd8534ae 100644 --- a/src/systems/diffeqs/odesystem.jl +++ b/src/systems/diffeqs/odesystem.jl @@ -137,7 +137,7 @@ struct ODESystem <: AbstractODESystem """ parameter_dependencies::Vector{Equation} """ - Mapping of conditions which should be true throughout the solve to corresponding error + Mapping of conditions which should be true throughout the solution process to corresponding error messages. These will be added to the equations when calling `debug_system`. """ assertions::Dict{BasicSymbolic, String} From f9b8f52ee8254400849e435f65ed64eea09c29a4 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 3 Feb 2025 18:09:03 +0530 Subject: [PATCH 0793/2176] Update src/systems/diffeqs/sdesystem.jl Co-authored-by: Fredrik Bagge Carlson --- src/systems/diffeqs/sdesystem.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/systems/diffeqs/sdesystem.jl b/src/systems/diffeqs/sdesystem.jl index 8b7bc8b0d8..92a8ddd710 100644 --- a/src/systems/diffeqs/sdesystem.jl +++ b/src/systems/diffeqs/sdesystem.jl @@ -126,7 +126,7 @@ struct SDESystem <: AbstractODESystem """ parameter_dependencies::Vector{Equation} """ - Mapping of conditions which should be true throughout the solve to corresponding error + Mapping of conditions which should be true throughout the solution process to corresponding error messages. These will be added to the equations when calling `debug_system`. """ assertions::Dict{BasicSymbolic, String} From 41a43fdf506ce291241a9e0077a68570c662cf57 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 3 Feb 2025 18:19:22 +0530 Subject: [PATCH 0794/2176] docs: add documentation for assertions functionality --- docs/src/basics/Debugging.md | 17 +++++++++++++++++ src/debugging.jl | 19 ++++++++++++++++++- src/systems/abstractsystem.jl | 8 ++++++++ 3 files changed, 43 insertions(+), 1 deletion(-) diff --git a/docs/src/basics/Debugging.md b/docs/src/basics/Debugging.md index d5c51ec0c1..b2524832d9 100644 --- a/docs/src/basics/Debugging.md +++ b/docs/src/basics/Debugging.md @@ -35,6 +35,23 @@ dsol = solve(dprob, Tsit5()); Now we see that it crashed because `u1` decreased so much that it became negative and outside the domain of the `√` function. We could have figured that out ourselves, but it is not always so obvious for more complex models. +Suppose we also want to validate that `u1 + u2 >= 2.0`. We can do this via the assertions functionality. + +```@example debug +@mtkbuild sys = ODESystem(eqs, t; defaults, assertions = [(u1 + u2 >= 2.0) => "Oh no!"]) +``` + +The assertions must be an iterable of pairs, where the first element is the symbolic condition and +the second is the message to be logged when the condition fails. + +```@repl debug +dsys = debug_system(sys; functions = []); +dprob = ODEProblem(dsys, [], (0.0, 10.0)); +dsol = solve(dprob, Tsit5()); +``` + +Note the messages containing the failed assertion and corresponding message. + ```@docs debug_system ``` diff --git a/src/debugging.jl b/src/debugging.jl index b30a1ac2ac..266819d60b 100644 --- a/src/debugging.jl +++ b/src/debugging.jl @@ -43,6 +43,13 @@ function debug_sub(ex, funcs; kw...) maketerm(typeof(ex), f, args, metadata(ex)) end +""" + $(TYPEDSIGNATURES) + +A function which takes a condition `expr` and returns `NaN` if it is false, +and zero if it is true. In case the condition is false and `log == true`, +`message` will be logged as an `@error`. +""" function _debug_assertion(expr::Bool, message::String, log::Bool) expr && return 0.0 log && @error message @@ -51,9 +58,19 @@ end @register_symbolic _debug_assertion(expr::Bool, message::String, log::Bool) +""" +Boolean parameter added to models returned from `debug_system` to control logging of +assertions. +""" const ASSERTION_LOG_VARIABLE = only(@parameters __log_assertions_ₘₜₖ::Bool = false) -function get_assertions_expr(assertions::Dict{BasicSymbolic, String}) +""" + $(TYPEDSIGNATURES) + +Get a symbolic expression as per the requirement of `debug_system` for all the assertions +in `assertions`. `is_split` denotes whether the corresponding system is a split system. +""" +function get_assertions_expr(assertions::Dict{BasicSymbolic, String}, is_split::Bool) term = 0 for (k, v) in assertions term += _debug_assertion(k, "Assertion $k failed:\n$v", ASSERTION_LOG_VARIABLE) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index 80b27e4e84..7857cc4c74 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -2302,6 +2302,14 @@ ERROR: Function /(1, sin(P(t))) output non-finite value Inf with input 1 => 1 sin(P(t)) => 0.0 ``` + +Additionally, all assertions in the system are validated in the equations. If any of +the conditions are false, the right hand side of at least one of the equations of +the system will evaluate to `NaN`. A new parameter is also added to the system which +controls whether the message associated with each assertion will be logged when the +assertion fails. This parameter defaults to `true` and can be toggled by +symbolic indexing with `ModelingToolkit.ASSERTION_LOG_VARIABLE`. For example, +`prob.ps[ModelingToolkit.ASSERTION_LOG_VARIABLE] = false` will disable logging. """ function debug_system( sys::AbstractSystem; functions = [log, sqrt, (^), /, inv, asin, acos], kw...) From b968b9244d2f9d4d99f7ed304f64a76eabcda171 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 10 Feb 2025 13:21:34 +0530 Subject: [PATCH 0795/2176] feat: add assertions to function regardless of `debug_system` add logging in `debug_system` --- src/debugging.jl | 35 +++++++++++++++++++----- src/systems/abstractsystem.jl | 19 ++++++------- src/systems/diffeqs/abstractodesystem.jl | 4 +++ test/debugging.jl | 17 ++++++++++-- 4 files changed, 54 insertions(+), 21 deletions(-) diff --git a/src/debugging.jl b/src/debugging.jl index 266819d60b..c16b47c2e3 100644 --- a/src/debugging.jl +++ b/src/debugging.jl @@ -43,6 +43,17 @@ function debug_sub(ex, funcs; kw...) maketerm(typeof(ex), f, args, metadata(ex)) end +""" + $(TYPEDSIGNATURES) + +A function which returns `NaN` if `condition` fails, and `0.0` otherwise. +""" +function _nan_condition(condition::Bool) + condition ? 0.0 : NaN +end + +@register_symbolic _nan_condition(condition::Bool) + """ $(TYPEDSIGNATURES) @@ -51,9 +62,10 @@ and zero if it is true. In case the condition is false and `log == true`, `message` will be logged as an `@error`. """ function _debug_assertion(expr::Bool, message::String, log::Bool) - expr && return 0.0 + value = _nan_condition(expr) + isnan(value) || return value log && @error message - return NaN + return value end @register_symbolic _debug_assertion(expr::Bool, message::String, log::Bool) @@ -67,13 +79,22 @@ const ASSERTION_LOG_VARIABLE = only(@parameters __log_assertions_ₘₜₖ::Bool """ $(TYPEDSIGNATURES) -Get a symbolic expression as per the requirement of `debug_system` for all the assertions -in `assertions`. `is_split` denotes whether the corresponding system is a split system. +Get a symbolic expression for all the assertions in `sys`. The expression returns `NaN` +if any of the assertions fail, and `0.0` otherwise. If `ASSERTION_LOG_VARIABLE` is a +parameter in the system, it will control whether the message associated with each +assertion is logged when it fails. """ -function get_assertions_expr(assertions::Dict{BasicSymbolic, String}, is_split::Bool) +function get_assertions_expr(sys::AbstractSystem) + asserts = assertions(sys) term = 0 - for (k, v) in assertions - term += _debug_assertion(k, "Assertion $k failed:\n$v", ASSERTION_LOG_VARIABLE) + if is_parameter(sys, ASSERTION_LOG_VARIABLE) + for (k, v) in asserts + term += _debug_assertion(k, "Assertion $k failed:\n$v", ASSERTION_LOG_VARIABLE) + end + else + for (k, v) in asserts + term += _nan_condition(k) + end end return term end diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index 7857cc4c74..8213b8f241 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -2303,12 +2303,11 @@ ERROR: Function /(1, sin(P(t))) output non-finite value Inf with input sin(P(t)) => 0.0 ``` -Additionally, all assertions in the system are validated in the equations. If any of -the conditions are false, the right hand side of at least one of the equations of -the system will evaluate to `NaN`. A new parameter is also added to the system which -controls whether the message associated with each assertion will be logged when the -assertion fails. This parameter defaults to `true` and can be toggled by -symbolic indexing with `ModelingToolkit.ASSERTION_LOG_VARIABLE`. For example, +Additionally, all assertions in the system are optionally logged when they fail. +A new parameter is also added to the system which controls whether the message associated +with each assertion will be logged when the assertion fails. This parameter defaults to +`true` and can be toggled by symbolic indexing with +`ModelingToolkit.ASSERTION_LOG_VARIABLE`. For example, `prob.ps[ModelingToolkit.ASSERTION_LOG_VARIABLE] = false` will disable logging. """ function debug_system( @@ -2321,18 +2320,16 @@ function debug_system( end if has_eqs(sys) eqs = debug_sub.(equations(sys), Ref(functions); kw...) - expr = get_assertions_expr(assertions(sys)) - eqs[end] = eqs[end].lhs ~ eqs[end].rhs + expr @set! sys.eqs = eqs @set! sys.ps = unique!([get_ps(sys); ASSERTION_LOG_VARIABLE]) @set! sys.defaults = merge(get_defaults(sys), Dict(ASSERTION_LOG_VARIABLE => true)) - if iscomplete(sys) - sys = complete(sys; split = is_split(sys)) - end end if has_observed(sys) @set! sys.observed = debug_sub.(observed(sys), Ref(functions); kw...) end + if iscomplete(sys) + sys = complete(sys; split = is_split(sys)) + end return sys end diff --git a/src/systems/diffeqs/abstractodesystem.jl b/src/systems/diffeqs/abstractodesystem.jl index 8d0720a1d2..ea55dce388 100644 --- a/src/systems/diffeqs/abstractodesystem.jl +++ b/src/systems/diffeqs/abstractodesystem.jl @@ -168,6 +168,10 @@ function generate_function(sys::AbstractODESystem, dvs = unknowns(sys), rhss = implicit_dae ? [_iszero(eq.lhs) ? eq.rhs : eq.rhs - eq.lhs for eq in eqs] : [eq.rhs for eq in eqs] + if !isempty(assertions(sys)) + rhss[end] += unwrap(get_assertions_expr(sys)) + end + # TODO: add an optional check on the ordering of observed equations u = dvs p = reorder_parameters(sys, ps) diff --git a/test/debugging.jl b/test/debugging.jl index f127df6cf8..aad36eb995 100644 --- a/test/debugging.jl +++ b/test/debugging.jl @@ -8,12 +8,23 @@ using ModelingToolkit: t_nounits as t, D_nounits as D, ASSERTION_LOG_VARIABLE sys_ode = structural_simplify(inner_ode) sys_sde = structural_simplify(inner_sde) -@testset "`debug_system` adds assertions" begin +@testset "assertions are present in generated `f`" begin + @testset "$(typeof(sys))" for (Problem, sys, alg) in [ + (ODEProblem, sys_ode, Tsit5()), (SDEProblem, sys_sde, ImplicitEM())] + @test !is_parameter(sys, ASSERTION_LOG_VARIABLE) + prob = Problem(sys, [x => 0.1], (0.0, 5.0)) + sol = solve(prob, alg) + @test !SciMLBase.successful_retcode(sol) + @test isnan(prob.f.f([0.0], prob.p, sol.t[end])[1]) + end +end + +@testset "`debug_system` adds logging" begin @testset "$(typeof(sys))" for (Problem, sys, alg) in [ (ODEProblem, sys_ode, Tsit5()), (SDEProblem, sys_sde, ImplicitEM())] dsys = debug_system(sys; functions = []) @test is_parameter(dsys, ASSERTION_LOG_VARIABLE) - prob = Problem(dsys, [x => 1.0], (0.0, 5.0)) + prob = Problem(dsys, [x => 0.1], (0.0, 5.0)) sol = solve(prob, alg) @test !SciMLBase.successful_retcode(sol) prob.ps[ASSERTION_LOG_VARIABLE] = true @@ -29,7 +40,7 @@ end @mtkbuild outer = ctor(Equation[], t; systems = [inner]) dsys = debug_system(outer; functions = []) @test is_parameter(dsys, ASSERTION_LOG_VARIABLE) - prob = Problem(dsys, [inner.x => 1.0], (0.0, 5.0)) + prob = Problem(dsys, [inner.x => 0.1], (0.0, 5.0)) sol = solve(prob, alg) @test !SciMLBase.successful_retcode(sol) prob.ps[ASSERTION_LOG_VARIABLE] = true From 6493175547aef97c06172cdd58288350acae9ab6 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 10 Feb 2025 14:48:09 +0530 Subject: [PATCH 0796/2176] test: update reference tests --- test/latexify/20.tex | 2 +- test/latexify/30.tex | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/test/latexify/20.tex b/test/latexify/20.tex index 0af631162c..ce6f5067c2 100644 --- a/test/latexify/20.tex +++ b/test/latexify/20.tex @@ -1,5 +1,5 @@ \begin{align} \frac{\mathrm{d} u\left( t \right)_{1}}{\mathrm{d}t} &= p_{3} \left( - u\left( t \right)_{1} + u\left( t \right)_{2} \right) \\ 0 &= - u\left( t \right)_{2} + \frac{1}{10} \left( p_{1} - u\left( t \right)_{1} \right) p_{2} p_{3} u\left( t \right)_{1} \\ -\frac{\mathrm{d} u\left( t \right)_{3}}{\mathrm{d}t} &= u\left( t \right)_{2}^{\frac{2}{3}} u\left( t \right)_{1} - p_{3} u\left( t \right)_{3} +\frac{\mathrm{d} u\left( t \right)_{3}}{\mathrm{d}t} &= \left( u\left( t \right)_{2} \right)^{\frac{2}{3}} u\left( t \right)_{1} - p_{3} u\left( t \right)_{3} \end{align} diff --git a/test/latexify/30.tex b/test/latexify/30.tex index 5cd2394374..d206c48d47 100644 --- a/test/latexify/30.tex +++ b/test/latexify/30.tex @@ -1,5 +1,5 @@ \begin{align} \frac{\mathrm{d} u\left( t \right)_{1}}{\mathrm{d}t} &= p_{3} \left( - u\left( t \right)_{1} + u\left( t \right)_{2} \right) \\ \frac{\mathrm{d} u\left( t \right)_{2}}{\mathrm{d}t} &= - u\left( t \right)_{2} + \frac{1}{10} \left( p_{1} - u\left( t \right)_{1} \right) p_{2} p_{3} u\left( t \right)_{1} \\ -\frac{\mathrm{d} u\left( t \right)_{3}}{\mathrm{d}t} &= u\left( t \right)_{2}^{\frac{2}{3}} u\left( t \right)_{1} - p_{3} u\left( t \right)_{3} +\frac{\mathrm{d} u\left( t \right)_{3}}{\mathrm{d}t} &= \left( u\left( t \right)_{2} \right)^{\frac{2}{3}} u\left( t \right)_{1} - p_{3} u\left( t \right)_{3} \end{align} From 3ca004e4b11e2513121c4e68f3d56ad6918effa2 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 10 Feb 2025 15:11:59 +0530 Subject: [PATCH 0797/2176] docs: update assertions documentation --- docs/src/basics/Debugging.md | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/docs/src/basics/Debugging.md b/docs/src/basics/Debugging.md index b2524832d9..b432810bd0 100644 --- a/docs/src/basics/Debugging.md +++ b/docs/src/basics/Debugging.md @@ -42,7 +42,17 @@ Suppose we also want to validate that `u1 + u2 >= 2.0`. We can do this via the a ``` The assertions must be an iterable of pairs, where the first element is the symbolic condition and -the second is the message to be logged when the condition fails. +the second is a message to be logged when the condition fails. All assertions are added to the +generated code and will cause the solver to reject steps that fail the assertions. For systems such +as the above where the assertion is guaranteed to eventually fail, the solver will likely exit +with a `dtmin` failure.. + +```@example debug +prob = ODEProblem(sys, [], (0.0, 10.0)) +sol = solve(prob, Tsit5()) +``` + +We can use `debug_system` to log the failing assertions in each call to the RHS function. ```@repl debug dsys = debug_system(sys; functions = []); @@ -50,7 +60,13 @@ dprob = ODEProblem(dsys, [], (0.0, 10.0)); dsol = solve(dprob, Tsit5()); ``` -Note the messages containing the failed assertion and corresponding message. +Note the logs containing the failed assertion and corresponding message. To temporarily disable +logging in a system returned from `debug_system`, use `ModelingToolkit.ASSERTION_LOG_VARIABLE`. + +```@repl debug +dprob[ModelingToolkit.ASSERTION_LOG_VARIABLE] = false; +solve(drob, Tsit5()); +``` ```@docs debug_system From 8fe1e957d5a1239bab24b219bc0cdab842659334 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 10 Feb 2025 16:42:16 +0530 Subject: [PATCH 0798/2176] docs: fix broken example blocks --- docs/src/basics/Variable_metadata.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/src/basics/Variable_metadata.md b/docs/src/basics/Variable_metadata.md index 44dfb30327..b2cc472f2f 100644 --- a/docs/src/basics/Variable_metadata.md +++ b/docs/src/basics/Variable_metadata.md @@ -183,7 +183,7 @@ A variable can be marked `irreducible` to prevent it from being moved to an it can be accessed in [callbacks](@ref events) ```@example metadata -@variable important_value [irreducible = true] +@variables important_value [irreducible = true] isirreducible(important_value) ``` @@ -192,7 +192,7 @@ isirreducible(important_value) When a model is structurally simplified, the algorithm will try to ensure that the variables with higher state priority become states of the system. A variable's state priority is a number set using the `state_priority` metadata. ```@example metadata -@variable important_dof [state_priority = 10] unimportant_dof [state_priority = -2] +@variables important_dof [state_priority = 10] unimportant_dof [state_priority = -2] state_priority(important_dof) ``` @@ -201,7 +201,7 @@ state_priority(important_dof) Units for variables can be designated using symbolic metadata. For more information, please see the [model validation and units](@ref units) section of the docs. Note that `getunit` is not equivalent to `get_unit` - the former is a metadata getter for individual variables (and is provided so the same interface function for `unit` exists like other metadata), while the latter is used to handle more general symbolic expressions. ```@example metadata -@variable speed [unit = u"m/s"] +@variables speed [unit = u"m/s"] hasunit(speed) ``` From 7cd399f82b0be3552789c441b8268769ccc4bb68 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 10 Feb 2025 18:13:54 +0530 Subject: [PATCH 0799/2176] feat: export `state_priority` --- src/ModelingToolkit.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ModelingToolkit.jl b/src/ModelingToolkit.jl index ed99fb2772..09b59c4ed6 100644 --- a/src/ModelingToolkit.jl +++ b/src/ModelingToolkit.jl @@ -249,7 +249,7 @@ export isinput, isoutput, getbounds, hasbounds, getguess, hasguess, isdisturbanc istunable, getdist, hasdist, tunable_parameters, isirreducible, getdescription, hasdescription, hasunit, getunit, hasconnect, getconnect, - hasmisc, getmisc + hasmisc, getmisc, state_priority export ode_order_lowering, dae_order_lowering, liouville_transform export PDESystem export Differential, expand_derivatives, @derivatives From 603c894044cd6589a0a15a7f8765369393ed9762 Mon Sep 17 00:00:00 2001 From: Christopher Rackauckas Date: Mon, 10 Feb 2025 06:10:32 -0800 Subject: [PATCH 0800/2176] Update src/systems/diffeqs/odesystem.jl --- src/systems/diffeqs/odesystem.jl | 1 - 1 file changed, 1 deletion(-) diff --git a/src/systems/diffeqs/odesystem.jl b/src/systems/diffeqs/odesystem.jl index 44cc7df46b..5b8041ba33 100644 --- a/src/systems/diffeqs/odesystem.jl +++ b/src/systems/diffeqs/odesystem.jl @@ -711,6 +711,5 @@ function process_constraint_system(constraints::Vector{Equation}, sts, ps, iv; c end end - @show constraints ConstraintsSystem(constraints, collect(constraintsts), collect(constraintps); name = consname) end From ed143a5d85d4b4660d478718fc9bcd9d79ec810c Mon Sep 17 00:00:00 2001 From: vyudu Date: Mon, 10 Feb 2025 15:18:05 -0500 Subject: [PATCH 0801/2176] refactor: refactor tearing_assemble into functions --- .../symbolics_tearing.jl | 503 ++++++++---------- src/structural_transformation/utils.jl | 71 +-- src/systems/systemstructure.jl | 6 +- 3 files changed, 239 insertions(+), 341 deletions(-) diff --git a/src/structural_transformation/symbolics_tearing.jl b/src/structural_transformation/symbolics_tearing.jl index 778473907e..b99e6ec21a 100644 --- a/src/structural_transformation/symbolics_tearing.jl +++ b/src/structural_transformation/symbolics_tearing.jl @@ -244,19 +244,17 @@ State selection may determine that some differential variables are algebraic variables in disguise. The derivative of such variables are called dummy derivatives. -`SelectedState` information is no longer needed past here. State selection -is done. All non-differentiated variables are algebraic variables, and all -variables that appear differentiated are differential variables. +`SelectedState` information is no longer needed after this function is called. +State selection is done. All non-differentiated variables are algebraic +variables, and all variables that appear differentiated are differential variables. """ -function substitute_dummy_derivatives!(ts::TearingState, neweqs, dummy_sub, var_eq_matching) +function substitute_derivatives_algevars!(ts::TearingState, neweqs, dummy_sub, var_eq_matching) @unpack fullvars, sys, structure = ts - @unpack solvable_graph, var_to_diff, eq_to_diff, graph, lowest_shift = structure + @unpack solvable_graph, var_to_diff, eq_to_diff, graph = structure diff_to_var = invview(var_to_diff) iv = ModelingToolkit.has_iv(sys) ? ModelingToolkit.get_iv(sys) : nothing - @show neweqs for var in 1:length(fullvars) - #@show neweqs dv = var_to_diff[var] dv === nothing && continue if var_eq_matching[var] !== SelectedState() @@ -339,199 +337,137 @@ variables and equations, don't add them when they already exist. Documenting the differences to structural simplification for discrete systems: -1. In discrete systems the lowest-order term is Shift(t, k)(x(t)), instead of x(t). We need to substitute the k-1 lowest order terms instead of the k-1 highest order terms. +In discrete systems the lowest-order term is Shift(t, k)(x(t)), instead of x(t). +We want to substitute the k-1 lowest order terms instead of the k-1 highest order terms. -The orders will also be off by one. The reason this is is that the dynamics of -the system should be given in terms of Shift(t, 1)(x(t), x(t-1), ...). But -having the observables be indexed by the next time step is not so nice. So we -handle the shifts in the renaming. - -The substitution should look like the following: - x(t) -> Shift(t, 1)(x(t)) - Shift(t, -1)(x(t)) -> Shift(t, 0)(x(t)) - Shift(t, -2)(x(t)) -> x_{t-1}(t) - Shift(t, -3)(x(t)) -> x_{t-2}(t) - and so on... - -In the implicit discrete case this shouldn't happen. The simplification should -look like a NonlinearSystem. - -2. For discrete systems Shift(t, 2)(x(t)) cannot be substituted as Shift(t, 1)(Shift(t,1)(x(t)). -This is different from the continuous case where D(D(x)) can be substituted for -by iteratively substituting x_t ~ D(x), then x_tt ~ D(x_t). For this reason the -shift_sub dict is updated at the time that the renamed variables are written, -inside the loop where new variables are generated. +In the system x(k) ~ x(k-1) + x(k-2), we want to lower +Shift(t, -1)(x(t)) -> x\_{t-1}(t) =# """ Generate new derivative variables for the system. -Effects on the state: +Effects on the system structure: - fullvars: add the new derivative variables x_t - neweqs: add the identity equations for the new variables, D(x) ~ x_t - graph: update graph with the new equations and variables, and their connections - solvable_graph: -- var_eq_matching: solvable equations +- var_eq_matching: match D(x) to the added identity equation """ function generate_derivative_variables!(ts::TearingState, neweqs, var_eq_matching; - is_discrete = false, mm = nothing, shift_sub = nothing) + is_discrete = false, mm = nothing) @unpack fullvars, sys, structure = ts - @unpack solvable_graph, var_to_diff, eq_to_diff, graph, lowest_shift = structure + @unpack solvable_graph, var_to_diff, eq_to_diff, graph = structure eq_var_matching = invview(var_eq_matching) diff_to_var = invview(var_to_diff) iv = ModelingToolkit.has_iv(sys) ? ModelingToolkit.get_iv(sys) : nothing lower_name = is_discrete ? lower_varname_withshift : lower_varname_with_unit - # index v gets mapped to the lowest shift and the index of the unshifted variable - if is_discrete - idx_to_lowest_shift = Dict{Int, Tuple{Int, Int}}(var => (0,0) for var in 1:length(fullvars)) - var_to_unshiftedidx = Dict{Any, Int}(var => findfirst(x -> isequal(x, var), fullvars) for var in keys(lowest_shift)) - - for (i,var) in enumerate(fullvars) - key = (operation(var) isa Shift) ? only(arguments(var)) : var - idx_to_lowest_shift[i] = (get(lowest_shift, key, 0), get(var_to_unshiftedidx, key, i)) - end - end - linear_eqs = mm === nothing ? Dict{Int, Int}() : Dict(reverse(en) for en in enumerate(mm.nzrows)) - # v is the index of the current variable, x = fullvars[v] - # dv is the index of the derivative dx = D(x), x_t is the substituted variable - # For ODESystems: lv is the index of the lowest-order variable (x(t)) - # For DiscreteSystems: - # - lv is the index of the lowest-order variable (Shift(t, k)(x(t))) - # - uv is the index of the highest-order variable (x(t)) + # Generate new derivative variables for all unsolved variables that have a derivative in the system for v in 1:length(var_to_diff) + # Check if a derivative 1) exists and 2) is unsolved for dv = var_to_diff[v] - - if is_discrete - x = fullvars[v] - op = operation(x) - (low, uv) = idx_to_lowest_shift[v] - - # If v is unshifted (i.e. x(t)), then substitute the lowest-shift variable - if !(op isa Shift) - dv = findfirst(_x -> isequal(_x, Shift(iv, low)(x)), fullvars) - end - dx = fullvars[dv] - order, lv = var_order(diff_to_var, dv) - @show fullvars[uv] - x_t = lower_name(fullvars[lv], iv, -low-order-1; unshifted = fullvars[uv], allow_zero = true) - shift_sub[dx] = x_t - (var_eq_matching[dv] isa Int) ? continue : @goto DISCRETE_VARIABLE - end dv isa Int || continue solved = var_eq_matching[dv] isa Int solved && continue - # Check if there's `D(x) = x_t` already - local v_t, dummy_eq - for eq in 𝑑neighbors(solvable_graph, dv) - mi = get(linear_eqs, eq, 0) - iszero(mi) && continue - row = @view mm[mi, :] - nzs = nonzeros(row) - rvs = SparseArrays.nonzeroinds(row) - # note that `v_t` must not be differentiated - if length(nzs) == 2 && - (abs(nzs[1]) == 1 && nzs[1] == -nzs[2]) && - (v_t = rvs[1] == dv ? rvs[2] : rvs[1]; - diff_to_var[v_t] === nothing) - @assert dv in rvs - dummy_eq = eq - @goto FOUND_DUMMY_EQ - end + # If there's `D(x) = x_t` already, update mappings and continue without + # adding new equations/variables + dd = find_duplicate_dd(dv, lineareqs, mm) + + if !isnothing(dd) + dummy_eq, v_t = dd + var_to_diff[v_t] = var_to_diff[dv] + var_eq_matching[dv] = unassigned + eq_var_matching[dummy_eq] = dv + continue end - # add `x_t` dx = fullvars[dv] order, lv = var_order(diff_to_var, dv) - x_t = lower_name(fullvars[lv], iv, order) + x_t = is_discrete ? lower_name(fullvars[lv], iv) + : lower_name(fullvars[lv], iv, order) - @label DISCRETE_VARIABLE - push!(fullvars, simplify_shifts(x_t)) - v_t = length(fullvars) - v_t_idx = add_vertex!(var_to_diff) - add_vertex!(graph, DST) - # TODO: do we care about solvable_graph? We don't use them after - # `dummy_derivative_graph`. - add_vertex!(solvable_graph, DST) - push!(var_eq_matching, unassigned) - - is_discrete && begin - idx_to_lowest_shift[v_t] = idx_to_lowest_shift[dv] - for e in 𝑑neighbors(graph, dv) - add_edge!(graph, e, v_t) - rem_edge!(graph, e, dv) - end - # Do not add the lowest-order substitution as an equation, just substitute - !(operation(x) isa Shift) && begin - var_to_diff[v_t] = var_to_diff[dv] - continue - end - end + # Add `x_t` to the graph + add_dd_variable!(structure, x_t, dv) + # Add `D(x) - x_t ~ 0` to the graph + add_dd_equation!(structure, neweqs, 0 ~ dx - x_t, dv) - # add `D(x) - x_t ~ 0` - push!(neweqs, 0 ~ dx - x_t) - add_vertex!(graph, SRC) - dummy_eq = length(neweqs) - add_edge!(graph, dummy_eq, dv) - add_edge!(graph, dummy_eq, v_t) - add_vertex!(solvable_graph, SRC) - add_edge!(solvable_graph, dummy_eq, dv) - - @label FOUND_DUMMY_EQ - var_to_diff[v_t] = var_to_diff[dv] + # Update matching + push!(var_eq_matching, unassigned) var_eq_matching[dv] = unassigned eq_var_matching[dummy_eq] = dv end end -function add_solvable_variable!(state::TearingState) - +""" +Check if there's `D(x) = x_t` already. +""" +function find_duplicate_dd(dv, lineareqs, mm) + for eq in 𝑑neighbors(solvable_graph, dv) + mi = get(linear_eqs, eq, 0) + iszero(mi) && continue + row = @view mm[mi, :] + nzs = nonzeros(row) + rvs = SparseArrays.nonzeroinds(row) + # note that `v_t` must not be differentiated + if length(nzs) == 2 && + (abs(nzs[1]) == 1 && nzs[1] == -nzs[2]) && + (v_t = rvs[1] == dv ? rvs[2] : rvs[1]; + diff_to_var[v_t] === nothing) + @assert dv in rvs + return eq, v_t + end + end + return nothing +end + +function add_dd_variable!(s::SystemStructure, x_t, dv) + push!(s.fullvars, simplify_shifts(x_t)) + v_t_idx = add_vertex!(s.var_to_diff) + @assert v_t_idx == ndsts(graph) == ndsts(solvable_graph) == length(fullvars) == + length(var_eq_matching) + add_vertex!(s.graph, DST) + # TODO: do we care about solvable_graph? We don't use them after + # `dummy_derivative_graph`. + add_vertex!(s.solvable_graph, DST) + var_to_diff[v_t] = var_to_diff[dv] end -function add_solvable_equation!(s::SystemStructure, neweqs, eq) +# dv = index of D(x), v_t = index of x_t +function add_dd_equation!(s::SystemStructure, neweqs, eq, dv) + push!(neweqs, eq) + add_vertex!(s.graph, SRC) + v_t = length(s.fullvars) + dummy_eq = length(neweqs) + add_edge!(s.graph, dummy_eq, dv) + add_edge!(s.graph, dummy_eq, v_t) + add_vertex!(s.solvable_graph, SRC) + add_edge!(s.solvable_graph, dummy_eq, dv) end """ Solve the solvable equations of the system and generate differential (or discrete) equations in terms of the selected states. - -Will reorder equations and unknowns to be: - [diffeqs; ...] - [diffvars; ...] -such that the mass matrix is: - [I 0 - 0 0]. - -Update the state to account for the new ordering and equations. - -####### DISCRETE CASE -- Differential equations: substitute variables with everything shifted forward one timestep. -- Algebraic and observable equations: substitute variables with everything shifted back one timestep. """ -function solve_and_generate_equations!(state::TearingState, neweqs, var_eq_matching; simplify = false, shift_sub = Dict()) +function generate_system_equations!(state::TearingState, neweqs, var_eq_matching; simplify = false) @unpack fullvars, sys, structure = state - @unpack solvable_graph, var_to_diff, eq_to_diff, graph, lowest_shift = structure + @unpack solvable_graph, var_to_diff, eq_to_diff, graph = structure eq_var_matching = invview(var_eq_matching) diff_to_var = invview(var_to_diff) - dx_sub = Dict() + total_sub = Dict() if ModelingToolkit.has_iv(sys) iv = get_iv(sys) if is_only_discrete(structure) D = Shift(iv, 1) - lower_name = lower_varname_withshift - total_sub = shift_sub else D = Differential(iv) - lower_name = lower_varname_with_unit - total_sub = dx_sub end else iv = D = nothing - lower_name = lower_varname_with_unit end # if var is like D(x) or Shift(t, 1)(x) @@ -544,14 +480,14 @@ function solve_and_generate_equations!(state::TearingState, neweqs, var_eq_match (eq, iv) -> eq isa Int && iv isa Int && BipartiteEdge(eq, iv) in solvable_graph end - diffeq_idxs = Int[] - algeeq_idxs = Int[] diff_eqs = Equation[] - alge_eqs = Equation[] + diffeq_idxs = Int[] diff_vars = Int[] - subeqs = Equation[] - solved_equations = Int[] - solved_variables = Int[] + alge_eqs = Equation[] + algeeq_idxs = Int[] + solved_eqs = Equation[] + solvedeq_idxs = Int[] + solved_vars = Int[] toporder = topological_sort(DiCMOBiGraph{false}(graph, var_eq_matching)) eqs = Iterators.reverse(toporder) @@ -561,100 +497,126 @@ function solve_and_generate_equations!(state::TearingState, neweqs, var_eq_match # fullvars[iv] is a differential variable of the form D^n(x), and neweqs[ieq] # is solved to give the RHS. for ieq in eqs - println() iv = eq_var_matching[ieq] if is_solvable(ieq, iv) - # We don't solve differential equations, but we will need to try to - # convert it into the mass matrix form. - # We cannot solve the differential variable like D(x) if isdervar(iv) isnothing(D) && error("Differential found in a non-differential system. Likely this is a bug in the construction of an initialization system. Please report this issue with a reproducible example. Offending equation: $(equations(sys)[ieq])") - order, lv = var_order(diff_to_var, iv) - dx = D(simplify_shifts(fullvars[lv])) - eq = dx ~ simplify_shifts(Symbolics.fixpoint_sub( - Symbolics.symbolic_linear_solve(neweqs[ieq], - fullvars[iv]), - total_sub; operator = ModelingToolkit.Shift)) - for e in 𝑑neighbors(graph, iv) - rem_edge!(graph, e, iv) - add_edge!(graph, e, lv) - end - push!(diff_eqs, eq) - total_sub[simplify_shifts(eq.lhs)] = eq.rhs - dx_sub[simplify_shifts(eq.lhs)] = eq.rhs - push!(diffeq_idxs, ieq) - push!(diff_vars, diff_to_var[iv]) - continue - end - eq = neweqs[ieq] - var = fullvars[iv] - residual = eq.lhs - eq.rhs - a, b, islinear = linear_expansion(residual, var) - @assert islinear - # 0 ~ a * var + b - # var ~ -b/a - if ModelingToolkit._iszero(a) - @warn "Tearing: solving $eq for $var is singular!" + add_differential_equation!(structure, iv, neweqs, ieq, + diff_vars, diff_eqs, diffeq_idxs, total_sub) else - rhs = -b / a - neweq = var ~ simplify_shifts(Symbolics.fixpoint_sub( - simplify ? - Symbolics.simplify(rhs) : rhs, - dx_sub; operator = ModelingToolkit.Shift)) - push!(subeqs, neweq) - push!(solved_equations, ieq) - push!(solved_variables, iv) + add_solved_equation!(structure, iv, neweqs, ieq, + solved_vars, solved_eqs, solvedeq_idxs, total_sub) end else - eq = neweqs[ieq] - rhs = eq.rhs - if !(eq.lhs isa Number && eq.lhs == 0) - rhs = eq.rhs - eq.lhs - end - push!(alge_eqs, 0 ~ simplify_shifts(Symbolics.fixpoint_sub(rhs, total_sub))) - push!(algeeq_idxs, ieq) + add_algebraic_equation!(structure, neweqs, ieq, + alge_eqs, algeeq_idxs, total_sub) end end - @show neweqs - @show subeqs - # TODO: BLT sorting + # Generate new equations and orderings neweqs = [diff_eqs; alge_eqs] - inveqsperm = [diffeq_idxs; algeeq_idxs] - eqsperm = zeros(Int, nsrcs(graph)) - for (i, v) in enumerate(inveqsperm) - eqsperm[v] = i - end + eq_ordering = [diffeq_idxs; algeeq_idxs] diff_vars_set = BitSet(diff_vars) if length(diff_vars_set) != length(diff_vars) error("Tearing internal error: lowering DAE into semi-implicit ODE failed!") end - solved_variables_set = BitSet(solved_variables) - invvarsperm = [diff_vars; + solved_vars_set = BitSet(solved_vars) + var_ordering = [diff_vars; setdiff!(setdiff(1:ndsts(graph), diff_vars_set), - solved_variables_set)] + solved_vars_set)] + + return neweqs, solved_eqs, eq_ordering, var_ordering, length(solved_vars), length(solved_vars_set) +end + +function add_differential_equation!(s::SystemStructure, iv, neweqs, ieq, diff_vars, diff_eqs, diffeqs_idxs, total_sub) + diff_to_var = invview(s.var_to_diff) + + order, lv = var_order(diff_to_var, iv) + dx = D(simplify_shifts(fullvars[lv])) + eq = dx ~ simplify_shifts(Symbolics.fixpoint_sub( + Symbolics.symbolic_linear_solve(neweqs[ieq], + fullvars[iv]), + total_sub; operator = ModelingToolkit.Shift)) + for e in 𝑑neighbors(s.graph, iv) + e == ieq && continue + rem_edge!(s.graph, e, iv) + end + + push!(diff_eqs, eq) + total_sub[simplify_shifts(eq.lhs)] = eq.rhs + push!(diffeq_idxs, ieq) + push!(diff_vars, diff_to_var[iv]) +end + +function add_algebraic_equation!(s::SystemStructure, neweqs, ieq, alge_eqs, algeeq_idxs, total_sub) + eq = neweqs[ieq] + rhs = eq.rhs + if !(eq.lhs isa Number && eq.lhs == 0) + rhs = eq.rhs - eq.lhs + end + push!(alge_eqs, 0 ~ simplify_shifts(Symbolics.fixpoint_sub(rhs, total_sub))) + push!(algeeq_idxs, ieq) +end + +function add_solved_equation!(s::SystemStructure, iv, neweqs, ieq, solved_vars, solved_eqs, solvedeq_idxs, total_sub) + eq = neweqs[ieq] + var = fullvars[iv] + residual = eq.lhs - eq.rhs + a, b, islinear = linear_expansion(residual, var) + @assert islinear + # 0 ~ a * var + b + # var ~ -b/a + if ModelingToolkit._iszero(a) + @warn "Tearing: solving $eq for $var is singular!" + else + rhs = -b / a + neweq = var ~ simplify_shifts(Symbolics.fixpoint_sub( + simplify ? + Symbolics.simplify(rhs) : rhs, + total_sub; operator = ModelingToolkit.Shift)) + push!(solved_eqs, neweq) + push!(solvedeq_idxs, ieq) + push!(solved_vars, iv) + end +end + +""" +Reorder the equations and unknowns to be: + [diffeqs; ...] + [diffvars; ...] +such that the mass matrix is: + [I 0 + 0 0]. + +Update the state to account for the new ordering and equations. +""" +# TODO: BLT sorting +function reorder_vars!(s::SystemStructure, var_eq_matching, eq_ordering, var_ordering, nelim_eq, nelim_var) + @unpack solvable_graph, var_to_diff, eq_to_diff, graph = structure + + eqsperm = zeros(Int, nsrcs(graph)) + for (i, v) in enumerate(eq_ordering) + eqsperm[v] = i + end varsperm = zeros(Int, ndsts(graph)) - for (i, v) in enumerate(invvarsperm) + for (i, v) in enumerate(var_ordering) varsperm[v] = i end - deps = Vector{Int}[i == 1 ? Int[] : collect(1:(i - 1)) - for i in 1:length(solved_equations)] - # Contract the vertices in the structure graph to make the structure match # the new reality of the system we've just created. - graph = contract_variables(graph, var_eq_matching, varsperm, eqsperm, - length(solved_variables), length(solved_variables_set)) + new_graph = contract_variables(graph, var_eq_matching, varsperm, eqsperm, + nelim_eq, nelim_var) - new_var_to_diff = complete(DiffGraph(length(invvarsperm))) + new_var_to_diff = complete(DiffGraph(length(var_ordering))) for (v, d) in enumerate(var_to_diff) v′ = varsperm[v] (v′ > 0 && d !== nothing) || continue d′ = varsperm[d] new_var_to_diff[v′] = d′ > 0 ? d′ : nothing end - new_eq_to_diff = complete(DiffGraph(length(inveqsperm))) + new_eq_to_diff = complete(DiffGraph(length(eq_ordering))) for (v, d) in enumerate(eq_to_diff) v′ = eqsperm[v] (v′ > 0 && d !== nothing) || continue @@ -663,7 +625,53 @@ function solve_and_generate_equations!(state::TearingState, neweqs, var_eq_match end new_fullvars = fullvars[invvarsperm] - new_fullvars, new_var_to_diff, new_eq_to_diff, neweqs, subeqs, graph + # Update system structure + @set! state.structure.graph = complete(new_graph) + @set! state.structure.var_to_diff = new_var_to_diff + @set! state.structure.eq_to_diff = new_eq_to_diff + @set! state.fullvars = new_fullvars +end + +""" +Set the system equations, unknowns, observables post-tearing. +""" +function update_simplified_system!(state::TearingState, neweqs, solved_eqs, dummy_sub, var_eq_matching, extra_unknowns) + @unpack solvable_graph, var_to_diff, eq_to_diff, graph = state.structure + diff_to_var = invview(var_to_diff) + + ispresent = let var_to_diff = var_to_diff, graph = graph + i -> (!isempty(𝑑neighbors(graph, i)) || + (var_to_diff[i] !== nothing && !isempty(𝑑neighbors(graph, var_to_diff[i])))) + end + + sys = state.sys + obs_sub = dummy_sub + for eq in neweqs + isdiffeq(eq) || continue + obs_sub[eq.lhs] = eq.rhs + end + # TODO: compute the dependency correctly so that we don't have to do this + obs = [fast_substitute(observed(sys), obs_sub); solved_eqs] + + unknowns = Any[v + for (i, v) in enumerate(state.fullvars) + if diff_to_var[i] === nothing && ispresent(i)] + unknowns = [unknowns; extra_unknowns] + @set! sys.unknowns = unknowns + + obs, subeqs, deps = cse_and_array_hacks( + sys, obs, solved_eqs, unknowns, neweqs; cse = cse_hack, array = array_hack) + + @set! sys.eqs = neweqs + @set! sys.observed = obs + @set! sys.substitutions = Substitutions(subeqs, deps) + + # Only makes sense for time-dependent + # TODO: generalize to SDE + if sys isa ODESystem + @set! sys.schedule = Schedule(var_eq_matching, dummy_sub) + end + sys = schedule(sys) end # Terminology and Definition: @@ -675,10 +683,8 @@ end # appear in the system. Algebraic variables are variables that are not # differential variables. -import ModelingToolkit: Shift - # Give the order of the variable indexed by dv -function var_order(diff_to_var, dv) +function var_order(diff_to_var, dv) order = 0 while (dv′ = diff_to_var[dv]) !== nothing order += 1 @@ -689,8 +695,7 @@ end function tearing_reassemble(state::TearingState, var_eq_matching, full_var_eq_matching = nothing; simplify = false, mm = nothing, cse_hack = true, array_hack = true) - @unpack fullvars, sys, structure = state - @unpack solvable_graph, var_to_diff, eq_to_diff, graph, lowest_shift = structure + extra_vars = Int[] if full_var_eq_matching !== nothing for v in 𝑑vertices(state.structure.graph) @@ -699,69 +704,21 @@ function tearing_reassemble(state::TearingState, var_eq_matching, push!(extra_vars, v) end end + extra_unknowns = fullvars[extra_vars] neweqs = collect(equations(state)) - diff_to_var = invview(var_to_diff) - is_discrete = is_only_discrete(state.structure) - - shift_sub = Dict() # Structural simplification dummy_sub = Dict() - substitute_dummy_derivatives!(state, neweqs, dummy_sub, var_eq_matching) - - generate_derivative_variables!(state, neweqs, var_eq_matching; - is_discrete, mm, shift_sub) - - new_fullvars, new_var_to_diff, new_eq_to_diff, neweqs, subeqs, graph = - solve_and_generate_equations!(state, neweqs, var_eq_matching; simplify, shift_sub) - - # Update system - var_to_diff = new_var_to_diff - eq_to_diff = new_eq_to_diff - diff_to_var = invview(var_to_diff) - - old_fullvars = fullvars - @set! state.structure.graph = complete(graph) - @set! state.structure.var_to_diff = var_to_diff - @set! state.structure.eq_to_diff = eq_to_diff - @set! state.fullvars = fullvars = new_fullvars - ispresent = let var_to_diff = var_to_diff, graph = graph - i -> (!isempty(𝑑neighbors(graph, i)) || - (var_to_diff[i] !== nothing && !isempty(𝑑neighbors(graph, var_to_diff[i])))) - end - - sys = state.sys - obs_sub = dummy_sub - for eq in neweqs - isdiffeq(eq) || continue - obs_sub[eq.lhs] = eq.rhs - end - # TODO: compute the dependency correctly so that we don't have to do this - obs = [fast_substitute(observed(sys), obs_sub); subeqs] + substitute_derivatives_algevars!(state, neweqs, dummy_sub, var_eq_matching) - unknowns = Any[v - for (i, v) in enumerate(fullvars) - if diff_to_var[i] === nothing && ispresent(i)] - if !isempty(extra_vars) - for v in extra_vars - push!(unknowns, old_fullvars[v]) - end - end - @set! sys.unknowns = unknowns + generate_derivative_variables!(state, neweqs, var_eq_matching; mm) - obs, subeqs, deps = cse_and_array_hacks( - sys, obs, subeqs, unknowns, neweqs; cse = cse_hack, array = array_hack) + neweqs, solved_eqs, eq_ordering, var_ordering, nelim_eq, nelim_var = + generate_system_equations!(state, neweqs, var_eq_matching; simplify) - @set! sys.eqs = neweqs - @set! sys.observed = obs - @set! sys.substitutions = Substitutions(subeqs, deps) + reorder_vars!(state.structure, var_eq_matching, eq_ordering, var_ordering, nelim_eq, nelim_var) - # Only makes sense for time-dependent - # TODO: generalize to SDE - if sys isa ODESystem - @set! sys.schedule = Schedule(var_eq_matching, dummy_sub) - end - sys = schedule(sys) + sys = update_simplified_system!(state, neweqs, solved_eqs, dummy_sub, var_eq_matching, extra_unknowns) @set! state.sys = sys @set! sys.tearing_state = state return invalidate_cache!(sys) diff --git a/src/structural_transformation/utils.jl b/src/structural_transformation/utils.jl index b947fa4269..77f0b62a1c 100644 --- a/src/structural_transformation/utils.jl +++ b/src/structural_transformation/utils.jl @@ -452,9 +452,13 @@ end ### Misc ### -function lower_varname_withshift(var, iv, backshift; unshifted = nothing, allow_zero = true) - backshift <= 0 && return Shift(iv, -backshift)(unshifted, allow_zero) - ds = backshift > 0 ? "$iv-$backshift" : "$iv+$(-backshift)" +function lower_varname_withshift(var, iv) + op = operation(var) + op isa Shift || return var + backshift = op.steps + backshift > 0 && return var + + ds = "$iv-$(-backshift)" d_separator = 'ˍ' if ModelingToolkit.isoperator(var, ModelingToolkit.Shift) @@ -477,15 +481,9 @@ function isdoubleshift(var) ModelingToolkit.isoperator(arguments(var)[1], ModelingToolkit.Shift) end -### Rules -# 1. x(t) -> x(t) -# 2. Shift(t, 0)(x(t)) -> x(t) -# 3. Shift(t, 3)(Shift(t, 2)(x(t)) -> Shift(t, 5)(x(t)) - function simplify_shifts(var) ModelingToolkit.hasshift(var) || return var var isa Equation && return simplify_shifts(var.lhs) ~ simplify_shifts(var.rhs) - ((op = operation(var)) isa Shift) && op.steps == 0 && return simplify_shifts(arguments(var)[1]) if isdoubleshift(var) op1 = operation(var) vv1 = arguments(var)[1] @@ -501,58 +499,3 @@ function simplify_shifts(var) unwrap(var).metadata) end end - -""" -Power expand the shifts. Used for substitution. - -Shift(t, -3)(x(t)) -> Shift(t, -1)(Shift(t, -1)(Shift(t, -1)(x))) -""" -function expand_shifts(var) - ModelingToolkit.hasshift(var) || return var - var = ModelingToolkit.value(var) - - var isa Equation && return expand_shifts(var.lhs) ~ expand_shifts(var.rhs) - op = operation(var) - s = sign(op.steps) - arg = only(arguments(var)) - - if ModelingToolkit.isvariable(arg) && (ModelingToolkit.getvariabletype(arg) === VARIABLE) && isequal(op.t, only(arguments(arg))) - out = arg - for i in 1:op.steps - out = Shift(op.t, s)(out) - end - return out - elseif iscall(arg) - return maketerm(typeof(var), operation(var), expand_shifts.(arguments(var)), - unwrap(var).metadata) - else - return arg - end -end - -""" -Shift(t, 1)(x + z) -> Shift(t, 1)(x) + Shift(t, 1)(z) -""" -function distribute_shift(var) - ModelingToolkit.hasshift(var) || return var - var isa Equation && return distribute_shift(var.lhs) ~ distribute_shift(var.rhs) - shift = operation(var) - expr = only(arguments(var)) - _distribute_shift(expr, shift) -end - -function _distribute_shift(expr, shift) - op = operation(expr) - args = arguments(expr) - - if length(args) == 1 - if ModelingToolkit.isvariable(only(args)) && isequal(op.t, only(args)) - return shift(only(args)) - else - return only(args) - end - else iscall(op) - return maketerm(typeof(expr), operation(expr), _distribute_shift.(args, shift), - unwrap(var).metadata) - end -end diff --git a/src/systems/systemstructure.jl b/src/systems/systemstructure.jl index 89fcebf549..e5a227d9fb 100644 --- a/src/systems/systemstructure.jl +++ b/src/systems/systemstructure.jl @@ -154,7 +154,6 @@ Base.@kwdef mutable struct SystemStructure var_types::Union{Vector{VariableType}, Nothing} """Whether the system is discrete.""" only_discrete::Bool - lowest_shift::Union{Dict, Nothing} end function Base.copy(structure::SystemStructure) @@ -436,7 +435,7 @@ function TearingState(sys; quick_cancel = false, check = true) ts = TearingState(sys, fullvars, SystemStructure(complete(var_to_diff), complete(eq_to_diff), - complete(graph), nothing, var_types, sys isa DiscreteSystem, lowest_shift), + complete(graph), nothing, var_types, sys isa DiscreteSystem), Any[]) if sys isa DiscreteSystem ts = shift_discrete_system(ts, lowest_shift) @@ -466,9 +465,8 @@ end Shift variable x by the largest shift s such that x(k-s) appears in the system of equations. The lowest-shift term will have. """ -function shift_discrete_system(ts::TearingState, lowest_shift) +function shift_discrete_system(ts::TearingState) @unpack fullvars, sys = ts - return ts discvars = OrderedSet() eqs = equations(sys) From 240ab215b2ec457186b889ca53160e9d23db02e8 Mon Sep 17 00:00:00 2001 From: vyudu Date: Mon, 10 Feb 2025 18:38:22 -0500 Subject: [PATCH 0802/2176] fix: properly rename variables inside generate_system_equations --- .../symbolics_tearing.jl | 174 ++++++++++-------- src/structural_transformation/utils.jl | 14 +- src/systems/systemstructure.jl | 16 +- 3 files changed, 119 insertions(+), 85 deletions(-) diff --git a/src/structural_transformation/symbolics_tearing.jl b/src/structural_transformation/symbolics_tearing.jl index b99e6ec21a..74205fcf86 100644 --- a/src/structural_transformation/symbolics_tearing.jl +++ b/src/structural_transformation/symbolics_tearing.jl @@ -248,7 +248,7 @@ called dummy derivatives. State selection is done. All non-differentiated variables are algebraic variables, and all variables that appear differentiated are differential variables. """ -function substitute_derivatives_algevars!(ts::TearingState, neweqs, dummy_sub, var_eq_matching) +function substitute_derivatives_algevars!(ts::TearingState, neweqs, var_eq_matching, dummy_sub) @unpack fullvars, sys, structure = ts @unpack solvable_graph, var_to_diff, eq_to_diff, graph = structure diff_to_var = invview(var_to_diff) @@ -353,30 +353,32 @@ Effects on the system structure: - solvable_graph: - var_eq_matching: match D(x) to the added identity equation """ -function generate_derivative_variables!(ts::TearingState, neweqs, var_eq_matching; - is_discrete = false, mm = nothing) +function generate_derivative_variables!(ts::TearingState, neweqs, var_eq_matching; mm = nothing) @unpack fullvars, sys, structure = ts @unpack solvable_graph, var_to_diff, eq_to_diff, graph = structure eq_var_matching = invview(var_eq_matching) diff_to_var = invview(var_to_diff) iv = ModelingToolkit.has_iv(sys) ? ModelingToolkit.get_iv(sys) : nothing - lower_name = is_discrete ? lower_varname_withshift : lower_varname_with_unit - + is_discrete = is_only_discrete(structure) + lower_varname = is_discrete ? lower_shift_varname : lower_varname_with_unit linear_eqs = mm === nothing ? Dict{Int, Int}() : Dict(reverse(en) for en in enumerate(mm.nzrows)) - # Generate new derivative variables for all unsolved variables that have a derivative in the system + # For variable x, make dummy derivative x_t if the + # derivative is in the system for v in 1:length(var_to_diff) - # Check if a derivative 1) exists and 2) is unsolved for dv = var_to_diff[v] + # For discrete systems, directly substitute lowest-order variable + if is_discrete && diff_to_var[v] == nothing + fullvars[v] = lower_varname(fullvars[v], iv) + end dv isa Int || continue solved = var_eq_matching[dv] isa Int solved && continue # If there's `D(x) = x_t` already, update mappings and continue without # adding new equations/variables - dd = find_duplicate_dd(dv, lineareqs, mm) - + dd = find_duplicate_dd(dv, solvable_graph, linear_eqs, mm) if !isnothing(dd) dummy_eq, v_t = dd var_to_diff[v_t] = var_to_diff[dv] @@ -386,26 +388,25 @@ function generate_derivative_variables!(ts::TearingState, neweqs, var_eq_matchin end dx = fullvars[dv] - order, lv = var_order(diff_to_var, dv) - x_t = is_discrete ? lower_name(fullvars[lv], iv) - : lower_name(fullvars[lv], iv, order) - + order, lv = var_order(dv, diff_to_var) + x_t = is_discrete ? lower_varname(fullvars[dv], iv) : lower_varname(fullvars[lv], iv, order) + # Add `x_t` to the graph - add_dd_variable!(structure, x_t, dv) + v_t = add_dd_variable!(structure, fullvars, x_t, dv) # Add `D(x) - x_t ~ 0` to the graph - add_dd_equation!(structure, neweqs, 0 ~ dx - x_t, dv) + dummy_eq = add_dd_equation!(structure, neweqs, 0 ~ dx - x_t, dv, v_t) # Update matching push!(var_eq_matching, unassigned) var_eq_matching[dv] = unassigned - eq_var_matching[dummy_eq] = dv + eq_var_matching[dummy_eq] = dv end end """ -Check if there's `D(x) = x_t` already. +Check if there's `D(x) = x_t` already. """ -function find_duplicate_dd(dv, lineareqs, mm) +function find_duplicate_dd(dv, solvable_graph, linear_eqs, mm) for eq in 𝑑neighbors(solvable_graph, dv) mi = get(linear_eqs, eq, 0) iszero(mi) && continue @@ -424,28 +425,28 @@ function find_duplicate_dd(dv, lineareqs, mm) return nothing end -function add_dd_variable!(s::SystemStructure, x_t, dv) - push!(s.fullvars, simplify_shifts(x_t)) +function add_dd_variable!(s::SystemStructure, fullvars, x_t, dv) + push!(fullvars, simplify_shifts(x_t)) + v_t = length(fullvars) v_t_idx = add_vertex!(s.var_to_diff) - @assert v_t_idx == ndsts(graph) == ndsts(solvable_graph) == length(fullvars) == - length(var_eq_matching) add_vertex!(s.graph, DST) # TODO: do we care about solvable_graph? We don't use them after # `dummy_derivative_graph`. add_vertex!(s.solvable_graph, DST) - var_to_diff[v_t] = var_to_diff[dv] + s.var_to_diff[v_t] = s.var_to_diff[dv] + v_t end # dv = index of D(x), v_t = index of x_t -function add_dd_equation!(s::SystemStructure, neweqs, eq, dv) +function add_dd_equation!(s::SystemStructure, neweqs, eq, dv, v_t) push!(neweqs, eq) add_vertex!(s.graph, SRC) - v_t = length(s.fullvars) dummy_eq = length(neweqs) add_edge!(s.graph, dummy_eq, dv) add_edge!(s.graph, dummy_eq, v_t) add_vertex!(s.solvable_graph, SRC) add_edge!(s.solvable_graph, dummy_eq, dv) + dummy_eq end """ @@ -463,6 +464,10 @@ function generate_system_equations!(state::TearingState, neweqs, var_eq_matching iv = get_iv(sys) if is_only_discrete(structure) D = Shift(iv, 1) + for v in fullvars + op = operation(v) + op isa Shift && (op.steps < 0) && (total_sub[v] = lower_shift_varname(v, iv)) + end else D = Differential(iv) end @@ -493,24 +498,40 @@ function generate_system_equations!(state::TearingState, neweqs, var_eq_matching eqs = Iterators.reverse(toporder) idep = iv - # Generate differential equations. - # fullvars[iv] is a differential variable of the form D^n(x), and neweqs[ieq] - # is solved to give the RHS. + # Generate equations. + # Solvable equations of differential variables D(x) become differential equations + # Solvable equations of non-differential variables become observable equations + # Non-solvable equations become algebraic equations. for ieq in eqs iv = eq_var_matching[ieq] - if is_solvable(ieq, iv) - if isdervar(iv) - isnothing(D) && - error("Differential found in a non-differential system. Likely this is a bug in the construction of an initialization system. Please report this issue with a reproducible example. Offending equation: $(equations(sys)[ieq])") - add_differential_equation!(structure, iv, neweqs, ieq, - diff_vars, diff_eqs, diffeq_idxs, total_sub) - else - add_solved_equation!(structure, iv, neweqs, ieq, - solved_vars, solved_eqs, solvedeq_idxs, total_sub) + var = fullvars[iv] + eq = neweqs[ieq] + + if is_solvable(ieq, iv) && isdervar(iv) + isnothing(D) && throw(UnexpectedDifferentialError(equations(sys)[ieq])) + order, lv = var_order(iv, diff_to_var) + dx = D(simplify_shifts(fullvars[lv])) + + neweq = make_differential_equation(var, dx, eq, total_sub) + for e in 𝑑neighbors(graph, iv) + e == ieq && continue + rem_edge!(graph, e, iv) + end + + push!(diff_eqs, neweq) + push!(diffeq_idxs, ieq) + push!(diff_vars, diff_to_var[iv]) + elseif is_solvable(ieq, iv) + neweq = make_solved_equation(var, eq, total_sub; simplify) + !isnothing(neweq) && begin + push!(solved_eqs, neweq) + push!(solvedeq_idxs, ieq) + push!(solved_vars, iv) end else - add_algebraic_equation!(structure, neweqs, ieq, - alge_eqs, algeeq_idxs, total_sub) + neweq = make_algebraic_equation(var, eq, total_sub) + push!(alge_eqs, neweq) + push!(algeeq_idxs, ieq) end end @@ -529,39 +550,29 @@ function generate_system_equations!(state::TearingState, neweqs, var_eq_matching return neweqs, solved_eqs, eq_ordering, var_ordering, length(solved_vars), length(solved_vars_set) end -function add_differential_equation!(s::SystemStructure, iv, neweqs, ieq, diff_vars, diff_eqs, diffeqs_idxs, total_sub) - diff_to_var = invview(s.var_to_diff) +struct UnexpectedDifferentialError + eq::Equation +end - order, lv = var_order(diff_to_var, iv) - dx = D(simplify_shifts(fullvars[lv])) - eq = dx ~ simplify_shifts(Symbolics.fixpoint_sub( - Symbolics.symbolic_linear_solve(neweqs[ieq], - fullvars[iv]), - total_sub; operator = ModelingToolkit.Shift)) - for e in 𝑑neighbors(s.graph, iv) - e == ieq && continue - rem_edge!(s.graph, e, iv) - end +function Base.showerror(io::IO, err::UnexpectedDifferentialError) + error("Differential found in a non-differential system. Likely this is a bug in the construction of an initialization system. Please report this issue with a reproducible example. Offending equation: $(err.eq)") +end - push!(diff_eqs, eq) - total_sub[simplify_shifts(eq.lhs)] = eq.rhs - push!(diffeq_idxs, ieq) - push!(diff_vars, diff_to_var[iv]) +function make_differential_equation(var, dx, eq, total_sub) + dx ~ simplify_shifts(Symbolics.fixpoint_sub( + Symbolics.symbolic_linear_solve(eq, var), + total_sub; operator = ModelingToolkit.Shift)) end -function add_algebraic_equation!(s::SystemStructure, neweqs, ieq, alge_eqs, algeeq_idxs, total_sub) - eq = neweqs[ieq] +function make_algebraic_equation(var, eq, total_sub) rhs = eq.rhs if !(eq.lhs isa Number && eq.lhs == 0) rhs = eq.rhs - eq.lhs end - push!(alge_eqs, 0 ~ simplify_shifts(Symbolics.fixpoint_sub(rhs, total_sub))) - push!(algeeq_idxs, ieq) + 0 ~ simplify_shifts(Symbolics.fixpoint_sub(rhs, total_sub)) end -function add_solved_equation!(s::SystemStructure, iv, neweqs, ieq, solved_vars, solved_eqs, solvedeq_idxs, total_sub) - eq = neweqs[ieq] - var = fullvars[iv] +function make_solved_equation(var, eq, total_sub; simplify = false) residual = eq.lhs - eq.rhs a, b, islinear = linear_expansion(residual, var) @assert islinear @@ -569,15 +580,13 @@ function add_solved_equation!(s::SystemStructure, iv, neweqs, ieq, solved_vars, # var ~ -b/a if ModelingToolkit._iszero(a) @warn "Tearing: solving $eq for $var is singular!" + return nothing else rhs = -b / a - neweq = var ~ simplify_shifts(Symbolics.fixpoint_sub( + return var ~ simplify_shifts(Symbolics.fixpoint_sub( simplify ? Symbolics.simplify(rhs) : rhs, total_sub; operator = ModelingToolkit.Shift)) - push!(solved_eqs, neweq) - push!(solvedeq_idxs, ieq) - push!(solved_vars, iv) end end @@ -592,8 +601,8 @@ such that the mass matrix is: Update the state to account for the new ordering and equations. """ # TODO: BLT sorting -function reorder_vars!(s::SystemStructure, var_eq_matching, eq_ordering, var_ordering, nelim_eq, nelim_var) - @unpack solvable_graph, var_to_diff, eq_to_diff, graph = structure +function reorder_vars!(state::TearingState, var_eq_matching, eq_ordering, var_ordering, nelim_eq, nelim_var) + @unpack solvable_graph, var_to_diff, eq_to_diff, graph = state.structure eqsperm = zeros(Int, nsrcs(graph)) for (i, v) in enumerate(eq_ordering) @@ -623,20 +632,26 @@ function reorder_vars!(s::SystemStructure, var_eq_matching, eq_ordering, var_ord d′ = eqsperm[d] new_eq_to_diff[v′] = d′ > 0 ? d′ : nothing end - new_fullvars = fullvars[invvarsperm] + new_fullvars = state.fullvars[var_ordering] + @show new_graph + @show new_var_to_diff # Update system structure @set! state.structure.graph = complete(new_graph) @set! state.structure.var_to_diff = new_var_to_diff @set! state.structure.eq_to_diff = new_eq_to_diff @set! state.fullvars = new_fullvars + state end """ Set the system equations, unknowns, observables post-tearing. """ -function update_simplified_system!(state::TearingState, neweqs, solved_eqs, dummy_sub, var_eq_matching, extra_unknowns) +function update_simplified_system!(state::TearingState, neweqs, solved_eqs, dummy_sub, var_eq_matching, extra_unknowns; + cse_hack = true, array_hack = true) @unpack solvable_graph, var_to_diff, eq_to_diff, graph = state.structure + @show graph + @show var_to_diff diff_to_var = invview(var_to_diff) ispresent = let var_to_diff = var_to_diff, graph = graph @@ -656,7 +671,12 @@ function update_simplified_system!(state::TearingState, neweqs, solved_eqs, dumm unknowns = Any[v for (i, v) in enumerate(state.fullvars) if diff_to_var[i] === nothing && ispresent(i)] + @show unknowns + @show state.fullvars + @show 𝑑neighbors(graph, 5) + @show neweqs unknowns = [unknowns; extra_unknowns] + @show unknowns @set! sys.unknowns = unknowns obs, subeqs, deps = cse_and_array_hacks( @@ -684,7 +704,7 @@ end # differential variables. # Give the order of the variable indexed by dv -function var_order(diff_to_var, dv) +function var_order(dv, diff_to_var) order = 0 while (dv′ = diff_to_var[dv]) !== nothing order += 1 @@ -695,7 +715,6 @@ end function tearing_reassemble(state::TearingState, var_eq_matching, full_var_eq_matching = nothing; simplify = false, mm = nothing, cse_hack = true, array_hack = true) - extra_vars = Int[] if full_var_eq_matching !== nothing for v in 𝑑vertices(state.structure.graph) @@ -704,21 +723,22 @@ function tearing_reassemble(state::TearingState, var_eq_matching, push!(extra_vars, v) end end - extra_unknowns = fullvars[extra_vars] + extra_unknowns = state.fullvars[extra_vars] neweqs = collect(equations(state)) + dummy_sub = Dict() # Structural simplification - dummy_sub = Dict() - substitute_derivatives_algevars!(state, neweqs, dummy_sub, var_eq_matching) + substitute_derivatives_algevars!(state, neweqs, var_eq_matching, dummy_sub) generate_derivative_variables!(state, neweqs, var_eq_matching; mm) neweqs, solved_eqs, eq_ordering, var_ordering, nelim_eq, nelim_var = generate_system_equations!(state, neweqs, var_eq_matching; simplify) - reorder_vars!(state.structure, var_eq_matching, eq_ordering, var_ordering, nelim_eq, nelim_var) + state = reorder_vars!(state, var_eq_matching, eq_ordering, var_ordering, nelim_eq, nelim_var) + + sys = update_simplified_system!(state, neweqs, solved_eqs, dummy_sub, var_eq_matching, extra_unknowns; cse_hack, array_hack) - sys = update_simplified_system!(state, neweqs, solved_eqs, dummy_sub, var_eq_matching, extra_unknowns) @set! state.sys = sys @set! sys.tearing_state = state return invalidate_cache!(sys) diff --git a/src/structural_transformation/utils.jl b/src/structural_transformation/utils.jl index 77f0b62a1c..fec57db080 100644 --- a/src/structural_transformation/utils.jl +++ b/src/structural_transformation/utils.jl @@ -452,9 +452,10 @@ end ### Misc ### -function lower_varname_withshift(var, iv) +# For discrete variables. Turn Shift(t, k)(x(t)) into xₜ₋ₖ(t) +function lower_shift_varname(var, iv) op = operation(var) - op isa Shift || return var + op isa Shift || return Shift(iv, 0)(var, true) # hack to prevent simplification of x(t) - x(t) backshift = op.steps backshift > 0 && return var @@ -476,6 +477,14 @@ function lower_varname_withshift(var, iv) return ModelingToolkit._with_unit(identity, newvar, iv) end +function lower_varname(var, iv, order; is_discrete = false) + if is_discrete + lower_shift_varname(var, iv) + else + lower_varname_with_unit(var, iv, order) + end +end + function isdoubleshift(var) return ModelingToolkit.isoperator(var, ModelingToolkit.Shift) && ModelingToolkit.isoperator(arguments(var)[1], ModelingToolkit.Shift) @@ -484,6 +493,7 @@ end function simplify_shifts(var) ModelingToolkit.hasshift(var) || return var var isa Equation && return simplify_shifts(var.lhs) ~ simplify_shifts(var.rhs) + (op = operation(var)) isa Shift && op.steps == 0 && return first(arguments(var)) if isdoubleshift(var) op1 = operation(var) vv1 = arguments(var)[1] diff --git a/src/systems/systemstructure.jl b/src/systems/systemstructure.jl index e5a227d9fb..ae4d21f225 100644 --- a/src/systems/systemstructure.jl +++ b/src/systems/systemstructure.jl @@ -142,15 +142,16 @@ has_equations(::TransformationState) = true Base.@kwdef mutable struct SystemStructure """Maps the (index of) a variable to the (index of) the variable describing its derivative.""" var_to_diff::DiffGraph - """Maps the (index of) a """ + """Maps the (index of) an equation.""" eq_to_diff::DiffGraph # Can be access as # `graph` to automatically look at the bipartite graph # or as `torn` to assert that tearing has run. - """Incidence graph of the system of equations. An edge from equation x to variable y exists if variable y appears in equation x.""" + """Graph that maps equations to variables that appear in them.""" graph::BipartiteGraph{Int, Nothing} - """.""" + """Graph that connects equations to the variable they will be solved for during simplification.""" solvable_graph::Union{BipartiteGraph{Int, Nothing}, Nothing} + """Variable types (brownian, variable, parameter) in the system.""" var_types::Union{Vector{VariableType}, Nothing} """Whether the system is discrete.""" only_discrete::Bool @@ -200,7 +201,9 @@ function complete!(s::SystemStructure) end mutable struct TearingState{T <: AbstractSystem} <: AbstractTearingState{T} + """The system of equations.""" sys::T + """The set of variables of the system.""" fullvars::Vector structure::SystemStructure extra_eqs::Vector @@ -438,7 +441,7 @@ function TearingState(sys; quick_cancel = false, check = true) complete(graph), nothing, var_types, sys isa DiscreteSystem), Any[]) if sys isa DiscreteSystem - ts = shift_discrete_system(ts, lowest_shift) + ts = shift_discrete_system(ts) end return ts end @@ -475,8 +478,9 @@ function shift_discrete_system(ts::TearingState) end iv = get_iv(sys) - discmap = Dict(k => StructuralTransformations.simplify_shifts(Shift(iv, 1)(k)) for k in discvars - if any(isequal(k), fullvars) && !isa(operation(k), Union{Sample, Hold})) + discmap = Dict(k => StructuralTransformations.simplify_shifts(Shift(iv, 1)(k)) + for k in discvars + if any(isequal(k), fullvars) && !isa(operation(k), Union{Sample, Hold})) for i in eachindex(fullvars) fullvars[i] = StructuralTransformations.simplify_shifts(fast_substitute( From 22a39bd41b2123c48a7f88016f25470041271b69 Mon Sep 17 00:00:00 2001 From: vyudu Date: Mon, 10 Feb 2025 18:42:01 -0500 Subject: [PATCH 0803/2176] delete comments --- src/structural_transformation/utils.jl | 3 --- src/systems/systems.jl | 8 ++++---- test/structural_transformation/utils.jl | 16 ---------------- 3 files changed, 4 insertions(+), 23 deletions(-) diff --git a/src/structural_transformation/utils.jl b/src/structural_transformation/utils.jl index fec57db080..757eb9a8db 100644 --- a/src/structural_transformation/utils.jl +++ b/src/structural_transformation/utils.jl @@ -271,8 +271,6 @@ end function find_solvables!(state::TearingState; kwargs...) @assert state.structure.solvable_graph === nothing - println("in find_solvables") - @show eqs eqs = equations(state) graph = state.structure.graph state.structure.solvable_graph = BipartiteGraph(nsrcs(graph), ndsts(graph)) @@ -280,7 +278,6 @@ function find_solvables!(state::TearingState; kwargs...) for ieq in 1:length(eqs) find_eq_solvables!(state, ieq, to_rm; kwargs...) end - @show eqs return nothing end diff --git a/src/systems/systems.jl b/src/systems/systems.jl index 0a7e4264e4..9c8c272c5c 100644 --- a/src/systems/systems.jl +++ b/src/systems/systems.jl @@ -41,10 +41,10 @@ function structural_simplify( end if newsys isa DiscreteSystem && any(eq -> symbolic_type(eq.lhs) == NotSymbolic(), equations(newsys)) - # error(""" - # Encountered algebraic equations when simplifying discrete system. This is \ - # not yet supported. - # """) + error(""" + Encountered algebraic equations when simplifying discrete system. This is \ + not yet supported. + """) end for pass in additional_passes newsys = pass(newsys) diff --git a/test/structural_transformation/utils.jl b/test/structural_transformation/utils.jl index 98d986ebd4..67f6016bc4 100644 --- a/test/structural_transformation/utils.jl +++ b/test/structural_transformation/utils.jl @@ -162,19 +162,3 @@ end structural_simplify(sys; additional_passes = [pass]) @test value[] == 1 end - -@testset "Shift simplification" begin - @variables x(t) y(t) z(t) - @parameters a b c - - # Expand shifts - @test isequal(ST.expand_shifts(Shift(t, -3)(x)), Shift(t, -1)(Shift(t, -1)(Shift(t, -1)(x)))) - expr = a * Shift(t, -2)(x) + Shift(t, 2)(y) + b - @test isequal(ST.expand_shifts(expr), - a * Shift(t, -1)(Shift(t, -1)(x)) + Shift(t, 1)(Shift(t, 1)(y)) + b) - @test isequal(ST.expand_shifts(Shift(t, 2)(Shift(t, 1)(a))), a) - - - # Distribute shifts - -end From 9aaf04a512853d5c9336674455ade5d92487c2f5 Mon Sep 17 00:00:00 2001 From: vyudu Date: Mon, 10 Feb 2025 18:50:23 -0500 Subject: [PATCH 0804/2176] more cleanup --- docs/src/systems/DiscreteSystem.md | 28 ------------------- .../symbolics_tearing.jl | 9 ------ src/systems/systemstructure.jl | 11 ++------ 3 files changed, 3 insertions(+), 45 deletions(-) delete mode 100644 docs/src/systems/DiscreteSystem.md diff --git a/docs/src/systems/DiscreteSystem.md b/docs/src/systems/DiscreteSystem.md deleted file mode 100644 index b6a8061e50..0000000000 --- a/docs/src/systems/DiscreteSystem.md +++ /dev/null @@ -1,28 +0,0 @@ -# DiscreteSystem - -## System Constructors - -```@docs -DiscreteSystem -``` - -## Composition and Accessor Functions - - - `get_eqs(sys)` or `equations(sys)`: The equations that define the discrete system. - - `get_unknowns(sys)` or `unknowns(sys)`: The set of unknowns in the discrete system. - - `get_ps(sys)` or `parameters(sys)`: The parameters of the discrete system. - - `get_iv(sys)`: The independent variable of the discrete system. - - `discrete_events(sys)`: The set of discrete events in the discrete system. - -## Transformations - -```@docs; canonical=false -structural_simplify -``` - -## Problem Constructors - -```@docs; canonical=false -DiscreteProblem(sys::DiscreteSystem, u0map, tspan) -DiscreteFunction(sys::DiscreteSystem, u0map, tspan) -``` diff --git a/src/structural_transformation/symbolics_tearing.jl b/src/structural_transformation/symbolics_tearing.jl index 74205fcf86..eed7bc3e29 100644 --- a/src/structural_transformation/symbolics_tearing.jl +++ b/src/structural_transformation/symbolics_tearing.jl @@ -634,8 +634,6 @@ function reorder_vars!(state::TearingState, var_eq_matching, eq_ordering, var_or end new_fullvars = state.fullvars[var_ordering] - @show new_graph - @show new_var_to_diff # Update system structure @set! state.structure.graph = complete(new_graph) @set! state.structure.var_to_diff = new_var_to_diff @@ -650,8 +648,6 @@ Set the system equations, unknowns, observables post-tearing. function update_simplified_system!(state::TearingState, neweqs, solved_eqs, dummy_sub, var_eq_matching, extra_unknowns; cse_hack = true, array_hack = true) @unpack solvable_graph, var_to_diff, eq_to_diff, graph = state.structure - @show graph - @show var_to_diff diff_to_var = invview(var_to_diff) ispresent = let var_to_diff = var_to_diff, graph = graph @@ -671,12 +667,7 @@ function update_simplified_system!(state::TearingState, neweqs, solved_eqs, dumm unknowns = Any[v for (i, v) in enumerate(state.fullvars) if diff_to_var[i] === nothing && ispresent(i)] - @show unknowns - @show state.fullvars - @show 𝑑neighbors(graph, 5) - @show neweqs unknowns = [unknowns; extra_unknowns] - @show unknowns @set! sys.unknowns = unknowns obs, subeqs, deps = cse_and_array_hacks( diff --git a/src/systems/systemstructure.jl b/src/systems/systemstructure.jl index ae4d21f225..b98bb8c616 100644 --- a/src/systems/systemstructure.jl +++ b/src/systems/systemstructure.jl @@ -140,14 +140,14 @@ get_fullvars(ts::TransformationState) = ts.fullvars has_equations(::TransformationState) = true Base.@kwdef mutable struct SystemStructure - """Maps the (index of) a variable to the (index of) the variable describing its derivative.""" + """Maps the index of variable x to the index of variable D(x).""" var_to_diff::DiffGraph - """Maps the (index of) an equation.""" + """Maps the index of an equation.""" eq_to_diff::DiffGraph # Can be access as # `graph` to automatically look at the bipartite graph # or as `torn` to assert that tearing has run. - """Graph that maps equations to variables that appear in them.""" + """Graph that connects equations to variables that appear in them.""" graph::BipartiteGraph{Int, Nothing} """Graph that connects equations to the variable they will be solved for during simplification.""" solvable_graph::Union{BipartiteGraph{Int, Nothing}, Nothing} @@ -464,15 +464,10 @@ function lower_order_var(dervar, t) diffvar end -""" - Shift variable x by the largest shift s such that x(k-s) appears in the system of equations. - The lowest-shift term will have. -""" function shift_discrete_system(ts::TearingState) @unpack fullvars, sys = ts discvars = OrderedSet() eqs = equations(sys) - for eq in eqs vars!(discvars, eq; op = Union{Sample, Hold}) end From 66266e410c88a61a9c4e6af1c8c829433d5b4f76 Mon Sep 17 00:00:00 2001 From: vyudu Date: Mon, 10 Feb 2025 19:10:34 -0500 Subject: [PATCH 0805/2176] better comments --- src/structural_transformation/symbolics_tearing.jl | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/structural_transformation/symbolics_tearing.jl b/src/structural_transformation/symbolics_tearing.jl index eed7bc3e29..5707ac8386 100644 --- a/src/structural_transformation/symbolics_tearing.jl +++ b/src/structural_transformation/symbolics_tearing.jl @@ -337,11 +337,19 @@ variables and equations, don't add them when they already exist. Documenting the differences to structural simplification for discrete systems: -In discrete systems the lowest-order term is Shift(t, k)(x(t)), instead of x(t). -We want to substitute the k-1 lowest order terms instead of the k-1 highest order terms. +In discrete systems everything gets shifted forward a timestep by `shift_discrete_system` +in order to properly generate the difference equations. + +In the system x(k) ~ x(k-1) + x(k-2), becomes Shift(t, 1)(x(t)) ~ x(t) + Shift(t, -1)(x(t)) + +The lowest-order term is Shift(t, k)(x(t)), instead of x(t). +As such we actually want dummy variables for the k-1 lowest order terms instead of the k-1 highest order terms. -In the system x(k) ~ x(k-1) + x(k-2), we want to lower Shift(t, -1)(x(t)) -> x\_{t-1}(t) + +Since Shift(t, -1)(x) is not a derivative, it is directly substituted in `fullvars`. No equation or variable is added for it. + +For ODESystems D(D(D(x))) in equations is recursively substituted as D(x) ~ x_t, D(x_t) ~ x_tt, etc. The analogue for discrete systems, Shift(t, 1)(Shift(t,1)(Shift(t,1)(Shift(t, -3)(x(t))))) does not actually appear. So `total_sub` in generate_system_equations` is directly initialized with all of the lowered variables `Shift(t, -3)(x) -> x_t-3(t)`, etc. =# """ Generate new derivative variables for the system. From ac737c0e07a15d37753c4698bb46062b3a3f19a5 Mon Sep 17 00:00:00 2001 From: vyudu Date: Mon, 10 Feb 2025 19:21:56 -0500 Subject: [PATCH 0806/2176] init --- docs/src/systems/DiscreteSystem.md | 28 +++++++++++++++++++ .../discrete_system/discrete_system.jl | 13 +++++++++ 2 files changed, 41 insertions(+) create mode 100644 docs/src/systems/DiscreteSystem.md diff --git a/docs/src/systems/DiscreteSystem.md b/docs/src/systems/DiscreteSystem.md new file mode 100644 index 0000000000..dcb98ca1cf --- /dev/null +++ b/docs/src/systems/DiscreteSystem.md @@ -0,0 +1,28 @@ +# DiscreteSystem + +## System Constructors + +```@docs +DiscreteSystem +``` + +## Composition and Accessor Functions + +- `get_eqs(sys)` or `equations(sys)`: The equations that define the discrete system. +- `get_unknowns(sys)` or `unknowns(sys)`: The set of unknowns in the discrete system. +- `get_ps(sys)` or `parameters(sys)`: The parameters of the discrete system. +- `get_iv(sys)`: The independent variable of the discrete system +- `discrete_events(sys)`: The set of discrete events in the discrete system. + +## Transformations + +```@docs; canonical=false +structural_simplify +``` + +## Problem Constructors + +```@docs; canonical=false +DiscreteProblem(sys::DiscreteSystem, u0map, tspan) +DiscreteFunction(sys::DiscreteSystem, args...) +``` diff --git a/src/systems/discrete_system/discrete_system.jl b/src/systems/discrete_system/discrete_system.jl index bd81255706..cd6ff45b6c 100644 --- a/src/systems/discrete_system/discrete_system.jl +++ b/src/systems/discrete_system/discrete_system.jl @@ -323,6 +323,19 @@ end function SciMLBase.DiscreteFunction{false}(sys::DiscreteSystem, args...; kwargs...) DiscreteFunction{false, SciMLBase.FullSpecialize}(sys, args...; kwargs...) end + +""" +```julia +SciMLBase.DiscreteFunction{iip}(sys::DiscreteSystem, + dvs = unknowns(sys), + ps = parameters(sys); + kwargs...) where {iip} +``` + +Create an `DiscreteFunction` from the [`DiscreteSystem`](@ref). The arguments `dvs` and `ps` +are used to set the order of the dependent variable and parameter vectors, +respectively. +""" function SciMLBase.DiscreteFunction{iip, specialize}( sys::DiscreteSystem, dvs = unknowns(sys), From 992ec21e4f077f4e81837a6fdd3430aa16fd6add Mon Sep 17 00:00:00 2001 From: vyudu Date: Mon, 10 Feb 2025 19:24:42 -0500 Subject: [PATCH 0807/2176] up --- docs/pages.jl | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/docs/pages.jl b/docs/pages.jl index 82cc23d4a8..6e18f403dd 100644 --- a/docs/pages.jl +++ b/docs/pages.jl @@ -41,7 +41,8 @@ pages = [ "systems/JumpSystem.md", "systems/NonlinearSystem.md", "systems/OptimizationSystem.md", - "systems/PDESystem.md"], + "systems/PDESystem.md", + "systems/DiscreteSystem.md"], "comparison.md", "internals.md" ] From 1ab62463adb298386145f57933f4ccd4667d90d1 Mon Sep 17 00:00:00 2001 From: vyudu Date: Mon, 10 Feb 2025 19:30:56 -0500 Subject: [PATCH 0808/2176] fix unassigned indexing --- src/structural_transformation/symbolics_tearing.jl | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/structural_transformation/symbolics_tearing.jl b/src/structural_transformation/symbolics_tearing.jl index 5707ac8386..48f9141dbd 100644 --- a/src/structural_transformation/symbolics_tearing.jl +++ b/src/structural_transformation/symbolics_tearing.jl @@ -512,10 +512,10 @@ function generate_system_equations!(state::TearingState, neweqs, var_eq_matching # Non-solvable equations become algebraic equations. for ieq in eqs iv = eq_var_matching[ieq] - var = fullvars[iv] eq = neweqs[ieq] if is_solvable(ieq, iv) && isdervar(iv) + var = fullvars[iv] isnothing(D) && throw(UnexpectedDifferentialError(equations(sys)[ieq])) order, lv = var_order(iv, diff_to_var) dx = D(simplify_shifts(fullvars[lv])) @@ -530,6 +530,7 @@ function generate_system_equations!(state::TearingState, neweqs, var_eq_matching push!(diffeq_idxs, ieq) push!(diff_vars, diff_to_var[iv]) elseif is_solvable(ieq, iv) + var = fullvars[iv] neweq = make_solved_equation(var, eq, total_sub; simplify) !isnothing(neweq) && begin push!(solved_eqs, neweq) @@ -537,7 +538,7 @@ function generate_system_equations!(state::TearingState, neweqs, var_eq_matching push!(solved_vars, iv) end else - neweq = make_algebraic_equation(var, eq, total_sub) + neweq = make_algebraic_equation(eq, total_sub) push!(alge_eqs, neweq) push!(algeeq_idxs, ieq) end @@ -572,7 +573,7 @@ function make_differential_equation(var, dx, eq, total_sub) total_sub; operator = ModelingToolkit.Shift)) end -function make_algebraic_equation(var, eq, total_sub) +function make_algebraic_equation(eq, total_sub) rhs = eq.rhs if !(eq.lhs isa Number && eq.lhs == 0) rhs = eq.rhs - eq.lhs From 19f58341d16910a7367f6e2907d50b089a705dc4 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 4 Feb 2025 19:25:03 +0530 Subject: [PATCH 0809/2176] refactor: default `create_bindings` to false in `build_function_wrapper` --- src/systems/codegen_utils.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/systems/codegen_utils.jl b/src/systems/codegen_utils.jl index fd54ef01ca..bee7564a4d 100644 --- a/src/systems/codegen_utils.jl +++ b/src/systems/codegen_utils.jl @@ -121,7 +121,7 @@ function build_function_wrapper(sys::AbstractSystem, expr, args...; p_start = 2, p_end = is_time_dependent(sys) ? length(args) - 1 : length(args), wrap_delays = is_dde(sys), wrap_code = identity, add_observed = true, filter_observed = Returns(true), - create_bindings = true, output_type = nothing, mkarray = nothing, + create_bindings = false, output_type = nothing, mkarray = nothing, wrap_mtkparameters = true, extra_assignments = Assignment[], kwargs...) isscalar = !(expr isa AbstractArray || symbolic_type(expr) == ArraySymbolic()) # filter observed equations From 698ccc466bf88576545554902c99d4fd18e4b854 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 11 Feb 2025 12:12:17 +0530 Subject: [PATCH 0810/2176] build: bump Symbolics, SymbolicUtils compats --- Project.toml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Project.toml b/Project.toml index c069b49b5a..ac865063f2 100644 --- a/Project.toml +++ b/Project.toml @@ -146,8 +146,8 @@ StaticArrays = "0.10, 0.11, 0.12, 1.0" StochasticDiffEq = "6.72.1" StochasticDelayDiffEq = "1.8.1" SymbolicIndexingInterface = "0.3.37" -SymbolicUtils = "3.10.1" -Symbolics = "6.27" +SymbolicUtils = "3.14" +Symbolics = "6.29" URIs = "1" UnPack = "0.1, 1.0" Unitful = "1.1" From c352c09378d4608b927dd307fb04fdbacbdb4316 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 11 Feb 2025 12:13:00 +0530 Subject: [PATCH 0811/2176] refactor: format --- docs/src/systems/DiscreteSystem.md | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/docs/src/systems/DiscreteSystem.md b/docs/src/systems/DiscreteSystem.md index dcb98ca1cf..5ede50c62a 100644 --- a/docs/src/systems/DiscreteSystem.md +++ b/docs/src/systems/DiscreteSystem.md @@ -6,20 +6,20 @@ DiscreteSystem ``` -## Composition and Accessor Functions - -- `get_eqs(sys)` or `equations(sys)`: The equations that define the discrete system. -- `get_unknowns(sys)` or `unknowns(sys)`: The set of unknowns in the discrete system. -- `get_ps(sys)` or `parameters(sys)`: The parameters of the discrete system. -- `get_iv(sys)`: The independent variable of the discrete system -- `discrete_events(sys)`: The set of discrete events in the discrete system. - +## Composition and Accessor Functions + + - `get_eqs(sys)` or `equations(sys)`: The equations that define the discrete system. + - `get_unknowns(sys)` or `unknowns(sys)`: The set of unknowns in the discrete system. + - `get_ps(sys)` or `parameters(sys)`: The parameters of the discrete system. + - `get_iv(sys)`: The independent variable of the discrete system + - `discrete_events(sys)`: The set of discrete events in the discrete system. + ## Transformations ```@docs; canonical=false structural_simplify ``` - + ## Problem Constructors ```@docs; canonical=false From 079901bd481b8ea4f505761bce6aa476602dd023 Mon Sep 17 00:00:00 2001 From: vyudu Date: Tue, 11 Feb 2025 15:13:17 -0500 Subject: [PATCH 0812/2176] move D, iv into tearing_reassemble --- .../symbolics_tearing.jl | 43 +++++++++---------- 1 file changed, 21 insertions(+), 22 deletions(-) diff --git a/src/structural_transformation/symbolics_tearing.jl b/src/structural_transformation/symbolics_tearing.jl index 48f9141dbd..45430595b6 100644 --- a/src/structural_transformation/symbolics_tearing.jl +++ b/src/structural_transformation/symbolics_tearing.jl @@ -248,11 +248,10 @@ called dummy derivatives. State selection is done. All non-differentiated variables are algebraic variables, and all variables that appear differentiated are differential variables. """ -function substitute_derivatives_algevars!(ts::TearingState, neweqs, var_eq_matching, dummy_sub) +function substitute_derivatives_algevars!(ts::TearingState, neweqs, var_eq_matching, dummy_sub; iv = nothing, D = nothing) @unpack fullvars, sys, structure = ts @unpack solvable_graph, var_to_diff, eq_to_diff, graph = structure diff_to_var = invview(var_to_diff) - iv = ModelingToolkit.has_iv(sys) ? ModelingToolkit.get_iv(sys) : nothing for var in 1:length(fullvars) dv = var_to_diff[var] @@ -361,12 +360,11 @@ Effects on the system structure: - solvable_graph: - var_eq_matching: match D(x) to the added identity equation """ -function generate_derivative_variables!(ts::TearingState, neweqs, var_eq_matching; mm = nothing) +function generate_derivative_variables!(ts::TearingState, neweqs, var_eq_matching; mm = nothing, iv = nothing, D = nothing) @unpack fullvars, sys, structure = ts @unpack solvable_graph, var_to_diff, eq_to_diff, graph = structure eq_var_matching = invview(var_eq_matching) diff_to_var = invview(var_to_diff) - iv = ModelingToolkit.has_iv(sys) ? ModelingToolkit.get_iv(sys) : nothing is_discrete = is_only_discrete(structure) lower_varname = is_discrete ? lower_shift_varname : lower_varname_with_unit linear_eqs = mm === nothing ? Dict{Int, Int}() : @@ -461,27 +459,18 @@ end Solve the solvable equations of the system and generate differential (or discrete) equations in terms of the selected states. """ -function generate_system_equations!(state::TearingState, neweqs, var_eq_matching; simplify = false) +function generate_system_equations!(state::TearingState, neweqs, var_eq_matching; simplify = false, iv = nothing, D = nothing) @unpack fullvars, sys, structure = state @unpack solvable_graph, var_to_diff, eq_to_diff, graph = structure eq_var_matching = invview(var_eq_matching) diff_to_var = invview(var_to_diff) total_sub = Dict() - - if ModelingToolkit.has_iv(sys) - iv = get_iv(sys) - if is_only_discrete(structure) - D = Shift(iv, 1) - for v in fullvars - op = operation(v) - op isa Shift && (op.steps < 0) && (total_sub[v] = lower_shift_varname(v, iv)) - end - else - D = Differential(iv) + if is_only_discrete(structure) + for v in fullvars + op = operation(v) + op isa Shift && (op.steps < 0) && (total_sub[v] = lower_shift_varname(v, iv)) end - else - iv = D = nothing - end + end # if var is like D(x) or Shift(t, 1)(x) isdervar = let diff_to_var = diff_to_var @@ -727,13 +716,23 @@ function tearing_reassemble(state::TearingState, var_eq_matching, neweqs = collect(equations(state)) dummy_sub = Dict() + if ModelingToolkit.has_iv(state.sys) + iv = get_iv(state.sys) + if !is_only_discrete(state.structure) + D = Differential(iv) + else + D = Shift(iv, 1) + end + iv = D = nothing + end + # Structural simplification - substitute_derivatives_algevars!(state, neweqs, var_eq_matching, dummy_sub) + substitute_derivatives_algevars!(state, neweqs, var_eq_matching, dummy_sub; iv, D) - generate_derivative_variables!(state, neweqs, var_eq_matching; mm) + generate_derivative_variables!(state, neweqs, var_eq_matching; mm, iv, D) neweqs, solved_eqs, eq_ordering, var_ordering, nelim_eq, nelim_var = - generate_system_equations!(state, neweqs, var_eq_matching; simplify) + generate_system_equations!(state, neweqs, var_eq_matching; simplify, iv, D) state = reorder_vars!(state, var_eq_matching, eq_ordering, var_ordering, nelim_eq, nelim_var) From 9217c62941226cd8dc24a80fe3723810f2463ab5 Mon Sep 17 00:00:00 2001 From: vyudu Date: Tue, 11 Feb 2025 15:29:46 -0500 Subject: [PATCH 0813/2176] refactor iv --- src/structural_transformation/symbolics_tearing.jl | 1 + 1 file changed, 1 insertion(+) diff --git a/src/structural_transformation/symbolics_tearing.jl b/src/structural_transformation/symbolics_tearing.jl index 45430595b6..883166cfa8 100644 --- a/src/structural_transformation/symbolics_tearing.jl +++ b/src/structural_transformation/symbolics_tearing.jl @@ -723,6 +723,7 @@ function tearing_reassemble(state::TearingState, var_eq_matching, else D = Shift(iv, 1) end + else iv = D = nothing end From 54910988509cfdf2f65ed8b8d24f565b3dab8584 Mon Sep 17 00:00:00 2001 From: vyudu Date: Tue, 11 Feb 2025 16:13:47 -0500 Subject: [PATCH 0814/2176] fix comment for eq_to_diff --- src/systems/systemstructure.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/systems/systemstructure.jl b/src/systems/systemstructure.jl index b98bb8c616..6fee78cfd6 100644 --- a/src/systems/systemstructure.jl +++ b/src/systems/systemstructure.jl @@ -142,7 +142,7 @@ has_equations(::TransformationState) = true Base.@kwdef mutable struct SystemStructure """Maps the index of variable x to the index of variable D(x).""" var_to_diff::DiffGraph - """Maps the index of an equation.""" + """Maps the index of an algebraic equation to the index of the equation it is differentiated into.""" eq_to_diff::DiffGraph # Can be access as # `graph` to automatically look at the bipartite graph From 5878ad67ef6a78ad55034d8138a6fee741a9e7d6 Mon Sep 17 00:00:00 2001 From: vyudu Date: Tue, 11 Feb 2025 16:51:06 -0500 Subject: [PATCH 0815/2176] fix total_sub --- src/structural_transformation/symbolics_tearing.jl | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/structural_transformation/symbolics_tearing.jl b/src/structural_transformation/symbolics_tearing.jl index 883166cfa8..7cb9798f01 100644 --- a/src/structural_transformation/symbolics_tearing.jl +++ b/src/structural_transformation/symbolics_tearing.jl @@ -464,6 +464,7 @@ function generate_system_equations!(state::TearingState, neweqs, var_eq_matching @unpack solvable_graph, var_to_diff, eq_to_diff, graph = structure eq_var_matching = invview(var_eq_matching) diff_to_var = invview(var_to_diff) + total_sub = Dict() if is_only_discrete(structure) for v in fullvars @@ -515,6 +516,7 @@ function generate_system_equations!(state::TearingState, neweqs, var_eq_matching rem_edge!(graph, e, iv) end + total_sub[simplify_shifts(neweq.lhs)] = neweq.rhs push!(diff_eqs, neweq) push!(diffeq_idxs, ieq) push!(diff_vars, diff_to_var[iv]) From cb64a00cd58209f5c02cab7ae1750dc8e258342c Mon Sep 17 00:00:00 2001 From: vyudu Date: Tue, 11 Feb 2025 22:43:18 -0500 Subject: [PATCH 0816/2176] refactor: sort systems tests into new folder --- test/{ => systems}/abstractsystem.jl | 0 test/{ => systems}/bigsystem.jl | 0 test/{ => systems}/discrete_system.jl | 0 test/{ => systems}/implicit_discrete_system.jl | 0 test/{ => systems}/initializationsystem.jl | 0 test/{ => systems}/jumpsystem.jl | 0 test/{ => systems}/nonlinearsystem.jl | 0 test/{ => systems}/odesystem.jl | 0 test/{ => systems}/optimizationsystem.jl | 0 test/{pde.jl => systems/pdesystem.jl} | 0 test/{ => systems}/sdesystem.jl | 0 test/{ => systems}/steadystatesystems.jl | 0 12 files changed, 0 insertions(+), 0 deletions(-) rename test/{ => systems}/abstractsystem.jl (100%) rename test/{ => systems}/bigsystem.jl (100%) rename test/{ => systems}/discrete_system.jl (100%) rename test/{ => systems}/implicit_discrete_system.jl (100%) rename test/{ => systems}/initializationsystem.jl (100%) rename test/{ => systems}/jumpsystem.jl (100%) rename test/{ => systems}/nonlinearsystem.jl (100%) rename test/{ => systems}/odesystem.jl (100%) rename test/{ => systems}/optimizationsystem.jl (100%) rename test/{pde.jl => systems/pdesystem.jl} (100%) rename test/{ => systems}/sdesystem.jl (100%) rename test/{ => systems}/steadystatesystems.jl (100%) diff --git a/test/abstractsystem.jl b/test/systems/abstractsystem.jl similarity index 100% rename from test/abstractsystem.jl rename to test/systems/abstractsystem.jl diff --git a/test/bigsystem.jl b/test/systems/bigsystem.jl similarity index 100% rename from test/bigsystem.jl rename to test/systems/bigsystem.jl diff --git a/test/discrete_system.jl b/test/systems/discrete_system.jl similarity index 100% rename from test/discrete_system.jl rename to test/systems/discrete_system.jl diff --git a/test/implicit_discrete_system.jl b/test/systems/implicit_discrete_system.jl similarity index 100% rename from test/implicit_discrete_system.jl rename to test/systems/implicit_discrete_system.jl diff --git a/test/initializationsystem.jl b/test/systems/initializationsystem.jl similarity index 100% rename from test/initializationsystem.jl rename to test/systems/initializationsystem.jl diff --git a/test/jumpsystem.jl b/test/systems/jumpsystem.jl similarity index 100% rename from test/jumpsystem.jl rename to test/systems/jumpsystem.jl diff --git a/test/nonlinearsystem.jl b/test/systems/nonlinearsystem.jl similarity index 100% rename from test/nonlinearsystem.jl rename to test/systems/nonlinearsystem.jl diff --git a/test/odesystem.jl b/test/systems/odesystem.jl similarity index 100% rename from test/odesystem.jl rename to test/systems/odesystem.jl diff --git a/test/optimizationsystem.jl b/test/systems/optimizationsystem.jl similarity index 100% rename from test/optimizationsystem.jl rename to test/systems/optimizationsystem.jl diff --git a/test/pde.jl b/test/systems/pdesystem.jl similarity index 100% rename from test/pde.jl rename to test/systems/pdesystem.jl diff --git a/test/sdesystem.jl b/test/systems/sdesystem.jl similarity index 100% rename from test/sdesystem.jl rename to test/systems/sdesystem.jl diff --git a/test/steadystatesystems.jl b/test/systems/steadystatesystems.jl similarity index 100% rename from test/steadystatesystems.jl rename to test/systems/steadystatesystems.jl From a901d097ac630ef6c553cbe947528af617a8ab64 Mon Sep 17 00:00:00 2001 From: vyudu Date: Tue, 11 Feb 2025 22:44:42 -0500 Subject: [PATCH 0817/2176] revert --- test/{systems => }/abstractsystem.jl | 0 test/{systems => }/bigsystem.jl | 0 test/{systems => }/discrete_system.jl | 0 test/{systems => }/implicit_discrete_system.jl | 0 test/{systems => }/initializationsystem.jl | 0 test/{systems => }/jumpsystem.jl | 0 test/{systems => }/nonlinearsystem.jl | 0 test/{systems => }/odesystem.jl | 0 test/{systems => }/optimizationsystem.jl | 0 test/{systems => }/pdesystem.jl | 0 test/{systems => }/sdesystem.jl | 0 test/{systems => }/steadystatesystems.jl | 0 12 files changed, 0 insertions(+), 0 deletions(-) rename test/{systems => }/abstractsystem.jl (100%) rename test/{systems => }/bigsystem.jl (100%) rename test/{systems => }/discrete_system.jl (100%) rename test/{systems => }/implicit_discrete_system.jl (100%) rename test/{systems => }/initializationsystem.jl (100%) rename test/{systems => }/jumpsystem.jl (100%) rename test/{systems => }/nonlinearsystem.jl (100%) rename test/{systems => }/odesystem.jl (100%) rename test/{systems => }/optimizationsystem.jl (100%) rename test/{systems => }/pdesystem.jl (100%) rename test/{systems => }/sdesystem.jl (100%) rename test/{systems => }/steadystatesystems.jl (100%) diff --git a/test/systems/abstractsystem.jl b/test/abstractsystem.jl similarity index 100% rename from test/systems/abstractsystem.jl rename to test/abstractsystem.jl diff --git a/test/systems/bigsystem.jl b/test/bigsystem.jl similarity index 100% rename from test/systems/bigsystem.jl rename to test/bigsystem.jl diff --git a/test/systems/discrete_system.jl b/test/discrete_system.jl similarity index 100% rename from test/systems/discrete_system.jl rename to test/discrete_system.jl diff --git a/test/systems/implicit_discrete_system.jl b/test/implicit_discrete_system.jl similarity index 100% rename from test/systems/implicit_discrete_system.jl rename to test/implicit_discrete_system.jl diff --git a/test/systems/initializationsystem.jl b/test/initializationsystem.jl similarity index 100% rename from test/systems/initializationsystem.jl rename to test/initializationsystem.jl diff --git a/test/systems/jumpsystem.jl b/test/jumpsystem.jl similarity index 100% rename from test/systems/jumpsystem.jl rename to test/jumpsystem.jl diff --git a/test/systems/nonlinearsystem.jl b/test/nonlinearsystem.jl similarity index 100% rename from test/systems/nonlinearsystem.jl rename to test/nonlinearsystem.jl diff --git a/test/systems/odesystem.jl b/test/odesystem.jl similarity index 100% rename from test/systems/odesystem.jl rename to test/odesystem.jl diff --git a/test/systems/optimizationsystem.jl b/test/optimizationsystem.jl similarity index 100% rename from test/systems/optimizationsystem.jl rename to test/optimizationsystem.jl diff --git a/test/systems/pdesystem.jl b/test/pdesystem.jl similarity index 100% rename from test/systems/pdesystem.jl rename to test/pdesystem.jl diff --git a/test/systems/sdesystem.jl b/test/sdesystem.jl similarity index 100% rename from test/systems/sdesystem.jl rename to test/sdesystem.jl diff --git a/test/systems/steadystatesystems.jl b/test/steadystatesystems.jl similarity index 100% rename from test/systems/steadystatesystems.jl rename to test/steadystatesystems.jl From 397b279df53c45a6c923ee89a24125237d4ee44c Mon Sep 17 00:00:00 2001 From: vyudu Date: Tue, 11 Feb 2025 23:08:07 -0500 Subject: [PATCH 0818/2176] add diff_to_var as argument for find_dumplicate_dd, fix substitution of lowest-order variable --- src/structural_transformation/symbolics_tearing.jl | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/structural_transformation/symbolics_tearing.jl b/src/structural_transformation/symbolics_tearing.jl index 7cb9798f01..ec1ff45fd7 100644 --- a/src/structural_transformation/symbolics_tearing.jl +++ b/src/structural_transformation/symbolics_tearing.jl @@ -374,9 +374,9 @@ function generate_derivative_variables!(ts::TearingState, neweqs, var_eq_matchin # derivative is in the system for v in 1:length(var_to_diff) dv = var_to_diff[v] - # For discrete systems, directly substitute lowest-order variable + # For discrete systems, directly substitute lowest-order shift if is_discrete && diff_to_var[v] == nothing - fullvars[v] = lower_varname(fullvars[v], iv) + operation(fullvars[v]) isa Shift && (fullvars[v] = lower_varname(fullvars[v], iv)) end dv isa Int || continue solved = var_eq_matching[dv] isa Int @@ -384,7 +384,7 @@ function generate_derivative_variables!(ts::TearingState, neweqs, var_eq_matchin # If there's `D(x) = x_t` already, update mappings and continue without # adding new equations/variables - dd = find_duplicate_dd(dv, solvable_graph, linear_eqs, mm) + dd = find_duplicate_dd(dv, solvable_graph, diff_to_var, linear_eqs, mm) if !isnothing(dd) dummy_eq, v_t = dd var_to_diff[v_t] = var_to_diff[dv] @@ -412,7 +412,7 @@ end """ Check if there's `D(x) = x_t` already. """ -function find_duplicate_dd(dv, solvable_graph, linear_eqs, mm) +function find_duplicate_dd(dv, solvable_graph, diff_to_var, linear_eqs, mm) for eq in 𝑑neighbors(solvable_graph, dv) mi = get(linear_eqs, eq, 0) iszero(mi) && continue From 77c4aed624b6c92e947bffbfcf376812545facb1 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 21 Jan 2025 14:53:52 +0530 Subject: [PATCH 0819/2176] fix: handle case when parameter object is `nothing` in `GetUpdatedMTKParameters` --- src/systems/problem_utils.jl | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/systems/problem_utils.jl b/src/systems/problem_utils.jl index 81be9c3d9e..0bea6b7a08 100644 --- a/src/systems/problem_utils.jl +++ b/src/systems/problem_utils.jl @@ -455,7 +455,9 @@ struct GetUpdatedMTKParameters{G, S} end function (f::GetUpdatedMTKParameters)(prob, initializesol) - mtkp = copy(parameter_values(prob)) + p = parameter_values(prob) + p === nothing && return nothing + mtkp = copy(p) f.setpunknowns(mtkp, f.getpunknowns(initializesol)) mtkp end From 5c873f766b6937f186838422ff2ea10c98978e55 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 21 Jan 2025 14:54:12 +0530 Subject: [PATCH 0820/2176] feat: update `u0map` and `pmap` in `build_operating_point` --- src/systems/problem_utils.jl | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/systems/problem_utils.jl b/src/systems/problem_utils.jl index 0bea6b7a08..29d821a26b 100644 --- a/src/systems/problem_utils.jl +++ b/src/systems/problem_utils.jl @@ -511,6 +511,9 @@ Construct the operating point of the system from the user-provided `u0map` and ` defaults `defs`, constant equations `cmap` (from `get_cmap(sys)`), unknowns `dvs` and parameters `ps`. Return the operating point as a dictionary, the list of unknowns for which no values can be determined, and the list of parameters for which no values can be determined. + +Also updates `u0map` and `pmap` in-place to contain all the initial conditions in `op`, split +by unknowns and parameters respectively. """ function build_operating_point( u0map::AbstractDict, pmap::AbstractDict, defs::AbstractDict, cmap, dvs, ps) @@ -525,6 +528,17 @@ function build_operating_point( for eq in cmap op[eq.lhs] = eq.rhs end + + empty!(u0map) + empty!(pmap) + for (k, v) in op + k = unwrap(k) + if isparameter(k) + pmap[k] = v + elseif !isconstant(k) + u0map[k] = v + end + end return op, missing_unknowns, missing_pars end From ba21ac58e850fa57713edaf3218c8e5e4897d537 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 21 Jan 2025 14:56:54 +0530 Subject: [PATCH 0821/2176] feat: add dummy initialization parameters instead of hard constraints --- src/systems/diffeqs/abstractodesystem.jl | 3 +- src/systems/nonlinear/initializesystem.jl | 66 +++++++++++++++++++---- 2 files changed, 59 insertions(+), 10 deletions(-) diff --git a/src/systems/diffeqs/abstractodesystem.jl b/src/systems/diffeqs/abstractodesystem.jl index ea55dce388..68e98ee4f2 100644 --- a/src/systems/diffeqs/abstractodesystem.jl +++ b/src/systems/diffeqs/abstractodesystem.jl @@ -1240,7 +1240,8 @@ function InitializationProblem{iip, specialize}(sys::AbstractSystem, meta = get_metadata(isys) if meta isa InitializationSystemMetadata - @set! isys.metadata.oop_reconstruct_u0_p = ReconstructInitializeprob(sys, isys) + @set! isys.metadata.oop_reconstruct_u0_p = ReconstructInitializeprob( + sys, isys; remap = meta.new_params) end ts = get_tearing_state(isys) diff --git a/src/systems/nonlinear/initializesystem.jl b/src/systems/nonlinear/initializesystem.jl index 68c9d62f62..433c4f9ecf 100644 --- a/src/systems/nonlinear/initializesystem.jl +++ b/src/systems/nonlinear/initializesystem.jl @@ -27,6 +27,38 @@ function generate_initializesystem(sys::AbstractSystem; guesses = merge(get_guesses(sys), additional_guesses) idxs_diff = isdiffeq.(eqs) + # PREPROCESSING + # for initial conditions of the form `var => constant`, we instead turn them into + # `var ~ var0` where `var0` is a new parameter, and make `update_initializeprob!` + # update `initializeprob.ps[var0] = prob[var]`. + + # map parameters in `initprob` which need to be updated in `update_initializeprob!` + # to the corresponding expressions that determine their values + new_params = Dict() + u0map = copy(anydict(u0map)) + pmap = copy(anydict(pmap)) + for (k, v) in u0map + k = unwrap(k) + is_variable(sys, k) || continue + (symbolic_type(v) == NotSymbolic() && !is_array_of_symbolics(v)) || continue + newvar = get_initial_value_parameter(k) + new_params[newvar] = k + pmap[newvar] = v + u0map[k] = newvar + defs[newvar] = v + end + for (k, v) in pmap + k = unwrap(k) + is_parameter_solvable(k, pmap, defs, guesses) || continue + (v !== missing && symbolic_type(v) == NotSymbolic() && !is_array_of_symbolics(v)) || + continue + newvar = get_initial_value_parameter(k) + new_params[newvar] = k + pmap[newvar] = v + pmap[k] = newvar + defs[newvar] = v + end + # 1) Use algebraic equations of time-dependent systems as initialization constraints if has_iv(sys) idxs_alge = .!idxs_diff @@ -103,10 +135,6 @@ function generate_initializesystem(sys::AbstractSystem; # 5) process parameters as initialization unknowns paramsubs = Dict() - if pmap isa SciMLBase.NullParameters - pmap = Dict() - end - pmap = todict(pmap) for p in parameters(sys) if is_parameter_solvable(p, pmap, defs, guesses) # If either of them are `missing` the parameter is an unknown @@ -180,6 +208,7 @@ function generate_initializesystem(sys::AbstractSystem; # parameters do not include ones that became initialization unknowns pars = Vector{SymbolicParam}(filter(p -> !haskey(paramsubs, p), parameters(sys))) + pars = [pars; map(unwrap, collect(keys(new_params)))] is_time_dependent(sys) && push!(pars, get_iv(sys)) if is_time_dependent(sys) @@ -214,7 +243,7 @@ function generate_initializesystem(sys::AbstractSystem; end meta = InitializationSystemMetadata( anydict(u0map), anydict(pmap), additional_guesses, - additional_initialization_eqs, extra_metadata, nothing) + additional_initialization_eqs, extra_metadata, nothing, new_params) return NonlinearSystem(eqs_ics, vars, pars; @@ -226,15 +255,33 @@ function generate_initializesystem(sys::AbstractSystem; kwargs...) end +""" + $(TYPEDSIGNATURES) + +Get a new symbolic variable of the same type and size as `sym`, which is a parameter. +""" +function get_initial_value_parameter(sym) + sym = default_toterm(unwrap(sym)) + name = hasname(sym) ? getname(sym) : Symbol(sym) + if iscall(sym) && operation(sym) === getindex + name = Symbol(name, :_, join(arguments(sym)[2:end], "_")) + end + name = Symbol(name, :ₘₜₖ_₀) + newvar = unwrap(similar_variable(sym, name; use_gensym = false)) + return toparam(newvar) +end + struct ReconstructInitializeprob getter::Any setter::Any end -function ReconstructInitializeprob(srcsys::AbstractSystem, dstsys::AbstractSystem) - syms = reduce(vcat, reorder_parameters(dstsys, parameters(dstsys)); init = []) - getter = getu(srcsys, syms) - setter = setp_oop(dstsys, syms) +function ReconstructInitializeprob( + srcsys::AbstractSystem, dstsys::AbstractSystem; remap = Dict()) + syms = [unknowns(dstsys); + reduce(vcat, reorder_parameters(dstsys, parameters(dstsys)); init = [])] + getter = getu(srcsys, map(x -> get(remap, x, x), syms)) + setter = setsym_oop(dstsys, syms) return ReconstructInitializeprob(getter, setter) end @@ -266,6 +313,7 @@ struct InitializationSystemMetadata additional_initialization_eqs::Vector{Equation} extra_metadata::NamedTuple oop_reconstruct_u0_p::Union{Nothing, ReconstructInitializeprob} + new_params::Dict{Any, Any} end function get_possibly_array_fallback_singletons(varmap, p) From 127a0c8d449478a2457ded43fa8d6670b49db34a Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 21 Jan 2025 14:58:13 +0530 Subject: [PATCH 0822/2176] feat: always build initialization problem, handle dummy parameters --- src/systems/problem_utils.jl | 147 ++++++++++++++++++++--------------- 1 file changed, 86 insertions(+), 61 deletions(-) diff --git a/src/systems/problem_utils.jl b/src/systems/problem_utils.jl index 29d821a26b..24e0559d98 100644 --- a/src/systems/problem_utils.jl +++ b/src/systems/problem_utils.jl @@ -556,75 +556,98 @@ function maybe_build_initialization_problem( sys::AbstractSystem, op::AbstractDict, u0map, pmap, t, defs, guesses, missing_unknowns; implicit_dae = false, kwargs...) guesses = merge(ModelingToolkit.guesses(sys), todict(guesses)) - has_observed_u0s = any( - k -> has_observed_with_lhs(sys, k) || has_parameter_dependency_with_lhs(sys, k), - keys(op)) - solvablepars = [p - for p in parameters(sys) - if is_parameter_solvable(p, pmap, defs, guesses)] - has_dependent_unknowns = any(unknowns(sys)) do sym - val = get(op, sym, nothing) - val === nothing && return false - return symbolic_type(val) != NotSymbolic() || is_array_of_symbolics(val) - end - if (((implicit_dae || has_observed_u0s || !isempty(missing_unknowns) || - !isempty(solvablepars) || has_dependent_unknowns) && - (!has_tearing_state(sys) || get_tearing_state(sys) !== nothing)) || - !isempty(initialization_equations(sys))) && - (!is_time_dependent(sys) || t !== nothing) - initializeprob = ModelingToolkit.InitializationProblem( - sys, t, u0map, pmap; guesses, kwargs...) - - if is_time_dependent(sys) - all_init_syms = Set(all_symbols(initializeprob)) - solved_unknowns = filter(var -> var in all_init_syms, unknowns(sys)) - initializeprobmap = getu(initializeprob, solved_unknowns) - else - initializeprobmap = nothing - end - punknowns = [p - for p in all_variable_symbols(initializeprob) - if is_parameter(sys, p)] - if isempty(punknowns) - initializeprobpmap = nothing - else - getpunknowns = getu(initializeprob, punknowns) - setpunknowns = setp(sys, punknowns) - initializeprobpmap = GetUpdatedMTKParameters(getpunknowns, setpunknowns) - end + if t === nothing && is_time_dependent(sys) + t = 0.0 + end - reqd_syms = parameter_symbols(initializeprob) - # we still want the `initialization_data` because it helps with `remake` - if initializeprobmap === nothing && initializeprobpmap === nothing - update_initializeprob! = nothing - else - update_initializeprob! = UpdateInitializeprob( - getu(sys, reqd_syms), setu(initializeprob, reqd_syms)) + initializeprob = ModelingToolkit.InitializationProblem( + sys, t, u0map, pmap; guesses, kwargs...) + meta = get_metadata(initializeprob.f.sys) + new_params = meta.new_params + + if is_time_dependent(sys) + all_init_syms = Set(all_symbols(initializeprob)) + solved_unknowns = filter(var -> var in all_init_syms, unknowns(sys)) + initializeprobmap = getu(initializeprob, solved_unknowns) + else + initializeprobmap = nothing + end + + punknowns = [p + for p in all_variable_symbols(initializeprob) + if is_parameter(sys, p)] + if isempty(punknowns) + initializeprobpmap = nothing + else + getpunknowns = getu(initializeprob, punknowns) + setpunknowns = setp(sys, punknowns) + initializeprobpmap = GetUpdatedMTKParameters(getpunknowns, setpunknowns) + end + + reqd_syms = parameter_symbols(initializeprob) + sources = [get(new_params, x, x) for x in reqd_syms] + # we still want the `initialization_data` because it helps with `remake` + if initializeprobmap === nothing && initializeprobpmap === nothing + update_initializeprob! = nothing + else + update_initializeprob! = UpdateInitializeprob( + getu(sys, sources), setu(initializeprob, reqd_syms)) + end + + for p in punknowns + p = unwrap(p) + stype = symtype(p) + op[p] = get_temporary_value(p) + if iscall(p) && operation(p) === getindex + arrp = arguments(p)[1] + op[arrp] = collect(arrp) end + end - for p in punknowns - p = unwrap(p) - stype = symtype(p) - op[p] = get_temporary_value(p) - if iscall(p) && operation(p) === getindex - arrp = arguments(p)[1] - op[arrp] = collect(arrp) - end + if is_time_dependent(sys) + for v in missing_unknowns + op[v] = zero_var(v) end + empty!(missing_unknowns) + end - if is_time_dependent(sys) - for v in missing_unknowns - op[v] = zero_var(v) - end - empty!(missing_unknowns) + for v in missing_unknowns + op[v] = zero_var(v) + end + empty!(missing_unknowns) + return (; + initialization_data = SciMLBase.OverrideInitData( + initializeprob, update_initializeprob!, initializeprobmap, + initializeprobpmap)) +end + +""" + $(TYPEDSIGNATURES) + +Remove all entries in `varmap` whose keys are not variables/parameters in `sys`, +substituting their values into the rest of `varmap`. Modifies `varmap` in place. +""" +function substitute_extra_variables!(sys::AbstractSystem, varmap::AbstractDict) + tmpmap = anydict() + syms = all_symbols(sys) + for (k, v) in varmap + k = unwrap(k) + if any(isequal(k), syms) || + iscall(k) && + (operation(k) == getindex && any(isequal(arguments(k)[1]), syms) || + operation(k) isa Differential) || isconstant(k) + continue end - return (; - initialization_data = SciMLBase.OverrideInitData( - initializeprob, update_initializeprob!, initializeprobmap, - initializeprobpmap)) + tmpmap[k] = v + end + for k in keys(tmpmap) + delete!(varmap, k) + end + for (k, v) in varmap + varmap[k] = unwrap(fixpoint_sub(v, tmpmap)) end - return (;) + return varmap end """ @@ -716,6 +739,8 @@ function process_SciMLProblem( op, missing_unknowns, missing_pars = build_operating_point( u0map, pmap, defs, cmap, dvs, ps) + substitute_extra_variables!(sys, u0map) + substitute_extra_variables!(sys, pmap) if build_initializeprob kws = maybe_build_initialization_problem( From a7b73ee52df579ac3a79dbd156a7a021633e6efd Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 21 Jan 2025 14:58:32 +0530 Subject: [PATCH 0823/2176] refactor: allow disabling `gensym` in `similar_variable` --- src/utils.jl | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/utils.jl b/src/utils.jl index cf49d9f445..38cfcd1228 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -1174,9 +1174,13 @@ end Create an anonymous symbolic variable of the same shape, size and symtype as `var`, with name `gensym(name)`. Does not support unsized array symbolics. + +If `use_gensym == false`, will not `gensym` the name. """ -function similar_variable(var::BasicSymbolic, name = :anon) - name = gensym(name) +function similar_variable(var::BasicSymbolic, name = :anon; use_gensym = true) + if use_gensym + name = gensym(name) + end stype = symtype(var) sym = Symbolics.variable(name; T = stype) if size(var) !== () From 3d88375f0ea2c4fdfd8fb0e460cf5c9eab3f86ac Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 21 Jan 2025 14:22:04 +0530 Subject: [PATCH 0824/2176] feat: count shift equations as differential equations --- src/utils.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utils.jl b/src/utils.jl index 38cfcd1228..3f52b0676f 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -358,7 +358,7 @@ isoperator(expr, op) = iscall(expr) && operation(expr) isa op isoperator(op) = expr -> isoperator(expr, op) isdifferential(expr) = isoperator(expr, Differential) -isdiffeq(eq) = isdifferential(eq.lhs) +isdiffeq(eq) = isdifferential(eq.lhs) || isoperator(eq.lhs, Shift) isvariable(x::Num)::Bool = isvariable(value(x)) function isvariable(x)::Bool From d9a1872743545f3edbf71c354d4bd3add18ec2f9 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 21 Jan 2025 14:22:25 +0530 Subject: [PATCH 0825/2176] test: provide missing parameters to `NonlinearProblem` test --- test/nonlinearsystem.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/nonlinearsystem.jl b/test/nonlinearsystem.jl index cbd95a50d3..c6318b1314 100644 --- a/test/nonlinearsystem.jl +++ b/test/nonlinearsystem.jl @@ -103,7 +103,7 @@ eqs1 = [ lorenz = name -> NonlinearSystem(eqs1, [x, y, z, u, F], [σ, ρ, β], name = name) lorenz1 = lorenz(:lorenz1) -@test_throws ArgumentError NonlinearProblem(complete(lorenz1), zeros(5)) +@test_throws ArgumentError NonlinearProblem(complete(lorenz1), zeros(5), zeros(3)) lorenz2 = lorenz(:lorenz2) @named connected = NonlinearSystem( [s ~ a + lorenz1.x From 420a4972386aba5f7eca63615d39b752d8fa0990 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 21 Jan 2025 14:23:11 +0530 Subject: [PATCH 0826/2176] feat: add `force_time_independent` keyword to `InitializationProblem` --- src/systems/diffeqs/abstractodesystem.jl | 29 +++++++++++++++++------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/src/systems/diffeqs/abstractodesystem.jl b/src/systems/diffeqs/abstractodesystem.jl index 68e98ee4f2..2c07974252 100644 --- a/src/systems/diffeqs/abstractodesystem.jl +++ b/src/systems/diffeqs/abstractodesystem.jl @@ -1220,22 +1220,35 @@ function InitializationProblem{iip, specialize}(sys::AbstractSystem, check_units = true, use_scc = true, allow_incomplete = false, + force_time_independent = false, kwargs...) where {iip, specialize} if !iscomplete(sys) error("A completed system is required. Call `complete` or `structural_simplify` on the system before creating an `ODEProblem`") end if isempty(u0map) && get_initializesystem(sys) !== nothing isys = get_initializesystem(sys; initialization_eqs, check_units) + simplify_system = false elseif isempty(u0map) && get_initializesystem(sys) === nothing - isys = structural_simplify( - generate_initializesystem( - sys; initialization_eqs, check_units, pmap = parammap, - guesses, extra_metadata = (; use_scc)); fully_determined) + isys = generate_initializesystem( + sys; initialization_eqs, check_units, pmap = parammap, + guesses, extra_metadata = (; use_scc)) + simplify_system = true else - isys = structural_simplify( - generate_initializesystem( - sys; u0map, initialization_eqs, check_units, - pmap = parammap, guesses, extra_metadata = (; use_scc)); fully_determined) + isys = generate_initializesystem( + sys; u0map, initialization_eqs, check_units, + pmap = parammap, guesses, extra_metadata = (; use_scc)) + simplify_system = true + end + + # useful for `SteadyStateProblem` since `f` has to be autonomous and the + # initialization should be too + if force_time_independent + idx = findfirst(isequal(get_iv(sys)), get_ps(isys)) + idx === nothing || deleteat!(get_ps(isys), idx) + end + + if simplify_system + isys = structural_simplify(isys; fully_determined) end meta = get_metadata(isys) From 3153677258da5463e5fb6fbfceeb54da93a37024 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 21 Jan 2025 14:23:45 +0530 Subject: [PATCH 0827/2176] feat: expose `force_time_independent` through `process_SciMLProblem` --- src/systems/problem_utils.jl | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/systems/problem_utils.jl b/src/systems/problem_utils.jl index 24e0559d98..add3fd4405 100644 --- a/src/systems/problem_utils.jl +++ b/src/systems/problem_utils.jl @@ -704,6 +704,8 @@ Keyword arguments: other to attempt to arrive at a numeric value. - `use_scc`: Whether to use `SCCNonlinearProblem` for initialization if the system is fully determined. +- `force_initialization_time_independent`: Whether to force the initialization to not use + the independent variable of `sys`. All other keyword arguments are passed as-is to `constructor`. """ @@ -717,7 +719,8 @@ function process_SciMLProblem( symbolic_u0 = false, warn_cyclic_dependency = false, circular_dependency_max_cycle_length = length(all_symbols(sys)), circular_dependency_max_cycles = 10, - substitution_limit = 100, use_scc = true, kwargs...) + substitution_limit = 100, use_scc = true, + force_initialization_time_independent = false, kwargs...) dvs = unknowns(sys) ps = parameters(sys) iv = has_iv(sys) ? get_iv(sys) : nothing @@ -748,7 +751,8 @@ function process_SciMLProblem( implicit_dae, warn_initialize_determined, initialization_eqs, eval_expression, eval_module, fully_determined, warn_cyclic_dependency, check_units = check_initialization_units, - circular_dependency_max_cycle_length, circular_dependency_max_cycles, use_scc) + circular_dependency_max_cycle_length, circular_dependency_max_cycles, use_scc, + force_time_independent = force_initialization_time_independent) kwargs = merge(kwargs, kws) end From 131ac816c0bbd71a8505b21f82587b217382161b Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 21 Jan 2025 14:24:11 +0530 Subject: [PATCH 0828/2176] fix: remove delayed terms in initialization system of DDEs --- src/systems/nonlinear/initializesystem.jl | 49 +++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/src/systems/nonlinear/initializesystem.jl b/src/systems/nonlinear/initializesystem.jl index 433c4f9ecf..d693940e4d 100644 --- a/src/systems/nonlinear/initializesystem.jl +++ b/src/systems/nonlinear/initializesystem.jl @@ -17,6 +17,9 @@ function generate_initializesystem(sys::AbstractSystem; eqs = Equation[x for x in eqs if x isa Equation] end trueobs, eqs = unhack_observed(observed(sys), eqs) + # remove any observed equations that directly or indirectly contain + # delayed unknowns + isempty(trueobs) || filter_delay_equations_variables!(sys, trueobs) vars = unique([unknowns(sys); getfield.(trueobs, :lhs)]) vars_set = Set(vars) # for efficient in-lookup @@ -271,6 +274,52 @@ function get_initial_value_parameter(sym) return toparam(newvar) end +""" + $(TYPEDSIGNATURES) + +Given `sys` and a list of observed equations `trueobs`, remove all the equations that +directly or indirectly contain a delayed unknown of `sys`. +""" +function filter_delay_equations_variables!(sys::AbstractSystem, trueobs::Vector{Equation}) + is_time_dependent(sys) || return trueobs + banned_vars = Set() + idxs_to_remove = Int[] + for (i, eq) in enumerate(trueobs) + _has_delays(sys, eq.rhs, banned_vars) || continue + push!(idxs_to_remove, i) + push!(banned_vars, eq.lhs) + end + return deleteat!(trueobs, idxs_to_remove) +end + +""" + $(TYPEDSIGNATURES) + +Check if the expression `ex` contains a delayed unknown of `sys` or a term in +`banned`. +""" +function _has_delays(sys::AbstractSystem, ex, banned) + ex = unwrap(ex) + ex in banned && return true + if symbolic_type(ex) == NotSymbolic() + if is_array_of_symbolics(ex) + return any(x -> _has_delays(sys, x, banned), ex) + end + return false + end + iscall(ex) || return false + op = operation(ex) + args = arguments(ex) + if iscalledparameter(ex) + return any(x -> _has_delays(sys, x, banned), args) + end + if issym(op) && length(args) == 1 && is_variable(sys, op(get_iv(sys))) && + iscall(args[1]) && get_iv(sys) in vars(args[1]) + return true + end + return any(x -> _has_delays(sys, x, banned), args) +end + struct ReconstructInitializeprob getter::Any setter::Any From c8a282dafbca7ef1260cae7587ffd6551dd16506 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 21 Jan 2025 14:24:36 +0530 Subject: [PATCH 0829/2176] fix: only add dummy parameters for unknowns of time-dependent systems --- src/systems/nonlinear/initializesystem.jl | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/src/systems/nonlinear/initializesystem.jl b/src/systems/nonlinear/initializesystem.jl index d693940e4d..566c94ed68 100644 --- a/src/systems/nonlinear/initializesystem.jl +++ b/src/systems/nonlinear/initializesystem.jl @@ -40,15 +40,17 @@ function generate_initializesystem(sys::AbstractSystem; new_params = Dict() u0map = copy(anydict(u0map)) pmap = copy(anydict(pmap)) - for (k, v) in u0map - k = unwrap(k) - is_variable(sys, k) || continue - (symbolic_type(v) == NotSymbolic() && !is_array_of_symbolics(v)) || continue - newvar = get_initial_value_parameter(k) - new_params[newvar] = k - pmap[newvar] = v - u0map[k] = newvar - defs[newvar] = v + if is_time_dependent(sys) + for (k, v) in u0map + k = unwrap(k) + is_variable(sys, k) || continue + (symbolic_type(v) == NotSymbolic() && !is_array_of_symbolics(v)) || continue + newvar = get_initial_value_parameter(k) + new_params[newvar] = k + pmap[newvar] = v + u0map[k] = newvar + defs[newvar] = v + end end for (k, v) in pmap k = unwrap(k) From b4289e6e7086e8d086432d85090adf8ac82e5a0d Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 21 Jan 2025 14:25:16 +0530 Subject: [PATCH 0830/2176] fix: change differential operator when building initialization for `DiscreteSystem` --- src/systems/nonlinear/initializesystem.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/systems/nonlinear/initializesystem.jl b/src/systems/nonlinear/initializesystem.jl index 566c94ed68..23cc504ca4 100644 --- a/src/systems/nonlinear/initializesystem.jl +++ b/src/systems/nonlinear/initializesystem.jl @@ -70,7 +70,7 @@ function generate_initializesystem(sys::AbstractSystem; append!(eqs_ics, eqs[idxs_alge]) # start equation list with algebraic equations eqs_diff = eqs[idxs_diff] - D = Differential(get_iv(sys)) + D = sys isa DiscreteSystem ? Shift(get_iv(sys), 1) : Differential(get_iv(sys)) diffmap = merge( Dict(eq.lhs => eq.rhs for eq in eqs_diff), Dict(D(eq.lhs) => D(eq.rhs) for eq in trueobs) From 1dbe2956741a18589ecdcb9992997dafe828a7f0 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 21 Jan 2025 14:25:51 +0530 Subject: [PATCH 0831/2176] fix: handle recursive default edge case in initializesystem --- src/systems/nonlinear/initializesystem.jl | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/systems/nonlinear/initializesystem.jl b/src/systems/nonlinear/initializesystem.jl index 23cc504ca4..7116f36e9f 100644 --- a/src/systems/nonlinear/initializesystem.jl +++ b/src/systems/nonlinear/initializesystem.jl @@ -90,6 +90,10 @@ function generate_initializesystem(sys::AbstractSystem; function process_u0map_with_dummysubs(y, x) y = get(schedule.dummy_sub, y, y) y = fixpoint_sub(y, diffmap) + # If we have `D(x) ~ x` and provide [D(x) => x, x => 1.0] to `u0map`, then + # without this condition `defs` would get `x => x` instead of retaining + # `x => 1.0`. + isequal(y, x) && return if y ∈ vars_set # variables specified in u0 overrides defaults push!(defs, y => x) From 3b1dbc1fae8ef3d5d42eeac0df9bf715d7456649 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 21 Jan 2025 14:26:23 +0530 Subject: [PATCH 0832/2176] fix: remove occurrences of dummy parameters when remaking initializeprob --- src/systems/nonlinear/initializesystem.jl | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/systems/nonlinear/initializesystem.jl b/src/systems/nonlinear/initializesystem.jl index 7116f36e9f..9736581135 100644 --- a/src/systems/nonlinear/initializesystem.jl +++ b/src/systems/nonlinear/initializesystem.jl @@ -460,6 +460,16 @@ function SciMLBase.remake_initialization_data( merge!(guesses, meta.additional_guesses) use_scc = get(meta.extra_metadata, :use_scc, true) initialization_eqs = meta.additional_initialization_eqs + + # remove occurrences of `keys(new_params)` from `u0map` and `pmap` + for (newvar, oldvar) in meta.new_params + if isequal(get(u0map, oldvar, nothing), newvar) + u0map[oldvar] = pmap[newvar] + elseif isequal(get(pmap, oldvar, nothing), newvar) + pmap[oldvar] = pmap[newvar] + end + delete!(pmap, newvar) + end end else # there is no initializeprob, so the original problem construction From 46f2b821c75afe19afeec5f24cb4b9cc26648cdb Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 21 Jan 2025 14:26:45 +0530 Subject: [PATCH 0833/2176] fix: only set solvable parameter values to zero in limited cases --- src/systems/problem_utils.jl | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/systems/problem_utils.jl b/src/systems/problem_utils.jl index add3fd4405..ed887e9386 100644 --- a/src/systems/problem_utils.jl +++ b/src/systems/problem_utils.jl @@ -596,6 +596,8 @@ function maybe_build_initialization_problem( end for p in punknowns + is_parameter_solvable(p, pmap, defs, guesses) || continue + get(op, p, missing) === missing || continue p = unwrap(p) stype = symtype(p) op[p] = get_temporary_value(p) From aa3e6db05f9196deb35b93753625b0862dad677b Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 21 Jan 2025 14:29:38 +0530 Subject: [PATCH 0834/2176] fix: ensure initialization systems for `SteadyStateProblem` do not involve independent variable --- src/systems/diffeqs/abstractodesystem.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/systems/diffeqs/abstractodesystem.jl b/src/systems/diffeqs/abstractodesystem.jl index 2c07974252..95a75dddad 100644 --- a/src/systems/diffeqs/abstractodesystem.jl +++ b/src/systems/diffeqs/abstractodesystem.jl @@ -1045,7 +1045,7 @@ function DiffEqBase.SteadyStateProblem{iip}(sys::AbstractODESystem, u0map, end f, u0, p = process_SciMLProblem(ODEFunction{iip}, sys, u0map, parammap; steady_state = true, - check_length, kwargs...) + check_length, force_initialization_time_independent = true, kwargs...) kwargs = filter_kwargs(kwargs) SteadyStateProblem{iip}(f, u0, p; kwargs...) end From 7e647aee73e7bd6adfa475bddd6b65114463e9ae Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 21 Jan 2025 14:30:16 +0530 Subject: [PATCH 0835/2176] test: disambiguate `observed` in initializesystem tests --- test/initializationsystem.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/initializationsystem.jl b/test/initializationsystem.jl index 026fd2c43d..2ed9c99aaf 100644 --- a/test/initializationsystem.jl +++ b/test/initializationsystem.jl @@ -3,7 +3,7 @@ using StochasticDiffEq, DelayDiffEq, StochasticDelayDiffEq, JumpProcesses using ForwardDiff using SymbolicIndexingInterface, SciMLStructures using SciMLStructures: Tunable -using ModelingToolkit: t_nounits as t, D_nounits as D +using ModelingToolkit: t_nounits as t, D_nounits as D, observed using DynamicQuantities @parameters g From d4192475e54cf9714271775008636e46171557d3 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 21 Jan 2025 14:30:26 +0530 Subject: [PATCH 0836/2176] test: remove unnecessary test --- test/initializationsystem.jl | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/test/initializationsystem.jl b/test/initializationsystem.jl index 2ed9c99aaf..175eda6465 100644 --- a/test/initializationsystem.jl +++ b/test/initializationsystem.jl @@ -600,8 +600,7 @@ end (ModelingToolkit.System, DDEProblem, MethodOfSteps(Tsit5()), [_x(t - 0.1), 0.0]), (ModelingToolkit.System, SDDEProblem, ImplicitEM(), [_x(t - 0.1) + a, b]) ] - function test_parameter(prob, sym, val, initialval = zero(val)) - @test prob.ps[sym] ≈ initialval + function test_parameter(prob, sym, val) if prob.u0 !== nothing @test init(prob, alg).ps[sym] ≈ val end From 44c05cd595314f463c34f119cf59a60c3c3e1c67 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 21 Jan 2025 14:31:13 +0530 Subject: [PATCH 0837/2176] test: refactor `initialization_data === nothing` tests --- test/initializationsystem.jl | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/test/initializationsystem.jl b/test/initializationsystem.jl index 175eda6465..8c99f730e9 100644 --- a/test/initializationsystem.jl +++ b/test/initializationsystem.jl @@ -687,7 +687,8 @@ end _pmap = merge(pmap, Dict(p => 1.0)) prob = Problem(sys, u0map, (0.0, 1.0), _pmap) @test prob.ps[p] ≈ 1.0 - @test prob.f.initialization_data === nothing + initsys = prob.f.initialization_data.initializeprob.f.sys + @test is_parameter(initsys, p) # Non-floating point @parameters r::Int s::Int @@ -696,7 +697,9 @@ end prob = Problem(sys, u0map, (0.0, 1.0), [r => 1]) @test prob.ps[r] == 1 @test prob.ps[s] == 2 - @test prob.f.initialization_data === nothing + initsys = prob.f.initialization_data.initializeprob.f.sys + @test is_parameter(initsys, r) + @test is_parameter(initsys, s) @mtkbuild sys = System( [D(x) ~ x + rhss[1], p ~ x + y + rhss[2]], t; guesses = [p => 0.0]) @@ -1180,21 +1183,28 @@ end @test integ2.ps[q] ≈ 2cbrt(3 / 28) end +function test_dummy_initialization_equation(prob, var) + initsys = prob.f.initialization_data.initializeprob.f.sys + @test isempty(equations(initsys)) + idx = findfirst(eq -> isequal(var, eq.lhs), observed(initsys)) + @test idx !== nothing && is_parameter(initsys, observed(initsys)[idx].rhs) +end + @testset "Remake problem with no initializeprob" begin @variables x(t) [guess = 1.0] y(t) [guess = 1.0] @parameters p [guess = 1.0] q [guess = 1.0] @mtkbuild sys = ODESystem( [D(x) ~ p * x + q * y, y ~ 2x], t; parameter_dependencies = [q ~ 2p]) prob = ODEProblem(sys, [x => 1.0], (0.0, 1.0), [p => 1.0]) - @test prob.f.initialization_data === nothing + test_dummy_initialization_equation(prob, x) prob2 = remake(prob; u0 = [x => 2.0]) @test prob2[x] == 2.0 - @test prob2.f.initialization_data === nothing + test_dummy_initialization_equation(prob2, x) prob3 = remake(prob; u0 = [y => 2.0]) @test prob3.f.initialization_data !== nothing @test init(prob3)[x] ≈ 1.0 prob4 = remake(prob; p = [p => 1.0]) - @test prob4.f.initialization_data === nothing + test_dummy_initialization_equation(prob4, x) prob5 = remake(prob; p = [p => missing, q => 2.0]) @test prob5.f.initialization_data !== nothing @test init(prob5).ps[p] ≈ 1.0 @@ -1206,12 +1216,12 @@ end @mtkbuild sys = ODESystem( [D(x) ~ p * x + q * y, y ~ 2x], t; parameter_dependencies = [q ~ 2p]) prob = ODEProblem(sys, [:x => 1.0], (0.0, 1.0), [p => 1.0]) - @test prob.f.initialization_data === nothing + test_dummy_initialization_equation(prob, x) prob2 = remake(prob; u0 = [:x => 2.0]) - @test prob2.f.initialization_data === nothing - prob3 = remake(prob; u0 = [:y => 1.0]) - @test prob3.f.initialization_data !== nothing + test_dummy_initialization_equation(prob2, x) + prob3 = remake(prob; u0 = [:y => 1.0, :x => nothing]) @test init(prob3)[x] ≈ 0.5 + @test SciMLBase.successful_retcode(solve(prob3)) end @testset "Issue#3246: type promotion with parameter dependent initialization_eqs" begin From fbcacfb90e6471b10ef3380a32e30de732c5aba7 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 21 Jan 2025 14:31:27 +0530 Subject: [PATCH 0838/2176] test: update initializeprob type promotion test --- test/initializationsystem.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/initializationsystem.jl b/test/initializationsystem.jl index 8c99f730e9..971a7aa9ca 100644 --- a/test/initializationsystem.jl +++ b/test/initializationsystem.jl @@ -950,7 +950,7 @@ end prob2 = remake(prob; u0 = ForwardDiff.Dual.(prob.u0)) @test eltype(state_values(prob2.f.initialization_data.initializeprob)) <: ForwardDiff.Dual - @test eltype(prob2.f.initialization_data.initializeprob.p.tunable) <: Float64 + @test eltype(prob2.f.initialization_data.initializeprob.p.tunable) <: ForwardDiff.Dual @test state_values(prob2.f.initialization_data.initializeprob) ≈ state_values(prob.f.initialization_data.initializeprob) From 0d5adc5ce54c1d0c477573cfa044f0019455d805 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 23 Jan 2025 12:08:05 +0530 Subject: [PATCH 0839/2176] fix: don't build initialization for `JumpProblem` --- src/systems/jumps/jumpsystem.jl | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/systems/jumps/jumpsystem.jl b/src/systems/jumps/jumpsystem.jl index beaa78e1e3..4e72d0853a 100644 --- a/src/systems/jumps/jumpsystem.jl +++ b/src/systems/jumps/jumpsystem.jl @@ -416,7 +416,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) + t = tspan === nothing ? nothing : tspan[1], use_union, tofloat = false, check_length = false, build_initializeprob = false) f = DiffEqBase.DISCRETE_INPLACE_DEFAULT observedfun = ObservedFunctionCache( @@ -513,7 +513,8 @@ function DiffEqBase.ODEProblem(sys::JumpSystem, u0map, tspan::Union{Tuple, Nothi parameter_dependencies = parameter_dependencies(sys), metadata = get_metadata(sys), gui_metadata = get_gui_metadata(sys)) osys = complete(osys) - return ODEProblem(osys, u0map, tspan, parammap; check_length = false, kwargs...) + return ODEProblem(osys, u0map, tspan, parammap; check_length = false, + build_initializeprob = false, kwargs...) else _, u0, p = process_SciMLProblem(EmptySciMLFunction, sys, u0map, parammap; t = tspan === nothing ? nothing : tspan[1], use_union, tofloat = false, From 2618c64df77a333260c70d0e556ad02a27943032 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 23 Jan 2025 12:08:26 +0530 Subject: [PATCH 0840/2176] fix: don't use observed equations for initialization of `DiscreteSystem` --- src/systems/nonlinear/initializesystem.jl | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/systems/nonlinear/initializesystem.jl b/src/systems/nonlinear/initializesystem.jl index 9736581135..ba8d8c5352 100644 --- a/src/systems/nonlinear/initializesystem.jl +++ b/src/systems/nonlinear/initializesystem.jl @@ -220,7 +220,10 @@ function generate_initializesystem(sys::AbstractSystem; pars = [pars; map(unwrap, collect(keys(new_params)))] is_time_dependent(sys) && push!(pars, get_iv(sys)) - if is_time_dependent(sys) + # FIXME: observed equations for discrete systems are broken. They don't express + # relations at the current time and instead express them in terms of past values. + # This precludes them from being useful in initialization. + if is_time_dependent(sys) && !(sys isa DiscreteSystem) # 8) use observed equations for guesses of observed variables if not provided for eq in trueobs haskey(defs, eq.lhs) && continue From ea8b8d618eae499c4801c6d9fea5b726fd1ec457 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 23 Jan 2025 15:18:44 +0530 Subject: [PATCH 0841/2176] fix: populate observed into `u0map` for time-independent systems --- src/systems/problem_utils.jl | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/systems/problem_utils.jl b/src/systems/problem_utils.jl index ed887e9386..21e0cec890 100644 --- a/src/systems/problem_utils.jl +++ b/src/systems/problem_utils.jl @@ -742,6 +742,8 @@ function process_SciMLProblem( cmap, cs = get_cmap(sys) kwargs = NamedTuple(kwargs) + is_time_dependent(sys) || add_observed!(sys, u0map) + op, missing_unknowns, missing_pars = build_operating_point( u0map, pmap, defs, cmap, dvs, ps) substitute_extra_variables!(sys, u0map) @@ -763,7 +765,7 @@ function process_SciMLProblem( op[iv] = t end - add_observed!(sys, op) + is_time_dependent(sys) && add_observed!(sys, op) add_parameter_dependencies!(sys, op) if warn_cyclic_dependency From c4c9ba2f77d08a11f61ad60c45f1251eccaccf70 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 24 Jan 2025 12:52:58 +0530 Subject: [PATCH 0842/2176] fix: remove incorrect fix to dummy derivatives in `u0map` added in #3337 --- src/systems/diffeqs/abstractodesystem.jl | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/src/systems/diffeqs/abstractodesystem.jl b/src/systems/diffeqs/abstractodesystem.jl index 95a75dddad..1d641cd3cb 100644 --- a/src/systems/diffeqs/abstractodesystem.jl +++ b/src/systems/diffeqs/abstractodesystem.jl @@ -1313,20 +1313,6 @@ function InitializationProblem{iip, specialize}(sys::AbstractSystem, u0map = merge(ModelingToolkit.guesses(sys), todict(guesses), todict(u0map)) - # Replace dummy derivatives in u0map: D(x) -> x_t etc. - if has_schedule(sys) - schedule = get_schedule(sys) - if !isnothing(schedule) - for (var, val) in u0map - dvar = get(schedule.dummy_sub, var, var) # with dummy derivatives - if dvar !== var # then replace it - delete!(u0map, var) - push!(u0map, dvar => val) - end - end - end - end - fullmap = merge(u0map, parammap) u0T = Union{} for sym in unknowns(isys) From 3d60e045e4a2d73dec88f2514f15ccd36e17e5e9 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 24 Jan 2025 12:53:51 +0530 Subject: [PATCH 0843/2176] fix: do not build initialization for `DiscreteProblem` --- src/systems/discrete_system/discrete_system.jl | 2 +- src/systems/nonlinear/initializesystem.jl | 7 ++----- 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/src/systems/discrete_system/discrete_system.jl b/src/systems/discrete_system/discrete_system.jl index cd6ff45b6c..cb33f5d339 100644 --- a/src/systems/discrete_system/discrete_system.jl +++ b/src/systems/discrete_system/discrete_system.jl @@ -307,7 +307,7 @@ function SciMLBase.DiscreteProblem( u0map = to_varmap(u0map, dvs) u0map = shift_u0map_forward(sys, u0map, defaults(sys)) f, u0, p = process_SciMLProblem( - DiscreteFunction, sys, u0map, parammap; eval_expression, eval_module) + DiscreteFunction, sys, u0map, parammap; eval_expression, eval_module, build_initializeprob = false) u0 = f(u0, p, tspan[1]) DiscreteProblem(f, u0, tspan, p; kwargs...) end diff --git a/src/systems/nonlinear/initializesystem.jl b/src/systems/nonlinear/initializesystem.jl index ba8d8c5352..a4edf23f6f 100644 --- a/src/systems/nonlinear/initializesystem.jl +++ b/src/systems/nonlinear/initializesystem.jl @@ -70,7 +70,7 @@ function generate_initializesystem(sys::AbstractSystem; append!(eqs_ics, eqs[idxs_alge]) # start equation list with algebraic equations eqs_diff = eqs[idxs_diff] - D = sys isa DiscreteSystem ? Shift(get_iv(sys), 1) : Differential(get_iv(sys)) + D = Differential(get_iv(sys)) diffmap = merge( Dict(eq.lhs => eq.rhs for eq in eqs_diff), Dict(D(eq.lhs) => D(eq.rhs) for eq in trueobs) @@ -220,10 +220,7 @@ function generate_initializesystem(sys::AbstractSystem; pars = [pars; map(unwrap, collect(keys(new_params)))] is_time_dependent(sys) && push!(pars, get_iv(sys)) - # FIXME: observed equations for discrete systems are broken. They don't express - # relations at the current time and instead express them in terms of past values. - # This precludes them from being useful in initialization. - if is_time_dependent(sys) && !(sys isa DiscreteSystem) + if is_time_dependent(sys) # 8) use observed equations for guesses of observed variables if not provided for eq in trueobs haskey(defs, eq.lhs) && continue From 2e6d172a5be5a2a3a77f3dac82f784b6138822db Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 24 Jan 2025 12:54:49 +0530 Subject: [PATCH 0844/2176] test: make `NonlinearSystem` initialization tests less temperamental --- test/initializationsystem.jl | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/test/initializationsystem.jl b/test/initializationsystem.jl index 971a7aa9ca..fb4f4199fe 100644 --- a/test/initializationsystem.jl +++ b/test/initializationsystem.jl @@ -786,20 +786,26 @@ end @testset "Parameter initialization" begin function test_parameter(prob, alg, param, val) integ = init(prob, alg) - @test integ.ps[param]≈val rtol=1e-6 + @test integ.ps[param]≈val rtol=1e-5 + # some algorithms are a little temperamental sol = solve(prob, alg) - @test sol.ps[param]≈val rtol=1e-6 + @test sol.ps[param]≈val rtol=1e-5 @test SciMLBase.successful_retcode(sol) end - @variables x=1.0 y=3.0 - @parameters p q - @named sys = NonlinearSystem( - [x + y - p ~ 0, x - q ~ 0]; defaults = [q => missing], - guesses = [q => 1.0], initialization_eqs = [p^2 + q^2 + 2p * q ~ 0]) - sys = complete(sys) + @parameters p=2.0 q=missing [guess = 1.0] c=1.0 + @variables x=1.0 y=2.0 z=3.0 + + eqs = [0 ~ p * (y - x), + 0 ~ x * (q - z) - y, + 0 ~ x * y - c * z] + @mtkbuild sys = NonlinearSystem(eqs; initialization_eqs = [p^2 + q^2 + 2p * q ~ 0]) + # @mtkbuild sys = NonlinearSystem( + # [p * x^2 + q * y^3 ~ 0, x - q ~ 0]; defaults = [q => missing], + # guesses = [q => 1.0], initialization_eqs = [p^2 + q^2 + 2p * q ~ 0]) + for (probT, algs) in prob_alg_combinations - prob = probT(sys, [], [p => 2.0]) + prob = probT(sys, []) @test prob.f.initialization_data !== nothing @test prob.f.initialization_data.initializeprobmap === nothing for alg in algs @@ -950,7 +956,8 @@ end prob2 = remake(prob; u0 = ForwardDiff.Dual.(prob.u0)) @test eltype(state_values(prob2.f.initialization_data.initializeprob)) <: ForwardDiff.Dual - @test eltype(prob2.f.initialization_data.initializeprob.p.tunable) <: ForwardDiff.Dual + @test eltype(prob2.f.initialization_data.initializeprob.p.tunable) <: + ForwardDiff.Dual @test state_values(prob2.f.initialization_data.initializeprob) ≈ state_values(prob.f.initialization_data.initializeprob) From 44a28afc694c009ad4377e11389428637f303225 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 24 Jan 2025 12:55:00 +0530 Subject: [PATCH 0845/2176] test: mark initialization of `JumpSystem` as broken --- test/initializationsystem.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/initializationsystem.jl b/test/initializationsystem.jl index fb4f4199fe..fe5f03b9c7 100644 --- a/test/initializationsystem.jl +++ b/test/initializationsystem.jl @@ -1287,7 +1287,7 @@ end u0s = [I => 1, R => 0] ps = [S0 => 999, β => 0.01, γ => 0.001] dprob = DiscreteProblem(js, u0s, (0.0, 10.0), ps) - @test dprob.f.initialization_data !== nothing + @test_broken dprob.f.initialization_data !== nothing sol = solve(dprob, FunctionMap()) @test sol[S, 1] ≈ 999 @test SciMLBase.successful_retcode(sol) From cab4506c9d5da1a297915081a92e99b11af2cce7 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 24 Jan 2025 12:55:17 +0530 Subject: [PATCH 0846/2176] fix: fix `ReconstructInitializeprob` overriding guesses --- src/systems/nonlinear/initializesystem.jl | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/systems/nonlinear/initializesystem.jl b/src/systems/nonlinear/initializesystem.jl index a4edf23f6f..d323e29f1c 100644 --- a/src/systems/nonlinear/initializesystem.jl +++ b/src/systems/nonlinear/initializesystem.jl @@ -333,10 +333,9 @@ end function ReconstructInitializeprob( srcsys::AbstractSystem, dstsys::AbstractSystem; remap = Dict()) - syms = [unknowns(dstsys); - reduce(vcat, reorder_parameters(dstsys, parameters(dstsys)); init = [])] + syms = reduce(vcat, reorder_parameters(dstsys, parameters(dstsys)); init = []) getter = getu(srcsys, map(x -> get(remap, x, x), syms)) - setter = setsym_oop(dstsys, syms) + setter = setp_oop(dstsys, syms) return ReconstructInitializeprob(getter, setter) end From 8d11252db0d1192c37e4095159cc40b037f41bd5 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 20 Jan 2025 17:34:23 +0530 Subject: [PATCH 0847/2176] feat: propagate `algebraic_only` kwarg in `InitializationProblem` --- src/systems/diffeqs/abstractodesystem.jl | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/systems/diffeqs/abstractodesystem.jl b/src/systems/diffeqs/abstractodesystem.jl index 1d641cd3cb..b4c51b52e9 100644 --- a/src/systems/diffeqs/abstractodesystem.jl +++ b/src/systems/diffeqs/abstractodesystem.jl @@ -1221,6 +1221,7 @@ function InitializationProblem{iip, specialize}(sys::AbstractSystem, use_scc = true, allow_incomplete = false, force_time_independent = false, + algebraic_only = false, kwargs...) where {iip, specialize} if !iscomplete(sys) error("A completed system is required. Call `complete` or `structural_simplify` on the system before creating an `ODEProblem`") @@ -1231,12 +1232,12 @@ function InitializationProblem{iip, specialize}(sys::AbstractSystem, elseif isempty(u0map) && get_initializesystem(sys) === nothing isys = generate_initializesystem( sys; initialization_eqs, check_units, pmap = parammap, - guesses, extra_metadata = (; use_scc)) + guesses, extra_metadata = (; use_scc), algebraic_only) simplify_system = true else isys = generate_initializesystem( sys; u0map, initialization_eqs, check_units, - pmap = parammap, guesses, extra_metadata = (; use_scc)) + pmap = parammap, guesses, extra_metadata = (; use_scc), algebraic_only) simplify_system = true end From 33fe6cd5f9575e29cc576861ae53745fc3eb3864 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 20 Jan 2025 17:34:54 +0530 Subject: [PATCH 0848/2176] fix: filter `nothing` values in `op`/`u0map`/`pmap` in `process_SciMLProblem` --- src/systems/problem_utils.jl | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/systems/problem_utils.jl b/src/systems/problem_utils.jl index 21e0cec890..a0c001f7ab 100644 --- a/src/systems/problem_utils.jl +++ b/src/systems/problem_utils.jl @@ -746,6 +746,9 @@ function process_SciMLProblem( op, missing_unknowns, missing_pars = build_operating_point( u0map, pmap, defs, cmap, dvs, ps) + filter_missing_values!(op) + filter_missing_values!(u0map) + filter_missing_values!(pmap) substitute_extra_variables!(sys, u0map) substitute_extra_variables!(sys, pmap) From 3e37d24628cf9b4481df2c5049691b7a4a1bd896 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 20 Jan 2025 17:35:23 +0530 Subject: [PATCH 0849/2176] feat: propagate `algebraic_only`, `allow_incomplete` kwargs to initialization in `process_SciMLProblem` --- src/systems/problem_utils.jl | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/systems/problem_utils.jl b/src/systems/problem_utils.jl index a0c001f7ab..9160366195 100644 --- a/src/systems/problem_utils.jl +++ b/src/systems/problem_utils.jl @@ -708,6 +708,8 @@ Keyword arguments: determined. - `force_initialization_time_independent`: Whether to force the initialization to not use the independent variable of `sys`. +- `algebraic_only`: Whether to build the initialization problem using only algebraic equations. +- `allow_incomplete`: Whether to allow incomplete initialization problems. All other keyword arguments are passed as-is to `constructor`. """ @@ -722,7 +724,8 @@ function process_SciMLProblem( circular_dependency_max_cycle_length = length(all_symbols(sys)), circular_dependency_max_cycles = 10, substitution_limit = 100, use_scc = true, - force_initialization_time_independent = false, kwargs...) + force_initialization_time_independent = false, algebraic_only = false, + allow_incomplete = false, kwargs...) dvs = unknowns(sys) ps = parameters(sys) iv = has_iv(sys) ? get_iv(sys) : nothing @@ -759,7 +762,7 @@ function process_SciMLProblem( eval_expression, eval_module, fully_determined, warn_cyclic_dependency, check_units = check_initialization_units, circular_dependency_max_cycle_length, circular_dependency_max_cycles, use_scc, - force_time_independent = force_initialization_time_independent) + force_time_independent = force_initialization_time_independent, algebraic_only, allow_incomplete) kwargs = merge(kwargs, kws) end From bca43ba370786387205d84b45d365b088dd8ed52 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 20 Jan 2025 17:37:20 +0530 Subject: [PATCH 0850/2176] fix: use `SciMLBase.get_initial_values` in `linearization_function` --- src/ModelingToolkit.jl | 2 +- src/systems/abstractsystem.jl | 148 +++++++++++++++------------------- 2 files changed, 68 insertions(+), 82 deletions(-) diff --git a/src/ModelingToolkit.jl b/src/ModelingToolkit.jl index 09b59c4ed6..303c88da73 100644 --- a/src/ModelingToolkit.jl +++ b/src/ModelingToolkit.jl @@ -45,7 +45,7 @@ using Compat using AbstractTrees using DiffEqBase, SciMLBase, ForwardDiff using SciMLBase: StandardODEProblem, StandardNonlinearProblem, handle_varmap, TimeDomain, - PeriodicClock, Clock, SolverStepClock, Continuous + PeriodicClock, Clock, SolverStepClock, Continuous, OverrideInit, NoInit using Distributed import JuliaFormatter using MLStyle diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index 8213b8f241..1fab568cb4 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -2394,6 +2394,9 @@ See also [`linearize`](@ref) which provides a higher-level interface. function linearization_function(sys::AbstractSystem, inputs, outputs; simplify = false, initialize = true, + initializealg = nothing, + initialization_abstol = 1e-5, + initialization_reltol = 1e-3, op = Dict(), p = DiffEqBase.NullParameters(), zero_dummy_der = false, @@ -2420,88 +2423,33 @@ function linearization_function(sys::AbstractSystem, inputs, op = merge(defs, op) end sys = ssys - u0map = Dict(k => v for (k, v) in op if is_variable(ssys, k)) - initsys = structural_simplify( - generate_initializesystem( - sys, u0map = u0map, guesses = guesses(sys), algebraic_only = true), - fully_determined = false) - - # HACK: some unknowns may not be involved in any initialization equations, and are - # thus removed from the system during `structural_simplify`. - # This causes `getu(initsys, unknowns(sys))` to fail, so we add them back as parameters - # for now. - missing_unknowns = setdiff(unknowns(sys), all_symbols(initsys)) - if !isempty(missing_unknowns) - if warn_initialize_determined - @warn "Initialization system is underdetermined. No equations for $(missing_unknowns). Initialization will default to using least squares. To suppress this warning pass warn_initialize_determined = false." - end - new_parameters = [parameters(initsys); missing_unknowns] - @set! initsys.ps = new_parameters - initsys = complete(initsys) - end - - if p isa SciMLBase.NullParameters - p = Dict() - else - p = todict(p) - end - x0 = merge(defaults_and_guesses(sys), op) - if has_index_cache(sys) && get_index_cache(sys) !== nothing - sys_ps = MTKParameters(sys, p, x0) - else - sys_ps = varmap_to_vars(p, parameters(sys); defaults = x0) - end - p[get_iv(sys)] = NaN - if has_index_cache(initsys) && get_index_cache(initsys) !== nothing - oldps = MTKParameters(initsys, p, merge(guesses(sys), defaults(sys), op)) - initsys_ps = parameters(initsys) - p_getter = build_explicit_observed_function( - sys, initsys_ps; eval_expression, eval_module) - - u_getter = isempty(unknowns(initsys)) ? (_...) -> nothing : - build_explicit_observed_function( - sys, unknowns(initsys); eval_expression, eval_module) - get_initprob_u_p = let p_getter = p_getter, - p_setter! = setp(initsys, initsys_ps), - u_getter = u_getter - - function (u, p, t) - p_setter!(oldps, p_getter(u, p, t)) - newu = u_getter(u, p, t) - return newu, oldps - end - end - else - get_initprob_u_p = let p_getter = getu(sys, parameters(initsys)), - u_getter = build_explicit_observed_function( - sys, unknowns(initsys); eval_expression, eval_module) - - function (u, p, t) - state = ProblemState(; u, p, t) - return u_getter( - state_values(state), parameter_values(state), current_time(state)), - p_getter(state) - end - end + + if initializealg === nothing + initializealg = initialize ? OverrideInit() : NoInit() end - initfn = NonlinearFunction(initsys; eval_expression, eval_module) - initprobmap = build_explicit_observed_function( - initsys, unknowns(sys); eval_expression, eval_module) + + fun, u0, p = process_SciMLProblem( + ODEFunction{true, SciMLBase.FullSpecialize}, sys, merge( + defaults_and_guesses(sys), op), p; + t = 0.0, build_initializeprob = initializealg isa OverrideInit, + allow_incomplete = true, algebraic_only = true) + prob = ODEProblem(fun, u0, (nothing, nothing), p) + ps = parameters(sys) h = build_explicit_observed_function(sys, outputs; eval_expression, eval_module) lin_fun = let diff_idxs = diff_idxs, alge_idxs = alge_idxs, input_idxs = input_idxs, sts = unknowns(sys), - get_initprob_u_p = get_initprob_u_p, - fun = ODEFunction{true, SciMLBase.FullSpecialize}( - sys, unknowns(sys), ps; eval_expression, eval_module), - initfn = initfn, - initprobmap = initprobmap, + fun = fun, + prob = prob, + sys_ps = p, h = h, + integ_cache = (similar(u0)), chunk = ForwardDiff.Chunk(input_idxs), - sys_ps = sys_ps, - initialize = initialize, + initializealg = initializealg, + initialization_abstol = initialization_abstol, + initialization_reltol = initialization_reltol, initialization_solver_alg = initialization_solver_alg, sys = sys @@ -2521,14 +2469,14 @@ function linearization_function(sys::AbstractSystem, inputs, if u !== nothing # Handle systems without unknowns length(sts) == length(u) || error("Number of unknown variables ($(length(sts))) does not match the number of input unknowns ($(length(u)))") - if initialize && !isempty(alge_idxs) # This is expensive and can be omitted if the user knows that the system is already initialized - residual = fun(u, p, t) - if norm(residual[alge_idxs]) > √(eps(eltype(residual))) - initu0, initp = get_initprob_u_p(u, p, t) - initprob = NonlinearLeastSquaresProblem(initfn, initu0, initp) - nlsol = solve(initprob, initialization_solver_alg) - u = initprobmap(state_values(nlsol), parameter_values(nlsol)) - end + + integ = MockIntegrator{true}(u, p, t, integ_cache) + u, p, success = SciMLBase.get_initial_values( + prob, integ, fun, initializealg, Val(true); + abstol = initialization_abstol, reltol = initialization_reltol, + nlsolve_alg = initialization_solver_alg) + if !success + error("Initialization algorithm $(initializealg) failed with `u = $u` and `p = $p`.") end uf = SciMLBase.UJacobianWrapper(fun, t, p) fg_xz = ForwardDiff.jacobian(uf, u) @@ -2563,6 +2511,44 @@ function linearization_function(sys::AbstractSystem, inputs, return lin_fun, sys end +""" + $(TYPEDEF) + +Mock `DEIntegrator` to allow using `CheckInit` without having to create a new integrator +(and consequently depend on `OrdinaryDiffEq`). + +# Fields + +$(TYPEDFIELDS) +""" +struct MockIntegrator{iip, U, P, T, C} <: SciMLBase.DEIntegrator{Nothing, iip, U, T} + """ + The state vector. + """ + u::U + """ + The parameter object. + """ + p::P + """ + The current time. + """ + t::T + """ + The integrator cache. + """ + cache::C +end + +function MockIntegrator{iip}(u::U, p::P, t::T, cache::C) where {iip, U, P, T, C} + return MockIntegrator{iip, U, P, T, C}(u, p, t, cache) +end + +SymbolicIndexingInterface.state_values(integ::MockIntegrator) = integ.u +SymbolicIndexingInterface.parameter_values(integ::MockIntegrator) = integ.p +SymbolicIndexingInterface.current_time(integ::MockIntegrator) = integ.t +SciMLBase.get_tmp_cache(integ::MockIntegrator) = integ.cache + """ (; A, B, C, D), simplified_sys = linearize_symbolic(sys::AbstractSystem, inputs, outputs; simplify = false, allow_input_derivatives = false, kwargs...) From f524be8f738c66049f301e572773ed21f89f42a8 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 20 Jan 2025 17:45:32 +0530 Subject: [PATCH 0851/2176] fix: do not forward keywords to `linearize` in `AnalysisPoint` linearization functions --- src/systems/analysis_points.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/systems/analysis_points.jl b/src/systems/analysis_points.jl index 9ce5fe2bc1..82b8d9507f 100644 --- a/src/systems/analysis_points.jl +++ b/src/systems/analysis_points.jl @@ -863,7 +863,7 @@ for f in [:get_sensitivity, :get_comp_sensitivity, :get_looptransfer] sys, ap, args...; loop_openings = [], system_modifier = identity, kwargs...) lin_fun, ssys = $(utility_fun)( sys, ap, args...; loop_openings, system_modifier, kwargs...) - ModelingToolkit.linearize(ssys, lin_fun; kwargs...), ssys + ModelingToolkit.linearize(ssys, lin_fun), ssys end end From 77939a3f31e4b37ac8dbdbc50c08c2d2062f0246 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 4 Feb 2025 11:55:38 +0530 Subject: [PATCH 0852/2176] fix: handle `=> nothing` entries in `u0map` in `generate_initializesystem` --- src/systems/nonlinear/initializesystem.jl | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/systems/nonlinear/initializesystem.jl b/src/systems/nonlinear/initializesystem.jl index d323e29f1c..f7fe7ea077 100644 --- a/src/systems/nonlinear/initializesystem.jl +++ b/src/systems/nonlinear/initializesystem.jl @@ -31,6 +31,13 @@ function generate_initializesystem(sys::AbstractSystem; idxs_diff = isdiffeq.(eqs) # PREPROCESSING + # If `=> nothing` in `u0map`, remove the key from `defs` + u0map = copy(anydict(u0map)) + for (k, v) in u0map + v === nothing || continue + delete!(defs, k) + end + filter_missing_values!(u0map) # for initial conditions of the form `var => constant`, we instead turn them into # `var ~ var0` where `var0` is a new parameter, and make `update_initializeprob!` # update `initializeprob.ps[var0] = prob[var]`. @@ -38,7 +45,6 @@ function generate_initializesystem(sys::AbstractSystem; # map parameters in `initprob` which need to be updated in `update_initializeprob!` # to the corresponding expressions that determine their values new_params = Dict() - u0map = copy(anydict(u0map)) pmap = copy(anydict(pmap)) if is_time_dependent(sys) for (k, v) in u0map From 7bdc1f60fd3533c67776705b54790ed21562def9 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 4 Feb 2025 11:57:07 +0530 Subject: [PATCH 0853/2176] fix: output variable in `PerturbOutput` should not have a default --- src/systems/analysis_points.jl | 1 - 1 file changed, 1 deletion(-) diff --git a/src/systems/analysis_points.jl b/src/systems/analysis_points.jl index 82b8d9507f..022a0909ed 100644 --- a/src/systems/analysis_points.jl +++ b/src/systems/analysis_points.jl @@ -591,7 +591,6 @@ function apply_transformation(tf::PerturbOutput, sys::AbstractSystem) # add output variable, equation, default out_var, out_def = get_analysis_variable( ap_ivar, nameof(ap), get_iv(sys); perturb = false) - defs[out_var] = out_def push!(ap_sys_eqs, out_var ~ ap_ivar + wrap(new_var)) push!(unks, out_var) From a3351ff28020e7d6d15192d41662b7075536a565 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 4 Feb 2025 11:58:12 +0530 Subject: [PATCH 0854/2176] fix: better handle `=> nothing` entries in `u0map` --- src/systems/problem_utils.jl | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/src/systems/problem_utils.jl b/src/systems/problem_utils.jl index 9160366195..99326aa285 100644 --- a/src/systems/problem_utils.jl +++ b/src/systems/problem_utils.jl @@ -442,9 +442,20 @@ end $(TYPEDSIGNATURES) Remove keys in `varmap` whose values are `nothing`. + +If `missing_values` is not `nothing`, it is assumed to be a collection and all removed +keys will be added to it. """ -function filter_missing_values!(varmap::AbstractDict) - filter!(kvp -> kvp[2] !== nothing, varmap) +function filter_missing_values!(varmap::AbstractDict; missing_values = nothing) + filter!(varmap) do kvp + if kvp[2] !== nothing + return true + end + if missing_values !== nothing + push!(missing_values, kvp[1]) + end + return false + end end struct GetUpdatedMTKParameters{G, S} @@ -523,14 +534,17 @@ function build_operating_point( haskey(op, k) && continue op[k] = v end + filter_missing_values!(op; missing_values = missing_unknowns) + merge!(op, pmap) missing_pars = add_fallbacks!(op, ps, defs) + filter_missing_values!(op; missing_values = missing_pars) for eq in cmap op[eq.lhs] = eq.rhs end - empty!(u0map) - empty!(pmap) + filter!(kvp -> kvp[2] === nothing, u0map) + filter!(kvp -> kvp[2] === nothing, pmap) for (k, v) in op k = unwrap(k) if isparameter(k) @@ -539,6 +553,7 @@ function build_operating_point( u0map[k] = v end end + return op, missing_unknowns, missing_pars end @@ -749,9 +764,6 @@ function process_SciMLProblem( op, missing_unknowns, missing_pars = build_operating_point( u0map, pmap, defs, cmap, dvs, ps) - filter_missing_values!(op) - filter_missing_values!(u0map) - filter_missing_values!(pmap) substitute_extra_variables!(sys, u0map) substitute_extra_variables!(sys, pmap) From 54bc7146f84d1bca4e0926a6cda78e85c25b9e49 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 4 Feb 2025 11:59:59 +0530 Subject: [PATCH 0855/2176] test: fix some issues with initialization of InverseControlledTank --- test/downstream/inversemodel.jl | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/test/downstream/inversemodel.jl b/test/downstream/inversemodel.jl index 8c2c6bebb9..523f8e6c0e 100644 --- a/test/downstream/inversemodel.jl +++ b/test/downstream/inversemodel.jl @@ -32,7 +32,7 @@ rc = 0.25 # Reference concentration gamma(t), [description = "Reaction speed"] xc(t) = c0, [description = "Concentration"] xT(t) = T0, [description = "Temperature"] - xT_c(t) = T0, [description = "Cooling temperature"] + xT_c(t), [description = "Cooling temperature"] end @components begin @@ -77,7 +77,11 @@ begin end # Create an MTK-compatible constructor - RefFilter(; y0, name) = ODESystem(Fss; name, x0 = init_filter(y0)) + function RefFilter(; name) + sys = ODESystem(Fss; name) + delete!(defaults(sys), @nonamespace(sys.x)[1]) + return sys + end end @mtkmodel InverseControlledTank begin begin @@ -97,10 +101,10 @@ end ff_gain = Gain(k = 1) # To allow turning ff off controller = PI(gainPI.k = 10, T = 500) tank = MixingTank(xc = c_start, xT = T_start, c0 = c0, T0 = T0) - inverse_tank = MixingTank(xc = c_start, xT = T_start, c0 = c0, T0 = T0) + inverse_tank = MixingTank(xc = nothing, xT = T_start, c0 = c0, T0 = T0) feedback = Feedback() add = Add() - filter = RefFilter(y0 = c_start) # Initialize filter states to the initial concentration + filter = RefFilter() noise_filter = FirstOrder(k = 1, T = 1, x = T_start) # limiter = Gain(k=1) limiter = Limiter(y_max = 370, y_min = 250) # Saturate the control input From d626783dd912c54dfc7191c0a8f699c56093abcc Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 4 Feb 2025 12:00:28 +0530 Subject: [PATCH 0856/2176] test: add #3319 as a test --- test/downstream/inversemodel.jl | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/test/downstream/inversemodel.jl b/test/downstream/inversemodel.jl index 523f8e6c0e..7102995b66 100644 --- a/test/downstream/inversemodel.jl +++ b/test/downstream/inversemodel.jl @@ -173,3 +173,32 @@ matrices2, _ = Blocks.get_comp_sensitivity(model, :y; op) # Test that we get the @test_broken matrices1.f_x ≈ matrices2.A[1:7, 1:7] nsys = get_named_comp_sensitivity(model, :y; op) # Test that we get the same result when calling an even higher-level API @test matrices2.A ≈ nsys.A + +@testset "Issue #3319" begin + op1 = Dict( + D(cm.inverse_tank.xT) => 1, + cm.tank.xc => 0.65 + ) + + op2 = Dict( + D(cm.inverse_tank.xT) => 1, + cm.tank.xc => 0.45 + ) + + output = :y + lin_fun, ssys = Blocks.get_sensitivity_function(model, output) + matrices1 = linearize(ssys, lin_fun, op = op1) + matrices2 = linearize(ssys, lin_fun, op = op2) + S1f = ss(matrices1...) + S2f = ss(matrices2...) + @test S1f != S2f + + matrices1, ssys = Blocks.get_sensitivity(model, output; op = op1) + matrices2, ssys = Blocks.get_sensitivity(model, output; op = op2) + S1 = ss(matrices1...) + S2 = ss(matrices2...) + @test S1 != S2 + + @test S1 == S1f + @test S2 == S2f +end From e2dc475e33a6d8b40f0a6e18aacc4ac000551b74 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 4 Feb 2025 13:16:41 +0530 Subject: [PATCH 0857/2176] test: fix initialization of model in tests --- test/input_output_handling.jl | 3 ++- test/split_parameters.jl | 6 ++++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/test/input_output_handling.jl b/test/input_output_handling.jl index de6fc92b5c..382c002108 100644 --- a/test/input_output_handling.jl +++ b/test/input_output_handling.jl @@ -131,7 +131,8 @@ model = ODESystem(eqs, t; systems = [torque, inertia1, inertia2, spring, damper] name = :name) model_outputs = [inertia1.w, inertia2.w, inertia1.phi, inertia2.phi] model_inputs = [torque.tau.u] -matrices, ssys = linearize(model, model_inputs, model_outputs); +matrices, ssys = linearize( + model, model_inputs, model_outputs); @test length(ModelingToolkit.outputs(ssys)) == 4 if VERSION >= v"1.8" # :opaque_closure not supported before diff --git a/test/split_parameters.jl b/test/split_parameters.jl index 2f8667faa8..b4b7314838 100644 --- a/test/split_parameters.jl +++ b/test/split_parameters.jl @@ -164,14 +164,16 @@ function SystemModel(u = nothing; name = :model) t; systems = [torque, inertia1, inertia2, spring, damper, u]) end - ODESystem(eqs, t; systems = [torque, inertia1, inertia2, spring, damper], name) + ODESystem(eqs, t; systems = [torque, inertia1, inertia2, spring, damper], + name) end model = SystemModel() # Model with load disturbance @named d = Step(start_time = 1.0, duration = 10.0, offset = 0.0, height = 1.0) # Disturbance model_outputs = [model.inertia1.w, model.inertia2.w, model.inertia1.phi, model.inertia2.phi] # This is the state realization we want to control inputs = [model.torque.tau.u] -matrices, ssys = ModelingToolkit.linearize(wr(model), inputs, model_outputs) +matrices, ssys = ModelingToolkit.linearize( + wr(model), inputs, model_outputs) # Design state-feedback gain using LQR # Define cost matrices From 5a75d6590948b904ca450d58bd6e9226ad260cd3 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 5 Feb 2025 17:09:03 +0530 Subject: [PATCH 0858/2176] refactor: remove `wrap_array_vars`, `wrap_mtkparameters`, `wrap_parameter_dependencies` --- src/systems/abstractsystem.jl | 209 ---------------------------------- 1 file changed, 209 deletions(-) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index 1fab568cb4..9929e5d010 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -206,217 +206,8 @@ function wrap_assignments(isscalar, assignments; let_block = false) end end -function wrap_parameter_dependencies(sys::AbstractSystem, isscalar) - wrap_assignments(isscalar, [eq.lhs ← eq.rhs for eq in parameter_dependencies(sys)]) -end - -""" - $(TYPEDSIGNATURES) - -Add the necessary assignment statements to allow use of unscalarized array variables -in the generated code. `expr` is the expression returned by the function. `dvs` and -`ps` are the unknowns and parameters of the system `sys` to use in the generated code. -`inputs` can be specified as an array of symbolics if the generated function has inputs. -If `history == true`, the generated function accepts a history function. `cachesyms` are -extra variables (arrays of variables) stored in the cache array(s) of the parameter -object. `extra_args` are extra arguments appended to the end of the argument list. - -The function is assumed to have the signature `f(du, u, h, x, p, cache_syms..., t, extra_args...)` -Where: -- `du` is the optional buffer to write to for in-place functions. -- `u` is the list of unknowns. This argument is not present if `dvs === nothing`. -- `h` is the optional history function, present if `history == true`. -- `x` is the array of inputs, present only if `inputs !== nothing`. Values are assumed - to be in the order of variables passed to `inputs`. -- `p` is the parameter object. -- `cache_syms` are the cache variables. These are part of the splatted parameter object. -- `t` is time, present only if the system is time dependent. -- `extra_args` are the extra arguments passed to the function, present only if - `extra_args` is non-empty. -""" -function wrap_array_vars( - sys::AbstractSystem, exprs; dvs = unknowns(sys), ps = parameters(sys), - inputs = nothing, history = false, cachesyms::Tuple = (), extra_args::Tuple = ()) - isscalar = !(exprs isa AbstractArray) - var_to_arridxs = Dict() - - if dvs === nothing - uind = 0 - else - uind = 1 - for (i, x) in enumerate(dvs) - iscall(x) && operation(x) == getindex || continue - arg = arguments(x)[1] - inds = get!(() -> [], var_to_arridxs, arg) - push!(inds, (uind, i)) - end - end - p_start = uind + 1 + history - rps = (reorder_parameters(sys, ps)..., cachesyms...) - if inputs !== nothing - rps = (inputs, rps...) - end - if has_iv(sys) - rps = (rps..., get_iv(sys)) - end - rps = (rps..., extra_args...) - for sym in reduce(vcat, rps; init = []) - iscall(sym) && operation(sym) == getindex || continue - arg = arguments(sym)[1] - - bufferidx = findfirst(buf -> any(isequal(sym), buf), rps) - idxinbuffer = findfirst(isequal(sym), rps[bufferidx]) - inds = get!(() -> [], var_to_arridxs, arg) - push!(inds, (p_start + bufferidx - 1, idxinbuffer)) - end - - viewsyms = Dict() - splitsyms = Dict() - for (arrsym, idxs) in var_to_arridxs - length(idxs) == length(arrsym) || continue - # allequal(first, idxs) is a 1.11 feature - if allequal(Iterators.map(first, idxs)) - viewsyms[arrsym] = (first(first(idxs)), reshape(last.(idxs), size(arrsym))) - else - splitsyms[arrsym] = reshape(idxs, size(arrsym)) - end - end - if isscalar - function (expr) - Func( - expr.args, - [], - Let( - vcat( - [sym ← :(view($(expr.args[i].name), $idxs)) - for (sym, (i, idxs)) in viewsyms], - [sym ← - MakeArray([expr.args[bufi].elems[vali] for (bufi, vali) in idxs], - expr.args[idxs[1][1]]) for (sym, idxs) in splitsyms] - ), - expr.body, - false - ) - ) - end - else - function (expr) - Func( - expr.args, - [], - Let( - vcat( - [sym ← :(view($(expr.args[i].name), $idxs)) - for (sym, (i, idxs)) in viewsyms], - [sym ← - MakeArray([expr.args[bufi].elems[vali] for (bufi, vali) in idxs], - expr.args[idxs[1][1]]) for (sym, idxs) in splitsyms] - ), - expr.body, - false - ) - ) - end, - function (expr) - Func( - expr.args, - [], - Let( - vcat( - [sym ← :(view($(expr.args[i + 1].name), $idxs)) - for (sym, (i, idxs)) in viewsyms], - [sym ← MakeArray( - [expr.args[bufi + 1].elems[vali] for (bufi, vali) in idxs], - expr.args[idxs[1][1] + 1]) for (sym, idxs) in splitsyms] - ), - expr.body, - false - ) - ) - end - end -end - const MTKPARAMETERS_ARG = Sym{Vector{Vector}}(:___mtkparameters___) -""" - wrap_mtkparameters(sys::AbstractSystem, isscalar::Bool, p_start = 2, offset = Int(is_time_dependent(sys))) - -Return function(s) to be passed to the `wrap_code` keyword of `build_function` which -allow the compiled function to be called as `f(u, p, t)` where `p isa MTKParameters` -instead of `f(u, p..., t)`. `isscalar` denotes whether the function expression being -wrapped is for a scalar value. `p_start` is the index of the argument containing -the first parameter vector in the out-of-place version of the function. For example, -if a history function (DDEs) was passed before `p`, then the function before wrapping -would have the signature `f(u, h, p..., t)` and hence `p_start` would need to be `3`. - -`offset` is the number of arguments at the end of the argument list to ignore. Defaults -to 1 if the system is time-dependent (to ignore `t`) and 0 otherwise. - -The returned function is `identity` if the system does not have an `IndexCache`. -""" -function wrap_mtkparameters(sys::AbstractSystem, isscalar::Bool, p_start = 2, - offset = Int(is_time_dependent(sys))) - if has_index_cache(sys) && get_index_cache(sys) !== nothing - if isscalar - function (expr) - param_args = expr.args[p_start:(end - offset)] - param_buffer_idxs = findall(x -> x isa DestructuredArgs, param_args) - param_buffer_args = param_args[param_buffer_idxs] - destructured_mtkparams = DestructuredArgs( - [x.name for x in param_buffer_args], - MTKPARAMETERS_ARG; inds = param_buffer_idxs) - Func( - [ - expr.args[begin:(p_start - 1)]..., - destructured_mtkparams, - expr.args[(end - offset + 1):end]... - ], - [], - Let(param_buffer_args, expr.body, false) - ) - end - else - function (expr) - param_args = expr.args[p_start:(end - offset)] - param_buffer_idxs = findall(x -> x isa DestructuredArgs, param_args) - param_buffer_args = param_args[param_buffer_idxs] - destructured_mtkparams = DestructuredArgs( - [x.name for x in param_buffer_args], - MTKPARAMETERS_ARG; inds = param_buffer_idxs) - Func( - [ - expr.args[begin:(p_start - 1)]..., - destructured_mtkparams, - expr.args[(end - offset + 1):end]... - ], - [], - Let(param_buffer_args, expr.body, false) - ) - end, - function (expr) - param_args = expr.args[(p_start + 1):(end - offset)] - param_buffer_idxs = findall(x -> x isa DestructuredArgs, param_args) - param_buffer_args = param_args[param_buffer_idxs] - destructured_mtkparams = DestructuredArgs( - [x.name for x in param_buffer_args], - MTKPARAMETERS_ARG; inds = param_buffer_idxs) - Func( - [ - expr.args[begin:p_start]..., - destructured_mtkparams, - expr.args[(end - offset + 1):end]... - ], - [], - Let(param_buffer_args, expr.body, false) - ) - end - end - else - identity - end -end - mutable struct Substitutions subs::Vector{Equation} deps::Vector{Vector{Int}} From 2956f0b36fa3f7dbc5e51a12d378bc35ee4a0493 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 5 Feb 2025 17:10:23 +0530 Subject: [PATCH 0859/2176] feat: add `Initial` operator --- src/systems/abstractsystem.jl | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index 9929e5d010..ea988e4c37 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -650,6 +650,40 @@ function isscheduled(sys::AbstractSystem) end end +struct Initial <: Symbolics.Operator end +Initial(x) = Initial()(x) +SymbolicUtils.promote_symtype(::Type{Initial}, T) = T +SymbolicUtils.isbinop(::Initial) = false +Base.nameof(::Initial) = :Initial +Base.show(io::IO, x::Initial) = print(io, "Initial") +input_timedomain(::Initial, _ = nothing) = Continuous() +output_timedomain(::Initial, _ = nothing) = Continuous() + +function (f::Initial)(x) + iw = Symbolics.iswrapped(x) + x = unwrap(x) + if symbolic_type(x) == NotSymbolic() + return x + end + if iscall(x) && operation(x) isa Differential + x = default_toterm(x) + end + iscall(x) && operation(x) isa Initial && return x + result = if symbolic_type(x) == ArraySymbolic() + Symbolics.array_term(f, toparam(x)) + elseif iscall(x) && operation(x) == getindex + arr = arguments(x)[1] + term(getindex, f(toparam(arr)), arguments(x)[2:end]...) + else + term(f, toparam(x)) + end + result = toparam(result) + if iw + result = wrap(result) + end + return result +end + """ $(TYPEDSIGNATURES) From 3096d09be4bb0228c816ca916f4803a1055aaa78 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 5 Feb 2025 17:10:44 +0530 Subject: [PATCH 0860/2176] feat: add initialization parameters in `complete` --- src/systems/abstractsystem.jl | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index ea988e4c37..1bb73657ba 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -684,6 +684,31 @@ function (f::Initial)(x) return result end +function add_initialization_parameters(sys::AbstractSystem) + is_time_dependent(sys) || return sys + @assert !has_systems(sys) || isempty(get_systems(sys)) + eqs = equations(sys) + if !(eqs isa Vector{Equation}) + eqs = Equation[x for x in eqs if x isa Equation] + end + obs, eqs = unhack_observed(observed(sys), eqs) + all_uvars = Set{BasicSymbolic}() + for x in Iterators.flatten((unknowns(sys), Iterators.map(eq -> eq.lhs, obs))) + x = unwrap(x) + if iscall(x) && operation(x) == getindex + push!(all_uvars, arguments(x)[1]) + else + push!(all_uvars, x) + end + end + all_uvars = collect(all_uvars) + initials = map(Initial(), all_uvars) + # existing_initials = filter(x -> iscall(x) && (operation(x) isa Initial), parameters(sys)) + @set! sys.ps = unique!([get_ps(sys); initials]) + @set! sys.defaults = merge(get_defaults(sys), Dict(initials .=> zero_var.(initials))) + return sys +end + """ $(TYPEDSIGNATURES) @@ -716,6 +741,7 @@ function complete(sys::AbstractSystem; split = true, flatten = true) @set! newsys.parent = complete(sys; split = false, flatten = false) end sys = newsys + sys = add_initialization_parameters(sys) end if split && has_index_cache(sys) @set! sys.index_cache = IndexCache(sys) From c916452b8fc388182ba49db4b4781475be724feb Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 5 Feb 2025 17:11:16 +0530 Subject: [PATCH 0861/2176] feat: optionally filter `Initial` parameters in `parameters` --- src/systems/abstractsystem.jl | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index 1bb73657ba..2bb7db41cf 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -1277,7 +1277,7 @@ Get the parameters of the system `sys` and its subsystems. See also [`@parameters`](@ref) and [`ModelingToolkit.get_ps`](@ref). """ -function parameters(sys::AbstractSystem) +function parameters(sys::AbstractSystem; initial_parameters = !is_time_dependent(sys)) ps = get_ps(sys) if ps == SciMLBase.NullParameters() return [] @@ -1286,7 +1286,12 @@ function parameters(sys::AbstractSystem) ps = first.(ps) end systems = get_systems(sys) - unique(isempty(systems) ? ps : [ps; reduce(vcat, namespace_parameters.(systems))]) + result = unique(isempty(systems) ? ps : + [ps; reduce(vcat, namespace_parameters.(systems))]) + if !initial_parameters + filter!(x -> !iscall(x) || !isa(operation(x), Initial), result) + end + return result end function dependent_parameters(sys::AbstractSystem) @@ -1314,7 +1319,7 @@ function parameter_dependencies(sys::AbstractSystem) end function full_parameters(sys::AbstractSystem) - vcat(parameters(sys), dependent_parameters(sys)) + vcat(parameters(sys; initial_parameters = true), dependent_parameters(sys)) end """ From 665d1d33c4237a83ef8c2b27f64b7cd3ce8a1183 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 5 Feb 2025 17:12:09 +0530 Subject: [PATCH 0862/2176] feat: put `Initial` parameters at the end of tunables --- src/systems/index_cache.jl | 39 ++++++++++++++++++++++---------------- 1 file changed, 23 insertions(+), 16 deletions(-) diff --git a/src/systems/index_cache.jl b/src/systems/index_cache.jl index e4ca0b6b48..d807691cf1 100644 --- a/src/systems/index_cache.jl +++ b/src/systems/index_cache.jl @@ -96,6 +96,7 @@ function IndexCache(sys::AbstractSystem) end tunable_buffers = Dict{Any, Set{BasicSymbolic}}() + initial_param_buffers = Dict{Any, Set{BasicSymbolic}}() constant_buffers = Dict{Any, Set{BasicSymbolic}}() nonnumeric_buffers = Dict{Any, Set{SymbolicParam}}() @@ -191,7 +192,7 @@ function IndexCache(sys::AbstractSystem) [BufferTemplate(symtype, length(buf)) for buf in disc_syms_by_partition]) end - for p in parameters(sys) + for p in parameters(sys; initial_parameters = true) p = unwrap(p) ctype = symtype(p) if ctype <: FnType @@ -206,7 +207,11 @@ function IndexCache(sys::AbstractSystem) (ctype == Real || ctype <: AbstractFloat || ctype <: AbstractArray{Real} || ctype <: AbstractArray{<:AbstractFloat}) - tunable_buffers + if iscall(p) && operation(p) isa Initial + initial_param_buffers + else + tunable_buffers + end else constant_buffers end @@ -246,20 +251,22 @@ function IndexCache(sys::AbstractSystem) tunable_idxs = TunableIndexMap() tunable_buffer_size = 0 - for (i, (_, buf)) in enumerate(tunable_buffers) - for (j, p) in enumerate(buf) - idx = if size(p) == () - tunable_buffer_size + 1 - else - reshape( - (tunable_buffer_size + 1):(tunable_buffer_size + length(p)), size(p)) - end - tunable_buffer_size += length(p) - tunable_idxs[p] = idx - tunable_idxs[default_toterm(p)] = idx - if hasname(p) && (!iscall(p) || operation(p) !== getindex) - symbol_to_variable[getname(p)] = p - symbol_to_variable[getname(default_toterm(p))] = p + for buffers in (tunable_buffers, initial_param_buffers) + for (i, (_, buf)) in enumerate(buffers) + for (j, p) in enumerate(buf) + idx = if size(p) == () + tunable_buffer_size + 1 + else + reshape( + (tunable_buffer_size + 1):(tunable_buffer_size + length(p)), size(p)) + end + tunable_buffer_size += length(p) + tunable_idxs[p] = idx + tunable_idxs[default_toterm(p)] = idx + if hasname(p) && (!iscall(p) || operation(p) !== getindex) + symbol_to_variable[getname(p)] = p + symbol_to_variable[getname(default_toterm(p))] = p + end end end end From aeffa9595d4c87fa6fda6874e3237476231a3513 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 5 Feb 2025 17:12:21 +0530 Subject: [PATCH 0863/2176] feat: add single-argument `reorder_parameters` --- src/systems/index_cache.jl | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/systems/index_cache.jl b/src/systems/index_cache.jl index d807691cf1..a956e17313 100644 --- a/src/systems/index_cache.jl +++ b/src/systems/index_cache.jl @@ -445,7 +445,8 @@ function check_index_map(idxmap, sym) end end -function reorder_parameters(sys::AbstractSystem, ps; kwargs...) +function reorder_parameters( + sys::AbstractSystem, ps = parameters(sys; initial_parameters = true); kwargs...) if has_index_cache(sys) && get_index_cache(sys) !== nothing reorder_parameters(get_index_cache(sys), ps; kwargs...) elseif ps isa Tuple From a726293b573f6bef4c329dad29539d82aff549f7 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 5 Feb 2025 17:12:50 +0530 Subject: [PATCH 0864/2176] fix: fix `zero_var` for array symbolics --- 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 99326aa285..86a7c12779 100644 --- a/src/systems/problem_utils.jl +++ b/src/systems/problem_utils.jl @@ -251,7 +251,7 @@ symbolics return an empty array of the appropriate `eltype`. function zero_var(x::Symbolic{T}) where {V <: Number, T <: Union{V, AbstractArray{V}}} if Symbolics.isarraysymbolic(x) if is_sized_array_symbolic(x) - return zeros(T, size(x)) + return zeros(eltype(T), size(x)) else return T[] end From 2a7432b6fe9432854949421a4507dee6695a3396 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 5 Feb 2025 17:15:11 +0530 Subject: [PATCH 0865/2176] fix: handle `Initial` parameters in `MTKParameters` constructor --- src/systems/parameter_buffer.jl | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/systems/parameter_buffer.jl b/src/systems/parameter_buffer.jl index bc4e62a773..3c4144c8be 100644 --- a/src/systems/parameter_buffer.jl +++ b/src/systems/parameter_buffer.jl @@ -33,10 +33,10 @@ 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)))) + all_ps = Set(unwrap.(parameters(sys; initial_parameters = true))) + union!(all_ps, default_toterm.(unwrap.(parameters(sys; initial_parameters = true)))) if p isa Vector && !(eltype(p) <: Pair) && !isempty(p) - ps = parameters(sys) + ps = parameters(sys; initial_parameters = true) length(p) == length(ps) || error("Invalid parameters") p = ps .=> p end From d19134fce41b8829b888a46643b61ac0945a99fe Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 5 Feb 2025 17:15:38 +0530 Subject: [PATCH 0866/2176] fix: handle `Initial` parameters in `tunable_parameters` --- src/variables.jl | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/variables.jl b/src/variables.jl index 536119f107..0068055832 100644 --- a/src/variables.jl +++ b/src/variables.jl @@ -414,7 +414,7 @@ end ## System interface """ - tunable_parameters(sys, p = parameters(sys); default=true) + tunable_parameters(sys, p = parameters(sys; initial_parameters = true); default=true) Get all parameters of `sys` that are marked as `tunable`. @@ -433,7 +433,8 @@ call `Symbolics.scalarize(tunable_parameters(sys))` and concatenate the resultin See also [`getbounds`](@ref), [`istunable`](@ref), [`MTKParameters`](@ref), [`complete`](@ref) """ -function tunable_parameters(sys, p = parameters(sys); default = true) +function tunable_parameters( + sys, p = parameters(sys; initial_parameters = true); default = true) filter(x -> istunable(x, default), p) end From 6531c87da96eebcfbcd54c10898eb73b36145a62 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 5 Feb 2025 17:15:57 +0530 Subject: [PATCH 0867/2176] fix: handle `Initial` parameters in code generation --- src/inputoutput.jl | 11 +++++------ src/systems/abstractsystem.jl | 6 +++--- src/systems/callbacks.jl | 14 ++++++++------ src/systems/diffeqs/abstractodesystem.jl | 15 ++++++++------- src/systems/diffeqs/odesystem.jl | 2 +- src/systems/diffeqs/sdesystem.jl | 2 +- src/systems/jumps/jumpsystem.jl | 4 ++-- src/systems/nonlinear/initializesystem.jl | 4 +++- src/systems/nonlinear/nonlinearsystem.jl | 13 ++++++++----- src/systems/optimization/constraints_system.jl | 8 +++++--- src/systems/optimization/modelingtoolkitize.jl | 2 +- src/systems/optimization/optimizationsystem.jl | 7 ++++--- 12 files changed, 49 insertions(+), 39 deletions(-) diff --git a/src/inputoutput.jl b/src/inputoutput.jl index 7d2fa875b1..2407ca6942 100644 --- a/src/inputoutput.jl +++ b/src/inputoutput.jl @@ -211,7 +211,7 @@ function generate_control_function(sys::AbstractODESystem, inputs = unbound_inpu sys, _ = io_preprocessing(sys, inputs, []; simplify, kwargs...) dvs = unknowns(sys) - ps = parameters(sys) + ps = parameters(sys; initial_parameters = true) ps = setdiff(ps, inputs) if disturbance_inputs !== nothing # remove from inputs since we do not want them as actual inputs to the dynamics @@ -234,16 +234,14 @@ function generate_control_function(sys::AbstractODESystem, inputs = unbound_inpu [eq.rhs for eq in eqs] # TODO: add an optional check on the ordering of observed equations - u = map(x -> time_varying_as_func(value(x), sys), dvs) - p = map(x -> time_varying_as_func(value(x), sys), ps) - p = reorder_parameters(sys, p) + p = reorder_parameters(sys, ps) t = get_iv(sys) # pre = has_difference ? (ex -> ex) : get_postprocess_fbody(sys) if disturbance_argument - args = (u, inputs, p..., t, disturbance_inputs) + args = (dvs, inputs, p..., t, disturbance_inputs) else - args = (u, inputs, p..., t) + args = (dvs, inputs, p..., t) end if implicit_dae ddvs = map(Differential(get_iv(sys)), dvs) @@ -252,6 +250,7 @@ function generate_control_function(sys::AbstractODESystem, inputs = unbound_inpu f = build_function_wrapper(sys, rhss, args...; p_start = 3 + implicit_dae, p_end = length(p) + 2 + implicit_dae) f = eval_or_rgf.(f; eval_expression, eval_module) + ps = setdiff(parameters(sys), inputs, disturbance_inputs) (; f, dvs, ps, io_sys = sys) end diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index 2bb7db41cf..ee0017150d 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -161,7 +161,7 @@ time-independent systems. If `split=true` (the default) was passed to [`complete object. """ function generate_custom_function(sys::AbstractSystem, exprs, dvs = unknowns(sys), - ps = parameters(sys); + ps = parameters(sys; initial_parameters = true); expression = Val{true}, eval_expression = false, eval_module = @__MODULE__, cachesyms::Tuple = (), kwargs...) if !iscomplete(sys) @@ -533,7 +533,7 @@ function SymbolicIndexingInterface.get_all_timeseries_indexes(sys::AbstractSyste end function SymbolicIndexingInterface.parameter_symbols(sys::AbstractSystem) - return parameters(sys) + return parameters(sys; initial_parameters = true) end function SymbolicIndexingInterface.is_independent_variable(sys::AbstractSystem, sym) @@ -2432,7 +2432,7 @@ function linearize_symbolic(sys::AbstractSystem, inputs, kwargs...) sts = unknowns(sys) t = get_iv(sys) - ps = parameters(sys) + ps = parameters(sys; initial_parameters = true) p = reorder_parameters(sys, ps) fun_expr = generate_function(sys, sts, ps; expression = Val{true})[1] diff --git a/src/systems/callbacks.jl b/src/systems/callbacks.jl index a58ff3f8ec..5cefe65651 100644 --- a/src/systems/callbacks.jl +++ b/src/systems/callbacks.jl @@ -682,7 +682,7 @@ function compile_affect(eqs::Vector{Equation}, cb, sys, dvs, ps; outputidxs = no end function generate_rootfinding_callback(sys::AbstractTimeDependentSystem, - dvs = unknowns(sys), ps = parameters(sys); kwargs...) + dvs = unknowns(sys), ps = parameters(sys; initial_parameters = true); kwargs...) cbs = continuous_events(sys) isempty(cbs) && return nothing generate_rootfinding_callback(cbs, sys, dvs, ps; kwargs...) @@ -693,7 +693,7 @@ generate_rootfinding_callback and thus we can produce a ContinuousCallback inste """ function generate_single_rootfinding_callback( eq, cb, sys::AbstractTimeDependentSystem, dvs = unknowns(sys), - ps = parameters(sys); kwargs...) + ps = parameters(sys; initial_parameters = true); kwargs...) if !isequal(eq.lhs, 0) eq = 0 ~ eq.lhs - eq.rhs end @@ -736,7 +736,7 @@ end function generate_vector_rootfinding_callback( cbs, sys::AbstractTimeDependentSystem, dvs = unknowns(sys), - ps = parameters(sys); rootfind = SciMLBase.RightRootFind, + ps = parameters(sys; initial_parameters = true); rootfind = SciMLBase.RightRootFind, reinitialization = SciMLBase.CheckInit(), kwargs...) eqs = map(cb -> flatten_equations(cb.eqs), cbs) num_eqs = length.(eqs) @@ -861,7 +861,7 @@ function compile_affect_fn(cb, sys::AbstractTimeDependentSystem, dvs, ps, kwargs end function generate_rootfinding_callback(cbs, sys::AbstractTimeDependentSystem, - dvs = unknowns(sys), ps = parameters(sys); kwargs...) + dvs = unknowns(sys), ps = parameters(sys; initial_parameters = true); kwargs...) eqs = map(cb -> flatten_equations(cb.eqs), cbs) num_eqs = length.(eqs) total_eqs = sum(num_eqs) @@ -949,7 +949,9 @@ function invalid_variables(sys, expr) end function unassignable_variables(sys, expr) assignable_syms = reduce( - vcat, Symbolics.scalarize.(vcat(unknowns(sys), parameters(sys))); init = []) + vcat, Symbolics.scalarize.(vcat( + unknowns(sys), parameters(sys; initial_parameters = true))); + init = []) written = reduce(vcat, Symbolics.scalarize.(vars(expr)); init = []) return filter( x -> !any(isequal(x), assignable_syms), written) @@ -1075,7 +1077,7 @@ function generate_discrete_callback(cb, sys, dvs, ps; postprocess_affect_expr! = end function generate_discrete_callbacks(sys::AbstractSystem, dvs = unknowns(sys), - ps = parameters(sys); kwargs...) + ps = parameters(sys; initial_parameters = true); kwargs...) has_discrete_events(sys) || return nothing symcbs = discrete_events(sys) isempty(symcbs) && return nothing diff --git a/src/systems/diffeqs/abstractodesystem.jl b/src/systems/diffeqs/abstractodesystem.jl index b4c51b52e9..5d3fa93cde 100644 --- a/src/systems/diffeqs/abstractodesystem.jl +++ b/src/systems/diffeqs/abstractodesystem.jl @@ -105,7 +105,8 @@ function calculate_control_jacobian(sys::AbstractODESystem; end function generate_tgrad( - sys::AbstractODESystem, dvs = unknowns(sys), ps = parameters(sys); + sys::AbstractODESystem, dvs = unknowns(sys), ps = parameters( + sys; initial_parameters = true); simplify = false, kwargs...) tgrad = calculate_tgrad(sys, simplify = simplify) p = reorder_parameters(sys, ps) @@ -117,7 +118,7 @@ function generate_tgrad( end function generate_jacobian(sys::AbstractODESystem, dvs = unknowns(sys), - ps = parameters(sys); + ps = parameters(sys; initial_parameters = true); simplify = false, sparse = false, kwargs...) jac = calculate_jacobian(sys; simplify = simplify, sparse = sparse) p = reorder_parameters(sys, ps) @@ -129,7 +130,7 @@ function generate_jacobian(sys::AbstractODESystem, dvs = unknowns(sys), end function generate_control_jacobian(sys::AbstractODESystem, dvs = unknowns(sys), - ps = parameters(sys); + ps = parameters(sys; initial_parameters = true); simplify = false, sparse = false, kwargs...) jac = calculate_control_jacobian(sys; simplify = simplify, sparse = sparse) p = reorder_parameters(sys, ps) @@ -137,7 +138,7 @@ function generate_control_jacobian(sys::AbstractODESystem, dvs = unknowns(sys), end function generate_dae_jacobian(sys::AbstractODESystem, dvs = unknowns(sys), - ps = parameters(sys); simplify = false, sparse = false, + ps = parameters(sys; initial_parameters = true); simplify = false, sparse = false, kwargs...) jac_u = calculate_jacobian(sys; simplify = simplify, sparse = sparse) derivatives = Differential(get_iv(sys)).(unknowns(sys)) @@ -153,7 +154,7 @@ function generate_dae_jacobian(sys::AbstractODESystem, dvs = unknowns(sys), end function generate_function(sys::AbstractODESystem, dvs = unknowns(sys), - ps = parameters(sys); + ps = parameters(sys; initial_parameters = true); implicit_dae = false, ddvs = implicit_dae ? map(Differential(get_iv(sys)), dvs) : nothing, @@ -699,7 +700,7 @@ function SymbolicTstops( term(:, t0, unwrap(val), t1; type = AbstractArray{Real}) end end - rps = reorder_parameters(sys, parameters(sys)) + rps = reorder_parameters(sys) tstops, _ = build_function_wrapper(sys, tstops, rps..., t0, @@ -825,7 +826,7 @@ function DiffEqBase.DAEProblem{iip}(sys::AbstractODESystem, du0map, u0map, tspan end function generate_history(sys::AbstractODESystem, u0; expression = Val{false}, kwargs...) - p = reorder_parameters(sys, parameters(sys)) + p = reorder_parameters(sys) build_function_wrapper( sys, u0, p..., get_iv(sys); expression, p_start = 1, p_end = length(p), similarto = typeof(u0), wrap_delays = false, kwargs...) diff --git a/src/systems/diffeqs/odesystem.jl b/src/systems/diffeqs/odesystem.jl index 96fd8534ae..0b2adaedb5 100644 --- a/src/systems/diffeqs/odesystem.jl +++ b/src/systems/diffeqs/odesystem.jl @@ -432,7 +432,7 @@ function build_explicit_observed_function(sys, ts; eval_module = @__MODULE__, output_type = Array, checkbounds = true, - ps = parameters(sys), + ps = parameters(sys; initial_parameters = true), return_inplace = false, param_only = false, op = Operator, diff --git a/src/systems/diffeqs/sdesystem.jl b/src/systems/diffeqs/sdesystem.jl index 92a8ddd710..00265cfc67 100644 --- a/src/systems/diffeqs/sdesystem.jl +++ b/src/systems/diffeqs/sdesystem.jl @@ -418,7 +418,7 @@ function __get_num_diag_noise(mat) end function generate_diffusion_function(sys::SDESystem, dvs = unknowns(sys), - ps = parameters(sys); isdde = false, kwargs...) + ps = parameters(sys; initial_parameters = true); isdde = false, kwargs...) eqs = get_noiseeqs(sys) p = reorder_parameters(sys, ps) return build_function_wrapper(sys, eqs, dvs, p..., get_iv(sys); kwargs...) diff --git a/src/systems/jumps/jumpsystem.jl b/src/systems/jumps/jumpsystem.jl index 4e72d0853a..164c38e161 100644 --- a/src/systems/jumps/jumpsystem.jl +++ b/src/systems/jumps/jumpsystem.jl @@ -282,7 +282,7 @@ function generate_rate_function(js::JumpSystem, rate) csubs = Dict(c => getdefault(c) for c in consts) rate = substitute(rate, csubs) end - p = reorder_parameters(js, parameters(js)) + p = reorder_parameters(js) rf = build_function_wrapper(js, rate, unknowns(js), p..., get_iv(js), expression = Val{true}) @@ -634,7 +634,7 @@ end function JumpSysMajParamMapper(js::JumpSystem, p; jseqs = nothing, rateconsttype = Float64) eqs = (jseqs === nothing) ? equations(js) : jseqs paramexprs = [maj.scaled_rates for maj in eqs.x[1]] - psyms = reduce(vcat, reorder_parameters(js, parameters(js)); init = []) + psyms = reduce(vcat, reorder_parameters(js); init = []) paramdict = Dict(value(k) => value(v) for (k, v) in zip(psyms, vcat(p...))) JumpSysMajParamMapper{typeof(paramexprs), typeof(psyms), rateconsttype}(paramexprs, psyms, diff --git a/src/systems/nonlinear/initializesystem.jl b/src/systems/nonlinear/initializesystem.jl index f7fe7ea077..1988534c8c 100644 --- a/src/systems/nonlinear/initializesystem.jl +++ b/src/systems/nonlinear/initializesystem.jl @@ -339,7 +339,9 @@ end function ReconstructInitializeprob( srcsys::AbstractSystem, dstsys::AbstractSystem; remap = Dict()) - syms = reduce(vcat, reorder_parameters(dstsys, parameters(dstsys)); init = []) + syms = reduce( + vcat, reorder_parameters(dstsys, parameters(dstsys; initial_parameters = true)); + init = []) getter = getu(srcsys, map(x -> get(remap, x, x), syms)) setter = setp_oop(dstsys, syms) return ReconstructInitializeprob(getter, setter) diff --git a/src/systems/nonlinear/nonlinearsystem.jl b/src/systems/nonlinear/nonlinearsystem.jl index b5256ca4ca..6f5de67fa7 100644 --- a/src/systems/nonlinear/nonlinearsystem.jl +++ b/src/systems/nonlinear/nonlinearsystem.jl @@ -248,7 +248,8 @@ function calculate_jacobian(sys::NonlinearSystem; sparse = false, simplify = fal end function generate_jacobian( - sys::NonlinearSystem, vs = unknowns(sys), ps = parameters(sys); + sys::NonlinearSystem, vs = unknowns(sys), ps = parameters( + sys; initial_parameters = true); sparse = false, simplify = false, kwargs...) jac = calculate_jacobian(sys, sparse = sparse, simplify = simplify) p = reorder_parameters(sys, ps) @@ -268,7 +269,8 @@ function calculate_hessian(sys::NonlinearSystem; sparse = false, simplify = fals end function generate_hessian( - sys::NonlinearSystem, vs = unknowns(sys), ps = parameters(sys); + sys::NonlinearSystem, vs = unknowns(sys), ps = parameters( + sys; initial_parameters = true); sparse = false, simplify = false, kwargs...) hess = calculate_hessian(sys, sparse = sparse, simplify = simplify) p = reorder_parameters(sys, ps) @@ -276,7 +278,8 @@ function generate_hessian( end function generate_function( - sys::NonlinearSystem, dvs = unknowns(sys), ps = parameters(sys); + sys::NonlinearSystem, dvs = unknowns(sys), ps = parameters( + sys; initial_parameters = true); scalar = false, kwargs...) rhss = [deq.rhs for deq in equations(sys)] dvs′ = value.(dvs) @@ -569,7 +572,7 @@ end function CacheWriter(sys::AbstractSystem, buffer_types::Vector{TypeT}, exprs::Dict{TypeT, Vector{Any}}, solsyms, obseqs::Vector{Equation}; eval_expression = false, eval_module = @__MODULE__) - ps = parameters(sys) + ps = parameters(sys; initial_parameters = true) rps = reorder_parameters(sys, ps) obs_assigns = [eq.lhs ← eq.rhs for eq in obseqs] body = map(eachindex(buffer_types), buffer_types) do i, T @@ -589,7 +592,7 @@ struct SCCNonlinearFunction{iip} end function SCCNonlinearFunction{iip}( sys::NonlinearSystem, _eqs, _dvs, _obs, cachesyms; eval_expression = false, eval_module = @__MODULE__, kwargs...) where {iip} - ps = parameters(sys) + ps = parameters(sys; initial_parameters = true) rps = reorder_parameters(sys, ps) obs_assignments = [eq.lhs ← eq.rhs for eq in _obs] diff --git a/src/systems/optimization/constraints_system.jl b/src/systems/optimization/constraints_system.jl index 275079297a..06795916f8 100644 --- a/src/systems/optimization/constraints_system.jl +++ b/src/systems/optimization/constraints_system.jl @@ -171,7 +171,8 @@ function calculate_jacobian(sys::ConstraintsSystem; sparse = false, simplify = f end function generate_jacobian( - sys::ConstraintsSystem, vs = unknowns(sys), ps = parameters(sys); + sys::ConstraintsSystem, vs = unknowns(sys), ps = parameters( + sys; initial_parameters = true); sparse = false, simplify = false, kwargs...) jac = calculate_jacobian(sys, sparse = sparse, simplify = simplify) p = reorder_parameters(sys, ps) @@ -190,7 +191,8 @@ function calculate_hessian(sys::ConstraintsSystem; sparse = false, simplify = fa end function generate_hessian( - sys::ConstraintsSystem, vs = unknowns(sys), ps = parameters(sys); + sys::ConstraintsSystem, vs = unknowns(sys), ps = parameters( + sys; initial_parameters = true); sparse = false, simplify = false, kwargs...) hess = calculate_hessian(sys, sparse = sparse, simplify = simplify) p = reorder_parameters(sys, ps) @@ -198,7 +200,7 @@ function generate_hessian( end function generate_function(sys::ConstraintsSystem, dvs = unknowns(sys), - ps = parameters(sys); + ps = parameters(sys; initial_parameters = true); kwargs...) lhss = generate_canonical_form_lhss(sys) p = reorder_parameters(sys, value.(ps)) diff --git a/src/systems/optimization/modelingtoolkitize.jl b/src/systems/optimization/modelingtoolkitize.jl index 0228a944cc..27dccc251a 100644 --- a/src/systems/optimization/modelingtoolkitize.jl +++ b/src/systems/optimization/modelingtoolkitize.jl @@ -53,7 +53,7 @@ function modelingtoolkitize(prob::DiffEqBase.OptimizationProblem; idx = parameter_index(prob, sym) old_to_new[unwrap(sym)] = unwrap(p_names[idx]) end - order = reorder_parameters(prob.f.sys, parameters(prob.f.sys)) + order = reorder_parameters(prob.f.sys) for arr in order for i in eachindex(arr) arr[i] = old_to_new[arr[i]] diff --git a/src/systems/optimization/optimizationsystem.jl b/src/systems/optimization/optimizationsystem.jl index 3ae98be084..c1c2237852 100644 --- a/src/systems/optimization/optimizationsystem.jl +++ b/src/systems/optimization/optimizationsystem.jl @@ -194,7 +194,7 @@ function calculate_gradient(sys::OptimizationSystem) end function generate_gradient(sys::OptimizationSystem, vs = unknowns(sys), - ps = parameters(sys); kwargs...) + ps = parameters(sys; initial_parameters = true); kwargs...) grad = calculate_gradient(sys) p = reorder_parameters(sys, ps) return build_function_wrapper(sys, grad, vs, p...; kwargs...) @@ -205,7 +205,8 @@ function calculate_hessian(sys::OptimizationSystem) end function generate_hessian( - sys::OptimizationSystem, vs = unknowns(sys), ps = parameters(sys); + sys::OptimizationSystem, vs = unknowns(sys), ps = parameters( + sys; initial_parameters = true); sparse = false, kwargs...) if sparse hess = sparsehessian(objective(sys), unknowns(sys)) @@ -217,7 +218,7 @@ function generate_hessian( end function generate_function(sys::OptimizationSystem, vs = unknowns(sys), - ps = parameters(sys); + ps = parameters(sys; initial_parameters = true); kwargs...) eqs = objective(sys) p = reorder_parameters(sys, ps) From f5cce1dd9a89768efc379bd6e2d5f3e8bda15f17 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 5 Feb 2025 17:16:20 +0530 Subject: [PATCH 0868/2176] test: fix usage of incorrect `MTKParameters` object in `generate_custom_function` test --- test/generate_custom_function.jl | 1 + 1 file changed, 1 insertion(+) diff --git a/test/generate_custom_function.jl b/test/generate_custom_function.jl index 9b64b20c12..ea1e429e30 100644 --- a/test/generate_custom_function.jl +++ b/test/generate_custom_function.jl @@ -30,6 +30,7 @@ fn5 = generate_custom_function(sys, ifelse(!p4, p1, p2[2]); expression = Val(fal @variables x y[1:3] sys = complete(NonlinearSystem(Equation[], [x; y], [p1, p2, p3, p4]; name = :sys)) +p = MTKParameters(sys, []) fn1 = generate_custom_function(sys, x + y[1] + p1 + p2[1] + p3; expression = Val(false)) @test fn1(u0, p) == 6.0 From c8f488d3b6a325ca29dfeca7af297cffe3907e0d Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 5 Feb 2025 17:17:20 +0530 Subject: [PATCH 0869/2176] test: refactor parameter dependency tests --- test/parameter_dependencies.jl | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/test/parameter_dependencies.jl b/test/parameter_dependencies.jl index 1f1aab9953..7f71120564 100644 --- a/test/parameter_dependencies.jl +++ b/test/parameter_dependencies.jl @@ -26,8 +26,8 @@ using NonlinearSolve continuous_events = [cb1, cb2], discrete_events = [cb3] ) - @test isequal(only(parameters(sys)), p1) - @test Set(full_parameters(sys)) == Set([p1, p2]) + @test !(p2 in Set(parameters(sys))) + @test p2 in Set(full_parameters(sys)) prob = ODEProblem(sys, [x => 1.0], (0.0, 1.5), jac = true) @test prob.ps[p1] == 1.0 @test prob.ps[p2] == 2.0 @@ -82,8 +82,8 @@ end parameter_dependencies = [p2 => 2p1] ) sys = extend(sys2, sys1) - @test isequal(only(parameters(sys)), p1) - @test Set(full_parameters(sys)) == Set([p1, p2]) + @test !(p2 in Set(parameters(sys))) + @test p2 in Set(full_parameters(sys)) prob = ODEProblem(complete(sys)) get_dep = getu(prob, 2p2) @test get_dep(prob) == 4 @@ -259,8 +259,8 @@ end @named sys = ODESystem(eqs, t) @named sdesys = SDESystem(sys, noiseeqs; parameter_dependencies = [ρ => 2σ]) sdesys = complete(sdesys) - @test Set(parameters(sdesys)) == Set([σ, β]) - @test Set(full_parameters(sdesys)) == Set([σ, β, ρ]) + @test !(ρ in Set(parameters(sdesys))) + @test ρ in Set(full_parameters(sdesys)) prob = SDEProblem( sdesys, [x => 1.0, y => 0.0, z => 0.0], (0.0, 100.0), [σ => 10.0, β => 2.33]) @@ -358,17 +358,18 @@ end ps = prob.p buffer, repack, _ = canonicalize(Tunable(), ps) - @test only(buffer) == 3.0 - buffer[1] = 4.0 + idx = parameter_index(sys, p1) + @test buffer[idx.idx] == 3.0 + buffer[idx.idx] = 4.0 ps = repack(buffer) @test getp(sys, p1)(ps) == 4.0 @test getp(sys, p2)(ps) == 8.0 - replace!(Tunable(), ps, [1.0]) + replace!(Tunable(), ps, ones(length(ps.tunable))) @test getp(sys, p1)(ps) == 1.0 @test getp(sys, p2)(ps) == 2.0 - ps2 = replace(Tunable(), ps, [2.0]) + ps2 = replace(Tunable(), ps, 2 .* ps.tunable) @test getp(sys, p1)(ps2) == 2.0 @test getp(sys, p2)(ps2) == 4.0 end From 1fc8c5f70e51399f85bb95ea61a6f90630a45821 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 5 Feb 2025 17:26:54 +0530 Subject: [PATCH 0870/2176] refactor: rename `build_operating_point` to `build_operating_point!` --- src/systems/nonlinear/initializesystem.jl | 2 +- src/systems/problem_utils.jl | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/systems/nonlinear/initializesystem.jl b/src/systems/nonlinear/initializesystem.jl index 1988534c8c..1cb9dee3bf 100644 --- a/src/systems/nonlinear/initializesystem.jl +++ b/src/systems/nonlinear/initializesystem.jl @@ -520,7 +520,7 @@ function SciMLBase.remake_initialization_data( filter_missing_values!(u0map) filter_missing_values!(pmap) - op, missing_unknowns, missing_pars = build_operating_point( + op, missing_unknowns, missing_pars = build_operating_point!( u0map, pmap, defs, cmap, dvs, ps) kws = maybe_build_initialization_problem( sys, op, u0map, pmap, t0, defs, guesses, missing_unknowns; diff --git a/src/systems/problem_utils.jl b/src/systems/problem_utils.jl index 86a7c12779..c25ae5ba48 100644 --- a/src/systems/problem_utils.jl +++ b/src/systems/problem_utils.jl @@ -526,7 +526,7 @@ no values can be determined, and the list of parameters for which no values can Also updates `u0map` and `pmap` in-place to contain all the initial conditions in `op`, split by unknowns and parameters respectively. """ -function build_operating_point( +function build_operating_point!( u0map::AbstractDict, pmap::AbstractDict, defs::AbstractDict, cmap, dvs, ps) op = add_toterms(u0map) missing_unknowns = add_fallbacks!(op, dvs, defs) @@ -762,7 +762,7 @@ function process_SciMLProblem( is_time_dependent(sys) || add_observed!(sys, u0map) - op, missing_unknowns, missing_pars = build_operating_point( + op, missing_unknowns, missing_pars = build_operating_point!( u0map, pmap, defs, cmap, dvs, ps) substitute_extra_variables!(sys, u0map) substitute_extra_variables!(sys, pmap) From 2ec306b8af347c9050b97d8150d75f86291656b1 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 7 Feb 2025 14:08:09 +0530 Subject: [PATCH 0871/2176] HACK: fix: add `maketerm` for `Initial` operator --- src/systems/abstractsystem.jl | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index ee0017150d..74e10352e9 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -684,6 +684,10 @@ function (f::Initial)(x) return result end +function SymbolicUtils.maketerm(::Type{<:BasicSymbolic}, ::Initial, args, meta) + return metadata(Initial()(args...), meta) +end + function add_initialization_parameters(sys::AbstractSystem) is_time_dependent(sys) || return sys @assert !has_systems(sys) || isempty(get_systems(sys)) From ecaa585a2e5e0267f4f27582a4832ec54182b326 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 7 Feb 2025 14:08:36 +0530 Subject: [PATCH 0872/2176] fix: filter existing `Initial`s in `add_initialization_parameters` --- src/systems/abstractsystem.jl | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index 74e10352e9..0deef1bc4c 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -707,9 +707,15 @@ function add_initialization_parameters(sys::AbstractSystem) end all_uvars = collect(all_uvars) initials = map(Initial(), all_uvars) - # existing_initials = filter(x -> iscall(x) && (operation(x) isa Initial), parameters(sys)) - @set! sys.ps = unique!([get_ps(sys); initials]) - @set! sys.defaults = merge(get_defaults(sys), Dict(initials .=> zero_var.(initials))) + existing_initials = filter( + x -> iscall(x) && (operation(x) isa Initial), parameters(sys)) + @set! sys.ps = unique!([setdiff(get_ps(sys), existing_initials); initials]) + defs = copy(get_defaults(sys)) + for x in existing_initials + delete!(defs, x) + end + merge!(defs, Dict(initials .=> zero_var.(initials))) + @set! sys.defaults = defs return sys end From 5150659d3c83f33410707bbe05e519028d3c2628 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 7 Feb 2025 14:09:37 +0530 Subject: [PATCH 0873/2176] fix: handle `Initial` parameters in `flatten` --- src/systems/diffeqs/odesystem.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/systems/diffeqs/odesystem.jl b/src/systems/diffeqs/odesystem.jl index 0b2adaedb5..8febfc253b 100644 --- a/src/systems/diffeqs/odesystem.jl +++ b/src/systems/diffeqs/odesystem.jl @@ -361,7 +361,7 @@ function flatten(sys::ODESystem, noeqs = false) return ODESystem(noeqs ? Equation[] : equations(sys), get_iv(sys), unknowns(sys), - parameters(sys), + parameters(sys; initial_parameters = true), parameter_dependencies = parameter_dependencies(sys), guesses = guesses(sys), observed = observed(sys), From 50bfcdd14336cd42219b2355e6c5ec442a73952a Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 7 Feb 2025 14:10:30 +0530 Subject: [PATCH 0874/2176] refactor: use problem building utils in `MTKParameters` constructor --- src/systems/parameter_buffer.jl | 99 ++++++++------------------------- 1 file changed, 24 insertions(+), 75 deletions(-) diff --git a/src/systems/parameter_buffer.jl b/src/systems/parameter_buffer.jl index 3c4144c8be..07b47a5ec4 100644 --- a/src/systems/parameter_buffer.jl +++ b/src/systems/parameter_buffer.jl @@ -27,7 +27,7 @@ the default behavior). """ function MTKParameters( sys::AbstractSystem, p, u0 = Dict(); tofloat = false, use_union = false, - t0 = nothing) + t0 = nothing, substitution_limit = 1000) ic = if has_index_cache(sys) && get_index_cache(sys) !== nothing get_index_cache(sys) else @@ -35,83 +35,32 @@ function MTKParameters( end all_ps = Set(unwrap.(parameters(sys; initial_parameters = true))) union!(all_ps, default_toterm.(unwrap.(parameters(sys; initial_parameters = true)))) - if p isa Vector && !(eltype(p) <: Pair) && !isempty(p) - ps = parameters(sys; initial_parameters = true) - length(p) == length(ps) || error("Invalid 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) - elseif u0 isa AbstractArray && !isempty(u0) - u0 = Dict(unknowns(sys) .=> vec(u0)) - elseif u0 === nothing || isempty(u0) - u0 = Dict() - end - defs = merge(defs, u0) - defs = merge(Dict(eq.lhs => eq.rhs for eq in observed(sys)), defs) - bigdefs = merge(defs, p) + + dvs = unknowns(sys) + ps = parameters(sys; initial_parameters = true) + u0 = to_varmap(u0, dvs) + symbols_to_symbolics!(sys, u0) + p = to_varmap(p, ps) + symbols_to_symbolics!(sys, p) + defs = add_toterms(recursive_unwrap(defaults(sys))) + cmap, cs = get_cmap(sys) + + is_time_dependent(sys) && add_observed!(sys, u0) + add_parameter_dependencies!(sys, p) + + op, missing_unknowns, missing_pars = build_operating_point!(sys, + u0, p, defs, cmap, dvs, ps) + if t0 !== nothing - bigdefs[get_iv(sys)] = t0 - end - p = Dict() - missing_params = Set() - pdeps = has_parameter_dependencies(sys) ? parameter_dependencies(sys) : [] - - for sym in all_ps - ttsym = default_toterm(sym) - isarr = iscall(sym) && operation(sym) === getindex - arrparent = isarr ? arguments(sym)[1] : nothing - ttarrparent = isarr ? default_toterm(arrparent) : nothing - pname = hasname(sym) ? getname(sym) : nothing - ttpname = hasname(ttsym) ? getname(ttsym) : nothing - p[sym] = p[ttsym] = if haskey(bigdefs, sym) - bigdefs[sym] - elseif haskey(bigdefs, ttsym) - bigdefs[ttsym] - elseif haskey(bigdefs, pname) - isarr ? bigdefs[pname][arguments(sym)[2:end]...] : bigdefs[pname] - elseif haskey(bigdefs, ttpname) - isarr ? bigdefs[ttpname][arguments(sym)[2:end]...] : bigdefs[pname] - elseif isarr && haskey(bigdefs, arrparent) - bigdefs[arrparent][arguments(sym)[2:end]...] - elseif isarr && haskey(bigdefs, ttarrparent) - bigdefs[ttarrparent][arguments(sym)[2:end]...] - end - if get(p, sym, nothing) === nothing - push!(missing_params, sym) - continue - end - # We may encounter the `ttsym` version first, add it to `missing_params` - # then encounter the "normal" version of a parameter or vice versa - # Remove the old one in `missing_params` just in case - delete!(missing_params, sym) - delete!(missing_params, ttsym) - end - - if !isempty(pdeps) - for eq in pdeps - sym = eq.lhs - expr = eq.rhs - sym = unwrap(sym) - ttsym = default_toterm(sym) - delete!(missing_params, sym) - delete!(missing_params, ttsym) - p[sym] = p[ttsym] = expr - end + op[get_iv(sys)] = t0 end - isempty(missing_params) || throw(MissingParametersError(collect(missing_params))) - p = Dict(unwrap(k) => (bigdefs[unwrap(k)] = fixpoint_sub(v, bigdefs)) for (k, v) in p) - for (sym, _) in p - if iscall(sym) && operation(sym) === getindex && - first(arguments(sym)) in all_ps - error("Scalarized parameter values ($sym) are not supported. Instead of `[p[1] => 1.0, p[2] => 2.0]` use `[p => [1.0, 2.0]]`") - end + isempty(missing_pars) || throw(MissingParametersError(collect(missing_pars))) + evaluate_varmap!(op, ps; limit = substitution_limit) + + p = op + filter!(p) do kvp + kvp[1] in all_ps end tunable_buffer = Vector{ic.tunable_buffer_size.type}( From 5432f51e7bd26db808941d18026dcf2fd13c0574 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 7 Feb 2025 14:10:51 +0530 Subject: [PATCH 0875/2176] fix: fix scalarization of array defaults in `structural_simplify` --- src/systems/systems.jl | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/src/systems/systems.jl b/src/systems/systems.jl index f8630f2d20..ef0f966eee 100644 --- a/src/systems/systems.jl +++ b/src/systems/systems.jl @@ -53,17 +53,6 @@ function structural_simplify( @set! newsys.parent = complete(sys; split, flatten = false) end newsys = complete(newsys; split) - if has_defaults(newsys) && (defs = get_defaults(newsys)) !== nothing - ks = collect(keys(defs)) # take copy to avoid mutating defs while iterating. - for k in ks - if Symbolics.isarraysymbolic(k) && Symbolics.shape(k) !== Symbolics.Unknown() - defs[k] === missing && continue - for i in eachindex(k) - defs[k[i]] = defs[k][i] - end - end - end - end if newsys′ isa Tuple idxs = [parameter_index(newsys, i) for i in io[1]] return newsys, idxs From 49a8ccf57df145c8fb7b6edbc479572cedfc062f Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 7 Feb 2025 14:13:50 +0530 Subject: [PATCH 0876/2176] test: fix tests taking into account new initialization parameters --- test/input_output_handling.jl | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/test/input_output_handling.jl b/test/input_output_handling.jl index 382c002108..b1d68bc323 100644 --- a/test/input_output_handling.jl +++ b/test/input_output_handling.jl @@ -169,7 +169,7 @@ end @test isequal(dvs[], x) @test isempty(ps) - p = nothing + p = [rand()] x = [rand()] u = [rand()] @test f[1](x, u, p, 1) == -x + u @@ -187,7 +187,7 @@ end @test isequal(dvs[], x) @test isempty(ps) - p = nothing + p = [rand()] x = [rand()] u = [rand()] @test f[1](x, u, p, 1) == -x + u @@ -205,7 +205,7 @@ end @test isequal(dvs[], x) @test isempty(ps) - p = nothing + p = [rand()] x = [rand()] u = [rand()] d = [rand()] @@ -431,7 +431,7 @@ matrices, ssys = linearize(augmented_sys, (; io_sys,) = ModelingToolkit.generate_control_function(sys, simplify = true) obsfn = ModelingToolkit.build_explicit_observed_function( io_sys, [x + u * t]; inputs = [u]) - @test obsfn([1.0], [2.0], nothing, 3.0) == [7.0] + @test obsfn([1.0], [2.0], MTKParameters(io_sys, []), 3.0) == [7.0] end # https://github.com/SciML/ModelingToolkit.jl/issues/2896 @@ -441,8 +441,8 @@ end eqs = [D(x) ~ c * x] @named sys = ODESystem(eqs, t, [x], []) - f, dvs, ps = ModelingToolkit.generate_control_function(sys, simplify = true) - @test f[1]([0.5], nothing, nothing, 0.0) == [1.0] + f, dvs, ps, io_sys = ModelingToolkit.generate_control_function(sys, simplify = true) + @test f[1]([0.5], nothing, MTKParameters(io_sys, []), 0.0) == [1.0] end @testset "With callable symbolic" begin From be89d29c73a411d2ccab996c33a67cd0e463e3e8 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 7 Feb 2025 14:14:13 +0530 Subject: [PATCH 0877/2176] test: account for additional initialization parameter defaults --- test/modelingtoolkitize.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/modelingtoolkitize.jl b/test/modelingtoolkitize.jl index 3bcfdecc8b..28ce7b5e01 100644 --- a/test/modelingtoolkitize.jl +++ b/test/modelingtoolkitize.jl @@ -460,7 +460,7 @@ sys = modelingtoolkitize(prob) p = [10.0, 28.0, 2.66] sprob = SDEProblem(sdef!, sdeg!, u0, tspan, p) sys = complete(modelingtoolkitize(sprob)) - @test length(ModelingToolkit.defaults(sys)) == 6 + @test length(ModelingToolkit.defaults(sys)) == 2length(u0) + length(p) sprob2 = SDEProblem(sys, [], tspan) truevals = similar(u0) From 8ca484f67d29fc7e1387bbdfe1168773bea7e5f1 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 7 Feb 2025 14:14:29 +0530 Subject: [PATCH 0878/2176] test: account for additional initialization parameters --- test/odesystem.jl | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/test/odesystem.jl b/test/odesystem.jl index 8281fafe93..a9677aad26 100644 --- a/test/odesystem.jl +++ b/test/odesystem.jl @@ -273,7 +273,10 @@ prob12 = ODEProblem(sys, u0, tspan, [k₁ => 0.04, k₂ => 3e7, k₃ => 1e4]) prob13 = ODEProblem(sys, u0, tspan, (k₁ => 0.04, k₂ => 3e7, k₃ => 1e4)) prob14 = ODEProblem(sys, u0, tspan, p2) for p in [prob1, prob14] - @test p.p == MTKParameters(sys, [k₁ => 0.04, k₂ => 3e7, k₃ => 1e4]) + @test p.p isa MTKParameters + p.ps[k₁] ≈ 0.04 + p.ps[k₂] ≈ 3e7 + p.ps[k₃] ≈ 1e-4 @test Set(Num.(unknowns(sys)) .=> p.u0) == Set([y₁ => 1, y₂ => 0, y₃ => 0]) end # test remake with symbols @@ -674,7 +677,8 @@ let prob = DAEProblem(sys, du0, u0, (0, 50)) @test prob.u0 ≈ u0 @test prob.du0 ≈ du0 - @test vcat(prob.p...) ≈ [1] + @test prob.p isa MTKParameters + @test prob.ps[k] ≈ 1 sol = solve(prob, IDA()) @test sol[y] ≈ 0.9 * sol[x[1]] + sol[x[2]] @test isapprox(sol[x[1]][end], 1, atol = 1e-3) @@ -683,7 +687,8 @@ let (0, 50)) @test prob.u0 ≈ [0.5, 0] @test prob.du0 ≈ [0, 0] - @test vcat(prob.p...) ≈ [1] + @test prob.p isa MTKParameters + @test prob.ps[k] ≈ 1 sol = solve(prob, IDA()) @test isapprox(sol[x[1]][end], 1, atol = 1e-3) @@ -691,7 +696,8 @@ let (0, 50), [k => 2]) @test prob.u0 ≈ [0.5, 0] @test prob.du0 ≈ [0, 0] - @test vcat(prob.p...) ≈ [2] + @test prob.p isa MTKParameters + @test prob.ps[k] ≈ 2 sol = solve(prob, IDA()) @test isapprox(sol[x[1]][end], 2, atol = 1e-3) @@ -715,7 +721,9 @@ let pmap = (k1 => 1.0, k2 => 1) tspan = (0.0, 1.0) prob = ODEProblem(sys, u0map, tspan, pmap; tofloat = false) - @test (prob.p...,) == ([1], [1.0]) || (prob.p...,) == ([1.0], [1]) + @test prob.p isa MTKParameters + @test prob.ps[k1] ≈ 1.0 + @test prob.ps[k2] == 1 && prob.ps[k2] isa Int prob = ODEProblem(sys, u0map, tspan, pmap) @test vcat(prob.p...) isa Vector{Float64} @@ -1288,9 +1296,13 @@ end t, [u..., x..., o...], [p...]) sys1, = structural_simplify(sys, ([x...], [])) fn1, = ModelingToolkit.generate_function(sys1; expression = Val{false}) - @test_nowarn fn1(ones(4), (2ones(2), 3ones(2, 2)), 4.0) + ps = MTKParameters(sys1, [x => 2ones(2), p => 3ones(2, 2)]) + @test_nowarn fn1(ones(4), ps, 4.0) sys2, = structural_simplify(sys, ([x...], []); split = false) fn2, = ModelingToolkit.generate_function(sys2; expression = Val{false}) + ps = zeros(8) + setp(sys2, x)(ps, 2ones(2)) + setp(sys2, p)(ps, 2ones(2, 2)) @test_nowarn fn2(ones(4), 2ones(6), 4.0) end From ec3d3a87ea6f281450ea2f30370a5c826c52592f Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 7 Feb 2025 14:17:27 +0530 Subject: [PATCH 0879/2176] refactor: don't create new parameters in `generate_initializesystem` --- src/systems/diffeqs/abstractodesystem.jl | 2 +- src/systems/nonlinear/initializesystem.jl | 58 ++++------------------- src/systems/problem_utils.jl | 4 +- 3 files changed, 12 insertions(+), 52 deletions(-) diff --git a/src/systems/diffeqs/abstractodesystem.jl b/src/systems/diffeqs/abstractodesystem.jl index 5d3fa93cde..d8996000cf 100644 --- a/src/systems/diffeqs/abstractodesystem.jl +++ b/src/systems/diffeqs/abstractodesystem.jl @@ -1256,7 +1256,7 @@ function InitializationProblem{iip, specialize}(sys::AbstractSystem, meta = get_metadata(isys) if meta isa InitializationSystemMetadata @set! isys.metadata.oop_reconstruct_u0_p = ReconstructInitializeprob( - sys, isys; remap = meta.new_params) + sys, isys) end ts = get_tearing_state(isys) diff --git a/src/systems/nonlinear/initializesystem.jl b/src/systems/nonlinear/initializesystem.jl index 1cb9dee3bf..6696f6b7c2 100644 --- a/src/systems/nonlinear/initializesystem.jl +++ b/src/systems/nonlinear/initializesystem.jl @@ -38,37 +38,7 @@ function generate_initializesystem(sys::AbstractSystem; delete!(defs, k) end filter_missing_values!(u0map) - # for initial conditions of the form `var => constant`, we instead turn them into - # `var ~ var0` where `var0` is a new parameter, and make `update_initializeprob!` - # update `initializeprob.ps[var0] = prob[var]`. - - # map parameters in `initprob` which need to be updated in `update_initializeprob!` - # to the corresponding expressions that determine their values - new_params = Dict() - pmap = copy(anydict(pmap)) - if is_time_dependent(sys) - for (k, v) in u0map - k = unwrap(k) - is_variable(sys, k) || continue - (symbolic_type(v) == NotSymbolic() && !is_array_of_symbolics(v)) || continue - newvar = get_initial_value_parameter(k) - new_params[newvar] = k - pmap[newvar] = v - u0map[k] = newvar - defs[newvar] = v - end - end - for (k, v) in pmap - k = unwrap(k) - is_parameter_solvable(k, pmap, defs, guesses) || continue - (v !== missing && symbolic_type(v) == NotSymbolic() && !is_array_of_symbolics(v)) || - continue - newvar = get_initial_value_parameter(k) - new_params[newvar] = k - pmap[newvar] = v - pmap[k] = newvar - defs[newvar] = v - end + pmap = anydict(pmap) # 1) Use algebraic equations of time-dependent systems as initialization constraints if has_iv(sys) @@ -222,8 +192,8 @@ function generate_initializesystem(sys::AbstractSystem; end # parameters do not include ones that became initialization unknowns - pars = Vector{SymbolicParam}(filter(p -> !haskey(paramsubs, p), parameters(sys))) - pars = [pars; map(unwrap, collect(keys(new_params)))] + pars = Vector{SymbolicParam}(filter( + p -> !haskey(paramsubs, p), parameters(sys; initial_parameters = true))) is_time_dependent(sys) && push!(pars, get_iv(sys)) if is_time_dependent(sys) @@ -258,7 +228,7 @@ function generate_initializesystem(sys::AbstractSystem; end meta = InitializationSystemMetadata( anydict(u0map), anydict(pmap), additional_guesses, - additional_initialization_eqs, extra_metadata, nothing, new_params) + additional_initialization_eqs, extra_metadata, nothing) return NonlinearSystem(eqs_ics, vars, pars; @@ -338,11 +308,14 @@ struct ReconstructInitializeprob end function ReconstructInitializeprob( - srcsys::AbstractSystem, dstsys::AbstractSystem; remap = Dict()) + srcsys::AbstractSystem, dstsys::AbstractSystem) syms = reduce( vcat, reorder_parameters(dstsys, parameters(dstsys; initial_parameters = true)); init = []) - getter = getu(srcsys, map(x -> get(remap, x, x), syms)) + srcsyms = map(syms) do sym + iscall(sym) && operation(sym) isa Initial ? arguments(sym)[1] : sym + end + getter = getu(srcsys, srcsyms) setter = setp_oop(dstsys, syms) return ReconstructInitializeprob(getter, setter) end @@ -375,7 +348,6 @@ struct InitializationSystemMetadata additional_initialization_eqs::Vector{Equation} extra_metadata::NamedTuple oop_reconstruct_u0_p::Union{Nothing, ReconstructInitializeprob} - new_params::Dict{Any, Any} end function get_possibly_array_fallback_singletons(varmap, p) @@ -467,16 +439,6 @@ function SciMLBase.remake_initialization_data( merge!(guesses, meta.additional_guesses) use_scc = get(meta.extra_metadata, :use_scc, true) initialization_eqs = meta.additional_initialization_eqs - - # remove occurrences of `keys(new_params)` from `u0map` and `pmap` - for (newvar, oldvar) in meta.new_params - if isequal(get(u0map, oldvar, nothing), newvar) - u0map[oldvar] = pmap[newvar] - elseif isequal(get(pmap, oldvar, nothing), newvar) - pmap[oldvar] = pmap[newvar] - end - delete!(pmap, newvar) - end end else # there is no initializeprob, so the original problem construction @@ -520,7 +482,7 @@ function SciMLBase.remake_initialization_data( filter_missing_values!(u0map) filter_missing_values!(pmap) - op, missing_unknowns, missing_pars = build_operating_point!( + op, missing_unknowns, missing_pars = build_operating_point!(sys, u0map, pmap, defs, cmap, dvs, ps) kws = maybe_build_initialization_problem( sys, op, u0map, pmap, t0, defs, guesses, missing_unknowns; diff --git a/src/systems/problem_utils.jl b/src/systems/problem_utils.jl index c25ae5ba48..487b32e0d4 100644 --- a/src/systems/problem_utils.jl +++ b/src/systems/problem_utils.jl @@ -579,7 +579,6 @@ function maybe_build_initialization_problem( initializeprob = ModelingToolkit.InitializationProblem( sys, t, u0map, pmap; guesses, kwargs...) meta = get_metadata(initializeprob.f.sys) - new_params = meta.new_params if is_time_dependent(sys) all_init_syms = Set(all_symbols(initializeprob)) @@ -601,13 +600,12 @@ function maybe_build_initialization_problem( end reqd_syms = parameter_symbols(initializeprob) - sources = [get(new_params, x, x) for x in reqd_syms] # we still want the `initialization_data` because it helps with `remake` if initializeprobmap === nothing && initializeprobpmap === nothing update_initializeprob! = nothing else update_initializeprob! = UpdateInitializeprob( - getu(sys, sources), setu(initializeprob, reqd_syms)) + getu(sys, reqd_syms), setu(initializeprob, reqd_syms)) end for p in punknowns From 4cef87863f575f8b543d3fb92a10b5eeca15a53f Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 7 Feb 2025 14:18:51 +0530 Subject: [PATCH 0880/2176] fix: better handling of extra symbolics and splitting in `build_operating_point` --- src/systems/problem_utils.jl | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/src/systems/problem_utils.jl b/src/systems/problem_utils.jl index 487b32e0d4..6b01bb0fdb 100644 --- a/src/systems/problem_utils.jl +++ b/src/systems/problem_utils.jl @@ -526,7 +526,7 @@ no values can be determined, and the list of parameters for which no values can Also updates `u0map` and `pmap` in-place to contain all the initial conditions in `op`, split by unknowns and parameters respectively. """ -function build_operating_point!( +function build_operating_point!(sys::AbstractSystem, u0map::AbstractDict, pmap::AbstractDict, defs::AbstractDict, cmap, dvs, ps) op = add_toterms(u0map) missing_unknowns = add_fallbacks!(op, dvs, defs) @@ -545,15 +545,33 @@ function build_operating_point!( filter!(kvp -> kvp[2] === nothing, u0map) filter!(kvp -> kvp[2] === nothing, pmap) + neithermap = anydict() + for (k, v) in op k = unwrap(k) - if isparameter(k) + if is_parameter(sys, k) pmap[k] = v - elseif !isconstant(k) + elseif is_variable(sys, k) || has_observed_with_lhs(sys, k) + if symbolic_type(v) == NotSymbolic() && !is_array_of_symbolics(v) && + v !== nothing + op[Initial(k)] = v + pmap[Initial(k)] = v + op[k] = Initial(k) + v = Initial(k) + end u0map[k] = v + else + neithermap[k] = v end end + for k in keys(u0map) + u0map[k] = fixpoint_sub(u0map[k], neithermap) + end + for k in keys(pmap) + pmap[k] = fixpoint_sub(pmap[k], neithermap) + end + return op, missing_unknowns, missing_pars end @@ -760,10 +778,8 @@ function process_SciMLProblem( is_time_dependent(sys) || add_observed!(sys, u0map) - op, missing_unknowns, missing_pars = build_operating_point!( + op, missing_unknowns, missing_pars = build_operating_point!(sys, u0map, pmap, defs, cmap, dvs, ps) - substitute_extra_variables!(sys, u0map) - substitute_extra_variables!(sys, pmap) if build_initializeprob kws = maybe_build_initialization_problem( From 91f4bae2f0e59cb670048ce84935c44ebec64b5c Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 7 Feb 2025 14:19:54 +0530 Subject: [PATCH 0881/2176] refactor: remove `substitute_extra_variables!` --- src/systems/problem_utils.jl | 28 ---------------------------- 1 file changed, 28 deletions(-) diff --git a/src/systems/problem_utils.jl b/src/systems/problem_utils.jl index 6b01bb0fdb..ffe9bd655b 100644 --- a/src/systems/problem_utils.jl +++ b/src/systems/problem_utils.jl @@ -655,34 +655,6 @@ function maybe_build_initialization_problem( initializeprobpmap)) end -""" - $(TYPEDSIGNATURES) - -Remove all entries in `varmap` whose keys are not variables/parameters in `sys`, -substituting their values into the rest of `varmap`. Modifies `varmap` in place. -""" -function substitute_extra_variables!(sys::AbstractSystem, varmap::AbstractDict) - tmpmap = anydict() - syms = all_symbols(sys) - for (k, v) in varmap - k = unwrap(k) - if any(isequal(k), syms) || - iscall(k) && - (operation(k) == getindex && any(isequal(arguments(k)[1]), syms) || - operation(k) isa Differential) || isconstant(k) - continue - end - tmpmap[k] = v - end - for k in keys(tmpmap) - delete!(varmap, k) - end - for (k, v) in varmap - varmap[k] = unwrap(fixpoint_sub(v, tmpmap)) - end - return varmap -end - """ $(TYPEDSIGNATURES) From 7f0342d89968cc708f9df3e8dd9ded93fd4757c1 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 7 Feb 2025 14:20:11 +0530 Subject: [PATCH 0882/2176] fix: handle `Initial` parameters in `process_SciMLProblem` --- src/systems/problem_utils.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/systems/problem_utils.jl b/src/systems/problem_utils.jl index ffe9bd655b..cb80512dc3 100644 --- a/src/systems/problem_utils.jl +++ b/src/systems/problem_utils.jl @@ -730,7 +730,7 @@ function process_SciMLProblem( force_initialization_time_independent = false, algebraic_only = false, allow_incomplete = false, kwargs...) dvs = unknowns(sys) - ps = parameters(sys) + ps = parameters(sys; initial_parameters = true) iv = has_iv(sys) ? get_iv(sys) : nothing eqs = equations(sys) @@ -742,7 +742,7 @@ function process_SciMLProblem( u0map = to_varmap(u0map, dvs) symbols_to_symbolics!(sys, u0map) _pmap = pmap - pmap = to_varmap(pmap, ps) + pmap = to_varmap(pmap, parameters(sys)) symbols_to_symbolics!(sys, pmap) defs = add_toterms(recursive_unwrap(defaults(sys))) cmap, cs = get_cmap(sys) From 8b9728a39715e89251030b375e01fe586685e7ee Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 7 Feb 2025 14:20:21 +0530 Subject: [PATCH 0883/2176] fix: handle `Initial` parameters in `get_u0_p` --- 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 cb80512dc3..11f6551bbe 100644 --- a/src/systems/problem_utils.jl +++ b/src/systems/problem_utils.jl @@ -852,7 +852,7 @@ function get_u0_p(sys, tofloat = true, symbolic_u0 = false) dvs = unknowns(sys) - ps = parameters(sys) + ps = parameters(sys; initial_parameters = true) defs = defaults(sys) if t0 !== nothing From 6273543f73f7ac03c857c3ae58d885fbd1c23c2b Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 7 Feb 2025 19:14:08 +0530 Subject: [PATCH 0884/2176] fix: fix infinite recursion in `full_equations` --- src/structural_transformation/symbolics_tearing.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/structural_transformation/symbolics_tearing.jl b/src/structural_transformation/symbolics_tearing.jl index d95951c41e..92e121c028 100644 --- a/src/structural_transformation/symbolics_tearing.jl +++ b/src/structural_transformation/symbolics_tearing.jl @@ -84,7 +84,7 @@ function eq_derivative!(ts::TearingState{ODESystem}, ieq::Int; kwargs...) end function tearing_sub(expr, dict, s) - expr = Symbolics.fixpoint_sub(expr, dict) + expr = Symbolics.fixpoint_sub(expr, dict; operator = ModelingToolkit.Initial) s ? simplify(expr) : expr end From 7c457e51adec53c8ab99cf6b4dc51d1551d1b845 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 7 Feb 2025 19:14:27 +0530 Subject: [PATCH 0885/2176] fix: fix modelingtoolkitize --- src/systems/diffeqs/modelingtoolkitize.jl | 14 ++++++++------ src/systems/parameter_buffer.jl | 9 +++++++++ 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/src/systems/diffeqs/modelingtoolkitize.jl b/src/systems/diffeqs/modelingtoolkitize.jl index 7ab68e3bee..e7321c4d2b 100644 --- a/src/systems/diffeqs/modelingtoolkitize.jl +++ b/src/systems/diffeqs/modelingtoolkitize.jl @@ -93,6 +93,8 @@ function modelingtoolkitize( else Dict() end + filter!(x -> !iscall(x) || !(operation(x) isa Initial), params) + filter!(x -> !iscall(x[1]) || !(operation(x[1]) isa Initial), default_p) de = ODESystem(eqs, t, sts, params, defaults = merge(default_u0, default_p); name = gensym(:MTKizedODE), @@ -207,17 +209,17 @@ function define_params(p::MTKParameters, names = nothing) for _ in buf push!( ps, - if names === nothing - toparam(variable(:α, i)) - else - toparam(variable(names[i])) - end + toparam(variable(:α, i)) ) end end return identity.(ps) else - return collect(values(names)) + new_p = as_any_buffer(p) + for (k, v) in names + new_p[k] = v + end + return reduce(vcat, new_p; init = []) end end diff --git a/src/systems/parameter_buffer.jl b/src/systems/parameter_buffer.jl index 07b47a5ec4..ab33cc5b63 100644 --- a/src/systems/parameter_buffer.jl +++ b/src/systems/parameter_buffer.jl @@ -536,6 +536,15 @@ function _remake_buffer(indp, oldbuf::MTKParameters, idxs, vals; validate = true return newbuf end +function as_any_buffer(p::MTKParameters) + @set! p.tunable = similar(p.tunable, Any) + @set! p.discrete = Tuple(similar(buf, Any) for buf in p.discrete) + @set! p.constant = Tuple(similar(buf, Any) for buf in p.constant) + @set! p.nonnumeric = Tuple(similar(buf, Any) for buf in p.nonnumeric) + @set! p.caches = Tuple(similar(buf, Any) for buf in p.caches) + return p +end + struct NestedGetIndex{T} x::T end From 9d88d5f3a4b21ad639c860a67312507ca24ac97f Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 7 Feb 2025 19:15:32 +0530 Subject: [PATCH 0886/2176] fix: fix handling of array unknowns in `add_initialization_parameters` --- src/systems/abstractsystem.jl | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index 0deef1bc4c..b022d8f24f 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -714,7 +714,16 @@ function add_initialization_parameters(sys::AbstractSystem) for x in existing_initials delete!(defs, x) end - merge!(defs, Dict(initials .=> zero_var.(initials))) + for ivar in initials + if symbolic_type(ivar) == ScalarSymbolic() + defs[ivar] = zero_var(ivar) + else + defs[ivar] = collect(ivar) + for scal_ivar in defs[ivar] + defs[scal_ivar] = zero_var(scal_ivar) + end + end + end @set! sys.defaults = defs return sys end From 14b28d485d8e4fe910e520160e2da046671da2a5 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 7 Feb 2025 19:15:45 +0530 Subject: [PATCH 0887/2176] fix: respect `validate` kward in `_remake_buffer` --- src/systems/parameter_buffer.jl | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/systems/parameter_buffer.jl b/src/systems/parameter_buffer.jl index ab33cc5b63..f2d0867a32 100644 --- a/src/systems/parameter_buffer.jl +++ b/src/systems/parameter_buffer.jl @@ -459,10 +459,12 @@ function _remake_buffer(indp, oldbuf::MTKParameters, idxs, vals; validate = true @set! newbuf.nonnumeric = Tuple(similar(buf, Any) for buf in newbuf.nonnumeric) function handle_parameter(ic, sym, idx, val) - if sym === nothing - validate_parameter_type(ic, idx, val) - else - validate_parameter_type(ic, sym, idx, val) + if validate + if sym === nothing + validate_parameter_type(ic, idx, val) + else + validate_parameter_type(ic, sym, idx, val) + end end # `ParameterIndex(idx)` turns off size validation since it relies on there # being an existing value From 7b5f29d1067b91aeb128657d064216726cc52689 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 7 Feb 2025 19:18:08 +0530 Subject: [PATCH 0888/2176] fix: handle constant initial conditions of derivatives of unknowns --- src/systems/problem_utils.jl | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/systems/problem_utils.jl b/src/systems/problem_utils.jl index 11f6551bbe..dcb9416da3 100644 --- a/src/systems/problem_utils.jl +++ b/src/systems/problem_utils.jl @@ -551,7 +551,9 @@ function build_operating_point!(sys::AbstractSystem, k = unwrap(k) if is_parameter(sys, k) pmap[k] = v - elseif is_variable(sys, k) || has_observed_with_lhs(sys, k) + elseif is_variable(sys, k) || has_observed_with_lhs(sys, k) || + iscall(k) && + operation(k) isa Differential && is_variable(sys, arguments(k)[1]) if symbolic_type(v) == NotSymbolic() && !is_array_of_symbolics(v) && v !== nothing op[Initial(k)] = v @@ -594,7 +596,7 @@ function maybe_build_initialization_problem( t = 0.0 end - initializeprob = ModelingToolkit.InitializationProblem( + initializeprob = ModelingToolkit.InitializationProblem{true, SciMLBase.FullSpecialize}( sys, t, u0map, pmap; guesses, kwargs...) meta = get_metadata(initializeprob.f.sys) From 8142b76a15afcc9fc9bc2d6cc26e8a720c8e13fe Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 10 Feb 2025 18:45:39 +0530 Subject: [PATCH 0889/2176] feat: implement `is_time_dependent` for `PDESystem` --- src/systems/pde/pdesystem.jl | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/systems/pde/pdesystem.jl b/src/systems/pde/pdesystem.jl index eac540e401..96e6a6b276 100644 --- a/src/systems/pde/pdesystem.jl +++ b/src/systems/pde/pdesystem.jl @@ -166,3 +166,5 @@ function Base.show(io::IO, ::MIME"text/plain", sys::PDESystem) print(io, "Default Parameter Values", get_defaults(sys)) return nothing end + +SymbolicIndexingInterface.is_time_dependent(::AbstractMultivariateSystem) = true From 2d9194debaac2d12a20c5753a07efdb96730ec08 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 10 Feb 2025 18:46:00 +0530 Subject: [PATCH 0890/2176] fix: retain parameter dependencies in `build_operating_point!` --- 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 dcb9416da3..38dec78335 100644 --- a/src/systems/problem_utils.jl +++ b/src/systems/problem_utils.jl @@ -549,7 +549,7 @@ function build_operating_point!(sys::AbstractSystem, for (k, v) in op k = unwrap(k) - if is_parameter(sys, k) + if is_parameter(sys, k) || has_parameter_dependency_with_lhs(sys, k) pmap[k] = v elseif is_variable(sys, k) || has_observed_with_lhs(sys, k) || iscall(k) && From cecb15c210b2c5de34ef8f68e6737c76d944cf70 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 10 Feb 2025 18:46:05 +0530 Subject: [PATCH 0891/2176] feat: export `Initial` --- src/ModelingToolkit.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ModelingToolkit.jl b/src/ModelingToolkit.jl index 303c88da73..c63011fbea 100644 --- a/src/ModelingToolkit.jl +++ b/src/ModelingToolkit.jl @@ -278,7 +278,7 @@ export toexpr, get_variables export simplify, substitute export build_function export modelingtoolkitize -export generate_initializesystem +export generate_initializesystem, Initial export alg_equations, diff_equations, has_alg_equations, has_diff_equations export get_alg_eqs, get_diff_eqs, has_alg_eqs, has_diff_eqs From 33e844b312f723defa002f7793500ba79d29a20a Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 10 Feb 2025 18:46:26 +0530 Subject: [PATCH 0892/2176] refactor: make `symbol_to_symbolic` a global utility function --- src/systems/diffeqs/odesystem.jl | 14 ++------------ src/utils.jl | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+), 12 deletions(-) diff --git a/src/systems/diffeqs/odesystem.jl b/src/systems/diffeqs/odesystem.jl index 8febfc253b..e53b9223e6 100644 --- a/src/systems/diffeqs/odesystem.jl +++ b/src/systems/diffeqs/odesystem.jl @@ -445,20 +445,10 @@ function build_explicit_observed_function(sys, ts; end allsyms = all_symbols(sys) - function symbol_to_symbolic(sym) - sym isa Symbol || return sym - idx = findfirst(x -> (hasname(x) ? getname(x) : Symbol(x)) == sym, allsyms) - idx === nothing && return sym - sym = allsyms[idx] - if iscall(sym) && operation(sym) == getindex - sym = arguments(sym)[1] - end - return sym - end if symbolic_type(ts) == NotSymbolic() && ts isa AbstractArray - ts = map(symbol_to_symbolic, ts) + ts = map(x -> symbol_to_symbolic(sys, x; allsyms), ts) else - ts = symbol_to_symbolic(ts) + ts = symbol_to_symbolic(sys, ts; allsyms) end vs = ModelingToolkit.vars(ts; op) diff --git a/src/utils.jl b/src/utils.jl index 3f52b0676f..fc6e49d894 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -1249,3 +1249,21 @@ function process_equations(eqs, iv) diffvars, allunknowns, ps, Equation[diffeq; algeeq; compressed_eqs] end + +""" + $(TYPEDSIGNATURES) + +If `sym isa Symbol`, try and convert it to a symbolic by matching against symbolic +variables in `allsyms`. If `sym` is not a `Symbol` or no match was found, return +`sym` as-is. +""" +function symbol_to_symbolic(sys::AbstractSystem, sym; allsyms = all_symbols(sys)) + sym isa Symbol || return sym + idx = findfirst(x -> (hasname(x) ? getname(x) : Symbol(x)) == sym, allsyms) + idx === nothing && return sym + sym = allsyms[idx] + if iscall(sym) && operation(sym) == getindex + sym = arguments(sym)[1] + end + return sym +end From b42075a3b97473980b894fa4851d0ca23fbf9d7f Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 10 Feb 2025 18:47:03 +0530 Subject: [PATCH 0893/2176] feat: implement `SciMLBase.late_binding_update_u0_p` to update `Initial` parameters --- src/systems/nonlinear/initializesystem.jl | 27 +++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/src/systems/nonlinear/initializesystem.jl b/src/systems/nonlinear/initializesystem.jl index 6696f6b7c2..d32a1befcc 100644 --- a/src/systems/nonlinear/initializesystem.jl +++ b/src/systems/nonlinear/initializesystem.jl @@ -490,6 +490,33 @@ function SciMLBase.remake_initialization_data( return get(kws, :initialization_data, nothing) end +function SciMLBase.late_binding_update_u0_p( + prob, sys::AbstractSystem, u0, p, t0, newu0, newp) + u0 === missing && return newu0, newp + eltype(u0) <: Pair || return newu0, newp + + newu0 = DiffEqBase.promote_u0(newu0, newp, t0) + tunables, repack, alias = SciMLStructures.canonicalize(SciMLStructures.Tunable(), newp) + tunables = DiffEqBase.promote_u0(tunables, newu0, t0) + newp = repack(tunables) + + allsyms = all_symbols(sys) + for (k, v) in u0 + v === nothing && continue + (symbolic_type(v) == NotSymbolic() && !is_array_of_symbolics(v)) || continue + if k isa Symbol + k2 = symbol_to_symbolic(sys, k; allsyms) + # if it is returned as-is, there is no match so skip it + k2 === k && continue + k = k2 + end + is_parameter(sys, Initial(k)) || continue + setp(sys, Initial(k))(newp, v) + end + + return newu0, newp +end + """ Counteracts the CSE/array variable hacks in `symbolics_tearing.jl` so it works with initialization. From 9d7e4131920dd147e56f4fd7173674ae696a744e Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 10 Feb 2025 18:47:29 +0530 Subject: [PATCH 0894/2176] fix: provide history function to `reconstruct_fn` in `remake_initialization_data` --- src/systems/nonlinear/initializesystem.jl | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/systems/nonlinear/initializesystem.jl b/src/systems/nonlinear/initializesystem.jl index d32a1befcc..f03785cf3c 100644 --- a/src/systems/nonlinear/initializesystem.jl +++ b/src/systems/nonlinear/initializesystem.jl @@ -403,8 +403,13 @@ function SciMLBase.remake_initialization_data( else reconstruct_fn = ReconstructInitializeprob(sys, oldinitsys) end + # the history function doesn't matter because `reconstruct_fn` is only going to + # update the values of parameters, which aren't time dependent. The reason it + # is called is because `Initial` parameters are calculated from the corresponding + # state values. + history_fn = is_time_dependent(sys) && !is_markovian(sys) ? Returns(newu0) : nothing new_initu0, new_initp = reconstruct_fn( - ProblemState(; u = newu0, p = newp, t = t0), oldinitprob) + ProblemState(; u = newu0, p = newp, t = t0, h = history_fn), oldinitprob) if oldinitprob.f.resid_prototype === nothing newf = oldinitprob.f else From fe329d0d9e36f10f8f4d56c811c28011a72c74a9 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 10 Feb 2025 18:47:52 +0530 Subject: [PATCH 0895/2176] test: fix unsatisfiable initialization --- test/initializationsystem.jl | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/initializationsystem.jl b/test/initializationsystem.jl index fe5f03b9c7..3a208bb940 100644 --- a/test/initializationsystem.jl +++ b/test/initializationsystem.jl @@ -1207,7 +1207,8 @@ end prob2 = remake(prob; u0 = [x => 2.0]) @test prob2[x] == 2.0 test_dummy_initialization_equation(prob2, x) - prob3 = remake(prob; u0 = [y => 2.0]) + # otherwise we have `x ~ 2, y ~ 2` which is unsatisfiable + prob3 = remake(prob; u0 = [x => nothing, y => 2.0]) @test prob3.f.initialization_data !== nothing @test init(prob3)[x] ≈ 1.0 prob4 = remake(prob; p = [p => 1.0]) From f5cdf2146414873f5a47fd1f0e9a13723d862901 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 10 Feb 2025 18:48:19 +0530 Subject: [PATCH 0896/2176] test: account for additional initialization parameters --- test/discrete_system.jl | 4 +++- test/mtkparameters.jl | 2 +- test/odesystem.jl | 10 ++++++++-- test/symbolic_indexing_interface.jl | 3 ++- 4 files changed, 14 insertions(+), 5 deletions(-) diff --git a/test/discrete_system.jl b/test/discrete_system.jl index b63f66d4e4..78afafd51d 100644 --- a/test/discrete_system.jl +++ b/test/discrete_system.jl @@ -83,7 +83,9 @@ prob_map2 = DiscreteProblem(sys) sol_map2 = solve(prob_map2, FunctionMap()); @test sol_map.u ≈ sol_map2.u -@test sol_map.prob.p == sol_map2.prob.p +for p in parameters(sys) + @test sol_map.prob.ps[p] ≈ sol_map2.prob.ps[p] +end @test_throws Any sol_map2[R2] @test sol_map2[R2(k + 1)][begin:(end - 1)] == sol_map2[R][(begin + 1):end] # Direct Implementation diff --git a/test/mtkparameters.jl b/test/mtkparameters.jl index ce524fdb76..a4e7ef9052 100644 --- a/test/mtkparameters.jl +++ b/test/mtkparameters.jl @@ -109,7 +109,7 @@ eq = D(X) ~ p[1] - p[2] * X u0 = [X => 1.0] ps = [p => [2.0, 0.1]] p = MTKParameters(osys, ps, u0) -@test p.tunable == [2.0, 0.1] +@test p.tunable == [2.0, 0.1, 1.0] # Ensure partial update promotes the buffer @parameters p q r diff --git a/test/odesystem.jl b/test/odesystem.jl index a9677aad26..65ceaabc16 100644 --- a/test/odesystem.jl +++ b/test/odesystem.jl @@ -287,7 +287,10 @@ u01 = [y₁ => 1, y₂ => 1, y₃ => 1] prob_pmap = remake(prob14; p = p3, u0 = u01) prob_dpmap = remake(prob14; p = Dict(p3), u0 = Dict(u01)) for p in [prob_pmap, prob_dpmap] - @test p.p == MTKParameters(sys, [k₁ => 0.05, k₂ => 2e7, k₃ => 1.1e4]) + @test p.p isa MTKParameters + p.ps[k₁] ≈ 0.05 + p.ps[k₂] ≈ 2e7 + p.ps[k₃] ≈ 1.1e-4 @test Set(Num.(unknowns(sys)) .=> p.u0) == Set([y₁ => 1, y₂ => 1, y₃ => 1]) end sol_pmap = solve(prob_pmap, Rodas5()) @@ -315,7 +318,10 @@ sol_dpmap = solve(prob_dpmap, Rodas5()) prob = ODEProblem(sys, Pair[]) prob_new = SciMLBase.remake(prob, p = Dict(sys1.a => 3.0, b => 4.0), u0 = Dict(sys1.x => 1.0)) - @test prob_new.p == MTKParameters(sys, [b => 4.0, sys1.a => 3.0, sys.sys2.a => 1.0]) + @test prob_new.p isa MTKParameters + @test prob_new.ps[b] ≈ 4.0 + @test prob_new.ps[sys1.a] ≈ 3.0 + @test prob_new.ps[sys.sys2.a] ≈ 1.0 @test prob_new.u0 == [1.0, 0.0] end diff --git a/test/symbolic_indexing_interface.jl b/test/symbolic_indexing_interface.jl index 4a7cd926b4..5979cd71ac 100644 --- a/test/symbolic_indexing_interface.jl +++ b/test/symbolic_indexing_interface.jl @@ -23,7 +23,8 @@ using SciMLStructures: Tunable @test parameter_index.( (odesys,), [x, y, t, ParameterIndex(Tunable(), 1), :x, :y]) == [nothing, nothing, nothing, ParameterIndex(Tunable(), 1), nothing, nothing] - @test isequal(parameter_symbols(odesys), [a, b]) + @test isequal( + Set(parameter_symbols(odesys)), Set([a, b, Initial(x), Initial(y), Initial(xy)])) @test all(is_independent_variable.((odesys,), [t, :t])) @test all(.!is_independent_variable.((odesys,), [x, y, a, :x, :y, :a])) @test isequal(independent_variable_symbols(odesys), [t]) From 525324a7d48d6906ae7f297d96877fca8952ceea Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 10 Feb 2025 23:35:56 +0530 Subject: [PATCH 0897/2176] fix: don't copy variables to `Initial` parameters in `ReconstructInitializeprob` --- src/systems/nonlinear/initializesystem.jl | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/systems/nonlinear/initializesystem.jl b/src/systems/nonlinear/initializesystem.jl index f03785cf3c..ed338720cf 100644 --- a/src/systems/nonlinear/initializesystem.jl +++ b/src/systems/nonlinear/initializesystem.jl @@ -310,12 +310,9 @@ end function ReconstructInitializeprob( srcsys::AbstractSystem, dstsys::AbstractSystem) syms = reduce( - vcat, reorder_parameters(dstsys, parameters(dstsys; initial_parameters = true)); + vcat, reorder_parameters(dstsys, parameters(dstsys)); init = []) - srcsyms = map(syms) do sym - iscall(sym) && operation(sym) isa Initial ? arguments(sym)[1] : sym - end - getter = getu(srcsys, srcsyms) + getter = getu(srcsys, syms) setter = setp_oop(dstsys, syms) return ReconstructInitializeprob(getter, setter) end @@ -338,6 +335,10 @@ function (rip::ReconstructInitializeprob)(srcvalp, dstvalp) else u0 = T.(state_values(dstvalp)) end + buf, repack, alias = SciMLStructures.canonicalize(SciMLStructures.Tunable(), newp) + if eltype(buf) != T + newp = repack(T.(buf)) + end return u0, newp end From b0749b647be9cb779b1480e255274abf3bb88df9 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 11 Feb 2025 14:53:23 +0530 Subject: [PATCH 0898/2176] feat: add `Initial` parameters for real-valued parameter dependencies --- src/systems/abstractsystem.jl | 57 +++++++++++++++-------- src/systems/nonlinear/initializesystem.jl | 12 ++--- src/systems/problem_utils.jl | 8 +++- 3 files changed, 51 insertions(+), 26 deletions(-) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index b022d8f24f..9278afc366 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -689,26 +689,33 @@ function SymbolicUtils.maketerm(::Type{<:BasicSymbolic}, ::Initial, args, meta) end function add_initialization_parameters(sys::AbstractSystem) - is_time_dependent(sys) || return sys @assert !has_systems(sys) || isempty(get_systems(sys)) - eqs = equations(sys) - if !(eqs isa Vector{Equation}) - eqs = Equation[x for x in eqs if x isa Equation] - end - obs, eqs = unhack_observed(observed(sys), eqs) - all_uvars = Set{BasicSymbolic}() - for x in Iterators.flatten((unknowns(sys), Iterators.map(eq -> eq.lhs, obs))) - x = unwrap(x) - if iscall(x) && operation(x) == getindex - push!(all_uvars, arguments(x)[1]) - else - push!(all_uvars, x) + all_initialvars = Set{BasicSymbolic}() + # time-independent systems don't initialize unknowns + if is_time_dependent(sys) + eqs = equations(sys) + if !(eqs isa Vector{Equation}) + eqs = Equation[x for x in eqs if x isa Equation] + end + obs, eqs = unhack_observed(observed(sys), eqs) + for x in Iterators.flatten((unknowns(sys), Iterators.map(eq -> eq.lhs, obs))) + x = unwrap(x) + if iscall(x) && operation(x) == getindex + push!(all_initialvars, arguments(x)[1]) + else + push!(all_initialvars, x) + end end end - all_uvars = collect(all_uvars) - initials = map(Initial(), all_uvars) - existing_initials = filter( - x -> iscall(x) && (operation(x) isa Initial), parameters(sys)) + for eq in parameter_dependencies(sys) + is_variable_floatingpoint(eq.lhs) || continue + push!(all_initialvars, eq.lhs) + end + all_initialvars = collect(all_initialvars) + initials = map(Initial(), all_initialvars) + # existing_initials = filter( + # x -> iscall(x) && (operation(x) isa Initial), parameters(sys)) + existing_initials = [] @set! sys.ps = unique!([setdiff(get_ps(sys), existing_initials); initials]) defs = copy(get_defaults(sys)) for x in existing_initials @@ -1296,7 +1303,7 @@ Get the parameters of the system `sys` and its subsystems. See also [`@parameters`](@ref) and [`ModelingToolkit.get_ps`](@ref). """ -function parameters(sys::AbstractSystem; initial_parameters = !is_time_dependent(sys)) +function parameters(sys::AbstractSystem; initial_parameters = false) ps = get_ps(sys) if ps == SciMLBase.NullParameters() return [] @@ -1308,7 +1315,19 @@ function parameters(sys::AbstractSystem; initial_parameters = !is_time_dependent result = unique(isempty(systems) ? ps : [ps; reduce(vcat, namespace_parameters.(systems))]) if !initial_parameters - filter!(x -> !iscall(x) || !isa(operation(x), Initial), result) + if is_time_dependent(sys) + # time-dependent systems have `Initial` parameters for all their + # unknowns/pdeps, all of which should be hidden. + filter!(x -> !iscall(x) || !isa(operation(x), Initial), result) + else + # time-independent systems only have `Initial` parameters for + # pdeps. Any other `Initial` parameters should be kept (e.g. initialization + # systems) + filter!( + x -> !iscall(x) || !isa(operation(x), Initial) || + !has_parameter_dependency_with_lhs(sys, only(arguments(x))), + result) + end end return result end diff --git a/src/systems/nonlinear/initializesystem.jl b/src/systems/nonlinear/initializesystem.jl index ed338720cf..da66ce66d7 100644 --- a/src/systems/nonlinear/initializesystem.jl +++ b/src/systems/nonlinear/initializesystem.jl @@ -207,6 +207,12 @@ function generate_initializesystem(sys::AbstractSystem; append!(eqs_ics, trueobs) end + if is_time_dependent(sys) + vars = [vars; collect(values(paramsubs))] + else + vars = collect(values(paramsubs)) + end + # even if `p => tovar(p)` is in `paramsubs`, `isparameter(p[1]) === true` after substitution # so add scalarized versions as well for k in collect(keys(paramsubs)) @@ -217,12 +223,6 @@ function generate_initializesystem(sys::AbstractSystem; end eqs_ics = Symbolics.substitute.(eqs_ics, (paramsubs,)) - if is_time_dependent(sys) - vars = [vars; collect(values(paramsubs))] - else - vars = collect(values(paramsubs)) - end - for k in keys(defs) defs[k] = substitute(defs[k], paramsubs) end diff --git a/src/systems/problem_utils.jl b/src/systems/problem_utils.jl index 38dec78335..87aafb8f73 100644 --- a/src/systems/problem_utils.jl +++ b/src/systems/problem_utils.jl @@ -549,8 +549,14 @@ function build_operating_point!(sys::AbstractSystem, for (k, v) in op k = unwrap(k) - if is_parameter(sys, k) || has_parameter_dependency_with_lhs(sys, k) + if is_parameter(sys, k) pmap[k] = v + elseif has_parameter_dependency_with_lhs(sys, k) && is_variable_floatingpoint(k) && + v !== nothing && !isequal(v, Initial(k)) + op[Initial(k)] = v + pmap[Initial(k)] = v + op[k] = Initial(k) + pmap[k] = Initial(k) elseif is_variable(sys, k) || has_observed_with_lhs(sys, k) || iscall(k) && operation(k) isa Differential && is_variable(sys, arguments(k)[1]) From 35ef0ab8b9f1585ae40284cae141be9045d2bdac Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 11 Feb 2025 14:53:37 +0530 Subject: [PATCH 0899/2176] test: update tests to consider `Initial` parameters of pdeps --- test/mtkparameters.jl | 2 +- test/parameter_dependencies.jl | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/test/mtkparameters.jl b/test/mtkparameters.jl index a4e7ef9052..3ac8f7569d 100644 --- a/test/mtkparameters.jl +++ b/test/mtkparameters.jl @@ -40,7 +40,7 @@ setp(sys, a)(ps, 1.0) @test getp(sys, a)(ps) == getp(sys, b)(ps) / 2 == getp(sys, c)(ps) / 3 == 1.0 -for (portion, values) in [(Tunable(), [1.0, 5.0, 6.0, 7.0]) +for (portion, values) in [(Tunable(), [1.0, 2.0, 5.0, 6.0, 7.0]) (Discrete(), [3.0]) (Constants(), vcat([0.1, 0.2, 0.3], ones(9), [4.0]))] buffer, repack, alias = canonicalize(portion, ps) diff --git a/test/parameter_dependencies.jl b/test/parameter_dependencies.jl index 7f71120564..f88f3f03b7 100644 --- a/test/parameter_dependencies.jl +++ b/test/parameter_dependencies.jl @@ -326,7 +326,7 @@ end eqs = [0 ~ p1 * x * exp(x) + p2] @mtkbuild sys = NonlinearSystem(eqs; parameter_dependencies = [p2 => 2p1]) @test isequal(only(parameters(sys)), p1) - @test Set(full_parameters(sys)) == Set([p1, p2]) + @test Set(full_parameters(sys)) == Set([p1, p2, Initial(p2)]) prob = NonlinearProblem(sys, [x => 1.0]) @test prob.ps[p1] == 1.0 @test prob.ps[p2] == 2.0 From bfae1d224cf960af46f4ffc64c5b4e60e71aff71 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 11 Feb 2025 16:18:40 +0530 Subject: [PATCH 0900/2176] refactor: move linearization-related code to new `linearization.jl` file --- src/ModelingToolkit.jl | 1 + src/linearization.jl | 537 +++++++++++++++++++++++++++++++++ src/systems/abstractsystem.jl | 538 ---------------------------------- 3 files changed, 538 insertions(+), 538 deletions(-) create mode 100644 src/linearization.jl diff --git a/src/ModelingToolkit.jl b/src/ModelingToolkit.jl index c63011fbea..7d08a9aac2 100644 --- a/src/ModelingToolkit.jl +++ b/src/ModelingToolkit.jl @@ -152,6 +152,7 @@ include("systems/imperative_affect.jl") include("systems/callbacks.jl") include("systems/codegen_utils.jl") include("systems/problem_utils.jl") +include("linearization.jl") include("systems/nonlinear/nonlinearsystem.jl") include("systems/nonlinear/homotopy_continuation.jl") diff --git a/src/linearization.jl b/src/linearization.jl new file mode 100644 index 0000000000..bbf0d8e2c1 --- /dev/null +++ b/src/linearization.jl @@ -0,0 +1,537 @@ +""" + lin_fun, simplified_sys = linearization_function(sys::AbstractSystem, inputs, outputs; simplify = false, initialize = true, initialization_solver_alg = TrustRegion(), kwargs...) + +Return a function that linearizes the system `sys`. The function [`linearize`](@ref) provides a higher-level and easier to use interface. + +`lin_fun` is a function `(variables, p, t) -> (; f_x, f_z, g_x, g_z, f_u, g_u, h_x, h_z, h_u)`, i.e., it returns a NamedTuple with the Jacobians of `f,g,h` for the nonlinear `sys` (technically for `simplified_sys`) on the form + +```math +\\begin{aligned} +ẋ &= f(x, z, u) \\\\ +0 &= g(x, z, u) \\\\ +y &= h(x, z, u) +\\end{aligned} +``` + +where `x` are differential unknown variables, `z` algebraic variables, `u` inputs and `y` outputs. To obtain a linear statespace representation, see [`linearize`](@ref). The input argument `variables` is a vector defining the operating point, corresponding to `unknowns(simplified_sys)` and `p` is a vector corresponding to the parameters of `simplified_sys`. Note: all variables in `inputs` have been converted to parameters in `simplified_sys`. + +The `simplified_sys` has undergone [`structural_simplify`](@ref) and had any occurring input or output variables replaced with the variables provided in arguments `inputs` and `outputs`. The unknowns of this system also indicate the order of the unknowns that holds for the linearized matrices. + +# Arguments: + + - `sys`: An [`ODESystem`](@ref). This function will automatically apply simplification passes on `sys` and return the resulting `simplified_sys`. + - `inputs`: A vector of variables that indicate the inputs of the linearized input-output model. + - `outputs`: A vector of variables that indicate the outputs of the linearized input-output model. + - `simplify`: Apply simplification in tearing. + - `initialize`: If true, a check is performed to ensure that the operating point is consistent (satisfies algebraic equations). If the op is not consistent, initialization is performed. + - `initialization_solver_alg`: A NonlinearSolve algorithm to use for solving for a feasible set of state and algebraic variables that satisfies the specified operating point. + - `kwargs`: Are passed on to `find_solvables!` + +See also [`linearize`](@ref) which provides a higher-level interface. +""" +function linearization_function(sys::AbstractSystem, inputs, + outputs; simplify = false, + initialize = true, + initializealg = nothing, + initialization_abstol = 1e-5, + initialization_reltol = 1e-3, + op = Dict(), + p = DiffEqBase.NullParameters(), + zero_dummy_der = false, + initialization_solver_alg = TrustRegion(), + eval_expression = false, eval_module = @__MODULE__, + warn_initialize_determined = true, + kwargs...) + op = Dict(op) + inputs isa AbstractVector || (inputs = [inputs]) + outputs isa AbstractVector || (outputs = [outputs]) + inputs = mapreduce(vcat, inputs; init = []) do var + symbolic_type(var) == ArraySymbolic() ? collect(var) : [var] + end + outputs = mapreduce(vcat, outputs; init = []) do var + symbolic_type(var) == ArraySymbolic() ? collect(var) : [var] + end + ssys, diff_idxs, alge_idxs, input_idxs = io_preprocessing(sys, inputs, outputs; + simplify, + kwargs...) + if zero_dummy_der + dummyder = setdiff(unknowns(ssys), unknowns(sys)) + defs = Dict(x => 0.0 for x in dummyder) + @set! ssys.defaults = merge(defs, defaults(ssys)) + op = merge(defs, op) + end + sys = ssys + + if initializealg === nothing + initializealg = initialize ? OverrideInit() : NoInit() + end + + fun, u0, p = process_SciMLProblem( + ODEFunction{true, SciMLBase.FullSpecialize}, sys, merge( + defaults_and_guesses(sys), op), p; + t = 0.0, build_initializeprob = initializealg isa OverrideInit, + allow_incomplete = true, algebraic_only = true) + prob = ODEProblem(fun, u0, (nothing, nothing), p) + + ps = parameters(sys) + h = build_explicit_observed_function(sys, outputs; eval_expression, eval_module) + lin_fun = let diff_idxs = diff_idxs, + alge_idxs = alge_idxs, + input_idxs = input_idxs, + sts = unknowns(sys), + fun = fun, + prob = prob, + sys_ps = p, + h = h, + integ_cache = (similar(u0)), + chunk = ForwardDiff.Chunk(input_idxs), + initializealg = initializealg, + initialization_abstol = initialization_abstol, + initialization_reltol = initialization_reltol, + initialization_solver_alg = initialization_solver_alg, + sys = sys + + function (u, p, t) + if !isa(p, MTKParameters) + p = todict(p) + newps = deepcopy(sys_ps) + for (k, v) in p + if is_parameter(sys, k) + v = fixpoint_sub(v, p) + setp(sys, k)(newps, v) + end + end + p = newps + end + + if u !== nothing # Handle systems without unknowns + length(sts) == length(u) || + error("Number of unknown variables ($(length(sts))) does not match the number of input unknowns ($(length(u)))") + + integ = MockIntegrator{true}(u, p, t, integ_cache) + u, p, success = SciMLBase.get_initial_values( + prob, integ, fun, initializealg, Val(true); + abstol = initialization_abstol, reltol = initialization_reltol, + nlsolve_alg = initialization_solver_alg) + if !success + error("Initialization algorithm $(initializealg) failed with `u = $u` and `p = $p`.") + end + uf = SciMLBase.UJacobianWrapper(fun, t, p) + fg_xz = ForwardDiff.jacobian(uf, u) + h_xz = ForwardDiff.jacobian( + let p = p, t = t + xz -> h(xz, p, t) + end, u) + pf = SciMLBase.ParamJacobianWrapper(fun, t, u) + fg_u = jacobian_wrt_vars(pf, p, input_idxs, chunk) + else + length(sts) == 0 || + error("Number of unknown variables (0) does not match the number of input unknowns ($(length(u)))") + fg_xz = zeros(0, 0) + h_xz = fg_u = zeros(0, length(inputs)) + end + hp = let u = u, t = t + _hp(p) = h(u, p, t) + _hp + end + h_u = jacobian_wrt_vars(hp, p, input_idxs, chunk) + (f_x = fg_xz[diff_idxs, diff_idxs], + f_z = fg_xz[diff_idxs, alge_idxs], + g_x = fg_xz[alge_idxs, diff_idxs], + g_z = fg_xz[alge_idxs, alge_idxs], + f_u = fg_u[diff_idxs, :], + g_u = fg_u[alge_idxs, :], + h_x = h_xz[:, diff_idxs], + h_z = h_xz[:, alge_idxs], + h_u = h_u) + end + end + return lin_fun, sys +end + +""" + $(TYPEDEF) + +Mock `DEIntegrator` to allow using `CheckInit` without having to create a new integrator +(and consequently depend on `OrdinaryDiffEq`). + +# Fields + +$(TYPEDFIELDS) +""" +struct MockIntegrator{iip, U, P, T, C} <: SciMLBase.DEIntegrator{Nothing, iip, U, T} + """ + The state vector. + """ + u::U + """ + The parameter object. + """ + p::P + """ + The current time. + """ + t::T + """ + The integrator cache. + """ + cache::C +end + +function MockIntegrator{iip}(u::U, p::P, t::T, cache::C) where {iip, U, P, T, C} + return MockIntegrator{iip, U, P, T, C}(u, p, t, cache) +end + +SymbolicIndexingInterface.state_values(integ::MockIntegrator) = integ.u +SymbolicIndexingInterface.parameter_values(integ::MockIntegrator) = integ.p +SymbolicIndexingInterface.current_time(integ::MockIntegrator) = integ.t +SciMLBase.get_tmp_cache(integ::MockIntegrator) = integ.cache + +""" + (; A, B, C, D), simplified_sys = linearize_symbolic(sys::AbstractSystem, inputs, outputs; simplify = false, allow_input_derivatives = false, kwargs...) + +Similar to [`linearize`](@ref), but returns symbolic matrices `A,B,C,D` rather than numeric. While `linearize` uses ForwardDiff to perform the linearization, this function uses `Symbolics.jacobian`. + +See [`linearize`](@ref) for a description of the arguments. + +# Extended help +The named tuple returned as the first argument additionally contains the jacobians `f_x, f_z, g_x, g_z, f_u, g_u, h_x, h_z, h_u` of +```math +\\begin{aligned} +ẋ &= f(x, z, u) \\\\ +0 &= g(x, z, u) \\\\ +y &= h(x, z, u) +\\end{aligned} +``` +where `x` are differential unknown variables, `z` algebraic variables, `u` inputs and `y` outputs. +""" +function linearize_symbolic(sys::AbstractSystem, inputs, + outputs; simplify = false, allow_input_derivatives = false, + eval_expression = false, eval_module = @__MODULE__, + kwargs...) + sys, diff_idxs, alge_idxs, input_idxs = io_preprocessing( + sys, inputs, outputs; simplify, + kwargs...) + sts = unknowns(sys) + t = get_iv(sys) + ps = parameters(sys; initial_parameters = true) + p = reorder_parameters(sys, ps) + + fun_expr = generate_function(sys, sts, ps; expression = Val{true})[1] + fun = eval_or_rgf(fun_expr; eval_expression, eval_module) + dx = fun(sts, p, t) + + h = build_explicit_observed_function(sys, outputs; eval_expression, eval_module) + y = h(sts, p, t) + + fg_xz = Symbolics.jacobian(dx, sts) + fg_u = Symbolics.jacobian(dx, inputs) + h_xz = Symbolics.jacobian(y, sts) + h_u = Symbolics.jacobian(y, inputs) + f_x = fg_xz[diff_idxs, diff_idxs] + f_z = fg_xz[diff_idxs, alge_idxs] + g_x = fg_xz[alge_idxs, diff_idxs] + g_z = fg_xz[alge_idxs, alge_idxs] + f_u = fg_u[diff_idxs, :] + g_u = fg_u[alge_idxs, :] + h_x = h_xz[:, diff_idxs] + h_z = h_xz[:, alge_idxs] + + nx, nu = size(f_u) + nz = size(f_z, 2) + ny = size(h_x, 1) + + D = h_u + + if isempty(g_z) # ODE + A = f_x + B = f_u + C = h_x + else + gz = lu(g_z; check = false) + issuccess(gz) || + error("g_z not invertible, this indicates that the DAE is of index > 1.") + gzgx = -(gz \ g_x) + A = [f_x f_z + gzgx*f_x gzgx*f_z] + B = [f_u + gzgx * f_u] # The cited paper has zeros in the bottom block, see derivation in https://github.com/SciML/ModelingToolkit.jl/pull/1691 for the correct formula + + C = [h_x h_z] + Bs = -(gz \ g_u) # This equation differ from the cited paper, the paper is likely wrong since their equaiton leads to a dimension mismatch. + if !iszero(Bs) + if !allow_input_derivatives + der_inds = findall(vec(any(!iszero, Bs, dims = 1))) + @show typeof(der_inds) + error("Input derivatives appeared in expressions (-g_z\\g_u != 0), the following inputs appeared differentiated: $(ModelingToolkit.inputs(sys)[der_inds]). Call `linearize_symbolic` with keyword argument `allow_input_derivatives = true` to allow this and have the returned `B` matrix be of double width ($(2nu)), where the last $nu inputs are the derivatives of the first $nu inputs.") + end + B = [B [zeros(nx, nu); Bs]] + D = [D zeros(ny, nu)] + end + end + + (; A, B, C, D, f_x, f_z, g_x, g_z, f_u, g_u, h_x, h_z, h_u), sys +end + +function markio!(state, orig_inputs, inputs, outputs; check = true) + fullvars = get_fullvars(state) + inputset = Dict{Any, Bool}(i => false for i in inputs) + outputset = Dict{Any, Bool}(o => false for o in outputs) + for (i, v) in enumerate(fullvars) + if v in keys(inputset) + if v in keys(outputset) + v = setio(v, true, true) + outputset[v] = true + else + v = setio(v, true, false) + end + inputset[v] = true + fullvars[i] = v + elseif v in keys(outputset) + v = setio(v, false, true) + outputset[v] = true + fullvars[i] = v + else + if isinput(v) + push!(orig_inputs, v) + end + v = setio(v, false, false) + fullvars[i] = v + end + end + if check + ikeys = keys(filter(!last, inputset)) + if !isempty(ikeys) + error( + "Some specified inputs were not found in system. The following variables were not found ", + ikeys) + end + end + check && (all(values(outputset)) || + error( + "Some specified outputs were not found in system. The following Dict indicates the found variables ", + outputset)) + state, orig_inputs +end + +""" + (; A, B, C, D), simplified_sys = linearize(sys, inputs, outputs; t=0.0, op = Dict(), allow_input_derivatives = false, zero_dummy_der=false, kwargs...) + (; A, B, C, D) = linearize(simplified_sys, lin_fun; t=0.0, op = Dict(), allow_input_derivatives = false, zero_dummy_der=false) + +Linearize `sys` between `inputs` and `outputs`, both vectors of variables. Return a NamedTuple with the matrices of a linear statespace representation +on the form + +```math +\\begin{aligned} +ẋ &= Ax + Bu\\\\ +y &= Cx + Du +\\end{aligned} +``` + +The first signature automatically calls [`linearization_function`](@ref) internally, +while the second signature expects the outputs of [`linearization_function`](@ref) as input. + +`op` denotes the operating point around which to linearize. If none is provided, +the default values of `sys` are used. + +If `allow_input_derivatives = false`, an error will be thrown if input derivatives (``u̇``) appear as inputs in the linearized equations. If input derivatives are allowed, the returned `B` matrix will be of double width, corresponding to the input `[u; u̇]`. + +`zero_dummy_der` can be set to automatically set the operating point to zero for all dummy derivatives. + +See also [`linearization_function`](@ref) which provides a lower-level interface, [`linearize_symbolic`](@ref) and [`ModelingToolkit.reorder_unknowns`](@ref). + +See extended help for an example. + +The implementation and notation follows that of +["Linear Analysis Approach for Modelica Models", Allain et al. 2009](https://ep.liu.se/ecp/043/075/ecp09430097.pdf) + +# Extended help + +This example builds the following feedback interconnection and linearizes it from the input of `F` to the output of `P`. + +``` + + r ┌─────┐ ┌─────┐ ┌─────┐ +───►│ ├──────►│ │ u │ │ + │ F │ │ C ├────►│ P │ y + └─────┘ ┌►│ │ │ ├─┬─► + │ └─────┘ └─────┘ │ + │ │ + └─────────────────────┘ +``` + +```julia +using ModelingToolkit +using ModelingToolkit: t_nounits as t, D_nounits as D +function plant(; name) + @variables x(t) = 1 + @variables u(t)=0 y(t)=0 + eqs = [D(x) ~ -x + u + y ~ x] + ODESystem(eqs, t; name = name) +end + +function ref_filt(; name) + @variables x(t)=0 y(t)=0 + @variables u(t)=0 [input = true] + eqs = [D(x) ~ -2 * x + u + y ~ x] + ODESystem(eqs, t, name = name) +end + +function controller(kp; name) + @variables y(t)=0 r(t)=0 u(t)=0 + @parameters kp = kp + eqs = [ + u ~ kp * (r - y), + ] + ODESystem(eqs, t; name = name) +end + +@named f = ref_filt() +@named c = controller(1) +@named p = plant() + +connections = [f.y ~ c.r # filtered reference to controller reference + c.u ~ p.u # controller output to plant input + p.y ~ c.y] + +@named cl = ODESystem(connections, t, systems = [f, c, p]) + +lsys0, ssys = linearize(cl, [f.u], [p.x]) +desired_order = [f.x, p.x] +lsys = ModelingToolkit.reorder_unknowns(lsys0, unknowns(ssys), desired_order) + +@assert lsys.A == [-2 0; 1 -2] +@assert lsys.B == [1; 0;;] +@assert lsys.C == [0 1] +@assert lsys.D[] == 0 + +## Symbolic linearization +lsys_sym, _ = ModelingToolkit.linearize_symbolic(cl, [f.u], [p.x]) + +@assert substitute(lsys_sym.A, ModelingToolkit.defaults(cl)) == lsys.A +``` +""" +function linearize(sys, lin_fun; t = 0.0, op = Dict(), allow_input_derivatives = false, + p = DiffEqBase.NullParameters()) + x0 = merge(defaults(sys), Dict(missing_variable_defaults(sys)), op) + u0, defs = get_u0(sys, x0, p) + if has_index_cache(sys) && get_index_cache(sys) !== nothing + if p isa SciMLBase.NullParameters + p = op + elseif p isa Dict + p = merge(p, op) + elseif p isa Vector && eltype(p) <: Pair + p = merge(Dict(p), op) + elseif p isa Vector + p = merge(Dict(parameters(sys) .=> p), op) + end + end + linres = lin_fun(u0, p, t) + f_x, f_z, g_x, g_z, f_u, g_u, h_x, h_z, h_u = linres + + nx, nu = size(f_u) + nz = size(f_z, 2) + ny = size(h_x, 1) + + D = h_u + + if isempty(g_z) + A = f_x + B = f_u + C = h_x + @assert iszero(g_x) + @assert iszero(g_z) + @assert iszero(g_u) + else + gz = lu(g_z; check = false) + issuccess(gz) || + error("g_z not invertible, this indicates that the DAE is of index > 1.") + gzgx = -(gz \ g_x) + A = [f_x f_z + gzgx*f_x gzgx*f_z] + B = [f_u + gzgx * f_u] # The cited paper has zeros in the bottom block, see derivation in https://github.com/SciML/ModelingToolkit.jl/pull/1691 for the correct formula + + C = [h_x h_z] + Bs = -(gz \ g_u) # This equation differ from the cited paper, the paper is likely wrong since their equaiton leads to a dimension mismatch. + if !iszero(Bs) + if !allow_input_derivatives + der_inds = findall(vec(any(!=(0), Bs, dims = 1))) + error("Input derivatives appeared in expressions (-g_z\\g_u != 0), the following inputs appeared differentiated: $(inputs(sys)[der_inds]). Call `linearize` with keyword argument `allow_input_derivatives = true` to allow this and have the returned `B` matrix be of double width ($(2nu)), where the last $nu inputs are the derivatives of the first $nu inputs.") + end + B = [B [zeros(nx, nu); Bs]] + D = [D zeros(ny, nu)] + end + end + + (; A, B, C, D) +end + +function linearize(sys, inputs, outputs; op = Dict(), t = 0.0, + allow_input_derivatives = false, + zero_dummy_der = false, + kwargs...) + lin_fun, ssys = linearization_function(sys, + inputs, + outputs; + zero_dummy_der, + op, + kwargs...) + linearize(ssys, lin_fun; op, t, allow_input_derivatives), ssys +end + +""" + (; Ã, B̃, C̃, D̃) = similarity_transform(sys, T; unitary=false) + +Perform a similarity transform `T : Tx̃ = x` on linear system represented by matrices in NamedTuple `sys` such that + +``` +Ã = T⁻¹AT +B̃ = T⁻¹ B +C̃ = CT +D̃ = D +``` + +If `unitary=true`, `T` is assumed unitary and the matrix adjoint is used instead of the inverse. +""" +function similarity_transform(sys::NamedTuple, T; unitary = false) + if unitary + A = T'sys.A * T + B = T'sys.B + else + Tf = lu(T) + A = Tf \ sys.A * T + B = Tf \ sys.B + end + C = sys.C * T + D = sys.D + (; A, B, C, D) +end + +""" + reorder_unknowns(sys::NamedTuple, old, new) + +Permute the state representation of `sys` obtained from [`linearize`](@ref) so that the state unknown is changed from `old` to `new` +Example: + +``` +lsys, ssys = linearize(pid, [reference.u, measurement.u], [ctr_output.u]) +desired_order = [int.x, der.x] # Unknowns that are present in unknowns(ssys) +lsys = ModelingToolkit.reorder_unknowns(lsys, unknowns(ssys), desired_order) +``` + +See also [`ModelingToolkit.similarity_transform`](@ref) +""" +function reorder_unknowns(sys::NamedTuple, old, new) + nx = length(old) + length(new) == nx || error("old and new must have the same length") + perm = [findfirst(isequal(n), old) for n in new] + issorted(perm) && return sys # shortcut return, no reordering + P = zeros(Int, nx, nx) + for i in 1:nx # Build permutation matrix + P[i, perm[i]] = 1 + end + similarity_transform(sys, P; unitary = true) +end diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index 9278afc366..8093ca5a13 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -2254,544 +2254,6 @@ function io_preprocessing(sys::AbstractSystem, inputs, sys, diff_idxs, alge_idxs, input_idxs end -""" - lin_fun, simplified_sys = linearization_function(sys::AbstractSystem, inputs, outputs; simplify = false, initialize = true, initialization_solver_alg = TrustRegion(), kwargs...) - -Return a function that linearizes the system `sys`. The function [`linearize`](@ref) provides a higher-level and easier to use interface. - -`lin_fun` is a function `(variables, p, t) -> (; f_x, f_z, g_x, g_z, f_u, g_u, h_x, h_z, h_u)`, i.e., it returns a NamedTuple with the Jacobians of `f,g,h` for the nonlinear `sys` (technically for `simplified_sys`) on the form - -```math -\\begin{aligned} -ẋ &= f(x, z, u) \\\\ -0 &= g(x, z, u) \\\\ -y &= h(x, z, u) -\\end{aligned} -``` - -where `x` are differential unknown variables, `z` algebraic variables, `u` inputs and `y` outputs. To obtain a linear statespace representation, see [`linearize`](@ref). The input argument `variables` is a vector defining the operating point, corresponding to `unknowns(simplified_sys)` and `p` is a vector corresponding to the parameters of `simplified_sys`. Note: all variables in `inputs` have been converted to parameters in `simplified_sys`. - -The `simplified_sys` has undergone [`structural_simplify`](@ref) and had any occurring input or output variables replaced with the variables provided in arguments `inputs` and `outputs`. The unknowns of this system also indicate the order of the unknowns that holds for the linearized matrices. - -# Arguments: - - - `sys`: An [`ODESystem`](@ref). This function will automatically apply simplification passes on `sys` and return the resulting `simplified_sys`. - - `inputs`: A vector of variables that indicate the inputs of the linearized input-output model. - - `outputs`: A vector of variables that indicate the outputs of the linearized input-output model. - - `simplify`: Apply simplification in tearing. - - `initialize`: If true, a check is performed to ensure that the operating point is consistent (satisfies algebraic equations). If the op is not consistent, initialization is performed. - - `initialization_solver_alg`: A NonlinearSolve algorithm to use for solving for a feasible set of state and algebraic variables that satisfies the specified operating point. - - `kwargs`: Are passed on to `find_solvables!` - -See also [`linearize`](@ref) which provides a higher-level interface. -""" -function linearization_function(sys::AbstractSystem, inputs, - outputs; simplify = false, - initialize = true, - initializealg = nothing, - initialization_abstol = 1e-5, - initialization_reltol = 1e-3, - op = Dict(), - p = DiffEqBase.NullParameters(), - zero_dummy_der = false, - initialization_solver_alg = TrustRegion(), - eval_expression = false, eval_module = @__MODULE__, - warn_initialize_determined = true, - kwargs...) - op = Dict(op) - inputs isa AbstractVector || (inputs = [inputs]) - outputs isa AbstractVector || (outputs = [outputs]) - inputs = mapreduce(vcat, inputs; init = []) do var - symbolic_type(var) == ArraySymbolic() ? collect(var) : [var] - end - outputs = mapreduce(vcat, outputs; init = []) do var - symbolic_type(var) == ArraySymbolic() ? collect(var) : [var] - end - ssys, diff_idxs, alge_idxs, input_idxs = io_preprocessing(sys, inputs, outputs; - simplify, - kwargs...) - if zero_dummy_der - dummyder = setdiff(unknowns(ssys), unknowns(sys)) - defs = Dict(x => 0.0 for x in dummyder) - @set! ssys.defaults = merge(defs, defaults(ssys)) - op = merge(defs, op) - end - sys = ssys - - if initializealg === nothing - initializealg = initialize ? OverrideInit() : NoInit() - end - - fun, u0, p = process_SciMLProblem( - ODEFunction{true, SciMLBase.FullSpecialize}, sys, merge( - defaults_and_guesses(sys), op), p; - t = 0.0, build_initializeprob = initializealg isa OverrideInit, - allow_incomplete = true, algebraic_only = true) - prob = ODEProblem(fun, u0, (nothing, nothing), p) - - ps = parameters(sys) - h = build_explicit_observed_function(sys, outputs; eval_expression, eval_module) - lin_fun = let diff_idxs = diff_idxs, - alge_idxs = alge_idxs, - input_idxs = input_idxs, - sts = unknowns(sys), - fun = fun, - prob = prob, - sys_ps = p, - h = h, - integ_cache = (similar(u0)), - chunk = ForwardDiff.Chunk(input_idxs), - initializealg = initializealg, - initialization_abstol = initialization_abstol, - initialization_reltol = initialization_reltol, - initialization_solver_alg = initialization_solver_alg, - sys = sys - - function (u, p, t) - if !isa(p, MTKParameters) - p = todict(p) - newps = deepcopy(sys_ps) - for (k, v) in p - if is_parameter(sys, k) - v = fixpoint_sub(v, p) - setp(sys, k)(newps, v) - end - end - p = newps - end - - if u !== nothing # Handle systems without unknowns - length(sts) == length(u) || - error("Number of unknown variables ($(length(sts))) does not match the number of input unknowns ($(length(u)))") - - integ = MockIntegrator{true}(u, p, t, integ_cache) - u, p, success = SciMLBase.get_initial_values( - prob, integ, fun, initializealg, Val(true); - abstol = initialization_abstol, reltol = initialization_reltol, - nlsolve_alg = initialization_solver_alg) - if !success - error("Initialization algorithm $(initializealg) failed with `u = $u` and `p = $p`.") - end - uf = SciMLBase.UJacobianWrapper(fun, t, p) - fg_xz = ForwardDiff.jacobian(uf, u) - h_xz = ForwardDiff.jacobian( - let p = p, t = t - xz -> h(xz, p, t) - end, u) - pf = SciMLBase.ParamJacobianWrapper(fun, t, u) - fg_u = jacobian_wrt_vars(pf, p, input_idxs, chunk) - else - length(sts) == 0 || - error("Number of unknown variables (0) does not match the number of input unknowns ($(length(u)))") - fg_xz = zeros(0, 0) - h_xz = fg_u = zeros(0, length(inputs)) - end - hp = let u = u, t = t - _hp(p) = h(u, p, t) - _hp - end - h_u = jacobian_wrt_vars(hp, p, input_idxs, chunk) - (f_x = fg_xz[diff_idxs, diff_idxs], - f_z = fg_xz[diff_idxs, alge_idxs], - g_x = fg_xz[alge_idxs, diff_idxs], - g_z = fg_xz[alge_idxs, alge_idxs], - f_u = fg_u[diff_idxs, :], - g_u = fg_u[alge_idxs, :], - h_x = h_xz[:, diff_idxs], - h_z = h_xz[:, alge_idxs], - h_u = h_u) - end - end - return lin_fun, sys -end - -""" - $(TYPEDEF) - -Mock `DEIntegrator` to allow using `CheckInit` without having to create a new integrator -(and consequently depend on `OrdinaryDiffEq`). - -# Fields - -$(TYPEDFIELDS) -""" -struct MockIntegrator{iip, U, P, T, C} <: SciMLBase.DEIntegrator{Nothing, iip, U, T} - """ - The state vector. - """ - u::U - """ - The parameter object. - """ - p::P - """ - The current time. - """ - t::T - """ - The integrator cache. - """ - cache::C -end - -function MockIntegrator{iip}(u::U, p::P, t::T, cache::C) where {iip, U, P, T, C} - return MockIntegrator{iip, U, P, T, C}(u, p, t, cache) -end - -SymbolicIndexingInterface.state_values(integ::MockIntegrator) = integ.u -SymbolicIndexingInterface.parameter_values(integ::MockIntegrator) = integ.p -SymbolicIndexingInterface.current_time(integ::MockIntegrator) = integ.t -SciMLBase.get_tmp_cache(integ::MockIntegrator) = integ.cache - -""" - (; A, B, C, D), simplified_sys = linearize_symbolic(sys::AbstractSystem, inputs, outputs; simplify = false, allow_input_derivatives = false, kwargs...) - -Similar to [`linearize`](@ref), but returns symbolic matrices `A,B,C,D` rather than numeric. While `linearize` uses ForwardDiff to perform the linearization, this function uses `Symbolics.jacobian`. - -See [`linearize`](@ref) for a description of the arguments. - -# Extended help -The named tuple returned as the first argument additionally contains the jacobians `f_x, f_z, g_x, g_z, f_u, g_u, h_x, h_z, h_u` of -```math -\\begin{aligned} -ẋ &= f(x, z, u) \\\\ -0 &= g(x, z, u) \\\\ -y &= h(x, z, u) -\\end{aligned} -``` -where `x` are differential unknown variables, `z` algebraic variables, `u` inputs and `y` outputs. -""" -function linearize_symbolic(sys::AbstractSystem, inputs, - outputs; simplify = false, allow_input_derivatives = false, - eval_expression = false, eval_module = @__MODULE__, - kwargs...) - sys, diff_idxs, alge_idxs, input_idxs = io_preprocessing( - sys, inputs, outputs; simplify, - kwargs...) - sts = unknowns(sys) - t = get_iv(sys) - ps = parameters(sys; initial_parameters = true) - p = reorder_parameters(sys, ps) - - fun_expr = generate_function(sys, sts, ps; expression = Val{true})[1] - fun = eval_or_rgf(fun_expr; eval_expression, eval_module) - dx = fun(sts, p, t) - - h = build_explicit_observed_function(sys, outputs; eval_expression, eval_module) - y = h(sts, p, t) - - fg_xz = Symbolics.jacobian(dx, sts) - fg_u = Symbolics.jacobian(dx, inputs) - h_xz = Symbolics.jacobian(y, sts) - h_u = Symbolics.jacobian(y, inputs) - f_x = fg_xz[diff_idxs, diff_idxs] - f_z = fg_xz[diff_idxs, alge_idxs] - g_x = fg_xz[alge_idxs, diff_idxs] - g_z = fg_xz[alge_idxs, alge_idxs] - f_u = fg_u[diff_idxs, :] - g_u = fg_u[alge_idxs, :] - h_x = h_xz[:, diff_idxs] - h_z = h_xz[:, alge_idxs] - - nx, nu = size(f_u) - nz = size(f_z, 2) - ny = size(h_x, 1) - - D = h_u - - if isempty(g_z) # ODE - A = f_x - B = f_u - C = h_x - else - gz = lu(g_z; check = false) - issuccess(gz) || - error("g_z not invertible, this indicates that the DAE is of index > 1.") - gzgx = -(gz \ g_x) - A = [f_x f_z - gzgx*f_x gzgx*f_z] - B = [f_u - gzgx * f_u] # The cited paper has zeros in the bottom block, see derivation in https://github.com/SciML/ModelingToolkit.jl/pull/1691 for the correct formula - - C = [h_x h_z] - Bs = -(gz \ g_u) # This equation differ from the cited paper, the paper is likely wrong since their equaiton leads to a dimension mismatch. - if !iszero(Bs) - if !allow_input_derivatives - der_inds = findall(vec(any(!iszero, Bs, dims = 1))) - @show typeof(der_inds) - error("Input derivatives appeared in expressions (-g_z\\g_u != 0), the following inputs appeared differentiated: $(ModelingToolkit.inputs(sys)[der_inds]). Call `linearize_symbolic` with keyword argument `allow_input_derivatives = true` to allow this and have the returned `B` matrix be of double width ($(2nu)), where the last $nu inputs are the derivatives of the first $nu inputs.") - end - B = [B [zeros(nx, nu); Bs]] - D = [D zeros(ny, nu)] - end - end - - (; A, B, C, D, f_x, f_z, g_x, g_z, f_u, g_u, h_x, h_z, h_u), sys -end - -function markio!(state, orig_inputs, inputs, outputs; check = true) - fullvars = get_fullvars(state) - inputset = Dict{Any, Bool}(i => false for i in inputs) - outputset = Dict{Any, Bool}(o => false for o in outputs) - for (i, v) in enumerate(fullvars) - if v in keys(inputset) - if v in keys(outputset) - v = setio(v, true, true) - outputset[v] = true - else - v = setio(v, true, false) - end - inputset[v] = true - fullvars[i] = v - elseif v in keys(outputset) - v = setio(v, false, true) - outputset[v] = true - fullvars[i] = v - else - if isinput(v) - push!(orig_inputs, v) - end - v = setio(v, false, false) - fullvars[i] = v - end - end - if check - ikeys = keys(filter(!last, inputset)) - if !isempty(ikeys) - error( - "Some specified inputs were not found in system. The following variables were not found ", - ikeys) - end - end - check && (all(values(outputset)) || - error( - "Some specified outputs were not found in system. The following Dict indicates the found variables ", - outputset)) - state, orig_inputs -end - -""" - (; A, B, C, D), simplified_sys = linearize(sys, inputs, outputs; t=0.0, op = Dict(), allow_input_derivatives = false, zero_dummy_der=false, kwargs...) - (; A, B, C, D) = linearize(simplified_sys, lin_fun; t=0.0, op = Dict(), allow_input_derivatives = false, zero_dummy_der=false) - -Linearize `sys` between `inputs` and `outputs`, both vectors of variables. Return a NamedTuple with the matrices of a linear statespace representation -on the form - -```math -\\begin{aligned} -ẋ &= Ax + Bu\\\\ -y &= Cx + Du -\\end{aligned} -``` - -The first signature automatically calls [`linearization_function`](@ref) internally, -while the second signature expects the outputs of [`linearization_function`](@ref) as input. - -`op` denotes the operating point around which to linearize. If none is provided, -the default values of `sys` are used. - -If `allow_input_derivatives = false`, an error will be thrown if input derivatives (``u̇``) appear as inputs in the linearized equations. If input derivatives are allowed, the returned `B` matrix will be of double width, corresponding to the input `[u; u̇]`. - -`zero_dummy_der` can be set to automatically set the operating point to zero for all dummy derivatives. - -See also [`linearization_function`](@ref) which provides a lower-level interface, [`linearize_symbolic`](@ref) and [`ModelingToolkit.reorder_unknowns`](@ref). - -See extended help for an example. - -The implementation and notation follows that of -["Linear Analysis Approach for Modelica Models", Allain et al. 2009](https://ep.liu.se/ecp/043/075/ecp09430097.pdf) - -# Extended help - -This example builds the following feedback interconnection and linearizes it from the input of `F` to the output of `P`. - -``` - - r ┌─────┐ ┌─────┐ ┌─────┐ -───►│ ├──────►│ │ u │ │ - │ F │ │ C ├────►│ P │ y - └─────┘ ┌►│ │ │ ├─┬─► - │ └─────┘ └─────┘ │ - │ │ - └─────────────────────┘ -``` - -```julia -using ModelingToolkit -using ModelingToolkit: t_nounits as t, D_nounits as D -function plant(; name) - @variables x(t) = 1 - @variables u(t)=0 y(t)=0 - eqs = [D(x) ~ -x + u - y ~ x] - ODESystem(eqs, t; name = name) -end - -function ref_filt(; name) - @variables x(t)=0 y(t)=0 - @variables u(t)=0 [input = true] - eqs = [D(x) ~ -2 * x + u - y ~ x] - ODESystem(eqs, t, name = name) -end - -function controller(kp; name) - @variables y(t)=0 r(t)=0 u(t)=0 - @parameters kp = kp - eqs = [ - u ~ kp * (r - y), - ] - ODESystem(eqs, t; name = name) -end - -@named f = ref_filt() -@named c = controller(1) -@named p = plant() - -connections = [f.y ~ c.r # filtered reference to controller reference - c.u ~ p.u # controller output to plant input - p.y ~ c.y] - -@named cl = ODESystem(connections, t, systems = [f, c, p]) - -lsys0, ssys = linearize(cl, [f.u], [p.x]) -desired_order = [f.x, p.x] -lsys = ModelingToolkit.reorder_unknowns(lsys0, unknowns(ssys), desired_order) - -@assert lsys.A == [-2 0; 1 -2] -@assert lsys.B == [1; 0;;] -@assert lsys.C == [0 1] -@assert lsys.D[] == 0 - -## Symbolic linearization -lsys_sym, _ = ModelingToolkit.linearize_symbolic(cl, [f.u], [p.x]) - -@assert substitute(lsys_sym.A, ModelingToolkit.defaults(cl)) == lsys.A -``` -""" -function linearize(sys, lin_fun; t = 0.0, op = Dict(), allow_input_derivatives = false, - p = DiffEqBase.NullParameters()) - x0 = merge(defaults(sys), Dict(missing_variable_defaults(sys)), op) - u0, defs = get_u0(sys, x0, p) - if has_index_cache(sys) && get_index_cache(sys) !== nothing - if p isa SciMLBase.NullParameters - p = op - elseif p isa Dict - p = merge(p, op) - elseif p isa Vector && eltype(p) <: Pair - p = merge(Dict(p), op) - elseif p isa Vector - p = merge(Dict(parameters(sys) .=> p), op) - end - end - linres = lin_fun(u0, p, t) - f_x, f_z, g_x, g_z, f_u, g_u, h_x, h_z, h_u = linres - - nx, nu = size(f_u) - nz = size(f_z, 2) - ny = size(h_x, 1) - - D = h_u - - if isempty(g_z) - A = f_x - B = f_u - C = h_x - @assert iszero(g_x) - @assert iszero(g_z) - @assert iszero(g_u) - else - gz = lu(g_z; check = false) - issuccess(gz) || - error("g_z not invertible, this indicates that the DAE is of index > 1.") - gzgx = -(gz \ g_x) - A = [f_x f_z - gzgx*f_x gzgx*f_z] - B = [f_u - gzgx * f_u] # The cited paper has zeros in the bottom block, see derivation in https://github.com/SciML/ModelingToolkit.jl/pull/1691 for the correct formula - - C = [h_x h_z] - Bs = -(gz \ g_u) # This equation differ from the cited paper, the paper is likely wrong since their equaiton leads to a dimension mismatch. - if !iszero(Bs) - if !allow_input_derivatives - der_inds = findall(vec(any(!=(0), Bs, dims = 1))) - error("Input derivatives appeared in expressions (-g_z\\g_u != 0), the following inputs appeared differentiated: $(inputs(sys)[der_inds]). Call `linearize` with keyword argument `allow_input_derivatives = true` to allow this and have the returned `B` matrix be of double width ($(2nu)), where the last $nu inputs are the derivatives of the first $nu inputs.") - end - B = [B [zeros(nx, nu); Bs]] - D = [D zeros(ny, nu)] - end - end - - (; A, B, C, D) -end - -function linearize(sys, inputs, outputs; op = Dict(), t = 0.0, - allow_input_derivatives = false, - zero_dummy_der = false, - kwargs...) - lin_fun, ssys = linearization_function(sys, - inputs, - outputs; - zero_dummy_der, - op, - kwargs...) - linearize(ssys, lin_fun; op, t, allow_input_derivatives), ssys -end - -""" - (; Ã, B̃, C̃, D̃) = similarity_transform(sys, T; unitary=false) - -Perform a similarity transform `T : Tx̃ = x` on linear system represented by matrices in NamedTuple `sys` such that - -``` -Ã = T⁻¹AT -B̃ = T⁻¹ B -C̃ = CT -D̃ = D -``` - -If `unitary=true`, `T` is assumed unitary and the matrix adjoint is used instead of the inverse. -""" -function similarity_transform(sys::NamedTuple, T; unitary = false) - if unitary - A = T'sys.A * T - B = T'sys.B - else - Tf = lu(T) - A = Tf \ sys.A * T - B = Tf \ sys.B - end - C = sys.C * T - D = sys.D - (; A, B, C, D) -end - -""" - reorder_unknowns(sys::NamedTuple, old, new) - -Permute the state representation of `sys` obtained from [`linearize`](@ref) so that the state unknown is changed from `old` to `new` -Example: - -``` -lsys, ssys = linearize(pid, [reference.u, measurement.u], [ctr_output.u]) -desired_order = [int.x, der.x] # Unknowns that are present in unknowns(ssys) -lsys = ModelingToolkit.reorder_unknowns(lsys, unknowns(ssys), desired_order) -``` - -See also [`ModelingToolkit.similarity_transform`](@ref) -""" -function reorder_unknowns(sys::NamedTuple, old, new) - nx = length(old) - length(new) == nx || error("old and new must have the same length") - perm = [findfirst(isequal(n), old) for n in new] - issorted(perm) && return sys # shortcut return, no reordering - P = zeros(Int, nx, nx) - for i in 1:nx # Build permutation matrix - P[i, perm[i]] = 1 - end - similarity_transform(sys, P; unitary = true) -end - @latexrecipe function f(sys::AbstractSystem) return latexify(equations(sys)) end From 382ce9b8c6bcb4d3afc4bd7f5db92aff04f2b0e9 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 11 Feb 2025 16:43:55 +0530 Subject: [PATCH 0901/2176] refactor: use new `LinearizationFunction` instead of closure in `linearization_function` --- src/linearization.jl | 183 +++++++++++++++++++++++++++---------------- 1 file changed, 115 insertions(+), 68 deletions(-) diff --git a/src/linearization.jl b/src/linearization.jl index bbf0d8e2c1..e517c02616 100644 --- a/src/linearization.jl +++ b/src/linearization.jl @@ -75,78 +75,125 @@ function linearization_function(sys::AbstractSystem, inputs, ps = parameters(sys) h = build_explicit_observed_function(sys, outputs; eval_expression, eval_module) - lin_fun = let diff_idxs = diff_idxs, - alge_idxs = alge_idxs, - input_idxs = input_idxs, - sts = unknowns(sys), - fun = fun, - prob = prob, - sys_ps = p, - h = h, - integ_cache = (similar(u0)), - chunk = ForwardDiff.Chunk(input_idxs), - initializealg = initializealg, - initialization_abstol = initialization_abstol, - initialization_reltol = initialization_reltol, - initialization_solver_alg = initialization_solver_alg, - sys = sys - - function (u, p, t) - if !isa(p, MTKParameters) - p = todict(p) - newps = deepcopy(sys_ps) - for (k, v) in p - if is_parameter(sys, k) - v = fixpoint_sub(v, p) - setp(sys, k)(newps, v) - end - end - p = newps - end - if u !== nothing # Handle systems without unknowns - length(sts) == length(u) || - error("Number of unknown variables ($(length(sts))) does not match the number of input unknowns ($(length(u)))") - - integ = MockIntegrator{true}(u, p, t, integ_cache) - u, p, success = SciMLBase.get_initial_values( - prob, integ, fun, initializealg, Val(true); - abstol = initialization_abstol, reltol = initialization_reltol, - nlsolve_alg = initialization_solver_alg) - if !success - error("Initialization algorithm $(initializealg) failed with `u = $u` and `p = $p`.") - end - uf = SciMLBase.UJacobianWrapper(fun, t, p) - fg_xz = ForwardDiff.jacobian(uf, u) - h_xz = ForwardDiff.jacobian( - let p = p, t = t - xz -> h(xz, p, t) - end, u) - pf = SciMLBase.ParamJacobianWrapper(fun, t, u) - fg_u = jacobian_wrt_vars(pf, p, input_idxs, chunk) - else - length(sts) == 0 || - error("Number of unknown variables (0) does not match the number of input unknowns ($(length(u)))") - fg_xz = zeros(0, 0) - h_xz = fg_u = zeros(0, length(inputs)) - end - hp = let u = u, t = t - _hp(p) = h(u, p, t) - _hp + initialization_kwargs = (; + abstol = initialization_abstol, reltol = initialization_reltol, + nlsolve_alg = initialization_solver_alg) + lin_fun = LinearizationFunction( + diff_idxs, alge_idxs, input_idxs, length(unknowns(sys)), prob, h, similar(u0), + ForwardDiff.Chunk(input_idxs), initializealg, initialization_kwargs) + return lin_fun, sys +end + +""" + $(TYPEDEF) + +A callable struct which linearizes a system. + +# Fields + +$(TYPEDFIELDS) +""" +struct LinearizationFunction{ + DI <: AbstractVector{Int}, AI <: AbstractVector{Int}, II, P <: ODEProblem, + H, C, Ch, IA <: SciMLBase.DAEInitializationAlgorithm, IK} + """ + The indexes of differential equations in the linearized system. + """ + diff_idxs::DI + """ + The indexes of algebraic equations in the linearized system. + """ + alge_idxs::AI + """ + The indexes of parameters in the linearized system which represent + input variables. + """ + input_idxs::II + """ + The number of unknowns in the linearized system. + """ + num_states::Int + """ + The `ODEProblem` of the linearized system. + """ + prob::P + """ + A function which takes `(u, p, t)` and returns the outputs of the linearized system. + """ + h::H + """ + Any required cache buffers. + """ + caches::C + # TODO: Use DI? + """ + A `ForwardDiff.Chunk` for taking the jacobian with respect to the inputs. + """ + chunk::Ch + """ + The initialization algorithm to use. + """ + initializealg::IA + """ + Keyword arguments to be passed to `SciMLBase.get_initial_values`. + """ + initialize_kwargs::IK +end + +function (linfun::LinearizationFunction)(u, p, t) + if eltype(p) <: Pair + p = todict(p) + newps = copy(parameter_values(linfun.prob)) + for (k, v) in p + if is_parameter(linfun, k) + v = fixpoint_sub(v, p) + setp(linfun, k)(newps, v) end - h_u = jacobian_wrt_vars(hp, p, input_idxs, chunk) - (f_x = fg_xz[diff_idxs, diff_idxs], - f_z = fg_xz[diff_idxs, alge_idxs], - g_x = fg_xz[alge_idxs, diff_idxs], - g_z = fg_xz[alge_idxs, alge_idxs], - f_u = fg_u[diff_idxs, :], - g_u = fg_u[alge_idxs, :], - h_x = h_xz[:, diff_idxs], - h_z = h_xz[:, alge_idxs], - h_u = h_u) end + p = newps end - return lin_fun, sys + + fun = linfun.prob.f + if u !== nothing # Handle systems without unknowns + linfun.num_states == length(u) || + error("Number of unknown variables ($(linfun.num_states)) does not match the number of input unknowns ($(length(u)))") + integ_cache = linfun.caches + integ = MockIntegrator{true}(u, p, t, integ_cache) + u, p, success = SciMLBase.get_initial_values( + linfun.prob, integ, fun, linfun.initializealg, Val(true); + linfun.initialize_kwargs...) + if !success + error("Initialization algorithm $(linfun.initializealg) failed with `u = $u` and `p = $p`.") + end + uf = SciMLBase.UJacobianWrapper(fun, t, p) + fg_xz = ForwardDiff.jacobian(uf, u) + h_xz = ForwardDiff.jacobian( + let p = p, t = t, h = linfun.h + xz -> h(xz, p, t) + end, u) + pf = SciMLBase.ParamJacobianWrapper(fun, t, u) + fg_u = jacobian_wrt_vars(pf, p, linfun.input_idxs, linfun.chunk) + else + linfun.num_states == 0 || + error("Number of unknown variables (0) does not match the number of input unknowns ($(length(u)))") + fg_xz = zeros(0, 0) + h_xz = fg_u = zeros(0, length(linfun.input_idxs)) + end + hp = let u = u, t = t, h = linfun.h + _hp(p) = h(u, p, t) + _hp + end + h_u = jacobian_wrt_vars(hp, p, linfun.input_idxs, linfun.chunk) + (f_x = fg_xz[linfun.diff_idxs, linfun.diff_idxs], + f_z = fg_xz[linfun.diff_idxs, linfun.alge_idxs], + g_x = fg_xz[linfun.alge_idxs, linfun.diff_idxs], + g_z = fg_xz[linfun.alge_idxs, linfun.alge_idxs], + f_u = fg_u[linfun.diff_idxs, :], + g_u = fg_u[linfun.alge_idxs, :], + h_x = h_xz[:, linfun.diff_idxs], + h_z = h_xz[:, linfun.alge_idxs], + h_u = h_u) end """ From f0590aedc64335fbaeb17e07e4c9f22e635aa7bf Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 11 Feb 2025 17:52:42 +0530 Subject: [PATCH 0902/2176] feat: add `LinearizationProblem` --- src/linearization.jl | 126 ++++++++++++++++++++++++++----------------- 1 file changed, 76 insertions(+), 50 deletions(-) diff --git a/src/linearization.jl b/src/linearization.jl index e517c02616..872ad7c6ed 100644 --- a/src/linearization.jl +++ b/src/linearization.jl @@ -141,6 +141,13 @@ struct LinearizationFunction{ initialize_kwargs::IK end +SymbolicIndexingInterface.symbolic_container(f::LinearizationFunction) = f.prob +SymbolicIndexingInterface.state_values(f::LinearizationFunction) = state_values(f.prob) +function SymbolicIndexingInterface.parameter_values(f::LinearizationFunction) + parameter_values(f.prob) +end +SymbolicIndexingInterface.current_time(f::LinearizationFunction) = current_time(f.prob) + function (linfun::LinearizationFunction)(u, p, t) if eltype(p) <: Pair p = todict(p) @@ -234,6 +241,61 @@ SymbolicIndexingInterface.parameter_values(integ::MockIntegrator) = integ.p SymbolicIndexingInterface.current_time(integ::MockIntegrator) = integ.t SciMLBase.get_tmp_cache(integ::MockIntegrator) = integ.cache +mutable struct LinearizationProblem{F <: LinearizationFunction, T} + const f::F + t::T +end + +SymbolicIndexingInterface.symbolic_container(p::LinearizationProblem) = p.f +SymbolicIndexingInterface.state_values(p::LinearizationProblem) = state_values(p.f) +SymbolicIndexingInterface.parameter_values(p::LinearizationProblem) = parameter_values(p.f) +SymbolicIndexingInterface.current_time(p::LinearizationProblem) = p.t + +function CommonSolve.solve(prob::LinearizationProblem; allow_input_derivatives = false) + u0 = state_values(prob) + p = parameter_values(prob) + t = current_time(prob) + linres = prob.f(u0, p, t) + f_x, f_z, g_x, g_z, f_u, g_u, h_x, h_z, h_u = linres + + nx, nu = size(f_u) + nz = size(f_z, 2) + ny = size(h_x, 1) + + D = h_u + + if isempty(g_z) + A = f_x + B = f_u + C = h_x + @assert iszero(g_x) + @assert iszero(g_z) + @assert iszero(g_u) + else + gz = lu(g_z; check = false) + issuccess(gz) || + error("g_z not invertible, this indicates that the DAE is of index > 1.") + gzgx = -(gz \ g_x) + A = [f_x f_z + gzgx*f_x gzgx*f_z] + B = [f_u + gzgx * f_u] # The cited paper has zeros in the bottom block, see derivation in https://github.com/SciML/ModelingToolkit.jl/pull/1691 for the correct formula + + C = [h_x h_z] + Bs = -(gz \ g_u) # This equation differ from the cited paper, the paper is likely wrong since their equaiton leads to a dimension mismatch. + if !iszero(Bs) + if !allow_input_derivatives + der_inds = findall(vec(any(!=(0), Bs, dims = 1))) + error("Input derivatives appeared in expressions (-g_z\\g_u != 0), the following inputs appeared differentiated: $(inputs(sys)[der_inds]). Call `linearize` with keyword argument `allow_input_derivatives = true` to allow this and have the returned `B` matrix be of double width ($(2nu)), where the last $nu inputs are the derivatives of the first $nu inputs.") + end + B = [B [zeros(nx, nu); Bs]] + D = [D zeros(ny, nu)] + end + end + + (; A, B, C, D) +end + """ (; A, B, C, D), simplified_sys = linearize_symbolic(sys::AbstractSystem, inputs, outputs; simplify = false, allow_input_derivatives = false, kwargs...) @@ -460,60 +522,24 @@ lsys_sym, _ = ModelingToolkit.linearize_symbolic(cl, [f.u], [p.x]) @assert substitute(lsys_sym.A, ModelingToolkit.defaults(cl)) == lsys.A ``` """ -function linearize(sys, lin_fun; t = 0.0, op = Dict(), allow_input_derivatives = false, +function linearize(sys, lin_fun::LinearizationFunction; t = 0.0, + op = Dict(), allow_input_derivatives = false, p = DiffEqBase.NullParameters()) - x0 = merge(defaults(sys), Dict(missing_variable_defaults(sys)), op) - u0, defs = get_u0(sys, x0, p) - if has_index_cache(sys) && get_index_cache(sys) !== nothing - if p isa SciMLBase.NullParameters - p = op - elseif p isa Dict - p = merge(p, op) - elseif p isa Vector && eltype(p) <: Pair - p = merge(Dict(p), op) - elseif p isa Vector - p = merge(Dict(parameters(sys) .=> p), op) + prob = LinearizationProblem(lin_fun, t) + op = anydict(op) + evaluate_varmap!(op, unknowns(sys)) + for (k, v) in op + if is_parameter(prob, Initial(k)) + setu(prob, Initial(k))(prob, v) + else + setu(prob, k)(prob, v) end end - linres = lin_fun(u0, p, t) - f_x, f_z, g_x, g_z, f_u, g_u, h_x, h_z, h_u = linres - - nx, nu = size(f_u) - nz = size(f_z, 2) - ny = size(h_x, 1) - - D = h_u - - if isempty(g_z) - A = f_x - B = f_u - C = h_x - @assert iszero(g_x) - @assert iszero(g_z) - @assert iszero(g_u) - else - gz = lu(g_z; check = false) - issuccess(gz) || - error("g_z not invertible, this indicates that the DAE is of index > 1.") - gzgx = -(gz \ g_x) - A = [f_x f_z - gzgx*f_x gzgx*f_z] - B = [f_u - gzgx * f_u] # The cited paper has zeros in the bottom block, see derivation in https://github.com/SciML/ModelingToolkit.jl/pull/1691 for the correct formula - - C = [h_x h_z] - Bs = -(gz \ g_u) # This equation differ from the cited paper, the paper is likely wrong since their equaiton leads to a dimension mismatch. - if !iszero(Bs) - if !allow_input_derivatives - der_inds = findall(vec(any(!=(0), Bs, dims = 1))) - error("Input derivatives appeared in expressions (-g_z\\g_u != 0), the following inputs appeared differentiated: $(inputs(sys)[der_inds]). Call `linearize` with keyword argument `allow_input_derivatives = true` to allow this and have the returned `B` matrix be of double width ($(2nu)), where the last $nu inputs are the derivatives of the first $nu inputs.") - end - B = [B [zeros(nx, nu); Bs]] - D = [D zeros(ny, nu)] - end + p = anydict(p) + for (k, v) in p + setu(prob, k)(prob, v) end - - (; A, B, C, D) + return solve(prob; allow_input_derivatives) end function linearize(sys, inputs, outputs; op = Dict(), t = 0.0, From 0f5c008c17d13979b1829c5ef2e8e3b732159c58 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 11 Feb 2025 18:11:13 +0530 Subject: [PATCH 0903/2176] feat: add `LinearizationProblem` constructor, `getindex` and `.ps` syntax for symbolic indexing --- src/linearization.jl | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/linearization.jl b/src/linearization.jl index 872ad7c6ed..c6c7b84860 100644 --- a/src/linearization.jl +++ b/src/linearization.jl @@ -246,11 +246,31 @@ mutable struct LinearizationProblem{F <: LinearizationFunction, T} t::T end +function LinearizationProblem(sys::AbstractSystem, inputs, outputs; t = 0.0, kwargs...) + linfun, _ = linearization_function(sys, inputs, outputs; kwargs...) + return LinearizationProblem(linfun, t) +end + SymbolicIndexingInterface.symbolic_container(p::LinearizationProblem) = p.f SymbolicIndexingInterface.state_values(p::LinearizationProblem) = state_values(p.f) SymbolicIndexingInterface.parameter_values(p::LinearizationProblem) = parameter_values(p.f) SymbolicIndexingInterface.current_time(p::LinearizationProblem) = p.t +function Base.getindex(prob::LinearizationProblem, idx) + getu(prob, idx)(prob) +end + +function Base.setindex!(prob::LinearizationProblem, val, idx) + setu(prob, idx)(prob, val) +end + +function Base.getproperty(prob::LinearizationProblem, x::Symbol) + if x == :ps + return ParameterIndexingProxy(prob) + end + return getfield(prob, x) +end + function CommonSolve.solve(prob::LinearizationProblem; allow_input_derivatives = false) u0 = state_values(prob) p = parameter_values(prob) From f9c328fb6dddaf6a569182c7aa6ac1d07b56171a Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 11 Feb 2025 18:11:22 +0530 Subject: [PATCH 0904/2176] feat: export `LinearizationProblem` --- src/ModelingToolkit.jl | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/ModelingToolkit.jl b/src/ModelingToolkit.jl index 7d08a9aac2..6501a4e026 100644 --- a/src/ModelingToolkit.jl +++ b/src/ModelingToolkit.jl @@ -259,7 +259,8 @@ export Term, Sym export SymScope, LocalScope, ParentScope, DelayParentScope, GlobalScope export independent_variable, equations, controls, observed, full_equations export initialization_equations, guesses, defaults, parameter_dependencies, hierarchy -export structural_simplify, expand_connections, linearize, linearization_function +export structural_simplify, expand_connections, linearize, linearization_function, + LinearizationProblem export calculate_jacobian, generate_jacobian, generate_function, generate_custom_function export calculate_control_jacobian, generate_control_jacobian From 494a7e7bdec619c9024103eb14b276e7ccf81f7c Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 11 Feb 2025 18:27:53 +0530 Subject: [PATCH 0905/2176] docs: add docstrings for linearization --- src/linearization.jl | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/src/linearization.jl b/src/linearization.jl index c6c7b84860..ad881c7f33 100644 --- a/src/linearization.jl +++ b/src/linearization.jl @@ -148,6 +148,11 @@ function SymbolicIndexingInterface.parameter_values(f::LinearizationFunction) end SymbolicIndexingInterface.current_time(f::LinearizationFunction) = current_time(f.prob) +""" + $(TYPEDSIGNATURES) + +Linearize the wrapped system at the point given by `(u, p, t)`. +""" function (linfun::LinearizationFunction)(u, p, t) if eltype(p) <: Pair p = todict(p) @@ -241,11 +246,33 @@ SymbolicIndexingInterface.parameter_values(integ::MockIntegrator) = integ.p SymbolicIndexingInterface.current_time(integ::MockIntegrator) = integ.t SciMLBase.get_tmp_cache(integ::MockIntegrator) = integ.cache +""" + $(TYPEDEF) + +A struct representing a linearization operation to be performed. Can be symbolically +indexed to efficiently update the operating point for multiple linearizations in a loop. +The value of the independent variable can be set by mutating the `.t` field of this struct. +""" mutable struct LinearizationProblem{F <: LinearizationFunction, T} + """ + The wrapped `LinearizationFunction` + """ const f::F t::T end +""" + $(TYPEDSIGNATURES) + +Construct a `LinearizationProblem` for linearizing the system `sys` with the given +`inputs` and `outputs`. + +# Keyword arguments + +- `t`: The value of the independent variable + +All other keyword arguments are forwarded to `linearization_function`. +""" function LinearizationProblem(sys::AbstractSystem, inputs, outputs; t = 0.0, kwargs...) linfun, _ = linearization_function(sys, inputs, outputs; kwargs...) return LinearizationProblem(linfun, t) From 4eee373d697376ddd3f0cc8c4c7584630afd3fe0 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 11 Feb 2025 20:30:15 +0530 Subject: [PATCH 0906/2176] fix: don't use `defaults_and_guesses` for operating point --- src/linearization.jl | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/linearization.jl b/src/linearization.jl index ad881c7f33..621db21f8c 100644 --- a/src/linearization.jl +++ b/src/linearization.jl @@ -67,8 +67,7 @@ function linearization_function(sys::AbstractSystem, inputs, end fun, u0, p = process_SciMLProblem( - ODEFunction{true, SciMLBase.FullSpecialize}, sys, merge( - defaults_and_guesses(sys), op), p; + ODEFunction{true, SciMLBase.FullSpecialize}, sys, op, p; t = 0.0, build_initializeprob = initializealg isa OverrideInit, allow_incomplete = true, algebraic_only = true) prob = ODEProblem(fun, u0, (nothing, nothing), p) From ce0533fd6c640905d700f94348252b788c5be438 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 11 Feb 2025 20:30:38 +0530 Subject: [PATCH 0907/2176] fix: handle case when `u0 === nothing` in `linearization_function` --- src/linearization.jl | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/linearization.jl b/src/linearization.jl index 621db21f8c..78dd5b1178 100644 --- a/src/linearization.jl +++ b/src/linearization.jl @@ -79,7 +79,8 @@ function linearization_function(sys::AbstractSystem, inputs, abstol = initialization_abstol, reltol = initialization_reltol, nlsolve_alg = initialization_solver_alg) lin_fun = LinearizationFunction( - diff_idxs, alge_idxs, input_idxs, length(unknowns(sys)), prob, h, similar(u0), + diff_idxs, alge_idxs, input_idxs, length(unknowns(sys)), + prob, h, u0 === nothing ? nothing : similar(u0), ForwardDiff.Chunk(input_idxs), initializealg, initialization_kwargs) return lin_fun, sys end From e16cf4d49ee8bccd8196be6363919ccd9e249b6b Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 11 Feb 2025 20:31:38 +0530 Subject: [PATCH 0908/2176] fix: fix initialization of `linearize` testset --- test/downstream/linearize.jl | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/test/downstream/linearize.jl b/test/downstream/linearize.jl index 5119e2f630..cdb33e6a0e 100644 --- a/test/downstream/linearize.jl +++ b/test/downstream/linearize.jl @@ -47,8 +47,8 @@ lsys, ssys = linearize(sys, r, r) # Test allow scalars ``` function plant(; name) - @variables x(t) = 1 - @variables u(t)=0 y(t)=0 + @variables x(t) + @variables u(t) y(t) D = Differential(t) eqs = [D(x) ~ -x + u y ~ x] @@ -56,7 +56,7 @@ function plant(; name) end function filt_(; name) - @variables x(t)=0 y(t)=0 + @variables x(t) y(t) @variables u(t)=0 [input = true] D = Differential(t) eqs = [D(x) ~ -2 * x + u @@ -65,7 +65,7 @@ function filt_(; name) end function controller(kp; name) - @variables y(t)=0 r(t)=0 u(t)=0 + @variables y(t)=0 r(t)=0 u(t) @parameters kp = kp eqs = [ u ~ kp * (r - y) @@ -108,7 +108,8 @@ Nd = 10 @named pid = LimPID(; k, Ti, Td, Nd) @unpack reference, measurement, ctr_output = pid -lsys0, ssys = linearize(pid, [reference.u, measurement.u], [ctr_output.u]) +lsys0, ssys = linearize(pid, [reference.u, measurement.u], [ctr_output.u]; + op = Dict(reference.u => 0.0, measurement.u => 0.0)) @unpack int, der = pid desired_order = [int.x, der.x] lsys = ModelingToolkit.reorder_unknowns(lsys0, unknowns(ssys), desired_order) @@ -314,7 +315,7 @@ matrices = linfun([1.0], Dict(p => 3.0), 1.0) end @testset "Issue #2941" begin - @variables x(t) y(t) + @variables x(t) y(t) [guess = 1.0] @parameters p eqs = [0 ~ x * log(y) - p] @named sys = ODESystem(eqs, t; defaults = [p => 1.0]) From f34a4e0445c8813078dcb7933a782020c4b3c3db Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 11 Feb 2025 20:31:50 +0530 Subject: [PATCH 0909/2176] test: minimally test `LinearizationProblem` --- test/downstream/linearize.jl | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/test/downstream/linearize.jl b/test/downstream/linearize.jl index cdb33e6a0e..3cab641d26 100644 --- a/test/downstream/linearize.jl +++ b/test/downstream/linearize.jl @@ -1,4 +1,5 @@ using ModelingToolkit, Test +using CommonSolve: solve # r is an input, and y is an output. @independent_variables t @@ -14,11 +15,13 @@ eqs = [u ~ kp * (r - y) @named sys = ODESystem(eqs, t) lsys, ssys = linearize(sys, [r], [y]) +lprob = LinearizationProblem(sys, [r], [y]) +lsys2 = solve(lprob) -@test lsys.A[] == -2 -@test lsys.B[] == 1 -@test lsys.C[] == 1 -@test lsys.D[] == 0 +@test lsys.A[] == lsys2.A[] == -2 +@test lsys.B[] == lsys2.B[] == 1 +@test lsys.C[] == lsys2.C[] == 1 +@test lsys.D[] == lsys2.D[] == 0 lsys, ssys = linearize(sys, [r], [r]) From 7793cb3557591e3183934ceb93f8455811991e11 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 11 Feb 2025 22:08:27 +0530 Subject: [PATCH 0910/2176] fix: handle `nothing` values in `InitializationProblem` --- src/systems/diffeqs/abstractodesystem.jl | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/systems/diffeqs/abstractodesystem.jl b/src/systems/diffeqs/abstractodesystem.jl index d8996000cf..c7eff90e03 100644 --- a/src/systems/diffeqs/abstractodesystem.jl +++ b/src/systems/diffeqs/abstractodesystem.jl @@ -1331,7 +1331,9 @@ function InitializationProblem{iip, specialize}(sys::AbstractSystem, end if u0T != Union{} u0T = eltype(u0T) - u0map = Dict(k => if symbolic_type(v) == NotSymbolic() && !is_array_of_symbolics(v) + u0map = Dict(k => if v === nothing + nothing + elseif symbolic_type(v) == NotSymbolic() && !is_array_of_symbolics(v) v isa AbstractArray ? u0T.(v) : u0T(v) else v From 2a2b8b37bd8640190ec0178bf3b6d3640490155b Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 11 Feb 2025 22:08:43 +0530 Subject: [PATCH 0911/2176] fix: handle `nothing` values in `linearize` --- src/linearization.jl | 1 + 1 file changed, 1 insertion(+) diff --git a/src/linearization.jl b/src/linearization.jl index 78dd5b1178..469c2cd766 100644 --- a/src/linearization.jl +++ b/src/linearization.jl @@ -576,6 +576,7 @@ function linearize(sys, lin_fun::LinearizationFunction; t = 0.0, op = anydict(op) evaluate_varmap!(op, unknowns(sys)) for (k, v) in op + v === nothing && continue if is_parameter(prob, Initial(k)) setu(prob, Initial(k))(prob, v) else From db2ae7b10f66f926d1115c5a81f3b2746d498d69 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 11 Feb 2025 22:08:53 +0530 Subject: [PATCH 0912/2176] test: fix initialization of `linearization_dd` testset --- test/downstream/linearization_dd.jl | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/test/downstream/linearization_dd.jl b/test/downstream/linearization_dd.jl index 11dc65f619..fc5d3b6785 100644 --- a/test/downstream/linearization_dd.jl +++ b/test/downstream/linearization_dd.jl @@ -25,14 +25,11 @@ eqs = [connect(link1.TX1, cart.flange) connect(link1.TY1, fixed.flange)] @named model = ODESystem(eqs, t, [], []; systems = [link1, cart, force, fixed]) -def = ModelingToolkit.defaults(model) -def[cart.s] = 10 -def[cart.v] = 0 -def[link1.A] = -pi / 2 -def[link1.dA] = 0 lin_outputs = [cart.s, cart.v, link1.A, link1.dA] lin_inputs = [force.f.u] +# => nothing to remove extra defaults +op = Dict(cart.s => 10, cart.v => 0, link1.A => -pi/2, link1.dA => 0, force.f.u => 0, link1.x1 => nothing, link1.y1 => nothing, link1.x2 => nothing, link1.x_cm => nothing) @test_broken begin @info "named_ss" G = named_ss(model, lin_inputs, lin_outputs, allow_symbolic = true, op = def, @@ -46,7 +43,7 @@ lin_inputs = [force.f.u] @test minimum(abs, ps) < 1e-6 @test minimum(abs, complex(0, 1.3777260367206716) .- ps) < 1e-10 - lsys, syss = linearize(model, lin_inputs, lin_outputs, allow_symbolic = true, op = def, + lsys, syss = linearize(model, lin_inputs, lin_outputs, allow_symbolic = true, op = op, allow_input_derivatives = true, zero_dummy_der = true) lsyss, sysss = ModelingToolkit.linearize_symbolic(model, lin_inputs, lin_outputs; allow_input_derivatives = true) From f0f1da91ad5f88a90b6ac5a599fda41f72d88c8e Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 12 Feb 2025 12:17:22 +0530 Subject: [PATCH 0913/2176] fix: disable discovering defaults from metadata in `flatten(::ODESystem)` --- src/systems/diffeqs/odesystem.jl | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/src/systems/diffeqs/odesystem.jl b/src/systems/diffeqs/odesystem.jl index e53b9223e6..76fcb9b823 100644 --- a/src/systems/diffeqs/odesystem.jl +++ b/src/systems/diffeqs/odesystem.jl @@ -245,7 +245,8 @@ function ODESystem(deqs::AbstractVector{<:Equation}, iv, dvs, ps; metadata = nothing, gui_metadata = nothing, is_dde = nothing, - tstops = []) + tstops = [], + discover_from_metadata = true) name === nothing && throw(ArgumentError("The `name` keyword must be provided. Please consider using the `@named` macro")) @assert all(control -> any(isequal.(control, ps)), controls) "All controls must also be parameters." @@ -264,12 +265,16 @@ function ODESystem(deqs::AbstractVector{<:Equation}, iv, dvs, ps; defaults = Dict{Any, Any}(todict(defaults)) guesses = Dict{Any, Any}(todict(guesses)) var_to_name = Dict() - process_variables!(var_to_name, defaults, guesses, dvs′) - process_variables!(var_to_name, defaults, guesses, ps′) - process_variables!( - var_to_name, defaults, guesses, [eq.lhs for eq in parameter_dependencies]) - process_variables!( - var_to_name, defaults, guesses, [eq.rhs for eq in parameter_dependencies]) + let defaults = discover_from_metadata ? defaults : Dict(), + guesses = discover_from_metadata ? guesses : Dict() + + process_variables!(var_to_name, defaults, guesses, dvs′) + process_variables!(var_to_name, defaults, guesses, ps′) + process_variables!( + var_to_name, defaults, guesses, [eq.lhs for eq in parameter_dependencies]) + process_variables!( + var_to_name, defaults, guesses, [eq.rhs for eq in parameter_dependencies]) + end defaults = Dict{Any, Any}(value(k) => value(v) for (k, v) in pairs(defaults) if v !== nothing) guesses = Dict{Any, Any}(value(k) => value(v) @@ -375,7 +380,11 @@ function flatten(sys::ODESystem, noeqs = false) is_dde = is_dde(sys), tstops = symbolic_tstops(sys), metadata = get_metadata(sys), - checks = false) + checks = false, + # without this, any defaults/guesses obtained from metadata that were + # later removed by the user will be re-added. Right now, we just want to + # retain `defaults(sys)` as-is. + discover_from_metadata = false) end end From 648956d71ad9736c1815eb151d3a0b6db3669afe Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 12 Feb 2025 12:17:35 +0530 Subject: [PATCH 0914/2176] test: fix inversemodel testset --- test/downstream/inversemodel.jl | 36 +++++++++++++++------------------ 1 file changed, 16 insertions(+), 20 deletions(-) diff --git a/test/downstream/inversemodel.jl b/test/downstream/inversemodel.jl index 7102995b66..8573964551 100644 --- a/test/downstream/inversemodel.jl +++ b/test/downstream/inversemodel.jl @@ -68,18 +68,10 @@ begin Ftf = tf(1, [(100), 1])^3 Fss = ss(Ftf) - "Compute initial state that yields y0 as output" - function init_filter(y0) - (; A, B, C, D) = Fss - Fx0 = -A \ B * y0 - @assert C * Fx0≈[y0] "C*Fx0*y0 ≈ y0 failed, got $(C*Fx0*y0) ≈ $(y0)]" - Fx0 - end - # Create an MTK-compatible constructor function RefFilter(; name) sys = ODESystem(Fss; name) - delete!(defaults(sys), @nonamespace(sys.x)[1]) + empty!(ModelingToolkit.get_defaults(sys)) return sys end end @@ -140,7 +132,7 @@ op = Dict(D(cm.inverse_tank.xT) => 1, cm.tank.xc => 0.65) tspan = (0.0, 1000.0) # https://github.com/SciML/ModelingToolkit.jl/issues/2786 -prob = ODEProblem(ssys, op, tspan; build_initializeprob = false) +prob = ODEProblem(ssys, op, tspan) sol = solve(prob, Rodas5P()) @test SciMLBase.successful_retcode(sol) @@ -150,27 +142,29 @@ sol = solve(prob, Rodas5P()) @test sol(tspan[2], idxs = cm.tank.xc)≈getp(prob, cm.ref.k)(prob) atol=1e-2 # Test that the inverse model led to the correct reference -Sf, simplified_sys = Blocks.get_sensitivity_function(model, :y) # This should work without providing an operating opint containing a dummy derivative -x, _ = ModelingToolkit.get_u0_p(simplified_sys, op) -p = ModelingToolkit.MTKParameters(simplified_sys, op) +# we need to provide `op` so the initialization system knows what to hold constant +# the values don't matter +Sf, simplified_sys = Blocks.get_sensitivity_function(model, :y; op); # This should work without providing an operating opint containing a dummy derivative +x = state_values(Sf) +p = parameter_values(Sf) # If this somehow passes, mention it on # https://github.com/SciML/ModelingToolkit.jl/issues/2786 matrices1 = Sf(x, p, 0) -matrices2, _ = Blocks.get_sensitivity(model, :y; op) # Test that we get the same result when calling the higher-level API -@test_broken matrices1.f_x ≈ matrices2.A[1:7, 1:7] +matrices2, _ = Blocks.get_sensitivity(model, :y; op); # Test that we get the same result when calling the higher-level API +@test matrices1.f_x ≈ matrices2.A[1:7, 1:7] nsys = get_named_sensitivity(model, :y; op) # Test that we get the same result when calling an even higher-level API @test matrices2.A ≈ nsys.A # Test the same thing for comp sensitivities -Sf, simplified_sys = Blocks.get_comp_sensitivity_function(model, :y) # This should work without providing an operating opint containing a dummy derivative -x, _ = ModelingToolkit.get_u0_p(simplified_sys, op) -p = ModelingToolkit.MTKParameters(simplified_sys, op) +Sf, simplified_sys = Blocks.get_comp_sensitivity_function(model, :y; op); # This should work without providing an operating opint containing a dummy derivative +x = state_values(Sf) +p = parameter_values(Sf) # If this somehow passes, mention it on # https://github.com/SciML/ModelingToolkit.jl/issues/2786 matrices1 = Sf(x, p, 0) matrices2, _ = Blocks.get_comp_sensitivity(model, :y; op) # Test that we get the same result when calling the higher-level API -@test_broken matrices1.f_x ≈ matrices2.A[1:7, 1:7] +@test matrices1.f_x ≈ matrices2.A[1:7, 1:7] nsys = get_named_comp_sensitivity(model, :y; op) # Test that we get the same result when calling an even higher-level API @test matrices2.A ≈ nsys.A @@ -186,7 +180,9 @@ nsys = get_named_comp_sensitivity(model, :y; op) # Test that we get the same res ) output = :y - lin_fun, ssys = Blocks.get_sensitivity_function(model, output) + # we need to provide `op` so the initialization system knows which + # values to hold constant + lin_fun, ssys = Blocks.get_sensitivity_function(model, output; op = op1) matrices1 = linearize(ssys, lin_fun, op = op1) matrices2 = linearize(ssys, lin_fun, op = op2) S1f = ss(matrices1...) From 370343b3ab5d4d127df7b0427a8e07d20c509fdf Mon Sep 17 00:00:00 2001 From: Fredrik Bagge Carlson Date: Wed, 12 Feb 2025 07:08:03 +0100 Subject: [PATCH 0915/2176] update pendcart test --- test/downstream/linearization_dd.jl | 61 +++++++++++++++-------------- 1 file changed, 31 insertions(+), 30 deletions(-) diff --git a/test/downstream/linearization_dd.jl b/test/downstream/linearization_dd.jl index fc5d3b6785..c4076b6aad 100644 --- a/test/downstream/linearization_dd.jl +++ b/test/downstream/linearization_dd.jl @@ -29,33 +29,34 @@ lin_outputs = [cart.s, cart.v, link1.A, link1.dA] lin_inputs = [force.f.u] # => nothing to remove extra defaults -op = Dict(cart.s => 10, cart.v => 0, link1.A => -pi/2, link1.dA => 0, force.f.u => 0, link1.x1 => nothing, link1.y1 => nothing, link1.x2 => nothing, link1.x_cm => nothing) -@test_broken begin - @info "named_ss" - G = named_ss(model, lin_inputs, lin_outputs, allow_symbolic = true, op = def, - allow_input_derivatives = true, zero_dummy_der = true) - G = sminreal(G) - @info "minreal" - G = minreal(G) - @info "poles" - ps = poles(G) - - @test minimum(abs, ps) < 1e-6 - @test minimum(abs, complex(0, 1.3777260367206716) .- ps) < 1e-10 - - lsys, syss = linearize(model, lin_inputs, lin_outputs, allow_symbolic = true, op = op, - allow_input_derivatives = true, zero_dummy_der = true) - lsyss, sysss = ModelingToolkit.linearize_symbolic(model, lin_inputs, lin_outputs; - allow_input_derivatives = true) - - dummyder = setdiff(unknowns(sysss), unknowns(model)) - def = merge(ModelingToolkit.guesses(model), def, Dict(x => 0.0 for x in dummyder)) - def[link1.fy1] = -def[link1.g] * def[link1.m] - - @test substitute(lsyss.A, def) ≈ lsys.A - # We cannot pivot symbolically, so the part where a linear solve is required - # is not reliable. - @test substitute(lsyss.B, def)[1:6, 1] ≈ lsys.B[1:6, 1] - @test substitute(lsyss.C, def) ≈ lsys.C - @test substitute(lsyss.D, def) ≈ lsys.D -end +op = Dict(cart.s => 10, cart.v => 0, link1.A => -pi / 2, link1.dA => 0, force.f.u => 0, + link1.x1 => nothing, link1.y1 => nothing, link1.x2 => nothing, link1.x_cm => nothing) +@info "named_ss" +G = named_ss(model, lin_inputs, lin_outputs; allow_symbolic = true, op, + allow_input_derivatives = true, zero_dummy_der = true) +G = sminreal(G) +@info "minreal" +G = minreal(G) +@info "poles" +ps = poles(G) + +@test minimum(abs, ps) < 1e-6 +@test minimum(abs, complex(0, 1.3777260367206716) .- ps) < 1e-10 + +lsys, syss = linearize(model, lin_inputs, lin_outputs, allow_symbolic = true, op = op, + allow_input_derivatives = true, zero_dummy_der = true) +lsyss, sysss = ModelingToolkit.linearize_symbolic(model, lin_inputs, lin_outputs; + allow_input_derivatives = true) + +dummyder = setdiff(unknowns(sysss), unknowns(model)) +# op2 = merge(ModelingToolkit.guesses(model), op, Dict(x => 0.0 for x in dummyder)) +op2 = merge(ModelingToolkit.defaults(syss), op) +op2[link1.fy1] = -op2[link1.g] * op2[link1.m] +op2[cart.f] = 0 + +@test substitute(lsyss.A, op2) ≈ lsys.A +# We cannot pivot symbolically, so the part where a linear solve is required +# is not reliable. +@test substitute(lsyss.B, op2)[1:6, 1] ≈ lsys.B[1:6, 1] +@test substitute(lsyss.C, op2) ≈ lsys.C +@test substitute(lsyss.D, op2) ≈ lsys.D From 9ed0fe2c662e90926917e9142468f1d2de560cdd Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 12 Feb 2025 13:55:55 +0530 Subject: [PATCH 0916/2176] test: fix initialization of some tests --- test/input_output_handling.jl | 13 ++++++++----- test/split_parameters.jl | 5 +++-- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/test/input_output_handling.jl b/test/input_output_handling.jl index b1d68bc323..4191571ed8 100644 --- a/test/input_output_handling.jl +++ b/test/input_output_handling.jl @@ -128,16 +128,17 @@ eqs = [connect(torque.flange, inertia1.flange_a) connect(inertia2.flange_a, spring.flange_b, damper.flange_b) y ~ inertia2.w + torque.tau.u] model = ODESystem(eqs, t; systems = [torque, inertia1, inertia2, spring, damper], - name = :name) + name = :name, guesses = [spring.flange_a.phi => 0.0]) model_outputs = [inertia1.w, inertia2.w, inertia1.phi, inertia2.phi] model_inputs = [torque.tau.u] +op = Dict(torque.tau.u => 0.0) matrices, ssys = linearize( - model, model_inputs, model_outputs); + model, model_inputs, model_outputs; op); @test length(ModelingToolkit.outputs(ssys)) == 4 if VERSION >= v"1.8" # :opaque_closure not supported before let # Just to have a local scope for D - matrices, ssys = linearize(model, model_inputs, [y]) + matrices, ssys = linearize(model, model_inputs, [y]; op) A, B, C, D = matrices obsf = ModelingToolkit.build_explicit_observed_function(ssys, [y], @@ -321,7 +322,8 @@ function SystemModel(u = nothing; name = :model) u ]) end - ODESystem(eqs, t; systems = [torque, inertia1, inertia2, spring, damper], name) + ODESystem(eqs, t; systems = [torque, inertia1, inertia2, spring, damper], + name, guesses = [spring.flange_a.phi => 0.0]) end model = SystemModel() # Model with load disturbance @@ -410,7 +412,8 @@ matrices, ssys = linearize(augmented_sys, augmented_sys.u, augmented_sys.input.u[2], augmented_sys.d - ], outs) + ], outs; + op = [augmented_sys.u => 0.0, augmented_sys.input.u[2] => 0.0, augmented_sys.d => 0.0]) @test matrices.A ≈ [A [1; 0]; zeros(1, 2) -0.001] @test matrices.B == I @test matrices.C == [C zeros(2)] diff --git a/test/split_parameters.jl b/test/split_parameters.jl index b4b7314838..a38dd24c38 100644 --- a/test/split_parameters.jl +++ b/test/split_parameters.jl @@ -165,15 +165,16 @@ function SystemModel(u = nothing; name = :model) systems = [torque, inertia1, inertia2, spring, damper, u]) end ODESystem(eqs, t; systems = [torque, inertia1, inertia2, spring, damper], - name) + name, guesses = [spring.flange_a.phi => 0.0]) end model = SystemModel() # Model with load disturbance @named d = Step(start_time = 1.0, duration = 10.0, offset = 0.0, height = 1.0) # Disturbance model_outputs = [model.inertia1.w, model.inertia2.w, model.inertia1.phi, model.inertia2.phi] # This is the state realization we want to control inputs = [model.torque.tau.u] +op = [model.torque.tau.u => 0.0] matrices, ssys = ModelingToolkit.linearize( - wr(model), inputs, model_outputs) + wr(model), inputs, model_outputs; op) # Design state-feedback gain using LQR # Define cost matrices From b664e1cd9381f09e36a13bb173e5d108a94d0f78 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 12 Feb 2025 14:22:27 +0530 Subject: [PATCH 0917/2176] fix: unhack observed in `process_SciMLProblem` --- 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 87aafb8f73..be05d8fcff 100644 --- a/src/systems/problem_utils.jl +++ b/src/systems/problem_utils.jl @@ -756,7 +756,12 @@ function process_SciMLProblem( cmap, cs = get_cmap(sys) kwargs = NamedTuple(kwargs) - is_time_dependent(sys) || add_observed!(sys, u0map) + if eltype(eqs) <: Equation + obs, eqs = unhack_observed(observed(sys), eqs) + else + obs, _ = unhack_observed(observed(sys), Equation[x for x in eqs if x isa Equation]) + end + is_time_dependent(sys) || add_observed_equations!(u0map, obs) op, missing_unknowns, missing_pars = build_operating_point!(sys, u0map, pmap, defs, cmap, dvs, ps) @@ -777,7 +782,7 @@ function process_SciMLProblem( op[iv] = t end - is_time_dependent(sys) && add_observed!(sys, op) + is_time_dependent(sys) && add_observed_equations!(op, obs) add_parameter_dependencies!(sys, op) if warn_cyclic_dependency From c73b0a261daf2761ba5ef3f7c83f7bb3f5583fd9 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 12 Feb 2025 14:22:47 +0530 Subject: [PATCH 0918/2176] test: test mutation of `Initial` parameters --- test/initializationsystem.jl | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/test/initializationsystem.jl b/test/initializationsystem.jl index 3a208bb940..4abefb00ee 100644 --- a/test/initializationsystem.jl +++ b/test/initializationsystem.jl @@ -1312,3 +1312,22 @@ end sol = solve(prob, Tsit5()) @test sol.ps[p] ≈ [2.0, 4.0] end + +@testset "Issue#3318: Mutating `Initial` parameters works" begin + @variables x(t) y(t)[1:2] [guess = ones(2)] + @parameters p[1:2, 1:2] + @mtkbuild sys = ODESystem( + [D(x) ~ x, D(y) ~ p * y], t; initialization_eqs = [x^2 + y[1]^2 + y[2]^2 ~ 4]) + prob = ODEProblem(sys, [x => 1.0, y[1] => 1], (0.0, 1.0), [p => 2ones(2, 2)]) + integ = init(prob, Tsit5()) + @test integ[x] ≈ 1.0 + @test integ[y] ≈ [1.0, sqrt(2.0)] + prob.ps[Initial(x)] = 0.5 + integ = init(prob, Tsit5()) + @test integ[x] ≈ 0.5 + @test integ[y] ≈ [1.0, sqrt(2.75)] + prob.ps[Initial(y[1])] = 0.5 + integ = init(prob, Tsit5()) + @test integ[x] ≈ 0.5 + @test integ[y]≈[0.5, sqrt(3.5)] atol=1e-6 +end From 472454094522c5358fce06385a0dbb5f11b58b4a Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 12 Feb 2025 14:37:00 +0530 Subject: [PATCH 0919/2176] test: test Issue#3342 --- test/initializationsystem.jl | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/test/initializationsystem.jl b/test/initializationsystem.jl index 4abefb00ee..392516cf8b 100644 --- a/test/initializationsystem.jl +++ b/test/initializationsystem.jl @@ -1331,3 +1331,26 @@ end @test integ[x] ≈ 0.5 @test integ[y]≈[0.5, sqrt(3.5)] atol=1e-6 end + +@testset "Issue#3342" begin + @variables x(t) y(t) + stop!(integrator, _, _, _) = terminate!(integrator) + @named sys = ODESystem([D(x) ~ 1.0 + D(y) ~ 1.0], t; initialization_eqs = [ + y ~ 0.0 + ], + continuous_events = [ + [y ~ 0.5] => (stop!, [y], [], [], nothing) + ]) + sys = structural_simplify(sys) + prob0 = ODEProblem(sys, [x => NaN], (0.0, 1.0), []) + + # final_x(x0) is equivalent to x0 + 0.5 + function final_x(x0) + prob = remake(prob0; u0 = [x => x0]) + sol = solve(prob) + return sol[x][end] + end + @test final_x(0.3) ≈ 0.8 # should be 0.8 + @test ForwardDiff.derivative(final_x, 0.3) ≈ 1.0 +end From ca1f389e5c6204ef0a10c06c5efcaabd7d2ad44a Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 12 Feb 2025 14:45:09 +0530 Subject: [PATCH 0920/2176] test: test initialization for unsimplified systems --- test/initializationsystem.jl | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/test/initializationsystem.jl b/test/initializationsystem.jl index 392516cf8b..0990101069 100644 --- a/test/initializationsystem.jl +++ b/test/initializationsystem.jl @@ -1354,3 +1354,10 @@ end @test final_x(0.3) ≈ 0.8 # should be 0.8 @test ForwardDiff.derivative(final_x, 0.3) ≈ 1.0 end + +@testset "Issue#3330: Initialization for unsimplified systems" begin + @variables x(t) [guess = 1.0] + @mtkbuild sys = ODESystem(D(x) ~ x, t; initialization_eqs = [x^2 ~ 4]) + prob = ODEProblem(sys, [], (0.0, 1.0)) + @test prob.f.initialization_data !== nothing +end From 6f38a1382de62cc7190f432352b01826406b7f6c Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 12 Feb 2025 14:50:41 +0530 Subject: [PATCH 0921/2176] fix: check `hasname` for parameters in `propertynames` --- src/systems/abstractsystem.jl | 1 + 1 file changed, 1 insertion(+) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index 8093ca5a13..9f8e292a81 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -956,6 +956,7 @@ function Base.propertynames(sys::AbstractSystem; private = false) push!(names, getname(s)) end has_ps(sys) && for s in get_ps(sys) + hasname(s) || continue push!(names, getname(s)) end has_observed(sys) && for s in get_observed(sys) From 158f3f899c72f5d77af57c0dad06bb5939ef174d Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 12 Feb 2025 15:12:27 +0530 Subject: [PATCH 0922/2176] build: bump SciMLBase compat --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index ac865063f2..ec7939a58c 100644 --- a/Project.toml +++ b/Project.toml @@ -135,7 +135,7 @@ RecursiveArrayTools = "3.26" Reexport = "0.2, 1" RuntimeGeneratedFunctions = "0.5.9" SCCNonlinearSolve = "1.0.0" -SciMLBase = "2.72.1" +SciMLBase = "2.73" SciMLStructures = "1.0" Serialization = "1" Setfield = "0.7, 0.8, 1" From c12374f3dcbaaa01d679a092d8be956cca10e3c0 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 12 Feb 2025 21:43:01 +0530 Subject: [PATCH 0923/2176] docs: add documentation for `Initial` parameters --- docs/src/tutorials/initialization.md | 70 ++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) diff --git a/docs/src/tutorials/initialization.md b/docs/src/tutorials/initialization.md index 26dbb58899..27eb2ae28d 100644 --- a/docs/src/tutorials/initialization.md +++ b/docs/src/tutorials/initialization.md @@ -203,6 +203,73 @@ long enough you will see that `λ = 0` is required for this equation, but since problem constructor. Additionally, any warning about not being fully determined can be suppressed via passing `warn_initialize_determined = false`. +## Constant constraints in initialization + +Consider the pendulum system again: + +```@repl init +equations(pend) +observed(pend) +``` + +Suppose we want to solve the same system with multiple different initial +y-velocities from a given position. + +```@example init +prob = ODEProblem( + pend, [x => 1, D(y) => 0], (0.0, 1.5), [g => 1], guesses = [λ => 0, y => 1, x => 1]) +sol1 = solve(prob, Rodas5P()) +``` + +```@example init +sol1[D(y), 1] +``` + +Repeatedly re-creating the `ODEProblem` with different values of `D(y)` and `x` or +repeatedly calling `remake` is slow. Instead, for any `variable => constant` constraint +in the `ODEProblem` initialization (whether provided to the `ODEProblem` constructor or +a default value) we can update the `constant` value. ModelingToolkit refers to these +values using the `Initial` operator. For example: + +```@example init +prob.ps[[Initial(x), Initial(D(y))]] +``` + +To solve with a different starting y-velocity, we can simply do + +```@example init +prob.ps[Initial(D(y))] = -0.1 +sol2 = solve(prob, Rodas5P()) +``` + +```@example init +sol2[D(y), 1] +``` + +Note that this _only_ applies for constant constraints for the current ODEProblem. +For example, `D(x)` does not have a constant constraint - it is solved for by +initialization. Thus, mutating `Initial(D(x))` does not have any effect: + +```@repl init +sol2[D(x), 1] +prob.ps[Initial(D(x))] = 1.0 +sol3 = solve(prob, Rodas5P()) +sol3[D(x), 1] +``` + +To enforce this constraint, we would have to `remake` the problem (or construct a new one). + +```@repl init +prob2 = remake(prob; u0 = [y => 0.0, D(x) => 0.0, x => nothing, D(y) => nothing]); +sol4 = solve(prob2, Rodas5P()) +sol4[D(x), 1] +``` + +Note the need to provide `x => nothing, D(y) => nothing` to override the previously +provided initial conditions. Since `remake` is a partial update, the constraints provided +to it are merged with the ones already present in the problem. Existing constraints can be +removed by providing a value of `nothing`. + ## Initialization of parameters Parameters may also be treated as unknowns in the initialization system. Doing so works @@ -231,6 +298,9 @@ constraints provided to it. The new values will be combined with the original variable-value mapping provided to `ODEProblem` and used to construct the initialization problem. +The variable on the left hand side of all parameter dependencies also has an `Initial` +variant, which is used if a constant constraint is provided for the variable. + ### Parameter initialization by example Consider the following system, where the sum of two unknowns is a constant parameter From 5d190f6239871024e58ab8a3ee633095f1bf7803 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 12 Feb 2025 21:54:28 +0530 Subject: [PATCH 0924/2176] fix: better handle overrides for differentiated variables in `remake` --- src/systems/nonlinear/initializesystem.jl | 1 + 1 file changed, 1 insertion(+) diff --git a/src/systems/nonlinear/initializesystem.jl b/src/systems/nonlinear/initializesystem.jl index da66ce66d7..77138c9887 100644 --- a/src/systems/nonlinear/initializesystem.jl +++ b/src/systems/nonlinear/initializesystem.jl @@ -428,6 +428,7 @@ function SciMLBase.remake_initialization_data( ps = parameters(sys) u0map = to_varmap(u0, dvs) symbols_to_symbolics!(sys, u0map) + add_toterms!(u0map) pmap = to_varmap(p, ps) symbols_to_symbolics!(sys, pmap) guesses = Dict() From 7baf7881852c7b888b3e3eaceced5fb4be31d904 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 13 Feb 2025 12:54:24 +0530 Subject: [PATCH 0925/2176] docs: import `DynamicQuantities` in doc example requiring it --- docs/src/basics/Variable_metadata.md | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/src/basics/Variable_metadata.md b/docs/src/basics/Variable_metadata.md index b2cc472f2f..43d0a7f2ea 100644 --- a/docs/src/basics/Variable_metadata.md +++ b/docs/src/basics/Variable_metadata.md @@ -201,6 +201,7 @@ state_priority(important_dof) Units for variables can be designated using symbolic metadata. For more information, please see the [model validation and units](@ref units) section of the docs. Note that `getunit` is not equivalent to `get_unit` - the former is a metadata getter for individual variables (and is provided so the same interface function for `unit` exists like other metadata), while the latter is used to handle more general symbolic expressions. ```@example metadata +using DynamicQuantities @variables speed [unit = u"m/s"] hasunit(speed) ``` From dfa4db8263225e24ec7de06b3516b18038c1fb28 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 13 Feb 2025 12:54:50 +0530 Subject: [PATCH 0926/2176] docs: update remake doc page to account for extra tunables --- docs/src/examples/remake.md | 55 +++++++++++++------------------------ 1 file changed, 19 insertions(+), 36 deletions(-) diff --git a/docs/src/examples/remake.md b/docs/src/examples/remake.md index f4396ef7a7..47ac02ee0a 100644 --- a/docs/src/examples/remake.md +++ b/docs/src/examples/remake.md @@ -45,12 +45,22 @@ parameters to optimize. ```@example Remake using SymbolicIndexingInterface: parameter_values, state_values -using SciMLStructures: Tunable, replace, replace! +using SciMLStructures: Tunable, canonicalize, replace, replace! +using PreallocationTools function loss(x, p) odeprob = p[1] # ODEProblem stored as parameters to avoid using global variables ps = parameter_values(odeprob) # obtain the parameter object from the problem - ps = replace(Tunable(), ps, x) # create a copy with the values passed to the loss function + diffcache = p[5] + # get an appropriately typed preallocated buffer to store the `x` values in + buffer = get_tmp(diffcache, x) + # copy the current values to this buffer + copyto!(buffer, canonicalize(Tunable(), ps)[1]) + # create a copy of the parameter object with the buffer + ps = replace(Tunable(), ps, buffer) + # set the updated values in the parameter object + setter = p[4] + setter(ps, x) # remake the problem, passing in our new parameter object newprob = remake(odeprob; p = ps) timesteps = p[2] @@ -81,49 +91,22 @@ We can perform the optimization as below: ```@example Remake using Optimization using OptimizationOptimJL +using SymbolicIndexingInterface # manually create an OptimizationFunction to ensure usage of `ForwardDiff`, which will # require changing the types of parameters from `Float64` to `ForwardDiff.Dual` optfn = OptimizationFunction(loss, Optimization.AutoForwardDiff()) +# function to set the parameters we are optimizing +setter = setp(odeprob, [α, β, γ, δ]) +# `DiffCache` to avoid allocations +diffcache = DiffCache(canonicalize(Tunable(), parameter_values(odeprob))[1]) # parameter object is a tuple, to store differently typed objects together optprob = OptimizationProblem( - optfn, rand(4), (odeprob, timesteps, data), lb = 0.1zeros(4), ub = 3ones(4)) + optfn, rand(4), (odeprob, timesteps, data, setter, diffcache), + lb = 0.1zeros(4), ub = 3ones(4)) sol = solve(optprob, BFGS()) ``` -To identify which values correspond to which parameters, we can `replace!` them into the -`ODEProblem`: - -```@example Remake -replace!(Tunable(), parameter_values(odeprob), sol.u) -odeprob.ps[[α, β, γ, δ]] -``` - -`replace!` operates in-place, so the values being replaced must be of the same type as those -stored in the parameter object, or convertible to that type. For demonstration purposes, we -can construct a loss function that uses `replace!`, and calculate gradients using -`AutoFiniteDiff` rather than `AutoForwardDiff`. - -```@example Remake -function loss2(x, p) - odeprob = p[1] # ODEProblem stored as parameters to avoid using global variables - newprob = remake(odeprob) # copy the problem with `remake` - # update the parameter values in-place - replace!(Tunable(), parameter_values(newprob), x) - timesteps = p[2] - sol = solve(newprob, AutoTsit5(Rosenbrock23()); saveat = timesteps) - truth = p[3] - data = Array(sol) - return sum((truth .- data) .^ 2) / length(truth) -end - -# use finite-differencing to calculate derivatives -optfn2 = OptimizationFunction(loss2, Optimization.AutoFiniteDiff()) -optprob2 = OptimizationProblem( - optfn2, rand(4), (odeprob, timesteps, data), lb = 0.1zeros(4), ub = 3ones(4)) -sol = solve(optprob2, BFGS()) -``` - # Re-creating the problem There are multiple ways to re-create a problem with new state/parameter values. We will go From c5b9fe0aba10cbba84a21b2c7476964d921db2b0 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 13 Feb 2025 12:55:26 +0530 Subject: [PATCH 0927/2176] fix: handle `state_priority` of wrapped variables --- src/variables.jl | 1 + 1 file changed, 1 insertion(+) diff --git a/src/variables.jl b/src/variables.jl index 0068055832..83f0681a09 100644 --- a/src/variables.jl +++ b/src/variables.jl @@ -127,6 +127,7 @@ isoutput(x) = isvarkind(VariableOutput, x) # irreducibility is independent from IO. isirreducible(x) = isvarkind(VariableIrreducible, x) setirreducible(x, v::Bool) = setmetadata(x, VariableIrreducible, v) +state_priority(x::Union{Num, Symbolics.Arr}) = state_priority(unwrap(x)) state_priority(x) = convert(Float64, getmetadata(x, VariableStatePriority, 0.0))::Float64 function default_toterm(x) From 1e9d28fbf91663320d9358bbeb69df09a5e40e60 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 13 Feb 2025 12:55:36 +0530 Subject: [PATCH 0928/2176] test: add guess for FMI test --- test/fmi/fmi.jl | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/fmi/fmi.jl b/test/fmi/fmi.jl index 39f624f616..a311bf44d2 100644 --- a/test/fmi/fmi.jl +++ b/test/fmi/fmi.jl @@ -235,7 +235,8 @@ end t; systems = [adder1, adder2]) prob = ODEProblem( - sys, [adder1.c => 1.0, adder2.c => 1.0, adder1.a => 2.0], (0.0, 1.0)) + sys, [adder1.c => 1.0, adder2.c => 1.0, adder1.a => 2.0], + (0.0, 1.0); guesses = [adder2.a => 0.0]) return sys, prob end @named adder1 = SimpleAdder() From 6bc28a8a8bb645b3a3819207e48e92f070f2f987 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 13 Feb 2025 12:56:07 +0530 Subject: [PATCH 0929/2176] fix: handle non-standard array indexes in `parameter_index` --- src/ModelingToolkit.jl | 1 + src/systems/index_cache.jl | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/src/ModelingToolkit.jl b/src/ModelingToolkit.jl index 6501a4e026..eaa7af85d2 100644 --- a/src/ModelingToolkit.jl +++ b/src/ModelingToolkit.jl @@ -56,6 +56,7 @@ using RecursiveArrayTools import Graphs: SimpleDiGraph, add_edge!, incidence_matrix import BlockArrays: BlockArray, BlockedArray, Block, blocksize, blocksizes, blockpush!, undef_blocks, blocks +using OffsetArrays: Origin import CommonSolve import EnumX diff --git a/src/systems/index_cache.jl b/src/systems/index_cache.jl index a956e17313..d71352da9f 100644 --- a/src/systems/index_cache.jl +++ b/src/systems/index_cache.jl @@ -397,7 +397,8 @@ function SymbolicIndexingInterface.parameter_index(ic::IndexCache, sym) pidx = parameter_index(ic, args[1]) pidx === nothing && return nothing if pidx.portion == SciMLStructures.Tunable() - ParameterIndex(pidx.portion, reshape(pidx.idx, size(args[1]))[args[2:end]...], + ParameterIndex(pidx.portion, + Origin(first.(axes((args[1]))))(reshape(pidx.idx, size(args[1])))[args[2:end]...], pidx.validate_size) else ParameterIndex(pidx.portion, (pidx.idx..., args[2:end]...), pidx.validate_size) From 3e5c436509f2963e1746cdaba564aaacbf92c92e Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 13 Feb 2025 12:56:46 +0530 Subject: [PATCH 0930/2176] fix: better handle scalarized array parameters with unscalarized defaults in `is_parameter_solvable` --- src/systems/nonlinear/initializesystem.jl | 25 ++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/src/systems/nonlinear/initializesystem.jl b/src/systems/nonlinear/initializesystem.jl index 77138c9887..07f745de18 100644 --- a/src/systems/nonlinear/initializesystem.jl +++ b/src/systems/nonlinear/initializesystem.jl @@ -355,16 +355,27 @@ function get_possibly_array_fallback_singletons(varmap, p) if haskey(varmap, p) return varmap[p] end - symbolic_type(p) == ArraySymbolic() || return nothing - scal = collect(p) - if all(x -> haskey(varmap, x), scal) - res = [varmap[x] for x in scal] - if any(x -> x === nothing, res) + if symbolic_type(p) == ArraySymbolic() + scal = collect(p) + if all(x -> haskey(varmap, x), scal) + res = [varmap[x] for x in scal] + if any(x -> x === nothing, res) + return nothing + elseif any(x -> x === missing, res) + return missing + end + return res + end + elseif iscall(p) && operation(p) == getindex + arrp = arguments(p)[1] + val = get_possibly_array_fallback_singletons(varmap, arrp) + if val === nothing return nothing - elseif any(x -> x === missing, res) + elseif val === missing return missing + else + return val end - return res end return nothing end From 85f98802048ded584e10dad034958776c3f50238 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 13 Feb 2025 13:04:27 +0530 Subject: [PATCH 0931/2176] test: move `generate_custom_function` tests to `InterfaceII` --- test/runtests.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/runtests.jl b/test/runtests.jl index 9537b1b44e..5b3c0f5a8b 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -60,7 +60,6 @@ end @safetestset "FuncAffect Test" include("funcaffect.jl") @safetestset "Constants Test" include("constants.jl") @safetestset "Parameter Dependency Test" include("parameter_dependencies.jl") - @safetestset "Generate Custom Function Test" include("generate_custom_function.jl") @safetestset "Equation Type Accessors Test" include("equation_type_accessors.jl") @safetestset "Equations with complex values" include("complex.jl") end @@ -75,6 +74,7 @@ end if GROUP == "All" || GROUP == "InterfaceII" @testset "InterfaceII" begin + @safetestset "Generate Custom Function Test" include("generate_custom_function.jl") @safetestset "IndexCache Test" include("index_cache.jl") @safetestset "Variable Utils Test" include("variable_utils.jl") @safetestset "Variable Metadata Test" include("test_variable_metadata.jl") From 471e77c1dae4ac7d98d02244f733fe3ef3075a39 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 13 Feb 2025 13:05:28 +0530 Subject: [PATCH 0932/2176] test: rename `generate_custom_function` testset to `code_generation` --- test/code_generation.jl | 56 ++++++++++++++++++++++++++++++++ test/generate_custom_function.jl | 53 ------------------------------ test/runtests.jl | 2 +- 3 files changed, 57 insertions(+), 54 deletions(-) create mode 100644 test/code_generation.jl delete mode 100644 test/generate_custom_function.jl diff --git a/test/code_generation.jl b/test/code_generation.jl new file mode 100644 index 0000000000..cc7a82a561 --- /dev/null +++ b/test/code_generation.jl @@ -0,0 +1,56 @@ +using ModelingToolkit +using ModelingToolkit: t_nounits as t, D_nounits as D + +@testset "`generate_custom_function`" begin + @variables x(t) y(t)[1:3] + @parameters p1=1.0 p2[1:3]=[1.0, 2.0, 3.0] p3::Int=1 p4::Bool=false + + sys = complete(ODESystem(Equation[], t, [x; y], [p1, p2, p3, p4]; name = :sys)) + u0 = [1.0, 2.0, 3.0, 4.0] + p = ModelingToolkit.MTKParameters(sys, []) + + fn1 = generate_custom_function( + sys, x + y[1] + p1 + p2[1] + p3 * t; expression = Val(false)) + @test fn1(u0, p, 0.0) == 5.0 + + fn2 = generate_custom_function( + sys, x + y[1] + p1 + p2[1] + p3 * t, [x], [p1, p2, p3]; expression = Val(false)) + @test fn1(u0, p, 0.0) == 5.0 + + fn3_oop, fn3_iip = generate_custom_function( + sys, [x + y[2], y[3] + p2[2], p1 + p3, 3t]; expression = Val(false)) + + buffer = zeros(4) + fn3_iip(buffer, u0, p, 1.0) + @test buffer == [4.0, 6.0, 2.0, 3.0] + @test fn3_oop(u0, p, 1.0) == [4.0, 6.0, 2.0, 3.0] + + fn4 = generate_custom_function(sys, ifelse(p4, p1, p2[2]); expression = Val(false)) + @test fn4(u0, p, 1.0) == 2.0 + fn5 = generate_custom_function(sys, ifelse(!p4, p1, p2[2]); expression = Val(false)) + @test fn5(u0, p, 1.0) == 1.0 + + @variables x y[1:3] + sys = complete(NonlinearSystem(Equation[], [x; y], [p1, p2, p3, p4]; name = :sys)) + p = MTKParameters(sys, []) + + fn1 = generate_custom_function(sys, x + y[1] + p1 + p2[1] + p3; expression = Val(false)) + @test fn1(u0, p) == 6.0 + + fn2 = generate_custom_function( + sys, x + y[1] + p1 + p2[1] + p3, [x], [p1, p2, p3]; expression = Val(false)) + @test fn1(u0, p) == 6.0 + + fn3_oop, fn3_iip = generate_custom_function( + sys, [x + y[2], y[3] + p2[2], p1 + p3]; expression = Val(false)) + + buffer = zeros(3) + fn3_iip(buffer, u0, p) + @test buffer == [4.0, 6.0, 2.0] + @test fn3_oop(u0, p, 1.0) == [4.0, 6.0, 2.0] + + fn4 = generate_custom_function(sys, ifelse(p4, p1, p2[2]); expression = Val(false)) + @test fn4(u0, p, 1.0) == 2.0 + fn5 = generate_custom_function(sys, ifelse(!p4, p1, p2[2]); expression = Val(false)) + @test fn5(u0, p, 1.0) == 1.0 +end diff --git a/test/generate_custom_function.jl b/test/generate_custom_function.jl deleted file mode 100644 index ea1e429e30..0000000000 --- a/test/generate_custom_function.jl +++ /dev/null @@ -1,53 +0,0 @@ -using ModelingToolkit -using ModelingToolkit: t_nounits as t, D_nounits as D - -@variables x(t) y(t)[1:3] -@parameters p1=1.0 p2[1:3]=[1.0, 2.0, 3.0] p3::Int=1 p4::Bool=false - -sys = complete(ODESystem(Equation[], t, [x; y], [p1, p2, p3, p4]; name = :sys)) -u0 = [1.0, 2.0, 3.0, 4.0] -p = ModelingToolkit.MTKParameters(sys, []) - -fn1 = generate_custom_function(sys, x + y[1] + p1 + p2[1] + p3 * t; expression = Val(false)) -@test fn1(u0, p, 0.0) == 5.0 - -fn2 = generate_custom_function( - sys, x + y[1] + p1 + p2[1] + p3 * t, [x], [p1, p2, p3]; expression = Val(false)) -@test fn1(u0, p, 0.0) == 5.0 - -fn3_oop, fn3_iip = generate_custom_function( - sys, [x + y[2], y[3] + p2[2], p1 + p3, 3t]; expression = Val(false)) - -buffer = zeros(4) -fn3_iip(buffer, u0, p, 1.0) -@test buffer == [4.0, 6.0, 2.0, 3.0] -@test fn3_oop(u0, p, 1.0) == [4.0, 6.0, 2.0, 3.0] - -fn4 = generate_custom_function(sys, ifelse(p4, p1, p2[2]); expression = Val(false)) -@test fn4(u0, p, 1.0) == 2.0 -fn5 = generate_custom_function(sys, ifelse(!p4, p1, p2[2]); expression = Val(false)) -@test fn5(u0, p, 1.0) == 1.0 - -@variables x y[1:3] -sys = complete(NonlinearSystem(Equation[], [x; y], [p1, p2, p3, p4]; name = :sys)) -p = MTKParameters(sys, []) - -fn1 = generate_custom_function(sys, x + y[1] + p1 + p2[1] + p3; expression = Val(false)) -@test fn1(u0, p) == 6.0 - -fn2 = generate_custom_function( - sys, x + y[1] + p1 + p2[1] + p3, [x], [p1, p2, p3]; expression = Val(false)) -@test fn1(u0, p) == 6.0 - -fn3_oop, fn3_iip = generate_custom_function( - sys, [x + y[2], y[3] + p2[2], p1 + p3]; expression = Val(false)) - -buffer = zeros(3) -fn3_iip(buffer, u0, p) -@test buffer == [4.0, 6.0, 2.0] -@test fn3_oop(u0, p, 1.0) == [4.0, 6.0, 2.0] - -fn4 = generate_custom_function(sys, ifelse(p4, p1, p2[2]); expression = Val(false)) -@test fn4(u0, p, 1.0) == 2.0 -fn5 = generate_custom_function(sys, ifelse(!p4, p1, p2[2]); expression = Val(false)) -@test fn5(u0, p, 1.0) == 1.0 diff --git a/test/runtests.jl b/test/runtests.jl index 5b3c0f5a8b..966b02cacb 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -74,7 +74,7 @@ end if GROUP == "All" || GROUP == "InterfaceII" @testset "InterfaceII" begin - @safetestset "Generate Custom Function Test" include("generate_custom_function.jl") + @safetestset "Code Generation Test" include("code_generation.jl") @safetestset "IndexCache Test" include("index_cache.jl") @safetestset "Variable Utils Test" include("variable_utils.jl") @safetestset "Variable Metadata Test" include("test_variable_metadata.jl") From a50081d29d0fe59ab0982bab024065db19a63ccb Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 13 Feb 2025 13:15:56 +0530 Subject: [PATCH 0933/2176] fix: handle non-standard array indexes in codegen --- src/systems/codegen_utils.jl | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/src/systems/codegen_utils.jl b/src/systems/codegen_utils.jl index bee7564a4d..1c9e6ce53c 100644 --- a/src/systems/codegen_utils.jl +++ b/src/systems/codegen_utils.jl @@ -34,7 +34,7 @@ function array_variable_assignments(args...) # get and/or construct the buffer storing indexes idxbuffer = get!( () -> map(Returns((0, 0)), eachindex(arrvar)), var_to_arridxs, arrvar) - idxbuffer[arguments(var)[2:end]...] = (i, j) + Origin(first.(axes(arrvar))...)(idxbuffer)[arguments(var)[2:end]...] = (i, j) end end @@ -59,18 +59,22 @@ function array_variable_assignments(args...) idxs = SArray{Tuple{size(idxs)...}}(idxs) end # view and reshape - push!(assignments, - arrvar ← - term(reshape, term(view, generated_argument_name(buffer_idx), idxs), - size(arrvar))) + + expr = term(reshape, term(view, generated_argument_name(buffer_idx), idxs), + size(arrvar)) else elems = map(idxs) do idx i, j = idx term(getindex, generated_argument_name(i), j) end - # use `MakeArray` and generate a stack-allocated array - push!(assignments, arrvar ← MakeArray(elems, SArray)) + # use `MakeArray` syntax and generate a stack-allocated array + expr = term(SymbolicUtils.Code.create_array, SArray, nothing, + Val(ndims(arrvar)), Val(length(arrvar)), elems...) + end + if any(x -> !isone(first(x)), axes(arrvar)) + expr = term(Origin(first.(axes(arrvar))...), expr) end + push!(assignments, arrvar ← expr) end return assignments From 4d9552c33ef7477066fb800e628427927b8c1c55 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 13 Feb 2025 13:16:21 +0530 Subject: [PATCH 0934/2176] test: test code generation for non-standard array indexes --- test/code_generation.jl | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/test/code_generation.jl b/test/code_generation.jl index cc7a82a561..cf3d660b81 100644 --- a/test/code_generation.jl +++ b/test/code_generation.jl @@ -1,4 +1,4 @@ -using ModelingToolkit +using ModelingToolkit, OrdinaryDiffEq, SymbolicIndexingInterface using ModelingToolkit: t_nounits as t, D_nounits as D @testset "`generate_custom_function`" begin @@ -54,3 +54,27 @@ using ModelingToolkit: t_nounits as t, D_nounits as D fn5 = generate_custom_function(sys, ifelse(!p4, p1, p2[2]); expression = Val(false)) @test fn5(u0, p, 1.0) == 1.0 end + +@testset "Non-standard array variables" begin + @variables x(t) + @parameters p[0:2] (f::Function)(..) + @mtkbuild sys = ODESystem(D(x) ~ p[0] * x + p[1] * t + p[2] + f(p), t) + prob = ODEProblem(sys, [x => 1.0], (0.0, 1.0), [p => [1.0, 2.0, 3.0], f => sum]) + @test prob.ps[p] == [1.0, 2.0, 3.0] + @test prob.ps[p[0]] == 1.0 + sol = solve(prob, Tsit5()) + @test SciMLBase.successful_retcode(sol) + + @testset "Array split across buffers" begin + @variables x(t)[0:2] + @parameters p[1:2] (f::Function)(..) + @named sys = ODESystem( + [D(x[0]) ~ p[1] * x[0] + x[2], D(x[1]) ~ p[2] * f(x) + x[2]], t) + sys, = structural_simplify(sys, ([x[2]], [])) + @test is_parameter(sys, x[2]) + prob = ODEProblem(sys, [x[0] => 1.0, x[1] => 1.0], (0.0, 1.0), + [p => ones(2), f => sum, x[2] => 2.0]) + sol = solve(prob, Tsit5()) + @test SciMLBase.successful_retcode(sol) + end +end From c78e9f0089acc4db816b70058f42330693c60658 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 13 Feb 2025 13:22:26 +0530 Subject: [PATCH 0935/2176] refactor: remove dead code --- src/systems/abstractsystem.jl | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index 9f8e292a81..8f46570b09 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -713,14 +713,8 @@ function add_initialization_parameters(sys::AbstractSystem) end all_initialvars = collect(all_initialvars) initials = map(Initial(), all_initialvars) - # existing_initials = filter( - # x -> iscall(x) && (operation(x) isa Initial), parameters(sys)) - existing_initials = [] - @set! sys.ps = unique!([setdiff(get_ps(sys), existing_initials); initials]) + @set! sys.ps = unique!([get_ps(sys); initials]) defs = copy(get_defaults(sys)) - for x in existing_initials - delete!(defs, x) - end for ivar in initials if symbolic_type(ivar) == ScalarSymbolic() defs[ivar] = zero_var(ivar) From 2748f7f2e42e136e97c45a24b91aa3180a88a326 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 13 Feb 2025 13:22:48 +0530 Subject: [PATCH 0936/2176] docs: add documentation for `Initial` operator --- src/systems/abstractsystem.jl | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index 8f46570b09..41c487dadc 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -650,6 +650,12 @@ function isscheduled(sys::AbstractSystem) end end +""" + Initial(x) + +The `Initial` operator. Used by initializaton to store constant constraints on variables +of a system. See the documentation section on initialization for more information. +""" struct Initial <: Symbolics.Operator end Initial(x) = Initial()(x) SymbolicUtils.promote_symtype(::Type{Initial}, T) = T @@ -660,23 +666,31 @@ input_timedomain(::Initial, _ = nothing) = Continuous() output_timedomain(::Initial, _ = nothing) = Continuous() function (f::Initial)(x) + # wrap output if wrapped input iw = Symbolics.iswrapped(x) x = unwrap(x) + # non-symbolic values don't change if symbolic_type(x) == NotSymbolic() return x end + # differential variables are default-toterm-ed if iscall(x) && operation(x) isa Differential x = default_toterm(x) end + # don't double wrap iscall(x) && operation(x) isa Initial && return x result = if symbolic_type(x) == ArraySymbolic() + # create an array for `Initial(array)` Symbolics.array_term(f, toparam(x)) elseif iscall(x) && operation(x) == getindex + # instead of `Initial(x[1])` create `Initial(x)[1]` + # which allows parameter indexing to handle this case automatically. arr = arguments(x)[1] term(getindex, f(toparam(arr)), arguments(x)[2:end]...) else term(f, toparam(x)) end + # the result should be a parameter result = toparam(result) if iw result = wrap(result) @@ -684,6 +698,7 @@ function (f::Initial)(x) return result end +# This is required so `fast_substitute` works function SymbolicUtils.maketerm(::Type{<:BasicSymbolic}, ::Initial, args, meta) return metadata(Initial()(args...), meta) end From 10250a2cb26a46f08d9ce59309233a6c6bb4cead Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 13 Feb 2025 15:45:42 +0530 Subject: [PATCH 0937/2176] docs: add PreallocationTools.jl to docs environment --- docs/Project.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/Project.toml b/docs/Project.toml index 40fcb16581..0b1393cf9e 100644 --- a/docs/Project.toml +++ b/docs/Project.toml @@ -15,6 +15,7 @@ Optimization = "7f7a1694-90dd-40f0-9382-eb1efda571ba" OptimizationOptimJL = "36348300-93cb-4f02-beb5-3c3902f8871e" OrdinaryDiffEq = "1dea7af3-3e70-54e6-95c3-0bf5283fa5ed" Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80" +PreallocationTools = "d236fae5-4411-538c-8e31-a6e3d9e00b46" SciMLStructures = "53ae85a6-f571-4167-b2af-e1d143709226" Setfield = "efcf1570-3423-57d1-acb7-fd33fddbac46" StochasticDiffEq = "789caeaf-c7a9-5a7d-9973-96adeb23e2a0" From bb42719158e876d21a9ddf12b934141632fd2823 Mon Sep 17 00:00:00 2001 From: vyudu Date: Thu, 13 Feb 2025 15:46:43 -0500 Subject: [PATCH 0938/2176] fix: fix initialization of DiscreteSystem with renamed variables --- .../StructuralTransformations.jl | 3 +- .../symbolics_tearing.jl | 20 +++++++----- src/structural_transformation/utils.jl | 32 ++++++++++--------- .../discrete_system/discrete_system.jl | 6 ++-- src/utils.jl | 2 ++ src/variables.jl | 7 +++- test/runtests.jl | 3 ++ 7 files changed, 45 insertions(+), 28 deletions(-) diff --git a/src/structural_transformation/StructuralTransformations.jl b/src/structural_transformation/StructuralTransformations.jl index 1220d517cc..510352ad59 100644 --- a/src/structural_transformation/StructuralTransformations.jl +++ b/src/structural_transformation/StructuralTransformations.jl @@ -22,7 +22,7 @@ using ModelingToolkit: ODESystem, AbstractSystem, var_from_nested_derivative, Di get_postprocess_fbody, vars!, IncrementalCycleTracker, add_edge_checked!, topological_sort, invalidate_cache!, Substitutions, get_or_construct_tearing_state, - filter_kwargs, lower_varname_with_unit, setio, SparseMatrixCLIL, + filter_kwargs, lower_varname_with_unit, lower_shift_varname_with_unit, setio, SparseMatrixCLIL, get_fullvars, has_equations, observed, Schedule, schedule @@ -63,6 +63,7 @@ export torn_system_jacobian_sparsity export full_equations export but_ordered_incidence, lowest_order_variable_mask, highest_order_variable_mask export computed_highest_diff_variables +export shift2term, lower_shift_varname include("utils.jl") include("pantelides.jl") diff --git a/src/structural_transformation/symbolics_tearing.jl b/src/structural_transformation/symbolics_tearing.jl index ec1ff45fd7..ffbd6e3452 100644 --- a/src/structural_transformation/symbolics_tearing.jl +++ b/src/structural_transformation/symbolics_tearing.jl @@ -366,7 +366,6 @@ function generate_derivative_variables!(ts::TearingState, neweqs, var_eq_matchin eq_var_matching = invview(var_eq_matching) diff_to_var = invview(var_to_diff) is_discrete = is_only_discrete(structure) - lower_varname = is_discrete ? lower_shift_varname : lower_varname_with_unit linear_eqs = mm === nothing ? Dict{Int, Int}() : Dict(reverse(en) for en in enumerate(mm.nzrows)) @@ -375,9 +374,9 @@ function generate_derivative_variables!(ts::TearingState, neweqs, var_eq_matchin for v in 1:length(var_to_diff) dv = var_to_diff[v] # For discrete systems, directly substitute lowest-order shift - if is_discrete && diff_to_var[v] == nothing - operation(fullvars[v]) isa Shift && (fullvars[v] = lower_varname(fullvars[v], iv)) - end + #if is_discrete && diff_to_var[v] == nothing + # operation(fullvars[v]) isa Shift && (fullvars[v] = lower_shift_varname_with_unit(fullvars[v], iv)) + #end dv isa Int || continue solved = var_eq_matching[dv] isa Int solved && continue @@ -395,7 +394,8 @@ function generate_derivative_variables!(ts::TearingState, neweqs, var_eq_matchin dx = fullvars[dv] order, lv = var_order(dv, diff_to_var) - x_t = is_discrete ? lower_varname(fullvars[dv], iv) : lower_varname(fullvars[lv], iv, order) + x_t = is_discrete ? lower_shift_varname_with_unit(fullvars[dv], iv) : + Symbolics.diff2term(fullvars[dv]) # Add `x_t` to the graph v_t = add_dd_variable!(structure, fullvars, x_t, dv) @@ -467,11 +467,15 @@ function generate_system_equations!(state::TearingState, neweqs, var_eq_matching total_sub = Dict() if is_only_discrete(structure) - for v in fullvars + for (i, v) in enumerate(fullvars) op = operation(v) - op isa Shift && (op.steps < 0) && (total_sub[v] = lower_shift_varname(v, iv)) + op isa Shift && (op.steps < 0) && begin + lowered = lower_shift_varname_with_unit(v, iv) + total_sub[v] = lowered + fullvars[i] = lowered + end end - end + end # if var is like D(x) or Shift(t, 1)(x) isdervar = let diff_to_var = diff_to_var diff --git a/src/structural_transformation/utils.jl b/src/structural_transformation/utils.jl index 757eb9a8db..f64a8da132 100644 --- a/src/structural_transformation/utils.jl +++ b/src/structural_transformation/utils.jl @@ -453,16 +453,25 @@ end function lower_shift_varname(var, iv) op = operation(var) op isa Shift || return Shift(iv, 0)(var, true) # hack to prevent simplification of x(t) - x(t) - backshift = op.steps - backshift > 0 && return var + if op.steps < 0 + return shift2term(var) + else + return var + end +end - ds = "$iv-$(-backshift)" - d_separator = 'ˍ' +function shift2term(var) + backshift = operation(var).steps + iv = operation(var).t + num = join(Char(0x2080 + d) for d in reverse!(digits(-backshift))) + ds = join([Char(0x209c), Char(0x208b), num]) + #ds = "$iv-$(-backshift)" + #d_separator = 'ˍ' if ModelingToolkit.isoperator(var, ModelingToolkit.Shift) O = only(arguments(var)) oldop = operation(O) - newname = Symbol(string(nameof(oldop)), d_separator, ds) + newname = Symbol(string(nameof(oldop)), ds) else O = var oldop = operation(var) @@ -470,16 +479,9 @@ function lower_shift_varname(var, iv) newname = Symbol(varname, d_separator, ds) end newvar = maketerm(typeof(O), Symbolics.rename(oldop, newname), Symbolics.children(O), Symbolics.metadata(O)) - setmetadata(newvar, Symbolics.VariableSource, (:variables, newname)) - return ModelingToolkit._with_unit(identity, newvar, iv) -end - -function lower_varname(var, iv, order; is_discrete = false) - if is_discrete - lower_shift_varname(var, iv) - else - lower_varname_with_unit(var, iv, order) - end + newvar = setmetadata(newvar, Symbolics.VariableSource, (:variables, newname)) + newvar = setmetadata(newvar, ModelingToolkit.VariableUnshifted, O) + return newvar end function isdoubleshift(var) diff --git a/src/systems/discrete_system/discrete_system.jl b/src/systems/discrete_system/discrete_system.jl index cd6ff45b6c..9a9ac47853 100644 --- a/src/systems/discrete_system/discrete_system.jl +++ b/src/systems/discrete_system/discrete_system.jl @@ -275,10 +275,10 @@ function shift_u0map_forward(sys::DiscreteSystem, u0map, defs) end for var in unknowns(sys) op = operation(var) - op isa Shift || continue haskey(updated, var) && continue - root = first(arguments(var)) - haskey(defs, root) || error("Initial condition for $var not provided.") + root = getunshifted(var) + isnothing(root) && continue + haskey(defs, root) || error("Initial condition for $root not provided.") updated[var] = defs[root] end return updated diff --git a/src/utils.jl b/src/utils.jl index cf49d9f445..c3a0f637a1 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -1028,6 +1028,8 @@ end diff2term_with_unit(x, t) = _with_unit(diff2term, x, t) lower_varname_with_unit(var, iv, order) = _with_unit(lower_varname, var, iv, iv, order) +shift2term_with_unit(x, t) = _with_unit(shift2term, x, t) +lower_shift_varname_with_unit(var, iv) = _with_unit(lower_shift_varname, var, iv, iv) """ $(TYPEDSIGNATURES) diff --git a/src/variables.jl b/src/variables.jl index 536119f107..a29a119607 100644 --- a/src/variables.jl +++ b/src/variables.jl @@ -6,6 +6,7 @@ struct VariableOutput end struct VariableIrreducible end struct VariableStatePriority end struct VariableMisc end +struct VariableUnshifted end Symbolics.option_to_metadata_type(::Val{:unit}) = VariableUnit Symbolics.option_to_metadata_type(::Val{:connect}) = VariableConnectType Symbolics.option_to_metadata_type(::Val{:input}) = VariableInput @@ -13,6 +14,7 @@ Symbolics.option_to_metadata_type(::Val{:output}) = VariableOutput Symbolics.option_to_metadata_type(::Val{:irreducible}) = VariableIrreducible Symbolics.option_to_metadata_type(::Val{:state_priority}) = VariableStatePriority Symbolics.option_to_metadata_type(::Val{:misc}) = VariableMisc +Symbolics.option_to_metadata_type(::Val{:unshifted}) = VariableUnshifted """ dump_variable_metadata(var) @@ -133,7 +135,7 @@ function default_toterm(x) if iscall(x) && (op = operation(x)) isa Operator if !(op isa Differential) if op isa Shift && op.steps < 0 - return x + return shift2term(x) end x = normalize_to_differential(op)(arguments(x)...) end @@ -600,3 +602,6 @@ getunit(x::Symbolic) = Symbolics.getmetadata(x, VariableUnit, nothing) Check if the variable `x` has a unit. """ hasunit(x) = getunit(x) !== nothing + +getunshifted(x) = getunshifted(unwrap(x)) +getunshifted(x::Symbolic) = Symbolics.getmetadata(x, VariableUnshifted, nothing) diff --git a/test/runtests.jl b/test/runtests.jl index 9537b1b44e..e600305232 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -22,6 +22,8 @@ function activate_downstream_env() Pkg.instantiate() end +@testset begin include("discrete_system.jl") end +#= @time begin if GROUP == "All" || GROUP == "InterfaceI" @testset "InterfaceI" begin @@ -136,3 +138,4 @@ end @safetestset "InfiniteOpt Extension Test" include("extensions/test_infiniteopt.jl") end end +=# From 09f31b23ca7338e3a646b26a3bc582bd58633a2a Mon Sep 17 00:00:00 2001 From: vyudu Date: Thu, 13 Feb 2025 15:49:35 -0500 Subject: [PATCH 0939/2176] revert runtest --- test/runtests.jl | 3 --- 1 file changed, 3 deletions(-) diff --git a/test/runtests.jl b/test/runtests.jl index e600305232..9537b1b44e 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -22,8 +22,6 @@ function activate_downstream_env() Pkg.instantiate() end -@testset begin include("discrete_system.jl") end -#= @time begin if GROUP == "All" || GROUP == "InterfaceI" @testset "InterfaceI" begin @@ -138,4 +136,3 @@ end @safetestset "InfiniteOpt Extension Test" include("extensions/test_infiniteopt.jl") end end -=# From f4a9d1369555a35e3c59542ce259fbcf3e6ba5f2 Mon Sep 17 00:00:00 2001 From: vyudu Date: Thu, 13 Feb 2025 16:15:48 -0500 Subject: [PATCH 0940/2176] up --- .../implicit_discrete_system.jl | 23 +++---------------- 1 file changed, 3 insertions(+), 20 deletions(-) diff --git a/src/systems/discrete_system/implicit_discrete_system.jl b/src/systems/discrete_system/implicit_discrete_system.jl index 3a137eb8c1..82ef66d17e 100644 --- a/src/systems/discrete_system/implicit_discrete_system.jl +++ b/src/systems/discrete_system/implicit_discrete_system.jl @@ -238,8 +238,6 @@ function ImplicitDiscreteSystem(eqs, iv; kwargs...) return ImplicitDiscreteSystem(eqs, iv, collect(allunknowns), collect(new_ps); kwargs...) end -# basically at every timestep it should build a nonlinear solve -# Previous timesteps should be treated as parameters? is this right? function flatten(sys::ImplicitDiscreteSystem, noeqs = false) systems = get_systems(sys) @@ -263,25 +261,10 @@ end function generate_function( sys::ImplicitDiscreteSystem, dvs = unknowns(sys), ps = parameters(sys); wrap_code = identity, kwargs...) - if !iscomplete(sys) - error("A completed system is required. Call `complete` or `structural_simplify` on the system.") - end - p = (reorder_parameters(sys, unwrap.(ps))..., cachesyms...) - isscalar = !(exprs isa AbstractArray) - pre, sol_states = get_substitutions_and_solved_unknowns(sys, isscalar ? [exprs] : exprs) - if postprocess_fbody === nothing - postprocess_fbody = pre - end - if states === nothing - states = sol_states - end exprs = [eq.lhs - eq.rhs for eq in equations(sys)] - u = map(Shift(iv, -1), dvs) - u_next = dvs - - wrap_code = wrap_code .∘ wrap_array_vars(sys, exprs) .∘ wrap_parameter_dependencies(sys, false) - - build_function(exprs, u_next, u, p..., get_iv(sys)) + u = dvs + u_next = map(Shift(iv, 1), u) + generate_custom_function(sys, exprs, u_next, u, ps..., get_iv(sys); kwargs...) end function shift_u0map_forward(sys::ImplicitDiscreteSystem, u0map, defs) From a25aab4ca128caef01a18560aaf32e1c5fa8e04d Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 14 Feb 2025 14:56:36 +0530 Subject: [PATCH 0941/2176] test: increase tolerances of FMI CS test --- test/fmi/fmi.jl | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/fmi/fmi.jl b/test/fmi/fmi.jl index a311bf44d2..f3ab3549cf 100644 --- a/test/fmi/fmi.jl +++ b/test/fmi/fmi.jl @@ -157,7 +157,7 @@ end @testset "v2, CS" begin fmu = loadFMU(joinpath(FMU_DIR, "SimpleAdder.fmu"); type = :CS) @named adder = MTK.FMIComponent( - Val(2); fmu, type = :CS, communication_step_size = 1e-3, + Val(2); fmu, type = :CS, communication_step_size = 1e-4, reinitializealg = BrownFullBasicInit()) @test MTK.isinput(adder.a) @test MTK.isinput(adder.b) @@ -169,9 +169,9 @@ end sol = solve(prob, Rodas5P(autodiff = false), abstol = 1e-8, reltol = 1e-8) @test SciMLBase.successful_retcode(sol) - @test truesol(sol.t; idxs = [truesys.a, truesys.b, truesys.c]).u≈sol.u rtol=1e-2 + @test truesol(sol.t; idxs = [truesys.a, truesys.b, truesys.c]).u≈sol.u rtol=4e-2 # sys.adder.c is a discrete variable - @test truesol(sol.t; idxs = truesys.adder.c).u≈sol(sol.t; idxs = sys.adder.c).u rtol=1e-3 + @test truesol(sol.t; idxs = truesys.adder.c).u≈sol(sol.t; idxs = sys.adder.c).u rtol=4e-2 end function build_sspace_model(sspace) From 7767fb509c40f7838f3f6988dc9e279d499da625 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 14 Feb 2025 15:05:18 +0530 Subject: [PATCH 0942/2176] fix: handle unsized array symbolics in `get_possibly_array_fallback_singletons` --- src/systems/nonlinear/initializesystem.jl | 1 + 1 file changed, 1 insertion(+) diff --git a/src/systems/nonlinear/initializesystem.jl b/src/systems/nonlinear/initializesystem.jl index 07f745de18..38cc65daf6 100644 --- a/src/systems/nonlinear/initializesystem.jl +++ b/src/systems/nonlinear/initializesystem.jl @@ -356,6 +356,7 @@ function get_possibly_array_fallback_singletons(varmap, p) return varmap[p] end if symbolic_type(p) == ArraySymbolic() + is_sized_array_symbolic(p) || return nothing scal = collect(p) if all(x -> haskey(varmap, x), scal) res = [varmap[x] for x in scal] From 0b7273786b89f810a5e89eda32b71bd39a9d9b72 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 14 Feb 2025 15:13:27 +0530 Subject: [PATCH 0943/2176] fix: search for required parameter dependencies in observed equations --- src/systems/codegen_utils.jl | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/systems/codegen_utils.jl b/src/systems/codegen_utils.jl index 1c9e6ce53c..5a645c2047 100644 --- a/src/systems/codegen_utils.jl +++ b/src/systems/codegen_utils.jl @@ -160,6 +160,9 @@ function build_function_wrapper(sys::AbstractSystem, expr, args...; p_start = 2, end # similarly for parameter dependency equations pdepidxs = observed_equations_used_by(sys, expr; obs = pdeps) + for i in obsidxs + union!(pdepidxs, observed_equations_used_by(sys, obs[i].rhs; obs = pdeps)) + end # assignments for reconstructing scalarized array symbolics assignments = array_variable_assignments(args...) From daa789856714d3926c265307a94bc6f3f7eecbbd Mon Sep 17 00:00:00 2001 From: vyudu Date: Fri, 14 Feb 2025 06:25:44 -0500 Subject: [PATCH 0944/2176] frefactor use toterm instead of lower_varname --- src/structural_transformation/symbolics_tearing.jl | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/src/structural_transformation/symbolics_tearing.jl b/src/structural_transformation/symbolics_tearing.jl index ffbd6e3452..47c7da371f 100644 --- a/src/structural_transformation/symbolics_tearing.jl +++ b/src/structural_transformation/symbolics_tearing.jl @@ -366,6 +366,7 @@ function generate_derivative_variables!(ts::TearingState, neweqs, var_eq_matchin eq_var_matching = invview(var_eq_matching) diff_to_var = invview(var_to_diff) is_discrete = is_only_discrete(structure) + toterm = is_discrete ? shift2term_with_unit : diff2term_with_unit linear_eqs = mm === nothing ? Dict{Int, Int}() : Dict(reverse(en) for en in enumerate(mm.nzrows)) @@ -373,10 +374,6 @@ function generate_derivative_variables!(ts::TearingState, neweqs, var_eq_matchin # derivative is in the system for v in 1:length(var_to_diff) dv = var_to_diff[v] - # For discrete systems, directly substitute lowest-order shift - #if is_discrete && diff_to_var[v] == nothing - # operation(fullvars[v]) isa Shift && (fullvars[v] = lower_shift_varname_with_unit(fullvars[v], iv)) - #end dv isa Int || continue solved = var_eq_matching[dv] isa Int solved && continue @@ -394,8 +391,7 @@ function generate_derivative_variables!(ts::TearingState, neweqs, var_eq_matchin dx = fullvars[dv] order, lv = var_order(dv, diff_to_var) - x_t = is_discrete ? lower_shift_varname_with_unit(fullvars[dv], iv) : - Symbolics.diff2term(fullvars[dv]) + x_t = toterm(fullvars[dv]) # Add `x_t` to the graph v_t = add_dd_variable!(structure, fullvars, x_t, dv) @@ -470,7 +466,7 @@ function generate_system_equations!(state::TearingState, neweqs, var_eq_matching for (i, v) in enumerate(fullvars) op = operation(v) op isa Shift && (op.steps < 0) && begin - lowered = lower_shift_varname_with_unit(v, iv) + lowered = shift2term_with_unit(v) total_sub[v] = lowered fullvars[i] = lowered end From ae19eeb84b3b2d3cf6ca6db1a658581b8f9ba714 Mon Sep 17 00:00:00 2001 From: vyudu Date: Fri, 14 Feb 2025 06:50:46 -0500 Subject: [PATCH 0945/2176] fix unit --- src/structural_transformation/StructuralTransformations.jl | 2 +- src/structural_transformation/symbolics_tearing.jl | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/structural_transformation/StructuralTransformations.jl b/src/structural_transformation/StructuralTransformations.jl index 510352ad59..7d2b9afa26 100644 --- a/src/structural_transformation/StructuralTransformations.jl +++ b/src/structural_transformation/StructuralTransformations.jl @@ -11,7 +11,7 @@ using SymbolicUtils: maketerm, iscall using ModelingToolkit using ModelingToolkit: ODESystem, AbstractSystem, var_from_nested_derivative, Differential, - unknowns, equations, vars, Symbolic, diff2term_with_unit, value, + unknowns, equations, vars, Symbolic, diff2term_with_unit, shift2term_with_unit, value, operation, arguments, Sym, Term, simplify, symbolic_linear_solve, isdiffeq, isdifferential, isirreducible, empty_substitutions, get_substitutions, diff --git a/src/structural_transformation/symbolics_tearing.jl b/src/structural_transformation/symbolics_tearing.jl index 47c7da371f..779f5931f5 100644 --- a/src/structural_transformation/symbolics_tearing.jl +++ b/src/structural_transformation/symbolics_tearing.jl @@ -366,7 +366,6 @@ function generate_derivative_variables!(ts::TearingState, neweqs, var_eq_matchin eq_var_matching = invview(var_eq_matching) diff_to_var = invview(var_to_diff) is_discrete = is_only_discrete(structure) - toterm = is_discrete ? shift2term_with_unit : diff2term_with_unit linear_eqs = mm === nothing ? Dict{Int, Int}() : Dict(reverse(en) for en in enumerate(mm.nzrows)) @@ -391,7 +390,8 @@ function generate_derivative_variables!(ts::TearingState, neweqs, var_eq_matchin dx = fullvars[dv] order, lv = var_order(dv, diff_to_var) - x_t = toterm(fullvars[dv]) + x_t = is_discrete ? lower_shift_varname_with_unit(fullvars[dv], iv) + : lower_varname_with_unit(fullvars[lv], iv, order) # Add `x_t` to the graph v_t = add_dd_variable!(structure, fullvars, x_t, dv) @@ -466,7 +466,7 @@ function generate_system_equations!(state::TearingState, neweqs, var_eq_matching for (i, v) in enumerate(fullvars) op = operation(v) op isa Shift && (op.steps < 0) && begin - lowered = shift2term_with_unit(v) + lowered = lower_shift_varname_with_unit(v, iv) total_sub[v] = lowered fullvars[i] = lowered end From 406d0a861d4904d10cf99ad11dbc118f38a0c718 Mon Sep 17 00:00:00 2001 From: vyudu Date: Fri, 14 Feb 2025 06:55:14 -0500 Subject: [PATCH 0946/2176] fix parse error --- src/structural_transformation/symbolics_tearing.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/structural_transformation/symbolics_tearing.jl b/src/structural_transformation/symbolics_tearing.jl index 779f5931f5..a166bb064b 100644 --- a/src/structural_transformation/symbolics_tearing.jl +++ b/src/structural_transformation/symbolics_tearing.jl @@ -390,8 +390,8 @@ function generate_derivative_variables!(ts::TearingState, neweqs, var_eq_matchin dx = fullvars[dv] order, lv = var_order(dv, diff_to_var) - x_t = is_discrete ? lower_shift_varname_with_unit(fullvars[dv], iv) - : lower_varname_with_unit(fullvars[lv], iv, order) + x_t = is_discrete ? lower_shift_varname_with_unit(fullvars[dv], iv) : + lower_varname_with_unit(fullvars[lv], iv, order) # Add `x_t` to the graph v_t = add_dd_variable!(structure, fullvars, x_t, dv) From 5f231c751a5def2e2a7d9911ae961793c8e2e0e2 Mon Sep 17 00:00:00 2001 From: vyudu Date: Fri, 14 Feb 2025 07:54:04 -0500 Subject: [PATCH 0947/2176] fix: fix initialization cases --- src/ModelingToolkit.jl | 3 ++ .../StructuralTransformations.jl | 2 +- src/structural_transformation/utils.jl | 5 ++-- .../discrete_system/discrete_system.jl | 10 +++++-- .../implicit_discrete_system.jl | 25 +++++++++------- src/systems/systems.jl | 4 +-- src/systems/systemstructure.jl | 2 +- test/implicit_discrete_system.jl | 30 +++++++++++++++++++ test/runtests.jl | 1 + 9 files changed, 62 insertions(+), 20 deletions(-) create mode 100644 test/implicit_discrete_system.jl diff --git a/src/ModelingToolkit.jl b/src/ModelingToolkit.jl index 09b59c4ed6..ce3db8bde7 100644 --- a/src/ModelingToolkit.jl +++ b/src/ModelingToolkit.jl @@ -123,6 +123,7 @@ abstract type AbstractTimeIndependentSystem <: AbstractSystem end abstract type AbstractODESystem <: AbstractTimeDependentSystem end abstract type AbstractMultivariateSystem <: AbstractSystem end abstract type AbstractOptimizationSystem <: AbstractTimeIndependentSystem end +abstract type AbstractDiscreteSystem <: AbstractTimeDependentSystem end function independent_variable end @@ -165,6 +166,7 @@ include("systems/diffeqs/modelingtoolkitize.jl") include("systems/diffeqs/basic_transformations.jl") include("systems/discrete_system/discrete_system.jl") +include("systems/discrete_system/implicit_discrete_system.jl") include("systems/jumps/jumpsystem.jl") @@ -230,6 +232,7 @@ export DAEFunctionExpr, DAEProblemExpr export SDESystem, SDEFunction, SDEFunctionExpr, SDEProblemExpr export SystemStructure export DiscreteSystem, DiscreteProblem, DiscreteFunction, DiscreteFunctionExpr +export ImplicitDiscreteSystem, ImplicitDiscreteProblem, ImplicitDiscreteFunction, ImplicitDiscreteFunctionExpr export JumpSystem export ODEProblem, SDEProblem export NonlinearFunction, NonlinearFunctionExpr diff --git a/src/structural_transformation/StructuralTransformations.jl b/src/structural_transformation/StructuralTransformations.jl index 510352ad59..c7b3957c62 100644 --- a/src/structural_transformation/StructuralTransformations.jl +++ b/src/structural_transformation/StructuralTransformations.jl @@ -63,7 +63,7 @@ export torn_system_jacobian_sparsity export full_equations export but_ordered_incidence, lowest_order_variable_mask, highest_order_variable_mask export computed_highest_diff_variables -export shift2term, lower_shift_varname +export shift2term, lower_shift_varname, simplify_shifts include("utils.jl") include("pantelides.jl") diff --git a/src/structural_transformation/utils.jl b/src/structural_transformation/utils.jl index f64a8da132..db79c8a124 100644 --- a/src/structural_transformation/utils.jl +++ b/src/structural_transformation/utils.jl @@ -452,11 +452,10 @@ end # For discrete variables. Turn Shift(t, k)(x(t)) into xₜ₋ₖ(t) function lower_shift_varname(var, iv) op = operation(var) - op isa Shift || return Shift(iv, 0)(var, true) # hack to prevent simplification of x(t) - x(t) - if op.steps < 0 + if op isa Shift && op.steps < 0 return shift2term(var) else - return var + return Shift(iv, 0)(var, true) end end diff --git a/src/systems/discrete_system/discrete_system.jl b/src/systems/discrete_system/discrete_system.jl index 9a9ac47853..703efcc462 100644 --- a/src/systems/discrete_system/discrete_system.jl +++ b/src/systems/discrete_system/discrete_system.jl @@ -17,7 +17,7 @@ eqs = [x(k+1) ~ σ*(y-x), @named de = DiscreteSystem(eqs) ``` """ -struct DiscreteSystem <: AbstractTimeDependentSystem +struct DiscreteSystem <: AbstractDiscreteSystem """ A tag for the system. If two systems have the same tag, then they are structurally identical. @@ -237,6 +237,8 @@ function DiscreteSystem(eqs, iv; kwargs...) collect(allunknowns), collect(new_ps); kwargs...) end +DiscreteSystem(eq::Equation, args...; kwargs...) = DiscreteSystem([eq], args...; kwargs...) + function flatten(sys::DiscreteSystem, noeqs = false) systems = get_systems(sys) if isempty(systems) @@ -271,14 +273,16 @@ function shift_u0map_forward(sys::DiscreteSystem, u0map, defs) if !((op = operation(k)) isa Shift) error("Initial conditions must be for the past state of the unknowns. Instead of providing the condition for $k, provide the condition for $(Shift(iv, -1)(k)).") end - updated[Shift(iv, op.steps + 1)(arguments(k)[1])] = v + k_next = Shift(iv, op.steps + 1)(arguments(k)[1]) + operation(k_next) isa Shift ? updated[shift2term(k_next)] = v : + updated[k_next] = v end for var in unknowns(sys) op = operation(var) haskey(updated, var) && continue root = getunshifted(var) isnothing(root) && continue - haskey(defs, root) || error("Initial condition for $root not provided.") + haskey(defs, root) || error("Initial condition for $var not provided.") updated[var] = defs[root] end return updated diff --git a/src/systems/discrete_system/implicit_discrete_system.jl b/src/systems/discrete_system/implicit_discrete_system.jl index 82ef66d17e..e0450a1ada 100644 --- a/src/systems/discrete_system/implicit_discrete_system.jl +++ b/src/systems/discrete_system/implicit_discrete_system.jl @@ -239,6 +239,8 @@ function ImplicitDiscreteSystem(eqs, iv; kwargs...) collect(allunknowns), collect(new_ps); kwargs...) end +ImplicitDiscreteSystem(eq::Equation, args...; kwargs...) = ImplicitDiscreteSystem([eq], args...; kwargs...) + function flatten(sys::ImplicitDiscreteSystem, noeqs = false) systems = get_systems(sys) if isempty(systems) @@ -261,10 +263,14 @@ end function generate_function( sys::ImplicitDiscreteSystem, dvs = unknowns(sys), ps = parameters(sys); wrap_code = identity, kwargs...) - exprs = [eq.lhs - eq.rhs for eq in equations(sys)] - u = dvs - u_next = map(Shift(iv, 1), u) - generate_custom_function(sys, exprs, u_next, u, ps..., get_iv(sys); kwargs...) + iv = get_iv(sys) + exprs = map(equations(sys)) do eq + _iszero(eq.lhs) ? eq.rhs : (simplify_shifts(Shift(iv, -1)(eq.rhs)) - simplify_shifts(Shift(iv, -1)(eq.lhs))) + end + + u_next = dvs + u = map(Shift(iv, -1), u_next) + build_function_wrapper(sys, exprs, u_next, u, ps..., iv; p_start = 3, kwargs...) end function shift_u0map_forward(sys::ImplicitDiscreteSystem, u0map, defs) @@ -275,13 +281,13 @@ function shift_u0map_forward(sys::ImplicitDiscreteSystem, u0map, defs) if !((op = operation(k)) isa Shift) error("Initial conditions must be for the past state of the unknowns. Instead of providing the condition for $k, provide the condition for $(Shift(iv, -1)(k)).") end - updated[Shift(iv, op.steps + 1)(arguments(k)[1])] = v + updated[shift2term(k)] = v end for var in unknowns(sys) op = operation(var) - op isa Shift || continue haskey(updated, var) && continue - root = first(arguments(var)) + root = getunshifted(var) + isnothing(root) && continue haskey(defs, root) || error("Initial condition for $var not provided.") updated[var] = defs[root] end @@ -301,7 +307,7 @@ function SciMLBase.ImplicitDiscreteProblem( kwargs... ) if !iscomplete(sys) - error("A completed `ImplicitDiscreteSystem` is required. Call `complete` or `structural_simplify` on the system before creating a `ImplicitDiscreteProblem`") + error("A completed `ImplicitDiscreteSystem` is required. Call `complete` or `structural_simplify` on the system before creating a `ImplicitDiscreteProblem`.") end dvs = unknowns(sys) ps = parameters(sys) @@ -312,8 +318,7 @@ function SciMLBase.ImplicitDiscreteProblem( u0map = shift_u0map_forward(sys, u0map, defaults(sys)) f, u0, p = process_SciMLProblem( ImplicitDiscreteFunction, sys, u0map, parammap; eval_expression, eval_module) - u0 = f(u0, p, tspan[1]) - NonlinearProblem(f, u0, tspan, p; kwargs...) + ImplicitDiscreteProblem(f, u0, tspan, p; kwargs...) end function SciMLBase.ImplicitDiscreteFunction(sys::ImplicitDiscreteSystem, args...; kwargs...) diff --git a/src/systems/systems.jl b/src/systems/systems.jl index f8630f2d20..40f3353884 100644 --- a/src/systems/systems.jl +++ b/src/systems/systems.jl @@ -42,8 +42,8 @@ function structural_simplify( if newsys isa DiscreteSystem && any(eq -> symbolic_type(eq.lhs) == NotSymbolic(), equations(newsys)) error(""" - Encountered algebraic equations when simplifying discrete system. This is \ - not yet supported. + Encountered algebraic equations when simplifying discrete system. Please construct \ + an ImplicitDiscreteSystem instead. """) end for pass in additional_passes diff --git a/src/systems/systemstructure.jl b/src/systems/systemstructure.jl index 6fee78cfd6..a6e6b1d218 100644 --- a/src/systems/systemstructure.jl +++ b/src/systems/systemstructure.jl @@ -438,7 +438,7 @@ function TearingState(sys; quick_cancel = false, check = true) ts = TearingState(sys, fullvars, SystemStructure(complete(var_to_diff), complete(eq_to_diff), - complete(graph), nothing, var_types, sys isa DiscreteSystem), + complete(graph), nothing, var_types, sys isa AbstractDiscreteSystem), Any[]) if sys isa DiscreteSystem ts = shift_discrete_system(ts) diff --git a/test/implicit_discrete_system.jl b/test/implicit_discrete_system.jl new file mode 100644 index 0000000000..88e79acc77 --- /dev/null +++ b/test/implicit_discrete_system.jl @@ -0,0 +1,30 @@ +using ModelingToolkit, Test +using ModelingToolkit: t_nounits as t + +k = ShiftIndex(t) +@variables x(t) = 1 +@mtkbuild sys = ImplicitDiscreteSystem([x(k) ~ x(k)*x(k-1) - 3], t) +tspan = (0, 10) + +# Shift(t, -1)(x(t)) - x_{t-1}(t) +# -3 - x(t) + x(t)*x_{t-1} +f = ImplicitDiscreteFunction(sys) +u_next = [3., 1.5] +@test f(u_next, [2.,3.], [], t) ≈ [0., 0.] +u_next = [0., 0.] +@test f(u_next, [2.,3.], [], t) ≈ [3., -3.] + +resid = rand(2) +f(resid, u_next, [2.,3.], [], t) +@test resid ≈ [3., -3.] + +# Initialization cases. +prob = ImplicitDiscreteProblem(sys, [x(k-1) => 3.], tspan) +@test prob.u0 == [3., 1.] +prob = ImplicitDiscreteProblem(sys, [], tspan) +@test prob.u0 == [1., 1.] +@variables x(t) +@mtkbuild sys = ImplicitDiscreteSystem([x(k) ~ x(k)*x(k-1) - 3], t) +@test_throws ErrorException prob = ImplicitDiscreteProblem(sys, [], tspan) + +# Test solvers diff --git a/test/runtests.jl b/test/runtests.jl index 9537b1b44e..e774bbe4cc 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -80,6 +80,7 @@ end @safetestset "Variable Metadata Test" include("test_variable_metadata.jl") @safetestset "OptimizationSystem Test" include("optimizationsystem.jl") @safetestset "Discrete System" include("discrete_system.jl") + @safetestset "Implicit Discrete System" include("implicit_discrete_system.jl") @safetestset "SteadyStateSystem Test" include("steadystatesystems.jl") @safetestset "SDESystem Test" include("sdesystem.jl") @safetestset "DDESystem Test" include("dde.jl") From a826a6241725db58ca87beb3f252f15d7c658273 Mon Sep 17 00:00:00 2001 From: vyudu Date: Fri, 14 Feb 2025 08:00:39 -0500 Subject: [PATCH 0948/2176] fix unit --- src/structural_transformation/symbolics_tearing.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/structural_transformation/symbolics_tearing.jl b/src/structural_transformation/symbolics_tearing.jl index ffbd6e3452..a3a90abdfb 100644 --- a/src/structural_transformation/symbolics_tearing.jl +++ b/src/structural_transformation/symbolics_tearing.jl @@ -395,7 +395,7 @@ function generate_derivative_variables!(ts::TearingState, neweqs, var_eq_matchin dx = fullvars[dv] order, lv = var_order(dv, diff_to_var) x_t = is_discrete ? lower_shift_varname_with_unit(fullvars[dv], iv) : - Symbolics.diff2term(fullvars[dv]) + lower_varname_with_unit(fullvars[lv], iv, order) # Add `x_t` to the graph v_t = add_dd_variable!(structure, fullvars, x_t, dv) From b58993313f0b1178b715ba1e9195a871cfba3a60 Mon Sep 17 00:00:00 2001 From: vyudu Date: Fri, 14 Feb 2025 08:02:01 -0500 Subject: [PATCH 0949/2176] add doc file --- docs/src/systems/ImplicitDiscreteSystem.md | 28 ++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 docs/src/systems/ImplicitDiscreteSystem.md diff --git a/docs/src/systems/ImplicitDiscreteSystem.md b/docs/src/systems/ImplicitDiscreteSystem.md new file mode 100644 index 0000000000..d02ccc5e42 --- /dev/null +++ b/docs/src/systems/ImplicitDiscreteSystem.md @@ -0,0 +1,28 @@ +# ImplicitDiscreteSystem + +## System Constructors + +```@docs +ImplicitDiscreteSystem +``` + +## Composition and Accessor Functions + + - `get_eqs(sys)` or `equations(sys)`: The equations that define the implicit discrete system. + - `get_unknowns(sys)` or `unknowns(sys)`: The set of unknowns in the implicit discrete system. + - `get_ps(sys)` or `parameters(sys)`: The parameters of the implicit discrete system. + - `get_iv(sys)`: The independent variable of the implicit discrete system + - `discrete_events(sys)`: The set of discrete events in the implicit discrete system. + +## Transformations + +```@docs; canonical=false +structural_simplify +``` + +## Problem Constructors + +```@docs; canonical=false +ImplicitDiscreteProblem(sys::ImplicitDiscreteSystem, u0map, tspan) +ImplicitDiscreteFunction(sys::ImplicitDiscreteSystem, args...) +``` From f5770844623e89d82d40e64b6489d92188d4b290 Mon Sep 17 00:00:00 2001 From: vyudu Date: Fri, 14 Feb 2025 08:11:58 -0500 Subject: [PATCH 0950/2176] add to docs/pages.jl --- docs/pages.jl | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/docs/pages.jl b/docs/pages.jl index 6e18f403dd..0b2c867507 100644 --- a/docs/pages.jl +++ b/docs/pages.jl @@ -42,7 +42,8 @@ pages = [ "systems/NonlinearSystem.md", "systems/OptimizationSystem.md", "systems/PDESystem.md", - "systems/DiscreteSystem.md"], + "systems/DiscreteSystem.md", + "systems/ImplicitDiscreteSystem.md"], "comparison.md", "internals.md" ] From f88b9af23e1dd3460a1afb6945ecb1765a537e53 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 14 Feb 2025 22:56:31 +0530 Subject: [PATCH 0951/2176] build: bump minor version --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index ec7939a58c..ce3ece8e9a 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "ModelingToolkit" uuid = "961ee093-0014-501f-94e3-6117800e7a78" authors = ["Yingbo Ma ", "Chris Rackauckas and contributors"] -version = "9.62.0" +version = "9.63.0" [deps] AbstractTrees = "1520ce14-60c1-5f80-bbc7-55ef81b5835c" From 05c49ca836af1612bac7317634d3e234717e3a62 Mon Sep 17 00:00:00 2001 From: CompatHelper Julia Date: Sat, 15 Feb 2025 00:23:47 +0000 Subject: [PATCH 0952/2176] CompatHelper: add new compat entry for PreallocationTools at version 0.4 for package docs, (keep existing compat) --- docs/Project.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/Project.toml b/docs/Project.toml index 0b1393cf9e..1f43934352 100644 --- a/docs/Project.toml +++ b/docs/Project.toml @@ -39,6 +39,7 @@ Optimization = "3.9, 4" OptimizationOptimJL = "0.1, 0.4" OrdinaryDiffEq = "6.31" Plots = "1.36" +PreallocationTools = "0.4" SciMLStructures = "1.1" Setfield = "1" StochasticDiffEq = "6" From c9822671b594b937dc67a7847f40451073eb0582 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Sun, 16 Feb 2025 18:19:14 +0530 Subject: [PATCH 0953/2176] feat: add `GeneratedFunctionWrapper` --- src/systems/codegen_utils.jl | 56 ++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/src/systems/codegen_utils.jl b/src/systems/codegen_utils.jl index 5a645c2047..910d0d1c1e 100644 --- a/src/systems/codegen_utils.jl +++ b/src/systems/codegen_utils.jl @@ -230,3 +230,59 @@ function build_function_wrapper(sys::AbstractSystem, expr, args...; p_start = 2, end return build_function(expr, args...; wrap_code, similarto, kwargs...) end + +""" + $(TYPEDEF) + +A wrapper around a generated in-place and out-of-place function. The type-parameter `P` +must be a 3-tuple where the first element is the index of the parameter object in the +arguments, the second is the expected number of arguments in the out-of-place variant +of the function, and the third is a boolean indicating whether the generated functions +are for a split system. For scalar functions, the inplace variant can be `nothing`. +""" +struct GeneratedFunctionWrapper{P, O, I} <: Function + f_oop::O + f_iip::I +end + +function GeneratedFunctionWrapper{P}(foop::O, fiip::I) where {P, O, I} + GeneratedFunctionWrapper{P, O, I}(foop, fiip) +end + +function (gfw::GeneratedFunctionWrapper)(args...) + _generated_call(gfw, args...) +end + +@generated function _generated_call(gfw::GeneratedFunctionWrapper{P}, args...) where {P} + paramidx, nargs, issplit = P + iip = false + # IIP case has one more argument + if length(args) == nargs + 1 + nargs += 1 + paramidx += 1 + iip = true + end + if length(args) != nargs + throw(ArgumentError("Expected $nargs arguments, got $(length(args)).")) + end + + # the function to use + f = iip ? :(gfw.f_iip) : :(gfw.f_oop) + # non-split systems just call it as-is + if !issplit + return :($f(args...)) + end + if args[paramidx] <: Union{Tuple, MTKParameters} && + !(args[paramidx] <: Tuple{Vararg{Number}}) + # for split systems, call it as-is if the parameter object is a tuple or MTKParameters + # but not if it is a tuple of numbers + return :($f(args...)) + else + # The user provided a single buffer/tuple for the parameter object, so wrap that + # one in a tuple + fargs = ntuple(Val(length(args))) do i + i == paramidx ? :((args[$i],)) : :(args[$i]) + end + return :($f($(fargs...))) + end +end From 4087cbfa9d2adecc932691e4d56516a8ce84edaf Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Sun, 16 Feb 2025 18:19:38 +0530 Subject: [PATCH 0954/2176] fix: use `GeneratedFunctionWrapper` in codegen --- src/systems/abstractsystem.jl | 35 +------------ src/systems/diffeqs/abstractodesystem.jl | 52 ++++++------------- src/systems/diffeqs/odesystem.jl | 9 +++- src/systems/diffeqs/sdesystem.jl | 19 +++---- .../discrete_system/discrete_system.jl | 3 +- src/systems/jumps/jumpsystem.jl | 6 +-- src/systems/nonlinear/nonlinearsystem.jl | 27 +++++----- 7 files changed, 47 insertions(+), 104 deletions(-) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index 41c487dadc..8baad025d1 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -422,28 +422,7 @@ function SymbolicIndexingInterface.timeseries_parameter_index(sys::AbstractSyste end function SymbolicIndexingInterface.parameter_observed(sys::AbstractSystem, sym) - if has_index_cache(sys) && (ic = get_index_cache(sys)) !== nothing - rawobs = build_explicit_observed_function( - sys, sym; param_only = true, return_inplace = true) - if rawobs isa Tuple - if is_time_dependent(sys) - obsfn = let oop = rawobs[1], iip = rawobs[2] - f1a(p, t) = oop(p, t) - f1a(out, p, t) = iip(out, p, t) - end - else - obsfn = let oop = rawobs[1], iip = rawobs[2] - f1b(p) = oop(p) - f1b(out, p) = iip(out, p) - end - end - else - obsfn = rawobs - end - else - obsfn = build_explicit_observed_function(sys, sym; param_only = true) - end - return obsfn + return build_explicit_observed_function(sys, sym; param_only = true) end function has_observed_with_lhs(sys, sym) @@ -579,18 +558,8 @@ function SymbolicIndexingInterface.observed( end end end - _fn = build_explicit_observed_function( + return build_explicit_observed_function( sys, sym; eval_expression, eval_module, checkbounds) - - if is_time_dependent(sys) - return _fn - else - return let _fn = _fn - fn2(u, p) = _fn(u, p) - fn2(::Nothing, p) = _fn([], p) - fn2 - end - end end function SymbolicIndexingInterface.default_values(sys::AbstractSystem) diff --git a/src/systems/diffeqs/abstractodesystem.jl b/src/systems/diffeqs/abstractodesystem.jl index c7eff90e03..04f7b468d2 100644 --- a/src/systems/diffeqs/abstractodesystem.jl +++ b/src/systems/diffeqs/abstractodesystem.jl @@ -320,9 +320,7 @@ function DiffEqBase.ODEFunction{iip, specialize}(sys::AbstractODESystem, expression_module = eval_module, checkbounds = checkbounds, kwargs...) f_oop, f_iip = eval_or_rgf.(f_gen; eval_expression, eval_module) - - f(u, p, t) = f_oop(u, p, t) - f(du, u, p, t) = f_iip(du, u, p, t) + f = GeneratedFunctionWrapper{(2, 3, is_split(sys))}(f_oop, f_iip) if specialize === SciMLBase.FunctionWrapperSpecialize && iip if u0 === nothing || p === nothing || t === nothing @@ -338,10 +336,7 @@ function DiffEqBase.ODEFunction{iip, specialize}(sys::AbstractODESystem, expression_module = eval_module, checkbounds = checkbounds, kwargs...) tgrad_oop, tgrad_iip = eval_or_rgf.(tgrad_gen; eval_expression, eval_module) - - ___tgrad(u, p, t) = tgrad_oop(u, p, t) - ___tgrad(J, u, p, t) = tgrad_iip(J, u, p, t) - _tgrad = ___tgrad + _tgrad = GeneratedFunctionWrapper{(2, 3, is_split(sys))}(tgrad_oop, tgrad_iip) else _tgrad = nothing end @@ -354,8 +349,7 @@ function DiffEqBase.ODEFunction{iip, specialize}(sys::AbstractODESystem, checkbounds = checkbounds, kwargs...) jac_oop, jac_iip = eval_or_rgf.(jac_gen; eval_expression, eval_module) - _jac(u, p, t) = jac_oop(u, p, t) - _jac(J, u, p, t) = jac_iip(J, u, p, t) + _jac = GeneratedFunctionWrapper{(2, 3, is_split(sys))}(jac_oop, jac_iip) else _jac = nothing end @@ -435,8 +429,7 @@ function DiffEqBase.DAEFunction{iip}(sys::AbstractODESystem, dvs = unknowns(sys) expression_module = eval_module, checkbounds = checkbounds, kwargs...) f_oop, f_iip = eval_or_rgf.(f_gen; eval_expression, eval_module) - f(du, u, p, t) = f_oop(du, u, p, t) - f(out, du, u, p, t) = f_iip(out, du, u, p, t) + f = GeneratedFunctionWrapper{(3, 4, is_split(sys))}(f_oop, f_iip) if jac jac_gen = generate_dae_jacobian(sys, dvs, ps; @@ -446,8 +439,7 @@ function DiffEqBase.DAEFunction{iip}(sys::AbstractODESystem, dvs = unknowns(sys) checkbounds = checkbounds, kwargs...) jac_oop, jac_iip = eval_or_rgf.(jac_gen; eval_expression, eval_module) - _jac(du, u, p, ˍ₋gamma, t) = jac_oop(du, u, p, ˍ₋gamma, t) - _jac(J, du, u, p, ˍ₋gamma, t) = jac_iip(J, du, u, p, ˍ₋gamma, t) + _jac = GeneratedFunctionWrapper{(3, 5, is_split(sys))}(jac_oop, jac_iip) else _jac = nothing end @@ -496,8 +488,7 @@ function DiffEqBase.DDEFunction{iip}(sys::AbstractODESystem, dvs = unknowns(sys) expression_module = eval_module, checkbounds = checkbounds, kwargs...) f_oop, f_iip = eval_or_rgf.(f_gen; eval_expression, eval_module) - f(u, h, p, t) = f_oop(u, h, p, t) - f(du, u, h, p, t) = f_iip(du, u, h, p, t) + f = GeneratedFunctionWrapper{(3, 4, is_split(sys))}(f_oop, f_iip) DDEFunction{iip}(f; sys = sys, initialization_data) end @@ -521,14 +512,12 @@ function DiffEqBase.SDDEFunction{iip}(sys::AbstractODESystem, dvs = unknowns(sys expression_module = eval_module, checkbounds = checkbounds, kwargs...) f_oop, f_iip = eval_or_rgf.(f_gen; eval_expression, eval_module) - f(u, h, p, t) = f_oop(u, h, p, t) - f(du, u, h, p, t) = f_iip(du, u, h, p, t) + f = GeneratedFunctionWrapper{(3, 4, is_split(sys))}(f_oop, f_iip) g_gen = generate_diffusion_function(sys, dvs, ps; expression = Val{true}, isdde = true, kwargs...) g_oop, g_iip = eval_or_rgf.(g_gen; eval_expression, eval_module) - g(u, h, p, t) = g_oop(u, h, p, t) - g(du, u, h, p, t) = g_iip(du, u, h, p, t) + g = GeneratedFunctionWrapper{(3, 4, is_split(sys))}(g_oop, g_iip) SDDEFunction{iip}(f, g; sys = sys, initialization_data) end @@ -549,13 +538,6 @@ variable and parameter vectors, respectively. """ struct ODEFunctionExpr{iip, specialize} end -struct ODEFunctionClosure{O, I} <: Function - f_oop::O - f_iip::I -end -(f::ODEFunctionClosure)(u, p, t) = f.f_oop(u, p, t) -(f::ODEFunctionClosure)(du, u, p, t) = f.f_iip(du, u, p, t) - function ODEFunctionExpr{iip, specialize}(sys::AbstractODESystem, dvs = unknowns(sys), ps = parameters(sys), u0 = nothing; version = nothing, tgrad = false, @@ -572,13 +554,14 @@ function ODEFunctionExpr{iip, specialize}(sys::AbstractODESystem, dvs = unknowns f_oop, f_iip = generate_function(sys, dvs, ps; expression = Val{true}, kwargs...) fsym = gensym(:f) - _f = :($fsym = $ODEFunctionClosure($f_oop, $f_iip)) + _f = :($fsym = $(GeneratedFunctionWrapper{(2, 3, is_split(sys))})($f_oop, $f_iip)) tgradsym = gensym(:tgrad) if tgrad tgrad_oop, tgrad_iip = generate_tgrad(sys, dvs, ps; simplify = simplify, expression = Val{true}, kwargs...) - _tgrad = :($tgradsym = $ODEFunctionClosure($tgrad_oop, $tgrad_iip)) + _tgrad = :($tgradsym = $(GeneratedFunctionWrapper{(2, 3, is_split(sys))})( + $tgrad_oop, $tgrad_iip)) else _tgrad = :($tgradsym = nothing) end @@ -588,7 +571,8 @@ function ODEFunctionExpr{iip, specialize}(sys::AbstractODESystem, dvs = unknowns jac_oop, jac_iip = generate_jacobian(sys, dvs, ps; sparse = sparse, simplify = simplify, expression = Val{true}, kwargs...) - _jac = :($jacsym = $ODEFunctionClosure($jac_oop, $jac_iip)) + _jac = :($jacsym = $(GeneratedFunctionWrapper{(2, 3, is_split(sys))})( + $jac_oop, $jac_iip)) else _jac = :($jacsym = nothing) end @@ -647,13 +631,6 @@ variable and parameter vectors, respectively. """ struct DAEFunctionExpr{iip} end -struct DAEFunctionClosure{O, I} <: Function - f_oop::O - f_iip::I -end -(f::DAEFunctionClosure)(du, u, p, t) = f.f_oop(du, u, p, t) -(f::DAEFunctionClosure)(out, du, u, p, t) = f.f_iip(out, du, u, p, t) - function DAEFunctionExpr{iip}(sys::AbstractODESystem, dvs = unknowns(sys), ps = parameters(sys), u0 = nothing; version = nothing, tgrad = false, @@ -667,7 +644,7 @@ function DAEFunctionExpr{iip}(sys::AbstractODESystem, dvs = unknowns(sys), f_oop, f_iip = generate_function(sys, dvs, ps; expression = Val{true}, implicit_dae = true, kwargs...) fsym = gensym(:f) - _f = :($fsym = $DAEFunctionClosure($f_oop, $f_iip)) + _f = :($fsym = $(GeneratedFunctionWrapper{(3, 4, is_split(sys))})($f_oop, $f_iip)) ex = quote $_f ODEFunction{$iip}($fsym) @@ -708,6 +685,7 @@ function SymbolicTstops( expression = Val{true}, p_start = 1, p_end = length(rps), add_observed = false, force_SA = true) tstops = eval_or_rgf(tstops; eval_expression, eval_module) + tstops = GeneratedFunctionWrapper{(1, 3, is_split(sys))}(tstops, nothing) return SymbolicTstops(tstops) end diff --git a/src/systems/diffeqs/odesystem.jl b/src/systems/diffeqs/odesystem.jl index 76fcb9b823..2c4dc04622 100644 --- a/src/systems/diffeqs/odesystem.jl +++ b/src/systems/diffeqs/odesystem.jl @@ -521,9 +521,14 @@ function build_explicit_observed_function(sys, ts; output_type, mkarray, try_namespaced = true, expression = Val{true}) if fns isa Tuple oop, iip = eval_or_rgf.(fns; eval_expression, eval_module) - return return_inplace ? (oop, iip) : oop + f = GeneratedFunctionWrapper{( + p_start, length(args) - length(ps) + 1, is_split(sys))}(oop, iip) + return return_inplace ? (f, f) : f else - return eval_or_rgf(fns; eval_expression, eval_module) + f = eval_or_rgf(fns; eval_expression, eval_module) + f = GeneratedFunctionWrapper{( + p_start, length(args) - length(ps) + 1, is_split(sys))}(f, nothing) + return f end end diff --git a/src/systems/diffeqs/sdesystem.jl b/src/systems/diffeqs/sdesystem.jl index 00265cfc67..bc3fd6b73e 100644 --- a/src/systems/diffeqs/sdesystem.jl +++ b/src/systems/diffeqs/sdesystem.jl @@ -604,18 +604,14 @@ function DiffEqBase.SDEFunction{iip, specialize}(sys::SDESystem, dvs = unknowns( kwargs...) g_oop, g_iip = eval_or_rgf.(g_gen; eval_expression, eval_module) - f(u, p, t) = f_oop(u, p, t) - f(du, u, p, t) = f_iip(du, u, p, t) - g(u, p, t) = g_oop(u, p, t) - g(du, u, p, t) = g_iip(du, u, p, t) + f = GeneratedFunctionWrapper{(2, 3, is_split(sys))}(f_oop, f_iip) + g = GeneratedFunctionWrapper{(2, 3, is_split(sys))}(g_oop, g_iip) if tgrad tgrad_gen = generate_tgrad(sys, dvs, ps; expression = Val{true}, kwargs...) tgrad_oop, tgrad_iip = eval_or_rgf.(tgrad_gen; eval_expression, eval_module) - - _tgrad(u, p, t) = tgrad_oop(u, p, t) - _tgrad(J, u, p, t) = tgrad_iip(J, u, p, t) + _tgrad = GeneratedFunctionWrapper{(2, 3, is_split(sys))}(tgrad_oop, tgrad_iip) else _tgrad = nothing end @@ -625,8 +621,7 @@ function DiffEqBase.SDEFunction{iip, specialize}(sys::SDESystem, dvs = unknowns( sparse = sparse, kwargs...) jac_oop, jac_iip = eval_or_rgf.(jac_gen; eval_expression, eval_module) - _jac(u, p, t) = jac_oop(u, p, t) - _jac(J, u, p, t) = jac_iip(J, u, p, t) + _jac = GeneratedFunctionWrapper{(2, 3, is_split(sys))}(jac_oop, jac_iip) else _jac = nothing end @@ -637,10 +632,8 @@ function DiffEqBase.SDEFunction{iip, specialize}(sys::SDESystem, dvs = unknowns( Wfact_oop, Wfact_iip = eval_or_rgf.(tmp_Wfact; eval_expression, eval_module) Wfact_oop_t, Wfact_iip_t = eval_or_rgf.(tmp_Wfact_t; eval_expression, eval_module) - _Wfact(u, p, dtgamma, t) = Wfact_oop(u, p, dtgamma, t) - _Wfact(W, u, p, dtgamma, t) = Wfact_iip(W, u, p, dtgamma, t) - _Wfact_t(u, p, dtgamma, t) = Wfact_oop_t(u, p, dtgamma, t) - _Wfact_t(W, u, p, dtgamma, t) = Wfact_iip_t(W, u, p, dtgamma, t) + _Wfact = GeneratedFunctionWrapper{(2, 4, is_split(sys))}(Wfact_oop, Wfact_iip) + _Wfact_t = GeneratedFunctionWrapper{(2, 4, is_split(sys))}(Wfact_oop_t, Wfact_iip_t) else _Wfact, _Wfact_t = nothing, nothing end diff --git a/src/systems/discrete_system/discrete_system.jl b/src/systems/discrete_system/discrete_system.jl index cb33f5d339..5af8e24c58 100644 --- a/src/systems/discrete_system/discrete_system.jl +++ b/src/systems/discrete_system/discrete_system.jl @@ -354,8 +354,7 @@ function SciMLBase.DiscreteFunction{iip, specialize}( f_gen = generate_function(sys, dvs, ps; expression = Val{true}, expression_module = eval_module, kwargs...) f_oop, f_iip = eval_or_rgf.(f_gen; eval_expression, eval_module) - f(u, p, t) = f_oop(u, p, t) - f(du, u, p, t) = f_iip(du, u, p, t) + f = GeneratedFunctionWrapper{(2, 3, is_split(sys))}(f_oop, f_iip) if specialize === SciMLBase.FunctionWrapperSpecialize && iip if u0 === nothing || p === nothing || t === nothing diff --git a/src/systems/jumps/jumpsystem.jl b/src/systems/jumps/jumpsystem.jl index 164c38e161..81f0e1371d 100644 --- a/src/systems/jumps/jumpsystem.jl +++ b/src/systems/jumps/jumpsystem.jl @@ -283,7 +283,7 @@ function generate_rate_function(js::JumpSystem, rate) rate = substitute(rate, csubs) end p = reorder_parameters(js) - rf = build_function_wrapper(js, rate, unknowns(js), p..., + build_function_wrapper(js, rate, unknowns(js), p..., get_iv(js), expression = Val{true}) end @@ -302,7 +302,7 @@ end function assemble_vrj( js, vrj, unknowntoid; eval_expression = false, eval_module = @__MODULE__) rate = eval_or_rgf(generate_rate_function(js, vrj.rate); eval_expression, eval_module) - + rate = GeneratedFunctionWrapper{(2, 3, is_split(js))}(rate, nothing) outputvars = (value(affect.lhs) for affect in vrj.affect!) outputidxs = [unknowntoid[var] for var in outputvars] affect = eval_or_rgf(generate_affect_function(js, vrj.affect!, outputidxs); @@ -326,7 +326,7 @@ end function assemble_crj( js, crj, unknowntoid; eval_expression = false, eval_module = @__MODULE__) rate = eval_or_rgf(generate_rate_function(js, crj.rate); eval_expression, eval_module) - + rate = GeneratedFunctionWrapper{(2, 3, is_split(js))}(rate, nothing) outputvars = (value(affect.lhs) for affect in crj.affect!) outputidxs = [unknowntoid[var] for var in outputvars] affect = eval_or_rgf(generate_affect_function(js, crj.affect!, outputidxs); diff --git a/src/systems/nonlinear/nonlinearsystem.jl b/src/systems/nonlinear/nonlinearsystem.jl index 6f5de67fa7..b026543af8 100644 --- a/src/systems/nonlinear/nonlinearsystem.jl +++ b/src/systems/nonlinear/nonlinearsystem.jl @@ -343,16 +343,14 @@ function SciMLBase.NonlinearFunction{iip}(sys::NonlinearSystem, dvs = unknowns(s end f_gen = generate_function(sys, dvs, ps; expression = Val{true}, kwargs...) f_oop, f_iip = eval_or_rgf.(f_gen; eval_expression, eval_module) - f(u, p) = f_oop(u, p) - f(du, u, p) = f_iip(du, u, p) + f = GeneratedFunctionWrapper{(2, 2, is_split(sys))}(f_oop, f_iip) if jac jac_gen = generate_jacobian(sys, dvs, ps; simplify = simplify, sparse = sparse, expression = Val{true}, kwargs...) jac_oop, jac_iip = eval_or_rgf.(jac_gen; eval_expression, eval_module) - _jac(u, p) = jac_oop(u, p) - _jac(J, u, p) = jac_iip(J, u, p) + _jac = GeneratedFunctionWrapper{(2, 2, is_split(sys))}(jac_oop, jac_iip) else _jac = nothing end @@ -397,6 +395,7 @@ function SciMLBase.IntervalNonlinearFunction( f_gen = generate_function( sys, dvs, ps; expression = Val{true}, scalar = true, kwargs...) f = eval_or_rgf(f_gen; eval_expression, eval_module) + f = GeneratedFunctionWrapper{(2, 2, is_split(sys))}(f, nothing) observedfun = ObservedFunctionCache( sys; eval_expression, eval_module, checkbounds = get(kwargs, :checkbounds, false)) @@ -431,13 +430,14 @@ function NonlinearFunctionExpr{iip}(sys::NonlinearSystem, dvs = unknowns(sys), if !iscomplete(sys) error("A completed `NonlinearSystem` is required. Call `complete` or `structural_simplify` on the system before creating a `NonlinearFunctionExpr`") end - idx = iip ? 2 : 1 - f = generate_function(sys, dvs, ps; expression = Val{true}, kwargs...)[idx] + f_oop, f_iip = generate_function(sys, dvs, ps; expression = Val{true}, kwargs...) + f = :($(GeneratedFunctionWrapper{(2, 2, is_split(sys))})($f_oop, $f_iip)) if jac - _jac = generate_jacobian(sys, dvs, ps; + jac_oop, jac_iip = generate_jacobian(sys, dvs, ps; sparse = sparse, simplify = simplify, - expression = Val{true}, kwargs...)[idx] + expression = Val{true}, kwargs...) + _jac = :($(GeneratedFunctionWrapper{(2, 2, is_split(sys))})($jac_oop, $jac_iip)) else _jac = :nothing end @@ -484,8 +484,7 @@ function IntervalNonlinearFunctionExpr( end f = generate_function(sys, dvs, ps; expression = Val{true}, scalar = true, kwargs...) - - IntervalNonlinearFunction{false}(f; sys = sys) + f = :($(GeneratedFunctionWrapper{2, 2, is_split(sys)})($f, nothing)) ex = quote f = $f @@ -584,7 +583,9 @@ function CacheWriter(sys::AbstractSystem, buffer_types::Vector{TypeT}, DestructuredArgs.(rps)...; p_start = 3, p_end = length(rps) + 2, expression = Val{true}, add_observed = false, extra_assignments = [obs_assigns; body]) - return CacheWriter(eval_or_rgf(fn; eval_expression, eval_module)) + fn = eval_or_rgf(fn; eval_expression, eval_module) + fn = GeneratedFunctionWrapper{(3, 3, is_split(sys))}(fn, nothing) + return CacheWriter(fn) end struct SCCNonlinearFunction{iip} end @@ -603,9 +604,7 @@ function SCCNonlinearFunction{iip}( p_end = length(rps) + length(cachesyms) + 1, add_observed = false, extra_assignments = obs_assignments, expression = Val{true}) f_oop, f_iip = eval_or_rgf.(f_gen; eval_expression, eval_module) - - f(u, p) = f_oop(u, p) - f(resid, u, p) = f_iip(resid, u, p) + f = GeneratedFunctionWrapper{(2, 2, is_split(sys))}(f_oop, f_iip) subsys = NonlinearSystem(_eqs, _dvs, ps; observed = _obs, parameter_dependencies = parameter_dependencies(sys), name = nameof(sys)) From 11e394597098559152a622956aa1e06873d1ee08 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Sun, 16 Feb 2025 18:19:46 +0530 Subject: [PATCH 0955/2176] test: update tests to not wrap parameter vector in tuple --- test/jumpsystem.jl | 4 ++-- test/labelledarrays.jl | 2 +- test/odesystem.jl | 4 ++-- test/sdesystem.jl | 2 +- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/test/jumpsystem.jl b/test/jumpsystem.jl index 4cd5135939..0fd6dc4af0 100644 --- a/test/jumpsystem.jl +++ b/test/jumpsystem.jl @@ -104,8 +104,8 @@ sol = solve(jprob, SSAStepper(); saveat = 1.0) rate2(u, p, t) = 0.01u[2] jump2 = ConstantRateJump(rate2, affect2!) mtjumps = jprob.discrete_jump_aggregation -@test abs(mtjumps.rates[1](u, (p,), tf) - jump1.rate(u, p, tf)) < 10 * eps() -@test abs(mtjumps.rates[2](u, (p,), tf) - jump2.rate(u, p, tf)) < 10 * eps() +@test abs(mtjumps.rates[1](u, p, tf) - jump1.rate(u, p, tf)) < 10 * eps() +@test abs(mtjumps.rates[2](u, p, tf) - jump2.rate(u, p, tf)) < 10 * eps() mtjumps.affects![1](mtintegrator) jump1.affect!(integrator) @test all(integrator.u .== mtintegrator.u) diff --git a/test/labelledarrays.jl b/test/labelledarrays.jl index 75d5de6d56..c9ee7ee50b 100644 --- a/test/labelledarrays.jl +++ b/test/labelledarrays.jl @@ -19,7 +19,7 @@ ff = ODEFunction(de, [x, y, z], [σ, ρ, β], jac = true) a = @SVector [1.0, 2.0, 3.0] b = SLVector(x = 1.0, y = 2.0, z = 3.0) c = [1.0, 2.0, 3.0] -p = (SLVector(σ = 10.0, ρ = 26.0, β = 8 / 3),) +p = SLVector(σ = 10.0, ρ = 26.0, β = 8 / 3) @test ff(a, p, 0.0) isa SVector @test typeof(ff(b, p, 0.0)) <: SLArray @test ff(c, p, 0.0) isa Vector diff --git a/test/odesystem.jl b/test/odesystem.jl index 65ceaabc16..de166ef0a1 100644 --- a/test/odesystem.jl +++ b/test/odesystem.jl @@ -235,7 +235,7 @@ end prob = ODEProblem(ODEFunction{false}(lotka), [1.0, 1.0], (0.0, 1.0), [1.5, 1.0, 3.0, 1.0]) de = complete(modelingtoolkitize(prob)) -ODEFunction(de)(similar(prob.u0), prob.u0, (prob.p,), 0.1) +ODEFunction(de)(similar(prob.u0), prob.u0, prob.p, 0.1) function lotka(du, u, p, t) x = u[1] @@ -247,7 +247,7 @@ end prob = ODEProblem(lotka, [1.0, 1.0], (0.0, 1.0), [1.5, 1.0, 3.0, 1.0]) de = complete(modelingtoolkitize(prob)) -ODEFunction(de)(similar(prob.u0), prob.u0, (prob.p,), 0.1) +ODEFunction(de)(similar(prob.u0), prob.u0, prob.p, 0.1) # automatic unknown detection for DAEs @parameters k₁ k₂ k₃ diff --git a/test/sdesystem.jl b/test/sdesystem.jl index 0a30982bac..b031a2f5ab 100644 --- a/test/sdesystem.jl +++ b/test/sdesystem.jl @@ -24,7 +24,7 @@ noiseeqs = [0.1 * x, @named de = SDESystem(eqs, noiseeqs, tt, [x, y, z], [σ, ρ, β], tspan = (0.0, 10.0)) de = complete(de) f = eval(generate_diffusion_function(de)[1]) -@test f(ones(3), (rand(3),), nothing) == 0.1ones(3) +@test f(ones(3), rand(3), nothing) == 0.1ones(3) f = SDEFunction(de) prob = SDEProblem(de, [1.0, 0.0, 0.0], (0.0, 100.0), [10.0, 26.0, 2.33]) From baefe858e9f34c57c67b0053227ab82e420e98ea Mon Sep 17 00:00:00 2001 From: Christopher Rackauckas Date: Sun, 16 Feb 2025 11:12:28 -0500 Subject: [PATCH 0956/2176] Update Project.toml --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index ce3ece8e9a..3fbf220ef8 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "ModelingToolkit" uuid = "961ee093-0014-501f-94e3-6117800e7a78" authors = ["Yingbo Ma ", "Chris Rackauckas and contributors"] -version = "9.63.0" +version = "9.64.0" [deps] AbstractTrees = "1520ce14-60c1-5f80-bbc7-55ef81b5835c" From 33caa99dfb3196f61eccdb93f5a228cca1b47b58 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 17 Feb 2025 12:53:26 +0530 Subject: [PATCH 0957/2176] fix: handle null state problem in `ReconstructInitializeprob` --- src/systems/nonlinear/initializesystem.jl | 5 +++-- test/initializationsystem.jl | 9 +++++++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/systems/nonlinear/initializesystem.jl b/src/systems/nonlinear/initializesystem.jl index 38cc65daf6..0301276183 100644 --- a/src/systems/nonlinear/initializesystem.jl +++ b/src/systems/nonlinear/initializesystem.jl @@ -322,7 +322,8 @@ function (rip::ReconstructInitializeprob)(srcvalp, dstvalp) if state_values(dstvalp) === nothing return nothing, newp end - T = eltype(state_values(srcvalp)) + srcu0 = state_values(srcvalp) + T = srcu0 === nothing || isempty(srcu0) ? Union{} : eltype(srcu0) if parameter_values(dstvalp) isa MTKParameters if !isempty(newp.tunable) T = promote_type(eltype(newp.tunable), T) @@ -332,7 +333,7 @@ function (rip::ReconstructInitializeprob)(srcvalp, dstvalp) end if T == eltype(state_values(dstvalp)) u0 = state_values(dstvalp) - else + elseif T != Union{} u0 = T.(state_values(dstvalp)) end buf, repack, alias = SciMLStructures.canonicalize(SciMLStructures.Tunable(), newp) diff --git a/test/initializationsystem.jl b/test/initializationsystem.jl index 0990101069..a2348c6647 100644 --- a/test/initializationsystem.jl +++ b/test/initializationsystem.jl @@ -1361,3 +1361,12 @@ end prob = ODEProblem(sys, [], (0.0, 1.0)) @test prob.f.initialization_data !== nothing end + +@testset "`ReconstructInitializeprob` with `nothing` state" begin + @parameters p + @variables x(t) + @mtkbuild sys = ODESystem(x ~ p * t, t) + prob = @test_nowarn ODEProblem(sys, [], (0.0, 1.0), [p => 1.0]) + @test_nowarn remake(prob, p = [p => 1.0]) + @test_nowarn remake(prob, p = [p => ForwardDiff.Dual(1.0)]) +end From a7ffae5fb42f566fb558622d41e146755c9f10ea Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 17 Feb 2025 13:00:14 +0530 Subject: [PATCH 0958/2176] fix: throw when observed variable not present in system --- src/systems/diffeqs/odesystem.jl | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/systems/diffeqs/odesystem.jl b/src/systems/diffeqs/odesystem.jl index 2c4dc04622..2dcb703d96 100644 --- a/src/systems/diffeqs/odesystem.jl +++ b/src/systems/diffeqs/odesystem.jl @@ -481,6 +481,11 @@ function build_explicit_observed_function(sys, ts; newvar = get(ns_map, var, nothing) if newvar !== nothing namespace_subs[var] = newvar + var = newvar + end + if throw && !(var in allsyms) && + (!iscall(var) || operation(var) !== getindex || !(arguments(var)[1] in allsyms)) + Base.throw(ArgumentError("Symbol $var is not present in the system.")) end end ts = fast_substitute(ts, namespace_subs) From 96dd399680c55ece42b6c2e4abc33911883de01f Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 17 Feb 2025 13:53:44 +0530 Subject: [PATCH 0959/2176] fix: handle edge case in ChainRulesCoreExt --- ext/MTKChainRulesCoreExt.jl | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/ext/MTKChainRulesCoreExt.jl b/ext/MTKChainRulesCoreExt.jl index 635f9d12cf..f153ee77de 100644 --- a/ext/MTKChainRulesCoreExt.jl +++ b/ext/MTKChainRulesCoreExt.jl @@ -16,7 +16,12 @@ end function subset_idxs(idxs, portion, template) ntuple(Val(length(template))) do subi - [Base.tail(idx.idx) for idx in idxs if idx.portion == portion && idx.idx[1] == subi] + result = [Base.tail(idx.idx) + for idx in idxs if idx.portion == portion && idx.idx[1] == subi] + if isempty(result) + result = [] + end + result end end From cfc5b099b7e89506cebf816d04d112c85441e4c2 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 17 Feb 2025 13:53:52 +0530 Subject: [PATCH 0960/2176] test: fix AD test --- test/extensions/ad.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/extensions/ad.jl b/test/extensions/ad.jl index 7b5c939b0a..0e72b2b7b7 100644 --- a/test/extensions/ad.jl +++ b/test/extensions/ad.jl @@ -25,7 +25,7 @@ prob = ODEProblem(sys, u0, tspan, ps) sol = solve(prob, Tsit5()) mtkparams = parameter_values(prob) -new_p = rand(10) +new_p = rand(14) gs = gradient(new_p) do new_p new_params = SciMLStructures.replace(SciMLStructures.Tunable(), mtkparams, new_p) new_prob = remake(prob, p = new_params) From 79363b21804a73117534fb7f9dcbb940db01a3ef Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 17 Feb 2025 15:26:33 +0530 Subject: [PATCH 0961/2176] test: try and make FMI test more consistent --- test/fmi/fmi.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/fmi/fmi.jl b/test/fmi/fmi.jl index f3ab3549cf..3db1c3f0e8 100644 --- a/test/fmi/fmi.jl +++ b/test/fmi/fmi.jl @@ -157,7 +157,7 @@ end @testset "v2, CS" begin fmu = loadFMU(joinpath(FMU_DIR, "SimpleAdder.fmu"); type = :CS) @named adder = MTK.FMIComponent( - Val(2); fmu, type = :CS, communication_step_size = 1e-4, + Val(2); fmu, type = :CS, communication_step_size = 1e-6, reinitializealg = BrownFullBasicInit()) @test MTK.isinput(adder.a) @test MTK.isinput(adder.b) From ce1584fa09d008da95c49f86c3c4ecb55940f75e Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 17 Feb 2025 17:58:40 +0530 Subject: [PATCH 0962/2176] fix: check symbolic type in `maketerm` for `Initial` --- src/systems/abstractsystem.jl | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index 8baad025d1..0aed6f81e7 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -669,7 +669,11 @@ end # This is required so `fast_substitute` works function SymbolicUtils.maketerm(::Type{<:BasicSymbolic}, ::Initial, args, meta) - return metadata(Initial()(args...), meta) + val = Initial()(args...) + if symbolic_type(val) == NotSymbolic() + return val + end + return metadata(val, meta) end function add_initialization_parameters(sys::AbstractSystem) From ae4e6f70200221e736bdeea732f50c9c8fbeced2 Mon Sep 17 00:00:00 2001 From: vyudu Date: Mon, 17 Feb 2025 11:44:51 -0500 Subject: [PATCH 0963/2176] add tests --- src/systems/problem_utils.jl | 32 ++++++++++++++++++++------------ test/odesystem.jl | 15 +++++++++++++++ 2 files changed, 35 insertions(+), 12 deletions(-) diff --git a/src/systems/problem_utils.jl b/src/systems/problem_utils.jl index 8541056272..3d5b00c418 100644 --- a/src/systems/problem_utils.jl +++ b/src/systems/problem_utils.jl @@ -687,11 +687,11 @@ function process_SciMLProblem( u0map = to_varmap(u0map, dvs) symbols_to_symbolics!(sys, u0map) - check_keys(sys, u0map) pmap = to_varmap(pmap, ps) symbols_to_symbolics!(sys, pmap) - check_keys(sys, pmap) + + check_inputmap_keys(sys, u0map, pmap) defs = add_toterms(recursive_unwrap(defaults(sys))) cmap, cs = get_cmap(sys) @@ -783,29 +783,37 @@ 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) +function check_inputmap_keys(sys, u0map, pmap) + badvarkeys = Any[] + for k in keys(u0map) if symbolic_type(k) === NotSymbolic() - push!(badkeys, k) + push!(badvarkeys, k) end end - isempty(badkeys) || throw(BadKeyError(collect(badkeys))) + badparamkeys = Any[] + for k in keys(pmap) + if symbolic_type(k) === NotSymbolic() + push!(badparamkeys, k) + end + end + (isempty(badvarkeys) && isempty(badparamkeys)) || throw(InvalidKeyError(collect(badvarkeys), collect(badparamkeys))) 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: + Undefined keys found in the parameter or initial condition maps. Check if symbolic variable names have been reassigned. + The following keys are invalid: """ -struct BadKeyError <: Exception +struct InvalidKeyError <: Exception vars::Any + params::Any end -function Base.showerror(io::IO, e::BadKeyError) +function Base.showerror(io::IO, e::InvalidKeyError) println(io, BAD_KEY_MESSAGE) - println(io, join(e.vars, ", ")) + println(io, "u0map: $(join(e.vars, ", "))") + println(io, "pmap: $(join(e.params, ", "))") end diff --git a/test/odesystem.jl b/test/odesystem.jl index a635c3dad9..7b4d580718 100644 --- a/test/odesystem.jl +++ b/test/odesystem.jl @@ -1626,3 +1626,18 @@ end prob = ODEProblem{false}(lowered_dae_sys; u0_constructor = x -> SVector(x...)) @test prob.u0 isa SVector end + +@testset "input map validation" begin + import ModelingToolkit: InvalidKeyError + @variables x(t) y(t) z(t) + @parameters a b c d + eqs = [D(x) ~ x*a, D(y) ~ y*c, D(z) ~ b + d] + @mtkbuild sys = ODESystem(eqs, t) + pmap = [a => 1, b => 2, c => 3, d => 4, "b" => 2] + u0map = [x => 1, y => 2, z => 3] + @test_throws InvalidKeyError ODEProblem(sys, u0map, (0., 1.), pmap) + + pmap = [a => 1, b => 2, c => 3, d => 4] + u0map = [x => 1, y => 2, z => 3, :0 => 3] + @test_throws InvalidKeyError ODEProblem(sys, u0map, (0., 1.), pmap) +end From 83f572c42a3787a395675bd5a0de4daa5aa30063 Mon Sep 17 00:00:00 2001 From: vyudu Date: Mon, 17 Feb 2025 18:25:45 -0500 Subject: [PATCH 0964/2176] refactor tests --- test/odesystem.jl | 15 --------------- test/problem_validation.jl | 21 +++++++++++++++------ 2 files changed, 15 insertions(+), 21 deletions(-) diff --git a/test/odesystem.jl b/test/odesystem.jl index 62bcf4c355..de166ef0a1 100644 --- a/test/odesystem.jl +++ b/test/odesystem.jl @@ -1673,18 +1673,3 @@ end prob = ODEProblem{false}(lowered_dae_sys; u0_constructor = x -> SVector(x...)) @test prob.u0 isa SVector end - -@testset "input map validation" begin - import ModelingToolkit: InvalidKeyError - @variables x(t) y(t) z(t) - @parameters a b c d - eqs = [D(x) ~ x*a, D(y) ~ y*c, D(z) ~ b + d] - @mtkbuild sys = ODESystem(eqs, t) - pmap = [a => 1, b => 2, c => 3, d => 4, "b" => 2] - u0map = [x => 1, y => 2, z => 3] - @test_throws InvalidKeyError ODEProblem(sys, u0map, (0., 1.), pmap) - - pmap = [a => 1, b => 2, c => 3, d => 4] - u0map = [x => 1, y => 2, z => 3, :0 => 3] - @test_throws InvalidKeyError ODEProblem(sys, u0map, (0., 1.), pmap) -end diff --git a/test/problem_validation.jl b/test/problem_validation.jl index f871327ae8..bce39b51d2 100644 --- a/test/problem_validation.jl +++ b/test/problem_validation.jl @@ -2,6 +2,7 @@ using ModelingToolkit using ModelingToolkit: t_nounits as t, D_nounits as D @testset "Input map validation" begin + import ModelingToolkit: InvalidKeyError, MissingParametersError @variables X(t) @parameters p d eqs = [D(X) ~ p - d*X] @@ -10,16 +11,24 @@ using ModelingToolkit: t_nounits as t, D_nounits as D 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) + @test_throws MissingParametersError 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) + @test_throws InvalidKeyError 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) + @test_throws InvalidKeyError 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) + @variables x(t) y(t) z(t) + @parameters a b c d + eqs = [D(x) ~ x*a, D(y) ~ y*c, D(z) ~ b + d] + @mtkbuild sys = ODESystem(eqs, t) + pmap = [a => 1, b => 2, c => 3, d => 4, "b" => 2] + u0map = [x => 1, y => 2, z => 3] + @test_throws InvalidKeyError ODEProblem(sys, u0map, (0., 1.), pmap) + + pmap = [a => 1, b => 2, c => 3, d => 4] + u0map = [x => 1, y => 2, z => 3, :0 => 3] + @test_throws InvalidKeyError ODEProblem(sys, u0map, (0., 1.), pmap) end From 2a97325d0b4e9b70f3a18de8049d14559504880d Mon Sep 17 00:00:00 2001 From: vyudu Date: Tue, 18 Feb 2025 15:19:38 -0500 Subject: [PATCH 0965/2176] feat: initialization of DiscreteSystem --- .../symbolics_tearing.jl | 114 +++++++++++++----- src/structural_transformation/utils.jl | 55 ++++++--- .../discrete_system/discrete_system.jl | 8 +- src/variables.jl | 16 ++- test/discrete_system.jl | 68 ++++++++--- 5 files changed, 193 insertions(+), 68 deletions(-) diff --git a/src/structural_transformation/symbolics_tearing.jl b/src/structural_transformation/symbolics_tearing.jl index a166bb064b..bbb6853a7c 100644 --- a/src/structural_transformation/symbolics_tearing.jl +++ b/src/structural_transformation/symbolics_tearing.jl @@ -341,14 +341,18 @@ in order to properly generate the difference equations. In the system x(k) ~ x(k-1) + x(k-2), becomes Shift(t, 1)(x(t)) ~ x(t) + Shift(t, -1)(x(t)) -The lowest-order term is Shift(t, k)(x(t)), instead of x(t). -As such we actually want dummy variables for the k-1 lowest order terms instead of the k-1 highest order terms. +The lowest-order term is Shift(t, k)(x(t)), instead of x(t). As such we actually want +dummy variables for the k-1 lowest order terms instead of the k-1 highest order terms. Shift(t, -1)(x(t)) -> x\_{t-1}(t) -Since Shift(t, -1)(x) is not a derivative, it is directly substituted in `fullvars`. No equation or variable is added for it. +Since Shift(t, -1)(x) is not a derivative, it is directly substituted in `fullvars`. +No equation or variable is added for it. -For ODESystems D(D(D(x))) in equations is recursively substituted as D(x) ~ x_t, D(x_t) ~ x_tt, etc. The analogue for discrete systems, Shift(t, 1)(Shift(t,1)(Shift(t,1)(Shift(t, -3)(x(t))))) does not actually appear. So `total_sub` in generate_system_equations` is directly initialized with all of the lowered variables `Shift(t, -3)(x) -> x_t-3(t)`, etc. +For ODESystems D(D(D(x))) in equations is recursively substituted as D(x) ~ x_t, D(x_t) ~ x_tt, etc. +The analogue for discrete systems, Shift(t, 1)(Shift(t,1)(Shift(t,1)(Shift(t, -3)(x(t))))) +does not actually appear. So `total_sub` in generate_system_equations` is directly +initialized with all of the lowered variables `Shift(t, -3)(x) -> x_t-3(t)`, etc. =# """ Generate new derivative variables for the system. @@ -358,7 +362,7 @@ Effects on the system structure: - neweqs: add the identity equations for the new variables, D(x) ~ x_t - graph: update graph with the new equations and variables, and their connections - solvable_graph: -- var_eq_matching: match D(x) to the added identity equation +- var_eq_matching: match D(x) to the added identity equation D(x) ~ x_t """ function generate_derivative_variables!(ts::TearingState, neweqs, var_eq_matching; mm = nothing, iv = nothing, D = nothing) @unpack fullvars, sys, structure = ts @@ -406,7 +410,7 @@ function generate_derivative_variables!(ts::TearingState, neweqs, var_eq_matchin end """ -Check if there's `D(x) = x_t` already. +Check if there's `D(x) ~ x_t` already. """ function find_duplicate_dd(dv, solvable_graph, diff_to_var, linear_eqs, mm) for eq in 𝑑neighbors(solvable_graph, dv) @@ -427,6 +431,10 @@ function find_duplicate_dd(dv, solvable_graph, diff_to_var, linear_eqs, mm) return nothing end +""" +Add a dummy derivative variable x_t corresponding to symbolic variable D(x) +which has index dv in `fullvars`. Return the new index of x_t. +""" function add_dd_variable!(s::SystemStructure, fullvars, x_t, dv) push!(fullvars, simplify_shifts(x_t)) v_t = length(fullvars) @@ -439,7 +447,11 @@ function add_dd_variable!(s::SystemStructure, fullvars, x_t, dv) v_t end -# dv = index of D(x), v_t = index of x_t +""" +Add the equation D(x) - x_t ~ 0 to `neweqs`. `dv` and `v_t` are the indices +of the higher-order derivative variable and the newly-introduced dummy +derivative variable. Return the index of the new equation in `neweqs`. +""" function add_dd_equation!(s::SystemStructure, neweqs, eq, dv, v_t) push!(neweqs, eq) add_vertex!(s.graph, SRC) @@ -452,8 +464,33 @@ function add_dd_equation!(s::SystemStructure, neweqs, eq, dv, v_t) end """ -Solve the solvable equations of the system and generate differential (or discrete) -equations in terms of the selected states. +Solve the equations in `neweqs` to obtain the final equations of the +system. + +For each equation of `neweqs`, do one of the following: + 1. If the equation is solvable for a differentiated variable D(x), + then solve for D(x), and add D(x) ~ sol as a differential equation + of the system. + 2. If the equation is solvable for an un-differentiated variable x, + solve for x and then add x ~ sol as a solved equation. These will + become observables. + 3. If the equation is not solvable, add it as an algebraic equation. + +Solved equations are added to `total_sub`. Occurrences of differential +or solved variables on the RHS of the final equations will get substituted. +The topological sort of the equations ensures that variables are solved for +before they appear in equations. + +Reorder the equations and unknowns to be: + [diffeqs; ...] + [diffvars; ...] +such that the mass matrix is: + [I 0 + 0 0]. + +Order the new equations and variables such that the differential equations +and variables come first. Return the new equations, the solved equations, +the new orderings, and the number of solved variables and equations. """ function generate_system_equations!(state::TearingState, neweqs, var_eq_matching; simplify = false, iv = nothing, D = nothing) @unpack fullvars, sys, structure = state @@ -550,6 +587,9 @@ function generate_system_equations!(state::TearingState, neweqs, var_eq_matching return neweqs, solved_eqs, eq_ordering, var_ordering, length(solved_vars), length(solved_vars_set) end +""" +Occurs when a variable D(x) occurs in a non-differential system. +""" struct UnexpectedDifferentialError eq::Equation end @@ -558,12 +598,20 @@ function Base.showerror(io::IO, err::UnexpectedDifferentialError) error("Differential found in a non-differential system. Likely this is a bug in the construction of an initialization system. Please report this issue with a reproducible example. Offending equation: $(err.eq)") end +""" +Generate a first-order differential equation whose LHS is `dx`. + +`var` and `dx` represent the same variable, but `var` may be a higher-order differential and `dx` is always first-order. For example, if `var` is D(D(x)), then `dx` would be `D(x_t)`. Solve `eq` for `var`, substitute previously solved variables, and return the differential equation. +""" function make_differential_equation(var, dx, eq, total_sub) dx ~ simplify_shifts(Symbolics.fixpoint_sub( Symbolics.symbolic_linear_solve(eq, var), total_sub; operator = ModelingToolkit.Shift)) end +""" +Generate an algebraic equation. Substitute solved variables into `eq` and return the equation. +""" function make_algebraic_equation(eq, total_sub) rhs = eq.rhs if !(eq.lhs isa Number && eq.lhs == 0) @@ -572,6 +620,9 @@ function make_algebraic_equation(eq, total_sub) 0 ~ simplify_shifts(Symbolics.fixpoint_sub(rhs, total_sub)) end +""" +Solve equation `eq` for `var`, substitute previously solved variables, and return the solved equation. +""" function make_solved_equation(var, eq, total_sub; simplify = false) residual = eq.lhs - eq.rhs a, b, islinear = linear_expansion(residual, var) @@ -591,17 +642,13 @@ function make_solved_equation(var, eq, total_sub; simplify = false) end """ -Reorder the equations and unknowns to be: - [diffeqs; ...] - [diffvars; ...] -such that the mass matrix is: - [I 0 - 0 0]. - -Update the state to account for the new ordering and equations. +Given the ordering returned by `generate_system_equations!`, update the +tearing state to account for the new order. Permute the variables and equations. +Eliminate the solved variables and equations from the graph and permute the +graph's vertices to account for the new variable/equation ordering. """ # TODO: BLT sorting -function reorder_vars!(state::TearingState, var_eq_matching, eq_ordering, var_ordering, nelim_eq, nelim_var) +function reorder_vars!(state::TearingState, var_eq_matching, eq_ordering, var_ordering, nsolved_eq, nsolved_var) @unpack solvable_graph, var_to_diff, eq_to_diff, graph = state.structure eqsperm = zeros(Int, nsrcs(graph)) @@ -616,7 +663,7 @@ function reorder_vars!(state::TearingState, var_eq_matching, eq_ordering, var_or # Contract the vertices in the structure graph to make the structure match # the new reality of the system we've just created. new_graph = contract_variables(graph, var_eq_matching, varsperm, eqsperm, - nelim_eq, nelim_var) + nsolved_eq, nsolved_var) new_var_to_diff = complete(DiffGraph(length(var_ordering))) for (v, d) in enumerate(var_to_diff) @@ -643,7 +690,7 @@ function reorder_vars!(state::TearingState, var_eq_matching, eq_ordering, var_or end """ -Set the system equations, unknowns, observables post-tearing. +Update the system equations, unknowns, and observables after simplification. """ function update_simplified_system!(state::TearingState, neweqs, solved_eqs, dummy_sub, var_eq_matching, extra_unknowns; cse_hack = true, array_hack = true) @@ -685,16 +732,10 @@ function update_simplified_system!(state::TearingState, neweqs, solved_eqs, dumm sys = schedule(sys) end -# Terminology and Definition: -# A general DAE is in the form of `F(u'(t), u(t), p, t) == 0`. We can -# characterize variables in `u(t)` into two classes: differential variables -# (denoted `v(t)`) and algebraic variables (denoted `z(t)`). Differential -# variables are marked as `SelectedState` and they are differentiated in the -# DAE system, i.e. `v'(t)` are all the variables in `u'(t)` that actually -# appear in the system. Algebraic variables are variables that are not -# differential variables. -# Give the order of the variable indexed by dv +""" +Give the order of the variable indexed by dv. +""" function var_order(dv, diff_to_var) order = 0 while (dv′ = diff_to_var[dv]) !== nothing @@ -704,6 +745,21 @@ function var_order(dv, diff_to_var) order, dv end +""" +Main internal function for structural simplification for DAE systems and discrete systems. +Generate dummy derivative variables, new equations in terms of variables, return updated +system and tearing state. + +Terminology and Definition: + +A general DAE is in the form of `F(u'(t), u(t), p, t) == 0`. We can +characterize variables in `u(t)` into two classes: differential variables +(denoted `v(t)`) and algebraic variables (denoted `z(t)`). Differential +variables are marked as `SelectedState` and they are differentiated in the +DAE system, i.e. `v'(t)` are all the variables in `u'(t)` that actually +appear in the system. Algebraic variables are variables that are not +differential variables. +""" function tearing_reassemble(state::TearingState, var_eq_matching, full_var_eq_matching = nothing; simplify = false, mm = nothing, cse_hack = true, array_hack = true) extra_vars = Int[] diff --git a/src/structural_transformation/utils.jl b/src/structural_transformation/utils.jl index f64a8da132..2753446a94 100644 --- a/src/structural_transformation/utils.jl +++ b/src/structural_transformation/utils.jl @@ -449,7 +449,12 @@ end ### Misc ### -# For discrete variables. Turn Shift(t, k)(x(t)) into xₜ₋ₖ(t) +""" +Handle renaming variable names for discrete structural simplification. Three cases: +- positive shift: do nothing +- zero shift: x(t) => Shift(t, 0)(x(t)) +- negative shift: rename the variable +""" function lower_shift_varname(var, iv) op = operation(var) op isa Shift || return Shift(iv, 0)(var, true) # hack to prevent simplification of x(t) - x(t) @@ -460,30 +465,46 @@ function lower_shift_varname(var, iv) end end -function shift2term(var) +""" +Rename a Shift variable with negative shift, Shift(t, k)(x(t)) to xₜ₋ₖ(t). +""" +function shift2term(var) backshift = operation(var).steps iv = operation(var).t - num = join(Char(0x2080 + d) for d in reverse!(digits(-backshift))) - ds = join([Char(0x209c), Char(0x208b), num]) - #ds = "$iv-$(-backshift)" - #d_separator = 'ˍ' - - if ModelingToolkit.isoperator(var, ModelingToolkit.Shift) - O = only(arguments(var)) - oldop = operation(O) - newname = Symbol(string(nameof(oldop)), ds) - else - O = var - oldop = operation(var) - varname = split(string(nameof(oldop)), d_separator)[1] - newname = Symbol(varname, d_separator, ds) - end + num = join(Char(0x2080 + d) for d in reverse!(digits(-backshift))) # subscripted number, e.g. ₁ + ds = join([Char(0x209c), Char(0x208b), num]) + # Char(0x209c) = ₜ + # Char(0x208b) = ₋ (subscripted minus) + + O = only(arguments(var)) + oldop = operation(O) + newname = Symbol(string(nameof(oldop)), ds) + newvar = maketerm(typeof(O), Symbolics.rename(oldop, newname), Symbolics.children(O), Symbolics.metadata(O)) newvar = setmetadata(newvar, Symbolics.VariableSource, (:variables, newname)) newvar = setmetadata(newvar, ModelingToolkit.VariableUnshifted, O) + newvar = setmetadata(newvar, ModelingToolkit.VariableShift, backshift) return newvar end +function term2shift(var) + var = Symbolics.unwrap(var) + name = Symbolics.getname(var) + O = only(arguments(var)) + oldop = operation(O) + iv = only(arguments(x)) + # Split on ₋ + if occursin(Char(0x208b), name) + substrings = split(name, Char(0x208b)) + shift = last(split(name, Char(0x208b))) + newname = join(substrings[1:end-1])[1:end-1] + newvar = maketerm(typeof(O), Symbolics.rename(oldop, newname), Symbolics.children(O), Symbolics.metadata(O)) + return Shift(iv, -shift)(newvar) + else + return var + end +end + function isdoubleshift(var) return ModelingToolkit.isoperator(var, ModelingToolkit.Shift) && ModelingToolkit.isoperator(arguments(var)[1], ModelingToolkit.Shift) diff --git a/src/systems/discrete_system/discrete_system.jl b/src/systems/discrete_system/discrete_system.jl index 9a9ac47853..40bf632339 100644 --- a/src/systems/discrete_system/discrete_system.jl +++ b/src/systems/discrete_system/discrete_system.jl @@ -270,15 +270,19 @@ function shift_u0map_forward(sys::DiscreteSystem, u0map, defs) v = u0map[k] if !((op = operation(k)) isa Shift) error("Initial conditions must be for the past state of the unknowns. Instead of providing the condition for $k, provide the condition for $(Shift(iv, -1)(k)).") + elseif op.steps > 0 + error("Initial conditions must be for the past state of the unknowns. Instead of providing the condition for $k, provide the condition for $(Shift(iv, -1)(only(arguments(k)))).") end + updated[Shift(iv, op.steps + 1)(arguments(k)[1])] = v end for var in unknowns(sys) op = operation(var) - haskey(updated, var) && continue root = getunshifted(var) + shift = getshift(var) isnothing(root) && continue - haskey(defs, root) || error("Initial condition for $root not provided.") + (haskey(updated, Shift(iv, shift)(root)) || haskey(updated, var)) && continue + haskey(defs, root) || error("Initial condition for $var not provided.") updated[var] = defs[root] end return updated diff --git a/src/variables.jl b/src/variables.jl index a29a119607..4e13ad2c5d 100644 --- a/src/variables.jl +++ b/src/variables.jl @@ -6,7 +6,9 @@ struct VariableOutput end struct VariableIrreducible end struct VariableStatePriority end struct VariableMisc end +# Metadata for renamed shift variables xₜ₋₁ struct VariableUnshifted end +struct VariableShift end Symbolics.option_to_metadata_type(::Val{:unit}) = VariableUnit Symbolics.option_to_metadata_type(::Val{:connect}) = VariableConnectType Symbolics.option_to_metadata_type(::Val{:input}) = VariableInput @@ -15,6 +17,7 @@ Symbolics.option_to_metadata_type(::Val{:irreducible}) = VariableIrreducible Symbolics.option_to_metadata_type(::Val{:state_priority}) = VariableStatePriority Symbolics.option_to_metadata_type(::Val{:misc}) = VariableMisc Symbolics.option_to_metadata_type(::Val{:unshifted}) = VariableUnshifted +Symbolics.option_to_metadata_type(::Val{:shift}) = VariableShift """ dump_variable_metadata(var) @@ -97,7 +100,7 @@ struct Stream <: AbstractConnectType end # special stream connector Get the connect type of x. See also [`hasconnect`](@ref). """ -getconnect(x) = getconnect(unwrap(x)) +getconnect(x::Num) = getconnect(unwrap(x)) getconnect(x::Symbolic) = Symbolics.getmetadata(x, VariableConnectType, nothing) """ hasconnect(x) @@ -264,7 +267,7 @@ end end struct IsHistory end -ishistory(x) = ishistory(unwrap(x)) +ishistory(x::Num) = ishistory(unwrap(x)) ishistory(x::Symbolic) = getmetadata(x, IsHistory, false) hist(x, t) = wrap(hist(unwrap(x), t)) function hist(x::Symbolic, t) @@ -575,7 +578,7 @@ end Fetch any miscellaneous data associated with symbolic variable `x`. See also [`hasmisc(x)`](@ref). """ -getmisc(x) = getmisc(unwrap(x)) +getmisc(x::Num) = getmisc(unwrap(x)) getmisc(x::Symbolic) = Symbolics.getmetadata(x, VariableMisc, nothing) """ hasmisc(x) @@ -594,7 +597,7 @@ setmisc(x, miscdata) = setmetadata(x, VariableMisc, miscdata) Fetch the unit associated with variable `x`. This function is a metadata getter for an individual variable, while `get_unit` is used for unit inference on more complicated sdymbolic expressions. """ -getunit(x) = getunit(unwrap(x)) +getunit(x::Num) = getunit(unwrap(x)) getunit(x::Symbolic) = Symbolics.getmetadata(x, VariableUnit, nothing) """ hasunit(x) @@ -603,5 +606,8 @@ Check if the variable `x` has a unit. """ hasunit(x) = getunit(x) !== nothing -getunshifted(x) = getunshifted(unwrap(x)) +getunshifted(x::Num) = getunshifted(unwrap(x)) getunshifted(x::Symbolic) = Symbolics.getmetadata(x, VariableUnshifted, nothing) + +getshift(x::Num) = getshift(unwrap(x)) +getshift(x::Symbolic) = Symbolics.getmetadata(x, VariableShift, 0) diff --git a/test/discrete_system.jl b/test/discrete_system.jl index b63f66d4e4..fa7ba993e6 100644 --- a/test/discrete_system.jl +++ b/test/discrete_system.jl @@ -220,21 +220,6 @@ sol = solve(prob, FunctionMap()) @test reduce(vcat, sol.u) == 1:11 -# test that default values apply to the entire history -@variables x(t) = 1.0 -@mtkbuild de = DiscreteSystem([x ~ x(k - 1) + x(k - 2)], t) -prob = DiscreteProblem(de, [], (0, 10)) -@test prob[x] == 2.0 -@test prob[x(k - 1)] == 1.0 - -# must provide initial conditions for history -@test_throws ErrorException DiscreteProblem(de, [x => 2.0], (0, 10)) - -# initial values only affect _that timestep_, not the entire history -prob = DiscreteProblem(de, [x(k - 1) => 2.0], (0, 10)) -@test prob[x] == 3.0 -@test prob[x(k - 1)] == 2.0 - # Issue#2585 getdata(buffer, t) = buffer[mod1(Int(t), length(buffer))] @register_symbolic getdata(buffer::Vector, t) @@ -272,6 +257,7 @@ k = ShiftIndex(t) @named sys = DiscreteSystem([x ~ x^2 + y^2, y ~ x(k - 1) + y(k - 1)], t) @test_throws ["algebraic equations", "not yet supported"] structural_simplify(sys) + @testset "Passing `nothing` to `u0`" begin @variables x(t) = 1 k = ShiftIndex() @@ -279,3 +265,55 @@ k = ShiftIndex(t) prob = @test_nowarn DiscreteProblem(sys, nothing, (0.0, 1.0)) @test_nowarn solve(prob, FunctionMap()) end + +@testset "Initialization" begin + # test that default values apply to the entire history + @variables x(t) = 1.0 + @mtkbuild de = DiscreteSystem([x ~ x(k - 1) + x(k - 2)], t) + prob = DiscreteProblem(de, [], (0, 10)) + @test prob[x] == 2.0 + @test prob[x(k - 1)] == 1.0 + + # must provide initial conditions for history + @test_throws ErrorException DiscreteProblem(de, [x => 2.0], (0, 10)) + @test_throws ErrorException DiscreteProblem(de, [x(k+1) => 2.], (0, 10)) + + # initial values only affect _that timestep_, not the entire history + prob = DiscreteProblem(de, [x(k - 1) => 2.0], (0, 10)) + @test prob[x] == 3.0 + @test prob[x(k - 1)] == 2.0 + @test prob[xₜ₋₁] == 2.0 + + # Test initial assignment with lowered variable + @variables xₜ₋₁(t) + prob = DiscreteProblem(de, [xₜ₋₁(k-1) => 4.0], (0, 10)) + @test prob[x(k-1)] == prob[xₜ₋₁] == 1.0 + @test prob[x] == 5. + + # Test missing initial throws error + @variables x(t) + @mtkbuild de = DiscreteSystem([x ~ x(k-1) + x(k-2)*x(k-3)], t) + @test_throws ErrorException prob = DiscreteProblem(de, [x(k-3) => 2.], (0, 10)) + @test_throws ErrorException prob = DiscreteProblem(de, [x(k-3) => 2., x(k-1) => 3.], (0, 10)) + + # Test non-assigned initials are given default value + @variables x(t) = 2. + prob = DiscreteProblem(de, [x(k-3) => 12.], (0, 10)) + @test prob[x] == 26.0 + @test prob[x(k-1)] == 2.0 + @test prob[x(k-2)] == 2.0 + + # Elaborate test + eqs = [x ~ x(k-1) + z(k-2), + z ~ x(k-2) * x(k-3) - z(k-1)^2] + @mtkbuild de = DiscreteSystem(eqs, t) + @variables xₜ₋₂(t) zₜ₋₁(t) + u0 = [x(k-1) => 3, + xₜ₋₂(k-1) => 4, + x(k-2) => 1, + z(k-1) => 5, + zₜ₋₁(k-1) => 12] + prob = DiscreteProblem(de, u0, (0, 10)) + @test prob[x] == 15 + @test prob[z] == -21 +end From fc2a309f3d9f8e607329b9ee640672ca4f872eb3 Mon Sep 17 00:00:00 2001 From: vyudu Date: Wed, 19 Feb 2025 10:12:41 -0800 Subject: [PATCH 0966/2176] fix tests and shift2term --- src/structural_transformation/utils.jl | 31 ++++++------------- .../discrete_system/discrete_system.jl | 7 +++-- test/discrete_system.jl | 13 ++++++-- 3 files changed, 25 insertions(+), 26 deletions(-) diff --git a/src/structural_transformation/utils.jl b/src/structural_transformation/utils.jl index 2753446a94..96f7f78f99 100644 --- a/src/structural_transformation/utils.jl +++ b/src/structural_transformation/utils.jl @@ -469,16 +469,21 @@ end Rename a Shift variable with negative shift, Shift(t, k)(x(t)) to xₜ₋ₖ(t). """ function shift2term(var) - backshift = operation(var).steps - iv = operation(var).t + op = operation(var) + iv = op.t + arg = only(arguments(var)) + is_lowered = !isnothing(ModelingToolkit.getunshifted(arg)) + + backshift = is_lowered ? op.steps + ModelingToolkit.getshift(arg) : op.steps + num = join(Char(0x2080 + d) for d in reverse!(digits(-backshift))) # subscripted number, e.g. ₁ ds = join([Char(0x209c), Char(0x208b), num]) # Char(0x209c) = ₜ # Char(0x208b) = ₋ (subscripted minus) - O = only(arguments(var)) + O = is_lowered ? ModelingToolkit.getunshifted(arg) : arg oldop = operation(O) - newname = Symbol(string(nameof(oldop)), ds) + newname = backshift != 0 ? Symbol(string(nameof(oldop)), ds) : Symbol(string(nameof(oldop))) newvar = maketerm(typeof(O), Symbolics.rename(oldop, newname), Symbolics.children(O), Symbolics.metadata(O)) newvar = setmetadata(newvar, Symbolics.VariableSource, (:variables, newname)) @@ -487,24 +492,6 @@ function shift2term(var) return newvar end -function term2shift(var) - var = Symbolics.unwrap(var) - name = Symbolics.getname(var) - O = only(arguments(var)) - oldop = operation(O) - iv = only(arguments(x)) - # Split on ₋ - if occursin(Char(0x208b), name) - substrings = split(name, Char(0x208b)) - shift = last(split(name, Char(0x208b))) - newname = join(substrings[1:end-1])[1:end-1] - newvar = maketerm(typeof(O), Symbolics.rename(oldop, newname), Symbolics.children(O), Symbolics.metadata(O)) - return Shift(iv, -shift)(newvar) - else - return var - end -end - function isdoubleshift(var) return ModelingToolkit.isoperator(var, ModelingToolkit.Shift) && ModelingToolkit.isoperator(arguments(var)[1], ModelingToolkit.Shift) diff --git a/src/systems/discrete_system/discrete_system.jl b/src/systems/discrete_system/discrete_system.jl index 40bf632339..3f8d9e85c6 100644 --- a/src/systems/discrete_system/discrete_system.jl +++ b/src/systems/discrete_system/discrete_system.jl @@ -269,12 +269,15 @@ function shift_u0map_forward(sys::DiscreteSystem, u0map, defs) for k in collect(keys(u0map)) v = u0map[k] if !((op = operation(k)) isa Shift) - error("Initial conditions must be for the past state of the unknowns. Instead of providing the condition for $k, provide the condition for $(Shift(iv, -1)(k)).") + isnothing(getunshifted(k)) && error("Initial conditions must be for the past state of the unknowns. Instead of providing the condition for $k, provide the condition for $(Shift(iv, -1)(k)).") + + updated[Shift(iv, 1)(k)] = v elseif op.steps > 0 error("Initial conditions must be for the past state of the unknowns. Instead of providing the condition for $k, provide the condition for $(Shift(iv, -1)(only(arguments(k)))).") + else + updated[Shift(iv, op.steps + 1)(only(arguments(k)))] = v end - updated[Shift(iv, op.steps + 1)(arguments(k)[1])] = v end for var in unknowns(sys) op = operation(var) diff --git a/test/discrete_system.jl b/test/discrete_system.jl index fa7ba993e6..756e5bca48 100644 --- a/test/discrete_system.jl +++ b/test/discrete_system.jl @@ -282,10 +282,10 @@ end prob = DiscreteProblem(de, [x(k - 1) => 2.0], (0, 10)) @test prob[x] == 3.0 @test prob[x(k - 1)] == 2.0 + @variables xₜ₋₁(t) @test prob[xₜ₋₁] == 2.0 # Test initial assignment with lowered variable - @variables xₜ₋₁(t) prob = DiscreteProblem(de, [xₜ₋₁(k-1) => 4.0], (0, 10)) @test prob[x(k-1)] == prob[xₜ₋₁] == 1.0 @test prob[x] == 5. @@ -298,16 +298,17 @@ end # Test non-assigned initials are given default value @variables x(t) = 2. + @mtkbuild de = DiscreteSystem([x ~ x(k-1) + x(k-2)*x(k-3)], t) prob = DiscreteProblem(de, [x(k-3) => 12.], (0, 10)) @test prob[x] == 26.0 @test prob[x(k-1)] == 2.0 @test prob[x(k-2)] == 2.0 # Elaborate test + @variables xₜ₋₂(t) zₜ₋₁(t) z(t) eqs = [x ~ x(k-1) + z(k-2), z ~ x(k-2) * x(k-3) - z(k-1)^2] @mtkbuild de = DiscreteSystem(eqs, t) - @variables xₜ₋₂(t) zₜ₋₁(t) u0 = [x(k-1) => 3, xₜ₋₂(k-1) => 4, x(k-2) => 1, @@ -316,4 +317,12 @@ end prob = DiscreteProblem(de, u0, (0, 10)) @test prob[x] == 15 @test prob[z] == -21 + + import ModelingToolkit: shift2term + # unknowns(de) = xₜ₋₁, x, zₜ₋₁, xₜ₋₂, z + vars = ModelingToolkit.value.(unknowns(de)) + @test isequal(shift2term(Shift(t, 1)(vars[1])), vars[2]) + @test isequal(shift2term(Shift(t, 1)(vars[4])), vars[1]) + @test isequal(shift2term(Shift(t, -1)(vars[5])), vars[3]) + @test isequal(shift2term(Shift(t, -2)(vars[2])), vars[4]) end From 3d1c2ffa9a33bb9095e885dd866ec17b7407b7c9 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 17 Feb 2025 18:47:32 +0530 Subject: [PATCH 0967/2176] fix: skip inplace check in BifurcationKit --- ext/MTKBifurcationKitExt.jl | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/ext/MTKBifurcationKitExt.jl b/ext/MTKBifurcationKitExt.jl index e5b57efc5a..0b9f104d9b 100644 --- a/ext/MTKBifurcationKitExt.jl +++ b/ext/MTKBifurcationKitExt.jl @@ -97,7 +97,10 @@ function BifurcationKit.BifurcationProblem(nsys::NonlinearSystem, @set! nsys.index_cache = nothing # force usage of a parameter vector instead of `MTKParameters` # Creates F and J functions. ofun = NonlinearFunction(nsys; jac = jac) - F = ofun.f + F = let f = ofun.f + _f(resid, u, p) = (f(resid, u, p); resid) + _f(u, p) = f(u, p) + end J = jac ? ofun.jac : nothing # Converts the input state guess. @@ -136,6 +139,7 @@ function BifurcationKit.BifurcationProblem(nsys::NonlinearSystem, args...; record_from_solution = record_from_solution, J = J, + inplace = true, kwargs...) end From 71fd4b2d9e882b4bdde3e570c6d3c430bdc71252 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 17 Feb 2025 19:42:00 +0530 Subject: [PATCH 0968/2176] build: bump OrdinaryDiffEqNonlinearSolve compat --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 3fbf220ef8..51e8f3acb2 100644 --- a/Project.toml +++ b/Project.toml @@ -128,7 +128,7 @@ OrderedCollections = "1" OrdinaryDiffEq = "6.82.0" OrdinaryDiffEqCore = "1.15.0" OrdinaryDiffEqDefault = "1.2" -OrdinaryDiffEqNonlinearSolve = "1.3.0" +OrdinaryDiffEqNonlinearSolve = "1.5.0" PrecompileTools = "1" REPL = "1" RecursiveArrayTools = "3.26" From 3192166ca0c7763a93b7d60130c9d845869b827f Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 18 Feb 2025 11:53:43 +0530 Subject: [PATCH 0969/2176] fix: don't unnecessarily construct arrays for unknowns --- src/structural_transformation/symbolics_tearing.jl | 1 + 1 file changed, 1 insertion(+) diff --git a/src/structural_transformation/symbolics_tearing.jl b/src/structural_transformation/symbolics_tearing.jl index 92e121c028..bf5c51797b 100644 --- a/src/structural_transformation/symbolics_tearing.jl +++ b/src/structural_transformation/symbolics_tearing.jl @@ -719,6 +719,7 @@ function cse_and_array_hacks(sys, obs, subeqs, unknowns, neweqs; cse = true, arr Symbolics.shape(sym) != Symbolics.Unknown() || continue arg1 = arguments(sym)[1] cnt = get(arr_obs_occurrences, arg1, 0) + cnt == 0 && continue arr_obs_occurrences[arg1] = cnt + 1 end for eq in neweqs From f5176c6d3fd496dee519e45a58ecd9afc337781e Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 18 Feb 2025 12:03:26 +0530 Subject: [PATCH 0970/2176] test: avoid spamming stderr in debugging tests --- Project.toml | 8 +++++--- test/debugging.jl | 13 +++++++------ 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/Project.toml b/Project.toml index 51e8f3acb2..0563e28bcf 100644 --- a/Project.toml +++ b/Project.toml @@ -104,11 +104,11 @@ DynamicQuantities = "^0.11.2, 0.12, 0.13, 1" EnumX = "1.0.4" ExprTools = "0.1.10" Expronicon = "0.8" +FMI = "0.14" FindFirstFunctions = "1" ForwardDiff = "0.10.3" FunctionWrappers = "1.1" FunctionWrappersWrappers = "0.1" -FMI = "0.14" Graphs = "1.5.2" HomotopyContinuation = "2.11" InfiniteOpt = "0.5" @@ -119,6 +119,7 @@ LabelledArrays = "1.3" Latexify = "0.11, 0.12, 0.13, 0.14, 0.15, 0.16" Libdl = "1" LinearAlgebra = "1" +Logging = "1" MLStyle = "0.4.17" ModelingToolkitStandardLibrary = "2.19" NaNMath = "0.3, 1" @@ -143,8 +144,8 @@ SimpleNonlinearSolve = "0.1.0, 1, 2" SparseArrays = "1" SpecialFunctions = "0.7, 0.8, 0.9, 0.10, 1.0, 2" StaticArrays = "0.10, 0.11, 0.12, 1.0" -StochasticDiffEq = "6.72.1" StochasticDelayDiffEq = "1.8.1" +StochasticDiffEq = "6.72.1" SymbolicIndexingInterface = "0.3.37" SymbolicUtils = "3.14" Symbolics = "6.29" @@ -164,6 +165,7 @@ ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210" Ipopt = "b6b21f68-93f8-5de0-b562-5493be1d77c9" Ipopt_jll = "9cc047cb-c261-5740-88fc-0cf96f7bdcc7" JET = "c3a54625-cd67-489e-a8e7-0a5a0ff4e31b" +Logging = "56ddb016-857b-54e1-b83d-db4d58db5568" ModelingToolkitStandardLibrary = "16a59e39-deab-5bd0-87e4-056b12336739" NonlinearSolve = "8913a72c-1f9b-4ce2-8d82-65094dcecaec" Optimization = "7f7a1694-90dd-40f0-9382-eb1efda571ba" @@ -187,4 +189,4 @@ Sundials = "c3572dad-4567-51f8-b174-8c6c989267f4" Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" [targets] -test = ["AmplNLWriter", "BenchmarkTools", "ControlSystemsBase", "DataInterpolations", "DelayDiffEq", "NonlinearSolve", "ForwardDiff", "Ipopt", "Ipopt_jll", "ModelingToolkitStandardLibrary", "Optimization", "OptimizationOptimJL", "OptimizationMOI", "OrdinaryDiffEq", "OrdinaryDiffEqCore", "OrdinaryDiffEqDefault", "REPL", "Random", "ReferenceTests", "SafeTestsets", "StableRNGs", "Statistics", "SteadyStateDiffEq", "Test", "StochasticDiffEq", "Sundials", "StochasticDelayDiffEq", "Pkg", "JET", "OrdinaryDiffEqNonlinearSolve"] +test = ["AmplNLWriter", "BenchmarkTools", "ControlSystemsBase", "DataInterpolations", "DelayDiffEq", "NonlinearSolve", "ForwardDiff", "Ipopt", "Ipopt_jll", "ModelingToolkitStandardLibrary", "Optimization", "OptimizationOptimJL", "OptimizationMOI", "OrdinaryDiffEq", "OrdinaryDiffEqCore", "OrdinaryDiffEqDefault", "REPL", "Random", "ReferenceTests", "SafeTestsets", "StableRNGs", "Statistics", "SteadyStateDiffEq", "Test", "StochasticDiffEq", "Sundials", "StochasticDelayDiffEq", "Pkg", "JET", "OrdinaryDiffEqNonlinearSolve", "Logging"] diff --git a/test/debugging.jl b/test/debugging.jl index aad36eb995..a55684737c 100644 --- a/test/debugging.jl +++ b/test/debugging.jl @@ -1,4 +1,5 @@ using ModelingToolkit, OrdinaryDiffEq, StochasticDiffEq, SymbolicIndexingInterface +import Logging using ModelingToolkit: t_nounits as t, D_nounits as D, ASSERTION_LOG_VARIABLE @variables x(t) @@ -25,11 +26,11 @@ end dsys = debug_system(sys; functions = []) @test is_parameter(dsys, ASSERTION_LOG_VARIABLE) prob = Problem(dsys, [x => 0.1], (0.0, 5.0)) - sol = solve(prob, alg) - @test !SciMLBase.successful_retcode(sol) - prob.ps[ASSERTION_LOG_VARIABLE] = true sol = @test_logs (:error, r"ohno") match_mode=:any solve(prob, alg) @test !SciMLBase.successful_retcode(sol) + prob.ps[ASSERTION_LOG_VARIABLE] = false + sol = @test_logs min_level=Logging.Error solve(prob, alg) + @test !SciMLBase.successful_retcode(sol) end end @@ -41,10 +42,10 @@ end dsys = debug_system(outer; functions = []) @test is_parameter(dsys, ASSERTION_LOG_VARIABLE) prob = Problem(dsys, [inner.x => 0.1], (0.0, 5.0)) - sol = solve(prob, alg) - @test !SciMLBase.successful_retcode(sol) - prob.ps[ASSERTION_LOG_VARIABLE] = true sol = @test_logs (:error, r"ohno") match_mode=:any solve(prob, alg) @test !SciMLBase.successful_retcode(sol) + prob.ps[ASSERTION_LOG_VARIABLE] = false + sol = @test_logs min_level=Logging.Error solve(prob, alg) + @test !SciMLBase.successful_retcode(sol) end end From 5b62aee9de092aa7b7710329e54e68d4e01d095a Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 18 Feb 2025 12:45:05 +0530 Subject: [PATCH 0971/2176] fix: improve validation of variables in `build_explicit_observed_function` --- src/systems/diffeqs/odesystem.jl | 4 ++-- src/utils.jl | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/src/systems/diffeqs/odesystem.jl b/src/systems/diffeqs/odesystem.jl index 2dcb703d96..bd40eeb853 100644 --- a/src/systems/diffeqs/odesystem.jl +++ b/src/systems/diffeqs/odesystem.jl @@ -476,6 +476,7 @@ function build_explicit_observed_function(sys, ts; end end allsyms = Set(all_symbols(sys)) + iv = has_iv(sys) ? get_iv(sys) : nothing for var in vs var = unwrap(var) newvar = get(ns_map, var, nothing) @@ -483,8 +484,7 @@ function build_explicit_observed_function(sys, ts; namespace_subs[var] = newvar var = newvar end - if throw && !(var in allsyms) && - (!iscall(var) || operation(var) !== getindex || !(arguments(var)[1] in allsyms)) + if throw && !var_in_varlist(var, allsyms, iv) Base.throw(ArgumentError("Symbol $var is not present in the system.")) end end diff --git a/src/utils.jl b/src/utils.jl index fc6e49d894..962801622a 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -1267,3 +1267,22 @@ function symbol_to_symbolic(sys::AbstractSystem, sym; allsyms = all_symbols(sys) end return sym end + +""" + $(TYPEDSIGNATURES) + +Check if `var` is present in `varlist`. `iv` is the independent variable of the system, +and should be `nothing` if not applicable. +""" +function var_in_varlist(var, varlist::AbstractSet, iv) + var = unwrap(var) + # simple case + return var in varlist || + # indexed array symbolic, unscalarized array present + (iscall(var) && operation(var) === getindex && arguments(var)[1] in varlist) || + # unscalarized sized array symbolic, all scalarized elements present + (symbolic_type(var) == ArraySymbolic() && is_sized_array_symbolic(var) && + all(x -> x in varlist, collect(var))) || + # delayed variables + (isdelay(var, iv) && var_in_varlist(operation(var)(iv), varlist, iv)) +end From 3dd2810c2843a4b3c27edc4cf12d04201bb37561 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 18 Feb 2025 12:45:19 +0530 Subject: [PATCH 0972/2176] test: fix hack test --- test/structural_transformation/utils.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/structural_transformation/utils.jl b/test/structural_transformation/utils.jl index 621aa8b8a8..863e091aad 100644 --- a/test/structural_transformation/utils.jl +++ b/test/structural_transformation/utils.jl @@ -92,7 +92,7 @@ end @mtkbuild sys = ODESystem( [D(y) ~ foo(x), D(x) ~ sum(y), zeros(2) ~ foo(prod(z))], t) @test length(equations(sys)) == 5 - @test length(observed(sys)) == 4 + @test length(observed(sys)) == 2 prob = ODEProblem( sys, [y => ones(2), z => 2ones(2), x => 3.0], (0.0, 1.0), [foo => _tmp_fn2]) @test_nowarn prob.f(prob.u0, prob.p, 0.0) From eb6bc157fdaf542b0fdd01c132f3902d2a835208 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 18 Feb 2025 14:11:06 +0530 Subject: [PATCH 0973/2176] fix: appropriately map array variables to scalarized versions in `add_fallbacks!` --- src/systems/problem_utils.jl | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/systems/problem_utils.jl b/src/systems/problem_utils.jl index be05d8fcff..60650e9ee9 100644 --- a/src/systems/problem_utils.jl +++ b/src/systems/problem_utils.jl @@ -117,6 +117,7 @@ Variables as they are specified in `vars` will take priority over their `toterm` function add_fallbacks!( varmap::AnyDict, vars::Vector, fallbacks::Dict; toterm = default_toterm) missingvars = Set() + arrvars = Set() for var in vars haskey(varmap, var) && continue ttvar = toterm(var) @@ -157,6 +158,7 @@ function add_fallbacks!( fallbacks, arrvar, nothing) get(fallbacks, ttarrvar, nothing) Some(nothing) if val !== nothing val = val[idxs...] + is_sized_array_symbolic(arrvar) && push!(arrvars, arrvar) end else val = nothing @@ -170,6 +172,10 @@ function add_fallbacks!( end end + for arrvar in arrvars + varmap[arrvar] = collect(arrvar) + end + return missingvars end From e735b4f70c0d707141e22658fac2d1d1464f0ba6 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 18 Feb 2025 14:11:28 +0530 Subject: [PATCH 0974/2176] fix: handle unscalarized defaults for scalarized observed variables --- src/systems/problem_utils.jl | 4 ++-- test/initial_values.jl | 16 ++++++++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/src/systems/problem_utils.jl b/src/systems/problem_utils.jl index 60650e9ee9..0be41ea6f7 100644 --- a/src/systems/problem_utils.jl +++ b/src/systems/problem_utils.jl @@ -275,9 +275,9 @@ entry for `eq.lhs`, insert the reverse mapping if `eq.rhs` is not a number. """ function add_observed_equations!(varmap::AbstractDict, eqs) for eq in eqs - if haskey(varmap, eq.lhs) + if var_in_varlist(eq.lhs, keys(varmap), nothing) eq.rhs isa Number && continue - haskey(varmap, eq.rhs) && continue + var_in_varlist(eq.rhs, keys(varmap), nothing) && continue !iscall(eq.rhs) || issym(operation(eq.rhs)) || continue varmap[eq.rhs] = eq.lhs else diff --git a/test/initial_values.jl b/test/initial_values.jl index 4543f3549e..2db552c3ba 100644 --- a/test/initial_values.jl +++ b/test/initial_values.jl @@ -1,5 +1,6 @@ using ModelingToolkit using ModelingToolkit: t_nounits as t, D_nounits as D, get_u0 +using OrdinaryDiffEq using SymbolicIndexingInterface: getu @variables x(t)[1:3]=[1.0, 2.0, 3.0] y(t) z(t)[1:2] @@ -183,3 +184,18 @@ end @test_throws ModelingToolkit.MissingParametersError ODEProblem( sys, [x => ones(2)], (0.0, 1.0)) end + +@testset "Unscalarized default for scalarized observed variable" begin + @parameters p[1:4] = rand(4) + @variables x(t)[1:4] y(t)[1:2] + eqs = [ + D(x) ~ x, + y[1] ~ x[3], + y[2] ~ x[4] + ] + @mtkbuild sys = ODESystem(eqs, t; defaults = [x => vcat(ones(2), y), y => x[1:2] ./ 2]) + prob = ODEProblem(sys, [], (0.0, 1.0)) + sol = solve(prob) + @test SciMLBase.successful_retcode(sol) + @test sol.u[1] ≈ [1.0, 1.0, 0.5, 0.5] +end From 6665eb0540e63894040bfd1569eda2cd80ba7576 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 18 Feb 2025 16:34:25 +0530 Subject: [PATCH 0975/2176] feat: allow swapping out argument name function in `array_variable_assignments` --- src/systems/codegen_utils.jl | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/systems/codegen_utils.jl b/src/systems/codegen_utils.jl index 910d0d1c1e..4dce62827d 100644 --- a/src/systems/codegen_utils.jl +++ b/src/systems/codegen_utils.jl @@ -12,8 +12,14 @@ end Given the arguments to `build_function_wrapper`, return a list of assignments which reconstruct array variables if they are present scalarized in `args`. + +# Keyword Arguments + +- `argument_name` a function of the form `(::Int) -> Symbol` which takes the index of + an argument to the generated function and returns the name of the argument in the + generated function. """ -function array_variable_assignments(args...) +function array_variable_assignments(args...; argument_name = generated_argument_name) # map array symbolic to an identically sized array where each element is (buffer_idx, idx_in_buffer) var_to_arridxs = Dict{BasicSymbolic, Array{Tuple{Int, Int}}}() for (i, arg) in enumerate(args) @@ -60,12 +66,12 @@ function array_variable_assignments(args...) end # view and reshape - expr = term(reshape, term(view, generated_argument_name(buffer_idx), idxs), + expr = term(reshape, term(view, argument_name(buffer_idx), idxs), size(arrvar)) else elems = map(idxs) do idx i, j = idx - term(getindex, generated_argument_name(i), j) + term(getindex, argument_name(i), j) end # use `MakeArray` syntax and generate a stack-allocated array expr = term(SymbolicUtils.Code.create_array, SArray, nothing, From b49628d065788e9f0a6269d4ab42d404e30cf68c Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 18 Feb 2025 16:34:42 +0530 Subject: [PATCH 0976/2176] fix: handle scalarized array arguments to `CacheWriter` --- src/systems/nonlinear/nonlinearsystem.jl | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/systems/nonlinear/nonlinearsystem.jl b/src/systems/nonlinear/nonlinearsystem.jl index b026543af8..c68fefa7d1 100644 --- a/src/systems/nonlinear/nonlinearsystem.jl +++ b/src/systems/nonlinear/nonlinearsystem.jl @@ -578,11 +578,19 @@ function CacheWriter(sys::AbstractSystem, buffer_types::Vector{TypeT}, Symbol(:tmp, i) ← SetArray(true, :(out[$i]), get(exprs, T, [])) end + function argument_name(i::Int) + if i <= length(solsyms) + return :($(generated_argument_name(1))[$i]) + end + return generated_argument_name(i - length(solsyms)) + end + array_assignments = array_variable_assignments(solsyms...; argument_name) fn = build_function_wrapper( - sys, nothing, :out, DestructuredArgs(DestructuredArgs.(solsyms)), - DestructuredArgs.(rps)...; p_start = 3, p_end = length(rps) + 2, + sys, nothing, :out, + DestructuredArgs(DestructuredArgs.(solsyms), generated_argument_name(1)), + rps...; p_start = 3, p_end = length(rps) + 2, expression = Val{true}, add_observed = false, - extra_assignments = [obs_assigns; body]) + extra_assignments = [array_assignments; obs_assigns; body]) fn = eval_or_rgf(fn; eval_expression, eval_module) fn = GeneratedFunctionWrapper{(3, 3, is_split(sys))}(fn, nothing) return CacheWriter(fn) From 93387c2ee7319faf168b2302d8a174f906f60271 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 19 Feb 2025 12:09:11 +0530 Subject: [PATCH 0977/2176] fix: fix parameter object aliasing after `remake` --- src/systems/nonlinear/initializesystem.jl | 5 +++-- test/initializationsystem.jl | 18 ++++++++++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/src/systems/nonlinear/initializesystem.jl b/src/systems/nonlinear/initializesystem.jl index 0301276183..81f5af5ddc 100644 --- a/src/systems/nonlinear/initializesystem.jl +++ b/src/systems/nonlinear/initializesystem.jl @@ -512,9 +512,10 @@ end function SciMLBase.late_binding_update_u0_p( prob, sys::AbstractSystem, u0, p, t0, newu0, newp) - u0 === missing && return newu0, newp - eltype(u0) <: Pair || return newu0, newp + u0 === missing && return newu0, (p === missing ? copy(newp) : newp) + eltype(u0) <: Pair || return newu0, (p === missing ? copy(newp) : newp) + newp = p === missing ? copy(newp) : newp newu0 = DiffEqBase.promote_u0(newu0, newp, t0) tunables, repack, alias = SciMLStructures.canonicalize(SciMLStructures.Tunable(), newp) tunables = DiffEqBase.promote_u0(tunables, newu0, t0) diff --git a/test/initializationsystem.jl b/test/initializationsystem.jl index a2348c6647..45afa4163a 100644 --- a/test/initializationsystem.jl +++ b/test/initializationsystem.jl @@ -1370,3 +1370,21 @@ end @test_nowarn remake(prob, p = [p => 1.0]) @test_nowarn remake(prob, p = [p => ForwardDiff.Dual(1.0)]) end + +@testset "`late_binding_update_u0_p` copies `newp`" begin + @parameters k1 k2 + @variables X1(t) X2(t) + @parameters Γ[1:1]=missing [guess = [1.0]] + eqs = [ + D(X1) ~ k1 * (Γ[1] - X1) - k2 * X1 + ] + obs = [X2 ~ Γ[1] - X1] + @mtkbuild osys = ODESystem(eqs, t, [X1, X2], [k1, k2, Γ]; observed = obs) + u0 = [X1 => 1.0, X2 => 2.0] + ps = [k1 => 0.1, k2 => 0.2] + + oprob1 = ODEProblem(osys, u0, 1.0, ps) + oprob2 = remake(oprob1, u0 = [X1 => 10.0]) + integ1 = init(oprob1) + @test integ1[X1] ≈ 1.0 +end From a3b4bdd13e1cb2e76e83673c0c360777c652d17d Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 19 Feb 2025 16:27:58 +0530 Subject: [PATCH 0978/2176] fix: handle DDEs in `GeneratedFunctionWrapper` call in `build_explicit_observed_function` --- src/systems/diffeqs/odesystem.jl | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/systems/diffeqs/odesystem.jl b/src/systems/diffeqs/odesystem.jl index bd40eeb853..1590fd6ebd 100644 --- a/src/systems/diffeqs/odesystem.jl +++ b/src/systems/diffeqs/odesystem.jl @@ -527,12 +527,14 @@ function build_explicit_observed_function(sys, ts; if fns isa Tuple oop, iip = eval_or_rgf.(fns; eval_expression, eval_module) f = GeneratedFunctionWrapper{( - p_start, length(args) - length(ps) + 1, is_split(sys))}(oop, iip) + p_start + is_dde(sys), length(args) - length(ps) + 1 + is_dde(sys), is_split(sys))}( + oop, iip) return return_inplace ? (f, f) : f else f = eval_or_rgf(fns; eval_expression, eval_module) f = GeneratedFunctionWrapper{( - p_start, length(args) - length(ps) + 1, is_split(sys))}(f, nothing) + p_start + is_dde(sys), length(args) - length(ps) + 1 + is_dde(sys), is_split(sys))}( + f, nothing) return f end end From 267eee9b6a3d3aa192ecdeb3d75e2594bbac47c0 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 19 Feb 2025 16:28:11 +0530 Subject: [PATCH 0979/2176] test: fix initialization of inversemodel tests --- test/downstream/inversemodel.jl | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/test/downstream/inversemodel.jl b/test/downstream/inversemodel.jl index 8573964551..a0b72c6b2c 100644 --- a/test/downstream/inversemodel.jl +++ b/test/downstream/inversemodel.jl @@ -157,15 +157,21 @@ nsys = get_named_sensitivity(model, :y; op) # Test that we get the same result w # Test the same thing for comp sensitivities -Sf, simplified_sys = Blocks.get_comp_sensitivity_function(model, :y; op); # This should work without providing an operating opint containing a dummy derivative +# This should work without providing an operating opint containing a dummy derivative +Sf, simplified_sys = Blocks.get_comp_sensitivity_function( + model, :y; op, initialization_solver_alg = NewtonRaphson()); x = state_values(Sf) p = parameter_values(Sf) # If this somehow passes, mention it on # https://github.com/SciML/ModelingToolkit.jl/issues/2786 matrices1 = Sf(x, p, 0) -matrices2, _ = Blocks.get_comp_sensitivity(model, :y; op) # Test that we get the same result when calling the higher-level API +# Test that we get the same result when calling the higher-level API +matrices2, _ = Blocks.get_comp_sensitivity( + model, :y; op, initialization_solver_alg = NewtonRaphson()) @test matrices1.f_x ≈ matrices2.A[1:7, 1:7] -nsys = get_named_comp_sensitivity(model, :y; op) # Test that we get the same result when calling an even higher-level API +# Test that we get the same result when calling an even higher-level API +nsys = get_named_comp_sensitivity( + model, :y; op, initialization_solver_alg = NewtonRaphson()) @test matrices2.A ≈ nsys.A @testset "Issue #3319" begin From f29266420eb8d18c683be80501662f136d73d3e6 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 19 Feb 2025 16:32:09 +0530 Subject: [PATCH 0980/2176] test: fix inversemodel tests Changing state realization in ControlSystemsBase made it unstable, so filter order was reduced. Co-authored-by: Fredrik Bagge Carlson --- test/downstream/inversemodel.jl | 42 ++++++++++----------------------- 1 file changed, 13 insertions(+), 29 deletions(-) diff --git a/test/downstream/inversemodel.jl b/test/downstream/inversemodel.jl index a0b72c6b2c..32d5ee87ec 100644 --- a/test/downstream/inversemodel.jl +++ b/test/downstream/inversemodel.jl @@ -27,20 +27,17 @@ rc = 0.25 # Reference concentration k0 = 1.05e14 ϵ = 34.2894 end - @variables begin gamma(t), [description = "Reaction speed"] xc(t) = c0, [description = "Concentration"] xT(t) = T0, [description = "Temperature"] xT_c(t), [description = "Cooling temperature"] end - @components begin T_c = RealInput() c = RealOutput() T = RealOutput() end - begin τ0 = 60 wk0 = k0 / c0 @@ -57,20 +54,18 @@ rc = 0.25 # Reference concentration gamma ~ xc * wk0 * exp(-wϵ / xT) D(xc) ~ -wa11 * xc - wa12 * gamma + wa13 D(xT) ~ -wa21 * xT + wa22 * gamma + wa23 + wb * xT_c - xc ~ c.u xT ~ T.u xT_c ~ T_c.u end end - begin - Ftf = tf(1, [(100), 1])^3 + Ftf = tf(1, [(100), 1])^2 Fss = ss(Ftf) - # Create an MTK-compatible constructor function RefFilter(; name) sys = ODESystem(Fss; name) + "Compute initial state that yields y0 as output" empty!(ModelingToolkit.get_defaults(sys)) return sys end @@ -82,7 +77,6 @@ end x10 = 0.42 x20 = 0.01 u0 = -0.0224 - c_start = c0 * (1 - x10) # Initial concentration T_start = T0 * (1 + x20) # Initial temperature c_high_start = c0 * (1 - 0.72) # Reference concentration @@ -104,22 +98,13 @@ end @equations begin connect(ref.output, :r, filter.input) connect(filter.output, inverse_tank.c) - connect(inverse_tank.T_c, ff_gain.input) connect(ff_gain.output, :uff, limiter.input) connect(limiter.output, add.input1) - connect(controller.ctr_output, :u, add.input2) - - #connect(add.output, :u_tot, limiter.input) - #connect(limiter.output, :v, tank.T_c) - connect(add.output, :u_tot, tank.T_c) - connect(inverse_tank.T, feedback.input1) - connect(tank.T, :y, noise_filter.input) - connect(noise_filter.output, feedback.input2) connect(feedback.output, :e, controller.err_input) end @@ -128,8 +113,10 @@ end; ssys = structural_simplify(model) cm = complete(model) -op = Dict(D(cm.inverse_tank.xT) => 1, - cm.tank.xc => 0.65) +op = Dict( + cm.filter.y.u => 0.8 * (1 - 0.42), + D(cm.filter.y.u) => 0 +) tspan = (0.0, 1000.0) # https://github.com/SciML/ModelingToolkit.jl/issues/2786 prob = ODEProblem(ssys, op, tspan) @@ -151,37 +138,34 @@ p = parameter_values(Sf) # https://github.com/SciML/ModelingToolkit.jl/issues/2786 matrices1 = Sf(x, p, 0) matrices2, _ = Blocks.get_sensitivity(model, :y; op); # Test that we get the same result when calling the higher-level API -@test matrices1.f_x ≈ matrices2.A[1:7, 1:7] +@test matrices1.f_x ≈ matrices2.A[1:6, 1:6] nsys = get_named_sensitivity(model, :y; op) # Test that we get the same result when calling an even higher-level API @test matrices2.A ≈ nsys.A # Test the same thing for comp sensitivities # This should work without providing an operating opint containing a dummy derivative -Sf, simplified_sys = Blocks.get_comp_sensitivity_function( - model, :y; op, initialization_solver_alg = NewtonRaphson()); +Sf, simplified_sys = Blocks.get_comp_sensitivity_function(model, :y; op); x = state_values(Sf) p = parameter_values(Sf) # If this somehow passes, mention it on # https://github.com/SciML/ModelingToolkit.jl/issues/2786 matrices1 = Sf(x, p, 0) # Test that we get the same result when calling the higher-level API -matrices2, _ = Blocks.get_comp_sensitivity( - model, :y; op, initialization_solver_alg = NewtonRaphson()) -@test matrices1.f_x ≈ matrices2.A[1:7, 1:7] +matrices2, _ = Blocks.get_comp_sensitivity(model, :y; op) +@test matrices1.f_x ≈ matrices2.A[1:6, 1:6] # Test that we get the same result when calling an even higher-level API -nsys = get_named_comp_sensitivity( - model, :y; op, initialization_solver_alg = NewtonRaphson()) +nsys = get_named_comp_sensitivity(model, :y; op) @test matrices2.A ≈ nsys.A @testset "Issue #3319" begin op1 = Dict( - D(cm.inverse_tank.xT) => 1, + cm.filter.y.u => 0.8 * (1 - 0.42), cm.tank.xc => 0.65 ) op2 = Dict( - D(cm.inverse_tank.xT) => 1, + cm.filter.y.u => 0.8 * (1 - 0.42), cm.tank.xc => 0.45 ) From b257e20cd13822577b389cfedbb0b774b87ca355 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 19 Feb 2025 17:48:23 +0530 Subject: [PATCH 0981/2176] refactor: don't do CSE hack for operators Prevents annoying equations with `Initial` parameters --- src/structural_transformation/symbolics_tearing.jl | 1 + 1 file changed, 1 insertion(+) diff --git a/src/structural_transformation/symbolics_tearing.jl b/src/structural_transformation/symbolics_tearing.jl index bf5c51797b..fe3ce45430 100644 --- a/src/structural_transformation/symbolics_tearing.jl +++ b/src/structural_transformation/symbolics_tearing.jl @@ -647,6 +647,7 @@ function cse_and_array_hacks(sys, obs, subeqs, unknowns, neweqs; cse = true, arr # HACK 1 if cse && is_getindexed_array(rhs) rhs_arr = arguments(rhs)[1] + iscall(rhs_arr) && operation(rhs_arr) isa Symbolics.Operator && continue if !haskey(rhs_to_tempvar, rhs_arr) tempvar = gensym(Symbol(lhs)) N = length(rhs_arr) From 0c84697d7c4bdf29c177ead1e117625c505558bf Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 20 Feb 2025 13:16:30 +0530 Subject: [PATCH 0982/2176] fix: filter `nothing` values in `u0map/parammap` in `InitializationProblem` --- src/systems/diffeqs/abstractodesystem.jl | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/systems/diffeqs/abstractodesystem.jl b/src/systems/diffeqs/abstractodesystem.jl index 04f7b468d2..9944d92a6e 100644 --- a/src/systems/diffeqs/abstractodesystem.jl +++ b/src/systems/diffeqs/abstractodesystem.jl @@ -1291,7 +1291,9 @@ function InitializationProblem{iip, specialize}(sys::AbstractSystem, guesses = Dict() end - u0map = merge(ModelingToolkit.guesses(sys), todict(guesses), todict(u0map)) + filter_missing_values!(u0map) + filter_missing_values!(parammap) + u0map = merge(ModelingToolkit.guesses(sys), todict(guesses), u0map) fullmap = merge(u0map, parammap) u0T = Union{} From 7c92d22afd42f50dcfe48b6b4c4897cc230b57b8 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 20 Feb 2025 13:16:45 +0530 Subject: [PATCH 0983/2176] fix: support `CheckInit` in `linearization_function` --- src/linearization.jl | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/linearization.jl b/src/linearization.jl index 469c2cd766..f83efc1642 100644 --- a/src/linearization.jl +++ b/src/linearization.jl @@ -170,8 +170,8 @@ function (linfun::LinearizationFunction)(u, p, t) if u !== nothing # Handle systems without unknowns linfun.num_states == length(u) || error("Number of unknown variables ($(linfun.num_states)) does not match the number of input unknowns ($(length(u)))") - integ_cache = linfun.caches - integ = MockIntegrator{true}(u, p, t, integ_cache) + integ_cache = (linfun.caches,) + integ = MockIntegrator{true}(u, p, t, integ_cache, nothing) u, p, success = SciMLBase.get_initial_values( linfun.prob, integ, fun, linfun.initializealg, Val(true); linfun.initialize_kwargs...) @@ -218,7 +218,7 @@ Mock `DEIntegrator` to allow using `CheckInit` without having to create a new in $(TYPEDFIELDS) """ -struct MockIntegrator{iip, U, P, T, C} <: SciMLBase.DEIntegrator{Nothing, iip, U, T} +struct MockIntegrator{iip, U, P, T, C, O} <: SciMLBase.DEIntegrator{Nothing, iip, U, T} """ The state vector. """ @@ -235,10 +235,14 @@ struct MockIntegrator{iip, U, P, T, C} <: SciMLBase.DEIntegrator{Nothing, iip, U The integrator cache. """ cache::C + """ + Integrator "options" for `CheckInit`. + """ + opts::O end -function MockIntegrator{iip}(u::U, p::P, t::T, cache::C) where {iip, U, P, T, C} - return MockIntegrator{iip, U, P, T, C}(u, p, t, cache) +function MockIntegrator{iip}(u::U, p::P, t::T, cache::C, opts::O) where {iip, U, P, T, C, O} + return MockIntegrator{iip, U, P, T, C, O}(u, p, t, cache, opts) end SymbolicIndexingInterface.state_values(integ::MockIntegrator) = integ.u From 6e15eec40b67eb2855c5649777d37ca15a01d613 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 20 Feb 2025 14:44:54 +0530 Subject: [PATCH 0984/2176] ci: add MTKNeuralNets to downstream CI --- .github/workflows/Downstream.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/Downstream.yml b/.github/workflows/Downstream.yml index 0debdbdc7c..9c5d05e022 100644 --- a/.github/workflows/Downstream.yml +++ b/.github/workflows/Downstream.yml @@ -37,6 +37,7 @@ jobs: - {user: SciML, repo: MethodOfLines.jl, group: Interface} - {user: SciML, repo: MethodOfLines.jl, group: 2D_Diffusion} - {user: SciML, repo: MethodOfLines.jl, group: DAE} + - {user: SciML, repo: ModelingToolkitNeuralNets.jl, group: All} steps: - uses: actions/checkout@v4 - uses: julia-actions/setup-julia@v1 From 069b096c62723567c30ff702b6a860d6cb149df7 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 20 Feb 2025 14:45:14 +0530 Subject: [PATCH 0985/2176] build: bump Symbolics compat --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 0563e28bcf..1ecd1f792f 100644 --- a/Project.toml +++ b/Project.toml @@ -148,7 +148,7 @@ StochasticDelayDiffEq = "1.8.1" StochasticDiffEq = "6.72.1" SymbolicIndexingInterface = "0.3.37" SymbolicUtils = "3.14" -Symbolics = "6.29" +Symbolics = "6.29.1" URIs = "1" UnPack = "0.1, 1.0" Unitful = "1.1" From ceb9cdc7493f8c622fcfb9821437de2d74ef5e18 Mon Sep 17 00:00:00 2001 From: Fredrik Bagge Carlson Date: Fri, 7 Feb 2025 09:20:28 +0100 Subject: [PATCH 0986/2176] add utilities and tests for disturbance modeling Apply suggestions from code review Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> rm plot --- src/systems/analysis_points.jl | 33 ++++ src/systems/diffeqs/odesystem.jl | 13 +- test/downstream/test_disturbance_model.jl | 215 ++++++++++++++++++++++ test/runtests.jl | 1 + 4 files changed, 261 insertions(+), 1 deletion(-) create mode 100644 test/downstream/test_disturbance_model.jl diff --git a/src/systems/analysis_points.jl b/src/systems/analysis_points.jl index 022a0909ed..135bf81729 100644 --- a/src/systems/analysis_points.jl +++ b/src/systems/analysis_points.jl @@ -960,3 +960,36 @@ Compute the (linearized) loop-transfer function in analysis point `ap`, from `ap See also [`get_sensitivity`](@ref), [`get_comp_sensitivity`](@ref), [`open_loop`](@ref). """ get_looptransfer +# + +""" + generate_control_function(sys::ModelingToolkit.AbstractODESystem, input_ap_name::Union{Symbol, Vector{Symbol}, AnalysisPoint, Vector{AnalysisPoint}}, dist_ap_name::Union{Symbol, Vector{Symbol}, AnalysisPoint, Vector{AnalysisPoint}}; system_modifier = identity, kwargs) + +When called with analysis points as input arguments, we assume that all analysis points corresponds to connections that should be opened (broken). The use case for this is to get rid of input signal blocks, such as `Step` or `Sine`, since these are useful for simulation but are not needed when using the plant model in a controller or state estimator. +""" +function generate_control_function( + sys::ModelingToolkit.AbstractODESystem, input_ap_name::Union{ + Symbol, Vector{Symbol}, AnalysisPoint, Vector{AnalysisPoint}}, + dist_ap_name::Union{ + Nothing, Symbol, Vector{Symbol}, AnalysisPoint, Vector{AnalysisPoint}} = nothing; + system_modifier = identity, + kwargs...) + input_ap_name = canonicalize_ap(sys, input_ap_name) + u = [] + for input_ap in input_ap_name + sys, (du, _) = open_loop(sys, input_ap) + push!(u, du) + end + if dist_ap_name === nothing + return ModelingToolkit.generate_control_function(system_modifier(sys), u; kwargs...) + end + + dist_ap_name = canonicalize_ap(sys, dist_ap_name) + d = [] + for dist_ap in dist_ap_name + sys, (du, _) = open_loop(sys, dist_ap) + push!(d, du) + end + + ModelingToolkit.generate_control_function(system_modifier(sys), u, d; kwargs...) +end diff --git a/src/systems/diffeqs/odesystem.jl b/src/systems/diffeqs/odesystem.jl index 1590fd6ebd..20d1e495dc 100644 --- a/src/systems/diffeqs/odesystem.jl +++ b/src/systems/diffeqs/odesystem.jl @@ -436,6 +436,8 @@ an array of inputs `inputs` is given, and `param_only` is false for a time-depen """ function build_explicit_observed_function(sys, ts; inputs = nothing, + disturbance_inputs = nothing, + disturbance_argument = false, expression = false, eval_expression = false, eval_module = @__MODULE__, @@ -512,13 +514,22 @@ function build_explicit_observed_function(sys, ts; ps = setdiff(ps, inputs) # Inputs have been converted to parameters by io_preprocessing, remove those from the parameter list inputs = (inputs,) end + if disturbance_inputs !== nothing + # Disturbance inputs may or may not be included as inputs, depending on disturbance_argument + ps = setdiff(ps, disturbance_inputs) + end + if disturbance_argument + disturbance_inputs = (disturbance_inputs,) + else + disturbance_inputs = () + end ps = reorder_parameters(sys, ps) iv = if is_time_dependent(sys) (get_iv(sys),) else () end - args = (dvs..., inputs..., ps..., iv...) + args = (dvs..., inputs..., ps..., iv..., disturbance_inputs...) p_start = length(dvs) + length(inputs) + 1 p_end = length(dvs) + length(inputs) + length(ps) fns = build_function_wrapper( diff --git a/test/downstream/test_disturbance_model.jl b/test/downstream/test_disturbance_model.jl new file mode 100644 index 0000000000..10fcf9fc1f --- /dev/null +++ b/test/downstream/test_disturbance_model.jl @@ -0,0 +1,215 @@ +#= +This file implements and tests a typical workflow for state estimation with disturbance models +The primary subject of the tests is the analysis-point features and the +analysis-point specific method for `generate_control_function`. +=# +using ModelingToolkit, OrdinaryDiffEq, LinearAlgebra, Test +using ModelingToolkitStandardLibrary.Mechanical.Rotational +using ModelingToolkitStandardLibrary.Blocks +using ModelingToolkit: connect +# using Plots + +using ModelingToolkit: t_nounits as t, D_nounits as D + +indexof(sym, syms) = findfirst(isequal(sym), syms) + +## Build the system model ====================================================== +@mtkmodel SystemModel begin + @parameters begin + m1 = 1 + m2 = 1 + k = 10 # Spring stiffness + c = 3 # Damping coefficient + end + @components begin + inertia1 = Inertia(; J = m1, phi = 0, w = 0) + inertia2 = Inertia(; J = m2, phi = 0, w = 0) + spring = Spring(; c = k) + damper = Damper(; d = c) + torque = Torque(use_support = false) + end + @equations begin + connect(torque.flange, inertia1.flange_a) + connect(inertia1.flange_b, spring.flange_a, damper.flange_a) + connect(inertia2.flange_a, spring.flange_b, damper.flange_b) + end +end + +@mtkmodel ModelWithInputs begin + @components begin + input_signal = Blocks.Sine(frequency = 1, amplitude = 1) + disturbance_signal1 = Blocks.Constant(k = 0) + disturbance_signal2 = Blocks.Constant(k = 0) + disturbance_torque1 = Torque(use_support = false) + disturbance_torque2 = Torque(use_support = false) + system_model = SystemModel() + end + @equations begin + connect(input_signal.output, :u, system_model.torque.tau) + connect(disturbance_signal1.output, :d1, disturbance_torque1.tau) + connect(disturbance_signal2.output, :d2, disturbance_torque2.tau) + connect(disturbance_torque1.flange, system_model.inertia1.flange_b) + connect(disturbance_torque2.flange, system_model.inertia2.flange_b) + end +end + +@named model = ModelWithInputs() # Model with load disturbance +ssys = structural_simplify(model) +prob = ODEProblem(ssys, [], (0.0, 10.0)) +sol = solve(prob, Tsit5()) +# plot(sol) + +## +using ControlSystemsBase, ControlSystemsMTK +cmodel = complete(model) +P = cmodel.system_model +lsys = named_ss( + model, [:u, :d1], [P.inertia1.phi, P.inertia2.phi, P.inertia1.w, P.inertia2.w]) + +## +# If we now want to add a disturbance model, we cannot do that since we have already connected a constant to the disturbance input. We have also already used the name `d` for an analysis point, but that might not be an issue since we create an outer model and get a new namespace. + +s = tf("s") +dist(; name) = ODESystem(1 / s; name) + +@mtkmodel SystemModelWithDisturbanceModel begin + @components begin + input_signal = Blocks.Sine(frequency = 1, amplitude = 1) + disturbance_signal1 = Blocks.Constant(k = 0) + disturbance_signal2 = Blocks.Constant(k = 0) + disturbance_torque1 = Torque(use_support = false) + disturbance_torque2 = Torque(use_support = false) + disturbance_model = dist() + system_model = SystemModel() + end + @equations begin + connect(input_signal.output, :u, system_model.torque.tau) + connect(disturbance_signal1.output, :d1, disturbance_model.input) + connect(disturbance_model.output, disturbance_torque1.tau) + connect(disturbance_signal2.output, :d2, disturbance_torque2.tau) + connect(disturbance_torque1.flange, system_model.inertia1.flange_b) + connect(disturbance_torque2.flange, system_model.inertia2.flange_b) + end +end + +@named model_with_disturbance = SystemModelWithDisturbanceModel() +# ssys = structural_simplify(open_loop(model_with_disturbance, :d)) # Open loop worked, but it's a bit awkward that we have to use it here +# lsys2 = named_ss(model_with_disturbance, [:u, :d1], +# [P.inertia1.phi, P.inertia2.phi, P.inertia1.w, P.inertia2.w]) +ssys = structural_simplify(model_with_disturbance) +prob = ODEProblem(ssys, [], (0.0, 10.0)) +sol = solve(prob, Tsit5()) +@test SciMLBase.successful_retcode(sol) +# plot(sol) + +## +# Now we only have an integrating disturbance affecting inertia1, what if we want both integrating and direct Gaussian? We'd need a "PI controller" disturbancemodel. If we add the disturbance model (s+1)/s we get the integrating and non-integrating noises being correlated which is fine, it reduces the dimensions of the sigma point by 1. + +dist3(; name) = ODESystem(ss(1 + 10 / s, balance = false); name) + +@mtkmodel SystemModelWithDisturbanceModel begin + @components begin + input_signal = Blocks.Sine(frequency = 1, amplitude = 1) + disturbance_signal1 = Blocks.Constant(k = 0) + disturbance_signal2 = Blocks.Constant(k = 0) + disturbance_torque1 = Torque(use_support = false) + disturbance_torque2 = Torque(use_support = false) + disturbance_model = dist3() + system_model = SystemModel() + + y = Blocks.Add() + angle_sensor = AngleSensor() + output_disturbance = Blocks.Constant(k = 0) + end + @equations begin + connect(input_signal.output, :u, system_model.torque.tau) + connect(disturbance_signal1.output, :d1, disturbance_model.input) + connect(disturbance_model.output, disturbance_torque1.tau) + connect(disturbance_signal2.output, :d2, disturbance_torque2.tau) + connect(disturbance_torque1.flange, system_model.inertia1.flange_b) + connect(disturbance_torque2.flange, system_model.inertia2.flange_b) + + connect(system_model.inertia1.flange_b, angle_sensor.flange) + connect(angle_sensor.phi, y.input1) + connect(output_disturbance.output, :dy, y.input2) + end +end + +@named model_with_disturbance = SystemModelWithDisturbanceModel() +# ssys = structural_simplify(open_loop(model_with_disturbance, :d)) # Open loop worked, but it's a bit awkward that we have to use it here +# lsys3 = named_ss(model_with_disturbance, [:u, :d1], +# [P.inertia1.phi, P.inertia2.phi, P.inertia1.w, P.inertia2.w]) +ssys = structural_simplify(model_with_disturbance) +prob = ODEProblem(ssys, [], (0.0, 10.0)) +sol = solve(prob, Tsit5()) +@test SciMLBase.successful_retcode(sol) +# plot(sol) + +## Generate function for an augmented Unscented Kalman Filter ===================== +# temp = open_loop(model_with_disturbance, :d) +outputs = [P.inertia1.phi, P.inertia2.phi, P.inertia1.w, P.inertia2.w] +(f_oop1, f_ip), x_sym, p_sym, io_sys = ModelingToolkit.generate_control_function( + model_with_disturbance, [:u], [:d1, :d2, :dy], split = false) + +(f_oop2, f_ip2), x_sym, p_sym, io_sys = ModelingToolkit.generate_control_function( + model_with_disturbance, [:u], [:d1, :d2, :dy], + disturbance_argument = true, split = false) + +measurement = ModelingToolkit.build_explicit_observed_function( + io_sys, outputs, inputs = ModelingToolkit.inputs(io_sys)[1:1]) +measurement2 = ModelingToolkit.build_explicit_observed_function( + io_sys, [io_sys.y.output.u], inputs = ModelingToolkit.inputs(io_sys)[1:1], + disturbance_inputs = ModelingToolkit.inputs(io_sys)[2:end], + disturbance_argument = true) + +op = ModelingToolkit.inputs(io_sys) .=> 0 +x0, p = ModelingToolkit.get_u0_p(io_sys, op, op) +x = zeros(5) +u = zeros(1) +d = zeros(3) +@test f_oop2(x, u, p, t, d) == zeros(5) +@test measurement(x, u, p, 0.0) == [0, 0, 0, 0] +@test measurement2(x, u, p, 0.0, d) == [0] + +# Add to the integrating disturbance input +d = [1, 0, 0] +@test sort(f_oop2(x, u, p, 0.0, d)) == [0, 0, 0, 1, 1] # Affects disturbance state and one velocity +@test measurement2(x, u, p, 0.0, d) == [0] + +d = [0, 1, 0] +@test sort(f_oop2(x, u, p, 0.0, d)) == [0, 0, 0, 0, 1] # Affects one velocity +@test measurement(x, u, p, 0.0) == [0, 0, 0, 0] +@test measurement2(x, u, p, 0.0, d) == [0] + +d = [0, 0, 1] +@test sort(f_oop2(x, u, p, 0.0, d)) == [0, 0, 0, 0, 0] # Affects nothing +@test measurement(x, u, p, 0.0) == [0, 0, 0, 0] +@test measurement2(x, u, p, 0.0, d) == [1] # We have now disturbed the output + +## Further downstream tests that the functions generated above actually have the properties required to use for state estimation +# +# using LowLevelParticleFilters, SeeToDee +# Ts = 0.001 +# discrete_dynamics = SeeToDee.Rk4(f_oop2, Ts) +# nx = length(x_sym) +# nu = 1 +# nw = 2 +# ny = length(outputs) +# R1 = Diagonal([1e-5, 1e-5]) +# R2 = 0.1 * I(ny) +# op = ModelingToolkit.inputs(io_sys) .=> 0 +# x0, p = ModelingToolkit.get_u0_p(io_sys, op, op) +# d0 = LowLevelParticleFilters.SimpleMvNormal(x0, 10.0I(nx)) +# measurement_model = UKFMeasurementModel{Float64, false, false}(measurement, R2; nx, ny) +# kf = UnscentedKalmanFilter{false, false, true, false}( +# discrete_dynamics, measurement_model, R1, d0; nu, Ts, p) + +# tvec = 0:Ts:sol.t[end] +# u = vcat.(Array(sol(tvec, idxs = P.torque.tau.u))) +# y = collect.(eachcol(Array(sol(tvec, idxs = outputs)) .+ 1e-2 .* randn.())) + +# inds = 1:5805 +# res = forward_trajectory(kf, u, y) + +# plot(res, size = (1000, 1000)); +# plot!(sol, idxs = x_sym, sp = (1:nx)', l = :dash); diff --git a/test/runtests.jl b/test/runtests.jl index 966b02cacb..11c78e43ca 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -120,6 +120,7 @@ end @safetestset "Linearization Dummy Derivative Tests" include("downstream/linearization_dd.jl") @safetestset "Inverse Models Test" include("downstream/inversemodel.jl") @safetestset "Analysis Points Test" include("downstream/analysis_points.jl") + @safetestset "Analysis Points Test" include("downstream/test_disturbance_model.jl") end if GROUP == "All" || GROUP == "FMI" From 43172c9ec98c383d42fe58a1c81754b3ce7b45a8 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 21 Feb 2025 11:23:37 +0530 Subject: [PATCH 0987/2176] build: bump patch version --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 1ecd1f792f..ba47d83eb6 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "ModelingToolkit" uuid = "961ee093-0014-501f-94e3-6117800e7a78" authors = ["Yingbo Ma ", "Chris Rackauckas and contributors"] -version = "9.64.0" +version = "9.64.1" [deps] AbstractTrees = "1520ce14-60c1-5f80-bbc7-55ef81b5835c" From 56b74353dec77231dc3f671c7358bba2a4e23734 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 14 Feb 2025 17:43:32 +0530 Subject: [PATCH 0988/2176] refactor: remove `MTKHomotopyContinuationExt` --- Project.toml | 3 - ext/MTKHomotopyContinuationExt.jl | 225 ------------------------------ 2 files changed, 228 deletions(-) delete mode 100644 ext/MTKHomotopyContinuationExt.jl diff --git a/Project.toml b/Project.toml index ba47d83eb6..3196235379 100644 --- a/Project.toml +++ b/Project.toml @@ -65,7 +65,6 @@ BifurcationKit = "0f109fa4-8a5d-4b75-95aa-f515264e7665" ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4" DeepDiffs = "ab62b9b5-e342-54a8-a765-a90f495de1a6" FMI = "14a09403-18e3-468f-ad8a-74f8dda2d9ac" -HomotopyContinuation = "f213a82b-91d6-5c5d-acf7-10f1c761b327" InfiniteOpt = "20393b10-9daf-11e9-18c9-8db751c92c57" LabelledArrays = "2ee39098-c373-598a-b85f-a56591580800" @@ -74,7 +73,6 @@ MTKBifurcationKitExt = "BifurcationKit" MTKChainRulesCoreExt = "ChainRulesCore" MTKDeepDiffsExt = "DeepDiffs" MTKFMIExt = "FMI" -MTKHomotopyContinuationExt = "HomotopyContinuation" MTKInfiniteOptExt = "InfiniteOpt" MTKLabelledArraysExt = "LabelledArrays" @@ -110,7 +108,6 @@ ForwardDiff = "0.10.3" FunctionWrappers = "1.1" FunctionWrappersWrappers = "0.1" Graphs = "1.5.2" -HomotopyContinuation = "2.11" InfiniteOpt = "0.5" InteractiveUtils = "1" JuliaFormatter = "1.0.47" diff --git a/ext/MTKHomotopyContinuationExt.jl b/ext/MTKHomotopyContinuationExt.jl deleted file mode 100644 index 8f17c05b18..0000000000 --- a/ext/MTKHomotopyContinuationExt.jl +++ /dev/null @@ -1,225 +0,0 @@ -module MTKHomotopyContinuationExt - -using ModelingToolkit -using ModelingToolkit.SciMLBase -using ModelingToolkit.Symbolics: unwrap, symtype, BasicSymbolic, simplify_fractions -using ModelingToolkit.SymbolicIndexingInterface -using ModelingToolkit.DocStringExtensions -using HomotopyContinuation -using ModelingToolkit: iscomplete, parameters, has_index_cache, get_index_cache, get_u0, - get_u0_p, check_eqs_u0, CommonSolve - -const MTK = ModelingToolkit - -""" -$(TYPEDSIGNATURES) - -Convert `expr` from a symbolics expression to one that uses `HomotopyContinuation.ModelKit`. -""" -function symbolics_to_hc(expr) - if iscall(expr) - if operation(expr) == getindex - args = arguments(expr) - return ModelKit.Variable(getname(args[1]), args[2:end]...) - else - return operation(expr)(symbolics_to_hc.(arguments(expr))...) - end - elseif symbolic_type(expr) == NotSymbolic() - return expr - else - return ModelKit.Variable(getname(expr)) - end -end - -""" -$(TYPEDEF) - -A subtype of `HomotopyContinuation.AbstractSystem` used to solve `HomotopyContinuationProblem`s. -""" -struct MTKHomotopySystem{F, P, J, V} <: HomotopyContinuation.AbstractSystem - """ - The generated function for the residual of the polynomial system. In-place. - """ - f::F - """ - The parameter object. - """ - p::P - """ - The generated function for the jacobian of the polynomial system. In-place. - """ - jac::J - """ - The `HomotopyContinuation.ModelKit.Variable` representation of the unknowns of - the system. - """ - vars::V - """ - The number of polynomials in the system. Must also be equal to `length(vars)`. - """ - nexprs::Int -end - -Base.size(sys::MTKHomotopySystem) = (sys.nexprs, length(sys.vars)) -ModelKit.variables(sys::MTKHomotopySystem) = sys.vars - -function (sys::MTKHomotopySystem)(x, p = nothing) - sys.f(x, sys.p) -end - -function ModelKit.evaluate!(u, sys::MTKHomotopySystem, x, p = nothing) - sys.f(u, x, sys.p) -end - -function ModelKit.evaluate_and_jacobian!(u, U, sys::MTKHomotopySystem, x, p = nothing) - sys.f(u, x, sys.p) - sys.jac(U, x, sys.p) -end - -SymbolicIndexingInterface.parameter_values(s::MTKHomotopySystem) = s.p - -""" - $(TYPEDSIGNATURES) - -Create a `HomotopyContinuationProblem` from a `NonlinearSystem` with polynomial equations. -The problem will be solved by HomotopyContinuation.jl. The resultant `NonlinearSolution` -will contain the polynomial root closest to the point specified by `u0map` (if real roots -exist for the system). - -Keyword arguments: -- `eval_expression`: Whether to `eval` the generated functions or use a `RuntimeGeneratedFunction`. -- `eval_module`: The module to use for `eval`/`@RuntimeGeneratedFunction` -- `warn_parametric_exponent`: Whether to warn if the system contains a parametric - exponent preventing the homotopy from being cached. - -All other keyword arguments are forwarded to `HomotopyContinuation.solver_startsystems`. -""" -function MTK.HomotopyContinuationProblem( - sys::NonlinearSystem, u0map, parammap = nothing; kwargs...) - prob = MTK._safe_HomotopyContinuationProblem(sys, u0map, parammap; kwargs...) - prob isa MTK.HomotopyContinuationProblem || throw(prob) - return prob -end - -function MTK._safe_HomotopyContinuationProblem(sys, u0map, parammap = nothing; - fraction_cancel_fn = SymbolicUtils.simplify_fractions, kwargs...) - if !iscomplete(sys) - error("A completed `NonlinearSystem` is required. Call `complete` or `structural_simplify` on the system before creating a `HomotopyContinuationProblem`") - end - transformation = MTK.PolynomialTransformation(sys) - if transformation isa MTK.NotPolynomialError - return transformation - end - result = MTK.transform_system(sys, transformation; fraction_cancel_fn) - if result isa MTK.NotPolynomialError - return result - end - MTK.HomotopyContinuationProblem(sys, transformation, result, u0map, parammap; kwargs...) -end - -function MTK.HomotopyContinuationProblem( - sys::MTK.NonlinearSystem, transformation::MTK.PolynomialTransformation, - result::MTK.PolynomialTransformationResult, u0map, - parammap = nothing; eval_expression = false, - eval_module = ModelingToolkit, warn_parametric_exponent = true, kwargs...) - sys2 = result.sys - denoms = result.denominators - polydata = transformation.polydata - new_dvs = transformation.new_dvs - all_solutions = transformation.all_solutions - - _, u0, p = MTK.process_SciMLProblem( - MTK.EmptySciMLFunction, sys, u0map, parammap; eval_expression, eval_module) - nlfn = NonlinearFunction{true}(sys2; jac = true, eval_expression, eval_module) - - denominator = MTK.build_explicit_observed_function(sys2, denoms) - unpack_solution = MTK.build_explicit_observed_function(sys2, all_solutions) - - hvars = symbolics_to_hc.(new_dvs) - mtkhsys = MTKHomotopySystem(nlfn.f, p, nlfn.jac, hvars, length(new_dvs)) - - obsfn = MTK.ObservedFunctionCache(sys; eval_expression, eval_module) - - has_parametric_exponents = any(d -> d.has_parametric_exponent, polydata) - if has_parametric_exponents - if warn_parametric_exponent - @warn """ - The system has parametric exponents, preventing caching of the homotopy. \ - This will cause `solve` to be slower. Pass `warn_parametric_exponent \ - = false` to turn off this warning - """ - end - solver_and_starts = nothing - else - solver_and_starts = HomotopyContinuation.solver_startsolutions(mtkhsys; kwargs...) - end - return MTK.HomotopyContinuationProblem( - u0, mtkhsys, denominator, sys, obsfn, solver_and_starts, unpack_solution) -end - -""" -$(TYPEDSIGNATURES) - -Solve a `HomotopyContinuationProblem`. Ignores the algorithm passed to it, and always -uses `HomotopyContinuation.jl`. The original solution as returned by -`HomotopyContinuation.jl` will be available in the `.original` field of the returned -`NonlinearSolution`. - -All keyword arguments except the ones listed below are forwarded to -`HomotopyContinuation.solve`. Note that the solver and start solutions are precomputed, -and only keyword arguments related to the solve process are valid. All keyword -arguments have their default values in HomotopyContinuation.jl, except `show_progress` -which defaults to `false`. - -Extra keyword arguments: -- `denominator_abstol`: In case `prob` is solving a rational function, roots which cause - the denominator to be below `denominator_abstol` will be discarded. -""" -function CommonSolve.solve(prob::MTK.HomotopyContinuationProblem, - alg = nothing; show_progress = false, denominator_abstol = 1e-7, kwargs...) - if prob.solver_and_starts === nothing - sol = HomotopyContinuation.solve( - prob.homotopy_continuation_system; show_progress, kwargs...) - else - solver, starts = prob.solver_and_starts - sol = HomotopyContinuation.solve(solver, starts; show_progress, kwargs...) - end - realsols = HomotopyContinuation.results(sol; only_real = true) - if isempty(realsols) - u = state_values(prob) - retcode = SciMLBase.ReturnCode.ConvergenceFailure - resid = prob.homotopy_continuation_system(u) - else - T = eltype(state_values(prob)) - distance = T(Inf) - u = state_values(prob) - resid = nothing - for result in realsols - if any(<=(denominator_abstol), - prob.denominator(real.(result.solution), parameter_values(prob))) - continue - end - for truesol in prob.unpack_solution(result.solution, parameter_values(prob)) - dist = norm(truesol - state_values(prob)) - if dist < distance - distance = dist - u = T.(real.(truesol)) - resid = T.(real.(prob.homotopy_continuation_system(result.solution))) - end - end - end - # all roots cause denominator to be zero - if isinf(distance) - u = state_values(prob) - resid = prob.homotopy_continuation_system(u) - retcode = SciMLBase.ReturnCode.Infeasible - else - retcode = SciMLBase.ReturnCode.Success - end - end - - return SciMLBase.build_solution( - prob, :HomotopyContinuation, u, resid; retcode, original = sol) -end - -end From ef5e7a00b3c22f051b2f53fc1a66a92588c2a9f2 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 14 Feb 2025 17:43:54 +0530 Subject: [PATCH 0989/2176] refactor: remove `use_homotopy_continuation` keyword from `NonlinearProblem` --- src/systems/nonlinear/nonlinearsystem.jl | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/src/systems/nonlinear/nonlinearsystem.jl b/src/systems/nonlinear/nonlinearsystem.jl index c68fefa7d1..73484eb0dd 100644 --- a/src/systems/nonlinear/nonlinearsystem.jl +++ b/src/systems/nonlinear/nonlinearsystem.jl @@ -512,17 +512,10 @@ end function DiffEqBase.NonlinearProblem{iip}(sys::NonlinearSystem, u0map, parammap = DiffEqBase.NullParameters(); - check_length = true, use_homotopy_continuation = false, kwargs...) where {iip} + check_length = true, kwargs...) where {iip} if !iscomplete(sys) error("A completed `NonlinearSystem` is required. Call `complete` or `structural_simplify` on the system before creating a `NonlinearProblem`") end - if use_homotopy_continuation - prob = safe_HomotopyContinuationProblem( - sys, u0map, parammap; check_length, kwargs...) - if prob isa HomotopyContinuationProblem - return prob - end - end f, u0, p = process_SciMLProblem(NonlinearFunction{iip}, sys, u0map, parammap; check_length, kwargs...) pt = something(get_metadata(sys), StandardNonlinearProblem()) From 18029477936545a07ab95da6f8cb2f296bc9a1be Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 14 Feb 2025 17:44:36 +0530 Subject: [PATCH 0990/2176] refactor: rewrite `HomotopyContinuationProblem` to target NonlinearSolveHomotopyContinuation.jl --- .../nonlinear/homotopy_continuation.jl | 179 +++++++++--------- test/extensions/homotopy_continuation.jl | 114 ++++++----- 2 files changed, 142 insertions(+), 151 deletions(-) diff --git a/src/systems/nonlinear/homotopy_continuation.jl b/src/systems/nonlinear/homotopy_continuation.jl index 09e11199e8..474224eacd 100644 --- a/src/systems/nonlinear/homotopy_continuation.jl +++ b/src/systems/nonlinear/homotopy_continuation.jl @@ -1,93 +1,3 @@ -""" -$(TYPEDEF) - -A type of Nonlinear problem which specializes on polynomial systems and uses -HomotopyContinuation.jl to solve the system. Requires importing HomotopyContinuation.jl to -create and solve. -""" -struct HomotopyContinuationProblem{uType, H, D, O, SS, U} <: - SciMLBase.AbstractNonlinearProblem{uType, true} - """ - The initial values of states in the system. If there are multiple real roots of - the system, the one closest to this point is returned. - """ - u0::uType - """ - A subtype of `HomotopyContinuation.AbstractSystem` to solve. Also contains the - parameter object. - """ - homotopy_continuation_system::H - """ - A function with signature `(u, p) -> resid`. In case of rational functions, this - is used to rule out roots of the system which would cause the denominator to be - zero. - """ - denominator::D - """ - The `NonlinearSystem` used to create this problem. Used for symbolic indexing. - """ - sys::NonlinearSystem - """ - A function which generates and returns observed expressions for the given system. - """ - obsfn::O - """ - The HomotopyContinuation.jl solver and start system, obtained through - `HomotopyContinuation.solver_startsystems`. - """ - solver_and_starts::SS - """ - A function which takes a solution of the transformed system, and returns a vector - of solutions for the original system. This is utilized when converting systems - to polynomials. - """ - unpack_solution::U -end - -function HomotopyContinuationProblem(::AbstractSystem, _u0, _p; kwargs...) - error("HomotopyContinuation.jl is required to create and solve `HomotopyContinuationProblem`s. Please run `Pkg.add(\"HomotopyContinuation\")` to continue.") -end - -""" - $(TYPEDSIGNATURES) - -Utility function for `safe_HomotopyContinuationProblem`, implemented in the extension. -""" -function _safe_HomotopyContinuationProblem end - -""" - $(TYPEDSIGNATURES) - -Return a `HomotopyContinuationProblem` if the extension is loaded and the system is -polynomial. If the extension is not loaded, return `nothing`. If the system is not -polynomial, return the appropriate `NotPolynomialError`. -""" -function safe_HomotopyContinuationProblem(sys::NonlinearSystem, args...; kwargs...) - if Base.get_extension(ModelingToolkit, :MTKHomotopyContinuationExt) === nothing - return nothing - end - return _safe_HomotopyContinuationProblem(sys, args...; kwargs...) -end - -SymbolicIndexingInterface.symbolic_container(p::HomotopyContinuationProblem) = p.sys -SymbolicIndexingInterface.state_values(p::HomotopyContinuationProblem) = p.u0 -function SymbolicIndexingInterface.set_state!(p::HomotopyContinuationProblem, args...) - set_state!(p.u0, args...) -end -function SymbolicIndexingInterface.parameter_values(p::HomotopyContinuationProblem) - parameter_values(p.homotopy_continuation_system) -end -function SymbolicIndexingInterface.set_parameter!(p::HomotopyContinuationProblem, args...) - set_parameter!(parameter_values(p), args...) -end -function SymbolicIndexingInterface.observed(p::HomotopyContinuationProblem, sym) - if p.obsfn !== nothing - return p.obsfn(sym) - else - return SymbolicIndexingInterface.observed(p.sys, sym) - end -end - function contains_variable(x, wrt) any(y -> occursin(y, x), wrt) end @@ -562,3 +472,92 @@ function handle_rational_polynomials(x, wrt; fraction_cancel_fn = simplify_fract end return num, den end + +function SciMLBase.HomotopyNonlinearFunction(sys::NonlinearSystem, args...; kwargs...) + ODEFunction{true}(sys, args...; kwargs...) +end + +function SciMLBase.HomotopyNonlinearFunction{true}(sys::NonlinearSystem, args...; + kwargs...) + ODEFunction{true, SciMLBase.AutoSpecialize}(sys, args...; kwargs...) +end + +function SciMLBase.HomotopyNonlinearFunction{false}(sys::NonlinearSystem, args...; + kwargs...) + ODEFunction{false, SciMLBase.FullSpecialize}(sys, args...; kwargs...) +end + +function SciMLBase.HomotopyNonlinearFunction{iip, specialize}( + sys::NonlinearSystem, args...; eval_expression = false, eval_module = @__MODULE__, + p = nothing, fraction_cancel_fn = SymbolicUtils.simplify_fractions, + kwargs...) where {iip, specialize} + if !iscomplete(sys) + error("A completed `NonlinearSystem` is required. Call `complete` or `structural_simplify` on the system before creating a `HomotopyContinuationFunction`") + end + transformation = PolynomialTransformation(sys) + if transformation isa NotPolynomialError + throw(transformation) + end + result = transform_system(sys, transformation; fraction_cancel_fn) + if result isa NotPolynomialError + throw(result) + end + + sys2 = result.sys + denoms = result.denominators + polydata = transformation.polydata + new_dvs = transformation.new_dvs + all_solutions = transformation.all_solutions + + # we want to create f, jac etc. according to `sys2` since that will do the solving + # but the `sys` inside for symbolic indexing should be the non-polynomial system + fn = NonlinearFunction{iip}(sys2; eval_expression, eval_module, kwargs...) + obsfn = ObservedFunctionCache( + sys; eval_expression, eval_module, checkbounds = get(kwargs, :checkbounds, false)) + fn = remake(fn; sys = sys, observed = obsfn) + + denominator = build_explicit_observed_function(sys2, denoms) + unpolynomialize = build_explicit_observed_function(sys2, all_solutions) + + inv_mapping = Dict(v => k for (k, v) in transformation.substitution_rules) + polynomialize_terms = [get(inv_mapping, var, var) for var in unknowns(sys2)] + polynomialize = build_explicit_observed_function(sys, polynomialize_terms) + + return HomotopyNonlinearFunction{iip, specialize}( + fn; polynomialize, unpolynomialize, denominator) +end + +struct HomotopyContinuationProblem{iip, specialization} end + +function HomotopyContinuationProblem(sys::NonlinearSystem, args...; kwargs...) + HomotopyContinuationProblem{true}(sys, args...; kwargs...) +end + +function HomotopyContinuationProblem(sys::NonlinearSystem, t, + u0map::StaticArray, + args...; + kwargs...) + HomotopyContinuationProblem{false, SciMLBase.FullSpecialize}( + sys, t, u0map, args...; kwargs...) +end + +function HomotopyContinuationProblem{true}(sys::NonlinearSystem, args...; kwargs...) + HomotopyContinuationProblem{true, SciMLBase.AutoSpecialize}(sys, args...; kwargs...) +end + +function HomotopyContinuationProblem{false}(sys::NonlinearSystem, args...; kwargs...) + HomotopyContinuationProblem{false, SciMLBase.FullSpecialize}(sys, args...; kwargs...) +end + +function HomotopyContinuationProblem{iip, spec}( + sys::NonlinearSystem, u0map, pmap = SciMLBase.NullParameters(); + kwargs...) where {iip, spec} + if !iscomplete(sys) + error("A completed `NonlinearSystem` is required. Call `complete` or `structural_simplify` on the system before creating a `HomotopyContinuationProblem`") + end + f, u0, p = process_SciMLProblem( + HomotopyNonlinearFunction{iip, spec}, sys, u0map, pmap; kwargs...) + + kwargs = filter_kwargs(kwargs) + return NonlinearProblem{iip}(f, u0, p; kwargs...) +end diff --git a/test/extensions/homotopy_continuation.jl b/test/extensions/homotopy_continuation.jl index 9e15ea857e..ed630ea8e3 100644 --- a/test/extensions/homotopy_continuation.jl +++ b/test/extensions/homotopy_continuation.jl @@ -1,20 +1,32 @@ -using ModelingToolkit, NonlinearSolve, SymbolicIndexingInterface +using ModelingToolkit, NonlinearSolve, NonlinearSolveHomotopyContinuation, + SymbolicIndexingInterface using SymbolicUtils import ModelingToolkit as MTK using LinearAlgebra using Test -@testset "Safe HCProblem" begin - @variables x y z - eqs = [0 ~ x^2 + y^2 + 2x * y - 0 ~ x^2 + 4x + 4 - 0 ~ y * z + 4x^2] - @mtkbuild sys = NonlinearSystem(eqs) - prob = MTK.safe_HomotopyContinuationProblem(sys, [x => 1.0, y => 1.0, z => 1.0], []) - @test prob === nothing +allrootsalg = HomotopyContinuationJL{true}(; threading = false) +singlerootalg = HomotopyContinuationJL{false}(; threading = false) + +function test_single_root(sol; atol = 1e-10) + @test SciMLBase.successful_retcode(sol) + @test norm(sol.resid)≈0.0 atol=atol end -import HomotopyContinuation +function test_all_roots(sol; atol = 1e-10) + @test sol.converged + for nlsol in sol.u + @test SciMLBase.successful_retcode(nlsol) + @test norm(nlsol.resid)≈0.0 atol=1e-10 + end +end + +function solve_allroots_closest(prob) + sol = solve(prob, allrootsalg) + return argmin(sol.u) do nlsol + return norm(nlsol.u - prob.u0) + end +end @testset "No parameters" begin @variables x y z @@ -24,19 +36,13 @@ import HomotopyContinuation @mtkbuild sys = NonlinearSystem(eqs) u0 = [x => 1.0, y => 1.0, z => 1.0] prob = HomotopyContinuationProblem(sys, u0) + @test prob isa NonlinearProblem @test prob[x] == prob[y] == prob[z] == 1.0 @test prob[x + y] == 2.0 - sol = solve(prob; threading = false) - @test SciMLBase.successful_retcode(sol) - @test norm(sol.resid)≈0.0 atol=1e-10 - - prob2 = NonlinearProblem(sys, u0; use_homotopy_continuation = true) - @test prob2 isa HomotopyContinuationProblem - sol = solve(prob2; threading = false) - @test SciMLBase.successful_retcode(sol) - @test norm(sol.resid)≈0.0 atol=1e-10 - - @test NonlinearProblem(sys, u0; use_homotopy_continuation = false) isa NonlinearProblem + sol = solve(prob, singlerootalg) + test_single_root(sol) + sol = solve(prob, allrootsalg) + test_all_roots(sol) end struct Wrapper @@ -61,9 +67,10 @@ end @test prob.ps[q] == 4 @test prob.ps[r].x == [1.0 1.0; 0.0 0.0] @test prob.ps[p * q] == 8.0 - sol = solve(prob; threading = false) - @test SciMLBase.successful_retcode(sol) - @test norm(sol.resid)≈0.0 atol=1e-10 + sol = solve(prob, singlerootalg) + test_single_root(sol) + sol = solve(prob, allrootsalg) + test_all_roots(sol) end @testset "Array variables" begin @@ -79,7 +86,7 @@ end @test prob[x] == 2ones(3) prob.ps[p] = [2, 3, 4] @test prob.ps[p] == [2, 3, 4] - sol = @test_nowarn solve(prob; threading = false) + sol = @test_nowarn solve(prob, singlerootalg) @test sol.retcode == ReturnCode.ConvergenceFailure end @@ -87,11 +94,11 @@ end @variables x = 1.0 @parameters n::Integer = 4 @mtkbuild sys = NonlinearSystem([x^n + x^2 - 1 ~ 0]) - prob = @test_warn ["parametric", "exponent"] HomotopyContinuationProblem(sys, []) - @test prob.solver_and_starts === nothing - @test_nowarn HomotopyContinuationProblem(sys, []; warn_parametric_exponent = false) - sol = solve(prob; threading = false) - @test SciMLBase.successful_retcode(sol) + prob = HomotopyContinuationProblem(sys, []) + sol = solve(prob, singlerootalg) + test_single_root(sol) + sol = solve(prob, allrootsalg) + test_all_roots(sol) end @testset "Polynomial check and warnings" begin @@ -100,45 +107,31 @@ end @test_throws ["Cannot convert", "Unable", "symbolically solve", "Exponent", "not an integer", "not a polynomial"] HomotopyContinuationProblem( sys, []) - @test MTK.safe_HomotopyContinuationProblem(sys, []) isa MTK.NotPolynomialError - @test NonlinearProblem(sys, []) isa NonlinearProblem @mtkbuild sys = NonlinearSystem([x^x - x ~ 0]) @test_throws ["Cannot convert", "Unable", "symbolically solve", "Exponent", "unknowns", "not a polynomial"] HomotopyContinuationProblem( sys, []) - @test MTK.safe_HomotopyContinuationProblem(sys, []) isa MTK.NotPolynomialError - @test NonlinearProblem(sys, []) isa NonlinearProblem @mtkbuild sys = NonlinearSystem([((x^2) / sin(x))^2 + x ~ 0]) @test_throws ["Cannot convert", "both polynomial", "non-polynomial", "recognized", "sin", "not a polynomial"] HomotopyContinuationProblem( sys, []) - @test MTK.safe_HomotopyContinuationProblem(sys, []) isa MTK.NotPolynomialError - @test NonlinearProblem(sys, []) isa NonlinearProblem @variables y = 2.0 @mtkbuild sys = NonlinearSystem([x^2 + y^2 + 2 ~ 0, y ~ sin(x)]) @test_throws ["Cannot convert", "recognized", "sin", "not a polynomial"] HomotopyContinuationProblem( sys, []) - @test MTK.safe_HomotopyContinuationProblem(sys, []) isa MTK.NotPolynomialError - @test NonlinearProblem(sys, []) isa NonlinearProblem @mtkbuild sys = NonlinearSystem([x^2 + y^2 - 2 ~ 0, sin(x + y) ~ 0]) @test_throws ["Cannot convert", "function of multiple unknowns"] HomotopyContinuationProblem( sys, []) - @test MTK.safe_HomotopyContinuationProblem(sys, []) isa MTK.NotPolynomialError - @test NonlinearProblem(sys, []) isa NonlinearProblem @mtkbuild sys = NonlinearSystem([sin(x)^2 + 1 ~ 0, cos(y) - cos(x) - 1 ~ 0]) @test_throws ["Cannot convert", "multiple non-polynomial terms", "same unknown"] HomotopyContinuationProblem( sys, []) - @test MTK.safe_HomotopyContinuationProblem(sys, []) isa MTK.NotPolynomialError - @test NonlinearProblem(sys, []) isa NonlinearProblem @mtkbuild sys = NonlinearSystem([sin(x^2)^2 + sin(x^2) - 1 ~ 0]) @test_throws ["import Nemo"] HomotopyContinuationProblem(sys, []) - @test MTK.safe_HomotopyContinuationProblem(sys, []) isa MTK.NotPolynomialError - @test NonlinearProblem(sys, []) isa NonlinearProblem end import Nemo @@ -148,7 +141,8 @@ import Nemo @mtkbuild sys = NonlinearSystem([sin(x^2)^2 + sin(x^2) - 1 ~ 0]) prob = HomotopyContinuationProblem(sys, []) @test prob[1] ≈ 2.0 - sol = solve(prob; threading = false) + # singlerootalg doesn't converge + sol = solve(prob, allrootsalg).u[1] _x = sol[1] @test sin(_x^2)^2 + sin(_x^2) - 1≈0.0 atol=1e-12 end @@ -162,9 +156,7 @@ end prob = HomotopyContinuationProblem(sys, []) @test prob[x] ≈ 0.25 @test prob[y] ≈ 0.125 - sol = solve(prob; threading = false) - # can't replicate the solve failure locally, so CI logs might help - @show sol.u sol.original.path_results + sol = solve(prob, allrootsalg).u[1] @test SciMLBase.successful_retcode(sol) @test sol[a]≈0.5 atol=1e-6 @test sol[b]≈0.25 atol=1e-6 @@ -177,12 +169,12 @@ end 0 ~ (x^2 - n * x + n) * (x - 1) / (x - 2) / (x - 3) ]) prob = HomotopyContinuationProblem(sys, []) - sol = solve(prob; threading = false) + sol = solve_allroots_closest(prob) @test sol[x] ≈ 1.0 p = parameter_values(prob) for invalid in [2.0, 3.0] for err in [-9e-8, 0, 9e-8] - @test any(<=(1e-7), prob.denominator([invalid + err, 2.0], p)) + @test any(<=(1e-7), prob.f.denominator([invalid + err, 2.0], p)) end end @@ -195,7 +187,7 @@ end [n]) sys = complete(sys) prob = HomotopyContinuationProblem(sys, []) - sol = solve(prob; threading = false) + sol = solve(prob, singlerootalg) disallowed_x = [4, 5.5] disallowed_y = [7, 5, 4] @test all(!isapprox(sol[x]; atol = 1e-8), disallowed_x) @@ -205,30 +197,30 @@ end p = parameter_values(prob) for val in disallowed_x for err in [-9e-8, 0, 9e-8] - @test any(<=(1e-7), prob.denominator([val + err, 2.0], p)) + @test any(<=(1e-7), prob.f.denominator([val + err, 2.0], p)) end end for val in disallowed_y for err in [-9e-8, 0, 9e-8] - @test any(<=(1e-7), prob.denominator([2.0, val + err], p)) + @test any(<=(1e-7), prob.f.denominator([2.0, val + err], p)) end end - @test prob.denominator([2.0, 4.0], p)[1] <= 1e-8 + @test prob.f.denominator([2.0, 4.0], p)[1] <= 1e-8 @testset "Rational function in observed" begin @variables x=1 y=1 @mtkbuild sys = NonlinearSystem([x^2 + y^2 - 2x - 2 ~ 0, y ~ (x - 1) / (x - 2)]) prob = HomotopyContinuationProblem(sys, []) - @test any(prob.denominator([2.0], parameter_values(prob)) .≈ 0.0) - @test SciMLBase.successful_retcode(solve(prob; threading = false)) + @test any(prob.f.denominator([2.0], parameter_values(prob)) .≈ 0.0) + @test SciMLBase.successful_retcode(solve(prob, singlerootalg)) end @testset "Rational function forced to common denominators" begin @variables x = 1 @mtkbuild sys = NonlinearSystem([0 ~ 1 / (1 + x) - x]) prob = HomotopyContinuationProblem(sys, []) - @test any(prob.denominator([-1.0], parameter_values(prob)) .≈ 0.0) - sol = solve(prob; threading = false) + @test any(prob.f.denominator([-1.0], parameter_values(prob)) .≈ 0.0) + sol = solve(prob, singlerootalg) @test SciMLBase.successful_retcode(sol) @test 1 / (1 + sol.u[1]) - sol.u[1]≈0.0 atol=1e-10 end @@ -238,7 +230,7 @@ end @variables x=1 y @mtkbuild sys = NonlinearSystem([x^2 - 2 ~ 0, y ~ sin(x)]) prob = HomotopyContinuationProblem(sys, []) - sol = @test_nowarn solve(prob; threading = false) + sol = @test_nowarn solve(prob, singlerootalg) @test sol[x] ≈ √2.0 @test sol[y] ≈ sin(√2.0) end @@ -251,10 +243,10 @@ end @testset "`simplify_fractions`" begin prob = HomotopyContinuationProblem(sys, []) - @test prob.denominator([0.0], parameter_values(prob)) ≈ [4.0] + @test prob.f.denominator([0.0], parameter_values(prob)) ≈ [4.0] end @testset "`nothing`" begin prob = HomotopyContinuationProblem(sys, []; fraction_cancel_fn = nothing) - @test sort(prob.denominator([0.0], parameter_values(prob))) ≈ [2.0, 4.0^3] + @test sort(prob.f.denominator([0.0], parameter_values(prob))) ≈ [2.0, 4.0^3] end end From 486bba7cc982bea2784b99553e67b21212651c75 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 14 Feb 2025 22:51:17 +0530 Subject: [PATCH 0991/2176] test: add NonlinearSolveHomotopyContinuation to `test/extensions` env --- test/extensions/Project.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/test/extensions/Project.toml b/test/extensions/Project.toml index 5f7afe222a..5b0de73cdf 100644 --- a/test/extensions/Project.toml +++ b/test/extensions/Project.toml @@ -10,6 +10,7 @@ JuMP = "4076af6c-e467-56ae-b986-b466b2749572" LabelledArrays = "2ee39098-c373-598a-b85f-a56591580800" ModelingToolkit = "961ee093-0014-501f-94e3-6117800e7a78" Nemo = "2edaba10-b0f1-5616-af89-8c11ac63239a" +NonlinearSolveHomotopyContinuation = "2ac3b008-d579-4536-8c91-a1a5998c2f8b" OrdinaryDiffEq = "1dea7af3-3e70-54e6-95c3-0bf5283fa5ed" SciMLSensitivity = "1ed8b502-d754-442c-8d5d-10ac956f44a1" SciMLStructures = "53ae85a6-f571-4167-b2af-e1d143709226" From 18e766efa165188ea6df5b2ea9a095c85c9ff40d Mon Sep 17 00:00:00 2001 From: vyudu Date: Fri, 21 Feb 2025 11:06:26 -0800 Subject: [PATCH 0992/2176] fix bug --- 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 e7345f5d5d..1f40f61eed 100644 --- a/src/systems/problem_utils.jl +++ b/src/systems/problem_utils.jl @@ -755,7 +755,7 @@ function process_SciMLProblem( u0map = to_varmap(u0map, dvs) symbols_to_symbolics!(sys, u0map) - pmap = to_varmap(pmap, ps) + pmap = to_varmap(pmap, parameters(sys)) symbols_to_symbolics!(sys, pmap) check_inputmap_keys(sys, u0map, pmap) From bb1522869cd80da051b7c3930f69384ef1880af5 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 14 Feb 2025 13:28:35 +0530 Subject: [PATCH 0993/2176] feat: add `available_vars` to `observed_equations_used_by` --- src/utils.jl | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/utils.jl b/src/utils.jl index 962801622a..1fe7da603d 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -1068,14 +1068,24 @@ Keyword arguments: providing this keyword is not necessary and is only useful to avoid repeatedly calling `vars(exprs)` - `obs`: the list of observed equations. +- `available_vars`: If `exprs` involves a variable `x[1]`, this function will look for + observed equations whose LHS is `x[1]` OR `x`. Sometimes, the latter is not required + since `x[1]` might already be present elsewhere in the generated code (e.g. an argument + to the function) but other elements of `x` are part of the observed equations, thus + requiring them to be obtained from the equation for `x`. Any variable present in + `available_vars` will not be searched for in the observed equations. """ function observed_equations_used_by(sys::AbstractSystem, exprs; - involved_vars = vars(exprs; op = Union{Shift, Differential}), obs = observed(sys)) + involved_vars = vars(exprs; op = Union{Shift, Differential}), obs = observed(sys), available_vars = []) obsvars = getproperty.(obs, :lhs) graph = observed_dependency_graph(obs) + if !(available_vars isa Set) + available_vars = Set(available_vars) + end obsidxs = BitSet() for sym in involved_vars + sym in available_vars && continue arrsym = iscall(sym) && operation(sym) === getindex ? arguments(sym)[1] : nothing idx = findfirst(v -> isequal(v, sym) || isequal(v, arrsym), obsvars) idx === nothing && continue From 7ee87a5f4c9d81895099aeacb57d3b232877be88 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 14 Feb 2025 13:28:50 +0530 Subject: [PATCH 0994/2176] fix: fix array varables split across SCCs in SCCNonlinearProblem --- src/systems/nonlinear/nonlinearsystem.jl | 17 ++++++++++------- test/scc_nonlinear_problem.jl | 10 ++++++++++ 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/src/systems/nonlinear/nonlinearsystem.jl b/src/systems/nonlinear/nonlinearsystem.jl index c68fefa7d1..e0aa84dbdb 100644 --- a/src/systems/nonlinear/nonlinearsystem.jl +++ b/src/systems/nonlinear/nonlinearsystem.jl @@ -676,23 +676,23 @@ function SciMLBase.SCCNonlinearProblem{iip}(sys::NonlinearSystem, u0map, scc_cacheexprs = Dict{TypeT, Vector{Any}}[] scc_eqs = Vector{Equation}[] scc_obs = Vector{Equation}[] + # variables solved in previous SCCs + available_vars = Set() for (i, (escc, vscc)) in enumerate(zip(eq_sccs, var_sccs)) # subset unknowns and equations _dvs = dvs[vscc] _eqs = eqs[escc] # get observed equations required by this SCC - obsidxs = observed_equations_used_by(sys, _eqs) + union!(available_vars, _dvs) + obsidxs = observed_equations_used_by(sys, _eqs; available_vars) # the ones used by previous SCCs can be precomputed into the cache setdiff!(obsidxs, prevobsidxs) _obs = obs[obsidxs] + union!(available_vars, getproperty.(_obs, (:lhs,))) # get all subexpressions in the RHS which we can precompute in the cache # precomputed subexpressions should not contain `banned_vars` banned_vars = Set{Any}(vcat(_dvs, getproperty.(_obs, (:lhs,)))) - filter!(banned_vars) do var - symbolic_type(var) != ArraySymbolic() || - all(j -> var[j] in banned_vars, eachindex(var)) - end state = Dict() for i in eachindex(_obs) _obs[i] = _obs[i].lhs ~ subexpressions_not_involving_vars!( @@ -753,9 +753,12 @@ function SciMLBase.SCCNonlinearProblem{iip}(sys::NonlinearSystem, u0map, _obs = scc_obs[i] cachevars = scc_cachevars[i] cacheexprs = scc_cacheexprs[i] + available_vars = [dvs[reduce(vcat, var_sccs[1:(i - 1)]; init = Int[])]; + getproperty.( + reduce(vcat, scc_obs[1:(i - 1)]; init = []), (:lhs,))] _prevobsidxs = vcat(_prevobsidxs, - observed_equations_used_by(sys, reduce(vcat, values(cacheexprs); init = []))) - + observed_equations_used_by( + sys, reduce(vcat, values(cacheexprs); init = []); available_vars)) if isempty(cachevars) push!(explicitfuns, Returns(nothing)) else diff --git a/test/scc_nonlinear_problem.jl b/test/scc_nonlinear_problem.jl index 57f3d72fb7..ef4638cdb0 100644 --- a/test/scc_nonlinear_problem.jl +++ b/test/scc_nonlinear_problem.jl @@ -253,3 +253,13 @@ import ModelingToolkitStandardLibrary.Hydraulic.IsothermalCompressible as IC sol = solve(prob) @test SciMLBase.successful_retcode(sol) end + +@testset "Array variables split across SCCs" begin + @variables x[1:3] + @parameters (f::Function)(..) + @mtkbuild sys = NonlinearSystem([ + 0 ~ x[1]^2 - 9, x[2] ~ 2x[1], 0 ~ x[3]^2 - x[1]^2 + f(x)]) + prob = SCCNonlinearProblem(sys, [x => ones(3)], [f => sum]) + sol = solve(prob, NewtonRaphson()) + @test SciMLBase.successful_retcode(sol) +end From cd1f01e4e0ef7b35d15413584e8f7f14bb95b9f0 Mon Sep 17 00:00:00 2001 From: Martijn Visser Date: Fri, 24 Jan 2025 17:26:30 +0100 Subject: [PATCH 0995/2176] Replace Expronicon with Moshi --- Project.toml | 6 ++-- src/ModelingToolkit.jl | 7 +++-- src/clock.jl | 28 +++++++------------ src/discretedomain.jl | 12 ++++---- src/systems/abstractsystem.jl | 4 +-- src/systems/clock_inference.jl | 6 ++-- src/systems/systemstructure.jl | 3 +- test/clock.jl | 50 +++++++++++++++++----------------- 8 files changed, 56 insertions(+), 60 deletions(-) diff --git a/Project.toml b/Project.toml index b3bc7ed18c..ffd5cd5bf8 100644 --- a/Project.toml +++ b/Project.toml @@ -23,7 +23,6 @@ DomainSets = "5b8099bc-c8ec-5219-889f-1d9e522a28bf" DynamicQuantities = "06fc5a27-2a28-4c7c-a15d-362465fb6821" EnumX = "4e289a0a-7415-4d19-859d-a7e5c4648b56" ExprTools = "e2ba6199-217a-4e67-a87a-7c52f15ade04" -Expronicon = "6b7a57c9-7cc1-4fdf-b7f5-e857abae3636" FindFirstFunctions = "64ca27bc-2ba2-4a57-88aa-44e436879224" ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210" FunctionWrappers = "069b7b12-0de2-55c6-9aab-29f3d0a68a2e" @@ -36,6 +35,7 @@ Latexify = "23fbe1c1-3f47-55db-b15f-69d7ec21a316" Libdl = "8f399da3-3557-5675-b5ff-fb832c97cbdb" LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" MLStyle = "d8e11817-5142-5d16-987a-aa16d5891078" +Moshi = "2e0e35c7-a2e4-4343-998d-7ef72827ed2d" NaNMath = "77ba4419-2d1f-58cd-9bb1-8ffee604a2e3" NonlinearSolve = "8913a72c-1f9b-4ce2-8d82-65094dcecaec" OffsetArrays = "6fe1bfb0-de20-5000-8ca7-80f57d26f881" @@ -105,7 +105,6 @@ DomainSets = "0.6, 0.7" DynamicQuantities = "^0.11.2, 0.12, 0.13, 1" EnumX = "1.0.4" ExprTools = "0.1.10" -Expronicon = "0.8" FMI = "0.14" FindFirstFunctions = "1" ForwardDiff = "0.10.3" @@ -124,6 +123,7 @@ LinearAlgebra = "1" Logging = "1" MLStyle = "0.4.17" ModelingToolkitStandardLibrary = "2.19" +Moshi = "0.3" NaNMath = "0.3, 1" NonlinearSolve = "4.3" OffsetArrays = "1" @@ -138,7 +138,7 @@ RecursiveArrayTools = "3.26" Reexport = "0.2, 1" RuntimeGeneratedFunctions = "0.5.9" SCCNonlinearSolve = "1.0.0" -SciMLBase = "2.73" +SciMLBase = "2.75" SciMLStructures = "1.0" Serialization = "1" Setfield = "0.7, 0.8, 1" diff --git a/src/ModelingToolkit.jl b/src/ModelingToolkit.jl index b683e132a2..e6e29cf736 100644 --- a/src/ModelingToolkit.jl +++ b/src/ModelingToolkit.jl @@ -45,10 +45,13 @@ using Compat using AbstractTrees using DiffEqBase, SciMLBase, ForwardDiff using SciMLBase: StandardODEProblem, StandardNonlinearProblem, handle_varmap, TimeDomain, - PeriodicClock, Clock, SolverStepClock, Continuous, OverrideInit, NoInit + PeriodicClock, Clock, SolverStepClock, ContinuousClock, OverrideInit, + NoInit using Distributed import JuliaFormatter using MLStyle +import Moshi +using Moshi.Data: @data using NonlinearSolve import SCCNonlinearSolve using Reexport @@ -290,7 +293,7 @@ export @variables, @parameters, @independent_variables, @constants, @brownian export @named, @nonamespace, @namespace, extend, compose, complete export debug_system -#export Continuous, Discrete, sampletime, input_timedomain, output_timedomain +#export ContinuousClock, Discrete, sampletime, input_timedomain, output_timedomain #export has_discrete_domain, has_continuous_domain #export is_discrete_domain, is_continuous_domain, is_hybrid_domain export Sample, Hold, Shift, ShiftIndex, sampletime, SampleTime diff --git a/src/clock.jl b/src/clock.jl index 2cd00a24bc..1c9ed89128 100644 --- a/src/clock.jl +++ b/src/clock.jl @@ -1,20 +1,12 @@ -module InferredClock - -export InferredTimeDomain - -using Expronicon.ADT: @adt, @match -using SciMLBase: TimeDomain - -@adt InferredTimeDomain begin +@data InferredClock begin Inferred InferredDiscrete end -Base.Broadcast.broadcastable(x::InferredTimeDomain) = Ref(x) +const InferredTimeDomain = InferredClock.Type +using .InferredClock: Inferred, InferredDiscrete -end - -using .InferredClock +Base.Broadcast.broadcastable(x::InferredTimeDomain) = Ref(x) struct VariableTimeDomain end Symbolics.option_to_metadata_type(::Val{:timedomain}) = VariableTimeDomain @@ -29,7 +21,7 @@ true if `x` contains only continuous-domain signals. See also [`has_continuous_domain`](@ref) """ function is_continuous_domain(x) - issym(x) && return getmetadata(x, VariableTimeDomain, false) == Continuous() + issym(x) && return getmetadata(x, VariableTimeDomain, false) == ContinuousClock() !has_discrete_domain(x) && has_continuous_domain(x) end @@ -50,7 +42,7 @@ has_time_domain(_, x) = has_time_domain(x) Determine if variable `x` has a time-domain attributed to it. """ function has_time_domain(x::Symbolic) - # getmetadata(x, Continuous, nothing) !== nothing || + # getmetadata(x, ContinuousClock, nothing) !== nothing || # getmetadata(x, Discrete, nothing) !== nothing getmetadata(x, VariableTimeDomain, nothing) !== nothing end @@ -58,8 +50,8 @@ has_time_domain(x::Num) = has_time_domain(value(x)) has_time_domain(x) = false for op in [Differential] - @eval input_timedomain(::$op, arg = nothing) = Continuous() - @eval output_timedomain(::$op, arg = nothing) = Continuous() + @eval input_timedomain(::$op, arg = nothing) = ContinuousClock() + @eval output_timedomain(::$op, arg = nothing) = ContinuousClock() end """ @@ -104,8 +96,8 @@ function is_discrete_domain(x) !has_discrete_domain(x) && has_continuous_domain(x) end -sampletime(c) = @match c begin - PeriodicClock(dt, _...) => dt +sampletime(c) = Moshi.Match.@match c begin + PeriodicClock(dt) => dt _ => nothing end diff --git a/src/discretedomain.jl b/src/discretedomain.jl index c7f90007c5..5693347a13 100644 --- a/src/discretedomain.jl +++ b/src/discretedomain.jl @@ -226,28 +226,28 @@ Base.:-(k::ShiftIndex, i::Int) = k + (-i) """ input_timedomain(op::Operator) -Return the time-domain type (`Continuous` or `InferredDiscrete`) that `op` operates on. +Return the time-domain type (`ContinuousClock()` or `InferredDiscrete()`) that `op` operates on. """ function input_timedomain(s::Shift, arg = nothing) if has_time_domain(arg) return get_time_domain(arg) end - InferredDiscrete + InferredDiscrete() end """ output_timedomain(op::Operator) -Return the time-domain type (`Continuous` or `InferredDiscrete`) that `op` results in. +Return the time-domain type (`ContinuousClock()` or `InferredDiscrete()`) that `op` results in. """ function output_timedomain(s::Shift, arg = nothing) if has_time_domain(t, arg) return get_time_domain(t, arg) end - InferredDiscrete + InferredDiscrete() end -input_timedomain(::Sample, _ = nothing) = Continuous() +input_timedomain(::Sample, _ = nothing) = ContinuousClock() output_timedomain(s::Sample, _ = nothing) = s.clock function input_timedomain(h::Hold, arg = nothing) @@ -256,7 +256,7 @@ function input_timedomain(h::Hold, arg = nothing) end InferredDiscrete # the Hold accepts any discrete end -output_timedomain(::Hold, _ = nothing) = Continuous() +output_timedomain(::Hold, _ = nothing) = ContinuousClock() sampletime(op::Sample, _ = nothing) = sampletime(op.clock) sampletime(op::ShiftIndex, _ = nothing) = sampletime(op.clock) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index 35685d5c73..8fdd36e291 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -631,8 +631,8 @@ SymbolicUtils.promote_symtype(::Type{Initial}, T) = T SymbolicUtils.isbinop(::Initial) = false Base.nameof(::Initial) = :Initial Base.show(io::IO, x::Initial) = print(io, "Initial") -input_timedomain(::Initial, _ = nothing) = Continuous() -output_timedomain(::Initial, _ = nothing) = Continuous() +input_timedomain(::Initial, _ = nothing) = ContinuousClock() +output_timedomain(::Initial, _ = nothing) = ContinuousClock() function (f::Initial)(x) # wrap output if wrapped input diff --git a/src/systems/clock_inference.jl b/src/systems/clock_inference.jl index 611f8e2fae..b535773061 100644 --- a/src/systems/clock_inference.jl +++ b/src/systems/clock_inference.jl @@ -8,8 +8,8 @@ end function ClockInference(ts::TransformationState) @unpack structure = ts @unpack graph = structure - eq_domain = TimeDomain[Continuous() for _ in 1:nsrcs(graph)] - var_domain = TimeDomain[Continuous() for _ in 1:ndsts(graph)] + eq_domain = TimeDomain[ContinuousClock() for _ in 1:nsrcs(graph)] + var_domain = TimeDomain[ContinuousClock() for _ in 1:ndsts(graph)] inferred = BitSet() for (i, v) in enumerate(get_fullvars(ts)) d = get_time_domain(ts, v) @@ -151,7 +151,7 @@ function split_system(ci::ClockInference{S}) where {S} get!(clock_to_id, d) do cid = (cid_counter[] += 1) push!(id_to_clock, d) - if d == Continuous() + if d == ContinuousClock() continuous_id[] = cid end cid diff --git a/src/systems/systemstructure.jl b/src/systems/systemstructure.jl index 1bdc11f06a..a8650d9bc8 100644 --- a/src/systems/systemstructure.jl +++ b/src/systems/systemstructure.jl @@ -662,7 +662,8 @@ function structural_simplify!(state::TearingState, io = nothing; simplify = fals Dict(v => 0.0 for v in Iterators.flatten(inputs))) end ps = [sym isa CallWithMetadata ? sym : - setmetadata(sym, VariableTimeDomain, get(time_domains, sym, Continuous())) + setmetadata( + sym, VariableTimeDomain, get(time_domains, sym, ContinuousClock())) for sym in get_ps(sys)] @set! sys.ps = ps else diff --git a/test/clock.jl b/test/clock.jl index 67f469e1a6..961c7af181 100644 --- a/test/clock.jl +++ b/test/clock.jl @@ -1,5 +1,5 @@ using ModelingToolkit, Test, Setfield, OrdinaryDiffEq, DiffEqCallbacks -using ModelingToolkit: Continuous +using ModelingToolkit: ContinuousClock using ModelingToolkit: t_nounits as t, D_nounits as D function infer_clocks(sys) @@ -77,19 +77,19 @@ k = ShiftIndex(d) d = Clock(dt) # Note that TearingState reorders the equations -@test eqmap[1] == Continuous() +@test eqmap[1] == ContinuousClock() @test eqmap[2] == d @test eqmap[3] == d @test eqmap[4] == d -@test eqmap[5] == Continuous() -@test eqmap[6] == Continuous() +@test eqmap[5] == ContinuousClock() +@test eqmap[6] == ContinuousClock() @test varmap[yd] == d @test varmap[ud] == d @test varmap[r] == d -@test varmap[x] == Continuous() -@test varmap[y] == Continuous() -@test varmap[u] == Continuous() +@test varmap[x] == ContinuousClock() +@test varmap[y] == ContinuousClock() +@test varmap[u] == ContinuousClock() @info "Testing shift normalization" dt = 0.1 @@ -192,10 +192,10 @@ eqs = [yd ~ Sample(dt)(y) @test varmap[ud1] == d @test varmap[yd2] == d2 @test varmap[ud2] == d2 - @test varmap[r] == Continuous() - @test varmap[x] == Continuous() - @test varmap[y] == Continuous() - @test varmap[u] == Continuous() + @test varmap[r] == ContinuousClock() + @test varmap[x] == ContinuousClock() + @test varmap[y] == ContinuousClock() + @test varmap[u] == ContinuousClock() @info "test composed systems" @@ -241,14 +241,14 @@ eqs = [yd ~ Sample(dt)(y) ci, varmap = infer_clocks(cl) @test varmap[f.x] == Clock(0.5) - @test varmap[p.x] == Continuous() - @test varmap[p.y] == Continuous() + @test varmap[p.x] == ContinuousClock() + @test varmap[p.y] == ContinuousClock() @test varmap[c.ud] == Clock(0.5) @test varmap[c.yd] == Clock(0.5) - @test varmap[c.y] == Continuous() + @test varmap[c.y] == ContinuousClock() @test varmap[f.y] == Clock(0.5) @test varmap[f.u] == Clock(0.5) - @test varmap[p.u] == Continuous() + @test varmap[p.u] == ContinuousClock() @test varmap[c.r] == Clock(0.5) ## Multiple clock rates @@ -281,9 +281,9 @@ eqs = [yd ~ Sample(dt)(y) @test varmap[ud1] == d @test varmap[yd2] == d2 @test varmap[ud2] == d2 - @test varmap[x] == Continuous() - @test varmap[y] == Continuous() - @test varmap[u] == Continuous() + @test varmap[x] == ContinuousClock() + @test varmap[y] == ContinuousClock() + @test varmap[u] == ContinuousClock() ss = structural_simplify(cl) ss_nosplit = structural_simplify(cl; split = false) @@ -398,13 +398,13 @@ eqs = [yd ~ Sample(dt)(y) ci, varmap = infer_clocks(expand_connections(_model)) - @test varmap[_model.plant.input.u] == Continuous() - @test varmap[_model.plant.u] == Continuous() - @test varmap[_model.plant.x] == Continuous() - @test varmap[_model.plant.y] == Continuous() - @test varmap[_model.plant.output.u] == Continuous() - @test varmap[_model.holder.output.u] == Continuous() - @test varmap[_model.sampler.input.u] == Continuous() + @test varmap[_model.plant.input.u] == ContinuousClock() + @test varmap[_model.plant.u] == ContinuousClock() + @test varmap[_model.plant.x] == ContinuousClock() + @test varmap[_model.plant.y] == ContinuousClock() + @test varmap[_model.plant.output.u] == ContinuousClock() + @test varmap[_model.holder.output.u] == ContinuousClock() + @test varmap[_model.sampler.input.u] == ContinuousClock() @test varmap[_model.controller.u] == d @test varmap[_model.holder.input.u] == d @test varmap[_model.controller.output.u] == d From a797008ab49846fcfa659842a2168c070866b31d Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 24 Feb 2025 18:11:46 +0530 Subject: [PATCH 0996/2176] refactor: format --- src/systems/diffeqs/abstractodesystem.jl | 14 +- src/systems/diffeqs/odesystem.jl | 35 ++-- src/systems/problem_utils.jl | 7 +- test/bvproblem.jl | 193 ++++++++++++----------- test/odesystem.jl | 12 +- test/problem_validation.jl | 22 +-- 6 files changed, 150 insertions(+), 133 deletions(-) diff --git a/src/systems/diffeqs/abstractodesystem.jl b/src/systems/diffeqs/abstractodesystem.jl index 1f6014a357..205d7e4601 100644 --- a/src/systems/diffeqs/abstractodesystem.jl +++ b/src/systems/diffeqs/abstractodesystem.jl @@ -841,35 +841,35 @@ function SciMLBase.BVProblem{iip, specialize}(sys::AbstractODESystem, u0map = [] eval_expression = false, eval_module = @__MODULE__, kwargs...) where {iip, specialize} - if !iscomplete(sys) error("A completed system is required. Call `complete` or `structural_simplify` on the system before creating an `BVProblem`") end !isnothing(callback) && error("BVP solvers do not support callbacks.") - has_alg_eqs(sys) && error("The BVProblem constructor currently does not support ODESystems with algebraic equations.") # Remove this when the BVDAE solvers get updated, the codegen should work when it does. + has_alg_eqs(sys) && + error("The BVProblem constructor currently does not support ODESystems with algebraic equations.") # Remove this when the BVDAE solvers get updated, the codegen should work when it does. sts = unknowns(sys) ps = parameters(sys) constraintsys = get_constraintsystem(sys) if !isnothing(constraintsys) - (length(constraints(constraintsys)) + length(u0map) > length(sts)) && - @warn "The BVProblem is overdetermined. The total number of conditions (# constraints + # fixed initial values given by u0map) exceeds the total number of states. The BVP solvers will default to doing a nonlinear least-squares optimization." + (length(constraints(constraintsys)) + length(u0map) > length(sts)) && + @warn "The BVProblem is overdetermined. The total number of conditions (# constraints + # fixed initial values given by u0map) exceeds the total number of states. The BVP solvers will default to doing a nonlinear least-squares optimization." end # ODESystems without algebraic equations should use both fixed values + guesses # for initialization. - _u0map = has_alg_eqs(sys) ? u0map : merge(Dict(u0map), Dict(guesses)) + _u0map = has_alg_eqs(sys) ? u0map : merge(Dict(u0map), Dict(guesses)) f, u0, p = process_SciMLProblem(ODEFunction{iip, specialize}, sys, _u0map, parammap; t = tspan !== nothing ? tspan[1] : tspan, guesses, check_length, warn_initialize_determined, eval_expression, eval_module, kwargs...) stidxmap = Dict([v => i for (i, v) in enumerate(sts)]) - u0_idxs = has_alg_eqs(sys) ? collect(1:length(sts)) : [stidxmap[k] for (k,v) in u0map] + u0_idxs = has_alg_eqs(sys) ? collect(1:length(sts)) : [stidxmap[k] for (k, v) in u0map] fns = generate_function_bc(sys, u0, u0_idxs, tspan) - bc_oop, bc_iip = eval_or_rgf.(fns; eval_expression, eval_module) + bc_oop, bc_iip = eval_or_rgf.(fns; eval_expression, eval_module) bc(sol, p, t) = bc_oop(sol, p, t) bc(resid, u, p, t) = bc_iip(resid, u, p, t) diff --git a/src/systems/diffeqs/odesystem.jl b/src/systems/diffeqs/odesystem.jl index 37f921303c..33cdc59909 100644 --- a/src/systems/diffeqs/odesystem.jl +++ b/src/systems/diffeqs/odesystem.jl @@ -193,7 +193,8 @@ struct ODESystem <: AbstractODESystem """ parent::Any - function ODESystem(tag, deqs, iv, dvs, ps, tspan, var_to_name, ctrls, observed, constraints, tgrad, + function ODESystem( + tag, deqs, iv, dvs, ps, tspan, var_to_name, ctrls, observed, constraints, tgrad, jac, ctrl_jac, Wfact, Wfact_t, name, description, systems, defaults, guesses, torn_matching, initializesystem, initialization_eqs, schedule, connector_type, preface, cevents, @@ -214,7 +215,8 @@ struct ODESystem <: AbstractODESystem u = __get_unit_type(dvs, ps, iv) check_units(u, deqs) end - new(tag, deqs, iv, dvs, ps, tspan, var_to_name, ctrls, observed, constraints, tgrad, jac, + new(tag, deqs, iv, dvs, ps, tspan, var_to_name, + ctrls, observed, constraints, tgrad, jac, ctrl_jac, Wfact, Wfact_t, name, description, systems, defaults, guesses, torn_matching, initializesystem, initialization_eqs, schedule, connector_type, preface, cevents, devents, parameter_dependencies, assertions, metadata, @@ -300,16 +302,16 @@ function ODESystem(deqs::AbstractVector{<:Equation}, iv, dvs, ps; if is_dde === nothing is_dde = _check_if_dde(deqs, iv′, systems) end - + if !isempty(systems) && !isnothing(constraintsystem) conssystems = ConstraintsSystem[] for sys in systems cons = get_constraintsystem(sys) - cons !== nothing && push!(conssystems, cons) + cons !== nothing && push!(conssystems, cons) end @show conssystems @set! constraintsystem.systems = conssystems - end + end assertions = Dict{BasicSymbolic, Any}(unwrap(k) => v for (k, v) in assertions) @@ -359,9 +361,9 @@ function ODESystem(eqs, iv; constraints = Equation[], kwargs...) if !isempty(constraints) constraintsystem = process_constraint_system(constraints, allunknowns, new_ps, iv) for st in get_unknowns(constraintsystem) - iscall(st) ? - !in(operation(st)(iv), allunknowns) && push!(consvars, st) : - !in(st, allunknowns) && push!(consvars, st) + iscall(st) ? + !in(operation(st)(iv), allunknowns) && push!(consvars, st) : + !in(st, allunknowns) && push!(consvars, st) end for p in parameters(constraintsystem) !in(p, new_ps) && push!(new_ps, p) @@ -712,7 +714,8 @@ end # Validate that all the variables in the BVP constraints are well-formed states or parameters. # - Callable/delay variables (e.g. of the form x(0.6) should be unknowns of the system (and have one arg, etc.) # - Callable/delay parameters should be parameters of the system (and have one arg, etc.) -function process_constraint_system(constraints::Vector{Equation}, sts, ps, iv; consname = :cons) +function process_constraint_system( + constraints::Vector{Equation}, sts, ps, iv; consname = :cons) isempty(constraints) && return nothing constraintsts = OrderedSet() @@ -725,22 +728,26 @@ function process_constraint_system(constraints::Vector{Equation}, sts, ps, iv; c # Validate the states. for var in constraintsts if !iscall(var) - occursin(iv, var) && (var ∈ sts || throw(ArgumentError("Time-dependent variable $var is not an unknown of the system."))) + occursin(iv, var) && (var ∈ sts || + throw(ArgumentError("Time-dependent variable $var is not an unknown of the system."))) elseif length(arguments(var)) > 1 throw(ArgumentError("Too many arguments for variable $var.")) elseif length(arguments(var)) == 1 arg = only(arguments(var)) - operation(var)(iv) ∈ sts || + operation(var)(iv) ∈ sts || throw(ArgumentError("Variable $var is not a variable of the ODESystem. Called variables must be variables of the ODESystem.")) - isequal(arg, iv) || isparameter(arg) || arg isa Integer || arg isa AbstractFloat || + isequal(arg, iv) || isparameter(arg) || arg isa Integer || + arg isa AbstractFloat || throw(ArgumentError("Invalid argument specified for variable $var. The argument of the variable should be either $iv, a parameter, or a value specifying the time that the constraint holds.")) isparameter(arg) && push!(constraintps, arg) else - var ∈ sts && @warn "Variable $var has no argument. It will be interpreted as $var($iv), and the constraint will apply to the entire interval." + var ∈ sts && + @warn "Variable $var has no argument. It will be interpreted as $var($iv), and the constraint will apply to the entire interval." end end - ConstraintsSystem(constraints, collect(constraintsts), collect(constraintps); name = consname) + ConstraintsSystem( + constraints, collect(constraintsts), collect(constraintps); name = consname) end diff --git a/src/systems/problem_utils.jl b/src/systems/problem_utils.jl index 1f40f61eed..77f4229696 100644 --- a/src/systems/problem_utils.jl +++ b/src/systems/problem_utils.jl @@ -872,7 +872,8 @@ function check_inputmap_keys(sys, u0map, pmap) push!(badparamkeys, k) end end - (isempty(badvarkeys) && isempty(badparamkeys)) || throw(InvalidKeyError(collect(badvarkeys), collect(badparamkeys))) + (isempty(badvarkeys) && isempty(badparamkeys)) || + throw(InvalidKeyError(collect(badvarkeys), collect(badparamkeys))) end const BAD_KEY_MESSAGE = """ @@ -885,14 +886,12 @@ struct InvalidKeyError <: Exception params::Any end -function Base.showerror(io::IO, e::InvalidKeyError) +function Base.showerror(io::IO, e::InvalidKeyError) println(io, BAD_KEY_MESSAGE) println(io, "u0map: $(join(e.vars, ", "))") println(io, "pmap: $(join(e.params, ", "))") end - - ############## # Legacy functions for backward compatibility ############## diff --git a/test/bvproblem.jl b/test/bvproblem.jl index f05be90281..c5451f681b 100644 --- a/test/bvproblem.jl +++ b/test/bvproblem.jl @@ -12,70 +12,73 @@ solvers = [MIRK4] daesolvers = [Ascher2, Ascher4, Ascher6] let - @parameters α=7.5 β=4.0 γ=8.0 δ=5.0 - @variables x(t)=1.0 y(t)=2.0 - - eqs = [D(x) ~ α * x - β * x * y, - D(y) ~ -γ * y + δ * x * y] - - u0map = [x => 1.0, y => 2.0] - parammap = [α => 7.5, β => 4, γ => 8.0, δ => 5.0] - tspan = (0.0, 10.0) - - @mtkbuild lotkavolterra = ODESystem(eqs, t) - op = ODEProblem(lotkavolterra, u0map, tspan, parammap) - osol = solve(op, Vern9()) - - bvp = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}(lotkavolterra, u0map, tspan, parammap) - - for solver in solvers - sol = solve(bvp, solver(), dt = 0.01) - @test isapprox(sol.u[end], osol.u[end]; atol = 0.01) - @test sol.u[1] == [1.0, 2.0] - end - - # Test out of place - bvp2 = SciMLBase.BVProblem{false, SciMLBase.AutoSpecialize}(lotkavolterra, u0map, tspan, parammap) - - for solver in solvers - sol = solve(bvp2, solver(), dt = 0.01) - @test isapprox(sol.u[end], osol.u[end]; atol = 0.01) - @test sol.u[1] == [1.0, 2.0] - end + @parameters α=7.5 β=4.0 γ=8.0 δ=5.0 + @variables x(t)=1.0 y(t)=2.0 + + eqs = [D(x) ~ α * x - β * x * y, + D(y) ~ -γ * y + δ * x * y] + + u0map = [x => 1.0, y => 2.0] + parammap = [α => 7.5, β => 4, γ => 8.0, δ => 5.0] + tspan = (0.0, 10.0) + + @mtkbuild lotkavolterra = ODESystem(eqs, t) + op = ODEProblem(lotkavolterra, u0map, tspan, parammap) + osol = solve(op, Vern9()) + + bvp = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}( + lotkavolterra, u0map, tspan, parammap) + + for solver in solvers + sol = solve(bvp, solver(), dt = 0.01) + @test isapprox(sol.u[end], osol.u[end]; atol = 0.01) + @test sol.u[1] == [1.0, 2.0] + end + + # Test out of place + bvp2 = SciMLBase.BVProblem{false, SciMLBase.AutoSpecialize}( + lotkavolterra, u0map, tspan, parammap) + + for solver in solvers + sol = solve(bvp2, solver(), dt = 0.01) + @test isapprox(sol.u[end], osol.u[end]; atol = 0.01) + @test sol.u[1] == [1.0, 2.0] + end end ### Testing on pendulum let - @parameters g=9.81 L=1.0 - @variables θ(t) = π / 2 θ_t(t) - - eqs = [D(θ) ~ θ_t - D(θ_t) ~ -(g / L) * sin(θ)] - - @mtkbuild pend = ODESystem(eqs, t) - - u0map = [θ => π / 2, θ_t => π / 2] - parammap = [:L => 1.0, :g => 9.81] - tspan = (0.0, 6.0) - - op = ODEProblem(pend, u0map, tspan, parammap) - osol = solve(op, Vern9()) - - bvp = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}(pend, u0map, tspan, parammap) - for solver in solvers - sol = solve(bvp, solver(), dt = 0.01) - @test isapprox(sol.u[end], osol.u[end]; atol = 0.01) - @test sol.u[1] == [π / 2, π / 2] - end - - # Test out-of-place - bvp2 = SciMLBase.BVProblem{false, SciMLBase.FullSpecialize}(pend, u0map, tspan, parammap) - - for solver in solvers - sol = solve(bvp2, solver(), dt = 0.01) - @test isapprox(sol.u[end], osol.u[end]; atol = 0.01) - @test sol.u[1] == [π / 2, π / 2] - end + @parameters g=9.81 L=1.0 + @variables θ(t)=π / 2 θ_t(t) + + eqs = [D(θ) ~ θ_t + D(θ_t) ~ -(g / L) * sin(θ)] + + @mtkbuild pend = ODESystem(eqs, t) + + u0map = [θ => π / 2, θ_t => π / 2] + parammap = [:L => 1.0, :g => 9.81] + tspan = (0.0, 6.0) + + op = ODEProblem(pend, u0map, tspan, parammap) + osol = solve(op, Vern9()) + + bvp = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}(pend, u0map, tspan, parammap) + for solver in solvers + sol = solve(bvp, solver(), dt = 0.01) + @test isapprox(sol.u[end], osol.u[end]; atol = 0.01) + @test sol.u[1] == [π / 2, π / 2] + end + + # Test out-of-place + bvp2 = SciMLBase.BVProblem{false, SciMLBase.FullSpecialize}( + pend, u0map, tspan, parammap) + + for solver in solvers + sol = solve(bvp2, solver(), dt = 0.01) + @test isapprox(sol.u[end], osol.u[end]; atol = 0.01) + @test sol.u[1] == [π / 2, π / 2] + end end ################################################################## @@ -87,40 +90,42 @@ let @parameters α=1.5 β=1.0 γ=3.0 δ=1.0 @variables x(..) y(..) eqs = [D(x(t)) ~ α * x(t) - β * x(t) * y(t), - D(y(t)) ~ -γ * y(t) + δ * x(t) * y(t)] - - tspan = (0., 1.) + D(y(t)) ~ -γ * y(t) + δ * x(t) * y(t)] + + tspan = (0.0, 1.0) @mtkbuild lksys = ODESystem(eqs, t) - function lotkavolterra!(du, u, p, t) - du[1] = p[1]*u[1] - p[2]*u[1]*u[2] - du[2] = -p[4]*u[2] + p[3]*u[1]*u[2] + function lotkavolterra!(du, u, p, t) + du[1] = p[1] * u[1] - p[2] * u[1] * u[2] + du[2] = -p[4] * u[2] + p[3] * u[1] * u[2] end - function lotkavolterra(u, p, t) - [p[1]*u[1] - p[2]*u[1]*u[2], -p[4]*u[2] + p[3]*u[1]*u[2]] + function lotkavolterra(u, p, t) + [p[1] * u[1] - p[2] * u[1] * u[2], -p[4] * u[2] + p[3] * u[1] * u[2]] end # Test with a constraint. - constr = [y(0.5) ~ 2.] + constr = [y(0.5) ~ 2.0] @mtkbuild lksys = ODESystem(eqs, t; constraints = constr) - function bc!(resid, u, p, t) - resid[1] = u(0.0)[1] - 1. - resid[2] = u(0.5)[2] - 2. + function bc!(resid, u, p, t) + resid[1] = u(0.0)[1] - 1.0 + resid[2] = u(0.5)[2] - 2.0 end function bc(u, p, t) - [u(0.0)[1] - 1., u(0.5)[2] - 2.] + [u(0.0)[1] - 1.0, u(0.5)[2] - 2.0] end - u0 = [1., 1.] - tspan = (0., 1.) - p = [1.5, 1., 1., 3.] + u0 = [1.0, 1.0] + tspan = (0.0, 1.0) + p = [1.5, 1.0, 1.0, 3.0] bvpi1 = SciMLBase.BVProblem(lotkavolterra!, bc!, u0, tspan, p) bvpi2 = SciMLBase.BVProblem(lotkavolterra, bc, u0, tspan, p) - bvpi3 = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}(lksys, [x(t) => 1.], tspan; guesses = [y(t) => 1.]) - bvpi4 = SciMLBase.BVProblem{false, SciMLBase.FullSpecialize}(lksys, [x(t) => 1.], tspan; guesses = [y(t) => 1.]) - + bvpi3 = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}( + lksys, [x(t) => 1.0], tspan; guesses = [y(t) => 1.0]) + bvpi4 = SciMLBase.BVProblem{false, SciMLBase.FullSpecialize}( + lksys, [x(t) => 1.0], tspan; guesses = [y(t) => 1.0]) + sol1 = @btime solve($bvpi1, MIRK4(), dt = 0.01) sol2 = @btime solve($bvpi2, MIRK4(), dt = 0.01) sol3 = @btime solve($bvpi3, MIRK4(), dt = 0.01) @@ -128,12 +133,15 @@ let @test sol1 ≈ sol2 ≈ sol3 ≈ sol4 # don't get true equality here, not sure why end -function test_solvers(solvers, prob, u0map, constraints, equations = []; dt = 0.05, atol = 1e-2) +function test_solvers( + solvers, prob, u0map, constraints, equations = []; dt = 0.05, atol = 1e-2) for solver in solvers println("Solver: $solver") sol = @btime solve($prob, $solver(), dt = $dt, abstol = $atol) @test SciMLBase.successful_retcode(sol.retcode) - p = prob.p; t = sol.t; bc = prob.f.bc + p = prob.p + t = sol.t + bc = prob.f.bc ns = length(prob.u0) if isinplace(prob.f) resid = zeros(ns) @@ -148,7 +156,7 @@ function test_solvers(solvers, prob, u0map, constraints, equations = []; dt = 0. for (k, v) in u0map @test sol[k][1] == v end - + # for cons in constraints # @test sol[cons.rhs - cons.lhs] ≈ 0 # end @@ -163,28 +171,31 @@ end let @parameters α=1.5 β=1.0 γ=3.0 δ=1.0 @variables x(..) y(..) - + eqs = [D(x(t)) ~ α * x(t) - β * x(t) * y(t), - D(y(t)) ~ -γ * y(t) + δ * x(t) * y(t)] - + D(y(t)) ~ -γ * y(t) + δ * x(t) * y(t)] + u0map = [] tspan = (0.0, 1.0) guess = [x(t) => 4.0, y(t) => 2.0] - constr = [x(.6) ~ 3.5, x(.3) ~ 7.] + constr = [x(0.6) ~ 3.5, x(0.3) ~ 7.0] @mtkbuild lksys = ODESystem(eqs, t; constraints = constr) - bvp = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}(lksys, u0map, tspan; guesses = guess) + bvp = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}( + lksys, u0map, tspan; guesses = guess) test_solvers(solvers, bvp, u0map, constr; dt = 0.05) # Testing that more complicated constraints give correct solutions. - constr = [y(.2) + x(.8) ~ 3., y(.3) ~ 2.] + constr = [y(0.2) + x(0.8) ~ 3.0, y(0.3) ~ 2.0] @mtkbuild lksys = ODESystem(eqs, t; constraints = constr) - bvp = SciMLBase.BVProblem{false, SciMLBase.FullSpecialize}(lksys, u0map, tspan; guesses = guess) + bvp = SciMLBase.BVProblem{false, SciMLBase.FullSpecialize}( + lksys, u0map, tspan; guesses = guess) test_solvers(solvers, bvp, u0map, constr; dt = 0.05) - constr = [α * β - x(.6) ~ 0.0, y(.2) ~ 3.] + constr = [α * β - x(0.6) ~ 0.0, y(0.2) ~ 3.0] @mtkbuild lksys = ODESystem(eqs, t; constraints = constr) - bvp = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}(lksys, u0map, tspan; guesses = guess) + bvp = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}( + lksys, u0map, tspan; guesses = guess) test_solvers(solvers, bvp, u0map, constr) end diff --git a/test/odesystem.jl b/test/odesystem.jl index ae39aa4c5b..01df23ede8 100644 --- a/test/odesystem.jl +++ b/test/odesystem.jl @@ -1676,9 +1676,9 @@ end @testset "Constraint system construction" begin @variables x(..) y(..) z(..) - @parameters a b c d e - eqs = [D(x(t)) ~ 3*a*y(t), D(y(t)) ~ x(t) - z(t), D(z(t)) ~ e*x(t)^2] - cons = [x(0.3) ~ c*d, y(0.7) ~ 3] + @parameters a b c d e + eqs = [D(x(t)) ~ 3 * a * y(t), D(y(t)) ~ x(t) - z(t), D(z(t)) ~ e * x(t)^2] + cons = [x(0.3) ~ c * d, y(0.7) ~ 3] # Test variables + parameters infer correctly. @mtkbuild sys = ODESystem(eqs, t; constraints = cons) @@ -1688,12 +1688,12 @@ end @parameters t_c cons = [x(t_c) ~ 3] @mtkbuild sys = ODESystem(eqs, t; constraints = cons) - @test issetequal(parameters(sys), [a, e, t_c]) + @test issetequal(parameters(sys), [a, e, t_c]) @parameters g(..) h i cons = [g(h, i) * x(3) ~ c] @mtkbuild sys = ODESystem(eqs, t; constraints = cons) - @test issetequal(parameters(sys), [g, h, i, a, e, c]) + @test issetequal(parameters(sys), [g, h, i, a, e, c]) # Test that bad constraints throw errors. cons = [x(3, 4) ~ 3] # unknowns cannot have multiple args. @@ -1716,7 +1716,7 @@ end 2 0 0 2 1 0 0 2 0 5] eqs = D(x(t)) ~ mat * x(t) - cons = [x(3) ~ [2,3,3,5,4]] + cons = [x(3) ~ [2, 3, 3, 5, 4]] @mtkbuild ode = ODESystem(D(x(t)) ~ mat * x(t), t; constraints = cons) @test length(constraints(ModelingToolkit.get_constraintsystem(ode))) == 5 end diff --git a/test/problem_validation.jl b/test/problem_validation.jl index bce39b51d2..a0a7afaf3c 100644 --- a/test/problem_validation.jl +++ b/test/problem_validation.jl @@ -2,33 +2,33 @@ using ModelingToolkit using ModelingToolkit: t_nounits as t, D_nounits as D @testset "Input map validation" begin - import ModelingToolkit: InvalidKeyError, MissingParametersError + import ModelingToolkit: InvalidKeyError, MissingParametersError @variables X(t) @parameters p d - eqs = [D(X) ~ p - d*X] + 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 MissingParametersError oprob = ODEProblem(osys, u0, (0.0, 1.0), ps) - + @test_throws MissingParametersError 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 InvalidKeyError oprob = ODEProblem(osys, u0, (0.0, 1.0), ps) + @test_throws InvalidKeyError oprob=ODEProblem(osys, u0, (0.0, 1.0), ps) u0 = [:X => 1.0, "random" => 3.0] - @test_throws InvalidKeyError oprob = ODEProblem(osys, u0, (0.0, 1.0), ps) + @test_throws InvalidKeyError oprob=ODEProblem(osys, u0, (0.0, 1.0), ps) @variables x(t) y(t) z(t) - @parameters a b c d - eqs = [D(x) ~ x*a, D(y) ~ y*c, D(z) ~ b + d] + @parameters a b c d + eqs = [D(x) ~ x * a, D(y) ~ y * c, D(z) ~ b + d] @mtkbuild sys = ODESystem(eqs, t) pmap = [a => 1, b => 2, c => 3, d => 4, "b" => 2] u0map = [x => 1, y => 2, z => 3] - @test_throws InvalidKeyError ODEProblem(sys, u0map, (0., 1.), pmap) + @test_throws InvalidKeyError ODEProblem(sys, u0map, (0.0, 1.0), pmap) pmap = [a => 1, b => 2, c => 3, d => 4] u0map = [x => 1, y => 2, z => 3, :0 => 3] - @test_throws InvalidKeyError ODEProblem(sys, u0map, (0., 1.), pmap) + @test_throws InvalidKeyError ODEProblem(sys, u0map, (0.0, 1.0), pmap) end From f29824e829041f01f60a32ada65c5249f91a9746 Mon Sep 17 00:00:00 2001 From: vyudu Date: Mon, 24 Feb 2025 09:25:21 -0500 Subject: [PATCH 0997/2176] Format --- .../StructuralTransformations.jl | 6 ++- .../symbolics_tearing.jl | 54 +++++++++++-------- src/structural_transformation/utils.jl | 8 +-- .../discrete_system/discrete_system.jl | 6 +-- src/systems/systemstructure.jl | 6 +-- src/variables.jl | 2 +- test/discrete_system.jl | 44 +++++++-------- 7 files changed, 69 insertions(+), 57 deletions(-) diff --git a/src/structural_transformation/StructuralTransformations.jl b/src/structural_transformation/StructuralTransformations.jl index 7d2b9afa26..f0124d7f4b 100644 --- a/src/structural_transformation/StructuralTransformations.jl +++ b/src/structural_transformation/StructuralTransformations.jl @@ -11,7 +11,8 @@ using SymbolicUtils: maketerm, iscall using ModelingToolkit using ModelingToolkit: ODESystem, AbstractSystem, var_from_nested_derivative, Differential, - unknowns, equations, vars, Symbolic, diff2term_with_unit, shift2term_with_unit, value, + unknowns, equations, vars, Symbolic, diff2term_with_unit, + shift2term_with_unit, value, operation, arguments, Sym, Term, simplify, symbolic_linear_solve, isdiffeq, isdifferential, isirreducible, empty_substitutions, get_substitutions, @@ -22,7 +23,8 @@ using ModelingToolkit: ODESystem, AbstractSystem, var_from_nested_derivative, Di get_postprocess_fbody, vars!, IncrementalCycleTracker, add_edge_checked!, topological_sort, invalidate_cache!, Substitutions, get_or_construct_tearing_state, - filter_kwargs, lower_varname_with_unit, lower_shift_varname_with_unit, setio, SparseMatrixCLIL, + filter_kwargs, lower_varname_with_unit, + lower_shift_varname_with_unit, setio, SparseMatrixCLIL, get_fullvars, has_equations, observed, Schedule, schedule diff --git a/src/structural_transformation/symbolics_tearing.jl b/src/structural_transformation/symbolics_tearing.jl index bbb6853a7c..ea594f2172 100644 --- a/src/structural_transformation/symbolics_tearing.jl +++ b/src/structural_transformation/symbolics_tearing.jl @@ -248,7 +248,8 @@ called dummy derivatives. State selection is done. All non-differentiated variables are algebraic variables, and all variables that appear differentiated are differential variables. """ -function substitute_derivatives_algevars!(ts::TearingState, neweqs, var_eq_matching, dummy_sub; iv = nothing, D = nothing) +function substitute_derivatives_algevars!( + ts::TearingState, neweqs, var_eq_matching, dummy_sub; iv = nothing, D = nothing) @unpack fullvars, sys, structure = ts @unpack solvable_graph, var_to_diff, eq_to_diff, graph = structure diff_to_var = invview(var_to_diff) @@ -288,7 +289,7 @@ end #= There are three cases where we want to generate new variables to convert the system into first order (semi-implicit) ODEs. - + 1. To first order: Whenever higher order differentiated variable like `D(D(D(x)))` appears, we introduce new variables `x_t`, `x_tt`, and `x_ttt` and new equations @@ -364,7 +365,8 @@ Effects on the system structure: - solvable_graph: - var_eq_matching: match D(x) to the added identity equation D(x) ~ x_t """ -function generate_derivative_variables!(ts::TearingState, neweqs, var_eq_matching; mm = nothing, iv = nothing, D = nothing) +function generate_derivative_variables!( + ts::TearingState, neweqs, var_eq_matching; mm = nothing, iv = nothing, D = nothing) @unpack fullvars, sys, structure = ts @unpack solvable_graph, var_to_diff, eq_to_diff, graph = structure eq_var_matching = invview(var_eq_matching) @@ -395,7 +397,7 @@ function generate_derivative_variables!(ts::TearingState, neweqs, var_eq_matchin dx = fullvars[dv] order, lv = var_order(dv, diff_to_var) x_t = is_discrete ? lower_shift_varname_with_unit(fullvars[dv], iv) : - lower_varname_with_unit(fullvars[lv], iv, order) + lower_varname_with_unit(fullvars[lv], iv, order) # Add `x_t` to the graph v_t = add_dd_variable!(structure, fullvars, x_t, dv) @@ -405,7 +407,7 @@ function generate_derivative_variables!(ts::TearingState, neweqs, var_eq_matchin # Update matching push!(var_eq_matching, unassigned) var_eq_matching[dv] = unassigned - eq_var_matching[dummy_eq] = dv + eq_var_matching[dummy_eq] = dv end end @@ -428,7 +430,7 @@ function find_duplicate_dd(dv, solvable_graph, diff_to_var, linear_eqs, mm) return eq, v_t end end - return nothing + return nothing end """ @@ -492,8 +494,9 @@ Order the new equations and variables such that the differential equations and variables come first. Return the new equations, the solved equations, the new orderings, and the number of solved variables and equations. """ -function generate_system_equations!(state::TearingState, neweqs, var_eq_matching; simplify = false, iv = nothing, D = nothing) - @unpack fullvars, sys, structure = state +function generate_system_equations!(state::TearingState, neweqs, var_eq_matching; + simplify = false, iv = nothing, D = nothing) + @unpack fullvars, sys, structure = state @unpack solvable_graph, var_to_diff, eq_to_diff, graph = structure eq_var_matching = invview(var_eq_matching) diff_to_var = invview(var_to_diff) @@ -502,11 +505,12 @@ function generate_system_equations!(state::TearingState, neweqs, var_eq_matching if is_only_discrete(structure) for (i, v) in enumerate(fullvars) op = operation(v) - op isa Shift && (op.steps < 0) && begin - lowered = lower_shift_varname_with_unit(v, iv) - total_sub[v] = lowered - fullvars[i] = lowered - end + op isa Shift && (op.steps < 0) && + begin + lowered = lower_shift_varname_with_unit(v, iv) + total_sub[v] = lowered + fullvars[i] = lowered + end end end @@ -581,10 +585,11 @@ function generate_system_equations!(state::TearingState, neweqs, var_eq_matching end solved_vars_set = BitSet(solved_vars) var_ordering = [diff_vars; - setdiff!(setdiff(1:ndsts(graph), diff_vars_set), - solved_vars_set)] + setdiff!(setdiff(1:ndsts(graph), diff_vars_set), + solved_vars_set)] - return neweqs, solved_eqs, eq_ordering, var_ordering, length(solved_vars), length(solved_vars_set) + return neweqs, solved_eqs, eq_ordering, var_ordering, length(solved_vars), + length(solved_vars_set) end """ @@ -648,7 +653,8 @@ Eliminate the solved variables and equations from the graph and permute the graph's vertices to account for the new variable/equation ordering. """ # TODO: BLT sorting -function reorder_vars!(state::TearingState, var_eq_matching, eq_ordering, var_ordering, nsolved_eq, nsolved_var) +function reorder_vars!(state::TearingState, var_eq_matching, eq_ordering, + var_ordering, nsolved_eq, nsolved_var) @unpack solvable_graph, var_to_diff, eq_to_diff, graph = state.structure eqsperm = zeros(Int, nsrcs(graph)) @@ -692,7 +698,8 @@ end """ Update the system equations, unknowns, and observables after simplification. """ -function update_simplified_system!(state::TearingState, neweqs, solved_eqs, dummy_sub, var_eq_matching, extra_unknowns; +function update_simplified_system!( + state::TearingState, neweqs, solved_eqs, dummy_sub, var_eq_matching, extra_unknowns; cse_hack = true, array_hack = true) @unpack solvable_graph, var_to_diff, eq_to_diff, graph = state.structure diff_to_var = invview(var_to_diff) @@ -732,7 +739,6 @@ function update_simplified_system!(state::TearingState, neweqs, solved_eqs, dumm sys = schedule(sys) end - """ Give the order of the variable indexed by dv. """ @@ -790,12 +796,14 @@ function tearing_reassemble(state::TearingState, var_eq_matching, generate_derivative_variables!(state, neweqs, var_eq_matching; mm, iv, D) - neweqs, solved_eqs, eq_ordering, var_ordering, nelim_eq, nelim_var = - generate_system_equations!(state, neweqs, var_eq_matching; simplify, iv, D) + neweqs, solved_eqs, eq_ordering, var_ordering, nelim_eq, nelim_var = generate_system_equations!( + state, neweqs, var_eq_matching; simplify, iv, D) - state = reorder_vars!(state, var_eq_matching, eq_ordering, var_ordering, nelim_eq, nelim_var) + state = reorder_vars!( + state, var_eq_matching, eq_ordering, var_ordering, nelim_eq, nelim_var) - sys = update_simplified_system!(state, neweqs, solved_eqs, dummy_sub, var_eq_matching, extra_unknowns; cse_hack, array_hack) + sys = update_simplified_system!(state, neweqs, solved_eqs, dummy_sub, var_eq_matching, + extra_unknowns; cse_hack, array_hack) @set! state.sys = sys @set! sys.tearing_state = state diff --git a/src/structural_transformation/utils.jl b/src/structural_transformation/utils.jl index 96f7f78f99..f3cea9c7ba 100644 --- a/src/structural_transformation/utils.jl +++ b/src/structural_transformation/utils.jl @@ -477,15 +477,17 @@ function shift2term(var) backshift = is_lowered ? op.steps + ModelingToolkit.getshift(arg) : op.steps num = join(Char(0x2080 + d) for d in reverse!(digits(-backshift))) # subscripted number, e.g. ₁ - ds = join([Char(0x209c), Char(0x208b), num]) + ds = join([Char(0x209c), Char(0x208b), num]) # Char(0x209c) = ₜ # Char(0x208b) = ₋ (subscripted minus) O = is_lowered ? ModelingToolkit.getunshifted(arg) : arg oldop = operation(O) - newname = backshift != 0 ? Symbol(string(nameof(oldop)), ds) : Symbol(string(nameof(oldop))) + newname = backshift != 0 ? Symbol(string(nameof(oldop)), ds) : + Symbol(string(nameof(oldop))) - newvar = maketerm(typeof(O), Symbolics.rename(oldop, newname), Symbolics.children(O), Symbolics.metadata(O)) + newvar = maketerm(typeof(O), Symbolics.rename(oldop, newname), + Symbolics.children(O), Symbolics.metadata(O)) newvar = setmetadata(newvar, Symbolics.VariableSource, (:variables, newname)) newvar = setmetadata(newvar, ModelingToolkit.VariableUnshifted, O) newvar = setmetadata(newvar, ModelingToolkit.VariableShift, backshift) diff --git a/src/systems/discrete_system/discrete_system.jl b/src/systems/discrete_system/discrete_system.jl index 3f8d9e85c6..e4949d812f 100644 --- a/src/systems/discrete_system/discrete_system.jl +++ b/src/systems/discrete_system/discrete_system.jl @@ -269,15 +269,15 @@ function shift_u0map_forward(sys::DiscreteSystem, u0map, defs) for k in collect(keys(u0map)) v = u0map[k] if !((op = operation(k)) isa Shift) - isnothing(getunshifted(k)) && error("Initial conditions must be for the past state of the unknowns. Instead of providing the condition for $k, provide the condition for $(Shift(iv, -1)(k)).") - + isnothing(getunshifted(k)) && + error("Initial conditions must be for the past state of the unknowns. Instead of providing the condition for $k, provide the condition for $(Shift(iv, -1)(k)).") + updated[Shift(iv, 1)(k)] = v elseif op.steps > 0 error("Initial conditions must be for the past state of the unknowns. Instead of providing the condition for $k, provide the condition for $(Shift(iv, -1)(only(arguments(k)))).") else updated[Shift(iv, op.steps + 1)(only(arguments(k)))] = v end - end for var in unknowns(sys) op = operation(var) diff --git a/src/systems/systemstructure.jl b/src/systems/systemstructure.jl index 6fee78cfd6..0643f32ec4 100644 --- a/src/systems/systemstructure.jl +++ b/src/systems/systemstructure.jl @@ -473,9 +473,9 @@ function shift_discrete_system(ts::TearingState) end iv = get_iv(sys) - discmap = Dict(k => StructuralTransformations.simplify_shifts(Shift(iv, 1)(k)) - for k in discvars - if any(isequal(k), fullvars) && !isa(operation(k), Union{Sample, Hold})) + discmap = Dict(k => StructuralTransformations.simplify_shifts(Shift(iv, 1)(k)) + for k in discvars + if any(isequal(k), fullvars) && !isa(operation(k), Union{Sample, Hold})) for i in eachindex(fullvars) fullvars[i] = StructuralTransformations.simplify_shifts(fast_substitute( diff --git a/src/variables.jl b/src/variables.jl index 4e13ad2c5d..a7dde165f9 100644 --- a/src/variables.jl +++ b/src/variables.jl @@ -138,7 +138,7 @@ function default_toterm(x) if iscall(x) && (op = operation(x)) isa Operator if !(op isa Differential) if op isa Shift && op.steps < 0 - return shift2term(x) + return shift2term(x) end x = normalize_to_differential(op)(arguments(x)...) end diff --git a/test/discrete_system.jl b/test/discrete_system.jl index 756e5bca48..f232faaf81 100644 --- a/test/discrete_system.jl +++ b/test/discrete_system.jl @@ -257,7 +257,6 @@ k = ShiftIndex(t) @named sys = DiscreteSystem([x ~ x^2 + y^2, y ~ x(k - 1) + y(k - 1)], t) @test_throws ["algebraic equations", "not yet supported"] structural_simplify(sys) - @testset "Passing `nothing` to `u0`" begin @variables x(t) = 1 k = ShiftIndex() @@ -273,11 +272,11 @@ end prob = DiscreteProblem(de, [], (0, 10)) @test prob[x] == 2.0 @test prob[x(k - 1)] == 1.0 - + # must provide initial conditions for history @test_throws ErrorException DiscreteProblem(de, [x => 2.0], (0, 10)) - @test_throws ErrorException DiscreteProblem(de, [x(k+1) => 2.], (0, 10)) - + @test_throws ErrorException DiscreteProblem(de, [x(k + 1) => 2.0], (0, 10)) + # initial values only affect _that timestep_, not the entire history prob = DiscreteProblem(de, [x(k - 1) => 2.0], (0, 10)) @test prob[x] == 3.0 @@ -286,34 +285,35 @@ end @test prob[xₜ₋₁] == 2.0 # Test initial assignment with lowered variable - prob = DiscreteProblem(de, [xₜ₋₁(k-1) => 4.0], (0, 10)) - @test prob[x(k-1)] == prob[xₜ₋₁] == 1.0 - @test prob[x] == 5. + prob = DiscreteProblem(de, [xₜ₋₁(k - 1) => 4.0], (0, 10)) + @test prob[x(k - 1)] == prob[xₜ₋₁] == 1.0 + @test prob[x] == 5.0 # Test missing initial throws error @variables x(t) - @mtkbuild de = DiscreteSystem([x ~ x(k-1) + x(k-2)*x(k-3)], t) - @test_throws ErrorException prob = DiscreteProblem(de, [x(k-3) => 2.], (0, 10)) - @test_throws ErrorException prob = DiscreteProblem(de, [x(k-3) => 2., x(k-1) => 3.], (0, 10)) + @mtkbuild de = DiscreteSystem([x ~ x(k - 1) + x(k - 2) * x(k - 3)], t) + @test_throws ErrorException prob=DiscreteProblem(de, [x(k - 3) => 2.0], (0, 10)) + @test_throws ErrorException prob=DiscreteProblem( + de, [x(k - 3) => 2.0, x(k - 1) => 3.0], (0, 10)) # Test non-assigned initials are given default value - @variables x(t) = 2. - @mtkbuild de = DiscreteSystem([x ~ x(k-1) + x(k-2)*x(k-3)], t) - prob = DiscreteProblem(de, [x(k-3) => 12.], (0, 10)) + @variables x(t) = 2.0 + @mtkbuild de = DiscreteSystem([x ~ x(k - 1) + x(k - 2) * x(k - 3)], t) + prob = DiscreteProblem(de, [x(k - 3) => 12.0], (0, 10)) @test prob[x] == 26.0 - @test prob[x(k-1)] == 2.0 - @test prob[x(k-2)] == 2.0 + @test prob[x(k - 1)] == 2.0 + @test prob[x(k - 2)] == 2.0 # Elaborate test @variables xₜ₋₂(t) zₜ₋₁(t) z(t) - eqs = [x ~ x(k-1) + z(k-2), - z ~ x(k-2) * x(k-3) - z(k-1)^2] + eqs = [x ~ x(k - 1) + z(k - 2), + z ~ x(k - 2) * x(k - 3) - z(k - 1)^2] @mtkbuild de = DiscreteSystem(eqs, t) - u0 = [x(k-1) => 3, - xₜ₋₂(k-1) => 4, - x(k-2) => 1, - z(k-1) => 5, - zₜ₋₁(k-1) => 12] + u0 = [x(k - 1) => 3, + xₜ₋₂(k - 1) => 4, + x(k - 2) => 1, + z(k - 1) => 5, + zₜ₋₁(k - 1) => 12] prob = DiscreteProblem(de, u0, (0, 10)) @test prob[x] == 15 @test prob[z] == -21 From b40c2a1dbda36d481067ffa5450599540cefdc40 Mon Sep 17 00:00:00 2001 From: vyudu Date: Mon, 24 Feb 2025 11:58:05 -0500 Subject: [PATCH 0998/2176] fix Complex typecheck --- src/utils.jl | 16 ++++++++++++---- test/odesystem.jl | 4 ++++ 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/src/utils.jl b/src/utils.jl index 962801622a..4685e76ebc 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -222,7 +222,8 @@ function collect_ivs_from_nested_operator!(ivs, x, target_op) end function iv_from_nested_derivative(x, op = Differential) - if iscall(x) && operation(x) == getindex + if iscall(x) && + (operation(x) == getindex || operation(x) == real || operation(x) == imag) iv_from_nested_derivative(arguments(x)[1], op) elseif iscall(x) operation(x) isa op ? iv_from_nested_derivative(arguments(x)[1], op) : @@ -1204,7 +1205,7 @@ end Find all the unknowns and parameters from the equations of a SDESystem or ODESystem. Return re-ordered equations, differential variables, all variables, and parameters. """ function process_equations(eqs, iv) - eqs = collect(eqs) + eqs = collect(Iterators.flatten(eqs)) diffvars = OrderedSet() allunknowns = OrderedSet() @@ -1237,8 +1238,8 @@ function process_equations(eqs, iv) throw(ArgumentError("An ODESystem can only have one independent variable.")) diffvar in diffvars && throw(ArgumentError("The differential variable $diffvar is not unique in the system of equations.")) - !(symtype(diffvar) === Real || eltype(symtype(diffvar)) === Real) && - throw(ArgumentError("Differential variable $diffvar has type $(symtype(diffvar)). Differential variables should not be concretely typed.")) + !has_diffvar_type(diffvar) && + throw(ArgumentError("Differential variable $diffvar has type $(symtype(diffvar)). Differential variables should be of a continuous, non-concrete number type: Real, Complex, AbstractFloat, or Number.")) push!(diffvars, diffvar) end push!(diffeq, eq) @@ -1250,6 +1251,13 @@ function process_equations(eqs, iv) diffvars, allunknowns, ps, Equation[diffeq; algeeq; compressed_eqs] end +function has_diffvar_type(diffvar) + st = symtype(diffvar) + st === Real || eltype(st) === Real || st === Complex || eltype(st) === Complex || + st === Number || eltype(st) === Number || st === AbstractFloat || + eltype(st) === AbstractFloat +end + """ $(TYPEDSIGNATURES) diff --git a/test/odesystem.jl b/test/odesystem.jl index 01df23ede8..b9a41e1c3d 100644 --- a/test/odesystem.jl +++ b/test/odesystem.jl @@ -1589,6 +1589,10 @@ end @variables Y(t)[1:3]::String eq = D(Y) ~ [p, p, p] @test_throws ArgumentError @mtkbuild osys = ODESystem([eq], t) + + @variables X(t)::Complex + eq = D(X) ~ p - d * X + @test_nowarn @named osys = ODESystem([eq], t) end # Test `isequal` From 956079768e8a8c1785440a8b370b3acb5b1fb756 Mon Sep 17 00:00:00 2001 From: vyudu Date: Mon, 24 Feb 2025 12:27:10 -0500 Subject: [PATCH 0999/2176] up --- src/utils.jl | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/utils.jl b/src/utils.jl index 4685e76ebc..950af5a1e2 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -1205,7 +1205,8 @@ end Find all the unknowns and parameters from the equations of a SDESystem or ODESystem. Return re-ordered equations, differential variables, all variables, and parameters. """ function process_equations(eqs, iv) - eqs = collect(Iterators.flatten(eqs)) + eltype(eqs) <: Vector && (eqs = vcat(eqs...)) + eqs = collect(eqs) diffvars = OrderedSet() allunknowns = OrderedSet() From 6fb59de1cbec6ba8a2662202b3f5a8135efb3b9c Mon Sep 17 00:00:00 2001 From: vyudu Date: Tue, 25 Feb 2025 19:25:08 -0500 Subject: [PATCH 1000/2176] feat: implement --- src/structural_transformation/utils.jl | 35 +++++++++++++++++++ .../implicit_discrete_system.jl | 2 +- src/systems/systemstructure.jl | 2 +- test/implicit_discrete_system.jl | 15 +++++++- test/structural_transformation/utils.jl | 20 +++++++++++ 5 files changed, 71 insertions(+), 3 deletions(-) diff --git a/src/structural_transformation/utils.jl b/src/structural_transformation/utils.jl index db79c8a124..fb78d707d9 100644 --- a/src/structural_transformation/utils.jl +++ b/src/structural_transformation/utils.jl @@ -507,3 +507,38 @@ function simplify_shifts(var) unwrap(var).metadata) end end + +function distribute_shift(var) + var = unwrap(var) + var isa Equation && return distribute_shift(var.lhs) ~ distribute_shift(var.rhs) + + ModelingToolkit.hasshift(var) || return var + shift = operation(var) + shift isa Shift || return var + + shift = operation(var) + expr = only(arguments(var)) + if expr isa Equation + return distribute_shift(shift(expr.lhs)) ~ distribute_shift(shift(expr.rhs)) + end + shiftexpr = _distribute_shift(expr, shift) + return simplify_shifts(shiftexpr) +end + +function _distribute_shift(expr, shift) + if iscall(expr) + op = operation(expr) + args = arguments(expr) + + if ModelingToolkit.isvariable(expr) + (length(args) == 1 && isequal(shift.t, only(args))) ? (return shift(expr)) : (return expr) + elseif op isa Shift + return shift(expr) + else + return maketerm(typeof(expr), operation(expr), Base.Fix2(_distribute_shift, shift).(args), + unwrap(expr).metadata) + end + else + return expr + end +end diff --git a/src/systems/discrete_system/implicit_discrete_system.jl b/src/systems/discrete_system/implicit_discrete_system.jl index e0450a1ada..5ff9631b3e 100644 --- a/src/systems/discrete_system/implicit_discrete_system.jl +++ b/src/systems/discrete_system/implicit_discrete_system.jl @@ -265,7 +265,7 @@ function generate_function( sys::ImplicitDiscreteSystem, dvs = unknowns(sys), ps = parameters(sys); wrap_code = identity, kwargs...) iv = get_iv(sys) exprs = map(equations(sys)) do eq - _iszero(eq.lhs) ? eq.rhs : (simplify_shifts(Shift(iv, -1)(eq.rhs)) - simplify_shifts(Shift(iv, -1)(eq.lhs))) + _iszero(eq.lhs) ? eq.rhs : (distribute_shift(Shift(iv, -1)(eq.rhs)) - distribute_shift(Shift(iv, -1)(eq.lhs))) end u_next = dvs diff --git a/src/systems/systemstructure.jl b/src/systems/systemstructure.jl index a6e6b1d218..9bd3d6c8ab 100644 --- a/src/systems/systemstructure.jl +++ b/src/systems/systemstructure.jl @@ -440,7 +440,7 @@ function TearingState(sys; quick_cancel = false, check = true) SystemStructure(complete(var_to_diff), complete(eq_to_diff), complete(graph), nothing, var_types, sys isa AbstractDiscreteSystem), Any[]) - if sys isa DiscreteSystem + if sys isa AbstractDiscreteSystem ts = shift_discrete_system(ts) end return ts diff --git a/test/implicit_discrete_system.jl b/test/implicit_discrete_system.jl index 88e79acc77..ffe4ce500f 100644 --- a/test/implicit_discrete_system.jl +++ b/test/implicit_discrete_system.jl @@ -18,7 +18,6 @@ resid = rand(2) f(resid, u_next, [2.,3.], [], t) @test resid ≈ [3., -3.] -# Initialization cases. prob = ImplicitDiscreteProblem(sys, [x(k-1) => 3.], tspan) @test prob.u0 == [3., 1.] prob = ImplicitDiscreteProblem(sys, [], tspan) @@ -28,3 +27,17 @@ prob = ImplicitDiscreteProblem(sys, [], tspan) @test_throws ErrorException prob = ImplicitDiscreteProblem(sys, [], tspan) # Test solvers +@testset "System with algebraic equations" begin + @variables x(t) y(t) + eqs = [x(k) ~ x(k-1) + x(k-2), + x^2 ~ 1 - y^2] + @mtkbuild sys = ImplicitDiscreteSystem(eqs, t) +end + +@testset "System with algebraic equations, implicit difference equations, explicit difference equations" begin + @variables x(t) y(t) + eqs = [x(k) ~ x(k-1) + x(k-2), + y(k) ~ x(k) + x(k-2)*y(k-1)] + @mtkbuild sys = ImplicitDiscreteSystem(eqs, t) +end + diff --git a/test/structural_transformation/utils.jl b/test/structural_transformation/utils.jl index 4da3d1e924..b7cc4b720b 100644 --- a/test/structural_transformation/utils.jl +++ b/test/structural_transformation/utils.jl @@ -162,3 +162,23 @@ end structural_simplify(sys; additional_passes = [pass]) @test value[] == 1 end + +@testset "Distribute shifts" begin + @variables x(t) y(t) z(t) + @parameters a b c + + # Expand shifts + @test isequal(ST.distribute_shift(Shift(t, -1)(x + y)), Shift(t, -1)(x) + Shift(t, -1)(y)) + + expr = a * Shift(t, -2)(x) + Shift(t, 2)(y) + b + @test isequal(ST.simplify_shifts(ST.distribute_shift(Shift(t, 2)(expr))), + a*x + Shift(t, 4)(y) + b) + @test isequal(ST.distribute_shift(Shift(t, 2)(exp(z))), exp(Shift(t, 2)(z))) + @test isequal(ST.distribute_shift(Shift(t, 2)(exp(a) + b)), exp(a) + b) + + expr = a^x - log(b*y) + z*x + @test isequal(ST.distribute_shift(Shift(t, -3)(expr)), a^(Shift(t, -3)(x)) - log(b * Shift(t, -3)(y)) + Shift(t, -3)(z)*Shift(t, -3)(x)) + + expr = x(k+1) ~ x + x(k-1) + @test isequal(ST.distribute_shift(Shift(t, -1)(expr)), x ~ x(k-1) + x(k-2)) +end From 00a8222960a6f3bcb147c0ae7e43e37c44bda3f7 Mon Sep 17 00:00:00 2001 From: vyudu Date: Tue, 25 Feb 2025 22:55:34 -0500 Subject: [PATCH 1001/2176] add shift2term for positive shifts --- src/discretedomain.jl | 13 ++++ .../StructuralTransformations.jl | 2 +- src/structural_transformation/utils.jl | 20 ++++-- test/implicit_discrete_system.jl | 62 ++++++++++++------- 4 files changed, 71 insertions(+), 26 deletions(-) diff --git a/src/discretedomain.jl b/src/discretedomain.jl index c7f90007c5..508cfcd216 100644 --- a/src/discretedomain.jl +++ b/src/discretedomain.jl @@ -70,6 +70,19 @@ Base.literal_pow(f::typeof(^), D::Shift, ::Val{n}) where {n} = Shift(D.t, D.step hasshift(eq::Equation) = hasshift(eq.lhs) || hasshift(eq.rhs) +""" + Next(x) + +An alias for Shift(t, 1)(x). +""" +Next(x) = Shift(t, 1)(x) +""" + Prev(x) + +An alias for Shift(t, -1)(x). +""" +Prev(x) = Shift(t, -1)(x) + """ hasshift(O) diff --git a/src/structural_transformation/StructuralTransformations.jl b/src/structural_transformation/StructuralTransformations.jl index e6be30891f..2e600e86e4 100644 --- a/src/structural_transformation/StructuralTransformations.jl +++ b/src/structural_transformation/StructuralTransformations.jl @@ -65,7 +65,7 @@ export torn_system_jacobian_sparsity export full_equations export but_ordered_incidence, lowest_order_variable_mask, highest_order_variable_mask export computed_highest_diff_variables -export shift2term, lower_shift_varname, simplify_shifts +export shift2term, lower_shift_varname, simplify_shifts, distribute_shift include("utils.jl") include("pantelides.jl") diff --git a/src/structural_transformation/utils.jl b/src/structural_transformation/utils.jl index 31c58e797d..031ef6b2d8 100644 --- a/src/structural_transformation/utils.jl +++ b/src/structural_transformation/utils.jl @@ -457,7 +457,7 @@ Handle renaming variable names for discrete structural simplification. Three cas """ function lower_shift_varname(var, iv) op = operation(var) - if op isa Shift && op.steps < 0 + if op isa Shift return shift2term(var) else return Shift(iv, 0)(var, true) @@ -475,10 +475,14 @@ function shift2term(var) backshift = is_lowered ? op.steps + ModelingToolkit.getshift(arg) : op.steps - num = join(Char(0x2080 + d) for d in reverse!(digits(-backshift))) # subscripted number, e.g. ₁ - ds = join([Char(0x209c), Char(0x208b), num]) - # Char(0x209c) = ₜ # Char(0x208b) = ₋ (subscripted minus) + # Char(0x208a) = ₊ (subscripted plus) + pm = backshift > 0 ? Char(0x208a) : Char(0x208b) + # subscripted number, e.g. ₁ + num = join(Char(0x2080 + d) for d in reverse!(digits(abs(backshift)))) + # Char(0x209c) = ₜ + # ds = ₜ₋₁ + ds = join([Char(0x209c), pm, num]) O = is_lowered ? ModelingToolkit.getunshifted(arg) : arg oldop = operation(O) @@ -498,6 +502,9 @@ function isdoubleshift(var) ModelingToolkit.isoperator(arguments(var)[1], ModelingToolkit.Shift) end +""" +Simplify multiple shifts: Shift(t, k1)(Shift(t, k2)(x)) becomes Shift(t, k1+k2)(x). +""" function simplify_shifts(var) ModelingToolkit.hasshift(var) || return var var isa Equation && return simplify_shifts(var.lhs) ~ simplify_shifts(var.rhs) @@ -518,6 +525,11 @@ function simplify_shifts(var) end end +""" +Distribute a shift applied to a whole expression or equation. +Shift(t, 1)(x + y) will become Shift(t, 1)(x) + Shift(t, 1)(y). +Only shifts variables whose independent variable is the same t that appears in the Shift (i.e. constants, time-independent parameters, etc. do not get shifted). +""" function distribute_shift(var) var = unwrap(var) var isa Equation && return distribute_shift(var.lhs) ~ distribute_shift(var.rhs) diff --git a/test/implicit_discrete_system.jl b/test/implicit_discrete_system.jl index ffe4ce500f..f2e7deaa32 100644 --- a/test/implicit_discrete_system.jl +++ b/test/implicit_discrete_system.jl @@ -1,30 +1,34 @@ using ModelingToolkit, Test using ModelingToolkit: t_nounits as t +using StableRNGs k = ShiftIndex(t) -@variables x(t) = 1 -@mtkbuild sys = ImplicitDiscreteSystem([x(k) ~ x(k)*x(k-1) - 3], t) -tspan = (0, 10) +rng = StableRNG(22525) # Shift(t, -1)(x(t)) - x_{t-1}(t) # -3 - x(t) + x(t)*x_{t-1} -f = ImplicitDiscreteFunction(sys) -u_next = [3., 1.5] -@test f(u_next, [2.,3.], [], t) ≈ [0., 0.] -u_next = [0., 0.] -@test f(u_next, [2.,3.], [], t) ≈ [3., -3.] - -resid = rand(2) -f(resid, u_next, [2.,3.], [], t) -@test resid ≈ [3., -3.] - -prob = ImplicitDiscreteProblem(sys, [x(k-1) => 3.], tspan) -@test prob.u0 == [3., 1.] -prob = ImplicitDiscreteProblem(sys, [], tspan) -@test prob.u0 == [1., 1.] -@variables x(t) -@mtkbuild sys = ImplicitDiscreteSystem([x(k) ~ x(k)*x(k-1) - 3], t) -@test_throws ErrorException prob = ImplicitDiscreteProblem(sys, [], tspan) +@testset "Correct ImplicitDiscreteFunction" begin + @variables x(t) = 1 + @mtkbuild sys = ImplicitDiscreteSystem([x(k) ~ x(k)*x(k-1) - 3], t) + tspan = (0, 10) + f = ImplicitDiscreteFunction(sys) + u_next = [3., 1.5] + @test f(u_next, [2.,3.], [], t) ≈ [0., 0.] + u_next = [0., 0.] + @test f(u_next, [2.,3.], [], t) ≈ [3., -3.] + + resid = rand(2) + f(resid, u_next, [2.,3.], [], t) + @test resid ≈ [3., -3.] + + prob = ImplicitDiscreteProblem(sys, [x(k-1) => 3.], tspan) + @test prob.u0 == [3., 1.] + prob = ImplicitDiscreteProblem(sys, [], tspan) + @test prob.u0 == [1., 1.] + @variables x(t) + @mtkbuild sys = ImplicitDiscreteSystem([x(k) ~ x(k)*x(k-1) - 3], t) + @test_throws ErrorException prob = ImplicitDiscreteProblem(sys, [], tspan) +end # Test solvers @testset "System with algebraic equations" begin @@ -32,6 +36,23 @@ prob = ImplicitDiscreteProblem(sys, [], tspan) eqs = [x(k) ~ x(k-1) + x(k-2), x^2 ~ 1 - y^2] @mtkbuild sys = ImplicitDiscreteSystem(eqs, t) + f = ImplicitDiscreteFunction(sys) + + function correct_f(u_next, u, p, t) + [u[2] - u_next[1], + u[1] + u[2] - u_next[2], + 1 - (u_next[1]+u_next[2])^2 - u_next[3]^2] + end + + for _ in 1:10 + u_next = rand(rng, 3) + u = rand(rng, 3) + @test correct_f(u_next, u, [], 0.) ≈ f(u_next, u, [], 0.) + end + + # Initialization is satisfied. + prob = ImplicitDiscreteProblem(sys, [x(k-1) => 3.], tspan) + @test (prob.u0[1] + prob.u0[2])^2 + prob.u0[3]^2 ≈ 1 end @testset "System with algebraic equations, implicit difference equations, explicit difference equations" begin @@ -40,4 +61,3 @@ end y(k) ~ x(k) + x(k-2)*y(k-1)] @mtkbuild sys = ImplicitDiscreteSystem(eqs, t) end - From ddf0d12c06e9afc7777d83777f6537bdb214757c Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 26 Feb 2025 22:08:02 +0530 Subject: [PATCH 1002/2176] feat: mark `getproperty(::AbstractSystem, ::Symbol)` as non-differentiable --- ext/MTKChainRulesCoreExt.jl | 2 ++ test/extensions/ad.jl | 10 ++++++++++ 2 files changed, 12 insertions(+) diff --git a/ext/MTKChainRulesCoreExt.jl b/ext/MTKChainRulesCoreExt.jl index f153ee77de..9cf17d203d 100644 --- a/ext/MTKChainRulesCoreExt.jl +++ b/ext/MTKChainRulesCoreExt.jl @@ -103,4 +103,6 @@ function ChainRulesCore.rrule( newbuf, pullback end +ChainRulesCore.@non_differentiable Base.getproperty(sys::MTK.AbstractSystem, x::Symbol) + end diff --git a/test/extensions/ad.jl b/test/extensions/ad.jl index 0e72b2b7b7..adaf6117c6 100644 --- a/test/extensions/ad.jl +++ b/test/extensions/ad.jl @@ -124,3 +124,13 @@ fwd, back = ChainRulesCore.rrule(remake_buffer, sys, ps, idxs, vals) nsol = solve(nprob, NewtonRaphson()) @test nsol[1] ≈ 10.0 / 1.0 + 9.81 * 1.0 / 2 # anal free fall solution is y = v0*t - g*t^2/2 -> v0 = y/t + g*t/2 end + +@testset "`sys.var` is non-differentiable" begin + @variables x(t) + @mtkbuild sys = ODESystem(D(x) ~ x, t) + prob = ODEProblem(sys, [x => 1.0], (0.0, 1.0)) + + grad = Zygote.gradient(prob) do prob + prob[sys.x] + end +end From f289c5a5934766376e4b05bab4a0b777499a931c Mon Sep 17 00:00:00 2001 From: David Widmann Date: Wed, 26 Feb 2025 19:31:41 +0100 Subject: [PATCH 1003/2176] Replace `vcat(eqs...)` with `reduce(vcat, eqs)` --- src/utils.jl | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/utils.jl b/src/utils.jl index 22ef4dc160..78c665ffca 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -1217,7 +1217,9 @@ end Find all the unknowns and parameters from the equations of a SDESystem or ODESystem. Return re-ordered equations, differential variables, all variables, and parameters. """ function process_equations(eqs, iv) - eltype(eqs) <: Vector && (eqs = vcat(eqs...)) + if eltype(eqs) <: AbstractVector + eqs = reduce(vcat, eqs) + end eqs = collect(eqs) diffvars = OrderedSet() From 3d9a8d847560717a8eab615f4ad16bb42451947c Mon Sep 17 00:00:00 2001 From: Christopher Rackauckas Date: Wed, 26 Feb 2025 10:57:59 -0800 Subject: [PATCH 1004/2176] Update Project.toml --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 14ea5743fa..8c4a58045e 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "ModelingToolkit" uuid = "961ee093-0014-501f-94e3-6117800e7a78" authors = ["Yingbo Ma ", "Chris Rackauckas and contributors"] -version = "9.64.1" +version = "9.64.2" [deps] AbstractTrees = "1520ce14-60c1-5f80-bbc7-55ef81b5835c" From c504aefa92bf974e24d3f1d4625a65ae25070157 Mon Sep 17 00:00:00 2001 From: vyudu Date: Wed, 26 Feb 2025 16:06:10 -0500 Subject: [PATCH 1005/2176] fix: make the codegen work with shifted observables --- .../symbolics_tearing.jl | 4 +++ .../implicit_discrete_system.jl | 32 ++++++++++++++----- src/systems/systemstructure.jl | 2 +- test/implicit_discrete_system.jl | 25 +++++++++++---- 4 files changed, 47 insertions(+), 16 deletions(-) diff --git a/src/structural_transformation/symbolics_tearing.jl b/src/structural_transformation/symbolics_tearing.jl index 9818bba361..f1cdd7ce9c 100644 --- a/src/structural_transformation/symbolics_tearing.jl +++ b/src/structural_transformation/symbolics_tearing.jl @@ -558,6 +558,10 @@ function generate_system_equations!(state::TearingState, neweqs, var_eq_matching end total_sub[simplify_shifts(neweq.lhs)] = neweq.rhs + # Substitute unshifted variables x(k), y(k) on RHS of implicit equations + if is_only_discrete(structure) + var_to_diff[iv] === nothing && (total_sub[var] = neweq.rhs) + end push!(diff_eqs, neweq) push!(diffeq_idxs, ieq) push!(diff_vars, diff_to_var[iv]) diff --git a/src/systems/discrete_system/implicit_discrete_system.jl b/src/systems/discrete_system/implicit_discrete_system.jl index 5ff9631b3e..5f13e87be4 100644 --- a/src/systems/discrete_system/implicit_discrete_system.jl +++ b/src/systems/discrete_system/implicit_discrete_system.jl @@ -264,13 +264,20 @@ end function generate_function( sys::ImplicitDiscreteSystem, dvs = unknowns(sys), ps = parameters(sys); wrap_code = identity, kwargs...) iv = get_iv(sys) + # Algebraic equations get shifted forward 1, to match with differential equations exprs = map(equations(sys)) do eq - _iszero(eq.lhs) ? eq.rhs : (distribute_shift(Shift(iv, -1)(eq.rhs)) - distribute_shift(Shift(iv, -1)(eq.lhs))) + _iszero(eq.lhs) ? distribute_shift(Shift(iv, 1)(eq.rhs)) : (eq.rhs - eq.lhs) end - u_next = dvs - u = map(Shift(iv, -1), u_next) - build_function_wrapper(sys, exprs, u_next, u, ps..., iv; p_start = 3, kwargs...) + # Handle observables in algebraic equations, since they are shifted + obs = observed(sys) + shifted_obs = [distribute_shift(Shift(iv, 1)(eq)) for eq in obs] + obsidxs = observed_equations_used_by(sys, exprs; obs = shifted_obs) + extra_assignments = [Assignment(shifted_obs[i].lhs, shifted_obs[i].rhs) for i in obsidxs] + + u_next = map(Shift(iv, 1), dvs) + u = dvs + build_function_wrapper(sys, exprs, u_next, u, ps..., iv; p_start = 3, extra_assignments, kwargs...) end function shift_u0map_forward(sys::ImplicitDiscreteSystem, u0map, defs) @@ -279,15 +286,22 @@ function shift_u0map_forward(sys::ImplicitDiscreteSystem, u0map, defs) for k in collect(keys(u0map)) v = u0map[k] if !((op = operation(k)) isa Shift) - error("Initial conditions must be for the past state of the unknowns. Instead of providing the condition for $k, provide the condition for $(Shift(iv, -1)(k)).") + isnothing(getunshifted(k)) && + error("Initial conditions must be for the past state of the unknowns. Instead of providing the condition for $k, provide the condition for $(Shift(iv, -1)(k)).") + + updated[k] = v + elseif op.steps > 0 + error("Initial conditions must be for the past state of the unknowns. Instead of providing the condition for $k, provide the condition for $(Shift(iv, -1)(k))).") + else + updated[k] = v end - updated[shift2term(k)] = v end for var in unknowns(sys) op = operation(var) - haskey(updated, var) && continue root = getunshifted(var) + shift = getshift(var) isnothing(root) && continue + (haskey(updated, Shift(iv, shift)(root)) || haskey(updated, var)) && continue haskey(defs, root) || error("Initial condition for $var not provided.") updated[var] = defs[root] end @@ -317,7 +331,9 @@ function SciMLBase.ImplicitDiscreteProblem( u0map = to_varmap(u0map, dvs) u0map = shift_u0map_forward(sys, u0map, defaults(sys)) f, u0, p = process_SciMLProblem( - ImplicitDiscreteFunction, sys, u0map, parammap; eval_expression, eval_module) + ImplicitDiscreteFunction, sys, u0map, parammap; eval_expression, eval_module, kwargs...) + + kwargs = filter_kwargs(kwargs) ImplicitDiscreteProblem(f, u0, tspan, p; kwargs...) end diff --git a/src/systems/systemstructure.jl b/src/systems/systemstructure.jl index e7d5fa71f1..e5909ed892 100644 --- a/src/systems/systemstructure.jl +++ b/src/systems/systemstructure.jl @@ -440,7 +440,7 @@ function TearingState(sys; quick_cancel = false, check = true) SystemStructure(complete(var_to_diff), complete(eq_to_diff), complete(graph), nothing, var_types, sys isa AbstractDiscreteSystem), Any[]) - if sys isa AbstractDiscreteSystem + if sys isa DiscreteSystem ts = shift_discrete_system(ts) end return ts diff --git a/test/implicit_discrete_system.jl b/test/implicit_discrete_system.jl index f2e7deaa32..f298f43e7c 100644 --- a/test/implicit_discrete_system.jl +++ b/test/implicit_discrete_system.jl @@ -5,12 +5,13 @@ using StableRNGs k = ShiftIndex(t) rng = StableRNG(22525) -# Shift(t, -1)(x(t)) - x_{t-1}(t) -# -3 - x(t) + x(t)*x_{t-1} @testset "Correct ImplicitDiscreteFunction" begin @variables x(t) = 1 @mtkbuild sys = ImplicitDiscreteSystem([x(k) ~ x(k)*x(k-1) - 3], t) tspan = (0, 10) + + # u[2] - u_next[1] + # -3 - u_next[2] + u_next[2]*u_next[1] f = ImplicitDiscreteFunction(sys) u_next = [3., 1.5] @test f(u_next, [2.,3.], [], t) ≈ [0., 0.] @@ -30,7 +31,6 @@ rng = StableRNG(22525) @test_throws ErrorException prob = ImplicitDiscreteProblem(sys, [], tspan) end -# Test solvers @testset "System with algebraic equations" begin @variables x(t) y(t) eqs = [x(k) ~ x(k-1) + x(k-2), @@ -51,13 +51,24 @@ end end # Initialization is satisfied. - prob = ImplicitDiscreteProblem(sys, [x(k-1) => 3.], tspan) + prob = ImplicitDiscreteProblem(sys, [x(k-1) => .3, x(k-2) => .4], (0, 10), guesses = [y => 1]) @test (prob.u0[1] + prob.u0[2])^2 + prob.u0[3]^2 ≈ 1 end -@testset "System with algebraic equations, implicit difference equations, explicit difference equations" begin - @variables x(t) y(t) +@testset "Handle observables in function codegen" begin + # Observable appears in differential equation + @variables x(t) y(t) z(t) eqs = [x(k) ~ x(k-1) + x(k-2), - y(k) ~ x(k) + x(k-2)*y(k-1)] + y(k) ~ x(k) + x(k-2)*z(k-1), + x + y + z ~ 2] + @mtkbuild sys = ImplicitDiscreteSystem(eqs, t) + @test length(unknowns(sys)) == length(equations(sys)) == 3 + @test occursin("var\"y(t)\"", string(ImplicitDiscreteFunctionExpr(sys))) + + # Shifted observable that appears in algebraic equation is properly handled. + eqs = [z(k) ~ x(k) + sin(x(k)), + y(k) ~ x(k-1) + x(k-2), + z(k) * x(k) ~ 3] @mtkbuild sys = ImplicitDiscreteSystem(eqs, t) + @test occursin("var\"Shift(t, 1)(z(t))\"", string(ImplicitDiscreteFunctionExpr(sys))) end From 48ec55c15ed46d03d6a5e1e8d0627fd026dde311 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 25 Feb 2025 13:22:23 +0530 Subject: [PATCH 1006/2176] feat: add `map_variables_to_equations` --- src/ModelingToolkit.jl | 1 + src/systems/systems.jl | 54 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+) diff --git a/src/ModelingToolkit.jl b/src/ModelingToolkit.jl index b683e132a2..48070f244a 100644 --- a/src/ModelingToolkit.jl +++ b/src/ModelingToolkit.jl @@ -276,6 +276,7 @@ export TearingState export BipartiteGraph, equation_dependencies, variable_dependencies export eqeq_dependencies, varvar_dependencies export asgraph, asdigraph +export map_variables_to_equations export toexpr, get_variables export simplify, substitute diff --git a/src/systems/systems.jl b/src/systems/systems.jl index ef0f966eee..6151ffa515 100644 --- a/src/systems/systems.jl +++ b/src/systems/systems.jl @@ -158,3 +158,57 @@ function __structural_simplify(sys::AbstractSystem, io = nothing; simplify = fal guesses = guesses(sys), initialization_eqs = initialization_equations(sys)) end end + +""" + $(TYPEDSIGNATURES) + +Given a system that has been simplified via `structural_simplify`, return a `Dict` mapping +variables of the system to equations that are used to solve for them. This includes +observed variables. + +# Keyword Arguments + +- `rename_dummy_derivatives`: Whether to rename dummy derivative variable keys into their + `Differential` forms. For example, this would turn the key `yˍt(t)` into + `Differential(t)(y(t))`. +""" +function map_variables_to_equations(sys::AbstractSystem; rename_dummy_derivatives = true) + if !has_tearing_state(sys) + throw(ArgumentError("$(typeof(sys)) is not supported.")) + end + ts = get_tearing_state(sys) + if ts === nothing + throw(ArgumentError("`map_variables_to_equations` requires a simplified system. Call `structural_simplify` on the system before calling this function.")) + end + + dummy_sub = Dict() + if rename_dummy_derivatives && has_schedule(sys) && (sc = get_schedule(sys)) !== nothing + dummy_sub = Dict(v => k for (k, v) in sc.dummy_sub if isequal(default_toterm(k), v)) + end + + mapping = Dict{Union{Num, BasicSymbolic}, Equation}() + eqs = equations(sys) + for eq in eqs + isdifferential(eq.lhs) || continue + var = arguments(eq.lhs)[1] + var = get(dummy_sub, var, var) + mapping[var] = eq + end + + graph = ts.structure.graph + algvars = BitSet(findall( + Base.Fix1(StructuralTransformations.isalgvar, ts.structure), 1:ndsts(graph))) + algeqs = BitSet(findall(1:nsrcs(graph)) do eq + all(!Base.Fix1(isdervar, ts.structure), 𝑠neighbors(graph, eq)) + end) + alge_var_eq_matching = complete(maximal_matching(graph, in(algeqs), in(algvars))) + for (i, eq) in enumerate(alge_var_eq_matching) + eq isa Unassigned && continue + mapping[get(dummy_sub, ts.fullvars[i], ts.fullvars[i])] = eqs[eq] + end + for eq in observed(sys) + mapping[get(dummy_sub, eq.lhs, eq.lhs)] = eq + end + + return mapping +end From 74de88b8acfb0efb9045801604969e682bc3504d Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 25 Feb 2025 13:22:30 +0530 Subject: [PATCH 1007/2176] test: test `map_variables_to_equations` --- test/structural_transformation/utils.jl | 98 ++++++++++++++++++++++++- 1 file changed, 96 insertions(+), 2 deletions(-) diff --git a/test/structural_transformation/utils.jl b/test/structural_transformation/utils.jl index 4da3d1e924..24cfb98d45 100644 --- a/test/structural_transformation/utils.jl +++ b/test/structural_transformation/utils.jl @@ -3,8 +3,8 @@ using ModelingToolkit using Graphs using SparseArrays using UnPack -using ModelingToolkit: t_nounits as t, D_nounits as D -const ST = StructuralTransformations +using ModelingToolkit: t_nounits as t, D_nounits as D, default_toterm +using Symbolics: unwrap # Define some variables @parameters L g @@ -162,3 +162,97 @@ end structural_simplify(sys; additional_passes = [pass]) @test value[] == 1 end + +@testset "`map_variables_to_equations`" begin + @testset "Not supported for systems without `.tearing_state`" begin + @variables x + @mtkbuild sys = OptimizationSystem(x^2) + @test_throws ArgumentError map_variables_to_equations(sys) + end + @testset "Requires simplified system" begin + @variables x(t) y(t) + @named sys = ODESystem([D(x) ~ x, y ~ 2x], t) + sys = complete(sys) + @test_throws ArgumentError map_variables_to_equations(sys) + end + @testset "`ODESystem`" begin + @variables x(t) y(t) z(t) + @mtkbuild sys = ODESystem([D(x) ~ 2x + y, y ~ x + z, z^3 + x^3 ~ 12], t) + mapping = map_variables_to_equations(sys) + @test mapping[x] == (D(x) ~ 2x + y) + @test mapping[y] == (y ~ x + z) + @test mapping[z] == (0 ~ 12 - z^3 - x^3) + @test length(mapping) == 3 + + @testset "With dummy derivatives" begin + @parameters g + @variables x(t) y(t) [state_priority = 10] λ(t) + eqs = [D(D(x)) ~ λ * x + D(D(y)) ~ λ * y - g + x^2 + y^2 ~ 1] + @mtkbuild sys = ODESystem(eqs, t) + mapping = map_variables_to_equations(sys) + + yt = default_toterm(unwrap(D(y))) + xt = default_toterm(unwrap(D(x))) + xtt = default_toterm(unwrap(D(D(x)))) + @test mapping[x] == (0 ~ 1 - x^2 - y^2) + @test mapping[y] == (D(y) ~ yt) + @test mapping[D(y)] == (D(yt) ~ -g + y * λ) + @test mapping[D(x)] == (0 ~ -2xt * x - 2yt * y) + @test mapping[D(D(x))] == (xtt ~ x * λ) + @test length(mapping) == 5 + + @testset "`rename_dummy_derivatives = false`" begin + mapping = map_variables_to_equations(sys; rename_dummy_derivatives = false) + + @test mapping[x] == (0 ~ 1 - x^2 - y^2) + @test mapping[y] == (D(y) ~ yt) + @test mapping[yt] == (D(yt) ~ -g + y * λ) + @test mapping[xt] == (0 ~ -2xt * x - 2yt * y) + @test mapping[xtt] == (xtt ~ x * λ) + @test length(mapping) == 5 + end + end + @testset "DDEs" begin + function oscillator(; name, k = 1.0, τ = 0.01) + @parameters k=k τ=τ + @variables x(..)=0.1 y(t)=0.1 jcn(t)=0.0 delx(t) + eqs = [D(x(t)) ~ y, + D(y) ~ -k * x(t - τ) + jcn, + delx ~ x(t - τ)] + return System(eqs, t; name = name) + end + + systems = @named begin + osc1 = oscillator(k = 1.0, τ = 0.01) + osc2 = oscillator(k = 2.0, τ = 0.04) + end + eqs = [osc1.jcn ~ osc2.delx, + osc2.jcn ~ osc1.delx] + @named coupledOsc = System(eqs, t) + @mtkbuild sys = compose(coupledOsc, systems) + mapping = map_variables_to_equations(sys) + x1 = operation(unwrap(osc1.x)) + x2 = operation(unwrap(osc2.x)) + @test mapping[osc1.x] == (D(osc1.x) ~ osc1.y) + @test mapping[osc1.y] == (D(osc1.y) ~ osc1.jcn - osc1.k * x1(t - osc1.τ)) + @test mapping[osc1.delx] == (osc1.delx ~ x1(t - osc1.τ)) + @test mapping[osc1.jcn] == (osc1.jcn ~ osc2.delx) + @test mapping[osc2.x] == (D(osc2.x) ~ osc2.y) + @test mapping[osc2.y] == (D(osc2.y) ~ osc2.jcn - osc2.k * x2(t - osc2.τ)) + @test mapping[osc2.delx] == (osc2.delx ~ x2(t - osc2.τ)) + @test mapping[osc2.jcn] == (osc2.jcn ~ osc1.delx) + @test length(mapping) == 8 + end + end + @testset "`NonlinearSystem`" begin + @variables x y z + @mtkbuild sys = NonlinearSystem([x^2 ~ 2y^2 + 1, sin(z) ~ y, z^3 + 4z + 1 ~ 0]) + mapping = map_variables_to_equations(sys) + @test mapping[x] == (0 ~ 2y^2 + 1 - x^2) + @test mapping[y] == (y ~ sin(z)) + @test mapping[z] == (0 ~ -1 - 4z - z^3) + @test length(mapping) == 3 + end +end From 55601e07318531341c118de9b3f4b38416a03373 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 25 Feb 2025 13:24:28 +0530 Subject: [PATCH 1008/2176] docs: add `map_variables_to_equations` to docs --- docs/src/basics/DependencyGraphs.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/docs/src/basics/DependencyGraphs.md b/docs/src/basics/DependencyGraphs.md index 67de5ea8f1..73fbbca9be 100644 --- a/docs/src/basics/DependencyGraphs.md +++ b/docs/src/basics/DependencyGraphs.md @@ -22,3 +22,9 @@ asdigraph eqeq_dependencies varvar_dependencies ``` + +# Miscellaneous + +```@docs +map_variables_to_equations +``` From 615c7fc37ff0d76e2c6f066c5e490716a5aecaae Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 24 Feb 2025 16:24:12 +0530 Subject: [PATCH 1009/2176] fix: allow passing guesses to `linearization_function` --- src/linearization.jl | 10 +++++----- test/downstream/linearize.jl | 9 ++++++--- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/src/linearization.jl b/src/linearization.jl index f83efc1642..01d8ee2f4a 100644 --- a/src/linearization.jl +++ b/src/linearization.jl @@ -41,6 +41,7 @@ function linearization_function(sys::AbstractSystem, inputs, initialization_solver_alg = TrustRegion(), eval_expression = false, eval_module = @__MODULE__, warn_initialize_determined = true, + guesses = Dict(), kwargs...) op = Dict(op) inputs isa AbstractVector || (inputs = [inputs]) @@ -66,11 +67,10 @@ function linearization_function(sys::AbstractSystem, inputs, initializealg = initialize ? OverrideInit() : NoInit() end - fun, u0, p = process_SciMLProblem( - ODEFunction{true, SciMLBase.FullSpecialize}, sys, op, p; - t = 0.0, build_initializeprob = initializealg isa OverrideInit, - allow_incomplete = true, algebraic_only = true) - prob = ODEProblem(fun, u0, (nothing, nothing), p) + prob = ODEProblem{true, SciMLBase.FullSpecialize}( + sys, op, (nothing, nothing), p; allow_incomplete = true, + algebraic_only = true, guesses) + u0 = state_values(prob) ps = parameters(sys) h = build_explicit_observed_function(sys, outputs; eval_expression, eval_module) diff --git a/test/downstream/linearize.jl b/test/downstream/linearize.jl index 3cab641d26..898040fb4f 100644 --- a/test/downstream/linearize.jl +++ b/test/downstream/linearize.jl @@ -317,12 +317,15 @@ matrices = linfun([1.0], Dict(p => 3.0), 1.0) @test matrices.f_u[] == 3.0 end -@testset "Issue #2941" begin - @variables x(t) y(t) [guess = 1.0] +@testset "Issue #2941 and #3400" begin + @variables x(t) y(t) @parameters p eqs = [0 ~ x * log(y) - p] @named sys = ODESystem(eqs, t; defaults = [p => 1.0]) sys = complete(sys) - @test_nowarn linearize( + @test_throws ModelingToolkit.MissingVariablesError linearize( sys, [x], []; op = Dict(x => 1.0), allow_input_derivatives = true) + @test_nowarn linearize( + sys, [x], []; op = Dict(x => 1.0), guesses = Dict(y => 1.0), + allow_input_derivatives = true) end From 23231d6095628ae4199537b1183f56b4b589daaa Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 24 Feb 2025 16:26:35 +0530 Subject: [PATCH 1010/2176] feat: add `Base.show` methods for `LinearizationFunction` and `LinearizationProblem` --- src/linearization.jl | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/linearization.jl b/src/linearization.jl index 01d8ee2f4a..411620e8f3 100644 --- a/src/linearization.jl +++ b/src/linearization.jl @@ -148,6 +148,12 @@ function SymbolicIndexingInterface.parameter_values(f::LinearizationFunction) end SymbolicIndexingInterface.current_time(f::LinearizationFunction) = current_time(f.prob) +function Base.show(io::IO, mime::MIME"text/plain", lf::LinearizationFunction) + printstyled(io, "LinearizationFunction"; bold = true, color = :blue) + println(io, " which wraps:") + show(io, mime, lf.prob) +end + """ $(TYPEDSIGNATURES) @@ -265,6 +271,12 @@ mutable struct LinearizationProblem{F <: LinearizationFunction, T} t::T end +function Base.show(io::IO, mime::MIME"text/plain", prob::LinearizationProblem) + printstyled(io, "LinearizationProblem"; bold = true, color = :blue) + println(io, " at time ", prob.t, " which wraps:") + show(io, mime, prob.f.prob) +end + """ $(TYPEDSIGNATURES) From bbfb34d6678b1f504ef4a5e0102b8412ec7c8766 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 24 Feb 2025 16:26:49 +0530 Subject: [PATCH 1011/2176] docs: link to MTK's linear analysis doc page rather than the standard library --- docs/src/basics/Linearization.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/src/basics/Linearization.md b/docs/src/basics/Linearization.md index 0b29beec2f..5338deda49 100644 --- a/docs/src/basics/Linearization.md +++ b/docs/src/basics/Linearization.md @@ -43,7 +43,7 @@ using ModelingToolkit: inputs, outputs !!! note "Inputs must be unconnected" - The model above has 4 variables but only three equations, there is no equation specifying the value of `r` since `r` is an input. This means that only unbalanced models can be linearized, or in other words, models that are balanced and can be simulated _cannot_ be linearized. To learn more about this, see [How to linearize a ModelingToolkit model (YouTube)](https://www.youtube.com/watch?v=-XOux-2XDGI&t=395s). Also see [ModelingToolkitStandardLibrary: Linear analysis](https://docs.sciml.ai/ModelingToolkitStandardLibrary/stable/API/linear_analysis/) for utilities that make linearization of completed models easier. + The model above has 4 variables but only three equations, there is no equation specifying the value of `r` since `r` is an input. This means that only unbalanced models can be linearized, or in other words, models that are balanced and can be simulated _cannot_ be linearized. To learn more about this, see [How to linearize a ModelingToolkit model (YouTube)](https://www.youtube.com/watch?v=-XOux-2XDGI&t=395s). Also see [ModelingToolkitStandardLibrary: Linear analysis](https://docs.sciml.ai/ModelingToolkit/stable/tutorials/linear_analysis/) for utilities that make linearization of completed models easier. !!! note "Un-simplified system" @@ -75,7 +75,7 @@ If the modeled system is actually proper (but MTK failed to find a proper realiz ## Tools for linear analysis -[ModelingToolkitStandardLibrary](https://docs.sciml.ai/ModelingToolkitStandardLibrary/stable/) contains a set of [tools for more advanced linear analysis](https://docs.sciml.ai/ModelingToolkitStandardLibrary/stable/API/linear_analysis/). These can be used to make it easier to work with and analyze causal models, such as control and signal-processing systems. +ModelingToolkit contains a set of [tools for more advanced linear analysis](https://docs.sciml.ai/ModelingToolkit/stable/tutorials/linear_analysis/). These can be used to make it easier to work with and analyze causal models, such as control and signal-processing systems. Also see [ControlSystemsMTK.jl](https://juliacontrol.github.io/ControlSystemsMTK.jl/dev/) for an interface to [ControlSystems.jl](https://github.com/JuliaControl/ControlSystems.jl) that contains tools for linear analysis and frequency-domain analysis. From c17ce0a3a2bcbdb79f47c38098f8114a5af2d439 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 24 Feb 2025 18:03:14 +0530 Subject: [PATCH 1012/2176] feat: export `CommonSolve.solve` --- src/ModelingToolkit.jl | 1 + 1 file changed, 1 insertion(+) diff --git a/src/ModelingToolkit.jl b/src/ModelingToolkit.jl index b683e132a2..27d398e633 100644 --- a/src/ModelingToolkit.jl +++ b/src/ModelingToolkit.jl @@ -262,6 +262,7 @@ export independent_variable, equations, controls, observed, full_equations export initialization_equations, guesses, defaults, parameter_dependencies, hierarchy export structural_simplify, expand_connections, linearize, linearization_function, LinearizationProblem +export solve export calculate_jacobian, generate_jacobian, generate_function, generate_custom_function export calculate_control_jacobian, generate_control_jacobian From 027fe713f3e5f17003f562b24e6338e52d50484d Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 24 Feb 2025 18:06:54 +0530 Subject: [PATCH 1013/2176] docs: add doc example for faster linearization --- docs/src/basics/Linearization.md | 71 +++++++++++++++++++++++++++++++- 1 file changed, 70 insertions(+), 1 deletion(-) diff --git a/docs/src/basics/Linearization.md b/docs/src/basics/Linearization.md index 5338deda49..1c06ce72d4 100644 --- a/docs/src/basics/Linearization.md +++ b/docs/src/basics/Linearization.md @@ -22,7 +22,7 @@ The `linearize` function expects the user to specify the inputs ``u`` and the ou ```@example LINEARIZE using ModelingToolkit using ModelingToolkit: t_nounits as t, D_nounits as D -@variables x(t)=0 y(t)=0 u(t)=0 r(t)=0 +@variables x(t)=0 y(t) u(t) r(t)=0 @parameters kp = 1 eqs = [u ~ kp * (r - y) # P controller @@ -57,6 +57,74 @@ The operating point to linearize around can be specified with the keyword argume If linearization is to be performed around multiple operating points, the simplification of the system has to be carried out a single time only. To facilitate this, the lower-level function [`ModelingToolkit.linearization_function`](@ref) is available. This function further allows you to obtain separate Jacobians for the differential and algebraic parts of the model. For ODE models without algebraic equations, the statespace representation above is available from the output of `linearization_function` as `A, B, C, D = f_x, f_u, h_x, h_u`. +All variables that will be fixed by an operating point _must_ be provided in the operating point to `linearization_function`. For example, if the operating points fix the value of +`x`, `y` and `z` then an operating point with constant values for these variables (e.g. `Dict(x => 1.0, y => 1.0, z => 1.0)`) must be provided. The constant values themselves +do not matter and can be changed by subsequent operating points. + +One approach to batch linearization would be to call `linearize` in a loop, providing a different operating point each time. For example: + +```@example LINEARIZE +using ModelingToolkitStandardLibrary +using ModelingToolkitStandardLibrary.Blocks + +@parameters k=10 k3=2 c=1 +@variables x(t)=0 [bounds = (-0.5, 1.5)] +@variables v(t) = 0 + +@named y = Blocks.RealOutput() +@named u = Blocks.RealInput() + +eqs = [D(x) ~ v + D(v) ~ -k * x - k3 * x^3 - c * v + 10u.u + y.u ~ x] + +@named duffing = ODESystem(eqs, t, systems = [y, u], defaults = [u.u => 0]) + +# pass a constant value for `x`, since it is the variable we will change in operating points +linfun, simplified_sys = linearization_function(duffing, [u.u], [y.u]; op = Dict(x => NaN)); + +println(linearize(simplified_sys, linfun; op = Dict(x => 1.0))) +println(linearize(simplified_sys, linfun; op = Dict(x => 0.0))) + +@time linearize(simplified_sys, linfun; op = Dict(x => 0.0)) + +nothing # hide +``` + +However, this route is still expensive since it has to repeatedly process the symbolic map provided to `op`. `linearize` is simply a wrapper for creating and solving a +[`ModelingToolkit.LinearizationProblem`](@ref). This object is symbolically indexable, and can thus integrate with SymbolicIndexingInterface.jl for fast updates. + +```@example LINEARIZE +using SymbolicIndexingInterface + +# The second argument is the value of the independent variable `t`. +linprob = LinearizationProblem(linfun, 1.0) +# It can be mutated +linprob.t = 0.0 +# create a setter function to update `x` efficiently +setter! = setu(linprob, x) + +function fast_linearize!(problem, setter!, value) + setter!(problem, value) + solve(problem) +end + +println(fast_linearize!(linprob, setter!, 1.0)) +println(fast_linearize!(linprob, setter!, 0.0)) + +@time fast_linearize!(linprob, setter!, 1.0) + +nothing # hide +``` + +Note that `linprob` above can be interacted with similar to a normal `ODEProblem`. + +```@repl LINEARIZE +prob[x] +prob[x] = 1.5 +prob[x] +``` + ## Symbolic linearization The function [`ModelingToolkit.linearize_symbolic`](@ref) works similar to [`ModelingToolkit.linearize`](@ref) but returns symbolic rather than numeric Jacobians. Symbolic linearization have several limitations and no all systems that can be linearized numerically can be linearized symbolically. @@ -89,4 +157,5 @@ Pages = ["Linearization.md"] linearize ModelingToolkit.linearize_symbolic ModelingToolkit.linearization_function +ModelingToolkit.LinearizationProblem ``` From 64427a9df3cc994304ed6c67499b4084a7a5e2db Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 27 Feb 2025 11:52:49 +0530 Subject: [PATCH 1014/2176] fix: fix `SCCNonlinearProblem` reordering parameters --- src/systems/nonlinear/nonlinearsystem.jl | 3 ++- test/scc_nonlinear_problem.jl | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/systems/nonlinear/nonlinearsystem.jl b/src/systems/nonlinear/nonlinearsystem.jl index 06e843d6be..ad3fbfdeef 100644 --- a/src/systems/nonlinear/nonlinearsystem.jl +++ b/src/systems/nonlinear/nonlinearsystem.jl @@ -792,7 +792,8 @@ function SciMLBase.SCCNonlinearProblem{iip}(sys::NonlinearSystem, u0map, new_eqs = eqs[reduce(vcat, eq_sccs)] @set! sys.unknowns = new_dvs @set! sys.eqs = new_eqs - sys = complete(sys) + @set! sys.index_cache = subset_unknowns_observed( + get_index_cache(sys), sys, new_dvs, getproperty.(obs, (:lhs,))) return SCCNonlinearProblem(subprobs, explicitfuns, p, true; sys) end diff --git a/test/scc_nonlinear_problem.jl b/test/scc_nonlinear_problem.jl index ef4638cdb0..92030a84aa 100644 --- a/test/scc_nonlinear_problem.jl +++ b/test/scc_nonlinear_problem.jl @@ -263,3 +263,22 @@ end sol = solve(prob, NewtonRaphson()) @test SciMLBase.successful_retcode(sol) end + +@testset "SCCNonlinearProblem retains parameter order" begin + @variables x y z + @parameters σ β ρ + @mtkbuild fullsys = NonlinearSystem( + [0 ~ x^3 * β + y^3 * ρ - σ, 0 ~ x^2 + 2x * y + y^2, 0 ~ z^2 - 4z + 4], + [x, y, z], [σ, β, ρ]) + + u0 = [x => 1.0, + y => 0.0, + z => 0.0] + + p = [σ => 28.0, + ρ => 10.0, + β => 8 / 3] + + sccprob = SCCNonlinearProblem(fullsys, u0, p) + @test isequal(parameters(fullsys), parameters(sccprob.f.sys)) +end From c82fc9b0fdd9b23370ad71846873d75922bb26f8 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 27 Feb 2025 15:16:12 +0530 Subject: [PATCH 1015/2176] build: bump patch version --- Project.toml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Project.toml b/Project.toml index 8c4a58045e..8061f6f1ef 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "ModelingToolkit" uuid = "961ee093-0014-501f-94e3-6117800e7a78" authors = ["Yingbo Ma ", "Chris Rackauckas and contributors"] -version = "9.64.2" +version = "9.64.3" [deps] AbstractTrees = "1520ce14-60c1-5f80-bbc7-55ef81b5835c" @@ -190,4 +190,4 @@ Sundials = "c3572dad-4567-51f8-b174-8c6c989267f4" Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" [targets] -test = ["AmplNLWriter", "BenchmarkTools", "BoundaryValueDiffEqMIRK", "BoundaryValueDiffEqAscher", "ControlSystemsBase", "DataInterpolations", "DelayDiffEq", "NonlinearSolve", "ForwardDiff", "Ipopt", "Ipopt_jll", "ModelingToolkitStandardLibrary", "Optimization", "OptimizationOptimJL", "OptimizationMOI", "OrdinaryDiffEq", "OrdinaryDiffEqCore", "OrdinaryDiffEqDefault", "REPL", "Random", "ReferenceTests", "SafeTestsets", "StableRNGs", "Statistics", "SteadyStateDiffEq", "Test", "StochasticDiffEq", "Sundials", "StochasticDelayDiffEq", "Pkg", "JET", "OrdinaryDiffEqNonlinearSolve", "Logging"] \ No newline at end of file +test = ["AmplNLWriter", "BenchmarkTools", "BoundaryValueDiffEqMIRK", "BoundaryValueDiffEqAscher", "ControlSystemsBase", "DataInterpolations", "DelayDiffEq", "NonlinearSolve", "ForwardDiff", "Ipopt", "Ipopt_jll", "ModelingToolkitStandardLibrary", "Optimization", "OptimizationOptimJL", "OptimizationMOI", "OrdinaryDiffEq", "OrdinaryDiffEqCore", "OrdinaryDiffEqDefault", "REPL", "Random", "ReferenceTests", "SafeTestsets", "StableRNGs", "Statistics", "SteadyStateDiffEq", "Test", "StochasticDiffEq", "Sundials", "StochasticDelayDiffEq", "Pkg", "JET", "OrdinaryDiffEqNonlinearSolve", "Logging"] From e5245e1256f732a2951af0ff1564d115553c01fe Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 21 Feb 2025 13:39:35 +0530 Subject: [PATCH 1016/2176] feat: run trivial initialization in problem constructor --- src/systems/diffeqs/abstractodesystem.jl | 13 ++++++------ src/systems/diffeqs/sdesystem.jl | 4 ++-- src/systems/nonlinear/nonlinearsystem.jl | 4 ++-- test/initializationsystem.jl | 26 ++++++++++++++++++++++++ 4 files changed, 37 insertions(+), 10 deletions(-) diff --git a/src/systems/diffeqs/abstractodesystem.jl b/src/systems/diffeqs/abstractodesystem.jl index 205d7e4601..e8f826eb49 100644 --- a/src/systems/diffeqs/abstractodesystem.jl +++ b/src/systems/diffeqs/abstractodesystem.jl @@ -759,7 +759,7 @@ function DiffEqBase.ODEProblem{iip, specialize}(sys::AbstractODESystem, u0map = kwargs1 = merge(kwargs1, (; tstops)) end - return ODEProblem{iip}(f, u0, tspan, p, pt; kwargs1..., kwargs...) + return remake(ODEProblem{iip}(f, u0, tspan, p, pt; kwargs1..., kwargs...)) end get_callback(prob::ODEProblem) = prob.kwargs[:callback] @@ -963,8 +963,9 @@ function DiffEqBase.DAEProblem{iip}(sys::AbstractODESystem, du0map, u0map, tspan kwargs1 = merge(kwargs1, (; tstops)) end - DAEProblem{iip}(f, du0, u0, tspan, p; differential_vars = differential_vars, - kwargs..., kwargs1...) + return remake(DAEProblem{iip}( + f, du0, u0, tspan, p; differential_vars = differential_vars, + kwargs..., kwargs1...)) end function generate_history(sys::AbstractODESystem, u0; expression = Val{false}, kwargs...) @@ -1008,7 +1009,7 @@ function DiffEqBase.DDEProblem{iip}(sys::AbstractODESystem, u0map = [], if cbs !== nothing kwargs1 = merge(kwargs1, (callback = cbs,)) end - DDEProblem{iip}(f, u0, h, tspan, p; kwargs1..., kwargs...) + return remake(DDEProblem{iip}(f, u0, h, tspan, p; kwargs1..., kwargs...)) end function DiffEqBase.SDDEProblem(sys::AbstractODESystem, args...; kwargs...) @@ -1057,9 +1058,9 @@ function DiffEqBase.SDDEProblem{iip}(sys::AbstractODESystem, u0map = [], else noise_rate_prototype = zeros(eltype(u0), size(noiseeqs)) end - SDDEProblem{iip}(f, f.g, u0, h, tspan, p; + return remake(SDDEProblem{iip}(f, f.g, u0, h, tspan, p; noise_rate_prototype = - noise_rate_prototype, kwargs1..., kwargs...) + noise_rate_prototype, kwargs1..., kwargs...)) end """ diff --git a/src/systems/diffeqs/sdesystem.jl b/src/systems/diffeqs/sdesystem.jl index bc3fd6b73e..70ace68d15 100644 --- a/src/systems/diffeqs/sdesystem.jl +++ b/src/systems/diffeqs/sdesystem.jl @@ -792,8 +792,8 @@ function DiffEqBase.SDEProblem{iip, specialize}( kwargs = filter_kwargs(kwargs) - SDEProblem{iip}(f, u0, tspan, p; callback = cbs, noise, - noise_rate_prototype = noise_rate_prototype, kwargs...) + return remake(SDEProblem{iip}(f, u0, tspan, p; callback = cbs, noise, + noise_rate_prototype = noise_rate_prototype, kwargs...)) end function DiffEqBase.SDEProblem(sys::ODESystem, args...; kwargs...) diff --git a/src/systems/nonlinear/nonlinearsystem.jl b/src/systems/nonlinear/nonlinearsystem.jl index ad3fbfdeef..a74fe0b8e6 100644 --- a/src/systems/nonlinear/nonlinearsystem.jl +++ b/src/systems/nonlinear/nonlinearsystem.jl @@ -519,7 +519,7 @@ function DiffEqBase.NonlinearProblem{iip}(sys::NonlinearSystem, u0map, f, u0, p = process_SciMLProblem(NonlinearFunction{iip}, sys, u0map, parammap; check_length, kwargs...) pt = something(get_metadata(sys), StandardNonlinearProblem()) - NonlinearProblem{iip}(f, u0, p, pt; filter_kwargs(kwargs)...) + return remake(NonlinearProblem{iip}(f, u0, p, pt; filter_kwargs(kwargs)...)) end """ @@ -548,7 +548,7 @@ function DiffEqBase.NonlinearLeastSquaresProblem{iip}(sys::NonlinearSystem, u0ma f, u0, p = process_SciMLProblem(NonlinearFunction{iip}, sys, u0map, parammap; check_length, kwargs...) pt = something(get_metadata(sys), StandardNonlinearProblem()) - NonlinearLeastSquaresProblem{iip}(f, u0, p; filter_kwargs(kwargs)...) + return remake(NonlinearLeastSquaresProblem{iip}(f, u0, p; filter_kwargs(kwargs)...)) end const TypeT = Union{DataType, UnionAll} diff --git a/test/initializationsystem.jl b/test/initializationsystem.jl index 45afa4163a..aaf7bdfce8 100644 --- a/test/initializationsystem.jl +++ b/test/initializationsystem.jl @@ -1388,3 +1388,29 @@ end integ1 = init(oprob1) @test integ1[X1] ≈ 1.0 end + +@testset "Trivial initialization is run on problem construction" begin + @variables _x(..) y(t) + @brownian a + @parameters tot + x = _x(t) + @testset "$Problem" for (Problem, lhs, rhs) in [ + (ODEProblem, D, 0.0), + (SDEProblem, D, a), + (DDEProblem, D, _x(t - 0.1)), + (SDDEProblem, D, _x(t - 0.1) + a) + ] + @mtkbuild sys = ModelingToolkit.System([lhs(x) ~ x + rhs, x + y ~ tot], t; + guesses = [tot => 1.0], defaults = [tot => missing]) + prob = Problem(sys, [x => 1.0, y => 1.0], (0.0, 1.0)) + @test prob.ps[tot] ≈ 2.0 + end + @testset "$Problem" for Problem in [NonlinearProblem, NonlinearLeastSquaresProblem] + @parameters p1 p2 + @mtkbuild sys = NonlinearSystem([x^2 + y^2 ~ p1, (x - 1)^2 + (y - 1)^2 ~ p2]; + parameter_dependencies = [p2 ~ 2p1], + guesses = [p1 => 0.0], defaults = [p1 => missing]) + prob = Problem(sys, [x => 1.0, y => 1.0], [p2 => 6.0]) + @test prob.ps[p1] ≈ 3.0 + end +end From 6ed57907526a4cd8e14e53e1fed2ee8da8a4a7ca Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 21 Feb 2025 17:52:45 +0530 Subject: [PATCH 1017/2176] fix: retain `u0_constructor` in `initializeprobmap` --- src/systems/problem_utils.jl | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/systems/problem_utils.jl b/src/systems/problem_utils.jl index 77f4229696..1cd8304eb9 100644 --- a/src/systems/problem_utils.jl +++ b/src/systems/problem_utils.jl @@ -601,7 +601,7 @@ All other keyword arguments are forwarded to `InitializationProblem`. """ function maybe_build_initialization_problem( sys::AbstractSystem, op::AbstractDict, u0map, pmap, t, defs, - guesses, missing_unknowns; implicit_dae = false, kwargs...) + guesses, missing_unknowns; implicit_dae = false, u0_constructor = identity, kwargs...) guesses = merge(ModelingToolkit.guesses(sys), todict(guesses)) if t === nothing && is_time_dependent(sys) @@ -615,7 +615,7 @@ function maybe_build_initialization_problem( if is_time_dependent(sys) all_init_syms = Set(all_symbols(initializeprob)) solved_unknowns = filter(var -> var in all_init_syms, unknowns(sys)) - initializeprobmap = getu(initializeprob, solved_unknowns) + initializeprobmap = u0_constructor ∘ getu(initializeprob, solved_unknowns) else initializeprobmap = nothing end @@ -781,7 +781,8 @@ function process_SciMLProblem( eval_expression, eval_module, fully_determined, warn_cyclic_dependency, check_units = check_initialization_units, circular_dependency_max_cycle_length, circular_dependency_max_cycles, use_scc, - force_time_independent = force_initialization_time_independent, algebraic_only, allow_incomplete) + force_time_independent = force_initialization_time_independent, algebraic_only, allow_incomplete, + u0_constructor) kwargs = merge(kwargs, kws) end From 76d022a3673332c088132d743a8e572b74e83dc7 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 24 Feb 2025 11:37:46 +0530 Subject: [PATCH 1018/2176] docs: add comments explaining why `remake` is called in the problem constructor --- src/systems/diffeqs/abstractodesystem.jl | 4 ++++ src/systems/diffeqs/sdesystem.jl | 1 + src/systems/nonlinear/nonlinearsystem.jl | 2 ++ 3 files changed, 7 insertions(+) diff --git a/src/systems/diffeqs/abstractodesystem.jl b/src/systems/diffeqs/abstractodesystem.jl index e8f826eb49..5140c0635f 100644 --- a/src/systems/diffeqs/abstractodesystem.jl +++ b/src/systems/diffeqs/abstractodesystem.jl @@ -759,6 +759,7 @@ function DiffEqBase.ODEProblem{iip, specialize}(sys::AbstractODESystem, u0map = kwargs1 = merge(kwargs1, (; tstops)) end + # Call `remake` so it runs initialization if it is trivial return remake(ODEProblem{iip}(f, u0, tspan, p, pt; kwargs1..., kwargs...)) end get_callback(prob::ODEProblem) = prob.kwargs[:callback] @@ -963,6 +964,7 @@ function DiffEqBase.DAEProblem{iip}(sys::AbstractODESystem, du0map, u0map, tspan kwargs1 = merge(kwargs1, (; tstops)) end + # Call `remake` so it runs initialization if it is trivial return remake(DAEProblem{iip}( f, du0, u0, tspan, p; differential_vars = differential_vars, kwargs..., kwargs1...)) @@ -1009,6 +1011,7 @@ function DiffEqBase.DDEProblem{iip}(sys::AbstractODESystem, u0map = [], if cbs !== nothing kwargs1 = merge(kwargs1, (callback = cbs,)) end + # Call `remake` so it runs initialization if it is trivial return remake(DDEProblem{iip}(f, u0, h, tspan, p; kwargs1..., kwargs...)) end @@ -1058,6 +1061,7 @@ function DiffEqBase.SDDEProblem{iip}(sys::AbstractODESystem, u0map = [], else noise_rate_prototype = zeros(eltype(u0), size(noiseeqs)) end + # Call `remake` so it runs initialization if it is trivial return remake(SDDEProblem{iip}(f, f.g, u0, h, tspan, p; noise_rate_prototype = noise_rate_prototype, kwargs1..., kwargs...)) diff --git a/src/systems/diffeqs/sdesystem.jl b/src/systems/diffeqs/sdesystem.jl index 70ace68d15..d8ac52dd1a 100644 --- a/src/systems/diffeqs/sdesystem.jl +++ b/src/systems/diffeqs/sdesystem.jl @@ -792,6 +792,7 @@ function DiffEqBase.SDEProblem{iip, specialize}( kwargs = filter_kwargs(kwargs) + # Call `remake` so it runs initialization if it is trivial return remake(SDEProblem{iip}(f, u0, tspan, p; callback = cbs, noise, noise_rate_prototype = noise_rate_prototype, kwargs...)) end diff --git a/src/systems/nonlinear/nonlinearsystem.jl b/src/systems/nonlinear/nonlinearsystem.jl index a74fe0b8e6..42ef380337 100644 --- a/src/systems/nonlinear/nonlinearsystem.jl +++ b/src/systems/nonlinear/nonlinearsystem.jl @@ -519,6 +519,7 @@ function DiffEqBase.NonlinearProblem{iip}(sys::NonlinearSystem, u0map, f, u0, p = process_SciMLProblem(NonlinearFunction{iip}, sys, u0map, parammap; check_length, kwargs...) pt = something(get_metadata(sys), StandardNonlinearProblem()) + # Call `remake` so it runs initialization if it is trivial return remake(NonlinearProblem{iip}(f, u0, p, pt; filter_kwargs(kwargs)...)) end @@ -548,6 +549,7 @@ function DiffEqBase.NonlinearLeastSquaresProblem{iip}(sys::NonlinearSystem, u0ma f, u0, p = process_SciMLProblem(NonlinearFunction{iip}, sys, u0map, parammap; check_length, kwargs...) pt = something(get_metadata(sys), StandardNonlinearProblem()) + # Call `remake` so it runs initialization if it is trivial return remake(NonlinearLeastSquaresProblem{iip}(f, u0, p; filter_kwargs(kwargs)...)) end From e2a4b31084528820bc35668c5bc34878da9d18ea Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 24 Feb 2025 14:38:40 +0530 Subject: [PATCH 1019/2176] fix: handle `u0` provided as `StaticArray` with no `u0_constructor` --- src/systems/problem_utils.jl | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/systems/problem_utils.jl b/src/systems/problem_utils.jl index 1cd8304eb9..85dfded21c 100644 --- a/src/systems/problem_utils.jl +++ b/src/systems/problem_utils.jl @@ -774,6 +774,10 @@ function process_SciMLProblem( op, missing_unknowns, missing_pars = build_operating_point!(sys, u0map, pmap, defs, cmap, dvs, ps) + if u0_constructor === identity && u0Type <: StaticArray + u0_constructor = vals -> SymbolicUtils.Code.create_array( + u0Type, eltype(vals), Val(1), Val(length(vals)), vals...) + end if build_initializeprob kws = maybe_build_initialization_problem( sys, op, u0map, pmap, t, defs, guesses, missing_unknowns; From 3207fc7afc15044e9dfafa6a0c6fc606d79cee38 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 24 Feb 2025 14:40:30 +0530 Subject: [PATCH 1020/2176] test: update tests to account for eager initialization --- test/initializationsystem.jl | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/test/initializationsystem.jl b/test/initializationsystem.jl index aaf7bdfce8..236f9e5ce3 100644 --- a/test/initializationsystem.jl +++ b/test/initializationsystem.jl @@ -720,7 +720,8 @@ end @parameters x0 y0 @mtkbuild sys = ODESystem([x ~ x0, y ~ y0, s ~ x + y], t; guesses = [y0 => 0.0]) prob = ODEProblem(sys, [s => 1.0], (0.0, 1.0), [x0 => 0.3, y0 => missing]) - @test prob.ps[y0] ≈ 0.0 + # trivial initialization run immediately + @test prob.ps[y0] ≈ 0.7 @test init(prob, Tsit5()).ps[y0] ≈ 0.7 @test solve(prob, Tsit5()).ps[y0] ≈ 0.7 end @@ -745,7 +746,8 @@ end systems = [fixed, spring, mass, gravity, constant, damper], guesses = [spring.s_rel0 => 1.0]) prob = ODEProblem(sys, [], (0.0, 1.0), [spring.s_rel0 => missing]) - @test prob.ps[spring.s_rel0] ≈ 0.0 + # trivial initialization run immediately + @test prob.ps[spring.s_rel0] ≈ -3.905 @test init(prob, Tsit5()).ps[spring.s_rel0] ≈ -3.905 @test solve(prob, Tsit5()).ps[spring.s_rel0] ≈ -3.905 end From c768c3e62f71fa278f57b982dd899ac82494c564 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 27 Feb 2025 16:00:56 +0530 Subject: [PATCH 1021/2176] fix: pass `u0_constructor` to `process_SciMLProblem` in `DDEProblem`, `SDDEProblem` --- src/systems/diffeqs/abstractodesystem.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/systems/diffeqs/abstractodesystem.jl b/src/systems/diffeqs/abstractodesystem.jl index 5140c0635f..01f847d5d6 100644 --- a/src/systems/diffeqs/abstractodesystem.jl +++ b/src/systems/diffeqs/abstractodesystem.jl @@ -994,7 +994,7 @@ function DiffEqBase.DDEProblem{iip}(sys::AbstractODESystem, u0map = [], end f, u0, p = process_SciMLProblem(DDEFunction{iip}, sys, u0map, parammap; t = tspan !== nothing ? tspan[1] : tspan, - symbolic_u0 = true, + symbolic_u0 = true, u0_constructor, check_length, eval_expression, eval_module, kwargs...) h_gen = generate_history(sys, u0; expression = Val{true}) h_oop, h_iip = eval_or_rgf.(h_gen; eval_expression, eval_module) @@ -1033,7 +1033,7 @@ function DiffEqBase.SDDEProblem{iip}(sys::AbstractODESystem, u0map = [], end f, u0, p = process_SciMLProblem(SDDEFunction{iip}, sys, u0map, parammap; t = tspan !== nothing ? tspan[1] : tspan, - symbolic_u0 = true, eval_expression, eval_module, + symbolic_u0 = true, eval_expression, eval_module, u0_constructor, check_length, kwargs...) h_gen = generate_history(sys, u0; expression = Val{true}) h_oop, h_iip = eval_or_rgf.(h_gen; eval_expression, eval_module) From 312983a6f638530a79c40c9a9e857711b37b4075 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 21 Feb 2025 17:42:32 +0530 Subject: [PATCH 1022/2176] docs: add doc page for FMU import capability --- docs/Project.toml | 2 + docs/pages.jl | 3 +- docs/src/tutorials/fmi.md | 226 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 230 insertions(+), 1 deletion(-) create mode 100644 docs/src/tutorials/fmi.md diff --git a/docs/Project.toml b/docs/Project.toml index 1f43934352..5e9cc200de 100644 --- a/docs/Project.toml +++ b/docs/Project.toml @@ -6,6 +6,8 @@ DataInterpolations = "82cc6244-b520-54b8-b5a6-8a565e85f1d0" Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f" Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4" DynamicQuantities = "06fc5a27-2a28-4c7c-a15d-362465fb6821" +FMI = "14a09403-18e3-468f-ad8a-74f8dda2d9ac" +FMIZoo = "724179cf-c260-40a9-bd27-cccc6fe2f195" LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" ModelingToolkit = "961ee093-0014-501f-94e3-6117800e7a78" ModelingToolkitStandardLibrary = "16a59e39-deab-5bd0-87e4-056b12336739" diff --git a/docs/pages.jl b/docs/pages.jl index 6e18f403dd..f6c49a0de3 100644 --- a/docs/pages.jl +++ b/docs/pages.jl @@ -14,7 +14,8 @@ pages = [ "tutorials/SampledData.md", "tutorials/domain_connections.md", "tutorials/callable_params.md", - "tutorials/linear_analysis.md"], + "tutorials/linear_analysis.md", + "tutorials/fmi.md"], "Examples" => Any[ "Basic Examples" => Any["examples/higher_order.md", "examples/spring_mass.md", diff --git a/docs/src/tutorials/fmi.md b/docs/src/tutorials/fmi.md new file mode 100644 index 0000000000..ef00477c78 --- /dev/null +++ b/docs/src/tutorials/fmi.md @@ -0,0 +1,226 @@ +# Importing FMUs + +ModelingToolkit is able to import FMUs following the [FMI Standard](https://fmi-standard.org/) versions 2 and 3. +This integration is done through [FMI.jl](https://github.com/ThummeTo/FMI.jl) and requires importing it to +enable the relevant functionality. Currently Model Exchange (ME) and CoSimulation (CS) FMUs are supported. +Events, non-floating-point variables and array variables are not supported. Additionally, calculating the +time derivatives of FMU states/outputs is not supported. + +!!! danger "Experimental" + + This functionality is currently experimental and subject to change without a breaking release of + ModelingToolkit.jl. + +## FMUs of full models + +Here, we will demonstrate the usage of an FMU of an entire model (as opposed to a single component). +First, the required libraries must be imported and the FMU loaded using FMI.jl. + +```@example fmi +using ModelingToolkit, FMI, FMIZoo, OrdinaryDiffEq +using ModelingToolkit: t_nounits as t, D_nounits as D + +# This is a spring-pendulum FMU from FMIZoo.jl. It is a v2 FMU +# and we are importing it in ModelExchange format. +fmu = loadFMU("SpringPendulum1D", "Dymola", "2022x"; type = :ME) +``` + +Following are the variables in the FMU (both states and parameters): + +```@example fmi +fmu.modelDescription.modelVariables +``` + +Next, [`FMIComponent`](@ref) is used to import the FMU as an MTK component. We provide the FMI +major version as a `Val` to the constructor, along with the loaded FMU and the type as keyword +arguments. + +```@example fmi +@named model = ModelingToolkit.FMIComponent(Val(2); fmu, type = :ME) +``` + +Note how hierarchical names in the FMU (e.g. `mass.m` or `spring.f`) are turned into flattened +names, with `__` being the namespace separator (`mass__m` and `spring__f`). + +!!! note + + Eventually we plan to reconstruct a hierarchical system structure mirroring the one indicated + by the variables in the FMU. This would allow accessing the above mentioned variables as + `model.mass.m` and `model.spring.f` instead of `model.mass__m` and `model.spring__f` respectively. + +Derivative variables such as `der(mass.v)` use the dummy derivative notation, and are hence transformed +into a form similar to `mass__vˍt`. However, they can still be referred to as `D(model.mass__v)`. + +```@example fmi +equations(model) +``` + +Since the FMI spec allows multiple names to alias the same quantity, ModelingToolkit.jl creates +equations to alias them. For example, it can be seen above that `der(mass.v)` and `mass.a` have the +same reference, and hence refer to the same quantity. Correspondingly, there is an equation +`mass__vˍt(t) ~ mass__a(t)` in the system. + +!!! note + + Any variables and/or parameters that are not part of the FMU should be ignored, as ModelingToolkit + creates them to manage the FMU. Unexpected usage of these variables/parameters can lead to errors. + +```@example fmi +defaults(model) +``` + +All parameters in the FMU are given a default equal to their start value, if present. Unknowns are not +assigned defaults even if a start value is present, as this would conflict with ModelingToolkit's own +initialization semantics. + +We can simulate this model like any other ModelingToolkit system. + +```@repl fmi +sys = structural_simplify(model) +prob = ODEProblem(sys, [sys.mass__s => 0.5, sys.mass__v => 0.0], (0.0, 5.0)) +sol = solve(prob, Tsit5()) +``` + +We can interpolate the solution object to obtain values at arbitrary time points in the solved interval, +just like a normal solution. + +```@repl fmi +sol(0.0:0.1:1.0; idxs = sys.mass_a) +``` + +FMUs following version 3 of the specification can be simulated with almost the same process. This time, +we will create a model from a CoSimulation FMU. + +```@example fmi +fmu = loadFMU("SpringPendulum1D", "Dymola", "2023x", "3.0"; type = :CS) +@named inner = ModelingToolkit.FMIComponent( + Val(3); fmu, communication_step_size = 0.001, type = :CS) +``` + +This FMU has fewer equations, partly due to missing aliasing variables and partly due to being a CS FMU. +CoSimulation FMUs are bundled with an integrator. As such, they do not function like ME FMUs. Instead, +a callback steps the FMU at periodic intervals in time and obtains the updated state. This state is held +constant until the next time the callback triggers. The periodic interval must be specified through the +`communication_step_size` keyword argument. A smaller step size typically leads to less error but is +more computationally expensive. + +This model alone does not have any differential variables, and calling `structural_simplify` will lead +to an `ODESystem` with no unknowns. + +```@example fmi +structural_simplify(inner) +``` + +Simulating this model will cause the OrdinaryDiffEq integrator to immediately finish, and will not +trigger the callback. Thus, we wrap this system in a trivial system with a differential variable. + +```@example fmi +@variables x(t) = 1.0 +@mtkbuild sys = ODESystem([D(x) ~ x], t; systems = [inner]) +``` + +We can now simulate `sys`. + +```@example fmi +prob = ODEProblem(sys, [sys.inner.mass__s => 0.5, sys.inner.mass__v => 0.0], (0.0, 5.0)) +sol = solve(prob, Tsit5()) +``` + +The variables of the FMU are discrete, and their timeseries can be obtained at intervals of +`communication_step_size`. + +```@example fmi +sol[sys.inner.mass__s] +``` + +## FMUs of components + +FMUs can also be imported as individual components. For this example, we will use custom FMUs used +in the test suite of ModelingToolkit.jl. + +```@example fmi +fmu = loadFMU( + joinpath(@__DIR__, "..", "..", "..", "test", "fmi", "fmus", "SimpleAdder.fmu"); + type = :ME) +fmu.modelDescription.modelVariables +``` + +This FMU is equivalent to the following model: + +```julia +@mtkmodel SimpleAdder begin + @variables begin + a(t) + b(t) + c(t) + out(t) + out2(t) + end + @parameters begin + value = 1.0 + end + @equations begin + out ~ a + b + value + D(c) ~ out + out2 ~ 2c + end +end +``` + +`a` and `b` are inputs, `c` is a state, and `out` and `out2` are outputs of the component. + +```@repl fmi +@named adder = ModelingToolkit.FMIComponent(Val(2); fmu, type = :ME); +isinput(adder.a) +isinput(adder.b) +isoutput(adder.out) +isoutput(adder.out2) +``` + +ModelingToolkit recognizes input and output variables of the component, and attaches the appropriate +metadata. We can now use this component as a subcomponent of a larger system. + +```@repl fmi +@variables a(t) b(t) c(t) [guess = 1.0]; +@mtkbuild sys = ODESystem( + [adder.a ~ a, adder.b ~ b, D(a) ~ t, + D(b) ~ adder.out + adder.c, c^2 ~ adder.out + adder.value], + t; + systems = [adder]) +equations(sys) +``` + +Note how the output `adder.out` is used in an algebraic equation of the system. We have also given +`sys.c` a guess, expecting it to be solved for by initialization. ModelingToolkit is able to use +FMUs in initialization to solve for initial states. As mentioned earlier, we cannot differentiate +through an FMU. Thus, automatic differentiation has to be disabled for the solver. + +```@example fmi +prob = ODEProblem(sys, [sys.adder.c => 2.0, sys.a => 1.0, sys.b => 1.0], + (0.0, 1.0), [sys.adder.value => 2.0]) +solve(prob, Rodas5P(autodiff = false)) +``` + +CoSimulation FMUs follow a nearly identical process. Since CoSimulation FMUs operate using callbacks, +after triggering the callbacks and altering the discrete state the algebraic equations may no longer +be satisfied. To resolve for the values of algebraic variables, we use the `reinitializealg` keyword +of `FMIComponent`. This is a DAE initialization algorithm to use at the end of every callback. Since +CoSimulation FMUs are not directly involved in the RHS of the system - instead operating through +callbacks - we can use a solver with automatic differentiation. + +```@example fmi +fmu = loadFMU( + joinpath(@__DIR__, "..", "..", "..", "test", "fmi", "fmus", "SimpleAdder.fmu"); + type = :CS) +@named adder = ModelingToolkit.FMIComponent( + Val(2); fmu, type = :CS, communication_step_size = 1e-3, + reinitializealg = BrownFullBasicInit()) +@mtkbuild sys = ODESystem( + [adder.a ~ a, adder.b ~ b, D(a) ~ t, + D(b) ~ adder.out + adder.c, c^2 ~ adder.out + adder.value], + t; + systems = [adder]) +prob = ODEProblem(sys, [sys.adder.c => 2.0, sys.a => 1.0, sys.b => 1.0], + (0.0, 1.0), [sys.adder.value => 2.0]) +solve(prob, Rodas5P()) +``` From 77774ae589eaf320ff165168043937f000fd63d7 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 24 Feb 2025 11:59:24 +0530 Subject: [PATCH 1023/2176] docs: fix `DiffCache` aliasing `odeprob.p.tunable` in `remake` doc example --- docs/src/examples/remake.md | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/docs/src/examples/remake.md b/docs/src/examples/remake.md index 47ac02ee0a..91dba4d7ae 100644 --- a/docs/src/examples/remake.md +++ b/docs/src/examples/remake.md @@ -98,8 +98,10 @@ using SymbolicIndexingInterface optfn = OptimizationFunction(loss, Optimization.AutoForwardDiff()) # function to set the parameters we are optimizing setter = setp(odeprob, [α, β, γ, δ]) -# `DiffCache` to avoid allocations -diffcache = DiffCache(canonicalize(Tunable(), parameter_values(odeprob))[1]) +# `DiffCache` to avoid allocations. +# `copy` prevents the buffer stored by `DiffCache` from aliasing the one in +# `parameter_values(odeprob)`. +diffcache = DiffCache(copy(canonicalize(Tunable(), parameter_values(odeprob))[1])) # parameter object is a tuple, to store differently typed objects together optprob = OptimizationProblem( optfn, rand(4), (odeprob, timesteps, data, setter, diffcache), From 0f04a5368edd0b8ab4d69263f320df105ab4bdc4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Miclu=C8=9Ba-C=C3=A2mpeanu?= Date: Mon, 17 Feb 2025 12:00:36 +0000 Subject: [PATCH 1024/2176] test: add test for vector parameters in function args Co-authored-by: Aayush Sabharwal --- test/scc_nonlinear_problem.jl | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/test/scc_nonlinear_problem.jl b/test/scc_nonlinear_problem.jl index 92030a84aa..620347403a 100644 --- a/test/scc_nonlinear_problem.jl +++ b/test/scc_nonlinear_problem.jl @@ -282,3 +282,12 @@ end sccprob = SCCNonlinearProblem(fullsys, u0, p) @test isequal(parameters(fullsys), parameters(sccprob.f.sys)) end + +@testset "Vector parameters in function arguments" begin + @variables x y + @parameters p[1:2] (f::Function)(..) + + @mtkbuild sys = NonlinearSystem([x^2 - p[1]^2 ~ 0, y^2 ~ f(p)]) + prob = NonlinearProblem(sys, [x => 1.0, y => 1.0], [p => ones(2), f => sum]) + @test_nowarn solve(prob, NewtonRaphson()) +end From 69e5c80cb65b47fc8da415ca5cc006cb283effdb Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 28 Feb 2025 12:06:13 +0530 Subject: [PATCH 1025/2176] fix: fix vector parameter discovery when used both unscalarized and partially scalarized --- src/systems/nonlinear/nonlinearsystem.jl | 6 ++++++ test/nonlinearsystem.jl | 10 ++++++++++ 2 files changed, 16 insertions(+) diff --git a/src/systems/nonlinear/nonlinearsystem.jl b/src/systems/nonlinear/nonlinearsystem.jl index ad3fbfdeef..8b893b6cd2 100644 --- a/src/systems/nonlinear/nonlinearsystem.jl +++ b/src/systems/nonlinear/nonlinearsystem.jl @@ -219,6 +219,12 @@ function NonlinearSystem(eqs; kwargs...) push!(new_ps, p) end else + if symbolic_type(p) == ArraySymbolic() && + Symbolics.shape(unwrap(p)) != Symbolics.Unknown() + for i in eachindex(p) + delete!(new_ps, p[i]) + end + end push!(new_ps, p) end end diff --git a/test/nonlinearsystem.jl b/test/nonlinearsystem.jl index c6318b1314..c8dda530e2 100644 --- a/test/nonlinearsystem.jl +++ b/test/nonlinearsystem.jl @@ -4,6 +4,7 @@ using DiffEqBase, SparseArrays using Test using NonlinearSolve using ForwardDiff +using SymbolicIndexingInterface using ModelingToolkit: value using ModelingToolkit: get_default_or_guess, MTKParameters @@ -380,3 +381,12 @@ end @test_throws ["single equation", "unknown"] IntervalNonlinearFunctionExpr( sys, (0.0, 1.0)) end + +@testset "Vector parameter used unscalarized and partially scalarized" begin + @variables x y + @parameters p[1:2] (f::Function)(..) + + @mtkbuild sys = NonlinearSystem([x^2 - p[1]^2 ~ 0, y^2 ~ f(p)]) + @test !any(isequal(p[1]), parameters(sys)) + @test is_parameter(sys, p) +end From d64b2126b27f093f79f766637af53ca03e01aaad Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 28 Feb 2025 12:06:39 +0530 Subject: [PATCH 1026/2176] test: use `SCCNonlinearProblem` in test --- test/scc_nonlinear_problem.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/scc_nonlinear_problem.jl b/test/scc_nonlinear_problem.jl index 620347403a..b2b326d090 100644 --- a/test/scc_nonlinear_problem.jl +++ b/test/scc_nonlinear_problem.jl @@ -288,6 +288,6 @@ end @parameters p[1:2] (f::Function)(..) @mtkbuild sys = NonlinearSystem([x^2 - p[1]^2 ~ 0, y^2 ~ f(p)]) - prob = NonlinearProblem(sys, [x => 1.0, y => 1.0], [p => ones(2), f => sum]) + prob = SCCNonlinearProblem(sys, [x => 1.0, y => 1.0], [p => ones(2), f => sum]) @test_nowarn solve(prob, NewtonRaphson()) end From a0019928f14e7e8e80fca9f2c34700d2bf1c4244 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 28 Feb 2025 16:47:02 +0530 Subject: [PATCH 1027/2176] fix: fix transition to Moshi.jl ADT --- src/discretedomain.jl | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/discretedomain.jl b/src/discretedomain.jl index c7f90007c5..67bef42c9e 100644 --- a/src/discretedomain.jl +++ b/src/discretedomain.jl @@ -85,7 +85,7 @@ $(TYPEDEF) Represents a sample operator. A discrete-time signal is created by sampling a continuous-time signal. # Constructors -`Sample(clock::Union{TimeDomain, InferredTimeDomain} = InferredDiscrete)` +`Sample(clock::Union{TimeDomain, InferredTimeDomain} = InferredDiscrete())` `Sample(dt::Real)` `Sample(x::Num)`, with a single argument, is shorthand for `Sample()(x)`. @@ -106,7 +106,7 @@ julia> Δ = Sample(0.01) """ struct Sample <: Operator clock::Any - Sample(clock::Union{TimeDomain, InferredTimeDomain} = InferredDiscrete) = new(clock) + Sample(clock::Union{TimeDomain, InferredTimeDomain} = InferredDiscrete()) = new(clock) end function Sample(arg::Real) @@ -190,7 +190,7 @@ struct ShiftIndex clock::Union{InferredTimeDomain, TimeDomain, IntegerSequence} steps::Int function ShiftIndex( - clock::Union{TimeDomain, InferredTimeDomain, IntegerSequence} = Inferred, steps::Int = 0) + clock::Union{TimeDomain, InferredTimeDomain, IntegerSequence} = Inferred(), steps::Int = 0) new(clock, steps) end ShiftIndex(dt::Real, steps::Int = 0) = new(Clock(dt), steps) @@ -232,7 +232,7 @@ function input_timedomain(s::Shift, arg = nothing) if has_time_domain(arg) return get_time_domain(arg) end - InferredDiscrete + InferredDiscrete() end """ @@ -244,7 +244,7 @@ function output_timedomain(s::Shift, arg = nothing) if has_time_domain(t, arg) return get_time_domain(t, arg) end - InferredDiscrete + InferredDiscrete() end input_timedomain(::Sample, _ = nothing) = Continuous() @@ -254,7 +254,7 @@ function input_timedomain(h::Hold, arg = nothing) if has_time_domain(arg) return get_time_domain(arg) end - InferredDiscrete # the Hold accepts any discrete + InferredDiscrete() # the Hold accepts any discrete end output_timedomain(::Hold, _ = nothing) = Continuous() From 97c5324ca03c25a2abd11df69170895a07e400dd Mon Sep 17 00:00:00 2001 From: vyudu Date: Fri, 28 Feb 2025 09:30:26 -0500 Subject: [PATCH 1028/2176] feat: add --- src/ModelingToolkit.jl | 2 +- src/systems/abstractsystem.jl | 16 ++++++++++++++++ test/variable_utils.jl | 13 +++++++++++++ 3 files changed, 30 insertions(+), 1 deletion(-) diff --git a/src/ModelingToolkit.jl b/src/ModelingToolkit.jl index 9fb5ff1299..099ce97324 100644 --- a/src/ModelingToolkit.jl +++ b/src/ModelingToolkit.jl @@ -286,7 +286,7 @@ export toexpr, get_variables export simplify, substitute export build_function export modelingtoolkitize -export generate_initializesystem, Initial +export generate_initializesystem, Initial, isInitial export alg_equations, diff_equations, has_alg_equations, has_diff_equations export get_alg_eqs, get_diff_eqs, has_alg_eqs, has_diff_eqs diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index 8fdd36e291..a1c6056492 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -717,6 +717,22 @@ function add_initialization_parameters(sys::AbstractSystem) return sys end +""" +Returns true if the variable or parameter `var` is of the form `Initial(x)`. +""" +function isInitial(var) + var = unwrap(var) + if iscall(var) + operation(var) isa Initial && return true + if operation(var) === getindex + operation(arguments(var)[1]) isa Initial && return true + end + else + return false + end + return false +end + """ $(TYPEDSIGNATURES) diff --git a/test/variable_utils.jl b/test/variable_utils.jl index ecc2421955..53515bd991 100644 --- a/test/variable_utils.jl +++ b/test/variable_utils.jl @@ -145,3 +145,16 @@ end @test isequal(parse_variable(sys, str), var) end end + +@testset "isInitial" begin + t = ModelingToolkit.to_nounits + @variables x(t) z(t)[1:5] + @parameters a b c[1:4] + @test isInitial(Initial(z)) + @test isInitial(Initial(x)) + @test isInitial(Initial(a)) + @test isInitial(Initial(z[1])) + @test isInitial(Initial(c[4])) + @test !isInitial(c) + @test !isInitial(x) +end From 5a56aa98afd54079438ed44bc3ab56fdf791b2ac Mon Sep 17 00:00:00 2001 From: vyudu Date: Fri, 28 Feb 2025 09:31:32 -0500 Subject: [PATCH 1029/2176] fix docstring --- src/systems/abstractsystem.jl | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index a1c6056492..f5b067ff0c 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -718,14 +718,14 @@ function add_initialization_parameters(sys::AbstractSystem) end """ -Returns true if the variable or parameter `var` is of the form `Initial(x)`. -""" -function isInitial(var) - var = unwrap(var) - if iscall(var) - operation(var) isa Initial && return true - if operation(var) === getindex - operation(arguments(var)[1]) isa Initial && return true +Returns true if the parameter `p` is of the form `Initial(x)`. +""" +function isInitial(p) + p = unwrap(p) + if iscall(p) + operation(p) isa Initial && return true + if operation(p) === getindex + operation(arguments(p)[1]) isa Initial && return true end else return false From 3036eaa3d55af4167c1f0fab383586e5803d22c2 Mon Sep 17 00:00:00 2001 From: vyudu Date: Fri, 28 Feb 2025 09:32:06 -0500 Subject: [PATCH 1030/2176] fix typo --- test/variable_utils.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/variable_utils.jl b/test/variable_utils.jl index 53515bd991..c21dd1765c 100644 --- a/test/variable_utils.jl +++ b/test/variable_utils.jl @@ -147,7 +147,7 @@ end end @testset "isInitial" begin - t = ModelingToolkit.to_nounits + t = ModelingToolkit.t_nounits @variables x(t) z(t)[1:5] @parameters a b c[1:4] @test isInitial(Initial(z)) From fc538244f1a9fff8afd3155646dbd33b4774902e Mon Sep 17 00:00:00 2001 From: vyudu Date: Fri, 28 Feb 2025 10:17:48 -0500 Subject: [PATCH 1031/2176] add Prev and Next to docs --- docs/src/systems/DiscreteSystem.md | 7 +++++++ docs/src/systems/ImplicitDiscreteSystem.md | 7 +++++++ docs/src/tutorials/SampledData.md | 13 +++++++++++-- src/ModelingToolkit.jl | 2 +- 4 files changed, 26 insertions(+), 3 deletions(-) diff --git a/docs/src/systems/DiscreteSystem.md b/docs/src/systems/DiscreteSystem.md index 5ede50c62a..1b2e6e81c5 100644 --- a/docs/src/systems/DiscreteSystem.md +++ b/docs/src/systems/DiscreteSystem.md @@ -26,3 +26,10 @@ structural_simplify DiscreteProblem(sys::DiscreteSystem, u0map, tspan) DiscreteFunction(sys::DiscreteSystem, args...) ``` + +## Discrete Domain +```@docs; canonical=false +Shift +Prev +Next +``` diff --git a/docs/src/systems/ImplicitDiscreteSystem.md b/docs/src/systems/ImplicitDiscreteSystem.md index d02ccc5e42..189f4fe65c 100644 --- a/docs/src/systems/ImplicitDiscreteSystem.md +++ b/docs/src/systems/ImplicitDiscreteSystem.md @@ -26,3 +26,10 @@ structural_simplify ImplicitDiscreteProblem(sys::ImplicitDiscreteSystem, u0map, tspan) ImplicitDiscreteFunction(sys::ImplicitDiscreteSystem, args...) ``` + +## Discrete Domain +```@docs; canonical=false +Shift +Prev +Next +``` diff --git a/docs/src/tutorials/SampledData.md b/docs/src/tutorials/SampledData.md index a72fd1698b..c700bae5c2 100644 --- a/docs/src/tutorials/SampledData.md +++ b/docs/src/tutorials/SampledData.md @@ -25,8 +25,10 @@ The operators [`Sample`](@ref) and [`Hold`](@ref) are thus providing the interfa The [`ShiftIndex`](@ref) operator is used to refer to past and future values of discrete-time variables. The example below illustrates its use, implementing the discrete-time system ```math -x(k+1) = 0.5x(k) + u(k) -y(k) = x(k) +\begin{align} + x(k+1) &= 0.5x(k) + u(k) \\ + y(k) &= x(k) +\end{align} ``` ```@example clocks @@ -187,3 +189,10 @@ connections = [r ~ sin(t) # reference signal @named cl = ODESystem(connections, t, systems = [f, c, p]) ``` + +```@docs +Sample +Hold +ShiftIndex +Clock +``` diff --git a/src/ModelingToolkit.jl b/src/ModelingToolkit.jl index 8adabb238f..11e0c0a390 100644 --- a/src/ModelingToolkit.jl +++ b/src/ModelingToolkit.jl @@ -301,7 +301,7 @@ export debug_system #export ContinuousClock, Discrete, sampletime, input_timedomain, output_timedomain #export has_discrete_domain, has_continuous_domain #export is_discrete_domain, is_continuous_domain, is_hybrid_domain -export Sample, Hold, Shift, ShiftIndex, sampletime, SampleTime +export Sample, Hold, Shift, ShiftIndex, sampletime, SampleTime, Next, Prev export Clock, SolverStepClock, TimeDomain export MTKParameters, reorder_dimension_by_tunables!, reorder_dimension_by_tunables From 1e2e6672f8c555070202e7e81018e27ecca22460 Mon Sep 17 00:00:00 2001 From: vyudu Date: Fri, 28 Feb 2025 10:23:38 -0500 Subject: [PATCH 1032/2176] format --- docs/src/systems/DiscreteSystem.md | 1 + docs/src/systems/ImplicitDiscreteSystem.md | 1 + src/ModelingToolkit.jl | 3 +- src/structural_transformation/utils.jl | 8 ++- .../implicit_discrete_system.jl | 25 +++++--- test/implicit_discrete_system.jl | 57 ++++++++++--------- test/structural_transformation/utils.jl | 14 +++-- 7 files changed, 62 insertions(+), 47 deletions(-) diff --git a/docs/src/systems/DiscreteSystem.md b/docs/src/systems/DiscreteSystem.md index 1b2e6e81c5..c00cc7f3c9 100644 --- a/docs/src/systems/DiscreteSystem.md +++ b/docs/src/systems/DiscreteSystem.md @@ -28,6 +28,7 @@ DiscreteFunction(sys::DiscreteSystem, args...) ``` ## Discrete Domain + ```@docs; canonical=false Shift Prev diff --git a/docs/src/systems/ImplicitDiscreteSystem.md b/docs/src/systems/ImplicitDiscreteSystem.md index 189f4fe65c..268d8ec13c 100644 --- a/docs/src/systems/ImplicitDiscreteSystem.md +++ b/docs/src/systems/ImplicitDiscreteSystem.md @@ -28,6 +28,7 @@ ImplicitDiscreteFunction(sys::ImplicitDiscreteSystem, args...) ``` ## Discrete Domain + ```@docs; canonical=false Shift Prev diff --git a/src/ModelingToolkit.jl b/src/ModelingToolkit.jl index 11e0c0a390..328e5b6df0 100644 --- a/src/ModelingToolkit.jl +++ b/src/ModelingToolkit.jl @@ -237,7 +237,8 @@ export DAEFunctionExpr, DAEProblemExpr export SDESystem, SDEFunction, SDEFunctionExpr, SDEProblemExpr export SystemStructure export DiscreteSystem, DiscreteProblem, DiscreteFunction, DiscreteFunctionExpr -export ImplicitDiscreteSystem, ImplicitDiscreteProblem, ImplicitDiscreteFunction, ImplicitDiscreteFunctionExpr +export ImplicitDiscreteSystem, ImplicitDiscreteProblem, ImplicitDiscreteFunction, + ImplicitDiscreteFunctionExpr export JumpSystem export ODEProblem, SDEProblem export NonlinearFunction, NonlinearFunctionExpr diff --git a/src/structural_transformation/utils.jl b/src/structural_transformation/utils.jl index 031ef6b2d8..14628f2958 100644 --- a/src/structural_transformation/utils.jl +++ b/src/structural_transformation/utils.jl @@ -530,7 +530,7 @@ Distribute a shift applied to a whole expression or equation. Shift(t, 1)(x + y) will become Shift(t, 1)(x) + Shift(t, 1)(y). Only shifts variables whose independent variable is the same t that appears in the Shift (i.e. constants, time-independent parameters, etc. do not get shifted). """ -function distribute_shift(var) +function distribute_shift(var) var = unwrap(var) var isa Equation && return distribute_shift(var.lhs) ~ distribute_shift(var.rhs) @@ -553,11 +553,13 @@ function _distribute_shift(expr, shift) args = arguments(expr) if ModelingToolkit.isvariable(expr) - (length(args) == 1 && isequal(shift.t, only(args))) ? (return shift(expr)) : (return expr) + (length(args) == 1 && isequal(shift.t, only(args))) ? (return shift(expr)) : + (return expr) elseif op isa Shift return shift(expr) else - return maketerm(typeof(expr), operation(expr), Base.Fix2(_distribute_shift, shift).(args), + return maketerm( + typeof(expr), operation(expr), Base.Fix2(_distribute_shift, shift).(args), unwrap(expr).metadata) end else diff --git a/src/systems/discrete_system/implicit_discrete_system.jl b/src/systems/discrete_system/implicit_discrete_system.jl index bf4e6037d1..cbc2555c36 100644 --- a/src/systems/discrete_system/implicit_discrete_system.jl +++ b/src/systems/discrete_system/implicit_discrete_system.jl @@ -239,7 +239,9 @@ function ImplicitDiscreteSystem(eqs, iv; kwargs...) collect(allunknowns), collect(new_ps); kwargs...) end -ImplicitDiscreteSystem(eq::Equation, args...; kwargs...) = ImplicitDiscreteSystem([eq], args...; kwargs...) +function ImplicitDiscreteSystem(eq::Equation, args...; kwargs...) + ImplicitDiscreteSystem([eq], args...; kwargs...) +end function flatten(sys::ImplicitDiscreteSystem, noeqs = false) systems = get_systems(sys) @@ -273,11 +275,13 @@ function generate_function( obs = observed(sys) shifted_obs = Symbolics.Equation[distribute_shift(Next(eq)) for eq in obs] obsidxs = observed_equations_used_by(sys, exprs; obs = shifted_obs) - extra_assignments = [Assignment(shifted_obs[i].lhs, shifted_obs[i].rhs) for i in obsidxs] + extra_assignments = [Assignment(shifted_obs[i].lhs, shifted_obs[i].rhs) + for i in obsidxs] u_next = map(Next, dvs) u = dvs - build_function_wrapper(sys, exprs, u_next, u, ps..., iv; p_start = 3, extra_assignments, kwargs...) + build_function_wrapper( + sys, exprs, u_next, u, ps..., iv; p_start = 3, extra_assignments, kwargs...) end function shift_u0map_forward(sys::ImplicitDiscreteSystem, u0map, defs) @@ -341,11 +345,13 @@ function SciMLBase.ImplicitDiscreteFunction(sys::ImplicitDiscreteSystem, args... ImplicitDiscreteFunction{true}(sys, args...; kwargs...) end -function SciMLBase.ImplicitDiscreteFunction{true}(sys::ImplicitDiscreteSystem, args...; kwargs...) +function SciMLBase.ImplicitDiscreteFunction{true}( + sys::ImplicitDiscreteSystem, args...; kwargs...) ImplicitDiscreteFunction{true, SciMLBase.AutoSpecialize}(sys, args...; kwargs...) end -function SciMLBase.ImplicitDiscreteFunction{false}(sys::ImplicitDiscreteSystem, args...; kwargs...) +function SciMLBase.ImplicitDiscreteFunction{false}( + sys::ImplicitDiscreteSystem, args...; kwargs...) ImplicitDiscreteFunction{false, SciMLBase.FullSpecialize}(sys, args...; kwargs...) end function SciMLBase.ImplicitDiscreteFunction{iip, specialize}( @@ -360,7 +366,6 @@ function SciMLBase.ImplicitDiscreteFunction{iip, specialize}( eval_module = @__MODULE__, analytic = nothing, kwargs...) where {iip, specialize} - if !iscomplete(sys) error("A completed `ImplicitDiscreteSystem` is required. Call `complete` or `structural_simplify` on the system before creating a `ImplicitDiscreteProblem`") end @@ -404,9 +409,12 @@ struct ImplicitDiscreteFunctionClosure{O, I} <: Function f_iip::I end (f::ImplicitDiscreteFunctionClosure)(u_next, u, p, t) = f.f_oop(u_next, u, p, t) -(f::ImplicitDiscreteFunctionClosure)(resid, u_next, u, p, t) = f.f_iip(resid, u_next, u, p, t) +function (f::ImplicitDiscreteFunctionClosure)(resid, u_next, u, p, t) + f.f_iip(resid, u_next, u, p, t) +end -function ImplicitDiscreteFunctionExpr{iip}(sys::ImplicitDiscreteSystem, dvs = unknowns(sys), +function ImplicitDiscreteFunctionExpr{iip}( + sys::ImplicitDiscreteSystem, dvs = unknowns(sys), ps = parameters(sys), u0 = nothing; version = nothing, p = nothing, linenumbers = false, @@ -427,4 +435,3 @@ end function ImplicitDiscreteFunctionExpr(sys::ImplicitDiscreteSystem, args...; kwargs...) ImplicitDiscreteFunctionExpr{true}(sys, args...; kwargs...) end - diff --git a/test/implicit_discrete_system.jl b/test/implicit_discrete_system.jl index f298f43e7c..70cc0b792e 100644 --- a/test/implicit_discrete_system.jl +++ b/test/implicit_discrete_system.jl @@ -3,72 +3,73 @@ using ModelingToolkit: t_nounits as t using StableRNGs k = ShiftIndex(t) -rng = StableRNG(22525) +rng = StableRNG(22525) @testset "Correct ImplicitDiscreteFunction" begin @variables x(t) = 1 - @mtkbuild sys = ImplicitDiscreteSystem([x(k) ~ x(k)*x(k-1) - 3], t) + @mtkbuild sys = ImplicitDiscreteSystem([x(k) ~ x(k) * x(k - 1) - 3], t) tspan = (0, 10) # u[2] - u_next[1] # -3 - u_next[2] + u_next[2]*u_next[1] f = ImplicitDiscreteFunction(sys) - u_next = [3., 1.5] - @test f(u_next, [2.,3.], [], t) ≈ [0., 0.] - u_next = [0., 0.] - @test f(u_next, [2.,3.], [], t) ≈ [3., -3.] - + u_next = [3.0, 1.5] + @test f(u_next, [2.0, 3.0], [], t) ≈ [0.0, 0.0] + u_next = [0.0, 0.0] + @test f(u_next, [2.0, 3.0], [], t) ≈ [3.0, -3.0] + resid = rand(2) - f(resid, u_next, [2.,3.], [], t) - @test resid ≈ [3., -3.] - - prob = ImplicitDiscreteProblem(sys, [x(k-1) => 3.], tspan) - @test prob.u0 == [3., 1.] + f(resid, u_next, [2.0, 3.0], [], t) + @test resid ≈ [3.0, -3.0] + + prob = ImplicitDiscreteProblem(sys, [x(k - 1) => 3.0], tspan) + @test prob.u0 == [3.0, 1.0] prob = ImplicitDiscreteProblem(sys, [], tspan) - @test prob.u0 == [1., 1.] + @test prob.u0 == [1.0, 1.0] @variables x(t) - @mtkbuild sys = ImplicitDiscreteSystem([x(k) ~ x(k)*x(k-1) - 3], t) - @test_throws ErrorException prob = ImplicitDiscreteProblem(sys, [], tspan) + @mtkbuild sys = ImplicitDiscreteSystem([x(k) ~ x(k) * x(k - 1) - 3], t) + @test_throws ErrorException prob=ImplicitDiscreteProblem(sys, [], tspan) end @testset "System with algebraic equations" begin @variables x(t) y(t) - eqs = [x(k) ~ x(k-1) + x(k-2), - x^2 ~ 1 - y^2] + eqs = [x(k) ~ x(k - 1) + x(k - 2), + x^2 ~ 1 - y^2] @mtkbuild sys = ImplicitDiscreteSystem(eqs, t) f = ImplicitDiscreteFunction(sys) - function correct_f(u_next, u, p, t) + function correct_f(u_next, u, p, t) [u[2] - u_next[1], - u[1] + u[2] - u_next[2], - 1 - (u_next[1]+u_next[2])^2 - u_next[3]^2] + u[1] + u[2] - u_next[2], + 1 - (u_next[1] + u_next[2])^2 - u_next[3]^2] end for _ in 1:10 u_next = rand(rng, 3) u = rand(rng, 3) - @test correct_f(u_next, u, [], 0.) ≈ f(u_next, u, [], 0.) + @test correct_f(u_next, u, [], 0.0) ≈ f(u_next, u, [], 0.0) end # Initialization is satisfied. - prob = ImplicitDiscreteProblem(sys, [x(k-1) => .3, x(k-2) => .4], (0, 10), guesses = [y => 1]) + prob = ImplicitDiscreteProblem( + sys, [x(k - 1) => 0.3, x(k - 2) => 0.4], (0, 10), guesses = [y => 1]) @test (prob.u0[1] + prob.u0[2])^2 + prob.u0[3]^2 ≈ 1 end @testset "Handle observables in function codegen" begin # Observable appears in differential equation @variables x(t) y(t) z(t) - eqs = [x(k) ~ x(k-1) + x(k-2), - y(k) ~ x(k) + x(k-2)*z(k-1), - x + y + z ~ 2] + eqs = [x(k) ~ x(k - 1) + x(k - 2), + y(k) ~ x(k) + x(k - 2) * z(k - 1), + x + y + z ~ 2] @mtkbuild sys = ImplicitDiscreteSystem(eqs, t) @test length(unknowns(sys)) == length(equations(sys)) == 3 @test occursin("var\"y(t)\"", string(ImplicitDiscreteFunctionExpr(sys))) # Shifted observable that appears in algebraic equation is properly handled. - eqs = [z(k) ~ x(k) + sin(x(k)), - y(k) ~ x(k-1) + x(k-2), - z(k) * x(k) ~ 3] + eqs = [z(k) ~ x(k) + sin(x(k)), + y(k) ~ x(k - 1) + x(k - 2), + z(k) * x(k) ~ 3] @mtkbuild sys = ImplicitDiscreteSystem(eqs, t) @test occursin("var\"Shift(t, 1)(z(t))\"", string(ImplicitDiscreteFunctionExpr(sys))) end diff --git a/test/structural_transformation/utils.jl b/test/structural_transformation/utils.jl index 0013fb4bbe..646b9ce74e 100644 --- a/test/structural_transformation/utils.jl +++ b/test/structural_transformation/utils.jl @@ -169,19 +169,21 @@ end k = ShiftIndex(t) # Expand shifts - @test isequal(ST.distribute_shift(Shift(t, -1)(x + y)), Shift(t, -1)(x) + Shift(t, -1)(y)) + @test isequal( + ST.distribute_shift(Shift(t, -1)(x + y)), Shift(t, -1)(x) + Shift(t, -1)(y)) expr = a * Shift(t, -2)(x) + Shift(t, 2)(y) + b @test isequal(ST.simplify_shifts(ST.distribute_shift(Shift(t, 2)(expr))), - a*x + Shift(t, 4)(y) + b) + a * x + Shift(t, 4)(y) + b) @test isequal(ST.distribute_shift(Shift(t, 2)(exp(z))), exp(Shift(t, 2)(z))) @test isequal(ST.distribute_shift(Shift(t, 2)(exp(a) + b)), exp(a) + b) - expr = a^x - log(b*y) + z*x - @test isequal(ST.distribute_shift(Shift(t, -3)(expr)), a^(Shift(t, -3)(x)) - log(b * Shift(t, -3)(y)) + Shift(t, -3)(z)*Shift(t, -3)(x)) + expr = a^x - log(b * y) + z * x + @test isequal(ST.distribute_shift(Shift(t, -3)(expr)), + a^(Shift(t, -3)(x)) - log(b * Shift(t, -3)(y)) + Shift(t, -3)(z) * Shift(t, -3)(x)) - expr = x(k+1) ~ x + x(k-1) - @test isequal(ST.distribute_shift(Shift(t, -1)(expr)), x ~ x(k-1) + x(k-2)) + expr = x(k + 1) ~ x + x(k - 1) + @test isequal(ST.distribute_shift(Shift(t, -1)(expr)), x ~ x(k - 1) + x(k - 2)) end @testset "`map_variables_to_equations`" begin From 506522c7f3b8ca2fafe4d205b70542301f26b26d Mon Sep 17 00:00:00 2001 From: vyudu Date: Fri, 28 Feb 2025 11:01:27 -0500 Subject: [PATCH 1033/2176] fix missing var --- test/structural_transformation/utils.jl | 1 + 1 file changed, 1 insertion(+) diff --git a/test/structural_transformation/utils.jl b/test/structural_transformation/utils.jl index 646b9ce74e..9bf239a574 100644 --- a/test/structural_transformation/utils.jl +++ b/test/structural_transformation/utils.jl @@ -5,6 +5,7 @@ using SparseArrays using UnPack using ModelingToolkit: t_nounits as t, D_nounits as D, default_toterm using Symbolics: unwrap +const ST = StructuralTransformations # Define some variables @parameters L g From af3995c842cca7d342a8a642797795ea83f859d4 Mon Sep 17 00:00:00 2001 From: vyudu Date: Fri, 28 Feb 2025 11:45:02 -0500 Subject: [PATCH 1034/2176] make initialization_data for IMplicitDIscreteFunction, fix bad initialization test --- src/systems/discrete_system/implicit_discrete_system.jl | 5 ++++- test/implicit_discrete_system.jl | 2 +- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/systems/discrete_system/implicit_discrete_system.jl b/src/systems/discrete_system/implicit_discrete_system.jl index cbc2555c36..ae5524768b 100644 --- a/src/systems/discrete_system/implicit_discrete_system.jl +++ b/src/systems/discrete_system/implicit_discrete_system.jl @@ -334,8 +334,10 @@ function SciMLBase.ImplicitDiscreteProblem( u0map = to_varmap(u0map, dvs) u0map = shift_u0map_forward(sys, u0map, defaults(sys)) + @show u0map f, u0, p = process_SciMLProblem( ImplicitDiscreteFunction, sys, u0map, parammap; eval_expression, eval_module, kwargs...) + @show u0 kwargs = filter_kwargs(kwargs) ImplicitDiscreteProblem(f, u0, tspan, p; kwargs...) @@ -388,7 +390,8 @@ function SciMLBase.ImplicitDiscreteFunction{iip, specialize}( ImplicitDiscreteFunction{iip, specialize}(f; sys = sys, observed = observedfun, - analytic = analytic) + analytic = analytic, + kwargs...) end """ diff --git a/test/implicit_discrete_system.jl b/test/implicit_discrete_system.jl index 70cc0b792e..932b6c6981 100644 --- a/test/implicit_discrete_system.jl +++ b/test/implicit_discrete_system.jl @@ -53,7 +53,7 @@ end # Initialization is satisfied. prob = ImplicitDiscreteProblem( sys, [x(k - 1) => 0.3, x(k - 2) => 0.4], (0, 10), guesses = [y => 1]) - @test (prob.u0[1] + prob.u0[2])^2 + prob.u0[3]^2 ≈ 1 + @test length(equations(prob.f.initialization_data.initializeprob.f.sys)) == 1 end @testset "Handle observables in function codegen" begin From 79f05d1b3baea8260471bc9f7eac5b8cd1d9597e Mon Sep 17 00:00:00 2001 From: CompatHelper Julia Date: Sat, 1 Mar 2025 00:27:01 +0000 Subject: [PATCH 1035/2176] CompatHelper: add new compat entry for FMI at version 0.14 for package docs, (keep existing compat) --- docs/Project.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/Project.toml b/docs/Project.toml index 5e9cc200de..815b73c1a0 100644 --- a/docs/Project.toml +++ b/docs/Project.toml @@ -33,6 +33,7 @@ DataInterpolations = "6.5" Distributions = "0.25" Documenter = "1" DynamicQuantities = "^0.11.2, 0.12, 1" +FMI = "0.14" ModelingToolkit = "8.33, 9" ModelingToolkitStandardLibrary = "2.19" NonlinearSolve = "3, 4" From 3d4c64811219eecb17cdacaf375bce63dfbaf6f7 Mon Sep 17 00:00:00 2001 From: CompatHelper Julia Date: Sat, 1 Mar 2025 00:27:05 +0000 Subject: [PATCH 1036/2176] CompatHelper: add new compat entry for FMIZoo at version 1 for package docs, (keep existing compat) --- docs/Project.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/Project.toml b/docs/Project.toml index 5e9cc200de..dabef83a4a 100644 --- a/docs/Project.toml +++ b/docs/Project.toml @@ -33,6 +33,7 @@ DataInterpolations = "6.5" Distributions = "0.25" Documenter = "1" DynamicQuantities = "^0.11.2, 0.12, 1" +FMIZoo = "1" ModelingToolkit = "8.33, 9" ModelingToolkitStandardLibrary = "2.19" NonlinearSolve = "3, 4" From 09db7327f8840aecdc99ae0203a4a5a422335bfd Mon Sep 17 00:00:00 2001 From: vyudu Date: Sat, 1 Mar 2025 18:02:31 -0500 Subject: [PATCH 1037/2176] change name, add docs --- docs/src/tutorials/initialization.md | 10 ++++++++++ src/ModelingToolkit.jl | 2 +- src/systems/abstractsystem.jl | 2 +- test/variable_utils.jl | 16 ++++++++-------- 4 files changed, 20 insertions(+), 10 deletions(-) diff --git a/docs/src/tutorials/initialization.md b/docs/src/tutorials/initialization.md index 27eb2ae28d..33694c1b7d 100644 --- a/docs/src/tutorials/initialization.md +++ b/docs/src/tutorials/initialization.md @@ -536,3 +536,13 @@ sol[α * x - β * x * y] ```@example init plot(sol) ``` + +## Summary of Initialization API +```@docs; canonical=false +Initial +isinitial +generate_initializesystem +initialization_equations +guesses +defaults +``` diff --git a/src/ModelingToolkit.jl b/src/ModelingToolkit.jl index 099ce97324..e6b8130af3 100644 --- a/src/ModelingToolkit.jl +++ b/src/ModelingToolkit.jl @@ -286,7 +286,7 @@ export toexpr, get_variables export simplify, substitute export build_function export modelingtoolkitize -export generate_initializesystem, Initial, isInitial +export generate_initializesystem, Initial, isinitial export alg_equations, diff_equations, has_alg_equations, has_diff_equations export get_alg_eqs, get_diff_eqs, has_alg_eqs, has_diff_eqs diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index f5b067ff0c..ddc70bb10a 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -720,7 +720,7 @@ end """ Returns true if the parameter `p` is of the form `Initial(x)`. """ -function isInitial(p) +function isinitial(p) p = unwrap(p) if iscall(p) operation(p) isa Initial && return true diff --git a/test/variable_utils.jl b/test/variable_utils.jl index c21dd1765c..3204d28836 100644 --- a/test/variable_utils.jl +++ b/test/variable_utils.jl @@ -146,15 +146,15 @@ end end end -@testset "isInitial" begin +@testset "isinitial" begin t = ModelingToolkit.t_nounits @variables x(t) z(t)[1:5] @parameters a b c[1:4] - @test isInitial(Initial(z)) - @test isInitial(Initial(x)) - @test isInitial(Initial(a)) - @test isInitial(Initial(z[1])) - @test isInitial(Initial(c[4])) - @test !isInitial(c) - @test !isInitial(x) + @test isinitial(Initial(z)) + @test isinitial(Initial(x)) + @test isinitial(Initial(a)) + @test isinitial(Initial(z[1])) + @test isinitial(Initial(c[4])) + @test !isinitial(c) + @test !isinitial(x) end From f883b8eb486fd998282e67025f1123605864e9d6 Mon Sep 17 00:00:00 2001 From: vyudu Date: Mon, 3 Mar 2025 09:17:11 -0500 Subject: [PATCH 1038/2176] Format --- docs/src/tutorials/initialization.md | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/src/tutorials/initialization.md b/docs/src/tutorials/initialization.md index 33694c1b7d..ba733e0bfb 100644 --- a/docs/src/tutorials/initialization.md +++ b/docs/src/tutorials/initialization.md @@ -538,6 +538,7 @@ plot(sol) ``` ## Summary of Initialization API + ```@docs; canonical=false Initial isinitial From 5567ab22186cf423e486b909d5a284bf8b020091 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 4 Mar 2025 14:17:04 +0530 Subject: [PATCH 1039/2176] feat: separate initial parameters for non-initialization-systems --- src/systems/index_cache.jl | 45 ++++++++++++++++++++++- src/systems/nonlinear/initializesystem.jl | 9 +++++ 2 files changed, 53 insertions(+), 1 deletion(-) diff --git a/src/systems/index_cache.jl b/src/systems/index_cache.jl index d71352da9f..5e6867edb2 100644 --- a/src/systems/index_cache.jl +++ b/src/systems/index_cache.jl @@ -49,6 +49,7 @@ struct IndexCache # sym => (clockidx, idx_in_clockbuffer) callback_to_clocks::Dict{Any, Vector{Int}} tunable_idx::TunableIndexMap + initials_idx::TunableIndexMap constant_idx::ParamIndexMap nonnumeric_idx::NonnumericMap observed_syms_to_timeseries::Dict{BasicSymbolic, TimeseriesSetType} @@ -56,6 +57,7 @@ struct IndexCache Union{BasicSymbolic, CallWithMetadata}, TimeseriesSetType} discrete_buffer_sizes::Vector{Vector{BufferTemplate}} tunable_buffer_size::BufferTemplate + initials_buffer_size::BufferTemplate constant_buffer_sizes::Vector{BufferTemplate} nonnumeric_buffer_sizes::Vector{BufferTemplate} symbol_to_variable::Dict{Symbol, SymbolicParam} @@ -251,7 +253,9 @@ function IndexCache(sys::AbstractSystem) tunable_idxs = TunableIndexMap() tunable_buffer_size = 0 - for buffers in (tunable_buffers, initial_param_buffers) + bufferlist = is_initializesystem(sys) ? (tunable_buffers, initial_param_buffers) : + (tunable_buffers,) + for buffers in bufferlist for (i, (_, buf)) in enumerate(buffers) for (j, p) in enumerate(buf) idx = if size(p) == () @@ -271,6 +275,43 @@ function IndexCache(sys::AbstractSystem) end end + initials_idxs = TunableIndexMap() + initials_buffer_size = 0 + if !is_initializesystem(sys) + for (i, (_, buf)) in enumerate(initial_param_buffers) + for (j, p) in enumerate(buf) + idx = if size(p) == () + initials_buffer_size + 1 + else + reshape( + (initials_buffer_size + 1):(initials_buffer_size + length(p)), size(p)) + end + initials_buffer_size += length(p) + initials_idxs[p] = idx + initials_idxs[default_toterm(p)] = idx + if hasname(p) && (!iscall(p) || operation(p) !== getindex) + symbol_to_variable[getname(p)] = p + symbol_to_variable[getname(default_toterm(p))] = p + end + end + end + end + + for k in collect(keys(tunable_idxs)) + v = tunable_idxs[k] + v isa AbstractArray || continue + for (kk, vv) in zip(collect(k), v) + tunable_idxs[kk] = vv + end + end + for k in collect(keys(initials_idxs)) + v = initials_idxs[k] + v isa AbstractArray || continue + for (kk, vv) in zip(collect(k), v) + initials_idxs[kk] = vv + end + end + dependent_pars_to_timeseries = Dict{ Union{BasicSymbolic, CallWithMetadata}, TimeseriesSetType}() @@ -341,12 +382,14 @@ function IndexCache(sys::AbstractSystem) disc_idxs, callback_to_clocks, tunable_idxs, + initials_idxs, const_idxs, nonnumeric_idxs, observed_syms_to_timeseries, dependent_pars_to_timeseries, disc_buffer_templates, BufferTemplate(Real, tunable_buffer_size), + BufferTemplate(Real, initials_buffer_size), const_buffer_sizes, nonnumeric_buffer_sizes, symbol_to_variable diff --git a/src/systems/nonlinear/initializesystem.jl b/src/systems/nonlinear/initializesystem.jl index 81f5af5ddc..ff203f12cb 100644 --- a/src/systems/nonlinear/initializesystem.jl +++ b/src/systems/nonlinear/initializesystem.jl @@ -538,6 +538,15 @@ function SciMLBase.late_binding_update_u0_p( return newu0, newp end +""" + $(TYPEDSIGNATURES) + +Check if the given system is an initialization system. +""" +function is_initializesystem(sys::AbstractSystem) + sys isa NonlinearSystem && get_metadata(sys) isa InitializationSystemMetadata +end + """ Counteracts the CSE/array variable hacks in `symbolics_tearing.jl` so it works with initialization. From b52ecc203fc78ff4c0b056d9787ea1a6227a7882 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 4 Mar 2025 14:17:47 +0530 Subject: [PATCH 1040/2176] feat: add `Initials` portion to `MTKParameters` --- src/systems/parameter_buffer.jl | 72 +++++++++++++++++++++++++++++---- 1 file changed, 64 insertions(+), 8 deletions(-) diff --git a/src/systems/parameter_buffer.jl b/src/systems/parameter_buffer.jl index 0954d9a40c..b9c6e0d382 100644 --- a/src/systems/parameter_buffer.jl +++ b/src/systems/parameter_buffer.jl @@ -3,8 +3,9 @@ symconvert(::Type{T}, x) where {T} = convert(T, x) symconvert(::Type{Real}, x::Integer) = convert(Float64, x) symconvert(::Type{V}, x) where {V <: AbstractArray} = convert(V, symconvert.(eltype(V), x)) -struct MTKParameters{T, D, C, N, H} +struct MTKParameters{T, I, D, C, N, H} tunable::T + initials::I discrete::D constant::C nonnumeric::N @@ -65,6 +66,8 @@ function MTKParameters( tunable_buffer = Vector{ic.tunable_buffer_size.type}( undef, ic.tunable_buffer_size.length) + initials_buffer = Vector{ic.initials_buffer_size.type}( + undef, ic.initials_buffer_size.length) disc_buffer = Tuple(BlockedArray( Vector{subbuffer_sizes[1].type}( undef, sum(x -> x.length, subbuffer_sizes)), @@ -79,6 +82,9 @@ function MTKParameters( if haskey(ic.tunable_idx, sym) idx = ic.tunable_idx[sym] tunable_buffer[idx] = val + elseif haskey(ic.initials_idx, sym) + idx = ic.initials_idx[sym] + initials_buffer[idx] = val elseif haskey(ic.discrete_idx, sym) idx = ic.discrete_idx[sym] disc_buffer[idx.buffer_idx][idx.idx_in_buffer] = val @@ -124,15 +130,19 @@ function MTKParameters( if isempty(tunable_buffer) tunable_buffer = SizedVector{0, Float64}() end + initials_buffer = narrow_buffer_type(initials_buffer) + if isempty(initials_buffer) + initials_buffer = SizedVector{0, Float64}() + end disc_buffer = narrow_buffer_type.(disc_buffer) const_buffer = narrow_buffer_type.(const_buffer) # Don't narrow nonnumeric types nonnumeric_buffer = nonnumeric_buffer mtkps = MTKParameters{ - typeof(tunable_buffer), typeof(disc_buffer), typeof(const_buffer), - typeof(nonnumeric_buffer), typeof(())}(tunable_buffer, - disc_buffer, const_buffer, nonnumeric_buffer, ()) + typeof(tunable_buffer), typeof(initials_buffer), typeof(disc_buffer), + typeof(const_buffer), typeof(nonnumeric_buffer), typeof(())}(tunable_buffer, + initials_buffer, disc_buffer, const_buffer, nonnumeric_buffer, ()) return mtkps end @@ -252,6 +262,26 @@ function SciMLStructures.replace!(::SciMLStructures.Tunable, p::MTKParameters, n return nothing end +function SciMLStructures.canonicalize(::SciMLStructures.Initials, p::MTKParameters) + arr = p.initials + repack = let p = p + function (new_val) + return SciMLStructures.replace(SciMLStructures.Initials(), p, new_val) + end + end + return arr, repack, true +end + +function SciMLStructures.replace(::SciMLStructures.Initials, p::MTKParameters, newvals) + @set! p.initials = newvals + return p +end + +function SciMLStructures.replace!(::SciMLStructures.Initials, p::MTKParameters, newvals) + copyto!(p.initials, newvals) + return nothing +end + for (Portion, field, recurse) in [(SciMLStructures.Discrete, :discrete, 1) (SciMLStructures.Constants, :constant, 1) (Nonnumeric, :nonnumeric, 1) @@ -279,12 +309,14 @@ end function Base.copy(p::MTKParameters) tunable = copy(p.tunable) + initials = copy(p.initials) discrete = Tuple(eltype(buf) <: Real ? copy(buf) : copy.(buf) for buf in p.discrete) constant = Tuple(eltype(buf) <: Real ? copy(buf) : copy.(buf) for buf in p.constant) nonnumeric = copy.(p.nonnumeric) caches = copy.(p.caches) return MTKParameters( tunable, + initials, discrete, constant, nonnumeric, @@ -300,6 +332,9 @@ function _ducktyped_parameter_values(p, pind::ParameterIndex) if portion isa SciMLStructures.Tunable return idx isa Int ? p.tunable[idx] : view(p.tunable, idx) end + if portion isa SciMLStructures.Initials + return idx isa Int ? p.initials[idx] : view(p.initials, idx) + end i, j, k... = idx if portion isa SciMLStructures.Discrete return isempty(k) ? p.discrete[i][j] : p.discrete[i][j][k...] @@ -320,6 +355,11 @@ function SymbolicIndexingInterface.set_parameter!( throw(InvalidParameterSizeException(size(idx), size(val))) end p.tunable[idx] = val + elseif portion isa SciMLStructures.Initials + if validate_size && size(val) !== size(idx) + throw(InvalidParameterSizeException(size(idx), size(val))) + end + p.initials[idx] = val else i, j, k... = idx if portion isa SciMLStructures.Discrete @@ -394,7 +434,8 @@ end function validate_parameter_type(ic::IndexCache, idx::ParameterIndex, val) stype = get_buffer_template(ic, idx).type - if idx.portion == SciMLStructures.Tunable() && !(idx.idx isa Int) + if (idx.portion == SciMLStructures.Tunable() || + idx.portion == SciMLStructures.Initials()) && !(idx.idx isa Int) stype = AbstractArray{<:stype} end validate_parameter_type( @@ -454,6 +495,7 @@ function SymbolicIndexingInterface.remake_buffer(indp, oldbuf::MTKParameters, id end function _remake_buffer(indp, oldbuf::MTKParameters, idxs, vals; validate = true) newbuf = @set oldbuf.tunable = similar(oldbuf.tunable, Any) + @set! newbuf.initials = similar(oldbuf.initials, Any) @set! newbuf.discrete = Tuple(similar(buf, Any) for buf in newbuf.discrete) @set! newbuf.constant = Tuple(similar(buf, Any) for buf in newbuf.constant) @set! newbuf.nonnumeric = Tuple(similar(buf, Any) for buf in newbuf.nonnumeric) @@ -529,6 +571,12 @@ function _remake_buffer(indp, oldbuf::MTKParameters, idxs, vals; validate = true T = promote_type(eltype(newbuf.tunable), Float64) @set! newbuf.tunable = T.(newbuf.tunable) end + @set! newbuf.initials = narrow_buffer_type_and_fallback_undefs( + oldbuf.initials, newbuf.initials) + if eltype(newbuf.initials) <: Integer + T = promote_type(eltype(newbuf.initials), Float64) + @set! newbuf.initials = T.(newbuf.initials) + end @set! newbuf.discrete = narrow_buffer_type_and_fallback_undefs.( oldbuf.discrete, newbuf.discrete) @set! newbuf.constant = narrow_buffer_type_and_fallback_undefs.( @@ -540,6 +588,7 @@ end function as_any_buffer(p::MTKParameters) @set! p.tunable = similar(p.tunable, Any) + @set! p.initials = similar(p.initials, Any) @set! p.discrete = Tuple(similar(buf, Any) for buf in p.discrete) @set! p.constant = Tuple(similar(buf, Any) for buf in p.constant) @set! p.nonnumeric = Tuple(similar(buf, Any) for buf in p.nonnumeric) @@ -615,11 +664,14 @@ end # getindex indexes the vectors, setindex! linearly indexes values # it's inconsistent, but we need it to be this way @generated function Base.getindex( - ps::MTKParameters{T, D, C, N, H}, idx::Int) where {T, D, C, N, H} + ps::MTKParameters{T, I, D, C, N, H}, idx::Int) where {T, I, D, C, N, H} paths = [] if !(T <: SizedVector{0, Float64}) push!(paths, :(ps.tunable)) end + if !(I <: SizedVector{0, Float64}) + push!(paths, :(ps.initials)) + end for i in 1:fieldcount(D) push!(paths, :(ps.discrete[$i])) end @@ -641,11 +693,15 @@ end return Expr(:block, expr, :(throw(BoundsError(ps, idx)))) end -@generated function Base.length(ps::MTKParameters{T, D, C, N, H}) where {T, D, C, N, H} +@generated function Base.length(ps::MTKParameters{ + T, I, D, C, N, H}) where {T, I, D, C, N, H} len = 0 if !(T <: SizedVector{0, Float64}) len += 1 end + if !(I <: SizedVector{0, Float64}) + len += 1 + end len += fieldcount(D) + fieldcount(C) + fieldcount(N) + fieldcount(H) return len end @@ -668,7 +724,7 @@ function Base.iterate(buf::MTKParameters, state = 1) end function Base.:(==)(a::MTKParameters, b::MTKParameters) - return a.tunable == b.tunable && a.discrete == b.discrete && + return a.tunable == b.tunable && a.initials == b.initials && a.discrete == b.discrete && a.constant == b.constant && a.nonnumeric == b.nonnumeric && all(Iterators.map(a.caches, b.caches) do acache, bcache eltype(acache) == eltype(bcache) && length(acache) == length(bcache) From e2251afbe1557e497e5a84a276701d80a645c70a Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 4 Mar 2025 14:17:53 +0530 Subject: [PATCH 1041/2176] refactor: remove dead code --- src/systems/parameter_buffer.jl | 6 ------ 1 file changed, 6 deletions(-) diff --git a/src/systems/parameter_buffer.jl b/src/systems/parameter_buffer.jl index b9c6e0d382..e132a5e596 100644 --- a/src/systems/parameter_buffer.jl +++ b/src/systems/parameter_buffer.jl @@ -760,12 +760,6 @@ function jacobian_wrt_vars(pf::F, p::MTKParameters, input_idxs, chunk::C) where ForwardDiff.jacobian(p_closure, p_small, cfg, Val(false)) end -function as_duals(p::MTKParameters, dualtype) - tunable = dualtype.(p.tunable) - discrete = dualtype.(p.discrete) - return MTKParameters{typeof(tunable), typeof(discrete)}(tunable, discrete) -end - const MISSING_PARAMETERS_MESSAGE = """ Some parameters are missing from the variable map. Please provide a value or default for the following variables: From 7f3b1e110774df31da108e469bca3d03951ced97 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 4 Mar 2025 14:18:17 +0530 Subject: [PATCH 1042/2176] fix: properly handle initial parameters in `complete` --- src/systems/abstractsystem.jl | 76 +++++++++++++++++++++-------------- 1 file changed, 46 insertions(+), 30 deletions(-) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index ddc70bb10a..8945d4022b 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -773,38 +773,21 @@ function complete(sys::AbstractSystem; split = true, flatten = true) if !isempty(all_ps) # reorder parameters by portions ps_split = reorder_parameters(sys, all_ps) + # if there are tunables, they will all be in `ps_split[1]` + # and the arrays will have been scalarized + ordered_ps = eltype(all_ps)[] # if there are no tunables, vcat them - if isempty(get_index_cache(sys).tunable_idx) - ordered_ps = reduce(vcat, ps_split) - else - # if there are tunables, they will all be in `ps_split[1]` - # and the arrays will have been scalarized - ordered_ps = eltype(all_ps)[] - i = 1 - # go through all the tunables - while i <= length(ps_split[1]) - sym = ps_split[1][i] - # if the sym is not a scalarized array symbolic OR it was already scalarized, - # just push it as-is - if !iscall(sym) || operation(sym) != getindex || - any(isequal(sym), all_ps) - push!(ordered_ps, sym) - i += 1 - continue - end - # the next `length(sym)` symbols should be scalarized versions of the same - # array symbolic - if !allequal(first(arguments(x)) - for x in view(ps_split[1], i:(i + length(sym) - 1))) - error("This should not be possible. Please open an issue in ModelingToolkit.jl with an MWE and stacktrace.") - end - arrsym = first(arguments(sym)) - push!(ordered_ps, arrsym) - i += length(arrsym) - end - ordered_ps = vcat( - ordered_ps, reduce(vcat, ps_split[2:end]; init = eltype(ordered_ps)[])) + if !isempty(get_index_cache(sys).tunable_idx) + unflatten_parameters!(ordered_ps, ps_split[1], all_ps) + ps_split = Base.tail(ps_split) end + # unflatten initial parameters + if !isempty(get_index_cache(sys).initials_idx) + unflatten_parameters!(ordered_ps, ps_split[1], all_ps) + ps_split = Base.tail(ps_split) + end + ordered_ps = vcat( + ordered_ps, reduce(vcat, ps_split; init = eltype(ordered_ps)[])) @set! sys.ps = ordered_ps end elseif has_index_cache(sys) @@ -816,6 +799,39 @@ function complete(sys::AbstractSystem; split = true, flatten = true) isdefined(sys, :complete) ? (@set! sys.complete = true) : sys end +""" + $(TYPEDSIGNATURES) + +Given a flattened array of parameters `params` and a collection of all (unscalarized) +parameters in the system `all_ps`, unscalarize the elements in `params` and append +to `buffer` in the same order as they are present in `params`. Effectively, if +`params = [p[1], p[2], p[3], q]` then this is equivalent to `push!(buffer, p, q)`. +""" +function unflatten_parameters!(buffer, params, all_ps) + i = 1 + # go through all the tunables + while i <= length(params) + sym = params[i] + # if the sym is not a scalarized array symbolic OR it was already scalarized, + # just push it as-is + if !iscall(sym) || operation(sym) != getindex || + any(isequal(sym), all_ps) + push!(buffer, sym) + i += 1 + continue + end + # the next `length(sym)` symbols should be scalarized versions of the same + # array symbolic + if !allequal(first(arguments(x)) + for x in view(params, i:(i + length(sym) - 1))) + error("This should not be possible. Please open an issue in ModelingToolkit.jl with an MWE and stacktrace.") + end + arrsym = first(arguments(sym)) + push!(buffer, arrsym) + i += length(arrsym) + end +end + for prop in [:eqs :tag :noiseeqs From 73fb137235a6c9d456fed5eab8f96eb42cebf328 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 4 Mar 2025 14:18:48 +0530 Subject: [PATCH 1043/2176] fix: handle initial parameters in symbolic indexing --- src/systems/index_cache.jl | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/systems/index_cache.jl b/src/systems/index_cache.jl index 5e6867edb2..c3822fc868 100644 --- a/src/systems/index_cache.jl +++ b/src/systems/index_cache.jl @@ -428,6 +428,8 @@ function SymbolicIndexingInterface.parameter_index(ic::IndexCache, sym) Symbolics.shape(sym) !== Symbolics.Unknown() return if (idx = check_index_map(ic.tunable_idx, sym)) !== nothing ParameterIndex(SciMLStructures.Tunable(), idx, validate_size) + elseif (idx = check_index_map(ic.initials_idx, sym)) !== nothing + ParameterIndex(SciMLStructures.Initials(), idx, validate_size) elseif (idx = check_index_map(ic.discrete_idx, sym)) !== nothing ParameterIndex( SciMLStructures.Discrete(), (idx.buffer_idx, idx.idx_in_buffer), validate_size) From e56346607c42b118fff263c136eea20571afc868 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 4 Mar 2025 14:19:37 +0530 Subject: [PATCH 1044/2176] fix: handle initial parameters in codegen --- src/systems/index_cache.jl | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/src/systems/index_cache.jl b/src/systems/index_cache.jl index c3822fc868..47a784c00b 100644 --- a/src/systems/index_cache.jl +++ b/src/systems/index_cache.jl @@ -510,6 +510,12 @@ function reorder_parameters(ic::IndexCache, ps; drop_missing = false) (BasicSymbolic[unwrap(variable(:DEF)) for _ in 1:(ic.tunable_buffer_size.length)],) end + initials_buf = if ic.initials_buffer_size.length == 0 + () + else + (BasicSymbolic[unwrap(variable(:DEF)) + for _ in 1:(ic.initials_buffer_size.length)],) + end disc_buf = Tuple(BasicSymbolic[unwrap(variable(:DEF)) for _ in 1:(sum(x -> x.length, temp))] @@ -531,6 +537,13 @@ function reorder_parameters(ic::IndexCache, ps; drop_missing = false) else param_buf[1][i] = unwrap.(collect(p)) end + elseif haskey(ic.initials_idx, p) + i = ic.initials_idx[p] + if i isa Int + initials_buf[1][i] = unwrap(p) + else + initials_buf[1][i] = unwrap.(collect(p)) + end elseif haskey(ic.constant_idx, p) i, j = ic.constant_idx[p] const_buf[i][j] = p @@ -543,7 +556,8 @@ function reorder_parameters(ic::IndexCache, ps; drop_missing = false) end result = broadcast.( - unwrap, (param_buf..., disc_buf..., const_buf..., nonnumeric_buf...)) + unwrap, ( + param_buf..., initials_buf..., disc_buf..., const_buf..., nonnumeric_buf...)) if drop_missing result = map(result) do buf filter(buf) do sym @@ -566,6 +580,11 @@ function iterated_buffer_index(ic::IndexCache, ind::ParameterIndex) elseif ic.tunable_buffer_size.length > 0 idx += 1 end + if ind.portion isa SciMLStructures.Initials + return idx + 1 + elseif ic.initials_buffer_size.length > 0 + idx += 1 + end if ind.portion isa SciMLStructures.Discrete return idx + ind.idx[1] elseif !isempty(ic.discrete_buffer_sizes) @@ -587,6 +606,8 @@ function get_buffer_template(ic::IndexCache, pidx::ParameterIndex) if portion isa SciMLStructures.Tunable return ic.tunable_buffer_size + elseif portion isa SciMLStructures.Initials + return ic.initials_buffer_size elseif portion isa SciMLStructures.Discrete return ic.discrete_buffer_sizes[idx[1]][1] elseif portion isa SciMLStructures.Constants From 793abb5b81ea78a607beedcaba23c46405425714 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 4 Mar 2025 14:20:19 +0530 Subject: [PATCH 1045/2176] fix: fix calling `MTKParameters` functions with just tunables vector --- src/systems/codegen_utils.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/systems/codegen_utils.jl b/src/systems/codegen_utils.jl index 4dce62827d..1eeb7e026b 100644 --- a/src/systems/codegen_utils.jl +++ b/src/systems/codegen_utils.jl @@ -287,7 +287,7 @@ end # The user provided a single buffer/tuple for the parameter object, so wrap that # one in a tuple fargs = ntuple(Val(length(args))) do i - i == paramidx ? :((args[$i],)) : :(args[$i]) + i == paramidx ? :((args[$i], nothing)) : :(args[$i]) end return :($f($(fargs...))) end From ef5326b3ae25061164073f8bcea35a5cdf9f3ecb Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 4 Mar 2025 14:20:47 +0530 Subject: [PATCH 1046/2176] fix: promote `Initials` portion in `ReconstructInitializeprob` --- src/systems/nonlinear/initializesystem.jl | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/systems/nonlinear/initializesystem.jl b/src/systems/nonlinear/initializesystem.jl index ff203f12cb..13e5a61da1 100644 --- a/src/systems/nonlinear/initializesystem.jl +++ b/src/systems/nonlinear/initializesystem.jl @@ -338,7 +338,15 @@ function (rip::ReconstructInitializeprob)(srcvalp, dstvalp) end buf, repack, alias = SciMLStructures.canonicalize(SciMLStructures.Tunable(), newp) if eltype(buf) != T - newp = repack(T.(buf)) + newbuf = similar(buf, T) + copyto!(newbuf, buf) + newp = repack(newbuf) + end + buf, repack, alias = SciMLStructures.canonicalize(SciMLStructures.Initials(), newp) + if eltype(buf) != T + newbuf = similar(buf, T) + copyto!(newbuf, buf) + newp = repack(newbuf) end return u0, newp end From 0c9f032c8054a47d840225671113bda4f2d83d84 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 4 Mar 2025 14:21:03 +0530 Subject: [PATCH 1047/2176] fix: promote `Initials` portion in `late_binding_update_u0_p` --- src/systems/nonlinear/initializesystem.jl | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/systems/nonlinear/initializesystem.jl b/src/systems/nonlinear/initializesystem.jl index 13e5a61da1..0f4016be44 100644 --- a/src/systems/nonlinear/initializesystem.jl +++ b/src/systems/nonlinear/initializesystem.jl @@ -528,6 +528,9 @@ function SciMLBase.late_binding_update_u0_p( tunables, repack, alias = SciMLStructures.canonicalize(SciMLStructures.Tunable(), newp) tunables = DiffEqBase.promote_u0(tunables, newu0, t0) newp = repack(tunables) + initials, repack, alias = SciMLStructures.canonicalize(SciMLStructures.Initials(), newp) + initials = DiffEqBase.promote_u0(initials, newu0, t0) + newp = repack(initials) allsyms = all_symbols(sys) for (k, v) in u0 From d830ab26f58a4c2b137bd58f16df788dea150933 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 4 Mar 2025 14:21:19 +0530 Subject: [PATCH 1048/2176] fix: simplify `modelingtoolkitize` --- src/systems/diffeqs/modelingtoolkitize.jl | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/systems/diffeqs/modelingtoolkitize.jl b/src/systems/diffeqs/modelingtoolkitize.jl index e7321c4d2b..b2954f81e4 100644 --- a/src/systems/diffeqs/modelingtoolkitize.jl +++ b/src/systems/diffeqs/modelingtoolkitize.jl @@ -62,9 +62,9 @@ function modelingtoolkitize( fill!(rhs, 0) if prob.f isa ODEFunction && prob.f.f isa FunctionWrappersWrappers.FunctionWrappersWrapper - prob.f.f.fw[1].obj[](rhs, vars, p isa MTKParameters ? (params,) : params, t) + prob.f.f.fw[1].obj[](rhs, vars, params, t) else - prob.f(rhs, vars, p isa MTKParameters ? (params,) : params, t) + prob.f(rhs, vars, params, t) end else rhs = prob.f(vars, params, t) @@ -255,14 +255,14 @@ function modelingtoolkitize(prob::DiffEqBase.SDEProblem; kwargs...) if DiffEqBase.isinplace(prob) lhs = similar(vars, Any) - prob.f(lhs, vars, p isa MTKParameters ? (params,) : params, t) + prob.f(lhs, vars, params, t) if DiffEqBase.is_diagonal_noise(prob) neqs = similar(vars, Any) - prob.g(neqs, vars, p isa MTKParameters ? (params,) : params, t) + prob.g(neqs, vars, params, t) else neqs = similar(vars, Any, size(prob.noise_rate_prototype)) - prob.g(neqs, vars, p isa MTKParameters ? (params,) : params, t) + prob.g(neqs, vars, params, t) end else lhs = prob.f(vars, params, t) From d7da992122f1487875098983f2310e3c144e5d1e Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 4 Mar 2025 14:21:38 +0530 Subject: [PATCH 1049/2176] fix: return `GeneratedFunctionWrapper` from `generate_control_function` --- src/inputoutput.jl | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/inputoutput.jl b/src/inputoutput.jl index 2407ca6942..5f9420ff3a 100644 --- a/src/inputoutput.jl +++ b/src/inputoutput.jl @@ -250,6 +250,9 @@ function generate_control_function(sys::AbstractODESystem, inputs = unbound_inpu f = build_function_wrapper(sys, rhss, args...; p_start = 3 + implicit_dae, p_end = length(p) + 2 + implicit_dae) f = eval_or_rgf.(f; eval_expression, eval_module) + f = GeneratedFunctionWrapper{( + 3 + implicit_dae, length(args) - length(p) + 1, is_split(sys))}(f...) + f = f, f ps = setdiff(parameters(sys), inputs, disturbance_inputs) (; f, dvs, ps, io_sys = sys) end From 43c3ccb3670d6d4ace9941dd48b6491256c8eec8 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 4 Mar 2025 14:22:03 +0530 Subject: [PATCH 1050/2176] fix: support initials in adjoints --- ext/MTKChainRulesCoreExt.jl | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/ext/MTKChainRulesCoreExt.jl b/ext/MTKChainRulesCoreExt.jl index 9cf17d203d..681603392d 100644 --- a/ext/MTKChainRulesCoreExt.jl +++ b/ext/MTKChainRulesCoreExt.jl @@ -80,6 +80,8 @@ function ChainRulesCore.rrule( newbuf = MTK.remake_buffer(indp, oldbuf, idxs, vals) tunable_idxs = reduce( vcat, (idx.idx for idx in idxs if idx.portion isa MTK.SciMLStructures.Tunable)) + initials_idxs = reduce( + vcat, (idx.idx for idx in idxs if idx.portion isa MTK.SciMLStructures.Initials)) disc_idxs = subset_idxs(idxs, MTK.SciMLStructures.Discrete(), oldbuf.discrete) const_idxs = subset_idxs(idxs, MTK.SciMLStructures.Constants(), oldbuf.constant) nn_idxs = subset_idxs(idxs, MTK.NONNUMERIC_PORTION, oldbuf.nonnumeric) @@ -91,10 +93,12 @@ function ChainRulesCore.rrule( indp′ = NoTangent() tunable = selected_tangents(buf′.tunable, tunable_idxs) + initials = selected_tangents(buf′.initials, initials_idxs) discrete = selected_tangents(buf′.discrete, disc_idxs) constant = selected_tangents(buf′.constant, const_idxs) nonnumeric = selected_tangents(buf′.nonnumeric, nn_idxs) - oldbuf′ = Tangent{typeof(oldbuf)}(; tunable, discrete, constant, nonnumeric) + oldbuf′ = Tangent{typeof(oldbuf)}(; + tunable, initials, discrete, constant, nonnumeric) idxs′ = NoTangent() vals′ = map(i -> MTK._ducktyped_parameter_values(buf′, i), idxs) return f′, indp′, oldbuf′, idxs′, vals′ From 5647deeaf3204798824e7c8c812bbf0d4a31880a Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 4 Mar 2025 14:22:17 +0530 Subject: [PATCH 1051/2176] test: mark test as not broken --- test/structural_transformation/tearing.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/structural_transformation/tearing.jl b/test/structural_transformation/tearing.jl index a0ccc7355d..e9cd92ec94 100644 --- a/test/structural_transformation/tearing.jl +++ b/test/structural_transformation/tearing.jl @@ -159,7 +159,7 @@ du = [0.0, 0.0]; u = [1.0, -0.5π]; pr = 0.2; tt = 0.1; -@test_skip (@ballocated $(prob.f)($du, $u, $pr, $tt)) == 0 +@test (@ballocated $(prob.f)($du, $u, $pr, $tt)) == 0 prob.f(du, u, pr, tt) @test du≈[u[2], u[1] + sin(u[2]) - pr * tt] atol=1e-5 From d6b3f27b20683c063c6d003ef2ed2db68fb732dc Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 4 Mar 2025 14:25:32 +0530 Subject: [PATCH 1052/2176] test: remove unnecessary wrapping of parameter vector --- test/function_registration.jl | 8 ++++---- test/input_output_handling.jl | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/test/function_registration.jl b/test/function_registration.jl index a52eb3245a..a1d9041127 100644 --- a/test/function_registration.jl +++ b/test/function_registration.jl @@ -22,7 +22,7 @@ sys = complete(sys) fun = ODEFunction(sys) u0 = 5.0 -@test fun([0.5], [u0], 0.0) == [do_something(u0) * 2] +@test fun([0.5], u0, 0.0) == [do_something(u0) * 2] end # TEST: Function registration in a nested module. @@ -45,7 +45,7 @@ sys = complete(sys) fun = ODEFunction(sys) u0 = 3.0 -@test fun([0.5], [u0], 0.0) == [do_something_2(u0) * 2] +@test fun([0.5], u0, 0.0) == [do_something_2(u0) * 2] end end @@ -67,7 +67,7 @@ sys = complete(sys) fun = ODEFunction(sys) u0 = 7.0 -@test fun([0.5], [u0], 0.0) == [do_something_3(u0) * 2] +@test fun([0.5], u0, 0.0) == [do_something_3(u0) * 2] # TEST: Function registration works with derivatives. # --------------------------------------------------- @@ -106,7 +106,7 @@ end function run_test() fun = build_ode() u0 = 10.0 - @test fun([0.5], [u0], 0.0) == [do_something_4(u0) * 2] + @test fun([0.5], u0, 0.0) == [do_something_4(u0) * 2] end run_test() diff --git a/test/input_output_handling.jl b/test/input_output_handling.jl index 4191571ed8..84539f0b37 100644 --- a/test/input_output_handling.jl +++ b/test/input_output_handling.jl @@ -145,9 +145,9 @@ if VERSION >= v"1.8" # :opaque_closure not supported before inputs = [torque.tau.u]) x = randn(size(A, 1)) u = randn(size(B, 2)) - p = (getindex.( + p = getindex.( Ref(ModelingToolkit.defaults_and_guesses(ssys)), - parameters(ssys)),) + parameters(ssys)) y1 = obsf(x, u, p, 0) y2 = C * x + D * u @test y1[] ≈ y2[] From 5186afd3adece0ffd5f12bbaf2e981578a6ce42e Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 4 Mar 2025 14:25:42 +0530 Subject: [PATCH 1053/2176] test: test promotion of initials portion in `remake` --- test/initializationsystem.jl | 1 + 1 file changed, 1 insertion(+) diff --git a/test/initializationsystem.jl b/test/initializationsystem.jl index 236f9e5ce3..4267f1d857 100644 --- a/test/initializationsystem.jl +++ b/test/initializationsystem.jl @@ -979,6 +979,7 @@ end ForwardDiff.Dual @test state_values(prob2.f.initialization_data.initializeprob) ≈ state_values(prob.f.initialization_data.initializeprob) + @test eltype(prob2.p.initials) <: ForwardDiff.Dual end end From 84f7309d6d573c79dfed22cc448674100b6ce49a Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 4 Mar 2025 14:26:16 +0530 Subject: [PATCH 1054/2176] test: remove splatting of parameter object only worked coincidentally --- test/odesystem.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/odesystem.jl b/test/odesystem.jl index b9a41e1c3d..87be5bbc54 100644 --- a/test/odesystem.jl +++ b/test/odesystem.jl @@ -1223,7 +1223,7 @@ end sys, [x + 1, x + P, x + t], return_inplace = true)[2] ps = ModelingToolkit.MTKParameters(sys, [P => 2.0]) buffer = zeros(3) - @test_nowarn obsfn(buffer, [1.0], ps..., 3.0) + @test_nowarn obsfn(buffer, [1.0], ps, 3.0) @test buffer ≈ [2.0, 3.0, 4.0] end From 25d52daba56d18e595763bbcba56d98292cdcfdd Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 4 Mar 2025 14:26:28 +0530 Subject: [PATCH 1055/2176] test: test indexing of `Initials` portion --- test/split_parameters.jl | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/test/split_parameters.jl b/test/split_parameters.jl index a38dd24c38..0a4f9fb25d 100644 --- a/test/split_parameters.jl +++ b/test/split_parameters.jl @@ -5,7 +5,7 @@ using DataInterpolations using BlockArrays: BlockedArray using ModelingToolkit: t_nounits as t, D_nounits as D using ModelingToolkit: MTKParameters, ParameterIndex, NONNUMERIC_PORTION -using SciMLStructures: Tunable, Discrete, Constants +using SciMLStructures: Tunable, Discrete, Constants, Initials using StaticArrays: SizedVector using SymbolicIndexingInterface: is_parameter, getp @@ -204,7 +204,7 @@ connections = [[state_feedback.input.u[i] ~ model_outputs[i] for i in 1:4] S = get_sensitivity(closed_loop, :u) @testset "Indexing MTKParameters with ParameterIndex" begin - ps = MTKParameters(collect(1.0:10.0), + ps = MTKParameters(collect(1.0:10.0), collect(11.0:20.0), (BlockedArray([true, false, false, true], [2, 2]), BlockedArray([[1 2; 3 4], [2 4; 6 8]], [1, 1])), # (BlockedArray([[true, false], [false, true]]), BlockedArray([[[1 2; 3 4]], [[2 4; 6 8]]])), @@ -213,6 +213,9 @@ S = get_sensitivity(closed_loop, :u) @test ps[ParameterIndex(Tunable(), 1)] == 1.0 @test ps[ParameterIndex(Tunable(), 2:4)] == collect(2.0:4.0) @test ps[ParameterIndex(Tunable(), reshape(4:7, 2, 2))] == reshape(4.0:7.0, 2, 2) + @test ps[ParameterIndex(Initials(), 1)] == 11.0 + @test ps[ParameterIndex(Initials(), 2:4)] == collect(12.0:14.0) + @test ps[ParameterIndex(Initials(), reshape(4:7, 2, 2))] == reshape(14.0:17.0, 2, 2) @test ps[ParameterIndex(Discrete(), (2, 1, 2, 2))] == 4 @test ps[ParameterIndex(Discrete(), (2, 2))] == [2 4; 6 8] @test ps[ParameterIndex(Constants(), (1, 1))] == 5 @@ -221,8 +224,12 @@ S = get_sensitivity(closed_loop, :u) ps[ParameterIndex(Tunable(), 1)] = 1.5 ps[ParameterIndex(Tunable(), 2:4)] = [2.5, 3.5, 4.5] ps[ParameterIndex(Tunable(), reshape(5:8, 2, 2))] = [5.5 7.5; 6.5 8.5] + ps[ParameterIndex(Initials(), 1)] = 11.5 + ps[ParameterIndex(Initials(), 2:4)] = [12.5, 13.5, 14.5] + ps[ParameterIndex(Initials(), reshape(5:8, 2, 2))] = [15.5 17.5; 16.5 18.5] ps[ParameterIndex(Discrete(), (2, 1, 2, 2))] = 5 @test ps[ParameterIndex(Tunable(), 1:8)] == collect(1.0:8.0) .+ 0.5 + @test ps[ParameterIndex(Initials(), 1:8)] == collect(11.0:18.0) .+ 0.5 @test ps[ParameterIndex(Discrete(), (2, 1, 2, 2))] == 5 end From ef75b3352d20aa5ee56f93edfd867c0ee9909474 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 4 Mar 2025 17:22:27 +0530 Subject: [PATCH 1056/2176] feat: allow `NonlinearSystem(::ODESystem)` and `NonlinearProblem(::ODESystem)` --- src/systems/nonlinear/nonlinearsystem.jl | 30 ++++++++++++++ test/nonlinearsystem.jl | 50 ++++++++++++++++++++++++ 2 files changed, 80 insertions(+) diff --git a/src/systems/nonlinear/nonlinearsystem.jl b/src/systems/nonlinear/nonlinearsystem.jl index f93e8a5838..523fee28fc 100644 --- a/src/systems/nonlinear/nonlinearsystem.jl +++ b/src/systems/nonlinear/nonlinearsystem.jl @@ -232,6 +232,32 @@ function NonlinearSystem(eqs; kwargs...) return NonlinearSystem(eqs, collect(allunknowns), collect(new_ps); kwargs...) end +""" + $(TYPEDSIGNATURES) + +Convert an `ODESystem` to a `NonlinearSystem` solving for its steady state (where derivatives are zero). +Any differential variable `D(x) ~ f(...)` will be turned into `0 ~ f(...)`. The returned system is not +simplified. If the input system is `complete`d, then so will the returned system. +""" +function NonlinearSystem(sys::ODESystem) + eqs = equations(sys) + obs = observed(sys) + subrules = Dict(D(x) => 0.0 for x in unknowns(sys)) + eqs = map(eqs) do eq + fast_substitute(eq, subrules) + end + + nsys = NonlinearSystem(eqs, unknowns(sys), [parameters(sys); get_iv(sys)]; + parameter_dependencies = parameter_dependencies(sys), + defaults = merge(defaults(sys), Dict(get_iv(sys) => Inf)), guesses = guesses(sys), + initialization_eqs = initialization_equations(sys), name = nameof(sys), + observed = obs) + if iscomplete(sys) + nsys = complete(nsys; split = is_split(sys)) + end + return nsys +end + function calculate_jacobian(sys::NonlinearSystem; sparse = false, simplify = false) cache = get_jac(sys)[] if cache isa Tuple && cache[2] == (sparse, simplify) @@ -529,6 +555,10 @@ function DiffEqBase.NonlinearProblem{iip}(sys::NonlinearSystem, u0map, return remake(NonlinearProblem{iip}(f, u0, p, pt; filter_kwargs(kwargs)...)) end +function DiffEqBase.NonlinearProblem(sys::ODESystem, args...; kwargs...) + NonlinearProblem(NonlinearSystem(sys), args...; kwargs...) +end + """ ```julia DiffEqBase.NonlinearLeastSquaresProblem{iip}(sys::NonlinearSystem, u0map, diff --git a/test/nonlinearsystem.jl b/test/nonlinearsystem.jl index c8dda530e2..cde0be7373 100644 --- a/test/nonlinearsystem.jl +++ b/test/nonlinearsystem.jl @@ -390,3 +390,53 @@ end @test !any(isequal(p[1]), parameters(sys)) @test is_parameter(sys, p) end + +@testset "Can convert from `ODESystem`" begin + @variables x(t) y(t) + @parameters p q r + @named sys = ODESystem([D(x) ~ p * x^3 + q, 0 ~ -y + q * x - r], t; + defaults = [x => 1.0, p => missing], guesses = [p => 1.0], + initialization_eqs = [p^3 + q^3 ~ 4r], parameter_dependencies = [r ~ 3p]) + nlsys = NonlinearSystem(sys) + defs = defaults(nlsys) + @test length(defs) == 3 + @test defs[x] == 1.0 + @test defs[p] === missing + @test isinf(defs[t]) + @test length(guesses(nlsys)) == 1 + @test guesses(nlsys)[p] == 1.0 + @test length(initialization_equations(nlsys)) == 1 + @test length(parameter_dependencies(nlsys)) == 1 + @test length(equations(nlsys)) == 2 + @test all(iszero, [eq.lhs for eq in equations(nlsys)]) + @test nameof(nlsys) == nameof(sys) + @test !ModelingToolkit.iscomplete(nlsys) + + sys1 = complete(sys; split = false) + nlsys = NonlinearSystem(sys1) + @test ModelingToolkit.iscomplete(nlsys) + @test !ModelingToolkit.is_split(nlsys) + + sys2 = complete(sys) + nlsys = NonlinearSystem(sys2) + @test ModelingToolkit.iscomplete(nlsys) + @test ModelingToolkit.is_split(nlsys) + + sys3 = structural_simplify(sys) + nlsys = NonlinearSystem(sys3) + @test length(equations(nlsys)) == length(observed(nlsys)) == 1 + + prob = NonlinearProblem(sys3, [q => 2.0]) + @test prob.f.initialization_data.initializeprobmap === nothing + sol = solve(prob) + @test SciMLBase.successful_retcode(sol) + @test sol.ps[p^3 + q^3]≈sol.ps[4r] atol=1e-10 + + @testset "Differential inside expression also substituted" begin + @named sys = ODESystem([0 ~ y * D(x) + x^2 - p, 0 ~ x * D(y) + y * p], t) + nlsys = NonlinearSystem(sys) + vs = ModelingToolkit.vars(equations(nlsys)) + @test !in(D(x), vs) + @test !in(D(y), vs) + end +end From ee84d23969f1eea39add5bed3feba6e9d74faec8 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 4 Mar 2025 18:11:03 +0530 Subject: [PATCH 1057/2176] fix: dispatch on `AbstractODESystem` to avoid use-before-define problems --- src/systems/nonlinear/nonlinearsystem.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/systems/nonlinear/nonlinearsystem.jl b/src/systems/nonlinear/nonlinearsystem.jl index 523fee28fc..ea3730fdb6 100644 --- a/src/systems/nonlinear/nonlinearsystem.jl +++ b/src/systems/nonlinear/nonlinearsystem.jl @@ -239,7 +239,7 @@ Convert an `ODESystem` to a `NonlinearSystem` solving for its steady state (wher Any differential variable `D(x) ~ f(...)` will be turned into `0 ~ f(...)`. The returned system is not simplified. If the input system is `complete`d, then so will the returned system. """ -function NonlinearSystem(sys::ODESystem) +function NonlinearSystem(sys::AbstractODESystem) eqs = equations(sys) obs = observed(sys) subrules = Dict(D(x) => 0.0 for x in unknowns(sys)) @@ -555,7 +555,7 @@ function DiffEqBase.NonlinearProblem{iip}(sys::NonlinearSystem, u0map, return remake(NonlinearProblem{iip}(f, u0, p, pt; filter_kwargs(kwargs)...)) end -function DiffEqBase.NonlinearProblem(sys::ODESystem, args...; kwargs...) +function DiffEqBase.NonlinearProblem(sys::AbstractODESystem, args...; kwargs...) NonlinearProblem(NonlinearSystem(sys), args...; kwargs...) end From 82b9b40371aedb6165fb7d88de080a6631f7b181 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 5 Mar 2025 11:29:25 +0530 Subject: [PATCH 1058/2176] fix: disambiguate `observed` --- test/nonlinearsystem.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/nonlinearsystem.jl b/test/nonlinearsystem.jl index cde0be7373..420bdc088e 100644 --- a/test/nonlinearsystem.jl +++ b/test/nonlinearsystem.jl @@ -424,7 +424,7 @@ end sys3 = structural_simplify(sys) nlsys = NonlinearSystem(sys3) - @test length(equations(nlsys)) == length(observed(nlsys)) == 1 + @test length(equations(nlsys)) == length(ModelingToolkit.observed(nlsys)) == 1 prob = NonlinearProblem(sys3, [q => 2.0]) @test prob.f.initialization_data.initializeprobmap === nothing From 6686524573989d1b5fe4fc6ed43668f444337aad Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 5 Mar 2025 11:33:48 +0530 Subject: [PATCH 1059/2176] test: update mtkparameters tests with new portion --- test/mtkparameters.jl | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/test/mtkparameters.jl b/test/mtkparameters.jl index 3ac8f7569d..f9d71f00bc 100644 --- a/test/mtkparameters.jl +++ b/test/mtkparameters.jl @@ -40,7 +40,7 @@ setp(sys, a)(ps, 1.0) @test getp(sys, a)(ps) == getp(sys, b)(ps) / 2 == getp(sys, c)(ps) / 3 == 1.0 -for (portion, values) in [(Tunable(), [1.0, 2.0, 5.0, 6.0, 7.0]) +for (portion, values) in [(Tunable(), [1.0, 5.0, 6.0, 7.0]) (Discrete(), [3.0]) (Constants(), vcat([0.1, 0.2, 0.3], ones(9), [4.0]))] buffer, repack, alias = canonicalize(portion, ps) @@ -109,7 +109,7 @@ eq = D(X) ~ p[1] - p[2] * X u0 = [X => 1.0] ps = [p => [2.0, 0.1]] p = MTKParameters(osys, ps, u0) -@test p.tunable == [2.0, 0.1, 1.0] +@test p.tunable == [2.0, 0.1] # Ensure partial update promotes the buffer @parameters p q r @@ -298,7 +298,7 @@ end end # Parameter timeseries -ps = MTKParameters(([1.0, 1.0],), (BlockedArray(zeros(4), [2, 2]),), +ps = MTKParameters([1.0, 1.0], (), (BlockedArray(zeros(4), [2, 2]),), (), (), ()) ps2 = SciMLStructures.replace(Discrete(), ps, ones(4)) @test typeof(ps2.discrete) == typeof(ps.discrete) @@ -314,7 +314,8 @@ with_updated_parameter_timeseries_values( # With multiple types and clocks ps = MTKParameters( - (), (BlockedArray([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], [3, 3]), + (), (), + (BlockedArray([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], [3, 3]), BlockedArray(falses(1), [1, 0])), (), (), ()) @test SciMLBase.get_saveable_values(sys, ps, 1).x isa Tuple{Vector{Float64}, Vector{Bool}} From 66ec1138ed0b8acb4c8ea8655a47c84f3d0501e8 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 5 Mar 2025 11:35:47 +0530 Subject: [PATCH 1060/2176] fix: handle empty `reduce` in rrule --- ext/MTKChainRulesCoreExt.jl | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/ext/MTKChainRulesCoreExt.jl b/ext/MTKChainRulesCoreExt.jl index 681603392d..a2974ea2dd 100644 --- a/ext/MTKChainRulesCoreExt.jl +++ b/ext/MTKChainRulesCoreExt.jl @@ -79,9 +79,11 @@ function ChainRulesCore.rrule( end newbuf = MTK.remake_buffer(indp, oldbuf, idxs, vals) tunable_idxs = reduce( - vcat, (idx.idx for idx in idxs if idx.portion isa MTK.SciMLStructures.Tunable)) + vcat, (idx.idx for idx in idxs if idx.portion isa MTK.SciMLStructures.Tunable); + init = Union{Int, AbstractVector{Int}}[]) initials_idxs = reduce( - vcat, (idx.idx for idx in idxs if idx.portion isa MTK.SciMLStructures.Initials)) + vcat, (idx.idx for idx in idxs if idx.portion isa MTK.SciMLStructures.Initials); + init = Union{Int, AbstractVector{Int}}[]) disc_idxs = subset_idxs(idxs, MTK.SciMLStructures.Discrete(), oldbuf.discrete) const_idxs = subset_idxs(idxs, MTK.SciMLStructures.Constants(), oldbuf.constant) nn_idxs = subset_idxs(idxs, MTK.NONNUMERIC_PORTION, oldbuf.nonnumeric) From bde633c2d16169ccd6b0b28e28beda4b5466a401 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 5 Mar 2025 11:44:38 +0530 Subject: [PATCH 1061/2176] build: bump SciMLStructures compat Co-authored-by: Christopher Rackauckas --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 6941a5f4d8..22ec0f80eb 100644 --- a/Project.toml +++ b/Project.toml @@ -136,7 +136,7 @@ Reexport = "0.2, 1" RuntimeGeneratedFunctions = "0.5.9" SCCNonlinearSolve = "1.0.0" SciMLBase = "2.75" -SciMLStructures = "1.0" +SciMLStructures = "1.7" Serialization = "1" Setfield = "0.7, 0.8, 1" SimpleNonlinearSolve = "0.1.0, 1, 2" From a553fd16cbe236b5f4c49fe8fb559d11bd10afa8 Mon Sep 17 00:00:00 2001 From: Christopher Rackauckas Date: Wed, 5 Mar 2025 08:20:37 -0500 Subject: [PATCH 1062/2176] Update Project.toml --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 22ec0f80eb..ad5b7f9d28 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "ModelingToolkit" uuid = "961ee093-0014-501f-94e3-6117800e7a78" authors = ["Yingbo Ma ", "Chris Rackauckas and contributors"] -version = "9.64.3" +version = "9.65.0" [deps] AbstractTrees = "1520ce14-60c1-5f80-bbc7-55ef81b5835c" From acec3cb7e2ae3037d239a4e09ef57d73e6a72559 Mon Sep 17 00:00:00 2001 From: Herman Sletmoen Date: Sun, 23 Feb 2025 17:35:10 +0100 Subject: [PATCH 1063/2176] Change independent variable of simple 1st order system --- src/systems/diffeqs/basic_transformations.jl | 31 ++++++++++++++++++++ test/basic_transformations.jl | 25 ++++++++++++++++ 2 files changed, 56 insertions(+) diff --git a/src/systems/diffeqs/basic_transformations.jl b/src/systems/diffeqs/basic_transformations.jl index e2be889c5e..7bf348911b 100644 --- a/src/systems/diffeqs/basic_transformations.jl +++ b/src/systems/diffeqs/basic_transformations.jl @@ -55,3 +55,34 @@ function liouville_transform(sys::AbstractODESystem) vars = [unknowns(sys); trJ] ODESystem(neweqs, t, vars, parameters(sys), checks = false) end + +# TODO: handle case when new iv is a variable already in the system +function change_independent_variable(sys::AbstractODESystem, iv, iv1_of_iv2; kwargs...) + iv1 = ModelingToolkit.get_iv(sys) # old independent variable + iv2 = iv # new independent variable + + name2 = nameof(iv2) + iv2func, = @variables $name2(iv1) + + eqs = ModelingToolkit.get_eqs(sys) |> copy # don't modify original system + vars = [] + for (i, eq) in enumerate(eqs) + vars = Symbolics.get_variables(eq) + for var1 in vars + if Symbolics.iscall(var1) # skip e.g. constants + name = nameof(operation(var1)) + var2, = @variables $name(iv2func) + eq = substitute(eq, var1 => var2; fold = false) + end + end + eq = expand_derivatives(eq) # expand out with chain rule to get d(iv2)/d(iv1) + div1_div2 = Differential(iv2)(iv1_of_iv2) |> expand_derivatives + div2_div1 = 1 / div1_div2 + eq = substitute(eq, Differential(iv1)(iv2func) => div2_div1) # substitute in d(iv1)/d(iv2) + eq = substitute(eq, iv2func => iv2) # make iv2 independent + eqs[i] = eq + end + + sys2 = typeof(sys)(eqs, iv2; name = nameof(sys), description = description(sys), kwargs...) + return sys2 +end diff --git a/test/basic_transformations.jl b/test/basic_transformations.jl index d9b59408e6..512510011c 100644 --- a/test/basic_transformations.jl +++ b/test/basic_transformations.jl @@ -32,3 +32,28 @@ u0 = [x => 1.0, prob = ODEProblem(sys2, u0, tspan, p, jac = true) sol = solve(prob, Tsit5()) @test sol[end, end] ≈ 1.0742818931017244 + +@testset "Change independent variable" begin + # Autonomous 1st order (t doesn't appear in the equations) + @independent_variables t + @variables x(t) y(t) z(t) + eqs = [ + D(x) ~ y + D(y) ~ 2*D(x) + z ~ x + D(y) + ] + @named sys1 = ODESystem(eqs, t) + + @independent_variables s + @named sys2 = ModelingToolkit.change_independent_variable(sys1, s, s^2) + + sys1 = structural_simplify(sys1) + sys2 = structural_simplify(sys2) + prob1 = ODEProblem(sys1, unknowns(sys1) .=> 1.0, (0.0, 1.0)) + prob2 = ODEProblem(sys2, unknowns(sys2) .=> 1.0, (0.0, 1.0)) + sol1 = solve(prob1, Tsit5(); reltol = 1e-10, abstol = 1e-10) + sol2 = solve(prob2, Tsit5(); reltol = 1e-10, abstol = 1e-10) + ts = range(0.0, 1.0, length = 50) + ss = .√(ts) + @test isapprox(sol1(ts), sol2(ss); atol = 1e-8) +end From 3600b0de65323f32ee6f47b422f810fc15ed1dc8 Mon Sep 17 00:00:00 2001 From: Herman Sletmoen Date: Sun, 23 Feb 2025 18:00:12 +0100 Subject: [PATCH 1064/2176] Change independent variable of 2nd-order systems --- src/systems/diffeqs/basic_transformations.jl | 8 ++++---- test/basic_transformations.jl | 11 ++++++----- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/src/systems/diffeqs/basic_transformations.jl b/src/systems/diffeqs/basic_transformations.jl index 7bf348911b..60eb46483e 100644 --- a/src/systems/diffeqs/basic_transformations.jl +++ b/src/systems/diffeqs/basic_transformations.jl @@ -57,7 +57,7 @@ function liouville_transform(sys::AbstractODESystem) end # TODO: handle case when new iv is a variable already in the system -function change_independent_variable(sys::AbstractODESystem, iv, iv1_of_iv2; kwargs...) +function change_independent_variable(sys::AbstractODESystem, iv, iv1_of_iv2, iv2_of_iv1; kwargs...) iv1 = ModelingToolkit.get_iv(sys) # old independent variable iv2 = iv # new independent variable @@ -76,10 +76,10 @@ function change_independent_variable(sys::AbstractODESystem, iv, iv1_of_iv2; kwa end end eq = expand_derivatives(eq) # expand out with chain rule to get d(iv2)/d(iv1) - div1_div2 = Differential(iv2)(iv1_of_iv2) |> expand_derivatives - div2_div1 = 1 / div1_div2 - eq = substitute(eq, Differential(iv1)(iv2func) => div2_div1) # substitute in d(iv1)/d(iv2) + eq = substitute(eq, Differential(iv1)(iv2func) => Differential(iv1)(iv2_of_iv1)) # substitute in d(iv2)/d(iv1) + eq = expand_derivatives(eq) eq = substitute(eq, iv2func => iv2) # make iv2 independent + eq = substitute(eq, iv1 => iv1_of_iv2) # substitute any remaining old ivars eqs[i] = eq end diff --git a/test/basic_transformations.jl b/test/basic_transformations.jl index 512510011c..efa799a0ee 100644 --- a/test/basic_transformations.jl +++ b/test/basic_transformations.jl @@ -39,21 +39,22 @@ sol = solve(prob, Tsit5()) @variables x(t) y(t) z(t) eqs = [ D(x) ~ y - D(y) ~ 2*D(x) + D(D(y)) ~ 2 * x * D(y) z ~ x + D(y) ] @named sys1 = ODESystem(eqs, t) @independent_variables s - @named sys2 = ModelingToolkit.change_independent_variable(sys1, s, s^2) + @named sys2 = ModelingToolkit.change_independent_variable(sys1, s, s^2, √(t)) sys1 = structural_simplify(sys1) sys2 = structural_simplify(sys2) - prob1 = ODEProblem(sys1, unknowns(sys1) .=> 1.0, (0.0, 1.0)) - prob2 = ODEProblem(sys2, unknowns(sys2) .=> 1.0, (0.0, 1.0)) + prob1 = ODEProblem(sys1, [sys1.x => 1.0, sys1.y => 1.0, Differential(t)(sys1.y) => 0.0], (1.0, 4.0)) + prob2 = ODEProblem(sys2, [sys2.x => 1.0, sys2.y => 1.0, Differential(s)(sys2.y) => 0.0], (1.0, 2.0)) sol1 = solve(prob1, Tsit5(); reltol = 1e-10, abstol = 1e-10) sol2 = solve(prob2, Tsit5(); reltol = 1e-10, abstol = 1e-10) ts = range(0.0, 1.0, length = 50) ss = .√(ts) - @test isapprox(sol1(ts), sol2(ss); atol = 1e-8) + @test all(isapprox.(sol1(ts, idxs=sys1.x), sol2(ss, idxs=sys2.x); atol = 1e-7)) && + all(isapprox.(sol1(ts, idxs=sys1.y), sol2(ss, idxs=sys2.y); atol = 1e-7)) end From acd0fabc7aea69e7702e98623f944a7171b85138 Mon Sep 17 00:00:00 2001 From: Herman Sletmoen Date: Sat, 1 Mar 2025 13:28:27 +0100 Subject: [PATCH 1065/2176] Change independent variable to a dependent one --- src/systems/diffeqs/basic_transformations.jl | 38 ++++++++++++++++++++ test/basic_transformations.jl | 21 +++++++++++ 2 files changed, 59 insertions(+) diff --git a/src/systems/diffeqs/basic_transformations.jl b/src/systems/diffeqs/basic_transformations.jl index 60eb46483e..01a103adab 100644 --- a/src/systems/diffeqs/basic_transformations.jl +++ b/src/systems/diffeqs/basic_transformations.jl @@ -86,3 +86,41 @@ function change_independent_variable(sys::AbstractODESystem, iv, iv1_of_iv2, iv2 sys2 = typeof(sys)(eqs, iv2; name = nameof(sys), description = description(sys), kwargs...) return sys2 end + +function change_independent_variable(sys::AbstractODESystem, iv; kwargs...) + iv1 = get_iv(sys) # e.g. t + iv2func = iv # e.g. a(t) + iv2name = nameof(operation(unwrap(iv))) # TODO: handle namespacing? + iv2, = @independent_variables $iv2name + + # TODO: find iv2func in system and replace it with some dummy variable + # TODO: not just to 1st, but to all orders + + div2name = Symbol(iv2name, :_t) + div2, = @variables $div2name(iv2) + + eqs = ModelingToolkit.get_eqs(sys) |> copy # don't modify original system + vars = [] + for (i, eq) in enumerate(eqs) + vars = Symbolics.get_variables(eq) + for var1 in vars + if Symbolics.iscall(var1) && !isequal(var1, iv2func) # && isequal(only(arguments(var1)), iv1) # skip e.g. constants + name = nameof(operation(var1)) + var2, = @variables $name(iv2func) + eq = substitute(eq, var1 => var2; fold = false) + end + end + eq = expand_derivatives(eq) # expand out with chain rule to get d(iv2)/d(iv1) + #eq = substitute(eq, Differential(iv1)(iv2func) => Differential(iv1)(iv2_of_iv1)) # substitute in d(iv2)/d(iv1) + #eq = expand_derivatives(eq) + eq = substitute(eq, Differential(iv1)(Differential(iv1)(iv2func)) => div2) # e.g. D(a(t)) => a_t(t) # TODO: more orders + eq = substitute(eq, Differential(iv1)(iv2func) => div2) # e.g. D(a(t)) => a_t(t) # TODO: more orders + eq = substitute(eq, iv2func => iv2) # make iv2 independent + #eq = substitute(eq, iv1 => iv1_of_iv2) # substitute any remaining old ivars + eqs[i] = eq + println(eq) + end + + sys2 = typeof(sys)(eqs, iv2; name = nameof(sys), description = description(sys), kwargs...) + return sys2 +end diff --git a/test/basic_transformations.jl b/test/basic_transformations.jl index efa799a0ee..3c7a4e6755 100644 --- a/test/basic_transformations.jl +++ b/test/basic_transformations.jl @@ -58,3 +58,24 @@ sol = solve(prob, Tsit5()) @test all(isapprox.(sol1(ts, idxs=sys1.x), sol2(ss, idxs=sys2.x); atol = 1e-7)) && all(isapprox.(sol1(ts, idxs=sys1.y), sol2(ss, idxs=sys2.y); atol = 1e-7)) end + +#@testset "Change independent variable (Friedmann equation)" begin + @independent_variables t + D = Differential(t) + @variables a(t) ρr(t) ρm(t) ρΛ(t) ρ(t) ϕ(t) + @parameters Ωr0 Ωm0 ΩΛ0 + eqs = [ + ρr ~ 3/(8*Num(π)) * Ωr0 / a^4 + ρm ~ 3/(8*Num(π)) * Ωm0 / a^3 + ρΛ ~ 3/(8*Num(π)) * ΩΛ0 + ρ ~ ρr + ρm + ρΛ + D(a) ~ √(8*Num(π)/3*ρ*a^4) + D(D(ϕ)) ~ -3*D(a)/a*D(ϕ) + ] + @named M1 = ODESystem(eqs, t) + M1 = complete(M1) + + M2 = ModelingToolkit.change_independent_variable(M1, M1.a) + + M2 = structural_simplify(M2; allow_symbolic = true) +#end From 865ef02623a9fcdb81b6227fa64a7aa4800a3744 Mon Sep 17 00:00:00 2001 From: Herman Sletmoen Date: Sat, 1 Mar 2025 23:45:22 +0100 Subject: [PATCH 1066/2176] Merge functions for changing independent variable into one --- src/systems/diffeqs/basic_transformations.jl | 83 +++++++++----------- test/basic_transformations.jl | 24 +++--- 2 files changed, 50 insertions(+), 57 deletions(-) diff --git a/src/systems/diffeqs/basic_transformations.jl b/src/systems/diffeqs/basic_transformations.jl index 01a103adab..841c0bda6d 100644 --- a/src/systems/diffeqs/basic_transformations.jl +++ b/src/systems/diffeqs/basic_transformations.jl @@ -56,52 +56,28 @@ function liouville_transform(sys::AbstractODESystem) ODESystem(neweqs, t, vars, parameters(sys), checks = false) end -# TODO: handle case when new iv is a variable already in the system -function change_independent_variable(sys::AbstractODESystem, iv, iv1_of_iv2, iv2_of_iv1; kwargs...) - iv1 = ModelingToolkit.get_iv(sys) # old independent variable - iv2 = iv # new independent variable - - name2 = nameof(iv2) - iv2func, = @variables $name2(iv1) - - eqs = ModelingToolkit.get_eqs(sys) |> copy # don't modify original system - vars = [] - for (i, eq) in enumerate(eqs) - vars = Symbolics.get_variables(eq) - for var1 in vars - if Symbolics.iscall(var1) # skip e.g. constants - name = nameof(operation(var1)) - var2, = @variables $name(iv2func) - eq = substitute(eq, var1 => var2; fold = false) - end - end - eq = expand_derivatives(eq) # expand out with chain rule to get d(iv2)/d(iv1) - eq = substitute(eq, Differential(iv1)(iv2func) => Differential(iv1)(iv2_of_iv1)) # substitute in d(iv2)/d(iv1) - eq = expand_derivatives(eq) - eq = substitute(eq, iv2func => iv2) # make iv2 independent - eq = substitute(eq, iv1 => iv1_of_iv2) # substitute any remaining old ivars - eqs[i] = eq - end - - sys2 = typeof(sys)(eqs, iv2; name = nameof(sys), description = description(sys), kwargs...) - return sys2 -end - -function change_independent_variable(sys::AbstractODESystem, iv; kwargs...) +function change_independent_variable(sys::AbstractODESystem, iv, eq = nothing; verbose = false, kwargs...) iv1 = get_iv(sys) # e.g. t - iv2func = iv # e.g. a(t) - iv2name = nameof(operation(unwrap(iv))) # TODO: handle namespacing? - iv2, = @independent_variables $iv2name - - # TODO: find iv2func in system and replace it with some dummy variable - # TODO: not just to 1st, but to all orders + iv2name = nameof(iv) # TODO: handle namespacing? + iv2, = @independent_variables $iv2name # e.g. a + iv2func, = @variables $iv2name(iv1) # e.g. a(t) + D1 = Differential(iv1) + D2 = Differential(iv2) div2name = Symbol(iv2name, :_t) - div2, = @variables $div2name(iv2) + div2, = @variables $div2name(iv2) # e.g. a_t(a) + + ddiv2name = Symbol(iv2name, :_tt) + ddiv2, = @variables $ddiv2name(iv2) # e.g. a_tt(a) eqs = ModelingToolkit.get_eqs(sys) |> copy # don't modify original system + !isnothing(eq) && push!(eqs, eq) vars = [] + div2_div1 = nothing for (i, eq) in enumerate(eqs) + verbose && println("1. ", eq) + + # 1) Substitute f(t₁) => f(t₂(t₁)) in all variables vars = Symbolics.get_variables(eq) for var1 in vars if Symbolics.iscall(var1) && !isequal(var1, iv2func) # && isequal(only(arguments(var1)), iv1) # skip e.g. constants @@ -110,17 +86,32 @@ function change_independent_variable(sys::AbstractODESystem, iv; kwargs...) eq = substitute(eq, var1 => var2; fold = false) end end + verbose && println("2. ", eq) + + # 2) Substitute in dummy variables for dⁿt₂/dt₁ⁿ eq = expand_derivatives(eq) # expand out with chain rule to get d(iv2)/d(iv1) - #eq = substitute(eq, Differential(iv1)(iv2func) => Differential(iv1)(iv2_of_iv1)) # substitute in d(iv2)/d(iv1) - #eq = expand_derivatives(eq) - eq = substitute(eq, Differential(iv1)(Differential(iv1)(iv2func)) => div2) # e.g. D(a(t)) => a_t(t) # TODO: more orders - eq = substitute(eq, Differential(iv1)(iv2func) => div2) # e.g. D(a(t)) => a_t(t) # TODO: more orders - eq = substitute(eq, iv2func => iv2) # make iv2 independent - #eq = substitute(eq, iv1 => iv1_of_iv2) # substitute any remaining old ivars + verbose && println("3. ", eq) + eq = substitute(eq, D1(D1(iv2func)) => ddiv2) # order 2 # TODO: more orders + eq = substitute(eq, D1(iv2func) => div2) # order 1; e.g. D(a(t)) => a_t(t) + eq = substitute(eq, iv2func => iv2) # order 0; make iv2 independent + verbose && println("4. ", eq) + verbose && println() + eqs[i] = eq - println(eq) + + if isequal(eq.lhs, div2) + div2_div1 = eq.rhs + end end + isnothing(div2_div1) && error("No equation for $D1($iv2func) was specified.") + verbose && println("Found $div2 = $div2_div1") + + # 3) Add equations for dummy variables + div1_div2 = 1 / div2_div1 + push!(eqs, ddiv2 ~ expand_derivatives(-D2(div1_div2) / div1_div2^3)) # e.g. https://math.stackexchange.com/questions/249253/second-derivative-of-the-inverse-function # TODO: higher orders + + # 4) Recreate system with new equations sys2 = typeof(sys)(eqs, iv2; name = nameof(sys), description = description(sys), kwargs...) return sys2 end diff --git a/test/basic_transformations.jl b/test/basic_transformations.jl index 3c7a4e6755..34f350222c 100644 --- a/test/basic_transformations.jl +++ b/test/basic_transformations.jl @@ -34,22 +34,23 @@ sol = solve(prob, Tsit5()) @test sol[end, end] ≈ 1.0742818931017244 @testset "Change independent variable" begin - # Autonomous 1st order (t doesn't appear in the equations) @independent_variables t - @variables x(t) y(t) z(t) + @variables x(t) y(t) z(t) s(t) eqs = [ D(x) ~ y D(D(y)) ~ 2 * x * D(y) z ~ x + D(y) + D(s) ~ 1 / (2*s) ] @named sys1 = ODESystem(eqs, t) + sys1 = complete(sys1) @independent_variables s - @named sys2 = ModelingToolkit.change_independent_variable(sys1, s, s^2, √(t)) + sys2 = ModelingToolkit.change_independent_variable(sys1, s) - sys1 = structural_simplify(sys1) - sys2 = structural_simplify(sys2) - prob1 = ODEProblem(sys1, [sys1.x => 1.0, sys1.y => 1.0, Differential(t)(sys1.y) => 0.0], (1.0, 4.0)) + sys1 = structural_simplify(sys1; allow_symbolic = true) + sys2 = structural_simplify(sys2; allow_symbolic = true) + prob1 = ODEProblem(sys1, [sys1.x => 1.0, sys1.y => 1.0, Differential(t)(sys1.y) => 0.0, sys1.s => 1.0], (1.0, 4.0)) prob2 = ODEProblem(sys2, [sys2.x => 1.0, sys2.y => 1.0, Differential(s)(sys2.y) => 0.0], (1.0, 2.0)) sol1 = solve(prob1, Tsit5(); reltol = 1e-10, abstol = 1e-10) sol2 = solve(prob2, Tsit5(); reltol = 1e-10, abstol = 1e-10) @@ -59,10 +60,10 @@ sol = solve(prob, Tsit5()) all(isapprox.(sol1(ts, idxs=sys1.y), sol2(ss, idxs=sys2.y); atol = 1e-7)) end -#@testset "Change independent variable (Friedmann equation)" begin +@testset "Change independent variable (Friedmann equation)" begin @independent_variables t D = Differential(t) - @variables a(t) ρr(t) ρm(t) ρΛ(t) ρ(t) ϕ(t) + @variables a(t) ρr(t) ρm(t) ρΛ(t) ρ(t) P(t) ϕ(t) @parameters Ωr0 Ωm0 ΩΛ0 eqs = [ ρr ~ 3/(8*Num(π)) * Ωr0 / a^4 @@ -75,7 +76,8 @@ end @named M1 = ODESystem(eqs, t) M1 = complete(M1) - M2 = ModelingToolkit.change_independent_variable(M1, M1.a) - + @independent_variables a + M2 = ModelingToolkit.change_independent_variable(M1, a) M2 = structural_simplify(M2; allow_symbolic = true) -#end + @test length(unknowns(M2)) == 2 +end From af3ae69d0a307822a2212462eccf63a843451ffb Mon Sep 17 00:00:00 2001 From: Herman Sletmoen Date: Sun, 2 Mar 2025 12:03:16 +0100 Subject: [PATCH 1067/2176] Fix broken Liouville transform test/documentation --- src/systems/diffeqs/basic_transformations.jl | 24 ++++------- test/basic_transformations.jl | 44 ++++++++------------ 2 files changed, 27 insertions(+), 41 deletions(-) diff --git a/src/systems/diffeqs/basic_transformations.jl b/src/systems/diffeqs/basic_transformations.jl index 841c0bda6d..4640aa9b5f 100644 --- a/src/systems/diffeqs/basic_transformations.jl +++ b/src/systems/diffeqs/basic_transformations.jl @@ -14,26 +14,20 @@ the final value of `trJ` is the probability of ``u(t)``. Example: ```julia -using ModelingToolkit, OrdinaryDiffEq, Test +using ModelingToolkit, OrdinaryDiffEq @independent_variables t @parameters α β γ δ @variables x(t) y(t) D = Differential(t) +eqs = [D(x) ~ α*x - β*x*y, D(y) ~ -δ*y + γ*x*y] +@named sys = ODESystem(eqs, t) -eqs = [D(x) ~ α*x - β*x*y, - D(y) ~ -δ*y + γ*x*y] - -sys = ODESystem(eqs) sys2 = liouville_transform(sys) -@variables trJ - -u0 = [x => 1.0, - y => 1.0, - trJ => 1.0] - -prob = ODEProblem(complete(sys2),u0,tspan,p) -sol = solve(prob,Tsit5()) +sys2 = complete(sys2) +u0 = [x => 1.0, y => 1.0, sys2.trJ => 1.0] +prob = ODEProblem(sys2, u0, tspan, p) +sol = solve(prob, Tsit5()) ``` Where `sol[3,:]` is the evolution of `trJ` over time. @@ -46,14 +40,14 @@ Optimal Transport Approach Abhishek Halder, Kooktae Lee, and Raktim Bhattacharya https://abhishekhalder.bitbucket.io/F16ACC2013Final.pdf """ -function liouville_transform(sys::AbstractODESystem) +function liouville_transform(sys::AbstractODESystem; kwargs...) t = get_iv(sys) @variables trJ D = ModelingToolkit.Differential(t) neweq = D(trJ) ~ trJ * -tr(calculate_jacobian(sys)) neweqs = [equations(sys); neweq] vars = [unknowns(sys); trJ] - ODESystem(neweqs, t, vars, parameters(sys), checks = false) + ODESystem(neweqs, t, vars, parameters(sys); checks = false, name = nameof(sys), kwargs...) end function change_independent_variable(sys::AbstractODESystem, iv, eq = nothing; verbose = false, kwargs...) diff --git a/test/basic_transformations.jl b/test/basic_transformations.jl index 34f350222c..2436dc7fc4 100644 --- a/test/basic_transformations.jl +++ b/test/basic_transformations.jl @@ -1,37 +1,29 @@ using ModelingToolkit, OrdinaryDiffEq, Test @independent_variables t -@parameters α β γ δ -@variables x(t) y(t) D = Differential(t) -eqs = [D(x) ~ α * x - β * x * y, - D(y) ~ -δ * y + γ * x * y] +@testset "Liouville transform" begin + @parameters α β γ δ + @variables x(t) y(t) + eqs = [D(x) ~ α * x - β * x * y, D(y) ~ -δ * y + γ * x * y] + @named sys = ODESystem(eqs, t) + sys = complete(sys) -sys = ODESystem(eqs) + u0 = [x => 1.0, y => 1.0] + p = [α => 1.5, β => 1.0, δ => 3.0, γ => 1.0] + tspan = (0.0, 10.0) + prob = ODEProblem(sys, u0, tspan, p) + sol = solve(prob, Tsit5()) -u0 = [x => 1.0, - y => 1.0] + sys2 = liouville_transform(sys) + sys2 = complete(sys2) -p = [α => 1.5, - β => 1.0, - δ => 3.0, - γ => 1.0] - -tspan = (0.0, 10.0) -prob = ODEProblem(sys, u0, tspan, p) -sol = solve(prob, Tsit5()) - -sys2 = liouville_transform(sys) -@variables trJ - -u0 = [x => 1.0, - y => 1.0, - trJ => 1.0] - -prob = ODEProblem(sys2, u0, tspan, p, jac = true) -sol = solve(prob, Tsit5()) -@test sol[end, end] ≈ 1.0742818931017244 + u0 = [x => 1.0, y => 1.0, sys2.trJ => 1.0] + prob = ODEProblem(sys2, u0, tspan, p, jac = true) + sol = solve(prob, Tsit5()) + @test sol[end, end] ≈ 1.0742818931017244 +end @testset "Change independent variable" begin @independent_variables t From cbceec481a050189015189edc56d4f879a112879 Mon Sep 17 00:00:00 2001 From: Herman Sletmoen Date: Sun, 2 Mar 2025 12:12:47 +0100 Subject: [PATCH 1068/2176] Specify new independent variable as a dependent variable in the old system --- src/systems/diffeqs/basic_transformations.jl | 2 +- test/basic_transformations.jl | 24 ++++++++------------ 2 files changed, 10 insertions(+), 16 deletions(-) diff --git a/src/systems/diffeqs/basic_transformations.jl b/src/systems/diffeqs/basic_transformations.jl index 4640aa9b5f..19c997e787 100644 --- a/src/systems/diffeqs/basic_transformations.jl +++ b/src/systems/diffeqs/basic_transformations.jl @@ -52,7 +52,7 @@ end function change_independent_variable(sys::AbstractODESystem, iv, eq = nothing; verbose = false, kwargs...) iv1 = get_iv(sys) # e.g. t - iv2name = nameof(iv) # TODO: handle namespacing? + iv2name = nameof(operation(unwrap(iv))) # TODO: handle namespacing? iv2, = @independent_variables $iv2name # e.g. a iv2func, = @variables $iv2name(iv1) # e.g. a(t) D1 = Differential(iv1) diff --git a/test/basic_transformations.jl b/test/basic_transformations.jl index 2436dc7fc4..55f1612fd3 100644 --- a/test/basic_transformations.jl +++ b/test/basic_transformations.jl @@ -26,7 +26,6 @@ D = Differential(t) end @testset "Change independent variable" begin - @independent_variables t @variables x(t) y(t) z(t) s(t) eqs = [ D(x) ~ y @@ -34,26 +33,22 @@ end z ~ x + D(y) D(s) ~ 1 / (2*s) ] - @named sys1 = ODESystem(eqs, t) - sys1 = complete(sys1) + M1 = ODESystem(eqs, t; name = :M) |> complete + M2 = ModelingToolkit.change_independent_variable(M1, M1.s) - @independent_variables s - sys2 = ModelingToolkit.change_independent_variable(sys1, s) - - sys1 = structural_simplify(sys1; allow_symbolic = true) - sys2 = structural_simplify(sys2; allow_symbolic = true) - prob1 = ODEProblem(sys1, [sys1.x => 1.0, sys1.y => 1.0, Differential(t)(sys1.y) => 0.0, sys1.s => 1.0], (1.0, 4.0)) - prob2 = ODEProblem(sys2, [sys2.x => 1.0, sys2.y => 1.0, Differential(s)(sys2.y) => 0.0], (1.0, 2.0)) + M1 = structural_simplify(M1; allow_symbolic = true) + M2 = structural_simplify(M2; allow_symbolic = true) + prob1 = ODEProblem(M1, [M1.x => 1.0, M1.y => 1.0, Differential(M1.t)(M1.y) => 0.0, M1.s => 1.0], (1.0, 4.0)) + prob2 = ODEProblem(M2, [M2.x => 1.0, M2.y => 1.0, Differential(M2.s)(M2.y) => 0.0], (1.0, 2.0)) sol1 = solve(prob1, Tsit5(); reltol = 1e-10, abstol = 1e-10) sol2 = solve(prob2, Tsit5(); reltol = 1e-10, abstol = 1e-10) ts = range(0.0, 1.0, length = 50) ss = .√(ts) - @test all(isapprox.(sol1(ts, idxs=sys1.x), sol2(ss, idxs=sys2.x); atol = 1e-7)) && - all(isapprox.(sol1(ts, idxs=sys1.y), sol2(ss, idxs=sys2.y); atol = 1e-7)) + @test all(isapprox.(sol1(ts, idxs=M1.x), sol2(ss, idxs=M2.x); atol = 1e-7)) && + all(isapprox.(sol1(ts, idxs=M1.y), sol2(ss, idxs=M2.y); atol = 1e-7)) end @testset "Change independent variable (Friedmann equation)" begin - @independent_variables t D = Differential(t) @variables a(t) ρr(t) ρm(t) ρΛ(t) ρ(t) P(t) ϕ(t) @parameters Ωr0 Ωm0 ΩΛ0 @@ -68,8 +63,7 @@ end @named M1 = ODESystem(eqs, t) M1 = complete(M1) - @independent_variables a - M2 = ModelingToolkit.change_independent_variable(M1, a) + M2 = ModelingToolkit.change_independent_variable(M1, M1.a) M2 = structural_simplify(M2; allow_symbolic = true) @test length(unknowns(M2)) == 2 end From 75fe0f062dce5a3d89c71b8feffd3fd4ed1ff751 Mon Sep 17 00:00:00 2001 From: Herman Sletmoen Date: Sun, 2 Mar 2025 14:51:59 +0100 Subject: [PATCH 1069/2176] Optionally simplify dummy derivative expressions when changing independent variable --- src/systems/diffeqs/basic_transformations.jl | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/systems/diffeqs/basic_transformations.jl b/src/systems/diffeqs/basic_transformations.jl index 19c997e787..a771f0e5f0 100644 --- a/src/systems/diffeqs/basic_transformations.jl +++ b/src/systems/diffeqs/basic_transformations.jl @@ -50,7 +50,7 @@ function liouville_transform(sys::AbstractODESystem; kwargs...) ODESystem(neweqs, t, vars, parameters(sys); checks = false, name = nameof(sys), kwargs...) end -function change_independent_variable(sys::AbstractODESystem, iv, eq = nothing; verbose = false, kwargs...) +function change_independent_variable(sys::AbstractODESystem, iv, eq = nothing; verbose = false, simplify = true, kwargs...) iv1 = get_iv(sys) # e.g. t iv2name = nameof(operation(unwrap(iv))) # TODO: handle namespacing? iv2, = @independent_variables $iv2name # e.g. a @@ -103,7 +103,11 @@ function change_independent_variable(sys::AbstractODESystem, iv, eq = nothing; v # 3) Add equations for dummy variables div1_div2 = 1 / div2_div1 - push!(eqs, ddiv2 ~ expand_derivatives(-D2(div1_div2) / div1_div2^3)) # e.g. https://math.stackexchange.com/questions/249253/second-derivative-of-the-inverse-function # TODO: higher orders + ddiv1_ddiv2 = expand_derivatives(-D2(div1_div2) / div1_div2^3) + if simplify + ddiv1_ddiv2 = Symbolics.simplify(ddiv1_ddiv2) + end + push!(eqs, ddiv2 ~ ddiv1_ddiv2) # e.g. https://math.stackexchange.com/questions/249253/second-derivative-of-the-inverse-function # TODO: higher orders # 4) Recreate system with new equations sys2 = typeof(sys)(eqs, iv2; name = nameof(sys), description = description(sys), kwargs...) From b8c9839bbfc071b7b32c7a1a0386b0a69a86f135 Mon Sep 17 00:00:00 2001 From: Herman Sletmoen Date: Sun, 2 Mar 2025 15:34:25 +0100 Subject: [PATCH 1070/2176] Improve change of independent variable tests --- test/basic_transformations.jl | 33 ++++++++++++++++++++++++++++----- 1 file changed, 28 insertions(+), 5 deletions(-) diff --git a/test/basic_transformations.jl b/test/basic_transformations.jl index 55f1612fd3..5f17abdd52 100644 --- a/test/basic_transformations.jl +++ b/test/basic_transformations.jl @@ -50,20 +50,43 @@ end @testset "Change independent variable (Friedmann equation)" begin D = Differential(t) - @variables a(t) ρr(t) ρm(t) ρΛ(t) ρ(t) P(t) ϕ(t) + @variables a(t) ȧ(t) ρr(t) ρm(t) ρΛ(t) ρ(t) P(t) ϕ(t) @parameters Ωr0 Ωm0 ΩΛ0 eqs = [ ρr ~ 3/(8*Num(π)) * Ωr0 / a^4 ρm ~ 3/(8*Num(π)) * Ωm0 / a^3 ρΛ ~ 3/(8*Num(π)) * ΩΛ0 ρ ~ ρr + ρm + ρΛ - D(a) ~ √(8*Num(π)/3*ρ*a^4) + ȧ ~ √(8*Num(π)/3*ρ*a^4) + D(a) ~ ȧ D(D(ϕ)) ~ -3*D(a)/a*D(ϕ) ] - @named M1 = ODESystem(eqs, t) - M1 = complete(M1) + M1 = ODESystem(eqs, t; name = :M) |> complete - M2 = ModelingToolkit.change_independent_variable(M1, M1.a) + # Apply in two steps, where derivatives are defined at each step: first t -> a, then a -> b + M2 = ModelingToolkit.change_independent_variable(M1, M1.a) |> complete #, D(b) ~ D(a)/a; verbose = true) + @variables b(M2.a) + M3 = ModelingToolkit.change_independent_variable(M2, b, Differential(M2.a)(b) ~ exp(-b)) M2 = structural_simplify(M2; allow_symbolic = true) + M3 = structural_simplify(M3; allow_symbolic = true) @test length(unknowns(M2)) == 2 end + +@testset "Change independent variable (simple)" begin + @variables x(t) + Mt = ODESystem([D(x) ~ 2*x], t; name = :M) |> complete + Mx = ModelingToolkit.change_independent_variable(Mt, Mt.x) + @test (@variables x x_t(x) x_tt(x); Set(equations(Mx)) == Set([x_t ~ 2x, x_tt ~ 4x])) +end + +@testset "Change independent variable (free fall)" begin + @variables x(t) y(t) + @parameters g v # gravitational acceleration and constant horizontal velocity + Mt = ODESystem([D(D(y)) ~ -g, D(x) ~ v], t; name = :M) |> complete # gives (x, y) as function of t, ... + Mx = ModelingToolkit.change_independent_variable(Mt, Mt.x) # ... but we want y as a function of x + Mx = structural_simplify(Mx; allow_symbolic = true) + Dx = Differential(Mx.x) + prob = ODEProblem(Mx, [Mx.y => 0.0, Dx(Mx.y) => 1.0], (0.0, 20.0), [g => 9.81, v => 10.0]) # 1 = dy/dx = (dy/dt)/(dx/dt) means equal initial horizontal and vertical velocities + sol = solve(prob, Tsit5(); reltol = 1e-5) + @test all(isapprox.(sol[Mx.y], sol[Mx.x - g*(Mx.x/v)^2/2]; atol = 1e-10)) # eliminate t from anal solution x(t) = v*t, y(t) = v*t - g*t^2/2 +end From dfc52694717c65710585e5aa96b24cc0ce0158f7 Mon Sep 17 00:00:00 2001 From: Herman Sletmoen Date: Sun, 2 Mar 2025 15:58:03 +0100 Subject: [PATCH 1071/2176] Explicitly insert dummy equations into system, if requested --- src/systems/diffeqs/basic_transformations.jl | 12 ++++++++++-- test/basic_transformations.jl | 15 ++++++++++++--- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/src/systems/diffeqs/basic_transformations.jl b/src/systems/diffeqs/basic_transformations.jl index a771f0e5f0..f0c8702e92 100644 --- a/src/systems/diffeqs/basic_transformations.jl +++ b/src/systems/diffeqs/basic_transformations.jl @@ -50,7 +50,7 @@ function liouville_transform(sys::AbstractODESystem; kwargs...) ODESystem(neweqs, t, vars, parameters(sys); checks = false, name = nameof(sys), kwargs...) end -function change_independent_variable(sys::AbstractODESystem, iv, eq = nothing; verbose = false, simplify = true, kwargs...) +function change_independent_variable(sys::AbstractODESystem, iv, eq = nothing; verbose = false, simplify = true, dummies = false, kwargs...) iv1 = get_iv(sys) # e.g. t iv2name = nameof(operation(unwrap(iv))) # TODO: handle namespacing? iv2, = @independent_variables $iv2name # e.g. a @@ -109,7 +109,15 @@ function change_independent_variable(sys::AbstractODESystem, iv, eq = nothing; v end push!(eqs, ddiv2 ~ ddiv1_ddiv2) # e.g. https://math.stackexchange.com/questions/249253/second-derivative-of-the-inverse-function # TODO: higher orders - # 4) Recreate system with new equations + # 4) If requested, instead remove and insert dummy equations + if !dummies + dummyidxs = findall(eq -> isequal(eq.lhs, div2) || isequal(eq.lhs, ddiv2), eqs) + dummyeqs = splice!(eqs, dummyidxs) # return and remove dummy equations + dummysubs = Dict(eq.lhs => eq.rhs for eq in dummyeqs) + eqs = substitute.(eqs, Ref(dummysubs)) # don't iterate over dummysubs + end + + # 5) Recreate system with new equations sys2 = typeof(sys)(eqs, iv2; name = nameof(sys), description = description(sys), kwargs...) return sys2 end diff --git a/test/basic_transformations.jl b/test/basic_transformations.jl index 5f17abdd52..add54a62c8 100644 --- a/test/basic_transformations.jl +++ b/test/basic_transformations.jl @@ -25,6 +25,15 @@ D = Differential(t) @test sol[end, end] ≈ 1.0742818931017244 end +@testset "Change independent variable (trivial)" begin + @variables x(t) y(t) + eqs1 = [D(D(x)) ~ D(x) + x, D(y) ~ 1] + M1 = ODESystem(eqs1, t; name = :M) |> complete + M2 = ModelingToolkit.change_independent_variable(M1, M1.y) + eqs2 = substitute(equations(M2), M2.y => M1.t) # system should be equivalent when parametrized with y (since D(y) ~ 1), so substitute back ... + @test eqs1[1] == only(eqs2) # ... and check that the equations are unmodified +end + @testset "Change independent variable" begin @variables x(t) y(t) z(t) s(t) eqs = [ @@ -75,7 +84,7 @@ end @testset "Change independent variable (simple)" begin @variables x(t) Mt = ODESystem([D(x) ~ 2*x], t; name = :M) |> complete - Mx = ModelingToolkit.change_independent_variable(Mt, Mt.x) + Mx = ModelingToolkit.change_independent_variable(Mt, Mt.x; dummies = true) @test (@variables x x_t(x) x_tt(x); Set(equations(Mx)) == Set([x_t ~ 2x, x_tt ~ 4x])) end @@ -83,10 +92,10 @@ end @variables x(t) y(t) @parameters g v # gravitational acceleration and constant horizontal velocity Mt = ODESystem([D(D(y)) ~ -g, D(x) ~ v], t; name = :M) |> complete # gives (x, y) as function of t, ... - Mx = ModelingToolkit.change_independent_variable(Mt, Mt.x) # ... but we want y as a function of x + Mx = ModelingToolkit.change_independent_variable(Mt, Mt.x; dummies = false) # ... but we want y as a function of x Mx = structural_simplify(Mx; allow_symbolic = true) Dx = Differential(Mx.x) prob = ODEProblem(Mx, [Mx.y => 0.0, Dx(Mx.y) => 1.0], (0.0, 20.0), [g => 9.81, v => 10.0]) # 1 = dy/dx = (dy/dt)/(dx/dt) means equal initial horizontal and vertical velocities sol = solve(prob, Tsit5(); reltol = 1e-5) - @test all(isapprox.(sol[Mx.y], sol[Mx.x - g*(Mx.x/v)^2/2]; atol = 1e-10)) # eliminate t from anal solution x(t) = v*t, y(t) = v*t - g*t^2/2 + @test all(isapprox.(sol[Mx.y], sol[Mx.x - g*(Mx.x/v)^2/2]; atol = 1e-10)) # compare to analytical solution (x(t) = v*t, y(t) = v*t - g*t^2/2) end From ae9c174b06e4a5eb959767861576f81af08b02cb Mon Sep 17 00:00:00 2001 From: Herman Sletmoen Date: Sun, 2 Mar 2025 18:09:03 +0100 Subject: [PATCH 1072/2176] Add and test errors when changing independent variable --- src/systems/diffeqs/basic_transformations.jl | 24 +++++++++++++++++--- test/basic_transformations.jl | 15 ++++++++++++ 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/src/systems/diffeqs/basic_transformations.jl b/src/systems/diffeqs/basic_transformations.jl index f0c8702e92..4991ef73f1 100644 --- a/src/systems/diffeqs/basic_transformations.jl +++ b/src/systems/diffeqs/basic_transformations.jl @@ -51,10 +51,24 @@ function liouville_transform(sys::AbstractODESystem; kwargs...) end function change_independent_variable(sys::AbstractODESystem, iv, eq = nothing; verbose = false, simplify = true, dummies = false, kwargs...) + if !iscomplete(sys) + error("Cannot change independent variable of incomplete system $(nameof(sys))") + elseif isscheduled(sys) + error("Cannot change independent variable of structurally simplified system $(nameof(sys))") + elseif !isempty(get_systems(sys)) + error("Cannot change independent variable of hierarchical system $(nameof(sys)). Flatten it first.") # TODO: implement + end + + iv = unwrap(iv) iv1 = get_iv(sys) # e.g. t - iv2name = nameof(operation(unwrap(iv))) # TODO: handle namespacing? + + if !iscall(iv) || !isequal(only(arguments(iv)), iv1) + error("New independent variable $iv is not a function of the independent variable $iv1 of the system $(nameof(sys))") + end + + iv2func = iv # e.g. a(t) + iv2name = nameof(operation(iv)) iv2, = @independent_variables $iv2name # e.g. a - iv2func, = @variables $iv2name(iv1) # e.g. a(t) D1 = Differential(iv1) D2 = Differential(iv2) @@ -98,8 +112,12 @@ function change_independent_variable(sys::AbstractODESystem, iv, eq = nothing; v end end - isnothing(div2_div1) && error("No equation for $D1($iv2func) was specified.") verbose && println("Found $div2 = $div2_div1") + if isnothing(div2_div1) + error("No equation for $D1($iv2func) was specified.") + elseif isequal(div2_div1, 0) + error("Cannot change independent variable from $iv1 to $iv2 with singular transformation $(div2 ~ div2_div1).") + end # 3) Add equations for dummy variables div1_div2 = 1 / div2_div1 diff --git a/test/basic_transformations.jl b/test/basic_transformations.jl index add54a62c8..992bbfcc48 100644 --- a/test/basic_transformations.jl +++ b/test/basic_transformations.jl @@ -99,3 +99,18 @@ end sol = solve(prob, Tsit5(); reltol = 1e-5) @test all(isapprox.(sol[Mx.y], sol[Mx.x - g*(Mx.x/v)^2/2]; atol = 1e-10)) # compare to analytical solution (x(t) = v*t, y(t) = v*t - g*t^2/2) end + +@testset "Change independent variable (errors)" begin + @variables x(t) y z(y) w(t) v(t) + M = ODESystem([D(x) ~ 0, v ~ x], t; name = :M) + @test_throws "incomplete" ModelingToolkit.change_independent_variable(M, M.x) + M = complete(M) + @test_throws "singular" ModelingToolkit.change_independent_variable(M, M.x) + @test_throws "structurally simplified" ModelingToolkit.change_independent_variable(structural_simplify(M), y) + @test_throws "No equation" ModelingToolkit.change_independent_variable(M, w) + @test_throws "No equation" ModelingToolkit.change_independent_variable(M, v) + @test_throws "not a function of the independent variable" ModelingToolkit.change_independent_variable(M, y) + @test_throws "not a function of the independent variable" ModelingToolkit.change_independent_variable(M, z) + M = compose(M, M) + @test_throws "hierarchical" ModelingToolkit.change_independent_variable(M, M.x) +end From 00b27111ddd3ec9b00ebd620cab0c3e5b6fd8116 Mon Sep 17 00:00:00 2001 From: Herman Sletmoen Date: Sun, 2 Mar 2025 19:45:22 +0100 Subject: [PATCH 1073/2176] Export and document change_independent_variable --- docs/src/systems/ODESystem.md | 1 + src/ModelingToolkit.jl | 2 +- src/systems/diffeqs/basic_transformations.jl | 41 +++++++++++++++++++- test/basic_transformations.jl | 36 ++++++++--------- 4 files changed, 60 insertions(+), 20 deletions(-) diff --git a/docs/src/systems/ODESystem.md b/docs/src/systems/ODESystem.md index 6cc34725c4..4be0f6e263 100644 --- a/docs/src/systems/ODESystem.md +++ b/docs/src/systems/ODESystem.md @@ -30,6 +30,7 @@ ODESystem structural_simplify ode_order_lowering dae_index_lowering +change_independent_variable liouville_transform alias_elimination tearing diff --git a/src/ModelingToolkit.jl b/src/ModelingToolkit.jl index e6b8130af3..51156ba993 100644 --- a/src/ModelingToolkit.jl +++ b/src/ModelingToolkit.jl @@ -255,7 +255,7 @@ export isinput, isoutput, getbounds, hasbounds, getguess, hasguess, isdisturbanc tunable_parameters, isirreducible, getdescription, hasdescription, hasunit, getunit, hasconnect, getconnect, hasmisc, getmisc, state_priority -export ode_order_lowering, dae_order_lowering, liouville_transform +export ode_order_lowering, dae_order_lowering, liouville_transform, change_independent_variable export PDESystem export Differential, expand_derivatives, @derivatives export Equation, ConstrainedEquation diff --git a/src/systems/diffeqs/basic_transformations.jl b/src/systems/diffeqs/basic_transformations.jl index 4991ef73f1..665f150407 100644 --- a/src/systems/diffeqs/basic_transformations.jl +++ b/src/systems/diffeqs/basic_transformations.jl @@ -50,7 +50,46 @@ function liouville_transform(sys::AbstractODESystem; kwargs...) ODESystem(neweqs, t, vars, parameters(sys); checks = false, name = nameof(sys), kwargs...) end -function change_independent_variable(sys::AbstractODESystem, iv, eq = nothing; verbose = false, simplify = true, dummies = false, kwargs...) +""" + function change_independent_variable(sys::AbstractODESystem, iv, eq = nothing; dummies = false, simplify = true, verbose = false, kwargs...) + +Transform the independent variable (e.g. ``t``) of the ODE system `sys` to a dependent variable `iv` (e.g. ``f(t)``). +An equation in `sys` must define the rate of change of the new independent variable (e.g. ``df(t)/dt``). +Alternatively, `eq` can specify such an equation. + +The transformation is well-defined when the mapping between the new and old independent variables are one-to-one. +This is satisfied if one is a strictly increasing function of the other (e.g. ``df(t)/dt > 0`` or ``df(t)/dt < 0``). + +Keyword arguments +================= +If `dummies`, derivatives of the new independent variable are expressed through dummy equations; otherwise they are explicitly inserted into the equations. +If `simplify`, these dummy expressions are simplified and often give a tidier transformation. +If `verbose`, the function prints intermediate transformations of equations to aid debugging. +Any additional keyword arguments `kwargs...` are forwarded to the constructor that rebuilds the system. + +Usage before structural simplification +====================================== +The variable change must take place before structural simplification. +Subsequently, consider passing `allow_symbolic = true` to `structural_simplify(sys)` to reduce the number of unknowns, with the understanding that the transformation is well-defined. + +Example +======= +Consider a free fall with constant horizontal velocity. +The laws of physics naturally describes position as a function of time. +By changing the independent variable, it can be reformulated for vertical position as a function of horizontal distance: +```julia +julia> @variables x(t) y(t); + +julia> @named M = ODESystem([D(D(y)) ~ -9.81, D(x) ~ 10.0], t); + +julia> M′ = change_independent_variable(complete(M), x); + +julia> unknowns(M′) +1-element Vector{SymbolicUtils.BasicSymbolic{Real}}: + y(x) +``` +""" +function change_independent_variable(sys::AbstractODESystem, iv, eq = nothing; dummies = false, simplify = true, verbose = false, kwargs...) if !iscomplete(sys) error("Cannot change independent variable of incomplete system $(nameof(sys))") elseif isscheduled(sys) diff --git a/test/basic_transformations.jl b/test/basic_transformations.jl index 992bbfcc48..2e7f0ce54b 100644 --- a/test/basic_transformations.jl +++ b/test/basic_transformations.jl @@ -29,7 +29,7 @@ end @variables x(t) y(t) eqs1 = [D(D(x)) ~ D(x) + x, D(y) ~ 1] M1 = ODESystem(eqs1, t; name = :M) |> complete - M2 = ModelingToolkit.change_independent_variable(M1, M1.y) + M2 = change_independent_variable(M1, M1.y) eqs2 = substitute(equations(M2), M2.y => M1.t) # system should be equivalent when parametrized with y (since D(y) ~ 1), so substitute back ... @test eqs1[1] == only(eqs2) # ... and check that the equations are unmodified end @@ -43,10 +43,10 @@ end D(s) ~ 1 / (2*s) ] M1 = ODESystem(eqs, t; name = :M) |> complete - M2 = ModelingToolkit.change_independent_variable(M1, M1.s) + M2 = change_independent_variable(M1, M1.s) - M1 = structural_simplify(M1; allow_symbolic = true) - M2 = structural_simplify(M2; allow_symbolic = true) + M1 = structural_simplify(M1) + M2 = structural_simplify(M2) prob1 = ODEProblem(M1, [M1.x => 1.0, M1.y => 1.0, Differential(M1.t)(M1.y) => 0.0, M1.s => 1.0], (1.0, 4.0)) prob2 = ODEProblem(M2, [M2.x => 1.0, M2.y => 1.0, Differential(M2.s)(M2.y) => 0.0], (1.0, 2.0)) sol1 = solve(prob1, Tsit5(); reltol = 1e-10, abstol = 1e-10) @@ -73,18 +73,18 @@ end M1 = ODESystem(eqs, t; name = :M) |> complete # Apply in two steps, where derivatives are defined at each step: first t -> a, then a -> b - M2 = ModelingToolkit.change_independent_variable(M1, M1.a) |> complete #, D(b) ~ D(a)/a; verbose = true) + M2 = change_independent_variable(M1, M1.a) |> complete #, D(b) ~ D(a)/a; verbose = true) @variables b(M2.a) - M3 = ModelingToolkit.change_independent_variable(M2, b, Differential(M2.a)(b) ~ exp(-b)) + M3 = change_independent_variable(M2, b, Differential(M2.a)(b) ~ exp(-b)) M2 = structural_simplify(M2; allow_symbolic = true) M3 = structural_simplify(M3; allow_symbolic = true) - @test length(unknowns(M2)) == 2 + @test length(unknowns(M2)) == 2 && length(unknowns(M3)) == 2 end @testset "Change independent variable (simple)" begin @variables x(t) - Mt = ODESystem([D(x) ~ 2*x], t; name = :M) |> complete - Mx = ModelingToolkit.change_independent_variable(Mt, Mt.x; dummies = true) + Mt = ODESystem([D(x) ~ 2*x], t; name = :M) |> complete # TODO: avoid complete. can avoid it if passing defined $variable directly to change_independent_variable + Mx = change_independent_variable(Mt, Mt.x; dummies = true) @test (@variables x x_t(x) x_tt(x); Set(equations(Mx)) == Set([x_t ~ 2x, x_tt ~ 4x])) end @@ -92,7 +92,7 @@ end @variables x(t) y(t) @parameters g v # gravitational acceleration and constant horizontal velocity Mt = ODESystem([D(D(y)) ~ -g, D(x) ~ v], t; name = :M) |> complete # gives (x, y) as function of t, ... - Mx = ModelingToolkit.change_independent_variable(Mt, Mt.x; dummies = false) # ... but we want y as a function of x + Mx = change_independent_variable(Mt, Mt.x; dummies = false) # ... but we want y as a function of x Mx = structural_simplify(Mx; allow_symbolic = true) Dx = Differential(Mx.x) prob = ODEProblem(Mx, [Mx.y => 0.0, Dx(Mx.y) => 1.0], (0.0, 20.0), [g => 9.81, v => 10.0]) # 1 = dy/dx = (dy/dt)/(dx/dt) means equal initial horizontal and vertical velocities @@ -103,14 +103,14 @@ end @testset "Change independent variable (errors)" begin @variables x(t) y z(y) w(t) v(t) M = ODESystem([D(x) ~ 0, v ~ x], t; name = :M) - @test_throws "incomplete" ModelingToolkit.change_independent_variable(M, M.x) + @test_throws "incomplete" change_independent_variable(M, M.x) M = complete(M) - @test_throws "singular" ModelingToolkit.change_independent_variable(M, M.x) - @test_throws "structurally simplified" ModelingToolkit.change_independent_variable(structural_simplify(M), y) - @test_throws "No equation" ModelingToolkit.change_independent_variable(M, w) - @test_throws "No equation" ModelingToolkit.change_independent_variable(M, v) - @test_throws "not a function of the independent variable" ModelingToolkit.change_independent_variable(M, y) - @test_throws "not a function of the independent variable" ModelingToolkit.change_independent_variable(M, z) + @test_throws "singular" change_independent_variable(M, M.x) + @test_throws "structurally simplified" change_independent_variable(structural_simplify(M), y) + @test_throws "No equation" change_independent_variable(M, w) + @test_throws "No equation" change_independent_variable(M, v) + @test_throws "not a function of the independent variable" change_independent_variable(M, y) + @test_throws "not a function of the independent variable" change_independent_variable(M, z) M = compose(M, M) - @test_throws "hierarchical" ModelingToolkit.change_independent_variable(M, M.x) + @test_throws "hierarchical" change_independent_variable(M, M.x) end From ee212232247ef248c969ae6dcc84be27dbebfb02 Mon Sep 17 00:00:00 2001 From: Herman Sletmoen Date: Mon, 3 Mar 2025 16:45:27 +0100 Subject: [PATCH 1074/2176] Handle autonomous systems and more fields --- src/systems/diffeqs/basic_transformations.jl | 66 +++++++++++--------- test/basic_transformations.jl | 21 ++++--- 2 files changed, 49 insertions(+), 38 deletions(-) diff --git a/src/systems/diffeqs/basic_transformations.jl b/src/systems/diffeqs/basic_transformations.jl index 665f150407..56d3ada089 100644 --- a/src/systems/diffeqs/basic_transformations.jl +++ b/src/systems/diffeqs/basic_transformations.jl @@ -51,7 +51,7 @@ function liouville_transform(sys::AbstractODESystem; kwargs...) end """ - function change_independent_variable(sys::AbstractODESystem, iv, eq = nothing; dummies = false, simplify = true, verbose = false, kwargs...) + function change_independent_variable(sys::AbstractODESystem, iv, eqs = []; dummies = false, simplify = true, verbose = false, kwargs...) Transform the independent variable (e.g. ``t``) of the ODE system `sys` to a dependent variable `iv` (e.g. ``f(t)``). An equation in `sys` must define the rate of change of the new independent variable (e.g. ``df(t)/dt``). @@ -89,7 +89,7 @@ julia> unknowns(M′) y(x) ``` """ -function change_independent_variable(sys::AbstractODESystem, iv, eq = nothing; dummies = false, simplify = true, verbose = false, kwargs...) +function change_independent_variable(sys::AbstractODESystem, iv, eqs = []; dummies = false, simplify = true, verbose = false, kwargs...) if !iscomplete(sys) error("Cannot change independent variable of incomplete system $(nameof(sys))") elseif isscheduled(sys) @@ -100,9 +100,10 @@ function change_independent_variable(sys::AbstractODESystem, iv, eq = nothing; d iv = unwrap(iv) iv1 = get_iv(sys) # e.g. t - if !iscall(iv) || !isequal(only(arguments(iv)), iv1) error("New independent variable $iv is not a function of the independent variable $iv1 of the system $(nameof(sys))") + elseif !isautonomous(sys) && isempty(findall(eq -> isequal(eq.lhs, iv1), eqs)) + error("System $(nameof(sys)) is autonomous in $iv1. An equation of the form $iv1 ~ F($iv) must be provided.") end iv2func = iv # e.g. a(t) @@ -111,50 +112,53 @@ function change_independent_variable(sys::AbstractODESystem, iv, eq = nothing; d D1 = Differential(iv1) D2 = Differential(iv2) + iv1name = nameof(iv1) # e.g. t + iv1func, = @variables $iv1name(iv2) # e.g. t(a) + div2name = Symbol(iv2name, :_t) div2, = @variables $div2name(iv2) # e.g. a_t(a) ddiv2name = Symbol(iv2name, :_tt) ddiv2, = @variables $ddiv2name(iv2) # e.g. a_tt(a) - eqs = ModelingToolkit.get_eqs(sys) |> copy # don't modify original system - !isnothing(eq) && push!(eqs, eq) - vars = [] - div2_div1 = nothing - for (i, eq) in enumerate(eqs) - verbose && println("1. ", eq) - + function transform(ex) # 1) Substitute f(t₁) => f(t₂(t₁)) in all variables - vars = Symbolics.get_variables(eq) + verbose && println("1. ", ex) + vars = Symbolics.get_variables(ex) for var1 in vars if Symbolics.iscall(var1) && !isequal(var1, iv2func) # && isequal(only(arguments(var1)), iv1) # skip e.g. constants name = nameof(operation(var1)) var2, = @variables $name(iv2func) - eq = substitute(eq, var1 => var2; fold = false) + ex = substitute(ex, var1 => var2; fold = false) end end - verbose && println("2. ", eq) # 2) Substitute in dummy variables for dⁿt₂/dt₁ⁿ - eq = expand_derivatives(eq) # expand out with chain rule to get d(iv2)/d(iv1) - verbose && println("3. ", eq) - eq = substitute(eq, D1(D1(iv2func)) => ddiv2) # order 2 # TODO: more orders - eq = substitute(eq, D1(iv2func) => div2) # order 1; e.g. D(a(t)) => a_t(t) - eq = substitute(eq, iv2func => iv2) # order 0; make iv2 independent - verbose && println("4. ", eq) - verbose && println() - - eqs[i] = eq - - if isequal(eq.lhs, div2) - div2_div1 = eq.rhs - end + verbose && println("2. ", ex) + ex = expand_derivatives(ex) # expand out with chain rule to get d(iv2)/d(iv1) + verbose && println("3. ", ex) + ex = substitute(ex, D1(D1(iv2func)) => ddiv2) # order 2 # TODO: more orders + ex = substitute(ex, D1(iv2func) => div2) # order 1; e.g. D(a(t)) => a_t(t) + ex = substitute(ex, iv2func => iv2) # order 0; make iv2 independent + verbose && println("4. ", ex) + ex = substitute(ex, iv1 => iv1func) # if autonomous + verbose && println("5. ", ex) + return ex end - verbose && println("Found $div2 = $div2_div1") - if isnothing(div2_div1) + eqs = [get_eqs(sys); eqs] + eqs = map(transform, eqs) + + initialization_eqs = map(transform, get_initialization_eqs(sys)) + + div2_div1_idxs = findall(eq -> isequal(eq.lhs, div2), eqs) + if length(div2_div1_idxs) == 0 error("No equation for $D1($iv2func) was specified.") - elseif isequal(div2_div1, 0) + elseif length(div2_div1_idxs) >= 2 + error("Multiple equations for $D1($iv2func) were specified.") + end + div2_div1 = eqs[only(div2_div1_idxs)].rhs + if isequal(div2_div1, 0) error("Cannot change independent variable from $iv1 to $iv2 with singular transformation $(div2 ~ div2_div1).") end @@ -167,7 +171,7 @@ function change_independent_variable(sys::AbstractODESystem, iv, eq = nothing; d push!(eqs, ddiv2 ~ ddiv1_ddiv2) # e.g. https://math.stackexchange.com/questions/249253/second-derivative-of-the-inverse-function # TODO: higher orders # 4) If requested, instead remove and insert dummy equations - if !dummies + if !dummies # TODO: make dummies = false work with more fields! dummyidxs = findall(eq -> isequal(eq.lhs, div2) || isequal(eq.lhs, ddiv2), eqs) dummyeqs = splice!(eqs, dummyidxs) # return and remove dummy equations dummysubs = Dict(eq.lhs => eq.rhs for eq in dummyeqs) @@ -175,6 +179,6 @@ function change_independent_variable(sys::AbstractODESystem, iv, eq = nothing; d end # 5) Recreate system with new equations - sys2 = typeof(sys)(eqs, iv2; name = nameof(sys), description = description(sys), kwargs...) + sys2 = typeof(sys)(eqs, iv2; initialization_eqs, name = nameof(sys), description = description(sys), kwargs...) return sys2 end diff --git a/test/basic_transformations.jl b/test/basic_transformations.jl index 2e7f0ce54b..dc9fa21981 100644 --- a/test/basic_transformations.jl +++ b/test/basic_transformations.jl @@ -42,13 +42,14 @@ end z ~ x + D(y) D(s) ~ 1 / (2*s) ] - M1 = ODESystem(eqs, t; name = :M) |> complete - M2 = change_independent_variable(M1, M1.s) + initialization_eqs = [x ~ 1.0, y ~ 1.0, D(y) ~ 0.0] + M1 = ODESystem(eqs, t; initialization_eqs, name = :M) |> complete + M2 = change_independent_variable(M1, M1.s; dummies = true) - M1 = structural_simplify(M1) - M2 = structural_simplify(M2) - prob1 = ODEProblem(M1, [M1.x => 1.0, M1.y => 1.0, Differential(M1.t)(M1.y) => 0.0, M1.s => 1.0], (1.0, 4.0)) - prob2 = ODEProblem(M2, [M2.x => 1.0, M2.y => 1.0, Differential(M2.s)(M2.y) => 0.0], (1.0, 2.0)) + M1 = structural_simplify(M1; allow_symbolic = true) + M2 = structural_simplify(M2; allow_symbolic = true) + prob1 = ODEProblem(M1, [M1.s => 1.0], (1.0, 4.0), []) + prob2 = ODEProblem(M2, [], (1.0, 2.0), []) sol1 = solve(prob1, Tsit5(); reltol = 1e-10, abstol = 1e-10) sol2 = solve(prob2, Tsit5(); reltol = 1e-10, abstol = 1e-10) ts = range(0.0, 1.0, length = 50) @@ -75,7 +76,7 @@ end # Apply in two steps, where derivatives are defined at each step: first t -> a, then a -> b M2 = change_independent_variable(M1, M1.a) |> complete #, D(b) ~ D(a)/a; verbose = true) @variables b(M2.a) - M3 = change_independent_variable(M2, b, Differential(M2.a)(b) ~ exp(-b)) + M3 = change_independent_variable(M2, b, [Differential(M2.a)(b) ~ exp(-b), M2.a ~ exp(b)]) M2 = structural_simplify(M2; allow_symbolic = true) M3 = structural_simplify(M3; allow_symbolic = true) @test length(unknowns(M2)) == 2 && length(unknowns(M3)) == 2 @@ -100,6 +101,12 @@ end @test all(isapprox.(sol[Mx.y], sol[Mx.x - g*(Mx.x/v)^2/2]; atol = 1e-10)) # compare to analytical solution (x(t) = v*t, y(t) = v*t - g*t^2/2) end +@testset "Change independent variable (autonomous system)" begin + M = ODESystem([D(x) ~ t], t; name = :M) |> complete # non-autonomous + @test_throws "t ~ F(x(t)) must be provided" change_independent_variable(M, M.x) + @test_nowarn change_independent_variable(M, M.x, [t ~ 2*x]) +end + @testset "Change independent variable (errors)" begin @variables x(t) y z(y) w(t) v(t) M = ODESystem([D(x) ~ 0, v ~ x], t; name = :M) From 9444d9395ac795042023687822f7a7a878db8be0 Mon Sep 17 00:00:00 2001 From: Herman Sletmoen Date: Mon, 3 Mar 2025 16:48:18 +0100 Subject: [PATCH 1075/2176] Add tutorial for changing independent variable --- docs/pages.jl | 1 + .../tutorials/change_independent_variable.md | 108 ++++++++++++++++++ 2 files changed, 109 insertions(+) create mode 100644 docs/src/tutorials/change_independent_variable.md diff --git a/docs/pages.jl b/docs/pages.jl index f6c49a0de3..829c0d1c08 100644 --- a/docs/pages.jl +++ b/docs/pages.jl @@ -10,6 +10,7 @@ pages = [ "tutorials/stochastic_diffeq.md", "tutorials/discrete_system.md", "tutorials/parameter_identifiability.md", + "tutorials/change_independent_variable.md", "tutorials/bifurcation_diagram_computation.md", "tutorials/SampledData.md", "tutorials/domain_connections.md", diff --git a/docs/src/tutorials/change_independent_variable.md b/docs/src/tutorials/change_independent_variable.md new file mode 100644 index 0000000000..97ca77fef8 --- /dev/null +++ b/docs/src/tutorials/change_independent_variable.md @@ -0,0 +1,108 @@ +# Changing the independent variable of ODEs + +Ordinary differential equations describe the rate of change of some dependent variables with respect to one independent variable. +For the modeler it is often most natural to write down the equations with a particular independent variable, say time $t$. +In many cases there are good reasons for reparametrizing ODEs in terms of a different independent variable: + +1. One may want $y(x)$ as a function of $x$ instead of $(x(t), y(t))$ as a function of $t$ +2. Some differential equations vary more nicely (e.g. less stiff or better behaved) with respect to one independent variable than another. +3. It can reduce the number of equations that must be solved (e.g. $y(x)$ is one equation, while $(x(t), y(t))$ are two). + +To manually change the independent variable of an ODE, one must rewrite all equations in terms of a new variable and transform differentials with the chain rule. +This is mechanical and error-prone. +ModelingToolkit provides the utility function [`change_independent_variable`](@ref) that automates this process. + +## 1. Get one dependent variable as a function of another + +Consider a projectile shot with some initial velocity in a gravitational field. +```@example changeivar +using ModelingToolkit +@independent_variables t +D = Differential(t) +@variables x(t) y(t) +@parameters g = 9.81 v # gravitational acceleration and constant horizontal velocity +M1 = ODESystem([ + D(D(y)) ~ -g, D(x) ~ v # constant horizontal velocity +], t; initialization_eqs = [ + #x ~ 0.0, # TODO: handle? + y ~ 0.0, D(x) ~ D(y) # equal initial horizontal and vertical velocity (45 °) +], name = :M) |> complete +M1s = structural_simplify(M1) +``` +This is the standard parametrization that arises naturally from kinematics and Newton's laws. +It expresses the position $(x(t), y(t))$ as a function of time $t$. +But suppose we want to determine whether the projectile hits a target 10 meters away. +There are at least three ways of answering this: +* Solve the ODE for $(x(t), y(t))$ and use a callback to terminate when $x$ reaches 10 meters, and evaluate $y$ at the final time. +* Solve the ODE for $(x(t), y(t))$ and use root finding to find the time when $x$ reaches 10 meters, and evaluate $y$ at that time. +* Solve the ODE for $y(x)$ and evaluate it at 10 meters. + +We will demonstrate the last method by changing the independent variable from $t$ to $x$. +This transformation is well-defined for any non-zero horizontal velocity $v$. +```@example changeivar +M2 = change_independent_variable(M1, M1.x; dummies = true) |> complete +@assert M2.x isa Num # hide +M2s = structural_simplify(M2; allow_symbolic = true) +``` +The derivatives are now with respect to the new independent variable $x$, which can be accessed with `M2.x`. +Notice how the number of equations has also decreased from three to two, as $\mathrm{d}x/\mathrm{d}t$ has been turned into an observed equation. +It is straightforward to evolve the ODE for 10 meters and plot the resulting trajectory $y(x)$: +```@example changeivar +using OrdinaryDiffEq, Plots +prob = ODEProblem(M2s, [], [0.0, 10.0], [v => 8.0]) # throw 10 meters with x-velocity 8 m/s +sol = solve(prob, Tsit5()) +plot(sol; idxs = M2.y) # must index by M2.y = y(x); not M1.y = y(t)! +``` + +## 2. Reduce stiffness by changing to a logarithmic time axis + +In cosmology, the [Friedmann equations](https://en.wikipedia.org/wiki/Friedmann_equations) describe the expansion of the universe. +In terms of conformal time $t$, they can be written +```@example changeivar +@variables a(t) Ω(t) Ωr(t) Ωm(t) ΩΛ(t) +M1 = ODESystem([ + D(a) ~ √(Ω) * a^2 + Ω ~ Ωr + Ωm + ΩΛ + D(Ωm) ~ -3 * D(a)/a * Ωm + D(Ωr) ~ -4 * D(a)/a * Ωr + D(ΩΛ) ~ 0 +], t; initialization_eqs = [ + ΩΛ + Ωr + Ωm ~ 1 +], name = :M) |> complete +M1s = structural_simplify(M1) +``` +Of course, we can solve this ODE as it is: +```@example changeivar +prob = ODEProblem(M1s, [M1.a => 1.0, M1.Ωr => 5e-5, M1.Ωm => 0.3], (0.0, -3.3), []) +sol = solve(prob, Tsit5(); reltol = 1e-5) +@assert Symbol(sol.retcode) == :Unstable # surrounding text assumes this was unstable # hide +plot(sol, idxs = [M1.a, M1.Ωr/M1.Ω, M1.Ωm/M1.Ω, M1.ΩΛ/M1.Ω]) +``` +The solver becomes unstable due to stiffness. +Also notice the interesting dynamics taking place towards the end of the integration (in the early universe), which gets compressed into a very small time interval. +These ODEs would benefit from being defined with respect to a logarithmic "time" that better captures the evolution of the universe through *orders of magnitude* of time. + +It is therefore common to write these ODEs in terms of $b = \ln a$. +To do this, we will change the independent variable in two stages; from $t$ to $a$ to $b$. +Notice that $\mathrm{d}a/\mathrm{d}t > 0$ provided that $\Omega > 0$, and $\mathrm{d}b/\mathrm{d}a > 0$, so the transformation is well-defined. +First, we transform from $t$ to $a$: +```@example changeivar +M2 = change_independent_variable(M1, M1.a; dummies = true) |> complete +``` +Unlike the original, notice that this system is *non-autonomous* because the independent variable $a$ appears explicitly in the equations! +This means that to change the independent variable from $a$ to $b$, we must provide not only the rate of change relation $db(a)/da = \exp(-b)$, but *also* the equation $a(b) = \exp(b)$ so $a$ can be eliminated in favor of $b$: +```@example changeivar +a = M2.a +Da = Differential(a) +@variables b(a) +M3 = change_independent_variable(M2, b, [Da(b) ~ exp(-b), a ~ exp(b)]) |> complete +``` +We can now solve and plot the ODE in terms of $b$: +```@example changeivar +M3s = structural_simplify(M3; allow_symbolic = true) +prob = ODEProblem(M3s, [M3.Ωr => 5e-5, M3.Ωm => 0.3], (0, -15), []) +sol = solve(prob, Tsit5()) +@assert Symbol(sol.retcode) == :Success # surrounding text assumes the solution was successful # hide +plot(sol, idxs = [M3.Ωr/M3.Ω, M3.Ωm/M3.Ω, M3.ΩΛ/M3.Ω]) +``` +Notice that the variables vary "more nicely" with respect to $b$ than $t$, making the solver happier and avoiding numerical problems. From 98318864dbfa9fc0ac0a4721ebb25433bd3524b8 Mon Sep 17 00:00:00 2001 From: Herman Sletmoen Date: Mon, 3 Mar 2025 21:31:23 +0100 Subject: [PATCH 1076/2176] Reorder things and make universal function that transforms all ODESystem fields --- .../tutorials/change_independent_variable.md | 6 +- src/systems/diffeqs/basic_transformations.jl | 120 +++++++++++------- test/basic_transformations.jl | 4 +- 3 files changed, 77 insertions(+), 53 deletions(-) diff --git a/docs/src/tutorials/change_independent_variable.md b/docs/src/tutorials/change_independent_variable.md index 97ca77fef8..6274465264 100644 --- a/docs/src/tutorials/change_independent_variable.md +++ b/docs/src/tutorials/change_independent_variable.md @@ -23,9 +23,11 @@ D = Differential(t) @parameters g = 9.81 v # gravitational acceleration and constant horizontal velocity M1 = ODESystem([ D(D(y)) ~ -g, D(x) ~ v # constant horizontal velocity -], t; initialization_eqs = [ +], t; defaults = [ + y => 0.0 +], initialization_eqs = [ #x ~ 0.0, # TODO: handle? - y ~ 0.0, D(x) ~ D(y) # equal initial horizontal and vertical velocity (45 °) + D(x) ~ D(y) # equal initial horizontal and vertical velocity (45 °) ], name = :M) |> complete M1s = structural_simplify(M1) ``` diff --git a/src/systems/diffeqs/basic_transformations.jl b/src/systems/diffeqs/basic_transformations.jl index 56d3ada089..396dbd51f5 100644 --- a/src/systems/diffeqs/basic_transformations.jl +++ b/src/systems/diffeqs/basic_transformations.jl @@ -110,20 +110,59 @@ function change_independent_variable(sys::AbstractODESystem, iv, eqs = []; dummi iv2name = nameof(operation(iv)) iv2, = @independent_variables $iv2name # e.g. a D1 = Differential(iv1) - D2 = Differential(iv2) iv1name = nameof(iv1) # e.g. t iv1func, = @variables $iv1name(iv2) # e.g. t(a) - div2name = Symbol(iv2name, :_t) - div2, = @variables $div2name(iv2) # e.g. a_t(a) + eqs = [get_eqs(sys); eqs] # copies system equations to avoid modifying original system - ddiv2name = Symbol(iv2name, :_tt) - ddiv2, = @variables $ddiv2name(iv2) # e.g. a_tt(a) + # 1) Find and compute all necessary expressions for e.g. df/dt, d²f/dt², ... + # 1.1) Find the 1st order derivative of the new independent variable (e.g. da(t)/dt = ...), ... + div2_div1_idxs = findall(eq -> isequal(eq.lhs, D1(iv2func)), eqs) # index of e.g. da/dt = ... + if length(div2_div1_idxs) != 1 + error("Exactly one equation for $D1($iv2func) was not specified.") + end + div2_div1_eq = popat!(eqs, only(div2_div1_idxs)) # get and remove e.g. df/dt = ... (may be added back later) + div2_div1 = div2_div1_eq.rhs + if isequal(div2_div1, 0) + error("Cannot change independent variable from $iv1 to $iv2 with singular transformation $div2_div1_eq.") + end + # 1.2) ... then compute the 2nd order derivative of the new independent variable + div1_div2 = 1 / div2_div1 # TODO: URL reference for clarity + ddiv2_ddiv1 = expand_derivatives(-Differential(iv2func)(div1_div2) / div1_div2^3, simplify) # e.g. https://math.stackexchange.com/questions/249253/second-derivative-of-the-inverse-function # TODO: higher orders # TODO: pass simplify here + # 1.3) # TODO: handle higher orders (3+) derivatives ... + + # 2) If requested, insert extra dummy equations for e.g. df/dt, d²f/dt², ... + # Otherwise, replace all these derivatives by their explicit expressions + if dummies + div2name = Symbol(iv2name, :_t) # TODO: not always t + div2, = @variables $div2name(iv2) # e.g. a_t(a) + ddiv2name = Symbol(iv2name, :_tt) # TODO: not always t + ddiv2, = @variables $ddiv2name(iv2) # e.g. a_tt(a) + eqs = [eqs; [div2 ~ div2_div1, ddiv2 ~ ddiv2_ddiv1]] # add dummy equations + derivsubs = [D1(D1(iv2func)) => ddiv2, D1(iv2func) => div2] # order is crucial! + else + derivsubs = [D1(D1(iv2func)) => ddiv2_ddiv1, D1(iv2func) => div2_div1] # order is crucial! + end + derivsubs = [derivsubs; [iv2func => iv2, iv1 => iv1func]] + + if verbose + # Explain what we just did + println("Order 1 (found): $div2_div1_eq") + println("Order 2 (computed): $(D1(div2_div1_eq.lhs) ~ ddiv2_ddiv1)") + println() + println("Substitutions will be made in this order:") + for (n, sub) in enumerate(derivsubs) + println("$n: $(sub[1]) => $(sub[2])") + end + println() + end + # 3) Define a transformation function that performs the change of variable on any expression/equation function transform(ex) - # 1) Substitute f(t₁) => f(t₂(t₁)) in all variables - verbose && println("1. ", ex) + verbose && println("Step 0: ", ex) + + # Step 1: substitute f(t₁) => f(t₂(t₁)) in all variables in the expression vars = Symbolics.get_variables(ex) for var1 in vars if Symbolics.iscall(var1) && !isequal(var1, iv2func) # && isequal(only(arguments(var1)), iv1) # skip e.g. constants @@ -132,53 +171,36 @@ function change_independent_variable(sys::AbstractODESystem, iv, eqs = []; dummi ex = substitute(ex, var1 => var2; fold = false) end end + verbose && println("Step 1: ", ex) - # 2) Substitute in dummy variables for dⁿt₂/dt₁ⁿ - verbose && println("2. ", ex) + # Step 2: expand out all chain rule derivatives ex = expand_derivatives(ex) # expand out with chain rule to get d(iv2)/d(iv1) - verbose && println("3. ", ex) - ex = substitute(ex, D1(D1(iv2func)) => ddiv2) # order 2 # TODO: more orders - ex = substitute(ex, D1(iv2func) => div2) # order 1; e.g. D(a(t)) => a_t(t) - ex = substitute(ex, iv2func => iv2) # order 0; make iv2 independent - verbose && println("4. ", ex) - ex = substitute(ex, iv1 => iv1func) # if autonomous - verbose && println("5. ", ex) + verbose && println("Step 2: ", ex) + + # Step 3: substitute d²f/dt², df/dt, ... (to dummy variables or explicit expressions, depending on dummies) + for sub in derivsubs + ex = substitute(ex, sub) + end + verbose && println("Step 3: ", ex) + verbose && println() + return ex end - eqs = [get_eqs(sys); eqs] + # 4) Transform all fields eqs = map(transform, eqs) - + observed = map(transform, get_observed(sys)) initialization_eqs = map(transform, get_initialization_eqs(sys)) - - div2_div1_idxs = findall(eq -> isequal(eq.lhs, div2), eqs) - if length(div2_div1_idxs) == 0 - error("No equation for $D1($iv2func) was specified.") - elseif length(div2_div1_idxs) >= 2 - error("Multiple equations for $D1($iv2func) were specified.") - end - div2_div1 = eqs[only(div2_div1_idxs)].rhs - if isequal(div2_div1, 0) - error("Cannot change independent variable from $iv1 to $iv2 with singular transformation $(div2 ~ div2_div1).") - end - - # 3) Add equations for dummy variables - div1_div2 = 1 / div2_div1 - ddiv1_ddiv2 = expand_derivatives(-D2(div1_div2) / div1_div2^3) - if simplify - ddiv1_ddiv2 = Symbolics.simplify(ddiv1_ddiv2) - end - push!(eqs, ddiv2 ~ ddiv1_ddiv2) # e.g. https://math.stackexchange.com/questions/249253/second-derivative-of-the-inverse-function # TODO: higher orders - - # 4) If requested, instead remove and insert dummy equations - if !dummies # TODO: make dummies = false work with more fields! - dummyidxs = findall(eq -> isequal(eq.lhs, div2) || isequal(eq.lhs, ddiv2), eqs) - dummyeqs = splice!(eqs, dummyidxs) # return and remove dummy equations - dummysubs = Dict(eq.lhs => eq.rhs for eq in dummyeqs) - eqs = substitute.(eqs, Ref(dummysubs)) # don't iterate over dummysubs - end - - # 5) Recreate system with new equations - sys2 = typeof(sys)(eqs, iv2; initialization_eqs, name = nameof(sys), description = description(sys), kwargs...) - return sys2 + parameter_dependencies = map(transform, get_parameter_dependencies(sys)) + defaults = Dict(transform(var) => transform(val) for (var, val) in get_defaults(sys)) + guesses = Dict(transform(var) => transform(val) for (var, val) in get_guesses(sys)) + assertions = Dict(transform(condition) => msg for (condition, msg) in get_assertions(sys)) + # TODO: handle subsystems + + # 5) Recreate system with transformed fields + return typeof(sys)( + eqs, iv2; + observed, initialization_eqs, parameter_dependencies, defaults, guesses, assertions, + name = nameof(sys), description = description(sys), kwargs... + ) |> complete # original system had to be complete end diff --git a/test/basic_transformations.jl b/test/basic_transformations.jl index dc9fa21981..99a252b5b8 100644 --- a/test/basic_transformations.jl +++ b/test/basic_transformations.jl @@ -114,8 +114,8 @@ end M = complete(M) @test_throws "singular" change_independent_variable(M, M.x) @test_throws "structurally simplified" change_independent_variable(structural_simplify(M), y) - @test_throws "No equation" change_independent_variable(M, w) - @test_throws "No equation" change_independent_variable(M, v) + @test_throws "not specified" change_independent_variable(M, w) + @test_throws "not specified" change_independent_variable(M, v) @test_throws "not a function of the independent variable" change_independent_variable(M, y) @test_throws "not a function of the independent variable" change_independent_variable(M, z) M = compose(M, M) From 1ac7c7d3c221b115e58ce4ed52d010926da22b90 Mon Sep 17 00:00:00 2001 From: Herman Sletmoen Date: Tue, 4 Mar 2025 22:37:37 +0100 Subject: [PATCH 1077/2176] Clean up independent variable change implementation --- .../tutorials/change_independent_variable.md | 2 +- src/systems/diffeqs/basic_transformations.jl | 167 ++++++++---------- test/basic_transformations.jl | 55 ++++-- 3 files changed, 115 insertions(+), 109 deletions(-) diff --git a/docs/src/tutorials/change_independent_variable.md b/docs/src/tutorials/change_independent_variable.md index 6274465264..0d4d8bcac8 100644 --- a/docs/src/tutorials/change_independent_variable.md +++ b/docs/src/tutorials/change_independent_variable.md @@ -26,7 +26,7 @@ M1 = ODESystem([ ], t; defaults = [ y => 0.0 ], initialization_eqs = [ - #x ~ 0.0, # TODO: handle? + #x ~ 0.0, # TODO: handle? # hide D(x) ~ D(y) # equal initial horizontal and vertical velocity (45 °) ], name = :M) |> complete M1s = structural_simplify(M1) diff --git a/src/systems/diffeqs/basic_transformations.jl b/src/systems/diffeqs/basic_transformations.jl index 396dbd51f5..11a5514f42 100644 --- a/src/systems/diffeqs/basic_transformations.jl +++ b/src/systems/diffeqs/basic_transformations.jl @@ -51,32 +51,37 @@ function liouville_transform(sys::AbstractODESystem; kwargs...) end """ - function change_independent_variable(sys::AbstractODESystem, iv, eqs = []; dummies = false, simplify = true, verbose = false, kwargs...) + change_independent_variable(sys::AbstractODESystem, iv, eqs = []; dummies = false, simplify = true, fold = false, kwargs...) -Transform the independent variable (e.g. ``t``) of the ODE system `sys` to a dependent variable `iv` (e.g. ``f(t)``). -An equation in `sys` must define the rate of change of the new independent variable (e.g. ``df(t)/dt``). -Alternatively, `eq` can specify such an equation. +Transform the independent variable (e.g. ``t``) of the ODE system `sys` to a dependent variable `iv` (e.g. ``u(t)``). +An equation in `sys` must define the rate of change of the new independent variable (e.g. ``du(t)/dt``). +This or other additional equations can also be specified through `eqs`. The transformation is well-defined when the mapping between the new and old independent variables are one-to-one. -This is satisfied if one is a strictly increasing function of the other (e.g. ``df(t)/dt > 0`` or ``df(t)/dt < 0``). +This is satisfied if one is a strictly increasing function of the other (e.g. ``du(t)/dt > 0`` or ``du(t)/dt < 0``). Keyword arguments ================= -If `dummies`, derivatives of the new independent variable are expressed through dummy equations; otherwise they are explicitly inserted into the equations. +If `dummies`, derivatives of the new independent variable with respect to the old one are expressed through dummy equations; otherwise they are explicitly inserted into the equations. If `simplify`, these dummy expressions are simplified and often give a tidier transformation. -If `verbose`, the function prints intermediate transformations of equations to aid debugging. -Any additional keyword arguments `kwargs...` are forwarded to the constructor that rebuilds the system. +If `fold`, internal substitutions will evaluate numerical expressions. +Additional keyword arguments `kwargs...` are forwarded to the constructor that rebuilds `sys`. Usage before structural simplification ====================================== The variable change must take place before structural simplification. Subsequently, consider passing `allow_symbolic = true` to `structural_simplify(sys)` to reduce the number of unknowns, with the understanding that the transformation is well-defined. +Usage with non-autonomous systems +================================= +If `sys` is autonomous (i.e. ``t`` appears explicitly in its equations), it is often desirable to also pass an algebraic equation relating the new and old independent variables (e.g. ``t = f(u(t))``). +Otherwise the transformed system will be underdetermined and cannot be structurally simplified without additional changes. + Example ======= Consider a free fall with constant horizontal velocity. -The laws of physics naturally describes position as a function of time. -By changing the independent variable, it can be reformulated for vertical position as a function of horizontal distance: +Physics naturally describes position as a function of time. +By changing the independent variable, it can be reformulated for vertical position as a function of horizontal position: ```julia julia> @variables x(t) y(t); @@ -89,118 +94,94 @@ julia> unknowns(M′) y(x) ``` """ -function change_independent_variable(sys::AbstractODESystem, iv, eqs = []; dummies = false, simplify = true, verbose = false, kwargs...) +function change_independent_variable(sys::AbstractODESystem, iv, eqs = []; dummies = false, simplify = true, fold = false, kwargs...) + iv2_of_iv1 = unwrap(iv) # e.g. u(t) + iv1 = get_iv(sys) # e.g. t + if !iscomplete(sys) - error("Cannot change independent variable of incomplete system $(nameof(sys))") + error("System $(nameof(sys)) is incomplete. Complete it first!") elseif isscheduled(sys) - error("Cannot change independent variable of structurally simplified system $(nameof(sys))") + error("System $(nameof(sys)) is structurally simplified. Change independent variable before structural simplification!") elseif !isempty(get_systems(sys)) - error("Cannot change independent variable of hierarchical system $(nameof(sys)). Flatten it first.") # TODO: implement + error("System $(nameof(sys)) is hierarchical. Flatten it first!") # TODO: implement and allow? + elseif !iscall(iv2_of_iv1) || !isequal(only(arguments(iv2_of_iv1)), iv1) + error("Variable $iv is not a function of the independent variable $iv1 of the system $(nameof(sys))!") end - iv = unwrap(iv) - iv1 = get_iv(sys) # e.g. t - if !iscall(iv) || !isequal(only(arguments(iv)), iv1) - error("New independent variable $iv is not a function of the independent variable $iv1 of the system $(nameof(sys))") - elseif !isautonomous(sys) && isempty(findall(eq -> isequal(eq.lhs, iv1), eqs)) - error("System $(nameof(sys)) is autonomous in $iv1. An equation of the form $iv1 ~ F($iv) must be provided.") + iv1name = nameof(iv1) # e.g. :t + iv2name = nameof(operation(iv2_of_iv1)) # e.g. :u + iv2, = @independent_variables $iv2name # e.g. u + iv1_of_iv2, = @variables $iv1name(iv2) # inverse in case sys is autonomous; e.g. t(u) + D1 = Differential(iv1) # e.g. d/d(t) + D2 = Differential(iv2_of_iv1) # e.g. d/d(u(t)) + + # 1) Utility that performs the chain rule on an expression, e.g. (d/dt)(f(t)) -> (d/dt)(f(u(t))) -> df(u(t))/du(t) * du(t)/dt + function chain_rule(ex) + vars = get_variables(ex) + for var_of_iv1 in vars # loop through e.g. f(t) + if iscall(var_of_iv1) && !isequal(var_of_iv1, iv2_of_iv1) # handle e.g. f(t) -> f(u(t)), but not u(t) -> u(u(t)) + varname = nameof(operation(var_of_iv1)) # e.g. :f + var_of_iv2, = @variables $varname(iv2_of_iv1) # e.g. f(u(t)) + ex = substitute(ex, var_of_iv1 => var_of_iv2; fold) # e.g. f(t) -> f(u(t)) + end + end + ex = expand_derivatives(ex, simplify) # expand chain rule, e.g. (d/dt)(f(u(t)))) -> df(u(t))/du(t) * du(t)/dt + return ex end - iv2func = iv # e.g. a(t) - iv2name = nameof(operation(iv)) - iv2, = @independent_variables $iv2name # e.g. a - D1 = Differential(iv1) - - iv1name = nameof(iv1) # e.g. t - iv1func, = @variables $iv1name(iv2) # e.g. t(a) - - eqs = [get_eqs(sys); eqs] # copies system equations to avoid modifying original system - - # 1) Find and compute all necessary expressions for e.g. df/dt, d²f/dt², ... - # 1.1) Find the 1st order derivative of the new independent variable (e.g. da(t)/dt = ...), ... - div2_div1_idxs = findall(eq -> isequal(eq.lhs, D1(iv2func)), eqs) # index of e.g. da/dt = ... - if length(div2_div1_idxs) != 1 - error("Exactly one equation for $D1($iv2func) was not specified.") + # 2) Find e.g. du/dt in equations, then calculate e.g. d²u/dt², ... + eqs = [get_eqs(sys); eqs] # all equations (system-defined + user-provided) we may use + idxs = findall(eq -> isequal(eq.lhs, D1(iv2_of_iv1)), eqs) + if length(idxs) != 1 + error("Exactly one equation for $D1($iv2_of_iv1) was not specified!") end - div2_div1_eq = popat!(eqs, only(div2_div1_idxs)) # get and remove e.g. df/dt = ... (may be added back later) - div2_div1 = div2_div1_eq.rhs - if isequal(div2_div1, 0) - error("Cannot change independent variable from $iv1 to $iv2 with singular transformation $div2_div1_eq.") + div2_of_iv1_eq = popat!(eqs, only(idxs)) # get and remove e.g. du/dt = ... (may be added back later as a dummy) + div2_of_iv1 = chain_rule(div2_of_iv1_eq.rhs) + if isequal(div2_of_iv1, 0) # e.g. du/dt ~ 0 + error("Independent variable transformation $(div2_of_iv1_eq) is singular!") end - # 1.2) ... then compute the 2nd order derivative of the new independent variable - div1_div2 = 1 / div2_div1 # TODO: URL reference for clarity - ddiv2_ddiv1 = expand_derivatives(-Differential(iv2func)(div1_div2) / div1_div2^3, simplify) # e.g. https://math.stackexchange.com/questions/249253/second-derivative-of-the-inverse-function # TODO: higher orders # TODO: pass simplify here - # 1.3) # TODO: handle higher orders (3+) derivatives ... + ddiv2_of_iv1 = chain_rule(D1(div2_of_iv1)) # TODO: implement higher orders (order >= 3) derivatives with a loop - # 2) If requested, insert extra dummy equations for e.g. df/dt, d²f/dt², ... + # 3) If requested, insert extra dummy equations for e.g. du/dt, d²u/dt², ... # Otherwise, replace all these derivatives by their explicit expressions if dummies - div2name = Symbol(iv2name, :_t) # TODO: not always t - div2, = @variables $div2name(iv2) # e.g. a_t(a) - ddiv2name = Symbol(iv2name, :_tt) # TODO: not always t - ddiv2, = @variables $ddiv2name(iv2) # e.g. a_tt(a) - eqs = [eqs; [div2 ~ div2_div1, ddiv2 ~ ddiv2_ddiv1]] # add dummy equations - derivsubs = [D1(D1(iv2func)) => ddiv2, D1(iv2func) => div2] # order is crucial! + div2name = Symbol(iv2name, :_, iv1name) # e.g. :u_t # TODO: customize + ddiv2name = Symbol(div2name, iv1name) # e.g. :u_tt # TODO: customize + div2, ddiv2 = @variables $div2name(iv2) $ddiv2name(iv2) # e.g. u_t(u), u_tt(u) + eqs = [eqs; [div2 ~ div2_of_iv1, ddiv2 ~ ddiv2_of_iv1]] # add dummy equations else - derivsubs = [D1(D1(iv2func)) => ddiv2_ddiv1, D1(iv2func) => div2_div1] # order is crucial! - end - derivsubs = [derivsubs; [iv2func => iv2, iv1 => iv1func]] - - if verbose - # Explain what we just did - println("Order 1 (found): $div2_div1_eq") - println("Order 2 (computed): $(D1(div2_div1_eq.lhs) ~ ddiv2_ddiv1)") - println() - println("Substitutions will be made in this order:") - for (n, sub) in enumerate(derivsubs) - println("$n: $(sub[1]) => $(sub[2])") - end - println() + div2 = div2_of_iv1 + ddiv2 = ddiv2_of_iv1 end - # 3) Define a transformation function that performs the change of variable on any expression/equation + # 4) Transform everything from old to new independent variable, e.g. t -> u. + # Substitution order matters! Must begin with highest order to get D(D(u(t))) -> u_tt(u). + # If we had started with the lowest order, we would get D(D(u(t))) -> D(u_t(u)) -> 0! + iv1_to_iv2_subs = [ # a vector ensures substitution order + D1(D1(iv2_of_iv1)) => ddiv2 # order 2, e.g. D(D(u(t))) -> u_tt(u) + D1(iv2_of_iv1) => div2 # order 1, e.g. D(u(t)) -> u_t(u) + iv2_of_iv1 => iv2 # order 0, e.g. u(t) -> u + iv1 => iv1_of_iv2 # in case sys was autonomous, e.g. t -> t(u) + ] function transform(ex) - verbose && println("Step 0: ", ex) - - # Step 1: substitute f(t₁) => f(t₂(t₁)) in all variables in the expression - vars = Symbolics.get_variables(ex) - for var1 in vars - if Symbolics.iscall(var1) && !isequal(var1, iv2func) # && isequal(only(arguments(var1)), iv1) # skip e.g. constants - name = nameof(operation(var1)) - var2, = @variables $name(iv2func) - ex = substitute(ex, var1 => var2; fold = false) - end + ex = chain_rule(ex) + for sub in iv1_to_iv2_subs + ex = substitute(ex, sub; fold) end - verbose && println("Step 1: ", ex) - - # Step 2: expand out all chain rule derivatives - ex = expand_derivatives(ex) # expand out with chain rule to get d(iv2)/d(iv1) - verbose && println("Step 2: ", ex) - - # Step 3: substitute d²f/dt², df/dt, ... (to dummy variables or explicit expressions, depending on dummies) - for sub in derivsubs - ex = substitute(ex, sub) - end - verbose && println("Step 3: ", ex) - verbose && println() - return ex end - - # 4) Transform all fields - eqs = map(transform, eqs) + eqs = map(transform, eqs) # we derived and added equations to eqs; they are not in get_eqs(sys)! observed = map(transform, get_observed(sys)) initialization_eqs = map(transform, get_initialization_eqs(sys)) parameter_dependencies = map(transform, get_parameter_dependencies(sys)) defaults = Dict(transform(var) => transform(val) for (var, val) in get_defaults(sys)) guesses = Dict(transform(var) => transform(val) for (var, val) in get_guesses(sys)) assertions = Dict(transform(condition) => msg for (condition, msg) in get_assertions(sys)) - # TODO: handle subsystems # 5) Recreate system with transformed fields return typeof(sys)( eqs, iv2; observed, initialization_eqs, parameter_dependencies, defaults, guesses, assertions, name = nameof(sys), description = description(sys), kwargs... - ) |> complete # original system had to be complete + ) |> complete # input system must be complete, so complete the output system end diff --git a/test/basic_transformations.jl b/test/basic_transformations.jl index 99a252b5b8..cfb3bf5db6 100644 --- a/test/basic_transformations.jl +++ b/test/basic_transformations.jl @@ -59,34 +59,44 @@ end end @testset "Change independent variable (Friedmann equation)" begin + @independent_variables t D = Differential(t) - @variables a(t) ȧ(t) ρr(t) ρm(t) ρΛ(t) ρ(t) P(t) ϕ(t) - @parameters Ωr0 Ωm0 ΩΛ0 + @variables a(t) ȧ(t) Ω(t) ϕ(t) eqs = [ - ρr ~ 3/(8*Num(π)) * Ωr0 / a^4 - ρm ~ 3/(8*Num(π)) * Ωm0 / a^3 - ρΛ ~ 3/(8*Num(π)) * ΩΛ0 - ρ ~ ρr + ρm + ρΛ - ȧ ~ √(8*Num(π)/3*ρ*a^4) + Ω ~ 123 D(a) ~ ȧ + ȧ ~ √(Ω) * a^2 D(D(ϕ)) ~ -3*D(a)/a*D(ϕ) ] M1 = ODESystem(eqs, t; name = :M) |> complete # Apply in two steps, where derivatives are defined at each step: first t -> a, then a -> b - M2 = change_independent_variable(M1, M1.a) |> complete #, D(b) ~ D(a)/a; verbose = true) + M2 = change_independent_variable(M1, M1.a; dummies = true) + @independent_variables a + @variables ȧ(a) Ω(a) ϕ(a) a_t(a) a_tt(a) + Da = Differential(a) + @test Set(equations(M2)) == Set([ + a_tt*Da(ϕ) + a_t^2*(Da^2)(ϕ) ~ -3*a_t^2/a*Da(ϕ) + ȧ ~ √(Ω) * a^2 + Ω ~ 123 + a_t ~ ȧ # 1st order dummy equation + a_tt ~ Da(ȧ) * a_t # 2nd order dummy equation + ]) + @variables b(M2.a) M3 = change_independent_variable(M2, b, [Differential(M2.a)(b) ~ exp(-b), M2.a ~ exp(b)]) + + M1 = structural_simplify(M1) M2 = structural_simplify(M2; allow_symbolic = true) M3 = structural_simplify(M3; allow_symbolic = true) - @test length(unknowns(M2)) == 2 && length(unknowns(M3)) == 2 + @test length(unknowns(M3)) == length(unknowns(M2)) == length(unknowns(M1)) - 1 end @testset "Change independent variable (simple)" begin @variables x(t) - Mt = ODESystem([D(x) ~ 2*x], t; name = :M) |> complete # TODO: avoid complete. can avoid it if passing defined $variable directly to change_independent_variable + Mt = ODESystem([D(x) ~ 2*x], t; name = :M) |> complete Mx = change_independent_variable(Mt, Mt.x; dummies = true) - @test (@variables x x_t(x) x_tt(x); Set(equations(Mx)) == Set([x_t ~ 2x, x_tt ~ 4x])) + @test (@variables x x_t(x) x_tt(x); Set(equations(Mx)) == Set([x_t ~ 2*x, x_tt ~ 2*x_t])) end @testset "Change independent variable (free fall)" begin @@ -101,10 +111,25 @@ end @test all(isapprox.(sol[Mx.y], sol[Mx.x - g*(Mx.x/v)^2/2]; atol = 1e-10)) # compare to analytical solution (x(t) = v*t, y(t) = v*t - g*t^2/2) end -@testset "Change independent variable (autonomous system)" begin - M = ODESystem([D(x) ~ t], t; name = :M) |> complete # non-autonomous - @test_throws "t ~ F(x(t)) must be provided" change_independent_variable(M, M.x) - @test_nowarn change_independent_variable(M, M.x, [t ~ 2*x]) +@testset "Change independent variable (crazy analytical example)" begin + @independent_variables t + D = Differential(t) + @variables x(t) y(t) + M1 = ODESystem([ # crazy non-autonomous non-linear 2nd order ODE + D(D(y)) ~ D(x)^2 + D(y^3) |> expand_derivatives # expand D(y^3) # TODO: make this test 3rd order + D(x) ~ x^4 + y^5 + t^6 + ], t; name = :M) |> complete + M2 = change_independent_variable(M1, M1.x; dummies = true) + + # Compare to pen-and-paper result + @independent_variables x + Dx = Differential(x) + @variables x_t(x) x_tt(x) y(x) t(x) + @test Set(equations(M2)) == Set([ + x_t^2*(Dx^2)(y) + x_tt*Dx(y) ~ x_t^2 + 3*y^2*Dx(y)*x_t # from D(D(y)) + x_t ~ x^4 + y^5 + t^6 # 1st order dummy equation + x_tt ~ 4*x^3*x_t + 5*y^4*Dx(y)*x_t + 6*t^5 # 2nd order dummy equation + ]) end @testset "Change independent variable (errors)" begin From 34a6a4ee655c68b93904b7cf04ca3e9a3bab28b7 Mon Sep 17 00:00:00 2001 From: Herman Sletmoen Date: Wed, 5 Mar 2025 12:25:06 +0100 Subject: [PATCH 1078/2176] Actually run basic transformations test --- test/runtests.jl | 1 + 1 file changed, 1 insertion(+) diff --git a/test/runtests.jl b/test/runtests.jl index 0ae66d3755..8f1f2a0be4 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -46,6 +46,7 @@ end @safetestset "Model Parsing Test" include("model_parsing.jl") @safetestset "Error Handling" include("error_handling.jl") @safetestset "StructuralTransformations" include("structural_transformation/runtests.jl") + @safetestset "Basic transformations" include("basic_transformations.jl") @safetestset "State Selection Test" include("state_selection.jl") @safetestset "Symbolic Event Test" include("symbolic_events.jl") @safetestset "Stream Connect Test" include("stream_connectors.jl") From 636ad045cebbf88f6755c9bd4b481ed9ee5c748b Mon Sep 17 00:00:00 2001 From: Herman Sletmoen Date: Wed, 5 Mar 2025 21:41:05 +0100 Subject: [PATCH 1079/2176] Change independent variable of hierarchical systems --- .../tutorials/change_independent_variable.md | 43 ++++++++----- src/systems/diffeqs/basic_transformations.jl | 63 ++++++++++++------- test/basic_transformations.jl | 25 +++++--- 3 files changed, 83 insertions(+), 48 deletions(-) diff --git a/docs/src/tutorials/change_independent_variable.md b/docs/src/tutorials/change_independent_variable.md index 0d4d8bcac8..e49ededacd 100644 --- a/docs/src/tutorials/change_independent_variable.md +++ b/docs/src/tutorials/change_independent_variable.md @@ -42,7 +42,7 @@ There are at least three ways of answering this: We will demonstrate the last method by changing the independent variable from $t$ to $x$. This transformation is well-defined for any non-zero horizontal velocity $v$. ```@example changeivar -M2 = change_independent_variable(M1, M1.x; dummies = true) |> complete +M2 = change_independent_variable(M1, M1.x; dummies = true) @assert M2.x isa Num # hide M2s = structural_simplify(M2; allow_symbolic = true) ``` @@ -56,29 +56,38 @@ sol = solve(prob, Tsit5()) plot(sol; idxs = M2.y) # must index by M2.y = y(x); not M1.y = y(t)! ``` -## 2. Reduce stiffness by changing to a logarithmic time axis +## 2. Alleviating stiffness by changing to logarithmic time In cosmology, the [Friedmann equations](https://en.wikipedia.org/wiki/Friedmann_equations) describe the expansion of the universe. In terms of conformal time $t$, they can be written ```@example changeivar -@variables a(t) Ω(t) Ωr(t) Ωm(t) ΩΛ(t) -M1 = ODESystem([ +@variables a(t) Ω(t) +a = GlobalScope(a) # global var needed by all species +function species(w; kw...) + eqs = [D(Ω) ~ -3(1 + w) * D(a)/a * Ω] + return ODESystem(eqs, t, [Ω], []; kw...) +end +@named r = species(1//3) # radiation +@named m = species(0) # matter +@named Λ = species(-1) # dark energy / cosmological constant +eqs = [ + Ω ~ r.Ω + m.Ω + Λ.Ω # total energy density D(a) ~ √(Ω) * a^2 - Ω ~ Ωr + Ωm + ΩΛ - D(Ωm) ~ -3 * D(a)/a * Ωm - D(Ωr) ~ -4 * D(a)/a * Ωr - D(ΩΛ) ~ 0 -], t; initialization_eqs = [ - ΩΛ + Ωr + Ωm ~ 1 -], name = :M) |> complete +] +initialization_eqs = [ + Λ.Ω + r.Ω + m.Ω ~ 1 +] +M1 = ODESystem(eqs, t, [Ω, a], []; initialization_eqs, name = :M) +M1 = compose(M1, r, m, Λ) +M1 = complete(M1; flatten = false) M1s = structural_simplify(M1) ``` Of course, we can solve this ODE as it is: ```@example changeivar -prob = ODEProblem(M1s, [M1.a => 1.0, M1.Ωr => 5e-5, M1.Ωm => 0.3], (0.0, -3.3), []) +prob = ODEProblem(M1s, [M1.a => 1.0, M1.r.Ω => 5e-5, M1.m.Ω => 0.3], (0.0, -3.3), []) sol = solve(prob, Tsit5(); reltol = 1e-5) @assert Symbol(sol.retcode) == :Unstable # surrounding text assumes this was unstable # hide -plot(sol, idxs = [M1.a, M1.Ωr/M1.Ω, M1.Ωm/M1.Ω, M1.ΩΛ/M1.Ω]) +plot(sol, idxs = [M1.a, M1.r.Ω/M1.Ω, M1.m.Ω/M1.Ω, M1.Λ.Ω/M1.Ω]) ``` The solver becomes unstable due to stiffness. Also notice the interesting dynamics taking place towards the end of the integration (in the early universe), which gets compressed into a very small time interval. @@ -89,7 +98,7 @@ To do this, we will change the independent variable in two stages; from $t$ to $ Notice that $\mathrm{d}a/\mathrm{d}t > 0$ provided that $\Omega > 0$, and $\mathrm{d}b/\mathrm{d}a > 0$, so the transformation is well-defined. First, we transform from $t$ to $a$: ```@example changeivar -M2 = change_independent_variable(M1, M1.a; dummies = true) |> complete +M2 = change_independent_variable(M1, M1.a; dummies = true) ``` Unlike the original, notice that this system is *non-autonomous* because the independent variable $a$ appears explicitly in the equations! This means that to change the independent variable from $a$ to $b$, we must provide not only the rate of change relation $db(a)/da = \exp(-b)$, but *also* the equation $a(b) = \exp(b)$ so $a$ can be eliminated in favor of $b$: @@ -97,14 +106,14 @@ This means that to change the independent variable from $a$ to $b$, we must prov a = M2.a Da = Differential(a) @variables b(a) -M3 = change_independent_variable(M2, b, [Da(b) ~ exp(-b), a ~ exp(b)]) |> complete +M3 = change_independent_variable(M2, b, [Da(b) ~ exp(-b), a ~ exp(b)]) ``` We can now solve and plot the ODE in terms of $b$: ```@example changeivar M3s = structural_simplify(M3; allow_symbolic = true) -prob = ODEProblem(M3s, [M3.Ωr => 5e-5, M3.Ωm => 0.3], (0, -15), []) +prob = ODEProblem(M3s, [M3.r.Ω => 5e-5, M3.m.Ω => 0.3], (0, -15), []) sol = solve(prob, Tsit5()) @assert Symbol(sol.retcode) == :Success # surrounding text assumes the solution was successful # hide -plot(sol, idxs = [M3.Ωr/M3.Ω, M3.Ωm/M3.Ω, M3.ΩΛ/M3.Ω]) +plot(sol, idxs = [M3.r.Ω/M3.Ω, M3.m.Ω/M3.Ω, M3.Λ.Ω/M3.Ω]) ``` Notice that the variables vary "more nicely" with respect to $b$ than $t$, making the solver happier and avoiding numerical problems. diff --git a/src/systems/diffeqs/basic_transformations.jl b/src/systems/diffeqs/basic_transformations.jl index 11a5514f42..a8f3db11e4 100644 --- a/src/systems/diffeqs/basic_transformations.jl +++ b/src/systems/diffeqs/basic_transformations.jl @@ -77,6 +77,11 @@ Usage with non-autonomous systems If `sys` is autonomous (i.e. ``t`` appears explicitly in its equations), it is often desirable to also pass an algebraic equation relating the new and old independent variables (e.g. ``t = f(u(t))``). Otherwise the transformed system will be underdetermined and cannot be structurally simplified without additional changes. +Usage with hierarchical systems +=============================== +It is recommended that `iv` is a non-namespaced variable in `sys`. +This means it can belong to the top-level system or be a variable in a subsystem declared with `GlobalScope`. + Example ======= Consider a free fall with constant horizontal velocity. @@ -102,8 +107,6 @@ function change_independent_variable(sys::AbstractODESystem, iv, eqs = []; dummi error("System $(nameof(sys)) is incomplete. Complete it first!") elseif isscheduled(sys) error("System $(nameof(sys)) is structurally simplified. Change independent variable before structural simplification!") - elseif !isempty(get_systems(sys)) - error("System $(nameof(sys)) is hierarchical. Flatten it first!") # TODO: implement and allow? elseif !iscall(iv2_of_iv1) || !isequal(only(arguments(iv2_of_iv1)), iv1) error("Variable $iv is not a function of the independent variable $iv1 of the system $(nameof(sys))!") end @@ -112,6 +115,7 @@ function change_independent_variable(sys::AbstractODESystem, iv, eqs = []; dummi iv2name = nameof(operation(iv2_of_iv1)) # e.g. :u iv2, = @independent_variables $iv2name # e.g. u iv1_of_iv2, = @variables $iv1name(iv2) # inverse in case sys is autonomous; e.g. t(u) + iv1_of_iv2 = GlobalScope(iv1_of_iv2) # do not namespace old independent variable as new dependent variable D1 = Differential(iv1) # e.g. d/d(t) D2 = Differential(iv2_of_iv1) # e.g. d/d(u(t)) @@ -119,10 +123,9 @@ function change_independent_variable(sys::AbstractODESystem, iv, eqs = []; dummi function chain_rule(ex) vars = get_variables(ex) for var_of_iv1 in vars # loop through e.g. f(t) - if iscall(var_of_iv1) && !isequal(var_of_iv1, iv2_of_iv1) # handle e.g. f(t) -> f(u(t)), but not u(t) -> u(u(t)) - varname = nameof(operation(var_of_iv1)) # e.g. :f - var_of_iv2, = @variables $varname(iv2_of_iv1) # e.g. f(u(t)) - ex = substitute(ex, var_of_iv1 => var_of_iv2; fold) # e.g. f(t) -> f(u(t)) + if iscall(var_of_iv1) && isequal(only(arguments(var_of_iv1)), iv1) && !isequal(var_of_iv1, iv2_of_iv1) # handle e.g. f(t) -> f(u(t)), but not u(t) -> u(u(t)) + var_of_iv2 = substitute(var_of_iv1, iv1 => iv2_of_iv1) # e.g. f(t) -> f(u(t)) + ex = substitute(ex, var_of_iv1 => var_of_iv2) end end ex = expand_derivatives(ex, simplify) # expand chain rule, e.g. (d/dt)(f(u(t)))) -> df(u(t))/du(t) * du(t)/dt @@ -130,7 +133,7 @@ function change_independent_variable(sys::AbstractODESystem, iv, eqs = []; dummi end # 2) Find e.g. du/dt in equations, then calculate e.g. d²u/dt², ... - eqs = [get_eqs(sys); eqs] # all equations (system-defined + user-provided) we may use + eqs = [eqs; get_eqs(sys)] # all equations (system-defined + user-provided) we may use idxs = findall(eq -> isequal(eq.lhs, D1(iv2_of_iv1)), eqs) if length(idxs) != 1 error("Exactly one equation for $D1($iv2_of_iv1) was not specified!") @@ -148,11 +151,14 @@ function change_independent_variable(sys::AbstractODESystem, iv, eqs = []; dummi div2name = Symbol(iv2name, :_, iv1name) # e.g. :u_t # TODO: customize ddiv2name = Symbol(div2name, iv1name) # e.g. :u_tt # TODO: customize div2, ddiv2 = @variables $div2name(iv2) $ddiv2name(iv2) # e.g. u_t(u), u_tt(u) - eqs = [eqs; [div2 ~ div2_of_iv1, ddiv2 ~ ddiv2_of_iv1]] # add dummy equations + div2, ddiv2 = GlobalScope.([div2, ddiv2]) # do not namespace dummies in new system + eqs = [[div2 ~ div2_of_iv1, ddiv2 ~ ddiv2_of_iv1]; eqs] # add dummy equations + @set! sys.unknowns = [get_unknowns(sys); [div2, ddiv2]] # add dummy variables else div2 = div2_of_iv1 ddiv2 = ddiv2_of_iv1 end + @set! sys.eqs = eqs # add extra equations we derived before starting transformation process # 4) Transform everything from old to new independent variable, e.g. t -> u. # Substitution order matters! Must begin with highest order to get D(D(u(t))) -> u_tt(u). @@ -170,18 +176,31 @@ function change_independent_variable(sys::AbstractODESystem, iv, eqs = []; dummi end return ex end - eqs = map(transform, eqs) # we derived and added equations to eqs; they are not in get_eqs(sys)! - observed = map(transform, get_observed(sys)) - initialization_eqs = map(transform, get_initialization_eqs(sys)) - parameter_dependencies = map(transform, get_parameter_dependencies(sys)) - defaults = Dict(transform(var) => transform(val) for (var, val) in get_defaults(sys)) - guesses = Dict(transform(var) => transform(val) for (var, val) in get_guesses(sys)) - assertions = Dict(transform(condition) => msg for (condition, msg) in get_assertions(sys)) - - # 5) Recreate system with transformed fields - return typeof(sys)( - eqs, iv2; - observed, initialization_eqs, parameter_dependencies, defaults, guesses, assertions, - name = nameof(sys), description = description(sys), kwargs... - ) |> complete # input system must be complete, so complete the output system + function transform(sys::AbstractODESystem) + eqs = map(transform, get_eqs(sys)) + unknowns = map(transform, get_unknowns(sys)) + unknowns = filter(var -> !isequal(var, iv2), unknowns) # remove e.g. u + ps = map(transform, get_ps(sys)) + ps = filter(!isinitial, ps) # remove Initial(...) # # TODO: shouldn't have to touch this + observed = map(transform, get_observed(sys)) + initialization_eqs = map(transform, get_initialization_eqs(sys)) + parameter_dependencies = map(transform, get_parameter_dependencies(sys)) + defaults = Dict(transform(var) => transform(val) for (var, val) in get_defaults(sys)) + guesses = Dict(transform(var) => transform(val) for (var, val) in get_guesses(sys)) + assertions = Dict(transform(condition) => msg for (condition, msg) in get_assertions(sys)) + systems = get_systems(sys) # save before reconstructing system + wascomplete = iscomplete(sys) # save before reconstructing system + sys = typeof(sys)( # recreate system with transformed fields + eqs, iv2, unknowns, ps; observed, initialization_eqs, parameter_dependencies, defaults, guesses, + assertions, name = nameof(sys), description = description(sys), kwargs... + ) + systems = map(transform, systems) # recurse through subsystems + sys = compose(sys, systems) # rebuild hierarchical system + if wascomplete + wasflat = isempty(systems) + sys = complete(sys; flatten = wasflat) # complete output if input was complete + end + return sys + end + return transform(sys) end diff --git a/test/basic_transformations.jl b/test/basic_transformations.jl index cfb3bf5db6..4fed453390 100644 --- a/test/basic_transformations.jl +++ b/test/basic_transformations.jl @@ -62,25 +62,34 @@ end @independent_variables t D = Differential(t) @variables a(t) ȧ(t) Ω(t) ϕ(t) + a, ȧ = GlobalScope.([a, ȧ]) + species(w; kw...) = ODESystem([D(Ω) ~ -3(1 + w) * D(a)/a * Ω], t, [Ω], []; kw...) + @named r = species(1//3) + @named m = species(0) + @named Λ = species(-1) eqs = [ - Ω ~ 123 + Ω ~ r.Ω + m.Ω + Λ.Ω D(a) ~ ȧ ȧ ~ √(Ω) * a^2 D(D(ϕ)) ~ -3*D(a)/a*D(ϕ) ] - M1 = ODESystem(eqs, t; name = :M) |> complete + M1 = ODESystem(eqs, t, [Ω, a, ȧ, ϕ], []; name = :M) + M1 = compose(M1, r, m, Λ) + M1 = complete(M1; flatten = false) # Apply in two steps, where derivatives are defined at each step: first t -> a, then a -> b M2 = change_independent_variable(M1, M1.a; dummies = true) - @independent_variables a - @variables ȧ(a) Ω(a) ϕ(a) a_t(a) a_tt(a) + a, ȧ, Ω, Ωr, Ωm, ΩΛ, ϕ, a_t, a_tt = M2.a, M2.ȧ, M2.Ω, M2.r.Ω, M2.m.Ω, M2.Λ.Ω, M2.ϕ, M2.a_t, M2.a_tt Da = Differential(a) @test Set(equations(M2)) == Set([ - a_tt*Da(ϕ) + a_t^2*(Da^2)(ϕ) ~ -3*a_t^2/a*Da(ϕ) - ȧ ~ √(Ω) * a^2 - Ω ~ 123 a_t ~ ȧ # 1st order dummy equation a_tt ~ Da(ȧ) * a_t # 2nd order dummy equation + Ω ~ Ωr + Ωm + ΩΛ + ȧ ~ √(Ω) * a^2 + a_tt*Da(ϕ) + a_t^2*(Da^2)(ϕ) ~ -3*a_t^2/a*Da(ϕ) + a_t*Da(Ωr) ~ -4*Ωr*a_t/a + a_t*Da(Ωm) ~ -3*Ωm*a_t/a + a_t*Da(ΩΛ) ~ 0 ]) @variables b(M2.a) @@ -143,6 +152,4 @@ end @test_throws "not specified" change_independent_variable(M, v) @test_throws "not a function of the independent variable" change_independent_variable(M, y) @test_throws "not a function of the independent variable" change_independent_variable(M, z) - M = compose(M, M) - @test_throws "hierarchical" change_independent_variable(M, M.x) end From 2afacb5c696c2e760fd62597b427fae21e19a560 Mon Sep 17 00:00:00 2001 From: Herman Sletmoen Date: Thu, 6 Mar 2025 18:34:25 +0100 Subject: [PATCH 1080/2176] Forbid DDEs for now --- src/systems/diffeqs/basic_transformations.jl | 2 ++ test/basic_transformations.jl | 4 ++++ 2 files changed, 6 insertions(+) diff --git a/src/systems/diffeqs/basic_transformations.jl b/src/systems/diffeqs/basic_transformations.jl index a8f3db11e4..0df7a5d51d 100644 --- a/src/systems/diffeqs/basic_transformations.jl +++ b/src/systems/diffeqs/basic_transformations.jl @@ -105,6 +105,8 @@ function change_independent_variable(sys::AbstractODESystem, iv, eqs = []; dummi if !iscomplete(sys) error("System $(nameof(sys)) is incomplete. Complete it first!") + elseif is_dde(sys) + error("System $(nameof(sys)) contains delay differential equations (DDEs). This is currently not supported!") elseif isscheduled(sys) error("System $(nameof(sys)) is structurally simplified. Change independent variable before structural simplification!") elseif !iscall(iv2_of_iv1) || !isequal(only(arguments(iv2_of_iv1)), iv1) diff --git a/test/basic_transformations.jl b/test/basic_transformations.jl index 4fed453390..798548ff44 100644 --- a/test/basic_transformations.jl +++ b/test/basic_transformations.jl @@ -152,4 +152,8 @@ end @test_throws "not specified" change_independent_variable(M, v) @test_throws "not a function of the independent variable" change_independent_variable(M, y) @test_throws "not a function of the independent variable" change_independent_variable(M, z) + M = ODESystem([D(x) ~ 0, v ~ x], t; name = :M) + @variables x(..) # require explicit argument + M = ODESystem([D(x(t)) ~ x(t-1)], t; name = :M) |> complete + @test_throws "DDE" change_independent_variable(M, M.x) end From 2d5aa12109c8b494e5f4af975e09a63630e14fce Mon Sep 17 00:00:00 2001 From: Herman Sletmoen Date: Thu, 6 Mar 2025 18:48:29 +0100 Subject: [PATCH 1081/2176] Use vars(ex; op = Nothing) instead of get_variables(ex) --- src/systems/diffeqs/basic_transformations.jl | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/systems/diffeqs/basic_transformations.jl b/src/systems/diffeqs/basic_transformations.jl index 0df7a5d51d..98c50758c2 100644 --- a/src/systems/diffeqs/basic_transformations.jl +++ b/src/systems/diffeqs/basic_transformations.jl @@ -123,8 +123,7 @@ function change_independent_variable(sys::AbstractODESystem, iv, eqs = []; dummi # 1) Utility that performs the chain rule on an expression, e.g. (d/dt)(f(t)) -> (d/dt)(f(u(t))) -> df(u(t))/du(t) * du(t)/dt function chain_rule(ex) - vars = get_variables(ex) - for var_of_iv1 in vars # loop through e.g. f(t) + for var_of_iv1 in vars(ex; op = Nothing) # loop over all variables in expression, e.g. f(t) (op = Nothing so "D(f(t))" yields only "f(t)" and not "D(f(t))"; we want to replace *inside* derivative signs) if iscall(var_of_iv1) && isequal(only(arguments(var_of_iv1)), iv1) && !isequal(var_of_iv1, iv2_of_iv1) # handle e.g. f(t) -> f(u(t)), but not u(t) -> u(u(t)) var_of_iv2 = substitute(var_of_iv1, iv1 => iv2_of_iv1) # e.g. f(t) -> f(u(t)) ex = substitute(ex, var_of_iv1 => var_of_iv2) From 5780d91fd26f5fdcdc48980bc22b0a318284a2f1 Mon Sep 17 00:00:00 2001 From: Herman Sletmoen Date: Thu, 6 Mar 2025 19:04:34 +0100 Subject: [PATCH 1082/2176] Print detected transformation equations when there is not exactly one --- src/systems/diffeqs/basic_transformations.jl | 2 +- test/basic_transformations.jl | 6 ++++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/systems/diffeqs/basic_transformations.jl b/src/systems/diffeqs/basic_transformations.jl index 98c50758c2..3d9d92b13f 100644 --- a/src/systems/diffeqs/basic_transformations.jl +++ b/src/systems/diffeqs/basic_transformations.jl @@ -137,7 +137,7 @@ function change_independent_variable(sys::AbstractODESystem, iv, eqs = []; dummi eqs = [eqs; get_eqs(sys)] # all equations (system-defined + user-provided) we may use idxs = findall(eq -> isequal(eq.lhs, D1(iv2_of_iv1)), eqs) if length(idxs) != 1 - error("Exactly one equation for $D1($iv2_of_iv1) was not specified!") + error("Exactly one equation for $D1($iv2_of_iv1) was not specified! Got $(length(idxs)) equations:\n", join(eqs[idxs], '\n')) end div2_of_iv1_eq = popat!(eqs, only(idxs)) # get and remove e.g. du/dt = ... (may be added back later as a dummy) div2_of_iv1 = chain_rule(div2_of_iv1_eq.rhs) diff --git a/test/basic_transformations.jl b/test/basic_transformations.jl index 798548ff44..b1d66fc9ee 100644 --- a/test/basic_transformations.jl +++ b/test/basic_transformations.jl @@ -148,8 +148,10 @@ end M = complete(M) @test_throws "singular" change_independent_variable(M, M.x) @test_throws "structurally simplified" change_independent_variable(structural_simplify(M), y) - @test_throws "not specified" change_independent_variable(M, w) - @test_throws "not specified" change_independent_variable(M, v) + @test_throws "Got 0 equations:" change_independent_variable(M, w) + @test_throws "Got 0 equations:" change_independent_variable(M, v) + M = ODESystem([D(x) ~ 1, v ~ 1], t; name = :M) |> complete + @test_throws "Got 2 equations:" change_independent_variable(M, M.x, [D(x) ~ 2]) @test_throws "not a function of the independent variable" change_independent_variable(M, y) @test_throws "not a function of the independent variable" change_independent_variable(M, z) M = ODESystem([D(x) ~ 0, v ~ x], t; name = :M) From 5ca058d64b1918c40ec910acdf652e8263f537d6 Mon Sep 17 00:00:00 2001 From: Herman Sletmoen Date: Thu, 6 Mar 2025 19:38:33 +0100 Subject: [PATCH 1083/2176] Use default_toterm (with special underscore) for dummies --- src/systems/diffeqs/basic_transformations.jl | 5 ++-- test/basic_transformations.jl | 24 ++++++++++---------- 2 files changed, 14 insertions(+), 15 deletions(-) diff --git a/src/systems/diffeqs/basic_transformations.jl b/src/systems/diffeqs/basic_transformations.jl index 3d9d92b13f..7ed3c16049 100644 --- a/src/systems/diffeqs/basic_transformations.jl +++ b/src/systems/diffeqs/basic_transformations.jl @@ -149,9 +149,8 @@ function change_independent_variable(sys::AbstractODESystem, iv, eqs = []; dummi # 3) If requested, insert extra dummy equations for e.g. du/dt, d²u/dt², ... # Otherwise, replace all these derivatives by their explicit expressions if dummies - div2name = Symbol(iv2name, :_, iv1name) # e.g. :u_t # TODO: customize - ddiv2name = Symbol(div2name, iv1name) # e.g. :u_tt # TODO: customize - div2, ddiv2 = @variables $div2name(iv2) $ddiv2name(iv2) # e.g. u_t(u), u_tt(u) + div2 = substitute(default_toterm(D1(iv2_of_iv1)), iv1 => iv2) # e.g. uˍt(u) + ddiv2 = substitute(default_toterm(D1(D1(iv2_of_iv1))), iv1 => iv2) # e.g. uˍtt(u) div2, ddiv2 = GlobalScope.([div2, ddiv2]) # do not namespace dummies in new system eqs = [[div2 ~ div2_of_iv1, ddiv2 ~ ddiv2_of_iv1]; eqs] # add dummy equations @set! sys.unknowns = [get_unknowns(sys); [div2, ddiv2]] # add dummy variables diff --git a/test/basic_transformations.jl b/test/basic_transformations.jl index b1d66fc9ee..7a8c063283 100644 --- a/test/basic_transformations.jl +++ b/test/basic_transformations.jl @@ -79,17 +79,17 @@ end # Apply in two steps, where derivatives are defined at each step: first t -> a, then a -> b M2 = change_independent_variable(M1, M1.a; dummies = true) - a, ȧ, Ω, Ωr, Ωm, ΩΛ, ϕ, a_t, a_tt = M2.a, M2.ȧ, M2.Ω, M2.r.Ω, M2.m.Ω, M2.Λ.Ω, M2.ϕ, M2.a_t, M2.a_tt + a, ȧ, Ω, Ωr, Ωm, ΩΛ, ϕ, aˍt, aˍtt = M2.a, M2.ȧ, M2.Ω, M2.r.Ω, M2.m.Ω, M2.Λ.Ω, M2.ϕ, M2.aˍt, M2.aˍtt Da = Differential(a) @test Set(equations(M2)) == Set([ - a_t ~ ȧ # 1st order dummy equation - a_tt ~ Da(ȧ) * a_t # 2nd order dummy equation + aˍt ~ ȧ # 1st order dummy equation + aˍtt ~ Da(ȧ) * aˍt # 2nd order dummy equation Ω ~ Ωr + Ωm + ΩΛ ȧ ~ √(Ω) * a^2 - a_tt*Da(ϕ) + a_t^2*(Da^2)(ϕ) ~ -3*a_t^2/a*Da(ϕ) - a_t*Da(Ωr) ~ -4*Ωr*a_t/a - a_t*Da(Ωm) ~ -3*Ωm*a_t/a - a_t*Da(ΩΛ) ~ 0 + aˍtt*Da(ϕ) + aˍt^2*(Da^2)(ϕ) ~ -3*aˍt^2/a*Da(ϕ) + aˍt*Da(Ωr) ~ -4*Ωr*aˍt/a + aˍt*Da(Ωm) ~ -3*Ωm*aˍt/a + aˍt*Da(ΩΛ) ~ 0 ]) @variables b(M2.a) @@ -105,7 +105,7 @@ end @variables x(t) Mt = ODESystem([D(x) ~ 2*x], t; name = :M) |> complete Mx = change_independent_variable(Mt, Mt.x; dummies = true) - @test (@variables x x_t(x) x_tt(x); Set(equations(Mx)) == Set([x_t ~ 2*x, x_tt ~ 2*x_t])) + @test (@variables x xˍt(x) xˍtt(x); Set(equations(Mx)) == Set([xˍt ~ 2*x, xˍtt ~ 2*xˍt])) end @testset "Change independent variable (free fall)" begin @@ -133,11 +133,11 @@ end # Compare to pen-and-paper result @independent_variables x Dx = Differential(x) - @variables x_t(x) x_tt(x) y(x) t(x) + @variables xˍt(x) xˍtt(x) y(x) t(x) @test Set(equations(M2)) == Set([ - x_t^2*(Dx^2)(y) + x_tt*Dx(y) ~ x_t^2 + 3*y^2*Dx(y)*x_t # from D(D(y)) - x_t ~ x^4 + y^5 + t^6 # 1st order dummy equation - x_tt ~ 4*x^3*x_t + 5*y^4*Dx(y)*x_t + 6*t^5 # 2nd order dummy equation + xˍt^2*(Dx^2)(y) + xˍtt*Dx(y) ~ xˍt^2 + 3*y^2*Dx(y)*xˍt # from D(D(y)) + xˍt ~ x^4 + y^5 + t^6 # 1st order dummy equation + xˍtt ~ 4*x^3*xˍt + 5*y^4*Dx(y)*xˍt + 6*t^5 # 2nd order dummy equation ]) end From 2397d9a22cda11008f0288d50e1b23a4d24b80b5 Mon Sep 17 00:00:00 2001 From: Herman Sletmoen Date: Thu, 6 Mar 2025 20:18:27 +0100 Subject: [PATCH 1084/2176] Change independent variable of incomplete systems --- .../tutorials/change_independent_variable.md | 11 +++--- src/systems/diffeqs/basic_transformations.jl | 4 +-- test/basic_transformations.jl | 36 +++++++++---------- 3 files changed, 23 insertions(+), 28 deletions(-) diff --git a/docs/src/tutorials/change_independent_variable.md b/docs/src/tutorials/change_independent_variable.md index e49ededacd..f196c3cb64 100644 --- a/docs/src/tutorials/change_independent_variable.md +++ b/docs/src/tutorials/change_independent_variable.md @@ -24,11 +24,11 @@ D = Differential(t) M1 = ODESystem([ D(D(y)) ~ -g, D(x) ~ v # constant horizontal velocity ], t; defaults = [ - y => 0.0 + y => 0.0 # start on the ground ], initialization_eqs = [ #x ~ 0.0, # TODO: handle? # hide D(x) ~ D(y) # equal initial horizontal and vertical velocity (45 °) -], name = :M) |> complete +], name = :M) M1s = structural_simplify(M1) ``` This is the standard parametrization that arises naturally from kinematics and Newton's laws. @@ -42,7 +42,7 @@ There are at least three ways of answering this: We will demonstrate the last method by changing the independent variable from $t$ to $x$. This transformation is well-defined for any non-zero horizontal velocity $v$. ```@example changeivar -M2 = change_independent_variable(M1, M1.x; dummies = true) +M2 = change_independent_variable(M1, x; dummies = true) @assert M2.x isa Num # hide M2s = structural_simplify(M2; allow_symbolic = true) ``` @@ -79,12 +79,11 @@ initialization_eqs = [ ] M1 = ODESystem(eqs, t, [Ω, a], []; initialization_eqs, name = :M) M1 = compose(M1, r, m, Λ) -M1 = complete(M1; flatten = false) M1s = structural_simplify(M1) ``` Of course, we can solve this ODE as it is: ```@example changeivar -prob = ODEProblem(M1s, [M1.a => 1.0, M1.r.Ω => 5e-5, M1.m.Ω => 0.3], (0.0, -3.3), []) +prob = ODEProblem(M1s, [M1s.a => 1.0, M1s.r.Ω => 5e-5, M1s.m.Ω => 0.3], (0.0, -3.3), []) sol = solve(prob, Tsit5(); reltol = 1e-5) @assert Symbol(sol.retcode) == :Unstable # surrounding text assumes this was unstable # hide plot(sol, idxs = [M1.a, M1.r.Ω/M1.Ω, M1.m.Ω/M1.Ω, M1.Λ.Ω/M1.Ω]) @@ -111,7 +110,7 @@ M3 = change_independent_variable(M2, b, [Da(b) ~ exp(-b), a ~ exp(b)]) We can now solve and plot the ODE in terms of $b$: ```@example changeivar M3s = structural_simplify(M3; allow_symbolic = true) -prob = ODEProblem(M3s, [M3.r.Ω => 5e-5, M3.m.Ω => 0.3], (0, -15), []) +prob = ODEProblem(M3s, [M3s.r.Ω => 5e-5, M3s.m.Ω => 0.3], (0, -15), []) sol = solve(prob, Tsit5()) @assert Symbol(sol.retcode) == :Success # surrounding text assumes the solution was successful # hide plot(sol, idxs = [M3.r.Ω/M3.Ω, M3.m.Ω/M3.Ω, M3.Λ.Ω/M3.Ω]) diff --git a/src/systems/diffeqs/basic_transformations.jl b/src/systems/diffeqs/basic_transformations.jl index 7ed3c16049..a6ce1fcc14 100644 --- a/src/systems/diffeqs/basic_transformations.jl +++ b/src/systems/diffeqs/basic_transformations.jl @@ -103,9 +103,7 @@ function change_independent_variable(sys::AbstractODESystem, iv, eqs = []; dummi iv2_of_iv1 = unwrap(iv) # e.g. u(t) iv1 = get_iv(sys) # e.g. t - if !iscomplete(sys) - error("System $(nameof(sys)) is incomplete. Complete it first!") - elseif is_dde(sys) + if is_dde(sys) error("System $(nameof(sys)) contains delay differential equations (DDEs). This is currently not supported!") elseif isscheduled(sys) error("System $(nameof(sys)) is structurally simplified. Change independent variable before structural simplification!") diff --git a/test/basic_transformations.jl b/test/basic_transformations.jl index 7a8c063283..0aa679a817 100644 --- a/test/basic_transformations.jl +++ b/test/basic_transformations.jl @@ -28,8 +28,8 @@ end @testset "Change independent variable (trivial)" begin @variables x(t) y(t) eqs1 = [D(D(x)) ~ D(x) + x, D(y) ~ 1] - M1 = ODESystem(eqs1, t; name = :M) |> complete - M2 = change_independent_variable(M1, M1.y) + M1 = ODESystem(eqs1, t; name = :M) + M2 = change_independent_variable(M1, y) eqs2 = substitute(equations(M2), M2.y => M1.t) # system should be equivalent when parametrized with y (since D(y) ~ 1), so substitute back ... @test eqs1[1] == only(eqs2) # ... and check that the equations are unmodified end @@ -43,8 +43,8 @@ end D(s) ~ 1 / (2*s) ] initialization_eqs = [x ~ 1.0, y ~ 1.0, D(y) ~ 0.0] - M1 = ODESystem(eqs, t; initialization_eqs, name = :M) |> complete - M2 = change_independent_variable(M1, M1.s; dummies = true) + M1 = ODESystem(eqs, t; initialization_eqs, name = :M) + M2 = change_independent_variable(M1, s; dummies = true) M1 = structural_simplify(M1; allow_symbolic = true) M2 = structural_simplify(M2; allow_symbolic = true) @@ -75,11 +75,11 @@ end ] M1 = ODESystem(eqs, t, [Ω, a, ȧ, ϕ], []; name = :M) M1 = compose(M1, r, m, Λ) - M1 = complete(M1; flatten = false) # Apply in two steps, where derivatives are defined at each step: first t -> a, then a -> b M2 = change_independent_variable(M1, M1.a; dummies = true) - a, ȧ, Ω, Ωr, Ωm, ΩΛ, ϕ, aˍt, aˍtt = M2.a, M2.ȧ, M2.Ω, M2.r.Ω, M2.m.Ω, M2.Λ.Ω, M2.ϕ, M2.aˍt, M2.aˍtt + M2c = complete(M2) # just for the following equation comparison (without namespacing) + a, ȧ, Ω, Ωr, Ωm, ΩΛ, ϕ, aˍt, aˍtt = M2c.a, M2c.ȧ, M2c.Ω, M2c.r.Ω, M2c.m.Ω, M2c.Λ.Ω, M2c.ϕ, M2c.aˍt, M2c.aˍtt Da = Differential(a) @test Set(equations(M2)) == Set([ aˍt ~ ȧ # 1st order dummy equation @@ -103,16 +103,16 @@ end @testset "Change independent variable (simple)" begin @variables x(t) - Mt = ODESystem([D(x) ~ 2*x], t; name = :M) |> complete - Mx = change_independent_variable(Mt, Mt.x; dummies = true) + Mt = ODESystem([D(x) ~ 2*x], t; name = :M) + Mx = change_independent_variable(Mt, x; dummies = true) @test (@variables x xˍt(x) xˍtt(x); Set(equations(Mx)) == Set([xˍt ~ 2*x, xˍtt ~ 2*xˍt])) end @testset "Change independent variable (free fall)" begin @variables x(t) y(t) @parameters g v # gravitational acceleration and constant horizontal velocity - Mt = ODESystem([D(D(y)) ~ -g, D(x) ~ v], t; name = :M) |> complete # gives (x, y) as function of t, ... - Mx = change_independent_variable(Mt, Mt.x; dummies = false) # ... but we want y as a function of x + Mt = ODESystem([D(D(y)) ~ -g, D(x) ~ v], t; name = :M) # gives (x, y) as function of t, ... + Mx = change_independent_variable(Mt, x; dummies = false) # ... but we want y as a function of x Mx = structural_simplify(Mx; allow_symbolic = true) Dx = Differential(Mx.x) prob = ODEProblem(Mx, [Mx.y => 0.0, Dx(Mx.y) => 1.0], (0.0, 20.0), [g => 9.81, v => 10.0]) # 1 = dy/dx = (dy/dt)/(dx/dt) means equal initial horizontal and vertical velocities @@ -127,8 +127,8 @@ end M1 = ODESystem([ # crazy non-autonomous non-linear 2nd order ODE D(D(y)) ~ D(x)^2 + D(y^3) |> expand_derivatives # expand D(y^3) # TODO: make this test 3rd order D(x) ~ x^4 + y^5 + t^6 - ], t; name = :M) |> complete - M2 = change_independent_variable(M1, M1.x; dummies = true) + ], t; name = :M) + M2 = change_independent_variable(M1, x; dummies = true) # Compare to pen-and-paper result @independent_variables x @@ -144,18 +144,16 @@ end @testset "Change independent variable (errors)" begin @variables x(t) y z(y) w(t) v(t) M = ODESystem([D(x) ~ 0, v ~ x], t; name = :M) - @test_throws "incomplete" change_independent_variable(M, M.x) - M = complete(M) - @test_throws "singular" change_independent_variable(M, M.x) + @test_throws "singular" change_independent_variable(M, x) @test_throws "structurally simplified" change_independent_variable(structural_simplify(M), y) @test_throws "Got 0 equations:" change_independent_variable(M, w) @test_throws "Got 0 equations:" change_independent_variable(M, v) - M = ODESystem([D(x) ~ 1, v ~ 1], t; name = :M) |> complete - @test_throws "Got 2 equations:" change_independent_variable(M, M.x, [D(x) ~ 2]) + M = ODESystem([D(x) ~ 1, v ~ 1], t; name = :M) + @test_throws "Got 2 equations:" change_independent_variable(M, x, [D(x) ~ 2]) @test_throws "not a function of the independent variable" change_independent_variable(M, y) @test_throws "not a function of the independent variable" change_independent_variable(M, z) M = ODESystem([D(x) ~ 0, v ~ x], t; name = :M) @variables x(..) # require explicit argument - M = ODESystem([D(x(t)) ~ x(t-1)], t; name = :M) |> complete - @test_throws "DDE" change_independent_variable(M, M.x) + M = ODESystem([D(x(t)) ~ x(t-1)], t; name = :M) + @test_throws "DDE" change_independent_variable(M, x(t)) end From a27785418814fe917fdeb096943375e92349ac79 Mon Sep 17 00:00:00 2001 From: Herman Sletmoen Date: Thu, 6 Mar 2025 20:38:01 +0100 Subject: [PATCH 1085/2176] Warn user about subtle differences between variables after change of independent variable in documentation --- .../tutorials/change_independent_variable.md | 22 +++++++++++++------ 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/docs/src/tutorials/change_independent_variable.md b/docs/src/tutorials/change_independent_variable.md index f196c3cb64..b308bd3f82 100644 --- a/docs/src/tutorials/change_independent_variable.md +++ b/docs/src/tutorials/change_independent_variable.md @@ -23,11 +23,8 @@ D = Differential(t) @parameters g = 9.81 v # gravitational acceleration and constant horizontal velocity M1 = ODESystem([ D(D(y)) ~ -g, D(x) ~ v # constant horizontal velocity -], t; defaults = [ - y => 0.0 # start on the ground -], initialization_eqs = [ - #x ~ 0.0, # TODO: handle? # hide - D(x) ~ D(y) # equal initial horizontal and vertical velocity (45 °) +], t; initialization_eqs = [ + D(x) ~ D(y) # equal initial horizontal and vertical velocity (45°) ], name = :M) M1s = structural_simplify(M1) ``` @@ -43,15 +40,26 @@ We will demonstrate the last method by changing the independent variable from $t This transformation is well-defined for any non-zero horizontal velocity $v$. ```@example changeivar M2 = change_independent_variable(M1, x; dummies = true) -@assert M2.x isa Num # hide M2s = structural_simplify(M2; allow_symbolic = true) +# a sanity test on the 10 x/y variables that are accessible to the user # hide +@assert allequal([x, M1s.x]) # hide +@assert allequal([M2.x, M2s.x]) # hide +@assert allequal([y, M1s.y]) # hide +@assert allunique([M1.x, M1.y, M2.y, M2s.y]) # hide +M2s # display this # hide ``` The derivatives are now with respect to the new independent variable $x$, which can be accessed with `M2.x`. + +!!! warn + At this point `x`, `M1.x`, `M1s.x`, `M2.x`, `M2s.x` are *three* different variables. + Meanwhile `y`, `M1.y`, `M1s.y`, `M2.y` and `M2s.y` are *four* different variables. + It can be instructive to inspect these yourself to see their subtle differences. + Notice how the number of equations has also decreased from three to two, as $\mathrm{d}x/\mathrm{d}t$ has been turned into an observed equation. It is straightforward to evolve the ODE for 10 meters and plot the resulting trajectory $y(x)$: ```@example changeivar using OrdinaryDiffEq, Plots -prob = ODEProblem(M2s, [], [0.0, 10.0], [v => 8.0]) # throw 10 meters with x-velocity 8 m/s +prob = ODEProblem(M2s, [M2s.y => 0.0], [0.0, 10.0], [v => 8.0]) # throw 10 meters with x-velocity 8 m/s sol = solve(prob, Tsit5()) plot(sol; idxs = M2.y) # must index by M2.y = y(x); not M1.y = y(t)! ``` From aa29107b95d0fdf109df73b67cf49a07125eb471 Mon Sep 17 00:00:00 2001 From: Herman Sletmoen Date: Thu, 6 Mar 2025 20:51:00 +0100 Subject: [PATCH 1086/2176] Update change_independent_variable docstring --- src/systems/diffeqs/basic_transformations.jl | 28 ++++++++++---------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/src/systems/diffeqs/basic_transformations.jl b/src/systems/diffeqs/basic_transformations.jl index a6ce1fcc14..60159023f7 100644 --- a/src/systems/diffeqs/basic_transformations.jl +++ b/src/systems/diffeqs/basic_transformations.jl @@ -60,30 +60,30 @@ This or other additional equations can also be specified through `eqs`. The transformation is well-defined when the mapping between the new and old independent variables are one-to-one. This is satisfied if one is a strictly increasing function of the other (e.g. ``du(t)/dt > 0`` or ``du(t)/dt < 0``). -Keyword arguments -================= -If `dummies`, derivatives of the new independent variable with respect to the old one are expressed through dummy equations; otherwise they are explicitly inserted into the equations. -If `simplify`, these dummy expressions are simplified and often give a tidier transformation. -If `fold`, internal substitutions will evaluate numerical expressions. +# Keyword arguments + +- `dummies`: Whether derivatives of the new independent variable with respect to the old one are expressed through dummy equations or explicitly inserted into the equations. +- `simplify`: Whether expanded derivative expressions are simplified. This can give a tidier transformation. +- `fold`: Whether internal substitutions will evaluate numerical expressions. Additional keyword arguments `kwargs...` are forwarded to the constructor that rebuilds `sys`. -Usage before structural simplification -====================================== +# Usage before structural simplification + The variable change must take place before structural simplification. Subsequently, consider passing `allow_symbolic = true` to `structural_simplify(sys)` to reduce the number of unknowns, with the understanding that the transformation is well-defined. -Usage with non-autonomous systems -================================= -If `sys` is autonomous (i.e. ``t`` appears explicitly in its equations), it is often desirable to also pass an algebraic equation relating the new and old independent variables (e.g. ``t = f(u(t))``). +# Usage with non-autonomous systems + +If `sys` is non-autonomous (i.e. ``t`` appears explicitly in its equations), it is often desirable to also pass an algebraic equation relating the new and old independent variables (e.g. ``t = f(u(t))``). Otherwise the transformed system will be underdetermined and cannot be structurally simplified without additional changes. -Usage with hierarchical systems -=============================== +# Usage with hierarchical systems + It is recommended that `iv` is a non-namespaced variable in `sys`. This means it can belong to the top-level system or be a variable in a subsystem declared with `GlobalScope`. -Example -======= +# Example + Consider a free fall with constant horizontal velocity. Physics naturally describes position as a function of time. By changing the independent variable, it can be reformulated for vertical position as a function of horizontal position: From ea5a0032165e56a2cf61985d664031e7b3409ce6 Mon Sep 17 00:00:00 2001 From: Herman Sletmoen Date: Thu, 6 Mar 2025 20:57:47 +0100 Subject: [PATCH 1087/2176] Explicitly test that c*D(x) ~ something fails, but add TODO to fix it --- test/basic_transformations.jl | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/basic_transformations.jl b/test/basic_transformations.jl index 0aa679a817..ea781536ef 100644 --- a/test/basic_transformations.jl +++ b/test/basic_transformations.jl @@ -148,6 +148,8 @@ end @test_throws "structurally simplified" change_independent_variable(structural_simplify(M), y) @test_throws "Got 0 equations:" change_independent_variable(M, w) @test_throws "Got 0 equations:" change_independent_variable(M, v) + M = ODESystem([2 * D(x) ~ 1, v ~ x], t; name = :M) # TODO: allow equations like this + @test_throws "Got 0 equations:" change_independent_variable(M, x) M = ODESystem([D(x) ~ 1, v ~ 1], t; name = :M) @test_throws "Got 2 equations:" change_independent_variable(M, x, [D(x) ~ 2]) @test_throws "not a function of the independent variable" change_independent_variable(M, y) From 0e41e045616cc260ce61b430607ef33ccf44eedb Mon Sep 17 00:00:00 2001 From: Herman Sletmoen Date: Fri, 7 Mar 2025 19:30:05 +0100 Subject: [PATCH 1088/2176] Prepare test for array variables (until expand_derivatives bug is fixed) --- src/systems/diffeqs/basic_transformations.jl | 8 +++++--- test/basic_transformations.jl | 8 +++++--- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/src/systems/diffeqs/basic_transformations.jl b/src/systems/diffeqs/basic_transformations.jl index 60159023f7..0806a105bb 100644 --- a/src/systems/diffeqs/basic_transformations.jl +++ b/src/systems/diffeqs/basic_transformations.jl @@ -121,9 +121,11 @@ function change_independent_variable(sys::AbstractODESystem, iv, eqs = []; dummi # 1) Utility that performs the chain rule on an expression, e.g. (d/dt)(f(t)) -> (d/dt)(f(u(t))) -> df(u(t))/du(t) * du(t)/dt function chain_rule(ex) - for var_of_iv1 in vars(ex; op = Nothing) # loop over all variables in expression, e.g. f(t) (op = Nothing so "D(f(t))" yields only "f(t)" and not "D(f(t))"; we want to replace *inside* derivative signs) - if iscall(var_of_iv1) && isequal(only(arguments(var_of_iv1)), iv1) && !isequal(var_of_iv1, iv2_of_iv1) # handle e.g. f(t) -> f(u(t)), but not u(t) -> u(u(t)) - var_of_iv2 = substitute(var_of_iv1, iv1 => iv2_of_iv1) # e.g. f(t) -> f(u(t)) + for var in vars(ex; op = Nothing) # loop over all variables in expression (op = Nothing prevents interpreting "D(f(t))" as one big variable) + is_function_of_iv1 = iscall(var) && isequal(only(arguments(var)), iv1) # is the expression of the form f(t)? + if is_function_of_iv1 && !isequal(var, iv2_of_iv1) # substitute f(t) -> f(u(t)), but not u(t) -> u(u(t)) + var_of_iv1 = var # e.g. f(t) + var_of_iv2 = substitute(var_of_iv1, iv1 => iv2_of_iv1) # e.g. f(u(t)) ex = substitute(ex, var_of_iv1 => var_of_iv2) end end diff --git a/test/basic_transformations.jl b/test/basic_transformations.jl index ea781536ef..cfbcea01b1 100644 --- a/test/basic_transformations.jl +++ b/test/basic_transformations.jl @@ -102,10 +102,12 @@ end end @testset "Change independent variable (simple)" begin - @variables x(t) - Mt = ODESystem([D(x) ~ 2*x], t; name = :M) + @variables x(t) y1(t) # y(t)[1:1] # TODO: use array variables y(t)[1:2] when fixed: https://github.com/JuliaSymbolics/Symbolics.jl/issues/1383 + Mt = ODESystem([D(x) ~ 2*x, D(y1) ~ y1], t; name = :M) Mx = change_independent_variable(Mt, x; dummies = true) - @test (@variables x xˍt(x) xˍtt(x); Set(equations(Mx)) == Set([xˍt ~ 2*x, xˍtt ~ 2*xˍt])) + @variables x xˍt(x) xˍtt(x) y1(x) # y(x)[1:1] # TODO: array variables + Dx = Differential(x) + @test (Set(equations(Mx)) == Set([xˍt ~ 2*x, xˍtt ~ 2*xˍt, xˍt*Dx(y1) ~ y1])) end @testset "Change independent variable (free fall)" begin From d66026947da0f471b8028348effce85e06bcc630 Mon Sep 17 00:00:00 2001 From: Herman Sletmoen Date: Fri, 7 Mar 2025 20:22:49 +0100 Subject: [PATCH 1089/2176] Test change_independent_variable with registered functions and callable parameters --- test/basic_transformations.jl | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/test/basic_transformations.jl b/test/basic_transformations.jl index cfbcea01b1..3853debcd4 100644 --- a/test/basic_transformations.jl +++ b/test/basic_transformations.jl @@ -1,4 +1,4 @@ -using ModelingToolkit, OrdinaryDiffEq, Test +using ModelingToolkit, OrdinaryDiffEq, DataInterpolations, Test @independent_variables t D = Differential(t) @@ -143,6 +143,34 @@ end ]) end +@testset "Change independent variable (registered function / callable parameter)" begin + @independent_variables t + D = Differential(t) + @variables x(t) y(t) + @parameters f::LinearInterpolation (fc::LinearInterpolation)(..) # non-callable and callable + callme(interp::LinearInterpolation, input) = interp(input) + @register_symbolic callme(interp::LinearInterpolation, input) + M1 = ODESystem([ + D(x) ~ 2*t + D(y) ~ 1*fc(t) + 2*fc(x) + 3*fc(y) + 1*callme(f, t) + 2*callme(f, x) + 3*callme(f, y) + ], t; name = :M) + + # Ensure that interpolations are called with the same variables + M2 = change_independent_variable(M1, x, [t ~ √(x)]) + @variables x y(x) t(x) + Dx = Differential(x) + @test Set(equations(M2)) == Set([ + t ~ √(x) + 2*t*Dx(y) ~ 1*fc(t) + 2*fc(x) + 3*fc(y) + 1*callme(f, t) + 2*callme(f, x) + 3*callme(f, y) + ]) + + _f = LinearInterpolation([1.0, 1.0], [-100.0, +100.0]) # constant value 1 + M2s = structural_simplify(M2; allow_symbolic = true) + prob = ODEProblem(M2s, [M2s.y => 0.0], (1.0, 4.0), [fc => _f, f => _f]) + sol = solve(prob, Tsit5(); abstol = 1e-5) + @test isapprox(sol(4.0, idxs=M2.y), 12.0; atol = 1e-5) # Anal solution is D(y) ~ 12 => y(t) ~ 12*t + C => y(x) ~ 12*√(x) + C. With y(x=1)=0 => 12*(√(x)-1), so y(x=4) ~ 12 +end + @testset "Change independent variable (errors)" begin @variables x(t) y z(y) w(t) v(t) M = ODESystem([D(x) ~ 0, v ~ x], t; name = :M) From 10793d28f28b5ce8bad2b3f3ffe2dc439f41e845 Mon Sep 17 00:00:00 2001 From: Herman Sletmoen Date: Fri, 7 Mar 2025 21:02:02 +0100 Subject: [PATCH 1090/2176] Optionally add differential equation for old independent variable --- .../tutorials/change_independent_variable.md | 5 ++++ src/systems/diffeqs/basic_transformations.jl | 23 ++++++++++++++----- test/basic_transformations.jl | 6 ++--- 3 files changed, 25 insertions(+), 9 deletions(-) diff --git a/docs/src/tutorials/change_independent_variable.md b/docs/src/tutorials/change_independent_variable.md index b308bd3f82..35a065e706 100644 --- a/docs/src/tutorials/change_independent_variable.md +++ b/docs/src/tutorials/change_independent_variable.md @@ -64,6 +64,11 @@ sol = solve(prob, Tsit5()) plot(sol; idxs = M2.y) # must index by M2.y = y(x); not M1.y = y(t)! ``` +!!! tip "Usage tips" + Look up the documentation of [`change_independent_variable`](@ref) for tips on how to use it. + + For example, if you also need $t(x)$, you can tell it to add a differential equation for the old independent variable in terms of the new one using the [inverse function rule](https://en.wikipedia.org/wiki/Inverse_function_rule) (here $\mathrm{d}t/\mathrm{d}x = 1 / (\mathrm{d}x/\mathrm{d}t)$). If you know an analytical expression between the independent variables (here $t = x/v$), you can also pass it directly to the function to avoid the extra differential equation. + ## 2. Alleviating stiffness by changing to logarithmic time In cosmology, the [Friedmann equations](https://en.wikipedia.org/wiki/Friedmann_equations) describe the expansion of the universe. diff --git a/src/systems/diffeqs/basic_transformations.jl b/src/systems/diffeqs/basic_transformations.jl index 0806a105bb..5e3c09d1d4 100644 --- a/src/systems/diffeqs/basic_transformations.jl +++ b/src/systems/diffeqs/basic_transformations.jl @@ -51,7 +51,7 @@ function liouville_transform(sys::AbstractODESystem; kwargs...) end """ - change_independent_variable(sys::AbstractODESystem, iv, eqs = []; dummies = false, simplify = true, fold = false, kwargs...) + change_independent_variable(sys::AbstractODESystem, iv, eqs = []; dummies = false, add_old_diff = false, simplify = true, fold = false, kwargs...) Transform the independent variable (e.g. ``t``) of the ODE system `sys` to a dependent variable `iv` (e.g. ``u(t)``). An equation in `sys` must define the rate of change of the new independent variable (e.g. ``du(t)/dt``). @@ -63,6 +63,7 @@ This is satisfied if one is a strictly increasing function of the other (e.g. `` # Keyword arguments - `dummies`: Whether derivatives of the new independent variable with respect to the old one are expressed through dummy equations or explicitly inserted into the equations. +- `add_old_diff`: Whether to add a differential equation for the old independent variable in terms of the new one using the inverse function rule. - `simplify`: Whether expanded derivative expressions are simplified. This can give a tidier transformation. - `fold`: Whether internal substitutions will evaluate numerical expressions. Additional keyword arguments `kwargs...` are forwarded to the constructor that rebuilds `sys`. @@ -99,7 +100,7 @@ julia> unknowns(M′) y(x) ``` """ -function change_independent_variable(sys::AbstractODESystem, iv, eqs = []; dummies = false, simplify = true, fold = false, kwargs...) +function change_independent_variable(sys::AbstractODESystem, iv, eqs = []; dummies = false, add_old_diff = false, simplify = true, fold = false, kwargs...) iv2_of_iv1 = unwrap(iv) # e.g. u(t) iv1 = get_iv(sys) # e.g. t @@ -148,24 +149,34 @@ function change_independent_variable(sys::AbstractODESystem, iv, eqs = []; dummi # 3) If requested, insert extra dummy equations for e.g. du/dt, d²u/dt², ... # Otherwise, replace all these derivatives by their explicit expressions + unks = get_unknowns(sys) if dummies div2 = substitute(default_toterm(D1(iv2_of_iv1)), iv1 => iv2) # e.g. uˍt(u) ddiv2 = substitute(default_toterm(D1(D1(iv2_of_iv1))), iv1 => iv2) # e.g. uˍtt(u) div2, ddiv2 = GlobalScope.([div2, ddiv2]) # do not namespace dummies in new system eqs = [[div2 ~ div2_of_iv1, ddiv2 ~ ddiv2_of_iv1]; eqs] # add dummy equations - @set! sys.unknowns = [get_unknowns(sys); [div2, ddiv2]] # add dummy variables + unks = [unks; [div2, ddiv2]] # add dummy variables else div2 = div2_of_iv1 ddiv2 = ddiv2_of_iv1 end + + # 4) If requested, add a differential equation for the old independent variable as a function of the old one + if add_old_diff + div1lhs = substitute(D2(iv1_of_iv2), iv2 => iv2_of_iv1) # e.g. (d/du(t))(t(u(t))); will be transformed to (d/du)(t(u)) by chain rule + div1rhs = 1 / div2 # du/dt = 1/(dt/du) (https://en.wikipedia.org/wiki/Inverse_function_rule) + eqs = [eqs; div1lhs ~ div1rhs] + unks = [unks; iv1_of_iv2] + end @set! sys.eqs = eqs # add extra equations we derived before starting transformation process + @set! sys.unknowns = unks # add dummy variables and old independent variable as a function of the new one - # 4) Transform everything from old to new independent variable, e.g. t -> u. + # 5) Transform everything from old to new independent variable, e.g. t -> u. # Substitution order matters! Must begin with highest order to get D(D(u(t))) -> u_tt(u). # If we had started with the lowest order, we would get D(D(u(t))) -> D(u_t(u)) -> 0! iv1_to_iv2_subs = [ # a vector ensures substitution order - D1(D1(iv2_of_iv1)) => ddiv2 # order 2, e.g. D(D(u(t))) -> u_tt(u) - D1(iv2_of_iv1) => div2 # order 1, e.g. D(u(t)) -> u_t(u) + D1(D1(iv2_of_iv1)) => ddiv2 # order 2, e.g. D(D(u(t))) -> u_tt(u) or explicit expression + D1(iv2_of_iv1) => div2 # order 1, e.g. D(u(t)) -> u_t(u) or explicit expression iv2_of_iv1 => iv2 # order 0, e.g. u(t) -> u iv1 => iv1_of_iv2 # in case sys was autonomous, e.g. t -> t(u) ] diff --git a/test/basic_transformations.jl b/test/basic_transformations.jl index 3853debcd4..ac3720469a 100644 --- a/test/basic_transformations.jl +++ b/test/basic_transformations.jl @@ -114,12 +114,12 @@ end @variables x(t) y(t) @parameters g v # gravitational acceleration and constant horizontal velocity Mt = ODESystem([D(D(y)) ~ -g, D(x) ~ v], t; name = :M) # gives (x, y) as function of t, ... - Mx = change_independent_variable(Mt, x; dummies = false) # ... but we want y as a function of x + Mx = change_independent_variable(Mt, x; add_old_diff = true) # ... but we want y as a function of x Mx = structural_simplify(Mx; allow_symbolic = true) Dx = Differential(Mx.x) - prob = ODEProblem(Mx, [Mx.y => 0.0, Dx(Mx.y) => 1.0], (0.0, 20.0), [g => 9.81, v => 10.0]) # 1 = dy/dx = (dy/dt)/(dx/dt) means equal initial horizontal and vertical velocities + prob = ODEProblem(Mx, [Mx.y => 0.0, Dx(Mx.y) => 1.0, Mx.t => 0.0], (0.0, 20.0), [g => 9.81, v => 10.0]) # 1 = dy/dx = (dy/dt)/(dx/dt) means equal initial horizontal and vertical velocities sol = solve(prob, Tsit5(); reltol = 1e-5) - @test all(isapprox.(sol[Mx.y], sol[Mx.x - g*(Mx.x/v)^2/2]; atol = 1e-10)) # compare to analytical solution (x(t) = v*t, y(t) = v*t - g*t^2/2) + @test all(isapprox.(sol[Mx.y], sol[Mx.x - g*(Mx.t)^2/2]; atol = 1e-10)) # compare to analytical solution (x(t) = v*t, y(t) = v*t - g*t^2/2) end @testset "Change independent variable (crazy analytical example)" begin From 2b0998b74409f3cf04aebeae2652a113fd1ec5bd Mon Sep 17 00:00:00 2001 From: Herman Sletmoen Date: Sun, 9 Mar 2025 00:47:27 +0100 Subject: [PATCH 1091/2176] Rewrite change_independent_variable to handle any derivative order and nonlinear equations --- .../tutorials/change_independent_variable.md | 4 +- src/systems/diffeqs/basic_transformations.jl | 120 +++++++----------- test/basic_transformations.jl | 56 ++++---- 3 files changed, 77 insertions(+), 103 deletions(-) diff --git a/docs/src/tutorials/change_independent_variable.md b/docs/src/tutorials/change_independent_variable.md index 35a065e706..8f9ee0cbd2 100644 --- a/docs/src/tutorials/change_independent_variable.md +++ b/docs/src/tutorials/change_independent_variable.md @@ -39,7 +39,7 @@ There are at least three ways of answering this: We will demonstrate the last method by changing the independent variable from $t$ to $x$. This transformation is well-defined for any non-zero horizontal velocity $v$. ```@example changeivar -M2 = change_independent_variable(M1, x; dummies = true) +M2 = change_independent_variable(M1, x) M2s = structural_simplify(M2; allow_symbolic = true) # a sanity test on the 10 x/y variables that are accessible to the user # hide @assert allequal([x, M1s.x]) # hide @@ -110,7 +110,7 @@ To do this, we will change the independent variable in two stages; from $t$ to $ Notice that $\mathrm{d}a/\mathrm{d}t > 0$ provided that $\Omega > 0$, and $\mathrm{d}b/\mathrm{d}a > 0$, so the transformation is well-defined. First, we transform from $t$ to $a$: ```@example changeivar -M2 = change_independent_variable(M1, M1.a; dummies = true) +M2 = change_independent_variable(M1, M1.a) ``` Unlike the original, notice that this system is *non-autonomous* because the independent variable $a$ appears explicitly in the equations! This means that to change the independent variable from $a$ to $b$, we must provide not only the rate of change relation $db(a)/da = \exp(-b)$, but *also* the equation $a(b) = \exp(b)$ so $a$ can be eliminated in favor of $b$: diff --git a/src/systems/diffeqs/basic_transformations.jl b/src/systems/diffeqs/basic_transformations.jl index 5e3c09d1d4..c05e5e4394 100644 --- a/src/systems/diffeqs/basic_transformations.jl +++ b/src/systems/diffeqs/basic_transformations.jl @@ -51,22 +51,19 @@ function liouville_transform(sys::AbstractODESystem; kwargs...) end """ - change_independent_variable(sys::AbstractODESystem, iv, eqs = []; dummies = false, add_old_diff = false, simplify = true, fold = false, kwargs...) + change_independent_variable(sys::AbstractODESystem, iv, eqs = []; add_old_diff = false, simplify = true, fold = false, kwargs...) Transform the independent variable (e.g. ``t``) of the ODE system `sys` to a dependent variable `iv` (e.g. ``u(t)``). -An equation in `sys` must define the rate of change of the new independent variable (e.g. ``du(t)/dt``). -This or other additional equations can also be specified through `eqs`. - The transformation is well-defined when the mapping between the new and old independent variables are one-to-one. This is satisfied if one is a strictly increasing function of the other (e.g. ``du(t)/dt > 0`` or ``du(t)/dt < 0``). +Any extra equations `eqs` involving the new and old independent variables will be taken into account in the transformation. + # Keyword arguments -- `dummies`: Whether derivatives of the new independent variable with respect to the old one are expressed through dummy equations or explicitly inserted into the equations. -- `add_old_diff`: Whether to add a differential equation for the old independent variable in terms of the new one using the inverse function rule. +- `add_old_diff`: Whether to add a differential equation for the old independent variable in terms of the new one using the inverse function rule ``dt/du = 1/(du/dt)``. - `simplify`: Whether expanded derivative expressions are simplified. This can give a tidier transformation. - `fold`: Whether internal substitutions will evaluate numerical expressions. -Additional keyword arguments `kwargs...` are forwarded to the constructor that rebuilds `sys`. # Usage before structural simplification @@ -77,6 +74,7 @@ Subsequently, consider passing `allow_symbolic = true` to `structural_simplify(s If `sys` is non-autonomous (i.e. ``t`` appears explicitly in its equations), it is often desirable to also pass an algebraic equation relating the new and old independent variables (e.g. ``t = f(u(t))``). Otherwise the transformed system will be underdetermined and cannot be structurally simplified without additional changes. +If an algebraic relation is not known, consider using `add_old_diff`. # Usage with hierarchical systems @@ -91,16 +89,20 @@ By changing the independent variable, it can be reformulated for vertical positi ```julia julia> @variables x(t) y(t); -julia> @named M = ODESystem([D(D(y)) ~ -9.81, D(x) ~ 10.0], t); +julia> @named M = ODESystem([D(D(y)) ~ -9.81, D(D(x)) ~ 0.0], t); + +julia> M = change_independent_variable(M, x); -julia> M′ = change_independent_variable(complete(M), x); +julia> M = structural_simplify(M; allow_symbolic = true); -julia> unknowns(M′) -1-element Vector{SymbolicUtils.BasicSymbolic{Real}}: +julia> unknowns(M) +3-element Vector{SymbolicUtils.BasicSymbolic{Real}}: + xˍt(x) y(x) + yˍx(x) ``` """ -function change_independent_variable(sys::AbstractODESystem, iv, eqs = []; dummies = false, add_old_diff = false, simplify = true, fold = false, kwargs...) +function change_independent_variable(sys::AbstractODESystem, iv, eqs = []; add_old_diff = false, simplify = true, fold = false, kwargs...) iv2_of_iv1 = unwrap(iv) # e.g. u(t) iv1 = get_iv(sys) # e.g. t @@ -115,78 +117,45 @@ function change_independent_variable(sys::AbstractODESystem, iv, eqs = []; dummi iv1name = nameof(iv1) # e.g. :t iv2name = nameof(operation(iv2_of_iv1)) # e.g. :u iv2, = @independent_variables $iv2name # e.g. u - iv1_of_iv2, = @variables $iv1name(iv2) # inverse in case sys is autonomous; e.g. t(u) - iv1_of_iv2 = GlobalScope(iv1_of_iv2) # do not namespace old independent variable as new dependent variable + iv1_of_iv2, = GlobalScope.(@variables $iv1name(iv2)) # inverse, e.g. t(u), global because iv1 has no namespacing in sys D1 = Differential(iv1) # e.g. d/d(t) - D2 = Differential(iv2_of_iv1) # e.g. d/d(u(t)) + div2_of_iv1 = GlobalScope(default_toterm(D1(iv2_of_iv1))) # e.g. uˍt(t) + div2_of_iv2 = substitute(div2_of_iv1, iv1 => iv2) # e.g. uˍt(u) + div2_of_iv2_of_iv1 = substitute(div2_of_iv2, iv2 => iv2_of_iv1) # e.g. uˍt(u(t)) + + # If requested, add a differential equation for the old independent variable as a function of the old one + if add_old_diff + eqs = [eqs; Differential(iv2)(iv1_of_iv2) ~ 1 / div2_of_iv2] # e.g. dt(u)/du ~ 1 / uˍt(u) (https://en.wikipedia.org/wiki/Inverse_function_rule) + end + @set! sys.eqs = [get_eqs(sys); eqs] # add extra equations we derived before starting transformation process + @set! sys.unknowns = [get_unknowns(sys); [iv1, div2_of_iv1]] # add new variables, will be transformed to e.g. t(u) and uˍt(u) # add dummy variables and old independent variable as a function of the new one - # 1) Utility that performs the chain rule on an expression, e.g. (d/dt)(f(t)) -> (d/dt)(f(u(t))) -> df(u(t))/du(t) * du(t)/dt - function chain_rule(ex) + # Create a utility that performs the chain rule on an expression, followed by insertion of the new independent variable + # e.g. (d/dt)(f(t)) -> (d/dt)(f(u(t))) -> df(u(t))/du(t) * du(t)/dt -> df(u)/du * uˍt(u) + # Then use it to transform everything in the system! + function transform(ex) + # 1) Replace the argument of every function; e.g. f(t) -> f(u(t)) for var in vars(ex; op = Nothing) # loop over all variables in expression (op = Nothing prevents interpreting "D(f(t))" as one big variable) is_function_of_iv1 = iscall(var) && isequal(only(arguments(var)), iv1) # is the expression of the form f(t)? - if is_function_of_iv1 && !isequal(var, iv2_of_iv1) # substitute f(t) -> f(u(t)), but not u(t) -> u(u(t)) + if is_function_of_iv1 && !isequal(var, iv2_of_iv1) # prevent e.g. u(t) -> u(u(t)) var_of_iv1 = var # e.g. f(t) - var_of_iv2 = substitute(var_of_iv1, iv1 => iv2_of_iv1) # e.g. f(u(t)) - ex = substitute(ex, var_of_iv1 => var_of_iv2) + var_of_iv2_of_iv1 = substitute(var_of_iv1, iv1 => iv2_of_iv1) # e.g. f(u(t)) + ex = substitute(ex, var_of_iv1 => var_of_iv2_of_iv1; fold) end end - ex = expand_derivatives(ex, simplify) # expand chain rule, e.g. (d/dt)(f(u(t)))) -> df(u(t))/du(t) * du(t)/dt - return ex - end - - # 2) Find e.g. du/dt in equations, then calculate e.g. d²u/dt², ... - eqs = [eqs; get_eqs(sys)] # all equations (system-defined + user-provided) we may use - idxs = findall(eq -> isequal(eq.lhs, D1(iv2_of_iv1)), eqs) - if length(idxs) != 1 - error("Exactly one equation for $D1($iv2_of_iv1) was not specified! Got $(length(idxs)) equations:\n", join(eqs[idxs], '\n')) - end - div2_of_iv1_eq = popat!(eqs, only(idxs)) # get and remove e.g. du/dt = ... (may be added back later as a dummy) - div2_of_iv1 = chain_rule(div2_of_iv1_eq.rhs) - if isequal(div2_of_iv1, 0) # e.g. du/dt ~ 0 - error("Independent variable transformation $(div2_of_iv1_eq) is singular!") - end - ddiv2_of_iv1 = chain_rule(D1(div2_of_iv1)) # TODO: implement higher orders (order >= 3) derivatives with a loop - - # 3) If requested, insert extra dummy equations for e.g. du/dt, d²u/dt², ... - # Otherwise, replace all these derivatives by their explicit expressions - unks = get_unknowns(sys) - if dummies - div2 = substitute(default_toterm(D1(iv2_of_iv1)), iv1 => iv2) # e.g. uˍt(u) - ddiv2 = substitute(default_toterm(D1(D1(iv2_of_iv1))), iv1 => iv2) # e.g. uˍtt(u) - div2, ddiv2 = GlobalScope.([div2, ddiv2]) # do not namespace dummies in new system - eqs = [[div2 ~ div2_of_iv1, ddiv2 ~ ddiv2_of_iv1]; eqs] # add dummy equations - unks = [unks; [div2, ddiv2]] # add dummy variables - else - div2 = div2_of_iv1 - ddiv2 = ddiv2_of_iv1 - end - - # 4) If requested, add a differential equation for the old independent variable as a function of the old one - if add_old_diff - div1lhs = substitute(D2(iv1_of_iv2), iv2 => iv2_of_iv1) # e.g. (d/du(t))(t(u(t))); will be transformed to (d/du)(t(u)) by chain rule - div1rhs = 1 / div2 # du/dt = 1/(dt/du) (https://en.wikipedia.org/wiki/Inverse_function_rule) - eqs = [eqs; div1lhs ~ div1rhs] - unks = [unks; iv1_of_iv2] - end - @set! sys.eqs = eqs # add extra equations we derived before starting transformation process - @set! sys.unknowns = unks # add dummy variables and old independent variable as a function of the new one - - # 5) Transform everything from old to new independent variable, e.g. t -> u. - # Substitution order matters! Must begin with highest order to get D(D(u(t))) -> u_tt(u). - # If we had started with the lowest order, we would get D(D(u(t))) -> D(u_t(u)) -> 0! - iv1_to_iv2_subs = [ # a vector ensures substitution order - D1(D1(iv2_of_iv1)) => ddiv2 # order 2, e.g. D(D(u(t))) -> u_tt(u) or explicit expression - D1(iv2_of_iv1) => div2 # order 1, e.g. D(u(t)) -> u_t(u) or explicit expression - iv2_of_iv1 => iv2 # order 0, e.g. u(t) -> u - iv1 => iv1_of_iv2 # in case sys was autonomous, e.g. t -> t(u) - ] - function transform(ex) - ex = chain_rule(ex) - for sub in iv1_to_iv2_subs - ex = substitute(ex, sub; fold) + # 2) Expand with chain rule until nothing changes anymore + orgex = nothing + while !isequal(ex, orgex) + orgex = ex # save original + ex = expand_derivatives(ex, simplify) # expand chain rule, e.g. (d/dt)(f(u(t)))) -> df(u(t))/du(t) * du(t)/dt + ex = substitute(ex, D1(iv2_of_iv1) => div2_of_iv2_of_iv1; fold) # e.g. du(t)/dt -> uˍt(u(t)) end + # 3) Set new independent variable + ex = substitute(ex, iv2_of_iv1 => iv2; fold) # set e.g. u(t) -> u everywhere + ex = substitute(ex, iv1 => iv1_of_iv2; fold) # set e.g. t -> t(u) everywhere return ex end + function transform(sys::AbstractODESystem) eqs = map(transform, get_eqs(sys)) unknowns = map(transform, get_unknowns(sys)) @@ -203,7 +172,7 @@ function change_independent_variable(sys::AbstractODESystem, iv, eqs = []; dummi wascomplete = iscomplete(sys) # save before reconstructing system sys = typeof(sys)( # recreate system with transformed fields eqs, iv2, unknowns, ps; observed, initialization_eqs, parameter_dependencies, defaults, guesses, - assertions, name = nameof(sys), description = description(sys), kwargs... + assertions, name = nameof(sys), description = description(sys) ) systems = map(transform, systems) # recurse through subsystems sys = compose(sys, systems) # rebuild hierarchical system @@ -213,5 +182,6 @@ function change_independent_variable(sys::AbstractODESystem, iv, eqs = []; dummi end return sys end + return transform(sys) end diff --git a/test/basic_transformations.jl b/test/basic_transformations.jl index ac3720469a..8bf60ed68a 100644 --- a/test/basic_transformations.jl +++ b/test/basic_transformations.jl @@ -30,8 +30,9 @@ end eqs1 = [D(D(x)) ~ D(x) + x, D(y) ~ 1] M1 = ODESystem(eqs1, t; name = :M) M2 = change_independent_variable(M1, y) - eqs2 = substitute(equations(M2), M2.y => M1.t) # system should be equivalent when parametrized with y (since D(y) ~ 1), so substitute back ... - @test eqs1[1] == only(eqs2) # ... and check that the equations are unmodified + @variables y x(y) yˍt(y) + Dy = Differential(y) + @test Set(equations(M2)) == Set([yˍt^2*(Dy^2)(x) + yˍt*Dy(yˍt)*Dy(x) ~ x + Dy(x)*yˍt, yˍt ~ 1]) end @testset "Change independent variable" begin @@ -44,7 +45,7 @@ end ] initialization_eqs = [x ~ 1.0, y ~ 1.0, D(y) ~ 0.0] M1 = ODESystem(eqs, t; initialization_eqs, name = :M) - M2 = change_independent_variable(M1, s; dummies = true) + M2 = change_independent_variable(M1, s) M1 = structural_simplify(M1; allow_symbolic = true) M2 = structural_simplify(M2; allow_symbolic = true) @@ -77,16 +78,15 @@ end M1 = compose(M1, r, m, Λ) # Apply in two steps, where derivatives are defined at each step: first t -> a, then a -> b - M2 = change_independent_variable(M1, M1.a; dummies = true) + M2 = change_independent_variable(M1, M1.a) M2c = complete(M2) # just for the following equation comparison (without namespacing) - a, ȧ, Ω, Ωr, Ωm, ΩΛ, ϕ, aˍt, aˍtt = M2c.a, M2c.ȧ, M2c.Ω, M2c.r.Ω, M2c.m.Ω, M2c.Λ.Ω, M2c.ϕ, M2c.aˍt, M2c.aˍtt + a, ȧ, Ω, Ωr, Ωm, ΩΛ, ϕ, aˍt = M2c.a, M2c.ȧ, M2c.Ω, M2c.r.Ω, M2c.m.Ω, M2c.Λ.Ω, M2c.ϕ, M2c.aˍt Da = Differential(a) @test Set(equations(M2)) == Set([ - aˍt ~ ȧ # 1st order dummy equation - aˍtt ~ Da(ȧ) * aˍt # 2nd order dummy equation + aˍt ~ ȧ # dummy equation Ω ~ Ωr + Ωm + ΩΛ ȧ ~ √(Ω) * a^2 - aˍtt*Da(ϕ) + aˍt^2*(Da^2)(ϕ) ~ -3*aˍt^2/a*Da(ϕ) + Da(aˍt)*Da(ϕ)*aˍt + aˍt^2*(Da^2)(ϕ) ~ -3*aˍt^2/a*Da(ϕ) aˍt*Da(Ωr) ~ -4*Ωr*aˍt/a aˍt*Da(Ωm) ~ -3*Ωm*aˍt/a aˍt*Da(ΩΛ) ~ 0 @@ -104,13 +104,13 @@ end @testset "Change independent variable (simple)" begin @variables x(t) y1(t) # y(t)[1:1] # TODO: use array variables y(t)[1:2] when fixed: https://github.com/JuliaSymbolics/Symbolics.jl/issues/1383 Mt = ODESystem([D(x) ~ 2*x, D(y1) ~ y1], t; name = :M) - Mx = change_independent_variable(Mt, x; dummies = true) + Mx = change_independent_variable(Mt, x) @variables x xˍt(x) xˍtt(x) y1(x) # y(x)[1:1] # TODO: array variables Dx = Differential(x) - @test (Set(equations(Mx)) == Set([xˍt ~ 2*x, xˍtt ~ 2*xˍt, xˍt*Dx(y1) ~ y1])) + @test (Set(equations(Mx)) == Set([xˍt ~ 2*x, xˍt*Dx(y1) ~ y1])) end -@testset "Change independent variable (free fall)" begin +@testset "Change independent variable (free fall with 1st order horizontal equation)" begin @variables x(t) y(t) @parameters g v # gravitational acceleration and constant horizontal velocity Mt = ODESystem([D(D(y)) ~ -g, D(x) ~ v], t; name = :M) # gives (x, y) as function of t, ... @@ -122,6 +122,18 @@ end @test all(isapprox.(sol[Mx.y], sol[Mx.x - g*(Mx.t)^2/2]; atol = 1e-10)) # compare to analytical solution (x(t) = v*t, y(t) = v*t - g*t^2/2) end +@testset "Change independent variable (free fall with 2nd order horizontal equation)" begin + @variables x(t) y(t) + @parameters g # gravitational acceleration + Mt = ODESystem([D(D(y)) ~ -g, D(D(x)) ~ 0], t; name = :M) # gives (x, y) as function of t, ... + Mx = change_independent_variable(Mt, x; add_old_diff = true) # ... but we want y as a function of x + Mx = structural_simplify(Mx; allow_symbolic = true) + Dx = Differential(Mx.x) + prob = ODEProblem(Mx, [Mx.y => 0.0, Dx(Mx.y) => 1.0, Mx.t => 0.0, Mx.xˍt => 10.0], (0.0, 20.0), [g => 9.81]) # 1 = dy/dx = (dy/dt)/(dx/dt) means equal initial horizontal and vertical velocities + sol = solve(prob, Tsit5(); reltol = 1e-5) + @test all(isapprox.(sol[Mx.y], sol[Mx.x - g*(Mx.t)^2/2]; atol = 1e-10)) # compare to analytical solution (x(t) = v*t, y(t) = v*t - g*t^2/2) +end + @testset "Change independent variable (crazy analytical example)" begin @independent_variables t D = Differential(t) @@ -130,16 +142,15 @@ end D(D(y)) ~ D(x)^2 + D(y^3) |> expand_derivatives # expand D(y^3) # TODO: make this test 3rd order D(x) ~ x^4 + y^5 + t^6 ], t; name = :M) - M2 = change_independent_variable(M1, x; dummies = true) + M2 = change_independent_variable(M1, x) # Compare to pen-and-paper result @independent_variables x Dx = Differential(x) @variables xˍt(x) xˍtt(x) y(x) t(x) @test Set(equations(M2)) == Set([ - xˍt^2*(Dx^2)(y) + xˍtt*Dx(y) ~ xˍt^2 + 3*y^2*Dx(y)*xˍt # from D(D(y)) - xˍt ~ x^4 + y^5 + t^6 # 1st order dummy equation - xˍtt ~ 4*x^3*xˍt + 5*y^4*Dx(y)*xˍt + 6*t^5 # 2nd order dummy equation + xˍt^2*(Dx^2)(y) + xˍt*Dx(xˍt)*Dx(y) ~ xˍt^2 + 3*y^2*Dx(y)*xˍt # from D(D(y)) + xˍt ~ x^4 + y^5 + t^6 # dummy equation ]) end @@ -157,11 +168,12 @@ end # Ensure that interpolations are called with the same variables M2 = change_independent_variable(M1, x, [t ~ √(x)]) - @variables x y(x) t(x) + @variables x xˍt(x) y(x) t(x) Dx = Differential(x) @test Set(equations(M2)) == Set([ t ~ √(x) - 2*t*Dx(y) ~ 1*fc(t) + 2*fc(x) + 3*fc(y) + 1*callme(f, t) + 2*callme(f, x) + 3*callme(f, y) + xˍt ~ 2*t + xˍt*Dx(y) ~ 1*fc(t) + 2*fc(x) + 3*fc(y) + 1*callme(f, t) + 2*callme(f, x) + 3*callme(f, y) ]) _f = LinearInterpolation([1.0, 1.0], [-100.0, +100.0]) # constant value 1 @@ -173,18 +185,10 @@ end @testset "Change independent variable (errors)" begin @variables x(t) y z(y) w(t) v(t) - M = ODESystem([D(x) ~ 0, v ~ x], t; name = :M) - @test_throws "singular" change_independent_variable(M, x) + M = ODESystem([D(x) ~ 1, v ~ x], t; name = :M) @test_throws "structurally simplified" change_independent_variable(structural_simplify(M), y) - @test_throws "Got 0 equations:" change_independent_variable(M, w) - @test_throws "Got 0 equations:" change_independent_variable(M, v) - M = ODESystem([2 * D(x) ~ 1, v ~ x], t; name = :M) # TODO: allow equations like this - @test_throws "Got 0 equations:" change_independent_variable(M, x) - M = ODESystem([D(x) ~ 1, v ~ 1], t; name = :M) - @test_throws "Got 2 equations:" change_independent_variable(M, x, [D(x) ~ 2]) @test_throws "not a function of the independent variable" change_independent_variable(M, y) @test_throws "not a function of the independent variable" change_independent_variable(M, z) - M = ODESystem([D(x) ~ 0, v ~ x], t; name = :M) @variables x(..) # require explicit argument M = ODESystem([D(x(t)) ~ x(t-1)], t; name = :M) @test_throws "DDE" change_independent_variable(M, x(t)) From f2a1a5ae580487e3725ca85fbd9e1083719a1b48 Mon Sep 17 00:00:00 2001 From: Herman Sletmoen Date: Sun, 9 Mar 2025 12:25:15 +0100 Subject: [PATCH 1092/2176] Test 3rd order nonlinear system --- test/basic_transformations.jl | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/test/basic_transformations.jl b/test/basic_transformations.jl index 8bf60ed68a..dd6d677b60 100644 --- a/test/basic_transformations.jl +++ b/test/basic_transformations.jl @@ -134,24 +134,24 @@ end @test all(isapprox.(sol[Mx.y], sol[Mx.x - g*(Mx.t)^2/2]; atol = 1e-10)) # compare to analytical solution (x(t) = v*t, y(t) = v*t - g*t^2/2) end -@testset "Change independent variable (crazy analytical example)" begin +@testset "Change independent variable (crazy 3rd order nonlinear system)" begin @independent_variables t D = Differential(t) @variables x(t) y(t) - M1 = ODESystem([ # crazy non-autonomous non-linear 2nd order ODE - D(D(y)) ~ D(x)^2 + D(y^3) |> expand_derivatives # expand D(y^3) # TODO: make this test 3rd order - D(x) ~ x^4 + y^5 + t^6 + M1 = ODESystem([ + (D^3)(y) ~ D(x)^2 + (D^2)(y^2) |> expand_derivatives + D(x)^2 + D(y)^2 ~ x^4 + y^5 + t^6 ], t; name = :M) - M2 = change_independent_variable(M1, x) + M2 = change_independent_variable(M1, x; add_old_diff = true) + @test_nowarn structural_simplify(M2) # Compare to pen-and-paper result - @independent_variables x + @variables x xˍt(x) xˍt(x) y(x) t(x) Dx = Differential(x) - @variables xˍt(x) xˍtt(x) y(x) t(x) - @test Set(equations(M2)) == Set([ - xˍt^2*(Dx^2)(y) + xˍt*Dx(xˍt)*Dx(y) ~ xˍt^2 + 3*y^2*Dx(y)*xˍt # from D(D(y)) - xˍt ~ x^4 + y^5 + t^6 # dummy equation - ]) + areequivalent(eq1, eq2) = isequal(expand(eq1.lhs - eq2.lhs), 0) && isequal(expand(eq1.rhs - eq2.rhs), 0) + @test areequivalent(equations(M2)[1], xˍt^3*(Dx^3)(y) + xˍt^2*Dx(y)*(Dx^2)(xˍt) + xˍt*Dx(y)*(Dx(xˍt))^2 + 3*xˍt^2*(Dx^2)(y)*Dx(xˍt) ~ xˍt^2 + 2*xˍt^2*Dx(y)^2 + 2*xˍt^2*y*(Dx^2)(y) + 2*y*Dx(y)*Dx(xˍt)*xˍt) + @test areequivalent(equations(M2)[2], xˍt^2 + xˍt^2*Dx(y)^2 ~ x^4 + y^5 + t^6) + @test areequivalent(equations(M2)[3], Dx(t) ~ 1 / xˍt) end @testset "Change independent variable (registered function / callable parameter)" begin From aaae59df56fa8a200317420fba65579d56e12ecc Mon Sep 17 00:00:00 2001 From: Herman Sletmoen Date: Sun, 9 Mar 2025 12:33:09 +0100 Subject: [PATCH 1093/2176] Clean up and format --- .../tutorials/change_independent_variable.md | 78 +++++++----- src/ModelingToolkit.jl | 3 +- src/systems/diffeqs/basic_transformations.jl | 49 ++++--- test/basic_transformations.jl | 120 +++++++++++------- 4 files changed, 150 insertions(+), 100 deletions(-) diff --git a/docs/src/tutorials/change_independent_variable.md b/docs/src/tutorials/change_independent_variable.md index 8f9ee0cbd2..e1243fb2ee 100644 --- a/docs/src/tutorials/change_independent_variable.md +++ b/docs/src/tutorials/change_independent_variable.md @@ -2,11 +2,11 @@ Ordinary differential equations describe the rate of change of some dependent variables with respect to one independent variable. For the modeler it is often most natural to write down the equations with a particular independent variable, say time $t$. -In many cases there are good reasons for reparametrizing ODEs in terms of a different independent variable: +However, in many cases there are good reasons for changing the independent variable: -1. One may want $y(x)$ as a function of $x$ instead of $(x(t), y(t))$ as a function of $t$ -2. Some differential equations vary more nicely (e.g. less stiff or better behaved) with respect to one independent variable than another. -3. It can reduce the number of equations that must be solved (e.g. $y(x)$ is one equation, while $(x(t), y(t))$ are two). + 1. One may want $y(x)$ as a function of $x$ instead of $(x(t), y(t))$ as a function of $t$ + 2. Some differential equations vary more nicely (e.g. less stiff) with respect to one independent variable than another. + 3. It can reduce the number of equations that must be solved (e.g. $y(x)$ is one equation, while $(x(t), y(t))$ are two). To manually change the independent variable of an ODE, one must rewrite all equations in terms of a new variable and transform differentials with the chain rule. This is mechanical and error-prone. @@ -14,30 +14,34 @@ ModelingToolkit provides the utility function [`change_independent_variable`](@r ## 1. Get one dependent variable as a function of another -Consider a projectile shot with some initial velocity in a gravitational field. +Consider a projectile shot with some initial velocity in a vertical gravitational field with constant horizontal velocity. + ```@example changeivar using ModelingToolkit @independent_variables t D = Differential(t) @variables x(t) y(t) -@parameters g = 9.81 v # gravitational acceleration and constant horizontal velocity -M1 = ODESystem([ - D(D(y)) ~ -g, D(x) ~ v # constant horizontal velocity -], t; initialization_eqs = [ - D(x) ~ D(y) # equal initial horizontal and vertical velocity (45°) -], name = :M) +@parameters g=9.81 v # gravitational acceleration and horizontal velocity +eqs = [D(D(y)) ~ -g, D(x) ~ v] +initialization_eqs = [D(x) ~ D(y)] # 45° initial angle +M1 = ODESystem(eqs, t; initialization_eqs, name = :M) M1s = structural_simplify(M1) +@assert length(equations(M1s)) == 3 # hide +M1s # hide ``` + This is the standard parametrization that arises naturally from kinematics and Newton's laws. It expresses the position $(x(t), y(t))$ as a function of time $t$. But suppose we want to determine whether the projectile hits a target 10 meters away. There are at least three ways of answering this: -* Solve the ODE for $(x(t), y(t))$ and use a callback to terminate when $x$ reaches 10 meters, and evaluate $y$ at the final time. -* Solve the ODE for $(x(t), y(t))$ and use root finding to find the time when $x$ reaches 10 meters, and evaluate $y$ at that time. -* Solve the ODE for $y(x)$ and evaluate it at 10 meters. + + - Solve the ODE for $(x(t), y(t))$ and use a callback to terminate when $x$ reaches 10 meters, and evaluate $y$ at the final time. + - Solve the ODE for $(x(t), y(t))$ and use root finding to find the time when $x$ reaches 10 meters, and evaluate $y$ at that time. + - Solve the ODE for $y(x)$ and evaluate it at 10 meters. We will demonstrate the last method by changing the independent variable from $t$ to $x$. -This transformation is well-defined for any non-zero horizontal velocity $v$. +This transformation is well-defined for any non-zero horizontal velocity $v$, so $x$ and $t$ are one-to-one. + ```@example changeivar M2 = change_independent_variable(M1, x) M2s = structural_simplify(M2; allow_symbolic = true) @@ -46,17 +50,21 @@ M2s = structural_simplify(M2; allow_symbolic = true) @assert allequal([M2.x, M2s.x]) # hide @assert allequal([y, M1s.y]) # hide @assert allunique([M1.x, M1.y, M2.y, M2s.y]) # hide +@assert length(equations(M2s)) == 2 # hide M2s # display this # hide ``` + The derivatives are now with respect to the new independent variable $x$, which can be accessed with `M2.x`. !!! warn + At this point `x`, `M1.x`, `M1s.x`, `M2.x`, `M2s.x` are *three* different variables. Meanwhile `y`, `M1.y`, `M1s.y`, `M2.y` and `M2s.y` are *four* different variables. It can be instructive to inspect these yourself to see their subtle differences. Notice how the number of equations has also decreased from three to two, as $\mathrm{d}x/\mathrm{d}t$ has been turned into an observed equation. It is straightforward to evolve the ODE for 10 meters and plot the resulting trajectory $y(x)$: + ```@example changeivar using OrdinaryDiffEq, Plots prob = ODEProblem(M2s, [M2s.y => 0.0], [0.0, 10.0], [v => 8.0]) # throw 10 meters with x-velocity 8 m/s @@ -65,67 +73,75 @@ plot(sol; idxs = M2.y) # must index by M2.y = y(x); not M1.y = y(t)! ``` !!! tip "Usage tips" + Look up the documentation of [`change_independent_variable`](@ref) for tips on how to use it. - + For example, if you also need $t(x)$, you can tell it to add a differential equation for the old independent variable in terms of the new one using the [inverse function rule](https://en.wikipedia.org/wiki/Inverse_function_rule) (here $\mathrm{d}t/\mathrm{d}x = 1 / (\mathrm{d}x/\mathrm{d}t)$). If you know an analytical expression between the independent variables (here $t = x/v$), you can also pass it directly to the function to avoid the extra differential equation. ## 2. Alleviating stiffness by changing to logarithmic time In cosmology, the [Friedmann equations](https://en.wikipedia.org/wiki/Friedmann_equations) describe the expansion of the universe. In terms of conformal time $t$, they can be written + ```@example changeivar @variables a(t) Ω(t) a = GlobalScope(a) # global var needed by all species function species(w; kw...) - eqs = [D(Ω) ~ -3(1 + w) * D(a)/a * Ω] + eqs = [D(Ω) ~ -3(1 + w) * D(a) / a * Ω] return ODESystem(eqs, t, [Ω], []; kw...) end -@named r = species(1//3) # radiation +@named r = species(1 // 3) # radiation @named m = species(0) # matter @named Λ = species(-1) # dark energy / cosmological constant -eqs = [ - Ω ~ r.Ω + m.Ω + Λ.Ω # total energy density - D(a) ~ √(Ω) * a^2 -] -initialization_eqs = [ - Λ.Ω + r.Ω + m.Ω ~ 1 -] +eqs = [Ω ~ r.Ω + m.Ω + Λ.Ω, D(a) ~ √(Ω) * a^2] +initialization_eqs = [Λ.Ω + r.Ω + m.Ω ~ 1] M1 = ODESystem(eqs, t, [Ω, a], []; initialization_eqs, name = :M) M1 = compose(M1, r, m, Λ) M1s = structural_simplify(M1) ``` + Of course, we can solve this ODE as it is: + ```@example changeivar prob = ODEProblem(M1s, [M1s.a => 1.0, M1s.r.Ω => 5e-5, M1s.m.Ω => 0.3], (0.0, -3.3), []) sol = solve(prob, Tsit5(); reltol = 1e-5) @assert Symbol(sol.retcode) == :Unstable # surrounding text assumes this was unstable # hide -plot(sol, idxs = [M1.a, M1.r.Ω/M1.Ω, M1.m.Ω/M1.Ω, M1.Λ.Ω/M1.Ω]) +plot(sol, idxs = [M1.a, M1.r.Ω / M1.Ω, M1.m.Ω / M1.Ω, M1.Λ.Ω / M1.Ω]) ``` -The solver becomes unstable due to stiffness. + +But the solver becomes unstable due to stiffness. Also notice the interesting dynamics taking place towards the end of the integration (in the early universe), which gets compressed into a very small time interval. -These ODEs would benefit from being defined with respect to a logarithmic "time" that better captures the evolution of the universe through *orders of magnitude* of time. +These ODEs would benefit from being defined with respect to a logarithmic "time" that better captures the evolution of the universe through *orders of magnitude* of time, rather than linear time. It is therefore common to write these ODEs in terms of $b = \ln a$. -To do this, we will change the independent variable in two stages; from $t$ to $a$ to $b$. -Notice that $\mathrm{d}a/\mathrm{d}t > 0$ provided that $\Omega > 0$, and $\mathrm{d}b/\mathrm{d}a > 0$, so the transformation is well-defined. +To do this, we will change the independent variable in two stages; first from $t$ to $a$, and then from $a$ to $b$. +Notice that $\mathrm{d}a/\mathrm{d}t > 0$ provided that $\Omega > 0$, and $\mathrm{d}b/\mathrm{d}a > 0$, so the transformation is well-defined since $t \leftrightarrow a \leftrightarrow b$ are one-to-one. First, we transform from $t$ to $a$: + ```@example changeivar M2 = change_independent_variable(M1, M1.a) +@assert !ModelingToolkit.isautonomous(M2) # hide +M2 # hide ``` + Unlike the original, notice that this system is *non-autonomous* because the independent variable $a$ appears explicitly in the equations! This means that to change the independent variable from $a$ to $b$, we must provide not only the rate of change relation $db(a)/da = \exp(-b)$, but *also* the equation $a(b) = \exp(b)$ so $a$ can be eliminated in favor of $b$: + ```@example changeivar a = M2.a Da = Differential(a) @variables b(a) M3 = change_independent_variable(M2, b, [Da(b) ~ exp(-b), a ~ exp(b)]) ``` + We can now solve and plot the ODE in terms of $b$: + ```@example changeivar M3s = structural_simplify(M3; allow_symbolic = true) prob = ODEProblem(M3s, [M3s.r.Ω => 5e-5, M3s.m.Ω => 0.3], (0, -15), []) sol = solve(prob, Tsit5()) @assert Symbol(sol.retcode) == :Success # surrounding text assumes the solution was successful # hide -plot(sol, idxs = [M3.r.Ω/M3.Ω, M3.m.Ω/M3.Ω, M3.Λ.Ω/M3.Ω]) +plot(sol, idxs = [M3.r.Ω / M3.Ω, M3.m.Ω / M3.Ω, M3.Λ.Ω / M3.Ω]) ``` + Notice that the variables vary "more nicely" with respect to $b$ than $t$, making the solver happier and avoiding numerical problems. diff --git a/src/ModelingToolkit.jl b/src/ModelingToolkit.jl index 51156ba993..41f9292c73 100644 --- a/src/ModelingToolkit.jl +++ b/src/ModelingToolkit.jl @@ -255,7 +255,8 @@ export isinput, isoutput, getbounds, hasbounds, getguess, hasguess, isdisturbanc tunable_parameters, isirreducible, getdescription, hasdescription, hasunit, getunit, hasconnect, getconnect, hasmisc, getmisc, state_priority -export ode_order_lowering, dae_order_lowering, liouville_transform, change_independent_variable +export ode_order_lowering, dae_order_lowering, liouville_transform, + change_independent_variable export PDESystem export Differential, expand_derivatives, @derivatives export Equation, ConstrainedEquation diff --git a/src/systems/diffeqs/basic_transformations.jl b/src/systems/diffeqs/basic_transformations.jl index c05e5e4394..a08c83ffb6 100644 --- a/src/systems/diffeqs/basic_transformations.jl +++ b/src/systems/diffeqs/basic_transformations.jl @@ -47,11 +47,17 @@ function liouville_transform(sys::AbstractODESystem; kwargs...) neweq = D(trJ) ~ trJ * -tr(calculate_jacobian(sys)) neweqs = [equations(sys); neweq] vars = [unknowns(sys); trJ] - ODESystem(neweqs, t, vars, parameters(sys); checks = false, name = nameof(sys), kwargs...) + ODESystem( + neweqs, t, vars, parameters(sys); + checks = false, name = nameof(sys), kwargs... + ) end """ - change_independent_variable(sys::AbstractODESystem, iv, eqs = []; add_old_diff = false, simplify = true, fold = false, kwargs...) + change_independent_variable( + sys::AbstractODESystem, iv, eqs = []; + add_old_diff = false, simplify = true, fold = false + ) Transform the independent variable (e.g. ``t``) of the ODE system `sys` to a dependent variable `iv` (e.g. ``u(t)``). The transformation is well-defined when the mapping between the new and old independent variables are one-to-one. @@ -68,13 +74,13 @@ Any extra equations `eqs` involving the new and old independent variables will b # Usage before structural simplification The variable change must take place before structural simplification. -Subsequently, consider passing `allow_symbolic = true` to `structural_simplify(sys)` to reduce the number of unknowns, with the understanding that the transformation is well-defined. +In following calls to `structural_simplify`, consider passing `allow_symbolic = true` to avoid undesired constraint equations between between dummy variables. # Usage with non-autonomous systems -If `sys` is non-autonomous (i.e. ``t`` appears explicitly in its equations), it is often desirable to also pass an algebraic equation relating the new and old independent variables (e.g. ``t = f(u(t))``). -Otherwise the transformed system will be underdetermined and cannot be structurally simplified without additional changes. -If an algebraic relation is not known, consider using `add_old_diff`. +If `sys` is non-autonomous (i.e. ``t`` appears explicitly in its equations), consider passing an algebraic equation relating the new and old independent variables (e.g. ``t = f(u(t))``). +Otherwise the transformed system can be underdetermined. +If an algebraic relation is not known, consider using `add_old_diff` instead. # Usage with hierarchical systems @@ -102,7 +108,10 @@ julia> unknowns(M) yˍx(x) ``` """ -function change_independent_variable(sys::AbstractODESystem, iv, eqs = []; add_old_diff = false, simplify = true, fold = false, kwargs...) +function change_independent_variable( + sys::AbstractODESystem, iv, eqs = []; + add_old_diff = false, simplify = true, fold = false +) iv2_of_iv1 = unwrap(iv) # e.g. u(t) iv1 = get_iv(sys) # e.g. t @@ -114,6 +123,7 @@ function change_independent_variable(sys::AbstractODESystem, iv, eqs = []; add_o error("Variable $iv is not a function of the independent variable $iv1 of the system $(nameof(sys))!") end + # Set up intermediate and final variables for the transformation iv1name = nameof(iv1) # e.g. :t iv2name = nameof(operation(iv2_of_iv1)) # e.g. :u iv2, = @independent_variables $iv2name # e.g. u @@ -127,23 +137,22 @@ function change_independent_variable(sys::AbstractODESystem, iv, eqs = []; add_o if add_old_diff eqs = [eqs; Differential(iv2)(iv1_of_iv2) ~ 1 / div2_of_iv2] # e.g. dt(u)/du ~ 1 / uˍt(u) (https://en.wikipedia.org/wiki/Inverse_function_rule) end - @set! sys.eqs = [get_eqs(sys); eqs] # add extra equations we derived before starting transformation process - @set! sys.unknowns = [get_unknowns(sys); [iv1, div2_of_iv1]] # add new variables, will be transformed to e.g. t(u) and uˍt(u) # add dummy variables and old independent variable as a function of the new one + @set! sys.eqs = [get_eqs(sys); eqs] # add extra equations we derived + @set! sys.unknowns = [get_unknowns(sys); [iv1, div2_of_iv1]] # add new variables, will be transformed to e.g. t(u) and uˍt(u) - # Create a utility that performs the chain rule on an expression, followed by insertion of the new independent variable - # e.g. (d/dt)(f(t)) -> (d/dt)(f(u(t))) -> df(u(t))/du(t) * du(t)/dt -> df(u)/du * uˍt(u) - # Then use it to transform everything in the system! + # Create a utility that performs the chain rule on an expression, followed by insertion of the new independent variable: + # e.g. (d/dt)(f(t)) -> (d/dt)(f(u(t))) -> df(u(t))/du(t) * du(t)/dt -> df(u)/du * uˍt(u) function transform(ex) # 1) Replace the argument of every function; e.g. f(t) -> f(u(t)) for var in vars(ex; op = Nothing) # loop over all variables in expression (op = Nothing prevents interpreting "D(f(t))" as one big variable) - is_function_of_iv1 = iscall(var) && isequal(only(arguments(var)), iv1) # is the expression of the form f(t)? + is_function_of_iv1 = iscall(var) && isequal(only(arguments(var)), iv1) # of the form f(t)? if is_function_of_iv1 && !isequal(var, iv2_of_iv1) # prevent e.g. u(t) -> u(u(t)) var_of_iv1 = var # e.g. f(t) var_of_iv2_of_iv1 = substitute(var_of_iv1, iv1 => iv2_of_iv1) # e.g. f(u(t)) ex = substitute(ex, var_of_iv1 => var_of_iv2_of_iv1; fold) end end - # 2) Expand with chain rule until nothing changes anymore + # 2) Repeatedly expand chain rule until nothing changes anymore orgex = nothing while !isequal(ex, orgex) orgex = ex # save original @@ -156,22 +165,25 @@ function change_independent_variable(sys::AbstractODESystem, iv, eqs = []; add_o return ex end + # Use the utility function to transform everything in the system! function transform(sys::AbstractODESystem) eqs = map(transform, get_eqs(sys)) unknowns = map(transform, get_unknowns(sys)) unknowns = filter(var -> !isequal(var, iv2), unknowns) # remove e.g. u ps = map(transform, get_ps(sys)) - ps = filter(!isinitial, ps) # remove Initial(...) # # TODO: shouldn't have to touch this + ps = filter(!isinitial, ps) # remove Initial(...) # TODO: shouldn't have to touch this observed = map(transform, get_observed(sys)) initialization_eqs = map(transform, get_initialization_eqs(sys)) parameter_dependencies = map(transform, get_parameter_dependencies(sys)) - defaults = Dict(transform(var) => transform(val) for (var, val) in get_defaults(sys)) + defaults = Dict(transform(var) => transform(val) + for (var, val) in get_defaults(sys)) guesses = Dict(transform(var) => transform(val) for (var, val) in get_guesses(sys)) - assertions = Dict(transform(condition) => msg for (condition, msg) in get_assertions(sys)) + assertions = Dict(transform(ass) => msg for (ass, msg) in get_assertions(sys)) systems = get_systems(sys) # save before reconstructing system wascomplete = iscomplete(sys) # save before reconstructing system sys = typeof(sys)( # recreate system with transformed fields - eqs, iv2, unknowns, ps; observed, initialization_eqs, parameter_dependencies, defaults, guesses, + eqs, iv2, unknowns, ps; observed, initialization_eqs, + parameter_dependencies, defaults, guesses, assertions, name = nameof(sys), description = description(sys) ) systems = map(transform, systems) # recurse through subsystems @@ -182,6 +194,5 @@ function change_independent_variable(sys::AbstractODESystem, iv, eqs = []; add_o end return sys end - return transform(sys) end diff --git a/test/basic_transformations.jl b/test/basic_transformations.jl index dd6d677b60..544a89cd29 100644 --- a/test/basic_transformations.jl +++ b/test/basic_transformations.jl @@ -32,16 +32,19 @@ end M2 = change_independent_variable(M1, y) @variables y x(y) yˍt(y) Dy = Differential(y) - @test Set(equations(M2)) == Set([yˍt^2*(Dy^2)(x) + yˍt*Dy(yˍt)*Dy(x) ~ x + Dy(x)*yˍt, yˍt ~ 1]) + @test Set(equations(M2)) == Set([ + yˍt^2 * (Dy^2)(x) + yˍt * Dy(yˍt) * Dy(x) ~ x + Dy(x) * yˍt, + yˍt ~ 1 + ]) end @testset "Change independent variable" begin @variables x(t) y(t) z(t) s(t) eqs = [ - D(x) ~ y - D(D(y)) ~ 2 * x * D(y) - z ~ x + D(y) - D(s) ~ 1 / (2*s) + D(x) ~ y, + D(D(y)) ~ 2 * x * D(y), + z ~ x + D(y), + D(s) ~ 1 / (2 * s) ] initialization_eqs = [x ~ 1.0, y ~ 1.0, D(y) ~ 0.0] M1 = ODESystem(eqs, t; initialization_eqs, name = :M) @@ -55,8 +58,8 @@ end sol2 = solve(prob2, Tsit5(); reltol = 1e-10, abstol = 1e-10) ts = range(0.0, 1.0, length = 50) ss = .√(ts) - @test all(isapprox.(sol1(ts, idxs=M1.x), sol2(ss, idxs=M2.x); atol = 1e-7)) && - all(isapprox.(sol1(ts, idxs=M1.y), sol2(ss, idxs=M2.y); atol = 1e-7)) + @test all(isapprox.(sol1(ts, idxs = M1.x), sol2(ss, idxs = M2.x); atol = 1e-7)) && + all(isapprox.(sol1(ts, idxs = M1.y), sol2(ss, idxs = M2.y); atol = 1e-7)) end @testset "Change independent variable (Friedmann equation)" begin @@ -64,15 +67,15 @@ end D = Differential(t) @variables a(t) ȧ(t) Ω(t) ϕ(t) a, ȧ = GlobalScope.([a, ȧ]) - species(w; kw...) = ODESystem([D(Ω) ~ -3(1 + w) * D(a)/a * Ω], t, [Ω], []; kw...) - @named r = species(1//3) + species(w; kw...) = ODESystem([D(Ω) ~ -3(1 + w) * D(a) / a * Ω], t, [Ω], []; kw...) + @named r = species(1 // 3) @named m = species(0) @named Λ = species(-1) eqs = [ - Ω ~ r.Ω + m.Ω + Λ.Ω - D(a) ~ ȧ - ȧ ~ √(Ω) * a^2 - D(D(ϕ)) ~ -3*D(a)/a*D(ϕ) + Ω ~ r.Ω + m.Ω + Λ.Ω, + D(a) ~ ȧ, + ȧ ~ √(Ω) * a^2, + D(D(ϕ)) ~ -3 * D(a) / a * D(ϕ) ] M1 = ODESystem(eqs, t, [Ω, a, ȧ, ϕ], []; name = :M) M1 = compose(M1, r, m, Λ) @@ -80,20 +83,22 @@ end # Apply in two steps, where derivatives are defined at each step: first t -> a, then a -> b M2 = change_independent_variable(M1, M1.a) M2c = complete(M2) # just for the following equation comparison (without namespacing) - a, ȧ, Ω, Ωr, Ωm, ΩΛ, ϕ, aˍt = M2c.a, M2c.ȧ, M2c.Ω, M2c.r.Ω, M2c.m.Ω, M2c.Λ.Ω, M2c.ϕ, M2c.aˍt + a, ȧ, Ω, ϕ, aˍt = M2c.a, M2c.ȧ, M2c.Ω, M2c.ϕ, M2c.aˍt + Ωr, Ωm, ΩΛ = M2c.r.Ω, M2c.m.Ω, M2c.Λ.Ω Da = Differential(a) @test Set(equations(M2)) == Set([ - aˍt ~ ȧ # dummy equation - Ω ~ Ωr + Ωm + ΩΛ - ȧ ~ √(Ω) * a^2 - Da(aˍt)*Da(ϕ)*aˍt + aˍt^2*(Da^2)(ϕ) ~ -3*aˍt^2/a*Da(ϕ) - aˍt*Da(Ωr) ~ -4*Ωr*aˍt/a - aˍt*Da(Ωm) ~ -3*Ωm*aˍt/a - aˍt*Da(ΩΛ) ~ 0 + aˍt ~ ȧ, # dummy equation + Ω ~ Ωr + Ωm + ΩΛ, + ȧ ~ √(Ω) * a^2, + Da(aˍt) * Da(ϕ) * aˍt + aˍt^2 * (Da^2)(ϕ) ~ -3 * aˍt^2 / a * Da(ϕ), + aˍt * Da(Ωr) ~ -4 * Ωr * aˍt / a, + aˍt * Da(Ωm) ~ -3 * Ωm * aˍt / a, + aˍt * Da(ΩΛ) ~ 0 ]) @variables b(M2.a) - M3 = change_independent_variable(M2, b, [Differential(M2.a)(b) ~ exp(-b), M2.a ~ exp(b)]) + extraeqs = [Differential(M2.a)(b) ~ exp(-b), M2.a ~ exp(b)] + M3 = change_independent_variable(M2, b, extraeqs) M1 = structural_simplify(M1) M2 = structural_simplify(M2; allow_symbolic = true) @@ -103,55 +108,69 @@ end @testset "Change independent variable (simple)" begin @variables x(t) y1(t) # y(t)[1:1] # TODO: use array variables y(t)[1:2] when fixed: https://github.com/JuliaSymbolics/Symbolics.jl/issues/1383 - Mt = ODESystem([D(x) ~ 2*x, D(y1) ~ y1], t; name = :M) + Mt = ODESystem([D(x) ~ 2 * x, D(y1) ~ y1], t; name = :M) Mx = change_independent_variable(Mt, x) @variables x xˍt(x) xˍtt(x) y1(x) # y(x)[1:1] # TODO: array variables Dx = Differential(x) - @test (Set(equations(Mx)) == Set([xˍt ~ 2*x, xˍt*Dx(y1) ~ y1])) + @test Set(equations(Mx)) == Set([xˍt ~ 2 * x, xˍt * Dx(y1) ~ y1]) end @testset "Change independent variable (free fall with 1st order horizontal equation)" begin @variables x(t) y(t) - @parameters g v # gravitational acceleration and constant horizontal velocity + @parameters g=9.81 v # gravitational acceleration and constant horizontal velocity Mt = ODESystem([D(D(y)) ~ -g, D(x) ~ v], t; name = :M) # gives (x, y) as function of t, ... Mx = change_independent_variable(Mt, x; add_old_diff = true) # ... but we want y as a function of x Mx = structural_simplify(Mx; allow_symbolic = true) Dx = Differential(Mx.x) - prob = ODEProblem(Mx, [Mx.y => 0.0, Dx(Mx.y) => 1.0, Mx.t => 0.0], (0.0, 20.0), [g => 9.81, v => 10.0]) # 1 = dy/dx = (dy/dt)/(dx/dt) means equal initial horizontal and vertical velocities + u0 = [Mx.y => 0.0, Dx(Mx.y) => 1.0, Mx.t => 0.0] + p = [v => 10.0] + prob = ODEProblem(Mx, u0, (0.0, 20.0), p) # 1 = dy/dx = (dy/dt)/(dx/dt) means equal initial horizontal and vertical velocities sol = solve(prob, Tsit5(); reltol = 1e-5) - @test all(isapprox.(sol[Mx.y], sol[Mx.x - g*(Mx.t)^2/2]; atol = 1e-10)) # compare to analytical solution (x(t) = v*t, y(t) = v*t - g*t^2/2) + @test all(isapprox.(sol[Mx.y], sol[Mx.x - g * (Mx.t)^2 / 2]; atol = 1e-10)) # compare to analytical solution (x(t) = v*t, y(t) = v*t - g*t^2/2) end @testset "Change independent variable (free fall with 2nd order horizontal equation)" begin @variables x(t) y(t) - @parameters g # gravitational acceleration + @parameters g = 9.81 # gravitational acceleration Mt = ODESystem([D(D(y)) ~ -g, D(D(x)) ~ 0], t; name = :M) # gives (x, y) as function of t, ... Mx = change_independent_variable(Mt, x; add_old_diff = true) # ... but we want y as a function of x Mx = structural_simplify(Mx; allow_symbolic = true) Dx = Differential(Mx.x) - prob = ODEProblem(Mx, [Mx.y => 0.0, Dx(Mx.y) => 1.0, Mx.t => 0.0, Mx.xˍt => 10.0], (0.0, 20.0), [g => 9.81]) # 1 = dy/dx = (dy/dt)/(dx/dt) means equal initial horizontal and vertical velocities + u0 = [Mx.y => 0.0, Dx(Mx.y) => 1.0, Mx.t => 0.0, Mx.xˍt => 10.0] + prob = ODEProblem(Mx, u0, (0.0, 20.0), []) # 1 = dy/dx = (dy/dt)/(dx/dt) means equal initial horizontal and vertical velocities sol = solve(prob, Tsit5(); reltol = 1e-5) - @test all(isapprox.(sol[Mx.y], sol[Mx.x - g*(Mx.t)^2/2]; atol = 1e-10)) # compare to analytical solution (x(t) = v*t, y(t) = v*t - g*t^2/2) + @test all(isapprox.(sol[Mx.y], sol[Mx.x - g * (Mx.t)^2 / 2]; atol = 1e-10)) # compare to analytical solution (x(t) = v*t, y(t) = v*t - g*t^2/2) end @testset "Change independent variable (crazy 3rd order nonlinear system)" begin @independent_variables t D = Differential(t) @variables x(t) y(t) - M1 = ODESystem([ - (D^3)(y) ~ D(x)^2 + (D^2)(y^2) |> expand_derivatives + eqs = [ + (D^3)(y) ~ D(x)^2 + (D^2)(y^2) |> expand_derivatives, D(x)^2 + D(y)^2 ~ x^4 + y^5 + t^6 - ], t; name = :M) + ] + M1 = ODESystem(eqs, t; name = :M) M2 = change_independent_variable(M1, x; add_old_diff = true) @test_nowarn structural_simplify(M2) # Compare to pen-and-paper result @variables x xˍt(x) xˍt(x) y(x) t(x) Dx = Differential(x) - areequivalent(eq1, eq2) = isequal(expand(eq1.lhs - eq2.lhs), 0) && isequal(expand(eq1.rhs - eq2.rhs), 0) - @test areequivalent(equations(M2)[1], xˍt^3*(Dx^3)(y) + xˍt^2*Dx(y)*(Dx^2)(xˍt) + xˍt*Dx(y)*(Dx(xˍt))^2 + 3*xˍt^2*(Dx^2)(y)*Dx(xˍt) ~ xˍt^2 + 2*xˍt^2*Dx(y)^2 + 2*xˍt^2*y*(Dx^2)(y) + 2*y*Dx(y)*Dx(xˍt)*xˍt) - @test areequivalent(equations(M2)[2], xˍt^2 + xˍt^2*Dx(y)^2 ~ x^4 + y^5 + t^6) - @test areequivalent(equations(M2)[3], Dx(t) ~ 1 / xˍt) + areequivalent(eq1, eq2) = isequal(expand(eq1.lhs - eq2.lhs), 0) && + isequal(expand(eq1.rhs - eq2.rhs), 0) + eq1lhs = xˍt^3 * (Dx^3)(y) + xˍt^2 * Dx(y) * (Dx^2)(xˍt) + + xˍt * Dx(y) * (Dx(xˍt))^2 + + 3 * xˍt^2 * (Dx^2)(y) * Dx(xˍt) + eq1rhs = xˍt^2 + 2 * xˍt^2 * Dx(y)^2 + + 2 * xˍt^2 * y * (Dx^2)(y) + + 2 * y * Dx(y) * Dx(xˍt) * xˍt + eq1 = eq1lhs ~ eq1rhs + eq2 = xˍt^2 + xˍt^2 * Dx(y)^2 ~ x^4 + y^5 + t^6 + eq3 = Dx(t) ~ 1 / xˍt + @test areequivalent(equations(M2)[1], eq1) + @test areequivalent(equations(M2)[2], eq2) + @test areequivalent(equations(M2)[3], eq3) end @testset "Change independent variable (registered function / callable parameter)" begin @@ -161,35 +180,38 @@ end @parameters f::LinearInterpolation (fc::LinearInterpolation)(..) # non-callable and callable callme(interp::LinearInterpolation, input) = interp(input) @register_symbolic callme(interp::LinearInterpolation, input) - M1 = ODESystem([ - D(x) ~ 2*t - D(y) ~ 1*fc(t) + 2*fc(x) + 3*fc(y) + 1*callme(f, t) + 2*callme(f, x) + 3*callme(f, y) - ], t; name = :M) + eqs = [ + D(x) ~ 2t, + D(y) ~ 1fc(t) + 2fc(x) + 3fc(y) + 1callme(f, t) + 2callme(f, x) + 3callme(f, y) + ] + M1 = ODESystem(eqs, t; name = :M) # Ensure that interpolations are called with the same variables M2 = change_independent_variable(M1, x, [t ~ √(x)]) @variables x xˍt(x) y(x) t(x) Dx = Differential(x) @test Set(equations(M2)) == Set([ - t ~ √(x) - xˍt ~ 2*t - xˍt*Dx(y) ~ 1*fc(t) + 2*fc(x) + 3*fc(y) + 1*callme(f, t) + 2*callme(f, x) + 3*callme(f, y) + t ~ √(x), + xˍt ~ 2t, + xˍt * Dx(y) ~ 1fc(t) + 2fc(x) + 3fc(y) + + 1callme(f, t) + 2callme(f, x) + 3callme(f, y) ]) _f = LinearInterpolation([1.0, 1.0], [-100.0, +100.0]) # constant value 1 M2s = structural_simplify(M2; allow_symbolic = true) prob = ODEProblem(M2s, [M2s.y => 0.0], (1.0, 4.0), [fc => _f, f => _f]) sol = solve(prob, Tsit5(); abstol = 1e-5) - @test isapprox(sol(4.0, idxs=M2.y), 12.0; atol = 1e-5) # Anal solution is D(y) ~ 12 => y(t) ~ 12*t + C => y(x) ~ 12*√(x) + C. With y(x=1)=0 => 12*(√(x)-1), so y(x=4) ~ 12 + @test isapprox(sol(4.0, idxs = M2.y), 12.0; atol = 1e-5) # Anal solution is D(y) ~ 12 => y(t) ~ 12*t + C => y(x) ~ 12*√(x) + C. With y(x=1)=0 => 12*(√(x)-1), so y(x=4) ~ 12 end @testset "Change independent variable (errors)" begin @variables x(t) y z(y) w(t) v(t) M = ODESystem([D(x) ~ 1, v ~ x], t; name = :M) - @test_throws "structurally simplified" change_independent_variable(structural_simplify(M), y) - @test_throws "not a function of the independent variable" change_independent_variable(M, y) - @test_throws "not a function of the independent variable" change_independent_variable(M, z) + Ms = structural_simplify(M) + @test_throws "structurally simplified" change_independent_variable(Ms, y) + @test_throws "not a function of" change_independent_variable(M, y) + @test_throws "not a function of" change_independent_variable(M, z) @variables x(..) # require explicit argument - M = ODESystem([D(x(t)) ~ x(t-1)], t; name = :M) + M = ODESystem([D(x(t)) ~ x(t - 1)], t; name = :M) @test_throws "DDE" change_independent_variable(M, x(t)) end From a98397ca205309603e87f2c12433ffd49cfd0296 Mon Sep 17 00:00:00 2001 From: vyudu Date: Mon, 10 Mar 2025 15:15:38 -0400 Subject: [PATCH 1094/2176] remove Next and Prev --- src/discretedomain.jl | 13 ------------- .../discrete_system/implicit_discrete_system.jl | 6 +++--- 2 files changed, 3 insertions(+), 16 deletions(-) diff --git a/src/discretedomain.jl b/src/discretedomain.jl index 0d8d38e15d..7260237053 100644 --- a/src/discretedomain.jl +++ b/src/discretedomain.jl @@ -70,19 +70,6 @@ Base.literal_pow(f::typeof(^), D::Shift, ::Val{n}) where {n} = Shift(D.t, D.step hasshift(eq::Equation) = hasshift(eq.lhs) || hasshift(eq.rhs) -""" - Next(x) - -An alias for Shift(t, 1)(x). -""" -Next(x) = Shift(t, 1)(x) -""" - Prev(x) - -An alias for Shift(t, -1)(x). -""" -Prev(x) = Shift(t, -1)(x) - """ hasshift(O) diff --git a/src/systems/discrete_system/implicit_discrete_system.jl b/src/systems/discrete_system/implicit_discrete_system.jl index ae5524768b..327c82c47f 100644 --- a/src/systems/discrete_system/implicit_discrete_system.jl +++ b/src/systems/discrete_system/implicit_discrete_system.jl @@ -268,17 +268,17 @@ function generate_function( iv = get_iv(sys) # Algebraic equations get shifted forward 1, to match with differential equations exprs = map(equations(sys)) do eq - _iszero(eq.lhs) ? distribute_shift(Next(eq.rhs)) : (eq.rhs - eq.lhs) + _iszero(eq.lhs) ? distribute_shift(Shift(iv, 1)(eq.rhs)) : (eq.rhs - eq.lhs) end # Handle observables in algebraic equations, since they are shifted obs = observed(sys) - shifted_obs = Symbolics.Equation[distribute_shift(Next(eq)) for eq in obs] + shifted_obs = Symbolics.Equation[distribute_shift(Shift(iv, 1)(eq)) for eq in obs] obsidxs = observed_equations_used_by(sys, exprs; obs = shifted_obs) extra_assignments = [Assignment(shifted_obs[i].lhs, shifted_obs[i].rhs) for i in obsidxs] - u_next = map(Next, dvs) + u_next = map(Shift(iv, 1), dvs) u = dvs build_function_wrapper( sys, exprs, u_next, u, ps..., iv; p_start = 3, extra_assignments, kwargs...) From d40fd6e69f21919d0984f61bf990100653121b99 Mon Sep 17 00:00:00 2001 From: vyudu Date: Mon, 10 Mar 2025 15:16:42 -0400 Subject: [PATCH 1095/2176] remove Next and Prev docs --- docs/src/systems/DiscreteSystem.md | 2 -- docs/src/systems/ImplicitDiscreteSystem.md | 2 -- src/doc | 1 + 3 files changed, 1 insertion(+), 4 deletions(-) create mode 100644 src/doc diff --git a/docs/src/systems/DiscreteSystem.md b/docs/src/systems/DiscreteSystem.md index c00cc7f3c9..f8a71043ab 100644 --- a/docs/src/systems/DiscreteSystem.md +++ b/docs/src/systems/DiscreteSystem.md @@ -31,6 +31,4 @@ DiscreteFunction(sys::DiscreteSystem, args...) ```@docs; canonical=false Shift -Prev -Next ``` diff --git a/docs/src/systems/ImplicitDiscreteSystem.md b/docs/src/systems/ImplicitDiscreteSystem.md index 268d8ec13c..d69f88f106 100644 --- a/docs/src/systems/ImplicitDiscreteSystem.md +++ b/docs/src/systems/ImplicitDiscreteSystem.md @@ -31,6 +31,4 @@ ImplicitDiscreteFunction(sys::ImplicitDiscreteSystem, args...) ```@docs; canonical=false Shift -Prev -Next ``` diff --git a/src/doc b/src/doc new file mode 100644 index 0000000000..8b13789179 --- /dev/null +++ b/src/doc @@ -0,0 +1 @@ + From e87b804aa6012a3570c22c27739825b984cd6869 Mon Sep 17 00:00:00 2001 From: Ashutosh Bharambe Date: Wed, 12 Mar 2025 09:37:51 +0000 Subject: [PATCH 1096/2176] fix: return `Expr` for `expression=true` in `build_explicit_observed_function` --- src/systems/diffeqs/odesystem.jl | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/systems/diffeqs/odesystem.jl b/src/systems/diffeqs/odesystem.jl index 33cdc59909..2660cdbd85 100644 --- a/src/systems/diffeqs/odesystem.jl +++ b/src/systems/diffeqs/odesystem.jl @@ -567,12 +567,18 @@ function build_explicit_observed_function(sys, ts; sys, ts, args...; p_start, p_end, filter_observed = obsfilter, output_type, mkarray, try_namespaced = true, expression = Val{true}) if fns isa Tuple + if expression + return return_inplace ? fns : fns[1] + end oop, iip = eval_or_rgf.(fns; eval_expression, eval_module) f = GeneratedFunctionWrapper{( p_start + is_dde(sys), length(args) - length(ps) + 1 + is_dde(sys), is_split(sys))}( oop, iip) return return_inplace ? (f, f) : f else + if expression + return fns + end f = eval_or_rgf(fns; eval_expression, eval_module) f = GeneratedFunctionWrapper{( p_start + is_dde(sys), length(args) - length(ps) + 1 + is_dde(sys), is_split(sys))}( From ddb385a7073b03a7225dbec6fa72082d7895164b Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 12 Mar 2025 15:31:51 +0530 Subject: [PATCH 1097/2176] feat: add `ignored_connections` field to `ODESystem` --- src/systems/abstractsystem.jl | 1 + src/systems/diffeqs/odesystem.jl | 11 +++++++++-- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index 8945d4022b..2004bbce95 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -878,6 +878,7 @@ for prop in [:eqs :assertions :solved_unknowns :split_idxs + :ignored_connections :parent :is_dde :tstops diff --git a/src/systems/diffeqs/odesystem.jl b/src/systems/diffeqs/odesystem.jl index 33cdc59909..491da2a8d6 100644 --- a/src/systems/diffeqs/odesystem.jl +++ b/src/systems/diffeqs/odesystem.jl @@ -189,6 +189,12 @@ struct ODESystem <: AbstractODESystem """ split_idxs::Union{Nothing, Vector{Vector{Int}}} """ + The connections to ignore (since they're removed by analysis point transformations). + The first element of the tuple are systems that can't be in the same connection set, + and the second are variables (for the trivial form of `connect`). + """ + ignored_connections::Union{Nothing, Tuple{Vector{ODESystem}, Vector{BasicSymbolic}}} + """ The hierarchical parent system before simplification. """ parent::Any @@ -203,7 +209,8 @@ struct ODESystem <: AbstractODESystem tstops = [], tearing_state = nothing, substitutions = nothing, complete = false, index_cache = nothing, discrete_subsystems = nothing, solved_unknowns = nothing, - split_idxs = nothing, parent = nothing; checks::Union{Bool, Int} = true) + split_idxs = nothing, ignored_connections = nothing, parent = nothing; + checks::Union{Bool, Int} = true) if checks == true || (checks & CheckComponents) > 0 check_independent_variables([iv]) check_variables(dvs, iv) @@ -221,7 +228,7 @@ struct ODESystem <: AbstractODESystem initializesystem, initialization_eqs, schedule, connector_type, preface, cevents, devents, parameter_dependencies, assertions, metadata, gui_metadata, is_dde, tstops, tearing_state, substitutions, complete, index_cache, - discrete_subsystems, solved_unknowns, split_idxs, parent) + discrete_subsystems, solved_unknowns, split_idxs, ignored_connections, parent) end end From 0a405f1b9fef71b831a61e6f294aecb49b845368 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 12 Mar 2025 15:32:56 +0530 Subject: [PATCH 1098/2176] feat: track connections removed by analysis point transformations --- src/systems/analysis_points.jl | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/src/systems/analysis_points.jl b/src/systems/analysis_points.jl index 135bf81729..70c41b50c9 100644 --- a/src/systems/analysis_points.jl +++ b/src/systems/analysis_points.jl @@ -412,6 +412,27 @@ function get_analysis_variable(var, name, iv; perturb = true) return pvar, default end +function with_analysis_point_ignored(sys::AbstractSystem, ap::AnalysisPoint) + has_ignored_connections(sys) || return sys + ignored = get_ignored_connections(sys) + if ignored === nothing + ignored = (ODESystem[], BasicSymbolic[]) + else + ignored = copy.(ignored) + end + if ap.outputs === nothing + error("Empty analysis point") + end + for x in ap.outputs + if x isa ODESystem + push!(ignored[1], x) + else + push!(ignored[2], unwrap(x)) + end + end + return @set sys.ignored_connections = ignored +end + #### PRIMITIVE TRANSFORMATIONS const DOC_WILL_REMOVE_AP = """ @@ -469,7 +490,9 @@ function apply_transformation(tf::Break, sys::AbstractSystem) ap = breaksys_eqs[ap_idx].rhs deleteat!(breaksys_eqs, ap_idx) - tf.add_input || return sys, () + breaksys = with_analysis_point_ignored(breaksys, ap) + + tf.add_input || return breaksys, () ap_ivar = ap_var(ap.input) new_var, new_def = get_analysis_variable(ap_ivar, nameof(ap), get_iv(sys)) @@ -511,7 +534,7 @@ function apply_transformation(tf::GetInput, sys::AbstractSystem) ap_idx === nothing && error("Analysis point $(nameof(tf.ap)) not found in system $(nameof(sys)).") # get the anlysis point - ap_sys_eqs = copy(get_eqs(ap_sys)) + ap_sys_eqs = get_eqs(ap_sys) ap = ap_sys_eqs[ap_idx].rhs # input variable @@ -570,6 +593,7 @@ function apply_transformation(tf::PerturbOutput, sys::AbstractSystem) ap = ap_sys_eqs[ap_idx].rhs # remove analysis point deleteat!(ap_sys_eqs, ap_idx) + ap_sys = with_analysis_point_ignored(ap_sys, ap) # add equations involving new variable ap_ivar = ap_var(ap.input) @@ -634,7 +658,7 @@ function apply_transformation(tf::AddVariable, sys::AbstractSystem) ap_idx = analysis_point_index(ap_sys, tf.ap) ap_idx === nothing && error("Analysis point $(nameof(tf.ap)) not found in system $(nameof(sys)).") - ap_sys_eqs = copy(get_eqs(ap_sys)) + ap_sys_eqs = get_eqs(ap_sys) ap = ap_sys_eqs[ap_idx].rhs # add equations involving new variable From 8629c6af85300476374410a06f89254dd3c343c8 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 12 Mar 2025 15:33:43 +0530 Subject: [PATCH 1099/2176] fix: ignore connections removed by AP transforms in `expand_connections` --- src/systems/abstractsystem.jl | 69 ++++++++++++++++++++++++++ src/systems/connectors.jl | 92 +++++++++++++++++++++++++++++++---- 2 files changed, 151 insertions(+), 10 deletions(-) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index 2004bbce95..c0b3601e21 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -1395,6 +1395,75 @@ function assertions(sys::AbstractSystem) return merge(asserts, namespaced_asserts) end +const HierarchyVariableT = Vector{Union{BasicSymbolic, Symbol}} +const HierarchySystemT = Vector{Union{AbstractSystem, Symbol}} +""" +The type returned from `as_hierarchy`. +""" +const HierarchyT = Union{HierarchyVariableT, HierarchySystemT} + +""" + $(TYPEDSIGNATURES) + +The inverse operation of `as_hierarchy`. +""" +function from_hierarchy(hierarchy::HierarchyT) + namefn = hierarchy[1] isa AbstractSystem ? nameof : getname + foldl(@view hierarchy[2:end]; init = hierarchy[1]) do sys, name + rename(sys, Symbol(name, NAMESPACE_SEPARATOR, namefn(sys))) + end +end + +""" + $(TYPEDSIGNATURES) + +Represent a namespaced system (or variable) `sys` as a hierarchy. Return a vector, where +the first element is the unnamespaced system (variable) and subsequent elements are +`Symbol`s representing the parents of the unnamespaced system (variable) in order from +inner to outer. +""" +function as_hierarchy(sys::Union{AbstractSystem, BasicSymbolic})::HierarchyT + namefn = sys isa AbstractSystem ? nameof : getname + # get the hierarchy + hierarchy = namespace_hierarchy(namefn(sys)) + # rename the system with unnamespaced name + newsys = rename(sys, hierarchy[end]) + # and remove it from the list + pop!(hierarchy) + # reverse it to go from inner to outer + reverse!(hierarchy) + # concatenate + T = sys isa AbstractSystem ? AbstractSystem : BasicSymbolic + return Union{Symbol, T}[newsys; hierarchy] +end + +""" + $(TYPEDSIGNATURES) + +Get the connections to ignore for `sys` and its subsystems. The returned value is a +`Tuple` similar in structure to the `ignored_connections` field. Each system (variable) +in the first (second) element of the tuple is also passed through `as_hierarchy`. +""" +function ignored_connections(sys::AbstractSystem) + has_ignored_connections(sys) || return (HierarchySystemT[], HierarchyVariableT[]) + + ics = get_ignored_connections(sys) + if ics === nothing + ics = (HierarchySystemT[], HierarchyVariableT[]) + end + # turn into hierarchies + ics = (map(as_hierarchy, ics[1]), map(as_hierarchy, ics[2])) + systems = get_systems(sys) + # for each subsystem, get its ignored connections, add the name of the subsystem + # to the hierarchy and concatenate corresponding buffers of the result + result = mapreduce(Broadcast.BroadcastFunction(vcat), systems; init = ics) do subsys + sub_ics = ignored_connections(subsys) + (map(Base.Fix2(push!, nameof(subsys)), sub_ics[1]), + map(Base.Fix2(push!, nameof(subsys)), sub_ics[2])) + end + return (Vector{HierarchySystemT}(result[1]), Vector{HierarchyVariableT}(result[2])) +end + """ $(TYPEDSIGNATURES) diff --git a/src/systems/connectors.jl b/src/systems/connectors.jl index db3349f546..f2ef5ac0f6 100644 --- a/src/systems/connectors.jl +++ b/src/systems/connectors.jl @@ -395,16 +395,43 @@ function generate_connection_set( connectionsets = ConnectionSet[] domain_csets = ConnectionSet[] sys = generate_connection_set!( - connectionsets, domain_csets, sys, find, replace, scalarize) + connectionsets, domain_csets, sys, find, replace, scalarize, nothing, + # include systems to be ignored + ignored_connections(sys)[1]) csets = merge(connectionsets) domain_csets = merge([csets; domain_csets], true) sys, (csets, domain_csets) end +""" + $(TYPEDSIGNATURES) + +Generate connection sets from `connect` equations. + +# Arguments + +- `connectionsets` is the list of connection sets to be populated by recursively + descending `sys`. +- `domain_csets` is the list of connection sets for domain connections. +- `sys` is the system whose equations are to be searched. +- `namespace` is a system representing the namespace in which `sys` exists, or `nothing` + for no namespace (if `sys` is top-level). +- `ignored_systems` is a list of systems (in the format returned by `as_hierarchy`) to + be ignored when generating connections. This is typically because the connections + they are used in were removed by analysis point transformations. +""" function generate_connection_set!(connectionsets, domain_csets, - sys::AbstractSystem, find, replace, scalarize, namespace = nothing) + sys::AbstractSystem, find, replace, scalarize, namespace = nothing, ignored_systems = []) subsys = get_systems(sys) + # turn hierarchies into namespaced systems + namespaced_ignored = from_hierarchy.(ignored_systems) + # filter the subsystems of `sys` to exclude ignored ones + filtered_subsys = filter(subsys) do ss + all(namespaced_ignored) do igsys + nameof(igsys) != nameof(ss) + end + end isouter = generate_isouter(sys) eqs′ = get_eqs(sys) @@ -430,9 +457,21 @@ function generate_connection_set!(connectionsets, domain_csets, neweq isa AbstractArray ? append!(eqs, neweq) : push!(eqs, neweq) else if lhs isa Connection && get_systems(lhs) === :domain - connection2set!(domain_csets, namespace, get_systems(rhs), isouter) + # don't consider systems that should be ignored + systems_to_connect = filter(get_systems(rhs)) do ss + all(namespaced_ignored) do igsys + nameof(igsys) != nameof(ss) + end + end + connection2set!(domain_csets, namespace, systems_to_connect, isouter) elseif isconnection(rhs) - push!(cts, get_systems(rhs)) + # ignore required systems + systems_to_connect = filter(get_systems(rhs)) do ss + all(namespaced_ignored) do igsys + nameof(igsys) != nameof(ss) + end + end + push!(cts, systems_to_connect) else # split connections and equations if eq.lhs isa AbstractArray || eq.rhs isa AbstractArray @@ -446,7 +485,8 @@ function generate_connection_set!(connectionsets, domain_csets, # all connectors are eventually inside connectors. T = ConnectionElement - for s in subsys + # only generate connection sets for systems that are not ignored + for s in filtered_subsys isconnector(s) || continue is_domain_connector(s) && continue for v in unknowns(s) @@ -465,12 +505,32 @@ function generate_connection_set!(connectionsets, domain_csets, end @set! sys.systems = map( s -> generate_connection_set!(connectionsets, domain_csets, s, - find, replace, scalarize, - renamespace(namespace, s)), + find, replace, scalarize, renamespace(namespace, s), + ignored_systems_for_subsystem(s, ignored_systems)), subsys) @set! sys.eqs = eqs end +""" + $(TYPEDSIGNATURES) + +Given a subsystem `subsys` of a parent system and a list of systems (variables) to be +ignored by `generate_connection_set!` (`expand_variable_connections`), filter +`ignored_systems` to only include those present in the subtree of `subsys` and update +their hierarchy to not include `subsys`. +""" +function ignored_systems_for_subsystem( + subsys::AbstractSystem, ignored_systems::Vector{<:HierarchyT}) + result = eltype(ignored_systems)[] + for igsys in ignored_systems + if igsys[end] == nameof(subsys) + push!(result, copy(igsys)) + pop!(result[end]) + end + end + return result +end + function Base.merge(csets::AbstractVector{<:ConnectionSet}, allouter = false) ele2idx = Dict{ConnectionElement, Int}() idx2ele = ConnectionElement[] @@ -576,7 +636,11 @@ end Recursively descend through the hierarchy of `sys` and expand all connection equations of causal variables. Return the modified system. """ -function expand_variable_connections(sys::AbstractSystem) +function expand_variable_connections(sys::AbstractSystem; ignored_variables = nothing) + if ignored_variables === nothing + ignored_variables = ignored_connections(sys)[2] + end + namespaced_ignored = from_hierarchy.(ignored_variables) eqs = copy(get_eqs(sys)) valid_idxs = trues(length(eqs)) additional_eqs = Equation[] @@ -584,8 +648,13 @@ function expand_variable_connections(sys::AbstractSystem) for (i, eq) in enumerate(eqs) eq.lhs isa Connection || continue connection = eq.rhs - elements = connection.systems + elements = get_systems(connection) is_causal_variable_connection(connection) || continue + elements = filter(elements) do el + all(namespaced_ignored) do var + getname(var) != getname(el.var) + end + end valid_idxs[i] = false elements = map(x -> x.var, elements) @@ -595,7 +664,10 @@ function expand_variable_connections(sys::AbstractSystem) end end eqs = [eqs[valid_idxs]; additional_eqs] - subsystems = map(expand_variable_connections, get_systems(sys)) + subsystems = map(get_systems(sys)) do subsys + expand_variable_connections(subsys; + ignored_variables = ignored_systems_for_subsystem(subsys, ignored_variables)) + end @set! sys.eqs = eqs @set! sys.systems = subsystems return sys From 5507939e48bf98d34d14d8a4599337c5c4d52a61 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 12 Mar 2025 15:33:57 +0530 Subject: [PATCH 1100/2176] test: test analysis point transformations with duplicate connections --- test/downstream/analysis_points.jl | 46 ++++++++++++++++++++++++++---- 1 file changed, 40 insertions(+), 6 deletions(-) diff --git a/test/downstream/analysis_points.jl b/test/downstream/analysis_points.jl index 7a433cb504..6a02bc25b4 100644 --- a/test/downstream/analysis_points.jl +++ b/test/downstream/analysis_points.jl @@ -80,18 +80,50 @@ import ControlSystemsBase as CS @test tf(So) ≈ tf(Si) end -@testset "Analysis points with subsystems" begin +@testset "Duplicate `connect` statements across subsystems with AP transforms - standard `connect`" begin @named P = FirstOrder(k = 1, T = 1) @named C = Gain(; k = 1) @named add = Blocks.Add(k2 = -1) eqs = [connect(P.output, :plant_output, add.input2) connect(add.output, C.input) - connect(C.output, :plant_input, P.input)] + connect(C.output, P.input)] + + sys_inner = ODESystem(eqs, t, systems = [P, C, add], name = :inner) + + @named r = Constant(k = 1) + @named F = FirstOrder(k = 1, T = 3) + + eqs = [connect(r.output, F.input) + connect(sys_inner.P.output, sys_inner.add.input2) + connect(sys_inner.C.output, :plant_input, sys_inner.P.input) + connect(F.output, sys_inner.add.input1)] + sys_outer = ODESystem(eqs, t, systems = [F, sys_inner, r], name = :outer) + + # test first that the structural_simplify works correctly + ssys = structural_simplify(sys_outer) + prob = ODEProblem(ssys, Pair[], (0, 10)) + @test_nowarn solve(prob, Rodas5()) - # eqs = [connect(P.output, add.input2) - # connect(add.output, C.input) - # connect(C.output, P.input)] + matrices, _ = get_sensitivity(sys_outer, sys_outer.plant_input) + lsys = sminreal(ss(matrices...)) + @test lsys.A[] == -2 + @test lsys.B[] * lsys.C[] == -1 # either one negative + @test lsys.D[] == 1 + + matrices_So, _ = get_sensitivity(sys_outer, sys_outer.inner.plant_output) + lsyso = sminreal(ss(matrices_So...)) + @test lsys == lsyso || lsys == -1 * lsyso * (-1) # Output and input sensitivities are equal for SISO systems +end + +@testset "Duplicate `connect` statements across subsystems with AP transforms - causal variable `connect`" begin + @named P = FirstOrder(k = 1, T = 1) + @named C = Gain(; k = 1) + @named add = Blocks.Add(k2 = -1) + + eqs = [connect(P.output.u, :plant_output, add.input2.u) + connect(add.output, C.input) + connect(C.output.u, P.input.u)] sys_inner = ODESystem(eqs, t, systems = [P, C, add], name = :inner) @@ -99,6 +131,8 @@ end @named F = FirstOrder(k = 1, T = 3) eqs = [connect(r.output, F.input) + connect(sys_inner.P.output.u, sys_inner.add.input2.u) + connect(sys_inner.C.output.u, :plant_input, sys_inner.P.input.u) connect(F.output, sys_inner.add.input1)] sys_outer = ODESystem(eqs, t, systems = [F, sys_inner, r], name = :outer) @@ -107,7 +141,7 @@ end prob = ODEProblem(ssys, Pair[], (0, 10)) @test_nowarn solve(prob, Rodas5()) - matrices, _ = get_sensitivity(sys_outer, sys_outer.inner.plant_input) + matrices, _ = get_sensitivity(sys_outer, sys_outer.plant_input) lsys = sminreal(ss(matrices...)) @test lsys.A[] == -2 @test lsys.B[] * lsys.C[] == -1 # either one negative From 6b7939aa56a316b38f84861b93e7968318184e1e Mon Sep 17 00:00:00 2001 From: Ashutosh Bharambe Date: Wed, 12 Mar 2025 11:21:13 +0000 Subject: [PATCH 1101/2176] test: add tests to check Expr is returned --- test/odesystem.jl | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/test/odesystem.jl b/test/odesystem.jl index 87be5bbc54..00f7e76b01 100644 --- a/test/odesystem.jl +++ b/test/odesystem.jl @@ -578,6 +578,11 @@ obsfn = ModelingToolkit.build_explicit_observed_function( outersys, bar(3outersys.sys.ms, 3outersys.sys.p)) @test_nowarn obsfn(sol.u[1], prob.p, sol.t[1]) +obsfn_expr = ModelingToolkit.build_explicit_observed_function( + outersys, bar(3outersys.sys.ms, 3outersys.sys.p), expression = true) +@test obsfn_expr isa Expr + + # x/x @variables x(t) @named sys = ODESystem([D(x) ~ x / x], t) @@ -1225,6 +1230,11 @@ end buffer = zeros(3) @test_nowarn obsfn(buffer, [1.0], ps, 3.0) @test buffer ≈ [2.0, 3.0, 4.0] + + obsfn_expr_oop, obsfn_expr_iip = ModelingToolkit.build_explicit_observed_function( + sys, [x + 1, x + P, x + t], return_inplace = true, expression = true) + @test obsfn_expr_oop isa Expr + @test obsfn_expr_iip isa Expr end # https://github.com/SciML/ModelingToolkit.jl/issues/2818 From 8386886e226d1a3a7e70f4856cc2b8f985ea0ef7 Mon Sep 17 00:00:00 2001 From: Ashutosh Bharambe Date: Wed, 12 Mar 2025 11:57:27 +0000 Subject: [PATCH 1102/2176] chore: fix formatting --- src/systems/diffeqs/odesystem.jl | 4 ++-- test/odesystem.jl | 5 ++--- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/src/systems/diffeqs/odesystem.jl b/src/systems/diffeqs/odesystem.jl index 2660cdbd85..9a082abf5e 100644 --- a/src/systems/diffeqs/odesystem.jl +++ b/src/systems/diffeqs/odesystem.jl @@ -576,8 +576,8 @@ function build_explicit_observed_function(sys, ts; oop, iip) return return_inplace ? (f, f) : f else - if expression - return fns + if expression + return fns end f = eval_or_rgf(fns; eval_expression, eval_module) f = GeneratedFunctionWrapper{( diff --git a/test/odesystem.jl b/test/odesystem.jl index 00f7e76b01..804aca4fd9 100644 --- a/test/odesystem.jl +++ b/test/odesystem.jl @@ -579,10 +579,9 @@ obsfn = ModelingToolkit.build_explicit_observed_function( @test_nowarn obsfn(sol.u[1], prob.p, sol.t[1]) obsfn_expr = ModelingToolkit.build_explicit_observed_function( - outersys, bar(3outersys.sys.ms, 3outersys.sys.p), expression = true) + outersys, bar(3outersys.sys.ms, 3outersys.sys.p), expression = true) @test obsfn_expr isa Expr - # x/x @variables x(t) @named sys = ODESystem([D(x) ~ x / x], t) @@ -1233,7 +1232,7 @@ end obsfn_expr_oop, obsfn_expr_iip = ModelingToolkit.build_explicit_observed_function( sys, [x + 1, x + P, x + t], return_inplace = true, expression = true) - @test obsfn_expr_oop isa Expr + @test obsfn_expr_oop isa Expr @test obsfn_expr_iip isa Expr end From 26668fd0c7e8c8de5d10e24b11bec022ad0fc236 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 12 Mar 2025 18:42:38 +0530 Subject: [PATCH 1103/2176] fix: handle causal variable AP ignoring part of normal `connect` --- src/systems/connectors.jl | 98 ++++++++++++++++++++++-------- test/downstream/analysis_points.jl | 36 +++++++++++ 2 files changed, 107 insertions(+), 27 deletions(-) diff --git a/src/systems/connectors.jl b/src/systems/connectors.jl index f2ef5ac0f6..94971e38ee 100644 --- a/src/systems/connectors.jl +++ b/src/systems/connectors.jl @@ -315,7 +315,34 @@ function ori(sys) end end -function connection2set!(connectionsets, namespace, ss, isouter) +""" + $(TYPEDSIGNATURES) + +Populate `connectionsets` with connections between the connectors `ss`, all of which are +namespaced by `namespace`. + +# Keyword Arguments +- `ignored_connects`: A tuple of the systems and variables for which connections should be + ignored. Of the format returned from `as_hierarchy`. +- `namespaced_ignored_systems`: The `from_hierarchy` versions of entries in + `ignored_connects[1]`, purely to avoid unnecessary recomputation. +""" +function connection2set!(connectionsets, namespace, ss, isouter; + ignored_connects = (HierarchySystemT[], HierarchyVariableT[]), + namespaced_ignored_systems = ODESystem[]) + ignored_systems, ignored_variables = ignored_connects + # ignore specified systems + ss = filter(ss) do s + all(namespaced_ignored_systems) do igsys + nameof(igsys) != nameof(s) + end + end + # `ignored_variables` for each `s` in `ss` + corresponding_ignored_variables = map( + Base.Fix2(ignored_systems_for_subsystem, ignored_variables), ss) + corresponding_namespaced_ignored_variables = map( + Broadcast.BroadcastFunction(from_hierarchy), corresponding_ignored_variables) + regular_ss = [] domain_ss = nothing for s in ss @@ -340,9 +367,12 @@ function connection2set!(connectionsets, namespace, ss, isouter) for (i, s) in enumerate(ss) sts = unknowns(s) io = isouter(s) - for (j, v) in enumerate(sts) + _ignored_variables = corresponding_ignored_variables[i] + _namespaced_ignored_variables = corresponding_namespaced_ignored_variables[i] + for v in sts vtype = get_connection_type(v) (vtype === Flow && isequal(v, dv)) || continue + any(isequal(v), _namespaced_ignored_variables) && continue push!(cset, T(LazyNamespace(namespace, domain_ss), dv, false)) push!(cset, T(LazyNamespace(namespace, s), v, io)) end @@ -360,6 +390,12 @@ function connection2set!(connectionsets, namespace, ss, isouter) end sts1 = Set(sts1v) num_unknowns = length(sts1) + + # we don't filter here because `csets` should include the full set of unknowns. + # not all of `ss` will have the same (or any) variables filtered so the ones + # that aren't should still go in the right cset. Since `sts1` is only used for + # validating that all systems being connected are of the same type, it has + # unfiltered entries. csets = [T[] for _ in 1:num_unknowns] # Add 9 orientation variables if connection is between multibody frames for (i, s) in enumerate(ss) unknown_vars = unknowns(s) @@ -372,7 +408,10 @@ function connection2set!(connectionsets, namespace, ss, isouter) all(Base.Fix2(in, sts1), unknown_vars)) || connection_error(ss)) io = isouter(s) + # don't `filter!` here so that `j` points to the correct cset regardless of + # which variables are filtered. for (j, v) in enumerate(unknown_vars) + any(isequal(v), corresponding_namespaced_ignored_variables[i]) && continue push!(csets[j], T(LazyNamespace(namespace, s), v, io)) end end @@ -397,7 +436,7 @@ function generate_connection_set( sys = generate_connection_set!( connectionsets, domain_csets, sys, find, replace, scalarize, nothing, # include systems to be ignored - ignored_connections(sys)[1]) + ignored_connections(sys)) csets = merge(connectionsets) domain_csets = merge([csets; domain_csets], true) @@ -417,18 +456,23 @@ Generate connection sets from `connect` equations. - `sys` is the system whose equations are to be searched. - `namespace` is a system representing the namespace in which `sys` exists, or `nothing` for no namespace (if `sys` is top-level). -- `ignored_systems` is a list of systems (in the format returned by `as_hierarchy`) to - be ignored when generating connections. This is typically because the connections - they are used in were removed by analysis point transformations. +- `ignored_connects` is a tuple. The first (second) element is a list of systems + (variables) in the format returned by `as_hierarchy` to be ignored when generating + connections. This is typically because the connections they are used in were removed by + analysis point transformations. """ function generate_connection_set!(connectionsets, domain_csets, - sys::AbstractSystem, find, replace, scalarize, namespace = nothing, ignored_systems = []) + sys::AbstractSystem, find, replace, scalarize, namespace = nothing, + ignored_connects = (HierarchySystemT[], HierarchyVariableT[])) subsys = get_systems(sys) + ignored_systems, ignored_variables = ignored_connects # turn hierarchies into namespaced systems - namespaced_ignored = from_hierarchy.(ignored_systems) + namespaced_ignored_systems = from_hierarchy.(ignored_systems) + namespaced_ignored_variables = from_hierarchy.(ignored_variables) + namespaced_ignored = (namespaced_ignored_systems, namespaced_ignored_variables) # filter the subsystems of `sys` to exclude ignored ones filtered_subsys = filter(subsys) do ss - all(namespaced_ignored) do igsys + all(namespaced_ignored_systems) do igsys nameof(igsys) != nameof(ss) end end @@ -457,21 +501,10 @@ function generate_connection_set!(connectionsets, domain_csets, neweq isa AbstractArray ? append!(eqs, neweq) : push!(eqs, neweq) else if lhs isa Connection && get_systems(lhs) === :domain - # don't consider systems that should be ignored - systems_to_connect = filter(get_systems(rhs)) do ss - all(namespaced_ignored) do igsys - nameof(igsys) != nameof(ss) - end - end - connection2set!(domain_csets, namespace, systems_to_connect, isouter) + connection2set!(domain_csets, namespace, get_systems(rhs), isouter; + ignored_connects, namespaced_ignored_systems) elseif isconnection(rhs) - # ignore required systems - systems_to_connect = filter(get_systems(rhs)) do ss - all(namespaced_ignored) do igsys - nameof(igsys) != nameof(ss) - end - end - push!(cts, systems_to_connect) + push!(cts, get_systems(rhs)) else # split connections and equations if eq.lhs isa AbstractArray || eq.rhs isa AbstractArray @@ -489,14 +522,19 @@ function generate_connection_set!(connectionsets, domain_csets, for s in filtered_subsys isconnector(s) || continue is_domain_connector(s) && continue + _ignored_variables = ignored_systems_for_subsystem(s, ignored_variables) + _namespaced_ignored_variables = from_hierarchy.(_ignored_variables) for v in unknowns(s) Flow === get_connection_type(v) || continue + # ignore specified variables + any(isequal(v), _namespaced_ignored_variables) && continue push!(connectionsets, ConnectionSet([T(LazyNamespace(namespace, s), v, false)])) end end for ct in cts - connection2set!(connectionsets, namespace, ct, isouter) + connection2set!(connectionsets, namespace, ct, isouter; + ignored_connects, namespaced_ignored_systems) end # pre order traversal @@ -506,7 +544,7 @@ function generate_connection_set!(connectionsets, domain_csets, @set! sys.systems = map( s -> generate_connection_set!(connectionsets, domain_csets, s, find, replace, scalarize, renamespace(namespace, s), - ignored_systems_for_subsystem(s, ignored_systems)), + ignored_systems_for_subsystem.((s,), ignored_connects)), subsys) @set! sys.eqs = eqs end @@ -522,10 +560,16 @@ their hierarchy to not include `subsys`. function ignored_systems_for_subsystem( subsys::AbstractSystem, ignored_systems::Vector{<:HierarchyT}) result = eltype(ignored_systems)[] + # in case `subsys` is namespaced, get its hierarchy and compare suffixes + # instead of the just the last element + suffix = reverse!(namespace_hierarchy(nameof(subsys))) + N = length(suffix) for igsys in ignored_systems - if igsys[end] == nameof(subsys) + if igsys[(end - N + 1):end] == suffix push!(result, copy(igsys)) - pop!(result[end]) + for i in 1:N + pop!(result[end]) + end end end return result diff --git a/test/downstream/analysis_points.jl b/test/downstream/analysis_points.jl index 6a02bc25b4..b5a4ba9c84 100644 --- a/test/downstream/analysis_points.jl +++ b/test/downstream/analysis_points.jl @@ -152,6 +152,42 @@ end @test lsys == lsyso || lsys == -1 * lsyso * (-1) # Output and input sensitivities are equal for SISO systems end +@testset "Duplicate `connect` statements across subsystems with AP transforms - mixed `connect`" begin + @named P = FirstOrder(k = 1, T = 1) + @named C = Gain(; k = 1) + @named add = Blocks.Add(k2 = -1) + + eqs = [connect(P.output.u, :plant_output, add.input2.u) + connect(add.output, C.input) + connect(C.output, P.input)] + + sys_inner = ODESystem(eqs, t, systems = [P, C, add], name = :inner) + + @named r = Constant(k = 1) + @named F = FirstOrder(k = 1, T = 3) + + eqs = [connect(r.output, F.input) + connect(sys_inner.P.output, sys_inner.add.input2) + connect(sys_inner.C.output.u, :plant_input, sys_inner.P.input.u) + connect(F.output, sys_inner.add.input1)] + sys_outer = ODESystem(eqs, t, systems = [F, sys_inner, r], name = :outer) + + # test first that the structural_simplify works correctly + ssys = structural_simplify(sys_outer) + prob = ODEProblem(ssys, Pair[], (0, 10)) + @test_nowarn solve(prob, Rodas5()) + + matrices, _ = get_sensitivity(sys_outer, sys_outer.plant_input) + lsys = sminreal(ss(matrices...)) + @test lsys.A[] == -2 + @test lsys.B[] * lsys.C[] == -1 # either one negative + @test lsys.D[] == 1 + + matrices_So, _ = get_sensitivity(sys_outer, sys_outer.inner.plant_output) + lsyso = sminreal(ss(matrices_So...)) + @test lsys == lsyso || lsys == -1 * lsyso * (-1) # Output and input sensitivities are equal for SISO systems +end + @testset "multilevel system with loop openings" begin @named P_inner = FirstOrder(k = 1, T = 1) @named feedback = Feedback() From 1de31d06ef7357ac661ed563249a2e6a81de20f7 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 12 Mar 2025 18:42:52 +0530 Subject: [PATCH 1104/2176] test: test multi-input `connect` with single-input AP --- test/downstream/analysis_points.jl | 98 ++++++++++++++++++++++++++++++ 1 file changed, 98 insertions(+) diff --git a/test/downstream/analysis_points.jl b/test/downstream/analysis_points.jl index b5a4ba9c84..62961f181a 100644 --- a/test/downstream/analysis_points.jl +++ b/test/downstream/analysis_points.jl @@ -340,3 +340,101 @@ end G = CS.ss(matrices...) |> sminreal @test tf(G) ≈ tf(CS.feedback(Ps, Cs)) end + +function normal_test_system() + @named F1 = FirstOrder(k = 1, T = 1) + @named F2 = FirstOrder(k = 1, T = 1) + @named add = Blocks.Add(k1 = 1, k2 = 2) + @named back = Feedback() + + eqs_normal = [connect(back.output, :ap, F1.input) + connect(back.output, F2.input) + connect(F1.output, add.input1) + connect(F2.output, add.input2) + connect(add.output, back.input2)] + @named normal_inner = ODESystem(eqs_normal, t; systems = [F1, F2, add, back]) + + @named step = Step() + eqs2_normal = [ + connect(step.output, normal_inner.back.input1) + ] + @named sys_normal = ODESystem(eqs2_normal, t; systems = [normal_inner, step]) +end + +sys_normal = normal_test_system() + +prob = ODEProblem(structural_simplify(sys_normal), [], (0.0, 10.0)) +@test SciMLBase.successful_retcode(solve(prob, Rodas5P())) +matrices_normal, _ = get_sensitivity(sys_normal, sys_normal.normal_inner.ap) + +@testset "Analysis point overriding part of connection - normal connect" begin + @named F1 = FirstOrder(k = 1, T = 1) + @named F2 = FirstOrder(k = 1, T = 1) + @named add = Blocks.Add(k1 = 1, k2 = 2) + @named back = Feedback() + + eqs = [connect(back.output, F1.input, F2.input) + connect(F1.output, add.input1) + connect(F2.output, add.input2) + connect(add.output, back.input2)] + @named inner = ODESystem(eqs, t; systems = [F1, F2, add, back]) + + @named step = Step() + eqs2 = [connect(step.output, inner.back.input1) + connect(inner.back.output, :ap, inner.F1.input)] + @named sys = ODESystem(eqs2, t; systems = [inner, step]) + + prob = ODEProblem(structural_simplify(sys), [], (0.0, 10.0)) + @test SciMLBase.successful_retcode(solve(prob, Rodas5P())) + + matrices, _ = get_sensitivity(sys, sys.ap) + @test matrices == matrices_normal +end + +@testset "Analysis point overriding part of connection - variable connect" begin + @named F1 = FirstOrder(k = 1, T = 1) + @named F2 = FirstOrder(k = 1, T = 1) + @named add = Blocks.Add(k1 = 1, k2 = 2) + @named back = Feedback() + + eqs = [connect(back.output.u, F1.input.u, F2.input.u) + connect(F1.output, add.input1) + connect(F2.output, add.input2) + connect(add.output, back.input2)] + @named inner = ODESystem(eqs, t; systems = [F1, F2, add, back]) + + @named step = Step() + eqs2 = [connect(step.output, inner.back.input1) + connect(inner.back.output.u, :ap, inner.F1.input.u)] + @named sys = ODESystem(eqs2, t; systems = [inner, step]) + + prob = ODEProblem(structural_simplify(sys), [], (0.0, 10.0)) + @test SciMLBase.successful_retcode(solve(prob, Rodas5P())) + + matrices, _ = get_sensitivity(sys, sys.ap) + @test matrices == matrices_normal +end + +@testset "Analysis point overriding part of connection - mixed connect" begin + @named F1 = FirstOrder(k = 1, T = 1) + @named F2 = FirstOrder(k = 1, T = 1) + @named add = Blocks.Add(k1 = 1, k2 = 2) + @named back = Feedback() + + eqs = [connect(back.output, F1.input, F2.input) + connect(F1.output, add.input1) + connect(F2.output, add.input2) + connect(add.output, back.input2)] + @named inner = ODESystem(eqs, t; systems = [F1, F2, add, back]) + + @named step = Step() + eqs2 = [connect(step.output, inner.back.input1) + connect(inner.back.output.u, :ap, inner.F1.input.u)] + @named sys = ODESystem(eqs2, t; systems = [inner, step]) + + prob = ODEProblem(structural_simplify(sys), [], (0.0, 10.0)) + @test SciMLBase.successful_retcode(solve(prob, Rodas5P())) + + matrices, _ = get_sensitivity(sys, sys.ap) + @test matrices == matrices_normal +end From d3a8f8dce842e93c17470b5cd676b252247eba6a Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 13 Mar 2025 00:25:41 +0530 Subject: [PATCH 1105/2176] test: relax initializationsystem test --- test/initializationsystem.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/initializationsystem.jl b/test/initializationsystem.jl index 4267f1d857..93054eb5f4 100644 --- a/test/initializationsystem.jl +++ b/test/initializationsystem.jl @@ -473,7 +473,7 @@ prob = ODEProblem(pend, [x => 1], (0.0, 1.5), [g => 1], unsimp = generate_initializesystem(pend; u0map = [x => 1], initialization_eqs = [y ~ 1]) sys = structural_simplify(unsimp; fully_determined = false) -@test length(equations(sys)) == 3 +@test length(equations(sys)) in (3, 4) # could be either depending on tearing # Extend two systems with initialization equations and guesses # https://github.com/SciML/ModelingToolkit.jl/issues/2845 From 508cd8a0ecce4e476111e28b98248f89cc8c3f28 Mon Sep 17 00:00:00 2001 From: Ashutosh Bharambe Date: Thu, 13 Mar 2025 09:33:28 +0000 Subject: [PATCH 1106/2176] test: add testsets for obs func expr --- test/odesystem.jl | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/test/odesystem.jl b/test/odesystem.jl index 804aca4fd9..0beee10238 100644 --- a/test/odesystem.jl +++ b/test/odesystem.jl @@ -578,10 +578,11 @@ obsfn = ModelingToolkit.build_explicit_observed_function( outersys, bar(3outersys.sys.ms, 3outersys.sys.p)) @test_nowarn obsfn(sol.u[1], prob.p, sol.t[1]) -obsfn_expr = ModelingToolkit.build_explicit_observed_function( - outersys, bar(3outersys.sys.ms, 3outersys.sys.p), expression = true) -@test obsfn_expr isa Expr - +@testset "Observed Function: Expression" begin + obsfn_expr = ModelingToolkit.build_explicit_observed_function( + outersys, bar(3outersys.sys.ms, 3outersys.sys.p), expression = true) + @test obsfn_expr isa Expr +end # x/x @variables x(t) @named sys = ODESystem([D(x) ~ x / x], t) @@ -1230,10 +1231,12 @@ end @test_nowarn obsfn(buffer, [1.0], ps, 3.0) @test buffer ≈ [2.0, 3.0, 4.0] - obsfn_expr_oop, obsfn_expr_iip = ModelingToolkit.build_explicit_observed_function( - sys, [x + 1, x + P, x + t], return_inplace = true, expression = true) - @test obsfn_expr_oop isa Expr - @test obsfn_expr_iip isa Expr + @testset "Observed Function: Expression" begin + obsfn_expr_oop, obsfn_expr_iip = ModelingToolkit.build_explicit_observed_function( + sys, [x + 1, x + P, x + t], return_inplace = true, expression = true) + @test obsfn_expr_oop isa Expr + @test obsfn_expr_iip isa Expr + end end # https://github.com/SciML/ModelingToolkit.jl/issues/2818 From 7fb06a6babc08c5294fd6e3895c1ef1fb649e956 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 27 Feb 2025 15:36:01 +0530 Subject: [PATCH 1107/2176] feat: implement `SciMLBase.detect_cycles` --- src/systems/problem_utils.jl | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/systems/problem_utils.jl b/src/systems/problem_utils.jl index 85dfded21c..93a0749acb 100644 --- a/src/systems/problem_utils.jl +++ b/src/systems/problem_utils.jl @@ -897,6 +897,13 @@ function Base.showerror(io::IO, e::InvalidKeyError) println(io, "pmap: $(join(e.params, ", "))") end +function SciMLBase.detect_cycles(sys::AbstractSystem, varmap::Dict{Any, Any}, vars) + varmap = AnyDict(unwrap(k) => unwrap(v) for (k, v) in varmap) + vars = map(unwrap, vars) + cycles = check_substitution_cycles(varmap, vars) + return !isempty(cycles) +end + ############## # Legacy functions for backward compatibility ############## From a7a9b13df526231c214240562b71580cfde83e42 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 21 Feb 2025 12:33:38 +0530 Subject: [PATCH 1108/2176] fix: fix `DEF` parameters for `split = true, flatten = false` systems --- src/systems/abstractsystem.jl | 4 +++- test/split_parameters.jl | 37 +++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index c0b3601e21..dfc25c65ec 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -769,7 +769,9 @@ function complete(sys::AbstractSystem; split = true, flatten = true) end if split && has_index_cache(sys) @set! sys.index_cache = IndexCache(sys) - all_ps = get_ps(sys) + # Ideally we'd do `get_ps` but if `flatten = false` + # we don't get all of them. So we call `parameters`. + all_ps = parameters(sys; initial_parameters = true) if !isempty(all_ps) # reorder parameters by portions ps_split = reorder_parameters(sys, all_ps) diff --git a/test/split_parameters.jl b/test/split_parameters.jl index 0a4f9fb25d..9b00097d54 100644 --- a/test/split_parameters.jl +++ b/test/split_parameters.jl @@ -275,3 +275,40 @@ end @test_nowarn sol = solve(prob) end end + +@testset "" begin + @mtkmodel SubSystem begin + @parameters begin + c = 1 + end + @variables begin + x(t) + end + @equations begin + D(x) ~ c * x + end + end + + @mtkmodel System begin + @components begin + subsys = SubSystem() + end + @parameters begin + k = 1 + end + @variables begin + y(t) + end + @equations begin + D(y) ~ k * y + subsys.x + end + end + + @named sys = System() + sysref = complete(sys) + sys2 = complete(sys; split = true, flatten = false) + ps = Set(full_parameters(sys2)) + @test sysref.k in ps + @test sysref.subsys.c in ps + @test length(ps) == 2 +end From ac76d5a91c23211cd48aa4336f6e37af23f5f3d4 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 24 Feb 2025 12:20:13 +0530 Subject: [PATCH 1109/2176] fix: use `split = false, flatten = false` when `complete`ing parent in `structural_simplify` --- src/systems/systems.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/systems/systems.jl b/src/systems/systems.jl index 9664e38cb6..367e1cd4cd 100644 --- a/src/systems/systems.jl +++ b/src/systems/systems.jl @@ -50,7 +50,7 @@ function structural_simplify( newsys = pass(newsys) end if newsys isa ODESystem || has_parent(newsys) - @set! newsys.parent = complete(sys; split, flatten = false) + @set! newsys.parent = complete(sys; split = false, flatten = false) end newsys = complete(newsys; split) if newsys′ isa Tuple From 9d35146bf62a341a4e7585a55da5f3ac4faa722f Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 27 Feb 2025 17:43:43 +0530 Subject: [PATCH 1110/2176] test: reduce step size for CS FMUs in test --- test/fmi/fmi.jl | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/test/fmi/fmi.jl b/test/fmi/fmi.jl index 3db1c3f0e8..98c93398ff 100644 --- a/test/fmi/fmi.jl +++ b/test/fmi/fmi.jl @@ -32,7 +32,7 @@ const FMU_DIR = joinpath(@__DIR__, "fmus") @testset "v2, CS" begin fmu = loadFMU("SpringPendulum1D", "Dymola", "2022x"; type = :CS) @named inner = MTK.FMIComponent( - Val(2); fmu, communication_step_size = 0.001, type = :CS) + Val(2); fmu, communication_step_size = 1e-5, type = :CS) @variables x(t) = 1.0 @mtkbuild sys = ODESystem([D(x) ~ x], t; systems = [inner]) test_no_inputs_outputs(sys) @@ -66,7 +66,7 @@ const FMU_DIR = joinpath(@__DIR__, "fmus") @testset "v3, CS" begin fmu = loadFMU("SpringPendulum1D", "Dymola", "2023x", "3.0"; type = :CS) @named inner = MTK.FMIComponent( - Val(3); fmu, communication_step_size = 0.001, type = :CS) + Val(3); fmu, communication_step_size = 1e-5, type = :CS) @variables x(t) = 1.0 @mtkbuild sys = ODESystem([D(x) ~ x], t; systems = [inner]) test_no_inputs_outputs(sys) @@ -210,7 +210,7 @@ end @testset "v3, CS" begin fmu = loadFMU(joinpath(FMU_DIR, "StateSpace.fmu"); type = :CS) @named sspace = MTK.FMIComponent( - Val(3); fmu, communication_step_size = 1e-4, type = :CS, + Val(3); fmu, communication_step_size = 1e-6, type = :CS, reinitializealg = BrownFullBasicInit()) @test MTK.isinput(sspace.u) @test MTK.isoutput(sspace.y) @@ -259,9 +259,9 @@ end @testset "v2, CS" begin fmu = loadFMU(joinpath(FMU_DIR, "SimpleAdder.fmu"); type = :CS) @named adder1 = MTK.FMIComponent( - Val(2); fmu, type = :CS, communication_step_size = 1e-3) + Val(2); fmu, type = :CS, communication_step_size = 1e-5) @named adder2 = MTK.FMIComponent( - Val(2); fmu, type = :CS, communication_step_size = 1e-3) + Val(2); fmu, type = :CS, communication_step_size = 1e-5) sys, prob = build_looped_adders(adder1, adder2) sol = solve(prob, Tsit5(); @@ -300,9 +300,9 @@ end @testset "v3, CS" begin fmu = loadFMU(joinpath(FMU_DIR, "StateSpace.fmu"); type = :CS) @named sspace1 = MTK.FMIComponent( - Val(3); fmu, type = :CS, communication_step_size = 1e-4) + Val(3); fmu, type = :CS, communication_step_size = 1e-5) @named sspace2 = MTK.FMIComponent( - Val(3); fmu, type = :CS, communication_step_size = 1e-4) + Val(3); fmu, type = :CS, communication_step_size = 1e-5) sys, prob = build_looped_sspace(sspace1, sspace2) sol = solve(prob, Rodas5P(autodiff = false); reltol = 1e-8) @test SciMLBase.successful_retcode(sol) From cf8b3a3ea6785d769d44ca776286876e2acb2600 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 28 Feb 2025 15:52:00 +0530 Subject: [PATCH 1111/2176] fix: handle symbolic values for parameters passed to `linearize` --- src/linearization.jl | 5 ++++- test/downstream/linearize.jl | 8 ++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/linearization.jl b/src/linearization.jl index 411620e8f3..70ae5360a8 100644 --- a/src/linearization.jl +++ b/src/linearization.jl @@ -590,9 +590,12 @@ function linearize(sys, lin_fun::LinearizationFunction; t = 0.0, p = DiffEqBase.NullParameters()) prob = LinearizationProblem(lin_fun, t) op = anydict(op) - evaluate_varmap!(op, unknowns(sys)) + evaluate_varmap!(op, keys(op)) for (k, v) in op v === nothing && continue + if symbolic_type(v) != NotSymbolic() || is_array_of_symbolics(v) + v = getu(prob, v)(prob) + end if is_parameter(prob, Initial(k)) setu(prob, Initial(k))(prob, v) else diff --git a/test/downstream/linearize.jl b/test/downstream/linearize.jl index 898040fb4f..fbae0656f9 100644 --- a/test/downstream/linearize.jl +++ b/test/downstream/linearize.jl @@ -329,3 +329,11 @@ end sys, [x], []; op = Dict(x => 1.0), guesses = Dict(y => 1.0), allow_input_derivatives = true) end + +@testset "Symbolic values for parameters in `linearize`" begin + @named tank_noi = Tank_noi() + @unpack md_i, h, m, ρ, A, K = tank_noi + m_ss = 2.4000000003229878 + @test_nowarn linearize( + tank_noi, [md_i], [h]; op = Dict(m => m_ss, md_i => 2, ρ => A / K, A => 5)) +end From 0cf9aadcd7c8330bc7f429a0e09f6a1de5ee8eac Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 28 Feb 2025 16:42:23 +0530 Subject: [PATCH 1112/2176] fix: warn if the operating point given to `linearization_function` is empty. --- src/linearization.jl | 4 ++++ test/downstream/linearize.jl | 8 ++++++++ 2 files changed, 12 insertions(+) diff --git a/src/linearization.jl b/src/linearization.jl index 70ae5360a8..501582f188 100644 --- a/src/linearization.jl +++ b/src/linearization.jl @@ -42,8 +42,12 @@ function linearization_function(sys::AbstractSystem, inputs, eval_expression = false, eval_module = @__MODULE__, warn_initialize_determined = true, guesses = Dict(), + warn_empty_op = true, kwargs...) op = Dict(op) + if isempty(op) + @warn "An empty operating point was passed to `linearization_function`. An operating point containing the variables that will be changed in `linearize` should be provided. Disable this warning by passing `warn_empty_op = false`." + end inputs isa AbstractVector || (inputs = [inputs]) outputs isa AbstractVector || (outputs = [outputs]) inputs = mapreduce(vcat, inputs; init = []) do var diff --git a/test/downstream/linearize.jl b/test/downstream/linearize.jl index fbae0656f9..704964daae 100644 --- a/test/downstream/linearize.jl +++ b/test/downstream/linearize.jl @@ -337,3 +337,11 @@ end @test_nowarn linearize( tank_noi, [md_i], [h]; op = Dict(m => m_ss, md_i => 2, ρ => A / K, A => 5)) end + +@testset "Warn on empty operating point" begin + @named tank_noi = Tank_noi() + @unpack md_i, h, m = tank_noi + m_ss = 2.4000000003229878 + @test_warn ["empty operating point", "warn_empty_op"] linearize( + tank_noi, [md_i], [h]; p = [md_i => 1.0]) +end From 89dec69df33b4b404613b4226eaa8c599e9f2611 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 7 Mar 2025 14:06:31 +0530 Subject: [PATCH 1113/2176] test: fix warning in `@test_nowarn` --- test/downstream/linearize.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/downstream/linearize.jl b/test/downstream/linearize.jl index 704964daae..0336709173 100644 --- a/test/downstream/linearize.jl +++ b/test/downstream/linearize.jl @@ -262,7 +262,7 @@ closed_loop = ODESystem(connections, t, systems = [model, pid, filt, sensor, r, filt.xd => 0.0 ]) -@test_nowarn linearize(closed_loop, :r, :y) +@test_nowarn linearize(closed_loop, :r, :y; warn_empty_op = false) # https://discourse.julialang.org/t/mtk-change-in-linearize/115760/3 @mtkmodel Tank_noi begin From c354feba39892aaef06ec84c3385ded1b07b2ec7 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 13 Mar 2025 16:34:53 +0530 Subject: [PATCH 1114/2176] fix: respect `warn_empty_op` --- src/linearization.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/linearization.jl b/src/linearization.jl index 501582f188..f58f3b4253 100644 --- a/src/linearization.jl +++ b/src/linearization.jl @@ -45,7 +45,7 @@ function linearization_function(sys::AbstractSystem, inputs, warn_empty_op = true, kwargs...) op = Dict(op) - if isempty(op) + if isempty(op) && warn_empty_op @warn "An empty operating point was passed to `linearization_function`. An operating point containing the variables that will be changed in `linearize` should be provided. Disable this warning by passing `warn_empty_op = false`." end inputs isa AbstractVector || (inputs = [inputs]) From 3d5fdd83eb3b76ee14c5d5d3d0672a4d08acf406 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 13 Mar 2025 16:57:45 +0530 Subject: [PATCH 1115/2176] test: isolate tests into new testset --- test/odesystem.jl | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/test/odesystem.jl b/test/odesystem.jl index 0beee10238..970a9afaad 100644 --- a/test/odesystem.jl +++ b/test/odesystem.jl @@ -578,11 +578,6 @@ obsfn = ModelingToolkit.build_explicit_observed_function( outersys, bar(3outersys.sys.ms, 3outersys.sys.p)) @test_nowarn obsfn(sol.u[1], prob.p, sol.t[1]) -@testset "Observed Function: Expression" begin - obsfn_expr = ModelingToolkit.build_explicit_observed_function( - outersys, bar(3outersys.sys.ms, 3outersys.sys.p), expression = true) - @test obsfn_expr isa Expr -end # x/x @variables x(t) @named sys = ODESystem([D(x) ~ x / x], t) @@ -1230,13 +1225,6 @@ end buffer = zeros(3) @test_nowarn obsfn(buffer, [1.0], ps, 3.0) @test buffer ≈ [2.0, 3.0, 4.0] - - @testset "Observed Function: Expression" begin - obsfn_expr_oop, obsfn_expr_iip = ModelingToolkit.build_explicit_observed_function( - sys, [x + 1, x + P, x + t], return_inplace = true, expression = true) - @test obsfn_expr_oop isa Expr - @test obsfn_expr_iip isa Expr - end end # https://github.com/SciML/ModelingToolkit.jl/issues/2818 @@ -1736,3 +1724,15 @@ end @mtkbuild ode = ODESystem(D(x(t)) ~ mat * x(t), t; constraints = cons) @test length(constraints(ModelingToolkit.get_constraintsystem(ode))) == 5 end + +@testset "`build_explicit_observed_function` with `expression = true` returns `Expr`" begin + @variables x(t) + @mtkbuild sys = ODESystem(D(x) ~ 2x, t) + obsfn_expr = ModelingToolkit.build_explicit_observed_function( + sys, 2x + 1, expression = true) + @test obsfn_expr isa Expr + obsfn_expr_oop, obsfn_expr_iip = ModelingToolkit.build_explicit_observed_function( + sys, [x + 1, x + 2, x + t], return_inplace = true, expression = true) + @test obsfn_expr_oop isa Expr + @test obsfn_expr_iip isa Expr +end From d157ebebd80cc7e878fbb82066bef28fc1c65630 Mon Sep 17 00:00:00 2001 From: Fredrik Bagge Carlson Date: Thu, 13 Mar 2025 14:29:05 +0100 Subject: [PATCH 1116/2176] error message threw error message `sys` had gone away from the function body, so it had to be grabbed from elsewhere --- src/linearization.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/linearization.jl b/src/linearization.jl index 411620e8f3..1bd24db113 100644 --- a/src/linearization.jl +++ b/src/linearization.jl @@ -349,7 +349,7 @@ function CommonSolve.solve(prob::LinearizationProblem; allow_input_derivatives = if !iszero(Bs) if !allow_input_derivatives der_inds = findall(vec(any(!=(0), Bs, dims = 1))) - error("Input derivatives appeared in expressions (-g_z\\g_u != 0), the following inputs appeared differentiated: $(inputs(sys)[der_inds]). Call `linearize` with keyword argument `allow_input_derivatives = true` to allow this and have the returned `B` matrix be of double width ($(2nu)), where the last $nu inputs are the derivatives of the first $nu inputs.") + error("Input derivatives appeared in expressions (-g_z\\g_u != 0), the following inputs appeared differentiated: $(inputs(prob.f.prob.f.sys)[der_inds]). Call `linearize` with keyword argument `allow_input_derivatives = true` to allow this and have the returned `B` matrix be of double width ($(2nu)), where the last $nu inputs are the derivatives of the first $nu inputs.") end B = [B [zeros(nx, nu); Bs]] D = [D zeros(ny, nu)] From 186a7d3de1cc87847983096a586e3da83b8b57d5 Mon Sep 17 00:00:00 2001 From: Torkel Loman Date: Sat, 15 Mar 2025 19:38:29 +0000 Subject: [PATCH 1117/2176] add toplevel arguments --- src/systems/abstractsystem.jl | 22 ++++++++++++---------- src/systems/callbacks.jl | 32 +++++++++++++++++--------------- 2 files changed, 29 insertions(+), 25 deletions(-) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index c0b3601e21..010456a094 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -1293,10 +1293,10 @@ Get the unknown variables of the system `sys` and its subsystems. See also [`ModelingToolkit.get_unknowns`](@ref). """ -function unknowns(sys::AbstractSystem) +function unknowns(sys::AbstractSystem; toplevel = false) sts = get_unknowns(sys) systems = get_systems(sys) - nonunique_unknowns = if isempty(systems) + nonunique_unknowns = if toplevel || isempty(systems) sts else system_unknowns = reduce(vcat, namespace_variables.(systems)) @@ -1320,7 +1320,7 @@ Get the parameters of the system `sys` and its subsystems. See also [`@parameters`](@ref) and [`ModelingToolkit.get_ps`](@ref). """ -function parameters(sys::AbstractSystem; initial_parameters = false) +function parameters(sys::AbstractSystem; initial_parameters = false, toplevel = false) ps = get_ps(sys) if ps == SciMLBase.NullParameters() return [] @@ -1329,8 +1329,8 @@ function parameters(sys::AbstractSystem; initial_parameters = false) ps = first.(ps) end systems = get_systems(sys) - result = unique(isempty(systems) ? ps : - [ps; reduce(vcat, namespace_parameters.(systems))]) + result = unique(toplevel || isempty(systems) ? + ps : [ps; reduce(vcat, namespace_parameters.(systems))]) if !initial_parameters if is_time_dependent(sys) # time-dependent systems have `Initial` parameters for all their @@ -1490,8 +1490,9 @@ function controls(sys::AbstractSystem) isempty(systems) ? ctrls : [ctrls; reduce(vcat, namespace_controls.(systems))] end -function observed(sys::AbstractSystem) +function observed(sys::AbstractSystem; toplevel = false) obs = get_observed(sys) + toplevel && return obs systems = get_systems(sys) [obs; reduce(vcat, @@ -1510,7 +1511,7 @@ If they are not explicitly provided, variables and parameters are initialized to See also [`initialization_equations`](@ref), [`parameter_dependencies`](@ref) and [`ModelingToolkit.get_defaults`](@ref). """ -function defaults(sys::AbstractSystem) +function defaults(sys::AbstractSystem; toplevel = false) systems = get_systems(sys) defs = get_defaults(sys) # `mapfoldr` is really important!!! We should prefer the base model for @@ -1519,7 +1520,8 @@ function defaults(sys::AbstractSystem) # `compose(ODESystem(...; defaults=defs), ...)` # # Thus, right associativity is required and crucial for correctness. - isempty(systems) ? defs : mapfoldr(namespace_defaults, merge, systems; init = defs) + (toplevel ||isempty(systems)) ? + defs : mapfoldr(namespace_defaults, merge, systems; init = defs) end function defaults_and_guesses(sys::AbstractSystem) @@ -1549,10 +1551,10 @@ It is often the most useful way to inspect the equations of a system. See also [`full_equations`](@ref) and [`ModelingToolkit.get_eqs`](@ref). """ -function equations(sys::AbstractSystem) +function equations(sys::AbstractSystem; toplevel = false) eqs = get_eqs(sys) systems = get_systems(sys) - if isempty(systems) + if toplevel || isempty(systems) return eqs else eqs = Equation[eqs; diff --git a/src/systems/callbacks.jl b/src/systems/callbacks.jl index 5cefe65651..5b9dab6fb0 100644 --- a/src/systems/callbacks.jl +++ b/src/systems/callbacks.jl @@ -94,17 +94,17 @@ A [`ContinuousCallback`](@ref SciMLBase.ContinuousCallback) specified symbolical as well as the positive-edge `affect` and negative-edge `affect_neg` that apply when *any* of `eq` are satisfied. By default `affect_neg = affect`; to only get rising edges specify `affect_neg = nothing`. -Assume without loss of generality that the equation is of the form `c(u,p,t) ~ 0`; we denote the integrator state as `i.u`. +Assume without loss of generality that the equation is of the form `c(u,p,t) ~ 0`; we denote the integrator state as `i.u`. For compactness, we define `prev_sign = sign(c(u[t-1], p[t-1], t-1))` and `cur_sign = sign(c(u[t], p[t], t))`. -A condition edge will be detected and the callback will be invoked iff `prev_sign * cur_sign <= 0`. +A condition edge will be detected and the callback will be invoked iff `prev_sign * cur_sign <= 0`. The positive edge `affect` will be triggered iff an edge is detected and if `prev_sign < 0`; similarly, `affect_neg` will be -triggered iff an edge is detected and `prev_sign > 0`. +triggered iff an edge is detected and `prev_sign > 0`. -Inter-sample condition activation is not guaranteed; for example if we use the dirac delta function as `c` to insert a +Inter-sample condition activation is not guaranteed; for example if we use the dirac delta function as `c` to insert a sharp discontinuity between integrator steps (which in this example would not normally be identified by adaptivity) then the condition is not guaranteed to be triggered. -Once detected the integrator will "wind back" through a root-finding process to identify the point when the condition became active; the method used +Once detected the integrator will "wind back" through a root-finding process to identify the point when the condition became active; the method used is specified by `rootfind` from [`SciMLBase.RootfindOpt`](@ref). If we denote the time when the condition becomes active as `tc`, the value in the integrator after windback will be: * `u[tc-epsilon], p[tc-epsilon], tc` if `LeftRootFind` is used, @@ -116,7 +116,7 @@ it passed through 0. Multiple callbacks in the same system with different `rootfind` operations will be grouped by their `rootfind` value into separate VectorContinuousCallbacks in the enumeration order of `SciMLBase.RootfindOpt`. This may cause some callbacks to not fire if several become -active at the same instant. See the `SciMLBase` documentation for more information on the semantic rules. +active at the same instant. See the `SciMLBase` documentation for more information on the semantic rules. Affects (i.e. `affect` and `affect_neg`) can be specified as either: * A list of equations that should be applied when the callback is triggered (e.g. `x ~ 3, y ~ 7`) which must be of the form `unknown ~ observed value` where each `unknown` appears only once. Equations will be applied in the order that they appear in the vector; parameters and state updates will become immediately visible to following equations. @@ -327,14 +327,15 @@ The `SymbolicContinuousCallback`s in the returned vector are structs with two fi `affect` which correspond to the first and second elements of a `Pair` used to define an event, i.e. `eqs => affect`. """ -function continuous_events(sys::AbstractSystem) - obs = get_continuous_events(sys) - filter(!isempty, obs) +function continuous_events(sys::AbstractSystem; toplevel = false) + cbs = get_continuous_events(sys) + filter(!isempty, cbs) + toplevel && return cbs systems = get_systems(sys) - cbs = [obs; + cbs = [cbs; reduce(vcat, - (map(o -> namespace_callback(o, s), continuous_events(s)) + (map(cb -> namespace_callback(cb, s), continuous_events(s)) for s in systems), init = SymbolicContinuousCallback[])] filter(!isempty, cbs) @@ -482,10 +483,11 @@ The `SymbolicDiscreteCallback`s in the returned vector are structs with two fiel `affect` which correspond to the first and second elements of a `Pair` used to define an event, i.e. `condition => affect`. """ -function discrete_events(sys::AbstractSystem) - obs = get_discrete_events(sys) +function discrete_events(sys::AbstractSystem; toplevel = false) + cbs = get_discrete_events(sys) + toplevel && return cbs systems = get_systems(sys) - cbs = [obs; + cbs = [cbs; reduce(vcat, (map(o -> namespace_callback(o, s), discrete_events(s)) for s in systems), init = SymbolicDiscreteCallback[])] @@ -688,7 +690,7 @@ function generate_rootfinding_callback(sys::AbstractTimeDependentSystem, generate_rootfinding_callback(cbs, sys, dvs, ps; kwargs...) end """ -Generate a single rootfinding callback; this happens if there is only one equation in `cbs` passed to +Generate a single rootfinding callback; this happens if there is only one equation in `cbs` passed to generate_rootfinding_callback and thus we can produce a ContinuousCallback instead of a VectorContinuousCallback. """ function generate_single_rootfinding_callback( From 91f938a530a20c4a76dbf5cb5f5d803f696e750b Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Sun, 16 Mar 2025 01:14:42 +0530 Subject: [PATCH 1118/2176] fix: don't use `===` for comparison in variable metadata tests --- test/test_variable_metadata.jl | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/test/test_variable_metadata.jl b/test/test_variable_metadata.jl index 8fca96a23d..aaf6addb59 100644 --- a/test/test_variable_metadata.jl +++ b/test/test_variable_metadata.jl @@ -54,30 +54,30 @@ end # Guess @variables y [guess = 0] -@test getguess(y) === 0 -@test hasguess(y) === true +@test getguess(y) == 0 +@test hasguess(y) == true @test ModelingToolkit.dump_variable_metadata(y).guess == 0 # Default @variables y = 0 -@test ModelingToolkit.getdefault(y) === 0 -@test ModelingToolkit.hasdefault(y) === true +@test ModelingToolkit.getdefault(y) == 0 +@test ModelingToolkit.hasdefault(y) == true @test ModelingToolkit.dump_variable_metadata(y).default == 0 # Issue#2653 @variables y[1:3] [guess = ones(3)] @test getguess(y) == ones(3) -@test hasguess(y) === true +@test hasguess(y) == true @test ModelingToolkit.dump_variable_metadata(y).guess == ones(3) for i in 1:3 @test getguess(y[i]) == 1.0 - @test hasguess(y[i]) === true + @test hasguess(y[i]) == true @test ModelingToolkit.dump_variable_metadata(y[i]).guess == 1.0 end @variables y -@test hasguess(y) === false +@test hasguess(y) == false @test !haskey(ModelingToolkit.dump_variable_metadata(y), :guess) # Disturbance From 1d872e04a7e727cf4f2e4d6c1bc59bb5748339e8 Mon Sep 17 00:00:00 2001 From: Torkel Loman Date: Sun, 16 Mar 2025 12:11:54 +0000 Subject: [PATCH 1119/2176] update with aayush code to handle flattened systems --- src/systems/abstractsystem.jl | 29 +++++++++++++++++++++++++++++ src/systems/callbacks.jl | 12 ++++++++++-- 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index 010456a094..6f72991297 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -1292,8 +1292,12 @@ $(TYPEDSIGNATURES) Get the unknown variables of the system `sys` and its subsystems. See also [`ModelingToolkit.get_unknowns`](@ref). + +Arguments: +- `toplevel = false`: if set to true, do not return the continuous events of the subsystems. """ function unknowns(sys::AbstractSystem; toplevel = false) + toplevel && (sys = recursive_get_parent(sys)) sts = get_unknowns(sys) systems = get_systems(sys) nonunique_unknowns = if toplevel || isempty(systems) @@ -1319,8 +1323,12 @@ $(TYPEDSIGNATURES) Get the parameters of the system `sys` and its subsystems. See also [`@parameters`](@ref) and [`ModelingToolkit.get_ps`](@ref). + +Arguments: +- `toplevel = false`: if set to true, do not return the continuous events of the subsystems. """ function parameters(sys::AbstractSystem; initial_parameters = false, toplevel = false) + toplevel && (sys = recursive_get_parent(sys)) ps = get_ps(sys) if ps == SciMLBase.NullParameters() return [] @@ -1353,6 +1361,22 @@ function dependent_parameters(sys::AbstractSystem) return map(eq -> eq.lhs, parameter_dependencies(sys)) end +""" + recursive_get_parent(sys::AbstractSystem) + +Loops through parent systems to find the original parent system. + +Warning: +- Curently only used (and tested) in the context of accessor functions (e.g. `parameters`), +specifically in the context of the `toplevel` keyword argument. +""" +function recursive_get_parent(sys::AbstractSystem) + if ModelingToolkit.has_parent(sys) && (p = ModelingToolkit.get_parent(sys)) !== nothing + return recursive_get_parent(p) + end + return sys +end + """ $(TYPEDSIGNATURES) Get the parameter dependencies of the system `sys` and its subsystems. @@ -1491,6 +1515,7 @@ function controls(sys::AbstractSystem) end function observed(sys::AbstractSystem; toplevel = false) + toplevel && (sys = recursive_get_parent(sys)) obs = get_observed(sys) toplevel && return obs systems = get_systems(sys) @@ -1550,8 +1575,12 @@ It may include some abbreviations and aliases of observables. It is often the most useful way to inspect the equations of a system. See also [`full_equations`](@ref) and [`ModelingToolkit.get_eqs`](@ref). + +Arguments: +- `toplevel = false`: if set to true, do not return the continuous events of the subsystems. """ function equations(sys::AbstractSystem; toplevel = false) + toplevel && (sys = recursive_get_parent(sys)) eqs = get_eqs(sys) systems = get_systems(sys) if toplevel || isempty(systems) diff --git a/src/systems/callbacks.jl b/src/systems/callbacks.jl index 5b9dab6fb0..715b12999a 100644 --- a/src/systems/callbacks.jl +++ b/src/systems/callbacks.jl @@ -320,14 +320,18 @@ function namespace_callback(cb::SymbolicContinuousCallback, s)::SymbolicContinuo end """ - continuous_events(sys::AbstractSystem)::Vector{SymbolicContinuousCallback} + continuous_events(sys::AbstractSystem; toplevel = false)::Vector{SymbolicContinuousCallback} Returns a vector of all the `continuous_events` in an abstract system and its component subsystems. The `SymbolicContinuousCallback`s in the returned vector are structs with two fields: `eqs` and `affect` which correspond to the first and second elements of a `Pair` used to define an event, i.e. `eqs => affect`. + +Arguments: +- `toplevel = false`: if set to true, do not return the continuous events of the subsystems. """ function continuous_events(sys::AbstractSystem; toplevel = false) + toplevel && (sys = recursive_get_parent(sys)) cbs = get_continuous_events(sys) filter(!isempty, cbs) toplevel && return cbs @@ -476,14 +480,18 @@ SymbolicDiscreteCallbacks(cbs::Vector{<:SymbolicDiscreteCallback}) = cbs SymbolicDiscreteCallbacks(::Nothing) = SymbolicDiscreteCallback[] """ - discrete_events(sys::AbstractSystem) :: Vector{SymbolicDiscreteCallback} + discrete_events(sys::AbstractSystem; toplevel = false) :: Vector{SymbolicDiscreteCallback} Returns a vector of all the `discrete_events` in an abstract system and its component subsystems. The `SymbolicDiscreteCallback`s in the returned vector are structs with two fields: `condition` and `affect` which correspond to the first and second elements of a `Pair` used to define an event, i.e. `condition => affect`. + +Arguments: +- `toplevel = false`: if set to true, do not return the discrete events of the subsystems. """ function discrete_events(sys::AbstractSystem; toplevel = false) + toplevel && (sys = recursive_get_parent(sys)) cbs = get_discrete_events(sys) toplevel && return cbs systems = get_systems(sys) From 06419376dcfea0105e68bdacb5121365b7f08f8a Mon Sep 17 00:00:00 2001 From: Torkel Loman Date: Sun, 16 Mar 2025 12:11:59 +0000 Subject: [PATCH 1120/2176] add tests --- test/accessor_functions.jl | 142 +++++++++++++++++++++++++++++++++++++ test/runtests.jl | 1 + 2 files changed, 143 insertions(+) create mode 100644 test/accessor_functions.jl diff --git a/test/accessor_functions.jl b/test/accessor_functions.jl new file mode 100644 index 0000000000..78c00b00fc --- /dev/null +++ b/test/accessor_functions.jl @@ -0,0 +1,142 @@ +### Preparations ### + +# Fetch packages. +using ModelingToolkit, Test +using ModelingToolkit: t_nounits as t, D_nounits as D +import ModelingToolkit: get_ps, get_unknowns, get_observed, get_eqs, get_continuous_events, + get_discrete_events, namespace_equations + +# Creates helper functions. +function all_sets_equal(args...) + for arg in args[2:end] + issetequal(args[1], arg) || return false + end + return true +end +function sym_issubset(set1, set2) + for sym1 in set1 + any(isequal(sym1, sym2) for sym2 in set2) || return false + end + return true +end + +### Basic Tests ### + +# Checks `toplevel = false` argument for various accessors (currently only for `ODESystem`s). +# Compares to `toplevel = true` version, and `get_` functions. +# Checks accessors for parameters, unknowns, equations, observables, and events. +let + # Prepares model components. + @parameters p_top p_mid1 p_mid2 p_bot d + @variables X_top(t) X_mid1(t) X_mid2(t) X_bot(t) Y(t) O(t) + + # Creates the systems (individual and hierarchical). + eqs_top = [ + D(X_top) ~ p_top - d*X_top, + D(Y) ~ log(X_top) - Y^2 + 3.0, + O ~ (p_top + d)*X_top + Y + ] + eqs_mid1 = [ + D(X_mid1) ~ p_mid1 - d*X_mid1^2, + D(Y) ~ D(X_mid1) - Y^3, + O ~ (p_mid1 + d)*X_mid1 + Y + ] + eqs_mid2 = [ + D(X_mid2) ~ p_mid2 - d*X_mid2, + X_mid2^3 ~ log(X_mid2 + Y) - Y^2 + 3.0, + O ~ (p_mid2 + d)*X_mid2 + Y + ] + eqs_bot = [ + D(X_bot) ~ p_bot - d*X_bot, + D(Y) ~ -Y^3, + O ~ (p_bot + d)*X_bot + Y + ] + cevs = [[t ~ 1.0] => [Y ~ Y + 2.0]] + devs = [(t == 2.0) => [Y ~ Y + 2.0]] + @named sys_bot = ODESystem(eqs_bot, t; systems = [], continuous_events = cevs, discrete_events = devs) + @named sys_mid2 = ODESystem(eqs_mid2, t; systems = [], continuous_events = cevs, discrete_events = devs) + @named sys_mid1 = ODESystem(eqs_mid1, t; systems = [sys_bot], continuous_events = cevs, discrete_events = devs) + @named sys_top = ODESystem(eqs_top, t; systems = [sys_mid1, sys_mid2], continuous_events = cevs, discrete_events = devs) + sys_bot_comp = complete(sys_bot) + sys_mid2_comp = complete(sys_mid2) + sys_mid1_comp = complete(sys_mid1) + sys_top_comp = complete(sys_top) + sys_bot_ss = structural_simplify(sys_bot) + sys_mid2_ss = structural_simplify(sys_mid2) + sys_mid1_ss = structural_simplify(sys_mid1) + sys_top_ss = structural_simplify(sys_top) + + # Checks `parameters` for `toplevel = false`. + @test all_sets_equal(parameters.([sys_bot, sys_bot_comp, sys_bot_ss])..., [d, p_bot]) + @test all_sets_equal(parameters.([sys_mid1, sys_mid1_comp, sys_mid1_ss])..., [d, p_mid1, sys_bot.d, sys_bot.p_bot]) + @test all_sets_equal(parameters.([sys_mid2, sys_mid2_comp, sys_mid2_ss])..., [d, p_mid2]) + @test all_sets_equal(parameters.([sys_top, sys_top_comp, sys_top_ss])..., [d, p_top, sys_mid1.d, sys_mid1.p_mid1, sys_mid1.sys_bot.d, sys_mid1.sys_bot.p_bot, sys_mid2.d, sys_mid2.p_mid2]) + + # Checks `parameters`` for `toplevel = true`. Compares to known parameters and also checks that + # these are subset of what `get_ps` returns. + @test all_sets_equal(parameters.([sys_bot, sys_bot_comp, sys_bot_ss]; toplevel = true)..., [d, p_bot]) + @test_broken all_sets_equal(parameters.([sys_mid1, sys_mid1_comp, sys_mid1_ss]; toplevel = true)..., [d, p_mid1]) + @test all_sets_equal(parameters.([sys_mid2, sys_mid2_comp, sys_mid2_ss]; toplevel = true)..., [d, p_mid2]) + @test_broken all_sets_equal(parameters.([sys_top, sys_top_comp, sys_top_ss]; toplevel = true)..., [d, p_top]) + @test all(sym_issubset(parameters(sys; toplevel = true), get_ps(sys)) for sys in [sys_bot, sys_mid2, sys_mid1, sys_top]) + + # Checks `unknowns` for `toplevel = false`. O(t) is eliminated by `structural_simplify` and + # must be considered separately. + @test all_sets_equal(unknowns.([sys_bot, sys_bot_comp])..., [O, Y, X_bot]) + @test all_sets_equal(unknowns.([sys_bot_ss])..., [Y, X_bot]) + @test all_sets_equal(unknowns.([sys_mid1, sys_mid1_comp])..., [O, Y, X_mid1, sys_bot.Y, sys_bot.O, sys_bot.X_bot]) + @test all_sets_equal(unknowns.([sys_mid1_ss])..., [Y, X_mid1, sys_bot.Y, sys_bot.X_bot]) + @test all_sets_equal(unknowns.([sys_mid2, sys_mid2_comp])..., [O, Y, X_mid2]) + @test all_sets_equal(unknowns.([sys_mid2_ss])..., [Y, X_mid2]) + @test all_sets_equal(unknowns.([sys_top, sys_top_comp])..., [O, Y, X_top, sys_mid1.O, sys_mid1.Y, sys_mid1.X_mid1, sys_mid1.sys_bot.O, sys_mid1.sys_bot.Y, sys_mid1.sys_bot.X_bot, sys_mid2.O, sys_mid2.Y, sys_mid2.X_mid2]) + @test all_sets_equal(unknowns.([sys_top_ss])..., [Y, X_top, sys_mid1.Y, sys_mid1.X_mid1, sys_mid1.sys_bot.Y, sys_mid1.sys_bot.X_bot, sys_mid2.Y, sys_mid2.X_mid2]) + + # Checks `unknowns` for `toplevel = true`. Note that O is not eliminated here (as we go back + # to original parent system). Also checks that outputs are subsets of what `get_ps` returns.. + @test all_sets_equal(unknowns.([sys_bot, sys_bot_comp, sys_bot_ss]; toplevel = true)..., [O, Y, X_bot]) + @test all_sets_equal(unknowns.([sys_mid1, sys_mid1_comp]; toplevel = true)..., [O, Y, X_mid1]) + @test all_sets_equal(unknowns.([sys_mid2, sys_mid2_comp]; toplevel = true)..., [O, Y, X_mid2]) + @test all_sets_equal(unknowns.([sys_top, sys_top_comp]; toplevel = true)..., [O, Y, X_top]) + @test all(sym_issubset(unknowns(sys; toplevel = true), get_unknowns(sys)) for sys in [sys_bot, sys_mid1, sys_mid2, sys_top]) + + # Checks `equations` for `toplevel = false`. Do not check ss equations as these might potentially + # be structurally simplified to new equations. + @test all_sets_equal(equations.([sys_bot, sys_bot_comp])..., eqs_bot) + @test all_sets_equal(equations.([sys_mid1, sys_mid1_comp])..., [eqs_mid1; namespace_equations(sys_bot)]) + @test all_sets_equal(equations.([sys_mid2, sys_mid2_comp])..., eqs_mid2) + @test all_sets_equal(equations.([sys_top, sys_top_comp])..., [eqs_top; namespace_equations(sys_mid1); namespace_equations(sys_mid2)]) + + # Checks `equations` for `toplevel = true`. Do not check ss equations directly as these + # might potentially be structurally simplified to new equations. + @test all_sets_equal(equations.([sys_bot, sys_bot_comp]; toplevel = true)..., eqs_bot) + @test all_sets_equal(equations.([sys_mid1, sys_mid1_comp]; toplevel = true)..., eqs_mid1) + @test all_sets_equal(equations.([sys_mid2, sys_mid2_comp]; toplevel = true)..., eqs_mid2) + @test all_sets_equal(equations.([sys_top, sys_top_comp]; toplevel = true)..., eqs_top) + @test all(sym_issubset(equations(sys; toplevel = true), get_eqs(sys)) for sys in [sys_bot, sys_mid2, sys_mid1, sys_top]) + + # Checks `observed for `toplevel = false`. For non-ss systems, there are no observables. + @test all_sets_equal(observed.([sys_bot, sys_bot_comp, sys_mid1, sys_mid1_comp, sys_mid2, sys_mid2_comp, sys_top, sys_top_comp])..., []) + @test issetequal(observed(sys_bot_ss), eqs_bot[3:3]) + @test issetequal(observed(sys_mid1_ss), [eqs_mid1[3:3]; namespace_equations(sys_bot)[3:3]]) + @test issetequal(observed(sys_mid2_ss), eqs_mid2[3:3]) + @test issetequal(observed(sys_top_ss), [eqs_top[3:3]; namespace_equations(sys_mid1)[3:3:6]; namespace_equations(sys_mid2)[3:3]]) + + # Checks `observed` for `toplevel = true`. Again, for non-ss systems, there are no observables. + # Also checks that `toplevel = true` yields subset of `get_observed`. + @test all_sets_equal(observed.([sys_bot, sys_bot_comp, sys_mid1, sys_mid1_comp, sys_mid2, sys_mid2_comp, sys_top, sys_top_comp]; toplevel = true)..., []) + @test_broken issetequal(observed(sys_bot_ss; toplevel = true), eqs_bot[3:3]) + @test_broken issetequal(observed(sys_mid1_ss; toplevel = true), eqs_mid1[3:3]) + @test_broken issetequal(observed(sys_mid2_ss; toplevel = true), eqs_mid2[3:3]) + @test_broken issetequal(observed(sys_top_ss; toplevel = true), eqs_top[3:3]) + @test all(sym_issubset(observed(sys; toplevel = true), get_observed(sys)) for sys in [sys_bot, sys_mid2, sys_mid1, sys_top]) + + # Checks `continuous_events` and `discrete_events` for `toplevel = true` (more straightforward + # as I stored the same singe event in all systems). Don't check for `toplevel = false` as + # technically not needed for these tests and name spacing the events is a mess. + mtk_cev = ModelingToolkit.SymbolicContinuousCallback.(cevs)[1] + mtk_dev = ModelingToolkit.SymbolicDiscreteCallback.(devs)[1] + @test all_sets_equal(continuous_events.([sys_bot, sys_bot_comp, sys_bot_ss, sys_mid1, sys_mid1_comp, sys_mid1_ss, sys_mid2, sys_mid2_comp, sys_mid2_ss, sys_top, sys_top_comp, sys_top_ss]; toplevel = true)..., [mtk_cev]) + @test all_sets_equal(discrete_events.([sys_bot, sys_bot_comp, sys_bot_ss, sys_mid1, sys_mid1_comp, sys_mid1_ss, sys_mid2, sys_mid2_comp, sys_mid2_ss, sys_top, sys_top_comp, sys_top_ss]; toplevel = true)..., [mtk_dev]) + @test all(sym_issubset(continuous_events(sys; toplevel = true), get_continuous_events(sys)) for sys in [sys_bot, sys_mid2, sys_mid1, sys_top]) + @test all(sym_issubset(discrete_events(sys; toplevel = true), get_discrete_events(sys)) for sys in [sys_bot, sys_mid2, sys_mid1, sys_top]) +end diff --git a/test/runtests.jl b/test/runtests.jl index e0ef4b8640..4537c27255 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -61,6 +61,7 @@ end @safetestset "Constants Test" include("constants.jl") @safetestset "Parameter Dependency Test" include("parameter_dependencies.jl") @safetestset "Equation Type Accessors Test" include("equation_type_accessors.jl") + @safetestset "System Accessor Functions Test" include("accessor_functions.jl") @safetestset "Equations with complex values" include("complex.jl") end end From 78672024c585e6fd320d502023a99e93c11263c6 Mon Sep 17 00:00:00 2001 From: Torkel Loman Date: Sun, 16 Mar 2025 12:19:50 +0000 Subject: [PATCH 1121/2176] formatting --- src/systems/abstractsystem.jl | 4 +- test/accessor_functions.jl | 142 ++++++++++++++++++++++++---------- 2 files changed, 101 insertions(+), 45 deletions(-) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index 6f72991297..969f5d89ec 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -1545,8 +1545,8 @@ function defaults(sys::AbstractSystem; toplevel = false) # `compose(ODESystem(...; defaults=defs), ...)` # # Thus, right associativity is required and crucial for correctness. - (toplevel ||isempty(systems)) ? - defs : mapfoldr(namespace_defaults, merge, systems; init = defs) + (toplevel || isempty(systems)) ? + defs : mapfoldr(namespace_defaults, merge, systems; init = defs) end function defaults_and_guesses(sys::AbstractSystem) diff --git a/test/accessor_functions.jl b/test/accessor_functions.jl index 78c00b00fc..2402c53da7 100644 --- a/test/accessor_functions.jl +++ b/test/accessor_functions.jl @@ -4,7 +4,7 @@ using ModelingToolkit, Test using ModelingToolkit: t_nounits as t, D_nounits as D import ModelingToolkit: get_ps, get_unknowns, get_observed, get_eqs, get_continuous_events, - get_discrete_events, namespace_equations + get_discrete_events, namespace_equations # Creates helper functions. function all_sets_equal(args...) @@ -32,31 +32,35 @@ let # Creates the systems (individual and hierarchical). eqs_top = [ - D(X_top) ~ p_top - d*X_top, + D(X_top) ~ p_top - d * X_top, D(Y) ~ log(X_top) - Y^2 + 3.0, - O ~ (p_top + d)*X_top + Y + O ~ (p_top + d) * X_top + Y ] eqs_mid1 = [ - D(X_mid1) ~ p_mid1 - d*X_mid1^2, + D(X_mid1) ~ p_mid1 - d * X_mid1^2, D(Y) ~ D(X_mid1) - Y^3, - O ~ (p_mid1 + d)*X_mid1 + Y + O ~ (p_mid1 + d) * X_mid1 + Y ] eqs_mid2 = [ - D(X_mid2) ~ p_mid2 - d*X_mid2, + D(X_mid2) ~ p_mid2 - d * X_mid2, X_mid2^3 ~ log(X_mid2 + Y) - Y^2 + 3.0, - O ~ (p_mid2 + d)*X_mid2 + Y + O ~ (p_mid2 + d) * X_mid2 + Y ] eqs_bot = [ - D(X_bot) ~ p_bot - d*X_bot, + D(X_bot) ~ p_bot - d * X_bot, D(Y) ~ -Y^3, - O ~ (p_bot + d)*X_bot + Y + O ~ (p_bot + d) * X_bot + Y ] cevs = [[t ~ 1.0] => [Y ~ Y + 2.0]] devs = [(t == 2.0) => [Y ~ Y + 2.0]] - @named sys_bot = ODESystem(eqs_bot, t; systems = [], continuous_events = cevs, discrete_events = devs) - @named sys_mid2 = ODESystem(eqs_mid2, t; systems = [], continuous_events = cevs, discrete_events = devs) - @named sys_mid1 = ODESystem(eqs_mid1, t; systems = [sys_bot], continuous_events = cevs, discrete_events = devs) - @named sys_top = ODESystem(eqs_top, t; systems = [sys_mid1, sys_mid2], continuous_events = cevs, discrete_events = devs) + @named sys_bot = ODESystem( + eqs_bot, t; systems = [], continuous_events = cevs, discrete_events = devs) + @named sys_mid2 = ODESystem( + eqs_mid2, t; systems = [], continuous_events = cevs, discrete_events = devs) + @named sys_mid1 = ODESystem( + eqs_mid1, t; systems = [sys_bot], continuous_events = cevs, discrete_events = devs) + @named sys_top = ODESystem(eqs_top, t; systems = [sys_mid1, sys_mid2], + continuous_events = cevs, discrete_events = devs) sys_bot_comp = complete(sys_bot) sys_mid2_comp = complete(sys_mid2) sys_mid1_comp = complete(sys_mid1) @@ -68,75 +72,127 @@ let # Checks `parameters` for `toplevel = false`. @test all_sets_equal(parameters.([sys_bot, sys_bot_comp, sys_bot_ss])..., [d, p_bot]) - @test all_sets_equal(parameters.([sys_mid1, sys_mid1_comp, sys_mid1_ss])..., [d, p_mid1, sys_bot.d, sys_bot.p_bot]) - @test all_sets_equal(parameters.([sys_mid2, sys_mid2_comp, sys_mid2_ss])..., [d, p_mid2]) - @test all_sets_equal(parameters.([sys_top, sys_top_comp, sys_top_ss])..., [d, p_top, sys_mid1.d, sys_mid1.p_mid1, sys_mid1.sys_bot.d, sys_mid1.sys_bot.p_bot, sys_mid2.d, sys_mid2.p_mid2]) + @test all_sets_equal(parameters.([sys_mid1, sys_mid1_comp, sys_mid1_ss])..., + [d, p_mid1, sys_bot.d, sys_bot.p_bot]) + @test all_sets_equal( + parameters.([sys_mid2, sys_mid2_comp, sys_mid2_ss])..., [d, p_mid2]) + @test all_sets_equal(parameters.([sys_top, sys_top_comp, sys_top_ss])..., + [d, p_top, sys_mid1.d, sys_mid1.p_mid1, sys_mid1.sys_bot.d, + sys_mid1.sys_bot.p_bot, sys_mid2.d, sys_mid2.p_mid2]) # Checks `parameters`` for `toplevel = true`. Compares to known parameters and also checks that # these are subset of what `get_ps` returns. - @test all_sets_equal(parameters.([sys_bot, sys_bot_comp, sys_bot_ss]; toplevel = true)..., [d, p_bot]) - @test_broken all_sets_equal(parameters.([sys_mid1, sys_mid1_comp, sys_mid1_ss]; toplevel = true)..., [d, p_mid1]) - @test all_sets_equal(parameters.([sys_mid2, sys_mid2_comp, sys_mid2_ss]; toplevel = true)..., [d, p_mid2]) - @test_broken all_sets_equal(parameters.([sys_top, sys_top_comp, sys_top_ss]; toplevel = true)..., [d, p_top]) - @test all(sym_issubset(parameters(sys; toplevel = true), get_ps(sys)) for sys in [sys_bot, sys_mid2, sys_mid1, sys_top]) + @test all_sets_equal( + parameters.([sys_bot, sys_bot_comp, sys_bot_ss]; toplevel = true)..., [d, p_bot]) + @test_broken all_sets_equal( + parameters.([sys_mid1, sys_mid1_comp, sys_mid1_ss]; toplevel = true)..., + [d, p_mid1]) + @test all_sets_equal( + parameters.([sys_mid2, sys_mid2_comp, sys_mid2_ss]; toplevel = true)..., + [d, p_mid2]) + @test_broken all_sets_equal( + parameters.([sys_top, sys_top_comp, sys_top_ss]; toplevel = true)..., [d, p_top]) + @test all(sym_issubset(parameters(sys; toplevel = true), get_ps(sys)) + for sys in [sys_bot, sys_mid2, sys_mid1, sys_top]) # Checks `unknowns` for `toplevel = false`. O(t) is eliminated by `structural_simplify` and # must be considered separately. @test all_sets_equal(unknowns.([sys_bot, sys_bot_comp])..., [O, Y, X_bot]) @test all_sets_equal(unknowns.([sys_bot_ss])..., [Y, X_bot]) - @test all_sets_equal(unknowns.([sys_mid1, sys_mid1_comp])..., [O, Y, X_mid1, sys_bot.Y, sys_bot.O, sys_bot.X_bot]) + @test all_sets_equal(unknowns.([sys_mid1, sys_mid1_comp])..., + [O, Y, X_mid1, sys_bot.Y, sys_bot.O, sys_bot.X_bot]) @test all_sets_equal(unknowns.([sys_mid1_ss])..., [Y, X_mid1, sys_bot.Y, sys_bot.X_bot]) @test all_sets_equal(unknowns.([sys_mid2, sys_mid2_comp])..., [O, Y, X_mid2]) @test all_sets_equal(unknowns.([sys_mid2_ss])..., [Y, X_mid2]) - @test all_sets_equal(unknowns.([sys_top, sys_top_comp])..., [O, Y, X_top, sys_mid1.O, sys_mid1.Y, sys_mid1.X_mid1, sys_mid1.sys_bot.O, sys_mid1.sys_bot.Y, sys_mid1.sys_bot.X_bot, sys_mid2.O, sys_mid2.Y, sys_mid2.X_mid2]) - @test all_sets_equal(unknowns.([sys_top_ss])..., [Y, X_top, sys_mid1.Y, sys_mid1.X_mid1, sys_mid1.sys_bot.Y, sys_mid1.sys_bot.X_bot, sys_mid2.Y, sys_mid2.X_mid2]) + @test all_sets_equal(unknowns.([sys_top, sys_top_comp])..., + [O, Y, X_top, sys_mid1.O, sys_mid1.Y, sys_mid1.X_mid1, + sys_mid1.sys_bot.O, sys_mid1.sys_bot.Y, sys_mid1.sys_bot.X_bot, + sys_mid2.O, sys_mid2.Y, sys_mid2.X_mid2]) + @test all_sets_equal(unknowns.([sys_top_ss])..., + [Y, X_top, sys_mid1.Y, sys_mid1.X_mid1, sys_mid1.sys_bot.Y, + sys_mid1.sys_bot.X_bot, sys_mid2.Y, sys_mid2.X_mid2]) # Checks `unknowns` for `toplevel = true`. Note that O is not eliminated here (as we go back # to original parent system). Also checks that outputs are subsets of what `get_ps` returns.. - @test all_sets_equal(unknowns.([sys_bot, sys_bot_comp, sys_bot_ss]; toplevel = true)..., [O, Y, X_bot]) - @test all_sets_equal(unknowns.([sys_mid1, sys_mid1_comp]; toplevel = true)..., [O, Y, X_mid1]) - @test all_sets_equal(unknowns.([sys_mid2, sys_mid2_comp]; toplevel = true)..., [O, Y, X_mid2]) - @test all_sets_equal(unknowns.([sys_top, sys_top_comp]; toplevel = true)..., [O, Y, X_top]) - @test all(sym_issubset(unknowns(sys; toplevel = true), get_unknowns(sys)) for sys in [sys_bot, sys_mid1, sys_mid2, sys_top]) + @test all_sets_equal( + unknowns.([sys_bot, sys_bot_comp, sys_bot_ss]; toplevel = true)..., [O, Y, X_bot]) + @test all_sets_equal( + unknowns.([sys_mid1, sys_mid1_comp]; toplevel = true)..., [O, Y, X_mid1]) + @test all_sets_equal( + unknowns.([sys_mid2, sys_mid2_comp]; toplevel = true)..., [O, Y, X_mid2]) + @test all_sets_equal( + unknowns.([sys_top, sys_top_comp]; toplevel = true)..., [O, Y, X_top]) + @test all(sym_issubset(unknowns(sys; toplevel = true), get_unknowns(sys)) + for sys in [sys_bot, sys_mid1, sys_mid2, sys_top]) # Checks `equations` for `toplevel = false`. Do not check ss equations as these might potentially # be structurally simplified to new equations. @test all_sets_equal(equations.([sys_bot, sys_bot_comp])..., eqs_bot) - @test all_sets_equal(equations.([sys_mid1, sys_mid1_comp])..., [eqs_mid1; namespace_equations(sys_bot)]) + @test all_sets_equal( + equations.([sys_mid1, sys_mid1_comp])..., [eqs_mid1; namespace_equations(sys_bot)]) @test all_sets_equal(equations.([sys_mid2, sys_mid2_comp])..., eqs_mid2) - @test all_sets_equal(equations.([sys_top, sys_top_comp])..., [eqs_top; namespace_equations(sys_mid1); namespace_equations(sys_mid2)]) + @test all_sets_equal(equations.([sys_top, sys_top_comp])..., + [eqs_top; namespace_equations(sys_mid1); namespace_equations(sys_mid2)]) # Checks `equations` for `toplevel = true`. Do not check ss equations directly as these # might potentially be structurally simplified to new equations. @test all_sets_equal(equations.([sys_bot, sys_bot_comp]; toplevel = true)..., eqs_bot) - @test all_sets_equal(equations.([sys_mid1, sys_mid1_comp]; toplevel = true)..., eqs_mid1) - @test all_sets_equal(equations.([sys_mid2, sys_mid2_comp]; toplevel = true)..., eqs_mid2) + @test all_sets_equal( + equations.([sys_mid1, sys_mid1_comp]; toplevel = true)..., eqs_mid1) + @test all_sets_equal( + equations.([sys_mid2, sys_mid2_comp]; toplevel = true)..., eqs_mid2) @test all_sets_equal(equations.([sys_top, sys_top_comp]; toplevel = true)..., eqs_top) - @test all(sym_issubset(equations(sys; toplevel = true), get_eqs(sys)) for sys in [sys_bot, sys_mid2, sys_mid1, sys_top]) + @test all(sym_issubset(equations(sys; toplevel = true), get_eqs(sys)) + for sys in [sys_bot, sys_mid2, sys_mid1, sys_top]) # Checks `observed for `toplevel = false`. For non-ss systems, there are no observables. - @test all_sets_equal(observed.([sys_bot, sys_bot_comp, sys_mid1, sys_mid1_comp, sys_mid2, sys_mid2_comp, sys_top, sys_top_comp])..., []) + @test all_sets_equal( + observed.([sys_bot, sys_bot_comp, sys_mid1, sys_mid1_comp, + sys_mid2, sys_mid2_comp, sys_top, sys_top_comp])..., + []) @test issetequal(observed(sys_bot_ss), eqs_bot[3:3]) - @test issetequal(observed(sys_mid1_ss), [eqs_mid1[3:3]; namespace_equations(sys_bot)[3:3]]) + @test issetequal( + observed(sys_mid1_ss), [eqs_mid1[3:3]; namespace_equations(sys_bot)[3:3]]) @test issetequal(observed(sys_mid2_ss), eqs_mid2[3:3]) - @test issetequal(observed(sys_top_ss), [eqs_top[3:3]; namespace_equations(sys_mid1)[3:3:6]; namespace_equations(sys_mid2)[3:3]]) + @test issetequal(observed(sys_top_ss), + [eqs_top[3:3]; namespace_equations(sys_mid1)[3:3:6]; + namespace_equations(sys_mid2)[3:3]]) # Checks `observed` for `toplevel = true`. Again, for non-ss systems, there are no observables. # Also checks that `toplevel = true` yields subset of `get_observed`. - @test all_sets_equal(observed.([sys_bot, sys_bot_comp, sys_mid1, sys_mid1_comp, sys_mid2, sys_mid2_comp, sys_top, sys_top_comp]; toplevel = true)..., []) + @test all_sets_equal( + observed.( + [sys_bot, sys_bot_comp, sys_mid1, sys_mid1_comp, + sys_mid2, sys_mid2_comp, sys_top, sys_top_comp]; + toplevel = true)..., + []) @test_broken issetequal(observed(sys_bot_ss; toplevel = true), eqs_bot[3:3]) @test_broken issetequal(observed(sys_mid1_ss; toplevel = true), eqs_mid1[3:3]) @test_broken issetequal(observed(sys_mid2_ss; toplevel = true), eqs_mid2[3:3]) @test_broken issetequal(observed(sys_top_ss; toplevel = true), eqs_top[3:3]) - @test all(sym_issubset(observed(sys; toplevel = true), get_observed(sys)) for sys in [sys_bot, sys_mid2, sys_mid1, sys_top]) + @test all(sym_issubset(observed(sys; toplevel = true), get_observed(sys)) + for sys in [sys_bot, sys_mid2, sys_mid1, sys_top]) # Checks `continuous_events` and `discrete_events` for `toplevel = true` (more straightforward # as I stored the same singe event in all systems). Don't check for `toplevel = false` as # technically not needed for these tests and name spacing the events is a mess. mtk_cev = ModelingToolkit.SymbolicContinuousCallback.(cevs)[1] mtk_dev = ModelingToolkit.SymbolicDiscreteCallback.(devs)[1] - @test all_sets_equal(continuous_events.([sys_bot, sys_bot_comp, sys_bot_ss, sys_mid1, sys_mid1_comp, sys_mid1_ss, sys_mid2, sys_mid2_comp, sys_mid2_ss, sys_top, sys_top_comp, sys_top_ss]; toplevel = true)..., [mtk_cev]) - @test all_sets_equal(discrete_events.([sys_bot, sys_bot_comp, sys_bot_ss, sys_mid1, sys_mid1_comp, sys_mid1_ss, sys_mid2, sys_mid2_comp, sys_mid2_ss, sys_top, sys_top_comp, sys_top_ss]; toplevel = true)..., [mtk_dev]) - @test all(sym_issubset(continuous_events(sys; toplevel = true), get_continuous_events(sys)) for sys in [sys_bot, sys_mid2, sys_mid1, sys_top]) - @test all(sym_issubset(discrete_events(sys; toplevel = true), get_discrete_events(sys)) for sys in [sys_bot, sys_mid2, sys_mid1, sys_top]) + @test all_sets_equal( + continuous_events.( + [sys_bot, sys_bot_comp, sys_bot_ss, sys_mid1, sys_mid1_comp, sys_mid1_ss, + sys_mid2, sys_mid2_comp, sys_mid2_ss, sys_top, sys_top_comp, sys_top_ss]; + toplevel = true)..., + [mtk_cev]) + @test all_sets_equal( + discrete_events.( + [sys_bot, sys_bot_comp, sys_bot_ss, sys_mid1, sys_mid1_comp, sys_mid1_ss, + sys_mid2, sys_mid2_comp, sys_mid2_ss, sys_top, sys_top_comp, sys_top_ss]; + toplevel = true)..., + [mtk_dev]) + @test all(sym_issubset( + continuous_events(sys; toplevel = true), get_continuous_events(sys)) + for sys in [sys_bot, sys_mid2, sys_mid1, sys_top]) + @test all(sym_issubset(discrete_events(sys; toplevel = true), get_discrete_events(sys)) + for sys in [sys_bot, sys_mid2, sys_mid1, sys_top]) end From 3e07adf208fbeb90dfc1e73faa14f3ffb2b70e4d Mon Sep 17 00:00:00 2001 From: Torkel Loman Date: Sun, 16 Mar 2025 13:20:04 +0000 Subject: [PATCH 1122/2176] create separate _toplevel function versions isntead --- src/systems/abstractsystem.jl | 77 ++++++++++++++++++++--------------- src/systems/callbacks.jl | 48 +++++++++++++++------- 2 files changed, 79 insertions(+), 46 deletions(-) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index 969f5d89ec..d23289d964 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -1292,15 +1292,11 @@ $(TYPEDSIGNATURES) Get the unknown variables of the system `sys` and its subsystems. See also [`ModelingToolkit.get_unknowns`](@ref). - -Arguments: -- `toplevel = false`: if set to true, do not return the continuous events of the subsystems. """ -function unknowns(sys::AbstractSystem; toplevel = false) - toplevel && (sys = recursive_get_parent(sys)) +function unknowns(sys::AbstractSystem) sts = get_unknowns(sys) systems = get_systems(sys) - nonunique_unknowns = if toplevel || isempty(systems) + nonunique_unknowns = if isempty(systems) sts else system_unknowns = reduce(vcat, namespace_variables.(systems)) @@ -1317,6 +1313,19 @@ function unknowns(sys::AbstractSystem; toplevel = false) unique(nonunique_unknowns) end +""" + unknowns_toplevel(sys::AbstractSystem) + +Replicates the behaviour of `unknowns`, but ignores unknowns of subsystems. +""" +function unknowns_toplevel(sys::AbstractSystem) + if has_parent(sys) && (parent = get_parent(sys)) !== nothing + return unknowns_toplevel(parent) + end + return get_unknowns(sys) +end + + """ $(TYPEDSIGNATURES) @@ -1324,11 +1333,8 @@ Get the parameters of the system `sys` and its subsystems. See also [`@parameters`](@ref) and [`ModelingToolkit.get_ps`](@ref). -Arguments: -- `toplevel = false`: if set to true, do not return the continuous events of the subsystems. """ -function parameters(sys::AbstractSystem; initial_parameters = false, toplevel = false) - toplevel && (sys = recursive_get_parent(sys)) +function parameters(sys::AbstractSystem; initial_parameters = false) ps = get_ps(sys) if ps == SciMLBase.NullParameters() return [] @@ -1337,7 +1343,7 @@ function parameters(sys::AbstractSystem; initial_parameters = false, toplevel = ps = first.(ps) end systems = get_systems(sys) - result = unique(toplevel || isempty(systems) ? + result = unique(isempty(systems) ? ps : [ps; reduce(vcat, namespace_parameters.(systems))]) if !initial_parameters if is_time_dependent(sys) @@ -1362,19 +1368,15 @@ function dependent_parameters(sys::AbstractSystem) end """ - recursive_get_parent(sys::AbstractSystem) + parameters_toplevel(sys::AbstractSystem) -Loops through parent systems to find the original parent system. - -Warning: -- Curently only used (and tested) in the context of accessor functions (e.g. `parameters`), -specifically in the context of the `toplevel` keyword argument. +Replicates the behaviour of `parameters`, but ignores parameters of subsystems. """ -function recursive_get_parent(sys::AbstractSystem) - if ModelingToolkit.has_parent(sys) && (p = ModelingToolkit.get_parent(sys)) !== nothing - return recursive_get_parent(p) +function parameters_toplevel(sys::AbstractSystem) + if has_parent(sys) && (parent = get_parent(sys)) !== nothing + return parameters_toplevel(parent) end - return sys + return get_ps(sys) end """ @@ -1514,10 +1516,8 @@ function controls(sys::AbstractSystem) isempty(systems) ? ctrls : [ctrls; reduce(vcat, namespace_controls.(systems))] end -function observed(sys::AbstractSystem; toplevel = false) - toplevel && (sys = recursive_get_parent(sys)) +function observed(sys::AbstractSystem) obs = get_observed(sys) - toplevel && return obs systems = get_systems(sys) [obs; reduce(vcat, @@ -1536,7 +1536,7 @@ If they are not explicitly provided, variables and parameters are initialized to See also [`initialization_equations`](@ref), [`parameter_dependencies`](@ref) and [`ModelingToolkit.get_defaults`](@ref). """ -function defaults(sys::AbstractSystem; toplevel = false) +function defaults(sys::AbstractSystem) systems = get_systems(sys) defs = get_defaults(sys) # `mapfoldr` is really important!!! We should prefer the base model for @@ -1545,8 +1545,7 @@ function defaults(sys::AbstractSystem; toplevel = false) # `compose(ODESystem(...; defaults=defs), ...)` # # Thus, right associativity is required and crucial for correctness. - (toplevel || isempty(systems)) ? - defs : mapfoldr(namespace_defaults, merge, systems; init = defs) + isempty(systems) ? defs : mapfoldr(namespace_defaults, merge, systems; init = defs) end function defaults_and_guesses(sys::AbstractSystem) @@ -1576,14 +1575,11 @@ It is often the most useful way to inspect the equations of a system. See also [`full_equations`](@ref) and [`ModelingToolkit.get_eqs`](@ref). -Arguments: -- `toplevel = false`: if set to true, do not return the continuous events of the subsystems. """ -function equations(sys::AbstractSystem; toplevel = false) - toplevel && (sys = recursive_get_parent(sys)) +function equations(sys::AbstractSystem) eqs = get_eqs(sys) systems = get_systems(sys) - if toplevel || isempty(systems) + if isempty(systems) return eqs else eqs = Equation[eqs; @@ -1594,6 +1590,23 @@ function equations(sys::AbstractSystem; toplevel = false) end end + +""" + equations_toplevel(sys::AbstractSystem) + +Replicates the behaviour of `equations`, but ignores equations of subsystems. + +Notes: +- Cannot be applied to non-complete systems. +""" +function equations_toplevel(sys::AbstractSystem) + iscomplete(sys) && error("Cannot apply `equations_toplevel` to complete systems.") + if has_parent(sys) && (parent = get_parent(sys)) !== nothing + return equations_toplevel(parent) + end + return get_eqs(sys) +end + """ $(TYPEDSIGNATURES) diff --git a/src/systems/callbacks.jl b/src/systems/callbacks.jl index 715b12999a..710ccc4946 100644 --- a/src/systems/callbacks.jl +++ b/src/systems/callbacks.jl @@ -320,21 +320,16 @@ function namespace_callback(cb::SymbolicContinuousCallback, s)::SymbolicContinuo end """ - continuous_events(sys::AbstractSystem; toplevel = false)::Vector{SymbolicContinuousCallback} + continuous_events(sys::AbstractSystem)::Vector{SymbolicContinuousCallback} Returns a vector of all the `continuous_events` in an abstract system and its component subsystems. The `SymbolicContinuousCallback`s in the returned vector are structs with two fields: `eqs` and `affect` which correspond to the first and second elements of a `Pair` used to define an event, i.e. `eqs => affect`. - -Arguments: -- `toplevel = false`: if set to true, do not return the continuous events of the subsystems. """ -function continuous_events(sys::AbstractSystem; toplevel = false) - toplevel && (sys = recursive_get_parent(sys)) +function continuous_events(sys::AbstractSystem) cbs = get_continuous_events(sys) filter(!isempty, cbs) - toplevel && return cbs systems = get_systems(sys) cbs = [cbs; @@ -361,6 +356,21 @@ function vars!(vars, cb::SymbolicContinuousCallback; op = Differential) return vars end +""" + continuous_events_toplevel(sys::AbstractSystem) + +Replicates the behaviour of `continuous_events`, but ignores events of subsystems. + +Notes: +- Cannot be applied to non-complete systems. +""" +function continuous_events_toplevel(sys::AbstractSystem) + if has_parent(sys) && (parent = get_parent(sys)) !== nothing + return continuous_events_toplevel(parent) + end + return get_continuous_events(sys) +end + #################################### discrete events ##################################### struct SymbolicDiscreteCallback @@ -480,20 +490,15 @@ SymbolicDiscreteCallbacks(cbs::Vector{<:SymbolicDiscreteCallback}) = cbs SymbolicDiscreteCallbacks(::Nothing) = SymbolicDiscreteCallback[] """ - discrete_events(sys::AbstractSystem; toplevel = false) :: Vector{SymbolicDiscreteCallback} + discrete_events(sys::AbstractSystem) :: Vector{SymbolicDiscreteCallback} Returns a vector of all the `discrete_events` in an abstract system and its component subsystems. The `SymbolicDiscreteCallback`s in the returned vector are structs with two fields: `condition` and `affect` which correspond to the first and second elements of a `Pair` used to define an event, i.e. `condition => affect`. - -Arguments: -- `toplevel = false`: if set to true, do not return the discrete events of the subsystems. """ -function discrete_events(sys::AbstractSystem; toplevel = false) - toplevel && (sys = recursive_get_parent(sys)) +function discrete_events(sys::AbstractSystem) cbs = get_discrete_events(sys) - toplevel && return cbs systems = get_systems(sys) cbs = [cbs; reduce(vcat, @@ -524,6 +529,21 @@ function vars!(vars, cb::SymbolicDiscreteCallback; op = Differential) return vars end +""" + discrete_events_toplevel(sys::AbstractSystem) + +Replicates the behaviour of `discrete_events`, but ignores events of subsystems. + +Notes: +- Cannot be applied to non-complete systems. +""" +function discrete_events_toplevel(sys::AbstractSystem) + if has_parent(sys) && (parent = get_parent(sys)) !== nothing + return discrete_events_toplevel(parent) + end + return get_discrete_events(sys) +end + ################################# compilation functions #################################### # handles ensuring that affect! functions work with integrator arguments From 04d8dda22eeb39005015c2831c9acf018f7096de Mon Sep 17 00:00:00 2001 From: Torkel Loman Date: Sun, 16 Mar 2025 13:20:11 +0000 Subject: [PATCH 1123/2176] update for new tests --- test/accessor_functions.jl | 97 ++++++++++++++------------------------ 1 file changed, 35 insertions(+), 62 deletions(-) diff --git a/test/accessor_functions.jl b/test/accessor_functions.jl index 2402c53da7..1ad4ef174d 100644 --- a/test/accessor_functions.jl +++ b/test/accessor_functions.jl @@ -5,6 +5,8 @@ using ModelingToolkit, Test using ModelingToolkit: t_nounits as t, D_nounits as D import ModelingToolkit: get_ps, get_unknowns, get_observed, get_eqs, get_continuous_events, get_discrete_events, namespace_equations +import ModelingToolkit: parameters_toplevel, unknowns_toplevel, equations_toplevel, + continuous_events_toplevel, discrete_events_toplevel # Creates helper functions. function all_sets_equal(args...) @@ -23,8 +25,9 @@ end ### Basic Tests ### # Checks `toplevel = false` argument for various accessors (currently only for `ODESystem`s). -# Compares to `toplevel = true` version, and `get_` functions. +# Compares to `` version, and `get_` functions. # Checks accessors for parameters, unknowns, equations, observables, and events. +# Some tests looks funny (caused by the formatter). let # Prepares model components. @parameters p_top p_mid1 p_mid2 p_bot d @@ -70,7 +73,7 @@ let sys_mid1_ss = structural_simplify(sys_mid1) sys_top_ss = structural_simplify(sys_top) - # Checks `parameters` for `toplevel = false`. + # Checks `parameters1. @test all_sets_equal(parameters.([sys_bot, sys_bot_comp, sys_bot_ss])..., [d, p_bot]) @test all_sets_equal(parameters.([sys_mid1, sys_mid1_comp, sys_mid1_ss])..., [d, p_mid1, sys_bot.d, sys_bot.p_bot]) @@ -80,22 +83,22 @@ let [d, p_top, sys_mid1.d, sys_mid1.p_mid1, sys_mid1.sys_bot.d, sys_mid1.sys_bot.p_bot, sys_mid2.d, sys_mid2.p_mid2]) - # Checks `parameters`` for `toplevel = true`. Compares to known parameters and also checks that + # Checks `parameters_toplevel`. Compares to known parameters and also checks that # these are subset of what `get_ps` returns. @test all_sets_equal( - parameters.([sys_bot, sys_bot_comp, sys_bot_ss]; toplevel = true)..., [d, p_bot]) + parameters_toplevel.([sys_bot, sys_bot_comp, sys_bot_ss])..., [d, p_bot]) @test_broken all_sets_equal( - parameters.([sys_mid1, sys_mid1_comp, sys_mid1_ss]; toplevel = true)..., + parameters_toplevel.([sys_mid1, sys_mid1_comp, sys_mid1_ss])..., [d, p_mid1]) @test all_sets_equal( - parameters.([sys_mid2, sys_mid2_comp, sys_mid2_ss]; toplevel = true)..., + parameters_toplevel.([sys_mid2, sys_mid2_comp, sys_mid2_ss])..., [d, p_mid2]) @test_broken all_sets_equal( - parameters.([sys_top, sys_top_comp, sys_top_ss]; toplevel = true)..., [d, p_top]) - @test all(sym_issubset(parameters(sys; toplevel = true), get_ps(sys)) + parameters_toplevel.([sys_top, sys_top_comp, sys_top_ss])..., [d, p_top]) + @test all(sym_issubset(parameters_toplevel(sys), get_ps(sys)) for sys in [sys_bot, sys_mid2, sys_mid1, sys_top]) - # Checks `unknowns` for `toplevel = false`. O(t) is eliminated by `structural_simplify` and + # Checks `unknowns`. O(t) is eliminated by `structural_simplify` and # must be considered separately. @test all_sets_equal(unknowns.([sys_bot, sys_bot_comp])..., [O, Y, X_bot]) @test all_sets_equal(unknowns.([sys_bot_ss])..., [Y, X_bot]) @@ -112,20 +115,20 @@ let [Y, X_top, sys_mid1.Y, sys_mid1.X_mid1, sys_mid1.sys_bot.Y, sys_mid1.sys_bot.X_bot, sys_mid2.Y, sys_mid2.X_mid2]) - # Checks `unknowns` for `toplevel = true`. Note that O is not eliminated here (as we go back - # to original parent system). Also checks that outputs are subsets of what `get_ps` returns.. + # Checks `unknowns_toplevel`. Note that O is not eliminated here (as we go back + # to original parent system). Also checks that outputs are subsets of what `get_unknowns` returns. @test all_sets_equal( - unknowns.([sys_bot, sys_bot_comp, sys_bot_ss]; toplevel = true)..., [O, Y, X_bot]) + unknowns_toplevel.([sys_bot, sys_bot_comp, sys_bot_ss])..., [O, Y, X_bot]) @test all_sets_equal( - unknowns.([sys_mid1, sys_mid1_comp]; toplevel = true)..., [O, Y, X_mid1]) + unknowns_toplevel.([sys_mid1, sys_mid1_comp])..., [O, Y, X_mid1]) @test all_sets_equal( - unknowns.([sys_mid2, sys_mid2_comp]; toplevel = true)..., [O, Y, X_mid2]) + unknowns_toplevel.([sys_mid2, sys_mid2_comp])..., [O, Y, X_mid2]) @test all_sets_equal( - unknowns.([sys_top, sys_top_comp]; toplevel = true)..., [O, Y, X_top]) - @test all(sym_issubset(unknowns(sys; toplevel = true), get_unknowns(sys)) + unknowns_toplevel.([sys_top, sys_top_comp])..., [O, Y, X_top]) + @test all(sym_issubset(unknowns_toplevel(sys), get_unknowns(sys)) for sys in [sys_bot, sys_mid1, sys_mid2, sys_top]) - # Checks `equations` for `toplevel = false`. Do not check ss equations as these might potentially + # Checks `equations`. Do not check ss equations as these might potentially # be structurally simplified to new equations. @test all_sets_equal(equations.([sys_bot, sys_bot_comp])..., eqs_bot) @test all_sets_equal( @@ -134,65 +137,35 @@ let @test all_sets_equal(equations.([sys_top, sys_top_comp])..., [eqs_top; namespace_equations(sys_mid1); namespace_equations(sys_mid2)]) - # Checks `equations` for `toplevel = true`. Do not check ss equations directly as these - # might potentially be structurally simplified to new equations. - @test all_sets_equal(equations.([sys_bot, sys_bot_comp]; toplevel = true)..., eqs_bot) + # Checks `equations_toplevel`. Do not check ss equations directly as these + # might potentially be structurally simplified to new equations. Do not check + @test all_sets_equal(equations_toplevel.([sys_bot])..., eqs_bot) @test all_sets_equal( - equations.([sys_mid1, sys_mid1_comp]; toplevel = true)..., eqs_mid1) + equations_toplevel.([sys_mid1])..., eqs_mid1) @test all_sets_equal( - equations.([sys_mid2, sys_mid2_comp]; toplevel = true)..., eqs_mid2) - @test all_sets_equal(equations.([sys_top, sys_top_comp]; toplevel = true)..., eqs_top) - @test all(sym_issubset(equations(sys; toplevel = true), get_eqs(sys)) + equations_toplevel.([sys_mid2])..., eqs_mid2) + @test all_sets_equal(equations_toplevel.([sys_top])..., eqs_top) + @test all(sym_issubset(equations_toplevel(sys), get_eqs(sys)) for sys in [sys_bot, sys_mid2, sys_mid1, sys_top]) - # Checks `observed for `toplevel = false`. For non-ss systems, there are no observables. - @test all_sets_equal( - observed.([sys_bot, sys_bot_comp, sys_mid1, sys_mid1_comp, - sys_mid2, sys_mid2_comp, sys_top, sys_top_comp])..., - []) - @test issetequal(observed(sys_bot_ss), eqs_bot[3:3]) - @test issetequal( - observed(sys_mid1_ss), [eqs_mid1[3:3]; namespace_equations(sys_bot)[3:3]]) - @test issetequal(observed(sys_mid2_ss), eqs_mid2[3:3]) - @test issetequal(observed(sys_top_ss), - [eqs_top[3:3]; namespace_equations(sys_mid1)[3:3:6]; - namespace_equations(sys_mid2)[3:3]]) - - # Checks `observed` for `toplevel = true`. Again, for non-ss systems, there are no observables. - # Also checks that `toplevel = true` yields subset of `get_observed`. - @test all_sets_equal( - observed.( - [sys_bot, sys_bot_comp, sys_mid1, sys_mid1_comp, - sys_mid2, sys_mid2_comp, sys_top, sys_top_comp]; - toplevel = true)..., - []) - @test_broken issetequal(observed(sys_bot_ss; toplevel = true), eqs_bot[3:3]) - @test_broken issetequal(observed(sys_mid1_ss; toplevel = true), eqs_mid1[3:3]) - @test_broken issetequal(observed(sys_mid2_ss; toplevel = true), eqs_mid2[3:3]) - @test_broken issetequal(observed(sys_top_ss; toplevel = true), eqs_top[3:3]) - @test all(sym_issubset(observed(sys; toplevel = true), get_observed(sys)) - for sys in [sys_bot, sys_mid2, sys_mid1, sys_top]) - - # Checks `continuous_events` and `discrete_events` for `toplevel = true` (more straightforward - # as I stored the same singe event in all systems). Don't check for `toplevel = false` as + # Checks `continuous_events_toplevel` and `discrete_events_toplevel` (straightforward + # as I stored the same singe event in all systems). Don't check for non-toplevel cases as # technically not needed for these tests and name spacing the events is a mess. mtk_cev = ModelingToolkit.SymbolicContinuousCallback.(cevs)[1] mtk_dev = ModelingToolkit.SymbolicDiscreteCallback.(devs)[1] @test all_sets_equal( - continuous_events.( + continuous_events_toplevel.( [sys_bot, sys_bot_comp, sys_bot_ss, sys_mid1, sys_mid1_comp, sys_mid1_ss, - sys_mid2, sys_mid2_comp, sys_mid2_ss, sys_top, sys_top_comp, sys_top_ss]; - toplevel = true)..., + sys_mid2, sys_mid2_comp, sys_mid2_ss, sys_top, sys_top_comp, sys_top_ss])..., [mtk_cev]) @test all_sets_equal( - discrete_events.( + discrete_events_toplevel.( [sys_bot, sys_bot_comp, sys_bot_ss, sys_mid1, sys_mid1_comp, sys_mid1_ss, - sys_mid2, sys_mid2_comp, sys_mid2_ss, sys_top, sys_top_comp, sys_top_ss]; - toplevel = true)..., + sys_mid2, sys_mid2_comp, sys_mid2_ss, sys_top, sys_top_comp, sys_top_ss])..., [mtk_dev]) @test all(sym_issubset( - continuous_events(sys; toplevel = true), get_continuous_events(sys)) + continuous_events_toplevel(sys), get_continuous_events(sys)) for sys in [sys_bot, sys_mid2, sys_mid1, sys_top]) - @test all(sym_issubset(discrete_events(sys; toplevel = true), get_discrete_events(sys)) + @test all(sym_issubset(discrete_events_toplevel(sys), get_discrete_events(sys)) for sys in [sys_bot, sys_mid2, sys_mid1, sys_top]) end From 9e459e20266712154d8ac17818ab211abe8c1b4a Mon Sep 17 00:00:00 2001 From: Torkel Loman Date: Sun, 16 Mar 2025 13:20:31 +0000 Subject: [PATCH 1124/2176] formating --- src/systems/abstractsystem.jl | 2 -- test/accessor_functions.jl | 6 +++--- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index d23289d964..de69338ec4 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -1325,7 +1325,6 @@ function unknowns_toplevel(sys::AbstractSystem) return get_unknowns(sys) end - """ $(TYPEDSIGNATURES) @@ -1590,7 +1589,6 @@ function equations(sys::AbstractSystem) end end - """ equations_toplevel(sys::AbstractSystem) diff --git a/test/accessor_functions.jl b/test/accessor_functions.jl index 1ad4ef174d..d930d202e5 100644 --- a/test/accessor_functions.jl +++ b/test/accessor_functions.jl @@ -156,15 +156,15 @@ let @test all_sets_equal( continuous_events_toplevel.( [sys_bot, sys_bot_comp, sys_bot_ss, sys_mid1, sys_mid1_comp, sys_mid1_ss, - sys_mid2, sys_mid2_comp, sys_mid2_ss, sys_top, sys_top_comp, sys_top_ss])..., + sys_mid2, sys_mid2_comp, sys_mid2_ss, sys_top, sys_top_comp, sys_top_ss])..., [mtk_cev]) @test all_sets_equal( discrete_events_toplevel.( [sys_bot, sys_bot_comp, sys_bot_ss, sys_mid1, sys_mid1_comp, sys_mid1_ss, - sys_mid2, sys_mid2_comp, sys_mid2_ss, sys_top, sys_top_comp, sys_top_ss])..., + sys_mid2, sys_mid2_comp, sys_mid2_ss, sys_top, sys_top_comp, sys_top_ss])..., [mtk_dev]) @test all(sym_issubset( - continuous_events_toplevel(sys), get_continuous_events(sys)) + continuous_events_toplevel(sys), get_continuous_events(sys)) for sys in [sys_bot, sys_mid2, sys_mid1, sys_top]) @test all(sym_issubset(discrete_events_toplevel(sys), get_discrete_events(sys)) for sys in [sys_bot, sys_mid2, sys_mid1, sys_top]) From e151999d5aeccf70fb1415df71f24b799c760ade Mon Sep 17 00:00:00 2001 From: Torkel Loman Date: Sun, 16 Mar 2025 13:29:11 +0000 Subject: [PATCH 1125/2176] add docs --- docs/src/basics/AbstractSystem.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/docs/src/basics/AbstractSystem.md b/docs/src/basics/AbstractSystem.md index e68a7bb94e..e1ff695245 100644 --- a/docs/src/basics/AbstractSystem.md +++ b/docs/src/basics/AbstractSystem.md @@ -69,6 +69,8 @@ Optionally, a system could have: Note that if you know a system is an `AbstractTimeDependentSystem` you could use `get_iv` to get the unique independent variable directly, rather than using `independent_variables(sys)[1]`, which is clunky and may cause problems if `sys` is an `AbstractMultivariateSystem` because there may be more than one independent variable. `AbstractTimeIndependentSystem`s do not have a method `get_iv`, and `independent_variables(sys)` will return a size-zero result for such. For an `AbstractMultivariateSystem`, `get_ivs` is equivalent. +For the `parameters`, `unknowns`, `continuous_events`, and `discrete_events` accessors there are corresponding `parameters_toplevel`, `unknowns_toplevel`, `continuous_events_toplevel`, and `discrete_events_toplevel` accessors which works similarly, but ignores the content of subsystems. Furthermore, a `equations_toplevel` version of `equations` exists as well, however, it can only be applied to non-complete systems. + A system could also have caches: - `get_jac(sys)`: The Jacobian of a system. From be8d20d8a96c53eb8271dc3fba96401e34dc2284 Mon Sep 17 00:00:00 2001 From: Torkel Loman Date: Sun, 16 Mar 2025 13:31:12 +0000 Subject: [PATCH 1126/2176] format fixes --- src/systems/abstractsystem.jl | 5 ++--- src/systems/callbacks.jl | 2 +- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index de69338ec4..9f0a2b61d4 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -1342,8 +1342,8 @@ function parameters(sys::AbstractSystem; initial_parameters = false) ps = first.(ps) end systems = get_systems(sys) - result = unique(isempty(systems) ? - ps : [ps; reduce(vcat, namespace_parameters.(systems))]) + result = unique(isempty(systems) ? ps : + [ps; reduce(vcat, namespace_parameters.(systems))]) if !initial_parameters if is_time_dependent(sys) # time-dependent systems have `Initial` parameters for all their @@ -1573,7 +1573,6 @@ It may include some abbreviations and aliases of observables. It is often the most useful way to inspect the equations of a system. See also [`full_equations`](@ref) and [`ModelingToolkit.get_eqs`](@ref). - """ function equations(sys::AbstractSystem) eqs = get_eqs(sys) diff --git a/src/systems/callbacks.jl b/src/systems/callbacks.jl index 710ccc4946..937264d083 100644 --- a/src/systems/callbacks.jl +++ b/src/systems/callbacks.jl @@ -502,7 +502,7 @@ function discrete_events(sys::AbstractSystem) systems = get_systems(sys) cbs = [cbs; reduce(vcat, - (map(o -> namespace_callback(o, s), discrete_events(s)) for s in systems), + (map(cb -> namespace_callback(cb, s), discrete_events(s)) for s in systems), init = SymbolicDiscreteCallback[])] cbs end From 3054252a929feaddac923838f397786083a74751 Mon Sep 17 00:00:00 2001 From: Christopher Rackauckas Date: Mon, 17 Mar 2025 06:43:18 -0100 Subject: [PATCH 1127/2176] Update .typos.toml --- .typos.toml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.typos.toml b/.typos.toml index b8c07088b4..951470242d 100644 --- a/.typos.toml +++ b/.typos.toml @@ -4,4 +4,5 @@ nd = "nd" Strat = "Strat" eles = "eles" ser = "ser" -isconnection = "isconnection" \ No newline at end of file +isconnection = "isconnection" +Ue = "Ue" From d90d02bdc940a0a435a82c09d6fdb1762b4f189a Mon Sep 17 00:00:00 2001 From: Christopher Rackauckas Date: Mon, 17 Mar 2025 06:45:08 -0100 Subject: [PATCH 1128/2176] Update analysis_points.jl --- src/systems/analysis_points.jl | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/systems/analysis_points.jl b/src/systems/analysis_points.jl index 70c41b50c9..16dee02934 100644 --- a/src/systems/analysis_points.jl +++ b/src/systems/analysis_points.jl @@ -533,7 +533,7 @@ function apply_transformation(tf::GetInput, sys::AbstractSystem) ap_idx = analysis_point_index(ap_sys, tf.ap) ap_idx === nothing && error("Analysis point $(nameof(tf.ap)) not found in system $(nameof(sys)).") - # get the anlysis point + # get the analysis point ap_sys_eqs = get_eqs(ap_sys) ap = ap_sys_eqs[ap_idx].rhs @@ -587,7 +587,7 @@ function apply_transformation(tf::PerturbOutput, sys::AbstractSystem) ap_idx = analysis_point_index(ap_sys, tf.ap) ap_idx === nothing && error("Analysis point $(nameof(tf.ap)) not found in system $(nameof(sys)).") - # modified quations + # modified equations ap_sys_eqs = copy(get_eqs(ap_sys)) @set! ap_sys.eqs = ap_sys_eqs ap = ap_sys_eqs[ap_idx].rhs @@ -899,7 +899,7 @@ result of `apply_transformation`. # Keyword Arguments - `system_modifier`: a function which takes the modified system and returns a new system - with any required further modifications peformed. + with any required further modifications performed. """ function open_loop(sys, ap::Union{Symbol, AnalysisPoint}; system_modifier = identity) ap = only(canonicalize_ap(sys, ap)) From 03f765744ba2413d62fb81ba4d4b22cd140b5359 Mon Sep 17 00:00:00 2001 From: Christopher Rackauckas Date: Mon, 17 Mar 2025 06:45:35 -0100 Subject: [PATCH 1129/2176] Update abstractsystem.jl --- src/systems/abstractsystem.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index dfc25c65ec..90d461aa87 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -622,7 +622,7 @@ end """ Initial(x) -The `Initial` operator. Used by initializaton to store constant constraints on variables +The `Initial` operator. Used by initialization to store constant constraints on variables of a system. See the documentation section on initialization for more information. """ struct Initial <: Symbolics.Operator end From 9bac25fa252d00b6910045a2f2e592493bc489e9 Mon Sep 17 00:00:00 2001 From: Christopher Rackauckas Date: Mon, 17 Mar 2025 06:46:10 -0100 Subject: [PATCH 1130/2176] Update if_lifting.jl --- src/systems/if_lifting.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/systems/if_lifting.jl b/src/systems/if_lifting.jl index 53e1ca4957..da069cc76e 100644 --- a/src/systems/if_lifting.jl +++ b/src/systems/if_lifting.jl @@ -85,7 +85,7 @@ function (cw::CondRewriter)(expr, dep) return (expr, true, true) # other singleton symbolic variables elseif !iscall(expr) - @warn "Automatic conversion of if statments to events requires use of a limited conditional grammar; see the documentation. Skipping due to $expr" + @warn "Automatic conversion of if statements to events requires use of a limited conditional grammar; see the documentation. Skipping due to $expr" return (expr, true, true) # error case => conservative assumption is that both true and false have to be evaluated elseif operation(expr) == Base.:(|) # OR of two conditions a, b = arguments(expr) @@ -405,7 +405,7 @@ const CONDITION_SIMPLIFIER = Rewriters.Fixpoint(Rewriters.Postwalk(Rewriters.Cha (@rule ifelse(false, (~x), (~y)) => (~y))]))) """ -If lifting converts (nested) if statements into a series of continous events + a logically equivalent if statement + parameters. +If lifting converts (nested) if statements into a series of continuous events + a logically equivalent if statement + parameters. Lifting proceeds through the following process: * rewrite comparisons to be of the form eqn [op] 0; subtract the RHS from the LHS From 14f173664652fd8101b9e68b2563c3ac246d8167 Mon Sep 17 00:00:00 2001 From: Christopher Rackauckas Date: Mon, 17 Mar 2025 06:46:45 -0100 Subject: [PATCH 1131/2176] Update .typos.toml --- .typos.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/.typos.toml b/.typos.toml index 951470242d..a933260fb5 100644 --- a/.typos.toml +++ b/.typos.toml @@ -6,3 +6,4 @@ eles = "eles" ser = "ser" isconnection = "isconnection" Ue = "Ue" +Derivate = "Derivate" From d0f1dc0e2c4e99585fb39db8f23f1414c304d45f Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 17 Mar 2025 15:07:34 +0530 Subject: [PATCH 1132/2176] build: bump minor version --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index ad5b7f9d28..2b204c5ed7 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "ModelingToolkit" uuid = "961ee093-0014-501f-94e3-6117800e7a78" authors = ["Yingbo Ma ", "Chris Rackauckas and contributors"] -version = "9.65.0" +version = "9.66.0" [deps] AbstractTrees = "1520ce14-60c1-5f80-bbc7-55ef81b5835c" From 56ec62202fa7f4f6d4250d140ae8fedb5a22a7c9 Mon Sep 17 00:00:00 2001 From: Torkel Loman Date: Mon, 17 Mar 2025 10:42:35 +0000 Subject: [PATCH 1133/2176] Update src/systems/abstractsystem.jl Co-authored-by: Aayush Sabharwal --- src/systems/abstractsystem.jl | 1 - 1 file changed, 1 deletion(-) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index 9f0a2b61d4..9e6e875283 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -1331,7 +1331,6 @@ $(TYPEDSIGNATURES) Get the parameters of the system `sys` and its subsystems. See also [`@parameters`](@ref) and [`ModelingToolkit.get_ps`](@ref). - """ function parameters(sys::AbstractSystem; initial_parameters = false) ps = get_ps(sys) From 4536f9800d2ac6ffe4ecd8fa917701b209e0f002 Mon Sep 17 00:00:00 2001 From: Torkel Loman Date: Mon, 17 Mar 2025 10:43:06 +0000 Subject: [PATCH 1134/2176] Update docs/src/basics/AbstractSystem.md Co-authored-by: Aayush Sabharwal --- docs/src/basics/AbstractSystem.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/src/basics/AbstractSystem.md b/docs/src/basics/AbstractSystem.md index e1ff695245..3aa43e6d25 100644 --- a/docs/src/basics/AbstractSystem.md +++ b/docs/src/basics/AbstractSystem.md @@ -69,7 +69,7 @@ Optionally, a system could have: Note that if you know a system is an `AbstractTimeDependentSystem` you could use `get_iv` to get the unique independent variable directly, rather than using `independent_variables(sys)[1]`, which is clunky and may cause problems if `sys` is an `AbstractMultivariateSystem` because there may be more than one independent variable. `AbstractTimeIndependentSystem`s do not have a method `get_iv`, and `independent_variables(sys)` will return a size-zero result for such. For an `AbstractMultivariateSystem`, `get_ivs` is equivalent. -For the `parameters`, `unknowns`, `continuous_events`, and `discrete_events` accessors there are corresponding `parameters_toplevel`, `unknowns_toplevel`, `continuous_events_toplevel`, and `discrete_events_toplevel` accessors which works similarly, but ignores the content of subsystems. Furthermore, a `equations_toplevel` version of `equations` exists as well, however, it can only be applied to non-complete systems. +For the `parameters`, `unknowns`, `continuous_events`, and `discrete_events` accessors there are corresponding `parameters_toplevel`, `unknowns_toplevel`, `continuous_events_toplevel`, and `discrete_events_toplevel` accessors which work similarly, but ignore the content of subsystems. Furthermore, a `equations_toplevel` version of `equations` exists as well, however, it can only be applied to non-complete systems. A system could also have caches: From 9a19c1d4c2b0fe75c6537936455831ba46ceef24 Mon Sep 17 00:00:00 2001 From: Torkel Loman Date: Mon, 17 Mar 2025 12:45:40 +0000 Subject: [PATCH 1135/2176] broken tests no longer broken --- test/accessor_functions.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/accessor_functions.jl b/test/accessor_functions.jl index d930d202e5..7ce477155b 100644 --- a/test/accessor_functions.jl +++ b/test/accessor_functions.jl @@ -87,13 +87,13 @@ let # these are subset of what `get_ps` returns. @test all_sets_equal( parameters_toplevel.([sys_bot, sys_bot_comp, sys_bot_ss])..., [d, p_bot]) - @test_broken all_sets_equal( + @test all_sets_equal( parameters_toplevel.([sys_mid1, sys_mid1_comp, sys_mid1_ss])..., [d, p_mid1]) @test all_sets_equal( parameters_toplevel.([sys_mid2, sys_mid2_comp, sys_mid2_ss])..., [d, p_mid2]) - @test_broken all_sets_equal( + @test all_sets_equal( parameters_toplevel.([sys_top, sys_top_comp, sys_top_ss])..., [d, p_top]) @test all(sym_issubset(parameters_toplevel(sys), get_ps(sys)) for sys in [sys_bot, sys_mid2, sys_mid1, sys_top]) From 14e810de1a18b35c515873c4af19bd10e708c730 Mon Sep 17 00:00:00 2001 From: vyudu Date: Wed, 12 Mar 2025 16:50:03 -0400 Subject: [PATCH 1136/2176] fix: add better warning for missing guess --- src/systems/diffeqs/abstractodesystem.jl | 2 +- src/systems/problem_utils.jl | 31 ++++++++++++++++++++---- src/variables.jl | 2 +- 3 files changed, 28 insertions(+), 7 deletions(-) diff --git a/src/systems/diffeqs/abstractodesystem.jl b/src/systems/diffeqs/abstractodesystem.jl index 01f847d5d6..f686ebcede 100644 --- a/src/systems/diffeqs/abstractodesystem.jl +++ b/src/systems/diffeqs/abstractodesystem.jl @@ -1504,5 +1504,5 @@ function InitializationProblem{iip, specialize}(sys::AbstractSystem, else NonlinearLeastSquaresProblem end - TProb(isys, u0map, parammap; kwargs..., build_initializeprob = false) + 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 93a0749acb..9aaf9eedab 100644 --- a/src/systems/problem_utils.jl +++ b/src/systems/problem_utils.jl @@ -325,6 +325,22 @@ function Base.showerror(io::IO, err::UnexpectedSymbolicValueInVarmap) """) end +struct MissingGuessError <: Exception + sym::Any + val::Any +end + +function Base.showerror(io::IO, err::MissingGuessError) + println(io, + """ + The problem cannot be initialized without providing an additional numeric \ + guess to serve as a starting point for solving for the initial state. Please \ + provide another numeric value to `guesses` in the problem constructor. + + This error was thrown because symbolic value $(err.val) was found for variable $(err.sym). + """) +end + """ $(TYPEDSIGNATURES) @@ -342,10 +358,11 @@ Keyword arguments: [`missingvars`](@ref) to perform the check. - `allow_symbolic` allows the returned array to contain symbolic values. If this is `true`, `promotetoconcrete` is set to `false`. +- `is_initializeprob`: Whether the parent problem is an initialization problem. """ function better_varmap_to_vars(varmap::AbstractDict, vars::Vector; tofloat = true, use_union = true, container_type = Array, - toterm = default_toterm, promotetoconcrete = nothing, check = true, allow_symbolic = false) + toterm = default_toterm, promotetoconcrete = nothing, check = true, allow_symbolic = false, is_initializeprob = false) isempty(vars) && return nothing if check @@ -356,7 +373,11 @@ function better_varmap_to_vars(varmap::AbstractDict, vars::Vector; if !allow_symbolic for (sym, val) in zip(vars, vals) symbolic_type(val) == NotSymbolic() && continue - throw(UnexpectedSymbolicValueInVarmap(sym, val)) + if is_initializeprob + throw(MissingGuessError(sym, val)) + else + throw(UnexpectedSymbolicValueInVarmap(sym, val)) + end end end @@ -704,7 +725,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. -- `tofloat`, `use_union`: Passed to [`better_varmap_to_vars`](@ref) for building `u0` (and +- `tofloat`, `use_union`, `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. @@ -742,7 +763,7 @@ function process_SciMLProblem( circular_dependency_max_cycles = 10, substitution_limit = 100, use_scc = true, force_initialization_time_independent = false, algebraic_only = false, - allow_incomplete = false, kwargs...) + allow_incomplete = false, is_initializeprob = false, kwargs...) dvs = unknowns(sys) ps = parameters(sys; initial_parameters = true) iv = has_iv(sys) ? get_iv(sys) : nothing @@ -815,7 +836,7 @@ function process_SciMLProblem( u0 = better_varmap_to_vars( op, dvs; tofloat = true, use_union = false, - container_type = u0Type, allow_symbolic = symbolic_u0) + container_type = u0Type, allow_symbolic = symbolic_u0, is_initializeprob) if u0 !== nothing u0 = u0_constructor(u0) diff --git a/src/variables.jl b/src/variables.jl index ddc33fa838..3f070fea3c 100644 --- a/src/variables.jl +++ b/src/variables.jl @@ -219,7 +219,7 @@ end function Base.showerror(io::IO, e::MissingVariablesError) println(io, MISSING_VARIABLES_MESSAGE) - println(io, e.vars) + println(io, join(e.vars, ", ")) end function _varmap_to_vars(varmap::Dict, varlist; defaults = Dict(), check = false, From 59d277bded4f93f0448c765e97a32e7f4d6d7cd4 Mon Sep 17 00:00:00 2001 From: vyudu Date: Wed, 12 Mar 2025 18:55:04 -0400 Subject: [PATCH 1137/2176] throw error only in case of initializationproblem --- src/systems/problem_utils.jl | 12 ++++++++---- test/initial_values.jl | 14 +++++++++++++- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/src/systems/problem_utils.jl b/src/systems/problem_utils.jl index 9aaf9eedab..2cae9deb3b 100644 --- a/src/systems/problem_utils.jl +++ b/src/systems/problem_utils.jl @@ -333,9 +333,13 @@ end function Base.showerror(io::IO, err::MissingGuessError) println(io, """ - The problem cannot be initialized without providing an additional numeric \ - guess to serve as a starting point for solving for the initial state. Please \ - provide another numeric value to `guesses` in the problem constructor. + Unable to resolve numeric guesses for all of the variables in the system. \ + This may be because your guesses are cyclic. + + In order for the problem to be initialized, all of the variables must have \ + a numeric value to serve as a starting point for the nonlinear solve. \ + Please provide one or more additional numeric guesses to `guesses` in \ + the problem constructor. This error was thrown because symbolic value $(err.val) was found for variable $(err.sym). """) @@ -358,7 +362,7 @@ Keyword arguments: [`missingvars`](@ref) to perform the check. - `allow_symbolic` allows the returned array to contain symbolic values. If this is `true`, `promotetoconcrete` is set to `false`. -- `is_initializeprob`: Whether the parent problem is an initialization problem. +- `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, diff --git a/test/initial_values.jl b/test/initial_values.jl index 2db552c3ba..67e9da1ffb 100644 --- a/test/initial_values.jl +++ b/test/initial_values.jl @@ -172,7 +172,7 @@ end [p => 2q, q => 3p]; warn_cyclic_dependency = true) catch end - @test_throws ModelingToolkit.UnexpectedSymbolicValueInVarmap ODEProblem( + @test_throws ModelingToolkit.MissingGuessError ODEProblem( sys, [x => 1, y => 2], (0.0, 1.0), [p => 2q, q => 3p]) end @@ -199,3 +199,15 @@ end @test SciMLBase.successful_retcode(sol) @test sol.u[1] ≈ [1.0, 1.0, 0.5, 0.5] end + +@testset "Missing/cyclic guesses throws error" begin + @parameters g + @variables x(t) y(t) [state_priority = 10] λ(t) + eqs = [D(D(x)) ~ λ * x + D(D(y)) ~ λ * y - g + 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]) + ODEProblem(pend, [x => 1], (0, 1), [g => 1], guesses = [y => λ, λ => 0.5]) +end From 6ddc1a7489fccc70a93ab4442751de699e83ae97 Mon Sep 17 00:00:00 2001 From: vyudu Date: Thu, 13 Mar 2025 21:36:40 -0400 Subject: [PATCH 1138/2176] adjust error message --- 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 2cae9deb3b..1a4ea523b8 100644 --- a/src/systems/problem_utils.jl +++ b/src/systems/problem_utils.jl @@ -338,7 +338,7 @@ function Base.showerror(io::IO, err::MissingGuessError) In order for the problem to be initialized, all of the variables must have \ a numeric value to serve as a starting point for the nonlinear solve. \ - Please provide one or more additional numeric guesses to `guesses` in \ + Please provide an additional numeric guess to `guesses` in \ the problem constructor. This error was thrown because symbolic value $(err.val) was found for variable $(err.sym). From f8846518a5a3c6ccf4c43364fbc82ab41e8727fc Mon Sep 17 00:00:00 2001 From: vyudu Date: Mon, 17 Mar 2025 11:36:22 -0400 Subject: [PATCH 1139/2176] print all missing values --- src/systems/problem_utils.jl | 33 +++++++++++++++++++-------------- test/initial_values.jl | 6 ++++++ 2 files changed, 25 insertions(+), 14 deletions(-) diff --git a/src/systems/problem_utils.jl b/src/systems/problem_utils.jl index 1a4ea523b8..877b16bb69 100644 --- a/src/systems/problem_utils.jl +++ b/src/systems/problem_utils.jl @@ -326,23 +326,24 @@ function Base.showerror(io::IO, err::UnexpectedSymbolicValueInVarmap) end struct MissingGuessError <: Exception - sym::Any - val::Any + syms::Vector{Any} + vals::Vector{Any} end function Base.showerror(io::IO, err::MissingGuessError) println(io, """ Unable to resolve numeric guesses for all of the variables in the system. \ - This may be because your guesses are cyclic. + This may be because your guesses are cyclic. In order for the problem to be \ + initialized, all of the variables must have a numeric value to serve as a \ + starting point for the nonlinear solve. - In order for the problem to be initialized, all of the variables must have \ - a numeric value to serve as a starting point for the nonlinear solve. \ - Please provide an additional numeric guess to `guesses` in \ - the problem constructor. - - This error was thrown because symbolic value $(err.val) was found for variable $(err.sym). + Symbolic values were found for the following variables/parameters in the map; \ + please provide additional numeric guesses so they can resolve to numbers: """) + for (sym, val) in zip(err.syms, err.vals) + println(sym, " => ", val) + end end """ @@ -375,13 +376,17 @@ function better_varmap_to_vars(varmap::AbstractDict, vars::Vector; end vals = map(x -> varmap[x], vars) if !allow_symbolic + missingsyms = Any[] + missingvals = Any[] for (sym, val) in zip(vars, vals) symbolic_type(val) == NotSymbolic() && continue - if is_initializeprob - throw(MissingGuessError(sym, val)) - else - throw(UnexpectedSymbolicValueInVarmap(sym, val)) - end + push!(missingsyms, sym) + push!(missingvals, val) + end + + if !isempty(missingsyms) + is_initializeprob ? throw(MissingGuessError(missingsyms, missingvals)) : + throw(UnexpectedSymbolicValueInVarmap(missingsyms[1], missingvals[1])) end end diff --git a/test/initial_values.jl b/test/initial_values.jl index 67e9da1ffb..7ad3dc4497 100644 --- a/test/initial_values.jl +++ b/test/initial_values.jl @@ -210,4 +210,10 @@ end @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)) end From d680da20c30c97090b6c8eefb34c2abe2140eb35 Mon Sep 17 00:00:00 2001 From: vyudu Date: Mon, 17 Mar 2025 12:25:51 -0400 Subject: [PATCH 1140/2176] refine message, fix test --- src/systems/problem_utils.jl | 13 +++++-------- test/initial_values.jl | 2 +- 2 files changed, 6 insertions(+), 9 deletions(-) diff --git a/src/systems/problem_utils.jl b/src/systems/problem_utils.jl index 877b16bb69..e9b836ddf7 100644 --- a/src/systems/problem_utils.jl +++ b/src/systems/problem_utils.jl @@ -333,17 +333,14 @@ end function Base.showerror(io::IO, err::MissingGuessError) println(io, """ - Unable to resolve numeric guesses for all of the variables in the system. \ - This may be because your guesses are cyclic. In order for the problem to be \ - initialized, all of the variables must have a numeric value to serve as a \ - starting point for the nonlinear solve. - - Symbolic values were found for the following variables/parameters in the map; \ - please provide additional numeric guesses so they can resolve to numbers: + 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(sym, " => ", val) + 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. """) end """ diff --git a/test/initial_values.jl b/test/initial_values.jl index 7ad3dc4497..66230557ab 100644 --- a/test/initial_values.jl +++ b/test/initial_values.jl @@ -215,5 +215,5 @@ end @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 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 1141/2176] 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 1142/2176] 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 1143/2176] 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 1144/2176] 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 1145/2176] 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 1146/2176] 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 1147/2176] 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 1148/2176] 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 1149/2176] 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 1150/2176] 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 1151/2176] 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 1152/2176] 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 7f821e21fecdfdf857dc220fd4856d5c6388a9b3 Mon Sep 17 00:00:00 2001 From: Herman Sletmoen Date: Tue, 18 Mar 2025 20:09:10 +0100 Subject: [PATCH 1153/2176] Use t_nounits, D_nounits --- docs/src/tutorials/change_independent_variable.md | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/docs/src/tutorials/change_independent_variable.md b/docs/src/tutorials/change_independent_variable.md index e1243fb2ee..4b7ba037fe 100644 --- a/docs/src/tutorials/change_independent_variable.md +++ b/docs/src/tutorials/change_independent_variable.md @@ -18,8 +18,7 @@ Consider a projectile shot with some initial velocity in a vertical gravitationa ```@example changeivar using ModelingToolkit -@independent_variables t -D = Differential(t) +using ModelingToolkit: t_nounits as t, D_nounits as D @variables x(t) y(t) @parameters g=9.81 v # gravitational acceleration and horizontal velocity eqs = [D(D(y)) ~ -g, D(x) ~ v] @@ -128,7 +127,7 @@ Unlike the original, notice that this system is *non-autonomous* because the ind This means that to change the independent variable from $a$ to $b$, we must provide not only the rate of change relation $db(a)/da = \exp(-b)$, but *also* the equation $a(b) = \exp(b)$ so $a$ can be eliminated in favor of $b$: ```@example changeivar -a = M2.a +a = M2.a # get independent variable of M2 Da = Differential(a) @variables b(a) M3 = change_independent_variable(M2, b, [Da(b) ~ exp(-b), a ~ exp(b)]) From cabd06063c3cdf7119d4b72cce10ed0a77b885ac Mon Sep 17 00:00:00 2001 From: vyudu Date: Tue, 18 Mar 2025 16:21:37 -0400 Subject: [PATCH 1154/2176] 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 From 8a577e971b7c7cd57b17fbc8e5a2f6c5f5e0ae6f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hans=20W=C3=BCrfel?= Date: Wed, 19 Mar 2025 13:11:40 +0100 Subject: [PATCH 1155/2176] prevent invalidation of mtkmodel macro code --- src/systems/model_parsing.jl | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/systems/model_parsing.jl b/src/systems/model_parsing.jl index 1976e89aa2..cb72e2e416 100644 --- a/src/systems/model_parsing.jl +++ b/src/systems/model_parsing.jl @@ -511,9 +511,10 @@ function generate_var!(dict, a, b, varclass, mod; end # Use the `t` defined in the `mod`. When it is unavailable, generate a new `t` with a warning. -function get_t(mod, t) +get_t(mod, t) = invokelatest(_get_t, mod, t) +function _get_t(mod, t) try - get_var(mod, t) + _get_var(mod, t) catch e if e isa UndefVarError @warn("Could not find a predefined `t` in `$mod`; generating a new one within this model.\nConsider defining it or importing `t` (or `t_nounits`, `t_unitful` as `t`) from ModelingToolkit.") @@ -588,7 +589,8 @@ function set_var_metadata(a, ms) a, metadata_with_exprs end -function get_var(mod::Module, b) +get_var(mod, b) = invokelatest(_get_var, mod, b) +function _get_var(mod::Module, b) if b isa Symbol isdefined(mod, b) && return getproperty(mod, b) isdefined(@__MODULE__, b) && return getproperty(@__MODULE__, b) @@ -1355,7 +1357,8 @@ push_something!(v, x...) = push_something!.(Ref(v), x) define_blocks(branch) = [Expr(branch), Expr(branch), Expr(branch), Expr(branch)] -function parse_top_level_branch(condition, x, y = nothing, branch = :if) +Base.@nospecializeinfer function parse_top_level_branch(condition, x::Expr, y::Union{Expr,Nothing} = nothing, branch::Symbol = :if) + @nospecialize condition x y blocks::Vector{Union{Expr, Nothing}} = component_blk, equations_blk, parameter_blk, variable_blk = define_blocks(branch) for arg in x From 36837e2315fb93c8bbeff1de0fa36a4fa696e0d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hans=20W=C3=BCrfel?= Date: Wed, 19 Mar 2025 14:05:16 +0100 Subject: [PATCH 1156/2176] add precompile workload --- src/ModelingToolkit.jl | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/ModelingToolkit.jl b/src/ModelingToolkit.jl index bbaeea1a40..8e84ac14f4 100644 --- a/src/ModelingToolkit.jl +++ b/src/ModelingToolkit.jl @@ -224,6 +224,25 @@ PrecompileTools.@compile_workload begin @variables x(ModelingToolkit.t_nounits) @named sys = ODESystem([ModelingToolkit.D_nounits(x) ~ -x], ModelingToolkit.t_nounits) prob = ODEProblem(structural_simplify(sys), [x => 30.0], (0, 100), [], jac = true) + @mtkmodel __testmod__ begin + @structural_parameters begin + structp = false + end + @variables begin + x(t) = 0.0, [description="foo", guess=1.0] + end + @parameters begin + a = 1.0, [description="bar"] + if structp + b=2*a, [description="if"] + else + c + end + end + @equations begin + x ~ a + b + end + end; end export AbstractTimeDependentSystem, From 8d3779036b0310c7406abfc5a8799dcabeaf0c5f Mon Sep 17 00:00:00 2001 From: Christopher Rackauckas Date: Wed, 19 Mar 2025 12:24:20 -0100 Subject: [PATCH 1157/2176] Update Downstream.yml --- .github/workflows/Downstream.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/Downstream.yml b/.github/workflows/Downstream.yml index 9c5d05e022..5026f798a3 100644 --- a/.github/workflows/Downstream.yml +++ b/.github/workflows/Downstream.yml @@ -27,7 +27,7 @@ jobs: package: - {user: SciML, repo: SciMLBase.jl, group: Downstream} - {user: SciML, repo: Catalyst.jl, group: All} - - {user: SciML, repo: CellMLToolkit.jl, group: All} + - {user: SciML, repo: CellMLToolkit.jl, group: Core} - {user: SciML, repo: SBMLToolkit.jl, group: All} - {user: SciML, repo: NeuralPDE.jl, group: NNPDE} - {user: SciML, repo: DataDrivenDiffEq.jl, group: Downstream} From abb589547a43a978c92cffd6e4256e5f54fb0cf4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hans=20W=C3=BCrfel?= Date: Wed, 19 Mar 2025 15:16:02 +0100 Subject: [PATCH 1158/2176] don't invalidate parse constants --- src/systems/model_parsing.jl | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/systems/model_parsing.jl b/src/systems/model_parsing.jl index cb72e2e416..f395e73b43 100644 --- a/src/systems/model_parsing.jl +++ b/src/systems/model_parsing.jl @@ -640,7 +640,7 @@ function parse_constants!(exprs, dict, body, mod) type = getfield(mod, type) b = _type_check!(get_var(mod, b), a, type, :constants) push!(exprs, - :($(Symbolics._parse_vars( + :($(Symbolics_parse_vars( :constants, type, [:($a = $b), metadata], toconstant)))) dict[:constants][a] = Dict(:value => b, :type => type) if @isdefined metadata @@ -651,7 +651,7 @@ function parse_constants!(exprs, dict, body, mod) end Expr(:(=), a, Expr(:tuple, b, metadata)) => begin push!(exprs, - :($(Symbolics._parse_vars( + :($(Symbolics_parse_vars( :constants, Real, [:($a = $b), metadata], toconstant)))) dict[:constants][a] = Dict{Symbol, Any}(:value => get_var(mod, b)) for data in metadata.args @@ -660,7 +660,7 @@ function parse_constants!(exprs, dict, body, mod) end Expr(:(=), a, b) => begin push!(exprs, - :($(Symbolics._parse_vars( + :($(Symbolics_parse_vars( :constants, Real, [:($a = $b)], toconstant)))) dict[:constants][a] = Dict(:value => get_var(mod, b)) end @@ -674,6 +674,8 @@ function parse_constants!(exprs, dict, body, mod) end end end +# HACK: _parse_vars seems invalidated, so we use invokelatest +Symbolics_parse_vars(x...) = invokelatest(Symbolics._parse_vars, x...) push_additional_defaults!(dict, a, b::Number) = dict[:defaults][a] = b push_additional_defaults!(dict, a, b::QuoteNode) = dict[:defaults][a] = b.value From a4f805d1b243b2795c4acd5695692cf2f232792b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hans=20W=C3=BCrfel?= Date: Wed, 19 Mar 2025 16:14:12 +0100 Subject: [PATCH 1159/2176] defering _pars_vars makes no real world difference --- src/systems/model_parsing.jl | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/systems/model_parsing.jl b/src/systems/model_parsing.jl index f395e73b43..cb72e2e416 100644 --- a/src/systems/model_parsing.jl +++ b/src/systems/model_parsing.jl @@ -640,7 +640,7 @@ function parse_constants!(exprs, dict, body, mod) type = getfield(mod, type) b = _type_check!(get_var(mod, b), a, type, :constants) push!(exprs, - :($(Symbolics_parse_vars( + :($(Symbolics._parse_vars( :constants, type, [:($a = $b), metadata], toconstant)))) dict[:constants][a] = Dict(:value => b, :type => type) if @isdefined metadata @@ -651,7 +651,7 @@ function parse_constants!(exprs, dict, body, mod) end Expr(:(=), a, Expr(:tuple, b, metadata)) => begin push!(exprs, - :($(Symbolics_parse_vars( + :($(Symbolics._parse_vars( :constants, Real, [:($a = $b), metadata], toconstant)))) dict[:constants][a] = Dict{Symbol, Any}(:value => get_var(mod, b)) for data in metadata.args @@ -660,7 +660,7 @@ function parse_constants!(exprs, dict, body, mod) end Expr(:(=), a, b) => begin push!(exprs, - :($(Symbolics_parse_vars( + :($(Symbolics._parse_vars( :constants, Real, [:($a = $b)], toconstant)))) dict[:constants][a] = Dict(:value => get_var(mod, b)) end @@ -674,8 +674,6 @@ function parse_constants!(exprs, dict, body, mod) end end end -# HACK: _parse_vars seems invalidated, so we use invokelatest -Symbolics_parse_vars(x...) = invokelatest(Symbolics._parse_vars, x...) push_additional_defaults!(dict, a, b::Number) = dict[:defaults][a] = b push_additional_defaults!(dict, a, b::QuoteNode) = dict[:defaults][a] = b.value From 6b5c445050f45d3d0f0a83ce83d6745e7bc8f12a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hans=20W=C3=BCrfel?= Date: Wed, 19 Mar 2025 20:02:00 +0100 Subject: [PATCH 1160/2176] despecialize parse_variable_Def --- src/systems/model_parsing.jl | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/systems/model_parsing.jl b/src/systems/model_parsing.jl index cb72e2e416..40a1213466 100644 --- a/src/systems/model_parsing.jl +++ b/src/systems/model_parsing.jl @@ -246,8 +246,9 @@ end # The comments indicate the syntax matched by a block; either when parsed directly # when it is called recursively for parsing a part of an expression. # These variable definitions are part of test suite in `test/model_parsing.jl` -function parse_variable_def!(dict, mod, arg, varclass, kwargs, where_types; +Base.@nospecializeinfer function parse_variable_def!(dict, mod, arg, varclass, kwargs, where_types; def = nothing, type::Type = Real, meta = Dict{DataType, Expr}()) + @nospecialize arg isa LineNumberNode && return MLStyle.@match arg begin # Parses: `a` From a0b413f81168cbc69389f27c75baa4791203df55 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hans=20W=C3=BCrfel?= Date: Wed, 19 Mar 2025 20:11:26 +0100 Subject: [PATCH 1161/2176] despecialize further --- src/systems/model_parsing.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/systems/model_parsing.jl b/src/systems/model_parsing.jl index 40a1213466..512e4e2971 100644 --- a/src/systems/model_parsing.jl +++ b/src/systems/model_parsing.jl @@ -1358,8 +1358,8 @@ push_something!(v, x...) = push_something!.(Ref(v), x) define_blocks(branch) = [Expr(branch), Expr(branch), Expr(branch), Expr(branch)] -Base.@nospecializeinfer function parse_top_level_branch(condition, x::Expr, y::Union{Expr,Nothing} = nothing, branch::Symbol = :if) - @nospecialize condition x y +Base.@nospecializeinfer function parse_top_level_branch(condition, x, y = nothing, branch::Symbol = :if) + @nospecialize blocks::Vector{Union{Expr, Nothing}} = component_blk, equations_blk, parameter_blk, variable_blk = define_blocks(branch) for arg in x From 9235009fcdd46b19a9bba516da10d14d32a5a2c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hans=20W=C3=BCrfel?= Date: Wed, 19 Mar 2025 20:21:25 +0100 Subject: [PATCH 1162/2176] roll back invalidation blocking invoke latest --- src/systems/model_parsing.jl | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/systems/model_parsing.jl b/src/systems/model_parsing.jl index 512e4e2971..01b29ebd29 100644 --- a/src/systems/model_parsing.jl +++ b/src/systems/model_parsing.jl @@ -512,10 +512,9 @@ function generate_var!(dict, a, b, varclass, mod; end # Use the `t` defined in the `mod`. When it is unavailable, generate a new `t` with a warning. -get_t(mod, t) = invokelatest(_get_t, mod, t) -function _get_t(mod, t) +function get_t(mod, t) try - _get_var(mod, t) + get_var(mod, t) catch e if e isa UndefVarError @warn("Could not find a predefined `t` in `$mod`; generating a new one within this model.\nConsider defining it or importing `t` (or `t_nounits`, `t_unitful` as `t`) from ModelingToolkit.") @@ -590,8 +589,7 @@ function set_var_metadata(a, ms) a, metadata_with_exprs end -get_var(mod, b) = invokelatest(_get_var, mod, b) -function _get_var(mod::Module, b) +function get_var(mod::Module, b) if b isa Symbol isdefined(mod, b) && return getproperty(mod, b) isdefined(@__MODULE__, b) && return getproperty(@__MODULE__, b) From bf5742a4dcf7d55e0f1b1fc9e33a18cf6e4199ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hans=20W=C3=BCrfel?= Date: Wed, 19 Mar 2025 20:22:04 +0100 Subject: [PATCH 1163/2176] extend workload --- src/ModelingToolkit.jl | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/ModelingToolkit.jl b/src/ModelingToolkit.jl index 8e84ac14f4..011654bb6a 100644 --- a/src/ModelingToolkit.jl +++ b/src/ModelingToolkit.jl @@ -225,11 +225,20 @@ PrecompileTools.@compile_workload begin @named sys = ODESystem([ModelingToolkit.D_nounits(x) ~ -x], ModelingToolkit.t_nounits) prob = ODEProblem(structural_simplify(sys), [x => 30.0], (0, 100), [], jac = true) @mtkmodel __testmod__ begin + @constants begin + c = 1.0 + end @structural_parameters begin structp = false end - @variables begin - x(t) = 0.0, [description="foo", guess=1.0] + if structp + @variables begin + x(t) = 0.0, [description="foo", guess=1.0] + end + else + @variables begin + x(t) = 0.0, [description="foo w/o structp", guess=1.0] + end end @parameters begin a = 1.0, [description="bar"] From ee2cac10cd5f5e43aad39bac019d684bceffbff2 Mon Sep 17 00:00:00 2001 From: Fredrik Bagge Carlson Date: Thu, 20 Mar 2025 03:27:15 +0100 Subject: [PATCH 1164/2176] Fix docstring for `InitializationProblem` It did not appear to match any of the constructors --- src/systems/diffeqs/abstractodesystem.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/systems/diffeqs/abstractodesystem.jl b/src/systems/diffeqs/abstractodesystem.jl index f39c76246d..306aa59fc1 100644 --- a/src/systems/diffeqs/abstractodesystem.jl +++ b/src/systems/diffeqs/abstractodesystem.jl @@ -1306,7 +1306,7 @@ struct InitializationProblem{iip, specialization} end """ ```julia -InitializationProblem{iip}(sys::AbstractODESystem, u0map, tspan, +InitializationProblem{iip}(sys::AbstractODESystem, t, u0map, parammap = DiffEqBase.NullParameters(); version = nothing, tgrad = false, jac = false, From 3466dee76213669cd63c56cc36b0ab03258d308b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hans=20W=C3=BCrfel?= Date: Thu, 20 Mar 2025 07:12:14 +0100 Subject: [PATCH 1165/2176] apply formatter --- src/systems/model_parsing.jl | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/systems/model_parsing.jl b/src/systems/model_parsing.jl index 01b29ebd29..4632c1b889 100644 --- a/src/systems/model_parsing.jl +++ b/src/systems/model_parsing.jl @@ -246,7 +246,8 @@ end # The comments indicate the syntax matched by a block; either when parsed directly # when it is called recursively for parsing a part of an expression. # These variable definitions are part of test suite in `test/model_parsing.jl` -Base.@nospecializeinfer function parse_variable_def!(dict, mod, arg, varclass, kwargs, where_types; +Base.@nospecializeinfer function parse_variable_def!( + dict, mod, arg, varclass, kwargs, where_types; def = nothing, type::Type = Real, meta = Dict{DataType, Expr}()) @nospecialize arg isa LineNumberNode && return @@ -1356,7 +1357,8 @@ push_something!(v, x...) = push_something!.(Ref(v), x) define_blocks(branch) = [Expr(branch), Expr(branch), Expr(branch), Expr(branch)] -Base.@nospecializeinfer function parse_top_level_branch(condition, x, y = nothing, branch::Symbol = :if) +Base.@nospecializeinfer function parse_top_level_branch( + condition, x, y = nothing, branch::Symbol = :if) @nospecialize blocks::Vector{Union{Expr, Nothing}} = component_blk, equations_blk, parameter_blk, variable_blk = define_blocks(branch) From 3a137d67ec578d336dca4c54a4ef77c5a0a823d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hans=20W=C3=BCrfel?= Date: Thu, 20 Mar 2025 07:35:26 +0100 Subject: [PATCH 1166/2176] apply formatter --- src/ModelingToolkit.jl | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/ModelingToolkit.jl b/src/ModelingToolkit.jl index 011654bb6a..7f934a5769 100644 --- a/src/ModelingToolkit.jl +++ b/src/ModelingToolkit.jl @@ -233,17 +233,17 @@ PrecompileTools.@compile_workload begin end if structp @variables begin - x(t) = 0.0, [description="foo", guess=1.0] + x(t) = 0.0, [description = "foo", guess = 1.0] end else @variables begin - x(t) = 0.0, [description="foo w/o structp", guess=1.0] + x(t) = 0.0, [description = "foo w/o structp", guess = 1.0] end end @parameters begin - a = 1.0, [description="bar"] + a = 1.0, [description = "bar"] if structp - b=2*a, [description="if"] + b = 2 * a, [description = "if"] else c end @@ -251,7 +251,7 @@ PrecompileTools.@compile_workload begin @equations begin x ~ a + b end - end; + end end export AbstractTimeDependentSystem, From 1dcf7c690af613b5ea4d2ebb726b7df92365e6af Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 18 Mar 2025 16:23:16 +0530 Subject: [PATCH 1167/2176] refactor: store ignored analysis points as `IgnoredAnalysisPoint` --- src/systems/abstractsystem.jl | 64 ++++++++++++++++++++++++++++---- src/systems/diffeqs/odesystem.jl | 9 +++-- 2 files changed, 62 insertions(+), 11 deletions(-) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index 39ca1baed3..7fe99cb2eb 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -1421,9 +1421,34 @@ function assertions(sys::AbstractSystem) return merge(asserts, namespaced_asserts) end +""" + $(TYPEDEF) + +Information about an `AnalysisPoint` for which the corresponding connection must be +ignored during `expand_connections`, since the analysis point has been transformed. + +# Fields + +$(TYPEDFIELDS) +""" +struct IgnoredAnalysisPoint + """ + The input variable/connector. + """ + input::Union{BasicSymbolic, AbstractSystem} + """ + The output variables/connectors. + """ + outputs::Vector{Union{BasicSymbolic, AbstractSystem}} +end + const HierarchyVariableT = Vector{Union{BasicSymbolic, Symbol}} const HierarchySystemT = Vector{Union{AbstractSystem, Symbol}} """ +The type returned from `analysis_point_common_hierarchy`. +""" +const HierarchyAnalysisPointT = Vector{Union{IgnoredAnalysisPoint, Symbol}} +""" The type returned from `as_hierarchy`. """ const HierarchyT = Union{HierarchyVariableT, HierarchySystemT} @@ -1440,6 +1465,29 @@ function from_hierarchy(hierarchy::HierarchyT) end end +""" + $(TYPEDSIGNATURES) + +Represent an ignored analysis point as a namespaced hierarchy. The hierarchy is built +using the common hierarchy of all involved systems/variables. +""" +function analysis_point_common_hierarchy(ap::IgnoredAnalysisPoint)::HierarchyAnalysisPointT + isys = as_hierarchy(ap.input) + osyss = as_hierarchy.(ap.outputs) + suffix = Symbol[] + while isys[end] == osyss[1][end] && allequal(last.(osyss)) + push!(suffix, isys[end]) + pop!(isys) + pop!.(osyss) + end + isys = from_hierarchy(isys) + osyss = from_hierarchy.(osyss) + newap = IgnoredAnalysisPoint(isys, osyss) + hierarchy = HierarchyAnalysisPointT([suffix; newap]) + reverse!(hierarchy) + return hierarchy +end + """ $(TYPEDSIGNATURES) @@ -1466,19 +1514,20 @@ end """ $(TYPEDSIGNATURES) -Get the connections to ignore for `sys` and its subsystems. The returned value is a -`Tuple` similar in structure to the `ignored_connections` field. Each system (variable) -in the first (second) element of the tuple is also passed through `as_hierarchy`. +Get the analysis points to ignore for `sys` and its subsystems. The returned value is a +`Tuple` similar in structure to the `ignored_connections` field. """ function ignored_connections(sys::AbstractSystem) - has_ignored_connections(sys) || return (HierarchySystemT[], HierarchyVariableT[]) + has_ignored_connections(sys) || + return (HierarchyAnalysisPointT[], HierarchyAnalysisPointT[]) ics = get_ignored_connections(sys) if ics === nothing - ics = (HierarchySystemT[], HierarchyVariableT[]) + ics = (HierarchyAnalysisPointT[], HierarchyAnalysisPointT[]) end # turn into hierarchies - ics = (map(as_hierarchy, ics[1]), map(as_hierarchy, ics[2])) + ics = (map(analysis_point_common_hierarchy, ics[1]), + map(analysis_point_common_hierarchy, ics[2])) systems = get_systems(sys) # for each subsystem, get its ignored connections, add the name of the subsystem # to the hierarchy and concatenate corresponding buffers of the result @@ -1487,7 +1536,8 @@ function ignored_connections(sys::AbstractSystem) (map(Base.Fix2(push!, nameof(subsys)), sub_ics[1]), map(Base.Fix2(push!, nameof(subsys)), sub_ics[2])) end - return (Vector{HierarchySystemT}(result[1]), Vector{HierarchyVariableT}(result[2])) + return (Vector{HierarchyAnalysisPointT}(result[1]), + Vector{HierarchyAnalysisPointT}(result[2])) end """ diff --git a/src/systems/diffeqs/odesystem.jl b/src/systems/diffeqs/odesystem.jl index 78e8a0e93d..7be3b0c08a 100644 --- a/src/systems/diffeqs/odesystem.jl +++ b/src/systems/diffeqs/odesystem.jl @@ -189,11 +189,12 @@ struct ODESystem <: AbstractODESystem """ split_idxs::Union{Nothing, Vector{Vector{Int}}} """ - The connections to ignore (since they're removed by analysis point transformations). - The first element of the tuple are systems that can't be in the same connection set, - and the second are variables (for the trivial form of `connect`). + The analysis points removed by transformations, representing connections to be + ignored. The first element of the tuple analysis points connecting systems and + the second are ones connecting variables (for the trivial form of `connect`). """ - ignored_connections::Union{Nothing, Tuple{Vector{ODESystem}, Vector{BasicSymbolic}}} + ignored_connections::Union{ + Nothing, Tuple{Vector{IgnoredAnalysisPoint}, Vector{IgnoredAnalysisPoint}}} """ The hierarchical parent system before simplification. """ From fdcac6f395067962e4afee855e42524e545c5f6b Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 18 Mar 2025 16:23:40 +0530 Subject: [PATCH 1168/2176] refactor: unwrap in causal variable `connect` --- src/systems/analysis_points.jl | 3 ++- src/systems/connectors.jl | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/systems/analysis_points.jl b/src/systems/analysis_points.jl index 16dee02934..fe5364205b 100644 --- a/src/systems/analysis_points.jl +++ b/src/systems/analysis_points.jl @@ -213,7 +213,8 @@ function Symbolics.connect( outs::ConnectableSymbolicT...; verbose = true) allvars = (in, out, outs...) validate_causal_variables_connection(allvars) - return AnalysisPoint() ~ AnalysisPoint(in, name, [out; collect(outs)]; verbose) + return AnalysisPoint() ~ AnalysisPoint( + unwrap(in), name, unwrap.([out; collect(outs)]); verbose) end """ diff --git a/src/systems/connectors.jl b/src/systems/connectors.jl index 94971e38ee..2cd8d1a12e 100644 --- a/src/systems/connectors.jl +++ b/src/systems/connectors.jl @@ -152,7 +152,7 @@ function Symbolics.connect(var1::ConnectableSymbolicT, var2::ConnectableSymbolic vars::ConnectableSymbolicT...) allvars = (var1, var2, vars...) validate_causal_variables_connection(allvars) - return Equation(Connection(), Connection(map(SymbolicWithNameof, allvars))) + return Equation(Connection(), Connection(map(SymbolicWithNameof, unwrap.(allvars)))) end function flowvar(sys::AbstractSystem) From ce72751a302336c81164602a06bf4077baa15d80 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 18 Mar 2025 16:24:36 +0530 Subject: [PATCH 1169/2176] fix: only ignore systems in connection sets involving ignored analysis point --- src/systems/analysis_points.jl | 14 ++-- src/systems/connectors.jl | 144 +++++++++++++++++++++++---------- 2 files changed, 110 insertions(+), 48 deletions(-) diff --git a/src/systems/analysis_points.jl b/src/systems/analysis_points.jl index fe5364205b..798524da5d 100644 --- a/src/systems/analysis_points.jl +++ b/src/systems/analysis_points.jl @@ -417,20 +417,20 @@ function with_analysis_point_ignored(sys::AbstractSystem, ap::AnalysisPoint) has_ignored_connections(sys) || return sys ignored = get_ignored_connections(sys) if ignored === nothing - ignored = (ODESystem[], BasicSymbolic[]) + ignored = (IgnoredAnalysisPoint[], IgnoredAnalysisPoint[]) else ignored = copy.(ignored) end if ap.outputs === nothing error("Empty analysis point") end - for x in ap.outputs - if x isa ODESystem - push!(ignored[1], x) - else - push!(ignored[2], unwrap(x)) - end + + if ap.input isa AbstractSystem && all(x -> x isa AbstractSystem, ap.outputs) + push!(ignored[1], IgnoredAnalysisPoint(ap.input, ap.outputs)) + else + push!(ignored[2], IgnoredAnalysisPoint(ap.input, ap.outputs)) end + return @set sys.ignored_connections = ignored end diff --git a/src/systems/connectors.jl b/src/systems/connectors.jl index 2cd8d1a12e..ff4eb3e501 100644 --- a/src/systems/connectors.jl +++ b/src/systems/connectors.jl @@ -328,14 +328,12 @@ namespaced by `namespace`. `ignored_connects[1]`, purely to avoid unnecessary recomputation. """ function connection2set!(connectionsets, namespace, ss, isouter; - ignored_connects = (HierarchySystemT[], HierarchyVariableT[]), - namespaced_ignored_systems = ODESystem[]) - ignored_systems, ignored_variables = ignored_connects + ignored_systems = HierarchySystemT[], ignored_variables = HierarchyVariableT[]) + ns_ignored_systems = from_hierarchy.(ignored_systems) + ns_ignored_variables = from_hierarchy.(ignored_variables) # ignore specified systems ss = filter(ss) do s - all(namespaced_ignored_systems) do igsys - nameof(igsys) != nameof(s) - end + !any(x -> nameof(x) == nameof(s), ns_ignored_systems) end # `ignored_variables` for each `s` in `ss` corresponding_ignored_variables = map( @@ -434,15 +432,95 @@ function generate_connection_set( connectionsets = ConnectionSet[] domain_csets = ConnectionSet[] sys = generate_connection_set!( - connectionsets, domain_csets, sys, find, replace, scalarize, nothing, - # include systems to be ignored - ignored_connections(sys)) + connectionsets, domain_csets, sys, find, replace, scalarize, nothing, ignored_connections(sys)) csets = merge(connectionsets) domain_csets = merge([csets; domain_csets], true) sys, (csets, domain_csets) end +""" + $(TYPEDSIGNATURES) + +For a list of `systems` in a connect equation, return the subset of it to ignore (as a +list of hierarchical systems) based on `ignored_system_aps`, the analysis points to be +ignored. All analysis points in `ignored_system_aps` must contain systems (connectors) +as their input/outputs. +""" +function systems_to_ignore(ignored_system_aps::Vector{HierarchyAnalysisPointT}, + systems::Union{Vector{<:AbstractSystem}, Tuple{Vararg{<:AbstractSystem}}}) + to_ignore = HierarchySystemT[] + for ap in ignored_system_aps + # if `systems` contains the input of the AP, ignore any outputs of the AP present in it. + isys_hierarchy = HierarchySystemT([ap[1].input; @view ap[2:end]]) + isys = from_hierarchy(isys_hierarchy) + any(x -> nameof(x) == nameof(isys), systems) || continue + + for outsys in ap[1].outputs + osys_hierarchy = HierarchySystemT([outsys; @view ap[2:end]]) + osys = from_hierarchy(osys_hierarchy) + any(x -> nameof(x) == nameof(osys), systems) || continue + push!(to_ignore, HierarchySystemT(osys_hierarchy)) + end + end + + return to_ignore +end + +""" + $(TYPEDSIGNATURES) + +For a list of `systems` in a connect equation, return the subset of their variables to +ignore (as a list of hierarchical variables) based on `ignored_system_aps`, the analysis +points to be ignored. All analysis points in `ignored_system_aps` must contain variables +as their input/outputs. +""" +function variables_to_ignore(ignored_variable_aps::Vector{HierarchyAnalysisPointT}, + systems::Union{Vector{<:AbstractSystem}, Tuple{Vararg{<:AbstractSystem}}}) + to_ignore = HierarchyVariableT[] + for ap in ignored_variable_aps + ivar_hierarchy = HierarchyVariableT([ap[1].input; @view ap[2:end]]) + ivar = from_hierarchy(ivar_hierarchy) + any(x -> any(isequal(ivar), renamespace.((x,), unknowns(x))), systems) || continue + + for outvar in ap[1].outputs + ovar_hierarchy = HierarchyVariableT([as_hierarchy(outvar); @view ap[2:end]]) + ovar = from_hierarchy(ovar_hierarchy) + any(x -> any(isequal(ovar), renamespace.((x,), unknowns(x))), systems) || + continue + push!(to_ignore, HierarchyVariableT(ovar_hierarchy)) + end + end + return to_ignore +end + +""" + $(TYPEDSIGNATURES) + +For a list of variables `vars` in a connect equation, return the subset of them ignore +(as a list of symbolic variables) based on `ignored_system_aps`, the analysis points to +be ignored. All analysis points in `ignored_system_aps` must contain variables as their +input/outputs. +""" +function variables_to_ignore(ignored_variable_aps::Vector{HierarchyAnalysisPointT}, + vars::Union{Vector{<:BasicSymbolic}, Tuple{Vararg{<:BasicSymbolic}}}) + to_ignore = eltype(vars)[] + for ap in ignored_variable_aps + ivar_hierarchy = HierarchyVariableT([ap[1].input; @view ap[2:end]]) + ivar = from_hierarchy(ivar_hierarchy) + any(isequal(ivar), vars) || continue + + for outvar in ap[1].outputs + ovar_hierarchy = HierarchyVariableT([outvar; @view ap[2:end]]) + ovar = from_hierarchy(ovar_hierarchy) + any(isequal(ovar), vars) || continue + push!(to_ignore, ovar) + end + end + + return to_ignore +end + """ $(TYPEDSIGNATURES) @@ -456,26 +534,12 @@ Generate connection sets from `connect` equations. - `sys` is the system whose equations are to be searched. - `namespace` is a system representing the namespace in which `sys` exists, or `nothing` for no namespace (if `sys` is top-level). -- `ignored_connects` is a tuple. The first (second) element is a list of systems - (variables) in the format returned by `as_hierarchy` to be ignored when generating - connections. This is typically because the connections they are used in were removed by - analysis point transformations. """ function generate_connection_set!(connectionsets, domain_csets, sys::AbstractSystem, find, replace, scalarize, namespace = nothing, - ignored_connects = (HierarchySystemT[], HierarchyVariableT[])) + ignored_connects = (HierarchyAnalysisPointT[], HierarchyAnalysisPointT[])) subsys = get_systems(sys) - ignored_systems, ignored_variables = ignored_connects - # turn hierarchies into namespaced systems - namespaced_ignored_systems = from_hierarchy.(ignored_systems) - namespaced_ignored_variables = from_hierarchy.(ignored_variables) - namespaced_ignored = (namespaced_ignored_systems, namespaced_ignored_variables) - # filter the subsystems of `sys` to exclude ignored ones - filtered_subsys = filter(subsys) do ss - all(namespaced_ignored_systems) do igsys - nameof(igsys) != nameof(ss) - end - end + ignored_system_aps, ignored_variable_aps = ignored_connects isouter = generate_isouter(sys) eqs′ = get_eqs(sys) @@ -501,8 +565,12 @@ function generate_connection_set!(connectionsets, domain_csets, neweq isa AbstractArray ? append!(eqs, neweq) : push!(eqs, neweq) else if lhs isa Connection && get_systems(lhs) === :domain - connection2set!(domain_csets, namespace, get_systems(rhs), isouter; - ignored_connects, namespaced_ignored_systems) + connected_systems = get_systems(rhs) + connection2set!(domain_csets, namespace, connected_systems, isouter; + ignored_systems = systems_to_ignore( + ignored_system_aps, connected_systems), + ignored_variables = variables_to_ignore( + ignored_variable_aps, connected_systems)) elseif isconnection(rhs) push!(cts, get_systems(rhs)) else @@ -519,22 +587,19 @@ function generate_connection_set!(connectionsets, domain_csets, # all connectors are eventually inside connectors. T = ConnectionElement # only generate connection sets for systems that are not ignored - for s in filtered_subsys + for s in subsys isconnector(s) || continue is_domain_connector(s) && continue - _ignored_variables = ignored_systems_for_subsystem(s, ignored_variables) - _namespaced_ignored_variables = from_hierarchy.(_ignored_variables) for v in unknowns(s) Flow === get_connection_type(v) || continue - # ignore specified variables - any(isequal(v), _namespaced_ignored_variables) && continue push!(connectionsets, ConnectionSet([T(LazyNamespace(namespace, s), v, false)])) end end for ct in cts connection2set!(connectionsets, namespace, ct, isouter; - ignored_connects, namespaced_ignored_systems) + ignored_systems = systems_to_ignore(ignored_system_aps, ct), + ignored_variables = variables_to_ignore(ignored_variable_aps, ct)) end # pre order traversal @@ -558,14 +623,15 @@ ignored by `generate_connection_set!` (`expand_variable_connections`), filter their hierarchy to not include `subsys`. """ function ignored_systems_for_subsystem( - subsys::AbstractSystem, ignored_systems::Vector{<:HierarchyT}) + subsys::AbstractSystem, ignored_systems::Vector{<:Union{ + HierarchyT, HierarchyAnalysisPointT}}) result = eltype(ignored_systems)[] # in case `subsys` is namespaced, get its hierarchy and compare suffixes # instead of the just the last element suffix = reverse!(namespace_hierarchy(nameof(subsys))) N = length(suffix) for igsys in ignored_systems - if igsys[(end - N + 1):end] == suffix + if length(igsys) > N && igsys[(end - N + 1):end] == suffix push!(result, copy(igsys)) for i in 1:N pop!(result[end]) @@ -684,7 +750,6 @@ function expand_variable_connections(sys::AbstractSystem; ignored_variables = no if ignored_variables === nothing ignored_variables = ignored_connections(sys)[2] end - namespaced_ignored = from_hierarchy.(ignored_variables) eqs = copy(get_eqs(sys)) valid_idxs = trues(length(eqs)) additional_eqs = Equation[] @@ -694,14 +759,11 @@ function expand_variable_connections(sys::AbstractSystem; ignored_variables = no connection = eq.rhs elements = get_systems(connection) is_causal_variable_connection(connection) || continue - elements = filter(elements) do el - all(namespaced_ignored) do var - getname(var) != getname(el.var) - end - end valid_idxs[i] = false elements = map(x -> x.var, elements) + to_ignore = variables_to_ignore(ignored_variables, elements) + elements = setdiff(elements, to_ignore) outvar = first(elements) for invar in Iterators.drop(elements, 1) push!(additional_eqs, outvar ~ invar) From c54bfe94aa71f31f67ef67ed9e75c2c0a58ad960 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 18 Mar 2025 16:25:06 +0530 Subject: [PATCH 1170/2176] test: test analysis point transform not ignoring extra connections --- test/downstream/analysis_points.jl | 49 ++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/test/downstream/analysis_points.jl b/test/downstream/analysis_points.jl index 62961f181a..c175803e48 100644 --- a/test/downstream/analysis_points.jl +++ b/test/downstream/analysis_points.jl @@ -438,3 +438,52 @@ end matrices, _ = get_sensitivity(sys, sys.ap) @test matrices == matrices_normal end + +@testset "Ignored analysis points only affect relevant connection sets" begin + m1 = 1 + m2 = 1 + k = 1000 # Spring stiffness + c = 10 # Damping coefficient + + @named inertia1 = Inertia(; J = m1, phi = 0, w = 0) + @named inertia2 = Inertia(; J = m2, phi = 0, w = 0) + + @named spring = Spring(; c = k) + @named damper = Damper(; d = c) + + @named torque = Torque(use_support = false) + + function SystemModel(u = nothing; name = :model) + eqs = [connect(torque.flange, inertia1.flange_a) + connect(inertia1.flange_b, spring.flange_a, damper.flange_a) + connect(inertia2.flange_a, spring.flange_b, damper.flange_b)] + if u !== nothing + push!(eqs, connect(torque.tau, u.output)) + return @named model = ODESystem( + eqs, t; systems = [torque, inertia1, inertia2, spring, damper, u]) + end + ODESystem(eqs, t; systems = [torque, inertia1, inertia2, spring, damper], name) + end + + @named r = Step(start_time = 1) + @named pid = LimPID(k = 400, Ti = 0.5, Td = 1, u_max = 350) + @named filt = SecondOrder(d = 0.9, w = 10, x = 0, xd = 0) + @named sensor = AngleSensor() + @named add = Add() # To add the feedback and feedforward control signals + model = SystemModel() + @named inverse_model = SystemModel() + @named inverse_sensor = AngleSensor() + connections = [connect(r.output, :r, filt.input) # Name connection r to form an analysis point + connect(inverse_model.inertia1.flange_b, inverse_sensor.flange) # Attach the inverse sensor to the inverse model + connect(filt.output, pid.reference, inverse_sensor.phi) # the filtered reference now goes to both the PID controller and the inverse model input + connect(inverse_model.torque.tau, add.input1) + connect(pid.ctr_output, add.input2) + connect(add.output, :u, model.torque.tau) # Name connection u to form an analysis point + connect(model.inertia1.flange_b, sensor.flange) + connect(sensor.phi, :y, pid.measurement)] + closed_loop = ODESystem(connections, t, + systems = [model, inverse_model, pid, filt, sensor, inverse_sensor, r, add], + name = :closed_loop) + # just ensure the system simplifies + Blocks.get_sensitivity(closed_loop, :y) +end From c014d8c893a96f55f6a6419158b7f677f7d38dc1 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 18 Mar 2025 21:33:53 +0530 Subject: [PATCH 1171/2176] fix: be extra safe with unwrapping --- src/systems/analysis_points.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/systems/analysis_points.jl b/src/systems/analysis_points.jl index 798524da5d..eb43bc40ef 100644 --- a/src/systems/analysis_points.jl +++ b/src/systems/analysis_points.jl @@ -428,7 +428,7 @@ function with_analysis_point_ignored(sys::AbstractSystem, ap::AnalysisPoint) if ap.input isa AbstractSystem && all(x -> x isa AbstractSystem, ap.outputs) push!(ignored[1], IgnoredAnalysisPoint(ap.input, ap.outputs)) else - push!(ignored[2], IgnoredAnalysisPoint(ap.input, ap.outputs)) + push!(ignored[2], IgnoredAnalysisPoint(unwrap(ap.input), unwrap.(ap.outputs))) end return @set sys.ignored_connections = ignored From 861abffb1f8107fad6a294e06b673aa9a7bad77d Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 19 Mar 2025 13:51:29 +0530 Subject: [PATCH 1172/2176] test: improve analysis point test --- test/downstream/analysis_points.jl | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/test/downstream/analysis_points.jl b/test/downstream/analysis_points.jl index c175803e48..29b9aad512 100644 --- a/test/downstream/analysis_points.jl +++ b/test/downstream/analysis_points.jl @@ -485,5 +485,12 @@ end systems = [model, inverse_model, pid, filt, sensor, inverse_sensor, r, add], name = :closed_loop) # just ensure the system simplifies - Blocks.get_sensitivity(closed_loop, :y) + mats, _ = get_sensitivity(closed_loop, :y) + S = CS.ss(mats...) + fr = CS.freqrespv(S, [0.01, 1, 100]) + # https://github.com/SciML/ModelingToolkit.jl/pull/3469 + reference_fr = ComplexF64[-1.2505330104772838e-11 - 2.500062613816021e-9im, + -0.0024688370221621625 - 0.002279011866413123im, + 1.8100018764334602 + 0.3623845793211718im] + @test isapprox(fr, reference_fr) end From 41ce7f5ba5fefcb4b4e9ba140bcc4e2b968a8672 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 17 Mar 2025 16:33:16 +0530 Subject: [PATCH 1173/2176] fix: remove extra print statements --- src/systems/discrete_system/implicit_discrete_system.jl | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/systems/discrete_system/implicit_discrete_system.jl b/src/systems/discrete_system/implicit_discrete_system.jl index a9cddcef49..514576e927 100644 --- a/src/systems/discrete_system/implicit_discrete_system.jl +++ b/src/systems/discrete_system/implicit_discrete_system.jl @@ -333,10 +333,8 @@ function SciMLBase.ImplicitDiscreteProblem( u0map = to_varmap(u0map, dvs) u0map = shift_u0map_forward(sys, u0map, defaults(sys)) - @show u0map f, u0, p = process_SciMLProblem( ImplicitDiscreteFunction, sys, u0map, parammap; eval_expression, eval_module, kwargs...) - @show u0 kwargs = filter_kwargs(kwargs) ImplicitDiscreteProblem(f, u0, tspan, p; kwargs...) From 16ac26f9cf3aec79b4eb34123a68848a3006b130 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 17 Mar 2025 16:33:55 +0530 Subject: [PATCH 1174/2176] feat: add `Initial` parameters for unknowns to all non-initialization systems --- src/systems/abstractsystem.jl | 54 ++++++++++++----------------------- 1 file changed, 18 insertions(+), 36 deletions(-) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index 39ca1baed3..c9debc06c2 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -678,21 +678,22 @@ end function add_initialization_parameters(sys::AbstractSystem) @assert !has_systems(sys) || isempty(get_systems(sys)) + is_initializesystem(sys) && return sys + all_initialvars = Set{BasicSymbolic}() # time-independent systems don't initialize unknowns - if is_time_dependent(sys) - eqs = equations(sys) - if !(eqs isa Vector{Equation}) - eqs = Equation[x for x in eqs if x isa Equation] - end - obs, eqs = unhack_observed(observed(sys), eqs) - for x in Iterators.flatten((unknowns(sys), Iterators.map(eq -> eq.lhs, obs))) - x = unwrap(x) - if iscall(x) && operation(x) == getindex - push!(all_initialvars, arguments(x)[1]) - else - push!(all_initialvars, x) - end + # but may initialize parameters using guesses for unknowns + eqs = equations(sys) + if !(eqs isa Vector{Equation}) + eqs = Equation[x for x in eqs if x isa Equation] + end + obs, eqs = unhack_observed(observed(sys), eqs) + for x in Iterators.flatten((unknowns(sys), Iterators.map(eq -> eq.lhs, obs))) + x = unwrap(x) + if iscall(x) && operation(x) == getindex + push!(all_initialvars, arguments(x)[1]) + else + push!(all_initialvars, x) end end for eq in parameter_dependencies(sys) @@ -722,15 +723,8 @@ Returns true if the parameter `p` is of the form `Initial(x)`. """ function isinitial(p) p = unwrap(p) - if iscall(p) - operation(p) isa Initial && return true - if operation(p) === getindex - operation(arguments(p)[1]) isa Initial && return true - end - else - return false - end - return false + return iscall(p) && (operation(p) isa Initial || + operation(p) === getindex && isinitial(arguments(p)[1])) end """ @@ -1345,20 +1339,8 @@ function parameters(sys::AbstractSystem; initial_parameters = false) systems = get_systems(sys) result = unique(isempty(systems) ? ps : [ps; reduce(vcat, namespace_parameters.(systems))]) - if !initial_parameters - if is_time_dependent(sys) - # time-dependent systems have `Initial` parameters for all their - # unknowns/pdeps, all of which should be hidden. - filter!(x -> !iscall(x) || !isa(operation(x), Initial), result) - else - # time-independent systems only have `Initial` parameters for - # pdeps. Any other `Initial` parameters should be kept (e.g. initialization - # systems) - filter!( - x -> !iscall(x) || !isa(operation(x), Initial) || - !has_parameter_dependency_with_lhs(sys, only(arguments(x))), - result) - end + if !initial_parameters && !is_initializesystem(sys) + filter!(x -> !iscall(x) || !isa(operation(x), Initial), result) end return result end From d3a5386a2b2fd28f393398508791de4e1e3a9010 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 17 Mar 2025 16:34:49 +0530 Subject: [PATCH 1175/2176] feat: handle `Initial(x)` initialization_eqs for time-independent systems --- src/systems/nonlinear/initializesystem.jl | 384 +++++++++++++++------- src/systems/problem_utils.jl | 17 + 2 files changed, 280 insertions(+), 121 deletions(-) diff --git a/src/systems/nonlinear/initializesystem.jl b/src/systems/nonlinear/initializesystem.jl index 0f4016be44..c03524c61b 100644 --- a/src/systems/nonlinear/initializesystem.jl +++ b/src/systems/nonlinear/initializesystem.jl @@ -1,9 +1,9 @@ """ $(TYPEDSIGNATURES) -Generate `NonlinearSystem` which initializes an ODE problem from specified initial conditions of an `ODESystem`. +Generate `NonlinearSystem` which initializes a problem from specified initial conditions of an `AbstractTimeDependentSystem`. """ -function generate_initializesystem(sys::AbstractSystem; +function generate_initializesystem(sys::AbstractTimeDependentSystem; u0map = Dict(), pmap = Dict(), initialization_eqs = [], @@ -31,81 +31,70 @@ function generate_initializesystem(sys::AbstractSystem; idxs_diff = isdiffeq.(eqs) # PREPROCESSING - # If `=> nothing` in `u0map`, remove the key from `defs` u0map = copy(anydict(u0map)) - for (k, v) in u0map - v === nothing || continue - delete!(defs, k) - end - filter_missing_values!(u0map) pmap = anydict(pmap) - - # 1) Use algebraic equations of time-dependent systems as initialization constraints - if has_iv(sys) - idxs_alge = .!idxs_diff - append!(eqs_ics, eqs[idxs_alge]) # start equation list with algebraic equations - - eqs_diff = eqs[idxs_diff] - D = Differential(get_iv(sys)) - diffmap = merge( - Dict(eq.lhs => eq.rhs for eq in eqs_diff), - Dict(D(eq.lhs) => D(eq.rhs) for eq in trueobs) - ) - else - diffmap = Dict() - end - - if is_time_dependent(sys) - if has_schedule(sys) && (schedule = get_schedule(sys); !isnothing(schedule)) - # 2) process dummy derivatives and u0map into initialization system - # prepare map for dummy derivative substitution - for x in filter(x -> !isnothing(x[1]), schedule.dummy_sub) - # set dummy derivatives to default_dd_guess unless specified - push!(defs, x[1] => get(guesses, x[1], default_dd_guess)) - end - function process_u0map_with_dummysubs(y, x) - y = get(schedule.dummy_sub, y, y) - y = fixpoint_sub(y, diffmap) - # If we have `D(x) ~ x` and provide [D(x) => x, x => 1.0] to `u0map`, then - # without this condition `defs` would get `x => x` instead of retaining - # `x => 1.0`. - isequal(y, x) && return - if y ∈ vars_set - # variables specified in u0 overrides defaults - push!(defs, y => x) - elseif y isa Symbolics.Arr - # TODO: don't scalarize arrays - merge!(defs, Dict(scalarize(y .=> x))) - elseif y isa Symbolics.BasicSymbolic - # y is a derivative expression expanded; add it to the initialization equations - push!(eqs_ics, y ~ x) - else - error("Initialization expression $y is currently not supported. If its a higher order derivative expression, then only the dummy derivative expressions are supported.") - end - end - for (y, x) in u0map - if Symbolics.isarraysymbolic(y) - process_u0map_with_dummysubs.(collect(y), collect(x)) - else - process_u0map_with_dummysubs(y, x) - end + initsys_preprocessing!(u0map, defs) + + # 1) Use algebraic equations of system as initialization constraints + idxs_alge = .!idxs_diff + append!(eqs_ics, eqs[idxs_alge]) # start equation list with algebraic equations + + eqs_diff = eqs[idxs_diff] + D = Differential(get_iv(sys)) + diffmap = merge( + Dict(eq.lhs => eq.rhs for eq in eqs_diff), + Dict(D(eq.lhs) => D(eq.rhs) for eq in trueobs) + ) + + if has_schedule(sys) && (schedule = get_schedule(sys); !isnothing(schedule)) + # 2) process dummy derivatives and u0map into initialization system + # prepare map for dummy derivative substitution + for x in filter(x -> !isnothing(x[1]), schedule.dummy_sub) + # set dummy derivatives to default_dd_guess unless specified + push!(defs, x[1] => get(guesses, x[1], default_dd_guess)) + end + function process_u0map_with_dummysubs(y, x) + y = get(schedule.dummy_sub, y, y) + y = fixpoint_sub(y, diffmap) + # If we have `D(x) ~ x` and provide [D(x) => x, x => 1.0] to `u0map`, then + # without this condition `defs` would get `x => x` instead of retaining + # `x => 1.0`. + isequal(y, x) && return + if y ∈ vars_set + # variables specified in u0 overrides defaults + push!(defs, y => x) + elseif y isa Symbolics.Arr + # TODO: don't scalarize arrays + merge!(defs, Dict(scalarize(y .=> x))) + elseif y isa Symbolics.BasicSymbolic + # y is a derivative expression expanded; add it to the initialization equations + push!(eqs_ics, y ~ x) + else + error("Initialization expression $y is currently not supported. If its a higher order derivative expression, then only the dummy derivative expressions are supported.") end - else - # 2) System doesn't have a schedule, so dummy derivatives don't exist/aren't handled (SDESystem) - for (k, v) in u0map - defs[k] = v + end + for (y, x) in u0map + if Symbolics.isarraysymbolic(y) + process_u0map_with_dummysubs.(collect(y), collect(x)) + else + process_u0map_with_dummysubs(y, x) end end + else + # 2) System doesn't have a schedule, so dummy derivatives don't exist/aren't handled (SDESystem) + for (k, v) in u0map + defs[k] = v + end + end - # 3) process other variables - for var in vars - if var ∈ keys(defs) - push!(eqs_ics, var ~ defs[var]) - elseif var ∈ keys(guesses) - push!(defs, var => guesses[var]) - elseif check_defguess - error("Invalid setup: variable $(var) has no default value or initial guess") - end + # 3) process other variables + for var in vars + if var ∈ keys(defs) + push!(eqs_ics, var ~ defs[var]) + elseif var ∈ keys(guesses) + push!(defs, var => guesses[var]) + elseif check_defguess + error("Invalid setup: variable $(var) has no default value or initial guess") end end @@ -119,6 +108,179 @@ function generate_initializesystem(sys::AbstractSystem; end # 5) process parameters as initialization unknowns + paramsubs = setup_parameter_initialization!( + sys, pmap, defs, guesses, eqs_ics; check_defguess) + + # 6) parameter dependencies become equations, their LHS become unknowns + # non-numeric dependent parameters stay as parameter dependencies + new_parameter_deps = solve_parameter_dependencies!( + sys, paramsubs, eqs_ics, defs, guesses) + + # 7) handle values provided for dependent parameters similar to values for observed variables + handle_dependent_parameter_constraints!(sys, pmap, eqs_ics, paramsubs) + + # parameters do not include ones that became initialization unknowns + pars = Vector{SymbolicParam}(filter( + p -> !haskey(paramsubs, p), parameters(sys; initial_parameters = true))) + push!(pars, get_iv(sys)) + + # 8) use observed equations for guesses of observed variables if not provided + for eq in trueobs + haskey(defs, eq.lhs) && continue + any(x -> isequal(default_toterm(x), eq.lhs), keys(defs)) && continue + + defs[eq.lhs] = eq.rhs + end + append!(eqs_ics, trueobs) + + vars = [vars; collect(values(paramsubs))] + + # even if `p => tovar(p)` is in `paramsubs`, `isparameter(p[1]) === true` after substitution + # so add scalarized versions as well + scalarize_varmap!(paramsubs) + + eqs_ics = Symbolics.substitute.(eqs_ics, (paramsubs,)) + for k in keys(defs) + defs[k] = substitute(defs[k], paramsubs) + end + meta = InitializationSystemMetadata( + anydict(u0map), anydict(pmap), additional_guesses, + additional_initialization_eqs, extra_metadata, nothing) + return NonlinearSystem(eqs_ics, + vars, + pars; + defaults = defs, + checks = check_units, + parameter_dependencies = new_parameter_deps, + name, + metadata = meta, + kwargs...) +end + +""" +$(TYPEDSIGNATURES) + +Generate `NonlinearSystem` which initializes a problem from specified initial conditions of an `AbstractTimeDependentSystem`. +""" +function generate_initializesystem(sys::AbstractTimeIndependentSystem; + u0map = Dict(), + pmap = Dict(), + initialization_eqs = [], + guesses = Dict(), + algebraic_only = false, + check_units = true, check_defguess = false, + name = nameof(sys), extra_metadata = (;), kwargs...) + eqs = equations(sys) + trueobs, eqs = unhack_observed(observed(sys), eqs) + vars = unique([unknowns(sys); getfield.(trueobs, :lhs)]) + vars_set = Set(vars) # for efficient in-lookup + + eqs_ics = Equation[] + defs = copy(defaults(sys)) # copy so we don't modify sys.defaults + additional_guesses = anydict(guesses) + additional_initialization_eqs = Vector{Equation}(initialization_eqs) + guesses = merge(get_guesses(sys), additional_guesses) + + # PREPROCESSING + u0map = copy(anydict(u0map)) + pmap = anydict(pmap) + initsys_preprocessing!(u0map, defs) + + # Calculate valid `Initial` parameters. These are unknowns for + # which constant initial values were provided. By this point, + # they have been separated into `x => Initial(x)` in `u0map` + # and `Initial(x) => val` in `pmap`. + valid_initial_parameters = Set{BasicSymbolic}() + for (k, v) in u0map + isequal(Initial(k), v) || continue + push!(valid_initial_parameters, v) + end + + # get the initialization equations + if !algebraic_only + initialization_eqs = [get_initialization_eqs(sys); initialization_eqs] + end + + # only include initialization equations where all the involved `Initial` + # parameters are valid. + vs = Set() + initialization_eqs = filter(initialization_eqs) do eq + empty!(vs) + vars!(vs, eq; op = Initial) + non_params = filter(!isparameter, vs) + # error if non-parameters are present in the initialization equations + if !isempty(non_params) + throw(UnknownsInTimeIndependentInitializationError(eq, non_params)) + end + filter!(x -> iscall(x) && isinitial(x), vs) + invalid_initials = setdiff(vs, valid_initial_parameters) + return isempty(invalid_initials) + end + + append!(eqs_ics, initialization_eqs) + + # process parameters as initialization unknowns + paramsubs = setup_parameter_initialization!( + sys, pmap, defs, guesses, eqs_ics; check_defguess) + + # parameter dependencies become equations, their LHS become unknowns + # non-numeric dependent parameters stay as parameter dependencies + new_parameter_deps = solve_parameter_dependencies!( + sys, paramsubs, eqs_ics, defs, guesses) + + # handle values provided for dependent parameters similar to values for observed variables + handle_dependent_parameter_constraints!(sys, pmap, eqs_ics, paramsubs) + + # parameters do not include ones that became initialization unknowns + pars = Vector{SymbolicParam}(filter( + p -> !haskey(paramsubs, p), parameters(sys; initial_parameters = true))) + vars = collect(values(paramsubs)) + + # even if `p => tovar(p)` is in `paramsubs`, `isparameter(p[1]) === true` after substitution + # so add scalarized versions as well + scalarize_varmap!(paramsubs) + + eqs_ics = Symbolics.substitute.(eqs_ics, (paramsubs,)) + for k in keys(defs) + defs[k] = substitute(defs[k], paramsubs) + end + meta = InitializationSystemMetadata( + anydict(u0map), anydict(pmap), additional_guesses, + additional_initialization_eqs, extra_metadata, nothing) + return NonlinearSystem(eqs_ics, + vars, + pars; + defaults = defs, + checks = check_units, + parameter_dependencies = new_parameter_deps, + name, + metadata = meta, + kwargs...) +end + +""" + $(TYPEDSIGNATURES) + +Preprocessing step for initialization. Currently removes key `k` from `defs` and `u0map` +if `k => nothing` is present in `u0map`. +""" +function initsys_preprocessing!(u0map::AbstractDict, defs::AbstractDict) + for (k, v) in u0map + v === nothing || continue + delete!(defs, k) + end + filter_missing_values!(u0map) +end + +""" + $(TYPEDSIGNATURES) + +Update `defs` and `eqs_ics` appropriately for parameter initialization. Return a dictionary +mapping solvable parameters to their `tovar` variants. +""" +function setup_parameter_initialization!( + sys::AbstractSystem, pmap::AbstractDict, defs::AbstractDict, + guesses::AbstractDict, eqs_ics::Vector{Equation}; check_defguess = false) paramsubs = Dict() for p in parameters(sys) if is_parameter_solvable(p, pmap, defs, guesses) @@ -169,8 +331,17 @@ function generate_initializesystem(sys::AbstractSystem; end end - # 6) parameter dependencies become equations, their LHS become unknowns - # non-numeric dependent parameters stay as parameter dependencies + return paramsubs +end + +""" + $(TYPEDSIGNATURES) + +Add appropriate parameter dependencies as initialization equations. Return the new list of +parameter dependencies for the initialization system. +""" +function solve_parameter_dependencies!(sys::AbstractSystem, paramsubs::AbstractDict, + eqs_ics::Vector{Equation}, defs::AbstractDict, guesses::AbstractDict) new_parameter_deps = Equation[] for eq in parameter_dependencies(sys) if !is_variable_floatingpoint(eq.lhs) @@ -184,60 +355,23 @@ function generate_initializesystem(sys::AbstractSystem; push!(defs, varp => guessval) end - # 7) handle values provided for dependent parameters similar to values for observed variables + return new_parameter_deps +end + +""" + $(TYPEDSIGNATURES) + +Turn values provided for parameter dependencies into initialization equations. +""" +function handle_dependent_parameter_constraints!(sys::AbstractSystem, pmap::AbstractDict, + eqs_ics::Vector{Equation}, paramsubs::AbstractDict) for (k, v) in merge(defaults(sys), pmap) if is_variable_floatingpoint(k) && has_parameter_dependency_with_lhs(sys, k) push!(eqs_ics, paramsubs[k] ~ v) end end - # parameters do not include ones that became initialization unknowns - pars = Vector{SymbolicParam}(filter( - p -> !haskey(paramsubs, p), parameters(sys; initial_parameters = true))) - is_time_dependent(sys) && push!(pars, get_iv(sys)) - - if is_time_dependent(sys) - # 8) use observed equations for guesses of observed variables if not provided - for eq in trueobs - haskey(defs, eq.lhs) && continue - any(x -> isequal(default_toterm(x), eq.lhs), keys(defs)) && continue - - defs[eq.lhs] = eq.rhs - end - append!(eqs_ics, trueobs) - end - - if is_time_dependent(sys) - vars = [vars; collect(values(paramsubs))] - else - vars = collect(values(paramsubs)) - end - - # even if `p => tovar(p)` is in `paramsubs`, `isparameter(p[1]) === true` after substitution - # so add scalarized versions as well - for k in collect(keys(paramsubs)) - symbolic_type(k) == ArraySymbolic() || continue - for i in eachindex(k) - paramsubs[k[i]] = paramsubs[k][i] - end - end - - eqs_ics = Symbolics.substitute.(eqs_ics, (paramsubs,)) - for k in keys(defs) - defs[k] = substitute(defs[k], paramsubs) - end - meta = InitializationSystemMetadata( - anydict(u0map), anydict(pmap), additional_guesses, - additional_initialization_eqs, extra_metadata, nothing) - return NonlinearSystem(eqs_ics, - vars, - pars; - defaults = defs, - checks = check_units, - parameter_dependencies = new_parameter_deps, - name, - metadata = meta, - kwargs...) + return nothing end """ @@ -604,3 +738,11 @@ function unhack_observed(obseqs::Vector{Equation}, eqs::Vector{Equation}) end return obseqs, eqs end + +function UnknownsInTimeIndependentInitializationError(eq, non_params) + ArgumentError(""" + Initialization equations for time-independent systems can only contain parameters. \ + Found $non_params in $eq. If the equations refer to the initial guess for unknowns, \ + use the `Initial` operator. + """) +end diff --git a/src/systems/problem_utils.jl b/src/systems/problem_utils.jl index b94a360f93..74aed297d3 100644 --- a/src/systems/problem_utils.jl +++ b/src/systems/problem_utils.jl @@ -491,6 +491,22 @@ function filter_missing_values!(varmap::AbstractDict; missing_values = nothing) end end +""" + $(TYPEDSIGNATURES) + +For each `k => v` in `varmap` where `k` is an array (or array symbolic) add +`k[i] => v[i]` for all `i in eachindex(k)`. Return the modified `varmap`. +""" +function scalarize_varmap!(varmap::AbstractDict) + for k in collect(keys(varmap)) + symbolic_type(k) == ArraySymbolic() || continue + for i in eachindex(k) + varmap[k[i]] = varmap[k][i] + end + end + return varmap +end + struct GetUpdatedMTKParameters{G, S} # `getu` functor which gets parameters that are unknowns during initialization getpunknowns::G @@ -800,6 +816,7 @@ function process_SciMLProblem( op, missing_unknowns, missing_pars = build_operating_point!(sys, u0map, pmap, defs, cmap, dvs, ps) + # Main.@infiltrate sys isa NonlinearSystem if u0_constructor === identity && u0Type <: StaticArray u0_constructor = vals -> SymbolicUtils.Code.create_array( u0Type, eltype(vals), Val(1), Val(length(vals)), vals...) From ff7d28de118a43cfccd1bdc71760511229642687 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 17 Mar 2025 16:35:00 +0530 Subject: [PATCH 1176/2176] test: test `Initial(x)` initialization_eqs for time-independent systems --- test/initializationsystem.jl | 59 ++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/test/initializationsystem.jl b/test/initializationsystem.jl index 93054eb5f4..54b847c64a 100644 --- a/test/initializationsystem.jl +++ b/test/initializationsystem.jl @@ -1417,3 +1417,62 @@ end @test prob.ps[p1] ≈ 3.0 end end + +@testset "`Initial(X)` in time-independent systems: $Problem" for Problem in [ + NonlinearProblem, NonlinearLeastSquaresProblem] + @parameters k1 k2 + @variables X1(t) X2(t) + @parameters Γ[1:1]=missing [guess = [1.0]] + eqs = [ + 0 ~ k1 * (Γ[1] - X1) - k2 * X1 + ] + initialization_eqs = [ + X2 ~ Γ[1] - X1 + ] + @mtkbuild nlsys = NonlinearSystem(eqs, [X1, X2], [k1, k2, Γ]; initialization_eqs) + + @testset "throws if initialization_eqs contain unknowns" begin + u0 = [X1 => 1.0, X2 => 2.0] + ps = [k1 => 0.1, k2 => 0.2] + @test_throws ArgumentError Problem(nlsys, u0, ps) + end + + eqs = [0 ~ k1 * (Γ[1] - X1) - k2 * X1 + X2 ~ Γ[1] - X1] + initialization_eqs = [ + Initial(X2) ~ Γ[1] - Initial(X1) + ] + @mtkbuild nlsys = NonlinearSystem(eqs, [X1, X2], [k1, k2, Γ]; initialization_eqs) + + @testset "solves initialization" begin + u0 = [X1 => 1.0, X2 => 2.0] + ps = [k1 => 0.1, k2 => 0.2] + prob = Problem(nlsys, u0, ps) + @test state_values(prob.f.initialization_data.initializeprob) === nothing + @test prob.ps[Γ[1]] ≈ 3.0 + end + + @testset "respects explicitly provided value" begin + u0 = [] + ps = [k1 => 0.1, k2 => 0.2, Γ => [5.0]] + prob = Problem(nlsys, u0, ps) + @test prob.ps[Γ[1]] ≈ 5.0 + end + + @testset "fails initialization if inconsistent explicit value" begin + u0 = [X1 => 1.0, X2 => 2.0] + ps = [k1 => 0.1, k2 => 0.2, Γ => [5.0]] + prob = Problem(nlsys, u0, ps) + sol = solve(prob) + @test sol.retcode == SciMLBase.ReturnCode.InitialFailure + end + + @testset "Ignores initial equation if given insufficient u0" begin + u0 = [X2 => 2.0] + ps = [k1 => 0.1, k2 => 0.2, Γ => [5.0]] + prob = Problem(nlsys, u0, ps) + sol = solve(prob) + @test SciMLBase.successful_retcode(sol) + @test sol.ps[Γ[1]] ≈ 5.0 + end +end From 3e58b5a148795073c9f8d78f05e3af6754e55259 Mon Sep 17 00:00:00 2001 From: Christopher Rackauckas Date: Mon, 17 Mar 2025 11:29:37 -0100 Subject: [PATCH 1177/2176] Update src/systems/problem_utils.jl --- src/systems/problem_utils.jl | 1 - 1 file changed, 1 deletion(-) diff --git a/src/systems/problem_utils.jl b/src/systems/problem_utils.jl index 74aed297d3..902fb0f774 100644 --- a/src/systems/problem_utils.jl +++ b/src/systems/problem_utils.jl @@ -816,7 +816,6 @@ function process_SciMLProblem( op, missing_unknowns, missing_pars = build_operating_point!(sys, u0map, pmap, defs, cmap, dvs, ps) - # Main.@infiltrate sys isa NonlinearSystem if u0_constructor === identity && u0Type <: StaticArray u0_constructor = vals -> SymbolicUtils.Code.create_array( u0Type, eltype(vals), Val(1), Val(length(vals)), vals...) From 25159b8ed86d19aa6fb534f762631cceefc62a47 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 17 Mar 2025 17:55:27 +0530 Subject: [PATCH 1178/2176] refactor: remove extra empty file --- src/doc | 1 - 1 file changed, 1 deletion(-) delete mode 100644 src/doc diff --git a/src/doc b/src/doc deleted file mode 100644 index 8b13789179..0000000000 --- a/src/doc +++ /dev/null @@ -1 +0,0 @@ - From f05b93f8d068523ab1b0ebe5ad490f2dba0717be Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 18 Mar 2025 16:40:13 +0530 Subject: [PATCH 1179/2176] build: bump DiffEqBase compat --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 2b204c5ed7..278cf0b8e8 100644 --- a/Project.toml +++ b/Project.toml @@ -92,7 +92,7 @@ DataInterpolations = "6.4" DataStructures = "0.17, 0.18" DeepDiffs = "1" DelayDiffEq = "5.50" -DiffEqBase = "6.157" +DiffEqBase = "6.165.1" DiffEqCallbacks = "2.16, 3, 4" DiffEqNoiseProcess = "5" DiffRules = "0.1, 1.0" From d5e3d1aafa7b7608c2456b33d4a34dddc3e5a91b Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 18 Mar 2025 21:23:23 +0530 Subject: [PATCH 1180/2176] feat: add `supports_initialization` trait --- src/systems/abstractsystem.jl | 2 ++ src/systems/discrete_system/discrete_system.jl | 2 ++ src/systems/jumps/jumpsystem.jl | 2 ++ src/systems/optimization/constraints_system.jl | 2 ++ src/systems/optimization/optimizationsystem.jl | 2 ++ 5 files changed, 10 insertions(+) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index c9debc06c2..1acb19cf3d 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -676,6 +676,8 @@ function SymbolicUtils.maketerm(::Type{<:BasicSymbolic}, ::Initial, args, meta) return metadata(val, meta) end +supports_initialization(sys::AbstractSystem) = true + function add_initialization_parameters(sys::AbstractSystem) @assert !has_systems(sys) || isempty(get_systems(sys)) is_initializesystem(sys) && return sys diff --git a/src/systems/discrete_system/discrete_system.jl b/src/systems/discrete_system/discrete_system.jl index 952c8b71b0..f51ba638a4 100644 --- a/src/systems/discrete_system/discrete_system.jl +++ b/src/systems/discrete_system/discrete_system.jl @@ -421,3 +421,5 @@ end function DiscreteFunctionExpr(sys::DiscreteSystem, args...; kwargs...) DiscreteFunctionExpr{true}(sys, args...; kwargs...) end + +supports_initialization(::DiscreteSystem) = false diff --git a/src/systems/jumps/jumpsystem.jl b/src/systems/jumps/jumpsystem.jl index b95b035bf6..34fc26bd44 100644 --- a/src/systems/jumps/jumpsystem.jl +++ b/src/systems/jumps/jumpsystem.jl @@ -685,3 +685,5 @@ function (ratemap::JumpSysMajParamMapper{U, V, W})(maj::MassActionJump, newparam scale_rates && JumpProcesses.scalerates!(maj.scaled_rates, maj.reactant_stoch) nothing end + +supports_initialization(::JumpSystem) = false diff --git a/src/systems/optimization/constraints_system.jl b/src/systems/optimization/constraints_system.jl index 094640eb37..8a71c58043 100644 --- a/src/systems/optimization/constraints_system.jl +++ b/src/systems/optimization/constraints_system.jl @@ -246,3 +246,5 @@ function get_cmap(sys::ConstraintsSystem, exprs = nothing) cmap = map(x -> x ~ getdefault(x), cs) return cmap, cs end + +supports_initialization(::ConstraintsSystem) = false diff --git a/src/systems/optimization/optimizationsystem.jl b/src/systems/optimization/optimizationsystem.jl index 4d01bd31c9..391ac88e19 100644 --- a/src/systems/optimization/optimizationsystem.jl +++ b/src/systems/optimization/optimizationsystem.jl @@ -751,3 +751,5 @@ function structural_simplify(sys::OptimizationSystem; split = true, kwargs...) sys = complete(sys; split) return sys end + +supports_initialization(::OptimizationSystem) = false From 62b8c3cf3c9f55a0a676ef16cc35513c61d686ee Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 18 Mar 2025 21:23:34 +0530 Subject: [PATCH 1181/2176] fix: only add `Initial` parameters for systems that support initialization --- src/systems/abstractsystem.jl | 1 + 1 file changed, 1 insertion(+) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index 1acb19cf3d..dd2a834e1c 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -680,6 +680,7 @@ supports_initialization(sys::AbstractSystem) = true function add_initialization_parameters(sys::AbstractSystem) @assert !has_systems(sys) || isempty(get_systems(sys)) + supports_initialization(sys) || return sys is_initializesystem(sys) && return sys all_initialvars = Set{BasicSymbolic}() From ca7b9c12c399e58da6c761ee0a58c4818f0bacb3 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 18 Mar 2025 21:23:48 +0530 Subject: [PATCH 1182/2176] fix: handle cyclic `u0map` in some cases --- src/systems/problem_utils.jl | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/systems/problem_utils.jl b/src/systems/problem_utils.jl index 902fb0f774..67096dda4e 100644 --- a/src/systems/problem_utils.jl +++ b/src/systems/problem_utils.jl @@ -623,10 +623,14 @@ function build_operating_point!(sys::AbstractSystem, end for k in keys(u0map) - u0map[k] = fixpoint_sub(u0map[k], neithermap) + v = fixpoint_sub(u0map[k], neithermap) + isequal(k, v) && continue + u0map[k] = v end for k in keys(pmap) - pmap[k] = fixpoint_sub(pmap[k], neithermap) + v = fixpoint_sub(pmap[k], neithermap) + isequal(k, v) && continue + pmap[k] = v end return op, missing_unknowns, missing_pars @@ -811,11 +815,11 @@ function process_SciMLProblem( else obs, _ = unhack_observed(observed(sys), Equation[x for x in eqs if x isa Equation]) end - is_time_dependent(sys) || add_observed_equations!(u0map, obs) op, missing_unknowns, missing_pars = build_operating_point!(sys, u0map, pmap, defs, cmap, dvs, ps) + add_observed_equations!(u0map, obs) if u0_constructor === identity && u0Type <: StaticArray u0_constructor = vals -> SymbolicUtils.Code.create_array( u0Type, eltype(vals), Val(1), Val(length(vals)), vals...) @@ -837,7 +841,7 @@ function process_SciMLProblem( op[iv] = t end - is_time_dependent(sys) && add_observed_equations!(op, obs) + add_observed_equations!(op, obs) add_parameter_dependencies!(sys, op) if warn_cyclic_dependency From bc0239cbfd92acae512944a6fd4181e5073c4ec9 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 18 Mar 2025 21:24:04 +0530 Subject: [PATCH 1183/2176] test: update NonlinearSystem tests with new `Initial` parameters --- test/nonlinearsystem.jl | 6 ++++-- test/parameter_dependencies.jl | 2 +- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/test/nonlinearsystem.jl b/test/nonlinearsystem.jl index 420bdc088e..5cf3b4b1be 100644 --- a/test/nonlinearsystem.jl +++ b/test/nonlinearsystem.jl @@ -239,11 +239,13 @@ testdict = Dict([:test => 1]) prob_ = remake(prob, u0 = [1.0, 2.0, 3.0], p = [a => 1.1, b => 1.2, c => 1.3]) @test prob_.u0 == [1.0, 2.0, 3.0] - @test prob_.p == MTKParameters(sys, [a => 1.1, b => 1.2, c => 1.3]) + initials = unknowns(sys) .=> ones(3) + @test prob_.p == MTKParameters(sys, [a => 1.1, b => 1.2, c => 1.3, initials...]) prob_ = remake(prob, u0 = Dict(y => 2.0), p = Dict(a => 2.0)) @test prob_.u0 == [1.0, 2.0, 1.0] - @test prob_.p == MTKParameters(sys, [a => 2.0, b => 1.0, c => 1.0]) + initials = [x => 1.0, y => 2.0, z => 1.0] + @test prob_.p == MTKParameters(sys, [a => 2.0, b => 1.0, c => 1.0, initials...]) end @testset "Observed function generation without parameters" begin diff --git a/test/parameter_dependencies.jl b/test/parameter_dependencies.jl index f88f3f03b7..ad2131f458 100644 --- a/test/parameter_dependencies.jl +++ b/test/parameter_dependencies.jl @@ -326,7 +326,7 @@ end eqs = [0 ~ p1 * x * exp(x) + p2] @mtkbuild sys = NonlinearSystem(eqs; parameter_dependencies = [p2 => 2p1]) @test isequal(only(parameters(sys)), p1) - @test Set(full_parameters(sys)) == Set([p1, p2, Initial(p2)]) + @test Set(full_parameters(sys)) == Set([p1, p2, Initial(p2), Initial(x)]) prob = NonlinearProblem(sys, [x => 1.0]) @test prob.ps[p1] == 1.0 @test prob.ps[p2] == 2.0 From b5cfedb0bf8dfefda5d54f604c38e0364e660704 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 19 Mar 2025 22:56:36 +0530 Subject: [PATCH 1184/2176] test: make hack tests more consistent --- test/structural_transformation/utils.jl | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/structural_transformation/utils.jl b/test/structural_transformation/utils.jl index 9bf239a574..6dfc107cc9 100644 --- a/test/structural_transformation/utils.jl +++ b/test/structural_transformation/utils.jl @@ -77,6 +77,7 @@ end @test length(equations(sys)) == 1 @test length(observed(sys)) == 3 prob = ODEProblem(sys, [x => 1.0], (0.0, 1.0), [foo => _tmp_fn2]) + val[] = 0 @test_nowarn prob.f(prob.u0, prob.p, 0.0) @test val[] == 1 @@ -97,6 +98,7 @@ end @test length(observed(sys)) == 2 prob = ODEProblem( sys, [y => ones(2), z => 2ones(2), x => 3.0], (0.0, 1.0), [foo => _tmp_fn2]) + val[] = 0 @test_nowarn prob.f(prob.u0, prob.p, 0.0) @test val[] == 2 From beb777a730b262cb59c205ee6758b1f140321e53 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 19 Mar 2025 22:56:59 +0530 Subject: [PATCH 1185/2176] fix: fix duplicated code running unconditionally --- src/systems/problem_utils.jl | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/systems/problem_utils.jl b/src/systems/problem_utils.jl index 67096dda4e..1d0c532cc5 100644 --- a/src/systems/problem_utils.jl +++ b/src/systems/problem_utils.jl @@ -706,10 +706,6 @@ function maybe_build_initialization_problem( empty!(missing_unknowns) end - for v in missing_unknowns - op[v] = zero_var(v) - end - empty!(missing_unknowns) return (; initialization_data = SciMLBase.OverrideInitData( initializeprob, update_initializeprob!, initializeprobmap, From 6dc358683c4f1934fcc846effd29926dc388a65d Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 19 Mar 2025 22:57:20 +0530 Subject: [PATCH 1186/2176] fix: only add observed equations to `u0map` for time-independent or initialization systems --- src/systems/problem_utils.jl | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/systems/problem_utils.jl b/src/systems/problem_utils.jl index 1d0c532cc5..d0ae0d174f 100644 --- a/src/systems/problem_utils.jl +++ b/src/systems/problem_utils.jl @@ -815,7 +815,9 @@ function process_SciMLProblem( op, missing_unknowns, missing_pars = build_operating_point!(sys, u0map, pmap, defs, cmap, dvs, ps) - add_observed_equations!(u0map, obs) + if !is_time_dependent(sys) || is_initializesystem(sys) + add_observed_equations!(u0map, obs) + end if u0_constructor === identity && u0Type <: StaticArray u0_constructor = vals -> SymbolicUtils.Code.create_array( u0Type, eltype(vals), Val(1), Val(length(vals)), vals...) From 9335a5ba8e18a73e1036900e9b7931699f65e6c7 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 19 Mar 2025 22:57:34 +0530 Subject: [PATCH 1187/2176] fix: fix `modelingtoolkitize(::NonlinearProblem)` --- src/systems/nonlinear/modelingtoolkitize.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/systems/nonlinear/modelingtoolkitize.jl b/src/systems/nonlinear/modelingtoolkitize.jl index ec35318e3e..2f12157884 100644 --- a/src/systems/nonlinear/modelingtoolkitize.jl +++ b/src/systems/nonlinear/modelingtoolkitize.jl @@ -45,7 +45,7 @@ function modelingtoolkitize( eqs = vcat([0.0 ~ rhs[i] for i in 1:length(prob.f.resid_prototype)]...) else rhs = ArrayInterface.restructure(prob.u0, similar(vars, Num)) - prob.f(rhs, vars, p isa MTKParameters ? (params,) : params) + prob.f(rhs, vars, params) eqs = vcat([0.0 ~ rhs[i] for i in 1:length(rhs)]...) end From f276d4c19a33b9cee811c5a9f7285803dbf73379 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 19 Mar 2025 22:57:47 +0530 Subject: [PATCH 1188/2176] fix: add `add_additional_parameters` kwarg to `complete` --- src/systems/abstractsystem.jl | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index dd2a834e1c..7675eef73b 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -741,7 +741,8 @@ the global structure of the system. One property to note is that if a system is complete, the system will no longer namespace its subsystems or variables, i.e. `isequal(complete(sys).v.i, v.i)`. """ -function complete(sys::AbstractSystem; split = true, flatten = true) +function complete( + sys::AbstractSystem; split = true, flatten = true, add_initial_parameters = true) newunknowns = OrderedSet() newparams = OrderedSet() iv = has_iv(sys) ? get_iv(sys) : nothing @@ -762,7 +763,9 @@ function complete(sys::AbstractSystem; split = true, flatten = true) @set! newsys.parent = complete(sys; split = false, flatten = false) end sys = newsys - sys = add_initialization_parameters(sys) + if add_initial_parameters + sys = add_initialization_parameters(sys) + end end if split && has_index_cache(sys) @set! sys.index_cache = IndexCache(sys) From ec2fbb4a8d4f3ba6ccd49327731de5329a77c3eb Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 19 Mar 2025 22:58:13 +0530 Subject: [PATCH 1189/2176] fix: don't add initial parameters in `ODEProblem(::JumpSystem)` --- src/systems/jumps/jumpsystem.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/systems/jumps/jumpsystem.jl b/src/systems/jumps/jumpsystem.jl index 34fc26bd44..60637ed6d1 100644 --- a/src/systems/jumps/jumpsystem.jl +++ b/src/systems/jumps/jumpsystem.jl @@ -507,7 +507,7 @@ function DiffEqBase.ODEProblem(sys::JumpSystem, u0map, tspan::Union{Tuple, Nothi systems = get_systems(sys), defaults = defaults(sys), guesses = guesses(sys), parameter_dependencies = parameter_dependencies(sys), metadata = get_metadata(sys), gui_metadata = get_gui_metadata(sys)) - osys = complete(osys) + osys = complete(osys; add_initial_parameters = false) return ODEProblem(osys, u0map, tspan, parammap; check_length = false, build_initializeprob = false, kwargs...) else From 77e4e704f825d43a4a52619df50b75a66673fe06 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 20 Mar 2025 12:29:26 +0530 Subject: [PATCH 1190/2176] fix: fix incorrect folding in `substitute` in `OptimizationSystem` --- .../optimization/optimizationsystem.jl | 6 ++--- test/optimizationsystem.jl | 23 ++++++++++++++++++- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/src/systems/optimization/optimizationsystem.jl b/src/systems/optimization/optimizationsystem.jl index 4d01bd31c9..a62580024e 100644 --- a/src/systems/optimization/optimizationsystem.jl +++ b/src/systems/optimization/optimizationsystem.jl @@ -114,8 +114,8 @@ function OptimizationSystem(op, unknowns, ps; unknowns′[i] = irrvar end end - op′ = substitute(op′, irreducible_subs) - constraints = substitute.(constraints, (irreducible_subs,)) + op′ = fast_substitute(op′, irreducible_subs) + constraints = fast_substitute.(constraints, (irreducible_subs,)) if !(isempty(default_u0) && isempty(default_p)) Base.depwarn( @@ -127,7 +127,7 @@ function OptimizationSystem(op, unknowns, ps; throw(ArgumentError("System names must be unique.")) end defaults = todict(defaults) - defaults = Dict(substitute(value(k), irreducible_subs) => substitute( + defaults = Dict(fast_substitute(value(k), irreducible_subs) => fast_substitute( value(v), irreducible_subs) for (k, v) in pairs(defaults) if value(v) !== nothing) diff --git a/test/optimizationsystem.jl b/test/optimizationsystem.jl index c20613441a..2ec9516721 100644 --- a/test/optimizationsystem.jl +++ b/test/optimizationsystem.jl @@ -1,5 +1,6 @@ using ModelingToolkit, SparseArrays, Test, Optimization, OptimizationOptimJL, - OptimizationMOI, Ipopt, AmplNLWriter, Ipopt_jll, SymbolicIndexingInterface + OptimizationMOI, Ipopt, AmplNLWriter, Ipopt_jll, SymbolicIndexingInterface, + LinearAlgebra using ModelingToolkit: get_metadata @testset "basic" begin @@ -388,3 +389,23 @@ end @test all(y -> any(x -> isequal(x, y), unknowns(sys2)), [x2, sys1.x1]) @test all(y -> any(x -> isequal(x, y), parameters(sys2)), [p2, sys1.p1]) end + +function myeigvals_1(A::AbstractMatrix) + eigvals(A)[1] +end + +@register_symbolic myeigvals_1(A::AbstractMatrix) + +@testset "Issue#3473: Registered array function in objective, no irreducible variables" begin + p_free = @variables begin + p1, [bounds = (0, 1)] + p2, [bounds = (0, 1)] + p3, [bounds = (0, 1)] + p4, [bounds = (0, 1)] + end + + m = diagm(p_free) + + obj = myeigvals_1(m) + @test_nowarn OptimizationSystem(obj, p_free, []; name = :osys) +end From dab80d944815f0c057d22fb2f0571563f437a0c2 Mon Sep 17 00:00:00 2001 From: Christopher Rackauckas Date: Thu, 20 Mar 2025 06:48:23 -0100 Subject: [PATCH 1191/2176] Update Debugging.md --- docs/src/basics/Debugging.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/src/basics/Debugging.md b/docs/src/basics/Debugging.md index b432810bd0..6e2d471461 100644 --- a/docs/src/basics/Debugging.md +++ b/docs/src/basics/Debugging.md @@ -65,7 +65,7 @@ logging in a system returned from `debug_system`, use `ModelingToolkit.ASSERTION ```@repl debug dprob[ModelingToolkit.ASSERTION_LOG_VARIABLE] = false; -solve(drob, Tsit5()); +solve(dprob, Tsit5()); ``` ```@docs From 0b8f4d5eae9987bf7541cfe791adbddec749250f Mon Sep 17 00:00:00 2001 From: Christopher Rackauckas Date: Thu, 20 Mar 2025 07:30:20 -0100 Subject: [PATCH 1192/2176] Update Project.toml --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 2b204c5ed7..cd3acc5279 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "ModelingToolkit" uuid = "961ee093-0014-501f-94e3-6117800e7a78" authors = ["Yingbo Ma ", "Chris Rackauckas and contributors"] -version = "9.66.0" +version = "9.67.0" [deps] AbstractTrees = "1520ce14-60c1-5f80-bbc7-55ef81b5835c" From a2d2855c095f9ea1073795d193617295c0ecd380 Mon Sep 17 00:00:00 2001 From: Christopher Rackauckas Date: Thu, 20 Mar 2025 07:53:02 -0100 Subject: [PATCH 1193/2176] Remove undefined exports --- src/ModelingToolkit.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ModelingToolkit.jl b/src/ModelingToolkit.jl index e671b869ae..55dbf69ab9 100644 --- a/src/ModelingToolkit.jl +++ b/src/ModelingToolkit.jl @@ -331,7 +331,7 @@ export debug_system #export ContinuousClock, Discrete, sampletime, input_timedomain, output_timedomain #export has_discrete_domain, has_continuous_domain #export is_discrete_domain, is_continuous_domain, is_hybrid_domain -export Sample, Hold, Shift, ShiftIndex, sampletime, SampleTime, Next, Prev +export Sample, Hold, Shift, ShiftIndex, sampletime, SampleTime export Clock, SolverStepClock, TimeDomain export MTKParameters, reorder_dimension_by_tunables!, reorder_dimension_by_tunables From 9a0dd4b0e86492ed26978598cb7bc124153c222f Mon Sep 17 00:00:00 2001 From: Christopher Rackauckas Date: Thu, 20 Mar 2025 08:20:59 -0100 Subject: [PATCH 1194/2176] Update Project.toml --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 9129523f5e..f4fb6a342c 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "ModelingToolkit" uuid = "961ee093-0014-501f-94e3-6117800e7a78" authors = ["Yingbo Ma ", "Chris Rackauckas and contributors"] -version = "9.67.0" +version = "9.68.0" [deps] AbstractTrees = "1520ce14-60c1-5f80-bbc7-55ef81b5835c" From 52bb5b47cbad0897e418daa29722a830f05542d1 Mon Sep 17 00:00:00 2001 From: Christopher Rackauckas Date: Thu, 20 Mar 2025 09:09:01 -0100 Subject: [PATCH 1195/2176] Update Downstream.yml --- .github/workflows/Downstream.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/Downstream.yml b/.github/workflows/Downstream.yml index 5026f798a3..1d1e4ce34e 100644 --- a/.github/workflows/Downstream.yml +++ b/.github/workflows/Downstream.yml @@ -31,7 +31,7 @@ jobs: - {user: SciML, repo: SBMLToolkit.jl, group: All} - {user: SciML, repo: NeuralPDE.jl, group: NNPDE} - {user: SciML, repo: DataDrivenDiffEq.jl, group: Downstream} - - {user: SciML, repo: StructuralIdentifiability.jl, group: All} + - {user: SciML, repo: StructuralIdentifiability.jl, group: Core} - {user: SciML, repo: ModelingToolkitStandardLibrary.jl, group: Core} - {user: SciML, repo: ModelOrderReduction.jl, group: All} - {user: SciML, repo: MethodOfLines.jl, group: Interface} From 8f6653be4bd40e9fe796df7e3f593df062f5cbe0 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 20 Mar 2025 18:28:21 +0530 Subject: [PATCH 1196/2176] test: make model parsing tests more generic --- test/model_parsing.jl | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/test/model_parsing.jl b/test/model_parsing.jl index 559992d7c5..62c19d2055 100644 --- a/test/model_parsing.jl +++ b/test/model_parsing.jl @@ -3,6 +3,7 @@ using ModelingToolkit: get_connector_type, get_defaults, get_gui_metadata, get_systems, get_ps, getdefault, getname, readable_code, scalarize, symtype, VariableDescription, RegularConnector, get_unit +using SymbolicIndexingInterface using URIs: URI using Distributions using DynamicQuantities, OrdinaryDiffEq @@ -819,15 +820,15 @@ end @named guess_model = GuessModel() j_guess = getguess(guess_model.j) - @test typeof(j_guess) == Num + @test symbolic_type(j_guess) == ScalarSymbolic() @test readable_code(j_guess) == "l(t) / i(t) + k(t)" i_guess = getguess(guess_model.i) - @test typeof(i_guess) == Num + @test symbolic_type(i_guess) == ScalarSymbolic() @test readable_code(i_guess) == "k(t)" l_guess = getguess(guess_model.l) - @test typeof(l_guess) == Num + @test symbolic_type(l_guess) == ScalarSymbolic() @test readable_code(l_guess) == "k(t)" end From 3f7397ba649c3bc4ccfea37d96de2141ef543bd5 Mon Sep 17 00:00:00 2001 From: Herman Sletmoen Date: Thu, 20 Mar 2025 21:54:07 +0100 Subject: [PATCH 1197/2176] Format tutorial --- .../tutorials/change_independent_variable.md | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/docs/src/tutorials/change_independent_variable.md b/docs/src/tutorials/change_independent_variable.md index 4b7ba037fe..d55639a669 100644 --- a/docs/src/tutorials/change_independent_variable.md +++ b/docs/src/tutorials/change_independent_variable.md @@ -4,9 +4,10 @@ Ordinary differential equations describe the rate of change of some dependent va For the modeler it is often most natural to write down the equations with a particular independent variable, say time $t$. However, in many cases there are good reasons for changing the independent variable: - 1. One may want $y(x)$ as a function of $x$ instead of $(x(t), y(t))$ as a function of $t$ - 2. Some differential equations vary more nicely (e.g. less stiff) with respect to one independent variable than another. - 3. It can reduce the number of equations that must be solved (e.g. $y(x)$ is one equation, while $(x(t), y(t))$ are two). + 1. One may want $y(x)$ as a function of $x$ instead of $(x(t), y(t))$ as a function of $t$ + + 2. Some differential equations vary more nicely (e.g. less stiff) with respect to one independent variable than another. + 3. It can reduce the number of equations that must be solved (e.g. $y(x)$ is one equation, while $(x(t), y(t))$ are two). To manually change the independent variable of an ODE, one must rewrite all equations in terms of a new variable and transform differentials with the chain rule. This is mechanical and error-prone. @@ -34,9 +35,9 @@ It expresses the position $(x(t), y(t))$ as a function of time $t$. But suppose we want to determine whether the projectile hits a target 10 meters away. There are at least three ways of answering this: - - Solve the ODE for $(x(t), y(t))$ and use a callback to terminate when $x$ reaches 10 meters, and evaluate $y$ at the final time. - - Solve the ODE for $(x(t), y(t))$ and use root finding to find the time when $x$ reaches 10 meters, and evaluate $y$ at that time. - - Solve the ODE for $y(x)$ and evaluate it at 10 meters. + - Solve the ODE for $(x(t), y(t))$ and use a callback to terminate when $x$ reaches 10 meters, and evaluate $y$ at the final time. + - Solve the ODE for $(x(t), y(t))$ and use root finding to find the time when $x$ reaches 10 meters, and evaluate $y$ at that time. + - Solve the ODE for $y(x)$ and evaluate it at 10 meters. We will demonstrate the last method by changing the independent variable from $t$ to $x$. This transformation is well-defined for any non-zero horizontal velocity $v$, so $x$ and $t$ are one-to-one. @@ -55,7 +56,7 @@ M2s # display this # hide The derivatives are now with respect to the new independent variable $x$, which can be accessed with `M2.x`. -!!! warn +!!! info At this point `x`, `M1.x`, `M1s.x`, `M2.x`, `M2s.x` are *three* different variables. Meanwhile `y`, `M1.y`, `M1s.y`, `M2.y` and `M2s.y` are *four* different variables. @@ -66,7 +67,7 @@ It is straightforward to evolve the ODE for 10 meters and plot the resulting tra ```@example changeivar using OrdinaryDiffEq, Plots -prob = ODEProblem(M2s, [M2s.y => 0.0], [0.0, 10.0], [v => 8.0]) # throw 10 meters with x-velocity 8 m/s +prob = ODEProblem(M2s, [M2s.y => 0.0], [0.0, 10.0], [v => 8.0]) # throw 10 meters sol = solve(prob, Tsit5()) plot(sol; idxs = M2.y) # must index by M2.y = y(x); not M1.y = y(t)! ``` From 1c003264bc60872d4b8726b94d03a62a43d1e8c4 Mon Sep 17 00:00:00 2001 From: Christopher Rackauckas Date: Fri, 21 Mar 2025 09:58:16 -0100 Subject: [PATCH 1198/2176] Update comparison.md Fixes https://github.com/SciML/ModelingToolkit.jl/issues/3472 --- docs/src/comparison.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/src/comparison.md b/docs/src/comparison.md index fcf0c70afb..52d5ab2f70 100644 --- a/docs/src/comparison.md +++ b/docs/src/comparison.md @@ -23,9 +23,9 @@ - Modelica is an object-oriented single dispatch language. ModelingToolkit.jl, built on Julia, uses multiple dispatch extensively to simplify code. - Many Modelica compilers supply a GUI. ModelingToolkit.jl does not. - - Modelica can be used to simulate ODE and DAE systems. ModelingToolkit.jl - has a much more expansive set of system types, including nonlinear systems, - SDEs, PDEs, and more. + - Modelica is designed for simulating ODE and DAE systems (which can include nonlinear dynamics). + In contrast, ModelingToolkit.jl supports a much broader range of system types, including SDEs, + PDEs, time-independent nonlinear systems (e.g. various forms of optimization problems) and more. ## Comparison Against Simulink From 90a8dd6ddfbc3faad04e892d46e5c23096222fa8 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Sat, 22 Mar 2025 14:22:46 +0530 Subject: [PATCH 1199/2176] fix: fix jump system hack --- src/systems/jumps/jumpsystem.jl | 25 +++++++------------------ 1 file changed, 7 insertions(+), 18 deletions(-) diff --git a/src/systems/jumps/jumpsystem.jl b/src/systems/jumps/jumpsystem.jl index 60637ed6d1..1ad6d30f59 100644 --- a/src/systems/jumps/jumpsystem.jl +++ b/src/systems/jumps/jumpsystem.jl @@ -4,25 +4,14 @@ const JumpType = Union{VariableRateJump, ConstantRateJump, MassActionJump} # call reset_aggregated_jumps!(integrator). # assumes iip function _reset_aggregator!(expr, integrator) - if expr isa Symbol - error("Error, encountered a symbol. This should not happen.") + @assert Meta.isexpr(expr, :function) + body = expr.args[end] + body = quote + $body + $reset_aggregated_jumps!($integrator) end - if expr isa LineNumberNode - return - end - - if (expr.head == :function) - _reset_aggregator!(expr.args[end], integrator) - else - if expr.args[end] == :nothing - expr.args[end] = :(reset_aggregated_jumps!($integrator)) - push!(expr.args, :nothing) - else - _reset_aggregator!(expr.args[end], integrator) - end - end - - nothing + expr.args[end] = body + return nothing end """ From ac51f7396ccdd47b56164d7434e3da51be2e2487 Mon Sep 17 00:00:00 2001 From: CompatHelper Julia Date: Sun, 23 Mar 2025 00:46:24 +0000 Subject: [PATCH 1200/2176] CompatHelper: bump compat for DataInterpolations to 8 for package docs, (keep existing compat) --- docs/Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/Project.toml b/docs/Project.toml index cedbcc9061..e1df906ca5 100644 --- a/docs/Project.toml +++ b/docs/Project.toml @@ -29,7 +29,7 @@ Unitful = "1986cc42-f94f-5a68-af5c-568840ba703d" [compat] BenchmarkTools = "1.3" BifurcationKit = "0.4" -DataInterpolations = "6.5" +DataInterpolations = "6.5, 8" Distributions = "0.25" Documenter = "1" DynamicQuantities = "^0.11.2, 0.12, 1" From f2d00b01daab6fc299ad7a498d37b418f8c928f4 Mon Sep 17 00:00:00 2001 From: Christopher Rackauckas Date: Sun, 23 Mar 2025 10:34:35 -0100 Subject: [PATCH 1201/2176] Update Project.toml --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index f4fb6a342c..404efdbbe3 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "ModelingToolkit" uuid = "961ee093-0014-501f-94e3-6117800e7a78" authors = ["Yingbo Ma ", "Chris Rackauckas and contributors"] -version = "9.68.0" +version = "9.68.1" [deps] AbstractTrees = "1520ce14-60c1-5f80-bbc7-55ef81b5835c" From 247160d89fd9fe560cf15179f96f1351d39c5582 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 20 Mar 2025 16:09:35 +0530 Subject: [PATCH 1202/2176] feat: use DI to calculate jacobians in `LinearizationFunction` --- Project.toml | 6 ++- src/ModelingToolkit.jl | 3 ++ src/linearization.jl | 118 ++++++++++++++++++++++++++++++++++------- 3 files changed, 107 insertions(+), 20 deletions(-) diff --git a/Project.toml b/Project.toml index 404efdbbe3..216ef0c73a 100644 --- a/Project.toml +++ b/Project.toml @@ -4,6 +4,7 @@ authors = ["Yingbo Ma ", "Chris Rackauckas h(xz, p, t) - end, u) - pf = SciMLBase.ParamJacobianWrapper(fun, t, u) - fg_u = jacobian_wrt_vars(pf, p, linfun.input_idxs, linfun.chunk) + fg_xz = linfun.uf_jac(u, DI.Constant(p), DI.Constant(t)) + h_xz = linfun.h_jac(u, DI.Constant(p), DI.Constant(t)) + fg_u = linfun.pf_jac([p[idx] for idx in linfun.input_idxs], DI.Constant(u), DI.Constant(p), DI.Constant(t)) else linfun.num_states == 0 || error("Number of unknown variables (0) does not match the number of input unknowns ($(length(u)))") fg_xz = zeros(0, 0) h_xz = fg_u = zeros(0, length(linfun.input_idxs)) end - hp = let u = u, t = t, h = linfun.h - _hp(p) = h(u, p, t) - _hp - end - h_u = jacobian_wrt_vars(hp, p, linfun.input_idxs, linfun.chunk) + h_u = linfun.hp_jac([p[idx] for idx in linfun.input_idxs], DI.Constant(u), DI.Constant(p), DI.Constant(t)) (f_x = fg_xz[linfun.diff_idxs, linfun.diff_idxs], f_z = fg_xz[linfun.diff_idxs, linfun.alge_idxs], g_x = fg_xz[linfun.alge_idxs, linfun.diff_idxs], From da84f0cd133c0c93749d1955b1fd178f43f1e7c4 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 20 Mar 2025 16:09:45 +0530 Subject: [PATCH 1203/2176] test: test alternative AD backends for linearization --- test/downstream/linearize.jl | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/test/downstream/linearize.jl b/test/downstream/linearize.jl index 0336709173..9918f8145d 100644 --- a/test/downstream/linearize.jl +++ b/test/downstream/linearize.jl @@ -1,4 +1,4 @@ -using ModelingToolkit, Test +using ModelingToolkit, ADTypes, Test using CommonSolve: solve # r is an input, and y is an output. @@ -17,11 +17,12 @@ eqs = [u ~ kp * (r - y) lsys, ssys = linearize(sys, [r], [y]) lprob = LinearizationProblem(sys, [r], [y]) lsys2 = solve(lprob) +lsys3, _ = linearize(sys, [r], [y]; autodiff = AutoFiniteDiff()) -@test lsys.A[] == lsys2.A[] == -2 -@test lsys.B[] == lsys2.B[] == 1 -@test lsys.C[] == lsys2.C[] == 1 -@test lsys.D[] == lsys2.D[] == 0 +@test lsys.A[] == lsys2.A[] == lsys3.A[] == -2 +@test lsys.B[] == lsys2.B[] == lsys3.B[] == 1 +@test lsys.C[] == lsys2.C[] == lsys3.C[] == 1 +@test lsys.D[] == lsys2.D[] == lsys3.D[] == 0 lsys, ssys = linearize(sys, [r], [r]) @@ -89,11 +90,13 @@ connections = [f.y ~ c.r # filtered reference to controller reference lsys0, ssys = linearize(cl, [f.u], [p.x]) desired_order = [f.x, p.x] lsys = ModelingToolkit.reorder_unknowns(lsys0, unknowns(ssys), desired_order) +lsys1, ssys = linearize(cl, [f.u], [p.x]; autodiff = AutoFiniteDiff()) +lsys2 = ModelingToolkit.reorder_unknowns(lsys1, unknowns(ssys), desired_order) -@test lsys.A == [-2 0; 1 -2] -@test lsys.B == reshape([1, 0], 2, 1) -@test lsys.C == [0 1] -@test lsys.D[] == 0 +@test lsys.A == lsys2.A == [-2 0; 1 -2] +@test lsys.B == lsys2.B == reshape([1, 0], 2, 1) +@test lsys.C == lsys2.C == [0 1] +@test lsys.D[] == lsys2.D[] == 0 ## Symbolic linearization lsyss, _ = ModelingToolkit.linearize_symbolic(cl, [f.u], [p.x]) From f9b0fd3e42b92f3fe7db1ac96dae1729b9422a2f Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 20 Mar 2025 16:09:52 +0530 Subject: [PATCH 1204/2176] refactor: remove `jacobian_wrt_vars` --- src/systems/parameter_buffer.jl | 29 ----------------------------- src/utils.jl | 21 --------------------- 2 files changed, 50 deletions(-) diff --git a/src/systems/parameter_buffer.jl b/src/systems/parameter_buffer.jl index 4f1bd1a234..a8ebeeb1e4 100644 --- a/src/systems/parameter_buffer.jl +++ b/src/systems/parameter_buffer.jl @@ -731,35 +731,6 @@ function Base.:(==)(a::MTKParameters, b::MTKParameters) end) end -# to support linearize/linearization_function -function jacobian_wrt_vars(pf::F, p::MTKParameters, input_idxs, chunk::C) where {F, C} - tunable, _, _ = SciMLStructures.canonicalize(SciMLStructures.Tunable(), p) - T = eltype(tunable) - tag = ForwardDiff.Tag(pf, T) - dualtype = ForwardDiff.Dual{typeof(tag), T, ForwardDiff.chunksize(chunk)} - p_big = SciMLStructures.replace(SciMLStructures.Tunable(), p, dualtype.(tunable)) - p_closure = let pf = pf, - input_idxs = input_idxs, - p_big = p_big - - function (p_small_inner) - for (i, val) in zip(input_idxs, p_small_inner) - set_parameter!(p_big, val, i) - end - return if pf isa SciMLBase.ParamJacobianWrapper - buffer = Array{dualtype}(undef, size(pf.u)) - pf(buffer, p_big) - buffer - else - pf(p_big) - end - end - end - p_small = parameter_values.((p,), input_idxs) - cfg = ForwardDiff.JacobianConfig(p_closure, p_small, chunk, tag) - ForwardDiff.jacobian(p_closure, p_small, cfg, Val(false)) -end - const MISSING_PARAMETERS_MESSAGE = """ Some parameters are missing from the variable map. Please provide a value or default for the following variables: diff --git a/src/utils.jl b/src/utils.jl index 78c665ffca..55f3d2cec5 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -963,27 +963,6 @@ function Base.iterate(it::StatefulBFS, queue = (eltype(it)[(0, it.t)])) return (lv, t), queue end -function jacobian_wrt_vars(pf::F, p, input_idxs, chunk::C) where {F, C} - E = eltype(p) - tag = ForwardDiff.Tag(pf, E) - T = typeof(tag) - dualtype = ForwardDiff.Dual{T, E, ForwardDiff.chunksize(chunk)} - p_big = similar(p, dualtype) - copyto!(p_big, p) - p_closure = let pf = pf, - input_idxs = input_idxs, - p_big = p_big - - function (p_small_inner) - p_big[input_idxs] .= p_small_inner - pf(p_big) - end - end - p_small = p[input_idxs] - cfg = ForwardDiff.JacobianConfig(p_closure, p_small, chunk, tag) - ForwardDiff.jacobian(p_closure, p_small, cfg, Val(false)) -end - function fold_constants(ex) if iscall(ex) maketerm(typeof(ex), operation(ex), map(fold_constants, arguments(ex)), From c1430957df0b7421680682439ac50e4f61730c8a Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 20 Mar 2025 17:07:30 +0530 Subject: [PATCH 1205/2176] Update src/linearization.jl Co-authored-by: Fredrik Bagge Carlson --- src/linearization.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/linearization.jl b/src/linearization.jl index eb6de1e899..1b8f03596a 100644 --- a/src/linearization.jl +++ b/src/linearization.jl @@ -25,7 +25,7 @@ The `simplified_sys` has undergone [`structural_simplify`](@ref) and had any occ - `simplify`: Apply simplification in tearing. - `initialize`: If true, a check is performed to ensure that the operating point is consistent (satisfies algebraic equations). If the op is not consistent, initialization is performed. - `initialization_solver_alg`: A NonlinearSolve algorithm to use for solving for a feasible set of state and algebraic variables that satisfies the specified operating point. - - `autodiff`: An `ADType` supported by DifferentiationInterface.jl to use for calculating the necessary jacobians. + - `autodiff`: An `ADType` supported by DifferentiationInterface.jl to use for calculating the necessary jacobians. Defaults to using `AutoForwardDiff()` - `kwargs`: Are passed on to `find_solvables!` See also [`linearize`](@ref) which provides a higher-level interface. From 5c5a6769083cf3422f79bfb17217bbec43c8a192 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 20 Mar 2025 18:38:15 +0530 Subject: [PATCH 1206/2176] fix: handle case when `prob.u0 === nothing` in `linearization_function` --- src/linearization.jl | 59 ++++++++++++++++++++++++++++---------------- 1 file changed, 38 insertions(+), 21 deletions(-) diff --git a/src/linearization.jl b/src/linearization.jl index 1b8f03596a..2da4fc5145 100644 --- a/src/linearization.jl +++ b/src/linearization.jl @@ -89,21 +89,6 @@ function linearization_function(sys::AbstractSystem, inputs, t0 = current_time(prob) inputvals = [p[idx] for idx in input_idxs] - uf_fun = let fun = prob.f - function uff(du, u, p, t) - SciMLBase.UJacobianWrapper(fun, t, p)(du, u) - end - end - uf_jac = PreparedJacobian{true}(uf_fun, similar(prob.u0), autodiff, prob.u0, DI.Constant(p), DI.Constant(t0)) - # observed function is a `GeneratedFunctionWrapper` with iip component - h_jac = PreparedJacobian{true}(h, similar(prob.u0, size(outputs)), autodiff, prob.u0, DI.Constant(p), DI.Constant(t0)) - pf_fun = let fun = prob.f, setter = setp_oop(sys, input_idxs) - function pff(du, input, u, p, t) - p = setter(p, input) - SciMLBase.ParamJacobianWrapper(fun, t, u)(du, p) - end - end - pf_jac = PreparedJacobian{true}(pf_fun, similar(prob.u0), autodiff, inputvals, DI.Constant(prob.u0), DI.Constant(p), DI.Constant(t0)) hp_fun = let fun = h, setter = setp_oop(sys, input_idxs) function hpf(du, input, u, p, t) p = setter(p, input) @@ -111,8 +96,36 @@ function linearization_function(sys::AbstractSystem, inputs, return du end end - hp_jac = PreparedJacobian{true}(hp_fun, similar(prob.u0, size(outputs)), autodiff, inputvals, DI.Constant(prob.u0), DI.Constant(p), DI.Constant(t0)) - + if u0 === nothing + uf_jac = h_jac = pf_jac = nothing + T = p isa MTKParameters ? eltype(p.tunable) : eltype(p) + hp_jac = PreparedJacobian{true}( + hp_fun, zeros(T, size(outputs)), autodiff, inputvals, + DI.Constant(prob.u0), DI.Constant(p), DI.Constant(t0)) + else + uf_fun = let fun = prob.f + function uff(du, u, p, t) + SciMLBase.UJacobianWrapper(fun, t, p)(du, u) + end + end + uf_jac = PreparedJacobian{true}( + uf_fun, similar(prob.u0), autodiff, prob.u0, DI.Constant(p), DI.Constant(t0)) + # observed function is a `GeneratedFunctionWrapper` with iip component + h_jac = PreparedJacobian{true}(h, similar(prob.u0, size(outputs)), autodiff, + prob.u0, DI.Constant(p), DI.Constant(t0)) + pf_fun = let fun = prob.f, setter = setp_oop(ssimilarys, input_idxs) + function pff(du, input, u, p, t) + p = setter(p, input) + SciMLBase.ParamJacobianWrapper(fun, t, u)(du, p) + end + end + pf_jac = PreparedJacobian{true}(pf_fun, similar(prob.u0), autodiff, inputvals, + DI.Constant(prob.u0), DI.Constant(p), DI.Constant(t0)) + hp_jac = PreparedJacobian{true}( + hp_fun, similar(prob.u0, size(outputs)), autodiff, inputvals, + DI.Constant(prob.u0), DI.Constant(p), DI.Constant(t0)) + end + lin_fun = LinearizationFunction( diff_idxs, alge_idxs, input_idxs, length(unknowns(sys)), prob, h, u0 === nothing ? nothing : similar(u0), uf_jac, h_jac, pf_jac, @@ -151,12 +164,14 @@ end function PreparedJacobian{true}(f, buf, autodiff, args...) prep = DI.prepare_jacobian(f, buf, autodiff, args...) - return PreparedJacobian{true, typeof(prep), typeof(f), typeof(buf), typeof(autodiff)}(prep, f, buf, autodiff) + return PreparedJacobian{true, typeof(prep), typeof(f), typeof(buf), typeof(autodiff)}( + prep, f, buf, autodiff) end function PreparedJacobian{false}(f, autodiff, args...) prep = DI.prepare_jacobian(f, autodiff, args...) - return PreparedJacobian{true, typeof(prep), typeof(f), Nothing, typeof(autodiff)}(prep, f, nothing) + return PreparedJacobian{true, typeof(prep), typeof(f), Nothing, typeof(autodiff)}( + prep, f, nothing) end function (pj::PreparedJacobian{true})(args...) @@ -279,14 +294,16 @@ function (linfun::LinearizationFunction)(u, p, t) end fg_xz = linfun.uf_jac(u, DI.Constant(p), DI.Constant(t)) h_xz = linfun.h_jac(u, DI.Constant(p), DI.Constant(t)) - fg_u = linfun.pf_jac([p[idx] for idx in linfun.input_idxs], DI.Constant(u), DI.Constant(p), DI.Constant(t)) + fg_u = linfun.pf_jac([p[idx] for idx in linfun.input_idxs], + DI.Constant(u), DI.Constant(p), DI.Constant(t)) else linfun.num_states == 0 || error("Number of unknown variables (0) does not match the number of input unknowns ($(length(u)))") fg_xz = zeros(0, 0) h_xz = fg_u = zeros(0, length(linfun.input_idxs)) end - h_u = linfun.hp_jac([p[idx] for idx in linfun.input_idxs], DI.Constant(u), DI.Constant(p), DI.Constant(t)) + h_u = linfun.hp_jac([p[idx] for idx in linfun.input_idxs], + DI.Constant(u), DI.Constant(p), DI.Constant(t)) (f_x = fg_xz[linfun.diff_idxs, linfun.diff_idxs], f_z = fg_xz[linfun.diff_idxs, linfun.alge_idxs], g_x = fg_xz[linfun.alge_idxs, linfun.diff_idxs], From d448fc50a0b2fbd5b698235366a8a00b21c80d55 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 21 Mar 2025 11:35:52 +0530 Subject: [PATCH 1207/2176] fix: fix typo --- src/linearization.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/linearization.jl b/src/linearization.jl index 2da4fc5145..57b171e874 100644 --- a/src/linearization.jl +++ b/src/linearization.jl @@ -113,7 +113,7 @@ function linearization_function(sys::AbstractSystem, inputs, # observed function is a `GeneratedFunctionWrapper` with iip component h_jac = PreparedJacobian{true}(h, similar(prob.u0, size(outputs)), autodiff, prob.u0, DI.Constant(p), DI.Constant(t0)) - pf_fun = let fun = prob.f, setter = setp_oop(ssimilarys, input_idxs) + pf_fun = let fun = prob.f, setter = setp_oop(sys, input_idxs) function pff(du, input, u, p, t) p = setter(p, input) SciMLBase.ParamJacobianWrapper(fun, t, u)(du, p) From 1dedcdbc4375406c4d0a8b8e5bb47b646efd2447 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 24 Mar 2025 11:48:01 +0530 Subject: [PATCH 1208/2176] fix: fix callable parameters in `remake_buffer` --- src/systems/parameter_buffer.jl | 3 +++ test/initial_values.jl | 17 +++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/src/systems/parameter_buffer.jl b/src/systems/parameter_buffer.jl index 4f1bd1a234..23fb2e11a7 100644 --- a/src/systems/parameter_buffer.jl +++ b/src/systems/parameter_buffer.jl @@ -444,6 +444,9 @@ end function validate_parameter_type(ic::IndexCache, stype, sz, sym, index, val) (; portion) = index + if stype <: FnType + stype = fntype_to_function_type(stype) + end # Nonnumeric parameters have to match the type if portion === NONNUMERIC_PORTION val isa stype && return nothing diff --git a/test/initial_values.jl b/test/initial_values.jl index f413c1a018..dbe08962cd 100644 --- a/test/initial_values.jl +++ b/test/initial_values.jl @@ -1,6 +1,7 @@ using ModelingToolkit using ModelingToolkit: t_nounits as t, D_nounits as D, get_u0 using OrdinaryDiffEq +using DataInterpolations using SymbolicIndexingInterface: getu @variables x(t)[1:3]=[1.0, 2.0, 3.0] y(t) z(t)[1:2] @@ -219,3 +220,19 @@ end @test_throws ["a(t)", "c(t)"] ODEProblem( sys, [e => 2, a => b, b => a + 1, c => d, d => c + 1], (0, 1)) end + +@testset "Issue#3490: `remake` works with callable parameters" begin + ts = collect(0.0:0.1:10.0) + spline = LinearInterpolation(ts .^ 2, ts) + Tspline = typeof(spline) + @variables x(t) + @parameters (interp::Tspline)(..) + + @mtkbuild sys = ODESystem(D(x) ~ interp(t), t) + + prob = ODEProblem(sys, [x => 0.0], (0.0, 1.0), [interp => spline]) + spline2 = LinearInterpolation(ts .^ 2, ts .^ 2) + p_new = [interp => spline2] + prob2 = remake(prob; p = p_new) + @test prob2.ps[interp] == spline2 +end From b3f627f4a0ab1f0ec55da765a0ee2b16fb70e4c4 Mon Sep 17 00:00:00 2001 From: Fredrik Bagge Carlson Date: Mon, 24 Mar 2025 11:26:03 +0100 Subject: [PATCH 1209/2176] add tutorial on disturbance modeling for state estimation --- docs/Project.toml | 1 + docs/pages.jl | 1 + docs/src/tutorials/disturbance_modeling.md | 223 +++++++++++++++++++++ test/downstream/test_disturbance_model.jl | 7 +- 4 files changed, 229 insertions(+), 3 deletions(-) create mode 100644 docs/src/tutorials/disturbance_modeling.md diff --git a/docs/Project.toml b/docs/Project.toml index e1df906ca5..10357db364 100644 --- a/docs/Project.toml +++ b/docs/Project.toml @@ -2,6 +2,7 @@ BenchmarkTools = "6e4b80f9-dd63-53aa-95a3-0cdb28fa8baf" BifurcationKit = "0f109fa4-8a5d-4b75-95aa-f515264e7665" ControlSystemsBase = "aaaaaaaa-a6ca-5380-bf3e-84a91bcd477e" +ControlSystemsMTK = "687d7614-c7e5-45fc-bfc3-9ee385575c88" DataInterpolations = "82cc6244-b520-54b8-b5a6-8a565e85f1d0" Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f" Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4" diff --git a/docs/pages.jl b/docs/pages.jl index 70a10c436f..4b2bcc31c8 100644 --- a/docs/pages.jl +++ b/docs/pages.jl @@ -16,6 +16,7 @@ pages = [ "tutorials/domain_connections.md", "tutorials/callable_params.md", "tutorials/linear_analysis.md", + "tutorials/disturbance_modeling.md", "tutorials/fmi.md"], "Examples" => Any[ "Basic Examples" => Any["examples/higher_order.md", diff --git a/docs/src/tutorials/disturbance_modeling.md b/docs/src/tutorials/disturbance_modeling.md new file mode 100644 index 0000000000..54c333ab00 --- /dev/null +++ b/docs/src/tutorials/disturbance_modeling.md @@ -0,0 +1,223 @@ +# Disturbance and input modeling modeling + +Disturbances are often seen as external factors that influence a system. Modeling and simulation of such external influences is common in order to ensure that the plant and or control system can adequately handle or suppress these disturbances. Disturbance modeling is also integral to the problem of state estimation, indeed, modeling how disturbances affect the evolution of the state of the system is crucial in order to accurately estimate this state. + +This tutorial will show how to model disturbances in ModelingToolkit as _disturbance inputs_. This involves demonstrating best practices that make it easy to use a single model to handle both disturbed and undisturbed systems, and making use of the model for both simulation and state estimation. + +## A flexible component-based workflow + +We will consider a simple system consisting of two inertias connected through a flexible shaft, such as a simple transmission system in a car. We start by modeling the plant _without any input signals_: + +```@example DISTURBANCE_MODELING +using ModelingToolkit, OrdinaryDiffEq, LinearAlgebra, Test +using ModelingToolkitStandardLibrary.Mechanical.Rotational +using ModelingToolkitStandardLibrary.Blocks +t = ModelingToolkit.t_nounits +D = ModelingToolkit.D_nounits + +@mtkmodel SystemModel begin + @parameters begin + m1 = 1 + m2 = 1 + k = 10 # Spring stiffness + c = 3 # Damping coefficient + end + @components begin + inertia1 = Inertia(; J = m1, phi = 0, w = 0) + inertia2 = Inertia(; J = m2, phi = 0, w = 0) + spring = Spring(; c = k) + damper = Damper(; d = c) + torque = Torque(use_support = false) + end + @equations begin + connect(torque.flange, inertia1.flange_a) + connect(inertia1.flange_b, spring.flange_a, damper.flange_a) + connect(inertia2.flange_a, spring.flange_b, damper.flange_b) + end +end +``` + +Here, we have added a `torque` component that allows us to add a torque input to drive the system, but we have not connected any signal to it yet. We have not yet made any attempts at modeling disturbances, and this is deliberate, we will handle this later in order to make the plant model as generically useful as possible. + +In order to simulate this system in the presence of disturbances, we must 1. Reason about how disturbances may affect the system, and 2. attach _disturbance inputs_ and _disturbance signals_ to the model. We distinguish between an _input_ and a _signal_ here, where we by _input_ mean an attachment point (connector) to which we may connect a _signal_, i.e., a time-varying function. + +We create a new model that includes disturbance inputs and signals, and attach those to the already defined plant model. We assume that each of the two inertias can be affected by a disturbance torque, such as due to friction or an unknown load on the output inertia. + +```@example DISTURBANCE_MODELING +@mtkmodel ModelWithInputs begin + @components begin + input_signal = Blocks.Sine(frequency = 1, amplitude = 1) + disturbance_signal1 = Blocks.Step(height = -1, start_time = 2) # We add an input signal that equals zero by default so that it has no effect during normal simulation + disturbance_signal2 = Blocks.Step(height = 2, start_time = 4) + disturbance_torque1 = Torque(use_support = false) + disturbance_torque2 = Torque(use_support = false) + system_model = SystemModel() + end + @equations begin + connect(input_signal.output, :u, system_model.torque.tau) + connect(disturbance_signal1.output, :d1, disturbance_torque1.tau) # When we connect the input _signals_, we do so through an analysis point. This allows us to easily disconnect the input signals in situations when we do not need them. + connect(disturbance_signal2.output, :d2, disturbance_torque2.tau) + connect(disturbance_torque1.flange, system_model.inertia1.flange_b) + connect(disturbance_torque2.flange, system_model.inertia2.flange_b) + end +end +``` + +This outer model, `ModelWithInputs`, contains two disturbance inputs, both of type `Torque`. It also contains three signal specifications, one for the control input and two for the corresponding disturbance inputs. Note how we added the disturbance torque inputs at this level of the model, but the control input was added inside the system model. This is a design choice that is up to the modeler, here, we consider the driving torque to be a fundamental part of the model that is always required to make use of it, while the disturbance inputs may be of interest only in certain situations, and we thus add them when needed. Since we have added not only input connectors, but also connected input signals to them, this model is complete and ready for simulation, i.e., there are no _unbound inputs_. + +```@example DISTURBANCE_MODELING +@named model = ModelWithInputs() # Model with load disturbance +ssys = structural_simplify(model) +prob = ODEProblem(ssys, [], (0.0, 6.0)) +sol = solve(prob, Tsit5()) +using Plots +plot(sol) +``` + +A thing to note in the specification of `ModelWithInputs` is the presence of three _analysis points_, `:u`, `:d1`, and `:d2`. When signals are connected through an analysis point, we may at any time linearize the model as if the signals were not connected, i.e., as if the corresponding inputs were unbound. We may also use this to generate a julia function for the dynamics on the form ``f(x,u,p,t,w)`` where the input ``u`` and disturbance ``w`` may be provided as separate function arguments, as if the corresponding input signals were not present in the model. More details regarding this will be presented further below, here, we just demonstrate how we could linearize this system model from the inputs to the angular velocity of the inertias + +```@example DISTURBANCE_MODELING +using ControlSystemsBase, ControlSystemsMTK # ControlSystemsMTK provides the high-level function named_ss and ControlSystemsBase provides the bodeplot function +P = named_ss(model, [ssys.u, ssys.d1, ssys.d2], + [ssys.system_model.inertia1.w, ssys.system_model.inertia2.w]) +bodeplot(P, plotphase = false) +``` + +It's worth noting at this point that the fact that we could connect disturbance outputs from outside of the plant-model definition was enabled by the fact that we used a component-based workflow, where the plant model had the appropriate connectors available. If the plant model had modeled the system using direct equations without connectors, this would not have been possible and the model would thus be significantly less flexible. + +We summarize the findings so far as a number of best practices: + +!!! tip "Best practices" + + - Use a component-based workflow to model the plant + - Model the plant without disturbance inputs to make it as generic as possible + - When disturbance inputs are needed, create a new model that includes the plant model and the disturbance inputs + - Only add input _signals_ at the top level of the model, this applies to both control inputs and disturbance inputs. + - Use analysis points to connect signals to inputs, this allows for easy disconnection of signals when needed, e.g., for linearization or function generation. + +## Modeling for state estimation + +In the example above, we constructed a model for _simulation_ of a disturbance affecting the system. When simulating, we connect an input signal of specified shape that simulates the disturbance, above, we used `Blocks.Step` as input signals. On the other hand, when performing state estimation, the exact shape of the disturbance is typically not known, we might only have some diffuse knowledge of the disturbance characteristics such as "varies smoothly", "makes sudden step changes" or "is approximately periodic with 24hr period". The encoding of such knowledge is commonly reasoned about in the frequency domain, where we specify a disturbance model as a dynamical system with a frequency response similar to the approximate spectrum of the disturbance. [For more details around this, see the in-depth tutorial notebook "How to tune a Kalman filter"](https://juliahub.com/pluto/editor.html?id=ad9ecbf9-bf83-45e7-bbe8-d2e5194f2240). Most algorithms for state estimation, such as a Kalman-filter like estimators, assume that disturbances are independent and identically distributed (i.i.d.). While seemingly restrictive at first glance, when combined with an appropriate disturbance models encoded as dynamical systems, this assumption still allows for a wide range of non i.i.d. disturbances to be modeled. + +When modeling a system in MTK, we essentially (without considering algebraic equations for simplicity in exposition) construct a model of a dynamical system + +```math +\begin{aligned} +\dot x &= f(x, p, t) \\ +y &= g(x, p, t) +``` + +where ``x`` is the state, ``y`` are observed variables, ``p`` are parameters, and ``t`` is time. When using MTK, which variables constitute ``x`` and which are considered part of the output, ``y``, is up to the tool rather than the user, this choice is made by MTK during the call to `@mtkbuild` or the lower-level function `structural_simplify`. + +If we further consider external inputs to the system, such as controlled input signals ``u`` and disturbance inputs ``w``, we can write the system as + +```math +\begin{aligned} +\dot x &= f(x, u, p, t, w) \\ +y &= g(x, u, p, t) +\end{aligned} +``` + +To make use of the model defined above for state estimation, we may want to generate a Julia functions for the dynamics ``f`` and the output equations ``g`` that we can plug into, e.g., a nonlinear version of a Kalman filter or a particle filter, etc. MTK contains utilities to do this, namely, [`generate_control_function`](@ref) and [`build_explicit_observed_function`](@ref) (described in more details in ["Input output"](@ref inputoutput)). These functions take keyword arguments `disturbance_inputs` and `disturbance_argument`, that indicate which variables in the model are considered part of ``w``, and whether or not these variables are to be added as function arguments to ``f``, i.e., whether we have ``f(x, u, p, t)`` or ``f(x, u, p, t, w)``. If we do not include the disturbance inputs as function arguments, MTK will assume that the ``w`` variables are all zero, but any dynamics associated with these variables, such as disturbance models, will be included in the generated function. This allows a state estimator to estimate the state of the disturbance model, provided that this state is [observable](https://en.wikipedia.org/wiki/Observability) from the measured outputs of the system. + +Below, we demonstrate + + 1. How to add an integrating disturbance model + 2. how to generate the functions ``f`` and ``g`` for a typical nonlinear state estimator with explicit disturbance inputs + +```@example DISTURBANCE_MODELING +@mtkmodel IntegratingDisturbance begin + @variables begin + x(t) = 0.0 + w(t) = 0.0, [disturbance = true, input = true] + end + @components begin + input = RealInput() + output = RealOutput() + end + @equations begin + D(x) ~ w + w ~ input.u + output.u ~ x + end +end + +@mtkmodel SystemModelWithDisturbanceModel begin + @components begin + input_signal = Blocks.Sine(frequency = 1, amplitude = 1) + disturbance_signal1 = Blocks.Constant(k = 0) + disturbance_signal2 = Blocks.Constant(k = 0) + disturbance_torque1 = Torque(use_support = false) + disturbance_torque2 = Torque(use_support = false) + disturbance_model = Blocks.Integrator() + system_model = SystemModel() + end + @equations begin + connect(input_signal.output, :u, system_model.torque.tau) + connect(disturbance_signal1.output, :d1, disturbance_model.input) + connect(disturbance_model.output, disturbance_torque1.tau) + connect(disturbance_signal2.output, :d2, disturbance_torque2.tau) + connect(disturbance_torque1.flange, system_model.inertia1.flange_b) + connect(disturbance_torque2.flange, system_model.inertia2.flange_b) + end +end + +@named model_with_disturbance = SystemModelWithDisturbanceModel() +``` + +We demonstrate that this model is complete and can be simulated: + +```@example DISTURBANCE_MODELING +ssys = structural_simplify(model_with_disturbance) +prob = ODEProblem(ssys, [], (0.0, 10.0)) +sol = solve(prob, Tsit5()) +using Test +@test SciMLBase.successful_retcode(sol) +``` + +but we may also generate the functions ``f`` and ``g`` for state estimation: + +```@example DISTURBANCE_MODELING +inputs = [ssys.u] +disturbance_inputs = [ssys.d1, ssys.d2] +P = ssys.system_model +outputs = [P.inertia1.phi, P.inertia2.phi, P.inertia1.w, P.inertia2.w] + +(f_oop, f_ip), x_sym, p_sym, io_sys = ModelingToolkit.generate_control_function( + model_with_disturbance, inputs, disturbance_inputs; disturbance_argument = true) + +g = ModelingToolkit.build_explicit_observed_function( + io_sys, outputs; inputs) + +op = ModelingToolkit.inputs(io_sys) .=> 0 +x0, _ = ModelingToolkit.get_u0_p(io_sys, op, op) +p = MTKParameters(io_sys, op) +u = zeros(1) # Control input +w = zeros(length(disturbance_inputs)) # Disturbance input +@test f_oop(x0, u, p, t, w) == zeros(5) +@test g(x, u, p, 0.0) == [0, 0, 0, 0] + +# Non-zero disturbance inputs should result in non-zero state derivatives. We call `sort` since we do not generally know the order of the state variables +w = [1.0, 2.0] +@test sort(f_oop(x0, u, p, t, w)) == [0, 0, 0, 1, 2] +``` + +## Further reading + +To see full examples that perform state estimation with ModelingToolkit models, see the following resources: + + - [C codegen considered unnecessary: go directly to binary, do not pass C. Compilation of Julia code for deployment in model-based engineering](https://arxiv.org/abs/2502.01128) + - [LowLevelParticleFiltersMTK.jl](https://github.com/baggepinnen/LowLevelParticleFiltersMTK.jl) + +## Index + +```@index +Pages = ["disturbance_modeling.md"] +``` + +```@autodocs +Modules = [ModelingToolkit] +Pages = ["systems/analysis_points.jl"] +Order = [:function, :type] +Private = false +``` diff --git a/test/downstream/test_disturbance_model.jl b/test/downstream/test_disturbance_model.jl index 10fcf9fc1f..97276437e2 100644 --- a/test/downstream/test_disturbance_model.jl +++ b/test/downstream/test_disturbance_model.jl @@ -35,10 +35,11 @@ indexof(sym, syms) = findfirst(isequal(sym), syms) end end +# The addition of disturbance inputs relies on the fact that the plant model has been constructed using connectors, we use these to connect the disturbance inputs from outside the plant-model definition @mtkmodel ModelWithInputs begin @components begin input_signal = Blocks.Sine(frequency = 1, amplitude = 1) - disturbance_signal1 = Blocks.Constant(k = 0) + disturbance_signal1 = Blocks.Constant(k = 0) # We add an input signal that equals zero by default so that it has no effect during normal simulation disturbance_signal2 = Blocks.Constant(k = 0) disturbance_torque1 = Torque(use_support = false) disturbance_torque2 = Torque(use_support = false) @@ -46,7 +47,7 @@ end end @equations begin connect(input_signal.output, :u, system_model.torque.tau) - connect(disturbance_signal1.output, :d1, disturbance_torque1.tau) + connect(disturbance_signal1.output, :d1, disturbance_torque1.tau) # When we connect the input _signals_, we do so through an analysis point. This allows us to easily disconnect the input signals in situations when we do not need them. connect(disturbance_signal2.output, :d2, disturbance_torque2.tau) connect(disturbance_torque1.flange, system_model.inertia1.flange_b) connect(disturbance_torque2.flange, system_model.inertia2.flange_b) @@ -67,7 +68,7 @@ lsys = named_ss( model, [:u, :d1], [P.inertia1.phi, P.inertia2.phi, P.inertia1.w, P.inertia2.w]) ## -# If we now want to add a disturbance model, we cannot do that since we have already connected a constant to the disturbance input. We have also already used the name `d` for an analysis point, but that might not be an issue since we create an outer model and get a new namespace. +# If we now want to add a disturbance model, we cannot do that since we have already connected a constant to the disturbance input, we thus create a new wrapper model with inputs s = tf("s") dist(; name) = ODESystem(1 / s; name) From 25c25ae4feb2b0c9eddc4c92005a0a813b9fb82b Mon Sep 17 00:00:00 2001 From: Fredrik Bagge Carlson Date: Mon, 24 Mar 2025 13:53:59 +0100 Subject: [PATCH 1210/2176] mention relevant signal and model libraries --- docs/src/tutorials/disturbance_modeling.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/docs/src/tutorials/disturbance_modeling.md b/docs/src/tutorials/disturbance_modeling.md index 54c333ab00..a2435c3725 100644 --- a/docs/src/tutorials/disturbance_modeling.md +++ b/docs/src/tutorials/disturbance_modeling.md @@ -202,6 +202,14 @@ w = [1.0, 2.0] @test sort(f_oop(x0, u, p, t, w)) == [0, 0, 0, 1, 2] ``` +## Input signal library + +The [`Blocks` module in ModelingToolkitStandardLibrary](https://docs.sciml.ai/ModelingToolkitStandardLibrary/stable/API/blocks/) contains several predefined input signals, such as `Sine, Step, Ramp, Constant` etc., a few of which were used in the examples above. If you have an input signal represented as a sequence of samples, you may use an [`Interpolation` block](https://docs.sciml.ai/ModelingToolkitStandardLibrary/stable/tutorials/input_component/), e.g., as `src = Interpolation(ConstantInterpolation, data, timepoints)`, see the docstring for a complete example. + +## Disturbance-model library + +There is no library explicitly constructed for disturbance modeling. Standard blocks from the [`Blocks` module in ModelingToolkitStandardLibrary](https://docs.sciml.ai/ModelingToolkitStandardLibrary/stable/API/blocks/), such as `Integrator, TransferFunction, StateSpace`, can model any disturbance with rational spectrum. Examples of this includes disturbance models such as constants, piecewise constant, periodic, highpass, lowpass, and bandpass. For help with filter design, see [ControlSystems.jl: Filter-design](https://juliacontrol.github.io/ControlSystems.jl/stable/man/creating_systems/#Filter-design) and the interface package [ControlSystemsMTK.jl](https://juliacontrol.github.io/ControlSystemsMTK.jl/dev/). In the example above, we made use of `Blocks.Integrator`, which is a disturbance model suitable for disturbances dominated by low-frequency components, such as piecewise constant signals or slowly drifting signals. + ## Further reading To see full examples that perform state estimation with ModelingToolkit models, see the following resources: From a46e0e0cd3211b572c8fdc12790b80b693fad850 Mon Sep 17 00:00:00 2001 From: Fredrik Bagge Carlson Date: Tue, 25 Mar 2025 05:52:54 +0100 Subject: [PATCH 1211/2176] Apply suggestions from code review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Cédric St-Jean --- docs/src/tutorials/disturbance_modeling.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/docs/src/tutorials/disturbance_modeling.md b/docs/src/tutorials/disturbance_modeling.md index a2435c3725..e51d6e8817 100644 --- a/docs/src/tutorials/disturbance_modeling.md +++ b/docs/src/tutorials/disturbance_modeling.md @@ -74,7 +74,7 @@ using Plots plot(sol) ``` -A thing to note in the specification of `ModelWithInputs` is the presence of three _analysis points_, `:u`, `:d1`, and `:d2`. When signals are connected through an analysis point, we may at any time linearize the model as if the signals were not connected, i.e., as if the corresponding inputs were unbound. We may also use this to generate a julia function for the dynamics on the form ``f(x,u,p,t,w)`` where the input ``u`` and disturbance ``w`` may be provided as separate function arguments, as if the corresponding input signals were not present in the model. More details regarding this will be presented further below, here, we just demonstrate how we could linearize this system model from the inputs to the angular velocity of the inertias +A thing to note in the specification of `ModelWithInputs` is the presence of three [analysis points](https://docs.sciml.ai/ModelingToolkit/dev/tutorials/linear_analysis/#ModelingToolkit.AnalysisPoint), `:u`, `:d1`, and `:d2`. When signals are connected through an analysis point, we may at any time linearize the model as if the signals were not connected, i.e., as if the corresponding inputs were unbound. We may also use this to generate a julia function for the dynamics on the form ``f(x,u,p,t,w)`` where the input ``u`` and disturbance ``w`` may be provided as separate function arguments, as if the corresponding input signals were not present in the model. More details regarding this will be presented further below, here, we just demonstrate how we could linearize this system model from the inputs to the angular velocity of the inertias ```@example DISTURBANCE_MODELING using ControlSystemsBase, ControlSystemsMTK # ControlSystemsMTK provides the high-level function named_ss and ControlSystemsBase provides the bodeplot function @@ -105,6 +105,7 @@ When modeling a system in MTK, we essentially (without considering algebraic equ \begin{aligned} \dot x &= f(x, p, t) \\ y &= g(x, p, t) +\end{aligned} ``` where ``x`` is the state, ``y`` are observed variables, ``p`` are parameters, and ``t`` is time. When using MTK, which variables constitute ``x`` and which are considered part of the output, ``y``, is up to the tool rather than the user, this choice is made by MTK during the call to `@mtkbuild` or the lower-level function `structural_simplify`. From a172640c06de001b3955ebe00da4c8840366b18a Mon Sep 17 00:00:00 2001 From: Fredrik Bagge Carlson Date: Tue, 25 Mar 2025 06:29:57 +0100 Subject: [PATCH 1212/2176] clarify that disturbance connectors may sometimes be needed in the plant model --- docs/src/tutorials/disturbance_modeling.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/src/tutorials/disturbance_modeling.md b/docs/src/tutorials/disturbance_modeling.md index e51d6e8817..e9e06ccae4 100644 --- a/docs/src/tutorials/disturbance_modeling.md +++ b/docs/src/tutorials/disturbance_modeling.md @@ -90,7 +90,7 @@ We summarize the findings so far as a number of best practices: !!! tip "Best practices" - Use a component-based workflow to model the plant - - Model the plant without disturbance inputs to make it as generic as possible + - If possible, model the plant without explicit disturbance inputs to make it as generic as possible - When disturbance inputs are needed, create a new model that includes the plant model and the disturbance inputs - Only add input _signals_ at the top level of the model, this applies to both control inputs and disturbance inputs. - Use analysis points to connect signals to inputs, this allows for easy disconnection of signals when needed, e.g., for linearization or function generation. From 0ce6ec054d2fed0a75e04474847f46956425ecbf Mon Sep 17 00:00:00 2001 From: Sam Isaacson Date: Tue, 25 Mar 2025 12:42:01 -0400 Subject: [PATCH 1213/2176] don't run initialization on variable rate jump problems --- src/systems/jumps/jumpsystem.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/systems/jumps/jumpsystem.jl b/src/systems/jumps/jumpsystem.jl index 1ad6d30f59..7aa7c6909b 100644 --- a/src/systems/jumps/jumpsystem.jl +++ b/src/systems/jumps/jumpsystem.jl @@ -502,7 +502,7 @@ function DiffEqBase.ODEProblem(sys::JumpSystem, u0map, tspan::Union{Tuple, Nothi else _, u0, p = process_SciMLProblem(EmptySciMLFunction, sys, u0map, parammap; t = tspan === nothing ? nothing : tspan[1], tofloat = false, - check_length = false) + check_length = false, build_initializeprob = false) f = (du, u, p, t) -> (du .= 0; nothing) observedfun = ObservedFunctionCache(sys; eval_expression, eval_module, checkbounds = get(kwargs, :checkbounds, false)) From 349e9d53d503022971e949584b98282491ef9466 Mon Sep 17 00:00:00 2001 From: Christopher Rackauckas Date: Wed, 26 Mar 2025 11:45:10 -0400 Subject: [PATCH 1214/2176] Update Project.toml --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 216ef0c73a..92d8f98ab4 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "ModelingToolkit" uuid = "961ee093-0014-501f-94e3-6117800e7a78" authors = ["Yingbo Ma ", "Chris Rackauckas and contributors"] -version = "9.68.1" +version = "9.69.0" [deps] ADTypes = "47edcb42-4c32-4615-8424-f2b9edc5f35b" From cd60c91dcdfe4df200088003a3add708febaa9fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Miclu=C8=9Ba-C=C3=A2mpeanu?= Date: Wed, 26 Mar 2025 21:44:54 +0200 Subject: [PATCH 1215/2176] fix: fix depwarn due to wrapping `Vararg` directly in `UnionAll` --- src/systems/connectors.jl | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/systems/connectors.jl b/src/systems/connectors.jl index ff4eb3e501..bb6c5ea838 100644 --- a/src/systems/connectors.jl +++ b/src/systems/connectors.jl @@ -448,7 +448,7 @@ ignored. All analysis points in `ignored_system_aps` must contain systems (conne as their input/outputs. """ function systems_to_ignore(ignored_system_aps::Vector{HierarchyAnalysisPointT}, - systems::Union{Vector{<:AbstractSystem}, Tuple{Vararg{<:AbstractSystem}}}) + systems::Union{Vector{S}, Tuple{Vararg{S}}}) where S <: AbstractSystem to_ignore = HierarchySystemT[] for ap in ignored_system_aps # if `systems` contains the input of the AP, ignore any outputs of the AP present in it. @@ -476,7 +476,7 @@ points to be ignored. All analysis points in `ignored_system_aps` must contain v as their input/outputs. """ function variables_to_ignore(ignored_variable_aps::Vector{HierarchyAnalysisPointT}, - systems::Union{Vector{<:AbstractSystem}, Tuple{Vararg{<:AbstractSystem}}}) + systems::Union{Vector{S}, Tuple{Vararg{S}}}) where S <: AbstractSystem to_ignore = HierarchyVariableT[] for ap in ignored_variable_aps ivar_hierarchy = HierarchyVariableT([ap[1].input; @view ap[2:end]]) @@ -503,7 +503,7 @@ be ignored. All analysis points in `ignored_system_aps` must contain variables a input/outputs. """ function variables_to_ignore(ignored_variable_aps::Vector{HierarchyAnalysisPointT}, - vars::Union{Vector{<:BasicSymbolic}, Tuple{Vararg{<:BasicSymbolic}}}) + vars::Union{Vector{S}, Tuple{Vararg{S}}}) where S <: BasicSymbolic to_ignore = eltype(vars)[] for ap in ignored_variable_aps ivar_hierarchy = HierarchyVariableT([ap[1].input; @view ap[2:end]]) From cca1ea10efc32a1699475af486ccb4184db6583a Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 27 Mar 2025 12:25:27 +0530 Subject: [PATCH 1216/2176] refactor: format --- src/systems/connectors.jl | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/systems/connectors.jl b/src/systems/connectors.jl index bb6c5ea838..6b0600fbb7 100644 --- a/src/systems/connectors.jl +++ b/src/systems/connectors.jl @@ -448,7 +448,7 @@ ignored. All analysis points in `ignored_system_aps` must contain systems (conne as their input/outputs. """ function systems_to_ignore(ignored_system_aps::Vector{HierarchyAnalysisPointT}, - systems::Union{Vector{S}, Tuple{Vararg{S}}}) where S <: AbstractSystem + systems::Union{Vector{S}, Tuple{Vararg{S}}}) where {S <: AbstractSystem} to_ignore = HierarchySystemT[] for ap in ignored_system_aps # if `systems` contains the input of the AP, ignore any outputs of the AP present in it. @@ -476,7 +476,7 @@ points to be ignored. All analysis points in `ignored_system_aps` must contain v as their input/outputs. """ function variables_to_ignore(ignored_variable_aps::Vector{HierarchyAnalysisPointT}, - systems::Union{Vector{S}, Tuple{Vararg{S}}}) where S <: AbstractSystem + systems::Union{Vector{S}, Tuple{Vararg{S}}}) where {S <: AbstractSystem} to_ignore = HierarchyVariableT[] for ap in ignored_variable_aps ivar_hierarchy = HierarchyVariableT([ap[1].input; @view ap[2:end]]) @@ -503,7 +503,7 @@ be ignored. All analysis points in `ignored_system_aps` must contain variables a input/outputs. """ function variables_to_ignore(ignored_variable_aps::Vector{HierarchyAnalysisPointT}, - vars::Union{Vector{S}, Tuple{Vararg{S}}}) where S <: BasicSymbolic + vars::Union{Vector{S}, Tuple{Vararg{S}}}) where {S <: BasicSymbolic} to_ignore = eltype(vars)[] for ap in ignored_variable_aps ivar_hierarchy = HierarchyVariableT([ap[1].input; @view ap[2:end]]) From 28a6ed2b52dec6b7581f3f50981ffc8c17628c6f Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 25 Feb 2025 16:31:33 +0530 Subject: [PATCH 1217/2176] feat: throw error when differentiating registered function with no derivative in `structural_simplify` --- src/structural_transformation/StructuralTransformations.jl | 1 + src/structural_transformation/symbolics_tearing.jl | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/src/structural_transformation/StructuralTransformations.jl b/src/structural_transformation/StructuralTransformations.jl index 2e600e86e4..4adc817ef8 100644 --- a/src/structural_transformation/StructuralTransformations.jl +++ b/src/structural_transformation/StructuralTransformations.jl @@ -4,6 +4,7 @@ using Setfield: @set!, @set using UnPack: @unpack using Symbolics: unwrap, linear_expansion, fast_substitute +import Symbolics using SymbolicUtils using SymbolicUtils.Code using SymbolicUtils.Rewriters diff --git a/src/structural_transformation/symbolics_tearing.jl b/src/structural_transformation/symbolics_tearing.jl index f1cdd7ce9c..6845351008 100644 --- a/src/structural_transformation/symbolics_tearing.jl +++ b/src/structural_transformation/symbolics_tearing.jl @@ -65,7 +65,7 @@ function eq_derivative!(ts::TearingState{ODESystem}, ieq::Int; kwargs...) sys = ts.sys eq = equations(ts)[ieq] - eq = 0 ~ ModelingToolkit.derivative(eq.rhs - eq.lhs, get_iv(sys)) + eq = 0 ~ Symbolics.derivative(eq.rhs - eq.lhs, get_iv(sys); throw_no_derivative = true) push!(equations(ts), eq) # Analyze the new equation and update the graph/solvable_graph # First, copy the previous incidence and add the derivative terms. From 5aceb9e9add2b8549f79489e74ba3b242fc6a5ba Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 26 Feb 2025 21:43:53 +0530 Subject: [PATCH 1218/2176] test: add required derivative, fix initialization in split parameters test --- test/split_parameters.jl | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/test/split_parameters.jl b/test/split_parameters.jl index 74f8d41d73..1052f4ad27 100644 --- a/test/split_parameters.jl +++ b/test/split_parameters.jl @@ -51,8 +51,8 @@ end get_value(interp::Interpolator, t) = interp(t) @register_symbolic get_value(interp::Interpolator, t) -# get_value(data, t, dt) = data[round(Int, t / dt + 1)] -# @register_symbolic get_value(data::Vector, t, dt) + +Symbolics.derivative(::typeof(get_value), args::NTuple{2, Any}, ::Val{2}) = 0 function Sampled(; name, interp = Interpolator(Float64[], 0.0)) pars = @parameters begin @@ -68,11 +68,10 @@ function Sampled(; name, interp = Interpolator(Float64[], 0.0)) output.u ~ get_value(interpolator, t) ] - return ODESystem(eqs, t, vars, [interpolator]; name, systems, - defaults = [output.u => interp.data[1]]) + return ODESystem(eqs, t, vars, [interpolator]; name, systems) end -vars = @variables y(t)=1 dy(t)=0 ddy(t)=0 +vars = @variables y(t) dy(t) ddy(t) @named src = Sampled(; interp = Interpolator(x, dt)) @named int = Integrator() @@ -84,11 +83,9 @@ eqs = [y ~ src.output.u @named sys = ODESystem(eqs, t, vars, []; systems = [int, src]) s = complete(sys) sys = structural_simplify(sys) -@test_broken ODEProblem( - sys, [], (0.0, t_end), [s.src.interpolator => Interpolator(x, dt)]; tofloat = false) prob = ODEProblem( sys, [], (0.0, t_end), [s.src.interpolator => Interpolator(x, dt)]; - tofloat = false, build_initializeprob = false) + tofloat = false) sol = solve(prob, ImplicitEuler()); @test sol.retcode == ReturnCode.Success @test sol[y][end] == x[end] From 8c43ac5dd9bde84a35619bd8e4817d6e5d7ffaf4 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 3 Mar 2025 16:52:54 +0530 Subject: [PATCH 1219/2176] build: bump Symbolics compat --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 92d8f98ab4..a9290b037a 100644 --- a/Project.toml +++ b/Project.toml @@ -151,7 +151,7 @@ StochasticDelayDiffEq = "1.8.1" StochasticDiffEq = "6.72.1" SymbolicIndexingInterface = "0.3.37" SymbolicUtils = "3.14" -Symbolics = "6.29.1" +Symbolics = "6.29.2" URIs = "1" UnPack = "0.1, 1.0" Unitful = "1.1" From 6e5be63ef1b321d59cb77918a72c41869c48fde1 Mon Sep 17 00:00:00 2001 From: Herman Sletmoen Date: Thu, 27 Mar 2025 12:33:40 +0100 Subject: [PATCH 1220/2176] Show system name in function hints also for extended fields --- src/systems/diffeqs/odesystem.jl | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/systems/diffeqs/odesystem.jl b/src/systems/diffeqs/odesystem.jl index 7be3b0c08a..ac08cbb5a7 100644 --- a/src/systems/diffeqs/odesystem.jl +++ b/src/systems/diffeqs/odesystem.jl @@ -717,10 +717,12 @@ function Base.show(io::IO, mime::MIME"text/plain", sys::ODESystem; hint = true, invoke(Base.show, Tuple{typeof(io), typeof(mime), AbstractSystem}, io, mime, sys; hint, bold) + name = nameof(sys) + # Print initialization equations (unique to ODESystems) nini = length(initialization_equations(sys)) nini > 0 && printstyled(io, "\nInitialization equations ($nini):"; bold) - nini > 0 && hint && print(io, " see initialization_equations(sys)") + nini > 0 && hint && print(io, " see initialization_equations($name)") return nothing end From 1d389bd47b343e53e0a734226a218c1283382a15 Mon Sep 17 00:00:00 2001 From: Herman Sletmoen Date: Thu, 27 Mar 2025 12:37:36 +0100 Subject: [PATCH 1221/2176] Show system parameter dependencies --- src/systems/abstractsystem.jl | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index 3da5201039..50440bcaae 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -2045,6 +2045,11 @@ function Base.show( limited && printstyled(io, "\n ⋮") # too many variables to print end + # Print parameter dependencies + npdeps = has_parameter_dependencies(sys) ? length(parameter_dependencies(sys)) : 0 + npdeps > 0 && printstyled(io, "\nParameter dependencies ($npdeps):"; bold) + npdeps > 0 && hint && print(io, " see parameter_dependencies($name)") + # Print observed nobs = has_observed(sys) ? length(observed(sys)) : 0 nobs > 0 && printstyled(io, "\nObserved ($nobs):"; bold) From 18435687c95f88c76bd8065e4a65dc53fb685ef4 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 25 Mar 2025 23:06:56 +0530 Subject: [PATCH 1222/2176] feat: allow toggling namespacing independently of `complete` --- src/ModelingToolkit.jl | 2 +- src/systems/abstractsystem.jl | 38 ++++++++++++++++++- src/systems/analysis_points.jl | 8 +++- src/systems/diffeqs/odesystem.jl | 14 +++++-- src/systems/diffeqs/sdesystem.jl | 19 ++++++---- .../discrete_system/discrete_system.jl | 12 ++++-- .../implicit_discrete_system.jl | 12 ++++-- src/systems/jumps/jumpsystem.jl | 11 ++++-- src/systems/nonlinear/nonlinearsystem.jl | 11 ++++-- .../optimization/constraints_system.jl | 13 +++++-- .../optimization/optimizationsystem.jl | 15 +++++--- src/utils.jl | 14 +++++++ 12 files changed, 132 insertions(+), 37 deletions(-) diff --git a/src/ModelingToolkit.jl b/src/ModelingToolkit.jl index 1fdb946a93..b53d3ec098 100644 --- a/src/ModelingToolkit.jl +++ b/src/ModelingToolkit.jl @@ -328,7 +328,7 @@ export alg_equations, diff_equations, has_alg_equations, has_diff_equations export get_alg_eqs, get_diff_eqs, has_alg_eqs, has_diff_eqs export @variables, @parameters, @independent_variables, @constants, @brownian -export @named, @nonamespace, @namespace, extend, compose, complete +export @named, @nonamespace, @namespace, extend, compose, complete, toggle_namespacing export debug_system #export ContinuousClock, Discrete, sampletime, input_timedomain, output_timedomain diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index 50440bcaae..89ff0e510b 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -590,7 +590,24 @@ function SymbolicIndexingInterface.all_symbols(sys::AbstractSystem) return syms end +""" + $(TYPEDSIGNATURES) + +Check whether a system is marked as `complete`. +""" iscomplete(sys::AbstractSystem) = isdefined(sys, :complete) && getfield(sys, :complete) +""" + $(TYPEDSIGNATURES) + +Check whether a system performs namespacing. +""" +function does_namespacing(sys::AbstractSystem) + if isdefined(sys, :namespacing) + getfield(sys, :namespacing) + else + iscomplete(sys) + end +end """ $(TYPEDSIGNATURES) @@ -798,9 +815,25 @@ function complete( if isdefined(sys, :initializesystem) && get_initializesystem(sys) !== nothing @set! sys.initializesystem = complete(get_initializesystem(sys); split) end + sys = toggle_namespacing(sys, false; safe = true) isdefined(sys, :complete) ? (@set! sys.complete = true) : sys end +""" + $(TYPEDSIGNATURES) + +Return a new `sys` with namespacing enabled or disabled, depending on `value`. The +keyword argument `safe` denotes whether systems that do not support such a toggle +should error or be ignored. +""" +function toggle_namespacing(sys::AbstractSystem, value::Bool; safe = false) + if !isdefined(sys, :namespacing) + safe && return sys + throw(ArgumentError("The system must define the `namespacing` flag to toggle namespacing")) + end + @set sys.namespacing = value +end + """ $(TYPEDSIGNATURES) @@ -985,13 +1018,14 @@ function Base.propertynames(sys::AbstractSystem; private = false) end end -function Base.getproperty(sys::AbstractSystem, name::Symbol; namespace = !iscomplete(sys)) +function Base.getproperty( + sys::AbstractSystem, name::Symbol; namespace = does_namespacing(sys)) if has_parent(sys) && (parent = get_parent(sys); parent !== nothing) return getproperty(parent, name; namespace) end wrap(getvar(sys, name; namespace = namespace)) end -function getvar(sys::AbstractSystem, name::Symbol; namespace = !iscomplete(sys)) +function getvar(sys::AbstractSystem, name::Symbol; namespace = does_namespacing(sys)) systems = get_systems(sys) if isdefined(sys, name) Base.depwarn( diff --git a/src/systems/analysis_points.jl b/src/systems/analysis_points.jl index eb43bc40ef..104c26cbfe 100644 --- a/src/systems/analysis_points.jl +++ b/src/systems/analysis_points.jl @@ -768,7 +768,13 @@ already an `AnalysisPoint` or `Symbol`) is simply wrapped in an array. `Symbol` `AnalysisPoint`s are namespaced with `sys`. """ canonicalize_ap(sys::AbstractSystem, ap::Symbol) = [AnalysisPoint(renamespace(sys, ap))] -canonicalize_ap(sys::AbstractSystem, ap::AnalysisPoint) = [ap] +function canonicalize_ap(sys::AbstractSystem, ap::AnalysisPoint) + if does_namespacing(sys) + return [ap] + else + return [renamespace(sys, ap)] + end +end canonicalize_ap(sys::AbstractSystem, ap) = [ap] function canonicalize_ap(sys::AbstractSystem, aps::Vector) mapreduce(Base.Fix1(canonicalize_ap, sys), vcat, aps; init = []) diff --git a/src/systems/diffeqs/odesystem.jl b/src/systems/diffeqs/odesystem.jl index ac08cbb5a7..bd0a58e10a 100644 --- a/src/systems/diffeqs/odesystem.jl +++ b/src/systems/diffeqs/odesystem.jl @@ -169,7 +169,11 @@ struct ODESystem <: AbstractODESystem """ substitutions::Any """ - If a model `sys` is complete, then `sys.x` no longer performs namespacing. + If false, then `sys.x` no longer performs namespacing. + """ + namespacing::Bool + """ + If true, denotes the model will not be modified any further. """ complete::Bool """ @@ -207,8 +211,8 @@ struct ODESystem <: AbstractODESystem connector_type, preface, cevents, devents, parameter_dependencies, assertions = Dict{BasicSymbolic, String}(), metadata = nothing, gui_metadata = nothing, is_dde = false, - tstops = [], tearing_state = nothing, - substitutions = nothing, complete = false, index_cache = nothing, + tstops = [], tearing_state = nothing, substitutions = nothing, + namespacing = true, complete = false, index_cache = nothing, discrete_subsystems = nothing, solved_unknowns = nothing, split_idxs = nothing, ignored_connections = nothing, parent = nothing; checks::Union{Bool, Int} = true) @@ -218,6 +222,7 @@ struct ODESystem <: AbstractODESystem check_parameters(ps, iv) check_equations(deqs, iv) check_equations(equations(cevents), iv) + check_subsystems(systems) end if checks == true || (checks & CheckUnits) > 0 u = __get_unit_type(dvs, ps, iv) @@ -228,7 +233,8 @@ struct ODESystem <: AbstractODESystem ctrl_jac, Wfact, Wfact_t, name, description, systems, defaults, guesses, torn_matching, initializesystem, initialization_eqs, schedule, connector_type, preface, cevents, devents, parameter_dependencies, assertions, metadata, - gui_metadata, is_dde, tstops, tearing_state, substitutions, complete, index_cache, + gui_metadata, is_dde, tstops, tearing_state, substitutions, namespacing, + complete, index_cache, discrete_subsystems, solved_unknowns, split_idxs, ignored_connections, parent) end end diff --git a/src/systems/diffeqs/sdesystem.jl b/src/systems/diffeqs/sdesystem.jl index d8ac52dd1a..f51529a559 100644 --- a/src/systems/diffeqs/sdesystem.jl +++ b/src/systems/diffeqs/sdesystem.jl @@ -139,7 +139,11 @@ struct SDESystem <: AbstractODESystem """ gui_metadata::Union{Nothing, GUIMetadata} """ - If a model `sys` is complete, then `sys.x` no longer performs namespacing. + If false, then `sys.x` no longer performs namespacing. + """ + namespacing::Bool + """ + If true, denotes the model will not be modified any further. """ complete::Bool """ @@ -166,7 +170,7 @@ struct SDESystem <: AbstractODESystem guesses, initializesystem, initialization_eqs, connector_type, cevents, devents, parameter_dependencies, assertions = Dict{ BasicSymbolic, Nothing}, - metadata = nothing, gui_metadata = nothing, + metadata = nothing, gui_metadata = nothing, namespacing = true, complete = false, index_cache = nothing, parent = nothing, is_scalar_noise = false, is_dde = false, isscheduled = false; @@ -184,6 +188,7 @@ struct SDESystem <: AbstractODESystem if is_scalar_noise && neqs isa AbstractMatrix throw(ArgumentError("Noise equations ill-formed. Received a matrix of noise equations of size $(size(neqs)), but `is_scalar_noise` was set to `true`. Scalar noise is only compatible with an `AbstractVector` of noise equations.")) end + check_subsystems(systems) end if checks == true || (checks & CheckUnits) > 0 u = __get_unit_type(dvs, ps, iv) @@ -192,8 +197,8 @@ struct SDESystem <: AbstractODESystem new(tag, deqs, neqs, iv, dvs, ps, tspan, var_to_name, ctrls, observed, tgrad, jac, ctrl_jac, Wfact, Wfact_t, name, description, systems, defaults, guesses, initializesystem, initialization_eqs, connector_type, cevents, - devents, parameter_dependencies, assertions, metadata, gui_metadata, complete, - index_cache, parent, is_scalar_noise, is_dde, isscheduled) + devents, parameter_dependencies, assertions, metadata, gui_metadata, namespacing, + complete, index_cache, parent, is_scalar_noise, is_dde, isscheduled) end end @@ -218,7 +223,6 @@ function SDESystem(deqs::AbstractVector{<:Equation}, neqs::AbstractArray, iv, dv assertions = Dict{BasicSymbolic, String}(), metadata = nothing, gui_metadata = nothing, - complete = false, index_cache = nothing, parent = nothing, is_scalar_noise = false, @@ -274,7 +278,7 @@ function SDESystem(deqs::AbstractVector{<:Equation}, neqs::AbstractArray, iv, dv ctrl_jac, Wfact, Wfact_t, name, description, systems, defaults, guesses, initializesystem, initialization_eqs, connector_type, cont_callbacks, disc_callbacks, parameter_dependencies, assertions, metadata, gui_metadata, - complete, index_cache, parent, is_scalar_noise, is_dde; checks = checks) + true, false, index_cache, parent, is_scalar_noise, is_dde; checks = checks) end function SDESystem(sys::ODESystem, neqs; kwargs...) @@ -321,9 +325,8 @@ function SDESystem(eqs::Vector{Equation}, noiseeqs::AbstractArray, iv; kwargs... throw(ArgumentError("Variable $dv in noise equations is not an unknown of the system.")) end algevars = setdiff(allunknowns, diffvars) - return SDESystem(eqs, noiseeqs, iv, Iterators.flatten((diffvars, algevars)), - [ps; collect(noiseps)]; kwargs...) + [collect(ps); collect(noiseps)]; kwargs...) end function SDESystem(eq::Equation, noiseeqs::AbstractArray, args...; kwargs...) diff --git a/src/systems/discrete_system/discrete_system.jl b/src/systems/discrete_system/discrete_system.jl index f51ba638a4..40f01769ee 100644 --- a/src/systems/discrete_system/discrete_system.jl +++ b/src/systems/discrete_system/discrete_system.jl @@ -97,7 +97,11 @@ struct DiscreteSystem <: AbstractDiscreteSystem """ substitutions::Any """ - If a model `sys` is complete, then `sys.x` no longer performs namespacing. + If false, then `sys.x` no longer performs namespacing. + """ + namespacing::Bool + """ + If true, denotes the model will not be modified any further. """ complete::Bool """ @@ -114,7 +118,7 @@ struct DiscreteSystem <: AbstractDiscreteSystem observed, name, description, systems, defaults, guesses, initializesystem, initialization_eqs, preface, connector_type, parameter_dependencies = Equation[], metadata = nothing, gui_metadata = nothing, - tearing_state = nothing, substitutions = nothing, + tearing_state = nothing, substitutions = nothing, namespacing = true, complete = false, index_cache = nothing, parent = nothing, isscheduled = false; checks::Union{Bool, Int} = true) @@ -122,6 +126,7 @@ struct DiscreteSystem <: AbstractDiscreteSystem check_independent_variables([iv]) check_variables(dvs, iv) check_parameters(ps, iv) + check_subsystems(systems) end if checks == true || (checks & CheckUnits) > 0 u = __get_unit_type(dvs, ps, iv) @@ -130,7 +135,8 @@ struct DiscreteSystem <: AbstractDiscreteSystem new(tag, discreteEqs, iv, dvs, ps, tspan, var_to_name, observed, name, description, systems, defaults, guesses, initializesystem, initialization_eqs, preface, connector_type, parameter_dependencies, metadata, gui_metadata, - tearing_state, substitutions, complete, index_cache, parent, isscheduled) + tearing_state, substitutions, namespacing, complete, index_cache, parent, + isscheduled) end end diff --git a/src/systems/discrete_system/implicit_discrete_system.jl b/src/systems/discrete_system/implicit_discrete_system.jl index 514576e927..ebae78384a 100644 --- a/src/systems/discrete_system/implicit_discrete_system.jl +++ b/src/systems/discrete_system/implicit_discrete_system.jl @@ -96,7 +96,11 @@ struct ImplicitDiscreteSystem <: AbstractDiscreteSystem """ substitutions::Any """ - If a model `sys` is complete, then `sys.x` no longer performs namespacing. + If false, then `sys.x` no longer performs namespacing. + """ + namespacing::Bool + """ + If true, denotes the model will not be modified any further. """ complete::Bool """ @@ -113,7 +117,7 @@ struct ImplicitDiscreteSystem <: AbstractDiscreteSystem observed, name, description, systems, defaults, guesses, initializesystem, initialization_eqs, preface, connector_type, parameter_dependencies = Equation[], metadata = nothing, gui_metadata = nothing, - tearing_state = nothing, substitutions = nothing, + tearing_state = nothing, substitutions = nothing, namespacing = true, complete = false, index_cache = nothing, parent = nothing, isscheduled = false; checks::Union{Bool, Int} = true) @@ -121,6 +125,7 @@ struct ImplicitDiscreteSystem <: AbstractDiscreteSystem check_independent_variables([iv]) check_variables(dvs, iv) check_parameters(ps, iv) + check_subsystems(systems) end if checks == true || (checks & CheckUnits) > 0 u = __get_unit_type(dvs, ps, iv) @@ -129,7 +134,8 @@ struct ImplicitDiscreteSystem <: AbstractDiscreteSystem new(tag, discreteEqs, iv, dvs, ps, tspan, var_to_name, observed, name, description, systems, defaults, guesses, initializesystem, initialization_eqs, preface, connector_type, parameter_dependencies, metadata, gui_metadata, - tearing_state, substitutions, complete, index_cache, parent, isscheduled) + tearing_state, substitutions, namespacing, complete, index_cache, parent, + isscheduled) end end diff --git a/src/systems/jumps/jumpsystem.jl b/src/systems/jumps/jumpsystem.jl index 7aa7c6909b..9da32a4305 100644 --- a/src/systems/jumps/jumpsystem.jl +++ b/src/systems/jumps/jumpsystem.jl @@ -116,7 +116,11 @@ struct JumpSystem{U <: ArrayPartition} <: AbstractTimeDependentSystem """ gui_metadata::Union{Nothing, GUIMetadata} """ - If a model `sys` is complete, then `sys.x` no longer performs namespacing. + If false, then `sys.x` no longer performs namespacing. + """ + namespacing::Bool + """ + If true, denotes the model will not be modified any further. """ complete::Bool """ @@ -130,12 +134,13 @@ struct JumpSystem{U <: ArrayPartition} <: AbstractTimeDependentSystem systems, defaults, guesses, initializesystem, initialization_eqs, connector_type, cevents, devents, parameter_dependencies, metadata = nothing, gui_metadata = nothing, - complete = false, index_cache = nothing, isscheduled = false; + namespacing = true, complete = false, index_cache = nothing, isscheduled = false; checks::Union{Bool, Int} = true) where {U <: ArrayPartition} if checks == true || (checks & CheckComponents) > 0 check_independent_variables([iv]) check_variables(unknowns, iv) check_parameters(ps, iv) + check_subsystems(systems) end if checks == true || (checks & CheckUnits) > 0 u = __get_unit_type(unknowns, ps, iv) @@ -145,7 +150,7 @@ struct JumpSystem{U <: ArrayPartition} <: AbstractTimeDependentSystem observed, name, description, systems, defaults, guesses, initializesystem, initialization_eqs, connector_type, cevents, devents, parameter_dependencies, metadata, - gui_metadata, complete, index_cache, isscheduled) + gui_metadata, namespacing, complete, index_cache, isscheduled) end end function JumpSystem(tag, ap, iv, states, ps, var_to_name, args...; kwargs...) diff --git a/src/systems/nonlinear/nonlinearsystem.jl b/src/systems/nonlinear/nonlinearsystem.jl index ea3730fdb6..77215be5eb 100644 --- a/src/systems/nonlinear/nonlinearsystem.jl +++ b/src/systems/nonlinear/nonlinearsystem.jl @@ -95,7 +95,11 @@ struct NonlinearSystem <: AbstractTimeIndependentSystem """ substitutions::Any """ - If a model `sys` is complete, then `sys.x` no longer performs namespacing. + If false, then `sys.x` no longer performs namespacing. + """ + namespacing::Bool + """ + If true, denotes the model will not be modified any further. """ complete::Bool """ @@ -112,17 +116,18 @@ struct NonlinearSystem <: AbstractTimeIndependentSystem tag, eqs, unknowns, ps, var_to_name, observed, jac, name, description, systems, defaults, guesses, initializesystem, initialization_eqs, connector_type, parameter_dependencies = Equation[], metadata = nothing, gui_metadata = nothing, - tearing_state = nothing, substitutions = nothing, + tearing_state = nothing, substitutions = nothing, namespacing = true, complete = false, index_cache = nothing, parent = nothing, isscheduled = false; checks::Union{Bool, Int} = true) if checks == true || (checks & CheckUnits) > 0 u = __get_unit_type(unknowns, ps) check_units(u, eqs) + check_subsystems(systems) end new(tag, eqs, unknowns, ps, var_to_name, observed, jac, name, description, systems, defaults, guesses, initializesystem, initialization_eqs, connector_type, parameter_dependencies, metadata, gui_metadata, tearing_state, - substitutions, complete, index_cache, parent, isscheduled) + substitutions, namespacing, complete, index_cache, parent, isscheduled) end end diff --git a/src/systems/optimization/constraints_system.jl b/src/systems/optimization/constraints_system.jl index 8a71c58043..0f69e6d0b9 100644 --- a/src/systems/optimization/constraints_system.jl +++ b/src/systems/optimization/constraints_system.jl @@ -74,7 +74,11 @@ struct ConstraintsSystem <: AbstractTimeIndependentSystem """ substitutions::Any """ - If a model `sys` is complete, then `sys.x` no longer performs namespacing. + If false, then `sys.x` no longer performs namespacing. + """ + namespacing::Bool + """ + If true, denotes the model will not be modified any further. """ complete::Bool """ @@ -86,17 +90,18 @@ struct ConstraintsSystem <: AbstractTimeIndependentSystem name, description, systems, defaults, connector_type, metadata = nothing, - tearing_state = nothing, substitutions = nothing, + tearing_state = nothing, substitutions = nothing, namespacing = true, complete = false, index_cache = nothing; checks::Union{Bool, Int} = true) if checks == true || (checks & CheckUnits) > 0 u = __get_unit_type(unknowns, ps) check_units(u, constraints) + check_subsystems(systems) end new(tag, constraints, unknowns, ps, var_to_name, observed, jac, name, description, systems, - defaults, - connector_type, metadata, tearing_state, substitutions, complete, index_cache) + defaults, connector_type, metadata, tearing_state, substitutions, + namespacing, complete, index_cache) end end diff --git a/src/systems/optimization/optimizationsystem.jl b/src/systems/optimization/optimizationsystem.jl index bd0f0aa679..e55f4b7871 100644 --- a/src/systems/optimization/optimizationsystem.jl +++ b/src/systems/optimization/optimizationsystem.jl @@ -55,7 +55,11 @@ struct OptimizationSystem <: AbstractOptimizationSystem """ gui_metadata::Union{Nothing, GUIMetadata} """ - If a model `sys` is complete, then `sys.x` no longer performs namespacing. + If false, then `sys.x` no longer performs namespacing. + """ + namespacing::Bool + """ + If true, denotes the model will not be modified any further. """ complete::Bool """ @@ -70,18 +74,19 @@ struct OptimizationSystem <: AbstractOptimizationSystem function OptimizationSystem(tag, op, unknowns, ps, var_to_name, observed, constraints, name, description, systems, defaults, metadata = nothing, - gui_metadata = nothing, complete = false, index_cache = nothing, parent = nothing, - isscheduled = false; + gui_metadata = nothing, namespacing = true, complete = false, + index_cache = nothing, parent = nothing, isscheduled = false; checks::Union{Bool, Int} = true) if checks == true || (checks & CheckUnits) > 0 u = __get_unit_type(unknowns, ps) unwrap(op) isa Symbolic && check_units(u, op) check_units(u, observed) check_units(u, constraints) + check_subsystems(systems) end new(tag, op, unknowns, ps, var_to_name, observed, - constraints, name, description, systems, defaults, metadata, gui_metadata, complete, - index_cache, parent, isscheduled) + constraints, name, description, systems, defaults, metadata, gui_metadata, + namespacing, complete, index_cache, parent, isscheduled) end end diff --git a/src/utils.jl b/src/utils.jl index 55f3d2cec5..726a2ce25f 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -202,6 +202,20 @@ function check_equations(eqs, iv) throw(ArgumentError("Differential w.r.t. variable ($single_iv) other than the independent variable ($iv) are not allowed.")) end end + +""" + $(TYPEDSIGNATURES) + +Assert that the subsystems have the appropriate namespacing behavior. +""" +function check_subsystems(systems) + idxs = findall(!does_namespacing, systems) + if !isempty(idxs) + names = join(" " .* string.(nameof.(systems[idxs])), "\n") + throw(ArgumentError("All subsystems have namespacing enabled. The following subsystems do not perform namespacing:\n$(names)")) + end +end + """ Get all the independent variables with respect to which differentials are taken. """ From f9bbdc5c0d2e85dc838914f3f7dc9c27334a8a32 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 25 Mar 2025 23:07:03 +0530 Subject: [PATCH 1223/2176] docs: document `toggle_namespacing` --- docs/src/basics/AbstractSystem.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/docs/src/basics/AbstractSystem.md b/docs/src/basics/AbstractSystem.md index 3aa43e6d25..d1707f822f 100644 --- a/docs/src/basics/AbstractSystem.md +++ b/docs/src/basics/AbstractSystem.md @@ -148,3 +148,15 @@ The `AbstractSystem` types allow for specifying default values, for example into the value maps, where for any repeats the value maps override the default. In addition, defaults of a higher level in the system override the defaults of a lower level in the system. + +## Namespacing + +By default, unsimplified systems will namespace variables accessed via `getproperty`. +Systems created via `@mtkbuild`, or ones passed through `structural_simplify` or +`complete` will not perform this namespacing. However, all of these processes modify +the system in a variety of ways. To toggle namespacing without transforming any other +property of the system, use `toggle_namespacing`. + +```@docs +toggle_namespacing +``` From e0ae7a9131ef5f32ffae362a5fbc8e027648a620 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 25 Mar 2025 23:10:46 +0530 Subject: [PATCH 1224/2176] test: test new namespacing functionality --- test/analysis_points.jl | 9 +- test/namespacing.jl | 186 ++++++++++++++++++++++++++++++++++++++++ test/runtests.jl | 1 + 3 files changed, 194 insertions(+), 2 deletions(-) create mode 100644 test/namespacing.jl diff --git a/test/analysis_points.jl b/test/analysis_points.jl index 9e8e3fb455..e36afc02f7 100644 --- a/test/analysis_points.jl +++ b/test/analysis_points.jl @@ -47,8 +47,11 @@ end @named sys = ODESystem(Equation[], t; systems = [sys_ap]) ap3 = @test_nowarn sys.hej.plant_input @test nameof(ap3) == Symbol(join(["sys", "hej", "plant_input"], NAMESPACE_SEPARATOR)) - sys = complete(sys) - ap4 = sys.hej.plant_input + csys = complete(sys) + ap4 = csys.hej.plant_input + @test nameof(ap4) == Symbol(join(["hej", "plant_input"], NAMESPACE_SEPARATOR)) + nsys = toggle_namespacing(sys, false) + ap5 = nsys.hej.plant_input @test nameof(ap4) == Symbol(join(["hej", "plant_input"], NAMESPACE_SEPARATOR)) end @@ -61,6 +64,7 @@ ap = AnalysisPoint(:plant_input) eqs = [connect(P.output, C.input), connect(C.output, ap, P.input)] sys = ODESystem(eqs, t, systems = [P, C], name = :hej) @named nested_sys = ODESystem(Equation[], t; systems = [sys]) +nonamespace_sys = toggle_namespacing(nested_sys, false) @testset "simplifies and solves" begin ssys = structural_simplify(sys) @@ -73,6 +77,7 @@ end test_cases = [ ("inner", sys, sys.plant_input), ("nested", nested_sys, nested_sys.hej.plant_input), + ("nonamespace", nonamespace_sys, nonamespace_sys.hej.plant_input), ("inner - Symbol", sys, :plant_input), ("nested - Symbol", nested_sys, nameof(sys.plant_input)) ] diff --git a/test/namespacing.jl b/test/namespacing.jl new file mode 100644 index 0000000000..b6305a1776 --- /dev/null +++ b/test/namespacing.jl @@ -0,0 +1,186 @@ +using ModelingToolkit +using ModelingToolkit: t_nounits as t, D_nounits as D, iscomplete, does_namespacing + +@testset "ODESystem" begin + @variables x(t) + @parameters p + sys = ODESystem(D(x) ~ p * x, t; name = :inner) + @test !iscomplete(sys) + @test does_namespacing(sys) + + csys = complete(sys) + @test iscomplete(csys) + @test !does_namespacing(csys) + + nsys = toggle_namespacing(sys, false) + @test !iscomplete(nsys) + @test !does_namespacing(nsys) + + @test isequal(x, csys.x) + @test isequal(x, nsys.x) + @test !isequal(x, sys.x) + @test isequal(p, csys.p) + @test isequal(p, nsys.p) + @test !isequal(p, sys.p) + + @test_throws ["namespacing", "inner"] ODESystem( + Equation[], t; systems = [nsys], name = :a) +end + +@testset "SDESystem" begin + @variables x(t) + @parameters p + sys = SDESystem([D(x) ~ p * x], [x], t, [x], [p]; name = :inner) + @test !iscomplete(sys) + @test does_namespacing(sys) + + csys = complete(sys) + @test iscomplete(csys) + @test !does_namespacing(csys) + + nsys = toggle_namespacing(sys, false) + @test !iscomplete(nsys) + @test !does_namespacing(nsys) + + @test isequal(x, csys.x) + @test isequal(x, nsys.x) + @test !isequal(x, sys.x) + @test isequal(p, csys.p) + @test isequal(p, nsys.p) + @test !isequal(p, sys.p) + + @test_throws ["namespacing", "inner"] SDESystem( + Equation[], [], t; systems = [nsys], name = :a) +end + +@testset "DiscreteSystem" begin + @variables x(t) + @parameters p + k = ShiftIndex(t) + sys = DiscreteSystem([x(k) ~ p * x(k - 1)], t; name = :inner) + @test !iscomplete(sys) + @test does_namespacing(sys) + + csys = complete(sys) + @test iscomplete(csys) + @test !does_namespacing(csys) + + nsys = toggle_namespacing(sys, false) + @test !iscomplete(nsys) + @test !does_namespacing(nsys) + + @test isequal(x, csys.x) + @test isequal(x, nsys.x) + @test !isequal(x, sys.x) + @test isequal(p, csys.p) + @test isequal(p, nsys.p) + @test !isequal(p, sys.p) + + @test_throws ["namespacing", "inner"] DiscreteSystem( + Equation[], t; systems = [nsys], name = :a) +end + +@testset "ImplicitDiscreteSystem" begin + @variables x(t) + @parameters p + k = ShiftIndex(t) + sys = ImplicitDiscreteSystem([x(k) ~ p + x(k - 1) * x(k)], t; name = :inner) + @test !iscomplete(sys) + @test does_namespacing(sys) + + csys = complete(sys) + @test iscomplete(csys) + @test !does_namespacing(csys) + + nsys = toggle_namespacing(sys, false) + @test !iscomplete(nsys) + @test !does_namespacing(nsys) + + @test isequal(x, csys.x) + @test isequal(x, nsys.x) + @test !isequal(x, sys.x) + @test isequal(p, csys.p) + @test isequal(p, nsys.p) + @test !isequal(p, sys.p) + + @test_throws ["namespacing", "inner"] ImplicitDiscreteSystem( + Equation[], t; systems = [nsys], name = :a) +end + +@testset "NonlinearSystem" begin + @variables x + @parameters p + sys = NonlinearSystem([x ~ p * x^2 + 1]; name = :inner) + @test !iscomplete(sys) + @test does_namespacing(sys) + + csys = complete(sys) + @test iscomplete(csys) + @test !does_namespacing(csys) + + nsys = toggle_namespacing(sys, false) + @test !iscomplete(nsys) + @test !does_namespacing(nsys) + + @test isequal(x, csys.x) + @test isequal(x, nsys.x) + @test !isequal(x, sys.x) + @test isequal(p, csys.p) + @test isequal(p, nsys.p) + @test !isequal(p, sys.p) + + @test_throws ["namespacing", "inner"] NonlinearSystem( + Equation[]; systems = [nsys], name = :a) +end + +@testset "OptimizationSystem" begin + @variables x + @parameters p + sys = OptimizationSystem(p * x^2 + 1; name = :inner) + @test !iscomplete(sys) + @test does_namespacing(sys) + + csys = complete(sys) + @test iscomplete(csys) + @test !does_namespacing(csys) + + nsys = toggle_namespacing(sys, false) + @test !iscomplete(nsys) + @test !does_namespacing(nsys) + + @test isequal(x, csys.x) + @test isequal(x, nsys.x) + @test !isequal(x, sys.x) + @test isequal(p, csys.p) + @test isequal(p, nsys.p) + @test !isequal(p, sys.p) + + @test_throws ["namespacing", "inner"] OptimizationSystem( + []; systems = [nsys], name = :a) +end + +@testset "ConstraintsSystem" begin + @variables x + @parameters p + sys = ConstraintsSystem([x^2 + p ~ 0], [x], [p]; name = :inner) + @test !iscomplete(sys) + @test does_namespacing(sys) + + csys = complete(sys) + @test iscomplete(csys) + @test !does_namespacing(csys) + + nsys = toggle_namespacing(sys, false) + @test !iscomplete(nsys) + @test !does_namespacing(nsys) + + @test isequal(x, csys.x) + @test isequal(x, nsys.x) + @test !isequal(x, sys.x) + @test isequal(p, csys.p) + @test isequal(p, nsys.p) + @test !isequal(p, sys.p) + + @test_throws ["namespacing", "inner"] ConstraintsSystem( + [], [], []; systems = [nsys], name = :a) +end diff --git a/test/runtests.jl b/test/runtests.jl index a03b0f1977..301134219e 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -97,6 +97,7 @@ end @safetestset "Analysis Points Test" include("analysis_points.jl") @safetestset "Causal Variables Connection Test" include("causal_variables_connection.jl") @safetestset "Debugging Test" include("debugging.jl") + @safetestset "Namespacing test" include("namespacing.jl") end end From d62da8fc2c1e8900024a69b8d11100235e0b85e2 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 26 Mar 2025 11:56:33 +0530 Subject: [PATCH 1225/2176] refactor: improve error message for subsystem namespacing --- src/utils.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utils.jl b/src/utils.jl index 726a2ce25f..2a0009b644 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -212,7 +212,7 @@ function check_subsystems(systems) idxs = findall(!does_namespacing, systems) if !isempty(idxs) names = join(" " .* string.(nameof.(systems[idxs])), "\n") - throw(ArgumentError("All subsystems have namespacing enabled. The following subsystems do not perform namespacing:\n$(names)")) + throw(ArgumentError("All subsystems must have namespacing enabled. The following subsystems do not perform namespacing:\n$(names)")) end end From 1e455e7257fe01dff8d074e88f07833975f03e7d Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 26 Mar 2025 11:56:43 +0530 Subject: [PATCH 1226/2176] test: fix usage of completed system as subsystem --- test/parameter_dependencies.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/parameter_dependencies.jl b/test/parameter_dependencies.jl index ad2131f458..31881e1ca8 100644 --- a/test/parameter_dependencies.jl +++ b/test/parameter_dependencies.jl @@ -121,7 +121,7 @@ end @parameters p1=1.0 p2=2.0 @variables x(t) = 0 - @mtkbuild sys1 = ODESystem( + @named sys1 = ODESystem( [D(x) ~ p1 * t + p2], t ) From be6156164b26692d09deab757a8ac0cc1438a5ad Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 28 Mar 2025 14:31:34 +0530 Subject: [PATCH 1227/2176] fix: fix `does_namespacing` fallback to `iscomplete` --- src/systems/abstractsystem.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index 89ff0e510b..fb692b4028 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -605,7 +605,7 @@ function does_namespacing(sys::AbstractSystem) if isdefined(sys, :namespacing) getfield(sys, :namespacing) else - iscomplete(sys) + !iscomplete(sys) end end From b54b56f5c9655aac6c7e5dfe8698b91906e44a7b Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 28 Mar 2025 16:25:31 +0530 Subject: [PATCH 1228/2176] test: fix rootfinding tests --- test/symbolic_events.jl | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test/symbolic_events.jl b/test/symbolic_events.jl index 3fb66081a2..804432408b 100644 --- a/test/symbolic_events.jl +++ b/test/symbolic_events.jl @@ -422,7 +422,7 @@ cond.rf_ip(out, [0, 1], p0, t0) # this should return 0 cond.rf_ip(out, [0, 2], p0, t0) @test out[2] ≈ 1 # signature is u,p,t -sol = solve(prob, Tsit5()) +sol = solve(prob, Tsit5(); abstol = 1e-14, reltol = 1e-14) @test minimum(t -> abs(t - 1), sol.t) < 1e-10 # test that the solver stepped at the first root @test minimum(t -> abs(t - 2), sol.t) < 1e-10 # test that the solver stepped at the second root @@ -430,7 +430,7 @@ sol = solve(prob, Tsit5()) sys = complete(sys) prob = ODEProblem(sys, Pair[], (0.0, 3.0)) @test get_callback(prob) isa ModelingToolkit.DiffEqCallbacks.VectorContinuousCallback -sol = solve(prob, Tsit5()) +sol = solve(prob, Tsit5(); abstol = 1e-14, reltol = 1e-14) @test minimum(t -> abs(t - 1), sol.t) < 1e-10 # test that the solver stepped at the first root @test minimum(t -> abs(t - 2), sol.t) < 1e-10 # test that the solver stepped at the second root @@ -540,8 +540,8 @@ ev = [sin(20pi * t) ~ 0.0] => [vmeasured ~ v] sys = structural_simplify(sys) prob = ODEProblem(sys, zeros(2), (0.0, 5.1)) sol = solve(prob, Tsit5()) -@test all(minimum((0:0.1:5) .- sol.t', dims = 2) .< 0.0001) # test that the solver stepped every 0.1s as dictated by event -@test sol([0.25])[vmeasured][] == sol([0.23])[vmeasured][] # test the hold property +@test all(minimum((0:0.05:5) .- sol.t', dims = 2) .< 0.0001) # test that the solver stepped every 0.05s as dictated by event +@test sol([0.25 - eps()])[vmeasured][] == sol([0.23])[vmeasured][] # test the hold property ## https://github.com/SciML/ModelingToolkit.jl/issues/1528 Dₜ = D From 8335980c42728ccfb66867b5090936c568b16db7 Mon Sep 17 00:00:00 2001 From: George Datseris Date: Sun, 30 Mar 2025 10:25:44 +0100 Subject: [PATCH 1229/2176] Add contribution of multi/nonlocal stability by Attractors.jl We have discussed this on Slack some time ago! --- docs/src/tutorials/attractors.md | 218 +++++++++++++++++++++++++++++++ 1 file changed, 218 insertions(+) create mode 100644 docs/src/tutorials/attractors.md diff --git a/docs/src/tutorials/attractors.md b/docs/src/tutorials/attractors.md new file mode 100644 index 0000000000..56e6026427 --- /dev/null +++ b/docs/src/tutorials/attractors.md @@ -0,0 +1,218 @@ +# [Multi- and Nonlocal- Continuation](@id attractors) + +In the tutorial on [Bifurcation Diagrams](@ref bifurcation_diagrams) we saw how one can create them by integrating ModelingToolkit.jl with BifurcationKit.jl. +This approach is also often called _continuation_ in the broader literature, +because in essence we are "continuing" the location of individual un/stable fixed points or limit cycles in a dynamical system across a parameter axis. + +Recently, an alternative continuation framework was proposed that takes a fundamentally different approach to continuation that is particularly suitable for complex systems. This framework is implemented in [Attractors.jl](https://juliadynamics.github.io/DynamicalSystemsDocs.jl/attractors/stable/) as part of the DynamicalSystems.jl software library. +This new continuation is called _global_ continuation, while the one of BifurcationKit.jl is called _local_ continuation. + +Instead of continuing an individual fixed point or limit cycle, the global continuation finds all attractors of the dynamical system and continues all of them, in parallel, in a single continuation. It distinguishes and labels automatically the different attractors. +Hence "multi-" for multiple attractors. +Another key difference is that instead of estimating the local (or linear, or Jacobian) stability of the attractors, it estimates various measures of _nonlocal_ stability (e.g, related with the size of the basins of attraction, or the size of a perturbation that would make the dynamical system state converge to an alternative attractor). +Hence the "nonlocal-" component. +More differences and pros & cons are discussed in the documentation of Attractors.jl. + +!!! note "Attractors and basins" + This tutorial assumes that you have some familiarity with dynamical systems, + specifically what are attractors and basins of attraction. If you don't have + this yet, we recommend Chapter 1 of the textbook + [Nonlinear Dynamics](https://link.springer.com/book/10.1007/978-3-030-91032-7). + +## Creating the `DynamicalSystem` via MTK + +Let's showcase this framework by modelling a chaotic bistable dynamical system that we define via ModelingToolkit.jl, which will the be casted into a `DynamicalSystem` type for the DynamicalSystems.jl library. The equations of our system are + +```@example Attractors +using ModelingToolkit +using ModelingToolkit: t_nounits as t, D_nounits as D + +@variables x(t) = -4.0 y(t) = 5.0 z(t) = 0.0 +@parameters a = 5.0 b = 0.1 + +eqs = [ + D(x) ~ y - x, + D(y) ~ - x*z + b*abs(z), + D(z) ~ x*y - a, +] +``` + +Because our dynamical system is super simple, we will directly make an `ODESystem` and cast it in an `ODEProblem` as in the [`ODESystems` tutorial](@ref programmatically). Since all state variables and parameters have a default value we can immediately write + +```@example Attractors +@named modlorenz = ODESystem(eqs, t) +ssys = structural_simplify(modlorenz) +# The timespan given to the problem is irrelevant for DynamicalSystems.jl +prob = ODEProblem(ssys, [], (0.0, 1.0), []) +``` + +This `prob` can be turned to a dynamical system as simply as + +```@example Attractors +using Attractors # or `DynamicalSystems` +ds = CoupledODEs(prob) +``` + +DynamicalSystems.jl integrates fully with ModelingToolkit.jl out of the box and understands whether a given problem has been created via ModelingToolkit.jl. For example you can use the symbolic variables, or their `Symbol` representations, to access a system state or parameter + +```@example Attractors +observe_state(ds, x) +``` + +```@example Attractors +current_parameter(ds, :a) # or `a` directly +``` + +## Finding all attractors in the state space + +Attractors.jl provides an extensive interface for finding all (within a state space region and numerical accuracy) attractors of a dynamical system. +This interface is structured around the type `AttractorMapper` and is discussed in the Attractors.jl documentation in detail. Here we will briefly mention one of the possible approaches, the recurrences-based algorithm. It finds attractors by finding locations in the state space where the trajectory returns again and again. + +To use this technique, we first need to create a tessellation of the state space + +```@example Attractors +grid = ( + range(-15.0, 15.0; length = 150), # x + range(-20.0, 20.0; length = 150), # y + range(-20.0, 20.0; length = 150), # z +) +``` + +which we then give as input to the `AttractorsViaRecurrences` mapper along with the dynamical system + +```@example Attractors +mapper = AttractorsViaRecurrences(ds, grid; + consecutive_recurrences = 1000, + consecutive_lost_steps = 100, +) +``` +to learn about the metaparameters of the algorithm visit the documentation of Attractors.jl. + +This `mapper` object is incredibly powerful! It can be used to map initial conditions to attractor they converge to, while ensuring that initial conditions that converge to the same attractor are given the same label. +For example, if we use the `mapper` as a function and give it an initial condition we get + +```@example Attractors +mapper([-4.0, 5, 0]) +``` + +```@example Attractors +mapper([4.0, 2, 0]) +``` + +```@example Attractors +mapper([1.0, 3, 2]) +``` + +The numbers returned are simply the unique identifiers of the attractors the initial conditions converged to. + +DynamicalSystems.jl library is the only dynamical systems software (in any language) that provides such an infrastructure for mapping initial conditions of any arbitrary dynamical system to its unique attractors. And this is only the tip of this iceberg! The rest of the functionality of Attractors.jl is all full of brand new cutting edge progress in dynamical systems research. + +The found attractors are stored in the mapper internally, to obtain them we +use the function + +```@example Attractors +attractors = extract_attractors(mapper) +``` + +This is a dictionary that maps attractor IDs to the attractor sets themselves. +`StateSpaceSet` is a wrapper of a vector of points and behaves exactly like a vector of points. We can plot them easily like + +```@example Attractors +using CairoMakie +fig = Figure() +ax = Axis(fig[1,1]) +colors = ["#7143E0", "#191E44"] +for (id, A) in attractors + scatter!(ax, A[:, [1, 3]]; color = colors[id]) +end +fig +``` + +## Basins of attraction + +Estimating the basins of attraction of these attractor is a matter of a couple lines of code. First we define the state space are to estimate the basins for. Here we can re-use the `grid` we defined above. Then we only have to call + +```julia +basins = basins_of_attraction(mapper, grid) +``` + +We won't run this in this tutorial because it is a length computation (150×150×150). +We will however estimate a slice of the 3D basins of attraction. +DynamicalSystems.jl allows for a rather straightforward setting of initial conditions: +```@example Attractors +ics = [Dict(:x => x, :y => 0, :z=>z) for x in grid[1] for z in grid[3]] +``` + +now we can estimate the basins of attraction on a slice on the x-z grid + +```@example Attractors +fs, labels = basins_fractions(mapper, ics) +labels = reshape(labels, (length(grid[1]), length(grid[3]))) +``` + +and visualize them + +```@example Attractors +heatmap(grid[1], grid[3], labels; colormap = colors) +``` + +## Global continuation + +We've already outlined the principles of the global continuation, so let's just do it here! +We first have to define a global continuation algorithm, which for this tutorial, +it is just a wrapper of the existing `mapper` + +```@example Attractors +ascm = AttractorSeedContinueMatch(mapper); +``` + +we need two more ingredients to perform the global continuation. +One is a sampler of initial conditions in the state space. +Here we'll uniformly sample initial conditions within this grid we have already defined + +```@example Attractors +sampler, = statespace_sampler(grid); +``` + +the last ingredient is what parameter(s) to perform the continuation over. +In contrast to local continuation, where we can only specify a parameter range, in global continuation one can specify an exact parameter curve to continue over. +This curve can span any-dimensional parameter space, in contrast to the 1D or 2D parameter spaces supported in local continuation. +Here we will use the curve + +```@example Attractors +params(θ) = [:a => 5 + 0.5cos(θ), :b => 0.1 + 0.01sin(θ)] +θs = range(0, 2π; length = 101) +pcurve = params.(θs) +``` +which makes an ellipsis over the parameter space. + +We put these three ingredients together to call the global continuation + +```@example Attractors +fractions_cont, attractors_cont = global_continuation(ascm, pcurve, sampler); +``` + +The output of the continuation is how the attractors and their basins fractions change over this parameter curve. We can visualize this directly using a convenience function + +```@example Attractors +fig = plot_basins_attractors_curves( + fractions_cont, attractors_cont, A -> minimum(A[:, 1]), θs, +) +``` + +The top panel shows the relative basins of attractions of the attractors and the bottom panel shows their minimum x-position. The colors correspond to unique attractors. Perhaps making a video is easier to understand: + +```@example Attractors +animate_attractors_continuation( + ds, attractors_cont, fractions_cont, pcurve; + savename = "curvecont.mp4" +); +``` + +```@raw html + +``` + +To learn more about this global continuation and its various options, and more details about how it compares with local continuation, visit the documentation of [Attractors.jl](https://juliadynamics.github.io/DynamicalSystemsDocs.jl/attractors/stable/). From 007ffd144908b257439caf0db04bc29d98bb7034 Mon Sep 17 00:00:00 2001 From: George Datseris Date: Sun, 30 Mar 2025 10:28:40 +0100 Subject: [PATCH 1230/2176] Add new page to pages.jl --- docs/pages.jl | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/pages.jl b/docs/pages.jl index 70a10c436f..97de7d8f76 100644 --- a/docs/pages.jl +++ b/docs/pages.jl @@ -12,6 +12,7 @@ pages = [ "tutorials/parameter_identifiability.md", "tutorials/change_independent_variable.md", "tutorials/bifurcation_diagram_computation.md", + "tutorials/attractors.md", "tutorials/SampledData.md", "tutorials/domain_connections.md", "tutorials/callable_params.md", From b864c4ba9de666f1083779c3e595d71c394d6991 Mon Sep 17 00:00:00 2001 From: George Datseris Date: Sun, 30 Mar 2025 10:29:46 +0100 Subject: [PATCH 1231/2176] Add CairoMakie as a docs dependency --- docs/Project.toml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/docs/Project.toml b/docs/Project.toml index e1df906ca5..0dcaf23262 100644 --- a/docs/Project.toml +++ b/docs/Project.toml @@ -1,6 +1,7 @@ [deps] BenchmarkTools = "6e4b80f9-dd63-53aa-95a3-0cdb28fa8baf" BifurcationKit = "0f109fa4-8a5d-4b75-95aa-f515264e7665" +CairoMakie = "13f3f980-e62b-5c42-98c6-ff1f3baf88f0" ControlSystemsBase = "aaaaaaaa-a6ca-5380-bf3e-84a91bcd477e" DataInterpolations = "82cc6244-b520-54b8-b5a6-8a565e85f1d0" Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f" @@ -29,6 +30,7 @@ Unitful = "1986cc42-f94f-5a68-af5c-568840ba703d" [compat] BenchmarkTools = "1.3" BifurcationKit = "0.4" +CairoMakie = "0.13" DataInterpolations = "6.5, 8" Distributions = "0.25" Documenter = "1" From 83c3a45e386ae79997329b1735071009a0dc62ab Mon Sep 17 00:00:00 2001 From: George Datseris Date: Sun, 30 Mar 2025 10:32:40 +0100 Subject: [PATCH 1232/2176] Update docs/src/tutorials/attractors.md --- docs/src/tutorials/attractors.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/docs/src/tutorials/attractors.md b/docs/src/tutorials/attractors.md index 56e6026427..ac4a643dbe 100644 --- a/docs/src/tutorials/attractors.md +++ b/docs/src/tutorials/attractors.md @@ -130,7 +130,9 @@ fig ## Basins of attraction -Estimating the basins of attraction of these attractor is a matter of a couple lines of code. First we define the state space are to estimate the basins for. Here we can re-use the `grid` we defined above. Then we only have to call +Estimating the basins of attraction of these attractors is a matter of a couple lines of code. +First we define the state space are to estimate the basins for. +Here we can re-use the `grid` we defined above. Then we only have to call ```julia basins = basins_of_attraction(mapper, grid) From 92344eccee9df29cf5840008f85b541d92d3e02e Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 1 Apr 2025 12:11:50 +0530 Subject: [PATCH 1233/2176] test: make homotopy continuation test more robust --- test/extensions/homotopy_continuation.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/extensions/homotopy_continuation.jl b/test/extensions/homotopy_continuation.jl index ed630ea8e3..ee13b89992 100644 --- a/test/extensions/homotopy_continuation.jl +++ b/test/extensions/homotopy_continuation.jl @@ -151,7 +151,7 @@ end @variables x=0.25 y=0.125 a = sin(x^2 - 4x + 1) b = cos(3log(y) + 4) - @mtkbuild sys = NonlinearSystem([(a^2 - 4a * b + 4b^2) / (a - 0.25) ~ 0 + @mtkbuild sys = NonlinearSystem([(a^2 - 5a * b + 6b^2) / (a - 0.25) ~ 0 (a^2 - 0.75a + 0.125) ~ 0]) prob = HomotopyContinuationProblem(sys, []) @test prob[x] ≈ 0.25 @@ -159,7 +159,7 @@ end sol = solve(prob, allrootsalg).u[1] @test SciMLBase.successful_retcode(sol) @test sol[a]≈0.5 atol=1e-6 - @test sol[b]≈0.25 atol=1e-6 + @test isapprox(sol[b], 0.25; atol = 1e-6) || isapprox(sol[b], 0.5 / 3; atol = 1e-6) end @testset "Rational functions" begin From 6a5a885eaeae8078eee120b9098dac20322c8d89 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 1 Apr 2025 12:53:45 +0530 Subject: [PATCH 1234/2176] test: make homotopy continuation test more robust --- test/extensions/homotopy_continuation.jl | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/extensions/homotopy_continuation.jl b/test/extensions/homotopy_continuation.jl index ee13b89992..65f7d765a4 100644 --- a/test/extensions/homotopy_continuation.jl +++ b/test/extensions/homotopy_continuation.jl @@ -164,9 +164,9 @@ end @testset "Rational functions" begin @variables x=2.0 y=2.0 - @parameters n = 4 + @parameters n = 5 @mtkbuild sys = NonlinearSystem([ - 0 ~ (x^2 - n * x + n) * (x - 1) / (x - 2) / (x - 3) + 0 ~ (x^2 - n * x + 6) * (x - 1) / (x - 2) / (x - 3) ]) prob = HomotopyContinuationProblem(sys, []) sol = solve_allroots_closest(prob) @@ -184,7 +184,7 @@ end 0 ~ ((y - 3) / (y - 4)) * (n / (y - 5)) + ((x - 1.5) / (x - 5.5))^2 ], [x, y], - [n]) + [n]; defaults = [n => 4]) sys = complete(sys) prob = HomotopyContinuationProblem(sys, []) sol = solve(prob, singlerootalg) From 9ef40dc3cce0f2d26555b6478f04b45b90bc5b11 Mon Sep 17 00:00:00 2001 From: Christopher Rackauckas Date: Tue, 1 Apr 2025 03:44:33 -0400 Subject: [PATCH 1235/2176] Delete .github/workflows/Invalidations.yml --- .github/workflows/Invalidations.yml | 17 ----------------- 1 file changed, 17 deletions(-) delete mode 100644 .github/workflows/Invalidations.yml diff --git a/.github/workflows/Invalidations.yml b/.github/workflows/Invalidations.yml deleted file mode 100644 index 1e5662eb28..0000000000 --- a/.github/workflows/Invalidations.yml +++ /dev/null @@ -1,17 +0,0 @@ -name: "Invalidations" - -on: - pull_request: - paths-ignore: - - 'docs/**' - -concurrency: - # Skip intermediate builds: always. - # Cancel intermediate builds: always. - group: ${{ github.workflow }}-${{ github.ref }} - cancel-in-progress: true - -jobs: - evaluate-invalidations: - name: "Evaluate Invalidations" - uses: "SciML/.github/.github/workflows/invalidations.yml@v1" From a46788281f45c9d1f7b14b4cdb91f2547c766fae Mon Sep 17 00:00:00 2001 From: George Datseris Date: Tue, 1 Apr 2025 08:50:21 +0100 Subject: [PATCH 1236/2176] add Attractors.jl to Project.toml --- docs/Project.toml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/docs/Project.toml b/docs/Project.toml index 0dcaf23262..2f7a3c1317 100644 --- a/docs/Project.toml +++ b/docs/Project.toml @@ -1,4 +1,5 @@ [deps] +Attractors = "f3fd9213-ca85-4dba-9dfd-7fc91308fec7" BenchmarkTools = "6e4b80f9-dd63-53aa-95a3-0cdb28fa8baf" BifurcationKit = "0f109fa4-8a5d-4b75-95aa-f515264e7665" CairoMakie = "13f3f980-e62b-5c42-98c6-ff1f3baf88f0" @@ -28,6 +29,7 @@ Symbolics = "0c5d862f-8b57-4792-8d23-62f2024744c7" Unitful = "1986cc42-f94f-5a68-af5c-568840ba703d" [compat] +Attractors = "1.24" BenchmarkTools = "1.3" BifurcationKit = "0.4" CairoMakie = "0.13" From 9781a5ee7d71dfd00312695b3af138ba4fc8512f Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 1 Apr 2025 15:24:53 +0530 Subject: [PATCH 1237/2176] build: bump minor version --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index a9290b037a..6e01126270 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "ModelingToolkit" uuid = "961ee093-0014-501f-94e3-6117800e7a78" authors = ["Yingbo Ma ", "Chris Rackauckas and contributors"] -version = "9.69.0" +version = "9.70.0" [deps] ADTypes = "47edcb42-4c32-4615-8424-f2b9edc5f35b" From cb9475156066c3396e8fc0d5eb89b357d2dd083f Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 1 Apr 2025 16:32:49 +0530 Subject: [PATCH 1238/2176] refactor: don't mark array variables as irreducible in `TearingState` --- src/systems/systemstructure.jl | 1 - 1 file changed, 1 deletion(-) diff --git a/src/systems/systemstructure.jl b/src/systems/systemstructure.jl index c0c4a5ff4d..d27e5c93a1 100644 --- a/src/systems/systemstructure.jl +++ b/src/systems/systemstructure.jl @@ -299,7 +299,6 @@ function TearingState(sys; quick_cancel = false, check = true) end v = scalarize(v) if v isa AbstractArray - v = setmetadata.(v, VariableIrreducible, true) append!(varsvec, v) else push!(varsvec, v) From bd95c856e18830835ff259ae996e463ae48dab02 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 1 Apr 2025 15:20:22 +0530 Subject: [PATCH 1239/2176] feat: add latexify recipe for `AnalysisPoint` --- src/systems/analysis_points.jl | 13 +++++++++++++ test/latexify.jl | 11 +++++++++++ test/latexify/50.tex | 19 +++++++++++++++++++ 3 files changed, 43 insertions(+) create mode 100644 test/latexify/50.tex diff --git a/src/systems/analysis_points.jl b/src/systems/analysis_points.jl index 104c26cbfe..0d1a2830cf 100644 --- a/src/systems/analysis_points.jl +++ b/src/systems/analysis_points.jl @@ -140,6 +140,19 @@ function Base.show(io::IO, ::MIME"text/plain", ap::AnalysisPoint) end end +@latexrecipe function f(ap::AnalysisPoint) + index --> :subscript + snakecase --> true + ap.input === nothing && return 0 + outs = Expr(:vect) + append!(outs.args, ap_var.(ap.outputs)) + return Expr(:call, :AnalysisPoint, ap_var(ap.input), ap.name, outs) +end + +function Base.show(io::IO, ::MIME"text/latex", ap::AnalysisPoint) + print(io, latexify(ap)) +end + """ $(TYPEDSIGNATURES) diff --git a/test/latexify.jl b/test/latexify.jl index eabaeacdd8..105a17ca6e 100644 --- a/test/latexify.jl +++ b/test/latexify.jl @@ -3,6 +3,7 @@ using Latexify using ModelingToolkit using ReferenceTests using ModelingToolkit: t_nounits as t, D_nounits as D +using ModelingToolkitStandardLibrary.Blocks ### Tips for generating latex tests: ### Latexify has an unexported macro: @@ -47,3 +48,13 @@ eqs = [D(u[1]) ~ p[3] * (u[2] - u[1]), eqs = [D(x) ~ (1 + cos(t)) / (1 + 2 * x)] @test_reference "latexify/40.tex" latexify(eqs) + +@named P = FirstOrder(k = 1, T = 1) +@named C = Gain(; k = -1) + +ap = AnalysisPoint(:plant_input) +eqs = [connect(P.output, C.input) + connect(C.output, ap, P.input)] +sys_ap = ODESystem(eqs, t, systems = [P, C], name = :hej) + +@test_reference "latexify/50.tex" latexify(sys_ap) diff --git a/test/latexify/50.tex b/test/latexify/50.tex new file mode 100644 index 0000000000..b1e6a1fda4 --- /dev/null +++ b/test/latexify/50.tex @@ -0,0 +1,19 @@ +\begin{equation} +\left[ +\begin{array}{c} +\mathrm{connect}\left( P_{+}output, C_{+}input \right) \\ +AnalysisPoint\left( \mathtt{C.output.u}\left( t \right), plant\_input, \left[ +\begin{array}{c} +\mathtt{P.input.u}\left( t \right) \\ +\end{array} +\right] \right) \\ +\mathtt{P.u}\left( t \right) = \mathtt{P.input.u}\left( t \right) \\ +\mathtt{P.y}\left( t \right) = \mathtt{P.output.u}\left( t \right) \\ +\mathtt{P.y}\left( t \right) = \mathtt{P.x}\left( t \right) \\ +\frac{\mathrm{d} \mathtt{P.x}\left( t \right)}{\mathrm{d}t} = \frac{ - \mathtt{P.x}\left( t \right) + \mathtt{P.k} \mathtt{P.u}\left( t \right)}{\mathtt{P.T}} \\ +\mathtt{C.u}\left( t \right) = \mathtt{C.input.u}\left( t \right) \\ +\mathtt{C.y}\left( t \right) = \mathtt{C.output.u}\left( t \right) \\ +\mathtt{C.y}\left( t \right) = \mathtt{C.k} \mathtt{C.u}\left( t \right) \\ +\end{array} +\right] +\end{equation} From 28447a161f7490afc3687591358a0e46aa16e380 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 1 Apr 2025 23:37:12 +0530 Subject: [PATCH 1240/2176] build: bump Symbolics compat --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 6e01126270..5b4f97e815 100644 --- a/Project.toml +++ b/Project.toml @@ -151,7 +151,7 @@ StochasticDelayDiffEq = "1.8.1" StochasticDiffEq = "6.72.1" SymbolicIndexingInterface = "0.3.37" SymbolicUtils = "3.14" -Symbolics = "6.29.2" +Symbolics = "6.36" URIs = "1" UnPack = "0.1, 1.0" Unitful = "1.1" From b96c611e79db24e6635dbd1c9f80e136ae89b2e5 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 2 Apr 2025 11:46:36 +0530 Subject: [PATCH 1241/2176] refactor: format --- docs/src/tutorials/attractors.md | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/docs/src/tutorials/attractors.md b/docs/src/tutorials/attractors.md index ac4a643dbe..317384b01a 100644 --- a/docs/src/tutorials/attractors.md +++ b/docs/src/tutorials/attractors.md @@ -14,6 +14,7 @@ Hence the "nonlocal-" component. More differences and pros & cons are discussed in the documentation of Attractors.jl. !!! note "Attractors and basins" + This tutorial assumes that you have some familiarity with dynamical systems, specifically what are attractors and basins of attraction. If you don't have this yet, we recommend Chapter 1 of the textbook @@ -27,13 +28,13 @@ Let's showcase this framework by modelling a chaotic bistable dynamical system t using ModelingToolkit using ModelingToolkit: t_nounits as t, D_nounits as D -@variables x(t) = -4.0 y(t) = 5.0 z(t) = 0.0 -@parameters a = 5.0 b = 0.1 +@variables x(t)=-4.0 y(t)=5.0 z(t)=0.0 +@parameters a=5.0 b=0.1 eqs = [ D(x) ~ y - x, - D(y) ~ - x*z + b*abs(z), - D(z) ~ x*y - a, + D(y) ~ -x * z + b * abs(z), + D(z) ~ x * y - a ] ``` @@ -74,7 +75,7 @@ To use this technique, we first need to create a tessellation of the state space grid = ( range(-15.0, 15.0; length = 150), # x range(-20.0, 20.0; length = 150), # y - range(-20.0, 20.0; length = 150), # z + range(-20.0, 20.0; length = 150) # z ) ``` @@ -83,9 +84,10 @@ which we then give as input to the `AttractorsViaRecurrences` mapper along with ```@example Attractors mapper = AttractorsViaRecurrences(ds, grid; consecutive_recurrences = 1000, - consecutive_lost_steps = 100, + consecutive_lost_steps = 100 ) ``` + to learn about the metaparameters of the algorithm visit the documentation of Attractors.jl. This `mapper` object is incredibly powerful! It can be used to map initial conditions to attractor they converge to, while ensuring that initial conditions that converge to the same attractor are given the same label. @@ -120,7 +122,7 @@ This is a dictionary that maps attractor IDs to the attractor sets themselves. ```@example Attractors using CairoMakie fig = Figure() -ax = Axis(fig[1,1]) +ax = Axis(fig[1, 1]) colors = ["#7143E0", "#191E44"] for (id, A) in attractors scatter!(ax, A[:, [1, 3]]; color = colors[id]) @@ -130,8 +132,8 @@ fig ## Basins of attraction -Estimating the basins of attraction of these attractors is a matter of a couple lines of code. -First we define the state space are to estimate the basins for. +Estimating the basins of attraction of these attractors is a matter of a couple lines of code. +First we define the state space are to estimate the basins for. Here we can re-use the `grid` we defined above. Then we only have to call ```julia @@ -141,8 +143,9 @@ basins = basins_of_attraction(mapper, grid) We won't run this in this tutorial because it is a length computation (150×150×150). We will however estimate a slice of the 3D basins of attraction. DynamicalSystems.jl allows for a rather straightforward setting of initial conditions: + ```@example Attractors -ics = [Dict(:x => x, :y => 0, :z=>z) for x in grid[1] for z in grid[3]] +ics = [Dict(:x => x, :y => 0, :z => z) for x in grid[1] for z in grid[3]] ``` now we can estimate the basins of attraction on a slice on the x-z grid @@ -186,6 +189,7 @@ params(θ) = [:a => 5 + 0.5cos(θ), :b => 0.1 + 0.01sin(θ)] θs = range(0, 2π; length = 101) pcurve = params.(θs) ``` + which makes an ellipsis over the parameter space. We put these three ingredients together to call the global continuation @@ -198,7 +202,7 @@ The output of the continuation is how the attractors and their basins fractions ```@example Attractors fig = plot_basins_attractors_curves( - fractions_cont, attractors_cont, A -> minimum(A[:, 1]), θs, + fractions_cont, attractors_cont, A -> minimum(A[:, 1]), θs ) ``` From 908c32b4a72f4cc43868730eeefa0c2a0e192315 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 26 Mar 2025 16:25:03 +0530 Subject: [PATCH 1242/2176] feat: add `substitute_component` --- src/ModelingToolkit.jl | 2 +- src/systems/abstractsystem.jl | 177 ++++++++++++++++++++++ test/runtests.jl | 1 + test/substitute_component.jl | 269 ++++++++++++++++++++++++++++++++++ 4 files changed, 448 insertions(+), 1 deletion(-) create mode 100644 test/substitute_component.jl diff --git a/src/ModelingToolkit.jl b/src/ModelingToolkit.jl index b53d3ec098..99d38c7b33 100644 --- a/src/ModelingToolkit.jl +++ b/src/ModelingToolkit.jl @@ -291,7 +291,7 @@ export isinput, isoutput, getbounds, hasbounds, getguess, hasguess, isdisturbanc hasunit, getunit, hasconnect, getconnect, hasmisc, getmisc, state_priority export ode_order_lowering, dae_order_lowering, liouville_transform, - change_independent_variable + change_independent_variable, substitute_component export PDESystem export Differential, expand_derivatives, @derivatives export Equation, ConstrainedEquation diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index fb692b4028..0372858b3a 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -3181,3 +3181,180 @@ has_diff_eqs(osys21) # returns `false`. ``` """ has_diff_eqs(sys::AbstractSystem) = any(is_diff_equation, get_eqs(sys)) + +""" + $(TYPEDSIGNATURES) + +Validate the rules for replacement of subcomponents as defined in `substitute_component`. +""" +function validate_replacement_rule( + rule::Pair{T, T}; namespace = []) where {T <: AbstractSystem} + lhs, rhs = rule + + iscomplete(lhs) && throw(ArgumentError("LHS of replacement rule cannot be completed.")) + iscomplete(rhs) && throw(ArgumentError("RHS of replacement rule cannot be completed.")) + + rhs_h = namespace_hierarchy(nameof(rhs)) + if length(rhs_h) != 1 + throw(ArgumentError("RHS of replacement rule must not be namespaced.")) + end + rhs_h[1] == namespace_hierarchy(nameof(lhs))[end] || + throw(ArgumentError("LHS and RHS must have the same name.")) + + if !isequal(get_iv(lhs), get_iv(rhs)) + throw(ArgumentError("LHS and RHS of replacement rule must have the same independent variable.")) + end + + lhs_u = get_unknowns(lhs) + rhs_u = Dict(get_unknowns(rhs) .=> nothing) + for u in lhs_u + if !haskey(rhs_u, u) + if isempty(namespace) + throw(ArgumentError("RHS of replacement rule does not contain unknown $u.")) + else + throw(ArgumentError("Subsystem $(join([namespace; nameof(lhs)], NAMESPACE_SEPARATOR)) of RHS does not contain unknown $u.")) + end + end + ru = getkey(rhs_u, u, nothing) + name = join([namespace; nameof(lhs); (hasname(u) ? getname(u) : Symbol(u))], + NAMESPACE_SEPARATOR) + l_connect = something(getconnect(u), Equality) + r_connect = something(getconnect(ru), Equality) + if l_connect != r_connect + throw(ArgumentError("Variable $(name) should have connection metadata $(l_connect),")) + end + + l_input = isinput(u) + r_input = isinput(ru) + if l_input != r_input + throw(ArgumentError("Variable $name has differing causality. Marked as `input = $l_input` in LHS and `input = $r_input` in RHS.")) + end + l_output = isoutput(u) + r_output = isoutput(ru) + if l_output != r_output + throw(ArgumentError("Variable $name has differing causality. Marked as `output = $l_output` in LHS and `output = $r_output` in RHS.")) + end + end + + lhs_p = get_ps(lhs) + rhs_p = Set(get_ps(rhs)) + for p in lhs_p + if !(p in rhs_p) + if isempty(namespace) + throw(ArgumentError("RHS of replacement rule does not contain parameter $p")) + else + throw(ArgumentError("Subsystem $(join([namespace; nameof(lhs)], NAMESPACE_SEPARATOR)) of RHS does not contain parameter $p.")) + end + end + end + + lhs_s = get_systems(lhs) + rhs_s = Dict(nameof(s) => s for s in get_systems(rhs)) + + for s in lhs_s + if haskey(rhs_s, nameof(s)) + rs = rhs_s[nameof(s)] + if isconnector(s) + name = join([namespace; nameof(lhs); nameof(s)], NAMESPACE_SEPARATOR) + if !isconnector(rs) + throw(ArgumentError("Subsystem $name of RHS is not a connector.")) + end + if (lct = get_connector_type(s)) !== (rct = get_connector_type(rs)) + throw(ArgumentError("Subsystem $name of RHS has connection type $rct but LHS has $lct.")) + end + end + validate_replacement_rule(s => rs; namespace = [namespace; nameof(rhs)]) + continue + end + name1 = join([namespace; nameof(lhs)], NAMESPACE_SEPARATOR) + throw(ArgumentError("$name1 of replacement rule does not contain subsystem $(nameof(s)).")) + end +end + +""" + $(TYPEDSIGNATURES) + +Chain `getproperty` calls on `root` in the order given in `hierarchy`. + +# Keyword Arguments + +- `skip_namespace_first`: Whether to avoid namespacing in the first `getproperty` call. +""" +function recursive_getproperty( + root::AbstractSystem, hierarchy::Vector{Symbol}; skip_namespace_first = true) + cur = root + for (i, name) in enumerate(hierarchy) + cur = getproperty(cur, name; namespace = i > 1 || !skip_namespace_first) + end + return cur +end + +""" + $(TYPEDSIGNATURES) + +Recursively descend through `sys`, finding all connection equations and re-creating them +using the names of the involved variables/systems and finding the required variables/ +systems in the hierarchy. +""" +function recreate_connections(sys::AbstractSystem) + eqs = map(get_eqs(sys)) do eq + eq.lhs isa Union{Connection, AnalysisPoint} || return eq + if eq.lhs isa Connection + oldargs = get_systems(eq.rhs) + else + ap::AnalysisPoint = eq.rhs + oldargs = [ap.input; ap.outputs] + end + newargs = map(get_systems(eq.rhs)) do arg + name = arg isa AbstractSystem ? nameof(arg) : getname(arg) + hierarchy = namespace_hierarchy(name) + return recursive_getproperty(sys, hierarchy) + end + if eq.lhs isa Connection + return eq.lhs ~ Connection(newargs) + else + return eq.lhs ~ AnalysisPoint(newargs[1], eq.rhs.name, newargs[2:end]) + end + end + @set! sys.eqs = eqs + @set! sys.systems = map(recreate_connections, get_systems(sys)) + return sys +end + +""" + $(TYPEDSIGNATURES) + +Given a hierarchical system `sys` and a rule `lhs => rhs`, replace the subsystem `lhs` in +`sys` by `rhs`. The `lhs` must be the namespaced version of a subsystem of `sys` (e.g. +obtained via `sys.inner.component`). The `rhs` must be valid as per the following +conditions: + +1. `rhs` must not be namespaced. +2. The name of `rhs` must be the same as the unnamespaced name of `lhs`. +3. Neither one of `lhs` or `rhs` can be marked as complete. +4. Both `lhs` and `rhs` must share the same independent variable. +5. `rhs` must contain at least all of the unknowns and parameters present in + `lhs`. +6. Corresponding unknowns in `rhs` must share the same connection and causality + (input/output) metadata as their counterparts in `lhs`. +7. For each subsystem of `lhs`, there must be an identically named subsystem of `rhs`. + These two corresponding subsystems must satisfy conditions 3, 4, 5, 6, 7. If the + subsystem of `lhs` is a connector, the corresponding subsystem of `rhs` must also + be a connector of the same type. + +`sys` also cannot be marked as complete. +""" +function substitute_component(sys::T, rule::Pair{T, T}) where {T <: AbstractSystem} + iscomplete(sys) && + throw(ArgumentError("Cannot replace subsystems of completed systems")) + + validate_replacement_rule(rule) + + lhs, rhs = rule + hierarchy = namespace_hierarchy(nameof(lhs)) + + newsys, _ = modify_nested_subsystem(sys, hierarchy) do inner + return rhs, () + end + return recreate_connections(newsys) +end diff --git a/test/runtests.jl b/test/runtests.jl index 301134219e..04d99cc8b9 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -98,6 +98,7 @@ end @safetestset "Causal Variables Connection Test" include("causal_variables_connection.jl") @safetestset "Debugging Test" include("debugging.jl") @safetestset "Namespacing test" include("namespacing.jl") + @safetestset "Subsystem replacement" include("substitute_component.jl") end end diff --git a/test/substitute_component.jl b/test/substitute_component.jl new file mode 100644 index 0000000000..f9dd8580c7 --- /dev/null +++ b/test/substitute_component.jl @@ -0,0 +1,269 @@ +using ModelingToolkit, ModelingToolkitStandardLibrary, Test +using ModelingToolkitStandardLibrary.Blocks +using ModelingToolkitStandardLibrary.Electrical +using OrdinaryDiffEq +using ModelingToolkit: t_nounits as t, D_nounits as D, renamespace, + NAMESPACE_SEPARATOR as NS + +@mtkmodel TwoComponent begin + @parameters begin + V = 1.0 + end + @components begin + component1 = OnePort() + component2 = OnePort() + source = Voltage() + constant = Constant(k = V) + ground = Ground() + end + @equations begin + connect(constant.output, source.V) + connect(source.p, component1.p) + connect(component1.n, component2.p) + connect(component2.n, source.n, ground.g) + end +end + +@mtkmodel RC begin + @parameters begin + R = 1.0 + C = 1.0 + V = 1.0 + end + @components begin + component1 = Resistor(R = R) + component2 = Capacitor(C = C, v = 0.0) + source = Voltage() + constant = Constant(k = V) + ground = Ground() + end + @equations begin + connect(constant.output, source.V) + connect(source.p, component1.p) + connect(component1.n, component2.p) + connect(component2.n, source.n, ground.g) + end +end + +@testset "Replacement with connections works" begin + @named templated = TwoComponent() + @named component1 = Resistor(R = 1.0) + @named component2 = Capacitor(C = 1.0, v = 0.0) + rsys = substitute_component(templated, templated.component1 => component1) + rcsys = substitute_component(rsys, rsys.component2 => component2) + + @named reference = RC() + + sys1 = structural_simplify(rcsys) + sys2 = structural_simplify(reference) + @test isequal(unknowns(sys1), unknowns(sys2)) + @test isequal(observed(sys1), observed(sys2)) + @test isequal(equations(sys1), equations(sys2)) + + prob1 = ODEProblem(sys1, [], (0.0, 10.0)) + prob2 = ODEProblem(sys2, [], (0.0, 10.0)) + + sol1 = solve(prob1, Tsit5()) + sol2 = solve(prob2, Tsit5(); saveat = sol1.t) + @test sol1.u≈sol2.u atol=1e-8 +end + +@mtkmodel BadOnePort1 begin + @components begin + p = Pin() + n = Pin() + end + @variables begin + i(t) + end + @equations begin + 0 ~ p.i + n.i + i ~ p.i + end +end + +@connector BadPin1 begin + v(t) +end + +@mtkmodel BadOnePort2 begin + @components begin + p = BadPin1() + n = Pin() + end + @variables begin + v(t) + i(t) + end + @equations begin + 0 ~ p.v + n.v + v ~ p.v + end +end + +@connector BadPin2 begin + v(t) + i(t) +end + +@mtkmodel BadOnePort3 begin + @components begin + p = BadPin2() + n = Pin() + end + @variables begin + v(t) + i(t) + end + @equations begin + 0 ~ p.v + n.v + v ~ p.v + end +end + +@connector BadPin3 begin + v(t), [input = true] + i(t), [connect = Flow] +end + +@mtkmodel BadOnePort4 begin + @components begin + p = BadPin3() + n = Pin() + end + @variables begin + v(t) + i(t) + end + @equations begin + 0 ~ p.v + n.v + v ~ p.v + end +end + +@connector BadPin4 begin + v(t), [output = true] + i(t), [connect = Flow] +end + +@mtkmodel BadOnePort5 begin + @components begin + p = BadPin4() + n = Pin() + end + @variables begin + v(t) + i(t) + end + @equations begin + 0 ~ p.v + n.v + v ~ p.v + end +end + +@mtkmodel BadPin5 begin + @variables begin + v(t) + i(t), [connect = Flow] + end +end + +@mtkmodel BadOnePort6 begin + @components begin + p = BadPin5() + n = Pin() + end + @variables begin + v(t) + i(t) + end + @equations begin + 0 ~ p.v + n.v + v ~ p.v + end +end + +@connector BadPin6 begin + i(t), [connect = Flow] +end + +@mtkmodel BadOnePort7 begin + @components begin + p = BadPin6() + n = Pin() + end + @variables begin + v(t) + i(t) + end + @equations begin + 0 ~ p.i + n.i + i ~ p.i + end +end + +@mtkmodel BadOnePort8 begin + @components begin + n = Pin() + end + @variables begin + v(t) + i(t) + end +end + +@testset "Error checking" begin + @named templated = TwoComponent() + @named component1 = Resistor(R = 1.0) + @named component2 = Capacitor(C = 1.0, v = 0.0) + @test_throws ["LHS", "cannot be completed"] substitute_component( + templated, complete(templated.component1) => component1) + @test_throws ["RHS", "cannot be completed"] substitute_component( + templated, templated.component1 => complete(component1)) + @test_throws ["RHS", "not be namespaced"] substitute_component( + templated, templated.component1 => renamespace(templated, component1)) + @named resistor = Resistor(R = 1.0) + @test_throws ["RHS", "same name"] substitute_component( + templated, templated.component1 => resistor) + + @testset "Different indepvar" begin + @independent_variables tt + @named empty = ODESystem(Equation[], t) + @named outer = ODESystem(Equation[], t; systems = [empty]) + @named empty = ODESystem(Equation[], tt) + @test_throws ["independent variable"] substitute_component( + outer, outer.empty => empty) + end + + @named component1 = BadOnePort1() + @test_throws ["RHS", "unknown", "v(t)"] substitute_component( + templated, templated.component1 => component1) + + @named component1 = BadOnePort2() + @test_throws ["component1$(NS)p", "i(t)"] substitute_component( + templated, templated.component1 => component1) + + @named component1 = BadOnePort3() + @test_throws ["component1$(NS)p$(NS)i", "Flow"] substitute_component( + templated, templated.component1 => component1) + + @named component1 = BadOnePort4() + @test_throws ["component1$(NS)p$(NS)v", "differing causality", "input"] substitute_component( + templated, templated.component1 => component1) + + @named component1 = BadOnePort5() + @test_throws ["component1$(NS)p$(NS)v", "differing causality", "output"] substitute_component( + templated, templated.component1 => component1) + + @named component1 = BadOnePort6() + @test_throws ["templated$(NS)component1$(NS)p", "not a connector"] substitute_component( + templated, templated.component1 => component1) + + @named component1 = BadOnePort7() + @test_throws ["templated$(NS)component1$(NS)p", "DomainConnector", "RegularConnector"] substitute_component( + templated, templated.component1 => component1) + + @named component1 = BadOnePort8() + @test_throws ["templated$(NS)component1", "subsystem p"] substitute_component( + templated, templated.component1 => component1) +end From e747405e6bf2ff73c480c3cf3f6b17351f50559c Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 28 Mar 2025 17:54:57 +0530 Subject: [PATCH 1243/2176] fix: test and handle scalar connections in `substitute_component` --- src/systems/abstractsystem.jl | 12 ++++++++++-- test/substitute_component.jl | 16 ++++++++++------ 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index 0372858b3a..299990951a 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -3286,7 +3286,7 @@ function recursive_getproperty( for (i, name) in enumerate(hierarchy) cur = getproperty(cur, name; namespace = i > 1 || !skip_namespace_first) end - return cur + return unwrap(cur) end """ @@ -3306,9 +3306,17 @@ function recreate_connections(sys::AbstractSystem) oldargs = [ap.input; ap.outputs] end newargs = map(get_systems(eq.rhs)) do arg + rewrap_nameof = arg isa SymbolicWithNameof + if rewrap_nameof + arg = arg.var + end name = arg isa AbstractSystem ? nameof(arg) : getname(arg) hierarchy = namespace_hierarchy(name) - return recursive_getproperty(sys, hierarchy) + newarg = recursive_getproperty(sys, hierarchy) + if rewrap_nameof + newarg = SymbolicWithNameof(newarg) + end + return newarg end if eq.lhs isa Connection return eq.lhs ~ Connection(newargs) diff --git a/test/substitute_component.jl b/test/substitute_component.jl index f9dd8580c7..9fb254136b 100644 --- a/test/substitute_component.jl +++ b/test/substitute_component.jl @@ -5,19 +5,22 @@ using OrdinaryDiffEq using ModelingToolkit: t_nounits as t, D_nounits as D, renamespace, NAMESPACE_SEPARATOR as NS -@mtkmodel TwoComponent begin - @parameters begin - V = 1.0 +@mtkmodel SignalInterface begin + @components begin + output = RealOutput() end +end + +@mtkmodel TwoComponent begin @components begin component1 = OnePort() component2 = OnePort() source = Voltage() - constant = Constant(k = V) + signal = SignalInterface() ground = Ground() end @equations begin - connect(constant.output, source.V) + connect(signal.output.u, source.V.u) connect(source.p, component1.p) connect(component1.n, component2.p) connect(component2.n, source.n, ground.g) @@ -49,15 +52,16 @@ end @named templated = TwoComponent() @named component1 = Resistor(R = 1.0) @named component2 = Capacitor(C = 1.0, v = 0.0) + @named signal = Constant(k = 1.0) rsys = substitute_component(templated, templated.component1 => component1) rcsys = substitute_component(rsys, rsys.component2 => component2) + rcsys = substitute_component(rcsys, rcsys.signal => signal) @named reference = RC() sys1 = structural_simplify(rcsys) sys2 = structural_simplify(reference) @test isequal(unknowns(sys1), unknowns(sys2)) - @test isequal(observed(sys1), observed(sys2)) @test isequal(equations(sys1), equations(sys2)) prob1 = ODEProblem(sys1, [], (0.0, 10.0)) From 2b8c0f645651508594e8d35c3c7e8779d8e0c5cc Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Sun, 30 Mar 2025 16:12:48 +0530 Subject: [PATCH 1244/2176] feat: enable CSE in `build_function_wrapper` --- src/systems/codegen_utils.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/systems/codegen_utils.jl b/src/systems/codegen_utils.jl index 1eeb7e026b..a3fe53b95d 100644 --- a/src/systems/codegen_utils.jl +++ b/src/systems/codegen_utils.jl @@ -132,7 +132,7 @@ function build_function_wrapper(sys::AbstractSystem, expr, args...; p_start = 2, wrap_delays = is_dde(sys), wrap_code = identity, add_observed = true, filter_observed = Returns(true), create_bindings = false, output_type = nothing, mkarray = nothing, - wrap_mtkparameters = true, extra_assignments = Assignment[], kwargs...) + wrap_mtkparameters = true, extra_assignments = Assignment[], cse = true, kwargs...) isscalar = !(expr isa AbstractArray || symbolic_type(expr) == ArraySymbolic()) # filter observed equations obs = filter(filter_observed, observed(sys)) @@ -234,7 +234,7 @@ function build_function_wrapper(sys::AbstractSystem, expr, args...; p_start = 2, if wrap_code isa Tuple && symbolic_type(expr) == ScalarSymbolic() wrap_code = wrap_code[1] end - return build_function(expr, args...; wrap_code, similarto, kwargs...) + return build_function(expr, args...; wrap_code, similarto, cse, kwargs...) end """ From d45b2dbc8397c0f676dc4acb09f9f2ec8556d6e5 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 1 Apr 2025 11:23:50 +0530 Subject: [PATCH 1245/2176] feat: proagate `cse` kwarg from problem constructors --- src/systems/abstractsystem.jl | 15 ++++--- src/systems/diffeqs/abstractodesystem.jl | 39 +++++++++++-------- src/systems/diffeqs/odesystem.jl | 7 ++-- src/systems/diffeqs/sdesystem.jl | 14 +++---- .../discrete_system/discrete_system.jl | 6 +-- .../implicit_discrete_system.jl | 6 +-- src/systems/jumps/jumpsystem.jl | 11 +++--- .../nonlinear/homotopy_continuation.jl | 6 +-- src/systems/nonlinear/nonlinearsystem.jl | 23 +++++------ .../optimization/optimizationsystem.jl | 16 ++++---- 10 files changed, 78 insertions(+), 65 deletions(-) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index fb692b4028..df07985f5a 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -535,7 +535,8 @@ end SymbolicIndexingInterface.supports_tuple_observed(::AbstractSystem) = true function SymbolicIndexingInterface.observed( - sys::AbstractSystem, sym; eval_expression = false, eval_module = @__MODULE__, checkbounds = true) + sys::AbstractSystem, sym; eval_expression = false, eval_module = @__MODULE__, + checkbounds = true, cse = true) if has_index_cache(sys) && (ic = get_index_cache(sys)) !== nothing if sym isa Symbol _sym = get(ic.symbol_to_variable, sym, nothing) @@ -559,7 +560,7 @@ function SymbolicIndexingInterface.observed( end end return build_explicit_observed_function( - sys, sym; eval_expression, eval_module, checkbounds) + sys, sym; eval_expression, eval_module, checkbounds, cse) end function SymbolicIndexingInterface.default_values(sys::AbstractSystem) @@ -1774,13 +1775,14 @@ struct ObservedFunctionCache{S} eval_expression::Bool eval_module::Module checkbounds::Bool + cse::Bool end function ObservedFunctionCache( sys; steady_state = false, eval_expression = false, - eval_module = @__MODULE__, checkbounds = true) + eval_module = @__MODULE__, checkbounds = true, cse = true) return ObservedFunctionCache( - sys, Dict(), steady_state, eval_expression, eval_module, checkbounds) + sys, Dict(), steady_state, eval_expression, eval_module, checkbounds, cse) end # This is hit because ensemble problems do a deepcopy @@ -1791,8 +1793,9 @@ function Base.deepcopy_internal(ofc::ObservedFunctionCache, stackdict::IdDict) eval_expression = ofc.eval_expression eval_module = ofc.eval_module checkbounds = ofc.checkbounds + cse = ofc.cse newofc = ObservedFunctionCache( - sys, dict, steady_state, eval_expression, eval_module, checkbounds) + sys, dict, steady_state, eval_expression, eval_module, checkbounds, cse) stackdict[ofc] = newofc return newofc end @@ -1801,7 +1804,7 @@ function (ofc::ObservedFunctionCache)(obsvar, args...) obs = get!(ofc.dict, value(obsvar)) do SymbolicIndexingInterface.observed( ofc.sys, obsvar; eval_expression = ofc.eval_expression, - eval_module = ofc.eval_module, checkbounds = ofc.checkbounds) + eval_module = ofc.eval_module, checkbounds = ofc.checkbounds, cse = ofc.cse) end if ofc.steady_state obs = let fn = obs diff --git a/src/systems/diffeqs/abstractodesystem.jl b/src/systems/diffeqs/abstractodesystem.jl index 306aa59fc1..23f20b00ec 100644 --- a/src/systems/diffeqs/abstractodesystem.jl +++ b/src/systems/diffeqs/abstractodesystem.jl @@ -312,12 +312,13 @@ function DiffEqBase.ODEFunction{iip, specialize}(sys::AbstractODESystem, analytic = nothing, split_idxs = nothing, initialization_data = nothing, + cse = true, kwargs...) where {iip, specialize} if !iscomplete(sys) error("A completed system is required. Call `complete` or `structural_simplify` on the system before creating an `ODEFunction`") end f_gen = generate_function(sys, dvs, ps; expression = Val{true}, - expression_module = eval_module, checkbounds = checkbounds, + expression_module = eval_module, checkbounds = checkbounds, cse, kwargs...) f_oop, f_iip = eval_or_rgf.(f_gen; eval_expression, eval_module) f = GeneratedFunctionWrapper{(2, 3, is_split(sys))}(f_oop, f_iip) @@ -333,7 +334,7 @@ function DiffEqBase.ODEFunction{iip, specialize}(sys::AbstractODESystem, tgrad_gen = generate_tgrad(sys, dvs, ps; simplify = simplify, expression = Val{true}, - expression_module = eval_module, + expression_module = eval_module, cse, checkbounds = checkbounds, kwargs...) tgrad_oop, tgrad_iip = eval_or_rgf.(tgrad_gen; eval_expression, eval_module) _tgrad = GeneratedFunctionWrapper{(2, 3, is_split(sys))}(tgrad_oop, tgrad_iip) @@ -345,7 +346,7 @@ function DiffEqBase.ODEFunction{iip, specialize}(sys::AbstractODESystem, jac_gen = generate_jacobian(sys, dvs, ps; simplify = simplify, sparse = sparse, expression = Val{true}, - expression_module = eval_module, + expression_module = eval_module, cse, checkbounds = checkbounds, kwargs...) jac_oop, jac_iip = eval_or_rgf.(jac_gen; eval_expression, eval_module) @@ -365,7 +366,7 @@ function DiffEqBase.ODEFunction{iip, specialize}(sys::AbstractODESystem, end observedfun = ObservedFunctionCache( - sys; steady_state, eval_expression, eval_module, checkbounds) + sys; steady_state, eval_expression, eval_module, checkbounds, cse) jac_prototype = if sparse uElType = u0 === nothing ? Float64 : eltype(u0) @@ -420,12 +421,13 @@ function DiffEqBase.DAEFunction{iip}(sys::AbstractODESystem, dvs = unknowns(sys) eval_module = @__MODULE__, checkbounds = false, initialization_data = nothing, + cse = true, kwargs...) where {iip} if !iscomplete(sys) error("A completed system is required. Call `complete` or `structural_simplify` on the system before creating a `DAEFunction`") end f_gen = generate_function(sys, dvs, ps; implicit_dae = true, - expression = Val{true}, + expression = Val{true}, cse, expression_module = eval_module, checkbounds = checkbounds, kwargs...) f_oop, f_iip = eval_or_rgf.(f_gen; eval_expression, eval_module) @@ -435,7 +437,7 @@ function DiffEqBase.DAEFunction{iip}(sys::AbstractODESystem, dvs = unknowns(sys) jac_gen = generate_dae_jacobian(sys, dvs, ps; simplify = simplify, sparse = sparse, expression = Val{true}, - expression_module = eval_module, + expression_module = eval_module, cse, checkbounds = checkbounds, kwargs...) jac_oop, jac_iip = eval_or_rgf.(jac_gen; eval_expression, eval_module) @@ -445,7 +447,7 @@ function DiffEqBase.DAEFunction{iip}(sys::AbstractODESystem, dvs = unknowns(sys) end observedfun = ObservedFunctionCache( - sys; eval_expression, eval_module, checkbounds = get(kwargs, :checkbounds, false)) + sys; eval_expression, eval_module, checkbounds = get(kwargs, :checkbounds, false), cse) jac_prototype = if sparse uElType = u0 === nothing ? Float64 : eltype(u0) @@ -479,6 +481,7 @@ function DiffEqBase.DDEFunction{iip}(sys::AbstractODESystem, dvs = unknowns(sys) eval_module = @__MODULE__, checkbounds = false, initialization_data = nothing, + cse = true, kwargs...) where {iip} if !iscomplete(sys) error("A completed system is required. Call `complete` or `structural_simplify` on the system before creating an `DDEFunction`") @@ -486,7 +489,7 @@ function DiffEqBase.DDEFunction{iip}(sys::AbstractODESystem, dvs = unknowns(sys) f_gen = generate_function(sys, dvs, ps; isdde = true, expression = Val{true}, expression_module = eval_module, checkbounds = checkbounds, - kwargs...) + cse, kwargs...) f_oop, f_iip = eval_or_rgf.(f_gen; eval_expression, eval_module) f = GeneratedFunctionWrapper{(3, 4, is_split(sys))}(f_oop, f_iip) @@ -503,6 +506,7 @@ function DiffEqBase.SDDEFunction{iip}(sys::AbstractODESystem, dvs = unknowns(sys eval_module = @__MODULE__, checkbounds = false, initialization_data = nothing, + cse = true, kwargs...) where {iip} if !iscomplete(sys) error("A completed system is required. Call `complete` or `structural_simplify` on the system before creating an `SDDEFunction`") @@ -510,12 +514,12 @@ function DiffEqBase.SDDEFunction{iip}(sys::AbstractODESystem, dvs = unknowns(sys f_gen = generate_function(sys, dvs, ps; isdde = true, expression = Val{true}, expression_module = eval_module, checkbounds = checkbounds, - kwargs...) + cse, kwargs...) f_oop, f_iip = eval_or_rgf.(f_gen; eval_expression, eval_module) f = GeneratedFunctionWrapper{(3, 4, is_split(sys))}(f_oop, f_iip) g_gen = generate_diffusion_function(sys, dvs, ps; expression = Val{true}, - isdde = true, kwargs...) + isdde = true, cse, kwargs...) g_oop, g_iip = eval_or_rgf.(g_gen; eval_expression, eval_module) g = GeneratedFunctionWrapper{(3, 4, is_split(sys))}(g_oop, g_iip) @@ -841,6 +845,7 @@ function SciMLBase.BVProblem{iip, specialize}(sys::AbstractODESystem, u0map = [] warn_initialize_determined = true, eval_expression = false, eval_module = @__MODULE__, + cse = true, kwargs...) where {iip, specialize} if !iscomplete(sys) error("A completed system is required. Call `complete` or `structural_simplify` on the system before creating an `BVProblem`") @@ -864,12 +869,12 @@ function SciMLBase.BVProblem{iip, specialize}(sys::AbstractODESystem, u0map = [] _u0map = has_alg_eqs(sys) ? u0map : merge(Dict(u0map), Dict(guesses)) f, u0, p = process_SciMLProblem(ODEFunction{iip, specialize}, sys, _u0map, parammap; t = tspan !== nothing ? tspan[1] : tspan, guesses, - check_length, warn_initialize_determined, eval_expression, eval_module, kwargs...) + check_length, warn_initialize_determined, eval_expression, eval_module, cse, kwargs...) stidxmap = Dict([v => i for (i, v) in enumerate(sts)]) u0_idxs = has_alg_eqs(sys) ? collect(1:length(sts)) : [stidxmap[k] for (k, v) in u0map] - fns = generate_function_bc(sys, u0, u0_idxs, tspan) + fns = generate_function_bc(sys, u0, u0_idxs, tspan; cse) bc_oop, bc_iip = eval_or_rgf.(fns; eval_expression, eval_module) bc(sol, p, t) = bc_oop(sol, p, t) bc(resid, u, p, t) = bc_iip(resid, u, p, t) @@ -988,15 +993,16 @@ function DiffEqBase.DDEProblem{iip}(sys::AbstractODESystem, u0map = [], eval_expression = false, eval_module = @__MODULE__, u0_constructor = identity, + cse = true, kwargs...) where {iip} if !iscomplete(sys) error("A completed system is required. Call `complete` or `structural_simplify` on the system before creating a `DDEProblem`") end f, u0, p = process_SciMLProblem(DDEFunction{iip}, sys, u0map, parammap; t = tspan !== nothing ? tspan[1] : tspan, - symbolic_u0 = true, u0_constructor, + symbolic_u0 = true, u0_constructor, cse, check_length, eval_expression, eval_module, kwargs...) - h_gen = generate_history(sys, u0; expression = Val{true}) + h_gen = generate_history(sys, u0; expression = Val{true}, cse) h_oop, h_iip = eval_or_rgf.(h_gen; eval_expression, eval_module) h = h_oop u0 = float.(h(p, tspan[1])) @@ -1027,6 +1033,7 @@ function DiffEqBase.SDDEProblem{iip}(sys::AbstractODESystem, u0map = [], eval_expression = false, eval_module = @__MODULE__, u0_constructor = identity, + cse = true, kwargs...) where {iip} if !iscomplete(sys) error("A completed system is required. Call `complete` or `structural_simplify` on the system before creating a `SDDEProblem`") @@ -1034,8 +1041,8 @@ function DiffEqBase.SDDEProblem{iip}(sys::AbstractODESystem, u0map = [], f, u0, p = process_SciMLProblem(SDDEFunction{iip}, sys, u0map, parammap; t = tspan !== nothing ? tspan[1] : tspan, symbolic_u0 = true, eval_expression, eval_module, u0_constructor, - check_length, kwargs...) - h_gen = generate_history(sys, u0; expression = Val{true}) + check_length, cse, kwargs...) + h_gen = generate_history(sys, u0; expression = Val{true}, cse) h_oop, h_iip = eval_or_rgf.(h_gen; eval_expression, eval_module) h = h_oop u0 = h(p, tspan[1]) diff --git a/src/systems/diffeqs/odesystem.jl b/src/systems/diffeqs/odesystem.jl index bd0a58e10a..57d71b18c8 100644 --- a/src/systems/diffeqs/odesystem.jl +++ b/src/systems/diffeqs/odesystem.jl @@ -454,8 +454,8 @@ Generates a function that computes the observed value(s) `ts` in the system `sys - `checkbounds = true` checks bounds if true when destructuring parameters - `op = Operator` sets the recursion terminator for the walk done by `vars` to identify the variables that appear in `ts`. See the documentation for `vars` for more detail. - `throw = true` if true, throw an error when generating a function for `ts` that reference variables that do not exist. -- `mkarray`; only used if the output is an array (that is, `!isscalar(ts)` and `ts` is not a tuple, in which case the result will always be a tuple). Called as `mkarray(ts, output_type)` where `ts` are the expressions to put in -the array and `output_type` is the argument of the same name passed to build_explicit_observed_function. +- `mkarray`: only used if the output is an array (that is, `!isscalar(ts)` and `ts` is not a tuple, in which case the result will always be a tuple). Called as `mkarray(ts, output_type)` where `ts` are the expressions to put in the array and `output_type` is the argument of the same name passed to build_explicit_observed_function. +- `cse = true`: Whether to use Common Subexpression Elimination (CSE) to generate a more efficient function. ## Returns @@ -493,6 +493,7 @@ function build_explicit_observed_function(sys, ts; param_only = false, op = Operator, throw = true, + cse = true, mkarray = nothing) is_tuple = ts isa Tuple if is_tuple @@ -579,7 +580,7 @@ function build_explicit_observed_function(sys, ts; p_end = length(dvs) + length(inputs) + length(ps) fns = build_function_wrapper( sys, ts, args...; p_start, p_end, filter_observed = obsfilter, - output_type, mkarray, try_namespaced = true, expression = Val{true}) + output_type, mkarray, try_namespaced = true, expression = Val{true}, cse) if fns isa Tuple if expression return return_inplace ? fns : fns[1] diff --git a/src/systems/diffeqs/sdesystem.jl b/src/systems/diffeqs/sdesystem.jl index f51529a559..66044427eb 100644 --- a/src/systems/diffeqs/sdesystem.jl +++ b/src/systems/diffeqs/sdesystem.jl @@ -595,23 +595,23 @@ function DiffEqBase.SDEFunction{iip, specialize}(sys::SDESystem, dvs = unknowns( jac = false, Wfact = false, eval_expression = false, eval_module = @__MODULE__, checkbounds = false, initialization_data = nothing, - kwargs...) where {iip, specialize} + cse = true, kwargs...) where {iip, specialize} if !iscomplete(sys) error("A completed `SDESystem` is required. Call `complete` or `structural_simplify` on the system before creating an `SDEFunction`") end dvs = scalarize.(dvs) - f_gen = generate_function(sys, dvs, ps; expression = Val{true}, kwargs...) + f_gen = generate_function(sys, dvs, ps; expression = Val{true}, cse, kwargs...) f_oop, f_iip = eval_or_rgf.(f_gen; eval_expression, eval_module) g_gen = generate_diffusion_function(sys, dvs, ps; expression = Val{true}, - kwargs...) + cse, kwargs...) g_oop, g_iip = eval_or_rgf.(g_gen; eval_expression, eval_module) f = GeneratedFunctionWrapper{(2, 3, is_split(sys))}(f_oop, f_iip) g = GeneratedFunctionWrapper{(2, 3, is_split(sys))}(g_oop, g_iip) if tgrad - tgrad_gen = generate_tgrad(sys, dvs, ps; expression = Val{true}, + tgrad_gen = generate_tgrad(sys, dvs, ps; expression = Val{true}, cse, kwargs...) tgrad_oop, tgrad_iip = eval_or_rgf.(tgrad_gen; eval_expression, eval_module) _tgrad = GeneratedFunctionWrapper{(2, 3, is_split(sys))}(tgrad_oop, tgrad_iip) @@ -621,7 +621,7 @@ function DiffEqBase.SDEFunction{iip, specialize}(sys::SDESystem, dvs = unknowns( if jac jac_gen = generate_jacobian(sys, dvs, ps; expression = Val{true}, - sparse = sparse, kwargs...) + sparse = sparse, cse, kwargs...) jac_oop, jac_iip = eval_or_rgf.(jac_gen; eval_expression, eval_module) _jac = GeneratedFunctionWrapper{(2, 3, is_split(sys))}(jac_oop, jac_iip) @@ -631,7 +631,7 @@ function DiffEqBase.SDEFunction{iip, specialize}(sys::SDESystem, dvs = unknowns( if Wfact tmp_Wfact, tmp_Wfact_t = generate_factorized_W(sys, dvs, ps, true; - expression = Val{true}, kwargs...) + expression = Val{true}, cse, kwargs...) Wfact_oop, Wfact_iip = eval_or_rgf.(tmp_Wfact; eval_expression, eval_module) Wfact_oop_t, Wfact_iip_t = eval_or_rgf.(tmp_Wfact_t; eval_expression, eval_module) @@ -645,7 +645,7 @@ function DiffEqBase.SDEFunction{iip, specialize}(sys::SDESystem, dvs = unknowns( _M = (u0 === nothing || M == I) ? M : ArrayInterface.restructure(u0 .* u0', M) observedfun = ObservedFunctionCache( - sys; eval_expression, eval_module, checkbounds = get(kwargs, :checkbounds, false)) + sys; eval_expression, eval_module, checkbounds = get(kwargs, :checkbounds, false), cse) SDEFunction{iip, specialize}(f, g; sys = sys, diff --git a/src/systems/discrete_system/discrete_system.jl b/src/systems/discrete_system/discrete_system.jl index 40f01769ee..5f7c986659 100644 --- a/src/systems/discrete_system/discrete_system.jl +++ b/src/systems/discrete_system/discrete_system.jl @@ -360,13 +360,13 @@ function SciMLBase.DiscreteFunction{iip, specialize}( t = nothing, eval_expression = false, eval_module = @__MODULE__, - analytic = nothing, + analytic = nothing, cse = true, kwargs...) where {iip, specialize} if !iscomplete(sys) error("A completed `DiscreteSystem` is required. Call `complete` or `structural_simplify` on the system before creating a `DiscreteProblem`") end f_gen = generate_function(sys, dvs, ps; expression = Val{true}, - expression_module = eval_module, kwargs...) + expression_module = eval_module, cse, kwargs...) f_oop, f_iip = eval_or_rgf.(f_gen; eval_expression, eval_module) f = GeneratedFunctionWrapper{(2, 3, is_split(sys))}(f_oop, f_iip) @@ -378,7 +378,7 @@ function SciMLBase.DiscreteFunction{iip, specialize}( end observedfun = ObservedFunctionCache( - sys; eval_expression, eval_module, checkbounds = get(kwargs, :checkbounds, false)) + sys; eval_expression, eval_module, checkbounds = get(kwargs, :checkbounds, false), cse) DiscreteFunction{iip, specialize}(f; sys = sys, diff --git a/src/systems/discrete_system/implicit_discrete_system.jl b/src/systems/discrete_system/implicit_discrete_system.jl index ebae78384a..3956c089d4 100644 --- a/src/systems/discrete_system/implicit_discrete_system.jl +++ b/src/systems/discrete_system/implicit_discrete_system.jl @@ -369,13 +369,13 @@ function SciMLBase.ImplicitDiscreteFunction{iip, specialize}( t = nothing, eval_expression = false, eval_module = @__MODULE__, - analytic = nothing, + analytic = nothing, cse = true, kwargs...) where {iip, specialize} if !iscomplete(sys) error("A completed `ImplicitDiscreteSystem` is required. Call `complete` or `structural_simplify` on the system before creating a `ImplicitDiscreteProblem`") end f_gen = generate_function(sys, dvs, ps; expression = Val{true}, - expression_module = eval_module, kwargs...) + expression_module = eval_module, cse, kwargs...) f_oop, f_iip = eval_or_rgf.(f_gen; eval_expression, eval_module) f(u_next, u, p, t) = f_oop(u_next, u, p, t) f(resid, u_next, u, p, t) = f_iip(resid, u_next, u, p, t) @@ -388,7 +388,7 @@ function SciMLBase.ImplicitDiscreteFunction{iip, specialize}( end observedfun = ObservedFunctionCache( - sys; eval_expression, eval_module, checkbounds = get(kwargs, :checkbounds, false)) + sys; eval_expression, eval_module, checkbounds = get(kwargs, :checkbounds, false), cse) ImplicitDiscreteFunction{iip, specialize}(f; sys = sys, diff --git a/src/systems/jumps/jumpsystem.jl b/src/systems/jumps/jumpsystem.jl index 9da32a4305..57a3aee7df 100644 --- a/src/systems/jumps/jumpsystem.jl +++ b/src/systems/jumps/jumpsystem.jl @@ -398,6 +398,7 @@ function DiffEqBase.DiscreteProblem(sys::JumpSystem, u0map, tspan::Union{Tuple, parammap = DiffEqBase.NullParameters(); eval_expression = false, eval_module = @__MODULE__, + cse = true, kwargs...) if !iscomplete(sys) error("A completed `JumpSystem` is required. Call `complete` or `structural_simplify` on the system before creating a `DiscreteProblem`") @@ -408,11 +409,11 @@ 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], tofloat = false, check_length = false, build_initializeprob = false) + t = tspan === nothing ? nothing : tspan[1], tofloat = false, check_length = false, build_initializeprob = false, cse) f = DiffEqBase.DISCRETE_INPLACE_DEFAULT observedfun = ObservedFunctionCache( - sys; eval_expression, eval_module, checkbounds = get(kwargs, :checkbounds, false)) + sys; eval_expression, eval_module, checkbounds = get(kwargs, :checkbounds, false), cse) df = DiscreteFunction{true, true}(f; sys = sys, observed = observedfun, initialization_data = get(_f.kwargs, :initialization_data, nothing)) @@ -488,7 +489,7 @@ oprob = ODEProblem(complete(js), u₀map, tspan, parammap) function DiffEqBase.ODEProblem(sys::JumpSystem, u0map, tspan::Union{Tuple, Nothing}, parammap = DiffEqBase.NullParameters(); eval_expression = false, - eval_module = @__MODULE__, + eval_module = @__MODULE__, cse = true, kwargs...) if !iscomplete(sys) error("A completed `JumpSystem` is required. Call `complete` or `structural_simplify` on the system before creating a `DiscreteProblem`") @@ -507,10 +508,10 @@ function DiffEqBase.ODEProblem(sys::JumpSystem, u0map, tspan::Union{Tuple, Nothi else _, u0, p = process_SciMLProblem(EmptySciMLFunction, sys, u0map, parammap; t = tspan === nothing ? nothing : tspan[1], tofloat = false, - check_length = false, build_initializeprob = false) + check_length = false, build_initializeprob = false, cse) f = (du, u, p, t) -> (du .= 0; nothing) observedfun = ObservedFunctionCache(sys; eval_expression, eval_module, - checkbounds = get(kwargs, :checkbounds, false)) + checkbounds = get(kwargs, :checkbounds, false), cse) df = ODEFunction(f; sys, observed = observedfun) return ODEProblem(df, u0, tspan, p; kwargs...) end diff --git a/src/systems/nonlinear/homotopy_continuation.jl b/src/systems/nonlinear/homotopy_continuation.jl index 474224eacd..9a77779103 100644 --- a/src/systems/nonlinear/homotopy_continuation.jl +++ b/src/systems/nonlinear/homotopy_continuation.jl @@ -489,7 +489,7 @@ end function SciMLBase.HomotopyNonlinearFunction{iip, specialize}( sys::NonlinearSystem, args...; eval_expression = false, eval_module = @__MODULE__, - p = nothing, fraction_cancel_fn = SymbolicUtils.simplify_fractions, + p = nothing, fraction_cancel_fn = SymbolicUtils.simplify_fractions, cse = true, kwargs...) where {iip, specialize} if !iscomplete(sys) error("A completed `NonlinearSystem` is required. Call `complete` or `structural_simplify` on the system before creating a `HomotopyContinuationFunction`") @@ -511,9 +511,9 @@ function SciMLBase.HomotopyNonlinearFunction{iip, specialize}( # we want to create f, jac etc. according to `sys2` since that will do the solving # but the `sys` inside for symbolic indexing should be the non-polynomial system - fn = NonlinearFunction{iip}(sys2; eval_expression, eval_module, kwargs...) + fn = NonlinearFunction{iip}(sys2; eval_expression, eval_module, cse, kwargs...) obsfn = ObservedFunctionCache( - sys; eval_expression, eval_module, checkbounds = get(kwargs, :checkbounds, false)) + sys; eval_expression, eval_module, checkbounds = get(kwargs, :checkbounds, false), cse) fn = remake(fn; sys = sys, observed = obsfn) denominator = build_explicit_observed_function(sys2, denoms) diff --git a/src/systems/nonlinear/nonlinearsystem.jl b/src/systems/nonlinear/nonlinearsystem.jl index 77215be5eb..856822492b 100644 --- a/src/systems/nonlinear/nonlinearsystem.jl +++ b/src/systems/nonlinear/nonlinearsystem.jl @@ -373,19 +373,19 @@ function SciMLBase.NonlinearFunction{iip}(sys::NonlinearSystem, dvs = unknowns(s eval_expression = false, eval_module = @__MODULE__, sparse = false, simplify = false, - initialization_data = nothing, + initialization_data = nothing, cse = true, kwargs...) where {iip} if !iscomplete(sys) error("A completed `NonlinearSystem` is required. Call `complete` or `structural_simplify` on the system before creating a `NonlinearFunction`") end - f_gen = generate_function(sys, dvs, ps; expression = Val{true}, kwargs...) + f_gen = generate_function(sys, dvs, ps; expression = Val{true}, cse, kwargs...) f_oop, f_iip = eval_or_rgf.(f_gen; eval_expression, eval_module) f = GeneratedFunctionWrapper{(2, 2, is_split(sys))}(f_oop, f_iip) if jac jac_gen = generate_jacobian(sys, dvs, ps; simplify = simplify, sparse = sparse, - expression = Val{true}, kwargs...) + expression = Val{true}, cse, kwargs...) jac_oop, jac_iip = eval_or_rgf.(jac_gen; eval_expression, eval_module) _jac = GeneratedFunctionWrapper{(2, 2, is_split(sys))}(jac_oop, jac_iip) else @@ -393,7 +393,7 @@ function SciMLBase.NonlinearFunction{iip}(sys::NonlinearSystem, dvs = unknowns(s end observedfun = ObservedFunctionCache( - sys; eval_expression, eval_module, checkbounds = get(kwargs, :checkbounds, false)) + sys; eval_expression, eval_module, checkbounds = get(kwargs, :checkbounds, false), cse) if length(dvs) == length(equations(sys)) resid_prototype = nothing @@ -606,7 +606,7 @@ end function CacheWriter(sys::AbstractSystem, buffer_types::Vector{TypeT}, exprs::Dict{TypeT, Vector{Any}}, solsyms, obseqs::Vector{Equation}; - eval_expression = false, eval_module = @__MODULE__) + eval_expression = false, eval_module = @__MODULE__, cse = true) ps = parameters(sys; initial_parameters = true) rps = reorder_parameters(sys, ps) obs_assigns = [eq.lhs ← eq.rhs for eq in obseqs] @@ -625,7 +625,7 @@ function CacheWriter(sys::AbstractSystem, buffer_types::Vector{TypeT}, sys, nothing, :out, DestructuredArgs(DestructuredArgs.(solsyms), generated_argument_name(1)), rps...; p_start = 3, p_end = length(rps) + 2, - expression = Val{true}, add_observed = false, + expression = Val{true}, add_observed = false, cse, extra_assignments = [array_assignments; obs_assigns; body]) fn = eval_or_rgf(fn; eval_expression, eval_module) fn = GeneratedFunctionWrapper{(3, 3, is_split(sys))}(fn, nothing) @@ -636,7 +636,7 @@ struct SCCNonlinearFunction{iip} end function SCCNonlinearFunction{iip}( sys::NonlinearSystem, _eqs, _dvs, _obs, cachesyms; eval_expression = false, - eval_module = @__MODULE__, kwargs...) where {iip} + eval_module = @__MODULE__, cse = true, kwargs...) where {iip} ps = parameters(sys; initial_parameters = true) rps = reorder_parameters(sys, ps) @@ -646,7 +646,7 @@ function SCCNonlinearFunction{iip}( f_gen = build_function_wrapper(sys, rhss, _dvs, rps..., cachesyms...; p_start = 2, p_end = length(rps) + length(cachesyms) + 1, add_observed = false, - extra_assignments = obs_assignments, expression = Val{true}) + extra_assignments = obs_assignments, expression = Val{true}, cse) f_oop, f_iip = eval_or_rgf.(f_gen; eval_expression, eval_module) f = GeneratedFunctionWrapper{(2, 2, is_split(sys))}(f_oop, f_iip) @@ -666,7 +666,8 @@ function SciMLBase.SCCNonlinearProblem(sys::NonlinearSystem, args...; kwargs...) end function SciMLBase.SCCNonlinearProblem{iip}(sys::NonlinearSystem, u0map, - parammap = SciMLBase.NullParameters(); eval_expression = false, eval_module = @__MODULE__, kwargs...) where {iip} + parammap = SciMLBase.NullParameters(); eval_expression = false, eval_module = @__MODULE__, + cse = true, kwargs...) where {iip} if !iscomplete(sys) || get_tearing_state(sys) === nothing error("A simplified `NonlinearSystem` is required. Call `structural_simplify` on the system before creating an `SCCNonlinearProblem`.") end @@ -801,14 +802,14 @@ function SciMLBase.SCCNonlinearProblem{iip}(sys::NonlinearSystem, u0map, solsyms = getindex.((dvs,), view(var_sccs, 1:(i - 1))) push!(explicitfuns, CacheWriter(sys, cachetypes, cacheexprs, solsyms, obs[_prevobsidxs]; - eval_expression, eval_module)) + eval_expression, eval_module, cse)) end cachebufsyms = Tuple(map(cachetypes) do T get(cachevars, T, []) end) f = SCCNonlinearFunction{iip}( - sys, _eqs, _dvs, _obs, cachebufsyms; eval_expression, eval_module, kwargs...) + sys, _eqs, _dvs, _obs, cachebufsyms; eval_expression, eval_module, cse, kwargs...) push!(nlfuns, f) end diff --git a/src/systems/optimization/optimizationsystem.jl b/src/systems/optimization/optimizationsystem.jl index e55f4b7871..fe238ee489 100644 --- a/src/systems/optimization/optimizationsystem.jl +++ b/src/systems/optimization/optimizationsystem.jl @@ -300,7 +300,7 @@ function DiffEqBase.OptimizationProblem{iip}(sys::OptimizationSystem, u0map, cons_sparse = false, checkbounds = false, linenumbers = true, parallel = SerialForm(), eval_expression = false, eval_module = @__MODULE__, - checks = true, + checks = true, cse = true, kwargs...) where {iip} if !iscomplete(sys) error("A completed `OptimizationSystem` is required. Call `complete` or `structural_simplify` on the system before creating a `OptimizationProblem`") @@ -355,7 +355,7 @@ function DiffEqBase.OptimizationProblem{iip}(sys::OptimizationSystem, u0map, f = let _f = eval_or_rgf( generate_function( sys, checkbounds = checkbounds, linenumbers = linenumbers, - expression = Val{true}, wrap_mtkparameters = false); + expression = Val{true}, wrap_mtkparameters = false, cse); eval_expression, eval_module) __f(u, p) = _f(u, p) @@ -370,7 +370,7 @@ function DiffEqBase.OptimizationProblem{iip}(sys::OptimizationSystem, u0map, sys, checkbounds = checkbounds, linenumbers = linenumbers, parallel = parallel, expression = Val{true}, - wrap_mtkparameters = false); + wrap_mtkparameters = false, cse); eval_expression, eval_module) _grad(u, p) = grad_oop(u, p) @@ -389,7 +389,7 @@ function DiffEqBase.OptimizationProblem{iip}(sys::OptimizationSystem, u0map, sys, checkbounds = checkbounds, linenumbers = linenumbers, sparse = sparse, parallel = parallel, - expression = Val{true}, wrap_mtkparameters = false); + expression = Val{true}, wrap_mtkparameters = false, cse); eval_expression, eval_module) _hess(u, p) = hess_oop(u, p) @@ -408,14 +408,14 @@ function DiffEqBase.OptimizationProblem{iip}(sys::OptimizationSystem, u0map, hess_prototype = nothing end - observedfun = ObservedFunctionCache(sys; eval_expression, eval_module, checkbounds) + observedfun = ObservedFunctionCache(sys; eval_expression, eval_module, checkbounds, cse) if length(cstr) > 0 @named cons_sys = ConstraintsSystem(cstr, dvs, ps; checks) cons_sys = complete(cons_sys) cons, lcons_, ucons_ = generate_function(cons_sys, checkbounds = checkbounds, linenumbers = linenumbers, - expression = Val{true}; wrap_mtkparameters = false) + expression = Val{true}; wrap_mtkparameters = false, cse) cons = let (cons_oop, cons_iip) = eval_or_rgf.(cons; eval_expression, eval_module) _cons(u, p) = cons_oop(u, p) _cons(resid, u, p) = cons_iip(resid, u, p) @@ -428,7 +428,7 @@ function DiffEqBase.OptimizationProblem{iip}(sys::OptimizationSystem, u0map, checkbounds = checkbounds, linenumbers = linenumbers, parallel = parallel, expression = Val{true}, - sparse = cons_sparse, wrap_mtkparameters = false); + sparse = cons_sparse, wrap_mtkparameters = false, cse); eval_expression, eval_module) _cons_j(u, p) = cons_jac_oop(u, p) @@ -446,7 +446,7 @@ function DiffEqBase.OptimizationProblem{iip}(sys::OptimizationSystem, u0map, cons_sys, checkbounds = checkbounds, linenumbers = linenumbers, sparse = cons_sparse, parallel = parallel, - expression = Val{true}, wrap_mtkparameters = false); + expression = Val{true}, wrap_mtkparameters = false, cse); eval_expression, eval_module) _cons_h(u, p) = cons_hess_oop(u, p) From b1a4b3607a1439bef8dc6137a230e36ad8f55c81 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 1 Apr 2025 16:06:33 +0530 Subject: [PATCH 1246/2176] fix: fix OptimizationProblem codegen with CSE --- src/systems/optimization/optimizationsystem.jl | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/systems/optimization/optimizationsystem.jl b/src/systems/optimization/optimizationsystem.jl index fe238ee489..be4567aee5 100644 --- a/src/systems/optimization/optimizationsystem.jl +++ b/src/systems/optimization/optimizationsystem.jl @@ -354,7 +354,7 @@ function DiffEqBase.OptimizationProblem{iip}(sys::OptimizationSystem, u0map, f = let _f = eval_or_rgf( generate_function( - sys, checkbounds = checkbounds, linenumbers = linenumbers, + sys; checkbounds = checkbounds, linenumbers = linenumbers, expression = Val{true}, wrap_mtkparameters = false, cse); eval_expression, eval_module) @@ -367,7 +367,7 @@ function DiffEqBase.OptimizationProblem{iip}(sys::OptimizationSystem, u0map, if grad _grad = let (grad_oop, grad_iip) = eval_or_rgf.( generate_gradient( - sys, checkbounds = checkbounds, + sys; checkbounds = checkbounds, linenumbers = linenumbers, parallel = parallel, expression = Val{true}, wrap_mtkparameters = false, cse); @@ -386,7 +386,7 @@ function DiffEqBase.OptimizationProblem{iip}(sys::OptimizationSystem, u0map, if hess _hess = let (hess_oop, hess_iip) = eval_or_rgf.( generate_hessian( - sys, checkbounds = checkbounds, + sys; checkbounds = checkbounds, linenumbers = linenumbers, sparse = sparse, parallel = parallel, expression = Val{true}, wrap_mtkparameters = false, cse); @@ -413,9 +413,9 @@ function DiffEqBase.OptimizationProblem{iip}(sys::OptimizationSystem, u0map, if length(cstr) > 0 @named cons_sys = ConstraintsSystem(cstr, dvs, ps; checks) cons_sys = complete(cons_sys) - cons, lcons_, ucons_ = generate_function(cons_sys, checkbounds = checkbounds, + cons, lcons_, ucons_ = generate_function(cons_sys; checkbounds = checkbounds, linenumbers = linenumbers, - expression = Val{true}; wrap_mtkparameters = false, cse) + expression = Val{true}, wrap_mtkparameters = false, cse) cons = let (cons_oop, cons_iip) = eval_or_rgf.(cons; eval_expression, eval_module) _cons(u, p) = cons_oop(u, p) _cons(resid, u, p) = cons_iip(resid, u, p) @@ -443,7 +443,7 @@ function DiffEqBase.OptimizationProblem{iip}(sys::OptimizationSystem, u0map, if cons_h _cons_h = let (cons_hess_oop, cons_hess_iip) = eval_or_rgf.( generate_hessian( - cons_sys, checkbounds = checkbounds, + cons_sys; checkbounds = checkbounds, linenumbers = linenumbers, sparse = cons_sparse, parallel = parallel, expression = Val{true}, wrap_mtkparameters = false, cse); From 53d0c9f822fba5710bfe5a71fa6c1aa3eb036760 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 1 Apr 2025 16:06:38 +0530 Subject: [PATCH 1247/2176] fix: disable CSE in callback codegen --- src/systems/callbacks.jl | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/systems/callbacks.jl b/src/systems/callbacks.jl index 937264d083..07809bf611 100644 --- a/src/systems/callbacks.jl +++ b/src/systems/callbacks.jl @@ -699,7 +699,7 @@ function compile_affect(eqs::Vector{Equation}, cb, sys, dvs, ps; outputidxs = no add_integrator_header(sys, integ, outvar), outputidxs = update_inds, create_bindings = false, - kwargs...) + kwargs..., cse = false) # applied user-provided function to the generated expression if postprocess_affect_expr! !== nothing postprocess_affect_expr!(rf_ip, integ) @@ -729,7 +729,7 @@ function generate_single_rootfinding_callback( end rf_oop, rf_ip = generate_custom_function( - sys, [eq.rhs], dvs, ps; expression = Val{false}, kwargs...) + sys, [eq.rhs], dvs, ps; expression = Val{false}, kwargs..., cse = false) affect_function = compile_affect_fn(cb, sys, dvs, ps, kwargs) cond = function (u, t, integ) if DiffEqBase.isinplace(integ.sol.prob) @@ -780,7 +780,7 @@ function generate_vector_rootfinding_callback( rhss = map(x -> x.rhs, eqs) _, rf_ip = generate_custom_function( - sys, rhss, dvs, ps; expression = Val{false}, kwargs...) + sys, rhss, dvs, ps; expression = Val{false}, kwargs..., cse = false) affect_functions = @NamedTuple{ affect::Function, From f7466d034d7dd516e6589882d66dcca967a1ffcb Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 2 Apr 2025 11:43:54 +0530 Subject: [PATCH 1248/2176] build: bump Symbolics, SymbolicUtils compat --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 5b4f97e815..b41ff59b22 100644 --- a/Project.toml +++ b/Project.toml @@ -150,7 +150,7 @@ StaticArrays = "0.10, 0.11, 0.12, 1.0" StochasticDelayDiffEq = "1.8.1" StochasticDiffEq = "6.72.1" SymbolicIndexingInterface = "0.3.37" -SymbolicUtils = "3.14" +SymbolicUtils = "3.25.1" Symbolics = "6.36" URIs = "1" UnPack = "0.1, 1.0" From 3e36c47938e5476a4f145dee1b0be616662a320d Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 2 Apr 2025 16:42:48 +0530 Subject: [PATCH 1249/2176] test: make input output tests less sensitive to FP error --- test/input_output_handling.jl | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/test/input_output_handling.jl b/test/input_output_handling.jl index 84539f0b37..7f4a3247ad 100644 --- a/test/input_output_handling.jl +++ b/test/input_output_handling.jl @@ -173,7 +173,7 @@ end p = [rand()] x = [rand()] u = [rand()] - @test f[1](x, u, p, 1) == -x + u + @test f[1](x, u, p, 1) ≈ -x + u # With disturbance inputs @variables x(t)=0 u(t)=0 [input = true] d(t)=0 @@ -191,7 +191,7 @@ end p = [rand()] x = [rand()] u = [rand()] - @test f[1](x, u, p, 1) == -x + u + @test f[1](x, u, p, 1) ≈ -x + u ## With added d argument @variables x(t)=0 u(t)=0 [input = true] d(t)=0 @@ -210,7 +210,7 @@ end x = [rand()] u = [rand()] d = [rand()] - @test f[1](x, u, p, t, d) == -x + u + [d[]^2] + @test f[1](x, u, p, t, d) ≈ -x + u + [d[]^2] end end @@ -434,7 +434,7 @@ matrices, ssys = linearize(augmented_sys, (; io_sys,) = ModelingToolkit.generate_control_function(sys, simplify = true) obsfn = ModelingToolkit.build_explicit_observed_function( io_sys, [x + u * t]; inputs = [u]) - @test obsfn([1.0], [2.0], MTKParameters(io_sys, []), 3.0) == [7.0] + @test obsfn([1.0], [2.0], MTKParameters(io_sys, []), 3.0) ≈ [7.0] end # https://github.com/SciML/ModelingToolkit.jl/issues/2896 @@ -445,7 +445,7 @@ end @named sys = ODESystem(eqs, t, [x], []) f, dvs, ps, io_sys = ModelingToolkit.generate_control_function(sys, simplify = true) - @test f[1]([0.5], nothing, MTKParameters(io_sys, []), 0.0) == [1.0] + @test f[1]([0.5], nothing, MTKParameters(io_sys, []), 0.0) ≈ [1.0] end @testset "With callable symbolic" begin From 5b81a118e35c662f04b8aead8b401957a0637fec Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 3 Apr 2025 01:00:35 +0530 Subject: [PATCH 1250/2176] fix: update initials with non-symbolic `u0` in `remake` --- src/systems/nonlinear/initializesystem.jl | 12 +++++++++++- test/initializationsystem.jl | 11 +++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/systems/nonlinear/initializesystem.jl b/src/systems/nonlinear/initializesystem.jl index c03524c61b..23573673b0 100644 --- a/src/systems/nonlinear/initializesystem.jl +++ b/src/systems/nonlinear/initializesystem.jl @@ -655,7 +655,17 @@ end function SciMLBase.late_binding_update_u0_p( prob, sys::AbstractSystem, u0, p, t0, newu0, newp) u0 === missing && return newu0, (p === missing ? copy(newp) : newp) - eltype(u0) <: Pair || return newu0, (p === missing ? copy(newp) : newp) + if !(eltype(u0) <: Pair) + p === missing || return newu0, newp + newu0 === nothing && return newu0, newp + newp = p === missing ? copy(newp) : newp + initials, repack, alias = SciMLStructures.canonicalize( + SciMLStructures.Initials(), newp) + initials = DiffEqBase.promote_u0(initials, newu0, t0) + newp = repack(initials) + setp(sys, Initial.(unknowns(sys)))(newp, newu0) + return newu0, newp + end newp = p === missing ? copy(newp) : newp newu0 = DiffEqBase.promote_u0(newu0, newp, t0) diff --git a/test/initializationsystem.jl b/test/initializationsystem.jl index 54b847c64a..1120394b01 100644 --- a/test/initializationsystem.jl +++ b/test/initializationsystem.jl @@ -1476,3 +1476,14 @@ end @test sol.ps[Γ[1]] ≈ 5.0 end end + +@testset "Issue#3504: Update initials when `remake` called with non-symbolic `u0`" begin + @variables x(t) y(t) + @parameters c1 c2 + @mtkbuild sys = ODESystem([D(x) ~ -c1 * x + c2 * y, D(y) ~ c1 * x - c2 * y], t) + prob1 = ODEProblem(sys, [1.0, 2.0], (0.0, 1.0), [c1 => 1.0, c2 => 2.0]) + prob2 = remake(prob1, u0 = [2.0, 3.0]) + integ1 = init(prob1, Tsit5()) + integ2 = init(prob2, Tsit5()) + @test integ2.u ≈ [2.0, 3.0] +end From 68e06054b83f63af76ed4f0a6b9ea3f56cb559dd Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 3 Apr 2025 01:10:43 +0530 Subject: [PATCH 1251/2176] fix: update initials in `remake` with non-symbolic `u0` and symbolic `p` --- src/systems/nonlinear/initializesystem.jl | 4 +++- test/initializationsystem.jl | 4 ++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/systems/nonlinear/initializesystem.jl b/src/systems/nonlinear/initializesystem.jl index 23573673b0..daeef1def6 100644 --- a/src/systems/nonlinear/initializesystem.jl +++ b/src/systems/nonlinear/initializesystem.jl @@ -655,8 +655,10 @@ end function SciMLBase.late_binding_update_u0_p( prob, sys::AbstractSystem, u0, p, t0, newu0, newp) u0 === missing && return newu0, (p === missing ? copy(newp) : newp) + # non-symbolic u0 updates initials... if !(eltype(u0) <: Pair) - p === missing || return newu0, newp + # if `p` is not provided or is symbolic + p === missing || eltype(p) <: Pair || return newu0, newp newu0 === nothing && return newu0, newp newp = p === missing ? copy(newp) : newp initials, repack, alias = SciMLStructures.canonicalize( diff --git a/test/initializationsystem.jl b/test/initializationsystem.jl index 1120394b01..94a1f4c14f 100644 --- a/test/initializationsystem.jl +++ b/test/initializationsystem.jl @@ -1483,7 +1483,11 @@ end @mtkbuild sys = ODESystem([D(x) ~ -c1 * x + c2 * y, D(y) ~ c1 * x - c2 * y], t) prob1 = ODEProblem(sys, [1.0, 2.0], (0.0, 1.0), [c1 => 1.0, c2 => 2.0]) prob2 = remake(prob1, u0 = [2.0, 3.0]) + prob3 = remake(prob1, u0 = [2.0, 3.0], p = [c1 => 2.0]) integ1 = init(prob1, Tsit5()) integ2 = init(prob2, Tsit5()) + integ3 = init(prob3, Tsit5()) @test integ2.u ≈ [2.0, 3.0] + @test integ3.u ≈ [2.0, 3.0] + @test integ3.ps[c1] ≈ 2.0 end From c70f1015cd4be94c858904fce407424edecffe6c Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 3 Apr 2025 11:42:25 +0530 Subject: [PATCH 1252/2176] fix: early exit `late_binding_update_u0_p` if system doesn't support initialization --- src/systems/nonlinear/initializesystem.jl | 1 + 1 file changed, 1 insertion(+) diff --git a/src/systems/nonlinear/initializesystem.jl b/src/systems/nonlinear/initializesystem.jl index daeef1def6..52eb886bf9 100644 --- a/src/systems/nonlinear/initializesystem.jl +++ b/src/systems/nonlinear/initializesystem.jl @@ -654,6 +654,7 @@ end function SciMLBase.late_binding_update_u0_p( prob, sys::AbstractSystem, u0, p, t0, newu0, newp) + supports_initialization(sys) || return newu0, newp u0 === missing && return newu0, (p === missing ? copy(newp) : newp) # non-symbolic u0 updates initials... if !(eltype(u0) <: Pair) From 69509276a2954665fab4a40c9496e5b64c196ef8 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 3 Apr 2025 11:42:39 +0530 Subject: [PATCH 1253/2176] test: update test to account for new new initial propogation --- test/nonlinearsystem.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/nonlinearsystem.jl b/test/nonlinearsystem.jl index 5cf3b4b1be..9475f24006 100644 --- a/test/nonlinearsystem.jl +++ b/test/nonlinearsystem.jl @@ -239,7 +239,7 @@ testdict = Dict([:test => 1]) prob_ = remake(prob, u0 = [1.0, 2.0, 3.0], p = [a => 1.1, b => 1.2, c => 1.3]) @test prob_.u0 == [1.0, 2.0, 3.0] - initials = unknowns(sys) .=> ones(3) + initials = unknowns(sys) .=> [1.0, 2.0, 3.0] @test prob_.p == MTKParameters(sys, [a => 1.1, b => 1.2, c => 1.3, initials...]) prob_ = remake(prob, u0 = Dict(y => 2.0), p = Dict(a => 2.0)) From a9ca3e7dca3cfab4e38ec2805a3647ee3b491682 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 3 Apr 2025 12:17:23 +0530 Subject: [PATCH 1254/2176] fix: remove unnecessary copies in `late_binding_update_u0_p` --- src/systems/nonlinear/initializesystem.jl | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/src/systems/nonlinear/initializesystem.jl b/src/systems/nonlinear/initializesystem.jl index 52eb886bf9..99fc02e5e7 100644 --- a/src/systems/nonlinear/initializesystem.jl +++ b/src/systems/nonlinear/initializesystem.jl @@ -664,8 +664,10 @@ function SciMLBase.late_binding_update_u0_p( newp = p === missing ? copy(newp) : newp initials, repack, alias = SciMLStructures.canonicalize( SciMLStructures.Initials(), newp) - initials = DiffEqBase.promote_u0(initials, newu0, t0) - newp = repack(initials) + if eltype(initials) != eltype(newu0) + initials = DiffEqBase.promote_u0(initials, newu0, t0) + newp = repack(initials) + end setp(sys, Initial.(unknowns(sys)))(newp, newu0) return newu0, newp end @@ -673,11 +675,15 @@ function SciMLBase.late_binding_update_u0_p( newp = p === missing ? copy(newp) : newp newu0 = DiffEqBase.promote_u0(newu0, newp, t0) tunables, repack, alias = SciMLStructures.canonicalize(SciMLStructures.Tunable(), newp) - tunables = DiffEqBase.promote_u0(tunables, newu0, t0) - newp = repack(tunables) + if eltype(tunables) != eltype(newu0) + tunables = DiffEqBase.promote_u0(tunables, newu0, t0) + newp = repack(tunables) + end initials, repack, alias = SciMLStructures.canonicalize(SciMLStructures.Initials(), newp) - initials = DiffEqBase.promote_u0(initials, newu0, t0) - newp = repack(initials) + if eltype(initials) != eltype(newu0) + initials = DiffEqBase.promote_u0(initials, newu0, t0) + newp = repack(initials) + end allsyms = all_symbols(sys) for (k, v) in u0 From 216aa73e5c0ae76e4642f29aa7de22b1ff75d92a Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 3 Apr 2025 16:03:16 +0530 Subject: [PATCH 1255/2176] fix: handle edge case in `late_binding_update_u0_p` when `Initial` parameters don't exist --- src/systems/nonlinear/initializesystem.jl | 1 + 1 file changed, 1 insertion(+) diff --git a/src/systems/nonlinear/initializesystem.jl b/src/systems/nonlinear/initializesystem.jl index 99fc02e5e7..2283e7a1d4 100644 --- a/src/systems/nonlinear/initializesystem.jl +++ b/src/systems/nonlinear/initializesystem.jl @@ -661,6 +661,7 @@ function SciMLBase.late_binding_update_u0_p( # if `p` is not provided or is symbolic p === missing || eltype(p) <: Pair || return newu0, newp newu0 === nothing && return newu0, newp + all(is_parameter(sys, Initial(x)) for x in unknowns(sys)) || return newu0, newp newp = p === missing ? copy(newp) : newp initials, repack, alias = SciMLStructures.canonicalize( SciMLStructures.Initials(), newp) From 01a43bec2f4bf1e1f2af0444dcc7abfdedaa6d77 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 3 Apr 2025 17:41:43 +0530 Subject: [PATCH 1256/2176] feat: error in `late_binding_update_u0_p` if `newu0` is of incorrect length --- src/systems/nonlinear/initializesystem.jl | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/systems/nonlinear/initializesystem.jl b/src/systems/nonlinear/initializesystem.jl index 2283e7a1d4..9c85b9b324 100644 --- a/src/systems/nonlinear/initializesystem.jl +++ b/src/systems/nonlinear/initializesystem.jl @@ -669,6 +669,9 @@ function SciMLBase.late_binding_update_u0_p( initials = DiffEqBase.promote_u0(initials, newu0, t0) newp = repack(initials) end + if length(newu0) != length(unknowns(sys)) + throw(ArgumentError("Expected `newu0` to be of same length as unknowns ($(length(unknowns(sys)))). Got $(typeof(newu0)) of length $(length(newu0))")) + end setp(sys, Initial.(unknowns(sys)))(newp, newu0) return newu0, newp end From 61a64f9f6b39ce2846e19761ebc9520e11b4ce0f Mon Sep 17 00:00:00 2001 From: Christopher Rackauckas Date: Thu, 3 Apr 2025 08:47:59 -0400 Subject: [PATCH 1257/2176] Update Project.toml --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index b41ff59b22..8766db8eee 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "ModelingToolkit" uuid = "961ee093-0014-501f-94e3-6117800e7a78" authors = ["Yingbo Ma ", "Chris Rackauckas and contributors"] -version = "9.70.0" +version = "9.71.0" [deps] ADTypes = "47edcb42-4c32-4615-8424-f2b9edc5f35b" From d690f389b8c21177de70c5eaa28186e1d1ea033e Mon Sep 17 00:00:00 2001 From: vyudu Date: Thu, 3 Apr 2025 10:48:39 -0400 Subject: [PATCH 1258/2176] init: add jac_prototype --- src/systems/diffeqs/sdesystem.jl | 33 ++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/src/systems/diffeqs/sdesystem.jl b/src/systems/diffeqs/sdesystem.jl index 66044427eb..98c8aa6b25 100644 --- a/src/systems/diffeqs/sdesystem.jl +++ b/src/systems/diffeqs/sdesystem.jl @@ -593,6 +593,7 @@ function DiffEqBase.SDEFunction{iip, specialize}(sys::SDESystem, dvs = unknowns( u0 = nothing; version = nothing, tgrad = false, sparse = false, jac = false, Wfact = false, eval_expression = false, + sparsity = false, analytic = nothing, eval_module = @__MODULE__, checkbounds = false, initialization_data = nothing, cse = true, kwargs...) where {iip, specialize} @@ -641,6 +642,17 @@ function DiffEqBase.SDEFunction{iip, specialize}(sys::SDESystem, dvs = unknowns( _Wfact, _Wfact_t = nothing, nothing end + jac_prototype = if sparse + uElType = u0 === nothing ? Float64 : eltype(u0) + if jac + similar(calculate_jacobian(sys, sparse = sparse), uElType) + else + similar(jacobian_sparsity(sys), uElType) + end + else + nothing + end + M = calculate_massmatrix(sys) _M = (u0 === nothing || M == I) ? M : ArrayInterface.restructure(u0 .* u0', M) @@ -651,10 +663,14 @@ function DiffEqBase.SDEFunction{iip, specialize}(sys::SDESystem, dvs = unknowns( sys = sys, jac = _jac === nothing ? nothing : _jac, tgrad = _tgrad === nothing ? nothing : _tgrad, + mass_matrix = _M + jac_prototype = jac_prototype, + observed = observedfun, + sparsity = sparsity ? jacobian_sparsity(sys) : nothing, + analytic = analytic, Wfact = _Wfact === nothing ? nothing : _Wfact, Wfact_t = _Wfact_t === nothing ? nothing : _Wfact_t, - mass_matrix = _M, initialization_data, - observed = observedfun) + initialization_data) end """ @@ -724,6 +740,17 @@ function SDEFunctionExpr{iip}(sys::SDESystem, dvs = unknowns(sys), _jac = :nothing end + jac_prototype = if sparse + uElType = u0 === nothing ? Float64 : eltype(u0) + if jac + similar(calculate_jacobian(sys, sparse = sparse), uElType) + else + similar(jacobian_sparsity(sys), uElType) + end + else + nothing + end + if Wfact tmp_Wfact, tmp_Wfact_t = generate_factorized_W( sys, dvs, ps; expression = Val{true}, @@ -743,11 +770,13 @@ function SDEFunctionExpr{iip}(sys::SDESystem, dvs = unknowns(sys), g = $g tgrad = $_tgrad jac = $_jac + jac_prototype = $jac_prototype Wfact = $_Wfact Wfact_t = $_Wfact_t M = $_M SDEFunction{$iip}(f, g, jac = jac, + jac_prototype = jac_prototype, tgrad = tgrad, Wfact = Wfact, Wfact_t = Wfact_t, From a71ba77435549da2ee8cf6dfd80c7879fae856de Mon Sep 17 00:00:00 2001 From: vyudu Date: Thu, 3 Apr 2025 10:54:59 -0400 Subject: [PATCH 1259/2176] typo: comma --- src/systems/diffeqs/sdesystem.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/systems/diffeqs/sdesystem.jl b/src/systems/diffeqs/sdesystem.jl index 98c8aa6b25..806dbdf38a 100644 --- a/src/systems/diffeqs/sdesystem.jl +++ b/src/systems/diffeqs/sdesystem.jl @@ -663,7 +663,7 @@ function DiffEqBase.SDEFunction{iip, specialize}(sys::SDESystem, dvs = unknowns( sys = sys, jac = _jac === nothing ? nothing : _jac, tgrad = _tgrad === nothing ? nothing : _tgrad, - mass_matrix = _M + mass_matrix = _M, jac_prototype = jac_prototype, observed = observedfun, sparsity = sparsity ? jacobian_sparsity(sys) : nothing, From bd8913b23db9e9fe6320bb05b3708998dc2fbdfd Mon Sep 17 00:00:00 2001 From: vyudu Date: Thu, 3 Apr 2025 12:50:12 -0400 Subject: [PATCH 1260/2176] feat: add W sparsity/generation functions --- src/systems/diffeqs/abstractodesystem.jl | 46 +++++++++++++++++++++--- src/systems/diffeqs/sdesystem.jl | 13 ++++--- 2 files changed, 49 insertions(+), 10 deletions(-) diff --git a/src/systems/diffeqs/abstractodesystem.jl b/src/systems/diffeqs/abstractodesystem.jl index 23f20b00ec..a231db040c 100644 --- a/src/systems/diffeqs/abstractodesystem.jl +++ b/src/systems/diffeqs/abstractodesystem.jl @@ -126,6 +126,33 @@ function generate_jacobian(sys::AbstractODESystem, dvs = unknowns(sys), dvs, p..., get_iv(sys); + wrap_code = sparse ? assert_jac_length_header(sys) : (identity, identity), + kwargs...) +end + +function assert_jac_length_header(sys) + W = W_sparsity(sys) + + identity, expr -> Func([expr.args...], [], LiteralExpr(quote + @assert nnz($(expr.args[1])) == nnz(W) + expr.body + end)) +end + +function generate_W(sys::AbstractODESystem, γ = 1., dvs = unknowns(sys), + ps = parameters(sys; initial_parameters = true); + simplify = false, sparse = false, kwargs...) + @variables ˍ₋gamma + M = calculate_massmatrix(sys; simplify) + J = calculate_jacobian(sys; simplify, sparse, dvs) + W = ˍ₋gamma*M + J + + p = reorder_parameters(sys, ps) + return build_function_wrapper(sys, W, + dvs, + p..., + ˍ₋gamma, + get_iv(sys); kwargs...) end @@ -264,6 +291,12 @@ function jacobian_dae_sparsity(sys::AbstractODESystem) J1 + J2 end +function W_sparsity(sys::AbstractODESystem) + jac_sparsity = jacobian_sparsity(sys) + M_sparsity = sparse(iszero.(calculate_massmatrix(sys))) + jac_sparsity .|| M_sparsity +end + function isautonomous(sys::AbstractODESystem) tgrad = calculate_tgrad(sys; simplify = true) all(iszero, tgrad) @@ -368,15 +401,17 @@ function DiffEqBase.ODEFunction{iip, specialize}(sys::AbstractODESystem, observedfun = ObservedFunctionCache( sys; steady_state, eval_expression, eval_module, checkbounds, cse) - jac_prototype = if sparse + if sparse uElType = u0 === nothing ? Float64 : eltype(u0) if jac - similar(calculate_jacobian(sys, sparse = sparse), uElType) + jac_prototype = similar(calculate_jacobian(sys; sparse), uElType) else - similar(jacobian_sparsity(sys), uElType) + jac_prototype = similar(jacobian_sparsity(sys), uElType) end + W_prototype = similar(W_sparsity(sys), uElType) else - nothing + jac_prototype = nothing + W_prototype = nothing end @set! sys.split_idxs = split_idxs @@ -386,7 +421,8 @@ function DiffEqBase.ODEFunction{iip, specialize}(sys::AbstractODESystem, jac = _jac === nothing ? nothing : _jac, tgrad = _tgrad === nothing ? nothing : _tgrad, mass_matrix = _M, - jac_prototype = jac_prototype, + jac_prototype = W_prototype, + W_prototype = W_prototype, observed = observedfun, sparsity = sparsity ? jacobian_sparsity(sys) : nothing, analytic = analytic, diff --git a/src/systems/diffeqs/sdesystem.jl b/src/systems/diffeqs/sdesystem.jl index 806dbdf38a..2f84b70dcd 100644 --- a/src/systems/diffeqs/sdesystem.jl +++ b/src/systems/diffeqs/sdesystem.jl @@ -642,15 +642,17 @@ function DiffEqBase.SDEFunction{iip, specialize}(sys::SDESystem, dvs = unknowns( _Wfact, _Wfact_t = nothing, nothing end - jac_prototype = if sparse + if sparse uElType = u0 === nothing ? Float64 : eltype(u0) if jac - similar(calculate_jacobian(sys, sparse = sparse), uElType) + jac_prototype = similar(calculate_jacobian(sys; sparse), uElType) else - similar(jacobian_sparsity(sys), uElType) + jac_prototype = similar(jacobian_sparsity(sys), uElType) end + W_prototype = similar(W_sparsity(sys), uElType) else - nothing + jac_prototype = nothing + W_prototype = nothing end M = calculate_massmatrix(sys) @@ -664,7 +666,8 @@ function DiffEqBase.SDEFunction{iip, specialize}(sys::SDESystem, dvs = unknowns( jac = _jac === nothing ? nothing : _jac, tgrad = _tgrad === nothing ? nothing : _tgrad, mass_matrix = _M, - jac_prototype = jac_prototype, + jac_prototype = W_prototype, + W_prototype = W_prototype, observed = observedfun, sparsity = sparsity ? jacobian_sparsity(sys) : nothing, analytic = analytic, From 1e6b29606960b4a7cb8c11b3c33cdd63188e903e Mon Sep 17 00:00:00 2001 From: vyudu Date: Thu, 3 Apr 2025 13:08:24 -0400 Subject: [PATCH 1261/2176] fix: check actual sparsity pattern --- src/systems/diffeqs/abstractodesystem.jl | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/systems/diffeqs/abstractodesystem.jl b/src/systems/diffeqs/abstractodesystem.jl index a231db040c..63aa97ac20 100644 --- a/src/systems/diffeqs/abstractodesystem.jl +++ b/src/systems/diffeqs/abstractodesystem.jl @@ -132,9 +132,10 @@ end function assert_jac_length_header(sys) W = W_sparsity(sys) - + (W_Is, W_Js, W_Vs) = findnz(W) identity, expr -> Func([expr.args...], [], LiteralExpr(quote - @assert nnz($(expr.args[1])) == nnz(W) + J_Is, J_Js, J_Vs = $(findnz)($(expr.args[1])) + @assert (J_Is, J_Js) == ($W_Is, $W_Js) expr.body end)) end From 9c05aa4999826fedd3d99e1798cab1be31a7f951 Mon Sep 17 00:00:00 2001 From: vyudu Date: Thu, 3 Apr 2025 13:28:22 -0400 Subject: [PATCH 1262/2176] test: test W_sparsity --- src/systems/diffeqs/abstractodesystem.jl | 6 ++---- test/jacobiansparsity.jl | 25 +++++++++++++++++++++++- 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/src/systems/diffeqs/abstractodesystem.jl b/src/systems/diffeqs/abstractodesystem.jl index 63aa97ac20..911762c765 100644 --- a/src/systems/diffeqs/abstractodesystem.jl +++ b/src/systems/diffeqs/abstractodesystem.jl @@ -132,10 +132,8 @@ end function assert_jac_length_header(sys) W = W_sparsity(sys) - (W_Is, W_Js, W_Vs) = findnz(W) identity, expr -> Func([expr.args...], [], LiteralExpr(quote - J_Is, J_Js, J_Vs = $(findnz)($(expr.args[1])) - @assert (J_Is, J_Js) == ($W_Is, $W_Js) + @assert $(findnz)($(expr.args[1]))[1:2] == $(findnz)($W)[1:2] expr.body end)) end @@ -294,7 +292,7 @@ end function W_sparsity(sys::AbstractODESystem) jac_sparsity = jacobian_sparsity(sys) - M_sparsity = sparse(iszero.(calculate_massmatrix(sys))) + M_sparsity = sparse((!iszero).(calculate_massmatrix(sys))) jac_sparsity .|| M_sparsity end diff --git a/test/jacobiansparsity.jl b/test/jacobiansparsity.jl index 24d47236b3..ef4a38c14f 100644 --- a/test/jacobiansparsity.jl +++ b/test/jacobiansparsity.jl @@ -1,4 +1,4 @@ -using OrdinaryDiffEq, ModelingToolkit, Test, SparseArrays +using OrdinaryDiffEq, ModelingToolkit, SparseArrays N = 3 xyd_brusselator = range(0, stop = 1, length = N) @@ -82,3 +82,26 @@ prob = ODEProblem(sys, u0, (0, 11.5), sparse = true, jac = false) prob = ODEProblem(sys, u0, (0, 11.5), sparse = true, jac = true) @test eltype(prob.f.jac_prototype) == Float32 + +@testset "W matrix sparsity" begin + @parameters g + @variables x(t) y(t) [state_priority = 10] λ(t) + eqs = [D(D(x)) ~ λ * x + D(D(y)) ~ λ * y - g + x^2 + y^2 ~ 1] + @mtkbuild pend = ODESystem(eqs, t) + + u0 = [x => 1, y => 0] + prob = ODEProblem(pend, u0, (0, 11.5), [g => 1], guesses = [λ => 1], sparse = true, jac = true) + jac, jac! = generate_jacobian(pend; expression = Val{false}, sparse = true) + jac_prototype = ModelingToolkit.jacobian_sparsity(pend) + W_prototype = ModelingToolkit.W_sparsity(pend) + @test nnz(W_prototype) == nnz(jac_prototype) + 2 + + @test findnz(prob.f.jac_prototype)[1:2] == findnz(W_prototype)[1:2] + + u = zeros(5) + p = prob.p + t = 0.0 + @test_throws AssertionError jac!(similar(jac_prototype, Float64), u, p, t) +end From 47c1418d0bcb7b95bd1540db77743f61aaa0ea1f Mon Sep 17 00:00:00 2001 From: vyudu Date: Thu, 3 Apr 2025 13:37:50 -0400 Subject: [PATCH 1263/2176] fix tests --- src/systems/diffeqs/abstractodesystem.jl | 7 +++++-- test/jacobiansparsity.jl | 7 ++++--- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/src/systems/diffeqs/abstractodesystem.jl b/src/systems/diffeqs/abstractodesystem.jl index 911762c765..199c1de9bb 100644 --- a/src/systems/diffeqs/abstractodesystem.jl +++ b/src/systems/diffeqs/abstractodesystem.jl @@ -134,7 +134,7 @@ function assert_jac_length_header(sys) W = W_sparsity(sys) identity, expr -> Func([expr.args...], [], LiteralExpr(quote @assert $(findnz)($(expr.args[1]))[1:2] == $(findnz)($W)[1:2] - expr.body + $(expr.body) end)) end @@ -292,7 +292,10 @@ end function W_sparsity(sys::AbstractODESystem) jac_sparsity = jacobian_sparsity(sys) - M_sparsity = sparse((!iszero).(calculate_massmatrix(sys))) + (n, n) = size(jac_sparsity) + + M = calculate_massmatrix(sys) + M_sparsity = M isa UniformScaling ? sparse(I(n)) : sparse((!iszero).(M)) jac_sparsity .|| M_sparsity end diff --git a/test/jacobiansparsity.jl b/test/jacobiansparsity.jl index ef4a38c14f..28f8c66dc0 100644 --- a/test/jacobiansparsity.jl +++ b/test/jacobiansparsity.jl @@ -3,15 +3,15 @@ using OrdinaryDiffEq, ModelingToolkit, SparseArrays N = 3 xyd_brusselator = range(0, stop = 1, length = N) brusselator_f(x, y, t) = (((x - 0.3)^2 + (y - 0.6)^2) <= 0.1^2) * (t >= 1.1) * 5.0 -limit(a, N) = ModelingToolkit.ifelse(a == N + 1, 1, ModelingToolkit.ifelse(a == 0, N, a)) +lim(a, N) = ModelingToolkit.ifelse(a == N + 1, 1, ModelingToolkit.ifelse(a == 0, N, a)) function brusselator_2d_loop(du, u, p, t) A, B, alpha, dx = p alpha = alpha / dx^2 @inbounds for I in CartesianIndices((N, N)) i, j = Tuple(I) x, y = xyd_brusselator[I[1]], xyd_brusselator[I[2]] - ip1, im1, jp1, jm1 = limit(i + 1, N), limit(i - 1, N), limit(j + 1, N), - limit(j - 1, N) + ip1, im1, jp1, jm1 = lim(i + 1, N), lim(i - 1, N), lim(j + 1, N), + lim(j - 1, N) du[i, j, 1] = alpha * (u[im1, j, 1] + u[ip1, j, 1] + u[i, jp1, 1] + u[i, jm1, 1] - 4u[i, j, 1]) + B + u[i, j, 1]^2 * u[i, j, 2] - (A + 1) * u[i, j, 1] + @@ -84,6 +84,7 @@ prob = ODEProblem(sys, u0, (0, 11.5), sparse = true, jac = true) @test eltype(prob.f.jac_prototype) == Float32 @testset "W matrix sparsity" begin + t = ModelingToolkit.t_nounits @parameters g @variables x(t) y(t) [state_priority = 10] λ(t) eqs = [D(D(x)) ~ λ * x From 92c1cb90e84b7b07c4091dea4b4906b860e96d5d Mon Sep 17 00:00:00 2001 From: vyudu Date: Thu, 3 Apr 2025 16:24:35 -0400 Subject: [PATCH 1264/2176] fix: fix generate_W --- src/ModelingToolkit.jl | 2 +- src/structural_transformation/symbolics_tearing.jl | 5 +++++ src/systems/diffeqs/abstractodesystem.jl | 5 +++++ test/jacobiansparsity.jl | 8 +++++++- 4 files changed, 18 insertions(+), 2 deletions(-) diff --git a/src/ModelingToolkit.jl b/src/ModelingToolkit.jl index b53d3ec098..bd77abfbb9 100644 --- a/src/ModelingToolkit.jl +++ b/src/ModelingToolkit.jl @@ -303,7 +303,7 @@ export structural_simplify, expand_connections, linearize, linearization_functio LinearizationProblem export solve -export calculate_jacobian, generate_jacobian, generate_function, generate_custom_function +export calculate_jacobian, generate_jacobian, generate_function, generate_custom_function, generate_W export calculate_control_jacobian, generate_control_jacobian export calculate_tgrad, generate_tgrad export calculate_gradient, generate_gradient diff --git a/src/structural_transformation/symbolics_tearing.jl b/src/structural_transformation/symbolics_tearing.jl index 6845351008..ea17b0d005 100644 --- a/src/structural_transformation/symbolics_tearing.jl +++ b/src/structural_transformation/symbolics_tearing.jl @@ -378,6 +378,7 @@ function generate_derivative_variables!( # For variable x, make dummy derivative x_t if the # derivative is in the system for v in 1:length(var_to_diff) + @show graph dv = var_to_diff[v] dv isa Int || continue solved = var_eq_matching[dv] isa Int @@ -403,6 +404,10 @@ function generate_derivative_variables!( v_t = add_dd_variable!(structure, fullvars, x_t, dv) # Add `D(x) - x_t ~ 0` to the graph dummy_eq = add_dd_equation!(structure, neweqs, 0 ~ dx - x_t, dv, v_t) + # Update graph to say, all the equations featuring D(x) also feature x_t + for e in 𝑑neighbors(graph, dv) + add_edge!(graph, e, v_t) + end # Update matching push!(var_eq_matching, unassigned) diff --git a/src/systems/diffeqs/abstractodesystem.jl b/src/systems/diffeqs/abstractodesystem.jl index 199c1de9bb..df3eb6cf92 100644 --- a/src/systems/diffeqs/abstractodesystem.jl +++ b/src/systems/diffeqs/abstractodesystem.jl @@ -143,15 +143,20 @@ function generate_W(sys::AbstractODESystem, γ = 1., dvs = unknowns(sys), simplify = false, sparse = false, kwargs...) @variables ˍ₋gamma M = calculate_massmatrix(sys; simplify) + sparse && (M = SparseArrays.sparse(M)) J = calculate_jacobian(sys; simplify, sparse, dvs) W = ˍ₋gamma*M + J + @show W p = reorder_parameters(sys, ps) + @show length(p) return build_function_wrapper(sys, W, dvs, p..., ˍ₋gamma, get_iv(sys); + wrap_code = sparse ? assert_jac_length_header(sys) : (identity, identity), + p_end = 1 + length(p), kwargs...) end diff --git a/test/jacobiansparsity.jl b/test/jacobiansparsity.jl index 28f8c66dc0..ced723507b 100644 --- a/test/jacobiansparsity.jl +++ b/test/jacobiansparsity.jl @@ -86,7 +86,7 @@ prob = ODEProblem(sys, u0, (0, 11.5), sparse = true, jac = true) @testset "W matrix sparsity" begin t = ModelingToolkit.t_nounits @parameters g - @variables x(t) y(t) [state_priority = 10] λ(t) + @variables x(t) y(t) λ(t) eqs = [D(D(x)) ~ λ * x D(D(y)) ~ λ * y - g x^2 + y^2 ~ 1] @@ -105,4 +105,10 @@ prob = ODEProblem(sys, u0, (0, 11.5), sparse = true, jac = true) p = prob.p t = 0.0 @test_throws AssertionError jac!(similar(jac_prototype, Float64), u, p, t) + + W, W! = generate_W(pend; expression = Val{false}, sparse = true) + γ = .1 + M = sparse(calculate_massmatrix(pend)) + @test_throws AssertionError W!(similar(jac_prototype, Float64), u, p, γ, t) + @test W!(similar(W_prototype, Float64), u, p, γ, t) == 0.1 * M + jac!(similar(W_prototype, Float64), u, p, t) end From 1d3da35f166ef46dfdbe9c1c23e2efa688cdd612 Mon Sep 17 00:00:00 2001 From: vyudu Date: Thu, 3 Apr 2025 17:01:10 -0400 Subject: [PATCH 1265/2176] fix --- src/linearization.jl | 1 - src/structural_transformation/symbolics_tearing.jl | 1 - src/systems/diffeqs/abstractodesystem.jl | 2 -- src/systems/diffeqs/odesystem.jl | 1 - test/jacobiansparsity.jl | 7 ++++--- 5 files changed, 4 insertions(+), 8 deletions(-) diff --git a/src/linearization.jl b/src/linearization.jl index 57b171e874..77f4422b63 100644 --- a/src/linearization.jl +++ b/src/linearization.jl @@ -535,7 +535,6 @@ function linearize_symbolic(sys::AbstractSystem, inputs, if !iszero(Bs) if !allow_input_derivatives der_inds = findall(vec(any(!iszero, Bs, dims = 1))) - @show typeof(der_inds) error("Input derivatives appeared in expressions (-g_z\\g_u != 0), the following inputs appeared differentiated: $(ModelingToolkit.inputs(sys)[der_inds]). Call `linearize_symbolic` with keyword argument `allow_input_derivatives = true` to allow this and have the returned `B` matrix be of double width ($(2nu)), where the last $nu inputs are the derivatives of the first $nu inputs.") end B = [B [zeros(nx, nu); Bs]] diff --git a/src/structural_transformation/symbolics_tearing.jl b/src/structural_transformation/symbolics_tearing.jl index ea17b0d005..552c6d13c3 100644 --- a/src/structural_transformation/symbolics_tearing.jl +++ b/src/structural_transformation/symbolics_tearing.jl @@ -378,7 +378,6 @@ function generate_derivative_variables!( # For variable x, make dummy derivative x_t if the # derivative is in the system for v in 1:length(var_to_diff) - @show graph dv = var_to_diff[v] dv isa Int || continue solved = var_eq_matching[dv] isa Int diff --git a/src/systems/diffeqs/abstractodesystem.jl b/src/systems/diffeqs/abstractodesystem.jl index df3eb6cf92..d006b8934b 100644 --- a/src/systems/diffeqs/abstractodesystem.jl +++ b/src/systems/diffeqs/abstractodesystem.jl @@ -146,10 +146,8 @@ function generate_W(sys::AbstractODESystem, γ = 1., dvs = unknowns(sys), sparse && (M = SparseArrays.sparse(M)) J = calculate_jacobian(sys; simplify, sparse, dvs) W = ˍ₋gamma*M + J - @show W p = reorder_parameters(sys, ps) - @show length(p) return build_function_wrapper(sys, W, dvs, p..., diff --git a/src/systems/diffeqs/odesystem.jl b/src/systems/diffeqs/odesystem.jl index 57d71b18c8..599ea06b7b 100644 --- a/src/systems/diffeqs/odesystem.jl +++ b/src/systems/diffeqs/odesystem.jl @@ -323,7 +323,6 @@ function ODESystem(deqs::AbstractVector{<:Equation}, iv, dvs, ps; cons = get_constraintsystem(sys) cons !== nothing && push!(conssystems, cons) end - @show conssystems @set! constraintsystem.systems = conssystems end diff --git a/test/jacobiansparsity.jl b/test/jacobiansparsity.jl index ced723507b..1668788375 100644 --- a/test/jacobiansparsity.jl +++ b/test/jacobiansparsity.jl @@ -1,4 +1,4 @@ -using OrdinaryDiffEq, ModelingToolkit, SparseArrays +using ModelingToolkit, SparseArrays#, OrdinaryDiffEq N = 3 xyd_brusselator = range(0, stop = 1, length = N) @@ -51,7 +51,7 @@ JP = prob.f.jac_prototype # test sparse jacobian prob = ODEProblem(sys, u0, (0, 11.5), sparse = true, jac = true) -@test_nowarn solve(prob, Rosenbrock23()) +#@test_nowarn solve(prob, Rosenbrock23()) @test findnz(calculate_jacobian(sys, sparse = true))[1:2] == findnz(prob.f.jac_prototype)[1:2] @@ -74,7 +74,7 @@ f = DiffEqBase.ODEFunction(sys, u0 = nothing, sparse = true, jac = false) # test when u0 is not Float64 u0 = similar(init_brusselator_2d(xyd_brusselator), Float32) prob_ode_brusselator_2d = ODEProblem(brusselator_2d_loop, - u0, (0.0, 11.5), p) + u0, (0.0, 11.5), p) sys = complete(modelingtoolkitize(prob_ode_brusselator_2d)) prob = ODEProblem(sys, u0, (0, 11.5), sparse = true, jac = false) @@ -85,6 +85,7 @@ prob = ODEProblem(sys, u0, (0, 11.5), sparse = true, jac = true) @testset "W matrix sparsity" begin t = ModelingToolkit.t_nounits + D = ModelingToolkit.D_nounits @parameters g @variables x(t) y(t) λ(t) eqs = [D(D(x)) ~ λ * x From d2e952f703a1ab75df2cee68ff9452864ee35ea7 Mon Sep 17 00:00:00 2001 From: vyudu Date: Thu, 3 Apr 2025 18:26:06 -0400 Subject: [PATCH 1266/2176] fix: sdesystems don't have W_prototype --- src/systems/diffeqs/sdesystem.jl | 1 - 1 file changed, 1 deletion(-) diff --git a/src/systems/diffeqs/sdesystem.jl b/src/systems/diffeqs/sdesystem.jl index 2f84b70dcd..78b7a02195 100644 --- a/src/systems/diffeqs/sdesystem.jl +++ b/src/systems/diffeqs/sdesystem.jl @@ -667,7 +667,6 @@ function DiffEqBase.SDEFunction{iip, specialize}(sys::SDESystem, dvs = unknowns( tgrad = _tgrad === nothing ? nothing : _tgrad, mass_matrix = _M, jac_prototype = W_prototype, - W_prototype = W_prototype, observed = observedfun, sparsity = sparsity ? jacobian_sparsity(sys) : nothing, analytic = analytic, From 7f37ea3179920bd2a8b0791ad5615ae2e6532447 Mon Sep 17 00:00:00 2001 From: vyudu Date: Fri, 4 Apr 2025 13:24:12 -0400 Subject: [PATCH 1267/2176] fix: SDE has no tearing state --- src/systems/diffeqs/sdesystem.jl | 25 +++++++++---------------- test/jacobiansparsity.jl | 3 ++- test/nonlinearsystem.jl | 2 +- 3 files changed, 12 insertions(+), 18 deletions(-) diff --git a/src/systems/diffeqs/sdesystem.jl b/src/systems/diffeqs/sdesystem.jl index 78b7a02195..605e340aa5 100644 --- a/src/systems/diffeqs/sdesystem.jl +++ b/src/systems/diffeqs/sdesystem.jl @@ -642,20 +642,16 @@ function DiffEqBase.SDEFunction{iip, specialize}(sys::SDESystem, dvs = unknowns( _Wfact, _Wfact_t = nothing, nothing end + M = calculate_massmatrix(sys) if sparse uElType = u0 === nothing ? Float64 : eltype(u0) - if jac - jac_prototype = similar(calculate_jacobian(sys; sparse), uElType) - else - jac_prototype = similar(jacobian_sparsity(sys), uElType) - end - W_prototype = similar(W_sparsity(sys), uElType) + jac_prototype = similar(calculate_jacobian(sys; sparse), uElType) + W_prototype = similar(jac_prototype .+ M, uElType) else jac_prototype = nothing W_prototype = nothing end - M = calculate_massmatrix(sys) _M = (u0 === nothing || M == I) ? M : ArrayInterface.restructure(u0 .* u0', M) observedfun = ObservedFunctionCache( @@ -742,15 +738,14 @@ function SDEFunctionExpr{iip}(sys::SDESystem, dvs = unknowns(sys), _jac = :nothing end - jac_prototype = if sparse + M = calculate_massmatrix(sys) + if sparse uElType = u0 === nothing ? Float64 : eltype(u0) - if jac - similar(calculate_jacobian(sys, sparse = sparse), uElType) - else - similar(jacobian_sparsity(sys), uElType) - end + jac_prototype = similar(calculate_jacobian(sys; sparse), uElType) + W_prototype = similar(jac_prototype .+ M, uElType) else - nothing + jac_prototype = nothing + W_prototype = nothing end if Wfact @@ -763,8 +758,6 @@ function SDEFunctionExpr{iip}(sys::SDESystem, dvs = unknowns(sys), _Wfact, _Wfact_t = :nothing, :nothing end - M = calculate_massmatrix(sys) - _M = (u0 === nothing || M == I) ? M : ArrayInterface.restructure(u0 .* u0', M) ex = quote diff --git a/test/jacobiansparsity.jl b/test/jacobiansparsity.jl index 1668788375..f6f9853137 100644 --- a/test/jacobiansparsity.jl +++ b/test/jacobiansparsity.jl @@ -1,4 +1,4 @@ -using ModelingToolkit, SparseArrays#, OrdinaryDiffEq +using ModelingToolkit, SparseArrays, OrdinaryDiffEq N = 3 xyd_brusselator = range(0, stop = 1, length = N) @@ -100,6 +100,7 @@ prob = ODEProblem(sys, u0, (0, 11.5), sparse = true, jac = true) W_prototype = ModelingToolkit.W_sparsity(pend) @test nnz(W_prototype) == nnz(jac_prototype) + 2 + # jac_prototype should be the same as W_prototype @test findnz(prob.f.jac_prototype)[1:2] == findnz(W_prototype)[1:2] u = zeros(5) diff --git a/test/nonlinearsystem.jl b/test/nonlinearsystem.jl index 9475f24006..a315371141 100644 --- a/test/nonlinearsystem.jl +++ b/test/nonlinearsystem.jl @@ -30,7 +30,7 @@ eqs = [0 ~ σ * (y - x) * h, @test eval(toexpr(ns)) == ns test_nlsys_inference("standard", ns, (x, y, z), (σ, ρ, β)) @test begin - f = eval(generate_function(ns, [x, y, z], [σ, ρ, β])[2]) + f = generate_function(ns, [x, y, z], [σ, ρ, β], expression = Val{false})[2] du = [0.0, 0.0, 0.0] f(du, [1, 2, 3], [1, 2, 3]) du ≈ [1, -3, -7] From 15453aab9713bb87414c6ec96e72be405a5110c3 Mon Sep 17 00:00:00 2001 From: vyudu Date: Fri, 4 Apr 2025 13:31:37 -0400 Subject: [PATCH 1268/2176] fix: fix tests --- test/discrete_system.jl | 25 ++++++--------- test/nonlinearsystem.jl | 2 +- test/odesystem.jl | 70 +++++++++++++++++++---------------------- 3 files changed, 44 insertions(+), 53 deletions(-) diff --git a/test/discrete_system.jl b/test/discrete_system.jl index 874d045aa9..1afd43c417 100644 --- a/test/discrete_system.jl +++ b/test/discrete_system.jl @@ -34,21 +34,16 @@ eqs = [S ~ S(k - 1) - infection * h, syss = structural_simplify(sys) @test syss == syss -for df in [ - DiscreteFunction(syss), - eval(DiscreteFunctionExpr(syss)) -] - - # iip - du = zeros(3) - u = collect(1:3) - p = MTKParameters(syss, [c, nsteps, δt, β, γ] .=> collect(1:5)) - df.f(du, u, p, 0) - @test du ≈ [0.01831563888873422, 0.9816849729159067, 4.999999388195359] - - # oop - @test df.f(u, p, 0) ≈ [0.01831563888873422, 0.9816849729159067, 4.999999388195359] -end +df = DiscreteFunction(ssys) +# iip +du = zeros(3) +u = collect(1:3) +p = MTKParameters(syss, [c, nsteps, δt, β, γ] .=> collect(1:5)) +df.f(du, u, p, 0) +@test du ≈ [0.01831563888873422, 0.9816849729159067, 4.999999388195359] + +# oop +@test df.f(u, p, 0) ≈ [0.01831563888873422, 0.9816849729159067, 4.999999388195359] # Problem u0 = [S(k - 1) => 990.0, I(k - 1) => 10.0, R(k - 1) => 0.0] diff --git a/test/nonlinearsystem.jl b/test/nonlinearsystem.jl index 9475f24006..a315371141 100644 --- a/test/nonlinearsystem.jl +++ b/test/nonlinearsystem.jl @@ -30,7 +30,7 @@ eqs = [0 ~ σ * (y - x) * h, @test eval(toexpr(ns)) == ns test_nlsys_inference("standard", ns, (x, y, z), (σ, ρ, β)) @test begin - f = eval(generate_function(ns, [x, y, z], [σ, ρ, β])[2]) + f = generate_function(ns, [x, y, z], [σ, ρ, β], expression = Val{false})[2] du = [0.0, 0.0, 0.0] f(du, [1, 2, 3], [1, 2, 3]) du ≈ [1, -3, -7] diff --git a/test/odesystem.jl b/test/odesystem.jl index 78218c5107..e6945891de 100644 --- a/test/odesystem.jl +++ b/test/odesystem.jl @@ -54,42 +54,38 @@ jac = calculate_jacobian(de) jacfun = eval(jac_expr[2]) de = complete(de) -for f in [ - ODEFunction(de, [x, y, z], [σ, ρ, β], tgrad = true, jac = true), - eval(ODEFunctionExpr(de, [x, y, z], [σ, ρ, β], tgrad = true, jac = true)) -] - # system - @test f.sys === de - - # iip - du = zeros(3) - u = collect(1:3) - p = ModelingToolkit.MTKParameters(de, [σ, ρ, β] .=> 4.0:6.0) - f.f(du, u, p, 0.1) - @test du == [4, 0, -16] - - # oop - du = @SArray zeros(3) - u = SVector(1:3...) - p = ModelingToolkit.MTKParameters(de, SVector{3}([σ, ρ, β] .=> 4.0:6.0)) - @test f.f(u, p, 0.1) === @SArray [4.0, 0.0, -16.0] - - # iip vs oop - du = zeros(3) - g = similar(du) - J = zeros(3, 3) - u = collect(1:3) - p = ModelingToolkit.MTKParameters(de, [σ, ρ, β] .=> 4.0:6.0) - f.f(du, u, p, 0.1) - @test du == f(u, p, 0.1) - f.tgrad(g, u, p, t) - @test g == f.tgrad(u, p, t) - f.jac(J, u, p, t) - @test J == f.jac(u, p, t) -end +f = ODEFunction(de, [x, y, z], [σ, ρ, β], tgrad = true, jac = true), +# system +@test f.sys === de + +# iip +du = zeros(3) +u = collect(1:3) +p = ModelingToolkit.MTKParameters(de, [σ, ρ, β] .=> 4.0:6.0) +f.f(du, u, p, 0.1) +@test du == [4, 0, -16] + +# oop +du = @SArray zeros(3) +u = SVector(1:3...) +p = ModelingToolkit.MTKParameters(de, SVector{3}([σ, ρ, β] .=> 4.0:6.0)) +@test f.f(u, p, 0.1) === @SArray [4.0, 0.0, -16.0] + +# iip vs oop +du = zeros(3) +g = similar(du) +J = zeros(3, 3) +u = collect(1:3) +p = ModelingToolkit.MTKParameters(de, [σ, ρ, β] .=> 4.0:6.0) +f.f(du, u, p, 0.1) +@test du == f(u, p, 0.1) +f.tgrad(g, u, p, t) +@test g == f.tgrad(u, p, t) +f.jac(J, u, p, t) +@test J == f.jac(u, p, t) #check iip_config -f = eval(ODEFunctionExpr(de, [x, y, z], [σ, ρ, β], iip_config = (false, true))) +f = ODEFunction(de, [x, y, z], [σ, ρ, β], iip_config = (false, true)) du = zeros(3) u = collect(1:3) p = ModelingToolkit.MTKParameters(de, [σ, ρ, β] .=> 4.0:6.0) @@ -146,7 +142,7 @@ eqs = [D(x) ~ σ′ * (y - x), @named de = ODESystem(eqs, t) test_diffeq_inference("global iv-varying", de, t, (x, y, z), (σ′, ρ, β)) -f = eval(generate_function(de, [x, y, z], [σ′, ρ, β])[2]) +f = generate_function(de, [x, y, z], [σ′, ρ, β], expression = Val{false})[2] du = [0.0, 0.0, 0.0] f(du, [1.0, 2.0, 3.0], [x -> x + 7, 2, 3], 5.0) @test du ≈ [11, -3, -7] @@ -157,7 +153,7 @@ eqs = [D(x) ~ σ(t - 1) * (y - x), D(z) ~ x * y - β * z * κ] @named de = ODESystem(eqs, t) test_diffeq_inference("single internal iv-varying", de, t, (x, y, z), (σ, ρ, β)) -f = eval(generate_function(de, [x, y, z], [σ, ρ, β])[2]) +f = generate_function(de, [x, y, z], [σ, ρ, β], expression = Val{false})[2] du = [0.0, 0.0, 0.0] f(du, [1.0, 2.0, 3.0], [x -> x + 7, 2, 3], 5.0) @test du ≈ [11, -3, -7] @@ -165,7 +161,7 @@ f(du, [1.0, 2.0, 3.0], [x -> x + 7, 2, 3], 5.0) eqs = [D(x) ~ x + 10σ(t - 1) + 100σ(t - 2) + 1000σ(t^2)] @named de = ODESystem(eqs, t) test_diffeq_inference("many internal iv-varying", de, t, (x,), (σ,)) -f = eval(generate_function(de, [x], [σ])[2]) +f = generate_function(de, [x], [σ], expression = Val{false})[2] du = [0.0] f(du, [1.0], [t -> t + 2], 5.0) @test du ≈ [27561] From db030e46bf8b59f67e499ef9c1ea2dbce765ce62 Mon Sep 17 00:00:00 2001 From: vyudu Date: Fri, 4 Apr 2025 17:00:32 -0400 Subject: [PATCH 1269/2176] fix: comma --- test/odesystem.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/odesystem.jl b/test/odesystem.jl index e6945891de..be5fb32b7e 100644 --- a/test/odesystem.jl +++ b/test/odesystem.jl @@ -54,7 +54,7 @@ jac = calculate_jacobian(de) jacfun = eval(jac_expr[2]) de = complete(de) -f = ODEFunction(de, [x, y, z], [σ, ρ, β], tgrad = true, jac = true), +f = ODEFunction(de, [x, y, z], [σ, ρ, β], tgrad = true, jac = true) # system @test f.sys === de From 400dbb43d3bf79b71030a07741b73b40f526df6a Mon Sep 17 00:00:00 2001 From: vyudu Date: Fri, 4 Apr 2025 17:04:32 -0400 Subject: [PATCH 1270/2176] fix: remove bad broadcast --- src/systems/diffeqs/sdesystem.jl | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/systems/diffeqs/sdesystem.jl b/src/systems/diffeqs/sdesystem.jl index 605e340aa5..1d7dd321bc 100644 --- a/src/systems/diffeqs/sdesystem.jl +++ b/src/systems/diffeqs/sdesystem.jl @@ -739,10 +739,12 @@ function SDEFunctionExpr{iip}(sys::SDESystem, dvs = unknowns(sys), end M = calculate_massmatrix(sys) + _M = (u0 === nothing || M == I) ? M : ArrayInterface.restructure(u0 .* u0', M) + if sparse uElType = u0 === nothing ? Float64 : eltype(u0) jac_prototype = similar(calculate_jacobian(sys; sparse), uElType) - W_prototype = similar(jac_prototype .+ M, uElType) + W_prototype = similar(jac_prototype + M, uElType) else jac_prototype = nothing W_prototype = nothing @@ -758,7 +760,6 @@ function SDEFunctionExpr{iip}(sys::SDESystem, dvs = unknowns(sys), _Wfact, _Wfact_t = :nothing, :nothing end - _M = (u0 === nothing || M == I) ? M : ArrayInterface.restructure(u0 .* u0', M) ex = quote f = $f From e21ac76764b7f6d3b662dce39f4d0b71c6f50d1c Mon Sep 17 00:00:00 2001 From: vyudu Date: Fri, 4 Apr 2025 17:05:14 -0400 Subject: [PATCH 1271/2176] up --- src/systems/diffeqs/sdesystem.jl | 1 - 1 file changed, 1 deletion(-) diff --git a/src/systems/diffeqs/sdesystem.jl b/src/systems/diffeqs/sdesystem.jl index 1d7dd321bc..af4273fc22 100644 --- a/src/systems/diffeqs/sdesystem.jl +++ b/src/systems/diffeqs/sdesystem.jl @@ -760,7 +760,6 @@ function SDEFunctionExpr{iip}(sys::SDESystem, dvs = unknowns(sys), _Wfact, _Wfact_t = :nothing, :nothing end - ex = quote f = $f g = $g From 483be933cb8f5b75786bc76cd78c72e530593ba7 Mon Sep 17 00:00:00 2001 From: Christopher Rackauckas Date: Fri, 4 Apr 2025 21:21:59 -0400 Subject: [PATCH 1272/2176] Update test/discrete_system.jl --- test/discrete_system.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/discrete_system.jl b/test/discrete_system.jl index 1afd43c417..0d215052d8 100644 --- a/test/discrete_system.jl +++ b/test/discrete_system.jl @@ -34,7 +34,7 @@ eqs = [S ~ S(k - 1) - infection * h, syss = structural_simplify(sys) @test syss == syss -df = DiscreteFunction(ssys) +df = DiscreteFunction(syss) # iip du = zeros(3) u = collect(1:3) From baf7de67daee654c68bc986960681a39685b7539 Mon Sep 17 00:00:00 2001 From: vyudu Date: Fri, 4 Apr 2025 23:02:32 -0400 Subject: [PATCH 1273/2176] fix a couple mroe cases --- test/odesystem.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/odesystem.jl b/test/odesystem.jl index be5fb32b7e..a45ed99f35 100644 --- a/test/odesystem.jl +++ b/test/odesystem.jl @@ -214,11 +214,11 @@ eqs = [D(x) ~ -A * x, @named de = ODESystem(eqs, t) @test begin local f, du - f = eval(generate_function(de, [x, y], [A, B, C])[2]) + f = generate_function(de, [x, y], [A, B, C], expression = Val{false})[2] du = [0.0, 0.0] f(du, [1.0, 2.0], [1, 2, 3], 0.0) du ≈ [-1, -1 / 3] - f = eval(generate_function(de, [x, y], [A, B, C])[1]) + f = generate_function(de, [x, y], [A, B, C], expression = Val{false})[1] du ≈ f([1.0, 2.0], [1, 2, 3], 0.0) end From be7b8d8d49bc93b3a5e1ce2bc177a1cc91c0b091 Mon Sep 17 00:00:00 2001 From: Christopher Rackauckas Date: Sat, 5 Apr 2025 08:40:04 -0400 Subject: [PATCH 1274/2176] Update steadystatesystems.jl --- test/steadystatesystems.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/steadystatesystems.jl b/test/steadystatesystems.jl index d29c809325..4f1b5ed063 100644 --- a/test/steadystatesystems.jl +++ b/test/steadystatesystems.jl @@ -17,7 +17,7 @@ for factor in [1e-1, 1e0, 1e10], ss_prob = SteadyStateProblem(de, u0, p) sol = solve(ss_prob, SSRootfind()).u[1] @test abs(sol^2 - factor * u0_p[2]) < 1e-8 - ss_prob = SteadyStateProblemExpr(de, u0, p) - sol_expr = solve(eval(ss_prob), SSRootfind()).u[1] + ss_prob = SteadyStateProblem(de, u0, p) + sol_expr = solve(ss_prob, SSRootfind()).u[1] @test all(x -> x == 0, sol - sol_expr) end From 8251a012f12966d9548f8684795fa990a3143020 Mon Sep 17 00:00:00 2001 From: vyudu Date: Sat, 5 Apr 2025 12:07:17 -0400 Subject: [PATCH 1275/2176] useW-sparsity --- src/systems/diffeqs/abstractodesystem.jl | 17 +++++++---------- src/systems/diffeqs/sdesystem.jl | 6 ++---- 2 files changed, 9 insertions(+), 14 deletions(-) diff --git a/src/systems/diffeqs/abstractodesystem.jl b/src/systems/diffeqs/abstractodesystem.jl index d006b8934b..ca9681cd8b 100644 --- a/src/systems/diffeqs/abstractodesystem.jl +++ b/src/systems/diffeqs/abstractodesystem.jl @@ -294,9 +294,13 @@ function jacobian_dae_sparsity(sys::AbstractODESystem) end function W_sparsity(sys::AbstractODESystem) - jac_sparsity = jacobian_sparsity(sys) + if has_tearing_state(sys) + jac_sparsity = jacobian_sparsity(sys) + else + jac = calculate_jacobian(sys; sparse = true) + jac_sparsity = sparse((!iszero).(jac)) + end (n, n) = size(jac_sparsity) - M = calculate_massmatrix(sys) M_sparsity = M isa UniformScaling ? sparse(I(n)) : sparse((!iszero).(M)) jac_sparsity .|| M_sparsity @@ -408,14 +412,8 @@ function DiffEqBase.ODEFunction{iip, specialize}(sys::AbstractODESystem, if sparse uElType = u0 === nothing ? Float64 : eltype(u0) - if jac - jac_prototype = similar(calculate_jacobian(sys; sparse), uElType) - else - jac_prototype = similar(jacobian_sparsity(sys), uElType) - end W_prototype = similar(W_sparsity(sys), uElType) else - jac_prototype = nothing W_prototype = nothing end @@ -427,9 +425,8 @@ function DiffEqBase.ODEFunction{iip, specialize}(sys::AbstractODESystem, tgrad = _tgrad === nothing ? nothing : _tgrad, mass_matrix = _M, jac_prototype = W_prototype, - W_prototype = W_prototype, observed = observedfun, - sparsity = sparsity ? jacobian_sparsity(sys) : nothing, + sparsity = sparsity ? W_sparsity(sys) : nothing, analytic = analytic, initialization_data) end diff --git a/src/systems/diffeqs/sdesystem.jl b/src/systems/diffeqs/sdesystem.jl index af4273fc22..e496b89cfa 100644 --- a/src/systems/diffeqs/sdesystem.jl +++ b/src/systems/diffeqs/sdesystem.jl @@ -645,10 +645,8 @@ function DiffEqBase.SDEFunction{iip, specialize}(sys::SDESystem, dvs = unknowns( M = calculate_massmatrix(sys) if sparse uElType = u0 === nothing ? Float64 : eltype(u0) - jac_prototype = similar(calculate_jacobian(sys; sparse), uElType) - W_prototype = similar(jac_prototype .+ M, uElType) + W_prototype = similar(W_sparsity(sys), uElType) else - jac_prototype = nothing W_prototype = nothing end @@ -664,7 +662,7 @@ function DiffEqBase.SDEFunction{iip, specialize}(sys::SDESystem, dvs = unknowns( mass_matrix = _M, jac_prototype = W_prototype, observed = observedfun, - sparsity = sparsity ? jacobian_sparsity(sys) : nothing, + sparsity = sparsity ? W_sparsity(sys) : nothing, analytic = analytic, Wfact = _Wfact === nothing ? nothing : _Wfact, Wfact_t = _Wfact_t === nothing ? nothing : _Wfact_t, From ade11d39ffa3fefc4bdc2a2eeacd6dd6a9343b8d Mon Sep 17 00:00:00 2001 From: vyudu Date: Sun, 6 Apr 2025 12:38:47 -0400 Subject: [PATCH 1276/2176] use W_sparsity in the SDEFunctionExpr --- src/systems/diffeqs/sdesystem.jl | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/systems/diffeqs/sdesystem.jl b/src/systems/diffeqs/sdesystem.jl index e496b89cfa..2fc9749309 100644 --- a/src/systems/diffeqs/sdesystem.jl +++ b/src/systems/diffeqs/sdesystem.jl @@ -741,10 +741,8 @@ function SDEFunctionExpr{iip}(sys::SDESystem, dvs = unknowns(sys), if sparse uElType = u0 === nothing ? Float64 : eltype(u0) - jac_prototype = similar(calculate_jacobian(sys; sparse), uElType) - W_prototype = similar(jac_prototype + M, uElType) + W_prototype = similar(W_sparsity(sys), uElType) else - jac_prototype = nothing W_prototype = nothing end @@ -763,13 +761,13 @@ function SDEFunctionExpr{iip}(sys::SDESystem, dvs = unknowns(sys), g = $g tgrad = $_tgrad jac = $_jac - jac_prototype = $jac_prototype + W_prototype = $W_prototype Wfact = $_Wfact Wfact_t = $_Wfact_t M = $_M SDEFunction{$iip}(f, g, jac = jac, - jac_prototype = jac_prototype, + jac_prototype = W_prototype, tgrad = tgrad, Wfact = Wfact, Wfact_t = Wfact_t, From 254cd330f28b5c0c4128d4914be2f817e9d10d62 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 2 Apr 2025 16:16:50 +0530 Subject: [PATCH 1277/2176] docs: document `SymScope` variants --- src/systems/abstractsystem.jl | 73 +++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index 1987cb9a60..9bbe05dbeb 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -1114,9 +1114,25 @@ function _apply_to_variables(f::F, ex) where {F} metadata(ex)) end +""" +Variable metadata key which contains information about scoping/namespacing of the +variable in a hierarchical system. +""" abstract type SymScope end +""" + $(TYPEDEF) + +The default scope of a variable. It belongs to the system whose equations it is involved +in and is namespaced by every level of the hierarchy. +""" struct LocalScope <: SymScope end + +""" + $(TYPEDSIGNATURES) + +Apply `LocalScope` to `sym`. +""" function LocalScope(sym::Union{Num, Symbolic, Symbolics.Arr{Num}}) apply_to_variables(sym) do sym if iscall(sym) && operation(sym) === getindex @@ -1130,9 +1146,25 @@ function LocalScope(sym::Union{Num, Symbolic, Symbolics.Arr{Num}}) end end +""" + $(TYPEDEF) + +Denotes that the variable does not belong to the system whose equations it is involved +in. It is not namespaced by this system. In the immediate parent of this system, the +scope of this variable is given by `parent`. + +# Fields + +$(TYPEDFIELDS) +""" struct ParentScope <: SymScope parent::SymScope end +""" + $(TYPEDSIGNATURES) + +Apply `ParentScope` to `sym`, with `parent` being `LocalScope`. +""" function ParentScope(sym::Union{Num, Symbolic, Symbolics.Arr{Num}}) apply_to_variables(sym) do sym if iscall(sym) && operation(sym) === getindex @@ -1148,10 +1180,31 @@ function ParentScope(sym::Union{Num, Symbolic, Symbolics.Arr{Num}}) end end +""" + $(TYPEDEF) + +Denotes that a variable belongs to a system that is at least `N + 1` levels up in the +hierarchy from the system whose equations it is involved in. It is namespaced by the +first `N` parents and not namespaced by the `N+1`th parent in the hierarchy. The scope +of the variable after this point is given by `parent`. + +In other words, this scope delays applying `ParentScope` by `N` levels, and applies +`LocalScope` in the meantime. + +# Fields + +$(TYPEDFIELDS) +""" struct DelayParentScope <: SymScope parent::SymScope N::Int end + +""" + $(TYPEDSIGNATURES) + +Apply `DelayParentScope` to `sym`, with a delay of `N` and `parent` being `LocalScope`. +""" function DelayParentScope(sym::Union{Num, Symbolic, Symbolics.Arr{Num}}, N) apply_to_variables(sym) do sym if iscall(sym) && operation(sym) == getindex @@ -1166,9 +1219,29 @@ function DelayParentScope(sym::Union{Num, Symbolic, Symbolics.Arr{Num}}, N) end end end + +""" + $(TYPEDSIGNATURES) + +Apply `DelayParentScope` to `sym`, with a delay of `1` and `parent` being `LocalScope`. +""" DelayParentScope(sym::Union{Num, Symbolic, Symbolics.Arr{Num}}) = DelayParentScope(sym, 1) +""" + $(TYPEDEF) + +Denotes that a variable belongs to the root system in the hierarchy, regardless of which +equations of subsystems in the hierarchy it is involved in. Variables with this scope +are never namespaced and only added to the unknowns/parameters of a system when calling +`complete` or `structural_simplify`. +""" struct GlobalScope <: SymScope end + +""" + $(TYPEDSIGNATURES) + +Apply `GlobalScope` to `sym`. +""" function GlobalScope(sym::Union{Num, Symbolic, Symbolics.Arr{Num}}) apply_to_variables(sym) do sym if iscall(sym) && operation(sym) == getindex From 96285da14a0118c387ebc52fbe3737a562de2b08 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 2 Apr 2025 16:17:29 +0530 Subject: [PATCH 1278/2176] fix: fix incorrect namespacing of `DelayParentScope` variables in `collect_scoped_vars!` --- src/utils.jl | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/src/utils.jl b/src/utils.jl index 2a0009b644..1884a91c19 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -535,7 +535,7 @@ recursively searches through all subsystems of `sys`, increasing the depth if it """ function collect_scoped_vars!(unknowns, parameters, sys, iv; depth = 1, op = Differential) if has_eqs(sys) - for eq in get_eqs(sys) + for eq in equations(sys) eqtype_supports_collect_vars(eq) || continue if eq isa Equation eq.lhs isa Union{Symbolic, Number} || continue @@ -544,7 +544,7 @@ function collect_scoped_vars!(unknowns, parameters, sys, iv; depth = 1, op = Dif end end if has_parameter_dependencies(sys) - for eq in get_parameter_dependencies(sys) + for eq in parameter_dependencies(sys) if eq isa Pair collect_vars!(unknowns, parameters, eq, iv; depth, op) else @@ -553,17 +553,13 @@ function collect_scoped_vars!(unknowns, parameters, sys, iv; depth = 1, op = Dif end end if has_constraints(sys) - for eq in get_constraints(sys) + for eq in constraints(sys) eqtype_supports_collect_vars(eq) || continue collect_vars!(unknowns, parameters, eq, iv; depth, op) end end if has_op(sys) - collect_vars!(unknowns, parameters, get_op(sys), iv; depth, op) - end - newdepth = depth == -1 ? depth : depth + 1 - for ssys in get_systems(sys) - collect_scoped_vars!(unknowns, parameters, ssys, iv; depth = newdepth, op) + collect_vars!(unknowns, parameters, objective(sys), iv; depth, op) end end @@ -608,6 +604,7 @@ end function collect_var!(unknowns, parameters, var, iv; depth = 0) isequal(var, iv) && return nothing check_scope_depth(getmetadata(var, SymScope, LocalScope()), depth) || return nothing + var = setmetadata(var, SymScope, LocalScope()) if iscalledparameter(var) callable = getcalledparameter(var) push!(parameters, callable) @@ -636,7 +633,7 @@ function check_scope_depth(scope, depth) elseif scope isa ParentScope return depth > 0 && check_scope_depth(scope.parent, depth - 1) elseif scope isa DelayParentScope - return depth >= scope.N && check_scope_depth(scope.parent, depth - scope.N) + return depth >= scope.N && check_scope_depth(scope.parent, depth - scope.N - 1) elseif scope isa GlobalScope return depth == -1 end From 520ac393263c8e52a54732d873537647c6d7d154 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 2 Apr 2025 16:17:46 +0530 Subject: [PATCH 1279/2176] test: test proper scoping of `DelayParentScope` during variable discovery --- test/variable_scope.jl | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test/variable_scope.jl b/test/variable_scope.jl index e90f7e6fbf..bd1d3cb0cf 100644 --- a/test/variable_scope.jl +++ b/test/variable_scope.jl @@ -106,12 +106,12 @@ defs = ModelingToolkit.defaults(bar) @variables x1(t) x2(t) x3(t) x4(t) x5(t) x2 = ParentScope(x2) x3 = ParentScope(ParentScope(x3)) -x4 = DelayParentScope(x4, 2) +x4 = DelayParentScope(x4) x5 = GlobalScope(x5) @parameters p1 p2 p3 p4 p5 p2 = ParentScope(p2) p3 = ParentScope(ParentScope(p3)) -p4 = DelayParentScope(p4, 2) +p4 = DelayParentScope(p4) p5 = GlobalScope(p5) @named sys1 = ODESystem([D(x1) ~ p1, D(x2) ~ p2, D(x3) ~ p3, D(x4) ~ p4, D(x5) ~ p5], t) @@ -126,10 +126,10 @@ p5 = GlobalScope(p5) sys3 = sys3 ∘ sys2 @test length(unknowns(sys3)) == 4 @test any(isequal(x3), unknowns(sys3)) -@test any(isequal(x4), unknowns(sys3)) +@test any(isequal(ModelingToolkit.renamespace(sys1, x4)), unknowns(sys3)) @test length(parameters(sys3)) == 4 @test any(isequal(p3), parameters(sys3)) -@test any(isequal(p4), parameters(sys3)) +@test any(isequal(ModelingToolkit.renamespace(sys1, p4)), parameters(sys3)) sys4 = complete(sys3) @test length(unknowns(sys3)) == 4 @test length(parameters(sys4)) == 5 From fd64e1962a4b1493371214565e984ac93c498b2c Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 3 Apr 2025 18:27:51 +0530 Subject: [PATCH 1280/2176] build: bump Symbolics compat --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 8766db8eee..06b20c4552 100644 --- a/Project.toml +++ b/Project.toml @@ -151,7 +151,7 @@ StochasticDelayDiffEq = "1.8.1" StochasticDiffEq = "6.72.1" SymbolicIndexingInterface = "0.3.37" SymbolicUtils = "3.25.1" -Symbolics = "6.36" +Symbolics = "6.37" URIs = "1" UnPack = "0.1, 1.0" Unitful = "1.1" From 115c793c6f141adca30c919993b521c09fff1f11 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 4 Apr 2025 12:17:13 +0530 Subject: [PATCH 1281/2176] test: fix usage of `DelayParentScope` in `JumpSystem` tests --- test/jumpsystem.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/jumpsystem.jl b/test/jumpsystem.jl index d77e37f516..9568990e73 100644 --- a/test/jumpsystem.jl +++ b/test/jumpsystem.jl @@ -381,12 +381,12 @@ let @variables x1(t) x2(t) x3(t) x4(t) x5(t) x2 = ParentScope(x2) x3 = ParentScope(ParentScope(x3)) - x4 = DelayParentScope(x4, 2) + x4 = DelayParentScope(x4) x5 = GlobalScope(x5) @parameters p1 p2 p3 p4 p5 p2 = ParentScope(p2) p3 = ParentScope(ParentScope(p3)) - p4 = DelayParentScope(p4, 2) + p4 = DelayParentScope(p4) p5 = GlobalScope(p5) j1 = ConstantRateJump(p1, [x1 ~ x1 + 1]) From 9890b26489ab81dfbca2a648cd96d9cb2e2e3062 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 7 Apr 2025 13:11:40 +0530 Subject: [PATCH 1282/2176] fix: filter equations in `getvar` --- src/systems/abstractsystem.jl | 1 + 1 file changed, 1 insertion(+) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index 9bbe05dbeb..e19e576d5d 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -1077,6 +1077,7 @@ function getvar(sys::AbstractSystem, name::Symbol; namespace = does_namespacing( if has_eqs(sys) for eq in get_eqs(sys) + eq isa Equation || continue if eq.lhs isa AnalysisPoint && nameof(eq.rhs) == name return namespace ? renamespace(sys, eq.rhs) : eq.rhs end From 5c494c2bfcd4ac39a776be1532da218cea8e777f Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 7 Apr 2025 13:19:03 +0530 Subject: [PATCH 1283/2176] refactor: add depwarn for `DelayParentScope` --- src/systems/abstractsystem.jl | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index e19e576d5d..dfd0c94a5b 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -1207,6 +1207,8 @@ end Apply `DelayParentScope` to `sym`, with a delay of `N` and `parent` being `LocalScope`. """ function DelayParentScope(sym::Union{Num, Symbolic, Symbolics.Arr{Num}}, N) + Base.depwarn( + "`DelayParentScope` is deprecated and will be removed soon", :DelayParentScope) apply_to_variables(sym) do sym if iscall(sym) && operation(sym) == getindex args = arguments(sym) From 5e439edff890b0646e9841c3f93971cd2e09be4f Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 2 Apr 2025 12:06:18 +0530 Subject: [PATCH 1284/2176] fix: don't substitute inside `Initial` in `build_operating_point!` --- src/systems/problem_utils.jl | 4 ++-- test/initial_values.jl | 16 ++++++++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/src/systems/problem_utils.jl b/src/systems/problem_utils.jl index d0ae0d174f..1db153781a 100644 --- a/src/systems/problem_utils.jl +++ b/src/systems/problem_utils.jl @@ -623,12 +623,12 @@ function build_operating_point!(sys::AbstractSystem, end for k in keys(u0map) - v = fixpoint_sub(u0map[k], neithermap) + v = fixpoint_sub(u0map[k], neithermap; operator = Symbolics.Operator) isequal(k, v) && continue u0map[k] = v end for k in keys(pmap) - v = fixpoint_sub(pmap[k], neithermap) + v = fixpoint_sub(pmap[k], neithermap; operator = Symbolics.Operator) isequal(k, v) && continue pmap[k] = v end diff --git a/test/initial_values.jl b/test/initial_values.jl index dbe08962cd..f63333cb2f 100644 --- a/test/initial_values.jl +++ b/test/initial_values.jl @@ -236,3 +236,19 @@ end prob2 = remake(prob; p = p_new) @test prob2.ps[interp] == spline2 end + +@testset "Issue#3523: don't substitute inside initial in `build_operating_point!`" begin + @variables (X(t))[1:2] + @parameters p[1:2] + eqs = [ + 0 ~ p[1] - X[1], + 0 ~ p[2] - X[2] + ] + @named nlsys = NonlinearSystem(eqs) + nlsys = complete(nlsys) + + # Creates the `NonlinearProblem`. + u0 = [X => [1.0, 2.0]] + ps = [p => [4.0, 5.0]] + @test_nowarn NonlinearProblem(nlsys, u0, ps) +end From 4626b5662c0c6dc11ab31dc914dfac4ee517423c Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 2 Apr 2025 16:36:13 +0530 Subject: [PATCH 1285/2176] fix: add `Initial` parameters for derivatives of all continuous variables Used for steady-state initial conditions --- src/systems/abstractsystem.jl | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index 1987cb9a60..a46d2c26a9 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -717,6 +717,11 @@ function add_initialization_parameters(sys::AbstractSystem) push!(all_initialvars, x) end end + + # add derivatives of all variables for steady-state initial conditions + if is_time_dependent(sys) && !(sys isa AbstractDiscreteSystem) + union!(all_initialvars, Differential(get_iv(sys)).(all_initialvars)) + end for eq in parameter_dependencies(sys) is_variable_floatingpoint(eq.lhs) || continue push!(all_initialvars, eq.lhs) From f5f1e6a342fb305fc43835e4a842a9871d1c78d4 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 2 Apr 2025 16:38:00 +0530 Subject: [PATCH 1286/2176] test: test present of steady-state `Initial` parameters --- test/initializationsystem.jl | 41 ++++++++++++++++++++---------------- 1 file changed, 23 insertions(+), 18 deletions(-) diff --git a/test/initializationsystem.jl b/test/initializationsystem.jl index 94a1f4c14f..0f40d4eaf1 100644 --- a/test/initializationsystem.jl +++ b/test/initializationsystem.jl @@ -406,30 +406,35 @@ sol = solve(prob, Tsit5()) @test sol.u[1] == [1.0] # Steady state initialization +@testset "Steady state initialization" begin + @parameters σ ρ β + @variables x(t) y(t) z(t) -@parameters σ ρ β -@variables x(t) y(t) z(t) + eqs = [D(D(x)) ~ σ * (y - x), + D(y) ~ x * (ρ - z) - y, + D(z) ~ x * y - β * z] -eqs = [D(D(x)) ~ σ * (y - x), - D(y) ~ x * (ρ - z) - y, - D(z) ~ x * y - β * z] + @named sys = ODESystem(eqs, t) + sys = structural_simplify(sys) -@named sys = ODESystem(eqs, t) -sys = structural_simplify(sys) + u0 = [D(x) => 2.0, + x => 1.0, + D(y) => 0.0, + z => 0.0] -u0 = [D(x) => 2.0, - x => 1.0, - D(y) => 0.0, - z => 0.0] + p = [σ => 28.0, + ρ => 10.0, + β => 8 / 3] -p = [σ => 28.0, - ρ => 10.0, - β => 8 / 3] + tspan = (0.0, 0.2) + prob_mtk = ODEProblem(sys, u0, tspan, p) + sol = solve(prob_mtk, Tsit5()) + @test sol[x * (ρ - z) - y][1] == 0.0 -tspan = (0.0, 0.2) -prob_mtk = ODEProblem(sys, u0, tspan, p) -sol = solve(prob_mtk, Tsit5()) -@test sol[x * (ρ - z) - y][1] == 0.0 + prob_mtk.ps[Initial(D(y))] = 1.0 + sol = solve(prob_mtk, Tsit5()) + @test sol[x * (ρ - z) - y][1] == 1.0 +end @variables x(t) y(t) z(t) @parameters α=1.5 β=1.0 γ=3.0 δ=1.0 From 50aaaaa732f393750fc250e007526e47fc816788 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 3 Apr 2025 18:30:59 +0530 Subject: [PATCH 1287/2176] test: update test to account for new `Initial` parameters --- test/modelingtoolkitize.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/modelingtoolkitize.jl b/test/modelingtoolkitize.jl index 28ce7b5e01..db99cc91a4 100644 --- a/test/modelingtoolkitize.jl +++ b/test/modelingtoolkitize.jl @@ -460,7 +460,7 @@ sys = modelingtoolkitize(prob) p = [10.0, 28.0, 2.66] sprob = SDEProblem(sdef!, sdeg!, u0, tspan, p) sys = complete(modelingtoolkitize(sprob)) - @test length(ModelingToolkit.defaults(sys)) == 2length(u0) + length(p) + @test length(ModelingToolkit.defaults(sys)) == 3length(u0) + length(p) sprob2 = SDEProblem(sys, [], tspan) truevals = similar(u0) From 6d07e11489fa81f105daa62783004f72d6e1ba4a Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 3 Apr 2025 18:31:33 +0530 Subject: [PATCH 1288/2176] fix: only add `Initial(D(x))` if `iscall(x)` --- src/systems/abstractsystem.jl | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index a46d2c26a9..4ba2a70022 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -720,7 +720,8 @@ function add_initialization_parameters(sys::AbstractSystem) # add derivatives of all variables for steady-state initial conditions if is_time_dependent(sys) && !(sys isa AbstractDiscreteSystem) - union!(all_initialvars, Differential(get_iv(sys)).(all_initialvars)) + D = Differential(get_iv(sys)) + union!(all_initialvars, [D(v) for v in all_initialvars if iscall(v)]) end for eq in parameter_dependencies(sys) is_variable_floatingpoint(eq.lhs) || continue From e9286b96fc0c88e6a4b290d6a5a654ed13e782d3 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 4 Apr 2025 13:49:00 +0530 Subject: [PATCH 1289/2176] test: update test to account for new `Initial` parameters --- test/symbolic_indexing_interface.jl | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/symbolic_indexing_interface.jl b/test/symbolic_indexing_interface.jl index 5979cd71ac..8b3da5fd72 100644 --- a/test/symbolic_indexing_interface.jl +++ b/test/symbolic_indexing_interface.jl @@ -24,7 +24,8 @@ using SciMLStructures: Tunable (odesys,), [x, y, t, ParameterIndex(Tunable(), 1), :x, :y]) == [nothing, nothing, nothing, ParameterIndex(Tunable(), 1), nothing, nothing] @test isequal( - Set(parameter_symbols(odesys)), Set([a, b, Initial(x), Initial(y), Initial(xy)])) + Set(parameter_symbols(odesys)), Set([a, b, Initial(x), Initial(y), Initial(xy), + Initial(D(x)), Initial(D(y)), Initial(D(xy))])) @test all(is_independent_variable.((odesys,), [t, :t])) @test all(.!is_independent_variable.((odesys,), [x, y, a, :x, :y, :a])) @test isequal(independent_variable_symbols(odesys), [t]) From 1bc3e04ae4ee5db0f8f5aaf8a6539b514050de5d Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 4 Apr 2025 13:49:17 +0530 Subject: [PATCH 1290/2176] fix: do not include `Initial` parameters in `toexpr(::AbstractSystem)` --- src/systems/abstractsystem.jl | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index 4ba2a70022..eaadef9be9 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -1930,7 +1930,9 @@ function toexpr(sys::AbstractSystem) end eqs_name = push_eqs!(stmt, full_equations(sys), var2name) - defs_name = push_defaults!(stmt, defaults(sys), var2name) + filtered_defs = filter( + kvp -> !(iscall(kvp[1]) && operation(kvp[1]) isa Initial), defaults(sys)) + defs_name = push_defaults!(stmt, filtered_defs, var2name) obs_name = push_eqs!(stmt, obs, var2name) if sys isa ODESystem From e74e754d51eda3f6f4d5dc8926e549fd007078a8 Mon Sep 17 00:00:00 2001 From: vyudu Date: Mon, 7 Apr 2025 10:03:50 -0400 Subject: [PATCH 1291/2176] fix type instability --- src/systems/diffeqs/abstractodesystem.jl | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/systems/diffeqs/abstractodesystem.jl b/src/systems/diffeqs/abstractodesystem.jl index ca9681cd8b..986aa27508 100644 --- a/src/systems/diffeqs/abstractodesystem.jl +++ b/src/systems/diffeqs/abstractodesystem.jl @@ -298,12 +298,12 @@ function W_sparsity(sys::AbstractODESystem) jac_sparsity = jacobian_sparsity(sys) else jac = calculate_jacobian(sys; sparse = true) - jac_sparsity = sparse((!iszero).(jac)) + jac_sparsity = SparseMatrixCSC{Bool, Int64}((!iszero).(jac)) end (n, n) = size(jac_sparsity) M = calculate_massmatrix(sys) - M_sparsity = M isa UniformScaling ? sparse(I(n)) : sparse((!iszero).(M)) - jac_sparsity .|| M_sparsity + M_sparsity = M isa UniformScaling ? sparse(I(n)) : SparseMatrixCSC{Bool, Int64}((!iszero).(M)) + jac_sparsity .| M_sparsity end function isautonomous(sys::AbstractODESystem) From 5ce7dac0157a8a17df043960a99c2144ff5cc147 Mon Sep 17 00:00:00 2001 From: vyudu Date: Tue, 25 Mar 2025 10:19:42 -0400 Subject: [PATCH 1292/2176] docs: document DAEProblem constructor --- docs/src/systems/ODESystem.md | 1 + src/systems/diffeqs/abstractodesystem.jl | 4 +++- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/docs/src/systems/ODESystem.md b/docs/src/systems/ODESystem.md index 4be0f6e263..24e2952fc5 100644 --- a/docs/src/systems/ODESystem.md +++ b/docs/src/systems/ODESystem.md @@ -62,6 +62,7 @@ jacobian_sparsity ODEFunction(sys::ModelingToolkit.AbstractODESystem, args...) ODEProblem(sys::ModelingToolkit.AbstractODESystem, args...) SteadyStateProblem(sys::ModelingToolkit.AbstractODESystem, args...) +DAEProblem(sys::ModelingToolkit.AbstractODESystem, args...) ``` ## Expression Constructors diff --git a/src/systems/diffeqs/abstractodesystem.jl b/src/systems/diffeqs/abstractodesystem.jl index 23f20b00ec..05aa3fce94 100644 --- a/src/systems/diffeqs/abstractodesystem.jl +++ b/src/systems/diffeqs/abstractodesystem.jl @@ -941,6 +941,8 @@ DiffEqBase.DAEProblem{iip}(sys::AbstractODESystem, du0map, u0map, tspan, Generates a DAEProblem from an ODESystem and allows for automatically symbolically calculating numerical enhancements. + +Note: Solvers for DAEProblems like DFBDF, DImplicitEuler, DABDF2 are generally slower than the ones for ODEProblems. If possible, it is recommended to formulate your problem in terms of an ODEProblem and use the corresponding ODE Solvers. """ function DiffEqBase.DAEProblem(sys::AbstractODESystem, args...; kwargs...) DAEProblem{true}(sys, args...; kwargs...) @@ -951,7 +953,7 @@ function DiffEqBase.DAEProblem{iip}(sys::AbstractODESystem, du0map, u0map, tspan warn_initialize_determined = true, check_length = true, eval_expression = false, eval_module = @__MODULE__, kwargs...) where {iip} if !iscomplete(sys) - error("A completed system is required. Call `complete` or `structural_simplify` on the system before creating a `DAEProblem`") + error("A completed system is required. Call `complete` or `structural_simplify` on the system before creating a `DAEProblem`.") end f, du0, u0, p = process_SciMLProblem(DAEFunction{iip}, sys, u0map, parammap; implicit_dae = true, du0map = du0map, check_length, From c1bca7445f92eb7ffb4c6d2c0ef3ee4f5c16ee0a Mon Sep 17 00:00:00 2001 From: vyudu Date: Tue, 25 Mar 2025 10:20:25 -0400 Subject: [PATCH 1293/2176] fix lines --- src/systems/diffeqs/abstractodesystem.jl | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/systems/diffeqs/abstractodesystem.jl b/src/systems/diffeqs/abstractodesystem.jl index 05aa3fce94..63cbf572ce 100644 --- a/src/systems/diffeqs/abstractodesystem.jl +++ b/src/systems/diffeqs/abstractodesystem.jl @@ -942,7 +942,10 @@ DiffEqBase.DAEProblem{iip}(sys::AbstractODESystem, du0map, u0map, tspan, Generates a DAEProblem from an ODESystem and allows for automatically symbolically calculating numerical enhancements. -Note: Solvers for DAEProblems like DFBDF, DImplicitEuler, DABDF2 are generally slower than the ones for ODEProblems. If possible, it is recommended to formulate your problem in terms of an ODEProblem and use the corresponding ODE Solvers. +Note: Solvers for DAEProblems like DFBDF, DImplicitEuler, DABDF2 are +generally slower than the ones for ODEProblems. If possible, it is +recommended to formulate your problem in terms of an ODEProblem and +use the corresponding ODE Solvers. """ function DiffEqBase.DAEProblem(sys::AbstractODESystem, args...; kwargs...) DAEProblem{true}(sys, args...; kwargs...) From 0f1c669fbbc1cf850c845b7f479584485b4e0309 Mon Sep 17 00:00:00 2001 From: vyudu Date: Mon, 7 Apr 2025 10:09:32 -0400 Subject: [PATCH 1294/2176] adjust docstring --- src/systems/diffeqs/abstractodesystem.jl | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/systems/diffeqs/abstractodesystem.jl b/src/systems/diffeqs/abstractodesystem.jl index 63cbf572ce..f098e437f7 100644 --- a/src/systems/diffeqs/abstractodesystem.jl +++ b/src/systems/diffeqs/abstractodesystem.jl @@ -943,9 +943,8 @@ Generates a DAEProblem from an ODESystem and allows for automatically symbolically calculating numerical enhancements. Note: Solvers for DAEProblems like DFBDF, DImplicitEuler, DABDF2 are -generally slower than the ones for ODEProblems. If possible, it is -recommended to formulate your problem in terms of an ODEProblem and -use the corresponding ODE Solvers. +generally slower than the ones for ODEProblems. We recommend trying +ODEProblem and its solvers for your problem first. """ function DiffEqBase.DAEProblem(sys::AbstractODESystem, args...; kwargs...) DAEProblem{true}(sys, args...; kwargs...) From 64fb10ef625fd5a0cad52d5dd5317645ed3e4e8b Mon Sep 17 00:00:00 2001 From: vyudu Date: Tue, 1 Apr 2025 17:37:13 -0400 Subject: [PATCH 1295/2176] init: add cost and coalesce --- src/systems/diffeqs/odesystem.jl | 63 +++++++++++++++++++++++++++----- 1 file changed, 54 insertions(+), 9 deletions(-) diff --git a/src/systems/diffeqs/odesystem.jl b/src/systems/diffeqs/odesystem.jl index 57d71b18c8..35f132f12f 100644 --- a/src/systems/diffeqs/odesystem.jl +++ b/src/systems/diffeqs/odesystem.jl @@ -51,6 +51,10 @@ struct ODESystem <: AbstractODESystem observed::Vector{Equation} """System of constraints that must be satisfied by the solution to the system.""" constraintsystem::Union{Nothing, ConstraintsSystem} + """A set of expressions defining the costs of the system for optimal control.""" + costs::Vector + """Takes the cost vector and returns a scalar for optimization.""" + coalesce::Function """ Time-derivative matrix. Note: this field will not be defined until [`calculate_tgrad`](@ref) is called on the system. @@ -338,7 +342,7 @@ function ODESystem(deqs::AbstractVector{<:Equation}, iv, dvs, ps; metadata, gui_metadata, is_dde, tstops, checks = checks) end -function ODESystem(eqs, iv; constraints = Equation[], kwargs...) +function ODESystem(eqs, iv; constraints = Equation[], costs = Equation[], kwargs...) diffvars, allunknowns, ps, eqs = process_equations(eqs, iv) for eq in get(kwargs, :parameter_dependencies, Equation[]) @@ -384,6 +388,13 @@ function ODESystem(eqs, iv; constraints = Equation[], kwargs...) end end + if !isempty(costs) + coststs, costps = process_costs(costs, allunknowns, new_ps, iv) + for p in costps + !in(p, new_ps) && push!(new_ps, p) + end + end + return ODESystem(eqs, iv, collect(Iterators.flatten((diffvars, algevars, consvars))), collect(new_ps); constraintsystem, kwargs...) end @@ -734,22 +745,52 @@ function Base.show(io::IO, mime::MIME"text/plain", sys::ODESystem; hint = true, return nothing end -# Validate that all the variables in the BVP constraints are well-formed states or parameters. -# - Callable/delay variables (e.g. of the form x(0.6) should be unknowns of the system (and have one arg, etc.) -# - Callable/delay parameters should be parameters of the system (and have one arg, etc.) +""" +Build the constraint system for the ODESystem. +""" function process_constraint_system( constraints::Vector{Equation}, sts, ps, iv; consname = :cons) isempty(constraints) && return nothing constraintsts = OrderedSet() constraintps = OrderedSet() - for cons in constraints collect_vars!(constraintsts, constraintps, cons, iv) end # Validate the states. - for var in constraintsts + validate_vars_and_find_ps!(coststs, costps, sts, iv) + + ConstraintsSystem( + constraints, collect(constraintsts), collect(constraintps); name = consname) +end + +""" +Process the costs for the constraint system. +""" +function process_costs(costs::Vector{Equation}, sts, ps, iv) + coststs = OrderedSet() + costps = OrderedSet() + for cost in costs + collect_vars!(coststs, costps, cost, iv) + end + + validate_vars_and_find_ps!(coststs, costps, sts, iv) +end + +""" +Validate that all the variables in an auxiliary system of the ODESystem (constraint or costs) are +well-formed states or parameters. + - Callable/delay variables (e.g. of the form x(0.6) should be unknowns of the system (and have one arg, etc.) + - Callable/delay parameters should be parameters of the system + +Return the set of additional parameters found in the system, e.g. in x(p) ~ 3 then p should be added as a +parameter of the system. +""" +function validate_vars_and_find_ps!(auxvars, auxps, sysvars, iv) + sts = sysvars + + for var in auxvars if !iscall(var) occursin(iv, var) && (var ∈ sts || throw(ArgumentError("Time-dependent variable $var is not an unknown of the system."))) @@ -764,13 +805,17 @@ function process_constraint_system( arg isa AbstractFloat || throw(ArgumentError("Invalid argument specified for variable $var. The argument of the variable should be either $iv, a parameter, or a value specifying the time that the constraint holds.")) - isparameter(arg) && push!(constraintps, arg) + isparameter(arg) && push!(auxps, arg) else var ∈ sts && @warn "Variable $var has no argument. It will be interpreted as $var($iv), and the constraint will apply to the entire interval." end end +end - ConstraintsSystem( - constraints, collect(constraintsts), collect(constraintps); name = consname) +function generate_cost_function(sys::ODESystem) + costs = get_costs(sys) + coalesce = get_coalesce(sys) + cost_fn = build_function_wrapper() + return (u, p, t) -> coalesce(cost_fn(u, p, t)) end From 246464ca98dc58f6a10f9218588b1093690588d7 Mon Sep 17 00:00:00 2001 From: vyudu Date: Wed, 2 Apr 2025 17:36:23 -0400 Subject: [PATCH 1296/2176] feat: add cost and coalesce to ODESystem --- src/systems/abstractsystem.jl | 4 +- src/systems/diffeqs/abstractodesystem.jl | 2 +- src/systems/diffeqs/odesystem.jl | 59 +++++++++++++++++++----- test/bvproblem.jl | 31 +++++++++++-- 4 files changed, 78 insertions(+), 18 deletions(-) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index 1987cb9a60..1e086eaaff 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -920,7 +920,9 @@ for prop in [:eqs :tstops :index_cache :is_scalar_noise - :isscheduled] + :isscheduled + :costs + :coalesce] fname_get = Symbol(:get_, prop) fname_has = Symbol(:has_, prop) @eval begin diff --git a/src/systems/diffeqs/abstractodesystem.jl b/src/systems/diffeqs/abstractodesystem.jl index 23f20b00ec..4e7adcc5de 100644 --- a/src/systems/diffeqs/abstractodesystem.jl +++ b/src/systems/diffeqs/abstractodesystem.jl @@ -924,7 +924,7 @@ function generate_function_bc(sys::ODESystem, u0, u0_idxs, tspan; kwargs...) exprs = vcat(init_conds, cons) _p = reorder_parameters(sys, ps) - build_function_wrapper(sys, exprs, sol, _p..., t; output_type = Array, kwargs...) + build_function_wrapper(sys, exprs, sol, _p..., iv; output_type = Array, kwargs...) end """ diff --git a/src/systems/diffeqs/odesystem.jl b/src/systems/diffeqs/odesystem.jl index 35f132f12f..cc63e1ec7f 100644 --- a/src/systems/diffeqs/odesystem.jl +++ b/src/systems/diffeqs/odesystem.jl @@ -54,7 +54,7 @@ struct ODESystem <: AbstractODESystem """A set of expressions defining the costs of the system for optimal control.""" costs::Vector """Takes the cost vector and returns a scalar for optimization.""" - coalesce::Function + coalesce::Union{Nothing, Function} """ Time-derivative matrix. Note: this field will not be defined until [`calculate_tgrad`](@ref) is called on the system. @@ -209,7 +209,7 @@ struct ODESystem <: AbstractODESystem parent::Any function ODESystem( - tag, deqs, iv, dvs, ps, tspan, var_to_name, ctrls, observed, constraints, tgrad, + tag, deqs, iv, dvs, ps, tspan, var_to_name, ctrls, observed, constraints, costs, coalesce, tgrad, jac, ctrl_jac, Wfact, Wfact_t, name, description, systems, defaults, guesses, torn_matching, initializesystem, initialization_eqs, schedule, connector_type, preface, cevents, @@ -233,7 +233,7 @@ struct ODESystem <: AbstractODESystem check_units(u, deqs) end new(tag, deqs, iv, dvs, ps, tspan, var_to_name, - ctrls, observed, constraints, tgrad, jac, + ctrls, observed, constraints, costs, coalesce, tgrad, jac, ctrl_jac, Wfact, Wfact_t, name, description, systems, defaults, guesses, torn_matching, initializesystem, initialization_eqs, schedule, connector_type, preface, cevents, devents, parameter_dependencies, assertions, metadata, @@ -247,6 +247,8 @@ function ODESystem(deqs::AbstractVector{<:Equation}, iv, dvs, ps; controls = Num[], observed = Equation[], constraintsystem = nothing, + costs = Num[], + coalesce = nothing, systems = ODESystem[], tspan = nothing, name = nothing, @@ -327,14 +329,18 @@ function ODESystem(deqs::AbstractVector{<:Equation}, iv, dvs, ps; cons = get_constraintsystem(sys) cons !== nothing && push!(conssystems, cons) end - @show conssystems @set! constraintsystem.systems = conssystems end + costs = wrap.(costs) + + if length(costs) > 1 && isnothing(coalesce) + error("Must specify a coalesce function for the costs vector.") + end assertions = Dict{BasicSymbolic, Any}(unwrap(k) => v for (k, v) in assertions) ODESystem(Threads.atomic_add!(SYSTEM_COUNT, UInt(1)), - deqs, iv′, dvs′, ps′, tspan, var_to_name, ctrl′, observed, constraintsystem, tgrad, jac, + deqs, iv′, dvs′, ps′, tspan, var_to_name, ctrl′, observed, constraintsystem, costs, coalesce, tgrad, jac, ctrl_jac, Wfact, Wfact_t, name, description, systems, defaults, guesses, nothing, initializesystem, initialization_eqs, schedule, connector_type, preface, cont_callbacks, @@ -342,7 +348,7 @@ function ODESystem(deqs::AbstractVector{<:Equation}, iv, dvs, ps; metadata, gui_metadata, is_dde, tstops, checks = checks) end -function ODESystem(eqs, iv; constraints = Equation[], costs = Equation[], kwargs...) +function ODESystem(eqs, iv; constraints = Equation[], costs = Num[], kwargs...) diffvars, allunknowns, ps, eqs = process_equations(eqs, iv) for eq in get(kwargs, :parameter_dependencies, Equation[]) @@ -394,9 +400,10 @@ function ODESystem(eqs, iv; constraints = Equation[], costs = Equation[], kwargs !in(p, new_ps) && push!(new_ps, p) end end + costs = wrap.(costs) return ODESystem(eqs, iv, collect(Iterators.flatten((diffvars, algevars, consvars))), - collect(new_ps); constraintsystem, kwargs...) + collect(new_ps); constraintsystem, costs, kwargs...) end # NOTE: equality does not check cached Jacobian @@ -411,7 +418,9 @@ function Base.:(==)(sys1::ODESystem, sys2::ODESystem) _eq_unordered(get_ps(sys1), get_ps(sys2)) && _eq_unordered(continuous_events(sys1), continuous_events(sys2)) && _eq_unordered(discrete_events(sys1), discrete_events(sys2)) && - all(s1 == s2 for (s1, s2) in zip(get_systems(sys1), get_systems(sys2))) + all(s1 == s2 for (s1, s2) in zip(get_systems(sys1), get_systems(sys2))) && + isequal(get_constraintsystem(sys1), get_constraintssystem(sys2)) && + _eq_unordered(get_costs(sys1), get_costs(sys2)) end function flatten(sys::ODESystem, noeqs = false) @@ -768,7 +777,7 @@ end """ Process the costs for the constraint system. """ -function process_costs(costs::Vector{Equation}, sts, ps, iv) +function process_costs(costs::Vector, sts, ps, iv) coststs = OrderedSet() costps = OrderedSet() for cost in costs @@ -776,6 +785,7 @@ function process_costs(costs::Vector{Equation}, sts, ps, iv) end validate_vars_and_find_ps!(coststs, costps, sts, iv) + coststs, costps end """ @@ -813,9 +823,34 @@ function validate_vars_and_find_ps!(auxvars, auxps, sysvars, iv) end end -function generate_cost_function(sys::ODESystem) +""" +Generate a function that takes a solution object and computes the cost function obtained by coalescing the costs vector. +""" +function generate_cost_function(sys::ODESystem, kwargs...) costs = get_costs(sys) coalesce = get_coalesce(sys) - cost_fn = build_function_wrapper() - return (u, p, t) -> coalesce(cost_fn(u, p, t)) + iv = get_iv(sys) + + ps = parameters(sys; initial_parameters = false) + sts = unknowns(sys) + np = length(ps) + ns = length(sts) + stidxmap = Dict([v => i for (i, v) in enumerate(sts)]) + pidxmap = Dict([v => i for (i, v) in enumerate(ps)]) + + @variables sol(..)[1:ns] + for st in vars(costs) + x = operation(st) + t = only(arguments(st)) + idx = stidxmap[x(iv)] + + costs = map(c -> Symbolics.fast_substitute(c, Dict(x(t) => sol(t)[idx])), costs) + end + + _p = reorder_parameters(sys, ps) + fs = build_function_wrapper(sys, costs, sol, _p..., t; output_type = Array, kwargs...) + vc_oop, vc_iip = eval_or_rgf.(fs) + + cost(sol, p, t) = coalesce(vc_oop(sol, p, t)) + return cost end diff --git a/test/bvproblem.jl b/test/bvproblem.jl index c5451f681b..de698eaa8f 100644 --- a/test/bvproblem.jl +++ b/test/bvproblem.jl @@ -11,7 +11,7 @@ using ModelingToolkit: t_nounits as t, D_nounits as D solvers = [MIRK4] daesolvers = [Ascher2, Ascher4, Ascher6] -let +@testset "Lotka-Volterra" begin @parameters α=7.5 β=4.0 γ=8.0 δ=5.0 @variables x(t)=1.0 y(t)=2.0 @@ -47,7 +47,7 @@ let end ### Testing on pendulum -let +@testset "Pendulum" begin @parameters g=9.81 L=1.0 @variables θ(t)=π / 2 θ_t(t) @@ -86,7 +86,7 @@ end ################################################################## # Test generation of boundary condition function using `generate_function_bc`. Compare solutions to manually written boundary conditions -let +@testset "Boundary Condition Compilation" begin @parameters α=1.5 β=1.0 γ=3.0 δ=1.0 @variables x(..) y(..) eqs = [D(x(t)) ~ α * x(t) - β * x(t) * y(t), @@ -168,7 +168,7 @@ function test_solvers( end # Simple ODESystem with BVP constraints. -let +@testset "ODE with constraints" begin @parameters α=1.5 β=1.0 γ=3.0 δ=1.0 @variables x(..) y(..) @@ -274,3 +274,26 @@ end # bvp = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}(pend, u0map, tspan, parammap; guesses, check_length = false) # test_solvers(daesolvers, bvp, u0map, constr, get_alg_eqs(pend)) # end + +@testset "Cost function compilation" begin + @parameters α=1.5 β=1.0 γ=3.0 δ=1.0 + @variables x(..) y(..) + + eqs = [D(x(t)) ~ α * x(t) - β * x(t) * y(t), + D(y(t)) ~ -γ * y(t) + δ * x(t) * y(t)] + + tspan = (0.0, 1.0) + u0map = [x(t) => 4.0, y(t) => 2.0] + parammap = [α => 7.5, β => 4, γ => 8.0, δ => 5.0] + costs = [x(0.6), x(0.3)^2] + consolidate(u) = (u[1] + 3)^2 + u[2] + @mtkbuild lksys = ODESystem(eqs, t; costs, coalesce = consolidate) + @test_throws ErrorException @mtkbuild lksys2 = ODESystem(eqs, t; costs) + + prob = ODEProblem(lksys, u0map, tspan, parammap) + sol = solve(prob, Tsit5()) + costfn = ModelingToolkit.generate_cost_function(lksys) + p = prob.p + t = tspan[2] + @test costfn(sol, p, t) ≈ (sol(0.6)[1] + 3)^2 + sol(0.3)[1]^2 +end From bf9181662d690484ed2e0b0e4eb4d39a44af6d59 Mon Sep 17 00:00:00 2001 From: vyudu Date: Wed, 2 Apr 2025 17:37:09 -0400 Subject: [PATCH 1297/2176] rename bvproblem --- test/{bvproblem.jl => optimal_control.jl} | 0 test/runtests.jl | 2 +- 2 files changed, 1 insertion(+), 1 deletion(-) rename test/{bvproblem.jl => optimal_control.jl} (100%) diff --git a/test/bvproblem.jl b/test/optimal_control.jl similarity index 100% rename from test/bvproblem.jl rename to test/optimal_control.jl diff --git a/test/runtests.jl b/test/runtests.jl index 04d99cc8b9..37c738eec9 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -90,7 +90,7 @@ end @safetestset "SCCNonlinearProblem Test" include("scc_nonlinear_problem.jl") @safetestset "PDE Construction Test" include("pdesystem.jl") @safetestset "JumpSystem Test" include("jumpsystem.jl") - @safetestset "BVProblem Test" include("bvproblem.jl") + @safetestset "Optimal Control + Constraints Tests" include("optimal_control.jl") @safetestset "print_tree" include("print_tree.jl") @safetestset "Constraints Test" include("constraints.jl") @safetestset "IfLifting Test" include("if_lifting.jl") From d69dae7ac40855de0de7bdfe50bfadc2b057c77e Mon Sep 17 00:00:00 2001 From: vyudu Date: Wed, 2 Apr 2025 17:39:06 -0400 Subject: [PATCH 1298/2176] format --- src/systems/diffeqs/odesystem.jl | 12 +++++++----- test/optimal_control.jl | 2 +- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/src/systems/diffeqs/odesystem.jl b/src/systems/diffeqs/odesystem.jl index cc63e1ec7f..23350ff7cc 100644 --- a/src/systems/diffeqs/odesystem.jl +++ b/src/systems/diffeqs/odesystem.jl @@ -209,7 +209,8 @@ struct ODESystem <: AbstractODESystem parent::Any function ODESystem( - tag, deqs, iv, dvs, ps, tspan, var_to_name, ctrls, observed, constraints, costs, coalesce, tgrad, + tag, deqs, iv, dvs, ps, tspan, var_to_name, ctrls, + observed, constraints, costs, coalesce, tgrad, jac, ctrl_jac, Wfact, Wfact_t, name, description, systems, defaults, guesses, torn_matching, initializesystem, initialization_eqs, schedule, connector_type, preface, cevents, @@ -332,7 +333,7 @@ function ODESystem(deqs::AbstractVector{<:Equation}, iv, dvs, ps; @set! constraintsystem.systems = conssystems end costs = wrap.(costs) - + if length(costs) > 1 && isnothing(coalesce) error("Must specify a coalesce function for the costs vector.") end @@ -340,7 +341,8 @@ function ODESystem(deqs::AbstractVector{<:Equation}, iv, dvs, ps; assertions = Dict{BasicSymbolic, Any}(unwrap(k) => v for (k, v) in assertions) ODESystem(Threads.atomic_add!(SYSTEM_COUNT, UInt(1)), - deqs, iv′, dvs′, ps′, tspan, var_to_name, ctrl′, observed, constraintsystem, costs, coalesce, tgrad, jac, + deqs, iv′, dvs′, ps′, tspan, var_to_name, ctrl′, observed, + constraintsystem, costs, coalesce, tgrad, jac, ctrl_jac, Wfact, Wfact_t, name, description, systems, defaults, guesses, nothing, initializesystem, initialization_eqs, schedule, connector_type, preface, cont_callbacks, @@ -403,7 +405,7 @@ function ODESystem(eqs, iv; constraints = Equation[], costs = Num[], kwargs...) costs = wrap.(costs) return ODESystem(eqs, iv, collect(Iterators.flatten((diffvars, algevars, consvars))), - collect(new_ps); constraintsystem, costs, kwargs...) + collect(new_ps); constraintsystem, costs, kwargs...) end # NOTE: equality does not check cached Jacobian @@ -800,7 +802,7 @@ parameter of the system. function validate_vars_and_find_ps!(auxvars, auxps, sysvars, iv) sts = sysvars - for var in auxvars + for var in auxvars if !iscall(var) occursin(iv, var) && (var ∈ sts || throw(ArgumentError("Time-dependent variable $var is not an unknown of the system."))) diff --git a/test/optimal_control.jl b/test/optimal_control.jl index de698eaa8f..0bd7f57323 100644 --- a/test/optimal_control.jl +++ b/test/optimal_control.jl @@ -293,7 +293,7 @@ end prob = ODEProblem(lksys, u0map, tspan, parammap) sol = solve(prob, Tsit5()) costfn = ModelingToolkit.generate_cost_function(lksys) - p = prob.p + p = prob.p t = tspan[2] @test costfn(sol, p, t) ≈ (sol(0.6)[1] + 3)^2 + sol(0.3)[1]^2 end From 2ba3da788a659bc714edef43ad2e59a5d4bbc46a Mon Sep 17 00:00:00 2001 From: vyudu Date: Thu, 3 Apr 2025 10:13:56 -0400 Subject: [PATCH 1299/2176] fix: fix tests, add parameter test --- src/systems/abstractsystem.jl | 2 +- src/systems/diffeqs/odesystem.jl | 22 +++++++++++----------- test/optimal_control.jl | 21 ++++++++++++++++----- 3 files changed, 28 insertions(+), 17 deletions(-) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index 1e086eaaff..109fa9712c 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -922,7 +922,7 @@ for prop in [:eqs :is_scalar_noise :isscheduled :costs - :coalesce] + :consolidate] fname_get = Symbol(:get_, prop) fname_has = Symbol(:has_, prop) @eval begin diff --git a/src/systems/diffeqs/odesystem.jl b/src/systems/diffeqs/odesystem.jl index 23350ff7cc..cbce569a9b 100644 --- a/src/systems/diffeqs/odesystem.jl +++ b/src/systems/diffeqs/odesystem.jl @@ -54,7 +54,7 @@ struct ODESystem <: AbstractODESystem """A set of expressions defining the costs of the system for optimal control.""" costs::Vector """Takes the cost vector and returns a scalar for optimization.""" - coalesce::Union{Nothing, Function} + consolidate::Union{Nothing, Function} """ Time-derivative matrix. Note: this field will not be defined until [`calculate_tgrad`](@ref) is called on the system. @@ -210,7 +210,7 @@ struct ODESystem <: AbstractODESystem function ODESystem( tag, deqs, iv, dvs, ps, tspan, var_to_name, ctrls, - observed, constraints, costs, coalesce, tgrad, + observed, constraints, costs, consolidate, tgrad, jac, ctrl_jac, Wfact, Wfact_t, name, description, systems, defaults, guesses, torn_matching, initializesystem, initialization_eqs, schedule, connector_type, preface, cevents, @@ -234,7 +234,7 @@ struct ODESystem <: AbstractODESystem check_units(u, deqs) end new(tag, deqs, iv, dvs, ps, tspan, var_to_name, - ctrls, observed, constraints, costs, coalesce, tgrad, jac, + ctrls, observed, constraints, costs, consolidate, tgrad, jac, ctrl_jac, Wfact, Wfact_t, name, description, systems, defaults, guesses, torn_matching, initializesystem, initialization_eqs, schedule, connector_type, preface, cevents, devents, parameter_dependencies, assertions, metadata, @@ -249,7 +249,7 @@ function ODESystem(deqs::AbstractVector{<:Equation}, iv, dvs, ps; observed = Equation[], constraintsystem = nothing, costs = Num[], - coalesce = nothing, + consolidate = nothing, systems = ODESystem[], tspan = nothing, name = nothing, @@ -334,15 +334,15 @@ function ODESystem(deqs::AbstractVector{<:Equation}, iv, dvs, ps; end costs = wrap.(costs) - if length(costs) > 1 && isnothing(coalesce) - error("Must specify a coalesce function for the costs vector.") + if length(costs) > 1 && isnothing(consolidate) + error("Must specify a consolidation function for the costs vector.") end assertions = Dict{BasicSymbolic, Any}(unwrap(k) => v for (k, v) in assertions) ODESystem(Threads.atomic_add!(SYSTEM_COUNT, UInt(1)), deqs, iv′, dvs′, ps′, tspan, var_to_name, ctrl′, observed, - constraintsystem, costs, coalesce, tgrad, jac, + constraintsystem, costs, consolidate, tgrad, jac, ctrl_jac, Wfact, Wfact_t, name, description, systems, defaults, guesses, nothing, initializesystem, initialization_eqs, schedule, connector_type, preface, cont_callbacks, @@ -421,7 +421,7 @@ function Base.:(==)(sys1::ODESystem, sys2::ODESystem) _eq_unordered(continuous_events(sys1), continuous_events(sys2)) && _eq_unordered(discrete_events(sys1), discrete_events(sys2)) && all(s1 == s2 for (s1, s2) in zip(get_systems(sys1), get_systems(sys2))) && - isequal(get_constraintsystem(sys1), get_constraintssystem(sys2)) && + isequal(get_constraintsystem(sys1), get_constraintsystem(sys2)) && _eq_unordered(get_costs(sys1), get_costs(sys2)) end @@ -770,7 +770,7 @@ function process_constraint_system( end # Validate the states. - validate_vars_and_find_ps!(coststs, costps, sts, iv) + validate_vars_and_find_ps!(constraintsts, constraintps, sts, iv) ConstraintsSystem( constraints, collect(constraintsts), collect(constraintps); name = consname) @@ -830,7 +830,7 @@ Generate a function that takes a solution object and computes the cost function """ function generate_cost_function(sys::ODESystem, kwargs...) costs = get_costs(sys) - coalesce = get_coalesce(sys) + consolidate = get_consolidate(sys) iv = get_iv(sys) ps = parameters(sys; initial_parameters = false) @@ -853,6 +853,6 @@ function generate_cost_function(sys::ODESystem, kwargs...) fs = build_function_wrapper(sys, costs, sol, _p..., t; output_type = Array, kwargs...) vc_oop, vc_iip = eval_or_rgf.(fs) - cost(sol, p, t) = coalesce(vc_oop(sol, p, t)) + cost(sol, p, t) = consolidate(vc_oop(sol, p, t)) return cost end diff --git a/test/optimal_control.jl b/test/optimal_control.jl index 0bd7f57323..1c5d1dd7aa 100644 --- a/test/optimal_control.jl +++ b/test/optimal_control.jl @@ -1,5 +1,4 @@ ### TODO: update when BoundaryValueDiffEqAscher is updated to use the normal boundary condition conventions - using OrdinaryDiffEq using BoundaryValueDiffEqMIRK, BoundaryValueDiffEqAscher using BenchmarkTools @@ -278,6 +277,7 @@ end @testset "Cost function compilation" begin @parameters α=1.5 β=1.0 γ=3.0 δ=1.0 @variables x(..) y(..) + t = ModelingToolkit.t_nounits eqs = [D(x(t)) ~ α * x(t) - β * x(t) * y(t), D(y(t)) ~ -γ * y(t) + δ * x(t) * y(t)] @@ -287,13 +287,24 @@ end parammap = [α => 7.5, β => 4, γ => 8.0, δ => 5.0] costs = [x(0.6), x(0.3)^2] consolidate(u) = (u[1] + 3)^2 + u[2] - @mtkbuild lksys = ODESystem(eqs, t; costs, coalesce = consolidate) + @mtkbuild lksys = ODESystem(eqs, t; costs, consolidate) @test_throws ErrorException @mtkbuild lksys2 = ODESystem(eqs, t; costs) prob = ODEProblem(lksys, u0map, tspan, parammap) sol = solve(prob, Tsit5()) costfn = ModelingToolkit.generate_cost_function(lksys) - p = prob.p - t = tspan[2] - @test costfn(sol, p, t) ≈ (sol(0.6)[1] + 3)^2 + sol(0.3)[1]^2 + _t = tspan[2] + @test costfn(sol, prob.p, _t) ≈ (sol(0.6)[1] + 3)^2 + sol(0.3)[1]^2 + + ### With a parameter + @parameters t_c + costs = [y(t_c) + x(0.0), x(0.4)^2] + consolidate(u) = log(u[1]) - u[2] + @mtkbuild lksys = ODESystem(eqs, t; costs, consolidate) + @test t_c ∈ Set(parameters(lksys)) + push!(parammap, t_c => 0.56) + prob = ODEProblem(lksys, u0map, tspan, parammap) + sol = solve(prob, Tsit5()) + costfn = ModelingToolkit.generate_cost_function(lksys) + @test costfn(sol, prob.p, _t) ≈ log(sol(0.56)[2] + sol(0.)[1]) - sol(0.4)[1]^2 end From 6451788e2533d8ef27b10762843ea08084de36ce Mon Sep 17 00:00:00 2001 From: vyudu Date: Sat, 5 Apr 2025 12:14:22 -0400 Subject: [PATCH 1300/2176] add errors --- src/systems/diffeqs/abstractodesystem.jl | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/systems/diffeqs/abstractodesystem.jl b/src/systems/diffeqs/abstractodesystem.jl index 4e7adcc5de..673d0a0dec 100644 --- a/src/systems/diffeqs/abstractodesystem.jl +++ b/src/systems/diffeqs/abstractodesystem.jl @@ -745,6 +745,11 @@ function DiffEqBase.ODEProblem{iip, specialize}(sys::AbstractODESystem, u0map = Consider a BVProblem instead.") end + if !isempty(get_costs(sys)) + error("An ODESystem with costs cannot be solved using a regular ODEProblem. + Solvers for optimal control problems are forthcoming.") + end + f, u0, p = process_SciMLProblem(ODEFunction{iip, specialize}, sys, u0map, parammap; t = tspan !== nothing ? tspan[1] : tspan, check_length, warn_initialize_determined, eval_expression, eval_module, kwargs...) @@ -852,6 +857,11 @@ function SciMLBase.BVProblem{iip, specialize}(sys::AbstractODESystem, u0map = [] end !isnothing(callback) && error("BVP solvers do not support callbacks.") + if !isempty(get_costs(sys)) + error("An ODESystem with costs cannot be solved using a regular DAEProblem. + Solvers for optimal control problems are forthcoming.") + end + has_alg_eqs(sys) && error("The BVProblem constructor currently does not support ODESystems with algebraic equations.") # Remove this when the BVDAE solvers get updated, the codegen should work when it does. @@ -953,6 +963,11 @@ function DiffEqBase.DAEProblem{iip}(sys::AbstractODESystem, du0map, u0map, tspan if !iscomplete(sys) error("A completed system is required. Call `complete` or `structural_simplify` on the system before creating a `DAEProblem`") end + + if !isempty(get_costs(sys)) + error("An ODESystem with costs cannot be solved using a regular DAEProblem. + Solvers for optimal control problems are forthcoming.") + end f, du0, u0, p = process_SciMLProblem(DAEFunction{iip}, sys, u0map, parammap; implicit_dae = true, du0map = du0map, check_length, t = tspan !== nothing ? tspan[1] : tspan, From 31b9954325d9cda32e40980ba9ba3dffb129fa66 Mon Sep 17 00:00:00 2001 From: vyudu Date: Mon, 7 Apr 2025 10:31:02 -0400 Subject: [PATCH 1301/2176] correct error messages --- src/systems/diffeqs/abstractodesystem.jl | 34 ++++++++++++++---------- test/optimal_control.jl | 5 ++-- 2 files changed, 23 insertions(+), 16 deletions(-) diff --git a/src/systems/diffeqs/abstractodesystem.jl b/src/systems/diffeqs/abstractodesystem.jl index 673d0a0dec..4c6e61e1f8 100644 --- a/src/systems/diffeqs/abstractodesystem.jl +++ b/src/systems/diffeqs/abstractodesystem.jl @@ -697,6 +697,7 @@ end ```julia DiffEqBase.ODEProblem{iip}(sys::AbstractODESystem, u0map, tspan, parammap = DiffEqBase.NullParameters(); + allow_cost = false, version = nothing, tgrad = false, jac = false, checkbounds = false, sparse = false, @@ -730,6 +731,7 @@ end function DiffEqBase.ODEProblem{iip, specialize}(sys::AbstractODESystem, u0map = [], tspan = get_tspan(sys), parammap = DiffEqBase.NullParameters(); + allow_cost = false, callback = nothing, check_length = true, warn_initialize_determined = true, @@ -745,9 +747,10 @@ function DiffEqBase.ODEProblem{iip, specialize}(sys::AbstractODESystem, u0map = Consider a BVProblem instead.") end - if !isempty(get_costs(sys)) - error("An ODESystem with costs cannot be solved using a regular ODEProblem. - Solvers for optimal control problems are forthcoming.") + if !isempty(get_costs(sys)) && !allow_cost + error("ODEProblem will not optimize solutions of ODESystems that have associated cost functions. + Solvers for optimal control problems are forthcoming. In order to bypass this error (e.g. + to check the cost of a regular solution), pass `allow_cost` = true into the constructor.") end f, u0, p = process_SciMLProblem(ODEFunction{iip, specialize}, sys, u0map, parammap; @@ -801,21 +804,19 @@ If an ODESystem without `constraints` is specified, it will be treated as an ini ```julia @parameters g t_c = 0.5 - @variables x(..) y(t) [state_priority = 10] λ(t) + @variables x(..) y(t) λ(t) eqs = [D(D(x(t))) ~ λ * x(t) D(D(y)) ~ λ * y - g x(t)^2 + y^2 ~ 1] cstr = [x(0.5) ~ 1] - @named cstrs = ConstraintsSystem(cstr, t) - @mtkbuild pend = ODESystem(eqs, t) + @mtkbuild pend = ODESystem(eqs, t; constraints = cstrs) tspan = (0.0, 1.5) u0map = [x(t) => 0.6, y => 0.8] parammap = [g => 1] guesses = [λ => 1] - constraints = [x(0.5) ~ 1] - bvp = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}(pend, u0map, tspan, parammap; constraints, guesses, check_length = false) + bvp = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}(pend, u0map, tspan, parammap; guesses, check_length = false) ``` If the `ODESystem` has algebraic equations, like `x(t)^2 + y(t)^2`, the resulting @@ -844,6 +845,7 @@ function SciMLBase.BVProblem{iip, specialize}(sys::AbstractODESystem, u0map = [] tspan = get_tspan(sys), parammap = DiffEqBase.NullParameters(); guesses = Dict(), + allow_cost = false, version = nothing, tgrad = false, callback = nothing, check_length = true, @@ -857,9 +859,10 @@ function SciMLBase.BVProblem{iip, specialize}(sys::AbstractODESystem, u0map = [] end !isnothing(callback) && error("BVP solvers do not support callbacks.") - if !isempty(get_costs(sys)) - error("An ODESystem with costs cannot be solved using a regular DAEProblem. - Solvers for optimal control problems are forthcoming.") + if !isempty(get_costs(sys)) && !allow_cost + error("BVProblem will not optimize solutions of ODESystems that have associated cost functions. + Solvers for optimal control problems are forthcoming. In order to bypass this error (e.g. + to check the cost of a regular solution), pass `allow_cost` = true into the constructor.") end has_alg_eqs(sys) && @@ -958,16 +961,19 @@ end function DiffEqBase.DAEProblem{iip}(sys::AbstractODESystem, du0map, u0map, tspan, parammap = DiffEqBase.NullParameters(); + allow_cost = false, warn_initialize_determined = true, check_length = true, eval_expression = false, eval_module = @__MODULE__, kwargs...) where {iip} if !iscomplete(sys) error("A completed system is required. Call `complete` or `structural_simplify` on the system before creating a `DAEProblem`") end - if !isempty(get_costs(sys)) - error("An ODESystem with costs cannot be solved using a regular DAEProblem. - Solvers for optimal control problems are forthcoming.") + if !isempty(get_costs(sys)) && !allow_cost + error("DAEProblem will not optimize solutions of ODESystems that have associated cost functions. + Solvers for optimal control problems are forthcoming. In order to bypass this error (e.g. + to check the cost of a regular solution), pass `allow_cost` = true into the constructor.") end + f, du0, u0, p = process_SciMLProblem(DAEFunction{iip}, sys, u0map, parammap; implicit_dae = true, du0map = du0map, check_length, t = tspan !== nothing ? tspan[1] : tspan, diff --git a/test/optimal_control.jl b/test/optimal_control.jl index 1c5d1dd7aa..18b554c624 100644 --- a/test/optimal_control.jl +++ b/test/optimal_control.jl @@ -290,7 +290,8 @@ end @mtkbuild lksys = ODESystem(eqs, t; costs, consolidate) @test_throws ErrorException @mtkbuild lksys2 = ODESystem(eqs, t; costs) - prob = ODEProblem(lksys, u0map, tspan, parammap) + @test_throws ErrorException ODEProblem(lksys, u0map, tspan, parammap) + prob = ODEProblem(lksys, u0map, tspan, parammap; allow_cost = true) sol = solve(prob, Tsit5()) costfn = ModelingToolkit.generate_cost_function(lksys) _t = tspan[2] @@ -303,7 +304,7 @@ end @mtkbuild lksys = ODESystem(eqs, t; costs, consolidate) @test t_c ∈ Set(parameters(lksys)) push!(parammap, t_c => 0.56) - prob = ODEProblem(lksys, u0map, tspan, parammap) + prob = ODEProblem(lksys, u0map, tspan, parammap; allow_cost = true) sol = solve(prob, Tsit5()) costfn = ModelingToolkit.generate_cost_function(lksys) @test costfn(sol, prob.p, _t) ≈ log(sol(0.56)[2] + sol(0.)[1]) - sol(0.4)[1]^2 From 220db08b3c11b790325d2cca1e9ab6388fe3325b Mon Sep 17 00:00:00 2001 From: vyudu Date: Mon, 7 Apr 2025 10:31:51 -0400 Subject: [PATCH 1302/2176] formats --- test/optimal_control.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/optimal_control.jl b/test/optimal_control.jl index 18b554c624..f32ba471d8 100644 --- a/test/optimal_control.jl +++ b/test/optimal_control.jl @@ -307,5 +307,5 @@ end prob = ODEProblem(lksys, u0map, tspan, parammap; allow_cost = true) sol = solve(prob, Tsit5()) costfn = ModelingToolkit.generate_cost_function(lksys) - @test costfn(sol, prob.p, _t) ≈ log(sol(0.56)[2] + sol(0.)[1]) - sol(0.4)[1]^2 + @test costfn(sol, prob.p, _t) ≈ log(sol(0.56)[2] + sol(0.0)[1]) - sol(0.4)[1]^2 end From b85ce1dafb1d32c69c467441892a8292c7039a58 Mon Sep 17 00:00:00 2001 From: vyudu Date: Mon, 7 Apr 2025 13:22:51 -0400 Subject: [PATCH 1303/2176] fix: calculate_jacobian(sparse) should use W-sparsity) --- src/systems/diffeqs/abstractodesystem.jl | 16 ++++++++++------ src/systems/diffeqs/sdesystem.jl | 6 ++++-- src/systems/systems.jl | 2 +- 3 files changed, 15 insertions(+), 9 deletions(-) diff --git a/src/systems/diffeqs/abstractodesystem.jl b/src/systems/diffeqs/abstractodesystem.jl index 986aa27508..afe2c6d1b5 100644 --- a/src/systems/diffeqs/abstractodesystem.jl +++ b/src/systems/diffeqs/abstractodesystem.jl @@ -73,6 +73,15 @@ function calculate_jacobian(sys::AbstractODESystem; if sparse jac = sparsejacobian(rhs, dvs, simplify = simplify) + W_s = W_sparsity(sys) + (Is, Js, Vs) = findnz(W_s) + # Add nonzeros of W as non-structural zeros of the Jacobian (to ensure equal results for oop and iip Jacobian.) + for (i, j) in zip(Is, Js) + iszero(jac[i, j]) && begin + jac[i, j] = 1 + jac[i, j] = 0 + end + end else jac = jacobian(rhs, dvs, simplify = simplify) end @@ -294,12 +303,7 @@ function jacobian_dae_sparsity(sys::AbstractODESystem) end function W_sparsity(sys::AbstractODESystem) - if has_tearing_state(sys) - jac_sparsity = jacobian_sparsity(sys) - else - jac = calculate_jacobian(sys; sparse = true) - jac_sparsity = SparseMatrixCSC{Bool, Int64}((!iszero).(jac)) - end + jac_sparsity = jacobian_sparsity(sys) (n, n) = size(jac_sparsity) M = calculate_massmatrix(sys) M_sparsity = M isa UniformScaling ? sparse(I(n)) : SparseMatrixCSC{Bool, Int64}((!iszero).(M)) diff --git a/src/systems/diffeqs/sdesystem.jl b/src/systems/diffeqs/sdesystem.jl index 2fc9749309..3fa1302630 100644 --- a/src/systems/diffeqs/sdesystem.jl +++ b/src/systems/diffeqs/sdesystem.jl @@ -164,6 +164,7 @@ struct SDESystem <: AbstractODESystem """ is_dde::Bool isscheduled::Bool + tearing_state::Any function SDESystem(tag, deqs, neqs, iv, dvs, ps, tspan, var_to_name, ctrls, observed, tgrad, jac, ctrl_jac, Wfact, Wfact_t, name, description, systems, defaults, @@ -173,7 +174,8 @@ struct SDESystem <: AbstractODESystem metadata = nothing, gui_metadata = nothing, namespacing = true, complete = false, index_cache = nothing, parent = nothing, is_scalar_noise = false, is_dde = false, - isscheduled = false; + isscheduled = false, + tearing_state = nothing; checks::Union{Bool, Int} = true) if checks == true || (checks & CheckComponents) > 0 check_independent_variables([iv]) @@ -198,7 +200,7 @@ struct SDESystem <: AbstractODESystem ctrl_jac, Wfact, Wfact_t, name, description, systems, defaults, guesses, initializesystem, initialization_eqs, connector_type, cevents, devents, parameter_dependencies, assertions, metadata, gui_metadata, namespacing, - complete, index_cache, parent, is_scalar_noise, is_dde, isscheduled) + complete, index_cache, parent, is_scalar_noise, is_dde, isscheduled, tearing_state) end end diff --git a/src/systems/systems.jl b/src/systems/systems.jl index 367e1cd4cd..2652572bfe 100644 --- a/src/systems/systems.jl +++ b/src/systems/systems.jl @@ -155,7 +155,7 @@ function __structural_simplify(sys::AbstractSystem, io = nothing; simplify = fal get_iv(ode_sys), unknowns(ode_sys), parameters(ode_sys); name = nameof(ode_sys), is_scalar_noise, observed = observed(ode_sys), defaults = defaults(sys), parameter_dependencies = parameter_dependencies(sys), assertions = assertions(sys), - guesses = guesses(sys), initialization_eqs = initialization_equations(sys)) + guesses = guesses(sys), initialization_eqs = initialization_equations(sys), tearing_state = get_tearing_state(ode_sys)) end end From 66f5d594bb0d717133fae965cfeb63f7b80f55d5 Mon Sep 17 00:00:00 2001 From: vyudu Date: Mon, 7 Apr 2025 13:44:23 -0400 Subject: [PATCH 1304/2176] fix simplify --- src/systems/systems.jl | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/systems/systems.jl b/src/systems/systems.jl index 2652572bfe..0f8633f31f 100644 --- a/src/systems/systems.jl +++ b/src/systems/systems.jl @@ -151,11 +151,13 @@ function __structural_simplify(sys::AbstractSystem, io = nothing; simplify = fal end noise_eqs = StructuralTransformations.tearing_substitute_expr(ode_sys, noise_eqs) - return SDESystem(Vector{Equation}(full_equations(ode_sys)), noise_eqs, + ssys = SDESystem(Vector{Equation}(full_equations(ode_sys)), noise_eqs, get_iv(ode_sys), unknowns(ode_sys), parameters(ode_sys); name = nameof(ode_sys), is_scalar_noise, observed = observed(ode_sys), defaults = defaults(sys), parameter_dependencies = parameter_dependencies(sys), assertions = assertions(sys), - guesses = guesses(sys), initialization_eqs = initialization_equations(sys), tearing_state = get_tearing_state(ode_sys)) + guesses = guesses(sys), initialization_eqs = initialization_equations(sys)) + @set! ssys.tearing_state = get_tearing_state(ode_sys) + return ssys end end From 7f071ee19fd8189e795e1f60b9cee47a57b775a9 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 9 Apr 2025 13:11:00 +0530 Subject: [PATCH 1305/2176] build: bump minor version --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 06b20c4552..63aef5b8da 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "ModelingToolkit" uuid = "961ee093-0014-501f-94e3-6117800e7a78" authors = ["Yingbo Ma ", "Chris Rackauckas and contributors"] -version = "9.71.0" +version = "9.72.0" [deps] ADTypes = "47edcb42-4c32-4615-8424-f2b9edc5f35b" From cc669c8cc9cf3d2f2d2a9cfd00b86430433cb55e Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 10 Apr 2025 17:31:28 +0530 Subject: [PATCH 1306/2176] fix: fix `assert_jac_length_header` --- src/systems/diffeqs/abstractodesystem.jl | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/systems/diffeqs/abstractodesystem.jl b/src/systems/diffeqs/abstractodesystem.jl index b89dc79722..48c3e6e659 100644 --- a/src/systems/diffeqs/abstractodesystem.jl +++ b/src/systems/diffeqs/abstractodesystem.jl @@ -141,10 +141,12 @@ end function assert_jac_length_header(sys) W = W_sparsity(sys) - identity, expr -> Func([expr.args...], [], LiteralExpr(quote - @assert $(findnz)($(expr.args[1]))[1:2] == $(findnz)($W)[1:2] - $(expr.body) - end)) + identity, + function add_header(expr) + Func(expr.args, [], expr.body, + [:(@assert $(SymbolicUtils.Code.toexpr(term(findnz, expr.args[1])))[1:2] == + $(findnz(W)[1:2]))]) + end end function generate_W(sys::AbstractODESystem, γ = 1., dvs = unknowns(sys), From b6c67ba0062b233bebba72aee4758295c3716dd5 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 10 Apr 2025 17:31:33 +0530 Subject: [PATCH 1307/2176] refactor: format --- src/ModelingToolkit.jl | 3 ++- src/systems/diffeqs/abstractodesystem.jl | 13 +++++++------ test/jacobiansparsity.jl | 10 ++++++---- 3 files changed, 15 insertions(+), 11 deletions(-) diff --git a/src/ModelingToolkit.jl b/src/ModelingToolkit.jl index 76783d6be0..9f69458528 100644 --- a/src/ModelingToolkit.jl +++ b/src/ModelingToolkit.jl @@ -303,7 +303,8 @@ export structural_simplify, expand_connections, linearize, linearization_functio LinearizationProblem export solve -export calculate_jacobian, generate_jacobian, generate_function, generate_custom_function, generate_W +export calculate_jacobian, generate_jacobian, generate_function, generate_custom_function, + generate_W export calculate_control_jacobian, generate_control_jacobian export calculate_tgrad, generate_tgrad export calculate_gradient, generate_gradient diff --git a/src/systems/diffeqs/abstractodesystem.jl b/src/systems/diffeqs/abstractodesystem.jl index 48c3e6e659..196c692c67 100644 --- a/src/systems/diffeqs/abstractodesystem.jl +++ b/src/systems/diffeqs/abstractodesystem.jl @@ -149,17 +149,17 @@ function assert_jac_length_header(sys) end end -function generate_W(sys::AbstractODESystem, γ = 1., dvs = unknowns(sys), - ps = parameters(sys; initial_parameters = true); +function generate_W(sys::AbstractODESystem, γ = 1.0, dvs = unknowns(sys), + ps = parameters(sys; initial_parameters = true); simplify = false, sparse = false, kwargs...) @variables ˍ₋gamma M = calculate_massmatrix(sys; simplify) sparse && (M = SparseArrays.sparse(M)) J = calculate_jacobian(sys; simplify, sparse, dvs) - W = ˍ₋gamma*M + J + W = ˍ₋gamma * M + J p = reorder_parameters(sys, ps) - return build_function_wrapper(sys, W, + return build_function_wrapper(sys, W, dvs, p..., ˍ₋gamma, @@ -304,11 +304,12 @@ function jacobian_dae_sparsity(sys::AbstractODESystem) J1 + J2 end -function W_sparsity(sys::AbstractODESystem) +function W_sparsity(sys::AbstractODESystem) jac_sparsity = jacobian_sparsity(sys) (n, n) = size(jac_sparsity) M = calculate_massmatrix(sys) - M_sparsity = M isa UniformScaling ? sparse(I(n)) : SparseMatrixCSC{Bool, Int64}((!iszero).(M)) + M_sparsity = M isa UniformScaling ? sparse(I(n)) : + SparseMatrixCSC{Bool, Int64}((!iszero).(M)) jac_sparsity .| M_sparsity end diff --git a/test/jacobiansparsity.jl b/test/jacobiansparsity.jl index f6f9853137..efa1534979 100644 --- a/test/jacobiansparsity.jl +++ b/test/jacobiansparsity.jl @@ -74,7 +74,7 @@ f = DiffEqBase.ODEFunction(sys, u0 = nothing, sparse = true, jac = false) # test when u0 is not Float64 u0 = similar(init_brusselator_2d(xyd_brusselator), Float32) prob_ode_brusselator_2d = ODEProblem(brusselator_2d_loop, - u0, (0.0, 11.5), p) + u0, (0.0, 11.5), p) sys = complete(modelingtoolkitize(prob_ode_brusselator_2d)) prob = ODEProblem(sys, u0, (0, 11.5), sparse = true, jac = false) @@ -94,7 +94,8 @@ prob = ODEProblem(sys, u0, (0, 11.5), sparse = true, jac = true) @mtkbuild pend = ODESystem(eqs, t) u0 = [x => 1, y => 0] - prob = ODEProblem(pend, u0, (0, 11.5), [g => 1], guesses = [λ => 1], sparse = true, jac = true) + prob = ODEProblem( + pend, u0, (0, 11.5), [g => 1], guesses = [λ => 1], sparse = true, jac = true) jac, jac! = generate_jacobian(pend; expression = Val{false}, sparse = true) jac_prototype = ModelingToolkit.jacobian_sparsity(pend) W_prototype = ModelingToolkit.W_sparsity(pend) @@ -109,8 +110,9 @@ prob = ODEProblem(sys, u0, (0, 11.5), sparse = true, jac = true) @test_throws AssertionError jac!(similar(jac_prototype, Float64), u, p, t) W, W! = generate_W(pend; expression = Val{false}, sparse = true) - γ = .1 + γ = 0.1 M = sparse(calculate_massmatrix(pend)) @test_throws AssertionError W!(similar(jac_prototype, Float64), u, p, γ, t) - @test W!(similar(W_prototype, Float64), u, p, γ, t) == 0.1 * M + jac!(similar(W_prototype, Float64), u, p, t) + @test W!(similar(W_prototype, Float64), u, p, γ, t) == + 0.1 * M + jac!(similar(W_prototype, Float64), u, p, t) end From 875837b9010b70efbc1bd2d612bdd85cc3d21048 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 10 Apr 2025 18:32:43 +0530 Subject: [PATCH 1308/2176] test: test jacobian exactness --- test/jacobiansparsity.jl | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/test/jacobiansparsity.jl b/test/jacobiansparsity.jl index efa1534979..1f936bc878 100644 --- a/test/jacobiansparsity.jl +++ b/test/jacobiansparsity.jl @@ -116,3 +116,22 @@ prob = ODEProblem(sys, u0, (0, 11.5), sparse = true, jac = true) @test W!(similar(W_prototype, Float64), u, p, γ, t) == 0.1 * M + jac!(similar(W_prototype, Float64), u, p, t) end + +@testset "Issue#3556: Numerical accuracy" begin + t = ModelingToolkit.t_nounits + D = ModelingToolkit.D_nounits + @parameters g + @variables x(t) y(t) [state_priority = 10] λ(t) + eqs = [D(D(x)) ~ λ * x + D(D(y)) ~ λ * y - g + x^2 + y^2 ~ 1] + @mtkbuild pend = ODESystem(eqs, t) + prob = ODEProblem(pend, [x => 0.0, D(x) => 1.0], (0.0, 1.0), [g => 1.0]; + guesses = [y => 1.0, λ => 1.0], jac = true, sparse = true) + J = deepcopy(prob.f.jac_prototype) + prob.f.jac(J, prob.u0, prob.p, 1.0) + # this currently works but may not continue to do so + # see https://github.com/SciML/ModelingToolkit.jl/pull/3556#issuecomment-2792664039 + @test J == prob.f.jac(prob.u0, prob.p, 1.0) + @test J ≈ prob.f.jac(prob.u0, prob.p, 1.0) +end From 2cea8eb19585818c7e2f2e3f6f567377340457eb Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 14 Apr 2025 18:13:58 +0530 Subject: [PATCH 1309/2176] test: update reference tests --- test/latexify/20.tex | 6 +++--- test/latexify/30.tex | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/test/latexify/20.tex b/test/latexify/20.tex index ce6f5067c2..012243e981 100644 --- a/test/latexify/20.tex +++ b/test/latexify/20.tex @@ -1,5 +1,5 @@ \begin{align} -\frac{\mathrm{d} u\left( t \right)_{1}}{\mathrm{d}t} &= p_{3} \left( - u\left( t \right)_{1} + u\left( t \right)_{2} \right) \\ -0 &= - u\left( t \right)_{2} + \frac{1}{10} \left( p_{1} - u\left( t \right)_{1} \right) p_{2} p_{3} u\left( t \right)_{1} \\ -\frac{\mathrm{d} u\left( t \right)_{3}}{\mathrm{d}t} &= \left( u\left( t \right)_{2} \right)^{\frac{2}{3}} u\left( t \right)_{1} - p_{3} u\left( t \right)_{3} +\frac{\mathrm{d} u\_{1}\left( t \right)}{\mathrm{d}t} &= p_{3} \left( - u\_{1}\left( t \right) + u\_{2}\left( t \right) \right) \\ +0 &= - u\_{2}\left( t \right) + \frac{1}{10} \left( p_{1} - u\_{1}\left( t \right) \right) p_{2} p_{3} u\_{1}\left( t \right) \\ +\frac{\mathrm{d} u\_{3}\left( t \right)}{\mathrm{d}t} &= u\_{2}\left( t \right)^{\frac{2}{3}} u\_{1}\left( t \right) - p_{3} u\_{3}\left( t \right) \end{align} diff --git a/test/latexify/30.tex b/test/latexify/30.tex index d206c48d47..b51b73c34b 100644 --- a/test/latexify/30.tex +++ b/test/latexify/30.tex @@ -1,5 +1,5 @@ \begin{align} -\frac{\mathrm{d} u\left( t \right)_{1}}{\mathrm{d}t} &= p_{3} \left( - u\left( t \right)_{1} + u\left( t \right)_{2} \right) \\ -\frac{\mathrm{d} u\left( t \right)_{2}}{\mathrm{d}t} &= - u\left( t \right)_{2} + \frac{1}{10} \left( p_{1} - u\left( t \right)_{1} \right) p_{2} p_{3} u\left( t \right)_{1} \\ -\frac{\mathrm{d} u\left( t \right)_{3}}{\mathrm{d}t} &= \left( u\left( t \right)_{2} \right)^{\frac{2}{3}} u\left( t \right)_{1} - p_{3} u\left( t \right)_{3} +\frac{\mathrm{d} u\_{1}\left( t \right)}{\mathrm{d}t} &= p_{3} \left( - u\_{1}\left( t \right) + u\_{2}\left( t \right) \right) \\ +\frac{\mathrm{d} u\_{2}\left( t \right)}{\mathrm{d}t} &= - u\_{2}\left( t \right) + \frac{1}{10} \left( p_{1} - u\_{1}\left( t \right) \right) p_{2} p_{3} u\_{1}\left( t \right) \\ +\frac{\mathrm{d} u\_{3}\left( t \right)}{\mathrm{d}t} &= u\_{2}\left( t \right)^{\frac{2}{3}} u\_{1}\left( t \right) - p_{3} u\_{3}\left( t \right) \end{align} From 50f932fd0589da2f0ab0e1821abdc08c73c04f34 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 14 Apr 2025 17:43:56 +0530 Subject: [PATCH 1310/2176] fix: use `Float16` zero default for `Initial` parameters --- src/systems/abstractsystem.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index 67bcbc88c0..b28b2d5279 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -733,11 +733,11 @@ function add_initialization_parameters(sys::AbstractSystem) defs = copy(get_defaults(sys)) for ivar in initials if symbolic_type(ivar) == ScalarSymbolic() - defs[ivar] = zero_var(ivar) + defs[ivar] = zero(Float16) else defs[ivar] = collect(ivar) for scal_ivar in defs[ivar] - defs[scal_ivar] = zero_var(scal_ivar) + defs[scal_ivar] = zero(Float16) end end end From 0305c9ddba2cf12bb4683cb08202dc2041b2958c Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 14 Apr 2025 17:44:16 +0530 Subject: [PATCH 1311/2176] test: test `Float32` values retained in problem construction --- test/initial_values.jl | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/test/initial_values.jl b/test/initial_values.jl index f63333cb2f..01a053a9bf 100644 --- a/test/initial_values.jl +++ b/test/initial_values.jl @@ -252,3 +252,16 @@ end ps = [p => [4.0, 5.0]] @test_nowarn NonlinearProblem(nlsys, u0, ps) end + +@testset "Issue#3553: Retain `Float32` initial values" begin + @parameters p d + @variables X(t) + eqs = [D(X) ~ p - d * X] + @mtkbuild osys = ODESystem(eqs, t) + u0 = [X => 1.0f0] + ps = [p => 1.0f0, d => 2.0f0] + oprob = ODEProblem(osys, u0, (0.0f0, 1.0f0), ps) + sol = solve(oprob) + @test eltype(oprob.u0) == Float32 + @test eltype(eltype(sol.u)) == Float32 +end From bcaa86941e2b7c0bb2b1e36acc0bbce26a0c82c2 Mon Sep 17 00:00:00 2001 From: Christopher Rackauckas Date: Mon, 14 Apr 2025 09:08:48 -0400 Subject: [PATCH 1312/2176] Update src/systems/abstractsystem.jl --- src/systems/abstractsystem.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index b28b2d5279..7da0179676 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -733,7 +733,7 @@ function add_initialization_parameters(sys::AbstractSystem) defs = copy(get_defaults(sys)) for ivar in initials if symbolic_type(ivar) == ScalarSymbolic() - defs[ivar] = zero(Float16) + defs[ivar] = false else defs[ivar] = collect(ivar) for scal_ivar in defs[ivar] From 65a326db7a8cb80b4fb4b50fe958cf4569b5d940 Mon Sep 17 00:00:00 2001 From: Christopher Rackauckas Date: Mon, 14 Apr 2025 09:08:53 -0400 Subject: [PATCH 1313/2176] Update src/systems/abstractsystem.jl --- src/systems/abstractsystem.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index 7da0179676..72f3132889 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -737,7 +737,7 @@ function add_initialization_parameters(sys::AbstractSystem) else defs[ivar] = collect(ivar) for scal_ivar in defs[ivar] - defs[scal_ivar] = zero(Float16) + defs[scal_ivar] = false end end end From aa7f38b25c7282ae93f6c12c63388293c9baa93f Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 15 Apr 2025 11:43:14 +0530 Subject: [PATCH 1314/2176] fix: fix type promotion in `MTKParameters` constructor --- src/systems/parameter_buffer.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/systems/parameter_buffer.jl b/src/systems/parameter_buffer.jl index 36105c0de8..2cf87769a0 100644 --- a/src/systems/parameter_buffer.jl +++ b/src/systems/parameter_buffer.jl @@ -1,6 +1,6 @@ symconvert(::Type{Symbolics.Struct{T}}, x) where {T} = convert(T, x) symconvert(::Type{T}, x) where {T} = convert(T, x) -symconvert(::Type{Real}, x::Integer) = convert(Float64, x) +symconvert(::Type{Real}, x::Integer) = x symconvert(::Type{V}, x) where {V <: AbstractArray} = convert(V, symconvert.(eltype(V), x)) struct MTKParameters{T, I, D, C, N, H} From 227a0d8edf5623a4be727ab0b5b7601d4a5e5c3d Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 15 Apr 2025 11:43:21 +0530 Subject: [PATCH 1315/2176] fix: use `Bool(0)` for `default_dd_guess` --- src/systems/nonlinear/initializesystem.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/systems/nonlinear/initializesystem.jl b/src/systems/nonlinear/initializesystem.jl index 9c85b9b324..3b90bfc66a 100644 --- a/src/systems/nonlinear/initializesystem.jl +++ b/src/systems/nonlinear/initializesystem.jl @@ -8,7 +8,7 @@ function generate_initializesystem(sys::AbstractTimeDependentSystem; pmap = Dict(), initialization_eqs = [], guesses = Dict(), - default_dd_guess = 0.0, + default_dd_guess = Bool(0), algebraic_only = false, check_units = true, check_defguess = false, name = nameof(sys), extra_metadata = (;), kwargs...) From b228fd957bb9ad4413c982254df85fd16cdad92b Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 15 Apr 2025 13:02:37 +0530 Subject: [PATCH 1316/2176] fix: promote integer valued reals to `Float16` --- src/systems/parameter_buffer.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/systems/parameter_buffer.jl b/src/systems/parameter_buffer.jl index 2cf87769a0..6035605a02 100644 --- a/src/systems/parameter_buffer.jl +++ b/src/systems/parameter_buffer.jl @@ -1,6 +1,6 @@ symconvert(::Type{Symbolics.Struct{T}}, x) where {T} = convert(T, x) symconvert(::Type{T}, x) where {T} = convert(T, x) -symconvert(::Type{Real}, x::Integer) = x +symconvert(::Type{Real}, x::Integer) = convert(Float16, x) symconvert(::Type{V}, x) where {V <: AbstractArray} = convert(V, symconvert.(eltype(V), x)) struct MTKParameters{T, I, D, C, N, H} From c814779c365205629edba03cf653e4840d0310cb Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 15 Apr 2025 17:08:21 +0530 Subject: [PATCH 1317/2176] feat: allow specifying `eltype` of numeric buffer in `better_varmap_to_vars` --- src/systems/problem_utils.jl | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/systems/problem_utils.jl b/src/systems/problem_utils.jl index 1db153781a..236a3b31b8 100644 --- a/src/systems/problem_utils.jl +++ b/src/systems/problem_utils.jl @@ -362,7 +362,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, container_type = Array, + tofloat = true, container_type = Array, floatT = Float64, toterm = default_toterm, promotetoconcrete = nothing, check = true, allow_symbolic = false, is_initializeprob = false) isempty(vars) && return nothing @@ -385,6 +385,8 @@ function better_varmap_to_vars(varmap::AbstractDict, vars::Vector; is_initializeprob ? throw(MissingGuessError(missingsyms, missingvals)) : throw(UnexpectedSymbolicValueInVarmap(missingsyms[1], missingvals[1])) end + + vals = floatT.(vals) end if container_type <: Union{AbstractDict, Tuple, Nothing, SciMLBase.NullParameters} From a8d59d65d985938e0495ec97964bacf48d112f87 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 15 Apr 2025 17:08:38 +0530 Subject: [PATCH 1318/2176] feat: allow specifying floating point type in `get_temporary_value` --- src/systems/problem_utils.jl | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/systems/problem_utils.jl b/src/systems/problem_utils.jl index 236a3b31b8..8ba5349011 100644 --- a/src/systems/problem_utils.jl +++ b/src/systems/problem_utils.jl @@ -535,12 +535,12 @@ function (f::UpdateInitializeprob)(initializeprob, prob) f.setvals(initializeprob, f.getvals(prob)) end -function get_temporary_value(p) +function get_temporary_value(p, floatT = Float64) stype = symtype(unwrap(p)) return if stype == Real - zero(Float64) + zero(floatT) elseif stype <: AbstractArray{Real} - zeros(Float64, size(p)) + zeros(floatT, size(p)) elseif stype <: Real zero(stype) elseif stype <: AbstractArray From 742146eac0f9fe085e648ff280297520d978b22d Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 15 Apr 2025 17:08:48 +0530 Subject: [PATCH 1319/2176] refactor: remove `zero_var` --- src/systems/problem_utils.jl | 19 ------------------- 1 file changed, 19 deletions(-) diff --git a/src/systems/problem_utils.jl b/src/systems/problem_utils.jl index 8ba5349011..78f2473f63 100644 --- a/src/systems/problem_utils.jl +++ b/src/systems/problem_utils.jl @@ -247,25 +247,6 @@ function recursive_unwrap(x::AbstractDict) return anydict(unwrap(k) => recursive_unwrap(v) for (k, v) in x) end -""" - $(TYPEDSIGNATURES) - -Return the appropriate zero value for a symbolic variable representing a number or array of -numbers. Sized array symbolics return a zero-filled array of matching size. Unsized array -symbolics return an empty array of the appropriate `eltype`. -""" -function zero_var(x::Symbolic{T}) where {V <: Number, T <: Union{V, AbstractArray{V}}} - if Symbolics.isarraysymbolic(x) - if is_sized_array_symbolic(x) - return zeros(eltype(T), size(x)) - else - return T[] - end - else - return zero(T) - end -end - """ $(TYPEDSIGNATURES) From fb1fb93e464d82af6490c77180180684623e9e7a Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 15 Apr 2025 17:09:10 +0530 Subject: [PATCH 1320/2176] refactor: determine floating point type earlier and propagate --- src/systems/problem_utils.jl | 31 +++++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/src/systems/problem_utils.jl b/src/systems/problem_utils.jl index 78f2473f63..877b844a32 100644 --- a/src/systems/problem_utils.jl +++ b/src/systems/problem_utils.jl @@ -631,11 +631,12 @@ All other keyword arguments are forwarded to `InitializationProblem`. """ function maybe_build_initialization_problem( sys::AbstractSystem, op::AbstractDict, u0map, pmap, t, defs, - guesses, missing_unknowns; implicit_dae = false, u0_constructor = identity, kwargs...) + guesses, missing_unknowns; implicit_dae = false, + u0_constructor = identity, floatT = Float64, kwargs...) guesses = merge(ModelingToolkit.guesses(sys), todict(guesses)) if t === nothing && is_time_dependent(sys) - t = 0.0 + t = zero(floatT) end initializeprob = ModelingToolkit.InitializationProblem{true, SciMLBase.FullSpecialize}( @@ -675,7 +676,7 @@ function maybe_build_initialization_problem( get(op, p, missing) === missing || continue p = unwrap(p) stype = symtype(p) - op[p] = get_temporary_value(p) + op[p] = get_temporary_value(p, floatT) if iscall(p) && operation(p) === getindex arrp = arguments(p)[1] op[arrp] = collect(arrp) @@ -684,7 +685,7 @@ function maybe_build_initialization_problem( if is_time_dependent(sys) for v in missing_unknowns - op[v] = zero_var(v) + op[v] = get_temporary_value(v, floatT) end empty!(missing_unknowns) end @@ -798,12 +799,26 @@ function process_SciMLProblem( op, missing_unknowns, missing_pars = build_operating_point!(sys, u0map, pmap, defs, cmap, dvs, ps) + floatT = Bool + for (k, v) in op + symbolic_type(v) == NotSymbolic() || continue + is_array_of_symbolics(v) && continue + + if v isa AbstractArray + isconcretetype(eltype(v)) || continue + floatT = promote_type(floatT, eltype(v)) + elseif v isa Real && isconcretetype(v) + floatT = promote_type(floatT, typeof(v)) + end + end + floatT = float(floatT) + if !is_time_dependent(sys) || is_initializesystem(sys) add_observed_equations!(u0map, obs) end if u0_constructor === identity && u0Type <: StaticArray u0_constructor = vals -> SymbolicUtils.Code.create_array( - u0Type, eltype(vals), Val(1), Val(length(vals)), vals...) + u0Type, floatT, Val(1), Val(length(vals)), vals...) end if build_initializeprob kws = maybe_build_initialization_problem( @@ -813,7 +828,7 @@ function process_SciMLProblem( warn_cyclic_dependency, check_units = check_initialization_units, circular_dependency_max_cycle_length, circular_dependency_max_cycles, use_scc, force_time_independent = force_initialization_time_independent, algebraic_only, allow_incomplete, - u0_constructor) + u0_constructor, floatT) kwargs = merge(kwargs, kws) end @@ -841,7 +856,7 @@ function process_SciMLProblem( evaluate_varmap!(op, dvs; limit = substitution_limit) u0 = better_varmap_to_vars( - op, dvs; tofloat, + op, dvs; tofloat, floatT, container_type = u0Type, allow_symbolic = symbolic_u0, is_initializeprob) if u0 !== nothing @@ -865,7 +880,7 @@ function process_SciMLProblem( end evaluate_varmap!(op, ps; limit = substitution_limit) if is_split(sys) - p = MTKParameters(sys, op) + p = MTKParameters(sys, op; floatT = floatT) else p = better_varmap_to_vars(op, ps; tofloat, container_type = pType) end From 59c6b31dd44b6d7194b49326270a182f8cf76f2d Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 15 Apr 2025 17:09:27 +0530 Subject: [PATCH 1321/2176] refactor: remove type-promotion hack in `InitializationProblem` --- src/systems/diffeqs/abstractodesystem.jl | 26 ------------------------ 1 file changed, 26 deletions(-) diff --git a/src/systems/diffeqs/abstractodesystem.jl b/src/systems/diffeqs/abstractodesystem.jl index 196c692c67..62ddd12a08 100644 --- a/src/systems/diffeqs/abstractodesystem.jl +++ b/src/systems/diffeqs/abstractodesystem.jl @@ -1541,32 +1541,6 @@ function InitializationProblem{iip, specialize}(sys::AbstractSystem, filter_missing_values!(parammap) u0map = merge(ModelingToolkit.guesses(sys), todict(guesses), u0map) - fullmap = merge(u0map, parammap) - u0T = Union{} - for sym in unknowns(isys) - val = fixpoint_sub(sym, fullmap) - symbolic_type(val) == NotSymbolic() || continue - u0T = promote_type(u0T, typeof(val)) - end - for eq in observed(isys) - # ignore HACK-ed observed equations - symbolic_type(eq.lhs) == ArraySymbolic() && continue - val = fixpoint_sub(eq.lhs, fullmap) - symbolic_type(val) == NotSymbolic() || continue - u0T = promote_type(u0T, typeof(val)) - end - if u0T != Union{} - u0T = eltype(u0T) - u0map = Dict(k => if v === nothing - nothing - elseif symbolic_type(v) == NotSymbolic() && !is_array_of_symbolics(v) - v isa AbstractArray ? u0T.(v) : u0T(v) - else - v - end - for (k, v) in u0map) - end - TProb = if neqs == nunknown && isempty(unassigned_vars) if use_scc && neqs > 0 if is_split(isys) From 27e7efbf73ccbe3ccb48c67e49c2ea8fa990830e Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 15 Apr 2025 17:09:56 +0530 Subject: [PATCH 1322/2176] fix: use `remake` to promote intialization problem --- src/systems/diffeqs/abstractodesystem.jl | 30 +++++++++++++++++------- src/systems/diffeqs/sdesystem.jl | 9 +++++-- src/systems/nonlinear/nonlinearsystem.jl | 9 +++++-- 3 files changed, 36 insertions(+), 12 deletions(-) diff --git a/src/systems/diffeqs/abstractodesystem.jl b/src/systems/diffeqs/abstractodesystem.jl index 62ddd12a08..c6b2eb8a87 100644 --- a/src/systems/diffeqs/abstractodesystem.jl +++ b/src/systems/diffeqs/abstractodesystem.jl @@ -817,7 +817,9 @@ function DiffEqBase.ODEProblem{iip, specialize}(sys::AbstractODESystem, u0map = end # Call `remake` so it runs initialization if it is trivial - return remake(ODEProblem{iip}(f, u0, tspan, p, pt; kwargs1..., kwargs...)) + # Pass `u0` and `p` to run `ReconstructInitializeprob` which will promote + # u0 and p of initializeprob + return remake(ODEProblem{iip}(f, u0, tspan, p, pt; kwargs1..., kwargs...); u0, p) end get_callback(prob::ODEProblem) = prob.kwargs[:callback] @@ -1040,9 +1042,14 @@ function DiffEqBase.DAEProblem{iip}(sys::AbstractODESystem, du0map, u0map, tspan end # Call `remake` so it runs initialization if it is trivial - return remake(DAEProblem{iip}( - f, du0, u0, tspan, p; differential_vars = differential_vars, - kwargs..., kwargs1...)) + # Pass `u0` and `p` to run `ReconstructInitializeprob` which will promote + # u0 and p of initializeprob + return remake( + DAEProblem{iip}( + f, du0, u0, tspan, p; differential_vars = differential_vars, + kwargs..., kwargs1...); + u0, + p) end function generate_history(sys::AbstractODESystem, u0; expression = Val{false}, kwargs...) @@ -1088,7 +1095,9 @@ function DiffEqBase.DDEProblem{iip}(sys::AbstractODESystem, u0map = [], kwargs1 = merge(kwargs1, (callback = cbs,)) end # Call `remake` so it runs initialization if it is trivial - return remake(DDEProblem{iip}(f, u0, h, tspan, p; kwargs1..., kwargs...)) + # Pass `u0` and `p` to run `ReconstructInitializeprob` which will promote + # u0 and p of initializeprob + return remake(DDEProblem{iip}(f, u0, h, tspan, p; kwargs1..., kwargs...); u0, p) end function DiffEqBase.SDDEProblem(sys::AbstractODESystem, args...; kwargs...) @@ -1139,9 +1148,14 @@ function DiffEqBase.SDDEProblem{iip}(sys::AbstractODESystem, u0map = [], noise_rate_prototype = zeros(eltype(u0), size(noiseeqs)) end # Call `remake` so it runs initialization if it is trivial - return remake(SDDEProblem{iip}(f, f.g, u0, h, tspan, p; - noise_rate_prototype = - noise_rate_prototype, kwargs1..., kwargs...)) + # Pass `u0` and `p` to run `ReconstructInitializeprob` which will promote + # u0 and p of initializeprob + return remake( + SDDEProblem{iip}(f, f.g, u0, h, tspan, p; + noise_rate_prototype = + noise_rate_prototype, kwargs1..., kwargs...); + u0, + p) end """ diff --git a/src/systems/diffeqs/sdesystem.jl b/src/systems/diffeqs/sdesystem.jl index 3fa1302630..f77b245b8a 100644 --- a/src/systems/diffeqs/sdesystem.jl +++ b/src/systems/diffeqs/sdesystem.jl @@ -818,8 +818,13 @@ function DiffEqBase.SDEProblem{iip, specialize}( kwargs = filter_kwargs(kwargs) # Call `remake` so it runs initialization if it is trivial - return remake(SDEProblem{iip}(f, u0, tspan, p; callback = cbs, noise, - noise_rate_prototype = noise_rate_prototype, kwargs...)) + # Pass `u0` and `p` to run `ReconstructInitializeprob` which will promote + # u0 and p of initializeprob + return remake( + SDEProblem{iip}(f, u0, tspan, p; callback = cbs, noise, + noise_rate_prototype = noise_rate_prototype, kwargs...); + u0, + p) end function DiffEqBase.SDEProblem(sys::ODESystem, args...; kwargs...) diff --git a/src/systems/nonlinear/nonlinearsystem.jl b/src/systems/nonlinear/nonlinearsystem.jl index 856822492b..c87d71abf5 100644 --- a/src/systems/nonlinear/nonlinearsystem.jl +++ b/src/systems/nonlinear/nonlinearsystem.jl @@ -557,7 +557,9 @@ function DiffEqBase.NonlinearProblem{iip}(sys::NonlinearSystem, u0map, check_length, kwargs...) pt = something(get_metadata(sys), StandardNonlinearProblem()) # Call `remake` so it runs initialization if it is trivial - return remake(NonlinearProblem{iip}(f, u0, p, pt; filter_kwargs(kwargs)...)) + # Pass `u0` and `p` to run `ReconstructInitializeprob` which will promote + # u0 and p of initializeprob + return remake(NonlinearProblem{iip}(f, u0, p, pt; filter_kwargs(kwargs)...); u0, p) end function DiffEqBase.NonlinearProblem(sys::AbstractODESystem, args...; kwargs...) @@ -591,7 +593,10 @@ function DiffEqBase.NonlinearLeastSquaresProblem{iip}(sys::NonlinearSystem, u0ma check_length, kwargs...) pt = something(get_metadata(sys), StandardNonlinearProblem()) # Call `remake` so it runs initialization if it is trivial - return remake(NonlinearLeastSquaresProblem{iip}(f, u0, p; filter_kwargs(kwargs)...)) + # Pass `u0` and `p` to run `ReconstructInitializeprob` which will promote + # u0 and p of initializeprob + return remake( + NonlinearLeastSquaresProblem{iip}(f, u0, p; filter_kwargs(kwargs)...); u0, p) end const TypeT = Union{DataType, UnionAll} From 8d2482c5b61ef18ee38a9c9da561d01ca6da7c57 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 15 Apr 2025 17:10:05 +0530 Subject: [PATCH 1323/2176] fix: fix `symconvert` --- src/systems/parameter_buffer.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/systems/parameter_buffer.jl b/src/systems/parameter_buffer.jl index 6035605a02..e1bb83238e 100644 --- a/src/systems/parameter_buffer.jl +++ b/src/systems/parameter_buffer.jl @@ -1,5 +1,5 @@ symconvert(::Type{Symbolics.Struct{T}}, x) where {T} = convert(T, x) -symconvert(::Type{T}, x) where {T} = convert(T, x) +symconvert(::Type{T}, x::V) where {T, V} = convert(promote_type(T, V), x) symconvert(::Type{Real}, x::Integer) = convert(Float16, x) symconvert(::Type{V}, x) where {V <: AbstractArray} = convert(V, symconvert.(eltype(V), x)) From 38724b2e38e180a8b8dae14cddf86a789dd376a5 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 15 Apr 2025 17:10:20 +0530 Subject: [PATCH 1324/2176] fix: allow specifying floating point type in `MTKParameters` --- src/systems/parameter_buffer.jl | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/systems/parameter_buffer.jl b/src/systems/parameter_buffer.jl index e1bb83238e..529be4eaf8 100644 --- a/src/systems/parameter_buffer.jl +++ b/src/systems/parameter_buffer.jl @@ -28,7 +28,7 @@ the default behavior). """ function MTKParameters( sys::AbstractSystem, p, u0 = Dict(); tofloat = false, - t0 = nothing, substitution_limit = 1000) + t0 = nothing, substitution_limit = 1000, floatT = nothing) ic = if has_index_cache(sys) && get_index_cache(sys) !== nothing get_index_cache(sys) else @@ -111,6 +111,9 @@ function MTKParameters( if ctype <: FnType ctype = fntype_to_function_type(ctype) end + if ctype == Real && floatT !== nothing + ctype = floatT + end val = symconvert(ctype, val) done = set_value(sym, val) if !done && Symbolics.isarraysymbolic(sym) From 57d7da10dc4fe8dd09ae690af84d21ba149c8b2e Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 15 Apr 2025 18:04:37 +0530 Subject: [PATCH 1325/2176] Revert "fix: use `remake` to promote intialization problem" This reverts commit 27e7efbf73ccbe3ccb48c67e49c2ea8fa990830e. --- src/systems/diffeqs/abstractodesystem.jl | 30 +++++++----------------- src/systems/diffeqs/sdesystem.jl | 9 ++----- src/systems/nonlinear/nonlinearsystem.jl | 9 ++----- 3 files changed, 12 insertions(+), 36 deletions(-) diff --git a/src/systems/diffeqs/abstractodesystem.jl b/src/systems/diffeqs/abstractodesystem.jl index c6b2eb8a87..62ddd12a08 100644 --- a/src/systems/diffeqs/abstractodesystem.jl +++ b/src/systems/diffeqs/abstractodesystem.jl @@ -817,9 +817,7 @@ function DiffEqBase.ODEProblem{iip, specialize}(sys::AbstractODESystem, u0map = end # Call `remake` so it runs initialization if it is trivial - # Pass `u0` and `p` to run `ReconstructInitializeprob` which will promote - # u0 and p of initializeprob - return remake(ODEProblem{iip}(f, u0, tspan, p, pt; kwargs1..., kwargs...); u0, p) + return remake(ODEProblem{iip}(f, u0, tspan, p, pt; kwargs1..., kwargs...)) end get_callback(prob::ODEProblem) = prob.kwargs[:callback] @@ -1042,14 +1040,9 @@ function DiffEqBase.DAEProblem{iip}(sys::AbstractODESystem, du0map, u0map, tspan end # Call `remake` so it runs initialization if it is trivial - # Pass `u0` and `p` to run `ReconstructInitializeprob` which will promote - # u0 and p of initializeprob - return remake( - DAEProblem{iip}( - f, du0, u0, tspan, p; differential_vars = differential_vars, - kwargs..., kwargs1...); - u0, - p) + return remake(DAEProblem{iip}( + f, du0, u0, tspan, p; differential_vars = differential_vars, + kwargs..., kwargs1...)) end function generate_history(sys::AbstractODESystem, u0; expression = Val{false}, kwargs...) @@ -1095,9 +1088,7 @@ function DiffEqBase.DDEProblem{iip}(sys::AbstractODESystem, u0map = [], kwargs1 = merge(kwargs1, (callback = cbs,)) end # Call `remake` so it runs initialization if it is trivial - # Pass `u0` and `p` to run `ReconstructInitializeprob` which will promote - # u0 and p of initializeprob - return remake(DDEProblem{iip}(f, u0, h, tspan, p; kwargs1..., kwargs...); u0, p) + return remake(DDEProblem{iip}(f, u0, h, tspan, p; kwargs1..., kwargs...)) end function DiffEqBase.SDDEProblem(sys::AbstractODESystem, args...; kwargs...) @@ -1148,14 +1139,9 @@ function DiffEqBase.SDDEProblem{iip}(sys::AbstractODESystem, u0map = [], noise_rate_prototype = zeros(eltype(u0), size(noiseeqs)) end # Call `remake` so it runs initialization if it is trivial - # Pass `u0` and `p` to run `ReconstructInitializeprob` which will promote - # u0 and p of initializeprob - return remake( - SDDEProblem{iip}(f, f.g, u0, h, tspan, p; - noise_rate_prototype = - noise_rate_prototype, kwargs1..., kwargs...); - u0, - p) + return remake(SDDEProblem{iip}(f, f.g, u0, h, tspan, p; + noise_rate_prototype = + noise_rate_prototype, kwargs1..., kwargs...)) end """ diff --git a/src/systems/diffeqs/sdesystem.jl b/src/systems/diffeqs/sdesystem.jl index f77b245b8a..3fa1302630 100644 --- a/src/systems/diffeqs/sdesystem.jl +++ b/src/systems/diffeqs/sdesystem.jl @@ -818,13 +818,8 @@ function DiffEqBase.SDEProblem{iip, specialize}( kwargs = filter_kwargs(kwargs) # Call `remake` so it runs initialization if it is trivial - # Pass `u0` and `p` to run `ReconstructInitializeprob` which will promote - # u0 and p of initializeprob - return remake( - SDEProblem{iip}(f, u0, tspan, p; callback = cbs, noise, - noise_rate_prototype = noise_rate_prototype, kwargs...); - u0, - p) + return remake(SDEProblem{iip}(f, u0, tspan, p; callback = cbs, noise, + noise_rate_prototype = noise_rate_prototype, kwargs...)) end function DiffEqBase.SDEProblem(sys::ODESystem, args...; kwargs...) diff --git a/src/systems/nonlinear/nonlinearsystem.jl b/src/systems/nonlinear/nonlinearsystem.jl index c87d71abf5..856822492b 100644 --- a/src/systems/nonlinear/nonlinearsystem.jl +++ b/src/systems/nonlinear/nonlinearsystem.jl @@ -557,9 +557,7 @@ function DiffEqBase.NonlinearProblem{iip}(sys::NonlinearSystem, u0map, check_length, kwargs...) pt = something(get_metadata(sys), StandardNonlinearProblem()) # Call `remake` so it runs initialization if it is trivial - # Pass `u0` and `p` to run `ReconstructInitializeprob` which will promote - # u0 and p of initializeprob - return remake(NonlinearProblem{iip}(f, u0, p, pt; filter_kwargs(kwargs)...); u0, p) + return remake(NonlinearProblem{iip}(f, u0, p, pt; filter_kwargs(kwargs)...)) end function DiffEqBase.NonlinearProblem(sys::AbstractODESystem, args...; kwargs...) @@ -593,10 +591,7 @@ function DiffEqBase.NonlinearLeastSquaresProblem{iip}(sys::NonlinearSystem, u0ma check_length, kwargs...) pt = something(get_metadata(sys), StandardNonlinearProblem()) # Call `remake` so it runs initialization if it is trivial - # Pass `u0` and `p` to run `ReconstructInitializeprob` which will promote - # u0 and p of initializeprob - return remake( - NonlinearLeastSquaresProblem{iip}(f, u0, p; filter_kwargs(kwargs)...); u0, p) + return remake(NonlinearLeastSquaresProblem{iip}(f, u0, p; filter_kwargs(kwargs)...)) end const TypeT = Union{DataType, UnionAll} From 9e6e8942ec8ab21548043a05801f2317bfc699af Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 15 Apr 2025 18:17:09 +0530 Subject: [PATCH 1326/2176] fix: use `remake_initialization_data` to promote the initialization problem --- src/systems/problem_utils.jl | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/systems/problem_utils.jl b/src/systems/problem_utils.jl index 877b844a32..8a343baf7b 100644 --- a/src/systems/problem_utils.jl +++ b/src/systems/problem_utils.jl @@ -896,6 +896,16 @@ function process_SciMLProblem( du0 = nothing end + if build_initializeprob + t0 = t + if is_time_dependent(sys) && t0 === nothing + t0 = zero(floatT) + end + initialization_data = SciMLBase.remake_initialization_data( + kwargs.initialization_data, kwargs, u0, t0, p, u0, p) + kwargs = merge(kwargs,) + end + f = constructor(sys, dvs, ps, u0; p = p, eval_expression = eval_expression, eval_module = eval_module, From 662217b28fc301b65d7e18a3a9a9f05e56ff22fa Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 15 Apr 2025 20:52:57 +0530 Subject: [PATCH 1327/2176] fix: promote initializeprob in `remake_initialization_data` --- src/systems/nonlinear/initializesystem.jl | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/systems/nonlinear/initializesystem.jl b/src/systems/nonlinear/initializesystem.jl index 3b90bfc66a..4d08042533 100644 --- a/src/systems/nonlinear/initializesystem.jl +++ b/src/systems/nonlinear/initializesystem.jl @@ -649,7 +649,8 @@ function SciMLBase.remake_initialization_data( kws = maybe_build_initialization_problem( sys, op, u0map, pmap, t0, defs, guesses, missing_unknowns; use_scc, initialization_eqs, allow_incomplete = true) - return get(kws, :initialization_data, nothing) + + return SciMLBase.remake_initialization_data(sys, kws, newu0, t0, newp, newu0, newp) end function SciMLBase.late_binding_update_u0_p( From f9cdd1684e852f0a6cb181dfa9b2b06a56c42289 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 15 Apr 2025 20:53:18 +0530 Subject: [PATCH 1328/2176] fix: respect `floatT` in `maybe_build_initialization_problem` --- src/systems/problem_utils.jl | 38 +++++++++++++++++++++++++++--------- 1 file changed, 29 insertions(+), 9 deletions(-) diff --git a/src/systems/problem_utils.jl b/src/systems/problem_utils.jl index 8a343baf7b..4cc03b52ab 100644 --- a/src/systems/problem_utils.jl +++ b/src/systems/problem_utils.jl @@ -641,6 +641,22 @@ function maybe_build_initialization_problem( initializeprob = ModelingToolkit.InitializationProblem{true, SciMLBase.FullSpecialize}( sys, t, u0map, pmap; guesses, kwargs...) + if state_values(initializeprob) !== nothing + initializeprob = remake(initializeprob; u0 = floatT.(state_values(initializeprob))) + end + initp = parameter_values(initializeprob) + if is_split(sys) + buffer, repack, _ = SciMLStructures.canonicalize(SciMLStructures.Tunable(), initp) + initp = repack(floatT.(buffer)) + buffer, repack, _ = SciMLStructures.canonicalize(SciMLStructures.Initials(), initp) + initp = repack(floatT.(buffer)) + elseif initp isa AbstractArray + initp′ = similar(initp, floatT) + copyto!(initp′, initp) + initp = initp′ + end + initializeprob = remake(initializeprob; p = initp) + meta = get_metadata(initializeprob.f.sys) if is_time_dependent(sys) @@ -800,15 +816,19 @@ function process_SciMLProblem( u0map, pmap, defs, cmap, dvs, ps) floatT = Bool - for (k, v) in op - symbolic_type(v) == NotSymbolic() || continue - is_array_of_symbolics(v) && continue - - if v isa AbstractArray - isconcretetype(eltype(v)) || continue - floatT = promote_type(floatT, eltype(v)) - elseif v isa Real && isconcretetype(v) - floatT = promote_type(floatT, typeof(v)) + if u0Type <: AbstractArray && isconcretetype(eltype(u0Type)) && eltype(u0Type) <: Real + floatT = eltype(u0Type) + else + for (k, v) in op + symbolic_type(v) == NotSymbolic() || continue + is_array_of_symbolics(v) && continue + + if v isa AbstractArray + isconcretetype(eltype(v)) || continue + floatT = promote_type(floatT, eltype(v)) + elseif v isa Real && isconcretetype(v) + floatT = promote_type(floatT, typeof(v)) + end end end floatT = float(floatT) From 9fe75863d927ba29a556fc1c8bad2ad67cade42d Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 15 Apr 2025 21:05:00 +0530 Subject: [PATCH 1329/2176] fix: respect `tofloat` when using `floatT`, default to not promoting --- src/systems/problem_utils.jl | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/systems/problem_utils.jl b/src/systems/problem_utils.jl index 4cc03b52ab..96444bc31f 100644 --- a/src/systems/problem_utils.jl +++ b/src/systems/problem_utils.jl @@ -343,7 +343,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, container_type = Array, floatT = Float64, + tofloat = true, container_type = Array, floatT = Nothing, toterm = default_toterm, promotetoconcrete = nothing, check = true, allow_symbolic = false, is_initializeprob = false) isempty(vars) && return nothing @@ -366,8 +366,9 @@ function better_varmap_to_vars(varmap::AbstractDict, vars::Vector; is_initializeprob ? throw(MissingGuessError(missingsyms, missingvals)) : throw(UnexpectedSymbolicValueInVarmap(missingsyms[1], missingvals[1])) end - - vals = floatT.(vals) + if tofloat && !(floatT == Nothing) + vals = floatT.(vals) + end end if container_type <: Union{AbstractDict, Tuple, Nothing, SciMLBase.NullParameters} From a8e2495964d04cd7818c43b841aea434f7a1d5c4 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 15 Apr 2025 21:05:14 +0530 Subject: [PATCH 1330/2176] fix: do not require `isconcretetype` when discovering `floatT` --- src/systems/problem_utils.jl | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/systems/problem_utils.jl b/src/systems/problem_utils.jl index 96444bc31f..51c018bacc 100644 --- a/src/systems/problem_utils.jl +++ b/src/systems/problem_utils.jl @@ -817,7 +817,7 @@ function process_SciMLProblem( u0map, pmap, defs, cmap, dvs, ps) floatT = Bool - if u0Type <: AbstractArray && isconcretetype(eltype(u0Type)) && eltype(u0Type) <: Real + if u0Type <: AbstractArray && eltype(u0Type) <: Real floatT = eltype(u0Type) else for (k, v) in op @@ -825,9 +825,8 @@ function process_SciMLProblem( is_array_of_symbolics(v) && continue if v isa AbstractArray - isconcretetype(eltype(v)) || continue floatT = promote_type(floatT, eltype(v)) - elseif v isa Real && isconcretetype(v) + elseif v isa Real floatT = promote_type(floatT, typeof(v)) end end From 6023efbad37a8e0326ab7a5e8990feb3296959dd Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 16 Apr 2025 10:42:05 +0530 Subject: [PATCH 1331/2176] refactor: add `float_type_from_varmap` --- src/systems/problem_utils.jl | 34 ++++++++++++++++++++++------------ 1 file changed, 22 insertions(+), 12 deletions(-) diff --git a/src/systems/problem_utils.jl b/src/systems/problem_utils.jl index 51c018bacc..0750585905 100644 --- a/src/systems/problem_utils.jl +++ b/src/systems/problem_utils.jl @@ -713,6 +713,26 @@ function maybe_build_initialization_problem( initializeprobpmap)) end +""" + $(TYPEDSIGNATURES) + +Calculate the floating point type to use from the given `varmap` by looking at variables +with a constant value. +""" +function float_type_from_varmap(varmap, floatT = Bool) + for (k, v) in varmap + symbolic_type(v) == NotSymbolic() || continue + is_array_of_symbolics(v) && continue + + if v isa AbstractArray + floatT = promote_type(floatT, eltype(v)) + elseif v isa Real + floatT = promote_type(floatT, typeof(v)) + end + end + return float(floatT) +end + """ $(TYPEDSIGNATURES) @@ -818,20 +838,10 @@ function process_SciMLProblem( floatT = Bool if u0Type <: AbstractArray && eltype(u0Type) <: Real - floatT = eltype(u0Type) + floatT = float(eltype(u0Type)) else - for (k, v) in op - symbolic_type(v) == NotSymbolic() || continue - is_array_of_symbolics(v) && continue - - if v isa AbstractArray - floatT = promote_type(floatT, eltype(v)) - elseif v isa Real - floatT = promote_type(floatT, typeof(v)) - end - end + floatT = float_type_from_varmap(op, floatT) end - floatT = float(floatT) if !is_time_dependent(sys) || is_initializesystem(sys) add_observed_equations!(u0map, obs) From ead8682fd88b02ff019d6e244e1100c90fc72631 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 16 Apr 2025 10:42:16 +0530 Subject: [PATCH 1332/2176] fix: infer `floatT` in `MTKParameters` constructor if not provided --- src/systems/parameter_buffer.jl | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/systems/parameter_buffer.jl b/src/systems/parameter_buffer.jl index 529be4eaf8..5fb830290f 100644 --- a/src/systems/parameter_buffer.jl +++ b/src/systems/parameter_buffer.jl @@ -56,6 +56,10 @@ function MTKParameters( op[get_iv(sys)] = t0 end + if floatT === nothing + floatT = float(float_type_from_varmap(op)) + end + isempty(missing_pars) || throw(MissingParametersError(collect(missing_pars))) evaluate_varmap!(op, ps; limit = substitution_limit) From cd19c7edab02ec27ed1015230c217ac9cff3a04d Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 16 Apr 2025 10:42:26 +0530 Subject: [PATCH 1333/2176] fix: infer `floatT` in `remake_initialization_data` --- src/systems/nonlinear/initializesystem.jl | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/systems/nonlinear/initializesystem.jl b/src/systems/nonlinear/initializesystem.jl index 4d08042533..ec25b9b660 100644 --- a/src/systems/nonlinear/initializesystem.jl +++ b/src/systems/nonlinear/initializesystem.jl @@ -646,9 +646,10 @@ function SciMLBase.remake_initialization_data( op, missing_unknowns, missing_pars = build_operating_point!(sys, u0map, pmap, defs, cmap, dvs, ps) + floatT = float_type_from_varmap(op) kws = maybe_build_initialization_problem( sys, op, u0map, pmap, t0, defs, guesses, missing_unknowns; - use_scc, initialization_eqs, allow_incomplete = true) + use_scc, initialization_eqs, floatT, allow_incomplete = true) return SciMLBase.remake_initialization_data(sys, kws, newu0, t0, newp, newu0, newp) end From b685ab5912dfee89682c7167f7c2c4e2afe2afb5 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 8 Apr 2025 17:22:25 +0530 Subject: [PATCH 1334/2176] feat: add `@generated` method for `_remake_buffer` for type-inference --- src/systems/parameter_buffer.jl | 66 +++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) diff --git a/src/systems/parameter_buffer.jl b/src/systems/parameter_buffer.jl index 36105c0de8..3122ace245 100644 --- a/src/systems/parameter_buffer.jl +++ b/src/systems/parameter_buffer.jl @@ -496,6 +496,72 @@ end function SymbolicIndexingInterface.remake_buffer(indp, oldbuf::MTKParameters, idxs, vals) _remake_buffer(indp, oldbuf, idxs, vals) end + +# For type-inference when using `SII.setp_oop` +@generated function _remake_buffer( + indp, oldbuf::MTKParameters{T, I, D, C, N, H}, idxs::Tuple{Vararg{ParameterIndex}}, + vals::Union{AbstractArray, Tuple}; validate = true) where {T, I, D, C, N, H} + valtype(i) = vals <: AbstractArray ? eltype(vals) : fieldtype(vals, i) + tunablesT = eltype(T) + for (i, idxT) in enumerate(fieldtypes(idxs)) + idxT <: ParameterIndex{SciMLStructures.Tunable} || continue + tunablesT = promote_type(tunablesT, valtype(i)) + end + initialsT = eltype(I) + for (i, idxT) in enumerate(fieldtypes(idxs)) + idxT <: ParameterIndex{SciMLStructures.Initials} || continue + initialsT = promote_type(initialsT, valtype(i)) + end + discretesT = ntuple(Val(fieldcount(D))) do i + bufT = eltype(fieldtype(D, i)) + for (j, idxT) in enumerate(fieldtypes(idxs)) + idxT <: ParameterIndex{SciMLStructures.Discrete, i} || continue + bufT = promote_type(bufT, valtype(i)) + end + bufT + end + constantsT = ntuple(Val(fieldcount(C))) do i + bufT = eltype(fieldtype(C, i)) + for (j, idxT) in enumerate(fieldtypes(idxs)) + idxT <: ParameterIndex{SciMLStructures.Constants, i} || continue + bufT = promote_type(bufT, valtype(i)) + end + bufT + end + nonnumericT = ntuple(Val(fieldcount(N))) do i + bufT = eltype(fieldtype(N, i)) + for (j, idxT) in enumerate(fieldtypes(idxs)) + idxT <: ParameterIndex{Nonnumeric, i} || continue + bufT = promote_type(bufT, valtype(i)) + end + bufT + end + + expr = quote + tunables = $similar(oldbuf.tunable, $tunablesT) + copyto!(tunables, oldbuf.tunable) + initials = $similar(oldbuf.initials, $initialsT) + copyto!(initials, oldbuf.initials) + discretes = $(Expr(:tuple, + (:($similar(oldbuf.discrete[$i], $(discretesT[i]))) for i in 1:length(discretesT))...)) + $((:($copyto!(discretes[$i], oldbuf.discrete[$i])) for i in 1:length(discretesT))...) + constants = $(Expr(:tuple, + (:($similar(oldbuf.constant[$i], $(constantsT[i]))) for i in 1:length(constantsT))...)) + $((:($copyto!(constants[$i], oldbuf.constant[$i])) for i in 1:length(constantsT))...) + nonnumerics = $(Expr(:tuple, + (:($similar(oldbuf.nonnumeric[$i], $(nonnumericT[i]))) for i in 1:length(nonnumericT))...)) + $((:($copyto!(nonnumerics[$i], oldbuf.nonnumeric[$i])) for i in 1:length(nonnumericT))...) + newbuf = MTKParameters( + tunables, initials, discretes, constants, nonnumerics, copy.(oldbuf.caches)) + end + for i in 1:fieldcount(idxs) + push!(expr.args, :($setindex!(newbuf, vals[$i], idxs[$i]))) + end + push!(expr.args, :(return newbuf)) + + return expr +end + function _remake_buffer(indp, oldbuf::MTKParameters, idxs, vals; validate = true) newbuf = @set oldbuf.tunable = similar(oldbuf.tunable, Any) @set! newbuf.initials = similar(oldbuf.initials, Any) From 0ba61201650b7e3a2d3adbb388e7a546a6bb8e63 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 15 Apr 2025 13:04:51 +0530 Subject: [PATCH 1335/2176] build: bump SII compat --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 63aef5b8da..693a81e148 100644 --- a/Project.toml +++ b/Project.toml @@ -149,7 +149,7 @@ SpecialFunctions = "0.7, 0.8, 0.9, 0.10, 1.0, 2" StaticArrays = "0.10, 0.11, 0.12, 1.0" StochasticDelayDiffEq = "1.8.1" StochasticDiffEq = "6.72.1" -SymbolicIndexingInterface = "0.3.37" +SymbolicIndexingInterface = "0.3.39" SymbolicUtils = "3.25.1" Symbolics = "6.37" URIs = "1" From 96694de0e47208da1e2d1e0f557a9c7f271e6fa2 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 16 Apr 2025 13:06:34 +0530 Subject: [PATCH 1336/2176] feat: handle more type-stable cases in `@generated _remake_buffer` --- src/systems/parameter_buffer.jl | 72 ++++++++++++++++++++++++++------- 1 file changed, 57 insertions(+), 15 deletions(-) diff --git a/src/systems/parameter_buffer.jl b/src/systems/parameter_buffer.jl index 3122ace245..0916bb1185 100644 --- a/src/systems/parameter_buffer.jl +++ b/src/systems/parameter_buffer.jl @@ -499,40 +499,76 @@ end # For type-inference when using `SII.setp_oop` @generated function _remake_buffer( - indp, oldbuf::MTKParameters{T, I, D, C, N, H}, idxs::Tuple{Vararg{ParameterIndex}}, - vals::Union{AbstractArray, Tuple}; validate = true) where {T, I, D, C, N, H} - valtype(i) = vals <: AbstractArray ? eltype(vals) : fieldtype(vals, i) + indp, oldbuf::MTKParameters{T, I, D, C, N, H}, + idxs::Union{Tuple{Vararg{ParameterIndex}}, AbstractArray{<:ParameterIndex{P}}}, + vals::Union{AbstractArray, Tuple}; validate = true) where {T, I, D, C, N, H, P} + + # fallback to non-generated method if values aren't type-stable + if vals <: AbstractArray && !isconcretetype(eltype(vals)) + return quote + $_remake_buffer(indp, oldbuf, collect(idxs), vals; validate) + end + end + + # given an index in idxs/vals and the current `eltype` of the buffer, + # return the promoted eltype of the buffer + function promote_valtype(i, valT) + # tuples have distinct types, arrays have a common eltype + valT′ = vals <: AbstractArray ? eltype(vals) : fieldtype(vals, i) + # if the buffer is a scalarized buffer but the variable is an array + # e.g. an array tunable, take the eltype + if valT′ <: AbstractArray && !(valT <: AbstractArray) + valT′ = eltype(valT′) + end + return promote_type(valT, valT′) + end + + # types of the idxs + idxtypes = if idxs <: AbstractArray + # if both are arrays, there is only one possible type to check + if vals <: AbstractArray + (eltype(idxs),) + else + # if `vals` is a tuple, we repeat `eltype(idxs)` to check against + # every possible type of the buffer + ntuple(Returns(eltype(idxs)), Val(fieldcount(vals))) + end + else + # `idxs` is a tuple, so we check against all buffers + fieldtypes(idxs) + end + # promote types tunablesT = eltype(T) - for (i, idxT) in enumerate(fieldtypes(idxs)) + for (i, idxT) in enumerate(idxtypes) idxT <: ParameterIndex{SciMLStructures.Tunable} || continue - tunablesT = promote_type(tunablesT, valtype(i)) + tunablesT = promote_valtype(i, tunablesT) end initialsT = eltype(I) - for (i, idxT) in enumerate(fieldtypes(idxs)) + for (i, idxT) in enumerate(idxtypes) idxT <: ParameterIndex{SciMLStructures.Initials} || continue - initialsT = promote_type(initialsT, valtype(i)) + initialsT = promote_valtype(i, initialsT) end discretesT = ntuple(Val(fieldcount(D))) do i bufT = eltype(fieldtype(D, i)) - for (j, idxT) in enumerate(fieldtypes(idxs)) + for (j, idxT) in enumerate(idxtypes) idxT <: ParameterIndex{SciMLStructures.Discrete, i} || continue - bufT = promote_type(bufT, valtype(i)) + bufT = promote_valtype(i, bufT) end bufT end constantsT = ntuple(Val(fieldcount(C))) do i bufT = eltype(fieldtype(C, i)) - for (j, idxT) in enumerate(fieldtypes(idxs)) + for (j, idxT) in enumerate(idxtypes) idxT <: ParameterIndex{SciMLStructures.Constants, i} || continue - bufT = promote_type(bufT, valtype(i)) + bufT = promote_valtype(i, bufT) end bufT end nonnumericT = ntuple(Val(fieldcount(N))) do i bufT = eltype(fieldtype(N, i)) - for (j, idxT) in enumerate(fieldtypes(idxs)) + for (j, idxT) in enumerate(idxtypes) idxT <: ParameterIndex{Nonnumeric, i} || continue - bufT = promote_type(bufT, valtype(i)) + bufT = promote_valtype(i, bufT) end bufT end @@ -554,8 +590,14 @@ end newbuf = MTKParameters( tunables, initials, discretes, constants, nonnumerics, copy.(oldbuf.caches)) end - for i in 1:fieldcount(idxs) - push!(expr.args, :($setindex!(newbuf, vals[$i], idxs[$i]))) + if idxs <: AbstractArray + push!(expr.args, :(for (idx, val) in zip(idxs, vals) + $setindex!(newbuf, val, idx) + end)) + else + for i in 1:fieldcount(idxs) + push!(expr.args, :($setindex!(newbuf, vals[$i], idxs[$i]))) + end end push!(expr.args, :(return newbuf)) From c5e8240458b503f2cfd8e92ad9c0c4b2e73456fb Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 16 Apr 2025 13:06:48 +0530 Subject: [PATCH 1337/2176] test: test type-stability of `remake_buffer` in supported cases --- test/mtkparameters.jl | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/test/mtkparameters.jl b/test/mtkparameters.jl index f9d71f00bc..22201b1988 100644 --- a/test/mtkparameters.jl +++ b/test/mtkparameters.jl @@ -91,6 +91,27 @@ end @test getp(sys, f)(newps) isa Matrix{UInt} @test getp(sys, g)(newps) isa Vector{Float32} +@testset "Type-stability of `remake_buffer`" begin + prob = ODEProblem(sys, [], (0.0, 1.0), ivs) + + idxs = (a, c, d, e, f, g, h) + vals = (1.0, 2.0, 3, ones(3), ones(Int, 3, 3), ones(2), "a") + + setter = setsym_oop(prob, idxs) + @test_nowarn @inferred setter(prob, vals) + @test_throws ErrorException @inferred setter(prob, collect(vals)) + + idxs = (a, c, e...) + vals = Float16[1.0, 2.0, 3.0, 4.0, 5.0] + setter = setsym_oop(prob, idxs) + @test_nowarn @inferred setter(prob, vals) + + idxs = [a, e] + vals = (Float16(1.0), ForwardDiff.Dual{Nothing, Float16, 0}[1.0, 2.0, 3.0]) + setter = setsym_oop(prob, idxs) + @test_nowarn @inferred setter(prob, vals) +end + ps = MTKParameters(sys, ivs) function loss(value, sys, ps) @test value isa ForwardDiff.Dual From 06a63df059223f6625e0a5b221102563f53825b4 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 16 Apr 2025 13:39:54 +0530 Subject: [PATCH 1338/2176] fix: fix stack overflow in `@generated _remake_buffer` fallback --- src/systems/parameter_buffer.jl | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/systems/parameter_buffer.jl b/src/systems/parameter_buffer.jl index 0916bb1185..672fb58795 100644 --- a/src/systems/parameter_buffer.jl +++ b/src/systems/parameter_buffer.jl @@ -506,7 +506,7 @@ end # fallback to non-generated method if values aren't type-stable if vals <: AbstractArray && !isconcretetype(eltype(vals)) return quote - $_remake_buffer(indp, oldbuf, collect(idxs), vals; validate) + $__remake_buffer(indp, oldbuf, collect(idxs), vals; validate) end end @@ -605,6 +605,10 @@ end end function _remake_buffer(indp, oldbuf::MTKParameters, idxs, vals; validate = true) + return __remake_buffer(indp, oldbuf, idxs, vals; validate) +end + +function __remake_buffer(indp, oldbuf::MTKParameters, idxs, vals; validate = true) newbuf = @set oldbuf.tunable = similar(oldbuf.tunable, Any) @set! newbuf.initials = similar(oldbuf.initials, Any) @set! newbuf.discrete = Tuple(similar(buf, Any) for buf in newbuf.discrete) From 0618a68c5ed0f7888bca2f439bc3c04e9c962e48 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 16 Apr 2025 13:36:40 +0530 Subject: [PATCH 1339/2176] fix: scalarize `Initial` parameters for `split = false` systems --- src/systems/abstractsystem.jl | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index 72f3132889..4ad08bb1f4 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -696,7 +696,7 @@ end supports_initialization(sys::AbstractSystem) = true -function add_initialization_parameters(sys::AbstractSystem) +function add_initialization_parameters(sys::AbstractSystem; split = true) @assert !has_systems(sys) || isempty(get_systems(sys)) supports_initialization(sys) || return sys is_initializesystem(sys) && return sys @@ -711,7 +711,7 @@ function add_initialization_parameters(sys::AbstractSystem) obs, eqs = unhack_observed(observed(sys), eqs) for x in Iterators.flatten((unknowns(sys), Iterators.map(eq -> eq.lhs, obs))) x = unwrap(x) - if iscall(x) && operation(x) == getindex + if iscall(x) && operation(x) == getindex && split push!(all_initialvars, arguments(x)[1]) else push!(all_initialvars, x) @@ -788,7 +788,7 @@ function complete( end sys = newsys if add_initial_parameters - sys = add_initialization_parameters(sys) + sys = add_initialization_parameters(sys; split) end end if split && has_index_cache(sys) From 8147aabad454263c2a5279394c3141145f9d2002 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 16 Apr 2025 13:36:54 +0530 Subject: [PATCH 1340/2176] fix: recognize scalarized `Initial` parameters in `parameters` --- src/systems/abstractsystem.jl | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index 4ad08bb1f4..29338d0722 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -1465,7 +1465,11 @@ function parameters(sys::AbstractSystem; initial_parameters = false) result = unique(isempty(systems) ? ps : [ps; reduce(vcat, namespace_parameters.(systems))]) if !initial_parameters && !is_initializesystem(sys) - filter!(x -> !iscall(x) || !isa(operation(x), Initial), result) + filter!(result) do sym + return !(isoperator(sym, Initial) || + iscall(sym) && operation(sym) == getindex && + isoperator(arguments(sym)[1], Initial)) + end end return result end From ec4fd42d6c31888f374d4852971683abecd33a54 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 16 Apr 2025 13:37:07 +0530 Subject: [PATCH 1341/2176] test: test mix of array initials and scalar parameters with `split = false` --- test/initial_values.jl | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/test/initial_values.jl b/test/initial_values.jl index 01a053a9bf..79b6b8e067 100644 --- a/test/initial_values.jl +++ b/test/initial_values.jl @@ -265,3 +265,19 @@ end @test eltype(oprob.u0) == Float32 @test eltype(eltype(sol.u)) == Float32 end + +@testset "Array initials and scalar parameters with `split = false`" begin + @variables x(t)[1:2] + @parameters p + @mtkbuild sys=ODESystem([D(x[1]) ~ x[1], D(x[2]) ~ x[2] + p], t) split=false + ps = Set(parameters(sys; initial_parameters = true)) + @test length(ps) == 5 + for i in 1:2 + @test Initial(x[i]) in ps + @test Initial(D(x[i])) in ps + end + @test p in ps + prob = ODEProblem(sys, [x => ones(2)], (0.0, 1.0), [p => 1.0]) + @test prob.p isa Vector{Float64} + @test length(prob.p) == 5 +end From 3dea97cd9430bc9ae75c81b96921587d4eec2650 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 16 Apr 2025 16:36:53 +0530 Subject: [PATCH 1342/2176] test: account for new scalarized initials in tests --- test/odesystem.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/odesystem.jl b/test/odesystem.jl index a45ed99f35..4b76da6e9d 100644 --- a/test/odesystem.jl +++ b/test/odesystem.jl @@ -1305,7 +1305,7 @@ end ps = zeros(8) setp(sys2, x)(ps, 2ones(2)) setp(sys2, p)(ps, 2ones(2, 2)) - @test_nowarn fn2(ones(4), 2ones(6), 4.0) + @test_nowarn fn2(ones(4), 2ones(14), 4.0) end # https://github.com/SciML/ModelingToolkit.jl/issues/2969 @@ -1416,7 +1416,7 @@ end obsfn = ModelingToolkit.build_explicit_observed_function( sys1, u + x + p[1:2]; inputs = [x...]) - @test obsfn(ones(2), 2ones(2), 3ones(4), 4.0) == 6ones(2) + @test obsfn(ones(2), 2ones(2), 3ones(12), 4.0) == 6ones(2) end @testset "Passing `nothing` to `u0`" begin From 756d80dea9f1bf0e6662d4c86de988f6e2284a8d Mon Sep 17 00:00:00 2001 From: Venkateshprasad <32921645+ven-k@users.noreply.github.com> Date: Wed, 2 Apr 2025 08:41:32 +0000 Subject: [PATCH 1343/2176] feat(mtkmodel): provision to specify the type of System --- docs/src/basics/MTKLanguage.md | 39 ++++++++++++++++++++++++++++------ src/systems/model_parsing.jl | 23 ++++++++++++++------ test/model_parsing.jl | 15 +++++++++++++ 3 files changed, 65 insertions(+), 12 deletions(-) diff --git a/docs/src/basics/MTKLanguage.md b/docs/src/basics/MTKLanguage.md index 2bcec99b6a..e91f2bcb67 100644 --- a/docs/src/basics/MTKLanguage.md +++ b/docs/src/basics/MTKLanguage.md @@ -16,8 +16,9 @@ equations. ### [Defining components with `@mtkmodel`](@id mtkmodel) `@mtkmodel` is a convenience macro to define components. It returns -`ModelingToolkit.Model`, which includes a constructor that returns the ODESystem, a -`structure` dictionary with metadata, and flag `isconnector` which is set to `false`. +`ModelingToolkit.Model`, which includes a system constructor (`ODESystem` by +default), a `structure` dictionary with metadata, and flag `isconnector` which is +set to `false`. ### What can an MTK-Model definition have? @@ -26,7 +27,7 @@ equations. - `@description`: for describing the whole system with a human-readable string - `@components`: for listing sub-components of the system - `@constants`: for declaring constants - - `@defaults`: for passing `defaults` to ODESystem + - `@defaults`: for passing `defaults` to the system - `@equations`: for the list of equations - `@extend`: for extending a base system and unpacking its unknowns - `@icon` : for embedding the model icon @@ -196,7 +197,7 @@ getdefault(model_c3.model_a.k_array[2]) #### `@defaults` begin block - Default values can be passed as pairs. - - This is equivalent to passing `defaults` argument to `ODESystem`. + - This is equivalent to passing `defaults` argument to the system. #### `@continuous_events` begin block @@ -256,6 +257,32 @@ end - Any other Julia operations can be included with dedicated begin blocks. +### Setting the type of system: + +By default `@mtkmodel` returns an ODESystem. Different types of system can be +defined with the following syntax: + +``` +@mtkmodel ModelName::SystemType begin + ... +end + +``` + +Example: + +```@example mtkmodel-example +@mtkmodel Float2Bool::DiscreteSystem begin + @variables begin + u(t)::Float64 + y(t)::Bool + end + @equations begin + y ~ u != 0 + end +end +``` + ## Connectors Connectors are special models that can be used to connect different components together. @@ -270,7 +297,7 @@ MTK provides 3 distinct connectors: ### [Defining connectors with `@connector`](@id connector) `@connector` returns `ModelingToolkit.Model`. It includes a constructor that returns -a connector ODESystem, a `structure` dictionary with metadata, and flag `isconnector` +a connector system (`ODESystem` by default), a `structure` dictionary with metadata, and flag `isconnector` which is set to `true`. A simple connector can be defined with syntax similar to following example: @@ -498,7 +525,7 @@ end ## Build structurally simplified models: -`@mtkbuild` builds an instance of a component and returns a structurally simplied `ODESystem`. +`@mtkbuild` builds an instance of a component and returns a structurally simplied system. ```julia @mtkbuild sys = CustomModel() diff --git a/src/systems/model_parsing.jl b/src/systems/model_parsing.jl index 4632c1b889..195b02118e 100644 --- a/src/systems/model_parsing.jl +++ b/src/systems/model_parsing.jl @@ -7,7 +7,7 @@ ModelingToolkit component or connector with metadata $(FIELDS) """ struct Model{F, S} - """The constructor that returns ODESystem.""" + """The constructor that returns System.""" f::F """ The dictionary with metadata like keyword arguments (:kwargs), base @@ -29,8 +29,8 @@ Base.parentmodule(m::Model) = parentmodule(m.f) for f in (:connector, :mtkmodel) isconnector = f == :connector ? true : false @eval begin - macro $f(name::Symbol, body) - esc($(:_model_macro)(__module__, name, body, $isconnector)) + macro $f(fullname::Union{Expr, Symbol}, body) + esc($(:_model_macro)(__module__, fullname, body, $isconnector)) end end end @@ -41,7 +41,16 @@ function flatten_equations(eqs::Vector{Union{Equation, Vector{Equation}}}) foldl(flatten_equations, eqs; init = Equation[]) end -function _model_macro(mod, name, expr, isconnector) +function _model_macro(mod, fullname::Union{Expr, Symbol}, expr, isconnector) + if fullname isa Symbol + name, type = fullname, :System + else + if fullname.head == :(::) + name, type = fullname.args + else + error("`$fullname` is not a valid name.") + end + end exprs = Expr(:block) dict = Dict{Symbol, Any}( :constants => Dict{Symbol, Dict}(), @@ -61,7 +70,9 @@ function _model_macro(mod, name, expr, isconnector) push!(exprs.args, :(variables = [])) push!(exprs.args, :(parameters = [])) - push!(exprs.args, :(systems = ODESystem[])) + # We build `System` by default; vectors can't be created for `System` as it is + # a function. + push!(exprs.args, :(systems = ModelingToolkit.AbstractSystem[])) push!(exprs.args, :(equations = Union{Equation, Vector{Equation}}[])) push!(exprs.args, :(defaults = Dict{Num, Union{Number, Symbol, Function}}())) @@ -114,7 +125,7 @@ function _model_macro(mod, name, expr, isconnector) @inline pop_structure_dict!.( Ref(dict), [:constants, :defaults, :kwargs, :structural_parameters]) - sys = :($ODESystem($(flatten_equations)(equations), $iv, variables, parameters; + sys = :($type($(flatten_equations)(equations), $iv, variables, parameters; name, description = $description, systems, gui_metadata = $gui_metadata, defaults)) if length(ext) == 0 diff --git a/test/model_parsing.jl b/test/model_parsing.jl index 62c19d2055..e8464707de 100644 --- a/test/model_parsing.jl +++ b/test/model_parsing.jl @@ -1011,3 +1011,18 @@ end @test any(isequal(u), vars) end end + +@testset "Specify the type of system" begin + @mtkmodel Float2Bool::DiscreteSystem begin + @variables begin + u(t)::Float64 + y(t)::Bool + end + @equations begin + y ~ u != 0 + end + end + + @named sys = Float2Bool() + @test typeof(sys) == DiscreteSystem +end From 070659d488e4403724b6995306a70b36b43a25fe Mon Sep 17 00:00:00 2001 From: Venkateshprasad <32921645+ven-k@users.noreply.github.com> Date: Fri, 4 Apr 2025 02:51:43 +0000 Subject: [PATCH 1344/2176] test: rename test to not use `System` System is an exported function. And now it is used to build ODESystem by default --- test/initializationsystem.jl | 4 ++-- test/split_parameters.jl | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/test/initializationsystem.jl b/test/initializationsystem.jl index 0f40d4eaf1..4ade3481cb 100644 --- a/test/initializationsystem.jl +++ b/test/initializationsystem.jl @@ -216,7 +216,7 @@ end end end -@mtkmodel System begin +@mtkmodel HydraulicSystem begin @components begin res₁ = Orifice(p′ = 300e5) res₂ = Orifice(p′ = 0) @@ -234,7 +234,7 @@ end end end -@mtkbuild sys = System() +@mtkbuild sys = HydraulicSystem() initprob = ModelingToolkit.InitializationProblem(sys, 0.0) conditions = getfield.(equations(initprob.f.sys), :rhs) diff --git a/test/split_parameters.jl b/test/split_parameters.jl index 1052f4ad27..18fdb49a48 100644 --- a/test/split_parameters.jl +++ b/test/split_parameters.jl @@ -285,7 +285,7 @@ end end end - @mtkmodel System begin + @mtkmodel ApexSystem begin @components begin subsys = SubSystem() end @@ -300,7 +300,7 @@ end end end - @named sys = System() + @named sys = ApexSystem() sysref = complete(sys) sys2 = complete(sys; split = true, flatten = false) ps = Set(full_parameters(sys2)) From af36197e47126420269c8a14f4c1e2990f845f5d Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 22 Apr 2025 12:22:09 +0530 Subject: [PATCH 1345/2176] build: bump minor version --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 693a81e148..30580d961b 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "ModelingToolkit" uuid = "961ee093-0014-501f-94e3-6117800e7a78" authors = ["Yingbo Ma ", "Chris Rackauckas and contributors"] -version = "9.72.0" +version = "9.73.0" [deps] ADTypes = "47edcb42-4c32-4615-8424-f2b9edc5f35b" From 916652fb2cca055e88afcb830513fc8e70dbca46 Mon Sep 17 00:00:00 2001 From: Christopher Rackauckas Date: Tue, 22 Apr 2025 07:30:21 -0400 Subject: [PATCH 1346/2176] Add Neuroblox downstream test Is dependent on https://github.com/Neuroblox/Neuroblox.jl/pull/580 --- .github/workflows/Downstream.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/Downstream.yml b/.github/workflows/Downstream.yml index 1d1e4ce34e..8d8915a9cc 100644 --- a/.github/workflows/Downstream.yml +++ b/.github/workflows/Downstream.yml @@ -38,6 +38,8 @@ jobs: - {user: SciML, repo: MethodOfLines.jl, group: 2D_Diffusion} - {user: SciML, repo: MethodOfLines.jl, group: DAE} - {user: SciML, repo: ModelingToolkitNeuralNets.jl, group: All} + + - {user: Neuroblox, repo: Neuroblox.jl, group: NNPDE} steps: - uses: actions/checkout@v4 - uses: julia-actions/setup-julia@v1 From eafe44d8caac154cf48ddc8864dc41ae51a2c5d5 Mon Sep 17 00:00:00 2001 From: Fredrik Bagge Carlson Date: Tue, 22 Apr 2025 15:37:21 +0200 Subject: [PATCH 1347/2176] fix docs example --- docs/src/tutorials/disturbance_modeling.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/src/tutorials/disturbance_modeling.md b/docs/src/tutorials/disturbance_modeling.md index e9e06ccae4..b77a73b0c1 100644 --- a/docs/src/tutorials/disturbance_modeling.md +++ b/docs/src/tutorials/disturbance_modeling.md @@ -119,7 +119,7 @@ y &= g(x, u, p, t) \end{aligned} ``` -To make use of the model defined above for state estimation, we may want to generate a Julia functions for the dynamics ``f`` and the output equations ``g`` that we can plug into, e.g., a nonlinear version of a Kalman filter or a particle filter, etc. MTK contains utilities to do this, namely, [`generate_control_function`](@ref) and [`build_explicit_observed_function`](@ref) (described in more details in ["Input output"](@ref inputoutput)). These functions take keyword arguments `disturbance_inputs` and `disturbance_argument`, that indicate which variables in the model are considered part of ``w``, and whether or not these variables are to be added as function arguments to ``f``, i.e., whether we have ``f(x, u, p, t)`` or ``f(x, u, p, t, w)``. If we do not include the disturbance inputs as function arguments, MTK will assume that the ``w`` variables are all zero, but any dynamics associated with these variables, such as disturbance models, will be included in the generated function. This allows a state estimator to estimate the state of the disturbance model, provided that this state is [observable](https://en.wikipedia.org/wiki/Observability) from the measured outputs of the system. +To make use of the model defined above for state estimation, we may want to generate a Julia function for the dynamics ``f`` and the output equations ``g`` that we can plug into, e.g., a nonlinear version of a Kalman filter or a particle filter, etc. MTK contains utilities to do this, namely, [`generate_control_function`](@ref) and [`build_explicit_observed_function`](@ref) (described in more details in ["Input output"](@ref inputoutput)). These functions take keyword arguments `disturbance_inputs` and `disturbance_argument`, that indicate which variables in the model are considered part of ``w``, and whether or not these variables are to be added as function arguments to ``f``, i.e., whether we have ``f(x, u, p, t)`` or ``f(x, u, p, t, w)``. If we do not include the disturbance inputs as function arguments, MTK will assume that the ``w`` variables are all zero, but any dynamics associated with these variables, such as disturbance models, will be included in the generated function. This allows a state estimator to estimate the state of the disturbance model, provided that this state is [observable](https://en.wikipedia.org/wiki/Observability) from the measured outputs of the system. Below, we demonstrate @@ -196,7 +196,7 @@ p = MTKParameters(io_sys, op) u = zeros(1) # Control input w = zeros(length(disturbance_inputs)) # Disturbance input @test f_oop(x0, u, p, t, w) == zeros(5) -@test g(x, u, p, 0.0) == [0, 0, 0, 0] +@test g(x0, u, p, 0.0) == [0, 0, 0, 0] # Non-zero disturbance inputs should result in non-zero state derivatives. We call `sort` since we do not generally know the order of the state variables w = [1.0, 2.0] From efea3d7ff0717544709ec7850f7bf043ab089c91 Mon Sep 17 00:00:00 2001 From: Herman Sletmoen Date: Sun, 13 Apr 2025 13:36:53 +0200 Subject: [PATCH 1348/2176] Get observed variables with observables(sys) --- docs/src/tutorials/acausal_components.md | 7 +++++ src/ModelingToolkit.jl | 4 +-- src/systems/abstractsystem.jl | 28 +++++++++++++++++-- src/systems/diffeqs/abstractodesystem.jl | 2 +- src/systems/diffeqs/odesystem.jl | 4 +-- src/systems/diffeqs/sdesystem.jl | 2 +- src/systems/jumps/jumpsystem.jl | 2 +- src/systems/nonlinear/nonlinearsystem.jl | 2 +- .../optimization/constraints_system.jl | 2 +- .../optimization/optimizationsystem.jl | 2 +- src/systems/problem_utils.jl | 2 +- src/systems/systemstructure.jl | 2 +- test/serialization.jl | 2 +- test/structural_transformation/utils.jl | 4 +-- 14 files changed, 47 insertions(+), 18 deletions(-) diff --git a/docs/src/tutorials/acausal_components.md b/docs/src/tutorials/acausal_components.md index b97500a3e9..751b678dae 100644 --- a/docs/src/tutorials/acausal_components.md +++ b/docs/src/tutorials/acausal_components.md @@ -320,6 +320,7 @@ sol = solve(prob) plot(sol) ``` +By default, this plots only the unknown variables that had to be solved for. However, what if we wanted to plot the timeseries of a different variable? Do not worry, that information was not thrown away! Instead, transformations like `structural_simplify` simply change unknown variables into observables which are @@ -346,3 +347,9 @@ or we can plot the timeseries of the resistor's voltage: ```@example acausal plot(sol, idxs = [rc_model.resistor.v]) ``` + +Although it may be more confusing than helpful here, we can of course also plot all unknown and observed variables together: + +```@example acausal +plot(sol, idxs = [unknowns(rc_model); observables(rc_model)]) +``` diff --git a/src/ModelingToolkit.jl b/src/ModelingToolkit.jl index 9f69458528..d0f427bd14 100644 --- a/src/ModelingToolkit.jl +++ b/src/ModelingToolkit.jl @@ -85,8 +85,8 @@ import Symbolics: rename, get_variables!, _solve, hessian_sparsity, substituter, scalarize, getparent, hasderiv, hasdiff import DiffEqBase: @add_kwonly -export independent_variables, unknowns, parameters, full_parameters, continuous_events, - discrete_events +export independent_variables, unknowns, observables, parameters, full_parameters, + continuous_events, discrete_events @reexport using Symbolics @reexport using UnPack RuntimeGeneratedFunctions.init(@__MODULE__) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index 29338d0722..2b58349ec4 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -430,7 +430,7 @@ function has_observed_with_lhs(sys, sym) if has_index_cache(sys) && (ic = get_index_cache(sys)) !== nothing return haskey(ic.observed_syms_to_timeseries, sym) else - return any(isequal(sym), [eq.lhs for eq in observed(sys)]) + return any(isequal(sym), observables(sys)) end end @@ -489,7 +489,7 @@ function _all_ts_idxs!(ts_idxs, ::ScalarSymbolic, sys, sym::Symbol) if has_index_cache(sys) && (ic = get_index_cache(sys)) !== nothing return _all_ts_idxs!(ts_idxs, sys, ic.symbol_to_variable[sym]) elseif is_variable(sys, sym) || is_independent_variable(sys, sym) || - any(isequal(sym), [getname(eq.lhs) for eq in observed(sys)]) + any(isequal(sym), getname.(observables(sys))) push!(ts_idxs, ContinuousTimeseries()) elseif is_timeseries_parameter(sys, sym) push!(ts_idxs, timeseries_parameter_index(sys, s).timeseries_idx) @@ -579,7 +579,7 @@ SymbolicIndexingInterface.constant_structure(::AbstractSystem) = true function SymbolicIndexingInterface.all_variable_symbols(sys::AbstractSystem) syms = variable_symbols(sys) - obs = getproperty.(observed(sys), :lhs) + obs = observables(sys) return isempty(obs) ? syms : vcat(syms, obs) end @@ -1411,6 +1411,7 @@ _nonum(@nospecialize x) = x isa Num ? x.val : x $(TYPEDSIGNATURES) Get the unknown variables of the system `sys` and its subsystems. +These must be explicitly solved for, unlike `observables(sys)`. See also [`ModelingToolkit.get_unknowns`](@ref). """ @@ -1677,6 +1678,14 @@ function controls(sys::AbstractSystem) isempty(systems) ? ctrls : [ctrls; reduce(vcat, namespace_controls.(systems))] end +""" +$(TYPEDSIGNATURES) + +Get the observed equations of the system `sys` and its subsystems. +These can be expressed in terms of `unknowns(sys)`, and do not have to be explicitly solved for. + +See also [`observables`](@ref) and [`ModelingToolkit.get_observed()`](@ref). +""" function observed(sys::AbstractSystem) obs = get_observed(sys) systems = get_systems(sys) @@ -1686,6 +1695,19 @@ function observed(sys::AbstractSystem) init = Equation[])] end +""" +$(TYPEDSIGNATURES) + +Get the observed variables of the system `sys` and its subsystems. +These can be expressed in terms of `unknowns(sys)`, and do not have to be explicitly solved for. +It is equivalent to all left hand sides of `observed(sys)`. + +See also [`observed`](@ref). +""" +function observables(sys::AbstractSystem) + return map(eq -> eq.lhs, observed(sys)) +end + Base.@deprecate default_u0(x) defaults(x) false Base.@deprecate default_p(x) defaults(x) false diff --git a/src/systems/diffeqs/abstractodesystem.jl b/src/systems/diffeqs/abstractodesystem.jl index 62ddd12a08..378105d962 100644 --- a/src/systems/diffeqs/abstractodesystem.jl +++ b/src/systems/diffeqs/abstractodesystem.jl @@ -1496,7 +1496,7 @@ function InitializationProblem{iip, specialize}(sys::AbstractSystem, @warn errmsg end - uninit = setdiff(unknowns(sys), [unknowns(isys); getfield.(observed(isys), :lhs)]) + uninit = setdiff(unknowns(sys), [unknowns(isys); observables(isys)]) # TODO: throw on uninitialized arrays filter!(x -> !(x isa Symbolics.Arr), uninit) diff --git a/src/systems/diffeqs/odesystem.jl b/src/systems/diffeqs/odesystem.jl index cbce569a9b..01b0ca5fbb 100644 --- a/src/systems/diffeqs/odesystem.jl +++ b/src/systems/diffeqs/odesystem.jl @@ -47,7 +47,7 @@ struct ODESystem <: AbstractODESystem var_to_name::Any """Control parameters (some subset of `ps`).""" ctrls::Vector - """Observed variables.""" + """Observed equations.""" observed::Vector{Equation} """System of constraints that must be satisfied by the solution to the system.""" constraintsystem::Union{Nothing, ConstraintsSystem} @@ -532,7 +532,7 @@ function build_explicit_observed_function(sys, ts; vs = ModelingToolkit.vars(ts; op) namespace_subs = Dict() - ns_map = Dict{Any, Any}(renamespace(sys, eq.lhs) => eq.lhs for eq in observed(sys)) + ns_map = Dict{Any, Any}(renamespace(sys, obs) => obs for obs in observables(sys)) for sym in unknowns(sys) ns_map[renamespace(sys, sym)] = sym if iscall(sym) && operation(sym) === getindex diff --git a/src/systems/diffeqs/sdesystem.jl b/src/systems/diffeqs/sdesystem.jl index 3fa1302630..c5299c28be 100644 --- a/src/systems/diffeqs/sdesystem.jl +++ b/src/systems/diffeqs/sdesystem.jl @@ -48,7 +48,7 @@ struct SDESystem <: AbstractODESystem var_to_name::Any """Control parameters (some subset of `ps`).""" ctrls::Vector - """Observed variables.""" + """Observed equations.""" observed::Vector{Equation} """ Time-derivative matrix. Note: this field will not be defined until diff --git a/src/systems/jumps/jumpsystem.jl b/src/systems/jumps/jumpsystem.jl index 57a3aee7df..06f5e1b623 100644 --- a/src/systems/jumps/jumpsystem.jl +++ b/src/systems/jumps/jumpsystem.jl @@ -59,7 +59,7 @@ struct JumpSystem{U <: ArrayPartition} <: AbstractTimeDependentSystem ps::Vector """Array variables.""" var_to_name::Any - """Observed variables.""" + """Observed equations.""" observed::Vector{Equation} """The name of the system.""" name::Symbol diff --git a/src/systems/nonlinear/nonlinearsystem.jl b/src/systems/nonlinear/nonlinearsystem.jl index 856822492b..1d44c5c42a 100644 --- a/src/systems/nonlinear/nonlinearsystem.jl +++ b/src/systems/nonlinear/nonlinearsystem.jl @@ -32,7 +32,7 @@ struct NonlinearSystem <: AbstractTimeIndependentSystem ps::Vector """Array variables.""" var_to_name::Any - """Observed variables.""" + """Observed equations.""" observed::Vector{Equation} """ Jacobian matrix. Note: this field will not be defined until diff --git a/src/systems/optimization/constraints_system.jl b/src/systems/optimization/constraints_system.jl index 0f69e6d0b9..ae8577662d 100644 --- a/src/systems/optimization/constraints_system.jl +++ b/src/systems/optimization/constraints_system.jl @@ -33,7 +33,7 @@ struct ConstraintsSystem <: AbstractTimeIndependentSystem ps::Vector """Array variables.""" var_to_name::Any - """Observed variables.""" + """Observed equations.""" observed::Vector{Equation} """ Jacobian matrix. Note: this field will not be defined until diff --git a/src/systems/optimization/optimizationsystem.jl b/src/systems/optimization/optimizationsystem.jl index be4567aee5..bfe15b62d7 100644 --- a/src/systems/optimization/optimizationsystem.jl +++ b/src/systems/optimization/optimizationsystem.jl @@ -31,7 +31,7 @@ struct OptimizationSystem <: AbstractOptimizationSystem ps::Vector """Array variables.""" var_to_name::Any - """Observed variables.""" + """Observed equations.""" observed::Vector{Equation} """List of constraint equations of the system.""" constraints::Vector{Union{Equation, Inequality}} diff --git a/src/systems/problem_utils.jl b/src/systems/problem_utils.jl index 0750585905..e2ad55da13 100644 --- a/src/systems/problem_utils.jl +++ b/src/systems/problem_utils.jl @@ -1016,7 +1016,7 @@ function get_u0_p(sys, u0map = Dict(u0map) end if u0map isa Dict - allobs = Set(getproperty.(observed(sys), :lhs)) + allobs = Set(observables(sys)) if any(in(allobs), keys(u0map)) u0s_in_obs = filter(in(allobs), keys(u0map)) @warn "Observed variables cannot be assigned initial values. Initial values for $u0s_in_obs will be ignored." diff --git a/src/systems/systemstructure.jl b/src/systems/systemstructure.jl index d27e5c93a1..73cdaba255 100644 --- a/src/systems/systemstructure.jl +++ b/src/systems/systemstructure.jl @@ -721,7 +721,7 @@ function _structural_simplify!(state::TearingState, io; simplify = false, sys = ModelingToolkit.tearing( sys, state; simplify, mm, check_consistency, kwargs...) end - fullunknowns = [map(eq -> eq.lhs, observed(sys)); unknowns(sys)] + fullunknowns = [observables(sys); unknowns(sys)] @set! sys.observed = ModelingToolkit.topsort_equations(observed(sys), fullunknowns) ModelingToolkit.invalidate_cache!(sys), input_idxs diff --git a/test/serialization.jl b/test/serialization.jl index e10de51299..feb3c7e4e7 100644 --- a/test/serialization.jl +++ b/test/serialization.jl @@ -31,7 +31,7 @@ sys = include_string(@__MODULE__, str) # check answer ss = structural_simplify(rc_model) -all_obs = [o.lhs for o in observed(ss)] +all_obs = observables(ss) prob = ODEProblem(ss, [capacitor.v => 0.0], (0, 0.1)) sol = solve(prob, ImplicitEuler()) diff --git a/test/structural_transformation/utils.jl b/test/structural_transformation/utils.jl index 6dfc107cc9..8894f0bbc9 100644 --- a/test/structural_transformation/utils.jl +++ b/test/structural_transformation/utils.jl @@ -51,8 +51,8 @@ end [D(x) ~ z[1] + z[2] + foo(z)[1], y[1] ~ 2t, y[2] ~ 3t, z ~ foo(y)], t) @test length(equations(sys)) == 1 @test length(observed(sys)) == 7 - @test any(eq -> isequal(eq.lhs, y), observed(sys)) - @test any(eq -> isequal(eq.lhs, z), observed(sys)) + @test any(obs -> isequal(obs, y), observables(sys)) + @test any(obs -> isequal(obs, z), observables(sys)) prob = ODEProblem(sys, [x => 1.0], (0.0, 1.0), [foo => _tmp_fn]) @test_nowarn prob.f(prob.u0, prob.p, 0.0) From 6feb8d79233c67332160f6d250b2d4aa30726664 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 14 Apr 2025 11:00:45 +0530 Subject: [PATCH 1349/2176] refactor: add `is_initializesystem` field to `NonlinearSystem` --- src/systems/abstractsystem.jl | 1 + src/systems/nonlinear/initializesystem.jl | 14 +++++--------- src/systems/nonlinear/nonlinearsystem.jl | 13 ++++++++++--- 3 files changed, 16 insertions(+), 12 deletions(-) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index 29338d0722..782d1b5229 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -915,6 +915,7 @@ for prop in [:eqs :substitutions :metadata :gui_metadata + :is_initializesystem :discrete_subsystems :parameter_dependencies :assertions diff --git a/src/systems/nonlinear/initializesystem.jl b/src/systems/nonlinear/initializesystem.jl index ec25b9b660..bff1454b2c 100644 --- a/src/systems/nonlinear/initializesystem.jl +++ b/src/systems/nonlinear/initializesystem.jl @@ -143,9 +143,7 @@ function generate_initializesystem(sys::AbstractTimeDependentSystem; for k in keys(defs) defs[k] = substitute(defs[k], paramsubs) end - meta = InitializationSystemMetadata( - anydict(u0map), anydict(pmap), additional_guesses, - additional_initialization_eqs, extra_metadata, nothing) + return NonlinearSystem(eqs_ics, vars, pars; @@ -153,7 +151,7 @@ function generate_initializesystem(sys::AbstractTimeDependentSystem; checks = check_units, parameter_dependencies = new_parameter_deps, name, - metadata = meta, + is_initializesystem = true, kwargs...) end @@ -244,9 +242,7 @@ function generate_initializesystem(sys::AbstractTimeIndependentSystem; for k in keys(defs) defs[k] = substitute(defs[k], paramsubs) end - meta = InitializationSystemMetadata( - anydict(u0map), anydict(pmap), additional_guesses, - additional_initialization_eqs, extra_metadata, nothing) + return NonlinearSystem(eqs_ics, vars, pars; @@ -254,7 +250,7 @@ function generate_initializesystem(sys::AbstractTimeIndependentSystem; checks = check_units, parameter_dependencies = new_parameter_deps, name, - metadata = meta, + is_initializesystem = true, kwargs...) end @@ -714,7 +710,7 @@ end Check if the given system is an initialization system. """ function is_initializesystem(sys::AbstractSystem) - sys isa NonlinearSystem && get_metadata(sys) isa InitializationSystemMetadata + has_is_initializesystem(sys) && get_is_initializesystem(sys) end """ diff --git a/src/systems/nonlinear/nonlinearsystem.jl b/src/systems/nonlinear/nonlinearsystem.jl index 856822492b..61fd4ecbf1 100644 --- a/src/systems/nonlinear/nonlinearsystem.jl +++ b/src/systems/nonlinear/nonlinearsystem.jl @@ -87,6 +87,10 @@ struct NonlinearSystem <: AbstractTimeIndependentSystem """ gui_metadata::Union{Nothing, GUIMetadata} """ + Whether this is an initialization system. + """ + is_initializesystem::Bool + """ Cache for intermediate tearing state. """ tearing_state::Any @@ -116,6 +120,7 @@ struct NonlinearSystem <: AbstractTimeIndependentSystem tag, eqs, unknowns, ps, var_to_name, observed, jac, name, description, systems, defaults, guesses, initializesystem, initialization_eqs, connector_type, parameter_dependencies = Equation[], metadata = nothing, gui_metadata = nothing, + is_initializesystem = false, tearing_state = nothing, substitutions = nothing, namespacing = true, complete = false, index_cache = nothing, parent = nothing, isscheduled = false; checks::Union{Bool, Int} = true) @@ -126,7 +131,8 @@ struct NonlinearSystem <: AbstractTimeIndependentSystem end new(tag, eqs, unknowns, ps, var_to_name, observed, jac, name, description, systems, defaults, guesses, initializesystem, initialization_eqs, - connector_type, parameter_dependencies, metadata, gui_metadata, tearing_state, + connector_type, parameter_dependencies, metadata, gui_metadata, + is_initializesystem, tearing_state, substitutions, namespacing, complete, index_cache, parent, isscheduled) end end @@ -148,7 +154,8 @@ function NonlinearSystem(eqs, unknowns, ps; checks = true, parameter_dependencies = Equation[], metadata = nothing, - gui_metadata = nothing) + gui_metadata = nothing, + is_initializesystem = false) continuous_events === nothing || isempty(continuous_events) || throw(ArgumentError("NonlinearSystem does not accept `continuous_events`, you provided $continuous_events")) discrete_events === nothing || isempty(discrete_events) || @@ -196,7 +203,7 @@ function NonlinearSystem(eqs, unknowns, ps; NonlinearSystem(Threads.atomic_add!(SYSTEM_COUNT, UInt(1)), eqs, dvs′, ps′, var_to_name, observed, jac, name, description, systems, defaults, guesses, initializesystem, initialization_eqs, connector_type, parameter_dependencies, - metadata, gui_metadata, checks = checks) + metadata, gui_metadata, is_initializesystem, checks = checks) end function NonlinearSystem(eqs; kwargs...) From 4a53fba56477efa065f1ade4fd5c0fcd619581cf Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 14 Apr 2025 13:53:32 +0530 Subject: [PATCH 1350/2176] refactor: don't store `InitializationSystemMetadata` in system metadata --- src/systems/diffeqs/abstractodesystem.jl | 10 +--- src/systems/nonlinear/initializesystem.jl | 62 +---------------------- 2 files changed, 4 insertions(+), 68 deletions(-) diff --git a/src/systems/diffeqs/abstractodesystem.jl b/src/systems/diffeqs/abstractodesystem.jl index 62ddd12a08..67b47a3964 100644 --- a/src/systems/diffeqs/abstractodesystem.jl +++ b/src/systems/diffeqs/abstractodesystem.jl @@ -1457,12 +1457,12 @@ function InitializationProblem{iip, specialize}(sys::AbstractSystem, elseif isempty(u0map) && get_initializesystem(sys) === nothing isys = generate_initializesystem( sys; initialization_eqs, check_units, pmap = parammap, - guesses, extra_metadata = (; use_scc), algebraic_only) + guesses, algebraic_only) simplify_system = true else isys = generate_initializesystem( sys; u0map, initialization_eqs, check_units, - pmap = parammap, guesses, extra_metadata = (; use_scc), algebraic_only) + pmap = parammap, guesses, algebraic_only) simplify_system = true end @@ -1477,12 +1477,6 @@ function InitializationProblem{iip, specialize}(sys::AbstractSystem, isys = structural_simplify(isys; fully_determined) end - meta = get_metadata(isys) - if meta isa InitializationSystemMetadata - @set! isys.metadata.oop_reconstruct_u0_p = ReconstructInitializeprob( - sys, isys) - end - ts = get_tearing_state(isys) unassigned_vars = StructuralTransformations.singular_check(ts) if warn_initialize_determined && !isempty(unassigned_vars) diff --git a/src/systems/nonlinear/initializesystem.jl b/src/systems/nonlinear/initializesystem.jl index bff1454b2c..d1a0c12a0d 100644 --- a/src/systems/nonlinear/initializesystem.jl +++ b/src/systems/nonlinear/initializesystem.jl @@ -11,7 +11,7 @@ function generate_initializesystem(sys::AbstractTimeDependentSystem; default_dd_guess = Bool(0), algebraic_only = false, check_units = true, check_defguess = false, - name = nameof(sys), extra_metadata = (;), kwargs...) + name = nameof(sys), kwargs...) eqs = equations(sys) if !(eqs isa Vector{Equation}) eqs = Equation[x for x in eqs if x isa Equation] @@ -167,7 +167,7 @@ function generate_initializesystem(sys::AbstractTimeIndependentSystem; guesses = Dict(), algebraic_only = false, check_units = true, check_defguess = false, - name = nameof(sys), extra_metadata = (;), kwargs...) + name = nameof(sys), kwargs...) eqs = equations(sys) trueobs, eqs = unhack_observed(observed(sys), eqs) vars = unique([unknowns(sys); getfield.(trueobs, :lhs)]) @@ -432,64 +432,6 @@ function _has_delays(sys::AbstractSystem, ex, banned) return any(x -> _has_delays(sys, x, banned), args) end -struct ReconstructInitializeprob - getter::Any - setter::Any -end - -function ReconstructInitializeprob( - srcsys::AbstractSystem, dstsys::AbstractSystem) - syms = reduce( - vcat, reorder_parameters(dstsys, parameters(dstsys)); - init = []) - getter = getu(srcsys, syms) - setter = setp_oop(dstsys, syms) - return ReconstructInitializeprob(getter, setter) -end - -function (rip::ReconstructInitializeprob)(srcvalp, dstvalp) - newp = rip.setter(dstvalp, rip.getter(srcvalp)) - if state_values(dstvalp) === nothing - return nothing, newp - end - srcu0 = state_values(srcvalp) - T = srcu0 === nothing || isempty(srcu0) ? Union{} : eltype(srcu0) - if parameter_values(dstvalp) isa MTKParameters - if !isempty(newp.tunable) - T = promote_type(eltype(newp.tunable), T) - end - elseif !isempty(newp) - T = promote_type(eltype(newp), T) - end - if T == eltype(state_values(dstvalp)) - u0 = state_values(dstvalp) - elseif T != Union{} - u0 = T.(state_values(dstvalp)) - end - buf, repack, alias = SciMLStructures.canonicalize(SciMLStructures.Tunable(), newp) - if eltype(buf) != T - newbuf = similar(buf, T) - copyto!(newbuf, buf) - newp = repack(newbuf) - end - buf, repack, alias = SciMLStructures.canonicalize(SciMLStructures.Initials(), newp) - if eltype(buf) != T - newbuf = similar(buf, T) - copyto!(newbuf, buf) - newp = repack(newbuf) - end - return u0, newp -end - -struct InitializationSystemMetadata - u0map::Dict{Any, Any} - pmap::Dict{Any, Any} - additional_guesses::Dict{Any, Any} - additional_initialization_eqs::Vector{Equation} - extra_metadata::NamedTuple - oop_reconstruct_u0_p::Union{Nothing, ReconstructInitializeprob} -end - function get_possibly_array_fallback_singletons(varmap, p) if haskey(varmap, p) return varmap[p] From 21325ca7e6a7d5e5b47c22003678a8df963f96b2 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 14 Apr 2025 13:54:05 +0530 Subject: [PATCH 1351/2176] feat: store `InitializationMetadata` in `OverrideInitData` --- src/systems/nonlinear/initializesystem.jl | 56 +++++++++++------------ src/systems/problem_utils.jl | 56 +++++++++++++++++++++-- 2 files changed, 77 insertions(+), 35 deletions(-) diff --git a/src/systems/nonlinear/initializesystem.jl b/src/systems/nonlinear/initializesystem.jl index d1a0c12a0d..f83ea6006f 100644 --- a/src/systems/nonlinear/initializesystem.jl +++ b/src/systems/nonlinear/initializesystem.jl @@ -481,22 +481,19 @@ function SciMLBase.remake_initialization_data( if u0 === missing && p === missing return odefn.initialization_data end + + oldinitdata = odefn.initialization_data + if !(eltype(u0) <: Pair) && !(eltype(p) <: Pair) - oldinitdata = odefn.initialization_data oldinitdata === nothing && return nothing oldinitprob = oldinitdata.initializeprob oldinitprob === nothing && return nothing - if !SciMLBase.has_sys(oldinitprob.f) || !(oldinitprob.f.sys isa NonlinearSystem) - return oldinitdata - end - oldinitsys = oldinitprob.f.sys - meta = get_metadata(oldinitsys) - if meta isa InitializationSystemMetadata && meta.oop_reconstruct_u0_p !== nothing - reconstruct_fn = meta.oop_reconstruct_u0_p - else - reconstruct_fn = ReconstructInitializeprob(sys, oldinitsys) - end + + meta = oldinitdata.metadata + meta isa InitializationMetadata || return oldinitdata + + reconstruct_fn = meta.oop_reconstruct_u0_p # the history function doesn't matter because `reconstruct_fn` is only going to # update the values of parameters, which aren't time dependent. The reason it # is called is because `Initial` parameters are calculated from the corresponding @@ -507,16 +504,15 @@ function SciMLBase.remake_initialization_data( if oldinitprob.f.resid_prototype === nothing newf = oldinitprob.f else - newf = NonlinearFunction{ - SciMLBase.isinplace(oldinitprob.f), SciMLBase.specialization(oldinitprob.f)}( - oldinitprob.f; + newf = remake(oldinitprob.f; resid_prototype = calculate_resid_prototype( length(oldinitprob.f.resid_prototype), new_initu0, new_initp)) end initprob = remake(oldinitprob; f = newf, u0 = new_initu0, p = new_initp) return SciMLBase.OverrideInitData(initprob, oldinitdata.update_initializeprob!, - oldinitdata.initializeprobmap, oldinitdata.initializeprobpmap) + oldinitdata.initializeprobmap, oldinitdata.initializeprobpmap; metadata = oldinitdata.metadata) end + dvs = unknowns(sys) ps = parameters(sys) u0map = to_varmap(u0, dvs) @@ -530,16 +526,13 @@ function SciMLBase.remake_initialization_data( use_scc = true initialization_eqs = Equation[] - if SciMLBase.has_initializeprob(odefn) - oldsys = odefn.initialization_data.initializeprob.f.sys - meta = get_metadata(oldsys) - if meta isa InitializationSystemMetadata - u0map = merge(meta.u0map, u0map) - pmap = merge(meta.pmap, pmap) - merge!(guesses, meta.additional_guesses) - use_scc = get(meta.extra_metadata, :use_scc, true) - initialization_eqs = meta.additional_initialization_eqs - end + if oldinitdata !== nothing && oldinitdata.metadata isa InitializationMetadata + meta = oldinitdata.metadata + u0map = merge(meta.u0map, u0map) + pmap = merge(meta.pmap, pmap) + merge!(guesses, meta.guesses) + use_scc = meta.use_scc + initialization_eqs = meta.additional_initialization_eqs else # there is no initializeprob, so the original problem construction # had no solvable parameters and had the differential variables @@ -600,8 +593,11 @@ function SciMLBase.late_binding_update_u0_p( if !(eltype(u0) <: Pair) # if `p` is not provided or is symbolic p === missing || eltype(p) <: Pair || return newu0, newp - newu0 === nothing && return newu0, newp - all(is_parameter(sys, Initial(x)) for x in unknowns(sys)) || return newu0, newp + (newu0 === nothing || isempty(newu0)) && return newu0, newp + initdata = prob.f.initialization_data + initdata === nothing && return newu0, newp + meta = initdata.metadata + meta isa InitializationMetadata || return newu0, newp newp = p === missing ? copy(newp) : newp initials, repack, alias = SciMLStructures.canonicalize( SciMLStructures.Initials(), newp) @@ -609,10 +605,10 @@ function SciMLBase.late_binding_update_u0_p( initials = DiffEqBase.promote_u0(initials, newu0, t0) newp = repack(initials) end - if length(newu0) != length(unknowns(sys)) - throw(ArgumentError("Expected `newu0` to be of same length as unknowns ($(length(unknowns(sys)))). Got $(typeof(newu0)) of length $(length(newu0))")) + if length(newu0) != length(prob.u0) + throw(ArgumentError("Expected `newu0` to be of same length as unknowns ($(length(prob.u0))). Got $(typeof(newu0)) of length $(length(newu0))")) end - setp(sys, Initial.(unknowns(sys)))(newp, newu0) + meta.set_initial_unknowns!(newp, newu0) return newu0, newp end diff --git a/src/systems/problem_utils.jl b/src/systems/problem_utils.jl index 0750585905..6f6a27fc14 100644 --- a/src/systems/problem_utils.jl +++ b/src/systems/problem_utils.jl @@ -620,6 +620,49 @@ function build_operating_point!(sys::AbstractSystem, return op, missing_unknowns, missing_pars end +""" + $(TYPEDEF) + +Metadata attached to `OverrideInitData` used in `remake` hooks for handling initialization +properly. + +# Fields + +$(TYPEDFIELDS) +""" +struct InitializationMetadata{R <: ReconstructInitializeprob, SIU} + """ + The `u0map` used to construct the initialization. + """ + u0map::Dict{Any, Any} + """ + The `pmap` used to construct the initialization. + """ + pmap::Dict{Any, Any} + """ + The `guesses` used to construct the initialization. + """ + guesses::Dict{Any, Any} + """ + The `initialization_eqs` in addition to those of the system that were used to construct + the initialization. + """ + additional_initialization_eqs::Vector{Equation} + """ + Whether to use `SCCNonlinearProblem` if possible. + """ + use_scc::Bool + """ + `ReconstructInitializeprob` for this initialization problem. + """ + oop_reconstruct_u0_p::R + """ + A function which takes the `u0` of the problem and sets + `Initial.(unknowns(sys))`. + """ + set_initial_unknowns!::SIU +end + """ $(TYPEDSIGNATURES) @@ -632,8 +675,8 @@ All other keyword arguments are forwarded to `InitializationProblem`. """ function maybe_build_initialization_problem( sys::AbstractSystem, op::AbstractDict, u0map, pmap, t, defs, - guesses, missing_unknowns; implicit_dae = false, - u0_constructor = identity, floatT = Float64, kwargs...) + guesses, missing_unknowns; implicit_dae = false, u0_constructor = identity, + floatT = Float64, initialization_eqs = [], use_scc = true, kwargs...) guesses = merge(ModelingToolkit.guesses(sys), todict(guesses)) if t === nothing && is_time_dependent(sys) @@ -641,7 +684,7 @@ function maybe_build_initialization_problem( end initializeprob = ModelingToolkit.InitializationProblem{true, SciMLBase.FullSpecialize}( - sys, t, u0map, pmap; guesses, kwargs...) + sys, t, u0map, pmap; guesses, initialization_eqs, use_scc, kwargs...) if state_values(initializeprob) !== nothing initializeprob = remake(initializeprob; u0 = floatT.(state_values(initializeprob))) end @@ -658,7 +701,10 @@ function maybe_build_initialization_problem( end initializeprob = remake(initializeprob; p = initp) - meta = get_metadata(initializeprob.f.sys) + meta = InitializationMetadata( + u0map, pmap, guesses, Equation[get_initialization_eqs(sys); initialization_eqs], + use_scc, ReconstructInitializeprob(sys, initializeprob.f.sys), + setp(sys, Initial.(unknowns(sys)))) if is_time_dependent(sys) all_init_syms = Set(all_symbols(initializeprob)) @@ -710,7 +756,7 @@ function maybe_build_initialization_problem( return (; initialization_data = SciMLBase.OverrideInitData( initializeprob, update_initializeprob!, initializeprobmap, - initializeprobpmap)) + initializeprobpmap; metadata = meta)) end """ From 934937b8ca4a096f5f0b5f5998feea82e009b3e1 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 14 Apr 2025 13:54:39 +0530 Subject: [PATCH 1352/2176] feat: add `flatten = false` kwarg to `reorder_parameters` --- src/systems/index_cache.jl | 41 +++++++++++++++++++++++++++----------- 1 file changed, 29 insertions(+), 12 deletions(-) diff --git a/src/systems/index_cache.jl b/src/systems/index_cache.jl index 47a784c00b..d0b687c212 100644 --- a/src/systems/index_cache.jl +++ b/src/systems/index_cache.jl @@ -502,7 +502,7 @@ function reorder_parameters( end end -function reorder_parameters(ic::IndexCache, ps; drop_missing = false) +function reorder_parameters(ic::IndexCache, ps; drop_missing = false, flatten = true) isempty(ps) && return () param_buf = if ic.tunable_buffer_size.length == 0 () @@ -555,20 +555,37 @@ function reorder_parameters(ic::IndexCache, ps; drop_missing = false) end end - result = broadcast.( - unwrap, ( - param_buf..., initials_buf..., disc_buf..., const_buf..., nonnumeric_buf...)) + param_buf = broadcast.(unwrap, param_buf) + initials_buf = broadcast.(unwrap, initials_buf) + disc_buf = broadcast.(unwrap, disc_buf) + const_buf = broadcast.(unwrap, const_buf) + nonnumeric_buf = broadcast.(unwrap, nonnumeric_buf) + if drop_missing - result = map(result) do buf - filter(buf) do sym - return !isequal(sym, unwrap(variable(:DEF))) - end + filterer = !isequal(unwrap(variable(:DEF))) + param_buf = filter.(filterer, param_buf) + initials_buf = filter.(filterer, initials_buf) + disc_buf = filter.(filterer, disc_buf) + const_buf = filter.(filterer, const_buf) + nonnumeric_buf = filter.(filterer, nonnumeric_buf) + end + + if flatten + result = ( + param_buf..., initials_buf..., disc_buf..., const_buf..., nonnumeric_buf...) + if all(isempty, result) + return () end + return result + else + if isempty(param_buf) + param_buf = ((),) + end + if isempty(initials_buf) + initials_buf = ((),) + end + return (param_buf..., initials_buf..., disc_buf, const_buf, nonnumeric_buf) end - if all(isempty, result) - return () - end - return result end # Given a parameter index, find the index of the buffer it is in when From 2d50f7e9e35e9a40dfcba3180a9dcf3004d98ed3 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 14 Apr 2025 13:55:44 +0530 Subject: [PATCH 1353/2176] fix: make `ReconstructInitializeprob` type-stable --- src/systems/problem_utils.jl | 132 +++++++++++++++++++++++++++++++++++ 1 file changed, 132 insertions(+) diff --git a/src/systems/problem_utils.jl b/src/systems/problem_utils.jl index 6f6a27fc14..edf6f8b22f 100644 --- a/src/systems/problem_utils.jl +++ b/src/systems/problem_utils.jl @@ -620,6 +620,138 @@ function build_operating_point!(sys::AbstractSystem, return op, missing_unknowns, missing_pars end +""" + $(TYPEDEF) + +A callable struct used to reconstruct the `u0` and `p` of the initialization problem +with promoted types. + +# Fields + +$(TYPEDFIELDS) +""" +struct ReconstructInitializeprob{G} + """ + A function which when called on the original problem returns the parameter object of + the initialization problem. + """ + getter::G +end + +""" + $(TYPEDSIGNATURES) + +Given an index provider `indp` and a vector of symbols `syms` return a type-stable getter +function by splitting `syms` into contiguous buffers where the getter of each buffer +is type-stable and constructing a function that calls and concatenates the results. +""" +function concrete_getu(indp, syms::AbstractVector) + # a list of contiguous buffer + split_syms = [Any[syms[1]]] + # the type of the getter of the last buffer + current = typeof(getu(indp, syms[1])) + for sym in syms[2:end] + getter = getu(indp, sym) + if typeof(getter) != current + # if types don't match, build a new buffer + push!(split_syms, []) + current = typeof(getter) + end + push!(split_syms[end], sym) + end + split_syms = Tuple(split_syms) + # the getter is now type-stable, and we can vcat it to get the full buffer + return Base.Fix1(reduce, vcat) ∘ getu(indp, split_syms) +end + +""" + $(TYPEDSIGNATURES) + +Construct a `ReconstructInitializeprob` which reconstructs the `u0` and `p` of `dstsys` +with values from `srcsys`. +""" +function ReconstructInitializeprob( + srcsys::AbstractSystem, dstsys::AbstractSystem) + @assert is_initializesystem(dstsys) + if is_split(dstsys) + # if we call `getu` on this (and it were able to handle empty tuples) we get the + # fields of `MTKParameters` except caches. + syms = reorder_parameters(dstsys, parameters(dstsys); flatten = false) + # `dstsys` is an initialization system, do basically everything is a tunable + # and tunables are a mix of different types in `srcsys`. No initials. Constants + # are going to be constants in `srcsys`, as are `nonnumeric`. + + # `syms[1]` is always the tunables because `srcsys` will have initials. + tunable_syms = syms[1] + tunable_getter = concrete_getu(srcsys, tunable_syms) + rest_getters = map(Base.tail(Base.tail(syms))) do buf + if buf == () + return Returns(()) + else + return getu(srcsys, buf) + end + end + getters = (tunable_getter, Returns(SizedVector{0, Float64}()), rest_getters...) + getter = let getters = getters + function _getter(valp) + MTKParameters(getters[1](valp), getters[2](valp), getters[3](valp), + getters[4](valp), getters[5](valp), ()) + end + end + else + syms = parameters(dstsys) + getter = concrete_getu(srcsys, syms) + end + return ReconstructInitializeprob(getter) +end + +""" + $(TYPEDSIGNATURES) + +Copy values from `srcvalp` to `dstvalp`. Returns the new `u0` and `p`. +""" +function (rip::ReconstructInitializeprob)(srcvalp, dstvalp) + # copy parameters + newp = rip.getter(srcvalp) + # no `u0`, so no type-promotion + if state_values(dstvalp) === nothing + return nothing, newp + end + # the `eltype` of the `u0` of the source + srcu0 = state_values(srcvalp) + T = srcu0 === nothing ? Union{} : eltype(srcu0) + # promote with the tunable eltype + if parameter_values(dstvalp) isa MTKParameters + if !isempty(newp.tunable) + T = promote_type(eltype(newp.tunable), T) + end + elseif !isempty(newp) + T = promote_type(eltype(newp), T) + end + # and the eltype of the destination u0 + if T == eltype(state_values(dstvalp)) + u0 = state_values(dstvalp) + elseif T != Union{} + u0 = T.(state_values(dstvalp)) + end + # apply the promotion to tunables portion + buf, repack, alias = SciMLStructures.canonicalize(SciMLStructures.Tunable(), newp) + if eltype(buf) != T + # only do a copy if the eltype doesn't match + newbuf = similar(buf, T) + copyto!(newbuf, buf) + newp = repack(newbuf) + end + # and initials portion + buf, repack, alias = SciMLStructures.canonicalize(SciMLStructures.Initials(), newp) + if eltype(buf) != T + newbuf = similar(buf, T) + copyto!(newbuf, buf) + newp = repack(newbuf) + end + return u0, newp +end + """ $(TYPEDEF) From 86fa00feceab706d63ed31cc6a6069327bd9295d Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 14 Apr 2025 20:29:48 +0530 Subject: [PATCH 1354/2176] test: test type-stability of `remake` --- test/initializationsystem.jl | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/test/initializationsystem.jl b/test/initializationsystem.jl index 4ade3481cb..31b157b3ec 100644 --- a/test/initializationsystem.jl +++ b/test/initializationsystem.jl @@ -1496,3 +1496,13 @@ end @test integ3.u ≈ [2.0, 3.0] @test integ3.ps[c1] ≈ 2.0 end + +# https://github.com/SciML/SciMLBase.jl/issues/985 +@testset "Type-stability of `remake`" begin + @parameters α=1 β=1 γ=1 δ=1 + @variables x(t)=1 y(t)=1 + eqs = [D(x) ~ α * x - β * x * y, D(y) ~ -δ * y + γ * x * y] + @named sys = ODESystem(eqs, t) + prob = ODEProblem(complete(sys), [], (0.0, 1)) + @inferred remake(prob; u0 = 2 .* prob.u0, p = prob.p) +end From 8ee30241d98cc417370fb66c21974ea0b3401cf4 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 14 Apr 2025 20:34:19 +0530 Subject: [PATCH 1355/2176] build: bump SciMLBase compat --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 30580d961b..23b0b4ef91 100644 --- a/Project.toml +++ b/Project.toml @@ -139,7 +139,7 @@ RecursiveArrayTools = "3.26" Reexport = "0.2, 1" RuntimeGeneratedFunctions = "0.5.9" SCCNonlinearSolve = "1.0.0" -SciMLBase = "2.75" +SciMLBase = "2.84" SciMLStructures = "1.7" Serialization = "1" Setfield = "0.7, 0.8, 1" From dc9bd04fcf7e811346f0b9276f0f61f78e9cc25b Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 15 Apr 2025 11:14:22 +0530 Subject: [PATCH 1356/2176] fix: retain caches in `ReconstructInitializeprob` --- src/systems/problem_utils.jl | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/src/systems/problem_utils.jl b/src/systems/problem_utils.jl index edf6f8b22f..e1864edd07 100644 --- a/src/systems/problem_utils.jl +++ b/src/systems/problem_utils.jl @@ -632,8 +632,9 @@ $(TYPEDFIELDS) """ struct ReconstructInitializeprob{G} """ - A function which when called on the original problem returns the parameter object of - the initialization problem. + A function which when given the original problem and initialization problem, returns + the parameter object of the initialization problem with values copied from the + original. """ getter::G end @@ -693,14 +694,18 @@ function ReconstructInitializeprob( end getters = (tunable_getter, Returns(SizedVector{0, Float64}()), rest_getters...) getter = let getters = getters - function _getter(valp) + function _getter(valp, initprob) MTKParameters(getters[1](valp), getters[2](valp), getters[3](valp), - getters[4](valp), getters[5](valp), ()) + getters[4](valp), getters[5](valp), copy.(parameter_values(initprob).caches)) end end else syms = parameters(dstsys) - getter = concrete_getu(srcsys, syms) + getter = let inner = concrete_getu(srcsys, syms) + function _getter2(valp, initprob) + inner(valp) + end + end end return ReconstructInitializeprob(getter) end @@ -712,7 +717,7 @@ Copy values from `srcvalp` to `dstvalp`. Returns the new `u0` and `p`. """ function (rip::ReconstructInitializeprob)(srcvalp, dstvalp) # copy parameters - newp = rip.getter(srcvalp) + newp = rip.getter(srcvalp, dstvalp) # no `u0`, so no type-promotion if state_values(dstvalp) === nothing return nothing, newp From 332fbf2016e42e749cc87db8367b095c9d3416c8 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 15 Apr 2025 11:16:09 +0530 Subject: [PATCH 1357/2176] build: bump SymbolicUtils compat --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 23b0b4ef91..5b5b2eeec8 100644 --- a/Project.toml +++ b/Project.toml @@ -150,7 +150,7 @@ StaticArrays = "0.10, 0.11, 0.12, 1.0" StochasticDelayDiffEq = "1.8.1" StochasticDiffEq = "6.72.1" SymbolicIndexingInterface = "0.3.39" -SymbolicUtils = "3.25.1" +SymbolicUtils = "3.26.1" Symbolics = "6.37" URIs = "1" UnPack = "0.1, 1.0" From d0c774c75a7f5767d8cd738b87fd43b4cb8ba3f8 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 15 Apr 2025 11:19:45 +0530 Subject: [PATCH 1358/2176] test: test `@inferred solve(prob)` --- test/initializationsystem.jl | 1 + 1 file changed, 1 insertion(+) diff --git a/test/initializationsystem.jl b/test/initializationsystem.jl index 31b157b3ec..dbc69da672 100644 --- a/test/initializationsystem.jl +++ b/test/initializationsystem.jl @@ -1505,4 +1505,5 @@ end @named sys = ODESystem(eqs, t) prob = ODEProblem(complete(sys), [], (0.0, 1)) @inferred remake(prob; u0 = 2 .* prob.u0, p = prob.p) + @inferred solve(prob) end From 44b56c51ed33ff59f03a61518f7c491787492d86 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 15 Apr 2025 12:57:15 +0530 Subject: [PATCH 1359/2176] fix: fix ADing through `ReconstructInitializeprob` --- src/systems/problem_utils.jl | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/systems/problem_utils.jl b/src/systems/problem_utils.jl index e1864edd07..b79b5bdd47 100644 --- a/src/systems/problem_utils.jl +++ b/src/systems/problem_utils.jl @@ -695,8 +695,10 @@ function ReconstructInitializeprob( getters = (tunable_getter, Returns(SizedVector{0, Float64}()), rest_getters...) getter = let getters = getters function _getter(valp, initprob) + oldcache = parameter_values(initprob).caches MTKParameters(getters[1](valp), getters[2](valp), getters[3](valp), - getters[4](valp), getters[5](valp), copy.(parameter_values(initprob).caches)) + getters[4](valp), getters[5](valp), oldcache isa Tuple{} ? () : + copy.(oldcache)) end end else From 849e5b5f354ce65560352fd2fedd1202e9f11506 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 23 Apr 2025 13:45:00 +0530 Subject: [PATCH 1360/2176] fix: do not duplicate `initialization_eqs` in `InitializationMetadata` --- 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 b79b5bdd47..81144213cc 100644 --- a/src/systems/problem_utils.jl +++ b/src/systems/problem_utils.jl @@ -841,7 +841,7 @@ function maybe_build_initialization_problem( initializeprob = remake(initializeprob; p = initp) meta = InitializationMetadata( - u0map, pmap, guesses, Equation[get_initialization_eqs(sys); initialization_eqs], + u0map, pmap, guesses, Vector{Equation}(initialization_eqs), use_scc, ReconstructInitializeprob(sys, initializeprob.f.sys), setp(sys, Initial.(unknowns(sys)))) From 819785a0cd610648f079b0366f78ab5261f90ac4 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 23 Apr 2025 16:38:15 +0530 Subject: [PATCH 1361/2176] docs: make `@autodocs` in `disturbance_modeling.md` non-canonical --- docs/src/tutorials/disturbance_modeling.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/src/tutorials/disturbance_modeling.md b/docs/src/tutorials/disturbance_modeling.md index b77a73b0c1..db8d926498 100644 --- a/docs/src/tutorials/disturbance_modeling.md +++ b/docs/src/tutorials/disturbance_modeling.md @@ -224,7 +224,7 @@ To see full examples that perform state estimation with ModelingToolkit models, Pages = ["disturbance_modeling.md"] ``` -```@autodocs +```@autodocs; canonical = false Modules = [ModelingToolkit] Pages = ["systems/analysis_points.jl"] Order = [:function, :type] From 0e7cad9a0c4fd16e1e601bef665ab590a8e45bfe Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 23 Apr 2025 16:38:31 +0530 Subject: [PATCH 1362/2176] docs: fix tolerances in `Events.md` --- docs/src/basics/Events.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/src/basics/Events.md b/docs/src/basics/Events.md index 23e1e6d7d1..3a76f478f1 100644 --- a/docs/src/basics/Events.md +++ b/docs/src/basics/Events.md @@ -126,8 +126,8 @@ prob = ODEProblem(ball, Pair[], tspan) sol = solve(prob, Tsit5()) @assert 0 <= minimum(sol[x]) <= 1e-10 # the ball never went through the floor but got very close -@assert minimum(sol[y]) > -1.5 # check wall conditions -@assert maximum(sol[y]) < 1.5 # check wall conditions +@assert minimum(sol[y]) >= -1.5 # check wall conditions +@assert maximum(sol[y]) <= 1.5 # check wall conditions tv = sort([LinRange(0, 10, 200); sol.t]) plot(sol(tv)[y], sol(tv)[x], line_z = tv) From 18855ab059934e859d72b165674ef2fef92c75cc Mon Sep 17 00:00:00 2001 From: DhairyaLGandhi Date: Wed, 23 Apr 2025 17:28:09 +0530 Subject: [PATCH 1363/2176] chore: copy MTKparameter fields only if not empty --- src/systems/parameter_buffer.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/systems/parameter_buffer.jl b/src/systems/parameter_buffer.jl index 4d33aa0a06..6142c95776 100644 --- a/src/systems/parameter_buffer.jl +++ b/src/systems/parameter_buffer.jl @@ -319,8 +319,8 @@ function Base.copy(p::MTKParameters) initials = copy(p.initials) discrete = Tuple(eltype(buf) <: Real ? copy(buf) : copy.(buf) for buf in p.discrete) constant = Tuple(eltype(buf) <: Real ? copy(buf) : copy.(buf) for buf in p.constant) - nonnumeric = copy.(p.nonnumeric) - caches = copy.(p.caches) + nonnumeric = isempty(p.nonnumeric) ? p.nonnumeric : copy.(p.nonnumeric) + caches = isempty(p.caches) ? p.caches : copy.(p.caches) return MTKParameters( tunable, initials, From e8b1d8ee0e5dedd410f149e155c9654ff4941b3c Mon Sep 17 00:00:00 2001 From: DhairyaLGandhi Date: Wed, 23 Apr 2025 17:46:19 +0530 Subject: [PATCH 1364/2176] test: add test to check field equality --- test/mtkparameters.jl | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/test/mtkparameters.jl b/test/mtkparameters.jl index 22201b1988..55de0768e0 100644 --- a/test/mtkparameters.jl +++ b/test/mtkparameters.jl @@ -18,6 +18,11 @@ ivs = Dict(c => 3a, d => 4, e => [5.0, 6.0, 7.0], ps = MTKParameters(sys, ivs) @test_nowarn copy(ps) +ps_copy = copy(ps) +ps_field_equals = map(fieldnames(typeof(ps))) do f + getfield(ps, f) == getfield(ps_copy, f) +end +@test all(ps_field_equals) # dependent initialization, also using defaults @test getp(sys, a)(ps) == getp(sys, b)(ps) == getp(sys, c)(ps) == 0.0 @test getp(sys, d)(ps) isa Int From 6d75b65c6d594c921d5b85fae7e9c272e6689b23 Mon Sep 17 00:00:00 2001 From: Christopher Rackauckas Date: Wed, 23 Apr 2025 11:15:18 -0400 Subject: [PATCH 1365/2176] Update Project.toml --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 5b5b2eeec8..3d52a4604f 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "ModelingToolkit" uuid = "961ee093-0014-501f-94e3-6117800e7a78" authors = ["Yingbo Ma ", "Chris Rackauckas and contributors"] -version = "9.73.0" +version = "9.74.0" [deps] ADTypes = "47edcb42-4c32-4615-8424-f2b9edc5f35b" From 44bb9d563e1373d9df75a89541f5c53ff9430622 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 24 Apr 2025 15:43:37 +0530 Subject: [PATCH 1366/2176] fix: fix `remake(::JumpProblem)` --- src/systems/nonlinear/initializesystem.jl | 4 ++++ test/jumpsystem.jl | 15 +++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/src/systems/nonlinear/initializesystem.jl b/src/systems/nonlinear/initializesystem.jl index f83ea6006f..50c0862863 100644 --- a/src/systems/nonlinear/initializesystem.jl +++ b/src/systems/nonlinear/initializesystem.jl @@ -484,6 +484,10 @@ function SciMLBase.remake_initialization_data( oldinitdata = odefn.initialization_data + # We _always_ build initialization now. So if we didn't build it before, don't do + # it now + oldinitdata === nothing && return nothing + if !(eltype(u0) <: Pair) && !(eltype(p) <: Pair) oldinitdata === nothing && return nothing diff --git a/test/jumpsystem.jl b/test/jumpsystem.jl index 9568990e73..1ee0408758 100644 --- a/test/jumpsystem.jl +++ b/test/jumpsystem.jl @@ -551,3 +551,18 @@ end sol = solve(jprob, SSAStepper()) @test eltype(sol[X]) === Int64 end + +@testset "Issue#3571: `remake(::JumpProblem)`" begin + @variables X(t) + @parameters a b + eq = D(X) ~ a + rate = b * X + affect = [X ~ X - 1] + crj = ConstantRateJump(rate, affect) + @named jsys = JumpSystem([crj, eq], t, [X], [a, b]) + jsys = complete(jsys) + oprob = ODEProblem(jsys, [:X => 1.0], (0.0, 10.0), [:a => 1.0, :b => 0.5]) + jprob = JumpProblem(jsys, oprob) + jprob2 = remake(jprob; u0 = [:X => 10.0]) + @test jprob2[X] ≈ 10.0 +end From d4d23a380f60c00227973ce9fb0714390c2f8e94 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 24 Mar 2025 13:21:28 +0530 Subject: [PATCH 1367/2176] fix: handle derivatives of time-dependent parameters --- src/structural_transformation/pantelides.jl | 1 + .../symbolics_tearing.jl | 4 ++- src/systems/systemstructure.jl | 8 ++++- test/structural_transformation/utils.jl | 30 +++++++++++++++++++ 4 files changed, 41 insertions(+), 2 deletions(-) diff --git a/src/structural_transformation/pantelides.jl b/src/structural_transformation/pantelides.jl index b6877d65f8..585c4a29d1 100644 --- a/src/structural_transformation/pantelides.jl +++ b/src/structural_transformation/pantelides.jl @@ -54,6 +54,7 @@ function pantelides_reassemble(state::TearingState, var_eq_matching) D(eq.lhs) end rhs = ModelingToolkit.expand_derivatives(D(eq.rhs)) + rhs = fast_substitute(rhs, state.param_derivative_map) substitution_dict = Dict(x.lhs => x.rhs for x in out_eqs if x !== nothing && x.lhs isa Symbolic) sub_rhs = substitute(rhs, substitution_dict) diff --git a/src/structural_transformation/symbolics_tearing.jl b/src/structural_transformation/symbolics_tearing.jl index 552c6d13c3..31307d132f 100644 --- a/src/structural_transformation/symbolics_tearing.jl +++ b/src/structural_transformation/symbolics_tearing.jl @@ -65,7 +65,9 @@ function eq_derivative!(ts::TearingState{ODESystem}, ieq::Int; kwargs...) sys = ts.sys eq = equations(ts)[ieq] - eq = 0 ~ Symbolics.derivative(eq.rhs - eq.lhs, get_iv(sys); throw_no_derivative = true) + eq = 0 ~ fast_substitute( + ModelingToolkit.derivative( + eq.rhs - eq.lhs, get_iv(sys); throw_no_derivative = true), ts.param_derivative_map) push!(equations(ts), eq) # Analyze the new equation and update the graph/solvable_graph # First, copy the previous incidence and add the derivative terms. diff --git a/src/systems/systemstructure.jl b/src/systems/systemstructure.jl index 73cdaba255..d31a45a653 100644 --- a/src/systems/systemstructure.jl +++ b/src/systems/systemstructure.jl @@ -207,6 +207,7 @@ mutable struct TearingState{T <: AbstractSystem} <: AbstractTearingState{T} fullvars::Vector structure::SystemStructure extra_eqs::Vector + param_derivative_map::Dict{BasicSymbolic, Real} end TransformationState(sys::AbstractSystem) = TearingState(sys) @@ -264,6 +265,7 @@ function TearingState(sys; quick_cancel = false, check = true) var2idx = Dict{Any, Int}() symbolic_incidence = [] fullvars = [] + param_derivative_map = Dict{BasicSymbolic, Real}() var_counter = Ref(0) var_types = VariableType[] addvar! = let fullvars = fullvars, var_counter = var_counter, var_types = var_types @@ -295,6 +297,10 @@ function TearingState(sys; quick_cancel = false, check = true) any(isequal(_var), ivs) && continue if isparameter(_var) || (iscall(_var) && isparameter(operation(_var)) || isconstant(_var)) + if iv !== nothing && isparameter(_var) && iscall(_var) && + (args = arguments(_var); length(args)) == 1 && isequal(only(args), iv) + param_derivative_map[Differential(iv)(_var)] = 0.0 + end continue end v = scalarize(v) @@ -438,7 +444,7 @@ function TearingState(sys; quick_cancel = false, check = true) ts = TearingState(sys, fullvars, SystemStructure(complete(var_to_diff), complete(eq_to_diff), complete(graph), nothing, var_types, sys isa AbstractDiscreteSystem), - Any[]) + Any[], param_derivative_map) if sys isa DiscreteSystem ts = shift_discrete_system(ts) end diff --git a/test/structural_transformation/utils.jl b/test/structural_transformation/utils.jl index 8894f0bbc9..aad43f9616 100644 --- a/test/structural_transformation/utils.jl +++ b/test/structural_transformation/utils.jl @@ -282,3 +282,33 @@ end @test length(mapping) == 3 end end + +@testset "Issue#3480: Derivatives of time-dependent parameters" begin + @component function FilteredInput(; name, x0 = 0, T = 0.1) + params = @parameters begin + k(t) = x0 + T = T + end + vars = @variables begin + x(t) = k + dx(t) = 0 + ddx(t) + end + systems = [] + eqs = [D(x) ~ dx + D(dx) ~ ddx + dx ~ (k - x) / T] + return ODESystem(eqs, t, vars, params; systems, name) + end + + @mtkbuild sys = FilteredInput() + vs = Set() + for eq in equations(sys) + ModelingToolkit.vars!(vs, eq) + end + for eq in observed(sys) + ModelingToolkit.vars!(vs, eq) + end + + @test !(D(sys.k) in vs) +end From b7dbe5138d7e1fed2bc42ae8e4ba2019fc1ecec4 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 24 Mar 2025 13:39:03 +0530 Subject: [PATCH 1368/2176] test: test called parameters are still differentiated --- test/structural_transformation/utils.jl | 32 +++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/test/structural_transformation/utils.jl b/test/structural_transformation/utils.jl index aad43f9616..2565cb9626 100644 --- a/test/structural_transformation/utils.jl +++ b/test/structural_transformation/utils.jl @@ -311,4 +311,36 @@ end end @test !(D(sys.k) in vs) + + @testset "Called parameter still has derivative" begin + @component function FilteredInput2(; name, x0 = 0, T = 0.1) + ts = collect(0.0:0.1:10.0) + spline = LinearInterpolation(ts .^ 2, ts) + params = @parameters begin + (k::LinearInterpolation)(..) = spline + T = T + end + vars = @variables begin + x(t) = k(t) + dx(t) = 0 + ddx(t) + end + systems = [] + eqs = [D(x) ~ dx + D(dx) ~ ddx + dx ~ (k(t) - x) / T] + return ODESystem(eqs, t, vars, params; systems, name) + end + + @mtkbuild sys = FilteredInput2() + vs = Set() + for eq in equations(sys) + ModelingToolkit.vars!(vs, eq) + end + for eq in observed(sys) + ModelingToolkit.vars!(vs, eq) + end + + @test D(sys.k(t)) in vs + end end From fdf3cb10a106506f6120f6e345765ac01bb19927 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 24 Mar 2025 15:21:45 +0530 Subject: [PATCH 1369/2176] test: import `DataInterpolations` in test --- test/structural_transformation/utils.jl | 1 + 1 file changed, 1 insertion(+) diff --git a/test/structural_transformation/utils.jl b/test/structural_transformation/utils.jl index 2565cb9626..7c0cb633c7 100644 --- a/test/structural_transformation/utils.jl +++ b/test/structural_transformation/utils.jl @@ -5,6 +5,7 @@ using SparseArrays using UnPack using ModelingToolkit: t_nounits as t, D_nounits as D, default_toterm using Symbolics: unwrap +using DataInterpolations const ST = StructuralTransformations # Define some variables From f1c9c164b9eb81847a8f2cf3672a554869215ac0 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 22 Apr 2025 13:49:24 +0530 Subject: [PATCH 1370/2176] fix: require explicitly specifying discrete variable derivatives --- .../symbolics_tearing.jl | 14 ++++++++++ src/systems/systemstructure.jl | 26 +++++++++++++++---- test/structural_transformation/utils.jl | 23 +++++++++++++++- 3 files changed, 57 insertions(+), 6 deletions(-) diff --git a/src/structural_transformation/symbolics_tearing.jl b/src/structural_transformation/symbolics_tearing.jl index 31307d132f..548c7da519 100644 --- a/src/structural_transformation/symbolics_tearing.jl +++ b/src/structural_transformation/symbolics_tearing.jl @@ -68,6 +68,20 @@ function eq_derivative!(ts::TearingState{ODESystem}, ieq::Int; kwargs...) eq = 0 ~ fast_substitute( ModelingToolkit.derivative( eq.rhs - eq.lhs, get_iv(sys); throw_no_derivative = true), ts.param_derivative_map) + + vs = ModelingToolkit.vars(eq.rhs) + for v in vs + # parameters with unknown derivatives have a value of `nothing` in the map, + # so use `missing` as the default. + get(ts.param_derivative_map, v, missing) === nothing || continue + _original_eq = equations(ts)[ieq] + error(""" + Encountered derivative of discrete variable `$(only(arguments(v)))` when \ + differentiating equation `$(_original_eq)`. This may indicate a model error or a \ + missing equation of the form `$v ~ ...` that defines this derivative. + """) + end + push!(equations(ts), eq) # Analyze the new equation and update the graph/solvable_graph # First, copy the previous incidence and add the derivative terms. diff --git a/src/systems/systemstructure.jl b/src/systems/systemstructure.jl index d31a45a653..61ae06aab3 100644 --- a/src/systems/systemstructure.jl +++ b/src/systems/systemstructure.jl @@ -207,7 +207,7 @@ mutable struct TearingState{T <: AbstractSystem} <: AbstractTearingState{T} fullvars::Vector structure::SystemStructure extra_eqs::Vector - param_derivative_map::Dict{BasicSymbolic, Real} + param_derivative_map::Dict{BasicSymbolic, Any} end TransformationState(sys::AbstractSystem) = TearingState(sys) @@ -254,6 +254,11 @@ function Base.push!(ev::EquationsView, eq) push!(ev.ts.extra_eqs, eq) end +function is_time_dependent_parameter(p, iv) + return iv !== nothing && isparameter(p) && iscall(p) && + (args = arguments(p); length(args)) == 1 && isequal(only(args), iv) +end + function TearingState(sys; quick_cancel = false, check = true) sys = flatten(sys) ivs = independent_variables(sys) @@ -265,7 +270,7 @@ function TearingState(sys; quick_cancel = false, check = true) var2idx = Dict{Any, Int}() symbolic_incidence = [] fullvars = [] - param_derivative_map = Dict{BasicSymbolic, Real}() + param_derivative_map = Dict{BasicSymbolic, Any}() var_counter = Ref(0) var_types = VariableType[] addvar! = let fullvars = fullvars, var_counter = var_counter, var_types = var_types @@ -278,11 +283,17 @@ function TearingState(sys; quick_cancel = false, check = true) vars = OrderedSet() varsvec = [] + eqs_to_retain = trues(length(eqs)) for (i, eq′) in enumerate(eqs) if eq′.lhs isa Connection check ? error("$(nameof(sys)) has unexpanded `connect` statements") : return nothing end + if iscall(eq′.lhs) && (op = operation(eq′.lhs)) isa Differential && + isequal(op.x, iv) && is_time_dependent_parameter(only(arguments(eq′.lhs)), iv) + param_derivative_map[eq′.lhs] = eq′.rhs + eqs_to_retain[i] = false + end if _iszero(eq′.lhs) rhs = quick_cancel ? quick_cancel_expr(eq′.rhs) : eq′.rhs eq = eq′ @@ -297,9 +308,11 @@ function TearingState(sys; quick_cancel = false, check = true) any(isequal(_var), ivs) && continue if isparameter(_var) || (iscall(_var) && isparameter(operation(_var)) || isconstant(_var)) - if iv !== nothing && isparameter(_var) && iscall(_var) && - (args = arguments(_var); length(args)) == 1 && isequal(only(args), iv) - param_derivative_map[Differential(iv)(_var)] = 0.0 + if is_time_dependent_parameter(_var, iv) && + !haskey(param_derivative_map, Differential(iv)(_var)) + # default to `nothing` since it is ignored during substitution, + # so `D(_var)` is retained in the expression. + param_derivative_map[Differential(iv)(_var)] = nothing end continue end @@ -357,6 +370,9 @@ function TearingState(sys; quick_cancel = false, check = true) eqs[i] = eqs[i].lhs ~ rhs end end + eqs = eqs[eqs_to_retain] + neqs = length(eqs) + symbolic_incidence = symbolic_incidence[eqs_to_retain] ### Handle discrete variables lowest_shift = Dict() diff --git a/test/structural_transformation/utils.jl b/test/structural_transformation/utils.jl index 7c0cb633c7..996f2e7b83 100644 --- a/test/structural_transformation/utils.jl +++ b/test/structural_transformation/utils.jl @@ -302,7 +302,28 @@ end return ODESystem(eqs, t, vars, params; systems, name) end - @mtkbuild sys = FilteredInput() + @component function FilteredInputFix(; name, x0 = 0, T = 0.1) + params = @parameters begin + k(t) = x0 + T = T + end + vars = @variables begin + x(t) = k + dx(t) = 0 + ddx(t) + end + systems = [] + eqs = [D(x) ~ dx + D(dx) ~ ddx + dx ~ (k - x) / T + D(k) ~ 0] + return ODESystem(eqs, t, vars, params; systems, name) + end + + @named sys = FilteredInput() + @test_throws ["derivative of discrete variable", "k(t)"] structural_simplify(sys) + + @mtkbuild sys = FilteredInputFix() vs = Set() for eq in equations(sys) ModelingToolkit.vars!(vs, eq) From 8e3f6e379775d4e10173f17795117c7b36190e6c Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 22 Apr 2025 18:13:46 +0530 Subject: [PATCH 1371/2176] test: fix state selection test --- test/state_selection.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/state_selection.jl b/test/state_selection.jl index b8404d1f26..a8d3e57773 100644 --- a/test/state_selection.jl +++ b/test/state_selection.jl @@ -2,7 +2,7 @@ using ModelingToolkit, OrdinaryDiffEq, Test using ModelingToolkit: t_nounits as t, D_nounits as D sts = @variables x1(t) x2(t) x3(t) x4(t) -params = @parameters u1(t) u2(t) u3(t) u4(t) +params = @parameters u1 u2 u3 u4 eqs = [x1 + x2 + u1 ~ 0 x1 + x2 + x3 + u2 ~ 0 x1 + D(x3) + x4 + u3 ~ 0 From 4935f3b32def60b03338ae9baed4967d1e3245ca Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 24 Apr 2025 17:59:59 +0530 Subject: [PATCH 1372/2176] fix: handle derivatives of time-dependent array parameters --- src/systems/systemstructure.jl | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/systems/systemstructure.jl b/src/systems/systemstructure.jl index 61ae06aab3..8ec731db5e 100644 --- a/src/systems/systemstructure.jl +++ b/src/systems/systemstructure.jl @@ -256,7 +256,8 @@ end function is_time_dependent_parameter(p, iv) return iv !== nothing && isparameter(p) && iscall(p) && - (args = arguments(p); length(args)) == 1 && isequal(only(args), iv) + (operation(p) === getindex && is_time_dependent_parameter(arguments(p)[1], iv) || + (args = arguments(p); length(args)) == 1 && isequal(only(args), iv)) end function TearingState(sys; quick_cancel = false, check = true) From 4d8463cbeb9134bcb49a95d8784301ae33be8a40 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 24 Apr 2025 18:00:23 +0530 Subject: [PATCH 1373/2176] refactor: default parameter derivatives to zero, allow opting-out --- src/systems/systemstructure.jl | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/systems/systemstructure.jl b/src/systems/systemstructure.jl index 8ec731db5e..5927d3ebad 100644 --- a/src/systems/systemstructure.jl +++ b/src/systems/systemstructure.jl @@ -292,8 +292,14 @@ function TearingState(sys; quick_cancel = false, check = true) end if iscall(eq′.lhs) && (op = operation(eq′.lhs)) isa Differential && isequal(op.x, iv) && is_time_dependent_parameter(only(arguments(eq′.lhs)), iv) - param_derivative_map[eq′.lhs] = eq′.rhs + # parameter derivatives are opted out by specifying `D(p) ~ missing`, but + # we want to store `nothing` in the map because that means `fast_substitute` + # will ignore the rule. We will this identify the presence of `eq′.lhs` in + # the differentiated expression and error. + param_derivative_map[eq′.lhs] = coalesce(eq′.rhs, nothing) eqs_to_retain[i] = false + # change the equation if the RHS is `missing` so the rest of this loop works + eq′ = eq′.lhs ~ coalesce(eq′.rhs, 0.0) end if _iszero(eq′.lhs) rhs = quick_cancel ? quick_cancel_expr(eq′.rhs) : eq′.rhs @@ -311,9 +317,9 @@ function TearingState(sys; quick_cancel = false, check = true) (iscall(_var) && isparameter(operation(_var)) || isconstant(_var)) if is_time_dependent_parameter(_var, iv) && !haskey(param_derivative_map, Differential(iv)(_var)) - # default to `nothing` since it is ignored during substitution, - # so `D(_var)` is retained in the expression. - param_derivative_map[Differential(iv)(_var)] = nothing + # Parameter derivatives default to zero - they stay constant + # between callbacks + param_derivative_map[Differential(iv)(_var)] = 0.0 end continue end From a2b47451946b5e64c379f9463173654ae632cd7b Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 24 Apr 2025 18:00:30 +0530 Subject: [PATCH 1374/2176] test: test new parameter derivative behavior --- test/structural_transformation/utils.jl | 33 ++++++++++++++++++++++--- 1 file changed, 29 insertions(+), 4 deletions(-) diff --git a/test/structural_transformation/utils.jl b/test/structural_transformation/utils.jl index 996f2e7b83..6c5a564e9f 100644 --- a/test/structural_transformation/utils.jl +++ b/test/structural_transformation/utils.jl @@ -302,7 +302,25 @@ end return ODESystem(eqs, t, vars, params; systems, name) end - @component function FilteredInputFix(; name, x0 = 0, T = 0.1) + @component function FilteredInputExplicit(; name, x0 = 0, T = 0.1) + params = @parameters begin + k(t)[1:1] = [x0] + T = T + end + vars = @variables begin + x(t) = k + dx(t) = 0 + ddx(t) + end + systems = [] + eqs = [D(x) ~ dx + D(dx) ~ ddx + D(k[1]) ~ 1.0 + dx ~ (k[1] - x) / T] + return ODESystem(eqs, t, vars, params; systems, name) + end + + @component function FilteredInputErr(; name, x0 = 0, T = 0.1) params = @parameters begin k(t) = x0 T = T @@ -316,14 +334,14 @@ end eqs = [D(x) ~ dx D(dx) ~ ddx dx ~ (k - x) / T - D(k) ~ 0] + D(k) ~ missing] return ODESystem(eqs, t, vars, params; systems, name) end - @named sys = FilteredInput() + @named sys = FilteredInputErr() @test_throws ["derivative of discrete variable", "k(t)"] structural_simplify(sys) - @mtkbuild sys = FilteredInputFix() + @mtkbuild sys = FilteredInput() vs = Set() for eq in equations(sys) ModelingToolkit.vars!(vs, eq) @@ -334,6 +352,13 @@ end @test !(D(sys.k) in vs) + @mtkbuild sys = FilteredInputExplicit() + obsfn1 = ModelingToolkit.build_explicit_observed_function(sys, sys.ddx) + obsfn2 = ModelingToolkit.build_explicit_observed_function(sys, sys.dx) + u = [1.0] + p = MTKParameters(sys, [sys.k => [2.0], sys.T => 3.0]) + @test obsfn1(u, p, 0.0) ≈ (1 - obsfn2(u, p, 0.0)) / 3.0 + @testset "Called parameter still has derivative" begin @component function FilteredInput2(; name, x0 = 0, T = 0.1) ts = collect(0.0:0.1:10.0) From 21dd5293b11254c6bef24cfa70a8d2db887662e8 Mon Sep 17 00:00:00 2001 From: Christopher Rackauckas Date: Fri, 25 Apr 2025 06:45:42 -0400 Subject: [PATCH 1375/2176] Update Project.toml --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 916ffaa824..35e03e2924 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "ModelingToolkit" uuid = "961ee093-0014-501f-94e3-6117800e7a78" authors = ["Yingbo Ma ", "Chris Rackauckas and contributors"] -version = "9.74.0" +version = "9.75.0" [deps] ADTypes = "47edcb42-4c32-4615-8424-f2b9edc5f35b" From 3c85f50a5055b6ca3f75281e2eb93d7cd25454be Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 8 Apr 2025 18:15:32 +0530 Subject: [PATCH 1376/2176] feat: lexicographically sort equations in `structural_simplify` --- src/systems/systemstructure.jl | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/systems/systemstructure.jl b/src/systems/systemstructure.jl index 5927d3ebad..83382cb1d2 100644 --- a/src/systems/systemstructure.jl +++ b/src/systems/systemstructure.jl @@ -381,6 +381,12 @@ function TearingState(sys; quick_cancel = false, check = true) neqs = length(eqs) symbolic_incidence = symbolic_incidence[eqs_to_retain] + # sort equations lexicographically to reduce simplification issues + # depending on order due to NP-completeness of tearing. + sortidxs = Base.sortperm(eqs, by = string) + eqs = eqs[sortidxs] + symbolic_incidence = symbolic_incidence[sortidxs] + ### Handle discrete variables lowest_shift = Dict() for var in fullvars From 9666196ea492d3c03b4fce29f4fb0aa57e50bbab Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 22 Apr 2025 17:28:51 +0530 Subject: [PATCH 1377/2176] test: fix initialization in `lowering_solving` test --- test/lowering_solving.jl | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/test/lowering_solving.jl b/test/lowering_solving.jl index bfaeee60cf..300d505ab0 100644 --- a/test/lowering_solving.jl +++ b/test/lowering_solving.jl @@ -59,8 +59,7 @@ u0 = [lorenz1.x => 1.0, lorenz1.z => 0.0, lorenz2.x => 0.0, lorenz2.y => 1.0, - lorenz2.z => 0.0, - α => 2.0] + lorenz2.z => 0.0] p = [lorenz1.σ => 10.0, lorenz1.ρ => 28.0, @@ -73,5 +72,5 @@ p = [lorenz1.σ => 10.0, tspan = (0.0, 100.0) prob = ODEProblem(connected, u0, tspan, p) sol = solve(prob, Rodas5()) -@test maximum(sol[2, :] + sol[6, :] + 2sol[1, :]) < 1e-12 +@test maximum(sol[lorenz1.x] + sol[lorenz2.y] + 2sol[α]) < 1e-12 #using Plots; plot(sol,idxs=(:α,Symbol(lorenz1.x),Symbol(lorenz2.y))) From cb80b6a3e42df5b9f59f1c9c6c002ee66ff98998 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 22 Apr 2025 17:30:19 +0530 Subject: [PATCH 1378/2176] test: fix symbolic events test --- test/symbolic_events.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/symbolic_events.jl b/test/symbolic_events.jl index 804432408b..9099d32d14 100644 --- a/test/symbolic_events.jl +++ b/test/symbolic_events.jl @@ -829,8 +829,8 @@ let sol = solve(prob, Tsit5(), saveat = 0.1) @test typeof(oneosc_ce_simpl) == ODESystem - @test sol[1, 6] < 1.0 # test whether x(t) decreases over time - @test sol[1, 18] > 0.5 # test whether event happened + @test sol[oscce.x, 6] < 1.0 # test whether x(t) decreases over time + @test sol[oscce.x, 18] > 0.5 # test whether event happened end @testset "Additional SymbolicContinuousCallback options" begin From 64269a5137a5f3608c48dcf661e4cf0debc3eb2a Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 22 Apr 2025 17:35:21 +0530 Subject: [PATCH 1379/2176] test: fix index reduction test --- test/structural_transformation/index_reduction.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/structural_transformation/index_reduction.jl b/test/structural_transformation/index_reduction.jl index d7f19e1fa2..f9d6037022 100644 --- a/test/structural_transformation/index_reduction.jl +++ b/test/structural_transformation/index_reduction.jl @@ -37,7 +37,7 @@ state = TearingState(pendulum) @test StructuralTransformations.maximal_matching(graph, eq -> true, v -> var_to_diff[v] === nothing) == map(x -> x == 0 ? StructuralTransformations.unassigned : x, - [1, 2, 3, 4, 0, 0, 0, 0, 0]) + [3, 4, 2, 5, 0, 0, 0, 0, 0]) using ModelingToolkit @parameters L g From 98490f5a98982993ffdf361113a6ec7451e69455 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 22 Apr 2025 17:46:08 +0530 Subject: [PATCH 1380/2176] test: fix discrete system tests --- test/discrete_system.jl | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/test/discrete_system.jl b/test/discrete_system.jl index 0d215052d8..b0e2481e56 100644 --- a/test/discrete_system.jl +++ b/test/discrete_system.jl @@ -3,7 +3,7 @@ - https://github.com/epirecipes/sir-julia/blob/master/markdown/function_map/function_map.md - https://en.wikipedia.org/wiki/Compartmental_models_in_epidemiology#Deterministic_versus_stochastic_epidemic_models =# -using ModelingToolkit, Test +using ModelingToolkit, SymbolicIndexingInterface, Test using ModelingToolkit: t_nounits as t using ModelingToolkit: get_metadata, MTKParameters @@ -37,13 +37,15 @@ syss = structural_simplify(sys) df = DiscreteFunction(syss) # iip du = zeros(3) -u = collect(1:3) +u = ModelingToolkit.better_varmap_to_vars(Dict([S => 1, I => 2, R => 3]), unknowns(syss)) p = MTKParameters(syss, [c, nsteps, δt, β, γ] .=> collect(1:5)) df.f(du, u, p, 0) -@test du ≈ [0.01831563888873422, 0.9816849729159067, 4.999999388195359] +reorderer = getu(syss, [S, I, R]) +@test reorderer(du) ≈ [0.01831563888873422, 0.9816849729159067, 4.999999388195359] # oop -@test df.f(u, p, 0) ≈ [0.01831563888873422, 0.9816849729159067, 4.999999388195359] +@test reorderer(df.f(u, p, 0)) ≈ + [0.01831563888873422, 0.9816849729159067, 4.999999388195359] # Problem u0 = [S(k - 1) => 990.0, I(k - 1) => 10.0, R(k - 1) => 0.0] @@ -98,12 +100,12 @@ function sir_map!(u_diff, u, p, t) end nothing end; -u0 = prob_map2.u0; +u0 = prob_map2[[S, I, R]]; p = [0.05, 10.0, 0.25, 0.1]; prob_map = DiscreteProblem(sir_map!, u0, tspan, p); sol_map2 = solve(prob_map, FunctionMap()); -@test Array(sol_map) ≈ Array(sol_map2) +@test reduce(hcat, sol_map[[S, I, R]]) ≈ Array(sol_map2) # Delayed difference equation # @variables x(..) y(..) z(t) @@ -317,9 +319,9 @@ end import ModelingToolkit: shift2term # unknowns(de) = xₜ₋₁, x, zₜ₋₁, xₜ₋₂, z - vars = ModelingToolkit.value.(unknowns(de)) - @test isequal(shift2term(Shift(t, 1)(vars[1])), vars[2]) - @test isequal(shift2term(Shift(t, 1)(vars[4])), vars[1]) - @test isequal(shift2term(Shift(t, -1)(vars[5])), vars[3]) - @test isequal(shift2term(Shift(t, -2)(vars[2])), vars[4]) + vars = sort(ModelingToolkit.value.(unknowns(de)); by = string) + @test isequal(shift2term(Shift(t, 1)(vars[2])), vars[1]) + @test isequal(shift2term(Shift(t, 1)(vars[3])), vars[2]) + @test isequal(shift2term(Shift(t, -1)(vars[4])), vars[5]) + @test isequal(shift2term(Shift(t, -2)(vars[1])), vars[3]) end From 1d544e101558b2c226a545d3522f1b681f15da97 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 24 Apr 2025 12:47:16 +0530 Subject: [PATCH 1381/2176] test: sort adjacency graph before comparing --- test/structural_transformation/utils.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/structural_transformation/utils.jl b/test/structural_transformation/utils.jl index 6c5a564e9f..b5335ad6b1 100644 --- a/test/structural_transformation/utils.jl +++ b/test/structural_transformation/utils.jl @@ -23,7 +23,7 @@ state = TearingState(pendulum) StructuralTransformations.find_solvables!(state) sss = state.structure @unpack graph, solvable_graph, var_to_diff = sss -@test graph.fadjlist == [[1, 7], [2, 8], [3, 5, 9], [4, 6, 9], [5, 6]] +@test sort(graph.fadjlist) == [[1, 7], [2, 8], [3, 5, 9], [4, 6, 9], [5, 6]] @test length(graph.badjlist) == 9 @test ne(graph) == nnz(incidence_matrix(graph)) == 12 @test nv(solvable_graph) == 9 + 5 From 54931e691598662cd76b28d7cd92feb72aa0c462 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 24 Apr 2025 12:47:34 +0530 Subject: [PATCH 1382/2176] test: reorder unknowns consistently in linearization test --- test/downstream/linearize.jl | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/test/downstream/linearize.jl b/test/downstream/linearize.jl index 9918f8145d..16df29f834 100644 --- a/test/downstream/linearize.jl +++ b/test/downstream/linearize.jl @@ -125,8 +125,9 @@ lsys = ModelingToolkit.reorder_unknowns(lsys0, unknowns(ssys), desired_order) @test lsys.C == [400 -4000] @test lsys.D == [4400 -4400] -lsyss, _ = ModelingToolkit.linearize_symbolic(pid, [reference.u, measurement.u], +lsyss0, ssys2 = ModelingToolkit.linearize_symbolic(pid, [reference.u, measurement.u], [ctr_output.u]) +lsyss = ModelingToolkit.reorder_unknowns(lsyss0, unknowns(ssys2), desired_order) @test ModelingToolkit.fixpoint_sub( lsyss.A, ModelingToolkit.defaults_and_guesses(pid)) == lsys.A @@ -138,7 +139,7 @@ lsyss, _ = ModelingToolkit.linearize_symbolic(pid, [reference.u, measurement.u], lsyss.D, ModelingToolkit.defaults_and_guesses(pid)) == lsys.D # Test with the reverse desired unknown order as well to verify that similarity transform and reoreder_unknowns really works -lsys = ModelingToolkit.reorder_unknowns(lsys, unknowns(ssys), reverse(desired_order)) +lsys = ModelingToolkit.reorder_unknowns(lsys, desired_order, reverse(desired_order)) @test lsys.A == [-10 0; 0 0] @test lsys.B == [10 -10; 2 -2] From b378c4435ffd756cd356e918aaa7e8051b2eb9ac Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 24 Apr 2025 13:05:37 +0530 Subject: [PATCH 1383/2176] test: workaround for `SciML/NonlinearSolve.jl#586` --- test/initializationsystem.jl | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/test/initializationsystem.jl b/test/initializationsystem.jl index dbc69da672..2804c70833 100644 --- a/test/initializationsystem.jl +++ b/test/initializationsystem.jl @@ -801,12 +801,17 @@ end end @parameters p=2.0 q=missing [guess = 1.0] c=1.0 - @variables x=1.0 y=2.0 z=3.0 - - eqs = [0 ~ p * (y - x), - 0 ~ x * (q - z) - y, - 0 ~ x * y - c * z] - @mtkbuild sys = NonlinearSystem(eqs; initialization_eqs = [p^2 + q^2 + 2p * q ~ 0]) + @variables x=1.0 z=3.0 + + # eqs = [0 ~ p * (y - x), + # 0 ~ x * (q - z) - y, + # 0 ~ x * y - c * z] + # specifically written this way due to + # https://github.com/SciML/NonlinearSolve.jl/issues/586 + eqs = [0 ~ -c * z + (q - z) * (x^2) + 0 ~ p * (-x + (q - z) * x)] + @named sys = NonlinearSystem(eqs; initialization_eqs = [p^2 + q^2 + 2p * q ~ 0]) + sys = complete(sys) # @mtkbuild sys = NonlinearSystem( # [p * x^2 + q * y^3 ~ 0, x - q ~ 0]; defaults = [q => missing], # guesses = [q => 1.0], initialization_eqs = [p^2 + q^2 + 2p * q ~ 0]) From 8f649f8c87a4e8502d15dec43c06a13d678cc9be Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 24 Apr 2025 14:10:25 +0530 Subject: [PATCH 1384/2176] test: make tests robust to reordering of equations/unknowns --- test/clock.jl | 5 +++-- test/input_output_handling.jl | 2 ++ test/odesystem.jl | 2 +- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/test/clock.jl b/test/clock.jl index 961c7af181..abbc6cedd3 100644 --- a/test/clock.jl +++ b/test/clock.jl @@ -72,8 +72,9 @@ sss, = ModelingToolkit._structural_simplify!(deepcopy(tss[1]), (inputs[1], ())) @test isempty(equations(sss)) d = Clock(dt) k = ShiftIndex(d) -@test observed(sss) == [yd(k + 1) ~ Sample(dt)(y); r(k + 1) ~ 1.0; - ud(k + 1) ~ kp * (r(k + 1) - yd(k + 1))] +@test issetequal(observed(sss), + [yd(k + 1) ~ Sample(dt)(y); r(k + 1) ~ 1.0; + ud(k + 1) ~ kp * (r(k + 1) - yd(k + 1))]) d = Clock(dt) # Note that TearingState reorders the equations diff --git a/test/input_output_handling.jl b/test/input_output_handling.jl index 7f4a3247ad..693a00b9ad 100644 --- a/test/input_output_handling.jl +++ b/test/input_output_handling.jl @@ -414,6 +414,8 @@ matrices, ssys = linearize(augmented_sys, augmented_sys.d ], outs; op = [augmented_sys.u => 0.0, augmented_sys.input.u[2] => 0.0, augmented_sys.d => 0.0]) +matrices = ModelingToolkit.reorder_unknowns( + matrices, unknowns(ssys), [ssys.x[2], ssys.integrator.x[1], ssys.x[1]]) @test matrices.A ≈ [A [1; 0]; zeros(1, 2) -0.001] @test matrices.B == I @test matrices.C == [C zeros(2)] diff --git a/test/odesystem.jl b/test/odesystem.jl index 4b76da6e9d..afb9e6e440 100644 --- a/test/odesystem.jl +++ b/test/odesystem.jl @@ -927,7 +927,7 @@ let sys_simp = structural_simplify(sys_con) true_eqs = [D(sys.x) ~ sys.v D(sys.v) ~ ctrl.kv * sys.v + ctrl.kx * sys.x] - @test isequal(full_equations(sys_simp), true_eqs) + @test issetequal(full_equations(sys_simp), true_eqs) end let From ea0acd45c21e678b8b37910cd70642168854d444 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 24 Apr 2025 19:32:17 +0530 Subject: [PATCH 1385/2176] fix: fix `algebraic_variables_scc` --- src/structural_transformation/tearing.jl | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/structural_transformation/tearing.jl b/src/structural_transformation/tearing.jl index d37eedc853..31ebb8370a 100644 --- a/src/structural_transformation/tearing.jl +++ b/src/structural_transformation/tearing.jl @@ -63,7 +63,8 @@ function algebraic_variables_scc(state::TearingState) all(v -> !isdervar(state.structure, v), 𝑠neighbors(graph, eq)) end)) - var_eq_matching = complete(maximal_matching(graph, e -> e in algeqs, v -> v in algvars)) + var_eq_matching = complete( + maximal_matching(graph, e -> e in algeqs, v -> v in algvars), ndsts(graph)) var_sccs = find_var_sccs(complete(graph), var_eq_matching) return var_eq_matching, var_sccs From 8bf5d9ea00935f5410209bf0bc9d2c1bc55b74b5 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 25 Apr 2025 17:09:42 +0530 Subject: [PATCH 1386/2176] feat: add kwarg to allow disabling sorting of equations in `structural_simplify` --- src/systems/systems.jl | 6 ++++-- src/systems/systemstructure.jl | 14 ++++++++------ 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/src/systems/systems.jl b/src/systems/systems.jl index 0f8633f31f..52f93afb9b 100644 --- a/src/systems/systems.jl +++ b/src/systems/systems.jl @@ -24,6 +24,7 @@ topological sort of the observed equations in `sys`. + When `simplify=true`, the `simplify` function will be applied during the tearing process. + `allow_symbolic=false`, `allow_parameter=true`, and `conservative=false` limit the coefficient types during tearing. In particular, `conservative=true` limits tearing to only solve for trivial linear systems where the coefficient has the absolute value of ``1``. + `fully_determined=true` controls whether or not an error will be thrown if the number of equations don't match the number of inputs, outputs, and equations. ++ `sort_eqs=true` controls whether equations are sorted lexicographically before simplification or not. """ function structural_simplify( sys::AbstractSystem, io = nothing; additional_passes = [], simplify = false, split = true, @@ -69,10 +70,11 @@ function __structural_simplify(sys::SDESystem, args...; kwargs...) return __structural_simplify(ODESystem(sys), args...; kwargs...) end -function __structural_simplify(sys::AbstractSystem, io = nothing; simplify = false, +function __structural_simplify( + sys::AbstractSystem, io = nothing; simplify = false, sort_eqs = true, kwargs...) sys = expand_connections(sys) - state = TearingState(sys) + state = TearingState(sys; sort_eqs) @unpack structure, fullvars = state @unpack graph, var_to_diff, var_types = structure diff --git a/src/systems/systemstructure.jl b/src/systems/systemstructure.jl index 83382cb1d2..e0feb0d34d 100644 --- a/src/systems/systemstructure.jl +++ b/src/systems/systemstructure.jl @@ -260,7 +260,7 @@ function is_time_dependent_parameter(p, iv) (args = arguments(p); length(args)) == 1 && isequal(only(args), iv)) end -function TearingState(sys; quick_cancel = false, check = true) +function TearingState(sys; quick_cancel = false, check = true, sort_eqs = true) sys = flatten(sys) ivs = independent_variables(sys) iv = length(ivs) == 1 ? ivs[1] : nothing @@ -381,11 +381,13 @@ function TearingState(sys; quick_cancel = false, check = true) neqs = length(eqs) symbolic_incidence = symbolic_incidence[eqs_to_retain] - # sort equations lexicographically to reduce simplification issues - # depending on order due to NP-completeness of tearing. - sortidxs = Base.sortperm(eqs, by = string) - eqs = eqs[sortidxs] - symbolic_incidence = symbolic_incidence[sortidxs] + if sort_eqs + # sort equations lexicographically to reduce simplification issues + # depending on order due to NP-completeness of tearing. + sortidxs = Base.sortperm(eqs, by = string) + eqs = eqs[sortidxs] + symbolic_incidence = symbolic_incidence[sortidxs] + end ### Handle discrete variables lowest_shift = Dict() From cb046ffe581c8320cb1ce8c14dcd3679d0f154fb Mon Sep 17 00:00:00 2001 From: vyudu Date: Fri, 25 Apr 2025 17:16:08 -0400 Subject: [PATCH 1387/2176] feat: At and costs in @mtkmodel --- src/systems/diffeqs/odesystem.jl | 52 +++++++++++++++----------------- src/systems/model_parsing.jl | 47 ++++++++++++++++++++++++++--- src/variables.jl | 39 ++++++++++++++++++++++++ test/model_parsing.jl | 32 ++++++++++++++++++++ test/variable_utils.jl | 26 ++++++++++++++++ 5 files changed, 165 insertions(+), 31 deletions(-) diff --git a/src/systems/diffeqs/odesystem.jl b/src/systems/diffeqs/odesystem.jl index 01b0ca5fbb..de59f3ae55 100644 --- a/src/systems/diffeqs/odesystem.jl +++ b/src/systems/diffeqs/odesystem.jl @@ -247,7 +247,7 @@ end function ODESystem(deqs::AbstractVector{<:Equation}, iv, dvs, ps; controls = Num[], observed = Equation[], - constraintsystem = nothing, + constraints = Any[], costs = Num[], consolidate = nothing, systems = ODESystem[], @@ -276,11 +276,30 @@ function ODESystem(deqs::AbstractVector{<:Equation}, iv, dvs, ps; name === nothing && throw(ArgumentError("The `name` keyword must be provided. Please consider using the `@named` macro")) @assert all(control -> any(isequal.(control, ps)), controls) "All controls must also be parameters." + + constraintsystem = nothing + if !isempty(constraints) + @show constraints + constraintsystem = process_constraint_system(constraints, dvs, ps, iv) + for p in parameters(constraintsystem) + !in(p, Set(ps)) && push!(ps, p) + end + end + + if !isempty(costs) + coststs, costps = process_costs(costs, dvs, ps, iv) + for p in costps + !in(p, Set(ps)) && push!(ps, p) + end + end + costs = wrap.(costs) + iv′ = value(iv) ps′ = value.(ps) ctrl′ = value.(controls) dvs′ = value.(dvs) dvs′ = filter(x -> !isdelay(x, iv), dvs′) + parameter_dependencies, ps′ = process_parameter_dependencies( parameter_dependencies, ps′) if !(isempty(default_u0) && isempty(default_p)) @@ -350,7 +369,7 @@ function ODESystem(deqs::AbstractVector{<:Equation}, iv, dvs, ps; metadata, gui_metadata, is_dde, tstops, checks = checks) end -function ODESystem(eqs, iv; constraints = Equation[], costs = Num[], kwargs...) +function ODESystem(eqs, iv; kwargs...) diffvars, allunknowns, ps, eqs = process_equations(eqs, iv) for eq in get(kwargs, :parameter_dependencies, Equation[]) @@ -382,29 +401,7 @@ function ODESystem(eqs, iv; constraints = Equation[], costs = Num[], kwargs...) end algevars = setdiff(allunknowns, diffvars) - consvars = OrderedSet() - constraintsystem = nothing - if !isempty(constraints) - constraintsystem = process_constraint_system(constraints, allunknowns, new_ps, iv) - for st in get_unknowns(constraintsystem) - iscall(st) ? - !in(operation(st)(iv), allunknowns) && push!(consvars, st) : - !in(st, allunknowns) && push!(consvars, st) - end - for p in parameters(constraintsystem) - !in(p, new_ps) && push!(new_ps, p) - end - end - - if !isempty(costs) - coststs, costps = process_costs(costs, allunknowns, new_ps, iv) - for p in costps - !in(p, new_ps) && push!(new_ps, p) - end - end - costs = wrap.(costs) - - return ODESystem(eqs, iv, collect(Iterators.flatten((diffvars, algevars, consvars))), + return ODESystem(eqs, iv, collect(Iterators.flatten((diffvars, algevars))), collect(new_ps); constraintsystem, costs, kwargs...) end @@ -760,7 +757,7 @@ end Build the constraint system for the ODESystem. """ function process_constraint_system( - constraints::Vector{Equation}, sts, ps, iv; consname = :cons) + constraints::Vector, sts, ps, iv; consname = :cons) isempty(constraints) && return nothing constraintsts = OrderedSet() @@ -800,7 +797,7 @@ Return the set of additional parameters found in the system, e.g. in x(p) ~ 3 th parameter of the system. """ function validate_vars_and_find_ps!(auxvars, auxps, sysvars, iv) - sts = sysvars + sts = Set(sysvars) for var in auxvars if !iscall(var) @@ -810,6 +807,7 @@ function validate_vars_and_find_ps!(auxvars, auxps, sysvars, iv) throw(ArgumentError("Too many arguments for variable $var.")) elseif length(arguments(var)) == 1 arg = only(arguments(var)) + @show sts operation(var)(iv) ∈ sts || throw(ArgumentError("Variable $var is not a variable of the ODESystem. Called variables must be variables of the ODESystem.")) diff --git a/src/systems/model_parsing.jl b/src/systems/model_parsing.jl index 195b02118e..e0afa098a7 100644 --- a/src/systems/model_parsing.jl +++ b/src/systems/model_parsing.jl @@ -65,6 +65,8 @@ function _model_macro(mod, fullname::Union{Expr, Symbol}, expr, isconnector) ps, sps, vs, = [], [], [] c_evts = [] d_evts = [] + cons = [] + costs = [] kwargs = OrderedCollections.OrderedSet() where_types = Union{Symbol, Expr}[] @@ -80,7 +82,7 @@ function _model_macro(mod, fullname::Union{Expr, Symbol}, expr, isconnector) for arg in expr.args if arg.head == :macrocall parse_model!(exprs.args, comps, ext, eqs, icon, vs, ps, - sps, c_evts, d_evts, dict, mod, arg, kwargs, where_types) + sps, c_evts, d_evts, cons, costs, dict, mod, arg, kwargs, where_types) elseif arg.head == :block push!(exprs.args, arg) elseif arg.head == :if @@ -117,16 +119,19 @@ function _model_macro(mod, fullname::Union{Expr, Symbol}, expr, isconnector) push!(exprs.args, :(push!(systems, $(comps...)))) push!(exprs.args, :(push!(variables, $(vs...)))) + gui_metadata = isassigned(icon) > 0 ? GUIMetadata(GlobalRef(mod, name), icon[]) : GUIMetadata(GlobalRef(mod, name)) + consolidate = get(dict, :consolidate, nothing) description = get(dict, :description, "") @inline pop_structure_dict!.( Ref(dict), [:constants, :defaults, :kwargs, :structural_parameters]) sys = :($type($(flatten_equations)(equations), $iv, variables, parameters; - name, description = $description, systems, gui_metadata = $gui_metadata, defaults)) + name, description = $description, systems, gui_metadata = $gui_metadata, defaults, + costs = [$(costs...)], constraints = [$(cons...)], consolidate = $consolidate)) if length(ext) == 0 push!(exprs.args, :(var"#___sys___" = $sys)) @@ -610,9 +615,10 @@ function get_var(mod::Module, b) end function parse_model!(exprs, comps, ext, eqs, icon, vs, ps, sps, c_evts, d_evts, - dict, mod, arg, kwargs, where_types) + cons, costs, dict, mod, arg, kwargs, where_types) mname = arg.args[1] body = arg.args[end] + @show dict if mname == Symbol("@description") parse_description!(body, dict) elseif mname == Symbol("@components") @@ -637,7 +643,13 @@ function parse_model!(exprs, comps, ext, eqs, icon, vs, ps, sps, c_evts, d_evts, isassigned(icon) && error("This model has more than one icon.") parse_icon!(body, dict, icon, mod) elseif mname == Symbol("@defaults") - parse_system_defaults!(exprs, arg, dict) + parse_system_defaults!(exprs, dict, body) + elseif mname == Symbol("@constraints") + parse_costs!(cons, dict, body) + elseif mname == Symbol("@costs") + parse_constraints!(costs, dict, body) + elseif mname == Symbol("@consolidate") + parse_consolidate!(body, dict) else error("$mname is not handled.") end @@ -1149,6 +1161,33 @@ function parse_discrete_events!(d_evts, dict, body) end end +function parse_constraints!(cons, dict, body) + dict[:constraints] = [] + Base.remove_linenums!(body) + for arg in body.args + push!(cons, arg) + push!(dict[:constraints], readable_code.(cons)...) + end +end + +function parse_costs!(costs, dict, body) + @show dict + dict[:costs] = [] + Base.remove_linenums!(body) + for arg in body.args + push!(costs, arg) + push!(dict[:costs], readable_code.(costs)...) + end +end + +function parse_consolidate!(body, dict) + if !(occursin("->", string(body)) || occursin("=", string(body))) + error("Consolidate must be a function definition.") + else + dict[:consolidate] = body + end +end + function parse_icon!(body::String, dict, icon, mod) icon_dir = get(ENV, "MTK_ICONS_DIR", joinpath(DEPOT_PATH[1], "mtk_icons")) dict[:icon] = icon[] = if isfile(body) diff --git a/src/variables.jl b/src/variables.jl index f3dd16819d..90afb5ee08 100644 --- a/src/variables.jl +++ b/src/variables.jl @@ -612,3 +612,42 @@ getunshifted(x::Symbolic) = Symbolics.getmetadata(x, VariableUnshifted, nothing) getshift(x::Num) = getshift(unwrap(x)) getshift(x::Symbolic) = Symbolics.getmetadata(x, VariableShift, 0) + +################### +### Evaluate at ### +################### +struct At <: Symbolics.Operator + t::Union{Symbolic, Number} +end + +function (A::At)(x::Symbolic) + if symbolic_type(x) == NotSymbolic() || !iscall(x) + if x isa Symbolics.CallWithMetadata + return x(A.t) + else + return x + end + end + + if iscall(x) && operation(x) == getindex + arr = arguments(x)[1] + term(getindex, A(arr), arguments(x)[2:end]...) + elseif operation(x) isa Differential + x = default_toterm(x) + A(x) + else + length(arguments(x)) !== 1 && error("Variable $x has too many arguments. At can only be applied to one-argument variables.") + (symbolic_type(only(arguments(x))) !== ScalarSymbolic()) && return x + return operation(x)(A.t) + end +end + +function (A::At)(x::Union{Num, Symbolics.Arr}) + wrap(A(unwrap(x))) +end +SymbolicUtils.isbinop(::At) = false + +Base.nameof(::At) = :At +Base.show(io::IO, A::At) = print(io, "At(", A.t, ")") +Base.:(==)(A1::At, A2::At) = isequal(A1.t, A2.t) +Base.hash(A::At, u::UInt) = hash(A.t, u) diff --git a/test/model_parsing.jl b/test/model_parsing.jl index e8464707de..0d49554257 100644 --- a/test/model_parsing.jl +++ b/test/model_parsing.jl @@ -1026,3 +1026,35 @@ end @named sys = Float2Bool() @test typeof(sys) == DiscreteSystem end + +@testset "Constraints, costs, consolidate" begin + @mtkmodel Example begin + @variables begin + x(t) + y(t) + end + @equations begin + x ~ y + end + @constraints begin + At(0.3)(x) ~ 3 + y ≲ 4 + end + @costs begin + x + y + At(1)(y)^2 + end + @consolidate f(u) = u[1]^2 + log(u[2]) + end + + @named ex = Example() + ex = complete(ex) + + costs = ModelingToolkit.get_costs(ex) + constrs = ModelingToolkit.get_constraints(ModelingToolkit.get_constraintsystem(ex)) + @test isequal(costs[1], ex.x + ex.y) + @test isequal(costs[2], At(1)(ex.y)^2) + @test isequal(constrs[1], -3 + At(0.3)(ex.x) ~ 0) + @test isequal(constrs[2], -4 + ex.y ≲ 0) + @test ModelingToolkit.get_consolidate(ex)([1, 2]) ≈ 1 + log(2) +end diff --git a/test/variable_utils.jl b/test/variable_utils.jl index 3204d28836..ce7f7d26eb 100644 --- a/test/variable_utils.jl +++ b/test/variable_utils.jl @@ -158,3 +158,29 @@ end @test !isinitial(c) @test !isinitial(x) end + +@testset "At" begin + @independent_variables u + @variables x(t) v(..) w(t)[1:3] + @parameters y z(u, t) r[1:3] + + @test At(1)(x) isa Num + @test isequal(At(1)(y), y) + @test_throws ErrorException At(1)(z) + @test isequal(At(1)(v), v(1)) + @test isequal(At(1)(v(t)), v(1)) + @test isequal(At(1)(v(2)), v(2)) + + arr = At(1)(w) + var = At(1)(w[1]) + @test arr isa Symbolics.Arr + @test var isa Num + + @test isequal(At(1)(r), r) + @test isequal(At(1)(r[2]), r[2]) + + _x = ModelingToolkit.unwrap(x) + @test At(1)(_x) isa Symbolics.BasicSymbolic + @test only(arguments(At(1)(_x))) == 1 + @test At(1)(D(x)) isa Num +end From 028ba8914fe4e8ac6d65263ed3b734a2718082f8 Mon Sep 17 00:00:00 2001 From: vyudu Date: Fri, 25 Apr 2025 17:16:28 -0400 Subject: [PATCH 1388/2176] format --- src/systems/model_parsing.jl | 9 ++++----- src/variables.jl | 5 +++-- test/model_parsing.jl | 2 +- test/variable_utils.jl | 4 ++-- 4 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/systems/model_parsing.jl b/src/systems/model_parsing.jl index e0afa098a7..6bea4b2b80 100644 --- a/src/systems/model_parsing.jl +++ b/src/systems/model_parsing.jl @@ -119,7 +119,6 @@ function _model_macro(mod, fullname::Union{Expr, Symbol}, expr, isconnector) push!(exprs.args, :(push!(systems, $(comps...)))) push!(exprs.args, :(push!(variables, $(vs...)))) - gui_metadata = isassigned(icon) > 0 ? GUIMetadata(GlobalRef(mod, name), icon[]) : GUIMetadata(GlobalRef(mod, name)) @@ -1161,8 +1160,8 @@ function parse_discrete_events!(d_evts, dict, body) end end -function parse_constraints!(cons, dict, body) - dict[:constraints] = [] +function parse_constraints!(cons, dict, body) + dict[:constraints] = [] Base.remove_linenums!(body) for arg in body.args push!(cons, arg) @@ -1170,9 +1169,9 @@ function parse_constraints!(cons, dict, body) end end -function parse_costs!(costs, dict, body) +function parse_costs!(costs, dict, body) @show dict - dict[:costs] = [] + dict[:costs] = [] Base.remove_linenums!(body) for arg in body.args push!(costs, arg) diff --git a/src/variables.jl b/src/variables.jl index 90afb5ee08..e5d8961225 100644 --- a/src/variables.jl +++ b/src/variables.jl @@ -616,7 +616,7 @@ getshift(x::Symbolic) = Symbolics.getmetadata(x, VariableShift, 0) ################### ### Evaluate at ### ################### -struct At <: Symbolics.Operator +struct At <: Symbolics.Operator t::Union{Symbolic, Number} end @@ -636,7 +636,8 @@ function (A::At)(x::Symbolic) x = default_toterm(x) A(x) else - length(arguments(x)) !== 1 && error("Variable $x has too many arguments. At can only be applied to one-argument variables.") + length(arguments(x)) !== 1 && + error("Variable $x has too many arguments. At can only be applied to one-argument variables.") (symbolic_type(only(arguments(x))) !== ScalarSymbolic()) && return x return operation(x)(A.t) end diff --git a/test/model_parsing.jl b/test/model_parsing.jl index 0d49554257..05380269e5 100644 --- a/test/model_parsing.jl +++ b/test/model_parsing.jl @@ -1050,7 +1050,7 @@ end @named ex = Example() ex = complete(ex) - costs = ModelingToolkit.get_costs(ex) + costs = ModelingToolkit.get_costs(ex) constrs = ModelingToolkit.get_constraints(ModelingToolkit.get_constraintsystem(ex)) @test isequal(costs[1], ex.x + ex.y) @test isequal(costs[2], At(1)(ex.y)^2) diff --git a/test/variable_utils.jl b/test/variable_utils.jl index ce7f7d26eb..f447cfeb8d 100644 --- a/test/variable_utils.jl +++ b/test/variable_utils.jl @@ -173,9 +173,9 @@ end arr = At(1)(w) var = At(1)(w[1]) - @test arr isa Symbolics.Arr + @test arr isa Symbolics.Arr @test var isa Num - + @test isequal(At(1)(r), r) @test isequal(At(1)(r[2]), r[2]) From 2085862dcb98b821ca30911ddc6d003e15e4cb20 Mon Sep 17 00:00:00 2001 From: vyudu Date: Fri, 25 Apr 2025 17:19:05 -0400 Subject: [PATCH 1389/2176] cleanup: remove @show staements --- src/systems/diffeqs/odesystem.jl | 2 -- src/systems/model_parsing.jl | 2 -- 2 files changed, 4 deletions(-) diff --git a/src/systems/diffeqs/odesystem.jl b/src/systems/diffeqs/odesystem.jl index de59f3ae55..61d437a11b 100644 --- a/src/systems/diffeqs/odesystem.jl +++ b/src/systems/diffeqs/odesystem.jl @@ -279,7 +279,6 @@ function ODESystem(deqs::AbstractVector{<:Equation}, iv, dvs, ps; constraintsystem = nothing if !isempty(constraints) - @show constraints constraintsystem = process_constraint_system(constraints, dvs, ps, iv) for p in parameters(constraintsystem) !in(p, Set(ps)) && push!(ps, p) @@ -807,7 +806,6 @@ function validate_vars_and_find_ps!(auxvars, auxps, sysvars, iv) throw(ArgumentError("Too many arguments for variable $var.")) elseif length(arguments(var)) == 1 arg = only(arguments(var)) - @show sts operation(var)(iv) ∈ sts || throw(ArgumentError("Variable $var is not a variable of the ODESystem. Called variables must be variables of the ODESystem.")) diff --git a/src/systems/model_parsing.jl b/src/systems/model_parsing.jl index 6bea4b2b80..3f37775dc7 100644 --- a/src/systems/model_parsing.jl +++ b/src/systems/model_parsing.jl @@ -617,7 +617,6 @@ function parse_model!(exprs, comps, ext, eqs, icon, vs, ps, sps, c_evts, d_evts, cons, costs, dict, mod, arg, kwargs, where_types) mname = arg.args[1] body = arg.args[end] - @show dict if mname == Symbol("@description") parse_description!(body, dict) elseif mname == Symbol("@components") @@ -1170,7 +1169,6 @@ function parse_constraints!(cons, dict, body) end function parse_costs!(costs, dict, body) - @show dict dict[:costs] = [] Base.remove_linenums!(body) for arg in body.args From 2de171c793237e84d6371f602e7ca36c804a84dc Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 21 Apr 2025 10:21:45 +0530 Subject: [PATCH 1390/2176] fix: copy initials to `u0` if `u0` not provided to `remake` --- src/systems/nonlinear/initializesystem.jl | 12 ++++++++++++ test/initializationsystem.jl | 21 +++++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/src/systems/nonlinear/initializesystem.jl b/src/systems/nonlinear/initializesystem.jl index 50c0862863..8e5b59c52d 100644 --- a/src/systems/nonlinear/initializesystem.jl +++ b/src/systems/nonlinear/initializesystem.jl @@ -593,6 +593,18 @@ function SciMLBase.late_binding_update_u0_p( prob, sys::AbstractSystem, u0, p, t0, newu0, newp) supports_initialization(sys) || return newu0, newp u0 === missing && return newu0, (p === missing ? copy(newp) : newp) + # If the user passes `p` to `remake` but not `u0` and `u0` isn't empty, + # and if the system supports initialization (so it has initial parameters), + # and if the initialization solves for `u0`, + # THEN copy the values of `Initial`s to `newu0`. + if u0 === missing && newu0 !== nothing && p !== missing && supports_initialization(sys) && prob.f.initialization_data !== nothing && prob.f.initialization_data.initializeprobmap !== nothing + if ArrayInterface.ismutable(newu0) + copyto!(newu0, getu(sys, Initial.(unknowns(sys)))(newp)) + else + T = StaticArrays.similar_type(newu0) + newu0 = T(getu(sys, Initial.(unknowns(sys)))(newp)) + end + end # non-symbolic u0 updates initials... if !(eltype(u0) <: Pair) # if `p` is not provided or is symbolic diff --git a/test/initializationsystem.jl b/test/initializationsystem.jl index 2804c70833..de7fd287ce 100644 --- a/test/initializationsystem.jl +++ b/test/initializationsystem.jl @@ -1512,3 +1512,24 @@ end @inferred remake(prob; u0 = 2 .* prob.u0, p = prob.p) @inferred solve(prob) end + +@testset "Issue#3570: `Initial`s are copied to `u0` if `u0` not provided to `remake`" begin + @parameters g + @variables x(t) [state_priority = 10] y(t) λ(t) + eqs = [D(D(x)) ~ λ * x + D(D(y)) ~ λ * y - g + x^2 + y^2 ~ 1] + @mtkbuild pend = ODESystem(eqs, t) + + prob = ODEProblem( + pend, [x => (√2 / 2)], (0.0, 1.5), [g => 1], guesses = [λ => 1, y => √2 / 2]) + sol = solve(prob) + + setter = setsym_oop(prob, [Initial(x)]) + (u0, p) = setter(prob, [0.8]) + + new_prob = remake(prob; p, initializealg = BrownFullBasicInit()) + @test new_prob[x] ≈ 0.8 + new_sol = solve(new_prob) + @test new_sol[x, 1] ≈ 0.8 +end From eb37a14f4f407bc56b50fd8bfc3db97441014a4d Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 21 Apr 2025 15:25:28 +0530 Subject: [PATCH 1391/2176] fix: remove early exit in `late_binding_update_u0_p` when `u0 === missing` --- src/systems/nonlinear/initializesystem.jl | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/src/systems/nonlinear/initializesystem.jl b/src/systems/nonlinear/initializesystem.jl index 8e5b59c52d..a6fef3b84e 100644 --- a/src/systems/nonlinear/initializesystem.jl +++ b/src/systems/nonlinear/initializesystem.jl @@ -592,19 +592,22 @@ end function SciMLBase.late_binding_update_u0_p( prob, sys::AbstractSystem, u0, p, t0, newu0, newp) supports_initialization(sys) || return newu0, newp - u0 === missing && return newu0, (p === missing ? copy(newp) : newp) # If the user passes `p` to `remake` but not `u0` and `u0` isn't empty, # and if the system supports initialization (so it has initial parameters), # and if the initialization solves for `u0`, # THEN copy the values of `Initial`s to `newu0`. - if u0 === missing && newu0 !== nothing && p !== missing && supports_initialization(sys) && prob.f.initialization_data !== nothing && prob.f.initialization_data.initializeprobmap !== nothing - if ArrayInterface.ismutable(newu0) - copyto!(newu0, getu(sys, Initial.(unknowns(sys)))(newp)) - else - T = StaticArrays.similar_type(newu0) - newu0 = T(getu(sys, Initial.(unknowns(sys)))(newp)) + if u0 === missing + if newu0 !== nothing && p !== missing && supports_initialization(sys) && prob.f.initialization_data !== nothing && prob.f.initialization_data.initializeprobmap !== nothing + if ArrayInterface.ismutable(newu0) + copyto!(newu0, getu(sys, Initial.(unknowns(sys)))(newp)) + else + T = StaticArrays.similar_type(newu0) + newu0 = T(getu(sys, Initial.(unknowns(sys)))(newp)) + end end + return newu0, newp end + # non-symbolic u0 updates initials... if !(eltype(u0) <: Pair) # if `p` is not provided or is symbolic From 6aa661def8a4eb8d96afe723a5c7c0892cae0ee0 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 23 Apr 2025 20:14:53 +0530 Subject: [PATCH 1392/2176] refactor: store `getu` function in `InitializationMetadata` --- src/systems/nonlinear/initializesystem.jl | 16 ++++++++++++---- src/systems/problem_utils.jl | 9 +++++++-- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/src/systems/nonlinear/initializesystem.jl b/src/systems/nonlinear/initializesystem.jl index a6fef3b84e..332e4b1bbb 100644 --- a/src/systems/nonlinear/initializesystem.jl +++ b/src/systems/nonlinear/initializesystem.jl @@ -592,17 +592,26 @@ end function SciMLBase.late_binding_update_u0_p( prob, sys::AbstractSystem, u0, p, t0, newu0, newp) supports_initialization(sys) || return newu0, newp + + initdata = prob.f.initialization_data + meta = initdata === nothing ? nothing : initdata.metadata # If the user passes `p` to `remake` but not `u0` and `u0` isn't empty, # and if the system supports initialization (so it has initial parameters), # and if the initialization solves for `u0`, # THEN copy the values of `Initial`s to `newu0`. if u0 === missing - if newu0 !== nothing && p !== missing && supports_initialization(sys) && prob.f.initialization_data !== nothing && prob.f.initialization_data.initializeprobmap !== nothing + if newu0 !== nothing && p !== missing && supports_initialization(sys) && + initdata !== nothing && initdata.initializeprobmap !== nothing + getter = if meta isa InitializationMetadata + meta.get_initial_unknowns + else + getu(sys, Initial.(unknowns(sys))) + end if ArrayInterface.ismutable(newu0) - copyto!(newu0, getu(sys, Initial.(unknowns(sys)))(newp)) + copyto!(newu0, getter(newp)) else T = StaticArrays.similar_type(newu0) - newu0 = T(getu(sys, Initial.(unknowns(sys)))(newp)) + newu0 = T(getter(newp)) end end return newu0, newp @@ -613,7 +622,6 @@ function SciMLBase.late_binding_update_u0_p( # if `p` is not provided or is symbolic p === missing || eltype(p) <: Pair || return newu0, newp (newu0 === nothing || isempty(newu0)) && return newu0, newp - initdata = prob.f.initialization_data initdata === nothing && return newu0, newp meta = initdata.metadata meta isa InitializationMetadata || return newu0, newp diff --git a/src/systems/problem_utils.jl b/src/systems/problem_utils.jl index 41c64b78c5..6d1a09b86a 100644 --- a/src/systems/problem_utils.jl +++ b/src/systems/problem_utils.jl @@ -769,7 +769,7 @@ properly. $(TYPEDFIELDS) """ -struct InitializationMetadata{R <: ReconstructInitializeprob, SIU} +struct InitializationMetadata{R <: ReconstructInitializeprob, GIU, SIU} """ The `u0map` used to construct the initialization. """ @@ -796,6 +796,11 @@ struct InitializationMetadata{R <: ReconstructInitializeprob, SIU} """ oop_reconstruct_u0_p::R """ + A function which takes the parameter object of the problem and returns + `Initial.(unknowns(sys))`. + """ + get_initial_unknowns::GIU + """ A function which takes the `u0` of the problem and sets `Initial.(unknowns(sys))`. """ @@ -843,7 +848,7 @@ function maybe_build_initialization_problem( meta = InitializationMetadata( u0map, pmap, guesses, Vector{Equation}(initialization_eqs), use_scc, ReconstructInitializeprob(sys, initializeprob.f.sys), - setp(sys, Initial.(unknowns(sys)))) + getp(sys, Initial.(unknowns(sys))), setp(sys, Initial.(unknowns(sys)))) if is_time_dependent(sys) all_init_syms = Set(all_symbols(initializeprob)) From 8b73ea21c51b900a27f832e72521f9f5bee95400 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 24 Apr 2025 11:07:47 +0530 Subject: [PATCH 1393/2176] refactor: copy initials to `u0` in `solve`/`init` instead of `remake` --- src/systems/nonlinear/initializesystem.jl | 55 ++++++++++++++--------- test/initializationsystem.jl | 25 ++++++++--- 2 files changed, 52 insertions(+), 28 deletions(-) diff --git a/src/systems/nonlinear/initializesystem.jl b/src/systems/nonlinear/initializesystem.jl index 332e4b1bbb..c081a02c70 100644 --- a/src/systems/nonlinear/initializesystem.jl +++ b/src/systems/nonlinear/initializesystem.jl @@ -595,27 +595,6 @@ function SciMLBase.late_binding_update_u0_p( initdata = prob.f.initialization_data meta = initdata === nothing ? nothing : initdata.metadata - # If the user passes `p` to `remake` but not `u0` and `u0` isn't empty, - # and if the system supports initialization (so it has initial parameters), - # and if the initialization solves for `u0`, - # THEN copy the values of `Initial`s to `newu0`. - if u0 === missing - if newu0 !== nothing && p !== missing && supports_initialization(sys) && - initdata !== nothing && initdata.initializeprobmap !== nothing - getter = if meta isa InitializationMetadata - meta.get_initial_unknowns - else - getu(sys, Initial.(unknowns(sys))) - end - if ArrayInterface.ismutable(newu0) - copyto!(newu0, getter(newp)) - else - T = StaticArrays.similar_type(newu0) - newu0 = T(getter(newp)) - end - end - return newu0, newp - end # non-symbolic u0 updates initials... if !(eltype(u0) <: Pair) @@ -669,6 +648,40 @@ function SciMLBase.late_binding_update_u0_p( return newu0, newp end +function DiffEqBase.get_updated_symbolic_problem(sys::AbstractSystem, prob; kw...) + supports_initialization(sys) || return prob + initdata = prob.f.initialization_data + initdata === nothing && return prob + + u0 = state_values(prob) + u0 === nothing && return prob + + p = parameter_values(prob) + t0 = is_time_dependent(prob) ? current_time(prob) : nothing + meta = initdata.metadata + + getter = if meta isa InitializationMetadata + meta.get_initial_unknowns + else + getu(sys, Initial.(unknowns(sys))) + end + + if p isa MTKParameters + buffer = p.initials + else + buffer = p + end + + u0 = DiffEqBase.promote_u0(u0, buffer, t0) + + if ArrayInterface.ismutable(u0) + T = typeof(u0) + else + T = StaticArrays.similar_type(u0) + end + return remake(prob; u0 = T(getter(p))) +end + """ $(TYPEDSIGNATURES) diff --git a/test/initializationsystem.jl b/test/initializationsystem.jl index de7fd287ce..841d29878b 100644 --- a/test/initializationsystem.jl +++ b/test/initializationsystem.jl @@ -1513,7 +1513,7 @@ end @inferred solve(prob) end -@testset "Issue#3570: `Initial`s are copied to `u0` if `u0` not provided to `remake`" begin +@testset "Issue#3570, #3552: `Initial`s are copied to `u0` during `solve`/`init`" begin @parameters g @variables x(t) [state_priority = 10] y(t) λ(t) eqs = [D(D(x)) ~ λ * x @@ -1525,11 +1525,22 @@ end pend, [x => (√2 / 2)], (0.0, 1.5), [g => 1], guesses = [λ => 1, y => √2 / 2]) sol = solve(prob) - setter = setsym_oop(prob, [Initial(x)]) - (u0, p) = setter(prob, [0.8]) + @testset "`setsym_oop`" begin + setter = setsym_oop(prob, [Initial(x)]) + (u0, p) = setter(prob, [0.8]) + new_prob = remake(prob; u0, p, initializealg = BrownFullBasicInit()) + new_sol = solve(new_prob) + @test new_sol[x, 1] ≈ 0.8 + integ = init(new_prob) + @test integ[x] ≈ 0.8 + end - new_prob = remake(prob; p, initializealg = BrownFullBasicInit()) - @test new_prob[x] ≈ 0.8 - new_sol = solve(new_prob) - @test new_sol[x, 1] ≈ 0.8 + @testset "`setsym`" begin + @test prob.ps[Initial(x)] ≈ √2 / 2 + prob.ps[Initial(x)] = 0.8 + sol = solve(prob; initializealg = BrownFullBasicInit()) + @test sol[x, 1] ≈ 0.8 + integ = init(prob; initializealg = BrownFullBasicInit()) + @test integ[x] ≈ 0.8 + end end From 2bfe21fa7556a05fdb80798f019dcbe547656046 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 24 Apr 2025 15:45:22 +0530 Subject: [PATCH 1394/2176] build: bump DiffEqBase compat --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 35e03e2924..8a7cd147b4 100644 --- a/Project.toml +++ b/Project.toml @@ -95,7 +95,7 @@ DataInterpolations = "6.4" DataStructures = "0.17, 0.18" DeepDiffs = "1" DelayDiffEq = "5.50" -DiffEqBase = "6.165.1" +DiffEqBase = "6.170.1" DiffEqCallbacks = "2.16, 3, 4" DiffEqNoiseProcess = "5" DiffRules = "0.1, 1.0" From cc2f068a88dfbe3d403e5b2e626c1c9081ab69d0 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 24 Apr 2025 18:47:53 +0530 Subject: [PATCH 1395/2176] fix: fix type promotion in `late_binding_update_u0_p` --- src/systems/nonlinear/initializesystem.jl | 36 ++++++++++++----------- 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/src/systems/nonlinear/initializesystem.jl b/src/systems/nonlinear/initializesystem.jl index c081a02c70..67f99f912b 100644 --- a/src/systems/nonlinear/initializesystem.jl +++ b/src/systems/nonlinear/initializesystem.jl @@ -589,6 +589,22 @@ function SciMLBase.remake_initialization_data( return SciMLBase.remake_initialization_data(sys, kws, newu0, t0, newp, newu0, newp) end +function promote_u0_p(u0, p::MTKParameters, t0) + u0 = DiffEqBase.promote_u0(u0, p.tunable, t0) + u0 = DiffEqBase.promote_u0(u0, p.initials, t0) + + tunables = DiffEqBase.promote_u0(p.tunable, u0, t0) + initials = DiffEqBase.promote_u0(p.initials, u0, t0) + p = SciMLStructures.replace(SciMLStructures.Tunable(), p, tunables) + p = SciMLStructures.replace(SciMLStructures.Initials(), p, initials) + + return u0, p +end + +function promote_u0_p(u0, p::AbstractArray, t0) + return DiffEqBase.promote_u0(u0, p, t0), DiffEqBase.promote_u0(p, u0, t0) +end + function SciMLBase.late_binding_update_u0_p( prob, sys::AbstractSystem, u0, p, t0, newu0, newp) supports_initialization(sys) || return newu0, newp @@ -596,6 +612,8 @@ function SciMLBase.late_binding_update_u0_p( initdata = prob.f.initialization_data meta = initdata === nothing ? nothing : initdata.metadata + newu0, newp = promote_u0_p(newu0, newp, t0) + # non-symbolic u0 updates initials... if !(eltype(u0) <: Pair) # if `p` is not provided or is symbolic @@ -605,12 +623,7 @@ function SciMLBase.late_binding_update_u0_p( meta = initdata.metadata meta isa InitializationMetadata || return newu0, newp newp = p === missing ? copy(newp) : newp - initials, repack, alias = SciMLStructures.canonicalize( - SciMLStructures.Initials(), newp) - if eltype(initials) != eltype(newu0) - initials = DiffEqBase.promote_u0(initials, newu0, t0) - newp = repack(initials) - end + if length(newu0) != length(prob.u0) throw(ArgumentError("Expected `newu0` to be of same length as unknowns ($(length(prob.u0))). Got $(typeof(newu0)) of length $(length(newu0))")) end @@ -619,17 +632,6 @@ function SciMLBase.late_binding_update_u0_p( end newp = p === missing ? copy(newp) : newp - newu0 = DiffEqBase.promote_u0(newu0, newp, t0) - tunables, repack, alias = SciMLStructures.canonicalize(SciMLStructures.Tunable(), newp) - if eltype(tunables) != eltype(newu0) - tunables = DiffEqBase.promote_u0(tunables, newu0, t0) - newp = repack(tunables) - end - initials, repack, alias = SciMLStructures.canonicalize(SciMLStructures.Initials(), newp) - if eltype(initials) != eltype(newu0) - initials = DiffEqBase.promote_u0(initials, newu0, t0) - newp = repack(initials) - end allsyms = all_symbols(sys) for (k, v) in u0 From df5afa92130bcad34e1d0ab96c98b27fc8e34fe1 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 25 Apr 2025 19:19:50 +0530 Subject: [PATCH 1396/2176] fix: propagate guesses for algebraic variables instead of `Initial(x)` --- src/systems/nonlinear/initializesystem.jl | 15 +++--- src/systems/problem_utils.jl | 56 +++++++++++++++++++++-- 2 files changed, 57 insertions(+), 14 deletions(-) diff --git a/src/systems/nonlinear/initializesystem.jl b/src/systems/nonlinear/initializesystem.jl index 67f99f912b..5995272be9 100644 --- a/src/systems/nonlinear/initializesystem.jl +++ b/src/systems/nonlinear/initializesystem.jl @@ -653,20 +653,16 @@ end function DiffEqBase.get_updated_symbolic_problem(sys::AbstractSystem, prob; kw...) supports_initialization(sys) || return prob initdata = prob.f.initialization_data - initdata === nothing && return prob + initdata isa SciMLBase.OverrideInitData || return prob + meta = initdata.metadata + meta isa InitializationMetadata || return prob + meta.get_updated_u0 === nothing && return prob u0 = state_values(prob) u0 === nothing && return prob p = parameter_values(prob) t0 = is_time_dependent(prob) ? current_time(prob) : nothing - meta = initdata.metadata - - getter = if meta isa InitializationMetadata - meta.get_initial_unknowns - else - getu(sys, Initial.(unknowns(sys))) - end if p isa MTKParameters buffer = p.initials @@ -681,7 +677,8 @@ function DiffEqBase.get_updated_symbolic_problem(sys::AbstractSystem, prob; kw.. else T = StaticArrays.similar_type(u0) end - return remake(prob; u0 = T(getter(p))) + + return remake(prob; u0 = T(meta.get_updated_u0(prob, initdata.initializeprob))) end """ diff --git a/src/systems/problem_utils.jl b/src/systems/problem_utils.jl index 6d1a09b86a..b46ef50f8e 100644 --- a/src/systems/problem_utils.jl +++ b/src/systems/problem_utils.jl @@ -769,7 +769,7 @@ properly. $(TYPEDFIELDS) """ -struct InitializationMetadata{R <: ReconstructInitializeprob, GIU, SIU} +struct InitializationMetadata{R <: ReconstructInitializeprob, GUU, SIU} """ The `u0map` used to construct the initialization. """ @@ -796,10 +796,9 @@ struct InitializationMetadata{R <: ReconstructInitializeprob, GIU, SIU} """ oop_reconstruct_u0_p::R """ - A function which takes the parameter object of the problem and returns - `Initial.(unknowns(sys))`. + A function which takes `(prob, initializeprob)` and return the `u0` to use for the problem. """ - get_initial_unknowns::GIU + get_updated_u0::GUU """ A function which takes the `u0` of the problem and sets `Initial.(unknowns(sys))`. @@ -807,6 +806,48 @@ struct InitializationMetadata{R <: ReconstructInitializeprob, GIU, SIU} set_initial_unknowns!::SIU end +""" + $(TYPEDEF) + +A callable struct to use as the `get_updated_u0` field of `InitializationMetadata`. +Returns the value of `Initial.(unknowns(sys))`, except with algebraic variables replaced +by their guess values in the initialization problem. + +# Fields + +$(TYPEDFIELDS) +""" +struct GetUpdatedU0{GA, GIU} + """ + Mask with length `length(unknowns(sys))` denoting indices of algebraic variables. + """ + algevars::BitVector + """ + Function which returns the values of algebraic variables in `initializeprob`, in the + order the algebraic variables occur in `unknowns(sys)`. + """ + get_algevars::GA + """ + Function which returns `Initial.(unknowns(sys))` as a `Vector`. + """ + get_initial_unknowns::GIU +end + +function GetUpdatedU0(sys::AbstractSystem, initsys::AbstractSystem) + algevaridxs = BitVector(is_alg_equation.(equations(sys))) + algevars = unknowns(sys)[algevaridxs] + get_algevars = getu(initsys, algevars) + get_initial_unknowns = getu(sys, Initial.(unknowns(sys))) + return GetUpdatedU0(algevaridxs, get_algevars, get_initial_unknowns) +end + +function (guu::GetUpdatedU0)(prob, initprob) + buffer = guu.get_initial_unknowns(prob) + algebuf = view(buffer, guu.algevars) + copyto!(algebuf, guu.get_algevars(initprob)) + return buffer +end + """ $(TYPEDSIGNATURES) @@ -845,10 +886,15 @@ function maybe_build_initialization_problem( end initializeprob = remake(initializeprob; p = initp) + get_initial_unknowns = if is_time_dependent(sys) + GetUpdatedU0(sys, initializeprob.f.sys) + else + nothing + end meta = InitializationMetadata( u0map, pmap, guesses, Vector{Equation}(initialization_eqs), use_scc, ReconstructInitializeprob(sys, initializeprob.f.sys), - getp(sys, Initial.(unknowns(sys))), setp(sys, Initial.(unknowns(sys)))) + get_initial_unknowns, setp(sys, Initial.(unknowns(sys)))) if is_time_dependent(sys) all_init_syms = Set(all_symbols(initializeprob)) From bc01882c588594bf51fd49b21082e0297f48f36d Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 25 Apr 2025 19:48:43 +0530 Subject: [PATCH 1397/2176] test: test `get_updated_symbolic_problem` copying guesses of algebraic variables --- test/initializationsystem.jl | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/test/initializationsystem.jl b/test/initializationsystem.jl index 841d29878b..2534fb163f 100644 --- a/test/initializationsystem.jl +++ b/test/initializationsystem.jl @@ -1513,7 +1513,7 @@ end @inferred solve(prob) end -@testset "Issue#3570, #3552: `Initial`s are copied to `u0` during `solve`/`init`" begin +@testset "Issue#3570, #3552: `Initial`s/guesses are copied to `u0` during `solve`/`init`" begin @parameters g @variables x(t) [state_priority = 10] y(t) λ(t) eqs = [D(D(x)) ~ λ * x @@ -1522,9 +1522,17 @@ end @mtkbuild pend = ODESystem(eqs, t) prob = ODEProblem( - pend, [x => (√2 / 2)], (0.0, 1.5), [g => 1], guesses = [λ => 1, y => √2 / 2]) + pend, [x => (√2 / 2), D(x) => 0.0], (0.0, 1.5), + [g => 1], guesses = [λ => 1, y => √2 / 2]) sol = solve(prob) + @testset "Guesses of initialization problem copied to algebraic variables" begin + prob.f.initialization_data.initializeprob[λ] = 1.0 + prob2 = DiffEqBase.get_updated_symbolic_problem( + pend, prob; u0 = prob.u0, p = prob.p) + @test prob2[λ] ≈ 1.0 + end + @testset "`setsym_oop`" begin setter = setsym_oop(prob, [Initial(x)]) (u0, p) = setter(prob, [0.8]) From 997da8d3319ca6b1b238d3988c0ac107fd810dfa Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 28 Apr 2025 11:08:33 +0530 Subject: [PATCH 1398/2176] fix: handle underdetermined systems in `GetUpdatedU0` --- 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 b46ef50f8e..56b7c25f17 100644 --- a/src/systems/problem_utils.jl +++ b/src/systems/problem_utils.jl @@ -834,10 +834,13 @@ struct GetUpdatedU0{GA, GIU} end function GetUpdatedU0(sys::AbstractSystem, initsys::AbstractSystem) - algevaridxs = BitVector(is_alg_equation.(equations(sys))) - algevars = unknowns(sys)[algevaridxs] + dvs = unknowns(sys) + eqs = equations(sys) + algevaridxs = BitVector(is_alg_equation.(eqs)) + append!(algevaridxs, falses(length(dvs) - length(eqs))) + algevars = dvs[algevaridxs] get_algevars = getu(initsys, algevars) - get_initial_unknowns = getu(sys, Initial.(unknowns(sys))) + get_initial_unknowns = getu(sys, Initial.(dvs)) return GetUpdatedU0(algevaridxs, get_algevars, get_initial_unknowns) end From 1026042855392012c73cfd0a5dbcb8e5b7e26b80 Mon Sep 17 00:00:00 2001 From: vyudu Date: Mon, 28 Apr 2025 15:35:45 -0400 Subject: [PATCH 1399/2176] fix: fix constructor and --- src/systems/diffeqs/odesystem.jl | 2 +- src/systems/model_parsing.jl | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/systems/diffeqs/odesystem.jl b/src/systems/diffeqs/odesystem.jl index 61d437a11b..50655d0074 100644 --- a/src/systems/diffeqs/odesystem.jl +++ b/src/systems/diffeqs/odesystem.jl @@ -401,7 +401,7 @@ function ODESystem(eqs, iv; kwargs...) algevars = setdiff(allunknowns, diffvars) return ODESystem(eqs, iv, collect(Iterators.flatten((diffvars, algevars))), - collect(new_ps); constraintsystem, costs, kwargs...) + collect(new_ps); kwargs...) end # NOTE: equality does not check cached Jacobian diff --git a/src/systems/model_parsing.jl b/src/systems/model_parsing.jl index 3f37775dc7..024c249363 100644 --- a/src/systems/model_parsing.jl +++ b/src/systems/model_parsing.jl @@ -641,7 +641,7 @@ function parse_model!(exprs, comps, ext, eqs, icon, vs, ps, sps, c_evts, d_evts, isassigned(icon) && error("This model has more than one icon.") parse_icon!(body, dict, icon, mod) elseif mname == Symbol("@defaults") - parse_system_defaults!(exprs, dict, body) + parse_system_defaults!(exprs, arg, dict) elseif mname == Symbol("@constraints") parse_costs!(cons, dict, body) elseif mname == Symbol("@costs") From a0ab00301fd9f5ced4566cf7d69b6c163c822457 Mon Sep 17 00:00:00 2001 From: vyudu Date: Mon, 28 Apr 2025 16:47:26 -0400 Subject: [PATCH 1400/2176] test: fix model parsing tests --- src/ModelingToolkit.jl | 1 + src/systems/discrete_system/discrete_system.jl | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/ModelingToolkit.jl b/src/ModelingToolkit.jl index d0f427bd14..bfe7b489b6 100644 --- a/src/ModelingToolkit.jl +++ b/src/ModelingToolkit.jl @@ -142,6 +142,7 @@ function var_derivative_graph! end include("bipartite_graph.jl") using .BipartiteGraphs +export At include("variables.jl") include("parameters.jl") include("independent_variables.jl") diff --git a/src/systems/discrete_system/discrete_system.jl b/src/systems/discrete_system/discrete_system.jl index 5f7c986659..075aa27e4d 100644 --- a/src/systems/discrete_system/discrete_system.jl +++ b/src/systems/discrete_system/discrete_system.jl @@ -121,7 +121,7 @@ struct DiscreteSystem <: AbstractDiscreteSystem tearing_state = nothing, substitutions = nothing, namespacing = true, complete = false, index_cache = nothing, parent = nothing, isscheduled = false; - checks::Union{Bool, Int} = true) + checks::Union{Bool, Int} = true, kwargs...) if checks == true || (checks & CheckComponents) > 0 check_independent_variables([iv]) check_variables(dvs, iv) @@ -199,7 +199,7 @@ function DiscreteSystem(eqs::AbstractVector{<:Equation}, iv, dvs, ps; DiscreteSystem(Threads.atomic_add!(SYSTEM_COUNT, UInt(1)), eqs, iv′, dvs′, ps′, tspan, var_to_name, observed, name, description, systems, defaults, guesses, initializesystem, initialization_eqs, preface, connector_type, - parameter_dependencies, metadata, gui_metadata, kwargs...) + parameter_dependencies, metadata, gui_metadata) end function DiscreteSystem(eqs, iv; kwargs...) From f50dc2653b91753ecab7812aa0c6b1071c8093a5 Mon Sep 17 00:00:00 2001 From: vyudu Date: Mon, 28 Apr 2025 20:09:34 -0400 Subject: [PATCH 1401/2176] fix: add validate overload for inequalities --- src/systems/unit_check.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/systems/unit_check.jl b/src/systems/unit_check.jl index 5035a22b5e..83a5ac5483 100644 --- a/src/systems/unit_check.jl +++ b/src/systems/unit_check.jl @@ -272,7 +272,7 @@ function validate(jumps::ArrayPartition{<:Union{Any, Vector{<:JumpType}}}, t::Sy all([validate(jumps.x[idx], t, info = labels[idx]) for idx in 1:3]) end -function validate(eq::Equation; info::String = "") +function validate(eq::Union{Inequality, Equation}; info::String = "") if typeof(eq.lhs) == Connection _validate(eq.rhs; info) else From 998a516741251c54fac82114ae25d16aa2a2037c Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 29 Apr 2025 11:36:33 +0530 Subject: [PATCH 1402/2176] fix: retain explicit initial value, copy guesses for unknowns in `GetUpdatedU0` --- src/systems/problem_utils.jl | 35 ++++++++++++++++++----------------- test/initializationsystem.jl | 11 +++++++++++ 2 files changed, 29 insertions(+), 17 deletions(-) diff --git a/src/systems/problem_utils.jl b/src/systems/problem_utils.jl index 56b7c25f17..47bf3c678d 100644 --- a/src/systems/problem_utils.jl +++ b/src/systems/problem_utils.jl @@ -810,44 +810,45 @@ end $(TYPEDEF) A callable struct to use as the `get_updated_u0` field of `InitializationMetadata`. -Returns the value of `Initial.(unknowns(sys))`, except with algebraic variables replaced -by their guess values in the initialization problem. +Returns the value to use for the `u0` of the problem. # Fields $(TYPEDFIELDS) """ -struct GetUpdatedU0{GA, GIU} +struct GetUpdatedU0{GG, GIU} """ - Mask with length `length(unknowns(sys))` denoting indices of algebraic variables. + Mask with length `length(unknowns(sys))` denoting indices of variables which should + take the guess value from `initializeprob`. """ - algevars::BitVector + guessvars::BitVector """ - Function which returns the values of algebraic variables in `initializeprob`, in the - order the algebraic variables occur in `unknowns(sys)`. + Function which returns the values of variables in `initializeprob` for which + `guessvars` is `true`, in the order they occur in `unknowns(sys)`. """ - get_algevars::GA + get_guessvars::GG """ Function which returns `Initial.(unknowns(sys))` as a `Vector`. """ get_initial_unknowns::GIU end -function GetUpdatedU0(sys::AbstractSystem, initsys::AbstractSystem) +function GetUpdatedU0(sys::AbstractSystem, initsys::AbstractSystem, op::AbstractDict) dvs = unknowns(sys) eqs = equations(sys) - algevaridxs = BitVector(is_alg_equation.(eqs)) - append!(algevaridxs, falses(length(dvs) - length(eqs))) - algevars = dvs[algevaridxs] - get_algevars = getu(initsys, algevars) + guessvars = trues(length(dvs)) + for (i, var) in enumerate(dvs) + guessvars[i] = !isequal(get(op, var, nothing), Initial(var)) + end + get_guessvars = getu(initsys, dvs[guessvars]) get_initial_unknowns = getu(sys, Initial.(dvs)) - return GetUpdatedU0(algevaridxs, get_algevars, get_initial_unknowns) + return GetUpdatedU0(guessvars, get_guessvars, get_initial_unknowns) end function (guu::GetUpdatedU0)(prob, initprob) buffer = guu.get_initial_unknowns(prob) - algebuf = view(buffer, guu.algevars) - copyto!(algebuf, guu.get_algevars(initprob)) + algebuf = view(buffer, guu.guessvars) + copyto!(algebuf, guu.get_guessvars(initprob)) return buffer end @@ -890,7 +891,7 @@ function maybe_build_initialization_problem( initializeprob = remake(initializeprob; p = initp) get_initial_unknowns = if is_time_dependent(sys) - GetUpdatedU0(sys, initializeprob.f.sys) + GetUpdatedU0(sys, initializeprob.f.sys, op) else nothing end diff --git a/test/initializationsystem.jl b/test/initializationsystem.jl index 2534fb163f..5c36fcba3e 100644 --- a/test/initializationsystem.jl +++ b/test/initializationsystem.jl @@ -1533,6 +1533,17 @@ end @test prob2[λ] ≈ 1.0 end + @testset "Initial values for algebraic variables are retained" begin + prob2 = ODEProblem( + pend, [x => (√2 / 2), D(y) => 0.0], (0.0, 1.5), + [g => 1], guesses = [λ => 1, y => √2 / 2]) + sol = solve(prob) + @test SciMLBase.successful_retcode(sol) + prob3 = DiffEqBase.get_updated_symbolic_problem( + pend, prob2; u0 = prob2.u0, p = prob2.p) + @test prob3[D(y)] ≈ 0.0 + end + @testset "`setsym_oop`" begin setter = setsym_oop(prob, [Initial(x)]) (u0, p) = setter(prob, [0.8]) From 6c0eb428a2ed42d422b20159c15897bdd4cec2a3 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 29 Apr 2025 11:59:45 +0530 Subject: [PATCH 1403/2176] test: make clock test robust to equation ordering --- test/clock.jl | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/test/clock.jl b/test/clock.jl index abbc6cedd3..c6051a52a8 100644 --- a/test/clock.jl +++ b/test/clock.jl @@ -76,14 +76,22 @@ k = ShiftIndex(d) [yd(k + 1) ~ Sample(dt)(y); r(k + 1) ~ 1.0; ud(k + 1) ~ kp * (r(k + 1) - yd(k + 1))]) +canonical_eqs = map(eqs) do eq + if iscall(eq.lhs) && operation(eq.lhs) isa Differential + return eq + else + return 0 ~ eq.rhs - eq.lhs + end +end +eqs_idxs = findfirst.(isequal.(canonical_eqs), (equations(ci.ts),)) d = Clock(dt) # Note that TearingState reorders the equations -@test eqmap[1] == ContinuousClock() -@test eqmap[2] == d -@test eqmap[3] == d -@test eqmap[4] == d -@test eqmap[5] == ContinuousClock() -@test eqmap[6] == ContinuousClock() +@test eqmap[eqs_idxs[1]] == d +@test eqmap[eqs_idxs[2]] == d +@test eqmap[eqs_idxs[3]] == d +@test eqmap[eqs_idxs[4]] == ContinuousClock() +@test eqmap[eqs_idxs[5]] == ContinuousClock() +@test eqmap[eqs_idxs[6]] == ContinuousClock() @test varmap[yd] == d @test varmap[ud] == d From cf45969616490036a5460396237b14c9106c4df1 Mon Sep 17 00:00:00 2001 From: Christopher Rackauckas Date: Tue, 29 Apr 2025 06:16:55 -0700 Subject: [PATCH 1404/2176] Update Project.toml --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 8a7cd147b4..2f48cc68fe 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "ModelingToolkit" uuid = "961ee093-0014-501f-94e3-6117800e7a78" authors = ["Yingbo Ma ", "Chris Rackauckas and contributors"] -version = "9.75.0" +version = "9.76.0" [deps] ADTypes = "47edcb42-4c32-4615-8424-f2b9edc5f35b" From 88b81bcecfac82325d18ef4d9876ad8945d9e5f9 Mon Sep 17 00:00:00 2001 From: Christopher Rackauckas Date: Wed, 30 Apr 2025 03:51:53 -0700 Subject: [PATCH 1405/2176] Fix Neuroblox downstream --- .github/workflows/Downstream.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/Downstream.yml b/.github/workflows/Downstream.yml index 8d8915a9cc..4f84e2c45d 100644 --- a/.github/workflows/Downstream.yml +++ b/.github/workflows/Downstream.yml @@ -39,7 +39,7 @@ jobs: - {user: SciML, repo: MethodOfLines.jl, group: DAE} - {user: SciML, repo: ModelingToolkitNeuralNets.jl, group: All} - - {user: Neuroblox, repo: Neuroblox.jl, group: NNPDE} + - {user: Neuroblox, repo: Neuroblox.jl, group: All} steps: - uses: actions/checkout@v4 - uses: julia-actions/setup-julia@v1 From f4d5d7f4c5530abaab9d454834cae6d615a62717 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 30 Apr 2025 18:53:07 +0530 Subject: [PATCH 1406/2176] refactor: remove `examples/` directory --- examples/electrical_components.jl | 95 ------------------------------- examples/rc_model.jl | 17 ------ examples/serial_inductor.jl | 33 ----------- 3 files changed, 145 deletions(-) delete mode 100644 examples/electrical_components.jl delete mode 100644 examples/rc_model.jl delete mode 100644 examples/serial_inductor.jl diff --git a/examples/electrical_components.jl b/examples/electrical_components.jl deleted file mode 100644 index 1f4f151c21..0000000000 --- a/examples/electrical_components.jl +++ /dev/null @@ -1,95 +0,0 @@ -using Test -using ModelingToolkit, OrdinaryDiffEq -using ModelingToolkit: t_nounits as t, D_nounits as D - -@connector function Pin(; name) - sts = @variables v(t) [guess = 1.0] i(t) [guess = 1.0, connect = Flow] - ODESystem(Equation[], t, sts, []; name = name) -end - -@component function Ground(; name) - @named g = Pin() - eqs = [g.v ~ 0] - compose(ODESystem(eqs, t, [], []; name = name), g) -end - -@component function OnePort(; name) - @named p = Pin() - @named n = Pin() - sts = @variables v(t) [guess = 1.0] i(t) [guess = 1.0] - eqs = [v ~ p.v - n.v - 0 ~ p.i + n.i - i ~ p.i] - compose(ODESystem(eqs, t, sts, []; name = name), p, n) -end - -@component function Resistor(; name, R = 1.0) - @named oneport = OnePort() - @unpack v, i = oneport - ps = @parameters R = R - eqs = [ - v ~ i * R - ] - extend(ODESystem(eqs, t, [], ps; name = name), oneport) -end - -@component function Capacitor(; name, C = 1.0) - @named oneport = OnePort() - @unpack v, i = oneport - ps = @parameters C = C - eqs = [ - D(v) ~ i / C - ] - extend(ODESystem(eqs, t, [], ps; name = name), oneport) -end - -@component function ConstantVoltage(; name, V = 1.0) - @named oneport = OnePort() - @unpack v = oneport - ps = @parameters V = V - eqs = [ - V ~ v - ] - extend(ODESystem(eqs, t, [], ps; name = name), oneport) -end - -@component function Inductor(; name, L = 1.0) - @named oneport = OnePort() - @unpack v, i = oneport - ps = @parameters L = L - eqs = [ - D(i) ~ v / L - ] - extend(ODESystem(eqs, t, [], ps; name = name), oneport) -end - -@connector function HeatPort(; name) - @variables T(t) [guess = 293.15] Q_flow(t) [guess = 0.0, connect = Flow] - ODESystem(Equation[], t, [T, Q_flow], [], name = name) -end - -@component function HeatingResistor(; name, R = 1.0, TAmbient = 293.15, alpha = 1.0) - @named p = Pin() - @named n = Pin() - @named h = HeatPort() - @variables v(t) RTherm(t) - @parameters R=R TAmbient=TAmbient alpha=alpha - eqs = [RTherm ~ R * (1 + alpha * (h.T - TAmbient)) - v ~ p.i * RTherm - h.Q_flow ~ -v * p.i # -LossPower - v ~ p.v - n.v - 0 ~ p.i + n.i] - compose(ODESystem(eqs, t, [v, RTherm], [R, TAmbient, alpha], - name = name), p, n, h) -end - -@component function HeatCapacitor(; name, rho = 8050, V = 1, cp = 460, TAmbient = 293.15) - @parameters rho=rho V=V cp=cp - C = rho * V * cp - @named h = HeatPort() - eqs = [ - D(h.T) ~ h.Q_flow / C - ] - compose(ODESystem(eqs, t, [], [rho, V, cp], - name = name), h) -end diff --git a/examples/rc_model.jl b/examples/rc_model.jl deleted file mode 100644 index 158436d980..0000000000 --- a/examples/rc_model.jl +++ /dev/null @@ -1,17 +0,0 @@ -include("electrical_components.jl") - -R = 1.0 -C = 1.0 -V = 1.0 -@named resistor = Resistor(R = R) -@named capacitor = Capacitor(C = C) -@named source = ConstantVoltage(V = V) -@named ground = Ground() - -rc_eqs = [connect(source.p, resistor.p) - connect(resistor.n, capacitor.p) - connect(capacitor.n, source.n) - connect(capacitor.n, ground.g)] - -@named rc_model = ODESystem(rc_eqs, t) -rc_model = compose(rc_model, [resistor, capacitor, source, ground]) diff --git a/examples/serial_inductor.jl b/examples/serial_inductor.jl deleted file mode 100644 index 302df32c17..0000000000 --- a/examples/serial_inductor.jl +++ /dev/null @@ -1,33 +0,0 @@ -include("electrical_components.jl") - -@named source = ConstantVoltage(V = 10.0) -@named resistor = Resistor(R = 1.0) -@named inductor1 = Inductor(L = 1.0e-2) -@named inductor2 = Inductor(L = 2.0e-2) -@named ground = Ground() - -eqs = [connect(source.p, resistor.p) - connect(resistor.n, inductor1.p) - connect(inductor1.n, inductor2.p) - connect(source.n, inductor2.n) - connect(inductor2.n, ground.g)] - -@named ll_model = ODESystem(eqs, t) -ll_model = compose(ll_model, [source, resistor, inductor1, inductor2, ground]) - -@named source = ConstantVoltage(V = 10.0) -@named resistor1 = Resistor(R = 1.0) -@named resistor2 = Resistor(R = 1.0) -@named inductor1 = Inductor(L = 1.0e-2) -@named inductor2 = Inductor(L = 2.0e-2) -@named ground = Ground() - -eqs = [connect(source.p, inductor1.p) - connect(inductor1.n, resistor1.p) - connect(inductor1.n, resistor2.p) - connect(resistor1.n, resistor2.n) - connect(resistor2.n, inductor2.p) - connect(source.n, inductor2.n) - connect(inductor2.n, ground.g)] -@named ll2_model = ODESystem(eqs, t) -ll2_model = compose(ll2_model, [source, resistor1, resistor2, inductor1, inductor2, ground]) From 1a19972dbd7c1bdba5a4bab4d5f0a58aec167551 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 30 Apr 2025 18:53:27 +0530 Subject: [PATCH 1407/2176] test: add common components under `test/common/` --- test/common/rc_model.jl | 26 +++++++++++++++++++ test/common/serial_inductor.jl | 47 ++++++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+) create mode 100644 test/common/rc_model.jl create mode 100644 test/common/serial_inductor.jl diff --git a/test/common/rc_model.jl b/test/common/rc_model.jl new file mode 100644 index 0000000000..8eec8d048f --- /dev/null +++ b/test/common/rc_model.jl @@ -0,0 +1,26 @@ +import ModelingToolkitStandardLibrary.Electrical as El +import ModelingToolkitStandardLibrary.Blocks as Bl + +@mtkmodel RCModel begin + @parameters begin + R = 1.0 + C = 1.0 + V = 1.0 + end + @components begin + resistor = El.Resistor(R = R) + capacitor = El.Capacitor(C = C) + shape = Bl.Constant(k = V) + source = El.Voltage() + ground = El.Ground() + end + @equations begin + connect(shape.output, source.V) + connect(source.p, resistor.p) + connect(resistor.n, capacitor.p) + connect(capacitor.n, source.n) + connect(capacitor.n, ground.g) + end +end + +@named rc_model = RCModel() diff --git a/test/common/serial_inductor.jl b/test/common/serial_inductor.jl new file mode 100644 index 0000000000..1e930cee56 --- /dev/null +++ b/test/common/serial_inductor.jl @@ -0,0 +1,47 @@ +import ModelingToolkitStandardLibrary.Electrical as El +import ModelingToolkitStandardLibrary.Blocks as Bl + +@mtkmodel LLModel begin + @components begin + shape = Bl.Constant(k = 10.0) + source = El.Voltage() + resistor = El.Resistor(R = 1.0) + inductor1 = El.Inductor(L = 1.0e-2) + inductor2 = El.Inductor(L = 2.0e-2) + ground = El.Ground() + end + @equations begin + connect(shape.output, source.V) + connect(source.p, resistor.p) + connect(resistor.n, inductor1.p) + connect(inductor1.n, inductor2.p) + connect(source.n, inductor2.n) + connect(inductor2.n, ground.g) + end +end + +@named ll_model = LLModel() + +@mtkmodel LL2Model begin + @components begin + shape = Bl.Constant(k = 10.0) + source = El.Voltage() + resistor1 = El.Resistor(R = 1.0) + resistor2 = El.Resistor(R = 1.0) + inductor1 = El.Inductor(L = 1.0e-2) + inductor2 = El.Inductor(L = 2.0e-2) + ground = El.Ground() + end + @equations begin + connect(shape.output, source.V) + connect(source.p, inductor1.p) + connect(inductor1.n, resistor1.p) + connect(inductor1.n, resistor2.p) + connect(resistor1.n, resistor2.n) + connect(resistor2.n, inductor2.p) + connect(source.n, inductor2.n) + connect(inductor2.n, ground.g) + end +end + +@named ll2_model = LL2Model() From 2af3997ef592e05655246848f5615a514d3bfd77 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 30 Apr 2025 18:53:38 +0530 Subject: [PATCH 1408/2176] test: use new common components in tests --- test/components.jl | 436 ++++++++++++++++++----------------------- test/error_handling.jl | 3 +- test/funcaffect.jl | 24 ++- test/print_tree.jl | 7 +- test/serialization.jl | 8 +- 5 files changed, 225 insertions(+), 253 deletions(-) diff --git a/test/components.jl b/test/components.jl index 8ac40f6fbb..43c0d1acf8 100644 --- a/test/components.jl +++ b/test/components.jl @@ -4,211 +4,159 @@ using ModelingToolkit: get_component_type using ModelingToolkit.BipartiteGraphs using ModelingToolkit.StructuralTransformations using ModelingToolkit: t_nounits as t, D_nounits as D -include("../examples/rc_model.jl") - -function check_contract(sys) - state = ModelingToolkit.get_tearing_state(sys) - graph = state.structure.graph - fullvars = state.fullvars - sys = tearing_substitution(sys) - - eqs = equations(sys) - for (i, eq) in enumerate(eqs) - actual = union(ModelingToolkit.vars(eq.lhs), ModelingToolkit.vars(eq.rhs)) - actual = filter(!ModelingToolkit.isparameter, collect(actual)) - current = Set(fullvars[𝑠neighbors(graph, i)]) - @test isempty(setdiff(actual, current)) +using ModelingToolkitStandardLibrary.Electrical +using ModelingToolkitStandardLibrary.Blocks +using LinearAlgebra +using ModelingToolkitStandardLibrary.Thermal +include("common/rc_model.jl") + +@testset "Basics" begin + @unpack resistor, capacitor, source = rc_model + function check_contract(sys) + state = ModelingToolkit.get_tearing_state(sys) + graph = state.structure.graph + fullvars = state.fullvars + sys = tearing_substitution(sys) + + eqs = equations(sys) + for (i, eq) in enumerate(eqs) + actual = union(ModelingToolkit.vars(eq.lhs), ModelingToolkit.vars(eq.rhs)) + actual = filter(!ModelingToolkit.isparameter, collect(actual)) + current = Set(fullvars[𝑠neighbors(graph, i)]) + @test isempty(setdiff(actual, current)) + end end -end -function check_rc_sol(sol) - rpi = sol[rc_model.resistor.p.i] - rpifun = sol.prob.f.observed(rc_model.resistor.p.i) - @test rpifun.(sol.u, (sol.prob.p,), sol.t) == rpi - @test any(!isequal(rpi[1]), rpi) # test that we don't have a constant system - @test sol[rc_model.resistor.p.i] == sol[resistor.p.i] == sol[capacitor.p.i] - @test sol[rc_model.resistor.n.i] == sol[resistor.n.i] == -sol[capacitor.p.i] - @test sol[rc_model.capacitor.n.i] == sol[capacitor.n.i] == -sol[capacitor.p.i] - @test iszero(sol[rc_model.ground.g.i]) - @test iszero(sol[rc_model.ground.g.v]) - @test sol[rc_model.resistor.v] == sol[resistor.v] == - sol[source.p.v] - sol[capacitor.p.v] -end - -@named pin = Pin() -@test get_component_type(pin).name == :Pin -@test get_component_type(rc_model.resistor).name == :Resistor - -completed_rc_model = complete(rc_model) -@test isequal(completed_rc_model.resistor.n.i, resistor.n.i) -@test ModelingToolkit.n_expanded_connection_equations(capacitor) == 2 -@test length(equations(structural_simplify(rc_model, allow_parameter = false))) == 2 -sys = structural_simplify(rc_model) -@test_throws ModelingToolkit.RepeatedStructuralSimplificationError structural_simplify(sys) -@test length(equations(sys)) == 1 -check_contract(sys) -@test !isempty(ModelingToolkit.defaults(sys)) -u0 = [capacitor.v => 0.0] -prob = ODEProblem(sys, u0, (0, 10.0)) -sol = solve(prob, Rodas4()) -check_rc_sol(sol) - -# https://discourse.julialang.org/t/using-optimization-parameters-in-modelingtoolkit/82099 -let - @parameters param_r1 param_c1 - @named resistor = Resistor(R = param_r1) - @named capacitor = Capacitor(C = param_c1) - @named source = ConstantVoltage(V = 1.0) - @named ground = Ground() + function check_rc_sol(sol) + rpi = sol[rc_model.resistor.p.i] + rpifun = sol.prob.f.observed(rc_model.resistor.p.i) + @test rpifun.(sol.u, (sol.prob.p,), sol.t) == rpi + @test any(!isequal(rpi[1]), rpi) # test that we don't have a constant system + @test sol[rc_model.resistor.p.i] == sol[resistor.p.i] == sol[capacitor.p.i] + @test sol[rc_model.resistor.n.i] == sol[resistor.n.i] == -sol[capacitor.p.i] + @test sol[rc_model.capacitor.n.i] == sol[capacitor.n.i] == -sol[capacitor.p.i] + @test iszero(sol[rc_model.ground.g.i]) + @test iszero(sol[rc_model.ground.g.v]) + @test sol[rc_model.resistor.v] == sol[resistor.v] == + sol[source.p.v] - sol[capacitor.p.v] + end - rc_eqs = [connect(source.p, resistor.p) - connect(resistor.n, capacitor.p) - connect(capacitor.n, source.n) - connect(capacitor.n, ground.g)] + @named pin = Pin() + @test get_component_type(pin).name == :Pin + @test get_component_type(rc_model.resistor).name == :Resistor - @named _rc_model = ODESystem(rc_eqs, t) - @named rc_model = compose(_rc_model, - [resistor, capacitor, source, ground]) + completed_rc_model = complete(rc_model) + @test isequal(completed_rc_model.resistor.n.i, resistor.n.i) + @test ModelingToolkit.n_expanded_connection_equations(capacitor) == 2 + @test length(equations(structural_simplify(rc_model, allow_parameter = false))) == 2 sys = structural_simplify(rc_model) - u0 = [ - capacitor.v => 0.0 - ] - - params = [param_r1 => 1.0, param_c1 => 1.0] - tspan = (0.0, 10.0) - - prob = ODEProblem(sys, u0, tspan, params) - @test solve(prob, Tsit5()).retcode == ReturnCode.Success + @test_throws ModelingToolkit.RepeatedStructuralSimplificationError structural_simplify(sys) + @test length(equations(sys)) == 1 + check_contract(sys) + @test !isempty(ModelingToolkit.defaults(sys)) + u0 = [capacitor.v => 0.0] + prob = ODEProblem(sys, u0, (0, 10.0)) + sol = solve(prob, Rodas4()) + check_rc_sol(sol) end -let - # 1478 - @named resistor2 = Resistor(R = R) - rc_eqs2 = [connect(source.p, resistor.p) - connect(resistor.n, resistor2.p) - connect(resistor2.n, capacitor.p) - connect(capacitor.n, source.n) - connect(capacitor.n, ground.g)] - - @named _rc_model2 = ODESystem(rc_eqs2, t) - @named rc_model2 = compose(_rc_model2, - [resistor, resistor2, capacitor, source, ground]) - sys2 = structural_simplify(rc_model2) - prob2 = ODEProblem(sys2, [source.p.i => 0.0], (0, 10.0), guesses = u0) - sol2 = solve(prob2, Rosenbrock23()) - @test sol2[source.p.i] ≈ sol2[rc_model2.source.p.i] ≈ -sol2[capacitor.i] - - prob3 = ODEProblem(sys2, [], (0, 10.0), guesses = u0) - sol3 = solve(prob2, Rosenbrock23()) - @test sol3[unknowns(rc_model2), end] ≈ sol2[unknowns(rc_model2), end] -end +@testset "Outer/inner connections" begin + sys = structural_simplify(rc_model) -# Outer/inner connections -function rc_component(; name, R = 1, C = 1) - @parameters R=R C=C - @named p = Pin() - @named n = Pin() - @named resistor = Resistor(R = R) # test parent scope default of @named - @named capacitor = Capacitor(C = ParentScope(C)) - eqs = [connect(p, resistor.p); - connect(resistor.n, capacitor.p); - connect(capacitor.n, n)] - @named sys = ODESystem(eqs, t, [], [R, C]) - compose(sys, [p, n, resistor, capacitor]; name = name) -end + prob = ODEProblem(sys, [sys.capacitor.v => 0.0], (0.0, 10.0)) + sol = solve(prob, Rodas4()) + function rc_component(; name, R = 1, C = 1) + local sys + @parameters R=R C=C + @named p = Pin() + @named n = Pin() + @named resistor = Resistor(R = R) # test parent scope default of @named + @named capacitor = Capacitor(C = C) + eqs = [connect(p, resistor.p); + connect(resistor.n, capacitor.p); + connect(capacitor.n, n)] + @named sys = ODESystem(eqs, t, [], [R, C]) + compose(sys, [p, n, resistor, capacitor]; name = name) + end -@named ground = Ground() -@named source = ConstantVoltage(V = 1) -@named rc_comp = rc_component() -eqs = [connect(source.p, rc_comp.p) - connect(source.n, rc_comp.n) - connect(source.n, ground.g)] -@named sys′ = ODESystem(eqs, t) -@named sys_inner_outer = compose(sys′, [ground, source, rc_comp]) -@test_nowarn show(IOBuffer(), MIME"text/plain"(), sys_inner_outer) -expand_connections(sys_inner_outer, debug = true) -sys_inner_outer = structural_simplify(sys_inner_outer) -@test !isempty(ModelingToolkit.defaults(sys_inner_outer)) -u0 = [rc_comp.capacitor.v => 0.0] -prob = ODEProblem(sys_inner_outer, u0, (0, 10.0), sparse = true) -sol_inner_outer = solve(prob, Rodas4()) -@test sol[capacitor.v] ≈ sol_inner_outer[rc_comp.capacitor.v] - -u0 = [ - capacitor.v => 0.0 -] -prob = ODEProblem(sys, u0, (0, 10.0)) -sol = solve(prob, Tsit5()) - -@test sol[resistor.p.i] == sol[capacitor.p.i] -@test sol[resistor.n.i] == -sol[capacitor.p.i] -@test sol[capacitor.n.i] == -sol[capacitor.p.i] -@test iszero(sol[ground.g.i]) -@test iszero(sol[ground.g.v]) -@test sol[resistor.v] == sol[source.p.v] - sol[capacitor.p.v] + @named ground = Ground() + @named shape = Constant(k = 1) + @named source = Voltage() + @named rc_comp = rc_component() + eqs = [connect(shape.output, source.V) + connect(source.p, rc_comp.p) + connect(source.n, rc_comp.n) + connect(source.n, ground.g)] + @named sys′ = ODESystem(eqs, t) + @named sys_inner_outer = compose(sys′, [ground, shape, source, rc_comp]) + @test_nowarn show(IOBuffer(), MIME"text/plain"(), sys_inner_outer) + expand_connections(sys_inner_outer, debug = true) + sys_inner_outer = structural_simplify(sys_inner_outer) + @test !isempty(ModelingToolkit.defaults(sys_inner_outer)) + u0 = [rc_comp.capacitor.v => 0.0] + prob = ODEProblem(sys_inner_outer, u0, (0, 10.0), sparse = true) + sol_inner_outer = solve(prob, Rodas4()) + @test sol[sys.capacitor.v] ≈ sol_inner_outer[rc_comp.capacitor.v] + + prob = ODEProblem(sys, [sys.capacitor.v => 0.0], (0, 10.0)) + sol = solve(prob, Tsit5()) + + @test sol[sys.resistor.p.i] == sol[sys.capacitor.p.i] + @test sol[sys.resistor.n.i] == -sol[sys.capacitor.p.i] + @test sol[sys.capacitor.n.i] == -sol[sys.capacitor.p.i] + @test iszero(sol[sys.ground.g.i]) + @test iszero(sol[sys.ground.g.v]) + @test sol[sys.resistor.v] == sol[sys.source.p.v] - sol[sys.capacitor.p.v] +end #using Plots #plot(sol) -include("../examples/serial_inductor.jl") -sys = structural_simplify(ll_model) -@test length(equations(sys)) == 2 -u0 = unknowns(sys) .=> 0 -@test_nowarn ODEProblem( - sys, [], (0, 10.0), guesses = u0, warn_initialize_determined = false) -prob = DAEProblem(sys, D.(unknowns(sys)) .=> 0, [], (0, 0.5), guesses = u0) -sol = solve(prob, DFBDF()) -@test sol.retcode == SciMLBase.ReturnCode.Success - -sys2 = structural_simplify(ll2_model) -@test length(equations(sys2)) == 3 -u0 = unknowns(sys) .=> 0 -prob = ODEProblem(sys, u0, (0, 10.0)) -@test_nowarn sol = solve(prob, FBDF()) - -@variables x1(t) x2(t) x3(t) x4(t) -@named sys1_inner = ODESystem([D(x1) ~ x1], t) -@named sys1_partial = compose(ODESystem([D(x2) ~ x2], t; name = :foo), sys1_inner) -@named sys1 = extend(ODESystem([D(x3) ~ x3], t; name = :foo), sys1_partial) -@named sys2 = compose(ODESystem([D(x4) ~ x4], t; name = :foo), sys1) -@test_nowarn sys2.sys1.sys1_inner.x1 # test the correct nesting - -# compose tests -function record_fun(; name) - pars = @parameters a=10 b=100 - ODESystem(Equation[], t, [], pars; name) +include("common/serial_inductor.jl") +@testset "Serial inductor" begin + sys = structural_simplify(ll_model) + @test length(equations(sys)) == 2 + u0 = unknowns(sys) .=> 0 + @test_nowarn ODEProblem( + sys, [], (0, 10.0), guesses = u0, warn_initialize_determined = false) + prob = DAEProblem(sys, D.(unknowns(sys)) .=> 0, [], (0, 0.5), guesses = u0) + sol = solve(prob, DFBDF()) + @test sol.retcode == SciMLBase.ReturnCode.Success + + sys2 = structural_simplify(ll2_model) + @test length(equations(sys2)) == 3 + u0 = unknowns(sys) .=> 0 + prob = ODEProblem(sys, u0, (0, 10.0)) + @test_nowarn sol = solve(prob, FBDF()) end -function first_model(; name) - @named foo = record_fun() +@testset "Compose/extend" begin + @variables x1(t) x2(t) x3(t) x4(t) + @named sys1_inner = ODESystem([D(x1) ~ x1], t) + @named sys1_partial = compose(ODESystem([D(x2) ~ x2], t; name = :foo), sys1_inner) + @named sys1 = extend(ODESystem([D(x3) ~ x3], t; name = :foo), sys1_partial) + @named sys2 = compose(ODESystem([D(x4) ~ x4], t; name = :foo), sys1) + @test_nowarn sys2.sys1.sys1_inner.x1 # test the correct nesting + + # compose tests + function record_fun(; name) + pars = @parameters a=10 b=100 + ODESystem(Equation[], t, [], pars; name) + end + + function first_model(; name) + @named foo = record_fun() - defs = Dict() - defs[foo.a] = 3 - defs[foo.b] = 300 - pars = @parameters x=2 y=20 - compose(ODESystem(Equation[], t, [], pars; name, defaults = defs), foo) + defs = Dict() + defs[foo.a] = 3 + defs[foo.b] = 300 + pars = @parameters x=2 y=20 + compose(ODESystem(Equation[], t, [], pars; name, defaults = defs), foo) + end + @named goo = first_model() + @unpack foo = goo + @test ModelingToolkit.defaults(goo)[foo.a] == 3 + @test ModelingToolkit.defaults(goo)[foo.b] == 300 end -@named goo = first_model() -@unpack foo = goo -@test ModelingToolkit.defaults(goo)[foo.a] == 3 -@test ModelingToolkit.defaults(goo)[foo.b] == 300 - -#= -model Circuit - Ground ground; - Load load; - Resistor resistor; -equation - connect(load.p , ground.p); - connect(resistor.p, ground.p); -end Circuit; -model Load - extends TwoPin; - Resistor resistor; -equation - connect(p, resistor.p); - connect(resistor.n, n); -end Load; -=# function Load(; name) R = 1 @@ -236,62 +184,68 @@ end @test structural_simplify(foo) isa ModelingToolkit.AbstractSystem # BLT tests -using LinearAlgebra -function parallel_rc_model(i; name, source, ground, R, C) - resistor = HeatingResistor(name = Symbol(:resistor, i), R = R) - capacitor = Capacitor(name = Symbol(:capacitor, i), C = C) - heat_capacitor = HeatCapacitor(name = Symbol(:heat_capacitor, i)) - - rc_eqs = [connect(source.p, resistor.p) - connect(resistor.n, capacitor.p) - connect(capacitor.n, source.n, ground.g) - connect(resistor.h, heat_capacitor.h)] - - compose(ODESystem(rc_eqs, t, name = Symbol(name, i)), - [resistor, capacitor, source, ground, heat_capacitor]) +@testset "BLT ordering" begin + function parallel_rc_model(i; name, shape, source, ground, R, C) + resistor = Resistor(name = Symbol(:resistor, i), R = R, T_dep = true) + capacitor = Capacitor(name = Symbol(:capacitor, i), C = C) + heat_capacitor = HeatCapacitor(name = Symbol(:heat_capacitor, i)) + + rc_eqs = [connect(shape.output, source.V) + connect(source.p, resistor.p) + connect(resistor.n, capacitor.p) + connect(capacitor.n, source.n, ground.g) + connect(resistor.heat_port, heat_capacitor.port)] + + compose(ODESystem(rc_eqs, t, name = Symbol(name, i)), + [resistor, capacitor, source, ground, heat_capacitor]) + end + V = 2.0 + @named shape = Constant(k = V) + @named source = Voltage() + @named ground = Ground() + N = 50 + Rs = 10 .^ range(0, stop = -4, length = N) + Cs = 10 .^ range(-3, stop = 0, length = N) + rc_systems = map(1:N) do i + parallel_rc_model(i; name = :rc, source, ground, shape, R = Rs[i], C = Cs[i]) + end + @variables E(t) = 0.0 + eqs = [ + D(E) ~ sum(((i, sys),) -> getproperty(sys, Symbol(:resistor, i)).heat_port.Q_flow, + enumerate(rc_systems)) + ] + @named _big_rc = ODESystem(eqs, t, [E], []) + @named big_rc = compose(_big_rc, rc_systems) + ts = TearingState(expand_connections(big_rc)) + # this is block upper triangular, so `istriu` needs a little leeway + @test istriu(but_ordered_incidence(ts)[1], -2) end -V = 2.0 -@named source = ConstantVoltage(V = V) -@named ground = Ground() -N = 50 -Rs = 10 .^ range(0, stop = -4, length = N) -Cs = 10 .^ range(-3, stop = 0, length = N) -rc_systems = map(1:N) do i - parallel_rc_model(i; name = :rc, source = source, ground = ground, R = Rs[i], C = Cs[i]) -end; -@variables E(t) = 0.0 -eqs = [ - D(E) ~ sum(((i, sys),) -> getproperty(sys, Symbol(:resistor, i)).h.Q_flow, - enumerate(rc_systems)) -] -@named _big_rc = ODESystem(eqs, t, [E], []) -@named big_rc = compose(_big_rc, rc_systems) -ts = TearingState(expand_connections(big_rc)) -@test istriu(but_ordered_incidence(ts)[1]) # Test using constants inside subsystems -function FixedResistor(; name, R = 1.0) - @named oneport = OnePort() - @unpack v, i = oneport - @constants R = R - eqs = [ - v ~ i * R - ] - extend(ODESystem(eqs, t, [], []; name = name), oneport) +@testset "Constants inside subsystems" begin + function FixedResistor(; name, R = 1.0) + @named oneport = OnePort() + @unpack v, i = oneport + @constants R = R + eqs = [ + v ~ i * R + ] + extend(ODESystem(eqs, t, [], []; name = name), oneport) + end + capacitor = Capacitor(; name = :c1, C = 1.0) + resistor = FixedResistor(; name = :r1) + ground = Ground(; name = :ground) + rc_eqs = [connect(capacitor.n, resistor.p) + connect(resistor.n, capacitor.p) + connect(capacitor.n, ground.g)] + + @named _rc_model = ODESystem(rc_eqs, t) + @named rc_model = compose(_rc_model, + [resistor, capacitor, ground]) + sys = structural_simplify(rc_model) + prob = ODEProblem(sys, [sys.c1.v => 0.0], (0, 10.0)) + sol = solve(prob, Tsit5()) end -capacitor = Capacitor(; name = :c1) -resistor = FixedResistor(; name = :r1) -ground = Ground(; name = :ground) -rc_eqs = [connect(capacitor.n, resistor.p) - connect(resistor.n, capacitor.p) - connect(capacitor.n, ground.g)] - -@named _rc_model = ODESystem(rc_eqs, t) -@named rc_model = compose(_rc_model, - [resistor, capacitor, ground]) -sys = structural_simplify(rc_model) -prob = ODEProblem(sys, u0, (0, 10.0)) -sol = solve(prob, Tsit5()) @testset "docstrings (#1155)" begin """ diff --git a/test/error_handling.jl b/test/error_handling.jl index 59aa6b79a1..d5605e3cbc 100644 --- a/test/error_handling.jl +++ b/test/error_handling.jl @@ -1,8 +1,9 @@ using Test using ModelingToolkit +using ModelingToolkit: t_nounits as t, D_nounits as D import ModelingToolkit: ExtraVariablesSystemException, ExtraEquationsSystemException -include("../examples/electrical_components.jl") +using ModelingToolkitStandardLibrary.Electrical function UnderdefinedConstantVoltage(; name, V = 1.0) val = V diff --git a/test/funcaffect.jl b/test/funcaffect.jl index 3004044d61..05543c9161 100644 --- a/test/funcaffect.jl +++ b/test/funcaffect.jl @@ -1,4 +1,6 @@ using ModelingToolkit, Test, OrdinaryDiffEq +using ModelingToolkitStandardLibrary.Electrical +using ModelingToolkitStandardLibrary.Blocks using ModelingToolkit: t_nounits as t, D_nounits as D @constants h=1 zr=0 @@ -134,19 +136,21 @@ prob = ODEProblem(s2, [resistor.v => 10.0], (0, 2.01)) sol = solve(prob, Tsit5()) @test ctx[1] == 2 -include("../examples/rc_model.jl") +include("common/rc_model.jl") function affect5!(integ, u, p, ctx) @test integ.u[u.capacitor₊v] ≈ 0.3 integ.ps[p.C] *= 200 end -@named rc_model = ODESystem(rc_eqs, t, +@unpack capacitor = rc_model +@named event_sys = ODESystem(Equation[], t; continuous_events = [ [capacitor.v ~ 0.3] => (affect5!, [capacitor.v], [capacitor.C => :C], [capacitor.C], nothing) ]) -rc_model = compose(rc_model, [resistor, capacitor, source, ground]) +rc_model = extend(rc_model, event_sys) +# rc_model = compose(rc_model, [resistor, capacitor, source, ground]) sys = structural_simplify(rc_model) u0 = [capacitor.v => 0.0 @@ -177,15 +181,23 @@ function Capacitor2(; name, C = 1.0) oneport) end -@named capacitor2 = Capacitor2(C = C) +@named begin + capacitor2 = Capacitor2(C = 1.0) + resistor = Resistor(R = 1.0) + capacitor = Capacitor(C = 1.0) + shape = Constant(k = 1.0) + source = Voltage() + ground = Ground() +end -rc_eqs2 = [connect(source.p, resistor.p) +rc_eqs2 = [connect(shape.output, source.V) + connect(source.p, resistor.p) connect(resistor.n, capacitor2.p) connect(capacitor2.n, source.n) connect(capacitor2.n, ground.g)] @named rc_model2 = ODESystem(rc_eqs2, t) -rc_model2 = compose(rc_model2, [resistor, capacitor2, source, ground]) +rc_model2 = compose(rc_model2, [resistor, capacitor2, shape, source, ground]) sys2 = structural_simplify(rc_model2) u0 = [capacitor2.v => 0.0 diff --git a/test/print_tree.jl b/test/print_tree.jl index 7584a22c60..351e95f612 100644 --- a/test/print_tree.jl +++ b/test/print_tree.jl @@ -1,6 +1,6 @@ using ModelingToolkit, AbstractTrees, Test -include("../examples/rc_model.jl") +include("common/rc_model.jl") io = IOBuffer() print_tree(io, rc_model) @@ -12,9 +12,12 @@ str = """rc_model ├─ capacitor │ ├─ p │ └─ n + ├─ shape + │ └─ output ├─ source │ ├─ p - │ └─ n + │ ├─ n + │ └─ V └─ ground └─ g """ diff --git a/test/serialization.jl b/test/serialization.jl index feb3c7e4e7..ee380b6616 100644 --- a/test/serialization.jl +++ b/test/serialization.jl @@ -22,12 +22,14 @@ for prob in [ run(`$(Base.julia_cmd()) -e $(_cmd)`) end -include("../examples/rc_model.jl") +include("common/rc_model.jl") +@unpack capacitor = rc_model io = IOBuffer() -write(io, rc_model) +write(io, expand_connections(rc_model)) str = String(take!(io)) + sys = include_string(@__MODULE__, str) -@test sys == flatten(rc_model) # this actually kind of works, but the variables would have different identities. +@test sys == expand_connections(rc_model) # this actually kind of works, but the variables would have different identities. # check answer ss = structural_simplify(rc_model) From abd896e5f6c1f8ee1353df0eb5023b66b01be41f Mon Sep 17 00:00:00 2001 From: Benjamin Chung Date: Thu, 1 May 2025 16:00:22 -0700 Subject: [PATCH 1409/2176] Fix imperativeaffect namespacing for arrays in non-root components --- src/systems/imperative_affect.jl | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/systems/imperative_affect.jl b/src/systems/imperative_affect.jl index 4c9ff3d248..bea4ac65a5 100644 --- a/src/systems/imperative_affect.jl +++ b/src/systems/imperative_affect.jl @@ -101,10 +101,22 @@ end namespace_affects(af::ImperativeAffect, s) = namespace_affect(af, s) function namespace_affect(affect::ImperativeAffect, s) + rmn = [] + for modded in modified(affect) + if modded isa AbstractArray + res = [] + for m in modded + push!(res, renamespace(s, m)) + end + push!(rmn, res) + else + push!(rmn, renamespace(s, modded)) + end + end ImperativeAffect(func(affect), namespace_expr.(observed(affect), (s,)), observed_syms(affect), - renamespace.((s,), modified(affect)), + rmn, modified_syms(affect), context(affect), affect.skip_checks) From 1fa12640b6461870b7ff67316c8b78d9f4764441 Mon Sep 17 00:00:00 2001 From: Benjamin Chung Date: Thu, 1 May 2025 16:02:44 -0700 Subject: [PATCH 1410/2176] format --- src/systems/imperative_affect.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/systems/imperative_affect.jl b/src/systems/imperative_affect.jl index bea4ac65a5..b0742e70a7 100644 --- a/src/systems/imperative_affect.jl +++ b/src/systems/imperative_affect.jl @@ -109,7 +109,7 @@ function namespace_affect(affect::ImperativeAffect, s) push!(res, renamespace(s, m)) end push!(rmn, res) - else + else push!(rmn, renamespace(s, modded)) end end From a70c52512e5614cab114b9b46eb714e258a357e9 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 2 May 2025 12:09:42 +0530 Subject: [PATCH 1411/2176] build: bump MTKStdlib compat --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 2f48cc68fe..1de504cbae 100644 --- a/Project.toml +++ b/Project.toml @@ -123,7 +123,7 @@ Libdl = "1" LinearAlgebra = "1" Logging = "1" MLStyle = "0.4.17" -ModelingToolkitStandardLibrary = "2.19" +ModelingToolkitStandardLibrary = "2.20" Moshi = "0.3" NaNMath = "0.3, 1" NonlinearSolve = "4.3" From 20b6035d6070a83a8a05405fe5d66467f88c8a64 Mon Sep 17 00:00:00 2001 From: Andrew Leonard Date: Fri, 2 May 2025 10:03:37 -0400 Subject: [PATCH 1412/2176] tmp commit --- test/runtests.jl | 212 +++++++++++++++++++++++------------------------ 1 file changed, 106 insertions(+), 106 deletions(-) diff --git a/test/runtests.jl b/test/runtests.jl index 37c738eec9..d214e75f69 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -25,121 +25,121 @@ end @time begin if GROUP == "All" || GROUP == "InterfaceI" @testset "InterfaceI" begin - @safetestset "Linear Algebra Test" include("linalg.jl") - @safetestset "AbstractSystem Test" include("abstractsystem.jl") - @safetestset "Variable Scope Tests" include("variable_scope.jl") - @safetestset "Symbolic Parameters Test" include("symbolic_parameters.jl") - @safetestset "Parsing Test" include("variable_parsing.jl") - @safetestset "Simplify Test" include("simplify.jl") - @safetestset "Direct Usage Test" include("direct.jl") - @safetestset "System Linearity Test" include("linearity.jl") - @safetestset "Input Output Test" include("input_output_handling.jl") - @safetestset "Clock Test" include("clock.jl") - @safetestset "ODESystem Test" include("odesystem.jl") - @safetestset "Dynamic Quantities Test" include("dq_units.jl") - @safetestset "Unitful Quantities Test" include("units.jl") - @safetestset "Mass Matrix Test" include("mass_matrix.jl") - @safetestset "Reduction Test" include("reduction.jl") - @safetestset "Split Parameters Test" include("split_parameters.jl") - @safetestset "StaticArrays Test" include("static_arrays.jl") - @safetestset "Components Test" include("components.jl") - @safetestset "Model Parsing Test" include("model_parsing.jl") - @safetestset "Error Handling" include("error_handling.jl") - @safetestset "StructuralTransformations" include("structural_transformation/runtests.jl") + # @safetestset "Linear Algebra Test" include("linalg.jl") + # @safetestset "AbstractSystem Test" include("abstractsystem.jl") + # @safetestset "Variable Scope Tests" include("variable_scope.jl") + # @safetestset "Symbolic Parameters Test" include("symbolic_parameters.jl") + # @safetestset "Parsing Test" include("variable_parsing.jl") + # @safetestset "Simplify Test" include("simplify.jl") + # @safetestset "Direct Usage Test" include("direct.jl") + # @safetestset "System Linearity Test" include("linearity.jl") + # @safetestset "Input Output Test" include("input_output_handling.jl") + # @safetestset "Clock Test" include("clock.jl") + # @safetestset "ODESystem Test" include("odesystem.jl") + # @safetestset "Dynamic Quantities Test" include("dq_units.jl") + # @safetestset "Unitful Quantities Test" include("units.jl") + # @safetestset "Mass Matrix Test" include("mass_matrix.jl") + # @safetestset "Reduction Test" include("reduction.jl") + # @safetestset "Split Parameters Test" include("split_parameters.jl") + # @safetestset "StaticArrays Test" include("static_arrays.jl") + # @safetestset "Components Test" include("components.jl") + # @safetestset "Model Parsing Test" include("model_parsing.jl") + # @safetestset "Error Handling" include("error_handling.jl") + # @safetestset "StructuralTransformations" include("structural_transformation/runtests.jl") @safetestset "Basic transformations" include("basic_transformations.jl") - @safetestset "State Selection Test" include("state_selection.jl") - @safetestset "Symbolic Event Test" include("symbolic_events.jl") - @safetestset "Stream Connect Test" include("stream_connectors.jl") - @safetestset "Domain Connect Test" include("domain_connectors.jl") - @safetestset "Lowering Integration Test" include("lowering_solving.jl") - @safetestset "Dependency Graph Test" include("dep_graphs.jl") - @safetestset "Function Registration Test" include("function_registration.jl") - @safetestset "Precompiled Modules Test" include("precompile_test.jl") - @safetestset "DAE Jacobians Test" include("dae_jacobian.jl") - @safetestset "Jacobian Sparsity" include("jacobiansparsity.jl") - @safetestset "Modelingtoolkitize Test" include("modelingtoolkitize.jl") - @safetestset "FuncAffect Test" include("funcaffect.jl") - @safetestset "Constants Test" include("constants.jl") - @safetestset "Parameter Dependency Test" include("parameter_dependencies.jl") - @safetestset "Equation Type Accessors Test" include("equation_type_accessors.jl") - @safetestset "System Accessor Functions Test" include("accessor_functions.jl") - @safetestset "Equations with complex values" include("complex.jl") + # @safetestset "State Selection Test" include("state_selection.jl") + # @safetestset "Symbolic Event Test" include("symbolic_events.jl") + # @safetestset "Stream Connect Test" include("stream_connectors.jl") + # @safetestset "Domain Connect Test" include("domain_connectors.jl") + # @safetestset "Lowering Integration Test" include("lowering_solving.jl") + # @safetestset "Dependency Graph Test" include("dep_graphs.jl") + # @safetestset "Function Registration Test" include("function_registration.jl") + # @safetestset "Precompiled Modules Test" include("precompile_test.jl") + # @safetestset "DAE Jacobians Test" include("dae_jacobian.jl") + # @safetestset "Jacobian Sparsity" include("jacobiansparsity.jl") + # @safetestset "Modelingtoolkitize Test" include("modelingtoolkitize.jl") + # @safetestset "FuncAffect Test" include("funcaffect.jl") + # @safetestset "Constants Test" include("constants.jl") + # @safetestset "Parameter Dependency Test" include("parameter_dependencies.jl") + # @safetestset "Equation Type Accessors Test" include("equation_type_accessors.jl") + # @safetestset "System Accessor Functions Test" include("accessor_functions.jl") + # @safetestset "Equations with complex values" include("complex.jl") end end - if GROUP == "All" || GROUP == "Initialization" - @safetestset "Guess Propagation" include("guess_propagation.jl") - @safetestset "Hierarchical Initialization Equations" include("hierarchical_initialization_eqs.jl") - @safetestset "InitializationSystem Test" include("initializationsystem.jl") - @safetestset "Initial Values Test" include("initial_values.jl") - end + # if GROUP == "All" || GROUP == "Initialization" + # @safetestset "Guess Propagation" include("guess_propagation.jl") + # @safetestset "Hierarchical Initialization Equations" include("hierarchical_initialization_eqs.jl") + # @safetestset "InitializationSystem Test" include("initializationsystem.jl") + # @safetestset "Initial Values Test" include("initial_values.jl") + # end - if GROUP == "All" || GROUP == "InterfaceII" - @testset "InterfaceII" begin - @safetestset "Code Generation Test" include("code_generation.jl") - @safetestset "IndexCache Test" include("index_cache.jl") - @safetestset "Variable Utils Test" include("variable_utils.jl") - @safetestset "Variable Metadata Test" include("test_variable_metadata.jl") - @safetestset "OptimizationSystem Test" include("optimizationsystem.jl") - @safetestset "Discrete System" include("discrete_system.jl") - @safetestset "Implicit Discrete System" include("implicit_discrete_system.jl") - @safetestset "SteadyStateSystem Test" include("steadystatesystems.jl") - @safetestset "SDESystem Test" include("sdesystem.jl") - @safetestset "DDESystem Test" include("dde.jl") - @safetestset "NonlinearSystem Test" include("nonlinearsystem.jl") - @safetestset "SCCNonlinearProblem Test" include("scc_nonlinear_problem.jl") - @safetestset "PDE Construction Test" include("pdesystem.jl") - @safetestset "JumpSystem Test" include("jumpsystem.jl") - @safetestset "Optimal Control + Constraints Tests" include("optimal_control.jl") - @safetestset "print_tree" include("print_tree.jl") - @safetestset "Constraints Test" include("constraints.jl") - @safetestset "IfLifting Test" include("if_lifting.jl") - @safetestset "Analysis Points Test" include("analysis_points.jl") - @safetestset "Causal Variables Connection Test" include("causal_variables_connection.jl") - @safetestset "Debugging Test" include("debugging.jl") - @safetestset "Namespacing test" include("namespacing.jl") - @safetestset "Subsystem replacement" include("substitute_component.jl") - end - end + # if GROUP == "All" || GROUP == "InterfaceII" + # @testset "InterfaceII" begin + # @safetestset "Code Generation Test" include("code_generation.jl") + # @safetestset "IndexCache Test" include("index_cache.jl") + # @safetestset "Variable Utils Test" include("variable_utils.jl") + # @safetestset "Variable Metadata Test" include("test_variable_metadata.jl") + # @safetestset "OptimizationSystem Test" include("optimizationsystem.jl") + # @safetestset "Discrete System" include("discrete_system.jl") + # @safetestset "Implicit Discrete System" include("implicit_discrete_system.jl") + # @safetestset "SteadyStateSystem Test" include("steadystatesystems.jl") + # @safetestset "SDESystem Test" include("sdesystem.jl") + # @safetestset "DDESystem Test" include("dde.jl") + # @safetestset "NonlinearSystem Test" include("nonlinearsystem.jl") + # @safetestset "SCCNonlinearProblem Test" include("scc_nonlinear_problem.jl") + # @safetestset "PDE Construction Test" include("pdesystem.jl") + # @safetestset "JumpSystem Test" include("jumpsystem.jl") + # @safetestset "Optimal Control + Constraints Tests" include("optimal_control.jl") + # @safetestset "print_tree" include("print_tree.jl") + # @safetestset "Constraints Test" include("constraints.jl") + # @safetestset "IfLifting Test" include("if_lifting.jl") + # @safetestset "Analysis Points Test" include("analysis_points.jl") + # @safetestset "Causal Variables Connection Test" include("causal_variables_connection.jl") + # @safetestset "Debugging Test" include("debugging.jl") + # @safetestset "Namespacing test" include("namespacing.jl") + # @safetestset "Subsystem replacement" include("substitute_component.jl") + # end + # end - if GROUP == "All" || GROUP == "SymbolicIndexingInterface" - @safetestset "SymbolicIndexingInterface test" include("symbolic_indexing_interface.jl") - @safetestset "SciML Problem Input Test" include("sciml_problem_inputs.jl") - @safetestset "MTKParameters Test" include("mtkparameters.jl") - end + # if GROUP == "All" || GROUP == "SymbolicIndexingInterface" + # @safetestset "SymbolicIndexingInterface test" include("symbolic_indexing_interface.jl") + # @safetestset "SciML Problem Input Test" include("sciml_problem_inputs.jl") + # @safetestset "MTKParameters Test" include("mtkparameters.jl") + # end - if GROUP == "All" || GROUP == "Extended" - @safetestset "Test Big System Usage" include("bigsystem.jl") - println("C compilation test requires gcc available in the path!") - @safetestset "C Compilation Test" include("ccompile.jl") - @testset "Distributed Test" include("distributed.jl") - @testset "Serialization" include("serialization.jl") - end + # if GROUP == "All" || GROUP == "Extended" + # @safetestset "Test Big System Usage" include("bigsystem.jl") + # println("C compilation test requires gcc available in the path!") + # @safetestset "C Compilation Test" include("ccompile.jl") + # @testset "Distributed Test" include("distributed.jl") + # @testset "Serialization" include("serialization.jl") + # end - if GROUP == "All" || GROUP == "RegressionI" - @safetestset "Latexify recipes Test" include("latexify.jl") - end + # if GROUP == "All" || GROUP == "RegressionI" + # @safetestset "Latexify recipes Test" include("latexify.jl") + # end - if GROUP == "All" || GROUP == "Downstream" - activate_downstream_env() - @safetestset "Linearization Tests" include("downstream/linearize.jl") - @safetestset "Linearization Dummy Derivative Tests" include("downstream/linearization_dd.jl") - @safetestset "Inverse Models Test" include("downstream/inversemodel.jl") - @safetestset "Analysis Points Test" include("downstream/analysis_points.jl") - @safetestset "Analysis Points Test" include("downstream/test_disturbance_model.jl") - end + # if GROUP == "All" || GROUP == "Downstream" + # activate_downstream_env() + # @safetestset "Linearization Tests" include("downstream/linearize.jl") + # @safetestset "Linearization Dummy Derivative Tests" include("downstream/linearization_dd.jl") + # @safetestset "Inverse Models Test" include("downstream/inversemodel.jl") + # @safetestset "Analysis Points Test" include("downstream/analysis_points.jl") + # @safetestset "Analysis Points Test" include("downstream/test_disturbance_model.jl") + # end - if GROUP == "All" || GROUP == "FMI" - activate_fmi_env() - @safetestset "FMI Extension Test" include("fmi/fmi.jl") - end + # if GROUP == "All" || GROUP == "FMI" + # activate_fmi_env() + # @safetestset "FMI Extension Test" include("fmi/fmi.jl") + # end - if GROUP == "All" || GROUP == "Extensions" - activate_extensions_env() - @safetestset "HomotopyContinuation Extension Test" include("extensions/homotopy_continuation.jl") - @safetestset "Auto Differentiation Test" include("extensions/ad.jl") - @safetestset "LabelledArrays Test" include("labelledarrays.jl") - @safetestset "BifurcationKit Extension Test" include("extensions/bifurcationkit.jl") - @safetestset "InfiniteOpt Extension Test" include("extensions/test_infiniteopt.jl") - end + # if GROUP == "All" || GROUP == "Extensions" + # activate_extensions_env() + # @safetestset "HomotopyContinuation Extension Test" include("extensions/homotopy_continuation.jl") + # @safetestset "Auto Differentiation Test" include("extensions/ad.jl") + # @safetestset "LabelledArrays Test" include("labelledarrays.jl") + # @safetestset "BifurcationKit Extension Test" include("extensions/bifurcationkit.jl") + # @safetestset "InfiniteOpt Extension Test" include("extensions/test_infiniteopt.jl") + # end end From 89a375a0c42596ff4022bf314ddc8d26b8948889 Mon Sep 17 00:00:00 2001 From: Andrew Leonard Date: Fri, 2 May 2025 11:09:29 -0400 Subject: [PATCH 1413/2176] units for change of variables --- src/systems/diffeqs/basic_transformations.jl | 19 +- test/basic_transformations.jl | 18 +- test/runtests.jl | 212 +++++++++---------- 3 files changed, 139 insertions(+), 110 deletions(-) diff --git a/src/systems/diffeqs/basic_transformations.jl b/src/systems/diffeqs/basic_transformations.jl index a08c83ffb6..d41e948d64 100644 --- a/src/systems/diffeqs/basic_transformations.jl +++ b/src/systems/diffeqs/basic_transformations.jl @@ -126,10 +126,23 @@ function change_independent_variable( # Set up intermediate and final variables for the transformation iv1name = nameof(iv1) # e.g. :t iv2name = nameof(operation(iv2_of_iv1)) # e.g. :u - iv2, = @independent_variables $iv2name # e.g. u - iv1_of_iv2, = GlobalScope.(@variables $iv1name(iv2)) # inverse, e.g. t(u), global because iv1 has no namespacing in sys D1 = Differential(iv1) # e.g. d/d(t) - div2_of_iv1 = GlobalScope(default_toterm(D1(iv2_of_iv1))) # e.g. uˍt(t) + + # construct new terms, e.g: + # iv2 -> u + # iv1_of_iv2 -> t(u), (inverse, global because iv1 has no namespacing in sys) + # div2_of_iv1 -> uˍt(t) + iv2_unit = getmetadata(iv2_of_iv1, VariableUnit, nothing) + if isnothing(iv2_unit) + iv2, = @independent_variables $iv2name + iv1_of_iv2, = GlobalScope.(@variables $iv1name(iv2)) + div2_of_iv1 = GlobalScope(default_toterm(D1(iv2_of_iv1))) + else + iv2, = @independent_variables $iv2name [unit = iv2_unit] + iv1_of_iv2, = GlobalScope.(@variables $iv1name(iv2) [unit = get_unit(iv1)]) + div2_of_iv1 = GlobalScope(diff2term_with_unit(D1(iv2_of_iv1), iv1)) + end + div2_of_iv2 = substitute(div2_of_iv1, iv1 => iv2) # e.g. uˍt(u) div2_of_iv2_of_iv1 = substitute(div2_of_iv2, iv2 => iv2_of_iv1) # e.g. uˍt(u(t)) diff --git a/test/basic_transformations.jl b/test/basic_transformations.jl index 544a89cd29..183feca6d2 100644 --- a/test/basic_transformations.jl +++ b/test/basic_transformations.jl @@ -1,4 +1,4 @@ -using ModelingToolkit, OrdinaryDiffEq, DataInterpolations, Test +using ModelingToolkit, OrdinaryDiffEq, DataInterpolations, DynamicQuantities, Test @independent_variables t D = Differential(t) @@ -215,3 +215,19 @@ end M = ODESystem([D(x(t)) ~ x(t - 1)], t; name = :M) @test_throws "DDE" change_independent_variable(M, x(t)) end + +@testset "Change independent variable w/ units (free fall with 2nd order horizontal equation)" begin + @independent_variables t_units [unit = u"s"] + D_units = Differential(t_units) + @variables x(t_units) [unit = u"m"] y(t_units) [unit = u"m"] + @parameters g = 9.81 [unit = u"m * s^-2"] # gravitational acceleration + Mt = ODESystem([D_units(D_units(y)) ~ -g, D_units(D_units(x)) ~ 0], t_units; name = :M) # gives (x, y) as function of t, ... + Mx = change_independent_variable(Mt, x; add_old_diff = true) # ... but we want y as a function of x + Mx = structural_simplify(Mx; allow_symbolic = true) + Dx = Differential(Mx.x) + u0 = [Mx.y => 0.0, Dx(Mx.y) => 1.0, Mx.t_units => 0.0, Mx.xˍt_units => 10.0] + prob = ODEProblem(Mx, u0, (0.0, 20.0), []) # 1 = dy/dx = (dy/dt)/(dx/dt) means equal initial horizontal and vertical velocities + sol = solve(prob, Tsit5(); reltol = 1e-5) + # compare to analytical solution (x(t) = v*t, y(t) = v*t - g*t^2/2) + @test all(isapprox.(sol[Mx.y], sol[Mx.x - g * (Mx.t_units)^2 / 2]; atol = 1e-10)) +end diff --git a/test/runtests.jl b/test/runtests.jl index d214e75f69..37c738eec9 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -25,121 +25,121 @@ end @time begin if GROUP == "All" || GROUP == "InterfaceI" @testset "InterfaceI" begin - # @safetestset "Linear Algebra Test" include("linalg.jl") - # @safetestset "AbstractSystem Test" include("abstractsystem.jl") - # @safetestset "Variable Scope Tests" include("variable_scope.jl") - # @safetestset "Symbolic Parameters Test" include("symbolic_parameters.jl") - # @safetestset "Parsing Test" include("variable_parsing.jl") - # @safetestset "Simplify Test" include("simplify.jl") - # @safetestset "Direct Usage Test" include("direct.jl") - # @safetestset "System Linearity Test" include("linearity.jl") - # @safetestset "Input Output Test" include("input_output_handling.jl") - # @safetestset "Clock Test" include("clock.jl") - # @safetestset "ODESystem Test" include("odesystem.jl") - # @safetestset "Dynamic Quantities Test" include("dq_units.jl") - # @safetestset "Unitful Quantities Test" include("units.jl") - # @safetestset "Mass Matrix Test" include("mass_matrix.jl") - # @safetestset "Reduction Test" include("reduction.jl") - # @safetestset "Split Parameters Test" include("split_parameters.jl") - # @safetestset "StaticArrays Test" include("static_arrays.jl") - # @safetestset "Components Test" include("components.jl") - # @safetestset "Model Parsing Test" include("model_parsing.jl") - # @safetestset "Error Handling" include("error_handling.jl") - # @safetestset "StructuralTransformations" include("structural_transformation/runtests.jl") + @safetestset "Linear Algebra Test" include("linalg.jl") + @safetestset "AbstractSystem Test" include("abstractsystem.jl") + @safetestset "Variable Scope Tests" include("variable_scope.jl") + @safetestset "Symbolic Parameters Test" include("symbolic_parameters.jl") + @safetestset "Parsing Test" include("variable_parsing.jl") + @safetestset "Simplify Test" include("simplify.jl") + @safetestset "Direct Usage Test" include("direct.jl") + @safetestset "System Linearity Test" include("linearity.jl") + @safetestset "Input Output Test" include("input_output_handling.jl") + @safetestset "Clock Test" include("clock.jl") + @safetestset "ODESystem Test" include("odesystem.jl") + @safetestset "Dynamic Quantities Test" include("dq_units.jl") + @safetestset "Unitful Quantities Test" include("units.jl") + @safetestset "Mass Matrix Test" include("mass_matrix.jl") + @safetestset "Reduction Test" include("reduction.jl") + @safetestset "Split Parameters Test" include("split_parameters.jl") + @safetestset "StaticArrays Test" include("static_arrays.jl") + @safetestset "Components Test" include("components.jl") + @safetestset "Model Parsing Test" include("model_parsing.jl") + @safetestset "Error Handling" include("error_handling.jl") + @safetestset "StructuralTransformations" include("structural_transformation/runtests.jl") @safetestset "Basic transformations" include("basic_transformations.jl") - # @safetestset "State Selection Test" include("state_selection.jl") - # @safetestset "Symbolic Event Test" include("symbolic_events.jl") - # @safetestset "Stream Connect Test" include("stream_connectors.jl") - # @safetestset "Domain Connect Test" include("domain_connectors.jl") - # @safetestset "Lowering Integration Test" include("lowering_solving.jl") - # @safetestset "Dependency Graph Test" include("dep_graphs.jl") - # @safetestset "Function Registration Test" include("function_registration.jl") - # @safetestset "Precompiled Modules Test" include("precompile_test.jl") - # @safetestset "DAE Jacobians Test" include("dae_jacobian.jl") - # @safetestset "Jacobian Sparsity" include("jacobiansparsity.jl") - # @safetestset "Modelingtoolkitize Test" include("modelingtoolkitize.jl") - # @safetestset "FuncAffect Test" include("funcaffect.jl") - # @safetestset "Constants Test" include("constants.jl") - # @safetestset "Parameter Dependency Test" include("parameter_dependencies.jl") - # @safetestset "Equation Type Accessors Test" include("equation_type_accessors.jl") - # @safetestset "System Accessor Functions Test" include("accessor_functions.jl") - # @safetestset "Equations with complex values" include("complex.jl") + @safetestset "State Selection Test" include("state_selection.jl") + @safetestset "Symbolic Event Test" include("symbolic_events.jl") + @safetestset "Stream Connect Test" include("stream_connectors.jl") + @safetestset "Domain Connect Test" include("domain_connectors.jl") + @safetestset "Lowering Integration Test" include("lowering_solving.jl") + @safetestset "Dependency Graph Test" include("dep_graphs.jl") + @safetestset "Function Registration Test" include("function_registration.jl") + @safetestset "Precompiled Modules Test" include("precompile_test.jl") + @safetestset "DAE Jacobians Test" include("dae_jacobian.jl") + @safetestset "Jacobian Sparsity" include("jacobiansparsity.jl") + @safetestset "Modelingtoolkitize Test" include("modelingtoolkitize.jl") + @safetestset "FuncAffect Test" include("funcaffect.jl") + @safetestset "Constants Test" include("constants.jl") + @safetestset "Parameter Dependency Test" include("parameter_dependencies.jl") + @safetestset "Equation Type Accessors Test" include("equation_type_accessors.jl") + @safetestset "System Accessor Functions Test" include("accessor_functions.jl") + @safetestset "Equations with complex values" include("complex.jl") end end - # if GROUP == "All" || GROUP == "Initialization" - # @safetestset "Guess Propagation" include("guess_propagation.jl") - # @safetestset "Hierarchical Initialization Equations" include("hierarchical_initialization_eqs.jl") - # @safetestset "InitializationSystem Test" include("initializationsystem.jl") - # @safetestset "Initial Values Test" include("initial_values.jl") - # end + if GROUP == "All" || GROUP == "Initialization" + @safetestset "Guess Propagation" include("guess_propagation.jl") + @safetestset "Hierarchical Initialization Equations" include("hierarchical_initialization_eqs.jl") + @safetestset "InitializationSystem Test" include("initializationsystem.jl") + @safetestset "Initial Values Test" include("initial_values.jl") + end - # if GROUP == "All" || GROUP == "InterfaceII" - # @testset "InterfaceII" begin - # @safetestset "Code Generation Test" include("code_generation.jl") - # @safetestset "IndexCache Test" include("index_cache.jl") - # @safetestset "Variable Utils Test" include("variable_utils.jl") - # @safetestset "Variable Metadata Test" include("test_variable_metadata.jl") - # @safetestset "OptimizationSystem Test" include("optimizationsystem.jl") - # @safetestset "Discrete System" include("discrete_system.jl") - # @safetestset "Implicit Discrete System" include("implicit_discrete_system.jl") - # @safetestset "SteadyStateSystem Test" include("steadystatesystems.jl") - # @safetestset "SDESystem Test" include("sdesystem.jl") - # @safetestset "DDESystem Test" include("dde.jl") - # @safetestset "NonlinearSystem Test" include("nonlinearsystem.jl") - # @safetestset "SCCNonlinearProblem Test" include("scc_nonlinear_problem.jl") - # @safetestset "PDE Construction Test" include("pdesystem.jl") - # @safetestset "JumpSystem Test" include("jumpsystem.jl") - # @safetestset "Optimal Control + Constraints Tests" include("optimal_control.jl") - # @safetestset "print_tree" include("print_tree.jl") - # @safetestset "Constraints Test" include("constraints.jl") - # @safetestset "IfLifting Test" include("if_lifting.jl") - # @safetestset "Analysis Points Test" include("analysis_points.jl") - # @safetestset "Causal Variables Connection Test" include("causal_variables_connection.jl") - # @safetestset "Debugging Test" include("debugging.jl") - # @safetestset "Namespacing test" include("namespacing.jl") - # @safetestset "Subsystem replacement" include("substitute_component.jl") - # end - # end + if GROUP == "All" || GROUP == "InterfaceII" + @testset "InterfaceII" begin + @safetestset "Code Generation Test" include("code_generation.jl") + @safetestset "IndexCache Test" include("index_cache.jl") + @safetestset "Variable Utils Test" include("variable_utils.jl") + @safetestset "Variable Metadata Test" include("test_variable_metadata.jl") + @safetestset "OptimizationSystem Test" include("optimizationsystem.jl") + @safetestset "Discrete System" include("discrete_system.jl") + @safetestset "Implicit Discrete System" include("implicit_discrete_system.jl") + @safetestset "SteadyStateSystem Test" include("steadystatesystems.jl") + @safetestset "SDESystem Test" include("sdesystem.jl") + @safetestset "DDESystem Test" include("dde.jl") + @safetestset "NonlinearSystem Test" include("nonlinearsystem.jl") + @safetestset "SCCNonlinearProblem Test" include("scc_nonlinear_problem.jl") + @safetestset "PDE Construction Test" include("pdesystem.jl") + @safetestset "JumpSystem Test" include("jumpsystem.jl") + @safetestset "Optimal Control + Constraints Tests" include("optimal_control.jl") + @safetestset "print_tree" include("print_tree.jl") + @safetestset "Constraints Test" include("constraints.jl") + @safetestset "IfLifting Test" include("if_lifting.jl") + @safetestset "Analysis Points Test" include("analysis_points.jl") + @safetestset "Causal Variables Connection Test" include("causal_variables_connection.jl") + @safetestset "Debugging Test" include("debugging.jl") + @safetestset "Namespacing test" include("namespacing.jl") + @safetestset "Subsystem replacement" include("substitute_component.jl") + end + end - # if GROUP == "All" || GROUP == "SymbolicIndexingInterface" - # @safetestset "SymbolicIndexingInterface test" include("symbolic_indexing_interface.jl") - # @safetestset "SciML Problem Input Test" include("sciml_problem_inputs.jl") - # @safetestset "MTKParameters Test" include("mtkparameters.jl") - # end + if GROUP == "All" || GROUP == "SymbolicIndexingInterface" + @safetestset "SymbolicIndexingInterface test" include("symbolic_indexing_interface.jl") + @safetestset "SciML Problem Input Test" include("sciml_problem_inputs.jl") + @safetestset "MTKParameters Test" include("mtkparameters.jl") + end - # if GROUP == "All" || GROUP == "Extended" - # @safetestset "Test Big System Usage" include("bigsystem.jl") - # println("C compilation test requires gcc available in the path!") - # @safetestset "C Compilation Test" include("ccompile.jl") - # @testset "Distributed Test" include("distributed.jl") - # @testset "Serialization" include("serialization.jl") - # end + if GROUP == "All" || GROUP == "Extended" + @safetestset "Test Big System Usage" include("bigsystem.jl") + println("C compilation test requires gcc available in the path!") + @safetestset "C Compilation Test" include("ccompile.jl") + @testset "Distributed Test" include("distributed.jl") + @testset "Serialization" include("serialization.jl") + end - # if GROUP == "All" || GROUP == "RegressionI" - # @safetestset "Latexify recipes Test" include("latexify.jl") - # end + if GROUP == "All" || GROUP == "RegressionI" + @safetestset "Latexify recipes Test" include("latexify.jl") + end - # if GROUP == "All" || GROUP == "Downstream" - # activate_downstream_env() - # @safetestset "Linearization Tests" include("downstream/linearize.jl") - # @safetestset "Linearization Dummy Derivative Tests" include("downstream/linearization_dd.jl") - # @safetestset "Inverse Models Test" include("downstream/inversemodel.jl") - # @safetestset "Analysis Points Test" include("downstream/analysis_points.jl") - # @safetestset "Analysis Points Test" include("downstream/test_disturbance_model.jl") - # end + if GROUP == "All" || GROUP == "Downstream" + activate_downstream_env() + @safetestset "Linearization Tests" include("downstream/linearize.jl") + @safetestset "Linearization Dummy Derivative Tests" include("downstream/linearization_dd.jl") + @safetestset "Inverse Models Test" include("downstream/inversemodel.jl") + @safetestset "Analysis Points Test" include("downstream/analysis_points.jl") + @safetestset "Analysis Points Test" include("downstream/test_disturbance_model.jl") + end - # if GROUP == "All" || GROUP == "FMI" - # activate_fmi_env() - # @safetestset "FMI Extension Test" include("fmi/fmi.jl") - # end + if GROUP == "All" || GROUP == "FMI" + activate_fmi_env() + @safetestset "FMI Extension Test" include("fmi/fmi.jl") + end - # if GROUP == "All" || GROUP == "Extensions" - # activate_extensions_env() - # @safetestset "HomotopyContinuation Extension Test" include("extensions/homotopy_continuation.jl") - # @safetestset "Auto Differentiation Test" include("extensions/ad.jl") - # @safetestset "LabelledArrays Test" include("labelledarrays.jl") - # @safetestset "BifurcationKit Extension Test" include("extensions/bifurcationkit.jl") - # @safetestset "InfiniteOpt Extension Test" include("extensions/test_infiniteopt.jl") - # end + if GROUP == "All" || GROUP == "Extensions" + activate_extensions_env() + @safetestset "HomotopyContinuation Extension Test" include("extensions/homotopy_continuation.jl") + @safetestset "Auto Differentiation Test" include("extensions/ad.jl") + @safetestset "LabelledArrays Test" include("labelledarrays.jl") + @safetestset "BifurcationKit Extension Test" include("extensions/bifurcationkit.jl") + @safetestset "InfiniteOpt Extension Test" include("extensions/test_infiniteopt.jl") + end end From a30df651129ec974972b4698b3b06092c0f98a7e Mon Sep 17 00:00:00 2001 From: vyudu Date: Fri, 2 May 2025 17:31:13 -0400 Subject: [PATCH 1414/2176] rename At to EvalAt --- src/ModelingToolkit.jl | 2 +- src/variables.jl | 18 +++++++++--------- test/model_parsing.jl | 8 ++++---- test/variable_utils.jl | 26 +++++++++++++------------- 4 files changed, 27 insertions(+), 27 deletions(-) diff --git a/src/ModelingToolkit.jl b/src/ModelingToolkit.jl index bfe7b489b6..992690cfe0 100644 --- a/src/ModelingToolkit.jl +++ b/src/ModelingToolkit.jl @@ -142,7 +142,7 @@ function var_derivative_graph! end include("bipartite_graph.jl") using .BipartiteGraphs -export At +export EvalAt include("variables.jl") include("parameters.jl") include("independent_variables.jl") diff --git a/src/variables.jl b/src/variables.jl index e5d8961225..83e72cea35 100644 --- a/src/variables.jl +++ b/src/variables.jl @@ -616,11 +616,11 @@ getshift(x::Symbolic) = Symbolics.getmetadata(x, VariableShift, 0) ################### ### Evaluate at ### ################### -struct At <: Symbolics.Operator +struct EvalAt <: Symbolics.Operator t::Union{Symbolic, Number} end -function (A::At)(x::Symbolic) +function (A::EvalAt)(x::Symbolic) if symbolic_type(x) == NotSymbolic() || !iscall(x) if x isa Symbolics.CallWithMetadata return x(A.t) @@ -637,18 +637,18 @@ function (A::At)(x::Symbolic) A(x) else length(arguments(x)) !== 1 && - error("Variable $x has too many arguments. At can only be applied to one-argument variables.") + error("Variable $x has too many arguments. EvalAt can only be applied to one-argument variables.") (symbolic_type(only(arguments(x))) !== ScalarSymbolic()) && return x return operation(x)(A.t) end end -function (A::At)(x::Union{Num, Symbolics.Arr}) +function (A::EvalAt)(x::Union{Num, Symbolics.Arr}) wrap(A(unwrap(x))) end -SymbolicUtils.isbinop(::At) = false +SymbolicUtils.isbinop(::EvalAt) = false -Base.nameof(::At) = :At -Base.show(io::IO, A::At) = print(io, "At(", A.t, ")") -Base.:(==)(A1::At, A2::At) = isequal(A1.t, A2.t) -Base.hash(A::At, u::UInt) = hash(A.t, u) +Base.nameof(::EvalAt) = :EvalAt +Base.show(io::IO, A::EvalAt) = print(io, "EvalAt(", A.t, ")") +Base.:(==)(A1::EvalAt, A2::EvalAt) = isequal(A1.t, A2.t) +Base.hash(A::EvalAt, u::UInt) = hash(A.t, u) diff --git a/test/model_parsing.jl b/test/model_parsing.jl index 05380269e5..fe2bcbfca6 100644 --- a/test/model_parsing.jl +++ b/test/model_parsing.jl @@ -1037,12 +1037,12 @@ end x ~ y end @constraints begin - At(0.3)(x) ~ 3 + EvalAt(0.3)(x) ~ 3 y ≲ 4 end @costs begin x + y - At(1)(y)^2 + EvalAt(1)(y)^2 end @consolidate f(u) = u[1]^2 + log(u[2]) end @@ -1053,8 +1053,8 @@ end costs = ModelingToolkit.get_costs(ex) constrs = ModelingToolkit.get_constraints(ModelingToolkit.get_constraintsystem(ex)) @test isequal(costs[1], ex.x + ex.y) - @test isequal(costs[2], At(1)(ex.y)^2) - @test isequal(constrs[1], -3 + At(0.3)(ex.x) ~ 0) + @test isequal(costs[2], EvalAt(1)(ex.y)^2) + @test isequal(constrs[1], -3 + EvalAt(0.3)(ex.x) ~ 0) @test isequal(constrs[2], -4 + ex.y ≲ 0) @test ModelingToolkit.get_consolidate(ex)([1, 2]) ≈ 1 + log(2) end diff --git a/test/variable_utils.jl b/test/variable_utils.jl index f447cfeb8d..1dc45e11ef 100644 --- a/test/variable_utils.jl +++ b/test/variable_utils.jl @@ -164,23 +164,23 @@ end @variables x(t) v(..) w(t)[1:3] @parameters y z(u, t) r[1:3] - @test At(1)(x) isa Num - @test isequal(At(1)(y), y) - @test_throws ErrorException At(1)(z) - @test isequal(At(1)(v), v(1)) - @test isequal(At(1)(v(t)), v(1)) - @test isequal(At(1)(v(2)), v(2)) + @test EvalAt(1)(x) isa Num + @test isequal(EvalAt(1)(y), y) + @test_throws ErrorException EvalAt(1)(z) + @test isequal(EvalAt(1)(v), v(1)) + @test isequal(EvalAt(1)(v(t)), v(1)) + @test isequal(EvalAt(1)(v(2)), v(2)) - arr = At(1)(w) - var = At(1)(w[1]) + arr = EvalAt(1)(w) + var = EvalAt(1)(w[1]) @test arr isa Symbolics.Arr @test var isa Num - @test isequal(At(1)(r), r) - @test isequal(At(1)(r[2]), r[2]) + @test isequal(EvalAt(1)(r), r) + @test isequal(EvalAt(1)(r[2]), r[2]) _x = ModelingToolkit.unwrap(x) - @test At(1)(_x) isa Symbolics.BasicSymbolic - @test only(arguments(At(1)(_x))) == 1 - @test At(1)(D(x)) isa Num + @test EvalAt(1)(_x) isa Symbolics.BasicSymbolic + @test only(arguments(EvalAt(1)(_x))) == 1 + @test EvalAt(1)(D(x)) isa Num end From f8f3ce9a95daa89230b1db88300cafe800e4d5e7 Mon Sep 17 00:00:00 2001 From: vyudu Date: Tue, 1 Apr 2025 11:22:13 -0400 Subject: [PATCH 1415/2176] init: JuMPProblem --- ext/MTKCASADIExt.jl | 11 +++++++++++ ext/MTKJuMPExt.jl | 19 +++++++++++++++++++ 2 files changed, 30 insertions(+) create mode 100644 ext/MTKCASADIExt.jl create mode 100644 ext/MTKJuMPExt.jl diff --git a/ext/MTKCASADIExt.jl b/ext/MTKCASADIExt.jl new file mode 100644 index 0000000000..b41ece6eb7 --- /dev/null +++ b/ext/MTKCASADIExt.jl @@ -0,0 +1,11 @@ +""" +an ODESystem with constraints to a JuMPProblem for optimal control solving. +""" +function CASADIProblem(sys::ODESystem) + +end + + +function CASADIProblem(prob::ODEProblem) + +end diff --git a/ext/MTKJuMPExt.jl b/ext/MTKJuMPExt.jl new file mode 100644 index 0000000000..b8e23a0f12 --- /dev/null +++ b/ext/MTKJuMPExt.jl @@ -0,0 +1,19 @@ +using JuMP, InfiniteOpt + +""" +Convert an ODESystem with constraints to a JuMPProblem for optimal control solving. +""" +function JuMPProblem(sys::ODESystem, u0, tspan, p; dt = error("dt must be provided for JuMPProblem.")) + steps = tspan[1]:dt:tspan[2] + nsteps = length(steps) + cost = sys.cost + coalesce = sys.coalesce + + @variables + @variables + @variables +end + +function symbolic_to_jump_constraint(cons::Union{Num, BasicSymbolic}) + +end From 89d2c0e9f3e1dea89a75ae85c1a9a79266ba4a1f Mon Sep 17 00:00:00 2001 From: vyudu Date: Thu, 3 Apr 2025 18:25:31 -0400 Subject: [PATCH 1416/2176] up --- ext/MTKJuMPExt.jl | 37 ++++++++++++++++++++++++++++--------- 1 file changed, 28 insertions(+), 9 deletions(-) diff --git a/ext/MTKJuMPExt.jl b/ext/MTKJuMPExt.jl index b8e23a0f12..c90a866aaa 100644 --- a/ext/MTKJuMPExt.jl +++ b/ext/MTKJuMPExt.jl @@ -4,16 +4,35 @@ using JuMP, InfiniteOpt Convert an ODESystem with constraints to a JuMPProblem for optimal control solving. """ function JuMPProblem(sys::ODESystem, u0, tspan, p; dt = error("dt must be provided for JuMPProblem.")) - steps = tspan[1]:dt:tspan[2] - nsteps = length(steps) - cost = sys.cost - coalesce = sys.coalesce - - @variables - @variables - @variables + ts = tspan[1] + te = tspan[2] + steps = ts:dt:te + costs = get_costs(sys) + consolidate = get_consolidate(sys) + ctrls = get_ctrls(sys) + states = unknowns(sys) + constraints = get_constraints(get_constraintsystem(sys)) + + model = Model() + + @infinite_parameter(model, t in [tspan[1],tspan[2]], num_supports = length(steps), derivative_method = OrthogonalCollocation(2)) + @variables(model, U[1:length(states)], Infinite(t), start = ts) + @variables(model, V[1:length(ctrls)], Infinite(t), start = ts) + @variables(model, K) + + jcost = generate_jump_cost_function(sys) + @objective + + constraints = generate_jump_constraints(constraints) + @constraints +end + +function generate_jump_cost_function(costs, tsteps) +end + +function generate_jump_constraints(constraints, jump_vars, jump_ps) end -function symbolic_to_jump_constraint(cons::Union{Num, BasicSymbolic}) +function t_to_tstep() end From 477af8b421c352bfea434916c8bdc0691662f9b9 Mon Sep 17 00:00:00 2001 From: vyudu Date: Mon, 7 Apr 2025 11:20:04 -0400 Subject: [PATCH 1417/2176] add solver getter --- ext/MTKJuMPExt.jl | 135 +++++++++++++++++++++++++++++++++++++++------- 1 file changed, 117 insertions(+), 18 deletions(-) diff --git a/ext/MTKJuMPExt.jl b/ext/MTKJuMPExt.jl index c90a866aaa..a0a913a59a 100644 --- a/ext/MTKJuMPExt.jl +++ b/ext/MTKJuMPExt.jl @@ -1,38 +1,137 @@ +module MTKJuMPControlExt +using ModelingToolkit using JuMP, InfiniteOpt +using DiffEqDevTools, DiffEqBase + +struct JuMPProblem{uType, tType, isinplace, P, F, K} <: + AbstractODEProblem{uType, tType, isinplace} + f::F + u0::uType + tspan + p + model + kwargs +end """ -Convert an ODESystem with constraints to a JuMPProblem for optimal control solving. + JuMPProblem(sys::ODESystem, u0, tspan, p; dt) + +Convert an ODESystem representing an optimal control system into a JuMP model +for solving using optimization. Must provide `dt` for determining the length +of the interpolation arrays. + +The optimization variables: +- a vector-of-vectors U representing the unknowns as an interpolation array +- a vector-of-vectors V representing the controls as an interpolation array + +The constraints are: +- The set of user constraints passed to the ODESystem via `constraints` +- The solver constraints that encode the time-stepping used by the solver """ -function JuMPProblem(sys::ODESystem, u0, tspan, p; dt = error("dt must be provided for JuMPProblem.")) +function JuMPProblem(sys::ODESystem, u0map, tspan, pmap; dt = error("dt must be provided for JuMPProblem."), solver = :Tsit5) ts = tspan[1] te = tspan[2] steps = ts:dt:te - costs = get_costs(sys) - consolidate = get_consolidate(sys) ctrls = get_ctrls(sys) states = unknowns(sys) - constraints = get_constraints(get_constraintsystem(sys)) - model = Model() + if !isnothing(constraintsys) + (length(constraints(constraintsys)) + length(u0map) > length(sts)) && + @warn "The BVProblem is overdetermined. The total number of conditions (# constraints + # fixed initial values given by u0map) exceeds the total number of states. The BVP solvers will default to doing a nonlinear least-squares optimization." + end - @infinite_parameter(model, t in [tspan[1],tspan[2]], num_supports = length(steps), derivative_method = OrthogonalCollocation(2)) - @variables(model, U[1:length(states)], Infinite(t), start = ts) - @variables(model, V[1:length(ctrls)], Infinite(t), start = ts) - @variables(model, K) + model = InfiniteModel() + @infinite_parameter(model, t in [ts, te], num_supports = length(steps), derivative_method = OrthogonalCollocation(2)) + @variable(model, U[1:length(states)], Infinite(t), start = ts) + @variable(model, V[1:length(ctrls)], Infinite(t), start = ts) + @variable(model, K) + + f, u0, p = process_SciMLProblem(ODEFunction{iip, specialize}, sys, _u0map, parammap; + t = tspan !== nothing ? tspan[1] : tspan, guesses, + check_length, warn_initialize_determined, eval_expression, eval_module, kwargs...) + + add_jump_cost_function!(model, sys) + add_user_constraints!(model, sys) + add_solve_constraints!(model) + + JuMPProblem{iip}(f, u0, tspan, p, model; kwargs...) +end + +function add_jump_cost_function!(model, sys) + jcosts = get_costs(sys) + consolidate = get_consolidate(sys) + iv = get_iv(sys) - jcost = generate_jump_cost_function(sys) - @objective + stidxmap = Dict([v => i for (i, v) in enumerate(get_unknowns(sys))]) + cidxmap = Dict([v => i for (i, v) in enumerate(get_ctrls(sys))]) - constraints = generate_jump_constraints(constraints) - @constraints + for st in get_unknowns(sys) + x = operation(st) + t = only(arguments(st)) + idx = stidxmap[x(iv)] + jcosts = Symbolics.substitute(costs, Dict(x(t) => model[:U][idx](t))) + end + + for ct in get_ctrls(sys) + p = operation(ct) + t = only(arguments(ct)) + idx = cidxmap[p(iv)] + jcosts = Symbolics.substitute(costs, Dict(p(t) => model[:V][idx](t))) + end + + @objective(model, Min, consolidate(jcosts)) end -function generate_jump_cost_function(costs, tsteps) +function add_user_constraints!(model, sys, u0map) + jconstraints = get_constraints(get_constraintsystem(sys)) + iv = get_iv(sys) + + stidxmap = Dict([v => i for (i, v) in enumerate(get_unknowns(sys))]) + cidxmap = Dict([v => i for (i, v) in enumerate(get_ctrls(sys))]) + + for st in get_unknowns(sys) + x = operation(st) + t = only(arguments(st)) + idx = stidxmap[x(iv)] + subval = isequal(t, iv) ? model[:U][idx] : model[:U][idx](t) + jconstraints = Symbolics.substitute(constraints, Dict(x(t) => subval)) + end + + for ct in get_ctrls(sys) + p = operation(ct) + t = only(arguments(ct)) + idx = cidxmap[p(iv)] + subval = isequal(t, iv) ? model[:V][idx] : model[:V][idx](t) + jconstraints = Symbolics.substitute(constraints, Dict(p(t) => subval)) + end + + for (i, cons) in enumerate(jconstraints) + if cons isa Equation + @constraint(model, user[i], cons.lhs - cons.rhs == 0) + elseif cons.relational_op === Symbolics.geq + @constraint(model, user[i], cons.lhs - cons.rhs ≥ 0) + else + @constraint(model, user[i], cons.lhs - cons.rhs ≤ 0) + end + end + + # Add initial constraints. end -function generate_jump_constraints(constraints, jump_vars, jump_ps) +function add_solve_constraints!(model, tsteps, solver) + tableau = fetch_tableau(solver) + + for (i, t) in collect(enumerate(tsteps)) + end end -function t_to_tstep() - +""" +Solve JuMPProblem. Takes in a symbol representing the solver. +""" +function solve(prob::JuMPProblem, solver_sym::Symbol) + model = prob.model + tableau_getter = Symbol(:construct, solver) + @eval tableau = $tableau_getter() + add_solve_constraints!(model, tableau) +end end From fecc1b6b367ce26c7c0c642aa29d1b140c07cd0b Mon Sep 17 00:00:00 2001 From: vyudu Date: Mon, 7 Apr 2025 11:23:13 -0400 Subject: [PATCH 1418/2176] add test/project --- Project.toml | 1 + test/extensions/JuMP.jl | 3 +++ 2 files changed, 4 insertions(+) create mode 100644 test/extensions/JuMP.jl diff --git a/Project.toml b/Project.toml index 1de504cbae..8ed109c610 100644 --- a/Project.toml +++ b/Project.toml @@ -76,6 +76,7 @@ MTKChainRulesCoreExt = "ChainRulesCore" MTKDeepDiffsExt = "DeepDiffs" MTKFMIExt = "FMI" MTKInfiniteOptExt = "InfiniteOpt" +MTKJuMP = "JuMP" MTKLabelledArraysExt = "LabelledArrays" [compat] diff --git a/test/extensions/JuMP.jl b/test/extensions/JuMP.jl new file mode 100644 index 0000000000..b6fc2b1fbc --- /dev/null +++ b/test/extensions/JuMP.jl @@ -0,0 +1,3 @@ +import ModelingToolkit as MTK +using JuMP, InfiniteOpt +using DiffEqDevTools, DiffEqBase From 35f5f0c0001e277c5a3cb6e53960f1352807bdbd Mon Sep 17 00:00:00 2001 From: vyudu Date: Mon, 7 Apr 2025 16:39:10 -0400 Subject: [PATCH 1419/2176] Implement solver tableau --- ext/MTKJuMPExt.jl | 71 +++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 59 insertions(+), 12 deletions(-) diff --git a/ext/MTKJuMPExt.jl b/ext/MTKJuMPExt.jl index a0a913a59a..83390461b2 100644 --- a/ext/MTKJuMPExt.jl +++ b/ext/MTKJuMPExt.jl @@ -3,18 +3,18 @@ using ModelingToolkit using JuMP, InfiniteOpt using DiffEqDevTools, DiffEqBase -struct JuMPProblem{uType, tType, isinplace, P, F, K} <: +struct JuMPControlProblem{uType, tType, isinplace, P, F, K} <: AbstractODEProblem{uType, tType, isinplace} f::F u0::uType - tspan + tspan::tType p model kwargs end """ - JuMPProblem(sys::ODESystem, u0, tspan, p; dt) + JuMPControlProblem(sys::ODESystem, u0, tspan, p; dt) Convert an ODESystem representing an optimal control system into a JuMP model for solving using optimization. Must provide `dt` for determining the length @@ -28,7 +28,7 @@ The constraints are: - The set of user constraints passed to the ODESystem via `constraints` - The solver constraints that encode the time-stepping used by the solver """ -function JuMPProblem(sys::ODESystem, u0map, tspan, pmap; dt = error("dt must be provided for JuMPProblem."), solver = :Tsit5) +function JuMPControlProblem(sys::ODESystem, u0map, tspan, pmap; dt = error("dt must be provided for JuMPProblem."), solver = :Tsit5) ts = tspan[1] te = tspan[2] steps = ts:dt:te @@ -54,7 +54,7 @@ function JuMPProblem(sys::ODESystem, u0map, tspan, pmap; dt = error("dt must be add_user_constraints!(model, sys) add_solve_constraints!(model) - JuMPProblem{iip}(f, u0, tspan, p, model; kwargs...) + JuMPControlProblem{iip}(f, u0, tspan, p, model; kwargs...) end function add_jump_cost_function!(model, sys) @@ -118,20 +118,67 @@ function add_user_constraints!(model, sys, u0map) # Add initial constraints. end -function add_solve_constraints!(model, tsteps, solver) - tableau = fetch_tableau(solver) - - for (i, t) in collect(enumerate(tsteps)) +function add_solve_constraints!(prob, talbeau, f, tsteps) + A = tableau.A + α = tableau.α + c = tableau.c + model = prob.model + p = prob.p + dt = step(tsteps) + + if is_explicit(tableau) + K = Any[] + for t in tsteps + for (i, h) in enumerate(c) + ΔU = sum([A[i, j] * K[j] for j in 1:i-1]) + Kₙ = f(U + ΔU*dt, p, t + h*dt) + push!(K, Kₙ) + end + @constraint(model, U(t) + dot(α, K) == U(t + dt)) + empty!(K) + end + else + @variable(model, K[1:length(a)], Infinite(t), start = tsteps[1]) + for t in tsteps + ΔUs = A * K(t) + for (i, h) in enumerate(c) + ΔU = ΔUs[i] + @constraint(model, K[i](t) == f(U + ΔU*dt, p, t + h*dt)) + end + @constraint(model, U(t) + dot(α, K(t)) == U(t + dt)) + end end end +is_explicit(tableau) = tableau isa DiffEqDevTools.ExplicitRKTableau + """ -Solve JuMPProblem. Takes in a symbol representing the solver. """ -function solve(prob::JuMPProblem, solver_sym::Symbol) +struct JuMPControlSolution + model + sol::ODESolution +end + +""" +Solve JuMPProblem. Takes in a symbol representing the solver. Acceptable solvers may be found at https://docs.sciml.ai/DiffEqDevDocs/stable/internals/tableaus/. +Note that the symbol may be different than the typical +name of the solver, e.g. :Tsitouras5 rather than Tsit5. +""" +function solve(prob::JuMPProblem, jump_solver, ode_solver::Symbol) model = prob.model + f = prob.f tableau_getter = Symbol(:construct, solver) @eval tableau = $tableau_getter() - add_solve_constraints!(model, tableau) + ts = prob.tspan[1]:dt:prob.tspan[2] + add_solve_constraints!(model, ts, tableau, f) + + set_optimizer(model, solver) + optimize!(model) + + if is_solved_and_feasible(model) + sol = DiffEqBase.build_solution(prob, ode_solver, ts, value(U)) + JuMPControlSolution(model, sol) + end end + end From 7ac9ad5bf1799a0a397215d2cfdd42dfc37fe87b Mon Sep 17 00:00:00 2001 From: vyudu Date: Mon, 7 Apr 2025 19:23:28 -0400 Subject: [PATCH 1420/2176] up --- ext/MTKJuMPExt.jl | 49 +++++++++++++++++++++++++++-------------------- 1 file changed, 28 insertions(+), 21 deletions(-) diff --git a/ext/MTKJuMPExt.jl b/ext/MTKJuMPExt.jl index 83390461b2..6cfcf432ef 100644 --- a/ext/MTKJuMPExt.jl +++ b/ext/MTKJuMPExt.jl @@ -8,9 +8,9 @@ struct JuMPControlProblem{uType, tType, isinplace, P, F, K} <: f::F u0::uType tspan::tType - p - model - kwargs + p::P + model::Model + kwargs::K end """ @@ -28,7 +28,7 @@ The constraints are: - The set of user constraints passed to the ODESystem via `constraints` - The solver constraints that encode the time-stepping used by the solver """ -function JuMPControlProblem(sys::ODESystem, u0map, tspan, pmap; dt = error("dt must be provided for JuMPProblem."), solver = :Tsit5) +function JuMPControlProblem(sys::ODESystem, u0map, tspan, pmap; dt = error("dt must be provided for JuMPControlProblem."), guesses, eval_expression, eval_module) ts = tspan[1] te = tspan[2] steps = ts:dt:te @@ -37,7 +37,7 @@ function JuMPControlProblem(sys::ODESystem, u0map, tspan, pmap; dt = error("dt m if !isnothing(constraintsys) (length(constraints(constraintsys)) + length(u0map) > length(sts)) && - @warn "The BVProblem is overdetermined. The total number of conditions (# constraints + # fixed initial values given by u0map) exceeds the total number of states. The BVP solvers will default to doing a nonlinear least-squares optimization." + @warn "The JuMPControlProblem is overdetermined. The total number of conditions (# constraints + # fixed initial values given by u0map) exceeds the total number of states. The solvers will default to doing a nonlinear least-squares optimization." end model = InfiniteModel() @@ -52,9 +52,12 @@ function JuMPControlProblem(sys::ODESystem, u0map, tspan, pmap; dt = error("dt m add_jump_cost_function!(model, sys) add_user_constraints!(model, sys) - add_solve_constraints!(model) - JuMPControlProblem{iip}(f, u0, tspan, p, model; kwargs...) + stidxmap = Dict([v => i for (i, v) in enumerate(sts)]) + u0_idxs = has_alg_eqs(sys) ? collect(1:length(sts)) : [stidxmap[k] for (k, v) in u0map] + add_initial_constraints!(model, u0, u0_idxs, tspan) + + JuMPControlProblem{iip}(f, u0, tspan, p, model, kwargs...) end function add_jump_cost_function!(model, sys) @@ -114,11 +117,16 @@ function add_user_constraints!(model, sys, u0map) @constraint(model, user[i], cons.lhs - cons.rhs ≤ 0) end end +end - # Add initial constraints. +function add_initial_constraints!(model, u0, u0_idxs, tspan) + ts = tspan[1] + @constraint(model, init_u0_idx[i in u0_idxs], U[i](ts) == u0[i]) end -function add_solve_constraints!(prob, talbeau, f, tsteps) +is_explicit(tableau) = tableau isa DiffEqDevTools.ExplicitRKTableau + +function add_solve_constraints!(prob, tableau, f, tsteps) A = tableau.A α = tableau.α c = tableau.c @@ -126,32 +134,31 @@ function add_solve_constraints!(prob, talbeau, f, tsteps) p = prob.p dt = step(tsteps) + U = model[:U] if is_explicit(tableau) K = Any[] - for t in tsteps + for τ in tsteps for (i, h) in enumerate(c) ΔU = sum([A[i, j] * K[j] for j in 1:i-1]) - Kₙ = f(U + ΔU*dt, p, t + h*dt) + Kₙ = f(U + ΔU*dt, p, τ + h*dt) push!(K, Kₙ) end - @constraint(model, U(t) + dot(α, K) == U(t + dt)) + @constraint(model, U(τ) + dot(α, K) == U(τ + dt)) empty!(K) end else @variable(model, K[1:length(a)], Infinite(t), start = tsteps[1]) - for t in tsteps - ΔUs = A * K(t) + for τ in tsteps + ΔUs = A * K(τ) for (i, h) in enumerate(c) ΔU = ΔUs[i] - @constraint(model, K[i](t) == f(U + ΔU*dt, p, t + h*dt)) + @constraint(model, K[i](τ) == f(U(τ) + ΔU*dt, p, τ + h*dt)) end - @constraint(model, U(t) + dot(α, K(t)) == U(t + dt)) + @constraint(model, U(τ) + dot(α, K(τ)) == U(τ + dt)) end end end -is_explicit(tableau) = tableau isa DiffEqDevTools.ExplicitRKTableau - """ """ struct JuMPControlSolution @@ -167,16 +174,16 @@ name of the solver, e.g. :Tsitouras5 rather than Tsit5. function solve(prob::JuMPProblem, jump_solver, ode_solver::Symbol) model = prob.model f = prob.f - tableau_getter = Symbol(:construct, solver) + tableau_getter = Symbol(:construct, ode_solver) @eval tableau = $tableau_getter() ts = prob.tspan[1]:dt:prob.tspan[2] add_solve_constraints!(model, ts, tableau, f) - set_optimizer(model, solver) + set_optimizer(model, jump_solver) optimize!(model) if is_solved_and_feasible(model) - sol = DiffEqBase.build_solution(prob, ode_solver, ts, value(U)) + sol = DiffEqBase.build_solution(prob, ode_solver, ts, value.(U)) JuMPControlSolution(model, sol) end end From 89e216a8327e94817e0aadf1d84d08929e9a5413 Mon Sep 17 00:00:00 2001 From: vyudu Date: Tue, 8 Apr 2025 00:40:15 -0400 Subject: [PATCH 1421/2176] feat: solver tableau debugging --- Project.toml | 6 +- ext/MTKJuMPControlExt.jl | 218 ++++++++++++++++++++++++++++++++ ext/MTKJuMPExt.jl | 191 ---------------------------- src/ModelingToolkit.jl | 3 + test/extensions/JuMP.jl | 3 - test/extensions/Project.toml | 4 + test/extensions/jump_control.jl | 57 +++++++++ 7 files changed, 287 insertions(+), 195 deletions(-) create mode 100644 ext/MTKJuMPControlExt.jl delete mode 100644 ext/MTKJuMPExt.jl delete mode 100644 test/extensions/JuMP.jl create mode 100644 test/extensions/jump_control.jl diff --git a/Project.toml b/Project.toml index 8ed109c610..3040619e1c 100644 --- a/Project.toml +++ b/Project.toml @@ -31,6 +31,7 @@ FunctionWrappers = "069b7b12-0de2-55c6-9aab-29f3d0a68a2e" FunctionWrappersWrappers = "77dc65aa-8811-40c2-897b-53d922fa7daf" Graphs = "86223c79-3864-5bf0-83f7-82e725a168b6" InteractiveUtils = "b77e0a4c-d291-57a0-90e8-8db25a27a240" +Ipopt = "b6b21f68-93f8-5de0-b562-5493be1d77c9" JuliaFormatter = "98e50ef6-434e-11e9-1051-2b60c6c9e899" JumpProcesses = "ccbc3e58-028d-4f4c-8cd5-9ae44345cda5" Latexify = "23fbe1c1-3f47-55db-b15f-69d7ec21a316" @@ -66,8 +67,10 @@ Unitful = "1986cc42-f94f-5a68-af5c-568840ba703d" BifurcationKit = "0f109fa4-8a5d-4b75-95aa-f515264e7665" ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4" DeepDiffs = "ab62b9b5-e342-54a8-a765-a90f495de1a6" +DiffEqDevTools = "f3b72e0c-5b89-59e1-b016-84e28bfd966d" FMI = "14a09403-18e3-468f-ad8a-74f8dda2d9ac" InfiniteOpt = "20393b10-9daf-11e9-18c9-8db751c92c57" +JuMP = "4076af6c-e467-56ae-b986-b466b2749572" LabelledArrays = "2ee39098-c373-598a-b85f-a56591580800" [extensions] @@ -76,7 +79,7 @@ MTKChainRulesCoreExt = "ChainRulesCore" MTKDeepDiffsExt = "DeepDiffs" MTKFMIExt = "FMI" MTKInfiniteOptExt = "InfiniteOpt" -MTKJuMP = "JuMP" +MTKJuMPControlExt = ["JuMP", "DiffEqDevTools"] MTKLabelledArraysExt = "LabelledArrays" [compat] @@ -98,6 +101,7 @@ DeepDiffs = "1" DelayDiffEq = "5.50" DiffEqBase = "6.170.1" DiffEqCallbacks = "2.16, 3, 4" +DiffEqDevTools = "2.48.0" DiffEqNoiseProcess = "5" DiffRules = "0.1, 1.0" DifferentiationInterface = "0.6.47" diff --git a/ext/MTKJuMPControlExt.jl b/ext/MTKJuMPControlExt.jl new file mode 100644 index 0000000000..1e788b05ec --- /dev/null +++ b/ext/MTKJuMPControlExt.jl @@ -0,0 +1,218 @@ +module MTKJuMPControlExt +using ModelingToolkit +using JuMP, InfiniteOpt +using DiffEqDevTools, DiffEqBase, SciMLBase +using LinearAlgebra +const MTK = ModelingToolkit + +struct JuMPControlProblem{uType, tType, P, F, K} + f::F + u0::uType + tspan::tType + p::P + model::InfiniteModel + kwargs::K + + function JuMPControlProblem(f, u0, tspan, p, model; kwargs...) + new{typeof(u0), typeof(tspan), typeof(p), typeof(f), typeof(kwargs)}(f, u0, tspan, p, model, kwargs) + end +end + +""" + JuMPControlProblem(sys::ODESystem, u0, tspan, p; dt) + +Convert an ODESystem representing an optimal control system into a JuMP model +for solving using optimization. Must provide `dt` for determining the length +of the interpolation arrays. + +The optimization variables: +- a vector-of-vectors U representing the unknowns as an interpolation array +- a vector-of-vectors V representing the controls as an interpolation array + +The constraints are: +- The set of user constraints passed to the ODESystem via `constraints` +- The solver constraints that encode the time-stepping used by the solver +""" +function MTK.JuMPControlProblem(sys::ODESystem, u0map, tspan, pmap; dt = error("dt must be provided for JuMPControlProblem."), kwargs...) + ts = tspan[1] + te = tspan[2] + steps = ts:dt:te + ctrls = controls(sys) + states = unknowns(sys) + constraintsys = MTK.get_constraintsystem(sys) + + if !isnothing(constraintsys) + (length(constraints(constraintsys)) + length(u0map) > length(sts)) && + @warn "The JuMPControlProblem is overdetermined. The total number of conditions (# constraints + # fixed initial values given by u0map) exceeds the total number of states. The solvers will default to doing a nonlinear least-squares optimization." + end + + f, u0, p = MTK.process_SciMLProblem(ODEFunction, sys, u0map, pmap; + t = tspan !== nothing ? tspan[1] : tspan, kwargs...) + + model = InfiniteModel() + @infinite_parameter(model, t in [ts, te], num_supports = length(steps), derivative_method = OrthogonalCollocation(2)) + @variable(model, U[1:length(states)], Infinite(t), start = ts) + @variable(model, V[1:length(ctrls)], Infinite(t), start = ts) + + add_jump_cost_function!(model, sys) + add_user_constraints!(model, sys) + + stidxmap = Dict([v => i for (i, v) in enumerate(states)]) + u0_idxs = has_alg_eqs(sys) ? collect(1:length(states)) : [stidxmap[k] for (k, v) in u0map] + add_initial_constraints!(model, u0, u0_idxs, tspan) + + JuMPControlProblem(f, u0, tspan, p, model, kwargs...) +end + +function add_jump_cost_function!(model, sys) + jcosts = MTK.get_costs(sys) + consolidate = MTK.get_consolidate(sys) + if isnothing(consolidate) + @objective(model, Min, 0) + return + end + iv = MTK.get_iv(sys) + + stidxmap = Dict([v => i for (i, v) in enumerate(unknowns(sys))]) + cidxmap = Dict([v => i for (i, v) in enumerate(controls(sys))]) + + for st in unknowns(sys) + x = operation(st) + t = only(arguments(st)) + idx = stidxmap[x(iv)] + subval = isequal(t, iv) ? model[:U][idx] : model[:U][idx](t) + jcosts = Symbolics.substitute(jcosts, Dict(x(t) => subval)) + end + + for ct in controls(sys) + p = operation(ct) + t = only(arguments(ct)) + idx = cidxmap[p(iv)] + subval = isequal(t, iv) ? model[:V][idx] : model[:V][idx](t) + jcosts = Symbolics.substitute(jcosts, Dict(x(t) => subval)) + end + + @objective(model, Min, consolidate(jcosts)) +end + +function add_user_constraints!(model, sys) + jconstraints = if !(csys = MTK.get_constraintsystem(sys) isa Nothing) + MTK.get_constraints(csys) + else + nothing + end + isnothing(jconstraints) && return nothing + + iv = MTK.get_iv(sys) + stidxmap = Dict([v => i for (i, v) in enumerate(unknowns(sys))]) + cidxmap = Dict([v => i for (i, v) in enumerate(controls(sys))]) + + for st in unknowns(sys) + x = operation(st) + t = only(arguments(st)) + idx = stidxmap[x(iv)] + subval = isequal(t, iv) ? model[:U][idx] : model[:U][idx](t) + jconstraints = Symbolics.substitute(jconstraints, Dict(x(t) => subval)) + end + + for ct in controls(sys) + p = operation(ct) + t = only(arguments(ct)) + idx = cidxmap[p(iv)] + subval = isequal(t, iv) ? model[:V][idx] : model[:V][idx](t) + jconstraints = Symbolics.substitute(jconstraints, Dict(p(t) => subval)) + end + + for (i, cons) in enumerate(jconstraints) + if cons isa Equation + @constraint(model, user[i], cons.lhs - cons.rhs == 0) + elseif cons.relational_op === Symbolics.geq + @constraint(model, user[i], cons.lhs - cons.rhs ≥ 0) + else + @constraint(model, user[i], cons.lhs - cons.rhs ≤ 0) + end + end +end + +function add_initial_constraints!(model, u0, u0_idxs, tspan) + ts = tspan[1] + @constraint(model, init_u0_idx[i in u0_idxs], model[:U][i](ts) == u0[i]) +end + +is_explicit(tableau) = tableau isa DiffEqDevTools.ExplicitRKTableau + +function add_solve_constraints!(prob, tableau) + A = tableau.A + α = tableau.α + c = tableau.c + model = prob.model + f = prob.f + p = prob.p + tsteps = supports(model[:t]) + pop!(tsteps) + dt = tsteps[2] - tsteps[1] + + U = model[:U] + nᵤ = length(U) + if is_explicit(tableau) + K = Any[] + for τ in tsteps + for (i, h) in enumerate(c) + ΔU = sum([A[i, j] * K[j] for j in 1:i-1], init = zeros(nᵤ)) + Uₙ = [U[i](τ) + ΔU[i]*dt for i in 1:nᵤ] + Kₙ = f(Uₙ, p, τ + h*dt) + push!(K, Kₙ) + end + ΔU = sum([α[i] * K[i] for i in 1:length(α)]) + @constraint(model, [n = 1:nᵤ], U[n](τ) + ΔU[n] == U[n](τ + dt)) + empty!(K) + end + else + @variable(model, K[1:length(a), 1:nᵤ], Infinite(t), start = tsteps[1]) + for τ in tsteps + ΔUs = [A * K(τ)] + for (i, h) in enumerate(c) + ΔU = ΔUs[i] + Uₙ = [U[j](τ) + ΔU[j](τ)*dt for j in 1:nᵤ] + @constraint(model, K[i](τ) == f(Uₙ, p, τ + h*dt)) + end + ΔU = sum([α[i] * K[i] for i in 1:length(α)]) + @constraint(model, U(τ) + dot(α, K(τ)) == U(τ + dt)) + end + end +end + +""" +""" +struct JuMPControlSolution + model::InfiniteModel + sol::ODESolution +end + +""" +Solve JuMPControlProblem. Arguments: +- prob: a JumpControlProblem +- jump_solver: a LP solver such as HiGHS +- ode_solver: Takes in a symbol representing the solver. Acceptable solvers may be found at https://docs.sciml.ai/DiffEqDevDocs/stable/internals/tableaus/. Note that the symbol may be different than the typical name of the solver, e.g. :Tsitouras5 rather than Tsit5. +""" +function DiffEqBase.solve(prob::JuMPControlProblem, jump_solver, ode_solver::Symbol) + model = prob.model + tableau_getter = Symbol(:construct, ode_solver) + @eval tableau = $tableau_getter() + ts = supports(model[:t]) + add_solve_constraints!(prob, tableau) + + set_optimizer(model, jump_solver) + optimize!(model) + + if is_solved_and_feasible(model) + sol = DiffEqBase.build_solution(prob, ode_solver, ts, value.(U)) + JuMPControlSolution(model, sol) + else + sol = DiffEqBase.build_solution(prob, ode_solver, ts, value.(U)) + sol = SciMLBase.solution_new_retcode(sol, SciMLBase.ReturnCode.ConvergenceFailure) + JuMPControlSolution(model, sol) + end +end + +end diff --git a/ext/MTKJuMPExt.jl b/ext/MTKJuMPExt.jl deleted file mode 100644 index 6cfcf432ef..0000000000 --- a/ext/MTKJuMPExt.jl +++ /dev/null @@ -1,191 +0,0 @@ -module MTKJuMPControlExt -using ModelingToolkit -using JuMP, InfiniteOpt -using DiffEqDevTools, DiffEqBase - -struct JuMPControlProblem{uType, tType, isinplace, P, F, K} <: - AbstractODEProblem{uType, tType, isinplace} - f::F - u0::uType - tspan::tType - p::P - model::Model - kwargs::K -end - -""" - JuMPControlProblem(sys::ODESystem, u0, tspan, p; dt) - -Convert an ODESystem representing an optimal control system into a JuMP model -for solving using optimization. Must provide `dt` for determining the length -of the interpolation arrays. - -The optimization variables: -- a vector-of-vectors U representing the unknowns as an interpolation array -- a vector-of-vectors V representing the controls as an interpolation array - -The constraints are: -- The set of user constraints passed to the ODESystem via `constraints` -- The solver constraints that encode the time-stepping used by the solver -""" -function JuMPControlProblem(sys::ODESystem, u0map, tspan, pmap; dt = error("dt must be provided for JuMPControlProblem."), guesses, eval_expression, eval_module) - ts = tspan[1] - te = tspan[2] - steps = ts:dt:te - ctrls = get_ctrls(sys) - states = unknowns(sys) - - if !isnothing(constraintsys) - (length(constraints(constraintsys)) + length(u0map) > length(sts)) && - @warn "The JuMPControlProblem is overdetermined. The total number of conditions (# constraints + # fixed initial values given by u0map) exceeds the total number of states. The solvers will default to doing a nonlinear least-squares optimization." - end - - model = InfiniteModel() - @infinite_parameter(model, t in [ts, te], num_supports = length(steps), derivative_method = OrthogonalCollocation(2)) - @variable(model, U[1:length(states)], Infinite(t), start = ts) - @variable(model, V[1:length(ctrls)], Infinite(t), start = ts) - @variable(model, K) - - f, u0, p = process_SciMLProblem(ODEFunction{iip, specialize}, sys, _u0map, parammap; - t = tspan !== nothing ? tspan[1] : tspan, guesses, - check_length, warn_initialize_determined, eval_expression, eval_module, kwargs...) - - add_jump_cost_function!(model, sys) - add_user_constraints!(model, sys) - - stidxmap = Dict([v => i for (i, v) in enumerate(sts)]) - u0_idxs = has_alg_eqs(sys) ? collect(1:length(sts)) : [stidxmap[k] for (k, v) in u0map] - add_initial_constraints!(model, u0, u0_idxs, tspan) - - JuMPControlProblem{iip}(f, u0, tspan, p, model, kwargs...) -end - -function add_jump_cost_function!(model, sys) - jcosts = get_costs(sys) - consolidate = get_consolidate(sys) - iv = get_iv(sys) - - stidxmap = Dict([v => i for (i, v) in enumerate(get_unknowns(sys))]) - cidxmap = Dict([v => i for (i, v) in enumerate(get_ctrls(sys))]) - - for st in get_unknowns(sys) - x = operation(st) - t = only(arguments(st)) - idx = stidxmap[x(iv)] - jcosts = Symbolics.substitute(costs, Dict(x(t) => model[:U][idx](t))) - end - - for ct in get_ctrls(sys) - p = operation(ct) - t = only(arguments(ct)) - idx = cidxmap[p(iv)] - jcosts = Symbolics.substitute(costs, Dict(p(t) => model[:V][idx](t))) - end - - @objective(model, Min, consolidate(jcosts)) -end - -function add_user_constraints!(model, sys, u0map) - jconstraints = get_constraints(get_constraintsystem(sys)) - iv = get_iv(sys) - - stidxmap = Dict([v => i for (i, v) in enumerate(get_unknowns(sys))]) - cidxmap = Dict([v => i for (i, v) in enumerate(get_ctrls(sys))]) - - for st in get_unknowns(sys) - x = operation(st) - t = only(arguments(st)) - idx = stidxmap[x(iv)] - subval = isequal(t, iv) ? model[:U][idx] : model[:U][idx](t) - jconstraints = Symbolics.substitute(constraints, Dict(x(t) => subval)) - end - - for ct in get_ctrls(sys) - p = operation(ct) - t = only(arguments(ct)) - idx = cidxmap[p(iv)] - subval = isequal(t, iv) ? model[:V][idx] : model[:V][idx](t) - jconstraints = Symbolics.substitute(constraints, Dict(p(t) => subval)) - end - - for (i, cons) in enumerate(jconstraints) - if cons isa Equation - @constraint(model, user[i], cons.lhs - cons.rhs == 0) - elseif cons.relational_op === Symbolics.geq - @constraint(model, user[i], cons.lhs - cons.rhs ≥ 0) - else - @constraint(model, user[i], cons.lhs - cons.rhs ≤ 0) - end - end -end - -function add_initial_constraints!(model, u0, u0_idxs, tspan) - ts = tspan[1] - @constraint(model, init_u0_idx[i in u0_idxs], U[i](ts) == u0[i]) -end - -is_explicit(tableau) = tableau isa DiffEqDevTools.ExplicitRKTableau - -function add_solve_constraints!(prob, tableau, f, tsteps) - A = tableau.A - α = tableau.α - c = tableau.c - model = prob.model - p = prob.p - dt = step(tsteps) - - U = model[:U] - if is_explicit(tableau) - K = Any[] - for τ in tsteps - for (i, h) in enumerate(c) - ΔU = sum([A[i, j] * K[j] for j in 1:i-1]) - Kₙ = f(U + ΔU*dt, p, τ + h*dt) - push!(K, Kₙ) - end - @constraint(model, U(τ) + dot(α, K) == U(τ + dt)) - empty!(K) - end - else - @variable(model, K[1:length(a)], Infinite(t), start = tsteps[1]) - for τ in tsteps - ΔUs = A * K(τ) - for (i, h) in enumerate(c) - ΔU = ΔUs[i] - @constraint(model, K[i](τ) == f(U(τ) + ΔU*dt, p, τ + h*dt)) - end - @constraint(model, U(τ) + dot(α, K(τ)) == U(τ + dt)) - end - end -end - -""" -""" -struct JuMPControlSolution - model - sol::ODESolution -end - -""" -Solve JuMPProblem. Takes in a symbol representing the solver. Acceptable solvers may be found at https://docs.sciml.ai/DiffEqDevDocs/stable/internals/tableaus/. -Note that the symbol may be different than the typical -name of the solver, e.g. :Tsitouras5 rather than Tsit5. -""" -function solve(prob::JuMPProblem, jump_solver, ode_solver::Symbol) - model = prob.model - f = prob.f - tableau_getter = Symbol(:construct, ode_solver) - @eval tableau = $tableau_getter() - ts = prob.tspan[1]:dt:prob.tspan[2] - add_solve_constraints!(model, ts, tableau, f) - - set_optimizer(model, jump_solver) - optimize!(model) - - if is_solved_and_feasible(model) - sol = DiffEqBase.build_solution(prob, ode_solver, ts, value.(U)) - JuMPControlSolution(model, sol) - end -end - -end diff --git a/src/ModelingToolkit.jl b/src/ModelingToolkit.jl index 992690cfe0..c6308a5bdd 100644 --- a/src/ModelingToolkit.jl +++ b/src/ModelingToolkit.jl @@ -348,4 +348,7 @@ export AnalysisPoint, get_sensitivity_function, get_comp_sensitivity_function, open_loop function FMIComponent end +function JuMPControlProblem end +export JuMPControlProblem + end # module diff --git a/test/extensions/JuMP.jl b/test/extensions/JuMP.jl deleted file mode 100644 index b6fc2b1fbc..0000000000 --- a/test/extensions/JuMP.jl +++ /dev/null @@ -1,3 +0,0 @@ -import ModelingToolkit as MTK -using JuMP, InfiniteOpt -using DiffEqDevTools, DiffEqBase diff --git a/test/extensions/Project.toml b/test/extensions/Project.toml index 5b0de73cdf..a6d548f86f 100644 --- a/test/extensions/Project.toml +++ b/test/extensions/Project.toml @@ -2,7 +2,10 @@ BifurcationKit = "0f109fa4-8a5d-4b75-95aa-f515264e7665" ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4" ChainRulesTestUtils = "cdddcdb0-9152-4a09-a978-84456f9df70a" +DiffEqBase = "2b5f629d-d688-5b77-993f-72d75c75574e" +DiffEqDevTools = "f3b72e0c-5b89-59e1-b016-84e28bfd966d" ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210" +HiGHS = "87dc4568-4c63-4d18-b0c0-bb2238e4078b" HomotopyContinuation = "f213a82b-91d6-5c5d-acf7-10f1c761b327" InfiniteOpt = "20393b10-9daf-11e9-18c9-8db751c92c57" Ipopt = "b6b21f68-93f8-5de0-b562-5493be1d77c9" @@ -12,6 +15,7 @@ ModelingToolkit = "961ee093-0014-501f-94e3-6117800e7a78" Nemo = "2edaba10-b0f1-5616-af89-8c11ac63239a" NonlinearSolveHomotopyContinuation = "2ac3b008-d579-4536-8c91-a1a5998c2f8b" OrdinaryDiffEq = "1dea7af3-3e70-54e6-95c3-0bf5283fa5ed" +OrdinaryDiffEqTsit5 = "b1df2697-797e-41e3-8120-5422d3b24e4a" SciMLSensitivity = "1ed8b502-d754-442c-8d5d-10ac956f44a1" SciMLStructures = "53ae85a6-f571-4167-b2af-e1d143709226" SymbolicIndexingInterface = "2efcf032-c050-4f8e-a9bb-153293bab1f5" diff --git a/test/extensions/jump_control.jl b/test/extensions/jump_control.jl new file mode 100644 index 0000000000..c0a071e31d --- /dev/null +++ b/test/extensions/jump_control.jl @@ -0,0 +1,57 @@ +using ModelingToolkit +using JuMP, InfiniteOpt +using DiffEqDevTools, DiffEqBase +using SimpleDiffEq +using HiGHS +const M = ModelingToolkit + +@testset "ODE Solution, no cost" begin + # Test solving without anything attached. + @parameters α=1.5 β=1.0 γ=3.0 δ=1.0 + M.@variables x(..) y(..) + t = M.t_nounits; D = M.D_nounits + + eqs = [D(x(t)) ~ α * x(t) - β * x(t) * y(t), + D(y(t)) ~ -γ * y(t) + δ * x(t) * y(t)] + + tspan = (0.0, 1.0) + u0map = [x(t) => 4.0, y(t) => 2.0] + parammap = [α => 7.5, β => 4, γ => 8.0, δ => 5.0] + @mtkbuild sys = ODESystem(eqs, t) + + # Test explicit method. + jprob = JuMPControlProblem(sys, u0map, tspan, parammap, dt = 0.01) + @test num_constraints(jprob.model) == 2 # initials + jsol = solve(jprob, Ipopt.Optimizer, :Tsitouras5) + oprob = ODEProblem(sys, u0map, tspan, parammap) + osol = solve(oprob, SimpleTsit5(), adaptive = false) + @test jsol.sol.u ≈ osol.u + + # Implicit method. + jsol2 = solve(prob, Ipopt.Optimizer, :RK4) + osol2 = solve(oprob, SimpleRK4(), adaptive = false) + @test jsol2.sol.u ≈ osol2.u + + # With a constraint + @parameters α=1.5 β=1.0 γ=3.0 δ=1.0 + @variables x(..) y(..) + + eqs = [D(x(t)) ~ α * x(t) - β * x(t) * y(t), + D(y(t)) ~ -γ * y(t) + δ * x(t) * y(t)] + + u0map = [] + tspan = (0.0, 1.0) + guess = [x(t) => 4.0, y(t) => 2.0] + constr = [x(0.6) ~ 3.5, x(0.3) ~ 7.0] + @mtkbuild lksys = ODESystem(eqs, t; constraints = constr) + + jprob = JuMPControlProblem(sys, u0map, tspan, parammap; guesses, dt = 0.01) + @test num_constraints(jprob.model) == 2 == num_variables(jprob.model) == 2 + jsol = solve(prob, HiGHS.Optimizer, :Tsitouras5) + sol = jsol.sol + @test sol(0.6)[1] ≈ 3.5 + @test sol(0.3)[1] ≈ 7.0 +end + +@testset "Optimal control problem" begin +end From ab233ef2a131118e7a5f8ce65eeb189c2713f65b Mon Sep 17 00:00:00 2001 From: vyudu Date: Tue, 8 Apr 2025 14:50:08 -0400 Subject: [PATCH 1422/2176] fix: use dt in the constraints --- Project.toml | 1 - ext/MTKJuMPControlExt.jl | 65 ++++++++++++++++++++++----------- test/extensions/Project.toml | 2 +- test/extensions/ad.jl | 2 +- test/extensions/jump_control.jl | 28 +++++++------- 5 files changed, 59 insertions(+), 39 deletions(-) diff --git a/Project.toml b/Project.toml index 3040619e1c..9149f51718 100644 --- a/Project.toml +++ b/Project.toml @@ -31,7 +31,6 @@ FunctionWrappers = "069b7b12-0de2-55c6-9aab-29f3d0a68a2e" FunctionWrappersWrappers = "77dc65aa-8811-40c2-897b-53d922fa7daf" Graphs = "86223c79-3864-5bf0-83f7-82e725a168b6" InteractiveUtils = "b77e0a4c-d291-57a0-90e8-8db25a27a240" -Ipopt = "b6b21f68-93f8-5de0-b562-5493be1d77c9" JuliaFormatter = "98e50ef6-434e-11e9-1051-2b60c6c9e899" JumpProcesses = "ccbc3e58-028d-4f4c-8cd5-9ae44345cda5" Latexify = "23fbe1c1-3f47-55db-b15f-69d7ec21a316" diff --git a/ext/MTKJuMPControlExt.jl b/ext/MTKJuMPControlExt.jl index 1e788b05ec..75260d4996 100644 --- a/ext/MTKJuMPControlExt.jl +++ b/ext/MTKJuMPControlExt.jl @@ -5,7 +5,7 @@ using DiffEqDevTools, DiffEqBase, SciMLBase using LinearAlgebra const MTK = ModelingToolkit -struct JuMPControlProblem{uType, tType, P, F, K} +struct JuMPControlProblem{uType, tType, isinplace, P, F, K} <: SciMLBase.AbstractODEProblem{uType, tType, isinplace} f::F u0::uType tspan::tType @@ -14,7 +14,7 @@ struct JuMPControlProblem{uType, tType, P, F, K} kwargs::K function JuMPControlProblem(f, u0, tspan, p, model; kwargs...) - new{typeof(u0), typeof(tspan), typeof(p), typeof(f), typeof(kwargs)}(f, u0, tspan, p, model, kwargs) + new{typeof(u0), typeof(tspan), SciMLBase.isinplace(f), typeof(p), typeof(f), typeof(kwargs)}(f, u0, tspan, p, model, kwargs) end end @@ -50,9 +50,9 @@ function MTK.JuMPControlProblem(sys::ODESystem, u0map, tspan, pmap; dt = error(" t = tspan !== nothing ? tspan[1] : tspan, kwargs...) model = InfiniteModel() - @infinite_parameter(model, t in [ts, te], num_supports = length(steps), derivative_method = OrthogonalCollocation(2)) - @variable(model, U[1:length(states)], Infinite(t), start = ts) - @variable(model, V[1:length(ctrls)], Infinite(t), start = ts) + @infinite_parameter(model, t in [ts, te], num_supports = length(steps)) + @variable(model, U[i = 1:length(states)], Infinite(t)) + @variable(model, V[1:length(ctrls)], Infinite(t)) add_jump_cost_function!(model, sys) add_user_constraints!(model, sys) @@ -136,7 +136,8 @@ end function add_initial_constraints!(model, u0, u0_idxs, tspan) ts = tspan[1] - @constraint(model, init_u0_idx[i in u0_idxs], model[:U][i](ts) == u0[i]) + U = model[:U] + @constraint(model, initial[i in u0_idxs], U[i](ts) == u0[i]) end is_explicit(tableau) = tableau isa DiffEqDevTools.ExplicitRKTableau @@ -148,6 +149,7 @@ function add_solve_constraints!(prob, tableau) model = prob.model f = prob.f p = prob.p + t = model[:t] tsteps = supports(model[:t]) pop!(tsteps) dt = tsteps[2] - tsteps[1] @@ -163,21 +165,21 @@ function add_solve_constraints!(prob, tableau) Kₙ = f(Uₙ, p, τ + h*dt) push!(K, Kₙ) end - ΔU = sum([α[i] * K[i] for i in 1:length(α)]) - @constraint(model, [n = 1:nᵤ], U[n](τ) + ΔU[n] == U[n](τ + dt)) + ΔU = dt*sum([α[i] * K[i] for i in 1:length(α)]) + @constraint(model, [n = 1:nᵤ], U[n](τ) + ΔU[n] == U[n](τ + dt), base_name = "solve_time_$τ") empty!(K) end else - @variable(model, K[1:length(a), 1:nᵤ], Infinite(t), start = tsteps[1]) + @variable(model, K[1:length(α), 1:nᵤ], Infinite(t), start = tsteps[1]) for τ in tsteps - ΔUs = [A * K(τ)] + ΔUs = A * K for (i, h) in enumerate(c) - ΔU = ΔUs[i] - Uₙ = [U[j](τ) + ΔU[j](τ)*dt for j in 1:nᵤ] - @constraint(model, K[i](τ) == f(Uₙ, p, τ + h*dt)) + ΔU = ΔUs[i, :] + Uₙ = [U[j] + ΔU[j]*dt for j in 1:nᵤ] + @constraint(model, [j in 1:nᵤ], K[i, j] == f(Uₙ, p, τ + h*dt)[j], DomainRestrictions(t => τ), base_name = "solve_K($τ)") end - ΔU = sum([α[i] * K[i] for i in 1:length(α)]) - @constraint(model, U(τ) + dot(α, K(τ)) == U(τ + dt)) + ΔU = dt*sum([α[i] * K[i, :] for i in 1:length(α)]) + @constraint(model, [n = 1:nᵤ], U[n] + ΔU[n] == U[n](τ + dt), DomainRestrictions(t => τ), base_name = "solve_U($τ)") end end end @@ -194,25 +196,46 @@ Solve JuMPControlProblem. Arguments: - prob: a JumpControlProblem - jump_solver: a LP solver such as HiGHS - ode_solver: Takes in a symbol representing the solver. Acceptable solvers may be found at https://docs.sciml.ai/DiffEqDevDocs/stable/internals/tableaus/. Note that the symbol may be different than the typical name of the solver, e.g. :Tsitouras5 rather than Tsit5. + +Returns a JuMPControlSolution, which contains both the model and the ODE solution. """ function DiffEqBase.solve(prob::JuMPControlProblem, jump_solver, ode_solver::Symbol) model = prob.model tableau_getter = Symbol(:construct, ode_solver) @eval tableau = $tableau_getter() ts = supports(model[:t]) + + # Unregister current solver constraints + for con in all_constraints(model) + if occursin("solve", JuMP.name(con)) + unregister(model, Symbol(JuMP.name(con))) + delete(model, con) + end + end + for var in all_variables(model) + @show JuMP.name(var) + if occursin("K", JuMP.name(var)) + unregister(model, Symbol(JuMP.name(var))) + delete(model, var) + end + end add_solve_constraints!(prob, tableau) set_optimizer(model, jump_solver) optimize!(model) - if is_solved_and_feasible(model) - sol = DiffEqBase.build_solution(prob, ode_solver, ts, value.(U)) - JuMPControlSolution(model, sol) - else - sol = DiffEqBase.build_solution(prob, ode_solver, ts, value.(U)) + tstatus = termination_status(model) + pstatus = primal_status(model) + !has_values(model) && error("Model not solvable; please report this to github.com/SciML/ModelingToolkit.jl.") + + U_vals = value.(model[:U]) + U_vals = [[U_vals[i][j] for i in 1:length(U_vals)] for j in 1:length(ts)] + sol = DiffEqBase.build_solution(prob, ode_solver, ts, U_vals) + + if !(pstatus === FEASIBLE_POINT && (tstatus === OPTIMAL || tstatus === LOCALLY_SOLVED || tstatus === ALMOST_OPTIMAL || tstatus === ALMOST_LOCALLY_SOLVED)) sol = SciMLBase.solution_new_retcode(sol, SciMLBase.ReturnCode.ConvergenceFailure) - JuMPControlSolution(model, sol) end + JuMPControlSolution(model, sol) end end diff --git a/test/extensions/Project.toml b/test/extensions/Project.toml index a6d548f86f..9ce343202d 100644 --- a/test/extensions/Project.toml +++ b/test/extensions/Project.toml @@ -14,10 +14,10 @@ LabelledArrays = "2ee39098-c373-598a-b85f-a56591580800" ModelingToolkit = "961ee093-0014-501f-94e3-6117800e7a78" Nemo = "2edaba10-b0f1-5616-af89-8c11ac63239a" NonlinearSolveHomotopyContinuation = "2ac3b008-d579-4536-8c91-a1a5998c2f8b" -OrdinaryDiffEq = "1dea7af3-3e70-54e6-95c3-0bf5283fa5ed" OrdinaryDiffEqTsit5 = "b1df2697-797e-41e3-8120-5422d3b24e4a" SciMLSensitivity = "1ed8b502-d754-442c-8d5d-10ac956f44a1" SciMLStructures = "53ae85a6-f571-4167-b2af-e1d143709226" +SimpleDiffEq = "05bca326-078c-5bf0-a5bf-ce7c7982d7fd" SymbolicIndexingInterface = "2efcf032-c050-4f8e-a9bb-153293bab1f5" Symbolics = "0c5d862f-8b57-4792-8d23-62f2024744c7" Zygote = "e88e6eb3-aa80-5325-afca-941959d7151f" diff --git a/test/extensions/ad.jl b/test/extensions/ad.jl index adaf6117c6..a456263655 100644 --- a/test/extensions/ad.jl +++ b/test/extensions/ad.jl @@ -3,7 +3,7 @@ using ModelingToolkit: t_nounits as t, D_nounits as D using Zygote using SymbolicIndexingInterface using SciMLStructures -using OrdinaryDiffEq +using OrdinaryDiffEqTsit5 using NonlinearSolve using SciMLSensitivity using ForwardDiff diff --git a/test/extensions/jump_control.jl b/test/extensions/jump_control.jl index c0a071e31d..a08c95b29b 100644 --- a/test/extensions/jump_control.jl +++ b/test/extensions/jump_control.jl @@ -2,7 +2,8 @@ using ModelingToolkit using JuMP, InfiniteOpt using DiffEqDevTools, DiffEqBase using SimpleDiffEq -using HiGHS +using OrdinaryDiffEqSDIRK +using Ipopt const M = ModelingToolkit @testset "ODE Solution, no cost" begin @@ -22,36 +23,33 @@ const M = ModelingToolkit # Test explicit method. jprob = JuMPControlProblem(sys, u0map, tspan, parammap, dt = 0.01) @test num_constraints(jprob.model) == 2 # initials - jsol = solve(jprob, Ipopt.Optimizer, :Tsitouras5) + jsol = solve(jprob, Ipopt.Optimizer, :RK4) oprob = ODEProblem(sys, u0map, tspan, parammap) - osol = solve(oprob, SimpleTsit5(), adaptive = false) + osol = solve(oprob, SimpleRK4(), dt = 0.01) @test jsol.sol.u ≈ osol.u # Implicit method. - jsol2 = solve(prob, Ipopt.Optimizer, :RK4) - osol2 = solve(oprob, SimpleRK4(), adaptive = false) - @test jsol2.sol.u ≈ osol2.u + jsol2 = solve(jprob, Ipopt.Optimizer, :ImplicitEuler) + osol2 = solve(oprob, ImplicitEuler(), dt = 0.01, adaptive = false) + @test ≈(jsol2.sol.u, osol2.u, rtol = 0.001) # With a constraint - @parameters α=1.5 β=1.0 γ=3.0 δ=1.0 - @variables x(..) y(..) - - eqs = [D(x(t)) ~ α * x(t) - β * x(t) * y(t), - D(y(t)) ~ -γ * y(t) + δ * x(t) * y(t)] - - u0map = [] - tspan = (0.0, 1.0) + u0map = Pair[] guess = [x(t) => 4.0, y(t) => 2.0] constr = [x(0.6) ~ 3.5, x(0.3) ~ 7.0] @mtkbuild lksys = ODESystem(eqs, t; constraints = constr) jprob = JuMPControlProblem(sys, u0map, tspan, parammap; guesses, dt = 0.01) @test num_constraints(jprob.model) == 2 == num_variables(jprob.model) == 2 - jsol = solve(prob, HiGHS.Optimizer, :Tsitouras5) + jsol = solve(prob, Ipopt.Optimizer, :Tsitouras5) sol = jsol.sol @test sol(0.6)[1] ≈ 3.5 @test sol(0.3)[1] ≈ 7.0 end @testset "Optimal control problem" begin + # Investing + + + # Bang-bang control end From 7d677311e0ba03cce0c791a19e7047b16e848620 Mon Sep 17 00:00:00 2001 From: vyudu Date: Tue, 8 Apr 2025 14:51:02 -0400 Subject: [PATCH 1423/2176] test: add to runtests --- test/runtests.jl | 1 + 1 file changed, 1 insertion(+) diff --git a/test/runtests.jl b/test/runtests.jl index 37c738eec9..08e7425a3c 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -141,5 +141,6 @@ end @safetestset "LabelledArrays Test" include("labelledarrays.jl") @safetestset "BifurcationKit Extension Test" include("extensions/bifurcationkit.jl") @safetestset "InfiniteOpt Extension Test" include("extensions/test_infiniteopt.jl") + @safetestset "JuMPControl Extension Test" include("extensions/jump_control.jl") end end From 3f0721e9d896a49157323e3f70a3c4b5b05185e0 Mon Sep 17 00:00:00 2001 From: vyudu Date: Tue, 8 Apr 2025 14:53:24 -0400 Subject: [PATCH 1424/2176] remove cassadi file --- ext/MTKCASADIExt.jl | 11 ----------- 1 file changed, 11 deletions(-) delete mode 100644 ext/MTKCASADIExt.jl diff --git a/ext/MTKCASADIExt.jl b/ext/MTKCASADIExt.jl deleted file mode 100644 index b41ece6eb7..0000000000 --- a/ext/MTKCASADIExt.jl +++ /dev/null @@ -1,11 +0,0 @@ -""" -an ODESystem with constraints to a JuMPProblem for optimal control solving. -""" -function CASADIProblem(sys::ODESystem) - -end - - -function CASADIProblem(prob::ODEProblem) - -end From f4be49bc6afaf924d7340adb052ac34cfa72ad7f Mon Sep 17 00:00:00 2001 From: vyudu Date: Tue, 8 Apr 2025 18:19:30 -0400 Subject: [PATCH 1425/2176] up? --- ext/MTKJuMPControlExt.jl | 47 ++++++++++++++++++-------------- src/systems/diffeqs/odesystem.jl | 2 ++ test/extensions/jump_control.jl | 36 +++++++++++++++++------- 3 files changed, 55 insertions(+), 30 deletions(-) diff --git a/ext/MTKJuMPControlExt.jl b/ext/MTKJuMPControlExt.jl index 75260d4996..81a6fd2f74 100644 --- a/ext/MTKJuMPControlExt.jl +++ b/ext/MTKJuMPControlExt.jl @@ -33,7 +33,7 @@ The constraints are: - The set of user constraints passed to the ODESystem via `constraints` - The solver constraints that encode the time-stepping used by the solver """ -function MTK.JuMPControlProblem(sys::ODESystem, u0map, tspan, pmap; dt = error("dt must be provided for JuMPControlProblem."), kwargs...) +function MTK.JuMPControlProblem(sys::ODESystem, u0map, tspan, pmap; dt = error("dt must be provided for JuMPControlProblem."), guesses = Dict(), kwargs...) ts = tspan[1] te = tspan[2] steps = ts:dt:te @@ -42,11 +42,12 @@ function MTK.JuMPControlProblem(sys::ODESystem, u0map, tspan, pmap; dt = error(" constraintsys = MTK.get_constraintsystem(sys) if !isnothing(constraintsys) - (length(constraints(constraintsys)) + length(u0map) > length(sts)) && + (length(constraints(constraintsys)) + length(u0map) > length(states)) && @warn "The JuMPControlProblem is overdetermined. The total number of conditions (# constraints + # fixed initial values given by u0map) exceeds the total number of states. The solvers will default to doing a nonlinear least-squares optimization." end - f, u0, p = MTK.process_SciMLProblem(ODEFunction, sys, u0map, pmap; + _u0map = has_alg_eqs(sys) ? u0map : merge(Dict(u0map), Dict(guesses)) + f, u0, p = MTK.process_SciMLProblem(ODEFunction, sys, _u0map, pmap; t = tspan !== nothing ? tspan[1] : tspan, kwargs...) model = InfiniteModel() @@ -67,7 +68,7 @@ end function add_jump_cost_function!(model, sys) jcosts = MTK.get_costs(sys) consolidate = MTK.get_consolidate(sys) - if isnothing(consolidate) + if isnothing(jcosts) @objective(model, Min, 0) return end @@ -81,7 +82,7 @@ function add_jump_cost_function!(model, sys) t = only(arguments(st)) idx = stidxmap[x(iv)] subval = isequal(t, iv) ? model[:U][idx] : model[:U][idx](t) - jcosts = Symbolics.substitute(jcosts, Dict(x(t) => subval)) + jcosts = map(c -> Symbolics.substitute(c, Dict(x(t) => subval)), jcosts) end for ct in controls(sys) @@ -89,30 +90,27 @@ function add_jump_cost_function!(model, sys) t = only(arguments(ct)) idx = cidxmap[p(iv)] subval = isequal(t, iv) ? model[:V][idx] : model[:V][idx](t) - jcosts = Symbolics.substitute(jcosts, Dict(x(t) => subval)) + jcosts = map(c -> Symbolics.substitute(c, Dict(p(t) => subval)), jcosts) end @objective(model, Min, consolidate(jcosts)) end function add_user_constraints!(model, sys) - jconstraints = if !(csys = MTK.get_constraintsystem(sys) isa Nothing) - MTK.get_constraints(csys) - else - nothing - end - isnothing(jconstraints) && return nothing + conssys = MTK.get_constraintsystem(sys) + jconstraints = isnothing(conssys) ? nothing : MTK.get_constraints(conssys) + (isnothing(jconstraints) || isempty(jconstraints)) && return nothing iv = MTK.get_iv(sys) stidxmap = Dict([v => i for (i, v) in enumerate(unknowns(sys))]) cidxmap = Dict([v => i for (i, v) in enumerate(controls(sys))]) - for st in unknowns(sys) + for st in unknowns(conssys) x = operation(st) t = only(arguments(st)) idx = stidxmap[x(iv)] subval = isequal(t, iv) ? model[:U][idx] : model[:U][idx](t) - jconstraints = Symbolics.substitute(jconstraints, Dict(x(t) => subval)) + jconstraints = map(c -> Symbolics.substitute(c, Dict(x(t) => subval)), jconstraints) end for ct in controls(sys) @@ -120,16 +118,16 @@ function add_user_constraints!(model, sys) t = only(arguments(ct)) idx = cidxmap[p(iv)] subval = isequal(t, iv) ? model[:V][idx] : model[:V][idx](t) - jconstraints = Symbolics.substitute(jconstraints, Dict(p(t) => subval)) + jconstraints = map(c -> Symbolics.substitute(jconstraints, Dict(p(t) => subval)), jconstriants) end for (i, cons) in enumerate(jconstraints) if cons isa Equation - @constraint(model, user[i], cons.lhs - cons.rhs == 0) + @constraint(model, cons.lhs - cons.rhs == 0, base_name = "user[$i]") elseif cons.relational_op === Symbolics.geq - @constraint(model, user[i], cons.lhs - cons.rhs ≥ 0) + @constraint(model, cons.lhs - cons.rhs ≥ 0, base_name = "user[$i]") else - @constraint(model, user[i], cons.lhs - cons.rhs ≤ 0) + @constraint(model, cons.lhs - cons.rhs ≤ 0, base_name = "user[$i]") end end end @@ -189,6 +187,7 @@ end struct JuMPControlSolution model::InfiniteModel sol::ODESolution + input_sol::Union{Nothing, ODESolution} end """ @@ -213,7 +212,6 @@ function DiffEqBase.solve(prob::JuMPControlProblem, jump_solver, ode_solver::Sym end end for var in all_variables(model) - @show JuMP.name(var) if occursin("K", JuMP.name(var)) unregister(model, Symbol(JuMP.name(var))) delete(model, var) @@ -232,10 +230,19 @@ function DiffEqBase.solve(prob::JuMPControlProblem, jump_solver, ode_solver::Sym U_vals = [[U_vals[i][j] for i in 1:length(U_vals)] for j in 1:length(ts)] sol = DiffEqBase.build_solution(prob, ode_solver, ts, U_vals) + input_sol = nothing + if !isempty(model[:V]) + V_vals = value.(model[:V]) + V_vals = [[V_vals[i][j] for i in 1:length(V_vals)] for j in 1:length(ts)] + input_sol = DiffEqBase.build_solution(prob, ode_solver, ts, V_vals) + end + if !(pstatus === FEASIBLE_POINT && (tstatus === OPTIMAL || tstatus === LOCALLY_SOLVED || tstatus === ALMOST_OPTIMAL || tstatus === ALMOST_LOCALLY_SOLVED)) sol = SciMLBase.solution_new_retcode(sol, SciMLBase.ReturnCode.ConvergenceFailure) + !isnothing(input_sol) && (input_sol = SciMLBase.solution_new_retcode(input_sol, SciMLBase.ReturnCode.ConvergenceFailure)) end - JuMPControlSolution(model, sol) + + JuMPControlSolution(model, sol, input_sol) end end diff --git a/src/systems/diffeqs/odesystem.jl b/src/systems/diffeqs/odesystem.jl index 50655d0074..c307d19110 100644 --- a/src/systems/diffeqs/odesystem.jl +++ b/src/systems/diffeqs/odesystem.jl @@ -354,6 +354,8 @@ function ODESystem(deqs::AbstractVector{<:Equation}, iv, dvs, ps; if length(costs) > 1 && isnothing(consolidate) error("Must specify a consolidation function for the costs vector.") + elseif isnothing(consolidate) + consolidate(u) = u[1] end assertions = Dict{BasicSymbolic, Any}(unwrap(k) => v for (k, v) in assertions) diff --git a/test/extensions/jump_control.jl b/test/extensions/jump_control.jl index a08c95b29b..62c0ea7dbd 100644 --- a/test/extensions/jump_control.jl +++ b/test/extensions/jump_control.jl @@ -17,7 +17,7 @@ const M = ModelingToolkit tspan = (0.0, 1.0) u0map = [x(t) => 4.0, y(t) => 2.0] - parammap = [α => 7.5, β => 4, γ => 8.0, δ => 5.0] + parammap = [α => 1.5, β => 1.0, γ => 3.0, δ => 1.0] @mtkbuild sys = ODESystem(eqs, t) # Test explicit method. @@ -39,17 +39,33 @@ const M = ModelingToolkit constr = [x(0.6) ~ 3.5, x(0.3) ~ 7.0] @mtkbuild lksys = ODESystem(eqs, t; constraints = constr) - jprob = JuMPControlProblem(sys, u0map, tspan, parammap; guesses, dt = 0.01) - @test num_constraints(jprob.model) == 2 == num_variables(jprob.model) == 2 - jsol = solve(prob, Ipopt.Optimizer, :Tsitouras5) + jprob = JuMPControlProblem(lksys, u0map, tspan, parammap; guesses = guess, dt = 0.01) + @test num_constraints(jprob.model) == 2 + jsol = solve(jprob, Ipopt.Optimizer, :Tsitouras5) sol = jsol.sol @test sol(0.6)[1] ≈ 3.5 @test sol(0.3)[1] ≈ 7.0 end -@testset "Optimal control problem" begin - # Investing - - - # Bang-bang control -end +#@testset "Optimal control: bees" begin +# # Example from Lawrence Evans' notes +# M.@variables w(..) q(..) +# M.@parameters α(t) [bounds = [0, 1]] b c μ s ν +# +# tspan = (0, 4) +# eqs = [D(w(t)) ~ -μ*w(t) + b*s*α*w(t), +# D(q(t)) ~ -ν*q(t) + c*(1 - α)*s*w(t)] +# costs = [-q(tspan[2])] +# +# @mtkbuild beesys = ODESystem(eqs, t; costs) +# u0map = [w(0) => 40, q(0) => 2] +# pmap = [b => 1, c => 1, μ => 1, s => 1, ν => 1] +# +# jprob = JuMPControlProblem(beesys, u0map, tspan, pmap) +# jsol = solve(jprob, Ipopt.Optimizer, :Tsitouras5) +# control_sol = jsol.control_sol +# # Bang-bang control +#end +# +#@testset "Constrained optimal control problems" begin +#end From 484ffdaa7146399d101ee3c0ac6c4340a19e2bd1 Mon Sep 17 00:00:00 2001 From: vyudu Date: Tue, 8 Apr 2025 18:38:53 -0400 Subject: [PATCH 1426/2176] fix: consolidate method --- src/systems/diffeqs/odesystem.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/systems/diffeqs/odesystem.jl b/src/systems/diffeqs/odesystem.jl index c307d19110..bb875b5908 100644 --- a/src/systems/diffeqs/odesystem.jl +++ b/src/systems/diffeqs/odesystem.jl @@ -355,7 +355,7 @@ function ODESystem(deqs::AbstractVector{<:Equation}, iv, dvs, ps; if length(costs) > 1 && isnothing(consolidate) error("Must specify a consolidation function for the costs vector.") elseif isnothing(consolidate) - consolidate(u) = u[1] + consolidate = u -> u[1] end assertions = Dict{BasicSymbolic, Any}(unwrap(k) => v for (k, v) in assertions) From 1cd9f9d2feaae8759501eacec6fcbbe553cbe672 Mon Sep 17 00:00:00 2001 From: vyudu Date: Wed, 9 Apr 2025 15:51:53 -0400 Subject: [PATCH 1427/2176] feat: InfiniteOptControlProblem --- ext/MTKJuMPControlExt.jl | 105 +++++++++++++++++++++++++++++++++------ 1 file changed, 90 insertions(+), 15 deletions(-) diff --git a/ext/MTKJuMPControlExt.jl b/ext/MTKJuMPControlExt.jl index 81a6fd2f74..1e775b1953 100644 --- a/ext/MTKJuMPControlExt.jl +++ b/ext/MTKJuMPControlExt.jl @@ -5,7 +5,9 @@ using DiffEqDevTools, DiffEqBase, SciMLBase using LinearAlgebra const MTK = ModelingToolkit -struct JuMPControlProblem{uType, tType, isinplace, P, F, K} <: SciMLBase.AbstractODEProblem{uType, tType, isinplace} +abstract type AbstractOptimalControlProblem{uType, tType, isinplace} <: SciMLBase.AbstractODEProblem{uType, tType, isinplace} end + +struct JuMPControlProblem{uType, tType, isinplace, P, F, K} <: AbstractOptimalControlProblem{uType, tType, isinplace} f::F u0::uType tspan::tType @@ -18,6 +20,19 @@ struct JuMPControlProblem{uType, tType, isinplace, P, F, K} <: SciMLBase.Abstrac end end +struct InfiniteOptControlProblem{uType, tType, isinplace, P, F, K} <: SciMLBase.AbstractODEProblem{uType, tType, isinplace} + f::F + u0::uType + tspan::tType + p::P + model::InfiniteModel + kwargs::K + + function InfiniteOptControlProblem(f, u0, tspan, p, model; kwargs...) + new{typeof(u0), typeof(tspan), SciMLBase.isinplace(f), typeof(p), typeof(f), typeof(kwargs)}(f, u0, tspan, p, model, kwargs) + end +end + """ JuMPControlProblem(sys::ODESystem, u0, tspan, p; dt) @@ -34,24 +49,52 @@ The constraints are: - The solver constraints that encode the time-stepping used by the solver """ function MTK.JuMPControlProblem(sys::ODESystem, u0map, tspan, pmap; dt = error("dt must be provided for JuMPControlProblem."), guesses = Dict(), kwargs...) - ts = tspan[1] - te = tspan[2] - steps = ts:dt:te - ctrls = controls(sys) - states = unknowns(sys) constraintsys = MTK.get_constraintsystem(sys) - if !isnothing(constraintsys) (length(constraints(constraintsys)) + length(u0map) > length(states)) && - @warn "The JuMPControlProblem is overdetermined. The total number of conditions (# constraints + # fixed initial values given by u0map) exceeds the total number of states. The solvers will default to doing a nonlinear least-squares optimization." + @warn "The control problem is overdetermined. The total number of conditions (# constraints + # fixed initial values given by u0map) exceeds the total number of states. The solvers will default to doing a nonlinear least-squares optimization." + end + + _u0map = has_alg_eqs(sys) ? u0map : merge(Dict(u0map), Dict(guesses)) + f, u0, p = MTK.process_SciMLProblem(ODEFunction, sys, _u0map, pmap; + t = tspan !== nothing ? tspan[1] : tspan, kwargs...) + model = init_model(sys, tspan[1]:dt:tspan[2], u0map) + + JuMPControlProblem(f, u0, tspan, p, model, kwargs...) +end + +""" + InfiniteOptControlProblem(sys::ODESystem, u0map, tspan, pmap; dt) + +Convert an ODESystem representing an optimal control system into a InfiniteOpt model +for solving using optimization. Must provide `dt` for determining the length +of the interpolation arrays. + +Related to `JuMPControlProblem`, but directly adds the differential equations +of the system as derivative constraints, rather than using a solver tableau. +""" +function MTK.InfiniteOptControlProblem(sys::ODESystem, u0map, tspan, pmap; dt = error("dt must be provided for InfiniteOptControlProblem."), guesses = Dict(), kwargs...) + constraintsys = MTK.get_constraintsystem(sys) + if !isnothing(constraintsys) + (length(constraints(constraintsys)) + length(u0map) > length(unknowns(sys))) && + @warn "The control problem is overdetermined. The total number of conditions (# constraints + # fixed initial values given by u0map) exceeds the total number of states. The solvers will default to doing a nonlinear least-squares optimization." end _u0map = has_alg_eqs(sys) ? u0map : merge(Dict(u0map), Dict(guesses)) f, u0, p = MTK.process_SciMLProblem(ODEFunction, sys, _u0map, pmap; t = tspan !== nothing ? tspan[1] : tspan, kwargs...) + model = init_model(sys, tspan[1]:dt:tspan[2], u0map) + add_infopt_solve_constraints!(model, sys, pmap) + InfiniteOptControlProblem(f, u0, tspan, p, model, kwargs...) +end + +function init_model(sys, tsteps, u0map) + ctrls = controls(sys) + states = unknowns(sys) + model = InfiniteModel() - @infinite_parameter(model, t in [ts, te], num_supports = length(steps)) + @infinite_parameter(model, t in [tsteps[1], tsteps[end]], num_supports = length(tsteps)) @variable(model, U[i = 1:length(states)], Infinite(t)) @variable(model, V[1:length(ctrls)], Infinite(t)) @@ -61,8 +104,6 @@ function MTK.JuMPControlProblem(sys::ODESystem, u0map, tspan, pmap; dt = error(" stidxmap = Dict([v => i for (i, v) in enumerate(states)]) u0_idxs = has_alg_eqs(sys) ? collect(1:length(states)) : [stidxmap[k] for (k, v) in u0map] add_initial_constraints!(model, u0, u0_idxs, tspan) - - JuMPControlProblem(f, u0, tspan, p, model, kwargs...) end function add_jump_cost_function!(model, sys) @@ -140,7 +181,29 @@ end is_explicit(tableau) = tableau isa DiffEqDevTools.ExplicitRKTableau -function add_solve_constraints!(prob, tableau) +function add_infopt_solve_constraints!(model, sys, pmap) + iv = get_iv(sys) + t = model[:t] + U = model[:U] + V = model[:V] + + stmap = Dict([v => U[i] for (i, v) in enumerate(unknowns(sys))]) + ctrlmap = Dict([v => V[i] for (i, v) in enumerate(controls(sys))]) + submap = merge(stmap, ctrlmap, pmap) + + @register_symbolic _D(x) = ∂(x, t) + # Differential equations + diff_eqs = diff_equations(sys) + diff_eqs = map(e -> Symbolics.substitute(e, submap, Differential(iv) => _D), diff_eqs) + @constraint(model, D[i = 1:length(diff_eqs)], diff_eqs[i].lhs == diff_eqs[i].rhs) + + # Algebraic equations + alg_eqs = alg_equations(sys) + alg_eqs = map(e -> Symbolics.substitute(e, submap), alg_eqs) + @constraint(model, A[i = 1:length(alg_eqs)], alg_eqs[i].lhs == alg_eqs[i].rhs) +end + +function add_jump_solve_constraints!(prob, tableau) A = tableau.A α = tableau.α c = tableau.c @@ -202,7 +265,6 @@ function DiffEqBase.solve(prob::JuMPControlProblem, jump_solver, ode_solver::Sym model = prob.model tableau_getter = Symbol(:construct, ode_solver) @eval tableau = $tableau_getter() - ts = supports(model[:t]) # Unregister current solver constraints for con in all_constraints(model) @@ -218,7 +280,19 @@ function DiffEqBase.solve(prob::JuMPControlProblem, jump_solver, ode_solver::Sym end end add_solve_constraints!(prob, tableau) + _solve(prob, jump_solver, ode_solver) +end +""" +`derivative_method` kwarg refers to the method used by InfiniteOpt to compute derivatives. The list of possible options can be found at https://infiniteopt.github.io/InfiniteOpt.jl/stable/guide/derivative/. Defaults to FiniteDifference(Backward()). +""" +function DiffEqBase.solve(prob::InfiniteOptControlProblem, jump_solver; derivative_method = InfiniteOpt.FiniteDifference(Backward())) + set_derivative_method(prob.model[:t], derivative_method) + _solve(prob, jump_solver, derivative_method) +end + +function _solve(prob::AbstractOptimalControlProblem, jump_solver, solver) + model = prob.model set_optimizer(model, jump_solver) optimize!(model) @@ -226,15 +300,16 @@ function DiffEqBase.solve(prob::JuMPControlProblem, jump_solver, ode_solver::Sym pstatus = primal_status(model) !has_values(model) && error("Model not solvable; please report this to github.com/SciML/ModelingToolkit.jl.") + ts = supports(model[:t]) U_vals = value.(model[:U]) U_vals = [[U_vals[i][j] for i in 1:length(U_vals)] for j in 1:length(ts)] - sol = DiffEqBase.build_solution(prob, ode_solver, ts, U_vals) + sol = DiffEqBase.build_solution(prob, solver, ts, U_vals) input_sol = nothing if !isempty(model[:V]) V_vals = value.(model[:V]) V_vals = [[V_vals[i][j] for i in 1:length(V_vals)] for j in 1:length(ts)] - input_sol = DiffEqBase.build_solution(prob, ode_solver, ts, V_vals) + input_sol = DiffEqBase.build_solution(prob, solver, ts, V_vals) end if !(pstatus === FEASIBLE_POINT && (tstatus === OPTIMAL || tstatus === LOCALLY_SOLVED || tstatus === ALMOST_OPTIMAL || tstatus === ALMOST_LOCALLY_SOLVED)) From 2641aa9cb041a6d3aa772cc00fd0b6577754600f Mon Sep 17 00:00:00 2001 From: vyudu Date: Thu, 10 Apr 2025 10:09:26 -0400 Subject: [PATCH 1428/2176] add InfiniteOPt dep --- Project.toml | 2 +- test/extensions/Project.toml | 1 - test/extensions/jump_control.jl | 2 ++ 3 files changed, 3 insertions(+), 2 deletions(-) diff --git a/Project.toml b/Project.toml index 9149f51718..e80a811477 100644 --- a/Project.toml +++ b/Project.toml @@ -78,7 +78,7 @@ MTKChainRulesCoreExt = "ChainRulesCore" MTKDeepDiffsExt = "DeepDiffs" MTKFMIExt = "FMI" MTKInfiniteOptExt = "InfiniteOpt" -MTKJuMPControlExt = ["JuMP", "DiffEqDevTools"] +MTKJuMPControlExt = ["JuMP", "DiffEqDevTools", "InfiniteOpt"] MTKLabelledArraysExt = "LabelledArrays" [compat] diff --git a/test/extensions/Project.toml b/test/extensions/Project.toml index 9ce343202d..5800fe612f 100644 --- a/test/extensions/Project.toml +++ b/test/extensions/Project.toml @@ -5,7 +5,6 @@ ChainRulesTestUtils = "cdddcdb0-9152-4a09-a978-84456f9df70a" DiffEqBase = "2b5f629d-d688-5b77-993f-72d75c75574e" DiffEqDevTools = "f3b72e0c-5b89-59e1-b016-84e28bfd966d" ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210" -HiGHS = "87dc4568-4c63-4d18-b0c0-bb2238e4078b" HomotopyContinuation = "f213a82b-91d6-5c5d-acf7-10f1c761b327" InfiniteOpt = "20393b10-9daf-11e9-18c9-8db751c92c57" Ipopt = "b6b21f68-93f8-5de0-b562-5493be1d77c9" diff --git a/test/extensions/jump_control.jl b/test/extensions/jump_control.jl index 62c0ea7dbd..7cd6b025b6 100644 --- a/test/extensions/jump_control.jl +++ b/test/extensions/jump_control.jl @@ -32,6 +32,8 @@ const M = ModelingToolkit jsol2 = solve(jprob, Ipopt.Optimizer, :ImplicitEuler) osol2 = solve(oprob, ImplicitEuler(), dt = 0.01, adaptive = false) @test ≈(jsol2.sol.u, osol2.u, rtol = 0.001) + iprob = InfiniteOptControlProblem(sys, u0map, tspan, parammap, dt = 0.01) + isol = solve(iprob, Ipopt.Optimizer, derivative_method = FiniteDifference(Backward())) # With a constraint u0map = Pair[] From 31724e7b9d9eaa97cf1865d9ad65cf0f6575c60f Mon Sep 17 00:00:00 2001 From: vyudu Date: Thu, 10 Apr 2025 13:47:28 -0400 Subject: [PATCH 1429/2176] feat: add InfiniteOptControlProblem --- ext/MTKJuMPControlExt.jl | 52 +++++++++++++++----------------- src/ModelingToolkit.jl | 2 ++ src/systems/diffeqs/odesystem.jl | 2 +- test/extensions/jump_control.jl | 15 ++++++--- 4 files changed, 39 insertions(+), 32 deletions(-) diff --git a/ext/MTKJuMPControlExt.jl b/ext/MTKJuMPControlExt.jl index 1e775b1953..55c9dfc3c5 100644 --- a/ext/MTKJuMPControlExt.jl +++ b/ext/MTKJuMPControlExt.jl @@ -20,7 +20,7 @@ struct JuMPControlProblem{uType, tType, isinplace, P, F, K} <: AbstractOptimalCo end end -struct InfiniteOptControlProblem{uType, tType, isinplace, P, F, K} <: SciMLBase.AbstractODEProblem{uType, tType, isinplace} +struct InfiniteOptControlProblem{uType, tType, isinplace, P, F, K} <: AbstractOptimalControlProblem{uType, tType, isinplace} f::F u0::uType tspan::tType @@ -49,16 +49,10 @@ The constraints are: - The solver constraints that encode the time-stepping used by the solver """ function MTK.JuMPControlProblem(sys::ODESystem, u0map, tspan, pmap; dt = error("dt must be provided for JuMPControlProblem."), guesses = Dict(), kwargs...) - constraintsys = MTK.get_constraintsystem(sys) - if !isnothing(constraintsys) - (length(constraints(constraintsys)) + length(u0map) > length(states)) && - @warn "The control problem is overdetermined. The total number of conditions (# constraints + # fixed initial values given by u0map) exceeds the total number of states. The solvers will default to doing a nonlinear least-squares optimization." - end - _u0map = has_alg_eqs(sys) ? u0map : merge(Dict(u0map), Dict(guesses)) f, u0, p = MTK.process_SciMLProblem(ODEFunction, sys, _u0map, pmap; t = tspan !== nothing ? tspan[1] : tspan, kwargs...) - model = init_model(sys, tspan[1]:dt:tspan[2], u0map) + model = init_model(sys, tspan[1]:dt:tspan[2], u0map, u0) JuMPControlProblem(f, u0, tspan, p, model, kwargs...) end @@ -74,25 +68,24 @@ Related to `JuMPControlProblem`, but directly adds the differential equations of the system as derivative constraints, rather than using a solver tableau. """ function MTK.InfiniteOptControlProblem(sys::ODESystem, u0map, tspan, pmap; dt = error("dt must be provided for InfiniteOptControlProblem."), guesses = Dict(), kwargs...) - constraintsys = MTK.get_constraintsystem(sys) - if !isnothing(constraintsys) - (length(constraints(constraintsys)) + length(u0map) > length(unknowns(sys))) && - @warn "The control problem is overdetermined. The total number of conditions (# constraints + # fixed initial values given by u0map) exceeds the total number of states. The solvers will default to doing a nonlinear least-squares optimization." - end - _u0map = has_alg_eqs(sys) ? u0map : merge(Dict(u0map), Dict(guesses)) f, u0, p = MTK.process_SciMLProblem(ODEFunction, sys, _u0map, pmap; t = tspan !== nothing ? tspan[1] : tspan, kwargs...) - model = init_model(sys, tspan[1]:dt:tspan[2], u0map) + model = init_model(sys, tspan[1]:dt:tspan[2], u0map, u0) add_infopt_solve_constraints!(model, sys, pmap) InfiniteOptControlProblem(f, u0, tspan, p, model, kwargs...) end -function init_model(sys, tsteps, u0map) +function init_model(sys, tsteps, u0map, u0) + constraintsys = MTK.get_constraintsystem(sys) + if !isnothing(constraintsys) + (length(constraints(constraintsys)) + length(u0map) > length(unknowns(sys))) && + @warn "The control problem is overdetermined. The total number of conditions (# constraints + # fixed initial values given by u0map) exceeds the total number of states. The solvers will default to doing a nonlinear least-squares optimization." + end + ctrls = controls(sys) states = unknowns(sys) - model = InfiniteModel() @infinite_parameter(model, t in [tsteps[1], tsteps[end]], num_supports = length(tsteps)) @variable(model, U[i = 1:length(states)], Infinite(t)) @@ -103,13 +96,14 @@ function init_model(sys, tsteps, u0map) stidxmap = Dict([v => i for (i, v) in enumerate(states)]) u0_idxs = has_alg_eqs(sys) ? collect(1:length(states)) : [stidxmap[k] for (k, v) in u0map] - add_initial_constraints!(model, u0, u0_idxs, tspan) + add_initial_constraints!(model, u0, u0_idxs, tsteps[1]) + return model end function add_jump_cost_function!(model, sys) jcosts = MTK.get_costs(sys) consolidate = MTK.get_consolidate(sys) - if isnothing(jcosts) + if isnothing(jcosts) || isempty(jcosts) @objective(model, Min, 0) return end @@ -173,8 +167,7 @@ function add_user_constraints!(model, sys) end end -function add_initial_constraints!(model, u0, u0_idxs, tspan) - ts = tspan[1] +function add_initial_constraints!(model, u0, u0_idxs, ts) U = model[:U] @constraint(model, initial[i in u0_idxs], U[i](ts) == u0[i]) end @@ -182,19 +175,24 @@ end is_explicit(tableau) = tableau isa DiffEqDevTools.ExplicitRKTableau function add_infopt_solve_constraints!(model, sys, pmap) - iv = get_iv(sys) + iv = MTK.get_iv(sys) t = model[:t] U = model[:U] V = model[:V] stmap = Dict([v => U[i] for (i, v) in enumerate(unknowns(sys))]) ctrlmap = Dict([v => V[i] for (i, v) in enumerate(controls(sys))]) - submap = merge(stmap, ctrlmap, pmap) + submap = merge(stmap, ctrlmap, Dict(pmap)) + @show submap - @register_symbolic _D(x) = ∂(x, t) # Differential equations diff_eqs = diff_equations(sys) - diff_eqs = map(e -> Symbolics.substitute(e, submap, Differential(iv) => _D), diff_eqs) + D = Differential(iv) + diffsubmap = Dict([D(U[i]) => ∂(U[i], t) for i in 1:length(U)]) + for u in unknowns(sys) + diff_eqs = map(e -> Symbolics.substitute(e, submap), diff_eqs) + diff_eqs = map(e -> Symbolics.substitute(e, diffsubmap), diff_eqs) + end @constraint(model, D[i = 1:length(diff_eqs)], diff_eqs[i].lhs == diff_eqs[i].rhs) # Algebraic equations @@ -273,13 +271,13 @@ function DiffEqBase.solve(prob::JuMPControlProblem, jump_solver, ode_solver::Sym delete(model, con) end end + unregister(model, :K) for var in all_variables(model) if occursin("K", JuMP.name(var)) - unregister(model, Symbol(JuMP.name(var))) delete(model, var) end end - add_solve_constraints!(prob, tableau) + add_jump_solve_constraints!(prob, tableau) _solve(prob, jump_solver, ode_solver) end diff --git a/src/ModelingToolkit.jl b/src/ModelingToolkit.jl index c6308a5bdd..b2808854bd 100644 --- a/src/ModelingToolkit.jl +++ b/src/ModelingToolkit.jl @@ -350,5 +350,7 @@ function FMIComponent end function JuMPControlProblem end export JuMPControlProblem +function InfiniteOptControlProblem end +export InfiniteOptControlProblem end # module diff --git a/src/systems/diffeqs/odesystem.jl b/src/systems/diffeqs/odesystem.jl index bb875b5908..227df7a156 100644 --- a/src/systems/diffeqs/odesystem.jl +++ b/src/systems/diffeqs/odesystem.jl @@ -354,7 +354,7 @@ function ODESystem(deqs::AbstractVector{<:Equation}, iv, dvs, ps; if length(costs) > 1 && isnothing(consolidate) error("Must specify a consolidation function for the costs vector.") - elseif isnothing(consolidate) + elseif length(costs) == 1 && isnothing(consolidate) consolidate = u -> u[1] end diff --git a/test/extensions/jump_control.jl b/test/extensions/jump_control.jl index 7cd6b025b6..2b01e612e3 100644 --- a/test/extensions/jump_control.jl +++ b/test/extensions/jump_control.jl @@ -4,6 +4,7 @@ using DiffEqDevTools, DiffEqBase using SimpleDiffEq using OrdinaryDiffEqSDIRK using Ipopt +using BenchmarkTools const M = ModelingToolkit @testset "ODE Solution, no cost" begin @@ -29,11 +30,11 @@ const M = ModelingToolkit @test jsol.sol.u ≈ osol.u # Implicit method. - jsol2 = solve(jprob, Ipopt.Optimizer, :ImplicitEuler) - osol2 = solve(oprob, ImplicitEuler(), dt = 0.01, adaptive = false) + jsol2 = @btime solve($jprob, Ipopt.Optimizer, :ImplicitEuler) # 63.031 ms, 26.49 MiB + osol2 = @btime solve($oprob, ImplicitEuler(), dt = 0.01, adaptive = false) # 129.375 μs, 61.91 KiB @test ≈(jsol2.sol.u, osol2.u, rtol = 0.001) iprob = InfiniteOptControlProblem(sys, u0map, tspan, parammap, dt = 0.01) - isol = solve(iprob, Ipopt.Optimizer, derivative_method = FiniteDifference(Backward())) + isol = @btime solve($iprob, Ipopt.Optimizer, derivative_method = FiniteDifference(Backward())) # 11.540 ms, 4.00 MiB # With a constraint u0map = Pair[] @@ -43,10 +44,16 @@ const M = ModelingToolkit jprob = JuMPControlProblem(lksys, u0map, tspan, parammap; guesses = guess, dt = 0.01) @test num_constraints(jprob.model) == 2 - jsol = solve(jprob, Ipopt.Optimizer, :Tsitouras5) + jsol = @btime solve($jprob, Ipopt.Optimizer, :Tsitouras5) # 12.190 s, 9.68 GiB sol = jsol.sol @test sol(0.6)[1] ≈ 3.5 @test sol(0.3)[1] ≈ 7.0 + + iprob = InfiniteOptControlProblem(lksys, u0map, tspan, parammap; guesses = guess, dt = 0.01) + isol = @btime solve($iprob, Ipopt.Optimizer, derivative_method = OrthogonalCollocation(3)) # 48.564 ms, 9.58 MiB + sol = isol.sol + @test sol(0.6)[1] ≈ 3.5 + @test sol(0.3)[1] ≈ 7.0 end #@testset "Optimal control: bees" begin From e67993973a87752b7e2f75abbfb81619ccac576b Mon Sep 17 00:00:00 2001 From: vyudu Date: Fri, 18 Apr 2025 05:25:46 -0400 Subject: [PATCH 1430/2176] fix merge --- ext/MTKJuMPControlExt.jl | 125 ++++++++++++++++++-------------- test/extensions/jump_control.jl | 14 ++-- 2 files changed, 81 insertions(+), 58 deletions(-) diff --git a/ext/MTKJuMPControlExt.jl b/ext/MTKJuMPControlExt.jl index 55c9dfc3c5..50bbc05151 100644 --- a/ext/MTKJuMPControlExt.jl +++ b/ext/MTKJuMPControlExt.jl @@ -5,32 +5,37 @@ using DiffEqDevTools, DiffEqBase, SciMLBase using LinearAlgebra const MTK = ModelingToolkit -abstract type AbstractOptimalControlProblem{uType, tType, isinplace} <: SciMLBase.AbstractODEProblem{uType, tType, isinplace} end - -struct JuMPControlProblem{uType, tType, isinplace, P, F, K} <: AbstractOptimalControlProblem{uType, tType, isinplace} - f::F - u0::uType - tspan::tType - p::P - model::InfiniteModel - kwargs::K - - function JuMPControlProblem(f, u0, tspan, p, model; kwargs...) - new{typeof(u0), typeof(tspan), SciMLBase.isinplace(f), typeof(p), typeof(f), typeof(kwargs)}(f, u0, tspan, p, model, kwargs) - end +abstract type AbstractOptimalControlProblem{uType, tType, isinplace} <: + SciMLBase.AbstractODEProblem{uType, tType, isinplace} end + +struct JuMPControlProblem{uType, tType, isinplace, P, F, K} <: + AbstractOptimalControlProblem{uType, tType, isinplace} + f::F + u0::uType + tspan::tType + p::P + model::InfiniteModel + kwargs::K + + function JuMPControlProblem(f, u0, tspan, p, model; kwargs...) + new{typeof(u0), typeof(tspan), SciMLBase.isinplace(f), + typeof(p), typeof(f), typeof(kwargs)}(f, u0, tspan, p, model, kwargs) + end end -struct InfiniteOptControlProblem{uType, tType, isinplace, P, F, K} <: AbstractOptimalControlProblem{uType, tType, isinplace} - f::F - u0::uType - tspan::tType - p::P - model::InfiniteModel - kwargs::K - - function InfiniteOptControlProblem(f, u0, tspan, p, model; kwargs...) - new{typeof(u0), typeof(tspan), SciMLBase.isinplace(f), typeof(p), typeof(f), typeof(kwargs)}(f, u0, tspan, p, model, kwargs) - end +struct InfiniteOptControlProblem{uType, tType, isinplace, P, F, K} <: + AbstractOptimalControlProblem{uType, tType, isinplace} + f::F + u0::uType + tspan::tType + p::P + model::InfiniteModel + kwargs::K + + function InfiniteOptControlProblem(f, u0, tspan, p, model; kwargs...) + new{typeof(u0), typeof(tspan), SciMLBase.isinplace(f), + typeof(p), typeof(f), typeof(kwargs)}(f, u0, tspan, p, model, kwargs) + end end """ @@ -48,7 +53,9 @@ The constraints are: - The set of user constraints passed to the ODESystem via `constraints` - The solver constraints that encode the time-stepping used by the solver """ -function MTK.JuMPControlProblem(sys::ODESystem, u0map, tspan, pmap; dt = error("dt must be provided for JuMPControlProblem."), guesses = Dict(), kwargs...) +function MTK.JuMPControlProblem(sys::ODESystem, u0map, tspan, pmap; + dt = error("dt must be provided for JuMPControlProblem."), + guesses = Dict(), kwargs...) _u0map = has_alg_eqs(sys) ? u0map : merge(Dict(u0map), Dict(guesses)) f, u0, p = MTK.process_SciMLProblem(ODEFunction, sys, _u0map, pmap; t = tspan !== nothing ? tspan[1] : tspan, kwargs...) @@ -67,7 +74,9 @@ of the interpolation arrays. Related to `JuMPControlProblem`, but directly adds the differential equations of the system as derivative constraints, rather than using a solver tableau. """ -function MTK.InfiniteOptControlProblem(sys::ODESystem, u0map, tspan, pmap; dt = error("dt must be provided for InfiniteOptControlProblem."), guesses = Dict(), kwargs...) +function MTK.InfiniteOptControlProblem(sys::ODESystem, u0map, tspan, pmap; + dt = error("dt must be provided for InfiniteOptControlProblem."), + guesses = Dict(), kwargs...) _u0map = has_alg_eqs(sys) ? u0map : merge(Dict(u0map), Dict(guesses)) f, u0, p = MTK.process_SciMLProblem(ODEFunction, sys, _u0map, pmap; t = tspan !== nothing ? tspan[1] : tspan, kwargs...) @@ -87,7 +96,7 @@ function init_model(sys, tsteps, u0map, u0) ctrls = controls(sys) states = unknowns(sys) model = InfiniteModel() - @infinite_parameter(model, t in [tsteps[1], tsteps[end]], num_supports = length(tsteps)) + @infinite_parameter(model, t in [tsteps[1], tsteps[end]], num_supports=length(tsteps)) @variable(model, U[i = 1:length(states)], Infinite(t)) @variable(model, V[1:length(ctrls)], Infinite(t)) @@ -95,7 +104,8 @@ function init_model(sys, tsteps, u0map, u0) add_user_constraints!(model, sys) stidxmap = Dict([v => i for (i, v) in enumerate(states)]) - u0_idxs = has_alg_eqs(sys) ? collect(1:length(states)) : [stidxmap[k] for (k, v) in u0map] + u0_idxs = has_alg_eqs(sys) ? collect(1:length(states)) : + [stidxmap[k] for (k, v) in u0map] add_initial_constraints!(model, u0, u0_idxs, tsteps[1]) return model end @@ -119,7 +129,7 @@ function add_jump_cost_function!(model, sys) subval = isequal(t, iv) ? model[:U][idx] : model[:U][idx](t) jcosts = map(c -> Symbolics.substitute(c, Dict(x(t) => subval)), jcosts) end - + for ct in controls(sys) p = operation(ct) t = only(arguments(ct)) @@ -127,7 +137,7 @@ function add_jump_cost_function!(model, sys) subval = isequal(t, iv) ? model[:V][idx] : model[:V][idx](t) jcosts = map(c -> Symbolics.substitute(c, Dict(p(t) => subval)), jcosts) end - + @objective(model, Min, consolidate(jcosts)) end @@ -153,23 +163,24 @@ function add_user_constraints!(model, sys) t = only(arguments(ct)) idx = cidxmap[p(iv)] subval = isequal(t, iv) ? model[:V][idx] : model[:V][idx](t) - jconstraints = map(c -> Symbolics.substitute(jconstraints, Dict(p(t) => subval)), jconstriants) + jconstraints = map( + c -> Symbolics.substitute(jconstraints, Dict(p(t) => subval)), jconstriants) end for (i, cons) in enumerate(jconstraints) if cons isa Equation - @constraint(model, cons.lhs - cons.rhs == 0, base_name = "user[$i]") - elseif cons.relational_op === Symbolics.geq - @constraint(model, cons.lhs - cons.rhs ≥ 0, base_name = "user[$i]") + @constraint(model, cons.lhs - cons.rhs==0, base_name="user[$i]") + elseif cons.relational_op === Symbolics.geq + @constraint(model, cons.lhs - cons.rhs≥0, base_name="user[$i]") else - @constraint(model, cons.lhs - cons.rhs ≤ 0, base_name = "user[$i]") + @constraint(model, cons.lhs - cons.rhs≤0, base_name="user[$i]") end end end function add_initial_constraints!(model, u0, u0_idxs, ts) U = model[:U] - @constraint(model, initial[i in u0_idxs], U[i](ts) == u0[i]) + @constraint(model, initial[i in u0_idxs], U[i](ts)==u0[i]) end is_explicit(tableau) = tableau isa DiffEqDevTools.ExplicitRKTableau @@ -193,12 +204,12 @@ function add_infopt_solve_constraints!(model, sys, pmap) diff_eqs = map(e -> Symbolics.substitute(e, submap), diff_eqs) diff_eqs = map(e -> Symbolics.substitute(e, diffsubmap), diff_eqs) end - @constraint(model, D[i = 1:length(diff_eqs)], diff_eqs[i].lhs == diff_eqs[i].rhs) + @constraint(model, D[i = 1:length(diff_eqs)], diff_eqs[i].lhs==diff_eqs[i].rhs) # Algebraic equations alg_eqs = alg_equations(sys) alg_eqs = map(e -> Symbolics.substitute(e, submap), alg_eqs) - @constraint(model, A[i = 1:length(alg_eqs)], alg_eqs[i].lhs == alg_eqs[i].rhs) + @constraint(model, A[i = 1:length(alg_eqs)], alg_eqs[i].lhs==alg_eqs[i].rhs) end function add_jump_solve_constraints!(prob, tableau) @@ -219,26 +230,29 @@ function add_jump_solve_constraints!(prob, tableau) K = Any[] for τ in tsteps for (i, h) in enumerate(c) - ΔU = sum([A[i, j] * K[j] for j in 1:i-1], init = zeros(nᵤ)) - Uₙ = [U[i](τ) + ΔU[i]*dt for i in 1:nᵤ] - Kₙ = f(Uₙ, p, τ + h*dt) + ΔU = sum([A[i, j] * K[j] for j in 1:(i - 1)], init = zeros(nᵤ)) + Uₙ = [U[i](τ) + ΔU[i] * dt for i in 1:nᵤ] + Kₙ = f(Uₙ, p, τ + h * dt) push!(K, Kₙ) end - ΔU = dt*sum([α[i] * K[i] for i in 1:length(α)]) - @constraint(model, [n = 1:nᵤ], U[n](τ) + ΔU[n] == U[n](τ + dt), base_name = "solve_time_$τ") + ΔU = dt * sum([α[i] * K[i] for i in 1:length(α)]) + @constraint(model, [n = 1:nᵤ], U[n](τ) + ΔU[n]==U[n](τ + dt), + base_name="solve_time_$τ") empty!(K) end else - @variable(model, K[1:length(α), 1:nᵤ], Infinite(t), start = tsteps[1]) + @variable(model, K[1:length(α), 1:nᵤ], Infinite(t), start=tsteps[1]) for τ in tsteps ΔUs = A * K for (i, h) in enumerate(c) ΔU = ΔUs[i, :] - Uₙ = [U[j] + ΔU[j]*dt for j in 1:nᵤ] - @constraint(model, [j in 1:nᵤ], K[i, j] == f(Uₙ, p, τ + h*dt)[j], DomainRestrictions(t => τ), base_name = "solve_K($τ)") + Uₙ = [U[j] + ΔU[j] * dt for j in 1:nᵤ] + @constraint(model, [j in 1:nᵤ], K[i, j]==f(Uₙ, p, τ + h * dt)[j], + DomainRestrictions(t => τ), base_name="solve_K($τ)") end - ΔU = dt*sum([α[i] * K[i, :] for i in 1:length(α)]) - @constraint(model, [n = 1:nᵤ], U[n] + ΔU[n] == U[n](τ + dt), DomainRestrictions(t => τ), base_name = "solve_U($τ)") + ΔU = dt * sum([α[i] * K[i, :] for i in 1:length(α)]) + @constraint(model, [n = 1:nᵤ], U[n] + ΔU[n]==U[n](τ + dt), + DomainRestrictions(t => τ), base_name="solve_U($τ)") end end end @@ -271,7 +285,7 @@ function DiffEqBase.solve(prob::JuMPControlProblem, jump_solver, ode_solver::Sym delete(model, con) end end - unregister(model, :K) + unregister(model, :K) for var in all_variables(model) if occursin("K", JuMP.name(var)) delete(model, var) @@ -284,7 +298,8 @@ end """ `derivative_method` kwarg refers to the method used by InfiniteOpt to compute derivatives. The list of possible options can be found at https://infiniteopt.github.io/InfiniteOpt.jl/stable/guide/derivative/. Defaults to FiniteDifference(Backward()). """ -function DiffEqBase.solve(prob::InfiniteOptControlProblem, jump_solver; derivative_method = InfiniteOpt.FiniteDifference(Backward())) +function DiffEqBase.solve(prob::InfiniteOptControlProblem, jump_solver; + derivative_method = InfiniteOpt.FiniteDifference(Backward())) set_derivative_method(prob.model[:t], derivative_method) _solve(prob, jump_solver, derivative_method) end @@ -296,7 +311,8 @@ function _solve(prob::AbstractOptimalControlProblem, jump_solver, solver) tstatus = termination_status(model) pstatus = primal_status(model) - !has_values(model) && error("Model not solvable; please report this to github.com/SciML/ModelingToolkit.jl.") + !has_values(model) && + error("Model not solvable; please report this to github.com/SciML/ModelingToolkit.jl.") ts = supports(model[:t]) U_vals = value.(model[:U]) @@ -310,9 +326,12 @@ function _solve(prob::AbstractOptimalControlProblem, jump_solver, solver) input_sol = DiffEqBase.build_solution(prob, solver, ts, V_vals) end - if !(pstatus === FEASIBLE_POINT && (tstatus === OPTIMAL || tstatus === LOCALLY_SOLVED || tstatus === ALMOST_OPTIMAL || tstatus === ALMOST_LOCALLY_SOLVED)) + if !(pstatus === FEASIBLE_POINT && + (tstatus === OPTIMAL || tstatus === LOCALLY_SOLVED || tstatus === ALMOST_OPTIMAL || + tstatus === ALMOST_LOCALLY_SOLVED)) sol = SciMLBase.solution_new_retcode(sol, SciMLBase.ReturnCode.ConvergenceFailure) - !isnothing(input_sol) && (input_sol = SciMLBase.solution_new_retcode(input_sol, SciMLBase.ReturnCode.ConvergenceFailure)) + !isnothing(input_sol) && (input_sol = SciMLBase.solution_new_retcode( + input_sol, SciMLBase.ReturnCode.ConvergenceFailure)) end JuMPControlSolution(model, sol, input_sol) diff --git a/test/extensions/jump_control.jl b/test/extensions/jump_control.jl index 2b01e612e3..7dd225aae0 100644 --- a/test/extensions/jump_control.jl +++ b/test/extensions/jump_control.jl @@ -3,7 +3,7 @@ using JuMP, InfiniteOpt using DiffEqDevTools, DiffEqBase using SimpleDiffEq using OrdinaryDiffEqSDIRK -using Ipopt +using Ipopt using BenchmarkTools const M = ModelingToolkit @@ -11,7 +11,8 @@ const M = ModelingToolkit # Test solving without anything attached. @parameters α=1.5 β=1.0 γ=3.0 δ=1.0 M.@variables x(..) y(..) - t = M.t_nounits; D = M.D_nounits + t = M.t_nounits + D = M.D_nounits eqs = [D(x(t)) ~ α * x(t) - β * x(t) * y(t), D(y(t)) ~ -γ * y(t) + δ * x(t) * y(t)] @@ -34,7 +35,8 @@ const M = ModelingToolkit osol2 = @btime solve($oprob, ImplicitEuler(), dt = 0.01, adaptive = false) # 129.375 μs, 61.91 KiB @test ≈(jsol2.sol.u, osol2.u, rtol = 0.001) iprob = InfiniteOptControlProblem(sys, u0map, tspan, parammap, dt = 0.01) - isol = @btime solve($iprob, Ipopt.Optimizer, derivative_method = FiniteDifference(Backward())) # 11.540 ms, 4.00 MiB + isol = @btime solve( + $iprob, Ipopt.Optimizer, derivative_method = FiniteDifference(Backward())) # 11.540 ms, 4.00 MiB # With a constraint u0map = Pair[] @@ -49,8 +51,10 @@ const M = ModelingToolkit @test sol(0.6)[1] ≈ 3.5 @test sol(0.3)[1] ≈ 7.0 - iprob = InfiniteOptControlProblem(lksys, u0map, tspan, parammap; guesses = guess, dt = 0.01) - isol = @btime solve($iprob, Ipopt.Optimizer, derivative_method = OrthogonalCollocation(3)) # 48.564 ms, 9.58 MiB + iprob = InfiniteOptControlProblem( + lksys, u0map, tspan, parammap; guesses = guess, dt = 0.01) + isol = @btime solve( + $iprob, Ipopt.Optimizer, derivative_method = OrthogonalCollocation(3)) # 48.564 ms, 9.58 MiB sol = isol.sol @test sol(0.6)[1] ≈ 3.5 @test sol(0.3)[1] ≈ 7.0 From c005848f282fd5535c8d0c3b719771daea7b9a4c Mon Sep 17 00:00:00 2001 From: vyudu Date: Thu, 10 Apr 2025 17:51:58 -0400 Subject: [PATCH 1431/2176] refactor: add optimal control interface file --- ext/MTKJuMPControlExt.jl | 31 ++++++------------------ src/ModelingToolkit.jl | 7 +++--- src/systems/optimal_control_interface.jl | 21 ++++++++++++++++ 3 files changed, 32 insertions(+), 27 deletions(-) create mode 100644 src/systems/optimal_control_interface.jl diff --git a/ext/MTKJuMPControlExt.jl b/ext/MTKJuMPControlExt.jl index 50bbc05151..091391caaa 100644 --- a/ext/MTKJuMPControlExt.jl +++ b/ext/MTKJuMPControlExt.jl @@ -1,13 +1,10 @@ module MTKJuMPControlExt using ModelingToolkit using JuMP, InfiniteOpt -using DiffEqDevTools, DiffEqBase, SciMLBase +using DiffEqDevTools, DiffEqBase using LinearAlgebra const MTK = ModelingToolkit -abstract type AbstractOptimalControlProblem{uType, tType, isinplace} <: - SciMLBase.AbstractODEProblem{uType, tType, isinplace} end - struct JuMPControlProblem{uType, tType, isinplace, P, F, K} <: AbstractOptimalControlProblem{uType, tType, isinplace} f::F @@ -56,6 +53,7 @@ The constraints are: function MTK.JuMPControlProblem(sys::ODESystem, u0map, tspan, pmap; dt = error("dt must be provided for JuMPControlProblem."), guesses = Dict(), kwargs...) + MTK.warn_overdetermined(sys, u0map) _u0map = has_alg_eqs(sys) ? u0map : merge(Dict(u0map), Dict(guesses)) f, u0, p = MTK.process_SciMLProblem(ODEFunction, sys, _u0map, pmap; t = tspan !== nothing ? tspan[1] : tspan, kwargs...) @@ -77,6 +75,7 @@ of the system as derivative constraints, rather than using a solver tableau. function MTK.InfiniteOptControlProblem(sys::ODESystem, u0map, tspan, pmap; dt = error("dt must be provided for InfiniteOptControlProblem."), guesses = Dict(), kwargs...) + MTK.warn_overdetermined(sys, u0map) _u0map = has_alg_eqs(sys) ? u0map : merge(Dict(u0map), Dict(guesses)) f, u0, p = MTK.process_SciMLProblem(ODEFunction, sys, _u0map, pmap; t = tspan !== nothing ? tspan[1] : tspan, kwargs...) @@ -87,12 +86,6 @@ function MTK.InfiniteOptControlProblem(sys::ODESystem, u0map, tspan, pmap; end function init_model(sys, tsteps, u0map, u0) - constraintsys = MTK.get_constraintsystem(sys) - if !isnothing(constraintsys) - (length(constraints(constraintsys)) + length(u0map) > length(unknowns(sys))) && - @warn "The control problem is overdetermined. The total number of conditions (# constraints + # fixed initial values given by u0map) exceeds the total number of states. The solvers will default to doing a nonlinear least-squares optimization." - end - ctrls = controls(sys) states = unknowns(sys) model = InfiniteModel() @@ -110,7 +103,7 @@ function init_model(sys, tsteps, u0map, u0) return model end -function add_jump_cost_function!(model, sys) +function add_jump_cost_function!(model::InfiniteModel, sys) jcosts = MTK.get_costs(sys) consolidate = MTK.get_consolidate(sys) if isnothing(jcosts) || isempty(jcosts) @@ -141,7 +134,7 @@ function add_jump_cost_function!(model, sys) @objective(model, Min, consolidate(jcosts)) end -function add_user_constraints!(model, sys) +function add_user_constraints!(model::InfiniteModel, sys) conssys = MTK.get_constraintsystem(sys) jconstraints = isnothing(conssys) ? nothing : MTK.get_constraints(conssys) (isnothing(jconstraints) || isempty(jconstraints)) && return nothing @@ -178,14 +171,14 @@ function add_user_constraints!(model, sys) end end -function add_initial_constraints!(model, u0, u0_idxs, ts) +function add_initial_constraints!(model::InfiniteModel, u0, u0_idxs, ts) U = model[:U] @constraint(model, initial[i in u0_idxs], U[i](ts)==u0[i]) end is_explicit(tableau) = tableau isa DiffEqDevTools.ExplicitRKTableau -function add_infopt_solve_constraints!(model, sys, pmap) +function add_infopt_solve_constraints!(model::InfiniteModel, sys, pmap) iv = MTK.get_iv(sys) t = model[:t] U = model[:U] @@ -257,14 +250,6 @@ function add_jump_solve_constraints!(prob, tableau) end end -""" -""" -struct JuMPControlSolution - model::InfiniteModel - sol::ODESolution - input_sol::Union{Nothing, ODESolution} -end - """ Solve JuMPControlProblem. Arguments: - prob: a JumpControlProblem @@ -334,7 +319,7 @@ function _solve(prob::AbstractOptimalControlProblem, jump_solver, solver) input_sol, SciMLBase.ReturnCode.ConvergenceFailure)) end - JuMPControlSolution(model, sol, input_sol) + OptimalControlSolution(model, sol, input_sol) end end diff --git a/src/ModelingToolkit.jl b/src/ModelingToolkit.jl index b2808854bd..deb78d2a2c 100644 --- a/src/ModelingToolkit.jl +++ b/src/ModelingToolkit.jl @@ -348,9 +348,8 @@ export AnalysisPoint, get_sensitivity_function, get_comp_sensitivity_function, open_loop function FMIComponent end -function JuMPControlProblem end -export JuMPControlProblem -function InfiniteOptControlProblem end -export InfiniteOptControlProblem +include("src/systems/optimal_control_interface.jl") +export JuMPControlProblem, InfiniteOptControlProblem, PyomoControlProblem, CasADiControlProblem +export OptimalControlSolution end # module diff --git a/src/systems/optimal_control_interface.jl b/src/systems/optimal_control_interface.jl new file mode 100644 index 0000000000..d93cb4cfb7 --- /dev/null +++ b/src/systems/optimal_control_interface.jl @@ -0,0 +1,21 @@ +abstract type AbstractOptimalControlProblem{uType, tType, isinplace} <: + SciMLBase.AbstractODEProblem{uType, tType, isinplace} end + +struct OptimalControlSolution + model::Any + sol::ODESolution + input_sol::Union{Nothing, ODESolution} +end + +function JuMPControlProblem end +function InfiniteOptControlProblem end +function CasADiControlProblem end +function PyomoControlProblem end + +function warn_overdetermined(sys, u0map) + constraintsys = get_constraintsystem(sys) + if !isnothing(constraintsys) + (length(constraints(constraintsys)) + length(u0map) > length(unknowns(sys))) && + @warn "The control problem is overdetermined. The total number of conditions (# constraints + # fixed initial values given by u0map) exceeds the total number of states. The solvers will default to doing a nonlinear least-squares optimization." + end +end From a4dc0222f2bf7a8ce9ef48b85dfc7c25987bc594 Mon Sep 17 00:00:00 2001 From: vyudu Date: Thu, 10 Apr 2025 18:03:55 -0400 Subject: [PATCH 1432/2176] add set_silent option --- ext/MTKJuMPControlExt.jl | 5 ++++- src/ModelingToolkit.jl | 4 ++-- test/extensions/jump_control.jl | 8 ++++---- 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/ext/MTKJuMPControlExt.jl b/ext/MTKJuMPControlExt.jl index 091391caaa..f4b7298945 100644 --- a/ext/MTKJuMPControlExt.jl +++ b/ext/MTKJuMPControlExt.jl @@ -255,13 +255,15 @@ Solve JuMPControlProblem. Arguments: - prob: a JumpControlProblem - jump_solver: a LP solver such as HiGHS - ode_solver: Takes in a symbol representing the solver. Acceptable solvers may be found at https://docs.sciml.ai/DiffEqDevDocs/stable/internals/tableaus/. Note that the symbol may be different than the typical name of the solver, e.g. :Tsitouras5 rather than Tsit5. +- silent: set the model silent (suppress model output) Returns a JuMPControlSolution, which contains both the model and the ODE solution. """ -function DiffEqBase.solve(prob::JuMPControlProblem, jump_solver, ode_solver::Symbol) +function DiffEqBase.solve(prob::JuMPControlProblem, jump_solver, ode_solver::Symbol; silent = false) model = prob.model tableau_getter = Symbol(:construct, ode_solver) @eval tableau = $tableau_getter() + silent && set_silent(model) # Unregister current solver constraints for con in all_constraints(model) @@ -285,6 +287,7 @@ end """ function DiffEqBase.solve(prob::InfiniteOptControlProblem, jump_solver; derivative_method = InfiniteOpt.FiniteDifference(Backward())) + silent && set_silent(model) set_derivative_method(prob.model[:t], derivative_method) _solve(prob, jump_solver, derivative_method) end diff --git a/src/ModelingToolkit.jl b/src/ModelingToolkit.jl index deb78d2a2c..61276956af 100644 --- a/src/ModelingToolkit.jl +++ b/src/ModelingToolkit.jl @@ -348,8 +348,8 @@ export AnalysisPoint, get_sensitivity_function, get_comp_sensitivity_function, open_loop function FMIComponent end -include("src/systems/optimal_control_interface.jl") -export JuMPControlProblem, InfiniteOptControlProblem, PyomoControlProblem, CasADiControlProblem +include("systems/optimal_control_interface.jl") +export AbstractOptimalControlProblem, JuMPControlProblem, InfiniteOptControlProblem, PyomoControlProblem, CasADiControlProblem export OptimalControlSolution end # module diff --git a/test/extensions/jump_control.jl b/test/extensions/jump_control.jl index 7dd225aae0..7e63edae24 100644 --- a/test/extensions/jump_control.jl +++ b/test/extensions/jump_control.jl @@ -31,12 +31,12 @@ const M = ModelingToolkit @test jsol.sol.u ≈ osol.u # Implicit method. - jsol2 = @btime solve($jprob, Ipopt.Optimizer, :ImplicitEuler) # 63.031 ms, 26.49 MiB + jsol2 = @btime solve($jprob, Ipopt.Optimizer, :ImplicitEuler, silent = true) # 63.031 ms, 26.49 MiB osol2 = @btime solve($oprob, ImplicitEuler(), dt = 0.01, adaptive = false) # 129.375 μs, 61.91 KiB @test ≈(jsol2.sol.u, osol2.u, rtol = 0.001) iprob = InfiniteOptControlProblem(sys, u0map, tspan, parammap, dt = 0.01) isol = @btime solve( - $iprob, Ipopt.Optimizer, derivative_method = FiniteDifference(Backward())) # 11.540 ms, 4.00 MiB + $iprob, Ipopt.Optimizer, derivative_method = FiniteDifference(Backward()), silent = true) # 11.540 ms, 4.00 MiB # With a constraint u0map = Pair[] @@ -46,7 +46,7 @@ const M = ModelingToolkit jprob = JuMPControlProblem(lksys, u0map, tspan, parammap; guesses = guess, dt = 0.01) @test num_constraints(jprob.model) == 2 - jsol = @btime solve($jprob, Ipopt.Optimizer, :Tsitouras5) # 12.190 s, 9.68 GiB + jsol = @btime solve($jprob, Ipopt.Optimizer, :Tsitouras5, silent = true) # 12.190 s, 9.68 GiB sol = jsol.sol @test sol(0.6)[1] ≈ 3.5 @test sol(0.3)[1] ≈ 7.0 @@ -54,7 +54,7 @@ const M = ModelingToolkit iprob = InfiniteOptControlProblem( lksys, u0map, tspan, parammap; guesses = guess, dt = 0.01) isol = @btime solve( - $iprob, Ipopt.Optimizer, derivative_method = OrthogonalCollocation(3)) # 48.564 ms, 9.58 MiB + $iprob, Ipopt.Optimizer, derivative_method = OrthogonalCollocation(3), silent = true) # 48.564 ms, 9.58 MiB sol = isol.sol @test sol(0.6)[1] ≈ 3.5 @test sol(0.3)[1] ≈ 7.0 From bec273957716a0e12d27a773c1919b0852138e8e Mon Sep 17 00:00:00 2001 From: vyudu Date: Thu, 10 Apr 2025 18:04:13 -0400 Subject: [PATCH 1433/2176] format --- ext/MTKJuMPControlExt.jl | 3 ++- src/ModelingToolkit.jl | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/ext/MTKJuMPControlExt.jl b/ext/MTKJuMPControlExt.jl index f4b7298945..45e64fa8c8 100644 --- a/ext/MTKJuMPControlExt.jl +++ b/ext/MTKJuMPControlExt.jl @@ -259,7 +259,8 @@ Solve JuMPControlProblem. Arguments: Returns a JuMPControlSolution, which contains both the model and the ODE solution. """ -function DiffEqBase.solve(prob::JuMPControlProblem, jump_solver, ode_solver::Symbol; silent = false) +function DiffEqBase.solve( + prob::JuMPControlProblem, jump_solver, ode_solver::Symbol; silent = false) model = prob.model tableau_getter = Symbol(:construct, ode_solver) @eval tableau = $tableau_getter() diff --git a/src/ModelingToolkit.jl b/src/ModelingToolkit.jl index 61276956af..d8c3434801 100644 --- a/src/ModelingToolkit.jl +++ b/src/ModelingToolkit.jl @@ -349,7 +349,8 @@ export AnalysisPoint, get_sensitivity_function, get_comp_sensitivity_function, function FMIComponent end include("systems/optimal_control_interface.jl") -export AbstractOptimalControlProblem, JuMPControlProblem, InfiniteOptControlProblem, PyomoControlProblem, CasADiControlProblem +export AbstractOptimalControlProblem, JuMPControlProblem, InfiniteOptControlProblem, + PyomoControlProblem, CasADiControlProblem export OptimalControlSolution end # module From 81d32e1c8995429996dfbf16b22fa1706f3279d4 Mon Sep 17 00:00:00 2001 From: vyudu Date: Wed, 16 Apr 2025 15:26:25 -0400 Subject: [PATCH 1434/2176] partial: add free final time and bounds-handling --- ext/MTKJuMPControlExt.jl | 33 +++++++-- src/systems/optimal_control_interface.jl | 6 ++ src/systems/problem_utils.jl | 1 + test/extensions/jump_control.jl | 88 ++++++++++++++++++------ 4 files changed, 99 insertions(+), 29 deletions(-) diff --git a/ext/MTKJuMPControlExt.jl b/ext/MTKJuMPControlExt.jl index 45e64fa8c8..a178280428 100644 --- a/ext/MTKJuMPControlExt.jl +++ b/ext/MTKJuMPControlExt.jl @@ -55,8 +55,11 @@ function MTK.JuMPControlProblem(sys::ODESystem, u0map, tspan, pmap; guesses = Dict(), kwargs...) MTK.warn_overdetermined(sys, u0map) _u0map = has_alg_eqs(sys) ? u0map : merge(Dict(u0map), Dict(guesses)) + @show _u0map f, u0, p = MTK.process_SciMLProblem(ODEFunction, sys, _u0map, pmap; t = tspan !== nothing ? tspan[1] : tspan, kwargs...) + + (f_i, f_o) = generate_control_function(sys) model = init_model(sys, tspan[1]:dt:tspan[2], u0map, u0) JuMPControlProblem(f, u0, tspan, p, model, kwargs...) @@ -86,13 +89,14 @@ function MTK.InfiniteOptControlProblem(sys::ODESystem, u0map, tspan, pmap; end function init_model(sys, tsteps, u0map, u0) - ctrls = controls(sys) + ctrls = MTK.unbound_inputs(sys) states = unknowns(sys) model = InfiniteModel() @infinite_parameter(model, t in [tsteps[1], tsteps[end]], num_supports=length(tsteps)) @variable(model, U[i = 1:length(states)], Infinite(t)) @variable(model, V[1:length(ctrls)], Infinite(t)) + set_bounds!(model, sys) add_jump_cost_function!(model, sys) add_user_constraints!(model, sys) @@ -103,6 +107,22 @@ function init_model(sys, tsteps, u0map, u0) return model end +function set_bounds!(model, sys) + U = model[:U] + for (i, u) in enumerate(unknowns(sys)) + lo, hi = MTK.getbounds(u) + set_lower_bound(U[i], lo) + set_upper_bound(U[i], hi) + end + + V = model[:V] + for (i, v) in enumerate(MTK.unbound_inputs(sys)) + lo, hi = MTK.getbounds(v) + set_lower_bound(V[i], lo) + set_upper_bound(V[i], hi) + end +end + function add_jump_cost_function!(model::InfiniteModel, sys) jcosts = MTK.get_costs(sys) consolidate = MTK.get_consolidate(sys) @@ -113,7 +133,7 @@ function add_jump_cost_function!(model::InfiniteModel, sys) iv = MTK.get_iv(sys) stidxmap = Dict([v => i for (i, v) in enumerate(unknowns(sys))]) - cidxmap = Dict([v => i for (i, v) in enumerate(controls(sys))]) + cidxmap = Dict([v => i for (i, v) in enumerate(MTK.unbound_inputs(sys))]) for st in unknowns(sys) x = operation(st) @@ -123,7 +143,7 @@ function add_jump_cost_function!(model::InfiniteModel, sys) jcosts = map(c -> Symbolics.substitute(c, Dict(x(t) => subval)), jcosts) end - for ct in controls(sys) + for ct in MTK.unbound_inputs(sys) p = operation(ct) t = only(arguments(ct)) idx = cidxmap[p(iv)] @@ -141,7 +161,7 @@ function add_user_constraints!(model::InfiniteModel, sys) iv = MTK.get_iv(sys) stidxmap = Dict([v => i for (i, v) in enumerate(unknowns(sys))]) - cidxmap = Dict([v => i for (i, v) in enumerate(controls(sys))]) + cidxmap = Dict([v => i for (i, v) in enumerate(MTK.unbound_inputs(sys))]) for st in unknowns(conssys) x = operation(st) @@ -151,7 +171,7 @@ function add_user_constraints!(model::InfiniteModel, sys) jconstraints = map(c -> Symbolics.substitute(c, Dict(x(t) => subval)), jconstraints) end - for ct in controls(sys) + for ct in MTK.unbound_inputs(sys) p = operation(ct) t = only(arguments(ct)) idx = cidxmap[p(iv)] @@ -185,9 +205,8 @@ function add_infopt_solve_constraints!(model::InfiniteModel, sys, pmap) V = model[:V] stmap = Dict([v => U[i] for (i, v) in enumerate(unknowns(sys))]) - ctrlmap = Dict([v => V[i] for (i, v) in enumerate(controls(sys))]) + ctrlmap = Dict([v => V[i] for (i, v) in enumerate(MTK.unbound_inputs(sys))]) submap = merge(stmap, ctrlmap, Dict(pmap)) - @show submap # Differential equations diff_eqs = diff_equations(sys) diff --git a/src/systems/optimal_control_interface.jl b/src/systems/optimal_control_interface.jl index d93cb4cfb7..f8e9c75887 100644 --- a/src/systems/optimal_control_interface.jl +++ b/src/systems/optimal_control_interface.jl @@ -19,3 +19,9 @@ function warn_overdetermined(sys, u0map) @warn "The control problem is overdetermined. The total number of conditions (# constraints + # fixed initial values given by u0map) exceeds the total number of states. The solvers will default to doing a nonlinear least-squares optimization." end end + +""" +IntegralNorm. When applied to an expression. +""" +struct IntegralNorm end + diff --git a/src/systems/problem_utils.jl b/src/systems/problem_utils.jl index 47bf3c678d..1e2e4811e2 100644 --- a/src/systems/problem_utils.jl +++ b/src/systems/problem_utils.jl @@ -872,6 +872,7 @@ function maybe_build_initialization_problem( t = zero(floatT) end + @show u0map initializeprob = ModelingToolkit.InitializationProblem{true, SciMLBase.FullSpecialize}( sys, t, u0map, pmap; guesses, initialization_eqs, use_scc, kwargs...) if state_values(initializeprob) !== nothing diff --git a/test/extensions/jump_control.jl b/test/extensions/jump_control.jl index 7e63edae24..ee9ccd3a07 100644 --- a/test/extensions/jump_control.jl +++ b/test/extensions/jump_control.jl @@ -1,26 +1,27 @@ using ModelingToolkit -using JuMP, InfiniteOpt +import JuMP, InfiniteOpt using DiffEqDevTools, DiffEqBase using SimpleDiffEq using OrdinaryDiffEqSDIRK using Ipopt using BenchmarkTools +using CairoMakie const M = ModelingToolkit @testset "ODE Solution, no cost" begin # Test solving without anything attached. @parameters α=1.5 β=1.0 γ=3.0 δ=1.0 - M.@variables x(..) y(..) + @variables x(..) y(..) t = M.t_nounits D = M.D_nounits eqs = [D(x(t)) ~ α * x(t) - β * x(t) * y(t), D(y(t)) ~ -γ * y(t) + δ * x(t) * y(t)] + @mtkbuild sys = ODESystem(eqs, t) tspan = (0.0, 1.0) u0map = [x(t) => 4.0, y(t) => 2.0] parammap = [α => 1.5, β => 1.0, γ => 3.0, δ => 1.0] - @mtkbuild sys = ODESystem(eqs, t) # Test explicit method. jprob = JuMPControlProblem(sys, u0map, tspan, parammap, dt = 0.01) @@ -58,27 +59,70 @@ const M = ModelingToolkit sol = isol.sol @test sol(0.6)[1] ≈ 3.5 @test sol(0.3)[1] ≈ 7.0 + + # Test whole-interval constraints + constr = [x(t) > 3, y(t) > 4] + @mtkbuild lksys = ODESystem(eqs, t; constraints = constr) + iprob = InfiniteOptControlProblem( + lksys, u0map, tspan, parammap; guesses = guess, dt = 0.01) + isol = @btime solve( + $iprob, Ipopt.Optimizer, derivative_method = OrthogonalCollocation(3), silent = true) # 48.564 ms, 9.58 MiB + sol = isol.sol + @test all(u -> u .> [3, 4], sol.u) + + jprob = JuMPControlProblem(lksys, u0map, tspan, parammap; guesses = guess, dt = 0.01) + jsol = @btime solve($jprob, Ipopt.Optimizer, :RadauIA3, silent = true) # 12.190 s, 9.68 GiB + sol = jsol.sol + @test all(u -> u .> [3, 4], sol.u) end -#@testset "Optimal control: bees" begin -# # Example from Lawrence Evans' notes -# M.@variables w(..) q(..) -# M.@parameters α(t) [bounds = [0, 1]] b c μ s ν -# -# tspan = (0, 4) -# eqs = [D(w(t)) ~ -μ*w(t) + b*s*α*w(t), -# D(q(t)) ~ -ν*q(t) + c*(1 - α)*s*w(t)] -# costs = [-q(tspan[2])] -# -# @mtkbuild beesys = ODESystem(eqs, t; costs) -# u0map = [w(0) => 40, q(0) => 2] -# pmap = [b => 1, c => 1, μ => 1, s => 1, ν => 1] -# -# jprob = JuMPControlProblem(beesys, u0map, tspan, pmap) -# jsol = solve(jprob, Ipopt.Optimizer, :Tsitouras5) -# control_sol = jsol.control_sol -# # Bang-bang control -#end +@testset "Linear systems" begin + function is_bangbang(input_sol, lbounds, ubounds) + bangbang = true + for v in 1:length(input_sol.u[1]) + all(i -> i[v] ≈ bounds[v] || i[v] ≈ bounds[u], input_sol.u) || (bangbang = false) + end + bangbang + end + + # Double integrator + @variables x(..) [bounds = (0., 0.25)] v(..) + @variables u(t) [bounds = (-1., 1.), input = true] + constr = [v(1.0) ~ 0.0] + cost = [-x(1.0)] # Optimize the final distance. + @named block = ODESystem([D(x(t)) ~ v(t), D(v(t)) ~ u], t) + block, input_idxs = structural_simplify(block, ([u],[])) + + u0map = [x(t) => 0., v(t) => 0.] + tspan = (0., 1.) + parammap = [u => 0.] + jprob = JuMPControlProblem(block, u0map, tspan, parammap; dt = 0.01) + jsol = solve(jprob, Ipopt.Optimizer, :Verner8) + # Linear systems have bang-bang controls + @test is_bangbang(jsol.input_sol, [-1.], [1.]) + # Test reached final position. + @test jsol.sol.u[end][1] ≈ 0.25 + + # Cart-pole system + + # Bee example (from Lawrence Evans' notes) + M.@variables w(..) q(..) + M.@parameters α(t) [bounds = [0, 1]] b c μ s ν + + tspan = (0, 4) + eqs = [D(w(t)) ~ -μ*w(t) + b*s*α*w(t), + D(q(t)) ~ -ν*q(t) + c*(1 - α)*s*w(t)] + costs = [-q(tspan[2])] + + @mtkbuild beesys = ODESystem(eqs, t; costs) + u0map = [w(0) => 40, q(0) => 2] + pmap = [b => 1, c => 1, μ => 1, s => 1, ν => 1] + + jprob = JuMPControlProblem(beesys, u0map, tspan, pmap) + jsol = solve(jprob, Ipopt.Optimizer, :Tsitouras5) + control_sol = jsol.control_sol + # Bang-bang control +end # #@testset "Constrained optimal control problems" begin #end From 300154288a4674a2edd81157ee1d1c544fcbf8fe Mon Sep 17 00:00:00 2001 From: vyudu Date: Wed, 16 Apr 2025 18:29:34 -0400 Subject: [PATCH 1435/2176] implement ControlFunction --- ext/MTKJuMPControlExt.jl | 16 +-- src/inputoutput.jl | 8 +- src/systems/diffeqs/abstractodesystem.jl | 2 +- src/systems/optimal_control_interface.jl | 126 +++++++++++++++++++++++ test/extensions/jump_control.jl | 6 +- 5 files changed, 143 insertions(+), 15 deletions(-) diff --git a/ext/MTKJuMPControlExt.jl b/ext/MTKJuMPControlExt.jl index a178280428..701443785c 100644 --- a/ext/MTKJuMPControlExt.jl +++ b/ext/MTKJuMPControlExt.jl @@ -15,7 +15,7 @@ struct JuMPControlProblem{uType, tType, isinplace, P, F, K} <: kwargs::K function JuMPControlProblem(f, u0, tspan, p, model; kwargs...) - new{typeof(u0), typeof(tspan), SciMLBase.isinplace(f), + new{typeof(u0), typeof(tspan), SciMLBase.isinplace(f, 5), typeof(p), typeof(f), typeof(kwargs)}(f, u0, tspan, p, model, kwargs) end end @@ -55,13 +55,10 @@ function MTK.JuMPControlProblem(sys::ODESystem, u0map, tspan, pmap; guesses = Dict(), kwargs...) MTK.warn_overdetermined(sys, u0map) _u0map = has_alg_eqs(sys) ? u0map : merge(Dict(u0map), Dict(guesses)) - @show _u0map - f, u0, p = MTK.process_SciMLProblem(ODEFunction, sys, _u0map, pmap; + f, u0, p = MTK.process_SciMLProblem(ControlFunction, sys, _u0map, pmap; t = tspan !== nothing ? tspan[1] : tspan, kwargs...) - (f_i, f_o) = generate_control_function(sys) model = init_model(sys, tspan[1]:dt:tspan[2], u0map, u0) - JuMPControlProblem(f, u0, tspan, p, model, kwargs...) end @@ -80,7 +77,7 @@ function MTK.InfiniteOptControlProblem(sys::ODESystem, u0map, tspan, pmap; guesses = Dict(), kwargs...) MTK.warn_overdetermined(sys, u0map) _u0map = has_alg_eqs(sys) ? u0map : merge(Dict(u0map), Dict(guesses)) - f, u0, p = MTK.process_SciMLProblem(ODEFunction, sys, _u0map, pmap; + f, u0, p = MTK.process_SciMLProblem(ControlFunction, sys, _u0map, pmap; t = tspan !== nothing ? tspan[1] : tspan, kwargs...) model = init_model(sys, tspan[1]:dt:tspan[2], u0map, u0) @@ -237,14 +234,17 @@ function add_jump_solve_constraints!(prob, tableau) dt = tsteps[2] - tsteps[1] U = model[:U] + V = model[:V] nᵤ = length(U) + nᵥ = length(V) if is_explicit(tableau) K = Any[] for τ in tsteps for (i, h) in enumerate(c) ΔU = sum([A[i, j] * K[j] for j in 1:(i - 1)], init = zeros(nᵤ)) Uₙ = [U[i](τ) + ΔU[i] * dt for i in 1:nᵤ] - Kₙ = f(Uₙ, p, τ + h * dt) + Vₙ = [V[i](τ) for i in 1:nᵥ] + Kₙ = f(Uₙ, Vₙ, p, τ + h * dt) push!(K, Kₙ) end ΔU = dt * sum([α[i] * K[i] for i in 1:length(α)]) @@ -259,7 +259,7 @@ function add_jump_solve_constraints!(prob, tableau) for (i, h) in enumerate(c) ΔU = ΔUs[i, :] Uₙ = [U[j] + ΔU[j] * dt for j in 1:nᵤ] - @constraint(model, [j in 1:nᵤ], K[i, j]==f(Uₙ, p, τ + h * dt)[j], + @constraint(model, [j in 1:nᵤ], K[i, j]==f(Uₙ, V, p, τ + h * dt)[j], DomainRestrictions(t => τ), base_name="solve_K($τ)") end ΔU = dt * sum([α[i] * K[i, :] for i in 1:length(α)]) diff --git a/src/inputoutput.jl b/src/inputoutput.jl index 5f9420ff3a..329a76d9c4 100644 --- a/src/inputoutput.jl +++ b/src/inputoutput.jl @@ -208,7 +208,9 @@ function generate_control_function(sys::AbstractODESystem, inputs = unbound_inpu inputs = [inputs; disturbance_inputs] end - sys, _ = io_preprocessing(sys, inputs, []; simplify, kwargs...) + if !iscomplete(sys) + sys, _ = io_preprocessing(sys, inputs, []; simplify, kwargs...) + end dvs = unknowns(sys) ps = parameters(sys; initial_parameters = true) @@ -250,9 +252,7 @@ function generate_control_function(sys::AbstractODESystem, inputs = unbound_inpu f = build_function_wrapper(sys, rhss, args...; p_start = 3 + implicit_dae, p_end = length(p) + 2 + implicit_dae) f = eval_or_rgf.(f; eval_expression, eval_module) - f = GeneratedFunctionWrapper{( - 3 + implicit_dae, length(args) - length(p) + 1, is_split(sys))}(f...) - f = f, f + f = GeneratedFunctionWrapper{(3, length(args) - length(p) + 1, is_split(sys))}(f...) ps = setdiff(parameters(sys), inputs, disturbance_inputs) (; f, dvs, ps, io_sys = sys) end diff --git a/src/systems/diffeqs/abstractodesystem.jl b/src/systems/diffeqs/abstractodesystem.jl index d9e8b05eeb..e66f03a85e 100644 --- a/src/systems/diffeqs/abstractodesystem.jl +++ b/src/systems/diffeqs/abstractodesystem.jl @@ -101,7 +101,7 @@ function calculate_control_jacobian(sys::AbstractODESystem; end rhs = [eq.rhs for eq in full_equations(sys)] - ctrls = controls(sys) + ctrls = unbound_inputs(sys) if sparse jac = sparsejacobian(rhs, ctrls, simplify = simplify) diff --git a/src/systems/optimal_control_interface.jl b/src/systems/optimal_control_interface.jl index f8e9c75887..fd7adfe643 100644 --- a/src/systems/optimal_control_interface.jl +++ b/src/systems/optimal_control_interface.jl @@ -20,8 +20,134 @@ function warn_overdetermined(sys, u0map) end end +""" +Generate the control function f(x, u, p, t) from the ODESystem. +Input variables are automatically inferred but can be manually specified. +""" +function SciMLBase.ControlFunction{iip, specialize}(sys::ODESystem, + dvs = unknowns(sys), + ps = parameters(sys), u0 = nothing, + inputs = unbound_inputs(sys), + disturbance_inputs = disturbances(sys); + version = nothing, tgrad = false, + jac = false, controljac = false, + p = nothing, t = nothing, + eval_expression = false, + sparse = false, simplify = false, + eval_module = @__MODULE__, + steady_state = false, + checkbounds = false, + sparsity = false, + analytic = nothing, + split_idxs = nothing, + initialization_data = nothing, + cse = true, + kwargs...) where {iip, specialize} + + (f), _, _ = generate_control_function(sys, inputs, disturbance_inputs; eval_expression = true, eval_module, cse, kwargs...) + + if tgrad + tgrad_gen = generate_tgrad(sys, dvs, ps; + simplify = simplify, + expression = Val{true}, + expression_module = eval_module, cse, + checkbounds = checkbounds, kwargs...) + tgrad_oop, tgrad_iip = eval_or_rgf.(tgrad_gen; eval_expression, eval_module) + _tgrad = GeneratedFunctionWrapper{(2, 3, is_split(sys))}(tgrad_oop, tgrad_iip) + else + _tgrad = nothing + end + + if jac + jac_gen = generate_jacobian(sys, dvs, ps; + simplify = simplify, sparse = sparse, + expression = Val{true}, + expression_module = eval_module, cse, + checkbounds = checkbounds, kwargs...) + jac_oop, jac_iip = eval_or_rgf.(jac_gen; eval_expression, eval_module) + + _jac = GeneratedFunctionWrapper{(2, 3, is_split(sys))}(jac_oop, jac_iip) + else + _jac = nothing + end + + if controljac + cjac_gen = generate_control_jacobian(sys, dvs, ps; + simplify = simplify, sparse = sparse, + expression = Val{true}, + expression_module = eval_module, cse, + checkbounds = checkbounds, kwargs...) + cjac_oop, cjac_iip = eval_or_rgf.(cjac_gen; eval_expression, eval_module) + + _cjac = GeneratedFunctionWrapper{(2, 3, is_split(sys))}(cjac_oop, cjac_iip) + else + _cjac = nothing + end + + M = calculate_massmatrix(sys) + _M = if sparse && !(u0 === nothing || M === I) + SparseArrays.sparse(M) + elseif u0 === nothing || M === I + M + else + ArrayInterface.restructure(u0 .* u0', M) + end + + observedfun = ObservedFunctionCache( + sys; steady_state, eval_expression, eval_module, checkbounds, cse) + + if sparse + uElType = u0 === nothing ? Float64 : eltype(u0) + W_prototype = similar(W_sparsity(sys), uElType) + controljac_prototype = similar(calculate_control_jacobian(sys), uElType) + else + W_prototype = nothing + controljac_prototype = nothing + end + + ControlFunction{iip, specialize}(f; + sys = sys, + jac = _jac === nothing ? nothing : _jac, + controljac = _cjac === nothing ? nothing : _cjac, + tgrad = _tgrad === nothing ? nothing : _tgrad, + mass_matrix = _M, + jac_prototype = W_prototype, + controljac_prototype = controljac_prototype, + observed = observedfun, + sparsity = sparsity ? W_sparsity(sys) : nothing, + analytic = analytic, + initialization_data) +end + +function SciMLBase.ControlFunction(sys::AbstractODESystem, args...; kwargs...) + ControlFunction{true}(sys, args...; kwargs...) +end + +function SciMLBase.ControlFunction{true}(sys::AbstractODESystem, args...; + kwargs...) + ControlFunction{true, SciMLBase.AutoSpecialize}(sys, args...; kwargs...) +end + +function SciMLBase.ControlFunction{false}(sys::AbstractODESystem, args...; + kwargs...) + ControlFunction{false, SciMLBase.FullSpecialize}(sys, args...; kwargs...) +end + """ IntegralNorm. When applied to an expression. """ struct IntegralNorm end +""" +$(SIGNATURES) + +Define one or more inputs. + +See also [`@independent_variables`](@ref), [`@variables`](@ref) and [`@constants`](@ref). +""" +macro inputs(xs...) + Symbolics._parse_vars(:inputs, + Real, + xs, + toparam) |> esc +end diff --git a/test/extensions/jump_control.jl b/test/extensions/jump_control.jl index ee9ccd3a07..5ae1e98f11 100644 --- a/test/extensions/jump_control.jl +++ b/test/extensions/jump_control.jl @@ -86,6 +86,8 @@ end end # Double integrator + t = M.t_nounits + D = M.D_nounits @variables x(..) [bounds = (0., 0.25)] v(..) @variables u(t) [bounds = (-1., 1.), input = true] constr = [v(1.0) ~ 0.0] @@ -106,8 +108,8 @@ end # Cart-pole system # Bee example (from Lawrence Evans' notes) - M.@variables w(..) q(..) - M.@parameters α(t) [bounds = [0, 1]] b c μ s ν + @variables w(..) q(..) + @parameters α(t) [bounds = [0, 1]] b c μ s ν tspan = (0, 4) eqs = [D(w(t)) ~ -μ*w(t) + b*s*α*w(t), From 0a1afce9977a249d989e9b0b427325e1ab3152ea Mon Sep 17 00:00:00 2001 From: vyudu Date: Thu, 17 Apr 2025 17:51:03 -0400 Subject: [PATCH 1436/2176] feat: working linear control problems --- ext/MTKJuMPControlExt.jl | 112 ++++++++++------------- src/systems/optimal_control_interface.jl | 10 +- src/systems/problem_utils.jl | 1 - test/extensions/jump_control.jl | 91 ++++++++++++++---- 4 files changed, 130 insertions(+), 84 deletions(-) diff --git a/ext/MTKJuMPControlExt.jl b/ext/MTKJuMPControlExt.jl index 701443785c..054f0ecb10 100644 --- a/ext/MTKJuMPControlExt.jl +++ b/ext/MTKJuMPControlExt.jl @@ -58,7 +58,7 @@ function MTK.JuMPControlProblem(sys::ODESystem, u0map, tspan, pmap; f, u0, p = MTK.process_SciMLProblem(ControlFunction, sys, _u0map, pmap; t = tspan !== nothing ? tspan[1] : tspan, kwargs...) - model = init_model(sys, tspan[1]:dt:tspan[2], u0map, u0) + model = init_model(sys, tspan[1]:dt:tspan[2], u0map, pmap, u0) JuMPControlProblem(f, u0, tspan, p, model, kwargs...) end @@ -80,22 +80,23 @@ function MTK.InfiniteOptControlProblem(sys::ODESystem, u0map, tspan, pmap; f, u0, p = MTK.process_SciMLProblem(ControlFunction, sys, _u0map, pmap; t = tspan !== nothing ? tspan[1] : tspan, kwargs...) - model = init_model(sys, tspan[1]:dt:tspan[2], u0map, u0) + model = init_model(sys, tspan[1]:dt:tspan[2], u0map, pmap, u0) add_infopt_solve_constraints!(model, sys, pmap) InfiniteOptControlProblem(f, u0, tspan, p, model, kwargs...) end -function init_model(sys, tsteps, u0map, u0) +function init_model(sys, tsteps, u0map, pmap, u0) ctrls = MTK.unbound_inputs(sys) states = unknowns(sys) model = InfiniteModel() + @infinite_parameter(model, t in [tsteps[1], tsteps[end]], num_supports=length(tsteps)) @variable(model, U[i = 1:length(states)], Infinite(t)) @variable(model, V[1:length(ctrls)], Infinite(t)) set_bounds!(model, sys) - add_jump_cost_function!(model, sys) - add_user_constraints!(model, sys) + add_jump_cost_function!(model, sys, (tsteps[1], tsteps[2]), pmap) + add_user_constraints!(model, sys, pmap) stidxmap = Dict([v => i for (i, v) in enumerate(states)]) u0_idxs = has_alg_eqs(sys) ? collect(1:length(states)) : @@ -120,63 +121,35 @@ function set_bounds!(model, sys) end end -function add_jump_cost_function!(model::InfiniteModel, sys) +function add_jump_cost_function!(model::InfiniteModel, sys, tspan, pmap) jcosts = MTK.get_costs(sys) consolidate = MTK.get_consolidate(sys) if isnothing(jcosts) || isempty(jcosts) @objective(model, Min, 0) return end - iv = MTK.get_iv(sys) - - stidxmap = Dict([v => i for (i, v) in enumerate(unknowns(sys))]) - cidxmap = Dict([v => i for (i, v) in enumerate(MTK.unbound_inputs(sys))]) - - for st in unknowns(sys) - x = operation(st) - t = only(arguments(st)) - idx = stidxmap[x(iv)] - subval = isequal(t, iv) ? model[:U][idx] : model[:U][idx](t) - jcosts = map(c -> Symbolics.substitute(c, Dict(x(t) => subval)), jcosts) - end + jcosts = substitute_jump_vars(model, sys, pmap, jcosts) - for ct in MTK.unbound_inputs(sys) - p = operation(ct) - t = only(arguments(ct)) - idx = cidxmap[p(iv)] - subval = isequal(t, iv) ? model[:V][idx] : model[:V][idx](t) - jcosts = map(c -> Symbolics.substitute(c, Dict(p(t) => subval)), jcosts) + # Substitute integral + iv = MTK.get_iv(sys) + jcosts = map(c -> Symbolics.substitute(c, ∫ => Symbolics.Integral(iv in tspan)), jcosts) + intmap = Dict() + + for int in MTK.collect_applied_operators(jcosts, Symbolics.Integral) + arg = only(arguments(MTK.value(int))) + lower_bound, upper_bound = (int.domain.domain.left, int.domain.domain.right) + intmap[int] = InfiniteOpt.∫(arg, iv; lower_bound, upper_bound) end - + jcosts = map(c -> Symbolics.substitute(c, intmap), jcosts) @objective(model, Min, consolidate(jcosts)) end -function add_user_constraints!(model::InfiniteModel, sys) +function add_user_constraints!(model::InfiniteModel, sys, pmap) conssys = MTK.get_constraintsystem(sys) jconstraints = isnothing(conssys) ? nothing : MTK.get_constraints(conssys) (isnothing(jconstraints) || isempty(jconstraints)) && return nothing - iv = MTK.get_iv(sys) - stidxmap = Dict([v => i for (i, v) in enumerate(unknowns(sys))]) - cidxmap = Dict([v => i for (i, v) in enumerate(MTK.unbound_inputs(sys))]) - - for st in unknowns(conssys) - x = operation(st) - t = only(arguments(st)) - idx = stidxmap[x(iv)] - subval = isequal(t, iv) ? model[:U][idx] : model[:U][idx](t) - jconstraints = map(c -> Symbolics.substitute(c, Dict(x(t) => subval)), jconstraints) - end - - for ct in MTK.unbound_inputs(sys) - p = operation(ct) - t = only(arguments(ct)) - idx = cidxmap[p(iv)] - subval = isequal(t, iv) ? model[:V][idx] : model[:V][idx](t) - jconstraints = map( - c -> Symbolics.substitute(jconstraints, Dict(p(t) => subval)), jconstriants) - end - + jconstraints = substitute_jump_vars(model, sys, pmap, jconstraints) for (i, cons) in enumerate(jconstraints) if cons isa Equation @constraint(model, cons.lhs - cons.rhs==0, base_name="user[$i]") @@ -193,31 +166,41 @@ function add_initial_constraints!(model::InfiniteModel, u0, u0_idxs, ts) @constraint(model, initial[i in u0_idxs], U[i](ts)==u0[i]) end -is_explicit(tableau) = tableau isa DiffEqDevTools.ExplicitRKTableau - -function add_infopt_solve_constraints!(model::InfiniteModel, sys, pmap) +function substitute_jump_vars(model, sys, pmap, exprs) iv = MTK.get_iv(sys) - t = model[:t] + sts = unknowns(sys) + cts = MTK.unbound_inputs(sys) U = model[:U] V = model[:V] + # for variables like x(t) + whole_interval_map = Dict([[v => U[i] for (i, v) in enumerate(sts)]; [v => V[i] for (i, v) in enumerate(cts)]]) + exprs = map(c -> Symbolics.substitute(c, whole_interval_map), exprs) + + # for variables like x(1.0) + x_ops = [MTK.operation(MTK.unwrap(st)) for st in sts] + c_ops = [MTK.operation(MTK.unwrap(ct)) for ct in cts] + fixed_t_map = Dict([[x_ops[i] => U[i] for i in 1:length(U)]; [c_ops[i] => V[i] for i in 1:length(V)]]) + exprs = map(c -> Symbolics.substitute(c, fixed_t_map), exprs) + + exprs = map(c -> Symbolics.substitute(c, Dict(pmap)), exprs) + exprs +end - stmap = Dict([v => U[i] for (i, v) in enumerate(unknowns(sys))]) - ctrlmap = Dict([v => V[i] for (i, v) in enumerate(MTK.unbound_inputs(sys))]) - submap = merge(stmap, ctrlmap, Dict(pmap)) +is_explicit(tableau) = tableau isa DiffEqDevTools.ExplicitRKTableau +function add_infopt_solve_constraints!(model::InfiniteModel, sys, pmap) # Differential equations - diff_eqs = diff_equations(sys) - D = Differential(iv) + U = model[:U] + t = model[:t] + D = Differential(MTK.get_iv(sys)) diffsubmap = Dict([D(U[i]) => ∂(U[i], t) for i in 1:length(U)]) - for u in unknowns(sys) - diff_eqs = map(e -> Symbolics.substitute(e, submap), diff_eqs) - diff_eqs = map(e -> Symbolics.substitute(e, diffsubmap), diff_eqs) - end + + diff_eqs = substitute_jump_vars(model, sys, pmap, diff_equations(sys)) + diff_eqs = map(e -> Symbolics.substitute(e, diffsubmap), diff_eqs) @constraint(model, D[i = 1:length(diff_eqs)], diff_eqs[i].lhs==diff_eqs[i].rhs) # Algebraic equations - alg_eqs = alg_equations(sys) - alg_eqs = map(e -> Symbolics.substitute(e, submap), alg_eqs) + alg_eqs = substitute_jump_vars(model, sys, pmap, alg_equations(sys)) @constraint(model, A[i = 1:length(alg_eqs)], alg_eqs[i].lhs==alg_eqs[i].rhs) end @@ -306,9 +289,10 @@ end `derivative_method` kwarg refers to the method used by InfiniteOpt to compute derivatives. The list of possible options can be found at https://infiniteopt.github.io/InfiniteOpt.jl/stable/guide/derivative/. Defaults to FiniteDifference(Backward()). """ function DiffEqBase.solve(prob::InfiniteOptControlProblem, jump_solver; - derivative_method = InfiniteOpt.FiniteDifference(Backward())) + derivative_method = InfiniteOpt.FiniteDifference(Backward()), silent = false) + model = prob.model silent && set_silent(model) - set_derivative_method(prob.model[:t], derivative_method) + set_derivative_method(model[:t], derivative_method) _solve(prob, jump_solver, derivative_method) end diff --git a/src/systems/optimal_control_interface.jl b/src/systems/optimal_control_interface.jl index fd7adfe643..d6cc7de320 100644 --- a/src/systems/optimal_control_interface.jl +++ b/src/systems/optimal_control_interface.jl @@ -134,9 +134,15 @@ function SciMLBase.ControlFunction{false}(sys::AbstractODESystem, args...; end """ -IntegralNorm. When applied to an expression. +IntegralNorm. When applied to an expression in a cost +function, assumes that the integration variable is the +iv of the system, and assumes that the bounds are the +tspan. +Equivalent to Integral(t in tspan) in Symbolics. """ -struct IntegralNorm end +struct ∫ <: Symbolics.Operator end +∫(x) = ∫()(x) +Base.show(io::IO, x::∫) = print(io, "∫") """ $(SIGNATURES) diff --git a/src/systems/problem_utils.jl b/src/systems/problem_utils.jl index 1e2e4811e2..47bf3c678d 100644 --- a/src/systems/problem_utils.jl +++ b/src/systems/problem_utils.jl @@ -872,7 +872,6 @@ function maybe_build_initialization_problem( t = zero(floatT) end - @show u0map initializeprob = ModelingToolkit.InitializationProblem{true, SciMLBase.FullSpecialize}( sys, t, u0map, pmap; guesses, initialization_eqs, use_scc, kwargs...) if state_values(initializeprob) !== nothing diff --git a/test/extensions/jump_control.jl b/test/extensions/jump_control.jl index 5ae1e98f11..47e962ab9a 100644 --- a/test/extensions/jump_control.jl +++ b/test/extensions/jump_control.jl @@ -77,10 +77,10 @@ const M = ModelingToolkit end @testset "Linear systems" begin - function is_bangbang(input_sol, lbounds, ubounds) + function is_bangbang(input_sol, lbounds, ubounds, rtol = 1e-4) bangbang = true - for v in 1:length(input_sol.u[1]) - all(i -> i[v] ≈ bounds[v] || i[v] ≈ bounds[u], input_sol.u) || (bangbang = false) + for v in 1:length(input_sol.u[1]) - 1 + all(i -> ≈(i[v], bounds[v]; rtol) || ≈(i[v], bounds[u]; rtol), input_sol.u) || (bangbang = false) end bangbang end @@ -91,8 +91,8 @@ end @variables x(..) [bounds = (0., 0.25)] v(..) @variables u(t) [bounds = (-1., 1.), input = true] constr = [v(1.0) ~ 0.0] - cost = [-x(1.0)] # Optimize the final distance. - @named block = ODESystem([D(x(t)) ~ v(t), D(v(t)) ~ u], t) + cost = [-x(1.0)] # Maximize the final distance. + @named block = ODESystem([D(x(t)) ~ v(t), D(v(t)) ~ u], t; costs = cost, constraints = constr) block, input_idxs = structural_simplify(block, ([u],[])) u0map = [x(t) => 0., v(t) => 0.] @@ -103,28 +103,85 @@ end # Linear systems have bang-bang controls @test is_bangbang(jsol.input_sol, [-1.], [1.]) # Test reached final position. - @test jsol.sol.u[end][1] ≈ 0.25 + @test ≈(jsol.sol.u[end][1], 0.25, rtol = 1e-5) - # Cart-pole system + iprob = InfiniteOptControlProblem(block, u0map, tspan, parammap; dt = 0.01) + isol = solve(iprob, Ipopt.Optimizer; silent = true) + @test is_bangbang(isol.input_sol, [-1.], [1.]) + @test ≈(isol.sol.u[end][1], 0.25, rtol = 1e-5) - # Bee example (from Lawrence Evans' notes) - @variables w(..) q(..) - @parameters α(t) [bounds = [0, 1]] b c μ s ν + ################### + ### Bee example ### + ################### + # From Lawrence Evans' notes + @variables w(..) q(..) α(t) [input = true, bounds = (0, 1)] + @parameters b c μ s ν tspan = (0, 4) eqs = [D(w(t)) ~ -μ*w(t) + b*s*α*w(t), D(q(t)) ~ -ν*q(t) + c*(1 - α)*s*w(t)] costs = [-q(tspan[2])] - @mtkbuild beesys = ODESystem(eqs, t; costs) - u0map = [w(0) => 40, q(0) => 2] - pmap = [b => 1, c => 1, μ => 1, s => 1, ν => 1] + @named beesys = ODESystem(eqs, t; costs) + beesys, input_idxs = structural_simplify(beesys, ([α],[])) + u0map = [w(t) => 40, q(t) => 2] + pmap = [b => 1, c => 1, μ => 1, s => 1, ν => 1, α => 1] - jprob = JuMPControlProblem(beesys, u0map, tspan, pmap) + jprob = JuMPControlProblem(beesys, u0map, tspan, pmap, dt = 0.01) jsol = solve(jprob, Ipopt.Optimizer, :Tsitouras5) - control_sol = jsol.control_sol - # Bang-bang control + @test is_bangbang(jsol.input_sol, [0.], [1.]) + iprob = InfiniteOptControlProblem(beesys, u0map, tspan, pmap, dt = 0.01) + isol = solve(jprob, Ipopt.Optimizer, :Tsitouras5) + @test is_bangbang(isol.input_sol, [0.], [1.]) end -# + +@testset "Rocket launch" begin + t = M.t_nounits + D = M.D_nounits + + @variables h(..) v(..) m(..) T(..) [input = true, bounds = (0, tₘ)] + @parameters h_c m₀ h₀ g₀ D_c c Tₘ + @parameters tf + drag(h, v) = D_c * v^2 * exp(-h_c * (h - h₀) / h₀) + gravity(h) = g₀ * (h₀ / h) + + eqs = [D(h(t)) ~ v(t), + D(v(t)) ~ (T(t) - drag(h(t), v(t))) / m(t) - gravity(t), + D(m(t)) ~ -T(t) / c] + + costs = [-h(tf)] + constraints = [T(tf) ~ 0] + @named rocket = ODESystem(eqs, t; costs, constraints) + @test tf ∈ Set(parameters(rocket)) + + u0map = [h(t) => h₀, m(t) => m₀, v(t) => 0] + pmap = [g₀ => 1, m₀ => 1.0, h_c => 500, c => 0.5*√(g₀*h₀), D_C => 0.5 * 620 * m₀/g₀, Tₘ => 3.5*g₀*m₀] + jprob = JuMPControlProblem(rocket, u0map, (0, tf), pmap) + jsol = solve(jprob, Ipopt.Optimizer, :RadauIA3) + @test jsol.sol.u[end][1] ≈ 1.012 +end + +@testset "Free final time problem" begin + t = M.t_nounits + D = M.D_nounits + + @variables x(..) u(..) [input = true, bounds = (0,1)] + @parameters tf + eqs = [D(x(t)) ~ -2 + 0.5*u] + + # Integral cost function + costs = [∫(x-u), x(tf)] + consolidate(u) = u[1] + u[2] + jprob = JuMPControlProblem(rocket, u0map, (0, tf), pmap) + jsol = solve(jprob, Ipopt.Optimizer, :RadauIA3) + @test jsol.sol.t[end] ≈ 10.0 + iprob = InfiniteOptControlProblem(rocket, u0map, (0, tf), pmap) + isol = solve(iprob, Ipopt.Optimizer, :RadauIA3) + @test isol.sol.t[end] ≈ 10.0 +end + +@testset "Cart-pole problem" begin +end + #@testset "Constrained optimal control problems" begin #end From d470df65975431194ba087ecc4f48325570868c3 Mon Sep 17 00:00:00 2001 From: vyudu Date: Fri, 18 Apr 2025 05:23:42 -0400 Subject: [PATCH 1437/2176] feat: free final time problems --- ext/MTKJuMPControlExt.jl | 127 +++++++++++++++-------- src/ModelingToolkit.jl | 1 + src/inputoutput.jl | 2 +- src/systems/diffeqs/odesystem.jl | 2 +- src/systems/optimal_control_interface.jl | 62 ++++++++--- src/variables.jl | 5 + test/extensions/jump_control.jl | 44 ++++---- 7 files changed, 165 insertions(+), 78 deletions(-) diff --git a/ext/MTKJuMPControlExt.jl b/ext/MTKJuMPControlExt.jl index 054f0ecb10..310a632292 100644 --- a/ext/MTKJuMPControlExt.jl +++ b/ext/MTKJuMPControlExt.jl @@ -3,6 +3,7 @@ using ModelingToolkit using JuMP, InfiniteOpt using DiffEqDevTools, DiffEqBase using LinearAlgebra +using StaticArrays const MTK = ModelingToolkit struct JuMPControlProblem{uType, tType, isinplace, P, F, K} <: @@ -14,7 +15,7 @@ struct JuMPControlProblem{uType, tType, isinplace, P, F, K} <: model::InfiniteModel kwargs::K - function JuMPControlProblem(f, u0, tspan, p, model; kwargs...) + function JuMPControlProblem(f, u0, tspan, p, model, kwargs...) new{typeof(u0), typeof(tspan), SciMLBase.isinplace(f, 5), typeof(p), typeof(f), typeof(kwargs)}(f, u0, tspan, p, model, kwargs) end @@ -51,14 +52,18 @@ The constraints are: - The solver constraints that encode the time-stepping used by the solver """ function MTK.JuMPControlProblem(sys::ODESystem, u0map, tspan, pmap; - dt = error("dt must be provided for JuMPControlProblem."), + dt = nothing, + steps = nothing, guesses = Dict(), kwargs...) MTK.warn_overdetermined(sys, u0map) _u0map = has_alg_eqs(sys) ? u0map : merge(Dict(u0map), Dict(guesses)) f, u0, p = MTK.process_SciMLProblem(ControlFunction, sys, _u0map, pmap; t = tspan !== nothing ? tspan[1] : tspan, kwargs...) - model = init_model(sys, tspan[1]:dt:tspan[2], u0map, pmap, u0) + pmap = MTK.todict(pmap) + steps, is_free_t = MTK.process_tspan(tspan, dt, steps) + model = init_model(sys, tspan, steps, u0map, pmap, u0; is_free_t) + JuMPControlProblem(f, u0, tspan, p, model, kwargs...) end @@ -73,55 +78,78 @@ Related to `JuMPControlProblem`, but directly adds the differential equations of the system as derivative constraints, rather than using a solver tableau. """ function MTK.InfiniteOptControlProblem(sys::ODESystem, u0map, tspan, pmap; - dt = error("dt must be provided for InfiniteOptControlProblem."), + dt = nothing, + steps = nothing, guesses = Dict(), kwargs...) MTK.warn_overdetermined(sys, u0map) _u0map = has_alg_eqs(sys) ? u0map : merge(Dict(u0map), Dict(guesses)) f, u0, p = MTK.process_SciMLProblem(ControlFunction, sys, _u0map, pmap; t = tspan !== nothing ? tspan[1] : tspan, kwargs...) - model = init_model(sys, tspan[1]:dt:tspan[2], u0map, pmap, u0) - add_infopt_solve_constraints!(model, sys, pmap) + pmap = MTK.todict(pmap) + steps, is_free_t = MTK.process_tspan(tspan, dt, steps) + model = init_model(sys, tspan, steps, u0map, pmap, u0; is_free_t) + + add_infopt_solve_constraints!(model, sys, pmap; is_free_t) InfiniteOptControlProblem(f, u0, tspan, p, model, kwargs...) end -function init_model(sys, tsteps, u0map, pmap, u0) +# Initialize InfiniteOpt model. +function init_model(sys, tspan, steps, u0map, pmap, u0; is_free_t = false) ctrls = MTK.unbound_inputs(sys) states = unknowns(sys) model = InfiniteModel() - @infinite_parameter(model, t in [tsteps[1], tsteps[end]], num_supports=length(tsteps)) - @variable(model, U[i = 1:length(states)], Infinite(t)) - @variable(model, V[1:length(ctrls)], Infinite(t)) + if is_free_t + (ts_sym, te_sym) = tspan + @variable(model, tf, start = pmap[te_sym]) + hasbounds(te_sym) && begin + lo, hi = getbounds(te_sym) + set_lower_bound(tf, lo) + set_upper_bound(tf, hi) + end + pmap[ts_sym] = 0 + pmap[te_sym] = 1 + tspan = (0, 1) + end + + @infinite_parameter(model, t in [tspan[1], tspan[2]], num_supports = steps) + @variable(model, U[i = 1:length(states)], Infinite(t), start = u0[i]) + c0 = [pmap[c] for c in ctrls] + @variable(model, V[i = 1:length(ctrls)], Infinite(t), start = c0[i]) - set_bounds!(model, sys) - add_jump_cost_function!(model, sys, (tsteps[1], tsteps[2]), pmap) - add_user_constraints!(model, sys, pmap) + set_jump_bounds!(model, sys, pmap) + add_jump_cost_function!(model, sys, (tspan[1], tspan[2]), pmap; is_free_t) + add_user_constraints!(model, sys, pmap; is_free_t) stidxmap = Dict([v => i for (i, v) in enumerate(states)]) u0_idxs = has_alg_eqs(sys) ? collect(1:length(states)) : [stidxmap[k] for (k, v) in u0map] - add_initial_constraints!(model, u0, u0_idxs, tsteps[1]) + add_initial_constraints!(model, u0, u0_idxs, tspan[1]) return model end -function set_bounds!(model, sys) +function set_jump_bounds!(model, sys, pmap) U = model[:U] for (i, u) in enumerate(unknowns(sys)) - lo, hi = MTK.getbounds(u) - set_lower_bound(U[i], lo) - set_upper_bound(U[i], hi) + if MTK.hasbounds(u) + lo, hi = MTK.getbounds(u) + set_lower_bound(U[i], Symbolics.fixpoint_sub(lo, pmap)) + set_upper_bound(U[i], Symbolics.fixpoint_sub(hi, pmap)) + end end V = model[:V] for (i, v) in enumerate(MTK.unbound_inputs(sys)) - lo, hi = MTK.getbounds(v) - set_lower_bound(V[i], lo) - set_upper_bound(V[i], hi) + if MTK.hasbounds(v) + lo, hi = MTK.getbounds(v) + set_lower_bound(V[i], Symbolics.fixpoint_sub(lo, pmap)) + set_upper_bound(V[i], Symbolics.fixpoint_sub(hi, pmap)) + end end end -function add_jump_cost_function!(model::InfiniteModel, sys, tspan, pmap) +function add_jump_cost_function!(model::InfiniteModel, sys, tspan, pmap; is_free_t = false) jcosts = MTK.get_costs(sys) consolidate = MTK.get_consolidate(sys) if isnothing(jcosts) || isempty(jcosts) @@ -129,26 +157,36 @@ function add_jump_cost_function!(model::InfiniteModel, sys, tspan, pmap) return end jcosts = substitute_jump_vars(model, sys, pmap, jcosts) + tₛ = is_free_t ? model[:tf] : 1 # Substitute integral iv = MTK.get_iv(sys) - jcosts = map(c -> Symbolics.substitute(c, ∫ => Symbolics.Integral(iv in tspan)), jcosts) + jcosts = map(c -> Symbolics.substitute(c, MTK.∫() => Symbolics.Integral(iv in tspan)), jcosts) + intmap = Dict() - for int in MTK.collect_applied_operators(jcosts, Symbolics.Integral) + op = MTK.operation(int) arg = only(arguments(MTK.value(int))) - lower_bound, upper_bound = (int.domain.domain.left, int.domain.domain.right) - intmap[int] = InfiniteOpt.∫(arg, iv; lower_bound, upper_bound) + lo, hi = (op.domain.domain.left, op.domain.domain.right) + intmap[int] = tₛ * InfiniteOpt.∫(arg, model[:t], lo, hi) end jcosts = map(c -> Symbolics.substitute(c, intmap), jcosts) @objective(model, Min, consolidate(jcosts)) end -function add_user_constraints!(model::InfiniteModel, sys, pmap) +function add_user_constraints!(model::InfiniteModel, sys, pmap; is_free_t = false) conssys = MTK.get_constraintsystem(sys) jconstraints = isnothing(conssys) ? nothing : MTK.get_constraints(conssys) (isnothing(jconstraints) || isempty(jconstraints)) && return nothing + if is_free_t + for u in MTK.get_unknowns(conssys) + x = MTK.operation(u) + t = only(arguments(u)) + MTK.symbolic_type(t) === NotSymbolic() && error("Provided specific time constraint in a free final time problem. This is not supported by the JuMP/InfiniteOpt collocation solvers. The offending variable is $u.") + end + end + jconstraints = substitute_jump_vars(model, sys, pmap, jconstraints) for (i, cons) in enumerate(jconstraints) if cons isa Equation @@ -188,23 +226,24 @@ end is_explicit(tableau) = tableau isa DiffEqDevTools.ExplicitRKTableau -function add_infopt_solve_constraints!(model::InfiniteModel, sys, pmap) +function add_infopt_solve_constraints!(model::InfiniteModel, sys, pmap; is_free_t = false) # Differential equations U = model[:U] t = model[:t] D = Differential(MTK.get_iv(sys)) diffsubmap = Dict([D(U[i]) => ∂(U[i], t) for i in 1:length(U)]) + tₛ = is_free_t ? model[:tf] : 1 diff_eqs = substitute_jump_vars(model, sys, pmap, diff_equations(sys)) diff_eqs = map(e -> Symbolics.substitute(e, diffsubmap), diff_eqs) - @constraint(model, D[i = 1:length(diff_eqs)], diff_eqs[i].lhs==diff_eqs[i].rhs) + @constraint(model, D[i = 1:length(diff_eqs)], diff_eqs[i].lhs == tₛ * diff_eqs[i].rhs) # Algebraic equations alg_eqs = substitute_jump_vars(model, sys, pmap, alg_equations(sys)) - @constraint(model, A[i = 1:length(alg_eqs)], alg_eqs[i].lhs==alg_eqs[i].rhs) + @constraint(model, A[i = 1:length(alg_eqs)], alg_eqs[i].lhs == alg_eqs[i].rhs) end -function add_jump_solve_constraints!(prob, tableau) +function add_jump_solve_constraints!(prob, tableau; is_free_t = false) A = tableau.A α = tableau.α c = tableau.c @@ -214,6 +253,7 @@ function add_jump_solve_constraints!(prob, tableau) t = model[:t] tsteps = supports(model[:t]) pop!(tsteps) + tₛ = is_free_t ? model[:tf] : 1 dt = tsteps[2] - tsteps[1] U = model[:U] @@ -227,7 +267,7 @@ function add_jump_solve_constraints!(prob, tableau) ΔU = sum([A[i, j] * K[j] for j in 1:(i - 1)], init = zeros(nᵤ)) Uₙ = [U[i](τ) + ΔU[i] * dt for i in 1:nᵤ] Vₙ = [V[i](τ) for i in 1:nᵥ] - Kₙ = f(Uₙ, Vₙ, p, τ + h * dt) + Kₙ = tₛ * f(Uₙ, Vₙ, p, τ + h * dt) # scale the time push!(K, Kₙ) end ΔU = dt * sum([α[i] * K[i] for i in 1:length(α)]) @@ -237,17 +277,17 @@ function add_jump_solve_constraints!(prob, tableau) end else @variable(model, K[1:length(α), 1:nᵤ], Infinite(t), start=tsteps[1]) + ΔUs = A * K + ΔU_tot = dt * (K' * α) for τ in tsteps - ΔUs = A * K for (i, h) in enumerate(c) - ΔU = ΔUs[i, :] - Uₙ = [U[j] + ΔU[j] * dt for j in 1:nᵤ] - @constraint(model, [j in 1:nᵤ], K[i, j]==f(Uₙ, V, p, τ + h * dt)[j], - DomainRestrictions(t => τ), base_name="solve_K($τ)") + ΔU = @view ΔUs[i, :] + Uₙ = U + ΔU * dt + @constraint(model, [j = 1:nᵤ], K[i, j](τ) == tₛ * f(Uₙ, V, p, τ + h * dt)[j], + DomainRestrictions(t => τ + h*dt), base_name="solve_K($τ)") end - ΔU = dt * sum([α[i] * K[i, :] for i in 1:length(α)]) - @constraint(model, [n = 1:nᵤ], U[n] + ΔU[n]==U[n](τ + dt), - DomainRestrictions(t => τ), base_name="solve_U($τ)") + @constraint(model, [n = 1:nᵤ], U[n](τ) + ΔU_tot[n] == U[n](τ + dt), + DomainRestrictions(t => τ), base_name="solve_U($τ)") end end end @@ -281,7 +321,7 @@ function DiffEqBase.solve( delete(model, var) end end - add_jump_solve_constraints!(prob, tableau) + add_jump_solve_constraints!(prob, tableau; is_free_t = haskey(model, :tf)) _solve(prob, jump_solver, ode_solver) end @@ -304,9 +344,10 @@ function _solve(prob::AbstractOptimalControlProblem, jump_solver, solver) tstatus = termination_status(model) pstatus = primal_status(model) !has_values(model) && - error("Model not solvable; please report this to github.com/SciML/ModelingToolkit.jl.") + error("Model not solvable; please report this to github.com/SciML/ModelingToolkit.jl with a MWE.") - ts = supports(model[:t]) + tf = haskey(model, :tf) ? value(model[:tf]) : 1 + ts = tf * supports(model[:t]) U_vals = value.(model[:U]) U_vals = [[U_vals[i][j] for i in 1:length(U_vals)] for j in 1:length(ts)] sol = DiffEqBase.build_solution(prob, solver, ts, U_vals) diff --git a/src/ModelingToolkit.jl b/src/ModelingToolkit.jl index d8c3434801..2fb7c41b0d 100644 --- a/src/ModelingToolkit.jl +++ b/src/ModelingToolkit.jl @@ -352,5 +352,6 @@ include("systems/optimal_control_interface.jl") export AbstractOptimalControlProblem, JuMPControlProblem, InfiniteOptControlProblem, PyomoControlProblem, CasADiControlProblem export OptimalControlSolution +export ∫ end # module diff --git a/src/inputoutput.jl b/src/inputoutput.jl index 329a76d9c4..11a58bd30a 100644 --- a/src/inputoutput.jl +++ b/src/inputoutput.jl @@ -250,7 +250,7 @@ function generate_control_function(sys::AbstractODESystem, inputs = unbound_inpu args = (ddvs, args...) end f = build_function_wrapper(sys, rhss, args...; p_start = 3 + implicit_dae, - p_end = length(p) + 2 + implicit_dae) + p_end = length(p) + 2 + implicit_dae, kwargs...) f = eval_or_rgf.(f; eval_expression, eval_module) f = GeneratedFunctionWrapper{(3, length(args) - length(p) + 1, is_split(sys))}(f...) ps = setdiff(parameters(sys), inputs, disturbance_inputs) diff --git a/src/systems/diffeqs/odesystem.jl b/src/systems/diffeqs/odesystem.jl index 227df7a156..9526f61a11 100644 --- a/src/systems/diffeqs/odesystem.jl +++ b/src/systems/diffeqs/odesystem.jl @@ -815,7 +815,7 @@ function validate_vars_and_find_ps!(auxvars, auxps, sysvars, iv) arg isa AbstractFloat || throw(ArgumentError("Invalid argument specified for variable $var. The argument of the variable should be either $iv, a parameter, or a value specifying the time that the constraint holds.")) - isparameter(arg) && push!(auxps, arg) + (isparameter(arg) && !isequal(arg, iv)) && push!(auxps, arg) else var ∈ sts && @warn "Variable $var has no argument. It will be interpreted as $var($iv), and the constraint will apply to the entire interval." diff --git a/src/systems/optimal_control_interface.jl b/src/systems/optimal_control_interface.jl index d6cc7de320..f6da3e4e70 100644 --- a/src/systems/optimal_control_interface.jl +++ b/src/systems/optimal_control_interface.jl @@ -7,6 +7,15 @@ struct OptimalControlSolution input_sol::Union{Nothing, ODESolution} end +function Base.show(io::IO, sol::OptimalControlSolution) + println("retcode: ", sol.sol.retcode, "\n") + + println("Optimal control solution for following model:\n") + show(sol.model) + + print("\n\nPlease query the model using sol.model, the solution trajectory for the system using sol.sol, or the solution trajectory for the controllers using sol.input_sol.") +end + function JuMPControlProblem end function InfiniteOptControlProblem end function CasADiControlProblem end @@ -44,7 +53,7 @@ function SciMLBase.ControlFunction{iip, specialize}(sys::ODESystem, cse = true, kwargs...) where {iip, specialize} - (f), _, _ = generate_control_function(sys, inputs, disturbance_inputs; eval_expression = true, eval_module, cse, kwargs...) + (f), _, _ = generate_control_function(sys, inputs, disturbance_inputs; eval_module, cse, kwargs...) if tgrad tgrad_gen = generate_tgrad(sys, dvs, ps; @@ -134,7 +143,7 @@ function SciMLBase.ControlFunction{false}(sys::AbstractODESystem, args...; end """ -IntegralNorm. When applied to an expression in a cost +Integral operator. When applied to an expression in a cost function, assumes that the integration variable is the iv of the system, and assumes that the bounds are the tspan. @@ -143,17 +152,46 @@ Equivalent to Integral(t in tspan) in Symbolics. struct ∫ <: Symbolics.Operator end ∫(x) = ∫()(x) Base.show(io::IO, x::∫) = print(io, "∫") +Base.nameof(::∫) = :∫ -""" -$(SIGNATURES) +function (I::∫)(x) + Term{symtype(x)}(I, Any[x]) +end + +function (I::∫)(x::Num) + v = value(x) + Num(I(v)) +end -Define one or more inputs. +SymbolicUtils.promote_symtype(::Int, t) = t -See also [`@independent_variables`](@ref), [`@variables`](@ref) and [`@constants`](@ref). -""" -macro inputs(xs...) - Symbolics._parse_vars(:inputs, - Real, - xs, - toparam) |> esc +# returns the JuMP timespan, the number of steps, and whether it is a free time problem. +function process_tspan(tspan, dt, steps) + is_free_time = false + if isnothing(dt) && isnothing(steps) + error("Must provide either the dt or the number of intervals to the collocation solvers (JuMP, InfiniteOpt, CasADi).") + elseif symbolic_type(tspan[1]) === ScalarSymbolic() || symbolic_type(tspan[2]) === ScalarSymbolic() + isnothing(steps) && error("Free final time problems require specifying the number of steps, rather than dt.") + isnothing(dt) || @warn "Specified dt for free final time problem. This will be ignored; dt will be determined by the number of timesteps." + + return steps, true + else + isnothing(steps) || @warn "Specified number of steps for problem with concrete tspan. This will be ignored; number of steps will be determined by dt." + + return length(tspan[1]:dt:tspan[2]), false + end end + +#""" +#$(SIGNATURES) +# +#Define one or more inputs. +# +#See also [`@independent_variables`](@ref), [`@variables`](@ref) and [`@constants`](@ref). +#""" +#macro inputs(xs...) +# Symbolics._parse_vars(:inputs, +# Real, +# xs, +# toparam) |> esc +#end diff --git a/src/variables.jl b/src/variables.jl index 83e72cea35..510bd5c28d 100644 --- a/src/variables.jl +++ b/src/variables.jl @@ -332,6 +332,11 @@ function hasbounds(x) any(isfinite.(b[1]) .|| isfinite.(b[2])) end +function setbounds(x::Num, bounds) + (lb, ub) = bounds + setmetadata(x, VariableBounds, (lb, ub)) +end + ## Disturbance ================================================================= struct VariableDisturbance end Symbolics.option_to_metadata_type(::Val{:disturbance}) = VariableDisturbance diff --git a/test/extensions/jump_control.jl b/test/extensions/jump_control.jl index 47e962ab9a..4a6788b008 100644 --- a/test/extensions/jump_control.jl +++ b/test/extensions/jump_control.jl @@ -139,26 +139,26 @@ end t = M.t_nounits D = M.D_nounits - @variables h(..) v(..) m(..) T(..) [input = true, bounds = (0, tₘ)] - @parameters h_c m₀ h₀ g₀ D_c c Tₘ - @parameters tf + @parameters h_c m₀ h₀ g₀ D_c c Tₘ m_c + @variables h(..) v(..) m(..) [bounds = (m_c, 1)] T(..) [input = true, bounds = (0, Tₘ)] drag(h, v) = D_c * v^2 * exp(-h_c * (h - h₀) / h₀) gravity(h) = g₀ * (h₀ / h) eqs = [D(h(t)) ~ v(t), - D(v(t)) ~ (T(t) - drag(h(t), v(t))) / m(t) - gravity(t), + D(v(t)) ~ (T(t) - drag(h(t), v(t))) / m(t) - gravity(h(t)), D(m(t)) ~ -T(t) / c] - costs = [-h(tf)] - constraints = [T(tf) ~ 0] + (ts, te) = (0., 0.2) + costs = [-h(te)] + constraints = [T(te) ~ 0] @named rocket = ODESystem(eqs, t; costs, constraints) - @test tf ∈ Set(parameters(rocket)) + rocket, input_idxs = structural_simplify(rocket, ([T(t)], [])) u0map = [h(t) => h₀, m(t) => m₀, v(t) => 0] - pmap = [g₀ => 1, m₀ => 1.0, h_c => 500, c => 0.5*√(g₀*h₀), D_C => 0.5 * 620 * m₀/g₀, Tₘ => 3.5*g₀*m₀] - jprob = JuMPControlProblem(rocket, u0map, (0, tf), pmap) + pmap = [g₀ => 1, m₀ => 1.0, h_c => 500, c => 0.5*√(g₀*h₀), D_c => 0.5 * 620 * m₀/g₀, Tₘ => 3.5*g₀*m₀, T(t) => 0., h₀ => 1, m_c => 0.6] + jprob = JuMPControlProblem(rocket, u0map, (ts, te), pmap; dt = 0.005, cse = false) jsol = solve(jprob, Ipopt.Optimizer, :RadauIA3) - @test jsol.sol.u[end][1] ≈ 1.012 + @test jsol.sol.u[end][1] > 1.012 end @testset "Free final time problem" begin @@ -167,20 +167,22 @@ end @variables x(..) u(..) [input = true, bounds = (0,1)] @parameters tf - eqs = [D(x(t)) ~ -2 + 0.5*u] - + eqs = [D(x(t)) ~ -2 + 0.5*u(t)] # Integral cost function - costs = [∫(x-u), x(tf)] + costs = [-∫(x(t)-u(t)), -x(tf)] consolidate(u) = u[1] + u[2] - jprob = JuMPControlProblem(rocket, u0map, (0, tf), pmap) - jsol = solve(jprob, Ipopt.Optimizer, :RadauIA3) - @test jsol.sol.t[end] ≈ 10.0 - iprob = InfiniteOptControlProblem(rocket, u0map, (0, tf), pmap) - isol = solve(iprob, Ipopt.Optimizer, :RadauIA3) - @test isol.sol.t[end] ≈ 10.0 -end + @named rocket = ODESystem(eqs, t; costs, consolidate) + rocket, input_idxs = structural_simplify(rocket, ([u(t)], [])) + + u0map = [x(t) => 17.5] + pmap = [u(t) => 0., tf => 8] + jprob = JuMPControlProblem(rocket, u0map, (0, tf), pmap; steps = 201) + jsol = solve(jprob, Ipopt.Optimizer, :Tsitouras5) + @test isapprox(jsol.sol.t[end], 10.0, rtol = 1e-3) -@testset "Cart-pole problem" begin + iprob = InfiniteOptControlProblem(rocket, u0map, (0, tf), pmap; steps = 200) + isol = solve(iprob, Ipopt.Optimizer) + @test isapprox(isol.sol.t[end], 10.0, rtol = 1e-3) end #@testset "Constrained optimal control problems" begin From 48cb00a3ff432249a51a0078ffd1e3e620ea1811 Mon Sep 17 00:00:00 2001 From: vyudu Date: Fri, 18 Apr 2025 05:23:58 -0400 Subject: [PATCH 1438/2176] format --- ext/MTKJuMPControlExt.jl | 34 +++++++------- src/systems/optimal_control_interface.jl | 22 ++++++---- test/extensions/jump_control.jl | 56 +++++++++++++----------- 3 files changed, 62 insertions(+), 50 deletions(-) diff --git a/ext/MTKJuMPControlExt.jl b/ext/MTKJuMPControlExt.jl index 310a632292..24dc05efd2 100644 --- a/ext/MTKJuMPControlExt.jl +++ b/ext/MTKJuMPControlExt.jl @@ -102,7 +102,7 @@ function init_model(sys, tspan, steps, u0map, pmap, u0; is_free_t = false) if is_free_t (ts_sym, te_sym) = tspan - @variable(model, tf, start = pmap[te_sym]) + @variable(model, tf, start=pmap[te_sym]) hasbounds(te_sym) && begin lo, hi = getbounds(te_sym) set_lower_bound(tf, lo) @@ -112,11 +112,11 @@ function init_model(sys, tspan, steps, u0map, pmap, u0; is_free_t = false) pmap[te_sym] = 1 tspan = (0, 1) end - - @infinite_parameter(model, t in [tspan[1], tspan[2]], num_supports = steps) - @variable(model, U[i = 1:length(states)], Infinite(t), start = u0[i]) + + @infinite_parameter(model, t in [tspan[1], tspan[2]], num_supports=steps) + @variable(model, U[i = 1:length(states)], Infinite(t), start=u0[i]) c0 = [pmap[c] for c in ctrls] - @variable(model, V[i = 1:length(ctrls)], Infinite(t), start = c0[i]) + @variable(model, V[i = 1:length(ctrls)], Infinite(t), start=c0[i]) set_jump_bounds!(model, sys, pmap) add_jump_cost_function!(model, sys, (tspan[1], tspan[2]), pmap; is_free_t) @@ -161,7 +161,8 @@ function add_jump_cost_function!(model::InfiniteModel, sys, tspan, pmap; is_free # Substitute integral iv = MTK.get_iv(sys) - jcosts = map(c -> Symbolics.substitute(c, MTK.∫() => Symbolics.Integral(iv in tspan)), jcosts) + jcosts = map( + c -> Symbolics.substitute(c, MTK.∫() => Symbolics.Integral(iv in tspan)), jcosts) intmap = Dict() for int in MTK.collect_applied_operators(jcosts, Symbolics.Integral) @@ -183,7 +184,8 @@ function add_user_constraints!(model::InfiniteModel, sys, pmap; is_free_t = fals for u in MTK.get_unknowns(conssys) x = MTK.operation(u) t = only(arguments(u)) - MTK.symbolic_type(t) === NotSymbolic() && error("Provided specific time constraint in a free final time problem. This is not supported by the JuMP/InfiniteOpt collocation solvers. The offending variable is $u.") + MTK.symbolic_type(t) === NotSymbolic() && + error("Provided specific time constraint in a free final time problem. This is not supported by the JuMP/InfiniteOpt collocation solvers. The offending variable is $u.") end end @@ -211,13 +213,15 @@ function substitute_jump_vars(model, sys, pmap, exprs) U = model[:U] V = model[:V] # for variables like x(t) - whole_interval_map = Dict([[v => U[i] for (i, v) in enumerate(sts)]; [v => V[i] for (i, v) in enumerate(cts)]]) + whole_interval_map = Dict([[v => U[i] for (i, v) in enumerate(sts)]; + [v => V[i] for (i, v) in enumerate(cts)]]) exprs = map(c -> Symbolics.substitute(c, whole_interval_map), exprs) # for variables like x(1.0) x_ops = [MTK.operation(MTK.unwrap(st)) for st in sts] c_ops = [MTK.operation(MTK.unwrap(ct)) for ct in cts] - fixed_t_map = Dict([[x_ops[i] => U[i] for i in 1:length(U)]; [c_ops[i] => V[i] for i in 1:length(V)]]) + fixed_t_map = Dict([[x_ops[i] => U[i] for i in 1:length(U)]; + [c_ops[i] => V[i] for i in 1:length(V)]]) exprs = map(c -> Symbolics.substitute(c, fixed_t_map), exprs) exprs = map(c -> Symbolics.substitute(c, Dict(pmap)), exprs) @@ -236,11 +240,11 @@ function add_infopt_solve_constraints!(model::InfiniteModel, sys, pmap; is_free_ diff_eqs = substitute_jump_vars(model, sys, pmap, diff_equations(sys)) diff_eqs = map(e -> Symbolics.substitute(e, diffsubmap), diff_eqs) - @constraint(model, D[i = 1:length(diff_eqs)], diff_eqs[i].lhs == tₛ * diff_eqs[i].rhs) + @constraint(model, D[i = 1:length(diff_eqs)], diff_eqs[i].lhs==tₛ * diff_eqs[i].rhs) # Algebraic equations alg_eqs = substitute_jump_vars(model, sys, pmap, alg_equations(sys)) - @constraint(model, A[i = 1:length(alg_eqs)], alg_eqs[i].lhs == alg_eqs[i].rhs) + @constraint(model, A[i = 1:length(alg_eqs)], alg_eqs[i].lhs==alg_eqs[i].rhs) end function add_jump_solve_constraints!(prob, tableau; is_free_t = false) @@ -283,11 +287,11 @@ function add_jump_solve_constraints!(prob, tableau; is_free_t = false) for (i, h) in enumerate(c) ΔU = @view ΔUs[i, :] Uₙ = U + ΔU * dt - @constraint(model, [j = 1:nᵤ], K[i, j](τ) == tₛ * f(Uₙ, V, p, τ + h * dt)[j], - DomainRestrictions(t => τ + h*dt), base_name="solve_K($τ)") + @constraint(model, [j = 1:nᵤ], K[i, j](τ)==tₛ * f(Uₙ, V, p, τ + h * dt)[j], + DomainRestrictions(t => τ + h * dt), base_name="solve_K($τ)") end - @constraint(model, [n = 1:nᵤ], U[n](τ) + ΔU_tot[n] == U[n](τ + dt), - DomainRestrictions(t => τ), base_name="solve_U($τ)") + @constraint(model, [n = 1:nᵤ], U[n](τ) + ΔU_tot[n]==U[n](τ + dt), + DomainRestrictions(t => τ), base_name="solve_U($τ)") end end end diff --git a/src/systems/optimal_control_interface.jl b/src/systems/optimal_control_interface.jl index f6da3e4e70..a2201d008e 100644 --- a/src/systems/optimal_control_interface.jl +++ b/src/systems/optimal_control_interface.jl @@ -39,7 +39,7 @@ function SciMLBase.ControlFunction{iip, specialize}(sys::ODESystem, inputs = unbound_inputs(sys), disturbance_inputs = disturbances(sys); version = nothing, tgrad = false, - jac = false, controljac = false, + jac = false, controljac = false, p = nothing, t = nothing, eval_expression = false, sparse = false, simplify = false, @@ -52,8 +52,8 @@ function SciMLBase.ControlFunction{iip, specialize}(sys::ODESystem, initialization_data = nothing, cse = true, kwargs...) where {iip, specialize} - - (f), _, _ = generate_control_function(sys, inputs, disturbance_inputs; eval_module, cse, kwargs...) + (f), _, _ = generate_control_function( + sys, inputs, disturbance_inputs; eval_module, cse, kwargs...) if tgrad tgrad_gen = generate_tgrad(sys, dvs, ps; @@ -113,7 +113,7 @@ function SciMLBase.ControlFunction{iip, specialize}(sys::ODESystem, W_prototype = nothing controljac_prototype = nothing end - + ControlFunction{iip, specialize}(f; sys = sys, jac = _jac === nothing ? nothing : _jac, @@ -170,15 +170,19 @@ function process_tspan(tspan, dt, steps) is_free_time = false if isnothing(dt) && isnothing(steps) error("Must provide either the dt or the number of intervals to the collocation solvers (JuMP, InfiniteOpt, CasADi).") - elseif symbolic_type(tspan[1]) === ScalarSymbolic() || symbolic_type(tspan[2]) === ScalarSymbolic() - isnothing(steps) && error("Free final time problems require specifying the number of steps, rather than dt.") - isnothing(dt) || @warn "Specified dt for free final time problem. This will be ignored; dt will be determined by the number of timesteps." + elseif symbolic_type(tspan[1]) === ScalarSymbolic() || + symbolic_type(tspan[2]) === ScalarSymbolic() + isnothing(steps) && + error("Free final time problems require specifying the number of steps, rather than dt.") + isnothing(dt) || + @warn "Specified dt for free final time problem. This will be ignored; dt will be determined by the number of timesteps." return steps, true else - isnothing(steps) || @warn "Specified number of steps for problem with concrete tspan. This will be ignored; number of steps will be determined by dt." + isnothing(steps) || + @warn "Specified number of steps for problem with concrete tspan. This will be ignored; number of steps will be determined by dt." - return length(tspan[1]:dt:tspan[2]), false + return length(tspan[1]:dt:tspan[2]), false end end diff --git a/test/extensions/jump_control.jl b/test/extensions/jump_control.jl index 4a6788b008..dcf029a40f 100644 --- a/test/extensions/jump_control.jl +++ b/test/extensions/jump_control.jl @@ -79,8 +79,9 @@ end @testset "Linear systems" begin function is_bangbang(input_sol, lbounds, ubounds, rtol = 1e-4) bangbang = true - for v in 1:length(input_sol.u[1]) - 1 - all(i -> ≈(i[v], bounds[v]; rtol) || ≈(i[v], bounds[u]; rtol), input_sol.u) || (bangbang = false) + for v in 1:(length(input_sol.u[1]) - 1) + all(i -> ≈(i[v], bounds[v]; rtol) || ≈(i[v], bounds[u]; rtol), input_sol.u) || + (bangbang = false) end bangbang end @@ -88,26 +89,27 @@ end # Double integrator t = M.t_nounits D = M.D_nounits - @variables x(..) [bounds = (0., 0.25)] v(..) - @variables u(t) [bounds = (-1., 1.), input = true] + @variables x(..) [bounds = (0.0, 0.25)] v(..) + @variables u(t) [bounds = (-1.0, 1.0), input = true] constr = [v(1.0) ~ 0.0] cost = [-x(1.0)] # Maximize the final distance. - @named block = ODESystem([D(x(t)) ~ v(t), D(v(t)) ~ u], t; costs = cost, constraints = constr) - block, input_idxs = structural_simplify(block, ([u],[])) + @named block = ODESystem( + [D(x(t)) ~ v(t), D(v(t)) ~ u], t; costs = cost, constraints = constr) + block, input_idxs = structural_simplify(block, ([u], [])) - u0map = [x(t) => 0., v(t) => 0.] - tspan = (0., 1.) - parammap = [u => 0.] + u0map = [x(t) => 0.0, v(t) => 0.0] + tspan = (0.0, 1.0) + parammap = [u => 0.0] jprob = JuMPControlProblem(block, u0map, tspan, parammap; dt = 0.01) jsol = solve(jprob, Ipopt.Optimizer, :Verner8) # Linear systems have bang-bang controls - @test is_bangbang(jsol.input_sol, [-1.], [1.]) + @test is_bangbang(jsol.input_sol, [-1.0], [1.0]) # Test reached final position. @test ≈(jsol.sol.u[end][1], 0.25, rtol = 1e-5) iprob = InfiniteOptControlProblem(block, u0map, tspan, parammap; dt = 0.01) isol = solve(iprob, Ipopt.Optimizer; silent = true) - @test is_bangbang(isol.input_sol, [-1.], [1.]) + @test is_bangbang(isol.input_sol, [-1.0], [1.0]) @test ≈(isol.sol.u[end][1], 0.25, rtol = 1e-5) ################### @@ -118,21 +120,21 @@ end @parameters b c μ s ν tspan = (0, 4) - eqs = [D(w(t)) ~ -μ*w(t) + b*s*α*w(t), - D(q(t)) ~ -ν*q(t) + c*(1 - α)*s*w(t)] + eqs = [D(w(t)) ~ -μ * w(t) + b * s * α * w(t), + D(q(t)) ~ -ν * q(t) + c * (1 - α) * s * w(t)] costs = [-q(tspan[2])] - + @named beesys = ODESystem(eqs, t; costs) - beesys, input_idxs = structural_simplify(beesys, ([α],[])) + beesys, input_idxs = structural_simplify(beesys, ([α], [])) u0map = [w(t) => 40, q(t) => 2] pmap = [b => 1, c => 1, μ => 1, s => 1, ν => 1, α => 1] jprob = JuMPControlProblem(beesys, u0map, tspan, pmap, dt = 0.01) jsol = solve(jprob, Ipopt.Optimizer, :Tsitouras5) - @test is_bangbang(jsol.input_sol, [0.], [1.]) + @test is_bangbang(jsol.input_sol, [0.0], [1.0]) iprob = InfiniteOptControlProblem(beesys, u0map, tspan, pmap, dt = 0.01) isol = solve(jprob, Ipopt.Optimizer, :Tsitouras5) - @test is_bangbang(isol.input_sol, [0.], [1.]) + @test is_bangbang(isol.input_sol, [0.0], [1.0]) end @testset "Rocket launch" begin @@ -144,18 +146,20 @@ end drag(h, v) = D_c * v^2 * exp(-h_c * (h - h₀) / h₀) gravity(h) = g₀ * (h₀ / h) - eqs = [D(h(t)) ~ v(t), - D(v(t)) ~ (T(t) - drag(h(t), v(t))) / m(t) - gravity(h(t)), - D(m(t)) ~ -T(t) / c] + eqs = [D(h(t)) ~ v(t), + D(v(t)) ~ (T(t) - drag(h(t), v(t))) / m(t) - gravity(h(t)), + D(m(t)) ~ -T(t) / c] - (ts, te) = (0., 0.2) + (ts, te) = (0.0, 0.2) costs = [-h(te)] constraints = [T(te) ~ 0] @named rocket = ODESystem(eqs, t; costs, constraints) rocket, input_idxs = structural_simplify(rocket, ([T(t)], [])) u0map = [h(t) => h₀, m(t) => m₀, v(t) => 0] - pmap = [g₀ => 1, m₀ => 1.0, h_c => 500, c => 0.5*√(g₀*h₀), D_c => 0.5 * 620 * m₀/g₀, Tₘ => 3.5*g₀*m₀, T(t) => 0., h₀ => 1, m_c => 0.6] + pmap = [ + g₀ => 1, m₀ => 1.0, h_c => 500, c => 0.5 * √(g₀ * h₀), D_c => 0.5 * 620 * m₀ / g₀, + Tₘ => 3.5 * g₀ * m₀, T(t) => 0.0, h₀ => 1, m_c => 0.6] jprob = JuMPControlProblem(rocket, u0map, (ts, te), pmap; dt = 0.005, cse = false) jsol = solve(jprob, Ipopt.Optimizer, :RadauIA3) @test jsol.sol.u[end][1] > 1.012 @@ -165,17 +169,17 @@ end t = M.t_nounits D = M.D_nounits - @variables x(..) u(..) [input = true, bounds = (0,1)] + @variables x(..) u(..) [input = true, bounds = (0, 1)] @parameters tf - eqs = [D(x(t)) ~ -2 + 0.5*u(t)] + eqs = [D(x(t)) ~ -2 + 0.5 * u(t)] # Integral cost function - costs = [-∫(x(t)-u(t)), -x(tf)] + costs = [-∫(x(t) - u(t)), -x(tf)] consolidate(u) = u[1] + u[2] @named rocket = ODESystem(eqs, t; costs, consolidate) rocket, input_idxs = structural_simplify(rocket, ([u(t)], [])) u0map = [x(t) => 17.5] - pmap = [u(t) => 0., tf => 8] + pmap = [u(t) => 0.0, tf => 8] jprob = JuMPControlProblem(rocket, u0map, (0, tf), pmap; steps = 201) jsol = solve(jprob, Ipopt.Optimizer, :Tsitouras5) @test isapprox(jsol.sol.t[end], 10.0, rtol = 1e-3) From b127c7378df57dafae464877850e5028cc3bace8 Mon Sep 17 00:00:00 2001 From: vyudu Date: Tue, 22 Apr 2025 16:47:44 -0400 Subject: [PATCH 1439/2176] test: add trasncription tests --- ext/MTKJuMPControlExt.jl | 5 +-- test/extensions/jump_control.jl | 62 +++++++++++++++++++++++++++------ 2 files changed, 54 insertions(+), 13 deletions(-) diff --git a/ext/MTKJuMPControlExt.jl b/ext/MTKJuMPControlExt.jl index 24dc05efd2..4c90cc8448 100644 --- a/ext/MTKJuMPControlExt.jl +++ b/ext/MTKJuMPControlExt.jl @@ -40,8 +40,9 @@ end JuMPControlProblem(sys::ODESystem, u0, tspan, p; dt) Convert an ODESystem representing an optimal control system into a JuMP model -for solving using optimization. Must provide `dt` for determining the length -of the interpolation arrays. +for solving using optimization. Must provide either `dt`, the timestep between collocation +points (which, along with the timespan, determines the number of points), or directly +provide the number of points as `nsteps`. The optimization variables: - a vector-of-vectors U representing the unknowns as an interpolation array diff --git a/test/extensions/jump_control.jl b/test/extensions/jump_control.jl index dcf029a40f..d285e86886 100644 --- a/test/extensions/jump_control.jl +++ b/test/extensions/jump_control.jl @@ -6,6 +6,7 @@ using OrdinaryDiffEqSDIRK using Ipopt using BenchmarkTools using CairoMakie +using DataInterpolations const M = ModelingToolkit @testset "ODE Solution, no cost" begin @@ -76,21 +77,26 @@ const M = ModelingToolkit @test all(u -> u .> [3, 4], sol.u) end -@testset "Linear systems" begin - function is_bangbang(input_sol, lbounds, ubounds, rtol = 1e-4) - bangbang = true - for v in 1:(length(input_sol.u[1]) - 1) - all(i -> ≈(i[v], bounds[v]; rtol) || ≈(i[v], bounds[u]; rtol), input_sol.u) || - (bangbang = false) - end - bangbang +function is_bangbang(input_sol, lbounds, ubounds, rtol = 1e-4) + for v in 1:(length(input_sol.u[1]) - 1) + all(i -> ≈(i[v], bounds[v]; rtol) || ≈(i[v], bounds[u]; rtol), input_sol.u) || + return false end + true +end + +function ctrl_to_spline(inputsol, splineType) + us = reduce(vcat, inputsol.u) + ts = reduce(vcat, inputsol.t) + splineType(us, ts) +end +@testset "Linear systems" begin # Double integrator t = M.t_nounits D = M.D_nounits @variables x(..) [bounds = (0.0, 0.25)] v(..) - @variables u(t) [bounds = (-1.0, 1.0), input = true] + @variables u(..) [bounds = (-1.0, 1.0), input = true] constr = [v(1.0) ~ 0.0] cost = [-x(1.0)] # Maximize the final distance. @named block = ODESystem( @@ -99,18 +105,27 @@ end u0map = [x(t) => 0.0, v(t) => 0.0] tspan = (0.0, 1.0) - parammap = [u => 0.0] + parammap = [u(t) => 0.0] jprob = JuMPControlProblem(block, u0map, tspan, parammap; dt = 0.01) jsol = solve(jprob, Ipopt.Optimizer, :Verner8) # Linear systems have bang-bang controls @test is_bangbang(jsol.input_sol, [-1.0], [1.0]) # Test reached final position. @test ≈(jsol.sol.u[end][1], 0.25, rtol = 1e-5) + # Test dynamics + @parameters (u_interp::LinearInterpolation)(..) + block_ode = ODESystem([D(x(t)) ~ v(t), D(v(t)) ~ u_interp(t)], t) + spline = ctrl_to_spline(jsol.input_sol, LinearInterpolation) + oprob = ODEProblem(block, u0map, tspan, [u_interp => spline]) + osol = solve(oprob, Vern8()) + @test jsol.sol.u ≈ osol.u iprob = InfiniteOptControlProblem(block, u0map, tspan, parammap; dt = 0.01) isol = solve(iprob, Ipopt.Optimizer; silent = true) @test is_bangbang(isol.input_sol, [-1.0], [1.0]) @test ≈(isol.sol.u[end][1], 0.25, rtol = 1e-5) + osol = solve(oprob, ImplicitEuler()) + @test isol.sol.u ≈ osol.u ################### ### Bee example ### @@ -133,8 +148,16 @@ end jsol = solve(jprob, Ipopt.Optimizer, :Tsitouras5) @test is_bangbang(jsol.input_sol, [0.0], [1.0]) iprob = InfiniteOptControlProblem(beesys, u0map, tspan, pmap, dt = 0.01) - isol = solve(jprob, Ipopt.Optimizer, :Tsitouras5) + isol = solve(iprob, Ipopt.Optimizer; silent = true) @test is_bangbang(isol.input_sol, [0.0], [1.0]) + + @parameters (α_interp::LinearInterpolation)(..) + eqs = [D(w(t)) ~ -μ * w(t) + b * s * α_interp(t) * w(t), + D(q(t)) ~ -ν * q(t) + c * (1 - α_interp(t)) * s * w(t)] + beesys_ode = ODESystem(eqs, t) + oprob = ODEProblem(beesys_ode, u0map, tspan, [α_interp => ctrl_to_spline(jsol.input_sol, LinearInterpolation)]) + osol = solve(oprob, Tsit5()) + @test osol.u ≈ jsol.sol.u end @testset "Rocket launch" begin @@ -163,6 +186,17 @@ end jprob = JuMPControlProblem(rocket, u0map, (ts, te), pmap; dt = 0.005, cse = false) jsol = solve(jprob, Ipopt.Optimizer, :RadauIA3) @test jsol.sol.u[end][1] > 1.012 + + # Test solution + @parameters (T_interp::CubicSpline)(..) + eqs = [D(h(t)) ~ v(t), + D(v(t)) ~ (T_interp(t) - drag(h(t), v(t))) / m(t) - gravity(h(t)), + D(m(t)) ~ -T_interp(t) / c] + rocket_ode = ODESystem(eqs, t) + interpmap = Dict(T_interp => ctrl_to_spline(jsol.inputsol, CubicSpline)) + oprob = ODEProblem(rocket_ode, u0map, tspan, merge(pmap, interpmap)) + osol = solve(oprob, RadauIA3()) + @test jsol.sol.u ≈ osol.u end @testset "Free final time problem" begin @@ -189,5 +223,11 @@ end @test isapprox(isol.sol.t[end], 10.0, rtol = 1e-3) end +using JuliaSimCompiler +using Multibody.PlanarMechanics + +@testset "Cart-pole problem" begin +end + #@testset "Constrained optimal control problems" begin #end From 740accf0483ea008ec72cd9d906ebd3ebb20d912 Mon Sep 17 00:00:00 2001 From: vyudu Date: Tue, 22 Apr 2025 19:48:20 -0400 Subject: [PATCH 1440/2176] init new project for optimal control tests --- src/systems/optimal_control_interface.jl | 16 ++++++++-------- test/dynamic_optimization/Project.toml | 5 +++++ .../jump_control.jl | 0 test/extensions/Project.toml | 3 --- 4 files changed, 13 insertions(+), 11 deletions(-) create mode 100644 test/dynamic_optimization/Project.toml rename test/{extensions => dynamic_optimization}/jump_control.jl (100%) diff --git a/src/systems/optimal_control_interface.jl b/src/systems/optimal_control_interface.jl index a2201d008e..c6d46bb0ba 100644 --- a/src/systems/optimal_control_interface.jl +++ b/src/systems/optimal_control_interface.jl @@ -33,7 +33,7 @@ end Generate the control function f(x, u, p, t) from the ODESystem. Input variables are automatically inferred but can be manually specified. """ -function SciMLBase.ControlFunction{iip, specialize}(sys::ODESystem, +function SciMLBase.ODEInputFunction{iip, specialize}(sys::ODESystem, dvs = unknowns(sys), ps = parameters(sys), u0 = nothing, inputs = unbound_inputs(sys), @@ -114,7 +114,7 @@ function SciMLBase.ControlFunction{iip, specialize}(sys::ODESystem, controljac_prototype = nothing end - ControlFunction{iip, specialize}(f; + ODEInputFunction{iip, specialize}(f; sys = sys, jac = _jac === nothing ? nothing : _jac, controljac = _cjac === nothing ? nothing : _cjac, @@ -128,18 +128,18 @@ function SciMLBase.ControlFunction{iip, specialize}(sys::ODESystem, initialization_data) end -function SciMLBase.ControlFunction(sys::AbstractODESystem, args...; kwargs...) - ControlFunction{true}(sys, args...; kwargs...) +function SciMLBase.ODEInputFunction(sys::AbstractODESystem, args...; kwargs...) + ODEInputFunction{true}(sys, args...; kwargs...) end -function SciMLBase.ControlFunction{true}(sys::AbstractODESystem, args...; +function SciMLBase.ODEInputFunction{true}(sys::AbstractODESystem, args...; kwargs...) - ControlFunction{true, SciMLBase.AutoSpecialize}(sys, args...; kwargs...) + ODEInputFunction{true, SciMLBase.AutoSpecialize}(sys, args...; kwargs...) end -function SciMLBase.ControlFunction{false}(sys::AbstractODESystem, args...; +function SciMLBase.ODEInputFunction{false}(sys::AbstractODESystem, args...; kwargs...) - ControlFunction{false, SciMLBase.FullSpecialize}(sys, args...; kwargs...) + ODEInputFunction{false, SciMLBase.FullSpecialize}(sys, args...; kwargs...) end """ diff --git a/test/dynamic_optimization/Project.toml b/test/dynamic_optimization/Project.toml new file mode 100644 index 0000000000..ae0890688b --- /dev/null +++ b/test/dynamic_optimization/Project.toml @@ -0,0 +1,5 @@ +[deps] +DiffEqDevTools = "f3b72e0c-5b89-59e1-b016-84e28bfd966d" +InfiniteOpt = "20393b10-9daf-11e9-18c9-8db751c92c57" +Ipopt = "b6b21f68-93f8-5de0-b562-5493be1d77c9" +Multibody = "e1cad5d1-98ef-44f9-a79a-9ca4547f95b9" diff --git a/test/extensions/jump_control.jl b/test/dynamic_optimization/jump_control.jl similarity index 100% rename from test/extensions/jump_control.jl rename to test/dynamic_optimization/jump_control.jl diff --git a/test/extensions/Project.toml b/test/extensions/Project.toml index 5800fe612f..fe2189b169 100644 --- a/test/extensions/Project.toml +++ b/test/extensions/Project.toml @@ -3,12 +3,9 @@ BifurcationKit = "0f109fa4-8a5d-4b75-95aa-f515264e7665" ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4" ChainRulesTestUtils = "cdddcdb0-9152-4a09-a978-84456f9df70a" DiffEqBase = "2b5f629d-d688-5b77-993f-72d75c75574e" -DiffEqDevTools = "f3b72e0c-5b89-59e1-b016-84e28bfd966d" ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210" HomotopyContinuation = "f213a82b-91d6-5c5d-acf7-10f1c761b327" InfiniteOpt = "20393b10-9daf-11e9-18c9-8db751c92c57" -Ipopt = "b6b21f68-93f8-5de0-b562-5493be1d77c9" -JuMP = "4076af6c-e467-56ae-b986-b466b2749572" LabelledArrays = "2ee39098-c373-598a-b85f-a56591580800" ModelingToolkit = "961ee093-0014-501f-94e3-6117800e7a78" Nemo = "2edaba10-b0f1-5616-af89-8c11ac63239a" From bdc782c7c09d0c20ec8f537d12cb6366e8d107c5 Mon Sep 17 00:00:00 2001 From: vyudu Date: Wed, 23 Apr 2025 19:09:18 -0400 Subject: [PATCH 1441/2176] test: more test fixes --- ext/MTKJuMPControlExt.jl | 39 ++++++---- src/systems/diffeqs/odesystem.jl | 5 ++ src/systems/systems.jl | 5 ++ test/dynamic_optimization/Project.toml | 11 ++- test/dynamic_optimization/jump_control.jl | 90 ++++++++++++++++------- test/runtests.jl | 11 +++ 6 files changed, 121 insertions(+), 40 deletions(-) diff --git a/ext/MTKJuMPControlExt.jl b/ext/MTKJuMPControlExt.jl index 4c90cc8448..463431e235 100644 --- a/ext/MTKJuMPControlExt.jl +++ b/ext/MTKJuMPControlExt.jl @@ -30,7 +30,7 @@ struct InfiniteOptControlProblem{uType, tType, isinplace, P, F, K} <: model::InfiniteModel kwargs::K - function InfiniteOptControlProblem(f, u0, tspan, p, model; kwargs...) + function InfiniteOptControlProblem(f, u0, tspan, p, model, kwargs...) new{typeof(u0), typeof(tspan), SciMLBase.isinplace(f), typeof(p), typeof(f), typeof(kwargs)}(f, u0, tspan, p, model, kwargs) end @@ -58,7 +58,7 @@ function MTK.JuMPControlProblem(sys::ODESystem, u0map, tspan, pmap; guesses = Dict(), kwargs...) MTK.warn_overdetermined(sys, u0map) _u0map = has_alg_eqs(sys) ? u0map : merge(Dict(u0map), Dict(guesses)) - f, u0, p = MTK.process_SciMLProblem(ControlFunction, sys, _u0map, pmap; + f, u0, p = MTK.process_SciMLProblem(ODEInputFunction, sys, _u0map, pmap; t = tspan !== nothing ? tspan[1] : tspan, kwargs...) pmap = MTK.todict(pmap) @@ -84,7 +84,7 @@ function MTK.InfiniteOptControlProblem(sys::ODESystem, u0map, tspan, pmap; guesses = Dict(), kwargs...) MTK.warn_overdetermined(sys, u0map) _u0map = has_alg_eqs(sys) ? u0map : merge(Dict(u0map), Dict(guesses)) - f, u0, p = MTK.process_SciMLProblem(ControlFunction, sys, _u0map, pmap; + f, u0, p = MTK.process_SciMLProblem(ODEInputFunction, sys, _u0map, pmap; t = tspan !== nothing ? tspan[1] : tspan, kwargs...) pmap = MTK.todict(pmap) @@ -116,7 +116,7 @@ function init_model(sys, tspan, steps, u0map, pmap, u0; is_free_t = false) @infinite_parameter(model, t in [tspan[1], tspan[2]], num_supports=steps) @variable(model, U[i = 1:length(states)], Infinite(t), start=u0[i]) - c0 = [pmap[c] for c in ctrls] + c0 = MTK.value.([pmap[c] for c in ctrls]) @variable(model, V[i = 1:length(ctrls)], Infinite(t), start=c0[i]) set_jump_bounds!(model, sys, pmap) @@ -124,8 +124,9 @@ function init_model(sys, tspan, steps, u0map, pmap, u0; is_free_t = false) add_user_constraints!(model, sys, pmap; is_free_t) stidxmap = Dict([v => i for (i, v) in enumerate(states)]) + u0map = Dict([MTK.default_toterm(MTK.value(k)) => v for (k, v) in u0map]) u0_idxs = has_alg_eqs(sys) ? collect(1:length(states)) : - [stidxmap[k] for (k, v) in u0map] + [stidxmap[MTK.default_toterm(k)] for (k, v) in u0map] add_initial_constraints!(model, u0, u0_idxs, tspan[1]) return model end @@ -190,7 +191,10 @@ function add_user_constraints!(model::InfiniteModel, sys, pmap; is_free_t = fals end end - jconstraints = substitute_jump_vars(model, sys, pmap, jconstraints) + auxmap = Dict([u => MTK.default_toterm(MTK.value(u)) for u in unknowns(conssys)]) + jconstraints = substitute_jump_vars(model, sys, pmap, jconstraints; auxmap) + + # Substitute to-term'd variables for (i, cons) in enumerate(jconstraints) if cons isa Equation @constraint(model, cons.lhs - cons.rhs==0, base_name="user[$i]") @@ -207,25 +211,28 @@ function add_initial_constraints!(model::InfiniteModel, u0, u0_idxs, ts) @constraint(model, initial[i in u0_idxs], U[i](ts)==u0[i]) end -function substitute_jump_vars(model, sys, pmap, exprs) +function substitute_jump_vars(model, sys, pmap, exprs; auxmap = Dict()) iv = MTK.get_iv(sys) sts = unknowns(sys) cts = MTK.unbound_inputs(sys) U = model[:U] V = model[:V] + exprs = map(c -> Symbolics.fixpoint_sub(c, auxmap), exprs) + # for variables like x(t) whole_interval_map = Dict([[v => U[i] for (i, v) in enumerate(sts)]; [v => V[i] for (i, v) in enumerate(cts)]]) - exprs = map(c -> Symbolics.substitute(c, whole_interval_map), exprs) + exprs = map(c -> Symbolics.fixpoint_sub(c, whole_interval_map), exprs) # for variables like x(1.0) x_ops = [MTK.operation(MTK.unwrap(st)) for st in sts] c_ops = [MTK.operation(MTK.unwrap(ct)) for ct in cts] fixed_t_map = Dict([[x_ops[i] => U[i] for i in 1:length(U)]; [c_ops[i] => V[i] for i in 1:length(V)]]) - exprs = map(c -> Symbolics.substitute(c, fixed_t_map), exprs) - exprs = map(c -> Symbolics.substitute(c, Dict(pmap)), exprs) + exprs = map(c -> Symbolics.fixpoint_sub(c, fixed_t_map), exprs) + + exprs = map(c -> Symbolics.fixpoint_sub(c, Dict(pmap)), exprs) exprs end @@ -255,8 +262,10 @@ function add_jump_solve_constraints!(prob, tableau; is_free_t = false) model = prob.model f = prob.f p = prob.p + t = model[:t] - tsteps = supports(model[:t]) + tsteps = supports(t) + tmax = tsteps[end] pop!(tsteps) tₛ = is_free_t ? model[:tf] : 1 dt = tsteps[2] - tsteps[1] @@ -280,6 +289,7 @@ function add_jump_solve_constraints!(prob, tableau; is_free_t = false) base_name="solve_time_$τ") empty!(K) end + @show num_variables(model) else @variable(model, K[1:length(α), 1:nᵤ], Infinite(t), start=tsteps[1]) ΔUs = A * K @@ -288,10 +298,10 @@ function add_jump_solve_constraints!(prob, tableau; is_free_t = false) for (i, h) in enumerate(c) ΔU = @view ΔUs[i, :] Uₙ = U + ΔU * dt - @constraint(model, [j = 1:nᵤ], K[i, j](τ)==tₛ * f(Uₙ, V, p, τ + h * dt)[j], - DomainRestrictions(t => τ + h * dt), base_name="solve_K($τ)") + @constraint(model, [j = 1:nᵤ], K[i, j](τ)==(tₛ * f(Uₙ, V, p, τ + h * dt)[j]), + DomainRestrictions(t => min(τ + h * dt, tmax)), base_name="solve_K($τ)") end - @constraint(model, [n = 1:nᵤ], U[n](τ) + ΔU_tot[n]==U[n](τ + dt), + @constraint(model, [n = 1:nᵤ], U[n](τ) + ΔU_tot[n]==U[n](min(τ + dt, tmax)), DomainRestrictions(t => τ), base_name="solve_U($τ)") end end @@ -323,6 +333,7 @@ function DiffEqBase.solve( unregister(model, :K) for var in all_variables(model) if occursin("K", JuMP.name(var)) + unregister(model, Symbol(JuMP.name(var))) delete(model, var) end end diff --git a/src/systems/diffeqs/odesystem.jl b/src/systems/diffeqs/odesystem.jl index 9526f61a11..9f0f3d00ed 100644 --- a/src/systems/diffeqs/odesystem.jl +++ b/src/systems/diffeqs/odesystem.jl @@ -765,7 +765,9 @@ function process_constraint_system( constraintps = OrderedSet() for cons in constraints collect_vars!(constraintsts, constraintps, cons, iv) + union!(constraintsts, collect_applied_operators(cons, Differential)) end + @show constraintsts # Validate the states. validate_vars_and_find_ps!(constraintsts, constraintps, sts, iv) @@ -807,6 +809,9 @@ function validate_vars_and_find_ps!(auxvars, auxps, sysvars, iv) elseif length(arguments(var)) > 1 throw(ArgumentError("Too many arguments for variable $var.")) elseif length(arguments(var)) == 1 + if iscall(var) && operation(var) isa Differential + var = only(arguments(var)) + end arg = only(arguments(var)) operation(var)(iv) ∈ sts || throw(ArgumentError("Variable $var is not a variable of the ODESystem. Called variables must be variables of the ODESystem.")) diff --git a/src/systems/systems.jl b/src/systems/systems.jl index 52f93afb9b..9da7249300 100644 --- a/src/systems/systems.jl +++ b/src/systems/systems.jl @@ -163,6 +163,11 @@ function __structural_simplify( end end +function toterm_auxsystems(system::ODESystem) + constraints = system.constraintsystem.constraints + +end + """ $(TYPEDSIGNATURES) diff --git a/test/dynamic_optimization/Project.toml b/test/dynamic_optimization/Project.toml index ae0890688b..a5eaf3144b 100644 --- a/test/dynamic_optimization/Project.toml +++ b/test/dynamic_optimization/Project.toml @@ -1,5 +1,14 @@ [deps] +BenchmarkTools = "6e4b80f9-dd63-53aa-95a3-0cdb28fa8baf" +CairoMakie = "13f3f980-e62b-5c42-98c6-ff1f3baf88f0" +DataInterpolations = "82cc6244-b520-54b8-b5a6-8a565e85f1d0" DiffEqDevTools = "f3b72e0c-5b89-59e1-b016-84e28bfd966d" InfiniteOpt = "20393b10-9daf-11e9-18c9-8db751c92c57" Ipopt = "b6b21f68-93f8-5de0-b562-5493be1d77c9" -Multibody = "e1cad5d1-98ef-44f9-a79a-9ca4547f95b9" +ModelingToolkit = "961ee093-0014-501f-94e3-6117800e7a78" +ModelingToolkitStandardLibrary = "16a59e39-deab-5bd0-87e4-056b12336739" +OrdinaryDiffEqFIRK = "5960d6e9-dd7a-4743-88e7-cf307b64f125" +OrdinaryDiffEqSDIRK = "2d112036-d095-4a1e-ab9a-08536f3ecdbf" +OrdinaryDiffEqTsit5 = "b1df2697-797e-41e3-8120-5422d3b24e4a" +OrdinaryDiffEqVerner = "79d7bb75-1356-48c1-b8c0-6832512096c2" +SimpleDiffEq = "05bca326-078c-5bf0-a5bf-ce7c7982d7fd" diff --git a/test/dynamic_optimization/jump_control.jl b/test/dynamic_optimization/jump_control.jl index d285e86886..1565cf387f 100644 --- a/test/dynamic_optimization/jump_control.jl +++ b/test/dynamic_optimization/jump_control.jl @@ -2,7 +2,7 @@ using ModelingToolkit import JuMP, InfiniteOpt using DiffEqDevTools, DiffEqBase using SimpleDiffEq -using OrdinaryDiffEqSDIRK +using OrdinaryDiffEqSDIRK, OrdinaryDiffEqVerner, OrdinaryDiffEqTsit5, OrdinaryDiffEqFIRK using Ipopt using BenchmarkTools using CairoMakie @@ -100,8 +100,8 @@ end constr = [v(1.0) ~ 0.0] cost = [-x(1.0)] # Maximize the final distance. @named block = ODESystem( - [D(x(t)) ~ v(t), D(v(t)) ~ u], t; costs = cost, constraints = constr) - block, input_idxs = structural_simplify(block, ([u], [])) + [D(x(t)) ~ v(t), D(v(t)) ~ u(t)], t; costs = cost, constraints = constr) + block, input_idxs = structural_simplify(block, ([u(t)], [])) u0map = [x(t) => 0.0, v(t) => 0.0] tspan = (0.0, 1.0) @@ -113,19 +113,19 @@ end # Test reached final position. @test ≈(jsol.sol.u[end][1], 0.25, rtol = 1e-5) # Test dynamics - @parameters (u_interp::LinearInterpolation)(..) - block_ode = ODESystem([D(x(t)) ~ v(t), D(v(t)) ~ u_interp(t)], t) - spline = ctrl_to_spline(jsol.input_sol, LinearInterpolation) - oprob = ODEProblem(block, u0map, tspan, [u_interp => spline]) - osol = solve(oprob, Vern8()) - @test jsol.sol.u ≈ osol.u + @parameters (u_interp::ConstantInterpolation)(..) + @mtkbuild block_ode = ODESystem([D(x(t)) ~ v(t), D(v(t)) ~ u_interp(t)], t) + spline = ctrl_to_spline(jsol.input_sol, ConstantInterpolation) + oprob = ODEProblem(block_ode, u0map, tspan, [u_interp => spline]) + osol = solve(oprob, Vern8(), dt = 0.01, adaptive = false) + @test ≈(jsol.sol.u, osol.u, rtol = 0.05) iprob = InfiniteOptControlProblem(block, u0map, tspan, parammap; dt = 0.01) isol = solve(iprob, Ipopt.Optimizer; silent = true) @test is_bangbang(isol.input_sol, [-1.0], [1.0]) @test ≈(isol.sol.u[end][1], 0.25, rtol = 1e-5) - osol = solve(oprob, ImplicitEuler()) - @test isol.sol.u ≈ osol.u + osol = solve(oprob, ImplicitEuler(); dt = 0.01, adaptive = false) + @test ≈(isol.sol.u, osol.u, rtol = 0.05) ################### ### Bee example ### @@ -154,10 +154,12 @@ end @parameters (α_interp::LinearInterpolation)(..) eqs = [D(w(t)) ~ -μ * w(t) + b * s * α_interp(t) * w(t), D(q(t)) ~ -ν * q(t) + c * (1 - α_interp(t)) * s * w(t)] - beesys_ode = ODESystem(eqs, t) - oprob = ODEProblem(beesys_ode, u0map, tspan, [α_interp => ctrl_to_spline(jsol.input_sol, LinearInterpolation)]) - osol = solve(oprob, Tsit5()) - @test osol.u ≈ jsol.sol.u + @mtkbuild beesys_ode = ODESystem(eqs, t) + oprob = ODEProblem(beesys_ode, u0map, tspan, merge(Dict(pmap), Dict(α_interp => ctrl_to_spline(jsol.input_sol, LinearInterpolation)))) + osol = solve(oprob, Tsit5(); dt = 0.01, adaptive = false) + @test ≈(osol.u, jsol.sol.u, rtol = 0.01) + osol2 = solve(oprob, ImplicitEuler(); dt = 0.01, adaptive = false) + @test ≈(osol2.u, isol.sol.u, rtol = 0.01) end @testset "Rocket launch" begin @@ -175,8 +177,8 @@ end (ts, te) = (0.0, 0.2) costs = [-h(te)] - constraints = [T(te) ~ 0] - @named rocket = ODESystem(eqs, t; costs, constraints) + cons = [T(te) ~ 0] + @named rocket = ODESystem(eqs, t; costs, constraints = cons) rocket, input_idxs = structural_simplify(rocket, ([T(t)], [])) u0map = [h(t) => h₀, m(t) => m₀, v(t) => 0] @@ -184,18 +186,22 @@ end g₀ => 1, m₀ => 1.0, h_c => 500, c => 0.5 * √(g₀ * h₀), D_c => 0.5 * 620 * m₀ / g₀, Tₘ => 3.5 * g₀ * m₀, T(t) => 0.0, h₀ => 1, m_c => 0.6] jprob = JuMPControlProblem(rocket, u0map, (ts, te), pmap; dt = 0.005, cse = false) - jsol = solve(jprob, Ipopt.Optimizer, :RadauIA3) + jsol = solve(jprob, Ipopt.Optimizer, :RadauIIA3) @test jsol.sol.u[end][1] > 1.012 + + iprob = InfiniteOptControlProblem(rocket, u0map, (ts, te), pmap; dt = 0.005, cse = false) + isol = solve(iprob, Ipopt.Optimizer, derivative_method = OrthogonalCollocation(3)) + @test isol.sol.u[end][1] > 1.012 # Test solution @parameters (T_interp::CubicSpline)(..) eqs = [D(h(t)) ~ v(t), D(v(t)) ~ (T_interp(t) - drag(h(t), v(t))) / m(t) - gravity(h(t)), D(m(t)) ~ -T_interp(t) / c] - rocket_ode = ODESystem(eqs, t) - interpmap = Dict(T_interp => ctrl_to_spline(jsol.inputsol, CubicSpline)) - oprob = ODEProblem(rocket_ode, u0map, tspan, merge(pmap, interpmap)) - osol = solve(oprob, RadauIA3()) + @mtkbuild rocket_ode = ODESystem(eqs, t) + interpmap = Dict(T_interp => ctrl_to_spline(jsol.input_sol, CubicSpline)) + oprob = ODEProblem(rocket_ode, u0map, (ts, te), merge(Dict(pmap), interpmap)) + osol = solve(oprob, RadauIIA3()) @test jsol.sol.u ≈ osol.u end @@ -223,10 +229,44 @@ end @test isapprox(isol.sol.t[end], 10.0, rtol = 1e-3) end -using JuliaSimCompiler -using Multibody.PlanarMechanics - @testset "Cart-pole problem" begin + # gravity, length, moment of Inertia, drag coeff + @parameters g l mₚ mₖ + @variables x(..) θ(..) u(t) [input = true, bounds = (-10, 10)] + + s = sin(θ(t)) + c = cos(θ(t)) + H = [mₖ+mₚ mₚ*l*c + mₚ*l*c mₚ*l^2] + C = [0 -mₚ*D(θ(t))*l*s + 0 0] + qd = [D(x(t)), D(θ(t))] + G = [0, mₚ*g*l*s] + B = [1, 0] + + tf = 5 + rhss = -H \ Vector(C*qd + G - B*u) + eqs = [D(D(x(t))) ~ rhss[1], D(D(θ(t))) ~ rhss[2]] + cons = [θ(tf) ~ π, x(tf) ~ 0, D(θ(tf)) ~ 0, D(x(tf)) ~ 0] + costs = [∫(u^2)] + tspan = (0, tf) + + @named cartpole = ODESystem(eqs, t; costs, constraints = cons) + cartpole, input_idxs = structural_simplify(cartpole, ([u], [])) + + u0map = [D(x(t)) => 0., D(θ(t)) => 0., θ(t) => 0., x(t) => 0.] + pmap = [mₖ => 1., mₚ => 0.2, l => 0.5, g => 9.81, u => 0] + jprob = JuMPControlProblem(cartpole, u0map, tspan, pmap; dt = 0.04) + jsol = solve(jprob, Ipopt.Optimizer, :RK4) + @test jsol.sol.u[end] ≈ [π, 0, 0, 0] + + iprob = InfiniteOptControlProblem(cartpole, u0map, tspan, pmap; dt = 0.04) + isol = solve(iprob, Ipopt.Optimizer) + @test isol.sol.u[end] ≈ [π, 0, 0, 0] +end + +# RC Circuit +@testset "MTK Components" begin end #@testset "Constrained optimal control problems" begin diff --git a/test/runtests.jl b/test/runtests.jl index 08e7425a3c..0e0ea29a2c 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -22,6 +22,12 @@ function activate_downstream_env() Pkg.instantiate() end +function activate_dynamic_optimization_env() + Pkg.activate("dynamic_optimization") + Pkg.develop(PackageSpec(path = dirname(@__DIR__))) + Pkg.instantiate() +end + @time begin if GROUP == "All" || GROUP == "InterfaceI" @testset "InterfaceI" begin @@ -143,4 +149,9 @@ end @safetestset "InfiniteOpt Extension Test" include("extensions/test_infiniteopt.jl") @safetestset "JuMPControl Extension Test" include("extensions/jump_control.jl") end + + if GROUP == "All" || GROUP == "Dynamic Optimization" + activate_dynamic_optimization_env() + @safetestset "JuMP Collocation Solvers" include("dynamic_optimization/jump_control") + end end From 55c3933ded706a097eb94b5911ae32172a89ecd8 Mon Sep 17 00:00:00 2001 From: vyudu Date: Fri, 25 Apr 2025 14:56:55 -0400 Subject: [PATCH 1442/2176] more test fixes --- ext/MTKJuMPControlExt.jl | 9 ++- src/systems/diffeqs/odesystem.jl | 1 - test/dynamic_optimization/jump_control.jl | 78 +++++++++++++---------- 3 files changed, 50 insertions(+), 38 deletions(-) diff --git a/ext/MTKJuMPControlExt.jl b/ext/MTKJuMPControlExt.jl index 463431e235..45cc9e4365 100644 --- a/ext/MTKJuMPControlExt.jl +++ b/ext/MTKJuMPControlExt.jl @@ -289,17 +289,16 @@ function add_jump_solve_constraints!(prob, tableau; is_free_t = false) base_name="solve_time_$τ") empty!(K) end - @show num_variables(model) else - @variable(model, K[1:length(α), 1:nᵤ], Infinite(t), start=tsteps[1]) + @variable(model, K[1:length(α), 1:nᵤ], Infinite(t)) ΔUs = A * K ΔU_tot = dt * (K' * α) for τ in tsteps for (i, h) in enumerate(c) ΔU = @view ΔUs[i, :] - Uₙ = U + ΔU * dt - @constraint(model, [j = 1:nᵤ], K[i, j](τ)==(tₛ * f(Uₙ, V, p, τ + h * dt)[j]), - DomainRestrictions(t => min(τ + h * dt, tmax)), base_name="solve_K($τ)") + Uₙ = U + ΔU * h * dt + @constraint(model, [j = 1:nᵤ], K[i, j]==(tₛ * f(Uₙ, V, p, τ + h * dt)[j]), + DomainRestrictions(t => τ), base_name="solve_K$i($τ)") end @constraint(model, [n = 1:nᵤ], U[n](τ) + ΔU_tot[n]==U[n](min(τ + dt, tmax)), DomainRestrictions(t => τ), base_name="solve_U($τ)") diff --git a/src/systems/diffeqs/odesystem.jl b/src/systems/diffeqs/odesystem.jl index 9f0f3d00ed..f80b5e4c91 100644 --- a/src/systems/diffeqs/odesystem.jl +++ b/src/systems/diffeqs/odesystem.jl @@ -767,7 +767,6 @@ function process_constraint_system( collect_vars!(constraintsts, constraintps, cons, iv) union!(constraintsts, collect_applied_operators(cons, Differential)) end - @show constraintsts # Validate the states. validate_vars_and_find_ps!(constraintsts, constraintps, sts, iv) diff --git a/test/dynamic_optimization/jump_control.jl b/test/dynamic_optimization/jump_control.jl index 1565cf387f..43ccedf33e 100644 --- a/test/dynamic_optimization/jump_control.jl +++ b/test/dynamic_optimization/jump_control.jl @@ -4,10 +4,8 @@ using DiffEqDevTools, DiffEqBase using SimpleDiffEq using OrdinaryDiffEqSDIRK, OrdinaryDiffEqVerner, OrdinaryDiffEqTsit5, OrdinaryDiffEqFIRK using Ipopt -using BenchmarkTools -using CairoMakie using DataInterpolations -const M = ModelingToolkit +#const M = ModelingToolkit @testset "ODE Solution, no cost" begin # Test solving without anything attached. @@ -26,19 +24,19 @@ const M = ModelingToolkit # Test explicit method. jprob = JuMPControlProblem(sys, u0map, tspan, parammap, dt = 0.01) - @test num_constraints(jprob.model) == 2 # initials + @test JuMP.num_constraints(jprob.model) == 2 # initials jsol = solve(jprob, Ipopt.Optimizer, :RK4) oprob = ODEProblem(sys, u0map, tspan, parammap) osol = solve(oprob, SimpleRK4(), dt = 0.01) @test jsol.sol.u ≈ osol.u # Implicit method. - jsol2 = @btime solve($jprob, Ipopt.Optimizer, :ImplicitEuler, silent = true) # 63.031 ms, 26.49 MiB - osol2 = @btime solve($oprob, ImplicitEuler(), dt = 0.01, adaptive = false) # 129.375 μs, 61.91 KiB + jsol2 = solve(jprob, Ipopt.Optimizer, :ImplicitEuler, silent = true) # 63.031 ms, 26.49 MiB + osol2 = solve(oprob, ImplicitEuler(), dt = 0.01, adaptive = false) # 129.375 μs, 61.91 KiB @test ≈(jsol2.sol.u, osol2.u, rtol = 0.001) iprob = InfiniteOptControlProblem(sys, u0map, tspan, parammap, dt = 0.01) - isol = @btime solve( - $iprob, Ipopt.Optimizer, derivative_method = FiniteDifference(Backward()), silent = true) # 11.540 ms, 4.00 MiB + isol = solve(iprob, Ipopt.Optimizer, derivative_method = InfiniteOpt.FiniteDifference(InfiniteOpt.Backward()), silent = true) # 11.540 ms, 4.00 MiB + @test ≈(jsol2.sol.u, osol2.u, rtol = 0.001) # With a constraint u0map = Pair[] @@ -47,34 +45,29 @@ const M = ModelingToolkit @mtkbuild lksys = ODESystem(eqs, t; constraints = constr) jprob = JuMPControlProblem(lksys, u0map, tspan, parammap; guesses = guess, dt = 0.01) - @test num_constraints(jprob.model) == 2 - jsol = @btime solve($jprob, Ipopt.Optimizer, :Tsitouras5, silent = true) # 12.190 s, 9.68 GiB - sol = jsol.sol - @test sol(0.6)[1] ≈ 3.5 - @test sol(0.3)[1] ≈ 7.0 + @test JuMP.num_constraints(jprob.model) == 2 + jsol = solve(jprob, Ipopt.Optimizer, :Tsitouras5, silent = true) # 12.190 s, 9.68 GiB + @test jsol.sol(0.6)[1] ≈ 3.5 + @test jsol.sol(0.3)[1] ≈ 7.0 iprob = InfiniteOptControlProblem( lksys, u0map, tspan, parammap; guesses = guess, dt = 0.01) - isol = @btime solve( - $iprob, Ipopt.Optimizer, derivative_method = OrthogonalCollocation(3), silent = true) # 48.564 ms, 9.58 MiB + isol = solve(iprob, Ipopt.Optimizer, derivative_method = InfiniteOpt.OrthogonalCollocation(3), silent = true) # 48.564 ms, 9.58 MiB sol = isol.sol @test sol(0.6)[1] ≈ 3.5 @test sol(0.3)[1] ≈ 7.0 # Test whole-interval constraints - constr = [x(t) > 3, y(t) > 4] + constr = [x(t) ≳ 1, y(t) ≳ 1] @mtkbuild lksys = ODESystem(eqs, t; constraints = constr) iprob = InfiniteOptControlProblem( lksys, u0map, tspan, parammap; guesses = guess, dt = 0.01) - isol = @btime solve( - $iprob, Ipopt.Optimizer, derivative_method = OrthogonalCollocation(3), silent = true) # 48.564 ms, 9.58 MiB - sol = isol.sol - @test all(u -> u .> [3, 4], sol.u) + isol = solve(iprob, Ipopt.Optimizer, derivative_method = InfiniteOpt.OrthogonalCollocation(3), silent = true) # 48.564 ms, 9.58 MiB + @test all(u -> u > [1, 1], isol.sol.u) jprob = JuMPControlProblem(lksys, u0map, tspan, parammap; guesses = guess, dt = 0.01) - jsol = @btime solve($jprob, Ipopt.Optimizer, :RadauIA3, silent = true) # 12.190 s, 9.68 GiB - sol = jsol.sol - @test all(u -> u .> [3, 4], sol.u) + jsol = solve(jprob, Ipopt.Optimizer, :RadauIA3, silent = true) # 12.190 s, 9.68 GiB + @test all(u -> u > [1, 1], jsol.sol.u) end function is_bangbang(input_sol, lbounds, ubounds, rtol = 1e-4) @@ -186,11 +179,11 @@ end g₀ => 1, m₀ => 1.0, h_c => 500, c => 0.5 * √(g₀ * h₀), D_c => 0.5 * 620 * m₀ / g₀, Tₘ => 3.5 * g₀ * m₀, T(t) => 0.0, h₀ => 1, m_c => 0.6] jprob = JuMPControlProblem(rocket, u0map, (ts, te), pmap; dt = 0.005, cse = false) - jsol = solve(jprob, Ipopt.Optimizer, :RadauIIA3) + jsol = solve(jprob, Ipopt.Optimizer, :RadauIIA5, silent = true) @test jsol.sol.u[end][1] > 1.012 iprob = InfiniteOptControlProblem(rocket, u0map, (ts, te), pmap; dt = 0.005, cse = false) - isol = solve(iprob, Ipopt.Optimizer, derivative_method = OrthogonalCollocation(3)) + isol = solve(iprob, Ipopt.Optimizer, derivative_method = InfiniteOpt.OrthogonalCollocation(3), silent = true) @test isol.sol.u[end][1] > 1.012 # Test solution @@ -201,8 +194,8 @@ end @mtkbuild rocket_ode = ODESystem(eqs, t) interpmap = Dict(T_interp => ctrl_to_spline(jsol.input_sol, CubicSpline)) oprob = ODEProblem(rocket_ode, u0map, (ts, te), merge(Dict(pmap), interpmap)) - osol = solve(oprob, RadauIIA3()) - @test jsol.sol.u ≈ osol.u + osol = solve(oprob, RadauIIA5(); adaptive = false, dt = 0.005) + @test ≈(jsol.sol.u, osol.u, rtol = 0.02) end @testset "Free final time problem" begin @@ -266,8 +259,29 @@ end end # RC Circuit -@testset "MTK Components" begin -end - -#@testset "Constrained optimal control problems" begin -#end +# using ModelingToolkitStandardLibrary.Electrical +# @testset "MTK Components" begin +# @mtkmodel RL begin +# @parameters begin +# R = 1.0 +# L = 1.0 +# end +# @components begin +# resistor = Resistor(R = R) +# inductor = Inductor(L = L) +# source = Voltage() +# ground = Ground() +# end +# @equations begin +# connect(source.p, resistor.p) +# connect(resistor.n, inductor.p) +# connect(inductor.n, source.n, ground.g) +# end +# end +# +# costs = [] +# coalesce = sum +# @named sys = RL() +# sys, _ = structural_simplify(sys, inputs = [sys.source.V.u]) +# @parameters tf λ i₀ +# end From 7e41540f44a8a9559867d0434afb56e7dc231c7c Mon Sep 17 00:00:00 2001 From: vyudu Date: Fri, 25 Apr 2025 15:01:21 -0400 Subject: [PATCH 1443/2176] clean up comments and tests --- src/systems/optimal_control_interface.jl | 14 ------------ src/systems/systems.jl | 5 ---- test/dynamic_optimization/jump_control.jl | 28 ----------------------- test/extensions/Project.toml | 2 -- 4 files changed, 49 deletions(-) diff --git a/src/systems/optimal_control_interface.jl b/src/systems/optimal_control_interface.jl index c6d46bb0ba..fbf04c1b99 100644 --- a/src/systems/optimal_control_interface.jl +++ b/src/systems/optimal_control_interface.jl @@ -185,17 +185,3 @@ function process_tspan(tspan, dt, steps) return length(tspan[1]:dt:tspan[2]), false end end - -#""" -#$(SIGNATURES) -# -#Define one or more inputs. -# -#See also [`@independent_variables`](@ref), [`@variables`](@ref) and [`@constants`](@ref). -#""" -#macro inputs(xs...) -# Symbolics._parse_vars(:inputs, -# Real, -# xs, -# toparam) |> esc -#end diff --git a/src/systems/systems.jl b/src/systems/systems.jl index 9da7249300..52f93afb9b 100644 --- a/src/systems/systems.jl +++ b/src/systems/systems.jl @@ -163,11 +163,6 @@ function __structural_simplify( end end -function toterm_auxsystems(system::ODESystem) - constraints = system.constraintsystem.constraints - -end - """ $(TYPEDSIGNATURES) diff --git a/test/dynamic_optimization/jump_control.jl b/test/dynamic_optimization/jump_control.jl index 43ccedf33e..284316d6d7 100644 --- a/test/dynamic_optimization/jump_control.jl +++ b/test/dynamic_optimization/jump_control.jl @@ -257,31 +257,3 @@ end isol = solve(iprob, Ipopt.Optimizer) @test isol.sol.u[end] ≈ [π, 0, 0, 0] end - -# RC Circuit -# using ModelingToolkitStandardLibrary.Electrical -# @testset "MTK Components" begin -# @mtkmodel RL begin -# @parameters begin -# R = 1.0 -# L = 1.0 -# end -# @components begin -# resistor = Resistor(R = R) -# inductor = Inductor(L = L) -# source = Voltage() -# ground = Ground() -# end -# @equations begin -# connect(source.p, resistor.p) -# connect(resistor.n, inductor.p) -# connect(inductor.n, source.n, ground.g) -# end -# end -# -# costs = [] -# coalesce = sum -# @named sys = RL() -# sys, _ = structural_simplify(sys, inputs = [sys.source.V.u]) -# @parameters tf λ i₀ -# end diff --git a/test/extensions/Project.toml b/test/extensions/Project.toml index fe2189b169..03e65b4978 100644 --- a/test/extensions/Project.toml +++ b/test/extensions/Project.toml @@ -2,7 +2,6 @@ BifurcationKit = "0f109fa4-8a5d-4b75-95aa-f515264e7665" ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4" ChainRulesTestUtils = "cdddcdb0-9152-4a09-a978-84456f9df70a" -DiffEqBase = "2b5f629d-d688-5b77-993f-72d75c75574e" ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210" HomotopyContinuation = "f213a82b-91d6-5c5d-acf7-10f1c761b327" InfiniteOpt = "20393b10-9daf-11e9-18c9-8db751c92c57" @@ -13,7 +12,6 @@ NonlinearSolveHomotopyContinuation = "2ac3b008-d579-4536-8c91-a1a5998c2f8b" OrdinaryDiffEqTsit5 = "b1df2697-797e-41e3-8120-5422d3b24e4a" SciMLSensitivity = "1ed8b502-d754-442c-8d5d-10ac956f44a1" SciMLStructures = "53ae85a6-f571-4167-b2af-e1d143709226" -SimpleDiffEq = "05bca326-078c-5bf0-a5bf-ce7c7982d7fd" SymbolicIndexingInterface = "2efcf032-c050-4f8e-a9bb-153293bab1f5" Symbolics = "0c5d862f-8b57-4792-8d23-62f2024744c7" Zygote = "e88e6eb3-aa80-5325-afca-941959d7151f" From feb9da46dc2688800a3b061c2fa0c7cf39505bce Mon Sep 17 00:00:00 2001 From: vyudu Date: Fri, 25 Apr 2025 15:04:09 -0400 Subject: [PATCH 1444/2176] format --- ext/MTKJuMPControlExt.jl | 6 ++-- test/dynamic_optimization/jump_control.jl | 40 ++++++++++++++--------- 2 files changed, 28 insertions(+), 18 deletions(-) diff --git a/ext/MTKJuMPControlExt.jl b/ext/MTKJuMPControlExt.jl index 45cc9e4365..30486f845d 100644 --- a/ext/MTKJuMPControlExt.jl +++ b/ext/MTKJuMPControlExt.jl @@ -126,7 +126,7 @@ function init_model(sys, tspan, steps, u0map, pmap, u0; is_free_t = false) stidxmap = Dict([v => i for (i, v) in enumerate(states)]) u0map = Dict([MTK.default_toterm(MTK.value(k)) => v for (k, v) in u0map]) u0_idxs = has_alg_eqs(sys) ? collect(1:length(states)) : - [stidxmap[MTK.default_toterm(k)] for (k, v) in u0map] + [stidxmap[MTK.default_toterm(k)] for (k, v) in u0map] add_initial_constraints!(model, u0, u0_idxs, tspan[1]) return model end @@ -193,7 +193,7 @@ function add_user_constraints!(model::InfiniteModel, sys, pmap; is_free_t = fals auxmap = Dict([u => MTK.default_toterm(MTK.value(u)) for u in unknowns(conssys)]) jconstraints = substitute_jump_vars(model, sys, pmap, jconstraints; auxmap) - + # Substitute to-term'd variables for (i, cons) in enumerate(jconstraints) if cons isa Equation @@ -298,7 +298,7 @@ function add_jump_solve_constraints!(prob, tableau; is_free_t = false) ΔU = @view ΔUs[i, :] Uₙ = U + ΔU * h * dt @constraint(model, [j = 1:nᵤ], K[i, j]==(tₛ * f(Uₙ, V, p, τ + h * dt)[j]), - DomainRestrictions(t => τ), base_name="solve_K$i($τ)") + DomainRestrictions(t => τ), base_name="solve_K$i($τ)") end @constraint(model, [n = 1:nᵤ], U[n](τ) + ΔU_tot[n]==U[n](min(τ + dt, tmax)), DomainRestrictions(t => τ), base_name="solve_U($τ)") diff --git a/test/dynamic_optimization/jump_control.jl b/test/dynamic_optimization/jump_control.jl index 284316d6d7..587afa8ecf 100644 --- a/test/dynamic_optimization/jump_control.jl +++ b/test/dynamic_optimization/jump_control.jl @@ -35,7 +35,9 @@ using DataInterpolations osol2 = solve(oprob, ImplicitEuler(), dt = 0.01, adaptive = false) # 129.375 μs, 61.91 KiB @test ≈(jsol2.sol.u, osol2.u, rtol = 0.001) iprob = InfiniteOptControlProblem(sys, u0map, tspan, parammap, dt = 0.01) - isol = solve(iprob, Ipopt.Optimizer, derivative_method = InfiniteOpt.FiniteDifference(InfiniteOpt.Backward()), silent = true) # 11.540 ms, 4.00 MiB + isol = solve(iprob, Ipopt.Optimizer, + derivative_method = InfiniteOpt.FiniteDifference(InfiniteOpt.Backward()), + silent = true) # 11.540 ms, 4.00 MiB @test ≈(jsol2.sol.u, osol2.u, rtol = 0.001) # With a constraint @@ -52,7 +54,8 @@ using DataInterpolations iprob = InfiniteOptControlProblem( lksys, u0map, tspan, parammap; guesses = guess, dt = 0.01) - isol = solve(iprob, Ipopt.Optimizer, derivative_method = InfiniteOpt.OrthogonalCollocation(3), silent = true) # 48.564 ms, 9.58 MiB + isol = solve(iprob, Ipopt.Optimizer, + derivative_method = InfiniteOpt.OrthogonalCollocation(3), silent = true) # 48.564 ms, 9.58 MiB sol = isol.sol @test sol(0.6)[1] ≈ 3.5 @test sol(0.3)[1] ≈ 7.0 @@ -62,7 +65,8 @@ using DataInterpolations @mtkbuild lksys = ODESystem(eqs, t; constraints = constr) iprob = InfiniteOptControlProblem( lksys, u0map, tspan, parammap; guesses = guess, dt = 0.01) - isol = solve(iprob, Ipopt.Optimizer, derivative_method = InfiniteOpt.OrthogonalCollocation(3), silent = true) # 48.564 ms, 9.58 MiB + isol = solve(iprob, Ipopt.Optimizer, + derivative_method = InfiniteOpt.OrthogonalCollocation(3), silent = true) # 48.564 ms, 9.58 MiB @test all(u -> u > [1, 1], isol.sol.u) jprob = JuMPControlProblem(lksys, u0map, tspan, parammap; guesses = guess, dt = 0.01) @@ -93,7 +97,7 @@ end constr = [v(1.0) ~ 0.0] cost = [-x(1.0)] # Maximize the final distance. @named block = ODESystem( - [D(x(t)) ~ v(t), D(v(t)) ~ u(t)], t; costs = cost, constraints = constr) + [D(x(t)) ~ v(t), D(v(t)) ~ u(t)], t; costs = cost, constraints = constr) block, input_idxs = structural_simplify(block, ([u(t)], [])) u0map = [x(t) => 0.0, v(t) => 0.0] @@ -146,9 +150,13 @@ end @parameters (α_interp::LinearInterpolation)(..) eqs = [D(w(t)) ~ -μ * w(t) + b * s * α_interp(t) * w(t), - D(q(t)) ~ -ν * q(t) + c * (1 - α_interp(t)) * s * w(t)] + D(q(t)) ~ -ν * q(t) + c * (1 - α_interp(t)) * s * w(t)] @mtkbuild beesys_ode = ODESystem(eqs, t) - oprob = ODEProblem(beesys_ode, u0map, tspan, merge(Dict(pmap), Dict(α_interp => ctrl_to_spline(jsol.input_sol, LinearInterpolation)))) + oprob = ODEProblem(beesys_ode, + u0map, + tspan, + merge(Dict(pmap), + Dict(α_interp => ctrl_to_spline(jsol.input_sol, LinearInterpolation)))) osol = solve(oprob, Tsit5(); dt = 0.01, adaptive = false) @test ≈(osol.u, jsol.sol.u, rtol = 0.01) osol2 = solve(oprob, ImplicitEuler(); dt = 0.01, adaptive = false) @@ -182,10 +190,12 @@ end jsol = solve(jprob, Ipopt.Optimizer, :RadauIIA5, silent = true) @test jsol.sol.u[end][1] > 1.012 - iprob = InfiniteOptControlProblem(rocket, u0map, (ts, te), pmap; dt = 0.005, cse = false) - isol = solve(iprob, Ipopt.Optimizer, derivative_method = InfiniteOpt.OrthogonalCollocation(3), silent = true) + iprob = InfiniteOptControlProblem( + rocket, u0map, (ts, te), pmap; dt = 0.005, cse = false) + isol = solve(iprob, Ipopt.Optimizer, + derivative_method = InfiniteOpt.OrthogonalCollocation(3), silent = true) @test isol.sol.u[end][1] > 1.012 - + # Test solution @parameters (T_interp::CubicSpline)(..) eqs = [D(h(t)) ~ v(t), @@ -229,16 +239,16 @@ end s = sin(θ(t)) c = cos(θ(t)) - H = [mₖ+mₚ mₚ*l*c + H = [mₖ+mₚ mₚ*l*c mₚ*l*c mₚ*l^2] C = [0 -mₚ*D(θ(t))*l*s - 0 0] + 0 0] qd = [D(x(t)), D(θ(t))] - G = [0, mₚ*g*l*s] + G = [0, mₚ * g * l * s] B = [1, 0] tf = 5 - rhss = -H \ Vector(C*qd + G - B*u) + rhss = -H \ Vector(C * qd + G - B * u) eqs = [D(D(x(t))) ~ rhss[1], D(D(θ(t))) ~ rhss[2]] cons = [θ(tf) ~ π, x(tf) ~ 0, D(θ(tf)) ~ 0, D(x(tf)) ~ 0] costs = [∫(u^2)] @@ -247,8 +257,8 @@ end @named cartpole = ODESystem(eqs, t; costs, constraints = cons) cartpole, input_idxs = structural_simplify(cartpole, ([u], [])) - u0map = [D(x(t)) => 0., D(θ(t)) => 0., θ(t) => 0., x(t) => 0.] - pmap = [mₖ => 1., mₚ => 0.2, l => 0.5, g => 9.81, u => 0] + u0map = [D(x(t)) => 0.0, D(θ(t)) => 0.0, θ(t) => 0.0, x(t) => 0.0] + pmap = [mₖ => 1.0, mₚ => 0.2, l => 0.5, g => 9.81, u => 0] jprob = JuMPControlProblem(cartpole, u0map, tspan, pmap; dt = 0.04) jsol = solve(jprob, Ipopt.Optimizer, :RK4) @test jsol.sol.u[end] ≈ [π, 0, 0, 0] From f388613e890556fc4352dac6f211e74f5e882b8a Mon Sep 17 00:00:00 2001 From: vyudu Date: Fri, 25 Apr 2025 17:49:52 -0400 Subject: [PATCH 1445/2176] rename Control -> DynamicOpt --- Project.toml | 2 +- ...PControlExt.jl => MTKJuMPDynamicOptExt.jl} | 42 ++-- src/ModelingToolkit.jl | 6 +- src/systems/optimal_control_interface.jl | 14 +- test/downstream/Project.toml | 1 + test/dynamic_optimization/jump_control.jl | 32 +-- test/extensions/ad.jl | 1 + test/runtests.jl | 182 +++++++++--------- 8 files changed, 141 insertions(+), 139 deletions(-) rename ext/{MTKJuMPControlExt.jl => MTKJuMPDynamicOptExt.jl} (90%) diff --git a/Project.toml b/Project.toml index e80a811477..09c0afaa92 100644 --- a/Project.toml +++ b/Project.toml @@ -78,7 +78,7 @@ MTKChainRulesCoreExt = "ChainRulesCore" MTKDeepDiffsExt = "DeepDiffs" MTKFMIExt = "FMI" MTKInfiniteOptExt = "InfiniteOpt" -MTKJuMPControlExt = ["JuMP", "DiffEqDevTools", "InfiniteOpt"] +MTKJuMPDynamicOptExt = ["JuMP", "DiffEqDevTools", "InfiniteOpt"] MTKLabelledArraysExt = "LabelledArrays" [compat] diff --git a/ext/MTKJuMPControlExt.jl b/ext/MTKJuMPDynamicOptExt.jl similarity index 90% rename from ext/MTKJuMPControlExt.jl rename to ext/MTKJuMPDynamicOptExt.jl index 30486f845d..882456dab3 100644 --- a/ext/MTKJuMPControlExt.jl +++ b/ext/MTKJuMPDynamicOptExt.jl @@ -1,4 +1,4 @@ -module MTKJuMPControlExt +module MTKJuMPDynamicOptExt using ModelingToolkit using JuMP, InfiniteOpt using DiffEqDevTools, DiffEqBase @@ -6,8 +6,8 @@ using LinearAlgebra using StaticArrays const MTK = ModelingToolkit -struct JuMPControlProblem{uType, tType, isinplace, P, F, K} <: - AbstractOptimalControlProblem{uType, tType, isinplace} +struct JuMPDynamicOptProblem{uType, tType, isinplace, P, F, K} <: + AbstractDynamicOptProblem{uType, tType, isinplace} f::F u0::uType tspan::tType @@ -15,14 +15,14 @@ struct JuMPControlProblem{uType, tType, isinplace, P, F, K} <: model::InfiniteModel kwargs::K - function JuMPControlProblem(f, u0, tspan, p, model, kwargs...) + function JuMPDynamicOptProblem(f, u0, tspan, p, model, kwargs...) new{typeof(u0), typeof(tspan), SciMLBase.isinplace(f, 5), typeof(p), typeof(f), typeof(kwargs)}(f, u0, tspan, p, model, kwargs) end end -struct InfiniteOptControlProblem{uType, tType, isinplace, P, F, K} <: - AbstractOptimalControlProblem{uType, tType, isinplace} +struct InfiniteOptDynamicOptProblem{uType, tType, isinplace, P, F, K} <: + AbstractDynamicOptProblem{uType, tType, isinplace} f::F u0::uType tspan::tType @@ -30,14 +30,14 @@ struct InfiniteOptControlProblem{uType, tType, isinplace, P, F, K} <: model::InfiniteModel kwargs::K - function InfiniteOptControlProblem(f, u0, tspan, p, model, kwargs...) + function InfiniteOptDynamicOptProblem(f, u0, tspan, p, model, kwargs...) new{typeof(u0), typeof(tspan), SciMLBase.isinplace(f), typeof(p), typeof(f), typeof(kwargs)}(f, u0, tspan, p, model, kwargs) end end """ - JuMPControlProblem(sys::ODESystem, u0, tspan, p; dt) + JuMPDynamicOptProblem(sys::ODESystem, u0, tspan, p; dt) Convert an ODESystem representing an optimal control system into a JuMP model for solving using optimization. Must provide either `dt`, the timestep between collocation @@ -52,7 +52,7 @@ The constraints are: - The set of user constraints passed to the ODESystem via `constraints` - The solver constraints that encode the time-stepping used by the solver """ -function MTK.JuMPControlProblem(sys::ODESystem, u0map, tspan, pmap; +function MTK.JuMPDynamicOptProblem(sys::ODESystem, u0map, tspan, pmap; dt = nothing, steps = nothing, guesses = Dict(), kwargs...) @@ -65,20 +65,20 @@ function MTK.JuMPControlProblem(sys::ODESystem, u0map, tspan, pmap; steps, is_free_t = MTK.process_tspan(tspan, dt, steps) model = init_model(sys, tspan, steps, u0map, pmap, u0; is_free_t) - JuMPControlProblem(f, u0, tspan, p, model, kwargs...) + JuMPDynamicOptProblem(f, u0, tspan, p, model, kwargs...) end """ - InfiniteOptControlProblem(sys::ODESystem, u0map, tspan, pmap; dt) + InfiniteOptDynamicOptProblem(sys::ODESystem, u0map, tspan, pmap; dt) Convert an ODESystem representing an optimal control system into a InfiniteOpt model for solving using optimization. Must provide `dt` for determining the length of the interpolation arrays. -Related to `JuMPControlProblem`, but directly adds the differential equations +Related to `JuMPDynamicOptProblem`, but directly adds the differential equations of the system as derivative constraints, rather than using a solver tableau. """ -function MTK.InfiniteOptControlProblem(sys::ODESystem, u0map, tspan, pmap; +function MTK.InfiniteOptDynamicOptProblem(sys::ODESystem, u0map, tspan, pmap; dt = nothing, steps = nothing, guesses = Dict(), kwargs...) @@ -92,7 +92,7 @@ function MTK.InfiniteOptControlProblem(sys::ODESystem, u0map, tspan, pmap; model = init_model(sys, tspan, steps, u0map, pmap, u0; is_free_t) add_infopt_solve_constraints!(model, sys, pmap; is_free_t) - InfiniteOptControlProblem(f, u0, tspan, p, model, kwargs...) + InfiniteOptDynamicOptProblem(f, u0, tspan, p, model, kwargs...) end # Initialize InfiniteOpt model. @@ -307,16 +307,16 @@ function add_jump_solve_constraints!(prob, tableau; is_free_t = false) end """ -Solve JuMPControlProblem. Arguments: -- prob: a JumpControlProblem +Solve JuMPDynamicOptProblem. Arguments: +- prob: a JumpDynamicOptProblem - jump_solver: a LP solver such as HiGHS - ode_solver: Takes in a symbol representing the solver. Acceptable solvers may be found at https://docs.sciml.ai/DiffEqDevDocs/stable/internals/tableaus/. Note that the symbol may be different than the typical name of the solver, e.g. :Tsitouras5 rather than Tsit5. - silent: set the model silent (suppress model output) -Returns a JuMPControlSolution, which contains both the model and the ODE solution. +Returns a DynamicOptSolution, which contains both the model and the ODE solution. """ function DiffEqBase.solve( - prob::JuMPControlProblem, jump_solver, ode_solver::Symbol; silent = false) + prob::JuMPDynamicOptProblem, jump_solver, ode_solver::Symbol; silent = false) model = prob.model tableau_getter = Symbol(:construct, ode_solver) @eval tableau = $tableau_getter() @@ -343,7 +343,7 @@ end """ `derivative_method` kwarg refers to the method used by InfiniteOpt to compute derivatives. The list of possible options can be found at https://infiniteopt.github.io/InfiniteOpt.jl/stable/guide/derivative/. Defaults to FiniteDifference(Backward()). """ -function DiffEqBase.solve(prob::InfiniteOptControlProblem, jump_solver; +function DiffEqBase.solve(prob::InfiniteOptDynamicOptProblem, jump_solver; derivative_method = InfiniteOpt.FiniteDifference(Backward()), silent = false) model = prob.model silent && set_silent(model) @@ -351,7 +351,7 @@ function DiffEqBase.solve(prob::InfiniteOptControlProblem, jump_solver; _solve(prob, jump_solver, derivative_method) end -function _solve(prob::AbstractOptimalControlProblem, jump_solver, solver) +function _solve(prob::AbstractDynamicOptProblem, jump_solver, solver) model = prob.model set_optimizer(model, jump_solver) optimize!(model) @@ -382,7 +382,7 @@ function _solve(prob::AbstractOptimalControlProblem, jump_solver, solver) input_sol, SciMLBase.ReturnCode.ConvergenceFailure)) end - OptimalControlSolution(model, sol, input_sol) + DynamicOptSolution(model, sol, input_sol) end end diff --git a/src/ModelingToolkit.jl b/src/ModelingToolkit.jl index 2fb7c41b0d..a7d4f58fce 100644 --- a/src/ModelingToolkit.jl +++ b/src/ModelingToolkit.jl @@ -349,9 +349,9 @@ export AnalysisPoint, get_sensitivity_function, get_comp_sensitivity_function, function FMIComponent end include("systems/optimal_control_interface.jl") -export AbstractOptimalControlProblem, JuMPControlProblem, InfiniteOptControlProblem, - PyomoControlProblem, CasADiControlProblem -export OptimalControlSolution +export AbstractDynamicOptProblem, JuMPDynamicOptProblem, InfiniteOptDynamicOptProblem, + PyomoDynamicOptProblem, CasADiDynamicOptProblem +export DynamicOptSolution export ∫ end # module diff --git a/src/systems/optimal_control_interface.jl b/src/systems/optimal_control_interface.jl index fbf04c1b99..1183d04db6 100644 --- a/src/systems/optimal_control_interface.jl +++ b/src/systems/optimal_control_interface.jl @@ -1,13 +1,13 @@ -abstract type AbstractOptimalControlProblem{uType, tType, isinplace} <: +abstract type AbstractDynamicOptProblem{uType, tType, isinplace} <: SciMLBase.AbstractODEProblem{uType, tType, isinplace} end -struct OptimalControlSolution +struct DynamicOptSolution model::Any sol::ODESolution input_sol::Union{Nothing, ODESolution} end -function Base.show(io::IO, sol::OptimalControlSolution) +function Base.show(io::IO, sol::DynamicOptSolution) println("retcode: ", sol.sol.retcode, "\n") println("Optimal control solution for following model:\n") @@ -16,10 +16,10 @@ function Base.show(io::IO, sol::OptimalControlSolution) print("\n\nPlease query the model using sol.model, the solution trajectory for the system using sol.sol, or the solution trajectory for the controllers using sol.input_sol.") end -function JuMPControlProblem end -function InfiniteOptControlProblem end -function CasADiControlProblem end -function PyomoControlProblem end +function JuMPDynamicOptProblem end +function InfiniteOptDynamicOptProblem end +function CasADiDynamicOptProblem end +function PyomoDynamicOptProblem end function warn_overdetermined(sys, u0map) constraintsys = get_constraintsystem(sys) diff --git a/test/downstream/Project.toml b/test/downstream/Project.toml index ade09e797b..aa58095744 100644 --- a/test/downstream/Project.toml +++ b/test/downstream/Project.toml @@ -4,6 +4,7 @@ LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" ModelingToolkit = "961ee093-0014-501f-94e3-6117800e7a78" ModelingToolkitStandardLibrary = "16a59e39-deab-5bd0-87e4-056b12336739" OrdinaryDiffEq = "1dea7af3-3e70-54e6-95c3-0bf5283fa5ed" +OrdinaryDiffEqNonlinearSolve = "127b3ac7-2247-4354-8eb6-78cf4e7c58e8" SymbolicIndexingInterface = "2efcf032-c050-4f8e-a9bb-153293bab1f5" [compat] diff --git a/test/dynamic_optimization/jump_control.jl b/test/dynamic_optimization/jump_control.jl index 587afa8ecf..431491ddcd 100644 --- a/test/dynamic_optimization/jump_control.jl +++ b/test/dynamic_optimization/jump_control.jl @@ -23,7 +23,7 @@ using DataInterpolations parammap = [α => 1.5, β => 1.0, γ => 3.0, δ => 1.0] # Test explicit method. - jprob = JuMPControlProblem(sys, u0map, tspan, parammap, dt = 0.01) + jprob = JuMPDynamicOptProblem(sys, u0map, tspan, parammap, dt = 0.01) @test JuMP.num_constraints(jprob.model) == 2 # initials jsol = solve(jprob, Ipopt.Optimizer, :RK4) oprob = ODEProblem(sys, u0map, tspan, parammap) @@ -34,7 +34,7 @@ using DataInterpolations jsol2 = solve(jprob, Ipopt.Optimizer, :ImplicitEuler, silent = true) # 63.031 ms, 26.49 MiB osol2 = solve(oprob, ImplicitEuler(), dt = 0.01, adaptive = false) # 129.375 μs, 61.91 KiB @test ≈(jsol2.sol.u, osol2.u, rtol = 0.001) - iprob = InfiniteOptControlProblem(sys, u0map, tspan, parammap, dt = 0.01) + iprob = InfiniteOptDynamicOptProblem(sys, u0map, tspan, parammap, dt = 0.01) isol = solve(iprob, Ipopt.Optimizer, derivative_method = InfiniteOpt.FiniteDifference(InfiniteOpt.Backward()), silent = true) # 11.540 ms, 4.00 MiB @@ -46,13 +46,13 @@ using DataInterpolations constr = [x(0.6) ~ 3.5, x(0.3) ~ 7.0] @mtkbuild lksys = ODESystem(eqs, t; constraints = constr) - jprob = JuMPControlProblem(lksys, u0map, tspan, parammap; guesses = guess, dt = 0.01) + jprob = JuMPDynamicOptProblem(lksys, u0map, tspan, parammap; guesses = guess, dt = 0.01) @test JuMP.num_constraints(jprob.model) == 2 jsol = solve(jprob, Ipopt.Optimizer, :Tsitouras5, silent = true) # 12.190 s, 9.68 GiB @test jsol.sol(0.6)[1] ≈ 3.5 @test jsol.sol(0.3)[1] ≈ 7.0 - iprob = InfiniteOptControlProblem( + iprob = InfiniteOptDynamicOptProblem( lksys, u0map, tspan, parammap; guesses = guess, dt = 0.01) isol = solve(iprob, Ipopt.Optimizer, derivative_method = InfiniteOpt.OrthogonalCollocation(3), silent = true) # 48.564 ms, 9.58 MiB @@ -63,13 +63,13 @@ using DataInterpolations # Test whole-interval constraints constr = [x(t) ≳ 1, y(t) ≳ 1] @mtkbuild lksys = ODESystem(eqs, t; constraints = constr) - iprob = InfiniteOptControlProblem( + iprob = InfiniteOptDynamicOptProblem( lksys, u0map, tspan, parammap; guesses = guess, dt = 0.01) isol = solve(iprob, Ipopt.Optimizer, derivative_method = InfiniteOpt.OrthogonalCollocation(3), silent = true) # 48.564 ms, 9.58 MiB @test all(u -> u > [1, 1], isol.sol.u) - jprob = JuMPControlProblem(lksys, u0map, tspan, parammap; guesses = guess, dt = 0.01) + jprob = JuMPDynamicOptProblem(lksys, u0map, tspan, parammap; guesses = guess, dt = 0.01) jsol = solve(jprob, Ipopt.Optimizer, :RadauIA3, silent = true) # 12.190 s, 9.68 GiB @test all(u -> u > [1, 1], jsol.sol.u) end @@ -103,7 +103,7 @@ end u0map = [x(t) => 0.0, v(t) => 0.0] tspan = (0.0, 1.0) parammap = [u(t) => 0.0] - jprob = JuMPControlProblem(block, u0map, tspan, parammap; dt = 0.01) + jprob = JuMPDynamicOptProblem(block, u0map, tspan, parammap; dt = 0.01) jsol = solve(jprob, Ipopt.Optimizer, :Verner8) # Linear systems have bang-bang controls @test is_bangbang(jsol.input_sol, [-1.0], [1.0]) @@ -117,7 +117,7 @@ end osol = solve(oprob, Vern8(), dt = 0.01, adaptive = false) @test ≈(jsol.sol.u, osol.u, rtol = 0.05) - iprob = InfiniteOptControlProblem(block, u0map, tspan, parammap; dt = 0.01) + iprob = InfiniteOptDynamicOptProblem(block, u0map, tspan, parammap; dt = 0.01) isol = solve(iprob, Ipopt.Optimizer; silent = true) @test is_bangbang(isol.input_sol, [-1.0], [1.0]) @test ≈(isol.sol.u[end][1], 0.25, rtol = 1e-5) @@ -141,10 +141,10 @@ end u0map = [w(t) => 40, q(t) => 2] pmap = [b => 1, c => 1, μ => 1, s => 1, ν => 1, α => 1] - jprob = JuMPControlProblem(beesys, u0map, tspan, pmap, dt = 0.01) + jprob = JuMPDynamicOptProblem(beesys, u0map, tspan, pmap, dt = 0.01) jsol = solve(jprob, Ipopt.Optimizer, :Tsitouras5) @test is_bangbang(jsol.input_sol, [0.0], [1.0]) - iprob = InfiniteOptControlProblem(beesys, u0map, tspan, pmap, dt = 0.01) + iprob = InfiniteOptDynamicOptProblem(beesys, u0map, tspan, pmap, dt = 0.01) isol = solve(iprob, Ipopt.Optimizer; silent = true) @test is_bangbang(isol.input_sol, [0.0], [1.0]) @@ -186,11 +186,11 @@ end pmap = [ g₀ => 1, m₀ => 1.0, h_c => 500, c => 0.5 * √(g₀ * h₀), D_c => 0.5 * 620 * m₀ / g₀, Tₘ => 3.5 * g₀ * m₀, T(t) => 0.0, h₀ => 1, m_c => 0.6] - jprob = JuMPControlProblem(rocket, u0map, (ts, te), pmap; dt = 0.005, cse = false) + jprob = JuMPDynamicOptProblem(rocket, u0map, (ts, te), pmap; dt = 0.005, cse = false) jsol = solve(jprob, Ipopt.Optimizer, :RadauIIA5, silent = true) @test jsol.sol.u[end][1] > 1.012 - iprob = InfiniteOptControlProblem( + iprob = InfiniteOptDynamicOptProblem( rocket, u0map, (ts, te), pmap; dt = 0.005, cse = false) isol = solve(iprob, Ipopt.Optimizer, derivative_method = InfiniteOpt.OrthogonalCollocation(3), silent = true) @@ -223,11 +223,11 @@ end u0map = [x(t) => 17.5] pmap = [u(t) => 0.0, tf => 8] - jprob = JuMPControlProblem(rocket, u0map, (0, tf), pmap; steps = 201) + jprob = JuMPDynamicOptProblem(rocket, u0map, (0, tf), pmap; steps = 201) jsol = solve(jprob, Ipopt.Optimizer, :Tsitouras5) @test isapprox(jsol.sol.t[end], 10.0, rtol = 1e-3) - iprob = InfiniteOptControlProblem(rocket, u0map, (0, tf), pmap; steps = 200) + iprob = InfiniteOptDynamicOptProblem(rocket, u0map, (0, tf), pmap; steps = 200) isol = solve(iprob, Ipopt.Optimizer) @test isapprox(isol.sol.t[end], 10.0, rtol = 1e-3) end @@ -259,11 +259,11 @@ end u0map = [D(x(t)) => 0.0, D(θ(t)) => 0.0, θ(t) => 0.0, x(t) => 0.0] pmap = [mₖ => 1.0, mₚ => 0.2, l => 0.5, g => 9.81, u => 0] - jprob = JuMPControlProblem(cartpole, u0map, tspan, pmap; dt = 0.04) + jprob = JuMPDynamicOptProblem(cartpole, u0map, tspan, pmap; dt = 0.04) jsol = solve(jprob, Ipopt.Optimizer, :RK4) @test jsol.sol.u[end] ≈ [π, 0, 0, 0] - iprob = InfiniteOptControlProblem(cartpole, u0map, tspan, pmap; dt = 0.04) + iprob = InfiniteOptDynamicOptProblem(cartpole, u0map, tspan, pmap; dt = 0.04) isol = solve(iprob, Ipopt.Optimizer) @test isol.sol.u[end] ≈ [π, 0, 0, 0] end diff --git a/test/extensions/ad.jl b/test/extensions/ad.jl index a456263655..14649b6bb6 100644 --- a/test/extensions/ad.jl +++ b/test/extensions/ad.jl @@ -4,6 +4,7 @@ using Zygote using SymbolicIndexingInterface using SciMLStructures using OrdinaryDiffEqTsit5 +using OrdinaryDiffEqNonlinearSolve using NonlinearSolve using SciMLSensitivity using ForwardDiff diff --git a/test/runtests.jl b/test/runtests.jl index 0e0ea29a2c..5f27f8bb88 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -29,102 +29,102 @@ function activate_dynamic_optimization_env() end @time begin - if GROUP == "All" || GROUP == "InterfaceI" - @testset "InterfaceI" begin - @safetestset "Linear Algebra Test" include("linalg.jl") - @safetestset "AbstractSystem Test" include("abstractsystem.jl") - @safetestset "Variable Scope Tests" include("variable_scope.jl") - @safetestset "Symbolic Parameters Test" include("symbolic_parameters.jl") - @safetestset "Parsing Test" include("variable_parsing.jl") - @safetestset "Simplify Test" include("simplify.jl") - @safetestset "Direct Usage Test" include("direct.jl") - @safetestset "System Linearity Test" include("linearity.jl") - @safetestset "Input Output Test" include("input_output_handling.jl") - @safetestset "Clock Test" include("clock.jl") - @safetestset "ODESystem Test" include("odesystem.jl") - @safetestset "Dynamic Quantities Test" include("dq_units.jl") - @safetestset "Unitful Quantities Test" include("units.jl") - @safetestset "Mass Matrix Test" include("mass_matrix.jl") - @safetestset "Reduction Test" include("reduction.jl") - @safetestset "Split Parameters Test" include("split_parameters.jl") - @safetestset "StaticArrays Test" include("static_arrays.jl") - @safetestset "Components Test" include("components.jl") - @safetestset "Model Parsing Test" include("model_parsing.jl") - @safetestset "Error Handling" include("error_handling.jl") - @safetestset "StructuralTransformations" include("structural_transformation/runtests.jl") - @safetestset "Basic transformations" include("basic_transformations.jl") - @safetestset "State Selection Test" include("state_selection.jl") - @safetestset "Symbolic Event Test" include("symbolic_events.jl") - @safetestset "Stream Connect Test" include("stream_connectors.jl") - @safetestset "Domain Connect Test" include("domain_connectors.jl") - @safetestset "Lowering Integration Test" include("lowering_solving.jl") - @safetestset "Dependency Graph Test" include("dep_graphs.jl") - @safetestset "Function Registration Test" include("function_registration.jl") - @safetestset "Precompiled Modules Test" include("precompile_test.jl") - @safetestset "DAE Jacobians Test" include("dae_jacobian.jl") - @safetestset "Jacobian Sparsity" include("jacobiansparsity.jl") - @safetestset "Modelingtoolkitize Test" include("modelingtoolkitize.jl") - @safetestset "FuncAffect Test" include("funcaffect.jl") - @safetestset "Constants Test" include("constants.jl") - @safetestset "Parameter Dependency Test" include("parameter_dependencies.jl") - @safetestset "Equation Type Accessors Test" include("equation_type_accessors.jl") - @safetestset "System Accessor Functions Test" include("accessor_functions.jl") - @safetestset "Equations with complex values" include("complex.jl") - end - end + #if GROUP == "All" || GROUP == "InterfaceI" + # @testset "InterfaceI" begin + # @safetestset "Linear Algebra Test" include("linalg.jl") + # @safetestset "AbstractSystem Test" include("abstractsystem.jl") + # @safetestset "Variable Scope Tests" include("variable_scope.jl") + # @safetestset "Symbolic Parameters Test" include("symbolic_parameters.jl") + # @safetestset "Parsing Test" include("variable_parsing.jl") + # @safetestset "Simplify Test" include("simplify.jl") + # @safetestset "Direct Usage Test" include("direct.jl") + # @safetestset "System Linearity Test" include("linearity.jl") + # @safetestset "Input Output Test" include("input_output_handling.jl") + # @safetestset "Clock Test" include("clock.jl") + # @safetestset "ODESystem Test" include("odesystem.jl") + # @safetestset "Dynamic Quantities Test" include("dq_units.jl") + # @safetestset "Unitful Quantities Test" include("units.jl") + # @safetestset "Mass Matrix Test" include("mass_matrix.jl") + # @safetestset "Reduction Test" include("reduction.jl") + # @safetestset "Split Parameters Test" include("split_parameters.jl") + # @safetestset "StaticArrays Test" include("static_arrays.jl") + # @safetestset "Components Test" include("components.jl") + # @safetestset "Model Parsing Test" include("model_parsing.jl") + # @safetestset "Error Handling" include("error_handling.jl") + # @safetestset "StructuralTransformations" include("structural_transformation/runtests.jl") + # @safetestset "Basic transformations" include("basic_transformations.jl") + # @safetestset "State Selection Test" include("state_selection.jl") + # @safetestset "Symbolic Event Test" include("symbolic_events.jl") + # @safetestset "Stream Connect Test" include("stream_connectors.jl") + # @safetestset "Domain Connect Test" include("domain_connectors.jl") + # @safetestset "Lowering Integration Test" include("lowering_solving.jl") + # @safetestset "Dependency Graph Test" include("dep_graphs.jl") + # @safetestset "Function Registration Test" include("function_registration.jl") + # @safetestset "Precompiled Modules Test" include("precompile_test.jl") + # @safetestset "DAE Jacobians Test" include("dae_jacobian.jl") + # @safetestset "Jacobian Sparsity" include("jacobiansparsity.jl") + # @safetestset "Modelingtoolkitize Test" include("modelingtoolkitize.jl") + # @safetestset "FuncAffect Test" include("funcaffect.jl") + # @safetestset "Constants Test" include("constants.jl") + # @safetestset "Parameter Dependency Test" include("parameter_dependencies.jl") + # @safetestset "Equation Type Accessors Test" include("equation_type_accessors.jl") + # @safetestset "System Accessor Functions Test" include("accessor_functions.jl") + # @safetestset "Equations with complex values" include("complex.jl") + # end + #end - if GROUP == "All" || GROUP == "Initialization" - @safetestset "Guess Propagation" include("guess_propagation.jl") - @safetestset "Hierarchical Initialization Equations" include("hierarchical_initialization_eqs.jl") - @safetestset "InitializationSystem Test" include("initializationsystem.jl") - @safetestset "Initial Values Test" include("initial_values.jl") - end + #if GROUP == "All" || GROUP == "Initialization" + # @safetestset "Guess Propagation" include("guess_propagation.jl") + # @safetestset "Hierarchical Initialization Equations" include("hierarchical_initialization_eqs.jl") + # @safetestset "InitializationSystem Test" include("initializationsystem.jl") + # @safetestset "Initial Values Test" include("initial_values.jl") + #end - if GROUP == "All" || GROUP == "InterfaceII" - @testset "InterfaceII" begin - @safetestset "Code Generation Test" include("code_generation.jl") - @safetestset "IndexCache Test" include("index_cache.jl") - @safetestset "Variable Utils Test" include("variable_utils.jl") - @safetestset "Variable Metadata Test" include("test_variable_metadata.jl") - @safetestset "OptimizationSystem Test" include("optimizationsystem.jl") - @safetestset "Discrete System" include("discrete_system.jl") - @safetestset "Implicit Discrete System" include("implicit_discrete_system.jl") - @safetestset "SteadyStateSystem Test" include("steadystatesystems.jl") - @safetestset "SDESystem Test" include("sdesystem.jl") - @safetestset "DDESystem Test" include("dde.jl") - @safetestset "NonlinearSystem Test" include("nonlinearsystem.jl") - @safetestset "SCCNonlinearProblem Test" include("scc_nonlinear_problem.jl") - @safetestset "PDE Construction Test" include("pdesystem.jl") - @safetestset "JumpSystem Test" include("jumpsystem.jl") - @safetestset "Optimal Control + Constraints Tests" include("optimal_control.jl") - @safetestset "print_tree" include("print_tree.jl") - @safetestset "Constraints Test" include("constraints.jl") - @safetestset "IfLifting Test" include("if_lifting.jl") - @safetestset "Analysis Points Test" include("analysis_points.jl") - @safetestset "Causal Variables Connection Test" include("causal_variables_connection.jl") - @safetestset "Debugging Test" include("debugging.jl") - @safetestset "Namespacing test" include("namespacing.jl") - @safetestset "Subsystem replacement" include("substitute_component.jl") - end - end + #if GROUP == "All" || GROUP == "InterfaceII" + # @testset "InterfaceII" begin + # @safetestset "Code Generation Test" include("code_generation.jl") + # @safetestset "IndexCache Test" include("index_cache.jl") + # @safetestset "Variable Utils Test" include("variable_utils.jl") + # @safetestset "Variable Metadata Test" include("test_variable_metadata.jl") + # @safetestset "OptimizationSystem Test" include("optimizationsystem.jl") + # @safetestset "Discrete System" include("discrete_system.jl") + # @safetestset "Implicit Discrete System" include("implicit_discrete_system.jl") + # @safetestset "SteadyStateSystem Test" include("steadystatesystems.jl") + # @safetestset "SDESystem Test" include("sdesystem.jl") + # @safetestset "DDESystem Test" include("dde.jl") + # @safetestset "NonlinearSystem Test" include("nonlinearsystem.jl") + # @safetestset "SCCNonlinearProblem Test" include("scc_nonlinear_problem.jl") + # @safetestset "PDE Construction Test" include("pdesystem.jl") + # @safetestset "JumpSystem Test" include("jumpsystem.jl") + # @safetestset "Optimal Control + Constraints Tests" include("optimal_control.jl") + # @safetestset "print_tree" include("print_tree.jl") + # @safetestset "Constraints Test" include("constraints.jl") + # @safetestset "IfLifting Test" include("if_lifting.jl") + # @safetestset "Analysis Points Test" include("analysis_points.jl") + # @safetestset "Causal Variables Connection Test" include("causal_variables_connection.jl") + # @safetestset "Debugging Test" include("debugging.jl") + # @safetestset "Namespacing test" include("namespacing.jl") + # @safetestset "Subsystem replacement" include("substitute_component.jl") + # end + #end - if GROUP == "All" || GROUP == "SymbolicIndexingInterface" - @safetestset "SymbolicIndexingInterface test" include("symbolic_indexing_interface.jl") - @safetestset "SciML Problem Input Test" include("sciml_problem_inputs.jl") - @safetestset "MTKParameters Test" include("mtkparameters.jl") - end + #if GROUP == "All" || GROUP == "SymbolicIndexingInterface" + # @safetestset "SymbolicIndexingInterface test" include("symbolic_indexing_interface.jl") + # @safetestset "SciML Problem Input Test" include("sciml_problem_inputs.jl") + # @safetestset "MTKParameters Test" include("mtkparameters.jl") + #end - if GROUP == "All" || GROUP == "Extended" - @safetestset "Test Big System Usage" include("bigsystem.jl") - println("C compilation test requires gcc available in the path!") - @safetestset "C Compilation Test" include("ccompile.jl") - @testset "Distributed Test" include("distributed.jl") - @testset "Serialization" include("serialization.jl") - end + #if GROUP == "All" || GROUP == "Extended" + # @safetestset "Test Big System Usage" include("bigsystem.jl") + # println("C compilation test requires gcc available in the path!") + # @safetestset "C Compilation Test" include("ccompile.jl") + # @testset "Distributed Test" include("distributed.jl") + # @testset "Serialization" include("serialization.jl") + #end - if GROUP == "All" || GROUP == "RegressionI" - @safetestset "Latexify recipes Test" include("latexify.jl") - end + #if GROUP == "All" || GROUP == "RegressionI" + # @safetestset "Latexify recipes Test" include("latexify.jl") + #end if GROUP == "All" || GROUP == "Downstream" activate_downstream_env() From 9dcc6b9bc5c8559d95e7429f519ec0b9d006085c Mon Sep 17 00:00:00 2001 From: vyudu Date: Fri, 25 Apr 2025 23:06:54 -0400 Subject: [PATCH 1446/2176] test fixes --- src/inputoutput.jl | 4 ++-- src/structural_transformation/utils.jl | 1 + src/systems/diffeqs/odesystem.jl | 3 +-- test/downstream/test_disturbance_model.jl | 12 ++++++------ test/extensions/Project.toml | 1 + test/input_output_handling.jl | 20 ++++++++++---------- test/odesystem.jl | 12 +++++------- 7 files changed, 26 insertions(+), 27 deletions(-) diff --git a/src/inputoutput.jl b/src/inputoutput.jl index 11a58bd30a..8c1a084c9b 100644 --- a/src/inputoutput.jl +++ b/src/inputoutput.jl @@ -430,7 +430,7 @@ function add_input_disturbance(sys, dist::DisturbanceModel, inputs = nothing; kw augmented_sys = ODESystem(eqs, t, systems = [dsys], name = gensym(:outer)) augmented_sys = extend(augmented_sys, sys) - (f_oop, f_ip), dvs, p, io_sys = generate_control_function(augmented_sys, all_inputs, + f, dvs, p, io_sys = generate_control_function(augmented_sys, all_inputs, [d]; kwargs...) - (f_oop, f_ip), augmented_sys, dvs, p, io_sys + f, augmented_sys, dvs, p, io_sys end diff --git a/src/structural_transformation/utils.jl b/src/structural_transformation/utils.jl index 14628f2958..15a46531d9 100644 --- a/src/structural_transformation/utils.jl +++ b/src/structural_transformation/utils.jl @@ -96,6 +96,7 @@ function check_consistency(state::TransformationState, orig_inputs; nothrow = fa fullvars = get_fullvars(state) neqs = n_concrete_eqs(state) @unpack graph, var_to_diff = state.structure + @show equations(state.sys) highest_vars = computed_highest_diff_variables(complete!(state.structure)) n_highest_vars = 0 for (v, h) in enumerate(highest_vars) diff --git a/src/systems/diffeqs/odesystem.jl b/src/systems/diffeqs/odesystem.jl index f80b5e4c91..1e8293cca3 100644 --- a/src/systems/diffeqs/odesystem.jl +++ b/src/systems/diffeqs/odesystem.jl @@ -803,8 +803,7 @@ function validate_vars_and_find_ps!(auxvars, auxps, sysvars, iv) for var in auxvars if !iscall(var) - occursin(iv, var) && (var ∈ sts || - throw(ArgumentError("Time-dependent variable $var is not an unknown of the system."))) + var ∈ sts || throw(ArgumentError("Time-independent variable $var is not an unknown of the system.")) elseif length(arguments(var)) > 1 throw(ArgumentError("Too many arguments for variable $var.")) elseif length(arguments(var)) == 1 diff --git a/test/downstream/test_disturbance_model.jl b/test/downstream/test_disturbance_model.jl index 97276437e2..c96830efa3 100644 --- a/test/downstream/test_disturbance_model.jl +++ b/test/downstream/test_disturbance_model.jl @@ -149,10 +149,10 @@ sol = solve(prob, Tsit5()) ## Generate function for an augmented Unscented Kalman Filter ===================== # temp = open_loop(model_with_disturbance, :d) outputs = [P.inertia1.phi, P.inertia2.phi, P.inertia1.w, P.inertia2.w] -(f_oop1, f_ip), x_sym, p_sym, io_sys = ModelingToolkit.generate_control_function( +f, x_sym, p_sym, io_sys = ModelingToolkit.generate_control_function( model_with_disturbance, [:u], [:d1, :d2, :dy], split = false) -(f_oop2, f_ip2), x_sym, p_sym, io_sys = ModelingToolkit.generate_control_function( +f, x_sym, p_sym, io_sys = ModelingToolkit.generate_control_function( model_with_disturbance, [:u], [:d1, :d2, :dy], disturbance_argument = true, split = false) @@ -168,22 +168,22 @@ x0, p = ModelingToolkit.get_u0_p(io_sys, op, op) x = zeros(5) u = zeros(1) d = zeros(3) -@test f_oop2(x, u, p, t, d) == zeros(5) +@test f(x, u, p, t, d) == zeros(5) @test measurement(x, u, p, 0.0) == [0, 0, 0, 0] @test measurement2(x, u, p, 0.0, d) == [0] # Add to the integrating disturbance input d = [1, 0, 0] -@test sort(f_oop2(x, u, p, 0.0, d)) == [0, 0, 0, 1, 1] # Affects disturbance state and one velocity +@test sort(f(x, u, p, 0.0, d)) == [0, 0, 0, 1, 1] # Affects disturbance state and one velocity @test measurement2(x, u, p, 0.0, d) == [0] d = [0, 1, 0] -@test sort(f_oop2(x, u, p, 0.0, d)) == [0, 0, 0, 0, 1] # Affects one velocity +@test sort(f(x, u, p, 0.0, d)) == [0, 0, 0, 0, 1] # Affects one velocity @test measurement(x, u, p, 0.0) == [0, 0, 0, 0] @test measurement2(x, u, p, 0.0, d) == [0] d = [0, 0, 1] -@test sort(f_oop2(x, u, p, 0.0, d)) == [0, 0, 0, 0, 0] # Affects nothing +@test sort(f(x, u, p, 0.0, d)) == [0, 0, 0, 0, 0] # Affects nothing @test measurement(x, u, p, 0.0) == [0, 0, 0, 0] @test measurement2(x, u, p, 0.0, d) == [1] # We have now disturbed the output diff --git a/test/extensions/Project.toml b/test/extensions/Project.toml index 03e65b4978..9e58e972d0 100644 --- a/test/extensions/Project.toml +++ b/test/extensions/Project.toml @@ -9,6 +9,7 @@ LabelledArrays = "2ee39098-c373-598a-b85f-a56591580800" ModelingToolkit = "961ee093-0014-501f-94e3-6117800e7a78" Nemo = "2edaba10-b0f1-5616-af89-8c11ac63239a" NonlinearSolveHomotopyContinuation = "2ac3b008-d579-4536-8c91-a1a5998c2f8b" +OrdinaryDiffEqNonlinearSolve = "127b3ac7-2247-4354-8eb6-78cf4e7c58e8" OrdinaryDiffEqTsit5 = "b1df2697-797e-41e3-8120-5422d3b24e4a" SciMLSensitivity = "1ed8b502-d754-442c-8d5d-10ac956f44a1" SciMLStructures = "53ae85a6-f571-4167-b2af-e1d143709226" diff --git a/test/input_output_handling.jl b/test/input_output_handling.jl index 693a00b9ad..2c16c89320 100644 --- a/test/input_output_handling.jl +++ b/test/input_output_handling.jl @@ -173,7 +173,7 @@ end p = [rand()] x = [rand()] u = [rand()] - @test f[1](x, u, p, 1) ≈ -x + u + @test f(x, u, p, 1) ≈ -x + u # With disturbance inputs @variables x(t)=0 u(t)=0 [input = true] d(t)=0 @@ -191,7 +191,7 @@ end p = [rand()] x = [rand()] u = [rand()] - @test f[1](x, u, p, 1) ≈ -x + u + @test f(x, u, p, 1) ≈ -x + u ## With added d argument @variables x(t)=0 u(t)=0 [input = true] d(t)=0 @@ -210,7 +210,7 @@ end x = [rand()] u = [rand()] d = [rand()] - @test f[1](x, u, p, t, d) ≈ -x + u + [d[]^2] + @test f(x, u, p, t, d) ≈ -x + u + [d[]^2] end end @@ -273,7 +273,7 @@ x = ModelingToolkit.varmap_to_vars( merge(ModelingToolkit.defaults(model), Dict(D.(unknowns(model)) .=> 0.0)), dvs) u = [rand()] -out = f[1](x, u, p, 1) +out = f(x, u, p, 1) i = findfirst(isequal(u[1]), out) @test i isa Int @test iszero(out[[1:(i - 1); (i + 1):end]]) @@ -333,7 +333,7 @@ model_outputs = [model.inertia1.w, model.inertia2.w, model.inertia1.phi, model.i @named dmodel = Blocks.StateSpace([0.0], [1.0], [1.0], [0.0]) # An integrating disturbance @named dist = ModelingToolkit.DisturbanceModel(model.torque.tau.u, dmodel) -(f_oop, f_ip), outersys, dvs, p, io_sys = ModelingToolkit.add_input_disturbance(model, dist) +f, outersys, dvs, p, io_sys = ModelingToolkit.add_input_disturbance(model, dist) @unpack u, d = outersys matrices, ssys = linearize(outersys, [u, d], model_outputs) @@ -348,8 +348,8 @@ x0 = randn(5) x1 = copy(x0) + x_add # add disturbance state perturbation u = randn(1) pn = MTKParameters(io_sys, []) -xp0 = f_oop(x0, u, pn, 0) -xp1 = f_oop(x1, u, pn, 0) +xp0 = f(x0, u, pn, 0) +xp1 = f(x1, u, pn, 0) @test xp0 ≈ matrices.A * x0 + matrices.B * [u; 0] @test xp1 ≈ matrices.A * x1 + matrices.B * [u; 0] @@ -402,7 +402,7 @@ outs = collect(complete(model).output.u) disturbed_input = ins[1] @named dist_integ = DisturbanceModel(disturbed_input, integrator) -(f_oop, f_ip), augmented_sys, dvs, p = ModelingToolkit.add_input_disturbance(model, +f, augmented_sys, dvs, p = ModelingToolkit.add_input_disturbance(model, dist_integ, ins) @@ -447,7 +447,7 @@ end @named sys = ODESystem(eqs, t, [x], []) f, dvs, ps, io_sys = ModelingToolkit.generate_control_function(sys, simplify = true) - @test f[1]([0.5], nothing, MTKParameters(io_sys, []), 0.0) ≈ [1.0] + @test f([0.5], nothing, MTKParameters(io_sys, []), 0.0) ≈ [1.0] end @testset "With callable symbolic" begin @@ -459,5 +459,5 @@ end p = MTKParameters(io_sys, []) u = [1.0] x = [1.0] - @test_nowarn f[1](x, u, p, 0.0) + @test_nowarn f(x, u, p, 0.0) end diff --git a/test/odesystem.jl b/test/odesystem.jl index afb9e6e440..9219fca5fb 100644 --- a/test/odesystem.jl +++ b/test/odesystem.jl @@ -472,14 +472,12 @@ sys = complete(sys) # check inputs let - @parameters f k d - @variables x(t) ẋ(t) - δ = D + @parameters k d + @variables x(t) ẋ(t) f(t) [input = true] - eqs = [δ(x) ~ ẋ, δ(ẋ) ~ f - k * x - d * ẋ] - @named sys = ODESystem(eqs, t, [x, ẋ], [f, d, k]; controls = [f]) - - calculate_control_jacobian(sys) + eqs = [D(x) ~ ẋ, D(ẋ) ~ f - k * x - d * ẋ] + @named sys = ODESystem(eqs, t, [x, ẋ], [d, k]) + sys, _ = structural_simplify(sys, ([f], [])) @test isequal(calculate_control_jacobian(sys), reshape(Num[0, 1], 2, 1)) From f4d1760f4fbd302bb178d2ca99b948256c5a8a63 Mon Sep 17 00:00:00 2001 From: vyudu Date: Fri, 25 Apr 2025 23:07:30 -0400 Subject: [PATCH 1447/2176] format --- src/ModelingToolkit.jl | 2 +- src/systems/diffeqs/odesystem.jl | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/ModelingToolkit.jl b/src/ModelingToolkit.jl index a7d4f58fce..f5db712147 100644 --- a/src/ModelingToolkit.jl +++ b/src/ModelingToolkit.jl @@ -351,7 +351,7 @@ function FMIComponent end include("systems/optimal_control_interface.jl") export AbstractDynamicOptProblem, JuMPDynamicOptProblem, InfiniteOptDynamicOptProblem, PyomoDynamicOptProblem, CasADiDynamicOptProblem -export DynamicOptSolution +export DynamicOptSolution export ∫ end # module diff --git a/src/systems/diffeqs/odesystem.jl b/src/systems/diffeqs/odesystem.jl index 1e8293cca3..4a13d7ccf1 100644 --- a/src/systems/diffeqs/odesystem.jl +++ b/src/systems/diffeqs/odesystem.jl @@ -803,7 +803,8 @@ function validate_vars_and_find_ps!(auxvars, auxps, sysvars, iv) for var in auxvars if !iscall(var) - var ∈ sts || throw(ArgumentError("Time-independent variable $var is not an unknown of the system.")) + var ∈ sts || + throw(ArgumentError("Time-independent variable $var is not an unknown of the system.")) elseif length(arguments(var)) > 1 throw(ArgumentError("Too many arguments for variable $var.")) elseif length(arguments(var)) == 1 From 3c71a871279bfc12a846adb855d6315fee0232a2 Mon Sep 17 00:00:00 2001 From: vyudu Date: Mon, 28 Apr 2025 13:58:41 -0400 Subject: [PATCH 1448/2176] add JuMP to extensions Project.toml --- test/extensions/Project.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/test/extensions/Project.toml b/test/extensions/Project.toml index 9e58e972d0..f5cedf49fe 100644 --- a/test/extensions/Project.toml +++ b/test/extensions/Project.toml @@ -5,6 +5,7 @@ ChainRulesTestUtils = "cdddcdb0-9152-4a09-a978-84456f9df70a" ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210" HomotopyContinuation = "f213a82b-91d6-5c5d-acf7-10f1c761b327" InfiniteOpt = "20393b10-9daf-11e9-18c9-8db751c92c57" +JuMP = "4076af6c-e467-56ae-b986-b466b2749572" LabelledArrays = "2ee39098-c373-598a-b85f-a56591580800" ModelingToolkit = "961ee093-0014-501f-94e3-6117800e7a78" Nemo = "2edaba10-b0f1-5616-af89-8c11ac63239a" From 14905864b8b3fe50e43a97c5268d4c325747ba80 Mon Sep 17 00:00:00 2001 From: vyudu Date: Mon, 28 Apr 2025 14:28:52 -0400 Subject: [PATCH 1449/2176] fix tests --- test/extensions/test_infiniteopt.jl | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/test/extensions/test_infiniteopt.jl b/test/extensions/test_infiniteopt.jl index e45aa0f2fd..833b9f3275 100644 --- a/test/extensions/test_infiniteopt.jl +++ b/test/extensions/test_infiniteopt.jl @@ -22,13 +22,14 @@ using ModelingToolkit: D_nounits as D, t_nounits as t, varmap_to_vars end @named model = Pendulum() model = complete(model) - inputs = [model.τ] -(f_oop, f_ip), dvs, psym, io_sys = ModelingToolkit.generate_control_function( - model, inputs, split = false) - outputs = [model.y] -f_obs = ModelingToolkit.build_explicit_observed_function(io_sys, outputs; inputs = inputs) +model, _ = structural_simplify(model, (inputs, outputs)) + +f, dvs, psym, io_sys = ModelingToolkit.generate_control_function( + model, split = false) + +f_obs = ModelingToolkit.build_explicit_observed_function(io_sys, outputs; inputs) expected_state_order = [model.θ, model.ω] permutation = [findfirst(isequal(x), expected_state_order) for x in dvs] # This maps our expected state order to the actual state order @@ -64,7 +65,7 @@ InfiniteOpt.@variables(m, # Trace the dynamics x0, p = ModelingToolkit.get_u0_p(io_sys, [model.θ => 0, model.ω => 0], [model.L => L]) -xp = f_oop(x, u, p, τ) +xp = f(x, u, p, τ) cp = f_obs(x, u, p, τ) # Test that it's possible to trace through an observed function @objective(m, Min, tf) From 4690d5b694a639a5fcc860f51575e3eccab7fe0b Mon Sep 17 00:00:00 2001 From: vyudu Date: Mon, 28 Apr 2025 14:41:14 -0400 Subject: [PATCH 1450/2176] move jump control to downsteram --- test/downstream/Project.toml | 11 ++++++++++- test/downstream/analysis_points.jl | 2 +- test/downstream/inversemodel.jl | 2 +- .../jump_control.jl | 0 test/downstream/linearize.jl | 2 +- test/downstream/test_disturbance_model.jl | 2 +- test/dynamic_optimization/Project.toml | 14 -------------- 7 files changed, 14 insertions(+), 19 deletions(-) rename test/{dynamic_optimization => downstream}/jump_control.jl (100%) delete mode 100644 test/dynamic_optimization/Project.toml diff --git a/test/downstream/Project.toml b/test/downstream/Project.toml index aa58095744..f7f6885b84 100644 --- a/test/downstream/Project.toml +++ b/test/downstream/Project.toml @@ -1,10 +1,19 @@ [deps] ControlSystemsMTK = "687d7614-c7e5-45fc-bfc3-9ee385575c88" +DataInterpolations = "82cc6244-b520-54b8-b5a6-8a565e85f1d0" +DiffEqDevTools = "f3b72e0c-5b89-59e1-b016-84e28bfd966d" +InfiniteOpt = "20393b10-9daf-11e9-18c9-8db751c92c57" +Ipopt = "b6b21f68-93f8-5de0-b562-5493be1d77c9" LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" ModelingToolkit = "961ee093-0014-501f-94e3-6117800e7a78" ModelingToolkitStandardLibrary = "16a59e39-deab-5bd0-87e4-056b12336739" -OrdinaryDiffEq = "1dea7af3-3e70-54e6-95c3-0bf5283fa5ed" +OrdinaryDiffEqFIRK = "5960d6e9-dd7a-4743-88e7-cf307b64f125" OrdinaryDiffEqNonlinearSolve = "127b3ac7-2247-4354-8eb6-78cf4e7c58e8" +OrdinaryDiffEqRosenbrock = "43230ef6-c299-4910-a778-202eb28ce4ce" +OrdinaryDiffEqSDIRK = "2d112036-d095-4a1e-ab9a-08536f3ecdbf" +OrdinaryDiffEqTsit5 = "b1df2697-797e-41e3-8120-5422d3b24e4a" +OrdinaryDiffEqVerner = "79d7bb75-1356-48c1-b8c0-6832512096c2" +SimpleDiffEq = "05bca326-078c-5bf0-a5bf-ce7c7982d7fd" SymbolicIndexingInterface = "2efcf032-c050-4f8e-a9bb-153293bab1f5" [compat] diff --git a/test/downstream/analysis_points.jl b/test/downstream/analysis_points.jl index 29b9aad512..58c4a97a8d 100644 --- a/test/downstream/analysis_points.jl +++ b/test/downstream/analysis_points.jl @@ -1,4 +1,4 @@ -using ModelingToolkit, OrdinaryDiffEq, LinearAlgebra, ControlSystemsBase +using ModelingToolkit, OrdinaryDiffEqRosenbrock, LinearAlgebra, ControlSystemsBase using ModelingToolkitStandardLibrary.Mechanical.Rotational using ModelingToolkitStandardLibrary.Blocks using ModelingToolkit: connect, t_nounits as t, D_nounits as D diff --git a/test/downstream/inversemodel.jl b/test/downstream/inversemodel.jl index 32d5ee87ec..7bed0bc1e8 100644 --- a/test/downstream/inversemodel.jl +++ b/test/downstream/inversemodel.jl @@ -1,7 +1,7 @@ using ModelingToolkit using ModelingToolkitStandardLibrary using ModelingToolkitStandardLibrary.Blocks -using OrdinaryDiffEq +using OrdinaryDiffEqRosenbrock using SymbolicIndexingInterface using Test using ControlSystemsMTK: tf, ss, get_named_sensitivity, get_named_comp_sensitivity diff --git a/test/dynamic_optimization/jump_control.jl b/test/downstream/jump_control.jl similarity index 100% rename from test/dynamic_optimization/jump_control.jl rename to test/downstream/jump_control.jl diff --git a/test/downstream/linearize.jl b/test/downstream/linearize.jl index 16df29f834..d82bd696dd 100644 --- a/test/downstream/linearize.jl +++ b/test/downstream/linearize.jl @@ -206,7 +206,7 @@ lsys, ssys = linearize(sat, [u], [y]; op = Dict(u => 2)) @test lsys.D[] == 0 # Test case when unknowns in system do not have equations in initialization system -using ModelingToolkit, OrdinaryDiffEq, LinearAlgebra +using ModelingToolkit, LinearAlgebra using ModelingToolkitStandardLibrary.Mechanical.Rotational using ModelingToolkitStandardLibrary.Blocks: Add, Sine, PID, SecondOrder, Step, RealOutput using ModelingToolkit: connect diff --git a/test/downstream/test_disturbance_model.jl b/test/downstream/test_disturbance_model.jl index c96830efa3..cd9d769e99 100644 --- a/test/downstream/test_disturbance_model.jl +++ b/test/downstream/test_disturbance_model.jl @@ -3,7 +3,7 @@ This file implements and tests a typical workflow for state estimation with dist The primary subject of the tests is the analysis-point features and the analysis-point specific method for `generate_control_function`. =# -using ModelingToolkit, OrdinaryDiffEq, LinearAlgebra, Test +using ModelingToolkit, OrdinaryDiffEqTsit5, LinearAlgebra, Test using ModelingToolkitStandardLibrary.Mechanical.Rotational using ModelingToolkitStandardLibrary.Blocks using ModelingToolkit: connect diff --git a/test/dynamic_optimization/Project.toml b/test/dynamic_optimization/Project.toml deleted file mode 100644 index a5eaf3144b..0000000000 --- a/test/dynamic_optimization/Project.toml +++ /dev/null @@ -1,14 +0,0 @@ -[deps] -BenchmarkTools = "6e4b80f9-dd63-53aa-95a3-0cdb28fa8baf" -CairoMakie = "13f3f980-e62b-5c42-98c6-ff1f3baf88f0" -DataInterpolations = "82cc6244-b520-54b8-b5a6-8a565e85f1d0" -DiffEqDevTools = "f3b72e0c-5b89-59e1-b016-84e28bfd966d" -InfiniteOpt = "20393b10-9daf-11e9-18c9-8db751c92c57" -Ipopt = "b6b21f68-93f8-5de0-b562-5493be1d77c9" -ModelingToolkit = "961ee093-0014-501f-94e3-6117800e7a78" -ModelingToolkitStandardLibrary = "16a59e39-deab-5bd0-87e4-056b12336739" -OrdinaryDiffEqFIRK = "5960d6e9-dd7a-4743-88e7-cf307b64f125" -OrdinaryDiffEqSDIRK = "2d112036-d095-4a1e-ab9a-08536f3ecdbf" -OrdinaryDiffEqTsit5 = "b1df2697-797e-41e3-8120-5422d3b24e4a" -OrdinaryDiffEqVerner = "79d7bb75-1356-48c1-b8c0-6832512096c2" -SimpleDiffEq = "05bca326-078c-5bf0-a5bf-ce7c7982d7fd" From eb51290c803c0dce4d6fd970e6f72e5ec4777cb1 Mon Sep 17 00:00:00 2001 From: vyudu Date: Mon, 28 Apr 2025 15:20:43 -0400 Subject: [PATCH 1451/2176] fix more tests --- src/structural_transformation/utils.jl | 1 - test/downstream/Project.toml | 1 + test/downstream/jump_control.jl | 8 +++++--- test/runtests.jl | 12 +----------- 4 files changed, 7 insertions(+), 15 deletions(-) diff --git a/src/structural_transformation/utils.jl b/src/structural_transformation/utils.jl index 15a46531d9..14628f2958 100644 --- a/src/structural_transformation/utils.jl +++ b/src/structural_transformation/utils.jl @@ -96,7 +96,6 @@ function check_consistency(state::TransformationState, orig_inputs; nothrow = fa fullvars = get_fullvars(state) neqs = n_concrete_eqs(state) @unpack graph, var_to_diff = state.structure - @show equations(state.sys) highest_vars = computed_highest_diff_variables(complete!(state.structure)) n_highest_vars = 0 for (v, h) in enumerate(highest_vars) diff --git a/test/downstream/Project.toml b/test/downstream/Project.toml index f7f6885b84..1192dda074 100644 --- a/test/downstream/Project.toml +++ b/test/downstream/Project.toml @@ -4,6 +4,7 @@ DataInterpolations = "82cc6244-b520-54b8-b5a6-8a565e85f1d0" DiffEqDevTools = "f3b72e0c-5b89-59e1-b016-84e28bfd966d" InfiniteOpt = "20393b10-9daf-11e9-18c9-8db751c92c57" Ipopt = "b6b21f68-93f8-5de0-b562-5493be1d77c9" +JuMP = "4076af6c-e467-56ae-b986-b466b2749572" LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" ModelingToolkit = "961ee093-0014-501f-94e3-6117800e7a78" ModelingToolkitStandardLibrary = "16a59e39-deab-5bd0-87e4-056b12336739" diff --git a/test/downstream/jump_control.jl b/test/downstream/jump_control.jl index 431491ddcd..303b18bbeb 100644 --- a/test/downstream/jump_control.jl +++ b/test/downstream/jump_control.jl @@ -5,7 +5,7 @@ using SimpleDiffEq using OrdinaryDiffEqSDIRK, OrdinaryDiffEqVerner, OrdinaryDiffEqTsit5, OrdinaryDiffEqFIRK using Ipopt using DataInterpolations -#const M = ModelingToolkit +const M = ModelingToolkit @testset "ODE Solution, no cost" begin # Test solving without anything attached. @@ -108,7 +108,7 @@ end # Linear systems have bang-bang controls @test is_bangbang(jsol.input_sol, [-1.0], [1.0]) # Test reached final position. - @test ≈(jsol.sol.u[end][1], 0.25, rtol = 1e-5) + @test ≈(jsol.sol.u[end][2], 0.25, rtol = 1e-5) # Test dynamics @parameters (u_interp::ConstantInterpolation)(..) @mtkbuild block_ode = ODESystem([D(x(t)) ~ v(t), D(v(t)) ~ u_interp(t)], t) @@ -120,7 +120,7 @@ end iprob = InfiniteOptDynamicOptProblem(block, u0map, tspan, parammap; dt = 0.01) isol = solve(iprob, Ipopt.Optimizer; silent = true) @test is_bangbang(isol.input_sol, [-1.0], [1.0]) - @test ≈(isol.sol.u[end][1], 0.25, rtol = 1e-5) + @test ≈(isol.sol.u[end][2], 0.25, rtol = 1e-5) osol = solve(oprob, ImplicitEuler(); dt = 0.01, adaptive = false) @test ≈(isol.sol.u, osol.u, rtol = 0.05) @@ -233,6 +233,8 @@ end end @testset "Cart-pole problem" begin + t = M.t_nounits + D = M.D_nounits # gravity, length, moment of Inertia, drag coeff @parameters g l mₚ mₖ @variables x(..) θ(..) u(t) [input = true, bounds = (-10, 10)] diff --git a/test/runtests.jl b/test/runtests.jl index 5f27f8bb88..3031e855a3 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -22,12 +22,6 @@ function activate_downstream_env() Pkg.instantiate() end -function activate_dynamic_optimization_env() - Pkg.activate("dynamic_optimization") - Pkg.develop(PackageSpec(path = dirname(@__DIR__))) - Pkg.instantiate() -end - @time begin #if GROUP == "All" || GROUP == "InterfaceI" # @testset "InterfaceI" begin @@ -128,6 +122,7 @@ end if GROUP == "All" || GROUP == "Downstream" activate_downstream_env() + @safetestset "JuMP Collocation Solvers" include("downstream/jump_control.jl") @safetestset "Linearization Tests" include("downstream/linearize.jl") @safetestset "Linearization Dummy Derivative Tests" include("downstream/linearization_dd.jl") @safetestset "Inverse Models Test" include("downstream/inversemodel.jl") @@ -149,9 +144,4 @@ end @safetestset "InfiniteOpt Extension Test" include("extensions/test_infiniteopt.jl") @safetestset "JuMPControl Extension Test" include("extensions/jump_control.jl") end - - if GROUP == "All" || GROUP == "Dynamic Optimization" - activate_dynamic_optimization_env() - @safetestset "JuMP Collocation Solvers" include("dynamic_optimization/jump_control") - end end From ed2caafa764874c6cd749e9e407d228120669360 Mon Sep 17 00:00:00 2001 From: vyudu Date: Mon, 28 Apr 2025 16:13:22 -0400 Subject: [PATCH 1452/2176] rtest: emove from extensions --- test/runtests.jl | 1 - 1 file changed, 1 deletion(-) diff --git a/test/runtests.jl b/test/runtests.jl index 3031e855a3..679a36f26c 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -142,6 +142,5 @@ end @safetestset "LabelledArrays Test" include("labelledarrays.jl") @safetestset "BifurcationKit Extension Test" include("extensions/bifurcationkit.jl") @safetestset "InfiniteOpt Extension Test" include("extensions/test_infiniteopt.jl") - @safetestset "JuMPControl Extension Test" include("extensions/jump_control.jl") end end From 15f3e8a215a61e83715867a4bbe732e79098d1e4 Mon Sep 17 00:00:00 2001 From: vyudu Date: Tue, 29 Apr 2025 14:14:55 -0400 Subject: [PATCH 1453/2176] fix: make free final time problems with constraints work --- ext/MTKJuMPDynamicOptExt.jl | 49 +++++++++++++++++------- src/systems/optimal_control_interface.jl | 2 +- test/downstream/jump_control.jl | 38 +++++++++++++++--- 3 files changed, 68 insertions(+), 21 deletions(-) diff --git a/ext/MTKJuMPDynamicOptExt.jl b/ext/MTKJuMPDynamicOptExt.jl index 882456dab3..448963dc60 100644 --- a/ext/MTKJuMPDynamicOptExt.jl +++ b/ext/MTKJuMPDynamicOptExt.jl @@ -42,7 +42,7 @@ end Convert an ODESystem representing an optimal control system into a JuMP model for solving using optimization. Must provide either `dt`, the timestep between collocation points (which, along with the timespan, determines the number of points), or directly -provide the number of points as `nsteps`. +provide the number of points as `steps`. The optimization variables: - a vector-of-vectors U representing the unknowns as an interpolation array @@ -61,7 +61,7 @@ function MTK.JuMPDynamicOptProblem(sys::ODESystem, u0map, tspan, pmap; f, u0, p = MTK.process_SciMLProblem(ODEInputFunction, sys, _u0map, pmap; t = tspan !== nothing ? tspan[1] : tspan, kwargs...) - pmap = MTK.todict(pmap) + pmap = Dict{Any, Any}(pmap) steps, is_free_t = MTK.process_tspan(tspan, dt, steps) model = init_model(sys, tspan, steps, u0map, pmap, u0; is_free_t) @@ -87,7 +87,7 @@ function MTK.InfiniteOptDynamicOptProblem(sys::ODESystem, u0map, tspan, pmap; f, u0, p = MTK.process_SciMLProblem(ODEInputFunction, sys, _u0map, pmap; t = tspan !== nothing ? tspan[1] : tspan, kwargs...) - pmap = MTK.todict(pmap) + pmap = Dict{Any, Any}(pmap) steps, is_free_t = MTK.process_tspan(tspan, dt, steps) model = init_model(sys, tspan, steps, u0map, pmap, u0; is_free_t) @@ -103,14 +103,15 @@ function init_model(sys, tspan, steps, u0map, pmap, u0; is_free_t = false) if is_free_t (ts_sym, te_sym) = tspan + MTK.symbolic_type(ts_sym) !== MTK.NotSymbolic() && error("Free initial time problems are not currently supported.") @variable(model, tf, start=pmap[te_sym]) + set_lower_bound(tf, ts_sym) hasbounds(te_sym) && begin lo, hi = getbounds(te_sym) set_lower_bound(tf, lo) set_upper_bound(tf, hi) end - pmap[ts_sym] = 0 - pmap[te_sym] = 1 + pmap[te_sym] = model[:tf] tspan = (0, 1) end @@ -118,6 +119,9 @@ function init_model(sys, tspan, steps, u0map, pmap, u0; is_free_t = false) @variable(model, U[i = 1:length(states)], Infinite(t), start=u0[i]) c0 = MTK.value.([pmap[c] for c in ctrls]) @variable(model, V[i = 1:length(ctrls)], Infinite(t), start=c0[i]) + for (i, ct) in enumerate(ctrls) + pmap[ct] = model[:V][i] + end set_jump_bounds!(model, sys, pmap) add_jump_cost_function!(model, sys, (tspan[1], tspan[2]), pmap; is_free_t) @@ -131,6 +135,13 @@ function init_model(sys, tspan, steps, u0map, pmap, u0; is_free_t = false) return model end +""" +Modify the pmap by replacing controls with V[i](t), and tf with the model's final time variable for free final time problems. +""" +function modified_pmap(model, sys, pmap) + pmap = Dict{Any, Any}(pmap) +end + function set_jump_bounds!(model, sys, pmap) U = model[:U] for (i, u) in enumerate(unknowns(sys)) @@ -158,7 +169,7 @@ function add_jump_cost_function!(model::InfiniteModel, sys, tspan, pmap; is_free @objective(model, Min, 0) return end - jcosts = substitute_jump_vars(model, sys, pmap, jcosts) + jcosts = substitute_jump_vars(model, sys, pmap, jcosts; is_free_t) tₛ = is_free_t ? model[:tf] : 1 # Substitute integral @@ -186,13 +197,14 @@ function add_user_constraints!(model::InfiniteModel, sys, pmap; is_free_t = fals for u in MTK.get_unknowns(conssys) x = MTK.operation(u) t = only(arguments(u)) - MTK.symbolic_type(t) === NotSymbolic() && - error("Provided specific time constraint in a free final time problem. This is not supported by the JuMP/InfiniteOpt collocation solvers. The offending variable is $u.") + if (MTK.symbolic_type(t) === MTK.NotSymbolic()) + error("Provided specific time constraint in a free final time problem. This is not supported by the JuMP/InfiniteOpt collocation solvers. The offending variable is $u. Specific-time constraints can only be specified at the beginning or end of the timespan.") + end end end auxmap = Dict([u => MTK.default_toterm(MTK.value(u)) for u in unknowns(conssys)]) - jconstraints = substitute_jump_vars(model, sys, pmap, jconstraints; auxmap) + jconstraints = substitute_jump_vars(model, sys, pmap, jconstraints; auxmap, is_free_t) # Substitute to-term'd variables for (i, cons) in enumerate(jconstraints) @@ -211,13 +223,22 @@ function add_initial_constraints!(model::InfiniteModel, u0, u0_idxs, ts) @constraint(model, initial[i in u0_idxs], U[i](ts)==u0[i]) end -function substitute_jump_vars(model, sys, pmap, exprs; auxmap = Dict()) +function substitute_jump_vars(model, sys, pmap, exprs; auxmap = Dict(), is_free_t = false) iv = MTK.get_iv(sys) sts = unknowns(sys) cts = MTK.unbound_inputs(sys) U = model[:U] V = model[:V] + x_ops = [MTK.operation(MTK.unwrap(st)) for st in sts] + c_ops = [MTK.operation(MTK.unwrap(ct)) for ct in cts] + exprs = map(c -> Symbolics.fixpoint_sub(c, auxmap), exprs) + exprs = map(c -> Symbolics.fixpoint_sub(c, Dict(pmap)), exprs) + if is_free_t + tf = model[:tf] + free_t_map = Dict([[x(tf) => U[i](1) for (i, x) in enumerate(x_ops)]; [c(tf) => V[i](1) for (i, c) in enumerate(c_ops)]]) + exprs = map(c -> Symbolics.fixpoint_sub(c, free_t_map), exprs) + end # for variables like x(t) whole_interval_map = Dict([[v => U[i] for (i, v) in enumerate(sts)]; @@ -225,14 +246,10 @@ function substitute_jump_vars(model, sys, pmap, exprs; auxmap = Dict()) exprs = map(c -> Symbolics.fixpoint_sub(c, whole_interval_map), exprs) # for variables like x(1.0) - x_ops = [MTK.operation(MTK.unwrap(st)) for st in sts] - c_ops = [MTK.operation(MTK.unwrap(ct)) for ct in cts] fixed_t_map = Dict([[x_ops[i] => U[i] for i in 1:length(U)]; [c_ops[i] => V[i] for i in 1:length(V)]]) exprs = map(c -> Symbolics.fixpoint_sub(c, fixed_t_map), exprs) - - exprs = map(c -> Symbolics.fixpoint_sub(c, Dict(pmap)), exprs) exprs end @@ -246,8 +263,12 @@ function add_infopt_solve_constraints!(model::InfiniteModel, sys, pmap; is_free_ diffsubmap = Dict([D(U[i]) => ∂(U[i], t) for i in 1:length(U)]) tₛ = is_free_t ? model[:tf] : 1 + @show diff_equations(sys) + @show pmap diff_eqs = substitute_jump_vars(model, sys, pmap, diff_equations(sys)) + @show diff_eqs diff_eqs = map(e -> Symbolics.substitute(e, diffsubmap), diff_eqs) + @show diff_eqs @constraint(model, D[i = 1:length(diff_eqs)], diff_eqs[i].lhs==tₛ * diff_eqs[i].rhs) # Algebraic equations diff --git a/src/systems/optimal_control_interface.jl b/src/systems/optimal_control_interface.jl index 1183d04db6..be303192e4 100644 --- a/src/systems/optimal_control_interface.jl +++ b/src/systems/optimal_control_interface.jl @@ -173,7 +173,7 @@ function process_tspan(tspan, dt, steps) elseif symbolic_type(tspan[1]) === ScalarSymbolic() || symbolic_type(tspan[2]) === ScalarSymbolic() isnothing(steps) && - error("Free final time problems require specifying the number of steps, rather than dt.") + error("Free final time problems require specifying the number of steps using the keyword arg `steps`, rather than dt.") isnothing(dt) || @warn "Specified dt for free final time problem. This will be ignored; dt will be determined by the number of timesteps." diff --git a/test/downstream/jump_control.jl b/test/downstream/jump_control.jl index 303b18bbeb..be36f8bbbc 100644 --- a/test/downstream/jump_control.jl +++ b/test/downstream/jump_control.jl @@ -178,7 +178,7 @@ end (ts, te) = (0.0, 0.2) costs = [-h(te)] - cons = [T(te) ~ 0] + cons = [T(te) ~ 0, m(te) ~ m_c] @named rocket = ODESystem(eqs, t; costs, constraints = cons) rocket, input_idxs = structural_simplify(rocket, ([T(t)], [])) @@ -186,14 +186,14 @@ end pmap = [ g₀ => 1, m₀ => 1.0, h_c => 500, c => 0.5 * √(g₀ * h₀), D_c => 0.5 * 620 * m₀ / g₀, Tₘ => 3.5 * g₀ * m₀, T(t) => 0.0, h₀ => 1, m_c => 0.6] - jprob = JuMPDynamicOptProblem(rocket, u0map, (ts, te), pmap; dt = 0.005, cse = false) + jprob = JuMPDynamicOptProblem(rocket, u0map, (ts, te), pmap; dt = 0.001, cse = false) jsol = solve(jprob, Ipopt.Optimizer, :RadauIIA5, silent = true) @test jsol.sol.u[end][1] > 1.012 iprob = InfiniteOptDynamicOptProblem( - rocket, u0map, (ts, te), pmap; dt = 0.005, cse = false) + rocket, u0map, (ts, te), pmap; dt = 0.001, cse = false) isol = solve(iprob, Ipopt.Optimizer, - derivative_method = InfiniteOpt.OrthogonalCollocation(3), silent = true) + derivative_method = InfiniteOpt.OrthogonalCollocation(3), silent = true) @test isol.sol.u[end][1] > 1.012 # Test solution @@ -204,11 +204,16 @@ end @mtkbuild rocket_ode = ODESystem(eqs, t) interpmap = Dict(T_interp => ctrl_to_spline(jsol.input_sol, CubicSpline)) oprob = ODEProblem(rocket_ode, u0map, (ts, te), merge(Dict(pmap), interpmap)) - osol = solve(oprob, RadauIIA5(); adaptive = false, dt = 0.005) + osol = solve(oprob, RadauIIA5(); adaptive = false, dt = 0.001) @test ≈(jsol.sol.u, osol.u, rtol = 0.02) + + interpmap1 = Dict(T_interp => ctrl_to_spline(isol.input_sol, CubicSpline)) + oprob1 = ODEProblem(rocket_ode, u0map, (ts, te), merge(Dict(pmap), interpmap1)) + osol1 = solve(oprob1, RadauIIA5(); adaptive = false, dt = 0.001) + @test ≈(isol.sol.u, osol1.u, rtol = 0.02) end -@testset "Free final time problem" begin +@testset "Free final time problems" begin t = M.t_nounits D = M.D_nounits @@ -230,6 +235,27 @@ end iprob = InfiniteOptDynamicOptProblem(rocket, u0map, (0, tf), pmap; steps = 200) isol = solve(iprob, Ipopt.Optimizer) @test isapprox(isol.sol.t[end], 10.0, rtol = 1e-3) + + @variables x(..) v(..) + @variables u(..) [bounds = (-1.0, 1.0), input = true] + @parameters tf + constr = [v(tf) ~ 0, x(tf) ~ 0] + cost = [tf] # Minimize time + + @named block = ODESystem( + [D(x(t)) ~ v(t), D(v(t)) ~ u(t)], t; costs = cost, constraints = constr) + block, input_idxs = structural_simplify(block, ([u(t)], [])) + + u0map = [x(t) => 1.0, v(t) => 0.0] + tspan = (0.0, tf) + parammap = [u(t) => 0.0, tf => 1.0] + jprob = JuMPDynamicOptProblem(block, u0map, tspan, parammap; steps = 51) + jsol = solve(jprob, Ipopt.Optimizer, :Verner8, silent = true) + @test isapprox(jsol.sol.t[end], 2.0, atol = 1e-5) + + iprob = InfiniteOptDynamicOptProblem(block, u0map, tspan, parammap; steps = 51) + isol = solve(iprob, Ipopt.Optimizer, silent = true) + @test isapprox(isol.sol.t[end], 2.0, atol = 1e-5) end @testset "Cart-pole problem" begin From ade94d92c969eb9b92cbf40e07015ad90d5f4f38 Mon Sep 17 00:00:00 2001 From: vyudu Date: Tue, 29 Apr 2025 14:17:34 -0400 Subject: [PATCH 1454/2176] format: format --- ext/MTKJuMPDynamicOptExt.jl | 6 ++++-- test/downstream/jump_control.jl | 4 ++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/ext/MTKJuMPDynamicOptExt.jl b/ext/MTKJuMPDynamicOptExt.jl index 448963dc60..0e86064ff4 100644 --- a/ext/MTKJuMPDynamicOptExt.jl +++ b/ext/MTKJuMPDynamicOptExt.jl @@ -103,7 +103,8 @@ function init_model(sys, tspan, steps, u0map, pmap, u0; is_free_t = false) if is_free_t (ts_sym, te_sym) = tspan - MTK.symbolic_type(ts_sym) !== MTK.NotSymbolic() && error("Free initial time problems are not currently supported.") + MTK.symbolic_type(ts_sym) !== MTK.NotSymbolic() && + error("Free initial time problems are not currently supported.") @variable(model, tf, start=pmap[te_sym]) set_lower_bound(tf, ts_sym) hasbounds(te_sym) && begin @@ -236,7 +237,8 @@ function substitute_jump_vars(model, sys, pmap, exprs; auxmap = Dict(), is_free_ exprs = map(c -> Symbolics.fixpoint_sub(c, Dict(pmap)), exprs) if is_free_t tf = model[:tf] - free_t_map = Dict([[x(tf) => U[i](1) for (i, x) in enumerate(x_ops)]; [c(tf) => V[i](1) for (i, c) in enumerate(c_ops)]]) + free_t_map = Dict([[x(tf) => U[i](1) for (i, x) in enumerate(x_ops)]; + [c(tf) => V[i](1) for (i, c) in enumerate(c_ops)]]) exprs = map(c -> Symbolics.fixpoint_sub(c, free_t_map), exprs) end diff --git a/test/downstream/jump_control.jl b/test/downstream/jump_control.jl index be36f8bbbc..ca339a60eb 100644 --- a/test/downstream/jump_control.jl +++ b/test/downstream/jump_control.jl @@ -193,7 +193,7 @@ end iprob = InfiniteOptDynamicOptProblem( rocket, u0map, (ts, te), pmap; dt = 0.001, cse = false) isol = solve(iprob, Ipopt.Optimizer, - derivative_method = InfiniteOpt.OrthogonalCollocation(3), silent = true) + derivative_method = InfiniteOpt.OrthogonalCollocation(3), silent = true) @test isol.sol.u[end][1] > 1.012 # Test solution @@ -245,7 +245,7 @@ end @named block = ODESystem( [D(x(t)) ~ v(t), D(v(t)) ~ u(t)], t; costs = cost, constraints = constr) block, input_idxs = structural_simplify(block, ([u(t)], [])) - + u0map = [x(t) => 1.0, v(t) => 0.0] tspan = (0.0, tf) parammap = [u(t) => 0.0, tf => 1.0] From 16d29861e318c4ccf859680502075db93d7c7af9 Mon Sep 17 00:00:00 2001 From: vyudu Date: Wed, 30 Apr 2025 09:45:32 -0400 Subject: [PATCH 1455/2176] fix: fix rocket launch test --- ext/MTKJuMPDynamicOptExt.jl | 4 ---- test/downstream/jump_control.jl | 23 +++++++++++------------ 2 files changed, 11 insertions(+), 16 deletions(-) diff --git a/ext/MTKJuMPDynamicOptExt.jl b/ext/MTKJuMPDynamicOptExt.jl index 0e86064ff4..fad10b5048 100644 --- a/ext/MTKJuMPDynamicOptExt.jl +++ b/ext/MTKJuMPDynamicOptExt.jl @@ -265,12 +265,8 @@ function add_infopt_solve_constraints!(model::InfiniteModel, sys, pmap; is_free_ diffsubmap = Dict([D(U[i]) => ∂(U[i], t) for i in 1:length(U)]) tₛ = is_free_t ? model[:tf] : 1 - @show diff_equations(sys) - @show pmap diff_eqs = substitute_jump_vars(model, sys, pmap, diff_equations(sys)) - @show diff_eqs diff_eqs = map(e -> Symbolics.substitute(e, diffsubmap), diff_eqs) - @show diff_eqs @constraint(model, D[i = 1:length(diff_eqs)], diff_eqs[i].lhs==tₛ * diff_eqs[i].rhs) # Algebraic equations diff --git a/test/downstream/jump_control.jl b/test/downstream/jump_control.jl index ca339a60eb..4b454914c9 100644 --- a/test/downstream/jump_control.jl +++ b/test/downstream/jump_control.jl @@ -25,7 +25,7 @@ const M = ModelingToolkit # Test explicit method. jprob = JuMPDynamicOptProblem(sys, u0map, tspan, parammap, dt = 0.01) @test JuMP.num_constraints(jprob.model) == 2 # initials - jsol = solve(jprob, Ipopt.Optimizer, :RK4) + jsol = solve(jprob, Ipopt.Optimizer, :RK4, silent = true) oprob = ODEProblem(sys, u0map, tspan, parammap) osol = solve(oprob, SimpleRK4(), dt = 0.01) @test jsol.sol.u ≈ osol.u @@ -104,7 +104,7 @@ end tspan = (0.0, 1.0) parammap = [u(t) => 0.0] jprob = JuMPDynamicOptProblem(block, u0map, tspan, parammap; dt = 0.01) - jsol = solve(jprob, Ipopt.Optimizer, :Verner8) + jsol = solve(jprob, Ipopt.Optimizer, :Verner8, silent = true) # Linear systems have bang-bang controls @test is_bangbang(jsol.input_sol, [-1.0], [1.0]) # Test reached final position. @@ -142,7 +142,7 @@ end pmap = [b => 1, c => 1, μ => 1, s => 1, ν => 1, α => 1] jprob = JuMPDynamicOptProblem(beesys, u0map, tspan, pmap, dt = 0.01) - jsol = solve(jprob, Ipopt.Optimizer, :Tsitouras5) + jsol = solve(jprob, Ipopt.Optimizer, :Tsitouras5, silent = true) @test is_bangbang(jsol.input_sol, [0.0], [1.0]) iprob = InfiniteOptDynamicOptProblem(beesys, u0map, tspan, pmap, dt = 0.01) isol = solve(iprob, Ipopt.Optimizer; silent = true) @@ -191,9 +191,8 @@ end @test jsol.sol.u[end][1] > 1.012 iprob = InfiniteOptDynamicOptProblem( - rocket, u0map, (ts, te), pmap; dt = 0.001, cse = false) - isol = solve(iprob, Ipopt.Optimizer, - derivative_method = InfiniteOpt.OrthogonalCollocation(3), silent = true) + rocket, u0map, (ts, te), pmap; dt = 0.001) + isol = solve(iprob, Ipopt.Optimizer, silent = true) @test isol.sol.u[end][1] > 1.012 # Test solution @@ -209,8 +208,8 @@ end interpmap1 = Dict(T_interp => ctrl_to_spline(isol.input_sol, CubicSpline)) oprob1 = ODEProblem(rocket_ode, u0map, (ts, te), merge(Dict(pmap), interpmap1)) - osol1 = solve(oprob1, RadauIIA5(); adaptive = false, dt = 0.001) - @test ≈(isol.sol.u, osol1.u, rtol = 0.02) + osol1 = solve(oprob1, ImplicitEuler(); adaptive = false, dt = 0.001) + @test ≈(isol.sol.u, osol1.u, rtol = 0.01) end @testset "Free final time problems" begin @@ -229,11 +228,11 @@ end u0map = [x(t) => 17.5] pmap = [u(t) => 0.0, tf => 8] jprob = JuMPDynamicOptProblem(rocket, u0map, (0, tf), pmap; steps = 201) - jsol = solve(jprob, Ipopt.Optimizer, :Tsitouras5) + jsol = solve(jprob, Ipopt.Optimizer, :Tsitouras5, silent = true) @test isapprox(jsol.sol.t[end], 10.0, rtol = 1e-3) iprob = InfiniteOptDynamicOptProblem(rocket, u0map, (0, tf), pmap; steps = 200) - isol = solve(iprob, Ipopt.Optimizer) + isol = solve(iprob, Ipopt.Optimizer, silent = true) @test isapprox(isol.sol.t[end], 10.0, rtol = 1e-3) @variables x(..) v(..) @@ -288,10 +287,10 @@ end u0map = [D(x(t)) => 0.0, D(θ(t)) => 0.0, θ(t) => 0.0, x(t) => 0.0] pmap = [mₖ => 1.0, mₚ => 0.2, l => 0.5, g => 9.81, u => 0] jprob = JuMPDynamicOptProblem(cartpole, u0map, tspan, pmap; dt = 0.04) - jsol = solve(jprob, Ipopt.Optimizer, :RK4) + jsol = solve(jprob, Ipopt.Optimizer, :RK4, silent = true) @test jsol.sol.u[end] ≈ [π, 0, 0, 0] iprob = InfiniteOptDynamicOptProblem(cartpole, u0map, tspan, pmap; dt = 0.04) - isol = solve(iprob, Ipopt.Optimizer) + isol = solve(iprob, Ipopt.Optimizer, silent = true) @test isol.sol.u[end] ≈ [π, 0, 0, 0] end From e5a2707aa550f15fa6d0743b0e56299895368886 Mon Sep 17 00:00:00 2001 From: vyudu Date: Fri, 2 May 2025 23:32:06 -0400 Subject: [PATCH 1456/2176] feat: add default ode solver' --- Project.toml | 3 +- ext/MTKJuMPDynamicOptExt.jl | 22 ++- src/ModelingToolkit.jl | 1 - src/systems/optimal_control_interface.jl | 25 ---- test/downstream/jump_control.jl | 2 +- test/runtests.jl | 182 +++++++++++------------ 6 files changed, 112 insertions(+), 123 deletions(-) diff --git a/Project.toml b/Project.toml index 09c0afaa92..6ee2226b3b 100644 --- a/Project.toml +++ b/Project.toml @@ -66,7 +66,6 @@ Unitful = "1986cc42-f94f-5a68-af5c-568840ba703d" BifurcationKit = "0f109fa4-8a5d-4b75-95aa-f515264e7665" ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4" DeepDiffs = "ab62b9b5-e342-54a8-a765-a90f495de1a6" -DiffEqDevTools = "f3b72e0c-5b89-59e1-b016-84e28bfd966d" FMI = "14a09403-18e3-468f-ad8a-74f8dda2d9ac" InfiniteOpt = "20393b10-9daf-11e9-18c9-8db751c92c57" JuMP = "4076af6c-e467-56ae-b986-b466b2749572" @@ -78,7 +77,7 @@ MTKChainRulesCoreExt = "ChainRulesCore" MTKDeepDiffsExt = "DeepDiffs" MTKFMIExt = "FMI" MTKInfiniteOptExt = "InfiniteOpt" -MTKJuMPDynamicOptExt = ["JuMP", "DiffEqDevTools", "InfiniteOpt"] +MTKJuMPDynamicOptExt = ["JuMP", "InfiniteOpt"] MTKLabelledArraysExt = "LabelledArrays" [compat] diff --git a/ext/MTKJuMPDynamicOptExt.jl b/ext/MTKJuMPDynamicOptExt.jl index fad10b5048..0dd1b5c564 100644 --- a/ext/MTKJuMPDynamicOptExt.jl +++ b/ext/MTKJuMPDynamicOptExt.jl @@ -175,14 +175,13 @@ function add_jump_cost_function!(model::InfiniteModel, sys, tspan, pmap; is_free # Substitute integral iv = MTK.get_iv(sys) - jcosts = map( - c -> Symbolics.substitute(c, MTK.∫() => Symbolics.Integral(iv in tspan)), jcosts) intmap = Dict() for int in MTK.collect_applied_operators(jcosts, Symbolics.Integral) op = MTK.operation(int) arg = only(arguments(MTK.value(int))) lo, hi = (op.domain.domain.left, op.domain.domain.right) + hi = haskey(pmap, hi) ? 1 : hi intmap[int] = tₛ * InfiniteOpt.∫(arg, model[:t], lo, hi) end jcosts = map(c -> Symbolics.substitute(c, intmap), jcosts) @@ -325,6 +324,23 @@ function add_jump_solve_constraints!(prob, tableau; is_free_t = false) end end +""" +Default ODE Tableau: RadauIIA5 +""" +function constructDefault(T::Type = Float64) + sq6 = sqrt(6) + A = [11 // 45-7sq6 / 360 37 // 225-169sq6 / 1800 -2 // 225+sq6 / 75 + 37 // 225+169sq6 / 1800 11 // 45+7sq6 / 360 -2 // 225-sq6 / 75 + 4 // 9-sq6 / 36 4 // 9+sq6 / 36 1//9] + c = [2 // 5 - sq6 / 10; 2 / 5 + sq6 / 10; 1] + α = [4 // 9 - sq6 / 36; 4 // 9 + sq6 / 36; 1 // 9] + A = map(T, A) + α = map(T, α) + c = map(T, c) + + (; A = A, α = α, c = c) +end + """ Solve JuMPDynamicOptProblem. Arguments: - prob: a JumpDynamicOptProblem @@ -335,7 +351,7 @@ Solve JuMPDynamicOptProblem. Arguments: Returns a DynamicOptSolution, which contains both the model and the ODE solution. """ function DiffEqBase.solve( - prob::JuMPDynamicOptProblem, jump_solver, ode_solver::Symbol; silent = false) + prob::JuMPDynamicOptProblem, jump_solver, ode_solver::Symbol = :Default; silent = false) model = prob.model tableau_getter = Symbol(:construct, ode_solver) @eval tableau = $tableau_getter() diff --git a/src/ModelingToolkit.jl b/src/ModelingToolkit.jl index f5db712147..694265bbc7 100644 --- a/src/ModelingToolkit.jl +++ b/src/ModelingToolkit.jl @@ -352,6 +352,5 @@ include("systems/optimal_control_interface.jl") export AbstractDynamicOptProblem, JuMPDynamicOptProblem, InfiniteOptDynamicOptProblem, PyomoDynamicOptProblem, CasADiDynamicOptProblem export DynamicOptSolution -export ∫ end # module diff --git a/src/systems/optimal_control_interface.jl b/src/systems/optimal_control_interface.jl index be303192e4..a0bd88befd 100644 --- a/src/systems/optimal_control_interface.jl +++ b/src/systems/optimal_control_interface.jl @@ -18,8 +18,6 @@ end function JuMPDynamicOptProblem end function InfiniteOptDynamicOptProblem end -function CasADiDynamicOptProblem end -function PyomoDynamicOptProblem end function warn_overdetermined(sys, u0map) constraintsys = get_constraintsystem(sys) @@ -142,29 +140,6 @@ function SciMLBase.ODEInputFunction{false}(sys::AbstractODESystem, args...; ODEInputFunction{false, SciMLBase.FullSpecialize}(sys, args...; kwargs...) end -""" -Integral operator. When applied to an expression in a cost -function, assumes that the integration variable is the -iv of the system, and assumes that the bounds are the -tspan. -Equivalent to Integral(t in tspan) in Symbolics. -""" -struct ∫ <: Symbolics.Operator end -∫(x) = ∫()(x) -Base.show(io::IO, x::∫) = print(io, "∫") -Base.nameof(::∫) = :∫ - -function (I::∫)(x) - Term{symtype(x)}(I, Any[x]) -end - -function (I::∫)(x::Num) - v = value(x) - Num(I(v)) -end - -SymbolicUtils.promote_symtype(::Int, t) = t - # returns the JuMP timespan, the number of steps, and whether it is a free time problem. function process_tspan(tspan, dt, steps) is_free_time = false diff --git a/test/downstream/jump_control.jl b/test/downstream/jump_control.jl index 4b454914c9..b93cfe3bbb 100644 --- a/test/downstream/jump_control.jl +++ b/test/downstream/jump_control.jl @@ -220,7 +220,7 @@ end @parameters tf eqs = [D(x(t)) ~ -2 + 0.5 * u(t)] # Integral cost function - costs = [-∫(x(t) - u(t)), -x(tf)] + costs = [-Symbolics.Integral(t in (0, tf))(x(t) - u(t)), -x(tf)] consolidate(u) = u[1] + u[2] @named rocket = ODESystem(eqs, t; costs, consolidate) rocket, input_idxs = structural_simplify(rocket, ([u(t)], [])) diff --git a/test/runtests.jl b/test/runtests.jl index 679a36f26c..a09aca2fc7 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -23,102 +23,102 @@ function activate_downstream_env() end @time begin - #if GROUP == "All" || GROUP == "InterfaceI" - # @testset "InterfaceI" begin - # @safetestset "Linear Algebra Test" include("linalg.jl") - # @safetestset "AbstractSystem Test" include("abstractsystem.jl") - # @safetestset "Variable Scope Tests" include("variable_scope.jl") - # @safetestset "Symbolic Parameters Test" include("symbolic_parameters.jl") - # @safetestset "Parsing Test" include("variable_parsing.jl") - # @safetestset "Simplify Test" include("simplify.jl") - # @safetestset "Direct Usage Test" include("direct.jl") - # @safetestset "System Linearity Test" include("linearity.jl") - # @safetestset "Input Output Test" include("input_output_handling.jl") - # @safetestset "Clock Test" include("clock.jl") - # @safetestset "ODESystem Test" include("odesystem.jl") - # @safetestset "Dynamic Quantities Test" include("dq_units.jl") - # @safetestset "Unitful Quantities Test" include("units.jl") - # @safetestset "Mass Matrix Test" include("mass_matrix.jl") - # @safetestset "Reduction Test" include("reduction.jl") - # @safetestset "Split Parameters Test" include("split_parameters.jl") - # @safetestset "StaticArrays Test" include("static_arrays.jl") - # @safetestset "Components Test" include("components.jl") - # @safetestset "Model Parsing Test" include("model_parsing.jl") - # @safetestset "Error Handling" include("error_handling.jl") - # @safetestset "StructuralTransformations" include("structural_transformation/runtests.jl") - # @safetestset "Basic transformations" include("basic_transformations.jl") - # @safetestset "State Selection Test" include("state_selection.jl") - # @safetestset "Symbolic Event Test" include("symbolic_events.jl") - # @safetestset "Stream Connect Test" include("stream_connectors.jl") - # @safetestset "Domain Connect Test" include("domain_connectors.jl") - # @safetestset "Lowering Integration Test" include("lowering_solving.jl") - # @safetestset "Dependency Graph Test" include("dep_graphs.jl") - # @safetestset "Function Registration Test" include("function_registration.jl") - # @safetestset "Precompiled Modules Test" include("precompile_test.jl") - # @safetestset "DAE Jacobians Test" include("dae_jacobian.jl") - # @safetestset "Jacobian Sparsity" include("jacobiansparsity.jl") - # @safetestset "Modelingtoolkitize Test" include("modelingtoolkitize.jl") - # @safetestset "FuncAffect Test" include("funcaffect.jl") - # @safetestset "Constants Test" include("constants.jl") - # @safetestset "Parameter Dependency Test" include("parameter_dependencies.jl") - # @safetestset "Equation Type Accessors Test" include("equation_type_accessors.jl") - # @safetestset "System Accessor Functions Test" include("accessor_functions.jl") - # @safetestset "Equations with complex values" include("complex.jl") - # end - #end + if GROUP == "All" || GROUP == "InterfaceI" + @testset "InterfaceI" begin + @safetestset "Linear Algebra Test" include("linalg.jl") + @safetestset "AbstractSystem Test" include("abstractsystem.jl") + @safetestset "Variable Scope Tests" include("variable_scope.jl") + @safetestset "Symbolic Parameters Test" include("symbolic_parameters.jl") + @safetestset "Parsing Test" include("variable_parsing.jl") + @safetestset "Simplify Test" include("simplify.jl") + @safetestset "Direct Usage Test" include("direct.jl") + @safetestset "System Linearity Test" include("linearity.jl") + @safetestset "Input Output Test" include("input_output_handling.jl") + @safetestset "Clock Test" include("clock.jl") + @safetestset "ODESystem Test" include("odesystem.jl") + @safetestset "Dynamic Quantities Test" include("dq_units.jl") + @safetestset "Unitful Quantities Test" include("units.jl") + @safetestset "Mass Matrix Test" include("mass_matrix.jl") + @safetestset "Reduction Test" include("reduction.jl") + @safetestset "Split Parameters Test" include("split_parameters.jl") + @safetestset "StaticArrays Test" include("static_arrays.jl") + @safetestset "Components Test" include("components.jl") + @safetestset "Model Parsing Test" include("model_parsing.jl") + @safetestset "Error Handling" include("error_handling.jl") + @safetestset "StructuralTransformations" include("structural_transformation/runtests.jl") + @safetestset "Basic transformations" include("basic_transformations.jl") + @safetestset "State Selection Test" include("state_selection.jl") + @safetestset "Symbolic Event Test" include("symbolic_events.jl") + @safetestset "Stream Connect Test" include("stream_connectors.jl") + @safetestset "Domain Connect Test" include("domain_connectors.jl") + @safetestset "Lowering Integration Test" include("lowering_solving.jl") + @safetestset "Dependency Graph Test" include("dep_graphs.jl") + @safetestset "Function Registration Test" include("function_registration.jl") + @safetestset "Precompiled Modules Test" include("precompile_test.jl") + @safetestset "DAE Jacobians Test" include("dae_jacobian.jl") + @safetestset "Jacobian Sparsity" include("jacobiansparsity.jl") + @safetestset "Modelingtoolkitize Test" include("modelingtoolkitize.jl") + @safetestset "FuncAffect Test" include("funcaffect.jl") + @safetestset "Constants Test" include("constants.jl") + @safetestset "Parameter Dependency Test" include("parameter_dependencies.jl") + @safetestset "Equation Type Accessors Test" include("equation_type_accessors.jl") + @safetestset "System Accessor Functions Test" include("accessor_functions.jl") + @safetestset "Equations with complex values" include("complex.jl") + end + end - #if GROUP == "All" || GROUP == "Initialization" - # @safetestset "Guess Propagation" include("guess_propagation.jl") - # @safetestset "Hierarchical Initialization Equations" include("hierarchical_initialization_eqs.jl") - # @safetestset "InitializationSystem Test" include("initializationsystem.jl") - # @safetestset "Initial Values Test" include("initial_values.jl") - #end + if GROUP == "All" || GROUP == "Initialization" + @safetestset "Guess Propagation" include("guess_propagation.jl") + @safetestset "Hierarchical Initialization Equations" include("hierarchical_initialization_eqs.jl") + @safetestset "InitializationSystem Test" include("initializationsystem.jl") + @safetestset "Initial Values Test" include("initial_values.jl") + end - #if GROUP == "All" || GROUP == "InterfaceII" - # @testset "InterfaceII" begin - # @safetestset "Code Generation Test" include("code_generation.jl") - # @safetestset "IndexCache Test" include("index_cache.jl") - # @safetestset "Variable Utils Test" include("variable_utils.jl") - # @safetestset "Variable Metadata Test" include("test_variable_metadata.jl") - # @safetestset "OptimizationSystem Test" include("optimizationsystem.jl") - # @safetestset "Discrete System" include("discrete_system.jl") - # @safetestset "Implicit Discrete System" include("implicit_discrete_system.jl") - # @safetestset "SteadyStateSystem Test" include("steadystatesystems.jl") - # @safetestset "SDESystem Test" include("sdesystem.jl") - # @safetestset "DDESystem Test" include("dde.jl") - # @safetestset "NonlinearSystem Test" include("nonlinearsystem.jl") - # @safetestset "SCCNonlinearProblem Test" include("scc_nonlinear_problem.jl") - # @safetestset "PDE Construction Test" include("pdesystem.jl") - # @safetestset "JumpSystem Test" include("jumpsystem.jl") - # @safetestset "Optimal Control + Constraints Tests" include("optimal_control.jl") - # @safetestset "print_tree" include("print_tree.jl") - # @safetestset "Constraints Test" include("constraints.jl") - # @safetestset "IfLifting Test" include("if_lifting.jl") - # @safetestset "Analysis Points Test" include("analysis_points.jl") - # @safetestset "Causal Variables Connection Test" include("causal_variables_connection.jl") - # @safetestset "Debugging Test" include("debugging.jl") - # @safetestset "Namespacing test" include("namespacing.jl") - # @safetestset "Subsystem replacement" include("substitute_component.jl") - # end - #end + if GROUP == "All" || GROUP == "InterfaceII" + @testset "InterfaceII" begin + @safetestset "Code Generation Test" include("code_generation.jl") + @safetestset "IndexCache Test" include("index_cache.jl") + @safetestset "Variable Utils Test" include("variable_utils.jl") + @safetestset "Variable Metadata Test" include("test_variable_metadata.jl") + @safetestset "OptimizationSystem Test" include("optimizationsystem.jl") + @safetestset "Discrete System" include("discrete_system.jl") + @safetestset "Implicit Discrete System" include("implicit_discrete_system.jl") + @safetestset "SteadyStateSystem Test" include("steadystatesystems.jl") + @safetestset "SDESystem Test" include("sdesystem.jl") + @safetestset "DDESystem Test" include("dde.jl") + @safetestset "NonlinearSystem Test" include("nonlinearsystem.jl") + @safetestset "SCCNonlinearProblem Test" include("scc_nonlinear_problem.jl") + @safetestset "PDE Construction Test" include("pdesystem.jl") + @safetestset "JumpSystem Test" include("jumpsystem.jl") + @safetestset "Optimal Control + Constraints Tests" include("optimal_control.jl") + @safetestset "print_tree" include("print_tree.jl") + @safetestset "Constraints Test" include("constraints.jl") + @safetestset "IfLifting Test" include("if_lifting.jl") + @safetestset "Analysis Points Test" include("analysis_points.jl") + @safetestset "Causal Variables Connection Test" include("causal_variables_connection.jl") + @safetestset "Debugging Test" include("debugging.jl") + @safetestset "Namespacing test" include("namespacing.jl") + @safetestset "Subsystem replacement" include("substitute_component.jl") + end + end - #if GROUP == "All" || GROUP == "SymbolicIndexingInterface" - # @safetestset "SymbolicIndexingInterface test" include("symbolic_indexing_interface.jl") - # @safetestset "SciML Problem Input Test" include("sciml_problem_inputs.jl") - # @safetestset "MTKParameters Test" include("mtkparameters.jl") - #end + if GROUP == "All" || GROUP == "SymbolicIndexingInterface" + @safetestset "SymbolicIndexingInterface test" include("symbolic_indexing_interface.jl") + @safetestset "SciML Problem Input Test" include("sciml_problem_inputs.jl") + @safetestset "MTKParameters Test" include("mtkparameters.jl") + end - #if GROUP == "All" || GROUP == "Extended" - # @safetestset "Test Big System Usage" include("bigsystem.jl") - # println("C compilation test requires gcc available in the path!") - # @safetestset "C Compilation Test" include("ccompile.jl") - # @testset "Distributed Test" include("distributed.jl") - # @testset "Serialization" include("serialization.jl") - #end + if GROUP == "All" || GROUP == "Extended" + @safetestset "Test Big System Usage" include("bigsystem.jl") + println("C compilation test requires gcc available in the path!") + @safetestset "C Compilation Test" include("ccompile.jl") + @testset "Distributed Test" include("distributed.jl") + @testset "Serialization" include("serialization.jl") + end - #if GROUP == "All" || GROUP == "RegressionI" - # @safetestset "Latexify recipes Test" include("latexify.jl") - #end + if GROUP == "All" || GROUP == "RegressionI" + @safetestset "Latexify recipes Test" include("latexify.jl") + end if GROUP == "All" || GROUP == "Downstream" activate_downstream_env() From 5b13745b6d4fcf299d01b3318c003dd1d00838e6 Mon Sep 17 00:00:00 2001 From: vyudu Date: Fri, 2 May 2025 23:44:46 -0400 Subject: [PATCH 1457/2176] refactor: merge InfiniteOptExts --- Project.toml | 3 - ext/MTKInfiniteOptExt.jl | 432 +++++++++++++++++++++++++++++++++++- ext/MTKJuMPDynamicOptExt.jl | 423 ----------------------------------- 3 files changed, 428 insertions(+), 430 deletions(-) delete mode 100644 ext/MTKJuMPDynamicOptExt.jl diff --git a/Project.toml b/Project.toml index 6ee2226b3b..1de504cbae 100644 --- a/Project.toml +++ b/Project.toml @@ -68,7 +68,6 @@ ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4" DeepDiffs = "ab62b9b5-e342-54a8-a765-a90f495de1a6" FMI = "14a09403-18e3-468f-ad8a-74f8dda2d9ac" InfiniteOpt = "20393b10-9daf-11e9-18c9-8db751c92c57" -JuMP = "4076af6c-e467-56ae-b986-b466b2749572" LabelledArrays = "2ee39098-c373-598a-b85f-a56591580800" [extensions] @@ -77,7 +76,6 @@ MTKChainRulesCoreExt = "ChainRulesCore" MTKDeepDiffsExt = "DeepDiffs" MTKFMIExt = "FMI" MTKInfiniteOptExt = "InfiniteOpt" -MTKJuMPDynamicOptExt = ["JuMP", "InfiniteOpt"] MTKLabelledArraysExt = "LabelledArrays" [compat] @@ -99,7 +97,6 @@ DeepDiffs = "1" DelayDiffEq = "5.50" DiffEqBase = "6.170.1" DiffEqCallbacks = "2.16, 3, 4" -DiffEqDevTools = "2.48.0" DiffEqNoiseProcess = "5" DiffRules = "0.1, 1.0" DifferentiationInterface = "0.6.47" diff --git a/ext/MTKInfiniteOptExt.jl b/ext/MTKInfiniteOptExt.jl index 3b482a1f03..532bc475c4 100644 --- a/ext/MTKInfiniteOptExt.jl +++ b/ext/MTKInfiniteOptExt.jl @@ -1,11 +1,435 @@ module MTKInfiniteOptExt -import ModelingToolkit +using ModelingToolkit +using InfiniteOpt +using DiffEqBase +using LinearAlgebra +using StaticArrays import SymbolicUtils import NaNMath -import InfiniteOpt -import InfiniteOpt: JuMP, GeneralVariableRef +const MTK = ModelingToolkit + +struct JuMPDynamicOptProblem{uType, tType, isinplace, P, F, K} <: + AbstractDynamicOptProblem{uType, tType, isinplace} + f::F + u0::uType + tspan::tType + p::P + model::InfiniteModel + kwargs::K + + function JuMPDynamicOptProblem(f, u0, tspan, p, model, kwargs...) + new{typeof(u0), typeof(tspan), SciMLBase.isinplace(f, 5), + typeof(p), typeof(f), typeof(kwargs)}(f, u0, tspan, p, model, kwargs) + end +end + +struct InfiniteOptDynamicOptProblem{uType, tType, isinplace, P, F, K} <: + AbstractDynamicOptProblem{uType, tType, isinplace} + f::F + u0::uType + tspan::tType + p::P + model::InfiniteModel + kwargs::K + + function InfiniteOptDynamicOptProblem(f, u0, tspan, p, model, kwargs...) + new{typeof(u0), typeof(tspan), SciMLBase.isinplace(f), + typeof(p), typeof(f), typeof(kwargs)}(f, u0, tspan, p, model, kwargs) + end +end + +""" + JuMPDynamicOptProblem(sys::ODESystem, u0, tspan, p; dt) + +Convert an ODESystem representing an optimal control system into a JuMP model +for solving using optimization. Must provide either `dt`, the timestep between collocation +points (which, along with the timespan, determines the number of points), or directly +provide the number of points as `steps`. + +The optimization variables: +- a vector-of-vectors U representing the unknowns as an interpolation array +- a vector-of-vectors V representing the controls as an interpolation array + +The constraints are: +- The set of user constraints passed to the ODESystem via `constraints` +- The solver constraints that encode the time-stepping used by the solver +""" +function MTK.JuMPDynamicOptProblem(sys::ODESystem, u0map, tspan, pmap; + dt = nothing, + steps = nothing, + guesses = Dict(), kwargs...) + MTK.warn_overdetermined(sys, u0map) + _u0map = has_alg_eqs(sys) ? u0map : merge(Dict(u0map), Dict(guesses)) + f, u0, p = MTK.process_SciMLProblem(ODEInputFunction, sys, _u0map, pmap; + t = tspan !== nothing ? tspan[1] : tspan, kwargs...) + + pmap = Dict{Any, Any}(pmap) + steps, is_free_t = MTK.process_tspan(tspan, dt, steps) + model = init_model(sys, tspan, steps, u0map, pmap, u0; is_free_t) + + JuMPDynamicOptProblem(f, u0, tspan, p, model, kwargs...) +end + +""" + InfiniteOptDynamicOptProblem(sys::ODESystem, u0map, tspan, pmap; dt) + +Convert an ODESystem representing an optimal control system into a InfiniteOpt model +for solving using optimization. Must provide `dt` for determining the length +of the interpolation arrays. + +Related to `JuMPDynamicOptProblem`, but directly adds the differential equations +of the system as derivative constraints, rather than using a solver tableau. +""" +function MTK.InfiniteOptDynamicOptProblem(sys::ODESystem, u0map, tspan, pmap; + dt = nothing, + steps = nothing, + guesses = Dict(), kwargs...) + MTK.warn_overdetermined(sys, u0map) + _u0map = has_alg_eqs(sys) ? u0map : merge(Dict(u0map), Dict(guesses)) + f, u0, p = MTK.process_SciMLProblem(ODEInputFunction, sys, _u0map, pmap; + t = tspan !== nothing ? tspan[1] : tspan, kwargs...) + + pmap = Dict{Any, Any}(pmap) + steps, is_free_t = MTK.process_tspan(tspan, dt, steps) + model = init_model(sys, tspan, steps, u0map, pmap, u0; is_free_t) + + add_infopt_solve_constraints!(model, sys, pmap; is_free_t) + InfiniteOptDynamicOptProblem(f, u0, tspan, p, model, kwargs...) +end + +# Initialize InfiniteOpt model. +function init_model(sys, tspan, steps, u0map, pmap, u0; is_free_t = false) + ctrls = MTK.unbound_inputs(sys) + states = unknowns(sys) + model = InfiniteModel() + + if is_free_t + (ts_sym, te_sym) = tspan + MTK.symbolic_type(ts_sym) !== MTK.NotSymbolic() && + error("Free initial time problems are not currently supported.") + @variable(model, tf, start=pmap[te_sym]) + set_lower_bound(tf, ts_sym) + hasbounds(te_sym) && begin + lo, hi = getbounds(te_sym) + set_lower_bound(tf, lo) + set_upper_bound(tf, hi) + end + pmap[te_sym] = model[:tf] + tspan = (0, 1) + end + + @infinite_parameter(model, t in [tspan[1], tspan[2]], num_supports=steps) + @variable(model, U[i = 1:length(states)], Infinite(t), start=u0[i]) + c0 = MTK.value.([pmap[c] for c in ctrls]) + @variable(model, V[i = 1:length(ctrls)], Infinite(t), start=c0[i]) + for (i, ct) in enumerate(ctrls) + pmap[ct] = model[:V][i] + end + + set_jump_bounds!(model, sys, pmap) + add_jump_cost_function!(model, sys, (tspan[1], tspan[2]), pmap; is_free_t) + add_user_constraints!(model, sys, pmap; is_free_t) + + stidxmap = Dict([v => i for (i, v) in enumerate(states)]) + u0map = Dict([MTK.default_toterm(MTK.value(k)) => v for (k, v) in u0map]) + u0_idxs = has_alg_eqs(sys) ? collect(1:length(states)) : + [stidxmap[MTK.default_toterm(k)] for (k, v) in u0map] + add_initial_constraints!(model, u0, u0_idxs, tspan[1]) + return model +end + +""" +Modify the pmap by replacing controls with V[i](t), and tf with the model's final time variable for free final time problems. +""" +function modified_pmap(model, sys, pmap) + pmap = Dict{Any, Any}(pmap) +end + +function set_jump_bounds!(model, sys, pmap) + U = model[:U] + for (i, u) in enumerate(unknowns(sys)) + if MTK.hasbounds(u) + lo, hi = MTK.getbounds(u) + set_lower_bound(U[i], Symbolics.fixpoint_sub(lo, pmap)) + set_upper_bound(U[i], Symbolics.fixpoint_sub(hi, pmap)) + end + end + + V = model[:V] + for (i, v) in enumerate(MTK.unbound_inputs(sys)) + if MTK.hasbounds(v) + lo, hi = MTK.getbounds(v) + set_lower_bound(V[i], Symbolics.fixpoint_sub(lo, pmap)) + set_upper_bound(V[i], Symbolics.fixpoint_sub(hi, pmap)) + end + end +end + +function add_jump_cost_function!(model::InfiniteModel, sys, tspan, pmap; is_free_t = false) + jcosts = MTK.get_costs(sys) + consolidate = MTK.get_consolidate(sys) + if isnothing(jcosts) || isempty(jcosts) + @objective(model, Min, 0) + return + end + jcosts = substitute_jump_vars(model, sys, pmap, jcosts; is_free_t) + tₛ = is_free_t ? model[:tf] : 1 + + # Substitute integral + iv = MTK.get_iv(sys) + + intmap = Dict() + for int in MTK.collect_applied_operators(jcosts, Symbolics.Integral) + op = MTK.operation(int) + arg = only(arguments(MTK.value(int))) + lo, hi = (op.domain.domain.left, op.domain.domain.right) + @show hi + @show pmap + @show haskey(pmap, hi) + hi = haskey(pmap, hi) ? 1 : hi + @show hi + @show typeof(arg) + @show typeof(lo) + intmap[int] = tₛ * InfiniteOpt.∫(arg, model[:t], lo, hi) + end + jcosts = map(c -> Symbolics.substitute(c, intmap), jcosts) + @objective(model, Min, consolidate(jcosts)) +end + +function add_user_constraints!(model::InfiniteModel, sys, pmap; is_free_t = false) + conssys = MTK.get_constraintsystem(sys) + jconstraints = isnothing(conssys) ? nothing : MTK.get_constraints(conssys) + (isnothing(jconstraints) || isempty(jconstraints)) && return nothing + + if is_free_t + for u in MTK.get_unknowns(conssys) + x = MTK.operation(u) + t = only(arguments(u)) + if (MTK.symbolic_type(t) === MTK.NotSymbolic()) + error("Provided specific time constraint in a free final time problem. This is not supported by the JuMP/InfiniteOpt collocation solvers. The offending variable is $u. Specific-time constraints can only be specified at the beginning or end of the timespan.") + end + end + end -# This file contains method definitions to make it possible to trace through functions generated by MTK using JuMP variables + auxmap = Dict([u => MTK.default_toterm(MTK.value(u)) for u in unknowns(conssys)]) + jconstraints = substitute_jump_vars(model, sys, pmap, jconstraints; auxmap, is_free_t) + + # Substitute to-term'd variables + for (i, cons) in enumerate(jconstraints) + if cons isa Equation + @constraint(model, cons.lhs - cons.rhs==0, base_name="user[$i]") + elseif cons.relational_op === Symbolics.geq + @constraint(model, cons.lhs - cons.rhs≥0, base_name="user[$i]") + else + @constraint(model, cons.lhs - cons.rhs≤0, base_name="user[$i]") + end + end +end + +function add_initial_constraints!(model::InfiniteModel, u0, u0_idxs, ts) + U = model[:U] + @constraint(model, initial[i in u0_idxs], U[i](ts)==u0[i]) +end + +function substitute_jump_vars(model, sys, pmap, exprs; auxmap = Dict(), is_free_t = false) + iv = MTK.get_iv(sys) + sts = unknowns(sys) + cts = MTK.unbound_inputs(sys) + U = model[:U] + V = model[:V] + x_ops = [MTK.operation(MTK.unwrap(st)) for st in sts] + c_ops = [MTK.operation(MTK.unwrap(ct)) for ct in cts] + + exprs = map(c -> Symbolics.fixpoint_sub(c, auxmap), exprs) + exprs = map(c -> Symbolics.fixpoint_sub(c, Dict(pmap)), exprs) + if is_free_t + tf = model[:tf] + free_t_map = Dict([[x(tf) => U[i](1) for (i, x) in enumerate(x_ops)]; + [c(tf) => V[i](1) for (i, c) in enumerate(c_ops)]]) + exprs = map(c -> Symbolics.fixpoint_sub(c, free_t_map), exprs) + end + + # for variables like x(t) + whole_interval_map = Dict([[v => U[i] for (i, v) in enumerate(sts)]; + [v => V[i] for (i, v) in enumerate(cts)]]) + exprs = map(c -> Symbolics.fixpoint_sub(c, whole_interval_map), exprs) + + # for variables like x(1.0) + fixed_t_map = Dict([[x_ops[i] => U[i] for i in 1:length(U)]; + [c_ops[i] => V[i] for i in 1:length(V)]]) + + exprs = map(c -> Symbolics.fixpoint_sub(c, fixed_t_map), exprs) + exprs +end + +is_explicit(tableau) = tableau isa DiffEqDevTools.ExplicitRKTableau + +function add_infopt_solve_constraints!(model::InfiniteModel, sys, pmap; is_free_t = false) + # Differential equations + U = model[:U] + t = model[:t] + D = Differential(MTK.get_iv(sys)) + diffsubmap = Dict([D(U[i]) => ∂(U[i], t) for i in 1:length(U)]) + tₛ = is_free_t ? model[:tf] : 1 + + diff_eqs = substitute_jump_vars(model, sys, pmap, diff_equations(sys)) + diff_eqs = map(e -> Symbolics.substitute(e, diffsubmap), diff_eqs) + @constraint(model, D[i = 1:length(diff_eqs)], diff_eqs[i].lhs==tₛ * diff_eqs[i].rhs) + + # Algebraic equations + alg_eqs = substitute_jump_vars(model, sys, pmap, alg_equations(sys)) + @constraint(model, A[i = 1:length(alg_eqs)], alg_eqs[i].lhs==alg_eqs[i].rhs) +end + +function add_jump_solve_constraints!(prob, tableau; is_free_t = false) + A = tableau.A + α = tableau.α + c = tableau.c + model = prob.model + f = prob.f + p = prob.p + + t = model[:t] + tsteps = supports(t) + tmax = tsteps[end] + pop!(tsteps) + tₛ = is_free_t ? model[:tf] : 1 + dt = tsteps[2] - tsteps[1] + + U = model[:U] + V = model[:V] + nᵤ = length(U) + nᵥ = length(V) + if is_explicit(tableau) + K = Any[] + for τ in tsteps + for (i, h) in enumerate(c) + ΔU = sum([A[i, j] * K[j] for j in 1:(i - 1)], init = zeros(nᵤ)) + Uₙ = [U[i](τ) + ΔU[i] * dt for i in 1:nᵤ] + Vₙ = [V[i](τ) for i in 1:nᵥ] + Kₙ = tₛ * f(Uₙ, Vₙ, p, τ + h * dt) # scale the time + push!(K, Kₙ) + end + ΔU = dt * sum([α[i] * K[i] for i in 1:length(α)]) + @constraint(model, [n = 1:nᵤ], U[n](τ) + ΔU[n]==U[n](τ + dt), + base_name="solve_time_$τ") + empty!(K) + end + else + @variable(model, K[1:length(α), 1:nᵤ], Infinite(t)) + ΔUs = A * K + ΔU_tot = dt * (K' * α) + for τ in tsteps + for (i, h) in enumerate(c) + ΔU = @view ΔUs[i, :] + Uₙ = U + ΔU * h * dt + @constraint(model, [j = 1:nᵤ], K[i, j]==(tₛ * f(Uₙ, V, p, τ + h * dt)[j]), + DomainRestrictions(t => τ), base_name="solve_K$i($τ)") + end + @constraint(model, [n = 1:nᵤ], U[n](τ) + ΔU_tot[n]==U[n](min(τ + dt, tmax)), + DomainRestrictions(t => τ), base_name="solve_U($τ)") + end + end +end + +""" +Default ODE Tableau: RadauIIA5 +""" +function constructDefault(T::Type = Float64) + sq6 = sqrt(6) + A = [11 // 45-7sq6 / 360 37 // 225-169sq6 / 1800 -2 // 225+sq6 / 75 + 37 // 225+169sq6 / 1800 11 // 45+7sq6 / 360 -2 // 225-sq6 / 75 + 4 // 9-sq6 / 36 4 // 9+sq6 / 36 1//9] + c = [2 // 5 - sq6 / 10; 2 / 5 + sq6 / 10; 1] + α = [4 // 9 - sq6 / 36; 4 // 9 + sq6 / 36; 1 // 9] + A = map(T, A) + α = map(T, α) + c = map(T, c) + + (; A = A, α = α, c = c) +end + +""" +Solve JuMPDynamicOptProblem. Arguments: +- prob: a JumpDynamicOptProblem +- jump_solver: a LP solver such as HiGHS +- ode_solver: Takes in a symbol representing the solver. Acceptable solvers may be found at https://docs.sciml.ai/DiffEqDevDocs/stable/internals/tableaus/. Note that the symbol may be different than the typical name of the solver, e.g. :Tsitouras5 rather than Tsit5. +- silent: set the model silent (suppress model output) + +Returns a DynamicOptSolution, which contains both the model and the ODE solution. +""" +function DiffEqBase.solve( + prob::JuMPDynamicOptProblem, jump_solver, ode_solver::Symbol = :Default; silent = false) + model = prob.model + tableau_getter = Symbol(:construct, ode_solver) + @eval tableau = $tableau_getter() + silent && set_silent(model) + + # Unregister current solver constraints + for con in all_constraints(model) + if occursin("solve", JuMP.name(con)) + unregister(model, Symbol(JuMP.name(con))) + delete(model, con) + end + end + unregister(model, :K) + for var in all_variables(model) + if occursin("K", JuMP.name(var)) + unregister(model, Symbol(JuMP.name(var))) + delete(model, var) + end + end + add_jump_solve_constraints!(prob, tableau; is_free_t = haskey(model, :tf)) + _solve(prob, jump_solver, ode_solver) +end + +""" +`derivative_method` kwarg refers to the method used by InfiniteOpt to compute derivatives. The list of possible options can be found at https://infiniteopt.github.io/InfiniteOpt.jl/stable/guide/derivative/. Defaults to FiniteDifference(Backward()). +""" +function DiffEqBase.solve(prob::InfiniteOptDynamicOptProblem, jump_solver; + derivative_method = InfiniteOpt.FiniteDifference(Backward()), silent = false) + model = prob.model + silent && set_silent(model) + set_derivative_method(model[:t], derivative_method) + _solve(prob, jump_solver, derivative_method) +end + +function _solve(prob::AbstractDynamicOptProblem, jump_solver, solver) + model = prob.model + set_optimizer(model, jump_solver) + optimize!(model) + + tstatus = termination_status(model) + pstatus = primal_status(model) + !has_values(model) && + error("Model not solvable; please report this to github.com/SciML/ModelingToolkit.jl with a MWE.") + + tf = haskey(model, :tf) ? value(model[:tf]) : 1 + ts = tf * supports(model[:t]) + U_vals = value.(model[:U]) + U_vals = [[U_vals[i][j] for i in 1:length(U_vals)] for j in 1:length(ts)] + sol = DiffEqBase.build_solution(prob, solver, ts, U_vals) + + input_sol = nothing + if !isempty(model[:V]) + V_vals = value.(model[:V]) + V_vals = [[V_vals[i][j] for i in 1:length(V_vals)] for j in 1:length(ts)] + input_sol = DiffEqBase.build_solution(prob, solver, ts, V_vals) + end + + if !(pstatus === FEASIBLE_POINT && + (tstatus === OPTIMAL || tstatus === LOCALLY_SOLVED || tstatus === ALMOST_OPTIMAL || + tstatus === ALMOST_LOCALLY_SOLVED)) + sol = SciMLBase.solution_new_retcode(sol, SciMLBase.ReturnCode.ConvergenceFailure) + !isnothing(input_sol) && (input_sol = SciMLBase.solution_new_retcode( + input_sol, SciMLBase.ReturnCode.ConvergenceFailure)) + end + + DynamicOptSolution(model, sol, input_sol) +end + + +import InfiniteOpt: JuMP, GeneralVariableRef for ff in [acos, log1p, acosh, log2, asin, tan, atanh, cos, log, sin, log10, sqrt] f = nameof(ff) diff --git a/ext/MTKJuMPDynamicOptExt.jl b/ext/MTKJuMPDynamicOptExt.jl deleted file mode 100644 index 0dd1b5c564..0000000000 --- a/ext/MTKJuMPDynamicOptExt.jl +++ /dev/null @@ -1,423 +0,0 @@ -module MTKJuMPDynamicOptExt -using ModelingToolkit -using JuMP, InfiniteOpt -using DiffEqDevTools, DiffEqBase -using LinearAlgebra -using StaticArrays -const MTK = ModelingToolkit - -struct JuMPDynamicOptProblem{uType, tType, isinplace, P, F, K} <: - AbstractDynamicOptProblem{uType, tType, isinplace} - f::F - u0::uType - tspan::tType - p::P - model::InfiniteModel - kwargs::K - - function JuMPDynamicOptProblem(f, u0, tspan, p, model, kwargs...) - new{typeof(u0), typeof(tspan), SciMLBase.isinplace(f, 5), - typeof(p), typeof(f), typeof(kwargs)}(f, u0, tspan, p, model, kwargs) - end -end - -struct InfiniteOptDynamicOptProblem{uType, tType, isinplace, P, F, K} <: - AbstractDynamicOptProblem{uType, tType, isinplace} - f::F - u0::uType - tspan::tType - p::P - model::InfiniteModel - kwargs::K - - function InfiniteOptDynamicOptProblem(f, u0, tspan, p, model, kwargs...) - new{typeof(u0), typeof(tspan), SciMLBase.isinplace(f), - typeof(p), typeof(f), typeof(kwargs)}(f, u0, tspan, p, model, kwargs) - end -end - -""" - JuMPDynamicOptProblem(sys::ODESystem, u0, tspan, p; dt) - -Convert an ODESystem representing an optimal control system into a JuMP model -for solving using optimization. Must provide either `dt`, the timestep between collocation -points (which, along with the timespan, determines the number of points), or directly -provide the number of points as `steps`. - -The optimization variables: -- a vector-of-vectors U representing the unknowns as an interpolation array -- a vector-of-vectors V representing the controls as an interpolation array - -The constraints are: -- The set of user constraints passed to the ODESystem via `constraints` -- The solver constraints that encode the time-stepping used by the solver -""" -function MTK.JuMPDynamicOptProblem(sys::ODESystem, u0map, tspan, pmap; - dt = nothing, - steps = nothing, - guesses = Dict(), kwargs...) - MTK.warn_overdetermined(sys, u0map) - _u0map = has_alg_eqs(sys) ? u0map : merge(Dict(u0map), Dict(guesses)) - f, u0, p = MTK.process_SciMLProblem(ODEInputFunction, sys, _u0map, pmap; - t = tspan !== nothing ? tspan[1] : tspan, kwargs...) - - pmap = Dict{Any, Any}(pmap) - steps, is_free_t = MTK.process_tspan(tspan, dt, steps) - model = init_model(sys, tspan, steps, u0map, pmap, u0; is_free_t) - - JuMPDynamicOptProblem(f, u0, tspan, p, model, kwargs...) -end - -""" - InfiniteOptDynamicOptProblem(sys::ODESystem, u0map, tspan, pmap; dt) - -Convert an ODESystem representing an optimal control system into a InfiniteOpt model -for solving using optimization. Must provide `dt` for determining the length -of the interpolation arrays. - -Related to `JuMPDynamicOptProblem`, but directly adds the differential equations -of the system as derivative constraints, rather than using a solver tableau. -""" -function MTK.InfiniteOptDynamicOptProblem(sys::ODESystem, u0map, tspan, pmap; - dt = nothing, - steps = nothing, - guesses = Dict(), kwargs...) - MTK.warn_overdetermined(sys, u0map) - _u0map = has_alg_eqs(sys) ? u0map : merge(Dict(u0map), Dict(guesses)) - f, u0, p = MTK.process_SciMLProblem(ODEInputFunction, sys, _u0map, pmap; - t = tspan !== nothing ? tspan[1] : tspan, kwargs...) - - pmap = Dict{Any, Any}(pmap) - steps, is_free_t = MTK.process_tspan(tspan, dt, steps) - model = init_model(sys, tspan, steps, u0map, pmap, u0; is_free_t) - - add_infopt_solve_constraints!(model, sys, pmap; is_free_t) - InfiniteOptDynamicOptProblem(f, u0, tspan, p, model, kwargs...) -end - -# Initialize InfiniteOpt model. -function init_model(sys, tspan, steps, u0map, pmap, u0; is_free_t = false) - ctrls = MTK.unbound_inputs(sys) - states = unknowns(sys) - model = InfiniteModel() - - if is_free_t - (ts_sym, te_sym) = tspan - MTK.symbolic_type(ts_sym) !== MTK.NotSymbolic() && - error("Free initial time problems are not currently supported.") - @variable(model, tf, start=pmap[te_sym]) - set_lower_bound(tf, ts_sym) - hasbounds(te_sym) && begin - lo, hi = getbounds(te_sym) - set_lower_bound(tf, lo) - set_upper_bound(tf, hi) - end - pmap[te_sym] = model[:tf] - tspan = (0, 1) - end - - @infinite_parameter(model, t in [tspan[1], tspan[2]], num_supports=steps) - @variable(model, U[i = 1:length(states)], Infinite(t), start=u0[i]) - c0 = MTK.value.([pmap[c] for c in ctrls]) - @variable(model, V[i = 1:length(ctrls)], Infinite(t), start=c0[i]) - for (i, ct) in enumerate(ctrls) - pmap[ct] = model[:V][i] - end - - set_jump_bounds!(model, sys, pmap) - add_jump_cost_function!(model, sys, (tspan[1], tspan[2]), pmap; is_free_t) - add_user_constraints!(model, sys, pmap; is_free_t) - - stidxmap = Dict([v => i for (i, v) in enumerate(states)]) - u0map = Dict([MTK.default_toterm(MTK.value(k)) => v for (k, v) in u0map]) - u0_idxs = has_alg_eqs(sys) ? collect(1:length(states)) : - [stidxmap[MTK.default_toterm(k)] for (k, v) in u0map] - add_initial_constraints!(model, u0, u0_idxs, tspan[1]) - return model -end - -""" -Modify the pmap by replacing controls with V[i](t), and tf with the model's final time variable for free final time problems. -""" -function modified_pmap(model, sys, pmap) - pmap = Dict{Any, Any}(pmap) -end - -function set_jump_bounds!(model, sys, pmap) - U = model[:U] - for (i, u) in enumerate(unknowns(sys)) - if MTK.hasbounds(u) - lo, hi = MTK.getbounds(u) - set_lower_bound(U[i], Symbolics.fixpoint_sub(lo, pmap)) - set_upper_bound(U[i], Symbolics.fixpoint_sub(hi, pmap)) - end - end - - V = model[:V] - for (i, v) in enumerate(MTK.unbound_inputs(sys)) - if MTK.hasbounds(v) - lo, hi = MTK.getbounds(v) - set_lower_bound(V[i], Symbolics.fixpoint_sub(lo, pmap)) - set_upper_bound(V[i], Symbolics.fixpoint_sub(hi, pmap)) - end - end -end - -function add_jump_cost_function!(model::InfiniteModel, sys, tspan, pmap; is_free_t = false) - jcosts = MTK.get_costs(sys) - consolidate = MTK.get_consolidate(sys) - if isnothing(jcosts) || isempty(jcosts) - @objective(model, Min, 0) - return - end - jcosts = substitute_jump_vars(model, sys, pmap, jcosts; is_free_t) - tₛ = is_free_t ? model[:tf] : 1 - - # Substitute integral - iv = MTK.get_iv(sys) - - intmap = Dict() - for int in MTK.collect_applied_operators(jcosts, Symbolics.Integral) - op = MTK.operation(int) - arg = only(arguments(MTK.value(int))) - lo, hi = (op.domain.domain.left, op.domain.domain.right) - hi = haskey(pmap, hi) ? 1 : hi - intmap[int] = tₛ * InfiniteOpt.∫(arg, model[:t], lo, hi) - end - jcosts = map(c -> Symbolics.substitute(c, intmap), jcosts) - @objective(model, Min, consolidate(jcosts)) -end - -function add_user_constraints!(model::InfiniteModel, sys, pmap; is_free_t = false) - conssys = MTK.get_constraintsystem(sys) - jconstraints = isnothing(conssys) ? nothing : MTK.get_constraints(conssys) - (isnothing(jconstraints) || isempty(jconstraints)) && return nothing - - if is_free_t - for u in MTK.get_unknowns(conssys) - x = MTK.operation(u) - t = only(arguments(u)) - if (MTK.symbolic_type(t) === MTK.NotSymbolic()) - error("Provided specific time constraint in a free final time problem. This is not supported by the JuMP/InfiniteOpt collocation solvers. The offending variable is $u. Specific-time constraints can only be specified at the beginning or end of the timespan.") - end - end - end - - auxmap = Dict([u => MTK.default_toterm(MTK.value(u)) for u in unknowns(conssys)]) - jconstraints = substitute_jump_vars(model, sys, pmap, jconstraints; auxmap, is_free_t) - - # Substitute to-term'd variables - for (i, cons) in enumerate(jconstraints) - if cons isa Equation - @constraint(model, cons.lhs - cons.rhs==0, base_name="user[$i]") - elseif cons.relational_op === Symbolics.geq - @constraint(model, cons.lhs - cons.rhs≥0, base_name="user[$i]") - else - @constraint(model, cons.lhs - cons.rhs≤0, base_name="user[$i]") - end - end -end - -function add_initial_constraints!(model::InfiniteModel, u0, u0_idxs, ts) - U = model[:U] - @constraint(model, initial[i in u0_idxs], U[i](ts)==u0[i]) -end - -function substitute_jump_vars(model, sys, pmap, exprs; auxmap = Dict(), is_free_t = false) - iv = MTK.get_iv(sys) - sts = unknowns(sys) - cts = MTK.unbound_inputs(sys) - U = model[:U] - V = model[:V] - x_ops = [MTK.operation(MTK.unwrap(st)) for st in sts] - c_ops = [MTK.operation(MTK.unwrap(ct)) for ct in cts] - - exprs = map(c -> Symbolics.fixpoint_sub(c, auxmap), exprs) - exprs = map(c -> Symbolics.fixpoint_sub(c, Dict(pmap)), exprs) - if is_free_t - tf = model[:tf] - free_t_map = Dict([[x(tf) => U[i](1) for (i, x) in enumerate(x_ops)]; - [c(tf) => V[i](1) for (i, c) in enumerate(c_ops)]]) - exprs = map(c -> Symbolics.fixpoint_sub(c, free_t_map), exprs) - end - - # for variables like x(t) - whole_interval_map = Dict([[v => U[i] for (i, v) in enumerate(sts)]; - [v => V[i] for (i, v) in enumerate(cts)]]) - exprs = map(c -> Symbolics.fixpoint_sub(c, whole_interval_map), exprs) - - # for variables like x(1.0) - fixed_t_map = Dict([[x_ops[i] => U[i] for i in 1:length(U)]; - [c_ops[i] => V[i] for i in 1:length(V)]]) - - exprs = map(c -> Symbolics.fixpoint_sub(c, fixed_t_map), exprs) - exprs -end - -is_explicit(tableau) = tableau isa DiffEqDevTools.ExplicitRKTableau - -function add_infopt_solve_constraints!(model::InfiniteModel, sys, pmap; is_free_t = false) - # Differential equations - U = model[:U] - t = model[:t] - D = Differential(MTK.get_iv(sys)) - diffsubmap = Dict([D(U[i]) => ∂(U[i], t) for i in 1:length(U)]) - tₛ = is_free_t ? model[:tf] : 1 - - diff_eqs = substitute_jump_vars(model, sys, pmap, diff_equations(sys)) - diff_eqs = map(e -> Symbolics.substitute(e, diffsubmap), diff_eqs) - @constraint(model, D[i = 1:length(diff_eqs)], diff_eqs[i].lhs==tₛ * diff_eqs[i].rhs) - - # Algebraic equations - alg_eqs = substitute_jump_vars(model, sys, pmap, alg_equations(sys)) - @constraint(model, A[i = 1:length(alg_eqs)], alg_eqs[i].lhs==alg_eqs[i].rhs) -end - -function add_jump_solve_constraints!(prob, tableau; is_free_t = false) - A = tableau.A - α = tableau.α - c = tableau.c - model = prob.model - f = prob.f - p = prob.p - - t = model[:t] - tsteps = supports(t) - tmax = tsteps[end] - pop!(tsteps) - tₛ = is_free_t ? model[:tf] : 1 - dt = tsteps[2] - tsteps[1] - - U = model[:U] - V = model[:V] - nᵤ = length(U) - nᵥ = length(V) - if is_explicit(tableau) - K = Any[] - for τ in tsteps - for (i, h) in enumerate(c) - ΔU = sum([A[i, j] * K[j] for j in 1:(i - 1)], init = zeros(nᵤ)) - Uₙ = [U[i](τ) + ΔU[i] * dt for i in 1:nᵤ] - Vₙ = [V[i](τ) for i in 1:nᵥ] - Kₙ = tₛ * f(Uₙ, Vₙ, p, τ + h * dt) # scale the time - push!(K, Kₙ) - end - ΔU = dt * sum([α[i] * K[i] for i in 1:length(α)]) - @constraint(model, [n = 1:nᵤ], U[n](τ) + ΔU[n]==U[n](τ + dt), - base_name="solve_time_$τ") - empty!(K) - end - else - @variable(model, K[1:length(α), 1:nᵤ], Infinite(t)) - ΔUs = A * K - ΔU_tot = dt * (K' * α) - for τ in tsteps - for (i, h) in enumerate(c) - ΔU = @view ΔUs[i, :] - Uₙ = U + ΔU * h * dt - @constraint(model, [j = 1:nᵤ], K[i, j]==(tₛ * f(Uₙ, V, p, τ + h * dt)[j]), - DomainRestrictions(t => τ), base_name="solve_K$i($τ)") - end - @constraint(model, [n = 1:nᵤ], U[n](τ) + ΔU_tot[n]==U[n](min(τ + dt, tmax)), - DomainRestrictions(t => τ), base_name="solve_U($τ)") - end - end -end - -""" -Default ODE Tableau: RadauIIA5 -""" -function constructDefault(T::Type = Float64) - sq6 = sqrt(6) - A = [11 // 45-7sq6 / 360 37 // 225-169sq6 / 1800 -2 // 225+sq6 / 75 - 37 // 225+169sq6 / 1800 11 // 45+7sq6 / 360 -2 // 225-sq6 / 75 - 4 // 9-sq6 / 36 4 // 9+sq6 / 36 1//9] - c = [2 // 5 - sq6 / 10; 2 / 5 + sq6 / 10; 1] - α = [4 // 9 - sq6 / 36; 4 // 9 + sq6 / 36; 1 // 9] - A = map(T, A) - α = map(T, α) - c = map(T, c) - - (; A = A, α = α, c = c) -end - -""" -Solve JuMPDynamicOptProblem. Arguments: -- prob: a JumpDynamicOptProblem -- jump_solver: a LP solver such as HiGHS -- ode_solver: Takes in a symbol representing the solver. Acceptable solvers may be found at https://docs.sciml.ai/DiffEqDevDocs/stable/internals/tableaus/. Note that the symbol may be different than the typical name of the solver, e.g. :Tsitouras5 rather than Tsit5. -- silent: set the model silent (suppress model output) - -Returns a DynamicOptSolution, which contains both the model and the ODE solution. -""" -function DiffEqBase.solve( - prob::JuMPDynamicOptProblem, jump_solver, ode_solver::Symbol = :Default; silent = false) - model = prob.model - tableau_getter = Symbol(:construct, ode_solver) - @eval tableau = $tableau_getter() - silent && set_silent(model) - - # Unregister current solver constraints - for con in all_constraints(model) - if occursin("solve", JuMP.name(con)) - unregister(model, Symbol(JuMP.name(con))) - delete(model, con) - end - end - unregister(model, :K) - for var in all_variables(model) - if occursin("K", JuMP.name(var)) - unregister(model, Symbol(JuMP.name(var))) - delete(model, var) - end - end - add_jump_solve_constraints!(prob, tableau; is_free_t = haskey(model, :tf)) - _solve(prob, jump_solver, ode_solver) -end - -""" -`derivative_method` kwarg refers to the method used by InfiniteOpt to compute derivatives. The list of possible options can be found at https://infiniteopt.github.io/InfiniteOpt.jl/stable/guide/derivative/. Defaults to FiniteDifference(Backward()). -""" -function DiffEqBase.solve(prob::InfiniteOptDynamicOptProblem, jump_solver; - derivative_method = InfiniteOpt.FiniteDifference(Backward()), silent = false) - model = prob.model - silent && set_silent(model) - set_derivative_method(model[:t], derivative_method) - _solve(prob, jump_solver, derivative_method) -end - -function _solve(prob::AbstractDynamicOptProblem, jump_solver, solver) - model = prob.model - set_optimizer(model, jump_solver) - optimize!(model) - - tstatus = termination_status(model) - pstatus = primal_status(model) - !has_values(model) && - error("Model not solvable; please report this to github.com/SciML/ModelingToolkit.jl with a MWE.") - - tf = haskey(model, :tf) ? value(model[:tf]) : 1 - ts = tf * supports(model[:t]) - U_vals = value.(model[:U]) - U_vals = [[U_vals[i][j] for i in 1:length(U_vals)] for j in 1:length(ts)] - sol = DiffEqBase.build_solution(prob, solver, ts, U_vals) - - input_sol = nothing - if !isempty(model[:V]) - V_vals = value.(model[:V]) - V_vals = [[V_vals[i][j] for i in 1:length(V_vals)] for j in 1:length(ts)] - input_sol = DiffEqBase.build_solution(prob, solver, ts, V_vals) - end - - if !(pstatus === FEASIBLE_POINT && - (tstatus === OPTIMAL || tstatus === LOCALLY_SOLVED || tstatus === ALMOST_OPTIMAL || - tstatus === ALMOST_LOCALLY_SOLVED)) - sol = SciMLBase.solution_new_retcode(sol, SciMLBase.ReturnCode.ConvergenceFailure) - !isnothing(input_sol) && (input_sol = SciMLBase.solution_new_retcode( - input_sol, SciMLBase.ReturnCode.ConvergenceFailure)) - end - - DynamicOptSolution(model, sol, input_sol) -end - -end From b32af3d72ad87dffdc57d7e0294f9b48c8e77cd0 Mon Sep 17 00:00:00 2001 From: vyudu Date: Sat, 3 May 2025 00:02:52 -0400 Subject: [PATCH 1458/2176] test default solver --- ext/MTKInfiniteOptExt.jl | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/ext/MTKInfiniteOptExt.jl b/ext/MTKInfiniteOptExt.jl index 532bc475c4..53b3edd9fe 100644 --- a/ext/MTKInfiniteOptExt.jl +++ b/ext/MTKInfiniteOptExt.jl @@ -183,13 +183,8 @@ function add_jump_cost_function!(model::InfiniteModel, sys, tspan, pmap; is_free op = MTK.operation(int) arg = only(arguments(MTK.value(int))) lo, hi = (op.domain.domain.left, op.domain.domain.right) - @show hi - @show pmap - @show haskey(pmap, hi) - hi = haskey(pmap, hi) ? 1 : hi - @show hi - @show typeof(arg) - @show typeof(lo) + lo = MTK.value(lo) + hi = haskey(pmap, hi) ? 1 : MTK.value(hi) intmap[int] = tₛ * InfiniteOpt.∫(arg, model[:t], lo, hi) end jcosts = map(c -> Symbolics.substitute(c, intmap), jcosts) @@ -262,7 +257,7 @@ function substitute_jump_vars(model, sys, pmap, exprs; auxmap = Dict(), is_free_ exprs end -is_explicit(tableau) = tableau isa DiffEqDevTools.ExplicitRKTableau +is_explicit(tableau) = tableau isa DiffEqBase.ExplicitRKTableau function add_infopt_solve_constraints!(model::InfiniteModel, sys, pmap; is_free_t = false) # Differential equations @@ -346,7 +341,7 @@ function constructDefault(T::Type = Float64) α = map(T, α) c = map(T, c) - (; A = A, α = α, c = c) + DiffEqBase.ImplicitRKTableau(A, c, α, 5) end """ @@ -362,7 +357,11 @@ function DiffEqBase.solve( prob::JuMPDynamicOptProblem, jump_solver, ode_solver::Symbol = :Default; silent = false) model = prob.model tableau_getter = Symbol(:construct, ode_solver) - @eval tableau = $tableau_getter() + if ode_solver == :Default + @eval tableau = $tableau_getter() + else + @eval tableau = Main.$tableau_getter() + end silent && set_silent(model) # Unregister current solver constraints From 58139921df17ee0e1e895d51144ec9bac9fd0268 Mon Sep 17 00:00:00 2001 From: vyudu Date: Sat, 3 May 2025 00:05:17 -0400 Subject: [PATCH 1459/2176] remove python dynamicopt problems --- src/ModelingToolkit.jl | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/ModelingToolkit.jl b/src/ModelingToolkit.jl index 694265bbc7..d2d20f902e 100644 --- a/src/ModelingToolkit.jl +++ b/src/ModelingToolkit.jl @@ -349,8 +349,7 @@ export AnalysisPoint, get_sensitivity_function, get_comp_sensitivity_function, function FMIComponent end include("systems/optimal_control_interface.jl") -export AbstractDynamicOptProblem, JuMPDynamicOptProblem, InfiniteOptDynamicOptProblem, - PyomoDynamicOptProblem, CasADiDynamicOptProblem +export AbstractDynamicOptProblem, JuMPDynamicOptProblem, InfiniteOptDynamicOptProblem export DynamicOptSolution end # module From dc0dbfa2bd24ad2ec46107cd1aaf187b8def8fd8 Mon Sep 17 00:00:00 2001 From: vyudu Date: Sat, 3 May 2025 01:16:39 -0400 Subject: [PATCH 1460/2176] use Module instead of Main --- ext/MTKInfiniteOptExt.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ext/MTKInfiniteOptExt.jl b/ext/MTKInfiniteOptExt.jl index 53b3edd9fe..bf9e5ae624 100644 --- a/ext/MTKInfiniteOptExt.jl +++ b/ext/MTKInfiniteOptExt.jl @@ -360,7 +360,7 @@ function DiffEqBase.solve( if ode_solver == :Default @eval tableau = $tableau_getter() else - @eval tableau = Main.$tableau_getter() + @eval tableau = @__MODULE__.$tableau_getter() end silent && set_silent(model) From 87736545cb7297335b6846e8f573d00d8f6d12fd Mon Sep 17 00:00:00 2001 From: vyudu Date: Sat, 3 May 2025 01:35:04 -0400 Subject: [PATCH 1461/2176] remove \int --- test/downstream/jump_control.jl | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/downstream/jump_control.jl b/test/downstream/jump_control.jl index b93cfe3bbb..19b144b98e 100644 --- a/test/downstream/jump_control.jl +++ b/test/downstream/jump_control.jl @@ -1,4 +1,5 @@ using ModelingToolkit +@show @__MODULE__ import JuMP, InfiniteOpt using DiffEqDevTools, DiffEqBase using SimpleDiffEq @@ -278,7 +279,7 @@ end rhss = -H \ Vector(C * qd + G - B * u) eqs = [D(D(x(t))) ~ rhss[1], D(D(θ(t))) ~ rhss[2]] cons = [θ(tf) ~ π, x(tf) ~ 0, D(θ(tf)) ~ 0, D(x(tf)) ~ 0] - costs = [∫(u^2)] + costs = [Symbolics.Integral(t in (0, tf))(u^2)] tspan = (0, tf) @named cartpole = ODESystem(eqs, t; costs, constraints = cons) From fe29a735ce8aef1c6df5262bfea7665f86bdf111 Mon Sep 17 00:00:00 2001 From: vyudu Date: Mon, 5 May 2025 09:53:48 -0400 Subject: [PATCH 1462/2176] fix: don't use eval --- ext/MTKInfiniteOptExt.jl | 13 ++++--------- test/downstream/jump_control.jl | 21 ++++++++++----------- 2 files changed, 14 insertions(+), 20 deletions(-) diff --git a/ext/MTKInfiniteOptExt.jl b/ext/MTKInfiniteOptExt.jl index bf9e5ae624..878e7ddb69 100644 --- a/ext/MTKInfiniteOptExt.jl +++ b/ext/MTKInfiniteOptExt.jl @@ -348,20 +348,15 @@ end Solve JuMPDynamicOptProblem. Arguments: - prob: a JumpDynamicOptProblem - jump_solver: a LP solver such as HiGHS -- ode_solver: Takes in a symbol representing the solver. Acceptable solvers may be found at https://docs.sciml.ai/DiffEqDevDocs/stable/internals/tableaus/. Note that the symbol may be different than the typical name of the solver, e.g. :Tsitouras5 rather than Tsit5. +- tableau_getter: Takes in a function to fetch a tableau. Tableau loaders look like `constructRK4` and may be found at https://docs.sciml.ai/DiffEqDevDocs/stable/internals/tableaus/. If this argument is not passed in, the solver will default to Radau second order. - silent: set the model silent (suppress model output) Returns a DynamicOptSolution, which contains both the model and the ODE solution. """ function DiffEqBase.solve( - prob::JuMPDynamicOptProblem, jump_solver, ode_solver::Symbol = :Default; silent = false) + prob::JuMPDynamicOptProblem, jump_solver, tableau_getter = constructDefault; silent = false) model = prob.model - tableau_getter = Symbol(:construct, ode_solver) - if ode_solver == :Default - @eval tableau = $tableau_getter() - else - @eval tableau = @__MODULE__.$tableau_getter() - end + tableau = tableau_getter() silent && set_silent(model) # Unregister current solver constraints @@ -379,7 +374,7 @@ function DiffEqBase.solve( end end add_jump_solve_constraints!(prob, tableau; is_free_t = haskey(model, :tf)) - _solve(prob, jump_solver, ode_solver) + _solve(prob, jump_solver, tableau_getter) end """ diff --git a/test/downstream/jump_control.jl b/test/downstream/jump_control.jl index 19b144b98e..a93146be10 100644 --- a/test/downstream/jump_control.jl +++ b/test/downstream/jump_control.jl @@ -1,5 +1,4 @@ using ModelingToolkit -@show @__MODULE__ import JuMP, InfiniteOpt using DiffEqDevTools, DiffEqBase using SimpleDiffEq @@ -26,13 +25,13 @@ const M = ModelingToolkit # Test explicit method. jprob = JuMPDynamicOptProblem(sys, u0map, tspan, parammap, dt = 0.01) @test JuMP.num_constraints(jprob.model) == 2 # initials - jsol = solve(jprob, Ipopt.Optimizer, :RK4, silent = true) + jsol = solve(jprob, Ipopt.Optimizer, constructRK4, silent = true) oprob = ODEProblem(sys, u0map, tspan, parammap) osol = solve(oprob, SimpleRK4(), dt = 0.01) @test jsol.sol.u ≈ osol.u # Implicit method. - jsol2 = solve(jprob, Ipopt.Optimizer, :ImplicitEuler, silent = true) # 63.031 ms, 26.49 MiB + jsol2 = solve(jprob, Ipopt.Optimizer, constructImplicitEuler, silent = true) # 63.031 ms, 26.49 MiB osol2 = solve(oprob, ImplicitEuler(), dt = 0.01, adaptive = false) # 129.375 μs, 61.91 KiB @test ≈(jsol2.sol.u, osol2.u, rtol = 0.001) iprob = InfiniteOptDynamicOptProblem(sys, u0map, tspan, parammap, dt = 0.01) @@ -49,7 +48,7 @@ const M = ModelingToolkit jprob = JuMPDynamicOptProblem(lksys, u0map, tspan, parammap; guesses = guess, dt = 0.01) @test JuMP.num_constraints(jprob.model) == 2 - jsol = solve(jprob, Ipopt.Optimizer, :Tsitouras5, silent = true) # 12.190 s, 9.68 GiB + jsol = solve(jprob, Ipopt.Optimizer, constructTsitouras5, silent = true) # 12.190 s, 9.68 GiB @test jsol.sol(0.6)[1] ≈ 3.5 @test jsol.sol(0.3)[1] ≈ 7.0 @@ -71,7 +70,7 @@ const M = ModelingToolkit @test all(u -> u > [1, 1], isol.sol.u) jprob = JuMPDynamicOptProblem(lksys, u0map, tspan, parammap; guesses = guess, dt = 0.01) - jsol = solve(jprob, Ipopt.Optimizer, :RadauIA3, silent = true) # 12.190 s, 9.68 GiB + jsol = solve(jprob, Ipopt.Optimizer, constructRadauIA3, silent = true) # 12.190 s, 9.68 GiB @test all(u -> u > [1, 1], jsol.sol.u) end @@ -105,7 +104,7 @@ end tspan = (0.0, 1.0) parammap = [u(t) => 0.0] jprob = JuMPDynamicOptProblem(block, u0map, tspan, parammap; dt = 0.01) - jsol = solve(jprob, Ipopt.Optimizer, :Verner8, silent = true) + jsol = solve(jprob, Ipopt.Optimizer, constructVerner8, silent = true) # Linear systems have bang-bang controls @test is_bangbang(jsol.input_sol, [-1.0], [1.0]) # Test reached final position. @@ -143,7 +142,7 @@ end pmap = [b => 1, c => 1, μ => 1, s => 1, ν => 1, α => 1] jprob = JuMPDynamicOptProblem(beesys, u0map, tspan, pmap, dt = 0.01) - jsol = solve(jprob, Ipopt.Optimizer, :Tsitouras5, silent = true) + jsol = solve(jprob, Ipopt.Optimizer, constructTsitouras5, silent = true) @test is_bangbang(jsol.input_sol, [0.0], [1.0]) iprob = InfiniteOptDynamicOptProblem(beesys, u0map, tspan, pmap, dt = 0.01) isol = solve(iprob, Ipopt.Optimizer; silent = true) @@ -188,7 +187,7 @@ end g₀ => 1, m₀ => 1.0, h_c => 500, c => 0.5 * √(g₀ * h₀), D_c => 0.5 * 620 * m₀ / g₀, Tₘ => 3.5 * g₀ * m₀, T(t) => 0.0, h₀ => 1, m_c => 0.6] jprob = JuMPDynamicOptProblem(rocket, u0map, (ts, te), pmap; dt = 0.001, cse = false) - jsol = solve(jprob, Ipopt.Optimizer, :RadauIIA5, silent = true) + jsol = solve(jprob, Ipopt.Optimizer, constructRadauIIA5, silent = true) @test jsol.sol.u[end][1] > 1.012 iprob = InfiniteOptDynamicOptProblem( @@ -229,7 +228,7 @@ end u0map = [x(t) => 17.5] pmap = [u(t) => 0.0, tf => 8] jprob = JuMPDynamicOptProblem(rocket, u0map, (0, tf), pmap; steps = 201) - jsol = solve(jprob, Ipopt.Optimizer, :Tsitouras5, silent = true) + jsol = solve(jprob, Ipopt.Optimizer, constructTsitouras5, silent = true) @test isapprox(jsol.sol.t[end], 10.0, rtol = 1e-3) iprob = InfiniteOptDynamicOptProblem(rocket, u0map, (0, tf), pmap; steps = 200) @@ -250,7 +249,7 @@ end tspan = (0.0, tf) parammap = [u(t) => 0.0, tf => 1.0] jprob = JuMPDynamicOptProblem(block, u0map, tspan, parammap; steps = 51) - jsol = solve(jprob, Ipopt.Optimizer, :Verner8, silent = true) + jsol = solve(jprob, Ipopt.Optimizer, constructVerner8, silent = true) @test isapprox(jsol.sol.t[end], 2.0, atol = 1e-5) iprob = InfiniteOptDynamicOptProblem(block, u0map, tspan, parammap; steps = 51) @@ -288,7 +287,7 @@ end u0map = [D(x(t)) => 0.0, D(θ(t)) => 0.0, θ(t) => 0.0, x(t) => 0.0] pmap = [mₖ => 1.0, mₚ => 0.2, l => 0.5, g => 9.81, u => 0] jprob = JuMPDynamicOptProblem(cartpole, u0map, tspan, pmap; dt = 0.04) - jsol = solve(jprob, Ipopt.Optimizer, :RK4, silent = true) + jsol = solve(jprob, Ipopt.Optimizer, constructRK4, silent = true) @test jsol.sol.u[end] ≈ [π, 0, 0, 0] iprob = InfiniteOptDynamicOptProblem(cartpole, u0map, tspan, pmap; dt = 0.04) From c7869e65ba4aabdd11d39c88d6825c945c7c774a Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 5 May 2025 22:09:16 +0530 Subject: [PATCH 1463/2176] fix: allow solving initialization separately and solving problem with `CheckInit` --- src/systems/problem_utils.jl | 32 +++++++++++++++++++------------- test/initializationsystem.jl | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 13 deletions(-) diff --git a/src/systems/problem_utils.jl b/src/systems/problem_utils.jl index 47bf3c678d..4cba787852 100644 --- a/src/systems/problem_utils.jl +++ b/src/systems/problem_utils.jl @@ -630,13 +630,17 @@ with promoted types. $(TYPEDFIELDS) """ -struct ReconstructInitializeprob{G} +struct ReconstructInitializeprob{GP, GU} """ A function which when given the original problem and initialization problem, returns the parameter object of the initialization problem with values copied from the original. """ - getter::G + pgetter::GP + """ + Given the original problem, return the `u0` of the initialization problem. + """ + ugetter::GU end """ @@ -674,6 +678,7 @@ with values from `srcsys`. function ReconstructInitializeprob( srcsys::AbstractSystem, dstsys::AbstractSystem) @assert is_initializesystem(dstsys) + ugetter = getu(srcsys, unknowns(dstsys)) if is_split(dstsys) # if we call `getu` on this (and it were able to handle empty tuples) we get the # fields of `MTKParameters` except caches. @@ -693,7 +698,7 @@ function ReconstructInitializeprob( end end getters = (tunable_getter, Returns(SizedVector{0, Float64}()), rest_getters...) - getter = let getters = getters + pgetter = let getters = getters function _getter(valp, initprob) oldcache = parameter_values(initprob).caches MTKParameters(getters[1](valp), getters[2](valp), getters[3](valp), @@ -703,13 +708,13 @@ function ReconstructInitializeprob( end else syms = parameters(dstsys) - getter = let inner = concrete_getu(srcsys, syms) + pgetter = let inner = concrete_getu(srcsys, syms) function _getter2(valp, initprob) inner(valp) end end end - return ReconstructInitializeprob(getter) + return ReconstructInitializeprob(pgetter, ugetter) end """ @@ -719,7 +724,7 @@ Copy values from `srcvalp` to `dstvalp`. Returns the new `u0` and `p`. """ function (rip::ReconstructInitializeprob)(srcvalp, dstvalp) # copy parameters - newp = rip.getter(srcvalp, dstvalp) + newp = rip.pgetter(srcvalp, dstvalp) # no `u0`, so no type-promotion if state_values(dstvalp) === nothing return nothing, newp @@ -735,11 +740,10 @@ function (rip::ReconstructInitializeprob)(srcvalp, dstvalp) elseif !isempty(newp) T = promote_type(eltype(newp), T) end + u0 = rip.ugetter(srcvalp) # and the eltype of the destination u0 - if T == eltype(state_values(dstvalp)) - u0 = state_values(dstvalp) - elseif T != Union{} - u0 = T.(state_values(dstvalp)) + if T != eltype(u0) && T != Union{} + u0 = T.(u0) end # apply the promotion to tunables portion buf, repack, alias = SciMLStructures.canonicalize(SciMLStructures.Tunable(), newp) @@ -911,11 +915,13 @@ function maybe_build_initialization_problem( punknowns = [p for p in all_variable_symbols(initializeprob) if is_parameter(sys, p)] - if isempty(punknowns) + if initializeprobmap === nothing && isempty(punknowns) initializeprobpmap = nothing else - getpunknowns = getu(initializeprob, punknowns) - setpunknowns = setp(sys, punknowns) + allsyms = all_symbols(initializeprob) + initdvs = filter(x -> any(isequal(x), allsyms), unknowns(sys)) + getpunknowns = getu(initializeprob, [punknowns; initdvs]) + setpunknowns = setp(sys, [punknowns; Initial.(initdvs)]) initializeprobpmap = GetUpdatedMTKParameters(getpunknowns, setpunknowns) end diff --git a/test/initializationsystem.jl b/test/initializationsystem.jl index 5c36fcba3e..13d609d2c4 100644 --- a/test/initializationsystem.jl +++ b/test/initializationsystem.jl @@ -1563,3 +1563,38 @@ end @test integ[x] ≈ 0.8 end end + +@testset "Initialization copies solved `u0` to `p`" begin + @parameters σ ρ β A[1:3] + @variables x(t) y(t) z(t) w(t) w2(t) + eqs = [D(D(x)) ~ σ * (y - x), + D(y) ~ x * (ρ - z) - y, + D(z) ~ x * y - β * z, + w ~ x + y + z + 2 * β, + 0 ~ x^2 + y^2 - w2^2 + ] + + @mtkbuild sys = ODESystem(eqs, t) + + u0 = [D(x) => 2.0, + x => 1.0, + y => 0.0, + z => 0.0] + + p = [σ => 28.0, + ρ => 10.0, + β => 8 / 3] + + tspan = (0.0, 100.0) + getter = getsym(sys, Initial.(unknowns(sys))) + prob = ODEProblem(sys, u0, tspan, p; guesses = [w2 => 3.0]) + new_u0, new_p, _ = SciMLBase.get_initial_values( + prob, prob, prob.f, SciMLBase.OverrideInit(), Val(true); + nlsolve_alg = NewtonRaphson(), abstol = 1e-6, reltol = 1e-3) + @test getter(prob) != getter(new_p) + @test getter(new_p) == new_u0 + _prob = remake(prob, u0 = new_u0, p = new_p) + sol = solve(_prob; initializealg = CheckInit()) + @test SciMLBase.successful_retcode(sol) + @test sol.u[1] ≈ new_u0 +end From 1c254264024698e96368c591d5ac3f4b8fba4dcc Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 5 May 2025 22:09:21 +0530 Subject: [PATCH 1464/2176] refactor: format --- test/basic_transformations.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/basic_transformations.jl b/test/basic_transformations.jl index 183feca6d2..b593deb345 100644 --- a/test/basic_transformations.jl +++ b/test/basic_transformations.jl @@ -220,7 +220,7 @@ end @independent_variables t_units [unit = u"s"] D_units = Differential(t_units) @variables x(t_units) [unit = u"m"] y(t_units) [unit = u"m"] - @parameters g = 9.81 [unit = u"m * s^-2"] # gravitational acceleration + @parameters g=9.81 [unit = u"m * s^-2"] # gravitational acceleration Mt = ODESystem([D_units(D_units(y)) ~ -g, D_units(D_units(x)) ~ 0], t_units; name = :M) # gives (x, y) as function of t, ... Mx = change_independent_variable(Mt, x; add_old_diff = true) # ... but we want y as a function of x Mx = structural_simplify(Mx; allow_symbolic = true) From 31ba02529739ab20f2c59ec77b3b967d91b2e85e Mon Sep 17 00:00:00 2001 From: vyudu Date: Mon, 5 May 2025 13:03:49 -0400 Subject: [PATCH 1465/2176] mark tests as broken for now --- src/systems/diffeqs/abstractodesystem.jl | 1 + test/{optimal_control.jl => bvproblem.jl} | 19 +++++++++---------- 2 files changed, 10 insertions(+), 10 deletions(-) rename test/{optimal_control.jl => bvproblem.jl} (95%) diff --git a/src/systems/diffeqs/abstractodesystem.jl b/src/systems/diffeqs/abstractodesystem.jl index d9e8b05eeb..21a8f6788c 100644 --- a/src/systems/diffeqs/abstractodesystem.jl +++ b/src/systems/diffeqs/abstractodesystem.jl @@ -982,6 +982,7 @@ function generate_function_bc(sys::ODESystem, u0, u0_idxs, tspan; kwargs...) exprs = vcat(init_conds, cons) _p = reorder_parameters(sys, ps) + @show exprs build_function_wrapper(sys, exprs, sol, _p..., iv; output_type = Array, kwargs...) end diff --git a/test/optimal_control.jl b/test/bvproblem.jl similarity index 95% rename from test/optimal_control.jl rename to test/bvproblem.jl index f32ba471d8..a85f7ca522 100644 --- a/test/optimal_control.jl +++ b/test/bvproblem.jl @@ -1,7 +1,6 @@ ### TODO: update when BoundaryValueDiffEqAscher is updated to use the normal boundary condition conventions using OrdinaryDiffEq using BoundaryValueDiffEqMIRK, BoundaryValueDiffEqAscher -using BenchmarkTools using ModelingToolkit using SciMLBase using ModelingToolkit: t_nounits as t, D_nounits as D @@ -30,8 +29,8 @@ daesolvers = [Ascher2, Ascher4, Ascher6] for solver in solvers sol = solve(bvp, solver(), dt = 0.01) - @test isapprox(sol.u[end], osol.u[end]; atol = 0.01) - @test sol.u[1] == [1.0, 2.0] + @test_broken isapprox(sol.u[end], osol.u[end]; atol = 0.01) + @test_broken sol.u[1] == [1.0, 2.0] end # Test out of place @@ -40,8 +39,8 @@ daesolvers = [Ascher2, Ascher4, Ascher6] for solver in solvers sol = solve(bvp2, solver(), dt = 0.01) - @test isapprox(sol.u[end], osol.u[end]; atol = 0.01) - @test sol.u[1] == [1.0, 2.0] + @test_broken isapprox(sol.u[end], osol.u[end]; atol = 0.01) + @test_broken sol.u[1] == [1.0, 2.0] end end @@ -125,10 +124,10 @@ end bvpi4 = SciMLBase.BVProblem{false, SciMLBase.FullSpecialize}( lksys, [x(t) => 1.0], tspan; guesses = [y(t) => 1.0]) - sol1 = @btime solve($bvpi1, MIRK4(), dt = 0.01) - sol2 = @btime solve($bvpi2, MIRK4(), dt = 0.01) - sol3 = @btime solve($bvpi3, MIRK4(), dt = 0.01) - sol4 = @btime solve($bvpi4, MIRK4(), dt = 0.01) + sol1 = solve(bvpi1, MIRK4(), dt = 0.01) + sol2 = solve(bvpi2, MIRK4(), dt = 0.01) + sol3 = solve(bvpi3, MIRK4(), dt = 0.01) + sol4 = solve(bvpi4, MIRK4(), dt = 0.01) @test sol1 ≈ sol2 ≈ sol3 ≈ sol4 # don't get true equality here, not sure why end @@ -136,7 +135,7 @@ function test_solvers( solvers, prob, u0map, constraints, equations = []; dt = 0.05, atol = 1e-2) for solver in solvers println("Solver: $solver") - sol = @btime solve($prob, $solver(), dt = $dt, abstol = $atol) + sol = solve(prob, solver(), dt = dt, abstol = atol) @test SciMLBase.successful_retcode(sol.retcode) p = prob.p t = sol.t From dd22c66360aea7e098c6c409122b59d6360db302 Mon Sep 17 00:00:00 2001 From: vyudu Date: Mon, 5 May 2025 13:05:05 -0400 Subject: [PATCH 1466/2176] remove show --- src/systems/diffeqs/abstractodesystem.jl | 1 - 1 file changed, 1 deletion(-) diff --git a/src/systems/diffeqs/abstractodesystem.jl b/src/systems/diffeqs/abstractodesystem.jl index 21a8f6788c..d9e8b05eeb 100644 --- a/src/systems/diffeqs/abstractodesystem.jl +++ b/src/systems/diffeqs/abstractodesystem.jl @@ -982,7 +982,6 @@ function generate_function_bc(sys::ODESystem, u0, u0_idxs, tspan; kwargs...) exprs = vcat(init_conds, cons) _p = reorder_parameters(sys, ps) - @show exprs build_function_wrapper(sys, exprs, sol, _p..., iv; output_type = Array, kwargs...) end From 23ef1865fa3fd21b2ff5fb7edf40e59db9922589 Mon Sep 17 00:00:00 2001 From: Christopher Rackauckas Date: Mon, 5 May 2025 16:39:00 -0400 Subject: [PATCH 1467/2176] Update initializationsystem.jl --- test/initializationsystem.jl | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/initializationsystem.jl b/test/initializationsystem.jl index 13d609d2c4..3f26db444a 100644 --- a/test/initializationsystem.jl +++ b/test/initializationsystem.jl @@ -1194,11 +1194,11 @@ end @test integ[x] ≈ 1 / cbrt(3) @test integ[y] ≈ 2 / cbrt(3) @test integ.ps[p] == 1.0 - @test integ.ps[q] ≈ 3 / cbrt(3) + @test integ.ps[q] ≈ 3 / cbrt(3) atol=1e-5 prob2 = remake(prob; u0 = [y => 3x], p = [q => 2x]) integ2 = init(prob2) - @test integ2[x] ≈ cbrt(3 / 28) - @test integ2[y] ≈ 3cbrt(3 / 28) + @test integ2[x] ≈ cbrt(3 / 28) atol=1e-5 + @test integ2[y] ≈ 3cbrt(3 / 28) atol=1e-5 @test integ2.ps[p] == 1.0 @test integ2.ps[q] ≈ 2cbrt(3 / 28) end From 428a1aee23c7208dc46a1fb8c3401b3a6097bfa2 Mon Sep 17 00:00:00 2001 From: Christopher Rackauckas Date: Mon, 5 May 2025 20:42:54 -0400 Subject: [PATCH 1468/2176] Update runtests.jl for bvproblem change Didn't see the rename... --- test/runtests.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/runtests.jl b/test/runtests.jl index a09aca2fc7..c4fee4110d 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -90,7 +90,7 @@ end @safetestset "SCCNonlinearProblem Test" include("scc_nonlinear_problem.jl") @safetestset "PDE Construction Test" include("pdesystem.jl") @safetestset "JumpSystem Test" include("jumpsystem.jl") - @safetestset "Optimal Control + Constraints Tests" include("optimal_control.jl") + @safetestset "Optimal Control + Constraints Tests" include("bvproblem.jl") @safetestset "print_tree" include("print_tree.jl") @safetestset "Constraints Test" include("constraints.jl") @safetestset "IfLifting Test" include("if_lifting.jl") From 78431af522a9e6d0ffe57e86793cf24304c5f912 Mon Sep 17 00:00:00 2001 From: Benjamin Chung Date: Mon, 5 May 2025 21:57:17 -0700 Subject: [PATCH 1469/2176] Add tests --- test/symbolic_events.jl | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/test/symbolic_events.jl b/test/symbolic_events.jl index 3fb66081a2..5c0a2ee7fc 100644 --- a/test/symbolic_events.jl +++ b/test/symbolic_events.jl @@ -1434,3 +1434,30 @@ end sol2 = solve(ODEProblem(sys2, [], (0.0, 1.0)), Tsit5()) @test 100.0 ∈ sol2[sys2.wd2.θ] end + +@testset "Array parameter updates of parent components in ImperativeEffect" begin + function child(vals; name, max_time = 0.1) + vars = @variables begin + x(t) = 0.0 + end + eqs = reduce(vcat, Symbolics.scalarize.([ + D(x) ~ 1.0 + ])) + reset = ModelingToolkit.ImperativeAffect( + modified = (; vals = Symbolics.scalarize(ParentScope.(vals)), x)) do m, o, _, i + @set! m.vals = m.vals .+ 1 + @set! m.x = 0.0 + return m + end + return ODESystem(eqs, t, vars, []; name = name, + continuous_events = [[x ~ max_time] => reset]) + end + shared_pars = @parameters begin + vals(t)[1:2] = 0.0 + end + + @named sys = ODESystem(Equation[], t, [], Symbolics.scalarize(vals); + systems = [child(vals; name = :child)]) + sys = structural_simplify(sys) + sol = solve(ODEProblem(sys, [], (0.0, 1.0)), Tsit5()) +end From 0c56d86961405ee91c57cd20e5550c5c58f2c8b8 Mon Sep 17 00:00:00 2001 From: Christopher Rackauckas Date: Tue, 6 May 2025 01:13:31 -0400 Subject: [PATCH 1470/2176] Update test/initializationsystem.jl --- test/initializationsystem.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/initializationsystem.jl b/test/initializationsystem.jl index 3f26db444a..4274f31cc7 100644 --- a/test/initializationsystem.jl +++ b/test/initializationsystem.jl @@ -1200,7 +1200,7 @@ end @test integ2[x] ≈ cbrt(3 / 28) atol=1e-5 @test integ2[y] ≈ 3cbrt(3 / 28) atol=1e-5 @test integ2.ps[p] == 1.0 - @test integ2.ps[q] ≈ 2cbrt(3 / 28) + @test integ2.ps[q] ≈ 2cbrt(3 / 28) atol=1e-5 end function test_dummy_initialization_equation(prob, var) From a44250bd33d6448e2e45f97033f3728cfc342820 Mon Sep 17 00:00:00 2001 From: Christopher Rackauckas Date: Tue, 6 May 2025 01:14:02 -0400 Subject: [PATCH 1471/2176] Update Project.toml --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 1de504cbae..266dee2ae6 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "ModelingToolkit" uuid = "961ee093-0014-501f-94e3-6117800e7a78" authors = ["Yingbo Ma ", "Chris Rackauckas and contributors"] -version = "9.76.0" +version = "9.77.0" [deps] ADTypes = "47edcb42-4c32-4615-8424-f2b9edc5f35b" From bfd996ddb80faf06dfe29529547bdcb872ca9d16 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Legat?= Date: Tue, 6 May 2025 12:13:33 +0200 Subject: [PATCH 1472/2176] Add show method without MIME type for ODESystem --- src/systems/diffeqs/odesystem.jl | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/systems/diffeqs/odesystem.jl b/src/systems/diffeqs/odesystem.jl index 4a13d7ccf1..229a2b8400 100644 --- a/src/systems/diffeqs/odesystem.jl +++ b/src/systems/diffeqs/odesystem.jl @@ -739,6 +739,8 @@ function add_accumulations(sys::ODESystem, vars::Vector{<:Pair}) @set! sys.defaults = merge(get_defaults(sys), Dict(a => 0.0 for a in avars)) end +Base.show(io::IO, sys::ODESystem; kws...) = show(io, MIME"text/plain"(), sys; kws...) + function Base.show(io::IO, mime::MIME"text/plain", sys::ODESystem; hint = true, bold = true) # Print general AbstractSystem information invoke(Base.show, Tuple{typeof(io), typeof(mime), AbstractSystem}, From 2bc8879544bf6e7cbb751d7391ad72c82ef43821 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Legat?= Date: Tue, 6 May 2025 12:22:27 +0200 Subject: [PATCH 1473/2176] Do it for AbstractSystem --- src/systems/abstractsystem.jl | 2 ++ src/systems/diffeqs/odesystem.jl | 2 -- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index ce70a33fe2..6c00806b7d 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -2116,6 +2116,8 @@ function n_expanded_connection_equations(sys::AbstractSystem) nextras = n_outer_stream_variables + length(ceqs) + n_variable_connect_eqs end +Base.show(io::IO, sys::AbstractSystem; kws...) = show(io, MIME"text/plain"(), sys; kws...) + function Base.show( io::IO, mime::MIME"text/plain", sys::AbstractSystem; hint = true, bold = true) limit = get(io, :limit, false) # if output should be limited, diff --git a/src/systems/diffeqs/odesystem.jl b/src/systems/diffeqs/odesystem.jl index 229a2b8400..4a13d7ccf1 100644 --- a/src/systems/diffeqs/odesystem.jl +++ b/src/systems/diffeqs/odesystem.jl @@ -739,8 +739,6 @@ function add_accumulations(sys::ODESystem, vars::Vector{<:Pair}) @set! sys.defaults = merge(get_defaults(sys), Dict(a => 0.0 for a in avars)) end -Base.show(io::IO, sys::ODESystem; kws...) = show(io, MIME"text/plain"(), sys; kws...) - function Base.show(io::IO, mime::MIME"text/plain", sys::ODESystem; hint = true, bold = true) # Print general AbstractSystem information invoke(Base.show, Tuple{typeof(io), typeof(mime), AbstractSystem}, From 9ff909828d3c9abd1c83f5d50b0f171e601cd290 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 6 May 2025 20:04:00 +0530 Subject: [PATCH 1474/2176] feat: allow shifting array and scalarized variables --- src/discretedomain.jl | 54 ++++++++++++++++--- src/structural_transformation/utils.jl | 7 +++ .../discrete_system/discrete_system.jl | 1 + src/utils.jl | 3 ++ test/discrete_system.jl | 18 +++++++ 5 files changed, 76 insertions(+), 7 deletions(-) diff --git a/src/discretedomain.jl b/src/discretedomain.jl index 7260237053..2d5410ee79 100644 --- a/src/discretedomain.jl +++ b/src/discretedomain.jl @@ -39,9 +39,13 @@ SymbolicUtils.isbinop(::Shift) = false function (D::Shift)(x, allow_zero = false) !allow_zero && D.steps == 0 && return x - Term{symtype(x)}(D, Any[x]) + if Symbolics.isarraysymbolic(x) + Symbolics.array_term(D, x) + else + term(D, x) + end end -function (D::Shift)(x::Num, allow_zero = false) +function (D::Shift)(x::Union{Num, Symbolics.Arr}, allow_zero = false) !allow_zero && D.steps == 0 && return x vt = value(x) if iscall(vt) @@ -52,11 +56,11 @@ function (D::Shift)(x::Num, allow_zero = false) if D.t === nothing || isequal(D.t, op.t) arg = arguments(vt)[1] newsteps = D.steps + op.steps - return Num(newsteps == 0 ? arg : Shift(D.t, newsteps)(arg)) + return wrap(newsteps == 0 ? arg : Shift(D.t, newsteps)(arg)) end end end - Num(D(vt, allow_zero)) + wrap(D(vt, allow_zero)) end SymbolicUtils.promote_symtype(::Shift, t) = t @@ -202,11 +206,19 @@ function (xn::Num)(k::ShiftIndex) x = value(xn) # Verify that the independent variables of k and x match and that the expression doesn't have multiple variables vars = Symbolics.get_variables(x) - length(vars) == 1 || + if length(vars) != 1 error("Cannot shift a multivariate expression $x. Either create a new unknown and shift this, or shift the individual variables in the expression.") - args = Symbolics.arguments(vars[]) # args should be one element vector with the t in x(t) - length(args) == 1 || + end + var = only(vars) + if !iscall(var) + throw(ArgumentError("Cannot shift time-independent variable $var")) + end + if operation(var) == getindex + var = first(arguments(var)) + end + if length(arguments(var)) != 1 error("Cannot shift an expression with multiple independent variables $x.") + end # d, _ = propagate_time_domain(xn) # if d != clock # this is only required if the variable has another clock @@ -220,6 +232,34 @@ function (xn::Num)(k::ShiftIndex) Shift(t, steps)(xn) # a shift of k steps end +function (xn::Symbolics.Arr)(k::ShiftIndex) + @unpack clock, steps = k + x = value(xn) + # Verify that the independent variables of k and x match and that the expression doesn't have multiple variables + vars = ModelingToolkit.vars(x) + if length(vars) != 1 + error("Cannot shift a multivariate expression $x. Either create a new unknown and shift this, or shift the individual variables in the expression.") + end + var = only(vars) + if !iscall(var) + throw(ArgumentError("Cannot shift time-independent variable $var")) + end + if length(arguments(var)) != 1 + error("Cannot shift an expression with multiple independent variables $x.") + end + + # d, _ = propagate_time_domain(xn) + # if d != clock # this is only required if the variable has another clock + # xn = Sample(t, clock)(xn) + # end + # QUESTION: should we return a variable with time domain set to k.clock? + xn = wrap(setmetadata(unwrap(xn), VariableTimeDomain, k.clock)) + if steps == 0 + return xn # x(k) needs no shift operator if the step of k is 0 + end + Shift(t, steps)(xn) # a shift of k steps +end + Base.:+(k::ShiftIndex, i::Int) = ShiftIndex(k.clock, k.steps + i) Base.:-(k::ShiftIndex, i::Int) = k + (-i) diff --git a/src/structural_transformation/utils.jl b/src/structural_transformation/utils.jl index 14628f2958..fa02f0b3cd 100644 --- a/src/structural_transformation/utils.jl +++ b/src/structural_transformation/utils.jl @@ -471,6 +471,13 @@ function shift2term(var) op = operation(var) iv = op.t arg = only(arguments(var)) + if operation(arg) === getindex + idxs = arguments(arg)[2:end] + newvar = shift2term(op(first(arguments(arg))))[idxs...] + unshifted = ModelingToolkit.getunshifted(newvar)[idxs...] + newvar = setmetadata(newvar, ModelingToolkit.VariableUnshifted, unshifted) + return newvar + end is_lowered = !isnothing(ModelingToolkit.getunshifted(arg)) backshift = is_lowered ? op.steps + ModelingToolkit.getshift(arg) : op.steps diff --git a/src/systems/discrete_system/discrete_system.jl b/src/systems/discrete_system/discrete_system.jl index 075aa27e4d..64c0c2c8e0 100644 --- a/src/systems/discrete_system/discrete_system.jl +++ b/src/systems/discrete_system/discrete_system.jl @@ -319,6 +319,7 @@ function SciMLBase.DiscreteProblem( iv = get_iv(sys) u0map = to_varmap(u0map, dvs) + scalarize_varmap!(u0map) u0map = shift_u0map_forward(sys, u0map, defaults(sys)) f, u0, p = process_SciMLProblem( DiscreteFunction, sys, u0map, parammap; eval_expression, eval_module, build_initializeprob = false) diff --git a/src/utils.jl b/src/utils.jl index 1884a91c19..d0fd3d40f9 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -130,6 +130,9 @@ function check_parameters(ps, iv) end function is_delay_var(iv, var) + if Symbolics.isarraysymbolic(var) + return is_delay_var(iv, first(collect(var))) + end args = nothing try args = arguments(var) diff --git a/test/discrete_system.jl b/test/discrete_system.jl index b0e2481e56..3897d17984 100644 --- a/test/discrete_system.jl +++ b/test/discrete_system.jl @@ -325,3 +325,21 @@ end @test isequal(shift2term(Shift(t, -1)(vars[4])), vars[5]) @test isequal(shift2term(Shift(t, -2)(vars[1])), vars[3]) end + +@testset "Shifted array variables" begin + @variables x(t)[1:2] y(t)[1:2] + k = ShiftIndex(t) + eqs = [ + x(k) ~ x(k - 1) + x(k - 2), + y[1](k) ~ y[1](k - 1) + y[1](k - 2), + y[2](k) ~ y[2](k - 1) + y[2](k - 2) + ] + @mtkbuild sys = DiscreteSystem(eqs, t) + prob = DiscreteProblem(sys, + [x(k - 1) => ones(2), x(k - 2) => zeros(2), y[1](k - 1) => 1.0, + y[1](k - 2) => 0.0, y[2](k - 1) => 1.0, y[2](k - 2) => 0.0], + (0, 4)) + @test all(isone, prob.u0) + sol = solve(prob, FunctionMap()) + @test sol[[x..., y...], end] == 8ones(4) +end From 5408efb3923a2f2d244596ff5527b4c370393d7e Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 6 May 2025 20:04:06 +0530 Subject: [PATCH 1475/2176] refactor: format --- ext/MTKInfiniteOptExt.jl | 3 +-- test/initializationsystem.jl | 8 ++++---- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/ext/MTKInfiniteOptExt.jl b/ext/MTKInfiniteOptExt.jl index 878e7ddb69..ba3a772582 100644 --- a/ext/MTKInfiniteOptExt.jl +++ b/ext/MTKInfiniteOptExt.jl @@ -340,7 +340,7 @@ function constructDefault(T::Type = Float64) A = map(T, A) α = map(T, α) c = map(T, c) - + DiffEqBase.ImplicitRKTableau(A, c, α, 5) end @@ -422,7 +422,6 @@ function _solve(prob::AbstractDynamicOptProblem, jump_solver, solver) DynamicOptSolution(model, sol, input_sol) end - import InfiniteOpt: JuMP, GeneralVariableRef for ff in [acos, log1p, acosh, log2, asin, tan, atanh, cos, log, sin, log10, sqrt] diff --git a/test/initializationsystem.jl b/test/initializationsystem.jl index 4274f31cc7..a9f3b53084 100644 --- a/test/initializationsystem.jl +++ b/test/initializationsystem.jl @@ -1194,13 +1194,13 @@ end @test integ[x] ≈ 1 / cbrt(3) @test integ[y] ≈ 2 / cbrt(3) @test integ.ps[p] == 1.0 - @test integ.ps[q] ≈ 3 / cbrt(3) atol=1e-5 + @test integ.ps[q]≈3 / cbrt(3) atol=1e-5 prob2 = remake(prob; u0 = [y => 3x], p = [q => 2x]) integ2 = init(prob2) - @test integ2[x] ≈ cbrt(3 / 28) atol=1e-5 - @test integ2[y] ≈ 3cbrt(3 / 28) atol=1e-5 + @test integ2[x]≈cbrt(3 / 28) atol=1e-5 + @test integ2[y]≈3cbrt(3 / 28) atol=1e-5 @test integ2.ps[p] == 1.0 - @test integ2.ps[q] ≈ 2cbrt(3 / 28) atol=1e-5 + @test integ2.ps[q]≈2cbrt(3 / 28) atol=1e-5 end function test_dummy_initialization_equation(prob, var) From 5b216097dbb1a5399da5fe40be799bc338556a73 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 6 May 2025 20:35:01 +0530 Subject: [PATCH 1476/2176] feat: use guesses as temporary values for variables solved by initialization --- src/systems/problem_utils.jl | 5 ++--- test/initial_values.jl | 28 ++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/src/systems/problem_utils.jl b/src/systems/problem_utils.jl index 4cba787852..4525b0e46b 100644 --- a/src/systems/problem_utils.jl +++ b/src/systems/problem_utils.jl @@ -938,8 +938,7 @@ function maybe_build_initialization_problem( is_parameter_solvable(p, pmap, defs, guesses) || continue get(op, p, missing) === missing || continue p = unwrap(p) - stype = symtype(p) - op[p] = get_temporary_value(p, floatT) + op[p] = getu(initializeprob, p)(initializeprob) if iscall(p) && operation(p) === getindex arrp = arguments(p)[1] op[arrp] = collect(arrp) @@ -948,7 +947,7 @@ function maybe_build_initialization_problem( if is_time_dependent(sys) for v in missing_unknowns - op[v] = get_temporary_value(v, floatT) + op[v] = getu(initializeprob, v)(initializeprob) end empty!(missing_unknowns) end diff --git a/test/initial_values.jl b/test/initial_values.jl index 79b6b8e067..b3614de0f4 100644 --- a/test/initial_values.jl +++ b/test/initial_values.jl @@ -281,3 +281,31 @@ end @test prob.p isa Vector{Float64} @test length(prob.p) == 5 end + +@testset "Temporary values for solved variables are guesses" begin + @parameters σ ρ β=missing [guess = 8 / 3] + @variables x(t) y(t) z(t) w(t) w2(t) + + eqs = [D(D(x)) ~ σ * (y - x), + D(y) ~ x * (ρ - z) - y, + D(z) ~ x * y - β * z, + w ~ x + y + z + 2 * β, + 0 ~ x^2 + y^2 - w2^2 + ] + + @mtkbuild sys = ODESystem(eqs, t) + + u0 = [D(x) => 2.0, + x => 1.0, + y => 0.0, + z => 0.0] + + p = [σ => 28.0, + ρ => 10.0] + + tspan = (0.0, 100.0) + prob = ODEProblem(sys, u0, tspan, p, jac = true, guesses = [w2 => -1.0], + warn_initialize_determined = false) + @test prob[w2] ≈ -1.0 + @test prob.ps[β] ≈ 8 / 3 +end From dc1e1c35f3f0811e4c4973af802d2f3e3f930607 Mon Sep 17 00:00:00 2001 From: Benjamin Chung Date: Tue, 6 May 2025 11:40:49 -0700 Subject: [PATCH 1477/2176] Update imperative affect namespacing to not recurse into symbolic arrays Co-authored-by: Aayush Sabharwal --- src/systems/imperative_affect.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/systems/imperative_affect.jl b/src/systems/imperative_affect.jl index b0742e70a7..9be9536c93 100644 --- a/src/systems/imperative_affect.jl +++ b/src/systems/imperative_affect.jl @@ -103,7 +103,7 @@ namespace_affects(af::ImperativeAffect, s) = namespace_affect(af, s) function namespace_affect(affect::ImperativeAffect, s) rmn = [] for modded in modified(affect) - if modded isa AbstractArray + if symbolic_type(modded) == NotSymbolic() && modded isa AbstractArray res = [] for m in modded push!(res, renamespace(s, m)) From b315138f0d4a1c7f8b3ec54cc152ed2c5c0827e2 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 7 May 2025 01:12:59 +0530 Subject: [PATCH 1478/2176] test: update tests to account for new u0 --- test/initializationsystem.jl | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/initializationsystem.jl b/test/initializationsystem.jl index 4274f31cc7..1ccc70607a 100644 --- a/test/initializationsystem.jl +++ b/test/initializationsystem.jl @@ -1186,10 +1186,10 @@ end @mtkbuild sys = ODESystem([D(x) ~ x * p + q, x^3 + y^3 ~ 3], t) prob = ODEProblem( sys, [], (0.0, 1.0), [p => 1.0]; guesses = [x => 1.0, y => 1.0, q => 1.0]) - @test prob[x] == 0.0 - @test prob[y] == 0.0 + @test prob[x] == 1.0 + @test prob[y] == 2.0 @test prob.ps[p] == 1.0 - @test prob.ps[q] == 0.0 + @test prob.ps[q] == 3.0 integ = init(prob) @test integ[x] ≈ 1 / cbrt(3) @test integ[y] ≈ 2 / cbrt(3) From 3ddffe4c4d777d4a18ce53426725e111ca64b36d Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 7 May 2025 01:14:28 +0530 Subject: [PATCH 1479/2176] build: bump minor version --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 266dee2ae6..b661dc402d 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "ModelingToolkit" uuid = "961ee093-0014-501f-94e3-6117800e7a78" authors = ["Yingbo Ma ", "Chris Rackauckas and contributors"] -version = "9.77.0" +version = "9.78.0" [deps] ADTypes = "47edcb42-4c32-4615-8424-f2b9edc5f35b" From bec63053add2511bffbd7965f493b23f6c9f11e4 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 7 May 2025 10:56:38 +0530 Subject: [PATCH 1480/2176] docs: fix `generate_control_function` docs --- docs/src/basics/InputOutput.md | 2 +- docs/src/tutorials/disturbance_modeling.md | 6 +++--- src/inputoutput.jl | 7 ++++--- src/systems/optimal_control_interface.jl | 2 +- 4 files changed, 9 insertions(+), 8 deletions(-) diff --git a/docs/src/basics/InputOutput.md b/docs/src/basics/InputOutput.md index 4dc5a3d50f..b01d093980 100644 --- a/docs/src/basics/InputOutput.md +++ b/docs/src/basics/InputOutput.md @@ -70,7 +70,7 @@ Now we can test the generated function `f` with random input and state values p = [1] x = [rand()] u = [rand()] -@test f[1](x, u, p, 1) ≈ -p[] * (x + u) # Test that the function computes what we expect D(x) = -k*(x + u) +@test f(x, u, p, 1) ≈ -p[] * (x + u) # Test that the function computes what we expect D(x) = -k*(x + u) ``` ## Generating an output function, ``g`` diff --git a/docs/src/tutorials/disturbance_modeling.md b/docs/src/tutorials/disturbance_modeling.md index db8d926498..0d85299744 100644 --- a/docs/src/tutorials/disturbance_modeling.md +++ b/docs/src/tutorials/disturbance_modeling.md @@ -184,7 +184,7 @@ disturbance_inputs = [ssys.d1, ssys.d2] P = ssys.system_model outputs = [P.inertia1.phi, P.inertia2.phi, P.inertia1.w, P.inertia2.w] -(f_oop, f_ip), x_sym, p_sym, io_sys = ModelingToolkit.generate_control_function( +f, x_sym, p_sym, io_sys = ModelingToolkit.generate_control_function( model_with_disturbance, inputs, disturbance_inputs; disturbance_argument = true) g = ModelingToolkit.build_explicit_observed_function( @@ -195,12 +195,12 @@ x0, _ = ModelingToolkit.get_u0_p(io_sys, op, op) p = MTKParameters(io_sys, op) u = zeros(1) # Control input w = zeros(length(disturbance_inputs)) # Disturbance input -@test f_oop(x0, u, p, t, w) == zeros(5) +@test f(x0, u, p, t, w) == zeros(5) @test g(x0, u, p, 0.0) == [0, 0, 0, 0] # Non-zero disturbance inputs should result in non-zero state derivatives. We call `sort` since we do not generally know the order of the state variables w = [1.0, 2.0] -@test sort(f_oop(x0, u, p, t, w)) == [0, 0, 0, 1, 2] +@test sort(f(x0, u, p, t, w)) == [0, 0, 0, 1, 2] ``` ## Input signal library diff --git a/src/inputoutput.jl b/src/inputoutput.jl index 8c1a084c9b..beaff42a0b 100644 --- a/src/inputoutput.jl +++ b/src/inputoutput.jl @@ -160,7 +160,7 @@ has_var(ex, x) = x ∈ Set(get_variables(ex)) # Build control function """ - (f_oop, f_ip), x_sym, p_sym, io_sys = generate_control_function( + f, x_sym, p_sym, io_sys = generate_control_function( sys::AbstractODESystem, inputs = unbound_inputs(sys), disturbance_inputs = nothing; @@ -168,8 +168,9 @@ has_var(ex, x) = x ∈ Set(get_variables(ex)) simplify = false, ) -For a system `sys` with inputs (as determined by [`unbound_inputs`](@ref) or user specified), generate a function with additional input argument `in` +For a system `sys` with inputs (as determined by [`unbound_inputs`](@ref) or user specified), generate a function with additional input argument `u` +The returned function `f` can be called in the out-of-place or in-place form: ``` f_oop : (x,u,p,t) -> rhs f_ip : (xout,x,u,p,t) -> nothing @@ -190,7 +191,7 @@ f, x_sym, ps = generate_control_function(sys, expression=Val{false}, simplify=fa p = varmap_to_vars(defaults(sys), ps) x = varmap_to_vars(defaults(sys), x_sym) t = 0 -f[1](x, inputs, p, t) +f(x, inputs, p, t) ``` """ function generate_control_function(sys::AbstractODESystem, inputs = unbound_inputs(sys), diff --git a/src/systems/optimal_control_interface.jl b/src/systems/optimal_control_interface.jl index a0bd88befd..beccbf8b41 100644 --- a/src/systems/optimal_control_interface.jl +++ b/src/systems/optimal_control_interface.jl @@ -50,7 +50,7 @@ function SciMLBase.ODEInputFunction{iip, specialize}(sys::ODESystem, initialization_data = nothing, cse = true, kwargs...) where {iip, specialize} - (f), _, _ = generate_control_function( + f, _, _ = generate_control_function( sys, inputs, disturbance_inputs; eval_module, cse, kwargs...) if tgrad From f9707d7cc0532b9acd2ee53d45b9c117f885c98a Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 7 May 2025 13:02:04 +0530 Subject: [PATCH 1481/2176] test: move jump control tests to `Extensions` test group --- test/downstream/Project.toml | 4 ---- test/extensions/Project.toml | 8 ++++++++ test/{downstream => extensions}/jump_control.jl | 0 test/runtests.jl | 2 +- 4 files changed, 9 insertions(+), 5 deletions(-) rename test/{downstream => extensions}/jump_control.jl (100%) diff --git a/test/downstream/Project.toml b/test/downstream/Project.toml index 1192dda074..f64ed17de6 100644 --- a/test/downstream/Project.toml +++ b/test/downstream/Project.toml @@ -1,10 +1,6 @@ [deps] ControlSystemsMTK = "687d7614-c7e5-45fc-bfc3-9ee385575c88" DataInterpolations = "82cc6244-b520-54b8-b5a6-8a565e85f1d0" -DiffEqDevTools = "f3b72e0c-5b89-59e1-b016-84e28bfd966d" -InfiniteOpt = "20393b10-9daf-11e9-18c9-8db751c92c57" -Ipopt = "b6b21f68-93f8-5de0-b562-5493be1d77c9" -JuMP = "4076af6c-e467-56ae-b986-b466b2749572" LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" ModelingToolkit = "961ee093-0014-501f-94e3-6117800e7a78" ModelingToolkitStandardLibrary = "16a59e39-deab-5bd0-87e4-056b12336739" diff --git a/test/extensions/Project.toml b/test/extensions/Project.toml index f5cedf49fe..0e018b4a22 100644 --- a/test/extensions/Project.toml +++ b/test/extensions/Project.toml @@ -2,18 +2,26 @@ BifurcationKit = "0f109fa4-8a5d-4b75-95aa-f515264e7665" ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4" ChainRulesTestUtils = "cdddcdb0-9152-4a09-a978-84456f9df70a" +DataInterpolations = "82cc6244-b520-54b8-b5a6-8a565e85f1d0" +DiffEqBase = "2b5f629d-d688-5b77-993f-72d75c75574e" +DiffEqDevTools = "f3b72e0c-5b89-59e1-b016-84e28bfd966d" ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210" HomotopyContinuation = "f213a82b-91d6-5c5d-acf7-10f1c761b327" InfiniteOpt = "20393b10-9daf-11e9-18c9-8db751c92c57" +Ipopt = "b6b21f68-93f8-5de0-b562-5493be1d77c9" JuMP = "4076af6c-e467-56ae-b986-b466b2749572" LabelledArrays = "2ee39098-c373-598a-b85f-a56591580800" ModelingToolkit = "961ee093-0014-501f-94e3-6117800e7a78" Nemo = "2edaba10-b0f1-5616-af89-8c11ac63239a" NonlinearSolveHomotopyContinuation = "2ac3b008-d579-4536-8c91-a1a5998c2f8b" +OrdinaryDiffEqFIRK = "5960d6e9-dd7a-4743-88e7-cf307b64f125" OrdinaryDiffEqNonlinearSolve = "127b3ac7-2247-4354-8eb6-78cf4e7c58e8" +OrdinaryDiffEqSDIRK = "2d112036-d095-4a1e-ab9a-08536f3ecdbf" OrdinaryDiffEqTsit5 = "b1df2697-797e-41e3-8120-5422d3b24e4a" +OrdinaryDiffEqVerner = "79d7bb75-1356-48c1-b8c0-6832512096c2" SciMLSensitivity = "1ed8b502-d754-442c-8d5d-10ac956f44a1" SciMLStructures = "53ae85a6-f571-4167-b2af-e1d143709226" +SimpleDiffEq = "05bca326-078c-5bf0-a5bf-ce7c7982d7fd" SymbolicIndexingInterface = "2efcf032-c050-4f8e-a9bb-153293bab1f5" Symbolics = "0c5d862f-8b57-4792-8d23-62f2024744c7" Zygote = "e88e6eb3-aa80-5325-afca-941959d7151f" diff --git a/test/downstream/jump_control.jl b/test/extensions/jump_control.jl similarity index 100% rename from test/downstream/jump_control.jl rename to test/extensions/jump_control.jl diff --git a/test/runtests.jl b/test/runtests.jl index c4fee4110d..80339c6606 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -122,7 +122,6 @@ end if GROUP == "All" || GROUP == "Downstream" activate_downstream_env() - @safetestset "JuMP Collocation Solvers" include("downstream/jump_control.jl") @safetestset "Linearization Tests" include("downstream/linearize.jl") @safetestset "Linearization Dummy Derivative Tests" include("downstream/linearization_dd.jl") @safetestset "Inverse Models Test" include("downstream/inversemodel.jl") @@ -137,6 +136,7 @@ end if GROUP == "All" || GROUP == "Extensions" activate_extensions_env() + @safetestset "JuMP Collocation Solvers" include("extensions/jump_control.jl") @safetestset "HomotopyContinuation Extension Test" include("extensions/homotopy_continuation.jl") @safetestset "Auto Differentiation Test" include("extensions/ad.jl") @safetestset "LabelledArrays Test" include("labelledarrays.jl") From a9ac7260a3b2b89f7eedb2bc5443fe59812c46f4 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 8 May 2025 12:16:44 +0530 Subject: [PATCH 1482/2176] fix: fix nested conditions in `IfLifting` --- src/systems/if_lifting.jl | 4 ++-- test/if_lifting.jl | 40 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/src/systems/if_lifting.jl b/src/systems/if_lifting.jl index da069cc76e..aeb8afc0a8 100644 --- a/src/systems/if_lifting.jl +++ b/src/systems/if_lifting.jl @@ -111,8 +111,8 @@ function (cw::CondRewriter)(expr, dep) # and ELSE branch is true # similarly for expression being false return (ifelse(rw_cond, rw_conda, rw_condb), - implies(ctrue, truea) | implies(cfalse, trueb), - implies(ctrue, falsea) | implies(cfalse, falseb)) + ctrue & truea | cfalse & trueb, + ctrue & falsea | cfalse & falseb) elseif operation(expr) == Base.:(!) # NOT of expression (a,) = arguments(expr) (rw, ctrue, cfalse) = cw(a, dep) diff --git a/test/if_lifting.jl b/test/if_lifting.jl index 9c58e676d0..085dc600c8 100644 --- a/test/if_lifting.jl +++ b/test/if_lifting.jl @@ -124,3 +124,43 @@ end end @test_nowarn @mtkbuild sys=SimpleAbs() additional_passes=[IfLifting] end + +@testset "Nested conditions are handled properly" begin + @mtkmodel RampModel begin + @variables begin + x(t) + y(t) + end + @parameters begin + start_time = 1.0 + duration = 1.0 + height = 1.0 + end + @equations begin + y ~ ifelse(start_time < t, + ifelse(t < start_time + duration, + (t - start_time) * height / duration, height), + 0.0) + D(x) ~ y + end + end + @mtkbuild sys = RampModel() + @mtkbuild sys2=RampModel() additional_passes=[IfLifting] + prob = ODEProblem(sys, [sys.x => 1.0], (0.0, 3.0)) + prob2 = ODEProblem(sys2, [sys.x => 1.0], (0.0, 3.0)) + sol = solve(prob) + sol2 = solve(prob2) + @test sol(0.99)[1] > 1.0 + @test sol2(0.99)[1] == 1.0 + # During ramp + # D(x) ~ t - 1 + # x ~ t^2 / 2 - t + C, and `x(1) ~ 1` => `C = 3/2` + # x(1.01) ~ 1.01^2 / 2 - 1.01 + 3/2 ~ 1.00005 + @test sol2(1.01)[1] ≈ 1.00005 + @test sol2(2)[1] ≈ 1.5 + # After ramp + # D(x) ~ 1 + # x ~ t + C and `x(2) ~ 3/2` => `C = -1/2` + # x(3) ~ 3 - 1/2 + @test sol2(3)[1] ≈ 5 / 2 +end From 1836e373c7ec59107fd1b559fb2353ee0b1dfbb1 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 8 May 2025 12:32:50 +0530 Subject: [PATCH 1483/2176] test: fix inversemodel test --- test/downstream/inversemodel.jl | 1 + 1 file changed, 1 insertion(+) diff --git a/test/downstream/inversemodel.jl b/test/downstream/inversemodel.jl index 7bed0bc1e8..dc2ee380a2 100644 --- a/test/downstream/inversemodel.jl +++ b/test/downstream/inversemodel.jl @@ -2,6 +2,7 @@ using ModelingToolkit using ModelingToolkitStandardLibrary using ModelingToolkitStandardLibrary.Blocks using OrdinaryDiffEqRosenbrock +using OrdinaryDiffEqNonlinearSolve using SymbolicIndexingInterface using Test using ControlSystemsMTK: tf, ss, get_named_sensitivity, get_named_comp_sensitivity From ab023f6e449241e21782198256f32fe671ea3509 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 8 May 2025 11:37:53 +0530 Subject: [PATCH 1484/2176] fix: fix breaking change to `generate_control_function` --- docs/src/basics/InputOutput.md | 2 +- docs/src/tutorials/disturbance_modeling.md | 6 +++--- src/inputoutput.jl | 13 +++++++------ src/systems/optimal_control_interface.jl | 1 + test/downstream/test_disturbance_model.jl | 8 ++++---- test/extensions/test_infiniteopt.jl | 2 +- test/input_output_handling.jl | 10 +++++----- 7 files changed, 22 insertions(+), 20 deletions(-) diff --git a/docs/src/basics/InputOutput.md b/docs/src/basics/InputOutput.md index b01d093980..4dc5a3d50f 100644 --- a/docs/src/basics/InputOutput.md +++ b/docs/src/basics/InputOutput.md @@ -70,7 +70,7 @@ Now we can test the generated function `f` with random input and state values p = [1] x = [rand()] u = [rand()] -@test f(x, u, p, 1) ≈ -p[] * (x + u) # Test that the function computes what we expect D(x) = -k*(x + u) +@test f[1](x, u, p, 1) ≈ -p[] * (x + u) # Test that the function computes what we expect D(x) = -k*(x + u) ``` ## Generating an output function, ``g`` diff --git a/docs/src/tutorials/disturbance_modeling.md b/docs/src/tutorials/disturbance_modeling.md index 0d85299744..db8d926498 100644 --- a/docs/src/tutorials/disturbance_modeling.md +++ b/docs/src/tutorials/disturbance_modeling.md @@ -184,7 +184,7 @@ disturbance_inputs = [ssys.d1, ssys.d2] P = ssys.system_model outputs = [P.inertia1.phi, P.inertia2.phi, P.inertia1.w, P.inertia2.w] -f, x_sym, p_sym, io_sys = ModelingToolkit.generate_control_function( +(f_oop, f_ip), x_sym, p_sym, io_sys = ModelingToolkit.generate_control_function( model_with_disturbance, inputs, disturbance_inputs; disturbance_argument = true) g = ModelingToolkit.build_explicit_observed_function( @@ -195,12 +195,12 @@ x0, _ = ModelingToolkit.get_u0_p(io_sys, op, op) p = MTKParameters(io_sys, op) u = zeros(1) # Control input w = zeros(length(disturbance_inputs)) # Disturbance input -@test f(x0, u, p, t, w) == zeros(5) +@test f_oop(x0, u, p, t, w) == zeros(5) @test g(x0, u, p, 0.0) == [0, 0, 0, 0] # Non-zero disturbance inputs should result in non-zero state derivatives. We call `sort` since we do not generally know the order of the state variables w = [1.0, 2.0] -@test sort(f(x0, u, p, t, w)) == [0, 0, 0, 1, 2] +@test sort(f_oop(x0, u, p, t, w)) == [0, 0, 0, 1, 2] ``` ## Input signal library diff --git a/src/inputoutput.jl b/src/inputoutput.jl index beaff42a0b..739df14ae0 100644 --- a/src/inputoutput.jl +++ b/src/inputoutput.jl @@ -160,7 +160,7 @@ has_var(ex, x) = x ∈ Set(get_variables(ex)) # Build control function """ - f, x_sym, p_sym, io_sys = generate_control_function( + (f_oop, f_ip), x_sym, p_sym, io_sys = generate_control_function( sys::AbstractODESystem, inputs = unbound_inputs(sys), disturbance_inputs = nothing; @@ -168,9 +168,9 @@ has_var(ex, x) = x ∈ Set(get_variables(ex)) simplify = false, ) -For a system `sys` with inputs (as determined by [`unbound_inputs`](@ref) or user specified), generate a function with additional input argument `u` +For a system `sys` with inputs (as determined by [`unbound_inputs`](@ref) or user specified), generate functions with additional input argument `u` -The returned function `f` can be called in the out-of-place or in-place form: +The returned functions are the out-of-place (`f_oop`) and in-place (`f_ip`) forms: ``` f_oop : (x,u,p,t) -> rhs f_ip : (xout,x,u,p,t) -> nothing @@ -191,7 +191,7 @@ f, x_sym, ps = generate_control_function(sys, expression=Val{false}, simplify=fa p = varmap_to_vars(defaults(sys), ps) x = varmap_to_vars(defaults(sys), x_sym) t = 0 -f(x, inputs, p, t) +f[1](x, inputs, p, t) ``` """ function generate_control_function(sys::AbstractODESystem, inputs = unbound_inputs(sys), @@ -253,9 +253,10 @@ function generate_control_function(sys::AbstractODESystem, inputs = unbound_inpu f = build_function_wrapper(sys, rhss, args...; p_start = 3 + implicit_dae, p_end = length(p) + 2 + implicit_dae, kwargs...) f = eval_or_rgf.(f; eval_expression, eval_module) - f = GeneratedFunctionWrapper{(3, length(args) - length(p) + 1, is_split(sys))}(f...) + f = GeneratedFunctionWrapper{( + 3 + implicit_dae, length(args) - length(p) + 1, is_split(sys))}(f...) ps = setdiff(parameters(sys), inputs, disturbance_inputs) - (; f, dvs, ps, io_sys = sys) + (; f = (f, f), dvs, ps, io_sys = sys) end function inputs_to_parameters!(state::TransformationState, io) diff --git a/src/systems/optimal_control_interface.jl b/src/systems/optimal_control_interface.jl index beccbf8b41..eb573da810 100644 --- a/src/systems/optimal_control_interface.jl +++ b/src/systems/optimal_control_interface.jl @@ -52,6 +52,7 @@ function SciMLBase.ODEInputFunction{iip, specialize}(sys::ODESystem, kwargs...) where {iip, specialize} f, _, _ = generate_control_function( sys, inputs, disturbance_inputs; eval_module, cse, kwargs...) + f = f[1] if tgrad tgrad_gen = generate_tgrad(sys, dvs, ps; diff --git a/test/downstream/test_disturbance_model.jl b/test/downstream/test_disturbance_model.jl index cd9d769e99..0e04200237 100644 --- a/test/downstream/test_disturbance_model.jl +++ b/test/downstream/test_disturbance_model.jl @@ -168,22 +168,22 @@ x0, p = ModelingToolkit.get_u0_p(io_sys, op, op) x = zeros(5) u = zeros(1) d = zeros(3) -@test f(x, u, p, t, d) == zeros(5) +@test f[1](x, u, p, t, d) == zeros(5) @test measurement(x, u, p, 0.0) == [0, 0, 0, 0] @test measurement2(x, u, p, 0.0, d) == [0] # Add to the integrating disturbance input d = [1, 0, 0] -@test sort(f(x, u, p, 0.0, d)) == [0, 0, 0, 1, 1] # Affects disturbance state and one velocity +@test sort(f[1](x, u, p, 0.0, d)) == [0, 0, 0, 1, 1] # Affects disturbance state and one velocity @test measurement2(x, u, p, 0.0, d) == [0] d = [0, 1, 0] -@test sort(f(x, u, p, 0.0, d)) == [0, 0, 0, 0, 1] # Affects one velocity +@test sort(f[1](x, u, p, 0.0, d)) == [0, 0, 0, 0, 1] # Affects one velocity @test measurement(x, u, p, 0.0) == [0, 0, 0, 0] @test measurement2(x, u, p, 0.0, d) == [0] d = [0, 0, 1] -@test sort(f(x, u, p, 0.0, d)) == [0, 0, 0, 0, 0] # Affects nothing +@test sort(f[1](x, u, p, 0.0, d)) == [0, 0, 0, 0, 0] # Affects nothing @test measurement(x, u, p, 0.0) == [0, 0, 0, 0] @test measurement2(x, u, p, 0.0, d) == [1] # We have now disturbed the output diff --git a/test/extensions/test_infiniteopt.jl b/test/extensions/test_infiniteopt.jl index 833b9f3275..eb734358c5 100644 --- a/test/extensions/test_infiniteopt.jl +++ b/test/extensions/test_infiniteopt.jl @@ -65,7 +65,7 @@ InfiniteOpt.@variables(m, # Trace the dynamics x0, p = ModelingToolkit.get_u0_p(io_sys, [model.θ => 0, model.ω => 0], [model.L => L]) -xp = f(x, u, p, τ) +xp = f[1](x, u, p, τ) cp = f_obs(x, u, p, τ) # Test that it's possible to trace through an observed function @objective(m, Min, tf) diff --git a/test/input_output_handling.jl b/test/input_output_handling.jl index 2c16c89320..115426444e 100644 --- a/test/input_output_handling.jl +++ b/test/input_output_handling.jl @@ -173,7 +173,7 @@ end p = [rand()] x = [rand()] u = [rand()] - @test f(x, u, p, 1) ≈ -x + u + @test f[1](x, u, p, 1) ≈ -x + u # With disturbance inputs @variables x(t)=0 u(t)=0 [input = true] d(t)=0 @@ -191,7 +191,7 @@ end p = [rand()] x = [rand()] u = [rand()] - @test f(x, u, p, 1) ≈ -x + u + @test f[1](x, u, p, 1) ≈ -x + u ## With added d argument @variables x(t)=0 u(t)=0 [input = true] d(t)=0 @@ -210,7 +210,7 @@ end x = [rand()] u = [rand()] d = [rand()] - @test f(x, u, p, t, d) ≈ -x + u + [d[]^2] + @test f[1](x, u, p, t, d) ≈ -x + u + [d[]^2] end end @@ -273,7 +273,7 @@ x = ModelingToolkit.varmap_to_vars( merge(ModelingToolkit.defaults(model), Dict(D.(unknowns(model)) .=> 0.0)), dvs) u = [rand()] -out = f(x, u, p, 1) +out = f[1](x, u, p, 1) i = findfirst(isequal(u[1]), out) @test i isa Int @test iszero(out[[1:(i - 1); (i + 1):end]]) @@ -447,7 +447,7 @@ end @named sys = ODESystem(eqs, t, [x], []) f, dvs, ps, io_sys = ModelingToolkit.generate_control_function(sys, simplify = true) - @test f([0.5], nothing, MTKParameters(io_sys, []), 0.0) ≈ [1.0] + @test f[1]([0.5], nothing, MTKParameters(io_sys, []), 0.0) ≈ [1.0] end @testset "With callable symbolic" begin From 948f38ca71297e6e1f4f7c0cea2427df9cdc1c79 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 8 May 2025 15:00:08 +0530 Subject: [PATCH 1485/2176] test: fix input output tests --- test/input_output_handling.jl | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/input_output_handling.jl b/test/input_output_handling.jl index 115426444e..67f60a04cd 100644 --- a/test/input_output_handling.jl +++ b/test/input_output_handling.jl @@ -348,8 +348,8 @@ x0 = randn(5) x1 = copy(x0) + x_add # add disturbance state perturbation u = randn(1) pn = MTKParameters(io_sys, []) -xp0 = f(x0, u, pn, 0) -xp1 = f(x1, u, pn, 0) +xp0 = f[1](x0, u, pn, 0) +xp1 = f[1](x1, u, pn, 0) @test xp0 ≈ matrices.A * x0 + matrices.B * [u; 0] @test xp1 ≈ matrices.A * x1 + matrices.B * [u; 0] @@ -459,5 +459,5 @@ end p = MTKParameters(io_sys, []) u = [1.0] x = [1.0] - @test_nowarn f(x, u, p, 0.0) + @test_nowarn f[1](x, u, p, 0.0) end From 1093c1cfe3a63a73953fd6c594825c7e0ca77ca3 Mon Sep 17 00:00:00 2001 From: vyudu Date: Tue, 8 Apr 2025 00:40:15 -0400 Subject: [PATCH 1486/2176] feat: solver tableau debugging --- Project.toml | 4 + ext/MTKJuMPControlExt.jl | 218 ++++++++++++++++++++++++ test/extensions/Project.toml | 1 + test/extensions/jump_control.jl | 291 +++----------------------------- 4 files changed, 249 insertions(+), 265 deletions(-) create mode 100644 ext/MTKJuMPControlExt.jl diff --git a/Project.toml b/Project.toml index b661dc402d..b29b2b9566 100644 --- a/Project.toml +++ b/Project.toml @@ -31,6 +31,7 @@ FunctionWrappers = "069b7b12-0de2-55c6-9aab-29f3d0a68a2e" FunctionWrappersWrappers = "77dc65aa-8811-40c2-897b-53d922fa7daf" Graphs = "86223c79-3864-5bf0-83f7-82e725a168b6" InteractiveUtils = "b77e0a4c-d291-57a0-90e8-8db25a27a240" +Ipopt = "b6b21f68-93f8-5de0-b562-5493be1d77c9" JuliaFormatter = "98e50ef6-434e-11e9-1051-2b60c6c9e899" JumpProcesses = "ccbc3e58-028d-4f4c-8cd5-9ae44345cda5" Latexify = "23fbe1c1-3f47-55db-b15f-69d7ec21a316" @@ -66,8 +67,10 @@ Unitful = "1986cc42-f94f-5a68-af5c-568840ba703d" BifurcationKit = "0f109fa4-8a5d-4b75-95aa-f515264e7665" ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4" DeepDiffs = "ab62b9b5-e342-54a8-a765-a90f495de1a6" +DiffEqDevTools = "f3b72e0c-5b89-59e1-b016-84e28bfd966d" FMI = "14a09403-18e3-468f-ad8a-74f8dda2d9ac" InfiniteOpt = "20393b10-9daf-11e9-18c9-8db751c92c57" +JuMP = "4076af6c-e467-56ae-b986-b466b2749572" LabelledArrays = "2ee39098-c373-598a-b85f-a56591580800" [extensions] @@ -97,6 +100,7 @@ DeepDiffs = "1" DelayDiffEq = "5.50" DiffEqBase = "6.170.1" DiffEqCallbacks = "2.16, 3, 4" +DiffEqDevTools = "2.48.0" DiffEqNoiseProcess = "5" DiffRules = "0.1, 1.0" DifferentiationInterface = "0.6.47" diff --git a/ext/MTKJuMPControlExt.jl b/ext/MTKJuMPControlExt.jl new file mode 100644 index 0000000000..1e788b05ec --- /dev/null +++ b/ext/MTKJuMPControlExt.jl @@ -0,0 +1,218 @@ +module MTKJuMPControlExt +using ModelingToolkit +using JuMP, InfiniteOpt +using DiffEqDevTools, DiffEqBase, SciMLBase +using LinearAlgebra +const MTK = ModelingToolkit + +struct JuMPControlProblem{uType, tType, P, F, K} + f::F + u0::uType + tspan::tType + p::P + model::InfiniteModel + kwargs::K + + function JuMPControlProblem(f, u0, tspan, p, model; kwargs...) + new{typeof(u0), typeof(tspan), typeof(p), typeof(f), typeof(kwargs)}(f, u0, tspan, p, model, kwargs) + end +end + +""" + JuMPControlProblem(sys::ODESystem, u0, tspan, p; dt) + +Convert an ODESystem representing an optimal control system into a JuMP model +for solving using optimization. Must provide `dt` for determining the length +of the interpolation arrays. + +The optimization variables: +- a vector-of-vectors U representing the unknowns as an interpolation array +- a vector-of-vectors V representing the controls as an interpolation array + +The constraints are: +- The set of user constraints passed to the ODESystem via `constraints` +- The solver constraints that encode the time-stepping used by the solver +""" +function MTK.JuMPControlProblem(sys::ODESystem, u0map, tspan, pmap; dt = error("dt must be provided for JuMPControlProblem."), kwargs...) + ts = tspan[1] + te = tspan[2] + steps = ts:dt:te + ctrls = controls(sys) + states = unknowns(sys) + constraintsys = MTK.get_constraintsystem(sys) + + if !isnothing(constraintsys) + (length(constraints(constraintsys)) + length(u0map) > length(sts)) && + @warn "The JuMPControlProblem is overdetermined. The total number of conditions (# constraints + # fixed initial values given by u0map) exceeds the total number of states. The solvers will default to doing a nonlinear least-squares optimization." + end + + f, u0, p = MTK.process_SciMLProblem(ODEFunction, sys, u0map, pmap; + t = tspan !== nothing ? tspan[1] : tspan, kwargs...) + + model = InfiniteModel() + @infinite_parameter(model, t in [ts, te], num_supports = length(steps), derivative_method = OrthogonalCollocation(2)) + @variable(model, U[1:length(states)], Infinite(t), start = ts) + @variable(model, V[1:length(ctrls)], Infinite(t), start = ts) + + add_jump_cost_function!(model, sys) + add_user_constraints!(model, sys) + + stidxmap = Dict([v => i for (i, v) in enumerate(states)]) + u0_idxs = has_alg_eqs(sys) ? collect(1:length(states)) : [stidxmap[k] for (k, v) in u0map] + add_initial_constraints!(model, u0, u0_idxs, tspan) + + JuMPControlProblem(f, u0, tspan, p, model, kwargs...) +end + +function add_jump_cost_function!(model, sys) + jcosts = MTK.get_costs(sys) + consolidate = MTK.get_consolidate(sys) + if isnothing(consolidate) + @objective(model, Min, 0) + return + end + iv = MTK.get_iv(sys) + + stidxmap = Dict([v => i for (i, v) in enumerate(unknowns(sys))]) + cidxmap = Dict([v => i for (i, v) in enumerate(controls(sys))]) + + for st in unknowns(sys) + x = operation(st) + t = only(arguments(st)) + idx = stidxmap[x(iv)] + subval = isequal(t, iv) ? model[:U][idx] : model[:U][idx](t) + jcosts = Symbolics.substitute(jcosts, Dict(x(t) => subval)) + end + + for ct in controls(sys) + p = operation(ct) + t = only(arguments(ct)) + idx = cidxmap[p(iv)] + subval = isequal(t, iv) ? model[:V][idx] : model[:V][idx](t) + jcosts = Symbolics.substitute(jcosts, Dict(x(t) => subval)) + end + + @objective(model, Min, consolidate(jcosts)) +end + +function add_user_constraints!(model, sys) + jconstraints = if !(csys = MTK.get_constraintsystem(sys) isa Nothing) + MTK.get_constraints(csys) + else + nothing + end + isnothing(jconstraints) && return nothing + + iv = MTK.get_iv(sys) + stidxmap = Dict([v => i for (i, v) in enumerate(unknowns(sys))]) + cidxmap = Dict([v => i for (i, v) in enumerate(controls(sys))]) + + for st in unknowns(sys) + x = operation(st) + t = only(arguments(st)) + idx = stidxmap[x(iv)] + subval = isequal(t, iv) ? model[:U][idx] : model[:U][idx](t) + jconstraints = Symbolics.substitute(jconstraints, Dict(x(t) => subval)) + end + + for ct in controls(sys) + p = operation(ct) + t = only(arguments(ct)) + idx = cidxmap[p(iv)] + subval = isequal(t, iv) ? model[:V][idx] : model[:V][idx](t) + jconstraints = Symbolics.substitute(jconstraints, Dict(p(t) => subval)) + end + + for (i, cons) in enumerate(jconstraints) + if cons isa Equation + @constraint(model, user[i], cons.lhs - cons.rhs == 0) + elseif cons.relational_op === Symbolics.geq + @constraint(model, user[i], cons.lhs - cons.rhs ≥ 0) + else + @constraint(model, user[i], cons.lhs - cons.rhs ≤ 0) + end + end +end + +function add_initial_constraints!(model, u0, u0_idxs, tspan) + ts = tspan[1] + @constraint(model, init_u0_idx[i in u0_idxs], model[:U][i](ts) == u0[i]) +end + +is_explicit(tableau) = tableau isa DiffEqDevTools.ExplicitRKTableau + +function add_solve_constraints!(prob, tableau) + A = tableau.A + α = tableau.α + c = tableau.c + model = prob.model + f = prob.f + p = prob.p + tsteps = supports(model[:t]) + pop!(tsteps) + dt = tsteps[2] - tsteps[1] + + U = model[:U] + nᵤ = length(U) + if is_explicit(tableau) + K = Any[] + for τ in tsteps + for (i, h) in enumerate(c) + ΔU = sum([A[i, j] * K[j] for j in 1:i-1], init = zeros(nᵤ)) + Uₙ = [U[i](τ) + ΔU[i]*dt for i in 1:nᵤ] + Kₙ = f(Uₙ, p, τ + h*dt) + push!(K, Kₙ) + end + ΔU = sum([α[i] * K[i] for i in 1:length(α)]) + @constraint(model, [n = 1:nᵤ], U[n](τ) + ΔU[n] == U[n](τ + dt)) + empty!(K) + end + else + @variable(model, K[1:length(a), 1:nᵤ], Infinite(t), start = tsteps[1]) + for τ in tsteps + ΔUs = [A * K(τ)] + for (i, h) in enumerate(c) + ΔU = ΔUs[i] + Uₙ = [U[j](τ) + ΔU[j](τ)*dt for j in 1:nᵤ] + @constraint(model, K[i](τ) == f(Uₙ, p, τ + h*dt)) + end + ΔU = sum([α[i] * K[i] for i in 1:length(α)]) + @constraint(model, U(τ) + dot(α, K(τ)) == U(τ + dt)) + end + end +end + +""" +""" +struct JuMPControlSolution + model::InfiniteModel + sol::ODESolution +end + +""" +Solve JuMPControlProblem. Arguments: +- prob: a JumpControlProblem +- jump_solver: a LP solver such as HiGHS +- ode_solver: Takes in a symbol representing the solver. Acceptable solvers may be found at https://docs.sciml.ai/DiffEqDevDocs/stable/internals/tableaus/. Note that the symbol may be different than the typical name of the solver, e.g. :Tsitouras5 rather than Tsit5. +""" +function DiffEqBase.solve(prob::JuMPControlProblem, jump_solver, ode_solver::Symbol) + model = prob.model + tableau_getter = Symbol(:construct, ode_solver) + @eval tableau = $tableau_getter() + ts = supports(model[:t]) + add_solve_constraints!(prob, tableau) + + set_optimizer(model, jump_solver) + optimize!(model) + + if is_solved_and_feasible(model) + sol = DiffEqBase.build_solution(prob, ode_solver, ts, value.(U)) + JuMPControlSolution(model, sol) + else + sol = DiffEqBase.build_solution(prob, ode_solver, ts, value.(U)) + sol = SciMLBase.solution_new_retcode(sol, SciMLBase.ReturnCode.ConvergenceFailure) + JuMPControlSolution(model, sol) + end +end + +end diff --git a/test/extensions/Project.toml b/test/extensions/Project.toml index 0e018b4a22..bd51ea5203 100644 --- a/test/extensions/Project.toml +++ b/test/extensions/Project.toml @@ -6,6 +6,7 @@ DataInterpolations = "82cc6244-b520-54b8-b5a6-8a565e85f1d0" DiffEqBase = "2b5f629d-d688-5b77-993f-72d75c75574e" DiffEqDevTools = "f3b72e0c-5b89-59e1-b016-84e28bfd966d" ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210" +HiGHS = "87dc4568-4c63-4d18-b0c0-bb2238e4078b" HomotopyContinuation = "f213a82b-91d6-5c5d-acf7-10f1c761b327" InfiniteOpt = "20393b10-9daf-11e9-18c9-8db751c92c57" Ipopt = "b6b21f68-93f8-5de0-b562-5493be1d77c9" diff --git a/test/extensions/jump_control.jl b/test/extensions/jump_control.jl index a93146be10..c0a071e31d 100644 --- a/test/extensions/jump_control.jl +++ b/test/extensions/jump_control.jl @@ -1,296 +1,57 @@ using ModelingToolkit -import JuMP, InfiniteOpt +using JuMP, InfiniteOpt using DiffEqDevTools, DiffEqBase using SimpleDiffEq -using OrdinaryDiffEqSDIRK, OrdinaryDiffEqVerner, OrdinaryDiffEqTsit5, OrdinaryDiffEqFIRK -using Ipopt -using DataInterpolations +using HiGHS const M = ModelingToolkit @testset "ODE Solution, no cost" begin # Test solving without anything attached. @parameters α=1.5 β=1.0 γ=3.0 δ=1.0 - @variables x(..) y(..) - t = M.t_nounits - D = M.D_nounits + M.@variables x(..) y(..) + t = M.t_nounits; D = M.D_nounits eqs = [D(x(t)) ~ α * x(t) - β * x(t) * y(t), D(y(t)) ~ -γ * y(t) + δ * x(t) * y(t)] - @mtkbuild sys = ODESystem(eqs, t) tspan = (0.0, 1.0) u0map = [x(t) => 4.0, y(t) => 2.0] - parammap = [α => 1.5, β => 1.0, γ => 3.0, δ => 1.0] + parammap = [α => 7.5, β => 4, γ => 8.0, δ => 5.0] + @mtkbuild sys = ODESystem(eqs, t) # Test explicit method. - jprob = JuMPDynamicOptProblem(sys, u0map, tspan, parammap, dt = 0.01) - @test JuMP.num_constraints(jprob.model) == 2 # initials - jsol = solve(jprob, Ipopt.Optimizer, constructRK4, silent = true) + jprob = JuMPControlProblem(sys, u0map, tspan, parammap, dt = 0.01) + @test num_constraints(jprob.model) == 2 # initials + jsol = solve(jprob, Ipopt.Optimizer, :Tsitouras5) oprob = ODEProblem(sys, u0map, tspan, parammap) - osol = solve(oprob, SimpleRK4(), dt = 0.01) + osol = solve(oprob, SimpleTsit5(), adaptive = false) @test jsol.sol.u ≈ osol.u # Implicit method. - jsol2 = solve(jprob, Ipopt.Optimizer, constructImplicitEuler, silent = true) # 63.031 ms, 26.49 MiB - osol2 = solve(oprob, ImplicitEuler(), dt = 0.01, adaptive = false) # 129.375 μs, 61.91 KiB - @test ≈(jsol2.sol.u, osol2.u, rtol = 0.001) - iprob = InfiniteOptDynamicOptProblem(sys, u0map, tspan, parammap, dt = 0.01) - isol = solve(iprob, Ipopt.Optimizer, - derivative_method = InfiniteOpt.FiniteDifference(InfiniteOpt.Backward()), - silent = true) # 11.540 ms, 4.00 MiB - @test ≈(jsol2.sol.u, osol2.u, rtol = 0.001) + jsol2 = solve(prob, Ipopt.Optimizer, :RK4) + osol2 = solve(oprob, SimpleRK4(), adaptive = false) + @test jsol2.sol.u ≈ osol2.u # With a constraint - u0map = Pair[] + @parameters α=1.5 β=1.0 γ=3.0 δ=1.0 + @variables x(..) y(..) + + eqs = [D(x(t)) ~ α * x(t) - β * x(t) * y(t), + D(y(t)) ~ -γ * y(t) + δ * x(t) * y(t)] + + u0map = [] + tspan = (0.0, 1.0) guess = [x(t) => 4.0, y(t) => 2.0] constr = [x(0.6) ~ 3.5, x(0.3) ~ 7.0] @mtkbuild lksys = ODESystem(eqs, t; constraints = constr) - jprob = JuMPDynamicOptProblem(lksys, u0map, tspan, parammap; guesses = guess, dt = 0.01) - @test JuMP.num_constraints(jprob.model) == 2 - jsol = solve(jprob, Ipopt.Optimizer, constructTsitouras5, silent = true) # 12.190 s, 9.68 GiB - @test jsol.sol(0.6)[1] ≈ 3.5 - @test jsol.sol(0.3)[1] ≈ 7.0 - - iprob = InfiniteOptDynamicOptProblem( - lksys, u0map, tspan, parammap; guesses = guess, dt = 0.01) - isol = solve(iprob, Ipopt.Optimizer, - derivative_method = InfiniteOpt.OrthogonalCollocation(3), silent = true) # 48.564 ms, 9.58 MiB - sol = isol.sol + jprob = JuMPControlProblem(sys, u0map, tspan, parammap; guesses, dt = 0.01) + @test num_constraints(jprob.model) == 2 == num_variables(jprob.model) == 2 + jsol = solve(prob, HiGHS.Optimizer, :Tsitouras5) + sol = jsol.sol @test sol(0.6)[1] ≈ 3.5 @test sol(0.3)[1] ≈ 7.0 - - # Test whole-interval constraints - constr = [x(t) ≳ 1, y(t) ≳ 1] - @mtkbuild lksys = ODESystem(eqs, t; constraints = constr) - iprob = InfiniteOptDynamicOptProblem( - lksys, u0map, tspan, parammap; guesses = guess, dt = 0.01) - isol = solve(iprob, Ipopt.Optimizer, - derivative_method = InfiniteOpt.OrthogonalCollocation(3), silent = true) # 48.564 ms, 9.58 MiB - @test all(u -> u > [1, 1], isol.sol.u) - - jprob = JuMPDynamicOptProblem(lksys, u0map, tspan, parammap; guesses = guess, dt = 0.01) - jsol = solve(jprob, Ipopt.Optimizer, constructRadauIA3, silent = true) # 12.190 s, 9.68 GiB - @test all(u -> u > [1, 1], jsol.sol.u) -end - -function is_bangbang(input_sol, lbounds, ubounds, rtol = 1e-4) - for v in 1:(length(input_sol.u[1]) - 1) - all(i -> ≈(i[v], bounds[v]; rtol) || ≈(i[v], bounds[u]; rtol), input_sol.u) || - return false - end - true end -function ctrl_to_spline(inputsol, splineType) - us = reduce(vcat, inputsol.u) - ts = reduce(vcat, inputsol.t) - splineType(us, ts) -end - -@testset "Linear systems" begin - # Double integrator - t = M.t_nounits - D = M.D_nounits - @variables x(..) [bounds = (0.0, 0.25)] v(..) - @variables u(..) [bounds = (-1.0, 1.0), input = true] - constr = [v(1.0) ~ 0.0] - cost = [-x(1.0)] # Maximize the final distance. - @named block = ODESystem( - [D(x(t)) ~ v(t), D(v(t)) ~ u(t)], t; costs = cost, constraints = constr) - block, input_idxs = structural_simplify(block, ([u(t)], [])) - - u0map = [x(t) => 0.0, v(t) => 0.0] - tspan = (0.0, 1.0) - parammap = [u(t) => 0.0] - jprob = JuMPDynamicOptProblem(block, u0map, tspan, parammap; dt = 0.01) - jsol = solve(jprob, Ipopt.Optimizer, constructVerner8, silent = true) - # Linear systems have bang-bang controls - @test is_bangbang(jsol.input_sol, [-1.0], [1.0]) - # Test reached final position. - @test ≈(jsol.sol.u[end][2], 0.25, rtol = 1e-5) - # Test dynamics - @parameters (u_interp::ConstantInterpolation)(..) - @mtkbuild block_ode = ODESystem([D(x(t)) ~ v(t), D(v(t)) ~ u_interp(t)], t) - spline = ctrl_to_spline(jsol.input_sol, ConstantInterpolation) - oprob = ODEProblem(block_ode, u0map, tspan, [u_interp => spline]) - osol = solve(oprob, Vern8(), dt = 0.01, adaptive = false) - @test ≈(jsol.sol.u, osol.u, rtol = 0.05) - - iprob = InfiniteOptDynamicOptProblem(block, u0map, tspan, parammap; dt = 0.01) - isol = solve(iprob, Ipopt.Optimizer; silent = true) - @test is_bangbang(isol.input_sol, [-1.0], [1.0]) - @test ≈(isol.sol.u[end][2], 0.25, rtol = 1e-5) - osol = solve(oprob, ImplicitEuler(); dt = 0.01, adaptive = false) - @test ≈(isol.sol.u, osol.u, rtol = 0.05) - - ################### - ### Bee example ### - ################### - # From Lawrence Evans' notes - @variables w(..) q(..) α(t) [input = true, bounds = (0, 1)] - @parameters b c μ s ν - - tspan = (0, 4) - eqs = [D(w(t)) ~ -μ * w(t) + b * s * α * w(t), - D(q(t)) ~ -ν * q(t) + c * (1 - α) * s * w(t)] - costs = [-q(tspan[2])] - - @named beesys = ODESystem(eqs, t; costs) - beesys, input_idxs = structural_simplify(beesys, ([α], [])) - u0map = [w(t) => 40, q(t) => 2] - pmap = [b => 1, c => 1, μ => 1, s => 1, ν => 1, α => 1] - - jprob = JuMPDynamicOptProblem(beesys, u0map, tspan, pmap, dt = 0.01) - jsol = solve(jprob, Ipopt.Optimizer, constructTsitouras5, silent = true) - @test is_bangbang(jsol.input_sol, [0.0], [1.0]) - iprob = InfiniteOptDynamicOptProblem(beesys, u0map, tspan, pmap, dt = 0.01) - isol = solve(iprob, Ipopt.Optimizer; silent = true) - @test is_bangbang(isol.input_sol, [0.0], [1.0]) - - @parameters (α_interp::LinearInterpolation)(..) - eqs = [D(w(t)) ~ -μ * w(t) + b * s * α_interp(t) * w(t), - D(q(t)) ~ -ν * q(t) + c * (1 - α_interp(t)) * s * w(t)] - @mtkbuild beesys_ode = ODESystem(eqs, t) - oprob = ODEProblem(beesys_ode, - u0map, - tspan, - merge(Dict(pmap), - Dict(α_interp => ctrl_to_spline(jsol.input_sol, LinearInterpolation)))) - osol = solve(oprob, Tsit5(); dt = 0.01, adaptive = false) - @test ≈(osol.u, jsol.sol.u, rtol = 0.01) - osol2 = solve(oprob, ImplicitEuler(); dt = 0.01, adaptive = false) - @test ≈(osol2.u, isol.sol.u, rtol = 0.01) -end - -@testset "Rocket launch" begin - t = M.t_nounits - D = M.D_nounits - - @parameters h_c m₀ h₀ g₀ D_c c Tₘ m_c - @variables h(..) v(..) m(..) [bounds = (m_c, 1)] T(..) [input = true, bounds = (0, Tₘ)] - drag(h, v) = D_c * v^2 * exp(-h_c * (h - h₀) / h₀) - gravity(h) = g₀ * (h₀ / h) - - eqs = [D(h(t)) ~ v(t), - D(v(t)) ~ (T(t) - drag(h(t), v(t))) / m(t) - gravity(h(t)), - D(m(t)) ~ -T(t) / c] - - (ts, te) = (0.0, 0.2) - costs = [-h(te)] - cons = [T(te) ~ 0, m(te) ~ m_c] - @named rocket = ODESystem(eqs, t; costs, constraints = cons) - rocket, input_idxs = structural_simplify(rocket, ([T(t)], [])) - - u0map = [h(t) => h₀, m(t) => m₀, v(t) => 0] - pmap = [ - g₀ => 1, m₀ => 1.0, h_c => 500, c => 0.5 * √(g₀ * h₀), D_c => 0.5 * 620 * m₀ / g₀, - Tₘ => 3.5 * g₀ * m₀, T(t) => 0.0, h₀ => 1, m_c => 0.6] - jprob = JuMPDynamicOptProblem(rocket, u0map, (ts, te), pmap; dt = 0.001, cse = false) - jsol = solve(jprob, Ipopt.Optimizer, constructRadauIIA5, silent = true) - @test jsol.sol.u[end][1] > 1.012 - - iprob = InfiniteOptDynamicOptProblem( - rocket, u0map, (ts, te), pmap; dt = 0.001) - isol = solve(iprob, Ipopt.Optimizer, silent = true) - @test isol.sol.u[end][1] > 1.012 - - # Test solution - @parameters (T_interp::CubicSpline)(..) - eqs = [D(h(t)) ~ v(t), - D(v(t)) ~ (T_interp(t) - drag(h(t), v(t))) / m(t) - gravity(h(t)), - D(m(t)) ~ -T_interp(t) / c] - @mtkbuild rocket_ode = ODESystem(eqs, t) - interpmap = Dict(T_interp => ctrl_to_spline(jsol.input_sol, CubicSpline)) - oprob = ODEProblem(rocket_ode, u0map, (ts, te), merge(Dict(pmap), interpmap)) - osol = solve(oprob, RadauIIA5(); adaptive = false, dt = 0.001) - @test ≈(jsol.sol.u, osol.u, rtol = 0.02) - - interpmap1 = Dict(T_interp => ctrl_to_spline(isol.input_sol, CubicSpline)) - oprob1 = ODEProblem(rocket_ode, u0map, (ts, te), merge(Dict(pmap), interpmap1)) - osol1 = solve(oprob1, ImplicitEuler(); adaptive = false, dt = 0.001) - @test ≈(isol.sol.u, osol1.u, rtol = 0.01) -end - -@testset "Free final time problems" begin - t = M.t_nounits - D = M.D_nounits - - @variables x(..) u(..) [input = true, bounds = (0, 1)] - @parameters tf - eqs = [D(x(t)) ~ -2 + 0.5 * u(t)] - # Integral cost function - costs = [-Symbolics.Integral(t in (0, tf))(x(t) - u(t)), -x(tf)] - consolidate(u) = u[1] + u[2] - @named rocket = ODESystem(eqs, t; costs, consolidate) - rocket, input_idxs = structural_simplify(rocket, ([u(t)], [])) - - u0map = [x(t) => 17.5] - pmap = [u(t) => 0.0, tf => 8] - jprob = JuMPDynamicOptProblem(rocket, u0map, (0, tf), pmap; steps = 201) - jsol = solve(jprob, Ipopt.Optimizer, constructTsitouras5, silent = true) - @test isapprox(jsol.sol.t[end], 10.0, rtol = 1e-3) - - iprob = InfiniteOptDynamicOptProblem(rocket, u0map, (0, tf), pmap; steps = 200) - isol = solve(iprob, Ipopt.Optimizer, silent = true) - @test isapprox(isol.sol.t[end], 10.0, rtol = 1e-3) - - @variables x(..) v(..) - @variables u(..) [bounds = (-1.0, 1.0), input = true] - @parameters tf - constr = [v(tf) ~ 0, x(tf) ~ 0] - cost = [tf] # Minimize time - - @named block = ODESystem( - [D(x(t)) ~ v(t), D(v(t)) ~ u(t)], t; costs = cost, constraints = constr) - block, input_idxs = structural_simplify(block, ([u(t)], [])) - - u0map = [x(t) => 1.0, v(t) => 0.0] - tspan = (0.0, tf) - parammap = [u(t) => 0.0, tf => 1.0] - jprob = JuMPDynamicOptProblem(block, u0map, tspan, parammap; steps = 51) - jsol = solve(jprob, Ipopt.Optimizer, constructVerner8, silent = true) - @test isapprox(jsol.sol.t[end], 2.0, atol = 1e-5) - - iprob = InfiniteOptDynamicOptProblem(block, u0map, tspan, parammap; steps = 51) - isol = solve(iprob, Ipopt.Optimizer, silent = true) - @test isapprox(isol.sol.t[end], 2.0, atol = 1e-5) -end - -@testset "Cart-pole problem" begin - t = M.t_nounits - D = M.D_nounits - # gravity, length, moment of Inertia, drag coeff - @parameters g l mₚ mₖ - @variables x(..) θ(..) u(t) [input = true, bounds = (-10, 10)] - - s = sin(θ(t)) - c = cos(θ(t)) - H = [mₖ+mₚ mₚ*l*c - mₚ*l*c mₚ*l^2] - C = [0 -mₚ*D(θ(t))*l*s - 0 0] - qd = [D(x(t)), D(θ(t))] - G = [0, mₚ * g * l * s] - B = [1, 0] - - tf = 5 - rhss = -H \ Vector(C * qd + G - B * u) - eqs = [D(D(x(t))) ~ rhss[1], D(D(θ(t))) ~ rhss[2]] - cons = [θ(tf) ~ π, x(tf) ~ 0, D(θ(tf)) ~ 0, D(x(tf)) ~ 0] - costs = [Symbolics.Integral(t in (0, tf))(u^2)] - tspan = (0, tf) - - @named cartpole = ODESystem(eqs, t; costs, constraints = cons) - cartpole, input_idxs = structural_simplify(cartpole, ([u], [])) - - u0map = [D(x(t)) => 0.0, D(θ(t)) => 0.0, θ(t) => 0.0, x(t) => 0.0] - pmap = [mₖ => 1.0, mₚ => 0.2, l => 0.5, g => 9.81, u => 0] - jprob = JuMPDynamicOptProblem(cartpole, u0map, tspan, pmap; dt = 0.04) - jsol = solve(jprob, Ipopt.Optimizer, constructRK4, silent = true) - @test jsol.sol.u[end] ≈ [π, 0, 0, 0] - - iprob = InfiniteOptDynamicOptProblem(cartpole, u0map, tspan, pmap; dt = 0.04) - isol = solve(iprob, Ipopt.Optimizer, silent = true) - @test isol.sol.u[end] ≈ [π, 0, 0, 0] +@testset "Optimal control problem" begin end From d075449f86139c64d6988f75ee75f9a667c679ce Mon Sep 17 00:00:00 2001 From: vyudu Date: Wed, 23 Apr 2025 19:09:18 -0400 Subject: [PATCH 1487/2176] test: more test fixes --- ext/MTKJuMPControlExt.jl | 153 ++++++++++++++++++++++++++----- src/systems/diffeqs/odesystem.jl | 1 + src/systems/systems.jl | 5 + test/runtests.jl | 11 +++ 4 files changed, 146 insertions(+), 24 deletions(-) diff --git a/ext/MTKJuMPControlExt.jl b/ext/MTKJuMPControlExt.jl index 1e788b05ec..631ec71ff0 100644 --- a/ext/MTKJuMPControlExt.jl +++ b/ext/MTKJuMPControlExt.jl @@ -18,6 +18,21 @@ struct JuMPControlProblem{uType, tType, P, F, K} end end +struct InfiniteOptControlProblem{uType, tType, isinplace, P, F, K} <: + AbstractOptimalControlProblem{uType, tType, isinplace} + f::F + u0::uType + tspan::tType + p::P + model::InfiniteModel + kwargs::K + + function InfiniteOptControlProblem(f, u0, tspan, p, model, kwargs...) + new{typeof(u0), typeof(tspan), SciMLBase.isinplace(f), + typeof(p), typeof(f), typeof(kwargs)}(f, u0, tspan, p, model, kwargs) + end +end + """ JuMPControlProblem(sys::ODESystem, u0, tspan, p; dt) @@ -33,20 +48,40 @@ The constraints are: - The set of user constraints passed to the ODESystem via `constraints` - The solver constraints that encode the time-stepping used by the solver """ -function MTK.JuMPControlProblem(sys::ODESystem, u0map, tspan, pmap; dt = error("dt must be provided for JuMPControlProblem."), kwargs...) - ts = tspan[1] - te = tspan[2] - steps = ts:dt:te - ctrls = controls(sys) - states = unknowns(sys) - constraintsys = MTK.get_constraintsystem(sys) +function MTK.JuMPControlProblem(sys::ODESystem, u0map, tspan, pmap; + dt = nothing, + steps = nothing, + guesses = Dict(), kwargs...) + MTK.warn_overdetermined(sys, u0map) + _u0map = has_alg_eqs(sys) ? u0map : merge(Dict(u0map), Dict(guesses)) + f, u0, p = MTK.process_SciMLProblem(ODEInputFunction, sys, _u0map, pmap; + t = tspan !== nothing ? tspan[1] : tspan, kwargs...) if !isnothing(constraintsys) (length(constraints(constraintsys)) + length(u0map) > length(sts)) && @warn "The JuMPControlProblem is overdetermined. The total number of conditions (# constraints + # fixed initial values given by u0map) exceeds the total number of states. The solvers will default to doing a nonlinear least-squares optimization." end - f, u0, p = MTK.process_SciMLProblem(ODEFunction, sys, u0map, pmap; + JuMPControlProblem(f, u0, tspan, p, model, kwargs...) +end + +""" + InfiniteOptControlProblem(sys::ODESystem, u0map, tspan, pmap; dt) + +Convert an ODESystem representing an optimal control system into a InfiniteOpt model +for solving using optimization. Must provide `dt` for determining the length +of the interpolation arrays. + +Related to `JuMPControlProblem`, but directly adds the differential equations +of the system as derivative constraints, rather than using a solver tableau. +""" +function MTK.InfiniteOptControlProblem(sys::ODESystem, u0map, tspan, pmap; + dt = nothing, + steps = nothing, + guesses = Dict(), kwargs...) + MTK.warn_overdetermined(sys, u0map) + _u0map = has_alg_eqs(sys) ? u0map : merge(Dict(u0map), Dict(guesses)) + f, u0, p = MTK.process_SciMLProblem(ODEInputFunction, sys, _u0map, pmap; t = tspan !== nothing ? tspan[1] : tspan, kwargs...) model = InfiniteModel() @@ -57,9 +92,22 @@ function MTK.JuMPControlProblem(sys::ODESystem, u0map, tspan, pmap; dt = error(" add_jump_cost_function!(model, sys) add_user_constraints!(model, sys) + @infinite_parameter(model, t in [tspan[1], tspan[2]], num_supports=steps) + @variable(model, U[i = 1:length(states)], Infinite(t), start=u0[i]) + c0 = MTK.value.([pmap[c] for c in ctrls]) + @variable(model, V[i = 1:length(ctrls)], Infinite(t), start=c0[i]) + + set_jump_bounds!(model, sys, pmap) + add_jump_cost_function!(model, sys, (tspan[1], tspan[2]), pmap; is_free_t) + add_user_constraints!(model, sys, pmap; is_free_t) + stidxmap = Dict([v => i for (i, v) in enumerate(states)]) - u0_idxs = has_alg_eqs(sys) ? collect(1:length(states)) : [stidxmap[k] for (k, v) in u0map] - add_initial_constraints!(model, u0, u0_idxs, tspan) + u0map = Dict([MTK.default_toterm(MTK.value(k)) => v for (k, v) in u0map]) + u0_idxs = has_alg_eqs(sys) ? collect(1:length(states)) : + [stidxmap[MTK.default_toterm(k)] for (k, v) in u0map] + add_initial_constraints!(model, u0, u0_idxs, tspan[1]) + return model +end JuMPControlProblem(f, u0, tspan, p, model, kwargs...) end @@ -115,14 +163,10 @@ function add_user_constraints!(model, sys) jconstraints = Symbolics.substitute(jconstraints, Dict(x(t) => subval)) end - for ct in controls(sys) - p = operation(ct) - t = only(arguments(ct)) - idx = cidxmap[p(iv)] - subval = isequal(t, iv) ? model[:V][idx] : model[:V][idx](t) - jconstraints = Symbolics.substitute(jconstraints, Dict(p(t) => subval)) - end - + auxmap = Dict([u => MTK.default_toterm(MTK.value(u)) for u in unknowns(conssys)]) + jconstraints = substitute_jump_vars(model, sys, pmap, jconstraints; auxmap) + + # Substitute to-term'd variables for (i, cons) in enumerate(jconstraints) if cons isa Equation @constraint(model, user[i], cons.lhs - cons.rhs == 0) @@ -139,6 +183,31 @@ function add_initial_constraints!(model, u0, u0_idxs, tspan) @constraint(model, init_u0_idx[i in u0_idxs], model[:U][i](ts) == u0[i]) end +function substitute_jump_vars(model, sys, pmap, exprs; auxmap = Dict()) + iv = MTK.get_iv(sys) + sts = unknowns(sys) + cts = MTK.unbound_inputs(sys) + U = model[:U] + V = model[:V] + exprs = map(c -> Symbolics.fixpoint_sub(c, auxmap), exprs) + + # for variables like x(t) + whole_interval_map = Dict([[v => U[i] for (i, v) in enumerate(sts)]; + [v => V[i] for (i, v) in enumerate(cts)]]) + exprs = map(c -> Symbolics.fixpoint_sub(c, whole_interval_map), exprs) + + # for variables like x(1.0) + x_ops = [MTK.operation(MTK.unwrap(st)) for st in sts] + c_ops = [MTK.operation(MTK.unwrap(ct)) for ct in cts] + fixed_t_map = Dict([[x_ops[i] => U[i] for i in 1:length(U)]; + [c_ops[i] => V[i] for i in 1:length(V)]]) + + exprs = map(c -> Symbolics.fixpoint_sub(c, fixed_t_map), exprs) + + exprs = map(c -> Symbolics.fixpoint_sub(c, Dict(pmap)), exprs) + exprs +end + is_explicit(tableau) = tableau isa DiffEqDevTools.ExplicitRKTableau function add_solve_constraints!(prob, tableau) @@ -148,7 +217,10 @@ function add_solve_constraints!(prob, tableau) model = prob.model f = prob.f p = prob.p - tsteps = supports(model[:t]) + + t = model[:t] + tsteps = supports(t) + tmax = tsteps[end] pop!(tsteps) dt = tsteps[2] - tsteps[1] @@ -167,17 +239,19 @@ function add_solve_constraints!(prob, tableau) @constraint(model, [n = 1:nᵤ], U[n](τ) + ΔU[n] == U[n](τ + dt)) empty!(K) end + @show num_variables(model) else @variable(model, K[1:length(a), 1:nᵤ], Infinite(t), start = tsteps[1]) for τ in tsteps ΔUs = [A * K(τ)] for (i, h) in enumerate(c) - ΔU = ΔUs[i] - Uₙ = [U[j](τ) + ΔU[j](τ)*dt for j in 1:nᵤ] - @constraint(model, K[i](τ) == f(Uₙ, p, τ + h*dt)) + ΔU = @view ΔUs[i, :] + Uₙ = U + ΔU * dt + @constraint(model, [j = 1:nᵤ], K[i, j](τ)==(tₛ * f(Uₙ, V, p, τ + h * dt)[j]), + DomainRestrictions(t => min(τ + h * dt, tmax)), base_name="solve_K($τ)") end - ΔU = sum([α[i] * K[i] for i in 1:length(α)]) - @constraint(model, U(τ) + dot(α, K(τ)) == U(τ + dt)) + @constraint(model, [n = 1:nᵤ], U[n](τ) + ΔU_tot[n]==U[n](min(τ + dt, tmax)), + DomainRestrictions(t => τ), base_name="solve_U($τ)") end end end @@ -202,6 +276,37 @@ function DiffEqBase.solve(prob::JuMPControlProblem, jump_solver, ode_solver::Sym ts = supports(model[:t]) add_solve_constraints!(prob, tableau) + # Unregister current solver constraints + for con in all_constraints(model) + if occursin("solve", JuMP.name(con)) + unregister(model, Symbol(JuMP.name(con))) + delete(model, con) + end + end + unregister(model, :K) + for var in all_variables(model) + if occursin("K", JuMP.name(var)) + unregister(model, Symbol(JuMP.name(var))) + delete(model, var) + end + end + add_jump_solve_constraints!(prob, tableau; is_free_t = haskey(model, :tf)) + _solve(prob, jump_solver, ode_solver) +end + +""" +`derivative_method` kwarg refers to the method used by InfiniteOpt to compute derivatives. The list of possible options can be found at https://infiniteopt.github.io/InfiniteOpt.jl/stable/guide/derivative/. Defaults to FiniteDifference(Backward()). +""" +function DiffEqBase.solve(prob::InfiniteOptControlProblem, jump_solver; + derivative_method = InfiniteOpt.FiniteDifference(Backward()), silent = false) + model = prob.model + silent && set_silent(model) + set_derivative_method(model[:t], derivative_method) + _solve(prob, jump_solver, derivative_method) +end + +function _solve(prob::AbstractOptimalControlProblem, jump_solver, solver) + model = prob.model set_optimizer(model, jump_solver) optimize!(model) diff --git a/src/systems/diffeqs/odesystem.jl b/src/systems/diffeqs/odesystem.jl index 4a13d7ccf1..c13670b5c7 100644 --- a/src/systems/diffeqs/odesystem.jl +++ b/src/systems/diffeqs/odesystem.jl @@ -767,6 +767,7 @@ function process_constraint_system( collect_vars!(constraintsts, constraintps, cons, iv) union!(constraintsts, collect_applied_operators(cons, Differential)) end + @show constraintsts # Validate the states. validate_vars_and_find_ps!(constraintsts, constraintps, sts, iv) diff --git a/src/systems/systems.jl b/src/systems/systems.jl index 52f93afb9b..9da7249300 100644 --- a/src/systems/systems.jl +++ b/src/systems/systems.jl @@ -163,6 +163,11 @@ function __structural_simplify( end end +function toterm_auxsystems(system::ODESystem) + constraints = system.constraintsystem.constraints + +end + """ $(TYPEDSIGNATURES) diff --git a/test/runtests.jl b/test/runtests.jl index 80339c6606..620394cc9e 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -22,6 +22,12 @@ function activate_downstream_env() Pkg.instantiate() end +function activate_dynamic_optimization_env() + Pkg.activate("dynamic_optimization") + Pkg.develop(PackageSpec(path = dirname(@__DIR__))) + Pkg.instantiate() +end + @time begin if GROUP == "All" || GROUP == "InterfaceI" @testset "InterfaceI" begin @@ -143,4 +149,9 @@ end @safetestset "BifurcationKit Extension Test" include("extensions/bifurcationkit.jl") @safetestset "InfiniteOpt Extension Test" include("extensions/test_infiniteopt.jl") end + + if GROUP == "All" || GROUP == "Dynamic Optimization" + activate_dynamic_optimization_env() + @safetestset "JuMP Collocation Solvers" include("dynamic_optimization/jump_control") + end end From e983d4f9c2ac7a2eda5e1c75d44624921f0b31df Mon Sep 17 00:00:00 2001 From: vyudu Date: Fri, 25 Apr 2025 14:56:55 -0400 Subject: [PATCH 1488/2176] more test fixes --- ext/MTKJuMPControlExt.jl | 11 ++++++----- src/systems/diffeqs/odesystem.jl | 1 - 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/ext/MTKJuMPControlExt.jl b/ext/MTKJuMPControlExt.jl index 631ec71ff0..de3aa96269 100644 --- a/ext/MTKJuMPControlExt.jl +++ b/ext/MTKJuMPControlExt.jl @@ -239,16 +239,17 @@ function add_solve_constraints!(prob, tableau) @constraint(model, [n = 1:nᵤ], U[n](τ) + ΔU[n] == U[n](τ + dt)) empty!(K) end - @show num_variables(model) else - @variable(model, K[1:length(a), 1:nᵤ], Infinite(t), start = tsteps[1]) + @variable(model, K[1:length(α), 1:nᵤ], Infinite(t)) + ΔUs = A * K + ΔU_tot = dt * (K' * α) for τ in tsteps ΔUs = [A * K(τ)] for (i, h) in enumerate(c) ΔU = @view ΔUs[i, :] - Uₙ = U + ΔU * dt - @constraint(model, [j = 1:nᵤ], K[i, j](τ)==(tₛ * f(Uₙ, V, p, τ + h * dt)[j]), - DomainRestrictions(t => min(τ + h * dt, tmax)), base_name="solve_K($τ)") + Uₙ = U + ΔU * h * dt + @constraint(model, [j = 1:nᵤ], K[i, j]==(tₛ * f(Uₙ, V, p, τ + h * dt)[j]), + DomainRestrictions(t => τ), base_name="solve_K$i($τ)") end @constraint(model, [n = 1:nᵤ], U[n](τ) + ΔU_tot[n]==U[n](min(τ + dt, tmax)), DomainRestrictions(t => τ), base_name="solve_U($τ)") diff --git a/src/systems/diffeqs/odesystem.jl b/src/systems/diffeqs/odesystem.jl index c13670b5c7..4a13d7ccf1 100644 --- a/src/systems/diffeqs/odesystem.jl +++ b/src/systems/diffeqs/odesystem.jl @@ -767,7 +767,6 @@ function process_constraint_system( collect_vars!(constraintsts, constraintps, cons, iv) union!(constraintsts, collect_applied_operators(cons, Differential)) end - @show constraintsts # Validate the states. validate_vars_and_find_ps!(constraintsts, constraintps, sts, iv) From d558fabb1383176545e95d89be3a6362ce7a5680 Mon Sep 17 00:00:00 2001 From: vyudu Date: Fri, 25 Apr 2025 23:06:54 -0400 Subject: [PATCH 1489/2176] test fixes --- src/structural_transformation/utils.jl | 1 + 1 file changed, 1 insertion(+) diff --git a/src/structural_transformation/utils.jl b/src/structural_transformation/utils.jl index fa02f0b3cd..0d8a12a06d 100644 --- a/src/structural_transformation/utils.jl +++ b/src/structural_transformation/utils.jl @@ -96,6 +96,7 @@ function check_consistency(state::TransformationState, orig_inputs; nothrow = fa fullvars = get_fullvars(state) neqs = n_concrete_eqs(state) @unpack graph, var_to_diff = state.structure + @show equations(state.sys) highest_vars = computed_highest_diff_variables(complete!(state.structure)) n_highest_vars = 0 for (v, h) in enumerate(highest_vars) From 4ac62b4905662aa6e9c367708bec0881a14ebacb Mon Sep 17 00:00:00 2001 From: vyudu Date: Mon, 28 Apr 2025 15:20:43 -0400 Subject: [PATCH 1490/2176] fix more tests --- src/structural_transformation/utils.jl | 1 - test/runtests.jl | 11 ----------- 2 files changed, 12 deletions(-) diff --git a/src/structural_transformation/utils.jl b/src/structural_transformation/utils.jl index 0d8a12a06d..fa02f0b3cd 100644 --- a/src/structural_transformation/utils.jl +++ b/src/structural_transformation/utils.jl @@ -96,7 +96,6 @@ function check_consistency(state::TransformationState, orig_inputs; nothrow = fa fullvars = get_fullvars(state) neqs = n_concrete_eqs(state) @unpack graph, var_to_diff = state.structure - @show equations(state.sys) highest_vars = computed_highest_diff_variables(complete!(state.structure)) n_highest_vars = 0 for (v, h) in enumerate(highest_vars) diff --git a/test/runtests.jl b/test/runtests.jl index 620394cc9e..80339c6606 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -22,12 +22,6 @@ function activate_downstream_env() Pkg.instantiate() end -function activate_dynamic_optimization_env() - Pkg.activate("dynamic_optimization") - Pkg.develop(PackageSpec(path = dirname(@__DIR__))) - Pkg.instantiate() -end - @time begin if GROUP == "All" || GROUP == "InterfaceI" @testset "InterfaceI" begin @@ -149,9 +143,4 @@ end @safetestset "BifurcationKit Extension Test" include("extensions/bifurcationkit.jl") @safetestset "InfiniteOpt Extension Test" include("extensions/test_infiniteopt.jl") end - - if GROUP == "All" || GROUP == "Dynamic Optimization" - activate_dynamic_optimization_env() - @safetestset "JuMP Collocation Solvers" include("dynamic_optimization/jump_control") - end end From b1bc16094f49103786ef265724a325f6b621b8a0 Mon Sep 17 00:00:00 2001 From: vyudu Date: Fri, 2 May 2025 12:47:39 -0400 Subject: [PATCH 1491/2176] refactor: start systems test file --- ext/MTKCasADiDynamicOptExt.jl | 115 +++++++++++++++++++++++++ test/downstream/dynamic_opt_systems.jl | 19 ++++ 2 files changed, 134 insertions(+) create mode 100644 ext/MTKCasADiDynamicOptExt.jl create mode 100644 test/downstream/dynamic_opt_systems.jl diff --git a/ext/MTKCasADiDynamicOptExt.jl b/ext/MTKCasADiDynamicOptExt.jl new file mode 100644 index 0000000000..af2067896a --- /dev/null +++ b/ext/MTKCasADiDynamicOptExt.jl @@ -0,0 +1,115 @@ +module MTKCasADiDynamicOptExt +using ModelingToolkit +using CasADi +using DiffEqDevTools, DiffEqBase +using DataInterpolations +const MTK = MOdelingToolkit + +struct CasADiDynamicOptProblem{uType, tType, isinplace, P, F, K} <: + AbstractDynamicOptProblem{uType, tType, isinplace} + f::F + u0::uType + tspan::tType + p::P + model::Opti + kwargs::K + + function CasADiDynamicOptProblem(f, u0, tspan, p, model, kwargs...) + new{typeof(u0), typeof(tspan), SciMLBase.isinplace(f, 5), + typeof(p), typeof(f), typeof(kwargs)}(f, u0, tspan, p, model, kwargs) + end +end + +struct CasADiModel + opti::Opti + U::MX + V::MX +end + +struct TimedMX +end + +function MTK.CasADiDynamicOptProblem(sys::ODESystem, u0map, tspan, pmap; + dt = nothing, + steps = nothing, + guesses = Dict(), kwargs...) + MTK.warn_overdetermined(sys, u0map) + _u0map = has_alg_eqs(sys) ? u0map : merge(Dict(u0map), Dict(guesses)) + f, u0, p = MTK.process_SciMLProblem(ODEInputFunction, sys, _u0map, pmap; + t = tspan !== nothing ? tspan[1] : tspan, kwargs...) + + pmap = Dict{Any, Any}(pmap) + steps, is_free_t = MTK.process_tspan(tspan, dt, steps) + model = init_model() +end + +function init_model(sys, tspan, steps, u0map, pmap, u0; is_free_t) + ctrls = MTK.unbound_inputs(sys) + states = unknowns(sys) + model = CasADi.Opti() + + U = CasADi.variable!(model, length(states), steps) + V = CasADi.variable!(model, length(ctrls), steps) +end + +function add_initial_constraints!() + +end + +function add_user_constraints!(model::CasADiModel, sys, pmap; is_free_t = false) + +end + +function add_cost_function!(model) + +end + +function add_solve_constraints!(prob, tableau; is_free_t) + A = tableau.A + α = tableau.α + c = tableau.c + model = prob.model + f = prob.f + p = prob.p + + opti = model.opti + t = model[:t] + tsteps = supports(t) + tmax = tsteps[end] + pop!(tsteps) + tₛ = is_free_t ? model[:tf] : 1 + dt = tsteps[2] - tsteps[1] + + U = model.U + V = model.V + nᵤ = length(U) + nᵥ = length(V) + + if is_explicit(tableau) + K = Any[] + for τ in tsteps + for (i, h) in enumerate(c) + ΔU = sum([A[i, j] * K[j] for j in 1:(i - 1)], init = zeros(nᵤ)) + Uₙ = [U[i](τ) + ΔU[i] * dt for i in 1:nᵤ] + Vₙ = [V[i](τ) for i in 1:nᵥ] + Kₙ = tₛ * f(Uₙ, Vₙ, p, τ + h * dt) # scale the time + push!(K, Kₙ) + end + ΔU = dt * sum([α[i] * K[i] for i in 1:length(α)]) + subject_to!(model, U[n](τ) + ΔU[n]==U[n](τ + dt)) + empty!(K) + end + else + end +end + +function DiffEqBase.solve(prob::CasADiDynamicOptProblem, solver::Union{String, Symbol}, ode_solver::Union{String, Symbol}; silent = false) + model = prob.model + opti = model.opti + + solver!(opti, solver) + sol = solve(opti) + DynamicOptSolution(model, sol, input_sol) +end + +end diff --git a/test/downstream/dynamic_opt_systems.jl b/test/downstream/dynamic_opt_systems.jl new file mode 100644 index 0000000000..30d8feb1cd --- /dev/null +++ b/test/downstream/dynamic_opt_systems.jl @@ -0,0 +1,19 @@ +function lotkavolterra() + +end + +function () + +end + +function rocket_fft() + +end + +function rocket() + +end + +function cartpole() + +end From 85e3ed666eab6943caade2465f9309d83afefb59 Mon Sep 17 00:00:00 2001 From: vyudu Date: Fri, 2 May 2025 20:59:04 -0400 Subject: [PATCH 1492/2176] feat: implementation for CasADiProblem --- Project.toml | 4 + ext/MTKCasADiDynamicOptExt.jl | 229 +++++++++++++++++++++---- test/downstream/dynamic_opt_systems.jl | 28 ++- 3 files changed, 219 insertions(+), 42 deletions(-) diff --git a/Project.toml b/Project.toml index b29b2b9566..2ea71a8ed3 100644 --- a/Project.toml +++ b/Project.toml @@ -65,7 +65,9 @@ Unitful = "1986cc42-f94f-5a68-af5c-568840ba703d" [weakdeps] BifurcationKit = "0f109fa4-8a5d-4b75-95aa-f515264e7665" +CasADi = "c49709b8-5c63-11e9-2fb2-69db5844192f" ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4" +DataInterpolations = "82cc6244-b520-54b8-b5a6-8a565e85f1d0" DeepDiffs = "ab62b9b5-e342-54a8-a765-a90f495de1a6" DiffEqDevTools = "f3b72e0c-5b89-59e1-b016-84e28bfd966d" FMI = "14a09403-18e3-468f-ad8a-74f8dda2d9ac" @@ -75,6 +77,7 @@ LabelledArrays = "2ee39098-c373-598a-b85f-a56591580800" [extensions] MTKBifurcationKitExt = "BifurcationKit" +MTKCasADiDynamicOptExt = ["CasADi", "DataInterpolations", "DiffEqDevTools"] MTKChainRulesCoreExt = "ChainRulesCore" MTKDeepDiffsExt = "DeepDiffs" MTKFMIExt = "FMI" @@ -89,6 +92,7 @@ BifurcationKit = "0.4" BlockArrays = "1.1" BoundaryValueDiffEqAscher = "1.1.0" BoundaryValueDiffEqMIRK = "1.4.0" +CasADi = "1.0.0" ChainRulesCore = "1" Combinatorics = "1" CommonSolve = "0.2.4" diff --git a/ext/MTKCasADiDynamicOptExt.jl b/ext/MTKCasADiDynamicOptExt.jl index af2067896a..e16ce1b6b5 100644 --- a/ext/MTKCasADiDynamicOptExt.jl +++ b/ext/MTKCasADiDynamicOptExt.jl @@ -3,6 +3,7 @@ using ModelingToolkit using CasADi using DiffEqDevTools, DiffEqBase using DataInterpolations +using UnPack const MTK = MOdelingToolkit struct CasADiDynamicOptProblem{uType, tType, isinplace, P, F, K} <: @@ -22,16 +23,15 @@ end struct CasADiModel opti::Opti - U::MX - V::MX -end - -struct TimedMX + U::AbstractInterpolation + V::AbstractInterpolation + tₛ::Union{Number, MX} end function MTK.CasADiDynamicOptProblem(sys::ODESystem, u0map, tspan, pmap; dt = nothing, - steps = nothing, + steps = nothing, + interpolation_method::AbstractInterpolation = LinearInterpolation, guesses = Dict(), kwargs...) MTK.warn_overdetermined(sys, u0map) _u0map = has_alg_eqs(sys) ? u0map : merge(Dict(u0map), Dict(guesses)) @@ -43,73 +43,228 @@ function MTK.CasADiDynamicOptProblem(sys::ODESystem, u0map, tspan, pmap; model = init_model() end -function init_model(sys, tspan, steps, u0map, pmap, u0; is_free_t) +function init_model(sys, tspan, steps, u0map, pmap, u0; is_free_t = false, interp_type::AbstractInterpolation) ctrls = MTK.unbound_inputs(sys) states = unknowns(sys) - model = CasADi.Opti() + opti = CasADi.Opti() + + if is_free_t + (ts_sym, te_sym) = tspan + MTK.symbolic_type(ts_sym) !== MTK.NotSymbolic() && + error("Free initial time problems are not currently supported.") + tₛ = variable!(opti) + tsteps = LinRange(0, 1, steps) + else + tₛ = 1 + tsteps = LinRange(tspan[1], tspan[2], steps) + end - U = CasADi.variable!(model, length(states), steps) - V = CasADi.variable!(model, length(ctrls), steps) + U = CasADi.variable!(opti, length(states), steps) + V = CasADi.variable!(opti, length(ctrls), steps) + + U_interp = interp_type(Matrix(U), tsteps) + V_interp = interp_type(Matrix(V), tsteps) + + CasADiModel(opti, U_interp, V_interp, tₛ) end -function add_initial_constraints!() - +function set_casadi_bounds!(model, sys, pmap) + @unpack opti, U, V = model + for (i, u) in enumerate(unknowns(sys)) + if MTK.hasbounds(u) + lo, hi = MTK.getbounds(u) + subject_to!(opti, lo <= U[i, :] <= hi) + end + end + for (i, v) in enumerate(MTK.unbound_inputs(sys)) + if MTK.hasbounds(v) + lo, hi = MTK.getbounds(v) + subject_to!(opti, lo <= V[i, :] <= hi) + end + end +end + +function add_initial_constraints!(model::CasADiModel, u0, u0_idxs, ts) + @unpack opti, U = model + for i in u0_idxs + subject_to!(opti, U.u[i, 1] == u0[i]) + end end function add_user_constraints!(model::CasADiModel, sys, pmap; is_free_t = false) - + @unpack opti, U, V, tₛ = model + + iv = get_iv(sys) + conssys = MTK.get_constraintsystem(sys) + jconstraints = isnothing(conssys) ? nothing : MTK.get_constraints(conssys) + (isnothing(jconstraints) || isempty(jconstraints)) && return nothing + + stidxmap = Dict([v => i for (i, v) in enumerate(sts)]) + pidxmap = Dict([v => i for (i, v) in enumerate(ps)]) + cons_unknowns = map(MTK.default_toterm, unknowns(conssys)) + for st in cons_unknowns + x = operation(st) + t = only(argments(st)) + idx = stidxmap[x(iv)] + + jconstraints = map(c -> Symbolics.substitute(c, Dict(x(t) => U(t)[idx])), jconstraints) + end + jconstraints = substitute_casadi_vars(model, sys, pmap, jconstraints) + + for (i, cons) in enumerate(jconstraints) + if cons isa Equation + subject_to!(opti, cons.lhs - cons.rhs==0) + elseif cons.relational_op === Symbolics.geq + subject_to!(model, cons.lhs - cons.rhs≥0) + else + subject_to!(model, cons.lhs - cons.rhs≤0) + end + end end -function add_cost_function!(model) +function add_cost_function!(model::CasADiModel, sys, tspan, pmap) + @unpack opti, U, V, tₛ = model + jcosts = MTK.get_costs(sys) + consolidate = MTK.get_consolidate(sys) + + if isnothing(jcosts) || isempty(jcosts) + minimize!(opti, 0) + return + end + stidxmap = Dict([v => i for (i, v) in enumerate(sts)]) + pidxmap = Dict([v => i for (i, v) in enumerate(ps)]) + for i in 1:length(jcosts) + vars = vars(jcosts[i]) + for st in vars + x = operation(st) + t = only(arguments(st)) + t isa Union{Num, MTK.Symbolic} && continue + idx = stidxmap[x(iv)] + jcosts[i] = Symbolics.substitute(jcosts[i], Dict(x(t) => U(t)[idx])) + end + end + jcosts = substitute_casadi_vars(model::CasADiModel, sys, pmap, jcosts; auxmap) + jcosts = map( + c -> Symbolics.substitute(c, MTK.∫() => Symbolics.Integral(iv in tspan)), jcosts) + + dt = U.t[2] - U.t[1] + intmap = Dict() + for int in MTK.collect_applied_operators(jcosts, Symbolics.Integral) + op = MTK.operation(int) + arg = only(arguments(MTK.value(int))) + lo, hi = (op.domain.domain.left, op.domain.domain.right) + (lo, hi) !== tspan && error("Non-whole interval bounds for integrals are not currently supported.") + intmap[int] = dt * tₛ * sum(arg) + end + jcosts = map(c -> Symbolics.substitute(c, intmap), jcosts) + minimize!(opti, consolidate(jcosts)) +end + +function substitute_casadi_vars(model::CasADiModel, sys, pmap, exprs; auxmap = Dict()) + @unpack opti, U, V = model + iv = MTK.get_iv(sys) + sts = unknowns(sys) + cts = MTK.unbound_inputs(sys) + + x_ops = [MTK.operation(MTK.unwrap(st)) for st in sts] + c_ops = [MTK.operation(MTK.unwrap(ct)) for ct in cts] + + exprs = map(c -> Symbolics.fixpoint_sub(c, auxmap), exprs) + exprs = map(c -> Symbolics.fixpoint_sub(c, Dict(pmap)), exprs) + + # for variables like x(t) + whole_interval_map = Dict([[v => U.u[i, :] for (i, v) in enumerate(sts)]; + [v => V.u[i, :] for (i, v) in enumerate(cts)]]) + exprs = map(c -> Symbolics.fixpoint_sub(c, whole_interval_map), exprs) + exprs end function add_solve_constraints!(prob, tableau; is_free_t) - A = tableau.A - α = tableau.α - c = tableau.c - model = prob.model - f = prob.f - p = prob.p + @unpack A, α, c = tableau + @unpack model, f, p = prob + @unpack opti, U, V, tₛ = model - opti = model.opti - t = model[:t] - tsteps = supports(t) - tmax = tsteps[end] - pop!(tsteps) - tₛ = is_free_t ? model[:tf] : 1 + tsteps = U.t dt = tsteps[2] - tsteps[1] - U = model.U - V = model.V nᵤ = length(U) nᵥ = length(V) if is_explicit(tableau) K = Any[] - for τ in tsteps + for k in 1:length(tsteps)-1 for (i, h) in enumerate(c) ΔU = sum([A[i, j] * K[j] for j in 1:(i - 1)], init = zeros(nᵤ)) - Uₙ = [U[i](τ) + ΔU[i] * dt for i in 1:nᵤ] - Vₙ = [V[i](τ) for i in 1:nᵥ] + Uₙ = U.u[:, k] + ΔU*dt + Vₙ = V.u[:, k] Kₙ = tₛ * f(Uₙ, Vₙ, p, τ + h * dt) # scale the time push!(K, Kₙ) end ΔU = dt * sum([α[i] * K[i] for i in 1:length(α)]) - subject_to!(model, U[n](τ) + ΔU[n]==U[n](τ + dt)) - empty!(K) + subject_to!(opti, U.u[:, k] + ΔU == U.u[:, k+1]) end else + ΔU_tot = dt * (K' * α) + for k in 1:length(tsteps)-1 + Kᵢ = variable!(opti, length(α), nᵤ) + ΔUs = A * Kᵢ # the stepsize at each stage of the implicit method + for (i, h) in enumerate(c) + ΔU = @view ΔUs[i, :] + Uₙ = U.u[:,k] + ΔU + Vₙ = V.u[:,k] + subject_to!(opti, K[i,:] == tₛ * f(Uₙ, Vₙ, p, τ + h*dt)) + end + ΔU_tot = dt*(Kᵢ'*α) + subject_to!(opti, U.u[:, k] + ΔU_tot == U.u[:,k+1]) + end end end -function DiffEqBase.solve(prob::CasADiDynamicOptProblem, solver::Union{String, Symbol}, ode_solver::Union{String, Symbol}; silent = false) +is_explicit(tableau) = tableau isa DiffEqDevTools.ExplicitRKTableau + +""" + solve(prob::CasADiDynamicOptProblem, casadi_solver, ode_solver; plugin_options, solver_options) + +`plugin_options` and `solver_options` get propagated to the Opti object in CasADi. +""" +function DiffEqBase.solve(prob::CasADiDynamicOptProblem, solver::Union{String, Symbol}, ode_solver::Union{String, Symbol}; plugin_options::Dict = Dict(), solver_options::Dict = Dict(), silent = false) model = prob.model opti = model.opti - solver!(opti, solver) - sol = solve(opti) - DynamicOptSolution(model, sol, input_sol) + solver!(opti, solver, plugin_options, solver_options) + add_casadi_solve_constraints!(prob, tableau) + solver!(cmodel, "$solver", plugin_options, solver_options) + + failed = false + try + sol = solve(opti) + value_getter = x -> CasADi.value(sol, x) + catch ErrorException + value_getter = x -> CasADi.debug_value(opti, x) + failed = true + continue + end + + ts = value_getter(tₛ) * U.t + U_vals = value_getter(U) + U_vals = [[U_vals[i][j] for i in 1:length(U_vals)] for j in 1:length(ts)] + sol = DiffEqBase.build_solution(prob, ode_solver, ts, U_vals) + + input_sol = nothing + if !isempty(V) + V_vals = value_getter(V) + V_vals = [[V_vals[i][j] for i in 1:length(V_vals)] for j in 1:length(ts)] + input_sol = DiffEqBase.build_solution(prob, ode_solver, ts, V_vals) + end + + if failed + sol = SciMLBase.solution_new_retcode(sol, SciMLBase.ReturnCode.ConvergenceFailure) + !isnothing(input_sol) && (input_sol = SciMLBase.solution_new_retcode( + input_sol, SciMLBase.ReturnCode.ConvergenceFailure)) + end + + DynamicOptSolution(cmodel, sol, input_sol) end end diff --git a/test/downstream/dynamic_opt_systems.jl b/test/downstream/dynamic_opt_systems.jl index 30d8feb1cd..f13b6446ef 100644 --- a/test/downstream/dynamic_opt_systems.jl +++ b/test/downstream/dynamic_opt_systems.jl @@ -1,9 +1,27 @@ -function lotkavolterra() - -end +function build_lotkavolterra(; with_constraint = false) + @parameters α=1.5 β=1.0 γ=3.0 δ=1.0 + @variables x(..) y(..) + t = M.t_nounits + D = M.D_nounits -function () - + eqs = [D(x(t)) ~ α * x(t) - β * x(t) * y(t), + D(y(t)) ~ -γ * y(t) + δ * x(t) * y(t)] + + tspan = (0.0, 1.0) + parammap = [α => 1.5, β => 1.0, γ => 3.0, δ => 1.0] + + if with_constraint + constr = [x(0.6) ~ 3.5, x(0.3) ~ 7.0] + guess = [x(t) => 4.0, y(t) => 2.0] + u0map = Pair[] + else + constr = nothing + guess = Pair[] + u0map = [x(t) => 4.0, y(t) => 2.0] + end + + @mtkbuild sys = ODESystem(eqs, t; constraints = constr) + sys, u0map, tspan, parammap, guess end function rocket_fft() From 1d5223634c9bdfb99dbcb04822c76b8359661d85 Mon Sep 17 00:00:00 2001 From: vyudu Date: Sat, 3 May 2025 00:26:08 -0400 Subject: [PATCH 1493/2176] refactor: move tableau generation to interface --- ext/MTKInfiniteOptExt.jl | 17 -- ext/MTKJuMPControlExt.jl | 324 --------------------------------------- 2 files changed, 341 deletions(-) delete mode 100644 ext/MTKJuMPControlExt.jl diff --git a/ext/MTKInfiniteOptExt.jl b/ext/MTKInfiniteOptExt.jl index ba3a772582..5a48f9b96f 100644 --- a/ext/MTKInfiniteOptExt.jl +++ b/ext/MTKInfiniteOptExt.jl @@ -327,23 +327,6 @@ function add_jump_solve_constraints!(prob, tableau; is_free_t = false) end end -""" -Default ODE Tableau: RadauIIA5 -""" -function constructDefault(T::Type = Float64) - sq6 = sqrt(6) - A = [11 // 45-7sq6 / 360 37 // 225-169sq6 / 1800 -2 // 225+sq6 / 75 - 37 // 225+169sq6 / 1800 11 // 45+7sq6 / 360 -2 // 225-sq6 / 75 - 4 // 9-sq6 / 36 4 // 9+sq6 / 36 1//9] - c = [2 // 5 - sq6 / 10; 2 / 5 + sq6 / 10; 1] - α = [4 // 9 - sq6 / 36; 4 // 9 + sq6 / 36; 1 // 9] - A = map(T, A) - α = map(T, α) - c = map(T, c) - - DiffEqBase.ImplicitRKTableau(A, c, α, 5) -end - """ Solve JuMPDynamicOptProblem. Arguments: - prob: a JumpDynamicOptProblem diff --git a/ext/MTKJuMPControlExt.jl b/ext/MTKJuMPControlExt.jl deleted file mode 100644 index de3aa96269..0000000000 --- a/ext/MTKJuMPControlExt.jl +++ /dev/null @@ -1,324 +0,0 @@ -module MTKJuMPControlExt -using ModelingToolkit -using JuMP, InfiniteOpt -using DiffEqDevTools, DiffEqBase, SciMLBase -using LinearAlgebra -const MTK = ModelingToolkit - -struct JuMPControlProblem{uType, tType, P, F, K} - f::F - u0::uType - tspan::tType - p::P - model::InfiniteModel - kwargs::K - - function JuMPControlProblem(f, u0, tspan, p, model; kwargs...) - new{typeof(u0), typeof(tspan), typeof(p), typeof(f), typeof(kwargs)}(f, u0, tspan, p, model, kwargs) - end -end - -struct InfiniteOptControlProblem{uType, tType, isinplace, P, F, K} <: - AbstractOptimalControlProblem{uType, tType, isinplace} - f::F - u0::uType - tspan::tType - p::P - model::InfiniteModel - kwargs::K - - function InfiniteOptControlProblem(f, u0, tspan, p, model, kwargs...) - new{typeof(u0), typeof(tspan), SciMLBase.isinplace(f), - typeof(p), typeof(f), typeof(kwargs)}(f, u0, tspan, p, model, kwargs) - end -end - -""" - JuMPControlProblem(sys::ODESystem, u0, tspan, p; dt) - -Convert an ODESystem representing an optimal control system into a JuMP model -for solving using optimization. Must provide `dt` for determining the length -of the interpolation arrays. - -The optimization variables: -- a vector-of-vectors U representing the unknowns as an interpolation array -- a vector-of-vectors V representing the controls as an interpolation array - -The constraints are: -- The set of user constraints passed to the ODESystem via `constraints` -- The solver constraints that encode the time-stepping used by the solver -""" -function MTK.JuMPControlProblem(sys::ODESystem, u0map, tspan, pmap; - dt = nothing, - steps = nothing, - guesses = Dict(), kwargs...) - MTK.warn_overdetermined(sys, u0map) - _u0map = has_alg_eqs(sys) ? u0map : merge(Dict(u0map), Dict(guesses)) - f, u0, p = MTK.process_SciMLProblem(ODEInputFunction, sys, _u0map, pmap; - t = tspan !== nothing ? tspan[1] : tspan, kwargs...) - - if !isnothing(constraintsys) - (length(constraints(constraintsys)) + length(u0map) > length(sts)) && - @warn "The JuMPControlProblem is overdetermined. The total number of conditions (# constraints + # fixed initial values given by u0map) exceeds the total number of states. The solvers will default to doing a nonlinear least-squares optimization." - end - - JuMPControlProblem(f, u0, tspan, p, model, kwargs...) -end - -""" - InfiniteOptControlProblem(sys::ODESystem, u0map, tspan, pmap; dt) - -Convert an ODESystem representing an optimal control system into a InfiniteOpt model -for solving using optimization. Must provide `dt` for determining the length -of the interpolation arrays. - -Related to `JuMPControlProblem`, but directly adds the differential equations -of the system as derivative constraints, rather than using a solver tableau. -""" -function MTK.InfiniteOptControlProblem(sys::ODESystem, u0map, tspan, pmap; - dt = nothing, - steps = nothing, - guesses = Dict(), kwargs...) - MTK.warn_overdetermined(sys, u0map) - _u0map = has_alg_eqs(sys) ? u0map : merge(Dict(u0map), Dict(guesses)) - f, u0, p = MTK.process_SciMLProblem(ODEInputFunction, sys, _u0map, pmap; - t = tspan !== nothing ? tspan[1] : tspan, kwargs...) - - model = InfiniteModel() - @infinite_parameter(model, t in [ts, te], num_supports = length(steps), derivative_method = OrthogonalCollocation(2)) - @variable(model, U[1:length(states)], Infinite(t), start = ts) - @variable(model, V[1:length(ctrls)], Infinite(t), start = ts) - - add_jump_cost_function!(model, sys) - add_user_constraints!(model, sys) - - @infinite_parameter(model, t in [tspan[1], tspan[2]], num_supports=steps) - @variable(model, U[i = 1:length(states)], Infinite(t), start=u0[i]) - c0 = MTK.value.([pmap[c] for c in ctrls]) - @variable(model, V[i = 1:length(ctrls)], Infinite(t), start=c0[i]) - - set_jump_bounds!(model, sys, pmap) - add_jump_cost_function!(model, sys, (tspan[1], tspan[2]), pmap; is_free_t) - add_user_constraints!(model, sys, pmap; is_free_t) - - stidxmap = Dict([v => i for (i, v) in enumerate(states)]) - u0map = Dict([MTK.default_toterm(MTK.value(k)) => v for (k, v) in u0map]) - u0_idxs = has_alg_eqs(sys) ? collect(1:length(states)) : - [stidxmap[MTK.default_toterm(k)] for (k, v) in u0map] - add_initial_constraints!(model, u0, u0_idxs, tspan[1]) - return model -end - - JuMPControlProblem(f, u0, tspan, p, model, kwargs...) -end - -function add_jump_cost_function!(model, sys) - jcosts = MTK.get_costs(sys) - consolidate = MTK.get_consolidate(sys) - if isnothing(consolidate) - @objective(model, Min, 0) - return - end - iv = MTK.get_iv(sys) - - stidxmap = Dict([v => i for (i, v) in enumerate(unknowns(sys))]) - cidxmap = Dict([v => i for (i, v) in enumerate(controls(sys))]) - - for st in unknowns(sys) - x = operation(st) - t = only(arguments(st)) - idx = stidxmap[x(iv)] - subval = isequal(t, iv) ? model[:U][idx] : model[:U][idx](t) - jcosts = Symbolics.substitute(jcosts, Dict(x(t) => subval)) - end - - for ct in controls(sys) - p = operation(ct) - t = only(arguments(ct)) - idx = cidxmap[p(iv)] - subval = isequal(t, iv) ? model[:V][idx] : model[:V][idx](t) - jcosts = Symbolics.substitute(jcosts, Dict(x(t) => subval)) - end - - @objective(model, Min, consolidate(jcosts)) -end - -function add_user_constraints!(model, sys) - jconstraints = if !(csys = MTK.get_constraintsystem(sys) isa Nothing) - MTK.get_constraints(csys) - else - nothing - end - isnothing(jconstraints) && return nothing - - iv = MTK.get_iv(sys) - stidxmap = Dict([v => i for (i, v) in enumerate(unknowns(sys))]) - cidxmap = Dict([v => i for (i, v) in enumerate(controls(sys))]) - - for st in unknowns(sys) - x = operation(st) - t = only(arguments(st)) - idx = stidxmap[x(iv)] - subval = isequal(t, iv) ? model[:U][idx] : model[:U][idx](t) - jconstraints = Symbolics.substitute(jconstraints, Dict(x(t) => subval)) - end - - auxmap = Dict([u => MTK.default_toterm(MTK.value(u)) for u in unknowns(conssys)]) - jconstraints = substitute_jump_vars(model, sys, pmap, jconstraints; auxmap) - - # Substitute to-term'd variables - for (i, cons) in enumerate(jconstraints) - if cons isa Equation - @constraint(model, user[i], cons.lhs - cons.rhs == 0) - elseif cons.relational_op === Symbolics.geq - @constraint(model, user[i], cons.lhs - cons.rhs ≥ 0) - else - @constraint(model, user[i], cons.lhs - cons.rhs ≤ 0) - end - end -end - -function add_initial_constraints!(model, u0, u0_idxs, tspan) - ts = tspan[1] - @constraint(model, init_u0_idx[i in u0_idxs], model[:U][i](ts) == u0[i]) -end - -function substitute_jump_vars(model, sys, pmap, exprs; auxmap = Dict()) - iv = MTK.get_iv(sys) - sts = unknowns(sys) - cts = MTK.unbound_inputs(sys) - U = model[:U] - V = model[:V] - exprs = map(c -> Symbolics.fixpoint_sub(c, auxmap), exprs) - - # for variables like x(t) - whole_interval_map = Dict([[v => U[i] for (i, v) in enumerate(sts)]; - [v => V[i] for (i, v) in enumerate(cts)]]) - exprs = map(c -> Symbolics.fixpoint_sub(c, whole_interval_map), exprs) - - # for variables like x(1.0) - x_ops = [MTK.operation(MTK.unwrap(st)) for st in sts] - c_ops = [MTK.operation(MTK.unwrap(ct)) for ct in cts] - fixed_t_map = Dict([[x_ops[i] => U[i] for i in 1:length(U)]; - [c_ops[i] => V[i] for i in 1:length(V)]]) - - exprs = map(c -> Symbolics.fixpoint_sub(c, fixed_t_map), exprs) - - exprs = map(c -> Symbolics.fixpoint_sub(c, Dict(pmap)), exprs) - exprs -end - -is_explicit(tableau) = tableau isa DiffEqDevTools.ExplicitRKTableau - -function add_solve_constraints!(prob, tableau) - A = tableau.A - α = tableau.α - c = tableau.c - model = prob.model - f = prob.f - p = prob.p - - t = model[:t] - tsteps = supports(t) - tmax = tsteps[end] - pop!(tsteps) - dt = tsteps[2] - tsteps[1] - - U = model[:U] - nᵤ = length(U) - if is_explicit(tableau) - K = Any[] - for τ in tsteps - for (i, h) in enumerate(c) - ΔU = sum([A[i, j] * K[j] for j in 1:i-1], init = zeros(nᵤ)) - Uₙ = [U[i](τ) + ΔU[i]*dt for i in 1:nᵤ] - Kₙ = f(Uₙ, p, τ + h*dt) - push!(K, Kₙ) - end - ΔU = sum([α[i] * K[i] for i in 1:length(α)]) - @constraint(model, [n = 1:nᵤ], U[n](τ) + ΔU[n] == U[n](τ + dt)) - empty!(K) - end - else - @variable(model, K[1:length(α), 1:nᵤ], Infinite(t)) - ΔUs = A * K - ΔU_tot = dt * (K' * α) - for τ in tsteps - ΔUs = [A * K(τ)] - for (i, h) in enumerate(c) - ΔU = @view ΔUs[i, :] - Uₙ = U + ΔU * h * dt - @constraint(model, [j = 1:nᵤ], K[i, j]==(tₛ * f(Uₙ, V, p, τ + h * dt)[j]), - DomainRestrictions(t => τ), base_name="solve_K$i($τ)") - end - @constraint(model, [n = 1:nᵤ], U[n](τ) + ΔU_tot[n]==U[n](min(τ + dt, tmax)), - DomainRestrictions(t => τ), base_name="solve_U($τ)") - end - end -end - -""" -""" -struct JuMPControlSolution - model::InfiniteModel - sol::ODESolution -end - -""" -Solve JuMPControlProblem. Arguments: -- prob: a JumpControlProblem -- jump_solver: a LP solver such as HiGHS -- ode_solver: Takes in a symbol representing the solver. Acceptable solvers may be found at https://docs.sciml.ai/DiffEqDevDocs/stable/internals/tableaus/. Note that the symbol may be different than the typical name of the solver, e.g. :Tsitouras5 rather than Tsit5. -""" -function DiffEqBase.solve(prob::JuMPControlProblem, jump_solver, ode_solver::Symbol) - model = prob.model - tableau_getter = Symbol(:construct, ode_solver) - @eval tableau = $tableau_getter() - ts = supports(model[:t]) - add_solve_constraints!(prob, tableau) - - # Unregister current solver constraints - for con in all_constraints(model) - if occursin("solve", JuMP.name(con)) - unregister(model, Symbol(JuMP.name(con))) - delete(model, con) - end - end - unregister(model, :K) - for var in all_variables(model) - if occursin("K", JuMP.name(var)) - unregister(model, Symbol(JuMP.name(var))) - delete(model, var) - end - end - add_jump_solve_constraints!(prob, tableau; is_free_t = haskey(model, :tf)) - _solve(prob, jump_solver, ode_solver) -end - -""" -`derivative_method` kwarg refers to the method used by InfiniteOpt to compute derivatives. The list of possible options can be found at https://infiniteopt.github.io/InfiniteOpt.jl/stable/guide/derivative/. Defaults to FiniteDifference(Backward()). -""" -function DiffEqBase.solve(prob::InfiniteOptControlProblem, jump_solver; - derivative_method = InfiniteOpt.FiniteDifference(Backward()), silent = false) - model = prob.model - silent && set_silent(model) - set_derivative_method(model[:t], derivative_method) - _solve(prob, jump_solver, derivative_method) -end - -function _solve(prob::AbstractOptimalControlProblem, jump_solver, solver) - model = prob.model - set_optimizer(model, jump_solver) - optimize!(model) - - if is_solved_and_feasible(model) - sol = DiffEqBase.build_solution(prob, ode_solver, ts, value.(U)) - JuMPControlSolution(model, sol) - else - sol = DiffEqBase.build_solution(prob, ode_solver, ts, value.(U)) - sol = SciMLBase.solution_new_retcode(sol, SciMLBase.ReturnCode.ConvergenceFailure) - JuMPControlSolution(model, sol) - end -end - -end From b863d5dc28f48ca22f1d9ba5a8f5479fefc0f1de Mon Sep 17 00:00:00 2001 From: vyudu Date: Sat, 3 May 2025 00:26:45 -0400 Subject: [PATCH 1494/2176] rebase --- Project.toml | 2 +- ext/MTKCasADiDynamicOptExt.jl | 13 +++++++++---- src/systems/optimal_control_interface.jl | 20 ++++++++++++++++++++ 3 files changed, 30 insertions(+), 5 deletions(-) diff --git a/Project.toml b/Project.toml index 2ea71a8ed3..060a54c1f8 100644 --- a/Project.toml +++ b/Project.toml @@ -77,7 +77,7 @@ LabelledArrays = "2ee39098-c373-598a-b85f-a56591580800" [extensions] MTKBifurcationKitExt = "BifurcationKit" -MTKCasADiDynamicOptExt = ["CasADi", "DataInterpolations", "DiffEqDevTools"] +MTKCasADiDynamicOptExt = ["CasADi", "DataInterpolations"] MTKChainRulesCoreExt = "ChainRulesCore" MTKDeepDiffsExt = "DeepDiffs" MTKFMIExt = "FMI" diff --git a/ext/MTKCasADiDynamicOptExt.jl b/ext/MTKCasADiDynamicOptExt.jl index e16ce1b6b5..bc04111714 100644 --- a/ext/MTKCasADiDynamicOptExt.jl +++ b/ext/MTKCasADiDynamicOptExt.jl @@ -1,10 +1,10 @@ module MTKCasADiDynamicOptExt using ModelingToolkit using CasADi -using DiffEqDevTools, DiffEqBase +using DiffEqBase using DataInterpolations using UnPack -const MTK = MOdelingToolkit +const MTK = ModelingToolkit struct CasADiDynamicOptProblem{uType, tType, isinplace, P, F, K} <: AbstractDynamicOptProblem{uType, tType, isinplace} @@ -221,17 +221,22 @@ function add_solve_constraints!(prob, tableau; is_free_t) end end -is_explicit(tableau) = tableau isa DiffEqDevTools.ExplicitRKTableau """ solve(prob::CasADiDynamicOptProblem, casadi_solver, ode_solver; plugin_options, solver_options) `plugin_options` and `solver_options` get propagated to the Opti object in CasADi. """ -function DiffEqBase.solve(prob::CasADiDynamicOptProblem, solver::Union{String, Symbol}, ode_solver::Union{String, Symbol}; plugin_options::Dict = Dict(), solver_options::Dict = Dict(), silent = false) +function DiffEqBase.solve(prob::CasADiDynamicOptProblem, solver::Union{String, Symbol}, ode_solver::Symbol = :Default; plugin_options::Dict = Dict(), solver_options::Dict = Dict(), silent = false) model = prob.model opti = model.opti + if ode_solver == :Default + tableau = MTK.constructDefault() + else + tableau_getter = Symbol(:construct, ode_solver) + tableau = @eval Main.tableau_getter() + end solver!(opti, solver, plugin_options, solver_options) add_casadi_solve_constraints!(prob, tableau) solver!(cmodel, "$solver", plugin_options, solver_options) diff --git a/src/systems/optimal_control_interface.jl b/src/systems/optimal_control_interface.jl index eb573da810..d979e7b64e 100644 --- a/src/systems/optimal_control_interface.jl +++ b/src/systems/optimal_control_interface.jl @@ -18,6 +18,7 @@ end function JuMPDynamicOptProblem end function InfiniteOptDynamicOptProblem end +function CasADiDynamicOptProblem end function warn_overdetermined(sys, u0map) constraintsys = get_constraintsystem(sys) @@ -27,6 +28,25 @@ function warn_overdetermined(sys, u0map) end end +""" +Default ODE Tableau: RadauIIA5 +""" +function constructDefault(T::Type = Float64) + sq6 = sqrt(6) + A = [11 // 45-7sq6 / 360 37 // 225-169sq6 / 1800 -2 // 225+sq6 / 75 + 37 // 225+169sq6 / 1800 11 // 45+7sq6 / 360 -2 // 225-sq6 / 75 + 4 // 9-sq6 / 36 4 // 9+sq6 / 36 1//9] + c = [2 // 5 - sq6 / 10; 2 / 5 + sq6 / 10; 1] + α = [4 // 9 - sq6 / 36; 4 // 9 + sq6 / 36; 1 // 9] + A = map(T, A) + α = map(T, α) + c = map(T, c) + + DiffEqBase.ImplicitRKTableau(A, c, α, 5) +end + +is_explicit(tableau) = tableau isa DiffEqBase.ExplicitRKTableau + """ Generate the control function f(x, u, p, t) from the ODESystem. Input variables are automatically inferred but can be manually specified. From 8db9a75d5adf00bbe09f02de354376dbbc484036 Mon Sep 17 00:00:00 2001 From: vyudu Date: Sat, 3 May 2025 01:42:20 -0400 Subject: [PATCH 1495/2176] apply stash --- ext/MTKCasADiDynamicOptExt.jl | 1 - src/ModelingToolkit.jl | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/ext/MTKCasADiDynamicOptExt.jl b/ext/MTKCasADiDynamicOptExt.jl index bc04111714..44f6d254ea 100644 --- a/ext/MTKCasADiDynamicOptExt.jl +++ b/ext/MTKCasADiDynamicOptExt.jl @@ -2,7 +2,6 @@ module MTKCasADiDynamicOptExt using ModelingToolkit using CasADi using DiffEqBase -using DataInterpolations using UnPack const MTK = ModelingToolkit diff --git a/src/ModelingToolkit.jl b/src/ModelingToolkit.jl index d2d20f902e..2f589c80e6 100644 --- a/src/ModelingToolkit.jl +++ b/src/ModelingToolkit.jl @@ -349,7 +349,7 @@ export AnalysisPoint, get_sensitivity_function, get_comp_sensitivity_function, function FMIComponent end include("systems/optimal_control_interface.jl") -export AbstractDynamicOptProblem, JuMPDynamicOptProblem, InfiniteOptDynamicOptProblem +export AbstractDynamicOptProblem, JuMPDynamicOptProblem, InfiniteOptDynamicOptProblem, CasADiDynamicOptProblem export DynamicOptSolution end # module From e81e9c4beee98e770c3627e6e89da450eb1cac86 Mon Sep 17 00:00:00 2001 From: vyudu Date: Sat, 3 May 2025 01:42:42 -0400 Subject: [PATCH 1496/2176] drop DataInteprolations --- Project.toml | 7 +------ ext/MTKCasADiDynamicOptExt.jl | 34 +++++++++++++++++++++++----------- 2 files changed, 24 insertions(+), 17 deletions(-) diff --git a/Project.toml b/Project.toml index 060a54c1f8..33280a5064 100644 --- a/Project.toml +++ b/Project.toml @@ -31,7 +31,6 @@ FunctionWrappers = "069b7b12-0de2-55c6-9aab-29f3d0a68a2e" FunctionWrappersWrappers = "77dc65aa-8811-40c2-897b-53d922fa7daf" Graphs = "86223c79-3864-5bf0-83f7-82e725a168b6" InteractiveUtils = "b77e0a4c-d291-57a0-90e8-8db25a27a240" -Ipopt = "b6b21f68-93f8-5de0-b562-5493be1d77c9" JuliaFormatter = "98e50ef6-434e-11e9-1051-2b60c6c9e899" JumpProcesses = "ccbc3e58-028d-4f4c-8cd5-9ae44345cda5" Latexify = "23fbe1c1-3f47-55db-b15f-69d7ec21a316" @@ -67,17 +66,14 @@ Unitful = "1986cc42-f94f-5a68-af5c-568840ba703d" BifurcationKit = "0f109fa4-8a5d-4b75-95aa-f515264e7665" CasADi = "c49709b8-5c63-11e9-2fb2-69db5844192f" ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4" -DataInterpolations = "82cc6244-b520-54b8-b5a6-8a565e85f1d0" DeepDiffs = "ab62b9b5-e342-54a8-a765-a90f495de1a6" -DiffEqDevTools = "f3b72e0c-5b89-59e1-b016-84e28bfd966d" FMI = "14a09403-18e3-468f-ad8a-74f8dda2d9ac" InfiniteOpt = "20393b10-9daf-11e9-18c9-8db751c92c57" -JuMP = "4076af6c-e467-56ae-b986-b466b2749572" LabelledArrays = "2ee39098-c373-598a-b85f-a56591580800" [extensions] MTKBifurcationKitExt = "BifurcationKit" -MTKCasADiDynamicOptExt = ["CasADi", "DataInterpolations"] +MTKCasADiDynamicOptExt = "CasADi" MTKChainRulesCoreExt = "ChainRulesCore" MTKDeepDiffsExt = "DeepDiffs" MTKFMIExt = "FMI" @@ -104,7 +100,6 @@ DeepDiffs = "1" DelayDiffEq = "5.50" DiffEqBase = "6.170.1" DiffEqCallbacks = "2.16, 3, 4" -DiffEqDevTools = "2.48.0" DiffEqNoiseProcess = "5" DiffRules = "0.1, 1.0" DifferentiationInterface = "0.6.47" diff --git a/ext/MTKCasADiDynamicOptExt.jl b/ext/MTKCasADiDynamicOptExt.jl index 44f6d254ea..a4cfcbd779 100644 --- a/ext/MTKCasADiDynamicOptExt.jl +++ b/ext/MTKCasADiDynamicOptExt.jl @@ -20,17 +20,32 @@ struct CasADiDynamicOptProblem{uType, tType, isinplace, P, F, K} <: end end +# Default linear interpolation for MX objects, likely to change down the line when we support interpolation with the collocation polynomial. +struct MXLinearInterpolation + u::MX + t::Vector{Float64} + dt::Float64 +end + struct CasADiModel opti::Opti - U::AbstractInterpolation - V::AbstractInterpolation + U::MXLinearInterpolation + V::MXLinearInterpolation tₛ::Union{Number, MX} end +function (M::MXLinearInterpolation)(τ) + nt = (τ - M.t) / M.dt + i = 1 + floor(Int, nt) + Δ = nt - i + 1 + + (i > length(M.t) || i < 1) && error("Cannot extrapolate past the tspan.") + M.u[i] + Δ*(M.u[i + 1] - M.u[i]) +end + function MTK.CasADiDynamicOptProblem(sys::ODESystem, u0map, tspan, pmap; dt = nothing, steps = nothing, - interpolation_method::AbstractInterpolation = LinearInterpolation, guesses = Dict(), kwargs...) MTK.warn_overdetermined(sys, u0map) _u0map = has_alg_eqs(sys) ? u0map : merge(Dict(u0map), Dict(guesses)) @@ -39,10 +54,10 @@ function MTK.CasADiDynamicOptProblem(sys::ODESystem, u0map, tspan, pmap; pmap = Dict{Any, Any}(pmap) steps, is_free_t = MTK.process_tspan(tspan, dt, steps) - model = init_model() + model = init_model(sys, tspan, steps, u0map, pmap, u0; is_free_t) end -function init_model(sys, tspan, steps, u0map, pmap, u0; is_free_t = false, interp_type::AbstractInterpolation) +function init_model(sys, tspan, steps, u0map, pmap, u0; is_free_t = false) ctrls = MTK.unbound_inputs(sys) states = unknowns(sys) opti = CasADi.Opti() @@ -61,8 +76,8 @@ function init_model(sys, tspan, steps, u0map, pmap, u0; is_free_t = false, inter U = CasADi.variable!(opti, length(states), steps) V = CasADi.variable!(opti, length(ctrls), steps) - U_interp = interp_type(Matrix(U), tsteps) - V_interp = interp_type(Matrix(V), tsteps) + U_interp = MXLinearInterpolation(U, tsteps, tsteps[2]-tsteps[1]) + V_interp = MXLinearInterpolation(V, tsteps, tsteps[2]-tsteps[1]) CasADiModel(opti, U_interp, V_interp, tₛ) end @@ -220,9 +235,8 @@ function add_solve_constraints!(prob, tableau; is_free_t) end end - """ - solve(prob::CasADiDynamicOptProblem, casadi_solver, ode_solver; plugin_options, solver_options) + solve(prob::CasADiDynamicOptProblem, casadi_solver, ode_solver; plugin_options, solver_options, silent) `plugin_options` and `solver_options` get propagated to the Opti object in CasADi. """ @@ -247,7 +261,6 @@ function DiffEqBase.solve(prob::CasADiDynamicOptProblem, solver::Union{String, S catch ErrorException value_getter = x -> CasADi.debug_value(opti, x) failed = true - continue end ts = value_getter(tₛ) * U.t @@ -270,5 +283,4 @@ function DiffEqBase.solve(prob::CasADiDynamicOptProblem, solver::Union{String, S DynamicOptSolution(cmodel, sol, input_sol) end - end From 280224403591da99e9327a60b925cbd80feac9d9 Mon Sep 17 00:00:00 2001 From: vyudu Date: Mon, 5 May 2025 10:33:39 -0400 Subject: [PATCH 1497/2176] fix: don't use eval --- ext/MTKCasADiDynamicOptExt.jl | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/ext/MTKCasADiDynamicOptExt.jl b/ext/MTKCasADiDynamicOptExt.jl index a4cfcbd779..4fea4a94e0 100644 --- a/ext/MTKCasADiDynamicOptExt.jl +++ b/ext/MTKCasADiDynamicOptExt.jl @@ -43,6 +43,22 @@ function (M::MXLinearInterpolation)(τ) M.u[i] + Δ*(M.u[i + 1] - M.u[i]) end +""" + CasADiDynamicOptProblem(sys::ODESystem, u0, tspan, p; dt, steps) + +Convert an ODESystem representing an optimal control system into a CasADi model +for solving using optimization. Must provide either `dt`, the timestep between collocation +points (which, along with the timespan, determines the number of points), or directly +provide the number of points as `steps`. + +The optimization variables: +- a vector-of-vectors U representing the unknowns as an interpolation array +- a vector-of-vectors V representing the controls as an interpolation array + +The constraints are: +- The set of user constraints passed to the ODESystem via `constraints` +- The solver constraints that encode the time-stepping used by the solver +""" function MTK.CasADiDynamicOptProblem(sys::ODESystem, u0map, tspan, pmap; dt = nothing, steps = nothing, @@ -240,16 +256,11 @@ end `plugin_options` and `solver_options` get propagated to the Opti object in CasADi. """ -function DiffEqBase.solve(prob::CasADiDynamicOptProblem, solver::Union{String, Symbol}, ode_solver::Symbol = :Default; plugin_options::Dict = Dict(), solver_options::Dict = Dict(), silent = false) +function DiffEqBase.solve(prob::CasADiDynamicOptProblem, solver::Union{String, Symbol}, tableau_getter = constructDefault; plugin_options::Dict = Dict(), solver_options::Dict = Dict(), silent = false) model = prob.model + tableau = tableau_getter() opti = model.opti - if ode_solver == :Default - tableau = MTK.constructDefault() - else - tableau_getter = Symbol(:construct, ode_solver) - tableau = @eval Main.tableau_getter() - end solver!(opti, solver, plugin_options, solver_options) add_casadi_solve_constraints!(prob, tableau) solver!(cmodel, "$solver", plugin_options, solver_options) @@ -266,13 +277,13 @@ function DiffEqBase.solve(prob::CasADiDynamicOptProblem, solver::Union{String, S ts = value_getter(tₛ) * U.t U_vals = value_getter(U) U_vals = [[U_vals[i][j] for i in 1:length(U_vals)] for j in 1:length(ts)] - sol = DiffEqBase.build_solution(prob, ode_solver, ts, U_vals) + sol = DiffEqBase.build_solution(prob, tableau_getter, ts, U_vals) input_sol = nothing if !isempty(V) V_vals = value_getter(V) V_vals = [[V_vals[i][j] for i in 1:length(V_vals)] for j in 1:length(ts)] - input_sol = DiffEqBase.build_solution(prob, ode_solver, ts, V_vals) + input_sol = DiffEqBase.build_solution(prob, tableau_getter, ts, V_vals) end if failed From 341d1757e9028a7644a7b7b7744563d59dec250a Mon Sep 17 00:00:00 2001 From: vyudu Date: Mon, 5 May 2025 17:19:15 -0400 Subject: [PATCH 1498/2176] feat: ode transcription working --- .CondaPkg/.gitattributes | 2 + .CondaPkg/.gitignore | 4 + .CondaPkg/meta | Bin 0 -> 624 bytes .CondaPkg/pixi.lock | 540 ++++++++++++++++++++++++++++++++++ .CondaPkg/pixi.toml | 15 + Project.toml | 2 +- ext/MTKCasADiDynamicOptExt.jl | 139 +++++---- 7 files changed, 640 insertions(+), 62 deletions(-) create mode 100644 .CondaPkg/.gitattributes create mode 100644 .CondaPkg/.gitignore create mode 100644 .CondaPkg/meta create mode 100644 .CondaPkg/pixi.lock create mode 100644 .CondaPkg/pixi.toml diff --git a/.CondaPkg/.gitattributes b/.CondaPkg/.gitattributes new file mode 100644 index 0000000000..887a2c18f0 --- /dev/null +++ b/.CondaPkg/.gitattributes @@ -0,0 +1,2 @@ +# SCM syntax highlighting & preventing 3-way merges +pixi.lock merge=binary linguist-language=YAML linguist-generated=true diff --git a/.CondaPkg/.gitignore b/.CondaPkg/.gitignore new file mode 100644 index 0000000000..740bb7d1ae --- /dev/null +++ b/.CondaPkg/.gitignore @@ -0,0 +1,4 @@ + +# pixi environments +.pixi +*.egg-info diff --git a/.CondaPkg/meta b/.CondaPkg/meta new file mode 100644 index 0000000000000000000000000000000000000000..f82e7e2daa6bea42c0dfd7469940ff53a33ba22d GIT binary patch literal 624 zcmb7BOHRWu5DmWygv1TFKxsWqQcxFAsR|nwm9hX;T@JCEx^ZpGb_!jv=Zsu|OOQ4W zLWnHz#`Ae^-uU?UwL>p(*)Fs*EI~WD=e>52 z#;m}cSxC2Tsbqpez-&634?3PRAQ6d1$39Cdmm6anM1~eAZxG{{G>&^t5S;i(Z`E3T tSAPY~IK5xw)OW{sF&Xu4hvz=eb|2nfD3h}@U+QKxrF-BDe_(wl_yTx^l-mFR literal 0 HcmV?d00001 diff --git a/.CondaPkg/pixi.lock b/.CondaPkg/pixi.lock new file mode 100644 index 0000000000..43d038b1dd --- /dev/null +++ b/.CondaPkg/pixi.lock @@ -0,0 +1,540 @@ +version: 6 +environments: + default: + channels: + - url: https://conda.anaconda.org/conda-forge/ + packages: + osx-arm64: + - conda: https://conda.anaconda.org/conda-forge/osx-arm64/ampl-asl-1.0.0-h286801f_2.conda + - conda: https://conda.anaconda.org/conda-forge/osx-arm64/bzip2-1.0.8-h99b78c6_7.conda + - conda: https://conda.anaconda.org/conda-forge/noarch/ca-certificates-2025.4.26-hbd8a1cb_0.conda + - conda: https://conda.anaconda.org/conda-forge/osx-arm64/casadi-3.7.0-py312h74b190f_2.conda + - conda: https://conda.anaconda.org/conda-forge/osx-arm64/ipopt-3.14.17-h945cc1c_2.conda + - conda: https://conda.anaconda.org/conda-forge/osx-arm64/libblas-3.9.0-31_h10e41b3_openblas.conda + - conda: https://conda.anaconda.org/conda-forge/osx-arm64/libblasfeo-0.1.4.2-h37ef02a_0.conda + - conda: https://conda.anaconda.org/conda-forge/osx-arm64/libcblas-3.9.0-31_hb3479ef_openblas.conda + - conda: https://conda.anaconda.org/conda-forge/osx-arm64/libcxx-20.1.4-ha82da77_0.conda + - conda: https://conda.anaconda.org/conda-forge/osx-arm64/libexpat-2.7.0-h286801f_0.conda + - conda: https://conda.anaconda.org/conda-forge/osx-arm64/libfatrop-0.0.4-h286801f_1.conda + - conda: https://conda.anaconda.org/conda-forge/osx-arm64/libffi-3.4.6-h1da3d7d_1.conda + - conda: https://conda.anaconda.org/conda-forge/osx-arm64/libgfortran-5.0.0-14_2_0_h6c33f7e_103.conda + - conda: https://conda.anaconda.org/conda-forge/osx-arm64/libgfortran5-14.2.0-h6c33f7e_103.conda + - conda: https://conda.anaconda.org/conda-forge/osx-arm64/liblapack-3.9.0-31_hc9a63f6_openblas.conda + - conda: https://conda.anaconda.org/conda-forge/osx-arm64/liblzma-5.8.1-h39f12f2_0.conda + - conda: https://conda.anaconda.org/conda-forge/osx-arm64/libopenblas-0.3.29-openmp_hf332438_0.conda + - conda: https://conda.anaconda.org/conda-forge/osx-arm64/libosqp-0.6.3-h5833ebf_1.conda + - conda: https://conda.anaconda.org/conda-forge/osx-arm64/libqdldl-0.1.7-hb7217d7_0.conda + - conda: https://conda.anaconda.org/conda-forge/osx-arm64/libscotch-7.0.6-he56f69b_1.conda + - conda: https://conda.anaconda.org/conda-forge/osx-arm64/libsqlite-3.49.1-h3f77e49_2.conda + - conda: https://conda.anaconda.org/conda-forge/osx-arm64/libzlib-1.3.1-h8359307_2.conda + - conda: https://conda.anaconda.org/conda-forge/osx-arm64/llvm-openmp-20.1.4-hdb05f8b_0.conda + - conda: https://conda.anaconda.org/conda-forge/osx-arm64/metis-5.1.0-h15f6cfe_1007.conda + - conda: https://conda.anaconda.org/conda-forge/osx-arm64/mumps-include-5.7.3-h8c5b6c6_10.conda + - conda: https://conda.anaconda.org/conda-forge/osx-arm64/mumps-seq-5.7.3-h390d176_10.conda + - conda: https://conda.anaconda.org/conda-forge/osx-arm64/ncurses-6.5-h5e97a16_3.conda + - conda: https://conda.anaconda.org/conda-forge/osx-arm64/numpy-2.2.5-py312h7c1f314_0.conda + - conda: https://conda.anaconda.org/conda-forge/osx-arm64/openssl-3.5.0-h81ee809_1.conda + - conda: https://conda.anaconda.org/conda-forge/noarch/ply-3.11-pyhd8ed1ab_3.conda + - conda: https://conda.anaconda.org/conda-forge/osx-arm64/pyomo-6.9.2-py312hd8f9ff3_0.conda + - conda: https://conda.anaconda.org/conda-forge/osx-arm64/python-3.12.10-hc22306f_0_cpython.conda + - conda: https://conda.anaconda.org/conda-forge/noarch/python_abi-3.12-7_cp312.conda + - conda: https://conda.anaconda.org/conda-forge/osx-arm64/readline-8.2-h1d1bf99_2.conda + - conda: https://conda.anaconda.org/conda-forge/noarch/setuptools-80.1.0-pyhff2d567_0.conda + - conda: https://conda.anaconda.org/conda-forge/osx-arm64/tinyxml2-11.0.0-ha1acc90_0.conda + - conda: https://conda.anaconda.org/conda-forge/osx-arm64/tk-8.6.13-h5083fa2_1.conda + - conda: https://conda.anaconda.org/conda-forge/noarch/tzdata-2025b-h78e105d_0.conda +packages: +- conda: https://conda.anaconda.org/conda-forge/osx-arm64/ampl-asl-1.0.0-h286801f_2.conda + sha256: 47a5da6cd38a32c086017a5d2ed2b67e0cc24e25268a9c4cc91ea7d8f1cb9507 + md5: bb25b8fa2b28474412dda4e1a95853b4 + depends: + - __osx >=11.0 + - libcxx >=18 + constrains: + - ampl-mp >=4.0.0 + arch: arm64 + platform: osx + license: BSD-3-Clause AND SMLNJ + size: 411989 + timestamp: 1732439500161 +- conda: https://conda.anaconda.org/conda-forge/osx-arm64/bzip2-1.0.8-h99b78c6_7.conda + sha256: adfa71f158cbd872a36394c56c3568e6034aa55c623634b37a4836bd036e6b91 + md5: fc6948412dbbbe9a4c9ddbbcfe0a79ab + depends: + - __osx >=11.0 + arch: arm64 + platform: osx + license: bzip2-1.0.6 + license_family: BSD + size: 122909 + timestamp: 1720974522888 +- conda: https://conda.anaconda.org/conda-forge/noarch/ca-certificates-2025.4.26-hbd8a1cb_0.conda + sha256: 2a70ed95ace8a3f8a29e6cd1476a943df294a7111dfb3e152e3478c4c889b7ac + md5: 95db94f75ba080a22eb623590993167b + depends: + - __unix + license: ISC + size: 152283 + timestamp: 1745653616541 +- conda: https://conda.anaconda.org/conda-forge/osx-arm64/casadi-3.7.0-py312h74b190f_2.conda + sha256: ae008bb0cd026b64d6cbcd7726fbbbb98904bd868fca3b7d53f258f0c80c27e7 + md5: 8431af5d6849a519573d8ed6ffaa8e28 + depends: + - __osx >=11.0 + - ipopt >=3.14.17,<3.14.18.0a0 + - libblas >=3.9.0,<4.0a0 + - libblasfeo >=0.1.4.2,<0.1.5.0a0 + - libcblas >=3.9.0,<4.0a0 + - libcxx >=18 + - libfatrop >=0.0.4,<0.0.5.0a0 + - liblapack >=3.9.0,<4.0a0 + - libosqp >=0.6.3,<0.6.4.0a0 + - numpy >=1.19,<3 + - python >=3.12,<3.13.0a0 + - python >=3.12,<3.13.0a0 *_cpython + - python_abi 3.12.* *_cp312 + - tinyxml2 >=11.0.0,<11.1.0a0 + arch: arm64 + platform: osx + license: LGPL-3.0-or-later + license_family: LGPL + size: 4681272 + timestamp: 1745474347317 +- conda: https://conda.anaconda.org/conda-forge/osx-arm64/ipopt-3.14.17-h945cc1c_2.conda + sha256: 379d9b44fc265493a8a42ca3694bb8d7538ed8c7bbaf8626dcf8d75f454317cc + md5: e465f880544e1e6b8d904fb65e25b130 + depends: + - __osx >=11.0 + - ampl-asl >=1.0.0,<1.0.1.0a0 + - libblas >=3.9.0,<4.0a0 + - libcxx >=18 + - liblapack >=3.9.0,<4.0a0 + - mumps-seq >=5.7.3,<5.7.4.0a0 + arch: arm64 + platform: osx + license: EPL-1.0 + size: 727718 + timestamp: 1745419955303 +- conda: https://conda.anaconda.org/conda-forge/osx-arm64/libblas-3.9.0-31_h10e41b3_openblas.conda + build_number: 31 + sha256: 369586e7688b59b4f92c709b99d847d66d4d095425db327dd32ee5e6ab74697f + md5: 39b053da5e7035c6592102280aa7612a + depends: + - libopenblas >=0.3.29,<0.3.30.0a0 + - libopenblas >=0.3.29,<1.0a0 + constrains: + - liblapacke =3.9.0=31*_openblas + - libcblas =3.9.0=31*_openblas + - blas =2.131=openblas + - mkl <2025 + - liblapack =3.9.0=31*_openblas + arch: arm64 + platform: osx + license: BSD-3-Clause + license_family: BSD + size: 17123 + timestamp: 1740088119350 +- conda: https://conda.anaconda.org/conda-forge/osx-arm64/libblasfeo-0.1.4.2-h37ef02a_0.conda + sha256: 406885b43c417025be9b2cdb541fa5a0f5426efbbf109d42ac5126efb5d3608d + md5: db8bf9db5c73a85b195f5c973f5514bf + depends: + - __osx >=11.0 + arch: arm64 + platform: osx + license: BSD-2-Clause + license_family: BSD + size: 509787 + timestamp: 1740067975938 +- conda: https://conda.anaconda.org/conda-forge/osx-arm64/libcblas-3.9.0-31_hb3479ef_openblas.conda + build_number: 31 + sha256: f237486cc9118d09d0f3ff8820280de34365f98ee7b7dc5ab923b04c7cbf25a5 + md5: 7353c2bf0e90834cb70545671996d871 + depends: + - libblas 3.9.0 31_h10e41b3_openblas + constrains: + - liblapacke =3.9.0=31*_openblas + - blas =2.131=openblas + - liblapack =3.9.0=31*_openblas + arch: arm64 + platform: osx + license: BSD-3-Clause + license_family: BSD + size: 17032 + timestamp: 1740088127097 +- conda: https://conda.anaconda.org/conda-forge/osx-arm64/libcxx-20.1.4-ha82da77_0.conda + sha256: 1837e2c65f8fc8cfd8b240cfe89406d0ce83112ac63f98c9fb3c9a15b4f2d4e1 + md5: 10c809af502fcdab799082d338170994 + depends: + - __osx >=11.0 + arch: arm64 + platform: osx + license: Apache-2.0 WITH LLVM-exception + license_family: Apache + size: 565811 + timestamp: 1745991653948 +- conda: https://conda.anaconda.org/conda-forge/osx-arm64/libexpat-2.7.0-h286801f_0.conda + sha256: ee550e44765a7bbcb2a0216c063dcd53ac914a7be5386dd0554bd06e6be61840 + md5: 6934bbb74380e045741eb8637641a65b + depends: + - __osx >=11.0 + constrains: + - expat 2.7.0.* + arch: arm64 + platform: osx + license: MIT + license_family: MIT + size: 65714 + timestamp: 1743431789879 +- conda: https://conda.anaconda.org/conda-forge/osx-arm64/libfatrop-0.0.4-h286801f_1.conda + sha256: ff193f966ee33546a297abe02e49b57ba2b728d4706cec9cf3898dc8ca81e3ee + md5: 00cb2b510ea71f73263e59e8253d0720 + depends: + - __osx >=11.0 + - libblasfeo >=0.1.4.1,<0.1.5.0a0 + - libcxx >=18 + arch: arm64 + platform: osx + license: LGPL-3.0-or-later + license_family: LGPL + size: 195859 + timestamp: 1736273349013 +- conda: https://conda.anaconda.org/conda-forge/osx-arm64/libffi-3.4.6-h1da3d7d_1.conda + sha256: c6a530924a9b14e193ea9adfe92843de2a806d1b7dbfd341546ece9653129e60 + md5: c215a60c2935b517dcda8cad4705734d + depends: + - __osx >=11.0 + arch: arm64 + platform: osx + license: MIT + license_family: MIT + size: 39839 + timestamp: 1743434670405 +- conda: https://conda.anaconda.org/conda-forge/osx-arm64/libgfortran-5.0.0-14_2_0_h6c33f7e_103.conda + sha256: 8628746a8ecd311f1c0d14bb4f527c18686251538f7164982ccbe3b772de58b5 + md5: 044a210bc1d5b8367857755665157413 + depends: + - libgfortran5 14.2.0 h6c33f7e_103 + arch: arm64 + platform: osx + license: GPL-3.0-only WITH GCC-exception-3.1 + license_family: GPL + size: 156291 + timestamp: 1743863532821 +- conda: https://conda.anaconda.org/conda-forge/osx-arm64/libgfortran5-14.2.0-h6c33f7e_103.conda + sha256: 8599453990bd3a449013f5fa3d72302f1c68f0680622d419c3f751ff49f01f17 + md5: 69806c1e957069f1d515830dcc9f6cbb + depends: + - llvm-openmp >=8.0.0 + constrains: + - libgfortran 5.0.0 14_2_0_*_103 + arch: arm64 + platform: osx + license: GPL-3.0-only WITH GCC-exception-3.1 + license_family: GPL + size: 806566 + timestamp: 1743863491726 +- conda: https://conda.anaconda.org/conda-forge/osx-arm64/liblapack-3.9.0-31_hc9a63f6_openblas.conda + build_number: 31 + sha256: fe55b9aaf82c6c0192c3d1fcc9b8e884f97492dda9a8de5dae29334b3135fab5 + md5: ff57a55a2cbce171ef5707fb463caf19 + depends: + - libblas 3.9.0 31_h10e41b3_openblas + constrains: + - liblapacke =3.9.0=31*_openblas + - libcblas =3.9.0=31*_openblas + - blas =2.131=openblas + arch: arm64 + platform: osx + license: BSD-3-Clause + license_family: BSD + size: 17033 + timestamp: 1740088134988 +- conda: https://conda.anaconda.org/conda-forge/osx-arm64/liblzma-5.8.1-h39f12f2_0.conda + sha256: 4291dde55ebe9868491dc29716b84ac3de21b8084cbd4d05c9eea79d206b8ab7 + md5: ba24e6f25225fea3d5b6912e2ac562f8 + depends: + - __osx >=11.0 + arch: arm64 + platform: osx + license: 0BSD + size: 92295 + timestamp: 1743771392206 +- conda: https://conda.anaconda.org/conda-forge/osx-arm64/libopenblas-0.3.29-openmp_hf332438_0.conda + sha256: 8989d9e01ec8c9b2d48dbb5efbe70b356fcd15990fb53b64fcb84798982c0343 + md5: 0cd1148c68f09027ee0b0f0179f77c30 + depends: + - __osx >=11.0 + - libgfortran >=5 + - libgfortran5 >=13.2.0 + - llvm-openmp >=18.1.8 + constrains: + - openblas >=0.3.29,<0.3.30.0a0 + arch: arm64 + platform: osx + license: BSD-3-Clause + license_family: BSD + size: 4168442 + timestamp: 1739825514918 +- conda: https://conda.anaconda.org/conda-forge/osx-arm64/libosqp-0.6.3-h5833ebf_1.conda + sha256: 05740cc58df7a9c966a277331dea1987db485508c0a35d2ac9ef562f8061df06 + md5: 7b4c6d7f35e9fe945e810e213d21b942 + depends: + - __osx >=11.0 + - libcxx >=17 + - libqdldl >=0.1.7,<0.1.8.0a0 + arch: arm64 + platform: osx + license: Apache-2.0 + license_family: APACHE + size: 360361 + timestamp: 1729342406705 +- conda: https://conda.anaconda.org/conda-forge/osx-arm64/libqdldl-0.1.7-hb7217d7_0.conda + sha256: 39bb718bca8ce87afdc592d857944ba39bfac54c31a1de4d669597b52bc668ea + md5: b4df6812b4ab33c1a520287f46d91220 + depends: + - libcxx >=14.0.6 + arch: arm64 + platform: osx + license: Apache-2.0 + license_family: APACHE + size: 17189 + timestamp: 1680741853527 +- conda: https://conda.anaconda.org/conda-forge/osx-arm64/libscotch-7.0.6-he56f69b_1.conda + sha256: b616a5943501e35f32699b5f29d4216a3a94bfca87b9ae30feda89a9fa5c2f7a + md5: 291ffb6a18b7e35bf42a317cc6856348 + depends: + - __osx >=11.0 + - bzip2 >=1.0.8,<2.0a0 + - libgfortran >=5 + - libgfortran5 >=13.2.0 + - liblzma >=5.6.3,<6.0a0 + - libzlib >=1.3.1,<2.0a0 + arch: arm64 + platform: osx + license: CECILL-C + size: 273604 + timestamp: 1737537249207 +- conda: https://conda.anaconda.org/conda-forge/osx-arm64/libsqlite-3.49.1-h3f77e49_2.conda + sha256: 907a95f73623c343fc14785cbfefcb7a6b4f2bcf9294fcb295c121611c3a590d + md5: 3b1e330d775170ac46dff9a94c253bd0 + depends: + - __osx >=11.0 + - libzlib >=1.3.1,<2.0a0 + arch: arm64 + platform: osx + license: Unlicense + size: 900188 + timestamp: 1742083865246 +- conda: https://conda.anaconda.org/conda-forge/osx-arm64/libzlib-1.3.1-h8359307_2.conda + sha256: ce34669eadaba351cd54910743e6a2261b67009624dbc7daeeafdef93616711b + md5: 369964e85dc26bfe78f41399b366c435 + depends: + - __osx >=11.0 + constrains: + - zlib 1.3.1 *_2 + arch: arm64 + platform: osx + license: Zlib + license_family: Other + size: 46438 + timestamp: 1727963202283 +- conda: https://conda.anaconda.org/conda-forge/osx-arm64/llvm-openmp-20.1.4-hdb05f8b_0.conda + sha256: b8e8547116dba85890d7b39bfad1c86ed69a6b923caed1e449c90850d271d4d5 + md5: 00cbae3f2127efef6db76bd423a09807 + depends: + - __osx >=11.0 + constrains: + - openmp 20.1.4|20.1.4.* + arch: arm64 + platform: osx + license: Apache-2.0 WITH LLVM-exception + size: 282599 + timestamp: 1746134861758 +- conda: https://conda.anaconda.org/conda-forge/osx-arm64/metis-5.1.0-h15f6cfe_1007.conda + sha256: f54ad3e5d47a0235ba2830848fee590faad550639336fe1e2413ab16fee7ac39 + md5: 7687ec5796288536947bf616179726d8 + depends: + - __osx >=11.0 + arch: arm64 + platform: osx + license: Apache-2.0 + license_family: APACHE + size: 3898314 + timestamp: 1728064659078 +- conda: https://conda.anaconda.org/conda-forge/osx-arm64/mumps-include-5.7.3-h8c5b6c6_10.conda + sha256: 9093f47e4a15fd34c90de93ebfa30c63799e2f99123420fd68653274f5574131 + md5: 077b856b81445e47942980465ca52450 + arch: arm64 + platform: osx + license: CECILL-C + size: 20799 + timestamp: 1745406952431 +- conda: https://conda.anaconda.org/conda-forge/osx-arm64/mumps-seq-5.7.3-h390d176_10.conda + sha256: 2afb273e8268a0960eb3934edffecfa617c941cccc762a11c86d15cb4f0e17a8 + md5: d211ba253191e04aa341a02203ed42fb + depends: + - mumps-include ==5.7.3 h8c5b6c6_10 + - __osx >=11.0 + - libgfortran 5.* + - libgfortran5 >=13.3.0 + - llvm-openmp >=18.1.8 + - liblapack >=3.9.0,<4.0a0 + - libblas >=3.9.0,<4.0a0 + - metis >=5.1.0,<5.1.1.0a0 + - libscotch >=7.0.6,<7.0.7.0a0 + constrains: + - libopenblas * *openmp* + arch: arm64 + platform: osx + license: CECILL-C + size: 2675502 + timestamp: 1745406952432 +- conda: https://conda.anaconda.org/conda-forge/osx-arm64/ncurses-6.5-h5e97a16_3.conda + sha256: 2827ada40e8d9ca69a153a45f7fd14f32b2ead7045d3bbb5d10964898fe65733 + md5: 068d497125e4bf8a66bf707254fff5ae + depends: + - __osx >=11.0 + arch: arm64 + platform: osx + license: X11 AND BSD-3-Clause + size: 797030 + timestamp: 1738196177597 +- conda: https://conda.anaconda.org/conda-forge/osx-arm64/numpy-2.2.5-py312h7c1f314_0.conda + sha256: 982aed7df71ae0ca8bc396ae25d43fd9a4f2b15d18faca15d6c27e9efb3955be + md5: 24a41dacf9d624b069d54a6e92594540 + depends: + - __osx >=11.0 + - libblas >=3.9.0,<4.0a0 + - libcblas >=3.9.0,<4.0a0 + - libcxx >=18 + - liblapack >=3.9.0,<4.0a0 + - python >=3.12,<3.13.0a0 + - python >=3.12,<3.13.0a0 *_cpython + - python_abi 3.12.* *_cp312 + constrains: + - numpy-base <0a0 + arch: arm64 + platform: osx + license: BSD-3-Clause + license_family: BSD + size: 6498553 + timestamp: 1745119367238 +- conda: https://conda.anaconda.org/conda-forge/osx-arm64/openssl-3.5.0-h81ee809_1.conda + sha256: 73d366c1597a10bcd5f3604b5f0734b31c23225536e03782c6a13f9be9d01bff + md5: 5c7aef00ef60738a14e0e612cfc5bcde + depends: + - __osx >=11.0 + - ca-certificates + arch: arm64 + platform: osx + license: Apache-2.0 + license_family: Apache + size: 3064197 + timestamp: 1746223530698 +- conda: https://conda.anaconda.org/conda-forge/noarch/ply-3.11-pyhd8ed1ab_3.conda + sha256: bae453e5cecf19cab23c2e8929c6e30f4866d996a8058be16c797ed4b935461f + md5: fd5062942bfa1b0bd5e0d2a4397b099e + depends: + - python >=3.9 + license: BSD-3-Clause + license_family: BSD + size: 49052 + timestamp: 1733239818090 +- conda: https://conda.anaconda.org/conda-forge/osx-arm64/pyomo-6.9.2-py312hd8f9ff3_0.conda + sha256: 2bec4e5d361ab39d0b06bd026484cdd6399307d4c0e74807c2e2a2cca2c859f2 + md5: a7fc6b79deae458d5522b5103002120c + depends: + - __osx >=11.0 + - libcxx >=18 + - ply + - python >=3.12,<3.13.0a0 + - python >=3.12,<3.13.0a0 *_cpython + - python_abi 3.12.* *_cp312 + - setuptools + arch: arm64 + platform: osx + license: BSD-3-Clause + license_family: BSD + size: 7421923 + timestamp: 1744833268609 +- conda: https://conda.anaconda.org/conda-forge/osx-arm64/python-3.12.10-hc22306f_0_cpython.conda + sha256: 69aed911271e3f698182e9a911250b05bdf691148b670a23e0bea020031e298e + md5: c88f1a7e1e7b917d9c139f03b0960fea + depends: + - __osx >=11.0 + - bzip2 >=1.0.8,<2.0a0 + - libexpat >=2.7.0,<3.0a0 + - libffi >=3.4.6,<3.5.0a0 + - liblzma >=5.8.1,<6.0a0 + - libsqlite >=3.49.1,<4.0a0 + - libzlib >=1.3.1,<2.0a0 + - ncurses >=6.5,<7.0a0 + - openssl >=3.5.0,<4.0a0 + - readline >=8.2,<9.0a0 + - tk >=8.6.13,<8.7.0a0 + - tzdata + constrains: + - python_abi 3.12.* *_cp312 + arch: arm64 + platform: osx + license: Python-2.0 + size: 12932743 + timestamp: 1744323815320 +- conda: https://conda.anaconda.org/conda-forge/noarch/python_abi-3.12-7_cp312.conda + build_number: 7 + sha256: a1bbced35e0df66cc713105344263570e835625c28d1bdee8f748f482b2d7793 + md5: 0dfcdc155cf23812a0c9deada86fb723 + constrains: + - python 3.12.* *_cpython + license: BSD-3-Clause + license_family: BSD + size: 6971 + timestamp: 1745258861359 +- conda: https://conda.anaconda.org/conda-forge/osx-arm64/readline-8.2-h1d1bf99_2.conda + sha256: 7db04684d3904f6151eff8673270922d31da1eea7fa73254d01c437f49702e34 + md5: 63ef3f6e6d6d5c589e64f11263dc5676 + depends: + - ncurses >=6.5,<7.0a0 + arch: arm64 + platform: osx + license: GPL-3.0-only + license_family: GPL + size: 252359 + timestamp: 1740379663071 +- conda: https://conda.anaconda.org/conda-forge/noarch/setuptools-80.1.0-pyhff2d567_0.conda + sha256: 777d34ed359cedd5a5004c930077c101bb3b70e5fbb04d29da5058d75b0ba487 + md5: f6f72d0837c79eaec77661be43e8a691 + depends: + - python >=3.9 + license: MIT + license_family: MIT + size: 778484 + timestamp: 1746085063737 +- conda: https://conda.anaconda.org/conda-forge/osx-arm64/tinyxml2-11.0.0-ha1acc90_0.conda + sha256: bbd9294551ff727305f8335819c24d2490d5d79e0f3d90957992c39d2146093a + md5: 6778d917f88222e8f27af8ec5c41f277 + depends: + - __osx >=11.0 + - libcxx >=18 + arch: arm64 + platform: osx + license: Zlib + size: 122269 + timestamp: 1742246179980 +- conda: https://conda.anaconda.org/conda-forge/osx-arm64/tk-8.6.13-h5083fa2_1.conda + sha256: 72457ad031b4c048e5891f3f6cb27a53cb479db68a52d965f796910e71a403a8 + md5: b50a57ba89c32b62428b71a875291c9b + depends: + - libzlib >=1.2.13,<2.0.0a0 + arch: arm64 + platform: osx + license: TCL + license_family: BSD + size: 3145523 + timestamp: 1699202432999 +- conda: https://conda.anaconda.org/conda-forge/noarch/tzdata-2025b-h78e105d_0.conda + sha256: 5aaa366385d716557e365f0a4e9c3fca43ba196872abbbe3d56bb610d131e192 + md5: 4222072737ccff51314b5ece9c7d6f5a + license: LicenseRef-Public-Domain + size: 122968 + timestamp: 1742727099393 diff --git a/.CondaPkg/pixi.toml b/.CondaPkg/pixi.toml new file mode 100644 index 0000000000..42e8ed6b17 --- /dev/null +++ b/.CondaPkg/pixi.toml @@ -0,0 +1,15 @@ +[dependencies] +pyomo = "*" +casadi = ">3.3" + + [dependencies.python] + channel = "conda-forge" + build = "*cpython*" + version = ">=3.8,<4" + +[project] +name = ".CondaPkg" +platforms = ["osx-arm64"] +channels = ["conda-forge"] +channel-priority = "strict" +description = "automatically generated by CondaPkg.jl" diff --git a/Project.toml b/Project.toml index 33280a5064..ff97c329a9 100644 --- a/Project.toml +++ b/Project.toml @@ -8,6 +8,7 @@ ADTypes = "47edcb42-4c32-4615-8424-f2b9edc5f35b" AbstractTrees = "1520ce14-60c1-5f80-bbc7-55ef81b5835c" ArrayInterface = "4fba245c-0d91-5ea0-9b3e-6abc04ee57a9" BlockArrays = "8e7c35d0-a365-5155-bbbb-fb81a777f24e" +CasADi = "c49709b8-5c63-11e9-2fb2-69db5844192f" Combinatorics = "861a8166-3701-5b0c-9a16-15d98fcdc6aa" CommonSolve = "38540f10-b2f7-11e9-35d8-d573e4eb0ff2" Compat = "34da2185-b29b-5c13-b0c7-acf172513d20" @@ -64,7 +65,6 @@ Unitful = "1986cc42-f94f-5a68-af5c-568840ba703d" [weakdeps] BifurcationKit = "0f109fa4-8a5d-4b75-95aa-f515264e7665" -CasADi = "c49709b8-5c63-11e9-2fb2-69db5844192f" ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4" DeepDiffs = "ab62b9b5-e342-54a8-a765-a90f495de1a6" FMI = "14a09403-18e3-468f-ad8a-74f8dda2d9ac" diff --git a/ext/MTKCasADiDynamicOptExt.jl b/ext/MTKCasADiDynamicOptExt.jl index 4fea4a94e0..6fd8fbdd65 100644 --- a/ext/MTKCasADiDynamicOptExt.jl +++ b/ext/MTKCasADiDynamicOptExt.jl @@ -5,13 +5,27 @@ using DiffEqBase using UnPack const MTK = ModelingToolkit +# Default linear interpolation for MX objects, likely to change down the line when we support interpolation with the collocation polynomial. +struct MXLinearInterpolation + u::MX + t::Vector{Float64} + dt::Float64 +end + +struct CasADiModel + opti::Opti + U::MXLinearInterpolation + V::MXLinearInterpolation + tₛ::MX +end + struct CasADiDynamicOptProblem{uType, tType, isinplace, P, F, K} <: AbstractDynamicOptProblem{uType, tType, isinplace} f::F u0::uType tspan::tType p::P - model::Opti + model::CasADiModel kwargs::K function CasADiDynamicOptProblem(f, u0, tspan, p, model, kwargs...) @@ -20,27 +34,13 @@ struct CasADiDynamicOptProblem{uType, tType, isinplace, P, F, K} <: end end -# Default linear interpolation for MX objects, likely to change down the line when we support interpolation with the collocation polynomial. -struct MXLinearInterpolation - u::MX - t::Vector{Float64} - dt::Float64 -end - -struct CasADiModel - opti::Opti - U::MXLinearInterpolation - V::MXLinearInterpolation - tₛ::Union{Number, MX} -end - function (M::MXLinearInterpolation)(τ) - nt = (τ - M.t) / M.dt + nt = (τ - M.t[1]) / M.dt i = 1 + floor(Int, nt) Δ = nt - i + 1 (i > length(M.t) || i < 1) && error("Cannot extrapolate past the tspan.") - M.u[i] + Δ*(M.u[i + 1] - M.u[i]) + M.u[:, i] + Δ*(M.u[:, i + 1] - M.u[:, i]) end """ @@ -66,11 +66,13 @@ function MTK.CasADiDynamicOptProblem(sys::ODESystem, u0map, tspan, pmap; MTK.warn_overdetermined(sys, u0map) _u0map = has_alg_eqs(sys) ? u0map : merge(Dict(u0map), Dict(guesses)) f, u0, p = MTK.process_SciMLProblem(ODEInputFunction, sys, _u0map, pmap; - t = tspan !== nothing ? tspan[1] : tspan, kwargs...) + t = tspan !== nothing ? tspan[1] : tspan, output_type = MX, kwargs...) pmap = Dict{Any, Any}(pmap) steps, is_free_t = MTK.process_tspan(tspan, dt, steps) model = init_model(sys, tspan, steps, u0map, pmap, u0; is_free_t) + + CasADiDynamicOptProblem(f, u0, tspan, p, model, kwargs...) end function init_model(sys, tspan, steps, u0map, pmap, u0; is_free_t = false) @@ -85,17 +87,28 @@ function init_model(sys, tspan, steps, u0map, pmap, u0; is_free_t = false) tₛ = variable!(opti) tsteps = LinRange(0, 1, steps) else - tₛ = 1 + tₛ = MX(1) tsteps = LinRange(tspan[1], tspan[2], steps) end U = CasADi.variable!(opti, length(states), steps) V = CasADi.variable!(opti, length(ctrls), steps) - U_interp = MXLinearInterpolation(U, tsteps, tsteps[2]-tsteps[1]) V_interp = MXLinearInterpolation(V, tsteps, tsteps[2]-tsteps[1]) - CasADiModel(opti, U_interp, V_interp, tₛ) + model = CasADiModel(opti, U_interp, V_interp, tₛ) + + set_casadi_bounds!(model, sys, pmap) + add_cost_function!(model, sys, (tspan[1], tspan[2]), pmap) + add_user_constraints!(model, sys, pmap; is_free_t) + + stidxmap = Dict([v => i for (i, v) in enumerate(states)]) + u0map = Dict([MTK.default_toterm(MTK.value(k)) => v for (k, v) in u0map]) + u0_idxs = has_alg_eqs(sys) ? collect(1:length(states)) : + [stidxmap[MTK.default_toterm(k)] for (k, v) in u0map] + add_initial_constraints!(model, u0, u0_idxs) + + model end function set_casadi_bounds!(model, sys, pmap) @@ -114,7 +127,7 @@ function set_casadi_bounds!(model, sys, pmap) end end -function add_initial_constraints!(model::CasADiModel, u0, u0_idxs, ts) +function add_initial_constraints!(model::CasADiModel, u0, u0_idxs) @unpack opti, U = model for i in u0_idxs subject_to!(opti, U.u[i, 1] == u0[i]) @@ -124,19 +137,19 @@ end function add_user_constraints!(model::CasADiModel, sys, pmap; is_free_t = false) @unpack opti, U, V, tₛ = model - iv = get_iv(sys) + iv = MTK.get_iv(sys) conssys = MTK.get_constraintsystem(sys) jconstraints = isnothing(conssys) ? nothing : MTK.get_constraints(conssys) (isnothing(jconstraints) || isempty(jconstraints)) && return nothing - stidxmap = Dict([v => i for (i, v) in enumerate(sts)]) - pidxmap = Dict([v => i for (i, v) in enumerate(ps)]) + stidxmap = Dict([v => i for (i, v) in enumerate(unknowns(sys))]) cons_unknowns = map(MTK.default_toterm, unknowns(conssys)) for st in cons_unknowns - x = operation(st) - t = only(argments(st)) + x = MTK.operation(st) + t = only(MTK.arguments(st)) idx = stidxmap[x(iv)] - + @show t + MTK.symbolic_type(t) === MTK.NotSymbolic() || continue jconstraints = map(c -> Symbolics.substitute(c, Dict(x(t) => U(t)[idx])), jconstraints) end jconstraints = substitute_casadi_vars(model, sys, pmap, jconstraints) @@ -145,9 +158,9 @@ function add_user_constraints!(model::CasADiModel, sys, pmap; is_free_t = false) if cons isa Equation subject_to!(opti, cons.lhs - cons.rhs==0) elseif cons.relational_op === Symbolics.geq - subject_to!(model, cons.lhs - cons.rhs≥0) + subject_to!(opti, cons.lhs - cons.rhs≥0) else - subject_to!(model, cons.lhs - cons.rhs≤0) + subject_to!(opti, cons.lhs - cons.rhs≤0) end end end @@ -158,7 +171,7 @@ function add_cost_function!(model::CasADiModel, sys, tspan, pmap) consolidate = MTK.get_consolidate(sys) if isnothing(jcosts) || isempty(jcosts) - minimize!(opti, 0) + minimize!(opti, MX(0)) return end stidxmap = Dict([v => i for (i, v) in enumerate(sts)]) @@ -175,8 +188,6 @@ function add_cost_function!(model::CasADiModel, sys, tspan, pmap) end end jcosts = substitute_casadi_vars(model::CasADiModel, sys, pmap, jcosts; auxmap) - jcosts = map( - c -> Symbolics.substitute(c, MTK.∫() => Symbolics.Integral(iv in tspan)), jcosts) dt = U.t[2] - U.t[1] intmap = Dict() @@ -210,64 +221,70 @@ function substitute_casadi_vars(model::CasADiModel, sys, pmap, exprs; auxmap = D exprs end -function add_solve_constraints!(prob, tableau; is_free_t) +function add_solve_constraints(prob, tableau; is_free_t = false) @unpack A, α, c = tableau @unpack model, f, p = prob @unpack opti, U, V, tₛ = model + solver_opti = copy(opti) tsteps = U.t dt = tsteps[2] - tsteps[1] - nᵤ = length(U) - nᵥ = length(V) + nᵤ = size(U.u, 1) + nᵥ = size(V.u, 1) - if is_explicit(tableau) - K = Any[] + if MTK.is_explicit(tableau) + K = MX[] for k in 1:length(tsteps)-1 + τ = tsteps[k] for (i, h) in enumerate(c) - ΔU = sum([A[i, j] * K[j] for j in 1:(i - 1)], init = zeros(nᵤ)) + ΔU = sum([A[i, j] * K[j] for j in 1:(i - 1)], init = MX(zeros(nᵤ))) Uₙ = U.u[:, k] + ΔU*dt Vₙ = V.u[:, k] Kₙ = tₛ * f(Uₙ, Vₙ, p, τ + h * dt) # scale the time push!(K, Kₙ) end ΔU = dt * sum([α[i] * K[i] for i in 1:length(α)]) - subject_to!(opti, U.u[:, k] + ΔU == U.u[:, k+1]) + subject_to!(solver_opti, U.u[:, k] + ΔU == U.u[:, k+1]) + empty!(K) end else - ΔU_tot = dt * (K' * α) for k in 1:length(tsteps)-1 - Kᵢ = variable!(opti, length(α), nᵤ) - ΔUs = A * Kᵢ # the stepsize at each stage of the implicit method + τ = tsteps[k] + Kᵢ = variable!(solver_opti, nᵤ, length(α)) + ΔUs = A * Kᵢ' # the stepsize at each stage of the implicit method for (i, h) in enumerate(c) - ΔU = @view ΔUs[i, :] - Uₙ = U.u[:,k] + ΔU + ΔU = ΔUs[i,:]' + Uₙ = U.u[:,k] + ΔU*dt Vₙ = V.u[:,k] - subject_to!(opti, K[i,:] == tₛ * f(Uₙ, Vₙ, p, τ + h*dt)) + subject_to!(solver_opti, Kᵢ[:,i] == tₛ * f(Uₙ, Vₙ, p, τ + h*dt)) end - ΔU_tot = dt*(Kᵢ'*α) - subject_to!(opti, U.u[:, k] + ΔU_tot == U.u[:,k+1]) + ΔU_tot = dt*(Kᵢ*α) + subject_to!(solver_opti, U.u[:, k] + ΔU_tot == U.u[:,k+1]) end end + solver_opti end """ solve(prob::CasADiDynamicOptProblem, casadi_solver, ode_solver; plugin_options, solver_options, silent) `plugin_options` and `solver_options` get propagated to the Opti object in CasADi. + +NOTE: the solver should be passed in as a string to CasADi. "ipopt" """ -function DiffEqBase.solve(prob::CasADiDynamicOptProblem, solver::Union{String, Symbol}, tableau_getter = constructDefault; plugin_options::Dict = Dict(), solver_options::Dict = Dict(), silent = false) - model = prob.model +function DiffEqBase.solve(prob::CasADiDynamicOptProblem, solver::Union{String, Symbol} = "ipopt", tableau_getter = MTK.constructDefault; plugin_options::Dict = Dict(), solver_options::Dict = Dict(), silent = false) + @unpack model, u0, p, tspan, f = prob tableau = tableau_getter() - opti = model.opti + @unpack opti, U, V, tₛ = model - solver!(opti, solver, plugin_options, solver_options) - add_casadi_solve_constraints!(prob, tableau) - solver!(cmodel, "$solver", plugin_options, solver_options) + opti = add_solve_constraints(prob, tableau) + solver!(opti, "$solver", plugin_options, solver_options) failed = false + value_getter = nothing try - sol = solve(opti) + sol = CasADi.solve!(opti) value_getter = x -> CasADi.value(sol, x) catch ErrorException value_getter = x -> CasADi.debug_value(opti, x) @@ -275,14 +292,14 @@ function DiffEqBase.solve(prob::CasADiDynamicOptProblem, solver::Union{String, S end ts = value_getter(tₛ) * U.t - U_vals = value_getter(U) - U_vals = [[U_vals[i][j] for i in 1:length(U_vals)] for j in 1:length(ts)] + U_vals = value_getter(U.u) + U_vals = [[U_vals[i, j] for i in 1:size(U_vals, 1)] for j in 1:length(ts)] sol = DiffEqBase.build_solution(prob, tableau_getter, ts, U_vals) input_sol = nothing - if !isempty(V) - V_vals = value_getter(V) - V_vals = [[V_vals[i][j] for i in 1:length(V_vals)] for j in 1:length(ts)] + if prod(size(V.u)) != 0 + V_vals = value_getter(V.u) + V_vals = [[V_vals[i, j] for i in 1:size(V_vals, 1)] for j in 1:length(ts)] input_sol = DiffEqBase.build_solution(prob, tableau_getter, ts, V_vals) end @@ -292,6 +309,6 @@ function DiffEqBase.solve(prob::CasADiDynamicOptProblem, solver::Union{String, S input_sol, SciMLBase.ReturnCode.ConvergenceFailure)) end - DynamicOptSolution(cmodel, sol, input_sol) + DynamicOptSolution(model, sol, input_sol) end end From 65975d4c03c115ebfd915ffdc491d408f042815c Mon Sep 17 00:00:00 2001 From: vyudu Date: Mon, 5 May 2025 21:27:34 -0400 Subject: [PATCH 1499/2176] feat: all problems working --- ext/MTKCasADiDynamicOptExt.jl | 131 +++++++++++++++++++++++++--------- ext/MTKInfiniteOptExt.jl | 1 + 2 files changed, 97 insertions(+), 35 deletions(-) diff --git a/ext/MTKCasADiDynamicOptExt.jl b/ext/MTKCasADiDynamicOptExt.jl index 6fd8fbdd65..e8fb93968c 100644 --- a/ext/MTKCasADiDynamicOptExt.jl +++ b/ext/MTKCasADiDynamicOptExt.jl @@ -3,8 +3,16 @@ using ModelingToolkit using CasADi using DiffEqBase using UnPack +using NaNMath const MTK = ModelingToolkit +# NaNMath +for ff in [acos, log1p, acosh, log2, asin, tan, atanh, cos, log, sin, log10, sqrt] + f = nameof(ff) + # These need to be defined so that JuMP can trace through functions built by Symbolics + @eval NaNMath.$f(x::CasadiSymbolicObject) = Base.$f(x) +end + # Default linear interpolation for MX objects, likely to change down the line when we support interpolation with the collocation polynomial. struct MXLinearInterpolation u::MX @@ -40,7 +48,11 @@ function (M::MXLinearInterpolation)(τ) Δ = nt - i + 1 (i > length(M.t) || i < 1) && error("Cannot extrapolate past the tspan.") - M.u[:, i] + Δ*(M.u[:, i + 1] - M.u[:, i]) + if i < length(M.t) + M.u[:, i] + Δ*(M.u[:, i + 1] - M.u[:, i]) + else + M.u[:, i] + end end """ @@ -83,8 +95,16 @@ function init_model(sys, tspan, steps, u0map, pmap, u0; is_free_t = false) if is_free_t (ts_sym, te_sym) = tspan MTK.symbolic_type(ts_sym) !== MTK.NotSymbolic() && - error("Free initial time problems are not currently supported.") + error("Free initial time problems are not currently supported in CasADiDynamicOptProblem.") tₛ = variable!(opti) + set_initial!(opti, tₛ, pmap[te_sym]) + subject_to!(opti, tₛ >= ts_sym) + hasbounds(te_sym) && begin + lo, hi = getbounds(te_sym) + subject_to!(opti, tₛ >= lo) + subject_to!(opti, tₛ >= hi) + end + pmap[te_sym] = tₛ tsteps = LinRange(0, 1, steps) else tₛ = MX(1) @@ -93,14 +113,21 @@ function init_model(sys, tspan, steps, u0map, pmap, u0; is_free_t = false) U = CasADi.variable!(opti, length(states), steps) V = CasADi.variable!(opti, length(ctrls), steps) + set_initial!(opti, U, DM(repeat(u0, 1, steps))) + c0 = MTK.value.([pmap[c] for c in ctrls]) + set_initial!(opti, V, DM(repeat(c0, 1, steps))) + U_interp = MXLinearInterpolation(U, tsteps, tsteps[2]-tsteps[1]) V_interp = MXLinearInterpolation(V, tsteps, tsteps[2]-tsteps[1]) + for (i, ct) in enumerate(ctrls) + pmap[ct] = V[i, :] + end model = CasADiModel(opti, U_interp, V_interp, tₛ) set_casadi_bounds!(model, sys, pmap) - add_cost_function!(model, sys, (tspan[1], tspan[2]), pmap) - add_user_constraints!(model, sys, pmap; is_free_t) + add_cost_function!(model, sys, tspan, pmap; is_free_t) + add_user_constraints!(model, sys, tspan, pmap; is_free_t) stidxmap = Dict([v => i for (i, v) in enumerate(states)]) u0map = Dict([MTK.default_toterm(MTK.value(k)) => v for (k, v) in u0map]) @@ -116,13 +143,15 @@ function set_casadi_bounds!(model, sys, pmap) for (i, u) in enumerate(unknowns(sys)) if MTK.hasbounds(u) lo, hi = MTK.getbounds(u) - subject_to!(opti, lo <= U[i, :] <= hi) + subject_to!(opti, Symbolics.fixpoint_sub(lo, pmap) <= U.u[i, :]) + subject_to!(opti, U.u[i, :] <= Symbolics.fixpoint_sub(hi, pmap)) end end for (i, v) in enumerate(MTK.unbound_inputs(sys)) if MTK.hasbounds(v) lo, hi = MTK.getbounds(v) - subject_to!(opti, lo <= V[i, :] <= hi) + subject_to!(opti, Symbolics.fixpoint_sub(lo, pmap) <= V.u[i, :]) + subject_to!(opti, V.u[i, :] <= Symbolics.fixpoint_sub(hi, pmap)) end end end @@ -134,7 +163,7 @@ function add_initial_constraints!(model::CasADiModel, u0, u0_idxs) end end -function add_user_constraints!(model::CasADiModel, sys, pmap; is_free_t = false) +function add_user_constraints!(model::CasADiModel, sys, tspan, pmap; is_free_t) @unpack opti, U, V, tₛ = model iv = MTK.get_iv(sys) @@ -143,18 +172,29 @@ function add_user_constraints!(model::CasADiModel, sys, pmap; is_free_t = false) (isnothing(jconstraints) || isempty(jconstraints)) && return nothing stidxmap = Dict([v => i for (i, v) in enumerate(unknowns(sys))]) + ctidxmap = Dict([v => i for (i, v) in enumerate(MTK.unbound_inputs(sys))]) cons_unknowns = map(MTK.default_toterm, unknowns(conssys)) - for st in cons_unknowns - x = MTK.operation(st) - t = only(MTK.arguments(st)) - idx = stidxmap[x(iv)] - @show t - MTK.symbolic_type(t) === MTK.NotSymbolic() || continue - jconstraints = map(c -> Symbolics.substitute(c, Dict(x(t) => U(t)[idx])), jconstraints) - end - jconstraints = substitute_casadi_vars(model, sys, pmap, jconstraints) + auxmap = Dict([u => MTK.default_toterm(MTK.value(u)) for u in unknowns(conssys)]) + jconstraints = substitute_casadi_vars(model, sys, pmap, jconstraints; is_free_t, auxmap) + # Manually substitute fixed-t variables for (i, cons) in enumerate(jconstraints) + consvars = MTK.vars(cons) + for st in consvars + MTK.iscall(st) || continue + x = MTK.operation(st) + t = only(MTK.arguments(st)) + MTK.symbolic_type(t) === MTK.NotSymbolic() || continue + if haskey(stidxmap, x(iv)) + idx = stidxmap[x(iv)] + cv = U + else + idx = ctidxmap[x(iv)] + cv = V + end + cons = Symbolics.substitute(cons, Dict(x(t) => cv(t)[idx])) + end + if cons isa Equation subject_to!(opti, cons.lhs - cons.rhs==0) elseif cons.relational_op === Symbolics.geq @@ -165,29 +205,38 @@ function add_user_constraints!(model::CasADiModel, sys, pmap; is_free_t = false) end end -function add_cost_function!(model::CasADiModel, sys, tspan, pmap) +function add_cost_function!(model::CasADiModel, sys, tspan, pmap; is_free_t) @unpack opti, U, V, tₛ = model - jcosts = MTK.get_costs(sys) + jcosts = copy(MTK.get_costs(sys)) consolidate = MTK.get_consolidate(sys) - if isnothing(jcosts) || isempty(jcosts) minimize!(opti, MX(0)) return end - stidxmap = Dict([v => i for (i, v) in enumerate(sts)]) - pidxmap = Dict([v => i for (i, v) in enumerate(ps)]) + iv = MTK.get_iv(sys) + stidxmap = Dict([v => i for (i, v) in enumerate(unknowns(sys))]) + ctidxmap = Dict([v => i for (i, v) in enumerate(MTK.unbound_inputs(sys))]) + + jcosts = substitute_casadi_vars(model, sys, pmap, jcosts; is_free_t) + # Substitute fixed-time variables. for i in 1:length(jcosts) - vars = vars(jcosts[i]) - for st in vars + costvars = MTK.vars(jcosts[i]) + for st in costvars + MTK.iscall(st) || continue x = operation(st) t = only(arguments(st)) - t isa Union{Num, MTK.Symbolic} && continue - idx = stidxmap[x(iv)] - jcosts[i] = Symbolics.substitute(jcosts[i], Dict(x(t) => U(t)[idx])) + MTK.symbolic_type(t) === MTK.NotSymbolic() || continue + if haskey(stidxmap, x(iv)) + idx = stidxmap[x(iv)] + cv = U + else + idx = ctidxmap[x(iv)] + cv = V + end + jcosts[i] = Symbolics.substitute(jcosts[i], Dict(x(t) => cv(t)[idx])) end end - jcosts = substitute_casadi_vars(model::CasADiModel, sys, pmap, jcosts; auxmap) dt = U.t[2] - U.t[1] intmap = Dict() @@ -195,15 +244,17 @@ function add_cost_function!(model::CasADiModel, sys, tspan, pmap) op = MTK.operation(int) arg = only(arguments(MTK.value(int))) lo, hi = (op.domain.domain.left, op.domain.domain.right) - (lo, hi) !== tspan && error("Non-whole interval bounds for integrals are not currently supported.") + !isequal((lo, hi), tspan) && error("Non-whole interval bounds for integrals are not currently supported for CasADiDynamicOptProblem.") + # Approximate integral as sum. intmap[int] = dt * tₛ * sum(arg) end jcosts = map(c -> Symbolics.substitute(c, intmap), jcosts) - minimize!(opti, consolidate(jcosts)) + jcosts = MTK.value.(jcosts) + minimize!(opti, MX(MTK.value(consolidate(jcosts)))) end -function substitute_casadi_vars(model::CasADiModel, sys, pmap, exprs; auxmap = Dict()) - @unpack opti, U, V = model +function substitute_casadi_vars(model::CasADiModel, sys, pmap, exprs; auxmap::Dict = Dict(), is_free_t) + @unpack opti, U, V, tₛ = model iv = MTK.get_iv(sys) sts = unknowns(sys) cts = MTK.unbound_inputs(sys) @@ -213,6 +264,13 @@ function substitute_casadi_vars(model::CasADiModel, sys, pmap, exprs; auxmap = D exprs = map(c -> Symbolics.fixpoint_sub(c, auxmap), exprs) exprs = map(c -> Symbolics.fixpoint_sub(c, Dict(pmap)), exprs) + # tf means different things in different contexts; a [tf] in a cost function + # should be tₛ, while a x(tf) should translate to x[1] + if is_free_t + free_t_map = Dict([[x(tₛ) => U.u[i, end] for (i, x) in enumerate(x_ops)]; + [c(tₛ) => V.u[i, end] for (i, c) in enumerate(c_ops)]]) + exprs = map(c -> Symbolics.fixpoint_sub(c, free_t_map), exprs) + end # for variables like x(t) whole_interval_map = Dict([[v => U.u[i, :] for (i, v) in enumerate(sts)]; @@ -221,7 +279,7 @@ function substitute_casadi_vars(model::CasADiModel, sys, pmap, exprs; auxmap = D exprs end -function add_solve_constraints(prob, tableau; is_free_t = false) +function add_solve_constraints(prob, tableau) @unpack A, α, c = tableau @unpack model, f, p = prob @unpack opti, U, V, tₛ = model @@ -283,6 +341,7 @@ function DiffEqBase.solve(prob::CasADiDynamicOptProblem, solver::Union{String, S failed = false value_getter = nothing + sol = nothing try sol = CasADi.solve!(opti) value_getter = x -> CasADi.value(sol, x) @@ -293,22 +352,24 @@ function DiffEqBase.solve(prob::CasADiDynamicOptProblem, solver::Union{String, S ts = value_getter(tₛ) * U.t U_vals = value_getter(U.u) + size(U_vals, 2) == 1 && (U_vals = U_vals') U_vals = [[U_vals[i, j] for i in 1:size(U_vals, 1)] for j in 1:length(ts)] - sol = DiffEqBase.build_solution(prob, tableau_getter, ts, U_vals) + ode_sol = DiffEqBase.build_solution(prob, tableau_getter, ts, U_vals) input_sol = nothing if prod(size(V.u)) != 0 V_vals = value_getter(V.u) + size(V_vals, 2) == 1 && (V_vals = V_vals') V_vals = [[V_vals[i, j] for i in 1:size(V_vals, 1)] for j in 1:length(ts)] input_sol = DiffEqBase.build_solution(prob, tableau_getter, ts, V_vals) end if failed - sol = SciMLBase.solution_new_retcode(sol, SciMLBase.ReturnCode.ConvergenceFailure) + ode_sol = SciMLBase.solution_new_retcode(ode_sol, SciMLBase.ReturnCode.ConvergenceFailure) !isnothing(input_sol) && (input_sol = SciMLBase.solution_new_retcode( input_sol, SciMLBase.ReturnCode.ConvergenceFailure)) end - DynamicOptSolution(model, sol, input_sol) + DynamicOptSolution(model, ode_sol, input_sol) end end diff --git a/ext/MTKInfiniteOptExt.jl b/ext/MTKInfiniteOptExt.jl index 5a48f9b96f..27361bd0c1 100644 --- a/ext/MTKInfiniteOptExt.jl +++ b/ext/MTKInfiniteOptExt.jl @@ -173,6 +173,7 @@ function add_jump_cost_function!(model::InfiniteModel, sys, tspan, pmap; is_free return end jcosts = substitute_jump_vars(model, sys, pmap, jcosts; is_free_t) + @show jcosts tₛ = is_free_t ? model[:tf] : 1 # Substitute integral From 583d295a5a6e61cde2f7a9b27ef2a7644ba60af7 Mon Sep 17 00:00:00 2001 From: vyudu Date: Mon, 5 May 2025 21:28:14 -0400 Subject: [PATCH 1500/2176] rename dynamic optimization test file --- test/downstream/dynamic_opt_systems.jl | 37 --- test/downstream/dynamic_optimization.jl | 353 ++++++++++++++++++++++++ 2 files changed, 353 insertions(+), 37 deletions(-) delete mode 100644 test/downstream/dynamic_opt_systems.jl create mode 100644 test/downstream/dynamic_optimization.jl diff --git a/test/downstream/dynamic_opt_systems.jl b/test/downstream/dynamic_opt_systems.jl deleted file mode 100644 index f13b6446ef..0000000000 --- a/test/downstream/dynamic_opt_systems.jl +++ /dev/null @@ -1,37 +0,0 @@ -function build_lotkavolterra(; with_constraint = false) - @parameters α=1.5 β=1.0 γ=3.0 δ=1.0 - @variables x(..) y(..) - t = M.t_nounits - D = M.D_nounits - - eqs = [D(x(t)) ~ α * x(t) - β * x(t) * y(t), - D(y(t)) ~ -γ * y(t) + δ * x(t) * y(t)] - - tspan = (0.0, 1.0) - parammap = [α => 1.5, β => 1.0, γ => 3.0, δ => 1.0] - - if with_constraint - constr = [x(0.6) ~ 3.5, x(0.3) ~ 7.0] - guess = [x(t) => 4.0, y(t) => 2.0] - u0map = Pair[] - else - constr = nothing - guess = Pair[] - u0map = [x(t) => 4.0, y(t) => 2.0] - end - - @mtkbuild sys = ODESystem(eqs, t; constraints = constr) - sys, u0map, tspan, parammap, guess -end - -function rocket_fft() - -end - -function rocket() - -end - -function cartpole() - -end diff --git a/test/downstream/dynamic_optimization.jl b/test/downstream/dynamic_optimization.jl new file mode 100644 index 0000000000..dfe3e6a36d --- /dev/null +++ b/test/downstream/dynamic_optimization.jl @@ -0,0 +1,353 @@ +using ModelingToolkit +import JuMP, InfiniteOpt +using DiffEqDevTools, DiffEqBase +using SimpleDiffEq +using OrdinaryDiffEqSDIRK, OrdinaryDiffEqVerner, OrdinaryDiffEqTsit5, OrdinaryDiffEqFIRK +using Ipopt +using DataInterpolations +using CasADi + +const M = ModelingToolkit + +probtypes = [JuMPDynamicOptProblem, InfiniteOptDynamicOptProblem, CasADiDynamicOptProblem] +solvers = [Ipopt.Optimizer, Ipopt.Optimizer, "opti"] + +function test_dynamic_opt_probs(tableaus, true_sol, args...; kwargs...) + for (probtype, solver, tableau) in zip(probtypes, solvers tableaus) + prob = probtype(args...; kwargs...) + sol = solve(prob, solver, tableau, silent = true) + end +end + +@testset "ODE Solution, no cost" begin + # Test solving without anything attached. + @parameters α=1.5 β=1.0 γ=3.0 δ=1.0 + @variables x(..) y(..) + t = M.t_nounits + D = M.D_nounits + + eqs = [D(x(t)) ~ α * x(t) - β * x(t) * y(t), + D(y(t)) ~ -γ * y(t) + δ * x(t) * y(t)] + + @mtkbuild sys = ODESystem(eqs, t) + tspan = (0.0, 1.0) + u0map = [x(t) => 4.0, y(t) => 2.0] + parammap = [α => 1.5, β => 1.0, γ => 3.0, δ => 1.0] + + # Test explicit method. + jprob = JuMPDynamicOptProblem(sys, u0map, tspan, parammap, dt = 0.01) + @test JuMP.num_constraints(jprob.model) == 2 # initials + jsol = solve(jprob, Ipopt.Optimizer, constructRK4, silent = true) + oprob = ODEProblem(sys, u0map, tspan, parammap) + osol = solve(oprob, SimpleRK4(), dt = 0.01) + cprob = CasADiDynamicOptProblem(sys, u0map, tspan, parammap, dt = 0.01) + csol = solve(oprob, "ipopt", constructRK4) + @test jsol.sol.u ≈ osol.u + @test csol.sol.u ≈ osol.u + + # Implicit method. + jsol2 = solve(jprob, Ipopt.Optimizer, constructImplicitEuler, silent = true) # 63.031 ms, 26.49 MiB + osol2 = solve(oprob, ImplicitEuler(), dt = 0.01, adaptive = false) # 129.375 μs, 61.91 KiB + jsol2 = solve(jprob, Ipopt.Optimizer, constructImplicitEuler, silent = true) # 63.031 ms, 26.49 MiB + @test ≈(jsol2.sol.u, osol2.u, rtol = 0.001) + iprob = InfiniteOptDynamicOptProblem(sys, u0map, tspan, parammap, dt = 0.01) + isol = solve(iprob, Ipopt.Optimizer, + derivative_method = InfiniteOpt.FiniteDifference(InfiniteOpt.Backward()), + silent = true) # 11.540 ms, 4.00 MiB + @test ≈(isol2.sol.u, osol2.u, rtol = 0.001) + csol2 = solve(cprob, "ipopt", constructImplicitEuler, silent = true) + @test ≈(csol2.sol.u, osol2.u, rtol = 0.001) + + # With a constraint + u0map = Pair[] + guess = [x(t) => 4.0, y(t) => 2.0] + constr = [x(0.6) ~ 3.5, x(0.3) ~ 7.0] + @mtkbuild lksys = ODESystem(eqs, t; constraints = constr) + + jprob = JuMPDynamicOptProblem(lksys, u0map, tspan, parammap; guesses = guess, dt = 0.01) + @test JuMP.num_constraints(jprob.model) == 2 + jsol = solve(jprob, Ipopt.Optimizer, constructTsitouras5, silent = true) # 12.190 s, 9.68 GiB + @test jsol.sol(0.6)[1] ≈ 3.5 + @test jsol.sol(0.3)[1] ≈ 7.0 + + cprob = CasADiDynamicOptProblem(lksys, u0map, tspan, parammap; guesses = guess, dt = 0.01) + csol = solve(cprob, "ipopt", constructTsitouras5, silent = true) + @test csol.sol(0.6)[1] ≈ 3.5 + @test csol.sol(0.3)[1] ≈ 7.0 + + iprob = InfiniteOptDynamicOptProblem( + lksys, u0map, tspan, parammap; guesses = guess, dt = 0.01) + isol = solve(iprob, Ipopt.Optimizer, + derivative_method = InfiniteOpt.OrthogonalCollocation(3), silent = true) # 48.564 ms, 9.58 MiB + sol = isol.sol + @test sol(0.6)[1] ≈ 3.5 + @test sol(0.3)[1] ≈ 7.0 + + # Test whole-interval constraints + constr = [x(t) ≳ 1, y(t) ≳ 1] + @mtkbuild lksys = ODESystem(eqs, t; constraints = constr) + iprob = InfiniteOptDynamicOptProblem( + lksys, u0map, tspan, parammap; guesses = guess, dt = 0.01) + isol = solve(iprob, Ipopt.Optimizer, + derivative_method = InfiniteOpt.OrthogonalCollocation(3), silent = true) + @test all(u -> u > [1, 1], isol.sol.u) + + jprob = JuMPDynamicOptProblem(lksys, u0map, tspan, parammap; guesses = guess, dt = 0.01) + jsol = solve(jprob, Ipopt.Optimizer, constructRadauIA3, silent = true) # 12.190 s, 9.68 GiB + @test all(u -> u > [1, 1], jsol.sol.u) + + cprob = CasADiDynamicOptProblem(lksys, u0map, tspan, parammap; guesses = guess, dt = 0.01) + csol = solve(cprob, "ipopt", constructRadauIA3, silent = true) + @test all(u -> u > [1, 1], csol.sol.u) +end + +function is_bangbang(input_sol, lbounds, ubounds, rtol = 1e-4) + for v in 1:(length(input_sol.u[1]) - 1) + all(i -> ≈(i[v], bounds[v]; rtol) || ≈(i[v], bounds[u]; rtol), input_sol.u) || + return false + end + true +end + +function ctrl_to_spline(inputsol, splineType) + us = reduce(vcat, inputsol.u) + ts = reduce(vcat, inputsol.t) + splineType(us, ts) +end + +@testset "Linear systems" begin + # Double integrator + t = M.t_nounits + D = M.D_nounits + @variables x(..) [bounds = (0.0, 0.25)] v(..) + @variables u(..) [bounds = (-1.0, 1.0), input = true] + constr = [v(1.0) ~ 0.0] + cost = [-x(1.0)] # Maximize the final distance. + @named block = ODESystem( + [D(x(t)) ~ v(t), D(v(t)) ~ u(t)], t; costs = cost, constraints = constr) + block, input_idxs = structural_simplify(block, ([u(t)], [])) + + u0map = [x(t) => 0.0, v(t) => 0.0] + tspan = (0.0, 1.0) + parammap = [u(t) => 0.0] + jprob = JuMPDynamicOptProblem(block, u0map, tspan, parammap; dt = 0.01) + jsol = solve(jprob, Ipopt.Optimizer, constructVerner8, silent = true) + # Linear systems have bang-bang controls + @test is_bangbang(jsol.input_sol, [-1.0], [1.0]) + # Test reached final position. + @test ≈(jsol.sol.u[end][2], 0.25, rtol = 1e-5) + + cprob = CasADiDynamicOptProblem(block, u0map, tspan, parammap; dt = 0.01) + csol = solve(cprob, "ipopt", constructVerner8, silent = true) + # Linear systems have bang-bang controls + @test is_bangbang(csol.input_sol, [-1.0], [1.0]) + # Test reached final position. + @test ≈(csol.sol.u[end][2], 0.25, rtol = 1e-5) + + # Test dynamics + @parameters (u_interp::ConstantInterpolation)(..) + @mtkbuild block_ode = ODESystem([D(x(t)) ~ v(t), D(v(t)) ~ u_interp(t)], t) + spline = ctrl_to_spline(jsol.input_sol, ConstantInterpolation) + oprob = ODEProblem(block_ode, u0map, tspan, [u_interp => spline]) + osol = solve(oprob, Vern8(), dt = 0.01, adaptive = false) + @test ≈(jsol.sol.u, osol.u, rtol = 0.05) + @test ≈(csol.sol.u, osol.u, rtol = 0.05) + + iprob = InfiniteOptDynamicOptProblem(block, u0map, tspan, parammap; dt = 0.01) + isol = solve(iprob, Ipopt.Optimizer; silent = true) + @test is_bangbang(isol.input_sol, [-1.0], [1.0]) + @test ≈(isol.sol.u[end][2], 0.25, rtol = 1e-5) + osol = solve(oprob, ImplicitEuler(); dt = 0.01, adaptive = false) + @test ≈(isol.sol.u, osol.u, rtol = 0.05) + + ################### + ### Bee example ### + ################### + # From Lawrence Evans' notes + @variables w(..) q(..) α(t) [input = true, bounds = (0, 1)] + @parameters b c μ s ν + + tspan = (0, 4) + eqs = [D(w(t)) ~ -μ * w(t) + b * s * α * w(t), + D(q(t)) ~ -ν * q(t) + c * (1 - α) * s * w(t)] + costs = [-q(tspan[2])] + + @named beesys = ODESystem(eqs, t; costs) + beesys, input_idxs = structural_simplify(beesys, ([α], [])) + u0map = [w(t) => 40, q(t) => 2] + pmap = [b => 1, c => 1, μ => 1, s => 1, ν => 1, α => 1] + + jprob = JuMPDynamicOptProblem(beesys, u0map, tspan, pmap, dt = 0.01) + jsol = solve(jprob, Ipopt.Optimizer, constructTsitouras5, silent = true) + @test is_bangbang(jsol.input_sol, [0.0], [1.0]) + iprob = InfiniteOptDynamicOptProblem(beesys, u0map, tspan, pmap, dt = 0.01) + isol = solve(iprob, Ipopt.Optimizer; silent = true) + @test is_bangbang(isol.input_sol, [0.0], [1.0]) + cprob = CasADiDynamicOptProblem(beesys, u0map, tspan, pmap; dt = 0.01) + csol = solve(cprob, "ipopt", constructTsitouras5, silent = true) + @test is_bangbang(csol.input_sol, [0.0], [1.0]) + + @parameters (α_interp::LinearInterpolation)(..) + eqs = [D(w(t)) ~ -μ * w(t) + b * s * α_interp(t) * w(t), + D(q(t)) ~ -ν * q(t) + c * (1 - α_interp(t)) * s * w(t)] + @mtkbuild beesys_ode = ODESystem(eqs, t) + oprob = ODEProblem(beesys_ode, + u0map, + tspan, + merge(Dict(pmap), + Dict(α_interp => ctrl_to_spline(jsol.input_sol, LinearInterpolation)))) + osol = solve(oprob, Tsit5(); dt = 0.01, adaptive = false) + @test ≈(osol.u, jsol.sol.u, rtol = 0.01) + @test ≈(osol.u, csol.sol.u, rtol = 0.01) + osol2 = solve(oprob, ImplicitEuler(); dt = 0.01, adaptive = false) + @test ≈(osol2.u, isol.sol.u, rtol = 0.01) +end + +@testset "Rocket launch" begin + t = M.t_nounits + D = M.D_nounits + + @parameters h_c m₀ h₀ g₀ D_c c Tₘ m_c + @variables h(..) v(..) m(..) [bounds = (m_c, 1)] T(..) [input = true, bounds = (0, Tₘ)] + drag(h, v) = D_c * v^2 * exp(-h_c * (h - h₀) / h₀) + gravity(h) = g₀ * (h₀ / h) + + eqs = [D(h(t)) ~ v(t), + D(v(t)) ~ (T(t) - drag(h(t), v(t))) / m(t) - gravity(h(t)), + D(m(t)) ~ -T(t) / c] + + (ts, te) = (0.0, 0.2) + costs = [-h(te)] + cons = [T(te) ~ 0, m(te) ~ m_c] + @named rocket = ODESystem(eqs, t; costs, constraints = cons) + rocket, input_idxs = structural_simplify(rocket, ([T(t)], [])) + + u0map = [h(t) => h₀, m(t) => m₀, v(t) => 0] + pmap = [ + g₀ => 1, m₀ => 1.0, h_c => 500, c => 0.5 * √(g₀ * h₀), D_c => 0.5 * 620 * m₀ / g₀, + Tₘ => 3.5 * g₀ * m₀, T(t) => 0.0, h₀ => 1, m_c => 0.6] + jprob = JuMPDynamicOptProblem(rocket, u0map, (ts, te), pmap; dt = 0.001, cse = false) + jsol = solve(jprob, Ipopt.Optimizer, constructRadauIIA5, silent = true) + @test jsol.sol.u[end][1] > 1.012 + + cprob = CasADiDynamicOptProblem(rocket, u0map, (ts, te), pmap; dt = 0.001, cse = false) + csol = solve(cprob, "ipopt"; silent = true) + @test csol.sol.u[end][1] > 1.012 + + iprob = InfiniteOptDynamicOptProblem( + rocket, u0map, (ts, te), pmap; dt = 0.001) + isol = solve(iprob, Ipopt.Optimizer, silent = true) + @test isol.sol.u[end][1] > 1.012 + + # Test solution + @parameters (T_interp::CubicSpline)(..) + eqs = [D(h(t)) ~ v(t), + D(v(t)) ~ (T_interp(t) - drag(h(t), v(t))) / m(t) - gravity(h(t)), + D(m(t)) ~ -T_interp(t) / c] + @mtkbuild rocket_ode = ODESystem(eqs, t) + interpmap = Dict(T_interp => ctrl_to_spline(jsol.input_sol, CubicSpline)) + oprob = ODEProblem(rocket_ode, u0map, (ts, te), merge(Dict(pmap), interpmap)) + osol = solve(oprob, RadauIIA5(); adaptive = false, dt = 0.001) + @test ≈(jsol.sol.u, osol.u, rtol = 0.02) + @test ≈(csol.sol.u, osol.u, rtol = 0.02) + + interpmap1 = Dict(T_interp => ctrl_to_spline(isol.input_sol, CubicSpline)) + oprob1 = ODEProblem(rocket_ode, u0map, (ts, te), merge(Dict(pmap), interpmap1)) + osol1 = solve(oprob1, ImplicitEuler(); adaptive = false, dt = 0.001) + @test ≈(isol.sol.u, osol1.u, rtol = 0.01) +end + +@testset "Free final time problems" begin + t = M.t_nounits + D = M.D_nounits + + @variables x(..) u(..) [input = true, bounds = (0, 1)] + @parameters tf + eqs = [D(x(t)) ~ -2 + 0.5 * u(t)] + # Integral cost function + costs = [-Symbolics.Integral(t in (0, tf))(x(t) - u(t)), -x(tf)] + consolidate(u) = u[1] + u[2] + @named rocket = ODESystem(eqs, t; costs, consolidate) + rocket, input_idxs = structural_simplify(rocket, ([u(t)], [])) + + u0map = [x(t) => 17.5] + pmap = [u(t) => 0.0, tf => 8] + jprob = JuMPDynamicOptProblem(rocket, u0map, (0, tf), pmap; steps = 201) + jsol = solve(jprob, Ipopt.Optimizer, constructTsitouras5, silent = true) + @test isapprox(jsol.sol.t[end], 10.0, rtol = 1e-3) + + cprob = CasADiDynamicOptProblem(rocket, u0map, (0, tf), pmap; steps = 201) + csol = solve(cprob, "ipopt", constructTsitouras5, silent = true) + @test isapprox(csol.sol.t[end], 10.0, rtol = 1e-3) + + iprob = InfiniteOptDynamicOptProblem(rocket, u0map, (0, tf), pmap; steps = 200) + isol = solve(iprob, Ipopt.Optimizer, silent = true) + @test isapprox(isol.sol.t[end], 10.0, rtol = 1e-3) + + @variables x(..) v(..) + @variables u(..) [bounds = (-1.0, 1.0), input = true] + @parameters tf + constr = [v(tf) ~ 0, x(tf) ~ 0] + cost = [tf] # Minimize time + + @named block = ODESystem( + [D(x(t)) ~ v(t), D(v(t)) ~ u(t)], t; costs = cost, constraints = constr) + block, input_idxs = structural_simplify(block, ([u(t)], [])) + + u0map = [x(t) => 1.0, v(t) => 0.0] + tspan = (0.0, tf) + parammap = [u(t) => 0.0, tf => 1.0] + jprob = JuMPDynamicOptProblem(block, u0map, tspan, parammap; steps = 51) + jsol = solve(jprob, Ipopt.Optimizer, constructVerner8, silent = true) + @test isapprox(jsol.sol.t[end], 2.0, atol = 1e-5) + + cprob = CasADiDynamicOptProblem(block, u0map, (0, tf), parammap; steps = 51) + csol = solve(cprob, "ipopt", constructVerner8, silent = true) + @test isapprox(csol.sol.t[end], 2.0, atol = 1e-5) + + iprob = InfiniteOptDynamicOptProblem(block, u0map, tspan, parammap; steps = 51) + isol = solve(iprob, Ipopt.Optimizer, silent = true) + @test isapprox(isol.sol.t[end], 2.0, atol = 1e-5) +end + +@testset "Cart-pole problem" begin + t = M.t_nounits + D = M.D_nounits + # gravity, length, moment of Inertia, drag coeff + @parameters g l mₚ mₖ + @variables x(..) θ(..) u(t) [input = true, bounds = (-10, 10)] + + s = sin(θ(t)) + c = cos(θ(t)) + H = [mₖ+mₚ mₚ*l*c + mₚ*l*c mₚ*l^2] + C = [0 -mₚ*D(θ(t))*l*s + 0 0] + qd = [D(x(t)), D(θ(t))] + G = [0, mₚ * g * l * s] + B = [1, 0] + + tf = 5 + rhss = -H \ Vector(C * qd + G - B * u) + eqs = [D(D(x(t))) ~ rhss[1], D(D(θ(t))) ~ rhss[2]] + cons = [θ(tf) ~ π, x(tf) ~ 0, D(θ(tf)) ~ 0, D(x(tf)) ~ 0] + costs = [Symbolics.Integral(t in (0, tf))(u^2)] + tspan = (0, tf) + + @named cartpole = ODESystem(eqs, t; costs, constraints = cons) + cartpole, input_idxs = structural_simplify(cartpole, ([u], [])) + + u0map = [D(x(t)) => 0.0, D(θ(t)) => 0.0, θ(t) => 0.0, x(t) => 0.0] + pmap = [mₖ => 1.0, mₚ => 0.2, l => 0.5, g => 9.81, u => 0] + jprob = JuMPDynamicOptProblem(cartpole, u0map, tspan, pmap; dt = 0.04) + jsol = solve(jprob, Ipopt.Optimizer, constructRK4, silent = true) + @test jsol.sol.u[end] ≈ [π, 0, 0, 0] + + cprob = CasADiDynamicOptProblem(cartpole, u0map, tspan, pmap; dt = 0.04) + csol = solve(cprob, "ipopt", constructRK4, silent = true) + @test csol.sol.u[end] ≈ [π, 0, 0, 0] + + iprob = InfiniteOptDynamicOptProblem(cartpole, u0map, tspan, pmap; dt = 0.04) + isol = solve(iprob, Ipopt.Optimizer, silent = true) + @test isol.sol.u[end] ≈ [π, 0, 0, 0] +end From 3b5ccfff8b86215d2246702cf90e205765ee2e2d Mon Sep 17 00:00:00 2001 From: vyudu Date: Mon, 5 May 2025 21:45:49 -0400 Subject: [PATCH 1501/2176] check tests working --- ext/MTKCasADiDynamicOptExt.jl | 3 ++- ext/MTKInfiniteOptExt.jl | 3 +-- test/downstream/dynamic_optimization.jl | 14 ++------------ 3 files changed, 5 insertions(+), 15 deletions(-) diff --git a/ext/MTKCasADiDynamicOptExt.jl b/ext/MTKCasADiDynamicOptExt.jl index e8fb93968c..2c4110b080 100644 --- a/ext/MTKCasADiDynamicOptExt.jl +++ b/ext/MTKCasADiDynamicOptExt.jl @@ -115,7 +115,7 @@ function init_model(sys, tspan, steps, u0map, pmap, u0; is_free_t = false) V = CasADi.variable!(opti, length(ctrls), steps) set_initial!(opti, U, DM(repeat(u0, 1, steps))) c0 = MTK.value.([pmap[c] for c in ctrls]) - set_initial!(opti, V, DM(repeat(c0, 1, steps))) + !isempty(c0) && set_initial!(opti, V, DM(repeat(c0, 1, steps))) U_interp = MXLinearInterpolation(U, tsteps, tsteps[2]-tsteps[1]) V_interp = MXLinearInterpolation(V, tsteps, tsteps[2]-tsteps[1]) @@ -337,6 +337,7 @@ function DiffEqBase.solve(prob::CasADiDynamicOptProblem, solver::Union{String, S @unpack opti, U, V, tₛ = model opti = add_solve_constraints(prob, tableau) + silent && solver_options["print_level"] = 0 solver!(opti, "$solver", plugin_options, solver_options) failed = false diff --git a/ext/MTKInfiniteOptExt.jl b/ext/MTKInfiniteOptExt.jl index 27361bd0c1..7c374cfdaf 100644 --- a/ext/MTKInfiniteOptExt.jl +++ b/ext/MTKInfiniteOptExt.jl @@ -173,7 +173,6 @@ function add_jump_cost_function!(model::InfiniteModel, sys, tspan, pmap; is_free return end jcosts = substitute_jump_vars(model, sys, pmap, jcosts; is_free_t) - @show jcosts tₛ = is_free_t ? model[:tf] : 1 # Substitute integral @@ -338,7 +337,7 @@ Solve JuMPDynamicOptProblem. Arguments: Returns a DynamicOptSolution, which contains both the model and the ODE solution. """ function DiffEqBase.solve( - prob::JuMPDynamicOptProblem, jump_solver, tableau_getter = constructDefault; silent = false) + prob::JuMPDynamicOptProblem, jump_solver, tableau_getter = MTK.constructDefault; silent = false) model = prob.model tableau = tableau_getter() silent && set_silent(model) diff --git a/test/downstream/dynamic_optimization.jl b/test/downstream/dynamic_optimization.jl index dfe3e6a36d..abb534ea0b 100644 --- a/test/downstream/dynamic_optimization.jl +++ b/test/downstream/dynamic_optimization.jl @@ -9,16 +9,6 @@ using CasADi const M = ModelingToolkit -probtypes = [JuMPDynamicOptProblem, InfiniteOptDynamicOptProblem, CasADiDynamicOptProblem] -solvers = [Ipopt.Optimizer, Ipopt.Optimizer, "opti"] - -function test_dynamic_opt_probs(tableaus, true_sol, args...; kwargs...) - for (probtype, solver, tableau) in zip(probtypes, solvers tableaus) - prob = probtype(args...; kwargs...) - sol = solve(prob, solver, tableau, silent = true) - end -end - @testset "ODE Solution, no cost" begin # Test solving without anything attached. @parameters α=1.5 β=1.0 γ=3.0 δ=1.0 @@ -41,7 +31,7 @@ end oprob = ODEProblem(sys, u0map, tspan, parammap) osol = solve(oprob, SimpleRK4(), dt = 0.01) cprob = CasADiDynamicOptProblem(sys, u0map, tspan, parammap, dt = 0.01) - csol = solve(oprob, "ipopt", constructRK4) + csol = solve(cprob, "ipopt", constructRK4) @test jsol.sol.u ≈ osol.u @test csol.sol.u ≈ osol.u @@ -54,7 +44,7 @@ end isol = solve(iprob, Ipopt.Optimizer, derivative_method = InfiniteOpt.FiniteDifference(InfiniteOpt.Backward()), silent = true) # 11.540 ms, 4.00 MiB - @test ≈(isol2.sol.u, osol2.u, rtol = 0.001) + @test ≈(isol.sol.u, osol2.u, rtol = 0.001) csol2 = solve(cprob, "ipopt", constructImplicitEuler, silent = true) @test ≈(csol2.sol.u, osol2.u, rtol = 0.001) From aa79f37eeb51b375e96a10529e589ec99d5ae5c9 Mon Sep 17 00:00:00 2001 From: vyudu Date: Mon, 5 May 2025 22:05:45 -0400 Subject: [PATCH 1502/2176] add silen t option --- ext/MTKCasADiDynamicOptExt.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ext/MTKCasADiDynamicOptExt.jl b/ext/MTKCasADiDynamicOptExt.jl index 2c4110b080..9d16a3ea5c 100644 --- a/ext/MTKCasADiDynamicOptExt.jl +++ b/ext/MTKCasADiDynamicOptExt.jl @@ -337,7 +337,7 @@ function DiffEqBase.solve(prob::CasADiDynamicOptProblem, solver::Union{String, S @unpack opti, U, V, tₛ = model opti = add_solve_constraints(prob, tableau) - silent && solver_options["print_level"] = 0 + silent && (solver_options["print_level"] = 0) solver!(opti, "$solver", plugin_options, solver_options) failed = false From c5d5853df1c83885eabbe090b930fd3a085a0c4d Mon Sep 17 00:00:00 2001 From: vyudu Date: Mon, 5 May 2025 22:08:11 -0400 Subject: [PATCH 1503/2176] update gitignore --- .CondaPkg/.gitattributes | 2 - .CondaPkg/.gitignore | 4 - .CondaPkg/meta | Bin 624 -> 0 bytes .CondaPkg/pixi.lock | 540 --------------------------------------- .CondaPkg/pixi.toml | 15 -- .gitignore | 1 + 6 files changed, 1 insertion(+), 561 deletions(-) delete mode 100644 .CondaPkg/.gitattributes delete mode 100644 .CondaPkg/.gitignore delete mode 100644 .CondaPkg/meta delete mode 100644 .CondaPkg/pixi.lock delete mode 100644 .CondaPkg/pixi.toml diff --git a/.CondaPkg/.gitattributes b/.CondaPkg/.gitattributes deleted file mode 100644 index 887a2c18f0..0000000000 --- a/.CondaPkg/.gitattributes +++ /dev/null @@ -1,2 +0,0 @@ -# SCM syntax highlighting & preventing 3-way merges -pixi.lock merge=binary linguist-language=YAML linguist-generated=true diff --git a/.CondaPkg/.gitignore b/.CondaPkg/.gitignore deleted file mode 100644 index 740bb7d1ae..0000000000 --- a/.CondaPkg/.gitignore +++ /dev/null @@ -1,4 +0,0 @@ - -# pixi environments -.pixi -*.egg-info diff --git a/.CondaPkg/meta b/.CondaPkg/meta deleted file mode 100644 index f82e7e2daa6bea42c0dfd7469940ff53a33ba22d..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 624 zcmb7BOHRWu5DmWygv1TFKxsWqQcxFAsR|nwm9hX;T@JCEx^ZpGb_!jv=Zsu|OOQ4W zLWnHz#`Ae^-uU?UwL>p(*)Fs*EI~WD=e>52 z#;m}cSxC2Tsbqpez-&634?3PRAQ6d1$39Cdmm6anM1~eAZxG{{G>&^t5S;i(Z`E3T tSAPY~IK5xw)OW{sF&Xu4hvz=eb|2nfD3h}@U+QKxrF-BDe_(wl_yTx^l-mFR diff --git a/.CondaPkg/pixi.lock b/.CondaPkg/pixi.lock deleted file mode 100644 index 43d038b1dd..0000000000 --- a/.CondaPkg/pixi.lock +++ /dev/null @@ -1,540 +0,0 @@ -version: 6 -environments: - default: - channels: - - url: https://conda.anaconda.org/conda-forge/ - packages: - osx-arm64: - - conda: https://conda.anaconda.org/conda-forge/osx-arm64/ampl-asl-1.0.0-h286801f_2.conda - - conda: https://conda.anaconda.org/conda-forge/osx-arm64/bzip2-1.0.8-h99b78c6_7.conda - - conda: https://conda.anaconda.org/conda-forge/noarch/ca-certificates-2025.4.26-hbd8a1cb_0.conda - - conda: https://conda.anaconda.org/conda-forge/osx-arm64/casadi-3.7.0-py312h74b190f_2.conda - - conda: https://conda.anaconda.org/conda-forge/osx-arm64/ipopt-3.14.17-h945cc1c_2.conda - - conda: https://conda.anaconda.org/conda-forge/osx-arm64/libblas-3.9.0-31_h10e41b3_openblas.conda - - conda: https://conda.anaconda.org/conda-forge/osx-arm64/libblasfeo-0.1.4.2-h37ef02a_0.conda - - conda: https://conda.anaconda.org/conda-forge/osx-arm64/libcblas-3.9.0-31_hb3479ef_openblas.conda - - conda: https://conda.anaconda.org/conda-forge/osx-arm64/libcxx-20.1.4-ha82da77_0.conda - - conda: https://conda.anaconda.org/conda-forge/osx-arm64/libexpat-2.7.0-h286801f_0.conda - - conda: https://conda.anaconda.org/conda-forge/osx-arm64/libfatrop-0.0.4-h286801f_1.conda - - conda: https://conda.anaconda.org/conda-forge/osx-arm64/libffi-3.4.6-h1da3d7d_1.conda - - conda: https://conda.anaconda.org/conda-forge/osx-arm64/libgfortran-5.0.0-14_2_0_h6c33f7e_103.conda - - conda: https://conda.anaconda.org/conda-forge/osx-arm64/libgfortran5-14.2.0-h6c33f7e_103.conda - - conda: https://conda.anaconda.org/conda-forge/osx-arm64/liblapack-3.9.0-31_hc9a63f6_openblas.conda - - conda: https://conda.anaconda.org/conda-forge/osx-arm64/liblzma-5.8.1-h39f12f2_0.conda - - conda: https://conda.anaconda.org/conda-forge/osx-arm64/libopenblas-0.3.29-openmp_hf332438_0.conda - - conda: https://conda.anaconda.org/conda-forge/osx-arm64/libosqp-0.6.3-h5833ebf_1.conda - - conda: https://conda.anaconda.org/conda-forge/osx-arm64/libqdldl-0.1.7-hb7217d7_0.conda - - conda: https://conda.anaconda.org/conda-forge/osx-arm64/libscotch-7.0.6-he56f69b_1.conda - - conda: https://conda.anaconda.org/conda-forge/osx-arm64/libsqlite-3.49.1-h3f77e49_2.conda - - conda: https://conda.anaconda.org/conda-forge/osx-arm64/libzlib-1.3.1-h8359307_2.conda - - conda: https://conda.anaconda.org/conda-forge/osx-arm64/llvm-openmp-20.1.4-hdb05f8b_0.conda - - conda: https://conda.anaconda.org/conda-forge/osx-arm64/metis-5.1.0-h15f6cfe_1007.conda - - conda: https://conda.anaconda.org/conda-forge/osx-arm64/mumps-include-5.7.3-h8c5b6c6_10.conda - - conda: https://conda.anaconda.org/conda-forge/osx-arm64/mumps-seq-5.7.3-h390d176_10.conda - - conda: https://conda.anaconda.org/conda-forge/osx-arm64/ncurses-6.5-h5e97a16_3.conda - - conda: https://conda.anaconda.org/conda-forge/osx-arm64/numpy-2.2.5-py312h7c1f314_0.conda - - conda: https://conda.anaconda.org/conda-forge/osx-arm64/openssl-3.5.0-h81ee809_1.conda - - conda: https://conda.anaconda.org/conda-forge/noarch/ply-3.11-pyhd8ed1ab_3.conda - - conda: https://conda.anaconda.org/conda-forge/osx-arm64/pyomo-6.9.2-py312hd8f9ff3_0.conda - - conda: https://conda.anaconda.org/conda-forge/osx-arm64/python-3.12.10-hc22306f_0_cpython.conda - - conda: https://conda.anaconda.org/conda-forge/noarch/python_abi-3.12-7_cp312.conda - - conda: https://conda.anaconda.org/conda-forge/osx-arm64/readline-8.2-h1d1bf99_2.conda - - conda: https://conda.anaconda.org/conda-forge/noarch/setuptools-80.1.0-pyhff2d567_0.conda - - conda: https://conda.anaconda.org/conda-forge/osx-arm64/tinyxml2-11.0.0-ha1acc90_0.conda - - conda: https://conda.anaconda.org/conda-forge/osx-arm64/tk-8.6.13-h5083fa2_1.conda - - conda: https://conda.anaconda.org/conda-forge/noarch/tzdata-2025b-h78e105d_0.conda -packages: -- conda: https://conda.anaconda.org/conda-forge/osx-arm64/ampl-asl-1.0.0-h286801f_2.conda - sha256: 47a5da6cd38a32c086017a5d2ed2b67e0cc24e25268a9c4cc91ea7d8f1cb9507 - md5: bb25b8fa2b28474412dda4e1a95853b4 - depends: - - __osx >=11.0 - - libcxx >=18 - constrains: - - ampl-mp >=4.0.0 - arch: arm64 - platform: osx - license: BSD-3-Clause AND SMLNJ - size: 411989 - timestamp: 1732439500161 -- conda: https://conda.anaconda.org/conda-forge/osx-arm64/bzip2-1.0.8-h99b78c6_7.conda - sha256: adfa71f158cbd872a36394c56c3568e6034aa55c623634b37a4836bd036e6b91 - md5: fc6948412dbbbe9a4c9ddbbcfe0a79ab - depends: - - __osx >=11.0 - arch: arm64 - platform: osx - license: bzip2-1.0.6 - license_family: BSD - size: 122909 - timestamp: 1720974522888 -- conda: https://conda.anaconda.org/conda-forge/noarch/ca-certificates-2025.4.26-hbd8a1cb_0.conda - sha256: 2a70ed95ace8a3f8a29e6cd1476a943df294a7111dfb3e152e3478c4c889b7ac - md5: 95db94f75ba080a22eb623590993167b - depends: - - __unix - license: ISC - size: 152283 - timestamp: 1745653616541 -- conda: https://conda.anaconda.org/conda-forge/osx-arm64/casadi-3.7.0-py312h74b190f_2.conda - sha256: ae008bb0cd026b64d6cbcd7726fbbbb98904bd868fca3b7d53f258f0c80c27e7 - md5: 8431af5d6849a519573d8ed6ffaa8e28 - depends: - - __osx >=11.0 - - ipopt >=3.14.17,<3.14.18.0a0 - - libblas >=3.9.0,<4.0a0 - - libblasfeo >=0.1.4.2,<0.1.5.0a0 - - libcblas >=3.9.0,<4.0a0 - - libcxx >=18 - - libfatrop >=0.0.4,<0.0.5.0a0 - - liblapack >=3.9.0,<4.0a0 - - libosqp >=0.6.3,<0.6.4.0a0 - - numpy >=1.19,<3 - - python >=3.12,<3.13.0a0 - - python >=3.12,<3.13.0a0 *_cpython - - python_abi 3.12.* *_cp312 - - tinyxml2 >=11.0.0,<11.1.0a0 - arch: arm64 - platform: osx - license: LGPL-3.0-or-later - license_family: LGPL - size: 4681272 - timestamp: 1745474347317 -- conda: https://conda.anaconda.org/conda-forge/osx-arm64/ipopt-3.14.17-h945cc1c_2.conda - sha256: 379d9b44fc265493a8a42ca3694bb8d7538ed8c7bbaf8626dcf8d75f454317cc - md5: e465f880544e1e6b8d904fb65e25b130 - depends: - - __osx >=11.0 - - ampl-asl >=1.0.0,<1.0.1.0a0 - - libblas >=3.9.0,<4.0a0 - - libcxx >=18 - - liblapack >=3.9.0,<4.0a0 - - mumps-seq >=5.7.3,<5.7.4.0a0 - arch: arm64 - platform: osx - license: EPL-1.0 - size: 727718 - timestamp: 1745419955303 -- conda: https://conda.anaconda.org/conda-forge/osx-arm64/libblas-3.9.0-31_h10e41b3_openblas.conda - build_number: 31 - sha256: 369586e7688b59b4f92c709b99d847d66d4d095425db327dd32ee5e6ab74697f - md5: 39b053da5e7035c6592102280aa7612a - depends: - - libopenblas >=0.3.29,<0.3.30.0a0 - - libopenblas >=0.3.29,<1.0a0 - constrains: - - liblapacke =3.9.0=31*_openblas - - libcblas =3.9.0=31*_openblas - - blas =2.131=openblas - - mkl <2025 - - liblapack =3.9.0=31*_openblas - arch: arm64 - platform: osx - license: BSD-3-Clause - license_family: BSD - size: 17123 - timestamp: 1740088119350 -- conda: https://conda.anaconda.org/conda-forge/osx-arm64/libblasfeo-0.1.4.2-h37ef02a_0.conda - sha256: 406885b43c417025be9b2cdb541fa5a0f5426efbbf109d42ac5126efb5d3608d - md5: db8bf9db5c73a85b195f5c973f5514bf - depends: - - __osx >=11.0 - arch: arm64 - platform: osx - license: BSD-2-Clause - license_family: BSD - size: 509787 - timestamp: 1740067975938 -- conda: https://conda.anaconda.org/conda-forge/osx-arm64/libcblas-3.9.0-31_hb3479ef_openblas.conda - build_number: 31 - sha256: f237486cc9118d09d0f3ff8820280de34365f98ee7b7dc5ab923b04c7cbf25a5 - md5: 7353c2bf0e90834cb70545671996d871 - depends: - - libblas 3.9.0 31_h10e41b3_openblas - constrains: - - liblapacke =3.9.0=31*_openblas - - blas =2.131=openblas - - liblapack =3.9.0=31*_openblas - arch: arm64 - platform: osx - license: BSD-3-Clause - license_family: BSD - size: 17032 - timestamp: 1740088127097 -- conda: https://conda.anaconda.org/conda-forge/osx-arm64/libcxx-20.1.4-ha82da77_0.conda - sha256: 1837e2c65f8fc8cfd8b240cfe89406d0ce83112ac63f98c9fb3c9a15b4f2d4e1 - md5: 10c809af502fcdab799082d338170994 - depends: - - __osx >=11.0 - arch: arm64 - platform: osx - license: Apache-2.0 WITH LLVM-exception - license_family: Apache - size: 565811 - timestamp: 1745991653948 -- conda: https://conda.anaconda.org/conda-forge/osx-arm64/libexpat-2.7.0-h286801f_0.conda - sha256: ee550e44765a7bbcb2a0216c063dcd53ac914a7be5386dd0554bd06e6be61840 - md5: 6934bbb74380e045741eb8637641a65b - depends: - - __osx >=11.0 - constrains: - - expat 2.7.0.* - arch: arm64 - platform: osx - license: MIT - license_family: MIT - size: 65714 - timestamp: 1743431789879 -- conda: https://conda.anaconda.org/conda-forge/osx-arm64/libfatrop-0.0.4-h286801f_1.conda - sha256: ff193f966ee33546a297abe02e49b57ba2b728d4706cec9cf3898dc8ca81e3ee - md5: 00cb2b510ea71f73263e59e8253d0720 - depends: - - __osx >=11.0 - - libblasfeo >=0.1.4.1,<0.1.5.0a0 - - libcxx >=18 - arch: arm64 - platform: osx - license: LGPL-3.0-or-later - license_family: LGPL - size: 195859 - timestamp: 1736273349013 -- conda: https://conda.anaconda.org/conda-forge/osx-arm64/libffi-3.4.6-h1da3d7d_1.conda - sha256: c6a530924a9b14e193ea9adfe92843de2a806d1b7dbfd341546ece9653129e60 - md5: c215a60c2935b517dcda8cad4705734d - depends: - - __osx >=11.0 - arch: arm64 - platform: osx - license: MIT - license_family: MIT - size: 39839 - timestamp: 1743434670405 -- conda: https://conda.anaconda.org/conda-forge/osx-arm64/libgfortran-5.0.0-14_2_0_h6c33f7e_103.conda - sha256: 8628746a8ecd311f1c0d14bb4f527c18686251538f7164982ccbe3b772de58b5 - md5: 044a210bc1d5b8367857755665157413 - depends: - - libgfortran5 14.2.0 h6c33f7e_103 - arch: arm64 - platform: osx - license: GPL-3.0-only WITH GCC-exception-3.1 - license_family: GPL - size: 156291 - timestamp: 1743863532821 -- conda: https://conda.anaconda.org/conda-forge/osx-arm64/libgfortran5-14.2.0-h6c33f7e_103.conda - sha256: 8599453990bd3a449013f5fa3d72302f1c68f0680622d419c3f751ff49f01f17 - md5: 69806c1e957069f1d515830dcc9f6cbb - depends: - - llvm-openmp >=8.0.0 - constrains: - - libgfortran 5.0.0 14_2_0_*_103 - arch: arm64 - platform: osx - license: GPL-3.0-only WITH GCC-exception-3.1 - license_family: GPL - size: 806566 - timestamp: 1743863491726 -- conda: https://conda.anaconda.org/conda-forge/osx-arm64/liblapack-3.9.0-31_hc9a63f6_openblas.conda - build_number: 31 - sha256: fe55b9aaf82c6c0192c3d1fcc9b8e884f97492dda9a8de5dae29334b3135fab5 - md5: ff57a55a2cbce171ef5707fb463caf19 - depends: - - libblas 3.9.0 31_h10e41b3_openblas - constrains: - - liblapacke =3.9.0=31*_openblas - - libcblas =3.9.0=31*_openblas - - blas =2.131=openblas - arch: arm64 - platform: osx - license: BSD-3-Clause - license_family: BSD - size: 17033 - timestamp: 1740088134988 -- conda: https://conda.anaconda.org/conda-forge/osx-arm64/liblzma-5.8.1-h39f12f2_0.conda - sha256: 4291dde55ebe9868491dc29716b84ac3de21b8084cbd4d05c9eea79d206b8ab7 - md5: ba24e6f25225fea3d5b6912e2ac562f8 - depends: - - __osx >=11.0 - arch: arm64 - platform: osx - license: 0BSD - size: 92295 - timestamp: 1743771392206 -- conda: https://conda.anaconda.org/conda-forge/osx-arm64/libopenblas-0.3.29-openmp_hf332438_0.conda - sha256: 8989d9e01ec8c9b2d48dbb5efbe70b356fcd15990fb53b64fcb84798982c0343 - md5: 0cd1148c68f09027ee0b0f0179f77c30 - depends: - - __osx >=11.0 - - libgfortran >=5 - - libgfortran5 >=13.2.0 - - llvm-openmp >=18.1.8 - constrains: - - openblas >=0.3.29,<0.3.30.0a0 - arch: arm64 - platform: osx - license: BSD-3-Clause - license_family: BSD - size: 4168442 - timestamp: 1739825514918 -- conda: https://conda.anaconda.org/conda-forge/osx-arm64/libosqp-0.6.3-h5833ebf_1.conda - sha256: 05740cc58df7a9c966a277331dea1987db485508c0a35d2ac9ef562f8061df06 - md5: 7b4c6d7f35e9fe945e810e213d21b942 - depends: - - __osx >=11.0 - - libcxx >=17 - - libqdldl >=0.1.7,<0.1.8.0a0 - arch: arm64 - platform: osx - license: Apache-2.0 - license_family: APACHE - size: 360361 - timestamp: 1729342406705 -- conda: https://conda.anaconda.org/conda-forge/osx-arm64/libqdldl-0.1.7-hb7217d7_0.conda - sha256: 39bb718bca8ce87afdc592d857944ba39bfac54c31a1de4d669597b52bc668ea - md5: b4df6812b4ab33c1a520287f46d91220 - depends: - - libcxx >=14.0.6 - arch: arm64 - platform: osx - license: Apache-2.0 - license_family: APACHE - size: 17189 - timestamp: 1680741853527 -- conda: https://conda.anaconda.org/conda-forge/osx-arm64/libscotch-7.0.6-he56f69b_1.conda - sha256: b616a5943501e35f32699b5f29d4216a3a94bfca87b9ae30feda89a9fa5c2f7a - md5: 291ffb6a18b7e35bf42a317cc6856348 - depends: - - __osx >=11.0 - - bzip2 >=1.0.8,<2.0a0 - - libgfortran >=5 - - libgfortran5 >=13.2.0 - - liblzma >=5.6.3,<6.0a0 - - libzlib >=1.3.1,<2.0a0 - arch: arm64 - platform: osx - license: CECILL-C - size: 273604 - timestamp: 1737537249207 -- conda: https://conda.anaconda.org/conda-forge/osx-arm64/libsqlite-3.49.1-h3f77e49_2.conda - sha256: 907a95f73623c343fc14785cbfefcb7a6b4f2bcf9294fcb295c121611c3a590d - md5: 3b1e330d775170ac46dff9a94c253bd0 - depends: - - __osx >=11.0 - - libzlib >=1.3.1,<2.0a0 - arch: arm64 - platform: osx - license: Unlicense - size: 900188 - timestamp: 1742083865246 -- conda: https://conda.anaconda.org/conda-forge/osx-arm64/libzlib-1.3.1-h8359307_2.conda - sha256: ce34669eadaba351cd54910743e6a2261b67009624dbc7daeeafdef93616711b - md5: 369964e85dc26bfe78f41399b366c435 - depends: - - __osx >=11.0 - constrains: - - zlib 1.3.1 *_2 - arch: arm64 - platform: osx - license: Zlib - license_family: Other - size: 46438 - timestamp: 1727963202283 -- conda: https://conda.anaconda.org/conda-forge/osx-arm64/llvm-openmp-20.1.4-hdb05f8b_0.conda - sha256: b8e8547116dba85890d7b39bfad1c86ed69a6b923caed1e449c90850d271d4d5 - md5: 00cbae3f2127efef6db76bd423a09807 - depends: - - __osx >=11.0 - constrains: - - openmp 20.1.4|20.1.4.* - arch: arm64 - platform: osx - license: Apache-2.0 WITH LLVM-exception - size: 282599 - timestamp: 1746134861758 -- conda: https://conda.anaconda.org/conda-forge/osx-arm64/metis-5.1.0-h15f6cfe_1007.conda - sha256: f54ad3e5d47a0235ba2830848fee590faad550639336fe1e2413ab16fee7ac39 - md5: 7687ec5796288536947bf616179726d8 - depends: - - __osx >=11.0 - arch: arm64 - platform: osx - license: Apache-2.0 - license_family: APACHE - size: 3898314 - timestamp: 1728064659078 -- conda: https://conda.anaconda.org/conda-forge/osx-arm64/mumps-include-5.7.3-h8c5b6c6_10.conda - sha256: 9093f47e4a15fd34c90de93ebfa30c63799e2f99123420fd68653274f5574131 - md5: 077b856b81445e47942980465ca52450 - arch: arm64 - platform: osx - license: CECILL-C - size: 20799 - timestamp: 1745406952431 -- conda: https://conda.anaconda.org/conda-forge/osx-arm64/mumps-seq-5.7.3-h390d176_10.conda - sha256: 2afb273e8268a0960eb3934edffecfa617c941cccc762a11c86d15cb4f0e17a8 - md5: d211ba253191e04aa341a02203ed42fb - depends: - - mumps-include ==5.7.3 h8c5b6c6_10 - - __osx >=11.0 - - libgfortran 5.* - - libgfortran5 >=13.3.0 - - llvm-openmp >=18.1.8 - - liblapack >=3.9.0,<4.0a0 - - libblas >=3.9.0,<4.0a0 - - metis >=5.1.0,<5.1.1.0a0 - - libscotch >=7.0.6,<7.0.7.0a0 - constrains: - - libopenblas * *openmp* - arch: arm64 - platform: osx - license: CECILL-C - size: 2675502 - timestamp: 1745406952432 -- conda: https://conda.anaconda.org/conda-forge/osx-arm64/ncurses-6.5-h5e97a16_3.conda - sha256: 2827ada40e8d9ca69a153a45f7fd14f32b2ead7045d3bbb5d10964898fe65733 - md5: 068d497125e4bf8a66bf707254fff5ae - depends: - - __osx >=11.0 - arch: arm64 - platform: osx - license: X11 AND BSD-3-Clause - size: 797030 - timestamp: 1738196177597 -- conda: https://conda.anaconda.org/conda-forge/osx-arm64/numpy-2.2.5-py312h7c1f314_0.conda - sha256: 982aed7df71ae0ca8bc396ae25d43fd9a4f2b15d18faca15d6c27e9efb3955be - md5: 24a41dacf9d624b069d54a6e92594540 - depends: - - __osx >=11.0 - - libblas >=3.9.0,<4.0a0 - - libcblas >=3.9.0,<4.0a0 - - libcxx >=18 - - liblapack >=3.9.0,<4.0a0 - - python >=3.12,<3.13.0a0 - - python >=3.12,<3.13.0a0 *_cpython - - python_abi 3.12.* *_cp312 - constrains: - - numpy-base <0a0 - arch: arm64 - platform: osx - license: BSD-3-Clause - license_family: BSD - size: 6498553 - timestamp: 1745119367238 -- conda: https://conda.anaconda.org/conda-forge/osx-arm64/openssl-3.5.0-h81ee809_1.conda - sha256: 73d366c1597a10bcd5f3604b5f0734b31c23225536e03782c6a13f9be9d01bff - md5: 5c7aef00ef60738a14e0e612cfc5bcde - depends: - - __osx >=11.0 - - ca-certificates - arch: arm64 - platform: osx - license: Apache-2.0 - license_family: Apache - size: 3064197 - timestamp: 1746223530698 -- conda: https://conda.anaconda.org/conda-forge/noarch/ply-3.11-pyhd8ed1ab_3.conda - sha256: bae453e5cecf19cab23c2e8929c6e30f4866d996a8058be16c797ed4b935461f - md5: fd5062942bfa1b0bd5e0d2a4397b099e - depends: - - python >=3.9 - license: BSD-3-Clause - license_family: BSD - size: 49052 - timestamp: 1733239818090 -- conda: https://conda.anaconda.org/conda-forge/osx-arm64/pyomo-6.9.2-py312hd8f9ff3_0.conda - sha256: 2bec4e5d361ab39d0b06bd026484cdd6399307d4c0e74807c2e2a2cca2c859f2 - md5: a7fc6b79deae458d5522b5103002120c - depends: - - __osx >=11.0 - - libcxx >=18 - - ply - - python >=3.12,<3.13.0a0 - - python >=3.12,<3.13.0a0 *_cpython - - python_abi 3.12.* *_cp312 - - setuptools - arch: arm64 - platform: osx - license: BSD-3-Clause - license_family: BSD - size: 7421923 - timestamp: 1744833268609 -- conda: https://conda.anaconda.org/conda-forge/osx-arm64/python-3.12.10-hc22306f_0_cpython.conda - sha256: 69aed911271e3f698182e9a911250b05bdf691148b670a23e0bea020031e298e - md5: c88f1a7e1e7b917d9c139f03b0960fea - depends: - - __osx >=11.0 - - bzip2 >=1.0.8,<2.0a0 - - libexpat >=2.7.0,<3.0a0 - - libffi >=3.4.6,<3.5.0a0 - - liblzma >=5.8.1,<6.0a0 - - libsqlite >=3.49.1,<4.0a0 - - libzlib >=1.3.1,<2.0a0 - - ncurses >=6.5,<7.0a0 - - openssl >=3.5.0,<4.0a0 - - readline >=8.2,<9.0a0 - - tk >=8.6.13,<8.7.0a0 - - tzdata - constrains: - - python_abi 3.12.* *_cp312 - arch: arm64 - platform: osx - license: Python-2.0 - size: 12932743 - timestamp: 1744323815320 -- conda: https://conda.anaconda.org/conda-forge/noarch/python_abi-3.12-7_cp312.conda - build_number: 7 - sha256: a1bbced35e0df66cc713105344263570e835625c28d1bdee8f748f482b2d7793 - md5: 0dfcdc155cf23812a0c9deada86fb723 - constrains: - - python 3.12.* *_cpython - license: BSD-3-Clause - license_family: BSD - size: 6971 - timestamp: 1745258861359 -- conda: https://conda.anaconda.org/conda-forge/osx-arm64/readline-8.2-h1d1bf99_2.conda - sha256: 7db04684d3904f6151eff8673270922d31da1eea7fa73254d01c437f49702e34 - md5: 63ef3f6e6d6d5c589e64f11263dc5676 - depends: - - ncurses >=6.5,<7.0a0 - arch: arm64 - platform: osx - license: GPL-3.0-only - license_family: GPL - size: 252359 - timestamp: 1740379663071 -- conda: https://conda.anaconda.org/conda-forge/noarch/setuptools-80.1.0-pyhff2d567_0.conda - sha256: 777d34ed359cedd5a5004c930077c101bb3b70e5fbb04d29da5058d75b0ba487 - md5: f6f72d0837c79eaec77661be43e8a691 - depends: - - python >=3.9 - license: MIT - license_family: MIT - size: 778484 - timestamp: 1746085063737 -- conda: https://conda.anaconda.org/conda-forge/osx-arm64/tinyxml2-11.0.0-ha1acc90_0.conda - sha256: bbd9294551ff727305f8335819c24d2490d5d79e0f3d90957992c39d2146093a - md5: 6778d917f88222e8f27af8ec5c41f277 - depends: - - __osx >=11.0 - - libcxx >=18 - arch: arm64 - platform: osx - license: Zlib - size: 122269 - timestamp: 1742246179980 -- conda: https://conda.anaconda.org/conda-forge/osx-arm64/tk-8.6.13-h5083fa2_1.conda - sha256: 72457ad031b4c048e5891f3f6cb27a53cb479db68a52d965f796910e71a403a8 - md5: b50a57ba89c32b62428b71a875291c9b - depends: - - libzlib >=1.2.13,<2.0.0a0 - arch: arm64 - platform: osx - license: TCL - license_family: BSD - size: 3145523 - timestamp: 1699202432999 -- conda: https://conda.anaconda.org/conda-forge/noarch/tzdata-2025b-h78e105d_0.conda - sha256: 5aaa366385d716557e365f0a4e9c3fca43ba196872abbbe3d56bb610d131e192 - md5: 4222072737ccff51314b5ece9c7d6f5a - license: LicenseRef-Public-Domain - size: 122968 - timestamp: 1742727099393 diff --git a/.CondaPkg/pixi.toml b/.CondaPkg/pixi.toml deleted file mode 100644 index 42e8ed6b17..0000000000 --- a/.CondaPkg/pixi.toml +++ /dev/null @@ -1,15 +0,0 @@ -[dependencies] -pyomo = "*" -casadi = ">3.3" - - [dependencies.python] - channel = "conda-forge" - build = "*cpython*" - version = ">=3.8,<4" - -[project] -name = ".CondaPkg" -platforms = ["osx-arm64"] -channels = ["conda-forge"] -channel-priority = "strict" -description = "automatically generated by CondaPkg.jl" diff --git a/.gitignore b/.gitignore index 6d305ad348..9193389ab8 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,4 @@ Manifest.toml .vscode/* docs/src/assets/Project.toml docs/src/assets/Manifest.toml +.CondaPkg From f3128613277b1fa4e3ed721626fc59a68dda8c4d Mon Sep 17 00:00:00 2001 From: vyudu Date: Mon, 5 May 2025 22:09:42 -0400 Subject: [PATCH 1504/2176] cleanup loose ends --- Project.toml | 2 +- src/systems/systems.jl | 5 --- test/extensions/Project.toml | 1 - test/extensions/jump_control.jl | 57 --------------------------------- 4 files changed, 1 insertion(+), 64 deletions(-) delete mode 100644 test/extensions/jump_control.jl diff --git a/Project.toml b/Project.toml index ff97c329a9..33280a5064 100644 --- a/Project.toml +++ b/Project.toml @@ -8,7 +8,6 @@ ADTypes = "47edcb42-4c32-4615-8424-f2b9edc5f35b" AbstractTrees = "1520ce14-60c1-5f80-bbc7-55ef81b5835c" ArrayInterface = "4fba245c-0d91-5ea0-9b3e-6abc04ee57a9" BlockArrays = "8e7c35d0-a365-5155-bbbb-fb81a777f24e" -CasADi = "c49709b8-5c63-11e9-2fb2-69db5844192f" Combinatorics = "861a8166-3701-5b0c-9a16-15d98fcdc6aa" CommonSolve = "38540f10-b2f7-11e9-35d8-d573e4eb0ff2" Compat = "34da2185-b29b-5c13-b0c7-acf172513d20" @@ -65,6 +64,7 @@ Unitful = "1986cc42-f94f-5a68-af5c-568840ba703d" [weakdeps] BifurcationKit = "0f109fa4-8a5d-4b75-95aa-f515264e7665" +CasADi = "c49709b8-5c63-11e9-2fb2-69db5844192f" ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4" DeepDiffs = "ab62b9b5-e342-54a8-a765-a90f495de1a6" FMI = "14a09403-18e3-468f-ad8a-74f8dda2d9ac" diff --git a/src/systems/systems.jl b/src/systems/systems.jl index 9da7249300..52f93afb9b 100644 --- a/src/systems/systems.jl +++ b/src/systems/systems.jl @@ -163,11 +163,6 @@ function __structural_simplify( end end -function toterm_auxsystems(system::ODESystem) - constraints = system.constraintsystem.constraints - -end - """ $(TYPEDSIGNATURES) diff --git a/test/extensions/Project.toml b/test/extensions/Project.toml index bd51ea5203..0e018b4a22 100644 --- a/test/extensions/Project.toml +++ b/test/extensions/Project.toml @@ -6,7 +6,6 @@ DataInterpolations = "82cc6244-b520-54b8-b5a6-8a565e85f1d0" DiffEqBase = "2b5f629d-d688-5b77-993f-72d75c75574e" DiffEqDevTools = "f3b72e0c-5b89-59e1-b016-84e28bfd966d" ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210" -HiGHS = "87dc4568-4c63-4d18-b0c0-bb2238e4078b" HomotopyContinuation = "f213a82b-91d6-5c5d-acf7-10f1c761b327" InfiniteOpt = "20393b10-9daf-11e9-18c9-8db751c92c57" Ipopt = "b6b21f68-93f8-5de0-b562-5493be1d77c9" diff --git a/test/extensions/jump_control.jl b/test/extensions/jump_control.jl deleted file mode 100644 index c0a071e31d..0000000000 --- a/test/extensions/jump_control.jl +++ /dev/null @@ -1,57 +0,0 @@ -using ModelingToolkit -using JuMP, InfiniteOpt -using DiffEqDevTools, DiffEqBase -using SimpleDiffEq -using HiGHS -const M = ModelingToolkit - -@testset "ODE Solution, no cost" begin - # Test solving without anything attached. - @parameters α=1.5 β=1.0 γ=3.0 δ=1.0 - M.@variables x(..) y(..) - t = M.t_nounits; D = M.D_nounits - - eqs = [D(x(t)) ~ α * x(t) - β * x(t) * y(t), - D(y(t)) ~ -γ * y(t) + δ * x(t) * y(t)] - - tspan = (0.0, 1.0) - u0map = [x(t) => 4.0, y(t) => 2.0] - parammap = [α => 7.5, β => 4, γ => 8.0, δ => 5.0] - @mtkbuild sys = ODESystem(eqs, t) - - # Test explicit method. - jprob = JuMPControlProblem(sys, u0map, tspan, parammap, dt = 0.01) - @test num_constraints(jprob.model) == 2 # initials - jsol = solve(jprob, Ipopt.Optimizer, :Tsitouras5) - oprob = ODEProblem(sys, u0map, tspan, parammap) - osol = solve(oprob, SimpleTsit5(), adaptive = false) - @test jsol.sol.u ≈ osol.u - - # Implicit method. - jsol2 = solve(prob, Ipopt.Optimizer, :RK4) - osol2 = solve(oprob, SimpleRK4(), adaptive = false) - @test jsol2.sol.u ≈ osol2.u - - # With a constraint - @parameters α=1.5 β=1.0 γ=3.0 δ=1.0 - @variables x(..) y(..) - - eqs = [D(x(t)) ~ α * x(t) - β * x(t) * y(t), - D(y(t)) ~ -γ * y(t) + δ * x(t) * y(t)] - - u0map = [] - tspan = (0.0, 1.0) - guess = [x(t) => 4.0, y(t) => 2.0] - constr = [x(0.6) ~ 3.5, x(0.3) ~ 7.0] - @mtkbuild lksys = ODESystem(eqs, t; constraints = constr) - - jprob = JuMPControlProblem(sys, u0map, tspan, parammap; guesses, dt = 0.01) - @test num_constraints(jprob.model) == 2 == num_variables(jprob.model) == 2 - jsol = solve(prob, HiGHS.Optimizer, :Tsitouras5) - sol = jsol.sol - @test sol(0.6)[1] ≈ 3.5 - @test sol(0.3)[1] ≈ 7.0 -end - -@testset "Optimal control problem" begin -end From ab2c777e24e1331ebabb2bc4e502d5ec0980ab5a Mon Sep 17 00:00:00 2001 From: vyudu Date: Mon, 5 May 2025 22:11:17 -0400 Subject: [PATCH 1505/2176] bump CasADi compat --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 33280a5064..ccbe65c6ab 100644 --- a/Project.toml +++ b/Project.toml @@ -88,7 +88,7 @@ BifurcationKit = "0.4" BlockArrays = "1.1" BoundaryValueDiffEqAscher = "1.1.0" BoundaryValueDiffEqMIRK = "1.4.0" -CasADi = "1.0.0" +CasADi = "1.0.2" ChainRulesCore = "1" Combinatorics = "1" CommonSolve = "0.2.4" From b2845736c0ee0dc57e8d3ac13da63849f935ac27 Mon Sep 17 00:00:00 2001 From: Christopher Rackauckas Date: Tue, 6 May 2025 09:47:09 -0400 Subject: [PATCH 1506/2176] Update Project.toml --- test/downstream/Project.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/test/downstream/Project.toml b/test/downstream/Project.toml index f64ed17de6..8de7753346 100644 --- a/test/downstream/Project.toml +++ b/test/downstream/Project.toml @@ -1,4 +1,5 @@ [deps] +CasADi = "c49709b8-5c63-11e9-2fb2-69db5844192f" ControlSystemsMTK = "687d7614-c7e5-45fc-bfc3-9ee385575c88" DataInterpolations = "82cc6244-b520-54b8-b5a6-8a565e85f1d0" LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" From 189f8330c1937beb6d48c5905aab637105be0648 Mon Sep 17 00:00:00 2001 From: vyudu Date: Wed, 7 May 2025 11:29:56 -0400 Subject: [PATCH 1507/2176] bump Project toml --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index ccbe65c6ab..f94d84b000 100644 --- a/Project.toml +++ b/Project.toml @@ -88,7 +88,7 @@ BifurcationKit = "0.4" BlockArrays = "1.1" BoundaryValueDiffEqAscher = "1.1.0" BoundaryValueDiffEqMIRK = "1.4.0" -CasADi = "1.0.2" +CasADi = "1.0.3" ChainRulesCore = "1" Combinatorics = "1" CommonSolve = "0.2.4" From 28a5e74c845595e5dce884c0637786b911900304 Mon Sep 17 00:00:00 2001 From: vyudu Date: Wed, 7 May 2025 12:31:07 -0400 Subject: [PATCH 1508/2176] remove is_explicit from jump --- ext/MTKInfiniteOptExt.jl | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/ext/MTKInfiniteOptExt.jl b/ext/MTKInfiniteOptExt.jl index 7c374cfdaf..e36b9c2a3e 100644 --- a/ext/MTKInfiniteOptExt.jl +++ b/ext/MTKInfiniteOptExt.jl @@ -257,8 +257,6 @@ function substitute_jump_vars(model, sys, pmap, exprs; auxmap = Dict(), is_free_ exprs end -is_explicit(tableau) = tableau isa DiffEqBase.ExplicitRKTableau - function add_infopt_solve_constraints!(model::InfiniteModel, sys, pmap; is_free_t = false) # Differential equations U = model[:U] @@ -295,7 +293,7 @@ function add_jump_solve_constraints!(prob, tableau; is_free_t = false) V = model[:V] nᵤ = length(U) nᵥ = length(V) - if is_explicit(tableau) + if MTK.is_explicit(tableau) K = Any[] for τ in tsteps for (i, h) in enumerate(c) From 54c01f6de0b4d6e326fc4c98e7daf4f136e69628 Mon Sep 17 00:00:00 2001 From: vyudu Date: Wed, 7 May 2025 14:24:06 -0400 Subject: [PATCH 1509/2176] bump downstream casadi compat --- test/downstream/Project.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/test/downstream/Project.toml b/test/downstream/Project.toml index 8de7753346..73e9b0becc 100644 --- a/test/downstream/Project.toml +++ b/test/downstream/Project.toml @@ -16,3 +16,4 @@ SymbolicIndexingInterface = "2efcf032-c050-4f8e-a9bb-153293bab1f5" [compat] ModelingToolkitStandardLibrary = "2.19" +CasADi = "1.0.3" From 47d9de4ed5b69761ce8f8bf75ee304f32d992800 Mon Sep 17 00:00:00 2001 From: vyudu Date: Thu, 8 May 2025 17:03:04 -0400 Subject: [PATCH 1510/2176] fix: bump CasADi compat to 1.0.5 --- Project.toml | 2 +- test/downstream/Project.toml | 2 +- test/downstream/dynamic_optimization.jl | 1 + 3 files changed, 3 insertions(+), 2 deletions(-) diff --git a/Project.toml b/Project.toml index f94d84b000..a38fdc99cc 100644 --- a/Project.toml +++ b/Project.toml @@ -88,7 +88,7 @@ BifurcationKit = "0.4" BlockArrays = "1.1" BoundaryValueDiffEqAscher = "1.1.0" BoundaryValueDiffEqMIRK = "1.4.0" -CasADi = "1.0.3" +CasADi = "1.0.5" ChainRulesCore = "1" Combinatorics = "1" CommonSolve = "0.2.4" diff --git a/test/downstream/Project.toml b/test/downstream/Project.toml index 73e9b0becc..9b3f79487b 100644 --- a/test/downstream/Project.toml +++ b/test/downstream/Project.toml @@ -16,4 +16,4 @@ SymbolicIndexingInterface = "2efcf032-c050-4f8e-a9bb-153293bab1f5" [compat] ModelingToolkitStandardLibrary = "2.19" -CasADi = "1.0.3" +CasADi = "1.0.5" diff --git a/test/downstream/dynamic_optimization.jl b/test/downstream/dynamic_optimization.jl index abb534ea0b..4b3b4edeb6 100644 --- a/test/downstream/dynamic_optimization.jl +++ b/test/downstream/dynamic_optimization.jl @@ -7,6 +7,7 @@ using Ipopt using DataInterpolations using CasADi +import DiffEqBase: solve const M = ModelingToolkit @testset "ODE Solution, no cost" begin From a9f6aea5e381299dd60c7958b37c755d2719861d Mon Sep 17 00:00:00 2001 From: vyudu Date: Thu, 8 May 2025 17:28:18 -0400 Subject: [PATCH 1511/2176] rebase: move tests --- test/downstream/Project.toml | 2 -- test/extensions/Project.toml | 4 ++++ test/{downstream => extensions}/dynamic_optimization.jl | 0 3 files changed, 4 insertions(+), 2 deletions(-) rename test/{downstream => extensions}/dynamic_optimization.jl (100%) diff --git a/test/downstream/Project.toml b/test/downstream/Project.toml index 9b3f79487b..f64ed17de6 100644 --- a/test/downstream/Project.toml +++ b/test/downstream/Project.toml @@ -1,5 +1,4 @@ [deps] -CasADi = "c49709b8-5c63-11e9-2fb2-69db5844192f" ControlSystemsMTK = "687d7614-c7e5-45fc-bfc3-9ee385575c88" DataInterpolations = "82cc6244-b520-54b8-b5a6-8a565e85f1d0" LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" @@ -16,4 +15,3 @@ SymbolicIndexingInterface = "2efcf032-c050-4f8e-a9bb-153293bab1f5" [compat] ModelingToolkitStandardLibrary = "2.19" -CasADi = "1.0.5" diff --git a/test/extensions/Project.toml b/test/extensions/Project.toml index 0e018b4a22..e4f59dbec7 100644 --- a/test/extensions/Project.toml +++ b/test/extensions/Project.toml @@ -1,5 +1,6 @@ [deps] BifurcationKit = "0f109fa4-8a5d-4b75-95aa-f515264e7665" +CasADi = "c49709b8-5c63-11e9-2fb2-69db5844192f" ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4" ChainRulesTestUtils = "cdddcdb0-9152-4a09-a978-84456f9df70a" DataInterpolations = "82cc6244-b520-54b8-b5a6-8a565e85f1d0" @@ -25,3 +26,6 @@ SimpleDiffEq = "05bca326-078c-5bf0-a5bf-ce7c7982d7fd" SymbolicIndexingInterface = "2efcf032-c050-4f8e-a9bb-153293bab1f5" Symbolics = "0c5d862f-8b57-4792-8d23-62f2024744c7" Zygote = "e88e6eb3-aa80-5325-afca-941959d7151f" + +[compat] +CasADi = "1.0.5" diff --git a/test/downstream/dynamic_optimization.jl b/test/extensions/dynamic_optimization.jl similarity index 100% rename from test/downstream/dynamic_optimization.jl rename to test/extensions/dynamic_optimization.jl From 7d561a3a3a703bf51a50854f326626fe476e7ffd Mon Sep 17 00:00:00 2001 From: vyudu Date: Thu, 8 May 2025 19:25:53 -0400 Subject: [PATCH 1512/2176] rename file in runtests --- test/runtests.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/runtests.jl b/test/runtests.jl index 80339c6606..1b8b5e03db 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -136,7 +136,7 @@ end if GROUP == "All" || GROUP == "Extensions" activate_extensions_env() - @safetestset "JuMP Collocation Solvers" include("extensions/jump_control.jl") + @safetestset "Dynamic Optimization Collocation Solvers" include("extensions/dynamic_optimization.jl") @safetestset "HomotopyContinuation Extension Test" include("extensions/homotopy_continuation.jl") @safetestset "Auto Differentiation Test" include("extensions/ad.jl") @safetestset "LabelledArrays Test" include("labelledarrays.jl") From e1e0b90a16399bf9ccf74b89b8649e07f29710e5 Mon Sep 17 00:00:00 2001 From: vyudu Date: Thu, 8 May 2025 20:18:18 -0400 Subject: [PATCH 1513/2176] bump Casadi compat --- Project.toml | 2 +- test/extensions/Project.toml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Project.toml b/Project.toml index a38fdc99cc..9538bb0894 100644 --- a/Project.toml +++ b/Project.toml @@ -88,7 +88,7 @@ BifurcationKit = "0.4" BlockArrays = "1.1" BoundaryValueDiffEqAscher = "1.1.0" BoundaryValueDiffEqMIRK = "1.4.0" -CasADi = "1.0.5" +CasADi = "1.0.6" ChainRulesCore = "1" Combinatorics = "1" CommonSolve = "0.2.4" diff --git a/test/extensions/Project.toml b/test/extensions/Project.toml index e4f59dbec7..9f43e6f4a4 100644 --- a/test/extensions/Project.toml +++ b/test/extensions/Project.toml @@ -28,4 +28,4 @@ Symbolics = "0c5d862f-8b57-4792-8d23-62f2024744c7" Zygote = "e88e6eb3-aa80-5325-afca-941959d7151f" [compat] -CasADi = "1.0.5" +CasADi = "1.0.6" From 32a7781951e0f0e036a3d6a43783635caff120f2 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 9 May 2025 13:38:14 +0530 Subject: [PATCH 1514/2176] fix: use `Diagonal` for diagonal mass matrices --- src/systems/diffeqs/abstractodesystem.jl | 5 +++++ src/systems/diffeqs/sdesystem.jl | 10 +++++++++- test/mass_matrix.jl | 21 ++++++++++++++++++++- 3 files changed, 34 insertions(+), 2 deletions(-) diff --git a/src/systems/diffeqs/abstractodesystem.jl b/src/systems/diffeqs/abstractodesystem.jl index e66f03a85e..942e508644 100644 --- a/src/systems/diffeqs/abstractodesystem.jl +++ b/src/systems/diffeqs/abstractodesystem.jl @@ -284,6 +284,9 @@ function calculate_massmatrix(sys::AbstractODESystem; simplify = false) end M = simplify ? ModelingToolkit.simplify.(M) : M # M should only contain concrete numbers + if isdiag(M) + M = Diagonal(M) + end M == I ? I : M end @@ -410,6 +413,8 @@ function DiffEqBase.ODEFunction{iip, specialize}(sys::AbstractODESystem, SparseArrays.sparse(M) elseif u0 === nothing || M === I M + elseif M isa Diagonal + Diagonal(ArrayInterface.restructure(u0, diag(M))) else ArrayInterface.restructure(u0 .* u0', M) end diff --git a/src/systems/diffeqs/sdesystem.jl b/src/systems/diffeqs/sdesystem.jl index c5299c28be..d743143e46 100644 --- a/src/systems/diffeqs/sdesystem.jl +++ b/src/systems/diffeqs/sdesystem.jl @@ -652,7 +652,15 @@ function DiffEqBase.SDEFunction{iip, specialize}(sys::SDESystem, dvs = unknowns( W_prototype = nothing end - _M = (u0 === nothing || M == I) ? M : ArrayInterface.restructure(u0 .* u0', M) + _M = if sparse && !(u0 === nothing || M === I) + SparseArrays.sparse(M) + elseif u0 === nothing || M === I + M + elseif M isa Diagonal + Diagonal(ArrayInterface.restructure(u0, diag(M))) + else + ArrayInterface.restructure(u0 .* u0', M) + end observedfun = ObservedFunctionCache( sys; eval_expression, eval_module, checkbounds = get(kwargs, :checkbounds, false), cse) diff --git a/test/mass_matrix.jl b/test/mass_matrix.jl index 5183b4ab3f..8b31123834 100644 --- a/test/mass_matrix.jl +++ b/test/mass_matrix.jl @@ -1,4 +1,4 @@ -using OrdinaryDiffEq, ModelingToolkit, Test, LinearAlgebra +using OrdinaryDiffEq, ModelingToolkit, Test, LinearAlgebra, StaticArrays using ModelingToolkit: t_nounits as t, D_nounits as D, MTKParameters @variables y(t)[1:3] @@ -12,13 +12,18 @@ eqs = [D(y[1]) ~ -k[1] * y[1] + k[3] * y[2] * y[3], sys = complete(sys) @test_throws ArgumentError ODESystem(eqs, y[1]) M = calculate_massmatrix(sys) +@test M isa Diagonal @test M == [1 0 0 0 1 0 0 0 0] prob_mm = ODEProblem(sys, [y => [1.0, 0.0, 0.0]], (0.0, 1e5), [k => [0.04, 3e7, 1e4]]) +@test prob_mm.f.mass_matrix isa Diagonal{Float64, Vector{Float64}} sol = solve(prob_mm, Rodas5(), reltol = 1e-8, abstol = 1e-8) +prob_mm = ODEProblem(sys, SA[y => [1.0, 0.0, 0.0]], (0.0, 1e5), + [k => [0.04, 3e7, 1e4]]) +@test prob_mm.f.mass_matrix isa Diagonal{Float64, SVector{3, Float64}} function rober(du, u, p, t) y₁, y₂, y₃ = u @@ -43,3 +48,17 @@ eqs = [D(y[1]) ~ y[1], D(y[2]) ~ y[2], D(y[3]) ~ y[3]] @named sys = ODESystem(eqs, t, collect(y), [k]) @test calculate_massmatrix(sys) === I + +@testset "Mass matrix `isa Diagonal` for `SDEProblem`" begin + eqs = [D(y[1]) ~ -k[1] * y[1] + k[3] * y[2] * y[3], + D(y[2]) ~ k[1] * y[1] - k[3] * y[2] * y[3] - k[2] * y[2]^2, + 0 ~ y[1] + y[2] + y[3] - 1] + + @named sys = ODESystem(eqs, t, collect(y), [k]) + @named sys = SDESystem(sys, [1, 1, 0]) + sys = complete(sys) + prob = SDEProblem(sys, [y => [1.0, 0.0, 0.0]], (0.0, 1e5), [k => [0.04, 3e7, 1e4]]) + @test prob.f.mass_matrix isa Diagonal{Float64, Vector{Float64}} + prob = SDEProblem(sys, SA[y => [1.0, 0.0, 0.0]], (0.0, 1e5), [k => [0.04, 3e7, 1e4]]) + @test prob.f.mass_matrix isa Diagonal{Float64, SVector{3, Float64}} +end From 155bbc2f731d37e944be041f3dc3e51bb71fdb83 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 9 May 2025 15:02:08 +0530 Subject: [PATCH 1515/2176] refactor: add error message for wrapped variable in `collect_var!` --- src/utils.jl | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/utils.jl b/src/utils.jl index d0fd3d40f9..90431a7749 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -606,6 +606,17 @@ end function collect_var!(unknowns, parameters, var, iv; depth = 0) isequal(var, iv) && return nothing + if Symbolics.iswrapped(var) + error(""" + Internal Error. Please open an issue with an MWE. + + Encountered a wrapped value in `collect_var!`. This function should only ever \ + receive unwrapped symbolic variables. This is likely a bug in the code generating \ + an expression passed to `collect_vars!` or `collect_scoped_vars!`. A common cause \ + is using `substitute` or `fast_substitute` with rules where the values are \ + wrapped symbolic variables. + """) + end check_scope_depth(getmetadata(var, SymScope, LocalScope()), depth) || return nothing var = setmetadata(var, SymScope, LocalScope()) if iscalledparameter(var) From 834154cb5b2b99052fbab00f07cabd04ead15e6e Mon Sep 17 00:00:00 2001 From: Andrew Leonard Date: Sat, 10 May 2025 06:56:31 -0400 Subject: [PATCH 1516/2176] fix change of var for sys with no equations --- src/systems/diffeqs/basic_transformations.jl | 4 +-- test/basic_transformations.jl | 28 ++++++++++++++++++++ 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/src/systems/diffeqs/basic_transformations.jl b/src/systems/diffeqs/basic_transformations.jl index d41e948d64..1a09de1fc0 100644 --- a/src/systems/diffeqs/basic_transformations.jl +++ b/src/systems/diffeqs/basic_transformations.jl @@ -155,7 +155,7 @@ function change_independent_variable( # Create a utility that performs the chain rule on an expression, followed by insertion of the new independent variable: # e.g. (d/dt)(f(t)) -> (d/dt)(f(u(t))) -> df(u(t))/du(t) * du(t)/dt -> df(u)/du * uˍt(u) - function transform(ex) + function transform(ex::T) where {T} # 1) Replace the argument of every function; e.g. f(t) -> f(u(t)) for var in vars(ex; op = Nothing) # loop over all variables in expression (op = Nothing prevents interpreting "D(f(t))" as one big variable) is_function_of_iv1 = iscall(var) && isequal(only(arguments(var)), iv1) # of the form f(t)? @@ -175,7 +175,7 @@ function change_independent_variable( # 3) Set new independent variable ex = substitute(ex, iv2_of_iv1 => iv2; fold) # set e.g. u(t) -> u everywhere ex = substitute(ex, iv1 => iv1_of_iv2; fold) # set e.g. t -> t(u) everywhere - return ex + return ex::T end # Use the utility function to transform everything in the system! diff --git a/test/basic_transformations.jl b/test/basic_transformations.jl index b593deb345..173c4ad062 100644 --- a/test/basic_transformations.jl +++ b/test/basic_transformations.jl @@ -231,3 +231,31 @@ end # compare to analytical solution (x(t) = v*t, y(t) = v*t - g*t^2/2) @test all(isapprox.(sol[Mx.y], sol[Mx.x - g * (Mx.t_units)^2 / 2]; atol = 1e-10)) end + +@testset "Change independent variable, no equations" begin + # make this "look" like the standard library RealInput + @mtkmodel Input begin + @variables begin + u(t) + end + end + @named input_sys = Input() + input_sys = complete(input_sys) + # test no failures + @test change_independent_variable(input_sys, input_sys.u) isa ODESystem + + @mtkmodel NestedInput begin + @components begin + in = Input() + end + @variables begin + x(t) + end + @equations begin + D(x) ~ in.u + end + end + @named nested_input_sys = NestedInput() + nested_input_sys = complete(nested_input_sys; flatten = false) + @test change_independent_variable(nested_input_sys, nested_input_sys.x) isa ODESystem +end From 1f443a67d30bda8ab3f93ea53d1275e0ce7cae77 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Sat, 10 May 2025 14:32:08 +0530 Subject: [PATCH 1517/2176] feat: add CI for testing against released package versions --- .github/workflows/ReleaseTest.yml | 62 +++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 .github/workflows/ReleaseTest.yml diff --git a/.github/workflows/ReleaseTest.yml b/.github/workflows/ReleaseTest.yml new file mode 100644 index 0000000000..f8b592e9d0 --- /dev/null +++ b/.github/workflows/ReleaseTest.yml @@ -0,0 +1,62 @@ +name: ReleaseTest +on: + push: + branches: [master] + tags: [v*] + pull_request: + paths-ignore: + - 'docs/**' + +concurrency: + # Skip intermediate builds: always, but for the master branch and tags. + # Cancel intermediate builds: always, but for the master branch and tags. + group: ${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: ${{ github.ref != 'refs/heads/master' && github.refs != 'refs/tags/*' }} + +jobs: + test: + name: ${{ matrix.package.package }}/${{ matrix.package.group }}/${{ matrix.julia-version }} + runs-on: ${{ matrix.os }} + env: + GROUP: ${{ matrix.package.group }} + strategy: + fail-fast: false + matrix: + julia-version: [1] + os: [ubuntu-latest] + package: + - {package: Catalyst, group: All} + steps: + - uses: actions/checkout@v4 + - uses: julia-actions/setup-julia@v1 + with: + version: ${{ matrix.julia-version }} + arch: x64 + - uses: julia-actions/julia-buildpkg@latest + - name: Create test directory + run: mkdir downstream + - name: Load this and run the downstream tests + shell: julia --color=yes --project=downstream {0} + run: | + using Pkg + try + Pkg.activate("downstream") + # force it to use this PR's version of the package + Pkg.develop(PackageSpec(path=".")) # resolver may fail with main deps + Pkg.add("${{ matrix.package.package }}") + Pkg.update() + Pkg.test("Catalyst"; coverage=true) # resolver may fail with test time deps + catch err + err isa Pkg.Resolve.ResolverError || rethrow() + # If we can't resolve that means this is incompatible by SemVer and this is fine + # It means we marked this as a breaking change, so we don't need to worry about + # Mistakenly introducing a breaking change, as we have intentionally made one + @info "Not compatible with this release. No problem." exception=err + exit(0) # Exit immediately, as a success + end + - uses: julia-actions/julia-processcoverage@v1 + - uses: codecov/codecov-action@v5 + with: + files: lcov.info + token: ${{ secrets.CODECOV_TOKEN }} + fail_ci_if_error: false From 5657aabd892479617434a6ce2ba722b9d701f7d5 Mon Sep 17 00:00:00 2001 From: David Widmann Date: Mon, 12 May 2025 09:25:02 +0200 Subject: [PATCH 1518/2176] Less restrictive `promote_u0_p` --- src/systems/nonlinear/initializesystem.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/systems/nonlinear/initializesystem.jl b/src/systems/nonlinear/initializesystem.jl index 5995272be9..c9d0c8f3a5 100644 --- a/src/systems/nonlinear/initializesystem.jl +++ b/src/systems/nonlinear/initializesystem.jl @@ -601,7 +601,7 @@ function promote_u0_p(u0, p::MTKParameters, t0) return u0, p end -function promote_u0_p(u0, p::AbstractArray, t0) +function promote_u0_p(u0, p, t0) return DiffEqBase.promote_u0(u0, p, t0), DiffEqBase.promote_u0(p, u0, t0) end From 365f5ed05890fa9cd36dc81357a81f54ddc41f99 Mon Sep 17 00:00:00 2001 From: Andrew Leonard Date: Mon, 12 May 2025 05:36:48 -0400 Subject: [PATCH 1519/2176] handle connections in change_independent_variable --- src/systems/diffeqs/basic_transformations.jl | 36 ++++++++++++++++---- test/basic_transformations.jl | 28 +++++++++++++++ 2 files changed, 58 insertions(+), 6 deletions(-) diff --git a/src/systems/diffeqs/basic_transformations.jl b/src/systems/diffeqs/basic_transformations.jl index 1a09de1fc0..a4b81f6103 100644 --- a/src/systems/diffeqs/basic_transformations.jl +++ b/src/systems/diffeqs/basic_transformations.jl @@ -53,6 +53,17 @@ function liouville_transform(sys::AbstractODESystem; kwargs...) ) end +function split_eqs_connections(eqs_in::Vector{<:Equation}) + eqs = Equation[] + cons = Equation[] + + for eq in eqs_in + eq.lhs isa Connection ? push!(cons, eq) : push!(eqs, eq) + end + + return eqs, cons +end + """ change_independent_variable( sys::AbstractODESystem, iv, eqs = []; @@ -158,7 +169,7 @@ function change_independent_variable( function transform(ex::T) where {T} # 1) Replace the argument of every function; e.g. f(t) -> f(u(t)) for var in vars(ex; op = Nothing) # loop over all variables in expression (op = Nothing prevents interpreting "D(f(t))" as one big variable) - is_function_of_iv1 = iscall(var) && isequal(only(arguments(var)), iv1) # of the form f(t)? + is_function_of_iv1 = iscall(var) && isequal(first(arguments(var)), iv1) # of the form f(t)? if is_function_of_iv1 && !isequal(var, iv2_of_iv1) # prevent e.g. u(t) -> u(u(t)) var_of_iv1 = var # e.g. f(t) var_of_iv2_of_iv1 = substitute(var_of_iv1, iv1 => iv2_of_iv1) # e.g. f(u(t)) @@ -178,9 +189,22 @@ function change_independent_variable( return ex::T end + # overload to specifically handle equations, which can be an equation of a connection + function transform(eq::Equation, systems_map) + if eq.rhs isa Connection + eq = connect((systems_map[nameof(s)] for s in eq.rhs.systems)...) + else + eq = transform(eq) + end + return eq::Equation + end + # Use the utility function to transform everything in the system! function transform(sys::AbstractODESystem) - eqs = map(transform, get_eqs(sys)) + systems = map(transform, get_systems(sys)) # recurse through subsystems + # transform equations and connections + systems_map = Dict(get_name(s) => s for s in systems) + eqs = map(eq -> transform(eq, systems_map)::Equation, get_eqs(sys)) unknowns = map(transform, get_unknowns(sys)) unknowns = filter(var -> !isequal(var, iv2), unknowns) # remove e.g. u ps = map(transform, get_ps(sys)) @@ -191,19 +215,19 @@ function change_independent_variable( defaults = Dict(transform(var) => transform(val) for (var, val) in get_defaults(sys)) guesses = Dict(transform(var) => transform(val) for (var, val) in get_guesses(sys)) + connector_type = get_connector_type(sys) assertions = Dict(transform(ass) => msg for (ass, msg) in get_assertions(sys)) - systems = get_systems(sys) # save before reconstructing system wascomplete = iscomplete(sys) # save before reconstructing system sys = typeof(sys)( # recreate system with transformed fields eqs, iv2, unknowns, ps; observed, initialization_eqs, - parameter_dependencies, defaults, guesses, + parameter_dependencies, defaults, guesses, connector_type, assertions, name = nameof(sys), description = description(sys) ) - systems = map(transform, systems) # recurse through subsystems sys = compose(sys, systems) # rebuild hierarchical system if wascomplete wasflat = isempty(systems) - sys = complete(sys; flatten = wasflat) # complete output if input was complete + wassplit = is_split(sys) + sys = complete(sys; split = wassplit, flatten = wasflat) # complete output if input was complete end return sys end diff --git a/test/basic_transformations.jl b/test/basic_transformations.jl index 173c4ad062..41923f973c 100644 --- a/test/basic_transformations.jl +++ b/test/basic_transformations.jl @@ -1,4 +1,5 @@ using ModelingToolkit, OrdinaryDiffEq, DataInterpolations, DynamicQuantities, Test +using ModelingToolkitStandardLibrary.Blocks: RealInput, RealOutput @independent_variables t D = Differential(t) @@ -259,3 +260,30 @@ end nested_input_sys = complete(nested_input_sys; flatten = false) @test change_independent_variable(nested_input_sys, nested_input_sys.x) isa ODESystem end + +@testset "Change of variables, connections" begin + @mtkmodel ConnectSys begin + @components begin + in = RealInput() + out = RealOutput() + end + @variables begin + x(t) + y(t) + end + @equations begin + connect(in, out) + in.u ~ x + D(x) ~ -out.u + D(y) ~ 1 + end + end + @named sys = ConnectSys() + sys = complete(sys; flatten = false) + new_sys = change_independent_variable(sys, sys.y; add_old_diff = true) + ss = structural_simplify(new_sys; allow_symbolic = true) + prob = ODEProblem(ss, [ss.t => 0.0, ss.x => 1.0], (0.0, 1.0)) + sol = solve(prob, Tsit5(); reltol = 1e-5) + @test all(isapprox.(sol[ss.t], sol[ss.y]; atol = 1e-10)) + @test all(sol[ss.x][2:end] .< sol[ss.x][1]) +end From 42dad86b26b62b12fafdca1396fe3c56ece8cb52 Mon Sep 17 00:00:00 2001 From: Andrew Leonard Date: Mon, 12 May 2025 05:39:13 -0400 Subject: [PATCH 1520/2176] rm unused code --- src/systems/diffeqs/basic_transformations.jl | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/src/systems/diffeqs/basic_transformations.jl b/src/systems/diffeqs/basic_transformations.jl index a4b81f6103..672ec6d058 100644 --- a/src/systems/diffeqs/basic_transformations.jl +++ b/src/systems/diffeqs/basic_transformations.jl @@ -53,17 +53,6 @@ function liouville_transform(sys::AbstractODESystem; kwargs...) ) end -function split_eqs_connections(eqs_in::Vector{<:Equation}) - eqs = Equation[] - cons = Equation[] - - for eq in eqs_in - eq.lhs isa Connection ? push!(cons, eq) : push!(eqs, eq) - end - - return eqs, cons -end - """ change_independent_variable( sys::AbstractODESystem, iv, eqs = []; From 51b3be15fd95326aa702e546d6c2b31f61caeb16 Mon Sep 17 00:00:00 2001 From: Andrew Leonard Date: Mon, 12 May 2025 05:39:42 -0400 Subject: [PATCH 1521/2176] typo --- src/systems/diffeqs/basic_transformations.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/systems/diffeqs/basic_transformations.jl b/src/systems/diffeqs/basic_transformations.jl index 672ec6d058..67eff62cdd 100644 --- a/src/systems/diffeqs/basic_transformations.jl +++ b/src/systems/diffeqs/basic_transformations.jl @@ -178,7 +178,7 @@ function change_independent_variable( return ex::T end - # overload to specifically handle equations, which can be an equation of a connection + # overload to specifically handle equations, which can be an equation or a connection function transform(eq::Equation, systems_map) if eq.rhs isa Connection eq = connect((systems_map[nameof(s)] for s in eq.rhs.systems)...) From 05ebbdca52946d0b96d8ed6015cd3243f683aa3d Mon Sep 17 00:00:00 2001 From: Andrew Leonard Date: Mon, 12 May 2025 08:19:59 -0400 Subject: [PATCH 1522/2176] revert first to only --- src/systems/diffeqs/basic_transformations.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/systems/diffeqs/basic_transformations.jl b/src/systems/diffeqs/basic_transformations.jl index 67eff62cdd..66571fb302 100644 --- a/src/systems/diffeqs/basic_transformations.jl +++ b/src/systems/diffeqs/basic_transformations.jl @@ -158,7 +158,7 @@ function change_independent_variable( function transform(ex::T) where {T} # 1) Replace the argument of every function; e.g. f(t) -> f(u(t)) for var in vars(ex; op = Nothing) # loop over all variables in expression (op = Nothing prevents interpreting "D(f(t))" as one big variable) - is_function_of_iv1 = iscall(var) && isequal(first(arguments(var)), iv1) # of the form f(t)? + is_function_of_iv1 = iscall(var) && isequal(only(arguments(var)), iv1) # of the form f(t)? if is_function_of_iv1 && !isequal(var, iv2_of_iv1) # prevent e.g. u(t) -> u(u(t)) var_of_iv1 = var # e.g. f(t) var_of_iv2_of_iv1 = substitute(var_of_iv1, iv1 => iv2_of_iv1) # e.g. f(u(t)) From 66e33923c0bf74642a2200c32a3141a0f9ac4df0 Mon Sep 17 00:00:00 2001 From: Qingyu Qu <2283984853@qq.com> Date: Mon, 12 May 2025 20:21:52 +0800 Subject: [PATCH 1523/2176] BVP tests are working fine --- Project.toml | 4 +- ext/MTKCasADiDynamicOptExt.jl | 66 +++++++++++++----------- src/ModelingToolkit.jl | 3 +- src/systems/optimal_control_interface.jl | 2 +- test/bvproblem.jl | 8 +-- test/extensions/dynamic_optimization.jl | 8 +-- 6 files changed, 50 insertions(+), 41 deletions(-) diff --git a/Project.toml b/Project.toml index 9538bb0894..c98e6eb2ac 100644 --- a/Project.toml +++ b/Project.toml @@ -86,8 +86,8 @@ AbstractTrees = "0.3, 0.4" ArrayInterface = "6, 7" BifurcationKit = "0.4" BlockArrays = "1.1" -BoundaryValueDiffEqAscher = "1.1.0" -BoundaryValueDiffEqMIRK = "1.4.0" +BoundaryValueDiffEqAscher = "1.6.0" +BoundaryValueDiffEqMIRK = "1.8.0" CasADi = "1.0.6" ChainRulesCore = "1" Combinatorics = "1" diff --git a/ext/MTKCasADiDynamicOptExt.jl b/ext/MTKCasADiDynamicOptExt.jl index 9d16a3ea5c..6aa15fb55a 100644 --- a/ext/MTKCasADiDynamicOptExt.jl +++ b/ext/MTKCasADiDynamicOptExt.jl @@ -42,14 +42,14 @@ struct CasADiDynamicOptProblem{uType, tType, isinplace, P, F, K} <: end end -function (M::MXLinearInterpolation)(τ) +function (M::MXLinearInterpolation)(τ) nt = (τ - M.t[1]) / M.dt i = 1 + floor(Int, nt) Δ = nt - i + 1 (i > length(M.t) || i < 1) && error("Cannot extrapolate past the tspan.") if i < length(M.t) - M.u[:, i] + Δ*(M.u[:, i + 1] - M.u[:, i]) + M.u[:, i] + Δ * (M.u[:, i + 1] - M.u[:, i]) else M.u[:, i] end @@ -74,7 +74,7 @@ The constraints are: function MTK.CasADiDynamicOptProblem(sys::ODESystem, u0map, tspan, pmap; dt = nothing, steps = nothing, - guesses = Dict(), kwargs...) + guesses = Dict(), kwargs...) MTK.warn_overdetermined(sys, u0map) _u0map = has_alg_eqs(sys) ? u0map : merge(Dict(u0map), Dict(guesses)) f, u0, p = MTK.process_SciMLProblem(ODEInputFunction, sys, _u0map, pmap; @@ -104,21 +104,21 @@ function init_model(sys, tspan, steps, u0map, pmap, u0; is_free_t = false) subject_to!(opti, tₛ >= lo) subject_to!(opti, tₛ >= hi) end - pmap[te_sym] = tₛ + pmap[te_sym] = tₛ tsteps = LinRange(0, 1, steps) else tₛ = MX(1) tsteps = LinRange(tspan[1], tspan[2], steps) end - + U = CasADi.variable!(opti, length(states), steps) V = CasADi.variable!(opti, length(ctrls), steps) set_initial!(opti, U, DM(repeat(u0, 1, steps))) c0 = MTK.value.([pmap[c] for c in ctrls]) !isempty(c0) && set_initial!(opti, V, DM(repeat(c0, 1, steps))) - U_interp = MXLinearInterpolation(U, tsteps, tsteps[2]-tsteps[1]) - V_interp = MXLinearInterpolation(V, tsteps, tsteps[2]-tsteps[1]) + U_interp = MXLinearInterpolation(U, tsteps, tsteps[2] - tsteps[1]) + V_interp = MXLinearInterpolation(V, tsteps, tsteps[2] - tsteps[1]) for (i, ct) in enumerate(ctrls) pmap[ct] = V[i, :] end @@ -185,8 +185,8 @@ function add_user_constraints!(model::CasADiModel, sys, tspan, pmap; is_free_t) x = MTK.operation(st) t = only(MTK.arguments(st)) MTK.symbolic_type(t) === MTK.NotSymbolic() || continue - if haskey(stidxmap, x(iv)) - idx = stidxmap[x(iv)] + if haskey(stidxmap, x(iv)) + idx = stidxmap[x(iv)] cv = U else idx = ctidxmap[x(iv)] @@ -196,11 +196,11 @@ function add_user_constraints!(model::CasADiModel, sys, tspan, pmap; is_free_t) end if cons isa Equation - subject_to!(opti, cons.lhs - cons.rhs==0) + subject_to!(opti, cons.lhs - cons.rhs == 0) elseif cons.relational_op === Symbolics.geq - subject_to!(opti, cons.lhs - cons.rhs≥0) + subject_to!(opti, cons.lhs - cons.rhs ≥ 0) else - subject_to!(opti, cons.lhs - cons.rhs≤0) + subject_to!(opti, cons.lhs - cons.rhs ≤ 0) end end end @@ -227,8 +227,8 @@ function add_cost_function!(model::CasADiModel, sys, tspan, pmap; is_free_t) x = operation(st) t = only(arguments(st)) MTK.symbolic_type(t) === MTK.NotSymbolic() || continue - if haskey(stidxmap, x(iv)) - idx = stidxmap[x(iv)] + if haskey(stidxmap, x(iv)) + idx = stidxmap[x(iv)] cv = U else idx = ctidxmap[x(iv)] @@ -244,7 +244,8 @@ function add_cost_function!(model::CasADiModel, sys, tspan, pmap; is_free_t) op = MTK.operation(int) arg = only(arguments(MTK.value(int))) lo, hi = (op.domain.domain.left, op.domain.domain.right) - !isequal((lo, hi), tspan) && error("Non-whole interval bounds for integrals are not currently supported for CasADiDynamicOptProblem.") + !isequal((lo, hi), tspan) && + error("Non-whole interval bounds for integrals are not currently supported for CasADiDynamicOptProblem.") # Approximate integral as sum. intmap[int] = dt * tₛ * sum(arg) end @@ -253,7 +254,8 @@ function add_cost_function!(model::CasADiModel, sys, tspan, pmap; is_free_t) minimize!(opti, MX(MTK.value(consolidate(jcosts)))) end -function substitute_casadi_vars(model::CasADiModel, sys, pmap, exprs; auxmap::Dict = Dict(), is_free_t) +function substitute_casadi_vars( + model::CasADiModel, sys, pmap, exprs; auxmap::Dict = Dict(), is_free_t) @unpack opti, U, V, tₛ = model iv = MTK.get_iv(sys) sts = unknowns(sys) @@ -281,11 +283,11 @@ end function add_solve_constraints(prob, tableau) @unpack A, α, c = tableau - @unpack model, f, p = prob + @unpack model, f, p = prob @unpack opti, U, V, tₛ = model solver_opti = copy(opti) - tsteps = U.t + tsteps = U.t dt = tsteps[2] - tsteps[1] nᵤ = size(U.u, 1) @@ -293,32 +295,32 @@ function add_solve_constraints(prob, tableau) if MTK.is_explicit(tableau) K = MX[] - for k in 1:length(tsteps)-1 + for k in 1:(length(tsteps) - 1) τ = tsteps[k] for (i, h) in enumerate(c) ΔU = sum([A[i, j] * K[j] for j in 1:(i - 1)], init = MX(zeros(nᵤ))) - Uₙ = U.u[:, k] + ΔU*dt + Uₙ = U.u[:, k] + ΔU * dt Vₙ = V.u[:, k] Kₙ = tₛ * f(Uₙ, Vₙ, p, τ + h * dt) # scale the time push!(K, Kₙ) end ΔU = dt * sum([α[i] * K[i] for i in 1:length(α)]) - subject_to!(solver_opti, U.u[:, k] + ΔU == U.u[:, k+1]) + subject_to!(solver_opti, U.u[:, k] + ΔU == U.u[:, k + 1]) empty!(K) end else - for k in 1:length(tsteps)-1 + for k in 1:(length(tsteps) - 1) τ = tsteps[k] Kᵢ = variable!(solver_opti, nᵤ, length(α)) ΔUs = A * Kᵢ' # the stepsize at each stage of the implicit method for (i, h) in enumerate(c) - ΔU = ΔUs[i,:]' - Uₙ = U.u[:,k] + ΔU*dt - Vₙ = V.u[:,k] - subject_to!(solver_opti, Kᵢ[:,i] == tₛ * f(Uₙ, Vₙ, p, τ + h*dt)) + ΔU = ΔUs[i, :]' + Uₙ = U.u[:, k] + ΔU * dt + Vₙ = V.u[:, k] + subject_to!(solver_opti, Kᵢ[:, i] == tₛ * f(Uₙ, Vₙ, p, τ + h * dt)) end - ΔU_tot = dt*(Kᵢ*α) - subject_to!(solver_opti, U.u[:, k] + ΔU_tot == U.u[:,k+1]) + ΔU_tot = dt * (Kᵢ * α) + subject_to!(solver_opti, U.u[:, k] + ΔU_tot == U.u[:, k + 1]) end end solver_opti @@ -331,7 +333,10 @@ end NOTE: the solver should be passed in as a string to CasADi. "ipopt" """ -function DiffEqBase.solve(prob::CasADiDynamicOptProblem, solver::Union{String, Symbol} = "ipopt", tableau_getter = MTK.constructDefault; plugin_options::Dict = Dict(), solver_options::Dict = Dict(), silent = false) +function DiffEqBase.solve( + prob::CasADiDynamicOptProblem, solver::Union{String, Symbol} = "ipopt", + tableau_getter = MTK.constructDefault; plugin_options::Dict = Dict(), + solver_options::Dict = Dict(), silent = false) @unpack model, u0, p, tspan, f = prob tableau = tableau_getter() @unpack opti, U, V, tₛ = model @@ -366,7 +371,8 @@ function DiffEqBase.solve(prob::CasADiDynamicOptProblem, solver::Union{String, S end if failed - ode_sol = SciMLBase.solution_new_retcode(ode_sol, SciMLBase.ReturnCode.ConvergenceFailure) + ode_sol = SciMLBase.solution_new_retcode( + ode_sol, SciMLBase.ReturnCode.ConvergenceFailure) !isnothing(input_sol) && (input_sol = SciMLBase.solution_new_retcode( input_sol, SciMLBase.ReturnCode.ConvergenceFailure)) end diff --git a/src/ModelingToolkit.jl b/src/ModelingToolkit.jl index 2f589c80e6..6d8d89a976 100644 --- a/src/ModelingToolkit.jl +++ b/src/ModelingToolkit.jl @@ -349,7 +349,8 @@ export AnalysisPoint, get_sensitivity_function, get_comp_sensitivity_function, function FMIComponent end include("systems/optimal_control_interface.jl") -export AbstractDynamicOptProblem, JuMPDynamicOptProblem, InfiniteOptDynamicOptProblem, CasADiDynamicOptProblem +export AbstractDynamicOptProblem, JuMPDynamicOptProblem, InfiniteOptDynamicOptProblem, + CasADiDynamicOptProblem export DynamicOptSolution end # module diff --git a/src/systems/optimal_control_interface.jl b/src/systems/optimal_control_interface.jl index d979e7b64e..fa1157dca8 100644 --- a/src/systems/optimal_control_interface.jl +++ b/src/systems/optimal_control_interface.jl @@ -41,7 +41,7 @@ function constructDefault(T::Type = Float64) A = map(T, A) α = map(T, α) c = map(T, c) - + DiffEqBase.ImplicitRKTableau(A, c, α, 5) end diff --git a/test/bvproblem.jl b/test/bvproblem.jl index a85f7ca522..6472608366 100644 --- a/test/bvproblem.jl +++ b/test/bvproblem.jl @@ -29,8 +29,8 @@ daesolvers = [Ascher2, Ascher4, Ascher6] for solver in solvers sol = solve(bvp, solver(), dt = 0.01) - @test_broken isapprox(sol.u[end], osol.u[end]; atol = 0.01) - @test_broken sol.u[1] == [1.0, 2.0] + @test isapprox(sol.u[end], osol.u[end]; atol = 0.01) + @test sol.u[1] == [1.0, 2.0] end # Test out of place @@ -39,8 +39,8 @@ daesolvers = [Ascher2, Ascher4, Ascher6] for solver in solvers sol = solve(bvp2, solver(), dt = 0.01) - @test_broken isapprox(sol.u[end], osol.u[end]; atol = 0.01) - @test_broken sol.u[1] == [1.0, 2.0] + @test isapprox(sol.u[end], osol.u[end]; atol = 0.01) + @test sol.u[1] == [1.0, 2.0] end end diff --git a/test/extensions/dynamic_optimization.jl b/test/extensions/dynamic_optimization.jl index 4b3b4edeb6..6e037746a3 100644 --- a/test/extensions/dynamic_optimization.jl +++ b/test/extensions/dynamic_optimization.jl @@ -61,7 +61,8 @@ const M = ModelingToolkit @test jsol.sol(0.6)[1] ≈ 3.5 @test jsol.sol(0.3)[1] ≈ 7.0 - cprob = CasADiDynamicOptProblem(lksys, u0map, tspan, parammap; guesses = guess, dt = 0.01) + cprob = CasADiDynamicOptProblem( + lksys, u0map, tspan, parammap; guesses = guess, dt = 0.01) csol = solve(cprob, "ipopt", constructTsitouras5, silent = true) @test csol.sol(0.6)[1] ≈ 3.5 @test csol.sol(0.3)[1] ≈ 7.0 @@ -87,7 +88,8 @@ const M = ModelingToolkit jsol = solve(jprob, Ipopt.Optimizer, constructRadauIA3, silent = true) # 12.190 s, 9.68 GiB @test all(u -> u > [1, 1], jsol.sol.u) - cprob = CasADiDynamicOptProblem(lksys, u0map, tspan, parammap; guesses = guess, dt = 0.01) + cprob = CasADiDynamicOptProblem( + lksys, u0map, tspan, parammap; guesses = guess, dt = 0.01) csol = solve(cprob, "ipopt", constructRadauIA3, silent = true) @test all(u -> u > [1, 1], csol.sol.u) end @@ -220,7 +222,7 @@ end jprob = JuMPDynamicOptProblem(rocket, u0map, (ts, te), pmap; dt = 0.001, cse = false) jsol = solve(jprob, Ipopt.Optimizer, constructRadauIIA5, silent = true) @test jsol.sol.u[end][1] > 1.012 - + cprob = CasADiDynamicOptProblem(rocket, u0map, (ts, te), pmap; dt = 0.001, cse = false) csol = solve(cprob, "ipopt"; silent = true) @test csol.sol.u[end][1] > 1.012 From be112aa188a5b7c35e3bc5b296bebcb7d362954a Mon Sep 17 00:00:00 2001 From: Herman Sletmoen Date: Mon, 12 May 2025 11:57:09 +0200 Subject: [PATCH 1524/2176] Change independent variable of ODEs with array variables --- src/systems/diffeqs/basic_transformations.jl | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/systems/diffeqs/basic_transformations.jl b/src/systems/diffeqs/basic_transformations.jl index 66571fb302..3f691477ff 100644 --- a/src/systems/diffeqs/basic_transformations.jl +++ b/src/systems/diffeqs/basic_transformations.jl @@ -153,13 +153,25 @@ function change_independent_variable( @set! sys.eqs = [get_eqs(sys); eqs] # add extra equations we derived @set! sys.unknowns = [get_unknowns(sys); [iv1, div2_of_iv1]] # add new variables, will be transformed to e.g. t(u) and uˍt(u) + # A utility function that returns whether var (e.g. f(t)) is a function of iv (e.g. t) + function is_function_of(var, iv) + # Peel off outer calls to find the argument of the function of + if iscall(var) && operation(var) === getindex # handle array variables + var = arguments(var)[1] # (f(t))[1] -> f(t) + end + if iscall(var) + var = only(arguments(var)) # e.g. f(t) -> t + return isequal(var, iv) + end + return false + end + # Create a utility that performs the chain rule on an expression, followed by insertion of the new independent variable: # e.g. (d/dt)(f(t)) -> (d/dt)(f(u(t))) -> df(u(t))/du(t) * du(t)/dt -> df(u)/du * uˍt(u) function transform(ex::T) where {T} # 1) Replace the argument of every function; e.g. f(t) -> f(u(t)) for var in vars(ex; op = Nothing) # loop over all variables in expression (op = Nothing prevents interpreting "D(f(t))" as one big variable) - is_function_of_iv1 = iscall(var) && isequal(only(arguments(var)), iv1) # of the form f(t)? - if is_function_of_iv1 && !isequal(var, iv2_of_iv1) # prevent e.g. u(t) -> u(u(t)) + if is_function_of(var, iv1) && !isequal(var, iv2_of_iv1) # of the form f(t)? but prevent e.g. u(t) -> u(u(t)) var_of_iv1 = var # e.g. f(t) var_of_iv2_of_iv1 = substitute(var_of_iv1, iv1 => iv2_of_iv1) # e.g. f(u(t)) ex = substitute(ex, var_of_iv1 => var_of_iv2_of_iv1; fold) From 38dddbeefe59fbe028e075e0d533afc40150c078 Mon Sep 17 00:00:00 2001 From: Herman Sletmoen Date: Mon, 12 May 2025 11:57:44 +0200 Subject: [PATCH 1525/2176] Test changing indpendent variable of ODEs with array variable --- test/basic_transformations.jl | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/test/basic_transformations.jl b/test/basic_transformations.jl index 41923f973c..bb57e27ea5 100644 --- a/test/basic_transformations.jl +++ b/test/basic_transformations.jl @@ -287,3 +287,20 @@ end @test all(isapprox.(sol[ss.t], sol[ss.y]; atol = 1e-10)) @test all(sol[ss.x][2:end] .< sol[ss.x][1]) end + +@testset "Change independent variable with array variables" begin + @variables x(t) y(t) z(t)[1:2] + eqs = [ + D(x) ~ 2, + z ~ ModelingToolkit.scalarize.([sin(y), cos(y)]), + D(y) ~ z[1]^2 + z[2]^2 + ] + @named sys = ODESystem(eqs, t) + sys = complete(sys) + new_sys = change_independent_variable(sys, sys.x; add_old_diff = true) + ss_new_sys = structural_simplify(new_sys; allow_symbolic = true) + u0 = [new_sys.y => 0.5, new_sys.t => 0.0] + prob = ODEProblem(ss_new_sys, u0, (0.0, 0.5), []) + sol = solve(prob, Tsit5(); reltol = 1e-5) + @test sol[new_sys.y][end] ≈ 0.75 +end From bd730a28e3a564d45c763d3cef7fc661097793a3 Mon Sep 17 00:00:00 2001 From: Christopher Rackauckas Date: Mon, 12 May 2025 18:34:20 +0000 Subject: [PATCH 1526/2176] Update Project.toml --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index c98e6eb2ac..f6925c4908 100644 --- a/Project.toml +++ b/Project.toml @@ -87,7 +87,7 @@ ArrayInterface = "6, 7" BifurcationKit = "0.4" BlockArrays = "1.1" BoundaryValueDiffEqAscher = "1.6.0" -BoundaryValueDiffEqMIRK = "1.8.0" +BoundaryValueDiffEqMIRK = "1.7.0" CasADi = "1.0.6" ChainRulesCore = "1" Combinatorics = "1" From 308c39ccadf826f1a880056f5ca23aab22086f32 Mon Sep 17 00:00:00 2001 From: Herman Sletmoen Date: Mon, 12 May 2025 23:53:28 +0200 Subject: [PATCH 1527/2176] Store if system was split/flat before rebuilding it --- src/systems/diffeqs/basic_transformations.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/systems/diffeqs/basic_transformations.jl b/src/systems/diffeqs/basic_transformations.jl index 3f691477ff..37c8d8d021 100644 --- a/src/systems/diffeqs/basic_transformations.jl +++ b/src/systems/diffeqs/basic_transformations.jl @@ -219,6 +219,8 @@ function change_independent_variable( connector_type = get_connector_type(sys) assertions = Dict(transform(ass) => msg for (ass, msg) in get_assertions(sys)) wascomplete = iscomplete(sys) # save before reconstructing system + wassplit = is_split(sys) + wasflat = isempty(systems) sys = typeof(sys)( # recreate system with transformed fields eqs, iv2, unknowns, ps; observed, initialization_eqs, parameter_dependencies, defaults, guesses, connector_type, @@ -226,8 +228,6 @@ function change_independent_variable( ) sys = compose(sys, systems) # rebuild hierarchical system if wascomplete - wasflat = isempty(systems) - wassplit = is_split(sys) sys = complete(sys; split = wassplit, flatten = wasflat) # complete output if input was complete end return sys From 60c95b9a71ec4350e4409b27b0eeb9d8c05fe1fe Mon Sep 17 00:00:00 2001 From: Christopher Rackauckas Date: Tue, 13 May 2025 11:50:02 +0000 Subject: [PATCH 1528/2176] Update Project.toml --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index f6925c4908..ace7bcd963 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "ModelingToolkit" uuid = "961ee093-0014-501f-94e3-6117800e7a78" authors = ["Yingbo Ma ", "Chris Rackauckas and contributors"] -version = "9.78.0" +version = "9.79.0" [deps] ADTypes = "47edcb42-4c32-4615-8424-f2b9edc5f35b" From 5f0758f5720d9d26c6eff88eb916128dc68fdd9b Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 19 May 2025 13:40:27 +0530 Subject: [PATCH 1529/2176] test: consider `StalledSuccess` a failure in initialization test --- test/initializationsystem.jl | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/initializationsystem.jl b/test/initializationsystem.jl index d2f35e57b7..fea989f0f3 100644 --- a/test/initializationsystem.jl +++ b/test/initializationsystem.jl @@ -39,7 +39,8 @@ initprob = ModelingToolkit.InitializationProblem( pend, 0.0, [], [g => 1]; guesses = ModelingToolkit.missing_variable_defaults(pend)) @test initprob isa NonlinearLeastSquaresProblem sol = solve(initprob) -@test !SciMLBase.successful_retcode(sol) +@test !SciMLBase.successful_retcode(sol) || + sol.retcode == SciMLBase.ReturnCode.StalledSuccess @test_throws ModelingToolkit.ExtraVariablesSystemException ModelingToolkit.InitializationProblem( pend, 0.0, [], [g => 1]; guesses = ModelingToolkit.missing_variable_defaults(pend), From 60da9d56486a378330bf2bde8943c1a60981c900 Mon Sep 17 00:00:00 2001 From: Christopher Rackauckas Date: Tue, 20 May 2025 10:55:20 +0000 Subject: [PATCH 1530/2176] Update Project.toml --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index ace7bcd963..6d637ef0a0 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "ModelingToolkit" uuid = "961ee093-0014-501f-94e3-6117800e7a78" authors = ["Yingbo Ma ", "Chris Rackauckas and contributors"] -version = "9.79.0" +version = "9.79.1" [deps] ADTypes = "47edcb42-4c32-4615-8424-f2b9edc5f35b" From c38d4733e3b1f419f6828b6b570049f422b8adb6 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 28 Apr 2025 11:51:35 +0530 Subject: [PATCH 1531/2176] fix: allow specifying type of buffers inside `MTKParameters` --- src/systems/parameter_buffer.jl | 56 ++++++++++++++++++++++++++------- src/systems/problem_utils.jl | 2 +- test/initial_values.jl | 26 +++++++++++++++ test/mtkparameters.jl | 13 ++++++-- 4 files changed, 82 insertions(+), 15 deletions(-) diff --git a/src/systems/parameter_buffer.jl b/src/systems/parameter_buffer.jl index 6142c95776..1e847256b3 100644 --- a/src/systems/parameter_buffer.jl +++ b/src/systems/parameter_buffer.jl @@ -28,7 +28,11 @@ the default behavior). """ function MTKParameters( sys::AbstractSystem, p, u0 = Dict(); tofloat = false, - t0 = nothing, substitution_limit = 1000, floatT = nothing) + t0 = nothing, substitution_limit = 1000, floatT = nothing, + container_type = Vector) + if !(container_type <: AbstractArray) + container_type = Array + end ic = if has_index_cache(sys) && get_index_cache(sys) !== nothing get_index_cache(sys) else @@ -133,18 +137,23 @@ function MTKParameters( end end end - tunable_buffer = narrow_buffer_type(tunable_buffer) + tunable_buffer = narrow_buffer_type(tunable_buffer; container_type) if isempty(tunable_buffer) tunable_buffer = SizedVector{0, Float64}() end - initials_buffer = narrow_buffer_type(initials_buffer) + initials_buffer = narrow_buffer_type(initials_buffer; container_type) if isempty(initials_buffer) initials_buffer = SizedVector{0, Float64}() end - disc_buffer = narrow_buffer_type.(disc_buffer) - const_buffer = narrow_buffer_type.(const_buffer) + disc_buffer = narrow_buffer_type.(disc_buffer; container_type) + const_buffer = narrow_buffer_type.(const_buffer; container_type) # Don't narrow nonnumeric types - nonnumeric_buffer = nonnumeric_buffer + if !isempty(nonnumeric_buffer) + nonnumeric_buffer = map(nonnumeric_buffer) do buf + SymbolicUtils.Code.create_array( + container_type, nothing, Val(1), Val(length(buf)), buf...) + end + end mtkps = MTKParameters{ typeof(tunable_buffer), typeof(initials_buffer), typeof(disc_buffer), @@ -160,21 +169,44 @@ function rebuild_with_caches(p::MTKParameters, cache_templates::BufferTemplate.. @set p.caches = buffers end -function narrow_buffer_type(buffer::AbstractArray) +function narrow_buffer_type(buffer::AbstractArray; container_type = typeof(buffer)) type = Union{} for x in buffer type = promote_type(type, typeof(x)) end - return convert.(type, buffer) + return SymbolicUtils.Code.create_array( + container_type, type, Val(ndims(buffer)), Val(length(buffer)), buffer...) end -function narrow_buffer_type(buffer::AbstractArray{<:AbstractArray}) - buffer = narrow_buffer_type.(buffer) +function narrow_buffer_type( + buffer::AbstractArray{<:AbstractArray}; container_type = typeof(buffer)) + type = Union{} + for arr in buffer + for x in arr + type = promote_type(type, typeof(x)) + end + end + buffer = map(buffer) do buf + SymbolicUtils.Code.create_array( + container_type, type, Val(ndims(buf)), Val(size(buf)), buf...) + end + return SymbolicUtils.Code.create_array( + container_type, nothing, Val(ndims(buffer)), Val(size(buffer)), buffer...) +end + +function narrow_buffer_type(buffer::BlockedArray; container_type = typeof(parent(buffer))) type = Union{} for x in buffer - type = promote_type(type, eltype(x)) + type = promote_type(type, typeof(x)) + end + tmp = SymbolicUtils.Code.create_array( + container_type, type, Val(ndims(buffer)), Val(size(buffer)), buffer...) + blocks = ntuple(Val(ndims(buffer))) do i + bsizes = blocksizes(buffer, i) + SymbolicUtils.Code.create_array( + container_type, Int, Val(1), Val(length(bsizes)), bsizes...) end - return broadcast.(convert, type, buffer) + return BlockedArray(tmp, blocks...) end function buffer_to_arraypartition(buf) diff --git a/src/systems/problem_utils.jl b/src/systems/problem_utils.jl index 4525b0e46b..5116453ef2 100644 --- a/src/systems/problem_utils.jl +++ b/src/systems/problem_utils.jl @@ -1155,7 +1155,7 @@ function process_SciMLProblem( end evaluate_varmap!(op, ps; limit = substitution_limit) if is_split(sys) - p = MTKParameters(sys, op; floatT = floatT) + p = MTKParameters(sys, op; floatT = floatT, container_type = pType) else p = better_varmap_to_vars(op, ps; tofloat, container_type = pType) end diff --git a/test/initial_values.jl b/test/initial_values.jl index b3614de0f4..ea5e8e01bb 100644 --- a/test/initial_values.jl +++ b/test/initial_values.jl @@ -2,6 +2,7 @@ using ModelingToolkit using ModelingToolkit: t_nounits as t, D_nounits as D, get_u0 using OrdinaryDiffEq using DataInterpolations +using StaticArrays using SymbolicIndexingInterface: getu @variables x(t)[1:3]=[1.0, 2.0, 3.0] y(t) z(t)[1:2] @@ -309,3 +310,28 @@ end @test prob[w2] ≈ -1.0 @test prob.ps[β] ≈ 8 / 3 end + +@testset "MTKParameters uses given `pType` for inner buffers" begin + @parameters σ ρ β + @variables x(t) y(t) z(t) + + eqs = [D(D(x)) ~ σ * (y - x), + D(y) ~ x * (ρ - z) - y, + D(z) ~ x * y - β * z] + + @mtkbuild sys = ODESystem(eqs, t) + + u0 = SA[D(x) => 2.0f0, + x => 1.0f0, + y => 0.0f0, + z => 0.0f0] + + p = SA[σ => 28.0f0, + ρ => 10.0f0, + β => 8.0f0 / 3] + + tspan = (0.0f0, 100.0f0) + prob = ODEProblem(sys, u0, tspan, p) + @test prob.p.tunable isa SVector + @test prob.p.initials isa SVector +end diff --git a/test/mtkparameters.jl b/test/mtkparameters.jl index 55de0768e0..9c857b6a55 100644 --- a/test/mtkparameters.jl +++ b/test/mtkparameters.jl @@ -1,8 +1,8 @@ using ModelingToolkit using ModelingToolkit: t_nounits as t, D_nounits as D, MTKParameters -using SymbolicIndexingInterface +using SymbolicIndexingInterface, StaticArrays using SciMLStructures: SciMLStructures, canonicalize, Tunable, Discrete, Constants -using BlockArrays: BlockedArray, Block +using BlockArrays: BlockedArray, BlockedVector, Block using OrdinaryDiffEq using ForwardDiff using JET @@ -27,6 +27,15 @@ end @test getp(sys, a)(ps) == getp(sys, b)(ps) == getp(sys, c)(ps) == 0.0 @test getp(sys, d)(ps) isa Int +@testset "`container_type`" begin + ps2 = MTKParameters(sys, ivs; container_type = SVector) + @test ps2.tunable isa SVector + @test ps2.initials isa SVector + @test ps2.discrete isa Tuple{<:BlockedVector{Float64, <:SVector}} + @test ps2.constant isa Tuple{<:SVector, <:SVector, <:SVector{1, <:SMatrix}} + @test ps2.nonnumeric isa Tuple{<:SVector} +end + ivs[a] = 1.0 ps = MTKParameters(sys, ivs) for (p, val) in ivs From 9c402402f5843fdf9884ed1028dda8d762b27e74 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 28 Apr 2025 15:46:34 +0530 Subject: [PATCH 1532/2176] fix: fix accidental narrowing of nonnumeric buffer --- src/systems/parameter_buffer.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/systems/parameter_buffer.jl b/src/systems/parameter_buffer.jl index 1e847256b3..419cb0cd77 100644 --- a/src/systems/parameter_buffer.jl +++ b/src/systems/parameter_buffer.jl @@ -151,7 +151,7 @@ function MTKParameters( if !isempty(nonnumeric_buffer) nonnumeric_buffer = map(nonnumeric_buffer) do buf SymbolicUtils.Code.create_array( - container_type, nothing, Val(1), Val(length(buf)), buf...) + container_type, eltype(buf), Val(1), Val(length(buf)), buf...) end end From 7b4b205b2fd9b55f2663bd6dfb646df07e18b80e Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 30 Apr 2025 12:24:20 +0530 Subject: [PATCH 1533/2176] fix: error if `container_type` passed to `MTKParameters` is not an `AbstractArray` subtype --- src/systems/parameter_buffer.jl | 5 ++++- src/systems/problem_utils.jl | 4 ++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/systems/parameter_buffer.jl b/src/systems/parameter_buffer.jl index 419cb0cd77..d899d7247d 100644 --- a/src/systems/parameter_buffer.jl +++ b/src/systems/parameter_buffer.jl @@ -31,7 +31,10 @@ function MTKParameters( t0 = nothing, substitution_limit = 1000, floatT = nothing, container_type = Vector) if !(container_type <: AbstractArray) - container_type = Array + throw(ArgumentError(""" + `container_type` for `MTKParameters` must be a subtype of `AbstractArray`. Found \ + $container_type. + """)) end 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 5116453ef2..67ecdd0f43 100644 --- a/src/systems/problem_utils.jl +++ b/src/systems/problem_utils.jl @@ -1155,6 +1155,10 @@ function process_SciMLProblem( end evaluate_varmap!(op, ps; limit = substitution_limit) if is_split(sys) + # `pType` is usually `Dict` when the user passes key-value pairs. + if !(pType <: AbstractArray) + pType = Array + end p = MTKParameters(sys, op; floatT = floatT, container_type = pType) else p = better_varmap_to_vars(op, ps; tofloat, container_type = pType) From 5f7b2fbf4409536a8791888120f8886dab8dca2c Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 5 May 2025 16:12:21 +0530 Subject: [PATCH 1534/2176] fix: retain `split` kwarg when simplifying initialization system --- src/systems/diffeqs/abstractodesystem.jl | 4 ++-- test/initializationsystem.jl | 12 ++++++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/systems/diffeqs/abstractodesystem.jl b/src/systems/diffeqs/abstractodesystem.jl index 942e508644..d802f49fee 100644 --- a/src/systems/diffeqs/abstractodesystem.jl +++ b/src/systems/diffeqs/abstractodesystem.jl @@ -1479,7 +1479,7 @@ function InitializationProblem{iip, specialize}(sys::AbstractSystem, end if simplify_system - isys = structural_simplify(isys; fully_determined) + isys = structural_simplify(isys; fully_determined, split = is_split(sys)) end ts = get_tearing_state(isys) @@ -1554,6 +1554,6 @@ function InitializationProblem{iip, specialize}(sys::AbstractSystem, else NonlinearLeastSquaresProblem end - TProb(isys, u0map, parammap; kwargs..., + TProb{iip}(isys, u0map, parammap; kwargs..., build_initializeprob = false, is_initializeprob = true) end diff --git a/test/initializationsystem.jl b/test/initializationsystem.jl index fea989f0f3..cadf1f0a01 100644 --- a/test/initializationsystem.jl +++ b/test/initializationsystem.jl @@ -1599,3 +1599,15 @@ end @test SciMLBase.successful_retcode(sol) @test sol.u[1] ≈ new_u0 end + +@testset "Initialization system retains `split` kwarg of parent" begin + @parameters g + @variables x(t) y(t) [state_priority = 10] λ(t) + eqs = [D(D(x)) ~ λ * x + D(D(y)) ~ λ * y - g + x^2 + y^2 ~ 1] + @mtkbuild pend=ODESystem(eqs, t) split=false + prob = ODEProblem(pend, [x => 1.0, D(x) => 0.0], (0.0, 1.0), + [g => 1.0]; guesses = [y => 1.0, λ => 1.0]) + @test !ModelingToolkit.is_split(prob.f.initialization_data.initializeprob.f.sys) +end From 7a5165233bd376b02124ac14081c1f0a3e05db0a Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 5 May 2025 16:13:13 +0530 Subject: [PATCH 1535/2176] refactor: make `EmptySciMLFunction` subtype `SciMLBase.AbstractSciMLFunction` --- src/systems/jumps/jumpsystem.jl | 6 +++--- src/systems/nonlinear/nonlinearsystem.jl | 2 +- src/systems/problem_utils.jl | 6 +++--- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/systems/jumps/jumpsystem.jl b/src/systems/jumps/jumpsystem.jl index 06f5e1b623..34b6df3bf5 100644 --- a/src/systems/jumps/jumpsystem.jl +++ b/src/systems/jumps/jumpsystem.jl @@ -408,7 +408,7 @@ function DiffEqBase.DiscreteProblem(sys::JumpSystem, u0map, tspan::Union{Tuple, error("The passed in JumpSystem contains `Equation`s or continuous events, please use a problem type that supports these features, such as ODEProblem.") end - _f, u0, p = process_SciMLProblem(EmptySciMLFunction, sys, u0map, parammap; + _f, u0, p = process_SciMLProblem(EmptySciMLFunction{true}, sys, u0map, parammap; t = tspan === nothing ? nothing : tspan[1], tofloat = false, check_length = false, build_initializeprob = false, cse) f = DiffEqBase.DISCRETE_INPLACE_DEFAULT @@ -449,7 +449,7 @@ function DiscreteProblemExpr{iip}(sys::JumpSystem, u0map, tspan::Union{Tuple, No 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; + _, u0, p = process_SciMLProblem(EmptySciMLFunction{iip}, sys, u0map, parammap; t = tspan === nothing ? nothing : tspan[1], tofloat = false, check_length = false) # identity function to make syms works quote @@ -506,7 +506,7 @@ function DiffEqBase.ODEProblem(sys::JumpSystem, u0map, tspan::Union{Tuple, Nothi return ODEProblem(osys, u0map, tspan, parammap; check_length = false, build_initializeprob = false, kwargs...) else - _, u0, p = process_SciMLProblem(EmptySciMLFunction, sys, u0map, parammap; + _, u0, p = process_SciMLProblem(EmptySciMLFunction{true}, sys, u0map, parammap; t = tspan === nothing ? nothing : tspan[1], tofloat = false, check_length = false, build_initializeprob = false, cse) f = (du, u, p, t) -> (du .= 0; nothing) diff --git a/src/systems/nonlinear/nonlinearsystem.jl b/src/systems/nonlinear/nonlinearsystem.jl index 7146fb6b5e..949184d8f7 100644 --- a/src/systems/nonlinear/nonlinearsystem.jl +++ b/src/systems/nonlinear/nonlinearsystem.jl @@ -705,7 +705,7 @@ function SciMLBase.SCCNonlinearProblem{iip}(sys::NonlinearSystem, u0map, obs = observed(sys) _, u0, p = process_SciMLProblem( - EmptySciMLFunction, sys, u0map, parammap; eval_expression, eval_module, kwargs...) + EmptySciMLFunction{iip}, sys, u0map, parammap; eval_expression, eval_module, kwargs...) explicitfuns = [] nlfuns = [] diff --git a/src/systems/problem_utils.jl b/src/systems/problem_utils.jl index 67ecdd0f43..8f9dd944cf 100644 --- a/src/systems/problem_utils.jl +++ b/src/systems/problem_utils.jl @@ -539,13 +539,13 @@ A simple utility meant to be used as the `constructor` passed to `process_SciMLP case constructing a SciMLFunction is not required. The arguments passed to it are available in the `args` field, and the keyword arguments in the `kwargs` field. """ -struct EmptySciMLFunction{A, K} +struct EmptySciMLFunction{iip, A, K} <: SciMLBase.AbstractSciMLFunction{iip} args::A kwargs::K end -function EmptySciMLFunction(args...; kwargs...) - return EmptySciMLFunction{typeof(args), typeof(kwargs)}(args, kwargs) +function EmptySciMLFunction{iip}(args...; kwargs...) where {iip} + return EmptySciMLFunction{iip, typeof(args), typeof(kwargs)}(args, kwargs) end """ From e56ea2a4606c2b11e1911839381a494dae3e167e Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 5 May 2025 16:16:18 +0530 Subject: [PATCH 1536/2176] fix: retain `iip` and `u0Type` in `SCCNonlinearProblem` constructor --- src/systems/nonlinear/nonlinearsystem.jl | 4 +++- test/scc_nonlinear_problem.jl | 7 +++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/systems/nonlinear/nonlinearsystem.jl b/src/systems/nonlinear/nonlinearsystem.jl index 949184d8f7..f8182fa28a 100644 --- a/src/systems/nonlinear/nonlinearsystem.jl +++ b/src/systems/nonlinear/nonlinearsystem.jl @@ -835,7 +835,9 @@ function SciMLBase.SCCNonlinearProblem{iip}(sys::NonlinearSystem, u0map, subprobs = [] for (f, vscc) in zip(nlfuns, var_sccs) - prob = NonlinearProblem(f, u0[vscc], p) + _u0 = SymbolicUtils.Code.create_array( + typeof(u0), eltype(u0), Val(1), Val(length(vscc)), u0[vscc]...) + prob = NonlinearProblem{iip}(f, _u0, p) push!(subprobs, prob) end diff --git a/test/scc_nonlinear_problem.jl b/test/scc_nonlinear_problem.jl index b2b326d090..ac7978d269 100644 --- a/test/scc_nonlinear_problem.jl +++ b/test/scc_nonlinear_problem.jl @@ -2,6 +2,7 @@ using ModelingToolkit using NonlinearSolve, SCCNonlinearSolve using OrdinaryDiffEq using SciMLBase, Symbolics +using StaticArrays using LinearAlgebra, Test using ModelingToolkit: t_nounits as t, D_nounits as D @@ -32,6 +33,12 @@ using ModelingToolkit: t_nounits as t, D_nounits as D @test SciMLBase.successful_retcode(sol1) @test SciMLBase.successful_retcode(sol2) @test sol1[u] ≈ sol2[u] + + sccprob = SCCNonlinearProblem{false}(model, SA[u => zeros(8)]) + for prob in sccprob.probs + @test prob.u0 isa SVector + @test !SciMLBase.isinplace(prob) + end end @testset "With parameters" begin From d1642f07d7bd9433d29a9bfd138e8b086a85bd8b Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 5 May 2025 16:16:52 +0530 Subject: [PATCH 1537/2176] feat: add `p_constructor` kwarg to `MTKParameters` constructor --- src/systems/parameter_buffer.jl | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/systems/parameter_buffer.jl b/src/systems/parameter_buffer.jl index d899d7247d..5c777dbfe4 100644 --- a/src/systems/parameter_buffer.jl +++ b/src/systems/parameter_buffer.jl @@ -29,7 +29,7 @@ the default behavior). function MTKParameters( sys::AbstractSystem, p, u0 = Dict(); tofloat = false, t0 = nothing, substitution_limit = 1000, floatT = nothing, - container_type = Vector) + container_type = Vector, p_constructor = identity) if !(container_type <: AbstractArray) throw(ArgumentError(""" `container_type` for `MTKParameters` must be a subtype of `AbstractArray`. Found \ @@ -140,21 +140,21 @@ function MTKParameters( end end end - tunable_buffer = narrow_buffer_type(tunable_buffer; container_type) + tunable_buffer = p_constructor(narrow_buffer_type(tunable_buffer; container_type)) if isempty(tunable_buffer) tunable_buffer = SizedVector{0, Float64}() end - initials_buffer = narrow_buffer_type(initials_buffer; container_type) + initials_buffer = p_constructor(narrow_buffer_type(initials_buffer; container_type)) if isempty(initials_buffer) initials_buffer = SizedVector{0, Float64}() end - disc_buffer = narrow_buffer_type.(disc_buffer; container_type) - const_buffer = narrow_buffer_type.(const_buffer; container_type) + disc_buffer = p_constructor.(narrow_buffer_type.(disc_buffer; container_type)) + const_buffer = p_constructor.(narrow_buffer_type.(const_buffer; container_type)) # Don't narrow nonnumeric types if !isempty(nonnumeric_buffer) nonnumeric_buffer = map(nonnumeric_buffer) do buf - SymbolicUtils.Code.create_array( - container_type, eltype(buf), Val(1), Val(length(buf)), buf...) + p_constructor(SymbolicUtils.Code.create_array( + container_type, eltype(buf), Val(1), Val(length(buf)), buf...)) end end From 91b93882d5f73345cadd0072cc77c2cd15f2aa24 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 5 May 2025 16:17:07 +0530 Subject: [PATCH 1538/2176] feat: implement `ArrayInterface.ismutable` for `MTKParameters` --- src/systems/parameter_buffer.jl | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/systems/parameter_buffer.jl b/src/systems/parameter_buffer.jl index 5c777dbfe4..82a44a94e8 100644 --- a/src/systems/parameter_buffer.jl +++ b/src/systems/parameter_buffer.jl @@ -366,6 +366,14 @@ function Base.copy(p::MTKParameters) ) end +function ArrayInterface.ismutable(::Type{MTKParameters{ + T, I, D, C, N, H}}) where {T, I, D, C, N, H} + ArrayInterface.ismutable(T) || ArrayInterface.ismutable(I) || + any(ArrayInterface.ismutable, fieldtypes(D)) || + any(ArrayInterface.ismutable, fieldtypes(C)) || + any(ArrayInterface.ismutable, fieldtypes(N)) +end + function SymbolicIndexingInterface.parameter_values(p::MTKParameters, pind::ParameterIndex) _ducktyped_parameter_values(p, pind) end From 762954616c237810197ffd716e93efed5f438259 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 5 May 2025 16:17:45 +0530 Subject: [PATCH 1539/2176] fix: handle immutable MTKParameters in `remake_buffer` --- src/systems/parameter_buffer.jl | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/src/systems/parameter_buffer.jl b/src/systems/parameter_buffer.jl index 82a44a94e8..7c34f67deb 100644 --- a/src/systems/parameter_buffer.jl +++ b/src/systems/parameter_buffer.jl @@ -637,8 +637,9 @@ end nonnumerics = $(Expr(:tuple, (:($similar(oldbuf.nonnumeric[$i], $(nonnumericT[i]))) for i in 1:length(nonnumericT))...)) $((:($copyto!(nonnumerics[$i], oldbuf.nonnumeric[$i])) for i in 1:length(nonnumericT))...) + caches = copy.(oldbuf.caches) newbuf = MTKParameters( - tunables, initials, discretes, constants, nonnumerics, copy.(oldbuf.caches)) + tunables, initials, discretes, constants, nonnumerics, caches) end if idxs <: AbstractArray push!(expr.args, :(for (idx, val) in zip(idxs, vals) @@ -649,6 +650,22 @@ end push!(expr.args, :($setindex!(newbuf, vals[$i], idxs[$i]))) end end + if !ArrayInterface.ismutable(oldbuf) + push!(expr.args, :(tunables = $similar_type($T, $tunablesT)(tunables))) + push!(expr.args, :(initials = $similar_type($I, $initialsT)(initials))) + push!(expr.args, + :(discretes = $(Expr(:tuple, + (:($similar_type($(fieldtype(D, i)), $(discretesT[i]))(discretes[$i])) for i in 1:length(discretesT))...)))) + push!(expr.args, + :(constants = $(Expr(:tuple, + (:($similar_type($(fieldtype(C, i)), $(constantsT[i]))(constants[$i])) for i in 1:length(constantsT))...)))) + push!(expr.args, + :(nonnumerics = $(Expr(:tuple, + (:($similar_type($(fieldtype(C, i)), $(nonnumericT[i]))(nonnumerics[$i])) for i in 1:length(nonnumericT))...)))) + push!(expr.args, + :(newbuf = MTKParameters( + tunables, initials, discretes, constants, nonnumerics, caches))) + end push!(expr.args, :(return newbuf)) return expr @@ -748,6 +765,19 @@ function __remake_buffer(indp, oldbuf::MTKParameters, idxs, vals; validate = tru oldbuf.constant, newbuf.constant) @set! newbuf.nonnumeric = narrow_buffer_type_and_fallback_undefs.( oldbuf.nonnumeric, newbuf.nonnumeric) + if !ArrayInterface.ismutable(oldbuf) + @set! newbuf.tunable = similar_type(oldbuf.tunable, eltype(newbuf.tunable))(newbuf.tunable) + @set! newbuf.initials = similar_type(oldbuf.initials, eltype(newbuf.initials))(newbuf.initials) + @set! newbuf.discrete = ntuple(Val(length(newbuf.discrete))) do i + similar_type.(oldbuf.discrete[i], eltype(newbuf.discrete[i]))(newbuf.discrete[i]) + end + @set! newbuf.constant = ntuple(Val(length(newbuf.constant))) do i + similar_type.(oldbuf.constant[i], eltype(newbuf.constant[i]))(newbuf.constant[i]) + end + @set! newbuf.nonnumeric = ntuple(Val(length(newbuf.nonnumeric))) do i + similar_type.(oldbuf.nonnumeric[i], eltype(newbuf.nonnumeric[i]))(newbuf.nonnumeric[i]) + end + end return newbuf end From 354706282a7b377ae4fbc7efce154b58c33ed4c2 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 5 May 2025 16:21:23 +0530 Subject: [PATCH 1540/2176] feat: add `p_constructor` kwarg to problem constructors --- src/systems/problem_utils.jl | 14 ++++++++++---- test/initial_values.jl | 28 ++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 4 deletions(-) diff --git a/src/systems/problem_utils.jl b/src/systems/problem_utils.jl index 8f9dd944cf..071a47a355 100644 --- a/src/systems/problem_utils.jl +++ b/src/systems/problem_utils.jl @@ -1016,6 +1016,7 @@ Keyword arguments: - `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. +- `p_constructor`: A function to apply to each array buffer created when constructing the parameter object. - `du0map`: A map of derivatives to values. See `implicit_dae`. - `check_length`: Whether to check the number of equations along with number of unknowns and length of `u0` vector for consistency. If `false`, do not check with equations. This is @@ -1044,8 +1045,8 @@ function process_SciMLProblem( warn_initialize_determined = true, initialization_eqs = [], eval_expression = false, eval_module = @__MODULE__, fully_determined = nothing, check_initialization_units = false, tofloat = true, - u0_constructor = identity, du0map = nothing, check_length = true, - symbolic_u0 = false, warn_cyclic_dependency = false, + u0_constructor = identity, p_constructor = identity, du0map = nothing, + check_length = true, symbolic_u0 = false, warn_cyclic_dependency = false, circular_dependency_max_cycle_length = length(all_symbols(sys)), circular_dependency_max_cycles = 10, substitution_limit = 100, use_scc = true, @@ -1095,6 +1096,11 @@ function process_SciMLProblem( u0_constructor = vals -> SymbolicUtils.Code.create_array( u0Type, floatT, Val(1), Val(length(vals)), vals...) end + if p_constructor === identity && pType <: StaticArray + p_constructor = vals -> SymbolicUtils.Code.create_array( + pType, floatT, Val(1), Val(length(vals)), vals...) + end + if build_initializeprob kws = maybe_build_initialization_problem( sys, op, u0map, pmap, t, defs, guesses, missing_unknowns; @@ -1159,9 +1165,9 @@ function process_SciMLProblem( if !(pType <: AbstractArray) pType = Array end - p = MTKParameters(sys, op; floatT = floatT, container_type = pType) + p = MTKParameters(sys, op; floatT = floatT, container_type = pType, p_constructor) else - p = better_varmap_to_vars(op, ps; tofloat, container_type = pType) + p = p_constructor(better_varmap_to_vars(op, ps; tofloat, container_type = pType)) end if implicit_dae && du0map !== nothing diff --git a/test/initial_values.jl b/test/initial_values.jl index ea5e8e01bb..8cff39f784 100644 --- a/test/initial_values.jl +++ b/test/initial_values.jl @@ -335,3 +335,31 @@ end @test prob.p.tunable isa SVector @test prob.p.initials isa SVector end + +@testset "`p_constructor` keyword argument" begin + @parameters σ ρ β + @variables x(t) y(t) z(t) + + eqs = [D(D(x)) ~ σ * (y - x), + D(y) ~ x * (ρ - z) - y, + D(z) ~ x * y - β * z] + + @mtkbuild sys = ODESystem(eqs, t) + + u0 = [D(x) => 2.0f0, + x => 1.0f0, + y => 0.0f0, + z => 0.0f0] + + p = [σ => 28.0f0, + ρ => 10.0f0, + β => 8.0f0 / 3] + u0_constructor = p_constructor = vals -> SVector{length(vals)}(vals...) + prob = ODEProblem(sys, u0, tspan, p; u0_constructor, p_constructor) + @test prob.p.tunable isa SVector + @test prob.p.initials isa SVector + + @mtkbuild sys=ODESystem(eqs, t) split=false + prob = ODEProblem(sys, u0, tspan, p; u0_constructor, p_constructor) + @test prob.p isa SVector +end From 35a7659cef421bc4a615ef1030f28a14ed2de83e Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 5 May 2025 16:27:37 +0530 Subject: [PATCH 1541/2176] feat: propagate `p_constructor` to `InitializationProblem` --- src/systems/problem_utils.jl | 8 +++++--- test/initial_values.jl | 39 ++++++++++++++++++------------------ 2 files changed, 24 insertions(+), 23 deletions(-) diff --git a/src/systems/problem_utils.jl b/src/systems/problem_utils.jl index 071a47a355..501ca14cd6 100644 --- a/src/systems/problem_utils.jl +++ b/src/systems/problem_utils.jl @@ -869,7 +869,8 @@ All other keyword arguments are forwarded to `InitializationProblem`. function maybe_build_initialization_problem( sys::AbstractSystem, op::AbstractDict, u0map, pmap, t, defs, guesses, missing_unknowns; implicit_dae = false, u0_constructor = identity, - floatT = Float64, initialization_eqs = [], use_scc = true, kwargs...) + p_constructor = identity, floatT = Float64, initialization_eqs = [], + use_scc = true, kwargs...) guesses = merge(ModelingToolkit.guesses(sys), todict(guesses)) if t === nothing && is_time_dependent(sys) @@ -877,7 +878,8 @@ function maybe_build_initialization_problem( end initializeprob = ModelingToolkit.InitializationProblem{true, SciMLBase.FullSpecialize}( - sys, t, u0map, pmap; guesses, initialization_eqs, use_scc, kwargs...) + sys, t, u0map, pmap; guesses, initialization_eqs, + use_scc, u0_constructor, p_constructor, kwargs...) if state_values(initializeprob) !== nothing initializeprob = remake(initializeprob; u0 = floatT.(state_values(initializeprob))) end @@ -1109,7 +1111,7 @@ function process_SciMLProblem( warn_cyclic_dependency, check_units = check_initialization_units, circular_dependency_max_cycle_length, circular_dependency_max_cycles, use_scc, force_time_independent = force_initialization_time_independent, algebraic_only, allow_incomplete, - u0_constructor, floatT) + u0_constructor, p_constructor, floatT) kwargs = merge(kwargs, kws) end diff --git a/test/initial_values.jl b/test/initial_values.jl index 8cff39f784..0ed8f7bffe 100644 --- a/test/initial_values.jl +++ b/test/initial_values.jl @@ -3,7 +3,7 @@ using ModelingToolkit: t_nounits as t, D_nounits as D, get_u0 using OrdinaryDiffEq using DataInterpolations using StaticArrays -using SymbolicIndexingInterface: getu +using SymbolicIndexingInterface @variables x(t)[1:3]=[1.0, 2.0, 3.0] y(t) z(t)[1:2] @@ -337,29 +337,28 @@ end end @testset "`p_constructor` keyword argument" begin - @parameters σ ρ β - @variables x(t) y(t) z(t) - - eqs = [D(D(x)) ~ σ * (y - x), - D(y) ~ x * (ρ - z) - y, - D(z) ~ x * y - β * z] - - @mtkbuild sys = ODESystem(eqs, t) - - u0 = [D(x) => 2.0f0, - x => 1.0f0, - y => 0.0f0, - z => 0.0f0] + @parameters g = 1.0 + @variables x(t) y(t) [state_priority = 10, guess = 1.0] λ(t) [guess = 1.0] + eqs = [D(D(x)) ~ λ * x + D(D(y)) ~ λ * y - g + x^2 + y^2 ~ 1] + @mtkbuild pend = ODESystem(eqs, t) - p = [σ => 28.0f0, - ρ => 10.0f0, - β => 8.0f0 / 3] + u0 = [x => 1.0, D(x) => 0.0] u0_constructor = p_constructor = vals -> SVector{length(vals)}(vals...) - prob = ODEProblem(sys, u0, tspan, p; u0_constructor, p_constructor) + tspan = (0.0, 5.0) + prob = ODEProblem(pend, u0, tspan; u0_constructor, p_constructor) + @test prob.u0 isa SVector @test prob.p.tunable isa SVector @test prob.p.initials isa SVector + initdata = prob.f.initialization_data + @test state_values(initdata.initializeprob) isa SVector + @test parameter_values(initdata.initializeprob).tunable isa SVector - @mtkbuild sys=ODESystem(eqs, t) split=false - prob = ODEProblem(sys, u0, tspan, p; u0_constructor, p_constructor) + @mtkbuild pend=ODESystem(eqs, t) split=false + prob = ODEProblem(pend, u0, tspan; u0_constructor, p_constructor) @test prob.p isa SVector + initdata = prob.f.initialization_data + @test state_values(initdata.initializeprob) isa SVector + @test parameter_values(initdata.initializeprob) isa SVector end From fd869e1d93c4a9d6b8192ab5c3aec28cd707716f Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 5 May 2025 16:30:10 +0530 Subject: [PATCH 1542/2176] fix: propagate `iip` of parent problem to `InitializationProblem` --- src/systems/problem_utils.jl | 7 ++++--- test/initializationsystem.jl | 13 +++++++++++++ 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/src/systems/problem_utils.jl b/src/systems/problem_utils.jl index 501ca14cd6..116adf9deb 100644 --- a/src/systems/problem_utils.jl +++ b/src/systems/problem_utils.jl @@ -867,7 +867,7 @@ denotes whether the `SciMLProblem` being constructed is in implicit DAE form (`D All other keyword arguments are forwarded to `InitializationProblem`. """ function maybe_build_initialization_problem( - sys::AbstractSystem, op::AbstractDict, u0map, pmap, t, defs, + sys::AbstractSystem, iip, op::AbstractDict, u0map, pmap, t, defs, guesses, missing_unknowns; implicit_dae = false, u0_constructor = identity, p_constructor = identity, floatT = Float64, initialization_eqs = [], use_scc = true, kwargs...) @@ -877,7 +877,7 @@ function maybe_build_initialization_problem( t = zero(floatT) end - initializeprob = ModelingToolkit.InitializationProblem{true, SciMLBase.FullSpecialize}( + initializeprob = ModelingToolkit.InitializationProblem{iip}( sys, t, u0map, pmap; guesses, initialization_eqs, use_scc, u0_constructor, p_constructor, kwargs...) if state_values(initializeprob) !== nothing @@ -1105,7 +1105,8 @@ function process_SciMLProblem( if build_initializeprob kws = maybe_build_initialization_problem( - sys, op, u0map, pmap, t, defs, guesses, missing_unknowns; + sys, constructor <: SciMLBase.AbstractSciMLFunction{true}, + op, u0map, pmap, t, defs, guesses, missing_unknowns; implicit_dae, warn_initialize_determined, initialization_eqs, eval_expression, eval_module, fully_determined, warn_cyclic_dependency, check_units = check_initialization_units, diff --git a/test/initializationsystem.jl b/test/initializationsystem.jl index cadf1f0a01..87ba755259 100644 --- a/test/initializationsystem.jl +++ b/test/initializationsystem.jl @@ -1611,3 +1611,16 @@ end [g => 1.0]; guesses = [y => 1.0, λ => 1.0]) @test !ModelingToolkit.is_split(prob.f.initialization_data.initializeprob.f.sys) end + +@testset "`InitializationProblem` retains `iip` of parent" begin + @parameters g + @variables x(t) y(t) [state_priority = 10] λ(t) + eqs = [D(D(x)) ~ λ * x + D(D(y)) ~ λ * y - g + x^2 + y^2 ~ 1] + @mtkbuild pend = ODESystem(eqs, t) + prob = ODEProblem(pend, SA[x => 1.0, D(x) => 0.0], (0.0, 1.0), + SA[g => 1.0]; guesses = [y => 1.0, λ => 1.0]) + @test !SciMLBase.isinplace(prob) + @test !SciMLBase.isinplace(prob.f.initialization_data.initializeprob) +end From b69d79e591217668f5491e5ed8da521e1fd92007 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 5 May 2025 16:33:06 +0530 Subject: [PATCH 1543/2176] fix: retain type of buffers when promoting `u0`/`p` of initialization problem --- src/systems/problem_utils.jl | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/src/systems/problem_utils.jl b/src/systems/problem_utils.jl index 116adf9deb..5e7edf4e55 100644 --- a/src/systems/problem_utils.jl +++ b/src/systems/problem_utils.jl @@ -881,7 +881,13 @@ function maybe_build_initialization_problem( sys, t, u0map, pmap; guesses, initialization_eqs, use_scc, u0_constructor, p_constructor, kwargs...) if state_values(initializeprob) !== nothing - initializeprob = remake(initializeprob; u0 = floatT.(state_values(initializeprob))) + _u0 = state_values(initializeprob) + if ArrayInterface.ismutable(_u0) + _u0 = floatT.(_u0) + else + _u0 = similar_type(_u0, floatT)(_u0) + end + initializeprob = remake(initializeprob; u0 = _u0) end initp = parameter_values(initializeprob) if is_split(sys) @@ -890,9 +896,13 @@ function maybe_build_initialization_problem( buffer, repack, _ = SciMLStructures.canonicalize(SciMLStructures.Initials(), initp) initp = repack(floatT.(buffer)) elseif initp isa AbstractArray - initp′ = similar(initp, floatT) - copyto!(initp′, initp) - initp = initp′ + if ArrayInterface.ismutable(initp) + initp′ = similar(initp, floatT) + copyto!(initp′, initp) + initp = initp′ + else + initp = similar_type(initp, floatT)(initp) + end end initializeprob = remake(initializeprob; p = initp) From aff87fa0bf62f45a5d732cffb2dec682f50939b4 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 5 May 2025 16:34:36 +0530 Subject: [PATCH 1544/2176] fix: handle immutable buffers in initialization --- src/systems/nonlinear/initializesystem.jl | 5 +- src/systems/problem_utils.jl | 228 +++++++++++++++------- 2 files changed, 162 insertions(+), 71 deletions(-) diff --git a/src/systems/nonlinear/initializesystem.jl b/src/systems/nonlinear/initializesystem.jl index c9d0c8f3a5..ce29db68b4 100644 --- a/src/systems/nonlinear/initializesystem.jl +++ b/src/systems/nonlinear/initializesystem.jl @@ -513,8 +513,7 @@ function SciMLBase.remake_initialization_data( length(oldinitprob.f.resid_prototype), new_initu0, new_initp)) end initprob = remake(oldinitprob; f = newf, u0 = new_initu0, p = new_initp) - return SciMLBase.OverrideInitData(initprob, oldinitdata.update_initializeprob!, - oldinitdata.initializeprobmap, oldinitdata.initializeprobpmap; metadata = oldinitdata.metadata) + return @set oldinitdata.initializeprob = initprob end dvs = unknowns(sys) @@ -627,7 +626,7 @@ function SciMLBase.late_binding_update_u0_p( if length(newu0) != length(prob.u0) throw(ArgumentError("Expected `newu0` to be of same length as unknowns ($(length(prob.u0))). Got $(typeof(newu0)) of length $(length(newu0))")) end - meta.set_initial_unknowns!(newp, newu0) + newp = meta.set_initial_unknowns!(newp, newu0) return newu0, newp end diff --git a/src/systems/problem_utils.jl b/src/systems/problem_utils.jl index 5e7edf4e55..897b2fcc44 100644 --- a/src/systems/problem_utils.jl +++ b/src/systems/problem_utils.jl @@ -491,32 +491,6 @@ function scalarize_varmap!(varmap::AbstractDict) return varmap end -struct GetUpdatedMTKParameters{G, S} - # `getu` functor which gets parameters that are unknowns during initialization - getpunknowns::G - # `setu` functor which returns a modified MTKParameters using those parameters - setpunknowns::S -end - -function (f::GetUpdatedMTKParameters)(prob, initializesol) - p = parameter_values(prob) - p === nothing && return nothing - mtkp = copy(p) - f.setpunknowns(mtkp, f.getpunknowns(initializesol)) - mtkp -end - -struct UpdateInitializeprob{G, S} - # `getu` functor which gets all values from prob - getvals::G - # `setu` functor which updates initializeprob with values - setvals::S -end - -function (f::UpdateInitializeprob)(initializeprob, prob) - f.setvals(initializeprob, f.getvals(prob)) -end - function get_temporary_value(p, floatT = Float64) stype = symtype(unwrap(p)) return if stype == Real @@ -669,6 +643,72 @@ function concrete_getu(indp, syms::AbstractVector) return Base.Fix1(reduce, vcat) ∘ getu(indp, split_syms) end +""" + $(TYPEDSIGNATURES) + +Given a source system `srcsys` and destination system `dstsys`, return a function that +takes a value provider of `srcsys` and a value provider of `dstsys` and returns the +`MTKParameters` object of the latter with values from the former. + +# Keyword Arguments +- `initials`: Whether to include the `Initial` parameters of `dstsys` among the values + to be transferred. +- `p_constructor`: The `p_constructor` argument to `process_SciMLProblem`. +""" +function get_mtkparameters_reconstructor(srcsys::AbstractSystem, dstsys::AbstractSystem; + initials = false, unwrap_initials = false, p_constructor = identity) + # if we call `getu` on this (and it were able to handle empty tuples) we get the + # fields of `MTKParameters` except caches. + syms = reorder_parameters( + dstsys, parameters(dstsys; initial_parameters = initials); flatten = false) + # `dstsys` is an initialization system, do basically everything is a tunable + # and tunables are a mix of different types in `srcsys`. No initials. Constants + # are going to be constants in `srcsys`, as are `nonnumeric`. + + # `syms[1]` is always the tunables because `srcsys` will have initials. + tunable_syms = syms[1] + tunable_getter = p_constructor ∘ concrete_getu(srcsys, tunable_syms) + rest_getters = map(Base.tail(Base.tail(syms))) do buf + if buf == () + return Returns(()) + else + return Base.Fix1(broadcast, p_constructor) ∘ getu(srcsys, buf) + end + end + initials_getter = if initials + initsyms = Vector{Any}(syms[2]) + allsyms = Set(all_symbols(srcsys)) + if unwrap_initials + for i in eachindex(initsyms) + sym = initsyms[i] + innersym = if operation(sym) === getindex + sym, idxs... = arguments(sym) + only(arguments(sym))[idxs...] + else + only(arguments(sym)) + end + if innersym in allsyms + initsyms[i] = innersym + end + end + end + p_constructor ∘ concrete_getu(srcsys, initsyms) + else + Returns(SizedVector{0, Float64}()) + end + getters = (tunable_getter, initials_getter, rest_getters...) + getter = let getters = getters + function _getter(valp, initprob) + oldcache = parameter_values(initprob).caches + MTKParameters(getters[1](valp), getters[2](valp), getters[3](valp), + getters[4](valp), getters[5](valp), oldcache isa Tuple{} ? () : + copy.(oldcache)) + end + end + + return getter +end + """ $(TYPEDSIGNATURES) @@ -676,41 +716,16 @@ Construct a `ReconstructInitializeprob` which reconstructs the `u0` and `p` of ` with values from `srcsys`. """ function ReconstructInitializeprob( - srcsys::AbstractSystem, dstsys::AbstractSystem) + srcsys::AbstractSystem, dstsys::AbstractSystem; u0_constructor = identity, p_constructor = identity) @assert is_initializesystem(dstsys) - ugetter = getu(srcsys, unknowns(dstsys)) + ugetter = u0_constructor ∘ getu(srcsys, unknowns(dstsys)) if is_split(dstsys) - # if we call `getu` on this (and it were able to handle empty tuples) we get the - # fields of `MTKParameters` except caches. - syms = reorder_parameters(dstsys, parameters(dstsys); flatten = false) - # `dstsys` is an initialization system, do basically everything is a tunable - # and tunables are a mix of different types in `srcsys`. No initials. Constants - # are going to be constants in `srcsys`, as are `nonnumeric`. - - # `syms[1]` is always the tunables because `srcsys` will have initials. - tunable_syms = syms[1] - tunable_getter = concrete_getu(srcsys, tunable_syms) - rest_getters = map(Base.tail(Base.tail(syms))) do buf - if buf == () - return Returns(()) - else - return getu(srcsys, buf) - end - end - getters = (tunable_getter, Returns(SizedVector{0, Float64}()), rest_getters...) - pgetter = let getters = getters - function _getter(valp, initprob) - oldcache = parameter_values(initprob).caches - MTKParameters(getters[1](valp), getters[2](valp), getters[3](valp), - getters[4](valp), getters[5](valp), oldcache isa Tuple{} ? () : - copy.(oldcache)) - end - end + pgetter = get_mtkparameters_reconstructor(srcsys, dstsys; p_constructor) else syms = parameters(dstsys) - pgetter = let inner = concrete_getu(srcsys, syms) + pgetter = let inner = concrete_getu(srcsys, syms), p_constructor = p_constructor function _getter2(valp, initprob) - inner(valp) + p_constructor(inner(valp)) end end end @@ -763,6 +778,54 @@ function (rip::ReconstructInitializeprob)(srcvalp, dstvalp) return u0, newp end +""" + $(TYPEDSIGNATURES) + +Given `sys` and its corresponding initialization system `initsys`, return the +`initializeprobpmap` function in `OverrideInitData` for the systems. +""" +function construct_initializeprobpmap( + sys::AbstractSystem, initsys::AbstractSystem; p_constructor = identity) + @assert is_initializesystem(initsys) + if is_split(sys) + return let getter = get_mtkparameters_reconstructor( + initsys, sys; initials = true, p_constructor) + function initprobpmap_split(prob, initsol) + getter(initsol, prob) + end + end + else + return let getter = getu(initsys, parameters(sys; initial_parameters = true)), + p_constructor = p_constructor + + function initprobpmap_nosplit(prob, initsol) + return p_constructor(getter(initsol)) + end + end + end +end + +function get_scimlfn(valp) + valp isa SciMLBase.AbstractSciMLFunction && return valp + if hasmethod(symbolic_container, Tuple{typeof(valp)}) && + (sc = symbolic_container(valp)) !== valp + return get_scimlfn(sc) + end + throw(ArgumentError("SciMLFunction not found. This should never happen.")) +end + +""" + $(TYPEDSIGNATURES) + +A function to be used as `update_initializeprob!` in `OverrideInitData`. Requires +`is_update_oop = Val{true}` to be passed to `update_initializeprob!`. +""" +function update_initializeprob!(initprob, prob) + p = get_scimlfn(prob).initialization_data.metadata.oop_reconstruct_u0_p.getter( + prob, initprob) + return remake(initprob; p) +end + """ $(TYPEDEF) @@ -804,8 +867,8 @@ struct InitializationMetadata{R <: ReconstructInitializeprob, GUU, SIU} """ get_updated_u0::GUU """ - A function which takes the `u0` of the problem and sets - `Initial.(unknowns(sys))`. + A function which takes parameter object and `u0` of the problem and sets + `Initial.(unknowns(sys))` in the former, returning the updated parameter object. """ set_initial_unknowns!::SIU end @@ -856,6 +919,38 @@ function (guu::GetUpdatedU0)(prob, initprob) return buffer end +struct SetInitialUnknowns{S} + setter!::S +end + +function SetInitialUnknowns(sys::AbstractSystem) + return SetInitialUnknowns(setu(sys, Initial.(unknowns(sys)))) +end + +function (siu::SetInitialUnknowns)(p::MTKParameters, u0) + if ArrayInterface.ismutable(p.initials) + siu.setter!(p, u0) + else + originalT = similar_type(p.initials) + @set! p.initials = MVector{length(p.initials), eltype(p.initials)}(p.initials) + siu.setter!(p, u0) + @set! p.initials = originalT(p.initials) + end + return p +end + +function (siu::SetInitialUnknowns)(p::Vector, u0) + if ArrayInterface.ismutable(p) + siu.setter!(p, u0) + else + originalT = similar_type(p) + p = MVector{length(p), eltype(p)}(p) + siu.setter!(p, u0) + p = originalT(p) + end + return p +end + """ $(TYPEDSIGNATURES) @@ -913,8 +1008,9 @@ function maybe_build_initialization_problem( end meta = InitializationMetadata( u0map, pmap, guesses, Vector{Equation}(initialization_eqs), - use_scc, ReconstructInitializeprob(sys, initializeprob.f.sys), - get_initial_unknowns, setp(sys, Initial.(unknowns(sys)))) + use_scc, ReconstructInitializeprob( + sys, initializeprob.f.sys; u0_constructor, p_constructor), + get_initial_unknowns, SetInitialUnknowns(sys)) if is_time_dependent(sys) all_init_syms = Set(all_symbols(initializeprob)) @@ -930,11 +1026,8 @@ function maybe_build_initialization_problem( if initializeprobmap === nothing && isempty(punknowns) initializeprobpmap = nothing else - allsyms = all_symbols(initializeprob) - initdvs = filter(x -> any(isequal(x), allsyms), unknowns(sys)) - getpunknowns = getu(initializeprob, [punknowns; initdvs]) - setpunknowns = setp(sys, [punknowns; Initial.(initdvs)]) - initializeprobpmap = GetUpdatedMTKParameters(getpunknowns, setpunknowns) + initializeprobpmap = construct_initializeprobpmap( + sys, initializeprob.f.sys; p_constructor) end reqd_syms = parameter_symbols(initializeprob) @@ -942,8 +1035,7 @@ function maybe_build_initialization_problem( if initializeprobmap === nothing && initializeprobpmap === nothing update_initializeprob! = nothing else - update_initializeprob! = UpdateInitializeprob( - getu(sys, reqd_syms), setu(initializeprob, reqd_syms)) + update_initializeprob! = ModelingToolkit.update_initializeprob! end for p in punknowns @@ -967,7 +1059,7 @@ function maybe_build_initialization_problem( return (; initialization_data = SciMLBase.OverrideInitData( initializeprob, update_initializeprob!, initializeprobmap, - initializeprobpmap; metadata = meta)) + initializeprobpmap; metadata = meta, is_update_oop = Val{true})) end """ From ad0347b431ffc74606b61f0048e6e01a1d36d3b6 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 5 May 2025 21:40:50 +0530 Subject: [PATCH 1545/2176] fix: handle `u0_constructor`, `p_constructor` in `remake_initialization_data` --- src/systems/nonlinear/initializesystem.jl | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/systems/nonlinear/initializesystem.jl b/src/systems/nonlinear/initializesystem.jl index ce29db68b4..fe932418a4 100644 --- a/src/systems/nonlinear/initializesystem.jl +++ b/src/systems/nonlinear/initializesystem.jl @@ -581,11 +581,21 @@ function SciMLBase.remake_initialization_data( op, missing_unknowns, missing_pars = build_operating_point!(sys, u0map, pmap, defs, cmap, dvs, ps) floatT = float_type_from_varmap(op) + u0_constructor = p_constructor = identity + if newu0 isa StaticArray + u0_constructor = vals -> SymbolicUtils.Code.create_array( + typeof(newu0), floatT, Val(1), Val(length(vals)), vals...) + end + if newp isa StaticArray || newp isa MTKParameters && newp.initials isa StaticArray + p_constructor = vals -> SymbolicUtils.Code.create_array( + typeof(newp.initials), floatT, Val(1), Val(length(vals)), vals...) + end kws = maybe_build_initialization_problem( - sys, op, u0map, pmap, t0, defs, guesses, missing_unknowns; - use_scc, initialization_eqs, floatT, allow_incomplete = true) + sys, SciMLBase.isinplace(odefn), op, u0map, pmap, t0, defs, guesses, missing_unknowns; + use_scc, initialization_eqs, floatT, u0_constructor, p_constructor, allow_incomplete = true) - return SciMLBase.remake_initialization_data(sys, kws, newu0, t0, newp, newu0, newp) + odefn = remake(odefn; kws...) + return SciMLBase.remake_initialization_data(sys, odefn, newu0, t0, newp, newu0, newp) end function promote_u0_p(u0, p::MTKParameters, t0) From 926fcedfc186bae3e755c8c65f1fbb86e5f9a49e Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 5 May 2025 21:41:10 +0530 Subject: [PATCH 1546/2176] fix: handle immutable MTKParameters in symbolic `late_binding_update_u0_p` --- src/systems/nonlinear/initializesystem.jl | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/systems/nonlinear/initializesystem.jl b/src/systems/nonlinear/initializesystem.jl index fe932418a4..c7a590197f 100644 --- a/src/systems/nonlinear/initializesystem.jl +++ b/src/systems/nonlinear/initializesystem.jl @@ -640,8 +640,8 @@ function SciMLBase.late_binding_update_u0_p( return newu0, newp end - newp = p === missing ? copy(newp) : newp - + syms = [] + vals = [] allsyms = all_symbols(sys) for (k, v) in u0 v === nothing && continue @@ -653,9 +653,11 @@ function SciMLBase.late_binding_update_u0_p( k = k2 end is_parameter(sys, Initial(k)) || continue - setp(sys, Initial(k))(newp, v) + push!(syms, Initial(k)) + push!(vals, v) end + newp = setp_oop(sys, syms)(newp, vals) return newu0, newp end From 52ce6418efbc1a81e4076e32bf14b5bc833196ba Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 5 May 2025 21:41:35 +0530 Subject: [PATCH 1547/2176] fix: handle edge case in floating point type promotion --- 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 897b2fcc44..999e4405ff 100644 --- a/src/systems/problem_utils.jl +++ b/src/systems/problem_utils.jl @@ -1187,7 +1187,7 @@ function process_SciMLProblem( u0map, pmap, defs, cmap, dvs, ps) floatT = Bool - if u0Type <: AbstractArray && eltype(u0Type) <: Real + if u0Type <: AbstractArray && eltype(u0Type) <: Real && eltype(u0Type) != Union{} floatT = float(eltype(u0Type)) else floatT = float_type_from_varmap(op, floatT) From 57e4778aa89059225eb0afef4a5499ce17c46390 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 5 May 2025 21:43:05 +0530 Subject: [PATCH 1548/2176] fix: call `u0_constructor` on `resid_prototype` --- src/systems/nonlinear/nonlinearsystem.jl | 17 +--------------- src/systems/problem_utils.jl | 25 +++++++++++++++++++++++- test/nonlinearsystem.jl | 16 +++++++++++++++ 3 files changed, 41 insertions(+), 17 deletions(-) diff --git a/src/systems/nonlinear/nonlinearsystem.jl b/src/systems/nonlinear/nonlinearsystem.jl index f8182fa28a..d0a12e22d4 100644 --- a/src/systems/nonlinear/nonlinearsystem.jl +++ b/src/systems/nonlinear/nonlinearsystem.jl @@ -345,16 +345,6 @@ function hessian_sparsity(sys::NonlinearSystem) unknowns(sys)) for eq in equations(sys)] end -function calculate_resid_prototype(N, u0, p) - u0ElType = u0 === nothing ? Float64 : eltype(u0) - if SciMLStructures.isscimlstructure(p) - u0ElType = promote_type( - eltype(SciMLStructures.canonicalize(SciMLStructures.Tunable(), p)[1]), - u0ElType) - end - return zeros(u0ElType, N) -end - """ ```julia SciMLBase.NonlinearFunction{iip}(sys::NonlinearSystem, dvs = unknowns(sys), @@ -381,6 +371,7 @@ function SciMLBase.NonlinearFunction{iip}(sys::NonlinearSystem, dvs = unknowns(s eval_module = @__MODULE__, sparse = false, simplify = false, initialization_data = nothing, cse = true, + resid_prototype = nothing, kwargs...) where {iip} if !iscomplete(sys) error("A completed `NonlinearSystem` is required. Call `complete` or `structural_simplify` on the system before creating a `NonlinearFunction`") @@ -402,12 +393,6 @@ function SciMLBase.NonlinearFunction{iip}(sys::NonlinearSystem, dvs = unknowns(s observedfun = ObservedFunctionCache( sys; eval_expression, eval_module, checkbounds = get(kwargs, :checkbounds, false), cse) - if length(dvs) == length(equations(sys)) - resid_prototype = nothing - else - resid_prototype = calculate_resid_prototype(length(equations(sys)), u0, p) - end - NonlinearFunction{iip}(f; sys = sys, jac = _jac === nothing ? nothing : _jac, diff --git a/src/systems/problem_utils.jl b/src/systems/problem_utils.jl index 999e4405ff..fc377607e8 100644 --- a/src/systems/problem_utils.jl +++ b/src/systems/problem_utils.jl @@ -1082,6 +1082,22 @@ function float_type_from_varmap(varmap, floatT = Bool) return float(floatT) end +""" + $(TYPEDSIGNATURES) + +Calculate the `resid_prototype` for a `NonlinearFunction` with `N` equations and the +provided `u0` and `p`. +""" +function calculate_resid_prototype(N::Int, u0, p) + u0ElType = u0 === nothing ? Float64 : eltype(u0) + if SciMLStructures.isscimlstructure(p) + u0ElType = promote_type( + eltype(SciMLStructures.canonicalize(SciMLStructures.Tunable(), p)[1]), + u0ElType) + end + return zeros(u0ElType, N) +end + """ $(TYPEDSIGNATURES) @@ -1293,7 +1309,14 @@ function process_SciMLProblem( end initialization_data = SciMLBase.remake_initialization_data( kwargs.initialization_data, kwargs, u0, t0, p, u0, p) - kwargs = merge(kwargs,) + kwargs = merge(kwargs, (; initialization_data)) + end + + if constructor <: NonlinearFunction && length(dvs) != length(eqs) + kwargs = merge(kwargs, + (; + resid_prototype = u0_constructor(calculate_resid_prototype( + length(eqs), u0, p)))) end f = constructor(sys, dvs, ps, u0; p = p, diff --git a/test/nonlinearsystem.jl b/test/nonlinearsystem.jl index a315371141..158475f7c9 100644 --- a/test/nonlinearsystem.jl +++ b/test/nonlinearsystem.jl @@ -442,3 +442,19 @@ end @test !in(D(y), vs) end end + +@testset "oop `NonlinearLeastSquaresProblem` with `u0 === nothing`" begin + @variables x y + @named sys = NonlinearSystem([0 ~ x - y], [], []; observed = [x ~ 1.0, y ~ 1.0]) + prob = NonlinearLeastSquaresProblem{false}(complete(sys), nothing) + sol = solve(prob) + resid = sol.resid + @test resid == [0.0] + @test resid isa Vector + prob = NonlinearLeastSquaresProblem{false}( + complete(sys), nothing; u0_constructor = splat(SVector)) + sol = solve(prob) + resid = sol.resid + @test resid == [0.0] + @test resid isa SVector +end From ba2bfff3c182053a71cdc326a09d3d2972e7a950 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 5 May 2025 21:43:16 +0530 Subject: [PATCH 1549/2176] test: test initialization on static array problems --- test/initializationsystem.jl | 80 ++++++++++++++++++++++++------------ 1 file changed, 53 insertions(+), 27 deletions(-) diff --git a/test/initializationsystem.jl b/test/initializationsystem.jl index 87ba755259..df52291fbe 100644 --- a/test/initializationsystem.jl +++ b/test/initializationsystem.jl @@ -1,6 +1,6 @@ using ModelingToolkit, OrdinaryDiffEq, NonlinearSolve, Test using StochasticDiffEq, DelayDiffEq, StochasticDelayDiffEq, JumpProcesses -using ForwardDiff +using ForwardDiff, StaticArrays using SymbolicIndexingInterface, SciMLStructures using SciMLStructures: Tunable using ModelingToolkit: t_nounits as t, D_nounits as D, observed @@ -594,22 +594,36 @@ end @parameters p q @brownian a b x = _x(t) - + sarray_ctor = splat(SVector) # `System` constructor creates appropriate type with mtkbuild # `Problem` and `alg` create the problem to test and allow calling `init` with # the correct solver. # `rhss` allows adding terms to the end of equations (only 2 equations allowed) to influence # the system type (brownian vars to turn it into an SDE). - @testset "$Problem with $(SciMLBase.parameterless_type(alg))" for (System, Problem, alg, rhss) in [ - (ModelingToolkit.System, ODEProblem, Tsit5(), zeros(2)), - (ModelingToolkit.System, SDEProblem, ImplicitEM(), [a, b]), - (ModelingToolkit.System, DDEProblem, MethodOfSteps(Tsit5()), [_x(t - 0.1), 0.0]), - (ModelingToolkit.System, SDDEProblem, ImplicitEM(), [_x(t - 0.1) + a, b]) - ] + @testset "$Problem with $(SciMLBase.parameterless_type(alg)) and $ctor ctor" for ((System, Problem, alg, rhss), (ctor, expectedT)) in Iterators.product( + [ + (ModelingToolkit.System, ODEProblem, Tsit5(), zeros(2)), + (ModelingToolkit.System, SDEProblem, ImplicitEM(), [a, b]), + (ModelingToolkit.System, DDEProblem, + MethodOfSteps(Tsit5()), [_x(t - 0.1), 0.0]), + (ModelingToolkit.System, SDDEProblem, ImplicitEM(), [_x(t - 0.1) + a, b]) + ], + [(identity, Any), (sarray_ctor, SVector)]) + u0_constructor = p_constructor = ctor + if ctor !== identity + Problem = Problem{false} + end function test_parameter(prob, sym, val) if prob.u0 !== nothing + @test prob.u0 isa expectedT @test init(prob, alg).ps[sym] ≈ val end + @test prob.p.tunable isa expectedT + initprob = prob.f.initialization_data.initializeprob + if state_values(initprob) !== nothing + @test state_values(initprob) isa expectedT + end + @test parameter_values(initprob).tunable isa expectedT @test solve(prob, alg).ps[sym] ≈ val end function test_initializesystem(sys, u0map, pmap, p, equation) @@ -626,64 +640,64 @@ end @mtkbuild sys = System( [D(x) ~ x * q + rhss[1], D(y) ~ y * p + rhss[2]], t; defaults = [p => missing], guesses = [p => 1.0]) pmap[p] = 2q - prob = Problem(sys, u0map, (0.0, 1.0), pmap) + prob = Problem(sys, u0map, (0.0, 1.0), pmap; u0_constructor, p_constructor) test_parameter(prob, p, 2.0) prob2 = remake(prob; u0 = u0map, p = pmap) - prob2.ps[p] = 0.0 + prob2 = remake(prob2; p = setp_oop(prob2, p)(prob2, 0.0)) test_parameter(prob2, p, 2.0) # `missing` default, provided guess @mtkbuild sys = System( [D(x) ~ x + rhss[1], p ~ x + y + rhss[2]], t; defaults = [p => missing], guesses = [p => 0.0]) - prob = Problem(sys, u0map, (0.0, 1.0)) + prob = Problem(sys, u0map, (0.0, 1.0); u0_constructor, p_constructor) test_parameter(prob, p, 2.0) test_initializesystem(sys, u0map, pmap, p, 0 ~ p - x - y) prob2 = remake(prob; u0 = u0map) - prob2.ps[p] = 0.0 + prob2 = remake(prob2; p = setp_oop(prob2, p)(prob2, 0.0)) test_parameter(prob2, p, 2.0) # `missing` to Problem, equation from default @mtkbuild sys = System( [D(x) ~ x * q + rhss[1], D(y) ~ y * p + rhss[2]], t; defaults = [p => 2q], guesses = [p => 1.0]) pmap[p] = missing - prob = Problem(sys, u0map, (0.0, 1.0), pmap) + prob = Problem(sys, u0map, (0.0, 1.0), pmap; u0_constructor, p_constructor) test_parameter(prob, p, 2.0) test_initializesystem(sys, u0map, pmap, p, 0 ~ 2q - p) prob2 = remake(prob; u0 = u0map, p = pmap) - prob2.ps[p] = 0.0 + prob2 = remake(prob2; p = setp_oop(prob2, p)(prob2, 0.0)) test_parameter(prob2, p, 2.0) # `missing` to Problem, provided guess @mtkbuild sys = System( [D(x) ~ x + rhss[1], p ~ x + y + rhss[2]], t; guesses = [p => 0.0]) - prob = Problem(sys, u0map, (0.0, 1.0), pmap) + prob = Problem(sys, u0map, (0.0, 1.0), pmap; u0_constructor, p_constructor) test_parameter(prob, p, 2.0) test_initializesystem(sys, u0map, pmap, p, 0 ~ x + y - p) prob2 = remake(prob; u0 = u0map, p = pmap) - prob2.ps[p] = 0.0 + prob2 = remake(prob2; p = setp_oop(prob2, p)(prob2, 0.0)) test_parameter(prob2, p, 2.0) # No `missing`, default and guess @mtkbuild sys = System( [D(x) ~ x * q + rhss[1], D(y) ~ y * p + rhss[2]], t; defaults = [p => 2q], guesses = [p => 0.0]) delete!(pmap, p) - prob = Problem(sys, u0map, (0.0, 1.0), pmap) + prob = Problem(sys, u0map, (0.0, 1.0), pmap; u0_constructor, p_constructor) test_parameter(prob, p, 2.0) test_initializesystem(sys, u0map, pmap, p, 0 ~ 2q - p) prob2 = remake(prob; u0 = u0map, p = pmap) - prob2.ps[p] = 0.0 + prob2 = remake(prob2; p = setp_oop(prob2, p)(prob2, 0.0)) test_parameter(prob2, p, 2.0) # Default overridden by Problem, guess provided @mtkbuild sys = System( [D(x) ~ q * x + rhss[1], D(y) ~ y * p + rhss[2]], t; defaults = [p => 2q], guesses = [p => 1.0]) _pmap = merge(pmap, Dict(p => q)) - prob = Problem(sys, u0map, (0.0, 1.0), _pmap) + prob = Problem(sys, u0map, (0.0, 1.0), _pmap; u0_constructor, p_constructor) test_parameter(prob, p, _pmap[q]) test_initializesystem(sys, u0map, _pmap, p, 0 ~ q - p) # Problem dependent value with guess, no `missing` @mtkbuild sys = System( [D(x) ~ y * q + p + rhss[1], D(y) ~ x * p + q + rhss[2]], t; guesses = [p => 0.0]) _pmap = merge(pmap, Dict(p => 3q)) - prob = Problem(sys, u0map, (0.0, 1.0), _pmap) + prob = Problem(sys, u0map, (0.0, 1.0), _pmap; u0_constructor, p_constructor) test_parameter(prob, p, 3pmap[q]) # Should not be solved for: @@ -691,7 +705,7 @@ end @mtkbuild sys = System( [D(x) ~ q * x + rhss[1], D(y) ~ y * p + rhss[2]], t; defaults = [p => 2q], guesses = [p => 1.0]) _pmap = merge(pmap, Dict(p => 1.0)) - prob = Problem(sys, u0map, (0.0, 1.0), _pmap) + prob = Problem(sys, u0map, (0.0, 1.0), _pmap; u0_constructor, p_constructor) @test prob.ps[p] ≈ 1.0 initsys = prob.f.initialization_data.initializeprob.f.sys @test is_parameter(initsys, p) @@ -700,7 +714,7 @@ end @parameters r::Int s::Int @mtkbuild sys = System( [D(x) ~ s * x + rhss[1], D(y) ~ y * r + rhss[2]], t; defaults = [s => 2r], guesses = [s => 1.0]) - prob = Problem(sys, u0map, (0.0, 1.0), [r => 1]) + prob = Problem(sys, u0map, (0.0, 1.0), [r => 1]; u0_constructor, p_constructor) @test prob.ps[r] == 1 @test prob.ps[s] == 2 initsys = prob.f.initialization_data.initializeprob.f.sys @@ -714,7 +728,7 @@ end # Unsatisfiable initialization prob = Problem(sys, [x => 1.0, y => 1.0], (0.0, 1.0), - [p => 2.0]; initialization_eqs = [x^2 + y^2 ~ 3]) + [p => 2.0]; initialization_eqs = [x^2 + y^2 ~ 3], u0_constructor, p_constructor) @test prob.f.initialization_data !== nothing @test solve(prob, alg).retcode == ReturnCode.InitialFailure cache = init(prob, alg) @@ -791,8 +805,17 @@ end prob_alg_combinations = zip( [NonlinearProblem, NonlinearLeastSquaresProblem], [nl_algs, nlls_algs]) - @testset "Parameter initialization" begin + sarray_ctor = splat(SVector) + @testset "Parameter initialization with ctor $ctor" for (ctor, expectedT) in [ + (identity, Any), + (sarray_ctor, SVector) + ] + u0_constructor = p_constructor = ctor function test_parameter(prob, alg, param, val) + if prob.u0 !== nothing + @test prob.u0 isa expectedT + end + @test prob.p.tunable isa expectedT integ = init(prob, alg) @test integ.ps[param]≈val rtol=1e-5 # some algorithms are a little temperamental @@ -818,7 +841,10 @@ end # guesses = [q => 1.0], initialization_eqs = [p^2 + q^2 + 2p * q ~ 0]) for (probT, algs) in prob_alg_combinations - prob = probT(sys, []) + if ctor != identity + probT = probT{false} + end + prob = probT(sys, []; u0_constructor, p_constructor) @test prob.f.initialization_data !== nothing @test prob.f.initialization_data.initializeprobmap === nothing for alg in algs @@ -826,11 +852,11 @@ end end # `update_initializeprob!` works - prob.ps[p] = -2.0 + prob = remake(prob; p = setp_oop(prob, p)(prob, -2.0)) for alg in algs test_parameter(prob, alg, q, 2.0) end - prob.ps[p] = 2.0 + prob = remake(prob; p = setp_oop(prob, p)(prob, 2.0)) # `remake` works prob2 = remake(prob; p = [p => -2.0]) From d3ce046101b5ccdfdbafacabd8d9c431f61ac900 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 8 May 2025 23:16:19 +0530 Subject: [PATCH 1550/2176] build: bump SciMLBase, StochasticDelayDiffEq compat --- Project.toml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Project.toml b/Project.toml index 6d637ef0a0..e602e97fe2 100644 --- a/Project.toml +++ b/Project.toml @@ -142,7 +142,7 @@ RecursiveArrayTools = "3.26" Reexport = "0.2, 1" RuntimeGeneratedFunctions = "0.5.9" SCCNonlinearSolve = "1.0.0" -SciMLBase = "2.84" +SciMLBase = "2.88" SciMLStructures = "1.7" Serialization = "1" Setfield = "0.7, 0.8, 1" @@ -150,7 +150,7 @@ SimpleNonlinearSolve = "0.1.0, 1, 2" SparseArrays = "1" SpecialFunctions = "0.7, 0.8, 0.9, 0.10, 1.0, 2" StaticArrays = "0.10, 0.11, 0.12, 1.0" -StochasticDelayDiffEq = "1.8.1" +StochasticDelayDiffEq = "1.10" StochasticDiffEq = "6.72.1" SymbolicIndexingInterface = "0.3.39" SymbolicUtils = "3.26.1" From d3512678bb6c8a7d5532862b540e776531366d14 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 12 May 2025 12:28:33 +0530 Subject: [PATCH 1551/2176] fix: handle empty `syms` in `concrete_getu` --- src/systems/problem_utils.jl | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/systems/problem_utils.jl b/src/systems/problem_utils.jl index fc377607e8..739d2c5d4d 100644 --- a/src/systems/problem_utils.jl +++ b/src/systems/problem_utils.jl @@ -667,7 +667,11 @@ function get_mtkparameters_reconstructor(srcsys::AbstractSystem, dstsys::Abstrac # `syms[1]` is always the tunables because `srcsys` will have initials. tunable_syms = syms[1] - tunable_getter = p_constructor ∘ concrete_getu(srcsys, tunable_syms) + tunable_getter = if isempty(tunable_syms) + Returns(SizedVector{0, Float64}()) + else + p_constructor ∘ concrete_getu(srcsys, tunable_syms) + end rest_getters = map(Base.tail(Base.tail(syms))) do buf if buf == () return Returns(()) @@ -675,7 +679,7 @@ function get_mtkparameters_reconstructor(srcsys::AbstractSystem, dstsys::Abstrac return Base.Fix1(broadcast, p_constructor) ∘ getu(srcsys, buf) end end - initials_getter = if initials + initials_getter = if initials && !isempty(syms[2]) initsyms = Vector{Any}(syms[2]) allsyms = Set(all_symbols(srcsys)) if unwrap_initials From 329b7d5ab22f996151cd6060e227845785a9b36d Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 12 May 2025 15:04:22 +0530 Subject: [PATCH 1552/2176] fix: fix `is_update_oop` passed as type --- src/systems/problem_utils.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/systems/problem_utils.jl b/src/systems/problem_utils.jl index 739d2c5d4d..087f99dcb4 100644 --- a/src/systems/problem_utils.jl +++ b/src/systems/problem_utils.jl @@ -822,7 +822,7 @@ end $(TYPEDSIGNATURES) A function to be used as `update_initializeprob!` in `OverrideInitData`. Requires -`is_update_oop = Val{true}` to be passed to `update_initializeprob!`. +`is_update_oop = Val(true)` to be passed to `update_initializeprob!`. """ function update_initializeprob!(initprob, prob) p = get_scimlfn(prob).initialization_data.metadata.oop_reconstruct_u0_p.getter( @@ -1063,7 +1063,7 @@ function maybe_build_initialization_problem( return (; initialization_data = SciMLBase.OverrideInitData( initializeprob, update_initializeprob!, initializeprobmap, - initializeprobpmap; metadata = meta, is_update_oop = Val{true})) + initializeprobpmap; metadata = meta, is_update_oop = Val(true))) end """ From 6c57557bd31e1787a5a25bcd633d35a78eaa5cd9 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 12 May 2025 15:04:31 +0530 Subject: [PATCH 1553/2176] fix: fix call to `remake_initialization_data` --- 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 087f99dcb4..3fef424ad6 100644 --- a/src/systems/problem_utils.jl +++ b/src/systems/problem_utils.jl @@ -1312,7 +1312,7 @@ function process_SciMLProblem( t0 = zero(floatT) end initialization_data = SciMLBase.remake_initialization_data( - kwargs.initialization_data, kwargs, u0, t0, p, u0, p) + sys, kwargs, u0, t0, p, u0, p) kwargs = merge(kwargs, (; initialization_data)) end From f1e422c42d421f8385610d959875397207c9f11c Mon Sep 17 00:00:00 2001 From: Christopher Rackauckas Date: Mon, 12 May 2025 12:52:39 +0000 Subject: [PATCH 1554/2176] Update Project.toml --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index e602e97fe2..1099993e33 100644 --- a/Project.toml +++ b/Project.toml @@ -142,7 +142,7 @@ RecursiveArrayTools = "3.26" Reexport = "0.2, 1" RuntimeGeneratedFunctions = "0.5.9" SCCNonlinearSolve = "1.0.0" -SciMLBase = "2.88" +SciMLBase = "2.89.1" SciMLStructures = "1.7" Serialization = "1" Setfield = "0.7, 0.8, 1" From 7600f5f2e0d4a13a189b7c8cd0873eb544a6beee Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 12 May 2025 19:37:30 +0530 Subject: [PATCH 1555/2176] fix: fix `update_initializeprob!` --- 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 3fef424ad6..c1e4642c79 100644 --- a/src/systems/problem_utils.jl +++ b/src/systems/problem_utils.jl @@ -825,7 +825,7 @@ A function to be used as `update_initializeprob!` in `OverrideInitData`. Require `is_update_oop = Val(true)` to be passed to `update_initializeprob!`. """ function update_initializeprob!(initprob, prob) - p = get_scimlfn(prob).initialization_data.metadata.oop_reconstruct_u0_p.getter( + p = get_scimlfn(prob).initialization_data.metadata.oop_reconstruct_u0_p.pgetter( prob, initprob) return remake(initprob; p) end From 4631a1e2062445d7e0e05aba175dd440673ef862 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 12 May 2025 23:49:03 +0530 Subject: [PATCH 1556/2176] fix: add `f` field to `MockIntegrator` --- src/linearization.jl | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/linearization.jl b/src/linearization.jl index 77f4422b63..b30d275818 100644 --- a/src/linearization.jl +++ b/src/linearization.jl @@ -285,7 +285,7 @@ function (linfun::LinearizationFunction)(u, p, t) linfun.num_states == length(u) || error("Number of unknown variables ($(linfun.num_states)) does not match the number of input unknowns ($(length(u)))") integ_cache = (linfun.caches,) - integ = MockIntegrator{true}(u, p, t, integ_cache, nothing) + integ = MockIntegrator{true}(u, p, t, fun, integ_cache, nothing) u, p, success = SciMLBase.get_initial_values( linfun.prob, integ, fun, linfun.initializealg, Val(true); linfun.initialize_kwargs...) @@ -325,7 +325,7 @@ Mock `DEIntegrator` to allow using `CheckInit` without having to create a new in $(TYPEDFIELDS) """ -struct MockIntegrator{iip, U, P, T, C, O} <: SciMLBase.DEIntegrator{Nothing, iip, U, T} +struct MockIntegrator{iip, U, P, T, F, C, O} <: SciMLBase.DEIntegrator{Nothing, iip, U, T} """ The state vector. """ @@ -339,6 +339,10 @@ struct MockIntegrator{iip, U, P, T, C, O} <: SciMLBase.DEIntegrator{Nothing, iip """ t::T """ + The wrapped `SciMLFunction`. + """ + f::F + """ The integrator cache. """ cache::C @@ -348,8 +352,9 @@ struct MockIntegrator{iip, U, P, T, C, O} <: SciMLBase.DEIntegrator{Nothing, iip opts::O end -function MockIntegrator{iip}(u::U, p::P, t::T, cache::C, opts::O) where {iip, U, P, T, C, O} - return MockIntegrator{iip, U, P, T, C, O}(u, p, t, cache, opts) +function MockIntegrator{iip}( + u::U, p::P, t::T, f::F, cache::C, opts::O) where {iip, U, P, T, F, C, O} + return MockIntegrator{iip, U, P, T, F, C, O}(u, p, t, f, cache, opts) end SymbolicIndexingInterface.state_values(integ::MockIntegrator) = integ.u From a35ae7d8d796c33c4e25e290f9894f0d2d2c319b Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 15 May 2025 17:23:03 +0530 Subject: [PATCH 1557/2176] fix: handle discretes properly in `get_mtkparameters_reconstructor` --- src/systems/problem_utils.jl | 47 ++++++++++++++++++++++++------------ 1 file changed, 32 insertions(+), 15 deletions(-) diff --git a/src/systems/problem_utils.jl b/src/systems/problem_utils.jl index c1e4642c79..48f03b07a4 100644 --- a/src/systems/problem_utils.jl +++ b/src/systems/problem_utils.jl @@ -672,13 +672,6 @@ function get_mtkparameters_reconstructor(srcsys::AbstractSystem, dstsys::Abstrac else p_constructor ∘ concrete_getu(srcsys, tunable_syms) end - rest_getters = map(Base.tail(Base.tail(syms))) do buf - if buf == () - return Returns(()) - else - return Base.Fix1(broadcast, p_constructor) ∘ getu(srcsys, buf) - end - end initials_getter = if initials && !isempty(syms[2]) initsyms = Vector{Any}(syms[2]) allsyms = Set(all_symbols(srcsys)) @@ -700,7 +693,29 @@ function get_mtkparameters_reconstructor(srcsys::AbstractSystem, dstsys::Abstrac else Returns(SizedVector{0, Float64}()) end - getters = (tunable_getter, initials_getter, rest_getters...) + discs_getter = if isempty(syms[3]) + Returns(()) + else + ic = get_index_cache(dstsys) + blockarrsizes = Tuple(map(ic.discrete_buffer_sizes) do bufsizes + map(x -> x.length, bufsizes) + end) + # discretes need to be blocked arrays + # the `getu` returns a tuple of arrays corresponding to `p.discretes` + # `Base.Fix1(...)` applies `p_constructor` to each of the arrays in the tuple + # `Base.Fix2(...)` does `BlockedArray.(tuple_of_arrs, blockarrsizes)` returning a + # tuple of `BlockedArray`s + Base.Fix2(Broadcast.BroadcastFunction(BlockedArray), blockarrsizes) ∘ + 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 + end + getters = (tunable_getter, initials_getter, discs_getter, rest_getters...) getter = let getters = getters function _getter(valp, initprob) oldcache = parameter_values(initprob).caches @@ -772,12 +787,14 @@ function (rip::ReconstructInitializeprob)(srcvalp, dstvalp) copyto!(newbuf, buf) newp = repack(newbuf) end - # and initials portion - buf, repack, alias = SciMLStructures.canonicalize(SciMLStructures.Initials(), newp) - if eltype(buf) != T - newbuf = similar(buf, T) - copyto!(newbuf, buf) - newp = repack(newbuf) + if newp isa MTKParameters + # and initials portion + buf, repack, alias = SciMLStructures.canonicalize(SciMLStructures.Initials(), newp) + if eltype(buf) != T + newbuf = similar(buf, T) + copyto!(newbuf, buf) + newp = repack(newbuf) + end end return u0, newp end @@ -793,7 +810,7 @@ function construct_initializeprobpmap( @assert is_initializesystem(initsys) if is_split(sys) return let getter = get_mtkparameters_reconstructor( - initsys, sys; initials = true, p_constructor) + initsys, sys; initials = true, unwrap_initials = true, p_constructor) function initprobpmap_split(prob, initsol) getter(initsol, prob) end From 1de4cd5e11549e6d2a05874e7fb64926a9d0e4ba Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 15 May 2025 17:43:12 +0530 Subject: [PATCH 1558/2176] refactor: move ChainRulesCoreExt into main package --- Project.toml | 3 +- src/ModelingToolkit.jl | 4 +++ .../adjoints.jl | 32 +++++++------------ 3 files changed, 17 insertions(+), 22 deletions(-) rename ext/MTKChainRulesCoreExt.jl => src/adjoints.jl (73%) diff --git a/Project.toml b/Project.toml index 1099993e33..302c74b1bc 100644 --- a/Project.toml +++ b/Project.toml @@ -8,6 +8,7 @@ ADTypes = "47edcb42-4c32-4615-8424-f2b9edc5f35b" AbstractTrees = "1520ce14-60c1-5f80-bbc7-55ef81b5835c" ArrayInterface = "4fba245c-0d91-5ea0-9b3e-6abc04ee57a9" BlockArrays = "8e7c35d0-a365-5155-bbbb-fb81a777f24e" +ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4" Combinatorics = "861a8166-3701-5b0c-9a16-15d98fcdc6aa" CommonSolve = "38540f10-b2f7-11e9-35d8-d573e4eb0ff2" Compat = "34da2185-b29b-5c13-b0c7-acf172513d20" @@ -65,7 +66,6 @@ Unitful = "1986cc42-f94f-5a68-af5c-568840ba703d" [weakdeps] BifurcationKit = "0f109fa4-8a5d-4b75-95aa-f515264e7665" CasADi = "c49709b8-5c63-11e9-2fb2-69db5844192f" -ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4" DeepDiffs = "ab62b9b5-e342-54a8-a765-a90f495de1a6" FMI = "14a09403-18e3-468f-ad8a-74f8dda2d9ac" InfiniteOpt = "20393b10-9daf-11e9-18c9-8db751c92c57" @@ -74,7 +74,6 @@ LabelledArrays = "2ee39098-c373-598a-b85f-a56591580800" [extensions] MTKBifurcationKitExt = "BifurcationKit" MTKCasADiDynamicOptExt = "CasADi" -MTKChainRulesCoreExt = "ChainRulesCore" MTKDeepDiffsExt = "DeepDiffs" MTKFMIExt = "FMI" MTKInfiniteOptExt = "InfiniteOpt" diff --git a/src/ModelingToolkit.jl b/src/ModelingToolkit.jl index 6d8d89a976..9ed6b9994a 100644 --- a/src/ModelingToolkit.jl +++ b/src/ModelingToolkit.jl @@ -62,6 +62,8 @@ import BlockArrays: BlockArray, BlockedArray, Block, blocksize, blocksizes, bloc using OffsetArrays: Origin import CommonSolve import EnumX +import ChainRulesCore +import ChainRulesCore: Tangent, ZeroTangent, NoTangent, zero_tangent, unthunk using RuntimeGeneratedFunctions using RuntimeGeneratedFunctions: drop_expr @@ -204,6 +206,8 @@ include("structural_transformation/StructuralTransformations.jl") @reexport using .StructuralTransformations include("inputoutput.jl") +include("adjoints.jl") + for S in subtypes(ModelingToolkit.AbstractSystem) S = nameof(S) @eval convert_system(::Type{<:$S}, sys::$S) = sys diff --git a/ext/MTKChainRulesCoreExt.jl b/src/adjoints.jl similarity index 73% rename from ext/MTKChainRulesCoreExt.jl rename to src/adjoints.jl index a2974ea2dd..98266de938 100644 --- a/ext/MTKChainRulesCoreExt.jl +++ b/src/adjoints.jl @@ -1,17 +1,11 @@ -module MTKChainRulesCoreExt - -import ModelingToolkit as MTK -import ChainRulesCore -import ChainRulesCore: Tangent, ZeroTangent, NoTangent, zero_tangent, unthunk - -function ChainRulesCore.rrule(::Type{MTK.MTKParameters}, tunables, args...) +function ChainRulesCore.rrule(::Type{MTKParameters}, tunables, args...) function mtp_pullback(dt) dt = unthunk(dt) dtunables = dt isa AbstractArray ? dt : dt.tunable (NoTangent(), dtunables[1:length(tunables)], ntuple(_ -> NoTangent(), length(args))...) end - MTK.MTKParameters(tunables, args...), mtp_pullback + MTKParameters(tunables, args...), mtp_pullback end function subset_idxs(idxs, portion, template) @@ -70,23 +64,23 @@ function selected_tangents( end function ChainRulesCore.rrule( - ::typeof(MTK.remake_buffer), indp, oldbuf::MTK.MTKParameters, idxs, vals) + ::typeof(remake_buffer), indp, oldbuf::MTKParameters, idxs, vals) if idxs isa AbstractSet idxs = collect(idxs) end idxs = map(idxs) do i - i isa MTK.ParameterIndex ? i : MTK.parameter_index(indp, i) + i isa ParameterIndex ? i : parameter_index(indp, i) end - newbuf = MTK.remake_buffer(indp, oldbuf, idxs, vals) + newbuf = remake_buffer(indp, oldbuf, idxs, vals) tunable_idxs = reduce( - vcat, (idx.idx for idx in idxs if idx.portion isa MTK.SciMLStructures.Tunable); + vcat, (idx.idx for idx in idxs if idx.portion isa SciMLStructures.Tunable); init = Union{Int, AbstractVector{Int}}[]) initials_idxs = reduce( - vcat, (idx.idx for idx in idxs if idx.portion isa MTK.SciMLStructures.Initials); + vcat, (idx.idx for idx in idxs if idx.portion isa SciMLStructures.Initials); init = Union{Int, AbstractVector{Int}}[]) - disc_idxs = subset_idxs(idxs, MTK.SciMLStructures.Discrete(), oldbuf.discrete) - const_idxs = subset_idxs(idxs, MTK.SciMLStructures.Constants(), oldbuf.constant) - nn_idxs = subset_idxs(idxs, MTK.NONNUMERIC_PORTION, oldbuf.nonnumeric) + disc_idxs = subset_idxs(idxs, SciMLStructures.Discrete(), oldbuf.discrete) + const_idxs = subset_idxs(idxs, SciMLStructures.Constants(), oldbuf.constant) + nn_idxs = subset_idxs(idxs, NONNUMERIC_PORTION, oldbuf.nonnumeric) pullback = let idxs = idxs function remake_buffer_pullback(buf′) @@ -102,13 +96,11 @@ function ChainRulesCore.rrule( oldbuf′ = Tangent{typeof(oldbuf)}(; tunable, initials, discrete, constant, nonnumeric) idxs′ = NoTangent() - vals′ = map(i -> MTK._ducktyped_parameter_values(buf′, i), idxs) + vals′ = map(i -> _ducktyped_parameter_values(buf′, i), idxs) return f′, indp′, oldbuf′, idxs′, vals′ end end newbuf, pullback end -ChainRulesCore.@non_differentiable Base.getproperty(sys::MTK.AbstractSystem, x::Symbol) - -end +ChainRulesCore.@non_differentiable Base.getproperty(sys::AbstractSystem, x::Symbol) From 1dac3e7d92eaa49229aad4dcf450bcec62ccb04e Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 15 May 2025 17:43:23 +0530 Subject: [PATCH 1559/2176] fix: use `@ignore_derivatives` inside `update_initializeprob!` --- src/systems/problem_utils.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/systems/problem_utils.jl b/src/systems/problem_utils.jl index 48f03b07a4..e8ce6bd443 100644 --- a/src/systems/problem_utils.jl +++ b/src/systems/problem_utils.jl @@ -842,8 +842,8 @@ A function to be used as `update_initializeprob!` in `OverrideInitData`. Require `is_update_oop = Val(true)` to be passed to `update_initializeprob!`. """ function update_initializeprob!(initprob, prob) - p = get_scimlfn(prob).initialization_data.metadata.oop_reconstruct_u0_p.pgetter( - prob, initprob) + pgetter = ChainRulesCore.@ignore_derivatives get_scimlfn(prob).initialization_data.metadata.oop_reconstruct_u0_p.pgetter + p = pgetter(prob, initprob) return remake(initprob; p) end From 179f0bd958b479b8d76013b62f87ef8c324681e7 Mon Sep 17 00:00:00 2001 From: Christopher Rackauckas Date: Thu, 15 May 2025 20:09:28 +0000 Subject: [PATCH 1560/2176] Update Project.toml --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 302c74b1bc..423d73b90b 100644 --- a/Project.toml +++ b/Project.toml @@ -141,7 +141,7 @@ RecursiveArrayTools = "3.26" Reexport = "0.2, 1" RuntimeGeneratedFunctions = "0.5.9" SCCNonlinearSolve = "1.0.0" -SciMLBase = "2.89.1" +SciMLBase = "2.90.0" SciMLStructures = "1.7" Serialization = "1" Setfield = "0.7, 0.8, 1" From 25699664039ad1060f1a68e1556f6ed247f0a74d Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 16 May 2025 11:20:43 +0530 Subject: [PATCH 1561/2176] fix: add `struct IntervalNonlinearFunctionExpr` --- src/systems/nonlinear/nonlinearsystem.jl | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/systems/nonlinear/nonlinearsystem.jl b/src/systems/nonlinear/nonlinearsystem.jl index d0a12e22d4..cf1c46207f 100644 --- a/src/systems/nonlinear/nonlinearsystem.jl +++ b/src/systems/nonlinear/nonlinearsystem.jl @@ -495,6 +495,8 @@ function NonlinearFunctionExpr{iip}(sys::NonlinearSystem, dvs = unknowns(sys), !linenumbers ? Base.remove_linenums!(ex) : ex end +struct IntervalNonlinearFunctionExpr end + """ $(TYPEDSIGNATURES) From 0987a9f5cbecd211e1a441dda03b46dc93394b62 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 16 May 2025 13:23:54 +0530 Subject: [PATCH 1562/2176] ci: add SciMLSensitivity/Core8 to downstream CI --- .github/workflows/Downstream.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/Downstream.yml b/.github/workflows/Downstream.yml index 4f84e2c45d..6d79ee746a 100644 --- a/.github/workflows/Downstream.yml +++ b/.github/workflows/Downstream.yml @@ -38,6 +38,7 @@ jobs: - {user: SciML, repo: MethodOfLines.jl, group: 2D_Diffusion} - {user: SciML, repo: MethodOfLines.jl, group: DAE} - {user: SciML, repo: ModelingToolkitNeuralNets.jl, group: All} + - {user: SciML, repo: SciMLSensitivity.jl, group: Core8} - {user: Neuroblox, repo: Neuroblox.jl, group: All} steps: From 37f5320fcf250779071a578c18cdc438843b464e Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Sun, 18 May 2025 02:17:25 +0530 Subject: [PATCH 1563/2176] build: bump SciMLBase compat --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 423d73b90b..ed9981dfdd 100644 --- a/Project.toml +++ b/Project.toml @@ -141,7 +141,7 @@ RecursiveArrayTools = "3.26" Reexport = "0.2, 1" RuntimeGeneratedFunctions = "0.5.9" SCCNonlinearSolve = "1.0.0" -SciMLBase = "2.90.0" +SciMLBase = "2.91.1" SciMLStructures = "1.7" Serialization = "1" Setfield = "0.7, 0.8, 1" From 22151e7801d124fece04b653aa6b83b2e5cddf74 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 20 May 2025 17:37:02 +0530 Subject: [PATCH 1564/2176] fix: only promote tunables/initials in `promote_u0_p` if non-empty --- src/systems/nonlinear/initializesystem.jl | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/systems/nonlinear/initializesystem.jl b/src/systems/nonlinear/initializesystem.jl index c7a590197f..d2a988dc07 100644 --- a/src/systems/nonlinear/initializesystem.jl +++ b/src/systems/nonlinear/initializesystem.jl @@ -602,10 +602,14 @@ function promote_u0_p(u0, p::MTKParameters, t0) u0 = DiffEqBase.promote_u0(u0, p.tunable, t0) u0 = DiffEqBase.promote_u0(u0, p.initials, t0) - tunables = DiffEqBase.promote_u0(p.tunable, u0, t0) - initials = DiffEqBase.promote_u0(p.initials, u0, t0) - p = SciMLStructures.replace(SciMLStructures.Tunable(), p, tunables) - p = SciMLStructures.replace(SciMLStructures.Initials(), p, initials) + if !isempty(p.tunable) + tunables = DiffEqBase.promote_u0(p.tunable, u0, t0) + p = SciMLStructures.replace(SciMLStructures.Tunable(), p, tunables) + end + if !isempty(p.initials) + initials = DiffEqBase.promote_u0(p.initials, u0, t0) + p = SciMLStructures.replace(SciMLStructures.Initials(), p, initials) + end return u0, p end From 2c0976e21581e42e204cb3568e554d62dbbcc8d4 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 20 May 2025 17:38:00 +0530 Subject: [PATCH 1565/2176] refactor: reorder tests --- test/runtests.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/runtests.jl b/test/runtests.jl index 1b8b5e03db..86d69228ce 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -138,9 +138,9 @@ end activate_extensions_env() @safetestset "Dynamic Optimization Collocation Solvers" include("extensions/dynamic_optimization.jl") @safetestset "HomotopyContinuation Extension Test" include("extensions/homotopy_continuation.jl") - @safetestset "Auto Differentiation Test" include("extensions/ad.jl") @safetestset "LabelledArrays Test" include("labelledarrays.jl") @safetestset "BifurcationKit Extension Test" include("extensions/bifurcationkit.jl") @safetestset "InfiniteOpt Extension Test" include("extensions/test_infiniteopt.jl") + @safetestset "Auto Differentiation Test" include("extensions/ad.jl") end end From 6d9e49b0f8b777f4baa0b96ecfd2402d9f22d095 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 20 May 2025 23:09:59 +0530 Subject: [PATCH 1566/2176] fix: always construct unscalarized observed equations for array variables --- src/structural_transformation/symbolics_tearing.jl | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/src/structural_transformation/symbolics_tearing.jl b/src/structural_transformation/symbolics_tearing.jl index 548c7da519..d41cbf12d2 100644 --- a/src/structural_transformation/symbolics_tearing.jl +++ b/src/structural_transformation/symbolics_tearing.jl @@ -860,12 +860,9 @@ function cse_and_array_hacks(sys, obs, subeqs, unknowns, neweqs; cse = true, arr # map of array observed variable (unscalarized) to number of its # scalarized terms that appear in observed equations arr_obs_occurrences = Dict() - # to check if array variables occur in unscalarized form anywhere - all_vars = Set() for (i, eq) in enumerate(obs) lhs = eq.lhs rhs = eq.rhs - vars!(all_vars, rhs) # HACK 1 if cse && is_getindexed_array(rhs) @@ -920,7 +917,6 @@ function cse_and_array_hacks(sys, obs, subeqs, unknowns, neweqs; cse = true, arr tempvar; T = Symbolics.symtype(rhs_arr))) tempvar = setmetadata( tempvar, Symbolics.ArrayShapeCtx, Symbolics.shape(rhs_arr)) - vars!(all_vars, rhs_arr) tempeq = tempvar ~ rhs_arr rhs_to_tempvar[rhs_arr] = tempvar push!(obs, tempeq) @@ -946,18 +942,10 @@ function cse_and_array_hacks(sys, obs, subeqs, unknowns, neweqs; cse = true, arr cnt == 0 && continue arr_obs_occurrences[arg1] = cnt + 1 end - for eq in neweqs - vars!(all_vars, eq.rhs) - end - # also count unscalarized variables used in callbacks - for ev in Iterators.flatten((continuous_events(sys), discrete_events(sys))) - vars!(all_vars, ev) - end obs_arr_eqs = Equation[] for (arrvar, cnt) in arr_obs_occurrences cnt == length(arrvar) || continue - arrvar in all_vars || continue # firstindex returns 1 for multidimensional array symbolics firstind = first(eachindex(arrvar)) scal = [arrvar[i] for i in eachindex(arrvar)] From ee9ee8a2171a92ff2f9ccff214d94b1e83af4d02 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 20 May 2025 23:10:26 +0530 Subject: [PATCH 1567/2176] test: update tests to account for changes to array hack --- test/initializationsystem.jl | 4 ++-- test/reduction.jl | 2 +- test/structural_transformation/utils.jl | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/test/initializationsystem.jl b/test/initializationsystem.jl index df52291fbe..7c512d37af 100644 --- a/test/initializationsystem.jl +++ b/test/initializationsystem.jl @@ -1347,8 +1347,8 @@ end guesses = [p[1] => q, p[2] => 2q]) @test ModelingToolkit.is_parameter_solvable(p, Dict(), defaults(sys), guesses(sys)) prob = ODEProblem(sys, [x => 1.0], (0.0, 1.0), [q => 2.0]) - @test length(ModelingToolkit.observed(prob.f.initialization_data.initializeprob.f.sys)) == - 3 + initsys = prob.f.initialization_data.initializeprob.f.sys + @test length(ModelingToolkit.observed(initsys)) == 4 sol = solve(prob, Tsit5()) @test sol.ps[p] ≈ [2.0, 4.0] end diff --git a/test/reduction.jl b/test/reduction.jl index fa9029a652..adeb4005d7 100644 --- a/test/reduction.jl +++ b/test/reduction.jl @@ -176,7 +176,7 @@ A = reshape(1:(N^2), N, N) eqs = xs ~ A * xs @named sys′ = NonlinearSystem(eqs, [xs], []) sys = structural_simplify(sys′) -@test length(equations(sys)) == 3 && length(observed(sys)) == 2 +@test length(equations(sys)) == 3 && length(observed(sys)) == 3 # issue 958 @parameters k₁ k₂ k₋₁ E₀ diff --git a/test/structural_transformation/utils.jl b/test/structural_transformation/utils.jl index b5335ad6b1..67cf2f72a0 100644 --- a/test/structural_transformation/utils.jl +++ b/test/structural_transformation/utils.jl @@ -76,7 +76,7 @@ end end @mtkbuild sys = ODESystem([D(x) ~ y[1] + y[2], y ~ foo(x)], t) @test length(equations(sys)) == 1 - @test length(observed(sys)) == 3 + @test length(observed(sys)) == 4 prob = ODEProblem(sys, [x => 1.0], (0.0, 1.0), [foo => _tmp_fn2]) val[] = 0 @test_nowarn prob.f(prob.u0, prob.p, 0.0) From 518959810701abdd8ae2f250153b64b4b99a4077 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 21 May 2025 00:27:52 +0530 Subject: [PATCH 1568/2176] fix: fix `narrow_buffer_type` for `BlockedArray` of arrays --- src/systems/parameter_buffer.jl | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/systems/parameter_buffer.jl b/src/systems/parameter_buffer.jl index 7c34f67deb..3eb3063f77 100644 --- a/src/systems/parameter_buffer.jl +++ b/src/systems/parameter_buffer.jl @@ -198,6 +198,9 @@ function narrow_buffer_type( end function narrow_buffer_type(buffer::BlockedArray; container_type = typeof(parent(buffer))) + if eltype(buffer) <: AbstractArray + buffer = narrow_buffer_type.(buffer; container_type) + end type = Union{} for x in buffer type = promote_type(type, typeof(x)) From b1818ba0ea7ee438982c928dd902cdb0a808c4e7 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 21 May 2025 13:46:36 +0530 Subject: [PATCH 1569/2176] refactor: remove `container_type` kwarg of `MTKParameters`, use `p_constructor` --- src/systems/parameter_buffer.jl | 44 +++++++++++---------------------- test/mtkparameters.jl | 4 +-- 2 files changed, 17 insertions(+), 31 deletions(-) diff --git a/src/systems/parameter_buffer.jl b/src/systems/parameter_buffer.jl index 3eb3063f77..c3d2a0e831 100644 --- a/src/systems/parameter_buffer.jl +++ b/src/systems/parameter_buffer.jl @@ -29,13 +29,7 @@ the default behavior). function MTKParameters( sys::AbstractSystem, p, u0 = Dict(); tofloat = false, t0 = nothing, substitution_limit = 1000, floatT = nothing, - container_type = Vector, p_constructor = identity) - if !(container_type <: AbstractArray) - throw(ArgumentError(""" - `container_type` for `MTKParameters` must be a subtype of `AbstractArray`. Found \ - $container_type. - """)) - end + p_constructor = identity) ic = if has_index_cache(sys) && get_index_cache(sys) !== nothing get_index_cache(sys) else @@ -140,22 +134,19 @@ function MTKParameters( end end end - tunable_buffer = p_constructor(narrow_buffer_type(tunable_buffer; container_type)) + tunable_buffer = narrow_buffer_type(tunable_buffer; p_constructor) if isempty(tunable_buffer) tunable_buffer = SizedVector{0, Float64}() end - initials_buffer = p_constructor(narrow_buffer_type(initials_buffer; container_type)) + initials_buffer = narrow_buffer_type(initials_buffer; p_constructor) if isempty(initials_buffer) initials_buffer = SizedVector{0, Float64}() end - disc_buffer = p_constructor.(narrow_buffer_type.(disc_buffer; container_type)) - const_buffer = p_constructor.(narrow_buffer_type.(const_buffer; container_type)) + disc_buffer = narrow_buffer_type.(disc_buffer; p_constructor) + const_buffer = narrow_buffer_type.(const_buffer; p_constructor) # Don't narrow nonnumeric types if !isempty(nonnumeric_buffer) - nonnumeric_buffer = map(nonnumeric_buffer) do buf - p_constructor(SymbolicUtils.Code.create_array( - container_type, eltype(buf), Val(1), Val(length(buf)), buf...)) - end + nonnumeric_buffer = map(p_constructor, nonnumeric_buffer) end mtkps = MTKParameters{ @@ -172,17 +163,16 @@ function rebuild_with_caches(p::MTKParameters, cache_templates::BufferTemplate.. @set p.caches = buffers end -function narrow_buffer_type(buffer::AbstractArray; container_type = typeof(buffer)) +function narrow_buffer_type(buffer::AbstractArray; p_constructor = identity) type = Union{} for x in buffer type = promote_type(type, typeof(x)) end - return SymbolicUtils.Code.create_array( - container_type, type, Val(ndims(buffer)), Val(length(buffer)), buffer...) + return p_constructor(type.(buffer)) end function narrow_buffer_type( - buffer::AbstractArray{<:AbstractArray}; container_type = typeof(buffer)) + buffer::AbstractArray{<:AbstractArray}; p_constructor = identity) type = Union{} for arr in buffer for x in arr @@ -190,27 +180,23 @@ function narrow_buffer_type( end end buffer = map(buffer) do buf - SymbolicUtils.Code.create_array( - container_type, type, Val(ndims(buf)), Val(size(buf)), buf...) + p_constructor(type.(buf)) end - return SymbolicUtils.Code.create_array( - container_type, nothing, Val(ndims(buffer)), Val(size(buffer)), buffer...) + return p_constructor(buffer) end -function narrow_buffer_type(buffer::BlockedArray; container_type = typeof(parent(buffer))) +function narrow_buffer_type(buffer::BlockedArray; p_constructor = identity) if eltype(buffer) <: AbstractArray - buffer = narrow_buffer_type.(buffer; container_type) + buffer = narrow_buffer_type.(buffer; p_constructor) end type = Union{} for x in buffer type = promote_type(type, typeof(x)) end - tmp = SymbolicUtils.Code.create_array( - container_type, type, Val(ndims(buffer)), Val(size(buffer)), buffer...) + tmp = p_constructor(type.(buffer)) blocks = ntuple(Val(ndims(buffer))) do i bsizes = blocksizes(buffer, i) - SymbolicUtils.Code.create_array( - container_type, Int, Val(1), Val(length(bsizes)), bsizes...) + p_constructor(Int.(bsizes)) end return BlockedArray(tmp, blocks...) end diff --git a/test/mtkparameters.jl b/test/mtkparameters.jl index 9c857b6a55..b7acbb84a8 100644 --- a/test/mtkparameters.jl +++ b/test/mtkparameters.jl @@ -27,8 +27,8 @@ end @test getp(sys, a)(ps) == getp(sys, b)(ps) == getp(sys, c)(ps) == 0.0 @test getp(sys, d)(ps) isa Int -@testset "`container_type`" begin - ps2 = MTKParameters(sys, ivs; container_type = SVector) +@testset "`p_constructor`" begin + ps2 = MTKParameters(sys, ivs; p_constructor = x -> SArray{Tuple{size(x)...}}(x)) @test ps2.tunable isa SVector @test ps2.initials isa SVector @test ps2.discrete isa Tuple{<:BlockedVector{Float64, <:SVector}} From 01dd661057a75f25ebe77a79d2cfe017b2caf15f Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 21 May 2025 13:46:50 +0530 Subject: [PATCH 1570/2176] fix: fix unwrapping of views in MTKParameters reconstructor --- src/systems/problem_utils.jl | 34 +++++++++++++++++++++++++++++++--- 1 file changed, 31 insertions(+), 3 deletions(-) diff --git a/src/systems/problem_utils.jl b/src/systems/problem_utils.jl index e8ce6bd443..58173dd46c 100644 --- a/src/systems/problem_utils.jl +++ b/src/systems/problem_utils.jl @@ -643,6 +643,32 @@ function concrete_getu(indp, syms::AbstractVector) return Base.Fix1(reduce, vcat) ∘ getu(indp, split_syms) end +""" + $(TYPEDEF) + +A callable struct which applies `p_constructor` to possibly nested arrays. It also +ensures that views (including nested ones) are concretized. +""" +struct PConstructorApplicator{F} + p_constructor::F +end + +function (pca::PConstructorApplicator)(x::AbstractArray) + pca.p_constructor(x) +end + +function (pca::PConstructorApplicator{typeof(identity)})(x::SubArray) + collect(x) +end + +function (pca::PConstructorApplicator{typeof(identity)})(x::SubArray{<:AbstractArray}) + collect(pca.(x)) +end + +function (pca::PConstructorApplicator)(x::AbstractArray{<:AbstractArray}) + pca.p_constructor(pca.(x)) +end + """ $(TYPEDSIGNATURES) @@ -657,6 +683,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 = 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. syms = reorder_parameters( @@ -698,7 +725,7 @@ function get_mtkparameters_reconstructor(srcsys::AbstractSystem, dstsys::Abstrac else ic = get_index_cache(dstsys) blockarrsizes = Tuple(map(ic.discrete_buffer_sizes) do bufsizes - map(x -> x.length, bufsizes) + p_constructor(map(x -> x.length, bufsizes)) end) # discretes need to be blocked arrays # the `getu` returns a tuple of arrays corresponding to `p.discretes` @@ -706,7 +733,8 @@ function get_mtkparameters_reconstructor(srcsys::AbstractSystem, dstsys::Abstrac # `Base.Fix2(...)` does `BlockedArray.(tuple_of_arrs, blockarrsizes)` returning a # tuple of `BlockedArray`s Base.Fix2(Broadcast.BroadcastFunction(BlockedArray), blockarrsizes) ∘ - Base.Fix1(broadcast, p_constructor) ∘ getu(srcsys, syms[3]) + Base.Fix1(broadcast, p_constructor) ∘ + getu(srcsys, syms[3]) end rest_getters = map(Base.tail(Base.tail(Base.tail(syms)))) do buf if buf == () @@ -1307,7 +1335,7 @@ function process_SciMLProblem( if !(pType <: AbstractArray) pType = Array end - p = MTKParameters(sys, op; floatT = floatT, container_type = pType, p_constructor) + p = MTKParameters(sys, op; floatT = floatT, p_constructor) else p = p_constructor(better_varmap_to_vars(op, ps; tofloat, container_type = pType)) end From a6fa1cc5db5150815bebf120141838fbcfdd5a04 Mon Sep 17 00:00:00 2001 From: Christopher Rackauckas Date: Wed, 21 May 2025 10:15:16 +0000 Subject: [PATCH 1571/2176] Update Project.toml --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index ed9981dfdd..6119198c12 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "ModelingToolkit" uuid = "961ee093-0014-501f-94e3-6117800e7a78" authors = ["Yingbo Ma ", "Chris Rackauckas and contributors"] -version = "9.79.1" +version = "9.80.0" [deps] ADTypes = "47edcb42-4c32-4615-8424-f2b9edc5f35b" From 59832b89ab635f70718f506165d98ccdc505159a Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 21 May 2025 16:56:26 +0530 Subject: [PATCH 1572/2176] fix: fix solving out-of-place `split = false` ODEs --- src/systems/problem_utils.jl | 2 +- test/odesystem.jl | 25 +++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/systems/problem_utils.jl b/src/systems/problem_utils.jl index 58173dd46c..5ff00b4845 100644 --- a/src/systems/problem_utils.jl +++ b/src/systems/problem_utils.jl @@ -988,7 +988,7 @@ function (siu::SetInitialUnknowns)(p::MTKParameters, u0) return p end -function (siu::SetInitialUnknowns)(p::Vector, u0) +function (siu::SetInitialUnknowns)(p::AbstractVector, u0) if ArrayInterface.ismutable(p) siu.setter!(p, u0) else diff --git a/test/odesystem.jl b/test/odesystem.jl index 9219fca5fb..3220555e62 100644 --- a/test/odesystem.jl +++ b/test/odesystem.jl @@ -1730,3 +1730,28 @@ end @test obsfn_expr_oop isa Expr @test obsfn_expr_iip isa Expr end + +@testset "Solve with `split=false` static arrays" begin + @parameters σ ρ β + @variables x(t) y(t) z(t) + + eqs = [D(D(x)) ~ σ * (y - x), + D(y) ~ x * (ρ - z) - y, + D(z) ~ x * y - β * z] + + @mtkbuild sys=ODESystem(eqs, t) split=false + + u0 = SA[D(x) => 2.0f0, + x => 1.0f0, + y => 0.0f0, + z => 0.0f0] + + p = SA[σ => 28.0f0, + ρ => 10.0f0, + β => 8.0f0 / 3.0f0] + + tspan = (0.0f0, 100.0f0) + prob = ODEProblem{false}(sys, u0, tspan, p) + sol = solve(prob, Tsit5()) + @test SciMLBase.successful_retcode(sol) +end From 250e7be8fdb5d52b149d56a34ee568be87a1eb02 Mon Sep 17 00:00:00 2001 From: Christopher Rackauckas Date: Wed, 21 May 2025 12:39:43 +0000 Subject: [PATCH 1573/2176] Update Project.toml --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 6119198c12..305f29c691 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "ModelingToolkit" uuid = "961ee093-0014-501f-94e3-6117800e7a78" authors = ["Yingbo Ma ", "Chris Rackauckas and contributors"] -version = "9.80.0" +version = "9.80.1" [deps] ADTypes = "47edcb42-4c32-4615-8424-f2b9edc5f35b" From 452727e62743d7b19bdea31b3e93903d4011e868 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 14 Apr 2025 18:57:00 +0530 Subject: [PATCH 1574/2176] chore!: bump MAJOR version --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 305f29c691..74d5424533 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "ModelingToolkit" uuid = "961ee093-0014-501f-94e3-6117800e7a78" authors = ["Yingbo Ma ", "Chris Rackauckas and contributors"] -version = "9.80.1" +version = "10.0.0" [deps] ADTypes = "47edcb42-4c32-4615-8424-f2b9edc5f35b" From 08075cec9af518371591a86607cee4010a2dd3b0 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 14 Apr 2025 18:57:09 +0530 Subject: [PATCH 1575/2176] ci: run workflows on PR to v10 branch --- .github/workflows/Documentation.yml | 1 + .github/workflows/FormatCheck.yml | 1 + .github/workflows/Tests.yml | 1 + 3 files changed, 3 insertions(+) diff --git a/.github/workflows/Documentation.yml b/.github/workflows/Documentation.yml index 14bea532b3..e696b42380 100644 --- a/.github/workflows/Documentation.yml +++ b/.github/workflows/Documentation.yml @@ -4,6 +4,7 @@ on: push: branches: - master + - v10 tags: '*' pull_request: diff --git a/.github/workflows/FormatCheck.yml b/.github/workflows/FormatCheck.yml index 6185015c44..0d3052b969 100644 --- a/.github/workflows/FormatCheck.yml +++ b/.github/workflows/FormatCheck.yml @@ -4,6 +4,7 @@ on: push: branches: - 'master' + - v10 tags: '*' pull_request: diff --git a/.github/workflows/Tests.yml b/.github/workflows/Tests.yml index 52c5482970..a62f7f272d 100644 --- a/.github/workflows/Tests.yml +++ b/.github/workflows/Tests.yml @@ -5,6 +5,7 @@ on: branches: - master - 'release-' + - v10 paths-ignore: - 'docs/**' push: From 970b2f349e985d82b8c97a919b1cb2044716f9ad Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 14 Apr 2025 19:10:06 +0530 Subject: [PATCH 1576/2176] docs: bump MTK compat --- docs/Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/Project.toml b/docs/Project.toml index ca13773492..ae0d89dd3a 100644 --- a/docs/Project.toml +++ b/docs/Project.toml @@ -40,7 +40,7 @@ Documenter = "1" DynamicQuantities = "^0.11.2, 0.12, 1" FMI = "0.14" FMIZoo = "1" -ModelingToolkit = "8.33, 9" +ModelingToolkit = "10" ModelingToolkitStandardLibrary = "2.19" NonlinearSolve = "3, 4" Optim = "1.7" From 1b22715292d458eb20ce77943af8542486e4cc2c Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 14 Apr 2025 19:15:37 +0530 Subject: [PATCH 1577/2176] TEMP COMMIT: use branch of MTKStdlib --- Project.toml | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 74d5424533..ca4e79255a 100644 --- a/Project.toml +++ b/Project.toml @@ -176,6 +176,7 @@ Logging = "56ddb016-857b-54e1-b83d-db4d58db5568" ModelingToolkitStandardLibrary = "16a59e39-deab-5bd0-87e4-056b12336739" NonlinearSolve = "8913a72c-1f9b-4ce2-8d82-65094dcecaec" Optimization = "7f7a1694-90dd-40f0-9382-eb1efda571ba" +OptimizationBase = "bca83a33-5cc9-4baa-983d-23429ab6bcbb" OptimizationMOI = "fd9f6733-72f4-499f-8506-86b2bdd0dea1" OptimizationOptimJL = "36348300-93cb-4f02-beb5-3c3902f8871e" OrdinaryDiffEq = "1dea7af3-3e70-54e6-95c3-0bf5283fa5ed" @@ -195,5 +196,10 @@ StochasticDiffEq = "789caeaf-c7a9-5a7d-9973-96adeb23e2a0" Sundials = "c3572dad-4567-51f8-b174-8c6c989267f4" Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" +[sources] +ModelingToolkitStandardLibrary = { url = "https://github.com/SciML/ModelingToolkitStandardLibrary.jl/", rev = "mtk-v10" } +OptimizationBase = { url = "https://github.com/AayushSabharwal/OptimizationBase.jl", rev = "as/mtk-v10" } +OptimizationMOI = { url = "https://github.com/AayushSabharwal/Optimization.jl", subdir = "lib/OptimizationMOI", rev = "as/mtk-v10" } + [targets] -test = ["AmplNLWriter", "BenchmarkTools", "BoundaryValueDiffEqMIRK", "BoundaryValueDiffEqAscher", "ControlSystemsBase", "DataInterpolations", "DelayDiffEq", "NonlinearSolve", "ForwardDiff", "Ipopt", "Ipopt_jll", "ModelingToolkitStandardLibrary", "Optimization", "OptimizationOptimJL", "OptimizationMOI", "OrdinaryDiffEq", "OrdinaryDiffEqCore", "OrdinaryDiffEqDefault", "REPL", "Random", "ReferenceTests", "SafeTestsets", "StableRNGs", "Statistics", "SteadyStateDiffEq", "Test", "StochasticDiffEq", "Sundials", "StochasticDelayDiffEq", "Pkg", "JET", "OrdinaryDiffEqNonlinearSolve", "Logging"] +test = ["AmplNLWriter", "BenchmarkTools", "BoundaryValueDiffEqMIRK", "BoundaryValueDiffEqAscher", "ControlSystemsBase", "DataInterpolations", "DelayDiffEq", "NonlinearSolve", "ForwardDiff", "Ipopt", "Ipopt_jll", "ModelingToolkitStandardLibrary", "Optimization", "OptimizationOptimJL", "OptimizationMOI", "OrdinaryDiffEq", "OrdinaryDiffEqCore", "OrdinaryDiffEqDefault", "REPL", "Random", "ReferenceTests", "SafeTestsets", "StableRNGs", "Statistics", "SteadyStateDiffEq", "Test", "StochasticDiffEq", "Sundials", "StochasticDelayDiffEq", "Pkg", "JET", "OrdinaryDiffEqNonlinearSolve", "Logging", "OptimizationBase"] From 3f9f5b7c5cbb83322708205139022f7dcc1ae0dd Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 3 Apr 2025 17:07:53 +0530 Subject: [PATCH 1578/2176] feat: make `@named` always wrap arguments in `ParentScope` --- src/systems/abstractsystem.jl | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index 6c00806b7d..a7e2c24ab1 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -2400,11 +2400,7 @@ function default_to_parentscope(v) uv = unwrap(v) uv isa Symbolic || return v apply_to_variables(v) do sym - if !hasmetadata(uv, SymScope) - ParentScope(sym) - else - sym - end + ParentScope(sym) end end From 3e665745fbd93aad332e665ff5d76a615ec7fb3d Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 14 Apr 2025 21:38:27 +0530 Subject: [PATCH 1579/2176] test: test `@named` always wrapping in `ParentScope` --- test/odesystem.jl | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/test/odesystem.jl b/test/odesystem.jl index 3220555e62..b824d18a24 100644 --- a/test/odesystem.jl +++ b/test/odesystem.jl @@ -9,6 +9,7 @@ using SymbolicUtils: issym using ForwardDiff using ModelingToolkit: value using ModelingToolkit: t_nounits as t, D_nounits as D +using Symbolics: unwrap # Define some variables @parameters σ ρ β @@ -1755,3 +1756,26 @@ end sol = solve(prob, Tsit5()) @test SciMLBase.successful_retcode(sol) end + +@testset "`@named` always wraps in `ParentScope`" begin + function SysA(; name, var1) + @variables x(t) + scope = ModelingToolkit.getmetadata(unwrap(var1), ModelingToolkit.SymScope, nothing) + @test scope isa ParentScope + @test scope.parent isa ParentScope + @test scope.parent.parent isa LocalScope + return ODESystem(D(x) ~ var1, t; name) + end + function SysB(; name, var1) + @variables x(t) + @named subsys = SysA(; var1) + return ODESystem(D(x) ~ x, t; systems = [subsys], name) + end + function SysC(; name) + @variables x(t) + @named subsys = SysB(; var1 = x) + return ODESystem(D(x) ~ x, t; systems = [subsys], name) + end + @mtkbuild sys = SysC() + @test length(unknowns(sys)) == 3 +end From 94f1c051324455b7c373aecdc2f4b77181ae7cca Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 14 Apr 2025 19:47:53 +0530 Subject: [PATCH 1580/2176] refactor: remove `DelayParentScope` --- docs/src/basics/Composition.md | 20 +++------ src/ModelingToolkit.jl | 2 +- src/systems/abstractsystem.jl | 58 ------------------------ src/utils.jl | 2 - test/jumpsystem.jl | 22 +++++----- test/variable_scope.jl | 80 +++++++++++++++------------------- 6 files changed, 53 insertions(+), 131 deletions(-) diff --git a/docs/src/basics/Composition.md b/docs/src/basics/Composition.md index 2e5d4be831..d3de71d696 100644 --- a/docs/src/basics/Composition.md +++ b/docs/src/basics/Composition.md @@ -135,16 +135,14 @@ sys.y = u * 1.1 In a hierarchical system, variables of the subsystem get namespaced by the name of the system they are in. This prevents naming clashes, but also enforces that every unknown and parameter is local to the subsystem it is used in. In some cases it might be desirable to have variables and parameters that are shared between subsystems, or even global. This can be accomplished as follows. ```julia -@parameters a b c d e f +@parameters a b c d # a is a local variable b = ParentScope(b) # b is a variable that belongs to one level up in the hierarchy c = ParentScope(ParentScope(c)) # ParentScope can be nested -d = DelayParentScope(d) # skips one level before applying ParentScope -e = DelayParentScope(e, 2) # second argument allows skipping N levels -f = GlobalScope(f) +d = GlobalScope(d) -p = [a, b, c, d, e, f] +p = [a, b, c, d] level0 = ODESystem(Equation[], t, [], p; name = :level0) level1 = ODESystem(Equation[], t, [], []; name = :level1) ∘ level0 @@ -152,25 +150,19 @@ parameters(level1) #level0₊a #b #c -#level0₊d -#level0₊e -#f +#d level2 = ODESystem(Equation[], t, [], []; name = :level2) ∘ level1 parameters(level2) #level1₊level0₊a #level1₊b #c -#level0₊d -#level1₊level0₊e -#f +#d level3 = ODESystem(Equation[], t, [], []; name = :level3) ∘ level2 parameters(level3) #level2₊level1₊level0₊a #level2₊level1₊b #level2₊c -#level2₊level0₊d -#level1₊level0₊e -#f +#d ``` ## Structural Simplify diff --git a/src/ModelingToolkit.jl b/src/ModelingToolkit.jl index 9ed6b9994a..049c1815e4 100644 --- a/src/ModelingToolkit.jl +++ b/src/ModelingToolkit.jl @@ -301,7 +301,7 @@ export PDESystem export Differential, expand_derivatives, @derivatives export Equation, ConstrainedEquation export Term, Sym -export SymScope, LocalScope, ParentScope, DelayParentScope, GlobalScope +export SymScope, LocalScope, ParentScope, GlobalScope export independent_variable, equations, controls, observed, full_equations export initialization_equations, guesses, defaults, parameter_dependencies, hierarchy export structural_simplify, expand_connections, linearize, linearization_function, diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index a7e2c24ab1..aa850edc67 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -1190,55 +1190,6 @@ function ParentScope(sym::Union{Num, Symbolic, Symbolics.Arr{Num}}) end end -""" - $(TYPEDEF) - -Denotes that a variable belongs to a system that is at least `N + 1` levels up in the -hierarchy from the system whose equations it is involved in. It is namespaced by the -first `N` parents and not namespaced by the `N+1`th parent in the hierarchy. The scope -of the variable after this point is given by `parent`. - -In other words, this scope delays applying `ParentScope` by `N` levels, and applies -`LocalScope` in the meantime. - -# Fields - -$(TYPEDFIELDS) -""" -struct DelayParentScope <: SymScope - parent::SymScope - N::Int -end - -""" - $(TYPEDSIGNATURES) - -Apply `DelayParentScope` to `sym`, with a delay of `N` and `parent` being `LocalScope`. -""" -function DelayParentScope(sym::Union{Num, Symbolic, Symbolics.Arr{Num}}, N) - Base.depwarn( - "`DelayParentScope` is deprecated and will be removed soon", :DelayParentScope) - apply_to_variables(sym) do sym - if iscall(sym) && operation(sym) == getindex - args = arguments(sym) - a1 = setmetadata(args[1], SymScope, - DelayParentScope(getmetadata(value(args[1]), SymScope, LocalScope()), N)) - maketerm(typeof(sym), operation(sym), [a1, args[2:end]...], - metadata(sym)) - else - setmetadata(sym, SymScope, - DelayParentScope(getmetadata(value(sym), SymScope, LocalScope()), N)) - end - end -end - -""" - $(TYPEDSIGNATURES) - -Apply `DelayParentScope` to `sym`, with a delay of `1` and `parent` being `LocalScope`. -""" -DelayParentScope(sym::Union{Num, Symbolic, Symbolics.Arr{Num}}) = DelayParentScope(sym, 1) - """ $(TYPEDEF) @@ -1291,15 +1242,6 @@ function renamespace(sys, x) rename(x, renamespace(getname(sys), getname(x)))::T elseif scope isa ParentScope setmetadata(x, SymScope, scope.parent)::T - elseif scope isa DelayParentScope - if scope.N > 0 - x = setmetadata(x, SymScope, - DelayParentScope(scope.parent, scope.N - 1)) - rename(x, renamespace(getname(sys), getname(x)))::T - else - #rename(x, renamespace(getname(sys), getname(x))) - setmetadata(x, SymScope, scope.parent)::T - end else # GlobalScope x::T end diff --git a/src/utils.jl b/src/utils.jl index 90431a7749..a2034ec58a 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -646,8 +646,6 @@ function check_scope_depth(scope, depth) return depth == 0 elseif scope isa ParentScope return depth > 0 && check_scope_depth(scope.parent, depth - 1) - elseif scope isa DelayParentScope - return depth >= scope.N && check_scope_depth(scope.parent, depth - scope.N - 1) elseif scope isa GlobalScope return depth == -1 end diff --git a/test/jumpsystem.jl b/test/jumpsystem.jl index 1ee0408758..6c96055270 100644 --- a/test/jumpsystem.jl +++ b/test/jumpsystem.jl @@ -378,22 +378,20 @@ end # scoping tests let - @variables x1(t) x2(t) x3(t) x4(t) x5(t) + @variables x1(t) x2(t) x3(t) x4(t) x2 = ParentScope(x2) x3 = ParentScope(ParentScope(x3)) - x4 = DelayParentScope(x4) - x5 = GlobalScope(x5) - @parameters p1 p2 p3 p4 p5 + x4 = GlobalScope(x4) + @parameters p1 p2 p3 p4 p2 = ParentScope(p2) p3 = ParentScope(ParentScope(p3)) - p4 = DelayParentScope(p4) - p5 = GlobalScope(p5) + p4 = GlobalScope(p4) j1 = ConstantRateJump(p1, [x1 ~ x1 + 1]) j2 = MassActionJump(p2, [x2 => 1], [x3 => -1]) j3 = VariableRateJump(p3, [x3 ~ x3 + 1, x4 ~ x4 + 1]) - j4 = MassActionJump(p4 * p5, [x1 => 1, x5 => 1], [x1 => -1, x5 => -1, x2 => 1]) - @named js = JumpSystem([j1, j2, j3, j4], t, [x1, x2, x3, x4, x5], [p1, p2, p3, p4, p5]) + j4 = MassActionJump(p4 * p4, [x1 => 1, x4 => 1], [x1 => -1, x4 => -1, x2 => 1]) + @named js = JumpSystem([j1, j2, j3, j4], t, [x1, x2, x3, x4], [p1, p2, p3, p4]) us = Set() ps = Set() @@ -414,13 +412,13 @@ let empty!.((us, ps)) MT.collect_scoped_vars!(us, ps, js, iv; depth = 2) - @test issetequal(us, [x3, x4]) - @test issetequal(ps, [p3, p4]) + @test issetequal(us, [x3]) + @test issetequal(ps, [p3]) empty!.((us, ps)) MT.collect_scoped_vars!(us, ps, js, iv; depth = -1) - @test issetequal(us, [x5]) - @test issetequal(ps, [p5]) + @test issetequal(us, [x4]) + @test issetequal(ps, [p4]) end # PDMP test diff --git a/test/variable_scope.jl b/test/variable_scope.jl index bd1d3cb0cf..59647bf441 100644 --- a/test/variable_scope.jl +++ b/test/variable_scope.jl @@ -51,13 +51,11 @@ end @test renamed([:foo :bar :baz], c) == Symbol("foo₊c") @test renamed([:foo :bar :baz], d) == :d -@parameters a b c d e f +@parameters a b c d p = [a ParentScope(b) ParentScope(ParentScope(c)) - DelayParentScope(d) - DelayParentScope(e, 2) - GlobalScope(f)] + GlobalScope(d)] level0 = ODESystem(Equation[], t, [], p; name = :level0) level1 = ODESystem(Equation[], t, [], []; name = :level1) ∘ level0 @@ -69,9 +67,7 @@ ps = ModelingToolkit.getname.(parameters(level3)) @test isequal(ps[1], :level2₊level1₊level0₊a) @test isequal(ps[2], :level2₊level1₊b) @test isequal(ps[3], :level2₊c) -@test isequal(ps[4], :level2₊level0₊d) -@test isequal(ps[5], :level1₊level0₊e) -@test isequal(ps[6], :f) +@test isequal(ps[4], :d) # Issue@2252 # Tests from PR#2354 @@ -102,40 +98,36 @@ defs = ModelingToolkit.defaults(bar) @test defs[bar.p] == 2 @test isequal(defs[bar.foo.p], bar.p) -# Issue#3101 -@variables x1(t) x2(t) x3(t) x4(t) x5(t) -x2 = ParentScope(x2) -x3 = ParentScope(ParentScope(x3)) -x4 = DelayParentScope(x4) -x5 = GlobalScope(x5) -@parameters p1 p2 p3 p4 p5 -p2 = ParentScope(p2) -p3 = ParentScope(ParentScope(p3)) -p4 = DelayParentScope(p4) -p5 = GlobalScope(p5) - -@named sys1 = ODESystem([D(x1) ~ p1, D(x2) ~ p2, D(x3) ~ p3, D(x4) ~ p4, D(x5) ~ p5], t) -@test isequal(x1, only(unknowns(sys1))) -@test isequal(p1, only(parameters(sys1))) -@named sys2 = ODESystem(Equation[], t; systems = [sys1]) -@test length(unknowns(sys2)) == 2 -@test any(isequal(x2), unknowns(sys2)) -@test length(parameters(sys2)) == 2 -@test any(isequal(p2), parameters(sys2)) -@named sys3 = ODESystem(Equation[], t) -sys3 = sys3 ∘ sys2 -@test length(unknowns(sys3)) == 4 -@test any(isequal(x3), unknowns(sys3)) -@test any(isequal(ModelingToolkit.renamespace(sys1, x4)), unknowns(sys3)) -@test length(parameters(sys3)) == 4 -@test any(isequal(p3), parameters(sys3)) -@test any(isequal(ModelingToolkit.renamespace(sys1, p4)), parameters(sys3)) -sys4 = complete(sys3) -@test length(unknowns(sys3)) == 4 -@test length(parameters(sys4)) == 5 -@test any(isequal(p5), parameters(sys4)) -sys5 = structural_simplify(sys3) -@test length(unknowns(sys5)) == 5 -@test any(isequal(x5), unknowns(sys5)) -@test length(parameters(sys5)) == 5 -@test any(isequal(p5), parameters(sys5)) +@testset "Issue#3101" begin + @variables x1(t) x2(t) x3(t) x4(t) + x2 = ParentScope(x2) + x3 = ParentScope(ParentScope(x3)) + x4 = GlobalScope(x4) + @parameters p1 p2 p3 p4 + p2 = ParentScope(p2) + p3 = ParentScope(ParentScope(p3)) + p4 = GlobalScope(p4) + + @named sys1 = ODESystem([D(x1) ~ p1, D(x2) ~ p2, D(x3) ~ p3, D(x4) ~ p4], t) + @test isequal(x1, only(unknowns(sys1))) + @test isequal(p1, only(parameters(sys1))) + @named sys2 = ODESystem(Equation[], t; systems = [sys1]) + @test length(unknowns(sys2)) == 2 + @test any(isequal(x2), unknowns(sys2)) + @test length(parameters(sys2)) == 2 + @test any(isequal(p2), parameters(sys2)) + @named sys3 = ODESystem(Equation[], t) + sys3 = sys3 ∘ sys2 + @test length(unknowns(sys3)) == 3 + @test any(isequal(x3), unknowns(sys3)) + @test length(parameters(sys3)) == 3 + @test any(isequal(p3), parameters(sys3)) + sys4 = complete(sys3) + @test length(unknowns(sys4)) == 3 + @test length(parameters(sys4)) == 4 + sys5 = structural_simplify(sys3) + @test length(unknowns(sys5)) == 4 + @test any(isequal(x4), unknowns(sys5)) + @test length(parameters(sys5)) == 4 + @test any(isequal(p4), parameters(sys5)) +end From 9bf44a78b5bcd22f1e30efcf61807dd1255c7eb9 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 14 Apr 2025 20:15:04 +0530 Subject: [PATCH 1581/2176] refactor: remove `time_varying_as_func` --- src/inputoutput.jl | 2 +- src/systems/abstractsystem.jl | 14 -------------- src/systems/callbacks.jl | 8 ++++---- src/systems/codegen_utils.jl | 11 ----------- 4 files changed, 5 insertions(+), 30 deletions(-) diff --git a/src/inputoutput.jl b/src/inputoutput.jl index 739df14ae0..8ee71077f8 100644 --- a/src/inputoutput.jl +++ b/src/inputoutput.jl @@ -221,7 +221,7 @@ function generate_control_function(sys::AbstractODESystem, inputs = unbound_inpu inputs = setdiff(inputs, disturbance_inputs) # ps = [ps; disturbance_inputs] end - inputs = map(x -> time_varying_as_func(value(x), sys), inputs) + inputs = map(value, inputs) disturbance_inputs = unwrap.(disturbance_inputs) eqs = [eq for eq in full_equations(sys)] diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index aa850edc67..5fdb7b4e25 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -1791,20 +1791,6 @@ function isaffine(sys::AbstractSystem) all(isaffine(r, unknowns(sys)) for r in rhs) end -function time_varying_as_func(x, sys::AbstractTimeDependentSystem) - # if something is not x(t) (the current unknown) - # but is `x(t-1)` or something like that, pass in `x` as a callable function rather - # than pass in a value in place of x(t). - # - # This is done by just making `x` the argument of the function. - if iscall(x) && - issym(operation(x)) && - !(length(arguments(x)) == 1 && isequal(arguments(x)[1], get_iv(sys))) - return operation(x) - end - return x -end - """ $(SIGNATURES) diff --git a/src/systems/callbacks.jl b/src/systems/callbacks.jl index 07809bf611..7d542d9bd0 100644 --- a/src/systems/callbacks.jl +++ b/src/systems/callbacks.jl @@ -598,8 +598,8 @@ Notes """ function compile_condition(cb::SymbolicDiscreteCallback, sys, dvs, ps; expression = Val{true}, eval_expression = false, eval_module = @__MODULE__, kwargs...) - u = map(x -> time_varying_as_func(value(x), sys), dvs) - p = map.(x -> time_varying_as_func(value(x), sys), reorder_parameters(sys, ps)) + u = map(value, dvs) + p = map.(value, reorder_parameters(sys, ps)) t = get_iv(sys) condit = condition(cb) cs = collect_constants(condit) @@ -685,8 +685,8 @@ function compile_affect(eqs::Vector{Equation}, cb, sys, dvs, ps; outputidxs = no _ps = ps ps = reorder_parameters(sys, ps) if checkvars - u = map(x -> time_varying_as_func(value(x), sys), dvs) - p = map.(x -> time_varying_as_func(value(x), sys), ps) + u = map(value, dvs) + p = map.(value, ps) else u = dvs p = ps diff --git a/src/systems/codegen_utils.jl b/src/systems/codegen_utils.jl index a3fe53b95d..bedfdbcc37 100644 --- a/src/systems/codegen_utils.jl +++ b/src/systems/codegen_utils.jl @@ -179,17 +179,6 @@ function build_function_wrapper(sys::AbstractSystem, expr, args...; p_start = 2, args = ntuple(Val(length(args))) do i arg = args[i] - # for time-dependent systems, all arguments are passed through `time_varying_as_func` - # TODO: This is legacy behavior and a candidate for removal in v10 since we have callable - # parameters now. - if is_time_dependent(sys) - arg = if symbolic_type(arg) == NotSymbolic() - arg isa AbstractArray ? - map(x -> time_varying_as_func(unwrap(x), sys), arg) : arg - else - time_varying_as_func(unwrap(arg), sys) - end - end # Make sure to use the proper names for arguments if symbolic_type(arg) == NotSymbolic() && arg isa AbstractArray DestructuredArgs(arg, generated_argument_name(i); create_bindings) From 6e902e0da9d93d3c72e3d2678aa61034313a2dec Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 14 Apr 2025 20:37:56 +0530 Subject: [PATCH 1582/2176] test: update tests with removed `time_varying_as_func` --- test/odesystem.jl | 14 +------------- 1 file changed, 1 insertion(+), 13 deletions(-) diff --git a/test/odesystem.jl b/test/odesystem.jl index b824d18a24..469b77611a 100644 --- a/test/odesystem.jl +++ b/test/odesystem.jl @@ -136,19 +136,7 @@ du = zeros(3) tgrad_iip(du, u, p, t) @test du == [0.0, -u[2], 0.0] -@parameters σ′(t - 1) -eqs = [D(x) ~ σ′ * (y - x), - D(y) ~ x * (ρ - z) - y, - D(z) ~ x * y - β * z * κ] -@named de = ODESystem(eqs, t) -test_diffeq_inference("global iv-varying", de, t, (x, y, z), (σ′, ρ, β)) - -f = generate_function(de, [x, y, z], [σ′, ρ, β], expression = Val{false})[2] -du = [0.0, 0.0, 0.0] -f(du, [1.0, 2.0, 3.0], [x -> x + 7, 2, 3], 5.0) -@test du ≈ [11, -3, -7] - -@parameters σ(..) +@parameters (σ::Function)(..) eqs = [D(x) ~ σ(t - 1) * (y - x), D(y) ~ x * (ρ - z) - y, D(z) ~ x * y - β * z * κ] From e050e5e48bcad02ba315e8fa9e155611a5a2ef4d Mon Sep 17 00:00:00 2001 From: vyudu Date: Fri, 18 Apr 2025 18:02:18 -0400 Subject: [PATCH 1583/2176] refactor: remove input_idxs output --- src/inputoutput.jl | 20 +++++++------- src/systems/clock_inference.jl | 13 +++++++++ src/systems/systems.jl | 19 ++++++++----- src/systems/systemstructure.jl | 49 ++++++++++++++-------------------- 4 files changed, 55 insertions(+), 46 deletions(-) diff --git a/src/inputoutput.jl b/src/inputoutput.jl index 8ee71077f8..7e40fe4e26 100644 --- a/src/inputoutput.jl +++ b/src/inputoutput.jl @@ -180,9 +180,6 @@ The return values also include the chosen state-realization (the remaining unkno If `disturbance_inputs` is an array of variables, the generated dynamics function will preserve any state and dynamics associated with disturbance inputs, but the disturbance inputs themselves will (by default) not be included as inputs to the generated function. The use case for this is to generate dynamics for state observers that estimate the influence of unmeasured disturbances, and thus require unknown variables for the disturbance model, but without disturbance inputs since the disturbances are not available for measurement. To add an input argument corresponding to the disturbance inputs, either include the disturbance inputs among the control inputs, or set `disturbance_argument=true`, in which case an additional input argument `w` is added to the generated function `(x,u,p,t,w)->rhs`. -!!! note "Un-simplified system" - This function expects `sys` to be un-simplified, i.e., `structural_simplify` or `@mtkbuild` should not be called on the system before passing it into this function. `generate_control_function` calls a special version of `structural_simplify` internally. - # Example ``` @@ -202,17 +199,17 @@ function generate_control_function(sys::AbstractODESystem, inputs = unbound_inpu eval_expression = false, eval_module = @__MODULE__, kwargs...) - isempty(inputs) && @warn("No unbound inputs were found in system.") + # Remove this when the ControlFunction gets merged. + if !iscomplete(sys) + error("A completed `ODESystem` is required. Call `complete` or `structural_simplify` on the system before creating the control function.") + end + isempty(inputs) && @warn("No unbound inputs were found in system.") if disturbance_inputs !== nothing # add to inputs for the purposes of io processing inputs = [inputs; disturbance_inputs] end - if !iscomplete(sys) - sys, _ = io_preprocessing(sys, inputs, []; simplify, kwargs...) - end - dvs = unknowns(sys) ps = parameters(sys; initial_parameters = true) ps = setdiff(ps, inputs) @@ -259,8 +256,11 @@ function generate_control_function(sys::AbstractODESystem, inputs = unbound_inpu (; f = (f, f), dvs, ps, io_sys = sys) end -function inputs_to_parameters!(state::TransformationState, io) - check_bound = io === nothing +""" +Turn input variables into parameters of the system. +""" +function inputs_to_parameters!(state::TransformationState, inputsyms) + check_bound = inputsyms === nothing @unpack structure, fullvars, sys = state @unpack var_to_diff, graph, solvable_graph = structure @assert solvable_graph === nothing diff --git a/src/systems/clock_inference.jl b/src/systems/clock_inference.jl index b535773061..42fe28f7c7 100644 --- a/src/systems/clock_inference.jl +++ b/src/systems/clock_inference.jl @@ -1,7 +1,11 @@ struct ClockInference{S} + """Tearing state.""" ts::S + """The time domain (discrete clock, continuous) of each equation.""" eq_domain::Vector{TimeDomain} + """The output time domain (discrete clock, continuous) of each variable.""" var_domain::Vector{TimeDomain} + """The set of variables with concrete domains.""" inferred::BitSet end @@ -67,6 +71,9 @@ function substitute_sample_time(ex, dt) end end +""" +Update the equation-to-time domain mapping by inferring the time domain from the variables. +""" function infer_clocks!(ci::ClockInference) @unpack ts, eq_domain, var_domain, inferred = ci @unpack var_to_diff, graph = ts.structure @@ -132,6 +139,9 @@ function is_time_domain_conversion(v) input_timedomain(o) != output_timedomain(o) end +""" +For multi-clock systems, create a separate system for each clock in the system, along with associated equations. Return the updated tearing state, and the sets of clocked variables associated with each time domain. +""" function split_system(ci::ClockInference{S}) where {S} @unpack ts, eq_domain, var_domain, inferred = ci fullvars = get_fullvars(ts) @@ -143,11 +153,14 @@ function split_system(ci::ClockInference{S}) where {S} cid_to_eq = Vector{Int}[] var_to_cid = Vector{Int}(undef, ndsts(graph)) cid_to_var = Vector{Int}[] + # cid_counter = number of clocks cid_counter = Ref(0) for (i, d) in enumerate(eq_domain) cid = let cid_counter = cid_counter, id_to_clock = id_to_clock, continuous_id = continuous_id + # Fill the clock_to_id dict as you go, + # ContinuousClock() => 1, ... get!(clock_to_id, d) do cid = (cid_counter[] += 1) push!(id_to_clock, d) diff --git a/src/systems/systems.jl b/src/systems/systems.jl index 52f93afb9b..f3d1dba12c 100644 --- a/src/systems/systems.jl +++ b/src/systems/systems.jl @@ -27,12 +27,15 @@ topological sort of the observed equations in `sys`. + `sort_eqs=true` controls whether equations are sorted lexicographically before simplification or not. """ function structural_simplify( - sys::AbstractSystem, io = nothing; additional_passes = [], simplify = false, split = true, + sys::AbstractSystem; additional_passes = [], simplify = false, split = true, allow_symbolic = false, allow_parameter = true, conservative = false, fully_determined = true, + inputs = nothing, outputs = nothing, + disturbance_inputs = nothing, kwargs...) isscheduled(sys) && throw(RepeatedStructuralSimplificationError()) - newsys′ = __structural_simplify(sys, io; simplify, + newsys′ = __structural_simplify(sys; simplify, allow_symbolic, allow_parameter, conservative, fully_determined, + inputs, outputs, disturbance_inputs, kwargs...) if newsys′ isa Tuple @assert length(newsys′) == 2 @@ -70,8 +73,9 @@ function __structural_simplify(sys::SDESystem, args...; kwargs...) return __structural_simplify(ODESystem(sys), args...; kwargs...) end -function __structural_simplify( - sys::AbstractSystem, io = nothing; simplify = false, sort_eqs = true, +function __structural_simplify(sys::AbstractSystem; simplify = false, + inputs = Any[], outputs = Any[], + disturbance_inputs = Any[], kwargs...) sys = expand_connections(sys) state = TearingState(sys; sort_eqs) @@ -90,7 +94,8 @@ function __structural_simplify( end end if isempty(brown_vars) - return structural_simplify!(state, io; simplify, kwargs...) + return structural_simplify!( + state; simplify, inputs, outputs, disturbance_inputs, kwargs...) else Is = Int[] Js = Int[] @@ -122,8 +127,8 @@ function __structural_simplify( for (i, v) in enumerate(fullvars) if !iszero(new_idxs[i]) && invview(var_to_diff)[i] === nothing] - # TODO: IO is not handled. - ode_sys = structural_simplify(sys, io; simplify, kwargs...) + ode_sys = structural_simplify( + sys; simplify, inputs, outputs, disturbance_inputs, kwargs...) eqs = equations(ode_sys) sorted_g_rows = zeros(Num, length(eqs), size(g, 2)) for (i, eq) in enumerate(eqs) diff --git a/src/systems/systemstructure.jl b/src/systems/systemstructure.jl index e0feb0d34d..81bf605805 100644 --- a/src/systems/systemstructure.jl +++ b/src/systems/systemstructure.jl @@ -657,29 +657,21 @@ function Base.show(io::IO, mime::MIME"text/plain", ms::MatchedSystemStructure) printstyled(io, " SelectedState") end -# TODO: clean up -function merge_io(io, inputs) - isempty(inputs) && return io - if io === nothing - io = (inputs, []) - else - io = ([inputs; io[1]], io[2]) - end - return io -end - -function structural_simplify!(state::TearingState, io = nothing; simplify = false, +function structural_simplify!(state::TearingState; simplify = false, check_consistency = true, fully_determined = true, warn_initialize_determined = true, + inputs = nothing, outputs = nothing, + disturbance_inputs = nothing, kwargs...) if state.sys isa ODESystem ci = ModelingToolkit.ClockInference(state) ci = ModelingToolkit.infer_clocks!(ci) time_domains = merge(Dict(state.fullvars .=> ci.var_domain), Dict(default_toterm.(state.fullvars) .=> ci.var_domain)) - tss, inputs, continuous_id, id_to_clock = ModelingToolkit.split_system(ci) - cont_io = merge_io(io, inputs[continuous_id]) - sys, input_idxs = _structural_simplify!(tss[continuous_id], cont_io; simplify, + tss, clocked_inputs, continuous_id, id_to_clock = ModelingToolkit.split_system(ci) + cont_inputs = [inputs; clocked_inputs[continuous_id]] + sys = _structural_simplify!(tss[continuous_id]; simplify, check_consistency, fully_determined, + cont_inputs, outputs, disturbance_inputs, kwargs...) if length(tss) > 1 if continuous_id > 0 @@ -695,8 +687,9 @@ function structural_simplify!(state::TearingState, io = nothing; simplify = fals discrete_subsystems[i] = sys continue end - dist_io = merge_io(io, inputs[i]) - ss, = _structural_simplify!(state, dist_io; simplify, check_consistency, + disc_inputs = [inputs; clocked_inputs[i]] + ss, = _structural_simplify!(state; simplify, check_consistency, + inputs = disc_inputs, outputs, disturbance_inputs, fully_determined, kwargs...) append!(appended_parameters, inputs[i], unknowns(ss)) discrete_subsystems[i] = ss @@ -713,31 +706,29 @@ function structural_simplify!(state::TearingState, io = nothing; simplify = fals for sym in get_ps(sys)] @set! sys.ps = ps else - sys, input_idxs = _structural_simplify!(state, io; simplify, check_consistency, + sys = _structural_simplify!(state; simplify, check_consistency, + inputs, outputs, disturbance_inputs, fully_determined, kwargs...) end - has_io = io !== nothing - return has_io ? (sys, input_idxs) : sys + return sys end -function _structural_simplify!(state::TearingState, io; simplify = false, +function _structural_simplify!(state::TearingState; simplify = false, check_consistency = true, fully_determined = true, warn_initialize_determined = false, dummy_derivative = true, + inputs = nothing, outputs = nothing, + disturbance_inputs = nothing, kwargs...) if fully_determined isa Bool check_consistency &= fully_determined else check_consistency = true end - has_io = io !== nothing + has_io = inputs !== nothing || outputs !== nothing orig_inputs = Set() if has_io - ModelingToolkit.markio!(state, orig_inputs, io...) - end - if io !== nothing - state, input_idxs = ModelingToolkit.inputs_to_parameters!(state, io) - else - input_idxs = 0:-1 # Empty range + ModelingToolkit.markio!(state, orig_inputs, inputs, outputs) + state = ModelingToolkit.inputs_to_parameters!(state, inputs) end sys, mm = ModelingToolkit.alias_elimination!(state; kwargs...) if check_consistency @@ -761,5 +752,5 @@ function _structural_simplify!(state::TearingState, io; simplify = false, fullunknowns = [observables(sys); unknowns(sys)] @set! sys.observed = ModelingToolkit.topsort_equations(observed(sys), fullunknowns) - ModelingToolkit.invalidate_cache!(sys), input_idxs + ModelingToolkit.invalidate_cache!(sys) end From 8c0fa25e9bdfea15b90af41374b1d4c459e5ad3b Mon Sep 17 00:00:00 2001 From: vyudu Date: Mon, 21 Apr 2025 14:42:16 -0400 Subject: [PATCH 1584/2176] refactor: use new `structural_simplify` in linearization --- src/linearization.jl | 41 ++++++++++++++++++++++------------- src/systems/abstractsystem.jl | 15 ------------- 2 files changed, 26 insertions(+), 30 deletions(-) diff --git a/src/linearization.jl b/src/linearization.jl index b30d275818..199d51b823 100644 --- a/src/linearization.jl +++ b/src/linearization.jl @@ -58,9 +58,8 @@ function linearization_function(sys::AbstractSystem, inputs, outputs = mapreduce(vcat, outputs; init = []) do var symbolic_type(var) == ArraySymbolic() ? collect(var) : [var] end - ssys, diff_idxs, alge_idxs, input_idxs = io_preprocessing(sys, inputs, outputs; - simplify, - kwargs...) + ssys = structural_simplify(sys; inputs, outputs, simplify, kwargs...) + diff_idxs, alge_idxs = eq_idxs(ssys) if zero_dummy_der dummyder = setdiff(unknowns(ssys), unknowns(sys)) defs = Dict(x => 0.0 for x in dummyder) @@ -87,9 +86,9 @@ function linearization_function(sys::AbstractSystem, inputs, p = parameter_values(prob) t0 = current_time(prob) - inputvals = [p[idx] for idx in input_idxs] + inputvals = [prob.ps[i] for i in inputs] - hp_fun = let fun = h, setter = setp_oop(sys, input_idxs) + hp_fun = let fun = h, setter = setp_oop(sys, inputs) function hpf(du, input, u, p, t) p = setter(p, input) fun(du, u, p, t) @@ -113,7 +112,7 @@ function linearization_function(sys::AbstractSystem, inputs, # observed function is a `GeneratedFunctionWrapper` with iip component h_jac = PreparedJacobian{true}(h, similar(prob.u0, size(outputs)), autodiff, prob.u0, DI.Constant(p), DI.Constant(t0)) - pf_fun = let fun = prob.f, setter = setp_oop(sys, input_idxs) + pf_fun = let fun = prob.f, setter = setp_oop(sys, inputs) function pff(du, input, u, p, t) p = setter(p, input) SciMLBase.ParamJacobianWrapper(fun, t, u)(du, p) @@ -127,12 +126,24 @@ function linearization_function(sys::AbstractSystem, inputs, end lin_fun = LinearizationFunction( - diff_idxs, alge_idxs, input_idxs, length(unknowns(sys)), + diff_idxs, alge_idxs, length(unknowns(sys)), prob, h, u0 === nothing ? nothing : similar(u0), uf_jac, h_jac, pf_jac, hp_jac, initializealg, initialization_kwargs) return lin_fun, sys end +function eq_idxs(sys::AbstractSystem) + eqs = equations(sys) + alg_start_idx = findfirst(!isdiffeq, eqs) + if alg_start_idx === nothing + alg_start_idx = length(eqs) + 1 + end + diff_idxs = 1:(alg_start_idx - 1) + alge_idxs = alg_start_idx:length(eqs) + + diff_idxs, alge_idxs +end + """ $(TYPEDEF) @@ -192,7 +203,7 @@ A callable struct which linearizes a system. $(TYPEDFIELDS) """ struct LinearizationFunction{ - DI <: AbstractVector{Int}, AI <: AbstractVector{Int}, II, P <: ODEProblem, + DI <: AbstractVector{Int}, AI <: AbstractVector{Int}, I, P <: ODEProblem, H, C, J1, J2, J3, J4, IA <: SciMLBase.DAEInitializationAlgorithm, IK} """ The indexes of differential equations in the linearized system. @@ -206,7 +217,7 @@ struct LinearizationFunction{ The indexes of parameters in the linearized system which represent input variables. """ - input_idxs::II + inputs::I """ The number of unknowns in the linearized system. """ @@ -281,6 +292,7 @@ function (linfun::LinearizationFunction)(u, p, t) end fun = linfun.prob.f + input_vals = [linfun.prob.ps[i] for i in linfun.inputs] if u !== nothing # Handle systems without unknowns linfun.num_states == length(u) || error("Number of unknown variables ($(linfun.num_states)) does not match the number of input unknowns ($(length(u)))") @@ -294,15 +306,15 @@ function (linfun::LinearizationFunction)(u, p, t) end fg_xz = linfun.uf_jac(u, DI.Constant(p), DI.Constant(t)) h_xz = linfun.h_jac(u, DI.Constant(p), DI.Constant(t)) - fg_u = linfun.pf_jac([p[idx] for idx in linfun.input_idxs], + fg_u = linfun.pf_jac(input_vals, DI.Constant(u), DI.Constant(p), DI.Constant(t)) else linfun.num_states == 0 || error("Number of unknown variables (0) does not match the number of input unknowns ($(length(u)))") fg_xz = zeros(0, 0) - h_xz = fg_u = zeros(0, length(linfun.input_idxs)) + h_xz = fg_u = zeros(0, length(linfun.inputs)) end - h_u = linfun.hp_jac([p[idx] for idx in linfun.input_idxs], + h_u = linfun.hp_jac(input_vals, DI.Constant(u), DI.Constant(p), DI.Constant(t)) (f_x = fg_xz[linfun.diff_idxs, linfun.diff_idxs], f_z = fg_xz[linfun.diff_idxs, linfun.alge_idxs], @@ -487,9 +499,8 @@ function linearize_symbolic(sys::AbstractSystem, inputs, outputs; simplify = false, allow_input_derivatives = false, eval_expression = false, eval_module = @__MODULE__, kwargs...) - sys, diff_idxs, alge_idxs, input_idxs = io_preprocessing( - sys, inputs, outputs; simplify, - kwargs...) + sys = structural_simplify(sys; inputs, outputs, simplify, kwargs...) + diff_idxs, alge_idxs = eq_idxs(sys) sts = unknowns(sys) t = get_iv(sys) ps = parameters(sys; initial_parameters = true) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index 5fdb7b4e25..1da2a8ff73 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -2486,21 +2486,6 @@ function eliminate_constants(sys::AbstractSystem) return sys end -function io_preprocessing(sys::AbstractSystem, inputs, - outputs; simplify = false, kwargs...) - sys, input_idxs = structural_simplify(sys, (inputs, outputs); simplify, kwargs...) - - eqs = equations(sys) - alg_start_idx = findfirst(!isdiffeq, eqs) - if alg_start_idx === nothing - alg_start_idx = length(eqs) + 1 - end - diff_idxs = 1:(alg_start_idx - 1) - alge_idxs = alg_start_idx:length(eqs) - - sys, diff_idxs, alge_idxs, input_idxs -end - @latexrecipe function f(sys::AbstractSystem) return latexify(equations(sys)) end From 082cd5d90b3a946fd934eca25464c0df8cb17273 Mon Sep 17 00:00:00 2001 From: vyudu Date: Mon, 21 Apr 2025 23:44:47 -0400 Subject: [PATCH 1585/2176] fix: fix linearization tests --- src/inputoutput.jl | 16 +++++++--------- src/linearization.jl | 2 +- src/systems/diffeqs/odesystem.jl | 10 +++++----- src/systems/systems.jl | 4 ++-- src/systems/systemstructure.jl | 10 +++++----- test/downstream/linearize.jl | 6 +++--- 6 files changed, 23 insertions(+), 25 deletions(-) diff --git a/src/inputoutput.jl b/src/inputoutput.jl index 7e40fe4e26..5d1c106fe2 100644 --- a/src/inputoutput.jl +++ b/src/inputoutput.jl @@ -163,7 +163,7 @@ has_var(ex, x) = x ∈ Set(get_variables(ex)) (f_oop, f_ip), x_sym, p_sym, io_sys = generate_control_function( sys::AbstractODESystem, inputs = unbound_inputs(sys), - disturbance_inputs = nothing; + disturbance_inputs = Any[]; implicit_dae = false, simplify = false, ) @@ -289,7 +289,7 @@ function inputs_to_parameters!(state::TransformationState, inputsyms) push!(new_fullvars, v) end end - ninputs == 0 && return (state, 1:0) + ninputs == 0 && return state nvars = ndsts(graph) - ninputs new_graph = BipartiteGraph(nsrcs(graph), nvars, Val(false)) @@ -318,14 +318,13 @@ function inputs_to_parameters!(state::TransformationState, inputsyms) @set! sys.unknowns = setdiff(unknowns(sys), keys(input_to_parameters)) ps = parameters(sys) - if io !== nothing - inputs, = io + if inputsyms !== nothing # Change order of new parameters to correspond to user-provided order in argument `inputs` d = Dict{Any, Int}() for (i, inp) in enumerate(new_parameters) d[inp] = i end - permutation = [d[i] for i in inputs] + permutation = [d[i] for i in inputsyms] new_parameters = new_parameters[permutation] end @@ -334,8 +333,7 @@ function inputs_to_parameters!(state::TransformationState, inputsyms) @set! state.sys = sys @set! state.fullvars = new_fullvars @set! state.structure = structure - base_params = length(ps) - return state, (base_params + 1):(base_params + length(new_parameters)) # (1:length(new_parameters)) .+ base_params + return state end """ @@ -361,7 +359,7 @@ function get_disturbance_system(dist::DisturbanceModel{<:ODESystem}) end """ - (f_oop, f_ip), augmented_sys, dvs, p = add_input_disturbance(sys, dist::DisturbanceModel, inputs = nothing) + (f_oop, f_ip), augmented_sys, dvs, p = add_input_disturbance(sys, dist::DisturbanceModel, inputs = Any[]) Add a model of an unmeasured disturbance to `sys`. The disturbance model is an instance of [`DisturbanceModel`](@ref). @@ -410,7 +408,7 @@ model_outputs = [model.inertia1.w, model.inertia2.w, model.inertia1.phi, model.i `f_oop` will have an extra state corresponding to the integrator in the disturbance model. This state will not be affected by any input, but will affect the dynamics from where it enters, in this case it will affect additively from `model.torque.tau.u`. """ -function add_input_disturbance(sys, dist::DisturbanceModel, inputs = nothing; kwargs...) +function add_input_disturbance(sys, dist::DisturbanceModel, inputs = Any[]; kwargs...) t = get_iv(sys) @variables d(t)=0 [disturbance = true] @variables u(t)=0 [input = true] # New system input diff --git a/src/linearization.jl b/src/linearization.jl index 199d51b823..35b905e569 100644 --- a/src/linearization.jl +++ b/src/linearization.jl @@ -126,7 +126,7 @@ function linearization_function(sys::AbstractSystem, inputs, end lin_fun = LinearizationFunction( - diff_idxs, alge_idxs, length(unknowns(sys)), + diff_idxs, alge_idxs, inputs, length(unknowns(sys)), prob, h, u0 === nothing ? nothing : similar(u0), uf_jac, h_jac, pf_jac, hp_jac, initializealg, initialization_kwargs) return lin_fun, sys diff --git a/src/systems/diffeqs/odesystem.jl b/src/systems/diffeqs/odesystem.jl index 4a13d7ccf1..18208cb3af 100644 --- a/src/systems/diffeqs/odesystem.jl +++ b/src/systems/diffeqs/odesystem.jl @@ -470,7 +470,7 @@ Generates a function that computes the observed value(s) `ts` in the system `sys - `eval_expression = false`: If true and `expression = false`, evaluates the returned function in the module `eval_module` - `output_type = Array` the type of the array generated by a out-of-place vector-valued function - `param_only = false` if true, only allow the generated function to access system parameters -- `inputs = nothing` additinoal symbolic variables that should be provided to the generated function +- `inputs = Any[]` additional symbolic variables that should be provided to the generated function - `checkbounds = true` checks bounds if true when destructuring parameters - `op = Operator` sets the recursion terminator for the walk done by `vars` to identify the variables that appear in `ts`. See the documentation for `vars` for more detail. - `throw = true` if true, throw an error when generating a function for `ts` that reference variables that do not exist. @@ -500,8 +500,8 @@ For example, a function `g(op, unknowns, p..., inputs, t)` will be the in-place an array of inputs `inputs` is given, and `param_only` is false for a time-dependent system. """ function build_explicit_observed_function(sys, ts; - inputs = nothing, - disturbance_inputs = nothing, + inputs = Any[], + disturbance_inputs = Any[], disturbance_argument = false, expression = false, eval_expression = false, @@ -574,13 +574,13 @@ function build_explicit_observed_function(sys, ts; else (unknowns(sys),) end - if inputs === nothing + if isempty(inputs) inputs = () else ps = setdiff(ps, inputs) # Inputs have been converted to parameters by io_preprocessing, remove those from the parameter list inputs = (inputs,) end - if disturbance_inputs !== nothing + if !isempty(disturbance_inputs) # Disturbance inputs may or may not be included as inputs, depending on disturbance_argument ps = setdiff(ps, disturbance_inputs) end diff --git a/src/systems/systems.jl b/src/systems/systems.jl index f3d1dba12c..270a3f567f 100644 --- a/src/systems/systems.jl +++ b/src/systems/systems.jl @@ -29,8 +29,8 @@ topological sort of the observed equations in `sys`. function structural_simplify( sys::AbstractSystem; additional_passes = [], simplify = false, split = true, allow_symbolic = false, allow_parameter = true, conservative = false, fully_determined = true, - inputs = nothing, outputs = nothing, - disturbance_inputs = nothing, + inputs = Any[], outputs = Any[], + disturbance_inputs = Any[], kwargs...) isscheduled(sys) && throw(RepeatedStructuralSimplificationError()) newsys′ = __structural_simplify(sys; simplify, diff --git a/src/systems/systemstructure.jl b/src/systems/systemstructure.jl index 81bf605805..9f848314cd 100644 --- a/src/systems/systemstructure.jl +++ b/src/systems/systemstructure.jl @@ -659,8 +659,8 @@ end function structural_simplify!(state::TearingState; simplify = false, check_consistency = true, fully_determined = true, warn_initialize_determined = true, - inputs = nothing, outputs = nothing, - disturbance_inputs = nothing, + inputs = Any[], outputs = Any[], + disturbance_inputs = Any[], kwargs...) if state.sys isa ODESystem ci = ModelingToolkit.ClockInference(state) @@ -671,7 +671,7 @@ function structural_simplify!(state::TearingState; simplify = false, cont_inputs = [inputs; clocked_inputs[continuous_id]] sys = _structural_simplify!(tss[continuous_id]; simplify, check_consistency, fully_determined, - cont_inputs, outputs, disturbance_inputs, + inputs = cont_inputs, outputs, disturbance_inputs, kwargs...) if length(tss) > 1 if continuous_id > 0 @@ -716,8 +716,8 @@ end function _structural_simplify!(state::TearingState; simplify = false, check_consistency = true, fully_determined = true, warn_initialize_determined = false, dummy_derivative = true, - inputs = nothing, outputs = nothing, - disturbance_inputs = nothing, + inputs = Any[], outputs = Any[], + disturbance_inputs = Any[], kwargs...) if fully_determined isa Bool check_consistency &= fully_determined diff --git a/test/downstream/linearize.jl b/test/downstream/linearize.jl index d82bd696dd..20a9d317e8 100644 --- a/test/downstream/linearize.jl +++ b/test/downstream/linearize.jl @@ -87,10 +87,10 @@ connections = [f.y ~ c.r # filtered reference to controller reference @named cl = ODESystem(connections, t, systems = [f, c, p]) -lsys0, ssys = linearize(cl, [f.u], [p.x]) +lsys0, ssys = linearize(cl) desired_order = [f.x, p.x] lsys = ModelingToolkit.reorder_unknowns(lsys0, unknowns(ssys), desired_order) -lsys1, ssys = linearize(cl, [f.u], [p.x]; autodiff = AutoFiniteDiff()) +lsys1, ssys = linearize(cl; autodiff = AutoFiniteDiff()) lsys2 = ModelingToolkit.reorder_unknowns(lsys1, unknowns(ssys), desired_order) @test lsys.A == lsys2.A == [-2 0; 1 -2] @@ -266,7 +266,7 @@ closed_loop = ODESystem(connections, t, systems = [model, pid, filt, sensor, r, filt.xd => 0.0 ]) -@test_nowarn linearize(closed_loop, :r, :y; warn_empty_op = false) +@test_nowarn linearize(closed_loop; warn_empty_op = false) # https://discourse.julialang.org/t/mtk-change-in-linearize/115760/3 @mtkmodel Tank_noi begin From 3d988620abbe9be932f41d5eb77c713822a23565 Mon Sep 17 00:00:00 2001 From: vyudu Date: Thu, 24 Apr 2025 15:37:58 -0400 Subject: [PATCH 1586/2176] test: test updates --- src/systems/systems.jl | 4 +--- test/clock.jl | 7 ++++--- test/code_generation.jl | 2 +- test/input_output_handling.jl | 8 ++++---- test/odesystem.jl | 6 +++--- test/reduction.jl | 2 +- 6 files changed, 14 insertions(+), 15 deletions(-) diff --git a/src/systems/systems.jl b/src/systems/systems.jl index 270a3f567f..a326703262 100644 --- a/src/systems/systems.jl +++ b/src/systems/systems.jl @@ -17,13 +17,11 @@ $(SIGNATURES) Structurally simplify algebraic equations in a system and compute the topological sort of the observed equations in `sys`. -### Optional Arguments: -+ optional argument `io` may take a tuple `(inputs, outputs)`. This will convert all `inputs` to parameters and allow them to be unconnected, i.e., simplification will allow models where `n_unknowns = n_equations - n_inputs`. - ### Optional Keyword Arguments: + When `simplify=true`, the `simplify` function will be applied during the tearing process. + `allow_symbolic=false`, `allow_parameter=true`, and `conservative=false` limit the coefficient types during tearing. In particular, `conservative=true` limits tearing to only solve for trivial linear systems where the coefficient has the absolute value of ``1``. + `fully_determined=true` controls whether or not an error will be thrown if the number of equations don't match the number of inputs, outputs, and equations. ++ `inputs`, `outputs` and `disturbance_inputs` are passed as keyword arguments.` All inputs` get converted to parameters and are allowed to be unconnected, allowing models where `n_unknowns = n_equations - n_inputs`. + `sort_eqs=true` controls whether equations are sorted lexicographically before simplification or not. """ function structural_simplify( diff --git a/test/clock.jl b/test/clock.jl index c6051a52a8..c4c64dbf90 100644 --- a/test/clock.jl +++ b/test/clock.jl @@ -65,10 +65,11 @@ By inference: ci, varmap = infer_clocks(sys) eqmap = ci.eq_domain tss, inputs, continuous_id = ModelingToolkit.split_system(deepcopy(ci)) -sss, = ModelingToolkit._structural_simplify!( - deepcopy(tss[continuous_id]), (inputs[continuous_id], ())) +sss = ModelingToolkit._structural_simplify!( + deepcopy(tss[continuous_id]), inputs = inputs[continuous_id], outputs = []) @test equations(sss) == [D(x) ~ u - x] -sss, = ModelingToolkit._structural_simplify!(deepcopy(tss[1]), (inputs[1], ())) +sss = ModelingToolkit._structural_simplify!( + deepcopy(tss[1]), inputs = inputs[1], outputs = []) @test isempty(equations(sss)) d = Clock(dt) k = ShiftIndex(d) diff --git a/test/code_generation.jl b/test/code_generation.jl index cf3d660b81..3ef5ac3e11 100644 --- a/test/code_generation.jl +++ b/test/code_generation.jl @@ -70,7 +70,7 @@ end @parameters p[1:2] (f::Function)(..) @named sys = ODESystem( [D(x[0]) ~ p[1] * x[0] + x[2], D(x[1]) ~ p[2] * f(x) + x[2]], t) - sys, = structural_simplify(sys, ([x[2]], [])) + sys, = structural_simplify(sys, inputs = [x[2]], outputs = []) @test is_parameter(sys, x[2]) prob = ODEProblem(sys, [x[0] => 1.0, x[1] => 1.0], (0.0, 1.0), [p => ones(2), f => sum, x[2] => 2.0]) diff --git a/test/input_output_handling.jl b/test/input_output_handling.jl index 67f60a04cd..5ebfe1ee3b 100644 --- a/test/input_output_handling.jl +++ b/test/input_output_handling.jl @@ -50,7 +50,7 @@ end @test !is_bound(sys31, sys1.v[2]) # simplification turns input variables into parameters -ssys, _ = structural_simplify(sys, ([u], [])) +ssys = structural_simplify(sys, inputs = [u], outputs = []) @test ModelingToolkit.isparameter(unbound_inputs(ssys)[]) @test !is_bound(ssys, u) @test u ∈ Set(unbound_inputs(ssys)) @@ -281,7 +281,7 @@ i = findfirst(isequal(u[1]), out) @variables x(t) u(t) [input = true] eqs = [D(x) ~ u] @named sys = ODESystem(eqs, t) -@test_nowarn structural_simplify(sys, ([u], [])) +@test_nowarn structural_simplify(sys, inputs = [u], outputs = []) #= ## Disturbance input handling @@ -366,9 +366,9 @@ eqs = [D(y₁) ~ -k₁ * y₁ + k₃ * y₂ * y₃ + u1 @named sys = ODESystem(eqs, t) m_inputs = [u[1], u[2]] m_outputs = [y₂] -sys_simp, input_idxs = structural_simplify(sys, (; inputs = m_inputs, outputs = m_outputs)) +sys_simp = structural_simplify(sys, inputs = m_inputs, outputs = m_outputs) @test isequal(unknowns(sys_simp), collect(x[1:2])) -@test length(input_idxs) == 2 +@test length(inputs(sys_simp)) == 2 # https://github.com/SciML/ModelingToolkit.jl/issues/1577 @named c = Constant(; k = 2) diff --git a/test/odesystem.jl b/test/odesystem.jl index 469b77611a..2dbf4b2161 100644 --- a/test/odesystem.jl +++ b/test/odesystem.jl @@ -1283,11 +1283,11 @@ end @named sys = ODESystem( [D(u) ~ (sum(u) + sum(x) + sum(p) + sum(o)) * x, o ~ prod(u) * x], t, [u..., x..., o...], [p...]) - sys1, = structural_simplify(sys, ([x...], [])) + sys1 = structural_simplify(sys, inputs = [x...], outputs = []) fn1, = ModelingToolkit.generate_function(sys1; expression = Val{false}) ps = MTKParameters(sys1, [x => 2ones(2), p => 3ones(2, 2)]) @test_nowarn fn1(ones(4), ps, 4.0) - sys2, = structural_simplify(sys, ([x...], []); split = false) + sys2 = structural_simplify(sys, inputs = [x...], outputs = [], split = false) fn2, = ModelingToolkit.generate_function(sys2; expression = Val{false}) ps = zeros(8) setp(sys2, x)(ps, 2ones(2)) @@ -1396,7 +1396,7 @@ end o[2] ~ sum(p) * sum(x)] @named sys = ODESystem(eqs, t, [u..., x..., o], [p...]) - sys1, = structural_simplify(sys, ([x...], [o...]), split = false) + sys1 = structural_simplify(sys, inputs = [x...], outputs = [o...], split = false) @test_nowarn ModelingToolkit.build_explicit_observed_function(sys1, u; inputs = [x...]) diff --git a/test/reduction.jl b/test/reduction.jl index adeb4005d7..81cfe8473b 100644 --- a/test/reduction.jl +++ b/test/reduction.jl @@ -233,7 +233,7 @@ eqs = [D(x) ~ σ * (y - x) u ~ z + a] lorenz1 = ODESystem(eqs, t, name = :lorenz1) -lorenz1_reduced, _ = structural_simplify(lorenz1, ([z], [])) +lorenz1_reduced, _ = structural_simplify(lorenz1, inputs = [z], outputs = []) @test z in Set(parameters(lorenz1_reduced)) # #2064 From c66e604829889dc57ea537d3e289518bde482c62 Mon Sep 17 00:00:00 2001 From: vyudu Date: Thu, 24 Apr 2025 17:35:48 -0400 Subject: [PATCH 1587/2176] fix input output tests --- src/inputoutput.jl | 13 +++++++------ src/linearization.jl | 9 ++++++++- src/systems/systemstructure.jl | 4 ++-- src/variables.jl | 2 ++ test/input_output_handling.jl | 28 ++++++++++++++++------------ test/reduction.jl | 2 +- 6 files changed, 36 insertions(+), 22 deletions(-) diff --git a/src/inputoutput.jl b/src/inputoutput.jl index 5d1c106fe2..2093277fe4 100644 --- a/src/inputoutput.jl +++ b/src/inputoutput.jl @@ -198,10 +198,10 @@ function generate_control_function(sys::AbstractODESystem, inputs = unbound_inpu simplify = false, eval_expression = false, eval_module = @__MODULE__, + check_simplified = true, kwargs...) - # Remove this when the ControlFunction gets merged. - if !iscomplete(sys) + if check_simplified && !iscomplete(sys) error("A completed `ODESystem` is required. Call `complete` or `structural_simplify` on the system before creating the control function.") end isempty(inputs) && @warn("No unbound inputs were found in system.") @@ -259,7 +259,7 @@ end """ Turn input variables into parameters of the system. """ -function inputs_to_parameters!(state::TransformationState, inputsyms) +function inputs_to_parameters!(state::TransformationState, inputsyms; is_disturbance = false) check_bound = inputsyms === nothing @unpack structure, fullvars, sys = state @unpack var_to_diff, graph, solvable_graph = structure @@ -414,7 +414,7 @@ function add_input_disturbance(sys, dist::DisturbanceModel, inputs = Any[]; kwar @variables u(t)=0 [input = true] # New system input dsys = get_disturbance_system(dist) - if inputs === nothing + if isempty(inputs) all_inputs = [u] else i = findfirst(isequal(dist.input), inputs) @@ -429,8 +429,9 @@ function add_input_disturbance(sys, dist::DisturbanceModel, inputs = Any[]; kwar dist.input ~ u + dsys.output.u[1]] augmented_sys = ODESystem(eqs, t, systems = [dsys], name = gensym(:outer)) augmented_sys = extend(augmented_sys, sys) + ssys = structural_simplify(augmented_sys, inputs = all_inputs, disturbance_inputs = [d]) - f, dvs, p, io_sys = generate_control_function(augmented_sys, all_inputs, - [d]; kwargs...) + f, dvs, p, io_sys = generate_control_function(ssys, all_inputs, + [d]; check_simplified = false, kwargs...) f, augmented_sys, dvs, p, io_sys end diff --git a/src/linearization.jl b/src/linearization.jl index 35b905e569..ddc71623c1 100644 --- a/src/linearization.jl +++ b/src/linearization.jl @@ -561,10 +561,11 @@ function linearize_symbolic(sys::AbstractSystem, inputs, (; A, B, C, D, f_x, f_z, g_x, g_z, f_u, g_u, h_x, h_z, h_u), sys end -function markio!(state, orig_inputs, inputs, outputs; check = true) +function markio!(state, orig_inputs, inputs, outputs, disturbances; check = true) fullvars = get_fullvars(state) inputset = Dict{Any, Bool}(i => false for i in inputs) outputset = Dict{Any, Bool}(o => false for o in outputs) + disturbanceset = Dict{Any, Bool}(d => false for d in disturbances) for (i, v) in enumerate(fullvars) if v in keys(inputset) if v in keys(outputset) @@ -586,6 +587,12 @@ function markio!(state, orig_inputs, inputs, outputs; check = true) v = setio(v, false, false) fullvars[i] = v end + + if v in keys(disturbanceset) + v = setio(v, true, false) + v = setdisturbance(v, true) + fullvars[i] = v + end end if check ikeys = keys(filter(!last, inputset)) diff --git a/src/systems/systemstructure.jl b/src/systems/systemstructure.jl index 9f848314cd..4daf4a29cc 100644 --- a/src/systems/systemstructure.jl +++ b/src/systems/systemstructure.jl @@ -727,8 +727,8 @@ function _structural_simplify!(state::TearingState; simplify = false, has_io = inputs !== nothing || outputs !== nothing orig_inputs = Set() if has_io - ModelingToolkit.markio!(state, orig_inputs, inputs, outputs) - state = ModelingToolkit.inputs_to_parameters!(state, inputs) + ModelingToolkit.markio!(state, orig_inputs, inputs, outputs, disturbance_inputs) + state = ModelingToolkit.inputs_to_parameters!(state, [inputs; disturbance_inputs]) end sys, mm = ModelingToolkit.alias_elimination!(state; kwargs...) if check_consistency diff --git a/src/variables.jl b/src/variables.jl index 510bd5c28d..eb2e54a268 100644 --- a/src/variables.jl +++ b/src/variables.jl @@ -354,6 +354,8 @@ function isdisturbance(x) Symbolics.getmetadata(x, VariableDisturbance, false) end +setdisturbance(x, v) = setmetadata(x, VariableDisturbance, v) + function disturbances(sys) [filter(isdisturbance, unknowns(sys)); filter(isdisturbance, parameters(sys))] end diff --git a/test/input_output_handling.jl b/test/input_output_handling.jl index 5ebfe1ee3b..68936b52bd 100644 --- a/test/input_output_handling.jl +++ b/test/input_output_handling.jl @@ -7,10 +7,10 @@ using ModelingToolkit: t_nounits as t, D_nounits as D @variables xx(t) some_input(t) [input = true] eqs = [D(xx) ~ some_input] @named model = ODESystem(eqs, t) -@test_throws ExtraVariablesSystemException structural_simplify(model, ((), ())) +@test_throws ExtraVariablesSystemException structural_simplify(model) if VERSION >= v"1.8" err = "In particular, the unset input(s) are:\n some_input(t)" - @test_throws err structural_simplify(model, ((), ())) + @test_throws err structural_simplify(model) end # Test input handling @@ -88,7 +88,7 @@ fsys4 = flatten(sys4) @variables x(t) y(t) [output = true] @test isoutput(y) @named sys = ODESystem([D(x) ~ -x, y ~ x], t) # both y and x are unbound -syss = structural_simplify(sys) # This makes y an observed variable +syss = structural_simplify(sys, outputs = [y]) # This makes y an observed variable @named sys2 = ODESystem([D(x) ~ -sys.x, y ~ sys.y], t, systems = [sys]) @@ -106,7 +106,7 @@ syss = structural_simplify(sys) # This makes y an observed variable @test isequal(unbound_outputs(sys2), [y]) @test isequal(bound_outputs(sys2), [sys.y]) -syss = structural_simplify(sys2) +syss = structural_simplify(sys2, outputs = [sys.y]) @test !is_bound(syss, y) @test !is_bound(syss, x) @@ -165,6 +165,7 @@ end ] @named sys = ODESystem(eqs, t) + sys = structural_simplify(sys, inputs = [u]) f, dvs, ps, io_sys = ModelingToolkit.generate_control_function(sys; simplify, split) @test isequal(dvs[], x) @@ -182,8 +183,8 @@ end ] @named sys = ODESystem(eqs, t) - f, dvs, ps, io_sys = ModelingToolkit.generate_control_function( - sys, [u], [d]; simplify, split) + sys = structural_simplify(sys, inputs = [u], disturbance_inputs = [d]) + f, dvs, ps, io_sys = ModelingToolkit.generate_control_function(sys; simplify, split) @test isequal(dvs[], x) @test isempty(ps) @@ -200,8 +201,9 @@ end ] @named sys = ODESystem(eqs, t) + sys = structural_simplify(sys, inputs = [u], disturbance_inputs = [d]) f, dvs, ps, io_sys = ModelingToolkit.generate_control_function( - sys, [u], [d]; simplify, split, disturbance_argument = true) + sys; simplify, split, disturbance_argument = true) @test isequal(dvs[], x) @test isempty(ps) @@ -265,9 +267,9 @@ eqs = [connect_sd(sd, mass1, mass2) @named _model = ODESystem(eqs, t) @named model = compose(_model, mass1, mass2, sd); +model = structural_simplify(model, inputs = [u]) f, dvs, ps, io_sys = ModelingToolkit.generate_control_function(model, simplify = true) @test length(dvs) == 4 -@test length(ps) == length(parameters(model)) p = MTKParameters(io_sys, [io_sys.u => NaN]) x = ModelingToolkit.varmap_to_vars( merge(ModelingToolkit.defaults(model), @@ -389,7 +391,7 @@ sys = structural_simplify(model) ## Disturbance models when plant has multiple inputs using ModelingToolkit, LinearAlgebra -using ModelingToolkit: DisturbanceModel, io_preprocessing, get_iv, get_disturbance_system +using ModelingToolkit: DisturbanceModel, get_iv, get_disturbance_system using ModelingToolkitStandardLibrary.Blocks A, C = [randn(2, 2) for i in 1:2] B = [1.0 0; 0 1.0] @@ -433,6 +435,7 @@ matrices = ModelingToolkit.reorder_unknowns( ] @named sys = ODESystem(eqs, t) + sys = structural_simplify(sys, inputs = [u]) (; io_sys,) = ModelingToolkit.generate_control_function(sys, simplify = true) obsfn = ModelingToolkit.build_explicit_observed_function( io_sys, [x + u * t]; inputs = [u]) @@ -444,9 +447,9 @@ end @constants c = 2.0 @variables x(t) eqs = [D(x) ~ c * x] - @named sys = ODESystem(eqs, t, [x], []) + @mtkbuild sys = ODESystem(eqs, t, [x], []) - f, dvs, ps, io_sys = ModelingToolkit.generate_control_function(sys, simplify = true) + f, dvs, ps, io_sys = ModelingToolkit.generate_control_function(sys) @test f[1]([0.5], nothing, MTKParameters(io_sys, []), 0.0) ≈ [1.0] end @@ -455,7 +458,8 @@ end @parameters p(::Real) = (x -> 2x) eqs = [D(x) ~ -x + p(u)] @named sys = ODESystem(eqs, t) - f, dvs, ps, io_sys = ModelingToolkit.generate_control_function(sys, simplify = true) + sys = structural_simplify(sys, inputs = [u]) + f, dvs, ps, io_sys = ModelingToolkit.generate_control_function(sys) p = MTKParameters(io_sys, []) u = [1.0] x = [1.0] diff --git a/test/reduction.jl b/test/reduction.jl index 81cfe8473b..a692dbff75 100644 --- a/test/reduction.jl +++ b/test/reduction.jl @@ -233,7 +233,7 @@ eqs = [D(x) ~ σ * (y - x) u ~ z + a] lorenz1 = ODESystem(eqs, t, name = :lorenz1) -lorenz1_reduced, _ = structural_simplify(lorenz1, inputs = [z], outputs = []) +lorenz1_reduced = structural_simplify(lorenz1, inputs = [z], outputs = []) @test z in Set(parameters(lorenz1_reduced)) # #2064 From 6ca9b12e92b8263da15906f4185a1b99a2cf801b Mon Sep 17 00:00:00 2001 From: vyudu Date: Fri, 25 Apr 2025 11:20:18 -0400 Subject: [PATCH 1588/2176] more test fixes --- src/inputoutput.jl | 2 +- test/code_generation.jl | 2 +- test/extensions/test_infiniteopt.jl | 3 +-- 3 files changed, 3 insertions(+), 4 deletions(-) diff --git a/src/inputoutput.jl b/src/inputoutput.jl index 2093277fe4..cecc2d36d4 100644 --- a/src/inputoutput.jl +++ b/src/inputoutput.jl @@ -163,7 +163,7 @@ has_var(ex, x) = x ∈ Set(get_variables(ex)) (f_oop, f_ip), x_sym, p_sym, io_sys = generate_control_function( sys::AbstractODESystem, inputs = unbound_inputs(sys), - disturbance_inputs = Any[]; + disturbance_inputs = disturbances(sys); implicit_dae = false, simplify = false, ) diff --git a/test/code_generation.jl b/test/code_generation.jl index 3ef5ac3e11..2fbf8f13d7 100644 --- a/test/code_generation.jl +++ b/test/code_generation.jl @@ -70,7 +70,7 @@ end @parameters p[1:2] (f::Function)(..) @named sys = ODESystem( [D(x[0]) ~ p[1] * x[0] + x[2], D(x[1]) ~ p[2] * f(x) + x[2]], t) - sys, = structural_simplify(sys, inputs = [x[2]], outputs = []) + sys = structural_simplify(sys, inputs = [x[2]], outputs = []) @test is_parameter(sys, x[2]) prob = ODEProblem(sys, [x[0] => 1.0, x[1] => 1.0], (0.0, 1.0), [p => ones(2), f => sum, x[2] => 2.0]) diff --git a/test/extensions/test_infiniteopt.jl b/test/extensions/test_infiniteopt.jl index eb734358c5..1a811359d3 100644 --- a/test/extensions/test_infiniteopt.jl +++ b/test/extensions/test_infiniteopt.jl @@ -24,8 +24,7 @@ end model = complete(model) inputs = [model.τ] outputs = [model.y] -model, _ = structural_simplify(model, (inputs, outputs)) - +model = structural_simplify(model; inputs, outputs) f, dvs, psym, io_sys = ModelingToolkit.generate_control_function( model, split = false) From 1edc7f192572f3c7404411edaadc2c267622f9b4 Mon Sep 17 00:00:00 2001 From: vyudu Date: Mon, 28 Apr 2025 16:12:50 -0400 Subject: [PATCH 1589/2176] fix: fix sort_eqs and check distrubances in markio --- src/inputoutput.jl | 15 ++------------- src/linearization.jl | 28 ++++++++++++++++++---------- src/systems/systems.jl | 1 + src/systems/systemstructure.jl | 3 ++- 4 files changed, 23 insertions(+), 24 deletions(-) diff --git a/src/inputoutput.jl b/src/inputoutput.jl index cecc2d36d4..283be6ed92 100644 --- a/src/inputoutput.jl +++ b/src/inputoutput.jl @@ -259,7 +259,7 @@ end """ Turn input variables into parameters of the system. """ -function inputs_to_parameters!(state::TransformationState, inputsyms; is_disturbance = false) +function inputs_to_parameters!(state::TransformationState, inputsyms) check_bound = inputsyms === nothing @unpack structure, fullvars, sys = state @unpack var_to_diff, graph, solvable_graph = structure @@ -318,18 +318,7 @@ function inputs_to_parameters!(state::TransformationState, inputsyms; is_disturb @set! sys.unknowns = setdiff(unknowns(sys), keys(input_to_parameters)) ps = parameters(sys) - if inputsyms !== nothing - # Change order of new parameters to correspond to user-provided order in argument `inputs` - d = Dict{Any, Int}() - for (i, inp) in enumerate(new_parameters) - d[inp] = i - end - permutation = [d[i] for i in inputsyms] - new_parameters = new_parameters[permutation] - end - @set! sys.ps = [ps; new_parameters] - @set! state.sys = sys @set! state.fullvars = new_fullvars @set! state.structure = structure @@ -432,6 +421,6 @@ function add_input_disturbance(sys, dist::DisturbanceModel, inputs = Any[]; kwar ssys = structural_simplify(augmented_sys, inputs = all_inputs, disturbance_inputs = [d]) f, dvs, p, io_sys = generate_control_function(ssys, all_inputs, - [d]; check_simplified = false, kwargs...) + [d]; kwargs...) f, augmented_sys, dvs, p, io_sys end diff --git a/src/linearization.jl b/src/linearization.jl index ddc71623c1..dd474ea1a0 100644 --- a/src/linearization.jl +++ b/src/linearization.jl @@ -132,14 +132,13 @@ function linearization_function(sys::AbstractSystem, inputs, return lin_fun, sys end +""" +Return the set of indexes of differential equations and algebraic equations in the simplified system. +""" function eq_idxs(sys::AbstractSystem) eqs = equations(sys) - alg_start_idx = findfirst(!isdiffeq, eqs) - if alg_start_idx === nothing - alg_start_idx = length(eqs) + 1 - end - diff_idxs = 1:(alg_start_idx - 1) - alge_idxs = alg_start_idx:length(eqs) + alge_idxs = findall(!isdiffeq, eqs) + diff_idxs = setdiff(1:length(eqs), alge_idxs) diff_idxs, alge_idxs end @@ -561,6 +560,9 @@ function linearize_symbolic(sys::AbstractSystem, inputs, (; A, B, C, D, f_x, f_z, g_x, g_z, f_u, g_u, h_x, h_z, h_u), sys end +""" +Modify the variable metadata of system variables to indicate which ones are inputs, outputs, and disturbances. Needed for `inputs`, `outputs`, `disturbances`, `unbound_inputs`, `unbound_outputs` to return the proper subsets. +""" function markio!(state, orig_inputs, inputs, outputs, disturbances; check = true) fullvars = get_fullvars(state) inputset = Dict{Any, Bool}(i => false for i in inputs) @@ -591,6 +593,7 @@ function markio!(state, orig_inputs, inputs, outputs, disturbances; check = true if v in keys(disturbanceset) v = setio(v, true, false) v = setdisturbance(v, true) + disturbanceset[v] = true fullvars[i] = v end end @@ -601,11 +604,16 @@ function markio!(state, orig_inputs, inputs, outputs, disturbances; check = true "Some specified inputs were not found in system. The following variables were not found ", ikeys) end - end - check && (all(values(outputset)) || - error( - "Some specified outputs were not found in system. The following Dict indicates the found variables ", + dkeys = keys(filter(!last, disturbanceset)) + if !isempty(dkeys) + error( + "Specified disturbance inputs were not found in system. The following variables were not found ", + ikeys) + end + (all(values(outputset)) || error( + "Some specified outputs were not found in system. The following Dict indicates the found variables ", outputset)) + end state, orig_inputs end diff --git a/src/systems/systems.jl b/src/systems/systems.jl index a326703262..e417337a95 100644 --- a/src/systems/systems.jl +++ b/src/systems/systems.jl @@ -74,6 +74,7 @@ end function __structural_simplify(sys::AbstractSystem; simplify = false, inputs = Any[], outputs = Any[], disturbance_inputs = Any[], + sort_eqs = true, kwargs...) sys = expand_connections(sys) state = TearingState(sys; sort_eqs) diff --git a/src/systems/systemstructure.jl b/src/systems/systemstructure.jl index 4daf4a29cc..1b561f97b4 100644 --- a/src/systems/systemstructure.jl +++ b/src/systems/systemstructure.jl @@ -724,7 +724,8 @@ function _structural_simplify!(state::TearingState; simplify = false, else check_consistency = true end - has_io = inputs !== nothing || outputs !== nothing + has_io = !isempty(inputs) || !isempty(outputs) !== nothing || + !isempty(disturbance_inputs) orig_inputs = Set() if has_io ModelingToolkit.markio!(state, orig_inputs, inputs, outputs, disturbance_inputs) From 07b8cde9c67824f157326d3e604b0999c8118276 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 7 May 2025 12:46:02 +0530 Subject: [PATCH 1590/2176] test: fix usage of old `structural_simplify` io syntax --- test/extensions/dynamic_optimization.jl | 12 ++++++------ test/odesystem.jl | 2 +- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/test/extensions/dynamic_optimization.jl b/test/extensions/dynamic_optimization.jl index 6e037746a3..14008bdbf7 100644 --- a/test/extensions/dynamic_optimization.jl +++ b/test/extensions/dynamic_optimization.jl @@ -118,7 +118,7 @@ end cost = [-x(1.0)] # Maximize the final distance. @named block = ODESystem( [D(x(t)) ~ v(t), D(v(t)) ~ u(t)], t; costs = cost, constraints = constr) - block, input_idxs = structural_simplify(block, ([u(t)], [])) + block = structural_simplify(block; inputs = [u(t)]) u0map = [x(t) => 0.0, v(t) => 0.0] tspan = (0.0, 1.0) @@ -166,7 +166,7 @@ end costs = [-q(tspan[2])] @named beesys = ODESystem(eqs, t; costs) - beesys, input_idxs = structural_simplify(beesys, ([α], [])) + beesys = structural_simplify(beesys; inputs = [α]) u0map = [w(t) => 40, q(t) => 2] pmap = [b => 1, c => 1, μ => 1, s => 1, ν => 1, α => 1] @@ -213,7 +213,7 @@ end costs = [-h(te)] cons = [T(te) ~ 0, m(te) ~ m_c] @named rocket = ODESystem(eqs, t; costs, constraints = cons) - rocket, input_idxs = structural_simplify(rocket, ([T(t)], [])) + rocket = structural_simplify(rocket; inputs = [T(t)]) u0map = [h(t) => h₀, m(t) => m₀, v(t) => 0] pmap = [ @@ -261,7 +261,7 @@ end costs = [-Symbolics.Integral(t in (0, tf))(x(t) - u(t)), -x(tf)] consolidate(u) = u[1] + u[2] @named rocket = ODESystem(eqs, t; costs, consolidate) - rocket, input_idxs = structural_simplify(rocket, ([u(t)], [])) + rocket = structural_simplify(rocket; inputs = [u(t)]) u0map = [x(t) => 17.5] pmap = [u(t) => 0.0, tf => 8] @@ -285,7 +285,7 @@ end @named block = ODESystem( [D(x(t)) ~ v(t), D(v(t)) ~ u(t)], t; costs = cost, constraints = constr) - block, input_idxs = structural_simplify(block, ([u(t)], [])) + block = structural_simplify(block, inputs = [u(t)]) u0map = [x(t) => 1.0, v(t) => 0.0] tspan = (0.0, tf) @@ -328,7 +328,7 @@ end tspan = (0, tf) @named cartpole = ODESystem(eqs, t; costs, constraints = cons) - cartpole, input_idxs = structural_simplify(cartpole, ([u], [])) + cartpole, input_idxs = structural_simplify(cartpole; inputs = [u]) u0map = [D(x(t)) => 0.0, D(θ(t)) => 0.0, θ(t) => 0.0, x(t) => 0.0] pmap = [mₖ => 1.0, mₚ => 0.2, l => 0.5, g => 9.81, u => 0] diff --git a/test/odesystem.jl b/test/odesystem.jl index 2dbf4b2161..e113e5d2c2 100644 --- a/test/odesystem.jl +++ b/test/odesystem.jl @@ -466,7 +466,7 @@ let eqs = [D(x) ~ ẋ, D(ẋ) ~ f - k * x - d * ẋ] @named sys = ODESystem(eqs, t, [x, ẋ], [d, k]) - sys, _ = structural_simplify(sys, ([f], [])) + sys = structural_simplify(sys; inputs = [f]) @test isequal(calculate_control_jacobian(sys), reshape(Num[0, 1], 2, 1)) From 8a7259e0fc473db414ccb8756aafea09fd4489ea Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 9 May 2025 15:07:25 +0530 Subject: [PATCH 1591/2176] fix: fix usage of `structural_simplify` with inputs --- test/extensions/dynamic_optimization.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/extensions/dynamic_optimization.jl b/test/extensions/dynamic_optimization.jl index 14008bdbf7..79810d2476 100644 --- a/test/extensions/dynamic_optimization.jl +++ b/test/extensions/dynamic_optimization.jl @@ -328,7 +328,7 @@ end tspan = (0, tf) @named cartpole = ODESystem(eqs, t; costs, constraints = cons) - cartpole, input_idxs = structural_simplify(cartpole; inputs = [u]) + cartpole = structural_simplify(cartpole; inputs = [u]) u0map = [D(x(t)) => 0.0, D(θ(t)) => 0.0, θ(t) => 0.0, x(t) => 0.0] pmap = [mₖ => 1.0, mₚ => 0.2, l => 0.5, g => 9.81, u => 0] From c477a332b9e9595ba0e0f55c4a3835b2cd7c7dc3 Mon Sep 17 00:00:00 2001 From: vyudu Date: Sat, 1 Mar 2025 17:35:49 -0500 Subject: [PATCH 1592/2176] feat: add new affect semantics, reorganize callback code --- src/systems/callbacks.jl | 1618 ++++++++++++++---------------- src/systems/imperative_affect.jl | 59 +- 2 files changed, 822 insertions(+), 855 deletions(-) diff --git a/src/systems/callbacks.jl b/src/systems/callbacks.jl index 7d542d9bd0..8a5c6131f3 100644 --- a/src/systems/callbacks.jl +++ b/src/systems/callbacks.jl @@ -1,15 +1,4 @@ -#################################### system operations ##################################### -has_continuous_events(sys::AbstractSystem) = isdefined(sys, :continuous_events) -function get_continuous_events(sys::AbstractSystem) - has_continuous_events(sys) || return SymbolicContinuousCallback[] - getfield(sys, :continuous_events) -end - -has_discrete_events(sys::AbstractSystem) = isdefined(sys, :discrete_events) -function get_discrete_events(sys::AbstractSystem) - has_discrete_events(sys) || return SymbolicDiscreteCallback[] - getfield(sys, :discrete_events) -end +abstract type AbstractCallback end struct FunctionalAffect f::Any @@ -38,7 +27,7 @@ function FunctionalAffect(; f, sts, pars, discretes, ctx = nothing) FunctionalAffect(f, sts, pars, discretes, ctx) end -func(f::FunctionalAffect) = f.f +func(a::FunctionalAffect) = a.f context(a::FunctionalAffect) = a.ctx parameters(a::FunctionalAffect) = a.pars parameters_syms(a::FunctionalAffect) = a.pars_syms @@ -62,33 +51,118 @@ function Base.hash(a::FunctionalAffect, s::UInt) hash(a.ctx, s) end -namespace_affect(affect, s) = namespace_equation(affect, s) -function namespace_affect(affect::FunctionalAffect, s) - FunctionalAffect(func(affect), - renamespace.((s,), unknowns(affect)), - unknowns_syms(affect), - renamespace.((s,), parameters(affect)), - parameters_syms(affect), - renamespace.((s,), discretes(affect)), - context(affect)) -end - function has_functional_affect(cb) (affects(cb) isa FunctionalAffect || affects(cb) isa ImperativeAffect) end -function vars!(vars, aff::FunctionalAffect; op = Differential) +struct AffectSystem + """The internal implicit discrete system whose equations are solved to obtain values after the affect.""" + system::ImplicitDiscreteSystem + """Unknowns of the parent ODESystem whose values are modified or accessed by the affect.""" + unknowns::Vector + """Parameters of the parent ODESystem whose values are accessed by the affect.""" + parameters::Vector + """Parameters of the parent ODESystem whose values are modified by the affect.""" + discretes::Vector + """Maps the symbols of unknowns/observed in the ImplicitDiscreteSystem to its corresponding unknown/parameter in the parent system.""" + aff_to_sys::Dict +end + +system(a::AffectSystem) = a.system +discretes(a::AffectSystem) = a.discretes +unknowns(a::AffectSystem) = a.unknowns +parameters(a::AffectSystem) = a.parameters +aff_to_sys(a::AffectSystem) = a.aff_to_sys +all_equations(a::AffectSystem) = vcat(equations(system(a)), observed(system(a))) + +function Base.show(iio::IO, aff::AffectSystem) + println(iio, "Affect system defined by equations:") + eqs = all_equations(aff) + show(iio, eqs) +end + +function Base.:(==)(a1::AffectSystem, a2::AffectSystem) + isequal(system(a1), system(a2)) && + isequal(discretes(a1), discretes(a2)) && + isequal(unknowns(a1), unknowns(a2)) && + isequal(parameters(a1), parameters(a2)) && + isequal(aff_to_sys(a1), aff_to_sys(a2)) +end + +function Base.hash(a::AffectSystem, s::UInt) + s = hash(system(a), s) + s = hash(unknowns(a), s) + s = hash(parameters(a), s) + s = hash(discretes(a), s) + hash(aff_to_sys(a), s) +end + +function vars!(vars, aff::Union{FunctionalAffect, AffectSystem}; op = Differential) for var in Iterators.flatten((unknowns(aff), parameters(aff), discretes(aff))) vars!(vars, var) end - return vars + vars end -#################################### continuous events ##################################### +""" + Pre(x) -const NULL_AFFECT = Equation[] +The `Pre` operator. Used by the callback system to indicate the value of a parameter or variable +before the callback is triggered. """ - SymbolicContinuousCallback(eqs::Vector{Equation}, affect, affect_neg, rootfind) +struct Pre <: Symbolics.Operator end +Pre(x) = Pre()(x) +SymbolicUtils.promote_symtype(::Type{Pre}, T) = T +SymbolicUtils.isbinop(::Pre) = false +Base.nameof(::Pre) = :Pre +Base.show(io::IO, x::Pre) = print(io, "Pre") +input_timedomain(::Pre, _ = nothing) = ContinuousClock() +output_timedomain(::Pre, _ = nothing) = ContinuousClock() +unPre(x::Num) = unPre(unwrap(x)) +unPre(x::BasicSymbolic) = (iscall(x) && operation(x) isa Pre) ? only(arguments(x)) : x + +function (p::Pre)(x) + iw = Symbolics.iswrapped(x) + x = unwrap(x) + # non-symbolic values don't change + if symbolic_type(x) == NotSymbolic() + return x + end + # differential variables are default-toterm-ed + if iscall(x) && operation(x) isa Differential + x = default_toterm(x) + end + # don't double wrap + iscall(x) && operation(x) isa Pre && return x + result = if symbolic_type(x) == ArraySymbolic() + # create an array for `Pre(array)` + Symbolics.array_term(p, x) + elseif iscall(x) && operation(x) == getindex + # instead of `Pre(x[1])` create `Pre(x)[1]` + # which allows parameter indexing to handle this case automatically. + arr = arguments(x)[1] + term(getindex, p(arr), arguments(x)[2:end]...) + else + term(p, x) + end + # the result should be a parameter + result = toparam(result) + if iw + result = wrap(result) + end + return result +end +haspre(eq::Equation) = haspre(eq.lhs) || haspre(eq.rhs) +haspre(O) = recursive_hasoperator(Pre, O) + +############################### +###### Continuous events ###### +############################### +const Affect = Union{AffectSystem, FunctionalAffect, ImperativeAffect} + +""" + SymbolicContinuousCallback(eqs::Vector{Equation}, affect = nothing, iv = nothing; + affect_neg = affect, initialize = nothing, finalize = nothing, rootfind = SciMLBase.LeftRootFind, alg_eqs = Equation[]) A [`ContinuousCallback`](@ref SciMLBase.ContinuousCallback) specified symbolically. Takes a vector of equations `eq` as well as the positive-edge `affect` and negative-edge `affect_neg` that apply when *any* of `eq` are satisfied. @@ -128,995 +202,766 @@ Affects (i.e. `affect` and `affect_neg`) can be specified as either: + `ctx` is a user-defined context object passed to `f!` when invoked. This value is aliased for each problem. * A [`ImperativeAffect`](@ref); refer to its documentation for details. -DAEs will be reinitialized using `reinitializealg` (which defaults to `SciMLBase.CheckInit`) after callbacks are applied. -This reinitialization algorithm ensures that the DAE is satisfied after the callback runs. The default value of `CheckInit` will simply validate -that the newly-assigned values indeed satisfy the algebraic system; see the documentation on DAE initialization for a more detailed discussion of -initialization. +`reinitializealg` is used to set how the system will be reinitialized after the callback. +- Symbolic affects have reinitialization built in. In this case the algorithm will default to SciMLBase.NoInit(), and should **not** be provided. +- Functional and imperative affects will default to SciMLBase.CheckInit(), which will error if the system is not properly reinitialized after the callback. If your system is a DAE, pass in an algorithm like SciMLBase.BrownBasicFullInit() to properly re-initialize. -Initial and final affects can also be specified with SCC, which are specified identically to positive and negative edge affects. Initialization affects +Initial and final affects can also be specified identically to positive and negative edge affects. Initialization affects will run as soon as the solver starts, while finalization affects will be executed after termination. """ -struct SymbolicContinuousCallback - eqs::Vector{Equation} - initialize::Union{Vector{Equation}, FunctionalAffect, ImperativeAffect} - finalize::Union{Vector{Equation}, FunctionalAffect, ImperativeAffect} - affect::Union{Vector{Equation}, FunctionalAffect, ImperativeAffect} - affect_neg::Union{Vector{Equation}, FunctionalAffect, ImperativeAffect, Nothing} - rootfind::SciMLBase.RootfindOpt +struct SymbolicContinuousCallback <: AbstractCallback + conditions::Vector{Equation} + affect::Union{Affect, Nothing} + affect_neg::Union{Affect, Nothing} + initialize::Union{Affect, Nothing} + finalize::Union{Affect, Nothing} + rootfind::Union{Nothing, SciMLBase.RootfindOpt} reinitializealg::SciMLBase.DAEInitializationAlgorithm - function SymbolicContinuousCallback(; - eqs::Vector{Equation}, - affect = NULL_AFFECT, + + function SymbolicContinuousCallback( + conditions::Union{Equation, Vector{Equation}}, + affect = nothing; affect_neg = affect, - initialize = NULL_AFFECT, - finalize = NULL_AFFECT, + initialize = nothing, + finalize = nothing, rootfind = SciMLBase.LeftRootFind, - reinitializealg = SciMLBase.CheckInit()) - new(eqs, initialize, finalize, make_affect(affect), - make_affect(affect_neg), rootfind, reinitializealg) + reinitializealg = nothing, + kwargs...) + conditions = (conditions isa AbstractVector) ? conditions : [conditions] + + if isnothing(reinitializealg) + if any(a -> (a isa FunctionalAffect || a isa ImperativeAffect), + [affect, affect_neg, initialize, finalize]) + reinitializealg = SciMLBase.CheckInit() + else + reinitializealg = SciMLBase.NoInit() + end + end + + new(conditions, make_affect(affect; kwargs...), + make_affect(affect_neg; kwargs...), + make_affect(initialize; kwargs...), make_affect( + finalize; kwargs...), + rootfind, reinitializealg) end # Default affect to nothing end -make_affect(affect) = affect -make_affect(affect::Tuple) = FunctionalAffect(affect...) -make_affect(affect::NamedTuple) = FunctionalAffect(; affect...) - -function Base.:(==)(e1::SymbolicContinuousCallback, e2::SymbolicContinuousCallback) - isequal(e1.eqs, e2.eqs) && isequal(e1.affect, e2.affect) && - isequal(e1.initialize, e2.initialize) && isequal(e1.finalize, e2.finalize) && - isequal(e1.affect_neg, e2.affect_neg) && isequal(e1.rootfind, e2.rootfind) + +function SymbolicContinuousCallback(p::Pair, args...; kwargs...) + SymbolicContinuousCallback(p[1], p[2], args...; kwargs...) end -Base.isempty(cb::SymbolicContinuousCallback) = isempty(cb.eqs) -function Base.hash(cb::SymbolicContinuousCallback, s::UInt) - hash_affect(affect::AbstractVector, s) = foldr(hash, affect, init = s) - hash_affect(affect, s) = hash(affect, s) - s = foldr(hash, cb.eqs, init = s) - s = hash_affect(cb.affect, s) - s = hash_affect(cb.affect_neg, s) - s = hash_affect(cb.initialize, s) - s = hash_affect(cb.finalize, s) - s = hash(cb.reinitializealg, s) - hash(cb.rootfind, s) +SymbolicContinuousCallback(cb::SymbolicContinuousCallback, args...; kwargs...) = cb +SymbolicContinuousCallback(cb::Nothing, args...; kwargs...) = nothing +function SymbolicContinuousCallback(cb::Tuple, args...; kwargs...) + if length(cb) == 2 + SymbolicContinuousCallback(cb[1]; kwargs..., cb[2]...) + else + error("Malformed tuple specifying callback. Should be a condition => affect pair, followed by a vector of kwargs.") + end end -function Base.show(io::IO, cb::SymbolicContinuousCallback) +make_affect(affect::Nothing; kwargs...) = nothing +make_affect(affect::Tuple; kwargs...) = FunctionalAffect(affect...) +make_affect(affect::NamedTuple; kwargs...) = FunctionalAffect(; affect...) +make_affect(affect::Affect; kwargs...) = affect + +function make_affect(affect::Vector{Equation}; discrete_parameters = Any[], + iv = nothing, alg_eqs::Vector{Equation} = Equation[], warn_no_algebraic = true, kwargs...) + isempty(affect) && return nothing + if isnothing(iv) + iv = t_nounits + @warn "No independent variable specified. Defaulting to t_nounits." + end + + discrete_parameters isa AbstractVector || (discrete_parameters = [discrete_parameters]) + for p in discrete_parameters + occursin(unwrap(iv), unwrap(p)) || + error("Non-time dependent parameter $p passed in as a discrete. Must be declared as @parameters $p(t).") + end + + dvs = OrderedSet() + params = OrderedSet() + for eq in affect + if !haspre(eq) && !(symbolic_type(eq.rhs) === NotSymbolic() || + symbolic_type(eq.lhs) === NotSymbolic()) + @warn "Affect equation $eq has no `Pre` operator. As such it will be interpreted as an algebraic equation to be satisfied after the callback. If you intended to use the value of a variable x before the affect, use Pre(x). Errors may be thrown if there is no `Pre` and the algebraic equation is unsatisfiable, such as X ~ X + 1." + end + collect_vars!(dvs, params, eq, iv; op = Pre) + diffvs = collect_applied_operators(eq, Differential) + union!(dvs, diffvs) + end + for eq in alg_eqs + collect_vars!(dvs, params, eq, iv) + end + + pre_params = filter(haspre ∘ value, params) + sys_params = collect(setdiff(params, union(discrete_parameters, pre_params))) + discretes = map(tovar, discrete_parameters) + dvs = collect(dvs) + _dvs = map(default_toterm, dvs) + + aff_map = Dict(zip(discretes, discrete_parameters)) + rev_map = Dict(zip(discrete_parameters, discretes)) + subs = merge(rev_map, Dict(zip(dvs, _dvs))) + affect = Symbolics.fast_substitute(affect, subs) + alg_eqs = Symbolics.fast_substitute(alg_eqs, subs) + + @named affectsys = ImplicitDiscreteSystem( + vcat(affect, alg_eqs), iv, collect(union(_dvs, discretes)), + collect(union(pre_params, sys_params))) + affectsys = structural_simplify(affectsys; fully_determined = nothing) + # get accessed parameters p from Pre(p) in the callback parameters + accessed_params = filter(isparameter, map(unPre, collect(pre_params))) + union!(accessed_params, sys_params) + + # add scalarized unknowns to the map. + _dvs = reduce(vcat, map(scalarize, _dvs), init = Any[]) + for u in _dvs + aff_map[u] = u + end + + AffectSystem(affectsys, collect(_dvs), collect(accessed_params), + collect(discrete_parameters), aff_map) +end + +function make_affect(affect; kwargs...) + error("Malformed affect $(affect). This should be a vector of equations or a tuple specifying a functional affect.") +end + +function Base.show(io::IO, cb::AbstractCallback) indent = get(io, :indent, 0) iio = IOContext(io, :indent => indent + 1) + is_discrete(cb) ? print(io, "SymbolicDiscreteCallback(") : print(io, "SymbolicContinuousCallback(") - print(iio, "Equations:") + print(iio, "Conditions:") show(iio, equations(cb)) print(iio, "; ") - if affects(cb) != NULL_AFFECT + if affects(cb) != nothing print(iio, "Affect:") show(iio, affects(cb)) print(iio, ", ") end - if affect_negs(cb) != NULL_AFFECT + if !is_discrete(cb) && affect_negs(cb) != nothing print(iio, "Negative-edge affect:") show(iio, affect_negs(cb)) print(iio, ", ") end - if initialize_affects(cb) != NULL_AFFECT + if initialize_affects(cb) != nothing print(iio, "Initialization affect:") show(iio, initialize_affects(cb)) print(iio, ", ") end - if finalize_affects(cb) != NULL_AFFECT + if finalize_affects(cb) != nothing print(iio, "Finalization affect:") show(iio, finalize_affects(cb)) end print(iio, ")") end -function Base.show(io::IO, mime::MIME"text/plain", cb::SymbolicContinuousCallback) +function Base.show(io::IO, mime::MIME"text/plain", cb::AbstractCallback) indent = get(io, :indent, 0) iio = IOContext(io, :indent => indent + 1) + is_discrete(cb) ? println(io, "SymbolicDiscreteCallback:") : println(io, "SymbolicContinuousCallback:") - println(iio, "Equations:") + println(iio, "Conditions:") show(iio, mime, equations(cb)) print(iio, "\n") - if affects(cb) != NULL_AFFECT + if affects(cb) != nothing println(iio, "Affect:") show(iio, mime, affects(cb)) print(iio, "\n") end - if affect_negs(cb) != NULL_AFFECT - println(iio, "Negative-edge affect:") + if !is_discrete(cb) && affect_negs(cb) != nothing + print(iio, "Negative-edge affect:\n") show(iio, mime, affect_negs(cb)) print(iio, "\n") end - if initialize_affects(cb) != NULL_AFFECT + if initialize_affects(cb) != nothing println(iio, "Initialization affect:") show(iio, mime, initialize_affects(cb)) print(iio, "\n") end - if finalize_affects(cb) != NULL_AFFECT + if finalize_affects(cb) != nothing println(iio, "Finalization affect:") show(iio, mime, finalize_affects(cb)) print(iio, "\n") end end -to_equation_vector(eq::Equation) = [eq] -to_equation_vector(eqs::Vector{Equation}) = eqs -function to_equation_vector(eqs::Vector{Any}) - isempty(eqs) || error("This should never happen") - Equation[] -end - -function SymbolicContinuousCallback(args...) - SymbolicContinuousCallback(to_equation_vector.(args)...) -end # wrap eq in vector -SymbolicContinuousCallback(p::Pair) = SymbolicContinuousCallback(p[1], p[2]) -SymbolicContinuousCallback(cb::SymbolicContinuousCallback) = cb # passthrough -function SymbolicContinuousCallback(eqs::Equation, affect = NULL_AFFECT; - initialize = NULL_AFFECT, finalize = NULL_AFFECT, - affect_neg = affect, rootfind = SciMLBase.LeftRootFind) - SymbolicContinuousCallback( - eqs = [eqs], affect = affect, affect_neg = affect_neg, - initialize = initialize, finalize = finalize, rootfind = rootfind) -end -function SymbolicContinuousCallback(eqs::Vector{Equation}, affect = NULL_AFFECT; - affect_neg = affect, initialize = NULL_AFFECT, finalize = NULL_AFFECT, - rootfind = SciMLBase.LeftRootFind) - SymbolicContinuousCallback( - eqs = eqs, affect = affect, affect_neg = affect_neg, - initialize = initialize, finalize = finalize, rootfind = rootfind) -end - -SymbolicContinuousCallbacks(cb::SymbolicContinuousCallback) = [cb] -SymbolicContinuousCallbacks(cbs::Vector{<:SymbolicContinuousCallback}) = cbs -SymbolicContinuousCallbacks(cbs::Vector) = SymbolicContinuousCallback.(cbs) -function SymbolicContinuousCallbacks(ve::Vector{Equation}) - SymbolicContinuousCallbacks(SymbolicContinuousCallback(ve)) -end -function SymbolicContinuousCallbacks(others) - SymbolicContinuousCallbacks(SymbolicContinuousCallback(others)) -end -SymbolicContinuousCallbacks(::Nothing) = SymbolicContinuousCallback[] - -equations(cb::SymbolicContinuousCallback) = cb.eqs -function equations(cbs::Vector{<:SymbolicContinuousCallback}) - mapreduce(equations, vcat, cbs, init = Equation[]) -end - -affects(cb::SymbolicContinuousCallback) = cb.affect -function affects(cbs::Vector{SymbolicContinuousCallback}) - mapreduce(affects, vcat, cbs, init = Equation[]) -end - -affect_negs(cb::SymbolicContinuousCallback) = cb.affect_neg -function affect_negs(cbs::Vector{SymbolicContinuousCallback}) - mapreduce(affect_negs, vcat, cbs, init = Equation[]) -end - -reinitialization_alg(cb::SymbolicContinuousCallback) = cb.reinitializealg -function reinitialization_algs(cbs::Vector{SymbolicContinuousCallback}) - mapreduce( - reinitialization_alg, vcat, cbs, init = SciMLBase.DAEInitializationAlgorithm[]) -end - -initialize_affects(cb::SymbolicContinuousCallback) = cb.initialize -function initialize_affects(cbs::Vector{SymbolicContinuousCallback}) - mapreduce(initialize_affects, vcat, cbs, init = Equation[]) -end - -finalize_affects(cb::SymbolicContinuousCallback) = cb.finalize -function finalize_affects(cbs::Vector{SymbolicContinuousCallback}) - mapreduce(finalize_affects, vcat, cbs, init = Equation[]) -end - -namespace_affects(af::Vector, s) = Equation[namespace_affect(a, s) for a in af] -namespace_affects(af::FunctionalAffect, s) = namespace_affect(af, s) -namespace_affects(::Nothing, s) = nothing - -function namespace_callback(cb::SymbolicContinuousCallback, s)::SymbolicContinuousCallback - SymbolicContinuousCallback(; - eqs = namespace_equation.(equations(cb), (s,)), - affect = namespace_affects(affects(cb), s), - affect_neg = namespace_affects(affect_negs(cb), s), - initialize = namespace_affects(initialize_affects(cb), s), - finalize = namespace_affects(finalize_affects(cb), s), - rootfind = cb.rootfind) -end - -""" - continuous_events(sys::AbstractSystem)::Vector{SymbolicContinuousCallback} - -Returns a vector of all the `continuous_events` in an abstract system and its component subsystems. -The `SymbolicContinuousCallback`s in the returned vector are structs with two fields: `eqs` and -`affect` which correspond to the first and second elements of a `Pair` used to define an event, i.e. -`eqs => affect`. -""" -function continuous_events(sys::AbstractSystem) - cbs = get_continuous_events(sys) - filter(!isempty, cbs) - - systems = get_systems(sys) - cbs = [cbs; - reduce(vcat, - (map(cb -> namespace_callback(cb, s), continuous_events(s)) - for s in systems), - init = SymbolicContinuousCallback[])] - filter(!isempty, cbs) -end - -function vars!(vars, cb::SymbolicContinuousCallback; op = Differential) - for eq in equations(cb) - vars!(vars, eq; op) - end - for aff in (affects(cb), affect_negs(cb), initialize_affects(cb), finalize_affects(cb)) - if aff isa Vector{Equation} - for eq in aff +function vars!(vars, cb::AbstractCallback; op = Differential) + if symbolic_type(conditions(cb)) == NotSymbolic + if conditions(cb) isa AbstractArray + for eq in conditions(cb) vars!(vars, eq; op) end - elseif aff !== nothing - vars!(vars, aff; op) end + else + vars!(vars, conditions(cb); op) + end + for aff in (affects(cb), initialize_affects(cb), finalize_affects(cb)) + isnothing(aff) || vars!(vars, aff; op) end + !is_discrete(cb) && vars!(vars, affect_negs(cb); op) return vars end +################################ +######## Discrete events ####### +################################ """ - continuous_events_toplevel(sys::AbstractSystem) + SymbolicDiscreteCallback(conditions::Vector{Equation}, affect = nothing, iv = nothing; + initialize = nothing, finalize = nothing, alg_eqs = Equation[]) -Replicates the behaviour of `continuous_events`, but ignores events of subsystems. +A callback that triggers at the first timestep that the conditions are satisfied. -Notes: -- Cannot be applied to non-complete systems. -""" -function continuous_events_toplevel(sys::AbstractSystem) - if has_parent(sys) && (parent = get_parent(sys)) !== nothing - return continuous_events_toplevel(parent) - end - return get_continuous_events(sys) -end +The condition can be one of: +- Δt::Real - periodic events with period Δt +- ts::Vector{Real} - events trigger at these preset times given by `ts` +- eqs::Vector{Symbolic} - events trigger when the condition evaluates to true -#################################### discrete events ##################################### - -struct SymbolicDiscreteCallback - # condition can be one of: - # Δt::Real - Periodic with period Δt - # Δts::Vector{Real} - events trigger in this times (Preset) - # condition::Vector{Equation} - event triggered when condition is true - # TODO: Iterative - condition::Any - affects::Any - initialize::Any - finalize::Any +Arguments: +- iv: The independent variable of the system. This must be specified if the independent variable appears in one of the equations explicitly, as in x ~ t + 1. +- alg_eqs: Algebraic equations of the system that must be satisfied after the callback occurs. +""" +struct SymbolicDiscreteCallback <: AbstractCallback + conditions::Union{Number, Vector{<:Number}, Symbolic{Bool}} + affect::Union{Affect, Nothing} + initialize::Union{Affect, Nothing} + finalize::Union{Affect, Nothing} reinitializealg::SciMLBase.DAEInitializationAlgorithm function SymbolicDiscreteCallback( - condition, affects = NULL_AFFECT; reinitializealg = SciMLBase.CheckInit(), - initialize = NULL_AFFECT, finalize = NULL_AFFECT) - c = scalarize_condition(condition) - a = scalarize_affects(affects) - new(c, a, scalarize_affects(initialize), - scalarize_affects(finalize), reinitializealg) + condition::Union{Symbolic{Bool}, Number, Vector{<:Number}}, affect = nothing; + initialize = nothing, finalize = nothing, + reinitializealg = nothing, kwargs...) + c = is_timed_condition(condition) ? condition : value(scalarize(condition)) + + if isnothing(reinitializealg) + reinitializealg = SciMLBase.CheckInit() + else + reinitializealg = SciMLBase.NoInit() + end + new(c, make_affect(affect; kwargs...), + make_affect(initialize; kwargs...), + make_affect(finalize; kwargs...), reinitializealg) end # Default affect to nothing end -is_timed_condition(cb) = false -is_timed_condition(::R) where {R <: Real} = true -is_timed_condition(::V) where {V <: AbstractVector} = eltype(V) <: Real -is_timed_condition(::Num) = false -is_timed_condition(cb::SymbolicDiscreteCallback) = is_timed_condition(condition(cb)) - -function scalarize_condition(condition) - is_timed_condition(condition) ? condition : value(scalarize(condition)) -end -function namespace_condition(condition, s) - is_timed_condition(condition) ? condition : namespace_expr(condition, s) +function SymbolicDiscreteCallback(p::Pair, args...; kwargs...) + SymbolicDiscreteCallback(p[1], p[2], args...; kwargs...) end - -scalarize_affects(affects) = scalarize(affects) -scalarize_affects(affects::Tuple) = FunctionalAffect(affects...) -scalarize_affects(affects::NamedTuple) = FunctionalAffect(; affects...) -scalarize_affects(affects::FunctionalAffect) = affects - -SymbolicDiscreteCallback(p::Pair) = SymbolicDiscreteCallback(p[1], p[2]) -SymbolicDiscreteCallback(cb::SymbolicDiscreteCallback) = cb # passthrough - -function Base.show(io::IO, db::SymbolicDiscreteCallback) - println(io, "condition: ", db.condition) - println(io, "affects:") - if db.affects isa FunctionalAffect || db.affects isa ImperativeAffect - # TODO - println(io, " ", db.affects) +SymbolicDiscreteCallback(cb::SymbolicDiscreteCallback, args...; kwargs...) = cb +SymbolicDiscreteCallback(cb::Nothing, args...; kwargs...) = nothing +function SymbolicDiscreteCallback(cb::Tuple, args...; kwargs...) + if length(cb) == 2 + SymbolicDiscreteCallback(cb[1]; cb[2]...) else - for affect in db.affects - println(io, " ", affect) - end + error("Malformed tuple specifying callback. Should be a condition => affect pair, followed by a vector of kwargs.") end end -function Base.:(==)(e1::SymbolicDiscreteCallback, e2::SymbolicDiscreteCallback) - isequal(e1.condition, e2.condition) && isequal(e1.affects, e2.affects) && - isequal(e1.initialize, e2.initialize) && isequal(e1.finalize, e2.finalize) -end -function Base.hash(cb::SymbolicDiscreteCallback, s::UInt) - s = hash(cb.condition, s) - s = cb.affects isa AbstractVector ? foldr(hash, cb.affects, init = s) : - hash(cb.affects, s) - s = cb.initialize isa AbstractVector ? foldr(hash, cb.initialize, init = s) : - hash(cb.initialize, s) - s = cb.finalize isa AbstractVector ? foldr(hash, cb.finalize, init = s) : - hash(cb.finalize, s) - s = hash(cb.reinitializealg, s) - return s +function is_timed_condition(condition::T) where {T} + if T === Num + false + elseif T <: Real + true + elseif T <: AbstractVector + eltype(condition) <: Real + else + false + end end -condition(cb::SymbolicDiscreteCallback) = cb.condition -function conditions(cbs::Vector{<:SymbolicDiscreteCallback}) - reduce(vcat, condition(cb) for cb in cbs) +to_cb_vector(cbs::Vector{<:AbstractCallback}; kwargs...) = cbs +to_cb_vector(cbs::Union{Nothing, Vector{Nothing}}; kwargs...) = AbstractCallback[] +to_cb_vector(cb::AbstractCallback; kwargs...) = [cb] +function to_cb_vector(cbs; CB_TYPE = SymbolicContinuousCallback, kwargs...) + if cbs isa Pair + [CB_TYPE(cbs; kwargs...)] + else + Vector{CB_TYPE}([CB_TYPE(cb; kwargs...) for cb in cbs]) + end end -affects(cb::SymbolicDiscreteCallback) = cb.affects - -function affects(cbs::Vector{SymbolicDiscreteCallback}) - reduce(vcat, affects(cb) for cb in cbs; init = []) +############################################ +########## Namespacing Utilities ########### +############################################ +function namespace_affects(affect::FunctionalAffect, s) + FunctionalAffect(func(affect), + renamespace.((s,), unknowns(affect)), + unknowns_syms(affect), + renamespace.((s,), parameters(affect)), + parameters_syms(affect), + renamespace.((s,), discretes(affect)), + context(affect)) end -reinitialization_alg(cb::SymbolicDiscreteCallback) = cb.reinitializealg -function reinitialization_algs(cbs::Vector{SymbolicDiscreteCallback}) - mapreduce( - reinitialization_alg, vcat, cbs, init = SciMLBase.DAEInitializationAlgorithm[]) +function namespace_affects(affect::AffectSystem, s) + AffectSystem(renamespace(s, system(affect)), + renamespace.((s,), unknowns(affect)), + renamespace.((s,), parameters(affect)), + renamespace.((s,), discretes(affect)), + Dict([k => renamespace(s, v) for (k, v) in aff_to_sys(affect)])) end +namespace_affects(af::Nothing, s) = nothing -initialize_affects(cb::SymbolicDiscreteCallback) = cb.initialize -function initialize_affects(cbs::Vector{SymbolicDiscreteCallback}) - mapreduce(initialize_affects, vcat, cbs, init = Equation[]) +function namespace_callback(cb::SymbolicContinuousCallback, s)::SymbolicContinuousCallback + SymbolicContinuousCallback( + namespace_equation.(equations(cb), (s,)), + namespace_affects(affects(cb), s), + affect_neg = namespace_affects(affect_negs(cb), s), + initialize = namespace_affects(initialize_affects(cb), s), + finalize = namespace_affects(finalize_affects(cb), s), + rootfind = cb.rootfind, reinitializealg = cb.reinitializealg) end -finalize_affects(cb::SymbolicDiscreteCallback) = cb.finalize -function finalize_affects(cbs::Vector{SymbolicDiscreteCallback}) - mapreduce(finalize_affects, vcat, cbs, init = Equation[]) +function namespace_conditions(condition, s) + is_timed_condition(condition) ? condition : namespace_expr(condition, s) end function namespace_callback(cb::SymbolicDiscreteCallback, s)::SymbolicDiscreteCallback - function namespace_affects(af) - return af isa AbstractVector ? namespace_affect.(af, Ref(s)) : - namespace_affect(af, s) - end SymbolicDiscreteCallback( - namespace_condition(condition(cb), s), namespace_affects(affects(cb)), - reinitializealg = cb.reinitializealg, initialize = namespace_affects(initialize_affects(cb)), - finalize = namespace_affects(finalize_affects(cb))) + namespace_conditions(conditions(cb), s), + namespace_affects(affects(cb), s), + initialize = namespace_affects(initialize_affects(cb), s), + finalize = namespace_affects(finalize_affects(cb), s), reinitializealg = cb.reinitializealg) end -SymbolicDiscreteCallbacks(cb::Pair) = SymbolicDiscreteCallback[SymbolicDiscreteCallback(cb)] -SymbolicDiscreteCallbacks(cbs::Vector) = SymbolicDiscreteCallback.(cbs) -SymbolicDiscreteCallbacks(cb::SymbolicDiscreteCallback) = [cb] -SymbolicDiscreteCallbacks(cbs::Vector{<:SymbolicDiscreteCallback}) = cbs -SymbolicDiscreteCallbacks(::Nothing) = SymbolicDiscreteCallback[] +function Base.hash(cb::AbstractCallback, s::UInt) + s = conditions(cb) isa AbstractVector ? foldr(hash, conditions(cb), init = s) : + hash(conditions(cb), s) + s = hash(affects(cb), s) + !is_discrete(cb) && (s = hash(affect_negs(cb), s)) + s = hash(initialize_affects(cb), s) + s = hash(finalize_affects(cb), s) + !is_discrete(cb) && (s = hash(cb.rootfind, s)) + hash(cb.reinitializealg, s) +end -""" - discrete_events(sys::AbstractSystem) :: Vector{SymbolicDiscreteCallback} +########################### +######### Helpers ######### +########################### -Returns a vector of all the `discrete_events` in an abstract system and its component subsystems. -The `SymbolicDiscreteCallback`s in the returned vector are structs with two fields: `condition` and -`affect` which correspond to the first and second elements of a `Pair` used to define an event, i.e. -`condition => affect`. -""" -function discrete_events(sys::AbstractSystem) - cbs = get_discrete_events(sys) - systems = get_systems(sys) - cbs = [cbs; - reduce(vcat, - (map(cb -> namespace_callback(cb, s), discrete_events(s)) for s in systems), - init = SymbolicDiscreteCallback[])] - cbs +conditions(cb::AbstractCallback) = cb.conditions +function conditions(cbs::Vector{<:AbstractCallback}) + reduce(vcat, conditions(cb) for cb in cbs; init = []) end +equations(cb::AbstractCallback) = conditions(cb) +equations(cb::Vector{<:AbstractCallback}) = conditions(cb) -function vars!(vars, cb::SymbolicDiscreteCallback; op = Differential) - if symbolic_type(cb.condition) == NotSymbolic - if cb.condition isa AbstractArray - for eq in cb.condition - vars!(vars, eq; op) - end - end - else - vars!(vars, cb.condition; op) - end - for aff in (cb.affects, cb.initialize, cb.finalize) - if aff isa Vector{Equation} - for eq in aff - vars!(vars, eq; op) - end - elseif aff !== nothing - vars!(vars, aff; op) - end - end - return vars +affects(cb::AbstractCallback) = cb.affect +function affects(cbs::Vector{<:AbstractCallback}) + reduce(vcat, affects(cb) for cb in cbs; init = []) end -""" - discrete_events_toplevel(sys::AbstractSystem) - -Replicates the behaviour of `discrete_events`, but ignores events of subsystems. - -Notes: -- Cannot be applied to non-complete systems. -""" -function discrete_events_toplevel(sys::AbstractSystem) - if has_parent(sys) && (parent = get_parent(sys)) !== nothing - return discrete_events_toplevel(parent) - end - return get_discrete_events(sys) +affect_negs(cb::SymbolicContinuousCallback) = cb.affect_neg +function affect_negs(cbs::Vector{SymbolicContinuousCallback}) + reduce(vcat, affect_negs(cb) for cb in cbs; init = []) end -################################# compilation functions #################################### - -# handles ensuring that affect! functions work with integrator arguments -function add_integrator_header( - sys::AbstractSystem, integrator = gensym(:MTKIntegrator), out = :u) - expr -> Func([DestructuredArgs(expr.args, integrator, inds = [:u, :p, :t])], [], - expr.body), - expr -> Func( - [DestructuredArgs(expr.args, integrator, inds = [out, :u, :p, :t])], [], - expr.body) +initialize_affects(cb::AbstractCallback) = cb.initialize +function initialize_affects(cbs::Vector{<:AbstractCallback}) + reduce(initialize_affects, vcat, cbs; init = []) end -function condition_header(sys::AbstractSystem, integrator = gensym(:MTKIntegrator)) - expr -> Func( - [expr.args[1], expr.args[2], - DestructuredArgs(expr.args[3:end], integrator, inds = [:p])], - [], - expr.body) +finalize_affects(cb::AbstractCallback) = cb.finalize +function finalize_affects(cbs::Vector{<:AbstractCallback}) + reduce(finalize_affects, vcat, cbs; init = []) end -function callback_save_header(sys::AbstractSystem, cb) - if !(has_index_cache(sys) && (ic = get_index_cache(sys)) !== nothing) - return (identity, identity) - end - save_idxs = get(ic.callback_to_clocks, cb, Int[]) - isempty(save_idxs) && return (identity, identity) - - wrapper = function (expr) - return Func(expr.args, [], - LiteralExpr(quote - $(expr.body) - save_idxs = $(save_idxs) - for idx in save_idxs - $(SciMLBase.save_discretes!)($(expr.args[1]), idx) - end - end)) - end - - return wrapper, wrapper +function Base.:(==)(e1::AbstractCallback, e2::AbstractCallback) + (is_discrete(e1) === is_discrete(e2)) || return false + (isequal(e1.conditions, e2.conditions) && isequal(e1.affect, e2.affect) && + isequal(e1.initialize, e2.initialize) && isequal(e1.finalize, e2.finalize)) && + isequal(e1.reinitializealg, e2.reinitializealg) || + return false + is_discrete(e1) || + (isequal(e1.affect_neg, e2.affect_neg) && isequal(e1.rootfind, e2.rootfind)) end -""" - compile_condition(cb::SymbolicDiscreteCallback, sys, dvs, ps; expression, kwargs...) - -Returns a function `condition(u,t,integrator)` returning the `condition(cb)`. +Base.isempty(cb::AbstractCallback) = isempty(cb.conditions) -Notes +#################################### +####### Compilation functions ###### +#################################### +""" + compile_condition(cb::AbstractCallback, sys, dvs, ps; expression, kwargs...) - - `expression = Val{true}`, causes the generated function to be returned as an expression. - If set to `Val{false}` a `RuntimeGeneratedFunction` will be returned. - - `kwargs` are passed through to `Symbolics.build_function`. +Returns a function `condition(u,t,integrator)`, condition(out,u,t,integrator)` returning the `condition(cb)`. """ -function compile_condition(cb::SymbolicDiscreteCallback, sys, dvs, ps; - expression = Val{true}, eval_expression = false, eval_module = @__MODULE__, kwargs...) +function compile_condition( + cbs::Union{AbstractCallback, Vector{<:AbstractCallback}}, sys, dvs, ps; + eval_expression = false, eval_module = @__MODULE__, kwargs...) u = map(value, dvs) p = map.(value, reorder_parameters(sys, ps)) t = get_iv(sys) - condit = condition(cb) + condit = conditions(cbs) cs = collect_constants(condit) if !isempty(cs) cmap = map(x -> x => getdefault(x), cs) - condit = substitute(condit, cmap) + condit = substitute(condit, Dict(cmap)) end - expr = build_function_wrapper(sys, - condit, u, t, p...; expression = Val{true}, - p_start = 3, p_end = length(p) + 2, - wrap_code = condition_header(sys), - kwargs...) - if expression == Val{true} - return expr - end - return eval_or_rgf(expr; eval_expression, eval_module) -end - -function compile_affect(cb::SymbolicContinuousCallback, args...; kwargs...) - compile_affect(affects(cb), cb, args...; kwargs...) -end -""" - compile_affect(eqs::Vector{Equation}, sys, dvs, ps; expression, outputidxs, kwargs...) - compile_affect(cb::SymbolicContinuousCallback, args...; kwargs...) - -Returns a function that takes an integrator as argument and modifies the state with the -affect. The generated function has the signature `affect!(integrator)`. + if !is_discrete(cbs) + condit = reduce(vcat, flatten_equations(condit)) + condit = condit isa AbstractVector ? [c.lhs - c.rhs for c in condit] : + [condit.lhs - condit.rhs] + end -Notes + fs = build_function_wrapper( + sys, condit, u, p..., t; kwargs..., expression = Val{false}, cse = false) + (f_oop, f_iip) = is_discrete(cbs) ? (fs, nothing) : fs - - `expression = Val{true}`, causes the generated function to be returned as an expression. - If set to `Val{false}` a `RuntimeGeneratedFunction` will be returned. - - `outputidxs`, a vector of indices of the output variables which should correspond to - `unknowns(sys)`. If provided, checks that the LHS of affect equations are variables are - dropped, i.e. it is assumed these indices are correct and affect equations are - well-formed. - - `kwargs` are passed through to `Symbolics.build_function`. -""" -function compile_affect(eqs::Vector{Equation}, cb, sys, dvs, ps; outputidxs = nothing, - expression = Val{true}, checkvars = true, eval_expression = false, - eval_module = @__MODULE__, - postprocess_affect_expr! = nothing, kwargs...) - if isempty(eqs) - if expression == Val{true} - return :((args...) -> ()) - else - return (args...) -> () # We don't do anything in the callback, we're just after the event - end + cond = if cbs isa AbstractVector + (out, u, t, integ) -> f_iip(out, u, parameter_values(integ), t) + elseif is_discrete(cbs) + (u, t, integ) -> f_oop(u, parameter_values(integ), t) else - eqs = flatten_equations(eqs) - rhss = map(x -> x.rhs, eqs) - outvar = :u - if outputidxs === nothing - lhss = map(x -> x.lhs, eqs) - all(isvariable, lhss) || - error("Non-variable symbolic expression found on the left hand side of an affect equation. Such equations must be of the form variable ~ symbolic expression for the new value of the variable.") - update_vars = collect(Iterators.flatten(map(ModelingToolkit.vars, lhss))) # these are the ones we're changing - length(update_vars) == length(unique(update_vars)) == length(eqs) || - error("affected variables not unique, each unknown can only be affected by one equation for a single `root_eqs => affects` pair.") - alleq = all(isequal(isparameter(first(update_vars))), - Iterators.map(isparameter, update_vars)) - if !isparameter(first(lhss)) && alleq - unknownind = Dict(reverse(en) for en in enumerate(dvs)) - update_inds = map(sym -> unknownind[sym], update_vars) - elseif isparameter(first(lhss)) && alleq - if has_index_cache(sys) && get_index_cache(sys) !== nothing - update_inds = map(update_vars) do sym - return parameter_index(sys, sym) - end - else - psind = Dict(reverse(en) for en in enumerate(ps)) - update_inds = map(sym -> psind[sym], update_vars) - end - outvar = :p + function (u, t, integ) + if DiffEqBase.isinplace(SciMLBase.get_sol(integ).prob) + tmp, = DiffEqBase.get_tmp_cache(integ) + f_iip(tmp, u, parameter_values(integ), t) + tmp[1] else - error("Error, building an affect function for a callback that wants to modify both parameters and unknowns. This is not currently allowed in one individual callback.") + f_oop(u, parameter_values(integ), t) end - else - update_inds = outputidxs - end - - _ps = ps - ps = reorder_parameters(sys, ps) - if checkvars - u = map(value, dvs) - p = map.(value, ps) - else - u = dvs - p = ps - end - t = get_iv(sys) - integ = gensym(:MTKIntegrator) - rf_oop, rf_ip = build_function_wrapper( - sys, rhss, u, p..., t; expression = Val{true}, - wrap_code = callback_save_header(sys, cb) .∘ - add_integrator_header(sys, integ, outvar), - outputidxs = update_inds, - create_bindings = false, - kwargs..., cse = false) - # applied user-provided function to the generated expression - if postprocess_affect_expr! !== nothing - postprocess_affect_expr!(rf_ip, integ) - end - if expression == Val{false} - return eval_or_rgf(rf_ip; eval_expression, eval_module) end - return rf_ip end end -function generate_rootfinding_callback(sys::AbstractTimeDependentSystem, - dvs = unknowns(sys), ps = parameters(sys; initial_parameters = true); kwargs...) - cbs = continuous_events(sys) - isempty(cbs) && return nothing - generate_rootfinding_callback(cbs, sys, dvs, ps; kwargs...) -end """ -Generate a single rootfinding callback; this happens if there is only one equation in `cbs` passed to -generate_rootfinding_callback and thus we can produce a ContinuousCallback instead of a VectorContinuousCallback. +Compile user-defined functional affect. """ -function generate_single_rootfinding_callback( - eq, cb, sys::AbstractTimeDependentSystem, dvs = unknowns(sys), - ps = parameters(sys; initial_parameters = true); kwargs...) - if !isequal(eq.lhs, 0) - eq = 0 ~ eq.lhs - eq.rhs - end +function compile_functional_affect(affect::FunctionalAffect, sys; kwargs...) + dvs = unknowns(sys) + ps = parameters(sys) + dvs_ind = Dict(reverse(en) for en in enumerate(dvs)) + v_inds = map(sym -> dvs_ind[sym], unknowns(affect)) - rf_oop, rf_ip = generate_custom_function( - sys, [eq.rhs], dvs, ps; expression = Val{false}, kwargs..., cse = false) - affect_function = compile_affect_fn(cb, sys, dvs, ps, kwargs) - cond = function (u, t, integ) - if DiffEqBase.isinplace(integ.sol.prob) - tmp, = DiffEqBase.get_tmp_cache(integ) - rf_ip(tmp, u, parameter_values(integ), t) - tmp[1] - else - rf_oop(u, parameter_values(integ), t) - end - end - user_initfun = isnothing(affect_function.initialize) ? SciMLBase.INITIALIZE_DEFAULT : - (c, u, t, i) -> affect_function.initialize(i) - if has_index_cache(sys) && (ic = get_index_cache(sys)) !== nothing && - (save_idxs = get(ic.callback_to_clocks, cb, nothing)) !== nothing - initfn = let save_idxs = save_idxs - function (cb, u, t, integrator) - user_initfun(cb, u, t, integrator) - for idx in save_idxs - SciMLBase.save_discretes!(integrator, idx) - end - end - end + if has_index_cache(sys) && (ic = get_index_cache(sys)) !== nothing + p_inds = [(pind = parameter_index(sys, sym)) === nothing ? sym : pind + for sym in parameters(affect)] else - initfn = user_initfun + ps_ind = Dict(reverse(en) for en in enumerate(ps)) + p_inds = map(sym -> get(ps_ind, sym, sym), parameters(affect)) end + # HACK: filter out eliminated symbols. Not clear this is the right thing to do + # (MTK should keep these symbols) + u = filter(x -> !isnothing(x[2]), collect(zip(unknowns_syms(affect), v_inds))) |> + NamedTuple + p = filter(x -> !isnothing(x[2]), collect(zip(parameters_syms(affect), p_inds))) |> + NamedTuple - return ContinuousCallback( - cond, affect_function.affect, affect_function.affect_neg, rootfind = cb.rootfind, - initialize = initfn, - finalize = isnothing(affect_function.finalize) ? SciMLBase.FINALIZE_DEFAULT : - (c, u, t, i) -> affect_function.finalize(i), - initializealg = reinitialization_alg(cb)) + let u = u, p = p, user_affect = func(affect), ctx = context(affect) + (integ) -> begin + user_affect(integ, u, p, ctx) + end + end end -function generate_vector_rootfinding_callback( - cbs, sys::AbstractTimeDependentSystem, dvs = unknowns(sys), - ps = parameters(sys; initial_parameters = true); rootfind = SciMLBase.RightRootFind, - reinitialization = SciMLBase.CheckInit(), kwargs...) - eqs = map(cb -> flatten_equations(cb.eqs), cbs) - num_eqs = length.(eqs) - # fuse equations to create VectorContinuousCallback - eqs = reduce(vcat, eqs) - # rewrite all equations as 0 ~ interesting stuff - eqs = map(eqs) do eq - isequal(eq.lhs, 0) && return eq - 0 ~ eq.lhs - eq.rhs - end +is_discrete(cb::AbstractCallback) = cb isa SymbolicDiscreteCallback +is_discrete(cb::Vector{<:AbstractCallback}) = eltype(cb) isa SymbolicDiscreteCallback - rhss = map(x -> x.rhs, eqs) - _, rf_ip = generate_custom_function( - sys, rhss, dvs, ps; expression = Val{false}, kwargs..., cse = false) - - affect_functions = @NamedTuple{ - affect::Function, - affect_neg::Union{Function, Nothing}, - initialize::Union{Function, Nothing}, - finalize::Union{Function, Nothing}}[ - compile_affect_fn(cb, sys, dvs, ps, kwargs) - for cb in cbs] - cond = function (out, u, t, integ) - rf_ip(out, u, parameter_values(integ), t) - end +function generate_continuous_callbacks(sys::AbstractSystem, dvs = unknowns(sys), + ps = parameters(sys; initial_parameters = true); kwargs...) + cbs = continuous_events(sys) + isempty(cbs) && return nothing + cb_classes = Dict{Tuple{SciMLBase.RootfindOpt, SciMLBase.DAEInitializationAlgorithm}, + Vector{SymbolicContinuousCallback}}() - # since there may be different number of conditions and affects, - # we build a map that translates the condition eq. number to the affect number - eq_ind2affect = reduce(vcat, - [fill(i, num_eqs[i]) for i in eachindex(affect_functions)]) - @assert length(eq_ind2affect) == length(eqs) - @assert maximum(eq_ind2affect) == length(affect_functions) - - affect = let affect_functions = affect_functions, eq_ind2affect = eq_ind2affect - function (integ, eq_ind) # eq_ind refers to the equation index that triggered the event, each event has num_eqs[i] equations - affect_functions[eq_ind2affect[eq_ind]].affect(integ) - end - end - affect_neg = let affect_functions = affect_functions, eq_ind2affect = eq_ind2affect - function (integ, eq_ind) # eq_ind refers to the equation index that triggered the event, each event has num_eqs[i] equations - affect_neg = affect_functions[eq_ind2affect[eq_ind]].affect_neg - if isnothing(affect_neg) - return # skip if the neg function doesn't exist - don't want to split this into a separate VCC because that'd break ordering - end - affect_neg(integ) - end - end - function handle_optional_setup_fn(funs, default) - if all(isnothing, funs) - return default - else - return let funs = funs - function (cb, u, t, integ) - for func in funs - if isnothing(func) - continue - else - func(integ) - end - end - end - end - end + # Sort the callbacks by their rootfinding method + for cb in cbs + _cbs = get!(() -> SymbolicContinuousCallback[], + cb_classes, (cb.rootfind, cb.reinitializealg)) + push!(_cbs, cb) end - initialize = nothing - if has_index_cache(sys) && (ic = get_index_cache(sys)) !== nothing - initialize = handle_optional_setup_fn( - map(cbs, affect_functions) do cb, fn - if (save_idxs = get(ic.callback_to_clocks, cb, nothing)) !== nothing - let save_idxs = save_idxs - custom_init = fn.initialize - (i) -> begin - !isnothing(custom_init) && custom_init(i) - for idx in save_idxs - SciMLBase.save_discretes!(i, idx) - end - end - end - else - fn.initialize - end - end, - SciMLBase.INITIALIZE_DEFAULT) - + sort!(OrderedDict(cb_classes), by = cb -> cb[1]) + compiled_callbacks = [generate_callback(cb, sys; kwargs...) + for ((rf, reinit), cb) in cb_classes] + if length(compiled_callbacks) == 1 + return only(compiled_callbacks) else - initialize = handle_optional_setup_fn( - map(fn -> fn.initialize, affect_functions), SciMLBase.INITIALIZE_DEFAULT) + return CallbackSet(compiled_callbacks...) end +end - finalize = handle_optional_setup_fn( - map(fn -> fn.finalize, affect_functions), SciMLBase.FINALIZE_DEFAULT) - return VectorContinuousCallback( - cond, affect, affect_neg, length(eqs), rootfind = rootfind, - initialize = initialize, finalize = finalize, initializealg = reinitialization) +function generate_discrete_callbacks(sys::AbstractSystem, dvs = unknowns(sys), + ps = parameters(sys; initial_parameters = true); kwargs...) + dbs = discrete_events(sys) + isempty(dbs) && return nothing + [generate_callback(db, sys; kwargs...) for db in dbs] end +EMPTY_AFFECT(args...) = nothing + """ -Compile a single continuous callback affect function(s). +Codegen a DifferentialEquations callback. A (set of) continuous callback with multiple equations becomes a VectorContinuousCallback. +Continuous callbacks with only one equation will become a ContinuousCallback. +Individual discrete callbacks become DiscreteCallback, PresetTimeCallback, PeriodicCallback depending on the case. """ -function compile_affect_fn(cb, sys::AbstractTimeDependentSystem, dvs, ps, kwargs) - eq_aff = affects(cb) - eq_neg_aff = affect_negs(cb) - affect = compile_affect(eq_aff, cb, sys, dvs, ps; expression = Val{false}, kwargs...) - function compile_optional_affect(aff, default = nothing) - if isnothing(aff) || aff == default - return nothing - else - return compile_affect(aff, cb, sys, dvs, ps; expression = Val{false}, kwargs...) - end - end - if eq_neg_aff === eq_aff - affect_neg = affect - else - affect_neg = _compile_optional_affect( - NULL_AFFECT, eq_neg_aff, cb, sys, dvs, ps; kwargs...) - end - initialize = _compile_optional_affect( - NULL_AFFECT, initialize_affects(cb), cb, sys, dvs, ps; kwargs...) - finalize = _compile_optional_affect( - NULL_AFFECT, finalize_affects(cb), cb, sys, dvs, ps; kwargs...) - (affect = affect, affect_neg = affect_neg, initialize = initialize, finalize = finalize) -end - -function generate_rootfinding_callback(cbs, sys::AbstractTimeDependentSystem, - dvs = unknowns(sys), ps = parameters(sys; initial_parameters = true); kwargs...) - eqs = map(cb -> flatten_equations(cb.eqs), cbs) +function generate_callback(cbs::Vector{SymbolicContinuousCallback}, sys; kwargs...) + eqs = map(cb -> flatten_equations(equations(cb)), cbs) num_eqs = length.(eqs) - total_eqs = sum(num_eqs) - (isempty(eqs) || total_eqs == 0) && return nothing - if total_eqs == 1 - # find the callback with only one eq + (isempty(eqs) || sum(num_eqs) == 0) && return nothing + if sum(num_eqs) == 1 cb_ind = findfirst(>(0), num_eqs) - if isnothing(cb_ind) - error("Inconsistent state in affect compilation; one equation but no callback with equations?") - end - cb = cbs[cb_ind] - return generate_single_rootfinding_callback(cb.eqs[], cb, sys, dvs, ps; kwargs...) + return generate_callback(cbs[cb_ind], sys; kwargs...) end - # group the cbs by what rootfind op they use - # groupby would be very useful here, but alas - cb_classes = Dict{ - @NamedTuple{ - rootfind::SciMLBase.RootfindOpt, - reinitialization::SciMLBase.DAEInitializationAlgorithm}, Vector{SymbolicContinuousCallback}}() + trigger = compile_condition( + cbs, sys, unknowns(sys), parameters(sys; initial_parameters = true); kwargs...) + affects = [] + affect_negs = [] + inits = [] + finals = [] for cb in cbs - push!( - get!(() -> SymbolicContinuousCallback[], cb_classes, - ( - rootfind = cb.rootfind, - reinitialization = reinitialization_alg(cb))), - cb) + affect = compile_affect(cb.affect, cb, sys; default = EMPTY_AFFECT, kwargs...) + push!(affects, affect) + affect_neg = (cb.affect_neg === cb.affect) ? affect : + compile_affect( + cb.affect_neg, cb, sys; default = EMPTY_AFFECT, kwargs...) + push!(affect_negs, affect_neg) + push!(inits, + compile_affect( + cb.initialize, cb, sys; default = nothing, is_init = true, kwargs...)) + push!(finals, compile_affect(cb.finalize, cb, sys; default = nothing, kwargs...)) end - # generate the callbacks out; we sort by the equivalence class to ensure a deterministic preference order - compiled_callbacks = map(collect(pairs(sort!( - OrderedDict(cb_classes); by = p -> p.rootfind)))) do (equiv_class, cbs_in_class) - return generate_vector_rootfinding_callback( - cbs_in_class, sys, dvs, ps; rootfind = equiv_class.rootfind, - reinitialization = equiv_class.reinitialization, kwargs...) + # Since there may be different number of conditions and affects, + # we build a map that translates the condition eq. number to the affect number + eq2affect = reduce(vcat, + [fill(i, num_eqs[i]) for i in eachindex(affects)]) + eqs = reduce(vcat, eqs) + + affect = let eq2affect = eq2affect, affects = affects + function (integ, idx) + affects[eq2affect[idx]](integ) + end end - if length(compiled_callbacks) == 1 - return compiled_callbacks[] - else - return CallbackSet(compiled_callbacks...) + affect_neg = let eq2affect = eq2affect, affect_negs = affect_negs + function (integ, idx) + f = affect_negs[eq2affect[idx]] + isnothing(f) && return + f(integ) + end end -end + initialize = wrap_vector_optional_affect(inits, SciMLBase.INITIALIZE_DEFAULT) + finalize = wrap_vector_optional_affect(finals, SciMLBase.FINALIZE_DEFAULT) -function compile_user_affect(affect::FunctionalAffect, cb, sys, dvs, ps; kwargs...) - dvs_ind = Dict(reverse(en) for en in enumerate(dvs)) - v_inds = map(sym -> dvs_ind[sym], unknowns(affect)) + return VectorContinuousCallback( + trigger, affect, affect_neg, length(eqs); initialize, finalize, + rootfind = cbs[1].rootfind, initializealg = cbs[1].reinitializealg) +end - if has_index_cache(sys) && get_index_cache(sys) !== nothing - p_inds = [if (pind = parameter_index(sys, sym)) === nothing - sym - else - pind - end - for sym in parameters(affect)] - else - ps_ind = Dict(reverse(en) for en in enumerate(ps)) - p_inds = map(sym -> get(ps_ind, sym, sym), parameters(affect)) - end - # HACK: filter out eliminated symbols. Not clear this is the right thing to do - # (MTK should keep these symbols) - u = filter(x -> !isnothing(x[2]), collect(zip(unknowns_syms(affect), v_inds))) |> - NamedTuple - p = filter(x -> !isnothing(x[2]), collect(zip(parameters_syms(affect), p_inds))) |> - NamedTuple +function generate_callback(cb, sys; kwargs...) + is_timed = is_timed_condition(conditions(cb)) + dvs = unknowns(sys) + ps = parameters(sys; initial_parameters = true) - if has_index_cache(sys) && (ic = get_index_cache(sys)) !== nothing - save_idxs = get(ic.callback_to_clocks, cb, Int[]) + trigger = is_timed ? conditions(cb) : compile_condition(cb, sys, dvs, ps; kwargs...) + affect = compile_affect(cb.affect, cb, sys; default = EMPTY_AFFECT, kwargs...) + affect_neg = if is_discrete(cb) + nothing else - save_idxs = Int[] + (cb.affect === cb.affect_neg) ? affect : + compile_affect(cb.affect_neg, cb, sys; default = EMPTY_AFFECT, kwargs...) end - let u = u, p = p, user_affect = func(affect), ctx = context(affect), - save_idxs = save_idxs - - function (integ) - user_affect(integ, u, p, ctx) - for idx in save_idxs - SciMLBase.save_discretes!(integ, idx) - end + init = compile_affect(cb.initialize, cb, sys; default = SciMLBase.INITIALIZE_DEFAULT, + is_init = true, kwargs...) + final = compile_affect( + cb.finalize, cb, sys; default = SciMLBase.FINALIZE_DEFAULT, kwargs...) + + initialize = isnothing(cb.initialize) ? init : ((c, u, t, i) -> init(i)) + finalize = isnothing(cb.finalize) ? final : ((c, u, t, i) -> final(i)) + + if is_discrete(cb) + if is_timed && conditions(cb) isa AbstractVector + return PresetTimeCallback(trigger, affect; initialize, + finalize, initializealg = cb.reinitializealg) + elseif is_timed + return PeriodicCallback( + affect, trigger; initialize, finalize, initializealg = cb.reinitializealg) + else + return DiscreteCallback(trigger, affect; initialize, + finalize, initializealg = cb.reinitializealg) end + else + return ContinuousCallback(trigger, affect, affect_neg; initialize, finalize, + rootfind = cb.rootfind, initializealg = cb.reinitializealg) end end -function invalid_variables(sys, expr) - filter(x -> !any(isequal(x), all_symbols(sys)), reduce(vcat, vars(expr); init = [])) -end -function unassignable_variables(sys, expr) - assignable_syms = reduce( - vcat, Symbolics.scalarize.(vcat( - unknowns(sys), parameters(sys; initial_parameters = true))); - init = []) - written = reduce(vcat, Symbolics.scalarize.(vars(expr)); init = []) - return filter( - x -> !any(isequal(x), assignable_syms), written) -end +""" + compile_affect(cb::AbstractCallback, sys::AbstractSystem, dvs, ps; expression, outputidxs, kwargs...) -@generated function _generated_writeback(integ, setters::NamedTuple{NS1, <:Tuple}, - values::NamedTuple{NS2, <:Tuple}) where {NS1, NS2} - setter_exprs = [] - for name in NS2 - if !(name in NS1) - missing_name = "Tried to write back to $name from affect; only declared states ($NS1) may be written to." - error(missing_name) - end - push!(setter_exprs, :(setters.$name(integ, values.$name))) - end - return :(begin - $(setter_exprs...) - end) -end +Returns a function that takes an integrator as argument and modifies the state with the +affect. The generated function has the signature `affect!(integrator)`. -function check_assignable(sys, sym) - if symbolic_type(sym) == ScalarSymbolic() - is_variable(sys, sym) || is_parameter(sys, sym) - elseif symbolic_type(sym) == ArraySymbolic() - is_variable(sys, sym) || is_parameter(sys, sym) || - all(x -> check_assignable(sys, x), collect(sym)) - elseif sym isa Union{AbstractArray, Tuple} - all(x -> check_assignable(sys, x), sym) +Notes + - `kwargs` are passed through to `Symbolics.build_function`. +""" +function compile_affect( + aff::Union{Nothing, Affect}, cb::AbstractCallback, sys::AbstractSystem; + default = nothing, is_init = false, kwargs...) + save_idxs = if !(has_index_cache(sys) && (ic = get_index_cache(sys)) !== nothing) + Int[] else - false + get(ic.callback_to_clocks, cb, Int[]) end -end -function compile_affect(affect::FunctionalAffect, cb, sys, dvs, ps; kwargs...) - compile_user_affect(affect, cb, sys, dvs, ps; kwargs...) -end -function _compile_optional_affect(default, aff, cb, sys, dvs, ps; kwargs...) - if isnothing(aff) || aff == default - return nothing - else - return compile_affect(aff, cb, sys, dvs, ps; expression = Val{false}, kwargs...) + if isnothing(aff) + is_init ? wrap_save_discretes(default, save_idxs) : default + elseif aff isa AffectSystem + f = compile_equational_affect(aff, sys; kwargs...) + wrap_save_discretes(f, save_idxs) + elseif aff isa FunctionalAffect || aff isa ImperativeAffect + f = compile_functional_affect(aff, sys; kwargs...) + wrap_save_discretes(f, save_idxs) end end -function generate_timed_callback(cb, sys, dvs, ps; postprocess_affect_expr! = nothing, - kwargs...) - cond = condition(cb) - as = compile_affect(affects(cb), cb, sys, dvs, ps; expression = Val{false}, - postprocess_affect_expr!, kwargs...) - - user_initfun = _compile_optional_affect( - NULL_AFFECT, initialize_affects(cb), cb, sys, dvs, ps; kwargs...) - user_finfun = _compile_optional_affect( - NULL_AFFECT, finalize_affects(cb), cb, sys, dvs, ps; kwargs...) - if has_index_cache(sys) && (ic = get_index_cache(sys)) !== nothing && - (save_idxs = get(ic.callback_to_clocks, cb, nothing)) !== nothing - initfn = let - save_idxs = save_idxs - initfun = user_initfun - function (cb, u, t, integrator) - if !isnothing(initfun) - initfun(integrator) + +function wrap_save_discretes(f, save_idxs) + let save_idxs = save_idxs, f = f + if f === SciMLBase.INITIALIZE_DEFAULT + (c, u, t, i) -> begin + f(c, u, t, i) + for idx in save_idxs + SciMLBase.save_discretes!(i, idx) end + end + else + (i) -> begin + isnothing(f) || f(i) for idx in save_idxs - SciMLBase.save_discretes!(integrator, idx) + SciMLBase.save_discretes!(i, idx) end end end - else - initfn = isnothing(user_initfun) ? SciMLBase.INITIALIZE_DEFAULT : - (_, _, _, i) -> user_initfun(i) - end - finfun = isnothing(user_finfun) ? SciMLBase.FINALIZE_DEFAULT : - (_, _, _, i) -> user_finfun(i) - if cond isa AbstractVector - # Preset Time - return PresetTimeCallback( - cond, as; initialize = initfn, finalize = finfun, - initializealg = reinitialization_alg(cb)) - else - # Periodic - return PeriodicCallback( - as, cond; initialize = initfn, finalize = finfun, - initializealg = reinitialization_alg(cb)) end end -function generate_discrete_callback(cb, sys, dvs, ps; postprocess_affect_expr! = nothing, - kwargs...) - if is_timed_condition(cb) - return generate_timed_callback(cb, sys, dvs, ps; postprocess_affect_expr!, - kwargs...) - else - c = compile_condition(cb, sys, dvs, ps; expression = Val{false}, kwargs...) - as = compile_affect(affects(cb), cb, sys, dvs, ps; expression = Val{false}, - postprocess_affect_expr!, kwargs...) - - user_initfun = _compile_optional_affect( - NULL_AFFECT, initialize_affects(cb), cb, sys, dvs, ps; kwargs...) - user_finfun = _compile_optional_affect( - NULL_AFFECT, finalize_affects(cb), cb, sys, dvs, ps; kwargs...) - if has_index_cache(sys) && (ic = get_index_cache(sys)) !== nothing && - (save_idxs = get(ic.callback_to_clocks, cb, nothing)) !== nothing - initfn = let save_idxs = save_idxs, initfun = user_initfun - function (cb, u, t, integrator) - if !isnothing(initfun) - initfun(integrator) - end - for idx in save_idxs - SciMLBase.save_discretes!(integrator, idx) - end - end +""" +Initialize and finalize for VectorContinuousCallback. +""" +function wrap_vector_optional_affect(funs, default) + all(isnothing, funs) && return default + return let funs = funs + function (cb, u, t, integ) + for func in funs + isnothing(func) ? continue : func(integ) end - else - initfn = isnothing(user_initfun) ? SciMLBase.INITIALIZE_DEFAULT : - (_, _, _, i) -> user_initfun(i) end - finfun = isnothing(user_finfun) ? SciMLBase.FINALIZE_DEFAULT : - (_, _, _, i) -> user_finfun(i) - return DiscreteCallback( - c, as; initialize = initfn, finalize = finfun, - initializealg = reinitialization_alg(cb)) end end -function generate_discrete_callbacks(sys::AbstractSystem, dvs = unknowns(sys), - ps = parameters(sys; initial_parameters = true); kwargs...) - has_discrete_events(sys) || return nothing - symcbs = discrete_events(sys) - isempty(symcbs) && return nothing +function add_integrator_header( + sys::AbstractSystem, integrator = gensym(:MTKIntegrator), out = :u) + expr -> Func([DestructuredArgs(expr.args, integrator, inds = [:u, :p, :t])], [], + expr.body), + expr -> Func( + [DestructuredArgs(expr.args, integrator, inds = [out, :u, :p, :t])], [], + expr.body) +end + +""" +Compile an affect defined by a set of equations. Systems with algebraic equations will solve implicit discrete problems to obtain their next state. Systems without will generate functions that perform explicit updates. +""" +function compile_equational_affect( + aff::Union{AffectSystem, Vector{Equation}}, sys; reset_jumps = false, kwargs...) + if aff isa AbstractVector + aff = make_affect( + aff; iv = get_iv(sys), warn_no_algebraic = false) + end + affsys = system(aff) + ps_to_update = discretes(aff) + dvs_to_update = setdiff(unknowns(aff), getfield.(observed(sys), :lhs)) + aff_map = aff_to_sys(aff) + sys_map = Dict([v => k for (k, v) in aff_map]) + + if isempty(equations(affsys)) + update_eqs = Symbolics.fast_substitute( + observed(affsys), Dict([p => unPre(p) for p in parameters(affsys)])) + rhss = map(x -> x.rhs, update_eqs) + lhss = map(x -> aff_map[x.lhs], update_eqs) + is_p = [lhs ∈ Set(ps_to_update) for lhs in lhss] + is_u = [lhs ∈ Set(dvs_to_update) for lhs in lhss] + dvs = unknowns(sys) + ps = parameters(sys) + t = get_iv(sys) + + u_idxs = indexin((@view lhss[is_u]), dvs) + + wrap_mtkparameters = has_index_cache(sys) && (get_index_cache(sys) !== nothing) + p_idxs = if wrap_mtkparameters + [parameter_index(sys, p) for (i, p) in enumerate(lhss) + if is_p[i]] + else + indexin((@view lhss[is_p]), ps) + end + _ps = reorder_parameters(sys, ps) + integ = gensym(:MTKIntegrator) + + u_up, u_up! = build_function_wrapper(sys, (@view rhss[is_u]), dvs, _ps..., t; + wrap_code = add_integrator_header(sys, integ, :u), + expression = Val{false}, outputidxs = u_idxs, wrap_mtkparameters, cse = false) + p_up, p_up! = build_function_wrapper(sys, (@view rhss[is_p]), dvs, _ps..., t; + wrap_code = add_integrator_header(sys, integ, :p), + expression = Val{false}, outputidxs = p_idxs, wrap_mtkparameters, cse = false) + + return let dvs_to_update = dvs_to_update, ps_to_update = ps_to_update, + reset_jumps = reset_jumps, u_up! = u_up!, p_up! = p_up! - dbs = map(symcbs) do cb - generate_discrete_callback(cb, sys, dvs, ps; kwargs...) + function explicit_affect!(integ) + isempty(dvs_to_update) || u_up!(integ) + isempty(ps_to_update) || p_up!(integ) + reset_jumps && reset_aggregated_jumps!(integ) + end + end + else + return let dvs_to_update = dvs_to_update, aff_map = aff_map, sys_map = sys_map, + affsys = affsys, ps_to_update = ps_to_update, aff = aff, sys = sys + + dvs_to_access = [aff_map[u] for u in unknowns(affsys)] + ps_to_access = [unPre(p) for p in parameters(affsys)] + + affu_getter = getsym(sys, dvs_to_access) + affp_getter = getsym(sys, ps_to_access) + affu_setter! = setsym(affsys, unknowns(affsys)) + affp_setter! = setsym(affsys, parameters(affsys)) + u_setter! = setsym(sys, dvs_to_update) + p_setter! = setsym(sys, ps_to_update) + u_getter = getsym(affsys, [sys_map[u] for u in dvs_to_update]) + p_getter = getsym(affsys, [sys_map[p] for p in ps_to_update]) + + affprob = ImplicitDiscreteProblem(affsys, [dv => 0 for dv in unknowns(affsys)], + (0, 0), [p => 0.0 for p in parameters(affsys)]; + build_initializeprob = false, check_length = false) + + function implicit_affect!(integ) + new_u0 = affu_getter(integ) + affu_setter!(affprob, new_u0) + new_ps = affp_getter(integ) + affp_setter!(affprob, new_ps) + + affprob = remake( + affprob, tspan = (integ.t, integ.t)) + affsol = init(affprob, IDSolve()) + (check_error(affsol) === ReturnCode.InitialFailure) && + throw(UnsolvableCallbackError(all_equations(aff))) + + u_setter!(integ, u_getter(affsol)) + p_setter!(integ, p_getter(affsol)) + end + end end +end - dbs +struct UnsolvableCallbackError + eqs::Vector{Equation} +end + +function Base.showerror(io::IO, err::UnsolvableCallbackError) + println(io, + "The callback defined by the following equations:\n\n$(join(err.eqs, "\n"))\n\nis not solvable. Please check that the algebraic equations and affect equations are correct, and that all parameters intended to be changed are passed in as `discrete_parameters`.") end merge_cb(::Nothing, ::Nothing) = nothing @@ -1124,18 +969,109 @@ merge_cb(::Nothing, x) = merge_cb(x, nothing) merge_cb(x, ::Nothing) = x merge_cb(x, y) = CallbackSet(x, y) +""" +Generate the CallbackSet for a ODESystem or SDESystem. +""" function process_events(sys; callback = nothing, kwargs...) - if has_continuous_events(sys) && !isempty(continuous_events(sys)) - contin_cb = generate_rootfinding_callback(sys; kwargs...) - else - contin_cb = nothing + contin_cbs = generate_continuous_callbacks(sys; kwargs...) + discrete_cbs = generate_discrete_callbacks(sys; kwargs...) + cb = merge_cb(contin_cbs, callback) + (discrete_cbs === nothing) ? cb : CallbackSet(contin_cbs, discrete_cbs...) +end + +""" + discrete_events(sys::AbstractSystem) :: Vector{SymbolicDiscreteCallback} + +Returns a vector of all the `discrete_events` in an abstract system and its component subsystems. +The `SymbolicDiscreteCallback`s in the returned vector are structs with two fields: `condition` and +`affect` which correspond to the first and second elements of a `Pair` used to define an event, i.e. +`condition => affect`. + +See also `get_discrete_events`, which only returns the events of the top-level system. +""" +function discrete_events(sys::AbstractSystem) + obs = get_discrete_events(sys) + systems = get_systems(sys) + cbs = [obs; + reduce(vcat, + (map(cb -> namespace_callback(cb, s), discrete_events(s)) for s in systems), + init = SymbolicDiscreteCallback[])] + cbs +end + +has_discrete_events(sys::AbstractSystem) = isdefined(sys, :discrete_events) +function get_discrete_events(sys::AbstractSystem) + has_discrete_events(sys) || return SymbolicDiscreteCallback[] + getfield(sys, :discrete_events) +end + +""" + discrete_events_toplevel(sys::AbstractSystem) + +Replicates the behaviour of `discrete_events`, but ignores events of subsystems. + +Notes: +- Cannot be applied to non-complete systems. +""" +function discrete_events_toplevel(sys::AbstractSystem) + if has_parent(sys) && (parent = get_parent(sys)) !== nothing + return discrete_events_toplevel(parent) end - if has_discrete_events(sys) && !isempty(discrete_events(sys)) - discrete_cb = generate_discrete_callbacks(sys; kwargs...) - else - discrete_cb = nothing + return get_discrete_events(sys) +end + +""" + continuous_events(sys::AbstractSystem)::Vector{SymbolicContinuousCallback} + +Returns a vector of all the `continuous_events` in an abstract system and its component subsystems. +The `SymbolicContinuousCallback`s in the returned vector are structs with two fields: `eqs` and +`affect` which correspond to the first and second elements of a `Pair` used to define an event, i.e. +`eqs => affect`. + +See also `get_continuous_events`, which only returns the events of the top-level system. +""" +function continuous_events(sys::AbstractSystem) + obs = get_continuous_events(sys) + filter(!isempty, obs) + + systems = get_systems(sys) + cbs = [obs; + reduce(vcat, + (map(o -> namespace_callback(o, s), continuous_events(s)) for s in systems), + init = SymbolicContinuousCallback[])] + filter(!isempty, cbs) +end + +has_continuous_events(sys::AbstractSystem) = isdefined(sys, :continuous_events) +function get_continuous_events(sys::AbstractSystem) + has_continuous_events(sys) || return SymbolicContinuousCallback[] + getfield(sys, :continuous_events) +end + +""" + continuous_events_toplevel(sys::AbstractSystem) + +Replicates the behaviour of `continuous_events`, but ignores events of subsystems. + +Notes: +- Cannot be applied to non-complete systems. +""" +function continuous_events_toplevel(sys::AbstractSystem) + if has_parent(sys) && (parent = get_parent(sys)) !== nothing + return continuous_events_toplevel(parent) end + return get_continuous_events(sys) +end - cb = merge_cb(contin_cb, callback) - (discrete_cb === nothing) ? cb : CallbackSet(contin_cb, discrete_cb...) +""" +Process the symbolic events of a system. +""" +function create_symbolic_events(cont_events, disc_events, sys_eqs, iv) + alg_eqs = filter(eq -> eq.lhs isa Union{Symbolic, Number} && !is_diff_equation(eq), + sys_eqs) + cont_callbacks = to_cb_vector(cont_events; CB_TYPE = SymbolicContinuousCallback, + iv = iv, alg_eqs = alg_eqs, warn_no_algebraic = false) + disc_callbacks = to_cb_vector(disc_events; CB_TYPE = SymbolicDiscreteCallback, + iv = iv, alg_eqs = alg_eqs, warn_no_algebraic = false) + cont_callbacks, disc_callbacks end diff --git a/src/systems/imperative_affect.jl b/src/systems/imperative_affect.jl index 9be9536c93..7b05a3ead2 100644 --- a/src/systems/imperative_affect.jl +++ b/src/systems/imperative_affect.jl @@ -122,11 +122,49 @@ function namespace_affect(affect::ImperativeAffect, s) affect.skip_checks) end -function compile_affect(affect::ImperativeAffect, cb, sys, dvs, ps; kwargs...) - compile_user_affect(affect, cb, sys, dvs, ps; kwargs...) +function invalid_variables(sys, expr) + filter(x -> !any(isequal(x), all_symbols(sys)), reduce(vcat, vars(expr); init = [])) end -function compile_user_affect(affect::ImperativeAffect, cb, sys, dvs, ps; kwargs...) +function unassignable_variables(sys, expr) + assignable_syms = reduce( + vcat, Symbolics.scalarize.(vcat( + unknowns(sys), parameters(sys; initial_parameters = true))); + init = []) + written = reduce(vcat, Symbolics.scalarize.(vars(expr)); init = []) + return filter( + x -> !any(isequal(x), assignable_syms), written) +end + +@generated function _generated_writeback(integ, setters::NamedTuple{NS1, <:Tuple}, + values::NamedTuple{NS2, <:Tuple}) where {NS1, NS2} + setter_exprs = [] + for name in NS2 + if !(name in NS1) + missing_name = "Tried to write back to $name from affect; only declared states ($NS1) may be written to." + error(missing_name) + end + push!(setter_exprs, :(setters.$name(integ, values.$name))) + end + return :(begin + $(setter_exprs...) + end) +end + +function check_assignable(sys, sym) + if symbolic_type(sym) == ScalarSymbolic() + is_variable(sys, sym) || is_parameter(sys, sym) + elseif symbolic_type(sym) == ArraySymbolic() + is_variable(sys, sym) || is_parameter(sys, sym) || + all(x -> check_assignable(sys, x), collect(sym)) + elseif sym isa Union{AbstractArray, Tuple} + all(x -> check_assignable(sys, x), sym) + else + false + end +end + +function compile_functional_affect(affect::ImperativeAffect, sys; kwargs...) #= Implementation sketch: generate observed function (oop), should save to a component array under obs_syms @@ -150,6 +188,9 @@ function compile_user_affect(affect::ImperativeAffect, cb, sys, dvs, ps; kwargs. return (syms_dedup, exprs_dedup) end + dvs = unknowns(sys) + ps = parameters(sys) + obs_exprs = observed(affect) if !affect.skip_checks for oexpr in obs_exprs @@ -203,14 +244,8 @@ function compile_user_affect(affect::ImperativeAffect, cb, sys, dvs, ps; kwargs. upd_funs = NamedTuple{mod_names}((setu.((sys,), first.(mod_pairs))...,)) - if has_index_cache(sys) && (ic = get_index_cache(sys)) !== nothing - save_idxs = get(ic.callback_to_clocks, cb, Int[]) - else - save_idxs = Int[] - end - let user_affect = func(affect), ctx = context(affect) - function (integ) + @inline function (integ) # update the to-be-mutated values; this ensures that if you do a no-op then nothing happens modvals = mod_og_val_fun(integ.u, integ.p, integ.t) upd_component_array = NamedTuple{mod_names}(modvals) @@ -224,10 +259,6 @@ function compile_user_affect(affect::ImperativeAffect, cb, sys, dvs, ps; kwargs. # write the new values back to the integrator _generated_writeback(integ, upd_funs, upd_vals) - - for idx in save_idxs - SciMLBase.save_discretes!(integ, idx) - end end end end From f504824f84e6bb63d1657c7ef13829c45bbea847 Mon Sep 17 00:00:00 2001 From: vyudu Date: Mon, 17 Mar 2025 10:06:00 -0400 Subject: [PATCH 1593/2176] build: update Project.toml --- Project.toml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Project.toml b/Project.toml index ca4e79255a..ca03a35ba0 100644 --- a/Project.toml +++ b/Project.toml @@ -31,6 +31,7 @@ ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210" FunctionWrappers = "069b7b12-0de2-55c6-9aab-29f3d0a68a2e" FunctionWrappersWrappers = "77dc65aa-8811-40c2-897b-53d922fa7daf" Graphs = "86223c79-3864-5bf0-83f7-82e725a168b6" +ImplicitDiscreteSolve = "3263718b-31ed-49cf-8a0f-35a466e8af96" InteractiveUtils = "b77e0a4c-d291-57a0-90e8-8db25a27a240" JuliaFormatter = "98e50ef6-434e-11e9-1051-2b60c6c9e899" JumpProcesses = "ccbc3e58-028d-4f4c-8cd5-9ae44345cda5" @@ -43,6 +44,7 @@ NaNMath = "77ba4419-2d1f-58cd-9bb1-8ffee604a2e3" NonlinearSolve = "8913a72c-1f9b-4ce2-8d82-65094dcecaec" OffsetArrays = "6fe1bfb0-de20-5000-8ca7-80f57d26f881" OrderedCollections = "bac558e1-5e72-5ebc-8fee-abe8a469f55d" +OrdinaryDiffEqCore = "bbf590c4-e513-4bbe-9b18-05decba2e5d8" PrecompileTools = "aea7be01-6a6a-4083-8856-8a6e6704d82a" RecursiveArrayTools = "731186ca-8d62-57ce-b412-fbd966d074cd" Reexport = "189a3867-3050-52da-a836-e630ba90ab69" @@ -115,6 +117,7 @@ ForwardDiff = "0.10.3" FunctionWrappers = "1.1" FunctionWrappersWrappers = "0.1" Graphs = "1.5.2" +ImplicitDiscreteSolve = "0.1.2" InfiniteOpt = "0.5" InteractiveUtils = "1" JuliaFormatter = "1.0.47, 2" From e1f2776d5d8e17ed79da55b3b5bc2f19b273908b Mon Sep 17 00:00:00 2001 From: vyudu Date: Mon, 3 Mar 2025 17:07:52 -0500 Subject: [PATCH 1594/2176] refactor: reorganize `include`s, export `Pre` --- src/ModelingToolkit.jl | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/ModelingToolkit.jl b/src/ModelingToolkit.jl index 049c1815e4..b8bbebfbbb 100644 --- a/src/ModelingToolkit.jl +++ b/src/ModelingToolkit.jl @@ -54,6 +54,7 @@ import Moshi using Moshi.Data: @data using NonlinearSolve import SCCNonlinearSolve +using ImplicitDiscreteSolve using Reexport using RecursiveArrayTools import Graphs: SimpleDiGraph, add_edge!, incidence_matrix @@ -160,7 +161,6 @@ include("systems/model_parsing.jl") include("systems/connectors.jl") include("systems/analysis_points.jl") include("systems/imperative_affect.jl") -include("systems/callbacks.jl") include("systems/codegen_utils.jl") include("systems/problem_utils.jl") include("linearization.jl") @@ -170,19 +170,20 @@ include("systems/optimization/optimizationsystem.jl") include("systems/optimization/modelingtoolkitize.jl") include("systems/nonlinear/nonlinearsystem.jl") -include("systems/nonlinear/homotopy_continuation.jl") +include("systems/discrete_system/discrete_system.jl") +include("systems/discrete_system/implicit_discrete_system.jl") +include("systems/callbacks.jl") + include("systems/diffeqs/odesystem.jl") include("systems/diffeqs/sdesystem.jl") include("systems/diffeqs/abstractodesystem.jl") +include("systems/nonlinear/homotopy_continuation.jl") include("systems/nonlinear/modelingtoolkitize.jl") include("systems/nonlinear/initializesystem.jl") include("systems/diffeqs/first_order_transform.jl") include("systems/diffeqs/modelingtoolkitize.jl") include("systems/diffeqs/basic_transformations.jl") -include("systems/discrete_system/discrete_system.jl") -include("systems/discrete_system/implicit_discrete_system.jl") - include("systems/jumps/jumpsystem.jl") include("systems/pde/pdesystem.jl") @@ -307,6 +308,7 @@ export initialization_equations, guesses, defaults, parameter_dependencies, hier export structural_simplify, expand_connections, linearize, linearization_function, LinearizationProblem export solve +export Pre export calculate_jacobian, generate_jacobian, generate_function, generate_custom_function, generate_W From 0bc41666f4fd2efcac8716bf8a4483bc2af75d70 Mon Sep 17 00:00:00 2001 From: vyudu Date: Mon, 21 Apr 2025 13:58:49 -0400 Subject: [PATCH 1595/2176] docs: update NEWS.md --- NEWS.md | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/NEWS.md b/NEWS.md index 038b1d79f6..d316ac23fb 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,22 @@ +# ModelingToolkit v10 Release Notes + +### Callbacks + +Callback semantics have changed. + + - There is a new `Pre` operator that is used to specify which values are before the callback. + For example, the affect `A ~ A + 1` should now be written as `A ~ Pre(A) + 1`. This is + **required** to be specified - `A ~ A + 1` will now be interpreted as an equation to be + satisfied after the callback (and will thus error since it is unsatisfiable). + + - All parameters that are changed by a callback must be declared as discrete parameters to + the callback constructor, using the `discrete_parameters` keyword argument. + +```julia +event = SymbolicDiscreteCallback( + [t == 1] => [p ~ Pre(p) + 1], discrete_parameters = [p]) +``` + # ModelingToolkit v9 Release Notes ### Upgrade guide From b507cfca91aeb0e5977938b0683a06d3842ced20 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 9 May 2025 12:38:53 +0530 Subject: [PATCH 1596/2176] docs: document new `AffectSystem`/`Pre` semantics --- docs/src/basics/Events.md | 61 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git a/docs/src/basics/Events.md b/docs/src/basics/Events.md index 3a76f478f1..12721038ab 100644 --- a/docs/src/basics/Events.md +++ b/docs/src/basics/Events.md @@ -25,6 +25,67 @@ the event occurs). These can both be specified symbolically, but a more [general functional affect](@ref func_affects) representation is also allowed, as described below. +## Symbolic Callback Semantics + +In callbacks, there is a distinction between values of the unknowns and parameters +*before* the callback, and the desired values *after* the callback. In MTK, this +is provided by the `Pre` operator. For example, if we would like to add 1 to an +unknown `x` in a callback, the equation would look like the following: + +```julia +x ~ Pre(x) + 1 +``` + +Non `Pre`-d values will be interpreted as values *after* the callback. As such, +writing + +```julia +x ~ x + 1 +``` + +will be interpreted as an algebraic equation to be satisfied after the callback. +Since this equation obviously cannot be satisfied, an error will result. + +Callbacks must maintain the consistency of DAEs, meaning that they must satisfy +all the algebraic equations of the system after their update. However, the affect +equations often do not fully specify which unknowns/parameters should be modified +to maintain consistency. To make this clear, MTK uses the following rules: + + 1. All unknowns are treated as modifiable by the callback. In order to enforce that an unknown `x` remains the same, one can add `x ~ Pre(x)` to the affect equations. + 2. All parameters are treated as un-modifiable, *unless* they are declared as `discrete_parameters` to the callback. In order to be a discrete parameter, the parameter must be time-dependent (the terminology *discretes* here means [discrete variables](@ref save_discretes)). + +For example, consider the following system. + +```julia +@variables x(t) y(t) +@parameters p(t) +@mtkbuild sys = ODESystem([x * y ~ p, D(x) ~ 0], t) +event = [t == 1] => [x ~ Pre(x) + 1] +``` + +By default what will happen is that `x` will increase by 1, `p` will remain constant, +and `y` will change in order to compensate the increase in `x`. But what if we +wanted to keep `y` constant and change `p` instead? We could use the callback +constructor as follows: + +```julia +event = SymbolicDiscreteCallback( + [t == 1] => [x ~ Pre(x) + 1, y ~ Pre(y)], discrete_parameters = [p]) +``` + +This way, we enforce that `y` will remain the same, and `p` will change. + +!!! warning + + Symbolic affects come with the guarantee that the state after the callback + will be consistent. However, when using [general functional affects](@ref func_affects) + or [imperative affects](@ref imp_affects) one must be more careful. In + particular, one can pass in `reinitializealg` as a keyword arg to the + callback constructor to re-initialize the system. This will default to + `SciMLBase.NoInit()` in the case of symbolic affects and `SciMLBase.CheckInit()` + in the case of functional affects. This keyword should *not* be provided + if the affect is purely symbolic. + ## Continuous Events The basic purely symbolic continuous event interface to encode *one* continuous From 7fc26d0f10f817055bebd5744c6d464739a48d64 Mon Sep 17 00:00:00 2001 From: vyudu Date: Tue, 1 Apr 2025 15:10:38 -0400 Subject: [PATCH 1597/2176] docs: update docs to account for new `Pre` semantics --- docs/src/basics/Events.md | 47 ++++++++++++---------- docs/src/basics/MTKLanguage.md | 10 +++-- docs/src/systems/DiscreteSystem.md | 1 - docs/src/systems/ImplicitDiscreteSystem.md | 1 - docs/src/tutorials/fmi.md | 6 ++- 5 files changed, 37 insertions(+), 28 deletions(-) diff --git a/docs/src/basics/Events.md b/docs/src/basics/Events.md index 12721038ab..2808204d61 100644 --- a/docs/src/basics/Events.md +++ b/docs/src/basics/Events.md @@ -152,7 +152,7 @@ like this @variables x(t)=1 v(t)=0 root_eqs = [x ~ 0] # the event happens at the ground x(t) = 0 -affect = [v ~ -v] # the effect is that the velocity changes sign +affect = [v ~ -Pre(v)] # the effect is that the velocity changes sign @mtkbuild ball = ODESystem([D(x) ~ v D(v) ~ -9.8], t; continuous_events = root_eqs => affect) # equation => affect @@ -171,8 +171,8 @@ Multiple events? No problem! This example models a bouncing ball in 2D that is e ```@example events @variables x(t)=1 y(t)=0 vx(t)=0 vy(t)=2 -continuous_events = [[x ~ 0] => [vx ~ -vx] - [y ~ -1.5, y ~ 1.5] => [vy ~ -vy]] +continuous_events = [[x ~ 0] => [vx ~ -Pre(vx)] + [y ~ -1.5, y ~ 1.5] => [vy ~ -Pre(vy)]] @mtkbuild ball = ODESystem( [ @@ -265,7 +265,7 @@ bb_sol = solve(bb_prob, Tsit5()) plot(bb_sol) ``` -## Discrete events support +## Discrete Events In addition to continuous events, discrete events are also supported. The general interface to represent a collection of discrete events is @@ -288,13 +288,13 @@ Suppose we have a population of `N(t)` cells that can grow and die, and at time `t1` we want to inject `M` more cells into the population. We can model this by ```@example events -@parameters M tinject α +@parameters M tinject α(t) @variables N(t) Dₜ = Differential(t) eqs = [Dₜ(N) ~ α - N] # at time tinject we inject M cells -injection = (t == tinject) => [N ~ N + M] +injection = (t == tinject) => [N ~ Pre(N) + M] u0 = [N => 0.0] tspan = (0.0, 20.0) @@ -316,7 +316,7 @@ its steady-state value (which is 100). We can encode this by modifying the event to ```@example events -injection = ((t == tinject) & (N < 50)) => [N ~ N + M] +injection = ((t == tinject) & (N < 50)) => [N ~ Pre(N) + M] @mtkbuild osys = ODESystem(eqs, t, [N], [M, tinject, α]; discrete_events = injection) oprob = ODEProblem(osys, u0, tspan, p) @@ -330,16 +330,18 @@ event time, the event condition now returns false. Here we used logical and, cannot be used within symbolic expressions. Let's now also add a drug at time `tkill` that turns off production of new -cells, modeled by setting `α = 0.0` +cells, modeled by setting `α = 0.0`. Since this is a parameter we must explicitly +set it as `discrete_parameters`. ```@example events @parameters tkill # we reset the first event to just occur at tinject -injection = (t == tinject) => [N ~ N + M] +injection = (t == tinject) => [N ~ Pre(N) + M] # at time tkill we turn off production of cells -killing = (t == tkill) => [α ~ 0.0] +killing = ModelingToolkit.SymbolicDiscreteCallback( + (t == tkill) => [α ~ 0.0]; discrete_parameters = α, iv = t) tspan = (0.0, 30.0) p = [α => 100.0, tinject => 10.0, M => 50, tkill => 20.0] @@ -359,7 +361,7 @@ A preset-time event is triggered at specific set times, which can be passed in a vector like ```julia -discrete_events = [[1.0, 4.0] => [v ~ -v]] +discrete_events = [[1.0, 4.0] => [v ~ -Pre(v)]] ``` This will change the sign of `v` *only* at `t = 1.0` and `t = 4.0`. @@ -367,8 +369,9 @@ This will change the sign of `v` *only* at `t = 1.0` and `t = 4.0`. As such, our last example with treatment and killing could instead be modeled by ```@example events -injection = [10.0] => [N ~ N + M] -killing = [20.0] => [α ~ 0.0] +injection = [10.0] => [N ~ Pre(N) + M] +killing = ModelingToolkit.SymbolicDiscreteCallback( + [20.0] => [α ~ 0.0], discrete_parameters = α, iv = t) p = [α => 100.0, M => 50] @mtkbuild osys = ODESystem(eqs, t, [N], [α, M]; @@ -386,7 +389,7 @@ specify a periodic interval, pass the interval as the condition for the event. For example, ```julia -discrete_events = [1.0 => [v ~ -v]] +discrete_events = [1.0 => [v ~ -Pre(v)]] ``` will change the sign of `v` at `t = 1.0`, `2.0`, ... @@ -395,10 +398,10 @@ Finally, we note that to specify an event at precisely one time, say 2.0 below, one must still use a vector ```julia -discrete_events = [[2.0] => [v ~ -v]] +discrete_events = [[2.0] => [v ~ -Pre(v)]] ``` -## Saving discrete values +## [Saving discrete values](@id save_discretes) Time-dependent parameters which are updated in callbacks are termed as discrete variables. ModelingToolkit enables automatically saving the timeseries of these discrete variables, @@ -409,8 +412,10 @@ example: @variables x(t) @parameters c(t) +ev = ModelingToolkit.SymbolicDiscreteCallback( + 1.0 => [c ~ Pre(c) + 1], discrete_parameters = c, iv = t) @mtkbuild sys = ODESystem( - D(x) ~ c * cos(x), t, [x], [c]; discrete_events = [1.0 => [c ~ c + 1]]) + D(x) ~ c * cos(x), t, [x], [c]; discrete_events = [ev]) prob = ODEProblem(sys, [x => 0.0], (0.0, 2pi), [c => 1.0]) sol = solve(prob, Tsit5()) @@ -423,15 +428,15 @@ The solution object can also be interpolated with the discrete variables sol([1.0, 2.0], idxs = [c, c * cos(x)]) ``` -Note that only time-dependent parameters will be saved. If we repeat the above example with -this change: +Note that only time-dependent parameters that are explicitly passed as `discrete_parameters` +will be saved. If we repeat the above example with `c` not a `discrete_parameter`: ```@example events @variables x(t) -@parameters c +@parameters c(t) @mtkbuild sys = ODESystem( - D(x) ~ c * cos(x), t, [x], [c]; discrete_events = [1.0 => [c ~ c + 1]]) + D(x) ~ c * cos(x), t, [x], [c]; discrete_events = [1.0 => [c ~ Pre(c) + 1]]) prob = ODEProblem(sys, [x => 0.0], (0.0, 2pi), [c => 1.0]) sol = solve(prob, Tsit5()) diff --git a/docs/src/basics/MTKLanguage.md b/docs/src/basics/MTKLanguage.md index e91f2bcb67..ba6d2c34b5 100644 --- a/docs/src/basics/MTKLanguage.md +++ b/docs/src/basics/MTKLanguage.md @@ -203,6 +203,7 @@ getdefault(model_c3.model_a.k_array[2]) - Defining continuous events as described [here](https://docs.sciml.ai/ModelingToolkit/stable/basics/Events/#Continuous-Events). - If this block is not defined in the model, no continuous events will be added. + - Discrete parameters and other keyword arguments should be specified in a vector, as seen below. ```@example mtkmodel-example using ModelingToolkit @@ -210,7 +211,7 @@ using ModelingToolkit: t @mtkmodel M begin @parameters begin - k + k(t) end @variables begin x(t) @@ -223,6 +224,7 @@ using ModelingToolkit: t @continuous_events begin [x ~ 1.5] => [x ~ 5, y ~ 5] [t ~ 4] => [x ~ 10] + [t ~ 5] => [k ~ 3], [discrete_parameters = k] end end ``` @@ -231,13 +233,14 @@ end - Defining discrete events as described [here](https://docs.sciml.ai/ModelingToolkit/stable/basics/Events/#Discrete-events-support). - If this block is not defined in the model, no discrete events will be added. + - Discrete parameters and other keyword arguments should be specified in a vector, as seen below. ```@example mtkmodel-example using ModelingToolkit @mtkmodel M begin @parameters begin - k + k(t) end @variables begin x(t) @@ -248,7 +251,8 @@ using ModelingToolkit D(y) ~ -k end @discrete_events begin - (t == 1.5) => [x ~ x + 5, y ~ 5] + (t == 1.5) => [x ~ Pre(x) + 5, y ~ 5] + (t == 2.5) => [k ~ Pre(k) * 2], [discrete_parameters = k] end end ``` diff --git a/docs/src/systems/DiscreteSystem.md b/docs/src/systems/DiscreteSystem.md index f8a71043ab..55a02e5714 100644 --- a/docs/src/systems/DiscreteSystem.md +++ b/docs/src/systems/DiscreteSystem.md @@ -12,7 +12,6 @@ DiscreteSystem - `get_unknowns(sys)` or `unknowns(sys)`: The set of unknowns in the discrete system. - `get_ps(sys)` or `parameters(sys)`: The parameters of the discrete system. - `get_iv(sys)`: The independent variable of the discrete system - - `discrete_events(sys)`: The set of discrete events in the discrete system. ## Transformations diff --git a/docs/src/systems/ImplicitDiscreteSystem.md b/docs/src/systems/ImplicitDiscreteSystem.md index d69f88f106..d687502b49 100644 --- a/docs/src/systems/ImplicitDiscreteSystem.md +++ b/docs/src/systems/ImplicitDiscreteSystem.md @@ -12,7 +12,6 @@ ImplicitDiscreteSystem - `get_unknowns(sys)` or `unknowns(sys)`: The set of unknowns in the implicit discrete system. - `get_ps(sys)` or `parameters(sys)`: The parameters of the implicit discrete system. - `get_iv(sys)`: The independent variable of the implicit discrete system - - `discrete_events(sys)`: The set of discrete events in the implicit discrete system. ## Transformations diff --git a/docs/src/tutorials/fmi.md b/docs/src/tutorials/fmi.md index ef00477c78..0e01393652 100644 --- a/docs/src/tutorials/fmi.md +++ b/docs/src/tutorials/fmi.md @@ -94,7 +94,8 @@ we will create a model from a CoSimulation FMU. ```@example fmi fmu = loadFMU("SpringPendulum1D", "Dymola", "2023x", "3.0"; type = :CS) @named inner = ModelingToolkit.FMIComponent( - Val(3); fmu, communication_step_size = 0.001, type = :CS) + Val(3); fmu, communication_step_size = 0.001, type = :CS, + reinitializealg = BrownFullBasicInit()) ``` This FMU has fewer equations, partly due to missing aliasing variables and partly due to being a CS FMU. @@ -170,7 +171,8 @@ end `a` and `b` are inputs, `c` is a state, and `out` and `out2` are outputs of the component. ```@repl fmi -@named adder = ModelingToolkit.FMIComponent(Val(2); fmu, type = :ME); +@named adder = ModelingToolkit.FMIComponent( + Val(2); fmu, type = :ME, reinitializealg = BrownFullBasicInit()); isinput(adder.a) isinput(adder.b) isoutput(adder.out) From bacfd6c16635112e5001ca62c29f3d79141262c8 Mon Sep 17 00:00:00 2001 From: vyudu Date: Sat, 22 Mar 2025 07:08:56 -0400 Subject: [PATCH 1598/2176] fix: fix `reinitializealg` usage in FMIExt --- ext/MTKFMIExt.jl | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/ext/MTKFMIExt.jl b/ext/MTKFMIExt.jl index 5cfe9a82ef..32023d0749 100644 --- a/ext/MTKFMIExt.jl +++ b/ext/MTKFMIExt.jl @@ -93,7 +93,7 @@ with the name `namespace__variable`. - `name`: The name of the system. """ function MTK.FMIComponent(::Val{Ver}; fmu = nothing, tolerance = 1e-6, - communication_step_size = nothing, reinitializealg = SciMLBase.NoInit(), type, name) where {Ver} + communication_step_size = nothing, reinitializealg = nothing, type, name) where {Ver} if Ver != 2 && Ver != 3 throw(ArgumentError("FMI Version must be `2` or `3`")) end @@ -238,7 +238,7 @@ function MTK.FMIComponent(::Val{Ver}; fmu = nothing, tolerance = 1e-6, finalize_affect = MTK.FunctionalAffect(fmiFinalize!, [], [wrapper], []) step_affect = MTK.FunctionalAffect(Returns(nothing), [], [], []) instance_management_callback = MTK.SymbolicDiscreteCallback( - (t != t - 1), step_affect; finalize = finalize_affect, reinitializealg = reinitializealg) + (t != t - 1), step_affect; finalize = finalize_affect, reinitializealg) push!(params, wrapper) append!(observed, der_observed) @@ -279,8 +279,7 @@ function MTK.FMIComponent(::Val{Ver}; fmu = nothing, tolerance = 1e-6, fmiCSStep!; observed = cb_observed, modified = cb_modified, ctx = _functor) instance_management_callback = MTK.SymbolicDiscreteCallback( communication_step_size, step_affect; initialize = initialize_affect, - finalize = finalize_affect, reinitializealg = reinitializealg - ) + finalize = finalize_affect, reinitializealg) # guarded in case there are no outputs/states and the variable is `[]`. symbolic_type(__mtk_internal_o) == NotSymbolic() || push!(params, __mtk_internal_o) From e28af63a18a482ededa8a8f3a56ea05d374d5fd2 Mon Sep 17 00:00:00 2001 From: vyudu Date: Thu, 20 Mar 2025 14:55:28 -0400 Subject: [PATCH 1599/2176] fix: do not distribute shifts inside `Pre` and `Initial` --- src/structural_transformation/utils.jl | 1 + 1 file changed, 1 insertion(+) diff --git a/src/structural_transformation/utils.jl b/src/structural_transformation/utils.jl index fa02f0b3cd..191c25ab68 100644 --- a/src/structural_transformation/utils.jl +++ b/src/structural_transformation/utils.jl @@ -557,6 +557,7 @@ end function _distribute_shift(expr, shift) if iscall(expr) op = operation(expr) + (op isa Pre || op isa Initial) && return expr args = arguments(expr) if ModelingToolkit.isvariable(expr) From 7ff1dda61034a6efacb1206bb84437bd060525b5 Mon Sep 17 00:00:00 2001 From: vyudu Date: Tue, 11 Mar 2025 18:19:58 -0400 Subject: [PATCH 1600/2176] test: update tests to account for new `Pre` semantics --- test/accessor_functions.jl | 23 +- test/discrete_system.jl | 1 - test/extensions/ad.jl | 3 +- test/fmi/fmi.jl | 4 +- test/funcaffect.jl | 5 +- test/initializationsystem.jl | 4 +- test/jumpsystem.jl | 41 +- test/model_parsing.jl | 2 +- test/mtkparameters.jl | 3 +- test/odesystem.jl | 63 +- test/parameter_dependencies.jl | 30 +- test/symbolic_events.jl | 909 ++++++++++++---------------- test/symbolic_indexing_interface.jl | 6 +- test/symbolic_parameters.jl | 6 +- 14 files changed, 494 insertions(+), 606 deletions(-) diff --git a/test/accessor_functions.jl b/test/accessor_functions.jl index 7ce477155b..4136736a8b 100644 --- a/test/accessor_functions.jl +++ b/test/accessor_functions.jl @@ -54,8 +54,8 @@ let D(Y) ~ -Y^3, O ~ (p_bot + d) * X_bot + Y ] - cevs = [[t ~ 1.0] => [Y ~ Y + 2.0]] - devs = [(t == 2.0) => [Y ~ Y + 2.0]] + cevs = [[t ~ 1.0] => [Y ~ Pre(Y) + 2.0]] + devs = [(t == 2.0) => [Y ~ Pre(Y) + 2.0]] @named sys_bot = ODESystem( eqs_bot, t; systems = [], continuous_events = cevs, discrete_events = devs) @named sys_mid2 = ODESystem( @@ -149,20 +149,19 @@ let for sys in [sys_bot, sys_mid2, sys_mid1, sys_top]) # Checks `continuous_events_toplevel` and `discrete_events_toplevel` (straightforward - # as I stored the same singe event in all systems). Don't check for non-toplevel cases as + # as I stored the same single event in all systems). Don't check for non-toplevel cases as # technically not needed for these tests and name spacing the events is a mess. - mtk_cev = ModelingToolkit.SymbolicContinuousCallback.(cevs)[1] - mtk_dev = ModelingToolkit.SymbolicDiscreteCallback.(devs)[1] + bot_cev = ModelingToolkit.SymbolicContinuousCallback( + cevs[1], alg_eqs = [O ~ (d + p_bot) * X_bot + Y]) + mid_dev = ModelingToolkit.SymbolicDiscreteCallback( + devs[1], alg_eqs = [O ~ (d + p_mid1) * X_mid1 + Y]) @test all_sets_equal( - continuous_events_toplevel.( - [sys_bot, sys_bot_comp, sys_bot_ss, sys_mid1, sys_mid1_comp, sys_mid1_ss, - sys_mid2, sys_mid2_comp, sys_mid2_ss, sys_top, sys_top_comp, sys_top_ss])..., - [mtk_cev]) + continuous_events_toplevel.([sys_bot, sys_bot_comp, sys_bot_ss])..., + [bot_cev]) @test all_sets_equal( discrete_events_toplevel.( - [sys_bot, sys_bot_comp, sys_bot_ss, sys_mid1, sys_mid1_comp, sys_mid1_ss, - sys_mid2, sys_mid2_comp, sys_mid2_ss, sys_top, sys_top_comp, sys_top_ss])..., - [mtk_dev]) + [sys_mid1, sys_mid1_comp, sys_mid1_ss])..., + [mid_dev]) @test all(sym_issubset( continuous_events_toplevel(sys), get_continuous_events(sys)) for sys in [sys_bot, sys_mid2, sys_mid1, sys_top]) diff --git a/test/discrete_system.jl b/test/discrete_system.jl index 3897d17984..43ee771b2b 100644 --- a/test/discrete_system.jl +++ b/test/discrete_system.jl @@ -254,7 +254,6 @@ end @variables x(t) y(t) k = ShiftIndex(t) @named sys = DiscreteSystem([x ~ x^2 + y^2, y ~ x(k - 1) + y(k - 1)], t) -@test_throws ["algebraic equations", "ImplicitDiscreteSystem"] structural_simplify(sys) @testset "Passing `nothing` to `u0`" begin @variables x(t) = 1 diff --git a/test/extensions/ad.jl b/test/extensions/ad.jl index 14649b6bb6..73b1710812 100644 --- a/test/extensions/ad.jl +++ b/test/extensions/ad.jl @@ -60,7 +60,8 @@ end @parameters a b[1:3] c(t) d::Integer e[1:3] f[1:3, 1:3]::Int g::Vector{AbstractFloat} h::String @named sys = ODESystem( Equation[], t, [], [a, b, c, d, e, f, g, h], - continuous_events = [[a ~ 0] => [c ~ 0]]) + continuous_events = [ModelingToolkit.SymbolicContinuousCallback( + [a ~ 0] => [c ~ 0], discrete_parameters = c)]) sys = complete(sys) ivs = Dict(c => 3a, b => ones(3), a => 1.0, d => 4, e => [5.0, 6.0, 7.0], diff --git a/test/fmi/fmi.jl b/test/fmi/fmi.jl index 98c93398ff..edbbb312d6 100644 --- a/test/fmi/fmi.jl +++ b/test/fmi/fmi.jl @@ -158,7 +158,7 @@ end fmu = loadFMU(joinpath(FMU_DIR, "SimpleAdder.fmu"); type = :CS) @named adder = MTK.FMIComponent( Val(2); fmu, type = :CS, communication_step_size = 1e-6, - reinitializealg = BrownFullBasicInit()) + reinitializealg = BrownFullBasicInit(abstol = 1e-7)) @test MTK.isinput(adder.a) @test MTK.isinput(adder.b) @test MTK.isoutput(adder.out) @@ -211,7 +211,7 @@ end fmu = loadFMU(joinpath(FMU_DIR, "StateSpace.fmu"); type = :CS) @named sspace = MTK.FMIComponent( Val(3); fmu, communication_step_size = 1e-6, type = :CS, - reinitializealg = BrownFullBasicInit()) + reinitializealg = BrownFullBasicInit(abstol = 1e-7)) @test MTK.isinput(sspace.u) @test MTK.isoutput(sspace.y) @test !MTK.isinput(sspace.x) && !MTK.isoutput(sspace.x) diff --git a/test/funcaffect.jl b/test/funcaffect.jl index 05543c9161..1e7c66f39a 100644 --- a/test/funcaffect.jl +++ b/test/funcaffect.jl @@ -26,8 +26,7 @@ cb1 = ModelingToolkit.SymbolicDiscreteCallback(t == zr, (affect1!, [], [], [], [ @test cb == cb1 @test ModelingToolkit.SymbolicDiscreteCallback(cb) === cb # passthrough @test hash(cb) == hash(cb1) -ModelingToolkit.generate_discrete_callback(cb, sys, ModelingToolkit.get_variables(sys), - ModelingToolkit.get_ps(sys)); +ModelingToolkit.generate_callback(cb, sys); cb = ModelingToolkit.SymbolicContinuousCallback([t ~ zr], (f = affect1!, sts = [], pars = [], discretes = [], @@ -48,7 +47,7 @@ sys1 = ODESystem(eqs, t, [u], [], name = :sys, de = ModelingToolkit.get_discrete_events(sys1) @test length(de) == 1 de = de[1] -@test ModelingToolkit.condition(de) == [4.0] +@test ModelingToolkit.conditions(de) == [4.0] @test ModelingToolkit.has_functional_affect(de) sys2 = ODESystem(eqs, t, [u], [], name = :sys, diff --git a/test/initializationsystem.jl b/test/initializationsystem.jl index 7c512d37af..2cd8499e69 100644 --- a/test/initializationsystem.jl +++ b/test/initializationsystem.jl @@ -1317,9 +1317,9 @@ end @parameters β γ S0 @variables S(t)=S0 I(t) R(t) rate₁ = β * S * I - affect₁ = [S ~ S - 1, I ~ I + 1] + affect₁ = [S ~ Pre(S) - 1, I ~ Pre(I) + 1] rate₂ = γ * I - affect₂ = [I ~ I - 1, R ~ R + 1] + affect₂ = [I ~ Pre(I) - 1, R ~ Pre(R) + 1] j₁ = ConstantRateJump(rate₁, affect₁) j₂ = ConstantRateJump(rate₂, affect₂) j₃ = MassActionJump(2 * β + γ, [R => 1], [S => 1, R => -1]) diff --git a/test/jumpsystem.jl b/test/jumpsystem.jl index 6c96055270..c5dbe1c56c 100644 --- a/test/jumpsystem.jl +++ b/test/jumpsystem.jl @@ -2,6 +2,7 @@ using ModelingToolkit, DiffEqBase, JumpProcesses, Test, LinearAlgebra using Random, StableRNGs, NonlinearSolve using OrdinaryDiffEq using ModelingToolkit: t_nounits as t, D_nounits as D +using BenchmarkTools MT = ModelingToolkit rng = StableRNG(12345) @@ -11,9 +12,9 @@ rng = StableRNG(12345) @constants h = 1 @variables S(t) I(t) R(t) rate₁ = β * S * I * h -affect₁ = [S ~ S - 1 * h, I ~ I + 1] +affect₁ = [S ~ Pre(S) - 1 * h, I ~ Pre(I) + 1] rate₂ = γ * I + t -affect₂ = [I ~ I - 1, R ~ R + 1] +affect₂ = [I ~ Pre(I) - 1, R ~ Pre(R) + 1] j₁ = ConstantRateJump(rate₁, affect₁) j₂ = VariableRateJump(rate₂, affect₂) @named js = JumpSystem([j₁, j₂], t, [S, I, R], [β, γ]) @@ -59,7 +60,7 @@ jump2.affect!(integrator) # test MT can make and solve a jump problem rate₃ = γ * I * h -affect₃ = [I ~ I * h - 1, R ~ R + 1] +affect₃ = [I ~ Pre(I) * h - 1, R ~ Pre(R) + 1] j₃ = ConstantRateJump(rate₃, affect₃) @named js2 = JumpSystem([j₁, j₃], t, [S, I, R], [β, γ]) js2 = complete(js2) @@ -247,7 +248,7 @@ end rate = k affect = [X ~ X - 1] -crj = ConstantRateJump(1.0, [X ~ X - 1]) +crj = ConstantRateJump(1.0, [X ~ Pre(X) - 1]) js1 = complete(JumpSystem([crj], t, [X], [k]; name = :js1)) js2 = complete(JumpSystem([crj], t, [X], []; name = :js2)) @@ -274,9 +275,9 @@ dp4 = DiscreteProblem(js4, u0, tspan) @parameters k @variables X(t) rate = k -affect = [X ~ X - 1] +affect = [X ~ Pre(X) - 1] -j1 = ConstantRateJump(k, [X ~ X - 1]) +j1 = ConstantRateJump(k, [X ~ Pre(X) - 1]) @test_nowarn @mtkbuild js1 = JumpSystem([j1], t, [X], [k]) # test correct autosolver is selected, which implies appropriate dep graphs are available @@ -284,8 +285,8 @@ let @parameters k @variables X(t) rate = k - affect = [X ~ X - 1] - j1 = ConstantRateJump(k, [X ~ X - 1]) + affect = [X ~ Pre(X) - 1] + j1 = ConstantRateJump(k, [X ~ Pre(X) - 1]) Nv = [1, JumpProcesses.USE_DIRECT_THRESHOLD + 1, JumpProcesses.USE_RSSA_THRESHOLD + 1] algtypes = [Direct, RSSA, RSSACR] @@ -304,7 +305,7 @@ let Random.seed!(rng, 1111) @variables A(t) B(t) C(t) @parameters k - vrj = VariableRateJump(k * (sin(t) + 1), [A ~ A + 1, C ~ C + 2]) + vrj = VariableRateJump(k * (sin(t) + 1), [A ~ Pre(A) + 1, C ~ Pre(C) + 2]) js = complete(JumpSystem([vrj], t, [A, C], [k]; name = :js, observed = [B ~ C * A])) oprob = ODEProblem(js, [A => 0, C => 0], (0.0, 10.0), [k => 1.0]) jprob = JumpProblem(js, oprob, Direct(); rng) @@ -345,9 +346,9 @@ end let @variables x1(t) x2(t) x3(t) x4(t) x5(t) @parameters p1 p2 p3 p4 p5 - j1 = ConstantRateJump(p1, [x1 ~ x1 + 1]) + j1 = ConstantRateJump(p1, [x1 ~ Pre(x1) + 1]) j2 = MassActionJump(p2, [x2 => 1], [x3 => -1]) - j3 = VariableRateJump(p3, [x3 ~ x3 + 1, x4 ~ x4 + 1]) + j3 = VariableRateJump(p3, [x3 ~ Pre(x3) + 1, x4 ~ Pre(x4) + 1]) j4 = MassActionJump(p4 * p5, [x1 => 1, x5 => 1], [x1 => -1, x5 => -1, x2 => 1]) us = Set() ps = Set() @@ -387,9 +388,9 @@ let p3 = ParentScope(ParentScope(p3)) p4 = GlobalScope(p4) - j1 = ConstantRateJump(p1, [x1 ~ x1 + 1]) + j1 = ConstantRateJump(p1, [x1 ~ Pre(x1) + 1]) j2 = MassActionJump(p2, [x2 => 1], [x3 => -1]) - j3 = VariableRateJump(p3, [x3 ~ x3 + 1, x4 ~ x4 + 1]) + j3 = VariableRateJump(p3, [x3 ~ Pre(x3) + 1, x4 ~ Pre(x4) + 1]) j4 = MassActionJump(p4 * p4, [x1 => 1, x4 => 1], [x1 => -1, x4 => -1, x2 => 1]) @named js = JumpSystem([j1, j2, j3, j4], t, [x1, x2, x3, x4], [p1, p2, p3, p4]) @@ -427,8 +428,8 @@ let Random.seed!(rng, seed) @variables X(t) Y(t) @parameters k1 k2 - vrj1 = VariableRateJump(k1 * X, [X ~ X - 1]; save_positions = (false, false)) - vrj2 = VariableRateJump(k1, [Y ~ Y + 1]; save_positions = (false, false)) + vrj1 = VariableRateJump(k1 * X, [X ~ Pre(X) - 1]; save_positions = (false, false)) + vrj2 = VariableRateJump(k1, [Y ~ Pre(Y) + 1]; save_positions = (false, false)) eqs = [D(X) ~ k2, D(Y) ~ -k2 / 10 * Y] @named jsys = JumpSystem([vrj1, vrj2, eqs[1], eqs[2]], t, [X, Y], [k1, k2]) jsys = complete(jsys) @@ -469,8 +470,8 @@ let Random.seed!(rng, seed) @variables X(t) Y(t) @parameters α β - vrj = VariableRateJump(β * X, [X ~ X - 1]; save_positions = (false, false)) - crj = ConstantRateJump(β * Y, [Y ~ Y - 1]) + vrj = VariableRateJump(β * X, [X ~ Pre(X) - 1]; save_positions = (false, false)) + crj = ConstantRateJump(β * Y, [Y ~ Pre(Y) - 1]) maj = MassActionJump(α, [0 => 1], [Y => 1]) eqs = [D(X) ~ α * (1 + Y)] @named jsys = JumpSystem([maj, crj, vrj, eqs[1]], t, [X, Y], [α, β]) @@ -537,8 +538,8 @@ end @variables X(t) rate1 = p rate2 = X * d - affect1 = [X ~ X + 1] - affect2 = [X ~ X - 1] + affect1 = [X ~ Pre(X) + 1] + affect2 = [X ~ Pre(X) - 1] j1 = ConstantRateJump(rate1, affect1) j2 = ConstantRateJump(rate2, affect2) @@ -555,7 +556,7 @@ end @parameters a b eq = D(X) ~ a rate = b * X - affect = [X ~ X - 1] + affect = [X ~ Pre(X) - 1] crj = ConstantRateJump(rate, affect) @named jsys = JumpSystem([crj, eq], t, [X], [a, b]) jsys = complete(jsys) diff --git a/test/model_parsing.jl b/test/model_parsing.jl index fe2bcbfca6..ad457a0ba3 100644 --- a/test/model_parsing.jl +++ b/test/model_parsing.jl @@ -484,7 +484,7 @@ using ModelingToolkit: D_nounits [x ~ 1.5] => [x ~ 5, y ~ 1] end @discrete_events begin - (t == 1.5) => [x ~ x + 5, z ~ 2] + (t == 1.5) => [x ~ Pre(x) + 5, z ~ 2] end end diff --git a/test/mtkparameters.jl b/test/mtkparameters.jl index b7acbb84a8..809da4df94 100644 --- a/test/mtkparameters.jl +++ b/test/mtkparameters.jl @@ -10,7 +10,8 @@ using JET @parameters a b c(t) d::Integer e[1:3] f[1:3, 1:3]::Int g::Vector{AbstractFloat} h::String @named sys = ODESystem( Equation[], t, [], [a, c, d, e, f, g, h], parameter_dependencies = [b ~ 2a], - continuous_events = [[a ~ 0] => [c ~ 0]], defaults = Dict(a => 0.0)) + continuous_events = [ModelingToolkit.SymbolicContinuousCallback( + [a ~ 0] => [c ~ 0], discrete_parameters = c)], defaults = Dict(a => 0.0)) sys = complete(sys) ivs = Dict(c => 3a, d => 4, e => [5.0, 6.0, 7.0], diff --git a/test/odesystem.jl b/test/odesystem.jl index e113e5d2c2..b707d119fc 100644 --- a/test/odesystem.jl +++ b/test/odesystem.jl @@ -1,5 +1,6 @@ using ModelingToolkit, StaticArrays, LinearAlgebra -using ModelingToolkit: get_metadata, MTKParameters +using ModelingToolkit: get_metadata, MTKParameters, SymbolicDiscreteCallback, + SymbolicContinuousCallback using SymbolicIndexingInterface using OrdinaryDiffEq, Sundials using DiffEqBase, SparseArrays @@ -1015,24 +1016,27 @@ prob = ODEProblem(sys, [x => 1.0], (0.0, 10.0)) @test_nowarn solve(prob, Tsit5()) # Issue#2383 -@variables x(t)[1:3] -@parameters p[1:3, 1:3] -eqs = [ - D(x) ~ p * x -] -@mtkbuild sys = ODESystem(eqs, t; continuous_events = [[norm(x) ~ 3.0] => [x ~ ones(3)]]) -# array affect equations used to not work -prob1 = @test_nowarn ODEProblem(sys, [x => ones(3)], (0.0, 10.0), [p => ones(3, 3)]) -sol1 = @test_nowarn solve(prob1, Tsit5()) +@testset "Arrays in affect/condition equations" begin + @variables x(t)[1:3] + @parameters p[1:3, 1:3] + eqs = [ + D(x) ~ p * x + ] + @mtkbuild sys = ODESystem( + eqs, t; continuous_events = [[norm(x) ~ 3.0] => [x ~ ones(3)]]) + # array affect equations used to not work + prob1 = @test_nowarn ODEProblem(sys, [x => ones(3)], (0.0, 10.0), [p => ones(3, 3)]) + sol1 = @test_nowarn solve(prob1, Tsit5()) -# array condition equations also used to not work -@mtkbuild sys = ODESystem( - eqs, t; continuous_events = [[x ~ sqrt(3) * ones(3)] => [x ~ ones(3)]]) -# array affect equations used to not work -prob2 = @test_nowarn ODEProblem(sys, [x => ones(3)], (0.0, 10.0), [p => ones(3, 3)]) -sol2 = @test_nowarn solve(prob2, Tsit5()) + # array condition equations also used to not work + @mtkbuild sys = ODESystem( + eqs, t; continuous_events = [[x ~ sqrt(3) * ones(3)] => [x ~ ones(3)]]) + # array affect equations used to not work + prob2 = @test_nowarn ODEProblem(sys, [x => ones(3)], (0.0, 10.0), [p => ones(3, 3)]) + sol2 = @test_nowarn solve(prob2, Tsit5()) -@test sol1 ≈ sol2 + @test sol1.u ≈ sol2.u[2:end] +end # Requires fix in symbolics for `linear_expansion(p * x, D(y))` @test_skip begin @@ -1179,10 +1183,12 @@ end end # Namespacing of array variables -@variables x(t)[1:2] -@named sys = ODESystem(Equation[], t) -@test getname(unknowns(sys, x)) == :sys₊x -@test size(unknowns(sys, x)) == size(x) +@testset "Namespacing of array variables" begin + @variables x(t)[1:2] + @named sys = ODESystem(Equation[], t) + @test getname(unknowns(sys, x)) == :sys₊x + @test size(unknowns(sys, x)) == size(x) +end # Issue#2667 and Issue#2953 @testset "ForwardDiff through ODEProblem constructor" begin @@ -1520,8 +1526,12 @@ end @testset "Observed variables dependent on discrete parameters" begin @variables x(t) obs(t) @parameters c(t) - @mtkbuild sys = ODESystem( - [D(x) ~ c * cos(x), obs ~ c], t, [x], [c]; discrete_events = [1.0 => [c ~ c + 1]]) + @mtkbuild sys = ODESystem([D(x) ~ c * cos(x), obs ~ c], + t, + [x], + [c]; + discrete_events = [SymbolicDiscreteCallback( + 1.0 => [c ~ Pre(c) + 1], discrete_parameters = [c])]) prob = ODEProblem(sys, [x => 0.0], (0.0, 2pi), [c => 1.0]) sol = solve(prob, Tsit5()) @test sol[obs] ≈ 1:7 @@ -1581,15 +1591,16 @@ end # Test `isequal` @testset "`isequal`" begin @variables X(t) - @parameters p d + @parameters p d(t) eq = D(X) ~ p - d * X osys1 = complete(ODESystem([eq], t; name = :osys)) osys2 = complete(ODESystem([eq], t; name = :osys)) @test osys1 == osys2 # true - continuous_events = [[X ~ 1.0] => [X ~ X + 5.0]] - discrete_events = [5.0 => [d ~ d / 2.0]] + continuous_events = [[X ~ 1.0] => [X ~ Pre(X) + 5.0]] + discrete_events = [SymbolicDiscreteCallback( + 5.0 => [d ~ d / 2.0], discrete_parameters = [d])] osys1 = complete(ODESystem([eq], t; name = :osys, continuous_events)) osys2 = complete(ODESystem([eq], t; name = :osys)) diff --git a/test/parameter_dependencies.jl b/test/parameter_dependencies.jl index 31881e1ca8..bdaa2a391b 100644 --- a/test/parameter_dependencies.jl +++ b/test/parameter_dependencies.jl @@ -1,6 +1,7 @@ using ModelingToolkit using Test -using ModelingToolkit: t_nounits as t, D_nounits as D +using ModelingToolkit: t_nounits as t, D_nounits as D, SymbolicDiscreteCallback, + SymbolicContinuousCallback using OrdinaryDiffEq using StochasticDiffEq using JumpProcesses @@ -10,14 +11,14 @@ using SymbolicIndexingInterface using NonlinearSolve @testset "ODESystem with callbacks" begin - @parameters p1=1.0 p2 + @parameters p1(t)=1.0 p2 @variables x(t) - cb1 = [x ~ 2.0] => [p1 ~ 2.0] # triggers at t=-2+√6 + cb1 = SymbolicContinuousCallback([x ~ 2.0] => [p1 ~ 2.0], discrete_parameters = [p1]) # triggers at t=-2+√6 function affect1!(integ, u, p, ctx) integ.ps[p[1]] = integ.ps[p[2]] end cb2 = [x ~ 4.0] => (affect1!, [], [p1, p2], [p1]) # triggers at t=-2+√7 - cb3 = [1.0] => [p1 ~ 5.0] + cb3 = SymbolicDiscreteCallback([1.0] => [p1 ~ 5.0], discrete_parameters = [p1]) @mtkbuild sys = ODESystem( [D(x) ~ p1 * t + p2], @@ -203,7 +204,7 @@ end @testset "Clock system" begin dt = 0.1 @variables x(t) y(t) u(t) yd(t) ud(t) r(t) z(t) - @parameters kp kq + @parameters kp(t) kq d = Clock(dt) k = ShiftIndex(d) @@ -225,7 +226,8 @@ end @test_nowarn solve(prob, Tsit5()) @mtkbuild sys = ODESystem(eqs, t; parameter_dependencies = [kq => 2kp], - discrete_events = [[0.5] => [kp ~ 2.0]]) + discrete_events = [SymbolicDiscreteCallback( + [0.5] => [kp ~ 2.0], discrete_parameters = [kp])]) prob = ODEProblem(sys, [x => 0.0, y => 0.0], (0.0, Tf), [kp => 1.0; z(k - 1) => 3.0; yd(k - 1) => 0.0; z(k - 2) => 4.0; yd(k - 2) => 2.0]) @@ -245,7 +247,7 @@ end end @testset "SDESystem" begin - @parameters σ ρ β + @parameters σ(t) ρ β @variables x(t) y(t) z(t) eqs = [D(x) ~ σ * (y - x), @@ -269,7 +271,8 @@ end @named sys = ODESystem(eqs, t) @named sdesys = SDESystem(sys, noiseeqs; parameter_dependencies = [ρ => 2σ], - discrete_events = [[10.0] => [σ ~ 15.0]]) + discrete_events = [SymbolicDiscreteCallback( + [10.0] => [σ ~ 15.0], discrete_parameters = [σ])]) sdesys = complete(sdesys) prob = SDEProblem( sdesys, [x => 1.0, y => 0.0, z => 0.0], (0.0, 100.0), [σ => 10.0, β => 2.33]) @@ -283,17 +286,17 @@ end @testset "JumpSystem" begin rng = StableRNG(12345) - @parameters β γ + @parameters β γ(t) @constants h = 1 @variables S(t) I(t) R(t) rate₁ = β * S * I * h - affect₁ = [S ~ S - 1 * h, I ~ I + 1] + affect₁ = [S ~ Pre(S) - 1 * h, I ~ Pre(I) + 1] rate₃ = γ * I * h - affect₃ = [I ~ I * h - 1, R ~ R + 1] + affect₃ = [I ~ Pre(I) * h - 1, R ~ Pre(R) + 1] j₁ = ConstantRateJump(rate₁, affect₁) j₃ = ConstantRateJump(rate₃, affect₃) @named js2 = JumpSystem( - [j₁, j₃], t, [S, I, R], [γ]; parameter_dependencies = [β => 0.01γ]) + [j₃], t, [S, I, R], [γ]; parameter_dependencies = [β => 0.01γ]) @test isequal(only(parameters(js2)), γ) @test Set(full_parameters(js2)) == Set([γ, β]) js2 = complete(js2) @@ -308,7 +311,8 @@ end @named js2 = JumpSystem( [j₁, j₃], t, [S, I, R], [γ]; parameter_dependencies = [β => 0.01γ], - discrete_events = [[10.0] => [γ ~ 0.02]]) + discrete_events = [SymbolicDiscreteCallback( + [10.0] => [γ ~ 0.02], discrete_parameters = [γ])]) js2 = complete(js2) dprob = DiscreteProblem(js2, u₀map, tspan, parammap) jprob = JumpProblem(js2, dprob, Direct(), save_positions = (false, false), rng = rng) diff --git a/test/symbolic_events.jl b/test/symbolic_events.jl index e72626849a..48faa76aa3 100644 --- a/test/symbolic_events.jl +++ b/test/symbolic_events.jl @@ -1,10 +1,11 @@ using ModelingToolkit, OrdinaryDiffEq, StochasticDiffEq, JumpProcesses, Test using SciMLStructures: canonicalize, Discrete using ModelingToolkit: SymbolicContinuousCallback, - SymbolicContinuousCallbacks, NULL_AFFECT, + SymbolicDiscreteCallback, get_callback, t_nounits as t, - D_nounits as D + D_nounits as D, + affects, affect_negs, system, observed, AffectSystem using StableRNGs import SciMLBase using SymbolicIndexingInterface @@ -17,215 +18,74 @@ eqs = [D(x) ~ 1] affect = [x ~ 0] affect_neg = [x ~ 1] -## Test SymbolicContinuousCallback @testset "SymbolicContinuousCallback constructors" begin e = SymbolicContinuousCallback(eqs[]) @test e isa SymbolicContinuousCallback - @test isequal(e.eqs, eqs) - @test e.affect == NULL_AFFECT - @test e.affect_neg == NULL_AFFECT + @test isequal(equations(e), eqs) + @test e.affect === nothing + @test e.affect_neg === nothing @test e.rootfind == SciMLBase.LeftRootFind e = SymbolicContinuousCallback(eqs) @test e isa SymbolicContinuousCallback - @test isequal(e.eqs, eqs) - @test e.affect == NULL_AFFECT - @test e.affect_neg == NULL_AFFECT + @test isequal(equations(e), eqs) + @test e.affect === nothing + @test e.affect_neg === nothing @test e.rootfind == SciMLBase.LeftRootFind - e = SymbolicContinuousCallback(eqs, NULL_AFFECT) + e = SymbolicContinuousCallback(eqs, nothing) @test e isa SymbolicContinuousCallback - @test isequal(e.eqs, eqs) - @test e.affect == NULL_AFFECT - @test e.affect_neg == NULL_AFFECT + @test isequal(equations(e), eqs) + @test e.affect === nothing + @test e.affect_neg === nothing @test e.rootfind == SciMLBase.LeftRootFind - e = SymbolicContinuousCallback(eqs[], NULL_AFFECT) + e = SymbolicContinuousCallback(eqs[], nothing) @test e isa SymbolicContinuousCallback - @test isequal(e.eqs, eqs) - @test e.affect == NULL_AFFECT - @test e.affect_neg == NULL_AFFECT + @test isequal(equations(e), eqs) + @test e.affect === nothing + @test e.affect_neg === nothing @test e.rootfind == SciMLBase.LeftRootFind - e = SymbolicContinuousCallback(eqs => NULL_AFFECT) + e = SymbolicContinuousCallback(eqs => nothing) @test e isa SymbolicContinuousCallback - @test isequal(e.eqs, eqs) - @test e.affect == NULL_AFFECT - @test e.affect_neg == NULL_AFFECT + @test isequal(equations(e), eqs) + @test e.affect === nothing + @test e.affect_neg === nothing @test e.rootfind == SciMLBase.LeftRootFind - e = SymbolicContinuousCallback(eqs[] => NULL_AFFECT) + e = SymbolicContinuousCallback(eqs[] => nothing) @test e isa SymbolicContinuousCallback - @test isequal(e.eqs, eqs) - @test e.affect == NULL_AFFECT - @test e.affect_neg == NULL_AFFECT + @test isequal(equations(e), eqs) + @test e.affect === nothing + @test e.affect_neg === nothing @test e.rootfind == SciMLBase.LeftRootFind ## With affect - - e = SymbolicContinuousCallback(eqs[], affect) - @test e isa SymbolicContinuousCallback - @test isequal(e.eqs, eqs) - @test e.affect == affect - @test e.affect_neg == affect - @test e.rootfind == SciMLBase.LeftRootFind - - e = SymbolicContinuousCallback(eqs, affect) - @test e isa SymbolicContinuousCallback - @test isequal(e.eqs, eqs) - @test e.affect == affect - @test e.affect_neg == affect - @test e.rootfind == SciMLBase.LeftRootFind - - e = SymbolicContinuousCallback(eqs, affect) - @test e isa SymbolicContinuousCallback - @test isequal(e.eqs, eqs) - @test e.affect == affect - @test e.affect_neg == affect - @test e.rootfind == SciMLBase.LeftRootFind - e = SymbolicContinuousCallback(eqs[], affect) @test e isa SymbolicContinuousCallback - @test isequal(e.eqs, eqs) - @test e.affect == affect - @test e.affect_neg == affect - @test e.rootfind == SciMLBase.LeftRootFind - - e = SymbolicContinuousCallback(eqs => affect) - @test e isa SymbolicContinuousCallback - @test isequal(e.eqs, eqs) - @test e.affect == affect - @test e.affect_neg == affect - @test e.rootfind == SciMLBase.LeftRootFind - - e = SymbolicContinuousCallback(eqs[] => affect) - @test e isa SymbolicContinuousCallback - @test isequal(e.eqs, eqs) - @test e.affect == affect - @test e.affect_neg == affect + @test isequal(equations(e), eqs) @test e.rootfind == SciMLBase.LeftRootFind # with only positive edge affect - e = SymbolicContinuousCallback(eqs[], affect, affect_neg = nothing) @test e isa SymbolicContinuousCallback - @test isequal(e.eqs, eqs) - @test e.affect == affect - @test isnothing(e.affect_neg) - @test e.rootfind == SciMLBase.LeftRootFind - - e = SymbolicContinuousCallback(eqs, affect, affect_neg = nothing) - @test e isa SymbolicContinuousCallback - @test isequal(e.eqs, eqs) - @test e.affect == affect - @test isnothing(e.affect_neg) - @test e.rootfind == SciMLBase.LeftRootFind - - e = SymbolicContinuousCallback(eqs, affect, affect_neg = nothing) - @test e isa SymbolicContinuousCallback - @test isequal(e.eqs, eqs) - @test e.affect == affect - @test isnothing(e.affect_neg) - @test e.rootfind == SciMLBase.LeftRootFind - - e = SymbolicContinuousCallback(eqs[], affect, affect_neg = nothing) - @test e isa SymbolicContinuousCallback - @test isequal(e.eqs, eqs) - @test e.affect == affect + @test isequal(equations(e), eqs) @test isnothing(e.affect_neg) @test e.rootfind == SciMLBase.LeftRootFind # with explicit edge affects - - e = SymbolicContinuousCallback(eqs[], affect, affect_neg = affect_neg) - @test e isa SymbolicContinuousCallback - @test isequal(e.eqs, eqs) - @test e.affect == affect - @test e.affect_neg == affect_neg - @test e.rootfind == SciMLBase.LeftRootFind - - e = SymbolicContinuousCallback(eqs, affect, affect_neg = affect_neg) - @test e isa SymbolicContinuousCallback - @test isequal(e.eqs, eqs) - @test e.affect == affect - @test e.affect_neg == affect_neg - @test e.rootfind == SciMLBase.LeftRootFind - - e = SymbolicContinuousCallback(eqs, affect, affect_neg = affect_neg) - @test e isa SymbolicContinuousCallback - @test isequal(e.eqs, eqs) - @test e.affect == affect - @test e.affect_neg == affect_neg - @test e.rootfind == SciMLBase.LeftRootFind - e = SymbolicContinuousCallback(eqs[], affect, affect_neg = affect_neg) @test e isa SymbolicContinuousCallback - @test isequal(e.eqs, eqs) - @test e.affect == affect - @test e.affect_neg == affect_neg + @test isequal(equations(e), eqs) @test e.rootfind == SciMLBase.LeftRootFind # with different root finding ops - e = SymbolicContinuousCallback( eqs[], affect, affect_neg = affect_neg, rootfind = SciMLBase.LeftRootFind) @test e isa SymbolicContinuousCallback - @test isequal(e.eqs, eqs) - @test e.affect == affect - @test e.affect_neg == affect_neg + @test isequal(equations(e), eqs) @test e.rootfind == SciMLBase.LeftRootFind - - e = SymbolicContinuousCallback( - eqs[], affect, affect_neg = affect_neg, rootfind = SciMLBase.RightRootFind) - @test e isa SymbolicContinuousCallback - @test isequal(e.eqs, eqs) - @test e.affect == affect - @test e.affect_neg == affect_neg - @test e.rootfind == SciMLBase.RightRootFind - - e = SymbolicContinuousCallback( - eqs[], affect, affect_neg = affect_neg, rootfind = SciMLBase.NoRootFind) - @test e isa SymbolicContinuousCallback - @test isequal(e.eqs, eqs) - @test e.affect == affect - @test e.affect_neg == affect_neg - @test e.rootfind == SciMLBase.NoRootFind - # test plural constructor - - e = SymbolicContinuousCallbacks(eqs[]) - @test e isa Vector{SymbolicContinuousCallback} - @test isequal(e[].eqs, eqs) - @test e[].affect == NULL_AFFECT - - e = SymbolicContinuousCallbacks(eqs) - @test e isa Vector{SymbolicContinuousCallback} - @test isequal(e[].eqs, eqs) - @test e[].affect == NULL_AFFECT - - e = SymbolicContinuousCallbacks(eqs[] => affect) - @test e isa Vector{SymbolicContinuousCallback} - @test isequal(e[].eqs, eqs) - @test e[].affect == affect - - e = SymbolicContinuousCallbacks(eqs => affect) - @test e isa Vector{SymbolicContinuousCallback} - @test isequal(e[].eqs, eqs) - @test e[].affect == affect - - e = SymbolicContinuousCallbacks([eqs[] => affect]) - @test e isa Vector{SymbolicContinuousCallback} - @test isequal(e[].eqs, eqs) - @test e[].affect == affect - - e = SymbolicContinuousCallbacks([eqs => affect]) - @test e isa Vector{SymbolicContinuousCallback} - @test isequal(e[].eqs, eqs) - @test e[].affect == affect - - e = SymbolicContinuousCallbacks(SymbolicContinuousCallbacks([eqs => affect])) - @test e isa Vector{SymbolicContinuousCallback} - @test isequal(e[].eqs, eqs) - @test e[].affect == affect end @testset "ImperativeAffect constructors" begin @@ -341,409 +201,357 @@ end @test m.ctx === 3 end -## - -@named sys = ODESystem(eqs, t, continuous_events = [x ~ 1]) -@test getfield(sys, :continuous_events)[] == - SymbolicContinuousCallback(Equation[x ~ 1], NULL_AFFECT) -@test isequal(equations(getfield(sys, :continuous_events))[], x ~ 1) -fsys = flatten(sys) -@test isequal(equations(getfield(fsys, :continuous_events))[], x ~ 1) - -@named sys2 = ODESystem([D(x) ~ 1], t, continuous_events = [x ~ 2], systems = [sys]) -@test getfield(sys2, :continuous_events)[] == - SymbolicContinuousCallback(Equation[x ~ 2], NULL_AFFECT) -@test all(ModelingToolkit.continuous_events(sys2) .== [ - SymbolicContinuousCallback(Equation[x ~ 2], NULL_AFFECT), - SymbolicContinuousCallback(Equation[sys.x ~ 1], NULL_AFFECT) -]) - -@test isequal(equations(getfield(sys2, :continuous_events))[1], x ~ 2) -@test length(ModelingToolkit.continuous_events(sys2)) == 2 -@test isequal(ModelingToolkit.continuous_events(sys2)[1].eqs[], x ~ 2) -@test isequal(ModelingToolkit.continuous_events(sys2)[2].eqs[], sys.x ~ 1) - -sys = complete(sys) -sys_nosplit = complete(sys; split = false) -sys2 = complete(sys2) -# Functions should be generated for root-finding equations -prob = ODEProblem(sys, Pair[], (0.0, 2.0)) -p0 = 0 -t0 = 0 -@test get_callback(prob) isa ModelingToolkit.DiffEqCallbacks.ContinuousCallback -cb = ModelingToolkit.generate_rootfinding_callback(sys) -cond = cb.condition -out = [0.0] -cond.rf_ip(out, [0], p0, t0) -@test out[] ≈ -1 # signature is u,p,t -cond.rf_ip(out, [1], p0, t0) -@test out[] ≈ 0 # signature is u,p,t -cond.rf_ip(out, [2], p0, t0) -@test out[] ≈ 1 # signature is u,p,t - -prob = ODEProblem(sys, Pair[], (0.0, 2.0)) -prob_nosplit = ODEProblem(sys_nosplit, Pair[], (0.0, 2.0)) -sol = solve(prob, Tsit5()) -sol_nosplit = solve(prob_nosplit, Tsit5()) -@test minimum(t -> abs(t - 1), sol.t) < 1e-10 # test that the solver stepped at the root -@test minimum(t -> abs(t - 1), sol_nosplit.t) < 1e-10 # test that the solver stepped at the root - -# Test that a user provided callback is respected -test_callback = DiscreteCallback(x -> x, x -> x) -prob = ODEProblem(sys, Pair[], (0.0, 2.0), callback = test_callback) -prob_nosplit = ODEProblem(sys_nosplit, Pair[], (0.0, 2.0), callback = test_callback) -cbs = get_callback(prob) -cbs_nosplit = get_callback(prob_nosplit) -@test cbs isa CallbackSet -@test cbs.discrete_callbacks[1] == test_callback -@test cbs_nosplit isa CallbackSet -@test cbs_nosplit.discrete_callbacks[1] == test_callback - -prob = ODEProblem(sys2, Pair[], (0.0, 3.0)) -cb = get_callback(prob) -@test cb isa ModelingToolkit.DiffEqCallbacks.VectorContinuousCallback - -cond = cb.condition -out = [0.0, 0.0] -# the root to find is 2 -cond.rf_ip(out, [0, 0], p0, t0) -@test out[1] ≈ -2 # signature is u,p,t -cond.rf_ip(out, [1, 0], p0, t0) -@test out[1] ≈ -1 # signature is u,p,t -cond.rf_ip(out, [2, 0], p0, t0) # this should return 0 -@test out[1] ≈ 0 # signature is u,p,t - -# the root to find is 1 -out = [0.0, 0.0] -cond.rf_ip(out, [0, 0], p0, t0) -@test out[2] ≈ -1 # signature is u,p,t -cond.rf_ip(out, [0, 1], p0, t0) # this should return 0 -@test out[2] ≈ 0 # signature is u,p,t -cond.rf_ip(out, [0, 2], p0, t0) -@test out[2] ≈ 1 # signature is u,p,t - -sol = solve(prob, Tsit5(); abstol = 1e-14, reltol = 1e-14) -@test minimum(t -> abs(t - 1), sol.t) < 1e-10 # test that the solver stepped at the first root -@test minimum(t -> abs(t - 2), sol.t) < 1e-10 # test that the solver stepped at the second root - -@named sys = ODESystem(eqs, t, continuous_events = [x ~ 1, x ~ 2]) # two root eqs using the same unknown -sys = complete(sys) -prob = ODEProblem(sys, Pair[], (0.0, 3.0)) -@test get_callback(prob) isa ModelingToolkit.DiffEqCallbacks.VectorContinuousCallback -sol = solve(prob, Tsit5(); abstol = 1e-14, reltol = 1e-14) -@test minimum(t -> abs(t - 1), sol.t) < 1e-10 # test that the solver stepped at the first root -@test minimum(t -> abs(t - 2), sol.t) < 1e-10 # test that the solver stepped at the second root - -## Test bouncing ball with equation affect -@variables x(t)=1 v(t)=0 - -root_eqs = [x ~ 0] -affect = [v ~ -v] - -@named ball = ODESystem([D(x) ~ v - D(v) ~ -9.8], t, continuous_events = root_eqs => affect) - -@test getfield(ball, :continuous_events)[] == - SymbolicContinuousCallback(Equation[x ~ 0], Equation[v ~ -v]) -ball = structural_simplify(ball) - -@test length(ModelingToolkit.continuous_events(ball)) == 1 - -tspan = (0.0, 5.0) -prob = ODEProblem(ball, Pair[], tspan) -sol = solve(prob, Tsit5()) -@test 0 <= minimum(sol[x]) <= 1e-10 # the ball never went through the floor but got very close -# plot(sol) - -## Test bouncing ball in 2D with walls -@variables x(t)=1 y(t)=0 vx(t)=0 vy(t)=1 - -continuous_events = [[x ~ 0] => [vx ~ -vx] - [y ~ -1.5, y ~ 1.5] => [vy ~ -vy]] - -@named ball = ODESystem( - [D(x) ~ vx - D(y) ~ vy - D(vx) ~ -9.8 - D(vy) ~ -0.01vy], t; continuous_events) - -_ball = ball -ball = structural_simplify(_ball) -ball_nosplit = structural_simplify(_ball; split = false) - -tspan = (0.0, 5.0) -prob = ODEProblem(ball, Pair[], tspan) -prob_nosplit = ODEProblem(ball_nosplit, Pair[], tspan) - -cb = get_callback(prob) -@test cb isa ModelingToolkit.DiffEqCallbacks.VectorContinuousCallback -@test getfield(ball, :continuous_events)[1] == - SymbolicContinuousCallback(Equation[x ~ 0], Equation[vx ~ -vx]) -@test getfield(ball, :continuous_events)[2] == - SymbolicContinuousCallback(Equation[y ~ -1.5, y ~ 1.5], Equation[vy ~ -vy]) -cond = cb.condition -out = [0.0, 0.0, 0.0] -cond.rf_ip(out, [0, 0, 0, 0], p0, t0) -@test out ≈ [0, 1.5, -1.5] - -sol = solve(prob, Tsit5()) -sol_nosplit = solve(prob_nosplit, Tsit5()) -@test 0 <= minimum(sol[x]) <= 1e-10 # the ball never went through the floor but got very close -@test minimum(sol[y]) ≈ -1.5 # check wall conditions -@test maximum(sol[y]) ≈ 1.5 # check wall conditions -@test 0 <= minimum(sol_nosplit[x]) <= 1e-10 # the ball never went through the floor but got very close -@test minimum(sol_nosplit[y]) ≈ -1.5 # check wall conditions -@test maximum(sol_nosplit[y]) ≈ 1.5 # check wall conditions - -# tv = sort([LinRange(0, 5, 200); sol.t]) -# plot(sol(tv)[y], sol(tv)[x], line_z=tv) -# vline!([-1.5, 1.5], l=(:black, 5), primary=false) -# hline!([0], l=(:black, 5), primary=false) - -## Test multi-variable affect -# in this test, there are two variables affected by a single event. -continuous_events = [ - [x ~ 0] => [vx ~ -vx, vy ~ -vy] -] - -@named ball = ODESystem([D(x) ~ vx - D(y) ~ vy - D(vx) ~ -1 - D(vy) ~ 0], t; continuous_events) - -ball_nosplit = structural_simplify(ball) -ball = structural_simplify(ball) - -tspan = (0.0, 5.0) -prob = ODEProblem(ball, Pair[], tspan) -prob_nosplit = ODEProblem(ball_nosplit, Pair[], tspan) -sol = solve(prob, Tsit5()) -sol_nosplit = solve(prob_nosplit, Tsit5()) -@test 0 <= minimum(sol[x]) <= 1e-10 # the ball never went through the floor but got very close -@test -minimum(sol[y]) ≈ maximum(sol[y]) ≈ sqrt(2) # the ball will never go further than √2 in either direction (gravity was changed to 1 to get this particular number) -@test 0 <= minimum(sol_nosplit[x]) <= 1e-10 # the ball never went through the floor but got very close -@test -minimum(sol_nosplit[y]) ≈ maximum(sol_nosplit[y]) ≈ sqrt(2) # the ball will never go further than √2 in either direction (gravity was changed to 1 to get this particular number) - -# tv = sort([LinRange(0, 5, 200); sol.t]) -# plot(sol(tv)[y], sol(tv)[x], line_z=tv) -# vline!([-1.5, 1.5], l=(:black, 5), primary=false) -# hline!([0], l=(:black, 5), primary=false) - -# issue https://github.com/SciML/ModelingToolkit.jl/issues/1386 -# tests that it works for ODAESystem -@variables vs(t) v(t) vmeasured(t) -eq = [vs ~ sin(2pi * t) - D(v) ~ vs - v - D(vmeasured) ~ 0.0] -ev = [sin(20pi * t) ~ 0.0] => [vmeasured ~ v] -@named sys = ODESystem(eq, t, continuous_events = ev) -sys = structural_simplify(sys) -prob = ODEProblem(sys, zeros(2), (0.0, 5.1)) -sol = solve(prob, Tsit5()) -@test all(minimum((0:0.05:5) .- sol.t', dims = 2) .< 0.0001) # test that the solver stepped every 0.05s as dictated by event -@test sol([0.25 - eps()])[vmeasured][] == sol([0.23])[vmeasured][] # test the hold property - -## https://github.com/SciML/ModelingToolkit.jl/issues/1528 -Dₜ = D +@testset "Condition Compilation" begin + @named sys = ODESystem(eqs, t, continuous_events = [x ~ 1]) + @test getfield(sys, :continuous_events)[] == + SymbolicContinuousCallback(Equation[x ~ 1], nothing) + @test isequal(equations(getfield(sys, :continuous_events))[], x ~ 1) + fsys = flatten(sys) + @test isequal(equations(getfield(fsys, :continuous_events))[], x ~ 1) + + @named sys2 = ODESystem([D(x) ~ 1], t, continuous_events = [x ~ 2], systems = [sys]) + @test getfield(sys2, :continuous_events)[] == + SymbolicContinuousCallback(Equation[x ~ 2], nothing) + @test all(ModelingToolkit.continuous_events(sys2) .== [ + SymbolicContinuousCallback(Equation[x ~ 2], nothing), + SymbolicContinuousCallback(Equation[sys.x ~ 1], nothing) + ]) + + @test isequal(equations(getfield(sys2, :continuous_events))[1], x ~ 2) + @test length(ModelingToolkit.continuous_events(sys2)) == 2 + @test isequal(equations(ModelingToolkit.continuous_events(sys2)[1])[], x ~ 2) + @test isequal(equations(ModelingToolkit.continuous_events(sys2)[2])[], sys.x ~ 1) + + sys = complete(sys) + sys_nosplit = complete(sys; split = false) + sys2 = complete(sys2) + + # Test proper rootfinding + prob = ODEProblem(sys, Pair[], (0.0, 2.0)) + p0 = 0 + t0 = 0 + @test get_callback(prob) isa ModelingToolkit.DiffEqCallbacks.ContinuousCallback + cb = ModelingToolkit.generate_continuous_callbacks(sys) + cond = cb.condition + out = [0.0] + cond.f_iip(out, [0], p0, t0) + @test out[] ≈ -1 # signature is u,p,t + cond.f_iip(out, [1], p0, t0) + @test out[] ≈ 0 # signature is u,p,t + cond.f_iip(out, [2], p0, t0) + @test out[] ≈ 1 # signature is u,p,t + + prob = ODEProblem(sys, Pair[], (0.0, 2.0)) + prob_nosplit = ODEProblem(sys_nosplit, Pair[], (0.0, 2.0)) + sol = solve(prob, Tsit5()) + sol_nosplit = solve(prob_nosplit, Tsit5()) + @test minimum(t -> abs(t - 1), sol.t) < 1e-10 # test that the solver stepped at the root + @test minimum(t -> abs(t - 1), sol_nosplit.t) < 1e-10 # test that the solver stepped at the root + + # Test user-provided callback is respected + test_callback = DiscreteCallback(x -> x, x -> x) + prob = ODEProblem(sys, Pair[], (0.0, 2.0), callback = test_callback) + prob_nosplit = ODEProblem(sys_nosplit, Pair[], (0.0, 2.0), callback = test_callback) + cbs = get_callback(prob) + cbs_nosplit = get_callback(prob_nosplit) + @test cbs isa CallbackSet + @test cbs.discrete_callbacks[1] == test_callback + @test cbs_nosplit isa CallbackSet + @test cbs_nosplit.discrete_callbacks[1] == test_callback + + prob = ODEProblem(sys2, Pair[], (0.0, 3.0)) + cb = get_callback(prob) + @test cb isa ModelingToolkit.DiffEqCallbacks.VectorContinuousCallback + + cond = cb.condition + out = [0.0, 0.0] + # the root to find is 2 + cond.f_iip(out, [0, 0], p0, t0) + @test out[1] ≈ -2 # signature is u,p,t + cond.f_iip(out, [1, 0], p0, t0) + @test out[1] ≈ -1 # signature is u,p,t + cond.f_iip(out, [2, 0], p0, t0) # this should return 0 + @test out[1] ≈ 0 # signature is u,p,t + + # the root to find is 1 + out = [0.0, 0.0] + cond.f_iip(out, [0, 0], p0, t0) + @test out[2] ≈ -1 # signature is u,p,t + cond.f_iip(out, [0, 1], p0, t0) # this should return 0 + @test out[2] ≈ 0 # signature is u,p,t + cond.f_iip(out, [0, 2], p0, t0) + @test out[2] ≈ 1 # signature is u,p,t -@parameters u(t) [input = true] # Indicate that this is a controlled input -@parameters y(t) [output = true] # Indicate that this is a measured output + sol = solve(prob, Tsit5()) + @test minimum(t -> abs(t - 1), sol.t) < 1e-9 # test that the solver stepped at the first root + @test minimum(t -> abs(t - 2), sol.t) < 1e-9 # test that the solver stepped at the second root -function Mass(; name, m = 1.0, p = 0, v = 0) - ps = @parameters m = m - sts = @variables pos(t)=p vel(t)=v - eqs = Dₜ(pos) ~ vel - ODESystem(eqs, t, [pos, vel], ps; name) -end -function Spring(; name, k = 1e4) - ps = @parameters k = k - @variables x(t) = 0 # Spring deflection - ODESystem(Equation[], t, [x], ps; name) -end -function Damper(; name, c = 10) - ps = @parameters c = c - @variables vel(t) = 0 - ODESystem(Equation[], t, [vel], ps; name) -end -function SpringDamper(; name, k = false, c = false) - spring = Spring(; name = :spring, k) - damper = Damper(; name = :damper, c) - compose(ODESystem(Equation[], t; name), - spring, damper) -end -connect_sd(sd, m1, m2) = [sd.spring.x ~ m1.pos - m2.pos, sd.damper.vel ~ m1.vel - m2.vel] -sd_force(sd) = -sd.spring.k * sd.spring.x - sd.damper.c * sd.damper.vel -@named mass1 = Mass(; m = 1) -@named mass2 = Mass(; m = 1) -@named sd = SpringDamper(; k = 1000, c = 10) -function Model(u, d = 0) - eqs = [connect_sd(sd, mass1, mass2) - Dₜ(mass1.vel) ~ (sd_force(sd) + u) / mass1.m - Dₜ(mass2.vel) ~ (-sd_force(sd) + d) / mass2.m] - @named _model = ODESystem(eqs, t; observed = [y ~ mass2.pos]) - @named model = compose(_model, mass1, mass2, sd) + @named sys = ODESystem(eqs, t, continuous_events = [x ~ 1, x ~ 2]) # two root eqs using the same unknown + sys = complete(sys) + prob = ODEProblem(sys, Pair[], (0.0, 3.0)) + @test get_callback(prob) isa ModelingToolkit.DiffEqCallbacks.VectorContinuousCallback + sol = solve(prob, Tsit5()) + @test minimum(t -> abs(t - 1), sol.t) < 1e-9 # test that the solver stepped at the first root + @test minimum(t -> abs(t - 2), sol.t) < 1e-9 # test that the solver stepped at the second root end -model = Model(sin(30t)) -sys = structural_simplify(model) -@test isempty(ModelingToolkit.continuous_events(sys)) -let - function testsol(osys, u0, p, tspan; tstops = Float64[], paramtotest = nothing, - kwargs...) - oprob = ODEProblem(complete(osys), u0, tspan, p; kwargs...) - sol = solve(oprob, Tsit5(); tstops = tstops, abstol = 1e-10, reltol = 1e-10) - @test isapprox(sol(1.0000000001)[1] - sol(0.999999999)[1], 1.0; rtol = 1e-6) - paramtotest === nothing || (@test sol.ps[paramtotest] == 1.0) - @test isapprox(sol(4.0)[1], 2 * exp(-2.0)) - sol - end +@testset "Bouncing Ball" begin + ###### 1D Bounce + @variables x(t)=1 v(t)=0 - @parameters k t1 t2 - @variables A(t) B(t) + root_eqs = [x ~ 0] + affect = [v ~ -Pre(v)] - cond1 = (t == t1) - affect1 = [A ~ A + 1] - cb1 = cond1 => affect1 - cond2 = (t == t2) - affect2 = [k ~ 1.0] - cb2 = cond2 => affect2 + @named ball = ODESystem( + [D(x) ~ v + D(v) ~ -9.8], t, continuous_events = root_eqs => affect) - ∂ₜ = D - eqs = [∂ₜ(A) ~ -k * A] - @named osys = ODESystem(eqs, t, [A], [k, t1, t2], discrete_events = [cb1, cb2]) - u0 = [A => 1.0] - p = [k => 0.0, t1 => 1.0, t2 => 2.0] - tspan = (0.0, 4.0) - testsol(osys, u0, p, tspan; tstops = [1.0, 2.0], paramtotest = k) + @test only(continuous_events(ball)) == + SymbolicContinuousCallback(Equation[x ~ 0], Equation[v ~ -Pre(v)]) + ball = structural_simplify(ball) - cond1a = (t == t1) - affect1a = [A ~ A + 1, B ~ A] - cb1a = cond1a => affect1a - @named osys1 = ODESystem(eqs, t, [A, B], [k, t1, t2], discrete_events = [cb1a, cb2]) - u0′ = [A => 1.0, B => 0.0] - sol = testsol( - osys1, u0′, p, tspan; tstops = [1.0, 2.0], check_length = false, paramtotest = k) - @test sol(1.0000001, idxs = B) == 2.0 + @test length(ModelingToolkit.continuous_events(ball)) == 1 - # same as above - but with set-time event syntax - cb1‵ = [1.0] => affect1 # needs to be a Vector for the event to happen only once - cb2‵ = [2.0] => affect2 - @named osys‵ = ODESystem(eqs, t, [A], [k], discrete_events = [cb1‵, cb2‵]) - testsol(osys‵, u0, p, tspan; paramtotest = k) + tspan = (0.0, 5.0) + prob = ODEProblem(ball, Pair[], tspan) + sol = solve(prob, Tsit5()) + @test 0 <= minimum(sol[x]) <= 1e-10 # the ball never went through the floor but got very close + + ###### 2D bouncing ball + @variables x(t)=1 y(t)=0 vx(t)=0 vy(t)=1 + + events = [[x ~ 0] => [vx ~ -Pre(vx)] + [y ~ -1.5, y ~ 1.5] => [vy ~ -Pre(vy)]] + + @named ball = ODESystem( + [D(x) ~ vx + D(y) ~ vy + D(vx) ~ -9.8 + D(vy) ~ -0.01vy], t; continuous_events = events) + + _ball = ball + ball = structural_simplify(_ball) + ball_nosplit = structural_simplify(_ball; split = false) + + tspan = (0.0, 5.0) + prob = ODEProblem(ball, Pair[], tspan) + prob_nosplit = ODEProblem(ball_nosplit, Pair[], tspan) + + cb = get_callback(prob) + @test cb isa ModelingToolkit.DiffEqCallbacks.VectorContinuousCallback + @test getfield(ball, :continuous_events)[1] == + SymbolicContinuousCallback(Equation[x ~ 0], Equation[vx ~ -Pre(vx)]) + @test getfield(ball, :continuous_events)[2] == + SymbolicContinuousCallback(Equation[y ~ -1.5, y ~ 1.5], Equation[vy ~ -Pre(vy)]) + cond = cb.condition + out = [0.0, 0.0, 0.0] + p0 = 0.0 + t0 = 0.0 + cond.f_iip(out, [0, 0, 0, 0], p0, t0) + @test out ≈ [0, 1.5, -1.5] - # mixing discrete affects - @named osys3 = ODESystem(eqs, t, [A], [k, t1, t2], discrete_events = [cb1, cb2‵]) - testsol(osys3, u0, p, tspan; tstops = [1.0], paramtotest = k) + sol = solve(prob, Tsit5()) + sol_nosplit = solve(prob_nosplit, Tsit5()) + @test 0 <= minimum(sol[x]) <= 1e-10 # the ball never went through the floor but got very close + @test minimum(sol[y]) ≈ -1.5 # check wall conditions + @test maximum(sol[y]) ≈ 1.5 # check wall conditions + @test 0 <= minimum(sol_nosplit[x]) <= 1e-10 # the ball never went through the floor but got very close + @test minimum(sol_nosplit[y]) ≈ -1.5 # check wall conditions + @test maximum(sol_nosplit[y]) ≈ 1.5 # check wall conditions + + ## Test multi-variable affect + # in this test, there are two variables affected by a single event. + events = [[x ~ 0] => [vx ~ -Pre(vx), vy ~ -Pre(vy)]] + + @named ball = ODESystem( + [D(x) ~ vx + D(y) ~ vy + D(vx) ~ -1 + D(vy) ~ 0], t; continuous_events = events) + + ball_nosplit = structural_simplify(ball) + ball = structural_simplify(ball) + + tspan = (0.0, 5.0) + prob = ODEProblem(ball, Pair[], tspan) + prob_nosplit = ODEProblem(ball_nosplit, Pair[], tspan) + sol = solve(prob, Tsit5()) + sol_nosplit = solve(prob_nosplit, Tsit5()) + @test 0 <= minimum(sol[x]) <= 1e-10 # the ball never went through the floor but got very close + @test -minimum(sol[y]) ≈ maximum(sol[y]) ≈ sqrt(2) # the ball will never go further than √2 in either direction (gravity was changed to 1 to get this particular number) + @test 0 <= minimum(sol_nosplit[x]) <= 1e-10 # the ball never went through the floor but got very close + @test -minimum(sol_nosplit[y]) ≈ maximum(sol_nosplit[y]) ≈ sqrt(2) # the ball will never go further than √2 in either direction (gravity was changed to 1 to get this particular number) +end - # mixing with a func affect - function affect!(integrator, u, p, ctx) - integrator.ps[p.k] = 1.0 - nothing - end - cb2‵‵ = [2.0] => (affect!, [], [k], [k], nothing) - @named osys4 = ODESystem(eqs, t, [A], [k, t1], discrete_events = [cb1, cb2‵‵]) - oprob4 = ODEProblem(complete(osys4), u0, tspan, p) - testsol(osys4, u0, p, tspan; tstops = [1.0], paramtotest = k) +# issue https://github.com/SciML/ModelingToolkit.jl/issues/1386 +# tests that it works for ODAESystem +@testset "ODAESystem" begin + @variables vs(t) v(t) vmeasured(t) + eq = [vs ~ sin(2pi * t) + D(v) ~ vs - v + D(vmeasured) ~ 0.0] + ev = [sin(20pi * t) ~ 0.0] => [vmeasured ~ Pre(v)] + @named sys = ODESystem(eq, t, continuous_events = ev) + sys = structural_simplify(sys) + prob = ODEProblem(sys, zeros(2), (0.0, 5.1)) + sol = solve(prob, Tsit5()) + @test all(minimum((0:0.1:5) .- sol.t', dims = 2) .< 0.0001) # test that the solver stepped every 0.1s as dictated by event + @test sol([0.25 - eps()])[vmeasured][] == sol([0.23])[vmeasured][] # test the hold property +end - # mixing with symbolic condition in the func affect - cb2‵‵‵ = (t == t2) => (affect!, [], [k], [k], nothing) - @named osys5 = ODESystem(eqs, t, [A], [k, t1, t2], discrete_events = [cb1, cb2‵‵‵]) - testsol(osys5, u0, p, tspan; tstops = [1.0, 2.0]) - @named osys6 = ODESystem(eqs, t, [A], [k, t1, t2], discrete_events = [cb2‵‵‵, cb1]) - testsol(osys6, u0, p, tspan; tstops = [1.0, 2.0], paramtotest = k) +## https://github.com/SciML/ModelingToolkit.jl/issues/1528 +@testset "Handle Empty Events" begin + Dₜ = D - # mix a continuous event too - cond3 = A ~ 0.1 - affect3 = [k ~ 0.0] - cb3 = cond3 => affect3 - @named osys7 = ODESystem(eqs, t, [A], [k, t1, t2], discrete_events = [cb1, cb2‵‵‵], - continuous_events = [cb3]) - sol = testsol(osys7, u0, p, (0.0, 10.0); tstops = [1.0, 2.0]) - @test isapprox(sol(10.0)[1], 0.1; atol = 1e-10, rtol = 1e-10) + @parameters u(t) [input = true] # Indicate that this is a controlled input + @parameters y(t) [output = true] # Indicate that this is a measured output + + function Mass(; name, m = 1.0, p = 0, v = 0) + ps = @parameters m = m + sts = @variables pos(t)=p vel(t)=v + eqs = Dₜ(pos) ~ vel + ODESystem(eqs, t, [pos, vel], ps; name) + end + function Spring(; name, k = 1e4) + ps = @parameters k = k + @variables x(t) = 0 # Spring deflection + ODESystem(Equation[], t, [x], ps; name) + end + function Damper(; name, c = 10) + ps = @parameters c = c + @variables vel(t) = 0 + ODESystem(Equation[], t, [vel], ps; name) + end + function SpringDamper(; name, k = false, c = false) + spring = Spring(; name = :spring, k) + damper = Damper(; name = :damper, c) + compose(ODESystem(Equation[], t; name), + spring, damper) + end + connect_sd(sd, m1, m2) = [ + sd.spring.x ~ m1.pos - m2.pos, sd.damper.vel ~ m1.vel - m2.vel] + sd_force(sd) = -sd.spring.k * sd.spring.x - sd.damper.c * sd.damper.vel + @named mass1 = Mass(; m = 1) + @named mass2 = Mass(; m = 1) + @named sd = SpringDamper(; k = 1000, c = 10) + function Model(u, d = 0) + eqs = [connect_sd(sd, mass1, mass2) + Dₜ(mass1.vel) ~ (sd_force(sd) + u) / mass1.m + Dₜ(mass2.vel) ~ (-sd_force(sd) + d) / mass2.m] + @named _model = ODESystem(eqs, t; observed = [y ~ mass2.pos]) + @named model = compose(_model, mass1, mass2, sd) + end + model = Model(sin(30t)) + sys = structural_simplify(model) + @test isempty(ModelingToolkit.continuous_events(sys)) end -let - function testsol(ssys, u0, p, tspan; tstops = Float64[], paramtotest = nothing, +@testset "SDE/ODESystem Discrete Callbacks" begin + function testsol( + sys, probtype, solver, u0, p, tspan; tstops = Float64[], paramtotest = nothing, kwargs...) - sprob = SDEProblem(complete(ssys), u0, tspan, p; kwargs...) - sol = solve(sprob, RI5(); tstops = tstops, abstol = 1e-10, reltol = 1e-10) - @test isapprox(sol(1.0000000001)[1] - sol(0.999999999)[1], 1.0; rtol = 1e-4) - paramtotest === nothing || (@test sol.ps[paramtotest] == 1.0) - @test isapprox(sol(4.0)[1], 2 * exp(-2.0), atol = 1e-4) + prob = probtype(complete(sys), u0, tspan, p; kwargs...) + sol = solve(prob, solver(); tstops = tstops, abstol = 1e-10, reltol = 1e-10) + @test isapprox(sol(1.0000000001)[1] - sol(0.999999999)[1], 1.0; rtol = 1e-6) + paramtotest === nothing || (@test sol.ps[paramtotest] == [0.0, 1.0]) + @test isapprox(sol(4.0)[1], 2 * exp(-2.0); rtol = 1e-6) sol end - @parameters k t1 t2 + @parameters k(t) t1 t2 @variables A(t) B(t) cond1 = (t == t1) - affect1 = [A ~ A + 1] + affect1 = [A ~ Pre(A) + 1] cb1 = cond1 => affect1 cond2 = (t == t2) affect2 = [k ~ 1.0] cb2 = cond2 => affect2 + cb2 = SymbolicDiscreteCallback(cb2, discrete_parameters = [k], iv = t) ∂ₜ = D eqs = [∂ₜ(A) ~ -k * A] - @named ssys = SDESystem(eqs, [0.0], t, [A], [k, t1, t2], - discrete_events = [cb1, cb2]) + @named osys = ODESystem(eqs, t, [A], [k, t1, t2], discrete_events = [cb1, cb2]) + @named ssys = SDESystem(eqs, [0.0], t, [A], [k, t1, t2], discrete_events = [cb1, cb2]) u0 = [A => 1.0] p = [k => 0.0, t1 => 1.0, t2 => 2.0] tspan = (0.0, 4.0) - testsol(ssys, u0, p, tspan; tstops = [1.0, 2.0], paramtotest = k) + testsol(osys, ODEProblem, Tsit5, u0, p, tspan; tstops = [1.0, 2.0], paramtotest = k) + testsol(ssys, SDEProblem, RI5, u0, p, tspan; tstops = [1.0, 2.0], paramtotest = k) cond1a = (t == t1) - affect1a = [A ~ A + 1, B ~ A] + affect1a = [A ~ Pre(A) + 1, B ~ A] cb1a = cond1a => affect1a + @named osys1 = ODESystem(eqs, t, [A, B], [k, t1, t2], discrete_events = [cb1a, cb2]) @named ssys1 = SDESystem(eqs, [0.0], t, [A, B], [k, t1, t2], discrete_events = [cb1a, cb2]) u0′ = [A => 1.0, B => 0.0] - sol = testsol( - ssys1, u0′, p, tspan; tstops = [1.0, 2.0], check_length = false, paramtotest = k) - @test sol(1.0000001, idxs = 2) == 2.0 + sol = testsol(osys1, ODEProblem, Tsit5, u0′, p, tspan; + tstops = [1.0, 2.0], check_length = false, paramtotest = k) + @test sol(1.0000001, idxs = B) == 2.0 + + sol = testsol(ssys1, SDEProblem, RI5, u0′, p, tspan; tstops = [1.0, 2.0], + check_length = false, paramtotest = k) + @test sol(1.0000001, idxs = B) == 2.0 # same as above - but with set-time event syntax cb1‵ = [1.0] => affect1 # needs to be a Vector for the event to happen only once - cb2‵ = [2.0] => affect2 + cb2‵ = SymbolicDiscreteCallback([2.0] => affect2, discrete_parameters = [k], iv = t) + @named osys‵ = ODESystem(eqs, t, [A], [k], discrete_events = [cb1‵, cb2‵]) @named ssys‵ = SDESystem(eqs, [0.0], t, [A], [k], discrete_events = [cb1‵, cb2‵]) - testsol(ssys‵, u0, p, tspan; paramtotest = k) + testsol(osys‵, ODEProblem, Tsit5, u0, p, tspan; paramtotest = k) + testsol(ssys‵, SDEProblem, RI5, u0, p, tspan; paramtotest = k) # mixing discrete affects + @named osys3 = ODESystem(eqs, t, [A], [k, t1, t2], discrete_events = [cb1, cb2‵]) @named ssys3 = SDESystem(eqs, [0.0], t, [A], [k, t1, t2], discrete_events = [cb1, cb2‵]) - testsol(ssys3, u0, p, tspan; tstops = [1.0], paramtotest = k) + testsol(osys3, ODEProblem, Tsit5, u0, p, tspan; tstops = [1.0], paramtotest = k) + testsol(ssys3, SDEProblem, RI5, u0, p, tspan; tstops = [1.0], paramtotest = k) # mixing with a func affect function affect!(integrator, u, p, ctx) - setp(integrator, p.k)(integrator, 1.0) + integrator.ps[p.k] = 1.0 nothing end cb2‵‵ = [2.0] => (affect!, [], [k], [k], nothing) + @named osys4 = ODESystem(eqs, t, [A], [k, t1], discrete_events = [cb1, cb2‵‵]) @named ssys4 = SDESystem(eqs, [0.0], t, [A], [k, t1], discrete_events = [cb1, cb2‵‵]) - testsol(ssys4, u0, p, tspan; tstops = [1.0], paramtotest = k) + oprob4 = ODEProblem(complete(osys4), u0, tspan, p) + testsol(osys4, ODEProblem, Tsit5, u0, p, tspan; tstops = [1.0], paramtotest = k) + testsol(ssys4, SDEProblem, RI5, u0, p, tspan; tstops = [1.0], paramtotest = k) # mixing with symbolic condition in the func affect cb2‵‵‵ = (t == t2) => (affect!, [], [k], [k], nothing) + @named osys5 = ODESystem(eqs, t, [A], [k, t1, t2], discrete_events = [cb1, cb2‵‵‵]) @named ssys5 = SDESystem(eqs, [0.0], t, [A], [k, t1, t2], discrete_events = [cb1, cb2‵‵‵]) - testsol(ssys5, u0, p, tspan; tstops = [1.0, 2.0], paramtotest = k) + testsol(osys5, ODEProblem, Tsit5, u0, p, tspan; tstops = [1.0, 2.0]) + testsol(ssys5, SDEProblem, RI5, u0, p, tspan; tstops = [1.0, 2.0]) + @named osys6 = ODESystem(eqs, t, [A], [k, t1, t2], discrete_events = [cb2‵‵‵, cb1]) @named ssys6 = SDESystem(eqs, [0.0], t, [A], [k, t1, t2], discrete_events = [cb2‵‵‵, cb1]) - testsol(ssys6, u0, p, tspan; tstops = [1.0, 2.0], paramtotest = k) + testsol(osys6, ODEProblem, Tsit5, u0, p, tspan; tstops = [1.0, 2.0], paramtotest = k) + testsol(ssys6, SDEProblem, RI5, u0, p, tspan; tstops = [1.0, 2.0], paramtotest = k) # mix a continuous event too cond3 = A ~ 0.1 affect3 = [k ~ 0.0] - cb3 = cond3 => affect3 + cb3 = SymbolicContinuousCallback(cond3 => affect3, discrete_parameters = [k], iv = t) + @named osys7 = ODESystem(eqs, t, [A], [k, t1, t2], discrete_events = [cb1, cb2‵‵‵], + continuous_events = [cb3]) @named ssys7 = SDESystem(eqs, [0.0], t, [A], [k, t1, t2], discrete_events = [cb1, cb2‵‵‵], continuous_events = [cb3]) - sol = testsol(ssys7, u0, p, (0.0, 10.0); tstops = [1.0, 2.0]) + + sol = testsol(osys7, ODEProblem, Tsit5, u0, p, (0.0, 10.0); tstops = [1.0, 2.0]) + @test isapprox(sol(10.0)[1], 0.1; atol = 1e-10, rtol = 1e-10) + sol = testsol(ssys7, SDEProblem, RI5, u0, p, (0.0, 10.0); tstops = [1.0, 2.0]) @test isapprox(sol(10.0)[1], 0.1; atol = 1e-10, rtol = 1e-10) end -let rng = rng +@testset "JumpSystem Discrete Callbacks" begin function testsol(jsys, u0, p, tspan; tstops = Float64[], paramtotest = nothing, N = 40000, kwargs...) jsys = complete(jsys) @@ -751,22 +559,23 @@ let rng = rng jprob = JumpProblem(jsys, dprob, Direct(); kwargs...) sol = solve(jprob, SSAStepper(); tstops = tstops) @test (sol(1.000000000001)[1] - sol(0.99999999999)[1]) == 1 - paramtotest === nothing || (@test sol.ps[paramtotest] == 1.0) + paramtotest === nothing || (@test sol.ps[paramtotest] == [0.0, 1.0]) @test sol(40.0)[1] == 0 sol end - @parameters k t1 t2 + @parameters k(t) t1 t2 @variables A(t) B(t) + eqs = [MassActionJump(k, [A => 1], [A => -1])] cond1 = (t == t1) - affect1 = [A ~ A + 1] + affect1 = [A ~ Pre(A) + 1] cb1 = cond1 => affect1 cond2 = (t == t2) affect2 = [k ~ 1.0] cb2 = cond2 => affect2 + cb2 = SymbolicDiscreteCallback(cb2, discrete_parameters = [k], iv = t) - eqs = [MassActionJump(k, [A => 1], [A => -1])] @named jsys = JumpSystem(eqs, t, [A], [k, t1, t2], discrete_events = [cb1, cb2]) u0 = [A => 1] p = [k => 0.0, t1 => 1.0, t2 => 2.0] @@ -774,7 +583,7 @@ let rng = rng testsol(jsys, u0, p, tspan; tstops = [1.0, 2.0], rng, paramtotest = k) cond1a = (t == t1) - affect1a = [A ~ A + 1, B ~ A] + affect1a = [A ~ Pre(A) + 1, B ~ A] cb1a = cond1a => affect1a @named jsys1 = JumpSystem(eqs, t, [A, B], [k, t1, t2], discrete_events = [cb1a, cb2]) u0′ = [A => 1, B => 0] @@ -784,7 +593,7 @@ let rng = rng # same as above - but with set-time event syntax cb1‵ = [1.0] => affect1 # needs to be a Vector for the event to happen only once - cb2‵ = [2.0] => affect2 + cb2‵ = SymbolicDiscreteCallback([2.0] => affect2, discrete_parameters = [k], iv = t) @named jsys‵ = JumpSystem(eqs, t, [A], [k], discrete_events = [cb1‵, cb2‵]) testsol(jsys‵, u0, [p[1]], tspan; rng, paramtotest = k) @@ -810,7 +619,7 @@ let rng = rng testsol(jsys6, u0, p, tspan; tstops = [1.0, 2.0], rng, paramtotest = k) end -let +@testset "Namespacing" begin function oscillator_ce(k = 1.0; name) sts = @variables x(t)=1.0 v(t)=0.0 F(t) ps = @parameters k=k Θ=0.5 @@ -829,8 +638,8 @@ let sol = solve(prob, Tsit5(), saveat = 0.1) @test typeof(oneosc_ce_simpl) == ODESystem - @test sol[oscce.x, 6] < 1.0 # test whether x(t) decreases over time - @test sol[oscce.x, 18] > 0.5 # test whether event happened + @test sol(0.5, idxs = oscce.x) < 1.0 # test whether x(t) decreases over time + @test sol(1.5, idxs = oscce.x) > 0.5 # test whether event happened end @testset "Additional SymbolicContinuousCallback options" begin @@ -1066,12 +875,12 @@ end @testset "Discrete variable timeseries" begin @variables x(t) @parameters a(t) b(t) c(t) - cb1 = [x ~ 1.0] => [a ~ -a] + cb1 = SymbolicContinuousCallback([x ~ 1.0] => [a ~ -Pre(a)], discrete_parameters = [a]) function save_affect!(integ, u, p, ctx) integ.ps[p.b] = 5.0 end cb2 = [x ~ 0.5] => (save_affect!, [], [b], [b], nothing) - cb3 = 1.0 => [c ~ t] + cb3 = SymbolicDiscreteCallback(1.0 => [c ~ t], discrete_parameters = [c]) @mtkbuild sys = ODESystem(D(x) ~ cos(t), t, [x], [a, b, c]; continuous_events = [cb1, cb2], discrete_events = [cb3]) @@ -1083,6 +892,7 @@ end @test sol[b] == [2.0, 5.0, 5.0] @test sol[c] == [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0] end + @testset "Heater" begin @variables temp(t) params = @parameters furnace_on_threshold=0.5 furnace_off_threshold=0.7 furnace_power=1.0 leakage=0.1 furnace_on::Bool=false @@ -1257,7 +1067,7 @@ end f = ModelingToolkit.FunctionalAffect( f = (i, u, p, c) -> seen = true, sts = [], pars = [], discretes = []) cb1 = ModelingToolkit.SymbolicContinuousCallback( - [x ~ 0], Equation[], initialize = [x ~ 1.5], finalize = f) + [x ~ 0], nothing, initialize = [x ~ 1.5], finalize = f) @mtkbuild sys = ODESystem(D(x) ~ -1, t, [x], []; continuous_events = [cb1]) prob = ODEProblem(sys, [x => 1.0], (0.0, 2), []) sol = solve(prob, Tsit5(); dtmax = 0.01) @@ -1270,7 +1080,7 @@ end f = ModelingToolkit.FunctionalAffect( f = (i, u, p, c) -> seen = true, sts = [], pars = [], discretes = []) cb1 = ModelingToolkit.SymbolicContinuousCallback( - [x ~ 0], Equation[], initialize = [x ~ 1.5], finalize = f) + [x ~ 0], nothing, initialize = [x ~ 1.5], finalize = f) inited = false finaled = false a = ModelingToolkit.FunctionalAffect( @@ -1278,7 +1088,7 @@ end b = ModelingToolkit.FunctionalAffect( f = (i, u, p, c) -> finaled = true, sts = [], pars = [], discretes = []) cb2 = ModelingToolkit.SymbolicContinuousCallback( - [x ~ 0.1], Equation[], initialize = a, finalize = b) + [x ~ 0.1], nothing, initialize = a, finalize = b) @mtkbuild sys = ODESystem(D(x) ~ -1, t, [x], []; continuous_events = [cb1, cb2]) prob = ODEProblem(sys, [x => 1.0], (0.0, 2), []) sol = solve(prob, Tsit5()) @@ -1344,7 +1154,7 @@ end cb = [x ~ 0.0] => [x ~ 0, y ~ 1] @mtkbuild pend = ODESystem(eqs, t; continuous_events = [cb]) prob = ODEProblem(pend, [x => 1], (0.0, 3.0), guesses = [y => x]) - @test_throws "DAE initialization failed" solve(prob, Rodas5()) + @test_broken !SciMLBase.successful_retcode(solve(prob, Rodas5())) cb = [x ~ 0.0] => [y ~ 1] @mtkbuild pend = ODESystem(eqs, t; continuous_events = [cb]) @@ -1361,7 +1171,7 @@ end @mtkmodel DECAY begin @parameters begin unrelated[1:2] = zeros(2) - k = 0.0 + k(t) = 0.0 end @variables begin x(t) = 10.0 @@ -1370,7 +1180,7 @@ end D(x) ~ -k * x end @discrete_events begin - (t == 1.0) => [k ~ 1.0] + (t == 1.0) => [k ~ 1.0], [discrete_parameters = k] end end @mtkbuild decay = DECAY() @@ -1378,7 +1188,7 @@ end @test_nowarn solve(prob, Tsit5(), tstops = [1.0]) end -@testset "Array parameter updates in ImperativeEffect" begin +@testset "Array parameter updates in ImperativeAffect" begin function weird1(max_time; name) params = @parameters begin θ(t) = 0.0 @@ -1435,6 +1245,67 @@ end @test 100.0 ∈ sol2[sys2.wd2.θ] end +@testset "Implicit affects with Pre" begin + using ModelingToolkit: UnsolvableCallbackError + @parameters g + @variables x(t) y(t) λ(t) + eqs = [D(D(x)) ~ λ * x + D(D(y)) ~ λ * y - g + x^2 + y^2 ~ 1] + c_evt = [t ~ 5.0] => [x ~ Pre(x) + 0.1] + @mtkbuild pend = ODESystem(eqs, t, continuous_events = c_evt) + prob = ODEProblem(pend, [x => -1, y => 0], (0.0, 10.0), [g => 1], guesses = [λ => 1]) + sol = solve(prob, FBDF()) + @test ≈(sol(5.000001, idxs = x) - sol(4.999999, idxs = x), 0.1, rtol = 1e-4) + @test ≈(sol(5.000001, idxs = x)^2 + sol(5.000001, idxs = y)^2, 1, rtol = 1e-4) + + # Implicit affect with Pre + c_evt = [t ~ 5.0] => [x ~ Pre(x) + y^2] + @mtkbuild pend = ODESystem(eqs, t, continuous_events = c_evt) + prob = ODEProblem(pend, [x => 1, y => 0], (0.0, 10.0), [g => 1], guesses = [λ => 1]) + sol = solve(prob, FBDF()) + @test ≈(sol(5.000001, idxs = y)^2 + sol(4.999999, idxs = x), + sol(5.000001, idxs = x), rtol = 1e-4) + @test ≈(sol(5.000001, idxs = x)^2 + sol(5.000001, idxs = y)^2, 1, rtol = 1e-4) + + # Impossible affect errors + c_evt = [t ~ 5.0] => [x ~ Pre(x) + 2] + @mtkbuild pend = ODESystem(eqs, t, continuous_events = c_evt) + prob = ODEProblem(pend, [x => 1, y => 0], (0.0, 10.0), [g => 1], guesses = [λ => 1]) + @test_throws UnsolvableCallbackError sol=solve(prob, FBDF()) + + # Changing both variables and parameters in the same affect. + @parameters g(t) + eqs = [D(D(x)) ~ λ * x + D(D(y)) ~ λ * y - g + x^2 + y^2 ~ 1] + c_evt = SymbolicContinuousCallback( + [t ~ 5.0], [x ~ Pre(x) + 0.1, g ~ Pre(g) + 1], discrete_parameters = [g], iv = t) + @mtkbuild pend = ODESystem(eqs, t, continuous_events = c_evt) + prob = ODEProblem(pend, [x => 1, y => 0], (0.0, 10.0), [g => 1], guesses = [λ => 1]) + sol = solve(prob, FBDF()) + @test sol.ps[g] ≈ [1, 2] + @test ≈(sol(5.0000001, idxs = x) - sol(4.999999, idxs = x), 0.1, rtol = 1e-4) + + # Proper re-initialization after parameter change + eqs = [y ~ g^2, D(x) ~ x] + c_evt = SymbolicContinuousCallback( + [t ~ 5.0], [x ~ Pre(x) + 1, g ~ Pre(g) + 1], discrete_parameters = [g], iv = t) + @mtkbuild sys = ODESystem(eqs, t, continuous_events = c_evt) + prob = ODEProblem(sys, [x => 1.0], (0.0, 10.0), [g => 2]) + sol = solve(prob, FBDF()) + @test sol.ps[g] ≈ [2.0, 3.0] + @test ≈(sol(5.00000001, idxs = x) - sol(4.9999999, idxs = x), 1; rtol = 1e-4) + @test ≈(sol(5.00000001, idxs = y), 9, rtol = 1e-4) + + # Parameters that don't appear in affects should not be mutated. + c_evt = [t ~ 5.0] => [x ~ Pre(x) + 1] + @mtkbuild sys = ODESystem(eqs, t, continuous_events = c_evt) + prob = ODEProblem(sys, [x => 0.5], (0.0, 10.0), [g => 2], guesses = [y => 0]) + sol = solve(prob, FBDF()) + @test prob.ps[g] == sol.ps[g] +end + @testset "Array parameter updates of parent components in ImperativeEffect" begin function child(vals; name, max_time = 0.1) vars = @variables begin diff --git a/test/symbolic_indexing_interface.jl b/test/symbolic_indexing_interface.jl index 8b3da5fd72..613bfc8213 100644 --- a/test/symbolic_indexing_interface.jl +++ b/test/symbolic_indexing_interface.jl @@ -1,5 +1,6 @@ using ModelingToolkit, SymbolicIndexingInterface, SciMLBase -using ModelingToolkit: t_nounits as t, D_nounits as D, ParameterIndex +using ModelingToolkit: t_nounits as t, D_nounits as D, ParameterIndex, + SymbolicContinuousCallback using SciMLStructures: Tunable @testset "ODESystem" begin @@ -230,7 +231,8 @@ end @testset "`timeseries_parameter_index` on unwrapped scalarized timeseries parameter" begin @variables x(t)[1:2] @parameters p(t)[1:2, 1:2] - ev = [x[1] ~ 2.0] => [p ~ -ones(2, 2)] + ev = SymbolicContinuousCallback( + [x[1] ~ 2.0] => [p ~ -ones(2, 2)], discrete_parameters = [p]) @mtkbuild sys = ODESystem(D(x) ~ p * x, t; continuous_events = [ev]) p = ModelingToolkit.unwrap(p) @test timeseries_parameter_index(sys, p) === ParameterTimeseriesIndex(1, (1, 1)) diff --git a/test/symbolic_parameters.jl b/test/symbolic_parameters.jl index a29090912c..f4fa21e614 100644 --- a/test/symbolic_parameters.jl +++ b/test/symbolic_parameters.jl @@ -28,7 +28,7 @@ resolved = ModelingToolkit.varmap_to_vars(Dict(), parameters(ns), prob = NonlinearProblem(complete(ns), [u => 1.0], Pair[]) @test prob.u0 == [1.0, 1.1, 0.9] -@show sol = solve(prob, NewtonRaphson()) +sol = solve(prob, NewtonRaphson()) @variables a @parameters b @@ -43,12 +43,12 @@ res = ModelingToolkit.varmap_to_vars(Dict(), parameters(top), top = complete(top) prob = NonlinearProblem(top, [unknowns(ns, u) => 1.0, a => 1.0], []) @test prob.u0 == [1.0, 0.5, 1.1, 0.9] -@show sol = solve(prob, NewtonRaphson()) +sol = solve(prob, NewtonRaphson()) # test NullParameters+defaults prob = NonlinearProblem(top, [unknowns(ns, u) => 1.0, a => 1.0]) @test prob.u0 == [1.0, 0.5, 1.1, 0.9] -@show sol = solve(prob, NewtonRaphson()) +sol = solve(prob, NewtonRaphson()) # test initial conditions and parameters at the problem level pars = @parameters(begin From 1c1907163190cc243dcb24d58ceb5529644bbf1e Mon Sep 17 00:00:00 2001 From: vyudu Date: Wed, 5 Mar 2025 15:22:28 -0500 Subject: [PATCH 1601/2176] refactor: update symbolic event parsing in system constructors --- src/systems/diffeqs/odesystem.jl | 4 ++-- src/systems/diffeqs/sdesystem.jl | 6 ++++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/systems/diffeqs/odesystem.jl b/src/systems/diffeqs/odesystem.jl index 18208cb3af..e061706416 100644 --- a/src/systems/diffeqs/odesystem.jl +++ b/src/systems/diffeqs/odesystem.jl @@ -335,9 +335,9 @@ function ODESystem(deqs::AbstractVector{<:Equation}, iv, dvs, ps; if length(unique(sysnames)) != length(sysnames) throw(ArgumentError("System names must be unique.")) end - cont_callbacks = SymbolicContinuousCallbacks(continuous_events) - disc_callbacks = SymbolicDiscreteCallbacks(discrete_events) + cont_callbacks, disc_callbacks = create_symbolic_events( + continuous_events, discrete_events, deqs, iv) if is_dde === nothing is_dde = _check_if_dde(deqs, iv′, systems) end diff --git a/src/systems/diffeqs/sdesystem.jl b/src/systems/diffeqs/sdesystem.jl index d743143e46..016154e1cc 100644 --- a/src/systems/diffeqs/sdesystem.jl +++ b/src/systems/diffeqs/sdesystem.jl @@ -269,8 +269,10 @@ function SDESystem(deqs::AbstractVector{<:Equation}, neqs::AbstractArray, iv, dv ctrl_jac = RefValue{Any}(EMPTY_JAC) Wfact = RefValue(EMPTY_JAC) Wfact_t = RefValue(EMPTY_JAC) - cont_callbacks = SymbolicContinuousCallbacks(continuous_events) - disc_callbacks = SymbolicDiscreteCallbacks(discrete_events) + + cont_callbacks, disc_callbacks = create_symbolic_events( + continuous_events, discrete_events, deqs, iv) + if is_dde === nothing is_dde = _check_if_dde(deqs, iv′, systems) end From f96227ed9ad2806a79cff175d7721c75305807ea Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 9 May 2025 12:44:23 +0530 Subject: [PATCH 1602/2176] feat: implement `==` for `DiscreteSystem` Co-authored-by: vyudu --- src/systems/discrete_system/discrete_system.jl | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/systems/discrete_system/discrete_system.jl b/src/systems/discrete_system/discrete_system.jl index 64c0c2c8e0..8b392e6145 100644 --- a/src/systems/discrete_system/discrete_system.jl +++ b/src/systems/discrete_system/discrete_system.jl @@ -429,4 +429,14 @@ function DiscreteFunctionExpr(sys::DiscreteSystem, args...; kwargs...) DiscreteFunctionExpr{true}(sys, args...; kwargs...) end +function Base.:(==)(sys1::DiscreteSystem, sys2::DiscreteSystem) + sys1 === sys2 && return true + isequal(nameof(sys1), nameof(sys2)) && + isequal(get_iv(sys1), get_iv(sys2)) && + _eq_unordered(get_eqs(sys1), get_eqs(sys2)) && + _eq_unordered(get_unknowns(sys1), get_unknowns(sys2)) && + _eq_unordered(get_ps(sys1), get_ps(sys2)) && + all(s1 == s2 for (s1, s2) in zip(get_systems(sys1), get_systems(sys2))) +end + supports_initialization(::DiscreteSystem) = false From 9de14e8591f92f8b8f14cad0ab78eb3e033557c2 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 9 May 2025 12:44:36 +0530 Subject: [PATCH 1603/2176] feat: implement `==` for `ImplicitDiscreteSystem` Co-authored-by: vyudu --- .../discrete_system/implicit_discrete_system.jl | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/systems/discrete_system/implicit_discrete_system.jl b/src/systems/discrete_system/implicit_discrete_system.jl index 3956c089d4..71f2112a61 100644 --- a/src/systems/discrete_system/implicit_discrete_system.jl +++ b/src/systems/discrete_system/implicit_discrete_system.jl @@ -441,3 +441,13 @@ end function ImplicitDiscreteFunctionExpr(sys::ImplicitDiscreteSystem, args...; kwargs...) ImplicitDiscreteFunctionExpr{true}(sys, args...; kwargs...) end + +function Base.:(==)(sys1::ImplicitDiscreteSystem, sys2::ImplicitDiscreteSystem) + sys1 === sys2 && return true + isequal(nameof(sys1), nameof(sys2)) && + isequal(get_iv(sys1), get_iv(sys2)) && + _eq_unordered(get_eqs(sys1), get_eqs(sys2)) && + _eq_unordered(get_unknowns(sys1), get_unknowns(sys2)) && + _eq_unordered(get_ps(sys1), get_ps(sys2)) && + all(s1 == s2 for (s1, s2) in zip(get_systems(sys1), get_systems(sys2))) +end From 540f3fbc3faa18d1c889e783e4712c960ebb1416 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 9 May 2025 12:45:18 +0530 Subject: [PATCH 1604/2176] feat: allow passing `cachesyms` to `ImplicitDiscreteFunction` codegen Co-authored-by: vyudu --- src/systems/discrete_system/implicit_discrete_system.jl | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/systems/discrete_system/implicit_discrete_system.jl b/src/systems/discrete_system/implicit_discrete_system.jl index 71f2112a61..401b03f571 100644 --- a/src/systems/discrete_system/implicit_discrete_system.jl +++ b/src/systems/discrete_system/implicit_discrete_system.jl @@ -270,7 +270,8 @@ function flatten(sys::ImplicitDiscreteSystem, noeqs = false) end function generate_function( - sys::ImplicitDiscreteSystem, dvs = unknowns(sys), ps = parameters(sys); wrap_code = identity, kwargs...) + sys::ImplicitDiscreteSystem, dvs = unknowns(sys), ps = parameters(sys); + wrap_code = identity, cachesyms::Tuple = (), kwargs...) iv = get_iv(sys) # Algebraic equations get shifted forward 1, to match with differential equations exprs = map(equations(sys)) do eq @@ -286,8 +287,9 @@ function generate_function( u_next = map(Shift(iv, 1), dvs) u = dvs + p = (reorder_parameters(sys, unwrap.(ps))..., cachesyms...) build_function_wrapper( - sys, exprs, u_next, u, ps..., iv; p_start = 3, extra_assignments, kwargs...) + sys, exprs, u_next, u, p..., iv; p_start = 3, extra_assignments, kwargs...) end function shift_u0map_forward(sys::ImplicitDiscreteSystem, u0map, defs) From 57716f025e4abea028841005026e02079d0d691b Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 9 May 2025 12:45:46 +0530 Subject: [PATCH 1605/2176] fix: update error checking for `ImplicitDiscreteProblem` initial conditions Co-authored-by: vyudu --- src/systems/discrete_system/implicit_discrete_system.jl | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/systems/discrete_system/implicit_discrete_system.jl b/src/systems/discrete_system/implicit_discrete_system.jl index 401b03f571..8fc5f7127f 100644 --- a/src/systems/discrete_system/implicit_discrete_system.jl +++ b/src/systems/discrete_system/implicit_discrete_system.jl @@ -298,12 +298,9 @@ function shift_u0map_forward(sys::ImplicitDiscreteSystem, u0map, defs) for k in collect(keys(u0map)) v = u0map[k] if !((op = operation(k)) isa Shift) - isnothing(getunshifted(k)) && - error("Initial conditions must be for the past state of the unknowns. Instead of providing the condition for $k, provide the condition for $(Shift(iv, -1)(k)).") - updated[k] = v elseif op.steps > 0 - error("Initial conditions must be for the past state of the unknowns. Instead of providing the condition for $k, provide the condition for $(Shift(iv, -1)(only(arguments(k)))).") + error("Initial conditions must be for the current or past state of the unknowns. Instead of providing the condition for $k, provide the condition for $(Shift(iv, -1)(only(arguments(k)))).") else updated[k] = v end From 104450f4c2e30e6e7484a33e58bcac842280d733 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 9 May 2025 12:46:00 +0530 Subject: [PATCH 1606/2176] feat: add `resid_prototype` for `ImplicitDiscreteFunction` Co-authored-by: vyudu --- src/systems/discrete_system/implicit_discrete_system.jl | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/systems/discrete_system/implicit_discrete_system.jl b/src/systems/discrete_system/implicit_discrete_system.jl index 8fc5f7127f..b818ffcc8d 100644 --- a/src/systems/discrete_system/implicit_discrete_system.jl +++ b/src/systems/discrete_system/implicit_discrete_system.jl @@ -379,6 +379,12 @@ function SciMLBase.ImplicitDiscreteFunction{iip, specialize}( f(u_next, u, p, t) = f_oop(u_next, u, p, t) f(resid, u_next, u, p, t) = f_iip(resid, u_next, u, p, t) + if length(dvs) == length(equations(sys)) + resid_prototype = nothing + else + resid_prototype = calculate_resid_prototype(length(equations(sys)), u0, p) + end + if specialize === SciMLBase.FunctionWrapperSpecialize && iip if u0 === nothing || p === nothing || t === nothing error("u0, p, and t must be specified for FunctionWrapperSpecialize on ImplicitDiscreteFunction.") @@ -393,6 +399,7 @@ function SciMLBase.ImplicitDiscreteFunction{iip, specialize}( sys = sys, observed = observedfun, analytic = analytic, + resid_prototype = resid_prototype, kwargs...) end From aa2b5dfafdb4ec14c208a3656886dc49931b7d0d Mon Sep 17 00:00:00 2001 From: vyudu Date: Tue, 11 Mar 2025 18:19:58 -0400 Subject: [PATCH 1607/2176] fix: fix discrete variable detection in `IndexCache` --- src/systems/index_cache.jl | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/systems/index_cache.jl b/src/systems/index_cache.jl index d0b687c212..948af4dfa5 100644 --- a/src/systems/index_cache.jl +++ b/src/systems/index_cache.jl @@ -117,10 +117,11 @@ function IndexCache(sys::AbstractSystem) affs = [affs] end for affect in affs - if affect isa Equation - is_parameter(sys, affect.lhs) && push!(discs, affect.lhs) - elseif affect isa FunctionalAffect || affect isa ImperativeAffect + if affect isa AffectSystem || affect isa FunctionalAffect || + affect isa ImperativeAffect union!(discs, unwrap.(discretes(affect))) + elseif isnothing(affect) + continue else error("Unhandled affect type $(typeof(affect))") end From d4e4d4d3efdd68cfded1b642eff1ffa2f05a2410 Mon Sep 17 00:00:00 2001 From: vyudu Date: Wed, 12 Mar 2025 11:37:33 -0400 Subject: [PATCH 1608/2176] fix: update callback and jump codegen in JumpProblem --- src/systems/jumps/jumpsystem.jl | 41 +++++++++------------------------ 1 file changed, 11 insertions(+), 30 deletions(-) diff --git a/src/systems/jumps/jumpsystem.jl b/src/systems/jumps/jumpsystem.jl index 34b6df3bf5..348c103511 100644 --- a/src/systems/jumps/jumpsystem.jl +++ b/src/systems/jumps/jumpsystem.jl @@ -1,19 +1,5 @@ const JumpType = Union{VariableRateJump, ConstantRateJump, MassActionJump} -# modifies the expression representing an affect function to -# call reset_aggregated_jumps!(integrator). -# assumes iip -function _reset_aggregator!(expr, integrator) - @assert Meta.isexpr(expr, :function) - body = expr.args[end] - body = quote - $body - $reset_aggregated_jumps!($integrator) - end - expr.args[end] = body - return nothing -end - """ $(TYPEDEF) @@ -230,8 +216,10 @@ function JumpSystem(eqs, iv, unknowns, ps; end end - cont_callbacks = SymbolicContinuousCallbacks(continuous_events) - disc_callbacks = SymbolicDiscreteCallbacks(discrete_events) + cont_callbacks = to_cb_vector(continuous_events; CB_TYPE = SymbolicContinuousCallback, + iv = iv, warn_no_algebraic = false) + disc_callbacks = to_cb_vector(discrete_events; CB_TYPE = SymbolicDiscreteCallback, + iv = iv, warn_no_algebraic = false) JumpSystem{typeof(ap)}(Threads.atomic_add!(SYSTEM_COUNT, UInt(1)), ap, iv′, us′, ps′, var_to_name, observed, name, description, systems, @@ -282,15 +270,13 @@ function generate_rate_function(js::JumpSystem, rate) expression = Val{true}) end -function generate_affect_function(js::JumpSystem, affect, outputidxs) +function generate_affect_function(js::JumpSystem, affect) consts = collect_constants(affect) if !isempty(consts) # The SymbolicUtils._build_function method of this case doesn't support postprocess_fbody csubs = Dict(c => getdefault(c) for c in consts) affect = substitute(affect, csubs) end - compile_affect( - affect, nothing, js, unknowns(js), parameters(js); outputidxs = outputidxs, - expression = Val{true}, checkvars = false) + compile_equational_affect(affect, js; expression = Val{true}, checkvars = false) end function assemble_vrj( @@ -299,8 +285,7 @@ function assemble_vrj( rate = GeneratedFunctionWrapper{(2, 3, is_split(js))}(rate, nothing) outputvars = (value(affect.lhs) for affect in vrj.affect!) outputidxs = [unknowntoid[var] for var in outputvars] - affect = eval_or_rgf(generate_affect_function(js, vrj.affect!, outputidxs); - eval_expression, eval_module) + affect = generate_affect_function(js, vrj.affect!) VariableRateJump(rate, affect; save_positions = vrj.save_positions) end @@ -308,10 +293,9 @@ function assemble_vrj_expr(js, vrj, unknowntoid) rate = generate_rate_function(js, vrj.rate) outputvars = (value(affect.lhs) for affect in vrj.affect!) outputidxs = ((unknowntoid[var] for var in outputvars)...,) - affect = generate_affect_function(js, vrj.affect!, outputidxs) + affect = generate_affect_function(js, vrj.affect!) quote rate = $rate - affect = $affect VariableRateJump(rate, affect) end @@ -323,8 +307,7 @@ function assemble_crj( rate = GeneratedFunctionWrapper{(2, 3, is_split(js))}(rate, nothing) outputvars = (value(affect.lhs) for affect in crj.affect!) outputidxs = [unknowntoid[var] for var in outputvars] - affect = eval_or_rgf(generate_affect_function(js, crj.affect!, outputidxs); - eval_expression, eval_module) + affect = generate_affect_function(js, crj.affect!) ConstantRateJump(rate, affect) end @@ -332,10 +315,9 @@ function assemble_crj_expr(js, crj, unknowntoid) rate = generate_rate_function(js, crj.rate) outputvars = (value(affect.lhs) for affect in crj.affect!) outputidxs = ((unknowntoid[var] for var in outputvars)...,) - affect = generate_affect_function(js, crj.affect!, outputidxs) + affect = generate_affect_function(js, crj.affect!) quote rate = $rate - affect = $affect ConstantRateJump(rate, affect) end @@ -574,8 +556,7 @@ function JumpProcesses.JumpProblem(js::JumpSystem, prob, end # handle events, making sure to reset aggregators in the generated affect functions - cbs = process_events(js; callback, eval_expression, eval_module, - postprocess_affect_expr! = _reset_aggregator!) + cbs = process_events(js; callback, eval_expression, eval_module, reset_jumps = true) JumpProblem(prob, aggregator, jset; dep_graph = jtoj, vartojumps_map = vtoj, jumptovars_map = jtov, scale_rates = false, nocopy = true, From 07f16a3d1f52ee6d2f102bd824b80682a069666a Mon Sep 17 00:00:00 2001 From: vyudu Date: Mon, 24 Mar 2025 15:46:15 -0400 Subject: [PATCH 1609/2176] refactor: update `@mtkmodel` to account for new callback syntax --- src/systems/model_parsing.jl | 49 ++++++++++++++++++++++++------------ 1 file changed, 33 insertions(+), 16 deletions(-) diff --git a/src/systems/model_parsing.jl b/src/systems/model_parsing.jl index 024c249363..2a852813a2 100644 --- a/src/systems/model_parsing.jl +++ b/src/systems/model_parsing.jl @@ -129,8 +129,9 @@ function _model_macro(mod, fullname::Union{Expr, Symbol}, expr, isconnector) Ref(dict), [:constants, :defaults, :kwargs, :structural_parameters]) sys = :($type($(flatten_equations)(equations), $iv, variables, parameters; - name, description = $description, systems, gui_metadata = $gui_metadata, defaults, - costs = [$(costs...)], constraints = [$(cons...)], consolidate = $consolidate)) + name, description = $description, systems, gui_metadata = $gui_metadata, + continuous_events = [$(c_evts...)], discrete_events = [$(d_evts...)], + defaults, costs = [$(costs...)], constraints = [$(cons...)], consolidate = $consolidate)) if length(ext) == 0 push!(exprs.args, :(var"#___sys___" = $sys)) @@ -141,16 +142,6 @@ function _model_macro(mod, fullname::Union{Expr, Symbol}, expr, isconnector) isconnector && push!(exprs.args, :($Setfield.@set!(var"#___sys___".connector_type=$connector_type(var"#___sys___")))) - !isempty(c_evts) && push!(exprs.args, - :($Setfield.@set!(var"#___sys___".continuous_events=$SymbolicContinuousCallback.([ - $(c_evts...) - ])))) - - !isempty(d_evts) && push!(exprs.args, - :($Setfield.@set!(var"#___sys___".discrete_events=$SymbolicDiscreteCallback.([ - $(d_evts...) - ])))) - f = if length(where_types) == 0 :($(Symbol(:__, name, :__))(; name, $(kwargs...)) = $exprs) else @@ -1144,8 +1135,16 @@ end function parse_continuous_events!(c_evts, dict, body) dict[:continuous_events] = [] Base.remove_linenums!(body) - for arg in body.args - push!(c_evts, arg) + for line in body.args + if length(line.args) == 3 && line.args[1] == :(=>) + push!(c_evts, :(($line, ()))) + elseif length(line.args) == 2 + event = line.args[1] + kwargs = parse_event_kwargs(line.args[2]) + push!(c_evts, :(($event, $kwargs))) + else + error("Malformed continuous event $line.") + end push!(dict[:continuous_events], readable_code.(c_evts)...) end end @@ -1153,12 +1152,30 @@ end function parse_discrete_events!(d_evts, dict, body) dict[:discrete_events] = [] Base.remove_linenums!(body) - for arg in body.args - push!(d_evts, arg) + for line in body.args + if length(line.args) == 3 && line.args[1] == :(=>) + push!(d_evts, :(($line, ()))) + elseif length(line.args) == 2 + event = line.args[1] + kwargs = parse_event_kwargs(line.args[2]) + push!(d_evts, :(($event, $kwargs))) + else + error("Malformed discrete event $line.") + end push!(dict[:discrete_events], readable_code.(d_evts)...) end end +function parse_event_kwargs(disc_expr) + kwargs = :([]) + for arg in disc_expr.args + (arg.head != :(=)) && error("Malformed event kwarg $arg.") + (arg.args[1] isa Symbol) || error("Invalid keyword argument name $(arg.args[1]).") + push!(kwargs.args, :($(QuoteNode(arg.args[1])) => $(arg.args[2]))) + end + kwargs +end + function parse_constraints!(cons, dict, body) dict[:constraints] = [] Base.remove_linenums!(body) From d12abee6f7d0acf3ae4d948f71b9729e151ca5da Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 9 May 2025 13:02:57 +0530 Subject: [PATCH 1610/2176] fix: do not error when simplifying implicit `DiscreteSystem`s Co-authored-by: vyudu --- src/systems/systems.jl | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/systems/systems.jl b/src/systems/systems.jl index e417337a95..cb8bbde15c 100644 --- a/src/systems/systems.jl +++ b/src/systems/systems.jl @@ -43,10 +43,6 @@ function structural_simplify( end if newsys isa DiscreteSystem && any(eq -> symbolic_type(eq.lhs) == NotSymbolic(), equations(newsys)) - error(""" - Encountered algebraic equations when simplifying discrete system. Please construct \ - an ImplicitDiscreteSystem instead. - """) end for pass in additional_passes newsys = pass(newsys) From 512029662d4aa88f6f857f3e9ae3ba5260bf92f2 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 9 May 2025 13:03:10 +0530 Subject: [PATCH 1611/2176] fix: propagate events when simplifying `SDESystem` Co-authored-by: vyudu --- src/systems/systems.jl | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/systems/systems.jl b/src/systems/systems.jl index cb8bbde15c..4e4a7f7ba4 100644 --- a/src/systems/systems.jl +++ b/src/systems/systems.jl @@ -157,9 +157,9 @@ function __structural_simplify(sys::AbstractSystem; simplify = false, get_iv(ode_sys), unknowns(ode_sys), parameters(ode_sys); name = nameof(ode_sys), is_scalar_noise, observed = observed(ode_sys), defaults = defaults(sys), parameter_dependencies = parameter_dependencies(sys), assertions = assertions(sys), - guesses = guesses(sys), initialization_eqs = initialization_equations(sys)) - @set! ssys.tearing_state = get_tearing_state(ode_sys) - return ssys + guesses = guesses(sys), initialization_eqs = initialization_equations(sys), + continuous_events = continuous_events(sys), + discrete_events = discrete_events(sys)) end end From 1e9c8f2209b7d9eddd36bb191ca05d429d00326c Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 9 May 2025 15:04:36 +0530 Subject: [PATCH 1612/2176] fix: unwrap `discrete_parameters` in `make_affect` --- src/systems/callbacks.jl | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/systems/callbacks.jl b/src/systems/callbacks.jl index 8a5c6131f3..3ed6fed268 100644 --- a/src/systems/callbacks.jl +++ b/src/systems/callbacks.jl @@ -273,6 +273,8 @@ function make_affect(affect::Vector{Equation}; discrete_parameters = Any[], end discrete_parameters isa AbstractVector || (discrete_parameters = [discrete_parameters]) + discrete_parameters = unwrap.(discrete_parameters) + for p in discrete_parameters occursin(unwrap(iv), unwrap(p)) || error("Non-time dependent parameter $p passed in as a discrete. Must be declared as @parameters $p(t).") From 43bc65949aa841fd8354b9c43d9bc57108abb03c Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 9 May 2025 15:05:36 +0530 Subject: [PATCH 1613/2176] fix: implement `tovar(::Arr)` --- src/parameters.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/parameters.jl b/src/parameters.jl index 91121b7cbb..de7a722af3 100644 --- a/src/parameters.jl +++ b/src/parameters.jl @@ -63,7 +63,7 @@ toparam(s::Num) = wrap(toparam(value(s))) Maps the variable to an unknown. """ tovar(s::Symbolic) = setmetadata(s, MTKVariableTypeCtx, VARIABLE) -tovar(s::Num) = Num(tovar(value(s))) +tovar(s::Union{Num, Symbolics.Arr}) = wrap(tovar(unwrap(s))) """ $(SIGNATURES) From 805b23dac73dd3eb9a58b443a5c2f64d7a6a1ca1 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 12 May 2025 12:23:56 +0530 Subject: [PATCH 1614/2176] fix: fix propagation of `reinitializealg` in `SymbolicDiscreteCallback` --- src/systems/callbacks.jl | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/systems/callbacks.jl b/src/systems/callbacks.jl index 3ed6fed268..4e24f1b765 100644 --- a/src/systems/callbacks.jl +++ b/src/systems/callbacks.jl @@ -438,9 +438,12 @@ struct SymbolicDiscreteCallback <: AbstractCallback c = is_timed_condition(condition) ? condition : value(scalarize(condition)) if isnothing(reinitializealg) - reinitializealg = SciMLBase.CheckInit() - else - reinitializealg = SciMLBase.NoInit() + if any(a -> (a isa FunctionalAffect || a isa ImperativeAffect), + [affect, initialize, finalize]) + reinitializealg = SciMLBase.CheckInit() + else + reinitializealg = SciMLBase.NoInit() + end end new(c, make_affect(affect; kwargs...), make_affect(initialize; kwargs...), From dff62b0fe73fc34ebc2ed9ae5059f5bd6ee6a42a Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 13 May 2025 00:30:06 +0530 Subject: [PATCH 1615/2176] fix: FMI ME state management callback shouldn't ever trigger or reinitialize --- ext/MTKFMIExt.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ext/MTKFMIExt.jl b/ext/MTKFMIExt.jl index 32023d0749..d155544ce9 100644 --- a/ext/MTKFMIExt.jl +++ b/ext/MTKFMIExt.jl @@ -238,7 +238,7 @@ function MTK.FMIComponent(::Val{Ver}; fmu = nothing, tolerance = 1e-6, finalize_affect = MTK.FunctionalAffect(fmiFinalize!, [], [wrapper], []) step_affect = MTK.FunctionalAffect(Returns(nothing), [], [], []) instance_management_callback = MTK.SymbolicDiscreteCallback( - (t != t - 1), step_affect; finalize = finalize_affect, reinitializealg) + (t == t - 1), step_affect; finalize = finalize_affect, reinitializealg = SciMLBase.NoInit()) push!(params, wrapper) append!(observed, der_observed) From 48be180d66e652e4f191710d7e414bf8576f7150 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 14 Apr 2025 23:13:05 +0530 Subject: [PATCH 1616/2176] refactor: remove old `System` function --- src/systems/systems.jl | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/systems/systems.jl b/src/systems/systems.jl index 4e4a7f7ba4..f606b6b63b 100644 --- a/src/systems/systems.jl +++ b/src/systems/systems.jl @@ -1,8 +1,3 @@ -function System(eqs::AbstractVector{<:Equation}, iv, args...; name = nothing, - kw...) - ODESystem(eqs, iv, args...; name, kw..., checks = false) -end - const REPEATED_SIMPLIFICATION_MESSAGE = "Structural simplification cannot be applied to a completed system. Double simplification is not allowed." struct RepeatedStructuralSimplificationError <: Exception end From 509643c2e04bfa9d4aa02993cbbf4ae8cf66df49 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 14 May 2025 12:11:39 +0530 Subject: [PATCH 1617/2176] refactor: remove `odesystem.jl` --- src/ModelingToolkit.jl | 8 +- src/systems/diffeqs/odesystem.jl | 860 ------------------------------- 2 files changed, 1 insertion(+), 867 deletions(-) delete mode 100644 src/systems/diffeqs/odesystem.jl diff --git a/src/ModelingToolkit.jl b/src/ModelingToolkit.jl index b8bbebfbbb..d959c6fabc 100644 --- a/src/ModelingToolkit.jl +++ b/src/ModelingToolkit.jl @@ -170,11 +170,6 @@ include("systems/optimization/optimizationsystem.jl") include("systems/optimization/modelingtoolkitize.jl") include("systems/nonlinear/nonlinearsystem.jl") -include("systems/discrete_system/discrete_system.jl") -include("systems/discrete_system/implicit_discrete_system.jl") -include("systems/callbacks.jl") - -include("systems/diffeqs/odesystem.jl") include("systems/diffeqs/sdesystem.jl") include("systems/diffeqs/abstractodesystem.jl") include("systems/nonlinear/homotopy_continuation.jl") @@ -267,8 +262,7 @@ export AbstractTimeDependentSystem, AbstractTimeIndependentSystem, AbstractMultivariateSystem -export ODESystem, - ODEFunction, ODEFunctionExpr, ODEProblemExpr, convert_system, +export ODEFunction, ODEFunctionExpr, ODEProblemExpr, convert_system, add_accumulations, System export DAEFunctionExpr, DAEProblemExpr export SDESystem, SDEFunction, SDEFunctionExpr, SDEProblemExpr diff --git a/src/systems/diffeqs/odesystem.jl b/src/systems/diffeqs/odesystem.jl deleted file mode 100644 index e061706416..0000000000 --- a/src/systems/diffeqs/odesystem.jl +++ /dev/null @@ -1,860 +0,0 @@ -""" -$(TYPEDEF) - -A system of ordinary differential equations. - -# Fields -$(FIELDS) - -# Example - -```julia -using ModelingToolkit -using ModelingToolkit: t_nounits as t, D_nounits as D - -@parameters σ ρ β -@variables x(t) y(t) z(t) - -eqs = [D(x) ~ σ*(y-x), - D(y) ~ x*(ρ-z)-y, - D(z) ~ x*y - β*z] - -@named de = ODESystem(eqs,t,[x,y,z],[σ,ρ,β],tspan=(0, 1000.0)) -``` -""" -struct ODESystem <: AbstractODESystem - """ - A tag for the system. If two systems have the same tag, then they are - structurally identical. - """ - tag::UInt - """The ODEs defining the system.""" - eqs::Vector{Equation} - """Independent variable.""" - iv::BasicSymbolic{Real} - """ - Dependent (unknown) variables. Must not contain the independent variable. - - N.B.: If `torn_matching !== nothing`, this includes all variables. Actual - ODE unknowns are determined by the `SelectedState()` entries in `torn_matching`. - """ - unknowns::Vector - """Parameter variables. Must not contain the independent variable.""" - ps::Vector - """Time span.""" - tspan::Union{NTuple{2, Any}, Nothing} - """Array variables.""" - var_to_name::Any - """Control parameters (some subset of `ps`).""" - ctrls::Vector - """Observed equations.""" - observed::Vector{Equation} - """System of constraints that must be satisfied by the solution to the system.""" - constraintsystem::Union{Nothing, ConstraintsSystem} - """A set of expressions defining the costs of the system for optimal control.""" - costs::Vector - """Takes the cost vector and returns a scalar for optimization.""" - consolidate::Union{Nothing, Function} - """ - Time-derivative matrix. Note: this field will not be defined until - [`calculate_tgrad`](@ref) is called on the system. - """ - tgrad::RefValue{Vector{Num}} - """ - Jacobian matrix. Note: this field will not be defined until - [`calculate_jacobian`](@ref) is called on the system. - """ - jac::RefValue{Any} - """ - Control Jacobian matrix. Note: this field will not be defined until - [`calculate_control_jacobian`](@ref) is called on the system. - """ - ctrl_jac::RefValue{Any} - """ - Note: this field will not be defined until - [`generate_factorized_W`](@ref) is called on the system. - """ - Wfact::RefValue{Matrix{Num}} - """ - Note: this field will not be defined until - [`generate_factorized_W`](@ref) is called on the system. - """ - Wfact_t::RefValue{Matrix{Num}} - """ - The name of the system. - """ - name::Symbol - """ - A description of the system. - """ - description::String - """ - The internal systems. These are required to have unique names. - """ - systems::Vector{ODESystem} - """ - The default values to use when initial conditions and/or - parameters are not supplied in `ODEProblem`. - """ - defaults::Dict - """ - The guesses to use as the initial conditions for the - initialization system. - """ - guesses::Dict - """ - Tearing result specifying how to solve the system. - """ - torn_matching::Union{Matching, Nothing} - """ - The system for performing the initialization. - """ - initializesystem::Union{Nothing, NonlinearSystem} - """ - Extra equations to be enforced during the initialization sequence. - """ - initialization_eqs::Vector{Equation} - """ - The schedule for the code generation process. - """ - schedule::Any - """ - Type of the system. - """ - connector_type::Any - """ - Inject assignment statements before the evaluation of the RHS function. - """ - preface::Any - """ - A `Vector{SymbolicContinuousCallback}` that model events. - The integrator will use root finding to guarantee that it steps at each zero crossing. - """ - continuous_events::Vector{SymbolicContinuousCallback} - """ - A `Vector{SymbolicDiscreteCallback}` that models events. Symbolic - analog to `SciMLBase.DiscreteCallback` that executes an affect when a given condition is - true at the end of an integration step. - """ - discrete_events::Vector{SymbolicDiscreteCallback} - """ - Topologically sorted parameter dependency equations, where all symbols are parameters and - the LHS is a single parameter. - """ - parameter_dependencies::Vector{Equation} - """ - Mapping of conditions which should be true throughout the solution process to corresponding error - messages. These will be added to the equations when calling `debug_system`. - """ - assertions::Dict{BasicSymbolic, String} - """ - Metadata for the system, to be used by downstream packages. - """ - metadata::Any - """ - Metadata for MTK GUI. - """ - gui_metadata::Union{Nothing, GUIMetadata} - """ - A boolean indicating if the given `ODESystem` represents a system of DDEs. - """ - is_dde::Bool - """ - A list of points to provide to the solver as tstops. Uses the same syntax as discrete - events. - """ - tstops::Vector{Any} - """ - Cache for intermediate tearing state. - """ - tearing_state::Any - """ - Substitutions generated by tearing. - """ - substitutions::Any - """ - If false, then `sys.x` no longer performs namespacing. - """ - namespacing::Bool - """ - If true, denotes the model will not be modified any further. - """ - complete::Bool - """ - Cached data for fast symbolic indexing. - """ - index_cache::Union{Nothing, IndexCache} - """ - A list of discrete subsystems. - """ - discrete_subsystems::Any - """ - A list of actual unknowns needed to be solved by solvers. - """ - solved_unknowns::Union{Nothing, Vector{Any}} - """ - A vector of vectors of indices for the split parameters. - """ - split_idxs::Union{Nothing, Vector{Vector{Int}}} - """ - The analysis points removed by transformations, representing connections to be - ignored. The first element of the tuple analysis points connecting systems and - the second are ones connecting variables (for the trivial form of `connect`). - """ - ignored_connections::Union{ - Nothing, Tuple{Vector{IgnoredAnalysisPoint}, Vector{IgnoredAnalysisPoint}}} - """ - The hierarchical parent system before simplification. - """ - parent::Any - - function ODESystem( - tag, deqs, iv, dvs, ps, tspan, var_to_name, ctrls, - observed, constraints, costs, consolidate, tgrad, - jac, ctrl_jac, Wfact, Wfact_t, name, description, systems, defaults, guesses, - torn_matching, initializesystem, initialization_eqs, schedule, - connector_type, preface, cevents, - devents, parameter_dependencies, assertions = Dict{BasicSymbolic, String}(), - metadata = nothing, gui_metadata = nothing, is_dde = false, - tstops = [], tearing_state = nothing, substitutions = nothing, - namespacing = true, complete = false, index_cache = nothing, - discrete_subsystems = nothing, solved_unknowns = nothing, - split_idxs = nothing, ignored_connections = nothing, parent = nothing; - checks::Union{Bool, Int} = true) - if checks == true || (checks & CheckComponents) > 0 - check_independent_variables([iv]) - check_variables(dvs, iv) - check_parameters(ps, iv) - check_equations(deqs, iv) - check_equations(equations(cevents), iv) - check_subsystems(systems) - end - if checks == true || (checks & CheckUnits) > 0 - u = __get_unit_type(dvs, ps, iv) - check_units(u, deqs) - end - new(tag, deqs, iv, dvs, ps, tspan, var_to_name, - ctrls, observed, constraints, costs, consolidate, tgrad, jac, - ctrl_jac, Wfact, Wfact_t, name, description, systems, defaults, guesses, torn_matching, - initializesystem, initialization_eqs, schedule, connector_type, preface, - cevents, devents, parameter_dependencies, assertions, metadata, - gui_metadata, is_dde, tstops, tearing_state, substitutions, namespacing, - complete, index_cache, - discrete_subsystems, solved_unknowns, split_idxs, ignored_connections, parent) - end -end - -function ODESystem(deqs::AbstractVector{<:Equation}, iv, dvs, ps; - controls = Num[], - observed = Equation[], - constraints = Any[], - costs = Num[], - consolidate = nothing, - systems = ODESystem[], - tspan = nothing, - name = nothing, - description = "", - default_u0 = Dict(), - default_p = Dict(), - defaults = _merge(Dict(default_u0), Dict(default_p)), - guesses = Dict(), - initializesystem = nothing, - initialization_eqs = Equation[], - schedule = nothing, - connector_type = nothing, - preface = nothing, - continuous_events = nothing, - discrete_events = nothing, - parameter_dependencies = Equation[], - assertions = Dict(), - checks = true, - metadata = nothing, - gui_metadata = nothing, - is_dde = nothing, - tstops = [], - discover_from_metadata = true) - name === nothing && - throw(ArgumentError("The `name` keyword must be provided. Please consider using the `@named` macro")) - @assert all(control -> any(isequal.(control, ps)), controls) "All controls must also be parameters." - - constraintsystem = nothing - if !isempty(constraints) - constraintsystem = process_constraint_system(constraints, dvs, ps, iv) - for p in parameters(constraintsystem) - !in(p, Set(ps)) && push!(ps, p) - end - end - - if !isempty(costs) - coststs, costps = process_costs(costs, dvs, ps, iv) - for p in costps - !in(p, Set(ps)) && push!(ps, p) - end - end - costs = wrap.(costs) - - iv′ = value(iv) - ps′ = value.(ps) - ctrl′ = value.(controls) - dvs′ = value.(dvs) - dvs′ = filter(x -> !isdelay(x, iv), dvs′) - - parameter_dependencies, ps′ = process_parameter_dependencies( - parameter_dependencies, ps′) - if !(isempty(default_u0) && isempty(default_p)) - Base.depwarn( - "`default_u0` and `default_p` are deprecated. Use `defaults` instead.", - :ODESystem, force = true) - end - defaults = Dict{Any, Any}(todict(defaults)) - guesses = Dict{Any, Any}(todict(guesses)) - var_to_name = Dict() - let defaults = discover_from_metadata ? defaults : Dict(), - guesses = discover_from_metadata ? guesses : Dict() - - process_variables!(var_to_name, defaults, guesses, dvs′) - process_variables!(var_to_name, defaults, guesses, ps′) - process_variables!( - var_to_name, defaults, guesses, [eq.lhs for eq in parameter_dependencies]) - process_variables!( - var_to_name, defaults, guesses, [eq.rhs for eq in parameter_dependencies]) - end - defaults = Dict{Any, Any}(value(k) => value(v) - for (k, v) in pairs(defaults) if v !== nothing) - guesses = Dict{Any, Any}(value(k) => value(v) - for (k, v) in pairs(guesses) if v !== nothing) - - isempty(observed) || collect_var_to_name!(var_to_name, (eq.lhs for eq in observed)) - - tgrad = RefValue(EMPTY_TGRAD) - jac = RefValue{Any}(EMPTY_JAC) - ctrl_jac = RefValue{Any}(EMPTY_JAC) - Wfact = RefValue(EMPTY_JAC) - Wfact_t = RefValue(EMPTY_JAC) - sysnames = nameof.(systems) - if length(unique(sysnames)) != length(sysnames) - throw(ArgumentError("System names must be unique.")) - end - - cont_callbacks, disc_callbacks = create_symbolic_events( - continuous_events, discrete_events, deqs, iv) - if is_dde === nothing - is_dde = _check_if_dde(deqs, iv′, systems) - end - - if !isempty(systems) && !isnothing(constraintsystem) - conssystems = ConstraintsSystem[] - for sys in systems - cons = get_constraintsystem(sys) - cons !== nothing && push!(conssystems, cons) - end - @set! constraintsystem.systems = conssystems - end - costs = wrap.(costs) - - if length(costs) > 1 && isnothing(consolidate) - error("Must specify a consolidation function for the costs vector.") - elseif length(costs) == 1 && isnothing(consolidate) - consolidate = u -> u[1] - end - - assertions = Dict{BasicSymbolic, Any}(unwrap(k) => v for (k, v) in assertions) - - ODESystem(Threads.atomic_add!(SYSTEM_COUNT, UInt(1)), - deqs, iv′, dvs′, ps′, tspan, var_to_name, ctrl′, observed, - constraintsystem, costs, consolidate, tgrad, jac, - ctrl_jac, Wfact, Wfact_t, name, description, systems, - defaults, guesses, nothing, initializesystem, - initialization_eqs, schedule, connector_type, preface, cont_callbacks, - disc_callbacks, parameter_dependencies, assertions, - metadata, gui_metadata, is_dde, tstops, checks = checks) -end - -function ODESystem(eqs, iv; kwargs...) - diffvars, allunknowns, ps, eqs = process_equations(eqs, iv) - - for eq in get(kwargs, :parameter_dependencies, Equation[]) - collect_vars!(allunknowns, ps, eq, iv) - end - - for ssys in get(kwargs, :systems, ODESystem[]) - collect_scoped_vars!(allunknowns, ps, ssys, iv) - end - - for v in allunknowns - isdelay(v, iv) || continue - collect_vars!(allunknowns, ps, arguments(v)[1], iv) - end - - new_ps = OrderedSet() - for p in ps - if iscall(p) && operation(p) === getindex - par = arguments(p)[begin] - if Symbolics.shape(Symbolics.unwrap(par)) !== Symbolics.Unknown() && - all(par[i] in ps for i in eachindex(par)) - push!(new_ps, par) - else - push!(new_ps, p) - end - else - push!(new_ps, p) - end - end - algevars = setdiff(allunknowns, diffvars) - - return ODESystem(eqs, iv, collect(Iterators.flatten((diffvars, algevars))), - collect(new_ps); kwargs...) -end - -# NOTE: equality does not check cached Jacobian -function Base.:(==)(sys1::ODESystem, sys2::ODESystem) - sys1 === sys2 && return true - iv1 = get_iv(sys1) - iv2 = get_iv(sys2) - isequal(iv1, iv2) && - isequal(nameof(sys1), nameof(sys2)) && - _eq_unordered(get_eqs(sys1), get_eqs(sys2)) && - _eq_unordered(get_unknowns(sys1), get_unknowns(sys2)) && - _eq_unordered(get_ps(sys1), get_ps(sys2)) && - _eq_unordered(continuous_events(sys1), continuous_events(sys2)) && - _eq_unordered(discrete_events(sys1), discrete_events(sys2)) && - all(s1 == s2 for (s1, s2) in zip(get_systems(sys1), get_systems(sys2))) && - isequal(get_constraintsystem(sys1), get_constraintsystem(sys2)) && - _eq_unordered(get_costs(sys1), get_costs(sys2)) -end - -function flatten(sys::ODESystem, noeqs = false) - systems = get_systems(sys) - if isempty(systems) - return sys - else - return ODESystem(noeqs ? Equation[] : equations(sys), - get_iv(sys), - unknowns(sys), - parameters(sys; initial_parameters = true), - parameter_dependencies = parameter_dependencies(sys), - guesses = guesses(sys), - observed = observed(sys), - continuous_events = continuous_events(sys), - discrete_events = discrete_events(sys), - defaults = defaults(sys), - name = nameof(sys), - description = description(sys), - initialization_eqs = initialization_equations(sys), - assertions = assertions(sys), - is_dde = is_dde(sys), - tstops = symbolic_tstops(sys), - metadata = get_metadata(sys), - checks = false, - # without this, any defaults/guesses obtained from metadata that were - # later removed by the user will be re-added. Right now, we just want to - # retain `defaults(sys)` as-is. - discover_from_metadata = false) - end -end - -ODESystem(eq::Equation, args...; kwargs...) = ODESystem([eq], args...; kwargs...) - -""" - build_explicit_observed_function(sys, ts; kwargs...) -> Function(s) - -Generates a function that computes the observed value(s) `ts` in the system `sys`, while making the assumption that there are no cycles in the equations. - -## Arguments -- `sys`: The system for which to generate the function -- `ts`: The symbolic observed values whose value should be computed - -## Keywords -- `return_inplace = false`: If true and the observed value is a vector, then return both the in place and out of place methods. -- `expression = false`: Generates a Julia `Expr`` computing the observed value if `expression` is true -- `eval_expression = false`: If true and `expression = false`, evaluates the returned function in the module `eval_module` -- `output_type = Array` the type of the array generated by a out-of-place vector-valued function -- `param_only = false` if true, only allow the generated function to access system parameters -- `inputs = Any[]` additional symbolic variables that should be provided to the generated function -- `checkbounds = true` checks bounds if true when destructuring parameters -- `op = Operator` sets the recursion terminator for the walk done by `vars` to identify the variables that appear in `ts`. See the documentation for `vars` for more detail. -- `throw = true` if true, throw an error when generating a function for `ts` that reference variables that do not exist. -- `mkarray`: only used if the output is an array (that is, `!isscalar(ts)` and `ts` is not a tuple, in which case the result will always be a tuple). Called as `mkarray(ts, output_type)` where `ts` are the expressions to put in the array and `output_type` is the argument of the same name passed to build_explicit_observed_function. -- `cse = true`: Whether to use Common Subexpression Elimination (CSE) to generate a more efficient function. - -## Returns - -The return value will be either: -* a single function `f_oop` if the input is a scalar or if the input is a Vector but `return_inplace` is false -* the out of place and in-place functions `(f_ip, f_oop)` if `return_inplace` is true and the input is a `Vector` - -The function(s) `f_oop` (and potentially `f_ip`) will be: -* `RuntimeGeneratedFunction`s by default, -* A Julia `Expr` if `expression` is true, -* A directly evaluated Julia function in the module `eval_module` if `eval_expression` is true and `expression` is false. - -The signatures will be of the form `g(...)` with arguments: - -- `output` for in-place functions -- `unknowns` if `param_only` is `false` -- `inputs` if `inputs` is an array of symbolic inputs that should be available in `ts` -- `p...` unconditionally; note that in the case of `MTKParameters` more than one parameters argument may be present, so it must be splatted -- `t` if the system is time-dependent; for example `NonlinearSystem` will not have `t` - -For example, a function `g(op, unknowns, p..., inputs, t)` will be the in-place function generated if `return_inplace` is true, `ts` is a vector, -an array of inputs `inputs` is given, and `param_only` is false for a time-dependent system. -""" -function build_explicit_observed_function(sys, ts; - inputs = Any[], - disturbance_inputs = Any[], - disturbance_argument = false, - expression = false, - eval_expression = false, - eval_module = @__MODULE__, - output_type = Array, - checkbounds = true, - ps = parameters(sys; initial_parameters = true), - return_inplace = false, - param_only = false, - op = Operator, - throw = true, - cse = true, - mkarray = nothing) - is_tuple = ts isa Tuple - if is_tuple - ts = collect(ts) - output_type = Tuple - end - - allsyms = all_symbols(sys) - if symbolic_type(ts) == NotSymbolic() && ts isa AbstractArray - ts = map(x -> symbol_to_symbolic(sys, x; allsyms), ts) - else - ts = symbol_to_symbolic(sys, ts; allsyms) - end - - vs = ModelingToolkit.vars(ts; op) - namespace_subs = Dict() - ns_map = Dict{Any, Any}(renamespace(sys, obs) => obs for obs in observables(sys)) - for sym in unknowns(sys) - ns_map[renamespace(sys, sym)] = sym - if iscall(sym) && operation(sym) === getindex - ns_map[renamespace(sys, arguments(sym)[1])] = arguments(sym)[1] - end - end - for sym in full_parameters(sys) - ns_map[renamespace(sys, sym)] = sym - if iscall(sym) && operation(sym) === getindex - ns_map[renamespace(sys, arguments(sym)[1])] = arguments(sym)[1] - end - end - allsyms = Set(all_symbols(sys)) - iv = has_iv(sys) ? get_iv(sys) : nothing - for var in vs - var = unwrap(var) - newvar = get(ns_map, var, nothing) - if newvar !== nothing - namespace_subs[var] = newvar - var = newvar - end - if throw && !var_in_varlist(var, allsyms, iv) - Base.throw(ArgumentError("Symbol $var is not present in the system.")) - end - end - ts = fast_substitute(ts, namespace_subs) - - obsfilter = if param_only - if is_split(sys) - let ic = get_index_cache(sys) - eq -> !(ContinuousTimeseries() in ic.observed_syms_to_timeseries[eq.lhs]) - end - else - Returns(false) - end - else - Returns(true) - end - dvs = if param_only - () - else - (unknowns(sys),) - end - if isempty(inputs) - inputs = () - else - ps = setdiff(ps, inputs) # Inputs have been converted to parameters by io_preprocessing, remove those from the parameter list - inputs = (inputs,) - end - if !isempty(disturbance_inputs) - # Disturbance inputs may or may not be included as inputs, depending on disturbance_argument - ps = setdiff(ps, disturbance_inputs) - end - if disturbance_argument - disturbance_inputs = (disturbance_inputs,) - else - disturbance_inputs = () - end - ps = reorder_parameters(sys, ps) - iv = if is_time_dependent(sys) - (get_iv(sys),) - else - () - end - args = (dvs..., inputs..., ps..., iv..., disturbance_inputs...) - p_start = length(dvs) + length(inputs) + 1 - p_end = length(dvs) + length(inputs) + length(ps) - fns = build_function_wrapper( - sys, ts, args...; p_start, p_end, filter_observed = obsfilter, - output_type, mkarray, try_namespaced = true, expression = Val{true}, cse) - if fns isa Tuple - if expression - return return_inplace ? fns : fns[1] - end - oop, iip = eval_or_rgf.(fns; eval_expression, eval_module) - f = GeneratedFunctionWrapper{( - p_start + is_dde(sys), length(args) - length(ps) + 1 + is_dde(sys), is_split(sys))}( - oop, iip) - return return_inplace ? (f, f) : f - else - if expression - return fns - end - f = eval_or_rgf(fns; eval_expression, eval_module) - f = GeneratedFunctionWrapper{( - p_start + is_dde(sys), length(args) - length(ps) + 1 + is_dde(sys), is_split(sys))}( - f, nothing) - return f - end -end - -function populate_delays(delays::Set, obsexprs, histfn, sys, sym) - _vars_util = vars(sym) - for v in _vars_util - v in delays && continue - iscall(v) && issym(operation(v)) && (args = arguments(v); length(args) == 1) && - iscall(only(args)) || continue - - idx = variable_index(sys, operation(v)(get_iv(sys))) - idx === nothing && error("Delay term $v is not an unknown in the system") - push!(delays, v) - push!(obsexprs, v ← histfn(only(args))[idx]) - end -end - -function _eq_unordered(a, b) - # a and b may be multidimensional - # e.g. comparing noiseeqs of SDESystem - a = vec(a) - b = vec(b) - length(a) === length(b) || return false - n = length(a) - idxs = Set(1:n) - for x in a - idx = findfirst(isequal(x), b) - # loop since there might be multiple identical entries in a/b - # and while we might have already matched the first there could - # be a second that is equal to x - while idx !== nothing && !(idx in idxs) - idx = findnext(isequal(x), b, idx + 1) - end - idx === nothing && return false - delete!(idxs, idx) - end - return true -end - -# We have a stand-alone function to convert a `NonlinearSystem` or `ODESystem` -# to an `ODESystem` to connect systems, and we later can reply on -# `structural_simplify` to convert `ODESystem`s to `NonlinearSystem`s. -""" -$(TYPEDSIGNATURES) - -Convert a `NonlinearSystem` to an `ODESystem` or converts an `ODESystem` to a -new `ODESystem` with a different independent variable. -""" -function convert_system(::Type{<:ODESystem}, sys, t; name = nameof(sys)) - isempty(observed(sys)) || - throw(ArgumentError("`convert_system` cannot handle reduced model (i.e. observed(sys) is non-empty).")) - t = value(t) - varmap = Dict() - sts = unknowns(sys) - newsts = similar(sts, Any) - for (i, s) in enumerate(sts) - if iscall(s) - args = arguments(s) - length(args) == 1 || - throw(InvalidSystemException("Illegal unknown: $s. The unknown can have at most one argument like `x(t)`.")) - arg = args[1] - if isequal(arg, t) - newsts[i] = s - continue - end - ns = maketerm(typeof(s), operation(s), Any[t], - SymbolicUtils.metadata(s)) - newsts[i] = ns - varmap[s] = ns - else - ns = variable(getname(s); T = FnType)(t) - newsts[i] = ns - varmap[s] = ns - end - end - sub = Base.Fix2(substitute, varmap) - if sys isa AbstractODESystem - iv = only(independent_variables(sys)) - sub.x[iv] = t # otherwise the Differentials aren't fixed - end - neweqs = map(sub, equations(sys)) - defs = Dict(sub(k) => sub(v) for (k, v) in defaults(sys)) - return ODESystem(neweqs, t, newsts, parameters(sys); defaults = defs, name = name, - checks = false) -end - -""" -$(SIGNATURES) - -Add accumulation variables for `vars`. -""" -function add_accumulations(sys::ODESystem, vars = unknowns(sys)) - avars = [rename(v, Symbol(:accumulation_, getname(v))) for v in vars] - add_accumulations(sys, avars .=> vars) -end - -""" -$(SIGNATURES) - -Add accumulation variables for `vars`. `vars` is a vector of pairs in the form -of - -```julia -[cumulative_var1 => x + y, cumulative_var2 => x^2] -``` -Then, cumulative variables `cumulative_var1` and `cumulative_var2` that computes -the cumulative `x + y` and `x^2` would be added to `sys`. -""" -function add_accumulations(sys::ODESystem, vars::Vector{<:Pair}) - eqs = get_eqs(sys) - avars = map(first, vars) - if (ints = intersect(avars, unknowns(sys)); !isempty(ints)) - error("$ints already exist in the system!") - end - D = Differential(get_iv(sys)) - @set! sys.eqs = [eqs; Equation[D(a) ~ v[2] for (a, v) in zip(avars, vars)]] - @set! sys.unknowns = [get_unknowns(sys); avars] - @set! sys.defaults = merge(get_defaults(sys), Dict(a => 0.0 for a in avars)) -end - -function Base.show(io::IO, mime::MIME"text/plain", sys::ODESystem; hint = true, bold = true) - # Print general AbstractSystem information - invoke(Base.show, Tuple{typeof(io), typeof(mime), AbstractSystem}, - io, mime, sys; hint, bold) - - name = nameof(sys) - - # Print initialization equations (unique to ODESystems) - nini = length(initialization_equations(sys)) - nini > 0 && printstyled(io, "\nInitialization equations ($nini):"; bold) - nini > 0 && hint && print(io, " see initialization_equations($name)") - - return nothing -end - -""" -Build the constraint system for the ODESystem. -""" -function process_constraint_system( - constraints::Vector, sts, ps, iv; consname = :cons) - isempty(constraints) && return nothing - - constraintsts = OrderedSet() - constraintps = OrderedSet() - for cons in constraints - collect_vars!(constraintsts, constraintps, cons, iv) - union!(constraintsts, collect_applied_operators(cons, Differential)) - end - - # Validate the states. - validate_vars_and_find_ps!(constraintsts, constraintps, sts, iv) - - ConstraintsSystem( - constraints, collect(constraintsts), collect(constraintps); name = consname) -end - -""" -Process the costs for the constraint system. -""" -function process_costs(costs::Vector, sts, ps, iv) - coststs = OrderedSet() - costps = OrderedSet() - for cost in costs - collect_vars!(coststs, costps, cost, iv) - end - - validate_vars_and_find_ps!(coststs, costps, sts, iv) - coststs, costps -end - -""" -Validate that all the variables in an auxiliary system of the ODESystem (constraint or costs) are -well-formed states or parameters. - - Callable/delay variables (e.g. of the form x(0.6) should be unknowns of the system (and have one arg, etc.) - - Callable/delay parameters should be parameters of the system - -Return the set of additional parameters found in the system, e.g. in x(p) ~ 3 then p should be added as a -parameter of the system. -""" -function validate_vars_and_find_ps!(auxvars, auxps, sysvars, iv) - sts = Set(sysvars) - - for var in auxvars - if !iscall(var) - var ∈ sts || - throw(ArgumentError("Time-independent variable $var is not an unknown of the system.")) - elseif length(arguments(var)) > 1 - throw(ArgumentError("Too many arguments for variable $var.")) - elseif length(arguments(var)) == 1 - if iscall(var) && operation(var) isa Differential - var = only(arguments(var)) - end - arg = only(arguments(var)) - operation(var)(iv) ∈ sts || - throw(ArgumentError("Variable $var is not a variable of the ODESystem. Called variables must be variables of the ODESystem.")) - - isequal(arg, iv) || isparameter(arg) || arg isa Integer || - arg isa AbstractFloat || - throw(ArgumentError("Invalid argument specified for variable $var. The argument of the variable should be either $iv, a parameter, or a value specifying the time that the constraint holds.")) - - (isparameter(arg) && !isequal(arg, iv)) && push!(auxps, arg) - else - var ∈ sts && - @warn "Variable $var has no argument. It will be interpreted as $var($iv), and the constraint will apply to the entire interval." - end - end -end - -""" -Generate a function that takes a solution object and computes the cost function obtained by coalescing the costs vector. -""" -function generate_cost_function(sys::ODESystem, kwargs...) - costs = get_costs(sys) - consolidate = get_consolidate(sys) - iv = get_iv(sys) - - ps = parameters(sys; initial_parameters = false) - sts = unknowns(sys) - np = length(ps) - ns = length(sts) - stidxmap = Dict([v => i for (i, v) in enumerate(sts)]) - pidxmap = Dict([v => i for (i, v) in enumerate(ps)]) - - @variables sol(..)[1:ns] - for st in vars(costs) - x = operation(st) - t = only(arguments(st)) - idx = stidxmap[x(iv)] - - costs = map(c -> Symbolics.fast_substitute(c, Dict(x(t) => sol(t)[idx])), costs) - end - - _p = reorder_parameters(sys, ps) - fs = build_function_wrapper(sys, costs, sol, _p..., t; output_type = Array, kwargs...) - vc_oop, vc_iip = eval_or_rgf.(fs) - - cost(sol, p, t) = consolidate(vc_oop(sol, p, t)) - return cost -end From a69b40324500cad9339f0261eeec8eca555bf079 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Sun, 20 Apr 2025 14:00:59 +0530 Subject: [PATCH 1618/2176] refactor: add `_eq_unordered` to `utils.jl` fixmeup --- src/utils.jl | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/src/utils.jl b/src/utils.jl index a2034ec58a..8505b798b1 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -1311,3 +1311,34 @@ function var_in_varlist(var, varlist::AbstractSet, iv) # delayed variables (isdelay(var, iv) && var_in_varlist(operation(var)(iv), varlist, iv)) end + +""" + $(TYPEDSIGNATURES) + +Check if `a` and `b` contain identical elements, regardless of order. This is not +equivalent to `issetequal` because the latter does not account for identical elements that +have different multiplicities in `a` and `b`. +""" +function _eq_unordered(a::AbstractArray, b::AbstractArray) + # a and b may be multidimensional + # e.g. comparing noiseeqs of SDESystem + a = vec(a) + b = vec(b) + length(a) === length(b) || return false + n = length(a) + idxs = Set(1:n) + for x in a + idx = findfirst(isequal(x), b) + # loop since there might be multiple identical entries in a/b + # and while we might have already matched the first there could + # be a second that is equal to x + while idx !== nothing && !(idx in idxs) + idx = findnext(isequal(x), b, idx + 1) + end + idx === nothing && return false + delete!(idxs, idx) + end + return true +end + +_eq_unordered(a, b) = isequal(a, b) From 66255d02343725462a4093c83fa02a10b60a50b2 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 14 May 2025 12:34:20 +0530 Subject: [PATCH 1619/2176] refactor: move `flatten_equations` to `utils.jl` --- src/utils.jl | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/utils.jl b/src/utils.jl index 8505b798b1..8f5ac311c5 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -1342,3 +1342,31 @@ function _eq_unordered(a::AbstractArray, b::AbstractArray) end _eq_unordered(a, b) = isequal(a, b) + +""" + $(TYPEDSIGNATURES) + +Given a list of equations where some may be array equations, flatten the array equations +without scalarizing occurrences of array variables and return the new list of equations. +""" +function flatten_equations(eqs::Vector{Equation}) + mapreduce(vcat, eqs; init = Equation[]) do eq + islhsarr = eq.lhs isa AbstractArray || Symbolics.isarraysymbolic(eq.lhs) + isrhsarr = eq.rhs isa AbstractArray || Symbolics.isarraysymbolic(eq.rhs) + if islhsarr || isrhsarr + islhsarr && isrhsarr || + error(""" + LHS ($(eq.lhs)) and RHS ($(eq.rhs)) must either both be array expressions \ + or both scalar + """) + size(eq.lhs) == size(eq.rhs) || + error(""" + Size of LHS ($(eq.lhs)) and RHS ($(eq.rhs)) must match: got \ + $(size(eq.lhs)) and $(size(eq.rhs)) + """) + return vec(collect(eq.lhs) .~ collect(eq.rhs)) + else + eq + end + end +end From 3e6f7f82bc62dd40da8e62bbe607d8eb498110d8 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Sun, 20 Apr 2025 15:21:23 +0530 Subject: [PATCH 1620/2176] refactor: remove `sdesystem.jl` --- src/ModelingToolkit.jl | 3 +- src/systems/diffeqs/sdesystem.jl | 941 ------------------------------- 2 files changed, 1 insertion(+), 943 deletions(-) delete mode 100644 src/systems/diffeqs/sdesystem.jl diff --git a/src/ModelingToolkit.jl b/src/ModelingToolkit.jl index d959c6fabc..f29df3ece3 100644 --- a/src/ModelingToolkit.jl +++ b/src/ModelingToolkit.jl @@ -170,7 +170,6 @@ include("systems/optimization/optimizationsystem.jl") include("systems/optimization/modelingtoolkitize.jl") include("systems/nonlinear/nonlinearsystem.jl") -include("systems/diffeqs/sdesystem.jl") include("systems/diffeqs/abstractodesystem.jl") include("systems/nonlinear/homotopy_continuation.jl") include("systems/nonlinear/modelingtoolkitize.jl") @@ -265,7 +264,7 @@ export AbstractTimeDependentSystem, export ODEFunction, ODEFunctionExpr, ODEProblemExpr, convert_system, add_accumulations, System export DAEFunctionExpr, DAEProblemExpr -export SDESystem, SDEFunction, SDEFunctionExpr, SDEProblemExpr +export SDEFunction, SDEFunctionExpr, SDEProblemExpr export SystemStructure export DiscreteSystem, DiscreteProblem, DiscreteFunction, DiscreteFunctionExpr export ImplicitDiscreteSystem, ImplicitDiscreteProblem, ImplicitDiscreteFunction, diff --git a/src/systems/diffeqs/sdesystem.jl b/src/systems/diffeqs/sdesystem.jl deleted file mode 100644 index 016154e1cc..0000000000 --- a/src/systems/diffeqs/sdesystem.jl +++ /dev/null @@ -1,941 +0,0 @@ -""" -$(TYPEDEF) - -A system of stochastic differential equations. - -# Fields -$(FIELDS) - -# Example - -```julia -using ModelingToolkit -using ModelingToolkit: t_nounits as t, D_nounits as D - -@parameters σ ρ β -@variables x(t) y(t) z(t) - -eqs = [D(x) ~ σ*(y-x), - D(y) ~ x*(ρ-z)-y, - D(z) ~ x*y - β*z] - -noiseeqs = [0.1*x, - 0.1*y, - 0.1*z] - -@named de = SDESystem(eqs,noiseeqs,t,[x,y,z],[σ,ρ,β]; tspan = (0, 1000.0)) -``` -""" -struct SDESystem <: AbstractODESystem - """ - A tag for the system. If two systems have the same tag, then they are - structurally identical. - """ - tag::UInt - """The expressions defining the drift term.""" - eqs::Vector{Equation} - """The expressions defining the diffusion term.""" - noiseeqs::AbstractArray - """Independent variable.""" - iv::BasicSymbolic{Real} - """Dependent variables. Must not contain the independent variable.""" - unknowns::Vector - """Parameter variables. Must not contain the independent variable.""" - ps::Vector - """Time span.""" - tspan::Union{NTuple{2, Any}, Nothing} - """Array variables.""" - var_to_name::Any - """Control parameters (some subset of `ps`).""" - ctrls::Vector - """Observed equations.""" - observed::Vector{Equation} - """ - Time-derivative matrix. Note: this field will not be defined until - [`calculate_tgrad`](@ref) is called on the system. - """ - tgrad::RefValue - """ - Jacobian matrix. Note: this field will not be defined until - [`calculate_jacobian`](@ref) is called on the system. - """ - jac::RefValue - """ - Control Jacobian matrix. Note: this field will not be defined until - [`calculate_control_jacobian`](@ref) is called on the system. - """ - ctrl_jac::RefValue{Any} - """ - Note: this field will not be defined until - [`generate_factorized_W`](@ref) is called on the system. - """ - Wfact::RefValue - """ - Note: this field will not be defined until - [`generate_factorized_W`](@ref) is called on the system. - """ - Wfact_t::RefValue - """ - The name of the system. - """ - name::Symbol - """ - A description of the system. - """ - description::String - """ - The internal systems. These are required to have unique names. - """ - systems::Vector{SDESystem} - """ - The default values to use when initial conditions and/or - parameters are not supplied in `ODEProblem`. - """ - defaults::Dict - """ - The guesses to use as the initial conditions for the - initialization system. - """ - guesses::Dict - """ - The system for performing the initialization. - """ - initializesystem::Union{Nothing, NonlinearSystem} - """ - Extra equations to be enforced during the initialization sequence. - """ - initialization_eqs::Vector{Equation} - """ - Type of the system. - """ - connector_type::Any - """ - A `Vector{SymbolicContinuousCallback}` that model events. - The integrator will use root finding to guarantee that it steps at each zero crossing. - """ - continuous_events::Vector{SymbolicContinuousCallback} - """ - A `Vector{SymbolicDiscreteCallback}` that models events. Symbolic - analog to `SciMLBase.DiscreteCallback` that executes an affect when a given condition is - true at the end of an integration step. - """ - discrete_events::Vector{SymbolicDiscreteCallback} - """ - Topologically sorted parameter dependency equations, where all symbols are parameters and - the LHS is a single parameter. - """ - parameter_dependencies::Vector{Equation} - """ - Mapping of conditions which should be true throughout the solution process to corresponding error - messages. These will be added to the equations when calling `debug_system`. - """ - assertions::Dict{BasicSymbolic, String} - """ - Metadata for the system, to be used by downstream packages. - """ - metadata::Any - """ - Metadata for MTK GUI. - """ - gui_metadata::Union{Nothing, GUIMetadata} - """ - If false, then `sys.x` no longer performs namespacing. - """ - namespacing::Bool - """ - If true, denotes the model will not be modified any further. - """ - complete::Bool - """ - Cached data for fast symbolic indexing. - """ - index_cache::Union{Nothing, IndexCache} - """ - The hierarchical parent system before simplification. - """ - parent::Any - """ - Signal for whether the noise equations should be treated as a scalar process. This should only - be `true` when `noiseeqs isa Vector`. - """ - is_scalar_noise::Bool - """ - A boolean indicating if the given `ODESystem` represents a system of DDEs. - """ - is_dde::Bool - isscheduled::Bool - tearing_state::Any - - function SDESystem(tag, deqs, neqs, iv, dvs, ps, tspan, var_to_name, ctrls, observed, - tgrad, jac, ctrl_jac, Wfact, Wfact_t, name, description, systems, defaults, - guesses, initializesystem, initialization_eqs, connector_type, - cevents, devents, parameter_dependencies, assertions = Dict{ - BasicSymbolic, Nothing}, - metadata = nothing, gui_metadata = nothing, namespacing = true, - complete = false, index_cache = nothing, parent = nothing, is_scalar_noise = false, - is_dde = false, - isscheduled = false, - tearing_state = nothing; - checks::Union{Bool, Int} = true) - if checks == true || (checks & CheckComponents) > 0 - check_independent_variables([iv]) - check_variables(dvs, iv) - check_parameters(ps, iv) - check_equations(deqs, iv) - check_equations(neqs, dvs) - if size(neqs, 1) != length(deqs) - throw(ArgumentError("Noise equations ill-formed. Number of rows must match number of drift equations. size(neqs,1) = $(size(neqs,1)) != length(deqs) = $(length(deqs))")) - end - check_equations(equations(cevents), iv) - if is_scalar_noise && neqs isa AbstractMatrix - throw(ArgumentError("Noise equations ill-formed. Received a matrix of noise equations of size $(size(neqs)), but `is_scalar_noise` was set to `true`. Scalar noise is only compatible with an `AbstractVector` of noise equations.")) - end - check_subsystems(systems) - end - if checks == true || (checks & CheckUnits) > 0 - u = __get_unit_type(dvs, ps, iv) - check_units(u, deqs, neqs) - end - new(tag, deqs, neqs, iv, dvs, ps, tspan, var_to_name, ctrls, observed, tgrad, jac, - ctrl_jac, Wfact, Wfact_t, name, description, systems, - defaults, guesses, initializesystem, initialization_eqs, connector_type, cevents, - devents, parameter_dependencies, assertions, metadata, gui_metadata, namespacing, - complete, index_cache, parent, is_scalar_noise, is_dde, isscheduled, tearing_state) - end -end - -function SDESystem(deqs::AbstractVector{<:Equation}, neqs::AbstractArray, iv, dvs, ps; - controls = Num[], - observed = Num[], - systems = SDESystem[], - tspan = nothing, - default_u0 = Dict(), - default_p = Dict(), - defaults = _merge(Dict(default_u0), Dict(default_p)), - guesses = Dict(), - initializesystem = nothing, - initialization_eqs = Equation[], - name = nothing, - description = "", - connector_type = nothing, - checks = true, - continuous_events = nothing, - discrete_events = nothing, - parameter_dependencies = Equation[], - assertions = Dict{BasicSymbolic, String}(), - metadata = nothing, - gui_metadata = nothing, - index_cache = nothing, - parent = nothing, - is_scalar_noise = false, - is_dde = nothing) - name === nothing && - throw(ArgumentError("The `name` keyword must be provided. Please consider using the `@named` macro")) - iv′ = value(iv) - dvs′ = value.(dvs) - ps′ = value.(ps) - ctrl′ = value.(controls) - parameter_dependencies, ps′ = process_parameter_dependencies( - parameter_dependencies, ps′) - - sysnames = nameof.(systems) - if length(unique(sysnames)) != length(sysnames) - throw(ArgumentError("System names must be unique.")) - end - if !(isempty(default_u0) && isempty(default_p)) - Base.depwarn( - "`default_u0` and `default_p` are deprecated. Use `defaults` instead.", - :SDESystem, force = true) - end - - defaults = Dict{Any, Any}(todict(defaults)) - guesses = Dict{Any, Any}(todict(guesses)) - var_to_name = Dict() - process_variables!(var_to_name, defaults, guesses, dvs′) - process_variables!(var_to_name, defaults, guesses, ps′) - process_variables!( - var_to_name, defaults, guesses, [eq.lhs for eq in parameter_dependencies]) - process_variables!( - var_to_name, defaults, guesses, [eq.rhs for eq in parameter_dependencies]) - defaults = Dict{Any, Any}(value(k) => value(v) - for (k, v) in pairs(defaults) if v !== nothing) - guesses = Dict{Any, Any}(value(k) => value(v) - for (k, v) in pairs(guesses) if v !== nothing) - - isempty(observed) || collect_var_to_name!(var_to_name, (eq.lhs for eq in observed)) - - tgrad = RefValue(EMPTY_TGRAD) - jac = RefValue{Any}(EMPTY_JAC) - ctrl_jac = RefValue{Any}(EMPTY_JAC) - Wfact = RefValue(EMPTY_JAC) - Wfact_t = RefValue(EMPTY_JAC) - - cont_callbacks, disc_callbacks = create_symbolic_events( - continuous_events, discrete_events, deqs, iv) - - if is_dde === nothing - is_dde = _check_if_dde(deqs, iv′, systems) - end - assertions = Dict{BasicSymbolic, Any}(unwrap(k) => v for (k, v) in assertions) - SDESystem(Threads.atomic_add!(SYSTEM_COUNT, UInt(1)), - deqs, neqs, iv′, dvs′, ps′, tspan, var_to_name, ctrl′, observed, tgrad, jac, - ctrl_jac, Wfact, Wfact_t, name, description, systems, defaults, guesses, - initializesystem, initialization_eqs, connector_type, - cont_callbacks, disc_callbacks, parameter_dependencies, assertions, metadata, gui_metadata, - true, false, index_cache, parent, is_scalar_noise, is_dde; checks = checks) -end - -function SDESystem(sys::ODESystem, neqs; kwargs...) - SDESystem(equations(sys), neqs, get_iv(sys), unknowns(sys), parameters(sys); kwargs...) -end - -function SDESystem(eqs::Vector{Equation}, noiseeqs::AbstractArray, iv; kwargs...) - diffvars, allunknowns, ps, eqs = process_equations(eqs, iv) - - for eq in get(kwargs, :parameter_dependencies, Equation[]) - collect_vars!(allunknowns, ps, eq, iv) - end - - for ssys in get(kwargs, :systems, ODESystem[]) - collect_scoped_vars!(allunknowns, ps, ssys, iv) - end - - for v in allunknowns - isdelay(v, iv) || continue - collect_vars!(allunknowns, ps, arguments(v)[1], iv) - end - - new_ps = OrderedSet() - for p in ps - if iscall(p) && operation(p) === getindex - par = arguments(p)[begin] - if Symbolics.shape(Symbolics.unwrap(par)) !== Symbolics.Unknown() && - all(par[i] in ps for i in eachindex(par)) - push!(new_ps, par) - else - push!(new_ps, p) - end - else - push!(new_ps, p) - end - end - - # validate noise equations - noisedvs = OrderedSet() - noiseps = OrderedSet() - collect_vars!(noisedvs, noiseps, noiseeqs, iv) - for dv in noisedvs - dv ∈ allunknowns || - throw(ArgumentError("Variable $dv in noise equations is not an unknown of the system.")) - end - algevars = setdiff(allunknowns, diffvars) - return SDESystem(eqs, noiseeqs, iv, Iterators.flatten((diffvars, algevars)), - [collect(ps); collect(noiseps)]; kwargs...) -end - -function SDESystem(eq::Equation, noiseeqs::AbstractArray, args...; kwargs...) - SDESystem([eq], noiseeqs, args...; kwargs...) -end -function SDESystem(eq::Equation, noiseeq, args...; kwargs...) - SDESystem([eq], [noiseeq], args...; kwargs...) -end - -function Base.:(==)(sys1::SDESystem, sys2::SDESystem) - sys1 === sys2 && return true - iv1 = get_iv(sys1) - iv2 = get_iv(sys2) - isequal(iv1, iv2) && - isequal(nameof(sys1), nameof(sys2)) && - _eq_unordered(get_eqs(sys1), get_eqs(sys2)) && - _eq_unordered(get_noiseeqs(sys1), get_noiseeqs(sys2)) && - isequal(get_is_scalar_noise(sys1), get_is_scalar_noise(sys2)) && - _eq_unordered(get_unknowns(sys1), get_unknowns(sys2)) && - _eq_unordered(get_ps(sys1), get_ps(sys2)) && - _eq_unordered(continuous_events(sys1), continuous_events(sys2)) && - _eq_unordered(discrete_events(sys1), discrete_events(sys2)) && - all(s1 == s2 for (s1, s2) in zip(get_systems(sys1), get_systems(sys2))) -end - -""" - function ODESystem(sys::SDESystem) - -Convert an `SDESystem` to the equivalent `ODESystem` using `@brownian` variables instead -of noise equations. The returned system will not be `iscomplete` and will not have an -index cache, regardless of `iscomplete(sys)`. -""" -function ODESystem(sys::SDESystem) - neqs = get_noiseeqs(sys) - eqs = equations(sys) - is_scalar_noise = get_is_scalar_noise(sys) - nbrownian = if is_scalar_noise - length(neqs) - else - size(neqs, 2) - end - brownvars = map(1:nbrownian) do i - name = gensym(Symbol(:brown_, i)) - only(@brownian $name) - end - if is_scalar_noise - brownterms = reduce(+, neqs .* brownvars; init = 0) - neweqs = map(eqs) do eq - eq.lhs ~ eq.rhs + brownterms - end - else - if neqs isa AbstractVector - neqs = reshape(neqs, (length(neqs), 1)) - end - brownterms = neqs * brownvars - neweqs = map(eqs, brownterms) do eq, brown - eq.lhs ~ eq.rhs + brown - end - end - newsys = ODESystem(neweqs, get_iv(sys), unknowns(sys), parameters(sys); - parameter_dependencies = parameter_dependencies(sys), defaults = defaults(sys), - continuous_events = continuous_events(sys), discrete_events = discrete_events(sys), - assertions = assertions(sys), - name = nameof(sys), description = description(sys), metadata = get_metadata(sys)) - @set newsys.parent = sys -end - -function __num_isdiag_noise(mat) - for i in axes(mat, 1) - nnz = 0 - for j in axes(mat, 2) - if !isequal(mat[i, j], 0) - nnz += 1 - end - end - if nnz > 1 - return (false) - end - end - true -end -function __get_num_diag_noise(mat) - map(axes(mat, 1)) do i - for j in axes(mat, 2) - mij = mat[i, j] - if !isequal(mij, 0) - return mij - end - end - 0 - end -end - -function generate_diffusion_function(sys::SDESystem, dvs = unknowns(sys), - ps = parameters(sys; initial_parameters = true); isdde = false, kwargs...) - eqs = get_noiseeqs(sys) - p = reorder_parameters(sys, ps) - return build_function_wrapper(sys, eqs, dvs, p..., get_iv(sys); kwargs...) -end - -""" -$(TYPEDSIGNATURES) - -Choose correction_factor=-1//2 (1//2) to convert Ito -> Stratonovich (Stratonovich->Ito). -""" -function stochastic_integral_transform(sys::SDESystem, correction_factor) - name = nameof(sys) - # use the general interface - if typeof(get_noiseeqs(sys)) <: Vector - eqs = vcat([equations(sys)[i].lhs ~ get_noiseeqs(sys)[i] - for i in eachindex(unknowns(sys))]...) - de = ODESystem(eqs, get_iv(sys), unknowns(sys), parameters(sys), name = name, - checks = false) - - jac = calculate_jacobian(de, sparse = false, simplify = false) - ∇σσ′ = simplify.(jac * get_noiseeqs(sys)) - - deqs = vcat([equations(sys)[i].lhs ~ equations(sys)[i].rhs + - correction_factor * ∇σσ′[i] - for i in eachindex(unknowns(sys))]...) - else - dimunknowns, m = size(get_noiseeqs(sys)) - eqs = vcat([equations(sys)[i].lhs ~ get_noiseeqs(sys)[i] - for i in eachindex(unknowns(sys))]...) - de = ODESystem(eqs, get_iv(sys), unknowns(sys), parameters(sys), name = name, - checks = false) - - jac = calculate_jacobian(de, sparse = false, simplify = false) - ∇σσ′ = simplify.(jac * get_noiseeqs(sys)[:, 1]) - for k in 2:m - eqs = vcat([equations(sys)[i].lhs ~ get_noiseeqs(sys)[Int(i + - (k - 1) * - dimunknowns)] - for i in eachindex(unknowns(sys))]...) - de = ODESystem(eqs, get_iv(sys), unknowns(sys), parameters(sys), name = name, - checks = false) - - jac = calculate_jacobian(de, sparse = false, simplify = false) - ∇σσ′ = ∇σσ′ + simplify.(jac * get_noiseeqs(sys)[:, k]) - end - - deqs = vcat([equations(sys)[i].lhs ~ equations(sys)[i].rhs + - correction_factor * ∇σσ′[i] - for i in eachindex(unknowns(sys))]...) - end - - SDESystem(deqs, get_noiseeqs(sys), get_iv(sys), unknowns(sys), parameters(sys), - name = name, description = description(sys), - parameter_dependencies = parameter_dependencies(sys), checks = false) -end - -""" -$(TYPEDSIGNATURES) - -Measure transformation method that allows for a reduction in the variance of an estimator `Exp(g(X_t))`. -Input: Original SDE system and symbolic function `u(t,x)` with scalar output that - defines the adjustable parameters `d` in the Girsanov transformation. Optional: initial - condition for `θ0`. -Output: Modified SDESystem with additional component `θ_t` and initial value `θ0`, as well as - the weight `θ_t/θ0` as observed equation, such that the estimator `Exp(g(X_t)θ_t/θ0)` - has a smaller variance. - -Reference: -Kloeden, P. E., Platen, E., & Schurz, H. (2012). Numerical solution of SDE through computer -experiments. Springer Science & Business Media. - -# Example - -```julia -using ModelingToolkit -using ModelingToolkit: t_nounits as t, D_nounits as D - -@parameters α β -@variables x(t) y(t) z(t) - -eqs = [D(x) ~ α*x] -noiseeqs = [β*x] - -@named de = SDESystem(eqs,noiseeqs,t,[x],[α,β]) - -# define u (user choice) -u = x -θ0 = 0.1 -g(x) = x[1]^2 -demod = ModelingToolkit.Girsanov_transform(de, u; θ0=0.1) - -u0modmap = [ - x => x0 -] - -parammap = [ - α => 1.5, - β => 1.0 -] - -probmod = SDEProblem(complete(demod),u0modmap,(0.0,1.0),parammap) -ensemble_probmod = EnsembleProblem(probmod; - output_func = (sol,i) -> (g(sol[x,end])*sol[demod.weight,end],false), - ) - -simmod = solve(ensemble_probmod,EM(),dt=dt,trajectories=numtraj) -``` - -""" -function Girsanov_transform(sys::SDESystem, u; θ0 = 1.0) - name = nameof(sys) - - # register new variable θ corresponding to 1D correction process θ(t) - t = get_iv(sys) - D = Differential(t) - @variables θ(t), weight(t) - - # determine the adjustable parameters `d` given `u` - # gradient of u with respect to unknowns - grad = Symbolics.gradient(u, unknowns(sys)) - - noiseeqs = get_noiseeqs(sys) - if noiseeqs isa Vector - d = simplify.(-(noiseeqs .* grad) / u) - drift_correction = noiseeqs .* d - else - d = simplify.(-noiseeqs * grad / u) - drift_correction = noiseeqs * d - end - - # transformation adds additional unknowns θ: newX = (X,θ) - # drift function for unknowns is modified - # θ has zero drift - deqs = vcat([equations(sys)[i].lhs ~ equations(sys)[i].rhs - drift_correction[i] - for i in eachindex(unknowns(sys))]...) - deqsθ = D(θ) ~ 0 - push!(deqs, deqsθ) - - # diffusion matrix is of size d x m (d unknowns, m noise), with diagonal noise represented as a d-dimensional vector - # for diagonal noise processes with m>1, the noise process will become non-diagonal; extra unknown component but no new noise process. - # new diffusion matrix is of size d+1 x M - # diffusion for state is unchanged - - noiseqsθ = θ * d - - if noiseeqs isa Vector - m = size(noiseeqs) - if m == 1 - push!(noiseeqs, noiseqsθ) - else - noiseeqs = [Array(Diagonal(noiseeqs)); noiseqsθ'] - end - else - noiseeqs = [Array(noiseeqs); noiseqsθ'] - end - - unknown_vars = [unknowns(sys); θ] - - # return modified SDE System - SDESystem(deqs, noiseeqs, get_iv(sys), unknown_vars, parameters(sys); - defaults = Dict(θ => θ0), observed = [weight ~ θ / θ0], - name = name, description = description(sys), - parameter_dependencies = parameter_dependencies(sys), - checks = false) -end - -function DiffEqBase.SDEFunction{iip, specialize}(sys::SDESystem, dvs = unknowns(sys), - ps = parameters(sys), - u0 = nothing; - version = nothing, tgrad = false, sparse = false, - jac = false, Wfact = false, eval_expression = false, - sparsity = false, analytic = nothing, - eval_module = @__MODULE__, - checkbounds = false, initialization_data = nothing, - cse = true, kwargs...) where {iip, specialize} - if !iscomplete(sys) - error("A completed `SDESystem` is required. Call `complete` or `structural_simplify` on the system before creating an `SDEFunction`") - end - dvs = scalarize.(dvs) - - f_gen = generate_function(sys, dvs, ps; expression = Val{true}, cse, kwargs...) - f_oop, f_iip = eval_or_rgf.(f_gen; eval_expression, eval_module) - g_gen = generate_diffusion_function(sys, dvs, ps; expression = Val{true}, - cse, kwargs...) - g_oop, g_iip = eval_or_rgf.(g_gen; eval_expression, eval_module) - - f = GeneratedFunctionWrapper{(2, 3, is_split(sys))}(f_oop, f_iip) - g = GeneratedFunctionWrapper{(2, 3, is_split(sys))}(g_oop, g_iip) - - if tgrad - tgrad_gen = generate_tgrad(sys, dvs, ps; expression = Val{true}, cse, - kwargs...) - tgrad_oop, tgrad_iip = eval_or_rgf.(tgrad_gen; eval_expression, eval_module) - _tgrad = GeneratedFunctionWrapper{(2, 3, is_split(sys))}(tgrad_oop, tgrad_iip) - else - _tgrad = nothing - end - - if jac - jac_gen = generate_jacobian(sys, dvs, ps; expression = Val{true}, - sparse = sparse, cse, kwargs...) - jac_oop, jac_iip = eval_or_rgf.(jac_gen; eval_expression, eval_module) - - _jac = GeneratedFunctionWrapper{(2, 3, is_split(sys))}(jac_oop, jac_iip) - else - _jac = nothing - end - - if Wfact - tmp_Wfact, tmp_Wfact_t = generate_factorized_W(sys, dvs, ps, true; - expression = Val{true}, cse, kwargs...) - Wfact_oop, Wfact_iip = eval_or_rgf.(tmp_Wfact; eval_expression, eval_module) - Wfact_oop_t, Wfact_iip_t = eval_or_rgf.(tmp_Wfact_t; eval_expression, eval_module) - - _Wfact = GeneratedFunctionWrapper{(2, 4, is_split(sys))}(Wfact_oop, Wfact_iip) - _Wfact_t = GeneratedFunctionWrapper{(2, 4, is_split(sys))}(Wfact_oop_t, Wfact_iip_t) - else - _Wfact, _Wfact_t = nothing, nothing - end - - M = calculate_massmatrix(sys) - if sparse - uElType = u0 === nothing ? Float64 : eltype(u0) - W_prototype = similar(W_sparsity(sys), uElType) - else - W_prototype = nothing - end - - _M = if sparse && !(u0 === nothing || M === I) - SparseArrays.sparse(M) - elseif u0 === nothing || M === I - M - elseif M isa Diagonal - Diagonal(ArrayInterface.restructure(u0, diag(M))) - else - ArrayInterface.restructure(u0 .* u0', M) - end - - observedfun = ObservedFunctionCache( - sys; eval_expression, eval_module, checkbounds = get(kwargs, :checkbounds, false), cse) - - SDEFunction{iip, specialize}(f, g; - sys = sys, - jac = _jac === nothing ? nothing : _jac, - tgrad = _tgrad === nothing ? nothing : _tgrad, - mass_matrix = _M, - jac_prototype = W_prototype, - observed = observedfun, - sparsity = sparsity ? W_sparsity(sys) : nothing, - analytic = analytic, - Wfact = _Wfact === nothing ? nothing : _Wfact, - Wfact_t = _Wfact_t === nothing ? nothing : _Wfact_t, - initialization_data) -end - -""" -```julia -DiffEqBase.SDEFunction{iip}(sys::SDESystem, dvs = sys.unknowns, ps = sys.ps; - version = nothing, tgrad = false, sparse = false, - jac = false, Wfact = false, kwargs...) where {iip} -``` - -Create an `SDEFunction` from the [`SDESystem`](@ref). The arguments `dvs` and `ps` -are used to set the order of the dependent variable and parameter vectors, -respectively. -""" -function DiffEqBase.SDEFunction(sys::SDESystem, args...; kwargs...) - SDEFunction{true}(sys, args...; kwargs...) -end - -function DiffEqBase.SDEFunction{true}(sys::SDESystem, args...; - kwargs...) - SDEFunction{true, SciMLBase.AutoSpecialize}(sys, args...; kwargs...) -end - -function DiffEqBase.SDEFunction{false}(sys::SDESystem, args...; - kwargs...) - SDEFunction{false, SciMLBase.FullSpecialize}(sys, args...; kwargs...) -end - -""" -```julia -DiffEqBase.SDEFunctionExpr{iip}(sys::AbstractODESystem, dvs = unknowns(sys), - ps = parameters(sys); - version = nothing, tgrad = false, - jac = false, Wfact = false, - skipzeros = true, fillzeros = true, - sparse = false, - kwargs...) where {iip} -``` - -Create a Julia expression for an `SDEFunction` from the [`SDESystem`](@ref). -The arguments `dvs` and `ps` are used to set the order of the dependent -variable and parameter vectors, respectively. -""" -struct SDEFunctionExpr{iip} end - -function SDEFunctionExpr{iip}(sys::SDESystem, dvs = unknowns(sys), - ps = parameters(sys), u0 = nothing; - version = nothing, tgrad = false, - jac = false, Wfact = false, - sparse = false, linenumbers = false, - kwargs...) where {iip} - if !iscomplete(sys) - error("A completed `SDESystem` is required. Call `complete` or `structural_simplify` on the system before creating an `SDEFunctionExpr`") - end - idx = iip ? 2 : 1 - f = generate_function(sys, dvs, ps; expression = Val{true}, kwargs...)[idx] - g = generate_diffusion_function(sys, dvs, ps; expression = Val{true}, kwargs...)[idx] - if tgrad - _tgrad = generate_tgrad(sys, dvs, ps; expression = Val{true}, kwargs...)[idx] - else - _tgrad = :nothing - end - - if jac - _jac = generate_jacobian(sys, dvs, ps; sparse = sparse, expression = Val{true}, - kwargs...)[idx] - else - _jac = :nothing - end - - M = calculate_massmatrix(sys) - _M = (u0 === nothing || M == I) ? M : ArrayInterface.restructure(u0 .* u0', M) - - if sparse - uElType = u0 === nothing ? Float64 : eltype(u0) - W_prototype = similar(W_sparsity(sys), uElType) - else - W_prototype = nothing - end - - if Wfact - tmp_Wfact, tmp_Wfact_t = generate_factorized_W( - sys, dvs, ps; expression = Val{true}, - kwargs...) - _Wfact = tmp_Wfact[idx] - _Wfact_t = tmp_Wfact_t[idx] - else - _Wfact, _Wfact_t = :nothing, :nothing - end - - ex = quote - f = $f - g = $g - tgrad = $_tgrad - jac = $_jac - W_prototype = $W_prototype - Wfact = $_Wfact - Wfact_t = $_Wfact_t - M = $_M - SDEFunction{$iip}(f, g, - jac = jac, - jac_prototype = W_prototype, - tgrad = tgrad, - Wfact = Wfact, - Wfact_t = Wfact_t, - mass_matrix = M) - end - !linenumbers ? Base.remove_linenums!(ex) : ex -end - -function SDEFunctionExpr(sys::SDESystem, args...; kwargs...) - SDEFunctionExpr{true}(sys, args...; kwargs...) -end - -function DiffEqBase.SDEProblem{iip, specialize}( - sys::SDESystem, u0map = [], tspan = get_tspan(sys), - parammap = DiffEqBase.NullParameters(); - sparsenoise = nothing, check_length = true, - callback = nothing, kwargs...) where {iip, specialize} - if !iscomplete(sys) - error("A completed `SDESystem` is required. Call `complete` or `structural_simplify` on the system before creating an `SDEProblem`") - end - - f, u0, p = process_SciMLProblem( - SDEFunction{iip, specialize}, sys, u0map, parammap; check_length, - t = tspan === nothing ? nothing : tspan[1], kwargs...) - cbs = process_events(sys; callback, kwargs...) - sparsenoise === nothing && (sparsenoise = get(kwargs, :sparse, false)) - - noiseeqs = get_noiseeqs(sys) - is_scalar_noise = get_is_scalar_noise(sys) - if noiseeqs isa AbstractVector - noise_rate_prototype = nothing - if is_scalar_noise - noise = WienerProcess(0.0, 0.0, 0.0) - else - noise = nothing - end - elseif sparsenoise - I, J, V = findnz(SparseArrays.sparse(noiseeqs)) - noise_rate_prototype = SparseArrays.sparse(I, J, zero(eltype(u0))) - noise = nothing - else - noise_rate_prototype = zeros(eltype(u0), size(noiseeqs)) - noise = nothing - end - - kwargs = filter_kwargs(kwargs) - - # Call `remake` so it runs initialization if it is trivial - return remake(SDEProblem{iip}(f, u0, tspan, p; callback = cbs, noise, - noise_rate_prototype = noise_rate_prototype, kwargs...)) -end - -function DiffEqBase.SDEProblem(sys::ODESystem, args...; kwargs...) - if any(ModelingToolkit.isbrownian, unknowns(sys)) - error("SDESystem constructed by defining Brownian variables with @brownian must be simplified by calling `structural_simplify` before a SDEProblem can be constructed.") - else - error("Cannot construct SDEProblem from a normal ODESystem.") - end -end - -""" -```julia -DiffEqBase.SDEProblem{iip}(sys::SDESystem, u0map, tspan, p = parammap; - version = nothing, tgrad = false, - jac = false, Wfact = false, - checkbounds = false, sparse = false, - sparsenoise = sparse, - skipzeros = true, fillzeros = true, - linenumbers = true, parallel = SerialForm(), - kwargs...) -``` - -Generates an SDEProblem from an SDESystem and allows for automatically -symbolically calculating numerical enhancements. -""" -function DiffEqBase.SDEProblem(sys::SDESystem, args...; kwargs...) - SDEProblem{true}(sys, args...; kwargs...) -end - -function DiffEqBase.SDEProblem(sys::SDESystem, - u0map::StaticArray, - args...; - kwargs...) - SDEProblem{false, SciMLBase.FullSpecialize}(sys, u0map, args...; kwargs...) -end - -function DiffEqBase.SDEProblem{true}(sys::SDESystem, args...; kwargs...) - SDEProblem{true, SciMLBase.AutoSpecialize}(sys, args...; kwargs...) -end - -function DiffEqBase.SDEProblem{false}(sys::SDESystem, args...; kwargs...) - SDEProblem{false, SciMLBase.FullSpecialize}(sys, args...; kwargs...) -end - -""" -```julia -DiffEqBase.SDEProblemExpr{iip}(sys::AbstractODESystem, u0map, tspan, - parammap = DiffEqBase.NullParameters(); - version = nothing, tgrad = false, - jac = false, Wfact = false, - checkbounds = false, sparse = false, - linenumbers = true, parallel = SerialForm(), - kwargs...) where {iip} -``` - -Generates a Julia expression for constructing an ODEProblem from an -ODESystem and allows for automatically symbolically calculating -numerical enhancements. -""" -struct SDEProblemExpr{iip} end - -function SDEProblemExpr{iip}(sys::SDESystem, u0map, tspan, - parammap = DiffEqBase.NullParameters(); - sparsenoise = nothing, check_length = true, - kwargs...) where {iip} - if !iscomplete(sys) - error("A completed `SDESystem` is required. Call `complete` or `structural_simplify` on the system before creating an `SDEProblemExpr`") - end - f, u0, p = process_SciMLProblem( - SDEFunctionExpr{iip}, sys, u0map, parammap; check_length, - kwargs...) - linenumbers = get(kwargs, :linenumbers, true) - sparsenoise === nothing && (sparsenoise = get(kwargs, :sparse, false)) - - noiseeqs = get_noiseeqs(sys) - is_scalar_noise = get_is_scalar_noise(sys) - if noiseeqs isa AbstractVector - noise_rate_prototype = nothing - if is_scalar_noise - noise = WienerProcess(0.0, 0.0, 0.0) - else - noise = nothing - end - elseif sparsenoise - I, J, V = findnz(SparseArrays.sparse(noiseeqs)) - noise_rate_prototype = SparseArrays.sparse(I, J, zero(eltype(u0))) - noise = nothing - else - T = u0 === nothing ? Float64 : eltype(u0) - noise_rate_prototype = zeros(T, size(get_noiseeqs(sys))) - noise = nothing - end - ex = quote - f = $f - u0 = $u0 - tspan = $tspan - p = $p - noise_rate_prototype = $noise_rate_prototype - noise = $noise - SDEProblem( - f, u0, tspan, p; noise_rate_prototype = noise_rate_prototype, noise = noise, - $(kwargs...)) - end - !linenumbers ? Base.remove_linenums!(ex) : ex -end - -function SDEProblemExpr(sys::SDESystem, args...; kwargs...) - SDEProblemExpr{true}(sys, args...; kwargs...) -end From e486b27de96e6e1f06115a9e13863c7a3f6d80ea Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Sun, 20 Apr 2025 20:18:52 +0530 Subject: [PATCH 1621/2176] refactor: move `__num_isdiag_noise`, `get_num_diag_noise` to location of use --- src/systems/systems.jl | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/src/systems/systems.jl b/src/systems/systems.jl index f606b6b63b..399b263178 100644 --- a/src/systems/systems.jl +++ b/src/systems/systems.jl @@ -158,6 +158,33 @@ function __structural_simplify(sys::AbstractSystem; simplify = false, end end +function __num_isdiag_noise(mat) + for i in axes(mat, 1) + nnz = 0 + for j in axes(mat, 2) + if !isequal(mat[i, j], 0) + nnz += 1 + end + end + if nnz > 1 + return (false) + end + end + true +end + +function __get_num_diag_noise(mat) + map(axes(mat, 1)) do i + for j in axes(mat, 2) + mij = mat[i, j] + if !isequal(mij, 0) + return mij + end + end + 0 + end +end + """ $(TYPEDSIGNATURES) From 11135247c8a0ffbce7bc7d35b6e3acd013c6aab5 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Sun, 20 Apr 2025 20:22:55 +0530 Subject: [PATCH 1622/2176] refactor: remove `discrete_system.jl` --- src/ModelingToolkit.jl | 4 +- .../discrete_system/discrete_system.jl | 442 ------------------ 2 files changed, 3 insertions(+), 443 deletions(-) delete mode 100644 src/systems/discrete_system/discrete_system.jl diff --git a/src/ModelingToolkit.jl b/src/ModelingToolkit.jl index f29df3ece3..24ffb10ee7 100644 --- a/src/ModelingToolkit.jl +++ b/src/ModelingToolkit.jl @@ -178,6 +178,8 @@ include("systems/diffeqs/first_order_transform.jl") include("systems/diffeqs/modelingtoolkitize.jl") include("systems/diffeqs/basic_transformations.jl") +include("systems/discrete_system/implicit_discrete_system.jl") + include("systems/jumps/jumpsystem.jl") include("systems/pde/pdesystem.jl") @@ -266,7 +268,7 @@ export ODEFunction, ODEFunctionExpr, ODEProblemExpr, convert_system, export DAEFunctionExpr, DAEProblemExpr export SDEFunction, SDEFunctionExpr, SDEProblemExpr export SystemStructure -export DiscreteSystem, DiscreteProblem, DiscreteFunction, DiscreteFunctionExpr +export DiscreteProblem, DiscreteFunction, DiscreteFunctionExpr export ImplicitDiscreteSystem, ImplicitDiscreteProblem, ImplicitDiscreteFunction, ImplicitDiscreteFunctionExpr export JumpSystem diff --git a/src/systems/discrete_system/discrete_system.jl b/src/systems/discrete_system/discrete_system.jl deleted file mode 100644 index 8b392e6145..0000000000 --- a/src/systems/discrete_system/discrete_system.jl +++ /dev/null @@ -1,442 +0,0 @@ -""" -$(TYPEDEF) -A system of difference equations. -# Fields -$(FIELDS) -# Example -``` -using ModelingToolkit -using ModelingToolkit: t_nounits as t -@parameters σ=28.0 ρ=10.0 β=8/3 δt=0.1 -@variables x(t)=1.0 y(t)=0.0 z(t)=0.0 -k = ShiftIndex(t) -eqs = [x(k+1) ~ σ*(y-x), - y(k+1) ~ x*(ρ-z)-y, - z(k+1) ~ x*y - β*z] -@named de = DiscreteSystem(eqs,t,[x,y,z],[σ,ρ,β]; tspan = (0, 1000.0)) # or -@named de = DiscreteSystem(eqs) -``` -""" -struct DiscreteSystem <: AbstractDiscreteSystem - """ - A tag for the system. If two systems have the same tag, then they are - structurally identical. - """ - tag::UInt - """The differential equations defining the discrete system.""" - eqs::Vector{Equation} - """Independent variable.""" - iv::BasicSymbolic{Real} - """Dependent (state) variables. Must not contain the independent variable.""" - unknowns::Vector - """Parameter variables. Must not contain the independent variable.""" - ps::Vector - """Time span.""" - tspan::Union{NTuple{2, Any}, Nothing} - """Array variables.""" - var_to_name::Any - """Observed states.""" - observed::Vector{Equation} - """ - The name of the system - """ - name::Symbol - """ - A description of the system. - """ - description::String - """ - The internal systems. These are required to have unique names. - """ - systems::Vector{DiscreteSystem} - """ - The default values to use when initial conditions and/or - parameters are not supplied in `DiscreteProblem`. - """ - defaults::Dict - """ - The guesses to use as the initial conditions for the - initialization system. - """ - guesses::Dict - """ - The system for performing the initialization. - """ - initializesystem::Union{Nothing, NonlinearSystem} - """ - Extra equations to be enforced during the initialization sequence. - """ - initialization_eqs::Vector{Equation} - """ - Inject assignment statements before the evaluation of the RHS function. - """ - preface::Any - """ - Type of the system. - """ - connector_type::Any - """ - Topologically sorted parameter dependency equations, where all symbols are parameters and - the LHS is a single parameter. - """ - parameter_dependencies::Vector{Equation} - """ - Metadata for the system, to be used by downstream packages. - """ - metadata::Any - """ - Metadata for MTK GUI. - """ - gui_metadata::Union{Nothing, GUIMetadata} - """ - Cache for intermediate tearing state. - """ - tearing_state::Any - """ - Substitutions generated by tearing. - """ - substitutions::Any - """ - If false, then `sys.x` no longer performs namespacing. - """ - namespacing::Bool - """ - If true, denotes the model will not be modified any further. - """ - complete::Bool - """ - Cached data for fast symbolic indexing. - """ - index_cache::Union{Nothing, IndexCache} - """ - The hierarchical parent system before simplification. - """ - parent::Any - isscheduled::Bool - - function DiscreteSystem(tag, discreteEqs, iv, dvs, ps, tspan, var_to_name, - observed, name, description, systems, defaults, guesses, initializesystem, - initialization_eqs, preface, connector_type, parameter_dependencies = Equation[], - metadata = nothing, gui_metadata = nothing, - tearing_state = nothing, substitutions = nothing, namespacing = true, - complete = false, index_cache = nothing, parent = nothing, - isscheduled = false; - checks::Union{Bool, Int} = true, kwargs...) - if checks == true || (checks & CheckComponents) > 0 - check_independent_variables([iv]) - check_variables(dvs, iv) - check_parameters(ps, iv) - check_subsystems(systems) - end - if checks == true || (checks & CheckUnits) > 0 - u = __get_unit_type(dvs, ps, iv) - check_units(u, discreteEqs) - end - new(tag, discreteEqs, iv, dvs, ps, tspan, var_to_name, observed, name, description, - systems, defaults, guesses, initializesystem, initialization_eqs, - preface, connector_type, parameter_dependencies, metadata, gui_metadata, - tearing_state, substitutions, namespacing, complete, index_cache, parent, - isscheduled) - end -end - -""" - $(TYPEDSIGNATURES) -Constructs a DiscreteSystem. -""" -function DiscreteSystem(eqs::AbstractVector{<:Equation}, iv, dvs, ps; - observed = Num[], - systems = DiscreteSystem[], - tspan = nothing, - name = nothing, - description = "", - default_u0 = Dict(), - default_p = Dict(), - guesses = Dict(), - initializesystem = nothing, - initialization_eqs = Equation[], - defaults = _merge(Dict(default_u0), Dict(default_p)), - preface = nothing, - connector_type = nothing, - parameter_dependencies = Equation[], - metadata = nothing, - gui_metadata = nothing, - kwargs...) - name === nothing && - throw(ArgumentError("The `name` keyword must be provided. Please consider using the `@named` macro")) - iv′ = value(iv) - dvs′ = value.(dvs) - ps′ = value.(ps) - if any(hasderiv, eqs) || any(hashold, eqs) || any(hassample, eqs) || any(hasdiff, eqs) - error("Equations in a `DiscreteSystem` can only have `Shift` operators.") - end - if !(isempty(default_u0) && isempty(default_p)) - Base.depwarn( - "`default_u0` and `default_p` are deprecated. Use `defaults` instead.", - :DiscreteSystem, force = true) - end - - defaults = Dict{Any, Any}(todict(defaults)) - guesses = Dict{Any, Any}(todict(guesses)) - var_to_name = Dict() - process_variables!(var_to_name, defaults, guesses, dvs′) - process_variables!(var_to_name, defaults, guesses, ps′) - process_variables!( - var_to_name, defaults, guesses, [eq.lhs for eq in parameter_dependencies]) - process_variables!( - var_to_name, defaults, guesses, [eq.rhs for eq in parameter_dependencies]) - defaults = Dict{Any, Any}(value(k) => value(v) - for (k, v) in pairs(defaults) if v !== nothing) - guesses = Dict{Any, Any}(value(k) => value(v) - for (k, v) in pairs(guesses) if v !== nothing) - - isempty(observed) || collect_var_to_name!(var_to_name, (eq.lhs for eq in observed)) - - sysnames = nameof.(systems) - if length(unique(sysnames)) != length(sysnames) - throw(ArgumentError("System names must be unique.")) - end - DiscreteSystem(Threads.atomic_add!(SYSTEM_COUNT, UInt(1)), - eqs, iv′, dvs′, ps′, tspan, var_to_name, observed, name, description, systems, - defaults, guesses, initializesystem, initialization_eqs, preface, connector_type, - parameter_dependencies, metadata, gui_metadata) -end - -function DiscreteSystem(eqs, iv; kwargs...) - eqs = collect(eqs) - diffvars = OrderedSet() - allunknowns = OrderedSet() - ps = OrderedSet() - iv = value(iv) - for eq in eqs - collect_vars!(allunknowns, ps, eq, iv; op = Shift) - if iscall(eq.lhs) && operation(eq.lhs) isa Shift - isequal(iv, operation(eq.lhs).t) || - throw(ArgumentError("A DiscreteSystem can only have one independent variable.")) - eq.lhs in diffvars && - throw(ArgumentError("The shift variable $(eq.lhs) is not unique in the system of equations.")) - push!(diffvars, eq.lhs) - end - end - for eq in get(kwargs, :parameter_dependencies, Equation[]) - if eq isa Pair - collect_vars!(allunknowns, ps, eq, iv) - else - collect_vars!(allunknowns, ps, eq, iv) - end - end - new_ps = OrderedSet() - for p in ps - if iscall(p) && operation(p) === getindex - par = arguments(p)[begin] - if Symbolics.shape(Symbolics.unwrap(par)) !== Symbolics.Unknown() && - all(par[i] in ps for i in eachindex(par)) - push!(new_ps, par) - else - push!(new_ps, p) - end - else - push!(new_ps, p) - end - end - return DiscreteSystem(eqs, iv, - collect(allunknowns), collect(new_ps); kwargs...) -end - -DiscreteSystem(eq::Equation, args...; kwargs...) = DiscreteSystem([eq], args...; kwargs...) - -function flatten(sys::DiscreteSystem, noeqs = false) - systems = get_systems(sys) - if isempty(systems) - return sys - else - return DiscreteSystem(noeqs ? Equation[] : equations(sys), - get_iv(sys), - unknowns(sys), - parameters(sys), - observed = observed(sys), - defaults = defaults(sys), - guesses = guesses(sys), - initialization_eqs = initialization_equations(sys), - name = nameof(sys), - description = description(sys), - metadata = get_metadata(sys), - checks = false) - end -end - -function generate_function( - sys::DiscreteSystem, dvs = unknowns(sys), ps = parameters(sys); wrap_code = identity, kwargs...) - exprs = [eq.rhs for eq in equations(sys)] - generate_custom_function(sys, exprs, dvs, ps; kwargs...) -end - -function shift_u0map_forward(sys::DiscreteSystem, u0map, defs) - iv = get_iv(sys) - updated = AnyDict() - for k in collect(keys(u0map)) - v = u0map[k] - if !((op = operation(k)) isa Shift) - isnothing(getunshifted(k)) && - error("Initial conditions must be for the past state of the unknowns. Instead of providing the condition for $k, provide the condition for $(Shift(iv, -1)(k)).") - - updated[Shift(iv, 1)(k)] = v - elseif op.steps > 0 - error("Initial conditions must be for the past state of the unknowns. Instead of providing the condition for $k, provide the condition for $(Shift(iv, -1)(only(arguments(k)))).") - else - updated[Shift(iv, op.steps + 1)(only(arguments(k)))] = v - end - end - for var in unknowns(sys) - op = operation(var) - root = getunshifted(var) - shift = getshift(var) - isnothing(root) && continue - (haskey(updated, Shift(iv, shift)(root)) || haskey(updated, var)) && continue - haskey(defs, root) || error("Initial condition for $var not provided.") - updated[var] = defs[root] - end - return updated -end - -""" - $(TYPEDSIGNATURES) -Generates an DiscreteProblem from an DiscreteSystem. -""" -function SciMLBase.DiscreteProblem( - sys::DiscreteSystem, u0map = [], tspan = get_tspan(sys), - parammap = SciMLBase.NullParameters(); - eval_module = @__MODULE__, - eval_expression = false, - kwargs... -) - if !iscomplete(sys) - error("A completed `DiscreteSystem` is required. Call `complete` or `structural_simplify` on the system before creating a `DiscreteProblem`") - end - dvs = unknowns(sys) - ps = parameters(sys) - eqs = equations(sys) - iv = get_iv(sys) - - u0map = to_varmap(u0map, dvs) - scalarize_varmap!(u0map) - u0map = shift_u0map_forward(sys, u0map, defaults(sys)) - f, u0, p = process_SciMLProblem( - DiscreteFunction, sys, u0map, parammap; eval_expression, eval_module, build_initializeprob = false) - u0 = f(u0, p, tspan[1]) - DiscreteProblem(f, u0, tspan, p; kwargs...) -end - -function SciMLBase.DiscreteFunction(sys::DiscreteSystem, args...; kwargs...) - DiscreteFunction{true}(sys, args...; kwargs...) -end - -function SciMLBase.DiscreteFunction{true}(sys::DiscreteSystem, args...; kwargs...) - DiscreteFunction{true, SciMLBase.AutoSpecialize}(sys, args...; kwargs...) -end - -function SciMLBase.DiscreteFunction{false}(sys::DiscreteSystem, args...; kwargs...) - DiscreteFunction{false, SciMLBase.FullSpecialize}(sys, args...; kwargs...) -end - -""" -```julia -SciMLBase.DiscreteFunction{iip}(sys::DiscreteSystem, - dvs = unknowns(sys), - ps = parameters(sys); - kwargs...) where {iip} -``` - -Create an `DiscreteFunction` from the [`DiscreteSystem`](@ref). The arguments `dvs` and `ps` -are used to set the order of the dependent variable and parameter vectors, -respectively. -""" -function SciMLBase.DiscreteFunction{iip, specialize}( - sys::DiscreteSystem, - dvs = unknowns(sys), - ps = parameters(sys), - u0 = nothing; - version = nothing, - p = nothing, - t = nothing, - eval_expression = false, - eval_module = @__MODULE__, - analytic = nothing, cse = true, - kwargs...) where {iip, specialize} - if !iscomplete(sys) - error("A completed `DiscreteSystem` is required. Call `complete` or `structural_simplify` on the system before creating a `DiscreteProblem`") - end - f_gen = generate_function(sys, dvs, ps; expression = Val{true}, - expression_module = eval_module, cse, kwargs...) - f_oop, f_iip = eval_or_rgf.(f_gen; eval_expression, eval_module) - f = GeneratedFunctionWrapper{(2, 3, is_split(sys))}(f_oop, f_iip) - - if specialize === SciMLBase.FunctionWrapperSpecialize && iip - if u0 === nothing || p === nothing || t === nothing - error("u0, p, and t must be specified for FunctionWrapperSpecialize on DiscreteFunction.") - end - f = SciMLBase.wrapfun_iip(f, (u0, u0, p, t)) - end - - observedfun = ObservedFunctionCache( - sys; eval_expression, eval_module, checkbounds = get(kwargs, :checkbounds, false), cse) - - DiscreteFunction{iip, specialize}(f; - sys = sys, - observed = observedfun, - analytic = analytic) -end - -""" -```julia -DiscreteFunctionExpr{iip}(sys::DiscreteSystem, dvs = states(sys), - ps = parameters(sys); - version = nothing, - kwargs...) where {iip} -``` - -Create a Julia expression for an `DiscreteFunction` from the [`DiscreteSystem`](@ref). -The arguments `dvs` and `ps` are used to set the order of the dependent -variable and parameter vectors, respectively. -""" -struct DiscreteFunctionExpr{iip} end -struct DiscreteFunctionClosure{O, I} <: Function - f_oop::O - f_iip::I -end -(f::DiscreteFunctionClosure)(u, p, t) = f.f_oop(u, p, t) -(f::DiscreteFunctionClosure)(du, u, p, t) = f.f_iip(du, u, p, t) - -function DiscreteFunctionExpr{iip}(sys::DiscreteSystem, dvs = unknowns(sys), - ps = parameters(sys), u0 = nothing; - version = nothing, p = nothing, - linenumbers = false, - simplify = false, - kwargs...) where {iip} - f_oop, f_iip = generate_function(sys, dvs, ps; expression = Val{true}, kwargs...) - - fsym = gensym(:f) - _f = :($fsym = $DiscreteFunctionClosure($f_oop, $f_iip)) - - ex = quote - $_f - DiscreteFunction{$iip}($fsym) - end - !linenumbers ? Base.remove_linenums!(ex) : ex -end - -function DiscreteFunctionExpr(sys::DiscreteSystem, args...; kwargs...) - DiscreteFunctionExpr{true}(sys, args...; kwargs...) -end - -function Base.:(==)(sys1::DiscreteSystem, sys2::DiscreteSystem) - sys1 === sys2 && return true - isequal(nameof(sys1), nameof(sys2)) && - isequal(get_iv(sys1), get_iv(sys2)) && - _eq_unordered(get_eqs(sys1), get_eqs(sys2)) && - _eq_unordered(get_unknowns(sys1), get_unknowns(sys2)) && - _eq_unordered(get_ps(sys1), get_ps(sys2)) && - all(s1 == s2 for (s1, s2) in zip(get_systems(sys1), get_systems(sys2))) -end - -supports_initialization(::DiscreteSystem) = false From 343606727caf7c65cb744d761c974b361083cc9a Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Sun, 20 Apr 2025 21:37:21 +0530 Subject: [PATCH 1623/2176] refactor: remove `implicit_discrete_system.jl` --- src/ModelingToolkit.jl | 4 +- .../implicit_discrete_system.jl | 459 ------------------ 2 files changed, 1 insertion(+), 462 deletions(-) delete mode 100644 src/systems/discrete_system/implicit_discrete_system.jl diff --git a/src/ModelingToolkit.jl b/src/ModelingToolkit.jl index 24ffb10ee7..0a4e8a475b 100644 --- a/src/ModelingToolkit.jl +++ b/src/ModelingToolkit.jl @@ -178,8 +178,6 @@ include("systems/diffeqs/first_order_transform.jl") include("systems/diffeqs/modelingtoolkitize.jl") include("systems/diffeqs/basic_transformations.jl") -include("systems/discrete_system/implicit_discrete_system.jl") - include("systems/jumps/jumpsystem.jl") include("systems/pde/pdesystem.jl") @@ -269,7 +267,7 @@ export DAEFunctionExpr, DAEProblemExpr export SDEFunction, SDEFunctionExpr, SDEProblemExpr export SystemStructure export DiscreteProblem, DiscreteFunction, DiscreteFunctionExpr -export ImplicitDiscreteSystem, ImplicitDiscreteProblem, ImplicitDiscreteFunction, +export ImplicitDiscreteProblem, ImplicitDiscreteFunction, ImplicitDiscreteFunctionExpr export JumpSystem export ODEProblem, SDEProblem diff --git a/src/systems/discrete_system/implicit_discrete_system.jl b/src/systems/discrete_system/implicit_discrete_system.jl deleted file mode 100644 index b818ffcc8d..0000000000 --- a/src/systems/discrete_system/implicit_discrete_system.jl +++ /dev/null @@ -1,459 +0,0 @@ -""" -$(TYPEDEF) -An implicit system of difference equations. -# Fields -$(FIELDS) -# Example -``` -using ModelingToolkit -using ModelingToolkit: t_nounits as t -@parameters σ=28.0 ρ=10.0 β=8/3 δt=0.1 -@variables x(t)=1.0 y(t)=0.0 z(t)=0.0 -k = ShiftIndex(t) -eqs = [x ~ σ*(y-x(k-1)), - y ~ x(k-1)*(ρ-z(k-1))-y, - z ~ x(k-1)*y(k-1) - β*z] -@named ide = ImplicitDiscreteSystem(eqs,t,[x,y,z],[σ,ρ,β]; tspan = (0, 1000.0)) -``` -""" -struct ImplicitDiscreteSystem <: AbstractDiscreteSystem - """ - A tag for the system. If two systems have the same tag, then they are - structurally identical. - """ - tag::UInt - """The difference equations defining the discrete system.""" - eqs::Vector{Equation} - """Independent variable.""" - iv::BasicSymbolic{Real} - """Dependent (state) variables. Must not contain the independent variable.""" - unknowns::Vector - """Parameter variables. Must not contain the independent variable.""" - ps::Vector - """Time span.""" - tspan::Union{NTuple{2, Any}, Nothing} - """Array variables.""" - var_to_name::Any - """Observed states.""" - observed::Vector{Equation} - """ - The name of the system - """ - name::Symbol - """ - A description of the system. - """ - description::String - """ - The internal systems. These are required to have unique names. - """ - systems::Vector{ImplicitDiscreteSystem} - """ - The default values to use when initial conditions and/or - parameters are not supplied in `ImplicitDiscreteProblem`. - """ - defaults::Dict - """ - The guesses to use as the initial conditions for the - initialization system. - """ - guesses::Dict - """ - The system for performing the initialization. - """ - initializesystem::Union{Nothing, NonlinearSystem} - """ - Extra equations to be enforced during the initialization sequence. - """ - initialization_eqs::Vector{Equation} - """ - Inject assignment statements before the evaluation of the RHS function. - """ - preface::Any - """ - Type of the system. - """ - connector_type::Any - """ - Topologically sorted parameter dependency equations, where all symbols are parameters and - the LHS is a single parameter. - """ - parameter_dependencies::Vector{Equation} - """ - Metadata for the system, to be used by downstream packages. - """ - metadata::Any - """ - Metadata for MTK GUI. - """ - gui_metadata::Union{Nothing, GUIMetadata} - """ - Cache for intermediate tearing state. - """ - tearing_state::Any - """ - Substitutions generated by tearing. - """ - substitutions::Any - """ - If false, then `sys.x` no longer performs namespacing. - """ - namespacing::Bool - """ - If true, denotes the model will not be modified any further. - """ - complete::Bool - """ - Cached data for fast symbolic indexing. - """ - index_cache::Union{Nothing, IndexCache} - """ - The hierarchical parent system before simplification. - """ - parent::Any - isscheduled::Bool - - function ImplicitDiscreteSystem(tag, discreteEqs, iv, dvs, ps, tspan, var_to_name, - observed, name, description, systems, defaults, guesses, initializesystem, - initialization_eqs, preface, connector_type, parameter_dependencies = Equation[], - metadata = nothing, gui_metadata = nothing, - tearing_state = nothing, substitutions = nothing, namespacing = true, - complete = false, index_cache = nothing, parent = nothing, - isscheduled = false; - checks::Union{Bool, Int} = true) - if checks == true || (checks & CheckComponents) > 0 - check_independent_variables([iv]) - check_variables(dvs, iv) - check_parameters(ps, iv) - check_subsystems(systems) - end - if checks == true || (checks & CheckUnits) > 0 - u = __get_unit_type(dvs, ps, iv) - check_units(u, discreteEqs) - end - new(tag, discreteEqs, iv, dvs, ps, tspan, var_to_name, observed, name, description, - systems, defaults, guesses, initializesystem, initialization_eqs, - preface, connector_type, parameter_dependencies, metadata, gui_metadata, - tearing_state, substitutions, namespacing, complete, index_cache, parent, - isscheduled) - end -end - -""" - $(TYPEDSIGNATURES) - -Constructs a ImplicitDiscreteSystem. -""" -function ImplicitDiscreteSystem(eqs::AbstractVector{<:Equation}, iv, dvs, ps; - observed = Num[], - systems = ImplicitDiscreteSystem[], - tspan = nothing, - name = nothing, - description = "", - default_u0 = Dict(), - default_p = Dict(), - guesses = Dict(), - initializesystem = nothing, - initialization_eqs = Equation[], - defaults = _merge(Dict(default_u0), Dict(default_p)), - preface = nothing, - connector_type = nothing, - parameter_dependencies = Equation[], - metadata = nothing, - gui_metadata = nothing, - kwargs...) - name === nothing && - throw(ArgumentError("The `name` keyword must be provided. Please consider using the `@named` macro")) - iv′ = value(iv) - dvs′ = value.(dvs) - ps′ = value.(ps) - if any(hasderiv, eqs) || any(hashold, eqs) || any(hassample, eqs) || any(hasdiff, eqs) - error("Equations in a `ImplicitDiscreteSystem` can only have `Shift` operators.") - end - if !(isempty(default_u0) && isempty(default_p)) - Base.depwarn( - "`default_u0` and `default_p` are deprecated. Use `defaults` instead.", - :ImplicitDiscreteSystem, force = true) - end - - # Copy equations to canonical form, but do not touch array expressions - eqs = [wrap(eq.lhs) isa Symbolics.Arr ? eq : 0 ~ eq.rhs - eq.lhs for eq in eqs] - defaults = Dict{Any, Any}(todict(defaults)) - guesses = Dict{Any, Any}(todict(guesses)) - var_to_name = Dict() - process_variables!(var_to_name, defaults, guesses, dvs′) - process_variables!(var_to_name, defaults, guesses, ps′) - process_variables!( - var_to_name, defaults, guesses, [eq.lhs for eq in parameter_dependencies]) - process_variables!( - var_to_name, defaults, guesses, [eq.rhs for eq in parameter_dependencies]) - defaults = Dict{Any, Any}(value(k) => value(v) - for (k, v) in pairs(defaults) if v !== nothing) - guesses = Dict{Any, Any}(value(k) => value(v) - for (k, v) in pairs(guesses) if v !== nothing) - - isempty(observed) || collect_var_to_name!(var_to_name, (eq.lhs for eq in observed)) - - sysnames = nameof.(systems) - if length(unique(sysnames)) != length(sysnames) - throw(ArgumentError("System names must be unique.")) - end - ImplicitDiscreteSystem(Threads.atomic_add!(SYSTEM_COUNT, UInt(1)), - eqs, iv′, dvs′, ps′, tspan, var_to_name, observed, name, description, systems, - defaults, guesses, initializesystem, initialization_eqs, preface, connector_type, - parameter_dependencies, metadata, gui_metadata, kwargs...) -end - -function ImplicitDiscreteSystem(eqs, iv; kwargs...) - eqs = collect(eqs) - diffvars = OrderedSet() - allunknowns = OrderedSet() - ps = OrderedSet() - iv = value(iv) - for eq in eqs - collect_vars!(allunknowns, ps, eq, iv; op = Shift) - if iscall(eq.lhs) && operation(eq.lhs) isa Shift - isequal(iv, operation(eq.lhs).t) || - throw(ArgumentError("An ImplicitDiscreteSystem can only have one independent variable.")) - eq.lhs in diffvars && - throw(ArgumentError("The shift variable $(eq.lhs) is not unique in the system of equations.")) - push!(diffvars, eq.lhs) - end - end - for eq in get(kwargs, :parameter_dependencies, Equation[]) - if eq isa Pair - collect_vars!(allunknowns, ps, eq, iv) - else - collect_vars!(allunknowns, ps, eq, iv) - end - end - new_ps = OrderedSet() - for p in ps - if iscall(p) && operation(p) === getindex - par = arguments(p)[begin] - if Symbolics.shape(Symbolics.unwrap(par)) !== Symbolics.Unknown() && - all(par[i] in ps for i in eachindex(par)) - push!(new_ps, par) - else - push!(new_ps, p) - end - else - push!(new_ps, p) - end - end - return ImplicitDiscreteSystem(eqs, iv, - collect(allunknowns), collect(new_ps); kwargs...) -end - -function ImplicitDiscreteSystem(eq::Equation, args...; kwargs...) - ImplicitDiscreteSystem([eq], args...; kwargs...) -end - -function flatten(sys::ImplicitDiscreteSystem, noeqs = false) - systems = get_systems(sys) - if isempty(systems) - return sys - else - return ImplicitDiscreteSystem(noeqs ? Equation[] : equations(sys), - get_iv(sys), - unknowns(sys), - parameters(sys), - observed = observed(sys), - defaults = defaults(sys), - guesses = guesses(sys), - initialization_eqs = initialization_equations(sys), - name = nameof(sys), - description = description(sys), - metadata = get_metadata(sys), - checks = false) - end -end - -function generate_function( - sys::ImplicitDiscreteSystem, dvs = unknowns(sys), ps = parameters(sys); - wrap_code = identity, cachesyms::Tuple = (), kwargs...) - iv = get_iv(sys) - # Algebraic equations get shifted forward 1, to match with differential equations - exprs = map(equations(sys)) do eq - _iszero(eq.lhs) ? distribute_shift(Shift(iv, 1)(eq.rhs)) : (eq.rhs - eq.lhs) - end - - # Handle observables in algebraic equations, since they are shifted - obs = observed(sys) - shifted_obs = Symbolics.Equation[distribute_shift(Shift(iv, 1)(eq)) for eq in obs] - obsidxs = observed_equations_used_by(sys, exprs; obs = shifted_obs) - extra_assignments = [Assignment(shifted_obs[i].lhs, shifted_obs[i].rhs) - for i in obsidxs] - - u_next = map(Shift(iv, 1), dvs) - u = dvs - p = (reorder_parameters(sys, unwrap.(ps))..., cachesyms...) - build_function_wrapper( - sys, exprs, u_next, u, p..., iv; p_start = 3, extra_assignments, kwargs...) -end - -function shift_u0map_forward(sys::ImplicitDiscreteSystem, u0map, defs) - iv = get_iv(sys) - updated = AnyDict() - for k in collect(keys(u0map)) - v = u0map[k] - if !((op = operation(k)) isa Shift) - updated[k] = v - elseif op.steps > 0 - error("Initial conditions must be for the current or past state of the unknowns. Instead of providing the condition for $k, provide the condition for $(Shift(iv, -1)(only(arguments(k)))).") - else - updated[k] = v - end - end - for var in unknowns(sys) - op = operation(var) - root = getunshifted(var) - shift = getshift(var) - isnothing(root) && continue - (haskey(updated, Shift(iv, shift)(root)) || haskey(updated, var)) && continue - haskey(defs, root) || error("Initial condition for $var not provided.") - updated[var] = defs[root] - end - return updated -end - -""" - $(TYPEDSIGNATURES) -Generates an ImplicitDiscreteProblem from an ImplicitDiscreteSystem. -""" -function SciMLBase.ImplicitDiscreteProblem( - sys::ImplicitDiscreteSystem, u0map = [], tspan = get_tspan(sys), - parammap = SciMLBase.NullParameters(); - eval_module = @__MODULE__, - eval_expression = false, - kwargs... -) - if !iscomplete(sys) - error("A completed `ImplicitDiscreteSystem` is required. Call `complete` or `structural_simplify` on the system before creating a `ImplicitDiscreteProblem`.") - end - dvs = unknowns(sys) - ps = parameters(sys) - eqs = equations(sys) - iv = get_iv(sys) - - u0map = to_varmap(u0map, dvs) - u0map = shift_u0map_forward(sys, u0map, defaults(sys)) - f, u0, p = process_SciMLProblem( - ImplicitDiscreteFunction, sys, u0map, parammap; eval_expression, eval_module, kwargs...) - - kwargs = filter_kwargs(kwargs) - ImplicitDiscreteProblem(f, u0, tspan, p; kwargs...) -end - -function SciMLBase.ImplicitDiscreteFunction(sys::ImplicitDiscreteSystem, args...; kwargs...) - ImplicitDiscreteFunction{true}(sys, args...; kwargs...) -end - -function SciMLBase.ImplicitDiscreteFunction{true}( - sys::ImplicitDiscreteSystem, args...; kwargs...) - ImplicitDiscreteFunction{true, SciMLBase.AutoSpecialize}(sys, args...; kwargs...) -end - -function SciMLBase.ImplicitDiscreteFunction{false}( - sys::ImplicitDiscreteSystem, args...; kwargs...) - ImplicitDiscreteFunction{false, SciMLBase.FullSpecialize}(sys, args...; kwargs...) -end -function SciMLBase.ImplicitDiscreteFunction{iip, specialize}( - sys::ImplicitDiscreteSystem, - dvs = unknowns(sys), - ps = parameters(sys), - u0 = nothing; - version = nothing, - p = nothing, - t = nothing, - eval_expression = false, - eval_module = @__MODULE__, - analytic = nothing, cse = true, - kwargs...) where {iip, specialize} - if !iscomplete(sys) - error("A completed `ImplicitDiscreteSystem` is required. Call `complete` or `structural_simplify` on the system before creating a `ImplicitDiscreteProblem`") - end - f_gen = generate_function(sys, dvs, ps; expression = Val{true}, - expression_module = eval_module, cse, kwargs...) - f_oop, f_iip = eval_or_rgf.(f_gen; eval_expression, eval_module) - f(u_next, u, p, t) = f_oop(u_next, u, p, t) - f(resid, u_next, u, p, t) = f_iip(resid, u_next, u, p, t) - - if length(dvs) == length(equations(sys)) - resid_prototype = nothing - else - resid_prototype = calculate_resid_prototype(length(equations(sys)), u0, p) - end - - if specialize === SciMLBase.FunctionWrapperSpecialize && iip - if u0 === nothing || p === nothing || t === nothing - error("u0, p, and t must be specified for FunctionWrapperSpecialize on ImplicitDiscreteFunction.") - end - f = SciMLBase.wrapfun_iip(f, (u0, u0, p, t)) - end - - observedfun = ObservedFunctionCache( - sys; eval_expression, eval_module, checkbounds = get(kwargs, :checkbounds, false), cse) - - ImplicitDiscreteFunction{iip, specialize}(f; - sys = sys, - observed = observedfun, - analytic = analytic, - resid_prototype = resid_prototype, - kwargs...) -end - -""" -```julia -ImplicitDiscreteFunctionExpr{iip}(sys::ImplicitDiscreteSystem, dvs = states(sys), - ps = parameters(sys); - version = nothing, - kwargs...) where {iip} -``` - -Create a Julia expression for an `ImplicitDiscreteFunction` from the [`ImplicitDiscreteSystem`](@ref). -The arguments `dvs` and `ps` are used to set the order of the dependent -variable and parameter vectors, respectively. -""" -struct ImplicitDiscreteFunctionExpr{iip} end -struct ImplicitDiscreteFunctionClosure{O, I} <: Function - f_oop::O - f_iip::I -end -(f::ImplicitDiscreteFunctionClosure)(u_next, u, p, t) = f.f_oop(u_next, u, p, t) -function (f::ImplicitDiscreteFunctionClosure)(resid, u_next, u, p, t) - f.f_iip(resid, u_next, u, p, t) -end - -function ImplicitDiscreteFunctionExpr{iip}( - sys::ImplicitDiscreteSystem, dvs = unknowns(sys), - ps = parameters(sys), u0 = nothing; - version = nothing, p = nothing, - linenumbers = false, - simplify = false, - kwargs...) where {iip} - f_oop, f_iip = generate_function(sys, dvs, ps; expression = Val{true}, kwargs...) - - fsym = gensym(:f) - _f = :($fsym = $ImplicitDiscreteFunctionClosure($f_oop, $f_iip)) - - ex = quote - $_f - ImplicitDiscreteFunction{$iip}($fsym) - end - !linenumbers ? Base.remove_linenums!(ex) : ex -end - -function ImplicitDiscreteFunctionExpr(sys::ImplicitDiscreteSystem, args...; kwargs...) - ImplicitDiscreteFunctionExpr{true}(sys, args...; kwargs...) -end - -function Base.:(==)(sys1::ImplicitDiscreteSystem, sys2::ImplicitDiscreteSystem) - sys1 === sys2 && return true - isequal(nameof(sys1), nameof(sys2)) && - isequal(get_iv(sys1), get_iv(sys2)) && - _eq_unordered(get_eqs(sys1), get_eqs(sys2)) && - _eq_unordered(get_unknowns(sys1), get_unknowns(sys2)) && - _eq_unordered(get_ps(sys1), get_ps(sys2)) && - all(s1 == s2 for (s1, s2) in zip(get_systems(sys1), get_systems(sys2))) -end From 6e5801d73499ed034726d2f2cf365b1d45d08351 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 14 May 2025 12:10:25 +0530 Subject: [PATCH 1624/2176] remove jumpsystem.jl refactor: remove `jumpsystem.jl` --- src/ModelingToolkit.jl | 3 - src/systems/jumps/jumpsystem.jl | 665 -------------------------------- 2 files changed, 668 deletions(-) delete mode 100644 src/systems/jumps/jumpsystem.jl diff --git a/src/ModelingToolkit.jl b/src/ModelingToolkit.jl index 0a4e8a475b..caa483a2e2 100644 --- a/src/ModelingToolkit.jl +++ b/src/ModelingToolkit.jl @@ -178,8 +178,6 @@ include("systems/diffeqs/first_order_transform.jl") include("systems/diffeqs/modelingtoolkitize.jl") include("systems/diffeqs/basic_transformations.jl") -include("systems/jumps/jumpsystem.jl") - include("systems/pde/pdesystem.jl") include("systems/sparsematrixclil.jl") @@ -269,7 +267,6 @@ export SystemStructure export DiscreteProblem, DiscreteFunction, DiscreteFunctionExpr export ImplicitDiscreteProblem, ImplicitDiscreteFunction, ImplicitDiscreteFunctionExpr -export JumpSystem export ODEProblem, SDEProblem export NonlinearFunction, NonlinearFunctionExpr export NonlinearProblem, NonlinearProblemExpr diff --git a/src/systems/jumps/jumpsystem.jl b/src/systems/jumps/jumpsystem.jl deleted file mode 100644 index 348c103511..0000000000 --- a/src/systems/jumps/jumpsystem.jl +++ /dev/null @@ -1,665 +0,0 @@ -const JumpType = Union{VariableRateJump, ConstantRateJump, MassActionJump} - -""" -$(TYPEDEF) - -A system of jump processes. - -# Fields -$(FIELDS) - -# Example - -```julia -using ModelingToolkit, JumpProcesses -using ModelingToolkit: t_nounits as t - -@parameters β γ -@variables S(t) I(t) R(t) -rate₁ = β*S*I -affect₁ = [S ~ S - 1, I ~ I + 1] -rate₂ = γ*I -affect₂ = [I ~ I - 1, R ~ R + 1] -j₁ = ConstantRateJump(rate₁,affect₁) -j₂ = ConstantRateJump(rate₂,affect₂) -j₃ = MassActionJump(2*β+γ, [R => 1], [S => 1, R => -1]) -@named js = JumpSystem([j₁,j₂,j₃], t, [S,I,R], [β,γ]) -``` -""" -struct JumpSystem{U <: ArrayPartition} <: AbstractTimeDependentSystem - """ - A tag for the system. If two systems have the same tag, then they are - structurally identical. - """ - tag::UInt - """ - The jumps of the system. Allowable types are `ConstantRateJump`, - `VariableRateJump`, `MassActionJump`. - """ - eqs::U - """The independent variable, usually time.""" - iv::Any - """The dependent variables, representing the state of the system. Must not contain the independent variable.""" - unknowns::Vector - """The parameters of the system. Must not contain the independent variable.""" - ps::Vector - """Array variables.""" - var_to_name::Any - """Observed equations.""" - observed::Vector{Equation} - """The name of the system.""" - name::Symbol - """A description of the system.""" - description::String - """The internal systems. These are required to have unique names.""" - systems::Vector{JumpSystem} - """ - The default values to use when initial conditions and/or - parameters are not supplied in `ODEProblem`. - """ - defaults::Dict - """ - The guesses to use as the initial conditions for the - initialization system. - """ - guesses::Dict - """ - The system for performing the initialization. - """ - initializesystem::Union{Nothing, NonlinearSystem} - """ - Extra equations to be enforced during the initialization sequence. - """ - initialization_eqs::Vector{Equation} - """ - Type of the system. - """ - connector_type::Any - """ - A `Vector{SymbolicContinuousCallback}` that model events. - The integrator will use root finding to guarantee that it steps at each zero crossing. - """ - continuous_events::Vector{SymbolicContinuousCallback} - """ - A `Vector{SymbolicDiscreteCallback}` that models events. Symbolic - analog to `SciMLBase.DiscreteCallback` that executes an affect when a given condition is - true at the end of an integration step. Note, one must make sure to call - `reset_aggregated_jumps!(integrator)` if using a custom affect function that changes any - unknown value or parameter. - """ - discrete_events::Vector{SymbolicDiscreteCallback} - """ - Topologically sorted parameter dependency equations, where all symbols are parameters and - the LHS is a single parameter. - """ - parameter_dependencies::Vector{Equation} - """ - Metadata for the system, to be used by downstream packages. - """ - metadata::Any - """ - Metadata for MTK GUI. - """ - gui_metadata::Union{Nothing, GUIMetadata} - """ - If false, then `sys.x` no longer performs namespacing. - """ - namespacing::Bool - """ - If true, denotes the model will not be modified any further. - """ - complete::Bool - """ - Cached data for fast symbolic indexing. - """ - index_cache::Union{Nothing, IndexCache} - isscheduled::Bool - - function JumpSystem{U}( - tag, ap::U, iv, unknowns, ps, var_to_name, observed, name, description, - systems, defaults, guesses, initializesystem, initialization_eqs, connector_type, - cevents, devents, - parameter_dependencies, metadata = nothing, gui_metadata = nothing, - namespacing = true, complete = false, index_cache = nothing, isscheduled = false; - checks::Union{Bool, Int} = true) where {U <: ArrayPartition} - if checks == true || (checks & CheckComponents) > 0 - check_independent_variables([iv]) - check_variables(unknowns, iv) - check_parameters(ps, iv) - check_subsystems(systems) - end - if checks == true || (checks & CheckUnits) > 0 - u = __get_unit_type(unknowns, ps, iv) - check_units(u, ap, iv) - end - new{U}(tag, ap, iv, unknowns, ps, var_to_name, - observed, name, description, systems, defaults, guesses, initializesystem, - initialization_eqs, - connector_type, cevents, devents, parameter_dependencies, metadata, - gui_metadata, namespacing, complete, index_cache, isscheduled) - end -end -function JumpSystem(tag, ap, iv, states, ps, var_to_name, args...; kwargs...) - JumpSystem{typeof(ap)}(tag, ap, iv, states, ps, var_to_name, args...; kwargs...) -end - -function JumpSystem(eqs, iv, unknowns, ps; - observed = Equation[], - systems = JumpSystem[], - default_u0 = Dict(), - default_p = Dict(), - defaults = _merge(Dict(default_u0), Dict(default_p)), - guesses = Dict(), - initializesystem = nothing, - initialization_eqs = Equation[], - name = nothing, - description = "", - connector_type = nothing, - checks = true, - continuous_events = nothing, - discrete_events = nothing, - parameter_dependencies = Equation[], - metadata = nothing, - gui_metadata = nothing, - kwargs...) - - # variable processing, similar to ODESystem - name === nothing && - throw(ArgumentError("The `name` keyword must be provided. Please consider using the `@named` macro")) - iv′ = value(iv) - us′ = value.(unknowns) - ps′ = value.(ps) - parameter_dependencies, ps′ = process_parameter_dependencies( - parameter_dependencies, ps′) - if !(isempty(default_u0) && isempty(default_p)) - Base.depwarn( - "`default_u0` and `default_p` are deprecated. Use `defaults` instead.", - :JumpSystem, force = true) - end - defaults = Dict{Any, Any}(todict(defaults)) - guesses = Dict{Any, Any}(todict(guesses)) - var_to_name = Dict() - process_variables!(var_to_name, defaults, guesses, us′) - process_variables!(var_to_name, defaults, guesses, ps′) - process_variables!( - var_to_name, defaults, guesses, [eq.lhs for eq in parameter_dependencies]) - process_variables!( - var_to_name, defaults, guesses, [eq.rhs for eq in parameter_dependencies]) - #! format: off - defaults = Dict{Any, Any}(value(k) => value(v) for (k, v) in pairs(defaults) if value(v) !== nothing) - guesses = Dict{Any, Any}(value(k) => value(v) for (k, v) in pairs(guesses) if v !== nothing) - #! format: on - isempty(observed) || collect_var_to_name!(var_to_name, (eq.lhs for eq in observed)) - - sysnames = nameof.(systems) - if length(unique(sysnames)) != length(sysnames) - throw(ArgumentError("System names must be unique.")) - end - - # equation processing - # this and the treatment of continuous events are the only part - # unique to JumpSystems - eqs = scalarize.(eqs) - ap = ArrayPartition( - MassActionJump[], ConstantRateJump[], VariableRateJump[], Equation[]) - for eq in eqs - if eq isa MassActionJump - push!(ap.x[1], eq) - elseif eq isa ConstantRateJump - push!(ap.x[2], eq) - elseif eq isa VariableRateJump - push!(ap.x[3], eq) - elseif eq isa Equation - push!(ap.x[4], eq) - else - error("JumpSystem equations must contain MassActionJumps, ConstantRateJumps, VariableRateJumps, or Equations.") - end - end - - cont_callbacks = to_cb_vector(continuous_events; CB_TYPE = SymbolicContinuousCallback, - iv = iv, warn_no_algebraic = false) - disc_callbacks = to_cb_vector(discrete_events; CB_TYPE = SymbolicDiscreteCallback, - iv = iv, warn_no_algebraic = false) - - JumpSystem{typeof(ap)}(Threads.atomic_add!(SYSTEM_COUNT, UInt(1)), - ap, iv′, us′, ps′, var_to_name, observed, name, description, systems, - defaults, guesses, initializesystem, initialization_eqs, connector_type, - cont_callbacks, disc_callbacks, - parameter_dependencies, metadata, gui_metadata, checks = checks) -end - -##### MTK dispatches for JumpSystems ##### -eqtype_supports_collect_vars(j::MassActionJump) = true -function collect_vars!(unknowns, parameters, j::MassActionJump, iv; depth = 0, - op = Differential) - collect_vars!(unknowns, parameters, j.scaled_rates, iv; depth, op) - for field in (j.reactant_stoch, j.net_stoch) - for el in field - collect_vars!(unknowns, parameters, el, iv; depth, op) - end - end - return nothing -end - -eqtype_supports_collect_vars(j::Union{ConstantRateJump, VariableRateJump}) = true -function collect_vars!(unknowns, parameters, j::Union{ConstantRateJump, VariableRateJump}, - iv; depth = 0, op = Differential) - collect_vars!(unknowns, parameters, j.rate, iv; depth, op) - for eq in j.affect! - (eq isa Equation) && collect_vars!(unknowns, parameters, eq, iv; depth, op) - end - return nothing -end - -########################################## - -has_massactionjumps(js::JumpSystem) = !isempty(equations(js).x[1]) -has_constantratejumps(js::JumpSystem) = !isempty(equations(js).x[2]) -has_variableratejumps(js::JumpSystem) = !isempty(equations(js).x[3]) -has_equations(js::JumpSystem) = !isempty(equations(js).x[4]) - -function generate_rate_function(js::JumpSystem, rate) - consts = collect_constants(rate) - if !isempty(consts) # The SymbolicUtils._build_function method of this case doesn't support postprocess_fbody - csubs = Dict(c => getdefault(c) for c in consts) - rate = substitute(rate, csubs) - end - p = reorder_parameters(js) - build_function_wrapper(js, rate, unknowns(js), p..., - get_iv(js), - expression = Val{true}) -end - -function generate_affect_function(js::JumpSystem, affect) - consts = collect_constants(affect) - if !isempty(consts) # The SymbolicUtils._build_function method of this case doesn't support postprocess_fbody - csubs = Dict(c => getdefault(c) for c in consts) - affect = substitute(affect, csubs) - end - compile_equational_affect(affect, js; expression = Val{true}, checkvars = false) -end - -function assemble_vrj( - js, vrj, unknowntoid; eval_expression = false, eval_module = @__MODULE__) - rate = eval_or_rgf(generate_rate_function(js, vrj.rate); eval_expression, eval_module) - rate = GeneratedFunctionWrapper{(2, 3, is_split(js))}(rate, nothing) - outputvars = (value(affect.lhs) for affect in vrj.affect!) - outputidxs = [unknowntoid[var] for var in outputvars] - affect = generate_affect_function(js, vrj.affect!) - VariableRateJump(rate, affect; save_positions = vrj.save_positions) -end - -function assemble_vrj_expr(js, vrj, unknowntoid) - rate = generate_rate_function(js, vrj.rate) - outputvars = (value(affect.lhs) for affect in vrj.affect!) - outputidxs = ((unknowntoid[var] for var in outputvars)...,) - affect = generate_affect_function(js, vrj.affect!) - quote - rate = $rate - affect = $affect - VariableRateJump(rate, affect) - end -end - -function assemble_crj( - js, crj, unknowntoid; eval_expression = false, eval_module = @__MODULE__) - rate = eval_or_rgf(generate_rate_function(js, crj.rate); eval_expression, eval_module) - rate = GeneratedFunctionWrapper{(2, 3, is_split(js))}(rate, nothing) - outputvars = (value(affect.lhs) for affect in crj.affect!) - outputidxs = [unknowntoid[var] for var in outputvars] - affect = generate_affect_function(js, crj.affect!) - ConstantRateJump(rate, affect) -end - -function assemble_crj_expr(js, crj, unknowntoid) - rate = generate_rate_function(js, crj.rate) - outputvars = (value(affect.lhs) for affect in crj.affect!) - outputidxs = ((unknowntoid[var] for var in outputvars)...,) - affect = generate_affect_function(js, crj.affect!) - quote - rate = $rate - affect = $affect - ConstantRateJump(rate, affect) - end -end - -function numericrstoich(mtrs::Vector{Pair{V, W}}, unknowntoid) where {V, W} - rs = Vector{Pair{Int, W}}() - for (wspec, stoich) in mtrs - spec = value(wspec) - if !iscall(spec) && _iszero(spec) - push!(rs, 0 => stoich) - else - push!(rs, unknowntoid[spec] => stoich) - end - end - sort!(rs) - rs -end - -function numericnstoich(mtrs::Vector{Pair{V, W}}, unknowntoid) where {V, W} - ns = Vector{Pair{Int, W}}() - for (wspec, stoich) in mtrs - spec = value(wspec) - !iscall(spec) && _iszero(spec) && - error("Net stoichiometry can not have a species labelled 0.") - push!(ns, unknowntoid[spec] => stoich) - end - sort!(ns) -end - -# assemble a numeric MassActionJump from a MT symbolics MassActionJumps -function assemble_maj(majv::Vector{U}, unknowntoid, pmapper) where {U <: MassActionJump} - rs = [numericrstoich(maj.reactant_stoch, unknowntoid) for maj in majv] - ns = [numericnstoich(maj.net_stoch, unknowntoid) for maj in majv] - MassActionJump(rs, ns; param_mapper = pmapper, nocopy = true) -end - -""" -```julia -DiffEqBase.DiscreteProblem(sys::JumpSystem, u0map, tspan, - parammap = DiffEqBase.NullParameters; - kwargs...) -``` - -Generates a blank DiscreteProblem for a pure jump JumpSystem to utilize as -its `prob.prob`. This is used in the case where there are no ODEs -and no SDEs associated with the system. - -Continuing the example from the [`JumpSystem`](@ref) definition: - -```julia -using DiffEqBase, JumpProcesses -u₀map = [S => 999, I => 1, R => 0] -parammap = [β => 0.1 / 1000, γ => 0.01] -tspan = (0.0, 250.0) -dprob = DiscreteProblem(complete(js), u₀map, tspan, parammap) -``` -""" -function DiffEqBase.DiscreteProblem(sys::JumpSystem, u0map, tspan::Union{Tuple, Nothing}, - parammap = DiffEqBase.NullParameters(); - eval_expression = false, - eval_module = @__MODULE__, - cse = true, - kwargs...) - if !iscomplete(sys) - error("A completed `JumpSystem` is required. Call `complete` or `structural_simplify` on the system before creating a `DiscreteProblem`") - end - - if has_equations(sys) || (!isempty(continuous_events(sys))) - error("The passed in JumpSystem contains `Equation`s or continuous events, please use a problem type that supports these features, such as ODEProblem.") - end - - _f, u0, p = process_SciMLProblem(EmptySciMLFunction{true}, sys, u0map, parammap; - t = tspan === nothing ? nothing : tspan[1], tofloat = false, check_length = false, build_initializeprob = false, cse) - f = DiffEqBase.DISCRETE_INPLACE_DEFAULT - - observedfun = ObservedFunctionCache( - sys; eval_expression, eval_module, checkbounds = get(kwargs, :checkbounds, false), cse) - - df = DiscreteFunction{true, true}(f; sys = sys, observed = observedfun, - initialization_data = get(_f.kwargs, :initialization_data, nothing)) - DiscreteProblem(df, u0, tspan, p; kwargs...) -end - -""" -```julia -DiffEqBase.DiscreteProblemExpr(sys::JumpSystem, u0map, tspan, - parammap = DiffEqBase.NullParameters; kwargs...) -``` - -Generates a blank DiscreteProblem for a JumpSystem to utilize as its -solving `prob.prob`. This is used in the case where there are no ODEs -and no SDEs associated with the system. - -Continuing the example from the [`JumpSystem`](@ref) definition: - -```julia -using DiffEqBase, JumpProcesses -u₀map = [S => 999, I => 1, R => 0] -parammap = [β => 0.1 / 1000, γ => 0.01] -tspan = (0.0, 250.0) -dprob = DiscreteProblem(complete(js), u₀map, tspan, parammap) -``` -""" -struct DiscreteProblemExpr{iip} end - -function DiscreteProblemExpr{iip}(sys::JumpSystem, u0map, tspan::Union{Tuple, Nothing}, - parammap = DiffEqBase.NullParameters(); - 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{iip}, sys, u0map, parammap; - t = tspan === nothing ? nothing : tspan[1], tofloat = false, check_length = false) - # identity function to make syms works - quote - f = DiffEqBase.DISCRETE_INPLACE_DEFAULT - u0 = $u0 - p = $p - sys = $sys - tspan = $tspan - df = DiscreteFunction{true, true}(f; sys = sys) - DiscreteProblem(df, u0, tspan, p) - end -end - -""" -```julia -DiffEqBase.ODEProblem(sys::JumpSystem, u0map, tspan, - parammap = DiffEqBase.NullParameters; - kwargs...) -``` - -Generates a blank ODEProblem for a pure jump JumpSystem to utilize as its `prob.prob`. This -is used in the case where there are no ODEs and no SDEs associated with the system but there -are jumps with an explicit time dependency (i.e. `VariableRateJump`s). If no jumps have an -explicit time dependence, i.e. all are `ConstantRateJump`s or `MassActionJump`s then -`DiscreteProblem` should be preferred for performance reasons. - -Continuing the example from the [`JumpSystem`](@ref) definition: - -```julia -using DiffEqBase, JumpProcesses -u₀map = [S => 999, I => 1, R => 0] -parammap = [β => 0.1 / 1000, γ => 0.01] -tspan = (0.0, 250.0) -oprob = ODEProblem(complete(js), u₀map, tspan, parammap) -``` -""" -function DiffEqBase.ODEProblem(sys::JumpSystem, u0map, tspan::Union{Tuple, Nothing}, - parammap = DiffEqBase.NullParameters(); - eval_expression = false, - eval_module = @__MODULE__, cse = true, - kwargs...) - if !iscomplete(sys) - error("A completed `JumpSystem` is required. Call `complete` or `structural_simplify` on the system before creating a `DiscreteProblem`") - end - - # forward everything to be an ODESystem but the jumps and discrete events - if has_equations(sys) - osys = ODESystem(equations(sys).x[4], get_iv(sys), unknowns(sys), parameters(sys); - observed = observed(sys), name = nameof(sys), description = description(sys), - systems = get_systems(sys), defaults = defaults(sys), guesses = guesses(sys), - parameter_dependencies = parameter_dependencies(sys), - metadata = get_metadata(sys), gui_metadata = get_gui_metadata(sys)) - osys = complete(osys; add_initial_parameters = false) - return ODEProblem(osys, u0map, tspan, parammap; check_length = false, - build_initializeprob = false, kwargs...) - else - _, u0, p = process_SciMLProblem(EmptySciMLFunction{true}, sys, u0map, parammap; - t = tspan === nothing ? nothing : tspan[1], tofloat = false, - check_length = false, build_initializeprob = false, cse) - f = (du, u, p, t) -> (du .= 0; nothing) - observedfun = ObservedFunctionCache(sys; eval_expression, eval_module, - checkbounds = get(kwargs, :checkbounds, false), cse) - df = ODEFunction(f; sys, observed = observedfun) - return ODEProblem(df, u0, tspan, p; kwargs...) - end -end - -""" -```julia -DiffEqBase.JumpProblem(js::JumpSystem, prob, aggregator; kwargs...) -``` - -Generates a JumpProblem from a JumpSystem. - -Continuing the example from the [`DiscreteProblem`](@ref) definition: - -```julia -jprob = JumpProblem(complete(js), dprob, Direct()) -sol = solve(jprob, SSAStepper()) -``` -""" -function JumpProcesses.JumpProblem(js::JumpSystem, prob, - aggregator = JumpProcesses.NullAggregator(); callback = nothing, - eval_expression = false, eval_module = @__MODULE__, kwargs...) - if !iscomplete(js) - error("A completed `JumpSystem` is required. Call `complete` or `structural_simplify` on the system before creating a `JumpProblem`") - end - unknowntoid = Dict(value(unknown) => i for (i, unknown) in enumerate(unknowns(js))) - eqs = equations(js) - invttype = prob.tspan[1] === nothing ? Float64 : typeof(1 / prob.tspan[2]) - - # handling parameter substitution and empty param vecs - p = (prob.p isa DiffEqBase.NullParameters || prob.p === nothing) ? Num[] : prob.p - - majpmapper = JumpSysMajParamMapper(js, p; jseqs = eqs, rateconsttype = invttype) - majs = isempty(eqs.x[1]) ? nothing : assemble_maj(eqs.x[1], unknowntoid, majpmapper) - crjs = ConstantRateJump[assemble_crj(js, j, unknowntoid; eval_expression, eval_module) - for j in eqs.x[2]] - vrjs = VariableRateJump[assemble_vrj(js, j, unknowntoid; eval_expression, eval_module) - for j in eqs.x[3]] - if prob isa DiscreteProblem - if (!isempty(vrjs) || has_equations(js) || !isempty(continuous_events(js))) - error("Use continuous problems such as an ODEProblem or a SDEProblem with VariableRateJumps, coupled differential equations, or continuous events.") - end - end - jset = JumpSet(Tuple(vrjs), Tuple(crjs), nothing, majs) - - # dep graphs are only for constant rate jumps - nonvrjs = ArrayPartition(eqs.x[1], eqs.x[2]) - if needs_vartojumps_map(aggregator) || needs_depgraph(aggregator) || - (aggregator isa JumpProcesses.NullAggregator) - jdeps = asgraph(js; eqs = nonvrjs) - vdeps = variable_dependencies(js; eqs = nonvrjs) - vtoj = jdeps.badjlist - jtov = vdeps.badjlist - jtoj = needs_depgraph(aggregator) ? eqeq_dependencies(jdeps, vdeps).fadjlist : - nothing - else - vtoj = nothing - jtov = nothing - jtoj = nothing - end - - # handle events, making sure to reset aggregators in the generated affect functions - cbs = process_events(js; callback, eval_expression, eval_module, reset_jumps = true) - - JumpProblem(prob, aggregator, jset; dep_graph = jtoj, vartojumps_map = vtoj, - jumptovars_map = jtov, scale_rates = false, nocopy = true, - callback = cbs, kwargs...) -end - -### Functions to determine which unknowns a jump depends on -function get_variables!(dep, jump::Union{ConstantRateJump, VariableRateJump}, variables) - jr = value(jump.rate) - (jr isa Symbolic) && get_variables!(dep, jr, variables) - dep -end - -function get_variables!(dep, jump::MassActionJump, variables) - sr = value(jump.scaled_rates) - (sr isa Symbolic) && get_variables!(dep, sr, variables) - for varasop in jump.reactant_stoch - any(isequal(varasop[1]), variables) && push!(dep, varasop[1]) - end - dep -end - -### Functions to determine which unknowns are modified by a given jump -function modified_unknowns!(munknowns, jump::Union{ConstantRateJump, VariableRateJump}, sts) - for eq in jump.affect! - st = eq.lhs - any(isequal(st), sts) && push!(munknowns, st) - end - munknowns -end - -function modified_unknowns!(munknowns, jump::MassActionJump, sts) - for (unknown, stoich) in jump.net_stoch - any(isequal(unknown), sts) && push!(munknowns, unknown) - end - munknowns -end - -###################### parameter mapper ########################### -struct JumpSysMajParamMapper{U, V, W} - paramexprs::U # the parameter expressions to use for each jump rate constant - sympars::V # parameters(sys) from the underlying JumpSystem - subdict::Any # mapping from an element of parameters(sys) to its current numerical value -end - -function JumpSysMajParamMapper(js::JumpSystem, p; jseqs = nothing, rateconsttype = Float64) - eqs = (jseqs === nothing) ? equations(js) : jseqs - paramexprs = [maj.scaled_rates for maj in eqs.x[1]] - psyms = reduce(vcat, reorder_parameters(js); init = []) - paramdict = Dict(value(k) => value(v) for (k, v) in zip(psyms, vcat(p...))) - JumpSysMajParamMapper{typeof(paramexprs), typeof(psyms), rateconsttype}(paramexprs, - psyms, - paramdict) -end - -function updateparams!(ratemap::JumpSysMajParamMapper{U, V, W}, - params) where {U <: AbstractArray, V <: AbstractArray, W} - for (i, p) in enumerate(params) - sympar = ratemap.sympars[i] - ratemap.subdict[sympar] = p - end - nothing -end - -function updateparams!(ratemap::JumpSysMajParamMapper{U, V, W}, - params::MTKParameters) where {U <: AbstractArray, V <: AbstractArray, W} - for (i, p) in enumerate(ArrayPartition(params...)) - sympar = ratemap.sympars[i] - ratemap.subdict[sympar] = p - end - nothing -end - -function updateparams!(::JumpSysMajParamMapper{U, V, W}, - params::Nothing) where {U <: AbstractArray, V <: AbstractArray, W} - nothing -end - -# create the initial parameter vector for use in a MassActionJump -function (ratemap::JumpSysMajParamMapper{ - U, - V, - W -})(params) where {U <: AbstractArray, - V <: AbstractArray, W} - updateparams!(ratemap, params) - [convert(W, value(substitute(paramexpr, ratemap.subdict))) - for paramexpr in ratemap.paramexprs] -end - -# update a maj with parameter vectors -function (ratemap::JumpSysMajParamMapper{U, V, W})(maj::MassActionJump, newparams; - scale_rates, - kwargs...) where {U <: AbstractArray, - V <: AbstractArray, W} - updateparams!(ratemap, newparams) - for i in 1:get_num_majumps(maj) - maj.scaled_rates[i] = convert(W, - value(substitute(ratemap.paramexprs[i], - ratemap.subdict))) - end - scale_rates && JumpProcesses.scalerates!(maj.scaled_rates, maj.reactant_stoch) - nothing -end - -supports_initialization(::JumpSystem) = false From 23093ae24507156cc4f2cf1d6b6041f3447229b2 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 22 Apr 2025 16:54:28 +0530 Subject: [PATCH 1625/2176] refactor: move `JumpType` definition to `utils.jl` --- src/utils.jl | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/utils.jl b/src/utils.jl index 8f5ac311c5..c3d736f92f 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -1370,3 +1370,5 @@ function flatten_equations(eqs::Vector{Equation}) end end end + +const JumpType = Union{VariableRateJump, ConstantRateJump, MassActionJump} From 5b4e80dd52ee32e876b1b563f99fe8964e648f61 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Sun, 20 Apr 2025 21:52:37 +0530 Subject: [PATCH 1626/2176] refactor: remove `nonlinearsystem.jl` --- src/ModelingToolkit.jl | 3 +- src/systems/nonlinear/nonlinearsystem.jl | 993 ----------------------- 2 files changed, 1 insertion(+), 995 deletions(-) delete mode 100644 src/systems/nonlinear/nonlinearsystem.jl diff --git a/src/ModelingToolkit.jl b/src/ModelingToolkit.jl index caa483a2e2..66fda07b8c 100644 --- a/src/ModelingToolkit.jl +++ b/src/ModelingToolkit.jl @@ -169,7 +169,6 @@ include("systems/optimization/constraints_system.jl") include("systems/optimization/optimizationsystem.jl") include("systems/optimization/modelingtoolkitize.jl") -include("systems/nonlinear/nonlinearsystem.jl") include("systems/diffeqs/abstractodesystem.jl") include("systems/nonlinear/homotopy_continuation.jl") include("systems/nonlinear/modelingtoolkitize.jl") @@ -275,7 +274,7 @@ export IntervalNonlinearProblem, IntervalNonlinearProblemExpr export OptimizationProblem, OptimizationProblemExpr, constraints export SteadyStateProblem, SteadyStateProblemExpr export JumpProblem -export NonlinearSystem, OptimizationSystem, ConstraintsSystem +export OptimizationSystem, ConstraintsSystem export alias_elimination, flatten export connect, domain_connect, @connector, Connection, AnalysisPoint, Flow, Stream, instream diff --git a/src/systems/nonlinear/nonlinearsystem.jl b/src/systems/nonlinear/nonlinearsystem.jl deleted file mode 100644 index cf1c46207f..0000000000 --- a/src/systems/nonlinear/nonlinearsystem.jl +++ /dev/null @@ -1,993 +0,0 @@ -""" -$(TYPEDEF) - -A nonlinear system of equations. - -# Fields -$(FIELDS) - -# Examples - -```julia -@variables x y z -@parameters σ ρ β - -eqs = [0 ~ σ*(y-x), - 0 ~ x*(ρ-z)-y, - 0 ~ x*y - β*z] -@named ns = NonlinearSystem(eqs, [x,y,z],[σ,ρ,β]) -``` -""" -struct NonlinearSystem <: AbstractTimeIndependentSystem - """ - A tag for the system. If two systems have the same tag, then they are - structurally identical. - """ - tag::UInt - """Vector of equations defining the system.""" - eqs::Vector{Equation} - """Unknown variables.""" - unknowns::Vector - """Parameters.""" - ps::Vector - """Array variables.""" - var_to_name::Any - """Observed equations.""" - observed::Vector{Equation} - """ - Jacobian matrix. Note: this field will not be defined until - [`calculate_jacobian`](@ref) is called on the system. - """ - jac::RefValue{Any} - """ - The name of the system. - """ - name::Symbol - """ - A description of the system. - """ - description::String - """ - The internal systems. These are required to have unique names. - """ - systems::Vector{NonlinearSystem} - """ - The default values to use when initial conditions and/or - parameters are not supplied in `ODEProblem`. - """ - defaults::Dict - """ - The guesses to use as the initial conditions for the - initialization system. - """ - guesses::Dict - """ - The system for performing the initialization. - """ - initializesystem::Union{Nothing, NonlinearSystem} - """ - Extra equations to be enforced during the initialization sequence. - """ - initialization_eqs::Vector{Equation} - """ - Type of the system. - """ - connector_type::Any - """ - Topologically sorted parameter dependency equations, where all symbols are parameters and - the LHS is a single parameter. - """ - parameter_dependencies::Vector{Equation} - """ - Metadata for the system, to be used by downstream packages. - """ - metadata::Any - """ - Metadata for MTK GUI. - """ - gui_metadata::Union{Nothing, GUIMetadata} - """ - Whether this is an initialization system. - """ - is_initializesystem::Bool - """ - Cache for intermediate tearing state. - """ - tearing_state::Any - """ - Substitutions generated by tearing. - """ - substitutions::Any - """ - If false, then `sys.x` no longer performs namespacing. - """ - namespacing::Bool - """ - If true, denotes the model will not be modified any further. - """ - complete::Bool - """ - Cached data for fast symbolic indexing. - """ - index_cache::Union{Nothing, IndexCache} - """ - The hierarchical parent system before simplification. - """ - parent::Any - isscheduled::Bool - - function NonlinearSystem( - tag, eqs, unknowns, ps, var_to_name, observed, jac, name, description, - systems, defaults, guesses, initializesystem, initialization_eqs, connector_type, - parameter_dependencies = Equation[], metadata = nothing, gui_metadata = nothing, - is_initializesystem = false, - tearing_state = nothing, substitutions = nothing, namespacing = true, - complete = false, index_cache = nothing, parent = nothing, - isscheduled = false; checks::Union{Bool, Int} = true) - if checks == true || (checks & CheckUnits) > 0 - u = __get_unit_type(unknowns, ps) - check_units(u, eqs) - check_subsystems(systems) - end - new(tag, eqs, unknowns, ps, var_to_name, observed, jac, name, description, - systems, defaults, guesses, initializesystem, initialization_eqs, - connector_type, parameter_dependencies, metadata, gui_metadata, - is_initializesystem, tearing_state, - substitutions, namespacing, complete, index_cache, parent, isscheduled) - end -end - -function NonlinearSystem(eqs, unknowns, ps; - observed = [], - name = nothing, - description = "", - default_u0 = Dict(), - default_p = Dict(), - defaults = _merge(Dict(default_u0), Dict(default_p)), - guesses = Dict(), - initializesystem = nothing, - initialization_eqs = Equation[], - systems = NonlinearSystem[], - connector_type = nothing, - continuous_events = nothing, # this argument is only required for ODESystems, but is added here for the constructor to accept it without error - discrete_events = nothing, # this argument is only required for ODESystems, but is added here for the constructor to accept it without error - checks = true, - parameter_dependencies = Equation[], - metadata = nothing, - gui_metadata = nothing, - is_initializesystem = false) - continuous_events === nothing || isempty(continuous_events) || - throw(ArgumentError("NonlinearSystem does not accept `continuous_events`, you provided $continuous_events")) - discrete_events === nothing || isempty(discrete_events) || - throw(ArgumentError("NonlinearSystem does not accept `discrete_events`, you provided $discrete_events")) - name === nothing && - throw(ArgumentError("The `name` keyword must be provided. Please consider using the `@named` macro")) - length(unique(nameof.(systems))) == length(systems) || - throw(ArgumentError("System names must be unique.")) - (isempty(default_u0) && isempty(default_p)) || - Base.depwarn( - "`default_u0` and `default_p` are deprecated. Use `defaults` instead.", - :NonlinearSystem, force = true) - - # Accept a single (scalar/vector) equation, but make array for consistent internal handling - if !(eqs isa AbstractArray) - eqs = [eqs] - end - - # Copy equations to canonical form, but do not touch array expressions - eqs = [wrap(eq.lhs) isa Symbolics.Arr ? eq : 0 ~ eq.rhs - eq.lhs for eq in eqs] - - jac = RefValue{Any}(EMPTY_JAC) - - ps′ = value.(ps) - dvs′ = value.(unknowns) - parameter_dependencies, ps′ = process_parameter_dependencies( - parameter_dependencies, ps′) - - defaults = Dict{Any, Any}(todict(defaults)) - guesses = Dict{Any, Any}(todict(guesses)) - var_to_name = Dict() - process_variables!(var_to_name, defaults, guesses, dvs′) - process_variables!(var_to_name, defaults, guesses, ps′) - process_variables!( - var_to_name, defaults, guesses, [eq.lhs for eq in parameter_dependencies]) - process_variables!( - var_to_name, defaults, guesses, [eq.rhs for eq in parameter_dependencies]) - defaults = Dict{Any, Any}(value(k) => value(v) - for (k, v) in pairs(defaults) if v !== nothing) - guesses = Dict{Any, Any}(value(k) => value(v) - for (k, v) in pairs(guesses) if v !== nothing) - - isempty(observed) || collect_var_to_name!(var_to_name, (eq.lhs for eq in observed)) - - NonlinearSystem(Threads.atomic_add!(SYSTEM_COUNT, UInt(1)), - eqs, dvs′, ps′, var_to_name, observed, jac, name, description, systems, defaults, - guesses, initializesystem, initialization_eqs, connector_type, parameter_dependencies, - metadata, gui_metadata, is_initializesystem, checks = checks) -end - -function NonlinearSystem(eqs; kwargs...) - eqs = collect(eqs) - allunknowns = OrderedSet() - ps = OrderedSet() - for eq in eqs - collect_vars!(allunknowns, ps, eq, nothing) - end - for eq in get(kwargs, :parameter_dependencies, Equation[]) - if eq isa Pair - collect_vars!(allunknowns, ps, eq, nothing) - else - collect_vars!(allunknowns, ps, eq, nothing) - end - end - new_ps = OrderedSet() - for p in ps - if iscall(p) && operation(p) === getindex - par = arguments(p)[begin] - if Symbolics.shape(Symbolics.unwrap(par)) !== Symbolics.Unknown() && - all(par[i] in ps for i in eachindex(par)) - push!(new_ps, par) - else - push!(new_ps, p) - end - else - if symbolic_type(p) == ArraySymbolic() && - Symbolics.shape(unwrap(p)) != Symbolics.Unknown() - for i in eachindex(p) - delete!(new_ps, p[i]) - end - end - push!(new_ps, p) - end - end - - return NonlinearSystem(eqs, collect(allunknowns), collect(new_ps); kwargs...) -end - -""" - $(TYPEDSIGNATURES) - -Convert an `ODESystem` to a `NonlinearSystem` solving for its steady state (where derivatives are zero). -Any differential variable `D(x) ~ f(...)` will be turned into `0 ~ f(...)`. The returned system is not -simplified. If the input system is `complete`d, then so will the returned system. -""" -function NonlinearSystem(sys::AbstractODESystem) - eqs = equations(sys) - obs = observed(sys) - subrules = Dict(D(x) => 0.0 for x in unknowns(sys)) - eqs = map(eqs) do eq - fast_substitute(eq, subrules) - end - - nsys = NonlinearSystem(eqs, unknowns(sys), [parameters(sys); get_iv(sys)]; - parameter_dependencies = parameter_dependencies(sys), - defaults = merge(defaults(sys), Dict(get_iv(sys) => Inf)), guesses = guesses(sys), - initialization_eqs = initialization_equations(sys), name = nameof(sys), - observed = obs) - if iscomplete(sys) - nsys = complete(nsys; split = is_split(sys)) - end - return nsys -end - -function calculate_jacobian(sys::NonlinearSystem; sparse = false, simplify = false) - cache = get_jac(sys)[] - if cache isa Tuple && cache[2] == (sparse, simplify) - return cache[1] - end - - # observed equations may depend on unknowns, so substitute them in first - # TODO: rather keep observed derivatives unexpanded, like "Differential(obs)(expr)"? - obs = Dict(eq.lhs => eq.rhs for eq in observed(sys)) - rhs = map(eq -> fixpoint_sub(eq.rhs, obs), equations(sys)) - vals = [dv for dv in unknowns(sys)] - - if sparse - jac = sparsejacobian(rhs, vals, simplify = simplify) - else - jac = jacobian(rhs, vals, simplify = simplify) - end - get_jac(sys)[] = jac, (sparse, simplify) - return jac -end - -function generate_jacobian( - sys::NonlinearSystem, vs = unknowns(sys), ps = parameters( - sys; initial_parameters = true); - sparse = false, simplify = false, kwargs...) - jac = calculate_jacobian(sys, sparse = sparse, simplify = simplify) - p = reorder_parameters(sys, ps) - return build_function_wrapper(sys, jac, vs, p...; kwargs...) -end - -function calculate_hessian(sys::NonlinearSystem; sparse = false, simplify = false) - obs = Dict(eq.lhs => eq.rhs for eq in observed(sys)) - rhs = map(eq -> fixpoint_sub(eq.rhs, obs), equations(sys)) - vals = [dv for dv in unknowns(sys)] - if sparse - hess = [sparsehessian(rhs[i], vals, simplify = simplify) for i in 1:length(rhs)] - else - hess = [hessian(rhs[i], vals, simplify = simplify) for i in 1:length(rhs)] - end - return hess -end - -function generate_hessian( - sys::NonlinearSystem, vs = unknowns(sys), ps = parameters( - sys; initial_parameters = true); - sparse = false, simplify = false, kwargs...) - hess = calculate_hessian(sys, sparse = sparse, simplify = simplify) - p = reorder_parameters(sys, ps) - return build_function_wrapper(sys, hess, vs, p...; kwargs...) -end - -function generate_function( - sys::NonlinearSystem, dvs = unknowns(sys), ps = parameters( - sys; initial_parameters = true); - scalar = false, kwargs...) - rhss = [deq.rhs for deq in equations(sys)] - dvs′ = value.(dvs) - if scalar - rhss = only(rhss) - dvs′ = only(dvs) - end - p = reorder_parameters(sys, value.(ps)) - return build_function_wrapper(sys, rhss, dvs′, p...; kwargs...) -end - -function jacobian_sparsity(sys::NonlinearSystem) - jacobian_sparsity([eq.rhs for eq in equations(sys)], - unknowns(sys)) -end - -function hessian_sparsity(sys::NonlinearSystem) - [hessian_sparsity(eq.rhs, - unknowns(sys)) for eq in equations(sys)] -end - -""" -```julia -SciMLBase.NonlinearFunction{iip}(sys::NonlinearSystem, dvs = unknowns(sys), - ps = parameters(sys); - version = nothing, - jac = false, - sparse = false, - kwargs...) where {iip} -``` - -Create a `NonlinearFunction` from the [`NonlinearSystem`](@ref). The arguments -`dvs` and `ps` are used to set the order of the dependent variable and parameter -vectors, respectively. -""" -function SciMLBase.NonlinearFunction(sys::NonlinearSystem, args...; kwargs...) - NonlinearFunction{true}(sys, args...; kwargs...) -end - -function SciMLBase.NonlinearFunction{iip}(sys::NonlinearSystem, dvs = unknowns(sys), - ps = parameters(sys), u0 = nothing; p = nothing, - version = nothing, - jac = false, - eval_expression = false, - eval_module = @__MODULE__, - sparse = false, simplify = false, - initialization_data = nothing, cse = true, - resid_prototype = nothing, - kwargs...) where {iip} - if !iscomplete(sys) - error("A completed `NonlinearSystem` is required. Call `complete` or `structural_simplify` on the system before creating a `NonlinearFunction`") - end - f_gen = generate_function(sys, dvs, ps; expression = Val{true}, cse, kwargs...) - f_oop, f_iip = eval_or_rgf.(f_gen; eval_expression, eval_module) - f = GeneratedFunctionWrapper{(2, 2, is_split(sys))}(f_oop, f_iip) - - if jac - jac_gen = generate_jacobian(sys, dvs, ps; - simplify = simplify, sparse = sparse, - expression = Val{true}, cse, kwargs...) - jac_oop, jac_iip = eval_or_rgf.(jac_gen; eval_expression, eval_module) - _jac = GeneratedFunctionWrapper{(2, 2, is_split(sys))}(jac_oop, jac_iip) - else - _jac = nothing - end - - observedfun = ObservedFunctionCache( - sys; eval_expression, eval_module, checkbounds = get(kwargs, :checkbounds, false), cse) - - NonlinearFunction{iip}(f; - sys = sys, - jac = _jac === nothing ? nothing : _jac, - resid_prototype = resid_prototype, - jac_prototype = sparse ? - similar(calculate_jacobian(sys, sparse = sparse), - Float64) : nothing, - observed = observedfun, initialization_data) -end - -""" -$(TYPEDSIGNATURES) - -Create an `IntervalNonlinearFunction` from the [`NonlinearSystem`](@ref). The arguments -`dvs` and `ps` are used to set the order of the dependent variable and parameter vectors, -respectively. -""" -function SciMLBase.IntervalNonlinearFunction( - sys::NonlinearSystem, dvs = unknowns(sys), ps = parameters(sys), u0 = nothing; - p = nothing, eval_expression = false, eval_module = @__MODULE__, - initialization_data = nothing, kwargs...) - if !iscomplete(sys) - error("A completed `NonlinearSystem` is required. Call `complete` or `structural_simplify` on the system before creating a `IntervalNonlinearFunction`") - end - if !isone(length(dvs)) || !isone(length(equations(sys))) - error("`IntervalNonlinearFunction` only supports systems with a single equation and a single unknown.") - end - - f_gen = generate_function( - sys, dvs, ps; expression = Val{true}, scalar = true, kwargs...) - f = eval_or_rgf(f_gen; eval_expression, eval_module) - f = GeneratedFunctionWrapper{(2, 2, is_split(sys))}(f, nothing) - - observedfun = ObservedFunctionCache( - sys; eval_expression, eval_module, checkbounds = get(kwargs, :checkbounds, false)) - - IntervalNonlinearFunction{false}( - f; observed = observedfun, sys = sys, initialization_data) -end - -""" -```julia -SciMLBase.NonlinearFunctionExpr{iip}(sys::NonlinearSystem, dvs = unknowns(sys), - ps = parameters(sys); - version = nothing, - jac = false, - sparse = false, - kwargs...) where {iip} -``` - -Create a Julia expression for a `NonlinearFunction` from the [`NonlinearSystem`](@ref). -The arguments `dvs` and `ps` are used to set the order of the dependent -variable and parameter vectors, respectively. -""" -struct NonlinearFunctionExpr{iip} end - -function NonlinearFunctionExpr{iip}(sys::NonlinearSystem, dvs = unknowns(sys), - ps = parameters(sys), u0 = nothing; p = nothing, - version = nothing, tgrad = false, - jac = false, - linenumbers = false, - sparse = false, simplify = false, - kwargs...) where {iip} - if !iscomplete(sys) - error("A completed `NonlinearSystem` is required. Call `complete` or `structural_simplify` on the system before creating a `NonlinearFunctionExpr`") - end - f_oop, f_iip = generate_function(sys, dvs, ps; expression = Val{true}, kwargs...) - f = :($(GeneratedFunctionWrapper{(2, 2, is_split(sys))})($f_oop, $f_iip)) - - if jac - jac_oop, jac_iip = generate_jacobian(sys, dvs, ps; - sparse = sparse, simplify = simplify, - expression = Val{true}, kwargs...) - _jac = :($(GeneratedFunctionWrapper{(2, 2, is_split(sys))})($jac_oop, $jac_iip)) - else - _jac = :nothing - end - - jp_expr = sparse ? :(similar($(get_jac(sys)[]), Float64)) : :nothing - if length(dvs) == length(equations(sys)) - resid_expr = :nothing - else - u0ElType = u0 === nothing ? Float64 : eltype(u0) - if SciMLStructures.isscimlstructure(p) - u0ElType = promote_type( - eltype(SciMLStructures.canonicalize(SciMLStructures.Tunable(), p)[1]), - u0ElType) - end - - resid_expr = :(zeros($u0ElType, $(length(equations(sys))))) - end - ex = quote - f = $f - jac = $_jac - NonlinearFunction{$iip}(f, - jac = jac, - resid_prototype = resid_expr, - jac_prototype = $jp_expr) - end - !linenumbers ? Base.remove_linenums!(ex) : ex -end - -struct IntervalNonlinearFunctionExpr end - -""" -$(TYPEDSIGNATURES) - -Create a Julia expression for an `IntervalNonlinearFunction` from the -[`NonlinearSystem`](@ref). The arguments `dvs` and `ps` are used to set the order of the -dependent variable and parameter vectors, respectively. -""" -function IntervalNonlinearFunctionExpr( - sys::NonlinearSystem, dvs = unknowns(sys), ps = parameters(sys), - u0 = nothing; p = nothing, linenumbers = false, kwargs...) - if !iscomplete(sys) - error("A completed `NonlinearSystem` is required. Call `complete` or `structural_simplify` on the system before creating a `IntervalNonlinearFunctionExpr`") - end - if !isone(length(dvs)) || !isone(length(equations(sys))) - error("`IntervalNonlinearFunctionExpr` only supports systems with a single equation and a single unknown.") - end - - f = generate_function(sys, dvs, ps; expression = Val{true}, scalar = true, kwargs...) - f = :($(GeneratedFunctionWrapper{2, 2, is_split(sys)})($f, nothing)) - - ex = quote - f = $f - NonlinearFunction{false}(f) - end - !linenumbers ? Base.remove_linenums!(ex) : ex -end - -""" -```julia -DiffEqBase.NonlinearProblem{iip}(sys::NonlinearSystem, u0map, - parammap = DiffEqBase.NullParameters(); - jac = false, sparse = false, - checkbounds = false, - linenumbers = true, parallel = SerialForm(), - kwargs...) where {iip} -``` - -Generates an NonlinearProblem from a NonlinearSystem and allows for automatically -symbolically calculating numerical enhancements. -""" -function DiffEqBase.NonlinearProblem(sys::NonlinearSystem, args...; kwargs...) - NonlinearProblem{true}(sys, args...; kwargs...) -end - -function DiffEqBase.NonlinearProblem{iip}(sys::NonlinearSystem, u0map, - parammap = DiffEqBase.NullParameters(); - check_length = true, kwargs...) where {iip} - if !iscomplete(sys) - error("A completed `NonlinearSystem` is required. Call `complete` or `structural_simplify` on the system before creating a `NonlinearProblem`") - end - f, u0, p = process_SciMLProblem(NonlinearFunction{iip}, sys, u0map, parammap; - check_length, kwargs...) - pt = something(get_metadata(sys), StandardNonlinearProblem()) - # Call `remake` so it runs initialization if it is trivial - return remake(NonlinearProblem{iip}(f, u0, p, pt; filter_kwargs(kwargs)...)) -end - -function DiffEqBase.NonlinearProblem(sys::AbstractODESystem, args...; kwargs...) - NonlinearProblem(NonlinearSystem(sys), args...; kwargs...) -end - -""" -```julia -DiffEqBase.NonlinearLeastSquaresProblem{iip}(sys::NonlinearSystem, u0map, - parammap = DiffEqBase.NullParameters(); - jac = false, sparse = false, - checkbounds = false, - linenumbers = true, parallel = SerialForm(), - kwargs...) where {iip} -``` - -Generates an NonlinearProblem from a NonlinearSystem and allows for automatically -symbolically calculating numerical enhancements. -""" -function DiffEqBase.NonlinearLeastSquaresProblem(sys::NonlinearSystem, args...; kwargs...) - NonlinearLeastSquaresProblem{true}(sys, args...; kwargs...) -end - -function DiffEqBase.NonlinearLeastSquaresProblem{iip}(sys::NonlinearSystem, u0map, - parammap = DiffEqBase.NullParameters(); - check_length = false, kwargs...) where {iip} - if !iscomplete(sys) - error("A completed `NonlinearSystem` is required. Call `complete` or `structural_simplify` on the system before creating a `NonlinearLeastSquaresProblem`") - end - f, u0, p = process_SciMLProblem(NonlinearFunction{iip}, sys, u0map, parammap; - check_length, kwargs...) - pt = something(get_metadata(sys), StandardNonlinearProblem()) - # Call `remake` so it runs initialization if it is trivial - return remake(NonlinearLeastSquaresProblem{iip}(f, u0, p; filter_kwargs(kwargs)...)) -end - -const TypeT = Union{DataType, UnionAll} - -struct CacheWriter{F} - fn::F -end - -function (cw::CacheWriter)(p, sols) - cw.fn(p.caches, sols, p) -end - -function CacheWriter(sys::AbstractSystem, buffer_types::Vector{TypeT}, - exprs::Dict{TypeT, Vector{Any}}, solsyms, obseqs::Vector{Equation}; - eval_expression = false, eval_module = @__MODULE__, cse = true) - ps = parameters(sys; initial_parameters = true) - rps = reorder_parameters(sys, ps) - obs_assigns = [eq.lhs ← eq.rhs for eq in obseqs] - body = map(eachindex(buffer_types), buffer_types) do i, T - Symbol(:tmp, i) ← SetArray(true, :(out[$i]), get(exprs, T, [])) - end - - function argument_name(i::Int) - if i <= length(solsyms) - return :($(generated_argument_name(1))[$i]) - end - return generated_argument_name(i - length(solsyms)) - end - array_assignments = array_variable_assignments(solsyms...; argument_name) - fn = build_function_wrapper( - sys, nothing, :out, - DestructuredArgs(DestructuredArgs.(solsyms), generated_argument_name(1)), - rps...; p_start = 3, p_end = length(rps) + 2, - expression = Val{true}, add_observed = false, cse, - extra_assignments = [array_assignments; obs_assigns; body]) - fn = eval_or_rgf(fn; eval_expression, eval_module) - fn = GeneratedFunctionWrapper{(3, 3, is_split(sys))}(fn, nothing) - return CacheWriter(fn) -end - -struct SCCNonlinearFunction{iip} end - -function SCCNonlinearFunction{iip}( - sys::NonlinearSystem, _eqs, _dvs, _obs, cachesyms; eval_expression = false, - eval_module = @__MODULE__, cse = true, kwargs...) where {iip} - ps = parameters(sys; initial_parameters = true) - rps = reorder_parameters(sys, ps) - - obs_assignments = [eq.lhs ← eq.rhs for eq in _obs] - - rhss = [eq.rhs - eq.lhs for eq in _eqs] - f_gen = build_function_wrapper(sys, - rhss, _dvs, rps..., cachesyms...; p_start = 2, - p_end = length(rps) + length(cachesyms) + 1, add_observed = false, - extra_assignments = obs_assignments, expression = Val{true}, cse) - f_oop, f_iip = eval_or_rgf.(f_gen; eval_expression, eval_module) - f = GeneratedFunctionWrapper{(2, 2, is_split(sys))}(f_oop, f_iip) - - subsys = NonlinearSystem(_eqs, _dvs, ps; observed = _obs, - parameter_dependencies = parameter_dependencies(sys), name = nameof(sys)) - if get_index_cache(sys) !== nothing - @set! subsys.index_cache = subset_unknowns_observed( - get_index_cache(sys), sys, _dvs, getproperty.(_obs, (:lhs,))) - @set! subsys.complete = true - end - - return NonlinearFunction{iip}(f; sys = subsys) -end - -function SciMLBase.SCCNonlinearProblem(sys::NonlinearSystem, args...; kwargs...) - SCCNonlinearProblem{true}(sys, args...; kwargs...) -end - -function SciMLBase.SCCNonlinearProblem{iip}(sys::NonlinearSystem, u0map, - parammap = SciMLBase.NullParameters(); eval_expression = false, eval_module = @__MODULE__, - cse = true, kwargs...) where {iip} - if !iscomplete(sys) || get_tearing_state(sys) === nothing - error("A simplified `NonlinearSystem` is required. Call `structural_simplify` on the system before creating an `SCCNonlinearProblem`.") - end - - if !is_split(sys) - error("The system has been simplified with `split = false`. `SCCNonlinearProblem` is not compatible with this system. Pass `split = true` to `structural_simplify` to use `SCCNonlinearProblem`.") - end - - ts = get_tearing_state(sys) - var_eq_matching, var_sccs = StructuralTransformations.algebraic_variables_scc(ts) - - if length(var_sccs) == 1 - return NonlinearProblem{iip}( - sys, u0map, parammap; eval_expression, eval_module, kwargs...) - end - - condensed_graph = MatchedCondensationGraph( - DiCMOBiGraph{true}(complete(ts.structure.graph), - complete(var_eq_matching)), - var_sccs) - toporder = topological_sort_by_dfs(condensed_graph) - var_sccs = var_sccs[toporder] - eq_sccs = map(Base.Fix1(getindex, var_eq_matching), var_sccs) - - dvs = unknowns(sys) - ps = parameters(sys) - eqs = equations(sys) - obs = observed(sys) - - _, u0, p = process_SciMLProblem( - EmptySciMLFunction{iip}, sys, u0map, parammap; eval_expression, eval_module, kwargs...) - - explicitfuns = [] - nlfuns = [] - prevobsidxs = BlockArray(undef_blocks, Vector{Int}, Int[]) - # Cache buffer types and corresponding sizes. Stored as a pair of arrays instead of a - # dict to maintain a consistent order of buffers across SCCs - cachetypes = TypeT[] - cachesizes = Int[] - # explicitfun! related information for each SCC - # We need to compute buffer sizes before doing any codegen - scc_cachevars = Dict{TypeT, Vector{Any}}[] - scc_cacheexprs = Dict{TypeT, Vector{Any}}[] - scc_eqs = Vector{Equation}[] - scc_obs = Vector{Equation}[] - # variables solved in previous SCCs - available_vars = Set() - for (i, (escc, vscc)) in enumerate(zip(eq_sccs, var_sccs)) - # subset unknowns and equations - _dvs = dvs[vscc] - _eqs = eqs[escc] - # get observed equations required by this SCC - union!(available_vars, _dvs) - obsidxs = observed_equations_used_by(sys, _eqs; available_vars) - # the ones used by previous SCCs can be precomputed into the cache - setdiff!(obsidxs, prevobsidxs) - _obs = obs[obsidxs] - union!(available_vars, getproperty.(_obs, (:lhs,))) - - # get all subexpressions in the RHS which we can precompute in the cache - # precomputed subexpressions should not contain `banned_vars` - banned_vars = Set{Any}(vcat(_dvs, getproperty.(_obs, (:lhs,)))) - state = Dict() - for i in eachindex(_obs) - _obs[i] = _obs[i].lhs ~ subexpressions_not_involving_vars!( - _obs[i].rhs, banned_vars, state) - end - for i in eachindex(_eqs) - _eqs[i] = _eqs[i].lhs ~ subexpressions_not_involving_vars!( - _eqs[i].rhs, banned_vars, state) - end - - # map from symtype to cached variables and their expressions - cachevars = Dict{Union{DataType, UnionAll}, Vector{Any}}() - cacheexprs = Dict{Union{DataType, UnionAll}, Vector{Any}}() - # observed of previous SCCs are in the cache - # NOTE: When we get proper CSE, we can substitute these - # and then use `subexpressions_not_involving_vars!` - for i in prevobsidxs - T = symtype(obs[i].lhs) - buf = get!(() -> Any[], cachevars, T) - push!(buf, obs[i].lhs) - - buf = get!(() -> Any[], cacheexprs, T) - push!(buf, obs[i].lhs) - end - - for (k, v) in state - k = unwrap(k) - v = unwrap(v) - T = symtype(k) - buf = get!(() -> Any[], cachevars, T) - push!(buf, v) - buf = get!(() -> Any[], cacheexprs, T) - push!(buf, k) - end - - # update the sizes of cache buffers - for (T, buf) in cachevars - idx = findfirst(isequal(T), cachetypes) - if idx === nothing - push!(cachetypes, T) - push!(cachesizes, 0) - idx = lastindex(cachetypes) - end - cachesizes[idx] = max(cachesizes[idx], length(buf)) - end - - push!(scc_cachevars, cachevars) - push!(scc_cacheexprs, cacheexprs) - push!(scc_eqs, _eqs) - push!(scc_obs, _obs) - blockpush!(prevobsidxs, obsidxs) - end - - for (i, (escc, vscc)) in enumerate(zip(eq_sccs, var_sccs)) - _dvs = dvs[vscc] - _eqs = scc_eqs[i] - _prevobsidxs = reduce(vcat, blocks(prevobsidxs)[1:(i - 1)]; init = Int[]) - _obs = scc_obs[i] - cachevars = scc_cachevars[i] - cacheexprs = scc_cacheexprs[i] - available_vars = [dvs[reduce(vcat, var_sccs[1:(i - 1)]; init = Int[])]; - getproperty.( - reduce(vcat, scc_obs[1:(i - 1)]; init = []), (:lhs,))] - _prevobsidxs = vcat(_prevobsidxs, - observed_equations_used_by( - sys, reduce(vcat, values(cacheexprs); init = []); available_vars)) - if isempty(cachevars) - push!(explicitfuns, Returns(nothing)) - else - solsyms = getindex.((dvs,), view(var_sccs, 1:(i - 1))) - push!(explicitfuns, - CacheWriter(sys, cachetypes, cacheexprs, solsyms, obs[_prevobsidxs]; - eval_expression, eval_module, cse)) - end - - cachebufsyms = Tuple(map(cachetypes) do T - get(cachevars, T, []) - end) - f = SCCNonlinearFunction{iip}( - sys, _eqs, _dvs, _obs, cachebufsyms; eval_expression, eval_module, cse, kwargs...) - push!(nlfuns, f) - end - - if !isempty(cachetypes) - templates = map(cachetypes, cachesizes) do T, n - # Real refers to `eltype(u0)` - if T == Real - T = eltype(u0) - elseif T <: Array && eltype(T) == Real - T = Array{eltype(u0), ndims(T)} - end - BufferTemplate(T, n) - end - p = rebuild_with_caches(p, templates...) - end - - subprobs = [] - for (f, vscc) in zip(nlfuns, var_sccs) - _u0 = SymbolicUtils.Code.create_array( - typeof(u0), eltype(u0), Val(1), Val(length(vscc)), u0[vscc]...) - prob = NonlinearProblem{iip}(f, _u0, p) - push!(subprobs, prob) - end - - new_dvs = dvs[reduce(vcat, var_sccs)] - new_eqs = eqs[reduce(vcat, eq_sccs)] - @set! sys.unknowns = new_dvs - @set! sys.eqs = new_eqs - @set! sys.index_cache = subset_unknowns_observed( - get_index_cache(sys), sys, new_dvs, getproperty.(obs, (:lhs,))) - return SCCNonlinearProblem(subprobs, explicitfuns, p, true; sys) -end - -""" -$(TYPEDSIGNATURES) - -Generate an `IntervalNonlinearProblem` from a `NonlinearSystem` and allow for automatically -symbolically calculating numerical enhancements. -""" -function DiffEqBase.IntervalNonlinearProblem(sys::NonlinearSystem, uspan::NTuple{2}, - parammap = SciMLBase.NullParameters(); kwargs...) - if !iscomplete(sys) - error("A completed `NonlinearSystem` is required. Call `complete` or `structural_simplify` on the system before creating a `IntervalNonlinearProblem`") - end - if !isone(length(unknowns(sys))) || !isone(length(equations(sys))) - error("`IntervalNonlinearProblem` only supports with a single equation and a single unknown.") - end - f, u0, p = process_SciMLProblem( - IntervalNonlinearFunction, sys, unknowns(sys) .=> uspan[1], parammap; kwargs...) - - return IntervalNonlinearProblem(f, uspan, p; filter_kwargs(kwargs)...) -end - -""" -```julia -DiffEqBase.NonlinearProblemExpr{iip}(sys::NonlinearSystem, u0map, - parammap = DiffEqBase.NullParameters(); - jac = false, sparse = false, - checkbounds = false, - linenumbers = true, parallel = SerialForm(), - kwargs...) where {iip} -``` - -Generates a Julia expression for a NonlinearProblem from a -NonlinearSystem and allows for automatically symbolically calculating -numerical enhancements. -""" -struct NonlinearProblemExpr{iip} end - -function NonlinearProblemExpr(sys::NonlinearSystem, args...; kwargs...) - NonlinearProblemExpr{true}(sys, args...; kwargs...) -end - -function NonlinearProblemExpr{iip}(sys::NonlinearSystem, u0map, - parammap = DiffEqBase.NullParameters(); - check_length = true, - kwargs...) where {iip} - if !iscomplete(sys) - error("A completed `NonlinearSystem` is required. Call `complete` or `structural_simplify` on the system before creating a `NonlinearProblemExpr`") - end - f, u0, p = process_SciMLProblem(NonlinearFunctionExpr{iip}, sys, u0map, parammap; - check_length, kwargs...) - linenumbers = get(kwargs, :linenumbers, true) - - ex = quote - f = $f - u0 = $u0 - p = $p - NonlinearProblem(f, u0, p; $(filter_kwargs(kwargs)...)) - end - !linenumbers ? Base.remove_linenums!(ex) : ex -end - -""" -```julia -DiffEqBase.NonlinearLeastSquaresProblemExpr{iip}(sys::NonlinearSystem, u0map, - parammap = DiffEqBase.NullParameters(); - jac = false, sparse = false, - checkbounds = false, - linenumbers = true, parallel = SerialForm(), - kwargs...) where {iip} -``` - -Generates a Julia expression for a NonlinearProblem from a -NonlinearSystem and allows for automatically symbolically calculating -numerical enhancements. -""" -struct NonlinearLeastSquaresProblemExpr{iip} end - -function NonlinearLeastSquaresProblemExpr(sys::NonlinearSystem, args...; kwargs...) - NonlinearLeastSquaresProblemExpr{true}(sys, args...; kwargs...) -end - -function NonlinearLeastSquaresProblemExpr{iip}(sys::NonlinearSystem, u0map, - parammap = DiffEqBase.NullParameters(); - check_length = false, - kwargs...) where {iip} - if !iscomplete(sys) - error("A completed `NonlinearSystem` is required. Call `complete` or `structural_simplify` on the system before creating a `NonlinearProblemExpr`") - end - f, u0, p = process_SciMLProblem(NonlinearFunctionExpr{iip}, sys, u0map, parammap; - check_length, kwargs...) - linenumbers = get(kwargs, :linenumbers, true) - - ex = quote - f = $f - u0 = $u0 - p = $p - NonlinearLeastSquaresProblem(f, u0, p; $(filter_kwargs(kwargs)...)) - end - !linenumbers ? Base.remove_linenums!(ex) : ex -end - -""" -$(TYPEDSIGNATURES) - -Generates a Julia expression for an IntervalNonlinearProblem from a -NonlinearSystem and allows for automatically symbolically calculating -numerical enhancements. -""" -function IntervalNonlinearProblemExpr(sys::NonlinearSystem, uspan::NTuple{2}, - parammap = SciMLBase.NullParameters(); kwargs...) - if !iscomplete(sys) - error("A completed `NonlinearSystem` is required. Call `complete` or `structural_simplify` on the system before creating a `IntervalNonlinearProblemExpr`") - end - if !isone(length(unknowns(sys))) || !isone(length(equations(sys))) - error("`IntervalNonlinearProblemExpr` only supports with a single equation and a single unknown.") - end - f, u0, p = process_SciMLProblem( - IntervalNonlinearFunctionExpr, sys, unknowns(sys) .=> uspan[1], parammap; kwargs...) - linenumbers = get(kwargs, :linenumbers, true) - - ex = quote - f = $f - uspan = $uspan - p = $p - IntervalNonlinearProblem(f, uspan, p; $(filter_kwargs(kwargs)...)) - end - !linenumbers ? Base.remove_linenums!(ex) : ex -end - -function flatten(sys::NonlinearSystem, noeqs = false) - systems = get_systems(sys) - if isempty(systems) - return sys - else - return NonlinearSystem(noeqs ? Equation[] : equations(sys), - unknowns(sys), - parameters(sys), - observed = observed(sys), - defaults = defaults(sys), - guesses = guesses(sys), - initialization_eqs = initialization_equations(sys), - name = nameof(sys), - description = description(sys), - metadata = get_metadata(sys), - checks = false) - end -end - -function Base.:(==)(sys1::NonlinearSystem, sys2::NonlinearSystem) - isequal(nameof(sys1), nameof(sys2)) && - _eq_unordered(get_eqs(sys1), get_eqs(sys2)) && - _eq_unordered(get_unknowns(sys1), get_unknowns(sys2)) && - _eq_unordered(get_ps(sys1), get_ps(sys2)) && - all(s1 == s2 for (s1, s2) in zip(get_systems(sys1), get_systems(sys2))) -end From a58d3d3fa7800b803ce9dfac7a51af9db227b4f6 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 21 Apr 2025 10:50:14 +0530 Subject: [PATCH 1627/2176] refactor: remove `optimizationsystem.jl` --- src/ModelingToolkit.jl | 3 +- .../optimization/optimizationsystem.jl | 760 ------------------ 2 files changed, 1 insertion(+), 762 deletions(-) delete mode 100644 src/systems/optimization/optimizationsystem.jl diff --git a/src/ModelingToolkit.jl b/src/ModelingToolkit.jl index 66fda07b8c..1bdb215ff0 100644 --- a/src/ModelingToolkit.jl +++ b/src/ModelingToolkit.jl @@ -166,7 +166,6 @@ include("systems/problem_utils.jl") include("linearization.jl") include("systems/optimization/constraints_system.jl") -include("systems/optimization/optimizationsystem.jl") include("systems/optimization/modelingtoolkitize.jl") include("systems/diffeqs/abstractodesystem.jl") @@ -274,7 +273,7 @@ export IntervalNonlinearProblem, IntervalNonlinearProblemExpr export OptimizationProblem, OptimizationProblemExpr, constraints export SteadyStateProblem, SteadyStateProblemExpr export JumpProblem -export OptimizationSystem, ConstraintsSystem +export ConstraintsSystem export alias_elimination, flatten export connect, domain_connect, @connector, Connection, AnalysisPoint, Flow, Stream, instream diff --git a/src/systems/optimization/optimizationsystem.jl b/src/systems/optimization/optimizationsystem.jl deleted file mode 100644 index bfe15b62d7..0000000000 --- a/src/systems/optimization/optimizationsystem.jl +++ /dev/null @@ -1,760 +0,0 @@ -""" -$(TYPEDEF) - -A scalar equation for optimization. - -# Fields -$(FIELDS) - -# Examples - -```julia -@variables x y z -@parameters a b c - -obj = a * (y - x) + x * (b - z) - y + x * y - c * z -cons = [x^2 + y^2 ≲ 1] -@named os = OptimizationSystem(obj, [x, y, z], [a, b, c]; constraints = cons) -``` -""" -struct OptimizationSystem <: AbstractOptimizationSystem - """ - A tag for the system. If two systems have the same tag, then they are - structurally identical. - """ - tag::UInt - """Objective function of the system.""" - op::Any - """Unknown variables.""" - unknowns::Array - """Parameters.""" - ps::Vector - """Array variables.""" - var_to_name::Any - """Observed equations.""" - observed::Vector{Equation} - """List of constraint equations of the system.""" - constraints::Vector{Union{Equation, Inequality}} - """The name of the system.""" - name::Symbol - """A description of the system.""" - description::String - """The internal systems. These are required to have unique names.""" - systems::Vector{OptimizationSystem} - """ - The default values to use when initial guess and/or - parameters are not supplied in `OptimizationProblem`. - """ - defaults::Dict - """ - Metadata for the system, to be used by downstream packages. - """ - metadata::Any - """ - Metadata for MTK GUI. - """ - gui_metadata::Union{Nothing, GUIMetadata} - """ - If false, then `sys.x` no longer performs namespacing. - """ - namespacing::Bool - """ - If true, denotes the model will not be modified any further. - """ - complete::Bool - """ - Cached data for fast symbolic indexing. - """ - index_cache::Union{Nothing, IndexCache} - """ - The hierarchical parent system before simplification. - """ - parent::Any - isscheduled::Bool - - function OptimizationSystem(tag, op, unknowns, ps, var_to_name, observed, - constraints, name, description, systems, defaults, metadata = nothing, - gui_metadata = nothing, namespacing = true, complete = false, - index_cache = nothing, parent = nothing, isscheduled = false; - checks::Union{Bool, Int} = true) - if checks == true || (checks & CheckUnits) > 0 - u = __get_unit_type(unknowns, ps) - unwrap(op) isa Symbolic && check_units(u, op) - check_units(u, observed) - check_units(u, constraints) - check_subsystems(systems) - end - new(tag, op, unknowns, ps, var_to_name, observed, - constraints, name, description, systems, defaults, metadata, gui_metadata, - namespacing, complete, index_cache, parent, isscheduled) - end -end - -equations(sys::AbstractOptimizationSystem) = objective(sys) # needed for Base.show - -function OptimizationSystem(op, unknowns, ps; - observed = [], - constraints = [], - default_u0 = Dict(), - default_p = Dict(), - defaults = _merge(Dict(default_u0), Dict(default_p)), - name = nothing, - description = "", - systems = OptimizationSystem[], - checks = true, - metadata = nothing, - gui_metadata = nothing) - name === nothing && - throw(ArgumentError("The `name` keyword must be provided. Please consider using the `@named` macro")) - constraints = value.(reduce(vcat, scalarize(constraints); init = [])) - unknowns′ = value.(reduce(vcat, scalarize(unknowns); init = [])) - ps′ = value.(ps) - op′ = value(scalarize(op)) - - irreducible_subs = Dict() - for i in eachindex(unknowns′) - var = unknowns′[i] - if hasbounds(var) - irreducible_subs[var] = irrvar = setirreducible(var, true) - unknowns′[i] = irrvar - end - end - op′ = fast_substitute(op′, irreducible_subs) - constraints = fast_substitute.(constraints, (irreducible_subs,)) - - if !(isempty(default_u0) && isempty(default_p)) - Base.depwarn( - "`default_u0` and `default_p` are deprecated. Use `defaults` instead.", - :OptimizationSystem, force = true) - end - sysnames = nameof.(systems) - if length(unique(sysnames)) != length(sysnames) - throw(ArgumentError("System names must be unique.")) - end - defaults = todict(defaults) - defaults = Dict(fast_substitute(value(k), irreducible_subs) => fast_substitute( - value(v), irreducible_subs) - for (k, v) in pairs(defaults) if value(v) !== nothing) - - var_to_name = Dict() - process_variables!(var_to_name, defaults, Dict(), unknowns′) - process_variables!(var_to_name, defaults, Dict(), ps′) - isempty(observed) || collect_var_to_name!(var_to_name, (eq.lhs for eq in observed)) - - OptimizationSystem(Threads.atomic_add!(SYSTEM_COUNT, UInt(1)), - op′, unknowns′, ps′, var_to_name, - observed, - constraints, - name, description, systems, defaults, metadata, gui_metadata; - checks = checks) -end - -function OptimizationSystem(objective; constraints = [], kwargs...) - allunknowns = OrderedSet() - ps = OrderedSet() - collect_vars!(allunknowns, ps, objective, nothing) - for cons in constraints - collect_vars!(allunknowns, ps, cons, nothing) - end - for ssys in get(kwargs, :systems, OptimizationSystem[]) - collect_scoped_vars!(allunknowns, ps, ssys, nothing) - end - new_ps = OrderedSet() - for p in ps - if iscall(p) && operation(p) === getindex - par = arguments(p)[begin] - if Symbolics.shape(Symbolics.unwrap(par)) !== Symbolics.Unknown() && - all(par[i] in ps for i in eachindex(par)) - push!(new_ps, par) - else - push!(new_ps, p) - end - else - push!(new_ps, p) - end - end - return OptimizationSystem( - objective, collect(allunknowns), collect(new_ps); constraints, kwargs...) -end - -function flatten(sys::OptimizationSystem) - systems = get_systems(sys) - isempty(systems) && return sys - - return OptimizationSystem( - objective(sys), - unknowns(sys), - parameters(sys); - observed = observed(sys), - constraints = constraints(sys), - defaults = defaults(sys), - name = nameof(sys), - metadata = get_metadata(sys), - checks = false - ) -end - -function calculate_gradient(sys::OptimizationSystem) - expand_derivatives.(gradient(objective(sys), unknowns(sys))) -end - -function generate_gradient(sys::OptimizationSystem, vs = unknowns(sys), - ps = parameters(sys; initial_parameters = true); kwargs...) - grad = calculate_gradient(sys) - p = reorder_parameters(sys, ps) - return build_function_wrapper(sys, grad, vs, p...; kwargs...) -end - -function calculate_hessian(sys::OptimizationSystem) - expand_derivatives.(hessian(objective(sys), unknowns(sys))) -end - -function generate_hessian( - sys::OptimizationSystem, vs = unknowns(sys), ps = parameters( - sys; initial_parameters = true); - sparse = false, kwargs...) - if sparse - hess = sparsehessian(objective(sys), unknowns(sys)) - else - hess = calculate_hessian(sys) - end - p = reorder_parameters(sys, ps) - return build_function_wrapper(sys, hess, vs, p...; kwargs...) -end - -function generate_function(sys::OptimizationSystem, vs = unknowns(sys), - ps = parameters(sys; initial_parameters = true); - kwargs...) - eqs = objective(sys) - p = reorder_parameters(sys, ps) - return build_function_wrapper(sys, eqs, vs, p...; kwargs...) -end - -function namespace_objective(sys::AbstractSystem) - op = objective(sys) - namespace_expr(op, sys) -end - -function objective(sys) - op = get_op(sys) - systems = get_systems(sys) - if isempty(systems) - op - else - op + reduce(+, map(sys_ -> namespace_objective(sys_), systems)) - end -end - -namespace_constraint(eq::Equation, sys) = namespace_equation(eq, sys) - -namespace_constraint(ineq::Inequality, sys) = namespace_inequality(ineq, sys) - -function namespace_inequality(ineq::Inequality, sys, n = nameof(sys)) - _lhs = namespace_expr(ineq.lhs, sys, n) - _rhs = namespace_expr(ineq.rhs, sys, n) - Inequality(_lhs, - _rhs, - ineq.relational_op) -end - -function namespace_constraints(sys) - cstrs = constraints(sys) - isempty(cstrs) && return Vector{Union{Equation, Inequality}}(undef, 0) - map(cstr -> namespace_constraint(cstr, sys), cstrs) -end - -function constraints(sys) - cs = get_constraints(sys) - systems = get_systems(sys) - isempty(systems) ? cs : [cs; reduce(vcat, namespace_constraints.(systems))] -end - -hessian_sparsity(sys::OptimizationSystem) = hessian_sparsity(get_op(sys), unknowns(sys)) - -""" -```julia -DiffEqBase.OptimizationProblem{iip}(sys::OptimizationSystem, u0map, - parammap = DiffEqBase.NullParameters(); - grad = false, - hess = false, sparse = false, - cons_j = false, cons_h = false, - checkbounds = false, - linenumbers = true, parallel = SerialForm(), - kwargs...) where {iip} -``` - -Generates an OptimizationProblem from an OptimizationSystem and allows for automatically -symbolically calculating numerical enhancements. - -Certain solvers require setting `cons_j`, `cons_h` to `true` for constrained-optimization problems. -""" -function DiffEqBase.OptimizationProblem(sys::OptimizationSystem, args...; kwargs...) - DiffEqBase.OptimizationProblem{true}(sys::OptimizationSystem, args...; kwargs...) -end -function DiffEqBase.OptimizationProblem{iip}(sys::OptimizationSystem, u0map, - parammap = DiffEqBase.NullParameters(); - lb = nothing, ub = nothing, - grad = false, - hess = false, sparse = false, - cons_j = false, cons_h = false, - cons_sparse = false, checkbounds = false, - linenumbers = true, parallel = SerialForm(), - eval_expression = false, eval_module = @__MODULE__, - checks = true, cse = true, - kwargs...) where {iip} - if !iscomplete(sys) - error("A completed `OptimizationSystem` is required. Call `complete` or `structural_simplify` on the system before creating a `OptimizationProblem`") - end - if haskey(kwargs, :lcons) || haskey(kwargs, :ucons) - Base.depwarn( - "`lcons` and `ucons` are deprecated. Specify constraints directly instead.", - :OptimizationProblem, force = true) - end - - dvs = unknowns(sys) - ps = parameters(sys) - cstr = constraints(sys) - - if isnothing(lb) && isnothing(ub) # use the symbolically specified bounds - lb = first.(getbounds.(dvs)) - ub = last.(getbounds.(dvs)) - isboolean = symtype.(unwrap.(dvs)) .<: Bool - lb[isboolean] .= 0 - ub[isboolean] .= 1 - else # use the user supplied variable bounds - xor(isnothing(lb), isnothing(ub)) && - throw(ArgumentError("Expected both `lb` and `ub` to be supplied")) - !isnothing(lb) && length(lb) != length(dvs) && - throw(ArgumentError("Expected both `lb` to be of the same length as the vector of optimization variables")) - !isnothing(ub) && length(ub) != length(dvs) && - throw(ArgumentError("Expected both `ub` to be of the same length as the vector of optimization variables")) - end - - int = symtype.(unwrap.(dvs)) .<: Integer - - defs = defaults(sys) - defs = mergedefaults(defs, parammap, ps) - defs = mergedefaults(defs, u0map, dvs) - - u0 = varmap_to_vars(u0map, dvs; defaults = defs, tofloat = false) - if parammap isa MTKParameters - p = parammap - 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) - end - 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 - ub = nothing - end - - f = let _f = eval_or_rgf( - generate_function( - sys; checkbounds = checkbounds, linenumbers = linenumbers, - expression = Val{true}, wrap_mtkparameters = false, cse); - eval_expression, - eval_module) - __f(u, p) = _f(u, p) - __f(u, p::MTKParameters) = _f(u, p...) - __f - end - obj_expr = subs_constants(objective(sys)) - - if grad - _grad = let (grad_oop, grad_iip) = eval_or_rgf.( - generate_gradient( - sys; checkbounds = checkbounds, - linenumbers = linenumbers, - parallel = parallel, expression = Val{true}, - wrap_mtkparameters = false, cse); - eval_expression, - eval_module) - _grad(u, p) = grad_oop(u, p) - _grad(J, u, p) = (grad_iip(J, u, p); J) - _grad(u, p::MTKParameters) = grad_oop(u, p...) - _grad(J, u, p::MTKParameters) = (grad_iip(J, u, p...); J) - _grad - end - else - _grad = nothing - end - - if hess - _hess = let (hess_oop, hess_iip) = eval_or_rgf.( - generate_hessian( - sys; checkbounds = checkbounds, - linenumbers = linenumbers, - sparse = sparse, parallel = parallel, - expression = Val{true}, wrap_mtkparameters = false, cse); - eval_expression, - eval_module) - _hess(u, p) = hess_oop(u, p) - _hess(J, u, p) = (hess_iip(J, u, p); J) - _hess(u, p::MTKParameters) = hess_oop(u, p...) - _hess(J, u, p::MTKParameters) = (hess_iip(J, u, p...); J) - _hess - end - else - _hess = nothing - end - - if sparse - hess_prototype = hessian_sparsity(sys) - else - hess_prototype = nothing - end - - observedfun = ObservedFunctionCache(sys; eval_expression, eval_module, checkbounds, cse) - - if length(cstr) > 0 - @named cons_sys = ConstraintsSystem(cstr, dvs, ps; checks) - cons_sys = complete(cons_sys) - cons, lcons_, ucons_ = generate_function(cons_sys; checkbounds = checkbounds, - linenumbers = linenumbers, - expression = Val{true}, wrap_mtkparameters = false, cse) - cons = let (cons_oop, cons_iip) = eval_or_rgf.(cons; eval_expression, eval_module) - _cons(u, p) = cons_oop(u, p) - _cons(resid, u, p) = cons_iip(resid, u, p) - _cons(u, p::MTKParameters) = cons_oop(u, p...) - _cons(resid, u, p::MTKParameters) = cons_iip(resid, u, p...) - end - if cons_j - _cons_j = let (cons_jac_oop, cons_jac_iip) = eval_or_rgf.( - generate_jacobian(cons_sys; - checkbounds = checkbounds, - linenumbers = linenumbers, - parallel = parallel, expression = Val{true}, - sparse = cons_sparse, wrap_mtkparameters = false, cse); - eval_expression, - eval_module) - _cons_j(u, p) = cons_jac_oop(u, p) - _cons_j(J, u, p) = (cons_jac_iip(J, u, p); J) - _cons_j(u, p::MTKParameters) = cons_jac_oop(u, p...) - _cons_j(J, u, p::MTKParameters) = (cons_jac_iip(J, u, p...); J) - _cons_j - end - else - _cons_j = nothing - end - if cons_h - _cons_h = let (cons_hess_oop, cons_hess_iip) = eval_or_rgf.( - generate_hessian( - cons_sys; checkbounds = checkbounds, - linenumbers = linenumbers, - sparse = cons_sparse, parallel = parallel, - expression = Val{true}, wrap_mtkparameters = false, cse); - eval_expression, - eval_module) - _cons_h(u, p) = cons_hess_oop(u, p) - _cons_h(J, u, p) = (cons_hess_iip(J, u, p); J) - _cons_h(u, p::MTKParameters) = cons_hess_oop(u, p...) - _cons_h(J, u, p::MTKParameters) = (cons_hess_iip(J, u, p...); J) - _cons_h - end - else - _cons_h = nothing - end - cons_expr = subs_constants(constraints(cons_sys)) - - if !haskey(kwargs, :lcons) && !haskey(kwargs, :ucons) # use the symbolically specified bounds - lcons = lcons_ - ucons = ucons_ - else # use the user supplied constraints bounds - (haskey(kwargs, :lcons) ⊻ haskey(kwargs, :ucons)) && - throw(ArgumentError("Expected both `ucons` and `lcons` to be supplied")) - haskey(kwargs, :lcons) && length(kwargs[:lcons]) != length(cstr) && - throw(ArgumentError("Expected `lcons` to be of the same length as the vector of constraints")) - haskey(kwargs, :ucons) && length(kwargs[:ucons]) != length(cstr) && - throw(ArgumentError("Expected `ucons` to be of the same length as the vector of constraints")) - lcons = haskey(kwargs, :lcons) - ucons = haskey(kwargs, :ucons) - end - - if cons_sparse - cons_jac_prototype = jacobian_sparsity(cons_sys) - cons_hess_prototype = hessian_sparsity(cons_sys) - else - cons_jac_prototype = nothing - cons_hess_prototype = nothing - end - _f = DiffEqBase.OptimizationFunction{iip}(f, - sys = sys, - SciMLBase.NoAD(); - grad = _grad, - hess = _hess, - hess_prototype = hess_prototype, - cons = cons, - cons_j = _cons_j, - cons_h = _cons_h, - cons_jac_prototype = cons_jac_prototype, - cons_hess_prototype = cons_hess_prototype, - expr = obj_expr, - cons_expr = cons_expr, - observed = observedfun) - OptimizationProblem{iip}(_f, u0, p; lb = lb, ub = ub, int = int, - lcons = lcons, ucons = ucons, kwargs...) - else - _f = DiffEqBase.OptimizationFunction{iip}(f, - sys = sys, - SciMLBase.NoAD(); - grad = _grad, - hess = _hess, - hess_prototype = hess_prototype, - expr = obj_expr, - observed = observedfun) - OptimizationProblem{iip}(_f, u0, p; lb = lb, ub = ub, int = int, - kwargs...) - end -end - -""" -```julia -DiffEqBase.OptimizationProblemExpr{iip}(sys::OptimizationSystem, - parammap = DiffEqBase.NullParameters(); - u0 = nothing, - grad = false, - hes = false, sparse = false, - checkbounds = false, - linenumbers = true, parallel = SerialForm(), - kwargs...) where {iip} -``` - -Generates a Julia expression for an OptimizationProblem from an -OptimizationSystem and allows for automatically symbolically -calculating numerical enhancements. -""" -struct OptimizationProblemExpr{iip} end - -function OptimizationProblemExpr(sys::OptimizationSystem, args...; kwargs...) - OptimizationProblemExpr{true}(sys::OptimizationSystem, args...; kwargs...) -end - -function OptimizationProblemExpr{iip}(sys::OptimizationSystem, u0map, - parammap = DiffEqBase.NullParameters(); - lb = nothing, ub = nothing, - grad = false, - hess = false, sparse = false, - cons_j = false, cons_h = false, - checkbounds = false, - linenumbers = false, parallel = SerialForm(), - eval_expression = false, eval_module = @__MODULE__, - kwargs...) where {iip} - if !iscomplete(sys) - error("A completed `OptimizationSystem` is required. Call `complete` or `structural_simplify` on the system before creating a `OptimizationProblemExpr`") - end - if haskey(kwargs, :lcons) || haskey(kwargs, :ucons) - Base.depwarn( - "`lcons` and `ucons` are deprecated. Specify constraints directly instead.", - :OptimizationProblem, force = true) - end - - dvs = unknowns(sys) - ps = parameters(sys) - cstr = constraints(sys) - - if isnothing(lb) && isnothing(ub) # use the symbolically specified bounds - lb = first.(getbounds.(dvs)) - ub = last.(getbounds.(dvs)) - isboolean = symtype.(unwrap.(dvs)) .<: Bool - lb[isboolean] .= 0 - ub[isboolean] .= 1 - else # use the user supplied variable bounds - xor(isnothing(lb), isnothing(ub)) && - throw(ArgumentError("Expected both `lb` and `ub` to be supplied")) - !isnothing(lb) && length(lb) != length(dvs) && - throw(ArgumentError("Expected `lb` to be of the same length as the vector of optimization variables")) - !isnothing(ub) && length(ub) != length(dvs) && - throw(ArgumentError("Expected `ub` to be of the same length as the vector of optimization variables")) - end - - int = symtype.(unwrap.(dvs)) .<: Integer - - defs = defaults(sys) - defs = mergedefaults(defs, parammap, ps) - defs = mergedefaults(defs, u0map, dvs) - - u0 = varmap_to_vars(u0map, dvs; defaults = defs, tofloat = false) - 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) - end - 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 - ub = nothing - end - - idx = iip ? 2 : 1 - f = generate_function(sys, checkbounds = checkbounds, linenumbers = linenumbers, - expression = Val{true}) - if grad - _grad = eval_or_rgf( - generate_gradient( - sys, checkbounds = checkbounds, linenumbers = linenumbers, - parallel = parallel, expression = Val{true})[idx]; - eval_expression, - eval_module) - else - _grad = :nothing - end - - if hess - _hess = eval_or_rgf( - generate_hessian(sys, checkbounds = checkbounds, linenumbers = linenumbers, - sparse = sparse, parallel = parallel, - expression = Val{false})[idx]; - eval_expression, - eval_module) - else - _hess = :nothing - end - - if sparse - hess_prototype = hessian_sparsity(sys) - else - hess_prototype = nothing - end - - obj_expr = toexpr(subs_constants(objective(sys))) - pairs_arr = if p isa SciMLBase.NullParameters - [Symbol(_s) => Expr(:ref, :x, i) for (i, _s) in enumerate(dvs)] - else - vcat([Symbol(_s) => Expr(:ref, :x, i) for (i, _s) in enumerate(dvs)], - [Symbol(_p) => p[i] for (i, _p) in enumerate(ps)]) - end - rep_pars_vals!(obj_expr, pairs_arr) - - if length(cstr) > 0 - @named cons_sys = ConstraintsSystem(cstr, dvs, ps) - cons, lcons_, ucons_ = generate_function(cons_sys, checkbounds = checkbounds, - linenumbers = linenumbers, - expression = Val{true}) - cons = eval_or_rgf(cons; eval_expression, eval_module) - if cons_j - _cons_j = eval_or_rgf( - generate_jacobian(cons_sys; expression = Val{true}, sparse = sparse)[2]; - eval_expression, eval_module) - else - _cons_j = nothing - end - if cons_h - _cons_h = eval_or_rgf( - generate_hessian(cons_sys; expression = Val{true}, sparse = sparse)[2]; - eval_expression, eval_module) - else - _cons_h = nothing - end - - cons_expr = toexpr.(subs_constants(constraints(cons_sys))) - rep_pars_vals!.(cons_expr, Ref(pairs_arr)) - - if !haskey(kwargs, :lcons) && !haskey(kwargs, :ucons) # use the symbolically specified bounds - lcons = lcons_ - ucons = ucons_ - else # use the user supplied constraints bounds - (haskey(kwargs, :lcons) ⊻ haskey(kwargs, :ucons)) && - throw(ArgumentError("Expected both `ucons` and `lcons` to be supplied")) - haskey(kwargs, :lcons) && length(kwargs[:lcons]) != length(cstr) && - throw(ArgumentError("Expected `lcons` to be of the same length as the vector of constraints")) - haskey(kwargs, :ucons) && length(kwargs[:ucons]) != length(cstr) && - throw(ArgumentError("Expected `ucons` to be of the same length as the vector of constraints")) - lcons = haskey(kwargs, :lcons) - ucons = haskey(kwargs, :ucons) - end - - if sparse - cons_jac_prototype = jacobian_sparsity(cons_sys) - cons_hess_prototype = hessian_sparsity(cons_sys) - else - cons_jac_prototype = nothing - cons_hess_prototype = nothing - end - - quote - f = $f - p = $p - u0 = $u0 - grad = $_grad - hess = $_hess - lb = $lb - ub = $ub - int = $int - cons = $cons[1] - lcons = $lcons - ucons = $ucons - cons_j = $_cons_j - cons_h = $_cons_h - _f = OptimizationFunction{iip}(f, SciMLBase.NoAD(); - grad = grad, - hess = hess, - hess_prototype = hess_prototype, - cons = cons, - cons_j = cons_j, - cons_h = cons_h, - cons_jac_prototype = cons_jac_prototype, - cons_hess_prototype = cons_hess_prototype, - expr = obj_expr, - cons_expr = cons_expr) - OptimizationProblem{$iip}( - _f, u0, p; lb = lb, ub = ub, int = int, lcons = lcons, - ucons = ucons, kwargs...) - end - else - quote - f = $f - p = $p - u0 = $u0 - grad = $_grad - hess = $_hess - lb = $lb - ub = $ub - int = $int - _f = OptimizationFunction{iip}(f, SciMLBase.NoAD(); - grad = grad, - hess = hess, - hess_prototype = hess_prototype, - expr = obj_expr) - OptimizationProblem{$iip}(_f, u0, p; lb = lb, ub = ub, int = int, kwargs...) - end - end -end - -function structural_simplify(sys::OptimizationSystem; split = true, kwargs...) - sys = flatten(sys) - cons = constraints(sys) - econs = Equation[] - icons = similar(cons, 0) - for e in cons - if e isa Equation - push!(econs, e) - else - push!(icons, e) - end - end - nlsys = NonlinearSystem(econs, unknowns(sys), parameters(sys); name = :___tmp_nlsystem) - snlsys = structural_simplify(nlsys; fully_determined = false, kwargs...) - obs = observed(snlsys) - subs = Dict(eq.lhs => eq.rhs for eq in observed(snlsys)) - seqs = equations(snlsys) - cons_simplified = similar(cons, length(icons) + length(seqs)) - for (i, eq) in enumerate(Iterators.flatten((seqs, icons))) - cons_simplified[i] = fixpoint_sub(eq, subs) - end - newsts = setdiff(unknowns(sys), keys(subs)) - @set! sys.constraints = cons_simplified - @set! sys.observed = [observed(sys); obs] - neweqs = fixpoint_sub.(equations(sys), (subs,)) - @set! sys.op = length(neweqs) == 1 ? first(neweqs) : neweqs - @set! sys.unknowns = newsts - sys = complete(sys; split) - return sys -end - -supports_initialization(::OptimizationSystem) = false From 5eb6a1c0ee645a07d99af5b565aabab6873e990b Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 21 Apr 2025 10:51:39 +0530 Subject: [PATCH 1628/2176] refactor: remove `constraints_system.jl` --- src/ModelingToolkit.jl | 2 - .../optimization/constraints_system.jl | 255 ------------------ 2 files changed, 257 deletions(-) delete mode 100644 src/systems/optimization/constraints_system.jl diff --git a/src/ModelingToolkit.jl b/src/ModelingToolkit.jl index 1bdb215ff0..d99730cf08 100644 --- a/src/ModelingToolkit.jl +++ b/src/ModelingToolkit.jl @@ -165,7 +165,6 @@ include("systems/codegen_utils.jl") include("systems/problem_utils.jl") include("linearization.jl") -include("systems/optimization/constraints_system.jl") include("systems/optimization/modelingtoolkitize.jl") include("systems/diffeqs/abstractodesystem.jl") @@ -273,7 +272,6 @@ export IntervalNonlinearProblem, IntervalNonlinearProblemExpr export OptimizationProblem, OptimizationProblemExpr, constraints export SteadyStateProblem, SteadyStateProblemExpr export JumpProblem -export ConstraintsSystem export alias_elimination, flatten export connect, domain_connect, @connector, Connection, AnalysisPoint, Flow, Stream, instream diff --git a/src/systems/optimization/constraints_system.jl b/src/systems/optimization/constraints_system.jl deleted file mode 100644 index ae8577662d..0000000000 --- a/src/systems/optimization/constraints_system.jl +++ /dev/null @@ -1,255 +0,0 @@ -""" -$(TYPEDEF) - -A constraint system of equations. - -# Fields -$(FIELDS) - -# Examples - -```julia -@variables x y z -@parameters a b c - -cstr = [0 ~ a*(y-x), - 0 ~ x*(b-z)-y, - 0 ~ x*y - c*z - x^2 + y^2 ≲ 1] -@named ns = ConstraintsSystem(cstr, [x,y,z],[a,b,c]) -``` -""" -struct ConstraintsSystem <: AbstractTimeIndependentSystem - """ - A tag for the system. If two systems have the same tag, then they are - structurally identical. - """ - tag::UInt - """Vector of equations defining the system.""" - constraints::Vector{Union{Equation, Inequality}} - """Unknown variables.""" - unknowns::Vector - """Parameters.""" - ps::Vector - """Array variables.""" - var_to_name::Any - """Observed equations.""" - observed::Vector{Equation} - """ - Jacobian matrix. Note: this field will not be defined until - [`calculate_jacobian`](@ref) is called on the system. - """ - jac::RefValue{Any} - """ - The name of the system. - """ - name::Symbol - """ - A description of the system. - """ - description::String - """ - The internal systems. These are required to have unique names. - """ - systems::Vector{ConstraintsSystem} - """ - The default values to use when initial conditions and/or - parameters are not supplied in `ODEProblem`. - """ - defaults::Dict - """ - Type of the system. - """ - connector_type::Any - """ - Metadata for the system, to be used by downstream packages. - """ - metadata::Any - """ - Cache for intermediate tearing state. - """ - tearing_state::Any - """ - Substitutions generated by tearing. - """ - substitutions::Any - """ - If false, then `sys.x` no longer performs namespacing. - """ - namespacing::Bool - """ - If true, denotes the model will not be modified any further. - """ - complete::Bool - """ - Cached data for fast symbolic indexing. - """ - index_cache::Union{Nothing, IndexCache} - - function ConstraintsSystem(tag, constraints, unknowns, ps, var_to_name, observed, jac, - name, description, - systems, - defaults, connector_type, metadata = nothing, - tearing_state = nothing, substitutions = nothing, namespacing = true, - complete = false, index_cache = nothing; - checks::Union{Bool, Int} = true) - if checks == true || (checks & CheckUnits) > 0 - u = __get_unit_type(unknowns, ps) - check_units(u, constraints) - check_subsystems(systems) - end - new(tag, constraints, unknowns, ps, var_to_name, - observed, jac, name, description, systems, - defaults, connector_type, metadata, tearing_state, substitutions, - namespacing, complete, index_cache) - end -end - -equations(sys::ConstraintsSystem) = constraints(sys) # needed for Base.show - -function ConstraintsSystem(constraints, unknowns, ps; - observed = [], - name = nothing, - description = "", - default_u0 = Dict(), - default_p = Dict(), - defaults = _merge(Dict(default_u0), Dict(default_p)), - systems = ConstraintsSystem[], - connector_type = nothing, - continuous_events = nothing, # this argument is only required for ODESystems, but is added here for the constructor to accept it without error - discrete_events = nothing, # this argument is only required for ODESystems, but is added here for the constructor to accept it without error - checks = true, - metadata = nothing) - continuous_events === nothing || isempty(continuous_events) || - throw(ArgumentError("ConstraintsSystem does not accept `continuous_events`, you provided $continuous_events")) - discrete_events === nothing || isempty(discrete_events) || - throw(ArgumentError("ConstraintsSystem does not accept `discrete_events`, you provided $discrete_events")) - - name === nothing && - throw(ArgumentError("The `name` keyword must be provided. Please consider using the `@named` macro")) - - cstr = value.(Symbolics.canonical_form.(vcat(scalarize(constraints)...))) - unknowns′ = value.(scalarize(unknowns)) - ps′ = value.(ps) - - if !(isempty(default_u0) && isempty(default_p)) - Base.depwarn( - "`default_u0` and `default_p` are deprecated. Use `defaults` instead.", - :ConstraintsSystem, force = true) - end - sysnames = nameof.(systems) - if length(unique(sysnames)) != length(sysnames) - throw(ArgumentError("System names must be unique.")) - end - - jac = RefValue{Any}(EMPTY_JAC) - defaults = todict(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, Dict(), unknowns′) - process_variables!(var_to_name, defaults, Dict(), ps′) - isempty(observed) || collect_var_to_name!(var_to_name, (eq.lhs for eq in observed)) - - ConstraintsSystem(Threads.atomic_add!(SYSTEM_COUNT, UInt(1)), - cstr, unknowns, ps, var_to_name, observed, jac, name, description, systems, - defaults, - connector_type, metadata, checks = checks) -end - -function calculate_jacobian(sys::ConstraintsSystem; sparse = false, simplify = false) - cache = get_jac(sys)[] - if cache isa Tuple && cache[2] == (sparse, simplify) - return cache[1] - end - - lhss = generate_canonical_form_lhss(sys) - vals = [dv for dv in unknowns(sys)] - if sparse - jac = sparsejacobian(lhss, vals, simplify = simplify) - else - jac = jacobian(lhss, vals, simplify = simplify) - end - get_jac(sys)[] = jac, (sparse, simplify) - return jac -end - -function generate_jacobian( - sys::ConstraintsSystem, vs = unknowns(sys), ps = parameters( - sys; initial_parameters = true); - sparse = false, simplify = false, kwargs...) - jac = calculate_jacobian(sys, sparse = sparse, simplify = simplify) - p = reorder_parameters(sys, ps) - return build_function_wrapper(sys, jac, vs, p...; kwargs...) -end - -function calculate_hessian(sys::ConstraintsSystem; sparse = false, simplify = false) - lhss = generate_canonical_form_lhss(sys) - vals = [dv for dv in unknowns(sys)] - if sparse - hess = [sparsehessian(lhs, vals, simplify = simplify) for lhs in lhss] - else - hess = [hessian(lhs, vals, simplify = simplify) for lhs in lhss] - end - return hess -end - -function generate_hessian( - sys::ConstraintsSystem, vs = unknowns(sys), ps = parameters( - sys; initial_parameters = true); - sparse = false, simplify = false, kwargs...) - hess = calculate_hessian(sys, sparse = sparse, simplify = simplify) - p = reorder_parameters(sys, ps) - return build_function_wrapper(sys, hess, vs, p...; kwargs...) -end - -function generate_function(sys::ConstraintsSystem, dvs = unknowns(sys), - ps = parameters(sys; initial_parameters = true); - kwargs...) - lhss = generate_canonical_form_lhss(sys) - p = reorder_parameters(sys, value.(ps)) - func = build_function_wrapper(sys, lhss, value.(dvs), p...; kwargs...) - - cstr = constraints(sys) - lcons = fill(-Inf, length(cstr)) - ucons = zeros(length(cstr)) - lcons[findall(Base.Fix2(isa, Equation), cstr)] .= 0.0 - - return func, lcons, ucons -end - -function jacobian_sparsity(sys::ConstraintsSystem) - lhss = generate_canonical_form_lhss(sys) - jacobian_sparsity(lhss, unknowns(sys)) -end - -function hessian_sparsity(sys::ConstraintsSystem) - lhss = generate_canonical_form_lhss(sys) - [hessian_sparsity(eq, unknowns(sys)) for eq in lhss] -end - -""" -Convert the system of equalities and inequalities into a canonical form: -h(x) = 0 -g(x) <= 0 -""" -function generate_canonical_form_lhss(sys) - lhss = subs_constants([Symbolics.canonical_form(eq).lhs for eq in constraints(sys)]) -end - -function get_cmap(sys::ConstraintsSystem, exprs = nothing) - #Inject substitutions for constants => values - cs = collect_constants([get_constraints(sys); get_observed(sys)]) #ctrls? what else? - if !empty_substitutions(sys) - cs = [cs; collect_constants(get_substitutions(sys).subs)] - end - if exprs !== nothing - cs = [cs; collect_constants(exprs)] - end - # Swap constants for their values - cmap = map(x -> x ~ getdefault(x), cs) - return cmap, cs -end - -supports_initialization(::ConstraintsSystem) = false From adfb917d7c261b8fc99d2e0fa0c7da887a0c394f Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 14 May 2025 12:10:51 +0530 Subject: [PATCH 1629/2176] remove abstractodesystem.jl refactor: remove `abstractodesystem.jl` refactor: remove `AbstractODESystem` --- src/ModelingToolkit.jl | 2 - src/systems/diffeqs/abstractodesystem.jl | 1559 ---------------------- 2 files changed, 1561 deletions(-) delete mode 100644 src/systems/diffeqs/abstractodesystem.jl diff --git a/src/ModelingToolkit.jl b/src/ModelingToolkit.jl index d99730cf08..9dd89d452c 100644 --- a/src/ModelingToolkit.jl +++ b/src/ModelingToolkit.jl @@ -130,7 +130,6 @@ TODO abstract type AbstractSystem end abstract type AbstractTimeDependentSystem <: AbstractSystem end abstract type AbstractTimeIndependentSystem <: AbstractSystem end -abstract type AbstractODESystem <: AbstractTimeDependentSystem end abstract type AbstractMultivariateSystem <: AbstractSystem end abstract type AbstractOptimizationSystem <: AbstractTimeIndependentSystem end abstract type AbstractDiscreteSystem <: AbstractTimeDependentSystem end @@ -167,7 +166,6 @@ include("linearization.jl") include("systems/optimization/modelingtoolkitize.jl") -include("systems/diffeqs/abstractodesystem.jl") include("systems/nonlinear/homotopy_continuation.jl") include("systems/nonlinear/modelingtoolkitize.jl") include("systems/nonlinear/initializesystem.jl") diff --git a/src/systems/diffeqs/abstractodesystem.jl b/src/systems/diffeqs/abstractodesystem.jl deleted file mode 100644 index d802f49fee..0000000000 --- a/src/systems/diffeqs/abstractodesystem.jl +++ /dev/null @@ -1,1559 +0,0 @@ -struct Schedule - var_eq_matching::Any - dummy_sub::Any -end - -""" - is_dde(sys::AbstractSystem) - -Return a boolean indicating whether a system represents a set of delay -differential equations. -""" -is_dde(sys::AbstractSystem) = has_is_dde(sys) && get_is_dde(sys) - -function _check_if_dde(eqs, iv, subsystems) - is_dde = any(ModelingToolkit.is_dde, subsystems) - if !is_dde - vs = Set() - for eq in eqs - vars!(vs, eq) - is_dde = any(vs) do sym - isdelay(unwrap(sym), iv) - end - is_dde && break - end - end - return is_dde -end - -function filter_kwargs(kwargs) - kwargs = Dict(kwargs) - for key in keys(kwargs) - key in DiffEqBase.allowedkeywords || delete!(kwargs, key) - end - pairs(NamedTuple(kwargs)) -end -function gen_quoted_kwargs(kwargs) - kwargparam = Expr(:parameters) - for kw in kwargs - push!(kwargparam.args, Expr(:kw, kw[1], kw[2])) - end - kwargparam -end - -function calculate_tgrad(sys::AbstractODESystem; - simplify = false) - isempty(get_tgrad(sys)[]) || return get_tgrad(sys)[] # use cached tgrad, if possible - - # We need to remove explicit time dependence on the unknown because when we - # have `u(t) * t` we want to have the tgrad to be `u(t)` instead of `u'(t) * - # t + u(t)`. - rhs = [detime_dvs(eq.rhs) for eq in full_equations(sys)] - iv = get_iv(sys) - xs = unknowns(sys) - rule = Dict(map((x, xt) -> xt => x, detime_dvs.(xs), xs)) - rhs = substitute.(rhs, Ref(rule)) - tgrad = [expand_derivatives(Differential(iv)(r), simplify) for r in rhs] - reverse_rule = Dict(map((x, xt) -> x => xt, detime_dvs.(xs), xs)) - tgrad = Num.(substitute.(tgrad, Ref(reverse_rule))) - get_tgrad(sys)[] = tgrad - return tgrad -end - -function calculate_jacobian(sys::AbstractODESystem; - sparse = false, simplify = false, dvs = unknowns(sys)) - if isequal(dvs, unknowns(sys)) - cache = get_jac(sys)[] - if cache isa Tuple && cache[2] == (sparse, simplify) - return cache[1] - end - end - - rhs = [eq.rhs - eq.lhs for eq in full_equations(sys)] #need du terms on rhs for differentiating wrt du - - if sparse - jac = sparsejacobian(rhs, dvs, simplify = simplify) - W_s = W_sparsity(sys) - (Is, Js, Vs) = findnz(W_s) - # Add nonzeros of W as non-structural zeros of the Jacobian (to ensure equal results for oop and iip Jacobian.) - for (i, j) in zip(Is, Js) - iszero(jac[i, j]) && begin - jac[i, j] = 1 - jac[i, j] = 0 - end - end - else - jac = jacobian(rhs, dvs, simplify = simplify) - end - - if isequal(dvs, unknowns(sys)) - get_jac(sys)[] = jac, (sparse, simplify) # cache Jacobian - end - - return jac -end - -function calculate_control_jacobian(sys::AbstractODESystem; - sparse = false, simplify = false) - cache = get_ctrl_jac(sys)[] - if cache isa Tuple && cache[2] == (sparse, simplify) - return cache[1] - end - - rhs = [eq.rhs for eq in full_equations(sys)] - ctrls = unbound_inputs(sys) - - if sparse - jac = sparsejacobian(rhs, ctrls, simplify = simplify) - else - jac = jacobian(rhs, ctrls, simplify = simplify) - end - - get_ctrl_jac(sys)[] = jac, (sparse, simplify) # cache Jacobian - return jac -end - -function generate_tgrad( - sys::AbstractODESystem, dvs = unknowns(sys), ps = parameters( - sys; initial_parameters = true); - simplify = false, kwargs...) - tgrad = calculate_tgrad(sys, simplify = simplify) - p = reorder_parameters(sys, ps) - return build_function_wrapper(sys, tgrad, - dvs, - p..., - get_iv(sys); - kwargs...) -end - -function generate_jacobian(sys::AbstractODESystem, dvs = unknowns(sys), - ps = parameters(sys; initial_parameters = true); - simplify = false, sparse = false, kwargs...) - jac = calculate_jacobian(sys; simplify = simplify, sparse = sparse) - p = reorder_parameters(sys, ps) - return build_function_wrapper(sys, jac, - dvs, - p..., - get_iv(sys); - wrap_code = sparse ? assert_jac_length_header(sys) : (identity, identity), - kwargs...) -end - -function assert_jac_length_header(sys) - W = W_sparsity(sys) - identity, - function add_header(expr) - Func(expr.args, [], expr.body, - [:(@assert $(SymbolicUtils.Code.toexpr(term(findnz, expr.args[1])))[1:2] == - $(findnz(W)[1:2]))]) - end -end - -function generate_W(sys::AbstractODESystem, γ = 1.0, dvs = unknowns(sys), - ps = parameters(sys; initial_parameters = true); - simplify = false, sparse = false, kwargs...) - @variables ˍ₋gamma - M = calculate_massmatrix(sys; simplify) - sparse && (M = SparseArrays.sparse(M)) - J = calculate_jacobian(sys; simplify, sparse, dvs) - W = ˍ₋gamma * M + J - - p = reorder_parameters(sys, ps) - return build_function_wrapper(sys, W, - dvs, - p..., - ˍ₋gamma, - get_iv(sys); - wrap_code = sparse ? assert_jac_length_header(sys) : (identity, identity), - p_end = 1 + length(p), - kwargs...) -end - -function generate_control_jacobian(sys::AbstractODESystem, dvs = unknowns(sys), - ps = parameters(sys; initial_parameters = true); - simplify = false, sparse = false, kwargs...) - jac = calculate_control_jacobian(sys; simplify = simplify, sparse = sparse) - p = reorder_parameters(sys, ps) - return build_function_wrapper(sys, jac, dvs, p..., get_iv(sys); kwargs...) -end - -function generate_dae_jacobian(sys::AbstractODESystem, dvs = unknowns(sys), - ps = parameters(sys; initial_parameters = true); simplify = false, sparse = false, - kwargs...) - jac_u = calculate_jacobian(sys; simplify = simplify, sparse = sparse) - derivatives = Differential(get_iv(sys)).(unknowns(sys)) - jac_du = calculate_jacobian(sys; simplify = simplify, sparse = sparse, - dvs = derivatives) - dvs = unknowns(sys) - @variables ˍ₋gamma - jac = ˍ₋gamma * jac_du + jac_u - pre = get_preprocess_constants(jac) - p = reorder_parameters(sys, ps) - return build_function_wrapper(sys, jac, derivatives, dvs, p..., ˍ₋gamma, get_iv(sys); - p_start = 3, p_end = 2 + length(p), kwargs...) -end - -function generate_function(sys::AbstractODESystem, dvs = unknowns(sys), - ps = parameters(sys; initial_parameters = true); - implicit_dae = false, - ddvs = implicit_dae ? map(Differential(get_iv(sys)), dvs) : - nothing, - isdde = false, - kwargs...) - eqs = [eq for eq in equations(sys)] - if !implicit_dae - check_operator_variables(eqs, Differential) - check_lhs(eqs, Differential, Set(dvs)) - end - - rhss = implicit_dae ? [_iszero(eq.lhs) ? eq.rhs : eq.rhs - eq.lhs for eq in eqs] : - [eq.rhs for eq in eqs] - - if !isempty(assertions(sys)) - rhss[end] += unwrap(get_assertions_expr(sys)) - end - - # TODO: add an optional check on the ordering of observed equations - u = dvs - p = reorder_parameters(sys, ps) - t = get_iv(sys) - - if implicit_dae - build_function_wrapper(sys, rhss, ddvs, u, p..., t; p_start = 3, kwargs...) - else - build_function_wrapper(sys, rhss, u, p..., t; kwargs...) - end -end - -function isdelay(var, iv) - iv === nothing && return false - isvariable(var) || return false - isparameter(var) && return false - if iscall(var) && !ModelingToolkit.isoperator(var, Symbolics.Operator) - args = arguments(var) - length(args) == 1 || return false - isequal(args[1], iv) || return true - end - return false -end -const DDE_HISTORY_FUN = Sym{Symbolics.FnType{Tuple{Any, <:Real}, Vector{Real}}}(:___history___) -const DEFAULT_PARAMS_ARG = Sym{Any}(:ˍ₋arg3) -function delay_to_function( - sys::AbstractODESystem, eqs = full_equations(sys); history_arg = DEFAULT_PARAMS_ARG) - delay_to_function(eqs, - get_iv(sys), - Dict{Any, Int}(operation(s) => i for (i, s) in enumerate(unknowns(sys))), - parameters(sys), - DDE_HISTORY_FUN; history_arg) -end -function delay_to_function(eqs::Vector, iv, sts, ps, h; history_arg = DEFAULT_PARAMS_ARG) - delay_to_function.(eqs, (iv,), (sts,), (ps,), (h,); history_arg) -end -function delay_to_function(eq::Equation, iv, sts, ps, h; history_arg = DEFAULT_PARAMS_ARG) - delay_to_function(eq.lhs, iv, sts, ps, h; history_arg) ~ delay_to_function( - eq.rhs, iv, sts, ps, h; history_arg) -end -function delay_to_function(expr, iv, sts, ps, h; history_arg = DEFAULT_PARAMS_ARG) - if isdelay(expr, iv) - v = operation(expr) - time = arguments(expr)[1] - idx = sts[v] - return term(getindex, h(history_arg, time), idx, type = Real) # BIG BIG HACK - elseif iscall(expr) - return maketerm(typeof(expr), - operation(expr), - map(x -> delay_to_function(x, iv, sts, ps, h; history_arg), arguments(expr)), - metadata(expr)) - else - return expr - end -end - -function calculate_massmatrix(sys::AbstractODESystem; simplify = false) - eqs = [eq for eq in equations(sys)] - M = zeros(length(eqs), length(eqs)) - for (i, eq) in enumerate(eqs) - if iscall(eq.lhs) && operation(eq.lhs) isa Differential - st = var_from_nested_derivative(eq.lhs)[1] - j = variable_index(sys, st) - M[i, j] = 1 - else - _iszero(eq.lhs) || - error("Only semi-explicit constant mass matrices are currently supported. Faulty equation: $eq.") - end - end - M = simplify ? ModelingToolkit.simplify.(M) : M - # M should only contain concrete numbers - if isdiag(M) - M = Diagonal(M) - end - M == I ? I : M -end - -function jacobian_sparsity(sys::AbstractODESystem) - sparsity = torn_system_jacobian_sparsity(sys) - sparsity === nothing || return sparsity - - jacobian_sparsity([eq.rhs for eq in full_equations(sys)], - [dv for dv in unknowns(sys)]) -end - -function jacobian_dae_sparsity(sys::AbstractODESystem) - J1 = jacobian_sparsity([eq.rhs for eq in full_equations(sys)], - [dv for dv in unknowns(sys)]) - derivatives = Differential(get_iv(sys)).(unknowns(sys)) - J2 = jacobian_sparsity([eq.rhs for eq in full_equations(sys)], - [dv for dv in derivatives]) - J1 + J2 -end - -function W_sparsity(sys::AbstractODESystem) - jac_sparsity = jacobian_sparsity(sys) - (n, n) = size(jac_sparsity) - M = calculate_massmatrix(sys) - M_sparsity = M isa UniformScaling ? sparse(I(n)) : - SparseMatrixCSC{Bool, Int64}((!iszero).(M)) - jac_sparsity .| M_sparsity -end - -function isautonomous(sys::AbstractODESystem) - tgrad = calculate_tgrad(sys; simplify = true) - all(iszero, tgrad) -end - -""" -```julia -DiffEqBase.ODEFunction{iip}(sys::AbstractODESystem, dvs = unknowns(sys), - ps = parameters(sys); - version = nothing, tgrad = false, - jac = false, - sparse = false, - kwargs...) where {iip} -``` - -Create an `ODEFunction` from the [`ODESystem`](@ref). The arguments `dvs` and `ps` -are used to set the order of the dependent variable and parameter vectors, -respectively. -""" -function DiffEqBase.ODEFunction(sys::AbstractODESystem, args...; kwargs...) - ODEFunction{true}(sys, args...; kwargs...) -end - -function DiffEqBase.ODEFunction{true}(sys::AbstractODESystem, args...; - kwargs...) - ODEFunction{true, SciMLBase.AutoSpecialize}(sys, args...; kwargs...) -end - -function DiffEqBase.ODEFunction{false}(sys::AbstractODESystem, args...; - kwargs...) - ODEFunction{false, SciMLBase.FullSpecialize}(sys, args...; kwargs...) -end - -function DiffEqBase.ODEFunction{iip, specialize}(sys::AbstractODESystem, - dvs = unknowns(sys), - ps = parameters(sys), u0 = nothing; - version = nothing, tgrad = false, - jac = false, p = nothing, - t = nothing, - eval_expression = false, - sparse = false, simplify = false, - eval_module = @__MODULE__, - steady_state = false, - checkbounds = false, - sparsity = false, - analytic = nothing, - split_idxs = nothing, - initialization_data = nothing, - cse = true, - kwargs...) where {iip, specialize} - if !iscomplete(sys) - error("A completed system is required. Call `complete` or `structural_simplify` on the system before creating an `ODEFunction`") - end - f_gen = generate_function(sys, dvs, ps; expression = Val{true}, - expression_module = eval_module, checkbounds = checkbounds, cse, - kwargs...) - f_oop, f_iip = eval_or_rgf.(f_gen; eval_expression, eval_module) - f = GeneratedFunctionWrapper{(2, 3, is_split(sys))}(f_oop, f_iip) - - if specialize === SciMLBase.FunctionWrapperSpecialize && iip - if u0 === nothing || p === nothing || t === nothing - error("u0, p, and t must be specified for FunctionWrapperSpecialize on ODEFunction.") - end - f = SciMLBase.wrapfun_iip(f, (u0, u0, p, t)) - end - - if tgrad - tgrad_gen = generate_tgrad(sys, dvs, ps; - simplify = simplify, - expression = Val{true}, - expression_module = eval_module, cse, - checkbounds = checkbounds, kwargs...) - tgrad_oop, tgrad_iip = eval_or_rgf.(tgrad_gen; eval_expression, eval_module) - _tgrad = GeneratedFunctionWrapper{(2, 3, is_split(sys))}(tgrad_oop, tgrad_iip) - else - _tgrad = nothing - end - - if jac - jac_gen = generate_jacobian(sys, dvs, ps; - simplify = simplify, sparse = sparse, - expression = Val{true}, - expression_module = eval_module, cse, - checkbounds = checkbounds, kwargs...) - jac_oop, jac_iip = eval_or_rgf.(jac_gen; eval_expression, eval_module) - - _jac = GeneratedFunctionWrapper{(2, 3, is_split(sys))}(jac_oop, jac_iip) - else - _jac = nothing - end - - M = calculate_massmatrix(sys) - - _M = if sparse && !(u0 === nothing || M === I) - SparseArrays.sparse(M) - elseif u0 === nothing || M === I - M - elseif M isa Diagonal - Diagonal(ArrayInterface.restructure(u0, diag(M))) - else - ArrayInterface.restructure(u0 .* u0', M) - end - - observedfun = ObservedFunctionCache( - sys; steady_state, eval_expression, eval_module, checkbounds, cse) - - if sparse - uElType = u0 === nothing ? Float64 : eltype(u0) - W_prototype = similar(W_sparsity(sys), uElType) - else - W_prototype = nothing - end - - @set! sys.split_idxs = split_idxs - - ODEFunction{iip, specialize}(f; - sys = sys, - jac = _jac === nothing ? nothing : _jac, - tgrad = _tgrad === nothing ? nothing : _tgrad, - mass_matrix = _M, - jac_prototype = W_prototype, - observed = observedfun, - sparsity = sparsity ? W_sparsity(sys) : nothing, - analytic = analytic, - initialization_data) -end - -""" -```julia -DiffEqBase.DAEFunction{iip}(sys::AbstractODESystem, dvs = unknowns(sys), - ps = parameters(sys); - version = nothing, tgrad = false, - jac = false, - sparse = false, - kwargs...) where {iip} -``` - -Create an `DAEFunction` from the [`ODESystem`](@ref). The arguments `dvs` and -`ps` are used to set the order of the dependent variable and parameter vectors, -respectively. -""" -function DiffEqBase.DAEFunction(sys::AbstractODESystem, args...; kwargs...) - DAEFunction{true}(sys, args...; kwargs...) -end - -function DiffEqBase.DAEFunction{iip}(sys::AbstractODESystem, dvs = unknowns(sys), - ps = parameters(sys), u0 = nothing; - ddvs = map(Base.Fix2(diff2term, get_iv(sys)) ∘ Differential(get_iv(sys)), dvs), - version = nothing, p = nothing, - jac = false, - eval_expression = false, - sparse = false, simplify = false, - eval_module = @__MODULE__, - checkbounds = false, - initialization_data = nothing, - cse = true, - kwargs...) where {iip} - if !iscomplete(sys) - error("A completed system is required. Call `complete` or `structural_simplify` on the system before creating a `DAEFunction`") - end - f_gen = generate_function(sys, dvs, ps; implicit_dae = true, - expression = Val{true}, cse, - expression_module = eval_module, checkbounds = checkbounds, - kwargs...) - f_oop, f_iip = eval_or_rgf.(f_gen; eval_expression, eval_module) - f = GeneratedFunctionWrapper{(3, 4, is_split(sys))}(f_oop, f_iip) - - if jac - jac_gen = generate_dae_jacobian(sys, dvs, ps; - simplify = simplify, sparse = sparse, - expression = Val{true}, - expression_module = eval_module, cse, - checkbounds = checkbounds, kwargs...) - jac_oop, jac_iip = eval_or_rgf.(jac_gen; eval_expression, eval_module) - - _jac = GeneratedFunctionWrapper{(3, 5, is_split(sys))}(jac_oop, jac_iip) - else - _jac = nothing - end - - observedfun = ObservedFunctionCache( - sys; eval_expression, eval_module, checkbounds = get(kwargs, :checkbounds, false), cse) - - jac_prototype = if sparse - uElType = u0 === nothing ? Float64 : eltype(u0) - if jac - J1 = calculate_jacobian(sys, sparse = sparse) - derivatives = Differential(get_iv(sys)).(unknowns(sys)) - J2 = calculate_jacobian(sys; sparse = sparse, dvs = derivatives) - similar(J1 + J2, uElType) - else - similar(jacobian_dae_sparsity(sys), uElType) - end - else - nothing - end - - DAEFunction{iip}(f; - sys = sys, - jac = _jac === nothing ? nothing : _jac, - jac_prototype = jac_prototype, - observed = observedfun, - initialization_data) -end - -function DiffEqBase.DDEFunction(sys::AbstractODESystem, args...; kwargs...) - DDEFunction{true}(sys, args...; kwargs...) -end - -function DiffEqBase.DDEFunction{iip}(sys::AbstractODESystem, dvs = unknowns(sys), - ps = parameters(sys), u0 = nothing; - eval_expression = false, - eval_module = @__MODULE__, - checkbounds = false, - initialization_data = nothing, - cse = true, - kwargs...) where {iip} - if !iscomplete(sys) - error("A completed system is required. Call `complete` or `structural_simplify` on the system before creating an `DDEFunction`") - end - f_gen = generate_function(sys, dvs, ps; isdde = true, - expression = Val{true}, - expression_module = eval_module, checkbounds = checkbounds, - cse, kwargs...) - f_oop, f_iip = eval_or_rgf.(f_gen; eval_expression, eval_module) - f = GeneratedFunctionWrapper{(3, 4, is_split(sys))}(f_oop, f_iip) - - DDEFunction{iip}(f; sys = sys, initialization_data) -end - -function DiffEqBase.SDDEFunction(sys::AbstractODESystem, args...; kwargs...) - SDDEFunction{true}(sys, args...; kwargs...) -end - -function DiffEqBase.SDDEFunction{iip}(sys::AbstractODESystem, dvs = unknowns(sys), - ps = parameters(sys), u0 = nothing; - eval_expression = false, - eval_module = @__MODULE__, - checkbounds = false, - initialization_data = nothing, - cse = true, - kwargs...) where {iip} - if !iscomplete(sys) - error("A completed system is required. Call `complete` or `structural_simplify` on the system before creating an `SDDEFunction`") - end - f_gen = generate_function(sys, dvs, ps; isdde = true, - expression = Val{true}, - expression_module = eval_module, checkbounds = checkbounds, - cse, kwargs...) - f_oop, f_iip = eval_or_rgf.(f_gen; eval_expression, eval_module) - f = GeneratedFunctionWrapper{(3, 4, is_split(sys))}(f_oop, f_iip) - - g_gen = generate_diffusion_function(sys, dvs, ps; expression = Val{true}, - isdde = true, cse, kwargs...) - g_oop, g_iip = eval_or_rgf.(g_gen; eval_expression, eval_module) - g = GeneratedFunctionWrapper{(3, 4, is_split(sys))}(g_oop, g_iip) - - SDDEFunction{iip}(f, g; sys = sys, initialization_data) -end - -""" -```julia -ODEFunctionExpr{iip}(sys::AbstractODESystem, dvs = unknowns(sys), - ps = parameters(sys); - version = nothing, tgrad = false, - jac = false, - sparse = false, - kwargs...) where {iip} -``` - -Create a Julia expression for an `ODEFunction` from the [`ODESystem`](@ref). -The arguments `dvs` and `ps` are used to set the order of the dependent -variable and parameter vectors, respectively. -""" -struct ODEFunctionExpr{iip, specialize} end - -function ODEFunctionExpr{iip, specialize}(sys::AbstractODESystem, dvs = unknowns(sys), - ps = parameters(sys), u0 = nothing; - version = nothing, tgrad = false, - jac = false, p = nothing, - linenumbers = false, - sparse = false, simplify = false, - steady_state = false, - sparsity = false, - observedfun_exp = nothing, - kwargs...) where {iip, specialize} - if !iscomplete(sys) - error("A completed system is required. Call `complete` or `structural_simplify` on the system before creating an `ODEFunctionExpr`") - end - f_oop, f_iip = generate_function(sys, dvs, ps; expression = Val{true}, kwargs...) - - fsym = gensym(:f) - _f = :($fsym = $(GeneratedFunctionWrapper{(2, 3, is_split(sys))})($f_oop, $f_iip)) - tgradsym = gensym(:tgrad) - if tgrad - tgrad_oop, tgrad_iip = generate_tgrad(sys, dvs, ps; - simplify = simplify, - expression = Val{true}, kwargs...) - _tgrad = :($tgradsym = $(GeneratedFunctionWrapper{(2, 3, is_split(sys))})( - $tgrad_oop, $tgrad_iip)) - else - _tgrad = :($tgradsym = nothing) - end - - jacsym = gensym(:jac) - if jac - jac_oop, jac_iip = generate_jacobian(sys, dvs, ps; - sparse = sparse, simplify = simplify, - expression = Val{true}, kwargs...) - _jac = :($jacsym = $(GeneratedFunctionWrapper{(2, 3, is_split(sys))})( - $jac_oop, $jac_iip)) - else - _jac = :($jacsym = nothing) - end - - Msym = gensym(:M) - M = calculate_massmatrix(sys) - if sparse && !(u0 === nothing || M === I) - _M = :($Msym = $(SparseArrays.sparse(M))) - elseif u0 === nothing || M === I - _M = :($Msym = $M) - else - _M = :($Msym = $(ArrayInterface.restructure(u0 .* u0', M))) - end - - jp_expr = sparse ? :($similar($(get_jac(sys)[]), Float64)) : :nothing - ex = quote - let $_f, $_tgrad, $_jac, $_M - ODEFunction{$iip, $specialize}($fsym, - sys = $sys, - jac = $jacsym, - tgrad = $tgradsym, - mass_matrix = $Msym, - jac_prototype = $jp_expr, - sparsity = $(sparsity ? jacobian_sparsity(sys) : nothing), - observed = $observedfun_exp) - end - end - !linenumbers ? Base.remove_linenums!(ex) : ex -end - -function ODEFunctionExpr(sys::AbstractODESystem, args...; kwargs...) - ODEFunctionExpr{true}(sys, args...; kwargs...) -end - -function ODEFunctionExpr{true}(sys::AbstractODESystem, args...; kwargs...) - return ODEFunctionExpr{true, SciMLBase.AutoSpecialize}(sys, args...; kwargs...) -end - -function ODEFunctionExpr{false}(sys::AbstractODESystem, args...; kwargs...) - return ODEFunctionExpr{false, SciMLBase.FullSpecialize}(sys, args...; kwargs...) -end - -""" -```julia -DAEFunctionExpr{iip}(sys::AbstractODESystem, dvs = unknowns(sys), - ps = parameters(sys); - version = nothing, tgrad = false, - jac = false, - sparse = false, - kwargs...) where {iip} -``` - -Create a Julia expression for an `ODEFunction` from the [`ODESystem`](@ref). -The arguments `dvs` and `ps` are used to set the order of the dependent -variable and parameter vectors, respectively. -""" -struct DAEFunctionExpr{iip} end - -function DAEFunctionExpr{iip}(sys::AbstractODESystem, dvs = unknowns(sys), - ps = parameters(sys), u0 = nothing; - version = nothing, tgrad = false, - jac = false, p = nothing, - linenumbers = false, - sparse = false, simplify = false, - kwargs...) where {iip} - if !iscomplete(sys) - error("A completed system is required. Call `complete` or `structural_simplify` on the system before creating an `DAEFunctionExpr`") - end - f_oop, f_iip = generate_function(sys, dvs, ps; expression = Val{true}, - implicit_dae = true, kwargs...) - fsym = gensym(:f) - _f = :($fsym = $(GeneratedFunctionWrapper{(3, 4, is_split(sys))})($f_oop, $f_iip)) - ex = quote - $_f - ODEFunction{$iip}($fsym) - end - !linenumbers ? Base.remove_linenums!(ex) : ex -end - -function DAEFunctionExpr(sys::AbstractODESystem, args...; kwargs...) - DAEFunctionExpr{true}(sys, args...; kwargs...) -end - -struct SymbolicTstops{F} - fn::F -end - -function (st::SymbolicTstops)(p, tspan) - unique!(sort!(reduce(vcat, st.fn(p, tspan...)))) -end - -function SymbolicTstops( - sys::AbstractSystem; eval_expression = false, eval_module = @__MODULE__) - tstops = symbolic_tstops(sys) - isempty(tstops) && return nothing - t0 = gensym(:t0) - t1 = gensym(:t1) - tstops = map(tstops) do val - if is_array_of_symbolics(val) || val isa AbstractArray - collect(val) - else - term(:, t0, unwrap(val), t1; type = AbstractArray{Real}) - end - end - rps = reorder_parameters(sys) - tstops, _ = build_function_wrapper(sys, tstops, - rps..., - t0, - t1; - expression = Val{true}, - p_start = 1, p_end = length(rps), add_observed = false, force_SA = true) - tstops = eval_or_rgf(tstops; eval_expression, eval_module) - tstops = GeneratedFunctionWrapper{(1, 3, is_split(sys))}(tstops, nothing) - return SymbolicTstops(tstops) -end - -""" -```julia -DiffEqBase.ODEProblem{iip}(sys::AbstractODESystem, u0map, tspan, - parammap = DiffEqBase.NullParameters(); - allow_cost = false, - version = nothing, tgrad = false, - jac = false, - checkbounds = false, sparse = false, - simplify = false, - linenumbers = true, parallel = SerialForm(), - kwargs...) where {iip} -``` - -Generates an ODEProblem from an ODESystem and allows for automatically -symbolically calculating numerical enhancements. -""" -function DiffEqBase.ODEProblem(sys::AbstractODESystem, args...; kwargs...) - ODEProblem{true}(sys, args...; kwargs...) -end - -function DiffEqBase.ODEProblem(sys::AbstractODESystem, - u0map::StaticArray, - args...; - kwargs...) - ODEProblem{false, SciMLBase.FullSpecialize}(sys, u0map, args...; kwargs...) -end - -function DiffEqBase.ODEProblem{true}(sys::AbstractODESystem, args...; kwargs...) - ODEProblem{true, SciMLBase.AutoSpecialize}(sys, args...; kwargs...) -end - -function DiffEqBase.ODEProblem{false}(sys::AbstractODESystem, args...; kwargs...) - ODEProblem{false, SciMLBase.FullSpecialize}(sys, args...; kwargs...) -end - -function DiffEqBase.ODEProblem{iip, specialize}(sys::AbstractODESystem, u0map = [], - tspan = get_tspan(sys), - parammap = DiffEqBase.NullParameters(); - allow_cost = false, - callback = nothing, - check_length = true, - warn_initialize_determined = true, - eval_expression = false, - eval_module = @__MODULE__, - kwargs...) where {iip, specialize} - if !iscomplete(sys) - error("A completed system is required. Call `complete` or `structural_simplify` on the system before creating an `ODEProblem`") - end - - if !isnothing(get_constraintsystem(sys)) - error("An ODESystem with constraints cannot be used to construct a regular ODEProblem. - Consider a BVProblem instead.") - end - - if !isempty(get_costs(sys)) && !allow_cost - error("ODEProblem will not optimize solutions of ODESystems that have associated cost functions. - Solvers for optimal control problems are forthcoming. In order to bypass this error (e.g. - to check the cost of a regular solution), pass `allow_cost` = true into the constructor.") - end - - f, u0, p = process_SciMLProblem(ODEFunction{iip, specialize}, sys, u0map, parammap; - t = tspan !== nothing ? tspan[1] : tspan, - check_length, warn_initialize_determined, eval_expression, eval_module, kwargs...) - cbs = process_events(sys; callback, eval_expression, eval_module, kwargs...) - - kwargs = filter_kwargs(kwargs) - pt = something(get_metadata(sys), StandardODEProblem()) - - kwargs1 = (;) - if cbs !== nothing - kwargs1 = merge(kwargs1, (callback = cbs,)) - end - - tstops = SymbolicTstops(sys; eval_expression, eval_module) - if tstops !== nothing - kwargs1 = merge(kwargs1, (; tstops)) - end - - # Call `remake` so it runs initialization if it is trivial - return remake(ODEProblem{iip}(f, u0, tspan, p, pt; kwargs1..., kwargs...)) -end -get_callback(prob::ODEProblem) = prob.kwargs[:callback] - -""" -```julia -SciMLBase.BVProblem{iip}(sys::AbstractODESystem, u0map, tspan, - parammap = DiffEqBase.NullParameters(); - constraints = nothing, guesses = nothing, - version = nothing, tgrad = false, - jac = true, sparse = true, - simplify = false, - kwargs...) where {iip} -``` - -Create a boundary value problem from the [`ODESystem`](@ref). - -`u0map` is used to specify fixed initial values for the states. Every variable -must have either an initial guess supplied using `guesses` or a fixed initial -value specified using `u0map`. - -Boundary value conditions are supplied to ODESystems -in the form of a ConstraintsSystem. These equations -should specify values that state variables should -take at specific points, as in `x(0.5) ~ 1`). More general constraints that -should hold over the entire solution, such as `x(t)^2 + y(t)^2`, should be -specified as one of the equations used to build the `ODESystem`. - -If an ODESystem without `constraints` is specified, it will be treated as an initial value problem. - -```julia - @parameters g t_c = 0.5 - @variables x(..) y(t) λ(t) - eqs = [D(D(x(t))) ~ λ * x(t) - D(D(y)) ~ λ * y - g - x(t)^2 + y^2 ~ 1] - cstr = [x(0.5) ~ 1] - @mtkbuild pend = ODESystem(eqs, t; constraints = cstrs) - - tspan = (0.0, 1.5) - u0map = [x(t) => 0.6, y => 0.8] - parammap = [g => 1] - guesses = [λ => 1] - - bvp = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}(pend, u0map, tspan, parammap; guesses, check_length = false) -``` - -If the `ODESystem` has algebraic equations, like `x(t)^2 + y(t)^2`, the resulting -`BVProblem` must be solved using BVDAE solvers, such as Ascher. -""" -function SciMLBase.BVProblem(sys::AbstractODESystem, args...; kwargs...) - BVProblem{true}(sys, args...; kwargs...) -end - -function SciMLBase.BVProblem(sys::AbstractODESystem, - u0map::StaticArray, - args...; - kwargs...) - BVProblem{false, SciMLBase.FullSpecialize}(sys, u0map, args...; kwargs...) -end - -function SciMLBase.BVProblem{true}(sys::AbstractODESystem, args...; kwargs...) - BVProblem{true, SciMLBase.AutoSpecialize}(sys, args...; kwargs...) -end - -function SciMLBase.BVProblem{false}(sys::AbstractODESystem, args...; kwargs...) - BVProblem{false, SciMLBase.FullSpecialize}(sys, args...; kwargs...) -end - -function SciMLBase.BVProblem{iip, specialize}(sys::AbstractODESystem, u0map = [], - tspan = get_tspan(sys), - parammap = DiffEqBase.NullParameters(); - guesses = Dict(), - allow_cost = false, - version = nothing, tgrad = false, - callback = nothing, - check_length = true, - warn_initialize_determined = true, - eval_expression = false, - eval_module = @__MODULE__, - cse = true, - kwargs...) where {iip, specialize} - if !iscomplete(sys) - error("A completed system is required. Call `complete` or `structural_simplify` on the system before creating an `BVProblem`") - end - !isnothing(callback) && error("BVP solvers do not support callbacks.") - - if !isempty(get_costs(sys)) && !allow_cost - error("BVProblem will not optimize solutions of ODESystems that have associated cost functions. - Solvers for optimal control problems are forthcoming. In order to bypass this error (e.g. - to check the cost of a regular solution), pass `allow_cost` = true into the constructor.") - end - - has_alg_eqs(sys) && - error("The BVProblem constructor currently does not support ODESystems with algebraic equations.") # Remove this when the BVDAE solvers get updated, the codegen should work when it does. - - sts = unknowns(sys) - ps = parameters(sys) - constraintsys = get_constraintsystem(sys) - - if !isnothing(constraintsys) - (length(constraints(constraintsys)) + length(u0map) > length(sts)) && - @warn "The BVProblem is overdetermined. The total number of conditions (# constraints + # fixed initial values given by u0map) exceeds the total number of states. The BVP solvers will default to doing a nonlinear least-squares optimization." - end - - # ODESystems without algebraic equations should use both fixed values + guesses - # for initialization. - _u0map = has_alg_eqs(sys) ? u0map : merge(Dict(u0map), Dict(guesses)) - f, u0, p = process_SciMLProblem(ODEFunction{iip, specialize}, sys, _u0map, parammap; - t = tspan !== nothing ? tspan[1] : tspan, guesses, - check_length, warn_initialize_determined, eval_expression, eval_module, cse, kwargs...) - - stidxmap = Dict([v => i for (i, v) in enumerate(sts)]) - u0_idxs = has_alg_eqs(sys) ? collect(1:length(sts)) : [stidxmap[k] for (k, v) in u0map] - - fns = generate_function_bc(sys, u0, u0_idxs, tspan; cse) - bc_oop, bc_iip = eval_or_rgf.(fns; eval_expression, eval_module) - bc(sol, p, t) = bc_oop(sol, p, t) - bc(resid, u, p, t) = bc_iip(resid, u, p, t) - - return BVProblem{iip}(f, bc, u0, tspan, p; kwargs...) -end - -get_callback(prob::BVProblem) = error("BVP solvers do not support callbacks.") - -""" - generate_function_bc(sys::ODESystem, u0, u0_idxs, tspan) - - Given an ODESystem with constraints, generate the boundary condition function to pass to boundary value problem solvers. - Expression uses the constraints and the provided initial conditions. -""" -function generate_function_bc(sys::ODESystem, u0, u0_idxs, tspan; kwargs...) - iv = get_iv(sys) - sts = unknowns(sys) - ps = parameters(sys) - np = length(ps) - ns = length(sts) - stidxmap = Dict([v => i for (i, v) in enumerate(sts)]) - pidxmap = Dict([v => i for (i, v) in enumerate(ps)]) - - @variables sol(..)[1:ns] - - conssys = get_constraintsystem(sys) - cons = Any[] - if !isnothing(conssys) - cons = [con.lhs - con.rhs for con in constraints(conssys)] - - for st in get_unknowns(conssys) - x = operation(st) - t = only(arguments(st)) - idx = stidxmap[x(iv)] - - cons = map(c -> Symbolics.substitute(c, Dict(x(t) => sol(t)[idx])), cons) - end - end - - init_conds = Any[] - for i in u0_idxs - expr = sol(tspan[1])[i] - u0[i] - push!(init_conds, expr) - end - - exprs = vcat(init_conds, cons) - _p = reorder_parameters(sys, ps) - - build_function_wrapper(sys, exprs, sol, _p..., iv; output_type = Array, kwargs...) -end - -""" -```julia -DiffEqBase.DAEProblem{iip}(sys::AbstractODESystem, du0map, u0map, tspan, - parammap = DiffEqBase.NullParameters(); - version = nothing, tgrad = false, - jac = false, - checkbounds = false, sparse = false, - simplify = false, - linenumbers = true, parallel = SerialForm(), - kwargs...) where {iip} -``` - -Generates a DAEProblem from an ODESystem and allows for automatically -symbolically calculating numerical enhancements. - -Note: Solvers for DAEProblems like DFBDF, DImplicitEuler, DABDF2 are -generally slower than the ones for ODEProblems. We recommend trying -ODEProblem and its solvers for your problem first. -""" -function DiffEqBase.DAEProblem(sys::AbstractODESystem, args...; kwargs...) - DAEProblem{true}(sys, args...; kwargs...) -end - -function DiffEqBase.DAEProblem{iip}(sys::AbstractODESystem, du0map, u0map, tspan, - parammap = DiffEqBase.NullParameters(); - allow_cost = false, - warn_initialize_determined = true, - check_length = true, eval_expression = false, eval_module = @__MODULE__, kwargs...) where {iip} - if !iscomplete(sys) - error("A completed system is required. Call `complete` or `structural_simplify` on the system before creating a `DAEProblem`.") - end - - if !isempty(get_costs(sys)) && !allow_cost - error("DAEProblem will not optimize solutions of ODESystems that have associated cost functions. - Solvers for optimal control problems are forthcoming. In order to bypass this error (e.g. - to check the cost of a regular solution), pass `allow_cost` = true into the constructor.") - end - - f, du0, u0, p = process_SciMLProblem(DAEFunction{iip}, sys, u0map, parammap; - implicit_dae = true, du0map = du0map, check_length, - t = tspan !== nothing ? tspan[1] : tspan, - warn_initialize_determined, kwargs...) - diffvars = collect_differential_variables(sys) - sts = unknowns(sys) - differential_vars = map(Base.Fix2(in, diffvars), sts) - kwargs = filter_kwargs(kwargs) - - kwargs1 = (;) - - tstops = SymbolicTstops(sys; eval_expression, eval_module) - if tstops !== nothing - kwargs1 = merge(kwargs1, (; tstops)) - end - - # Call `remake` so it runs initialization if it is trivial - return remake(DAEProblem{iip}( - f, du0, u0, tspan, p; differential_vars = differential_vars, - kwargs..., kwargs1...)) -end - -function generate_history(sys::AbstractODESystem, u0; expression = Val{false}, kwargs...) - p = reorder_parameters(sys) - build_function_wrapper( - sys, u0, p..., get_iv(sys); expression, p_start = 1, p_end = length(p), - similarto = typeof(u0), wrap_delays = false, kwargs...) -end - -function DiffEqBase.DDEProblem(sys::AbstractODESystem, args...; kwargs...) - DDEProblem{true}(sys, args...; kwargs...) -end -function DiffEqBase.DDEProblem{iip}(sys::AbstractODESystem, u0map = [], - tspan = get_tspan(sys), - parammap = DiffEqBase.NullParameters(); - callback = nothing, - check_length = true, - eval_expression = false, - eval_module = @__MODULE__, - u0_constructor = identity, - cse = true, - kwargs...) where {iip} - if !iscomplete(sys) - error("A completed system is required. Call `complete` or `structural_simplify` on the system before creating a `DDEProblem`") - end - f, u0, p = process_SciMLProblem(DDEFunction{iip}, sys, u0map, parammap; - t = tspan !== nothing ? tspan[1] : tspan, - symbolic_u0 = true, u0_constructor, cse, - check_length, eval_expression, eval_module, kwargs...) - h_gen = generate_history(sys, u0; expression = Val{true}, cse) - h_oop, h_iip = eval_or_rgf.(h_gen; eval_expression, eval_module) - h = h_oop - u0 = float.(h(p, tspan[1])) - if u0 !== nothing - u0 = u0_constructor(u0) - end - - cbs = process_events(sys; callback, eval_expression, eval_module, kwargs...) - kwargs = filter_kwargs(kwargs) - - kwargs1 = (;) - if cbs !== nothing - kwargs1 = merge(kwargs1, (callback = cbs,)) - end - # Call `remake` so it runs initialization if it is trivial - return remake(DDEProblem{iip}(f, u0, h, tspan, p; kwargs1..., kwargs...)) -end - -function DiffEqBase.SDDEProblem(sys::AbstractODESystem, args...; kwargs...) - SDDEProblem{true}(sys, args...; kwargs...) -end -function DiffEqBase.SDDEProblem{iip}(sys::AbstractODESystem, u0map = [], - tspan = get_tspan(sys), - parammap = DiffEqBase.NullParameters(); - callback = nothing, - check_length = true, - sparsenoise = nothing, - eval_expression = false, - eval_module = @__MODULE__, - u0_constructor = identity, - cse = true, - kwargs...) where {iip} - if !iscomplete(sys) - error("A completed system is required. Call `complete` or `structural_simplify` on the system before creating a `SDDEProblem`") - end - f, u0, p = process_SciMLProblem(SDDEFunction{iip}, sys, u0map, parammap; - t = tspan !== nothing ? tspan[1] : tspan, - symbolic_u0 = true, eval_expression, eval_module, u0_constructor, - check_length, cse, kwargs...) - h_gen = generate_history(sys, u0; expression = Val{true}, cse) - h_oop, h_iip = eval_or_rgf.(h_gen; eval_expression, eval_module) - h = h_oop - u0 = h(p, tspan[1]) - if u0 !== nothing - u0 = u0_constructor(u0) - end - - cbs = process_events(sys; callback, eval_expression, eval_module, kwargs...) - kwargs = filter_kwargs(kwargs) - - kwargs1 = (;) - if cbs !== nothing - kwargs1 = merge(kwargs1, (callback = cbs,)) - end - - noiseeqs = get_noiseeqs(sys) - sparsenoise === nothing && (sparsenoise = get(kwargs, :sparse, false)) - if noiseeqs isa AbstractVector - noise_rate_prototype = nothing - elseif sparsenoise - I, J, V = findnz(SparseArrays.sparse(noiseeqs)) - noise_rate_prototype = SparseArrays.sparse(I, J, zero(eltype(u0))) - else - noise_rate_prototype = zeros(eltype(u0), size(noiseeqs)) - end - # Call `remake` so it runs initialization if it is trivial - return remake(SDDEProblem{iip}(f, f.g, u0, h, tspan, p; - noise_rate_prototype = - noise_rate_prototype, kwargs1..., kwargs...)) -end - -""" -```julia -ODEProblemExpr{iip}(sys::AbstractODESystem, u0map, tspan, - parammap = DiffEqBase.NullParameters(); - version = nothing, tgrad = false, - jac = false, - checkbounds = false, sparse = false, - linenumbers = true, parallel = SerialForm(), - skipzeros = true, fillzeros = true, - simplify = false, - kwargs...) where {iip} -``` - -Generates a Julia expression for constructing an ODEProblem from an -ODESystem and allows for automatically symbolically calculating -numerical enhancements. -""" -struct ODEProblemExpr{iip} end - -function ODEProblemExpr{iip}(sys::AbstractODESystem, u0map, tspan, - parammap = DiffEqBase.NullParameters(); check_length = true, - kwargs...) where {iip} - if !iscomplete(sys) - error("A completed system is required. Call `complete` or `structural_simplify` on the system before creating a `ODEProblemExpr`") - end - f, u0, p = process_SciMLProblem( - ODEFunctionExpr{iip}, sys, u0map, parammap; check_length, - t = tspan !== nothing ? tspan[1] : tspan, - kwargs...) - linenumbers = get(kwargs, :linenumbers, true) - kwargs = filter_kwargs(kwargs) - kwarg_params = gen_quoted_kwargs(kwargs) - odep = Expr(:call, :ODEProblem, kwarg_params, :f, :u0, :tspan, :p) - ex = quote - f = $f - u0 = $u0 - tspan = $tspan - p = $p - $odep - end - !linenumbers ? Base.remove_linenums!(ex) : ex -end - -function ODEProblemExpr(sys::AbstractODESystem, args...; kwargs...) - ODEProblemExpr{true}(sys, args...; kwargs...) -end - -""" -```julia -DAEProblemExpr{iip}(sys::AbstractODESystem, u0map, tspan, - parammap = DiffEqBase.NullParameters(); - version = nothing, tgrad = false, - jac = false, - checkbounds = false, sparse = false, - linenumbers = true, parallel = SerialForm(), - skipzeros = true, fillzeros = true, - simplify = false, - kwargs...) where {iip} -``` - -Generates a Julia expression for constructing a DAEProblem from an -ODESystem and allows for automatically symbolically calculating -numerical enhancements. -""" -struct DAEProblemExpr{iip} end - -function DAEProblemExpr{iip}(sys::AbstractODESystem, du0map, u0map, tspan, - parammap = DiffEqBase.NullParameters(); check_length = true, - kwargs...) where {iip} - if !iscomplete(sys) - error("A completed system is required. Call `complete` or `structural_simplify` on the system before creating a `DAEProblemExpr`") - end - f, du0, u0, p = process_SciMLProblem(DAEFunctionExpr{iip}, sys, u0map, parammap; - t = tspan !== nothing ? tspan[1] : tspan, - implicit_dae = true, du0map = du0map, check_length, - kwargs...) - linenumbers = get(kwargs, :linenumbers, true) - diffvars = collect_differential_variables(sys) - sts = unknowns(sys) - differential_vars = map(Base.Fix2(in, diffvars), sts) - kwargs = filter_kwargs(kwargs) - kwarg_params = gen_quoted_kwargs(kwargs) - push!(kwarg_params.args, Expr(:kw, :differential_vars, :differential_vars)) - prob = Expr(:call, :(DAEProblem{$iip}), kwarg_params, :f, :du0, :u0, :tspan, :p) - ex = quote - f = $f - u0 = $u0 - du0 = $du0 - tspan = $tspan - p = $p - differential_vars = $differential_vars - $prob - end - !linenumbers ? Base.remove_linenums!(ex) : ex -end - -function DAEProblemExpr(sys::AbstractODESystem, args...; kwargs...) - DAEProblemExpr{true}(sys, args...; kwargs...) -end - -""" -```julia -SciMLBase.SteadyStateProblem(sys::AbstractODESystem, u0map, - parammap = DiffEqBase.NullParameters(); - version = nothing, tgrad = false, - jac = false, - checkbounds = false, sparse = false, - linenumbers = true, parallel = SerialForm(), - kwargs...) where {iip} -``` - -Generates an SteadyStateProblem from an ODESystem and allows for automatically -symbolically calculating numerical enhancements. -""" -function SciMLBase.SteadyStateProblem(sys::AbstractODESystem, args...; kwargs...) - SteadyStateProblem{true}(sys, args...; kwargs...) -end - -function DiffEqBase.SteadyStateProblem{iip}(sys::AbstractODESystem, u0map, - parammap = SciMLBase.NullParameters(); - check_length = true, kwargs...) where {iip} - if !iscomplete(sys) - error("A completed system is required. Call `complete` or `structural_simplify` on the system before creating a `SteadyStateProblem`") - end - f, u0, p = process_SciMLProblem(ODEFunction{iip}, sys, u0map, parammap; - steady_state = true, - check_length, force_initialization_time_independent = true, kwargs...) - kwargs = filter_kwargs(kwargs) - SteadyStateProblem{iip}(f, u0, p; kwargs...) -end - -""" -```julia -SciMLBase.SteadyStateProblemExpr(sys::AbstractODESystem, u0map, - parammap = DiffEqBase.NullParameters(); - version = nothing, tgrad = false, - jac = false, - checkbounds = false, sparse = false, - skipzeros = true, fillzeros = true, - linenumbers = true, parallel = SerialForm(), - kwargs...) where {iip} -``` - -Generates a Julia expression for building a SteadyStateProblem from -an ODESystem and allows for automatically symbolically calculating -numerical enhancements. -""" -struct SteadyStateProblemExpr{iip} end - -function SteadyStateProblemExpr{iip}(sys::AbstractODESystem, u0map, - parammap = SciMLBase.NullParameters(); - check_length = true, - kwargs...) where {iip} - if !iscomplete(sys) - error("A completed system is required. Call `complete` or `structural_simplify` on the system before creating a `SteadyStateProblemExpr`") - end - f, u0, p = process_SciMLProblem(ODEFunctionExpr{iip}, sys, u0map, parammap; - steady_state = true, - check_length, kwargs...) - linenumbers = get(kwargs, :linenumbers, true) - kwargs = filter_kwargs(kwargs) - kwarg_params = gen_quoted_kwargs(kwargs) - prob = Expr(:call, :SteadyStateProblem, kwarg_params, :f, :u0, :p) - ex = quote - f = $f - u0 = $u0 - p = $p - $prob - end - !linenumbers ? Base.remove_linenums!(ex) : ex -end - -function SteadyStateProblemExpr(sys::AbstractODESystem, args...; kwargs...) - SteadyStateProblemExpr{true}(sys, args...; kwargs...) -end - -function _match_eqs(eqs1, eqs2) - eqpairs = Pair[] - for (i, eq) in enumerate(eqs1) - for (j, eq2) in enumerate(eqs2) - if isequal(eq, eq2) - push!(eqpairs, i => j) - break - end - end - end - eqpairs -end - -function isisomorphic(sys1::AbstractODESystem, sys2::AbstractODESystem) - sys1 = flatten(sys1) - sys2 = flatten(sys2) - - iv2 = only(independent_variables(sys2)) - sys1 = convert_system(ODESystem, sys1, iv2) - s1, s2 = unknowns(sys1), unknowns(sys2) - p1, p2 = parameters(sys1), parameters(sys2) - - (length(s1) != length(s2)) || (length(p1) != length(p2)) && return false - - eqs1 = equations(sys1) - eqs2 = equations(sys2) - - pps = permutations(p2) - psts = permutations(s2) - orig = [p1; s1] - perms = ([x; y] for x in pps for y in psts) - - for perm in perms - rules = Dict(orig .=> perm) - neweqs1 = substitute(eqs1, rules) - eqpairs = _match_eqs(neweqs1, eqs2) - if length(eqpairs) == length(eqs1) - return true - end - end - return false -end - -function flatten_equations(eqs) - mapreduce(vcat, eqs; init = Equation[]) do eq - islhsarr = eq.lhs isa AbstractArray || Symbolics.isarraysymbolic(eq.lhs) - isrhsarr = eq.rhs isa AbstractArray || Symbolics.isarraysymbolic(eq.rhs) - if islhsarr || isrhsarr - islhsarr && isrhsarr || - error("LHS ($(eq.lhs)) and RHS ($(eq.rhs)) must either both be array expressions or both scalar") - size(eq.lhs) == size(eq.rhs) || - error("Size of LHS ($(eq.lhs)) and RHS ($(eq.rhs)) must match: got $(size(eq.lhs)) and $(size(eq.rhs))") - return vec(collect(eq.lhs) .~ collect(eq.rhs)) - else - eq - end - end -end - -struct InitializationProblem{iip, specialization} end - -""" -```julia -InitializationProblem{iip}(sys::AbstractODESystem, t, u0map, - parammap = DiffEqBase.NullParameters(); - version = nothing, tgrad = false, - jac = false, - checkbounds = false, sparse = false, - simplify = false, - linenumbers = true, parallel = SerialForm(), - initialization_eqs = [], - fully_determined = false, - kwargs...) where {iip} -``` - -Generates a NonlinearProblem or NonlinearLeastSquaresProblem from an ODESystem -which represents the initialization, i.e. the calculation of the consistent -initial conditions for the given DAE. -""" -function InitializationProblem(sys::AbstractSystem, args...; kwargs...) - InitializationProblem{true}(sys, args...; kwargs...) -end - -function InitializationProblem(sys::AbstractSystem, t, - u0map::StaticArray, - args...; - kwargs...) - InitializationProblem{false, SciMLBase.FullSpecialize}( - sys, t, u0map, args...; kwargs...) -end - -function InitializationProblem{true}(sys::AbstractSystem, args...; kwargs...) - InitializationProblem{true, SciMLBase.AutoSpecialize}(sys, args...; kwargs...) -end - -function InitializationProblem{false}(sys::AbstractSystem, args...; kwargs...) - InitializationProblem{false, SciMLBase.FullSpecialize}(sys, args...; kwargs...) -end - -const INCOMPLETE_INITIALIZATION_MESSAGE = """ - Initialization incomplete. Not all of the state variables of the - DAE system can be determined by the initialization. Missing - variables: - """ - -struct IncompleteInitializationError <: Exception - uninit::Any -end - -function Base.showerror(io::IO, e::IncompleteInitializationError) - println(io, INCOMPLETE_INITIALIZATION_MESSAGE) - println(io, e.uninit) -end - -function InitializationProblem{iip, specialize}(sys::AbstractSystem, - t, u0map = [], - parammap = DiffEqBase.NullParameters(); - guesses = [], - check_length = true, - warn_initialize_determined = true, - initialization_eqs = [], - fully_determined = nothing, - check_units = true, - use_scc = true, - allow_incomplete = false, - force_time_independent = false, - algebraic_only = false, - kwargs...) where {iip, specialize} - if !iscomplete(sys) - error("A completed system is required. Call `complete` or `structural_simplify` on the system before creating an `ODEProblem`") - end - if isempty(u0map) && get_initializesystem(sys) !== nothing - isys = get_initializesystem(sys; initialization_eqs, check_units) - simplify_system = false - elseif isempty(u0map) && get_initializesystem(sys) === nothing - isys = generate_initializesystem( - sys; initialization_eqs, check_units, pmap = parammap, - guesses, algebraic_only) - simplify_system = true - else - isys = generate_initializesystem( - sys; u0map, initialization_eqs, check_units, - pmap = parammap, guesses, algebraic_only) - simplify_system = true - end - - # useful for `SteadyStateProblem` since `f` has to be autonomous and the - # initialization should be too - if force_time_independent - idx = findfirst(isequal(get_iv(sys)), get_ps(isys)) - idx === nothing || deleteat!(get_ps(isys), idx) - end - - if simplify_system - isys = structural_simplify(isys; fully_determined, split = is_split(sys)) - end - - ts = get_tearing_state(isys) - unassigned_vars = StructuralTransformations.singular_check(ts) - if warn_initialize_determined && !isempty(unassigned_vars) - errmsg = """ - The initialization system is structurally singular. Guess values may \ - significantly affect the initial values of the ODE. The problematic variables \ - are $unassigned_vars. - - Note that the identification of problematic variables is a best-effort heuristic. - """ - @warn errmsg - end - - uninit = setdiff(unknowns(sys), [unknowns(isys); observables(isys)]) - - # TODO: throw on uninitialized arrays - filter!(x -> !(x isa Symbolics.Arr), uninit) - if is_time_dependent(sys) && !isempty(uninit) - allow_incomplete || throw(IncompleteInitializationError(uninit)) - # for incomplete initialization, we will add the missing variables as parameters. - # they will be updated by `update_initializeprob!` and `initializeprobmap` will - # use them to construct the new `u0`. - newparams = map(toparam, uninit) - append!(get_ps(isys), newparams) - isys = complete(isys) - end - - neqs = length(equations(isys)) - nunknown = length(unknowns(isys)) - - if use_scc - scc_message = "`SCCNonlinearProblem` can only be used for initialization of fully determined systems and hence will not be used here. " - else - scc_message = "" - end - - if warn_initialize_determined && neqs > nunknown - @warn "Initialization system is overdetermined. $neqs equations for $nunknown unknowns. Initialization will default to using least squares. $(scc_message)To suppress this warning pass warn_initialize_determined = false. To make this warning into an error, pass fully_determined = true" - end - if warn_initialize_determined && neqs < nunknown - @warn "Initialization system is underdetermined. $neqs equations for $nunknown unknowns. Initialization will default to using least squares. $(scc_message)To suppress this warning pass warn_initialize_determined = false. To make this warning into an error, pass fully_determined = true" - end - - parammap = recursive_unwrap(anydict(parammap)) - if t !== nothing - parammap[get_iv(sys)] = t - end - filter!(kvp -> kvp[2] !== missing, parammap) - - u0map = to_varmap(u0map, unknowns(sys)) - if isempty(guesses) - guesses = Dict() - end - - filter_missing_values!(u0map) - filter_missing_values!(parammap) - u0map = merge(ModelingToolkit.guesses(sys), todict(guesses), u0map) - - TProb = if neqs == nunknown && isempty(unassigned_vars) - if use_scc && neqs > 0 - if is_split(isys) - SCCNonlinearProblem - else - @warn "`SCCNonlinearProblem` can only be used with `split = true` systems. Simplify your `ODESystem` with `split = true` or pass `use_scc = false` to disable this warning" - NonlinearProblem - end - else - NonlinearProblem - end - else - NonlinearLeastSquaresProblem - end - TProb{iip}(isys, u0map, parammap; kwargs..., - build_initializeprob = false, is_initializeprob = true) -end From 1dcc454e27527cb757adf49fecc3239394a244a4 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 14 May 2025 13:50:38 +0530 Subject: [PATCH 1630/2176] refactor: do not rely on `ArrayPartition` when unit-checking jumps --- src/systems/unit_check.jl | 8 ++++++-- src/systems/validation.jl | 9 +++++++-- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/src/systems/unit_check.jl b/src/systems/unit_check.jl index 83a5ac5483..acf7451065 100644 --- a/src/systems/unit_check.jl +++ b/src/systems/unit_check.jl @@ -267,9 +267,13 @@ function validate(jump::MassActionJump, t::Symbolic; info::String = "") ["scaled_rates", "1/(t*reactants^$n))"]; info) end -function validate(jumps::ArrayPartition{<:Union{Any, Vector{<:JumpType}}}, t::Symbolic) +function validate(jumps::Vector{JumpType}, t::Symbolic) labels = ["in Mass Action Jumps,", "in Constant Rate Jumps,", "in Variable Rate Jumps,"] - all([validate(jumps.x[idx], t, info = labels[idx]) for idx in 1:3]) + majs = filter(x -> x isa MassActionJump, jumps) + crjs = filter(x -> x isa ConstantRateJump, jumps) + vrjs = filter(x -> x isa VariableRateJump, jumps) + splitjumps = [majs, crjs, vrjs] + all([validate(js, t; info) for (js, info) in zip(splitjumps, labels)]) end function validate(eq::Union{Inequality, Equation}; info::String = "") diff --git a/src/systems/validation.jl b/src/systems/validation.jl index 84dd3b07e5..d416a02ea2 100644 --- a/src/systems/validation.jl +++ b/src/systems/validation.jl @@ -5,6 +5,7 @@ using ..ModelingToolkit: ValidationError, ModelingToolkit, Connection, instream, JumpType, VariableUnit, get_systems, Conditional, Comparison +using JumpProcesses: MassActionJump, ConstantRateJump, VariableRateJump using Symbolics: Symbolic, value, issym, isadd, ismul, ispow const MT = ModelingToolkit @@ -231,9 +232,13 @@ function validate(jump::MT.MassActionJump, t::Symbolic; info::String = "") ["scaled_rates", "1/(t*reactants^$n))"]; info) end -function validate(jumps::ArrayPartition{<:Union{Any, Vector{<:JumpType}}}, t::Symbolic) +function validate(jumps::Vector{JumpType}, t::Symbolic) labels = ["in Mass Action Jumps,", "in Constant Rate Jumps,", "in Variable Rate Jumps,"] - all([validate(jumps.x[idx], t, info = labels[idx]) for idx in 1:3]) + majs = filter(x -> x isa MassActionJump, jumps) + crjs = filter(x -> x isa ConstantRateJump, jumps) + vrjs = filter(x -> x isa VariableRateJump, jumps) + splitjumps = [majs, crjs, vrjs] + all([validate(js, t; info) for (js, info) in zip(splitjumps, labels)]) end function validate(eq::MT.Equation; info::String = "") From 74a21539369fac1318336886659730f6f0d41916 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 14 May 2025 12:23:23 +0530 Subject: [PATCH 1631/2176] feat: add unified `System` type feat: add unified `System` type feat: add `is_discrete_system` feat: add `is_dde` to `System` refactor: change defaults of `costs` and `consolidate` feat: implement `is_time_dependent` for `System` feat: add `check_complete` feat: add `flatten(::System)` feat: add `isscheduled` to `System` feat: implement `Base.:(==)` for `System` feat: implement `supports_initialization(::System)` feat: add `schedule` field in `System` refactor: improve `System` constructors fix: fix `==` and `hash` implementations for `System` feat: add utility constructors for `OptimizationSystem` and `JumpSystem` feat: add utility constructor for `SDESystem` fix: fix error message for events in time-independent systems refactor: do not use `process_equations` in `System` constructor fix: fix default `costs` in `System` constructor feat: add `initializesystem` field to `System` fix: fix brownians passed to `System` constructor fix: fix `flatten(::System)` feat: add `System(::Equation, ...)` constructor fix: unwrap costs in `System` constructor fix: fix noise equations unit checking fix: convert `constraints` to appropriate type fix: correctly order unknowns in `System` constructor feat: add `preface` to `System` fix: respect scoping in `System` constructor variable discovery fix: fix `flatten(::System)` feat: add `OptimizationSystem` ctor where cost is an array --- src/ModelingToolkit.jl | 4 +- src/systems/abstractsystem.jl | 17 - src/systems/system.jl | 779 ++++++++++++++++++++++++++++++++++ 3 files changed, 782 insertions(+), 18 deletions(-) create mode 100644 src/systems/system.jl diff --git a/src/ModelingToolkit.jl b/src/ModelingToolkit.jl index 9dd89d452c..9f8b19aa1d 100644 --- a/src/ModelingToolkit.jl +++ b/src/ModelingToolkit.jl @@ -160,6 +160,8 @@ include("systems/model_parsing.jl") include("systems/connectors.jl") include("systems/analysis_points.jl") include("systems/imperative_affect.jl") +include("systems/callbacks.jl") +include("systems/system.jl") include("systems/codegen_utils.jl") include("systems/problem_utils.jl") include("linearization.jl") @@ -255,7 +257,7 @@ export AbstractTimeDependentSystem, AbstractMultivariateSystem export ODEFunction, ODEFunctionExpr, ODEProblemExpr, convert_system, - add_accumulations, System + System, OptimizationSystem, JumpSystem, SDESystem export DAEFunctionExpr, DAEProblemExpr export SDEFunction, SDEFunctionExpr, SDEProblemExpr export SystemStructure diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index 1da2a8ff73..6b5ede1369 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -2579,23 +2579,6 @@ end ### ### Inheritance & composition ### -function Base.hash(sys::AbstractSystem, s::UInt) - s = hash(nameof(sys), s) - s = foldr(hash, get_systems(sys), init = s) - s = foldr(hash, get_unknowns(sys), init = s) - s = foldr(hash, get_ps(sys), init = s) - if sys isa OptimizationSystem - s = hash(get_op(sys), s) - else - s = foldr(hash, get_eqs(sys), init = s) - end - s = foldr(hash, get_observed(sys), init = s) - s = foldr(hash, get_continuous_events(sys), init = s) - s = foldr(hash, get_discrete_events(sys), init = s) - s = hash(independent_variables(sys), s) - return s -end - """ $(TYPEDSIGNATURES) diff --git a/src/systems/system.jl b/src/systems/system.jl new file mode 100644 index 0000000000..d89bdb0f8e --- /dev/null +++ b/src/systems/system.jl @@ -0,0 +1,779 @@ +struct Schedule{V <: BipartiteGraphs.Matching} + """ + Maximal matching of variables to equations calculated during structural simplification. + """ + var_eq_matching::V + """ + Mapping of `Differential`s of variables to corresponding derivative expressions. + """ + dummy_sub::Dict{Any, Any} +end + +struct System <: AbstractSystem + tag::UInt + eqs::Vector{Equation} + # nothing - no noise + # vector - diagonal noise + # matrix - generic form + # column matrix - scalar noise + noise_eqs::Union{Nothing, AbstractVector, AbstractMatrix} + jumps::Vector{JumpType} + constraints::Vector{Union{Equation, Inequality}} + costs::Vector{<:BasicSymbolic} + consolidate::Any + unknowns::Vector + ps::Vector + brownians::Vector + iv::Union{Nothing, BasicSymbolic{Real}} + observed::Vector{Equation} + parameter_dependencies::Vector{Equation} + var_to_name::Dict{Symbol, Any} + name::Symbol + description::String + defaults::Dict + guesses::Dict + systems::Vector{System} + initialization_eqs::Vector{Equation} + continuous_events::Vector{SymbolicContinuousCallback} + discrete_events::Vector{SymbolicDiscreteCallback} + connector_type::Any + assertions::Dict{BasicSymbolic, String} + metadata::Any + gui_metadata::Any # ? + is_dde::Bool + tstops::Vector{Any} + tearing_state::Any + namespacing::Bool + complete::Bool + index_cache::Union{Nothing, IndexCache} + ignored_connections::Union{ + Nothing, Tuple{Vector{IgnoredAnalysisPoint}, Vector{IgnoredAnalysisPoint}}} + preface::Any + parent::Union{Nothing, System} + initializesystem::Union{Nothing, System} + is_initializesystem::Bool + isscheduled::Bool + schedule::Union{Schedule, Nothing} + + function System( + tag, eqs, noise_eqs, jumps, constraints, costs, consolidate, unknowns, ps, + brownians, iv, observed, parameter_dependencies, var_to_name, name, description, + defaults, guesses, systems, initialization_eqs, continuous_events, discrete_events, + connector_type, assertions = Dict{BasicSymbolic, String}(), + metadata = nothing, gui_metadata = nothing, + is_dde = false, tstops = [], tearing_state = nothing, namespacing = true, + complete = false, index_cache = nothing, ignored_connections = nothing, + preface = nothing, parent = nothing, initializesystem = nothing, + is_initializesystem = false, isscheduled = false, schedule = nothing; + checks::Union{Bool, Int} = true) + if is_initializesystem && iv !== nothing + throw(ArgumentError(""" + Expected initialization system to be time-independent. Found independent + variable $iv. + """)) + end + jumps = Vector{JumpType}(jumps) + if (checks == true || (checks & CheckComponents) > 0) && iv !== nothing + check_independent_variables([iv]) + check_variables(unknowns, iv) + check_parameters(ps, iv) + check_equations(eqs, iv) + if noise_eqs !== nothing && size(noise_eqs, 1) != length(eqs) + throw(IllFormedNoiseEquationsError(size(noise_eqs, 1), length(eqs))) + end + check_equations(equations(continuous_events), iv) + check_subsystems(systems) + end + if checks == true || (checks & CheckUnits) > 0 + u = __get_unit_type(unknowns, ps, iv) + if noise_eqs === nothing + check_units(u, eqs) + else + check_units(u, eqs, noise_eqs) + end + if iv !== nothing + check_units(u, jumps, iv) + end + isempty(constraints) || check_units(u, constraints) + end + new(tag, eqs, noise_eqs, jumps, constraints, costs, + consolidate, unknowns, ps, brownians, iv, + observed, parameter_dependencies, var_to_name, name, description, defaults, + guesses, systems, initialization_eqs, continuous_events, discrete_events, + connector_type, assertions, metadata, gui_metadata, is_dde, + tstops, tearing_state, namespacing, complete, index_cache, ignored_connections, + preface, parent, initializesystem, is_initializesystem, isscheduled, schedule) + end +end + +function default_consolidate(costs, subcosts) + return sum(costs; init = 0.0) + sum(subcosts; init = 0.0) +end + +function System(eqs::Vector{Equation}, iv, dvs, ps, brownians = []; + constraints = Union{Equation, Inequality}[], noise_eqs = nothing, jumps = [], + costs = BasicSymbolic[], consolidate = default_consolidate, + observed = Equation[], parameter_dependencies = Equation[], defaults = Dict(), + guesses = Dict(), systems = System[], initialization_eqs = Equation[], + continuous_events = SymbolicContinuousCallback[], discrete_events = SymbolicDiscreteCallback[], + connector_type = nothing, assertions = Dict{BasicSymbolic, String}(), + metadata = nothing, gui_metadata = nothing, is_dde = nothing, tstops = [], + tearing_state = nothing, ignored_connections = nothing, parent = nothing, + description = "", name = nothing, discover_from_metadata = true, + initializesystem = nothing, is_initializesystem = false, preface = [], + checks = true) + name === nothing && throw(NoNameError()) + + iv = unwrap(iv) + ps = unwrap.(ps) + dvs = unwrap.(dvs) + filter!(!Base.Fix2(isdelay, iv), dvs) + brownians = unwrap.(brownians) + + if !(eqs isa AbstractArray) + eqs = [eqs] + end + + if noise_eqs !== nothing + noise_eqs = unwrap.(noise_eqs) + end + + costs = unwrap.(costs) + if isempty(costs) + costs = Union{BasicSymbolic, Real}[] + end + + parameter_dependencies, ps = process_parameter_dependencies(parameter_dependencies, ps) + defaults = anydict(defaults) + guesses = anydict(guesses) + var_to_name = anydict() + + let defaults = discover_from_metadata ? defaults : Dict(), + guesses = discover_from_metadata ? guesses : Dict() + + process_variables!(var_to_name, defaults, guesses, dvs) + process_variables!(var_to_name, defaults, guesses, ps) + process_variables!( + var_to_name, defaults, guesses, [eq.lhs for eq in parameter_dependencies]) + process_variables!( + var_to_name, defaults, guesses, [eq.rhs for eq in parameter_dependencies]) + process_variables!(var_to_name, defaults, guesses, [eq.lhs for eq in observed]) + process_variables!(var_to_name, defaults, guesses, [eq.rhs for eq in observed]) + end + filter!(!(isnothing ∘ last), defaults) + filter!(!(isnothing ∘ last), guesses) + defaults = anydict([unwrap(k) => unwrap(v) for (k, v) in defaults]) + guesses = anydict([unwrap(k) => unwrap(v) for (k, v) in guesses]) + + sysnames = nameof.(systems) + unique_sysnames = Set(sysnames) + if length(unique_sysnames) != length(sysnames) + throw(NonUniqueSubsystemsError(sysnames, unique_sysnames)) + end + continuous_events, discrete_events = create_symbolic_events( + continuous_events, discrete_events, eqs, iv) + + if iv === nothing && (!isempty(continuous_events) || !isempty(discrete_events)) + throw(EventsInTimeIndependentSystemError(continuous_events, discrete_events)) + end + + if is_dde === nothing + is_dde = _check_if_dde(eqs, iv, systems) + end + + assertions = Dict{BasicSymbolic, String}(unwrap(k) => v for (k, v) in assertions) + + System(Threads.atomic_add!(SYSTEM_COUNT, UInt(1)), eqs, noise_eqs, jumps, constraints, + costs, consolidate, dvs, ps, brownians, iv, observed, parameter_dependencies, + var_to_name, name, description, defaults, guesses, systems, initialization_eqs, + continuous_events, discrete_events, connector_type, assertions, metadata, gui_metadata, is_dde, + tstops, tearing_state, true, false, nothing, ignored_connections, preface, parent, + initializesystem, is_initializesystem; checks) +end + +function System(eqs::Vector{Equation}, dvs, ps; kwargs...) + System(eqs, nothing, dvs, ps; kwargs...) +end + +function System(eqs::Vector{Equation}, iv; kwargs...) + iv === nothing && return System(eqs; kwargs...) + + diffvars = OrderedSet() + othervars = OrderedSet() + ps = Set() + diffeqs = Equation[] + othereqs = Equation[] + for eq in eqs + if !(eq.lhs isa Union{Symbolic, Number}) + push!(othereqs, eq) + continue + end + collect_vars!(othervars, ps, eq, iv) + if iscall(eq.lhs) && operation(eq.lhs) isa Differential + var, _ = var_from_nested_derivative(eq.lhs) + if var in diffvars + throw(ArgumentError(""" + The differential variable $var is not unique in the system of \ + equations. + """)) + end + # this check ensures var is correctly scoped, since `collect_vars!` won't pick + # it up if it belongs to an ancestor system. + if var in othervars + push!(diffvars, var) + end + push!(diffeqs, eq) + else + push!(othereqs, eq) + end + end + + allunknowns = union(diffvars, othervars) + eqs = [diffeqs; othereqs] + + brownians = Set() + for x in allunknowns + x = unwrap(x) + if getvariabletype(x) == BROWNIAN + push!(brownians, x) + end + end + setdiff!(allunknowns, brownians) + + for eq in get(kwargs, :parameter_dependencies, Equation[]) + collect_vars!(allunknowns, ps, eq, iv) + end + + cstrs = Vector{Union{Equation, Inequality}}(get(kwargs, :constraints, [])) + cstrunknowns, cstrps = process_constraint_system(cstrs, allunknowns, ps, iv) + union!(allunknowns, cstrunknowns) + union!(ps, cstrps) + + for ssys in get(kwargs, :systems, System[]) + collect_scoped_vars!(allunknowns, ps, ssys, iv) + end + + costs = get(kwargs, :costs, nothing) + if costs !== nothing + costunknowns, costps = process_costs(costs, allunknowns, ps, iv) + union!(allunknowns, costunknowns) + union!(ps, costps) + end + + for v in allunknowns + isdelay(v, iv) || continue + collect_vars!(allunknowns, ps, arguments(v)[1], iv) + end + + new_ps = gather_array_params(ps) + + noiseeqs = get(kwargs, :noise_eqs, nothing) + if noiseeqs !== nothing + # validate noise equations + noisedvs = OrderedSet() + noiseps = OrderedSet() + collect_vars!(noisedvs, noiseps, noiseeqs, iv) + for dv in noisedvs + dv ∈ allunknowns || + throw(ArgumentError("Variable $dv in noise equations is not an unknown of the system.")) + end + end + + return System( + eqs, iv, collect(allunknowns), collect(new_ps), collect(brownians); kwargs...) +end + +function System(eqs::Vector{Equation}; kwargs...) + eqs = collect(eqs) + + allunknowns = OrderedSet() + ps = OrderedSet() + for eq in eqs + collect_vars!(allunknowns, ps, eq, nothing) + end + for eq in get(kwargs, :parameter_dependencies, Equation[]) + collect_vars!(allunknowns, ps, eq, nothing) + end + for ssys in get(kwargs, :systems, System[]) + collect_scoped_vars!(allunknowns, ps, ssys, nothing) + end + costs = get(kwargs, :costs, nothing) + if costs !== nothing + costunknowns, costps = process_costs(costs, allunknowns, ps, nothing) + union!(allunknowns, costunknowns) + union!(ps, costps) + end + cstrs = Vector{Union{Equation, Inequality}}(get(kwargs, :constraints, [])) + for eq in cstrs + collect_vars!(allunknowns, ps, eq, nothing) + end + + new_ps = gather_array_params(ps) + + return System(eqs, nothing, collect(allunknowns), collect(new_ps); kwargs...) +end + +System(eq::Equation, args...; kwargs...) = System([eq], args...; kwargs...) + +function gather_array_params(ps) + new_ps = OrderedSet() + for p in ps + if iscall(p) && operation(p) === getindex + par = arguments(p)[begin] + if Symbolics.shape(Symbolics.unwrap(par)) !== Symbolics.Unknown() && + all(par[i] in ps for i in eachindex(par)) + push!(new_ps, par) + else + push!(new_ps, p) + end + else + if symbolic_type(p) == ArraySymbolic() && + Symbolics.shape(unwrap(p)) != Symbolics.Unknown() + for i in eachindex(p) + delete!(new_ps, p[i]) + end + end + push!(new_ps, p) + end + end + return new_ps +end + +""" +Process variables in constraints of the (ODE) System. +""" +function process_constraint_system( + constraints::Vector{Union{Equation, Inequality}}, sts, ps, iv; validate = true) + isempty(constraints) && return Set(), Set() + + constraintsts = OrderedSet() + constraintps = OrderedSet() + for cons in constraints + collect_vars!(constraintsts, constraintps, cons, iv) + union!(constraintsts, collect_applied_operators(cons, Differential)) + end + + # Validate the states. + if validate + validate_vars_and_find_ps!(constraintsts, constraintps, sts, iv) + end + + return constraintsts, constraintps +end + +""" +Process the costs for the constraint system. +""" +function process_costs(costs::Vector, sts, ps, iv) + coststs = OrderedSet() + costps = OrderedSet() + for cost in costs + collect_vars!(coststs, costps, cost, iv) + end + + validate_vars_and_find_ps!(coststs, costps, sts, iv) + coststs, costps +end + +""" +Validate that all the variables in an auxiliary system of the (ODE) System (constraint or costs) are +well-formed states or parameters. + - Callable/delay variables (e.g. of the form x(0.6) should be unknowns of the system (and have one arg, etc.) + - Callable/delay parameters should be parameters of the system + +Return the set of additional parameters found in the system, e.g. in x(p) ~ 3 then p should be added as a +parameter of the system. +""" +function validate_vars_and_find_ps!(auxvars, auxps, sysvars, iv) + sts = sysvars + + for var in auxvars + if !iscall(var) + occursin(iv, var) && (var ∈ sts || + throw(ArgumentError("Time-dependent variable $var is not an unknown of the system."))) + elseif length(arguments(var)) > 1 + throw(ArgumentError("Too many arguments for variable $var.")) + elseif length(arguments(var)) == 1 + if iscall(var) && operation(var) isa Differential + var = only(arguments(var)) + end + arg = only(arguments(var)) + operation(var)(iv) ∈ sts || + throw(ArgumentError("Variable $var is not a variable of the System. Called variables must be variables of the System.")) + + isequal(arg, iv) || isparameter(arg) || arg isa Integer || + arg isa AbstractFloat || + throw(ArgumentError("Invalid argument specified for variable $var. The argument of the variable should be either $iv, a parameter, or a value specifying the time that the constraint holds.")) + + isparameter(arg) && !isequal(arg, iv) && push!(auxps, arg) + else + var ∈ sts && + @warn "Variable $var has no argument. It will be interpreted as $var($iv), and the constraint will apply to the entire interval." + end + end +end + +""" + $(TYPEDSIGNATURES) + +Check if a system is a (possibly implicit) discrete system. Hybrid systems are turned into +callbacks, so checking if any LHS is shifted is sufficient. If a variable is shifted in +the input equations there _will_ be a `Shift` equation in the simplified system. +""" +function is_discrete_system(sys::System) + any(eq -> isoperator(eq.lhs, Shift), equations(sys)) +end + +SymbolicIndexingInterface.is_time_dependent(sys::System) = get_iv(sys) !== nothing + +""" + is_dde(sys::AbstractSystem) + +Return a boolean indicating whether a system represents a set of delay +differential equations. +""" +is_dde(sys::AbstractSystem) = has_is_dde(sys) && get_is_dde(sys) + +function _check_if_dde(eqs, iv, subsystems) + is_dde = any(ModelingToolkit.is_dde, subsystems) + if !is_dde + vs = Set() + for eq in eqs + vars!(vs, eq) + is_dde = any(vs) do sym + isdelay(unwrap(sym), iv) + end + is_dde && break + end + end + return is_dde +end + +function flatten(sys::System, noeqs = false) + systems = get_systems(sys) + isempty(systems) && return sys + costs = cost(sys) + if _iszero(costs) + costs = Union{Real, BasicSymbolic}[] + else + costs = [costs] + end + # We don't include `ignored_connections` in the flattened system, because + # connection expansion inherently requires the hierarchy structure. If the system + # is being flattened, then we no longer want to expand connections (or have already + # done so) and thus don't care about `ignored_connections`. + return System(noeqs ? Equation[] : equations(sys), get_iv(sys), unknowns(sys), + parameters(sys; initial_parameters = true), brownians(sys); + jumps = jumps(sys), constraints = constraints(sys), costs = costs, + consolidate = default_consolidate, observed = observed(sys), + parameter_dependencies = parameter_dependencies(sys), defaults = defaults(sys), + guesses = guesses(sys), continuous_events = continuous_events(sys), + discrete_events = discrete_events(sys), assertions = assertions(sys), + is_dde = is_dde(sys), tstops = symbolic_tstops(sys), + initialization_eqs = initialization_equations(sys), + # without this, any defaults/guesses obtained from metadata that were + # later removed by the user will be re-added. Right now, we just want to + # retain `defaults(sys)` as-is. + discover_from_metadata = false, metadata = get_metadata(sys), + description = description(sys), name = nameof(sys)) +end + +has_massactionjumps(js::System) = any(x -> x isa MassActionJump, jumps(js)) +has_constantratejumps(js::System) = any(x -> x isa ConstantRateJump, jumps(js)) +has_variableratejumps(js::System) = any(x -> x isa VariableRateJump, jumps(js)) +# TODO: do we need this? it's kind of weird to keep +has_equations(js::System) = !isempty(equations(js)) + +function noise_equations_equal(sys1::System, sys2::System) + neqs1 = get_noise_eqs(sys1) + neqs2 = get_noise_eqs(sys2) + if neqs1 === nothing && neqs2 === nothing + return true + elseif neqs1 === nothing || neqs2 === nothing + return false + end + ndims(neqs1) == ndims(neqs2) || return false + + eqs1 = get_eqs(sys1) + eqs2 = get_eqs(sys2) + + # get the permutation vector of `eqs2` in terms of `eqs1` + # eqs1_used tracks the elements of `eqs1` already used in the permutation + eqs1_used = falses(length(eqs1)) + # the permutation of `eqs1` that gives `eqs2` + eqs2_perm = Int[] + for eq in eqs2 + # find the first unused element of `eqs1` equal to `eq` + idx = findfirst(i -> isequal(eq, eqs1[i]) && !eqs1_used[i], eachindex(eqs1)) + # none found, so noise equations are not equal + idx === nothing && return false + push!(eqs2_perm, idx) + end + + if neqs1 isa Vector + return isequal(@view(neqs1[eqs2_perm]), neqs2) + else + return isequal(@view(neqs1[eqs2_perm, :]), neqs2) + end +end + +function ignored_connections_equal(sys1::System, sys2::System) + ic1 = get_ignored_connections(sys1) + ic2 = get_ignored_connections(sys2) + if ic1 === nothing && ic2 === nothing + return true + elseif ic1 === nothing || ic2 === nothing + return false + end + return _eq_unordered(ic1[1], ic2[1]) && _eq_unordered(ic1[2], ic2[2]) +end + +function Base.:(==)(sys1::System, sys2::System) + sys1 === sys2 && return true + iv1 = get_iv(sys1) + iv2 = get_iv(sys2) + isequal(iv1, iv2) && + isequal(nameof(sys1), nameof(sys2)) && + _eq_unordered(get_eqs(sys1), get_eqs(sys2)) && + noise_equations_equal(sys1, sys2) && + _eq_unordered(get_jumps(sys1), get_jumps(sys2)) && + _eq_unordered(get_constraints(sys1), get_constraints(sys2)) && + _eq_unordered(get_costs(sys1), get_costs(sys2)) && + isequal(get_consolidate(sys1), get_consolidate(sys2)) && + _eq_unordered(get_unknowns(sys1), get_unknowns(sys2)) && + _eq_unordered(get_ps(sys1), get_ps(sys2)) && + _eq_unordered(get_brownians(sys1), get_brownians(sys2)) && + _eq_unordered(get_observed(sys1), get_observed(sys2)) && + _eq_unordered(get_parameter_dependencies(sys1), get_parameter_dependencies(sys2)) && + isequal(get_description(sys1), get_description(sys2)) && + isequal(get_defaults(sys1), get_defaults(sys2)) && + isequal(get_guesses(sys1), get_guesses(sys2)) && + _eq_unordered(get_initialization_eqs(sys1), get_initialization_eqs(sys2)) && + _eq_unordered(get_continuous_events(sys1), get_continuous_events(sys2)) && + _eq_unordered(get_discrete_events(sys1), get_discrete_events(sys2)) && + isequal(get_connector_type(sys1), get_connector_type(sys2)) && + isequal(get_assertions(sys1), get_assertions(sys2)) && + isequal(get_metadata(sys1), get_metadata(sys2)) && + isequal(get_gui_metadata(sys1), get_gui_metadata(sys2)) && + get_is_dde(sys1) == get_is_dde(sys2) && + _eq_unordered(get_tstops(sys1), get_tstops(sys2)) && + # not comparing tearing states because checking if they're equal up to ordering + # is difficult + getfield(sys1, :namespacing) == getfield(sys2, :namespacing) && + getfield(sys1, :complete) == getfield(sys2, :complete) && + ignored_connections_equal(sys1, sys2) && + get_parent(sys1) == get_parent(sys2) && + get_isscheduled(sys1) == get_isscheduled(sys2) && + all(s1 == s2 for (s1, s2) in zip(get_systems(sys1), get_systems(sys2))) +end + +function Base.hash(sys::System, h::UInt) + h = hash(nameof(sys), h) + h = hash(get_iv(sys), h) + # be considerate of things compared using `_eq_unordered` in `==` + eqs = get_eqs(sys) + eq_sortperm = sortperm(eqs; by = string) + h = hash(@view(eqs[eq_sortperm]), h) + neqs = get_noise_eqs(sys) + if neqs === nothing + h = hash(nothing, h) + elseif neqs isa Vector + h = hash(@view(neqs[eq_sortperm]), h) + else + h = hash(@view(neqs[eq_sortperm, :]), h) + end + h = hash(Set(get_jumps(sys)), h) + h = hash(Set(get_constraints(sys)), h) + h = hash(Set(get_costs(sys)), h) + h = hash(get_consolidate(sys), h) + h = hash(Set(get_unknowns(sys)), h) + h = hash(Set(get_ps(sys)), h) + h = hash(Set(get_brownians(sys)), h) + h = hash(Set(get_observed(sys)), h) + h = hash(Set(get_parameter_dependencies(sys)), h) + h = hash(get_description(sys), h) + h = hash(get_defaults(sys), h) + h = hash(get_guesses(sys), h) + h = hash(Set(get_initialization_eqs(sys)), h) + h = hash(Set(get_continuous_events(sys)), h) + h = hash(Set(get_discrete_events(sys)), h) + h = hash(get_connector_type(sys), h) + h = hash(get_assertions(sys), h) + h = hash(get_metadata(sys), h) + h = hash(get_gui_metadata(sys), h) + h = hash(get_is_dde(sys), h) + h = hash(Set(get_tstops(sys)), h) + h = hash(Set(getfield(sys, :namespacing)), h) + h = hash(Set(getfield(sys, :complete)), h) + ics = get_ignored_connections(sys) + if ics === nothing + h = hash(ics, h) + else + h = hash(Set(ics[1]), hash(Set(ics[2]), h), h) + end + h = hash(get_parent(sys), h) + h = hash(get_isscheduled(sys), h) + for s in get_systems(sys) + h = hash(s, h) + end + return h +end + +""" + $(TYPEDSIGNATURES) +""" +function check_complete(sys::System, obj) + iscomplete(sys) || throw(SystemNotCompleteError(obj)) +end + +function NonlinearSystem(sys::System) + if !is_time_dependent(sys) + throw(ArgumentError("`NonlinearSystem` constructor expects a time-dependent `System`")) + end + eqs = equations(sys) + obs = observed(sys) + subrules = Dict([D(x) => 0.0 for x in unknowns(sys)]) + eqs = map(eqs) do eq + fast_substitute(eq, subrules) + end + nsys = System(eqs, unknowns(sys), [parameters(sys); get_iv(sys)]; + parameter_dependencies = parameter_dependencies(sys), + defaults = merge(defaults(sys), Dict(get_iv(sys) => Inf)), guesses = guesses(sys), + initialization_eqs = initialization_equations(sys), name = nameof(sys), + observed = obs) + if iscomplete(sys) + nsys = complete(nsys; split = is_split(sys)) + end + return nsys +end + +######## +# Utility constructors +######## + +function OptimizationSystem(cost; kwargs...) + return System(Equation[]; costs = [cost], kwargs...) +end + +function OptimizationSystem(cost, dvs, ps; kwargs...) + return System(Equation[], nothing, dvs, ps; costs = [cost], kwargs...) +end + +function OptimizationSystem(cost::Array; kwargs...) + return System(Equation[]; costs = vec(cost), kwargs...) +end + +function OptimizationSystem(cost::Array, dvs, ps; kwargs...) + return System(Equation[], nothing, dvs, ps; costs = vec(cost), kwargs...) +end + +function JumpSystem(jumps, iv; kwargs...) + mask = isa.(jumps, Equation) + eqs = Vector{Equation}(jumps[mask]) + jumps = jumps[.!mask] + return System(eqs, iv; jumps, kwargs...) +end + +function JumpSystem(jumps, iv, dvs, ps; kwargs...) + mask = isa.(jumps, Equation) + eqs = Vector{Equation}(jumps[mask]) + jumps = jumps[.!mask] + return System(eqs, iv, dvs, ps; jumps, kwargs...) +end + +function SDESystem(eqs::Vector{Equation}, noise, iv; is_scalar_noise = false, kwargs...) + if is_scalar_noise + if !(noise isa Vector) + throw(ArgumentError("Expected noise to be a vector if `is_scalar_noise`")) + end + noise = repeat(reshape(noise, (1, :)), length(eqs)) + end + return System(eqs, iv; noise_eqs = noise, kwargs...) +end + +function SDESystem( + eqs::Vector{Equation}, noise, iv, dvs, ps; is_scalar_noise = false, kwargs...) + if is_scalar_noise + if !(noise isa Vector) + throw(ArgumentError("Expected noise to be a vector if `is_scalar_noise`")) + end + noise = repeat(reshape(noise, (1, :)), length(eqs)) + end + return System(eqs, iv, dvs, ps; noise_eqs = noise, kwargs...) +end + +function SDESystem(sys::System, noise; kwargs...) + SDESystem(equations(sys), noise, get_iv(sys); kwargs...) +end + +struct SystemNotCompleteError <: Exception + obj::Any +end + +function Base.showerror(io::IO, err::SystemNotCompleteError) + print(io, """ + A completed system is required. Call `complete` or `structural_simplify` on the \ + system before creating a `$(err.obj)`. + """) +end + +struct IllFormedNoiseEquationsError <: Exception + noise_eqs_rows::Int + eqs_length::Int +end + +function Base.showerror(io::IO, err::IllFormedNoiseEquationsError) + print(io, """ + Noise equations are ill-formed. The number of rows much must number of drift \ + equations. `size(neqs, 1) == $(err.noise_eqs_rows) != length(eqs) == \ + $(err.eqs_length)`. + """) +end + +function NoNameError() + ArgumentError(""" + The `name` keyword must be provided. Please consider using the `@named` macro. + """) +end + +struct NonUniqueSubsystemsError <: Exception + names::Vector{Symbol} + uniques::Set{Symbol} +end + +function Base.showerror(io::IO, err::NonUniqueSubsystemsError) + dupes = Set{Symbol}() + for n in err.names + if !(n in err.uniques) + push!(dupes, n) + end + delete!(err.uniques, n) + end + println(io, "System names must be unique. The following system names were duplicated:") + for n in dupes + println(io, " ", n) + end +end + +struct EventsInTimeIndependentSystemError <: Exception + cevents::Vector + devents::Vector +end + +function Base.showerror(io::IO, err::EventsInTimeIndependentSystemError) + println(io, """ + Events are not supported in time-indepent systems. Provide an independent variable to \ + make the system time-dependent or remove the events. + + The following continuous events were provided: + $(err.cevents) + + The following discrete events were provided: + $(err.devents) + """) +end + +function supports_initialization(sys::System) + return isempty(jumps(sys)) && _iszero(cost(sys)) && + isempty(constraints(sys)) +end From c146dd08ee0202b68fc13bb4ee35702f715091f1 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 14 Apr 2025 23:14:54 +0530 Subject: [PATCH 1632/2176] feat: add getters for new `System` fields --- src/systems/abstractsystem.jl | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index 6b5ede1369..7ed9d97ad0 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -876,11 +876,14 @@ end for prop in [:eqs :tag - :noiseeqs + :noiseeqs # TODO: remove + :noise_eqs :iv :unknowns :ps :tspan + :brownians + :jumps :name :description :var_to_name From 0da70f3ff4d96b9d6038014d583ae2cc8eb88760 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 14 Apr 2025 23:15:20 +0530 Subject: [PATCH 1633/2176] feat: add hierarchical aggregator functions for jumps, brownians and cost feat: export `jumps` --- src/ModelingToolkit.jl | 2 +- src/systems/abstractsystem.jl | 51 +++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 1 deletion(-) diff --git a/src/ModelingToolkit.jl b/src/ModelingToolkit.jl index 9f8b19aa1d..0fd1347117 100644 --- a/src/ModelingToolkit.jl +++ b/src/ModelingToolkit.jl @@ -289,7 +289,7 @@ export Differential, expand_derivatives, @derivatives export Equation, ConstrainedEquation export Term, Sym export SymScope, LocalScope, ParentScope, GlobalScope -export independent_variable, equations, controls, observed, full_equations +export independent_variable, equations, controls, observed, full_equations, jumps export initialization_equations, guesses, defaults, parameter_dependencies, hierarchy export structural_simplify, expand_connections, linearize, linearization_function, LinearizationProblem diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index 7ed9d97ad0..243865dda1 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -1301,6 +1301,28 @@ function namespace_equation(eq::Equation, (_lhs ~ _rhs)::Equation end +function namespace_jump(j::ConstantRateJump, sys) + return ConstantRateJump(namespace_expr(j.rate, sys), namespace_expr(j.affect!, sys)) +end + +function namespace_jump(j::VariableRateJump, sys) + return VariableRateJump(namespace_expr(j.rate, sys), namespace_expr(j.affect!, sys)) +end + +function namespace_jump(j::MassActionJump, sys) + return MassActionJump(namespace_expr(j.scaled_rates, sys), + [namespace_expr(k, sys) => namespace_expr(v, sys) for (k, v) in j.reactant_stoch], + [namespace_expr(k, sys) => namespace_expr(v, sys) for (k, v) in j.net_stoch]) +end + +function namespace_jumps(sys::AbstractSystem) + return [namespace_jump(j, sys) for j in get_jumps(sys)] +end + +function namespace_brownians(sys::AbstractSystem) + return [renamespace(sys, b) for b in brownians(sys)] +end + function namespace_assignment(eq::Assignment, sys) _lhs = namespace_expr(eq.lhs, sys) _rhs = namespace_expr(eq.rhs, sys) @@ -1734,6 +1756,35 @@ function equations_toplevel(sys::AbstractSystem) return get_eqs(sys) end +function jumps(sys::AbstractSystem) + js = get_jumps(sys) + systems = get_systems(sys) + if isempty(systems) + return js + end + return [js; reduce(vcat, namespace_jumps.(systems); init = [])] +end + +function brownians(sys::AbstractSystem) + bs = get_brownians(sys) + systems = get_systems(sys) + if isempty(systems) + return bs + end + return [bs; reduce(vcat, namespace_brownians.(systems); init = [])] +end + +function cost(sys::AbstractSystem) + cs = get_costs(sys) + consolidate = get_consolidate(sys) + systems = get_systems(sys) + if isempty(systems) + return consolidate(cs, Float64[]) + end + subcosts = [namespace_expr(cost(subsys), subsys) for subsys in systems] + return consolidate(cs, subcosts) +end + """ $(TYPEDSIGNATURES) From d15f60efab218b39552bcfd92b6d3991dcf2a8b0 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 21 Apr 2025 10:49:51 +0530 Subject: [PATCH 1634/2176] refactor: move `constraints` to `abstractsystem.jl` --- src/systems/abstractsystem.jl | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index 243865dda1..4bb45738de 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -1785,6 +1785,30 @@ function cost(sys::AbstractSystem) return consolidate(cs, subcosts) end +namespace_constraint(eq::Equation, sys) = namespace_equation(eq, sys) + +namespace_constraint(ineq::Inequality, sys) = namespace_inequality(ineq, sys) + +function namespace_inequality(ineq::Inequality, sys, n = nameof(sys)) + _lhs = namespace_expr(ineq.lhs, sys, n) + _rhs = namespace_expr(ineq.rhs, sys, n) + Inequality(_lhs, + _rhs, + ineq.relational_op) +end + +function namespace_constraints(sys) + cstrs = constraints(sys) + isempty(cstrs) && return Vector{Union{Equation, Inequality}}(undef, 0) + map(cstr -> namespace_constraint(cstr, sys), cstrs) +end + +function constraints(sys) + cs = get_constraints(sys) + systems = get_systems(sys) + isempty(systems) ? cs : [cs; reduce(vcat, namespace_constraints.(systems))] +end + """ $(TYPEDSIGNATURES) From e5bad1ed88a085dcaf48b4226e412f241d097fb2 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 14 Apr 2025 23:55:02 +0530 Subject: [PATCH 1635/2176] refactor: don't warn about system supertype for `System` --- src/systems/abstractsystem.jl | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index 4bb45738de..a5567f7743 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -244,7 +244,9 @@ Get the independent variable(s) of the system `sys`. See also [`@independent_variables`](@ref) and [`ModelingToolkit.get_iv`](@ref). """ function independent_variables(sys::AbstractSystem) - @warn "Please declare ($(typeof(sys))) as a subtype of `AbstractTimeDependentSystem`, `AbstractTimeIndependentSystem` or `AbstractMultivariateSystem`." + if !(sys isa System) + @warn "Please declare ($(typeof(sys))) as a subtype of `AbstractTimeDependentSystem`, `AbstractTimeIndependentSystem` or `AbstractMultivariateSystem`." + end if isdefined(sys, :iv) return [getfield(sys, :iv)] elseif isdefined(sys, :ivs) From acc4f3186cb39b39eff52b2ed06c1f1eb134eee8 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Sun, 20 Apr 2025 15:22:32 +0530 Subject: [PATCH 1636/2176] refactor: port `stochastic_integral_transform` and `Girsanov_transform` --- src/systems/diffeqs/basic_transformations.jl | 175 +++++++++++++++++++ 1 file changed, 175 insertions(+) diff --git a/src/systems/diffeqs/basic_transformations.jl b/src/systems/diffeqs/basic_transformations.jl index 37c8d8d021..3c1081fcbe 100644 --- a/src/systems/diffeqs/basic_transformations.jl +++ b/src/systems/diffeqs/basic_transformations.jl @@ -234,3 +234,178 @@ function change_independent_variable( end return transform(sys) end + +""" +$(TYPEDSIGNATURES) + +Choose correction_factor=-1//2 (1//2) to convert Ito -> Stratonovich (Stratonovich->Ito). +""" +function stochastic_integral_transform(sys::System, correction_factor) + if !isempty(get_systems(sys)) + throw(ArgumentError("The system must be flattened.")) + end + if get_noise_eqs(sys) === nothing + throw(ArgumentError(""" + `$stochastic_integral_transform` expects a system with noise_eqs. If your \ + noise is specified using brownian variables, consider calling \ + `structural_simplify`. + """)) + end + name = nameof(sys) + noise_eqs = get_noise_eqs(sys) + eqs = equations(sys) + dvs = unknowns(sys) + ps = parameters(sys) + # use the general interface + if noise_eqs isa Vector + _eqs = reduce(vcat, [eqs[i].lhs ~ noise_eqs[i] for i in eachindex(dvs)]) + de = System(_eqs, get_iv(sys), dvs, ps, name = name, checks = false) + + jac = calculate_jacobian(de, sparse = false, simplify = false) + ∇σσ′ = simplify.(jac * noise_eqs) + else + dimunknowns, m = size(noise_eqs) + _eqs = reduce(vcat, [eqs[i].lhs ~ noise_eqs[i] for i in eachindex(dvs)]) + de = System(_eqs, get_iv(sys), dvs, ps, name = name, checks = false) + + jac = calculate_jacobian(de, sparse = false, simplify = false) + ∇σσ′ = simplify.(jac * noise_eqs[:, 1]) + for k in 2:m + __eqs = reduce(vcat, + [eqs[i].lhs ~ noise_eqs[Int(i + (k - 1) * dimunknowns)] + for i in eachindex(dvs)]) + de = System(__eqs, get_iv(sys), dvs, dvs, name = name, checks = false) + + jac = calculate_jacobian(de, sparse = false, simplify = false) + ∇σσ′ = ∇σσ′ + simplify.(jac * noise_eqs[:, k]) + end + end + deqs = reduce(vcat, + [eqs[i].lhs ~ eqs[i].rhs + correction_factor * ∇σσ′[i] for i in eachindex(dvs)]) + + # reduce(vcat, [1]) == 1 for some reason + if deqs isa Equation + deqs = [deqs] + end + return @set sys.eqs = deqs +end + +""" +$(TYPEDSIGNATURES) + +Measure transformation method that allows for a reduction in the variance of an estimator `Exp(g(X_t))`. +Input: Original SDE system and symbolic function `u(t,x)` with scalar output that + defines the adjustable parameters `d` in the Girsanov transformation. Optional: initial + condition for `θ0`. +Output: Modified SDESystem with additional component `θ_t` and initial value `θ0`, as well as + the weight `θ_t/θ0` as observed equation, such that the estimator `Exp(g(X_t)θ_t/θ0)` + has a smaller variance. + +Reference: +Kloeden, P. E., Platen, E., & Schurz, H. (2012). Numerical solution of SDE through computer +experiments. Springer Science & Business Media. + +# Example + +```julia +using ModelingToolkit +using ModelingToolkit: t_nounits as t, D_nounits as D + +@parameters α β +@variables x(t) y(t) z(t) + +eqs = [D(x) ~ α*x] +noiseeqs = [β*x] + +@named de = SDESystem(eqs,noiseeqs,t,[x],[α,β]) + +# define u (user choice) +u = x +θ0 = 0.1 +g(x) = x[1]^2 +demod = ModelingToolkit.Girsanov_transform(de, u; θ0=0.1) + +u0modmap = [ + x => x0 +] + +parammap = [ + α => 1.5, + β => 1.0 +] + +probmod = SDEProblem(complete(demod),u0modmap,(0.0,1.0),parammap) +ensemble_probmod = EnsembleProblem(probmod; + output_func = (sol,i) -> (g(sol[x,end])*sol[demod.weight,end],false), + ) + +simmod = solve(ensemble_probmod,EM(),dt=dt,trajectories=numtraj) +``` + +""" +function Girsanov_transform(sys::System, u; θ0 = 1.0) + name = nameof(sys) + + # register new variable θ corresponding to 1D correction process θ(t) + t = get_iv(sys) + D = Differential(t) + @variables θ(t), weight(t) + + # determine the adjustable parameters `d` given `u` + # gradient of u with respect to unknowns + grad = Symbolics.gradient(u, unknowns(sys)) + + noiseeqs = copy(get_noise_eqs(sys)) + if noiseeqs isa Vector + d = simplify.(-(noiseeqs .* grad) / u) + drift_correction = noiseeqs .* d + else + d = simplify.(-noiseeqs * grad / u) + drift_correction = noiseeqs * d + end + + eqs = equations(sys) + dvs = unknowns(sys) + # transformation adds additional unknowns θ: newX = (X,θ) + # drift function for unknowns is modified + # θ has zero drift + deqs = reduce( + vcat, [eqs[i].lhs ~ eqs[i].rhs - drift_correction[i] for i in eachindex(dvs)]) + if deqs isa Equation + deqs = [deqs] + end + deqsθ = D(θ) ~ 0 + push!(deqs, deqsθ) + + # diffusion matrix is of size d x m (d unknowns, m noise), with diagonal noise represented as a d-dimensional vector + # for diagonal noise processes with m>1, the noise process will become non-diagonal; extra unknown component but no new noise process. + # new diffusion matrix is of size d+1 x M + # diffusion for state is unchanged + + noiseqsθ = θ * d + + if noiseeqs isa Vector + m = size(noiseeqs) + if m == 1 + push!(noiseeqs, noiseqsθ) + else + noiseeqs = [Array(Diagonal(wrap.(noiseeqs))); noiseqsθ'] + end + else + noiseeqs = [Array(noiseeqs); noiseqsθ'] + end + + unknown_vars = [dvs; θ] + + # return modified SDE System + @set! sys.eqs = deqs + @set! sys.noise_eqs = noiseeqs + @set! sys.unknowns = unknown_vars + get_defaults(sys)[θ] = θ0 + obs = observed(sys) + @set! sys.observed = [weight ~ θ / θ0; obs] + if get_parent(sys) !== nothing + @set! sys.parent.unknowns = [get_unknowns(get_parent(sys)); [θ, weight]] + end + return sys +end From a0dbe7ffc7ee9918c0bd853ee7719fa4e1c0e77f Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Sun, 27 Apr 2025 23:01:16 +0530 Subject: [PATCH 1637/2176] refactor: move `eval_or_rgf` to `codegen_utils.jl` --- src/systems/codegen_utils.jl | 21 +++++++++++++++++++++ src/utils.jl | 8 -------- 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/src/systems/codegen_utils.jl b/src/systems/codegen_utils.jl index bedfdbcc37..5d8982c244 100644 --- a/src/systems/codegen_utils.jl +++ b/src/systems/codegen_utils.jl @@ -1,3 +1,24 @@ +""" + $(TYPEDSIGNATURES) + +Given a function expression `expr`, return a callable version of it. + +# Keyword arguments +- `eval_expression`: Whether to use `eval` to make `expr` callable. If `false`, uses + RuntimeGeneratedFunctions.jl. +- `eval_module`: The module to `eval` the expression `expr` in. If `!eval_expression`, + this is the cache and context module for the `RuntimeGeneratedFunction`. +""" +function eval_or_rgf(expr::Expr; eval_expression = false, eval_module = @__MODULE__) + if eval_expression + return eval_module.eval(expr) + else + return drop_expr(RuntimeGeneratedFunction(eval_module, eval_module, expr)) + end +end + +eval_or_rgf(::Nothing; kws...) = nothing + """ $(TYPEDSIGNATURES) diff --git a/src/utils.jl b/src/utils.jl index c3d736f92f..be28fa3401 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -1011,14 +1011,6 @@ function restrict_array_to_union(arr) return Array{T, ndims(arr)}(arr) end -function eval_or_rgf(expr::Expr; eval_expression = false, eval_module = @__MODULE__) - if eval_expression - return eval_module.eval(expr) - else - return drop_expr(RuntimeGeneratedFunction(eval_module, eval_module, expr)) - end -end - function _with_unit(f, x, t, args...) x = f(x, args...) if hasmetadata(x, VariableUnit) && (t isa Symbolic && hasmetadata(t, VariableUnit)) From b938241a70214dcc2debe632b8b6cdcd3cff3331 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Sun, 27 Apr 2025 23:01:50 +0530 Subject: [PATCH 1638/2176] feat: allow `GeneratedFunctionWrapper` to compile functions and build expressions --- src/systems/codegen_utils.jl | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/systems/codegen_utils.jl b/src/systems/codegen_utils.jl index 5d8982c244..0e6a6979fc 100644 --- a/src/systems/codegen_utils.jl +++ b/src/systems/codegen_utils.jl @@ -265,6 +265,14 @@ function GeneratedFunctionWrapper{P}(foop::O, fiip::I) where {P, O, I} GeneratedFunctionWrapper{P, O, I}(foop, fiip) end +function GeneratedFunctionWrapper{P}(::Type{Val{true}}, foop, fiip; kwargs...) where {P} + :($(GeneratedFunctionWrapper{P})($foop, $fiip)) +end + +function GeneratedFunctionWrapper{P}(::Type{Val{false}}, foop, fiip; kws...) where {P} + GeneratedFunctionWrapper{P}(eval_or_rgf(foop; kws...), eval_or_rgf(fiip; kws...)) +end + function (gfw::GeneratedFunctionWrapper)(args...) _generated_call(gfw, args...) end From 84d69bce8d35cbf6cb1e7c4c51ed228eaccdb0bd Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 28 Apr 2025 12:15:19 +0530 Subject: [PATCH 1639/2176] feat: add `maybe_compile_function` --- src/systems/codegen_utils.jl | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/src/systems/codegen_utils.jl b/src/systems/codegen_utils.jl index 0e6a6979fc..6f90af8c16 100644 --- a/src/systems/codegen_utils.jl +++ b/src/systems/codegen_utils.jl @@ -310,3 +310,37 @@ end return :($f($(fargs...))) end end + +""" + $(TYPEDSIGNATURES) + +Optionally compile a method and optionally wrap it in a `GeneratedFunctionWrapper` on the +basis of `expression` `wrap_gfw`, both of type `Union{Type{Val{true}}, Type{Val{false}}}`. +`gfw_args` is the first type parameter of `GeneratedFunctionWrapper`. `f` is a tuple of +function expressions of the form `(oop, iip)` or a single out-of-place function expression. +Keyword arguments are forwarded to `eval_or_rgf`. +""" +function maybe_compile_function(expression, wrap_gfw::Type{Val{true}}, + gfw_args::Tuple{Int, Int, Bool}, f::NTuple{2, Expr}; kwargs...) + GeneratedFunctionWrapper{gfw_args}(expression, f...; kwargs...) +end + +function maybe_compile_function(expression::Type{Val{false}}, wrap_gfw::Type{Val{false}}, + gfw_args::Tuple{Int, Int, Bool}, f::NTuple{2, Expr}; kwargs...) + eval_or_rgf.(f; kwargs...) +end + +function maybe_compile_function(expression::Type{Val{true}}, wrap_gfw::Type{Val{false}}, + gfw_args::Tuple{Int, Int, Bool}, f::Union{Expr, NTuple{2, Expr}}; kwargs...) + return f +end + +function maybe_compile_function(expression, wrap_gfw::Type{Val{true}}, + gfw_args::Tuple{Int, Int, Bool}, f::Expr; kwargs...) + GeneratedFunctionWrapper{gfw_args}(expression, f, nothing; kwargs...) +end + +function maybe_compile_function(expression::Type{Val{false}}, wrap_gfw::Type{Val{false}}, + gfw_args::Tuple{Int, Int, Bool}, f::Expr; kwargs...) + eval_or_rgf(f; kwargs...) +end From c8e4be53895aa6e155d026a014babb8f34a8d760 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 15 Apr 2025 15:39:08 +0530 Subject: [PATCH 1640/2176] refactor: fix and document `delay_to_function`, implement it for `System` --- src/systems/codegen_utils.jl | 74 ++++++++++++++++++++++++++++++++++-- 1 file changed, 71 insertions(+), 3 deletions(-) diff --git a/src/systems/codegen_utils.jl b/src/systems/codegen_utils.jl index 6f90af8c16..e7ba2659d7 100644 --- a/src/systems/codegen_utils.jl +++ b/src/systems/codegen_utils.jl @@ -107,6 +107,74 @@ function array_variable_assignments(args...; argument_name = generated_argument_ return assignments end +""" + $(TYPEDSIGNATURES) + +Check if the variable `var` is a delayed variable, where `iv` is the independent +variable. +""" +function isdelay(var, iv) + iv === nothing && return false + isvariable(var) || return false + isparameter(var) && return false + if iscall(var) && !ModelingToolkit.isoperator(var, Symbolics.Operator) + args = arguments(var) + length(args) == 1 || return false + isequal(args[1], iv) || return true + end + return false +end + +""" +The argument of generated functions corresponding to the history function. +""" +const DDE_HISTORY_FUN = Sym{Symbolics.FnType{Tuple{Any, <:Real}, Vector{Real}}}(:___history___) + +""" + $(TYPEDSIGNATURES) + +Turn delayed unknowns in `eqs` into calls to `DDE_HISTORY_FUNCTION`. + +# Arguments + +- `sys`: The system of DDEs. +- `eqs`: The equations to convert. + +# Keyword Arguments + +- `param_arg`: The name of the variable containing the parameter object. +""" +function delay_to_function( + sys::AbstractSystem, eqs = full_equations(sys); param_arg = MTKPARAMETERS_ARG) + delay_to_function(eqs, + get_iv(sys), + Dict{Any, Int}(operation(s) => i for (i, s) in enumerate(unknowns(sys))), + parameters(sys), + DDE_HISTORY_FUN; param_arg) +end +function delay_to_function(eqs::Vector, iv, sts, ps, h; param_arg = MTKPARAMETERS_ARG) + delay_to_function.(eqs, (iv,), (sts,), (ps,), (h,); param_arg) +end +function delay_to_function(eq::Equation, iv, sts, ps, h; param_arg = MTKPARAMETERS_ARG) + delay_to_function(eq.lhs, iv, sts, ps, h; param_arg) ~ delay_to_function( + eq.rhs, iv, sts, ps, h; param_arg) +end +function delay_to_function(expr, iv, sts, ps, h; param_arg = MTKPARAMETERS_ARG) + if isdelay(expr, iv) + v = operation(expr) + time = arguments(expr)[1] + idx = sts[v] + return term(getindex, h(param_arg, time), idx, type = Real) + elseif iscall(expr) + return maketerm(typeof(expr), + operation(expr), + map(x -> delay_to_function(x, iv, sts, ps, h; param_arg), arguments(expr)), + metadata(expr)) + else + return expr + end +end + """ $(TYPEDSIGNATURES) @@ -159,11 +227,11 @@ function build_function_wrapper(sys::AbstractSystem, expr, args...; p_start = 2, obs = filter(filter_observed, observed(sys)) # turn delayed unknowns into calls to the history function if wrap_delays - history_arg = is_split(sys) ? MTKPARAMETERS_ARG : generated_argument_name(p_start) + param_arg = is_split(sys) ? MTKPARAMETERS_ARG : generated_argument_name(p_start) obs = map(obs) do eq - delay_to_function(sys, eq; history_arg) + delay_to_function(sys, eq; param_arg) end - expr = delay_to_function(sys, expr; history_arg) + expr = delay_to_function(sys, expr; param_arg) # add extra argument args = (args[1:(p_start - 1)]..., DDE_HISTORY_FUN, args[p_start:end]...) p_start += 1 From e96e76b01138eec15ea32fca555fd21ac363e357 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Sun, 13 Apr 2025 16:33:25 +0530 Subject: [PATCH 1641/2176] feat: add initial codegen for `System` feat: add more codegen implementation refactor: compile functions in `generate_*` fix: fix `calculate_jacobian` fix: respect `return_sparsity` in `generate_cost_hessian` feat: add `generate_control_jacobian` --- src/ModelingToolkit.jl | 1 + src/systems/codegen.jl | 562 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 563 insertions(+) create mode 100644 src/systems/codegen.jl diff --git a/src/ModelingToolkit.jl b/src/ModelingToolkit.jl index 0fd1347117..352ac8b68f 100644 --- a/src/ModelingToolkit.jl +++ b/src/ModelingToolkit.jl @@ -163,6 +163,7 @@ include("systems/imperative_affect.jl") include("systems/callbacks.jl") include("systems/system.jl") include("systems/codegen_utils.jl") +include("systems/codegen.jl") include("systems/problem_utils.jl") include("linearization.jl") diff --git a/src/systems/codegen.jl b/src/systems/codegen.jl new file mode 100644 index 0000000000..00f5a714f8 --- /dev/null +++ b/src/systems/codegen.jl @@ -0,0 +1,562 @@ +""" + $(TYPEDSIGNATURES) + +Generate the RHS function for the `equations` of a `System`. + +# Arguments + +# Keyword Arguments + +""" +function generate_rhs(sys::System, dvs = unknowns(sys), + ps = parameters(sys; initial_parameters = true); implicit_dae = false, + scalar = false, expression = Val{true}, wrap_gfw = Val{false}, + eval_expression = false, eval_module = @__MODULE__, override_discrete = false, + kwargs...) + eqs = equations(sys) + obs = observed(sys) + u = dvs + p = reorder_parameters(sys, ps) + t = get_iv(sys) + ddvs = nothing + extra_assignments = Assignment[] + + # used for DAEProblem and ImplicitDiscreteProblem + if implicit_dae + if override_discrete || is_discrete_system(sys) + # ImplicitDiscrete case + D = Shift(t, 1) + rhss = map(eqs) do eq + # Algebraic equations get shifted forward 1, to match with differential + # equations + _iszero(eq.lhs) ? distribute_shift(D(eq.rhs)) : (eq.rhs - eq.lhs) + end + # Handle observables in algebraic equations, since they are shifted + shifted_obs = Equation[distribute_shift(D(eq)) for eq in obs] + obsidxs = observed_equations_used_by(sys, rhss; obs = shifted_obs) + extra_assignments = [Assignment(shifted_obs[i].lhs, shifted_obs[i].rhs) + for i in obsidxs] + else + D = Differential(t) + rhss = [_iszero(eq.lhs) ? eq.rhs : eq.rhs - eq.lhs for eq in eqs] + end + ddvs = map(D, dvs) + else + if !override_discrete && !is_discrete_system(sys) + check_operator_variables(eqs, Differential) + check_lhs(eqs, Differential, Set(dvs)) + end + rhss = [eq.rhs for eq in eqs] + end + + if !isempty(assertions(sys)) + rhss[end] += unwrap(get_assertions_expr(sys)) + end + + # TODO: add an optional check on the ordering of observed equations + if scalar + rhss = only(rhss) + u = only(u) + end + + args = (u, p...) + p_start = 2 + if t !== nothing + args = (args..., t) + end + if implicit_dae + args = (ddvs, args...) + p_start += 1 + end + + res = build_function_wrapper(sys, rhss, args...; p_start, extra_assignments, + expression = Val{true}, expression_module = eval_module, kwargs...) + nargs = length(args) - length(p) + 1 + if is_dde(sys) + p_start += 1 + nargs += 1 + end + return maybe_compile_function( + expression, wrap_gfw, (p_start, nargs, is_split(sys)), + res; eval_expression, eval_module) +end + +function generate_diffusion_function(sys::System, dvs = unknowns(sys), + ps = parameters(sys; initial_parameters = true); expression = Val{true}, + wrap_gfw = Val{false}, eval_expression = false, + eval_module = @__MODULE__, kwargs...) + eqs = get_noise_eqs(sys) + if ndims(eqs) == 2 && size(eqs, 2) == 1 + # scalar noise + eqs = vec(eqs) + end + p = reorder_parameters(sys, ps) + res = build_function_wrapper(sys, eqs, dvs, p..., get_iv(sys); kwargs...) + if expression == Val{true} + return res + end + f_oop, f_iip = eval_or_rgf.(res; eval_expression, eval_module) + p_start = 2 + nargs = 3 + if is_dde(sys) + p_start += 1 + nargs += 1 + end + return maybe_compile_function( + expression, wrap_gfw, (p_start, nargs, is_split(sys)), res; eval_expression, eval_module) +end + +function calculate_tgrad(sys::System; simplify = false) + # We need to remove explicit time dependence on the unknown because when we + # have `u(t) * t` we want to have the tgrad to be `u(t)` instead of `u'(t) * + # t + u(t)`. + rhs = [detime_dvs(eq.rhs) for eq in full_equations(sys)] + iv = get_iv(sys) + xs = unknowns(sys) + rule = Dict(map((x, xt) -> xt => x, detime_dvs.(xs), xs)) + rhs = substitute.(rhs, Ref(rule)) + tgrad = [expand_derivatives(Differential(iv)(r), simplify) for r in rhs] + reverse_rule = Dict(map((x, xt) -> x => xt, detime_dvs.(xs), xs)) + tgrad = Num.(substitute.(tgrad, Ref(reverse_rule))) + return tgrad +end + +function calculate_jacobian(sys::System; + sparse = false, simplify = false, dvs = unknowns(sys)) + obs = Dict(eq.lhs => eq.rhs for eq in observed(sys)) + rhs = map(eq -> fixpoint_sub(eq.rhs - eq.lhs, obs), equations(sys)) + + if sparse + jac = sparsejacobian(rhs, dvs; simplify) + if get_iv(sys) !== nothing + W_s = W_sparsity(sys) + (Is, Js, Vs) = findnz(W_s) + # Add nonzeros of W as non-structural zeros of the Jacobian (to ensure equal + # results for oop and iip Jacobian) + for (i, j) in zip(Is, Js) + iszero(jac[i, j]) && begin + jac[i, j] = 1 + jac[i, j] = 0 + end + end + end + else + jac = jacobian(rhs, dvs; simplify) + end + + return jac +end + +function generate_jacobian(sys::System; + simplify = false, sparse = false, eval_expression = false, + eval_module = @__MODULE__, expression = Val{true}, wrap_gfw = Val{false}, + kwargs...) + dvs = unknowns(sys) + jac = calculate_jacobian(sys; simplify, sparse, dvs) + p = reorder_parameters(sys) + t = get_iv(sys) + if t === nothing + wrap_code = (identity, identity) + else + wrap_code = sparse ? assert_jac_length_header(sys) : (identity, identity) + end + args = (dvs, p...) + nargs = 2 + if is_time_dependent(sys) + args = (args..., t) + nargs = 3 + end + res = build_function_wrapper(sys, jac, args...; wrap_code, expression = Val{true}, + expression_module = eval_module, kwargs...) + return maybe_compile_function( + expression, wrap_gfw, (2, nargs, is_split(sys)), res; eval_expression, eval_module) +end + +function assert_jac_length_header(sys) + W = W_sparsity(sys) + identity, + function add_header(expr) + Func(expr.args, [], expr.body, + [:(@assert $(SymbolicUtils.Code.toexpr(term(findnz, expr.args[1])))[1:2] == + $(findnz(W)[1:2]))]) + end +end + +function generate_tgrad( + sys::System, dvs = unknowns(sys), ps = parameters( + sys; initial_parameters = true); + simplify = false, eval_expression = false, eval_module = @__MODULE__, + expression = Val{true}, wrap_gfw = Val{false}, kwargs...) + tgrad = calculate_tgrad(sys, simplify = simplify) + p = reorder_parameters(sys, ps) + res = build_function_wrapper(sys, tgrad, + dvs, + p..., + get_iv(sys); + expression = Val{true}, + expression_module = eval_module, + kwargs...) + + return maybe_compile_function( + expression, wrap_gfw, (2, 3, is_split(sys)), res; eval_expression, eval_module) +end + +const W_GAMMA = only(@variables ˍ₋gamma) + +function generate_W(sys::System, γ = 1.0, dvs = unknowns(sys), + ps = parameters(sys; initial_parameters = true); + simplify = false, sparse = false, expression = Val{true}, wrap_gfw = Val{false}, + eval_expression = false, eval_module = @__MODULE__, kwargs...) + M = calculate_massmatrix(sys; simplify) + if sparse + M = SparseArrays.sparse(M) + end + J = calculate_jacobian(sys; simplify, sparse, dvs) + W = W_GAMMA * M + J + t = get_iv(sys) + if t !== nothing + wrap_code = sparse ? assert_jac_length_header(sys) : (identity, identity) + end + + p = reorder_parameters(sys, ps) + res = build_function_wrapper(sys, W, dvs, p..., W_GAMMA, t; wrap_code, + p_end = 1 + length(p), kwargs...) + return maybe_compile_function( + expression, wrap_gfw, (2, 4, is_split(sys)), res; eval_expression, eval_module) +end + +function generate_dae_jacobian(sys::System, dvs = unknowns(sys), + ps = parameters(sys; initial_parameters = true); simplify = false, sparse = false, + expression = Val{true}, wrap_gfw = Val{false}, eval_expression = false, + eval_module = @__MODULE__, kwargs...) + jac_u = calculate_jacobian(sys; simplify = simplify, sparse = sparse) + t = get_iv(sys) + derivatives = Differential(t).(unknowns(sys)) + jac_du = calculate_jacobian(sys; simplify = simplify, sparse = sparse, + dvs = derivatives) + dvs = unknowns(sys) + jac = W_GAMMA * jac_du + jac_u + p = reorder_parameters(sys, ps) + res = build_function_wrapper(sys, jac, derivatives, dvs, p..., W_GAMMA, t; + p_start = 3, p_end = 2 + length(p), kwargs...) + return maybe_compile_function( + expression, wrap_gfw, (3, 5, is_split(sys)), res; eval_expression, eval_module) +end + +function generate_history(sys::System, u0; expression = Val{true}, wrap_gfw = Val{false}, + eval_expression = false, eval_module = @__MODULE__, kwargs...) + p = reorder_parameters(sys) + res = build_function_wrapper(sys, u0, p..., get_iv(sys); expression = Val{true}, + expression_module = eval_module, p_start = 1, p_end = length(p), + similarto = typeof(u0), wrap_delays = false, kwargs...) + return maybe_compile_function( + expression, wrap_gfw, (1, 2, is_split(sys)), res; eval_expression, eval_module) +end + +function calculate_massmatrix(sys::System; simplify = false) + eqs = [eq for eq in equations(sys)] + M = zeros(length(eqs), length(eqs)) + for (i, eq) in enumerate(eqs) + if iscall(eq.lhs) && operation(eq.lhs) isa Differential + st = var_from_nested_derivative(eq.lhs)[1] + j = variable_index(sys, st) + M[i, j] = 1 + else + _iszero(eq.lhs) || + error("Only semi-explicit constant mass matrices are currently supported. Faulty equation: $eq.") + end + end + M = simplify ? simplify.(M) : M + if isdiag(M) + M = Diagonal(M) + end + # M should only contain concrete numbers + M == I ? I : M +end + +function concrete_massmatrix(M; sparse = false, u0 = nothing) + if sparse && !(u0 === nothing || M === I) + SparseArrays.sparse(M) + elseif u0 === nothing || M === I + M + elseif M isa Diagonal + Diagonal(ArrayInterface.restructure(u0, diag(M))) + else + ArrayInterface.restructure(u0 .* u0', M) + end +end + +function jacobian_sparsity(sys::System) + sparsity = torn_system_jacobian_sparsity(sys) + sparsity === nothing || return sparsity + + Symbolics.jacobian_sparsity([eq.rhs for eq in full_equations(sys)], + [dv for dv in unknowns(sys)]) +end + +function jacobian_dae_sparsity(sys::System) + J1 = jacobian_sparsity([eq.rhs for eq in full_equations(sys)], + [dv for dv in unknowns(sys)]) + derivatives = Differential(get_iv(sys)).(unknowns(sys)) + J2 = jacobian_sparsity([eq.rhs for eq in full_equations(sys)], + [dv for dv in derivatives]) + J1 + J2 +end + +function W_sparsity(sys::System) + jac_sparsity = jacobian_sparsity(sys) + (n, n) = size(jac_sparsity) + M = calculate_massmatrix(sys) + M_sparsity = M isa UniformScaling ? sparse(I(n)) : + SparseMatrixCSC{Bool, Int64}((!iszero).(M)) + jac_sparsity .| M_sparsity +end + +function calculate_W_prototype(W_sparsity; u0 = nothing, sparse = false) + sparse || return nothing + uElType = u0 === nothing ? Float64 : eltype(u0) + return similar(W_sparsity, uElType) +end + +function isautonomous(sys::System) + tgrad = calculate_tgrad(sys; simplify = true) + all(iszero, tgrad) +end + +function get_bv_solution_symbol(ns) + only(@variables BV_SOLUTION(..)[1:ns]) +end + +function get_constraint_unknown_subs!(subs::Dict, cons::Vector, stidxmap::Dict, iv, sol) + vs = vars(cons) + for v in vs + iscall(v) || continue + op = operation(v) + args = arguments(v) + issym(op) && length(args) == 1 || continue + newv = op(iv) + haskey(stidxmap, newv) || continue + subs[v] = sol(args[1])[stidxmap[newv]] + end +end + +function generate_boundary_conditions(sys::System, u0, u0_idxs, t0; expression = Val{true}, + wrap_gfw = Val{false}, eval_expression = false, eval_module = @__MODULE__, + kwargs...) + iv = get_iv(sys) + sts = unknowns(sys) + ps = parameters(sys) + np = length(ps) + ns = length(sts) + stidxmap = Dict([v => i for (i, v) in enumerate(sts)]) + pidxmap = Dict([v => i for (i, v) in enumerate(ps)]) + + # sol = get_bv_solution_symbol(ns) + + cons = [con.lhs - con.rhs for con in constraints(sys)] + # conssubs = Dict() + # get_constraint_unknown_subs!(conssubs, cons, stidxmap, iv, sol) + # cons = map(x -> fast_substitute(x, conssubs), cons) + + init_conds = Any[] + for i in u0_idxs + expr = BVP_SOLUTION(t0)[i] - u0[i] + push!(init_conds, expr) + end + + exprs = vcat(init_conds, cons) + _p = reorder_parameters(sys, ps) + + res = build_function_wrapper(sys, exprs, _p..., iv; output_type = Array, + p_start = 1, histfn = (p, t) -> BVP_SOLUTION(t), + histfn_symbolic = BVP_SOLUTION, wrap_delays = true, kwargs...) + return maybe_compile_function( + expression, wrap_gfw, (2, 3, is_split(sys)), res; eval_expression, eval_module) +end + +function generate_cost(sys::System; expression = Val{true}, wrap_gfw = Val{false}, + eval_expression = false, eval_module = @__MODULE__, kwargs...) + obj = cost(sys) + dvs = unknowns(sys) + ps = reorder_parameters(sys) + res = build_function_wrapper(sys, obj, dvs, ps...; expression = Val{true}, kwargs...) + if expression == Val{true} + return res + end + f_oop = eval_or_rgf(res; eval_expression, eval_module) + return maybe_compile_function( + expression, wrap_gfw, (2, 2, is_split(sys)), res; eval_expression, eval_module) +end + +function calculate_cost_gradient(sys::System; simplify = false) + obj = cost(sys) + dvs = unknowns(sys) + return Symbolics.gradient(obj, dvs; simplify) +end + +function generate_cost_gradient( + sys::System; expression = Val{true}, wrap_gfw = Val{false}, + eval_expression = false, eval_module = @__MODULE__, simplify = false, kwargs...) + obj = cost(sys) + dvs = unknowns(sys) + ps = reorder_parameters(sys) + exprs = calculate_cost_gradient(sys; simplify) + res = build_function_wrapper(sys, exprs, dvs, ps...; expression = Val{true}, kwargs...) + return maybe_compile_function( + expression, wrap_gfw, (2, 2, is_split(sys)), res; eval_expression, eval_module) +end + +function calculate_cost_hessian(sys::System; sparse = false, simplify = false) + obj = cost(sys) + dvs = unknowns(sys) + if sparse + exprs = Symbolics.sparsehessian(obj, dvs; simplify)::AbstractSparseArray + sparsity = similar(exprs, Float64) + else + exprs = Symbolics.hessian(obj, dvs; simplify) + end +end + +function cost_hessian_sparsity(sys::System) + return similar(calculate_cost_hessian(sys; sparse = true), Float64) +end + +function generate_cost_hessian( + sys::System; expression = Val{true}, wrap_gfw = Val{false}, + eval_expression = false, eval_module = @__MODULE__, simplify = false, + sparse = false, return_sparsity = false, kwargs...) + obj = cost(sys) + dvs = unknowns(sys) + ps = reorder_parameters(sys) + sparsity = nothing + exprs = calculate_cost_hessian(sys; sparse, simplify) + if sparse + sparsity = similar(exprs, Float64) + end + res = build_function_wrapper(sys, exprs, dvs, ps...; expression = Val{true}, kwargs...) + fn = maybe_compile_function( + expression, wrap_gfw, (2, 2, is_split(sys)), res; eval_expression, eval_module) + + return return_sparsity ? (fn, sparsity) : fn +end + +function canonical_constraints(sys::System) + return map(constraints(sys)) do cstr + Symbolics.canonical_form(cstr).lhs + end +end + +function generate_cons(sys::System; expression = Val{true}, wrap_gfw = Val{false}, + eval_expression = false, eval_module = @__MODULE__, kwargs...) + cons = canonical_constraints(sys) + dvs = unknowns(sys) + ps = reorder_parameters(sys) + res = build_function_wrapper(sys, cons, dvs, ps...; expression = Val{true}, kwargs...) + return maybe_compile_function( + expression, wrap_gfw, (2, 2, is_split(sys)), res; eval_expression, eval_module) +end + +function generate_constraint_jacobian( + sys::System; expression = Val{true}, wrap_gfw = Val{false}, + eval_expression = false, eval_module = @__MODULE__, return_sparsity = false, + simplify = false, sparse = false, kwargs...) + cons = canonical_constraints(sys) + dvs = unknowns(sys) + ps = reorder_parameters(sys) + sparsity = nothing + if sparse + jac = Symbolics.sparsejacobian(cons, dvs; simplify)::AbstractSparseArray + sparsity = similar(jac, Float64) + else + jac = Symbolics.jacobian(cons, dvs; simplify) + end + res = build_function_wrapper(sys, jac, dvs, ps...; expression = Val{true}, kwargs...) + fn = maybe_compile_function( + expression, wrap_gfw, (2, 2, is_split(sys)), res; eval_expression, eval_module) + return return_sparsity ? (fn, sparsity) : fn +end + +function generate_constraint_hessian( + sys::System; expression = Val{true}, wrap_gfw = Val{false}, + eval_expression = false, eval_module = @__MODULE__, return_sparsity = false, + simplify = false, sparse = false, kwargs...) + cons = canonical_constraints(sys) + dvs = unknowns(sys) + ps = reorder_parameters(sys) + sparsity = nothing + if sparse + hess = map(cons) do cstr + Symbolics.sparsehessian(cstr, dvs; simplify)::AbstractSparseArray + end + sparsity = similar.(hess, Float64) + else + hess = [Symbolics.hessian(cstr, dvs; simplify) for cstr in cons] + end + res = build_function_wrapper(sys, hess, dvs, ps...; expression = Val{true}, kwargs...) + fn = maybe_compile_function( + expression, wrap_gfw, (2, 2, is_split(sys)), res; eval_expression, eval_module) + return return_sparsity ? (fn, sparsity) : fn +end + +function calculate_control_jacobian(sys::AbstractSystem; + sparse = false, simplify = false) + rhs = [eq.rhs for eq in full_equations(sys)] + ctrls = unbound_inputs(sys) + + if sparse + jac = sparsejacobian(rhs, ctrls, simplify = simplify) + else + jac = jacobian(rhs, ctrls, simplify = simplify) + end + + return jac +end + +function generate_control_jacobian(sys::AbstractSystem, dvs = unknowns(sys), + ps = parameters(sys; initial_parameters = true); + expression = Val{true}, wrap_gfw = Val{false}, eval_expression = false, + eval_module = @__MODULE__, simplify = false, sparse = false, kwargs...) + jac = calculate_control_jacobian(sys; simplify = simplify, sparse = sparse) + p = reorder_parameters(sys, ps) + res = build_function_wrapper(sys, jac, dvs, p..., get_iv(sys); kwargs...) + return maybe_compile_function( + expression, wrap_gfw, (2, 3, is_split(sys)), res; eval_expression, eval_module) +end + +function generate_rate_function(js::System, rate) + p = reorder_parameters(js) + build_function_wrapper(js, rate, unknowns(js), p..., + get_iv(js), + expression = Val{true}) +end + +function generate_affect_function(js::System, affect; kwargs...) + compile_equational_affect(affect, js; checkvars = false, kwargs...) +end + +function assemble_vrj( + js, vrj, unknowntoid; eval_expression = false, eval_module = @__MODULE__) + rate = eval_or_rgf(generate_rate_function(js, vrj.rate); eval_expression, eval_module) + rate = GeneratedFunctionWrapper{(2, 3, is_split(js))}(rate, nothing) + outputvars = (value(affect.lhs) for affect in vrj.affect!) + outputidxs = [unknowntoid[var] for var in outputvars] + affect = generate_affect_function(js, vrj.affect!; eval_expression, eval_module) + VariableRateJump(rate, affect; save_positions = vrj.save_positions) +end + +function assemble_crj( + js, crj, unknowntoid; eval_expression = false, eval_module = @__MODULE__) + rate = eval_or_rgf(generate_rate_function(js, crj.rate); eval_expression, eval_module) + rate = GeneratedFunctionWrapper{(2, 3, is_split(js))}(rate, nothing) + outputvars = (value(affect.lhs) for affect in crj.affect!) + outputidxs = [unknowntoid[var] for var in outputvars] + affect = generate_affect_function(js, crj.affect!; eval_expression, eval_module) + ConstantRateJump(rate, affect) +end + +# assemble a numeric MassActionJump from a MT symbolics MassActionJumps +function assemble_maj(majv::Vector{U}, unknowntoid, pmapper) where {U <: MassActionJump} + rs = [numericrstoich(maj.reactant_stoch, unknowntoid) for maj in majv] + ns = [numericnstoich(maj.net_stoch, unknowntoid) for maj in majv] + MassActionJump(rs, ns; param_mapper = pmapper, nocopy = true) +end From b174f035a324899c221e1e6073f7272caa4b75b9 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Sun, 20 Apr 2025 14:01:12 +0530 Subject: [PATCH 1642/2176] refactor: port `build_explicit_observed_function` to `codegen.jl` --- src/systems/codegen.jl | 168 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 168 insertions(+) diff --git a/src/systems/codegen.jl b/src/systems/codegen.jl index 00f5a714f8..acb319377a 100644 --- a/src/systems/codegen.jl +++ b/src/systems/codegen.jl @@ -560,3 +560,171 @@ function assemble_maj(majv::Vector{U}, unknowntoid, pmapper) where {U <: MassAct ns = [numericnstoich(maj.net_stoch, unknowntoid) for maj in majv] MassActionJump(rs, ns; param_mapper = pmapper, nocopy = true) end + +""" + build_explicit_observed_function(sys, ts; kwargs...) -> Function(s) + +Generates a function that computes the observed value(s) `ts` in the system `sys`, while making the assumption that there are no cycles in the equations. + +## Arguments +- `sys`: The system for which to generate the function +- `ts`: The symbolic observed values whose value should be computed + +## Keywords +- `return_inplace = false`: If true and the observed value is a vector, then return both the in place and out of place methods. +- `expression = false`: Generates a Julia `Expr`` computing the observed value if `expression` is true +- `eval_expression = false`: If true and `expression = false`, evaluates the returned function in the module `eval_module` +- `output_type = Array` the type of the array generated by a out-of-place vector-valued function +- `param_only = false` if true, only allow the generated function to access system parameters +- `inputs = nothing` additinoal symbolic variables that should be provided to the generated function +- `checkbounds = true` checks bounds if true when destructuring parameters +- `op = Operator` sets the recursion terminator for the walk done by `vars` to identify the variables that appear in `ts`. See the documentation for `vars` for more detail. +- `throw = true` if true, throw an error when generating a function for `ts` that reference variables that do not exist. +- `mkarray`: only used if the output is an array (that is, `!isscalar(ts)` and `ts` is not a tuple, in which case the result will always be a tuple). Called as `mkarray(ts, output_type)` where `ts` are the expressions to put in the array and `output_type` is the argument of the same name passed to build_explicit_observed_function. +- `cse = true`: Whether to use Common Subexpression Elimination (CSE) to generate a more efficient function. + +## Returns + +The return value will be either: +* a single function `f_oop` if the input is a scalar or if the input is a Vector but `return_inplace` is false +* the out of place and in-place functions `(f_ip, f_oop)` if `return_inplace` is true and the input is a `Vector` + +The function(s) `f_oop` (and potentially `f_ip`) will be: +* `RuntimeGeneratedFunction`s by default, +* A Julia `Expr` if `expression` is true, +* A directly evaluated Julia function in the module `eval_module` if `eval_expression` is true and `expression` is false. + +The signatures will be of the form `g(...)` with arguments: + +- `output` for in-place functions +- `unknowns` if `param_only` is `false` +- `inputs` if `inputs` is an array of symbolic inputs that should be available in `ts` +- `p...` unconditionally; note that in the case of `MTKParameters` more than one parameters argument may be present, so it must be splatted +- `t` if the system is time-dependent; for example `NonlinearSystem` will not have `t` + +For example, a function `g(op, unknowns, p..., inputs, t)` will be the in-place function generated if `return_inplace` is true, `ts` is a vector, +an array of inputs `inputs` is given, and `param_only` is false for a time-dependent system. +""" +function build_explicit_observed_function(sys, ts; + inputs = nothing, + disturbance_inputs = nothing, + disturbance_argument = false, + expression = false, + eval_expression = false, + eval_module = @__MODULE__, + output_type = Array, + checkbounds = true, + ps = parameters(sys; initial_parameters = true), + return_inplace = false, + param_only = false, + op = Operator, + throw = true, + cse = true, + mkarray = nothing) + # TODO: cleanup + is_tuple = ts isa Tuple + if is_tuple + ts = collect(ts) + output_type = Tuple + end + + allsyms = all_symbols(sys) + if symbolic_type(ts) == NotSymbolic() && ts isa AbstractArray + ts = map(x -> symbol_to_symbolic(sys, x; allsyms), ts) + else + ts = symbol_to_symbolic(sys, ts; allsyms) + end + + vs = ModelingToolkit.vars(ts; op) + namespace_subs = Dict() + ns_map = Dict{Any, Any}(renamespace(sys, eq.lhs) => eq.lhs for eq in observed(sys)) + for sym in unknowns(sys) + ns_map[renamespace(sys, sym)] = sym + if iscall(sym) && operation(sym) === getindex + ns_map[renamespace(sys, arguments(sym)[1])] = arguments(sym)[1] + end + end + for sym in full_parameters(sys) + ns_map[renamespace(sys, sym)] = sym + if iscall(sym) && operation(sym) === getindex + ns_map[renamespace(sys, arguments(sym)[1])] = arguments(sym)[1] + end + end + allsyms = Set(all_symbols(sys)) + iv = has_iv(sys) ? get_iv(sys) : nothing + for var in vs + var = unwrap(var) + newvar = get(ns_map, var, nothing) + if newvar !== nothing + namespace_subs[var] = newvar + var = newvar + end + if throw && !var_in_varlist(var, allsyms, iv) + Base.throw(ArgumentError("Symbol $var is not present in the system.")) + end + end + ts = fast_substitute(ts, namespace_subs) + + obsfilter = if param_only + if is_split(sys) + let ic = get_index_cache(sys) + eq -> !(ContinuousTimeseries() in ic.observed_syms_to_timeseries[eq.lhs]) + end + else + Returns(false) + end + else + Returns(true) + end + dvs = if param_only + () + else + (unknowns(sys),) + end + if inputs === nothing + inputs = () + else + ps = setdiff(ps, inputs) # Inputs have been converted to parameters by io_preprocessing, remove those from the parameter list + inputs = (inputs,) + end + if disturbance_inputs !== nothing + # Disturbance inputs may or may not be included as inputs, depending on disturbance_argument + ps = setdiff(ps, disturbance_inputs) + end + if disturbance_argument + disturbance_inputs = (disturbance_inputs,) + else + disturbance_inputs = () + end + ps = reorder_parameters(sys, ps) + iv = if is_time_dependent(sys) + (get_iv(sys),) + else + () + end + args = (dvs..., inputs..., ps..., iv..., disturbance_inputs...) + p_start = length(dvs) + length(inputs) + 1 + p_end = length(dvs) + length(inputs) + length(ps) + fns = build_function_wrapper( + sys, ts, args...; p_start, p_end, filter_observed = obsfilter, + output_type, mkarray, try_namespaced = true, expression = Val{true}, cse) + if fns isa Tuple + if expression + return return_inplace ? fns : fns[1] + end + oop, iip = eval_or_rgf.(fns; eval_expression, eval_module) + f = GeneratedFunctionWrapper{( + p_start + is_dde(sys), length(args) - length(ps) + 1 + is_dde(sys), is_split(sys))}( + oop, iip) + return return_inplace ? (f, f) : f + else + if expression + return fns + end + f = eval_or_rgf(fns; eval_expression, eval_module) + f = GeneratedFunctionWrapper{( + p_start + is_dde(sys), length(args) - length(ps) + 1 + is_dde(sys), is_split(sys))}( + f, nothing) + return f + end +end From 6008eba2d062f984d8fef3dc69ffa552b5f65793 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 14 Apr 2025 23:16:47 +0530 Subject: [PATCH 1643/2176] feat: add `@fallback_iip_specialize` fix: fix bugs in `@fallback_iip_specialize`, handle static array problems --- src/systems/problem_utils.jl | 91 ++++++++++++++++++++++++++++++++++++ 1 file changed, 91 insertions(+) diff --git a/src/systems/problem_utils.jl b/src/systems/problem_utils.jl index 5ff00b4845..39e9c8e650 100644 --- a/src/systems/problem_utils.jl +++ b/src/systems/problem_utils.jl @@ -1418,6 +1418,97 @@ function SciMLBase.detect_cycles(sys::AbstractSystem, varmap::Dict{Any, Any}, va return !isempty(cycles) end +""" + $(TYPEDSIGNATURES) + +Macro for writing problem/function constructors. Expects a function definition with type +parameters for `iip` and `specialize`. Generates fallbacks with +`specialize = SciMLBase.FullSpecialize` and `iip = true`. +""" +macro fallback_iip_specialize(ex) + @assert Meta.isexpr(ex, :function) + # fnname is ODEProblem{iip, spec}(args...) where {iip, spec} + # body is function body + fnname, body = ex.args + @assert Meta.isexpr(fnname, :where) + # fnname_call is ODEProblem{iip, spec}(args...) + # where_args are `iip, spec` + fnname_call, where_args... = fnname.args + @assert length(where_args) == 2 + iiparg, specarg = where_args + + @assert Meta.isexpr(fnname_call, :call) + # fnname_curly is ODEProblem{iip, spec} + fnname_curly, args... = fnname_call.args + # the function should have keyword arguments + @assert Meta.isexpr(args[1], :parameters) + + # arguments to call with + call_args = map(args) do arg + # keyword args are in `Expr(:parameters)` so any `Expr(:kw)` here + # are optional positional arguments. Analyze `:(f(a, b = 1; k = 1, l...))` + # to understand + Meta.isexpr(arg, :kw) && return arg.args[1] + return arg + end + call_kwargs = map(call_args[1].args) do arg + Meta.isexpr(arg, :...) && return arg + @assert Meta.isexpr(arg, :kw) + return Expr(:kw, arg.args[1], arg.args[1]) + end + call_args[1] = Expr(:parameters, call_kwargs...) + + @assert Meta.isexpr(fnname_curly, :curly) + # fnname_name is `ODEProblem` + # curly_args is `iip, spec` + fnname_name, curly_args... = fnname_curly.args + @assert curly_args == where_args + + # callexpr_iip is `ODEProblem{iip, FullSpecialize}(call_args...)` + callexpr_iip = Expr( + :call, Expr(:curly, fnname_name, curly_args[1], SciMLBase.FullSpecialize), call_args...) + # `ODEProblem{iip}` + fnname_iip = Expr(:curly, fnname_name, curly_args[1]) + # `ODEProblem{iip}(args...)` + fncall_iip = Expr(:call, fnname_iip, args...) + # ODEProblem{iip}(args...) where {iip} + fnwhere_iip = Expr(:where, fncall_iip, where_args[1]) + fn_iip = Expr(:function, fnwhere_iip, callexpr_iip) + + # `ODEProblem{true}(call_args...)` + callexpr_base = Expr(:call, Expr(:curly, fnname_name, true), call_args...) + # `ODEProblem(args...)` + fncall_base = Expr(:call, fnname_name, args...) + fn_base = Expr(:function, fncall_base, callexpr_base) + + # Handle case when this is a problem constructor and `u0map` is a `StaticArray`, + # where `iip` should default to `false`. + fn_sarr = nothing + if occursin("Problem", string(fnname_name)) + # args should at least contain an argument for the `u0map` + @assert length(args) > 3 + u0_arg = args[3] + # should not have a type-annotation + @assert !Meta.isexpr(u0_arg, :(::)) + if Meta.isexpr(u0_arg, :kw) + argname, default = u0_arg.args + u0_arg = Expr(:kw, Expr(:(::), argname, StaticArray), default) + else + u0_arg = Expr(:(::), u0_arg, StaticArray) + end + + callexpr_sarr = Expr(:call, Expr(:curly, fnname_name, false), call_args...) + fncall_sarr = Expr(:call, fnname_name, args[1], args[2], u0_arg, args[4:end]...) + fn_sarr = Expr(:function, fncall_sarr, callexpr_sarr) + end + return quote + $fn_base + $fn_sarr + $fn_iip + Base.@__doc__ $ex + end |> esc +end + ############## # Legacy functions for backward compatibility ############## From 67a598b1bfe09b99e04700a4f4d3d3459639e4e5 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 14 Apr 2025 23:16:58 +0530 Subject: [PATCH 1644/2176] feat: add `check_compatible_system` --- src/ModelingToolkit.jl | 1 + src/problems/compatibility.jl | 171 ++++++++++++++++++++++++++++++++++ 2 files changed, 172 insertions(+) create mode 100644 src/problems/compatibility.jl diff --git a/src/ModelingToolkit.jl b/src/ModelingToolkit.jl index 352ac8b68f..c54c4474ba 100644 --- a/src/ModelingToolkit.jl +++ b/src/ModelingToolkit.jl @@ -167,6 +167,7 @@ include("systems/codegen.jl") include("systems/problem_utils.jl") include("linearization.jl") +include("problems/compatibility.jl") include("systems/optimization/modelingtoolkitize.jl") include("systems/nonlinear/homotopy_continuation.jl") diff --git a/src/problems/compatibility.jl b/src/problems/compatibility.jl new file mode 100644 index 0000000000..84c2eefc3f --- /dev/null +++ b/src/problems/compatibility.jl @@ -0,0 +1,171 @@ +""" + function check_compatible_system(T::Type, sys::System) + +Check if `sys` can be used to construct a problem/function of type `T`. +""" +function check_compatible_system end + +struct SystemCompatibilityError <: Exception + msg::String +end + +function Base.showerror(io::IO, err::SystemCompatibilityError) + println(io, err.msg) + println(io) + print(io, "To disable this check, pass `check_compatibility = false`.") +end + +function check_time_dependent(sys::System, T) + if !is_time_dependent(sys) + throw(SystemCompatibilityError(""" + `$T` requires a time-dependent system. + """)) + end +end + +function check_time_independent(sys::System, T) + if is_time_dependent(sys) + throw(SystemCompatibilityError(""" + `$T` requires a time-independent system. + """)) + end +end + +function check_is_dde(sys::System) + altT = get_noise_eqs(sys) === nothing ? ODEProblem : SDEProblem + if !is_dde(sys) + throw(SystemCompatibilityError(""" + The system does not have delays. Consider an `$altT` instead. + """)) + end +end + +function check_not_dde(sys::System) + altT = get_noise_eqs(sys) === nothing ? DDEProblem : SDDEProblem + if is_dde(sys) + throw(SystemCompatibilityError(""" + The system has delays. Consider a `$altT` instead. + """)) + end +end + +function check_no_cost(sys::System, T) + cost = ModelingToolkit.cost(sys) + if !_iszero(cost) + throw(SystemCompatibilityError(""" + `$T` will not optimize solutions of systems that have associated cost \ + functions. Solvers for optimal control problems are forthcoming. + """)) + end +end + +function check_has_cost(sys::System, T) + cost = ModelingToolkit.cost(sys) + if _iszero(cost) + throw(SystemCompatibilityError(""" + A system without cost cannot be used to construct a `$T`. + """)) + end +end + +function check_no_constraints(sys::System, T) + if !isempty(constraints(sys)) + throw(SystemCompatibilityError(""" + A system with constraints cannot be used to construct a `$T`. + """)) + end +end + +function check_has_constraints(sys::System, T) + if isempty(constraints(sys)) + throw(SystemCompatibilityError(""" + A system without constraints cannot be used to construct a `$T`. Consider an \ + `ODEProblem` instead. + """)) + end +end + +function check_no_jumps(sys::System, T) + if !isempty(jumps(sys)) + throw(SystemCompatibilityError(""" + A system with jumps cannot be used to construct a `$T`. Consider a \ + `JumpProblem` instead. + """)) + end +end + +function check_has_jumps(sys::System, T) + if isempty(jumps(sys)) + throw(SystemCompatibilityError("`$T` requires a system with jumps.")) + end +end + +function check_no_noise(sys::System, T) + altT = is_dde(sys) ? SDDEProblem : SDEProblem + if get_noise_eqs(sys) !== nothing + throw(SystemCompatibilityError(""" + A system with noise cannot be used to construct a `$T`. Consider an \ + `$altT` instead. + """)) + end +end + +function check_has_noise(sys::System, T) + altT = is_dde(sys) ? DDEProblem : ODEProblem + if get_noise_eqs(sys) === nothing + msg = """ + A system without noise cannot be used to construct a `$T`. Consider an \ + `$altT` instead. + """ + if !isempty(brownians(sys)) + msg = """ + Systems constructed by defining Brownian variables with `@brownian` must be \ + simplified by calling `structural_simplify` before a `$T` can be constructed. + """ + end + throw(SystemCompatibilityError(msg)) + end +end + +function check_is_discrete(sys::System, T) + if !is_discrete_system(sys) + throw(SystemCompatibilityError(""" + `$T` expects a discrete system. Consider an `ODEProblem` instead. If your system \ + is discrete, ensure `structural_simplify` has been run on it. + """)) + end +end + +function check_is_continuous(sys::System, T) + altT = has_alg_equations(sys) ? ImplicitDiscreteProblem : DiscreteProblem + if is_discrete_system(sys) + throw(SystemCompatibilityError(""" + A discrete system cannot be used to construct a `$T`. Consider a `$altT` instead. + """)) + end +end + +function check_is_explicit(sys::System, T, altT) + if has_alg_equations(sys) + throw(SystemCompatibilityError(""" + `$T` expects an explicit system. Consider a `$altT` instead. + """)) + end +end + +function check_is_implicit(sys::System, T, altT) + if !has_alg_equations(sys) + throw(SystemCompatibilityError(""" + `$T` expects an implicit system. Consider a `$altT` instead. + """)) + end +end + +function check_no_equations(sys::System, T) + if !isempty(equations(sys)) + throw(SystemCompatibilityError(""" + A system with equations cannot be used to construct a `$T`. Consider turning the + equations into constraints instead. + """)) + end +end From 4a96614f761c13579bcf0084c29ab20de485b523 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 14 Apr 2025 23:53:12 +0530 Subject: [PATCH 1645/2176] feat: implement `generate_initializesystem` for `System` --- src/systems/nonlinear/initializesystem.jl | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/systems/nonlinear/initializesystem.jl b/src/systems/nonlinear/initializesystem.jl index d2a988dc07..492b52489d 100644 --- a/src/systems/nonlinear/initializesystem.jl +++ b/src/systems/nonlinear/initializesystem.jl @@ -1,9 +1,17 @@ +function generate_initializesystem(sys::AbstractSystem; kwargs...) + if is_time_dependent(sys) + generate_initializesystem_timevarying(sys; kwargs...) + else + generate_initializesystem_timeindependent(sys; kwargs...) + end +end + """ $(TYPEDSIGNATURES) Generate `NonlinearSystem` which initializes a problem from specified initial conditions of an `AbstractTimeDependentSystem`. """ -function generate_initializesystem(sys::AbstractTimeDependentSystem; +function generate_initializesystem_timevarying(sys::AbstractSystem; u0map = Dict(), pmap = Dict(), initialization_eqs = [], @@ -160,7 +168,7 @@ $(TYPEDSIGNATURES) Generate `NonlinearSystem` which initializes a problem from specified initial conditions of an `AbstractTimeDependentSystem`. """ -function generate_initializesystem(sys::AbstractTimeIndependentSystem; +function generate_initializesystem_timeindependent(sys::AbstractSystem; u0map = Dict(), pmap = Dict(), initialization_eqs = [], From 97a6cad07b67542de3e5b3abe6fab2bbdfc339c6 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 16 Apr 2025 17:07:15 +0530 Subject: [PATCH 1646/2176] refactor: remove `generate_factorized_W` --- src/systems/abstractsystem.jl | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index a5567f7743..a1a576f639 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -113,17 +113,6 @@ the arguments to the internal [`build_function`](@ref) call. """ function generate_jacobian end -""" -```julia -generate_factorized_W(sys::AbstractSystem, dvs = unknowns(sys), ps = parameters(sys), - expression = Val{true}; sparse = false, kwargs...) -``` - -Generates a function for the factorized W matrix of a system. Extra arguments control -the arguments to the internal [`build_function`](@ref) call. -""" -function generate_factorized_W end - """ ```julia generate_hessian(sys::AbstractSystem, dvs = unknowns(sys), ps = parameters(sys), From 5ee21c34fc9d76ddff980de159fb0426f2769892 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 16 Apr 2025 19:58:36 +0530 Subject: [PATCH 1647/2176] fix: fix `remake` for `IntervalNonlinearProblem` --- src/systems/nonlinear/initializesystem.jl | 1 + 1 file changed, 1 insertion(+) diff --git a/src/systems/nonlinear/initializesystem.jl b/src/systems/nonlinear/initializesystem.jl index 492b52489d..056d35df37 100644 --- a/src/systems/nonlinear/initializesystem.jl +++ b/src/systems/nonlinear/initializesystem.jl @@ -629,6 +629,7 @@ end function SciMLBase.late_binding_update_u0_p( prob, sys::AbstractSystem, u0, p, t0, newu0, newp) supports_initialization(sys) || return newu0, newp + prob isa IntervalNonlinearProblem && return newu0, newp initdata = prob.f.initialization_data meta = initdata === nothing ? nothing : initdata.metadata From 304f6408ce8cafa1dfed45cba570b96212788e26 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 14 May 2025 11:49:57 +0530 Subject: [PATCH 1648/2176] feat: add `replace` kwarg to `add_toterms!` --- src/systems/problem_utils.jl | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/systems/problem_utils.jl b/src/systems/problem_utils.jl index 39e9c8e650..6f93f14a2d 100644 --- a/src/systems/problem_utils.jl +++ b/src/systems/problem_utils.jl @@ -32,11 +32,14 @@ is_split(sys::AbstractSystem) = has_index_cache(sys) && get_index_cache(sys) !== """ $(TYPEDSIGNATURES) -Given a variable-value mapping, add mappings for the `toterm` of each of the keys. +Given a variable-value mapping, add mappings for the `toterm` of each of the keys. `replace` controls whether +the old value should be removed. """ -function add_toterms!(varmap::AbstractDict; toterm = default_toterm) +function add_toterms!(varmap::AbstractDict; toterm = default_toterm, replace = false) for k in collect(keys(varmap)) - varmap[toterm(k)] = varmap[k] + ttk = toterm(k) + varmap[ttk] = varmap[k] + !isequal(k, ttk) && replace && delete!(varmap, k) end return nothing end @@ -46,9 +49,9 @@ end Out-of-place version of [`add_toterms!`](@ref). """ -function add_toterms(varmap::AbstractDict; toterm = default_toterm) +function add_toterms(varmap::AbstractDict; kwargs...) cp = copy(varmap) - add_toterms!(cp; toterm) + add_toterms!(cp; kwargs...) return cp end From 4decfa88c0584a691f8bb2bf36e1a4ccc727095e Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 14 May 2025 12:08:40 +0530 Subject: [PATCH 1649/2176] refactor: centralize problem kwargs handling --- src/systems/problem_utils.jl | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/systems/problem_utils.jl b/src/systems/problem_utils.jl index 6f93f14a2d..63b8418257 100644 --- a/src/systems/problem_utils.jl +++ b/src/systems/problem_utils.jl @@ -1421,6 +1421,26 @@ function SciMLBase.detect_cycles(sys::AbstractSystem, varmap::Dict{Any, Any}, va return !isempty(cycles) end +function process_kwargs(sys::System; callback = nothing, eval_expression = false, + eval_module = @__MODULE__, kwargs...) + kwargs = filter_kwargs(kwargs) + kwargs1 = (;) + + if is_time_dependent(sys) + cbs = process_events(sys; callback, eval_expression, eval_module, kwargs...) + if cbs !== nothing + kwargs1 = merge(kwargs1, (callback = cbs,)) + end + + tstops = SymbolicTstops(sys; eval_expression, eval_module) + if tstops !== nothing + kwargs1 = merge(kwargs1, (; tstops)) + end + end + + return merge(kwargs1, kwargs) +end + """ $(TYPEDSIGNATURES) From 8d313bfc1bb513f4a8791e9de7e9d0d0c43a49a3 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 21 Apr 2025 12:44:46 +0530 Subject: [PATCH 1650/2176] refactor: move `filter_kwargs` and `SymbolicTstops` to `problem_utils.jl` --- src/systems/problem_utils.jl | 41 ++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/src/systems/problem_utils.jl b/src/systems/problem_utils.jl index 63b8418257..a224a10df0 100644 --- a/src/systems/problem_utils.jl +++ b/src/systems/problem_utils.jl @@ -1441,6 +1441,47 @@ function process_kwargs(sys::System; callback = nothing, eval_expression = false return merge(kwargs1, kwargs) end +function filter_kwargs(kwargs) + kwargs = Dict(kwargs) + for key in keys(kwargs) + key in DiffEqBase.allowedkeywords || delete!(kwargs, key) + end + pairs(NamedTuple(kwargs)) +end + +struct SymbolicTstops{F} + fn::F +end + +function (st::SymbolicTstops)(p, tspan) + unique!(sort!(reduce(vcat, st.fn(p, tspan...)))) +end + +function SymbolicTstops( + sys::AbstractSystem; eval_expression = false, eval_module = @__MODULE__) + tstops = symbolic_tstops(sys) + isempty(tstops) && return nothing + t0 = gensym(:t0) + t1 = gensym(:t1) + tstops = map(tstops) do val + if is_array_of_symbolics(val) || val isa AbstractArray + collect(val) + else + term(:, t0, unwrap(val), t1; type = AbstractArray{Real}) + end + end + rps = reorder_parameters(sys) + tstops, _ = build_function_wrapper(sys, tstops, + rps..., + t0, + t1; + expression = Val{true}, + p_start = 1, p_end = length(rps), add_observed = false, force_SA = true) + tstops = eval_or_rgf(tstops; eval_expression, eval_module) + tstops = GeneratedFunctionWrapper{(1, 3, is_split(sys))}(tstops, nothing) + return SymbolicTstops(tstops) +end + """ $(TYPEDSIGNATURES) From d469f7c01a1234038376f38edd31cda57ce708c3 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 28 Apr 2025 15:09:15 +0530 Subject: [PATCH 1651/2176] feat: support returning `Expr` from `SymbolicTstops` and `ObservedFunctionCache` --- src/systems/abstractsystem.jl | 11 ++++++++--- src/systems/problem_utils.jl | 14 ++++++++++---- 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index a1a576f639..2adc879e94 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -1887,10 +1887,15 @@ struct ObservedFunctionCache{S} end function ObservedFunctionCache( - sys; steady_state = false, eval_expression = false, + sys; expression = Val{false}, steady_state = false, eval_expression = false, eval_module = @__MODULE__, checkbounds = true, cse = true) - return ObservedFunctionCache( - sys, Dict(), steady_state, eval_expression, eval_module, checkbounds, cse) + if expression == Val{true} + :($ObservedFunctionCache($sys, Dict(), $steady_state, $eval_expression, + $eval_module, $checkbounds, $cse)) + else + ObservedFunctionCache( + sys, Dict(), steady_state, eval_expression, eval_module, checkbounds, cse) + end end # This is hit because ensemble problems do a deepcopy diff --git a/src/systems/problem_utils.jl b/src/systems/problem_utils.jl index a224a10df0..75342938bb 100644 --- a/src/systems/problem_utils.jl +++ b/src/systems/problem_utils.jl @@ -1458,7 +1458,8 @@ function (st::SymbolicTstops)(p, tspan) end function SymbolicTstops( - sys::AbstractSystem; eval_expression = false, eval_module = @__MODULE__) + sys::AbstractSystem; expression = Val{false}, eval_expression = false, + eval_module = @__MODULE__) tstops = symbolic_tstops(sys) isempty(tstops) && return nothing t0 = gensym(:t0) @@ -1477,9 +1478,14 @@ function SymbolicTstops( t1; expression = Val{true}, p_start = 1, p_end = length(rps), add_observed = false, force_SA = true) - tstops = eval_or_rgf(tstops; eval_expression, eval_module) - tstops = GeneratedFunctionWrapper{(1, 3, is_split(sys))}(tstops, nothing) - return SymbolicTstops(tstops) + tstops = GeneratedFunctionWrapper{(1, 3, is_split(sys))}( + expression, tstops, nothing; eval_expression, eval_module) + + if expression == Val{true} + return :($SymbolicTstops($tstops)) + else + return SymbolicTstops(tstops) + end end """ From 8b9cb5a05190c334e6da2d0dc57ecc0df123d99a Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 14 May 2025 12:54:51 +0530 Subject: [PATCH 1652/2176] refactor: pass `u0` and `p` as kwargs in `process_SciMLProblem` feat: handle `expression = Val{true}` in `process_kwargs` --- src/systems/problem_utils.jl | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/systems/problem_utils.jl b/src/systems/problem_utils.jl index 75342938bb..c366bd81b9 100644 --- a/src/systems/problem_utils.jl +++ b/src/systems/problem_utils.jl @@ -1371,7 +1371,7 @@ function process_SciMLProblem( length(eqs), u0, p)))) end - f = constructor(sys, dvs, ps, u0; p = p, + f = constructor(sys; u0 = u0, p = p, eval_expression = eval_expression, eval_module = eval_module, kwargs...) @@ -1421,18 +1421,20 @@ function SciMLBase.detect_cycles(sys::AbstractSystem, varmap::Dict{Any, Any}, va return !isempty(cycles) end -function process_kwargs(sys::System; callback = nothing, eval_expression = false, - eval_module = @__MODULE__, kwargs...) +function process_kwargs(sys::System; expression = Val{false}, callback = nothing, + eval_expression = false, eval_module = @__MODULE__, kwargs...) kwargs = filter_kwargs(kwargs) kwargs1 = (;) if is_time_dependent(sys) - cbs = process_events(sys; callback, eval_expression, eval_module, kwargs...) - if cbs !== nothing - kwargs1 = merge(kwargs1, (callback = cbs,)) + if expression == Val{false} + cbs = process_events(sys; callback, eval_expression, eval_module, kwargs...) + if cbs !== nothing + kwargs1 = merge(kwargs1, (callback = cbs,)) + end end - tstops = SymbolicTstops(sys; eval_expression, eval_module) + tstops = SymbolicTstops(sys; expression, eval_expression, eval_module) if tstops !== nothing kwargs1 = merge(kwargs1, (; tstops)) end From 4c5841aa5d7efe2703563b068f383010eb8154f7 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 28 Apr 2025 18:43:39 +0530 Subject: [PATCH 1653/2176] feat: add `maybe_codegen_scimlfn` and `maybe_codegen_scimlproblem` --- src/systems/problem_utils.jl | 94 ++++++++++++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) diff --git a/src/systems/problem_utils.jl b/src/systems/problem_utils.jl index c366bd81b9..3bc8d9d8ac 100644 --- a/src/systems/problem_utils.jl +++ b/src/systems/problem_utils.jl @@ -1581,6 +1581,100 @@ macro fallback_iip_specialize(ex) end |> esc end +""" + $(TYPEDSIGNATURES) + +Turn key-value pairs in `kws` into assignments and appent them to `block.args`. `head` is +the head of the `Expr` used to create the assignment. `filter` is a function that takes the +key and returns whether or not to include it in the assignments. +""" +function namedtuple_to_assignments!( + block, kws::NamedTuple; head = :(=), filter = Returns(true)) + for (k, v) in pairs(kws) + filter(k) || continue + push!(block.args, Expr(head, k, v)) + end +end + +""" + $(TYPEDSIGNATURES) + +Build an expression that constructs SciMLFunction `T`. `args` is a `NamedTuple` mapping +names of positional arguments to `T` to their (expression) values. `kwargs` are parsed +as keyword arguments to the constructor. +""" +function build_scimlfn_expr(T, args::NamedTuple; kwargs...) + kwargs = NamedTuple(kwargs) + let_args = Expr(:block) + namedtuple_to_assignments!(let_args, args) + + kwexpr = Expr(:parameters) + # don't include initialization data in the generated expression + filter = !isequal(:initialization_data) + namedtuple_to_assignments!(let_args, kwargs; filter = filter) + namedtuple_to_assignments!(kwexpr, kwargs; head = :kw, filter) + let_body = Expr(:call, T, kwexpr, keys(args)...) + return Expr(:let, let_args, let_body) +end + +""" + $(TYPEDSIGNATURES) + +Build an expression that constructs SciMLProblem `T`. `args` is a `NamedTuple` mapping +names of positional arguments to `T` to their (expression) values. `kwargs` are parsed +as keyword arguments to the constructor. +""" +function build_scimlproblem_expr(T, args::NamedTuple; kwargs...) + kwargs = NamedTuple(kwargs) + let_args = Expr(:block) + namedtuple_to_assignments!(let_args, args) + + kwexpr = Expr(:parameters) + namedtuple_to_assignments!(let_args, kwargs) + namedtuple_to_assignments!(kwexpr, kwargs; head = :kw) + let_body = Expr(:call, remake, Expr(:call, T, kwexpr, keys(args)...)) + return Expr(:let, let_args, let_body) +end + +""" + $(TYPEDSIGNATURES) + +Return an expression constructing SciMLFunction `T` with positional arguments `args` +and keywords `kwargs`. +""" +function maybe_codegen_scimlfn(::Type{Val{true}}, T, args::NamedTuple; kwargs...) + build_scimlfn_expr(T, args; kwargs...) +end + +""" + $(TYPEDSIGNATURES) + +Construct SciMLFunction `T` with positional arguments `args` and keywords `kwargs`. +""" +function maybe_codegen_scimlfn(::Type{Val{false}}, T, args::NamedTuple; kwargs...) + T(args...; kwargs...) +end + +""" + $(TYPEDSIGNATURES) + +Return an expression constructing SciMLProblem `T` with positional arguments `args` +and keywords `kwargs`. +""" +function maybe_codegen_scimlproblem(::Type{Val{true}}, T, args::NamedTuple; kwargs...) + build_scimlproblem_expr(T, args; kwargs...) +end + +""" + $(TYPEDSIGNATURES) + +Construct SciMLProblem `T` with positional arguments `args` and keywords `kwargs`. +""" +function maybe_codegen_scimlproblem(::Type{Val{false}}, T, args::NamedTuple; kwargs...) + # Call `remake` so it runs initialization if it is trivial + remake(T(args...; kwargs...)) +end + ############## # Legacy functions for backward compatibility ############## From 9d5ebdad8a5c40902c612f7198c56523b1c8e32e Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 14 Apr 2025 23:17:20 +0530 Subject: [PATCH 1654/2176] feat: implement `ODEProblem` and `ODEFunction` for `System` refactor: use new compiled `generate_*` functions --- src/ModelingToolkit.jl | 1 + src/problems/odeproblem.jl | 94 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 95 insertions(+) create mode 100644 src/problems/odeproblem.jl diff --git a/src/ModelingToolkit.jl b/src/ModelingToolkit.jl index c54c4474ba..d32d8f7f2d 100644 --- a/src/ModelingToolkit.jl +++ b/src/ModelingToolkit.jl @@ -168,6 +168,7 @@ include("systems/problem_utils.jl") include("linearization.jl") include("problems/compatibility.jl") +include("problems/odeproblem.jl") include("systems/optimization/modelingtoolkitize.jl") include("systems/nonlinear/homotopy_continuation.jl") diff --git a/src/problems/odeproblem.jl b/src/problems/odeproblem.jl new file mode 100644 index 0000000000..42e49a0aea --- /dev/null +++ b/src/problems/odeproblem.jl @@ -0,0 +1,94 @@ +@fallback_iip_specialize function SciMLBase.ODEFunction{iip, spec}( + sys::System; u0 = nothing, p = nothing, tgrad = false, jac = false, + t = nothing, eval_expression = false, eval_module = @__MODULE__, sparse = false, + steady_state = false, checkbounds = false, sparsity = false, analytic = nothing, + simplify = false, cse = true, initialization_data = nothing, expression = Val{false}, + check_compatibility = true, kwargs...) where {iip, spec} + check_complete(sys, ODEFunction) + check_compatibility && check_compatible_system(ODEFunction, sys) + + dvs = unknowns(sys) + ps = parameters(sys) + f = generate_rhs(sys, dvs, ps; expression, wrap_gfw = Val{true}, + eval_expression, eval_module, checkbounds = checkbounds, cse, + kwargs...) + + if spec === SciMLBase.FunctionWrapperSpecialize && iip + if u0 === nothing || p === nothing || t === nothing + error("u0, p, and t must be specified for FunctionWrapperSpecialize on ODEFunction.") + end + if expression == Val{true} + f = :($(SciMLBase.wrapfun_iip)($f, ($u0, $u0, $p, $t))) + else + f = SciMLBase.wrapfun_iip(f, (u0, u0, p, t)) + end + end + + if tgrad + _tgrad = generate_tgrad( + sys, dvs, ps; expression, wrap_gfw = Val{true}, + simplify, cse, eval_expression, eval_module, checkbounds, kwargs...) + else + _tgrad = nothing + end + + if jac + _jac = generate_jacobian( + sys; expression, wrap_gfw = Val{true}, + simplify, sparse, cse, eval_expression, eval_module, checkbounds, kwargs...) + else + _jac = nothing + end + + M = calculate_massmatrix(sys) + _M = concrete_massmatrix(M; sparse, u0) + + observedfun = ObservedFunctionCache( + sys; expression, steady_state, eval_expression, eval_module, checkbounds, cse) + + _W_sparsity = W_sparsity(sys) + W_prototype = calculate_W_prototype(_W_sparsity; u0, sparse) + + args = (; f) + kwargs = (; + sys = sys, + jac = _jac, + tgrad = _tgrad, + mass_matrix = _M, + jac_prototype = W_prototype, + observed = observedfun, + sparsity = sparsity ? _W_sparsity : nothing, + analytic = analytic, + initialization_data) + + maybe_codegen_scimlfn(expression, ODEFunction{iip, spec}, args; kwargs...) +end + +@fallback_iip_specialize function SciMLBase.ODEProblem{iip, spec}( + sys::System, u0map, tspan, parammap = SciMLBase.NullParameters(); + callback = nothing, check_length = true, eval_expression = false, + expression = Val{false}, eval_module = @__MODULE__, check_compatibility = true, + kwargs...) where {iip, spec} + check_complete(sys, ODEProblem) + check_compatibility && check_compatible_system(ODEProblem, sys) + + f, u0, p = process_SciMLProblem(ODEFunction{iip, spec}, sys, u0map, parammap; + t = tspan !== nothing ? tspan[1] : tspan, check_length, eval_expression, + eval_module, expression, check_compatibility, kwargs...) + + kwargs = process_kwargs( + sys; expression, callback, eval_expression, eval_module, kwargs...) + + args = (; f, u0, tspan, p, ptype = StandardODEProblem()) + maybe_codegen_scimlproblem(expression, ODEProblem{iip}, args; kwargs...) +end + +function check_compatible_system(T::Union{Type{ODEFunction}, Type{ODEProblem}}, sys::System) + check_time_dependent(sys, T) + check_not_dde(sys, T) + check_no_cost(sys, T) + check_no_constraints(sys, T) + check_no_jumps(sys, T) + check_no_noise(sys, T) + check_is_continuous(sys, T) +end From 556c0a7761def12d9ef23cbd621c35b83403c779 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 14 May 2025 11:46:15 +0530 Subject: [PATCH 1655/2176] feat: implement `DDEFunction`, `DDEProblem` for `System` --- src/ModelingToolkit.jl | 1 + src/problems/ddeproblem.jl | 86 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 87 insertions(+) create mode 100644 src/problems/ddeproblem.jl diff --git a/src/ModelingToolkit.jl b/src/ModelingToolkit.jl index d32d8f7f2d..26a0e16edc 100644 --- a/src/ModelingToolkit.jl +++ b/src/ModelingToolkit.jl @@ -169,6 +169,7 @@ include("linearization.jl") include("problems/compatibility.jl") include("problems/odeproblem.jl") +include("problems/ddeproblem.jl") include("systems/optimization/modelingtoolkitize.jl") include("systems/nonlinear/homotopy_continuation.jl") diff --git a/src/problems/ddeproblem.jl b/src/problems/ddeproblem.jl new file mode 100644 index 0000000000..dfc660effd --- /dev/null +++ b/src/problems/ddeproblem.jl @@ -0,0 +1,86 @@ +@fallback_iip_specialize function SciMLBase.DDEFunction{iip, spec}( + sys::System; u0 = nothing, p = nothing, eval_expression = false, + eval_module = @__MODULE__, expression = Val{false}, checkbounds = false, + initialization_data = nothing, cse = true, check_compatibility = true, + sparse = false, simplify = false, analytic = nothing, kwargs...) where {iip, spec} + check_complete(sys, DDEFunction) + check_compatibility && check_compatible_system(DDEFunction, sys) + + dvs = unknowns(sys) + ps = parameters(sys) + + f = generate_rhs(sys, dvs, ps; expression, wrap_gfw = Val{true}, + eval_expression, eval_module, checkbounds = checkbounds, cse, + kwargs...) + + if spec === SciMLBase.FunctionWrapperSpecialize && iip + if u0 === nothing || p === nothing || t === nothing + error("u0, p, and t must be specified for FunctionWrapperSpecialize on DDEFunction.") + end + if expression == Val{true} + f = :($(SciMLBase.wrapfun_iip)($f, ($u0, $u0, $p, $t))) + else + f = SciMLBase.wrapfun_iip(f, (u0, u0, p, t)) + end + end + + M = calculate_massmatrix(sys) + _M = concrete_massmatrix(M; sparse, u0) + + observedfun = ObservedFunctionCache( + sys; expression, eval_expression, eval_module, checkbounds, cse) + + kwargs = (; + sys = sys, + mass_matrix = _M, + observed = observedfun, + analytic = analytic, + initialization_data) + args = (; f) + + return maybe_codegen_scimlfn(expression, DDEFunction{iip, spec}, args; kwargs...) +end + +@fallback_iip_specialize function SciMLBase.DDEProblem{iip, spec}( + sys::System, u0map, tspan, parammap = SciMLBase.NullParameters(); + callback = nothing, check_length = true, cse = true, checkbounds = false, + eval_expression = false, eval_module = @__MODULE__, check_compatibility = true, + u0_constructor = identity, expression = Val{false}, kwargs...) where {iip, spec} + check_complete(sys, DDEProblem) + check_compatibility && check_compatible_system(DDEProblem, sys) + + f, u0, p = process_SciMLProblem(DDEFunction{iip, spec}, sys, u0map, parammap; + t = tspan !== nothing ? tspan[1] : tspan, check_length, cse, checkbounds, + eval_expression, eval_module, check_compatibility, symbolic_u0 = true, + expression, u0_constructor, kwargs...) + + h = generate_history( + sys, u0; expression, wrap_gfw = Val{true}, cse, eval_expression, eval_module, + checkbounds) + + if expression == Val{true} + if u0 !== nothing + u0 = :($u0_constructor($map($float, h(p, tspan[1])))) + end + else + if u0 !== nothing + u0 = u0_constructor(float.(h(p, tspan[1]))) + end + end + + kwargs = process_kwargs( + sys; expression, callback, eval_expression, eval_module, kwargs...) + args = (; f, u0, h, tspan, p) + + return maybe_codegen_scimlproblem(expression, DDEProblem{iip}, args; kwargs...) +end + +function check_compatible_system(T::Union{Type{DDEFunction}, Type{DDEProblem}}, sys::System) + check_time_dependent(sys, T) + check_is_dde(sys) + check_no_cost(sys, T) + check_no_constraints(sys, T) + check_no_jumps(sys, T) + check_no_noise(sys, T) + check_is_continuous(sys, T) +end From 78b2f1ce9ecec14c9dc6324e4ed05815d08e13fa Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 14 May 2025 11:47:01 +0530 Subject: [PATCH 1656/2176] feat: implement `DAEProblem` and `DAEFunction` for `System` --- src/ModelingToolkit.jl | 1 + src/problems/daeproblem.jl | 87 ++++++++++++++++++++++++++++++++++++++ src/problems/odeproblem.jl | 6 ++- 3 files changed, 92 insertions(+), 2 deletions(-) create mode 100644 src/problems/daeproblem.jl diff --git a/src/ModelingToolkit.jl b/src/ModelingToolkit.jl index 26a0e16edc..cee03fb5ff 100644 --- a/src/ModelingToolkit.jl +++ b/src/ModelingToolkit.jl @@ -170,6 +170,7 @@ include("linearization.jl") include("problems/compatibility.jl") include("problems/odeproblem.jl") include("problems/ddeproblem.jl") +include("problems/daeproblem.jl") include("systems/optimization/modelingtoolkitize.jl") include("systems/nonlinear/homotopy_continuation.jl") diff --git a/src/problems/daeproblem.jl b/src/problems/daeproblem.jl new file mode 100644 index 0000000000..d1f8893cd5 --- /dev/null +++ b/src/problems/daeproblem.jl @@ -0,0 +1,87 @@ +@fallback_iip_specialize function SciMLBase.DAEFunction{iip, spec}( + sys::System; u0 = nothing, p = nothing, tgrad = false, jac = false, + t = nothing, eval_expression = false, eval_module = @__MODULE__, sparse = false, + steady_state = false, checkbounds = false, sparsity = false, analytic = nothing, + simplify = false, cse = true, initialization_data = nothing, + expression = Val{false}, check_compatibility = true, kwargs...) where {iip, spec} + check_complete(sys, DAEFunction) + check_compatibility && check_compatible_system(DAEFunction, sys) + + dvs = unknowns(sys) + ps = parameters(sys) + f = generate_rhs(sys, dvs, ps; expression, wrap_gfw = Val{true}, + implicit_dae = true, eval_expression, eval_module, checkbounds = checkbounds, cse, + kwargs...) + + if spec === SciMLBase.FunctionWrapperSpecialize && iip + if u0 === nothing || p === nothing || t === nothing + error("u0, p, and t must be specified for FunctionWrapperSpecialize on ODEFunction.") + end + if expression == Val{true} + f = :($(SciMLBase.wrapfun_iip)($f, ($u0, $u0, $u0, $p, $t))) + else + f = SciMLBase.wrapfun_iip(f, (u0, u0, u0, p, t)) + end + end + + if jac + _jac = generate_dae_jacobian(sys, dvs, ps; expression, + wrap_gfw = Val{true}, simplify, sparse, cse, eval_expression, eval_module, + checkbounds, kwargs...) + else + _jac = nothing + end + + observedfun = ObservedFunctionCache( + sys; expression, steady_state, eval_expression, eval_module, checkbounds, cse) + + jac_prototype = if sparse + uElType = u0 === nothing ? Float64 : eltype(u0) + if jac + J1 = calculate_jacobian(sys, sparse = sparse) + derivatives = Differential(get_iv(sys)).(unknowns(sys)) + J2 = calculate_jacobian(sys; sparse = sparse, dvs = derivatives) + similar(J1 + J2, uElType) + else + similar(jacobian_dae_sparsity(sys), uElType) + end + else + nothing + end + + kwargs = (; + sys = sys, + jac = _jac, + jac_prototype = jac_prototype, + observed = observedfun, + analytic = analytic, + initialization_data) + args = (; f) + + return maybe_codegen_scimlfn(expression, DAEFunction{iip, spec}, args; kwargs...) +end + +@fallback_iip_specialize function SciMLBase.DAEProblem{iip, spec}( + sys::System, du0map, u0map, tspan, parammap = SciMLBase.NullParameters(); + callback = nothing, check_length = true, eval_expression = false, + eval_module = @__MODULE__, check_compatibility = true, + expression = Val{false}, kwargs...) where {iip, spec} + check_complete(sys, DAEProblem) + check_compatibility && check_compatible_system(DAEProblem, sys) + + f, du0, u0, p = process_SciMLProblem(DAEFunction{iip, spec}, sys, u0map, parammap; + du0map, t = tspan !== nothing ? tspan[1] : tspan, check_length, eval_expression, + eval_module, check_compatibility, implicit_dae = true, expression, kwargs...) + + kwargs = process_kwargs(sys; expression, callback, eval_expression, eval_module, + kwargs...) + + diffvars = collect_differential_variables(sys) + sts = unknowns(sys) + differential_vars = map(Base.Fix2(in, diffvars), sts) + + args = (; f, du0, u0, tspan, p) + kwargs = (; differential_vars, kwargs...) + + return maybe_codegen_scimlproblem(expression, DAEProblem{iip}, args; kwargs...) +end diff --git a/src/problems/odeproblem.jl b/src/problems/odeproblem.jl index 42e49a0aea..0d5a146c48 100644 --- a/src/problems/odeproblem.jl +++ b/src/problems/odeproblem.jl @@ -83,9 +83,11 @@ end maybe_codegen_scimlproblem(expression, ODEProblem{iip}, args; kwargs...) end -function check_compatible_system(T::Union{Type{ODEFunction}, Type{ODEProblem}}, sys::System) +function check_compatible_system( + T::Union{Type{ODEFunction}, Type{ODEProblem}, Type{DAEFunction}, Type{DAEProblem}}, + sys::System) check_time_dependent(sys, T) - check_not_dde(sys, T) + check_not_dde(sys) check_no_cost(sys, T) check_no_constraints(sys, T) check_no_jumps(sys, T) From fa7f1f7551fdfea33d854a764517976cd0ccfc17 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 14 May 2025 11:48:22 +0530 Subject: [PATCH 1657/2176] feat: implement `SDEProblem` and `SDEFunction` for `System` --- src/ModelingToolkit.jl | 1 + src/problems/sdeproblem.jl | 120 +++++++++++++++++++++++++++++++++++++ 2 files changed, 121 insertions(+) create mode 100644 src/problems/sdeproblem.jl diff --git a/src/ModelingToolkit.jl b/src/ModelingToolkit.jl index cee03fb5ff..4304864087 100644 --- a/src/ModelingToolkit.jl +++ b/src/ModelingToolkit.jl @@ -171,6 +171,7 @@ include("problems/compatibility.jl") include("problems/odeproblem.jl") include("problems/ddeproblem.jl") include("problems/daeproblem.jl") +include("problems/sdeproblem.jl") include("systems/optimization/modelingtoolkitize.jl") include("systems/nonlinear/homotopy_continuation.jl") diff --git a/src/problems/sdeproblem.jl b/src/problems/sdeproblem.jl new file mode 100644 index 0000000000..a6270973f2 --- /dev/null +++ b/src/problems/sdeproblem.jl @@ -0,0 +1,120 @@ +@fallback_iip_specialize function SciMLBase.SDEFunction{iip, spec}( + sys::System; u0 = nothing, p = nothing, tgrad = false, jac = false, + t = nothing, eval_expression = false, eval_module = @__MODULE__, sparse = false, + steady_state = false, checkbounds = false, sparsity = false, analytic = nothing, + simplify = false, cse = true, initialization_data = nothing, + check_compatibility = true, expression = Val{false}, kwargs...) where {iip, spec} + check_complete(sys, SDEFunction) + check_compatibility && check_compatible_system(SDEFunction, sys) + + dvs = unknowns(sys) + ps = parameters(sys) + f = generate_rhs(sys, dvs, ps; expression, wrap_gfw = Val{true}, + eval_expression, eval_module, checkbounds = checkbounds, cse, + kwargs...) + g = generate_diffusion_function(sys, dvs, ps; expression, + wrap_gfw = Val{true}, eval_expression, eval_module, checkbounds, cse, kwargs...) + + if spec === SciMLBase.FunctionWrapperSpecialize && iip + if u0 === nothing || p === nothing || t === nothing + error("u0, p, and t must be specified for FunctionWrapperSpecialize on SDEFunction.") + end + if expression == Val{true} + f = :($(SciMLBase.wrapfun_iip)($f, ($u0, $u0, $p, $t))) + else + f = SciMLBase.wrapfun_iip(f, (u0, u0, p, t)) + end + end + + if tgrad + _tgrad = generate_tgrad(sys, dvs, ps; expression, + wrap_gfw = Val{true}, simplify, cse, eval_expression, eval_module, checkbounds, + kwargs...) + else + _tgrad = nothing + end + + if jac + _jac = generate_jacobian(sys; expression, + wrap_gfw = Val{true}, simplify, sparse, cse, eval_expression, eval_module, + checkbounds, kwargs...) + else + _jac = nothing + end + + M = calculate_massmatrix(sys) + _M = concrete_massmatrix(M; sparse, u0) + + observedfun = ObservedFunctionCache( + sys; expression, steady_state, eval_expression, eval_module, checkbounds, cse) + + _W_sparsity = W_sparsity(sys) + W_prototype = calculate_W_prototype(_W_sparsity; u0, sparse) + + kwargs = (; + sys = sys, + jac = _jac, + tgrad = _tgrad, + mass_matrix = _M, + jac_prototype = W_prototype, + observed = observedfun, + sparsity = sparsity ? _W_sparsity : nothing, + analytic = analytic, + initialization_data) + args = (; f, g) + + return maybe_codegen_scimlfn(expression, SDEFunction{iip, spec}, args; kwargs...) +end + +@fallback_iip_specialize function SciMLBase.SDEProblem{iip, spec}( + sys::System, u0map, tspan, parammap = SciMLBase.NullParameters(); + callback = nothing, check_length = true, eval_expression = false, + eval_module = @__MODULE__, check_compatibility = true, sparse = false, + sparsenoise = sparse, expression = Val{false}, kwargs...) where {iip, spec} + check_complete(sys, SDEProblem) + check_compatibility && check_compatible_system(SDEProblem, sys) + + f, u0, p = process_SciMLProblem(SDEFunction{iip, spec}, sys, u0map, parammap; + t = tspan !== nothing ? tspan[1] : tspan, check_length, eval_expression, + eval_module, check_compatibility, sparse, expression, kwargs...) + + noise, noise_rate_prototype = calculate_noise_and_rate_prototype(sys, u0; sparsenoise) + kwargs = process_kwargs(sys; expression, callback, eval_expression, eval_module, + kwargs...) + + args = (; f, u0, tspan, p) + kwargs = (; noise, noise_rate_prototype, kwargs...) + + return maybe_codegen_scimlproblem(expression, SDEProblem{iip}, args; kwargs...) +end + +function check_compatible_system(T::Union{Type{SDEFunction}, Type{SDEProblem}}, sys::System) + check_time_dependent(sys, T) + check_not_dde(sys) + check_no_cost(sys, T) + check_no_constraints(sys, T) + check_no_jumps(sys, T) + check_has_noise(sys, T) + check_is_continuous(sys, T) +end + +function calculate_noise_and_rate_prototype(sys::System, u0; sparsenoise = false) + noiseeqs = get_noise_eqs(sys) + if noiseeqs isa AbstractVector + # diagonal noise + noise_rate_prototype = nothing + noise = nothing + elseif size(noiseeqs, 2) == 1 + # scalar noise + noise_rate_prototype = nothing + noise = WienerProcess(0.0, 0.0, 0.0) + elseif sparsenoise + I, J, V = findnz(SparseArrays.sparse(noiseeqs)) + noise_rate_prototype = SparseArrays.sparse(I, J, zero(eltype(u0))) + noise = nothing + else + noise_rate_prototype = zeros(eltype(u0), size(noiseeqs)) + noise = nothing + end + return noise, noise_rate_prototype +end From f3433db063d663eef006e491c1952b89d77132dc Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 16 Apr 2025 17:38:55 +0530 Subject: [PATCH 1658/2176] feat: implement `SDDEProblem`, `SDDEFunction` for `System` --- src/ModelingToolkit.jl | 1 + src/problems/sddeproblem.jl | 96 +++++++++++++++++++++++++++++++++++++ 2 files changed, 97 insertions(+) create mode 100644 src/problems/sddeproblem.jl diff --git a/src/ModelingToolkit.jl b/src/ModelingToolkit.jl index 4304864087..67148369e5 100644 --- a/src/ModelingToolkit.jl +++ b/src/ModelingToolkit.jl @@ -172,6 +172,7 @@ include("problems/odeproblem.jl") include("problems/ddeproblem.jl") include("problems/daeproblem.jl") include("problems/sdeproblem.jl") +include("problems/sddeproblem.jl") include("systems/optimization/modelingtoolkitize.jl") include("systems/nonlinear/homotopy_continuation.jl") diff --git a/src/problems/sddeproblem.jl b/src/problems/sddeproblem.jl new file mode 100644 index 0000000000..3bc20c0412 --- /dev/null +++ b/src/problems/sddeproblem.jl @@ -0,0 +1,96 @@ +@fallback_iip_specialize function SciMLBase.SDDEFunction{iip, spec}( + sys::System; u0 = nothing, p = nothing, expression = Val{false}, + eval_expression = false, eval_module = @__MODULE__, checkbounds = false, + initialization_data = nothing, cse = true, check_compatibility = true, + sparse = false, simplify = false, analytic = nothing, kwargs...) where {iip, spec} + check_complete(sys, SDDEFunction) + check_compatibility && check_compatible_system(SDDEFunction, sys) + + dvs = unknowns(sys) + ps = parameters(sys) + + f = generate_rhs(sys, dvs, ps; expression, wrap_gfw = Val{true}, + eval_expression, eval_module, checkbounds = checkbounds, cse, kwargs...) + g = generate_diffusion_function(sys, dvs, ps; expression, + wrap_gfw = Val{true}, eval_expression, eval_module, checkbounds, cse, kwargs...) + + if spec === SciMLBase.FunctionWrapperSpecialize && iip + if u0 === nothing || p === nothing || t === nothing + error("u0, p, and t must be specified for FunctionWrapperSpecialize on SDDEFunction.") + end + if expression == Val{true} + f = :($(SciMLBase.wrapfun_iip)($f, ($u0, $u0, $p, $t))) + else + f = SciMLBase.wrapfun_iip(f, (u0, u0, p, t)) + end + end + + M = calculate_massmatrix(sys) + _M = concrete_massmatrix(M; sparse, u0) + + observedfun = ObservedFunctionCache( + sys; expression, eval_expression, eval_module, checkbounds, cse) + + kwargs = (; + sys = sys, + mass_matrix = _M, + observed = observedfun, + analytic = analytic, + initialization_data) + args = (; f, g) + + return maybe_codegen_scimlfn(expression, SDDEFunction{iip, spec}, args; kwargs...) +end + +@fallback_iip_specialize function SciMLBase.SDDEProblem{iip, spec}( + sys::System, u0map, tspan, parammap = SciMLBase.NullParameters(); + callback = nothing, check_length = true, cse = true, checkbounds = false, + eval_expression = false, eval_module = @__MODULE__, check_compatibility = true, + u0_constructor = identity, sparse = false, sparsenoise = sparse, + expression = Val{false}, kwargs...) where {iip, spec} + check_complete(sys, SDDEProblem) + check_compatibility && check_compatible_system(SDDEProblem, sys) + + f, u0, p = process_SciMLProblem(SDDEFunction{iip, spec}, sys, u0map, parammap; + t = tspan !== nothing ? tspan[1] : tspan, check_length, cse, checkbounds, + eval_expression, eval_module, check_compatibility, sparse, symbolic_u0 = true, + expression, u0_constructor, kwargs...) + + h = generate_history( + sys, u0; expression, wrap_gfw = Val{true}, cse, eval_expression, eval_module, + checkbounds) + + if expression == Val{true} + if u0 !== nothing + u0 = :($u0_constructor($map($float, h(p, tspan[1])))) + end + else + if u0 !== nothing + u0 = u0_constructor(float.(h(p, tspan[1]))) + end + end + + noise, noise_rate_prototype = calculate_noise_and_rate_prototype(sys, u0; sparsenoise) + kwargs = process_kwargs(sys; callback, eval_expression, eval_module, kwargs...) + + if expression == Val{true} + g = :(f.g) + else + g = f.g + end + args = (; f, g, u0, h, tspan, p) + kwargs = (; noise, noise_rate_prototype, kwargs...) + + return maybe_codegen_scimlproblem(expression, SDDEProblem{iip}, args; kwargs...) +end + +function check_compatible_system( + T::Union{Type{SDDEFunction}, Type{SDDEProblem}}, sys::System) + check_time_dependent(sys, T) + check_is_dde(sys) + check_no_cost(sys, T) + check_no_constraints(sys, T) + check_no_jumps(sys, T) + check_has_noise(sys, T) + check_is_continuous(sys, T) +end From aeb23165fa39a34c7d94e9c465a83133cb5a67d7 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 14 May 2025 11:49:06 +0530 Subject: [PATCH 1659/2176] feat: implement `NonlinearProblem`, `NonlinearFunction` for `System` --- src/ModelingToolkit.jl | 1 + src/problems/nonlinearproblem.jl | 85 ++++++++++++++++++++++++++++++++ 2 files changed, 86 insertions(+) create mode 100644 src/problems/nonlinearproblem.jl diff --git a/src/ModelingToolkit.jl b/src/ModelingToolkit.jl index 67148369e5..75ff8a0711 100644 --- a/src/ModelingToolkit.jl +++ b/src/ModelingToolkit.jl @@ -173,6 +173,7 @@ include("problems/ddeproblem.jl") include("problems/daeproblem.jl") include("problems/sdeproblem.jl") include("problems/sddeproblem.jl") +include("problems/nonlinearproblem.jl") include("systems/optimization/modelingtoolkitize.jl") include("systems/nonlinear/homotopy_continuation.jl") diff --git a/src/problems/nonlinearproblem.jl b/src/problems/nonlinearproblem.jl new file mode 100644 index 0000000000..b4f846f3a4 --- /dev/null +++ b/src/problems/nonlinearproblem.jl @@ -0,0 +1,85 @@ +@fallback_iip_specialize function SciMLBase.NonlinearFunction{iip, spec}( + sys::System; u0 = nothing, p = nothing, jac = false, + eval_expression = false, eval_module = @__MODULE__, sparse = false, + checkbounds = false, sparsity = false, analytic = nothing, + simplify = false, cse = true, initialization_data = nothing, + resid_prototype = nothing, check_compatibility = true, expression = Val{false}, + kwargs...) where {iip, spec} + check_complete(sys, NonlinearFunction) + check_compatibility && check_compatible_system(NonlinearFunction, sys) + + dvs = unknowns(sys) + ps = parameters(sys) + f = generate_rhs(sys, dvs, ps; expression, wrap_gfw = Val{true}, + eval_expression, eval_module, checkbounds = checkbounds, cse, + kwargs...) + + if spec === SciMLBase.FunctionWrapperSpecialize && iip + if u0 === nothing || p === nothing + error("u0, and p must be specified for FunctionWrapperSpecialize on NonlinearFunction.") + end + if expression == Val{true} + f = :($(SciMLBase.wrapfun_iip)($f, ($u0, $u0, $p))) + else + f = SciMLBase.wrapfun_iip(f, (u0, u0, p)) + end + end + + if jac + _jac = generate_jacobian(sys; expression, + wrap_gfw = Val{true}, simplify, sparse, cse, eval_expression, eval_module, + checkbounds, kwargs...) + else + _jac = nothing + end + + observedfun = ObservedFunctionCache( + sys; steady_state = false, expression, eval_expression, eval_module, checkbounds, + cse) + + if sparse + jac_prototype = similar(calculate_jacobian(sys; sparse), eltype(u0)) + else + jac_prototype = nothing + end + + kwargs = (; + sys = sys, + jac = _jac, + observed = observedfun, + analytic = analytic, + jac_prototype, + resid_prototype, + initialization_data) + args = (; f) + + return maybe_codegen_scimlfn(expression, NonlinearFunction{iip, spec}, args; kwargs...) +end + +@fallback_iip_specialize function SciMLBase.NonlinearProblem{iip, spec}( + sys::System, u0map, parammap = SciMLBase.NullParameters(); expression = Val{false}, + check_length = true, check_compatibility = true, kwargs...) where {iip, spec} + check_complete(sys, NonlinearProblem) + if is_time_dependent(sys) + sys = NonlinearSystem(sys) + end + check_compatibility && check_compatible_system(NonlinearProblem, sys) + + f, u0, p = process_SciMLProblem(NonlinearFunction{iip, spec}, sys, u0map, parammap; + check_length, check_compatibility, expression, kwargs...) + + kwargs = process_kwargs(sys; kwargs...) + args = (; f, u0, p, ptype = StandardNonlinearProblem()) + + return maybe_codegen_scimlproblem(expression, NonlinearProblem{iip}, args; kwargs...) +end + +function check_compatible_system( + T::Union{Type{NonlinearFunction}, Type{NonlinearProblem}}, sys::System) + check_time_independent(sys, T) + check_not_dde(sys) + check_no_cost(sys, T) + check_no_constraints(sys, T) + check_no_jumps(sys, T) + check_no_noise(sys, T) +end From 575e302311438c3d21e29d0a7b75e5e1e516a810 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 16 Apr 2025 19:58:55 +0530 Subject: [PATCH 1660/2176] feat: implement `IntervalNonlinearProblem`, `IntervalNonlinearFunction` for `System` --- src/ModelingToolkit.jl | 1 + src/problems/intervalnonlinearproblem.jl | 62 ++++++++++++++++++++++++ 2 files changed, 63 insertions(+) create mode 100644 src/problems/intervalnonlinearproblem.jl diff --git a/src/ModelingToolkit.jl b/src/ModelingToolkit.jl index 75ff8a0711..5bcfc09119 100644 --- a/src/ModelingToolkit.jl +++ b/src/ModelingToolkit.jl @@ -174,6 +174,7 @@ include("problems/daeproblem.jl") include("problems/sdeproblem.jl") include("problems/sddeproblem.jl") include("problems/nonlinearproblem.jl") +include("problems/intervalnonlinearproblem.jl") include("systems/optimization/modelingtoolkitize.jl") include("systems/nonlinear/homotopy_continuation.jl") diff --git a/src/problems/intervalnonlinearproblem.jl b/src/problems/intervalnonlinearproblem.jl new file mode 100644 index 0000000000..9c9e5d8688 --- /dev/null +++ b/src/problems/intervalnonlinearproblem.jl @@ -0,0 +1,62 @@ +function SciMLBase.IntervalNonlinearFunction( + sys::System; u0 = nothing, p = nothing, eval_expression = false, + eval_module = @__MODULE__, expression = Val{false}, checkbounds = false, + analytic = nothing, cse = true, initialization_data = nothing, + check_compatibility = true, kwargs...) + check_complete(sys, IntervalNonlinearFunction) + check_compatibility && check_compatible_system(IntervalNonlinearFunction, sys) + + dvs = unknowns(sys) + ps = parameters(sys) + f = generate_rhs(sys, dvs, ps; expression, wrap_gfw = Val{true}, + scalar = true, eval_expression, eval_module, checkbounds, cse, kwargs...) + + observedfun = ObservedFunctionCache( + sys; steady_state = false, expression, eval_expression, eval_module, checkbounds, + cse) + + args = (; f) + kwargs = (; + sys = sys, + observed = observedfun, + analytic = analytic, + initialization_data) + + return maybe_codegen_scimlfn( + expression, IntervalNonlinearFunction{false}, args; kwargs...) +end + +function SciMLBase.IntervalNonlinearProblem( + sys::System, uspan::NTuple{2}, parammap = SciMLBase.NullParameters(); + check_compatibility = true, expression = Val{false}, kwargs...) + check_complete(sys, IntervalNonlinearProblem) + check_compatibility && check_compatible_system(IntervalNonlinearProblem, sys) + + u0map = unknowns(sys) .=> uspan[1] + f, u0, p = process_SciMLProblem(IntervalNonlinearFunction, sys, u0map, parammap; + check_compatibility, expression, kwargs...) + + kwargs = process_kwargs(sys; kwargs...) + args = (; f, uspan, p) + return maybe_codegen_scimlproblem(expression, IntervalNonlinearProblem, args; kwargs...) +end + +function check_compatible_system( + T::Union{Type{IntervalNonlinearFunction}, Type{IntervalNonlinearProblem}}, sys::System) + check_time_independent(sys, T) + if !isone(length(unknowns(sys))) + throw(SystemCompatibilityError(""" + `$T` requires a system with a single unknown. Found `$(unknowns(sys))`. + """)) + end + if !isone(length(equations(sys))) + throw(SystemCompatibilityError(""" + `$T` requires a system with a single equation. Found `$(equations(sys))`. + """)) + end + check_not_dde(sys) + check_no_cost(sys, T) + check_no_constraints(sys, T) + check_no_jumps(sys, T) + check_no_noise(sys, T) +end From 90baa3d76a8f2e37cd2aaa1db6d5061d3e2b4a72 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 14 May 2025 11:51:24 +0530 Subject: [PATCH 1661/2176] feat: implement `ImplicitDiscreteProblem` `ImplicitDiscreteFunction` for `System` --- src/ModelingToolkit.jl | 1 + src/problems/implicitdiscreteproblem.jl | 74 +++++++++++++++++++++++++ 2 files changed, 75 insertions(+) create mode 100644 src/problems/implicitdiscreteproblem.jl diff --git a/src/ModelingToolkit.jl b/src/ModelingToolkit.jl index 5bcfc09119..308d17e21f 100644 --- a/src/ModelingToolkit.jl +++ b/src/ModelingToolkit.jl @@ -175,6 +175,7 @@ include("problems/sdeproblem.jl") include("problems/sddeproblem.jl") include("problems/nonlinearproblem.jl") include("problems/intervalnonlinearproblem.jl") +include("problems/implicitdiscreteproblem.jl") include("systems/optimization/modelingtoolkitize.jl") include("systems/nonlinear/homotopy_continuation.jl") diff --git a/src/problems/implicitdiscreteproblem.jl b/src/problems/implicitdiscreteproblem.jl new file mode 100644 index 0000000000..476265e049 --- /dev/null +++ b/src/problems/implicitdiscreteproblem.jl @@ -0,0 +1,74 @@ +@fallback_iip_specialize function SciMLBase.ImplicitDiscreteFunction{iip, spec}( + sys::System; u0 = nothing, p = nothing, t = nothing, eval_expression = false, + eval_module = @__MODULE__, expression = Val{false}, + checkbounds = false, analytic = nothing, simplify = false, cse = true, + initialization_data = nothing, check_compatibility = true, kwargs...) where { + iip, spec} + check_complete(sys, ImplicitDiscreteFunction) + check_compatibility && check_compatible_system(ImplicitDiscreteFunction, sys) + + iv = get_iv(sys) + dvs = unknowns(sys) + ps = parameters(sys) + f = generate_rhs(sys, dvs, ps; expression, wrap_gfw = Val{true}, + implicit_dae = true, eval_expression, eval_module, checkbounds = checkbounds, cse, + override_discrete = true, kwargs...) + + if spec === SciMLBase.FunctionWrapperSpecialize && iip + if u0 === nothing || p === nothing || t === nothing + error("u0, p, and t must be specified for FunctionWrapperSpecialize on ImplicitDiscreteFunction.") + end + f = SciMLBase.wrapfun_iip(f, (u0, u0, u0, p, t)) + end + + if length(dvs) == length(equations(sys)) + resid_prototype = nothing + else + resid_prototype = calculate_resid_prototype(length(equations(sys)), u0, p) + end + + observedfun = ObservedFunctionCache( + sys; steady_state = false, expression, eval_expression, eval_module, checkbounds, cse) + + args = (; f) + kwargs = (; + sys = sys, + observed = observedfun, + analytic = analytic, + initialization_data, + resid_prototype) + + return maybe_codegen_scimlfn( + expression, ImplicitDiscreteFunction{iip, spec}, args; kwargs...) +end + +@fallback_iip_specialize function SciMLBase.ImplicitDiscreteProblem{iip, spec}( + sys::System, u0map, tspan, parammap = SciMLBase.NullParameters(); + check_compatibility = true, expression = Val{false}, kwargs...) where {iip, spec} + check_complete(sys, ImplicitDiscreteProblem) + check_compatibility && check_compatible_system(ImplicitDiscreteProblem, sys) + + dvs = unknowns(sys) + u0map = to_varmap(u0map, dvs) + add_toterms!(u0map; replace = true) + f, u0, p = process_SciMLProblem( + ImplicitDiscreteFunction{iip, spec}, sys, u0map, parammap; + t = tspan !== nothing ? tspan[1] : tspan, check_compatibility, + expression, kwargs...) + + kwargs = process_kwargs(sys; kwargs...) + args = (; f, u0, tspan, p) + return maybe_codegen_scimlproblem( + expression, ImplicitDiscreteProblem{iip}, args; kwargs...) +end + +function check_compatible_system( + T::Union{Type{ImplicitDiscreteFunction}, Type{ImplicitDiscreteProblem}}, sys::System) + check_time_dependent(sys, T) + check_not_dde(sys) + check_no_cost(sys, T) + check_no_constraints(sys, T) + check_no_jumps(sys, T) + check_no_noise(sys, T) + check_is_discrete(sys, T) +end From 025e319a4114e5b3651554abcaab2b4a5e660b4d Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 14 May 2025 11:50:09 +0530 Subject: [PATCH 1662/2176] feat: implement `DiscreteProblem` and `DiscreteFunction` for `System` refactor: move `shift_u0map_forward` to `discreteproblem.jl` --- src/ModelingToolkit.jl | 1 + src/problems/discreteproblem.jl | 104 ++++++++++++++++++++++++++++++++ 2 files changed, 105 insertions(+) create mode 100644 src/problems/discreteproblem.jl diff --git a/src/ModelingToolkit.jl b/src/ModelingToolkit.jl index 308d17e21f..7a50089985 100644 --- a/src/ModelingToolkit.jl +++ b/src/ModelingToolkit.jl @@ -176,6 +176,7 @@ include("problems/sddeproblem.jl") include("problems/nonlinearproblem.jl") include("problems/intervalnonlinearproblem.jl") include("problems/implicitdiscreteproblem.jl") +include("problems/discreteproblem.jl") include("systems/optimization/modelingtoolkitize.jl") include("systems/nonlinear/homotopy_continuation.jl") diff --git a/src/problems/discreteproblem.jl b/src/problems/discreteproblem.jl new file mode 100644 index 0000000000..d77efed225 --- /dev/null +++ b/src/problems/discreteproblem.jl @@ -0,0 +1,104 @@ +@fallback_iip_specialize function SciMLBase.DiscreteFunction{iip, spec}( + sys::System; u0 = nothing, p = nothing, t = nothing, + eval_expression = false, eval_module = @__MODULE__, expression = Val{false}, + checkbounds = false, analytic = nothing, simplify = false, cse = true, + initialization_data = nothing, check_compatibility = true, + kwargs...) where {iip, spec} + check_complete(sys, DiscreteFunction) + check_compatibility && check_compatible_system(DiscreteFunction, sys) + + dvs = unknowns(sys) + ps = parameters(sys) + f = generate_rhs(sys, dvs, ps; expression, wrap_gfw = Val{true}, + eval_expression, eval_module, checkbounds = checkbounds, cse, + kwargs...) + + if spec === SciMLBase.FunctionWrapperSpecialize && iip + if u0 === nothing || p === nothing || t === nothing + error("u0, p, and t must be specified for FunctionWrapperSpecialize on DiscreteFunction.") + end + if expression == Val{true} + f = :($(SciMLBase.wrapfun_iip)($f, ($u0, $u0, $p, $t))) + else + f = SciMLBase.wrapfun_iip(f, (u0, u0, p, t)) + end + end + + observedfun = ObservedFunctionCache( + sys; steady_state = false, expression, eval_expression, eval_module, checkbounds, + cse) + + kwargs = (; + sys = sys, + observed = observedfun, + analytic = analytic, + initialization_data) + args = (; f) + + return maybe_codegen_scimlfn(expression, DiscreteFunction{iip, spec}, args; kwargs...) +end + +@fallback_iip_specialize function SciMLBase.DiscreteProblem{iip, spec}( + sys::System, u0map, tspan, parammap = SciMLBase.NullParameters(); + check_compatibility = true, expression = Val{false}, kwargs...) where {iip, spec} + check_complete(sys, DiscreteProblem) + check_compatibility && check_compatible_system(DiscreteProblem, sys) + + dvs = unknowns(sys) + u0map = to_varmap(u0map, dvs) + add_toterms!(u0map; replace = true) + f, u0, p = process_SciMLProblem(DiscreteFunction{iip, spec}, sys, u0map, parammap; + t = tspan !== nothing ? tspan[1] : tspan, check_compatibility, expression, + kwargs...) + + if expression == Val{true} + u0 = :(f($u0, p, tspan[1])) + else + u0 = f(u0, p, tspan[1]) + end + + kwargs = process_kwargs(sys; kwargs...) + args = (; f, u0, tspan, p) + + return maybe_codegen_scimlproblem(expression, DiscreteProblem{iip}, args; kwargs...) +end + +function check_compatible_system( + T::Union{Type{DiscreteFunction}, Type{DiscreteProblem}}, sys::System) + check_time_dependent(sys, T) + check_not_dde(sys) + check_no_cost(sys, T) + check_no_constraints(sys, T) + check_no_jumps(sys, T) + check_no_noise(sys, T) + check_is_discrete(sys, T) + check_is_explicit(sys, T, ImplicitDiscreteProblem) +end + +function shift_u0map_forward(sys::System, u0map, defs) + iv = get_iv(sys) + updated = AnyDict() + for k in collect(keys(u0map)) + v = u0map[k] + if !((op = operation(k)) isa Shift) + isnothing(getunshifted(k)) && + error("Initial conditions must be for the past state of the unknowns. Instead of providing the condition for $k, provide the condition for $(Shift(iv, -1)(k)).") + + updated[Shift(iv, 1)(k)] = v + elseif op.steps > 0 + error("Initial conditions must be for the past state of the unknowns. Instead of providing the condition for $k, provide the condition for $(Shift(iv, -1)(only(arguments(k)))).") + else + updated[Shift(iv, op.steps + 1)(only(arguments(k)))] = v + end + end + for var in unknowns(sys) + op = operation(var) + root = getunshifted(var) + shift = getshift(var) + isnothing(root) && continue + (haskey(updated, Shift(iv, shift)(root)) || haskey(updated, var)) && continue + haskey(defs, root) || error("Initial condition for $var not provided.") + updated[var] = defs[root] + end + return updated +end From cd19b841ac0cdf4065bf84c6afe64abd89552a1e Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 17 Apr 2025 19:45:39 +0530 Subject: [PATCH 1663/2176] feat: allow manually choosing time-independent initialization --- src/systems/nonlinear/initializesystem.jl | 11 +++++++---- src/systems/problem_utils.jl | 23 ++++++++++++++++------- 2 files changed, 23 insertions(+), 11 deletions(-) diff --git a/src/systems/nonlinear/initializesystem.jl b/src/systems/nonlinear/initializesystem.jl index 056d35df37..0396074300 100644 --- a/src/systems/nonlinear/initializesystem.jl +++ b/src/systems/nonlinear/initializesystem.jl @@ -1,5 +1,6 @@ -function generate_initializesystem(sys::AbstractSystem; kwargs...) - if is_time_dependent(sys) +function generate_initializesystem( + sys::AbstractSystem; time_dependent_init = is_time_dependent(sys), kwargs...) + if time_dependent_init generate_initializesystem_timevarying(sys; kwargs...) else generate_initializesystem_timeindependent(sys; kwargs...) @@ -544,6 +545,7 @@ function SciMLBase.remake_initialization_data( merge!(guesses, meta.guesses) use_scc = meta.use_scc initialization_eqs = meta.additional_initialization_eqs + time_dependent_init = meta.time_dependent_init else # there is no initializeprob, so the original problem construction # had no solvable parameters and had the differential variables @@ -599,8 +601,9 @@ function SciMLBase.remake_initialization_data( typeof(newp.initials), floatT, Val(1), Val(length(vals)), vals...) end kws = maybe_build_initialization_problem( - sys, SciMLBase.isinplace(odefn), op, u0map, pmap, t0, defs, guesses, missing_unknowns; - use_scc, initialization_eqs, floatT, u0_constructor, p_constructor, allow_incomplete = true) + sys, SciMLBase.isinplace(odefn), op, u0map, pmap, t0, defs, guesses, + missing_unknowns; time_dependent_init, use_scc, initialization_eqs, floatT, + u0_constructor, p_constructor, allow_incomplete = true) odefn = remake(odefn; kws...) return SciMLBase.remake_initialization_data(sys, odefn, newu0, t0, newp, newu0, newp) diff --git a/src/systems/problem_utils.jl b/src/systems/problem_utils.jl index 3bc8d9d8ac..867ee80ddb 100644 --- a/src/systems/problem_utils.jl +++ b/src/systems/problem_utils.jl @@ -911,6 +911,10 @@ struct InitializationMetadata{R <: ReconstructInitializeprob, GUU, SIU} """ use_scc::Bool """ + Whether the initialization uses the independent variable. + """ + time_dependent_init::Bool + """ `ReconstructInitializeprob` for this initialization problem. """ oop_reconstruct_u0_p::R @@ -1015,7 +1019,8 @@ All other keyword arguments are forwarded to `InitializationProblem`. """ function maybe_build_initialization_problem( sys::AbstractSystem, iip, op::AbstractDict, u0map, pmap, t, defs, - guesses, missing_unknowns; implicit_dae = false, u0_constructor = identity, + guesses, missing_unknowns; implicit_dae = false, + time_dependent_init = is_time_dependent(sys), u0_constructor = identity, p_constructor = identity, floatT = Float64, initialization_eqs = [], use_scc = true, kwargs...) guesses = merge(ModelingToolkit.guesses(sys), todict(guesses)) @@ -1025,7 +1030,7 @@ function maybe_build_initialization_problem( end initializeprob = ModelingToolkit.InitializationProblem{iip}( - sys, t, u0map, pmap; guesses, initialization_eqs, + sys, t, u0map, pmap; guesses, time_dependent_init, initialization_eqs, use_scc, u0_constructor, p_constructor, kwargs...) if state_values(initializeprob) !== nothing _u0 = state_values(initializeprob) @@ -1060,11 +1065,15 @@ function maybe_build_initialization_problem( end meta = InitializationMetadata( u0map, pmap, guesses, Vector{Equation}(initialization_eqs), - use_scc, ReconstructInitializeprob( + use_scc, time_dependent_init, + ReconstructInitializeprob( sys, initializeprob.f.sys; u0_constructor, p_constructor), get_initial_unknowns, SetInitialUnknowns(sys)) - if is_time_dependent(sys) + if time_dependent_init === nothing + time_dependent_init = is_time_dependent(sys) + end + if time_dependent_init all_init_syms = Set(all_symbols(initializeprob)) solved_unknowns = filter(var -> var in all_init_syms, unknowns(sys)) initializeprobmap = u0_constructor ∘ getu(initializeprob, solved_unknowns) @@ -1101,7 +1110,7 @@ function maybe_build_initialization_problem( end end - if is_time_dependent(sys) + if time_dependent_init for v in missing_unknowns op[v] = getu(initializeprob, v)(initializeprob) end @@ -1221,7 +1230,7 @@ function process_SciMLProblem( check_length = true, symbolic_u0 = false, warn_cyclic_dependency = false, circular_dependency_max_cycle_length = length(all_symbols(sys)), circular_dependency_max_cycles = 10, - substitution_limit = 100, use_scc = true, + substitution_limit = 100, use_scc = true, time_dependent_init = is_time_dependent(sys), force_initialization_time_independent = false, algebraic_only = false, allow_incomplete = false, is_initializeprob = false, kwargs...) dvs = unknowns(sys) @@ -1282,7 +1291,7 @@ function process_SciMLProblem( warn_cyclic_dependency, check_units = check_initialization_units, circular_dependency_max_cycle_length, circular_dependency_max_cycles, use_scc, force_time_independent = force_initialization_time_independent, algebraic_only, allow_incomplete, - u0_constructor, p_constructor, floatT) + u0_constructor, p_constructor, floatT, time_dependent_init) kwargs = merge(kwargs, kws) end From 3d5e4c08972bee90d6eda2544187f24006358e45 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 14 May 2025 11:54:53 +0530 Subject: [PATCH 1664/2176] feat: implement `OptimizationProblem` and `OptimizationFunction` for `System` --- src/ModelingToolkit.jl | 1 + src/problems/optimizationproblem.jl | 149 ++++++++++++++++++++++++++++ 2 files changed, 150 insertions(+) create mode 100644 src/problems/optimizationproblem.jl diff --git a/src/ModelingToolkit.jl b/src/ModelingToolkit.jl index 7a50089985..f9a0423e21 100644 --- a/src/ModelingToolkit.jl +++ b/src/ModelingToolkit.jl @@ -177,6 +177,7 @@ include("problems/nonlinearproblem.jl") include("problems/intervalnonlinearproblem.jl") include("problems/implicitdiscreteproblem.jl") include("problems/discreteproblem.jl") +include("problems/optimizationproblem.jl") include("systems/optimization/modelingtoolkitize.jl") include("systems/nonlinear/homotopy_continuation.jl") diff --git a/src/problems/optimizationproblem.jl b/src/problems/optimizationproblem.jl new file mode 100644 index 0000000000..243d453ada --- /dev/null +++ b/src/problems/optimizationproblem.jl @@ -0,0 +1,149 @@ +function SciMLBase.OptimizationFunction(sys::System, args...; kwargs...) + return OptimizationFunction{true}(sys, args...; kwargs...) +end + +function SciMLBase.OptimizationFunction{iip}(sys::System; + u0 = nothing, p = nothing, grad = false, hess = false, + sparse = false, cons_j = false, cons_h = false, cons_sparse = false, + linenumbers = true, eval_expression = false, eval_module = @__MODULE__, + simplify = false, check_compatibility = true, checkbounds = false, cse = true, + expression = Val{false}, kwargs...) where {iip} + check_complete(sys, OptimizationFunction) + check_compatibility && check_compatible_system(OptimizationFunction, sys) + dvs = unknowns(sys) + ps = parameters(sys) + cstr = constraints(sys) + + f = generate_cost(sys; expression, wrap_gfw = Val{true}, eval_expression, + eval_module, checkbounds, cse, kwargs...) + + if grad + _grad = generate_cost_gradient(sys; expression, wrap_gfw = Val{true}, + eval_expression, eval_module, checkbounds, cse, kwargs...) + else + _grad = nothing + end + if hess + _hess, hess_prototype = generate_cost_hessian( + sys; expression, wrap_gfw = Val{true}, eval_expression, + eval_module, checkbounds, cse, sparse, simplify, return_sparsity = true, + kwargs...) + else + _hess = hess_prototype = nothing + if sparse + hess_prototype = cost_hessian_sparsity(sys) + end + end + if isempty(cstr) + cons = _cons_j = cons_jac_prototype = _cons_h = nothing + cons_hess_prototype = cons_expr = nothing + else + cons = generate_cons(sys; expression, wrap_gfw = Val{true}, + eval_expression, eval_module, checkbounds, cse, kwargs...) + if cons_j + _cons_j, cons_jac_prototype = generate_constraint_jacobian( + sys; expression, wrap_gfw = Val{true}, eval_expression, + eval_module, checkbounds, cse, simplify, sparse = cons_sparse, + return_sparsity = true, kwargs...) + else + _cons_j = cons_jac_prototype = nothing + end + if cons_h + _cons_h, cons_hess_prototype = generate_constraint_hessian( + sys; expression, wrap_gfw = Val{true}, eval_expression, + eval_module, checkbounds, cse, simplify, sparse = cons_sparse, + return_sparsity = true, kwargs...) + else + _cons_h = cons_hess_prototype = nothing + end + cons_expr = subs_constants(cstr) + end + + obj_expr = subs_constants(cost(sys)) + + observedfun = ObservedFunctionCache( + sys; expression, eval_expression, eval_module, checkbounds, cse) + + args = (; f, ad = SciMLBase.NoAD()) + kwargs = (; + sys = sys, + grad = _grad, + hess = _hess, + hess_prototype = hess_prototype, + cons = cons, + cons_j = _cons_j, + cons_jac_prototype = cons_jac_prototype, + cons_h = _cons_h, + cons_hess_prototype = cons_hess_prototype, + cons_expr = cons_expr, + expr = obj_expr, + observed = observedfun) + + return maybe_codegen_scimlfn(expression, OptimizationFunction{iip}, args; kwargs...) +end + +function SciMLBase.OptimizationProblem(sys::System, args...; kwargs...) + return OptimizationProblem{true}(sys, args...; kwargs...) +end + +function SciMLBase.OptimizationProblem{iip}( + sys::System, u0map, parammap = SciMLBase.NullParameters(); lb = nothing, + ub = nothing, check_compatibility = true, expression = Val{false}, + kwargs...) where {iip} + check_complete(sys, OptimizationProblem) + check_compatibility && check_compatible_system(OptimizationProblem, sys) + + f, u0, p = process_SciMLProblem(OptimizationFunction{iip}, sys, u0map, parammap; + check_compatibility, tofloat = false, check_length = false, expression, kwargs...) + + dvs = unknowns(sys) + int = symtype.(unwrap.(dvs)) .<: Integer + if lb === nothing && ub === nothing + lb = first.(getbounds.(dvs)) + ub = last.(getbounds.(dvs)) + isboolean = symtype.(unwrap.(dvs)) .<: Bool + lb[isboolean] .= 0 + ub[isboolean] .= 1 + else + xor(isnothing(lb), isnothing(ub)) && + throw(ArgumentError("Expected both `lb` and `ub` to be supplied")) + !isnothing(lb) && length(lb) != length(dvs) && + throw(ArgumentError("Expected both `lb` to be of the same length as the vector of optimization variables")) + !isnothing(ub) && length(ub) != length(dvs) && + throw(ArgumentError("Expected both `ub` to be of the same length as the vector of optimization variables")) + end + + ps = parameters(sys) + defs = merge(defaults(sys), to_varmap(parammap, ps), to_varmap(u0map, dvs)) + 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 + ub = nothing + end + + cstr = constraints(sys) + if isempty(cstr) + lcons = ucons = nothing + else + lcons = fill(-Inf, length(cstr)) + ucons = zeros(length(cstr)) + lcons[findall(Base.Fix2(isa, Equation), cstr)] .= 0.0 + end + + kwargs = process_kwargs(sys; kwargs...) + kwargs = (; lb, ub, int, lcons, ucons, kwargs...) + args = (; f, u0, p) + return maybe_codegen_scimlproblem(expression, OptimizationProblem{iip}, args; kwargs...) +end + +function check_compatible_system( + T::Union{Type{OptimizationFunction}, Type{OptimizationProblem}}, sys::System) + check_time_independent(sys, T) + check_not_dde(sys) + check_has_cost(sys, T) + check_no_jumps(sys, T) + check_no_noise(sys, T) + check_no_equations(sys, T) +end From b875120db2d930f20d44ba9b2921bbc9925a53b4 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 14 May 2025 12:10:33 +0530 Subject: [PATCH 1665/2176] feat: implement `JumpProblem` for `System` refactor: move jumpsystem functions to generalized locations --- src/ModelingToolkit.jl | 1 + src/problems/jumpproblem.jl | 223 ++++++++++++++++++++++++++++++++++++ src/systems/codegen.jl | 25 ++++ 3 files changed, 249 insertions(+) create mode 100644 src/problems/jumpproblem.jl diff --git a/src/ModelingToolkit.jl b/src/ModelingToolkit.jl index f9a0423e21..7e5b7e3caa 100644 --- a/src/ModelingToolkit.jl +++ b/src/ModelingToolkit.jl @@ -178,6 +178,7 @@ include("problems/intervalnonlinearproblem.jl") include("problems/implicitdiscreteproblem.jl") include("problems/discreteproblem.jl") include("problems/optimizationproblem.jl") +include("problems/jumpproblem.jl") include("systems/optimization/modelingtoolkitize.jl") include("systems/nonlinear/homotopy_continuation.jl") diff --git a/src/problems/jumpproblem.jl b/src/problems/jumpproblem.jl new file mode 100644 index 0000000000..242b9549fa --- /dev/null +++ b/src/problems/jumpproblem.jl @@ -0,0 +1,223 @@ +@fallback_iip_specialize function JumpProcesses.JumpProblem{iip, spec}( + sys::System, u0map, tspan::Union{Tuple, Nothing}, pmap = SciMLBase.NullParameters(); + check_compatibility = true, eval_expression = false, eval_module = @__MODULE__, + checkbounds = false, cse = true, aggregator = JumpProcesses.NullAggregator(), + callback = nothing, rng = nothing, kwargs...) where {iip, spec} + check_complete(sys, JumpProblem) + check_compatibility && check_compatible_system(JumpProblem, sys) + + has_vrjs = any(x -> x isa VariableRateJump, jumps(sys)) + has_eqs = !isempty(equations(sys)) + has_noise = get_noise_eqs(sys) !== nothing + + if (has_vrjs || has_eqs) + if has_eqs && has_noise + prob = SDEProblem{iip, spec}( + sys, u0map, tspan, pmap; check_compatibility = false, + build_initializeprob = false, checkbounds, cse, check_length = false, + kwargs...) + elseif has_eqs + prob = ODEProblem{iip, spec}( + sys, u0map, tspan, pmap; check_compatibility = false, + build_initializeprob = false, checkbounds, cse, check_length = false, + kwargs...) + else + _, u0, p = process_SciMLProblem(EmptySciMLFunction{iip}, sys, u0map, pmap; + t = tspan === nothing ? nothing : tspan[1], tofloat = false, + check_length = false, build_initializeprob = false) + observedfun = ObservedFunctionCache(sys; eval_expression, eval_module, + checkbounds, cse) + f = (du, u, p, t) -> (du .= 0; nothing) + df = ODEFunction{true, spec}(f; sys, observed = observedfun) + prob = ODEProblem{true}(df, u0, tspan, p; kwargs...) + end + else + _f, u0, p = process_SciMLProblem(EmptySciMLFunction{iip}, sys, u0map, pmap; + t = tspan === nothing ? nothing : tspan[1], tofloat = false, check_length = false, build_initializeprob = false, cse) + f = DiffEqBase.DISCRETE_INPLACE_DEFAULT + + observedfun = ObservedFunctionCache( + sys; eval_expression, eval_module, checkbounds, cse) + + df = DiscreteFunction{true, true}(f; sys = sys, observed = observedfun, + initialization_data = get(_f.kwargs, :initialization_data, nothing)) + prob = DiscreteProblem(df, u0, tspan, p; kwargs...) + end + + dvs = unknowns(sys) + unknowntoid = Dict(value(unknown) => i for (i, unknown) in enumerate(dvs)) + js = jumps(sys) + invttype = prob.tspan[1] === nothing ? Float64 : typeof(1 / prob.tspan[2]) + + # handling parameter substitution and empty param vecs + p = (prob.p isa DiffEqBase.NullParameters || prob.p === nothing) ? Num[] : prob.p + + majpmapper = JumpSysMajParamMapper(sys, p; jseqs = js, rateconsttype = invttype) + _majs = Vector{MassActionJump}(filter(x -> x isa MassActionJump, js)) + _crjs = Vector{ConstantRateJump}(filter(x -> x isa ConstantRateJump, js)) + vrjs = Vector{VariableRateJump}(filter(x -> x isa VariableRateJump, js)) + majs = isempty(_majs) ? nothing : assemble_maj(_majs, unknowntoid, majpmapper) + crjs = ConstantRateJump[assemble_crj(sys, j, unknowntoid; eval_expression, eval_module) + for j in _crjs] + vrjs = VariableRateJump[assemble_vrj(sys, j, unknowntoid; eval_expression, eval_module) + for j in vrjs] + jset = JumpSet(Tuple(vrjs), Tuple(crjs), nothing, majs) + + # dep graphs are only for constant rate jumps + nonvrjs = ArrayPartition(_majs, _crjs) + if needs_vartojumps_map(aggregator) || needs_depgraph(aggregator) || + (aggregator isa JumpProcesses.NullAggregator) + jdeps = asgraph(sys; eqs = nonvrjs) + vdeps = variable_dependencies(sys; eqs = nonvrjs) + vtoj = jdeps.badjlist + jtov = vdeps.badjlist + jtoj = needs_depgraph(aggregator) ? eqeq_dependencies(jdeps, vdeps).fadjlist : + nothing + else + vtoj = nothing + jtov = nothing + jtoj = nothing + end + + # handle events, making sure to reset aggregators in the generated affect functions + cbs = process_events(sys; callback, eval_expression, eval_module, reset_jumps = true) + + if rng !== nothing + kwargs = (; kwargs..., rng) + end + return JumpProblem(prob, aggregator, jset; dep_graph = jtoj, vartojumps_map = vtoj, + jumptovars_map = jtov, scale_rates = false, nocopy = true, + callback = cbs, kwargs...) +end + +function check_compatible_system(T::Union{Type{JumpProblem}}, sys::System) + check_time_dependent(sys, T) + check_not_dde(sys) + check_no_cost(sys, T) + check_no_constraints(sys, T) + check_has_jumps(sys, T) + check_is_continuous(sys, T) +end + +###################### parameter mapper ########################### +struct JumpSysMajParamMapper{U, V, W} + paramexprs::U # the parameter expressions to use for each jump rate constant + sympars::V # parameters(sys) from the underlying JumpSystem + subdict::Any # mapping from an element of parameters(sys) to its current numerical value +end + +function JumpSysMajParamMapper(js::System, p; jseqs = nothing, rateconsttype = Float64) + eqs = (jseqs === nothing) ? jumps(js) : jseqs + majs = MassActionJump[x for x in eqs if x isa MassActionJump] + paramexprs = [maj.scaled_rates for maj in majs] + psyms = reduce(vcat, reorder_parameters(js); init = []) + paramdict = Dict(value(k) => value(v) for (k, v) in zip(psyms, vcat(p...))) + JumpSysMajParamMapper{typeof(paramexprs), typeof(psyms), rateconsttype}(paramexprs, + psyms, + paramdict) +end + +function updateparams!(ratemap::JumpSysMajParamMapper{U, V, W}, + params) where {U <: AbstractArray, V <: AbstractArray, W} + for (i, p) in enumerate(params) + sympar = ratemap.sympars[i] + ratemap.subdict[sympar] = p + end + nothing +end + +function updateparams!(ratemap::JumpSysMajParamMapper{U, V, W}, + params::MTKParameters) where {U <: AbstractArray, V <: AbstractArray, W} + for (i, p) in enumerate(ArrayPartition(params...)) + sympar = ratemap.sympars[i] + ratemap.subdict[sympar] = p + end + nothing +end + +function updateparams!(::JumpSysMajParamMapper{U, V, W}, + params::Nothing) where {U <: AbstractArray, V <: AbstractArray, W} + nothing +end + +# update a maj with parameter vectors +function (ratemap::JumpSysMajParamMapper{U, V, W})(maj::MassActionJump, newparams; + scale_rates, + kwargs...) where {U <: AbstractArray, + V <: AbstractArray, W} + updateparams!(ratemap, newparams) + for i in 1:get_num_majumps(maj) + maj.scaled_rates[i] = convert(W, + value(substitute(ratemap.paramexprs[i], + ratemap.subdict))) + end + scale_rates && JumpProcesses.scalerates!(maj.scaled_rates, maj.reactant_stoch) + nothing +end + +# create the initial parameter vector for use in a MassActionJump +function (ratemap::JumpSysMajParamMapper{ + U, + V, + W +})(params) where {U <: AbstractArray, + V <: AbstractArray, W} + updateparams!(ratemap, params) + [convert(W, value(substitute(paramexpr, ratemap.subdict))) + for paramexpr in ratemap.paramexprs] +end + +##### MTK dispatches for Symbolic jumps ##### +eqtype_supports_collect_vars(j::MassActionJump) = true +function collect_vars!(unknowns, parameters, j::MassActionJump, iv; depth = 0, + op = Differential) + collect_vars!(unknowns, parameters, j.scaled_rates, iv; depth, op) + for field in (j.reactant_stoch, j.net_stoch) + for el in field + collect_vars!(unknowns, parameters, el, iv; depth, op) + end + end + return nothing +end + +eqtype_supports_collect_vars(j::Union{ConstantRateJump, VariableRateJump}) = true +function collect_vars!(unknowns, parameters, j::Union{ConstantRateJump, VariableRateJump}, + iv; depth = 0, op = Differential) + collect_vars!(unknowns, parameters, j.rate, iv; depth, op) + for eq in j.affect! + (eq isa Equation) && collect_vars!(unknowns, parameters, eq, iv; depth, op) + end + return nothing +end + +### Functions to determine which unknowns a jump depends on +function get_variables!(dep, jump::Union{ConstantRateJump, VariableRateJump}, variables) + jr = value(jump.rate) + (jr isa Symbolic) && get_variables!(dep, jr, variables) + dep +end + +function get_variables!(dep, jump::MassActionJump, variables) + sr = value(jump.scaled_rates) + (sr isa Symbolic) && get_variables!(dep, sr, variables) + for varasop in jump.reactant_stoch + any(isequal(varasop[1]), variables) && push!(dep, varasop[1]) + end + dep +end + +### Functions to determine which unknowns are modified by a given jump +function modified_unknowns!(munknowns, jump::Union{ConstantRateJump, VariableRateJump}, sts) + for eq in jump.affect! + st = eq.lhs + any(isequal(st), sts) && push!(munknowns, st) + end + munknowns +end + +function modified_unknowns!(munknowns, jump::MassActionJump, sts) + for (unknown, stoich) in jump.net_stoch + any(isequal(unknown), sts) && push!(munknowns, unknown) + end + munknowns +end diff --git a/src/systems/codegen.jl b/src/systems/codegen.jl index acb319377a..60d3c4dd2d 100644 --- a/src/systems/codegen.jl +++ b/src/systems/codegen.jl @@ -561,6 +561,31 @@ function assemble_maj(majv::Vector{U}, unknowntoid, pmapper) where {U <: MassAct MassActionJump(rs, ns; param_mapper = pmapper, nocopy = true) end +function numericrstoich(mtrs::Vector{Pair{V, W}}, unknowntoid) where {V, W} + rs = Vector{Pair{Int, W}}() + for (wspec, stoich) in mtrs + spec = value(wspec) + if !iscall(spec) && _iszero(spec) + push!(rs, 0 => stoich) + else + push!(rs, unknowntoid[spec] => stoich) + end + end + sort!(rs) + rs +end + +function numericnstoich(mtrs::Vector{Pair{V, W}}, unknowntoid) where {V, W} + ns = Vector{Pair{Int, W}}() + for (wspec, stoich) in mtrs + spec = value(wspec) + !iscall(spec) && _iszero(spec) && + error("Net stoichiometry can not have a species labelled 0.") + push!(ns, unknowntoid[spec] => stoich) + end + sort!(ns) +end + """ build_explicit_observed_function(sys, ts; kwargs...) -> Function(s) From e973971d94acee4e521fb96a532ff7dacfe8f09e Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 14 May 2025 12:11:23 +0530 Subject: [PATCH 1666/2176] feat: add `InitializationProblem` --- src/ModelingToolkit.jl | 1 + src/problems/initializationproblem.jl | 155 ++++++++++++++++++++++++++ 2 files changed, 156 insertions(+) create mode 100644 src/problems/initializationproblem.jl diff --git a/src/ModelingToolkit.jl b/src/ModelingToolkit.jl index 7e5b7e3caa..f11bb7ad76 100644 --- a/src/ModelingToolkit.jl +++ b/src/ModelingToolkit.jl @@ -179,6 +179,7 @@ include("problems/implicitdiscreteproblem.jl") include("problems/discreteproblem.jl") include("problems/optimizationproblem.jl") include("problems/jumpproblem.jl") +include("problems/initializationproblem.jl") include("systems/optimization/modelingtoolkitize.jl") include("systems/nonlinear/homotopy_continuation.jl") diff --git a/src/problems/initializationproblem.jl b/src/problems/initializationproblem.jl new file mode 100644 index 0000000000..132376e3e6 --- /dev/null +++ b/src/problems/initializationproblem.jl @@ -0,0 +1,155 @@ +struct InitializationProblem{iip, specialization} end + +""" +```julia +InitializationProblem{iip}(sys::AbstractSystem, t, u0map, + parammap = DiffEqBase.NullParameters(); + version = nothing, tgrad = false, + jac = false, + checkbounds = false, sparse = false, + simplify = false, + linenumbers = true, parallel = SerialForm(), + initialization_eqs = [], + fully_determined = false, + kwargs...) where {iip} +``` + +Generates a NonlinearProblem or NonlinearLeastSquaresProblem from a System +which represents the initialization, i.e. the calculation of the consistent +initial conditions for the given DAE. +""" +@fallback_iip_specialize function InitializationProblem{iip, specialize}( + sys::AbstractSystem, + t, u0map = [], + parammap = DiffEqBase.NullParameters(); + guesses = [], + check_length = true, + warn_initialize_determined = true, + initialization_eqs = [], + fully_determined = nothing, + check_units = true, + use_scc = true, + allow_incomplete = false, + force_time_independent = false, + algebraic_only = false, + time_dependent_init = is_time_dependent(sys), + kwargs...) where {iip, specialize} + if !iscomplete(sys) + error("A completed system is required. Call `complete` or `structural_simplify` on the system before creating an `ODEProblem`") + end + if isempty(u0map) && get_initializesystem(sys) !== nothing + isys = get_initializesystem(sys; initialization_eqs, check_units) + simplify_system = false + elseif isempty(u0map) && get_initializesystem(sys) === nothing + isys = generate_initializesystem( + sys; initialization_eqs, check_units, pmap = parammap, + guesses, algebraic_only) + simplify_system = true + else + isys = generate_initializesystem( + sys; u0map, initialization_eqs, check_units, time_dependent_init, + pmap = parammap, guesses, algebraic_only) + simplify_system = true + end + + # useful for `SteadyStateProblem` since `f` has to be autonomous and the + # initialization should be too + if force_time_independent + idx = findfirst(isequal(get_iv(sys)), get_ps(isys)) + idx === nothing || deleteat!(get_ps(isys), idx) + end + + if simplify_system + isys = structural_simplify(isys; fully_determined, split = is_split(sys)) + end + + ts = get_tearing_state(isys) + unassigned_vars = StructuralTransformations.singular_check(ts) + if warn_initialize_determined && !isempty(unassigned_vars) + errmsg = """ + The initialization system is structurally singular. Guess values may \ + significantly affect the initial values of the ODE. The problematic variables \ + are $unassigned_vars. + + Note that the identification of problematic variables is a best-effort heuristic. + """ + @warn errmsg + end + + uninit = setdiff(unknowns(sys), [unknowns(isys); observables(isys)]) + + # TODO: throw on uninitialized arrays + filter!(x -> !(x isa Symbolics.Arr), uninit) + if time_dependent_init && !isempty(uninit) + allow_incomplete || throw(IncompleteInitializationError(uninit)) + # for incomplete initialization, we will add the missing variables as parameters. + # they will be updated by `update_initializeprob!` and `initializeprobmap` will + # use them to construct the new `u0`. + newparams = map(toparam, uninit) + append!(get_ps(isys), newparams) + isys = complete(isys) + end + + neqs = length(equations(isys)) + nunknown = length(unknowns(isys)) + + if use_scc + scc_message = "`SCCNonlinearProblem` can only be used for initialization of fully determined systems and hence will not be used here. " + else + scc_message = "" + end + + if warn_initialize_determined && neqs > nunknown + @warn "Initialization system is overdetermined. $neqs equations for $nunknown unknowns. Initialization will default to using least squares. $(scc_message)To suppress this warning pass warn_initialize_determined = false. To make this warning into an error, pass fully_determined = true" + end + if warn_initialize_determined && neqs < nunknown + @warn "Initialization system is underdetermined. $neqs equations for $nunknown unknowns. Initialization will default to using least squares. $(scc_message)To suppress this warning pass warn_initialize_determined = false. To make this warning into an error, pass fully_determined = true" + end + + parammap = recursive_unwrap(anydict(parammap)) + if t !== nothing + parammap[get_iv(sys)] = t + end + filter!(kvp -> kvp[2] !== missing, parammap) + + u0map = to_varmap(u0map, unknowns(sys)) + if isempty(guesses) + guesses = Dict() + end + + filter_missing_values!(u0map) + filter_missing_values!(parammap) + u0map = merge(ModelingToolkit.guesses(sys), todict(guesses), u0map) + + TProb = if neqs == nunknown && isempty(unassigned_vars) + if use_scc && neqs > 0 + if is_split(isys) + SCCNonlinearProblem + else + @warn "`SCCNonlinearProblem` can only be used with `split = true` systems. Simplify your `ODESystem` with `split = true` or pass `use_scc = false` to disable this warning" + NonlinearProblem + end + else + NonlinearProblem + end + else + NonlinearLeastSquaresProblem + end + TProb{iip}(isys, u0map, parammap; kwargs..., + build_initializeprob = false, is_initializeprob = true) +end + +const INCOMPLETE_INITIALIZATION_MESSAGE = """ + Initialization incomplete. Not all of the state variables of the + DAE system can be determined by the initialization. Missing + variables: + """ + +struct IncompleteInitializationError <: Exception + uninit::Any +end + +function Base.showerror(io::IO, e::IncompleteInitializationError) + println(io, INCOMPLETE_INITIALIZATION_MESSAGE) + println(io, e.uninit) +end From b4aa4b01ed3c07c503a4ea527b8e8b4d97389716 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Sun, 20 Apr 2025 21:50:27 +0530 Subject: [PATCH 1667/2176] feat: implement `NonlinearLeastSquaresProblem` for `System` --- src/problems/nonlinearproblem.jl | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/src/problems/nonlinearproblem.jl b/src/problems/nonlinearproblem.jl index b4f846f3a4..0d3ffad17f 100644 --- a/src/problems/nonlinearproblem.jl +++ b/src/problems/nonlinearproblem.jl @@ -74,8 +74,25 @@ end return maybe_codegen_scimlproblem(expression, NonlinearProblem{iip}, args; kwargs...) end +@fallback_iip_specialize function SciMLBase.NonlinearLeastSquaresProblem{iip, spec}( + sys::System, u0map, parammap = DiffEqBase.NullParameters(); check_length = false, + check_compatibility = true, expression = Val{false}, kwargs...) where {iip, spec} + check_complete(sys, NonlinearLeastSquaresProblem) + check_compatibility && check_compatible_system(NonlinearLeastSquaresProblem, sys) + + f, u0, p = process_SciMLProblem(NonlinearFunction{iip}, sys, u0map, parammap; + check_length, expression, kwargs...) + + kwargs = process_kwargs(sys; kwargs...) + args = (; f, u0, p) + + return maybe_codegen_scimlproblem( + expression, NonlinearLeastSquaresProblem{iip}, args; kwargs...) +end + function check_compatible_system( - T::Union{Type{NonlinearFunction}, Type{NonlinearProblem}}, sys::System) + T::Union{Type{NonlinearFunction}, Type{NonlinearProblem}, + Type{NonlinearLeastSquaresProblem}}, sys::System) check_time_independent(sys, T) check_not_dde(sys) check_no_cost(sys, T) @@ -83,3 +100,13 @@ function check_compatible_system( check_no_jumps(sys, T) check_no_noise(sys, T) end + +function calculate_resid_prototype(N, u0, p) + u0ElType = u0 === nothing ? Float64 : eltype(u0) + if SciMLStructures.isscimlstructure(p) + u0ElType = promote_type( + eltype(SciMLStructures.canonicalize(SciMLStructures.Tunable(), p)[1]), + u0ElType) + end + return zeros(u0ElType, N) +end From c3034b3d1ba53f8710684ad3b0decf609a5db170 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Sun, 20 Apr 2025 21:52:29 +0530 Subject: [PATCH 1668/2176] refactor: port `SCCNonlinearProblem` to separate file --- src/ModelingToolkit.jl | 1 + src/problems/sccnonlinearproblem.jl | 248 ++++++++++++++++++++++++++++ 2 files changed, 249 insertions(+) create mode 100644 src/problems/sccnonlinearproblem.jl diff --git a/src/ModelingToolkit.jl b/src/ModelingToolkit.jl index f11bb7ad76..2c8a86bbb2 100644 --- a/src/ModelingToolkit.jl +++ b/src/ModelingToolkit.jl @@ -180,6 +180,7 @@ include("problems/discreteproblem.jl") include("problems/optimizationproblem.jl") include("problems/jumpproblem.jl") include("problems/initializationproblem.jl") +include("problems/sccnonlinearproblem.jl") include("systems/optimization/modelingtoolkitize.jl") include("systems/nonlinear/homotopy_continuation.jl") diff --git a/src/problems/sccnonlinearproblem.jl b/src/problems/sccnonlinearproblem.jl new file mode 100644 index 0000000000..0b4174f552 --- /dev/null +++ b/src/problems/sccnonlinearproblem.jl @@ -0,0 +1,248 @@ +const TypeT = Union{DataType, UnionAll} + +struct CacheWriter{F} + fn::F +end + +function (cw::CacheWriter)(p, sols) + cw.fn(p.caches, sols, p) +end + +function CacheWriter(sys::AbstractSystem, buffer_types::Vector{TypeT}, + exprs::Dict{TypeT, Vector{Any}}, solsyms, obseqs::Vector{Equation}; + eval_expression = false, eval_module = @__MODULE__, cse = true) + ps = parameters(sys; initial_parameters = true) + rps = reorder_parameters(sys, ps) + obs_assigns = [eq.lhs ← eq.rhs for eq in obseqs] + body = map(eachindex(buffer_types), buffer_types) do i, T + Symbol(:tmp, i) ← SetArray(true, :(out[$i]), get(exprs, T, [])) + end + + function argument_name(i::Int) + if i <= length(solsyms) + return :($(generated_argument_name(1))[$i]) + end + return generated_argument_name(i - length(solsyms)) + end + array_assignments = array_variable_assignments(solsyms...; argument_name) + fn = build_function_wrapper( + sys, nothing, :out, + DestructuredArgs(DestructuredArgs.(solsyms), generated_argument_name(1)), + rps...; p_start = 3, p_end = length(rps) + 2, + expression = Val{true}, add_observed = false, cse, + extra_assignments = [array_assignments; obs_assigns; body]) + fn = eval_or_rgf(fn; eval_expression, eval_module) + fn = GeneratedFunctionWrapper{(3, 3, is_split(sys))}(fn, nothing) + return CacheWriter(fn) +end + +struct SCCNonlinearFunction{iip} end + +function SCCNonlinearFunction{iip}( + sys::System, _eqs, _dvs, _obs, cachesyms; eval_expression = false, + eval_module = @__MODULE__, cse = true, kwargs...) where {iip} + ps = parameters(sys; initial_parameters = true) + rps = reorder_parameters(sys, ps) + + obs_assignments = [eq.lhs ← eq.rhs for eq in _obs] + + rhss = [eq.rhs - eq.lhs for eq in _eqs] + f_gen = build_function_wrapper(sys, + rhss, _dvs, rps..., cachesyms...; p_start = 2, + p_end = length(rps) + length(cachesyms) + 1, add_observed = false, + extra_assignments = obs_assignments, expression = Val{true}, cse) + f_oop, f_iip = eval_or_rgf.(f_gen; eval_expression, eval_module) + f = GeneratedFunctionWrapper{(2, 2, is_split(sys))}(f_oop, f_iip) + + subsys = NonlinearSystem(_eqs, _dvs, ps; observed = _obs, + parameter_dependencies = parameter_dependencies(sys), name = nameof(sys)) + if get_index_cache(sys) !== nothing + @set! subsys.index_cache = subset_unknowns_observed( + get_index_cache(sys), sys, _dvs, getproperty.(_obs, (:lhs,))) + @set! subsys.complete = true + end + + return NonlinearFunction{iip}(f; sys = subsys) +end + +function SciMLBase.SCCNonlinearProblem(sys::NonlinearSystem, args...; kwargs...) + SCCNonlinearProblem{true}(sys, args...; kwargs...) +end + +function SciMLBase.SCCNonlinearProblem{iip}(sys::NonlinearSystem, u0map, + parammap = SciMLBase.NullParameters(); eval_expression = false, eval_module = @__MODULE__, + cse = true, kwargs...) where {iip} + if !iscomplete(sys) || get_tearing_state(sys) === nothing + error("A simplified `NonlinearSystem` is required. Call `structural_simplify` on the system before creating an `SCCNonlinearProblem`.") + end + + if !is_split(sys) + error("The system has been simplified with `split = false`. `SCCNonlinearProblem` is not compatible with this system. Pass `split = true` to `structural_simplify` to use `SCCNonlinearProblem`.") + end + + ts = get_tearing_state(sys) + var_eq_matching, var_sccs = StructuralTransformations.algebraic_variables_scc(ts) + + if length(var_sccs) == 1 + return NonlinearProblem{iip}( + sys, u0map, parammap; eval_expression, eval_module, kwargs...) + end + + condensed_graph = MatchedCondensationGraph( + DiCMOBiGraph{true}(complete(ts.structure.graph), + complete(var_eq_matching)), + var_sccs) + toporder = topological_sort_by_dfs(condensed_graph) + var_sccs = var_sccs[toporder] + eq_sccs = map(Base.Fix1(getindex, var_eq_matching), var_sccs) + + dvs = unknowns(sys) + ps = parameters(sys) + eqs = equations(sys) + obs = observed(sys) + + _, u0, p = process_SciMLProblem( + EmptySciMLFunction{iip}, sys, u0map, parammap; eval_expression, eval_module, kwargs...) + + explicitfuns = [] + nlfuns = [] + prevobsidxs = BlockArray(undef_blocks, Vector{Int}, Int[]) + # Cache buffer types and corresponding sizes. Stored as a pair of arrays instead of a + # dict to maintain a consistent order of buffers across SCCs + cachetypes = TypeT[] + cachesizes = Int[] + # explicitfun! related information for each SCC + # We need to compute buffer sizes before doing any codegen + scc_cachevars = Dict{TypeT, Vector{Any}}[] + scc_cacheexprs = Dict{TypeT, Vector{Any}}[] + scc_eqs = Vector{Equation}[] + scc_obs = Vector{Equation}[] + # variables solved in previous SCCs + available_vars = Set() + for (i, (escc, vscc)) in enumerate(zip(eq_sccs, var_sccs)) + # subset unknowns and equations + _dvs = dvs[vscc] + _eqs = eqs[escc] + # get observed equations required by this SCC + union!(available_vars, _dvs) + obsidxs = observed_equations_used_by(sys, _eqs; available_vars) + # the ones used by previous SCCs can be precomputed into the cache + setdiff!(obsidxs, prevobsidxs) + _obs = obs[obsidxs] + union!(available_vars, getproperty.(_obs, (:lhs,))) + + # get all subexpressions in the RHS which we can precompute in the cache + # precomputed subexpressions should not contain `banned_vars` + banned_vars = Set{Any}(vcat(_dvs, getproperty.(_obs, (:lhs,)))) + state = Dict() + for i in eachindex(_obs) + _obs[i] = _obs[i].lhs ~ subexpressions_not_involving_vars!( + _obs[i].rhs, banned_vars, state) + end + for i in eachindex(_eqs) + _eqs[i] = _eqs[i].lhs ~ subexpressions_not_involving_vars!( + _eqs[i].rhs, banned_vars, state) + end + + # map from symtype to cached variables and their expressions + cachevars = Dict{Union{DataType, UnionAll}, Vector{Any}}() + cacheexprs = Dict{Union{DataType, UnionAll}, Vector{Any}}() + # observed of previous SCCs are in the cache + # NOTE: When we get proper CSE, we can substitute these + # and then use `subexpressions_not_involving_vars!` + for i in prevobsidxs + T = symtype(obs[i].lhs) + buf = get!(() -> Any[], cachevars, T) + push!(buf, obs[i].lhs) + + buf = get!(() -> Any[], cacheexprs, T) + push!(buf, obs[i].lhs) + end + + for (k, v) in state + k = unwrap(k) + v = unwrap(v) + T = symtype(k) + buf = get!(() -> Any[], cachevars, T) + push!(buf, v) + buf = get!(() -> Any[], cacheexprs, T) + push!(buf, k) + end + + # update the sizes of cache buffers + for (T, buf) in cachevars + idx = findfirst(isequal(T), cachetypes) + if idx === nothing + push!(cachetypes, T) + push!(cachesizes, 0) + idx = lastindex(cachetypes) + end + cachesizes[idx] = max(cachesizes[idx], length(buf)) + end + + push!(scc_cachevars, cachevars) + push!(scc_cacheexprs, cacheexprs) + push!(scc_eqs, _eqs) + push!(scc_obs, _obs) + blockpush!(prevobsidxs, obsidxs) + end + + for (i, (escc, vscc)) in enumerate(zip(eq_sccs, var_sccs)) + _dvs = dvs[vscc] + _eqs = scc_eqs[i] + _prevobsidxs = reduce(vcat, blocks(prevobsidxs)[1:(i - 1)]; init = Int[]) + _obs = scc_obs[i] + cachevars = scc_cachevars[i] + cacheexprs = scc_cacheexprs[i] + available_vars = [dvs[reduce(vcat, var_sccs[1:(i - 1)]; init = Int[])]; + getproperty.( + reduce(vcat, scc_obs[1:(i - 1)]; init = []), (:lhs,))] + _prevobsidxs = vcat(_prevobsidxs, + observed_equations_used_by( + sys, reduce(vcat, values(cacheexprs); init = []); available_vars)) + if isempty(cachevars) + push!(explicitfuns, Returns(nothing)) + else + solsyms = getindex.((dvs,), view(var_sccs, 1:(i - 1))) + push!(explicitfuns, + CacheWriter(sys, cachetypes, cacheexprs, solsyms, obs[_prevobsidxs]; + eval_expression, eval_module, cse)) + end + + cachebufsyms = Tuple(map(cachetypes) do T + get(cachevars, T, []) + end) + f = SCCNonlinearFunction{iip}( + sys, _eqs, _dvs, _obs, cachebufsyms; eval_expression, eval_module, cse, kwargs...) + push!(nlfuns, f) + end + + if !isempty(cachetypes) + templates = map(cachetypes, cachesizes) do T, n + # Real refers to `eltype(u0)` + if T == Real + T = eltype(u0) + elseif T <: Array && eltype(T) == Real + T = Array{eltype(u0), ndims(T)} + end + BufferTemplate(T, n) + end + p = rebuild_with_caches(p, templates...) + end + + subprobs = [] + for (f, vscc) in zip(nlfuns, var_sccs) + _u0 = SymbolicUtils.Code.create_array( + typeof(u0), eltype(u0), Val(1), Val(length(vscc)), u0[vscc]...) + prob = NonlinearProblem(f, _u0, p) + push!(subprobs, prob) + end + + new_dvs = dvs[reduce(vcat, var_sccs)] + new_eqs = eqs[reduce(vcat, eq_sccs)] + @set! sys.unknowns = new_dvs + @set! sys.eqs = new_eqs + @set! sys.index_cache = subset_unknowns_observed( + get_index_cache(sys), sys, new_dvs, getproperty.(obs, (:lhs,))) + return SCCNonlinearProblem(subprobs, explicitfuns, p, true; sys) +end From 6a5d841bb8acf04f08fc62ae00e48aefef745966 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 14 May 2025 11:52:22 +0530 Subject: [PATCH 1669/2176] feat: implement `BVProblem` for `System` docs: add docstring for new `BVProblem` constructor refactor: improve `BVProblem` validation --- src/ModelingToolkit.jl | 1 + src/problems/bvproblem.jl | 93 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 94 insertions(+) create mode 100644 src/problems/bvproblem.jl diff --git a/src/ModelingToolkit.jl b/src/ModelingToolkit.jl index 2c8a86bbb2..92e139b3e7 100644 --- a/src/ModelingToolkit.jl +++ b/src/ModelingToolkit.jl @@ -181,6 +181,7 @@ include("problems/optimizationproblem.jl") include("problems/jumpproblem.jl") include("problems/initializationproblem.jl") include("problems/sccnonlinearproblem.jl") +include("problems/bvproblem.jl") include("systems/optimization/modelingtoolkitize.jl") include("systems/nonlinear/homotopy_continuation.jl") diff --git a/src/problems/bvproblem.jl b/src/problems/bvproblem.jl new file mode 100644 index 0000000000..b0ad28a842 --- /dev/null +++ b/src/problems/bvproblem.jl @@ -0,0 +1,93 @@ +""" +```julia +SciMLBase.BVProblem{iip}(sys::AbstractSystem, u0map, tspan, + parammap = DiffEqBase.NullParameters(); + constraints = nothing, guesses = nothing, + version = nothing, tgrad = false, + jac = true, sparse = true, + simplify = false, + kwargs...) where {iip} +``` + +Create a boundary value problem from the [`System`](@ref). + +`u0map` is used to specify fixed initial values for the states. Every variable +must have either an initial guess supplied using `guesses` or a fixed initial +value specified using `u0map`. + +Boundary value conditions are supplied to Systems in the form of a list of constraints. +These equations should specify values that state variables should take at specific points, +as in `x(0.5) ~ 1`). More general constraints that should hold over the entire solution, +such as `x(t)^2 + y(t)^2`, should be specified as one of the equations used to build the +`System`. + +If a `System` without `constraints` is specified, it will be treated as an initial value problem. + +```julia + @parameters g t_c = 0.5 + @variables x(..) y(t) λ(t) + eqs = [D(D(x(t))) ~ λ * x(t) + D(D(y)) ~ λ * y - g + x(t)^2 + y^2 ~ 1] + cstr = [x(0.5) ~ 1] + @mtkbuild pend = System(eqs, t; constraints = cstrs) + + tspan = (0.0, 1.5) + u0map = [x(t) => 0.6, y => 0.8] + parammap = [g => 1] + guesses = [λ => 1] + + bvp = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}(pend, u0map, tspan, parammap; guesses, check_length = false) +``` + +If the `System` has algebraic equations, like `x(t)^2 + y(t)^2`, the resulting +`BVProblem` must be solved using BVDAE solvers, such as Ascher. +""" +@fallback_iip_specialize function SciMLBase.BVProblem{iip, spec}( + sys::System, u0map, tspan, parammap = SciMLBase.NullParameters(); + check_compatibility = true, cse = true, + checkbounds = false, eval_expression = false, eval_module = @__MODULE__, + expression = Val{false}, guesses = Dict(), callback = nothing, + kwargs...) where {iip, spec} + check_complete(sys, BVProblem) + check_compatibility && check_compatible_system(BVProblem, sys) + isnothing(callback) || error("BVP solvers do not support callbacks.") + + # Systems without algebraic equations should use both fixed values + guesses + # for initialization. + _u0map = has_alg_eqs(sys) ? u0map : merge(Dict(u0map), Dict(guesses)) + + fode, u0, p = process_SciMLProblem( + ODEFunction{iip, spec}, sys, _u0map, parammap; guesses, + t = tspan !== nothing ? tspan[1] : tspan, check_compatibility = false, cse, + checkbounds, time_dependent_init = false, expression, kwargs...) + + dvs = unknowns(sys) + stidxmap = Dict([v => i for (i, v) in enumerate(dvs)]) + u0_idxs = has_alg_eqs(sys) ? collect(1:length(dvs)) : [stidxmap[k] for (k, v) in u0map] + fbc = generate_boundary_conditions( + sys, u0, u0_idxs, tspan[1]; expression = Val{false}, + wrap_gfw = Val{true}, cse, checkbounds) + + if (length(constraints(sys)) + length(u0map) > length(dvs)) + @warn "The BVProblem is overdetermined. The total number of conditions (# constraints + # fixed initial values given by u0map) exceeds the total number of states. The BVP solvers will default to doing a nonlinear least-squares optimization." + end + + kwargs = process_kwargs(sys; expression, kwargs...) + args = (; fode, fbc, u0, tspan, p) + + return maybe_codegen_scimlproblem(expression, BVProblem{iip}, args; kwargs...) +end + +function check_compatible_system(T::Type{BVProblem}, sys::System) + check_time_dependent(sys, T) + check_not_dde(sys) + check_no_cost(sys, T) + check_no_jumps(sys, T) + check_no_noise(sys, T) + check_is_continuous(sys, T) + + if !isempty(discrete_events(sys)) || !isempty(continuous_events(sys)) + throw(SystemCompatibilityError("BVP solvers do not support events.")) + end +end From 4e52917b39e524638c5fda59ceb7dfccfcb6ebdf Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 21 Apr 2025 12:41:46 +0530 Subject: [PATCH 1670/2176] feat: implement `SteadyStateProblem` for `System` --- src/problems/odeproblem.jl | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/src/problems/odeproblem.jl b/src/problems/odeproblem.jl index 0d5a146c48..99cff129e0 100644 --- a/src/problems/odeproblem.jl +++ b/src/problems/odeproblem.jl @@ -83,8 +83,39 @@ end maybe_codegen_scimlproblem(expression, ODEProblem{iip}, args; kwargs...) end +""" +```julia +SciMLBase.SteadyStateProblem(sys::System, u0map, + parammap = DiffEqBase.NullParameters(); + version = nothing, tgrad = false, + jac = false, + checkbounds = false, sparse = false, + linenumbers = true, parallel = SerialForm(), + kwargs...) where {iip} +``` + +Generates an SteadyStateProblem from a `System` of ODEs and allows for automatically +symbolically calculating numerical enhancements. +""" +@fallback_iip_specialize function DiffEqBase.SteadyStateProblem{iip, spec}( + sys::System, u0map, parammap; check_length = true, check_compatibility = true, + expression = Val{false}, kwargs...) where {iip, spec} + check_complete(sys, SteadyStateProblem) + check_compatibility && check_compatible_system(SteadyStateProblem, sys) + + f, u0, p = process_SciMLProblem(ODEFunction{iip}, sys, u0map, parammap; + steady_state = true, check_length, check_compatibility, expression, + force_initialization_time_independent = true, kwargs...) + + kwargs = process_kwargs(sys; expression, kwargs...) + args = (; f, u0, p) + + maybe_codegen_scimlproblem(expression, SteadyStateProblem{iip}, args; kwargs...) +end + function check_compatible_system( - T::Union{Type{ODEFunction}, Type{ODEProblem}, Type{DAEFunction}, Type{DAEProblem}}, + T::Union{Type{ODEFunction}, Type{ODEProblem}, Type{DAEFunction}, + Type{DAEProblem}, Type{SteadyStateProblem}}, sys::System) check_time_dependent(sys, T) check_not_dde(sys) From 37d7f6ad51c673c032af2e526a0deaba02d86d8f Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 21 Apr 2025 12:42:52 +0530 Subject: [PATCH 1671/2176] fix: fix `toexpr(::AbstractSystem)` --- src/systems/abstractsystem.jl | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index 2adc879e94..5d9a80d6d1 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -1990,8 +1990,8 @@ function push_eqs!(stmt, eqs, var2name) return eqs_name end -function push_defaults!(stmt, defs, var2name) - defs_name = gensym(:defs) +function push_defaults!(stmt, defs, var2name; name = :defs) + defs_name = gensym(name) defs_expr = Expr(:call, Dict) defs_blk = Expr(:(=), defs_name, defs_expr) for d in defs @@ -2039,23 +2039,23 @@ function toexpr(sys::AbstractSystem) eqs_name = push_eqs!(stmt, full_equations(sys), var2name) filtered_defs = filter( kvp -> !(iscall(kvp[1]) && operation(kvp[1]) isa Initial), defaults(sys)) + filtered_guesses = filter( + kvp -> !(iscall(kvp[1]) && operation(kvp[1]) isa Initial), guesses(sys)) defs_name = push_defaults!(stmt, filtered_defs, var2name) + guesses_name = push_defaults!(stmt, filtered_guesses, var2name; name = :guesses) obs_name = push_eqs!(stmt, obs, var2name) - if sys isa ODESystem - iv = get_iv(sys) + iv = get_iv(sys) + if iv === nothing + ivname = nothing + else ivname = gensym(:iv) push!(stmt, :($ivname = (@variables $(getname(iv)))[1])) - push!(stmt, - :($ODESystem($eqs_name, $ivname, $stsname, $psname; defaults = $defs_name, - observed = $obs_name, - name = $name, checks = false))) - elseif sys isa NonlinearSystem - push!(stmt, - :($NonlinearSystem($eqs_name, $stsname, $psname; defaults = $defs_name, - observed = $obs_name, - name = $name, checks = false))) end + push!(stmt, + :($System($eqs_name, $ivname, $stsname, $psname; defaults = $defs_name, + guesses = $guesses_name, observed = $obs_name, + name = $name, checks = false))) expr = :(let $expr From 60d73b984941e1c4d2a61f5cd56140ac753bbf05 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 21 Apr 2025 12:43:12 +0530 Subject: [PATCH 1672/2176] fix: fix `extend(::AbstractSystem)` --- src/systems/abstractsystem.jl | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index 5d9a80d6d1..f013c786b5 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -2693,11 +2693,9 @@ function extend(sys::AbstractSystem, basesys::AbstractSystem; name = name, description = description, gui_metadata = gui_metadata) # collect fields specific to some system types - if basesys isa ODESystem - ieqs = union(get_initialization_eqs(basesys), get_initialization_eqs(sys)) - guesses = merge(get_guesses(basesys), get_guesses(sys)) # prefer `sys` - kwargs = merge(kwargs, (initialization_eqs = ieqs, guesses = guesses)) - end + ieqs = union(get_initialization_eqs(basesys), get_initialization_eqs(sys)) + guesses = merge(get_guesses(basesys), get_guesses(sys)) # prefer `sys` + kwargs = merge(kwargs, (initialization_eqs = ieqs, guesses = guesses)) if has_assertions(basesys) kwargs = merge( From 81e8c98a484c59e842da30a1a2c6e54ef9ad7695 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 21 Apr 2025 12:43:45 +0530 Subject: [PATCH 1673/2176] fix: fix `substitute(::AbstractSystem, _...)` --- src/systems/abstractsystem.jl | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index f013c786b5..a314e20449 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -2796,7 +2796,7 @@ function Symbolics.substitute(sys::AbstractSystem, rules::Union{Vector{<:Pair}, # post-walk to avoid infinite recursion @set! sys.systems = map(Base.Fix2(substitute, dict), systems) something(get(rules, nameof(sys), nothing), sys) - elseif sys isa ODESystem + elseif sys isa System rules = todict(map(r -> Symbolics.unwrap(r[1]) => Symbolics.unwrap(r[2]), collect(rules))) eqs = fast_substitute(get_eqs(sys), rules) @@ -2805,9 +2805,15 @@ function Symbolics.substitute(sys::AbstractSystem, rules::Union{Vector{<:Pair}, for (k, v) in get_defaults(sys)) guess = Dict(fast_substitute(k, rules) => fast_substitute(v, rules) for (k, v) in get_guesses(sys)) + noise_eqs = fast_substitute(get_noise_eqs(sys), rules) + costs = fast_substitute(get_costs(sys), rules) + observed = fast_substitute(get_observed(sys), rules) + initialization_eqs = fast_substitute(get_initialization_eqs(sys), rules) + cstrs = fast_substitute(get_constraints(sys), rules) subsys = map(s -> substitute(s, rules), get_systems(sys)) - ODESystem(eqs, get_iv(sys); name = nameof(sys), defaults = defs, - guesses = guess, parameter_dependencies = pdeps, systems = subsys) + System(eqs, get_iv(sys); name = nameof(sys), defaults = defs, + guesses = guess, parameter_dependencies = pdeps, systems = subsys, noise_eqs, + observed, initialization_eqs, constraints = cstrs) else error("substituting symbols is not supported for $(typeof(sys))") end From 3f5428ad7fe23263e6b77c05515408d55fb325a8 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 21 Apr 2025 12:44:12 +0530 Subject: [PATCH 1674/2176] fix: construct `System` in `@mtkmodel` --- src/systems/model_parsing.jl | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/systems/model_parsing.jl b/src/systems/model_parsing.jl index 2a852813a2..fdb26ba7ec 100644 --- a/src/systems/model_parsing.jl +++ b/src/systems/model_parsing.jl @@ -72,8 +72,7 @@ function _model_macro(mod, fullname::Union{Expr, Symbol}, expr, isconnector) push!(exprs.args, :(variables = [])) push!(exprs.args, :(parameters = [])) - # We build `System` by default; vectors can't be created for `System` as it is - # a function. + # We build `System` by default push!(exprs.args, :(systems = ModelingToolkit.AbstractSystem[])) push!(exprs.args, :(equations = Union{Equation, Vector{Equation}}[])) push!(exprs.args, :(defaults = Dict{Num, Union{Number, Symbol, Function}}())) From 8556dee7d8168845435d5212e53648028e9153cc Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 21 Apr 2025 12:44:31 +0530 Subject: [PATCH 1675/2176] docs: fix docstring of `process_SciMLProblem` --- src/systems/problem_utils.jl | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/systems/problem_utils.jl b/src/systems/problem_utils.jl index 867ee80ddb..df59dc201c 100644 --- a/src/systems/problem_utils.jl +++ b/src/systems/problem_utils.jl @@ -1173,12 +1173,11 @@ Initial values provided in terms of other variables will be symbolically evaluat type of the containers (if parameters are not in an `MTKParameters` object). `Dict`s will be turned into `Array`s. -If `sys isa ODESystem`, this will also build the initialization problem and related objects -and pass them to the SciMLFunction as keyword arguments. +This will also build the initialization problem and related objects and pass them to the +SciMLFunction as keyword arguments. Keyword arguments: -- `build_initializeprob`: If `false`, avoids building the initialization problem for an - `ODESystem`. +- `build_initializeprob`: If `false`, avoids building the initialization problem. - `t`: The initial time of the `ODEProblem`. If this is not provided, the initialization problem cannot be built. - `implicit_dae`: Also build a mapping of derivatives of states to values for implicit DAEs, From 1048557043711f5dd4f13d04ebff3ec27813243c Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 25 Apr 2025 19:10:09 +0530 Subject: [PATCH 1676/2176] refactor: allow real values in `costs` --- src/systems/system.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/systems/system.jl b/src/systems/system.jl index d89bdb0f8e..a638debfb4 100644 --- a/src/systems/system.jl +++ b/src/systems/system.jl @@ -19,7 +19,7 @@ struct System <: AbstractSystem noise_eqs::Union{Nothing, AbstractVector, AbstractMatrix} jumps::Vector{JumpType} constraints::Vector{Union{Equation, Inequality}} - costs::Vector{<:BasicSymbolic} + costs::Vector{<:Union{BasicSymbolic, Real}} consolidate::Any unknowns::Vector ps::Vector From fcc91421489e489b9166392b4019ae40ba7fc8ed Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 21 Apr 2025 12:54:14 +0530 Subject: [PATCH 1677/2176] refactor: remove old clock handling code, retain error messages --- src/systems/systemstructure.jl | 66 +++++++++++----------------------- 1 file changed, 20 insertions(+), 46 deletions(-) diff --git a/src/systems/systemstructure.jl b/src/systems/systemstructure.jl index 1b561f97b4..c6f023e0e1 100644 --- a/src/systems/systemstructure.jl +++ b/src/systems/systemstructure.jl @@ -662,54 +662,28 @@ function structural_simplify!(state::TearingState; simplify = false, inputs = Any[], outputs = Any[], disturbance_inputs = Any[], kwargs...) - if state.sys isa ODESystem - ci = ModelingToolkit.ClockInference(state) - ci = ModelingToolkit.infer_clocks!(ci) - time_domains = merge(Dict(state.fullvars .=> ci.var_domain), - Dict(default_toterm.(state.fullvars) .=> ci.var_domain)) - tss, clocked_inputs, continuous_id, id_to_clock = ModelingToolkit.split_system(ci) - cont_inputs = [inputs; clocked_inputs[continuous_id]] - sys = _structural_simplify!(tss[continuous_id]; simplify, - check_consistency, fully_determined, - inputs = cont_inputs, outputs, disturbance_inputs, - kwargs...) - if length(tss) > 1 - if continuous_id > 0 - throw(HybridSystemNotSupportedException("Hybrid continuous-discrete systems are currently not supported with the standard MTK compiler. This system requires JuliaSimCompiler.jl, see https://help.juliahub.com/juliasimcompiler/stable/")) - end - # TODO: rename it to something else - discrete_subsystems = Vector{ODESystem}(undef, length(tss)) - # Note that the appended_parameters must agree with - # `generate_discrete_affect`! - appended_parameters = parameters(sys) - for (i, state) in enumerate(tss) - if i == continuous_id - discrete_subsystems[i] = sys - continue - end - disc_inputs = [inputs; clocked_inputs[i]] - ss, = _structural_simplify!(state; simplify, check_consistency, - inputs = disc_inputs, outputs, disturbance_inputs, - fully_determined, kwargs...) - append!(appended_parameters, inputs[i], unknowns(ss)) - discrete_subsystems[i] = ss - end - @set! sys.discrete_subsystems = discrete_subsystems, inputs, continuous_id, - id_to_clock - @set! sys.ps = appended_parameters - @set! sys.defaults = merge(ModelingToolkit.defaults(sys), - Dict(v => 0.0 for v in Iterators.flatten(inputs))) + ci = ModelingToolkit.ClockInference(state) + ci = ModelingToolkit.infer_clocks!(ci) + time_domains = merge(Dict(state.fullvars .=> ci.var_domain), + Dict(default_toterm.(state.fullvars) .=> ci.var_domain)) + tss, clocked_inputs, continuous_id, id_to_clock = ModelingToolkit.split_system(ci) + if length(tss) > 1 + if continuous_id == 0 + throw(HybridSystemNotSupportedException(""" + Discrete systems with multiple clocks are not supported with the standard \ + MTK compiler. + """)) + else + throw(HybridSystemNotSupportedException(""" + Hybrid continuous-discrete systems are currently not supported with \ + the standard MTK compiler. This system requires JuliaSimCompiler.jl, \ + see https://help.juliahub.com/juliasimcompiler/stable/ + """)) end - ps = [sym isa CallWithMetadata ? sym : - setmetadata( - sym, VariableTimeDomain, get(time_domains, sym, ContinuousClock())) - for sym in get_ps(sys)] - @set! sys.ps = ps - else - sys = _structural_simplify!(state; simplify, check_consistency, - inputs, outputs, disturbance_inputs, - fully_determined, kwargs...) end + sys = _structural_simplify!(state; simplify, check_consistency, + inputs, outputs, disturbance_inputs, + fully_determined, kwargs...) return sys end From 1b1ea3d59a2494818c029975b80c99b841e65006 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 21 Apr 2025 12:54:47 +0530 Subject: [PATCH 1678/2176] fix: allow simplifying systems with noise --- src/systems/systems.jl | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/systems/systems.jl b/src/systems/systems.jl index 399b263178..f6992a2d03 100644 --- a/src/systems/systems.jl +++ b/src/systems/systems.jl @@ -67,6 +67,11 @@ function __structural_simplify(sys::AbstractSystem; simplify = false, disturbance_inputs = Any[], sort_eqs = true, kwargs...) + # TODO: convert noise_eqs to brownians for simplification + if has_noise_eqs(sys) && get_noise_eqs(sys) !== nothing + sys = noise_to_brownians(sys; names = :αₘₜₖ) + end + sys = expand_connections(sys) state = TearingState(sys; sort_eqs) From 239664678683f6a3877e3dacb8977a99647bd58f Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 21 Apr 2025 12:57:44 +0530 Subject: [PATCH 1679/2176] refactor: remove `schedule(sys)` --- src/systems/abstractsystem.jl | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index a314e20449..ebd314f82a 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -604,17 +604,6 @@ end """ $(TYPEDSIGNATURES) -Mark a system as scheduled. It is only intended in compiler internals. A system -is scheduled after tearing based simplifications where equations are converted -into assignments. -""" -function schedule(sys::AbstractSystem) - has_schedule(sys) ? sys : (@set! sys.isscheduled = true) -end - -""" -$(TYPEDSIGNATURES) - If a system is scheduled, then changing its equations, variables, and parameters is no longer legal. """ From 001f2b7fc4564431d8ed805bc7871d1cf14a9edf Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 21 Apr 2025 12:58:17 +0530 Subject: [PATCH 1680/2176] feat: set system scheduling information in `structural_simplify` --- src/structural_transformation/symbolics_tearing.jl | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/structural_transformation/symbolics_tearing.jl b/src/structural_transformation/symbolics_tearing.jl index d41cbf12d2..ac37c918dd 100644 --- a/src/structural_transformation/symbolics_tearing.jl +++ b/src/structural_transformation/symbolics_tearing.jl @@ -756,11 +756,13 @@ function update_simplified_system!( @set! sys.substitutions = Substitutions(subeqs, deps) # Only makes sense for time-dependent - # TODO: generalize to SDE - if sys isa ODESystem + if ModelingToolkit.has_schedule(sys) @set! sys.schedule = Schedule(var_eq_matching, dummy_sub) end - sys = schedule(sys) + if ModelingToolkit.has_isscheduled(sys) + @set! sys.isscheduled = true + end + return sys end """ From 260d2b19a616c83ba714d9adb1ba91734b89da56 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 21 Apr 2025 13:01:03 +0530 Subject: [PATCH 1681/2176] refactor: remove references to `ODESystem` in source code --- ext/MTKBifurcationKitExt.jl | 10 +++--- ext/MTKFMIExt.jl | 2 +- ext/MTKInfiniteOptExt.jl | 14 ++++---- src/ModelingToolkit.jl | 2 +- src/inputoutput.jl | 12 +++---- src/linearization.jl | 10 +++--- .../StructuralTransformations.jl | 2 +- src/structural_transformation/pantelides.jl | 6 ++-- .../symbolics_tearing.jl | 4 +-- src/systems/abstractsystem.jl | 36 +++++++++---------- src/systems/analysis_points.jl | 6 ++-- src/systems/diffeqs/basic_transformations.jl | 16 ++++----- src/systems/diffeqs/first_order_transform.jl | 6 ++-- src/systems/diffeqs/modelingtoolkitize.jl | 4 +-- src/systems/if_lifting.jl | 5 ++- src/systems/optimal_control_interface.jl | 8 ++--- src/systems/systems.jl | 6 +--- src/systems/systemstructure.jl | 2 +- src/utils.jl | 5 +-- 19 files changed, 78 insertions(+), 78 deletions(-) diff --git a/ext/MTKBifurcationKitExt.jl b/ext/MTKBifurcationKitExt.jl index 0b9f104d9b..bcb3152702 100644 --- a/ext/MTKBifurcationKitExt.jl +++ b/ext/MTKBifurcationKitExt.jl @@ -23,7 +23,7 @@ struct ObservableRecordFromSolution{S, T} # A Vector of pairs (Symbolic => value) with the default values of all system variables and parameters. subs_vals::T - function ObservableRecordFromSolution(nsys::NonlinearSystem, + function ObservableRecordFromSolution(nsys::System, plot_var, bif_idx, u0_vals, @@ -82,7 +82,7 @@ end ### Creates BifurcationProblem Overloads ### # When input is a NonlinearSystem. -function BifurcationKit.BifurcationProblem(nsys::NonlinearSystem, +function BifurcationKit.BifurcationProblem(nsys::System, u0_bif, ps, bif_par, @@ -92,7 +92,7 @@ function BifurcationKit.BifurcationProblem(nsys::NonlinearSystem, jac = true, kwargs...) if !ModelingToolkit.iscomplete(nsys) - error("A completed `NonlinearSystem` is required. Call `complete` or `structural_simplify` on the system before creating a `BifurcationProblem`") + error("A completed `System` is required. Call `complete` or `structural_simplify` on the system before creating a `BifurcationProblem`") end @set! nsys.index_cache = nothing # force usage of a parameter vector instead of `MTKParameters` # Creates F and J functions. @@ -144,11 +144,11 @@ function BifurcationKit.BifurcationProblem(nsys::NonlinearSystem, end # When input is a ODESystem. -function BifurcationKit.BifurcationProblem(osys::ODESystem, args...; kwargs...) +function BifurcationKit.BifurcationProblem(osys::System, args...; kwargs...) if !ModelingToolkit.iscomplete(osys) error("A completed `ODESystem` is required. Call `complete` or `structural_simplify` on the system before creating a `BifurcationProblem`") end - nsys = NonlinearSystem([0 ~ eq.rhs for eq in full_equations(osys)], + nsys = System([0 ~ eq.rhs for eq in full_equations(osys)], unknowns(osys), parameters(osys); observed = observed(osys), diff --git a/ext/MTKFMIExt.jl b/ext/MTKFMIExt.jl index d155544ce9..1c70a385a6 100644 --- a/ext/MTKFMIExt.jl +++ b/ext/MTKFMIExt.jl @@ -289,7 +289,7 @@ function MTK.FMIComponent(::Val{Ver}; fmu = nothing, tolerance = 1e-6, end eqs = [observed; diffeqs] - return ODESystem(eqs, t, states, params; parameter_dependencies, defaults = defs, + return System(eqs, t, states, params; parameter_dependencies, defaults = defs, discrete_events = [instance_management_callback], name, initialization_eqs) end diff --git a/ext/MTKInfiniteOptExt.jl b/ext/MTKInfiniteOptExt.jl index e36b9c2a3e..dd7da24672 100644 --- a/ext/MTKInfiniteOptExt.jl +++ b/ext/MTKInfiniteOptExt.jl @@ -39,9 +39,9 @@ struct InfiniteOptDynamicOptProblem{uType, tType, isinplace, P, F, K} <: end """ - JuMPDynamicOptProblem(sys::ODESystem, u0, tspan, p; dt) + JuMPDynamicOptProblem(sys::System, u0, tspan, p; dt) -Convert an ODESystem representing an optimal control system into a JuMP model +Convert a System representing an optimal control system into a JuMP model for solving using optimization. Must provide either `dt`, the timestep between collocation points (which, along with the timespan, determines the number of points), or directly provide the number of points as `steps`. @@ -51,10 +51,10 @@ The optimization variables: - a vector-of-vectors V representing the controls as an interpolation array The constraints are: -- The set of user constraints passed to the ODESystem via `constraints` +- The set of user constraints passed to the System via `constraints` - The solver constraints that encode the time-stepping used by the solver """ -function MTK.JuMPDynamicOptProblem(sys::ODESystem, u0map, tspan, pmap; +function MTK.JuMPDynamicOptProblem(sys::System, u0map, tspan, pmap; dt = nothing, steps = nothing, guesses = Dict(), kwargs...) @@ -71,16 +71,16 @@ function MTK.JuMPDynamicOptProblem(sys::ODESystem, u0map, tspan, pmap; end """ - InfiniteOptDynamicOptProblem(sys::ODESystem, u0map, tspan, pmap; dt) + InfiniteOptDynamicOptProblem(sys::System, u0map, tspan, pmap; dt) -Convert an ODESystem representing an optimal control system into a InfiniteOpt model +Convert System representing an optimal control system into a InfiniteOpt model for solving using optimization. Must provide `dt` for determining the length of the interpolation arrays. Related to `JuMPDynamicOptProblem`, but directly adds the differential equations of the system as derivative constraints, rather than using a solver tableau. """ -function MTK.InfiniteOptDynamicOptProblem(sys::ODESystem, u0map, tspan, pmap; +function MTK.InfiniteOptDynamicOptProblem(sys::System, u0map, tspan, pmap; dt = nothing, steps = nothing, guesses = Dict(), kwargs...) diff --git a/src/ModelingToolkit.jl b/src/ModelingToolkit.jl index 92e139b3e7..6d567c0625 100644 --- a/src/ModelingToolkit.jl +++ b/src/ModelingToolkit.jl @@ -236,7 +236,7 @@ const D = Differential(t) PrecompileTools.@compile_workload begin using ModelingToolkit @variables x(ModelingToolkit.t_nounits) - @named sys = ODESystem([ModelingToolkit.D_nounits(x) ~ -x], ModelingToolkit.t_nounits) + @named sys = System([ModelingToolkit.D_nounits(x) ~ -x], ModelingToolkit.t_nounits) prob = ODEProblem(structural_simplify(sys), [x => 30.0], (0, 100), [], jac = true) @mtkmodel __testmod__ begin @constants begin diff --git a/src/inputoutput.jl b/src/inputoutput.jl index 283be6ed92..1beb229664 100644 --- a/src/inputoutput.jl +++ b/src/inputoutput.jl @@ -161,7 +161,7 @@ has_var(ex, x) = x ∈ Set(get_variables(ex)) """ (f_oop, f_ip), x_sym, p_sym, io_sys = generate_control_function( - sys::AbstractODESystem, + sys::System, inputs = unbound_inputs(sys), disturbance_inputs = disturbances(sys); implicit_dae = false, @@ -191,7 +191,7 @@ t = 0 f[1](x, inputs, p, t) ``` """ -function generate_control_function(sys::AbstractODESystem, inputs = unbound_inputs(sys), +function generate_control_function(sys::AbstractSystem, inputs = unbound_inputs(sys), disturbance_inputs = disturbances(sys); disturbance_argument = false, implicit_dae = false, @@ -333,7 +333,7 @@ The structure represents a model of a disturbance, along with the input variable # Fields: - `input`: The variable affected by the disturbance. - - `model::M`: A model of the disturbance. This is typically an `ODESystem`, but type that implements [`ModelingToolkit.get_disturbance_system`](@ref)`(dist::DisturbanceModel) -> ::ODESystem` is supported. + - `model::M`: A model of the disturbance. This is typically a `System`, but type that implements [`ModelingToolkit.get_disturbance_system`](@ref)`(dist::DisturbanceModel) -> ::System` is supported. """ struct DisturbanceModel{M} input::Any @@ -343,7 +343,7 @@ end DisturbanceModel(input, model; name) = DisturbanceModel(input, model, name) # Point of overloading for libraries, e.g., to be able to support disturbance models from ControlSystemsBase -function get_disturbance_system(dist::DisturbanceModel{<:ODESystem}) +function get_disturbance_system(dist::DisturbanceModel{System}) dist.model end @@ -384,7 +384,7 @@ c = 10 # Damping coefficient eqs = [connect(torque.flange, inertia1.flange_a) connect(inertia1.flange_b, spring.flange_a, damper.flange_a) connect(inertia2.flange_a, spring.flange_b, damper.flange_b)] -model = ODESystem(eqs, t; systems = [torque, inertia1, inertia2, spring, damper], +model = System(eqs, t; systems = [torque, inertia1, inertia2, spring, damper], name = :model) model = complete(model) model_outputs = [model.inertia1.w, model.inertia2.w, model.inertia1.phi, model.inertia2.phi] @@ -416,7 +416,7 @@ function add_input_disturbance(sys, dist::DisturbanceModel, inputs = Any[]; kwar eqs = [dsys.input.u[1] ~ d dist.input ~ u + dsys.output.u[1]] - augmented_sys = ODESystem(eqs, t, systems = [dsys], name = gensym(:outer)) + augmented_sys = System(eqs, t, systems = [dsys], name = gensym(:outer)) augmented_sys = extend(augmented_sys, sys) ssys = structural_simplify(augmented_sys, inputs = all_inputs, disturbance_inputs = [d]) diff --git a/src/linearization.jl b/src/linearization.jl index dd474ea1a0..8d6edf8f07 100644 --- a/src/linearization.jl +++ b/src/linearization.jl @@ -19,7 +19,7 @@ The `simplified_sys` has undergone [`structural_simplify`](@ref) and had any occ # Arguments: - - `sys`: An [`ODESystem`](@ref). This function will automatically apply simplification passes on `sys` and return the resulting `simplified_sys`. + - `sys`: A [`System`](@ref) of ODEs. This function will automatically apply simplification passes on `sys` and return the resulting `simplified_sys`. - `inputs`: A vector of variables that indicate the inputs of the linearized input-output model. - `outputs`: A vector of variables that indicate the outputs of the linearized input-output model. - `simplify`: Apply simplification in tearing. @@ -671,7 +671,7 @@ function plant(; name) @variables u(t)=0 y(t)=0 eqs = [D(x) ~ -x + u y ~ x] - ODESystem(eqs, t; name = name) + System(eqs, t; name = name) end function ref_filt(; name) @@ -679,7 +679,7 @@ function ref_filt(; name) @variables u(t)=0 [input = true] eqs = [D(x) ~ -2 * x + u y ~ x] - ODESystem(eqs, t, name = name) + System(eqs, t, name = name) end function controller(kp; name) @@ -688,7 +688,7 @@ function controller(kp; name) eqs = [ u ~ kp * (r - y), ] - ODESystem(eqs, t; name = name) + System(eqs, t; name = name) end @named f = ref_filt() @@ -699,7 +699,7 @@ connections = [f.y ~ c.r # filtered reference to controller reference c.u ~ p.u # controller output to plant input p.y ~ c.y] -@named cl = ODESystem(connections, t, systems = [f, c, p]) +@named cl = System(connections, t, systems = [f, c, p]) lsys0, ssys = linearize(cl, [f.u], [p.x]) desired_order = [f.x, p.x] diff --git a/src/structural_transformation/StructuralTransformations.jl b/src/structural_transformation/StructuralTransformations.jl index 4adc817ef8..16d3a75464 100644 --- a/src/structural_transformation/StructuralTransformations.jl +++ b/src/structural_transformation/StructuralTransformations.jl @@ -11,7 +11,7 @@ using SymbolicUtils.Rewriters using SymbolicUtils: maketerm, iscall using ModelingToolkit -using ModelingToolkit: ODESystem, AbstractSystem, var_from_nested_derivative, Differential, +using ModelingToolkit: System, AbstractSystem, var_from_nested_derivative, Differential, unknowns, equations, vars, Symbolic, diff2term_with_unit, shift2term_with_unit, value, operation, arguments, Sym, Term, simplify, symbolic_linear_solve, diff --git a/src/structural_transformation/pantelides.jl b/src/structural_transformation/pantelides.jl index 585c4a29d1..53c790863f 100644 --- a/src/structural_transformation/pantelides.jl +++ b/src/structural_transformation/pantelides.jl @@ -196,7 +196,7 @@ function pantelides!( eq′ = eq_to_diff[eq′] end # for _ in 1:maxiters pathfound || - error("maxiters=$maxiters reached! File a bug report if your system has a reasonable index (<100), and you are using the default `maxiters`. Try to increase the maxiters by `pantelides(sys::ODESystem; maxiters=1_000_000)` if your system has an incredibly high index and it is truly extremely large.") + error("maxiters=$maxiters reached! File a bug report if your system has a reasonable index (<100), and you are using the default `maxiters`. Try to increase the maxiters by `pantelides(sys::System; maxiters=1_000_000)` if your system has an incredibly high index and it is truly extremely large.") end # for k in 1:neqs′ finalize && for var in 1:ndsts(graph) @@ -207,13 +207,13 @@ function pantelides!( end """ - dae_index_lowering(sys::ODESystem; kwargs...) -> ODESystem + dae_index_lowering(sys::System; kwargs...) -> System Perform the Pantelides algorithm to transform a higher index DAE to an index 1 DAE. `kwargs` are forwarded to [`pantelides!`](@ref). End users are encouraged to call [`structural_simplify`](@ref) instead, which calls this function internally. """ -function dae_index_lowering(sys::ODESystem; kwargs...) +function dae_index_lowering(sys::System; kwargs...) state = TearingState(sys) var_eq_matching = pantelides!(state; finalize = false, kwargs...) return invalidate_cache!(pantelides_reassemble(state, var_eq_matching)) diff --git a/src/structural_transformation/symbolics_tearing.jl b/src/structural_transformation/symbolics_tearing.jl index ac37c918dd..60d24e69ce 100644 --- a/src/structural_transformation/symbolics_tearing.jl +++ b/src/structural_transformation/symbolics_tearing.jl @@ -40,7 +40,7 @@ function var_derivative_graph!(s::SystemStructure, v::Int) return var_diff end -function var_derivative!(ts::TearingState{ODESystem}, v::Int) +function var_derivative!(ts::TearingState, v::Int) s = ts.structure var_diff = var_derivative_graph!(s, v) sys = ts.sys @@ -58,7 +58,7 @@ function eq_derivative_graph!(s::SystemStructure, eq::Int) return eq_diff end -function eq_derivative!(ts::TearingState{ODESystem}, ieq::Int; kwargs...) +function eq_derivative!(ts::TearingState, ieq::Int; kwargs...) s = ts.structure eq_diff = eq_derivative_graph!(s, ieq) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index ebd314f82a..f28b0f1559 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -1673,7 +1673,7 @@ function defaults(sys::AbstractSystem) # `mapfoldr` is really important!!! We should prefer the base model for # defaults, because people write: # - # `compose(ODESystem(...; defaults=defs), ...)` + # `compose(System(...; defaults=defs), ...)` # # Thus, right associativity is required and crucial for correctness. isempty(systems) ? defs : mapfoldr(namespace_defaults, merge, systems; init = defs) @@ -2869,7 +2869,7 @@ using ModelingToolkit: t, D @parameters p = 1.0, [description = "My parameter", tunable = false] q = 2.0, [description = "Other parameter"] @variables x(t) = 3.0 [unit = u"m"] -@named sys = ODESystem(Equation[], t, [x], [p, q]) +@named sys = System(Equation[], t, [x], [p, q]) ModelingToolkit.dump_parameters(sys) ``` @@ -2910,7 +2910,7 @@ using ModelingToolkit: t, D @parameters p = 1.0, [description = "My parameter", tunable = false] q = 2.0, [description = "Other parameter"] @variables x(t) = 3.0 [unit = u"m"] -@named sys = ODESystem(Equation[], t, [x], [p, q]) +@named sys = System(Equation[], t, [x], [p, q]) ModelingToolkit.dump_unknowns(sys) ``` @@ -3088,7 +3088,7 @@ using ModelingToolkit: t_nounits as t, D_nounits as D @variables X(t) eq1 = D(X) ~ p - d*X eq2 = 0 ~ p - d*X -@named osys = ODESystem([eq1, eq2], t) +@named osys = System([eq1, eq2], t) alg_equations(osys) # returns `[0 ~ p - d*X(t)]`. """ @@ -3107,7 +3107,7 @@ using ModelingToolkit: t_nounits as t, D_nounits as D @variables X(t) eq1 = D(X) ~ p - d*X eq2 = 0 ~ p - d*X -@named osys = ODESystem([eq1, eq2], t) +@named osys = System([eq1, eq2], t) diff_equations(osys) # returns `[Differential(t)(X(t)) ~ p - d*X(t)]`. """ @@ -3127,8 +3127,8 @@ using ModelingToolkit: t_nounits as t, D_nounits as D @variables X(t) eq1 = D(X) ~ p - d*X eq2 = 0 ~ p - d*X -@named osys1 = ODESystem([eq1], t) -@named osys2 = ODESystem([eq2], t) +@named osys1 = System([eq1], t) +@named osys2 = System([eq2], t) has_alg_equations(osys1) # returns `false`. has_alg_equations(osys2) # returns `true`. @@ -3149,8 +3149,8 @@ using ModelingToolkit: t_nounits as t, D_nounits as D @variables X(t) eq1 = D(X) ~ p - d*X eq2 = 0 ~ p - d*X -@named osys1 = ODESystem([eq1], t) -@named osys2 = ODESystem([eq2], t) +@named osys1 = System([eq1], t) +@named osys2 = System([eq2], t) has_diff_equations(osys1) # returns `true`. has_diff_equations(osys2) # returns `false`. @@ -3172,9 +3172,9 @@ using ModelingToolkit: t_nounits as t, D_nounits as D @variables X(t) eq1 = D(X) ~ p - d*X eq2 = 0 ~ p - d*X -@named osys1 = ODESystem([eq1], t) -@named osys2 = ODESystem([eq2], t) -osys12 = compose(osys1, [osys2]) +@named osys1 = ([eq1], t) +@named osys2 = ([eq2], t) +osys12 = compose(sys1, [osys2]) osys21 = compose(osys2, [osys1]) get_alg_eqs(osys12) # returns `Equation[]`. @@ -3197,8 +3197,8 @@ using ModelingToolkit: t_nounits as t, D_nounits as D @variables X(t) eq1 = D(X) ~ p - d*X eq2 = 0 ~ p - d*X -@named osys1 = ODESystem([eq1], t) -@named osys2 = ODESystem([eq2], t) +@named osys1 = tem([eq1], t) +@named osys2 = tem([eq2], t) osys12 = compose(osys1, [osys2]) osys21 = compose(osys2, [osys1]) @@ -3222,8 +3222,8 @@ using ModelingToolkit: t_nounits as t, D_nounits as D @variables X(t) eq1 = D(X) ~ p - d*X eq2 = 0 ~ p - d*X -@named osys1 = ODESystem([eq1], t) -@named osys2 = ODESystem([eq2], t) +@named osys1 = System([eq1], t) +@named osys2 = System([eq2], t) osys12 = compose(osys1, [osys2]) osys21 = compose(osys2, [osys1]) @@ -3248,8 +3248,8 @@ using ModelingToolkit: t_nounits as t, D_nounits as D @variables X(t) eq1 = D(X) ~ p - d*X eq2 = 0 ~ p - d*X -@named osys1 = ODESystem([eq1], t) -@named osys2 = ODESystem([eq2], t) +@named osys1 = tem([eq1], t) +@named osys2 = tem([eq2], t) osys12 = compose(osys1, [osys2]) osys21 = compose(osys2, [osys1]) diff --git a/src/systems/analysis_points.jl b/src/systems/analysis_points.jl index 0d1a2830cf..27a0204cb8 100644 --- a/src/systems/analysis_points.jl +++ b/src/systems/analysis_points.jl @@ -31,7 +31,7 @@ t = ModelingToolkit.get_iv(P) eqs = [connect(P.output, C.input) connect(C.output, :plant_input, P.input)] -sys = ODESystem(eqs, t, systems = [P, C], name = :feedback_system) +sys = System(eqs, t, systems = [P, C], name = :feedback_system) matrices_S, _ = get_sensitivity(sys, :plant_input) # Compute the matrices of a state-space representation of the (input) sensitivity function. matrices_T, _ = get_comp_sensitivity(sys, :plant_input) @@ -1007,12 +1007,12 @@ See also [`get_sensitivity`](@ref), [`get_comp_sensitivity`](@ref), [`open_loop` # """ - generate_control_function(sys::ModelingToolkit.AbstractODESystem, input_ap_name::Union{Symbol, Vector{Symbol}, AnalysisPoint, Vector{AnalysisPoint}}, dist_ap_name::Union{Symbol, Vector{Symbol}, AnalysisPoint, Vector{AnalysisPoint}}; system_modifier = identity, kwargs) + generate_control_function(sys::ModelingToolkit.AbstractSystem, input_ap_name::Union{Symbol, Vector{Symbol}, AnalysisPoint, Vector{AnalysisPoint}}, dist_ap_name::Union{Symbol, Vector{Symbol}, AnalysisPoint, Vector{AnalysisPoint}}; system_modifier = identity, kwargs) When called with analysis points as input arguments, we assume that all analysis points corresponds to connections that should be opened (broken). The use case for this is to get rid of input signal blocks, such as `Step` or `Sine`, since these are useful for simulation but are not needed when using the plant model in a controller or state estimator. """ function generate_control_function( - sys::ModelingToolkit.AbstractODESystem, input_ap_name::Union{ + sys::ModelingToolkit.AbstractSystem, input_ap_name::Union{ Symbol, Vector{Symbol}, AnalysisPoint, Vector{AnalysisPoint}}, dist_ap_name::Union{ Nothing, Symbol, Vector{Symbol}, AnalysisPoint, Vector{AnalysisPoint}} = nothing; diff --git a/src/systems/diffeqs/basic_transformations.jl b/src/systems/diffeqs/basic_transformations.jl index 3c1081fcbe..9fa90681cd 100644 --- a/src/systems/diffeqs/basic_transformations.jl +++ b/src/systems/diffeqs/basic_transformations.jl @@ -21,7 +21,7 @@ using ModelingToolkit, OrdinaryDiffEq @variables x(t) y(t) D = Differential(t) eqs = [D(x) ~ α*x - β*x*y, D(y) ~ -δ*y + γ*x*y] -@named sys = ODESystem(eqs, t) +@named sys = System(eqs, t) sys2 = liouville_transform(sys) sys2 = complete(sys2) @@ -40,14 +40,14 @@ Optimal Transport Approach Abhishek Halder, Kooktae Lee, and Raktim Bhattacharya https://abhishekhalder.bitbucket.io/F16ACC2013Final.pdf """ -function liouville_transform(sys::AbstractODESystem; kwargs...) +function liouville_transform(sys::System; kwargs...) t = get_iv(sys) @variables trJ - D = ModelingToolkit.Differential(t) + D = Differential(t) neweq = D(trJ) ~ trJ * -tr(calculate_jacobian(sys)) neweqs = [equations(sys); neweq] vars = [unknowns(sys); trJ] - ODESystem( + System( neweqs, t, vars, parameters(sys); checks = false, name = nameof(sys), kwargs... ) @@ -55,7 +55,7 @@ end """ change_independent_variable( - sys::AbstractODESystem, iv, eqs = []; + sys::System, iv, eqs = []; add_old_diff = false, simplify = true, fold = false ) @@ -95,7 +95,7 @@ By changing the independent variable, it can be reformulated for vertical positi ```julia julia> @variables x(t) y(t); -julia> @named M = ODESystem([D(D(y)) ~ -9.81, D(D(x)) ~ 0.0], t); +julia> @named M = System([D(D(y)) ~ -9.81, D(D(x)) ~ 0.0], t); julia> M = change_independent_variable(M, x); @@ -109,7 +109,7 @@ julia> unknowns(M) ``` """ function change_independent_variable( - sys::AbstractODESystem, iv, eqs = []; + sys::System, iv, eqs = []; add_old_diff = false, simplify = true, fold = false ) iv2_of_iv1 = unwrap(iv) # e.g. u(t) @@ -201,7 +201,7 @@ function change_independent_variable( end # Use the utility function to transform everything in the system! - function transform(sys::AbstractODESystem) + function transform(sys::System) systems = map(transform, get_systems(sys)) # recurse through subsystems # transform equations and connections systems_map = Dict(get_name(s) => s for s in systems) diff --git a/src/systems/diffeqs/first_order_transform.jl b/src/systems/diffeqs/first_order_transform.jl index 97fd6460d9..d017ea362b 100644 --- a/src/systems/diffeqs/first_order_transform.jl +++ b/src/systems/diffeqs/first_order_transform.jl @@ -1,10 +1,10 @@ """ $(TYPEDSIGNATURES) -Takes a Nth order ODESystem and returns a new ODESystem written in first order +Takes a Nth order System and returns a new System written in first order form by defining new variables which represent the N-1 derivatives. """ -function ode_order_lowering(sys::ODESystem) +function ode_order_lowering(sys::System) iv = get_iv(sys) eqs_lowered, new_vars = ode_order_lowering(equations(sys), iv, unknowns(sys)) @set! sys.eqs = eqs_lowered @@ -12,7 +12,7 @@ function ode_order_lowering(sys::ODESystem) return sys end -function dae_order_lowering(sys::ODESystem) +function dae_order_lowering(sys::System) iv = get_iv(sys) eqs_lowered, new_vars = dae_order_lowering(equations(sys), iv, unknowns(sys)) @set! sys.eqs = eqs_lowered diff --git a/src/systems/diffeqs/modelingtoolkitize.jl b/src/systems/diffeqs/modelingtoolkitize.jl index b2954f81e4..ab13ecca29 100644 --- a/src/systems/diffeqs/modelingtoolkitize.jl +++ b/src/systems/diffeqs/modelingtoolkitize.jl @@ -1,7 +1,7 @@ """ $(TYPEDSIGNATURES) -Generate `ODESystem`, dependent variables, and parameters from an `ODEProblem`. +Generate `System`, dependent variables, and parameters from an `ODEProblem`. """ function modelingtoolkitize( prob::DiffEqBase.ODEProblem; u_names = nothing, p_names = nothing, kwargs...) @@ -95,7 +95,7 @@ function modelingtoolkitize( end filter!(x -> !iscall(x) || !(operation(x) isa Initial), params) filter!(x -> !iscall(x[1]) || !(operation(x[1]) isa Initial), default_p) - de = ODESystem(eqs, t, sts, params, + de = System(eqs, t, sts, params, defaults = merge(default_u0, default_p); name = gensym(:MTKizedODE), tspan = prob.tspan, diff --git a/src/systems/if_lifting.jl b/src/systems/if_lifting.jl index aeb8afc0a8..130a4bdab4 100644 --- a/src/systems/if_lifting.jl +++ b/src/systems/if_lifting.jl @@ -411,7 +411,10 @@ Lifting proceeds through the following process: * rewrite comparisons to be of the form eqn [op] 0; subtract the RHS from the LHS * replace comparisons with generated parameters; for each comparison eqn [op] 0, generate an event (dependent on op) that sets the parameter """ -function IfLifting(sys::ODESystem) +function IfLifting(sys::System) + if !is_time_dependent(sys) + throw(ArgumentError("IfLifting is only supported for time-dependent systems.")) + end cw = CondRewriter(get_iv(sys)) eqs = copy(equations(sys)) diff --git a/src/systems/optimal_control_interface.jl b/src/systems/optimal_control_interface.jl index fa1157dca8..2ee2d0e9ca 100644 --- a/src/systems/optimal_control_interface.jl +++ b/src/systems/optimal_control_interface.jl @@ -51,7 +51,7 @@ is_explicit(tableau) = tableau isa DiffEqBase.ExplicitRKTableau Generate the control function f(x, u, p, t) from the ODESystem. Input variables are automatically inferred but can be manually specified. """ -function SciMLBase.ODEInputFunction{iip, specialize}(sys::ODESystem, +function SciMLBase.ODEInputFunction{iip, specialize}(sys::System, dvs = unknowns(sys), ps = parameters(sys), u0 = nothing, inputs = unbound_inputs(sys), @@ -147,16 +147,16 @@ function SciMLBase.ODEInputFunction{iip, specialize}(sys::ODESystem, initialization_data) end -function SciMLBase.ODEInputFunction(sys::AbstractODESystem, args...; kwargs...) +function SciMLBase.ODEInputFunction(sys::System, args...; kwargs...) ODEInputFunction{true}(sys, args...; kwargs...) end -function SciMLBase.ODEInputFunction{true}(sys::AbstractODESystem, args...; +function SciMLBase.ODEInputFunction{true}(sys::System, args...; kwargs...) ODEInputFunction{true, SciMLBase.AutoSpecialize}(sys, args...; kwargs...) end -function SciMLBase.ODEInputFunction{false}(sys::AbstractODESystem, args...; +function SciMLBase.ODEInputFunction{false}(sys::System, args...; kwargs...) ODEInputFunction{false, SciMLBase.FullSpecialize}(sys, args...; kwargs...) end diff --git a/src/systems/systems.jl b/src/systems/systems.jl index f6992a2d03..29584825fe 100644 --- a/src/systems/systems.jl +++ b/src/systems/systems.jl @@ -42,7 +42,7 @@ function structural_simplify( for pass in additional_passes newsys = pass(newsys) end - if newsys isa ODESystem || has_parent(newsys) + if has_parent(newsys) @set! newsys.parent = complete(sys; split = false, flatten = false) end newsys = complete(newsys; split) @@ -58,10 +58,6 @@ function __structural_simplify(sys::JumpSystem, args...; kwargs...) return sys end -function __structural_simplify(sys::SDESystem, args...; kwargs...) - return __structural_simplify(ODESystem(sys), args...; kwargs...) -end - function __structural_simplify(sys::AbstractSystem; simplify = false, inputs = Any[], outputs = Any[], disturbance_inputs = Any[], diff --git a/src/systems/systemstructure.jl b/src/systems/systemstructure.jl index c6f023e0e1..611ff72095 100644 --- a/src/systems/systemstructure.jl +++ b/src/systems/systemstructure.jl @@ -9,7 +9,7 @@ import ..ModelingToolkit: isdiffeq, var_from_nested_derivative, vars!, flatten, independent_variables, SparseMatrixCLIL, AbstractSystem, equations, isirreducible, input_timedomain, TimeDomain, InferredTimeDomain, - VariableType, getvariabletype, has_equations, ODESystem + VariableType, getvariabletype, has_equations, System using ..BipartiteGraphs import ..BipartiteGraphs: invview, complete using Graphs diff --git a/src/utils.jl b/src/utils.jl index be28fa3401..33856c71dc 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -1208,7 +1208,8 @@ end """ $(TYPEDSIGNATURES) -Find all the unknowns and parameters from the equations of a SDESystem or ODESystem. Return re-ordered equations, differential variables, all variables, and parameters. +Find all the unknowns and parameters from the equations of a System. Return re-ordered +equations, differential variables, all variables, and parameters. """ function process_equations(eqs, iv) if eltype(eqs) <: AbstractVector @@ -1244,7 +1245,7 @@ function process_equations(eqs, iv) diffvar, _ = var_from_nested_derivative(eq.lhs) if check_scope_depth(getmetadata(diffvar, SymScope, LocalScope()), 0) isequal(iv, iv_from_nested_derivative(eq.lhs)) || - throw(ArgumentError("An ODESystem can only have one independent variable.")) + throw(ArgumentError("A system of differential equations can only have one independent variable.")) diffvar in diffvars && throw(ArgumentError("The differential variable $diffvar is not unique in the system of equations.")) !has_diffvar_type(diffvar) && From 28999f316113d4a9962396ca060389e40af8fa71 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 21 Apr 2025 13:01:25 +0530 Subject: [PATCH 1682/2176] refactor: remove `__structural_simplify(::JumpSystem)` --- src/systems/systems.jl | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/systems/systems.jl b/src/systems/systems.jl index 29584825fe..4e70476f94 100644 --- a/src/systems/systems.jl +++ b/src/systems/systems.jl @@ -54,10 +54,6 @@ function structural_simplify( end end -function __structural_simplify(sys::JumpSystem, args...; kwargs...) - return sys -end - function __structural_simplify(sys::AbstractSystem; simplify = false, inputs = Any[], outputs = Any[], disturbance_inputs = Any[], From 330d0e76d6260c509122d45dde0a35df71a35a8c Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 21 Apr 2025 15:30:21 +0530 Subject: [PATCH 1683/2176] refactor: remove references to `SDESystem` in source code --- src/systems/diffeqs/basic_transformations.jl | 4 ++-- src/systems/diffeqs/modelingtoolkitize.jl | 4 ++-- src/systems/nonlinear/initializesystem.jl | 1 + src/systems/systems.jl | 4 ++-- src/utils.jl | 2 +- 5 files changed, 8 insertions(+), 7 deletions(-) diff --git a/src/systems/diffeqs/basic_transformations.jl b/src/systems/diffeqs/basic_transformations.jl index 9fa90681cd..21dcc7977a 100644 --- a/src/systems/diffeqs/basic_transformations.jl +++ b/src/systems/diffeqs/basic_transformations.jl @@ -297,7 +297,7 @@ Measure transformation method that allows for a reduction in the variance of an Input: Original SDE system and symbolic function `u(t,x)` with scalar output that defines the adjustable parameters `d` in the Girsanov transformation. Optional: initial condition for `θ0`. -Output: Modified SDESystem with additional component `θ_t` and initial value `θ0`, as well as +Output: Modified SDE System with additional component `θ_t` and initial value `θ0`, as well as the weight `θ_t/θ0` as observed equation, such that the estimator `Exp(g(X_t)θ_t/θ0)` has a smaller variance. @@ -317,7 +317,7 @@ using ModelingToolkit: t_nounits as t, D_nounits as D eqs = [D(x) ~ α*x] noiseeqs = [β*x] -@named de = SDESystem(eqs,noiseeqs,t,[x],[α,β]) +@named de = System(eqs,t,[x],[α,β]; noise_eqs = noiseeqs) # define u (user choice) u = x diff --git a/src/systems/diffeqs/modelingtoolkitize.jl b/src/systems/diffeqs/modelingtoolkitize.jl index ab13ecca29..cfa3ac43dd 100644 --- a/src/systems/diffeqs/modelingtoolkitize.jl +++ b/src/systems/diffeqs/modelingtoolkitize.jl @@ -226,7 +226,7 @@ end """ $(TYPEDSIGNATURES) -Generate `SDESystem`, dependent variables, and parameters from an `SDEProblem`. +Generate `System`, dependent variables, and parameters from an `SDEProblem`. """ function modelingtoolkitize(prob::DiffEqBase.SDEProblem; kwargs...) prob.f isa DiffEqBase.AbstractParameterizedFunction && @@ -293,7 +293,7 @@ function modelingtoolkitize(prob::DiffEqBase.SDEProblem; kwargs...) Dict() end - de = SDESystem(deqs, neqs, t, sts, params; + de = System(deqs, t, sts, params; noise_eqs = neqs, name = gensym(:MTKizedSDE), tspan = prob.tspan, defaults = merge(default_u0, default_p), diff --git a/src/systems/nonlinear/initializesystem.jl b/src/systems/nonlinear/initializesystem.jl index 0396074300..0c60ac2ca1 100644 --- a/src/systems/nonlinear/initializesystem.jl +++ b/src/systems/nonlinear/initializesystem.jl @@ -90,6 +90,7 @@ function generate_initializesystem_timevarying(sys::AbstractSystem; end end else + # TODO: Check if this is still necessary # 2) System doesn't have a schedule, so dummy derivatives don't exist/aren't handled (SDESystem) for (k, v) in u0map defs[k] = v diff --git a/src/systems/systems.jl b/src/systems/systems.jl index 4e70476f94..1aaa07eaea 100644 --- a/src/systems/systems.jl +++ b/src/systems/systems.jl @@ -145,8 +145,8 @@ function __structural_simplify(sys::AbstractSystem; simplify = false, end noise_eqs = StructuralTransformations.tearing_substitute_expr(ode_sys, noise_eqs) - ssys = SDESystem(Vector{Equation}(full_equations(ode_sys)), noise_eqs, - get_iv(ode_sys), unknowns(ode_sys), parameters(ode_sys); + ssys = System(Vector{Equation}(full_equations(ode_sys)), + get_iv(ode_sys), unknowns(ode_sys), parameters(ode_sys); noise_eqs, name = nameof(ode_sys), is_scalar_noise, observed = observed(ode_sys), defaults = defaults(sys), parameter_dependencies = parameter_dependencies(sys), assertions = assertions(sys), guesses = guesses(sys), initialization_eqs = initialization_equations(sys), diff --git a/src/utils.jl b/src/utils.jl index 33856c71dc..a12b251384 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -1314,7 +1314,7 @@ have different multiplicities in `a` and `b`. """ function _eq_unordered(a::AbstractArray, b::AbstractArray) # a and b may be multidimensional - # e.g. comparing noiseeqs of SDESystem + # e.g. comparing noiseeqs of SDEs a = vec(a) b = vec(b) length(a) === length(b) || return false From 35b0ae3b34decab3523bedf9d0db1e137245bd98 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 21 Apr 2025 15:35:37 +0530 Subject: [PATCH 1684/2176] refactor: remove references to `NonlinearSystem` --- src/problems/sccnonlinearproblem.jl | 8 ++--- src/systems/codegen.jl | 2 +- .../nonlinear/homotopy_continuation.jl | 30 +++++++++---------- src/systems/nonlinear/initializesystem.jl | 10 +++---- src/systems/nonlinear/modelingtoolkitize.jl | 4 +-- 5 files changed, 26 insertions(+), 28 deletions(-) diff --git a/src/problems/sccnonlinearproblem.jl b/src/problems/sccnonlinearproblem.jl index 0b4174f552..36330e339f 100644 --- a/src/problems/sccnonlinearproblem.jl +++ b/src/problems/sccnonlinearproblem.jl @@ -54,7 +54,7 @@ function SCCNonlinearFunction{iip}( f_oop, f_iip = eval_or_rgf.(f_gen; eval_expression, eval_module) f = GeneratedFunctionWrapper{(2, 2, is_split(sys))}(f_oop, f_iip) - subsys = NonlinearSystem(_eqs, _dvs, ps; observed = _obs, + subsys = System(_eqs, _dvs, ps; observed = _obs, parameter_dependencies = parameter_dependencies(sys), name = nameof(sys)) if get_index_cache(sys) !== nothing @set! subsys.index_cache = subset_unknowns_observed( @@ -65,15 +65,15 @@ function SCCNonlinearFunction{iip}( return NonlinearFunction{iip}(f; sys = subsys) end -function SciMLBase.SCCNonlinearProblem(sys::NonlinearSystem, args...; kwargs...) +function SciMLBase.SCCNonlinearProblem(sys::System, args...; kwargs...) SCCNonlinearProblem{true}(sys, args...; kwargs...) end -function SciMLBase.SCCNonlinearProblem{iip}(sys::NonlinearSystem, u0map, +function SciMLBase.SCCNonlinearProblem{iip}(sys::System, u0map, parammap = SciMLBase.NullParameters(); eval_expression = false, eval_module = @__MODULE__, cse = true, kwargs...) where {iip} if !iscomplete(sys) || get_tearing_state(sys) === nothing - error("A simplified `NonlinearSystem` is required. Call `structural_simplify` on the system before creating an `SCCNonlinearProblem`.") + error("A simplified `System` is required. Call `structural_simplify` on the system before creating an `SCCNonlinearProblem`.") end if !is_split(sys) diff --git a/src/systems/codegen.jl b/src/systems/codegen.jl index 60d3c4dd2d..1ca879322a 100644 --- a/src/systems/codegen.jl +++ b/src/systems/codegen.jl @@ -625,7 +625,7 @@ The signatures will be of the form `g(...)` with arguments: - `unknowns` if `param_only` is `false` - `inputs` if `inputs` is an array of symbolic inputs that should be available in `ts` - `p...` unconditionally; note that in the case of `MTKParameters` more than one parameters argument may be present, so it must be splatted -- `t` if the system is time-dependent; for example `NonlinearSystem` will not have `t` +- `t` if the system is time-dependent; for example systems of nonlinear equations will not have `t` For example, a function `g(op, unknowns, p..., inputs, t)` will be the in-place function generated if `return_inplace` is true, `ts` is a vector, an array of inputs `inputs` is given, and `param_only` is false for a time-dependent system. diff --git a/src/systems/nonlinear/homotopy_continuation.jl b/src/systems/nonlinear/homotopy_continuation.jl index 9a77779103..e68737c19b 100644 --- a/src/systems/nonlinear/homotopy_continuation.jl +++ b/src/systems/nonlinear/homotopy_continuation.jl @@ -211,7 +211,7 @@ end """ $(TYPEDEF) -Information representing how to transform a `NonlinearSystem` into a polynomial +Information representing how to transform a `System` into a polynomial system. """ struct PolynomialTransformation @@ -236,7 +236,7 @@ struct PolynomialTransformation polydata::Vector{PolynomialData} end -function PolynomialTransformation(sys::NonlinearSystem) +function PolynomialTransformation(sys::System) # we need to consider `full_equations` because observed also should be # polynomials (if used in equations) and we don't know if observed is used # in denominator. @@ -342,7 +342,7 @@ using the appropriate `PolynomialTransformation`. Also contains the denominators in the equations, to rule out invalid roots. """ struct PolynomialTransformationResult - sys::NonlinearSystem + sys::System denominators::Vector{BasicSymbolic} end @@ -353,7 +353,7 @@ Transform the system `sys` with `transformation` and return a `PolynomialTransformationResult`, or a `NotPolynomialError` if the system cannot be transformed. """ -function transform_system(sys::NonlinearSystem, transformation::PolynomialTransformation; +function transform_system(sys::System, transformation::PolynomialTransformation; fraction_cancel_fn = simplify_fractions) subrules = transformation.substitution_rules dvs = unknowns(sys) @@ -473,26 +473,26 @@ function handle_rational_polynomials(x, wrt; fraction_cancel_fn = simplify_fract return num, den end -function SciMLBase.HomotopyNonlinearFunction(sys::NonlinearSystem, args...; kwargs...) +function SciMLBase.HomotopyNonlinearFunction(sys::System, args...; kwargs...) ODEFunction{true}(sys, args...; kwargs...) end -function SciMLBase.HomotopyNonlinearFunction{true}(sys::NonlinearSystem, args...; +function SciMLBase.HomotopyNonlinearFunction{true}(sys::System, args...; kwargs...) ODEFunction{true, SciMLBase.AutoSpecialize}(sys, args...; kwargs...) end -function SciMLBase.HomotopyNonlinearFunction{false}(sys::NonlinearSystem, args...; +function SciMLBase.HomotopyNonlinearFunction{false}(sys::System, args...; kwargs...) ODEFunction{false, SciMLBase.FullSpecialize}(sys, args...; kwargs...) end function SciMLBase.HomotopyNonlinearFunction{iip, specialize}( - sys::NonlinearSystem, args...; eval_expression = false, eval_module = @__MODULE__, + sys::System, args...; eval_expression = false, eval_module = @__MODULE__, p = nothing, fraction_cancel_fn = SymbolicUtils.simplify_fractions, cse = true, kwargs...) where {iip, specialize} if !iscomplete(sys) - error("A completed `NonlinearSystem` is required. Call `complete` or `structural_simplify` on the system before creating a `HomotopyContinuationFunction`") + error("A completed `System` is required. Call `complete` or `structural_simplify` on the system before creating a `HomotopyContinuationFunction`") end transformation = PolynomialTransformation(sys) if transformation isa NotPolynomialError @@ -529,11 +529,11 @@ end struct HomotopyContinuationProblem{iip, specialization} end -function HomotopyContinuationProblem(sys::NonlinearSystem, args...; kwargs...) +function HomotopyContinuationProblem(sys::System, args...; kwargs...) HomotopyContinuationProblem{true}(sys, args...; kwargs...) end -function HomotopyContinuationProblem(sys::NonlinearSystem, t, +function HomotopyContinuationProblem(sys::System, t, u0map::StaticArray, args...; kwargs...) @@ -541,19 +541,19 @@ function HomotopyContinuationProblem(sys::NonlinearSystem, t, sys, t, u0map, args...; kwargs...) end -function HomotopyContinuationProblem{true}(sys::NonlinearSystem, args...; kwargs...) +function HomotopyContinuationProblem{true}(sys::System, args...; kwargs...) HomotopyContinuationProblem{true, SciMLBase.AutoSpecialize}(sys, args...; kwargs...) end -function HomotopyContinuationProblem{false}(sys::NonlinearSystem, args...; kwargs...) +function HomotopyContinuationProblem{false}(sys::System, args...; kwargs...) HomotopyContinuationProblem{false, SciMLBase.FullSpecialize}(sys, args...; kwargs...) end function HomotopyContinuationProblem{iip, spec}( - sys::NonlinearSystem, u0map, pmap = SciMLBase.NullParameters(); + sys::System, u0map, pmap = SciMLBase.NullParameters(); kwargs...) where {iip, spec} if !iscomplete(sys) - error("A completed `NonlinearSystem` is required. Call `complete` or `structural_simplify` on the system before creating a `HomotopyContinuationProblem`") + error("A completed `System` is required. Call `complete` or `structural_simplify` on the system before creating a `HomotopyContinuationProblem`") end f, u0, p = process_SciMLProblem( HomotopyNonlinearFunction{iip, spec}, sys, u0map, pmap; kwargs...) diff --git a/src/systems/nonlinear/initializesystem.jl b/src/systems/nonlinear/initializesystem.jl index 0c60ac2ca1..1e7e4dc85c 100644 --- a/src/systems/nonlinear/initializesystem.jl +++ b/src/systems/nonlinear/initializesystem.jl @@ -10,7 +10,7 @@ end """ $(TYPEDSIGNATURES) -Generate `NonlinearSystem` which initializes a problem from specified initial conditions of an `AbstractTimeDependentSystem`. +Generate `System` of nonlinear equations which initializes a problem from specified initial conditions of an `AbstractTimeDependentSystem`. """ function generate_initializesystem_timevarying(sys::AbstractSystem; u0map = Dict(), @@ -153,8 +153,7 @@ function generate_initializesystem_timevarying(sys::AbstractSystem; for k in keys(defs) defs[k] = substitute(defs[k], paramsubs) end - - return NonlinearSystem(eqs_ics, + return System(eqs_ics, vars, pars; defaults = defs, @@ -168,7 +167,7 @@ end """ $(TYPEDSIGNATURES) -Generate `NonlinearSystem` which initializes a problem from specified initial conditions of an `AbstractTimeDependentSystem`. +Generate `System` of nonlinear equations which initializes a problem from specified initial conditions of an `AbstractTimeDependentSystem`. """ function generate_initializesystem_timeindependent(sys::AbstractSystem; u0map = Dict(), @@ -252,8 +251,7 @@ function generate_initializesystem_timeindependent(sys::AbstractSystem; for k in keys(defs) defs[k] = substitute(defs[k], paramsubs) end - - return NonlinearSystem(eqs_ics, + return System(eqs_ics, vars, pars; defaults = defs, diff --git a/src/systems/nonlinear/modelingtoolkitize.jl b/src/systems/nonlinear/modelingtoolkitize.jl index 2f12157884..4ce3769ef7 100644 --- a/src/systems/nonlinear/modelingtoolkitize.jl +++ b/src/systems/nonlinear/modelingtoolkitize.jl @@ -1,7 +1,7 @@ """ $(TYPEDSIGNATURES) -Generate `NonlinearSystem`, dependent variables, and parameters from an `NonlinearProblem`. +Generate `System`, dependent variables, and parameters from an `NonlinearProblem`. """ function modelingtoolkitize( prob::Union{NonlinearProblem, NonlinearLeastSquaresProblem}; @@ -76,7 +76,7 @@ function modelingtoolkitize( Dict() end - de = NonlinearSystem(eqs, sts, params, + de = System(eqs, sts, params, defaults = merge(default_u0, default_p); name = gensym(:MTKizedNonlinProb), kwargs...) From 25808618899fe4d313f086f26f93cced16326134 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 21 Apr 2025 16:25:24 +0530 Subject: [PATCH 1685/2176] refactor: remove references to `DiscreteSystem` --- src/ModelingToolkit.jl | 1 - src/systems/abstractsystem.jl | 2 +- src/systems/systems.jl | 3 --- src/systems/systemstructure.jl | 10 ++++++---- 4 files changed, 7 insertions(+), 9 deletions(-) diff --git a/src/ModelingToolkit.jl b/src/ModelingToolkit.jl index 6d567c0625..23741dfe43 100644 --- a/src/ModelingToolkit.jl +++ b/src/ModelingToolkit.jl @@ -132,7 +132,6 @@ abstract type AbstractTimeDependentSystem <: AbstractSystem end abstract type AbstractTimeIndependentSystem <: AbstractSystem end abstract type AbstractMultivariateSystem <: AbstractSystem end abstract type AbstractOptimizationSystem <: AbstractTimeIndependentSystem end -abstract type AbstractDiscreteSystem <: AbstractTimeDependentSystem end function independent_variable end diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index f28b0f1559..099d569d2f 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -699,7 +699,7 @@ function add_initialization_parameters(sys::AbstractSystem; split = true) end # add derivatives of all variables for steady-state initial conditions - if is_time_dependent(sys) && !(sys isa AbstractDiscreteSystem) + if is_time_dependent(sys) && !is_discrete_system(sys) D = Differential(get_iv(sys)) union!(all_initialvars, [D(v) for v in all_initialvars if iscall(v)]) end diff --git a/src/systems/systems.jl b/src/systems/systems.jl index 1aaa07eaea..44a31ab921 100644 --- a/src/systems/systems.jl +++ b/src/systems/systems.jl @@ -36,9 +36,6 @@ function structural_simplify( else newsys = newsys′ end - if newsys isa DiscreteSystem && - any(eq -> symbolic_type(eq.lhs) == NotSymbolic(), equations(newsys)) - end for pass in additional_passes newsys = pass(newsys) end diff --git a/src/systems/systemstructure.jl b/src/systems/systemstructure.jl index 611ff72095..144aad148e 100644 --- a/src/systems/systemstructure.jl +++ b/src/systems/systemstructure.jl @@ -474,11 +474,9 @@ function TearingState(sys; quick_cancel = false, check = true, sort_eqs = true) ts = TearingState(sys, fullvars, SystemStructure(complete(var_to_diff), complete(eq_to_diff), - complete(graph), nothing, var_types, sys isa AbstractDiscreteSystem), + complete(graph), nothing, var_types, false), Any[], param_derivative_map) - if sys isa DiscreteSystem - ts = shift_discrete_system(ts) - end + return ts end @@ -681,6 +679,10 @@ function structural_simplify!(state::TearingState; simplify = false, """)) end end + if continuous_id == 1 && any(Base.Fix2(isoperator, Shift), state.fullvars) + state.structure.only_discrete = true + end + sys = _structural_simplify!(state; simplify, check_consistency, inputs, outputs, disturbance_inputs, fully_determined, kwargs...) From aa7961eda277f1491ff9442c1c7111bdc0a56ec8 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 22 Apr 2025 16:54:06 +0530 Subject: [PATCH 1686/2176] refactor: do not use `sys.substitutions` --- src/structural_transformation/symbolics_tearing.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/structural_transformation/symbolics_tearing.jl b/src/structural_transformation/symbolics_tearing.jl index 60d24e69ce..04c88d1d52 100644 --- a/src/structural_transformation/symbolics_tearing.jl +++ b/src/structural_transformation/symbolics_tearing.jl @@ -150,7 +150,7 @@ end function tearing_substitution(sys::AbstractSystem; kwargs...) neweqs = full_equations(sys::AbstractSystem; kwargs...) @set! sys.eqs = neweqs - @set! sys.substitutions = nothing + # @set! sys.substitutions = nothing @set! sys.schedule = nothing end @@ -753,7 +753,7 @@ function update_simplified_system!( @set! sys.eqs = neweqs @set! sys.observed = obs - @set! sys.substitutions = Substitutions(subeqs, deps) + # @set! sys.substitutions = Substitutions(subeqs, deps) # Only makes sense for time-dependent if ModelingToolkit.has_schedule(sys) From 83489081101a0263a235888daab887ba9d2a79aa Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 14 May 2025 15:23:03 +0530 Subject: [PATCH 1687/2176] test: replace `ODESystem` with `System` --- test/accessor_functions.jl | 8 +- test/analysis_points.jl | 16 +- test/basic_transformations.jl | 32 +- test/bvproblem.jl | 32 +- test/causal_variables_connection.jl | 8 +- test/clock.jl | 18 +- test/code_generation.jl | 6 +- test/components.jl | 36 +-- test/constants.jl | 8 +- test/dae_jacobian.jl | 2 +- test/debugging.jl | 4 +- test/dep_graphs.jl | 2 +- test/distributed.jl | 2 +- test/domain_connectors.jl | 12 +- test/downstream/analysis_points.jl | 52 ++-- test/downstream/inversemodel.jl | 2 +- test/downstream/linearization_dd.jl | 2 +- test/downstream/linearize.jl | 22 +- test/downstream/test_disturbance_model.jl | 4 +- test/dq_units.jl | 38 +-- test/equation_type_accessors.jl | 6 +- test/error_handling.jl | 8 +- test/extensions/ad.jl | 10 +- test/extensions/bifurcationkit.jl | 2 +- test/extensions/dynamic_optimization.jl | 24 +- test/fmi/fmi.jl | 12 +- test/funcaffect.jl | 32 +- test/function_registration.jl | 8 +- test/guess_propagation.jl | 16 +- test/hierarchical_initialization_eqs.jl | 12 +- test/index_cache.jl | 16 +- test/initial_values.jl | 46 +-- test/initializationsystem.jl | 92 +++--- test/input_output_handling.jl | 54 ++-- test/inputoutput.jl | 6 +- test/jacobiansparsity.jl | 4 +- test/labelledarrays.jl | 2 +- test/latexify.jl | 2 +- test/linearity.jl | 6 +- test/lowering_solving.jl | 10 +- test/mass_matrix.jl | 8 +- test/model_parsing.jl | 10 +- test/modelingtoolkitize.jl | 2 +- test/mtkparameters.jl | 22 +- test/namespacing.jl | 6 +- test/nonlinearsystem.jl | 14 +- test/odesystem.jl | 282 +++++++++--------- test/parameter_dependencies.jl | 36 +-- test/precompile_test/ODEPrecompileTest.jl | 2 +- test/problem_validation.jl | 4 +- test/reduction.jl | 36 +-- test/scc_nonlinear_problem.jl | 4 +- test/sciml_problem_inputs.jl | 2 +- test/sdesystem.jl | 22 +- test/serialization.jl | 4 +- test/split_parameters.jl | 20 +- test/state_selection.jl | 22 +- test/static_arrays.jl | 2 +- test/steadystatesystems.jl | 2 +- test/stream_connectors.jl | 50 ++-- .../index_reduction.jl | 14 +- test/structural_transformation/tearing.jl | 8 +- test/structural_transformation/utils.jl | 20 +- test/substitute_component.jl | 6 +- test/symbolic_events.jl | 118 ++++---- test/symbolic_indexing_interface.jl | 18 +- test/symbolic_parameters.jl | 2 +- test/test_variable_metadata.jl | 6 +- test/units.jl | 36 +-- test/variable_scope.jl | 22 +- test/variable_utils.jl | 6 +- 71 files changed, 740 insertions(+), 740 deletions(-) diff --git a/test/accessor_functions.jl b/test/accessor_functions.jl index 4136736a8b..707b928e75 100644 --- a/test/accessor_functions.jl +++ b/test/accessor_functions.jl @@ -56,13 +56,13 @@ let ] cevs = [[t ~ 1.0] => [Y ~ Pre(Y) + 2.0]] devs = [(t == 2.0) => [Y ~ Pre(Y) + 2.0]] - @named sys_bot = ODESystem( + @named sys_bot = System( eqs_bot, t; systems = [], continuous_events = cevs, discrete_events = devs) - @named sys_mid2 = ODESystem( + @named sys_mid2 = System( eqs_mid2, t; systems = [], continuous_events = cevs, discrete_events = devs) - @named sys_mid1 = ODESystem( + @named sys_mid1 = System( eqs_mid1, t; systems = [sys_bot], continuous_events = cevs, discrete_events = devs) - @named sys_top = ODESystem(eqs_top, t; systems = [sys_mid1, sys_mid2], + @named sys_top = System(eqs_top, t; systems = [sys_mid1, sys_mid2], continuous_events = cevs, discrete_events = devs) sys_bot_comp = complete(sys_bot) sys_mid2_comp = complete(sys_mid2) diff --git a/test/analysis_points.jl b/test/analysis_points.jl index e36afc02f7..54cf0004c6 100644 --- a/test/analysis_points.jl +++ b/test/analysis_points.jl @@ -12,14 +12,14 @@ using Symbolics: NAMESPACE_SEPARATOR ap = AnalysisPoint(:plant_input) eqs = [connect(P.output, C.input) connect(C.output, ap, P.input)] - sys_ap = ODESystem(eqs, t, systems = [P, C], name = :hej) + sys_ap = System(eqs, t, systems = [P, C], name = :hej) sys_ap2 = @test_nowarn expand_connections(sys_ap) @test all(eq -> !(eq.lhs isa AnalysisPoint), equations(sys_ap2)) eqs = [connect(P.output, C.input) connect(C.output, P.input)] - sys_normal = ODESystem(eqs, t, systems = [P, C], name = :hej) + sys_normal = System(eqs, t, systems = [P, C], name = :hej) sys_normal2 = @test_nowarn expand_connections(sys_normal) @test isequal(sys_ap2, sys_normal2) @@ -41,10 +41,10 @@ end @named C = Gain(; k = -1) eqs = [connect(P.output, C.input), connect(C.output, :plant_input, P.input)] - sys_ap = ODESystem(eqs, t, systems = [P, C], name = :hej) + sys_ap = System(eqs, t, systems = [P, C], name = :hej) ap2 = @test_nowarn sys_ap.plant_input @test nameof(ap2) == Symbol(join(["hej", "plant_input"], NAMESPACE_SEPARATOR)) - @named sys = ODESystem(Equation[], t; systems = [sys_ap]) + @named sys = System(Equation[], t; systems = [sys_ap]) ap3 = @test_nowarn sys.hej.plant_input @test nameof(ap3) == Symbol(join(["sys", "hej", "plant_input"], NAMESPACE_SEPARATOR)) csys = complete(sys) @@ -62,8 +62,8 @@ end ap = AnalysisPoint(:plant_input) eqs = [connect(P.output, C.input), connect(C.output, ap, P.input)] -sys = ODESystem(eqs, t, systems = [P, C], name = :hej) -@named nested_sys = ODESystem(Equation[], t; systems = [sys]) +sys = System(eqs, t, systems = [P, C], name = :hej) +@named nested_sys = System(Equation[], t; systems = [sys]) nonamespace_sys = toggle_namespacing(nested_sys, false) @testset "simplifies and solves" begin @@ -132,8 +132,8 @@ end eqs = [connect(P.output, :plant_output, C.input) connect(C.output, :plant_input, P.input)] -sys = ODESystem(eqs, t, systems = [P, C], name = :hej) -@named nested_sys = ODESystem(Equation[], t; systems = [sys]) +sys = System(eqs, t, systems = [P, C], name = :hej) +@named nested_sys = System(Equation[], t; systems = [sys]) test_cases = [ ("inner", sys, sys.plant_input), diff --git a/test/basic_transformations.jl b/test/basic_transformations.jl index bb57e27ea5..a414120ed1 100644 --- a/test/basic_transformations.jl +++ b/test/basic_transformations.jl @@ -8,7 +8,7 @@ D = Differential(t) @parameters α β γ δ @variables x(t) y(t) eqs = [D(x) ~ α * x - β * x * y, D(y) ~ -δ * y + γ * x * y] - @named sys = ODESystem(eqs, t) + @named sys = System(eqs, t) sys = complete(sys) u0 = [x => 1.0, y => 1.0] @@ -29,7 +29,7 @@ end @testset "Change independent variable (trivial)" begin @variables x(t) y(t) eqs1 = [D(D(x)) ~ D(x) + x, D(y) ~ 1] - M1 = ODESystem(eqs1, t; name = :M) + M1 = System(eqs1, t; name = :M) M2 = change_independent_variable(M1, y) @variables y x(y) yˍt(y) Dy = Differential(y) @@ -48,7 +48,7 @@ end D(s) ~ 1 / (2 * s) ] initialization_eqs = [x ~ 1.0, y ~ 1.0, D(y) ~ 0.0] - M1 = ODESystem(eqs, t; initialization_eqs, name = :M) + M1 = System(eqs, t; initialization_eqs, name = :M) M2 = change_independent_variable(M1, s) M1 = structural_simplify(M1; allow_symbolic = true) @@ -68,7 +68,7 @@ end D = Differential(t) @variables a(t) ȧ(t) Ω(t) ϕ(t) a, ȧ = GlobalScope.([a, ȧ]) - species(w; kw...) = ODESystem([D(Ω) ~ -3(1 + w) * D(a) / a * Ω], t, [Ω], []; kw...) + species(w; kw...) = System([D(Ω) ~ -3(1 + w) * D(a) / a * Ω], t, [Ω], []; kw...) @named r = species(1 // 3) @named m = species(0) @named Λ = species(-1) @@ -78,7 +78,7 @@ end ȧ ~ √(Ω) * a^2, D(D(ϕ)) ~ -3 * D(a) / a * D(ϕ) ] - M1 = ODESystem(eqs, t, [Ω, a, ȧ, ϕ], []; name = :M) + M1 = System(eqs, t, [Ω, a, ȧ, ϕ], []; name = :M) M1 = compose(M1, r, m, Λ) # Apply in two steps, where derivatives are defined at each step: first t -> a, then a -> b @@ -109,7 +109,7 @@ end @testset "Change independent variable (simple)" begin @variables x(t) y1(t) # y(t)[1:1] # TODO: use array variables y(t)[1:2] when fixed: https://github.com/JuliaSymbolics/Symbolics.jl/issues/1383 - Mt = ODESystem([D(x) ~ 2 * x, D(y1) ~ y1], t; name = :M) + Mt = System([D(x) ~ 2 * x, D(y1) ~ y1], t; name = :M) Mx = change_independent_variable(Mt, x) @variables x xˍt(x) xˍtt(x) y1(x) # y(x)[1:1] # TODO: array variables Dx = Differential(x) @@ -119,7 +119,7 @@ end @testset "Change independent variable (free fall with 1st order horizontal equation)" begin @variables x(t) y(t) @parameters g=9.81 v # gravitational acceleration and constant horizontal velocity - Mt = ODESystem([D(D(y)) ~ -g, D(x) ~ v], t; name = :M) # gives (x, y) as function of t, ... + Mt = System([D(D(y)) ~ -g, D(x) ~ v], t; name = :M) # gives (x, y) as function of t, ... Mx = change_independent_variable(Mt, x; add_old_diff = true) # ... but we want y as a function of x Mx = structural_simplify(Mx; allow_symbolic = true) Dx = Differential(Mx.x) @@ -133,7 +133,7 @@ end @testset "Change independent variable (free fall with 2nd order horizontal equation)" begin @variables x(t) y(t) @parameters g = 9.81 # gravitational acceleration - Mt = ODESystem([D(D(y)) ~ -g, D(D(x)) ~ 0], t; name = :M) # gives (x, y) as function of t, ... + Mt = System([D(D(y)) ~ -g, D(D(x)) ~ 0], t; name = :M) # gives (x, y) as function of t, ... Mx = change_independent_variable(Mt, x; add_old_diff = true) # ... but we want y as a function of x Mx = structural_simplify(Mx; allow_symbolic = true) Dx = Differential(Mx.x) @@ -151,7 +151,7 @@ end (D^3)(y) ~ D(x)^2 + (D^2)(y^2) |> expand_derivatives, D(x)^2 + D(y)^2 ~ x^4 + y^5 + t^6 ] - M1 = ODESystem(eqs, t; name = :M) + M1 = System(eqs, t; name = :M) M2 = change_independent_variable(M1, x; add_old_diff = true) @test_nowarn structural_simplify(M2) @@ -185,7 +185,7 @@ end D(x) ~ 2t, D(y) ~ 1fc(t) + 2fc(x) + 3fc(y) + 1callme(f, t) + 2callme(f, x) + 3callme(f, y) ] - M1 = ODESystem(eqs, t; name = :M) + M1 = System(eqs, t; name = :M) # Ensure that interpolations are called with the same variables M2 = change_independent_variable(M1, x, [t ~ √(x)]) @@ -207,13 +207,13 @@ end @testset "Change independent variable (errors)" begin @variables x(t) y z(y) w(t) v(t) - M = ODESystem([D(x) ~ 1, v ~ x], t; name = :M) + M = System([D(x) ~ 1, v ~ x], t; name = :M) Ms = structural_simplify(M) @test_throws "structurally simplified" change_independent_variable(Ms, y) @test_throws "not a function of" change_independent_variable(M, y) @test_throws "not a function of" change_independent_variable(M, z) @variables x(..) # require explicit argument - M = ODESystem([D(x(t)) ~ x(t - 1)], t; name = :M) + M = System([D(x(t)) ~ x(t - 1)], t; name = :M) @test_throws "DDE" change_independent_variable(M, x(t)) end @@ -222,7 +222,7 @@ end D_units = Differential(t_units) @variables x(t_units) [unit = u"m"] y(t_units) [unit = u"m"] @parameters g=9.81 [unit = u"m * s^-2"] # gravitational acceleration - Mt = ODESystem([D_units(D_units(y)) ~ -g, D_units(D_units(x)) ~ 0], t_units; name = :M) # gives (x, y) as function of t, ... + Mt = System([D_units(D_units(y)) ~ -g, D_units(D_units(x)) ~ 0], t_units; name = :M) # gives (x, y) as function of t, ... Mx = change_independent_variable(Mt, x; add_old_diff = true) # ... but we want y as a function of x Mx = structural_simplify(Mx; allow_symbolic = true) Dx = Differential(Mx.x) @@ -243,7 +243,7 @@ end @named input_sys = Input() input_sys = complete(input_sys) # test no failures - @test change_independent_variable(input_sys, input_sys.u) isa ODESystem + @test change_independent_variable(input_sys, input_sys.u) isa System @mtkmodel NestedInput begin @components begin @@ -258,7 +258,7 @@ end end @named nested_input_sys = NestedInput() nested_input_sys = complete(nested_input_sys; flatten = false) - @test change_independent_variable(nested_input_sys, nested_input_sys.x) isa ODESystem + @test change_independent_variable(nested_input_sys, nested_input_sys.x) isa System end @testset "Change of variables, connections" begin @@ -295,7 +295,7 @@ end z ~ ModelingToolkit.scalarize.([sin(y), cos(y)]), D(y) ~ z[1]^2 + z[2]^2 ] - @named sys = ODESystem(eqs, t) + @named sys = System(eqs, t) sys = complete(sys) new_sys = change_independent_variable(sys, sys.x; add_old_diff = true) ss_new_sys = structural_simplify(new_sys; allow_symbolic = true) diff --git a/test/bvproblem.jl b/test/bvproblem.jl index 6472608366..dc5f1ef658 100644 --- a/test/bvproblem.jl +++ b/test/bvproblem.jl @@ -20,7 +20,7 @@ daesolvers = [Ascher2, Ascher4, Ascher6] parammap = [α => 7.5, β => 4, γ => 8.0, δ => 5.0] tspan = (0.0, 10.0) - @mtkbuild lotkavolterra = ODESystem(eqs, t) + @mtkbuild lotkavolterra = System(eqs, t) op = ODEProblem(lotkavolterra, u0map, tspan, parammap) osol = solve(op, Vern9()) @@ -52,7 +52,7 @@ end eqs = [D(θ) ~ θ_t D(θ_t) ~ -(g / L) * sin(θ)] - @mtkbuild pend = ODESystem(eqs, t) + @mtkbuild pend = System(eqs, t) u0map = [θ => π / 2, θ_t => π / 2] parammap = [:L => 1.0, :g => 9.81] @@ -80,7 +80,7 @@ end end ################################################################## -### ODESystem with constraint equations, DAEs with constraints ### +### System with constraint equations, DAEs with constraints ### ################################################################## # Test generation of boundary condition function using `generate_function_bc`. Compare solutions to manually written boundary conditions @@ -91,7 +91,7 @@ end D(y(t)) ~ -γ * y(t) + δ * x(t) * y(t)] tspan = (0.0, 1.0) - @mtkbuild lksys = ODESystem(eqs, t) + @mtkbuild lksys = System(eqs, t) function lotkavolterra!(du, u, p, t) du[1] = p[1] * u[1] - p[2] * u[1] * u[2] @@ -104,7 +104,7 @@ end # Test with a constraint. constr = [y(0.5) ~ 2.0] - @mtkbuild lksys = ODESystem(eqs, t; constraints = constr) + @mtkbuild lksys = System(eqs, t; constraints = constr) function bc!(resid, u, p, t) resid[1] = u(0.0)[1] - 1.0 @@ -165,7 +165,7 @@ function test_solvers( end end -# Simple ODESystem with BVP constraints. +# Simple System with BVP constraints. @testset "ODE with constraints" begin @parameters α=1.5 β=1.0 γ=3.0 δ=1.0 @variables x(..) y(..) @@ -177,7 +177,7 @@ end tspan = (0.0, 1.0) guess = [x(t) => 4.0, y(t) => 2.0] constr = [x(0.6) ~ 3.5, x(0.3) ~ 7.0] - @mtkbuild lksys = ODESystem(eqs, t; constraints = constr) + @mtkbuild lksys = System(eqs, t; constraints = constr) bvp = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}( lksys, u0map, tspan; guesses = guess) @@ -185,13 +185,13 @@ end # Testing that more complicated constraints give correct solutions. constr = [y(0.2) + x(0.8) ~ 3.0, y(0.3) ~ 2.0] - @mtkbuild lksys = ODESystem(eqs, t; constraints = constr) + @mtkbuild lksys = System(eqs, t; constraints = constr) bvp = SciMLBase.BVProblem{false, SciMLBase.FullSpecialize}( lksys, u0map, tspan; guesses = guess) test_solvers(solvers, bvp, u0map, constr; dt = 0.05) constr = [α * β - x(0.6) ~ 0.0, y(0.2) ~ 3.0] - @mtkbuild lksys = ODESystem(eqs, t; constraints = constr) + @mtkbuild lksys = System(eqs, t; constraints = constr) bvp = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}( lksys, u0map, tspan; guesses = guess) test_solvers(solvers, bvp, u0map, constr) @@ -205,7 +205,7 @@ end # eqs = [D(D(x)) ~ λ * x # D(D(y)) ~ λ * y - g # x^2 + y^2 ~ 1] -# @mtkbuild pend = ODESystem(eqs, t) +# @mtkbuild pend = System(eqs, t) # # tspan = (0.0, 1.5) # u0map = [x => 1, y => 0] @@ -243,7 +243,7 @@ end # D(D(y)) ~ λ * y - g # x(t)^2 + y^2 ~ 1] # constr = [x(0.5) ~ 1] -# @mtkbuild pend = ODESystem(eqs, t; constr) +# @mtkbuild pend = System(eqs, t; constr) # # tspan = (0.0, 1.5) # u0map = [x(t) => 0.6, y => 0.8] @@ -262,13 +262,13 @@ end # # constr = [x(0.5) ~ 1, # x(0.3)^3 + y(0.6)^2 ~ 0.5] -# @mtkbuild pend = ODESystem(eqs, t; constr) +# @mtkbuild pend = System(eqs, t; constr) # bvp = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}(pend, u0map, tspan, parammap; guesses, check_length = false) # test_solvers(daesolvers, bvp, u0map, constr, get_alg_eqs(pend)) # # constr = [x(0.4) * g ~ y(0.2), # y(0.7) ~ 0.3] -# @mtkbuild pend = ODESystem(eqs, t; constr) +# @mtkbuild pend = System(eqs, t; constr) # bvp = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}(pend, u0map, tspan, parammap; guesses, check_length = false) # test_solvers(daesolvers, bvp, u0map, constr, get_alg_eqs(pend)) # end @@ -286,8 +286,8 @@ end parammap = [α => 7.5, β => 4, γ => 8.0, δ => 5.0] costs = [x(0.6), x(0.3)^2] consolidate(u) = (u[1] + 3)^2 + u[2] - @mtkbuild lksys = ODESystem(eqs, t; costs, consolidate) - @test_throws ErrorException @mtkbuild lksys2 = ODESystem(eqs, t; costs) + @mtkbuild lksys = System(eqs, t; costs, consolidate) + @test_throws ErrorException @mtkbuild lksys2 = System(eqs, t; costs) @test_throws ErrorException ODEProblem(lksys, u0map, tspan, parammap) prob = ODEProblem(lksys, u0map, tspan, parammap; allow_cost = true) @@ -300,7 +300,7 @@ end @parameters t_c costs = [y(t_c) + x(0.0), x(0.4)^2] consolidate(u) = log(u[1]) - u[2] - @mtkbuild lksys = ODESystem(eqs, t; costs, consolidate) + @mtkbuild lksys = System(eqs, t; costs, consolidate) @test t_c ∈ Set(parameters(lksys)) push!(parammap, t_c => 0.56) prob = ODEProblem(lksys, u0map, tspan, parammap; allow_cost = true) diff --git a/test/causal_variables_connection.jl b/test/causal_variables_connection.jl index c22e8319d4..222db540de 100644 --- a/test/causal_variables_connection.jl +++ b/test/causal_variables_connection.jl @@ -40,12 +40,12 @@ end eqs = [connect(P.output.u, C.input.u) connect(C.output.u, P.input.u)] - sys1 = ODESystem(eqs, t, systems = [P, C], name = :hej) + sys1 = System(eqs, t, systems = [P, C], name = :hej) sys = expand_connections(sys1) @test any(isequal(P.output.u ~ C.input.u), equations(sys)) @test any(isequal(C.output.u ~ P.input.u), equations(sys)) - @named sysouter = ODESystem(Equation[], t; systems = [sys1]) + @named sysouter = System(Equation[], t; systems = [sys1]) sys = expand_connections(sysouter) @test any(isequal(sys1.P.output.u ~ sys1.C.input.u), equations(sys)) @test any(isequal(sys1.C.output.u ~ sys1.P.input.u), equations(sys)) @@ -57,8 +57,8 @@ end ap = AnalysisPoint(:plant_input) eqs = [connect(P.output, C.input), connect(C.output.u, ap, P.input.u)] - sys = ODESystem(eqs, t, systems = [P, C], name = :hej) - @named nested_sys = ODESystem(Equation[], t; systems = [sys]) + sys = System(eqs, t, systems = [P, C], name = :hej) + @named nested_sys = System(Equation[], t; systems = [sys]) test_cases = [ ("inner", sys, sys.plant_input), diff --git a/test/clock.jl b/test/clock.jl index c4c64dbf90..3c5a32c1ce 100644 --- a/test/clock.jl +++ b/test/clock.jl @@ -22,7 +22,7 @@ eqs = [yd ~ Sample(dt)(y) u ~ Hold(ud) D(x) ~ -x + u y ~ x] -@named sys = ODESystem(eqs, t) +@named sys = System(eqs, t) # compute equation and variables' time domains #TODO: test linearize @@ -115,7 +115,7 @@ eqs = [yd ~ Sample(dt)(y) u ~ Hold(ud) D(x) ~ -x + u y ~ x] -@named sys = ODESystem(eqs, t) +@named sys = System(eqs, t) @test_throws ModelingToolkit.HybridSystemNotSupportedException ss=structural_simplify(sys); @test_skip begin @@ -190,7 +190,7 @@ eqs = [yd ~ Sample(dt)(y) u ~ Hold(ud1) + Hold(ud2) D(x) ~ -x + u y ~ x] - @named sys = ODESystem(eqs, t) + @named sys = System(eqs, t) ci, varmap = infer_clocks(sys) d = Clock(dt) @@ -218,7 +218,7 @@ eqs = [yd ~ Sample(dt)(y) @variables x(t)=1 u(t)=0 y(t)=0 eqs = [D(x) ~ -x + u y ~ x] - ODESystem(eqs, t; name = name) + System(eqs, t; name = name) end function filt(; name) @@ -226,7 +226,7 @@ eqs = [yd ~ Sample(dt)(y) a = 1 / exp(dt) eqs = [x ~ a * x(k - 1) + (1 - a) * u(k - 1) y ~ x] - ODESystem(eqs, t, name = name) + System(eqs, t, name = name) end function controller(kp; name) @@ -234,7 +234,7 @@ eqs = [yd ~ Sample(dt)(y) @parameters kp = kp eqs = [yd ~ Sample(y) ud ~ kp * (r - yd)] - ODESystem(eqs, t; name = name) + System(eqs, t; name = name) end @named f = filt() @@ -246,7 +246,7 @@ eqs = [yd ~ Sample(dt)(y) Hold(c.ud) ~ p.u # controller output to plant input p.y ~ c.y] - @named cl = ODESystem(connections, t, systems = [f, c, p]) + @named cl = System(connections, t, systems = [f, c, p]) ci, varmap = infer_clocks(cl) @@ -281,7 +281,7 @@ eqs = [yd ~ Sample(dt)(y) D(x) ~ -x + u y ~ x] - @named cl = ODESystem(eqs, t) + @named cl = System(eqs, t) d = Clock(dt) d2 = Clock(dt2) @@ -529,7 +529,7 @@ eqs = [yd ~ Sample(dt)(y) @variables x(t)=1.0 y(t)=1.0 eqs = [D(y) ~ Hold(x) x ~ x(k - 1) + x(k - 2)] - @mtkbuild sys = ODESystem(eqs, t) + @mtkbuild sys = System(eqs, t) prob = ODEProblem(sys, [], (0.0, 10.0)) int = init(prob, Tsit5(); kwargshandle = KeywordArgSilent) @test int.ps[x] == 2.0 diff --git a/test/code_generation.jl b/test/code_generation.jl index 2fbf8f13d7..87a485e198 100644 --- a/test/code_generation.jl +++ b/test/code_generation.jl @@ -5,7 +5,7 @@ using ModelingToolkit: t_nounits as t, D_nounits as D @variables x(t) y(t)[1:3] @parameters p1=1.0 p2[1:3]=[1.0, 2.0, 3.0] p3::Int=1 p4::Bool=false - sys = complete(ODESystem(Equation[], t, [x; y], [p1, p2, p3, p4]; name = :sys)) + sys = complete(System(Equation[], t, [x; y], [p1, p2, p3, p4]; name = :sys)) u0 = [1.0, 2.0, 3.0, 4.0] p = ModelingToolkit.MTKParameters(sys, []) @@ -58,7 +58,7 @@ end @testset "Non-standard array variables" begin @variables x(t) @parameters p[0:2] (f::Function)(..) - @mtkbuild sys = ODESystem(D(x) ~ p[0] * x + p[1] * t + p[2] + f(p), t) + @mtkbuild sys = System(D(x) ~ p[0] * x + p[1] * t + p[2] + f(p), t) prob = ODEProblem(sys, [x => 1.0], (0.0, 1.0), [p => [1.0, 2.0, 3.0], f => sum]) @test prob.ps[p] == [1.0, 2.0, 3.0] @test prob.ps[p[0]] == 1.0 @@ -68,7 +68,7 @@ end @testset "Array split across buffers" begin @variables x(t)[0:2] @parameters p[1:2] (f::Function)(..) - @named sys = ODESystem( + @named sys = System( [D(x[0]) ~ p[1] * x[0] + x[2], D(x[1]) ~ p[2] * f(x) + x[2]], t) sys = structural_simplify(sys, inputs = [x[2]], outputs = []) @test is_parameter(sys, x[2]) diff --git a/test/components.jl b/test/components.jl index 43c0d1acf8..282c4c0aa7 100644 --- a/test/components.jl +++ b/test/components.jl @@ -75,7 +75,7 @@ end eqs = [connect(p, resistor.p); connect(resistor.n, capacitor.p); connect(capacitor.n, n)] - @named sys = ODESystem(eqs, t, [], [R, C]) + @named sys = System(eqs, t, [], [R, C]) compose(sys, [p, n, resistor, capacitor]; name = name) end @@ -87,7 +87,7 @@ end connect(source.p, rc_comp.p) connect(source.n, rc_comp.n) connect(source.n, ground.g)] - @named sys′ = ODESystem(eqs, t) + @named sys′ = System(eqs, t) @named sys_inner_outer = compose(sys′, [ground, shape, source, rc_comp]) @test_nowarn show(IOBuffer(), MIME"text/plain"(), sys_inner_outer) expand_connections(sys_inner_outer, debug = true) @@ -131,16 +131,16 @@ end @testset "Compose/extend" begin @variables x1(t) x2(t) x3(t) x4(t) - @named sys1_inner = ODESystem([D(x1) ~ x1], t) - @named sys1_partial = compose(ODESystem([D(x2) ~ x2], t; name = :foo), sys1_inner) - @named sys1 = extend(ODESystem([D(x3) ~ x3], t; name = :foo), sys1_partial) - @named sys2 = compose(ODESystem([D(x4) ~ x4], t; name = :foo), sys1) + @named sys1_inner = System([D(x1) ~ x1], t) + @named sys1_partial = compose(System([D(x2) ~ x2], t; name = :foo), sys1_inner) + @named sys1 = extend(System([D(x3) ~ x3], t; name = :foo), sys1_partial) + @named sys2 = compose(System([D(x4) ~ x4], t; name = :foo), sys1) @test_nowarn sys2.sys1.sys1_inner.x1 # test the correct nesting # compose tests function record_fun(; name) pars = @parameters a=10 b=100 - ODESystem(Equation[], t, [], pars; name) + System(Equation[], t, [], pars; name) end function first_model(; name) @@ -150,7 +150,7 @@ end defs[foo.a] = 3 defs[foo.b] = 300 pars = @parameters x=2 y=20 - compose(ODESystem(Equation[], t, [], pars; name, defaults = defs), foo) + compose(System(Equation[], t, [], pars; name, defaults = defs), foo) end @named goo = first_model() @unpack foo = goo @@ -165,7 +165,7 @@ function Load(; name) @named resistor = Resistor(R = R) eqs = [connect(p, resistor.p); connect(resistor.n, n)] - @named sys = ODESystem(eqs, t) + @named sys = System(eqs, t) compose(sys, [p, n, resistor]; name = name) end @@ -176,7 +176,7 @@ function Circuit(; name) @named resistor = Resistor(R = R) eqs = [connect(load.p, ground.g); connect(resistor.p, ground.g)] - @named sys = ODESystem(eqs, t) + @named sys = System(eqs, t) compose(sys, [ground, resistor, load]; name = name) end @@ -196,7 +196,7 @@ end connect(capacitor.n, source.n, ground.g) connect(resistor.heat_port, heat_capacitor.port)] - compose(ODESystem(rc_eqs, t, name = Symbol(name, i)), + compose(System(rc_eqs, t, name = Symbol(name, i)), [resistor, capacitor, source, ground, heat_capacitor]) end V = 2.0 @@ -214,7 +214,7 @@ end D(E) ~ sum(((i, sys),) -> getproperty(sys, Symbol(:resistor, i)).heat_port.Q_flow, enumerate(rc_systems)) ] - @named _big_rc = ODESystem(eqs, t, [E], []) + @named _big_rc = System(eqs, t, [E], []) @named big_rc = compose(_big_rc, rc_systems) ts = TearingState(expand_connections(big_rc)) # this is block upper triangular, so `istriu` needs a little leeway @@ -230,7 +230,7 @@ end eqs = [ v ~ i * R ] - extend(ODESystem(eqs, t, [], []; name = name), oneport) + extend(System(eqs, t, [], []; name = name), oneport) end capacitor = Capacitor(; name = :c1, C = 1.0) resistor = FixedResistor(; name = :r1) @@ -239,7 +239,7 @@ end connect(resistor.n, capacitor.p) connect(capacitor.n, ground.g)] - @named _rc_model = ODESystem(rc_eqs, t) + @named _rc_model = System(rc_eqs, t) @named rc_model = compose(_rc_model, [resistor, capacitor, ground]) sys = structural_simplify(rc_model) @@ -254,7 +254,7 @@ end @connector function Pin1(; name) @independent_variables t sts = @variables v(t)=1.0 i(t)=1.0 - ODESystem(Equation[], t, sts, []; name = name) + System(Equation[], t, sts, []; name = name) end @test string(Base.doc(Pin1)) == "Hey there, Pin1!\n" @@ -264,7 +264,7 @@ end @component function Pin2(; name) @independent_variables t sts = @variables v(t)=1.0 i(t)=1.0 - ODESystem(Equation[], t, sts, []; name = name) + System(Equation[], t, sts, []; name = name) end @test string(Base.doc(Pin2)) == "Hey there, Pin2!\n" end @@ -328,8 +328,8 @@ end @testset "Issue#3275: Metadata retained on `complete`" begin @variables x(t) y(t) @testset "ODESystem" begin - @named inner = ODESystem(D(x) ~ x, t) - @named outer = ODESystem(D(y) ~ y, t; systems = [inner], metadata = "test") + @named inner = System(D(x) ~ x, t) + @named outer = System(D(y) ~ y, t; systems = [inner], metadata = "test") @test ModelingToolkit.get_metadata(outer) == "test" sys = complete(outer) @test ModelingToolkit.get_metadata(sys) == "test" diff --git a/test/constants.jl b/test/constants.jl index f2c4fdaa86..5e97d52d7f 100644 --- a/test/constants.jl +++ b/test/constants.jl @@ -10,7 +10,7 @@ UMT = ModelingToolkit.UnitfulUnitCheck @variables x(t) w(t) D = Differential(t) eqs = [D(x) ~ a] -@named sys = ODESystem(eqs, t) +@named sys = System(eqs, t) prob = ODEProblem(complete(sys), [0], [0.0, 1.0], []) sol = solve(prob, Tsit5()) @@ -20,7 +20,7 @@ newsys = MT.eliminate_constants(sys) # Test structural_simplify substitutions & observed values eqs = [D(x) ~ 1, w ~ a] -@named sys = ODESystem(eqs, t) +@named sys = System(eqs, t) # Now eliminate the constants first simp = structural_simplify(sys) @test equations(simp) == [D(x) ~ 1.0] @@ -33,7 +33,7 @@ UMT.get_unit(β) @variables x(t) [unit = u"m"] D = Differential(t) eqs = [D(x) ~ β] -@named sys = ODESystem(eqs, t) +@named sys = System(eqs, t) simp = structural_simplify(sys) @test isempty(MT.collect_constants(nothing)) @@ -44,7 +44,7 @@ simp = structural_simplify(sys) @variables x(MT.t_nounits) = h eqs = [MT.D_nounits(x) ~ (h - x) / τ] - @mtkbuild fol_model = ODESystem(eqs, MT.t_nounits) + @mtkbuild fol_model = System(eqs, MT.t_nounits) prob = ODEProblem(fol_model, [], (0.0, 10.0)) @test prob[x] ≈ 1 diff --git a/test/dae_jacobian.jl b/test/dae_jacobian.jl index 8c68df767a..94f15cbb7c 100644 --- a/test/dae_jacobian.jl +++ b/test/dae_jacobian.jl @@ -36,7 +36,7 @@ sol1 = solve(prob1, IDA(linear_solver = :KLU)) eqs = [D(u1) ~ p1 * u1 - u1 * u2, D(u2) ~ u1 * u2 - p2 * u2] -@named sys = ODESystem(eqs, t) +@named sys = System(eqs, t) u0 = [u1 => 1.0, u2 => 1.0] diff --git a/test/debugging.jl b/test/debugging.jl index a55684737c..de4420d08c 100644 --- a/test/debugging.jl +++ b/test/debugging.jl @@ -4,7 +4,7 @@ using ModelingToolkit: t_nounits as t, D_nounits as D, ASSERTION_LOG_VARIABLE @variables x(t) @brownian a -@named inner_ode = ODESystem(D(x) ~ -sqrt(x), t; assertions = [(x > 0) => "ohno"]) +@named inner_ode = System(D(x) ~ -sqrt(x), t; assertions = [(x > 0) => "ohno"]) @named inner_sde = System([D(x) ~ -sqrt(x) + a], t; assertions = [(x > 0) => "ohno"]) sys_ode = structural_simplify(inner_ode) sys_sde = structural_simplify(inner_sde) @@ -36,7 +36,7 @@ end @testset "Hierarchical system" begin @testset "$(typeof(inner))" for (ctor, Problem, inner, alg) in [ - (ODESystem, ODEProblem, inner_ode, Tsit5()), + (System, ODEProblem, inner_ode, Tsit5()), (System, SDEProblem, inner_sde, ImplicitEM())] @mtkbuild outer = ctor(Equation[], t; systems = [inner]) dsys = debug_system(outer; functions = []) diff --git a/test/dep_graphs.jl b/test/dep_graphs.jl index 3b02efb2d0..76cc216635 100644 --- a/test/dep_graphs.jl +++ b/test/dep_graphs.jl @@ -155,7 +155,7 @@ eqs = [D(S) ~ k1 - k1 * S - k2 * S * I - k1 * k2 / (1 + t) * S, D(I) ~ k2 * S * I, D(R) ~ -k2 * S^2 * R / 2 + k1 * I + k1 * k2 * S / (1 + t)] noiseeqs = [S, I, R] -@named os = ODESystem(eqs, t, [S, I, R], [k1, k2]) +@named os = System(eqs, t, [S, I, R], [k1, k2]) deps = equation_dependencies(os) S = value(S); I = value(I); diff --git a/test/distributed.jl b/test/distributed.jl index 0b75b8eeb3..e7edc17a66 100644 --- a/test/distributed.jl +++ b/test/distributed.jl @@ -13,7 +13,7 @@ addprocs(2) D(y) ~ x * (ρ - z) - y, D(z) ~ x * y - β * z] -@everywhere @named de = ODESystem(eqs, t) +@everywhere @named de = System(eqs, t) @everywhere de = complete(de) @everywhere u0 = [19.0, 20.0, 50.0] diff --git a/test/domain_connectors.jl b/test/domain_connectors.jl index 9a43d2938f..03d6ff264a 100644 --- a/test/domain_connectors.jl +++ b/test/domain_connectors.jl @@ -14,7 +14,7 @@ using Test dm(t), [connect = Flow] end - ODESystem(Equation[], t, vars, pars; name, defaults = [dm => 0]) + System(Equation[], t, vars, pars; name, defaults = [dm => 0]) end @connector function HydraulicFluid(; @@ -36,7 +36,7 @@ end dm ~ 0 ] - ODESystem(eqs, t, vars, pars; name, defaults = [dm => 0]) + System(eqs, t, vars, pars; name, defaults = [dm => 0]) end function FixedPressure(; p, name) @@ -54,7 +54,7 @@ function FixedPressure(; p, name) port.p ~ p ] - ODESystem(eqs, t, vars, pars; name, systems) + System(eqs, t, vars, pars; name, systems) end function FixedVolume(; vol, p_int, name) @@ -80,7 +80,7 @@ function FixedVolume(; vol, p_int, name) rho ~ port.ρ * (1 + p / port.β) dm ~ drho * vol] - ODESystem(eqs, t, vars, pars; name, systems) + System(eqs, t, vars, pars; name, systems) end function Valve2Port(; p_s_int, p_r_int, p_int, name) @@ -120,7 +120,7 @@ function Valve2Port(; p_s_int, p_r_int, p_int, name) HS.dm ~ ifelse(x >= 0, port.dm, 0) HR.dm ~ ifelse(x < 0, port.dm, 0)] - ODESystem(eqs, t, vars, pars; name, systems) + System(eqs, t, vars, pars; name, systems) end function System(; name) @@ -139,7 +139,7 @@ function System(; name) connect(vol.port, valve.port) valve.x ~ sin(2π * t * 10)] - return ODESystem(eqs, t, vars, pars; systems, name) + return System(eqs, t, vars, pars; systems, name) end @named odesys = System() diff --git a/test/downstream/analysis_points.jl b/test/downstream/analysis_points.jl index 58c4a97a8d..5dc0026138 100644 --- a/test/downstream/analysis_points.jl +++ b/test/downstream/analysis_points.jl @@ -22,7 +22,7 @@ import ControlSystemsBase as CS connect(inertia2.flange_a, spring.flange_b, damper.flange_b)] if u !== nothing push!(eqs, connect(torque.tau, u.output)) - return ODESystem(eqs, t; + return System(eqs, t; systems = [ torque, inertia1, @@ -33,7 +33,7 @@ import ControlSystemsBase as CS ], name) end - ODESystem(eqs, t; systems = [torque, inertia1, inertia2, spring, damper], name) + System(eqs, t; systems = [torque, inertia1, inertia2, spring, damper], name) end @named r = Step(start_time = 0) @@ -50,7 +50,7 @@ import ControlSystemsBase as CS connect(sensor.phi, :y, er.input2) connect(er.output, :e, pid.err_input)] - closed_loop = ODESystem(connections, t, systems = [model, pid, filt, sensor, r, er], + closed_loop = System(connections, t, systems = [model, pid, filt, sensor, r, er], name = :closed_loop, defaults = [ model.inertia1.phi => 0.0, model.inertia2.phi => 0.0, @@ -89,7 +89,7 @@ end connect(add.output, C.input) connect(C.output, P.input)] - sys_inner = ODESystem(eqs, t, systems = [P, C, add], name = :inner) + sys_inner = System(eqs, t, systems = [P, C, add], name = :inner) @named r = Constant(k = 1) @named F = FirstOrder(k = 1, T = 3) @@ -98,7 +98,7 @@ end connect(sys_inner.P.output, sys_inner.add.input2) connect(sys_inner.C.output, :plant_input, sys_inner.P.input) connect(F.output, sys_inner.add.input1)] - sys_outer = ODESystem(eqs, t, systems = [F, sys_inner, r], name = :outer) + sys_outer = System(eqs, t, systems = [F, sys_inner, r], name = :outer) # test first that the structural_simplify works correctly ssys = structural_simplify(sys_outer) @@ -125,7 +125,7 @@ end connect(add.output, C.input) connect(C.output.u, P.input.u)] - sys_inner = ODESystem(eqs, t, systems = [P, C, add], name = :inner) + sys_inner = System(eqs, t, systems = [P, C, add], name = :inner) @named r = Constant(k = 1) @named F = FirstOrder(k = 1, T = 3) @@ -134,7 +134,7 @@ end connect(sys_inner.P.output.u, sys_inner.add.input2.u) connect(sys_inner.C.output.u, :plant_input, sys_inner.P.input.u) connect(F.output, sys_inner.add.input1)] - sys_outer = ODESystem(eqs, t, systems = [F, sys_inner, r], name = :outer) + sys_outer = System(eqs, t, systems = [F, sys_inner, r], name = :outer) # test first that the structural_simplify works correctly ssys = structural_simplify(sys_outer) @@ -161,7 +161,7 @@ end connect(add.output, C.input) connect(C.output, P.input)] - sys_inner = ODESystem(eqs, t, systems = [P, C, add], name = :inner) + sys_inner = System(eqs, t, systems = [P, C, add], name = :inner) @named r = Constant(k = 1) @named F = FirstOrder(k = 1, T = 3) @@ -170,7 +170,7 @@ end connect(sys_inner.P.output, sys_inner.add.input2) connect(sys_inner.C.output.u, :plant_input, sys_inner.P.input.u) connect(F.output, sys_inner.add.input1)] - sys_outer = ODESystem(eqs, t, systems = [F, sys_inner, r], name = :outer) + sys_outer = System(eqs, t, systems = [F, sys_inner, r], name = :outer) # test first that the structural_simplify works correctly ssys = structural_simplify(sys_outer) @@ -192,7 +192,7 @@ end @named P_inner = FirstOrder(k = 1, T = 1) @named feedback = Feedback() @named ref = Step() - @named sys_inner = ODESystem( + @named sys_inner = System( [connect(P_inner.output, :y, feedback.input2) connect(feedback.output, :u, P_inner.input) connect(ref.output, :r, feedback.input1)], @@ -208,7 +208,7 @@ end Sinner = sminreal(ss(get_sensitivity(sys_inner, :u)[1]...)) - @named sys_inner = ODESystem( + @named sys_inner = System( [connect(P_inner.output, :y, feedback.input2) connect(feedback.output, :u, P_inner.input)], t, @@ -216,7 +216,7 @@ end @named P_outer = FirstOrder(k = rand(), T = rand()) - @named sys_outer = ODESystem( + @named sys_outer = System( [connect(sys_inner.P_inner.output, :y2, P_outer.input) connect(P_outer.output, :u2, sys_inner.feedback.input1)], t, @@ -249,7 +249,7 @@ end eqs = [connect(P.output, :plant_output, K.input) connect(K.output, :plant_input, P.input)] - sys = ODESystem(eqs, t, systems = [P, K], name = :hej) + sys = System(eqs, t, systems = [P, K], name = :hej) matrices, _ = get_sensitivity(sys, :plant_input) S = CS.feedback(I(2), Kss * Pss, pos_feedback = true) @@ -281,14 +281,14 @@ end connect(add.output, C.input) connect(C.output, :plant_input, P.input)] - sys_inner = ODESystem(eqs, t, systems = [P, C, add], name = :inner) + sys_inner = System(eqs, t, systems = [P, C, add], name = :inner) @named r = Constant(k = 1) @named F = FirstOrder(k = 1, T = 3) eqs = [connect(r.output, F.input) connect(F.output, sys_inner.add.input1)] - sys_outer = ODESystem(eqs, t, systems = [F, sys_inner, r], name = :outer) + sys_outer = System(eqs, t, systems = [F, sys_inner, r], name = :outer) matrices, _ = get_sensitivity( sys_outer, [sys_outer.inner.plant_input, sys_outer.inner.plant_output]) @@ -352,13 +352,13 @@ function normal_test_system() connect(F1.output, add.input1) connect(F2.output, add.input2) connect(add.output, back.input2)] - @named normal_inner = ODESystem(eqs_normal, t; systems = [F1, F2, add, back]) + @named normal_inner = System(eqs_normal, t; systems = [F1, F2, add, back]) @named step = Step() eqs2_normal = [ connect(step.output, normal_inner.back.input1) ] - @named sys_normal = ODESystem(eqs2_normal, t; systems = [normal_inner, step]) + @named sys_normal = System(eqs2_normal, t; systems = [normal_inner, step]) end sys_normal = normal_test_system() @@ -377,12 +377,12 @@ matrices_normal, _ = get_sensitivity(sys_normal, sys_normal.normal_inner.ap) connect(F1.output, add.input1) connect(F2.output, add.input2) connect(add.output, back.input2)] - @named inner = ODESystem(eqs, t; systems = [F1, F2, add, back]) + @named inner = System(eqs, t; systems = [F1, F2, add, back]) @named step = Step() eqs2 = [connect(step.output, inner.back.input1) connect(inner.back.output, :ap, inner.F1.input)] - @named sys = ODESystem(eqs2, t; systems = [inner, step]) + @named sys = System(eqs2, t; systems = [inner, step]) prob = ODEProblem(structural_simplify(sys), [], (0.0, 10.0)) @test SciMLBase.successful_retcode(solve(prob, Rodas5P())) @@ -401,12 +401,12 @@ end connect(F1.output, add.input1) connect(F2.output, add.input2) connect(add.output, back.input2)] - @named inner = ODESystem(eqs, t; systems = [F1, F2, add, back]) + @named inner = System(eqs, t; systems = [F1, F2, add, back]) @named step = Step() eqs2 = [connect(step.output, inner.back.input1) connect(inner.back.output.u, :ap, inner.F1.input.u)] - @named sys = ODESystem(eqs2, t; systems = [inner, step]) + @named sys = System(eqs2, t; systems = [inner, step]) prob = ODEProblem(structural_simplify(sys), [], (0.0, 10.0)) @test SciMLBase.successful_retcode(solve(prob, Rodas5P())) @@ -425,12 +425,12 @@ end connect(F1.output, add.input1) connect(F2.output, add.input2) connect(add.output, back.input2)] - @named inner = ODESystem(eqs, t; systems = [F1, F2, add, back]) + @named inner = System(eqs, t; systems = [F1, F2, add, back]) @named step = Step() eqs2 = [connect(step.output, inner.back.input1) connect(inner.back.output.u, :ap, inner.F1.input.u)] - @named sys = ODESystem(eqs2, t; systems = [inner, step]) + @named sys = System(eqs2, t; systems = [inner, step]) prob = ODEProblem(structural_simplify(sys), [], (0.0, 10.0)) @test SciMLBase.successful_retcode(solve(prob, Rodas5P())) @@ -459,10 +459,10 @@ end connect(inertia2.flange_a, spring.flange_b, damper.flange_b)] if u !== nothing push!(eqs, connect(torque.tau, u.output)) - return @named model = ODESystem( + return @named model = System( eqs, t; systems = [torque, inertia1, inertia2, spring, damper, u]) end - ODESystem(eqs, t; systems = [torque, inertia1, inertia2, spring, damper], name) + System(eqs, t; systems = [torque, inertia1, inertia2, spring, damper], name) end @named r = Step(start_time = 1) @@ -481,7 +481,7 @@ end connect(add.output, :u, model.torque.tau) # Name connection u to form an analysis point connect(model.inertia1.flange_b, sensor.flange) connect(sensor.phi, :y, pid.measurement)] - closed_loop = ODESystem(connections, t, + closed_loop = System(connections, t, systems = [model, inverse_model, pid, filt, sensor, inverse_sensor, r, add], name = :closed_loop) # just ensure the system simplifies diff --git a/test/downstream/inversemodel.jl b/test/downstream/inversemodel.jl index dc2ee380a2..0e4b1ea5eb 100644 --- a/test/downstream/inversemodel.jl +++ b/test/downstream/inversemodel.jl @@ -65,7 +65,7 @@ begin Fss = ss(Ftf) # Create an MTK-compatible constructor function RefFilter(; name) - sys = ODESystem(Fss; name) + sys = System(Fss; name) "Compute initial state that yields y0 as output" empty!(ModelingToolkit.get_defaults(sys)) return sys diff --git a/test/downstream/linearization_dd.jl b/test/downstream/linearization_dd.jl index c4076b6aad..3e94a02469 100644 --- a/test/downstream/linearization_dd.jl +++ b/test/downstream/linearization_dd.jl @@ -24,7 +24,7 @@ eqs = [connect(link1.TX1, cart.flange) connect(cart.flange, force.flange) connect(link1.TY1, fixed.flange)] -@named model = ODESystem(eqs, t, [], []; systems = [link1, cart, force, fixed]) +@named model = System(eqs, t, [], []; systems = [link1, cart, force, fixed]) lin_outputs = [cart.s, cart.v, link1.A, link1.dA] lin_inputs = [force.f.u] diff --git a/test/downstream/linearize.jl b/test/downstream/linearize.jl index 20a9d317e8..f86d7c937e 100644 --- a/test/downstream/linearize.jl +++ b/test/downstream/linearize.jl @@ -12,7 +12,7 @@ eqs = [u ~ kp * (r - y) D(x) ~ -x + u y ~ x] -@named sys = ODESystem(eqs, t) +@named sys = System(eqs, t) lsys, ssys = linearize(sys, [r], [y]) lprob = LinearizationProblem(sys, [r], [y]) @@ -56,7 +56,7 @@ function plant(; name) D = Differential(t) eqs = [D(x) ~ -x + u y ~ x] - ODESystem(eqs, t; name = name) + System(eqs, t; name = name) end function filt_(; name) @@ -65,7 +65,7 @@ function filt_(; name) D = Differential(t) eqs = [D(x) ~ -2 * x + u y ~ x] - ODESystem(eqs, t, name = name) + System(eqs, t, name = name) end function controller(kp; name) @@ -74,7 +74,7 @@ function controller(kp; name) eqs = [ u ~ kp * (r - y) ] - ODESystem(eqs, t; name = name) + System(eqs, t; name = name) end @named f = filt_() @@ -85,7 +85,7 @@ connections = [f.y ~ c.r # filtered reference to controller reference c.u ~ p.u # controller output to plant input p.y ~ c.y] -@named cl = ODESystem(connections, t, systems = [f, c, p]) +@named cl = System(connections, t, systems = [f, c, p]) lsys0, ssys = linearize(cl) desired_order = [f.x, p.x] @@ -181,7 +181,7 @@ function saturation(; y_max, y_min = y_max > 0 ? -y_max : -Inf, name) # The equation below is equivalent to y ~ clamp(u, y_min, y_max) y ~ ie(u > y_max, y_max, ie((y_min < u) & (u < y_max), u, y_min)) ] - ODESystem(eqs, t, name = name) + System(eqs, t, name = name) end @named sat = saturation(; y_max = 1) # inside the linear region, the function is identity @@ -228,7 +228,7 @@ function SystemModel(u = nothing; name = :model) connect(inertia2.flange_a, spring.flange_b, damper.flange_b)] if u !== nothing push!(eqs, connect(torque.tau, u.output)) - return ODESystem(eqs, t; + return System(eqs, t; systems = [ torque, inertia1, @@ -239,7 +239,7 @@ function SystemModel(u = nothing; name = :model) ], name) end - ODESystem(eqs, t; systems = [torque, inertia1, inertia2, spring, damper], name) + System(eqs, t; systems = [torque, inertia1, inertia2, spring, damper], name) end @named r = Step(start_time = 0) @@ -256,7 +256,7 @@ connections = [connect(r.output, :r, filt.input) connect(sensor.phi, :y, er.input2) connect(er.output, :e, pid.err_input)] -closed_loop = ODESystem(connections, t, systems = [model, pid, filt, sensor, r, er], +closed_loop = System(connections, t, systems = [model, pid, filt, sensor, r, er], name = :closed_loop, defaults = [ model.inertia1.phi => 0.0, model.inertia2.phi => 0.0, @@ -303,7 +303,7 @@ m_ss = 2.4000000003229878 @variables x(t) y(t) u(t)=1.0 @parameters p = 1.0 eqs = [D(x) ~ p * u, x ~ y] -@named sys = ODESystem(eqs, t) +@named sys = System(eqs, t) matrices1, _ = linearize(sys, [u], []; op = Dict(x => 2.0)) matrices2, _ = linearize(sys, [u], []; op = Dict(y => 2.0)) @@ -325,7 +325,7 @@ end @variables x(t) y(t) @parameters p eqs = [0 ~ x * log(y) - p] - @named sys = ODESystem(eqs, t; defaults = [p => 1.0]) + @named sys = System(eqs, t; defaults = [p => 1.0]) sys = complete(sys) @test_throws ModelingToolkit.MissingVariablesError linearize( sys, [x], []; op = Dict(x => 1.0), allow_input_derivatives = true) diff --git a/test/downstream/test_disturbance_model.jl b/test/downstream/test_disturbance_model.jl index 0e04200237..6da6c249a1 100644 --- a/test/downstream/test_disturbance_model.jl +++ b/test/downstream/test_disturbance_model.jl @@ -71,7 +71,7 @@ lsys = named_ss( # If we now want to add a disturbance model, we cannot do that since we have already connected a constant to the disturbance input, we thus create a new wrapper model with inputs s = tf("s") -dist(; name) = ODESystem(1 / s; name) +dist(; name) = System(1 / s; name) @mtkmodel SystemModelWithDisturbanceModel begin @components begin @@ -106,7 +106,7 @@ sol = solve(prob, Tsit5()) ## # Now we only have an integrating disturbance affecting inertia1, what if we want both integrating and direct Gaussian? We'd need a "PI controller" disturbancemodel. If we add the disturbance model (s+1)/s we get the integrating and non-integrating noises being correlated which is fine, it reduces the dimensions of the sigma point by 1. -dist3(; name) = ODESystem(ss(1 + 10 / s, balance = false); name) +dist3(; name) = System(ss(1 + 10 / s, balance = false); name) @mtkmodel SystemModelWithDisturbanceModel begin @components begin diff --git a/test/dq_units.jl b/test/dq_units.jl index 3c59c479c1..267e993971 100644 --- a/test/dq_units.jl +++ b/test/dq_units.jl @@ -17,63 +17,63 @@ using ModelingToolkit: t, D eqs = [D(E) ~ P - E / τ 0 ~ P] @test MT.validate(eqs) -@named sys = ODESystem(eqs, t) +@named sys = System(eqs, t) @test !MT.validate(D(D(E)) ~ P) @test !MT.validate(0 ~ P + E * τ) # Disabling unit validation/checks selectively -@test_throws MT.ArgumentError ODESystem(eqs, t, [E, P, t], [τ], name = :sys) -ODESystem(eqs, t, [E, P, t], [τ], name = :sys, checks = MT.CheckUnits) +@test_throws MT.ArgumentError System(eqs, t, [E, P, t], [τ], name = :sys) +System(eqs, t, [E, P, t], [τ], name = :sys, checks = MT.CheckUnits) eqs = [D(E) ~ P - E / τ 0 ~ P + E * τ] -@test_throws MT.ValidationError ODESystem(eqs, t, name = :sys, checks = MT.CheckAll) -@test_throws MT.ValidationError ODESystem(eqs, t, name = :sys, checks = true) -ODESystem(eqs, t, name = :sys, checks = MT.CheckNone) -ODESystem(eqs, t, name = :sys, checks = false) -@test_throws MT.ValidationError ODESystem(eqs, t, name = :sys, +@test_throws MT.ValidationError System(eqs, t, name = :sys, checks = MT.CheckAll) +@test_throws MT.ValidationError System(eqs, t, name = :sys, checks = true) +System(eqs, t, name = :sys, checks = MT.CheckNone) +System(eqs, t, name = :sys, checks = false) +@test_throws MT.ValidationError System(eqs, t, name = :sys, checks = MT.CheckComponents | MT.CheckUnits) -@named sys = ODESystem(eqs, t, checks = MT.CheckComponents) -@test_throws MT.ValidationError ODESystem(eqs, t, [E, P, t], [τ], name = :sys, +@named sys = System(eqs, t, checks = MT.CheckComponents) +@test_throws MT.ValidationError System(eqs, t, [E, P, t], [τ], name = :sys, checks = MT.CheckUnits) # connection validation @connector function Pin(; name) sts = @variables(v(t)=1.0, [unit = u"V"], i(t)=1.0, [unit = u"A", connect = Flow]) - ODESystem(Equation[], t, sts, []; name = name) + System(Equation[], t, sts, []; name = name) end @connector function OtherPin(; name) sts = @variables(v(t)=1.0, [unit = u"mV"], i(t)=1.0, [unit = u"mA", connect = Flow]) - ODESystem(Equation[], t, sts, []; name = name) + System(Equation[], t, sts, []; name = name) end @connector function LongPin(; name) sts = @variables(v(t)=1.0, [unit = u"V"], i(t)=1.0, [unit = u"A", connect = Flow], x(t)=1.0) - ODESystem(Equation[], t, sts, []; name = name) + System(Equation[], t, sts, []; name = name) end @named p1 = Pin() @named p2 = Pin() @named lp = LongPin() good_eqs = [connect(p1, p2)] @test MT.validate(good_eqs) -@named sys = ODESystem(good_eqs, t, [], []) +@named sys = System(good_eqs, t, [], []) @named op = OtherPin() bad_eqs = [connect(p1, op)] @test !MT.validate(bad_eqs) -@test_throws MT.ValidationError @named sys = ODESystem(bad_eqs, t, [], []) +@test_throws MT.ValidationError @named sys = System(bad_eqs, t, [], []) @named op2 = OtherPin() good_eqs = [connect(op, op2)] @test MT.validate(good_eqs) -@named sys = ODESystem(good_eqs, t, [], []) +@named sys = System(good_eqs, t, [], []) # Array variables @variables x(t)[1:3] [unit = u"m"] @parameters v[1:3]=[1, 2, 3] [unit = u"m/s"] eqs = D.(x) .~ v -ODESystem(eqs, t, name = :sys) +System(eqs, t, name = :sys) # Nonlinear system @parameters a [unit = u"kg"^-1] @@ -112,12 +112,12 @@ noiseeqs = [0.1us"W" 0.1us"W" @parameters v [unit = u"m/s"] r [unit = u"m"^3 / u"s"] eqs = [D(L) ~ v, V ~ L^3] -@named sys = ODESystem(eqs, t) +@named sys = System(eqs, t) sys_simple = structural_simplify(sys) eqs = [D(V) ~ r, V ~ L^3] -@named sys = ODESystem(eqs, t) +@named sys = System(eqs, t) sys_simple = structural_simplify(sys) @variables V [unit = u"m"^3] L [unit = u"m"] diff --git a/test/equation_type_accessors.jl b/test/equation_type_accessors.jl index f118784f44..1bf92743ac 100644 --- a/test/equation_type_accessors.jl +++ b/test/equation_type_accessors.jl @@ -44,9 +44,9 @@ eqs2 = [X + Y + c ~ b * X^(X + Z + a) eqs3 = [D(X) ~ sqrt(X + b) + sqrt(Z + c) 2Z * (Z + Y) ~ D(Y) * log(a) D(Z) + c * X ~ b / (X + Y^d) + D(Z)] -@named osys1 = ODESystem(eqs1, t) -@named osys2 = ODESystem(eqs2, t) -@named osys3 = ODESystem(eqs3, t) +@named osys1 = System(eqs1, t) +@named osys2 = System(eqs2, t) +@named osys3 = System(eqs3, t) # Test `has...` for non-composed systems. @test has_alg_equations(osys1) diff --git a/test/error_handling.jl b/test/error_handling.jl index d5605e3cbc..d6c0fa8caa 100644 --- a/test/error_handling.jl +++ b/test/error_handling.jl @@ -14,7 +14,7 @@ function UnderdefinedConstantVoltage(; name, V = 1.0) V ~ p.v - n.v # Remove equation # 0 ~ p.i + n.i ] - ODESystem(eqs, t, [], [V], systems = [p, n], defaults = Dict(V => val), name = name) + System(eqs, t, [], [V], systems = [p, n], defaults = Dict(V => val), name = name) end function OverdefinedConstantVoltage(; name, V = 1.0, I = 1.0) @@ -27,7 +27,7 @@ function OverdefinedConstantVoltage(; name, V = 1.0, I = 1.0) # Overdefine p.i and n.i n.i ~ I p.i ~ I] - ODESystem(eqs, t, [], [V], systems = [p, n], defaults = Dict(V => val, I => val2), + System(eqs, t, [], [V], systems = [p, n], defaults = Dict(V => val, I => val2), name = name) end @@ -42,7 +42,7 @@ rc_eqs = [connect(source.p, resistor.p) connect(resistor.n, capacitor.p) connect(capacitor.n, source.n)] -@named rc_model = ODESystem(rc_eqs, t, systems = [resistor, capacitor, source]) +@named rc_model = System(rc_eqs, t, systems = [resistor, capacitor, source]) @test_throws ModelingToolkit.ExtraVariablesSystemException structural_simplify(rc_model) @named source2 = OverdefinedConstantVoltage(V = V, I = V / R) @@ -50,5 +50,5 @@ rc_eqs2 = [connect(source2.p, resistor.p) connect(resistor.n, capacitor.p) connect(capacitor.n, source2.n)] -@named rc_model2 = ODESystem(rc_eqs2, t, systems = [resistor, capacitor, source2]) +@named rc_model2 = System(rc_eqs2, t, systems = [resistor, capacitor, source2]) @test_throws ModelingToolkit.ExtraEquationsSystemException structural_simplify(rc_model2) diff --git a/test/extensions/ad.jl b/test/extensions/ad.jl index 73b1710812..8764200fac 100644 --- a/test/extensions/ad.jl +++ b/test/extensions/ad.jl @@ -21,7 +21,7 @@ u0 = [x => zeros(3), ps = [p => zeros(3, 3), q => 1.0] tspan = (0.0, 10.0) -@mtkbuild sys = ODESystem(eqs, t) +@mtkbuild sys = System(eqs, t) prob = ODEProblem(sys, u0, tspan, ps) sol = solve(prob, Tsit5()) @@ -37,7 +37,7 @@ end @testset "Issue#2997" begin pars = @parameters y0 mh Tγ0 Th0 h ργ0 vars = @variables x(t) - @named sys = ODESystem([D(x) ~ y0], + @named sys = System([D(x) ~ y0], t, vars, pars; @@ -58,7 +58,7 @@ end end @parameters a b[1:3] c(t) d::Integer e[1:3] f[1:3, 1:3]::Int g::Vector{AbstractFloat} h::String -@named sys = ODESystem( +@named sys = System( Equation[], t, [], [a, b, c, d, e, f, g, h], continuous_events = [ModelingToolkit.SymbolicContinuousCallback( [a ~ 0] => [c ~ 0], discrete_parameters = c)]) @@ -112,7 +112,7 @@ fwd, back = ChainRulesCore.rrule(remake_buffer, sys, ps, idxs, vals) @variables y(t) eqs = [D(D(y)) ~ -9.81] initialization_eqs = [y^2 ~ 0] # initialize y = 0 in a way that builds an initialization problem - @named sys = ODESystem(eqs, t; initialization_eqs) + @named sys = System(eqs, t; initialization_eqs) sys = structural_simplify(sys) # Find initial throw velocity that reaches exactly 10 m after 1 s @@ -129,7 +129,7 @@ end @testset "`sys.var` is non-differentiable" begin @variables x(t) - @mtkbuild sys = ODESystem(D(x) ~ x, t) + @mtkbuild sys = System(D(x) ~ x, t) prob = ODEProblem(sys, [x => 1.0], (0.0, 1.0)) grad = Zygote.gradient(prob) do prob diff --git a/test/extensions/bifurcationkit.jl b/test/extensions/bifurcationkit.jl index 629edf46a6..227cd175e0 100644 --- a/test/extensions/bifurcationkit.jl +++ b/test/extensions/bifurcationkit.jl @@ -60,7 +60,7 @@ let @variables x(t) y(t) z(t) eqs = [D(x) ~ -x + a * y + x^2 * y, D(y) ~ b - a * y - x^2 * y] - @named sys = ODESystem(eqs, t) + @named sys = System(eqs, t) sys = complete(sys) # Creates BifurcationProblem bprob = BifurcationProblem(sys, diff --git a/test/extensions/dynamic_optimization.jl b/test/extensions/dynamic_optimization.jl index 79810d2476..1074cae620 100644 --- a/test/extensions/dynamic_optimization.jl +++ b/test/extensions/dynamic_optimization.jl @@ -20,7 +20,7 @@ const M = ModelingToolkit eqs = [D(x(t)) ~ α * x(t) - β * x(t) * y(t), D(y(t)) ~ -γ * y(t) + δ * x(t) * y(t)] - @mtkbuild sys = ODESystem(eqs, t) + @mtkbuild sys = System(eqs, t) tspan = (0.0, 1.0) u0map = [x(t) => 4.0, y(t) => 2.0] parammap = [α => 1.5, β => 1.0, γ => 3.0, δ => 1.0] @@ -53,7 +53,7 @@ const M = ModelingToolkit u0map = Pair[] guess = [x(t) => 4.0, y(t) => 2.0] constr = [x(0.6) ~ 3.5, x(0.3) ~ 7.0] - @mtkbuild lksys = ODESystem(eqs, t; constraints = constr) + @mtkbuild lksys = System(eqs, t; constraints = constr) jprob = JuMPDynamicOptProblem(lksys, u0map, tspan, parammap; guesses = guess, dt = 0.01) @test JuMP.num_constraints(jprob.model) == 2 @@ -77,7 +77,7 @@ const M = ModelingToolkit # Test whole-interval constraints constr = [x(t) ≳ 1, y(t) ≳ 1] - @mtkbuild lksys = ODESystem(eqs, t; constraints = constr) + @mtkbuild lksys = System(eqs, t; constraints = constr) iprob = InfiniteOptDynamicOptProblem( lksys, u0map, tspan, parammap; guesses = guess, dt = 0.01) isol = solve(iprob, Ipopt.Optimizer, @@ -116,7 +116,7 @@ end @variables u(..) [bounds = (-1.0, 1.0), input = true] constr = [v(1.0) ~ 0.0] cost = [-x(1.0)] # Maximize the final distance. - @named block = ODESystem( + @named block = System( [D(x(t)) ~ v(t), D(v(t)) ~ u(t)], t; costs = cost, constraints = constr) block = structural_simplify(block; inputs = [u(t)]) @@ -139,7 +139,7 @@ end # Test dynamics @parameters (u_interp::ConstantInterpolation)(..) - @mtkbuild block_ode = ODESystem([D(x(t)) ~ v(t), D(v(t)) ~ u_interp(t)], t) + @mtkbuild block_ode = System([D(x(t)) ~ v(t), D(v(t)) ~ u_interp(t)], t) spline = ctrl_to_spline(jsol.input_sol, ConstantInterpolation) oprob = ODEProblem(block_ode, u0map, tspan, [u_interp => spline]) osol = solve(oprob, Vern8(), dt = 0.01, adaptive = false) @@ -165,7 +165,7 @@ end D(q(t)) ~ -ν * q(t) + c * (1 - α) * s * w(t)] costs = [-q(tspan[2])] - @named beesys = ODESystem(eqs, t; costs) + @named beesys = System(eqs, t; costs) beesys = structural_simplify(beesys; inputs = [α]) u0map = [w(t) => 40, q(t) => 2] pmap = [b => 1, c => 1, μ => 1, s => 1, ν => 1, α => 1] @@ -183,7 +183,7 @@ end @parameters (α_interp::LinearInterpolation)(..) eqs = [D(w(t)) ~ -μ * w(t) + b * s * α_interp(t) * w(t), D(q(t)) ~ -ν * q(t) + c * (1 - α_interp(t)) * s * w(t)] - @mtkbuild beesys_ode = ODESystem(eqs, t) + @mtkbuild beesys_ode = System(eqs, t) oprob = ODEProblem(beesys_ode, u0map, tspan, @@ -212,7 +212,7 @@ end (ts, te) = (0.0, 0.2) costs = [-h(te)] cons = [T(te) ~ 0, m(te) ~ m_c] - @named rocket = ODESystem(eqs, t; costs, constraints = cons) + @named rocket = System(eqs, t; costs, constraints = cons) rocket = structural_simplify(rocket; inputs = [T(t)]) u0map = [h(t) => h₀, m(t) => m₀, v(t) => 0] @@ -237,7 +237,7 @@ end eqs = [D(h(t)) ~ v(t), D(v(t)) ~ (T_interp(t) - drag(h(t), v(t))) / m(t) - gravity(h(t)), D(m(t)) ~ -T_interp(t) / c] - @mtkbuild rocket_ode = ODESystem(eqs, t) + @mtkbuild rocket_ode = System(eqs, t) interpmap = Dict(T_interp => ctrl_to_spline(jsol.input_sol, CubicSpline)) oprob = ODEProblem(rocket_ode, u0map, (ts, te), merge(Dict(pmap), interpmap)) osol = solve(oprob, RadauIIA5(); adaptive = false, dt = 0.001) @@ -260,7 +260,7 @@ end # Integral cost function costs = [-Symbolics.Integral(t in (0, tf))(x(t) - u(t)), -x(tf)] consolidate(u) = u[1] + u[2] - @named rocket = ODESystem(eqs, t; costs, consolidate) + @named rocket = System(eqs, t; costs, consolidate) rocket = structural_simplify(rocket; inputs = [u(t)]) u0map = [x(t) => 17.5] @@ -283,7 +283,7 @@ end constr = [v(tf) ~ 0, x(tf) ~ 0] cost = [tf] # Minimize time - @named block = ODESystem( + @named block = System( [D(x(t)) ~ v(t), D(v(t)) ~ u(t)], t; costs = cost, constraints = constr) block = structural_simplify(block, inputs = [u(t)]) @@ -327,7 +327,7 @@ end costs = [Symbolics.Integral(t in (0, tf))(u^2)] tspan = (0, tf) - @named cartpole = ODESystem(eqs, t; costs, constraints = cons) + @named cartpole = System(eqs, t; costs, constraints = cons) cartpole = structural_simplify(cartpole; inputs = [u]) u0map = [D(x(t)) => 0.0, D(θ(t)) => 0.0, θ(t) => 0.0, x(t) => 0.0] diff --git a/test/fmi/fmi.jl b/test/fmi/fmi.jl index edbbb312d6..b2684b4524 100644 --- a/test/fmi/fmi.jl +++ b/test/fmi/fmi.jl @@ -34,7 +34,7 @@ const FMU_DIR = joinpath(@__DIR__, "fmus") @named inner = MTK.FMIComponent( Val(2); fmu, communication_step_size = 1e-5, type = :CS) @variables x(t) = 1.0 - @mtkbuild sys = ODESystem([D(x) ~ x], t; systems = [inner]) + @mtkbuild sys = System([D(x) ~ x], t; systems = [inner]) test_no_inputs_outputs(sys) prob = ODEProblem{true, SciMLBase.FullSpecialize}( @@ -68,7 +68,7 @@ const FMU_DIR = joinpath(@__DIR__, "fmus") @named inner = MTK.FMIComponent( Val(3); fmu, communication_step_size = 1e-5, type = :CS) @variables x(t) = 1.0 - @mtkbuild sys = ODESystem([D(x) ~ x], t; systems = [inner]) + @mtkbuild sys = System([D(x) ~ x], t; systems = [inner]) test_no_inputs_outputs(sys) prob = ODEProblem{true, SciMLBase.FullSpecialize}( @@ -120,7 +120,7 @@ end @testset "IO Model" begin function build_simple_adder(adder) @variables a(t) b(t) c(t) [guess = 1.0] - @mtkbuild sys = ODESystem( + @mtkbuild sys = System( [adder.a ~ a, adder.b ~ b, D(a) ~ t, D(b) ~ adder.out + adder.c, c^2 ~ adder.out + adder.value], t; @@ -176,7 +176,7 @@ end function build_sspace_model(sspace) @variables u(t)=1.0 x(t)=1.0 y(t) [guess = 1.0] - @mtkbuild sys = ODESystem( + @mtkbuild sys = System( [sspace.u ~ u, D(u) ~ t, D(x) ~ sspace.x + sspace.y, y^2 ~ sspace.y + sspace.x], t; systems = [sspace] ) @@ -229,7 +229,7 @@ end @testset "FMUs in a loop" begin function build_looped_adders(adder1, adder2) @variables x(t) = 1 - @mtkbuild sys = ODESystem( + @mtkbuild sys = System( [D(x) ~ x, adder1.a ~ adder2.out2, adder2.a ~ adder1.out2, adder1.b ~ 1.0, adder2.b ~ 2.0], t; @@ -274,7 +274,7 @@ end function build_looped_sspace(sspace1, sspace2) @variables x(t) = 1 - @mtkbuild sys = ODESystem([D(x) ~ x, sspace1.u ~ sspace2.x, sspace2.u ~ sspace1.y], + @mtkbuild sys = System([D(x) ~ x, sspace1.u ~ sspace2.x, sspace2.u ~ sspace1.y], t; systems = [sspace1, sspace2]) prob = ODEProblem(sys, [sspace1.x => 1.0, sspace2.x => 1.0], (0.0, 1.0)) return sys, prob diff --git a/test/funcaffect.jl b/test/funcaffect.jl index 1e7c66f39a..8e73280eb3 100644 --- a/test/funcaffect.jl +++ b/test/funcaffect.jl @@ -10,7 +10,7 @@ eqs = [D(u) ~ -u] affect1!(integ, u, p, ctx) = integ.u[u.u] += 10 -@named sys = ODESystem(eqs, t, [u], [], +@named sys = System(eqs, t, [u], [], discrete_events = [[4.0] => (affect1!, [u], [], [], nothing)]) prob = ODEProblem(complete(sys), [u => 10.0], (0, 10.0)) sol = solve(prob, Tsit5()) @@ -37,7 +37,7 @@ cb1 = ModelingToolkit.SymbolicContinuousCallback([t ~ zr], (affect1!, [], [], [] @test hash(cb) == hash(cb1) # named tuple -sys1 = ODESystem(eqs, t, [u], [], name = :sys, +sys1 = System(eqs, t, [u], [], name = :sys, discrete_events = [ [4.0] => (f = affect1!, sts = [u], pars = [], discretes = [], ctx = nothing) ]) @@ -50,7 +50,7 @@ de = de[1] @test ModelingToolkit.conditions(de) == [4.0] @test ModelingToolkit.has_functional_affect(de) -sys2 = ODESystem(eqs, t, [u], [], name = :sys, +sys2 = System(eqs, t, [u], [], name = :sys, discrete_events = [[4.0] => [u ~ -u * h]]) @test !ModelingToolkit.has_functional_affect(ModelingToolkit.get_discrete_events(sys2)[1]) @@ -60,7 +60,7 @@ function affect2!(integ, u, p, ctx) ctx[1] *= 2 end ctx1 = [10.0] -@named sys = ODESystem(eqs, t, [u], [], +@named sys = System(eqs, t, [u], [], discrete_events = [[4.0, 8.0] => (affect2!, [u], [], [], ctx1)]) prob = ODEProblem(complete(sys), [u => 10.0], (0, 10.0)) sol = solve(prob, Tsit5()) @@ -77,7 +77,7 @@ function affect3!(integ, u, p, ctx) end @parameters a = 10.0 -@named sys = ODESystem(eqs, t, [u], [a], +@named sys = System(eqs, t, [u], [a], discrete_events = [[4.0, 8.0] => (affect3!, [u], [a], [a], nothing)]) prob = ODEProblem(complete(sys), [u => 10.0], (0, 10.0)) @@ -93,7 +93,7 @@ function affect3!(integ, u, p, ctx) integ.ps[p.b] *= 2 end -@named sys = ODESystem(eqs, t, [u], [a], +@named sys = System(eqs, t, [u], [a], discrete_events = [ [4.0, 8.0] => (affect3!, [u], [a => :b], [a], nothing) ]) @@ -107,18 +107,18 @@ i8 = findfirst(==(8.0), sol[:t]) # same name @variables v(t) -@test_throws ErrorException ODESystem(eqs, t, [u], [a], +@test_throws ErrorException System(eqs, t, [u], [a], discrete_events = [ [4.0, 8.0] => (affect3!, [u, v => :u], [a], [a], nothing) ]; name = :sys) -@test_nowarn ODESystem(eqs, t, [u], [a], +@test_nowarn System(eqs, t, [u], [a], discrete_events = [ [4.0, 8.0] => (affect3!, [u], [a => :u], [a], nothing) ]; name = :sys) -@named resistor = ODESystem(D(v) ~ v, t, [v], []) +@named resistor = System(D(v) ~ v, t, [v], []) # nested namespace ctx = [0] @@ -127,7 +127,7 @@ function affect4!(integ, u, p, ctx) @test u.resistor₊v == 1 end s1 = compose( - ODESystem(Equation[], t, [], [], name = :s1, + System(Equation[], t, [], [], name = :s1, discrete_events = 1.0 => (affect4!, [resistor.v], [], [], ctx)), resistor) s2 = structural_simplify(s1) @@ -143,7 +143,7 @@ function affect5!(integ, u, p, ctx) end @unpack capacitor = rc_model -@named event_sys = ODESystem(Equation[], t; +@named event_sys = System(Equation[], t; continuous_events = [ [capacitor.v ~ 0.3] => (affect5!, [capacitor.v], [capacitor.C => :C], [capacitor.C], nothing) @@ -175,7 +175,7 @@ function Capacitor2(; name, C = 1.0) D(v) ~ i / C ] extend( - ODESystem(eqs, t, [], ps; name = name, + System(eqs, t, [], ps; name = name, continuous_events = [[v ~ 0.3] => (affect6!, [v], [C], [C], nothing)]), oneport) end @@ -195,7 +195,7 @@ rc_eqs2 = [connect(shape.output, source.V) connect(capacitor2.n, source.n) connect(capacitor2.n, ground.g)] -@named rc_model2 = ODESystem(rc_eqs2, t) +@named rc_model2 = System(rc_eqs2, t) rc_model2 = compose(rc_model2, [resistor, capacitor2, shape, source, ground]) sys2 = structural_simplify(rc_model2) @@ -223,14 +223,14 @@ function Ball(; name, g = 9.8, anti_gravity_time = 1.0) pars = @parameters g = g sts = @variables x(t), v(t) eqs = [D(x) ~ v, D(v) ~ g] - ODESystem(eqs, t, sts, pars; name = name, + System(eqs, t, sts, pars; name = name, discrete_events = [[anti_gravity_time] => (affect7!, [], [g], [g], a7_ctx)]) end @named ball1 = Ball(anti_gravity_time = 1.0) @named ball2 = Ball(anti_gravity_time = 2.0) -@named balls = ODESystem(Equation[], t) +@named balls = System(Equation[], t) balls = compose(balls, [ball1, ball2]) @test ModelingToolkit.has_discrete_events(balls) @@ -282,7 +282,7 @@ function bb_affect!(integ, u, p, ctx) integ.u[u.v] = -integ.u[u.v] end -@named bb_model = ODESystem(bb_eqs, t, sts, par, +@named bb_model = System(bb_eqs, t, sts, par, continuous_events = [ [y ~ zr] => (bb_affect!, [v], [], [], nothing) ]) diff --git a/test/function_registration.jl b/test/function_registration.jl index a1d9041127..7ab9835433 100644 --- a/test/function_registration.jl +++ b/test/function_registration.jl @@ -17,7 +17,7 @@ end @register_symbolic do_something(a) eq = Dt(u) ~ do_something(x) + MyModule.do_something(x) -@named sys = ODESystem([eq], t, [u], [x]) +@named sys = System([eq], t, [u], [x]) sys = complete(sys) fun = ODEFunction(sys) @@ -40,7 +40,7 @@ end @register_symbolic do_something_2(a) eq = Dt(u) ~ do_something_2(x) + MyNestedModule.do_something_2(x) -@named sys = ODESystem([eq], t, [u], [x]) +@named sys = System([eq], t, [u], [x]) sys = complete(sys) fun = ODEFunction(sys) @@ -62,7 +62,7 @@ end @register_symbolic do_something_3(a) eq = Dt(u) ~ do_something_3(x) + (@__MODULE__).do_something_3(x) -@named sys = ODESystem([eq], t, [u], [x]) +@named sys = System([eq], t, [u], [x]) sys = complete(sys) fun = ODEFunction(sys) @@ -99,7 +99,7 @@ function build_ode() @parameters x @variables u(t) eq = Dt(u) ~ do_something_4(x) + (@__MODULE__).do_something_4(x) - @named sys = ODESystem([eq], t, [u], [x]) + @named sys = System([eq], t, [u], [x]) sys = complete(sys) fun = ODEFunction(sys, eval_expression = false) end diff --git a/test/guess_propagation.jl b/test/guess_propagation.jl index 738e930adc..f2131e28cb 100644 --- a/test/guess_propagation.jl +++ b/test/guess_propagation.jl @@ -10,7 +10,7 @@ eqs = [D(x) ~ 1 x ~ y] initialization_eqs = [1 ~ exp(1 + x)] -@named sys = ODESystem(eqs, t; initialization_eqs) +@named sys = System(eqs, t; initialization_eqs) sys = complete(structural_simplify(sys)) tspan = (0.0, 0.2) prob = ODEProblem(sys, [], tspan, []) @@ -27,7 +27,7 @@ eqs = [D(x) ~ 1 x ~ y] initialization_eqs = [1 ~ exp(1 + x)] -@named sys = ODESystem(eqs, t; initialization_eqs) +@named sys = System(eqs, t; initialization_eqs) sys = complete(structural_simplify(sys)) tspan = (0.0, 0.2) prob = ODEProblem(sys, [], tspan, []) @@ -45,7 +45,7 @@ eqs = [D(x) ~ a] initialization_eqs = [1 ~ exp(1 + x)] -@named sys = ODESystem(eqs, t; initialization_eqs) +@named sys = System(eqs, t; initialization_eqs) sys = complete(structural_simplify(sys)) tspan = (0.0, 0.2) @@ -65,7 +65,7 @@ eqs = [D(x) ~ a, initialization_eqs = [1 ~ exp(1 + x)] -@named sys = ODESystem(eqs, t; initialization_eqs) +@named sys = System(eqs, t; initialization_eqs) sys = complete(structural_simplify(sys)) tspan = (0.0, 0.2) @@ -80,7 +80,7 @@ sol = solve(prob.f.initializeprob; show_trace = Val(true)) @parameters x0 @variables x(t) @variables y(t) = x -@mtkbuild sys = ODESystem([x ~ x0, D(y) ~ x], t) +@mtkbuild sys = System([x ~ x0, D(y) ~ x], t) prob = ODEProblem(sys, [], (0.0, 1.0), [x0 => 1.0]) @test prob[x] == 1.0 @test prob[y] == 1.0 @@ -88,7 +88,7 @@ prob = ODEProblem(sys, [], (0.0, 1.0), [x0 => 1.0]) @parameters x0 @variables x(t) @variables y(t) = x0 -@mtkbuild sys = ODESystem([x ~ x0, D(y) ~ x], t) +@mtkbuild sys = System([x ~ x0, D(y) ~ x], t) prob = ODEProblem(sys, [], (0.0, 1.0), [x0 => 1.0]) @test prob[x] == 1.0 @test prob[y] == 1.0 @@ -96,7 +96,7 @@ prob = ODEProblem(sys, [], (0.0, 1.0), [x0 => 1.0]) @parameters x0 @variables x(t) @variables y(t) = x0 -@mtkbuild sys = ODESystem([x ~ y, D(y) ~ x], t) +@mtkbuild sys = System([x ~ y, D(y) ~ x], t) prob = ODEProblem(sys, [], (0.0, 1.0), [x0 => 1.0]) @test prob[x] == 1.0 @test prob[y] == 1.0 @@ -104,7 +104,7 @@ prob = ODEProblem(sys, [], (0.0, 1.0), [x0 => 1.0]) @parameters x0 @variables x(t) = x0 @variables y(t) = x -@mtkbuild sys = ODESystem([x ~ y, D(y) ~ x], t) +@mtkbuild sys = System([x ~ y, D(y) ~ x], t) prob = ODEProblem(sys, [], (0.0, 1.0), [x0 => 1.0]) @test prob[x] == 1.0 @test prob[y] == 1.0 diff --git a/test/hierarchical_initialization_eqs.jl b/test/hierarchical_initialization_eqs.jl index 1e3109a66e..e9ea36947e 100644 --- a/test/hierarchical_initialization_eqs.jl +++ b/test/hierarchical_initialization_eqs.jl @@ -24,7 +24,7 @@ A simple linear resistor model p.i + n.i ~ 0 # Ohm's Law v ~ i * R] - return ODESystem(eqs, t, vars, params; systems, name) + return System(eqs, t, vars, params; systems, name) end @connector Pin begin v(t) @@ -46,7 +46,7 @@ end i ~ p.i p.i + n.i ~ 0 v ~ V] - return ODESystem(eqs, t, vars, params; systems, name) + return System(eqs, t, vars, params; systems, name) end @component function Capacitor(; name, C = 1.0) @@ -68,7 +68,7 @@ end i ~ p.i p.i + n.i ~ 0 C * D(v) ~ i] - return ODESystem(eqs, t, vars, params; systems, name, initialization_eqs) + return System(eqs, t, vars, params; systems, name, initialization_eqs) end @component function Ground(; name) @@ -78,7 +78,7 @@ end eqs = [ g.v ~ 0 ] - return ODESystem(eqs, t, [], []; systems, name) + return System(eqs, t, [], []; systems, name) end @component function Inductor(; name, L = 1.0) @@ -97,7 +97,7 @@ end i ~ p.i p.i + n.i ~ 0 L * D(i) ~ v] - return ODESystem(eqs, t, vars, params; systems, name) + return System(eqs, t, vars, params; systems, name) end """ @@ -118,7 +118,7 @@ HTML as well. eqs = [connect(source.p, inductor.n) connect(inductor.p, resistor.p, capacitor.p) connect(resistor.n, ground.g, capacitor.n, source.n)] - return ODESystem(eqs, t, [], []; systems, name, initialization_eqs) + return System(eqs, t, [], []; systems, name, initialization_eqs) end """Run model RLCModel from 0 to 10""" function simple() diff --git a/test/index_cache.jl b/test/index_cache.jl index 455203d759..3048084d96 100644 --- a/test/index_cache.jl +++ b/test/index_cache.jl @@ -3,9 +3,9 @@ using ModelingToolkit: t_nounits as t # Ensure indexes of array symbolics are cached appropriately @variables x(t)[1:2] -@named sys = ODESystem(Equation[], t, [x], []) +@named sys = System(Equation[], t, [x], []) sys1 = complete(sys) -@named sys = ODESystem(Equation[], t, [x...], []) +@named sys = System(Equation[], t, [x...], []) sys2 = complete(sys) for sys in [sys1, sys2] for (sym, idx) in [(x, 1:2), (x[1], 1), (x[2], 2)] @@ -15,9 +15,9 @@ for sys in [sys1, sys2] end @variables x(t)[1:2, 1:2] -@named sys = ODESystem(Equation[], t, [x], []) +@named sys = System(Equation[], t, [x], []) sys1 = complete(sys) -@named sys = ODESystem(Equation[], t, [x...], []) +@named sys = System(Equation[], t, [x...], []) sys2 = complete(sys) for sys in [sys1, sys2] @test is_variable(sys, x) @@ -32,7 +32,7 @@ end @parameters p1 p2[1:2] p3::String @variables x(t) y(t)[1:2] z(t) -@named sys = ODESystem(Equation[], t, [x, y, z], [p1, p2, p3]) +@named sys = System(Equation[], t, [x, y, z], [p1, p2, p3]) sys = complete(sys) ic = ModelingToolkit.get_index_cache(sys) @@ -46,7 +46,7 @@ ic = ModelingToolkit.get_index_cache(sys) @testset "tunable_parameters is ordered" begin @parameters p q[1:3] r[1:2, 1:2] s [tunable = false] - @named sys = ODESystem(Equation[], t, [], [p, q, r, s]) + @named sys = System(Equation[], t, [], [p, q, r, s]) sys = complete(sys) @test all(splat(isequal), zip(tunable_parameters(sys), parameters(sys)[1:3])) @@ -65,7 +65,7 @@ end @testset "reorder_dimension_by_tunables" begin @parameters p q[1:3] r[1:2, 1:2] s [tunable = false] - @named sys = ODESystem(Equation[], t, [], [p, q, r, s]) + @named sys = System(Equation[], t, [], [p, q, r, s]) src = ones(8) dst = zeros(8) # system must be complete... @@ -108,7 +108,7 @@ end event1 = [1.0, 2, 3] => (update_affect!, [], [p_1], [p_1], nothing) - @named sys = ODESystem([ + @named sys = System([ ModelingToolkit.D_nounits(x) ~ p_1(x) ], ModelingToolkit.t_nounits; diff --git a/test/initial_values.jl b/test/initial_values.jl index 0ed8f7bffe..c030555b82 100644 --- a/test/initial_values.jl +++ b/test/initial_values.jl @@ -7,13 +7,13 @@ using SymbolicIndexingInterface @variables x(t)[1:3]=[1.0, 2.0, 3.0] y(t) z(t)[1:2] -@mtkbuild sys=ODESystem([D(x) ~ t * x], t) simplify=false +@mtkbuild sys=System([D(x) ~ t * x], t) simplify=false @test get_u0(sys, [])[1] == [1.0, 2.0, 3.0] @test get_u0(sys, [x => [2.0, 3.0, 4.0]])[1] == [2.0, 3.0, 4.0] @test get_u0(sys, [x[1] => 2.0, x[2] => 3.0, x[3] => 4.0])[1] == [2.0, 3.0, 4.0] @test get_u0(sys, [2.0, 3.0, 4.0])[1] == [2.0, 3.0, 4.0] -@mtkbuild sys=ODESystem([ +@mtkbuild sys=System([ D(x) ~ 3x, D(y) ~ t, D(z[1]) ~ z[2] + t, @@ -56,7 +56,7 @@ vals = ModelingToolkit.varmap_to_vars(var_vals, desired_values; defaults = defau @parameters k1 k2 Γ[1:1]=X1 + X2 eq = D(X1) ~ -k1 * X1 + k2 * (-X1 + Γ[1]) obs = X2 ~ Γ[1] - X1 -@mtkbuild osys_m = ODESystem([eq], t, [X1], [k1, k2, Γ[1]]; observed = [X2 ~ Γ[1] - X1]) +@mtkbuild osys_m = System([eq], t, [X1], [k1, k2, Γ[1]]; observed = [X2 ~ Γ[1] - X1]) # Creates ODEProblem. u0 = [X1 => 1.0, X2 => 2.0] @@ -77,14 +77,14 @@ target_varmap = Dict(p => ones(3), q => 2ones(3), q[1] => 2.0, q[2] => 2.0, q[3] # Issue#1283 @variables z(t)[1:2, 1:2] eqs = [D(D(z)) ~ ones(2, 2)] -@mtkbuild sys = ODESystem(eqs, t) +@mtkbuild sys = System(eqs, t) @test_nowarn ODEProblem(sys, [z => zeros(2, 2), D(z) => ones(2, 2)], (0.0, 10.0)) # Initialization with defaults involving parameters that are not part of the system # Issue#2817 @parameters A1 A2 B1 B2 @variables x1(t) x2(t) -@mtkbuild sys = ODESystem( +@mtkbuild sys = System( [ x1 ~ B1, x2 ~ B2 @@ -101,7 +101,7 @@ prob = ODEProblem(sys, [], (0.0, 1.0), [A1 => 0.3]) @parameters p = nothing @variables x(t)=nothing y(t) for sys in [ - ODESystem(Equation[], t, [x, y], [p]; defaults = [y => nothing], name = :osys), + System(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), @@ -118,14 +118,14 @@ end # Issue#2799 @variables x(t) @parameters p -@mtkbuild sys = ODESystem([D(x) ~ p], t; defaults = [x => t, p => 2t]) +@mtkbuild sys = System([D(x) ~ p], t; defaults = [x => t, p => 2t]) prob = ODEProblem(sys, [], (1.0, 2.0), []) @test prob[x] == 1.0 @test prob.ps[p] == 2.0 @testset "Array of symbolics is unwrapped" begin @variables x(t)[1:2] y(t) - @mtkbuild sys = ODESystem([D(x) ~ x, D(y) ~ t], t; defaults = [x => [y, 3.0]]) + @mtkbuild sys = System([D(x) ~ x, D(y) ~ t], t; defaults = [x => [y, 3.0]]) prob = ODEProblem(sys, [y => 1.0], (0.0, 1.0)) @test eltype(prob.u0) <: Float64 prob = ODEProblem(sys, [x => [y, 4.0], y => 2.0], (0.0, 1.0)) @@ -135,7 +135,7 @@ end @testset "split=false systems with all parameter defaults" begin @variables x(t) = 1.0 @parameters p=1.0 q=2.0 r=3.0 - @mtkbuild sys=ODESystem(D(x) ~ p * x + q * t + r, t) split=false + @mtkbuild sys=System(D(x) ~ p * x + q * t + r, t) split=false prob = @test_nowarn ODEProblem(sys, [], (0.0, 1.0)) @test prob.p isa Vector{Float64} end @@ -147,7 +147,7 @@ end y ~ ifelse(t < c1, 0.0, (-c1 + t)^(c3))] sps = [x, y] ps = [c1, c2, c3] - @mtkbuild osys = ODESystem(eqs, t, sps, ps) + @mtkbuild osys = System(eqs, t, sps, ps) u0map = [x => 1.0] pmap = [c1 => 5.0, c2 => 1.0, c3 => 1.2] oprob = ODEProblem(osys, u0map, (0.0, 10.0), pmap) @@ -155,7 +155,7 @@ end @testset "Cyclic dependency checking and substitution limits" begin @variables x(t) y(t) - @mtkbuild sys = ODESystem( + @mtkbuild sys = System( [D(x) ~ x, D(y) ~ y], t; initialization_eqs = [x ~ 2y + 3, y ~ 2x], guesses = [x => 2y, y => 2x]) @test_warn ["Cycle", "unknowns", "x", "y"] try @@ -166,7 +166,7 @@ end sys, [x => 2y + 1, y => 2x], (0.0, 1.0); build_initializeprob = false) @parameters p q - @mtkbuild sys = ODESystem( + @mtkbuild sys = System( [D(x) ~ x * p, D(y) ~ y * q], t; guesses = [p => 1.0, q => 2.0]) # "unknowns" because they are initialization unknowns @test_warn ["Cycle", "unknowns", "p", "q"] try @@ -181,7 +181,7 @@ end @testset "`add_fallbacks!` checks scalarized array parameters correctly" begin @variables x(t)[1:2] @parameters p[1:2, 1:2] - @mtkbuild sys = ODESystem(D(x) ~ p * x, t) + @mtkbuild sys = System(D(x) ~ p * x, t) # used to throw a `MethodError` complaining about `getindex(::Nothing, ::CartesianIndex{2})` @test_throws ModelingToolkit.MissingParametersError ODEProblem( sys, [x => ones(2)], (0.0, 1.0)) @@ -195,7 +195,7 @@ end y[1] ~ x[3], y[2] ~ x[4] ] - @mtkbuild sys = ODESystem(eqs, t; defaults = [x => vcat(ones(2), y), y => x[1:2] ./ 2]) + @mtkbuild sys = System(eqs, t; defaults = [x => vcat(ones(2), y), y => x[1:2] ./ 2]) prob = ODEProblem(sys, [], (0.0, 1.0)) sol = solve(prob) @test SciMLBase.successful_retcode(sol) @@ -208,7 +208,7 @@ end eqs = [D(D(x)) ~ λ * x D(D(y)) ~ λ * y - g x^2 + y^2 ~ 1] - @mtkbuild pend = ODESystem(eqs, t) + @mtkbuild pend = System(eqs, t) @test_throws ModelingToolkit.MissingGuessError ODEProblem( pend, [x => 1], (0, 1), [g => 1], guesses = [y => λ, λ => y + 1]) @@ -217,7 +217,7 @@ end # 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) + @mtkbuild sys = System(eqs, t) @test_throws ["a(t)", "c(t)"] ODEProblem( sys, [e => 2, a => b, b => a + 1, c => d, d => c + 1], (0, 1)) end @@ -229,7 +229,7 @@ end @variables x(t) @parameters (interp::Tspline)(..) - @mtkbuild sys = ODESystem(D(x) ~ interp(t), t) + @mtkbuild sys = System(D(x) ~ interp(t), t) prob = ODEProblem(sys, [x => 0.0], (0.0, 1.0), [interp => spline]) spline2 = LinearInterpolation(ts .^ 2, ts .^ 2) @@ -258,7 +258,7 @@ end @parameters p d @variables X(t) eqs = [D(X) ~ p - d * X] - @mtkbuild osys = ODESystem(eqs, t) + @mtkbuild osys = System(eqs, t) u0 = [X => 1.0f0] ps = [p => 1.0f0, d => 2.0f0] oprob = ODEProblem(osys, u0, (0.0f0, 1.0f0), ps) @@ -270,7 +270,7 @@ end @testset "Array initials and scalar parameters with `split = false`" begin @variables x(t)[1:2] @parameters p - @mtkbuild sys=ODESystem([D(x[1]) ~ x[1], D(x[2]) ~ x[2] + p], t) split=false + @mtkbuild sys=System([D(x[1]) ~ x[1], D(x[2]) ~ x[2] + p], t) split=false ps = Set(parameters(sys; initial_parameters = true)) @test length(ps) == 5 for i in 1:2 @@ -294,7 +294,7 @@ end 0 ~ x^2 + y^2 - w2^2 ] - @mtkbuild sys = ODESystem(eqs, t) + @mtkbuild sys = System(eqs, t) u0 = [D(x) => 2.0, x => 1.0, @@ -319,7 +319,7 @@ end D(y) ~ x * (ρ - z) - y, D(z) ~ x * y - β * z] - @mtkbuild sys = ODESystem(eqs, t) + @mtkbuild sys = System(eqs, t) u0 = SA[D(x) => 2.0f0, x => 1.0f0, @@ -342,7 +342,7 @@ end eqs = [D(D(x)) ~ λ * x D(D(y)) ~ λ * y - g x^2 + y^2 ~ 1] - @mtkbuild pend = ODESystem(eqs, t) + @mtkbuild pend = System(eqs, t) u0 = [x => 1.0, D(x) => 0.0] u0_constructor = p_constructor = vals -> SVector{length(vals)}(vals...) @@ -355,7 +355,7 @@ end @test state_values(initdata.initializeprob) isa SVector @test parameter_values(initdata.initializeprob).tunable isa SVector - @mtkbuild pend=ODESystem(eqs, t) split=false + @mtkbuild pend=System(eqs, t) split=false prob = ODEProblem(pend, u0, tspan; u0_constructor, p_constructor) @test prob.p isa SVector initdata = prob.f.initialization_data diff --git a/test/initializationsystem.jl b/test/initializationsystem.jl index 2cd8499e69..22fd6faf88 100644 --- a/test/initializationsystem.jl +++ b/test/initializationsystem.jl @@ -11,7 +11,7 @@ using DynamicQuantities eqs = [D(D(x)) ~ λ * x D(D(y)) ~ λ * y - g x^2 + y^2 ~ 1] -@mtkbuild pend = ODESystem(eqs, t) +@mtkbuild pend = System(eqs, t) initprob = ModelingToolkit.InitializationProblem(pend, 0.0, [], [g => 1]; guesses = [ModelingToolkit.missing_variable_defaults(pend); x => 1; y => 0.2]) @@ -348,7 +348,7 @@ eqs = [D(D(x)) ~ σ * (y - x), D(y) ~ x * (ρ - z) - y, D(z) ~ x * y - β * z] -@mtkbuild sys = ODESystem(eqs, t) +@mtkbuild sys = System(eqs, t) u0 = [D(x) => 2.0, y => 0.0, @@ -375,7 +375,7 @@ function System2(; name) end eqs = [D(dx) ~ ddx 0 ~ ddx + dx + 1] - return ODESystem(eqs, t, vars, []; name) + return System(eqs, t, vars, []; name) end @mtkbuild sys = System2() @@ -397,7 +397,7 @@ function System3(; name) initialization_eqs = [ ddx ~ -2 ] - return ODESystem(eqs, t, vars, []; name, initialization_eqs) + return System(eqs, t, vars, []; name, initialization_eqs) end @mtkbuild sys = System3() @@ -415,7 +415,7 @@ sol = solve(prob, Tsit5()) D(y) ~ x * (ρ - z) - y, D(z) ~ x * y - β * z] - @named sys = ODESystem(eqs, t) + @named sys = System(eqs, t) sys = structural_simplify(sys) u0 = [D(x) => 2.0, @@ -444,7 +444,7 @@ eqs = [D(x) ~ α * x - β * x * y D(y) ~ -γ * y + δ * x * y z ~ x + y] -@named sys = ODESystem(eqs, t) +@named sys = System(eqs, t) simpsys = structural_simplify(sys) tspan = (0.0, 10.0) @@ -472,7 +472,7 @@ sol = solve(prob, Tsit5()) eqs = [D(D(x)) ~ λ * x D(D(y)) ~ λ * y - g x^2 + y^2 ~ 1] -@mtkbuild pend = ODESystem(eqs, t) +@mtkbuild pend = System(eqs, t) prob = ODEProblem(pend, [x => 1], (0.0, 1.5), [g => 1], guesses = [λ => 0, y => 1], initialization_eqs = [y ~ 1]) @@ -484,8 +484,8 @@ sys = structural_simplify(unsimp; fully_determined = false) # Extend two systems with initialization equations and guesses # https://github.com/SciML/ModelingToolkit.jl/issues/2845 @variables x(t) y(t) -@named sysx = ODESystem([D(x) ~ 0], t; initialization_eqs = [x ~ 1]) -@named sysy = ODESystem([D(y) ~ 0], t; initialization_eqs = [y^2 ~ 2], guesses = [y => 1]) +@named sysx = System([D(x) ~ 0], t; initialization_eqs = [x ~ 1]) +@named sysy = System([D(y) ~ 0], t; initialization_eqs = [y^2 ~ 2], guesses = [y => 1]) sys = extend(sysx, sysy) @test length(equations(generate_initializesystem(sys))) == 2 @test length(ModelingToolkit.guesses(sys)) == 1 @@ -493,7 +493,7 @@ sys = extend(sysx, sysy) # https://github.com/SciML/ModelingToolkit.jl/issues/2873 @testset "Error on missing defaults" begin @variables x(t) y(t) - @named sys = ODESystem([x^2 + y^2 ~ 25, D(x) ~ 1], t) + @named sys = System([x^2 + y^2 ~ 25, D(x) ~ 1], t) ssys = structural_simplify(sys) @test_throws ModelingToolkit.MissingVariablesError ODEProblem( ssys, [x => 3], (0, 1), []) # y should have a guess @@ -505,7 +505,7 @@ end # system 1 should solve to x = 1 ics1 = [x => 1] - sys1 = ODESystem([D(x) ~ 0], t; defaults = ics1, name = :sys1) |> structural_simplify + sys1 = System([D(x) ~ 0], t; defaults = ics1, name = :sys1) |> structural_simplify prob1 = ODEProblem(sys1, [], (0.0, 1.0), []) sol1 = solve(prob1, Tsit5()) @test all(sol1[x] .== 1) @@ -513,7 +513,7 @@ end # system 2 should solve to x = y = 2 sys2 = extend( sys1, - ODESystem([D(y) ~ 0], t; initialization_eqs = [y ~ 2], name = :sys2) + System([D(y) ~ 0], t; initialization_eqs = [y ~ 2], name = :sys2) ) |> structural_simplify ics2 = unknowns(sys1) .=> 2 # should be equivalent to "ics2 = [x => 2]" prob2 = ODEProblem(sys2, ics2, (0.0, 1.0), []; fully_determined = true) @@ -524,12 +524,12 @@ end # https://github.com/SciML/ModelingToolkit.jl/issues/3029 @testset "Derivatives in initialization equations" begin @variables x(t) - sys = ODESystem( + sys = System( [D(D(x)) ~ 0], t; initialization_eqs = [x ~ 0, D(x) ~ 1], name = :sys) |> structural_simplify @test_nowarn ODEProblem(sys, [], (0.0, 1.0), []) - sys = ODESystem( + sys = System( [D(D(x)) ~ 0], t; initialization_eqs = [x ~ 0, D(D(x)) ~ 0], name = :sys) |> structural_simplify @test_nowarn ODEProblem(sys, [D(x) => 1.0], (0.0, 1.0), []) @@ -539,7 +539,7 @@ end @testset "Derivatives in initialization guesses" begin for sign in [-1.0, +1.0] @variables x(t) - sys = ODESystem( + sys = System( [D(D(x)) ~ 0], t; initialization_eqs = [D(x)^2 ~ 1, x ~ 0], guesses = [D(x) => sign], name = :sys ) |> structural_simplify @@ -556,8 +556,8 @@ eqs_1st_order = [D(Y) + Y - ω ~ 0, X + k1 ~ Y + k2] eqs_2nd_order = [D(D(Y)) + 2ω * D(Y) + (ω^2) * Y ~ 0, X + k1 ~ Y + k2] -@mtkbuild sys_1st_order = ODESystem(eqs_1st_order, t) -@mtkbuild sys_2nd_order = ODESystem(eqs_2nd_order, t) +@mtkbuild sys_1st_order = System(eqs_1st_order, t) +@mtkbuild sys_2nd_order = System(eqs_2nd_order, t) u0_1st_order_1 = [X => 1.0, Y => 2.0] u0_1st_order_2 = [Y => 2.0] @@ -582,7 +582,7 @@ sol = solve(oprob_2nd_order_2, Rosenbrock23()) # retcode: Success @testset "Vector in initial conditions" begin @variables x(t)[1:5] y(t)[1:5] - @named sys = ODESystem([D(x) ~ x, D(y) ~ y], t; initialization_eqs = [y ~ -x]) + @named sys = System([D(x) ~ x, D(y) ~ y], t; initialization_eqs = [y ~ -x]) sys = structural_simplify(sys) prob = ODEProblem(sys, [sys.x => ones(5)], (0.0, 1.0), []) sol = solve(prob, Tsit5(), reltol = 1e-4) @@ -738,7 +738,7 @@ end @testset "Null system" begin @variables x(t) y(t) s(t) @parameters x0 y0 - @mtkbuild sys = ODESystem([x ~ x0, y ~ y0, s ~ x + y], t; guesses = [y0 => 0.0]) + @mtkbuild sys = System([x ~ x0, y ~ y0, s ~ x + y], t; guesses = [y0 => 0.0]) prob = ODEProblem(sys, [s => 1.0], (0.0, 1.0), [x0 => 0.3, y0 => missing]) # trivial initialization run immediately @test prob.ps[y0] ≈ 0.7 @@ -758,7 +758,7 @@ end @named gravity = Force() @named constant = Constant(; k = 9.81) @named damper = TM.Damper(; d = 0.1) - @mtkbuild sys = ODESystem( + @mtkbuild sys = System( [connect(fixed.flange, spring.flange_a), connect(spring.flange_b, mass.flange_a), connect(mass.flange_a, gravity.flange), connect(constant.output, gravity.f), connect(fixed.flange, damper.flange_a), connect(damper.flange_b, mass.flange_a)], @@ -1062,7 +1062,7 @@ end @testset "Nonnumeric parameter dependencies are retained" begin @variables x(t) y(t) @parameters foo(::Real, ::Real) p - @mtkbuild sys = ODESystem([D(x) ~ t, 0 ~ foo(x, y)], t; + @mtkbuild sys = System([D(x) ~ t, 0 ~ foo(x, y)], t; parameter_dependencies = [foo ~ Multiplier(p, 2p)], guesses = [y => -1.0]) prob = ODEProblem(sys, [x => 1.0], (0.0, 1.0), [p => 1.0]) integ = init(prob, Rosenbrock23()) @@ -1071,7 +1071,7 @@ end @testset "Use observed equations for guesses of observed variables" begin @variables x(t) y(t) [state_priority = 100] - @mtkbuild sys = ODESystem( + @mtkbuild sys = System( [D(x) ~ x + t, y ~ 2x + 1], t; initialization_eqs = [x^3 + y^3 ~ 1]) isys = ModelingToolkit.generate_initializesystem(sys) @test isequal(defaults(isys)[y], 2x + 1) @@ -1079,14 +1079,14 @@ end @testset "Create initializeprob when unknown has dependent value" begin @variables x(t) y(t) - @mtkbuild sys = ODESystem([D(x) ~ x, D(y) ~ t * y], t; defaults = [x => 2y]) + @mtkbuild sys = System([D(x) ~ x, D(y) ~ t * y], t; defaults = [x => 2y]) prob = ODEProblem(sys, [y => 1.0], (0.0, 1.0)) @test prob.f.initializeprob !== nothing integ = init(prob) @test integ[x] ≈ 2.0 @variables x(t)[1:2] y(t) - @mtkbuild sys = ODESystem([D(x) ~ x, D(y) ~ t], t; defaults = [x => [y, 3.0]]) + @mtkbuild sys = System([D(x) ~ x, D(y) ~ t], t; defaults = [x => [y, 3.0]]) prob = ODEProblem(sys, [y => 1.0], (0.0, 1.0)) @test prob.f.initializeprob !== nothing integ = init(prob) @@ -1101,7 +1101,7 @@ end eqs = [D(D(x)) ~ λ * x D(D(y)) ~ λ * y - g x^2 + y^2 ~ L] - @mtkbuild pend = ODESystem(eqs, t) + @mtkbuild pend = System(eqs, t) prob = ODEProblem(pend, [x => 1, y => 0], (0.0, 1.5), [g => 1], guesses = ModelingToolkit.missing_variable_defaults(pend)) @@ -1152,7 +1152,7 @@ end connect(L1.n, emf.p) connect(emf.n, source.n, ground.g)] - @named model = ODESystem(connections, t, + @named model = System(connections, t, systems = [ ground, ref, @@ -1185,7 +1185,7 @@ end eqs = [D(D(x)) ~ λ * x D(D(y)) ~ λ * y - g x^2 + y^2 ~ 1] - @mtkbuild pend = ODESystem(eqs, t) + @mtkbuild pend = System(eqs, t) @test_warn ["structurally singular", "initialization", "Guess", "heuristic"] ODEProblem( pend, [x => 1, y => 0], (0.0, 1.5), [g => 1], guesses = [λ => 1]) end @@ -1193,7 +1193,7 @@ end @testset "DAEProblem initialization" begin @variables x(t) [guess = 1.0] y(t) [guess = 1.0] @parameters p=missing [guess = 1.0] q=missing [guess = 1.0] - @mtkbuild sys = ODESystem( + @mtkbuild sys = System( [D(x) ~ p * y + q * t, x^3 + y^3 ~ 5], t; initialization_eqs = [p^2 + q^3 ~ 3]) # FIXME: solve for du0 @@ -1210,7 +1210,7 @@ end @testset "Guesses provided to `ODEProblem` are used in `remake`" begin @variables x(t) y(t)=2x @parameters p q=3x - @mtkbuild sys = ODESystem([D(x) ~ x * p + q, x^3 + y^3 ~ 3], t) + @mtkbuild sys = System([D(x) ~ x * p + q, x^3 + y^3 ~ 3], t) prob = ODEProblem( sys, [], (0.0, 1.0), [p => 1.0]; guesses = [x => 1.0, y => 1.0, q => 1.0]) @test prob[x] == 1.0 @@ -1240,7 +1240,7 @@ end @testset "Remake problem with no initializeprob" begin @variables x(t) [guess = 1.0] y(t) [guess = 1.0] @parameters p [guess = 1.0] q [guess = 1.0] - @mtkbuild sys = ODESystem( + @mtkbuild sys = System( [D(x) ~ p * x + q * y, y ~ 2x], t; parameter_dependencies = [q ~ 2p]) prob = ODEProblem(sys, [x => 1.0], (0.0, 1.0), [p => 1.0]) test_dummy_initialization_equation(prob, x) @@ -1261,7 +1261,7 @@ end @testset "Variables provided as symbols" begin @variables x(t) [guess = 1.0] y(t) [guess = 1.0] @parameters p [guess = 1.0] q [guess = 1.0] - @mtkbuild sys = ODESystem( + @mtkbuild sys = System( [D(x) ~ p * x + q * y, y ~ 2x], t; parameter_dependencies = [q ~ 2p]) prob = ODEProblem(sys, [:x => 1.0], (0.0, 1.0), [p => 1.0]) test_dummy_initialization_equation(prob, x) @@ -1275,7 +1275,7 @@ end @testset "Issue#3246: type promotion with parameter dependent initialization_eqs" begin @variables x(t)=1 y(t)=1 @parameters a = 1 - @named sys = ODESystem([D(x) ~ 0, D(y) ~ x + a], t; initialization_eqs = [y ~ a]) + @named sys = System([D(x) ~ 0, D(y) ~ x + a], t; initialization_eqs = [y ~ a]) ssys = structural_simplify(sys) prob = ODEProblem(ssys, [], (0, 1), []) @@ -1296,7 +1296,7 @@ end D(X) ~ p - d * X, D(Y) ~ p - d * Y ] - @mtkbuild osys = ODESystem(eqs, t) + @mtkbuild osys = System(eqs, t) # Make problem. u0_vals = [X => 4, Y => 5.0] @@ -1342,7 +1342,7 @@ end @testset "Solvable array parameters with scalarized guesses" begin @variables x(t) @parameters p[1:2] q - @mtkbuild sys = ODESystem( + @mtkbuild sys = System( D(x) ~ p[1] + p[2] + q, t; defaults = [p[1] => q, p[2] => 2q], guesses = [p[1] => q, p[2] => 2q]) @test ModelingToolkit.is_parameter_solvable(p, Dict(), defaults(sys), guesses(sys)) @@ -1356,7 +1356,7 @@ end @testset "Issue#3318: Mutating `Initial` parameters works" begin @variables x(t) y(t)[1:2] [guess = ones(2)] @parameters p[1:2, 1:2] - @mtkbuild sys = ODESystem( + @mtkbuild sys = System( [D(x) ~ x, D(y) ~ p * y], t; initialization_eqs = [x^2 + y[1]^2 + y[2]^2 ~ 4]) prob = ODEProblem(sys, [x => 1.0, y[1] => 1], (0.0, 1.0), [p => 2ones(2, 2)]) integ = init(prob, Tsit5()) @@ -1375,8 +1375,8 @@ end @testset "Issue#3342" begin @variables x(t) y(t) stop!(integrator, _, _, _) = terminate!(integrator) - @named sys = ODESystem([D(x) ~ 1.0 - D(y) ~ 1.0], t; initialization_eqs = [ + @named sys = System([D(x) ~ 1.0 + D(y) ~ 1.0], t; initialization_eqs = [ y ~ 0.0 ], continuous_events = [ @@ -1397,7 +1397,7 @@ end @testset "Issue#3330: Initialization for unsimplified systems" begin @variables x(t) [guess = 1.0] - @mtkbuild sys = ODESystem(D(x) ~ x, t; initialization_eqs = [x^2 ~ 4]) + @mtkbuild sys = System(D(x) ~ x, t; initialization_eqs = [x^2 ~ 4]) prob = ODEProblem(sys, [], (0.0, 1.0)) @test prob.f.initialization_data !== nothing end @@ -1405,7 +1405,7 @@ end @testset "`ReconstructInitializeprob` with `nothing` state" begin @parameters p @variables x(t) - @mtkbuild sys = ODESystem(x ~ p * t, t) + @mtkbuild sys = System(x ~ p * t, t) prob = @test_nowarn ODEProblem(sys, [], (0.0, 1.0), [p => 1.0]) @test_nowarn remake(prob, p = [p => 1.0]) @test_nowarn remake(prob, p = [p => ForwardDiff.Dual(1.0)]) @@ -1419,7 +1419,7 @@ end D(X1) ~ k1 * (Γ[1] - X1) - k2 * X1 ] obs = [X2 ~ Γ[1] - X1] - @mtkbuild osys = ODESystem(eqs, t, [X1, X2], [k1, k2, Γ]; observed = obs) + @mtkbuild osys = System(eqs, t, [X1, X2], [k1, k2, Γ]; observed = obs) u0 = [X1 => 1.0, X2 => 2.0] ps = [k1 => 0.1, k2 => 0.2] @@ -1517,7 +1517,7 @@ end @testset "Issue#3504: Update initials when `remake` called with non-symbolic `u0`" begin @variables x(t) y(t) @parameters c1 c2 - @mtkbuild sys = ODESystem([D(x) ~ -c1 * x + c2 * y, D(y) ~ c1 * x - c2 * y], t) + @mtkbuild sys = System([D(x) ~ -c1 * x + c2 * y, D(y) ~ c1 * x - c2 * y], t) prob1 = ODEProblem(sys, [1.0, 2.0], (0.0, 1.0), [c1 => 1.0, c2 => 2.0]) prob2 = remake(prob1, u0 = [2.0, 3.0]) prob3 = remake(prob1, u0 = [2.0, 3.0], p = [c1 => 2.0]) @@ -1534,7 +1534,7 @@ end @parameters α=1 β=1 γ=1 δ=1 @variables x(t)=1 y(t)=1 eqs = [D(x) ~ α * x - β * x * y, D(y) ~ -δ * y + γ * x * y] - @named sys = ODESystem(eqs, t) + @named sys = System(eqs, t) prob = ODEProblem(complete(sys), [], (0.0, 1)) @inferred remake(prob; u0 = 2 .* prob.u0, p = prob.p) @inferred solve(prob) @@ -1546,7 +1546,7 @@ end eqs = [D(D(x)) ~ λ * x D(D(y)) ~ λ * y - g x^2 + y^2 ~ 1] - @mtkbuild pend = ODESystem(eqs, t) + @mtkbuild pend = System(eqs, t) prob = ODEProblem( pend, [x => (√2 / 2), D(x) => 0.0], (0.0, 1.5), @@ -1601,7 +1601,7 @@ end 0 ~ x^2 + y^2 - w2^2 ] - @mtkbuild sys = ODESystem(eqs, t) + @mtkbuild sys = System(eqs, t) u0 = [D(x) => 2.0, x => 1.0, @@ -1632,7 +1632,7 @@ end eqs = [D(D(x)) ~ λ * x D(D(y)) ~ λ * y - g x^2 + y^2 ~ 1] - @mtkbuild pend=ODESystem(eqs, t) split=false + @mtkbuild pend=System(eqs, t) split=false prob = ODEProblem(pend, [x => 1.0, D(x) => 0.0], (0.0, 1.0), [g => 1.0]; guesses = [y => 1.0, λ => 1.0]) @test !ModelingToolkit.is_split(prob.f.initialization_data.initializeprob.f.sys) @@ -1644,7 +1644,7 @@ end eqs = [D(D(x)) ~ λ * x D(D(y)) ~ λ * y - g x^2 + y^2 ~ 1] - @mtkbuild pend = ODESystem(eqs, t) + @mtkbuild pend = System(eqs, t) prob = ODEProblem(pend, SA[x => 1.0, D(x) => 0.0], (0.0, 1.0), SA[g => 1.0]; guesses = [y => 1.0, λ => 1.0]) @test !SciMLBase.isinplace(prob) diff --git a/test/input_output_handling.jl b/test/input_output_handling.jl index 68936b52bd..1fd7732c50 100644 --- a/test/input_output_handling.jl +++ b/test/input_output_handling.jl @@ -6,7 +6,7 @@ using ModelingToolkit: t_nounits as t, D_nounits as D @variables xx(t) some_input(t) [input = true] eqs = [D(xx) ~ some_input] -@named model = ODESystem(eqs, t) +@named model = System(eqs, t) @test_throws ExtraVariablesSystemException structural_simplify(model) if VERSION >= v"1.8" err = "In particular, the unset input(s) are:\n some_input(t)" @@ -17,14 +17,14 @@ end @variables x(t) u(t) [input = true] v(t)[1:2] [input = true] @test isinput(u) -@named sys = ODESystem([D(x) ~ -x + u], t) # both u and x are unbound -@named sys1 = ODESystem([D(x) ~ -x + v[1] + v[2]], t) # both v and x are unbound -@named sys2 = ODESystem([D(x) ~ -sys.x], t, systems = [sys]) # this binds sys.x in the context of sys2, sys2.x is still unbound -@named sys21 = ODESystem([D(x) ~ -sys1.x], t, systems = [sys1]) # this binds sys.x in the context of sys2, sys2.x is still unbound -@named sys3 = ODESystem([D(x) ~ -sys.x + sys.u], t, systems = [sys]) # This binds both sys.x and sys.u -@named sys31 = ODESystem([D(x) ~ -sys1.x + sys1.v[1]], t, systems = [sys1]) # This binds both sys.x and sys1.v[1] +@named sys = System([D(x) ~ -x + u], t) # both u and x are unbound +@named sys1 = System([D(x) ~ -x + v[1] + v[2]], t) # both v and x are unbound +@named sys2 = System([D(x) ~ -sys.x], t, systems = [sys]) # this binds sys.x in the context of sys2, sys2.x is still unbound +@named sys21 = System([D(x) ~ -sys1.x], t, systems = [sys1]) # this binds sys.x in the context of sys2, sys2.x is still unbound +@named sys3 = System([D(x) ~ -sys.x + sys.u], t, systems = [sys]) # This binds both sys.x and sys.u +@named sys31 = System([D(x) ~ -sys1.x + sys1.v[1]], t, systems = [sys1]) # This binds both sys.x and sys1.v[1] -@named sys4 = ODESystem([D(x) ~ -sys.x, u ~ sys.u], t, systems = [sys]) # This binds both sys.x and sys3.u, this system is one layer deeper than the previous. u is directly forwarded to sys.u, and in this case sys.u is bound while u is not +@named sys4 = System([D(x) ~ -sys.x, u ~ sys.u], t, systems = [sys]) # This binds both sys.x and sys3.u, this system is one layer deeper than the previous. u is directly forwarded to sys.u, and in this case sys.u is bound while u is not @test has_var(x ~ 1, x) @test has_var(1 ~ x, x) @@ -87,10 +87,10 @@ fsys4 = flatten(sys4) # Test output handling @variables x(t) y(t) [output = true] @test isoutput(y) -@named sys = ODESystem([D(x) ~ -x, y ~ x], t) # both y and x are unbound +@named sys = System([D(x) ~ -x, y ~ x], t) # both y and x are unbound syss = structural_simplify(sys, outputs = [y]) # This makes y an observed variable -@named sys2 = ODESystem([D(x) ~ -sys.x, y ~ sys.y], t, systems = [sys]) +@named sys2 = System([D(x) ~ -sys.x, y ~ sys.y], t, systems = [sys]) @test !is_bound(sys, y) @test !is_bound(sys, x) @@ -127,7 +127,7 @@ eqs = [connect(torque.flange, inertia1.flange_a) connect(inertia1.flange_b, spring.flange_a, damper.flange_a) connect(inertia2.flange_a, spring.flange_b, damper.flange_b) y ~ inertia2.w + torque.tau.u] -model = ODESystem(eqs, t; systems = [torque, inertia1, inertia2, spring, damper], +model = System(eqs, t; systems = [torque, inertia1, inertia2, spring, damper], name = :name, guesses = [spring.flange_a.phi => 0.0]) model_outputs = [inertia1.w, inertia2.w, inertia1.phi, inertia2.phi] model_inputs = [torque.tau.u] @@ -164,7 +164,7 @@ end D(x) ~ -x + u ] - @named sys = ODESystem(eqs, t) + @named sys = System(eqs, t) sys = structural_simplify(sys, inputs = [u]) f, dvs, ps, io_sys = ModelingToolkit.generate_control_function(sys; simplify, split) @@ -182,7 +182,7 @@ end D(x) ~ -x + u + d^2 ] - @named sys = ODESystem(eqs, t) + @named sys = System(eqs, t) sys = structural_simplify(sys, inputs = [u], disturbance_inputs = [d]) f, dvs, ps, io_sys = ModelingToolkit.generate_control_function(sys; simplify, split) @@ -200,7 +200,7 @@ end D(x) ~ -x + u + d^2 ] - @named sys = ODESystem(eqs, t) + @named sys = System(eqs, t) sys = structural_simplify(sys, inputs = [u], disturbance_inputs = [d]) f, dvs, ps, io_sys = ModelingToolkit.generate_control_function( sys; simplify, split, disturbance_argument = true) @@ -226,25 +226,25 @@ function Mass(; name, m = 1.0, p = 0, v = 0) sts = @variables pos(t)=p vel(t)=v eqs = [D(pos) ~ vel y ~ pos] - ODESystem(eqs, t, [pos, vel, y], ps; name) + System(eqs, t, [pos, vel, y], ps; name) end function MySpring(; name, k = 1e4) ps = @parameters k = k @variables x(t) = 0 # Spring deflection - ODESystem(Equation[], t, [x], ps; name) + System(Equation[], t, [x], ps; name) end function MyDamper(; name, c = 10) ps = @parameters c = c @variables vel(t) = 0 - ODESystem(Equation[], t, [vel], ps; name) + System(Equation[], t, [vel], ps; name) end function SpringDamper(; name, k = false, c = false) spring = MySpring(; name = :spring, k) damper = MyDamper(; name = :damper, c) - compose(ODESystem(Equation[], t; name), + compose(System(Equation[], t; name), spring, damper) end @@ -264,7 +264,7 @@ c = 10 eqs = [connect_sd(sd, mass1, mass2) D(mass1.vel) ~ (sd_force(sd) + u) / mass1.m D(mass2.vel) ~ (-sd_force(sd)) / mass2.m] -@named _model = ODESystem(eqs, t) +@named _model = System(eqs, t) @named model = compose(_model, mass1, mass2, sd); model = structural_simplify(model, inputs = [u]) @@ -282,7 +282,7 @@ i = findfirst(isequal(u[1]), out) @variables x(t) u(t) [input = true] eqs = [D(x) ~ u] -@named sys = ODESystem(eqs, t) +@named sys = System(eqs, t) @test_nowarn structural_simplify(sys, inputs = [u], outputs = []) #= @@ -314,7 +314,7 @@ function SystemModel(u = nothing; name = :model) connect(inertia2.flange_a, spring.flange_b, damper.flange_b)] if u !== nothing push!(eqs, connect(torque.tau, u.output)) - return @named model = ODESystem(eqs, t; + return @named model = System(eqs, t; systems = [ torque, inertia1, @@ -324,7 +324,7 @@ function SystemModel(u = nothing; name = :model) u ]) end - ODESystem(eqs, t; systems = [torque, inertia1, inertia2, spring, damper], + System(eqs, t; systems = [torque, inertia1, inertia2, spring, damper], name, guesses = [spring.flange_a.phi => 0.0]) end @@ -365,7 +365,7 @@ eqs = [D(y₁) ~ -k₁ * y₁ + k₃ * y₂ * y₃ + u1 D(y₂) ~ k₁ * y₁ - k₃ * y₂ * y₃ - k₂ * y₂^2 + u2 y₁ + y₂ + y₃ ~ 1] -@named sys = ODESystem(eqs, t) +@named sys = System(eqs, t) m_inputs = [u[1], u[2]] m_outputs = [y₂] sys_simp = structural_simplify(sys, inputs = m_inputs, outputs = m_outputs) @@ -377,7 +377,7 @@ sys_simp = structural_simplify(sys, inputs = m_inputs, outputs = m_outputs) @named gain = Gain(1;) @named int = Integrator(; k = 1) @named fb = Feedback(;) -@named model = ODESystem( +@named model = System( [ connect(c.output, fb.input1), connect(fb.input2, int.output), @@ -434,7 +434,7 @@ matrices = ModelingToolkit.reorder_unknowns( D(x) ~ -x + u ] - @named sys = ODESystem(eqs, t) + @named sys = System(eqs, t) sys = structural_simplify(sys, inputs = [u]) (; io_sys,) = ModelingToolkit.generate_control_function(sys, simplify = true) obsfn = ModelingToolkit.build_explicit_observed_function( @@ -447,7 +447,7 @@ end @constants c = 2.0 @variables x(t) eqs = [D(x) ~ c * x] - @mtkbuild sys = ODESystem(eqs, t, [x], []) + @mtkbuild sys = System(eqs, t, [x], []) f, dvs, ps, io_sys = ModelingToolkit.generate_control_function(sys) @test f[1]([0.5], nothing, MTKParameters(io_sys, []), 0.0) ≈ [1.0] @@ -457,7 +457,7 @@ end @variables x(t)=0 u(t)=0 [input = true] @parameters p(::Real) = (x -> 2x) eqs = [D(x) ~ -x + p(u)] - @named sys = ODESystem(eqs, t) + @named sys = System(eqs, t) sys = structural_simplify(sys, inputs = [u]) f, dvs, ps, io_sys = ModelingToolkit.generate_control_function(sys) p = MTKParameters(io_sys, []) diff --git a/test/inputoutput.jl b/test/inputoutput.jl index 2a81a0e315..28f112425f 100644 --- a/test/inputoutput.jl +++ b/test/inputoutput.jl @@ -10,12 +10,12 @@ eqs = [D(x) ~ σ * (y - x) + F, D(z) ~ x * y - β * z] aliases = [u ~ x + y - z] -lorenz1 = ODESystem(eqs, pins = [F], observed = aliases, name = :lorenz1) -lorenz2 = ODESystem(eqs, pins = [F], observed = aliases, name = :lorenz2) +lorenz1 = System(eqs, pins = [F], observed = aliases, name = :lorenz1) +lorenz2 = System(eqs, pins = [F], observed = aliases, name = :lorenz2) connections = [lorenz1.F ~ lorenz2.u, lorenz2.F ~ lorenz1.u] -connected = ODESystem(Equation[], t, [], [], observed = connections, +connected = System(Equation[], t, [], [], observed = connections, systems = [lorenz1, lorenz2]) sys = connected diff --git a/test/jacobiansparsity.jl b/test/jacobiansparsity.jl index 1f936bc878..d10ec66c44 100644 --- a/test/jacobiansparsity.jl +++ b/test/jacobiansparsity.jl @@ -91,7 +91,7 @@ prob = ODEProblem(sys, u0, (0, 11.5), sparse = true, jac = true) eqs = [D(D(x)) ~ λ * x D(D(y)) ~ λ * y - g x^2 + y^2 ~ 1] - @mtkbuild pend = ODESystem(eqs, t) + @mtkbuild pend = System(eqs, t) u0 = [x => 1, y => 0] prob = ODEProblem( @@ -125,7 +125,7 @@ end eqs = [D(D(x)) ~ λ * x D(D(y)) ~ λ * y - g x^2 + y^2 ~ 1] - @mtkbuild pend = ODESystem(eqs, t) + @mtkbuild pend = System(eqs, t) prob = ODEProblem(pend, [x => 0.0, D(x) => 1.0], (0.0, 1.0), [g => 1.0]; guesses = [y => 1.0, λ => 1.0], jac = true, sparse = true) J = deepcopy(prob.f.jac_prototype) diff --git a/test/labelledarrays.jl b/test/labelledarrays.jl index c9ee7ee50b..59c269dff7 100644 --- a/test/labelledarrays.jl +++ b/test/labelledarrays.jl @@ -12,7 +12,7 @@ eqs = [D(x) ~ σ * (y - x), D(y) ~ t * x * (ρ - z) - y, D(z) ~ x * y - β * z] -@named de = ODESystem(eqs, t) +@named de = System(eqs, t) de = complete(de) ff = ODEFunction(de, [x, y, z], [σ, ρ, β], jac = true) diff --git a/test/latexify.jl b/test/latexify.jl index 105a17ca6e..de5c195610 100644 --- a/test/latexify.jl +++ b/test/latexify.jl @@ -55,6 +55,6 @@ eqs = [D(x) ~ (1 + cos(t)) / (1 + 2 * x)] ap = AnalysisPoint(:plant_input) eqs = [connect(P.output, C.input) connect(C.output, ap, P.input)] -sys_ap = ODESystem(eqs, t, systems = [P, C], name = :hej) +sys_ap = System(eqs, t, systems = [P, C], name = :hej) @test_reference "latexify/50.tex" latexify(sys_ap) diff --git a/test/linearity.jl b/test/linearity.jl index aed9a256d2..d472cdb087 100644 --- a/test/linearity.jl +++ b/test/linearity.jl @@ -12,16 +12,16 @@ eqs = [D(x) ~ σ * (y - x), D(y) ~ -z - y, D(z) ~ y - β * z] -@test ModelingToolkit.islinear(@named sys = ODESystem(eqs, t)) +@test ModelingToolkit.islinear(@named sys = System(eqs, t)) eqs2 = [D(x) ~ σ * (y - x), D(y) ~ -z - 1 / y, D(z) ~ y - β * z] -@test !ModelingToolkit.islinear(@named sys = ODESystem(eqs2, t)) +@test !ModelingToolkit.islinear(@named sys = System(eqs2, t)) eqs3 = [D(x) ~ σ * (y - x), D(y) ~ -z - y, D(z) ~ y - β * z + 1] -@test ModelingToolkit.isaffine(@named sys = ODESystem(eqs, t)) +@test ModelingToolkit.isaffine(@named sys = System(eqs, t)) diff --git a/test/lowering_solving.jl b/test/lowering_solving.jl index 300d505ab0..8c4db26287 100644 --- a/test/lowering_solving.jl +++ b/test/lowering_solving.jl @@ -8,14 +8,14 @@ eqs = [D(D(x)) ~ σ * (y - x), D(y) ~ x * (ρ - z) - y, D(z) ~ x * y - β * z] -@named sys′ = ODESystem(eqs, t) +@named sys′ = System(eqs, t) sys = ode_order_lowering(sys′) eqs2 = [0 ~ x * y - k, D(D(x)) ~ σ * (y - x), D(y) ~ x * (ρ - z) - y, D(z) ~ x * y - β * z] -@named sys2 = ODESystem(eqs2, t, [x, y, z, k], parameters(sys′)) +@named sys2 = System(eqs2, t, [x, y, z, k], parameters(sys′)) sys2 = ode_order_lowering(sys2) # test equation/variable ordering ModelingToolkit.calculate_massmatrix(sys2) == Diagonal([1, 1, 1, 1, 0]) @@ -46,13 +46,13 @@ eqs = [D(x) ~ σ * (y - x), D(y) ~ x * (ρ - z) - y, D(z) ~ x * y - β * z] -lorenz1 = ODESystem(eqs, t, name = :lorenz1) -lorenz2 = ODESystem(eqs, t, name = :lorenz2) +lorenz1 = System(eqs, t, name = :lorenz1) +lorenz2 = System(eqs, t, name = :lorenz2) @variables α(t) @parameters γ connections = [0 ~ lorenz1.x + lorenz2.y + α * γ] -@named connected = ODESystem(connections, t, [α], [γ], systems = [lorenz1, lorenz2]) +@named connected = System(connections, t, [α], [γ], systems = [lorenz1, lorenz2]) connected = complete(connected) u0 = [lorenz1.x => 1.0, lorenz1.y => 0.0, diff --git a/test/mass_matrix.jl b/test/mass_matrix.jl index 8b31123834..f181bdfbde 100644 --- a/test/mass_matrix.jl +++ b/test/mass_matrix.jl @@ -8,9 +8,9 @@ eqs = [D(y[1]) ~ -k[1] * y[1] + k[3] * y[2] * y[3], D(y[2]) ~ k[1] * y[1] - k[3] * y[2] * y[3] - k[2] * y[2]^2, 0 ~ y[1] + y[2] + y[3] - 1] -@named sys = ODESystem(eqs, t, collect(y), [k]) +@named sys = System(eqs, t, collect(y), [k]) sys = complete(sys) -@test_throws ArgumentError ODESystem(eqs, y[1]) +@test_throws ArgumentError System(eqs, y[1]) M = calculate_massmatrix(sys) @test M isa Diagonal @test M == [1 0 0 @@ -45,7 +45,7 @@ sol2 = solve(prob_mm2, Rodas5(), reltol = 1e-8, abstol = 1e-8, tstops = sol.t, # Test mass matrix in the identity case eqs = [D(y[1]) ~ y[1], D(y[2]) ~ y[2], D(y[3]) ~ y[3]] -@named sys = ODESystem(eqs, t, collect(y), [k]) +@named sys = System(eqs, t, collect(y), [k]) @test calculate_massmatrix(sys) === I @@ -54,7 +54,7 @@ eqs = [D(y[1]) ~ y[1], D(y[2]) ~ y[2], D(y[3]) ~ y[3]] D(y[2]) ~ k[1] * y[1] - k[3] * y[2] * y[3] - k[2] * y[2]^2, 0 ~ y[1] + y[2] + y[3] - 1] - @named sys = ODESystem(eqs, t, collect(y), [k]) + @named sys = System(eqs, t, collect(y), [k]) @named sys = SDESystem(sys, [1, 1, 0]) sys = complete(sys) prob = SDEProblem(sys, [y => [1.0, 0.0, 0.0]], (0.0, 1e5), [k => [0.04, 3e7, 1e4]]) diff --git a/test/model_parsing.jl b/test/model_parsing.jl index ad457a0ba3..7271d9bd1d 100644 --- a/test/model_parsing.jl +++ b/test/model_parsing.jl @@ -1044,17 +1044,17 @@ end x + y EvalAt(1)(y)^2 end - @consolidate f(u) = u[1]^2 + log(u[2]) + @consolidate f(u, sub) = u[1]^2 + log(u[2]) + sum(sub; init = 0) end @named ex = Example() ex = complete(ex) costs = ModelingToolkit.get_costs(ex) - constrs = ModelingToolkit.get_constraints(ModelingToolkit.get_constraintsystem(ex)) + constrs = ModelingToolkit.get_constraints(ex) @test isequal(costs[1], ex.x + ex.y) @test isequal(costs[2], EvalAt(1)(ex.y)^2) - @test isequal(constrs[1], -3 + EvalAt(0.3)(ex.x) ~ 0) - @test isequal(constrs[2], -4 + ex.y ≲ 0) - @test ModelingToolkit.get_consolidate(ex)([1, 2]) ≈ 1 + log(2) + @test isequal(constrs[1], EvalAt(0.3)(ex.x) ~ 3) + @test isequal(constrs[2], ex.y ≲ 4) + @test ModelingToolkit.get_consolidate(ex)([1, 2], [3, 4]) ≈ 8 + log(2) end diff --git a/test/modelingtoolkitize.jl b/test/modelingtoolkitize.jl index db99cc91a4..d146fa95c9 100644 --- a/test/modelingtoolkitize.jl +++ b/test/modelingtoolkitize.jl @@ -375,7 +375,7 @@ sys = modelingtoolkitize(prob) @testset "ODE" begin @variables x(t)=1.0 y(t)=2.0 @parameters p=3.0 q=4.0 - @mtkbuild sys = ODESystem([D(x) ~ p * y, D(y) ~ q * x], t) + @mtkbuild sys = System([D(x) ~ p * y, D(y) ~ q * x], t) prob1 = ODEProblem(sys, [], (0.0, 5.0)) newsys = complete(modelingtoolkitize(prob1)) @test is_variable(newsys, newsys.x) diff --git a/test/mtkparameters.jl b/test/mtkparameters.jl index 809da4df94..122b7acdd1 100644 --- a/test/mtkparameters.jl +++ b/test/mtkparameters.jl @@ -8,7 +8,7 @@ using ForwardDiff using JET @parameters a b c(t) d::Integer e[1:3] f[1:3, 1:3]::Int g::Vector{AbstractFloat} h::String -@named sys = ODESystem( +@named sys = System( Equation[], t, [], [a, c, d, e, f, g, h], parameter_dependencies = [b ~ 2a], continuous_events = [ModelingToolkit.SymbolicContinuousCallback( [a ~ 0] => [c ~ 0], discrete_parameters = c)], defaults = Dict(a => 0.0)) @@ -140,7 +140,7 @@ end @parameters p::Vector{Float64} @variables X(t) eq = D(X) ~ p[1] - p[2] * X -@mtkbuild osys = ODESystem([eq], t) +@mtkbuild osys = System([eq], t) u0 = [X => 1.0] ps = [p => [2.0, 0.1]] @@ -149,7 +149,7 @@ p = MTKParameters(osys, ps, u0) # Ensure partial update promotes the buffer @parameters p q r -@named sys = ODESystem(Equation[], t, [], [p, q, r]) +@named sys = System(Equation[], t, [], [p, q, r]) sys = complete(sys) ps = MTKParameters(sys, [p => 1.0, q => 2.0, r => 3.0]) newps = remake_buffer(sys, ps, (p,), (1.0f0,)) @@ -160,7 +160,7 @@ newps = remake_buffer(sys, ps, (p,), (1.0f0,)) @parameters p d @variables X(t) eqs = [D(X) ~ p - d * X] -@mtkbuild sys = ODESystem(eqs, t) +@mtkbuild sys = System(eqs, t) u0 = [X => 1.0] tspan = (0.0, 100.0) @@ -180,7 +180,7 @@ function level1() eqs = [D(x) ~ p1 * x - p2 * x * y D(y) ~ -p3 * y + p4 * x * y] - sys = structural_simplify(complete(ODESystem( + sys = structural_simplify(complete(System( eqs, t, tspan = (0, 3.0), name = :sys, parameter_dependencies = [y0 => 2p4]))) prob = ODEProblem{true, SciMLBase.FullSpecialize}(sys) end @@ -194,7 +194,7 @@ function level2() eqs = [D(x) ~ p1 * x - p23[1] * x * y D(y) ~ -p23[2] * y + p4 * x * y] - sys = structural_simplify(complete(ODESystem( + sys = structural_simplify(complete(System( eqs, t, tspan = (0, 3.0), name = :sys, parameter_dependencies = [y0 => 2p4]))) prob = ODEProblem{true, SciMLBase.FullSpecialize}(sys) end @@ -208,7 +208,7 @@ function level3() eqs = [D(x) ~ p1 * x - p23[1] * x * y D(y) ~ -p23[2] * y + p4 * x * y] - sys = structural_simplify(complete(ODESystem( + sys = structural_simplify(complete(System( eqs, t, tspan = (0, 3.0), name = :sys, parameter_dependencies = [y0 => 2p4]))) prob = ODEProblem{true, SciMLBase.FullSpecialize}(sys) end @@ -248,7 +248,7 @@ end @variables x(t) y(t) eqs = [D(x) ~ (α - β * y) * x D(y) ~ (δ * x - γ) * y] -@mtkbuild odesys = ODESystem(eqs, t) +@mtkbuild odesys = System(eqs, t) odeprob = ODEProblem( odesys, [x => 1.0, y => 1.0], (0.0, 10.0), [α => 1.5, β => 1.0, γ => 3.0, δ => 1.0]) tunables, _... = canonicalize(Tunable(), odeprob.p) @@ -273,7 +273,7 @@ VVDual = Vector{<:Vector{<:ForwardDiff.Dual}} end @parameters a b::Int c::Vector{Float64} d[1:2, 1:2]::Int e::Foo{Int} f::Foo - @named sys = ODESystem(Equation[], t, [], [a, b, c, d, e, f]) + @named sys = System(Equation[], t, [], [a, b, c, d, e, f]) sys = complete(sys) ps = MTKParameters(sys, Dict(a => 1.0, b => 2, c => 3ones(2), @@ -311,7 +311,7 @@ end @testset "Error on missing parameter defaults" begin @parameters a b c - @named sys = ODESystem(Equation[], t, [], [a, b]; defaults = Dict(b => 2c)) + @named sys = System(Equation[], t, [], [a, b]; defaults = Dict(b => 2c)) sys = complete(sys) @test_throws ["Could not evaluate", "b", "Missing", "2c"] MTKParameters(sys, [a => 1.0]) end @@ -323,7 +323,7 @@ end D(V[1]) ~ k[1] - k[2] * V[1], D(V[2]) ~ k[3] - k[4] * V[2] ] - @mtkbuild osys_scal = ODESystem(eqs, t, [V[1], V[2]], [k[1], k[2], k[3], k[4]]) + @mtkbuild osys_scal = System(eqs, t, [V[1], V[2]], [k[1], k[2], k[3], k[4]]) u0 = [V => [10.0, 20.0]] ps_vec = [k => [2.0, 3.0, 4.0, 5.0]] diff --git a/test/namespacing.jl b/test/namespacing.jl index b6305a1776..3871398144 100644 --- a/test/namespacing.jl +++ b/test/namespacing.jl @@ -1,10 +1,10 @@ using ModelingToolkit using ModelingToolkit: t_nounits as t, D_nounits as D, iscomplete, does_namespacing -@testset "ODESystem" begin +@testset "System" begin @variables x(t) @parameters p - sys = ODESystem(D(x) ~ p * x, t; name = :inner) + sys = System(D(x) ~ p * x, t; name = :inner) @test !iscomplete(sys) @test does_namespacing(sys) @@ -23,7 +23,7 @@ using ModelingToolkit: t_nounits as t, D_nounits as D, iscomplete, does_namespac @test isequal(p, nsys.p) @test !isequal(p, sys.p) - @test_throws ["namespacing", "inner"] ODESystem( + @test_throws ["namespacing", "inner"] System( Equation[], t; systems = [nsys], name = :a) end diff --git a/test/nonlinearsystem.jl b/test/nonlinearsystem.jl index 158475f7c9..3a22cba409 100644 --- a/test/nonlinearsystem.jl +++ b/test/nonlinearsystem.jl @@ -119,14 +119,14 @@ lorenz2 = lorenz(:lorenz2) using OrdinaryDiffEq @independent_variables t D = Differential(t) -@named subsys = convert_system(ODESystem, lorenz1, t) -@named sys = ODESystem([D(subsys.x) ~ subsys.x + subsys.x], t, systems = [subsys]) +@named subsys = convert_system(System, lorenz1, t) +@named sys = System([D(subsys.x) ~ subsys.x + subsys.x], t, systems = [subsys]) sys = structural_simplify(sys) u0 = [subsys.x => 1, subsys.z => 2.0, subsys.y => 1.0] prob = ODEProblem(sys, u0, (0, 1.0), [subsys.σ => 1, subsys.ρ => 2, subsys.β => 3]) sol = solve(prob, FBDF(), reltol = 1e-7, abstol = 1e-7) @test sol[subsys.x] + sol[subsys.y] - sol[subsys.z]≈sol[subsys.u] atol=1e-7 -@test_throws ArgumentError convert_system(ODESystem, sys, t) +@test_throws ArgumentError convert_system(System, sys, t) @parameters σ ρ β @variables x y z @@ -197,7 +197,7 @@ eq = [v1 ~ sin(2pi * t * h) v1 - v2 ~ i1 v2 ~ i2 i1 ~ i2] -@named sys = ODESystem(eq, t) +@named sys = System(eq, t) @test length(equations(structural_simplify(sys))) == 0 @testset "Issue: 1504" begin @@ -393,10 +393,10 @@ end @test is_parameter(sys, p) end -@testset "Can convert from `ODESystem`" begin +@testset "Can convert from `System`" begin @variables x(t) y(t) @parameters p q r - @named sys = ODESystem([D(x) ~ p * x^3 + q, 0 ~ -y + q * x - r], t; + @named sys = System([D(x) ~ p * x^3 + q, 0 ~ -y + q * x - r], t; defaults = [x => 1.0, p => missing], guesses = [p => 1.0], initialization_eqs = [p^3 + q^3 ~ 4r], parameter_dependencies = [r ~ 3p]) nlsys = NonlinearSystem(sys) @@ -435,7 +435,7 @@ end @test sol.ps[p^3 + q^3]≈sol.ps[4r] atol=1e-10 @testset "Differential inside expression also substituted" begin - @named sys = ODESystem([0 ~ y * D(x) + x^2 - p, 0 ~ x * D(y) + y * p], t) + @named sys = System([0 ~ y * D(x) + x^2 - p, 0 ~ x * D(y) + y * p], t) nlsys = NonlinearSystem(sys) vs = ModelingToolkit.vars(equations(nlsys)) @test !in(D(x), vs) diff --git a/test/odesystem.jl b/test/odesystem.jl index b707d119fc..75914e40a7 100644 --- a/test/odesystem.jl +++ b/test/odesystem.jl @@ -24,7 +24,7 @@ eqs = [D(x) ~ σ * (y - x), D(z) ~ x * y - β * z * κ] ModelingToolkit.toexpr.(eqs)[1] -@named de = ODESystem(eqs, t; defaults = Dict(x => 1)) +@named de = System(eqs, t; defaults = Dict(x => 1)) subed = substitute(de, [σ => k]) ssort(eqs) = sort(eqs, by = string) @test isequal(ssort(parameters(subed)), [k, β, ρ]) @@ -32,7 +32,7 @@ ssort(eqs) = sort(eqs, by = string) [D(x) ~ k * (y - x) D(y) ~ (ρ - z) * x - y D(z) ~ x * y - β * κ * z]) -@named des[1:3] = ODESystem(eqs, t) +@named des[1:3] = System(eqs, t) @test length(unique(x -> ModelingToolkit.get_tag(x), des)) == 1 @test eval(toexpr(de)) == de @@ -41,7 +41,7 @@ ssort(eqs) = sort(eqs, by = string) generate_function(de) function test_diffeq_inference(name, sys, iv, dvs, ps) - @testset "ODESystem construction: $name" begin + @testset "System construction: $name" begin @test isequal(independent_variables(sys)[1], value(iv)) @test length(independent_variables(sys)) == 1 @test isempty(setdiff(Set(unknowns(sys)), Set(value.(dvs)))) @@ -124,7 +124,7 @@ f = eval(ODEFunctionExpr(de, [x, y, z], [σ, ρ, β], sparsity = false)) eqs = [D(x) ~ σ * (y - x), D(y) ~ x * (ρ - z) - y * t, D(z) ~ x * y - β * z * κ] -@named de = ODESystem(eqs, t) +@named de = System(eqs, t) de = complete(de) ModelingToolkit.calculate_tgrad(de) @@ -141,7 +141,7 @@ tgrad_iip(du, u, p, t) eqs = [D(x) ~ σ(t - 1) * (y - x), D(y) ~ x * (ρ - z) - y, D(z) ~ x * y - β * z * κ] -@named de = ODESystem(eqs, t) +@named de = System(eqs, t) test_diffeq_inference("single internal iv-varying", de, t, (x, y, z), (σ, ρ, β)) f = generate_function(de, [x, y, z], [σ, ρ, β], expression = Val{false})[2] du = [0.0, 0.0, 0.0] @@ -149,7 +149,7 @@ f(du, [1.0, 2.0, 3.0], [x -> x + 7, 2, 3], 5.0) @test du ≈ [11, -3, -7] eqs = [D(x) ~ x + 10σ(t - 1) + 100σ(t - 2) + 1000σ(t^2)] -@named de = ODESystem(eqs, t) +@named de = System(eqs, t) test_diffeq_inference("many internal iv-varying", de, t, (x,), (σ,)) f = generate_function(de, [x], [σ], expression = Val{false})[2] du = [0.0] @@ -162,7 +162,7 @@ D2 = D^2 @variables u(t) uˍtt(t) uˍt(t) xˍt(t) eqs = [D3(u) ~ 2(D2(u)) + D(u) + D(x) + 1 D2(x) ~ D(x) + 2] -@named de = ODESystem(eqs, t) +@named de = System(eqs, t) de1 = ode_order_lowering(de) lowered_eqs = [D(uˍtt) ~ 2uˍtt + uˍt + xˍt + 1 D(xˍt) ~ xˍt + 2 @@ -170,13 +170,13 @@ lowered_eqs = [D(uˍtt) ~ 2uˍtt + uˍt + xˍt + 1 D(u) ~ uˍt D(x) ~ xˍt] -#@test de1 == ODESystem(lowered_eqs) +#@test de1 == System(lowered_eqs) # issue #219 @test all(isequal.( [ModelingToolkit.var_from_nested_derivative(eq.lhs)[1] for eq in equations(de1)], - unknowns(@named lowered = ODESystem(lowered_eqs, t)))) + unknowns(@named lowered = System(lowered_eqs, t)))) test_diffeq_inference("first-order transform", de1, t, [uˍtt, xˍt, uˍt, u, x], []) du = zeros(5) @@ -189,7 +189,7 @@ a = y - x eqs = [D(x) ~ σ * a, D(y) ~ x * (ρ - z) - y, D(z) ~ x * y - β * z * κ] -@named de = ODESystem(eqs, t) +@named de = System(eqs, t) generate_function(de, [x, y, z], [σ, ρ, β]) jac = calculate_jacobian(de) @test ModelingToolkit.jacobian_sparsity(de).colptr == sparse(jac).colptr @@ -201,7 +201,7 @@ f = ODEFunction(complete(de), [x, y, z], [σ, ρ, β]) _x = y / C eqs = [D(x) ~ -A * x, D(y) ~ A * x - B * _x] -@named de = ODESystem(eqs, t) +@named de = System(eqs, t) @test begin local f, du f = generate_function(de, [x, y], [A, B, C], expression = Val{false})[2] @@ -242,7 +242,7 @@ ODEFunction(de)(similar(prob.u0), prob.u0, prob.p, 0.1) eqs = [D(y₁) ~ -k₁ * y₁ + k₃ * y₂ * y₃, 0 ~ y₁ + y₂ + y₃ - 1, D(y₂) ~ k₁ * y₁ - k₂ * y₂^2 - k₃ * y₂ * y₃ * κ] -@named sys = ODESystem(eqs, t, defaults = [k₁ => 100, k₂ => 3e7, y₁ => 1.0]) +@named sys = System(eqs, t, defaults = [k₁ => 100, k₂ => 3e7, y₁ => 1.0]) sys = complete(sys) u0 = Pair[] push!(u0, y₂ => 0.0) @@ -289,14 +289,14 @@ sol_dpmap = solve(prob_dpmap, Rodas5()) function makesys(name) @parameters a = 1.0 @variables x(t) = 0.0 - ODESystem([D(x) ~ -a * x], t; name) + System([D(x) ~ -a * x], t; name) end function makecombinedsys() sys1 = makesys(:sys1) sys2 = makesys(:sys2) @parameters b = 1.0 - complete(ODESystem(Equation[], t, [], [b]; systems = [sys1, sys2], name = :foo)) + complete(System(Equation[], t, [], [b]; systems = [sys1, sys2], name = :foo)) end sys = makecombinedsys() @@ -341,7 +341,7 @@ end eqs = [D(x) ~ σ * (y - x), D(y) ~ x - β * y, x + z ~ y] -@named sys = ODESystem(eqs, t) +@named sys = System(eqs, t) @test all(isequal.(unknowns(sys), [x, y, z])) @test all(isequal.(parameters(sys), [σ, β])) @test equations(sys) == eqs @@ -351,13 +351,13 @@ eqs = [D(x) ~ σ * (y - x), using ModelingToolkit @parameters a @variables x(t) -@named sys = ODESystem([D(x) ~ a], t) +@named sys = System([D(x) ~ a], t) @test issym(equations(sys)[1].rhs) # issue 708 @parameters a @variables x(t) y(t) z(t) -@named sys = ODESystem([D(x) ~ y, 0 ~ x + z, 0 ~ x - y], t, [z, y, x], []) +@named sys = System([D(x) ~ y, 0 ~ x + z, 0 ~ x - y], t, [z, y, x], []) asys = add_accumulations(sys) @variables accumulation_x(t) accumulation_y(t) accumulation_z(t) eqs = [0 ~ x + z @@ -386,16 +386,16 @@ eqs = [ D(x1) ~ -x1, 0 ~ x1 - x2 ] -@named sys = ODESystem(eqs, t) +@named sys = System(eqs, t) @test isequal(ModelingToolkit.get_iv(sys), t) @test isequal(unknowns(sys), [x1, x2]) @test isempty(parameters(sys)) -# one equation ODESystem test +# one equation System test @parameters r @variables x(t) eq = D(x) ~ r * x -@named ode = ODESystem(eq, t) +@named ode = System(eq, t) @test equations(ode) == [eq] # issue #808 @testset "Combined system name collisions" begin @@ -403,14 +403,14 @@ eq = D(x) ~ r * x @parameters a @variables x(t) f(t) - ODESystem([D(x) ~ -a * x + f], t; name) + System([D(x) ~ -a * x + f], t; name) end function issue808() sys1 = makesys(:sys1) sys2 = makesys(:sys1) - @test_throws ArgumentError ODESystem([sys2.f ~ sys1.x, D(sys1.f) ~ 0], t, + @test_throws ArgumentError System([sys2.f ~ sys1.x, D(sys1.f) ~ 0], t, systems = [sys1, sys2], name = :foo) end issue808() @@ -422,19 +422,19 @@ vars = @variables((u1,)) eqs = [ D(u1) ~ 1 ] -@test_throws ArgumentError ODESystem(eqs, t, vars, pars, name = :foo) +@test_throws ArgumentError System(eqs, t, vars, pars, name = :foo) #Issue 1063/998 pars = [t] vars = @variables((u1(t),)) -@test_throws ArgumentError ODESystem(eqs, t, vars, pars, name = :foo) +@test_throws ArgumentError System(eqs, t, vars, pars, name = :foo) @parameters w der = Differential(w) eqs = [ der(u1) ~ t ] -@test_throws ArgumentError ModelingToolkit.ODESystem(eqs, t, vars, pars, name = :foo) +@test_throws ArgumentError ModelingToolkit.System(eqs, t, vars, pars, name = :foo) @variables x(t) @parameters M b k @@ -442,7 +442,7 @@ eqs = [D(D(x)) ~ -b / M * D(x) - k / M * x] ps = [M, b, k] default_u0 = [D(x) => 0.0, x => 10.0] default_p = [M => 1.0, b => 1.0, k => 1.0] -@named sys = ODESystem(eqs, t, [x], ps; defaults = [default_u0; default_p], tspan) +@named sys = System(eqs, t, [x], ps; defaults = [default_u0; default_p], tspan) sys = ode_order_lowering(sys) sys = complete(sys) prob = ODEProblem(sys) @@ -455,7 +455,7 @@ prob = ODEProblem{false}(sys; u0_constructor = x -> SVector(x...)) # check_eqs_u0 kwarg test @variables x1(t) x2(t) eqs = [D(x1) ~ -x1] -@named sys = ODESystem(eqs, t, [x1, x2], []) +@named sys = System(eqs, t, [x1, x2], []) sys = complete(sys) @test_throws ArgumentError ODEProblem(sys, [1.0, 1.0], (0.0, 1.0)) @test_nowarn ODEProblem(sys, [1.0, 1.0], (0.0, 1.0), check_length = false) @@ -466,7 +466,7 @@ let @variables x(t) ẋ(t) f(t) [input = true] eqs = [D(x) ~ ẋ, D(ẋ) ~ f - k * x - d * ẋ] - @named sys = ODESystem(eqs, t, [x, ẋ], [d, k]) + @named sys = System(eqs, t, [x, ẋ], [f, d, k]) sys = structural_simplify(sys; inputs = [f]) @test isequal(calculate_control_jacobian(sys), @@ -476,7 +476,7 @@ end # issue 1109 let @variables x(t)[1:3, 1:3] - @named sys = ODESystem(D.(x) .~ x, t) + @named sys = System(D.(x) .~ x, t) @test_nowarn structural_simplify(sys) end @@ -487,7 +487,7 @@ sts = @variables x(t)[1:3]=[1, 2, 3.0] y(t)=1.0 ps = @parameters p[1:3] = [1, 2, 3] eqs = [collect(D.(x) .~ x) D(y) ~ norm(collect(x)) * y - x[1]] -@named sys = ODESystem(eqs, t, sts, ps) +@named sys = System(eqs, t, sts, ps) sys = structural_simplify(sys) @test isequal(@nonamespace(sys.x), x) @test isequal(@nonamespace(sys.y), y) @@ -511,14 +511,14 @@ function submodel(; name) @variables y(t) @parameters A[1:5] A = collect(A) - ODESystem(D(y) ~ sum(A) * y, t; name = name) + System(D(y) ~ sum(A) * y, t; name = name) end # Build system @named sys1 = submodel() @named sys2 = submodel() -@named sys = ODESystem([0 ~ sys1.y + sys2.y], t; systems = [sys1, sys2]) +@named sys = System([0 ~ sys1.y + sys2.y], t; systems = [sys1, sys2]) # DelayDiffEq using ModelingToolkit: hist @@ -526,7 +526,7 @@ using ModelingToolkit: hist xₜ₋₁ = hist(x, t - 1) eqs = [D(x) ~ x * y D(y) ~ y * x - xₜ₋₁] -@named sys = ODESystem(eqs, t) +@named sys = System(eqs, t) # register using StaticArrays @@ -537,8 +537,8 @@ foo(a, ms::AbstractVector) = a + sum(ms) @register_symbolic foo(a, ms::AbstractVector) @variables x(t) ms(t)[1:3] eqs = [D(x) ~ foo(x, ms); D(ms) ~ ones(3)] -@named sys = ODESystem(eqs, t, [x; ms], []) -@named emptysys = ODESystem(Equation[], t) +@named sys = System(eqs, t, [x; ms], []) +@named emptysys = System(Equation[], t) @mtkbuild outersys = compose(emptysys, sys) prob = ODEProblem( outersys, [outersys.sys.x => 1.0; collect(outersys.sys.ms .=> 1:3)], (0, 1.0)) @@ -552,8 +552,8 @@ bar(x, p) = p * x end @parameters p[1:3, 1:3] eqs = [D(x) ~ foo(x, ms); D(ms) ~ bar(ms, p)] -@named sys = ODESystem(eqs, t) -@named emptysys = ODESystem(Equation[], t) +@named sys = System(eqs, t) +@named emptysys = System(Equation[], t) @mtkbuild outersys = compose(emptysys, sys) prob = ODEProblem( outersys, [sys.x => 1.0, sys.ms => 1:3], (0.0, 1.0), [sys.p => ones(3, 3)]) @@ -564,13 +564,13 @@ obsfn = ModelingToolkit.build_explicit_observed_function( # x/x @variables x(t) -@named sys = ODESystem([D(x) ~ x / x], t) +@named sys = System([D(x) ~ x / x], t) @test equations(alias_elimination(sys)) == [D(x) ~ 1] # observed variable handling @variables x(t) RHS(t) @parameters τ -@named fol = ODESystem([D(x) ~ (1 - x) / τ], t; observed = [RHS ~ (1 - x) / τ]) +@named fol = System([D(x) ~ (1 - x) / τ], t; observed = [RHS ~ (1 - x) / τ]) @test isequal(RHS, @nonamespace fol.RHS) RHS2 = RHS @unpack RHS = fol @@ -586,13 +586,13 @@ eqs = [ z ~ α * x - β * y ] -@named sys = ODESystem(eqs, t, [x, y, z], [α, β]) +@named sys = System(eqs, t, [x, y, z], [α, β]) sys = complete(sys) @test_throws Any ODEFunction(sys) eqs = copy(eqs) eqs[end] = D(D(z)) ~ α * x - β * y -@named sys = ODESystem(eqs, t, [x, y, z], [α, β]) +@named sys = System(eqs, t, [x, y, z], [α, β]) sys = complete(sys) @test_throws Any ODEFunction(sys) @@ -644,7 +644,7 @@ sys = complete(sys) D(us[i]) ~ dummy_identity(buffer[i], us[i]) end - @named sys = ODESystem(eqs, t, us, ps; defaults = defs, preface = preface) + @named sys = System(eqs, t, us, ps; defaults = defs, preface = preface) sys = complete(sys) prob = ODEProblem(sys, [], (0.0, 1.0)) sol = solve(prob, Euler(); dt = 0.1) @@ -659,7 +659,7 @@ let eqs = [D(x[1]) ~ x[2] D(x[2]) ~ -x[1] - 0.5 * x[2] + k y ~ 0.9 * x[1] + x[2]] - @named sys = ODESystem(eqs, t, vcat(x, [y]), [k], defaults = Dict(x .=> 0)) + @named sys = System(eqs, t, vcat(x, [y]), [k], defaults = Dict(x .=> 0)) sys = structural_simplify(sys) u0 = [0.5, 0] @@ -705,7 +705,7 @@ let @parameters k1 k2::Int @variables A(t) eqs = [D(A) ~ -k1 * k2 * A] - @named sys = ODESystem(eqs, t) + @named sys = System(eqs, t) sys = complete(sys) u0map = [A => 1.0] pmap = (k1 => 1.0, k2 => 1) @@ -741,7 +741,7 @@ let D(p) ~ q / C 0 ~ q / C - R * F] - @named sys = ODESystem(eqs, t) + @named sys = System(eqs, t) @test length(equations(structural_simplify(sys))) == 2 end @@ -774,11 +774,11 @@ let D(z2) ~ y2 - beta * z2 # missing x2 term ] - @named sys1 = ODESystem(eqs, t) - @named sys2 = ODESystem(eqs2, t) - @named sys3 = ODESystem(eqs3, t) + @named sys1 = System(eqs, t) + @named sys2 = System(eqs2, t) + @named sys3 = System(eqs3, t) ssys3 = structural_simplify(sys3) - @named sys4 = ODESystem(eqs4, t) + @named sys4 = System(eqs4, t) @test ModelingToolkit.isisomorphic(sys1, sys2) @test !ModelingToolkit.isisomorphic(sys1, sys3) @@ -787,7 +787,7 @@ let # 1281 iv2 = only(independent_variables(sys2)) - @test isequal(only(independent_variables(convert_system(ODESystem, sys1, iv2))), iv2) + @test isequal(only(independent_variables(convert_system(System, sys1, iv2))), iv2) end let @@ -799,7 +799,7 @@ let sph ~ b spm ~ 0 sph ~ a] - @named sys = ODESystem(eqs, t, vars, pars) + @named sys = System(eqs, t, vars, pars) @test_throws ModelingToolkit.ExtraEquationsSystemException structural_simplify(sys) end @@ -823,7 +823,7 @@ let ps = [] - @named sys = ODESystem(eqs, t, u, ps) + @named sys = System(eqs, t, u, ps) @test_nowarn simpsys = structural_simplify(sys) sys = structural_simplify(sys) @@ -844,7 +844,7 @@ let @parameters k @variables A(t) eqs = [D(A) ~ -k * A] - @named osys = ODESystem(eqs, t) + @named osys = System(eqs, t) osys = complete(osys) oprob = ODEProblem(osys, [A => 1.0], (0.0, 10.0), [k => 1.0]; check_length = false) @test_nowarn sol = solve(oprob, Tsit5()) @@ -854,13 +854,13 @@ let function sys1(; name) vars = @variables x(t)=0.0 dx(t)=0.0 - ODESystem([D(x) ~ dx], t, vars, []; name, defaults = [D(x) => x]) + System([D(x) ~ dx], t, vars, []; name, defaults = [D(x) => x]) end function sys2(; name) @named s1 = sys1() - ODESystem(Equation[], t, [], []; systems = [s1], name) + System(Equation[], t, [], []; systems = [s1], name) end s1′ = sys1(; name = :s1) @@ -889,7 +889,7 @@ let @variables u(t) x(t) v(t) eqs = [u ~ kx * x + kv * v] - ODESystem(eqs, t; name) + System(eqs, t; name) end @named ctrl = pd_ctrl() @@ -900,7 +900,7 @@ let @variables u(t) x(t) v(t) eqs = [D(x) ~ v, D(v) ~ u] - ODESystem(eqs, t; name) + System(eqs, t; name) end @named sys = double_int() @@ -909,7 +909,7 @@ let connections = [sys.u ~ ctrl.u, ctrl.x ~ sys.x, ctrl.v ~ sys.v] - @named connected = ODESystem(connections, t) + @named connected = System(connections, t) @named sys_con = compose(connected, sys, ctrl) sys_simp = structural_simplify(sys_con) @@ -922,7 +922,7 @@ let @variables x(t) = 1 @variables y(t) = 1 @parameters pp = -1 - @named sys4 = ODESystem([D(x) ~ -y; D(y) ~ 1 + pp * y + x], t) + @named sys4 = System([D(x) ~ -y; D(y) ~ 1 + pp * y + x], t) sys4s = structural_simplify(sys4) prob = ODEProblem(sys4s, [x => 1.0, D(x) => 1.0], (0, 1.0)) @test string.(unknowns(prob.f.sys)) == ["x(t)", "y(t)"] @@ -935,7 +935,7 @@ let ∂t = D eqs = [∂t(Q) ~ 0.2P ∂t(P) ~ -80.0sin(Q)] - @test_throws ArgumentError @named sys = ODESystem(eqs, t) + @test_throws ArgumentError @named sys = System(eqs, t) end @parameters C L R @@ -945,12 +945,12 @@ eqs = [D(q) ~ -p / L - F D(p) ~ q / C 0 ~ q / C - R * F] testdict = Dict([:name => "test"]) -@named sys = ODESystem(eqs, t, metadata = testdict) +@named sys = System(eqs, t, metadata = testdict) @test get_metadata(sys) == testdict @variables P(t)=NaN Q(t)=NaN eqs = [D(Q) ~ 1 / sin(P), D(P) ~ log(-cos(Q))] -@named sys = ODESystem(eqs, t, [P, Q], []) +@named sys = System(eqs, t, [P, Q], []) sys = complete(debug_system(sys)) prob = ODEProblem(sys, [], (0.0, 1.0)) @test_throws "log(-cos(Q(t))) errors" prob.f([1, 0], prob.p, 0.0) @@ -961,7 +961,7 @@ let @variables y(t) = 1 @parameters pp = -1 der = Differential(t) - @named sys4 = ODESystem([der(x) ~ -y; der(y) ~ 1 + pp * y + x], t) + @named sys4 = System([der(x) ~ -y; der(y) ~ 1 + pp * y + x], t) sys4s = structural_simplify(sys4) prob = ODEProblem(sys4s, [x => 1.0, D(x) => 1.0], (0, 1.0)) @test !isnothing(prob.f.sys) @@ -969,15 +969,15 @@ end # SYS 1: vars_sub1 = @variables s1(t) -@named sub = ODESystem(Equation[], t, vars_sub1, []) +@named sub = System(Equation[], t, vars_sub1, []) vars1 = @variables x1(t) -@named sys1 = ODESystem(Equation[], t, vars1, [], systems = [sub]) -@named sys2 = ODESystem(Equation[], t, vars1, [], systems = [sys1, sub]) +@named sys1 = System(Equation[], t, vars1, [], systems = [sub]) +@named sys2 = System(Equation[], t, vars1, [], systems = [sys1, sub]) # SYS 2: Extension to SYS 1 vars_sub2 = @variables s2(t) -@named partial_sub = ODESystem(Equation[], t, vars_sub2, []) +@named partial_sub = System(Equation[], t, vars_sub2, []) @named sub = extend(partial_sub, sub) # no warnings for systems without events @@ -996,7 +996,7 @@ let # Issue https://github.com/SciML/ModelingToolkit.jl/issues/2322 eqs = [Dt(x) ~ -b * (x - z), 0 ~ z - c * x] - sys = ODESystem(eqs, t; name = :kjshdf) + sys = System(eqs, t; name = :kjshdf) sys_simp = structural_simplify(sys) @@ -1011,7 +1011,7 @@ end # Issue#2599 @variables x(t) y(t) eqs = [D(x) ~ x * t, y ~ 2x] -@mtkbuild sys = ODESystem(eqs, t; continuous_events = [[y ~ 3] => [x ~ 2]]) +@mtkbuild sys = System(eqs, t; continuous_events = [[y ~ 3] => [x ~ 2]]) prob = ODEProblem(sys, [x => 1.0], (0.0, 10.0)) @test_nowarn solve(prob, Tsit5()) @@ -1022,14 +1022,14 @@ prob = ODEProblem(sys, [x => 1.0], (0.0, 10.0)) eqs = [ D(x) ~ p * x ] - @mtkbuild sys = ODESystem( + @mtkbuild sys = System( eqs, t; continuous_events = [[norm(x) ~ 3.0] => [x ~ ones(3)]]) # array affect equations used to not work prob1 = @test_nowarn ODEProblem(sys, [x => ones(3)], (0.0, 10.0), [p => ones(3, 3)]) sol1 = @test_nowarn solve(prob1, Tsit5()) # array condition equations also used to not work - @mtkbuild sys = ODESystem( + @mtkbuild sys = System( eqs, t; continuous_events = [[x ~ sqrt(3) * ones(3)] => [x ~ ones(3)]]) # array affect equations used to not work prob2 = @test_nowarn ODEProblem(sys, [x => ones(3)], (0.0, 10.0), [p => ones(3, 3)]) @@ -1042,7 +1042,7 @@ end @test_skip begin @variables x(t)[1:3] y(t) @parameters p[1:3, 1:3] - @test_nowarn @mtkbuild sys = ODESystem([D(x) ~ p * x, D(y) ~ x' * p * x], t) + @test_nowarn @mtkbuild sys = System([D(x) ~ p * x, D(y) ~ x' * p * x], t) @test_nowarn ODEProblem(sys, [x => ones(3), y => 2], (0.0, 10.0), [p => ones(3, 3)]) end @@ -1054,7 +1054,7 @@ eqs = [D(D(q₁)) ~ -λ * q₁, q₁ ~ L * sin(θ), q₂ ~ L * cos(θ)] -@named pend = ODESystem(eqs, t) +@named pend = System(eqs, t) @test_nowarn generate_initializesystem( pend, u0map = [q₁ => 1.0, q₂ => 0.0], guesses = [λ => 1]) @@ -1066,7 +1066,7 @@ eqs = [D(D(x)) ~ σ * (y - x), D(y) ~ x * (ρ - z) - y, D(z) ~ x * y - β * z] -@mtkbuild sys = ODESystem(eqs, t) +@mtkbuild sys = System(eqs, t) u0 = [D(x) => 2.0, x => 1.0, @@ -1098,7 +1098,7 @@ function FML2(; name) eqs = [ D(x) ~ constant.output.u + k2[1] ] - ODESystem(eqs, t; systems, name) + System(eqs, t; systems, name) end @mtkbuild model = FML2() @@ -1114,7 +1114,7 @@ function RealExpression(; name, y) eqns = [ u ~ y ] - sys = ODESystem(eqns, t, vars, []; name) + sys = System(eqns, t, vars, []; name) end function RealExpressionSystem(; name) @@ -1125,7 +1125,7 @@ function RealExpressionSystem(; name) @named e1 = RealExpression(y = x) # This works perfectly. @named e2 = RealExpression(y = z[1]) # This bugs. However, `full_equations(e2)` works as expected. systems = [e1, e2] - ODESystem(Equation[], t, Iterators.flatten(vars), []; systems, name) + System(Equation[], t, Iterators.flatten(vars), []; systems, name) end @named sys = RealExpressionSystem() @@ -1138,8 +1138,8 @@ orig_vars = unknowns(sys) # Guesses in hierarchical systems @variables x(t) y(t) -@named sys = ODESystem(Equation[], t, [x], []; guesses = [x => 1.0]) -@named outer = ODESystem( +@named sys = System(Equation[], t, [x], []; guesses = [x => 1.0]) +@named outer = System( [D(y) ~ sys.x + t, 0 ~ t + y - sys.x * y], t, [y], []; systems = [sys]) @test ModelingToolkit.guesses(outer)[sys.x] == 1.0 outer = structural_simplify(outer) @@ -1150,9 +1150,9 @@ int = init(prob, Rodas4()) # Ensure indexes of array symbolics are cached appropriately @variables x(t)[1:2] -@named sys = ODESystem(Equation[], t, [x], []) +@named sys = System(Equation[], t, [x], []) sys1 = complete(sys) -@named sys = ODESystem(Equation[], t, [x...], []) +@named sys = System(Equation[], t, [x...], []) sys2 = complete(sys) for sys in [sys1, sys2] for (sym, idx) in [(x, 1:2), (x[1], 1), (x[2], 2)] @@ -1162,9 +1162,9 @@ for sys in [sys1, sys2] end @variables x(t)[1:2, 1:2] -@named sys = ODESystem(Equation[], t, [x], []) +@named sys = System(Equation[], t, [x], []) sys1 = complete(sys) -@named sys = ODESystem(Equation[], t, [x...], []) +@named sys = System(Equation[], t, [x...], []) sys2 = complete(sys) for sys in [sys1, sys2] @test is_variable(sys, x) @@ -1177,7 +1177,7 @@ end @testset "Non-1-indexed variable array (issue #2670)" begin @variables x(t)[0:1] # 0-indexed variable array - @named sys = ODESystem([x[0] ~ 0.0, D(x[1]) ~ x[0]], t, [x], []) + @named sys = System([x[0] ~ 0.0, D(x[1]) ~ x[0]], t, [x], []) @test_nowarn sys = structural_simplify(sys) @test equations(sys) == [D(x[1]) ~ 0.0] end @@ -1185,7 +1185,7 @@ end # Namespacing of array variables @testset "Namespacing of array variables" begin @variables x(t)[1:2] - @named sys = ODESystem(Equation[], t) + @named sys = System(Equation[], t) @test getname(unknowns(sys, x)) == :sys₊x @test size(unknowns(sys, x)) == size(x) end @@ -1194,7 +1194,7 @@ end @testset "ForwardDiff through ODEProblem constructor" begin @parameters P @variables x(t) - sys = structural_simplify(ODESystem([D(x) ~ P], t, [x], [P]; name = :sys)) + sys = structural_simplify(System([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]) @@ -1207,7 +1207,7 @@ end @testset "Inplace observed functions" begin @parameters P @variables x(t) - sys = structural_simplify(ODESystem([D(x) ~ P], t, [x], [P]; name = :sys)) + sys = structural_simplify(System([D(x) ~ P], t, [x], [P]; name = :sys)) obsfn = ModelingToolkit.build_explicit_observed_function( sys, [x + 1, x + P, x + t], return_inplace = true)[2] ps = ModelingToolkit.MTKParameters(sys, [P => 2.0]) @@ -1220,7 +1220,7 @@ end @testset "Custom independent variable" begin @independent_variables x @variables y(x) - @test_nowarn @named sys = ODESystem([y ~ 0], x) + @test_nowarn @named sys = System([y ~ 0], x) # the same, but with @mtkmodel @independent_variables x @@ -1235,7 +1235,7 @@ end @test_nowarn @mtkbuild sys = MyModel() @variables x y(x) - @test_logs (:warn,) @named sys = ODESystem([y ~ 0], x) + @test_logs (:warn,) @named sys = System([y ~ 0], x) @parameters T D = Differential(T) @@ -1243,7 +1243,7 @@ end eqs = [D(x) ~ 0.0] initialization_eqs = [x ~ T] guesses = [x => 0.0] - @named sys2 = ODESystem(eqs, T; initialization_eqs, guesses) + @named sys2 = System(eqs, T; initialization_eqs, guesses) prob2 = ODEProblem(structural_simplify(sys2), [], (1.0, 2.0), []) sol2 = solve(prob2) @test all(sol2[x] .== 1.0) @@ -1253,10 +1253,10 @@ end @testset "Extend systems with a field that can be nothing" begin A = Dict(:a => 1) B = Dict(:b => 2) - @named A1 = ODESystem(Equation[], t, [], []) - @named B1 = ODESystem(Equation[], t, [], []) - @named A2 = ODESystem(Equation[], t, [], []; metadata = A) - @named B2 = ODESystem(Equation[], t, [], []; metadata = B) + @named A1 = System(Equation[], t, [], []) + @named B1 = System(Equation[], t, [], []) + @named A2 = System(Equation[], t, [], []; metadata = A) + @named B2 = System(Equation[], t, [], []; metadata = B) @test ModelingToolkit.get_metadata(extend(A1, B1)) == nothing @test ModelingToolkit.get_metadata(extend(A1, B2)) == B @test ModelingToolkit.get_metadata(extend(A2, B1)) == A @@ -1268,7 +1268,7 @@ end @variables x(t) y(t) z(t) eqs = [D(x) ~ 0, y ~ x, D(z) ~ 0] defaults = [x => 1, z => y] - @named sys = ODESystem(eqs, t; defaults) + @named sys = System(eqs, t; defaults) ssys = structural_simplify(sys) prob = ODEProblem(ssys, [], (0.0, 1.0), []) @test prob[x] == prob[y] == prob[z] == 1.0 @@ -1277,7 +1277,7 @@ end @variables x(t) y(t) z(t) eqs = [D(x) ~ 0, y ~ y0 / x, D(z) ~ y] defaults = [y0 => 1, x => 1, z => y] - @named sys = ODESystem(eqs, t; defaults) + @named sys = System(eqs, t; defaults) ssys = structural_simplify(sys) prob = ODEProblem(ssys, [], (0.0, 1.0), []) @test prob[x] == prob[y] == prob[z] == 1.0 @@ -1286,7 +1286,7 @@ end @testset "Scalarized parameters in array functions" begin @variables u(t)[1:2] x(t)[1:2] o(t)[1:2] @parameters p[1:2, 1:2] [tunable = false] - @named sys = ODESystem( + @named sys = System( [D(u) ~ (sum(u) + sum(x) + sum(p) + sum(o)) * x, o ~ prod(u) * x], t, [u..., x..., o...], [p...]) sys1 = structural_simplify(sys, inputs = [x...], outputs = []) @@ -1347,7 +1347,7 @@ end @testset "Independent variable as system property" begin @variables x(t) - @named sys = ODESystem([x ~ t], t) + @named sys = System([x ~ t], t) @named sys = compose(sys, sys) # nest into a hierarchical system @test t === sys.t === sys.sys.t end @@ -1355,7 +1355,7 @@ end @testset "Substituting preserves parameter dependencies, defaults, guesses" begin @parameters p1 p2 @variables x(t) y(t) - @named sys = ODESystem([D(x) ~ y + p2], t; parameter_dependencies = [p2 ~ 2p1], + @named sys = System([D(x) ~ y + p2], t; parameter_dependencies = [p2 ~ 2p1], defaults = [p1 => 1.0, p2 => 2.0], guesses = [p1 => 2.0, p2 => 3.0]) @parameters p3 sys2 = substitute(sys, [p1 => p3]) @@ -1371,10 +1371,10 @@ end @testset "Substituting with nested systems" begin @parameters p1 p2 @variables x(t) y(t) - @named innersys = ODESystem([D(x) ~ y + p2], t; parameter_dependencies = [p2 ~ 2p1], + @named innersys = System([D(x) ~ y + p2], t; parameter_dependencies = [p2 ~ 2p1], defaults = [p1 => 1.0, p2 => 2.0], guesses = [p1 => 2.0, p2 => 3.0]) @parameters p3 p4 - @named outersys = ODESystem( + @named outersys = System( [D(innersys.y) ~ innersys.y + p4], t; parameter_dependencies = [p4 ~ 3p3], defaults = [p3 => 3.0, p4 => 9.0], guesses = [p4 => 10.0], systems = [innersys]) @test_nowarn structural_simplify(outersys) @@ -1401,7 +1401,7 @@ end o[1] ~ sum(p) * sum(u) o[2] ~ sum(p) * sum(x)] - @named sys = ODESystem(eqs, t, [u..., x..., o], [p...]) + @named sys = System(eqs, t, [u..., x..., o], [p...]) sys1 = structural_simplify(sys, inputs = [x...], outputs = [o...], split = false) @test_nowarn ModelingToolkit.build_explicit_observed_function(sys1, u; inputs = [x...]) @@ -1414,17 +1414,17 @@ end @testset "Passing `nothing` to `u0`" begin @variables x(t) = 1 - @mtkbuild sys = ODESystem(D(x) ~ t, t) + @mtkbuild sys = System(D(x) ~ t, t) prob = @test_nowarn ODEProblem(sys, nothing, (0.0, 1.0)) @test_nowarn solve(prob) end @testset "ODEs are not DDEs" begin @variables x(t) - @named sys = ODESystem(D(x) ~ x, t) + @named sys = System(D(x) ~ x, t) @test !ModelingToolkit.is_dde(sys) @test is_markovian(sys) - @named sys2 = ODESystem(Equation[], t; systems = [sys]) + @named sys2 = System(Equation[], t; systems = [sys]) @test !ModelingToolkit.is_dde(sys) @test is_markovian(sys) end @@ -1434,7 +1434,7 @@ end for eqs in [D(x) ~ x, collect(D(x) .~ x)] for dvs in [[x], collect(x)] - @named sys = ODESystem(eqs, t, dvs, []) + @named sys = System(eqs, t, dvs, []) sys = complete(sys) if eqs isa Vector && length(eqs) == 2 && length(dvs) == 2 @test_nowarn ODEProblem(sys, [], (0.0, 1.0)) @@ -1447,7 +1447,7 @@ end end for eqs in [[D(x) ~ x, D(y) ~ y], [collect(D(x) .~ x); D(y) ~ y]] for dvs in [[x, y], [x..., y]] - @named sys = ODESystem(eqs, t, dvs, []) + @named sys = System(eqs, t, dvs, []) sys = complete(sys) if eqs isa Vector && length(eqs) == 3 && length(dvs) == 3 @test_nowarn ODEProblem(sys, [], (0.0, 1.0)) @@ -1462,13 +1462,13 @@ end @testset "Parameter dependencies with constant RHS" begin @parameters p - @test_nowarn ODESystem(Equation[], t; parameter_dependencies = [p ~ 1.0], name = :a) + @test_nowarn System(Equation[], t; parameter_dependencies = [p ~ 1.0], name = :a) end @testset "Variable discovery in arrays of `Num` inside callable symbolic" begin @variables x(t) y(t) @parameters foo(::AbstractVector) - sys = @test_nowarn ODESystem(D(x) ~ foo([x, 2y]), t; name = :sys) + sys = @test_nowarn System(D(x) ~ foo([x, 2y]), t; name = :sys) @test length(unknowns(sys)) == 2 @test any(isequal(y), unknowns(sys)) end @@ -1476,7 +1476,7 @@ end @testset "Inplace observed" begin @variables x(t) @parameters p[1:2] q - @mtkbuild sys = ODESystem(D(x) ~ sum(p) * x + q * t, t) + @mtkbuild sys = System(D(x) ~ sum(p) * x + q * t, t) prob = ODEProblem(sys, [x => 1.0], (0.0, 1.0), [p => ones(2), q => 2]) obsfn = ModelingToolkit.build_explicit_observed_function( sys, [p..., q], return_inplace = true)[2] @@ -1516,7 +1516,7 @@ end @testset "`complete` with `split = false` removes the index cache" begin @variables x(t) @parameters p - @mtkbuild sys = ODESystem(D(x) ~ p * t, t) + @mtkbuild sys = System(D(x) ~ p * t, t) @test ModelingToolkit.get_index_cache(sys) !== nothing sys2 = complete(sys; split = false) @test ModelingToolkit.get_index_cache(sys2) === nothing @@ -1526,7 +1526,7 @@ end @testset "Observed variables dependent on discrete parameters" begin @variables x(t) obs(t) @parameters c(t) - @mtkbuild sys = ODESystem([D(x) ~ c * cos(x), obs ~ c], + @mtkbuild sys = System([D(x) ~ c * cos(x), obs ~ c], t, [x], [c]; @@ -1540,7 +1540,7 @@ end @testset "DAEProblem with array parameters" begin @variables x(t)=1.0 y(t) [guess = 1.0] @parameters p[1:2] = [1.0, 2.0] - @mtkbuild sys = ODESystem([D(x) ~ x, y^2 ~ x + sum(p)], t) + @mtkbuild sys = System([D(x) ~ x, y^2 ~ x + sum(p)], t) prob = DAEProblem(sys, [D(x) => x, D(y) => D(x) / 2y], [], (0.0, 1.0)) sol = solve(prob, DFBDF(), abstol = 1e-8, reltol = 1e-8) @test sol[x]≈sol[y^2 - sum(p)] atol=1e-5 @@ -1549,7 +1549,7 @@ end @testset "Symbolic tstops" begin @variables x(t) = 1.0 @parameters p=0.15 q=0.25 r[1:2]=[0.35, 0.45] - @mtkbuild sys = ODESystem( + @mtkbuild sys = System( [D(x) ~ p * x + q * t + sum(r)], t; tstops = [0.5p, [0.1, 0.2], [p + 2q], r]) prob = ODEProblem(sys, [], (0.0, 5.0)) sol = solve(prob) @@ -1561,7 +1561,7 @@ end @test all(x -> any(isapprox(x, atol = 1e-6), sol2.t), expected_tstops) @variables y(t) [guess = 1.0] - @mtkbuild sys = ODESystem([D(x) ~ p * x + q * t + sum(r), y^3 ~ 2x + 1], + @mtkbuild sys = System([D(x) ~ p * x + q * t + sum(r), y^3 ~ 2x + 1], t; tstops = [0.5p, [0.1, 0.2], [p + 2q], r]) prob = DAEProblem( sys, [D(y) => 2D(x) / 3y^2, D(x) => p * x + q * t + sum(r)], [], (0.0, 5.0)) @@ -1578,14 +1578,14 @@ end @parameters p d @variables X(t)::Int64 eq = D(X) ~ p - d * X - @test_throws ArgumentError @mtkbuild osys = ODESystem([eq], t) + @test_throws ArgumentError @mtkbuild osys = System([eq], t) @variables Y(t)[1:3]::String eq = D(Y) ~ [p, p, p] - @test_throws ArgumentError @mtkbuild osys = ODESystem([eq], t) + @test_throws ArgumentError @mtkbuild osys = System([eq], t) @variables X(t)::Complex eq = D(X) ~ p - d * X - @test_nowarn @named osys = ODESystem([eq], t) + @test_nowarn @named osys = System([eq], t) end # Test `isequal` @@ -1594,31 +1594,31 @@ end @parameters p d(t) eq = D(X) ~ p - d * X - osys1 = complete(ODESystem([eq], t; name = :osys)) - osys2 = complete(ODESystem([eq], t; name = :osys)) + osys1 = complete(System([eq], t; name = :osys)) + osys2 = complete(System([eq], t; name = :osys)) @test osys1 == osys2 # true continuous_events = [[X ~ 1.0] => [X ~ Pre(X) + 5.0]] discrete_events = [SymbolicDiscreteCallback( 5.0 => [d ~ d / 2.0], discrete_parameters = [d])] - osys1 = complete(ODESystem([eq], t; name = :osys, continuous_events)) - osys2 = complete(ODESystem([eq], t; name = :osys)) + osys1 = complete(System([eq], t; name = :osys, continuous_events)) + osys2 = complete(System([eq], t; name = :osys)) @test osys1 !== osys2 - osys1 = complete(ODESystem([eq], t; name = :osys, discrete_events)) - osys2 = complete(ODESystem([eq], t; name = :osys)) + osys1 = complete(System([eq], t; name = :osys, discrete_events)) + osys2 = complete(System([eq], t; name = :osys)) @test osys1 !== osys2 - osys1 = complete(ODESystem([eq], t; name = :osys, continuous_events)) - osys2 = complete(ODESystem([eq], t; name = :osys, discrete_events)) + osys1 = complete(System([eq], t; name = :osys, continuous_events)) + osys2 = complete(System([eq], t; name = :osys, discrete_events)) @test osys1 !== osys2 end @testset "dae_order_lowering basic test" begin @parameters a @variables x(t) y(t) z(t) - @named dae_sys = ODESystem([ + @named dae_sys = System([ D(x) ~ y, 0 ~ x + z, 0 ~ x - y + z @@ -1654,7 +1654,7 @@ end D(x) => 0.0, x => 10.0, y => 0.0, z => 10.0 ] default_p = [M => 1.0, b => 1.0, k => 1.0] - @named dae_sys = ODESystem(eqs, t, [x, y, z], ps; defaults = [default_u0; default_p]) + @named dae_sys = System(eqs, t, [x, y, z], ps; defaults = [default_u0; default_p]) simplified_dae_sys = structural_simplify(dae_sys) @@ -1679,32 +1679,32 @@ end cons = [x(0.3) ~ c * d, y(0.7) ~ 3] # Test variables + parameters infer correctly. - @mtkbuild sys = ODESystem(eqs, t; constraints = cons) + @mtkbuild sys = System(eqs, t; constraints = cons) @test issetequal(parameters(sys), [a, c, d, e]) @test issetequal(unknowns(sys), [x(t), y(t), z(t)]) @parameters t_c cons = [x(t_c) ~ 3] - @mtkbuild sys = ODESystem(eqs, t; constraints = cons) + @mtkbuild sys = System(eqs, t; constraints = cons) @test issetequal(parameters(sys), [a, e, t_c]) @parameters g(..) h i cons = [g(h, i) * x(3) ~ c] - @mtkbuild sys = ODESystem(eqs, t; constraints = cons) + @mtkbuild sys = System(eqs, t; constraints = cons) @test issetequal(parameters(sys), [g, h, i, a, e, c]) # Test that bad constraints throw errors. cons = [x(3, 4) ~ 3] # unknowns cannot have multiple args. - @test_throws ArgumentError @mtkbuild sys = ODESystem(eqs, t; constraints = cons) + @test_throws ArgumentError @mtkbuild sys = System(eqs, t; constraints = cons) cons = [x(y(t)) ~ 2] # unknown arg must be parameter, value, or t - @test_throws ArgumentError @mtkbuild sys = ODESystem(eqs, t; constraints = cons) + @test_throws ArgumentError @mtkbuild sys = System(eqs, t; constraints = cons) @variables u(t) v cons = [x(t) * u ~ 3] - @test_throws ArgumentError @mtkbuild sys = ODESystem(eqs, t; constraints = cons) + @test_throws ArgumentError @mtkbuild sys = System(eqs, t; constraints = cons) cons = [x(t) * v ~ 3] - @test_throws ArgumentError @mtkbuild sys = ODESystem(eqs, t; constraints = cons) # Need time argument. + @test_throws ArgumentError @mtkbuild sys = System(eqs, t; constraints = cons) # Need time argument. # Test array variables @variables x(..)[1:5] @@ -1715,13 +1715,13 @@ end 0 0 2 0 5] eqs = D(x(t)) ~ mat * x(t) cons = [x(3) ~ [2, 3, 3, 5, 4]] - @mtkbuild ode = ODESystem(D(x(t)) ~ mat * x(t), t; constraints = cons) + @mtkbuild ode = System(D(x(t)) ~ mat * x(t), t; constraints = cons) @test length(constraints(ModelingToolkit.get_constraintsystem(ode))) == 5 end @testset "`build_explicit_observed_function` with `expression = true` returns `Expr`" begin @variables x(t) - @mtkbuild sys = ODESystem(D(x) ~ 2x, t) + @mtkbuild sys = System(D(x) ~ 2x, t) obsfn_expr = ModelingToolkit.build_explicit_observed_function( sys, 2x + 1, expression = true) @test obsfn_expr isa Expr @@ -1739,7 +1739,7 @@ end D(y) ~ x * (ρ - z) - y, D(z) ~ x * y - β * z] - @mtkbuild sys=ODESystem(eqs, t) split=false + @mtkbuild sys=System(eqs, t) split=false u0 = SA[D(x) => 2.0f0, x => 1.0f0, @@ -1763,17 +1763,17 @@ end @test scope isa ParentScope @test scope.parent isa ParentScope @test scope.parent.parent isa LocalScope - return ODESystem(D(x) ~ var1, t; name) + return System(D(x) ~ var1, t; name) end function SysB(; name, var1) @variables x(t) @named subsys = SysA(; var1) - return ODESystem(D(x) ~ x, t; systems = [subsys], name) + return System(D(x) ~ x, t; systems = [subsys], name) end function SysC(; name) @variables x(t) @named subsys = SysB(; var1 = x) - return ODESystem(D(x) ~ x, t; systems = [subsys], name) + return System(D(x) ~ x, t; systems = [subsys], name) end @mtkbuild sys = SysC() @test length(unknowns(sys)) == 3 diff --git a/test/parameter_dependencies.jl b/test/parameter_dependencies.jl index bdaa2a391b..6f4f56c140 100644 --- a/test/parameter_dependencies.jl +++ b/test/parameter_dependencies.jl @@ -20,7 +20,7 @@ using NonlinearSolve cb2 = [x ~ 4.0] => (affect1!, [], [p1, p2], [p1]) # triggers at t=-2+√7 cb3 = SymbolicDiscreteCallback([1.0] => [p1 ~ 5.0], discrete_parameters = [p1]) - @mtkbuild sys = ODESystem( + @mtkbuild sys = System( [D(x) ~ p1 * t + p2], t; parameter_dependencies = [p2 => 2p1], @@ -54,7 +54,7 @@ end @parameters p1[1:2]=[1.0, 2.0] p2[1:2]=[0.0, 0.0] @variables x(t) = 0 - @named sys = ODESystem( + @named sys = System( [D(x) ~ sum(p1) * t + sum(p2)], t; parameter_dependencies = [p2 => 2p1] @@ -73,11 +73,11 @@ end @parameters p1=1.0 p2=1.0 @variables x(t) = 0 - @mtkbuild sys1 = ODESystem( + @mtkbuild sys1 = System( [D(x) ~ p1 * t + p2], t ) - @named sys2 = ODESystem( + @named sys2 = System( [], t; parameter_dependencies = [p2 => 2p1] @@ -94,7 +94,7 @@ end @parameters p1=1.0 p2=1.0 @variables x(t) = 0 - @named sys = ODESystem( + @named sys = System( [D(x) ~ p1 * t + p2], t; parameter_dependencies = [p2 => 2p1] @@ -108,7 +108,7 @@ end @parameters p1[1:2]=[1.0, 2.0] p2[1:2]=[0.0, 0.0] @variables x(t) = 0 - @named sys = ODESystem( + @named sys = System( [D(x) ~ sum(p1) * t + sum(p2)], t; parameter_dependencies = [p2 => 2p1] @@ -122,16 +122,16 @@ end @parameters p1=1.0 p2=2.0 @variables x(t) = 0 - @named sys1 = ODESystem( + @named sys1 = System( [D(x) ~ p1 * t + p2], t ) - @named sys2 = ODESystem( + @named sys2 = System( [D(x) ~ p1 * t - p2], t; parameter_dependencies = [p2 => 2p1] ) - sys = complete(ODESystem([], t, systems = [sys1, sys2], name = :sys)) + sys = complete(System([], t, systems = [sys1, sys2], name = :sys)) prob = ODEProblem(sys) v1 = sys.sys2.p2 @@ -159,12 +159,12 @@ end @parameters p2 @variables x(t) = 1.0 eqs = [D(x) ~ p2] - ODESystem(eqs, t, [x], [p2]; name) + System(eqs, t, [x], [p2]; name) end @parameters p1 = 1.0 parameter_dependencies = [sys2.p2 ~ p1 * 2.0] - sys1 = ODESystem( + sys1 = System( Equation[], t, [], [p1]; parameter_dependencies, name = :sys1, systems = [sys2]) # ensure that parameter_dependencies is type stable @@ -191,7 +191,7 @@ end @parameters p=2 (i::CallableFoo)(..) eqs = [D(y) ~ i(t) + p] - @named model = ODESystem(eqs, t, [y], [p, i]; + @named model = System(eqs, t, [y], [p, i]; parameter_dependencies = [i ~ CallableFoo(p)]) sys = structural_simplify(model) @@ -215,7 +215,7 @@ end D(x) ~ -x + u y ~ x z(k) ~ z(k - 2) + yd(k - 2)] - @test_throws ModelingToolkit.HybridSystemNotSupportedException @mtkbuild sys = ODESystem( + @test_throws ModelingToolkit.HybridSystemNotSupportedException @mtkbuild sys = System( eqs, t; parameter_dependencies = [kq => 2kp]) @test_skip begin @@ -225,7 +225,7 @@ end yd(k - 2) => 2.0]) @test_nowarn solve(prob, Tsit5()) - @mtkbuild sys = ODESystem(eqs, t; parameter_dependencies = [kq => 2kp], + @mtkbuild sys = System(eqs, t; parameter_dependencies = [kq => 2kp], discrete_events = [SymbolicDiscreteCallback( [0.5] => [kp ~ 2.0], discrete_parameters = [kp])]) prob = ODEProblem(sys, [x => 0.0, y => 0.0], (0.0, Tf), @@ -258,7 +258,7 @@ end 0.1 * y, 0.1 * z] - @named sys = ODESystem(eqs, t) + @named sys = System(eqs, t) @named sdesys = SDESystem(sys, noiseeqs; parameter_dependencies = [ρ => 2σ]) sdesys = complete(sdesys) @test !(ρ in Set(parameters(sdesys))) @@ -269,7 +269,7 @@ end @test prob.ps[ρ] == 2prob.ps[σ] @test_nowarn solve(prob, SRIW1()) - @named sys = ODESystem(eqs, t) + @named sys = System(eqs, t) @named sdesys = SDESystem(sys, noiseeqs; parameter_dependencies = [ρ => 2σ], discrete_events = [SymbolicDiscreteCallback( [10.0] => [σ ~ 15.0], discrete_parameters = [σ])]) @@ -350,7 +350,7 @@ end cb2 = [x ~ 4.0] => (affect1!, [], [p1, p2], [p1]) # triggers at t=-2+√7 cb3 = [1.0] => [p1 ~ 5.0] - @mtkbuild sys = ODESystem( + @mtkbuild sys = System( [D(x) ~ p1 * t + p2], t; parameter_dependencies = [p2 => 2p1] @@ -381,7 +381,7 @@ end @testset "Discovery of parameters from dependencies" begin @parameters p1 p2 @variables x(t) y(t) - @named sys = ODESystem([D(x) ~ y + p2], t; parameter_dependencies = [p2 ~ 2p1]) + @named sys = System([D(x) ~ y + p2], t; parameter_dependencies = [p2 ~ 2p1]) @test is_parameter(sys, p1) @named sys = NonlinearSystem([x * y^2 ~ y + p2]; parameter_dependencies = [p2 ~ 2p1]) @test is_parameter(sys, p1) diff --git a/test/precompile_test/ODEPrecompileTest.jl b/test/precompile_test/ODEPrecompileTest.jl index 81187c4075..911f45152e 100644 --- a/test/precompile_test/ODEPrecompileTest.jl +++ b/test/precompile_test/ODEPrecompileTest.jl @@ -13,7 +13,7 @@ function system(; kwargs...) D(y) ~ x * (ρ - z) - y, D(z) ~ x * y - β * z] - @named de = ODESystem(eqs, t) + @named de = System(eqs, t) de = complete(de) return ODEFunction(de, [x, y, z], [σ, ρ, β]; kwargs...) end diff --git a/test/problem_validation.jl b/test/problem_validation.jl index a0a7afaf3c..3db151bda5 100644 --- a/test/problem_validation.jl +++ b/test/problem_validation.jl @@ -6,7 +6,7 @@ using ModelingToolkit: t_nounits as t, D_nounits as D @variables X(t) @parameters p d eqs = [D(X) ~ p - d * X] - @mtkbuild osys = ODESystem(eqs, t) + @mtkbuild osys = System(eqs, t) p = "I accidentally renamed p" u0 = [X => 1.0] @@ -23,7 +23,7 @@ using ModelingToolkit: t_nounits as t, D_nounits as D @variables x(t) y(t) z(t) @parameters a b c d eqs = [D(x) ~ x * a, D(y) ~ y * c, D(z) ~ b + d] - @mtkbuild sys = ODESystem(eqs, t) + @mtkbuild sys = System(eqs, t) pmap = [a => 1, b => 2, c => 3, d => 4, "b" => 2] u0map = [x => 1, y => 2, z => 3] @test_throws InvalidKeyError ODEProblem(sys, u0map, (0.0, 1.0), pmap) diff --git a/test/reduction.jl b/test/reduction.jl index a692dbff75..7e988a2d55 100644 --- a/test/reduction.jl +++ b/test/reduction.jl @@ -28,7 +28,7 @@ eqs = [D(x) ~ σ * (y - x) 0 ~ a + z u ~ z + a] -lorenz1 = ODESystem(eqs, t, name = :lorenz1) +lorenz1 = System(eqs, t, name = :lorenz1) lorenz1_aliased = structural_simplify(lorenz1) io = IOBuffer(); @@ -53,13 +53,13 @@ eqs1 = [ u ~ x + y - z ] -lorenz = name -> ODESystem(eqs1, t, name = name) +lorenz = name -> System(eqs1, t, name = name) lorenz1 = lorenz(:lorenz1) state = TearingState(lorenz1) @test isempty(setdiff(state.fullvars, [D(x), F, y, x, D(y), u, z, D(z)])) lorenz2 = lorenz(:lorenz2) -@named connected = ODESystem( +@named connected = System( [s ~ a + lorenz1.x lorenz2.y ~ s lorenz1.u ~ lorenz2.F @@ -125,13 +125,13 @@ prob2 = SteadyStateProblem(reduced_system, u0, pp) let @variables x(t) u(t) y(t) @parameters a b c d - ol = ODESystem([D(x) ~ a * x + b * u; y ~ c * x + d * u], t, name = :ol) + ol = System([D(x) ~ a * x + b * u; y ~ c * x + d * u], t, name = :ol) @variables u_c(t) y_c(t) @parameters k_P - pc = ODESystem(Equation[u_c ~ k_P * y_c], t, name = :pc) + pc = System(Equation[u_c ~ k_P * y_c], t, name = :pc) connections = [pc.u_c ~ ol.u pc.y_c ~ ol.y] - @named connected = ODESystem(connections, t, systems = [ol, pc]) + @named connected = System(connections, t, systems = [ol, pc]) @test equations(connected) isa Vector{Equation} reduced_sys = structural_simplify(connected) ref_eqs = [D(ol.x) ~ ol.a * ol.x + ol.b * ol.u @@ -142,7 +142,7 @@ end # issue #889 let @variables x(t) - @named sys = ODESystem([0 ~ D(x) + x], t, [x], []) + @named sys = System([0 ~ D(x) + x], t, [x], []) #@test_throws ModelingToolkit.InvalidSystemException ODEProblem(sys, [1.0], (0, 10.0)) sys = structural_simplify(sys) #@test_nowarn ODEProblem(sys, [1.0], (0, 10.0)) @@ -188,7 +188,7 @@ eqs = [D(E) ~ k₋₁ * C - k₁ * E * S D(P) ~ k₂ * C E₀ ~ E + C] -@named sys = ODESystem(eqs, t, [E, C, S, P], [k₁, k₂, k₋₁, E₀]) +@named sys = System(eqs, t, [E, C, S, P], [k₁, k₂, k₋₁, E₀]) @test_throws ModelingToolkit.ExtraEquationsSystemException structural_simplify(sys) # Example 5 from Pantelides' original paper @@ -197,7 +197,7 @@ sts = collect(@variables x(t) u1(t) u2(t)) eqs = [0 ~ x + sin(u1 + u2) D(x) ~ x + y1 cos(x) ~ sin(y2)] -@named sys = ODESystem(eqs, t, sts, params) +@named sys = System(eqs, t, sts, params) @test_throws ModelingToolkit.InvalidSystemException structural_simplify(sys) # issue #963 @@ -214,7 +214,7 @@ eq = [v47 ~ v1 0 ~ i74 + i75 - i64 0 ~ i64 + i71] -@named sys0 = ODESystem(eq, t) +@named sys0 = System(eq, t) sys = structural_simplify(sys0) @test length(equations(sys)) == 1 eq = equations(tearing_substitution(sys))[1] @@ -232,7 +232,7 @@ eqs = [D(x) ~ σ * (y - x) 0 ~ a + z u ~ z + a] -lorenz1 = ODESystem(eqs, t, name = :lorenz1) +lorenz1 = System(eqs, t, name = :lorenz1) lorenz1_reduced = structural_simplify(lorenz1, inputs = [z], outputs = []) @test z in Set(parameters(lorenz1_reduced)) @@ -241,7 +241,7 @@ vars = @variables x(t) y(t) z(t) eqs = [D(x) ~ x D(y) ~ y D(z) ~ t] -@named model = ODESystem(eqs, t) +@named model = System(eqs, t) sys = structural_simplify(model) Js = ModelingToolkit.jacobian_sparsity(sys) @test size(Js) == (3, 3) @@ -252,29 +252,29 @@ vars = @variables a(t) w(t) phi(t) eqs = [a ~ D(w) w ~ D(phi) w ~ sin(t)] -@named sys = ODESystem(eqs, t, vars, []) +@named sys = System(eqs, t, vars, []) ss = alias_elimination(sys) @test isempty(observed(ss)) @variables x(t) y(t) -@named sys = ODESystem([D(x) ~ 1 - x, +@named sys = System([D(x) ~ 1 - x, D(y) + D(x) ~ 0], t) new_sys = alias_elimination(sys) @test isempty(observed(new_sys)) -@named sys = ODESystem([D(x) ~ x, +@named sys = System([D(x) ~ x, D(y) + D(x) ~ 0], t) new_sys = alias_elimination(sys) @test isempty(observed(new_sys)) -@named sys = ODESystem([D(x) ~ 1 - x, +@named sys = System([D(x) ~ 1 - x, y + D(x) ~ 0], t) new_sys = alias_elimination(sys) @test isempty(observed(new_sys)) eqs = [x ~ 0 D(x) ~ x + y] -@named sys = ODESystem(eqs, t, [x, y], []) +@named sys = System(eqs, t, [x, y], []) ss = structural_simplify(sys) @test isempty(equations(ss)) @test sort(string.(observed(ss))) == ["x(t) ~ 0.0" @@ -282,7 +282,7 @@ ss = structural_simplify(sys) "y(t) ~ xˍt(t) - x(t)"] eqs = [D(D(x)) ~ -x] -@named sys = ODESystem(eqs, t, [x], []) +@named sys = System(eqs, t, [x], []) ss = alias_elimination(sys) @test length(equations(ss)) == length(unknowns(ss)) == 1 ss = structural_simplify(sys) diff --git a/test/scc_nonlinear_problem.jl b/test/scc_nonlinear_problem.jl index ac7978d269..f98f588032 100644 --- a/test/scc_nonlinear_problem.jl +++ b/test/scc_nonlinear_problem.jl @@ -226,7 +226,7 @@ import ModelingToolkitStandardLibrary.Hydraulic.IsothermalCompressible as IC f ~ p * area m ~ rho * x * area] - return ODESystem(eqs, t, vars, pars; name, systems) + return System(eqs, t, vars, pars; name, systems) end systems = @named begin @@ -255,7 +255,7 @@ import ModelingToolkitStandardLibrary.Hydraulic.IsothermalCompressible as IC initialization_eqs = [mass.s ~ 0.0 mass.v ~ 0.0] - @mtkbuild sys = ODESystem(eqs, t, [], []; systems, initialization_eqs) + @mtkbuild sys = System(eqs, t, [], []; systems, initialization_eqs) prob = ODEProblem(sys, [], (0, 5)) sol = solve(prob) @test SciMLBase.successful_retcode(sol) diff --git a/test/sciml_problem_inputs.jl b/test/sciml_problem_inputs.jl index bfa560cda3..deae4b4772 100644 --- a/test/sciml_problem_inputs.jl +++ b/test/sciml_problem_inputs.jl @@ -38,7 +38,7 @@ begin ] # Create systems (without structural_simplify, since that might modify systems to affect intended tests). - osys = complete(ODESystem(diff_eqs, t; name = :osys)) + osys = complete(System(diff_eqs, t; name = :osys)) ssys = complete(SDESystem( diff_eqs, noise_eqs, t, [X, Y, Z], [kp, kd, k1, k2]; name = :ssys)) jsys = complete(JumpSystem(jumps, t, [X, Y, Z], [kp, kd, k1, k2]; name = :jsys)) diff --git a/test/sdesystem.jl b/test/sdesystem.jl index b031a2f5ab..96251ceb00 100644 --- a/test/sdesystem.jl +++ b/test/sdesystem.jl @@ -17,8 +17,8 @@ noiseeqs = [0.1 * x, 0.1 * y, 0.1 * z] -# ODESystem -> SDESystem shorthand constructor -@named sys = ODESystem(eqs, tt, [x, y, z], [σ, ρ, β]) +# System -> SDESystem shorthand constructor +@named sys = System(eqs, tt, [x, y, z], [σ, ρ, β]) @test SDESystem(sys, noiseeqs, name = :foo) isa SDESystem @named de = SDESystem(eqs, noiseeqs, tt, [x, y, z], [σ, ρ, β], tspan = (0.0, 10.0)) @@ -510,7 +510,7 @@ noiseeqs = [0.1 * x] @test observed(de) == [weight ~ x * 10] @test sol[weight] == 10 * sol[x] - @named ode = ODESystem(eqs, tt, [x], [α, β], observed = [weight ~ x * 10]) + @named ode = System(eqs, tt, [x], [α, β], observed = [weight ~ x * 10]) ode = complete(ode) odeprob = ODEProblem(ode, u0map, (0.0, 1.0), parammap) solode = solve(odeprob, Tsit5()) @@ -810,13 +810,13 @@ end @test prob[z] ≈ 2.0 end -@testset "SDESystem to ODESystem" begin +@testset "SDESystem to System" begin @variables x(t) y(t) z(t) @testset "Scalar noise" begin @named sys = SDESystem([D(x) ~ x, D(y) ~ y, z ~ x + y], [x, y, 3], t, [x, y, z], [], is_scalar_noise = true) - odesys = ODESystem(sys) - @test odesys isa ODESystem + odesys = System(sys) + @test odesys isa System vs = ModelingToolkit.vars(equations(odesys)) nbrownian = count( v -> ModelingToolkit.getvariabletype(v) == ModelingToolkit.BROWNIAN, vs) @@ -830,8 +830,8 @@ end @testset "Non-scalar vector noise" begin @named sys = SDESystem([D(x) ~ x, D(y) ~ y, z ~ x + y], [x, y, 0], t, [x, y, z], [], is_scalar_noise = false) - odesys = ODESystem(sys) - @test odesys isa ODESystem + odesys = System(sys) + @test odesys isa System vs = ModelingToolkit.vars(equations(odesys)) nbrownian = count( v -> ModelingToolkit.getvariabletype(v) == ModelingToolkit.BROWNIAN, vs) @@ -847,8 +847,8 @@ end 2y 2z 2x z+1 x+1 y+1] @named sys = SDESystem([D(x) ~ x, D(y) ~ y, D(z) ~ z], noiseeqs, t, [x, y, z], []) - odesys = ODESystem(sys) - @test odesys isa ODESystem + odesys = System(sys) + @test odesys isa System vs = ModelingToolkit.vars(equations(odesys)) nbrownian = count( v -> ModelingToolkit.getvariabletype(v) == ModelingToolkit.BROWNIAN, vs) @@ -892,7 +892,7 @@ end 0.1 * y, 0.1 * z] - @named sys = ODESystem(eqs, tt, [x, y, z], [σ, ρ, β]) + @named sys = System(eqs, tt, [x, y, z], [σ, ρ, β]) @named de = SDESystem(eqs, noiseeqs, tt, [x, y, z], [σ, ρ, β], tspan = (0.0, 10.0)) de = complete(de) diff --git a/test/serialization.jl b/test/serialization.jl index ee380b6616..cce015c776 100644 --- a/test/serialization.jl +++ b/test/serialization.jl @@ -3,7 +3,7 @@ using ModelingToolkit: t_nounits as t, D_nounits as D @variables x(t) -@named sys = ODESystem([D(x) ~ -0.5 * x], t, defaults = Dict(x => 1.0)) +@named sys = System([D(x) ~ -0.5 * x], t, defaults = Dict(x => 1.0)) sys = complete(sys) for prob in [ eval(ModelingToolkit.ODEProblem{false}(sys, nothing, nothing, @@ -37,7 +37,7 @@ all_obs = observables(ss) prob = ODEProblem(ss, [capacitor.v => 0.0], (0, 0.1)) sol = solve(prob, ImplicitEuler()) -## Check ODESystem with Observables ---------- +## Check System with Observables ---------- ss_exp = ModelingToolkit.toexpr(ss) ss_ = complete(eval(ss_exp)) prob_ = ODEProblem(ss_, [capacitor.v => 0.0], (0, 0.1)) diff --git a/test/split_parameters.jl b/test/split_parameters.jl index 18fdb49a48..39b242db08 100644 --- a/test/split_parameters.jl +++ b/test/split_parameters.jl @@ -68,7 +68,7 @@ function Sampled(; name, interp = Interpolator(Float64[], 0.0)) output.u ~ get_value(interpolator, t) ] - return ODESystem(eqs, t, vars, [interpolator]; name, systems) + return System(eqs, t, vars, [interpolator]; name, systems) end vars = @variables y(t) dy(t) ddy(t) @@ -80,7 +80,7 @@ eqs = [y ~ src.output.u D(dy) ~ ddy connect(src.output, int.input)] -@named sys = ODESystem(eqs, t, vars, []; systems = [int, src]) +@named sys = System(eqs, t, vars, []; systems = [int, src]) s = complete(sys) sys = structural_simplify(sys) prob = ODEProblem( @@ -107,7 +107,7 @@ eqs = [D(y) ~ dy * a D(dy) ~ ddy * b ddy ~ sin(t) * c] -@named model = ODESystem(eqs, t, vars, pars) +@named model = System(eqs, t, vars, pars) sys = structural_simplify(model; split = false) tspan = (0.0, t_end) @@ -134,7 +134,7 @@ using ModelingToolkit: connect "A wrapper function to make symbolic indexing easier" function wr(sys) - ODESystem(Equation[], ModelingToolkit.get_iv(sys), systems = [sys], name = :a_wrapper) + System(Equation[], ModelingToolkit.get_iv(sys), systems = [sys], name = :a_wrapper) end indexof(sym, syms) = findfirst(isequal(sym), syms) @@ -156,11 +156,11 @@ function SystemModel(u = nothing; name = :model) connect(inertia2.flange_a, spring.flange_b, damper.flange_b)] if u !== nothing push!(eqs, connect(torque.tau, u.output)) - return @named model = ODESystem(eqs, + return @named model = System(eqs, t; systems = [torque, inertia1, inertia2, spring, damper, u]) end - ODESystem(eqs, t; systems = [torque, inertia1, inertia2, spring, damper], + System(eqs, t; systems = [torque, inertia1, inertia2, spring, damper], name, guesses = [spring.flange_a.phi => 0.0]) end @@ -186,7 +186,7 @@ L = randn(1, 4) # Post-multiply by `C` to get the correct input to the controlle # @named input = RealInput(; nin = nin) # @named output = RealOutput(; nout = nout) # eqs = [output.u[i] ~ sum(K[i, j] * input.u[j] for j in 1:nin) for i in 1:nout] -# compose(ODESystem(eqs, t, [], []; name = name), [input, output]) +# compose(System(eqs, t, [], []; name = name), [input, output]) # end @named state_feedback = MatrixGain(K = -L) # Build negative feedback into the feedback matrix @@ -196,7 +196,7 @@ connections = [[state_feedback.input.u[i] ~ model_outputs[i] for i in 1:4] connect(d.output, :d, add.input1) connect(add.input2, state_feedback.output) connect(add.output, :u, model.torque.tau)] -@named closed_loop = ODESystem(connections, t, systems = [model, state_feedback, add, d]) +@named closed_loop = System(connections, t, systems = [model, state_feedback, add, d]) S = get_sensitivity(closed_loop, :u) @testset "Indexing MTKParameters with ParameterIndex" begin @@ -236,7 +236,7 @@ end (::Foo)(x) = 3x @variables x(t) @parameters fn(::Real) = _f1 - @mtkbuild sys = ODESystem(D(x) ~ fn(t), t) + @mtkbuild sys = System(D(x) ~ fn(t), t) @test is_parameter(sys, fn) @test ModelingToolkit.defaults(sys)[fn] == _f1 @@ -260,7 +260,7 @@ end interp = LinearInterpolation(ts .^ 2, ts; extrapolate = true) @variables x(t) @parameters (fn::typeof(interp))(..) - @mtkbuild sys = ODESystem(D(x) ~ fn(x), t) + @mtkbuild sys = System(D(x) ~ fn(x), t) @test is_parameter(sys, fn) getter = getp(sys, fn) prob = ODEProblem(sys, [x => 1.0], (0.0, 1.0), [fn => interp]) diff --git a/test/state_selection.jl b/test/state_selection.jl index a8d3e57773..b8bec5d7b7 100644 --- a/test/state_selection.jl +++ b/test/state_selection.jl @@ -7,7 +7,7 @@ eqs = [x1 + x2 + u1 ~ 0 x1 + x2 + x3 + u2 ~ 0 x1 + D(x3) + x4 + u3 ~ 0 2 * D(D(x1)) + D(D(x2)) + D(D(x3)) + D(x4) + u4 ~ 0] -@named sys = ODESystem(eqs, t) +@named sys = System(eqs, t) let dd = dummy_derivative(sys) has_dx1 = has_dx2 = false @@ -35,7 +35,7 @@ eqs = [D(x) ~ σ * (y - x) 0 ~ a + z u ~ z + a] -lorenz1 = ODESystem(eqs, t, name = :lorenz1) +lorenz1 = System(eqs, t, name = :lorenz1) let al1 = alias_elimination(lorenz1) let lss = partial_state_selection(al1) @test length(equations(lss)) == 2 @@ -47,13 +47,13 @@ let @connector function Fluid_port(; name, p = 101325.0, m = 0.0, T = 293.15) sts = @variables p(t) [guess = p] m(t) [guess = m, connect = Flow] T(t) [ guess = T, connect = Stream] - ODESystem(Equation[], t, sts, []; name = name) + System(Equation[], t, sts, []; name = name) end #this one is for latter @connector function Heat_port(; name, Q = 0.0, T = 293.15) sts = @variables T(t) [guess = T] Q(t) [guess = Q, connect = Flow] - ODESystem(Equation[], t, sts, []; name = name) + System(Equation[], t, sts, []; name = name) end # like ground but for fluid systems (fluid_port.m is expected to be zero in closed loop) @@ -62,7 +62,7 @@ let ps = @parameters p=p T_back=T_back eqs = [fluid_port.p ~ p fluid_port.T ~ T_back] - compose(ODESystem(eqs, t, [], ps; name = name), fluid_port) + compose(System(eqs, t, [], ps; name = name), fluid_port) end function Source(; name, delta_p = 100, T_feed = 293.15) @@ -73,7 +73,7 @@ let supply_port.p ~ return_port.p + delta_p supply_port.T ~ instream(supply_port.T) return_port.T ~ T_feed] - compose(ODESystem(eqs, t, [], ps; name = name), [supply_port, return_port]) + compose(System(eqs, t, [], ps; name = name), [supply_port, return_port]) end function Substation(; name, T_return = 343.15) @@ -84,7 +84,7 @@ let supply_port.p ~ return_port.p # zero pressure loss for now supply_port.T ~ instream(supply_port.T) return_port.T ~ T_return] - compose(ODESystem(eqs, t, [], ps; name = name), [supply_port, return_port]) + compose(System(eqs, t, [], ps; name = name), [supply_port, return_port]) end function Pipe(; name, L = 1000, d = 0.1, N = 100, rho = 1000, f = 1) @@ -98,7 +98,7 @@ let v * pi * d^2 / 4 * rho ~ fluid_port_a.m dp_z ~ abs(v) * v * 0.5 * rho * L / d * f # pressure loss D(v) * rho * L ~ (fluid_port_a.p - fluid_port_b.p - dp_z)] - compose(ODESystem(eqs, t, sts, ps; name = name), [fluid_port_a, fluid_port_b]) + compose(System(eqs, t, sts, ps; name = name), [fluid_port_a, fluid_port_b]) end function System(; name, L = 10.0) @named compensator = Compensator() @@ -113,7 +113,7 @@ let connect(supply_pipe.fluid_port_b, substation.supply_port) connect(substation.return_port, return_pipe.fluid_port_b) connect(return_pipe.fluid_port_a, source.return_port)] - compose(ODESystem(eqs, t, [], ps; name = name), subs) + compose(System(eqs, t, [], ps; name = name), subs) end @named system = System(L = 10) @@ -167,7 +167,7 @@ let D(rho_2) ~ (mo_1 - mo_3) / dx D(mo_2) ~ (Ek_1 - Ek_3 + p_1 - p_2) / dx - f / 2 / pipe_D * u_2 * u_2] - @named trans = ODESystem(eqs, t) + @named trans = System(eqs, t) sys = structural_simplify(trans) @@ -273,7 +273,7 @@ let # ---------------------------------------------------------------------------- # solution ------------------------------------------------------------------- - @named catapult = ODESystem(eqs, t, vars, params, defaults = defs) + @named catapult = System(eqs, t, vars, params, defaults = defs) sys = structural_simplify(catapult) prob = ODEProblem(sys, [], (0.0, 0.1), [l_2f => 0.55, damp => 1e7]; jac = true) @test solve(prob, Rodas4()).retcode == ReturnCode.Success diff --git a/test/static_arrays.jl b/test/static_arrays.jl index 61177e5ab2..61b9bb6b25 100644 --- a/test/static_arrays.jl +++ b/test/static_arrays.jl @@ -8,7 +8,7 @@ eqs = [D(D(x)) ~ σ * (y - x), D(y) ~ x * (ρ - z) - y, D(z) ~ x * y - β * z] -@named sys = ODESystem(eqs, t) +@named sys = System(eqs, t) sys = structural_simplify(sys) u0 = @SVector [D(x) => 2.0, diff --git a/test/steadystatesystems.jl b/test/steadystatesystems.jl index 4f1b5ed063..a99e84d9d2 100644 --- a/test/steadystatesystems.jl +++ b/test/steadystatesystems.jl @@ -6,7 +6,7 @@ using ModelingToolkit: t_nounits as t, D_nounits as D @parameters r @variables x(t) eqs = [D(x) ~ x^2 - r] -@named de = ODESystem(eqs, t) +@named de = System(eqs, t) de = complete(de) for factor in [1e-1, 1e0, 1e10], diff --git a/test/stream_connectors.jl b/test/stream_connectors.jl index 834ebce1a7..23c648d9e1 100644 --- a/test/stream_connectors.jl +++ b/test/stream_connectors.jl @@ -15,7 +15,7 @@ using ModelingToolkit: t_nounits as t, D_nounits as D P(t) = P end - ODESystem(Equation[], t, vars, pars; name = name) + System(Equation[], t, vars, pars; name = name) end @connector function TwoPhaseFluid(; name, R, B, V) @@ -32,7 +32,7 @@ end # equations --------------------------- eqs = Equation[m_flow ~ 0] - ODESystem(eqs, t, vars, pars; name) + System(eqs, t, vars, pars; name) end function MassFlowSource_h(; name, @@ -57,7 +57,7 @@ function MassFlowSource_h(; name, push!(eqns, port.m_flow ~ -m_flow_in) push!(eqns, port.h_outflow ~ h_in) - compose(ODESystem(eqns, t, vars, pars; name = name), subs) + compose(System(eqns, t, vars, pars; name = name), subs) end # Simplified components. @@ -74,7 +74,7 @@ function AdiabaticStraightPipe(; name, eqns = Equation[] push!(eqns, connect(port_a, port_b)) - sys = ODESystem(eqns, t, vars, pars; name = name) + sys = System(eqns, t, vars, pars; name = name) sys = compose(sys, subs) end @@ -97,7 +97,7 @@ function SmallBoundary_Ph(; name, push!(eqns, port1.P ~ P) push!(eqns, port1.h_outflow ~ h) - compose(ODESystem(eqns, t, vars, pars; name = name), subs) + compose(System(eqns, t, vars, pars; name = name), subs) end # N1M1 model and test code. @@ -114,7 +114,7 @@ function N1M1(; name, push!(eqns, connect(source.port1, port_a)) - sys = ODESystem(eqns, t, [], [], name = name) + sys = System(eqns, t, [], [], name = name) sys = compose(sys, subs) end @@ -126,13 +126,13 @@ end eqns = [connect(n1m1.port_a, pipe.port_a) connect(pipe.port_b, sink.port)] -@named sys = ODESystem(eqns, t) +@named sys = System(eqns, t) eqns = [domain_connect(fluid, n1m1.port_a) connect(n1m1.port_a, pipe.port_a) connect(pipe.port_b, sink.port)] -@named n1m1Test = ODESystem(eqns, t, [], []; systems = [fluid, n1m1, pipe, sink]) +@named n1m1Test = System(eqns, t, [], []; systems = [fluid, n1m1, pipe, sink]) @test_nowarn structural_simplify(n1m1Test) @unpack source, port_a = n1m1 @@ -192,7 +192,7 @@ function N1M2(; name, push!(eqns, connect(source.port1, port_a)) push!(eqns, connect(source.port1, port_b)) - sys = ODESystem(eqns, t, [], [], name = name) + sys = System(eqns, t, [], [], name = name) sys = compose(sys, subs) end @@ -203,7 +203,7 @@ end eqns = [connect(n1m2.port_a, sink1.port) connect(n1m2.port_b, sink2.port)] -@named sys = ODESystem(eqns, t) +@named sys = System(eqns, t) @named n1m2Test = compose(sys, n1m2, sink1, sink2) @test_nowarn structural_simplify(n1m2Test) @@ -218,7 +218,7 @@ eqns = [connect(n1m2.port_a, pipe1.port_a) connect(n1m2.port_b, pipe2.port_a) connect(pipe2.port_b, sink2.port)] -@named sys = ODESystem(eqns, t) +@named sys = System(eqns, t) @named n1m2AltTest = compose(sys, n1m2, pipe1, pipe2, sink1, sink2) @test_nowarn structural_simplify(n1m2AltTest) @@ -236,7 +236,7 @@ function N2M2(; name, push!(eqns, connect(port_a, pipe.port_a)) push!(eqns, connect(pipe.port_b, port_b)) - sys = ODESystem(eqns, t, [], [], name = name) + sys = System(eqns, t, [], [], name = name) sys = compose(sys, subs) end @@ -247,14 +247,14 @@ end eqns = [connect(source.port, n2m2.port_a) connect(n2m2.port_b, sink.port1)] -@named sys = ODESystem(eqns, t) +@named sys = System(eqns, t) @named n2m2Test = compose(sys, n2m2, source, sink) @test_nowarn structural_simplify(n2m2Test) # stream var @named sp1 = TwoPhaseFluidPort() @named sp2 = TwoPhaseFluidPort() -@named sys = ODESystem([connect(sp1, sp2)], t) +@named sys = System([connect(sp1, sp2)], t) sys_exp = expand_connections(compose(sys, [sp1, sp2])) @test ssort(equations(sys_exp)) == ssort([0 ~ -sp1.m_flow - sp2.m_flow 0 ~ sp1.m_flow @@ -266,14 +266,14 @@ sys_exp = expand_connections(compose(sys, [sp1, sp2])) # array var @connector function VecPin(; name) sts = @variables v(t)[1:2]=[1.0, 0.0] i(t)[1:2]=1.0 [connect = Flow] - ODESystem(Equation[], t, [sts...;], []; name = name) + System(Equation[], t, [sts...;], []; name = name) end @named vp1 = VecPin() @named vp2 = VecPin() @named vp3 = VecPin() -@named simple = ODESystem([connect(vp1, vp2, vp3)], t) +@named simple = System([connect(vp1, vp2, vp3)], t) sys = expand_connections(compose(simple, [vp1, vp2, vp3])) @test ssort(equations(sys)) == ssort([0 .~ collect(vp1.i) 0 .~ collect(vp2.i) @@ -287,7 +287,7 @@ sys = expand_connections(compose(simple, [vp1, vp2, vp3])) @connector function VectorHeatPort(; name, N = 100, T0 = 0.0, Q0 = 0.0) @variables (T(t))[1:N]=T0 (Q(t))[1:N]=Q0 [connect = Flow] - ODESystem(Equation[], t, [T; Q], []; name = name) + System(Equation[], t, [T; Q], []; name = name) end @test_nowarn @named a = VectorHeatPort() @@ -319,7 +319,7 @@ csys = complete(n1m1Test) # equations --------------------------- eqs = Equation[] - ODESystem(eqs, t, vars, pars; name, defaults = [dm => 0]) + System(eqs, t, vars, pars; name, defaults = [dm => 0]) end @connector function Fluid(; name, R, B, V) @@ -338,7 +338,7 @@ end dm ~ 0 ] - ODESystem(eqs, t, vars, pars; name) + System(eqs, t, vars, pars; name) end function StepSource(; P, name) @@ -358,7 +358,7 @@ function StepSource(; P, name) H.p ~ p_int * (t > 0.01) ] - ODESystem(eqs, t, vars, pars; name, systems) + System(eqs, t, vars, pars; name, systems) end function StaticVolume(; P, V, name) @@ -387,7 +387,7 @@ function StaticVolume(; P, V, name) H.p ~ p H.dm ~ drho * V] - ODESystem(eqs, t, vars, pars; name, systems, + System(eqs, t, vars, pars; name, systems, defaults = [vrho => rho_0 * (1 + p_int / H.bulk)]) end @@ -410,7 +410,7 @@ function PipeBase(; P, R, name) 0 ~ HA.dm + HB.dm domain_connect(HA, HB)] - ODESystem(eqs, t, vars, pars; name, systems) + System(eqs, t, vars, pars; name, systems) end function Pipe(; P, R, name) @@ -432,7 +432,7 @@ function Pipe(; P, R, name) eqs = [connect(v1.H, p12.HA, HA) connect(v2.H, p12.HB, HB)] - ODESystem(eqs, t, vars, pars; name, systems) + System(eqs, t, vars, pars; name, systems) end function TwoFluidSystem(; name) @@ -460,7 +460,7 @@ function TwoFluidSystem(; name) connect(source_b.H, pipe_b.HA) connect(pipe_b.HB, volume_b.H)] - ODESystem(eqs, t, vars, pars; name, systems) + System(eqs, t, vars, pars; name, systems) end @named two_fluid_system = TwoFluidSystem() @@ -498,7 +498,7 @@ function OneFluidSystem(; name) connect(source_b.H, pipe_b.HA) connect(pipe_b.HB, volume_b.H)] - ODESystem(eqs, t, vars, pars; name, systems) + System(eqs, t, vars, pars; name, systems) end @named one_fluid_system = OneFluidSystem() diff --git a/test/structural_transformation/index_reduction.jl b/test/structural_transformation/index_reduction.jl index f9d6037022..26f73db4e7 100644 --- a/test/structural_transformation/index_reduction.jl +++ b/test/structural_transformation/index_reduction.jl @@ -12,7 +12,7 @@ using ModelingToolkit: t_nounits as t, D_nounits as D eqs2 = [D(D(x)) ~ T * x, D(D(y)) ~ T * y - g, 0 ~ x^2 + y^2 - L^2] -pendulum2 = ODESystem(eqs2, t, [x, y, T], [L, g], name = :pendulum) +pendulum2 = System(eqs2, t, [x, y, T], [L, g], name = :pendulum) lowered_sys = ModelingToolkit.ode_order_lowering(pendulum2) lowered_eqs = [D(xˍt) ~ T * x, @@ -20,7 +20,7 @@ lowered_eqs = [D(xˍt) ~ T * x, D(x) ~ xˍt, D(y) ~ yˍt, 0 ~ x^2 + y^2 - L^2] -@test ODESystem(lowered_eqs, t, [xˍt, yˍt, x, y, T], [L, g], name = :pendulum) == +@test System(lowered_eqs, t, [xˍt, yˍt, x, y, T], [L, g], name = :pendulum) == lowered_sys @test isequal(equations(lowered_sys), lowered_eqs) @@ -30,7 +30,7 @@ eqs = [D(x) ~ w, D(w) ~ T * x, D(z) ~ T * y - g, 0 ~ x^2 + y^2 - L^2] -pendulum = ODESystem(eqs, t, [x, y, w, z, T], [L, g], name = :pendulum) +pendulum = System(eqs, t, [x, y, w, z, T], [L, g], name = :pendulum) state = TearingState(pendulum) @unpack graph, var_to_diff = state.structure @@ -57,7 +57,7 @@ idx1_pendulum = [D(x) ~ w, # 2x*D(D(x)) + 2*D(x)*D(x) + 2y*D(D(y)) + 2*D(y)*D(y) and # substitute the rhs 0 ~ 2x * (T * x) + 2 * xˍt * xˍt + 2y * (T * y - g) + 2 * yˍt * yˍt] -@named idx1_pendulum = ODESystem(idx1_pendulum, t, [x, y, w, z, xˍt, yˍt, T], [L, g]) +@named idx1_pendulum = System(idx1_pendulum, t, [x, y, w, z, xˍt, yˍt, T], [L, g]) first_order_idx1_pendulum = complete(ode_order_lowering(idx1_pendulum)) using OrdinaryDiffEq @@ -90,7 +90,7 @@ sol = solve(prob_auto, Rodas5()); eqs2 = [D(D(x)) ~ T * x, D(D(y)) ~ T * y - g, 0 ~ x^2 + y^2 - L^2] -pendulum2 = ODESystem(eqs2, t, [x, y, T], [L, g], name = :pendulum) +pendulum2 = System(eqs2, t, [x, y, T], [L, g], name = :pendulum) # Turn into a first order differential equation system first_order_sys = ModelingToolkit.ode_order_lowering(pendulum2) @@ -126,7 +126,7 @@ eqs = [D(x) ~ w, D(w) ~ T * x, D(z) ~ T * y - g, 0 ~ x^2 + y^2 - L^2] -pendulum = ODESystem(eqs, t, [x, y, w, z, T], [L, g], name = :pendulum) +pendulum = System(eqs, t, [x, y, w, z, T], [L, g], name = :pendulum) let pss_pendulum = partial_state_selection(pendulum) # This currently selects `T` rather than `x` at top level. Needs tearing priorities to fix. @@ -159,7 +159,7 @@ let eqs = [D(D(x)) ~ λ * x D(D(y)) ~ λ * y - g x^2 + y^2 ~ 1] - @named pend = ODESystem(eqs, t) + @named pend = System(eqs, t) sys = complete(structural_simplify(pend; dummy_derivative = false)) prob = ODEProblem( sys, [x => 1, y => 0, D(x) => 0.0], (0.0, 10.0), [g => 1], guesses = [λ => 0.0]) diff --git a/test/structural_transformation/tearing.jl b/test/structural_transformation/tearing.jl index e9cd92ec94..24083b3524 100644 --- a/test/structural_transformation/tearing.jl +++ b/test/structural_transformation/tearing.jl @@ -147,7 +147,7 @@ using ModelingToolkit, OrdinaryDiffEq, BenchmarkTools eqs = [D(x) ~ z * h 0 ~ x - y 0 ~ sin(z) + y - p * t] -@named daesys = ODESystem(eqs, t) +@named daesys = System(eqs, t) newdaesys = structural_simplify(daesys) @test equations(newdaesys) == [D(x) ~ z; 0 ~ y + sin(z) - p * t] @test equations(tearing_substitution(newdaesys)) == [D(x) ~ z; 0 ~ x + sin(z) - p * t] @@ -164,7 +164,7 @@ prob.f(du, u, pr, tt) @test du≈[u[2], u[1] + sin(u[2]) - pr * tt] atol=1e-5 # test the initial guess is respected -@named sys = ODESystem(eqs, t, defaults = Dict(z => NaN)) +@named sys = System(eqs, t, defaults = Dict(z => NaN)) infprob = ODEProblem(structural_simplify(sys), [x => 1.0], (0, 1.0), [p => 0.2]) infprob.f(du, infprob.u0, pr, tt) @test any(isnan, du) @@ -177,7 +177,7 @@ function Translational_Mass(; name, m = 1.0) eqs = [D(s) ~ v D(v) ~ a m * a ~ 0.0] - ODESystem(eqs, t, sts, ps; name = name) + System(eqs, t, sts, ps; name = name) end m = 1.0 @@ -185,7 +185,7 @@ m = 1.0 ms_eqs = [] -@named _ms_model = ODESystem(ms_eqs, t) +@named _ms_model = System(ms_eqs, t) @named ms_model = compose(_ms_model, [mass]) diff --git a/test/structural_transformation/utils.jl b/test/structural_transformation/utils.jl index 67cf2f72a0..7008a31111 100644 --- a/test/structural_transformation/utils.jl +++ b/test/structural_transformation/utils.jl @@ -18,7 +18,7 @@ eqs = [D(x) ~ w, D(w) ~ T * x, D(z) ~ T * y - g, 0 ~ x^2 + y^2 - L^2] -pendulum = ODESystem(eqs, t, [x, y, w, z, T], [L, g], name = :pendulum) +pendulum = System(eqs, t, [x, y, w, z, T], [L, g], name = :pendulum) state = TearingState(pendulum) StructuralTransformations.find_solvables!(state) sss = state.structure @@ -48,7 +48,7 @@ end @variables x(t) y(t)[1:2] z(t)[1:2] @parameters foo(::AbstractVector)[1:2] _tmp_fn(x) = 2x - @mtkbuild sys = ODESystem( + @mtkbuild sys = System( [D(x) ~ z[1] + z[2] + foo(z)[1], y[1] ~ 2t, y[2] ~ 3t, z ~ foo(y)], t) @test length(equations(sys)) == 1 @test length(observed(sys)) == 7 @@ -74,7 +74,7 @@ end val[] += 1 return [x, 2x] end - @mtkbuild sys = ODESystem([D(x) ~ y[1] + y[2], y ~ foo(x)], t) + @mtkbuild sys = System([D(x) ~ y[1] + y[2], y ~ foo(x)], t) @test length(equations(sys)) == 1 @test length(observed(sys)) == 4 prob = ODEProblem(sys, [x => 1.0], (0.0, 1.0), [foo => _tmp_fn2]) @@ -93,7 +93,7 @@ end @testset "CSE hack in equations(sys)" begin val[] = 0 @variables z(t)[1:2] - @mtkbuild sys = ODESystem( + @mtkbuild sys = System( [D(y) ~ foo(x), D(x) ~ sum(y), zeros(2) ~ foo(prod(z))], t) @test length(equations(sys)) == 5 @test length(observed(sys)) == 2 @@ -119,7 +119,7 @@ end @variables x(t) y(t)[1:2] z(t)[1:2] @parameters foo(::AbstractVector)[1:2] _tmp_fn(x) = 2x - @named sys = ODESystem( + @named sys = System( [D(x) ~ z[1] + z[2] + foo(z)[1], y[1] ~ 2t, y[2] ~ 3t, z ~ foo(y)], t) sys1 = structural_simplify(sys; cse_hack = false) @@ -140,7 +140,7 @@ end @variables x(t) y(t)[1:2] z(t)[1:2] w(t) @parameters foo(::AbstractVector)[1:2] _tmp_fn(x) = 2x - @named sys = ODESystem( + @named sys = System( [D(x) ~ z[1] + z[2] + foo(z)[1] + w, y[1] ~ 2t, y[2] ~ 3t, z ~ foo(y)], t) sys1 = structural_simplify(sys; cse_hack = false, fully_determined = false) @@ -160,7 +160,7 @@ end @testset "additional passes" begin @variables x(t) y(t) - @named sys = ODESystem([D(x) ~ x, y ~ x + t], t) + @named sys = System([D(x) ~ x, y ~ x + t], t) value = Ref(0) pass(sys; kwargs...) = (value[] += 1; return sys) structural_simplify(sys; additional_passes = [pass]) @@ -198,13 +198,13 @@ end end @testset "Requires simplified system" begin @variables x(t) y(t) - @named sys = ODESystem([D(x) ~ x, y ~ 2x], t) + @named sys = System([D(x) ~ x, y ~ 2x], t) sys = complete(sys) @test_throws ArgumentError map_variables_to_equations(sys) end @testset "`ODESystem`" begin @variables x(t) y(t) z(t) - @mtkbuild sys = ODESystem([D(x) ~ 2x + y, y ~ x + z, z^3 + x^3 ~ 12], t) + @mtkbuild sys = System([D(x) ~ 2x + y, y ~ x + z, z^3 + x^3 ~ 12], t) mapping = map_variables_to_equations(sys) @test mapping[x] == (D(x) ~ 2x + y) @test mapping[y] == (y ~ x + z) @@ -217,7 +217,7 @@ end eqs = [D(D(x)) ~ λ * x D(D(y)) ~ λ * y - g x^2 + y^2 ~ 1] - @mtkbuild sys = ODESystem(eqs, t) + @mtkbuild sys = System(eqs, t) mapping = map_variables_to_equations(sys) yt = default_toterm(unwrap(D(y))) diff --git a/test/substitute_component.jl b/test/substitute_component.jl index 9fb254136b..ad20dbcbc1 100644 --- a/test/substitute_component.jl +++ b/test/substitute_component.jl @@ -232,9 +232,9 @@ end @testset "Different indepvar" begin @independent_variables tt - @named empty = ODESystem(Equation[], t) - @named outer = ODESystem(Equation[], t; systems = [empty]) - @named empty = ODESystem(Equation[], tt) + @named empty = System(Equation[], t) + @named outer = System(Equation[], t; systems = [empty]) + @named empty = System(Equation[], tt) @test_throws ["independent variable"] substitute_component( outer, outer.empty => empty) end diff --git a/test/symbolic_events.jl b/test/symbolic_events.jl index 48faa76aa3..35a5369869 100644 --- a/test/symbolic_events.jl +++ b/test/symbolic_events.jl @@ -202,14 +202,14 @@ end end @testset "Condition Compilation" begin - @named sys = ODESystem(eqs, t, continuous_events = [x ~ 1]) + @named sys = System(eqs, t, continuous_events = [x ~ 1]) @test getfield(sys, :continuous_events)[] == SymbolicContinuousCallback(Equation[x ~ 1], nothing) @test isequal(equations(getfield(sys, :continuous_events))[], x ~ 1) fsys = flatten(sys) @test isequal(equations(getfield(fsys, :continuous_events))[], x ~ 1) - @named sys2 = ODESystem([D(x) ~ 1], t, continuous_events = [x ~ 2], systems = [sys]) + @named sys2 = System([D(x) ~ 1], t, continuous_events = [x ~ 2], systems = [sys]) @test getfield(sys2, :continuous_events)[] == SymbolicContinuousCallback(Equation[x ~ 2], nothing) @test all(ModelingToolkit.continuous_events(sys2) .== [ @@ -286,7 +286,7 @@ end @test minimum(t -> abs(t - 1), sol.t) < 1e-9 # test that the solver stepped at the first root @test minimum(t -> abs(t - 2), sol.t) < 1e-9 # test that the solver stepped at the second root - @named sys = ODESystem(eqs, t, continuous_events = [x ~ 1, x ~ 2]) # two root eqs using the same unknown + @named sys = System(eqs, t, continuous_events = [x ~ 1, x ~ 2]) # two root eqs using the same unknown sys = complete(sys) prob = ODEProblem(sys, Pair[], (0.0, 3.0)) @test get_callback(prob) isa ModelingToolkit.DiffEqCallbacks.VectorContinuousCallback @@ -302,7 +302,7 @@ end root_eqs = [x ~ 0] affect = [v ~ -Pre(v)] - @named ball = ODESystem( + @named ball = System( [D(x) ~ v D(v) ~ -9.8], t, continuous_events = root_eqs => affect) @@ -323,7 +323,7 @@ end events = [[x ~ 0] => [vx ~ -Pre(vx)] [y ~ -1.5, y ~ 1.5] => [vy ~ -Pre(vy)]] - @named ball = ODESystem( + @named ball = System( [D(x) ~ vx D(y) ~ vy D(vx) ~ -9.8 @@ -363,7 +363,7 @@ end # in this test, there are two variables affected by a single event. events = [[x ~ 0] => [vx ~ -Pre(vx), vy ~ -Pre(vy)]] - @named ball = ODESystem( + @named ball = System( [D(x) ~ vx D(y) ~ vy D(vx) ~ -1 @@ -391,7 +391,7 @@ end D(v) ~ vs - v D(vmeasured) ~ 0.0] ev = [sin(20pi * t) ~ 0.0] => [vmeasured ~ Pre(v)] - @named sys = ODESystem(eq, t, continuous_events = ev) + @named sys = System(eq, t, continuous_events = ev) sys = structural_simplify(sys) prob = ODEProblem(sys, zeros(2), (0.0, 5.1)) sol = solve(prob, Tsit5()) @@ -410,22 +410,22 @@ end ps = @parameters m = m sts = @variables pos(t)=p vel(t)=v eqs = Dₜ(pos) ~ vel - ODESystem(eqs, t, [pos, vel], ps; name) + System(eqs, t, [pos, vel], ps; name) end function Spring(; name, k = 1e4) ps = @parameters k = k @variables x(t) = 0 # Spring deflection - ODESystem(Equation[], t, [x], ps; name) + System(Equation[], t, [x], ps; name) end function Damper(; name, c = 10) ps = @parameters c = c @variables vel(t) = 0 - ODESystem(Equation[], t, [vel], ps; name) + System(Equation[], t, [vel], ps; name) end function SpringDamper(; name, k = false, c = false) spring = Spring(; name = :spring, k) damper = Damper(; name = :damper, c) - compose(ODESystem(Equation[], t; name), + compose(System(Equation[], t; name), spring, damper) end connect_sd(sd, m1, m2) = [ @@ -438,7 +438,7 @@ end eqs = [connect_sd(sd, mass1, mass2) Dₜ(mass1.vel) ~ (sd_force(sd) + u) / mass1.m Dₜ(mass2.vel) ~ (-sd_force(sd) + d) / mass2.m] - @named _model = ODESystem(eqs, t; observed = [y ~ mass2.pos]) + @named _model = System(eqs, t; observed = [y ~ mass2.pos]) @named model = compose(_model, mass1, mass2, sd) end model = Model(sin(30t)) @@ -471,7 +471,7 @@ end ∂ₜ = D eqs = [∂ₜ(A) ~ -k * A] - @named osys = ODESystem(eqs, t, [A], [k, t1, t2], discrete_events = [cb1, cb2]) + @named osys = System(eqs, t, [A], [k, t1, t2], discrete_events = [cb1, cb2]) @named ssys = SDESystem(eqs, [0.0], t, [A], [k, t1, t2], discrete_events = [cb1, cb2]) u0 = [A => 1.0] p = [k => 0.0, t1 => 1.0, t2 => 2.0] @@ -482,7 +482,7 @@ end cond1a = (t == t1) affect1a = [A ~ Pre(A) + 1, B ~ A] cb1a = cond1a => affect1a - @named osys1 = ODESystem(eqs, t, [A, B], [k, t1, t2], discrete_events = [cb1a, cb2]) + @named osys1 = System(eqs, t, [A, B], [k, t1, t2], discrete_events = [cb1a, cb2]) @named ssys1 = SDESystem(eqs, [0.0], t, [A, B], [k, t1, t2], discrete_events = [cb1a, cb2]) u0′ = [A => 1.0, B => 0.0] @@ -497,13 +497,13 @@ end # same as above - but with set-time event syntax cb1‵ = [1.0] => affect1 # needs to be a Vector for the event to happen only once cb2‵ = SymbolicDiscreteCallback([2.0] => affect2, discrete_parameters = [k], iv = t) - @named osys‵ = ODESystem(eqs, t, [A], [k], discrete_events = [cb1‵, cb2‵]) + @named osys‵ = System(eqs, t, [A], [k], discrete_events = [cb1‵, cb2‵]) @named ssys‵ = SDESystem(eqs, [0.0], t, [A], [k], discrete_events = [cb1‵, cb2‵]) testsol(osys‵, ODEProblem, Tsit5, u0, p, tspan; paramtotest = k) testsol(ssys‵, SDEProblem, RI5, u0, p, tspan; paramtotest = k) # mixing discrete affects - @named osys3 = ODESystem(eqs, t, [A], [k, t1, t2], discrete_events = [cb1, cb2‵]) + @named osys3 = System(eqs, t, [A], [k, t1, t2], discrete_events = [cb1, cb2‵]) @named ssys3 = SDESystem(eqs, [0.0], t, [A], [k, t1, t2], discrete_events = [cb1, cb2‵]) testsol(osys3, ODEProblem, Tsit5, u0, p, tspan; tstops = [1.0], paramtotest = k) @@ -515,7 +515,7 @@ end nothing end cb2‵‵ = [2.0] => (affect!, [], [k], [k], nothing) - @named osys4 = ODESystem(eqs, t, [A], [k, t1], discrete_events = [cb1, cb2‵‵]) + @named osys4 = System(eqs, t, [A], [k, t1], discrete_events = [cb1, cb2‵‵]) @named ssys4 = SDESystem(eqs, [0.0], t, [A], [k, t1], discrete_events = [cb1, cb2‵‵]) oprob4 = ODEProblem(complete(osys4), u0, tspan, p) @@ -524,12 +524,12 @@ end # mixing with symbolic condition in the func affect cb2‵‵‵ = (t == t2) => (affect!, [], [k], [k], nothing) - @named osys5 = ODESystem(eqs, t, [A], [k, t1, t2], discrete_events = [cb1, cb2‵‵‵]) + @named osys5 = System(eqs, t, [A], [k, t1, t2], discrete_events = [cb1, cb2‵‵‵]) @named ssys5 = SDESystem(eqs, [0.0], t, [A], [k, t1, t2], discrete_events = [cb1, cb2‵‵‵]) testsol(osys5, ODEProblem, Tsit5, u0, p, tspan; tstops = [1.0, 2.0]) testsol(ssys5, SDEProblem, RI5, u0, p, tspan; tstops = [1.0, 2.0]) - @named osys6 = ODESystem(eqs, t, [A], [k, t1, t2], discrete_events = [cb2‵‵‵, cb1]) + @named osys6 = System(eqs, t, [A], [k, t1, t2], discrete_events = [cb2‵‵‵, cb1]) @named ssys6 = SDESystem(eqs, [0.0], t, [A], [k, t1, t2], discrete_events = [cb2‵‵‵, cb1]) testsol(osys6, ODEProblem, Tsit5, u0, p, tspan; tstops = [1.0, 2.0], paramtotest = k) @@ -539,7 +539,7 @@ end cond3 = A ~ 0.1 affect3 = [k ~ 0.0] cb3 = SymbolicContinuousCallback(cond3 => affect3, discrete_parameters = [k], iv = t) - @named osys7 = ODESystem(eqs, t, [A], [k, t1, t2], discrete_events = [cb1, cb2‵‵‵], + @named osys7 = System(eqs, t, [A], [k, t1, t2], discrete_events = [cb1, cb2‵‵‵], continuous_events = [cb3]) @named ssys7 = SDESystem(eqs, [0.0], t, [A], [k, t1, t2], discrete_events = [cb1, cb2‵‵‵], @@ -625,19 +625,19 @@ end ps = @parameters k=k Θ=0.5 eqs = [D(x) ~ v, D(v) ~ -k * x + F] ev = [x ~ Θ] => [x ~ 1.0, v ~ 0.0] - ODESystem(eqs, t, sts, ps, continuous_events = [ev]; name) + System(eqs, t, sts, ps, continuous_events = [ev]; name) end @named oscce = oscillator_ce() eqs = [oscce.F ~ 0] - @named eqs_sys = ODESystem(eqs, t) + @named eqs_sys = System(eqs, t) @named oneosc_ce = compose(eqs_sys, oscce) oneosc_ce_simpl = structural_simplify(oneosc_ce) prob = ODEProblem(oneosc_ce_simpl, [], (0.0, 2.0), []) sol = solve(prob, Tsit5(), saveat = 0.1) - @test typeof(oneosc_ce_simpl) == ODESystem + @test typeof(oneosc_ce_simpl) == System @test sol(0.5, idxs = oscce.x) < 1.0 # test whether x(t) decreases over time @test sol(1.5, idxs = oscce.x) > 0.5 # test whether event happened end @@ -653,7 +653,7 @@ end [c1 ~ 0], (record_crossings, [c1 => :v], [], [], cr1)) evt2 = ModelingToolkit.SymbolicContinuousCallback( [c2 ~ 0], (record_crossings, [c2 => :v], [], [], cr2)) - @named trigsys = ODESystem(eqs, t; continuous_events = [evt1, evt2]) + @named trigsys = System(eqs, t; continuous_events = [evt1, evt2]) trigsys_ss = structural_simplify(trigsys) prob = ODEProblem(trigsys_ss, [], (0.0, 2π)) sol = solve(prob, Tsit5()) @@ -675,7 +675,7 @@ end evt2 = ModelingToolkit.SymbolicContinuousCallback( [c2 ~ 0], (record_crossings, [c2 => :v], [], [], cr2p); affect_neg = (record_crossings, [c2 => :v], [], [], cr2n)) - @named trigsys = ODESystem(eqs, t; continuous_events = [evt1, evt2]) + @named trigsys = System(eqs, t; continuous_events = [evt1, evt2]) trigsys_ss = structural_simplify(trigsys) prob = ODEProblem(trigsys_ss, [], (0.0, 2π)) sol = solve(prob, Tsit5(); dtmax = 0.01) @@ -699,7 +699,7 @@ end [c1 ~ 0], (record_crossings, [c1 => :v], [], [], cr1p); affect_neg = nothing) evt2 = ModelingToolkit.SymbolicContinuousCallback( [c2 ~ 0], (record_crossings, [c2 => :v], [], [], cr2p); affect_neg = nothing) - @named trigsys = ODESystem(eqs, t; continuous_events = [evt1, evt2]) + @named trigsys = System(eqs, t; continuous_events = [evt1, evt2]) trigsys_ss = structural_simplify(trigsys) prob = ODEProblem(trigsys_ss, [], (0.0, 2π)) sol = solve(prob, Tsit5(); dtmax = 0.01) @@ -718,7 +718,7 @@ end evt2 = ModelingToolkit.SymbolicContinuousCallback( [c2 ~ 0], (record_crossings, [c2 => :v], [], [], cr2p); affect_neg = (record_crossings, [c2 => :v], [], [], cr2n)) - @named trigsys = ODESystem(eqs, t; continuous_events = [evt1, evt2]) + @named trigsys = System(eqs, t; continuous_events = [evt1, evt2]) trigsys_ss = structural_simplify(trigsys) prob = ODEProblem(trigsys_ss, [], (0.0, 2π)) sol = solve(prob, Tsit5(); dtmax = 0.01) @@ -742,7 +742,7 @@ end evt2 = ModelingToolkit.SymbolicContinuousCallback( [c2 ~ 0], (record_crossings, [c2 => :v], [], [], cr2); rootfind = SciMLBase.RightRootFind) - @named trigsys = ODESystem(eqs, t; continuous_events = [evt1, evt2]) + @named trigsys = System(eqs, t; continuous_events = [evt1, evt2]) trigsys_ss = structural_simplify(trigsys) prob = ODEProblem(trigsys_ss, [], (0.0, 2π)) sol = solve(prob, Tsit5(); dtmax = 0.01) @@ -762,7 +762,7 @@ end evt2 = ModelingToolkit.SymbolicContinuousCallback( [c2 ~ 0], (record_crossings, [c2 => :v], [], [], cr2); rootfind = SciMLBase.RightRootFind) - @named trigsys = ODESystem(eqs, t; continuous_events = [evt1, evt2]) + @named trigsys = System(eqs, t; continuous_events = [evt1, evt2]) trigsys_ss = structural_simplify(trigsys) prob = ODEProblem(trigsys_ss, [], (0.0, 2π)) sol = solve(prob, Tsit5()) @@ -780,7 +780,7 @@ end evt2 = ModelingToolkit.SymbolicContinuousCallback( [c2 ~ 0], (record_crossings, [c2 => :v], [], [], cr2); rootfind = SciMLBase.RightRootFind) - @named trigsys = ODESystem(eqs, t; continuous_events = [evt2, evt1]) + @named trigsys = System(eqs, t; continuous_events = [evt2, evt1]) trigsys_ss = structural_simplify(trigsys) prob = ODEProblem(trigsys_ss, [], (0.0, 2π)) sol = solve(prob, Tsit5()) @@ -882,7 +882,7 @@ end cb2 = [x ~ 0.5] => (save_affect!, [], [b], [b], nothing) cb3 = SymbolicDiscreteCallback(1.0 => [c ~ t], discrete_parameters = [c]) - @mtkbuild sys = ODESystem(D(x) ~ cos(t), t, [x], [a, b, c]; + @mtkbuild sys = System(D(x) ~ cos(t), t, [x], [a, b, c]; continuous_events = [cb1, cb2], discrete_events = [cb3]) prob = ODEProblem(sys, [x => 1.0], (0.0, 2pi), [a => 1.0, b => 2.0, c => 0.0]) @test sort(canonicalize(Discrete(), prob.p)[1]) == [0.0, 1.0, 2.0] @@ -910,7 +910,7 @@ end ModelingToolkit.ImperativeAffect(modified = (; furnace_on)) do x, o, i, c @set! x.furnace_on = true end) - @named sys = ODESystem( + @named sys = System( eqs, t, [temp], params; continuous_events = [furnace_off, furnace_enable]) ss = structural_simplify(sys) prob = ODEProblem(ss, [temp => 0.0, furnace_on => true], (0.0, 100.0)) @@ -930,7 +930,7 @@ end ModelingToolkit.ImperativeAffect(modified = (; furnace_on)) do x, o, c, i @set! x.furnace_on = true end) - @named sys = ODESystem( + @named sys = System( eqs, t, [temp], params; continuous_events = [furnace_off, furnace_enable]) ss = structural_simplify(sys) prob = ODEProblem(ss, [temp => 0.0, furnace_on => true], (0.0, 100.0)) @@ -952,7 +952,7 @@ end modified = (; furnace_on), observed = (; furnace_on)) do x, o, c, i @set! x.furnace_on = false end) - @named sys = ODESystem(eqs, t, [temp], params; continuous_events = [furnace_off]) + @named sys = System(eqs, t, [temp], params; continuous_events = [furnace_off]) ss = structural_simplify(sys) @test_logs (:warn, "The symbols Any[:furnace_on] are declared as both observed and modified; this is a code smell because it becomes easy to confuse them and assign/not assign a value.") prob=ODEProblem( @@ -968,7 +968,7 @@ end modified = (; furnace_on, tempsq), observed = (; furnace_on)) do x, o, c, i @set! x.furnace_on = false end) - @named sys = ODESystem( + @named sys = System( eqs, t, [temp, tempsq], params; continuous_events = [furnace_off]) ss = structural_simplify(sys) @test_throws "refers to missing variable(s)" prob=ODEProblem( @@ -981,7 +981,7 @@ end observed = (; furnace_on, not_actually_here)) do x, o, c, i @set! x.furnace_on = false end) - @named sys = ODESystem( + @named sys = System( eqs, t, [temp, tempsq], params; continuous_events = [furnace_off]) ss = structural_simplify(sys) @test_throws "refers to missing variable(s)" prob=ODEProblem( @@ -993,7 +993,7 @@ end observed = (; furnace_on)) do x, o, c, i return (; fictional2 = false) end) - @named sys = ODESystem( + @named sys = System( eqs, t, [temp, tempsq], params; continuous_events = [furnace_off]) ss = structural_simplify(sys) prob = ODEProblem( @@ -1053,7 +1053,7 @@ end @set! x.cnt += decoder(x.hA, x.hB, o.qA, x.qB) x end; rootfind = SciMLBase.RightRootFind) - @named sys = ODESystem( + @named sys = System( eqs, t, [theta, omega], params; continuous_events = [qAevt, qBevt]) ss = structural_simplify(sys) prob = ODEProblem(ss, [theta => 1e-5], (0.0, pi)) @@ -1068,7 +1068,7 @@ end f = (i, u, p, c) -> seen = true, sts = [], pars = [], discretes = []) cb1 = ModelingToolkit.SymbolicContinuousCallback( [x ~ 0], nothing, initialize = [x ~ 1.5], finalize = f) - @mtkbuild sys = ODESystem(D(x) ~ -1, t, [x], []; continuous_events = [cb1]) + @mtkbuild sys = System(D(x) ~ -1, t, [x], []; continuous_events = [cb1]) prob = ODEProblem(sys, [x => 1.0], (0.0, 2), []) sol = solve(prob, Tsit5(); dtmax = 0.01) @test sol[x][1] ≈ 1.0 @@ -1089,7 +1089,7 @@ end f = (i, u, p, c) -> finaled = true, sts = [], pars = [], discretes = []) cb2 = ModelingToolkit.SymbolicContinuousCallback( [x ~ 0.1], nothing, initialize = a, finalize = b) - @mtkbuild sys = ODESystem(D(x) ~ -1, t, [x], []; continuous_events = [cb1, cb2]) + @mtkbuild sys = System(D(x) ~ -1, t, [x], []; continuous_events = [cb1, cb2]) prob = ODEProblem(sys, [x => 1.0], (0.0, 2), []) sol = solve(prob, Tsit5()) @test sol[x][1] ≈ 1.0 @@ -1103,7 +1103,7 @@ end finaled = false cb3 = ModelingToolkit.SymbolicDiscreteCallback( 1.0, [x ~ 2], initialize = a, finalize = b) - @mtkbuild sys = ODESystem(D(x) ~ -1, t, [x], []; discrete_events = [cb3]) + @mtkbuild sys = System(D(x) ~ -1, t, [x], []; discrete_events = [cb3]) prob = ODEProblem(sys, [x => 1.0], (0.0, 2), []) sol = solve(prob, Tsit5()) @test inited == true @@ -1116,7 +1116,7 @@ end inited = false finaled = false cb3 = ModelingToolkit.SymbolicDiscreteCallback(1.0, f, initialize = a, finalize = b) - @mtkbuild sys = ODESystem(D(x) ~ -1, t, [x], []; discrete_events = [cb3]) + @mtkbuild sys = System(D(x) ~ -1, t, [x], []; discrete_events = [cb3]) prob = ODEProblem(sys, [x => 1.0], (0.0, 2), []) sol = solve(prob, Tsit5()) @test seen == true @@ -1127,7 +1127,7 @@ end inited = false finaled = false cb3 = ModelingToolkit.SymbolicDiscreteCallback([1.0], f, initialize = a, finalize = b) - @mtkbuild sys = ODESystem(D(x) ~ -1, t, [x], []; discrete_events = [cb3]) + @mtkbuild sys = System(D(x) ~ -1, t, [x], []; discrete_events = [cb3]) prob = ODEProblem(sys, [x => 1.0], (0.0, 2), []) sol = solve(prob, Tsit5()) @test seen == true @@ -1140,7 +1140,7 @@ end finaled = false cb3 = ModelingToolkit.SymbolicDiscreteCallback( t == 1.0, f, initialize = a, finalize = b) - @mtkbuild sys = ODESystem(D(x) ~ -1, t, [x], []; discrete_events = [cb3]) + @mtkbuild sys = System(D(x) ~ -1, t, [x], []; discrete_events = [cb3]) prob = ODEProblem(sys, [x => 1.0], (0.0, 2), []) sol = solve(prob, Tsit5(); tstops = 1.0) @test seen == true @@ -1152,17 +1152,17 @@ end @variables x(t) [irreducible = true] y(t) [irreducible = true] eqs = [x ~ y, D(x) ~ -1] cb = [x ~ 0.0] => [x ~ 0, y ~ 1] - @mtkbuild pend = ODESystem(eqs, t; continuous_events = [cb]) + @mtkbuild pend = System(eqs, t; continuous_events = [cb]) prob = ODEProblem(pend, [x => 1], (0.0, 3.0), guesses = [y => x]) @test_broken !SciMLBase.successful_retcode(solve(prob, Rodas5())) cb = [x ~ 0.0] => [y ~ 1] - @mtkbuild pend = ODESystem(eqs, t; continuous_events = [cb]) + @mtkbuild pend = System(eqs, t; continuous_events = [cb]) prob = ODEProblem(pend, [x => 1], (0.0, 3.0), guesses = [y => x]) @test_broken !SciMLBase.successful_retcode(solve(prob, Rodas5())) cb = [x ~ 0.0] => [x ~ 1, y ~ 1] - @mtkbuild pend = ODESystem(eqs, t; continuous_events = [cb]) + @mtkbuild pend = System(eqs, t; continuous_events = [cb]) prob = ODEProblem(pend, [x => 1], (0.0, 3.0), guesses = [y => x]) @test all(≈(0.0; atol = 1e-9), solve(prob, Rodas5())[[x, y]][end]) end @@ -1205,7 +1205,7 @@ end @set! m.x = 0.0 return m end - return ODESystem(eqs, t, vars, params; name = name, + return System(eqs, t, vars, params; name = name, continuous_events = [[x ~ max_time] => reset]) end @@ -1219,19 +1219,19 @@ end eqs = reduce(vcat, Symbolics.scalarize.([ D(x) ~ 1.0 ])) - return ODESystem(eqs, t, vars, params; name = name) # note no event + return System(eqs, t, vars, params; name = name) # note no event end @named wd1 = weird1(0.021) @named wd2 = weird2(0.021) - sys1 = structural_simplify(ODESystem([], t; name = :parent, + sys1 = structural_simplify(System([], t; name = :parent, discrete_events = [0.01 => ModelingToolkit.ImperativeAffect( modified = (; θs = reduce(vcat, [[wd1.θ]])), ctx = [1]) do m, o, c, i @set! m.θs[1] = c[] += 1 end], systems = [wd1])) - sys2 = structural_simplify(ODESystem([], t; name = :parent, + sys2 = structural_simplify(System([], t; name = :parent, discrete_events = [0.01 => ModelingToolkit.ImperativeAffect( modified = (; θs = reduce(vcat, [[wd2.θ]])), ctx = [1]) do m, o, c, i @set! m.θs[1] = c[] += 1 @@ -1253,7 +1253,7 @@ end D(D(y)) ~ λ * y - g x^2 + y^2 ~ 1] c_evt = [t ~ 5.0] => [x ~ Pre(x) + 0.1] - @mtkbuild pend = ODESystem(eqs, t, continuous_events = c_evt) + @mtkbuild pend = System(eqs, t, continuous_events = c_evt) prob = ODEProblem(pend, [x => -1, y => 0], (0.0, 10.0), [g => 1], guesses = [λ => 1]) sol = solve(prob, FBDF()) @test ≈(sol(5.000001, idxs = x) - sol(4.999999, idxs = x), 0.1, rtol = 1e-4) @@ -1261,7 +1261,7 @@ end # Implicit affect with Pre c_evt = [t ~ 5.0] => [x ~ Pre(x) + y^2] - @mtkbuild pend = ODESystem(eqs, t, continuous_events = c_evt) + @mtkbuild pend = System(eqs, t, continuous_events = c_evt) prob = ODEProblem(pend, [x => 1, y => 0], (0.0, 10.0), [g => 1], guesses = [λ => 1]) sol = solve(prob, FBDF()) @test ≈(sol(5.000001, idxs = y)^2 + sol(4.999999, idxs = x), @@ -1270,7 +1270,7 @@ end # Impossible affect errors c_evt = [t ~ 5.0] => [x ~ Pre(x) + 2] - @mtkbuild pend = ODESystem(eqs, t, continuous_events = c_evt) + @mtkbuild pend = System(eqs, t, continuous_events = c_evt) prob = ODEProblem(pend, [x => 1, y => 0], (0.0, 10.0), [g => 1], guesses = [λ => 1]) @test_throws UnsolvableCallbackError sol=solve(prob, FBDF()) @@ -1281,7 +1281,7 @@ end x^2 + y^2 ~ 1] c_evt = SymbolicContinuousCallback( [t ~ 5.0], [x ~ Pre(x) + 0.1, g ~ Pre(g) + 1], discrete_parameters = [g], iv = t) - @mtkbuild pend = ODESystem(eqs, t, continuous_events = c_evt) + @mtkbuild pend = System(eqs, t, continuous_events = c_evt) prob = ODEProblem(pend, [x => 1, y => 0], (0.0, 10.0), [g => 1], guesses = [λ => 1]) sol = solve(prob, FBDF()) @test sol.ps[g] ≈ [1, 2] @@ -1291,7 +1291,7 @@ end eqs = [y ~ g^2, D(x) ~ x] c_evt = SymbolicContinuousCallback( [t ~ 5.0], [x ~ Pre(x) + 1, g ~ Pre(g) + 1], discrete_parameters = [g], iv = t) - @mtkbuild sys = ODESystem(eqs, t, continuous_events = c_evt) + @mtkbuild sys = System(eqs, t, continuous_events = c_evt) prob = ODEProblem(sys, [x => 1.0], (0.0, 10.0), [g => 2]) sol = solve(prob, FBDF()) @test sol.ps[g] ≈ [2.0, 3.0] @@ -1300,7 +1300,7 @@ end # Parameters that don't appear in affects should not be mutated. c_evt = [t ~ 5.0] => [x ~ Pre(x) + 1] - @mtkbuild sys = ODESystem(eqs, t, continuous_events = c_evt) + @mtkbuild sys = System(eqs, t, continuous_events = c_evt) prob = ODEProblem(sys, [x => 0.5], (0.0, 10.0), [g => 2], guesses = [y => 0]) sol = solve(prob, FBDF()) @test prob.ps[g] == sol.ps[g] @@ -1320,14 +1320,14 @@ end @set! m.x = 0.0 return m end - return ODESystem(eqs, t, vars, []; name = name, + return System(eqs, t, vars, []; name = name, continuous_events = [[x ~ max_time] => reset]) end shared_pars = @parameters begin vals(t)[1:2] = 0.0 end - @named sys = ODESystem(Equation[], t, [], Symbolics.scalarize(vals); + @named sys = System(Equation[], t, [], Symbolics.scalarize(vals); systems = [child(vals; name = :child)]) sys = structural_simplify(sys) sol = solve(ODEProblem(sys, [], (0.0, 1.0)), Tsit5()) diff --git a/test/symbolic_indexing_interface.jl b/test/symbolic_indexing_interface.jl index 613bfc8213..b99ece927e 100644 --- a/test/symbolic_indexing_interface.jl +++ b/test/symbolic_indexing_interface.jl @@ -3,11 +3,11 @@ using ModelingToolkit: t_nounits as t, D_nounits as D, ParameterIndex, SymbolicContinuousCallback using SciMLStructures: Tunable -@testset "ODESystem" begin +@testset "System" begin @parameters a b @variables x(t)=1.0 y(t)=2.0 xy(t) eqs = [D(x) ~ a * y + t, D(y) ~ b * t] - @named odesys = ODESystem(eqs, t, [x, y], [a, b]; observed = [xy ~ x + y]) + @named odesys = System(eqs, t, [x, y], [a, b]; observed = [xy ~ x + y]) odesys = complete(odesys) @test SymbolicIndexingInterface.supports_tuple_observed(odesys) @test all(is_variable.((odesys,), [x, y, 1, 2, :x, :y])) @@ -45,7 +45,7 @@ using SciMLStructures: Tunable @test getter(prob) isa Tuple @test_nowarn @inferred getter(prob) - @named odesys = ODESystem( + @named odesys = System( eqs, t, [x, y], [a, b]; defaults = [xy => 3.0], observed = [xy ~ x + y]) odesys = complete(odesys) @test default_values(odesys)[xy] == 3.0 @@ -78,7 +78,7 @@ end # D(x) ~ -x + u # y ~ x] -# @mtkbuild cl = ODESystem(eqs, t) +# @mtkbuild cl = System(eqs, t) # partition1_params = [Hold(ud1), Sample(t, dt)(y), ud1, yd1] # partition2_params = [Hold(ud2), Sample(t, dt2)(y), ud2, yd2] # @test all( @@ -187,7 +187,7 @@ using SymbolicIndexingInterface @parameters p1[1:2]=[1.0, 2.0] p2[1:2]=[0.0, 0.0] @variables x(t) = 0 -@named sys = ODESystem( +@named sys = System( [D(x) ~ sum(p1) * t + sum(p2)], t; ) @@ -198,7 +198,7 @@ get_dep = @test_nowarn getu(prob, 2p1) @testset "Observed functions with variables as `Symbol`s" begin @variables x(t) y(t) z(t)[1:2] @parameters p1 p2[1:2, 1:2] - @mtkbuild sys = ODESystem([D(x) ~ x * t + p1, y ~ 2x, D(z) ~ p2 * z], t) + @mtkbuild sys = System([D(x) ~ x * t + p1, y ~ 2x, D(z) ~ p2 * z], t) prob = ODEProblem( sys, [x => 1.0, z => ones(2)], (0.0, 1.0), [p1 => 2.0, p2 => ones(2, 2)]) @test getu(prob, x)(prob) == getu(prob, :x)(prob) @@ -211,7 +211,7 @@ end @testset "Parameter dependencies as symbols" begin @variables x(t) = 1.0 @parameters a=1 b - @named model = ODESystem(D(x) ~ x + a - b, t, parameter_dependencies = [b ~ a + 1]) + @named model = System(D(x) ~ x + a - b, t, parameter_dependencies = [b ~ a + 1]) sys = complete(model) prob = ODEProblem(sys, [], (0.0, 1.0)) @test prob.ps[b] == prob.ps[:b] @@ -220,7 +220,7 @@ end @testset "`get_all_timeseries_indexes` with non-split systems" begin @variables x(t) y(t) z(t) @parameters a - @named sys = ODESystem([D(x) ~ a * x, y ~ 2x, z ~ 0.0], t) + @named sys = System([D(x) ~ a * x, y ~ 2x, z ~ 0.0], t) sys = structural_simplify(sys, split = false) for sym in [x, y, z, x + y, x + a, y / x] @test only(get_all_timeseries_indexes(sys, sym)) == ContinuousTimeseries() @@ -233,7 +233,7 @@ end @parameters p(t)[1:2, 1:2] ev = SymbolicContinuousCallback( [x[1] ~ 2.0] => [p ~ -ones(2, 2)], discrete_parameters = [p]) - @mtkbuild sys = ODESystem(D(x) ~ p * x, t; continuous_events = [ev]) + @mtkbuild sys = System(D(x) ~ p * x, t; continuous_events = [ev]) p = ModelingToolkit.unwrap(p) @test timeseries_parameter_index(sys, p) === ParameterTimeseriesIndex(1, (1, 1)) @test timeseries_parameter_index(sys, p[1, 1]) === diff --git a/test/symbolic_parameters.jl b/test/symbolic_parameters.jl index f4fa21e614..281002f626 100644 --- a/test/symbolic_parameters.jl +++ b/test/symbolic_parameters.jl @@ -59,7 +59,7 @@ vars = @variables(begin end) der = Differential(t) eqs = [der(x) ~ x] -@named sys = ODESystem(eqs, t, vars, [x0]) +@named sys = System(eqs, t, vars, [x0]) sys = complete(sys) pars = [ x0 => 10.0 diff --git a/test/test_variable_metadata.jl b/test/test_variable_metadata.jl index aaf6addb59..7ce45bb211 100644 --- a/test/test_variable_metadata.jl +++ b/test/test_variable_metadata.jl @@ -123,7 +123,7 @@ Dₜ = Differential(t) @parameters k2 [tunable = false] eqs = [Dₜ(x) ~ (-k2 * x + k * u) / T y ~ x] -sys = ODESystem(eqs, t, name = :tunable_first_order) +sys = System(eqs, t, name = :tunable_first_order) unk_meta = ModelingToolkit.dump_unknowns(sys) @test length(unk_meta) == 3 @test all(iszero, meta.default for meta in unk_meta) @@ -169,14 +169,14 @@ sp = Set(p) @independent_variables t @variables u(t) [description = "A short description of u"] @parameters p [description = "A description of p"] -@named sys = ODESystem([u ~ p], t) +@named sys = System([u ~ p], t) @test_nowarn show(stdout, "text/plain", sys) # Defaults, guesses overridden by system, parameter dependencies @variables x(t)=1.0 y(t) [guess = 1.0] @parameters p=2.0 q -@named sys = ODESystem(Equation[], t, [x, y], [p]; defaults = Dict(x => 2.0, p => 3.0), +@named sys = System(Equation[], t, [x, y], [p]; defaults = Dict(x => 2.0, p => 3.0), guesses = Dict(y => 2.0), parameter_dependencies = [q => 2p]) unks_meta = ModelingToolkit.dump_unknowns(sys) unks_meta = Dict([ModelingToolkit.getname(meta.var) => meta for meta in unks_meta]) diff --git a/test/units.jl b/test/units.jl index ff0cd42ac3..267b526c4b 100644 --- a/test/units.jl +++ b/test/units.jl @@ -44,42 +44,42 @@ D = Differential(t) eqs = [D(E) ~ P - E / τ 0 ~ P] @test UMT.validate(eqs) -@named sys = ODESystem(eqs, t) +@named sys = System(eqs, t) @test !UMT.validate(D(D(E)) ~ P) @test !UMT.validate(0 ~ P + E * τ) # Disabling unit validation/checks selectively -@test_throws MT.ArgumentError ODESystem(eqs, t, [E, P, t], [τ], name = :sys) -ODESystem(eqs, t, [E, P, t], [τ], name = :sys, checks = MT.CheckUnits) +@test_throws MT.ArgumentError System(eqs, t, [E, P, t], [τ], name = :sys) +System(eqs, t, [E, P, t], [τ], name = :sys, checks = MT.CheckUnits) eqs = [D(E) ~ P - E / τ 0 ~ P + E * τ] -@test_throws MT.ValidationError ODESystem(eqs, t, name = :sys, checks = MT.CheckAll) -@test_throws MT.ValidationError ODESystem(eqs, t, name = :sys, checks = true) -ODESystem(eqs, t, name = :sys, checks = MT.CheckNone) -ODESystem(eqs, t, name = :sys, checks = false) -@test_throws MT.ValidationError ODESystem(eqs, t, name = :sys, +@test_throws MT.ValidationError System(eqs, t, name = :sys, checks = MT.CheckAll) +@test_throws MT.ValidationError System(eqs, t, name = :sys, checks = true) +System(eqs, t, name = :sys, checks = MT.CheckNone) +System(eqs, t, name = :sys, checks = false) +@test_throws MT.ValidationError System(eqs, t, name = :sys, checks = MT.CheckComponents | MT.CheckUnits) -@named sys = ODESystem(eqs, t, checks = MT.CheckComponents) -@test_throws MT.ValidationError ODESystem(eqs, t, [E, P, t], [τ], name = :sys, +@named sys = System(eqs, t, checks = MT.CheckComponents) +@test_throws MT.ValidationError System(eqs, t, [E, P, t], [τ], name = :sys, checks = MT.CheckUnits) # connection validation @connector function Pin(; name) sts = @variables(v(t)=1.0, [unit = u"V"], i(t)=1.0, [unit = u"A", connect = Flow]) - ODESystem(Equation[], t, sts, []; name = name) + System(Equation[], t, sts, []; name = name) end @connector function OtherPin(; name) sts = @variables(v(t)=1.0, [unit = u"mV"], i(t)=1.0, [unit = u"mA", connect = Flow]) - ODESystem(Equation[], t, sts, []; name = name) + System(Equation[], t, sts, []; name = name) end @connector function LongPin(; name) sts = @variables(v(t)=1.0, [unit = u"V"], i(t)=1.0, [unit = u"A", connect = Flow], x(t)=1.0, [unit = NoUnits]) - ODESystem(Equation[], t, sts, []; name = name) + System(Equation[], t, sts, []; name = name) end @named p1 = Pin() @named p2 = Pin() @@ -91,8 +91,8 @@ bad_length_eqs = [connect(op, lp)] @test UMT.validate(good_eqs) @test !UMT.validate(bad_eqs) @test !UMT.validate(bad_length_eqs) -@named sys = ODESystem(good_eqs, t, [], []) -@test_throws MT.ValidationError ODESystem(bad_eqs, t, [], []; name = :sys) +@named sys = System(good_eqs, t, [], []) +@test_throws MT.ValidationError System(bad_eqs, t, [], []; name = :sys) # Array variables @independent_variables t [unit = u"s"] @@ -100,7 +100,7 @@ bad_length_eqs = [connect(op, lp)] @variables x(t)[1:3] [unit = u"m"] D = Differential(t) eqs = D.(x) .~ v -ODESystem(eqs, t, name = :sys) +System(eqs, t, name = :sys) # Nonlinear system @parameters a [unit = u"kg"^-1] @@ -139,12 +139,12 @@ noiseeqs = [0.1u"MW" 0.1u"MW" D = Differential(t) eqs = [D(L) ~ v, V ~ L^3] -@named sys = ODESystem(eqs, t) +@named sys = System(eqs, t) sys_simple = structural_simplify(sys) eqs = [D(V) ~ r, V ~ L^3] -@named sys = ODESystem(eqs, t) +@named sys = System(eqs, t) sys_simple = structural_simplify(sys) @variables V [unit = u"m"^3] L [unit = u"m"] diff --git a/test/variable_scope.jl b/test/variable_scope.jl index 59647bf441..a7cfe0a1af 100644 --- a/test/variable_scope.jl +++ b/test/variable_scope.jl @@ -57,10 +57,10 @@ p = [a ParentScope(ParentScope(c)) GlobalScope(d)] -level0 = ODESystem(Equation[], t, [], p; name = :level0) -level1 = ODESystem(Equation[], t, [], []; name = :level1) ∘ level0 -level2 = ODESystem(Equation[], t, [], []; name = :level2) ∘ level1 -level3 = ODESystem(Equation[], t, [], []; name = :level3) ∘ level2 +level0 = System(Equation[], t, [], p; name = :level0) +level1 = System(Equation[], t, [], []; name = :level1) ∘ level0 +level2 = System(Equation[], t, [], []; name = :level2) ∘ level1 +level3 = System(Equation[], t, [], []; name = :level3) ∘ level2 ps = ModelingToolkit.getname.(parameters(level3)) @@ -73,8 +73,8 @@ ps = ModelingToolkit.getname.(parameters(level3)) # Tests from PR#2354 @parameters xx[1:2] arr_p = [ParentScope(xx[1]), xx[2]] -arr0 = ODESystem(Equation[], t, [], arr_p; name = :arr0) -arr1 = ODESystem(Equation[], t, [], []; name = :arr1) ∘ arr0 +arr0 = System(Equation[], t, [], arr_p; name = :arr0) +arr1 = System(Equation[], t, [], []; name = :arr1) ∘ arr0 arr_ps = ModelingToolkit.getname.(parameters(arr1)) @test isequal(arr_ps[1], Symbol("xx")) @test isequal(arr_ps[2], Symbol("arr0₊xx")) @@ -82,13 +82,13 @@ arr_ps = ModelingToolkit.getname.(parameters(arr1)) function Foo(; name, p = 1) @parameters p = p @variables x(t) - return ODESystem(D(x) ~ p, t; name) + return System(D(x) ~ p, t; name) end function Bar(; name, p = 2) @parameters p = p @variables x(t) @named foo = Foo(; p) - return ODESystem(D(x) ~ p + t, t; systems = [foo], name) + return System(D(x) ~ p + t, t; systems = [foo], name) end @named bar = Bar() bar = complete(bar) @@ -108,15 +108,15 @@ defs = ModelingToolkit.defaults(bar) p3 = ParentScope(ParentScope(p3)) p4 = GlobalScope(p4) - @named sys1 = ODESystem([D(x1) ~ p1, D(x2) ~ p2, D(x3) ~ p3, D(x4) ~ p4], t) + @named sys1 = System([D(x1) ~ p1, D(x2) ~ p2, D(x3) ~ p3, D(x4) ~ p4], t) @test isequal(x1, only(unknowns(sys1))) @test isequal(p1, only(parameters(sys1))) - @named sys2 = ODESystem(Equation[], t; systems = [sys1]) + @named sys2 = System(Equation[], t; systems = [sys1]) @test length(unknowns(sys2)) == 2 @test any(isequal(x2), unknowns(sys2)) @test length(parameters(sys2)) == 2 @test any(isequal(p2), parameters(sys2)) - @named sys3 = ODESystem(Equation[], t) + @named sys3 = System(Equation[], t) sys3 = sys3 ∘ sys2 @test length(unknowns(sys3)) == 3 @test any(isequal(x3), unknowns(sys3)) diff --git a/test/variable_utils.jl b/test/variable_utils.jl index 1dc45e11ef..36b80a21dd 100644 --- a/test/variable_utils.jl +++ b/test/variable_utils.jl @@ -56,7 +56,7 @@ end ρ β end - sys = ODESystem( + sys = System( [D(D(x)) ~ σ * (y - x) D(y) ~ x * (ρ - z) - y D(z) ~ x * y - β * z], iv; name) @@ -68,12 +68,12 @@ end @parameters begin p[1:2, 1:2] end - sys = ODESystem([D(D(x)) ~ p * x], iv; name) + sys = System([D(D(x)) ~ p * x], iv; name) end function Outer(; name) @named 😄 = Lorenz() @named arr = ArrSys() - sys = ODESystem(Equation[], iv; name, systems = [😄, arr]) + sys = System(Equation[], iv; name, systems = [😄, arr]) end @mtkbuild sys = Outer() From e027e9324e819c8c4acaa0de9c1db6811993b606 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 22 Apr 2025 18:58:10 +0530 Subject: [PATCH 1688/2176] test: replace `NonlinearSystem` with `System` --- test/code_generation.jl | 2 +- test/components.jl | 4 +- test/dep_graphs.jl | 2 +- test/dq_units.jl | 6 +-- test/extensions/bifurcationkit.jl | 4 +- test/extensions/homotopy_continuation.jl | 42 +++++++++--------- test/initial_values.jl | 4 +- test/initializationsystem.jl | 10 ++--- test/modelingtoolkitize.jl | 2 +- test/namespacing.jl | 4 +- test/nonlinearsystem.jl | 54 +++++++++++------------ test/parameter_dependencies.jl | 4 +- test/reduction.jl | 4 +- test/scc_nonlinear_problem.jl | 20 ++++----- test/sciml_problem_inputs.jl | 2 +- test/structural_transformation/tearing.jl | 4 +- test/structural_transformation/utils.jl | 2 +- test/symbolic_indexing_interface.jl | 2 +- test/symbolic_parameters.jl | 4 +- test/units.jl | 6 +-- test/variable_scope.jl | 14 +++--- 21 files changed, 98 insertions(+), 98 deletions(-) diff --git a/test/code_generation.jl b/test/code_generation.jl index 87a485e198..0dc86871c5 100644 --- a/test/code_generation.jl +++ b/test/code_generation.jl @@ -31,7 +31,7 @@ using ModelingToolkit: t_nounits as t, D_nounits as D @test fn5(u0, p, 1.0) == 1.0 @variables x y[1:3] - sys = complete(NonlinearSystem(Equation[], [x; y], [p1, p2, p3, p4]; name = :sys)) + sys = complete(System(Equation[], [x; y], [p1, p2, p3, p4]; name = :sys)) p = MTKParameters(sys, []) fn1 = generate_custom_function(sys, x + y[1] + p1 + p2[1] + p3; expression = Val(false)) diff --git a/test/components.jl b/test/components.jl index 282c4c0aa7..ce3f7ecba2 100644 --- a/test/components.jl +++ b/test/components.jl @@ -335,8 +335,8 @@ end @test ModelingToolkit.get_metadata(sys) == "test" end @testset "NonlinearSystem" begin - @named inner = NonlinearSystem([0 ~ x^2 + 4x + 4], [x], []) - @named outer = NonlinearSystem( + @named inner = System([0 ~ x^2 + 4x + 4], [x], []) + @named outer = System( [0 ~ x^3 - y^3], [x, y], []; systems = [inner], metadata = "test") @test ModelingToolkit.get_metadata(outer) == "test" sys = complete(outer) diff --git a/test/dep_graphs.jl b/test/dep_graphs.jl index 76cc216635..3c7b88dd05 100644 --- a/test/dep_graphs.jl +++ b/test/dep_graphs.jl @@ -184,7 +184,7 @@ s_eqdeps = [[1], [2], [3]] eqs = [0 ~ σ * (y - x), 0 ~ ρ - y, 0 ~ y - β * z] -@named ns = NonlinearSystem(eqs, [x, y, z], [σ, ρ, β]) +@named ns = System(eqs, [x, y, z], [σ, ρ, β]) deps = equation_dependencies(ns) eq_sdeps = [[x, y], [y], [y, z]] @test all(i -> isequal(Set(deps[i]), Set(value.(eq_sdeps[i]))), 1:length(deps)) diff --git a/test/dq_units.jl b/test/dq_units.jl index 267e993971..c6cada3363 100644 --- a/test/dq_units.jl +++ b/test/dq_units.jl @@ -81,7 +81,7 @@ System(eqs, t, name = :sys) eqs = [ 0 ~ a * x ] -@named nls = NonlinearSystem(eqs, [x], [a]) +@named nls = System(eqs, [x], [a]) # SDE test w/ noise vector @parameters τ [unit = u"s"] Q [unit = u"W"] @@ -124,12 +124,12 @@ sys_simple = structural_simplify(sys) @parameters v [unit = u"m/s"] r [unit = u"m"^3 / u"s"] eqs = [V ~ r * t, V ~ L^3] -@named sys = NonlinearSystem(eqs, [V, L], [t, r]) +@named sys = System(eqs, [V, L], [t, r]) sys_simple = structural_simplify(sys) eqs = [L ~ v * t, V ~ L^3] -@named sys = NonlinearSystem(eqs, [V, L], [t, r]) +@named sys = System(eqs, [V, L], [t, r]) sys_simple = structural_simplify(sys) #Jump System diff --git a/test/extensions/bifurcationkit.jl b/test/extensions/bifurcationkit.jl index 227cd175e0..7b95ffd82e 100644 --- a/test/extensions/bifurcationkit.jl +++ b/test/extensions/bifurcationkit.jl @@ -8,7 +8,7 @@ let @parameters μ α eqs = [0 ~ μ * x - x^3 + α * y, 0 ~ -y] - @named nsys = NonlinearSystem(eqs, [x, y], [μ, α]) + @named nsys = System(eqs, [x, y], [μ, α]) nsys = complete(nsys) # Creates BifurcationProblem bif_par = μ @@ -103,7 +103,7 @@ let eqs = [0 ~ μ - x^3 + 2x^2, 0 ~ p * μ - y, 0 ~ y - z] - @named nsys = NonlinearSystem(eqs, [x, y, z], [μ, p]) + @named nsys = System(eqs, [x, y, z], [μ, p]) nsys = structural_simplify(nsys) # Creates BifurcationProblem. diff --git a/test/extensions/homotopy_continuation.jl b/test/extensions/homotopy_continuation.jl index 65f7d765a4..607c27346c 100644 --- a/test/extensions/homotopy_continuation.jl +++ b/test/extensions/homotopy_continuation.jl @@ -33,7 +33,7 @@ end eqs = [0 ~ x^2 + y^2 + 2x * y 0 ~ x^2 + 4x + 4 0 ~ y * z + 4x^2] - @mtkbuild sys = NonlinearSystem(eqs) + @mtkbuild sys = System(eqs) u0 = [x => 1.0, y => 1.0, z => 1.0] prob = HomotopyContinuationProblem(sys, u0) @test prob isa NonlinearProblem @@ -60,7 +60,7 @@ end 0 ~ x^2 + 4x + q 0 ~ y * z + 4x^2 + wrapper(r)] - @mtkbuild sys = NonlinearSystem(eqs) + @mtkbuild sys = System(eqs) prob = HomotopyContinuationProblem(sys, [x => 1.0, y => 1.0, z => 1.0], [p => 2.0, q => 4, r => Wrapper([1.0 1.0; 0.0 0.0])]) @test prob.ps[p] == 2.0 @@ -78,7 +78,7 @@ end @parameters p[1:3] _x = collect(x) eqs = collect(0 .~ vec(sum(_x * _x'; dims = 2)) + collect(p)) - @mtkbuild sys = NonlinearSystem(eqs) + @mtkbuild sys = System(eqs) prob = HomotopyContinuationProblem(sys, [x => ones(3)], [p => 1:3]) @test prob[x] == ones(3) @test prob[p + x] == [2, 3, 4] @@ -93,7 +93,7 @@ end @testset "Parametric exponents" begin @variables x = 1.0 @parameters n::Integer = 4 - @mtkbuild sys = NonlinearSystem([x^n + x^2 - 1 ~ 0]) + @mtkbuild sys = System([x^n + x^2 - 1 ~ 0]) prob = HomotopyContinuationProblem(sys, []) sol = solve(prob, singlerootalg) test_single_root(sol) @@ -103,34 +103,34 @@ end @testset "Polynomial check and warnings" begin @variables x = 1.0 - @mtkbuild sys = NonlinearSystem([x^1.5 + x^2 - 1 ~ 0]) + @mtkbuild sys = System([x^1.5 + x^2 - 1 ~ 0]) @test_throws ["Cannot convert", "Unable", "symbolically solve", "Exponent", "not an integer", "not a polynomial"] HomotopyContinuationProblem( sys, []) - @mtkbuild sys = NonlinearSystem([x^x - x ~ 0]) + @mtkbuild sys = System([x^x - x ~ 0]) @test_throws ["Cannot convert", "Unable", "symbolically solve", "Exponent", "unknowns", "not a polynomial"] HomotopyContinuationProblem( sys, []) - @mtkbuild sys = NonlinearSystem([((x^2) / sin(x))^2 + x ~ 0]) + @mtkbuild sys = System([((x^2) / sin(x))^2 + x ~ 0]) @test_throws ["Cannot convert", "both polynomial", "non-polynomial", "recognized", "sin", "not a polynomial"] HomotopyContinuationProblem( sys, []) @variables y = 2.0 - @mtkbuild sys = NonlinearSystem([x^2 + y^2 + 2 ~ 0, y ~ sin(x)]) + @mtkbuild sys = System([x^2 + y^2 + 2 ~ 0, y ~ sin(x)]) @test_throws ["Cannot convert", "recognized", "sin", "not a polynomial"] HomotopyContinuationProblem( sys, []) - @mtkbuild sys = NonlinearSystem([x^2 + y^2 - 2 ~ 0, sin(x + y) ~ 0]) + @mtkbuild sys = System([x^2 + y^2 - 2 ~ 0, sin(x + y) ~ 0]) @test_throws ["Cannot convert", "function of multiple unknowns"] HomotopyContinuationProblem( sys, []) - @mtkbuild sys = NonlinearSystem([sin(x)^2 + 1 ~ 0, cos(y) - cos(x) - 1 ~ 0]) + @mtkbuild sys = System([sin(x)^2 + 1 ~ 0, cos(y) - cos(x) - 1 ~ 0]) @test_throws ["Cannot convert", "multiple non-polynomial terms", "same unknown"] HomotopyContinuationProblem( sys, []) - @mtkbuild sys = NonlinearSystem([sin(x^2)^2 + sin(x^2) - 1 ~ 0]) + @mtkbuild sys = System([sin(x^2)^2 + sin(x^2) - 1 ~ 0]) @test_throws ["import Nemo"] HomotopyContinuationProblem(sys, []) end @@ -138,7 +138,7 @@ import Nemo @testset "With Nemo" begin @variables x = 2.0 - @mtkbuild sys = NonlinearSystem([sin(x^2)^2 + sin(x^2) - 1 ~ 0]) + @mtkbuild sys = System([sin(x^2)^2 + sin(x^2) - 1 ~ 0]) prob = HomotopyContinuationProblem(sys, []) @test prob[1] ≈ 2.0 # singlerootalg doesn't converge @@ -151,8 +151,8 @@ end @variables x=0.25 y=0.125 a = sin(x^2 - 4x + 1) b = cos(3log(y) + 4) - @mtkbuild sys = NonlinearSystem([(a^2 - 5a * b + 6b^2) / (a - 0.25) ~ 0 - (a^2 - 0.75a + 0.125) ~ 0]) + @mtkbuild sys = System([(a^2 - 5a * b + 6b^2) / (a - 0.25) ~ 0 + (a^2 - 0.75a + 0.125) ~ 0]) prob = HomotopyContinuationProblem(sys, []) @test prob[x] ≈ 0.25 @test prob[y] ≈ 0.125 @@ -165,7 +165,7 @@ end @testset "Rational functions" begin @variables x=2.0 y=2.0 @parameters n = 5 - @mtkbuild sys = NonlinearSystem([ + @mtkbuild sys = System([ 0 ~ (x^2 - n * x + 6) * (x - 1) / (x - 2) / (x - 3) ]) prob = HomotopyContinuationProblem(sys, []) @@ -178,7 +178,7 @@ end end end - @named sys = NonlinearSystem( + @named sys = System( [ 0 ~ (x - 2) / (x - 4) + ((x - 3) / (y - 7)) / ((x^2 - 4x + y) / (x - 2.5)), 0 ~ ((y - 3) / (y - 4)) * (n / (y - 5)) + ((x - 1.5) / (x - 5.5))^2 @@ -209,7 +209,7 @@ end @testset "Rational function in observed" begin @variables x=1 y=1 - @mtkbuild sys = NonlinearSystem([x^2 + y^2 - 2x - 2 ~ 0, y ~ (x - 1) / (x - 2)]) + @mtkbuild sys = System([x^2 + y^2 - 2x - 2 ~ 0, y ~ (x - 1) / (x - 2)]) prob = HomotopyContinuationProblem(sys, []) @test any(prob.f.denominator([2.0], parameter_values(prob)) .≈ 0.0) @test SciMLBase.successful_retcode(solve(prob, singlerootalg)) @@ -217,7 +217,7 @@ end @testset "Rational function forced to common denominators" begin @variables x = 1 - @mtkbuild sys = NonlinearSystem([0 ~ 1 / (1 + x) - x]) + @mtkbuild sys = System([0 ~ 1 / (1 + x) - x]) prob = HomotopyContinuationProblem(sys, []) @test any(prob.f.denominator([-1.0], parameter_values(prob)) .≈ 0.0) sol = solve(prob, singlerootalg) @@ -228,7 +228,7 @@ end @testset "Non-polynomial observed not used in equations" begin @variables x=1 y - @mtkbuild sys = NonlinearSystem([x^2 - 2 ~ 0, y ~ sin(x)]) + @mtkbuild sys = System([x^2 - 2 ~ 0, y ~ sin(x)]) prob = HomotopyContinuationProblem(sys, []) sol = @test_nowarn solve(prob, singlerootalg) @test sol[x] ≈ √2.0 @@ -237,8 +237,8 @@ end @testset "`fraction_cancel_fn`" begin @variables x = 1 - @named sys = NonlinearSystem([0 ~ ((x^2 - 5x + 6) / (x - 2) - 1) * (x^2 - 7x + 12) / - (x - 4)^3]) + @named sys = System([0 ~ ((x^2 - 5x + 6) / (x - 2) - 1) * (x^2 - 7x + 12) / + (x - 4)^3]) sys = complete(sys) @testset "`simplify_fractions`" begin diff --git a/test/initial_values.jl b/test/initial_values.jl index c030555b82..aca533acbd 100644 --- a/test/initial_values.jl +++ b/test/initial_values.jl @@ -104,7 +104,7 @@ prob = ODEProblem(sys, [], (0.0, 1.0), [A1 => 0.3]) System(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), + System(Equation[], [x, y], [p]; defaults = [y => nothing], name = :nsys), OptimizationSystem( Equation[], [x, y], [p]; defaults = [y => nothing], name = :optsys), ConstraintsSystem( @@ -245,7 +245,7 @@ end 0 ~ p[1] - X[1], 0 ~ p[2] - X[2] ] - @named nlsys = NonlinearSystem(eqs) + @named nlsys = System(eqs) nlsys = complete(nlsys) # Creates the `NonlinearProblem`. diff --git a/test/initializationsystem.jl b/test/initializationsystem.jl index 22fd6faf88..93a0845bdb 100644 --- a/test/initializationsystem.jl +++ b/test/initializationsystem.jl @@ -784,7 +784,7 @@ end eqs = [0 ~ σ * (y - x), 0 ~ x * (ρ - z) - y, 0 ~ x * y - β * z] - @mtkbuild ns = NonlinearSystem(eqs, [x, y, z], [σ, ρ, β]) + @mtkbuild ns = System(eqs, [x, y, z], [σ, ρ, β]) prob = NonlinearProblem(ns, []) @test prob.f.initialization_data.update_initializeprob! === nothing @@ -834,7 +834,7 @@ end # https://github.com/SciML/NonlinearSolve.jl/issues/586 eqs = [0 ~ -c * z + (q - z) * (x^2) 0 ~ p * (-x + (q - z) * x)] - @named sys = NonlinearSystem(eqs; initialization_eqs = [p^2 + q^2 + 2p * q ~ 0]) + @named sys = System(eqs; initialization_eqs = [p^2 + q^2 + 2p * q ~ 0]) sys = complete(sys) # @mtkbuild sys = NonlinearSystem( # [p * x^2 + q * y^3 ~ 0, x - q ~ 0]; defaults = [q => missing], @@ -1447,7 +1447,7 @@ end end @testset "$Problem" for Problem in [NonlinearProblem, NonlinearLeastSquaresProblem] @parameters p1 p2 - @mtkbuild sys = NonlinearSystem([x^2 + y^2 ~ p1, (x - 1)^2 + (y - 1)^2 ~ p2]; + @mtkbuild sys = System([x^2 + y^2 ~ p1, (x - 1)^2 + (y - 1)^2 ~ p2]; parameter_dependencies = [p2 ~ 2p1], guesses = [p1 => 0.0], defaults = [p1 => missing]) prob = Problem(sys, [x => 1.0, y => 1.0], [p2 => 6.0]) @@ -1466,7 +1466,7 @@ end initialization_eqs = [ X2 ~ Γ[1] - X1 ] - @mtkbuild nlsys = NonlinearSystem(eqs, [X1, X2], [k1, k2, Γ]; initialization_eqs) + @mtkbuild nlsys = System(eqs, [X1, X2], [k1, k2, Γ]; initialization_eqs) @testset "throws if initialization_eqs contain unknowns" begin u0 = [X1 => 1.0, X2 => 2.0] @@ -1479,7 +1479,7 @@ end initialization_eqs = [ Initial(X2) ~ Γ[1] - Initial(X1) ] - @mtkbuild nlsys = NonlinearSystem(eqs, [X1, X2], [k1, k2, Γ]; initialization_eqs) + @mtkbuild nlsys = System(eqs, [X1, X2], [k1, k2, Γ]; initialization_eqs) @testset "solves initialization" begin u0 = [X1 => 1.0, X2 => 2.0] diff --git a/test/modelingtoolkitize.jl b/test/modelingtoolkitize.jl index d146fa95c9..04e6263c7c 100644 --- a/test/modelingtoolkitize.jl +++ b/test/modelingtoolkitize.jl @@ -391,7 +391,7 @@ sys = modelingtoolkitize(prob) @testset "Nonlinear" begin @variables x=1.0 y=2.0 @parameters p=3.0 q=4.0 - @mtkbuild nlsys = NonlinearSystem([0 ~ p * y^2 + x, 0 ~ x + exp(x) * q]) + @mtkbuild nlsys = System([0 ~ p * y^2 + x, 0 ~ x + exp(x) * q]) prob1 = NonlinearProblem(nlsys, []) newsys = complete(modelingtoolkitize(prob1)) @test is_variable(newsys, newsys.x) diff --git a/test/namespacing.jl b/test/namespacing.jl index 3871398144..de33f9e927 100644 --- a/test/namespacing.jl +++ b/test/namespacing.jl @@ -110,7 +110,7 @@ end @testset "NonlinearSystem" begin @variables x @parameters p - sys = NonlinearSystem([x ~ p * x^2 + 1]; name = :inner) + sys = System([x ~ p * x^2 + 1]; name = :inner) @test !iscomplete(sys) @test does_namespacing(sys) @@ -129,7 +129,7 @@ end @test isequal(p, nsys.p) @test !isequal(p, sys.p) - @test_throws ["namespacing", "inner"] NonlinearSystem( + @test_throws ["namespacing", "inner"] System( Equation[]; systems = [nsys], name = :a) end diff --git a/test/nonlinearsystem.jl b/test/nonlinearsystem.jl index 3a22cba409..e120e75e07 100644 --- a/test/nonlinearsystem.jl +++ b/test/nonlinearsystem.jl @@ -26,7 +26,7 @@ end eqs = [0 ~ σ * (y - x) * h, 0 ~ x * (ρ - z) - y, 0 ~ x * y - β * z] -@named ns = NonlinearSystem(eqs, [x, y, z], [σ, ρ, β], defaults = Dict(x => 2)) +@named ns = System(eqs, [x, y, z], [σ, ρ, β], defaults = Dict(x => 2)) @test eval(toexpr(ns)) == ns test_nlsys_inference("standard", ns, (x, y, z), (σ, ρ, β)) @test begin @@ -44,7 +44,7 @@ end eqs = [0 ~ σ * (y - x), y ~ x * (ρ - z), β * z ~ x * y] -@named ns = NonlinearSystem(eqs, [x, y, z], [σ, ρ, β]) +@named ns = System(eqs, [x, y, z], [σ, ρ, β]) jac = calculate_jacobian(ns) @testset "nlsys jacobian" begin @test canonequal(jac[1, 1], σ * -1) @@ -67,7 +67,7 @@ a = y - x eqs = [0 ~ σ * a * h, 0 ~ x * (ρ - z) - y, 0 ~ x * y - β * z] -@named ns = NonlinearSystem(eqs, [x, y, z], [σ, ρ, β]) +@named ns = System(eqs, [x, y, z], [σ, ρ, β]) ns = complete(ns) nlsys_func = generate_function(ns, [x, y, z], [σ, ρ, β]) nf = NonlinearFunction(ns) @@ -102,11 +102,11 @@ eqs1 = [ 0 ~ x + y - z - u ] -lorenz = name -> NonlinearSystem(eqs1, [x, y, z, u, F], [σ, ρ, β], name = name) +lorenz = name -> System(eqs1, [x, y, z, u, F], [σ, ρ, β], name = name) lorenz1 = lorenz(:lorenz1) @test_throws ArgumentError NonlinearProblem(complete(lorenz1), zeros(5), zeros(3)) lorenz2 = lorenz(:lorenz2) -@named connected = NonlinearSystem( +@named connected = System( [s ~ a + lorenz1.x lorenz2.y ~ s * h lorenz1.F ~ lorenz2.u @@ -135,7 +135,7 @@ sol = solve(prob, FBDF(), reltol = 1e-7, abstol = 1e-7) eqs = [0 ~ σ * (y - x), 0 ~ x * (ρ - z) - y, 0 ~ x * y - β * z * h] -@named ns = NonlinearSystem(eqs, [x, y, z], [σ, ρ, β]) +@named ns = System(eqs, [x, y, z], [σ, ρ, β]) np = NonlinearProblem( complete(ns), [0, 0, 0], [σ => 1, ρ => 2, β => 3], jac = true, sparse = true) @test calculate_jacobian(ns, sparse = true) isa SparseMatrixCSC @@ -146,13 +146,13 @@ np = NonlinearProblem( @parameters a @variables x f - NonlinearSystem([0 ~ -a * x + f], [x, f], [a]; name) + System([0 ~ -a * x + f], [x, f], [a]; name) end function issue819() sys1 = makesys(:sys1) sys2 = makesys(:sys1) - @test_throws ArgumentError NonlinearSystem([sys2.f ~ sys1.x, sys1.f ~ 0], [], [], + @test_throws ArgumentError System([sys2.f ~ sys1.x, sys1.f ~ 0], [], [], systems = [sys1, sys2], name = :foo) end issue819() @@ -169,8 +169,8 @@ end 0 ~ b * y ] - @named sys1 = NonlinearSystem(eqs1, [x], [a]) - @named sys2 = NonlinearSystem(eqs2, [y], [b]) + @named sys1 = System(eqs1, [x], [a]) + @named sys2 = System(eqs2, [y], [b]) @named sys3 = extend(sys1, sys2) @test isequal(union(Set(parameters(sys1)), Set(parameters(sys2))), @@ -183,7 +183,7 @@ end @independent_variables t @parameters τ @variables x(t) RHS(t) -@named fol = NonlinearSystem([0 ~ (1 - x * h) / τ], [x], [τ]; +@named fol = System([0 ~ (1 - x * h) / τ], [x], [τ]; observed = [RHS ~ (1 - x) / τ]) @test isequal(RHS, @nonamespace fol.RHS) RHS2 = RHS @@ -208,7 +208,7 @@ eq = [v1 ~ sin(2pi * t * h) u[3] ~ 1, u[4] ~ h] - sys = NonlinearSystem(eqs, collect(u[1:4]), Num[], defaults = Dict([]), name = :test) + sys = System(eqs, collect(u[1:4]), Num[], defaults = Dict([]), name = :test) sys = complete(sys) prob = NonlinearProblem(sys, ones(length(unknowns(sys)))) @@ -222,7 +222,7 @@ end eqs = [0 ~ a * x] testdict = Dict([:test => 1]) -@named sys = NonlinearSystem(eqs, [x], [a], metadata = testdict) +@named sys = System(eqs, [x], [a], metadata = testdict) @test get_metadata(sys) == testdict @testset "Remake" begin @@ -233,7 +233,7 @@ testdict = Dict([:test => 1]) eqs = [0 ~ a * (y - x) * h, 0 ~ x * (b - z) - y, 0 ~ x * y - c * z] - @named sys = NonlinearSystem(eqs, [x, y, z], [a, b, c], defaults = Dict(x => 2.0)) + @named sys = System(eqs, [x, y, z], [a, b, c], defaults = Dict(x => 2.0)) sys = complete(sys) prob = NonlinearProblem(sys, ones(length(unknowns(sys)))) @@ -254,7 +254,7 @@ end eqs = [0 ~ x + sin(y), 0 ~ z - cos(x), 0 ~ x * y] - @named ns = NonlinearSystem(eqs, [x, y, z], []) + @named ns = System(eqs, [x, y, z], []) ns = complete(ns) vs = [unknowns(ns); parameters(ns)] ss_mtk = structural_simplify(ns) @@ -268,7 +268,7 @@ end @variables X(t) alg_eqs = [0 ~ p - d * X] -sys = @test_nowarn NonlinearSystem(alg_eqs; name = :name) +sys = @test_nowarn System(alg_eqs; name = :name) @test isequal(only(unknowns(sys)), X) @test all(isequal.(parameters(sys), [p, d])) @@ -276,14 +276,14 @@ sys = @test_nowarn NonlinearSystem(alg_eqs; name = :name) @variables u1 u2 @parameters u3 u4 eqs = [u3 ~ u1 + u2, u4 ~ 2 * (u1 + u2), u3 + u4 ~ 3 * (u1 + u2)] -@named ns = NonlinearSystem(eqs, [u1, u2], [u3, u4]) +@named ns = System(eqs, [u1, u2], [u3, u4]) sys = structural_simplify(ns; fully_determined = false) @test length(unknowns(sys)) == 1 # Conservative @variables X(t) alg_eqs = [1 ~ 2X] -@named ns = NonlinearSystem(alg_eqs) +@named ns = System(alg_eqs) sys = structural_simplify(ns) @test length(equations(sys)) == 0 sys = structural_simplify(ns; conservative = true) @@ -298,7 +298,7 @@ sys = structural_simplify(ns; conservative = true) 0 ~ x * y - β * z] guesses = [x => 1.0, z => 0.0] ps = [σ => 10.0, ρ => 26.0, β => 8 / 3] - @mtkbuild ns = NonlinearSystem(eqs) + @mtkbuild ns = System(eqs) @test isequal(calculate_jacobian(ns), [(-1 - z + ρ)*σ -x*σ 2x*(-z + ρ) -β-(x^2)]) @@ -315,7 +315,7 @@ sys = structural_simplify(ns; conservative = true) # system that contains a chain of observed variables when simplified @variables x y z eqs = [0 ~ x^2 + 2z + y, z ~ y, y ~ x] # analytical solution x = y = z = 0 or -3 - @mtkbuild ns = NonlinearSystem(eqs) # solve for y with observed chain z -> x -> y + @mtkbuild ns = System(eqs) # solve for y with observed chain z -> x -> y @test isequal(expand.(calculate_jacobian(ns)), [3 // 2 + y;;]) @test isequal(calculate_hessian(ns), [[1;;]]) prob = NonlinearProblem(ns, unknowns(ns) .=> -4.0) # give guess < -3 to reach -3 @@ -325,7 +325,7 @@ end @testset "Passing `nothing` to `u0`" begin @variables x = 1 - @mtkbuild sys = NonlinearSystem([0 ~ x^2 - x^3 + 3]) + @mtkbuild sys = System([0 ~ x^2 - x^3 + 3]) prob = @test_nowarn NonlinearProblem(sys, nothing) @test_nowarn solve(prob) end @@ -337,7 +337,7 @@ end 2 -2 4 -1 1/2 -1] b = [1, -2, 0] - @named sys = NonlinearSystem(A * x ~ b, [x], []) + @named sys = System(A * x ~ b, [x], []) sys = structural_simplify(sys) prob = NonlinearProblem(sys, unknowns(sys) .=> 0.0) sol = solve(prob) @@ -347,7 +347,7 @@ end @testset "resid_prototype when system has no unknowns and an equation" begin @variables x @parameters p - @named sys = NonlinearSystem([x ~ 1, x^2 - p ~ 0]) + @named sys = System([x ~ 1, x^2 - p ~ 0]) for sys in [ structural_simplify(sys, fully_determined = false), structural_simplify(sys, fully_determined = false, split = false) @@ -365,7 +365,7 @@ end @testset "IntervalNonlinearProblem" begin @variables x @parameters p - @named nlsys = NonlinearSystem([0 ~ x * x - p]) + @named nlsys = System([0 ~ x * x - p]) for sys in [complete(nlsys), complete(nlsys; split = false)] prob = IntervalNonlinearProblem(sys, (0.0, 2.0), [p => 1.0]) @@ -375,7 +375,7 @@ end end @variables y - @mtkbuild sys = NonlinearSystem([0 ~ x * x - p * x + p, 0 ~ x * y + p]) + @mtkbuild sys = System([0 ~ x * x - p * x + p, 0 ~ x * y + p]) @test_throws ["single equation", "unknown"] IntervalNonlinearProblem(sys, (0.0, 1.0)) @test_throws ["single equation", "unknown"] IntervalNonlinearFunction(sys, (0.0, 1.0)) @test_throws ["single equation", "unknown"] IntervalNonlinearProblemExpr( @@ -388,7 +388,7 @@ end @variables x y @parameters p[1:2] (f::Function)(..) - @mtkbuild sys = NonlinearSystem([x^2 - p[1]^2 ~ 0, y^2 ~ f(p)]) + @mtkbuild sys = System([x^2 - p[1]^2 ~ 0, y^2 ~ f(p)]) @test !any(isequal(p[1]), parameters(sys)) @test is_parameter(sys, p) end @@ -445,7 +445,7 @@ end @testset "oop `NonlinearLeastSquaresProblem` with `u0 === nothing`" begin @variables x y - @named sys = NonlinearSystem([0 ~ x - y], [], []; observed = [x ~ 1.0, y ~ 1.0]) + @named sys = System([0 ~ x - y], [], []; observed = [x ~ 1.0, y ~ 1.0]) prob = NonlinearLeastSquaresProblem{false}(complete(sys), nothing) sol = solve(prob) resid = sol.resid diff --git a/test/parameter_dependencies.jl b/test/parameter_dependencies.jl index 6f4f56c140..eb42098fd3 100644 --- a/test/parameter_dependencies.jl +++ b/test/parameter_dependencies.jl @@ -328,7 +328,7 @@ end @parameters p1=1.0 p2=1.0 @variables x(t) eqs = [0 ~ p1 * x * exp(x) + p2] - @mtkbuild sys = NonlinearSystem(eqs; parameter_dependencies = [p2 => 2p1]) + @mtkbuild sys = System(eqs; parameter_dependencies = [p2 => 2p1]) @test isequal(only(parameters(sys)), p1) @test Set(full_parameters(sys)) == Set([p1, p2, Initial(p2), Initial(x)]) prob = NonlinearProblem(sys, [x => 1.0]) @@ -383,7 +383,7 @@ end @variables x(t) y(t) @named sys = System([D(x) ~ y + p2], t; parameter_dependencies = [p2 ~ 2p1]) @test is_parameter(sys, p1) - @named sys = NonlinearSystem([x * y^2 ~ y + p2]; parameter_dependencies = [p2 ~ 2p1]) + @named sys = System([x * y^2 ~ y + p2]; parameter_dependencies = [p2 ~ 2p1]) @test is_parameter(sys, p1) k = ShiftIndex(t) @named sys = DiscreteSystem( diff --git a/test/reduction.jl b/test/reduction.jl index 7e988a2d55..e75b2afdee 100644 --- a/test/reduction.jl +++ b/test/reduction.jl @@ -154,7 +154,7 @@ end eqs = [u1 ~ u2 u3 ~ u1 + u2 + p u3 ~ hypot(u1, u2) * p] -@named sys = NonlinearSystem(eqs, [u1, u2, u3], [p]) +@named sys = System(eqs, [u1, u2, u3], [p]) reducedsys = structural_simplify(sys) @test length(observed(reducedsys)) == 2 @@ -174,7 +174,7 @@ N = 5 @variables xs[1:N] A = reshape(1:(N^2), N, N) eqs = xs ~ A * xs -@named sys′ = NonlinearSystem(eqs, [xs], []) +@named sys′ = System(eqs, [xs], []) sys = structural_simplify(sys′) @test length(equations(sys)) == 3 && length(observed(sys)) == 3 diff --git a/test/scc_nonlinear_problem.jl b/test/scc_nonlinear_problem.jl index f98f588032..3759508c0d 100644 --- a/test/scc_nonlinear_problem.jl +++ b/test/scc_nonlinear_problem.jl @@ -21,7 +21,7 @@ using ModelingToolkit: t_nounits as t, D_nounits as D eqs = Any[0 for _ in 1:8] f!(eqs, u, nothing) eqs = 0 .~ eqs - @named model = NonlinearSystem(eqs) + @named model = System(eqs) @test_throws ["simplified", "required"] SCCNonlinearProblem(model, []) _model = structural_simplify(model; split = false) @test_throws ["not compatible"] SCCNonlinearProblem(_model, []) @@ -85,7 +85,7 @@ end @variables u[1:5] [irreducible = true] @parameters p1[1:6] p2 eqs = 0 .~ collect(nlf(u, (u0, (p1, p2)))) - @mtkbuild sys = NonlinearSystem(eqs, [u], [p1, p2]) + @mtkbuild sys = System(eqs, [u], [p1, p2]) sccprob = SCCNonlinearProblem(sys, [u => u0], [p1 => p[1], p2 => p[2][]]) sccsol = solve(sccprob, SimpleNewtonRaphson(); abstol = 1e-9) @test SciMLBase.successful_retcode(sccsol) @@ -141,7 +141,7 @@ end eqs = 0 .~ eqs subrules = Dict(Symbolics.unwrap(D(y[i])) => ((y[i] - u0[i]) / dt) for i in 1:8) eqs = substitute.(eqs, (subrules,)) - @mtkbuild sys = NonlinearSystem(eqs) + @mtkbuild sys = System(eqs) prob = NonlinearProblem(sys, [y => u0], [t => t0]) sol = solve(prob, NewtonRaphson(); abstol = 1e-12) @@ -159,10 +159,10 @@ end x + y end @register_symbolic func(x, y) - @mtkbuild sys = NonlinearSystem([0 ~ x[1]^3 + x[2]^3 - 5 - 0 ~ sin(x[1] - x[2]) - 0.5 - 0 ~ func(x[1], x[2]) * exp(x[3]) - x[4]^3 - 5 - 0 ~ func(x[1], x[2]) * exp(x[4]) - x[3]^3 - 4]) + @mtkbuild sys = System([0 ~ x[1]^3 + x[2]^3 - 5 + 0 ~ sin(x[1] - x[2]) - 0.5 + 0 ~ func(x[1], x[2]) * exp(x[3]) - x[4]^3 - 5 + 0 ~ func(x[1], x[2]) * exp(x[4]) - x[3]^3 - 4]) sccprob = SCCNonlinearProblem(sys, []) sccsol = solve(sccprob, NewtonRaphson()) @test SciMLBase.successful_retcode(sccsol) @@ -264,7 +264,7 @@ end @testset "Array variables split across SCCs" begin @variables x[1:3] @parameters (f::Function)(..) - @mtkbuild sys = NonlinearSystem([ + @mtkbuild sys = System([ 0 ~ x[1]^2 - 9, x[2] ~ 2x[1], 0 ~ x[3]^2 - x[1]^2 + f(x)]) prob = SCCNonlinearProblem(sys, [x => ones(3)], [f => sum]) sol = solve(prob, NewtonRaphson()) @@ -274,7 +274,7 @@ end @testset "SCCNonlinearProblem retains parameter order" begin @variables x y z @parameters σ β ρ - @mtkbuild fullsys = NonlinearSystem( + @mtkbuild fullsys = System( [0 ~ x^3 * β + y^3 * ρ - σ, 0 ~ x^2 + 2x * y + y^2, 0 ~ z^2 - 4z + 4], [x, y, z], [σ, β, ρ]) @@ -294,7 +294,7 @@ end @variables x y @parameters p[1:2] (f::Function)(..) - @mtkbuild sys = NonlinearSystem([x^2 - p[1]^2 ~ 0, y^2 ~ f(p)]) + @mtkbuild sys = System([x^2 - p[1]^2 ~ 0, y^2 ~ f(p)]) prob = SCCNonlinearProblem(sys, [x => 1.0, y => 1.0], [p => ones(2), f => sum]) @test_nowarn solve(prob, NewtonRaphson()) end diff --git a/test/sciml_problem_inputs.jl b/test/sciml_problem_inputs.jl index deae4b4772..a1b9d1e416 100644 --- a/test/sciml_problem_inputs.jl +++ b/test/sciml_problem_inputs.jl @@ -42,7 +42,7 @@ begin ssys = complete(SDESystem( diff_eqs, noise_eqs, t, [X, Y, Z], [kp, kd, k1, k2]; name = :ssys)) jsys = complete(JumpSystem(jumps, t, [X, Y, Z], [kp, kd, k1, k2]; name = :jsys)) - nsys = complete(NonlinearSystem(alg_eqs; name = :nsys)) + nsys = complete(System(alg_eqs; name = :nsys)) u0_alts = [ # Vectors not providing default values. diff --git a/test/structural_transformation/tearing.jl b/test/structural_transformation/tearing.jl index 24083b3524..5bf88e1dd5 100644 --- a/test/structural_transformation/tearing.jl +++ b/test/structural_transformation/tearing.jl @@ -18,7 +18,7 @@ eqs = [ 0 ~ u4 - hypot(u2, u3), 0 ~ u5 - hypot(u4, u1) ] -@named sys = NonlinearSystem(eqs, [u1, u2, u3, u4, u5], []) +@named sys = System(eqs, [u1, u2, u3, u4, u5], []) state = TearingState(sys) StructuralTransformations.find_solvables!(state) @@ -133,7 +133,7 @@ eqs = [ 0 ~ z + y, 0 ~ x + z ] -@named nlsys = NonlinearSystem(eqs, [x, y, z], []) +@named nlsys = System(eqs, [x, y, z], []) newsys = tearing(nlsys) @test length(equations(newsys)) <= 1 diff --git a/test/structural_transformation/utils.jl b/test/structural_transformation/utils.jl index 7008a31111..af3be46572 100644 --- a/test/structural_transformation/utils.jl +++ b/test/structural_transformation/utils.jl @@ -275,7 +275,7 @@ end end @testset "`NonlinearSystem`" begin @variables x y z - @mtkbuild sys = NonlinearSystem([x^2 ~ 2y^2 + 1, sin(z) ~ y, z^3 + 4z + 1 ~ 0]) + @mtkbuild sys = System([x^2 ~ 2y^2 + 1, sin(z) ~ y, z^3 + 4z + 1 ~ 0]) mapping = map_variables_to_equations(sys) @test mapping[x] == (0 ~ 2y^2 + 1 - x^2) @test mapping[y] == (y ~ sin(z)) diff --git a/test/symbolic_indexing_interface.jl b/test/symbolic_indexing_interface.jl index b99ece927e..9c478565fa 100644 --- a/test/symbolic_indexing_interface.jl +++ b/test/symbolic_indexing_interface.jl @@ -109,7 +109,7 @@ end eqs = [0 ~ σ * (y - x), 0 ~ x * (ρ - z) - y, 0 ~ x * y - β * z] - @named ns = NonlinearSystem(eqs, [x, y, z], [σ, ρ, β]) + @named ns = System(eqs, [x, y, z], [σ, ρ, β]) ns = complete(ns) @test SymbolicIndexingInterface.supports_tuple_observed(ns) @test !is_time_dependent(ns) diff --git a/test/symbolic_parameters.jl b/test/symbolic_parameters.jl index 281002f626..6a6a434ccc 100644 --- a/test/symbolic_parameters.jl +++ b/test/symbolic_parameters.jl @@ -20,7 +20,7 @@ u0 = [ y => σ, # default u0 from default p z => u - 0.1 ] -ns = NonlinearSystem(eqs, [x, y, z], [σ, ρ, β], name = :ns, defaults = [par; u0]) +ns = System(eqs, [x, y, z], [σ, ρ, β], name = :ns, defaults = [par; u0]) ns.y = u * 1.1 resolved = ModelingToolkit.varmap_to_vars(Dict(), parameters(ns), defaults = ModelingToolkit.defaults(ns)) @@ -32,7 +32,7 @@ sol = solve(prob, NewtonRaphson()) @variables a @parameters b -top = NonlinearSystem([0 ~ -a + ns.x + b], [a], [b], systems = [ns], name = :top) +top = System([0 ~ -a + ns.x + b], [a], [b], systems = [ns], name = :top) top.b = ns.σ * 0.5 top.ns.x = u * 0.5 diff --git a/test/units.jl b/test/units.jl index 267b526c4b..2bd67f2745 100644 --- a/test/units.jl +++ b/test/units.jl @@ -108,7 +108,7 @@ System(eqs, t, name = :sys) eqs = [ 0 ~ a * x ] -@named nls = NonlinearSystem(eqs, [x], [a]) +@named nls = System(eqs, [x], [a]) # SDE test w/ noise vector @independent_variables t [unit = u"ms"] @@ -151,12 +151,12 @@ sys_simple = structural_simplify(sys) @parameters v [unit = u"m/s"] r [unit = u"m"^3 / u"s"] t [unit = u"s"] eqs = [V ~ r * t, V ~ L^3] -@named sys = NonlinearSystem(eqs, [V, L], [t, r]) +@named sys = System(eqs, [V, L], [t, r]) sys_simple = structural_simplify(sys) eqs = [L ~ v * t, V ~ L^3] -@named sys = NonlinearSystem(eqs, [V, L], [t, r]) +@named sys = System(eqs, [V, L], [t, r]) sys_simple = structural_simplify(sys) #Jump System diff --git a/test/variable_scope.jl b/test/variable_scope.jl index a7cfe0a1af..6d7d20d948 100644 --- a/test/variable_scope.jl +++ b/test/variable_scope.jl @@ -22,11 +22,11 @@ eqs = [0 ~ a 0 ~ b 0 ~ c 0 ~ d] -@named sub4 = NonlinearSystem(eqs, [a, b, c, d], []) -@named sub3 = NonlinearSystem(eqs, [a, b, c, d], []) -@named sub2 = NonlinearSystem([], [], [], systems = [sub3, sub4]) -@named sub1 = NonlinearSystem([], [], [], systems = [sub2]) -@named sys = NonlinearSystem([], [], [], systems = [sub1]) +@named sub4 = System(eqs, [a, b, c, d], []) +@named sub3 = System(eqs, [a, b, c, d], []) +@named sub2 = System([], [], [], systems = [sub3, sub4]) +@named sub1 = System([], [], [], systems = [sub2]) +@named sys = System([], [], [], systems = [sub1]) names = ModelingToolkit.getname.(unknowns(sys)) @test :d in names @@ -35,8 +35,8 @@ names = ModelingToolkit.getname.(unknowns(sys)) @test Symbol("sub1₊sub2₊sub3₊a") in names @test Symbol("sub1₊sub2₊sub4₊a") in names -@named foo = NonlinearSystem(eqs, [a, b, c, d], []) -@named bar = NonlinearSystem(eqs, [a, b, c, d], []) +@named foo = System(eqs, [a, b, c, d], []) +@named bar = System(eqs, [a, b, c, d], []) @test ModelingToolkit.getname(ModelingToolkit.namespace_expr( ModelingToolkit.namespace_expr(b, foo), From a0c13945cea8f14986bc704d726cb1610c48cd47 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 22 Apr 2025 18:45:51 +0530 Subject: [PATCH 1689/2176] test: replace `ImplicitDiscreteSystem` with `System` --- src/systems/callbacks.jl | 4 ++-- test/implicit_discrete_system.jl | 10 +++++----- test/namespacing.jl | 4 ++-- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/systems/callbacks.jl b/src/systems/callbacks.jl index 4e24f1b765..891943c6e7 100644 --- a/src/systems/callbacks.jl +++ b/src/systems/callbacks.jl @@ -57,7 +57,7 @@ end struct AffectSystem """The internal implicit discrete system whose equations are solved to obtain values after the affect.""" - system::ImplicitDiscreteSystem + system::AbstractSystem """Unknowns of the parent ODESystem whose values are modified or accessed by the affect.""" unknowns::Vector """Parameters of the parent ODESystem whose values are accessed by the affect.""" @@ -307,7 +307,7 @@ function make_affect(affect::Vector{Equation}; discrete_parameters = Any[], affect = Symbolics.fast_substitute(affect, subs) alg_eqs = Symbolics.fast_substitute(alg_eqs, subs) - @named affectsys = ImplicitDiscreteSystem( + @named affectsys = System( vcat(affect, alg_eqs), iv, collect(union(_dvs, discretes)), collect(union(pre_params, sys_params))) affectsys = structural_simplify(affectsys; fully_determined = nothing) diff --git a/test/implicit_discrete_system.jl b/test/implicit_discrete_system.jl index 932b6c6981..45c89f969e 100644 --- a/test/implicit_discrete_system.jl +++ b/test/implicit_discrete_system.jl @@ -7,7 +7,7 @@ rng = StableRNG(22525) @testset "Correct ImplicitDiscreteFunction" begin @variables x(t) = 1 - @mtkbuild sys = ImplicitDiscreteSystem([x(k) ~ x(k) * x(k - 1) - 3], t) + @mtkbuild sys = System([x(k) ~ x(k) * x(k - 1) - 3], t) tspan = (0, 10) # u[2] - u_next[1] @@ -27,7 +27,7 @@ rng = StableRNG(22525) prob = ImplicitDiscreteProblem(sys, [], tspan) @test prob.u0 == [1.0, 1.0] @variables x(t) - @mtkbuild sys = ImplicitDiscreteSystem([x(k) ~ x(k) * x(k - 1) - 3], t) + @mtkbuild sys = System([x(k) ~ x(k) * x(k - 1) - 3], t) @test_throws ErrorException prob=ImplicitDiscreteProblem(sys, [], tspan) end @@ -35,7 +35,7 @@ end @variables x(t) y(t) eqs = [x(k) ~ x(k - 1) + x(k - 2), x^2 ~ 1 - y^2] - @mtkbuild sys = ImplicitDiscreteSystem(eqs, t) + @mtkbuild sys = System(eqs, t) f = ImplicitDiscreteFunction(sys) function correct_f(u_next, u, p, t) @@ -62,7 +62,7 @@ end eqs = [x(k) ~ x(k - 1) + x(k - 2), y(k) ~ x(k) + x(k - 2) * z(k - 1), x + y + z ~ 2] - @mtkbuild sys = ImplicitDiscreteSystem(eqs, t) + @mtkbuild sys = System(eqs, t) @test length(unknowns(sys)) == length(equations(sys)) == 3 @test occursin("var\"y(t)\"", string(ImplicitDiscreteFunctionExpr(sys))) @@ -70,6 +70,6 @@ end eqs = [z(k) ~ x(k) + sin(x(k)), y(k) ~ x(k - 1) + x(k - 2), z(k) * x(k) ~ 3] - @mtkbuild sys = ImplicitDiscreteSystem(eqs, t) + @mtkbuild sys = System(eqs, t) @test occursin("var\"Shift(t, 1)(z(t))\"", string(ImplicitDiscreteFunctionExpr(sys))) end diff --git a/test/namespacing.jl b/test/namespacing.jl index de33f9e927..7ae4702304 100644 --- a/test/namespacing.jl +++ b/test/namespacing.jl @@ -84,7 +84,7 @@ end @variables x(t) @parameters p k = ShiftIndex(t) - sys = ImplicitDiscreteSystem([x(k) ~ p + x(k - 1) * x(k)], t; name = :inner) + sys = System([x(k) ~ p + x(k - 1) * x(k)], t; name = :inner) @test !iscomplete(sys) @test does_namespacing(sys) @@ -103,7 +103,7 @@ end @test isequal(p, nsys.p) @test !isequal(p, sys.p) - @test_throws ["namespacing", "inner"] ImplicitDiscreteSystem( + @test_throws ["namespacing", "inner"] System( Equation[], t; systems = [nsys], name = :a) end From 36d0c766bce83d9efb529ca1776d42455675f290 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 22 Apr 2025 18:47:40 +0530 Subject: [PATCH 1690/2176] test: replace `DiscreteSystem` with `System` --- test/components.jl | 4 ++-- test/discrete_system.jl | 34 +++++++++++++++++----------------- test/namespacing.jl | 4 ++-- test/parameter_dependencies.jl | 2 +- 4 files changed, 22 insertions(+), 22 deletions(-) diff --git a/test/components.jl b/test/components.jl index ce3f7ecba2..19f529bd0a 100644 --- a/test/components.jl +++ b/test/components.jl @@ -344,8 +344,8 @@ end end k = ShiftIndex(t) @testset "DiscreteSystem" begin - @named inner = DiscreteSystem([x(k) ~ x(k - 1) + x(k - 2)], t, [x], []) - @named outer = DiscreteSystem([y(k) ~ y(k - 1) + y(k - 2)], t, [x, y], + @named inner = System([x(k) ~ x(k - 1) + x(k - 2)], t, [x], []) + @named outer = System([y(k) ~ y(k - 1) + y(k - 2)], t, [x, y], []; systems = [inner], metadata = "test") @test ModelingToolkit.get_metadata(outer) == "test" sys = complete(outer) diff --git a/test/discrete_system.jl b/test/discrete_system.jl index 43ee771b2b..0c5412977f 100644 --- a/test/discrete_system.jl +++ b/test/discrete_system.jl @@ -10,7 +10,7 @@ using ModelingToolkit: get_metadata, MTKParameters # Make sure positive shifts error @variables x(t) k = ShiftIndex(t) -@test_throws ErrorException @mtkbuild sys = DiscreteSystem([x(k + 1) ~ x + x(k - 1)], t) +@test_throws ErrorException @mtkbuild sys = System([x(k + 1) ~ x + x(k - 1)], t) @inline function rate_to_proportion(r, t) 1 - exp(-r * t) @@ -30,7 +30,7 @@ eqs = [S ~ S(k - 1) - infection * h, R ~ R(k - 1) + recovery] # System -@named sys = DiscreteSystem(eqs, t, [S, I, R], [c, nsteps, δt, β, γ]) +@named sys = System(eqs, t, [S, I, R], [c, nsteps, δt, β, γ]) syss = structural_simplify(sys) @test syss == syss @@ -72,7 +72,7 @@ eqs2 = [S ~ S(k - 1) - infection2, R ~ R(k - 1) + recovery2, R2 ~ R] -@mtkbuild sys = DiscreteSystem( +@mtkbuild sys = System( eqs2, t, [S, I, R, R2], [c, nsteps, δt, β, γ]; controls = [β, γ], tspan) @test ModelingToolkit.defaults(sys) != Dict() @@ -127,7 +127,7 @@ sol_map2 = solve(prob_map, FunctionMap()); # ] # # System -# @named sys = DiscreteSystem(eqs, t, [x(t), x(t - 1.5), x(t - 3), y(t), y(t - 2), z], []) +# @named sys = System(eqs, t, [x(t), x(t - 1.5), x(t - 3), y(t), y(t - 2), z], []) # eqs2, max_delay = ModelingToolkit.linearize_eqs(sys; return_max_delay = true) @@ -143,7 +143,7 @@ sol_map2 = solve(prob_map, FunctionMap()); # observed variable handling @variables x(t) RHS(t) @parameters τ -@named fol = DiscreteSystem( +@named fol = System( [x ~ (1 - x(k - 1)) / τ], t, [x, RHS], [τ]; observed = [RHS ~ (1 - x) / τ * h]) @test isequal(RHS, @nonamespace fol.RHS) RHS2 = RHS @@ -198,7 +198,7 @@ RHS2 = RHS # Δ(us[i]) ~ dummy_identity(buffer[i], us[i]) # end -# @mtkbuild sys = DiscreteSystem(eqs, t, us, ps; defaults = defs, preface = preface) +# @mtkbuild sys = System(eqs, t, us, ps; defaults = defs, preface = preface) # prob = DiscreteProblem(sys, [], (0.0, 1.0)) # sol = solve(prob, FunctionMap(); dt = dt) # @test c[1] + 1 == length(sol) @@ -206,14 +206,14 @@ RHS2 = RHS @variables x(t) y(t) testdict = Dict([:test => 1]) -@named sys = DiscreteSystem([x(k + 1) ~ 1.0], t, [x], []; metadata = testdict) +@named sys = System([x(k + 1) ~ 1.0], t, [x], []; metadata = testdict) @test get_metadata(sys) == testdict @variables x(t) y(t) u(t) eqs = [u ~ 1 x ~ x(k - 1) + u y ~ x + u] -@mtkbuild de = DiscreteSystem(eqs, t) +@mtkbuild de = System(eqs, t) prob = DiscreteProblem(de, [x(k - 1) => 0.0], (0, 10)) sol = solve(prob, FunctionMap()) @@ -231,7 +231,7 @@ function SampledData(; name, buffer) @variables output(t) time(t) eqs = [time ~ time(k - 1) + 1 output ~ getdata(buffer, time)] - return DiscreteSystem(eqs, t; name) + return System(eqs, t; name) end function System(; name, buffer) @named y_sys = SampledData(; buffer = buffer) @@ -245,7 +245,7 @@ function System(; name, buffer) # y[t] = 0.5 * y[t - 1] + 0.5 * y[t + 1] + y_shk[t] y(k - 1) ~ α * y(k - 2) + (β * y(k) + y_shk(k - 1))] - DiscreteSystem(eqs, t, vars, pars; systems = [y_sys], name = name) + System(eqs, t, vars, pars; systems = [y_sys], name = name) end @test_nowarn @mtkbuild sys = System(; buffer = ones(10)) @@ -253,12 +253,12 @@ end # Ensure discrete systems with algebraic equations throw @variables x(t) y(t) k = ShiftIndex(t) -@named sys = DiscreteSystem([x ~ x^2 + y^2, y ~ x(k - 1) + y(k - 1)], t) +@named sys = System([x ~ x^2 + y^2, y ~ x(k - 1) + y(k - 1)], t) @testset "Passing `nothing` to `u0`" begin @variables x(t) = 1 k = ShiftIndex() - @mtkbuild sys = DiscreteSystem([x(k) ~ x(k - 1) + 1], t) + @mtkbuild sys = System([x(k) ~ x(k - 1) + 1], t) prob = @test_nowarn DiscreteProblem(sys, nothing, (0.0, 1.0)) @test_nowarn solve(prob, FunctionMap()) end @@ -266,7 +266,7 @@ end @testset "Initialization" begin # test that default values apply to the entire history @variables x(t) = 1.0 - @mtkbuild de = DiscreteSystem([x ~ x(k - 1) + x(k - 2)], t) + @mtkbuild de = System([x ~ x(k - 1) + x(k - 2)], t) prob = DiscreteProblem(de, [], (0, 10)) @test prob[x] == 2.0 @test prob[x(k - 1)] == 1.0 @@ -289,14 +289,14 @@ end # Test missing initial throws error @variables x(t) - @mtkbuild de = DiscreteSystem([x ~ x(k - 1) + x(k - 2) * x(k - 3)], t) + @mtkbuild de = System([x ~ x(k - 1) + x(k - 2) * x(k - 3)], t) @test_throws ErrorException prob=DiscreteProblem(de, [x(k - 3) => 2.0], (0, 10)) @test_throws ErrorException prob=DiscreteProblem( de, [x(k - 3) => 2.0, x(k - 1) => 3.0], (0, 10)) # Test non-assigned initials are given default value @variables x(t) = 2.0 - @mtkbuild de = DiscreteSystem([x ~ x(k - 1) + x(k - 2) * x(k - 3)], t) + @mtkbuild de = System([x ~ x(k - 1) + x(k - 2) * x(k - 3)], t) prob = DiscreteProblem(de, [x(k - 3) => 12.0], (0, 10)) @test prob[x] == 26.0 @test prob[x(k - 1)] == 2.0 @@ -306,7 +306,7 @@ end @variables xₜ₋₂(t) zₜ₋₁(t) z(t) eqs = [x ~ x(k - 1) + z(k - 2), z ~ x(k - 2) * x(k - 3) - z(k - 1)^2] - @mtkbuild de = DiscreteSystem(eqs, t) + @mtkbuild de = System(eqs, t) u0 = [x(k - 1) => 3, xₜ₋₂(k - 1) => 4, x(k - 2) => 1, @@ -333,7 +333,7 @@ end y[1](k) ~ y[1](k - 1) + y[1](k - 2), y[2](k) ~ y[2](k - 1) + y[2](k - 2) ] - @mtkbuild sys = DiscreteSystem(eqs, t) + @mtkbuild sys = System(eqs, t) prob = DiscreteProblem(sys, [x(k - 1) => ones(2), x(k - 2) => zeros(2), y[1](k - 1) => 1.0, y[1](k - 2) => 0.0, y[2](k - 1) => 1.0, y[2](k - 2) => 0.0], diff --git a/test/namespacing.jl b/test/namespacing.jl index 7ae4702304..50cbd7e3a3 100644 --- a/test/namespacing.jl +++ b/test/namespacing.jl @@ -57,7 +57,7 @@ end @variables x(t) @parameters p k = ShiftIndex(t) - sys = DiscreteSystem([x(k) ~ p * x(k - 1)], t; name = :inner) + sys = System([x(k) ~ p * x(k - 1)], t; name = :inner) @test !iscomplete(sys) @test does_namespacing(sys) @@ -76,7 +76,7 @@ end @test isequal(p, nsys.p) @test !isequal(p, sys.p) - @test_throws ["namespacing", "inner"] DiscreteSystem( + @test_throws ["namespacing", "inner"] System( Equation[], t; systems = [nsys], name = :a) end diff --git a/test/parameter_dependencies.jl b/test/parameter_dependencies.jl index eb42098fd3..52a064d0be 100644 --- a/test/parameter_dependencies.jl +++ b/test/parameter_dependencies.jl @@ -386,7 +386,7 @@ end @named sys = System([x * y^2 ~ y + p2]; parameter_dependencies = [p2 ~ 2p1]) @test is_parameter(sys, p1) k = ShiftIndex(t) - @named sys = DiscreteSystem( + @named sys = System( [x(k - 1) ~ x(k) + y(k) + p2], t; parameter_dependencies = [p2 ~ 2p1]) @test is_parameter(sys, p1) end From dfd16e50d0975d65cfcd03de8302a3350a2b3da2 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 25 Apr 2025 15:48:28 +0530 Subject: [PATCH 1691/2176] fix: ensure equations are `Vector{Equation}` in `generate_initializesystem` --- src/systems/nonlinear/initializesystem.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/systems/nonlinear/initializesystem.jl b/src/systems/nonlinear/initializesystem.jl index 1e7e4dc85c..646ce81112 100644 --- a/src/systems/nonlinear/initializesystem.jl +++ b/src/systems/nonlinear/initializesystem.jl @@ -247,7 +247,7 @@ function generate_initializesystem_timeindependent(sys::AbstractSystem; # so add scalarized versions as well scalarize_varmap!(paramsubs) - eqs_ics = Symbolics.substitute.(eqs_ics, (paramsubs,)) + eqs_ics = Vector{Equation}(Symbolics.substitute.(eqs_ics, (paramsubs,))) for k in keys(defs) defs[k] = substitute(defs[k], paramsubs) end From 9b35a001557d2ce2f3c31688dd30b0e35ac97cb7 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 25 Apr 2025 15:48:51 +0530 Subject: [PATCH 1692/2176] test: fix usage of array equations in test --- test/dq_units.jl | 2 +- test/units.jl | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/test/dq_units.jl b/test/dq_units.jl index c6cada3363..4d8c245e06 100644 --- a/test/dq_units.jl +++ b/test/dq_units.jl @@ -72,7 +72,7 @@ good_eqs = [connect(op, op2)] # Array variables @variables x(t)[1:3] [unit = u"m"] @parameters v[1:3]=[1, 2, 3] [unit = u"m/s"] -eqs = D.(x) .~ v +eqs = [D(x) ~ v] System(eqs, t, name = :sys) # Nonlinear system diff --git a/test/units.jl b/test/units.jl index 2bd67f2745..b7d141f347 100644 --- a/test/units.jl +++ b/test/units.jl @@ -99,7 +99,7 @@ bad_length_eqs = [connect(op, lp)] @parameters v[1:3]=[1, 2, 3] [unit = u"m/s"] @variables x(t)[1:3] [unit = u"m"] D = Differential(t) -eqs = D.(x) .~ v +eqs = [D(x) ~ v] System(eqs, t, name = :sys) # Nonlinear system From 56741c94994a8646a51ea8a17d637beadd158a36 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 25 Apr 2025 15:49:13 +0530 Subject: [PATCH 1693/2176] test: ensure equations passed to system are `Vector{Equation}` --- test/variable_scope.jl | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/variable_scope.jl b/test/variable_scope.jl index 6d7d20d948..2ecd62ec1f 100644 --- a/test/variable_scope.jl +++ b/test/variable_scope.jl @@ -24,9 +24,9 @@ eqs = [0 ~ a 0 ~ d] @named sub4 = System(eqs, [a, b, c, d], []) @named sub3 = System(eqs, [a, b, c, d], []) -@named sub2 = System([], [], [], systems = [sub3, sub4]) -@named sub1 = System([], [], [], systems = [sub2]) -@named sys = System([], [], [], systems = [sub1]) +@named sub2 = System(Equation[], [], [], systems = [sub3, sub4]) +@named sub1 = System(Equation[], [], [], systems = [sub2]) +@named sys = System(Equation[], [], [], systems = [sub1]) names = ModelingToolkit.getname.(unknowns(sys)) @test :d in names From 5ea5f81d9cecfec78aa693b2e69c75cb3b388375 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 25 Apr 2025 15:49:31 +0530 Subject: [PATCH 1694/2176] refactor: remove `process_equations` --- src/utils.jl | 56 ---------------------------------------------------- 1 file changed, 56 deletions(-) diff --git a/src/utils.jl b/src/utils.jl index a12b251384..fe8fb66c53 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -1205,62 +1205,6 @@ function guesses_from_metadata!(guesses, vars) end end -""" - $(TYPEDSIGNATURES) - -Find all the unknowns and parameters from the equations of a System. Return re-ordered -equations, differential variables, all variables, and parameters. -""" -function process_equations(eqs, iv) - if eltype(eqs) <: AbstractVector - eqs = reduce(vcat, eqs) - end - eqs = collect(eqs) - - diffvars = OrderedSet() - allunknowns = OrderedSet() - ps = OrderedSet() - - # NOTE: this assumes that the order of algebraic equations doesn't matter - # reorder equations such that it is in the form of `diffeq, algeeq` - diffeq = Equation[] - algeeq = Equation[] - # initial loop for finding `iv` - if iv === nothing - for eq in eqs - if !(eq.lhs isa Number) # assume eq.lhs is either Differential or Number - iv = iv_from_nested_derivative(eq.lhs) - break - end - end - end - iv = value(iv) - iv === nothing && throw(ArgumentError("Please pass in independent variables.")) - - compressed_eqs = Equation[] # equations that need to be expanded later, like `connect(a, b)` - for eq in eqs - eq.lhs isa Union{Symbolic, Number} || (push!(compressed_eqs, eq); continue) - collect_vars!(allunknowns, ps, eq, iv) - if isdiffeq(eq) - diffvar, _ = var_from_nested_derivative(eq.lhs) - if check_scope_depth(getmetadata(diffvar, SymScope, LocalScope()), 0) - isequal(iv, iv_from_nested_derivative(eq.lhs)) || - throw(ArgumentError("A system of differential equations can only have one independent variable.")) - diffvar in diffvars && - throw(ArgumentError("The differential variable $diffvar is not unique in the system of equations.")) - !has_diffvar_type(diffvar) && - throw(ArgumentError("Differential variable $diffvar has type $(symtype(diffvar)). Differential variables should be of a continuous, non-concrete number type: Real, Complex, AbstractFloat, or Number.")) - push!(diffvars, diffvar) - end - push!(diffeq, eq) - else - push!(algeeq, eq) - end - end - - diffvars, allunknowns, ps, Equation[diffeq; algeeq; compressed_eqs] -end - function has_diffvar_type(diffvar) st = symtype(diffvar) st === Real || eltype(st) === Real || st === Complex || eltype(st) === Complex || From 8a277f46a93d8c2f80991c6ab79c70cbfc738bad Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 25 Apr 2025 15:49:56 +0530 Subject: [PATCH 1695/2176] docs: document `collect_var!` --- src/utils.jl | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/utils.jl b/src/utils.jl index fe8fb66c53..15c90c149d 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -604,6 +604,13 @@ function collect_vars!(unknowns, parameters, p::Pair, iv; depth = 0, op = Differ return nothing end +""" + $(TYPEDSIGNATURES) + +Identify whether `var` belongs to the current system using `depth` and scoping information. +Add `var` to `unknowns` or `parameters` appropriately, and search through any expressions +in known metadata of `var` using `collect_vars!`. +""" function collect_var!(unknowns, parameters, var, iv; depth = 0) isequal(var, iv) && return nothing if Symbolics.iswrapped(var) From 7678ffcb974a42939b90eadeb50aaad118c10d77 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 25 Apr 2025 15:50:34 +0530 Subject: [PATCH 1696/2176] refactor: change default operator in `collect_vars!` to `Symbolics.Operator` --- src/utils.jl | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/utils.jl b/src/utils.jl index 15c90c149d..ad10ac157c 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -566,7 +566,7 @@ function collect_scoped_vars!(unknowns, parameters, sys, iv; depth = 1, op = Dif end end -function collect_vars!(unknowns, parameters, expr, iv; depth = 0, op = Differential) +function collect_vars!(unknowns, parameters, expr, iv; depth = 0, op = Symbolics.Operator) if issym(expr) collect_var!(unknowns, parameters, expr, iv; depth) else @@ -592,13 +592,14 @@ eqtype_supports_collect_vars(eq::Inequality) = true eqtype_supports_collect_vars(eq::Pair) = true function collect_vars!(unknowns, parameters, eq::Union{Equation, Inequality}, iv; - depth = 0, op = Differential) + depth = 0, op = Symbolics.Operator) collect_vars!(unknowns, parameters, eq.lhs, iv; depth, op) collect_vars!(unknowns, parameters, eq.rhs, iv; depth, op) return nothing end -function collect_vars!(unknowns, parameters, p::Pair, iv; depth = 0, op = Differential) +function collect_vars!( + unknowns, parameters, p::Pair, iv; depth = 0, op = Symbolics.Operator) collect_vars!(unknowns, parameters, p[1], iv; depth, op) collect_vars!(unknowns, parameters, p[2], iv; depth, op) return nothing From 1b7766689eb107fe881b3df76f0db8e536f816d8 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 25 Apr 2025 15:50:58 +0530 Subject: [PATCH 1697/2176] feat: add `validate_operator` --- src/discretedomain.jl | 21 +++++++++++++++ src/systems/callbacks.jl | 3 +++ src/utils.jl | 55 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 79 insertions(+) diff --git a/src/discretedomain.jl b/src/discretedomain.jl index 2d5410ee79..1eeec4c014 100644 --- a/src/discretedomain.jl +++ b/src/discretedomain.jl @@ -7,6 +7,8 @@ SymbolicUtils.promote_symtype(::Type{<:SampleTime}, t...) = Real Base.nameof(::SampleTime) = :SampleTime SymbolicUtils.isbinop(::SampleTime) = false +function validate_operator(op::SampleTime, args, iv; context = nothing) end + # Shift """ @@ -72,6 +74,13 @@ Base.hash(D::Shift, u::UInt) = hash(D.steps, hash(D.t, xor(u, 0x055640d6d952f101 Base.:^(D::Shift, n::Integer) = Shift(D.t, D.steps * n) Base.literal_pow(f::typeof(^), D::Shift, ::Val{n}) where {n} = Shift(D.t, D.steps * n) +function validate_operator(op::Shift, args, iv; context = nothing) + isequal(op.t, iv) || throw(OperatorIndepvarMismatchError(op, iv, context)) + op.steps <= 0 || error(""" + Only non-positive shifts are allowed. Found shift of $(op.steps) in $context. + """) +end + hasshift(eq::Equation) = hasshift(eq.lhs) || hasshift(eq.rhs) """ @@ -132,6 +141,13 @@ Base.show(io::IO, D::Sample) = print(io, "Sample(", D.clock, ")") Base.:(==)(D1::Sample, D2::Sample) = isequal(D1.clock, D2.clock) Base.hash(D::Sample, u::UInt) = hash(D.clock, xor(u, 0x055640d6d952f101)) +function validate_operator(op::Sample, args, iv; context = nothing) + arg = unwrap(only(args)) + if !is_variable_floatingpoint(arg) + throw(ContinuousOperatorDiscreteArgumentError(op, arg, context)) + end +end + """ hassample(O) @@ -160,6 +176,11 @@ SymbolicUtils.isbinop(::Hold) = false Hold(x) = Hold()(x) +function validate_operator(op::Hold, args, iv; context = nothing) + # TODO: maybe validate `VariableTimeDomain`? + return nothing +end + """ hashold(O) diff --git a/src/systems/callbacks.jl b/src/systems/callbacks.jl index 891943c6e7..d0bd18a65a 100644 --- a/src/systems/callbacks.jl +++ b/src/systems/callbacks.jl @@ -155,6 +155,9 @@ end haspre(eq::Equation) = haspre(eq.lhs) || haspre(eq.rhs) haspre(O) = recursive_hasoperator(Pre, O) +function validate_operator(op::Pre, args, iv; context = nothing) +end + ############################### ###### Continuous events ###### ############################### diff --git a/src/utils.jl b/src/utils.jl index ad10ac157c..293a3fdd69 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -566,6 +566,61 @@ function collect_scoped_vars!(unknowns, parameters, sys, iv; depth = 1, op = Dif end end +""" + $(TYPEDSIGNATURES) + +Check whether the usage of operator `op` is valid in a system with independent variable +`iv`. If the system is time-independent, `iv` should be `nothing`. Throw an appropriate +error if `op` is invalid. `args` are the arguments to `op`. + +# Keyword arguments + +- `context`: The place where the operator occurs in the system/expression, or any other + relevant information. Useful for providing extra information in the error message. +""" +function validate_operator(op, args, iv; context = nothing) + error("`$validate_operator` is not implemented for operator `$op` in $context.") +end + +function validate_operator(op::Differential, args, iv; context = nothing) + isequal(op.x, iv) || throw(OperatorIndepvarMismatchError(op, iv, context)) + arg = unwrap(only(args)) + if !is_variable_floatingpoint(arg) + throw(ContinuousOperatorDiscreteArgumentError(op, arg, context)) + end +end + +struct ContinuousOperatorDiscreteArgumentError <: Exception + op::Any + arg::Any + context::Any +end + +function Base.showerror(io::IO, err::ContinuousOperatorDiscreteArgumentError) + print(io, """ + Operator $(err.op) expects continuous arguments, with a `symtype` such as `Number`, + `Real`, `Complex` or a subtype of `AbstractFloat`. Found $(err.arg) with a symtype of + $(symtype(err.arg))$(err.context === nothing ? "." : "in $(err.context).") + """) +end + +struct OperatorIndepvarMismatchError <: Exception + op::Any + iv::Any + context::Any +end + +function Base.showerror(io::IO, err::OperatorIndepvarMismatchError) + print(io, """ + Encountered operator `$(err.op)` which has different independent variable than the \ + one used in the system `$(err.iv)`. + """) + if err.context !== nothing + println(io) + print(io, "Context:\n$(err.context)") + end +end + function collect_vars!(unknowns, parameters, expr, iv; depth = 0, op = Symbolics.Operator) if issym(expr) collect_var!(unknowns, parameters, expr, iv; depth) From a2b712aae0e1a6f81c66540e41af2b1d0b60e43b Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 25 Apr 2025 15:51:29 +0530 Subject: [PATCH 1698/2176] refactor: document `collect_vars!` and use `validate_operator` --- src/utils.jl | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/src/utils.jl b/src/utils.jl index 293a3fdd69..6d0140e7e9 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -621,16 +621,30 @@ function Base.showerror(io::IO, err::OperatorIndepvarMismatchError) end end +""" + $(TYPEDSIGNATURES) + +Search through `expr` for all symbolic variables present in it. Populate `dvs` with +unknowns and `ps` with parameters present. `iv` should be the independent variable of the +system or `nothing` for time-independent systems. Expressions where the operator `isa op` +go through `validate_operator`. + +`depth` is a keyword argument which indicates how many levels down `expr` is from the root +of the system hierarchy. This is used to resolve scoping operators. The scope of a variable +can be checked using `check_scope_depth`. + +This function should return `nothing`. +""" function collect_vars!(unknowns, parameters, expr, iv; depth = 0, op = Symbolics.Operator) if issym(expr) - collect_var!(unknowns, parameters, expr, iv; depth) - else - for var in vars(expr; op) - if iscall(var) && operation(var) isa Differential - var, _ = var_from_nested_derivative(var) - end - collect_var!(unknowns, parameters, var, iv; depth) + return collect_var!(unknowns, parameters, expr, iv; depth) + end + for var in vars(expr; op) + while iscall(var) && operation(var) isa op + validate_operator(operation(var), arguments(var), iv; context = expr) + var = arguments(var)[1] end + collect_var!(unknowns, parameters, var, iv; depth) end return nothing end From d5ee2877f94ef696b3b63f2189d0a33a50094208 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 25 Apr 2025 17:22:07 +0530 Subject: [PATCH 1699/2176] feat: add `is_floatingpoint_symtype` --- src/utils.jl | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/utils.jl b/src/utils.jl index 6d0140e7e9..9996635235 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -1113,6 +1113,16 @@ function is_variable_floatingpoint(sym) T = symtype(sym) return T == Real || T <: AbstractFloat || T <: AbstractArray{Real} || T <: AbstractArray{<:AbstractFloat} + +""" + $(TYPEDSIGNATURES) + +Check if `T` is an appropriate symtype for a symbolic variable representing a floating +point number or array of such numbers. +""" +function is_floatingpoint_symtype(T::Type) + return T == Real || T == Number || T <: AbstractFloat || + T <: AbstractArray && is_floatingpoint_symtype(eltype(T)) end """ From f7dfb961c34ebb10c7a0e143bb511a8965058cd5 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 25 Apr 2025 17:22:22 +0530 Subject: [PATCH 1700/2176] refactor: use `is_floatingpoint_symtype` in `is_variable_floatingpoint` --- src/utils.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/utils.jl b/src/utils.jl index 9996635235..804976ae28 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -1111,8 +1111,8 @@ Check if `sym` represents a symbolic floating point number or array of such numb function is_variable_floatingpoint(sym) sym = unwrap(sym) T = symtype(sym) - return T == Real || T <: AbstractFloat || T <: AbstractArray{Real} || - T <: AbstractArray{<:AbstractFloat} + is_floatingpoint_symtype(T) +end """ $(TYPEDSIGNATURES) From b7258df36fd995ac2821a04e4c4099657839c8fa Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 25 Apr 2025 19:15:14 +0530 Subject: [PATCH 1701/2176] test: don't pass dvs/ps to `ODEFunction` --- test/precompile_test/ODEPrecompileTest.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/precompile_test/ODEPrecompileTest.jl b/test/precompile_test/ODEPrecompileTest.jl index 911f45152e..2111f7ba64 100644 --- a/test/precompile_test/ODEPrecompileTest.jl +++ b/test/precompile_test/ODEPrecompileTest.jl @@ -15,7 +15,7 @@ function system(; kwargs...) @named de = System(eqs, t) de = complete(de) - return ODEFunction(de, [x, y, z], [σ, ρ, β]; kwargs...) + return ODEFunction(de; kwargs...) end # Build an ODEFunction as part of the module's precompilation. These cases From 43f32bd11db3316730be75931ca1f89eb4eecca9 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 25 Apr 2025 19:15:38 +0530 Subject: [PATCH 1702/2176] test: fix shadowing of `System` in tests --- test/domain_connectors.jl | 4 ++-- test/state_selection.jl | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/test/domain_connectors.jl b/test/domain_connectors.jl index 03d6ff264a..e35cee8f30 100644 --- a/test/domain_connectors.jl +++ b/test/domain_connectors.jl @@ -123,7 +123,7 @@ function Valve2Port(; p_s_int, p_r_int, p_int, name) System(eqs, t, vars, pars; name, systems) end -function System(; name) +function HydraulicSystem(; name) vars = [] pars = [] systems = @named begin @@ -142,7 +142,7 @@ function System(; name) return System(eqs, t, vars, pars; systems, name) end -@named odesys = System() +@named odesys = HydraulicSystem() esys = ModelingToolkit.expand_connections(odesys) @test length(equations(esys)) == length(unknowns(esys)) diff --git a/test/state_selection.jl b/test/state_selection.jl index b8bec5d7b7..6db8e8c5a0 100644 --- a/test/state_selection.jl +++ b/test/state_selection.jl @@ -100,7 +100,7 @@ let D(v) * rho * L ~ (fluid_port_a.p - fluid_port_b.p - dp_z)] compose(System(eqs, t, sts, ps; name = name), [fluid_port_a, fluid_port_b]) end - function System(; name, L = 10.0) + function HydraulicSystem(; name, L = 10.0) @named compensator = Compensator() @named source = Source() @named substation = Substation() @@ -116,7 +116,7 @@ let compose(System(eqs, t, [], ps; name = name), subs) end - @named system = System(L = 10) + @named system = HydraulicSystem(L = 10) @unpack supply_pipe, return_pipe = system sys = structural_simplify(system) u0 = [ From 1aaa64b8c47e69ca9882ee3801cddf56c4cb808b Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 25 Apr 2025 19:15:56 +0530 Subject: [PATCH 1703/2176] test: pass `u0map` and `tspan` to `ODEProblem` --- test/parameter_dependencies.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/parameter_dependencies.jl b/test/parameter_dependencies.jl index 52a064d0be..fc3960534c 100644 --- a/test/parameter_dependencies.jl +++ b/test/parameter_dependencies.jl @@ -59,7 +59,7 @@ end t; parameter_dependencies = [p2 => 2p1] ) - prob = ODEProblem(complete(sys)) + prob = ODEProblem(complete(sys), [], (0.0, 1.0)) setp1! = setp(prob, p1) get_p1 = getp(prob, p1) get_p2 = getp(prob, p2) @@ -113,7 +113,7 @@ end t; parameter_dependencies = [p2 => 2p1] ) - prob = ODEProblem(complete(sys)) + prob = ODEProblem(complete(sys), [], (0.0, 1.0)) get_dep = getu(prob, 2p1) @test get_dep(prob) == [2.0, 4.0] end From 268947dad1882e572fa3567df9d9470d6a817a96 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 25 Apr 2025 19:16:56 +0530 Subject: [PATCH 1704/2176] test: create `JumpProblem` directly --- test/jumpsystem.jl | 74 +++++++++++++++++----------------- test/parameter_dependencies.jl | 8 ++-- 2 files changed, 41 insertions(+), 41 deletions(-) diff --git a/test/jumpsystem.jl b/test/jumpsystem.jl index c5dbe1c56c..d80ee40666 100644 --- a/test/jumpsystem.jl +++ b/test/jumpsystem.jl @@ -69,8 +69,9 @@ p = (0.1 / 1000, 0.01); tspan = (0.0, 250.0); u₀map = [S => 999, I => 1, R => 0] parammap = [β => 0.1 / 1000, γ => 0.01] -dprob = DiscreteProblem(js2, u₀map, tspan, parammap) -jprob = JumpProblem(js2, dprob, Direct(); save_positions = (false, false), rng) +jprob = JumpProblem(js2, u₀map, tspan, parammap; aggregator = Direct(), + save_positions = (false, false), rng) +@test jprob.prob isa DiscreteProblem Nsims = 30000 function getmean(jprob, Nsims; use_stepper = true) m = 0.0 @@ -83,7 +84,7 @@ end m = getmean(jprob, Nsims) # test auto-alg selection works -jprobb = JumpProblem(js2, dprob; save_positions = (false, false), rng) +jprobb = JumpProblem(js2, u₀map, tspan, parammap; save_positions = (false, false), rng) mb = getmean(jprobb, Nsims; use_stepper = false) @test abs(m - mb) / m < 0.01 @@ -91,13 +92,15 @@ mb = getmean(jprobb, Nsims; use_stepper = false) obs = [S2 ~ 2 * S] @named js2b = JumpSystem([j₁, j₃], t, [S, I, R], [β, γ], observed = obs) js2b = complete(js2b) -dprob = DiscreteProblem(js2b, u₀map, tspan, parammap) -jprob = JumpProblem(js2b, dprob, Direct(); save_positions = (false, false), rng) +jprob = JumpProblem(js2b, u₀map, tspan, parammap; aggregator = Direct(), + save_positions = (false, false), rng) +@test jprob.prob isa DiscreteProblem sol = solve(jprob, SSAStepper(); saveat = tspan[2] / 10) @test all(2 .* sol[S] .== sol[S2]) # test save_positions is working -jprob = JumpProblem(js2, dprob, Direct(); save_positions = (false, false), rng) +jprob = JumpProblem(js2, u₀map, tspan, parammap; aggregator = Direct(), + save_positions = (false, false), rng) sol = solve(jprob, SSAStepper(); saveat = 1.0) @test all((sol.t) .== collect(0.0:tspan[2])) @@ -143,18 +146,20 @@ maj1 = MassActionJump(2 * β / 2, [S => 1, I => 1], [S => -1, I => 1]) maj2 = MassActionJump(γ, [I => 1], [I => -1, R => 1]) @named js3 = JumpSystem([maj1, maj2], t, [S, I, R], [β, γ]) js3 = complete(js3) -dprob = DiscreteProblem(js3, u₀map, tspan, parammap) -jprob = JumpProblem(js3, dprob, Direct(); rng) +jprob = JumpProblem(js3, u₀map, tspan, parammap; aggregator = Direct(), rng) +@test jprob.prob isa DiscreteProblem m3 = getmean(jprob, Nsims) @test abs(m - m3) / m < 0.01 # maj jump test with various dep graphs @named js3b = JumpSystem([maj1, maj2], t, [S, I, R], [β, γ]) js3b = complete(js3b) -jprobb = JumpProblem(js3b, dprob, NRM(); rng) +jprobb = JumpProblem(js3b, u₀map, tspan, parammap; aggregator = NRM(), rng) +@test jprobb.prob isa DiscreteProblem m4 = getmean(jprobb, Nsims) @test abs(m - m4) / m < 0.01 -jprobc = JumpProblem(js3b, dprob, RSSA(); rng) +jprobc = JumpProblem(js3b, u₀map, tspan, parammap; aggregator = RSSA(), rng) +@test jprobc.prob isa DiscreteProblem m4 = getmean(jprobc, Nsims) @test abs(m - m4) / m < 0.01 @@ -163,8 +168,9 @@ maj1 = MassActionJump(2.0, [0 => 1], [S => 1]) maj2 = MassActionJump(γ, [S => 1], [S => -1]) @named js4 = JumpSystem([maj1, maj2], t, [S], [β, γ]) js4 = complete(js4) -dprob = DiscreteProblem(js4, [S => 999], (0, 1000.0), [β => 100.0, γ => 0.01]) -jprob = JumpProblem(js4, dprob, Direct(); rng) +jprob = JumpProblem( + js4, [S => 999], (0, 1000.0), [β => 100.0, γ => 0.01]; aggregator = Direct(), rng) +@test jprob.prob isa DiscreteProblem m4 = getmean(jprob, Nsims) @test abs(m4 - 2.0 / 0.01) * 0.01 / 2.0 < 0.01 @@ -173,8 +179,9 @@ maj1 = MassActionJump(2.0, [0 => 1], [S => 1]) maj2 = MassActionJump(γ, [S => 2], [S => -1]) @named js4 = JumpSystem([maj1, maj2], t, [S], [β, γ]) js4 = complete(js4) -dprob = DiscreteProblem(js4, [S => 999], (0, 1000.0), [β => 100.0, γ => 0.01]) -jprob = JumpProblem(js4, dprob, Direct(); rng) +jprob = JumpProblem( + js4, [S => 999], (0, 1000.0), [β => 100.0, γ => 0.01]; aggregator = Direct(), rng) +@test jprob.prob isa DiscreteProblem sol = solve(jprob, SSAStepper()); # issue #819 @@ -196,8 +203,9 @@ let p = [k1 => 2.0, k2 => 0.0, k3 => 0.5] u₀ = [A => 100, B => 0] tspan = (0.0, 2000.0) - dprob = DiscreteProblem(js5, u₀, tspan, p) - jprob = JumpProblem(js5, dprob, Direct(); save_positions = (false, false), rng) + jprob = JumpProblem( + js5, u₀, tspan, p; aggregator = Direct(), save_positions = (false, false), rng) + @test jprob.prob isa DiscreteProblem @test all(jprob.massaction_jump.scaled_rates .== [1.0, 0.0]) pcondit(u, t, integrator) = t == 1000.0 @@ -260,15 +268,10 @@ u0 = [X => 10] tspan = (0.0, 1.0) ps = [k => 1.0] -dp1 = DiscreteProblem(js1, u0, tspan, ps) -dp2 = DiscreteProblem(js2, u0, tspan) -dp3 = DiscreteProblem(js3, u0, tspan, ps) -dp4 = DiscreteProblem(js4, u0, tspan) - -@test_nowarn jp1 = JumpProblem(js1, dp1, Direct()) -@test_nowarn jp2 = JumpProblem(js2, dp2, Direct()) -@test_nowarn jp3 = JumpProblem(js3, dp3, Direct()) -@test_nowarn jp4 = JumpProblem(js4, dp4, Direct()) +@test_nowarn jp1 = JumpProblem(js1, u0, tspan, ps; aggregator = Direct()) +@test_nowarn jp2 = JumpProblem(js2, u0, tspan; aggregator = Direct()) +@test_nowarn jp3 = JumpProblem(js3, u0, tspan, ps; aggregator = Direct()) +@test_nowarn jp4 = JumpProblem(js4, u0, tspan; aggregator = Direct()) # Ensure `structural_simplify` (and `@mtkbuild`) works on JumpSystem (by doing nothing) # Issue#2558 @@ -293,8 +296,7 @@ let for (N, algtype) in zip(Nv, algtypes) @named jsys = JumpSystem([deepcopy(j1) for _ in 1:N], t, [X], [k]) jsys = complete(jsys) - dprob = DiscreteProblem(jsys, [X => 10], (0.0, 10.0), [k => 1]) - jprob = JumpProblem(jsys, dprob) + jprob = JumpProblem(jsys, [X => 10], (0.0, 10.0), [k => 1]) @test jprob.aggregator isa algtype end end @@ -307,8 +309,9 @@ let @parameters k vrj = VariableRateJump(k * (sin(t) + 1), [A ~ Pre(A) + 1, C ~ Pre(C) + 2]) js = complete(JumpSystem([vrj], t, [A, C], [k]; name = :js, observed = [B ~ C * A])) - oprob = ODEProblem(js, [A => 0, C => 0], (0.0, 10.0), [k => 1.0]) - jprob = JumpProblem(js, oprob, Direct(); rng) + jprob = JumpProblem( + js, [A => 0, C => 0], (0.0, 10.0), [k => 1.0]; aggregtor = Direct(), rng) + @test jprob.prob isa ODEProblem sol = solve(jprob, Tsit5()) # test observed and symbolic indexing work @@ -440,8 +443,7 @@ let k2val = 20.0 p = [k1 => k1val, k2 => k2val] tspan = (0.0, 10.0) - oprob = ODEProblem(jsys, u0, tspan, p) - jprob = JumpProblem(jsys, oprob; rng, save_positions = (false, false)) + jprob = JumpProblem(jsys, u0, tspan, p; rng, save_positions = (false, false)) times = range(0.0, tspan[2], length = 100) Nsims = 4000 @@ -480,8 +482,7 @@ let u0map = [X => p.X₀, Y => p.Y₀] pmap = [α => p.α, β => p.β] tspan = (0.0, 20.0) - oprob = ODEProblem(jsys, u0map, tspan, pmap) - jprob = JumpProblem(jsys, oprob; rng, save_positions = (false, false)) + jprob = JumpProblem(jsys, u0, tspan, pmap; rng, save_positions = (false, false)) times = range(0.0, tspan[2], length = 100) Nsims = 4000 Xv = zeros(length(times)) @@ -519,8 +520,7 @@ let continuous_events = cevents) jsys = complete(jsys) tspan = (0.0, 200.0) - oprob = ODEProblem(jsys, u0map, tspan, pmap) - jprob = JumpProblem(jsys, oprob; rng, save_positions = (false, false)) + jprob = JumpProblem(jsys, u0, tspan, pmap; rng, save_positions = (false, false)) Xsamp = 0.0 Nsims = 4000 for n in 1:Nsims @@ -545,8 +545,8 @@ end # Works. @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()) + jprob = JumpProblem( + js, [X => 15], (0.0, 10.0), [p => 2.0, d => 0.5]; aggregator = Direct()) sol = solve(jprob, SSAStepper()) @test eltype(sol[X]) === Int64 end diff --git a/test/parameter_dependencies.jl b/test/parameter_dependencies.jl index fc3960534c..71ca78a101 100644 --- a/test/parameter_dependencies.jl +++ b/test/parameter_dependencies.jl @@ -303,8 +303,8 @@ end tspan = (0.0, 250.0) u₀map = [S => 999, I => 1, R => 0] parammap = [γ => 0.01] - dprob = DiscreteProblem(js2, u₀map, tspan, parammap) - jprob = JumpProblem(js2, dprob, Direct(), save_positions = (false, false), rng = rng) + jprob = JumpProblem(js2, u₀map, tspan, parammap; aggregator = Direct(), + save_positions = (false, false), rng = rng) @test jprob.ps[γ] == 0.01 @test jprob.ps[β] == 0.0001 @test_nowarn solve(jprob, SSAStepper()) @@ -314,8 +314,8 @@ end discrete_events = [SymbolicDiscreteCallback( [10.0] => [γ ~ 0.02], discrete_parameters = [γ])]) js2 = complete(js2) - dprob = DiscreteProblem(js2, u₀map, tspan, parammap) - jprob = JumpProblem(js2, dprob, Direct(), save_positions = (false, false), rng = rng) + jprob = JumpProblem(js2, u₀map, tspan, parammap; aggregator = Direct(), + save_positions = (false, false), rng = rng) integ = init(jprob, SSAStepper()) @test integ.ps[γ] == 0.01 @test integ.ps[β] == 0.0001 From ddeeee17b5fe6da85c6a6f3051e5c1695912388e Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 25 Apr 2025 19:17:09 +0530 Subject: [PATCH 1705/2176] test: pass `Vector{Equation}` to `System` --- test/structural_transformation/tearing.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/structural_transformation/tearing.jl b/test/structural_transformation/tearing.jl index 5bf88e1dd5..0cbabbfa3b 100644 --- a/test/structural_transformation/tearing.jl +++ b/test/structural_transformation/tearing.jl @@ -183,7 +183,7 @@ end m = 1.0 @named mass = Translational_Mass(m = m) -ms_eqs = [] +ms_eqs = Equation[] @named _ms_model = System(ms_eqs, t) @named ms_model = compose(_ms_model, From 6b5dab8a704dc1d30b4174d3edb286926d7bdfd7 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Sun, 27 Apr 2025 13:38:32 +0530 Subject: [PATCH 1706/2176] refactor: remove `build_torn_function`, `tearing_assignments` --- .../StructuralTransformations.jl | 4 +- src/structural_transformation/codegen.jl | 139 ------------------ .../symbolics_tearing.jl | 13 -- 3 files changed, 2 insertions(+), 154 deletions(-) diff --git a/src/structural_transformation/StructuralTransformations.jl b/src/structural_transformation/StructuralTransformations.jl index 16d3a75464..06d8e440cc 100644 --- a/src/structural_transformation/StructuralTransformations.jl +++ b/src/structural_transformation/StructuralTransformations.jl @@ -57,11 +57,11 @@ using DocStringExtensions export tearing, partial_state_selection, dae_index_lowering, check_consistency export dummy_derivative -export build_torn_function, build_observed_function, ODAEProblem +export build_observed_function, ODAEProblem export sorted_incidence_matrix, pantelides!, pantelides_reassemble, tearing_reassemble, find_solvables!, linear_subsys_adjmat! -export tearing_assignments, tearing_substitution +export tearing_substitution export torn_system_jacobian_sparsity export full_equations export but_ordered_incidence, lowest_order_variable_mask, highest_order_variable_mask diff --git a/src/structural_transformation/codegen.jl b/src/structural_transformation/codegen.jl index d2ca5b8748..144e19aa31 100644 --- a/src/structural_transformation/codegen.jl +++ b/src/structural_transformation/codegen.jl @@ -226,145 +226,6 @@ function gen_nlsolve!(is_not_prepended_assignment, eqs, vars, u0map::AbstractDic nlsolve_expr end -function build_torn_function(sys; - expression = false, - jacobian_sparsity = true, - checkbounds = false, - max_inlining_size = nothing, - kw...) - max_inlining_size = something(max_inlining_size, MAX_INLINE_NLSOLVE_SIZE) - rhss = [] - eqs = equations(sys) - eqs_idxs = Int[] - for (i, eq) in enumerate(eqs) - isdiffeq(eq) || continue - push!(eqs_idxs, i) - push!(rhss, eq.rhs) - end - - state = get_or_construct_tearing_state(sys) - fullvars = state.fullvars - var_eq_matching, var_sccs = algebraic_variables_scc(state) - condensed_graph = MatchedCondensationGraph( - DiCMOBiGraph{true}(complete(state.structure.graph), - complete(var_eq_matching)), - var_sccs) - toporder = topological_sort_by_dfs(condensed_graph) - var_sccs = var_sccs[toporder] - - unknowns_idxs = collect(diffvars_range(state.structure)) - mass_matrix_diag = ones(length(unknowns_idxs)) - - assignments, deps, sol_states = tearing_assignments(sys) - invdeps = map(_ -> BitSet(), deps) - for (i, d) in enumerate(deps) - for a in d - push!(invdeps[a], i) - end - end - var2assignment = Dict{Any, Int}(eq.lhs => i for (i, eq) in enumerate(assignments)) - is_not_prepended_assignment = trues(length(assignments)) - - torn_expr = Assignment[] - - defs = defaults(sys) - nlsolve_scc_idxs = Int[] - - needs_extending = false - @views for (i, scc) in enumerate(var_sccs) - torn_vars_idxs = Int[var for var in scc if var_eq_matching[var] !== unassigned] - torn_eqs_idxs = [var_eq_matching[var] for var in torn_vars_idxs] - isempty(torn_eqs_idxs) && continue - if length(torn_eqs_idxs) <= max_inlining_size - nlsolve_expr = gen_nlsolve!(is_not_prepended_assignment, eqs[torn_eqs_idxs], - fullvars[torn_vars_idxs], defs, assignments, - (deps, invdeps), var2assignment, - checkbounds = checkbounds) - append!(torn_expr, nlsolve_expr) - push!(nlsolve_scc_idxs, i) - else - needs_extending = true - append!(eqs_idxs, torn_eqs_idxs) - append!(rhss, map(x -> x.rhs, eqs[torn_eqs_idxs])) - append!(unknowns_idxs, torn_vars_idxs) - append!(mass_matrix_diag, zeros(length(torn_eqs_idxs))) - end - end - sort!(unknowns_idxs) - - mass_matrix = needs_extending ? Diagonal(mass_matrix_diag) : I - - out = Sym{Any}(gensym("out")) - funbody = SetArray(!checkbounds, - out, - rhss) - - unknown_vars = Any[fullvars[i] for i in unknowns_idxs] - @set! sys.solved_unknowns = unknown_vars - - pre = get_postprocess_fbody(sys) - cpre = get_preprocess_constants(rhss) - pre2 = x -> pre(cpre(x)) - - expr = SymbolicUtils.Code.toexpr( - Func( - [out - DestructuredArgs(unknown_vars, - inbounds = !checkbounds) - DestructuredArgs(parameters(sys), - inbounds = !checkbounds) - independent_variables(sys)], - [], - pre2(Let([torn_expr; - assignments[is_not_prepended_assignment]], - funbody, - false))), - sol_states) - if expression - expr, unknown_vars - else - observedfun = let state = state, - dict = Dict(), - is_solver_unknown_idxs = insorted.(1:length(fullvars), (unknowns_idxs,)), - assignments = assignments, - deps = (deps, invdeps), - sol_states = sol_states, - var2assignment = var2assignment - - function generated_observed(obsvar, args...) - obs = get!(dict, value(obsvar)) do - build_observed_function(state, obsvar, var_eq_matching, var_sccs, - is_solver_unknown_idxs, assignments, deps, - sol_states, var2assignment, - checkbounds = checkbounds) - end - if args === () - let obs = obs - (u, p, t) -> obs(u, p, t) - end - else - obs(args...) - end - end - end - - ODEFunction{true, SciMLBase.AutoSpecialize}( - drop_expr(@RuntimeGeneratedFunction(expr)), - sparsity = jacobian_sparsity ? - torn_system_with_nlsolve_jacobian_sparsity(state, - var_eq_matching, - var_sccs, - nlsolve_scc_idxs, - eqs_idxs, - unknowns_idxs) : - nothing, - observed = observedfun, - mass_matrix = mass_matrix, - sys = sys), - unknown_vars - end -end - """ find_solve_sequence(sccs, vars) diff --git a/src/structural_transformation/symbolics_tearing.jl b/src/structural_transformation/symbolics_tearing.jl index 04c88d1d52..65a9982c06 100644 --- a/src/structural_transformation/symbolics_tearing.jl +++ b/src/structural_transformation/symbolics_tearing.jl @@ -154,19 +154,6 @@ function tearing_substitution(sys::AbstractSystem; kwargs...) @set! sys.schedule = nothing end -function tearing_assignments(sys::AbstractSystem) - if empty_substitutions(sys) - assignments = [] - deps = Int[] - sol_states = Code.LazyState() - else - @unpack subs, deps = get_substitutions(sys) - assignments = [Assignment(eq.lhs, eq.rhs) for eq in subs] - sol_states = Code.NameState(Dict(eq.lhs => Symbol(eq.lhs) for eq in subs)) - end - return assignments, deps, sol_states -end - function solve_equation(eq, var, simplify) rhs = value(symbolic_linear_solve(eq, var; simplify = simplify, check = false)) occursin(var, rhs) && throw(EquationSolveErrors(eq, var, rhs)) From 14d8d40da8d1cbdef16b6a93cd3ef89251748460 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Sun, 27 Apr 2025 13:40:26 +0530 Subject: [PATCH 1707/2176] refactor: remove `get_substitutions`, `has_substitutions` field getters --- src/systems/abstractsystem.jl | 1 - 1 file changed, 1 deletion(-) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index 099d569d2f..d4a57cddc4 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -895,7 +895,6 @@ for prop in [:eqs :initialization_eqs :schedule :tearing_state - :substitutions :metadata :gui_metadata :is_initializesystem From a7f7ae62f99952ca857bc0aa44b113d07b288214 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Sun, 27 Apr 2025 13:43:43 +0530 Subject: [PATCH 1708/2176] refactor: implement `empty_substitutions` and `get_substitutions` using `observed` --- src/utils.jl | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/utils.jl b/src/utils.jl index 804976ae28..2c50f2be47 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -832,12 +832,6 @@ end isarray(x) = x isa AbstractArray || x isa Symbolics.Arr -function empty_substitutions(sys) - has_substitutions(sys) || return true - subs = get_substitutions(sys) - isnothing(subs) || isempty(subs.deps) -end - function get_cmap(sys, exprs = nothing) #Inject substitutions for constants => values buffer = [] @@ -880,6 +874,12 @@ function get_substitutions_and_solved_unknowns(sys, exprs = nothing; no_postproc end end return pre, sol_states +function empty_substitutions(sys) + isempty(observed(sys)) +end + +function get_substitutions(sys) + Dict([eq.lhs => eq.rhs for eq in observed(sys)]) end function mergedefaults(defaults, varmap, vars) From c6a15013eca5554e4137a064a166420ac8c87e79 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Sun, 27 Apr 2025 13:43:51 +0530 Subject: [PATCH 1709/2176] refactor: remove `get_substitutions_and_solved_unknowns` --- src/utils.jl | 23 ----------------------- 1 file changed, 23 deletions(-) diff --git a/src/utils.jl b/src/utils.jl index 2c50f2be47..cf6855999c 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -851,29 +851,6 @@ function get_cmap(sys, exprs = nothing) return cmap, cs end -function get_substitutions_and_solved_unknowns(sys, exprs = nothing; no_postprocess = false) - cmap, cs = get_cmap(sys, exprs) - if empty_substitutions(sys) && isempty(cs) - sol_states = Code.LazyState() - pre = no_postprocess ? (ex -> ex) : get_postprocess_fbody(sys) - else # Have to do some work - if !empty_substitutions(sys) - @unpack subs = get_substitutions(sys) - else - subs = [] - end - subs = [cmap; subs] # The constants need to go first - sol_states = Code.NameState(Dict(eq.lhs => Symbol(eq.lhs) for eq in subs)) - if no_postprocess - pre = ex -> Let(Assignment[Assignment(eq.lhs, eq.rhs) for eq in subs], ex, - false) - else - process = get_postprocess_fbody(sys) - pre = ex -> Let(Assignment[Assignment(eq.lhs, eq.rhs) for eq in subs], - process(ex), false) - end - end - return pre, sol_states function empty_substitutions(sys) isempty(observed(sys)) end From 26b3304f0187b3891a7b42bff932ab648e45638b Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Sun, 27 Apr 2025 13:39:06 +0530 Subject: [PATCH 1710/2176] refactor: update `tearing_substitute_expr`, `full_equations` to use `observed` --- .../symbolics_tearing.jl | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/src/structural_transformation/symbolics_tearing.jl b/src/structural_transformation/symbolics_tearing.jl index 65a9982c06..43c3e78e32 100644 --- a/src/structural_transformation/symbolics_tearing.jl +++ b/src/structural_transformation/symbolics_tearing.jl @@ -107,9 +107,7 @@ end function tearing_substitute_expr(sys::AbstractSystem, expr; simplify = false) empty_substitutions(sys) && return expr substitutions = get_substitutions(sys) - @unpack subs = substitutions - solved = Dict(eq.lhs => eq.rhs for eq in subs) - return tearing_sub(expr, solved, simplify) + return tearing_sub(expr, substitutions, simplify) end """ @@ -121,20 +119,17 @@ These equations matches generated numerical code. See also [`equations`](@ref) and [`ModelingToolkit.get_eqs`](@ref). """ function full_equations(sys::AbstractSystem; simplify = false) - empty_substitutions(sys) && return equations(sys) - substitutions = get_substitutions(sys) - substitutions.subed_eqs === nothing || return substitutions.subed_eqs - @unpack subs = substitutions - solved = Dict(eq.lhs => eq.rhs for eq in subs) + isempty(observed(sys)) && return equations(sys) + subs = Dict([eq.lhs => eq.rhs for eq in observed(sys)]) neweqs = map(equations(sys)) do eq if iscall(eq.lhs) && operation(eq.lhs) isa Union{Shift, Differential} - return tearing_sub(eq.lhs, solved, simplify) ~ tearing_sub(eq.rhs, solved, + return tearing_sub(eq.lhs, subs, simplify) ~ tearing_sub(eq.rhs, subs, simplify) else if !(eq.lhs isa Number && eq.lhs == 0) eq = 0 ~ eq.rhs - eq.lhs end - rhs = tearing_sub(eq.rhs, solved, simplify) + rhs = tearing_sub(eq.rhs, subs, simplify) if rhs isa Symbolic return 0 ~ rhs else # a number @@ -143,7 +138,6 @@ function full_equations(sys::AbstractSystem; simplify = false) end eq end - substitutions.subed_eqs = neweqs return neweqs end From b07c935d748a18c3deeb6c5d51a578a85bcbecfe Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Sun, 27 Apr 2025 13:39:43 +0530 Subject: [PATCH 1711/2176] refactor: do not use `get_substitutions` in `get_cmap` --- src/utils.jl | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/utils.jl b/src/utils.jl index cf6855999c..b3df0b957f 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -840,9 +840,6 @@ function get_cmap(sys, exprs = nothing) has_op(sys) && push!(buffer, get_op(sys)) has_constraints(sys) && append!(buffer, get_constraints(sys)) cs = collect_constants(buffer) #ctrls? what else? - if !empty_substitutions(sys) - cs = [cs; collect_constants(get_substitutions(sys).subs)] - end if exprs !== nothing cs = [cs; collect_constants(exprs)] end From a6f889a6be187f028ee603faae9fbeb26ee43b76 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Sun, 27 Apr 2025 13:41:41 +0530 Subject: [PATCH 1712/2176] test: fix mass matrix tests --- test/mass_matrix.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/mass_matrix.jl b/test/mass_matrix.jl index f181bdfbde..2e828cda64 100644 --- a/test/mass_matrix.jl +++ b/test/mass_matrix.jl @@ -10,7 +10,7 @@ eqs = [D(y[1]) ~ -k[1] * y[1] + k[3] * y[2] * y[3], @named sys = System(eqs, t, collect(y), [k]) sys = complete(sys) -@test_throws ArgumentError System(eqs, y[1]) +@test_throws ModelingToolkit.OperatorIndepvarMismatchError System(eqs, y[1]) M = calculate_massmatrix(sys) @test M isa Diagonal @test M == [1 0 0 From b7d48902c4d5984db1e6e235e6b660f56f1d682e Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Sun, 27 Apr 2025 18:23:44 +0530 Subject: [PATCH 1713/2176] test: fix odesystem tests --- test/odesystem.jl | 72 ++++++++++++++++++++++------------------------- 1 file changed, 33 insertions(+), 39 deletions(-) diff --git a/test/odesystem.jl b/test/odesystem.jl index 75914e40a7..7f82807459 100644 --- a/test/odesystem.jl +++ b/test/odesystem.jl @@ -38,8 +38,6 @@ ssort(eqs) = sort(eqs, by = string) @test eval(toexpr(de)) == de @test hash(deepcopy(de)) == hash(de) -generate_function(de) - function test_diffeq_inference(name, sys, iv, dvs, ps) @testset "System construction: $name" begin @test isequal(independent_variables(sys)[1], value(iv)) @@ -50,13 +48,12 @@ function test_diffeq_inference(name, sys, iv, dvs, ps) end test_diffeq_inference("standard", de, t, [x, y, z], [ρ, σ, β]) -generate_function(de, [x, y, z], [σ, ρ, β]) jac_expr = generate_jacobian(de) jac = calculate_jacobian(de) jacfun = eval(jac_expr[2]) de = complete(de) -f = ODEFunction(de, [x, y, z], [σ, ρ, β], tgrad = true, jac = true) +f = ODEFunction(de, tgrad = true, jac = true) # system @test f.sys === de @@ -87,7 +84,7 @@ f.jac(J, u, p, t) @test J == f.jac(u, p, t) #check iip_config -f = ODEFunction(de, [x, y, z], [σ, ρ, β], iip_config = (false, true)) +f = ODEFunction(de; iip_config = (false, true)) du = zeros(3) u = collect(1:3) p = ModelingToolkit.MTKParameters(de, [σ, ρ, β] .=> 4.0:6.0) @@ -143,7 +140,7 @@ eqs = [D(x) ~ σ(t - 1) * (y - x), D(z) ~ x * y - β * z * κ] @named de = System(eqs, t) test_diffeq_inference("single internal iv-varying", de, t, (x, y, z), (σ, ρ, β)) -f = generate_function(de, [x, y, z], [σ, ρ, β], expression = Val{false})[2] +f = generate_rhs(de, [x, y, z], [σ, ρ, β], expression = Val{false}) du = [0.0, 0.0, 0.0] f(du, [1.0, 2.0, 3.0], [x -> x + 7, 2, 3], 5.0) @test du ≈ [11, -3, -7] @@ -151,37 +148,36 @@ f(du, [1.0, 2.0, 3.0], [x -> x + 7, 2, 3], 5.0) eqs = [D(x) ~ x + 10σ(t - 1) + 100σ(t - 2) + 1000σ(t^2)] @named de = System(eqs, t) test_diffeq_inference("many internal iv-varying", de, t, (x,), (σ,)) -f = generate_function(de, [x], [σ], expression = Val{false})[2] +f = generate_rhs(de, [x], [σ], expression = Val{false}) du = [0.0] f(du, [1.0], [t -> t + 2], 5.0) @test du ≈ [27561] -# Conversion to first-order ODEs #17 -D3 = D^3 -D2 = D^2 -@variables u(t) uˍtt(t) uˍt(t) xˍt(t) -eqs = [D3(u) ~ 2(D2(u)) + D(u) + D(x) + 1 - D2(x) ~ D(x) + 2] -@named de = System(eqs, t) -de1 = ode_order_lowering(de) -lowered_eqs = [D(uˍtt) ~ 2uˍtt + uˍt + xˍt + 1 - D(xˍt) ~ xˍt + 2 - D(uˍt) ~ uˍtt - D(u) ~ uˍt - D(x) ~ xˍt] - -#@test de1 == System(lowered_eqs) - -# issue #219 -@test all(isequal.( - [ModelingToolkit.var_from_nested_derivative(eq.lhs)[1] - for eq in equations(de1)], - unknowns(@named lowered = System(lowered_eqs, t)))) - -test_diffeq_inference("first-order transform", de1, t, [uˍtt, xˍt, uˍt, u, x], []) -du = zeros(5) -ODEFunction(complete(de1), [uˍtt, xˍt, uˍt, u, x], [])(du, ones(5), nothing, 0.1) -@test du == [5.0, 3.0, 1.0, 1.0, 1.0] +@testset "Issue#17: Conversion to first order ODEs" begin + D3 = D^3 + D2 = D^2 + @variables u(t) uˍtt(t) uˍt(t) xˍt(t) + eqs = [D3(u) ~ 2(D2(u)) + D(u) + D(x) + 1 + D2(x) ~ D(x) + 2] + @named de = System(eqs, t) + de1 = ode_order_lowering(de) + + @testset "Issue#219: Ordering of equations in `ode_order_lowering`" begin + lowered_eqs = [D(uˍtt) ~ 2uˍtt + uˍt + xˍt + 1 + D(xˍt) ~ xˍt + 2 + D(uˍt) ~ uˍtt + D(u) ~ uˍt + D(x) ~ xˍt] + @test isequal( + [ModelingToolkit.var_from_nested_derivative(eq.lhs)[1] for eq in equations(de1)], + unknowns(@named lowered = System(lowered_eqs, t))) + end + + test_diffeq_inference("first-order transform", de1, t, [uˍtt, xˍt, uˍt, u, x], []) + du = zeros(5) + ODEFunction(complete(de1))(du, ones(5), nothing, 0.1) + @test du == [5.0, 3.0, 1.0, 1.0, 1.0] +end # Internal calculations @parameters σ @@ -190,12 +186,11 @@ eqs = [D(x) ~ σ * a, D(y) ~ x * (ρ - z) - y, D(z) ~ x * y - β * z * κ] @named de = System(eqs, t) -generate_function(de, [x, y, z], [σ, ρ, β]) jac = calculate_jacobian(de) @test ModelingToolkit.jacobian_sparsity(de).colptr == sparse(jac).colptr @test ModelingToolkit.jacobian_sparsity(de).rowval == sparse(jac).rowval -f = ODEFunction(complete(de), [x, y, z], [σ, ρ, β]) +f = ODEFunction(complete(de)) @parameters A B C _x = y / C @@ -204,11 +199,10 @@ eqs = [D(x) ~ -A * x, @named de = System(eqs, t) @test begin local f, du - f = generate_function(de, [x, y], [A, B, C], expression = Val{false})[2] + f = generate_rhs(de, [x, y], [A, B, C], expression = Val{false}) du = [0.0, 0.0] f(du, [1.0, 2.0], [1, 2, 3], 0.0) du ≈ [-1, -1 / 3] - f = generate_function(de, [x, y], [A, B, C], expression = Val{false})[1] du ≈ f([1.0, 2.0], [1, 2, 3], 0.0) end @@ -1290,11 +1284,11 @@ end [D(u) ~ (sum(u) + sum(x) + sum(p) + sum(o)) * x, o ~ prod(u) * x], t, [u..., x..., o...], [p...]) sys1 = structural_simplify(sys, inputs = [x...], outputs = []) - fn1, = ModelingToolkit.generate_function(sys1; expression = Val{false}) + fn1, = ModelingToolkit.generate_rhs(sys1; expression = Val{false}) ps = MTKParameters(sys1, [x => 2ones(2), p => 3ones(2, 2)]) @test_nowarn fn1(ones(4), ps, 4.0) sys2 = structural_simplify(sys, inputs = [x...], outputs = [], split = false) - fn2, = ModelingToolkit.generate_function(sys2; expression = Val{false}) + fn2, = ModelingToolkit.generate_rhs(sys2; expression = Val{false}) ps = zeros(8) setp(sys2, x)(ps, 2ones(2)) setp(sys2, p)(ps, 2ones(2, 2)) From 9ec0721efb5115f0da6d75b3da49a6d915e7769b Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Sun, 27 Apr 2025 13:44:47 +0530 Subject: [PATCH 1714/2176] refactor: rename `generate_function` to `generate_rhs` --- src/ModelingToolkit.jl | 2 +- src/systems/abstractsystem.jl | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ModelingToolkit.jl b/src/ModelingToolkit.jl index 23741dfe43..4ca60e4e63 100644 --- a/src/ModelingToolkit.jl +++ b/src/ModelingToolkit.jl @@ -311,7 +311,7 @@ export structural_simplify, expand_connections, linearize, linearization_functio export solve export Pre -export calculate_jacobian, generate_jacobian, generate_function, generate_custom_function, +export calculate_jacobian, generate_jacobian, generate_rhs, generate_custom_function, generate_W export calculate_control_jacobian, generate_control_jacobian export calculate_tgrad, generate_tgrad diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index d4a57cddc4..feb98b2926 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -132,7 +132,7 @@ generate_function(sys::AbstractSystem, dvs = unknowns(sys), ps = parameters(sys) Generate a function to evaluate the system's equations. """ -function generate_function end +function generate_rhs end """ ```julia From 23ac3d8d651e151b7257ebc7ef1e06a494125431 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 29 Apr 2025 15:23:56 +0530 Subject: [PATCH 1715/2176] fix: fix type-piracy of `Symbolics.rename` --- src/systems/abstractsystem.jl | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index feb98b2926..23b4859484 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -982,7 +982,7 @@ end end end -rename(x, name) = @set x.name = name +Symbolics.rename(x::AbstractSystem, name) = @set x.name = name function Base.propertynames(sys::AbstractSystem; private = false) if private @@ -2305,6 +2305,10 @@ function _named_idxs(name::Symbol, idxs, call; extra_args = "") end, $idxs)) end +function setname(x, name) + @set x.name = name +end + function single_named_expr(expr) name, call = split_assign(expr) if Meta.isexpr(name, :ref) @@ -2313,7 +2317,7 @@ function single_named_expr(expr) var = gensym(name) ex = quote $var = $(_named(name, call)) - $name = map(i -> $rename($var, Symbol($(Meta.quot(name)), :_, i)), $idxs) + $name = map(i -> $setname($var, Symbol($(Meta.quot(name)), :_, i)), $idxs) end ex else From 9b1617f10c629362ff15ac329051bdc5f61ad342 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 29 Apr 2025 15:24:28 +0530 Subject: [PATCH 1716/2176] refactor: remove `systems/diffeqs/modelingtoolkitize.jl` --- src/ModelingToolkit.jl | 1 - 1 file changed, 1 deletion(-) diff --git a/src/ModelingToolkit.jl b/src/ModelingToolkit.jl index 4ca60e4e63..a08aeca013 100644 --- a/src/ModelingToolkit.jl +++ b/src/ModelingToolkit.jl @@ -187,7 +187,6 @@ include("systems/nonlinear/homotopy_continuation.jl") include("systems/nonlinear/modelingtoolkitize.jl") include("systems/nonlinear/initializesystem.jl") include("systems/diffeqs/first_order_transform.jl") -include("systems/diffeqs/modelingtoolkitize.jl") include("systems/diffeqs/basic_transformations.jl") include("systems/pde/pdesystem.jl") From c6d20eeacba7b641335e30e7186f17a64256da4a Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 29 Apr 2025 15:24:56 +0530 Subject: [PATCH 1717/2176] feat: add `modelingtoolkitize` for `ODEProblem` feat: support passing `t::Nothing` to `trace_rhs` feat: support time-independent variable declaration in mtkize utils fix: handle bounds in `construct_vars` when `prob.f.sys isa System` --- src/ModelingToolkit.jl | 4 + src/modelingtoolkitize/common.jl | 393 +++++++++++++++++++++++++++ src/modelingtoolkitize/odeproblem.jl | 59 ++++ 3 files changed, 456 insertions(+) create mode 100644 src/modelingtoolkitize/common.jl create mode 100644 src/modelingtoolkitize/odeproblem.jl diff --git a/src/ModelingToolkit.jl b/src/ModelingToolkit.jl index a08aeca013..f4ffb8459b 100644 --- a/src/ModelingToolkit.jl +++ b/src/ModelingToolkit.jl @@ -181,6 +181,10 @@ include("problems/jumpproblem.jl") include("problems/initializationproblem.jl") include("problems/sccnonlinearproblem.jl") include("problems/bvproblem.jl") + +include("modelingtoolkitize/common.jl") +include("modelingtoolkitize/odeproblem.jl") + include("systems/optimization/modelingtoolkitize.jl") include("systems/nonlinear/homotopy_continuation.jl") diff --git a/src/modelingtoolkitize/common.jl b/src/modelingtoolkitize/common.jl new file mode 100644 index 0000000000..a3d059a66a --- /dev/null +++ b/src/modelingtoolkitize/common.jl @@ -0,0 +1,393 @@ + +""" + $(TYPEDSIGNATURES) + +Check if the length of variables `vars` matches the number of names for those variables, +given by `names`. `is_unknowns` denotes whether the variable are unknowns or parameters. +""" +function varnames_length_check(vars, names; is_unknowns = false) + length(names) == length(vars) && return + throw(ArgumentError(""" + Number of $(is_unknowns ? "unknowns" : "parameters") ($(length(vars))) \ + does not match number of names ($(length(names))). + """)) +end + +""" + $(TYPEDSIGNATURES) + +Define a subscripted time-dependent variable with name `x` and subscript `i`. Equivalent +to `@variables \$name(..)`. `T` is the desired symtype of the variable when called with +the independent variable. +""" +_defvaridx(x, i; T = Real) = variable(x, i, T = SymbolicUtils.FnType{Tuple, T}) +""" + $(TYPEDSIGNATURES) + +Define a time-dependent variable with name `x`. Equivalent to `@variables \$x(..)`. +`T` is the desired symtype of the variable when called with the independent variable. +""" +_defvar(x; T = Real) = variable(x, T = SymbolicUtils.FnType{Tuple, T}) + +""" + $(TYPEDSIGNATURES) + +Define an array of symbolic unknowns of the appropriate type and size for `u` with +independent variable `t`. +""" +function define_vars(u, t) + [_defvaridx(:x, i)(t) for i in eachindex(u)] +end + +function define_vars(u, ::Nothing) + [variable(:x, i) for i in eachindex(u)] +end + +""" + $(TYPEDSIGNATURES) + +Return a symbolic state for the given proble `prob.`. `t` is the independent variable. +`u_names` optionally contains the names to use for the created symbolic variables. +""" +function construct_vars(prob, t, u_names = nothing) + if prob.u0 === nothing + return [] + end + # construct `_vars`, AbstractSciMLFunction, AbstractSciMLFunction, a list of MTK variables for `prob.u0`. + if u_names !== nothing + # explicitly provided names + varnames_length_check(state_values(prob), u_names; is_unknowns = true) + if t === nothing + _vars = [variable(name) for name in u_names] + else + _vars = [_defvar(name)(t) for name in u_names] + end + elseif SciMLBase.has_sys(prob.f) + # get names from the system + varnames = getname.(variable_symbols(prob.f.sys)) + varidxs = variable_index.((prob.f.sys,), varnames) + invpermute!(varnames, varidxs) + if t === nothing + _vars = [variable(name) for name in varnames] + else + _vars = [_defvar(name)(t) for name in varnames] + end + if prob.f.sys isa System + for (i, sym) in enumerate(variable_symbols(prob.f.sys)) + if hasbounds(sym) + _vars[i] = Symbolics.setmetadata( + _vars[i], VariableBounds, getbounds(sym)) + end + end + end + else + # auto-generate names + _vars = define_vars(state_values(prob), t) + end + + # Handle different types of arrays + return prob.u0 isa Number ? _vars : ArrayInterface.restructure(prob.u0, _vars) +end + +""" + $(METHODLIST) + +Define symbolic names for each value in parameter object `p`. `t` is the independent +variable of the system. `names` is a collection mapping indexes of `p` to their +names, or `nothing` to automatically generate names. + +The returned value has the same structure as `p`, but symbolic variables instead of +values. +""" +function define_params(p, t, _ = nothing) + throw(ModelingtoolkitizeParametersNotSupportedError(typeof(p))) +end + +function define_params(p::AbstractArray, t, names = nothing) + if names === nothing + [toparam(variable(:α, i)) for i in eachindex(p)] + else + varnames_length_check(p, names) + [toparam(variable(names[i])) for i in eachindex(p)] + end +end + +function define_params(p::Number, t, names = nothing) + if names === nothing + [toparam(variable(:α))] + elseif names isa Union{AbstractArray, AbstractDict} + varnames_length_check(p, names) + [toparam(variable(names[i])) for i in eachindex(p)] + else + [toparam(variable(names))] + end +end + +function define_params(p::AbstractDict, t, names = nothing) + if names === nothing + OrderedDict(k => toparam(variable(:α, i)) for (i, k) in zip(1:length(p), keys(p))) + else + varnames_length_check(p, names) + OrderedDict(k => toparam(variable(names[k])) for k in keys(p)) + end +end + +function define_params(p::Tuple, t, names = nothing) + if names === nothing + tuple((toparam(variable(:α, i)) for i in eachindex(p))...) + else + varnames_length_check(p, names) + tuple((toparam(variable(names[i])) for i in eachindex(p))...) + end +end + +function define_params(p::NamedTuple, t, names = nothing) + if names === nothing + NamedTuple(x => toparam(variable(x)) for x in keys(p)) + else + varnames_length_check(p, names) + NamedTuple(x => toparam(variable(names[x])) for x in keys(p)) + end +end + +function define_params(p::MTKParameters, t, names = nothing) + if names === nothing + ps = [] + i = 1 + # tunables are all treated as scalar reals + for x in p.tunable + push!(ps, toparam(variable(:α, i))) + i += 1 + end + # ignore initials + # discretes should be time-dependent + for buf in p.discrete + T = eltype(buf) + for val in buf + # respect array sizes + shape = val isa AbstractArray ? axes(val) : nothing + push!(ps, declare_timevarying_parameter(:α, i, t; T, shape)) + i += 1 + end + end + # handle constants + for buf in p.constant + T = eltype(buf) + for val in buf + # respect array sizes + shape = val isa AbstractArray ? axes(val) : nothing + push!(ps, declare_parameter(:α, i; T, shape)) + i += 1 + end + end + # handle nonnumerics + for buf in p.nonnumeric + T = eltype(buf) + for val in buf + # respect array sizes + shape = val isa AbstractArray ? axes(val) : nothing + push!(ps, declare_parameter(:α, i; T, shape)) + i += 1 + end + end + return identity.(ps) + else + new_p = as_any_buffer(p) + @set! new_p.initials = [] + for (k, v) in names + val = p[k] + shape = val isa AbstractArray ? axes(val) : nothing + T = typeof(val) + if k.portion == SciMLStructures.Initials() + continue + end + if k.portion == SciMLStructures.Tunable() + T = Real + end + if k.portion == SciMLStructures.Discrete() + var = declare_timevarying_parameter(getname(v), nothing, t; T, shape) + else + var = declare_parameter(getname(v), nothing; T, shape) + end + new_p[k] = var + end + return new_p + end +end + +""" + $(TYPEDSIGNATURES) + +Given a parameter object `p` containing symbolic variables instead of values, return +a vector of the symbolic variables. +""" +function to_paramvec(p) + vec(collect(values(p))) +end + +function to_paramvec(p::MTKParameters) + reduce(vcat, collect(p); init = []) +end + +""" + $(TYPEDSIGNATURES) + +Create a time-varying parameter with name `x`, subscript `i`, independent variable `t` +which stores values of type `T`. `shape` denotes the shape of array values, or `nothing` +for scalars. + +To ignore the subscript, pass `nothing` for `i`. +""" +function declare_timevarying_parameter(x::Symbol, i, t; T, shape = nothing) + # turn specific floating point numbers to `Real` + if T <: Union{AbstractFloat, ForwardDiff.Dual} + T = Real + end + if T <: Array{<:Union{AbstractFloat, ForwardDiff.Dual}, N} where {N} + T = Array{Real, ndims(T)} + end + + if i === nothing + var = _defvar(x; T) + else + var = _defvaridx(x, i; T) + end + var = toparam(unwrap(var(t))) + if shape !== nothing + var = setmetadata(var, Symbolics.ArrayShapeCtx, shape) + end + return var +end + +""" + $(TYPEDSIGNATURES) + +Create a time-varying parameter with name `x` and subscript `i`, which stores values of +type `T`. `shape` denotes the shape of array values, or `nothing` for scalars. + +To ignore the subscript, pass `nothing` for `i`. +""" +function declare_parameter(x::Symbol, i; T, shape = nothing) + # turn specific floating point numbers to `Real` + if T <: Union{AbstractFloat, ForwardDiff.Dual} + T = Real + end + if T <: Array{<:Union{AbstractFloat, ForwardDiff.Dual}, N} where {N} + T = Array{Real, ndims(T)} + end + + i = i === nothing ? () : (i,) + var = toparam(unwrap(variable(x, i...; T))) + if shape !== nothing + var = setmetadata(var, Symbolics.ArrayShapeCtx, shape) + end + return var +end + +""" + $(TYPEDSIGNATURES) + +Return a symbolic parameter object for the given proble `prob.`. `t` is the independent +variable. `p_names` optionally contains the names to use for the created symbolic +variables. +""" +function construct_params(prob, t, p_names = nothing) + p = parameter_values(prob) + has_p = !(p isa Union{DiffEqBase.NullParameters, Nothing}) + + # Get names of parameters + if has_p + if p_names === nothing && SciMLBase.has_sys(prob.f) + # get names from the system + p_names = Dict(parameter_index(prob.f.sys, sym) => sym + for sym in parameter_symbols(prob.f.sys)) + end + params = define_params(p, t, p_names) + if p isa Number + params = params[1] + elseif p isa AbstractArray + params = ArrayInterface.restructure(p, params) + end + else + params = [] + end + + return params +end + +""" + $(TYPEDSIGNATURES) + +Given the differential operator `D`, mass matrix `mm` and ordered list of unknowns `vars`, +return the list of +""" +function lhs_from_mass_matrix(D, mm, vars) + var_set = Set(vars) + # calculate equation LHS from mass matrix + if mm === I + lhs = map(v -> D(v), vars) + else + lhs = map(mm * vars) do v + if iszero(v) + 0 + elseif v in var_set + D(v) + else + error("Non-permutation mass matrix is not supported.") + end + end + end + return lhs +end + +""" + $(TYPEDSIGNATURES) + +Given a problem `prob`, the symbolic unknowns and params and the independent variable, +trace through `prob.f` and return the resultant expression. +""" +function trace_rhs(prob, vars, params, t) + args = (vars, params) + if t !== nothing + args = (args..., t) + end + # trace prob.f to get equation RHS + if SciMLBase.isinplace(prob.f) + rhs = ArrayInterface.restructure(prob.u0, similar(vars, Num)) + fill!(rhs, 0) + if prob.f isa SciMLBase.AbstractSciMLFunction && + prob.f.f isa FunctionWrappersWrappers.FunctionWrappersWrapper + prob.f.f.fw[1].obj[](rhs, args...) + else + prob.f(rhs, args...) + end + else + rhs = prob.f(args...) + end + return rhs +end + +""" + $(TYPEDSIGNATURES) + +Obtain default values for unknowns `vars` and parameters `paramvec` +given the problem `prob` and symbolic parameter object `paramobj`. +""" +function defaults_from_u0_p(prob, vars, paramobj, paramvec) + u0 = state_values(prob) + p = parameter_values(prob) + defaults = Dict{Any, Any}(vec(vars) .=> vec(collect(u0))) + if !(p isa Union{SciMLBase.NullParameters, Nothing}) + if p isa Union{NamedTuple, AbstractDict} + merge!(defaults, Dict(v => p[k] for (k, v) in pairs(paramobj))) + elseif p isa MTKParameters + pvals = [p.tunable; reduce(vcat, p.discrete; init = []); + reduce(vcat, p.constant; init = []); + reduce(vcat, p.nonnumeric; init = [])] + merge!(defaults, Dict(paramvec .=> pvals)) + else + merge!(defaults, Dict(paramvec .=> vec(collect(p)))) + end + end + return defaults +end diff --git a/src/modelingtoolkitize/odeproblem.jl b/src/modelingtoolkitize/odeproblem.jl new file mode 100644 index 0000000000..3bc74d8887 --- /dev/null +++ b/src/modelingtoolkitize/odeproblem.jl @@ -0,0 +1,59 @@ +""" + $(TYPEDSIGNATURES) + +Convert an `ODEProblem` to a `ModelingToolkit.System`. + +# Keyword arguments + +- `u_names`: An array of names of the same size as `prob.u0` to use as the names of the + unknowns of the system. The names should be given as `Symbol`s. +- `p_names`: A collection of names to use for parameters of the system. The collection + should have keys corresponding to indexes of `prob.p`. For example, if `prob.p` is an + associative container like `NamedTuple`, then `p_names` should map keys of `prob.p` to + the name that the corresponding parameter should have in the returned system. The names + should be given as `Symbol`s. +- INTERNAL `return_symbolic_u0_p`: Also return the symbolic state and parameter objects. + +All other keyword arguments are forwarded to the created `System`. +""" +function modelingtoolkitize(prob::ODEProblem; u_names = nothing, p_names = nothing, + return_symbolic_u0_p = false, kwargs...) + if prob.f isa DiffEqBase.AbstractParameterizedFunction + return prob.f.sys + end + + t = t_nounits + p = prob.p + has_p = !(p isa Union{DiffEqBase.NullParameters, Nothing}) + + vars = construct_vars(prob, t, u_names) + params = construct_params(prob, t, p_names) + + lhs = lhs_from_mass_matrix(D_nounits, prob.f.mass_matrix, vars) + rhs = trace_rhs(prob, vars, params, t) + eqs = vcat([lhs[i] ~ rhs[i] for i in eachindex(prob.u0)]...) + + sts = vec(collect(vars)) + + # turn `params` into a list of symbolic variables as opposed to + # a parameter object containing symbolic variables. + _params = params + params = to_paramvec(params) + + defaults = defaults_from_u0_p(prob, vars, _params, params) + # In case initials crept in, specifically from when we constructed parameters + # using prob.f.sys + filter!(x -> !iscall(x) || !(operation(x) isa Initial), params) + filter!(x -> !iscall(x[1]) || !(operation(x[1]) isa Initial), defaults) + + sys = System(eqs, t, sts, params; + defaults, + name = gensym(:MTKizedODE), + kwargs...) + + if return_symbolic_u0_p + return sys, vars, _params + else + return sys + end +end From 0425dc26691fd8d305ce3760c7011511c148351e Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 29 Apr 2025 15:25:04 +0530 Subject: [PATCH 1718/2176] feat: add `modelingtoolkitize` for `SDEProblem` --- src/ModelingToolkit.jl | 1 + src/modelingtoolkitize/sdeproblem.jl | 53 ++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+) create mode 100644 src/modelingtoolkitize/sdeproblem.jl diff --git a/src/ModelingToolkit.jl b/src/ModelingToolkit.jl index f4ffb8459b..ccfbaf9a52 100644 --- a/src/ModelingToolkit.jl +++ b/src/ModelingToolkit.jl @@ -184,6 +184,7 @@ include("problems/bvproblem.jl") include("modelingtoolkitize/common.jl") include("modelingtoolkitize/odeproblem.jl") +include("modelingtoolkitize/sdeproblem.jl") include("systems/optimization/modelingtoolkitize.jl") diff --git a/src/modelingtoolkitize/sdeproblem.jl b/src/modelingtoolkitize/sdeproblem.jl new file mode 100644 index 0000000000..ff8c238559 --- /dev/null +++ b/src/modelingtoolkitize/sdeproblem.jl @@ -0,0 +1,53 @@ +""" + $(TYPEDSIGNATURES) + +Convert an `SDEProblem` to a `ModelingToolkit.System`. + +# Keyword arguments + +- `u_names`: an array of names of the same size as `prob.u0` to use as the names of the + unknowns of the system. The names should be given as `Symbol`s. +- `p_names`: a collection of names to use for parameters of the system. The collection + should have keys corresponding to indexes of `prob.p`. For example, if `prob.p` is an + associative container like `NamedTuple`, then `p_names` should map keys of `prob.p` to + the name that the corresponding parameter should have in the returned system. The names + should be given as `Symbol`s. + +All other keyword arguments are forwarded to the created `System`. +""" +function modelingtoolkitize( + prob::SDEProblem; u_names = nothing, p_names = nothing, kwargs...) + if prob.f isa DiffEqBase.AbstractParameterizedFunction + return prob.f.sys + end + + # just create the equivalent ODEProblem, `modelingtoolkitize` that + # and add on the noise + odefn = ODEFunction{SciMLBase.isinplace(prob)}( + prob.f.f; mass_matrix = prob.f.mass_matrix, sys = prob.f.sys) + odeprob = ODEProblem(odefn, prob.u0, prob.tspan, prob.p) + sys, vars, params = modelingtoolkitize( + odeprob; u_names, p_names, return_symbolic_u0_p = true, + name = gensym(:MTKizedSDE), kwargs...) + t = get_iv(sys) + + if SciMLBase.isinplace(prob) + if SciMLBase.is_diagonal_noise(prob) + neqs = similar(vars, Any) + prob.g(neqs, vars, params, t) + else + neqs = similar(prob.noise_rate_prototype, Any) + prob.g(neqs, vars, params, t) + end + else + if SciMLBase.is_diagonal_noise(prob) + neqs = prob.g(vars, params, t) + else + neqs = prob.g(vars, params, t) + end + end + + @set! sys.noise_eqs = neqs + + return sys +end From cee2a6d35c911937947caf5dcd747149355781db Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 29 Apr 2025 16:05:33 +0530 Subject: [PATCH 1719/2176] feat: add `add_accumulations` --- src/ModelingToolkit.jl | 2 +- src/systems/diffeqs/basic_transformations.jl | 38 ++++++++++++++++++++ test/basic_transformations.jl | 22 ++++++++++++ 3 files changed, 61 insertions(+), 1 deletion(-) diff --git a/src/ModelingToolkit.jl b/src/ModelingToolkit.jl index ccfbaf9a52..c22d78f30f 100644 --- a/src/ModelingToolkit.jl +++ b/src/ModelingToolkit.jl @@ -302,7 +302,7 @@ export isinput, isoutput, getbounds, hasbounds, getguess, hasguess, isdisturbanc hasunit, getunit, hasconnect, getconnect, hasmisc, getmisc, state_priority export ode_order_lowering, dae_order_lowering, liouville_transform, - change_independent_variable, substitute_component + change_independent_variable, substitute_component, add_accumulations export PDESystem export Differential, expand_derivatives, @derivatives export Equation, ConstrainedEquation diff --git a/src/systems/diffeqs/basic_transformations.jl b/src/systems/diffeqs/basic_transformations.jl index 21dcc7977a..c9d5234751 100644 --- a/src/systems/diffeqs/basic_transformations.jl +++ b/src/systems/diffeqs/basic_transformations.jl @@ -409,3 +409,41 @@ function Girsanov_transform(sys::System, u; θ0 = 1.0) end return sys end + +""" + $(TYPEDSIGNATURES) + +Add accumulation variables for `vars`. For every unknown `x` in `vars`, add +`D(accumulation_x) ~ x` as an equation. +""" +function add_accumulations(sys::System, vars = unknowns(sys)) + avars = [rename(v, Symbol(:accumulation_, getname(v))) for v in vars] + return add_accumulations(sys, avars .=> vars) +end + +""" + $(TYPEDSIGNATURES) + +Add accumulation variables for `vars`. `vars` is a vector of pairs in the form +of + +```julia +[cumulative_var1 => x + y, cumulative_var2 => x^2] +``` +Then, cumulative variables `cumulative_var1` and `cumulative_var2` that computes +the cumulative `x + y` and `x^2` would be added to `sys`. + +All accumulation variables have a default of zero. +""" +function add_accumulations(sys::System, vars::Vector{<:Pair}) + eqs = get_eqs(sys) + avars = map(first, vars) + if (ints = intersect(avars, unknowns(sys)); !isempty(ints)) + error("$ints already exist in the system!") + end + D = Differential(get_iv(sys)) + @set! sys.eqs = [eqs; Equation[D(a) ~ v[2] for (a, v) in zip(avars, vars)]] + @set! sys.unknowns = [get_unknowns(sys); avars] + @set! sys.defaults = merge(get_defaults(sys), Dict(a => 0.0 for a in avars)) + return sys +end diff --git a/test/basic_transformations.jl b/test/basic_transformations.jl index a414120ed1..50f4c0feaf 100644 --- a/test/basic_transformations.jl +++ b/test/basic_transformations.jl @@ -304,3 +304,25 @@ end sol = solve(prob, Tsit5(); reltol = 1e-5) @test sol[new_sys.y][end] ≈ 0.75 end + +@testset "`add_accumulations`" begin + @parameters a + @variables x(t) y(t) z(t) + @named sys = System([D(x) ~ y, 0 ~ x + z, 0 ~ x - y], t, [z, y, x], []) + asys = add_accumulations(sys) + @variables accumulation_x(t) accumulation_y(t) accumulation_z(t) + eqs = [0 ~ x + z + 0 ~ x - y + D(accumulation_x) ~ x + D(accumulation_y) ~ y + D(accumulation_z) ~ z + D(x) ~ y] + @test issetequal(equations(asys), eqs) + @variables ac(t) + asys = add_accumulations(sys, [ac => (x + y)^2]) + eqs = [0 ~ x + z + 0 ~ x - y + D(ac) ~ (x + y)^2 + D(x) ~ y] + @test issetequal(equations(asys), eqs) +end From 1e4cb23943873b7bb9eb8b0ec3df582d130216c3 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 29 Apr 2025 16:27:10 +0530 Subject: [PATCH 1720/2176] test: fix `odesystem` tests --- test/odesystem.jl | 164 +++++++++++++--------------------------------- 1 file changed, 47 insertions(+), 117 deletions(-) diff --git a/test/odesystem.jl b/test/odesystem.jl index 7f82807459..defef0bbaa 100644 --- a/test/odesystem.jl +++ b/test/odesystem.jl @@ -93,29 +93,29 @@ f.f(du, u, p, 0.1) @test_throws ArgumentError f.f(u, p, 0.1) #check iip -f = eval(ODEFunctionExpr(de, [x, y, z], [σ, ρ, β])) -f2 = ODEFunction(de, [x, y, z], [σ, ρ, β]) +f = eval(ODEFunction(de; expression = Val{true})) +f2 = ODEFunction(de) @test SciMLBase.isinplace(f) === SciMLBase.isinplace(f2) @test SciMLBase.specialization(f) === SciMLBase.specialization(f2) for iip in (true, false) - f = eval(ODEFunctionExpr{iip}(de, [x, y, z], [σ, ρ, β])) - f2 = ODEFunction{iip}(de, [x, y, z], [σ, ρ, β]) + f = eval(ODEFunction{iip}(de; expression = Val{true})) + f2 = ODEFunction{iip}(de) @test SciMLBase.isinplace(f) === SciMLBase.isinplace(f2) === iip @test SciMLBase.specialization(f) === SciMLBase.specialization(f2) for specialize in (SciMLBase.AutoSpecialize, SciMLBase.FullSpecialize) - f = eval(ODEFunctionExpr{iip, specialize}(de, [x, y, z], [σ, ρ, β])) - f2 = ODEFunction{iip, specialize}(de, [x, y, z], [σ, ρ, β]) + f = eval(ODEFunction{iip, specialize}(de; expression = Val{true})) + f2 = ODEFunction{iip, specialize}(de) @test SciMLBase.isinplace(f) === SciMLBase.isinplace(f2) === iip @test SciMLBase.specialization(f) === SciMLBase.specialization(f2) === specialize end end #check sparsity -f = eval(ODEFunctionExpr(de, [x, y, z], [σ, ρ, β], sparsity = true)) +f = eval(ODEFunction(de, sparsity = true, expression = Val{true})) @test f.sparsity == ModelingToolkit.jacobian_sparsity(de) -f = eval(ODEFunctionExpr(de, [x, y, z], [σ, ρ, β], sparsity = false)) +f = eval(ODEFunction(de, sparsity = false, expression = Val{true})) @test isnothing(f.sparsity) eqs = [D(x) ~ σ * (y - x), @@ -138,9 +138,9 @@ tgrad_iip(du, u, p, t) eqs = [D(x) ~ σ(t - 1) * (y - x), D(y) ~ x * (ρ - z) - y, D(z) ~ x * y - β * z * κ] -@named de = System(eqs, t) +@named de = System(eqs, t, [x, y, z], [σ, ρ, β]) test_diffeq_inference("single internal iv-varying", de, t, (x, y, z), (σ, ρ, β)) -f = generate_rhs(de, [x, y, z], [σ, ρ, β], expression = Val{false}) +f = generate_rhs(de, expression = Val{false}, wrap_gfw = Val{true}) du = [0.0, 0.0, 0.0] f(du, [1.0, 2.0, 3.0], [x -> x + 7, 2, 3], 5.0) @test du ≈ [11, -3, -7] @@ -148,7 +148,7 @@ f(du, [1.0, 2.0, 3.0], [x -> x + 7, 2, 3], 5.0) eqs = [D(x) ~ x + 10σ(t - 1) + 100σ(t - 2) + 1000σ(t^2)] @named de = System(eqs, t) test_diffeq_inference("many internal iv-varying", de, t, (x,), (σ,)) -f = generate_rhs(de, [x], [σ], expression = Val{false}) +f = generate_rhs(de, [x], [σ], expression = Val{false}, wrap_gfw = Val{true}) du = [0.0] f(du, [1.0], [t -> t + 2], 5.0) @test du ≈ [27561] @@ -199,7 +199,7 @@ eqs = [D(x) ~ -A * x, @named de = System(eqs, t) @test begin local f, du - f = generate_rhs(de, [x, y], [A, B, C], expression = Val{false}) + f = generate_rhs(de, [x, y], [A, B, C], expression = Val{false}, wrap_gfw = Val{true}) du = [0.0, 0.0] f(du, [1.0, 2.0], [1, 2, 3], 0.0) du ≈ [-1, -1 / 3] @@ -295,7 +295,7 @@ sol_dpmap = solve(prob_dpmap, Rodas5()) sys = makecombinedsys() @unpack sys1, b = sys - prob = ODEProblem(sys, Pair[]) + prob = ODEProblem(sys, Pair[], (0.0, 1.0)) prob_new = SciMLBase.remake(prob, p = Dict(sys1.a => 3.0, b => 4.0), u0 = Dict(sys1.x => 1.0)) @test prob_new.p isa MTKParameters @@ -322,7 +322,7 @@ du0 = [D(y₁) => -0.04 D(y₂) => 0.04 D(y₃) => 0.0] prob4 = DAEProblem(sys, du0, u0, tspan, p2) -prob5 = eval(DAEProblemExpr(sys, du0, u0, tspan, p2)) +prob5 = eval(DAEProblem(sys, du0, u0, tspan, p2; expression = Val{true})) for prob in [prob4, prob5] local sol @test prob.differential_vars == [true, true, false] @@ -336,42 +336,27 @@ eqs = [D(x) ~ σ * (y - x), D(y) ~ x - β * y, x + z ~ y] @named sys = System(eqs, t) -@test all(isequal.(unknowns(sys), [x, y, z])) -@test all(isequal.(parameters(sys), [σ, β])) +@test issetequal(unknowns(sys), [x, y, z]) +@test issetequal(parameters(sys), [σ, β]) @test equations(sys) == eqs @test ModelingToolkit.isautonomous(sys) -# issue 701 -using ModelingToolkit -@parameters a -@variables x(t) -@named sys = System([D(x) ~ a], t) -@test issym(equations(sys)[1].rhs) +@testset "Issue#701: `collect_vars!` handles non-call symbolics" begin + @parameters a + @variables x(t) + @named sys = System([D(x) ~ a], t) + @test issym(equations(sys)[1].rhs) +end -# issue 708 -@parameters a -@variables x(t) y(t) z(t) -@named sys = System([D(x) ~ y, 0 ~ x + z, 0 ~ x - y], t, [z, y, x], []) -asys = add_accumulations(sys) -@variables accumulation_x(t) accumulation_y(t) accumulation_z(t) -eqs = [0 ~ x + z - 0 ~ x - y - D(accumulation_x) ~ x - D(accumulation_y) ~ y - D(accumulation_z) ~ z - D(x) ~ y] -@test ssort(equations(asys)) == ssort(eqs) -@variables ac(t) -asys = add_accumulations(sys, [ac => (x + y)^2]) -eqs = [0 ~ x + z - 0 ~ x - y - D(ac) ~ (x + y)^2 - D(x) ~ y] -@test ssort(equations(asys)) == ssort(eqs) - -sys2 = ode_order_lowering(sys) -M = ModelingToolkit.calculate_massmatrix(sys2) -@test M == Diagonal([1, 0, 0]) +@testset "Issue#708" begin + @parameters a + @variables x(t) y(t) z(t) + @named sys = System([D(x) ~ y, 0 ~ x + z, 0 ~ x - y], t, [z, y, x], []) + + sys2 = ode_order_lowering(sys) + M = ModelingToolkit.calculate_massmatrix(sys2) + @test M == Diagonal([1, 0, 0]) +end # issue #609 @variables x1(t) x2(t) @@ -404,7 +389,8 @@ eq = D(x) ~ r * x sys1 = makesys(:sys1) sys2 = makesys(:sys1) - @test_throws ArgumentError System([sys2.f ~ sys1.x, D(sys1.f) ~ 0], t, + @test_throws ModelingToolkit.NonUniqueSubsystemsError System( + [sys2.f ~ sys1.x, D(sys1.f) ~ 0], t, systems = [sys1, sys2], name = :foo) end issue808() @@ -436,14 +422,14 @@ eqs = [D(D(x)) ~ -b / M * D(x) - k / M * x] ps = [M, b, k] default_u0 = [D(x) => 0.0, x => 10.0] default_p = [M => 1.0, b => 1.0, k => 1.0] -@named sys = System(eqs, t, [x], ps; defaults = [default_u0; default_p], tspan) +@named sys = System(eqs, t, [x], ps; defaults = [default_u0; default_p]) sys = ode_order_lowering(sys) sys = complete(sys) -prob = ODEProblem(sys) +prob = ODEProblem(sys, nothing, tspan) sol = solve(prob, Tsit5()) @test sol.t[end] == tspan[end] @test sum(abs, sol.u[end]) < 1 -prob = ODEProblem{false}(sys; u0_constructor = x -> SVector(x...)) +prob = ODEProblem{false}(sys, nothing, tspan; u0_constructor = x -> SVector(x...)) @test prob.u0 isa SVector # check_eqs_u0 kwarg test @@ -454,23 +440,9 @@ sys = complete(sys) @test_throws ArgumentError ODEProblem(sys, [1.0, 1.0], (0.0, 1.0)) @test_nowarn ODEProblem(sys, [1.0, 1.0], (0.0, 1.0), check_length = false) -# check inputs -let - @parameters k d - @variables x(t) ẋ(t) f(t) [input = true] - - eqs = [D(x) ~ ẋ, D(ẋ) ~ f - k * x - d * ẋ] - @named sys = System(eqs, t, [x, ẋ], [f, d, k]) - sys = structural_simplify(sys; inputs = [f]) - - @test isequal(calculate_control_jacobian(sys), - reshape(Num[0, 1], 2, 1)) -end - -# issue 1109 -let +@testset "Issue#1109" begin @variables x(t)[1:3, 1:3] - @named sys = System(D.(x) .~ x, t) + @named sys = System(D(x) ~ x, t) @test_nowarn structural_simplify(sys) end @@ -739,51 +711,6 @@ let @test length(equations(structural_simplify(sys))) == 2 end -let - eq_to_lhs(eq) = eq.lhs - eq.rhs ~ 0 - eqs_to_lhs(eqs) = eq_to_lhs.(eqs) - - @parameters σ=10 ρ=28 β=8 / 3 sigma rho beta - @variables x(t)=1 y(t)=0 z(t)=0 x2(t)=1 y2(t)=0 z2(t)=0 u(t)[1:3] - - eqs = [D(x) ~ σ * (y - x), - D(y) ~ x * (ρ - z) - y, - D(z) ~ x * y - β * z] - - eqs2 = [ - D(y2) ~ x2 * (rho - z2) - y2, - D(x2) ~ sigma * (y2 - x2), - D(z2) ~ x2 * y2 - beta * z2 - ] - - # array u - eqs3 = [D(u[1]) ~ sigma * (u[2] - u[1]), - D(u[2]) ~ u[1] * (rho - u[3]) - u[2], - D(u[3]) ~ u[1] * u[2] - beta * u[3]] - eqs3 = eqs_to_lhs(eqs3) - - eqs4 = [ - D(y2) ~ x2 * (rho - z2) - y2, - D(x2) ~ sigma * (y2 - x2), - D(z2) ~ y2 - beta * z2 # missing x2 term - ] - - @named sys1 = System(eqs, t) - @named sys2 = System(eqs2, t) - @named sys3 = System(eqs3, t) - ssys3 = structural_simplify(sys3) - @named sys4 = System(eqs4, t) - - @test ModelingToolkit.isisomorphic(sys1, sys2) - @test !ModelingToolkit.isisomorphic(sys1, sys3) - @test ModelingToolkit.isisomorphic(sys1, ssys3) # I don't call structural_simplify in isisomorphic - @test !ModelingToolkit.isisomorphic(sys1, sys4) - - # 1281 - iv2 = only(independent_variables(sys2)) - @test isequal(only(independent_variables(convert_system(System, sys1, iv2))), iv2) -end - let vars = @variables sP(t) spP(t) spm(t) sph(t) pars = @parameters a b @@ -1572,14 +1499,16 @@ end @parameters p d @variables X(t)::Int64 eq = D(X) ~ p - d * X - @test_throws ArgumentError @mtkbuild osys = System([eq], t) + @test_throws ModelingToolkit.ContinuousOperatorDiscreteArgumentError @mtkbuild osys = System( + [eq], t) @variables Y(t)[1:3]::String eq = D(Y) ~ [p, p, p] - @test_throws ArgumentError @mtkbuild osys = System([eq], t) + @test_throws ModelingToolkit.ContinuousOperatorDiscreteArgumentError @mtkbuild osys = System( + [eq], t) @variables X(t)::Complex - eq = D(X) ~ p - d * X - @test_nowarn @named osys = System([eq], t) + eqs = D(X) ~ p - d * X + @test_nowarn @named osys = System(eqs, t) end # Test `isequal` @@ -1662,7 +1591,8 @@ end @test sol.t[end] == tspan[end] @test sum(abs, sol.u[end]) < 1 - prob = ODEProblem{false}(lowered_dae_sys; u0_constructor = x -> SVector(x...)) + prob = ODEProblem{false}( + lowered_dae_sys, nothing, tspan; u0_constructor = x -> SVector(x...)) @test prob.u0 isa SVector end @@ -1710,7 +1640,7 @@ end eqs = D(x(t)) ~ mat * x(t) cons = [x(3) ~ [2, 3, 3, 5, 4]] @mtkbuild ode = System(D(x(t)) ~ mat * x(t), t; constraints = cons) - @test length(constraints(ModelingToolkit.get_constraintsystem(ode))) == 5 + @test length(constraints(ode)) == 1 end @testset "`build_explicit_observed_function` with `expression = true` returns `Expr`" begin From a815755de4798965589651ea1bc277af5e01eedb Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 29 Apr 2025 21:20:29 +0530 Subject: [PATCH 1721/2176] fix: validate that `Sample` operates on unknowns --- src/discretedomain.jl | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/discretedomain.jl b/src/discretedomain.jl index 1eeec4c014..f8a1a17009 100644 --- a/src/discretedomain.jl +++ b/src/discretedomain.jl @@ -146,6 +146,11 @@ function validate_operator(op::Sample, args, iv; context = nothing) if !is_variable_floatingpoint(arg) throw(ContinuousOperatorDiscreteArgumentError(op, arg, context)) end + if isparameter(arg) + throw(ArgumentError(""" + Expected argument of $op to be an unknown, found $arg which is a parameter. + """)) + end end """ From c7d37e5728fae05f3ce3a09ec20f1ddef611fec3 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 29 Apr 2025 21:20:57 +0530 Subject: [PATCH 1722/2176] test: fix `test/structural_transformation/utils.jl` --- test/structural_transformation/utils.jl | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/test/structural_transformation/utils.jl b/test/structural_transformation/utils.jl index af3be46572..7611ab5d33 100644 --- a/test/structural_transformation/utils.jl +++ b/test/structural_transformation/utils.jl @@ -191,11 +191,6 @@ end end @testset "`map_variables_to_equations`" begin - @testset "Not supported for systems without `.tearing_state`" begin - @variables x - @mtkbuild sys = OptimizationSystem(x^2) - @test_throws ArgumentError map_variables_to_equations(sys) - end @testset "Requires simplified system" begin @variables x(t) y(t) @named sys = System([D(x) ~ x, y ~ 2x], t) @@ -299,7 +294,7 @@ end eqs = [D(x) ~ dx D(dx) ~ ddx dx ~ (k - x) / T] - return ODESystem(eqs, t, vars, params; systems, name) + return System(eqs, t, vars, params; systems, name) end @component function FilteredInputExplicit(; name, x0 = 0, T = 0.1) @@ -317,7 +312,7 @@ end D(dx) ~ ddx D(k[1]) ~ 1.0 dx ~ (k[1] - x) / T] - return ODESystem(eqs, t, vars, params; systems, name) + return System(eqs, t, vars, params; systems, name) end @component function FilteredInputErr(; name, x0 = 0, T = 0.1) @@ -335,7 +330,7 @@ end D(dx) ~ ddx dx ~ (k - x) / T D(k) ~ missing] - return ODESystem(eqs, t, vars, params; systems, name) + return System(eqs, t, vars, params; systems, name) end @named sys = FilteredInputErr() @@ -376,7 +371,7 @@ end eqs = [D(x) ~ dx D(dx) ~ ddx dx ~ (k(t) - x) / T] - return ODESystem(eqs, t, vars, params; systems, name) + return System(eqs, t, vars, params; systems, name) end @mtkbuild sys = FilteredInput2() From 2b7124bdc1319fecc0a3bd45c8adcb95ea4e8ea0 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 29 Apr 2025 21:21:27 +0530 Subject: [PATCH 1723/2176] test: simplify test for metadata retention in `complete` --- test/components.jl | 37 +++++-------------------------------- 1 file changed, 5 insertions(+), 32 deletions(-) diff --git a/test/components.jl b/test/components.jl index 19f529bd0a..7680afc50c 100644 --- a/test/components.jl +++ b/test/components.jl @@ -327,36 +327,9 @@ end @testset "Issue#3275: Metadata retained on `complete`" begin @variables x(t) y(t) - @testset "ODESystem" begin - @named inner = System(D(x) ~ x, t) - @named outer = System(D(y) ~ y, t; systems = [inner], metadata = "test") - @test ModelingToolkit.get_metadata(outer) == "test" - sys = complete(outer) - @test ModelingToolkit.get_metadata(sys) == "test" - end - @testset "NonlinearSystem" begin - @named inner = System([0 ~ x^2 + 4x + 4], [x], []) - @named outer = System( - [0 ~ x^3 - y^3], [x, y], []; systems = [inner], metadata = "test") - @test ModelingToolkit.get_metadata(outer) == "test" - sys = complete(outer) - @test ModelingToolkit.get_metadata(sys) == "test" - end - k = ShiftIndex(t) - @testset "DiscreteSystem" begin - @named inner = System([x(k) ~ x(k - 1) + x(k - 2)], t, [x], []) - @named outer = System([y(k) ~ y(k - 1) + y(k - 2)], t, [x, y], - []; systems = [inner], metadata = "test") - @test ModelingToolkit.get_metadata(outer) == "test" - sys = complete(outer) - @test ModelingToolkit.get_metadata(sys) == "test" - end - @testset "OptimizationSystem" begin - @named inner = OptimizationSystem(x^2 + y^2 - 3, [x, y], []) - @named outer = OptimizationSystem( - x^3 - y, [x, y], []; systems = [inner], metadata = "test") - @test ModelingToolkit.get_metadata(outer) == "test" - sys = complete(outer) - @test ModelingToolkit.get_metadata(sys) == "test" - end + @named inner = System(D(x) ~ x, t) + @named outer = System(D(y) ~ y, t; systems = [inner], metadata = "test") + @test ModelingToolkit.get_metadata(outer) == "test" + sys = complete(outer) + @test ModelingToolkit.get_metadata(sys) == "test" end From 22a175f8b3d898d8288c71147c702c944d65344c Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 29 Apr 2025 21:22:11 +0530 Subject: [PATCH 1724/2176] test: improve readability of dependency graph tests --- test/dep_graphs.jl | 290 +++++++++++++++++++++------------------------ 1 file changed, 136 insertions(+), 154 deletions(-) diff --git a/test/dep_graphs.jl b/test/dep_graphs.jl index 3c7b88dd05..04e6bf159a 100644 --- a/test/dep_graphs.jl +++ b/test/dep_graphs.jl @@ -6,74 +6,7 @@ import ModelingToolkit: value ################################# # testing for Jumps / all dgs ################################# -@parameters k1 k2 -@variables S(t) I(t) R(t) -j₁ = MassActionJump(k1, [0 => 1], [S => 1]) -j₂ = MassActionJump(k1, [S => 1], [S => -1]) -j₃ = MassActionJump(k2, [S => 1, I => 1], [S => -1, I => 1]) -j₄ = MassActionJump(k2, [S => 2, R => 1], [R => -1]) -j₅ = ConstantRateJump(k1 * I, [R ~ R + 1]) -j₆ = VariableRateJump(k1 * k2 / (1 + t) * S, [S ~ S - 1, R ~ R + 1]) -eqs = [j₁, j₂, j₃, j₄, j₅, j₆] -@named js = JumpSystem(eqs, t, [S, I, R], [k1, k2]) -S = value(S) -I = value(I) -R = value(R) -k1 = value(k1) -k2 = value(k2) -# eq to vars they depend on -eq_sdeps = [Variable[], [S], [S, I], [S, R], [I], [S]] -eq_sidepsf = [Int[], [1], [1, 2], [1, 3], [2], [1]] -eq_sidepsb = [[2, 3, 4, 6], [3, 5], [4]] -deps = equation_dependencies(js) -@test all(i -> isequal(Set(eq_sdeps[i]), Set(deps[i])), 1:length(eqs)) -depsbg = asgraph(js) -@test depsbg.fadjlist == eq_sidepsf -@test depsbg.badjlist == eq_sidepsb - -# eq to params they depend on -eq_pdeps = [[k1], [k1], [k2], [k2], [k1], [k1, k2]] -eq_pidepsf = [[1], [1], [2], [2], [1], [1, 2]] -eq_pidepsb = [[1, 2, 5, 6], [3, 4, 6]] -deps = equation_dependencies(js, variables = parameters(js)) -@test all(i -> isequal(Set(eq_pdeps[i]), Set(deps[i])), 1:length(eqs)) -depsbg2 = asgraph(js, variables = parameters(js)) -@test depsbg2.fadjlist == eq_pidepsf -@test depsbg2.badjlist == eq_pidepsb - -# var to eqs that modify them -s_eqdepsf = [[1, 2, 3, 6], [3], [4, 5, 6]] -s_eqdepsb = [[1], [1], [1, 2], [3], [3], [1, 3]] -ne = 8 -bg = BipartiteGraph(ne, s_eqdepsf, s_eqdepsb) -deps2 = variable_dependencies(js) -@test isequal(bg, deps2) - -# eq to eqs that depend on them -eq_eqdeps = [[2, 3, 4, 6], [2, 3, 4, 6], [2, 3, 4, 5, 6], [4], [4], [2, 3, 4, 6]] -dg = SimpleDiGraph(6) -for (eqidx, eqdeps) in enumerate(eq_eqdeps) - for eqdepidx in eqdeps - add_edge!(dg, eqidx, eqdepidx) - end -end -dg3 = eqeq_dependencies(depsbg, deps2) -@test dg == dg3 - -# var to vars that depend on them -var_vardeps = [[1, 2, 3], [1, 2, 3], [3]] -ne = 7 -dg = SimpleDiGraph(3) -for (vidx, vdeps) in enumerate(var_vardeps) - for vdepidx in vdeps - add_edge!(dg, vidx, vdepidx) - end -end -dg4 = varvar_dependencies(depsbg, deps2) -@test dg == dg4 - -# testing when ignoring VariableRateJumps -let +@testset "JumpSystem" begin @parameters k1 k2 @variables S(t) I(t) R(t) j₁ = MassActionJump(k1, [0 => 1], [S => 1]) @@ -82,109 +15,158 @@ let j₄ = MassActionJump(k2, [S => 2, R => 1], [R => -1]) j₅ = ConstantRateJump(k1 * I, [R ~ R + 1]) j₆ = VariableRateJump(k1 * k2 / (1 + t) * S, [S ~ S - 1, R ~ R + 1]) - eqs = [j₁, j₂, j₃, j₄, j₅, j₆] - @named js = JumpSystem(eqs, t, [S, I, R], [k1, k2]) + alleqs = [j₁, j₂, j₃, j₄, j₅, j₆] + @named js = JumpSystem(alleqs, t, [S, I, R], [k1, k2]) S = value(S) I = value(I) R = value(R) k1 = value(k1) k2 = value(k2) - # eq to vars they depend on - eq_sdeps = [Variable[], [S], [S, I], [S, R], [I]] - eq_sidepsf = [Int[], [1], [1, 2], [1, 3], [2]] - eq_sidepsb = [[2, 3, 4], [3, 5], [4]] - - # filter out vrjs in making graphs - eqs = ArrayPartition(equations(js).x[1], equations(js).x[2]) - deps = equation_dependencies(js; eqs) - @test length(deps) == length(eq_sdeps) - @test all(i -> isequal(Set(eq_sdeps[i]), Set(deps[i])), 1:length(eqs)) - depsbg = asgraph(js; eqs) - @test depsbg.fadjlist == eq_sidepsf - @test depsbg.badjlist == eq_sidepsb - # eq to params they depend on - eq_pdeps = [[k1], [k1], [k2], [k2], [k1]] - eq_pidepsf = [[1], [1], [2], [2], [1]] - eq_pidepsb = [[1, 2, 5], [3, 4]] - deps = equation_dependencies(js; variables = parameters(js), eqs) - @test length(deps) == length(eq_pdeps) - @test all(i -> isequal(Set(eq_pdeps[i]), Set(deps[i])), 1:length(eqs)) - depsbg2 = asgraph(js; variables = parameters(js), eqs) - @test depsbg2.fadjlist == eq_pidepsf - @test depsbg2.badjlist == eq_pidepsb - - # var to eqs that modify them - s_eqdepsf = [[1, 2, 3], [3], [4, 5]] - s_eqdepsb = [[1], [1], [1, 2], [3], [3]] - ne = 6 - bg = BipartiteGraph(ne, s_eqdepsf, s_eqdepsb) - deps2 = variable_dependencies(js; eqs) - @test isequal(bg, deps2) - - # eq to eqs that depend on them - eq_eqdeps = [[2, 3, 4], [2, 3, 4], [2, 3, 4, 5], [4], [4], [2, 3, 4]] - dg = SimpleDiGraph(5) - for (eqidx, eqdeps) in enumerate(eq_eqdeps) - for eqdepidx in eqdeps - add_edge!(dg, eqidx, eqdepidx) + test_case_1 = (; + eqs = jumps(js), + # eq to vars they depend on + eq_sdeps = [Variable[], [S], [S, I], [S, R], [I], [S]], + eq_sidepsf = [Int[], [1], [1, 2], [1, 3], [2], [1]], + eq_sidepsb = [[2, 3, 4, 6], [3, 5], [4]], + # eq to params they depend on + eq_pdeps = [[k1], [k1], [k2], [k2], [k1], [k1, k2]], + eq_pidepsf = [[1], [1], [2], [2], [1], [1, 2]], + eq_pidepsb = [[1, 2, 5, 6], [3, 4, 6]], + # var to eqs that modify them + s_eqdepsf = [[1, 2, 3, 6], [3], [4, 5, 6]], + s_eqdepsb = [[1], [1], [1, 2], [3], [3], [1, 3]], + var_eq_ne = 8, + # eq to eqs that depend on them + eq_eqdeps = [[2, 3, 4, 6], [2, 3, 4, 6], [2, 3, 4, 5, 6], [4], [4], [2, 3, 4, 6]], + eq_eq_ne = 6, + # var to vars that depend on them + var_vardeps = [[1, 2, 3], [1, 2, 3], [3]], + var_var_ne = 3 + ) + # testing when ignoring VariableRateJumps + test_case_2 = (; + # filter out vrjs in making graphs + eqs = filter(x -> !(x isa VariableRateJump), jumps(js)), + # eq to vars they depend on + eq_sdeps = [Variable[], [S], [S, I], [S, R], [I]], + eq_sidepsf = [Int[], [1], [1, 2], [1, 3], [2]], + eq_sidepsb = [[2, 3, 4], [3, 5], [4]], + # eq to params they depend on + eq_pdeps = [[k1], [k1], [k2], [k2], [k1]], + eq_pidepsf = [[1], [1], [2], [2], [1]], + eq_pidepsb = [[1, 2, 5], [3, 4]], + # var to eqs that modify them + s_eqdepsf = [[1, 2, 3], [3], [4, 5]], + s_eqdepsb = [[1], [1], [1, 2], [3], [3]], + var_eq_ne = 6, + # eq to eqs that depend on them + eq_eqdeps = [[2, 3, 4], [2, 3, 4], [2, 3, 4, 5], [4], [4], [2, 3, 4]], + eq_eq_ne = 5, + # var to vars that depend on them + var_vardeps = [[1, 2, 3], [1, 2, 3], [3]], + var_var_ne = 3 + ) + + @testset "Case $i" for (i, test_case) in enumerate([test_case_1, test_case_2]) + (; # filter out vrjs in making graphs + eqs, # eq to vars they depend on + eq_sdeps, + eq_sidepsf, + eq_sidepsb, # eq to params they depend on + eq_pdeps, + eq_pidepsf, + eq_pidepsb, # var to eqs that modify them + s_eqdepsf, + s_eqdepsb, + var_eq_ne, # eq to eqs that depend on them + eq_eqdeps, + eq_eq_ne, # var to vars that depend on them + var_vardeps, + var_var_ne +) = test_case + deps = equation_dependencies(js; eqs) + @test length(deps) == length(eq_sdeps) + @test all([issetequal(a, b) for (a, b) in zip(eq_sdeps, deps)]) + # @test all(i -> ) + # @test all(i -> isequal(Set(eq_sdeps[i]), Set(deps[i])), 1:length(alleqs)) + depsbg = asgraph(js; eqs) + @test depsbg.fadjlist == eq_sidepsf + @test depsbg.badjlist == eq_sidepsb + + deps = equation_dependencies(js; variables = parameters(js), eqs) + @test length(deps) == length(eq_pdeps) + @test all([issetequal(a, b) for (a, b) in zip(eq_pdeps, deps)]) + depsbg2 = asgraph(js; variables = parameters(js), eqs) + @test depsbg2.fadjlist == eq_pidepsf + @test depsbg2.badjlist == eq_pidepsb + + bg = BipartiteGraph(var_eq_ne, s_eqdepsf, s_eqdepsb) + deps2 = variable_dependencies(js; eqs) + @test isequal(bg, deps2) + + dg = SimpleDiGraph(eq_eq_ne) + for (eqidx, eqdeps) in enumerate(eq_eqdeps) + for eqdepidx in eqdeps + add_edge!(dg, eqidx, eqdepidx) + end end - end - dg3 = eqeq_dependencies(depsbg, deps2) - @test dg == dg3 - - # var to vars that depend on them - var_vardeps = [[1, 2, 3], [1, 2, 3], [3]] - ne = 7 - dg = SimpleDiGraph(3) - for (vidx, vdeps) in enumerate(var_vardeps) - for vdepidx in vdeps - add_edge!(dg, vidx, vdepidx) + dg3 = eqeq_dependencies(depsbg, deps2) + @test dg == dg3 + + dg = SimpleDiGraph(var_var_ne) + for (vidx, vdeps) in enumerate(var_vardeps) + for vdepidx in vdeps + add_edge!(dg, vidx, vdepidx) + end end + dg4 = varvar_dependencies(depsbg, deps2) + @test dg == dg4 end - dg4 = varvar_dependencies(depsbg, deps2) - @test dg == dg4 end ##################################### # testing for ODE/SDEs ##################################### -@parameters k1 k2 -@variables S(t) I(t) R(t) -eqs = [D(S) ~ k1 - k1 * S - k2 * S * I - k1 * k2 / (1 + t) * S, - D(I) ~ k2 * S * I, - D(R) ~ -k2 * S^2 * R / 2 + k1 * I + k1 * k2 * S / (1 + t)] -noiseeqs = [S, I, R] -@named os = System(eqs, t, [S, I, R], [k1, k2]) -deps = equation_dependencies(os) -S = value(S); -I = value(I); -R = value(R); -k1 = value(k1); -k2 = value(k2); -eq_sdeps = [[S, I], [S, I], [S, I, R]] -@test all(i -> isequal(Set(eq_sdeps[i]), Set(deps[i])), 1:length(deps)) -@parameters k1 k2 -@variables S(t) I(t) R(t) -@named sdes = SDESystem(eqs, noiseeqs, t, [S, I, R], [k1, k2]) -deps = equation_dependencies(sdes) -@test all(i -> isequal(Set(eq_sdeps[i]), Set(deps[i])), 1:length(deps)) +@testset "ODEs, SDEs" begin + @parameters k1 k2 + @variables S(t) I(t) R(t) + eqs = [D(S) ~ k1 - k1 * S - k2 * S * I - k1 * k2 / (1 + t) * S + D(I) ~ k2 * S * I + D(R) ~ -k2 * S^2 * R / 2 + k1 * I + k1 * k2 * S / (1 + t)] + @named os = System(eqs, t, [S, I, R], [k1, k2]) + deps = equation_dependencies(os) + S = value(S) + I = value(I) + R = value(R) + k1 = value(k1) + k2 = value(k2) + eq_sdeps = [[S, I], [S, I], [S, I, R]] + @test all(i -> isequal(Set(eq_sdeps[i]), Set(deps[i])), 1:length(deps)) + + noiseeqs = [S, I, R] + @named sdes = SDESystem(eqs, noiseeqs, t, [S, I, R], [k1, k2]) + deps = equation_dependencies(sdes) + @test all(i -> isequal(Set(eq_sdeps[i]), Set(deps[i])), 1:length(deps)) -deps = variable_dependencies(os) -s_eqdeps = [[1], [2], [3]] -@test deps.fadjlist == s_eqdeps + deps = variable_dependencies(os) + s_eqdeps = [[1], [2], [3]] + @test deps.fadjlist == s_eqdeps +end ##################################### # testing for nonlin sys ##################################### -@variables x y z -@parameters σ ρ β - -eqs = [0 ~ σ * (y - x), - 0 ~ ρ - y, - 0 ~ y - β * z] -@named ns = System(eqs, [x, y, z], [σ, ρ, β]) -deps = equation_dependencies(ns) -eq_sdeps = [[x, y], [y], [y, z]] -@test all(i -> isequal(Set(deps[i]), Set(value.(eq_sdeps[i]))), 1:length(deps)) +@testset "Nonlinear" begin + @variables x y z + @parameters σ ρ β + + eqs = [0 ~ σ * (y - x), + 0 ~ ρ - y, + 0 ~ y - β * z] + @named ns = System(eqs, [x, y, z], [σ, ρ, β]) + deps = equation_dependencies(ns) + eq_sdeps = [[x, y], [y], [y, z]] + @test all(i -> isequal(Set(deps[i]), Set(value.(eq_sdeps[i]))), 1:length(deps)) +end From f82ff93c0ad66387cdb47cd263894b29b91aedff Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 29 Apr 2025 21:22:29 +0530 Subject: [PATCH 1725/2176] test: fix usage of `ODEProblemExpr` in lowering test --- test/lowering_solving.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/lowering_solving.jl b/test/lowering_solving.jl index 8c4db26287..280848f6ad 100644 --- a/test/lowering_solving.jl +++ b/test/lowering_solving.jl @@ -33,7 +33,7 @@ tspan = (0.0, 100.0) sys = complete(sys) prob = ODEProblem(sys, u0, tspan, p, jac = true) -probexpr = ODEProblemExpr(sys, u0, tspan, p, jac = true) +probexpr = ODEProblem(sys, u0, tspan, p; jac = true, expression = Val{true}) sol = solve(prob, Tsit5()) solexpr = solve(eval(prob), Tsit5()) @test all(x -> x == 0, Array(sol - solexpr)) From b55e28f5b645d93e9c89c7bbeffdc29c44788ae3 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 29 Apr 2025 21:22:44 +0530 Subject: [PATCH 1726/2176] test: remove test for specifying type of system in `@mtkmodel` --- test/model_parsing.jl | 15 --------------- 1 file changed, 15 deletions(-) diff --git a/test/model_parsing.jl b/test/model_parsing.jl index 7271d9bd1d..705ec79816 100644 --- a/test/model_parsing.jl +++ b/test/model_parsing.jl @@ -1012,21 +1012,6 @@ end end end -@testset "Specify the type of system" begin - @mtkmodel Float2Bool::DiscreteSystem begin - @variables begin - u(t)::Float64 - y(t)::Bool - end - @equations begin - y ~ u != 0 - end - end - - @named sys = Float2Bool() - @test typeof(sys) == DiscreteSystem -end - @testset "Constraints, costs, consolidate" begin @mtkmodel Example begin @variables begin From d5fd328c3b14caba394beb24a7aeb13f95b03999 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 29 Apr 2025 21:23:05 +0530 Subject: [PATCH 1727/2176] test: fix parameter dependencies test --- test/parameter_dependencies.jl | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/test/parameter_dependencies.jl b/test/parameter_dependencies.jl index 71ca78a101..624dbe8b6b 100644 --- a/test/parameter_dependencies.jl +++ b/test/parameter_dependencies.jl @@ -78,14 +78,14 @@ end t ) @named sys2 = System( - [], + Equation[], t; parameter_dependencies = [p2 => 2p1] ) sys = extend(sys2, sys1) @test !(p2 in Set(parameters(sys))) @test p2 in Set(full_parameters(sys)) - prob = ODEProblem(complete(sys)) + prob = ODEProblem(complete(sys), nothing, (0.0, 1.0)) get_dep = getu(prob, 2p2) @test get_dep(prob) == 4 end @@ -99,7 +99,7 @@ end t; parameter_dependencies = [p2 => 2p1] ) - prob = ODEProblem(complete(sys)) + prob = ODEProblem(complete(sys), nothing, (0.0, 1.0)) get_dep = getu(prob, 2p2) @test get_dep(prob) == 4 end @@ -131,9 +131,9 @@ end t; parameter_dependencies = [p2 => 2p1] ) - sys = complete(System([], t, systems = [sys1, sys2], name = :sys)) + sys = complete(System(Equation[], t, systems = [sys1, sys2], name = :sys)) - prob = ODEProblem(sys) + prob = ODEProblem(sys, [], (0.0, 1.0)) v1 = sys.sys2.p2 v2 = 2 * v1 @test is_observed(prob, v1) From ce50a4d3f3b8d25617450a5585b5b377c4937e28 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 29 Apr 2025 21:23:14 +0530 Subject: [PATCH 1728/2176] test: fix symbolic events test --- test/symbolic_events.jl | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/test/symbolic_events.jl b/test/symbolic_events.jl index 35a5369869..1d611360a3 100644 --- a/test/symbolic_events.jl +++ b/test/symbolic_events.jl @@ -12,6 +12,10 @@ using SymbolicIndexingInterface using Setfield rng = StableRNG(12345) +function get_callback(prob) + prob.kwargs[:callback] +end + @variables x(t) = 0 eqs = [D(x) ~ 1] @@ -555,8 +559,7 @@ end function testsol(jsys, u0, p, tspan; tstops = Float64[], paramtotest = nothing, N = 40000, kwargs...) jsys = complete(jsys) - dprob = DiscreteProblem(jsys, u0, tspan, p) - jprob = JumpProblem(jsys, dprob, Direct(); kwargs...) + jprob = JumpProblem(jsys, u0, tspan, p; aggregator = Direct(), kwargs...) sol = solve(jprob, SSAStepper(); tstops = tstops) @test (sol(1.000000000001)[1] - sol(0.99999999999)[1]) == 1 paramtotest === nothing || (@test sol.ps[paramtotest] == [0.0, 1.0]) @@ -1225,13 +1228,13 @@ end @named wd1 = weird1(0.021) @named wd2 = weird2(0.021) - sys1 = structural_simplify(System([], t; name = :parent, + sys1 = structural_simplify(System(Equation[], t; name = :parent, discrete_events = [0.01 => ModelingToolkit.ImperativeAffect( modified = (; θs = reduce(vcat, [[wd1.θ]])), ctx = [1]) do m, o, c, i @set! m.θs[1] = c[] += 1 end], systems = [wd1])) - sys2 = structural_simplify(System([], t; name = :parent, + sys2 = structural_simplify(System(Equation[], t; name = :parent, discrete_events = [0.01 => ModelingToolkit.ImperativeAffect( modified = (; θs = reduce(vcat, [[wd2.θ]])), ctx = [1]) do m, o, c, i @set! m.θs[1] = c[] += 1 From 9788f3ed97f240cf9c75ab78561286e9c8c54f2e Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 29 Apr 2025 22:44:18 +0530 Subject: [PATCH 1729/2176] test: fix modelingtoolkitize test --- test/modelingtoolkitize.jl | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/test/modelingtoolkitize.jl b/test/modelingtoolkitize.jl index 04e6263c7c..0157a50acb 100644 --- a/test/modelingtoolkitize.jl +++ b/test/modelingtoolkitize.jl @@ -253,7 +253,6 @@ prob = ODEProblem(ode_prob_dict, u0, (0.0, 1.0), params) sys = modelingtoolkitize(prob) @test [ModelingToolkit.defaults(sys)[s] for s in unknowns(sys)] == u0 @test [ModelingToolkit.defaults(sys)[s] for s in parameters(sys)] == [10, 20] -@test ModelingToolkit.has_tspan(sys) @parameters sig=10 rho=28.0 beta=8 / 3 @variables x(t)=100 y(t)=1.0 z(t)=1 @@ -266,10 +265,9 @@ noiseeqs = [0.1 * x, 0.1 * y, 0.1 * z] -@named sys = SDESystem(eqs, noiseeqs, t, [x, y, z], [sig, rho, beta]; tspan = (0, 1000.0)) -prob = SDEProblem(complete(sys)) +@named sys = SDESystem(eqs, noiseeqs, t, [x, y, z], [sig, rho, beta]) +prob = SDEProblem(complete(sys), nothing, (0.0, 1.0)) sys = modelingtoolkitize(prob) -@test ModelingToolkit.has_tspan(sys) @testset "Explicit variable names" begin function fn(du, u, p::NamedTuple, t) From e2a94aa7c94f422e193d73671b946b13cfe40b5a Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 1 May 2025 13:56:50 +0530 Subject: [PATCH 1730/2176] test: remove outdated test We allow time-dependent parameter derivatives now --- test/odesystem.jl | 8 -------- 1 file changed, 8 deletions(-) diff --git a/test/odesystem.jl b/test/odesystem.jl index defef0bbaa..8b24691fea 100644 --- a/test/odesystem.jl +++ b/test/odesystem.jl @@ -851,14 +851,6 @@ let @test string.(independent_variables(prob.f.sys)) == ["t"] end -let - @parameters P(t) Q(t) - ∂t = D - eqs = [∂t(Q) ~ 0.2P - ∂t(P) ~ -80.0sin(Q)] - @test_throws ArgumentError @named sys = System(eqs, t) -end - @parameters C L R @variables q(t) p(t) F(t) From 63feb394d134a2c8306a5e29dd9a5feb3935d801 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 1 May 2025 15:55:30 +0530 Subject: [PATCH 1731/2176] refactor: remove old `modelingtoolkitize(::OptimizationProblem)` --- .../optimization/modelingtoolkitize.jl | 148 ------------------ 1 file changed, 148 deletions(-) delete mode 100644 src/systems/optimization/modelingtoolkitize.jl diff --git a/src/systems/optimization/modelingtoolkitize.jl b/src/systems/optimization/modelingtoolkitize.jl deleted file mode 100644 index 27dccc251a..0000000000 --- a/src/systems/optimization/modelingtoolkitize.jl +++ /dev/null @@ -1,148 +0,0 @@ -""" -$(TYPEDSIGNATURES) - -Generate `OptimizationSystem`, dependent variables, and parameters from an `OptimizationProblem`. -""" -function modelingtoolkitize(prob::DiffEqBase.OptimizationProblem; - u_names = nothing, p_names = nothing, kwargs...) - num_cons = isnothing(prob.lcons) ? 0 : length(prob.lcons) - if prob.p isa Tuple || prob.p isa NamedTuple - p = [x for x in prob.p] - else - p = prob.p - end - has_p = !(p isa Union{DiffEqBase.NullParameters, Nothing}) - if u_names !== nothing - varnames_length_check(prob.u0, u_names; is_unknowns = true) - _vars = [variable(name) for name in u_names] - elseif SciMLBase.has_sys(prob.f) - varnames = getname.(variable_symbols(prob.f.sys)) - varidxs = variable_index.((prob.f.sys,), varnames) - invpermute!(varnames, varidxs) - _vars = [variable(name) for name in varnames] - if prob.f.sys isa OptimizationSystem - for (i, sym) in enumerate(variable_symbols(prob.f.sys)) - if hasbounds(sym) - _vars[i] = Symbolics.setmetadata( - _vars[i], VariableBounds, getbounds(sym)) - end - end - end - else - _vars = [variable(:x, i) for i in eachindex(prob.u0)] - end - _vars = reshape(_vars, size(prob.u0)) - vars = ArrayInterface.restructure(prob.u0, _vars) - if prob.ub !== nothing # lb is also !== nothing - vars = map(vars, prob.lb, prob.ub) do sym, lb, ub - if iszero(lb) && iszero(ub) || isinf(lb) && lb < 0 && isinf(ub) && ub > 0 - sym - else - Symbolics.setmetadata(sym, VariableBounds, (lb, ub)) - end - end - end - params = if has_p - if p_names === nothing && SciMLBase.has_sys(prob.f) - p_names = Dict(parameter_index(prob.f.sys, sym) => sym - for sym in parameter_symbols(prob.f.sys)) - end - if p isa MTKParameters - old_to_new = Dict() - for sym in parameter_symbols(prob) - idx = parameter_index(prob, sym) - old_to_new[unwrap(sym)] = unwrap(p_names[idx]) - end - order = reorder_parameters(prob.f.sys) - for arr in order - for i in eachindex(arr) - arr[i] = old_to_new[arr[i]] - end - end - _params = order - else - _params = define_params(p, p_names) - end - p isa Number ? _params[1] : - (p isa Tuple || p isa NamedTuple || p isa AbstractDict || p isa MTKParameters ? - _params : - ArrayInterface.restructure(p, _params)) - else - [] - end - - if p isa MTKParameters - eqs = prob.f(vars, params...) - else - eqs = prob.f(vars, params) - end - - if DiffEqBase.isinplace(prob) && !isnothing(prob.f.cons) - lhs = Array{Num}(undef, num_cons) - if p isa MTKParameters - prob.f.cons(lhs, vars, params...) - else - prob.f.cons(lhs, vars, params) - end - cons = Union{Equation, Inequality}[] - - if !isnothing(prob.lcons) - for i in 1:num_cons - if !isinf(prob.lcons[i]) - if prob.lcons[i] != prob.ucons[i] - push!(cons, prob.lcons[i] ≲ lhs[i]) - else - push!(cons, lhs[i] ~ prob.ucons[i]) - end - end - end - end - - if !isnothing(prob.ucons) - for i in 1:num_cons - if !isinf(prob.ucons[i]) && prob.lcons[i] != prob.ucons[i] - push!(cons, lhs[i] ≲ prob.ucons[i]) - end - end - end - if (isnothing(prob.lcons) || all(isinf, prob.lcons)) && - (isnothing(prob.ucons) || all(isinf, prob.ucons)) - throw(ArgumentError("Constraints passed have no proper bounds defined. - Ensure you pass equal bounds (the scalar that the constraint should evaluate to) for equality constraints - or pass the lower and upper bounds for inequality constraints.")) - end - elseif !isnothing(prob.f.cons) - cons = p isa MTKParameters ? prob.f.cons(vars, params...) : - prob.f.cons(vars, params) - else - cons = [] - end - params = values(params) - params = if params isa Number || (params isa Array && ndims(params) == 0) - [params[1]] - elseif p isa MTKParameters - reduce(vcat, params) - else - vec(collect(params)) - end - - sts = vec(collect(vars)) - default_u0 = Dict(sts .=> vec(collect(prob.u0))) - default_p = if has_p - if prob.p isa AbstractDict - Dict(v => prob.p[k] for (k, v) in pairs(_params)) - elseif prob.p isa MTKParameters - Dict(params .=> reduce(vcat, prob.p)) - else - Dict(params .=> vec(collect(prob.p))) - end - else - Dict() - end - de = OptimizationSystem(eqs, sts, params; - name = gensym(:MTKizedOpt), - constraints = cons, - defaults = merge(default_u0, default_p), - kwargs...) - de -end From a70615038069b76a893e72161df03ea263cec260 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 1 May 2025 15:55:40 +0530 Subject: [PATCH 1732/2176] feat: add `modelingtoolkitize(::OptimizationProblem)` --- src/ModelingToolkit.jl | 3 +- src/modelingtoolkitize/optimizationproblem.jl | 95 +++++++++++++++++++ 2 files changed, 96 insertions(+), 2 deletions(-) create mode 100644 src/modelingtoolkitize/optimizationproblem.jl diff --git a/src/ModelingToolkit.jl b/src/ModelingToolkit.jl index c22d78f30f..c4909524c4 100644 --- a/src/ModelingToolkit.jl +++ b/src/ModelingToolkit.jl @@ -185,8 +185,7 @@ include("problems/bvproblem.jl") include("modelingtoolkitize/common.jl") include("modelingtoolkitize/odeproblem.jl") include("modelingtoolkitize/sdeproblem.jl") - -include("systems/optimization/modelingtoolkitize.jl") +include("modelingtoolkitize/optimizationproblem.jl") include("systems/nonlinear/homotopy_continuation.jl") include("systems/nonlinear/modelingtoolkitize.jl") diff --git a/src/modelingtoolkitize/optimizationproblem.jl b/src/modelingtoolkitize/optimizationproblem.jl new file mode 100644 index 0000000000..26d557152a --- /dev/null +++ b/src/modelingtoolkitize/optimizationproblem.jl @@ -0,0 +1,95 @@ +""" + $(TYPEDSIGNATURES) + +Convert an `OptimizationProblem` to a `ModelingToolkit.System`. + +# Keyword arguments + +- `u_names`: An array of names of the same size as `prob.u0` to use as the names of the + unknowns of the system. The names should be given as `Symbol`s. +- `p_names`: A collection of names to use for parameters of the system. The collection + should have keys corresponding to indexes of `prob.p`. For example, if `prob.p` is an + associative container like `NamedTuple`, then `p_names` should map keys of `prob.p` to + the name that the corresponding parameter should have in the returned system. The names + should be given as `Symbol`s. + +All other keyword arguments are forwarded to the created `System`. +""" +function modelingtoolkitize( + prob::OptimizationProblem; u_names = nothing, p_names = nothing, + kwargs...) + num_cons = isnothing(prob.lcons) ? 0 : length(prob.lcons) + p = prob.p + has_p = !(p isa Union{DiffEqBase.NullParameters, Nothing}) + + vars = construct_vars(prob, nothing, u_names) + if prob.ub !== nothing # lb is also !== nothing + vars = map(vars, prob.lb, prob.ub) do sym, lb, ub + if iszero(lb) && iszero(ub) || isinf(lb) && lb < 0 && isinf(ub) && ub > 0 + sym + else + Symbolics.setmetadata(sym, VariableBounds, (lb, ub)) + end + end + end + params = construct_params(prob, nothing, p_names) + + objective = prob.f(vars, params) + + if prob.f.cons === nothing + cons = [] + else + if DiffEqBase.isinplace(prob.f) + lhs = Array{Num}(undef, num_cons) + prob.f.cons(lhs, vars, params) + else + lhs = prob.f.cons(vars, params) + end + cons = Union{Equation, Inequality}[] + + if !isnothing(prob.lcons) + for i in 1:num_cons + if !isinf(prob.lcons[i]) + if prob.lcons[i] != prob.ucons[i] + push!(cons, prob.lcons[i] ≲ lhs[i]) + else + push!(cons, lhs[i] ~ prob.ucons[i]) + end + end + end + end + + if !isnothing(prob.ucons) + for i in 1:num_cons + if !isinf(prob.ucons[i]) && prob.lcons[i] != prob.ucons[i] + push!(cons, lhs[i] ≲ prob.ucons[i]) + end + end + end + + if (isnothing(prob.lcons) || all(isinf, prob.lcons)) && + (isnothing(prob.ucons) || all(isinf, prob.ucons)) + throw(ArgumentError("Constraints passed have no proper bounds defined. + Ensure you pass equal bounds (the scalar that the constraint should evaluate to) for equality constraints + or pass the lower and upper bounds for inequality constraints.")) + end + end + + # turn `params` into a list of symbolic variables as opposed to + # a parameter object containing symbolic variables. + _params = params + params = to_paramvec(params) + + defaults = defaults_from_u0_p(prob, vars, _params, params) + # In case initials crept in, specifically from when we constructed parameters + # using prob.f.sys + filter!(x -> !iscall(x) || !(operation(x) isa Initial), params) + filter!(x -> !iscall(x[1]) || !(operation(x[1]) isa Initial), defaults) + + sts = vec(collect(vars)) + sys = OptimizationSystem(objective, sts, params; + defaults, + constraints = cons, + name = gensym(:MTKizedOpt), + kwargs...) +end From 72fd93090e6f0a2e586bb1a0c47e2bb41c281d5e Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 1 May 2025 16:10:57 +0530 Subject: [PATCH 1733/2176] refactor: remove old `modelingtoolkitize(::NonlinearProblem)` --- src/ModelingToolkit.jl | 1 - src/systems/nonlinear/modelingtoolkitize.jl | 85 --------------------- 2 files changed, 86 deletions(-) delete mode 100644 src/systems/nonlinear/modelingtoolkitize.jl diff --git a/src/ModelingToolkit.jl b/src/ModelingToolkit.jl index c4909524c4..cd79a0a553 100644 --- a/src/ModelingToolkit.jl +++ b/src/ModelingToolkit.jl @@ -188,7 +188,6 @@ include("modelingtoolkitize/sdeproblem.jl") include("modelingtoolkitize/optimizationproblem.jl") include("systems/nonlinear/homotopy_continuation.jl") -include("systems/nonlinear/modelingtoolkitize.jl") include("systems/nonlinear/initializesystem.jl") include("systems/diffeqs/first_order_transform.jl") include("systems/diffeqs/basic_transformations.jl") diff --git a/src/systems/nonlinear/modelingtoolkitize.jl b/src/systems/nonlinear/modelingtoolkitize.jl deleted file mode 100644 index 4ce3769ef7..0000000000 --- a/src/systems/nonlinear/modelingtoolkitize.jl +++ /dev/null @@ -1,85 +0,0 @@ -""" -$(TYPEDSIGNATURES) - -Generate `System`, dependent variables, and parameters from an `NonlinearProblem`. -""" -function modelingtoolkitize( - prob::Union{NonlinearProblem, NonlinearLeastSquaresProblem}; - u_names = nothing, p_names = nothing, kwargs...) - p = prob.p - has_p = !(p isa Union{DiffEqBase.NullParameters, Nothing}) - - if u_names !== nothing - varnames_length_check(prob.u0, u_names; is_unknowns = true) - _vars = [variable(name) for name in u_names] - elseif SciMLBase.has_sys(prob.f) - varnames = getname.(variable_symbols(prob.f.sys)) - varidxs = variable_index.((prob.f.sys,), varnames) - invpermute!(varnames, varidxs) - _vars = [variable(name) for name in varnames] - else - _vars = [variable(:x, i) for i in eachindex(prob.u0)] - end - _vars = reshape(_vars, size(prob.u0)) - - vars = prob.u0 isa Number ? _vars : ArrayInterface.restructure(prob.u0, _vars) - params = if has_p - if p_names === nothing && SciMLBase.has_sys(prob.f) - p_names = Dict(parameter_index(prob.f.sys, sym) => sym - for sym in parameter_symbols(prob.f.sys)) - end - _params = define_params(p, p_names) - p isa Number ? _params[1] : - (p isa Tuple || p isa NamedTuple || p isa AbstractDict || p isa MTKParameters ? - _params : - ArrayInterface.restructure(p, _params)) - else - [] - end - - if DiffEqBase.isinplace(prob) - if prob isa NonlinearLeastSquaresProblem - rhs = ArrayInterface.restructure( - prob.f.resid_prototype, similar(prob.f.resid_prototype, Num)) - prob.f(rhs, vars, params) - eqs = vcat([0.0 ~ rhs[i] for i in 1:length(prob.f.resid_prototype)]...) - else - rhs = ArrayInterface.restructure(prob.u0, similar(vars, Num)) - prob.f(rhs, vars, params) - eqs = vcat([0.0 ~ rhs[i] for i in 1:length(rhs)]...) - end - - else - rhs = prob.f(vars, params) - out_def = prob.f(prob.u0, prob.p) - eqs = vcat([0.0 ~ rhs[i] for i in 1:length(out_def)]...) - end - - sts = vec(collect(vars)) - _params = params - params = values(params) - params = if params isa Number || (params isa Array && ndims(params) == 0) - [params[1]] - else - vec(collect(params)) - end - default_u0 = Dict(sts .=> vec(collect(prob.u0))) - default_p = if has_p - if prob.p isa AbstractDict - Dict(v => prob.p[k] for (k, v) in pairs(_params)) - elseif prob.p isa MTKParameters - Dict(params .=> reduce(vcat, prob.p)) - else - Dict(params .=> vec(collect(prob.p))) - end - else - Dict() - end - - de = System(eqs, sts, params, - defaults = merge(default_u0, default_p); - name = gensym(:MTKizedNonlinProb), - kwargs...) - - de -end From e7a96b60924cef3a3e01d458255c085d5e25b289 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 1 May 2025 16:11:07 +0530 Subject: [PATCH 1734/2176] feat: add `modelingtoolkitize(::NonlinearProblem)` --- src/ModelingToolkit.jl | 1 + src/modelingtoolkitize/common.jl | 8 +++- src/modelingtoolkitize/nonlinearproblem.jl | 48 ++++++++++++++++++++++ 3 files changed, 55 insertions(+), 2 deletions(-) create mode 100644 src/modelingtoolkitize/nonlinearproblem.jl diff --git a/src/ModelingToolkit.jl b/src/ModelingToolkit.jl index cd79a0a553..7f228eabbc 100644 --- a/src/ModelingToolkit.jl +++ b/src/ModelingToolkit.jl @@ -186,6 +186,7 @@ include("modelingtoolkitize/common.jl") include("modelingtoolkitize/odeproblem.jl") include("modelingtoolkitize/sdeproblem.jl") include("modelingtoolkitize/optimizationproblem.jl") +include("modelingtoolkitize/nonlinearproblem.jl") include("systems/nonlinear/homotopy_continuation.jl") include("systems/nonlinear/initializesystem.jl") diff --git a/src/modelingtoolkitize/common.jl b/src/modelingtoolkitize/common.jl index a3d059a66a..8291fd5710 100644 --- a/src/modelingtoolkitize/common.jl +++ b/src/modelingtoolkitize/common.jl @@ -346,14 +346,18 @@ end Given a problem `prob`, the symbolic unknowns and params and the independent variable, trace through `prob.f` and return the resultant expression. """ -function trace_rhs(prob, vars, params, t) +function trace_rhs(prob, vars, params, t; prototype = nothing) args = (vars, params) if t !== nothing args = (args..., t) end # trace prob.f to get equation RHS if SciMLBase.isinplace(prob.f) - rhs = ArrayInterface.restructure(prob.u0, similar(vars, Num)) + if prototype === nothing + rhs = ArrayInterface.restructure(prob.u0, similar(vars, Num)) + else + rhs = similar(prototype, Num) + end fill!(rhs, 0) if prob.f isa SciMLBase.AbstractSciMLFunction && prob.f.f isa FunctionWrappersWrappers.FunctionWrappersWrapper diff --git a/src/modelingtoolkitize/nonlinearproblem.jl b/src/modelingtoolkitize/nonlinearproblem.jl new file mode 100644 index 0000000000..92425be373 --- /dev/null +++ b/src/modelingtoolkitize/nonlinearproblem.jl @@ -0,0 +1,48 @@ +""" + $(TYPEDSIGNATURES) + +Convert a `NonlinearProblem` or `NonlinearLeastSquaresProblem` to a +`ModelingToolkit.System`. + +# Keyword arguments + +- `u_names`: An array of names of the same size as `prob.u0` to use as the names of the + unknowns of the system. The names should be given as `Symbol`s. +- `p_names`: A collection of names to use for parameters of the system. The collection + should have keys corresponding to indexes of `prob.p`. For example, if `prob.p` is an + associative container like `NamedTuple`, then `p_names` should map keys of `prob.p` to + the name that the corresponding parameter should have in the returned system. The names + should be given as `Symbol`s. + +All other keyword arguments are forwarded to the created `System`. +""" +function modelingtoolkitize( + prob::Union{NonlinearProblem, NonlinearLeastSquaresProblem}; + u_names = nothing, p_names = nothing, kwargs...) + p = prob.p + has_p = !(p isa Union{DiffEqBase.NullParameters, Nothing}) + + vars = construct_vars(prob, nothing, u_names) + params = construct_params(prob, nothing, p_names) + + rhs = trace_rhs(prob, vars, params, nothing; prototype = prob.f.resid_prototype) + eqs = vcat([0 ~ rhs[i] for i in eachindex(rhs)]...) + + sts = vec(collect(vars)) + + # turn `params` into a list of symbolic variables as opposed to + # a parameter object containing symbolic variables. + _params = params + params = to_paramvec(params) + + defaults = defaults_from_u0_p(prob, vars, _params, params) + # In case initials crept in, specifically from when we constructed parameters + # using prob.f.sys + filter!(x -> !iscall(x) || !(operation(x) isa Initial), params) + filter!(x -> !iscall(x[1]) || !(operation(x[1]) isa Initial), defaults) + + return System(eqs, sts, params; + defaults, + name = gensym(:MTKizedNonlin), + kwargs...) +end From 397455066a950dbcf476a3aaf1933d5139c4fa4d Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 1 May 2025 16:12:05 +0530 Subject: [PATCH 1735/2176] refactor: remove old `modelingtoolkitize(::ODEProblem)` and `::SDEProblem` --- src/systems/diffeqs/modelingtoolkitize.jl | 303 ---------------------- 1 file changed, 303 deletions(-) delete mode 100644 src/systems/diffeqs/modelingtoolkitize.jl diff --git a/src/systems/diffeqs/modelingtoolkitize.jl b/src/systems/diffeqs/modelingtoolkitize.jl deleted file mode 100644 index cfa3ac43dd..0000000000 --- a/src/systems/diffeqs/modelingtoolkitize.jl +++ /dev/null @@ -1,303 +0,0 @@ -""" -$(TYPEDSIGNATURES) - -Generate `System`, dependent variables, and parameters from an `ODEProblem`. -""" -function modelingtoolkitize( - prob::DiffEqBase.ODEProblem; u_names = nothing, p_names = nothing, kwargs...) - prob.f isa DiffEqBase.AbstractParameterizedFunction && - return prob.f.sys - t = t_nounits - p = prob.p - has_p = !(p isa Union{DiffEqBase.NullParameters, Nothing}) - - if u_names !== nothing - varnames_length_check(prob.u0, u_names; is_unknowns = true) - _vars = [_defvar(name)(t) for name in u_names] - elseif SciMLBase.has_sys(prob.f) - varnames = getname.(variable_symbols(prob.f.sys)) - varidxs = variable_index.((prob.f.sys,), varnames) - invpermute!(varnames, varidxs) - _vars = [_defvar(name)(t) for name in varnames] - else - _vars = define_vars(prob.u0, t) - end - - vars = prob.u0 isa Number ? _vars : ArrayInterface.restructure(prob.u0, _vars) - params = if has_p - if p_names === nothing && SciMLBase.has_sys(prob.f) - p_names = Dict(parameter_index(prob.f.sys, sym) => sym - for sym in parameter_symbols(prob.f.sys)) - end - _params = define_params(p, p_names) - p isa Number ? _params[1] : - (p isa Tuple || p isa NamedTuple || p isa AbstractDict || p isa MTKParameters ? - _params : - ArrayInterface.restructure(p, _params)) - else - [] - end - - var_set = Set(vars) - - D = D_nounits - mm = prob.f.mass_matrix - - if mm === I - lhs = map(v -> D(v), vars) - else - lhs = map(mm * vars) do v - if iszero(v) - 0 - elseif v in var_set - D(v) - else - error("Non-permutation mass matrix is not supported.") - end - end - end - - if DiffEqBase.isinplace(prob) - rhs = ArrayInterface.restructure(prob.u0, similar(vars, Num)) - fill!(rhs, 0) - if prob.f isa ODEFunction && - prob.f.f isa FunctionWrappersWrappers.FunctionWrappersWrapper - prob.f.f.fw[1].obj[](rhs, vars, params, t) - else - prob.f(rhs, vars, params, t) - end - else - rhs = prob.f(vars, params, t) - end - - eqs = vcat([lhs[i] ~ rhs[i] for i in eachindex(prob.u0)]...) - - sts = vec(collect(vars)) - - _params = params - params = values(params) - params = if params isa Number || (params isa Array && ndims(params) == 0) - [params[1]] - else - vec(collect(params)) - end - default_u0 = Dict(sts .=> vec(collect(prob.u0))) - default_p = if has_p - if prob.p isa AbstractDict - Dict(v => prob.p[k] for (k, v) in pairs(_params)) - elseif prob.p isa MTKParameters - Dict(params .=> reduce(vcat, prob.p)) - else - Dict(params .=> vec(collect(prob.p))) - end - else - Dict() - end - filter!(x -> !iscall(x) || !(operation(x) isa Initial), params) - filter!(x -> !iscall(x[1]) || !(operation(x[1]) isa Initial), default_p) - de = System(eqs, t, sts, params, - defaults = merge(default_u0, default_p); - name = gensym(:MTKizedODE), - tspan = prob.tspan, - kwargs...) - - de -end - -_defvaridx(x, i) = variable(x, i, T = SymbolicUtils.FnType{Tuple, Real}) -_defvar(x) = variable(x, T = SymbolicUtils.FnType{Tuple, Real}) - -function define_vars(u, t) - [_defvaridx(:x, i)(t) for i in eachindex(u)] -end - -function define_vars(u::NTuple{<:Number}, t) - tuple((_defvaridx(:x, i)(ModelingToolkit.value(t)) for i in eachindex(u))...) -end - -function define_vars(u::NamedTuple, t) - NamedTuple(x => _defvar(x)(ModelingToolkit.value(t)) for x in keys(u)) -end - -const PARAMETERS_NOT_SUPPORTED_MESSAGE = """ - The chosen parameter type is currently not supported by `modelingtoolkitize`. The - current supported types are: - - - AbstractArrays - - AbstractDicts - - LabelledArrays (SLArray, LArray) - - Flat tuples (tuples of numbers) - - Flat named tuples (namedtuples of numbers) - """ - -struct ModelingtoolkitizeParametersNotSupportedError <: Exception - type::Any -end - -function Base.showerror(io::IO, e::ModelingtoolkitizeParametersNotSupportedError) - println(io, PARAMETERS_NOT_SUPPORTED_MESSAGE) - print(io, "Parameter type: ") - println(io, e.type) -end - -function varnames_length_check(vars, names; is_unknowns = false) - if length(names) != length(vars) - throw(ArgumentError(""" - Number of $(is_unknowns ? "unknowns" : "parameters") ($(length(vars))) \ - does not match number of names ($(length(names))). - """)) - end -end - -function define_params(p, _ = nothing) - throw(ModelingtoolkitizeParametersNotSupportedError(typeof(p))) -end - -function define_params(p::AbstractArray, names = nothing) - if names === nothing - [toparam(variable(:α, i)) for i in eachindex(p)] - else - varnames_length_check(p, names) - [toparam(variable(names[i])) for i in eachindex(p)] - end -end - -function define_params(p::Number, names = nothing) - if names === nothing - [toparam(variable(:α))] - elseif names isa Union{AbstractArray, AbstractDict} - varnames_length_check(p, names) - [toparam(variable(names[i])) for i in eachindex(p)] - else - [toparam(variable(names))] - end -end - -function define_params(p::AbstractDict, names = nothing) - if names === nothing - OrderedDict(k => toparam(variable(:α, i)) for (i, k) in zip(1:length(p), keys(p))) - else - varnames_length_check(p, names) - OrderedDict(k => toparam(variable(names[k])) for k in keys(p)) - end -end - -function define_params(p::Tuple, names = nothing) - if names === nothing - tuple((toparam(variable(:α, i)) for i in eachindex(p))...) - else - varnames_length_check(p, names) - tuple((toparam(variable(names[i])) for i in eachindex(p))...) - end -end - -function define_params(p::NamedTuple, names = nothing) - if names === nothing - NamedTuple(x => toparam(variable(x)) for x in keys(p)) - else - varnames_length_check(p, names) - NamedTuple(x => toparam(variable(names[x])) for x in keys(p)) - end -end - -function define_params(p::MTKParameters, names = nothing) - if names === nothing - bufs = (p...,) - i = 1 - ps = [] - for buf in bufs - for _ in buf - push!( - ps, - toparam(variable(:α, i)) - ) - end - end - return identity.(ps) - else - new_p = as_any_buffer(p) - for (k, v) in names - new_p[k] = v - end - return reduce(vcat, new_p; init = []) - end -end - -""" -$(TYPEDSIGNATURES) - -Generate `System`, dependent variables, and parameters from an `SDEProblem`. -""" -function modelingtoolkitize(prob::DiffEqBase.SDEProblem; kwargs...) - prob.f isa DiffEqBase.AbstractParameterizedFunction && - return (prob.f.sys, prob.f.sys.unknowns, prob.f.sys.ps) - @independent_variables t - p = prob.p - has_p = !(p isa Union{DiffEqBase.NullParameters, Nothing}) - - _vars = define_vars(prob.u0, t) - - vars = prob.u0 isa Number ? _vars : ArrayInterface.restructure(prob.u0, _vars) - params = if has_p - _params = define_params(p) - p isa MTKParameters ? _params : - p isa Number ? _params[1] : - (p isa Tuple || p isa NamedTuple ? _params : - ArrayInterface.restructure(p, _params)) - else - [] - end - - D = Differential(t) - - rhs = [D(var) for var in vars] - - if DiffEqBase.isinplace(prob) - lhs = similar(vars, Any) - - prob.f(lhs, vars, params, t) - - if DiffEqBase.is_diagonal_noise(prob) - neqs = similar(vars, Any) - prob.g(neqs, vars, params, t) - else - neqs = similar(vars, Any, size(prob.noise_rate_prototype)) - prob.g(neqs, vars, params, t) - end - else - lhs = prob.f(vars, params, t) - if DiffEqBase.is_diagonal_noise(prob) - neqs = prob.g(vars, params, t) - else - neqs = prob.g(vars, params, t) - end - end - deqs = vcat([rhs[i] ~ lhs[i] for i in eachindex(prob.u0)]...) - - params = if ndims(params) == 0 - [params[1]] - else - Vector(vec(params)) - end - sts = Vector(vec(vars)) - default_u0 = Dict(sts .=> vec(collect(prob.u0))) - default_p = if has_p - if prob.p isa AbstractDict - Dict(v => prob.p[k] for (k, v) in pairs(_params)) - elseif prob.p isa MTKParameters - Dict(params .=> reduce(vcat, prob.p)) - else - Dict(params .=> vec(collect(prob.p))) - end - else - Dict() - end - - de = System(deqs, t, sts, params; noise_eqs = neqs, - name = gensym(:MTKizedSDE), - tspan = prob.tspan, - defaults = merge(default_u0, default_p), - kwargs...) - - de -end From 153aa736becc1edbffbc0b2f24dde8509cc10b9d Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 2 May 2025 17:30:46 +0530 Subject: [PATCH 1736/2176] feat: add `structural_simplify` for optimization systems --- src/systems/systems.jl | 46 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/src/systems/systems.jl b/src/systems/systems.jl index 44a31ab921..8d61614a29 100644 --- a/src/systems/systems.jl +++ b/src/systems/systems.jl @@ -60,6 +60,9 @@ function __structural_simplify(sys::AbstractSystem; simplify = false, if has_noise_eqs(sys) && get_noise_eqs(sys) !== nothing sys = noise_to_brownians(sys; names = :αₘₜₖ) end + if isempty(equations(sys)) && !is_time_dependent(sys) && !_iszero(cost(sys)) + return simplify_optimization_system(sys; kwargs..., sort_eqs, simplify) + end sys = expand_connections(sys) state = TearingState(sys; sort_eqs) @@ -152,6 +155,49 @@ function __structural_simplify(sys::AbstractSystem; simplify = false, end end +function simplify_optimization_system(sys::System; split = true, kwargs...) + sys = flatten(sys) + cons = constraints(sys) + econs = Equation[] + icons = similar(cons, 0) + for e in cons + if e isa Equation + push!(econs, e) + else + push!(icons, e) + end + end + irreducible_subs = Dict() + dvs = mapreduce(Symbolics.scalarize, vcat, unknowns(sys)) + if !(dvs isa Array) + dvs = [dvs] + end + for i in eachindex(dvs) + var = dvs[i] + if hasbounds(var) + irreducible_subs[var] = irrvar = setirreducible(var, true) + dvs[i] = irrvar + end + end + econs = fast_substitute.(econs, (irreducible_subs,)) + nlsys = System(econs, dvs, parameters(sys); name = :___tmp_nlsystem) + snlsys = structural_simplify(nlsys; kwargs..., fully_determined = false) + obs = observed(snlsys) + subs = Dict(eq.lhs => eq.rhs for eq in observed(snlsys)) + seqs = equations(snlsys) + cons_simplified = similar(cons, length(icons) + length(seqs)) + for (i, eq) in enumerate(Iterators.flatten((seqs, icons))) + cons_simplified[i] = fixpoint_sub(eq, subs) + end + newsts = setdiff(dvs, keys(subs)) + @set! sys.constraints = cons_simplified + @set! sys.observed = [observed(sys); obs] + newcost = fixpoint_sub.(get_costs(sys), (subs,)) + @set! sys.costs = newcost + @set! sys.unknowns = newsts + return sys +end + function __num_isdiag_noise(mat) for i in axes(mat, 1) nnz = 0 From 9602a8196c93002541fd638b6160f5f549d63647 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 7 May 2025 13:41:04 +0530 Subject: [PATCH 1737/2176] fix: change default `consolidate` to `default_consolidate` in `@mtkmodel` --- src/systems/model_parsing.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/systems/model_parsing.jl b/src/systems/model_parsing.jl index fdb26ba7ec..8fe07f7f99 100644 --- a/src/systems/model_parsing.jl +++ b/src/systems/model_parsing.jl @@ -121,7 +121,7 @@ function _model_macro(mod, fullname::Union{Expr, Symbol}, expr, isconnector) gui_metadata = isassigned(icon) > 0 ? GUIMetadata(GlobalRef(mod, name), icon[]) : GUIMetadata(GlobalRef(mod, name)) - consolidate = get(dict, :consolidate, nothing) + consolidate = get(dict, :consolidate, default_consolidate) description = get(dict, :description, "") @inline pop_structure_dict!.( From 9d8df2a3759f884b7e7a17e7871685dc224f72be Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 8 May 2025 18:23:59 +0530 Subject: [PATCH 1738/2176] fix: handle `Shift`s in `is_diff_equation` --- src/systems/abstractsystem.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index 23b4859484..04d520838b 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -3047,9 +3047,9 @@ is_diff_equation(eq2) # false """ function is_diff_equation(eq) (eq isa Equation) || (return false) - isdefined(eq, :lhs) && hasnode(is_derivative, wrap(eq.lhs)) && + isdefined(eq, :lhs) && recursive_hasoperator(Union{Differential, Shift}, eq.lhs) && (return true) - isdefined(eq, :rhs) && hasnode(is_derivative, wrap(eq.rhs)) && + isdefined(eq, :rhs) && recursive_hasoperator(Union{Differential, Shift}, eq.rhs) && (return true) return false end From f19daed361a490e118ac9377a29034aa5328ae65 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 8 May 2025 18:25:39 +0530 Subject: [PATCH 1739/2176] feat: implement `calculate_hessian` and `hessian_sparsity` for `System` --- src/systems/codegen.jl | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/systems/codegen.jl b/src/systems/codegen.jl index 1ca879322a..6f035670e6 100644 --- a/src/systems/codegen.jl +++ b/src/systems/codegen.jl @@ -201,6 +201,25 @@ function generate_tgrad( expression, wrap_gfw, (2, 3, is_split(sys)), res; eval_expression, eval_module) end +function calculate_hessian(sys::System; simplify = false, sparse = false) + rhs = [eq.rhs - eq.lhs for eq in full_equations(sys)] + dvs = unknowns(sys) + if sparse + hess = map(rhs) do expr + Symbolics.sparsehessian(expr, dvs; simplify)::AbstractSparseArray + end + else + hess = [Symbolics.hessian(expr, dvs; simplify) for expr in rhs] + end + + return hess +end + +function Symbolics.hessian_sparsity(sys::System) + hess = calculate_hessian(sys; sparse = true) + return similar.(hess, Float64) +end + const W_GAMMA = only(@variables ˍ₋gamma) function generate_W(sys::System, γ = 1.0, dvs = unknowns(sys), From c656c33edb226f3fc560c5af3613aa818dd58e95 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 8 May 2025 18:26:11 +0530 Subject: [PATCH 1740/2176] feat: make delay processing more modular in `build_function_wrapper` --- src/systems/codegen_utils.jl | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/src/systems/codegen_utils.jl b/src/systems/codegen_utils.jl index e7ba2659d7..9ee8a7fd81 100644 --- a/src/systems/codegen_utils.jl +++ b/src/systems/codegen_utils.jl @@ -129,6 +129,7 @@ end The argument of generated functions corresponding to the history function. """ const DDE_HISTORY_FUN = Sym{Symbolics.FnType{Tuple{Any, <:Real}, Vector{Real}}}(:___history___) +const BVP_SOLUTION = Sym{Symbolics.FnType{Tuple{<:Real}, Vector{Real}}}(:__sol__) """ $(TYPEDSIGNATURES) @@ -143,14 +144,15 @@ Turn delayed unknowns in `eqs` into calls to `DDE_HISTORY_FUNCTION`. # Keyword Arguments - `param_arg`: The name of the variable containing the parameter object. +- `histfn`: The history function to use for codegen, called as `histfn(p, t)` """ function delay_to_function( - sys::AbstractSystem, eqs = full_equations(sys); param_arg = MTKPARAMETERS_ARG) + sys::AbstractSystem, eqs = full_equations(sys); param_arg = MTKPARAMETERS_ARG, histfn = DDE_HISTORY_FUN) delay_to_function(eqs, get_iv(sys), Dict{Any, Int}(operation(s) => i for (i, s) in enumerate(unknowns(sys))), parameters(sys), - DDE_HISTORY_FUN; param_arg) + histfn; param_arg) end function delay_to_function(eqs::Vector, iv, sts, ps, h; param_arg = MTKPARAMETERS_ARG) delay_to_function.(eqs, (iv,), (sts,), (ps,), (h,); param_arg) @@ -191,6 +193,10 @@ generated functions, and `args` are the arguments. - `wrap_delays`: Whether to transform delayed unknowns of `sys` present in `expr` into calls to a history function. The history function is added to the list of arguments right before parameters, at the index `p_start`. +- `histfn`: The history function to use for transforming delayed terms. For any delayed + term `x(expr)`, this is called as `histfn(p, expr)` where `p` is the parameter object. +- `histfn_symbolic`: The symbolic history function variable to add as an argument to the + generated function. - `wrap_code`: Forwarded to `build_function`. - `add_observed`: Whether to add assignment statements for observed equations in the generated code. @@ -218,7 +224,7 @@ All other keyword arguments are forwarded to `build_function`. """ function build_function_wrapper(sys::AbstractSystem, expr, args...; p_start = 2, p_end = is_time_dependent(sys) ? length(args) - 1 : length(args), - wrap_delays = is_dde(sys), wrap_code = identity, + wrap_delays = is_dde(sys), histfn = DDE_HISTORY_FUN, histfn_symbolic = histfn, wrap_code = identity, add_observed = true, filter_observed = Returns(true), create_bindings = false, output_type = nothing, mkarray = nothing, wrap_mtkparameters = true, extra_assignments = Assignment[], cse = true, kwargs...) @@ -229,11 +235,11 @@ function build_function_wrapper(sys::AbstractSystem, expr, args...; p_start = 2, if wrap_delays param_arg = is_split(sys) ? MTKPARAMETERS_ARG : generated_argument_name(p_start) obs = map(obs) do eq - delay_to_function(sys, eq; param_arg) + delay_to_function(sys, eq; param_arg, histfn) end - expr = delay_to_function(sys, expr; param_arg) + expr = delay_to_function(sys, expr; param_arg, histfn) # add extra argument - args = (args[1:(p_start - 1)]..., DDE_HISTORY_FUN, args[p_start:end]...) + args = (args[1:(p_start - 1)]..., histfn_symbolic, args[p_start:end]...) p_start += 1 p_end += 1 end From 7102e48a9250d3d3c7ad60b0a05e423082a6a716 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 8 May 2025 18:26:50 +0530 Subject: [PATCH 1741/2176] fix: fix `generate_cost` for time-dependent (BV) systems --- src/systems/codegen.jl | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/src/systems/codegen.jl b/src/systems/codegen.jl index 6f035670e6..064be6fdfb 100644 --- a/src/systems/codegen.jl +++ b/src/systems/codegen.jl @@ -398,13 +398,29 @@ function generate_cost(sys::System; expression = Val{true}, wrap_gfw = Val{false obj = cost(sys) dvs = unknowns(sys) ps = reorder_parameters(sys) - res = build_function_wrapper(sys, obj, dvs, ps...; expression = Val{true}, kwargs...) + + if is_time_dependent(sys) + wrap_delays = true + p_start = 1 + p_end = length(ps) + args = (ps..., get_iv(sys)) + nargs = 3 + else + wrap_delays = false + p_start = 2 + p_end = length(ps) + 1 + args = (dvs, ps...) + nargs = 2 + end + res = build_function_wrapper( + sys, obj, args...; expression = Val{true}, p_start, p_end, wrap_delays, + histfn = (p, t) -> BVP_SOLUTION(t), histfn_symbolic = BVP_SOLUTION, kwargs...) if expression == Val{true} return res end f_oop = eval_or_rgf(res; eval_expression, eval_module) return maybe_compile_function( - expression, wrap_gfw, (2, 2, is_split(sys)), res; eval_expression, eval_module) + expression, wrap_gfw, (2, nargs, is_split(sys)), res; eval_expression, eval_module) end function calculate_cost_gradient(sys::System; simplify = false) From a654f5e988a99c3fdf3e49feff8694c41ceb59ee Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 8 May 2025 18:30:48 +0530 Subject: [PATCH 1742/2176] fix: default `build_initializeprob` to `supports_initialization(sys)` --- src/systems/problem_utils.jl | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/systems/problem_utils.jl b/src/systems/problem_utils.jl index df59dc201c..912496fc69 100644 --- a/src/systems/problem_utils.jl +++ b/src/systems/problem_utils.jl @@ -1220,7 +1220,8 @@ Keyword arguments: All other keyword arguments are passed as-is to `constructor`. """ function process_SciMLProblem( - constructor, sys::AbstractSystem, u0map, pmap; build_initializeprob = true, + constructor, sys::AbstractSystem, u0map, pmap; + build_initializeprob = supports_initialization(sys), implicit_dae = false, t = nothing, guesses = AnyDict(), warn_initialize_determined = true, initialization_eqs = [], eval_expression = false, eval_module = @__MODULE__, fully_determined = nothing, From 7cad5fef22651be84a09c45b81e8d3cb9c6a0d72 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 8 May 2025 18:32:44 +0530 Subject: [PATCH 1743/2176] fix: disallow simplification of jump systems --- src/systems/systems.jl | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/systems/systems.jl b/src/systems/systems.jl index 8d61614a29..70b3ddcfa7 100644 --- a/src/systems/systems.jl +++ b/src/systems/systems.jl @@ -60,6 +60,9 @@ function __structural_simplify(sys::AbstractSystem; simplify = false, if has_noise_eqs(sys) && get_noise_eqs(sys) !== nothing sys = noise_to_brownians(sys; names = :αₘₜₖ) end + if !isempty(jumps(sys)) + return sys + end if isempty(equations(sys)) && !is_time_dependent(sys) && !_iszero(cost(sys)) return simplify_optimization_system(sys; kwargs..., sort_eqs, simplify) end From 7f6456a9118dbee3ddd1af18da042b42a0fe032a Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 8 May 2025 18:33:18 +0530 Subject: [PATCH 1744/2176] fix: remove usage of `is_scalar_noise` kwarg --- src/systems/systems.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/systems/systems.jl b/src/systems/systems.jl index 70b3ddcfa7..eb9e27d409 100644 --- a/src/systems/systems.jl +++ b/src/systems/systems.jl @@ -150,7 +150,7 @@ function __structural_simplify(sys::AbstractSystem; simplify = false, noise_eqs = StructuralTransformations.tearing_substitute_expr(ode_sys, noise_eqs) ssys = System(Vector{Equation}(full_equations(ode_sys)), get_iv(ode_sys), unknowns(ode_sys), parameters(ode_sys); noise_eqs, - name = nameof(ode_sys), is_scalar_noise, observed = observed(ode_sys), defaults = defaults(sys), + name = nameof(ode_sys), observed = observed(ode_sys), defaults = defaults(sys), parameter_dependencies = parameter_dependencies(sys), assertions = assertions(sys), guesses = guesses(sys), initialization_eqs = initialization_equations(sys), continuous_events = continuous_events(sys), From be1d04b57114cd2481515198a1c99b925777cb6a Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 8 May 2025 18:34:37 +0530 Subject: [PATCH 1745/2176] feat: inspect jumps in `collect_scoped_vars!` --- src/utils.jl | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/utils.jl b/src/utils.jl index b3df0b957f..efa6196af8 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -546,6 +546,12 @@ function collect_scoped_vars!(unknowns, parameters, sys, iv; depth = 1, op = Dif collect_vars!(unknowns, parameters, eq, iv; depth, op) end end + if has_jumps(sys) + for eq in jumps(sys) + eqtype_supports_collect_vars(eq) || continue + collect_vars!(unknowns, parameters, eq, iv; depth, op) + end + end if has_parameter_dependencies(sys) for eq in parameter_dependencies(sys) if eq isa Pair From 854a3400eda0870f8e14cc277822acc1d63f048d Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 8 May 2025 18:35:00 +0530 Subject: [PATCH 1746/2176] test: comment out jac/tgrad caching test --- test/structural_transformation/tearing.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/structural_transformation/tearing.jl b/test/structural_transformation/tearing.jl index 0cbabbfa3b..e91f3fa988 100644 --- a/test/structural_transformation/tearing.jl +++ b/test/structural_transformation/tearing.jl @@ -197,8 +197,8 @@ u0 = [mass.s => 0.0 mass.v => 1.0] sys = structural_simplify(ms_model) -@test ModelingToolkit.get_jac(sys)[] === ModelingToolkit.EMPTY_JAC -@test ModelingToolkit.get_tgrad(sys)[] === ModelingToolkit.EMPTY_TGRAD +# @test ModelingToolkit.get_jac(sys)[] === ModelingToolkit.EMPTY_JAC +# @test ModelingToolkit.get_tgrad(sys)[] === ModelingToolkit.EMPTY_TGRAD prob_complex = ODEProblem(sys, u0, (0, 1.0)) sol = solve(prob_complex, Tsit5()) @test all(sol[mass.v] .== 1) From de3b7e8154828b4a3c7d2c3ce52f8a0a94254fda Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 8 May 2025 18:38:02 +0530 Subject: [PATCH 1747/2176] test: update BVProblem tests to new semantics --- test/bvproblem.jl | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/test/bvproblem.jl b/test/bvproblem.jl index dc5f1ef658..0931a0ab6d 100644 --- a/test/bvproblem.jl +++ b/test/bvproblem.jl @@ -285,26 +285,28 @@ end u0map = [x(t) => 4.0, y(t) => 2.0] parammap = [α => 7.5, β => 4, γ => 8.0, δ => 5.0] costs = [x(0.6), x(0.3)^2] - consolidate(u) = (u[1] + 3)^2 + u[2] + consolidate(u, sub) = (u[1] + 3)^2 + u[2] + sum(sub; init = 0) @mtkbuild lksys = System(eqs, t; costs, consolidate) - @test_throws ErrorException @mtkbuild lksys2 = System(eqs, t; costs) - @test_throws ErrorException ODEProblem(lksys, u0map, tspan, parammap) - prob = ODEProblem(lksys, u0map, tspan, parammap; allow_cost = true) + @test_throws ModelingToolkit.SystemCompatibilityError ODEProblem( + lksys, u0map, tspan, parammap) + prob = ODEProblem(lksys, u0map, tspan, parammap; check_compatibility = false) sol = solve(prob, Tsit5()) - costfn = ModelingToolkit.generate_cost_function(lksys) + costfn = ModelingToolkit.generate_cost( + lksys; expression = Val{false}, wrap_gfw = Val{true}) _t = tspan[2] @test costfn(sol, prob.p, _t) ≈ (sol(0.6)[1] + 3)^2 + sol(0.3)[1]^2 ### With a parameter @parameters t_c costs = [y(t_c) + x(0.0), x(0.4)^2] - consolidate(u) = log(u[1]) - u[2] + consolidate(u, sub) = log(u[1]) - u[2] + sum(sub; init = 0) @mtkbuild lksys = System(eqs, t; costs, consolidate) @test t_c ∈ Set(parameters(lksys)) push!(parammap, t_c => 0.56) - prob = ODEProblem(lksys, u0map, tspan, parammap; allow_cost = true) + prob = ODEProblem(lksys, u0map, tspan, parammap; check_compatibility = false) sol = solve(prob, Tsit5()) - costfn = ModelingToolkit.generate_cost_function(lksys) + costfn = ModelingToolkit.generate_cost( + lksys; expression = Val{false}, wrap_gfw = Val{true}) @test costfn(sol, prob.p, _t) ≈ log(sol(0.56)[2] + sol(0.0)[1]) - sol(0.4)[1]^2 end From 45b3d5924da84e0afe55539ff100fe7735281c1a Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 8 May 2025 18:38:12 +0530 Subject: [PATCH 1748/2176] test: update DDE tests to new semantics --- test/dde.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/dde.jl b/test/dde.jl index c7561e6c24..a5a0f6ee0d 100644 --- a/test/dde.jl +++ b/test/dde.jl @@ -86,7 +86,7 @@ eqs = [D(x(t)) ~ a * x(t) + b * x(t - τ) + c + (α * x(t) + γ) * η, delx ~ x( @test ModelingToolkit.is_dde(sys) @test !is_markovian(sys) @test equations(sys) == [D(x(t)) ~ a * x(t) + b * x(t - τ) + c] -@test isequal(ModelingToolkit.get_noiseeqs(sys), [α * x(t) + γ]) +@test isequal(ModelingToolkit.get_noise_eqs(sys), [α * x(t) + γ;;]) prob_mtk = SDDEProblem(sys, [x(t) => 1.0 + t], tspan; constant_lags = (τ,)); @test_nowarn sol_mtk = solve(prob_mtk, RKMil(), seed = 100) From f5fffbb865c1d7e4437c56a328940548449012af Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 8 May 2025 18:38:52 +0530 Subject: [PATCH 1749/2176] test: make debugging tests more reproducible --- test/debugging.jl | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/test/debugging.jl b/test/debugging.jl index de4420d08c..615b452b38 100644 --- a/test/debugging.jl +++ b/test/debugging.jl @@ -5,15 +5,17 @@ using ModelingToolkit: t_nounits as t, D_nounits as D, ASSERTION_LOG_VARIABLE @variables x(t) @brownian a @named inner_ode = System(D(x) ~ -sqrt(x), t; assertions = [(x > 0) => "ohno"]) -@named inner_sde = System([D(x) ~ -sqrt(x) + a], t; assertions = [(x > 0) => "ohno"]) +@named inner_sde = System([D(x) ~ -10sqrt(x) + 0.01a], t; assertions = [(x > 0) => "ohno"]) sys_ode = structural_simplify(inner_ode) sys_sde = structural_simplify(inner_sde) +SEED = 42 @testset "assertions are present in generated `f`" begin - @testset "$(typeof(sys))" for (Problem, sys, alg) in [ + @testset "$(Problem)" for (Problem, sys, alg) in [ (ODEProblem, sys_ode, Tsit5()), (SDEProblem, sys_sde, ImplicitEM())] + kwargs = Problem == SDEProblem ? (; seed = SEED) : (;) @test !is_parameter(sys, ASSERTION_LOG_VARIABLE) - prob = Problem(sys, [x => 0.1], (0.0, 5.0)) + prob = Problem(sys, [x => 0.1], (0.0, 5.0); kwargs...) sol = solve(prob, alg) @test !SciMLBase.successful_retcode(sol) @test isnan(prob.f.f([0.0], prob.p, sol.t[end])[1]) @@ -21,11 +23,12 @@ sys_sde = structural_simplify(inner_sde) end @testset "`debug_system` adds logging" begin - @testset "$(typeof(sys))" for (Problem, sys, alg) in [ + @testset "$(Problem)" for (Problem, sys, alg) in [ (ODEProblem, sys_ode, Tsit5()), (SDEProblem, sys_sde, ImplicitEM())] + kwargs = Problem == SDEProblem ? (; seed = SEED) : (;) dsys = debug_system(sys; functions = []) @test is_parameter(dsys, ASSERTION_LOG_VARIABLE) - prob = Problem(dsys, [x => 0.1], (0.0, 5.0)) + prob = Problem(dsys, [x => 0.1], (0.0, 5.0); kwargs...) sol = @test_logs (:error, r"ohno") match_mode=:any solve(prob, alg) @test !SciMLBase.successful_retcode(sol) prob.ps[ASSERTION_LOG_VARIABLE] = false @@ -35,13 +38,14 @@ end end @testset "Hierarchical system" begin - @testset "$(typeof(inner))" for (ctor, Problem, inner, alg) in [ + @testset "$(Problem)" for (ctor, Problem, inner, alg) in [ (System, ODEProblem, inner_ode, Tsit5()), (System, SDEProblem, inner_sde, ImplicitEM())] + kwargs = Problem == SDEProblem ? (; seed = SEED) : (;) @mtkbuild outer = ctor(Equation[], t; systems = [inner]) dsys = debug_system(outer; functions = []) @test is_parameter(dsys, ASSERTION_LOG_VARIABLE) - prob = Problem(dsys, [inner.x => 0.1], (0.0, 5.0)) + prob = Problem(dsys, [inner.x => 0.1], (0.0, 5.0); kwargs...) sol = @test_logs (:error, r"ohno") match_mode=:any solve(prob, alg) @test !SciMLBase.successful_retcode(sol) prob.ps[ASSERTION_LOG_VARIABLE] = false From a77d685d25fb25c913b25e468600fea33db798ba Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 8 May 2025 18:39:14 +0530 Subject: [PATCH 1750/2176] test: update discrete system tests to new semantics --- test/discrete_system.jl | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/discrete_system.jl b/test/discrete_system.jl index 0c5412977f..1ed6438d76 100644 --- a/test/discrete_system.jl +++ b/test/discrete_system.jl @@ -37,7 +37,8 @@ syss = structural_simplify(sys) df = DiscreteFunction(syss) # iip du = zeros(3) -u = ModelingToolkit.better_varmap_to_vars(Dict([S => 1, I => 2, R => 3]), unknowns(syss)) +u = ModelingToolkit.better_varmap_to_vars( + Dict([S(k - 1) => 1, I(k - 1) => 2, R(k - 1) => 3]), unknowns(syss)) p = MTKParameters(syss, [c, nsteps, δt, β, γ] .=> collect(1:5)) df.f(du, u, p, 0) reorderer = getu(syss, [S, I, R]) From f02179123e65fc4caee91d613bd5c5b2b1ee00e7 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 8 May 2025 18:39:25 +0530 Subject: [PATCH 1751/2176] test: update implicit discrete system tests to new semantics --- test/implicit_discrete_system.jl | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/test/implicit_discrete_system.jl b/test/implicit_discrete_system.jl index 45c89f969e..49ab2a5921 100644 --- a/test/implicit_discrete_system.jl +++ b/test/implicit_discrete_system.jl @@ -28,7 +28,8 @@ rng = StableRNG(22525) @test prob.u0 == [1.0, 1.0] @variables x(t) @mtkbuild sys = System([x(k) ~ x(k) * x(k - 1) - 3], t) - @test_throws ErrorException prob=ImplicitDiscreteProblem(sys, [], tspan) + @test_throws ModelingToolkit.MissingVariablesError prob=ImplicitDiscreteProblem( + sys, [], tspan) end @testset "System with algebraic equations" begin @@ -64,12 +65,14 @@ end x + y + z ~ 2] @mtkbuild sys = System(eqs, t) @test length(unknowns(sys)) == length(equations(sys)) == 3 - @test occursin("var\"y(t)\"", string(ImplicitDiscreteFunctionExpr(sys))) + @test occursin( + "var\"y(t)\"", string(ImplicitDiscreteFunction(sys; expression = Val{true}))) # Shifted observable that appears in algebraic equation is properly handled. eqs = [z(k) ~ x(k) + sin(x(k)), y(k) ~ x(k - 1) + x(k - 2), z(k) * x(k) ~ 3] @mtkbuild sys = System(eqs, t) - @test occursin("var\"Shift(t, 1)(z(t))\"", string(ImplicitDiscreteFunctionExpr(sys))) + @test occursin("var\"Shift(t, 1)(z(t))\"", + string(ImplicitDiscreteFunction(sys; expression = Val{true}))) end From efbe0c87b89b37bcb768e4cfaa7473f12764ece5 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 8 May 2025 18:39:35 +0530 Subject: [PATCH 1752/2176] test: remove redundant initial values tests --- test/initial_values.jl | 14 ++------------ 1 file changed, 2 insertions(+), 12 deletions(-) diff --git a/test/initial_values.jl b/test/initial_values.jl index aca533acbd..cc75c2e41e 100644 --- a/test/initial_values.jl +++ b/test/initial_values.jl @@ -100,18 +100,8 @@ prob = ODEProblem(sys, [], (0.0, 1.0), [A1 => 0.3]) @testset "default=nothing is skipped" begin @parameters p = nothing @variables x(t)=nothing y(t) - for sys in [ - System(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), - System(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 + @named sys = System(Equation[], t, [x, y], [p]; defaults = [y => nothing]) + @test isempty(ModelingToolkit.defaults(sys)) end # Using indepvar in initialization From 33da82f96647abb0a019ca2f4f135e6fb2425e94 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 8 May 2025 18:39:53 +0530 Subject: [PATCH 1753/2176] test: update initialization system tests to new semantics --- test/initializationsystem.jl | 47 +++++++++++++++--------------------- 1 file changed, 20 insertions(+), 27 deletions(-) diff --git a/test/initializationsystem.jl b/test/initializationsystem.jl index 93a0845bdb..eda381b5a3 100644 --- a/test/initializationsystem.jl +++ b/test/initializationsystem.jl @@ -600,13 +600,12 @@ end # the correct solver. # `rhss` allows adding terms to the end of equations (only 2 equations allowed) to influence # the system type (brownian vars to turn it into an SDE). - @testset "$Problem with $(SciMLBase.parameterless_type(alg)) and $ctor ctor" for ((System, Problem, alg, rhss), (ctor, expectedT)) in Iterators.product( + @testset "$Problem with $(SciMLBase.parameterless_type(alg)) and $ctor ctor" for ((Problem, alg, rhss), (ctor, expectedT)) in Iterators.product( [ - (ModelingToolkit.System, ODEProblem, Tsit5(), zeros(2)), - (ModelingToolkit.System, SDEProblem, ImplicitEM(), [a, b]), - (ModelingToolkit.System, DDEProblem, - MethodOfSteps(Tsit5()), [_x(t - 0.1), 0.0]), - (ModelingToolkit.System, SDDEProblem, ImplicitEM(), [_x(t - 0.1) + a, b]) + (ODEProblem, Tsit5(), zeros(2)), + (SDEProblem, ImplicitEM(), [a, b]), + (DDEProblem, MethodOfSteps(Tsit5()), [_x(t - 0.1), 0.0]), + (SDDEProblem, ImplicitEM(), [_x(t - 0.1) + a, b]) ], [(identity, Any), (sarray_ctor, SVector)]) u0_constructor = p_constructor = ctor @@ -626,11 +625,10 @@ end @test parameter_values(initprob).tunable isa expectedT @test solve(prob, alg).ps[sym] ≈ val end - function test_initializesystem(sys, u0map, pmap, p, equation) - isys = ModelingToolkit.generate_initializesystem( - sys; u0map, pmap, guesses = ModelingToolkit.guesses(sys)) - @test is_variable(isys, p) - @test equation in equations(isys) || (0 ~ -equation.rhs) in equations(isys) + function test_initializesystem(prob, p, equation) + isys = prob.f.initialization_data.initializeprob.f.sys + @test is_variable(isys, p) || ModelingToolkit.has_observed_with_lhs(isys, p) + @test equation in [equations(isys); observed(isys)] end u0map = Dict(x => 1.0, y => 1.0) @@ -650,7 +648,7 @@ end [D(x) ~ x + rhss[1], p ~ x + y + rhss[2]], t; defaults = [p => missing], guesses = [p => 0.0]) prob = Problem(sys, u0map, (0.0, 1.0); u0_constructor, p_constructor) test_parameter(prob, p, 2.0) - test_initializesystem(sys, u0map, pmap, p, 0 ~ p - x - y) + test_initializesystem(prob, p, p ~ x + y) prob2 = remake(prob; u0 = u0map) prob2 = remake(prob2; p = setp_oop(prob2, p)(prob2, 0.0)) test_parameter(prob2, p, 2.0) @@ -661,7 +659,7 @@ end pmap[p] = missing prob = Problem(sys, u0map, (0.0, 1.0), pmap; u0_constructor, p_constructor) test_parameter(prob, p, 2.0) - test_initializesystem(sys, u0map, pmap, p, 0 ~ 2q - p) + test_initializesystem(prob, p, p ~ 2q) prob2 = remake(prob; u0 = u0map, p = pmap) prob2 = remake(prob2; p = setp_oop(prob2, p)(prob2, 0.0)) test_parameter(prob2, p, 2.0) @@ -670,7 +668,7 @@ end [D(x) ~ x + rhss[1], p ~ x + y + rhss[2]], t; guesses = [p => 0.0]) prob = Problem(sys, u0map, (0.0, 1.0), pmap; u0_constructor, p_constructor) test_parameter(prob, p, 2.0) - test_initializesystem(sys, u0map, pmap, p, 0 ~ x + y - p) + test_initializesystem(prob, p, p ~ x + y) prob2 = remake(prob; u0 = u0map, p = pmap) prob2 = remake(prob2; p = setp_oop(prob2, p)(prob2, 0.0)) test_parameter(prob2, p, 2.0) @@ -681,7 +679,7 @@ end delete!(pmap, p) prob = Problem(sys, u0map, (0.0, 1.0), pmap; u0_constructor, p_constructor) test_parameter(prob, p, 2.0) - test_initializesystem(sys, u0map, pmap, p, 0 ~ 2q - p) + test_initializesystem(prob, p, p ~ 2q) prob2 = remake(prob; u0 = u0map, p = pmap) prob2 = remake(prob2; p = setp_oop(prob2, p)(prob2, 0.0)) test_parameter(prob2, p, 2.0) @@ -692,7 +690,7 @@ end _pmap = merge(pmap, Dict(p => q)) prob = Problem(sys, u0map, (0.0, 1.0), _pmap; u0_constructor, p_constructor) test_parameter(prob, p, _pmap[q]) - test_initializesystem(sys, u0map, _pmap, p, 0 ~ q - p) + test_initializesystem(prob, p, p ~ q) # Problem dependent value with guess, no `missing` @mtkbuild sys = System( [D(x) ~ y * q + p + rhss[1], D(y) ~ x * p + q + rhss[2]], t; guesses = [p => 0.0]) @@ -937,11 +935,11 @@ end @brownian a b x = _x(t) - @testset "$Problem with $(SciMLBase.parameterless_type(typeof(alg)))" for (System, Problem, alg, rhss) in [ - (ModelingToolkit.System, ODEProblem, Tsit5(), zeros(2)), - (ModelingToolkit.System, SDEProblem, ImplicitEM(), [a, b]), - (ModelingToolkit.System, DDEProblem, MethodOfSteps(Tsit5()), [_x(t - 0.1), 0.0]), - (ModelingToolkit.System, SDDEProblem, ImplicitEM(), [_x(t - 0.1) + a, b]) + @testset "$Problem with $(SciMLBase.parameterless_type(typeof(alg)))" for (Problem, alg, rhss) in [ + (ODEProblem, Tsit5(), zeros(2)), + (SDEProblem, ImplicitEM(), [a, b]), + (DDEProblem, MethodOfSteps(Tsit5()), [_x(t - 0.1), 0.0]), + (SDDEProblem, ImplicitEM(), [_x(t - 0.1) + a, b]) ] @mtkbuild sys = System( [D(x) ~ x + rhss[1], p ~ x + y + rhss[2]], t; defaults = [p => missing], guesses = [ @@ -1327,13 +1325,8 @@ end u0s = [I => 1, R => 0] ps = [S0 => 999, β => 0.01, γ => 0.001] - dprob = DiscreteProblem(js, u0s, (0.0, 10.0), ps) - @test_broken dprob.f.initialization_data !== nothing - sol = solve(dprob, FunctionMap()) - @test sol[S, 1] ≈ 999 - @test SciMLBase.successful_retcode(sol) - jprob = JumpProblem(js, dprob) + jprob = JumpProblem(js, u0s, (0.0, 10.0), ps) sol = solve(jprob, SSAStepper()) @test sol[S, 1] ≈ 999 @test SciMLBase.successful_retcode(sol) From 5849d3491e59b2f6893e0a5aad8a9afdf3ec2e51 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 8 May 2025 18:40:06 +0530 Subject: [PATCH 1754/2176] test: fix jump system tests test: fix jumpsystem tests --- test/jumpsystem.jl | 46 ++++++++++++++++++++++++---------------------- 1 file changed, 24 insertions(+), 22 deletions(-) diff --git a/test/jumpsystem.jl b/test/jumpsystem.jl index d80ee40666..50d59b3313 100644 --- a/test/jumpsystem.jl +++ b/test/jumpsystem.jl @@ -188,12 +188,13 @@ sol = solve(jprob, SSAStepper()); @testset "Combined system name collisions" begin sys1 = JumpSystem([maj1, maj2], t, [S], [β, γ], name = :sys1) sys2 = JumpSystem([maj1, maj2], t, [S], [β, γ], name = :sys1) - @test_throws ArgumentError JumpSystem([sys1.γ ~ sys2.γ], t, [], [], + @test_throws ModelingToolkit.NonUniqueSubsystemsError JumpSystem( + [sys1.γ ~ sys2.γ], t, [], [], systems = [sys1, sys2], name = :foo) end # test if param mapper is setup correctly for callbacks -let +@testset "Parammapper with callbacks" begin @parameters k1 k2 k3 @variables A(t) B(t) maj1 = MassActionJump(k1 * k3, [0 => 1], [A => -1, B => 1]) @@ -220,15 +221,17 @@ let end # observed variable handling -@variables OBS(t) -@named js5 = JumpSystem([maj1, maj2], t, [S], [β, γ]; observed = [OBS ~ 2 * S * h]) -OBS2 = OBS -@test isequal(OBS2, @nonamespace js5.OBS) -@unpack OBS = js5 -@test isequal(OBS2, OBS) +@testset "Observed handling tests" begin + @variables OBS(t) + @named js5 = JumpSystem([maj1, maj2], t, [S], [β, γ]; observed = [OBS ~ 2 * S * h]) + OBS2 = OBS + @test isequal(OBS2, @nonamespace js5.OBS) + @unpack OBS = js5 + @test isequal(OBS2, OBS) +end # test to make sure dep graphs are correct -let +@testset "Dependency graph tests" begin # A + 2X --> 3X # 3X --> A + 2X # B --> X @@ -239,8 +242,8 @@ let MassActionJump(1.0, [B => 1], [B => -1, X => 1]), MassActionJump(1.0, [X => 1], [B => 1, X => -1])] @named js = JumpSystem(jumps, t, [A, X, B], []) - jdeps = asgraph(js) - vdeps = variable_dependencies(js) + jdeps = asgraph(js; eqs = MT.jumps(js)) + vdeps = variable_dependencies(js; eqs = MT.jumps(js)) vtoj = jdeps.badjlist @test vtoj == [[1], [1, 2, 4], [3]] jtov = vdeps.badjlist @@ -284,7 +287,7 @@ j1 = ConstantRateJump(k, [X ~ Pre(X) - 1]) @test_nowarn @mtkbuild js1 = JumpSystem([j1], t, [X], [k]) # test correct autosolver is selected, which implies appropriate dep graphs are available -let +@testset "Autosolver test" begin @parameters k @variables X(t) rate = k @@ -302,7 +305,7 @@ let end # basic VariableRateJump test -let +@testset "VRJ test" begin N = 1000 # number of simulations for testing solve accuracy Random.seed!(rng, 1111) @variables A(t) B(t) C(t) @@ -310,7 +313,7 @@ let vrj = VariableRateJump(k * (sin(t) + 1), [A ~ Pre(A) + 1, C ~ Pre(C) + 2]) js = complete(JumpSystem([vrj], t, [A, C], [k]; name = :js, observed = [B ~ C * A])) jprob = JumpProblem( - js, [A => 0, C => 0], (0.0, 10.0), [k => 1.0]; aggregtor = Direct(), rng) + js, [A => 0, C => 0], (0.0, 10.0), [k => 1.0]; aggregator = Direct(), rng) @test jprob.prob isa ODEProblem sol = solve(jprob, Tsit5()) @@ -346,7 +349,7 @@ let end # collect_vars! tests for jumps -let +@testset "`collect_vars!` for jumps" begin @variables x1(t) x2(t) x3(t) x4(t) x5(t) @parameters p1 p2 p3 p4 p5 j1 = ConstantRateJump(p1, [x1 ~ Pre(x1) + 1]) @@ -381,7 +384,7 @@ let end # scoping tests -let +@testset "Scoping tests" begin @variables x1(t) x2(t) x3(t) x4(t) x2 = ParentScope(x2) x3 = ParentScope(ParentScope(x3)) @@ -426,7 +429,7 @@ let end # PDMP test -let +@testset "PDMP test" begin seed = 1111 Random.seed!(rng, seed) @variables X(t) Y(t) @@ -467,7 +470,7 @@ let end # that mixes ODEs and jump types, and then contin events -let +@testset "ODEs + Jumps + Continuous events" begin seed = 1111 Random.seed!(rng, seed) @variables X(t) Y(t) @@ -482,7 +485,7 @@ let u0map = [X => p.X₀, Y => p.Y₀] pmap = [α => p.α, β => p.β] tspan = (0.0, 20.0) - jprob = JumpProblem(jsys, u0, tspan, pmap; rng, save_positions = (false, false)) + jprob = JumpProblem(jsys, u0map, tspan, pmap; rng, save_positions = (false, false)) times = range(0.0, tspan[2], length = 100) Nsims = 4000 Xv = zeros(length(times)) @@ -520,7 +523,7 @@ let continuous_events = cevents) jsys = complete(jsys) tspan = (0.0, 200.0) - jprob = JumpProblem(jsys, u0, tspan, pmap; rng, save_positions = (false, false)) + jprob = JumpProblem(jsys, u0map, tspan, pmap; rng, save_positions = (false, false)) Xsamp = 0.0 Nsims = 4000 for n in 1:Nsims @@ -560,8 +563,7 @@ end crj = ConstantRateJump(rate, affect) @named jsys = JumpSystem([crj, eq], t, [X], [a, b]) jsys = complete(jsys) - oprob = ODEProblem(jsys, [:X => 1.0], (0.0, 10.0), [:a => 1.0, :b => 0.5]) - jprob = JumpProblem(jsys, oprob) + jprob = JumpProblem(jsys, [:X => 1.0], (0.0, 10.0), [:a => 1.0, :b => 0.5]) jprob2 = remake(jprob; u0 = [:X => 10.0]) @test jprob2[X] ≈ 10.0 end From 3c539414262a14f869e0eac42409383aa47a9588 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 8 May 2025 18:40:15 +0530 Subject: [PATCH 1755/2176] test: remove redundant namespacing tests --- test/namespacing.jl | 206 +++++--------------------------------------- 1 file changed, 23 insertions(+), 183 deletions(-) diff --git a/test/namespacing.jl b/test/namespacing.jl index 50cbd7e3a3..6a8a654ecf 100644 --- a/test/namespacing.jl +++ b/test/namespacing.jl @@ -1,186 +1,26 @@ using ModelingToolkit using ModelingToolkit: t_nounits as t, D_nounits as D, iscomplete, does_namespacing -@testset "System" begin - @variables x(t) - @parameters p - sys = System(D(x) ~ p * x, t; name = :inner) - @test !iscomplete(sys) - @test does_namespacing(sys) - - csys = complete(sys) - @test iscomplete(csys) - @test !does_namespacing(csys) - - nsys = toggle_namespacing(sys, false) - @test !iscomplete(nsys) - @test !does_namespacing(nsys) - - @test isequal(x, csys.x) - @test isequal(x, nsys.x) - @test !isequal(x, sys.x) - @test isequal(p, csys.p) - @test isequal(p, nsys.p) - @test !isequal(p, sys.p) - - @test_throws ["namespacing", "inner"] System( - Equation[], t; systems = [nsys], name = :a) -end - -@testset "SDESystem" begin - @variables x(t) - @parameters p - sys = SDESystem([D(x) ~ p * x], [x], t, [x], [p]; name = :inner) - @test !iscomplete(sys) - @test does_namespacing(sys) - - csys = complete(sys) - @test iscomplete(csys) - @test !does_namespacing(csys) - - nsys = toggle_namespacing(sys, false) - @test !iscomplete(nsys) - @test !does_namespacing(nsys) - - @test isequal(x, csys.x) - @test isequal(x, nsys.x) - @test !isequal(x, sys.x) - @test isequal(p, csys.p) - @test isequal(p, nsys.p) - @test !isequal(p, sys.p) - - @test_throws ["namespacing", "inner"] SDESystem( - Equation[], [], t; systems = [nsys], name = :a) -end - -@testset "DiscreteSystem" begin - @variables x(t) - @parameters p - k = ShiftIndex(t) - sys = System([x(k) ~ p * x(k - 1)], t; name = :inner) - @test !iscomplete(sys) - @test does_namespacing(sys) - - csys = complete(sys) - @test iscomplete(csys) - @test !does_namespacing(csys) - - nsys = toggle_namespacing(sys, false) - @test !iscomplete(nsys) - @test !does_namespacing(nsys) - - @test isequal(x, csys.x) - @test isequal(x, nsys.x) - @test !isequal(x, sys.x) - @test isequal(p, csys.p) - @test isequal(p, nsys.p) - @test !isequal(p, sys.p) - - @test_throws ["namespacing", "inner"] System( - Equation[], t; systems = [nsys], name = :a) -end - -@testset "ImplicitDiscreteSystem" begin - @variables x(t) - @parameters p - k = ShiftIndex(t) - sys = System([x(k) ~ p + x(k - 1) * x(k)], t; name = :inner) - @test !iscomplete(sys) - @test does_namespacing(sys) - - csys = complete(sys) - @test iscomplete(csys) - @test !does_namespacing(csys) - - nsys = toggle_namespacing(sys, false) - @test !iscomplete(nsys) - @test !does_namespacing(nsys) - - @test isequal(x, csys.x) - @test isequal(x, nsys.x) - @test !isequal(x, sys.x) - @test isequal(p, csys.p) - @test isequal(p, nsys.p) - @test !isequal(p, sys.p) - - @test_throws ["namespacing", "inner"] System( - Equation[], t; systems = [nsys], name = :a) -end - -@testset "NonlinearSystem" begin - @variables x - @parameters p - sys = System([x ~ p * x^2 + 1]; name = :inner) - @test !iscomplete(sys) - @test does_namespacing(sys) - - csys = complete(sys) - @test iscomplete(csys) - @test !does_namespacing(csys) - - nsys = toggle_namespacing(sys, false) - @test !iscomplete(nsys) - @test !does_namespacing(nsys) - - @test isequal(x, csys.x) - @test isequal(x, nsys.x) - @test !isequal(x, sys.x) - @test isequal(p, csys.p) - @test isequal(p, nsys.p) - @test !isequal(p, sys.p) - - @test_throws ["namespacing", "inner"] System( - Equation[]; systems = [nsys], name = :a) -end - -@testset "OptimizationSystem" begin - @variables x - @parameters p - sys = OptimizationSystem(p * x^2 + 1; name = :inner) - @test !iscomplete(sys) - @test does_namespacing(sys) - - csys = complete(sys) - @test iscomplete(csys) - @test !does_namespacing(csys) - - nsys = toggle_namespacing(sys, false) - @test !iscomplete(nsys) - @test !does_namespacing(nsys) - - @test isequal(x, csys.x) - @test isequal(x, nsys.x) - @test !isequal(x, sys.x) - @test isequal(p, csys.p) - @test isequal(p, nsys.p) - @test !isequal(p, sys.p) - - @test_throws ["namespacing", "inner"] OptimizationSystem( - []; systems = [nsys], name = :a) -end - -@testset "ConstraintsSystem" begin - @variables x - @parameters p - sys = ConstraintsSystem([x^2 + p ~ 0], [x], [p]; name = :inner) - @test !iscomplete(sys) - @test does_namespacing(sys) - - csys = complete(sys) - @test iscomplete(csys) - @test !does_namespacing(csys) - - nsys = toggle_namespacing(sys, false) - @test !iscomplete(nsys) - @test !does_namespacing(nsys) - - @test isequal(x, csys.x) - @test isequal(x, nsys.x) - @test !isequal(x, sys.x) - @test isequal(p, csys.p) - @test isequal(p, nsys.p) - @test !isequal(p, sys.p) - - @test_throws ["namespacing", "inner"] ConstraintsSystem( - [], [], []; systems = [nsys], name = :a) -end +@variables x(t) +@parameters p +sys = System(D(x) ~ p * x, t; name = :inner) +@test !iscomplete(sys) +@test does_namespacing(sys) + +csys = complete(sys) +@test iscomplete(csys) +@test !does_namespacing(csys) + +nsys = toggle_namespacing(sys, false) +@test !iscomplete(nsys) +@test !does_namespacing(nsys) + +@test isequal(x, csys.x) +@test isequal(x, nsys.x) +@test !isequal(x, sys.x) +@test isequal(p, csys.p) +@test isequal(p, nsys.p) +@test !isequal(p, sys.p) + +@test_throws ["namespacing", "inner"] System( + Equation[], t; systems = [nsys], name = :a) From eb53edd57bab94ec3928a9f773feb785754e1947 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 8 May 2025 18:40:46 +0530 Subject: [PATCH 1756/2176] fix: fix `independent_variables` --- src/systems/abstractsystem.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index 04d520838b..4021f1fc44 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -236,7 +236,7 @@ function independent_variables(sys::AbstractSystem) if !(sys isa System) @warn "Please declare ($(typeof(sys))) as a subtype of `AbstractTimeDependentSystem`, `AbstractTimeIndependentSystem` or `AbstractMultivariateSystem`." end - if isdefined(sys, :iv) + if isdefined(sys, :iv) && getfield(sys, :iv) !== nothing return [getfield(sys, :iv)] elseif isdefined(sys, :ivs) return getfield(sys, :ivs) From e2f142255da8fdd8b6ce494054896828d794526e Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 8 May 2025 18:41:03 +0530 Subject: [PATCH 1757/2176] feat: export newly added functions --- src/ModelingToolkit.jl | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/ModelingToolkit.jl b/src/ModelingToolkit.jl index 7f228eabbc..65be6cbf75 100644 --- a/src/ModelingToolkit.jl +++ b/src/ModelingToolkit.jl @@ -307,7 +307,8 @@ export Differential, expand_derivatives, @derivatives export Equation, ConstrainedEquation export Term, Sym export SymScope, LocalScope, ParentScope, GlobalScope -export independent_variable, equations, controls, observed, full_equations, jumps +export independent_variable, equations, controls, observed, full_equations, jumps, cost, + brownians export initialization_equations, guesses, defaults, parameter_dependencies, hierarchy export structural_simplify, expand_connections, linearize, linearization_function, LinearizationProblem @@ -315,12 +316,12 @@ export solve export Pre export calculate_jacobian, generate_jacobian, generate_rhs, generate_custom_function, - generate_W + generate_W, calculate_hessian export calculate_control_jacobian, generate_control_jacobian export calculate_tgrad, generate_tgrad -export calculate_gradient, generate_gradient +export generate_cost, calculate_cost_gradient, generate_cost_gradient export calculate_factorized_W, generate_factorized_W -export calculate_hessian, generate_hessian +export calculate_cost_hessian, generate_cost_hessian export calculate_massmatrix, generate_diffusion_function export stochastic_integral_transform export TearingState From 4a39883b94819fa8ef1f663d66d37c1d654ede83 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 14 May 2025 13:57:03 +0530 Subject: [PATCH 1758/2176] feat: add `noise_to_brownians` --- src/ModelingToolkit.jl | 3 +- src/systems/diffeqs/basic_transformations.jl | 54 ++++++++++++++++++++ 2 files changed, 56 insertions(+), 1 deletion(-) diff --git a/src/ModelingToolkit.jl b/src/ModelingToolkit.jl index 65be6cbf75..06b9d43653 100644 --- a/src/ModelingToolkit.jl +++ b/src/ModelingToolkit.jl @@ -301,7 +301,8 @@ export isinput, isoutput, getbounds, hasbounds, getguess, hasguess, isdisturbanc hasunit, getunit, hasconnect, getconnect, hasmisc, getmisc, state_priority export ode_order_lowering, dae_order_lowering, liouville_transform, - change_independent_variable, substitute_component, add_accumulations + change_independent_variable, substitute_component, add_accumulations, + noise_to_brownians export PDESystem export Differential, expand_derivatives, @derivatives export Equation, ConstrainedEquation diff --git a/src/systems/diffeqs/basic_transformations.jl b/src/systems/diffeqs/basic_transformations.jl index c9d5234751..9b7c72494d 100644 --- a/src/systems/diffeqs/basic_transformations.jl +++ b/src/systems/diffeqs/basic_transformations.jl @@ -447,3 +447,57 @@ function add_accumulations(sys::System, vars::Vector{<:Pair}) @set! sys.defaults = merge(get_defaults(sys), Dict(a => 0.0 for a in avars)) return sys end + +""" + $(TYPEDSIGNATURES) + +Given a system with noise in the form of noise equation (`get_noise_eqs(sys) !== nothing`) +return an equivalent system which represents the noise using brownian variables. + +# Keyword Arguments + +- `names`: The name(s) to use for the brownian variables. If this is a `Symbol`, variables + with the given name and successive numeric `_i` suffixes will be used. If a `Vector`, + this must have appropriate length for the noise equations of the system. The + corresponding number of brownian variables are created with the given names. +""" +function noise_to_brownians(sys::System; names::Union{Symbol, Vector{Symbol}} = :α) + neqs = get_noise_eqs(sys) + if neqs === nothing + throw(ArgumentError("Expected a system with `noise_eqs`.")) + end + if !isempty(get_systems(sys)) + throw(ArgumentError("The system must be flattened.")) + end + # vector means diagonal noise + nbrownians = ndims(neqs) == 1 ? length(neqs) : size(neqs, 2) + if names isa Symbol + names = [Symbol(names, :_, i) for i in 1:nbrownians] + end + if length(names) != nbrownians + throw(ArgumentError(""" + The system has $nbrownians brownian variables. Received $(length(names)) names \ + for the brownian variables. Provide $nbrownians names or a single `Symbol` to use \ + an array variable of the appropriately length. + """)) + end + brownvars = map(names) do name + only(@brownian $name) + end + + terms = if ndims(neqs) == 1 + neqs .* brownvars + else + neqs * brownvars + end + + eqs = map(get_eqs(sys), terms) do eq, term + eq.lhs ~ eq.rhs + term + end + + @set! sys.eqs = eqs + @set! sys.brownians = brownvars + @set! sys.noise_eqs = nothing + + return sys +end From bfcae7232b0c174118bbe38a0034e6953a98127f Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 14 May 2025 14:01:57 +0530 Subject: [PATCH 1759/2176] feat: add `convert_system_indepvar` --- src/systems/diffeqs/basic_transformations.jl | 71 ++++++++++++++++++++ 1 file changed, 71 insertions(+) diff --git a/src/systems/diffeqs/basic_transformations.jl b/src/systems/diffeqs/basic_transformations.jl index 9b7c72494d..4bf3b40177 100644 --- a/src/systems/diffeqs/basic_transformations.jl +++ b/src/systems/diffeqs/basic_transformations.jl @@ -501,3 +501,74 @@ function noise_to_brownians(sys::System; names::Union{Symbol, Vector{Symbol}} = return sys end + +""" + $(TYPEDSIGNATURES) + +Function which takes a system `sys` and an independent variable `t` and changes the +independent variable of `sys` to `t`. This is different from +[`change_independent_variable`](@ref) since this function only does a symbolic substitution +of the independent variable. `sys` must not be a reduced system (`observed(sys)` must be +empty). If `sys` is time-independent, this can be used to turn it into a time-dependent +system. + +# Keyword arguments + +- `name`: The name of the returned system. +""" +function convert_system_indepvar(sys::System, t; name = nameof(sys)) + isempty(observed(sys)) || + throw(ArgumentError(""" + `convert_system_indepvar` cannot handle reduced model (i.e. observed(sys) is non-\ + empty). + """)) + t = value(t) + varmap = Dict() + sts = unknowns(sys) + newsts = similar(sts, Any) + for (i, s) in enumerate(sts) + if iscall(s) + args = arguments(s) + length(args) == 1 || + throw(InvalidSystemException("Illegal unknown: $s. The unknown can have at most one argument like `x(t)`.")) + arg = args[1] + if isequal(arg, t) + newsts[i] = s + continue + end + ns = maketerm(typeof(s), operation(s), Any[t], + SymbolicUtils.metadata(s)) + newsts[i] = ns + varmap[s] = ns + else + ns = variable(getname(s); T = FnType)(t) + newsts[i] = ns + varmap[s] = ns + end + end + sub = Base.Fix2(substitute, varmap) + if is_time_dependent(sys) + iv = only(independent_variables(sys)) + sub.x[iv] = t # otherwise the Differentials aren't fixed + end + neweqs = map(sub, equations(sys)) + defs = Dict(sub(k) => sub(v) for (k, v) in defaults(sys)) + neqs = get_noise_eqs(sys) + if neqs !== nothing + neqs = map(sub, neqs) + end + cstrs = map(sub, get_constraints(sys)) + costs = Vector{Union{Real, BasicSymbolic}}(map(sub, get_costs(sys))) + @set! sys.eqs = neweqs + @set! sys.iv = t + @set! sys.unknowns = newsts + @set! sys.defaults = defs + @set! sys.name = name + @set! sys.noise_eqs = neqs + @set! sys.constraints = cstrs + @set! sys.costs = costs + + var_to_name = Dict(k => get(varmap, v, v) for (k, v) in get_var_to_name(sys)) + @set! sys.var_to_name = var_to_name + return sys +end From d94e88b0767ac2795ecd2d36b0cac00bf010f9e4 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 14 May 2025 14:02:56 +0530 Subject: [PATCH 1760/2176] fix: fix structural simplification for SDEs --- src/systems/systems.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/systems/systems.jl b/src/systems/systems.jl index eb9e27d409..13ee0767cf 100644 --- a/src/systems/systems.jl +++ b/src/systems/systems.jl @@ -134,7 +134,7 @@ function __structural_simplify(sys::AbstractSystem; simplify = false, if sorted_g_rows isa AbstractMatrix && size(sorted_g_rows, 2) == 1 # If there's only one brownian variable referenced across all the equations, # we get a Nx1 matrix of noise equations, which is a special case known as scalar noise - noise_eqs = sorted_g_rows[:, 1] + noise_eqs = reshape(sorted_g_rows[:, 1], (:, 1)) is_scalar_noise = true elseif __num_isdiag_noise(sorted_g_rows) # If each column of the noise matrix has either 0 or 1 non-zero entry, then this is "diagonal noise". From 3cfba7f1c913eade2df26ccb9a241283c9fc8b92 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 14 May 2025 14:04:19 +0530 Subject: [PATCH 1761/2176] refactor: remove `convert_system` --- src/ModelingToolkit.jl | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/src/ModelingToolkit.jl b/src/ModelingToolkit.jl index 06b9d43653..37b4629992 100644 --- a/src/ModelingToolkit.jl +++ b/src/ModelingToolkit.jl @@ -216,11 +216,6 @@ include("inputoutput.jl") include("adjoints.jl") -for S in subtypes(ModelingToolkit.AbstractSystem) - S = nameof(S) - @eval convert_system(::Type{<:$S}, sys::$S) = sys -end - const t_nounits = let only(@independent_variables t) end @@ -274,8 +269,8 @@ export AbstractTimeDependentSystem, AbstractTimeIndependentSystem, AbstractMultivariateSystem -export ODEFunction, ODEFunctionExpr, ODEProblemExpr, convert_system, - System, OptimizationSystem, JumpSystem, SDESystem +export ODEFunction, ODEFunctionExpr, ODEProblemExpr, convert_system_indepvar, + System, OptimizationSystem, JumpSystem, SDESystem, NonlinearSystem export DAEFunctionExpr, DAEProblemExpr export SDEFunction, SDEFunctionExpr, SDEProblemExpr export SystemStructure From 4bea746d861cff3979f48fc397b81fe34007e30e Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 14 May 2025 14:05:14 +0530 Subject: [PATCH 1762/2176] test: fix nonlinearsystem tests --- test/nonlinearsystem.jl | 46 +++++++++++++---------------------------- 1 file changed, 14 insertions(+), 32 deletions(-) diff --git a/test/nonlinearsystem.jl b/test/nonlinearsystem.jl index e120e75e07..3eb141ebc1 100644 --- a/test/nonlinearsystem.jl +++ b/test/nonlinearsystem.jl @@ -30,7 +30,7 @@ eqs = [0 ~ σ * (y - x) * h, @test eval(toexpr(ns)) == ns test_nlsys_inference("standard", ns, (x, y, z), (σ, ρ, β)) @test begin - f = generate_function(ns, [x, y, z], [σ, ρ, β], expression = Val{false})[2] + f = generate_rhs(ns, [x, y, z], [σ, ρ, β], expression = Val{false})[2] du = [0.0, 0.0, 0.0] f(du, [1, 2, 3], [1, 2, 3]) du ≈ [1, -3, -7] @@ -57,9 +57,6 @@ jac = calculate_jacobian(ns) @test canonequal(jac[3, 2], x) @test canonequal(jac[3, 3], -1 * β) end -nlsys_func = generate_function(ns, [x, y, z], [σ, ρ, β]) -jac_func = generate_jacobian(ns) -f = @eval eval(nlsys_func) # Intermediate calculations a = y - x @@ -69,7 +66,7 @@ eqs = [0 ~ σ * a * h, 0 ~ x * y - β * z] @named ns = System(eqs, [x, y, z], [σ, ρ, β]) ns = complete(ns) -nlsys_func = generate_function(ns, [x, y, z], [σ, ρ, β]) +nlsys_func = generate_rhs(ns, [x, y, z], [σ, ρ, β]) nf = NonlinearFunction(ns) jac = calculate_jacobian(ns) @@ -119,14 +116,14 @@ lorenz2 = lorenz(:lorenz2) using OrdinaryDiffEq @independent_variables t D = Differential(t) -@named subsys = convert_system(System, lorenz1, t) +@named subsys = convert_system_indepvar(lorenz1, t) @named sys = System([D(subsys.x) ~ subsys.x + subsys.x], t, systems = [subsys]) sys = structural_simplify(sys) u0 = [subsys.x => 1, subsys.z => 2.0, subsys.y => 1.0] prob = ODEProblem(sys, u0, (0, 1.0), [subsys.σ => 1, subsys.ρ => 2, subsys.β => 3]) sol = solve(prob, FBDF(), reltol = 1e-7, abstol = 1e-7) @test sol[subsys.x] + sol[subsys.y] - sol[subsys.z]≈sol[subsys.u] atol=1e-7 -@test_throws ArgumentError convert_system(System, sys, t) +@test_throws ArgumentError convert_system_indepvar(sys, t) @parameters σ ρ β @variables x y z @@ -152,7 +149,8 @@ np = NonlinearProblem( function issue819() sys1 = makesys(:sys1) sys2 = makesys(:sys1) - @test_throws ArgumentError System([sys2.f ~ sys1.x, sys1.f ~ 0], [], [], + @test_throws ModelingToolkit.NonUniqueSubsystemsError System( + [sys2.f ~ sys1.x, sys1.f ~ 0], [], [], systems = [sys1, sys2], name = :foo) end issue819() @@ -200,23 +198,6 @@ eq = [v1 ~ sin(2pi * t * h) @named sys = System(eq, t) @test length(equations(structural_simplify(sys))) == 0 -@testset "Issue: 1504" begin - @variables u[1:4] - - eqs = [u[1] ~ 1, - u[2] ~ 1, - u[3] ~ 1, - u[4] ~ h] - - sys = System(eqs, collect(u[1:4]), Num[], defaults = Dict([]), name = :test) - sys = complete(sys) - prob = NonlinearProblem(sys, ones(length(unknowns(sys)))) - - sol = NonlinearSolve.solve(prob, NewtonRaphson()) - - @test sol[u] ≈ ones(4) -end - @variables x(t) @parameters a eqs = [0 ~ a * x] @@ -371,17 +352,18 @@ end prob = IntervalNonlinearProblem(sys, (0.0, 2.0), [p => 1.0]) sol = @test_nowarn solve(prob, ITP()) @test SciMLBase.successful_retcode(sol) - @test_nowarn IntervalNonlinearProblemExpr(sys, (0.0, 2.0), [p => 1.0]) + @test_nowarn IntervalNonlinearProblem( + sys, (0.0, 2.0), [p => 1.0]; expression = Val{true}) end @variables y @mtkbuild sys = System([0 ~ x * x - p * x + p, 0 ~ x * y + p]) - @test_throws ["single equation", "unknown"] IntervalNonlinearProblem(sys, (0.0, 1.0)) - @test_throws ["single equation", "unknown"] IntervalNonlinearFunction(sys, (0.0, 1.0)) - @test_throws ["single equation", "unknown"] IntervalNonlinearProblemExpr( - sys, (0.0, 1.0)) - @test_throws ["single equation", "unknown"] IntervalNonlinearFunctionExpr( - sys, (0.0, 1.0)) + @test_throws ["single unknown"] IntervalNonlinearProblem(sys, (0.0, 1.0)) + @test_throws ["single unknown"] IntervalNonlinearFunction(sys) + @test_throws ["single unknown"] IntervalNonlinearProblem( + sys, (0.0, 1.0); expression = Val{true}) + @test_throws ["single unknown"] IntervalNonlinearFunction( + sys; expression = Val{true}) end @testset "Vector parameter used unscalarized and partially scalarized" begin From 966ccea0059189358f9902722ddfe9364ebe14ff Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 14 May 2025 14:05:21 +0530 Subject: [PATCH 1763/2176] test: fix optimizationsystem tests --- test/optimizationsystem.jl | 26 +++++++++----------------- 1 file changed, 9 insertions(+), 17 deletions(-) diff --git a/test/optimizationsystem.jl b/test/optimizationsystem.jl index 2ec9516721..a59cb6421b 100644 --- a/test/optimizationsystem.jl +++ b/test/optimizationsystem.jl @@ -22,14 +22,14 @@ using ModelingToolkit: get_metadata unknowns(combinedsys) parameters(combinedsys) - calculate_gradient(combinedsys) - calculate_hessian(combinedsys) - generate_function(combinedsys) - generate_gradient(combinedsys) - generate_hessian(combinedsys) - hess_sparsity = ModelingToolkit.hessian_sparsity(sys1) + calculate_cost_gradient(combinedsys) + calculate_cost_hessian(combinedsys) + generate_cost(combinedsys) + generate_cost_gradient(combinedsys) + generate_cost_hessian(combinedsys) + hess_sparsity = ModelingToolkit.cost_hessian_sparsity(sys1) sparse_prob = OptimizationProblem(complete(sys1), - [x, y], + [x => 1, y => 1], [a => 0.0, b => 0.0], grad = true, sparse = true) @@ -158,7 +158,7 @@ end constraints = [sys1.x + sys2.y ~ 2], checks = false)) prob = OptimizationProblem(sys, [0.0, 0.0]) @test isequal(constraints(sys), vcat(sys1.x + sys2.y ~ 2, sys1.x ~ 1, sys2.y ~ 1)) - @test isequal(equations(sys), (sys1.x - sys1.a)^2 + (sys2.y - 1 / 2)^2) + @test isequal(cost(sys), (sys1.x - sys1.a)^2 + (sys2.y - 1 / 2)^2) @test isequal(unknowns(sys), [sys1.x, sys2.y]) prob_ = remake(prob, u0 = [1.0, 0.0], p = [2.0]) @@ -177,7 +177,7 @@ end @named sys3 = OptimizationSystem(x3, [x3], [], systems = [sys2]) @named sys4 = OptimizationSystem(x4, [x4], [], systems = [sys3]) - @test isequal(equations(sys4), sys3.sys2.sys1.x1 + sys3.sys2.x2 + sys3.x3 + x4) + @test isequal(cost(sys4), sys3.sys2.sys1.x1 + sys3.sys2.x2 + sys3.x3 + x4) end @testset "time dependent var" begin @@ -299,14 +299,6 @@ end loss = (a - x)^2 + b * (y - x^2)^2 @named sys = OptimizationSystem(loss, [x, y], [a, b], constraints = [x^2 + y^2 ≲ 0.0]) sys = complete(sys) - @test_throws Any OptimizationProblem(sys, - [x => 0.0, y => 0.0], - [a => 1.0, b => 100.0], - lcons = [0.0]) - @test_throws Any OptimizationProblem(sys, - [x => 0.0, y => 0.0], - [a => 1.0, b => 100.0], - ucons = [0.0]) prob = OptimizationProblem(sys, [x => 0.0, y => 0.0], [a => 1.0, b => 100.0]) @test prob.f.expr isa Symbolics.Symbolic From 24a56f93ca75926c1dcbbc7aa9e1c2739184f786 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 14 May 2025 14:05:27 +0530 Subject: [PATCH 1764/2176] test: fix sdesystem tests --- test/sdesystem.jl | 130 +++++++++++++++++++++++----------------------- 1 file changed, 66 insertions(+), 64 deletions(-) diff --git a/test/sdesystem.jl b/test/sdesystem.jl index 96251ceb00..5bdc033498 100644 --- a/test/sdesystem.jl +++ b/test/sdesystem.jl @@ -1,6 +1,7 @@ using ModelingToolkit, StaticArrays, LinearAlgebra using StochasticDiffEq, OrdinaryDiffEq, SparseArrays using Random, Test +using Setfield using Statistics # imported as tt because `t` is used extensively below using ModelingToolkit: t_nounits as tt, D_nounits as D, MTKParameters @@ -19,9 +20,8 @@ noiseeqs = [0.1 * x, # System -> SDESystem shorthand constructor @named sys = System(eqs, tt, [x, y, z], [σ, ρ, β]) -@test SDESystem(sys, noiseeqs, name = :foo) isa SDESystem -@named de = SDESystem(eqs, noiseeqs, tt, [x, y, z], [σ, ρ, β], tspan = (0.0, 10.0)) +@named de = SDESystem(eqs, noiseeqs, tt, [x, y, z], [σ, ρ, β]) de = complete(de) f = eval(generate_diffusion_function(de)[1]) @test f(ones(3), rand(3), nothing) == 0.1ones(3) @@ -105,11 +105,11 @@ f1!(du, u, p, t) = (du .= p[1] * u) prob = SDEProblem(f1!, σ1!, u0, trange, p) # no correction sys = modelingtoolkitize(prob) -fdrift = eval(generate_function(sys)[1]) +fdrift = eval(generate_rhs(sys)[1]) fdif = eval(generate_diffusion_function(sys)[1]) @test fdrift(u0, p, t) == p[1] * u0 @test fdif(u0, p, t) == p[2] * u0 -fdrift! = eval(generate_function(sys)[2]) +fdrift! = eval(generate_rhs(sys)[2]) fdif! = eval(generate_diffusion_function(sys)[2]) du = similar(u0) fdrift!(du, u0, p, t) @@ -119,11 +119,11 @@ fdif!(du, u0, p, t) # Ito -> Strat sys2 = stochastic_integral_transform(sys, -1 // 2) -fdrift = eval(generate_function(sys2)[1]) +fdrift = eval(generate_rhs(sys2)[1]) fdif = eval(generate_diffusion_function(sys2)[1]) @test fdrift(u0, p, t) == p[1] * u0 - 1 // 2 * p[2]^2 * u0 @test fdif(u0, p, t) == p[2] * u0 -fdrift! = eval(generate_function(sys2)[2]) +fdrift! = eval(generate_rhs(sys2)[2]) fdif! = eval(generate_diffusion_function(sys2)[2]) du = similar(u0) fdrift!(du, u0, p, t) @@ -133,11 +133,11 @@ fdif!(du, u0, p, t) # Strat -> Ito sys2 = stochastic_integral_transform(sys, 1 // 2) -fdrift = eval(generate_function(sys2)[1]) +fdrift = eval(generate_rhs(sys2)[1]) fdif = eval(generate_diffusion_function(sys2)[1]) @test fdrift(u0, p, t) == p[1] * u0 + 1 // 2 * p[2]^2 * u0 @test fdif(u0, p, t) == p[2] * u0 -fdrift! = eval(generate_function(sys2)[2]) +fdrift! = eval(generate_rhs(sys2)[2]) fdif! = eval(generate_diffusion_function(sys2)[2]) du = similar(u0) fdrift!(du, u0, p, t) @@ -153,11 +153,11 @@ u0 = rand(1) prob = SDEProblem(f2!, σ2!, u0, trange) # no correction sys = modelingtoolkitize(prob) -fdrift = eval(generate_function(sys)[1]) +fdrift = eval(generate_rhs(sys)[1]) fdif = eval(generate_diffusion_function(sys)[1]) @test fdrift(u0, p, t) == @. sin(t) + cos(u0) @test fdif(u0, p, t) == pi .+ atan.(u0) -fdrift! = eval(generate_function(sys)[2]) +fdrift! = eval(generate_rhs(sys)[2]) fdif! = eval(generate_diffusion_function(sys)[2]) du = similar(u0) fdrift!(du, u0, p, t) @@ -167,11 +167,11 @@ fdif!(du, u0, p, t) # Ito -> Strat sys2 = stochastic_integral_transform(sys, -1 // 2) -fdrift = eval(generate_function(sys2)[1]) +fdrift = eval(generate_rhs(sys2)[1]) fdif = eval(generate_diffusion_function(sys2)[1]) @test fdrift(u0, p, t) == @. sin(t) + cos(u0) - 1 // 2 * 1 / (1 + u0^2) * (pi + atan(u0)) @test fdif(u0, p, t) == pi .+ atan.(u0) -fdrift! = eval(generate_function(sys2)[2]) +fdrift! = eval(generate_rhs(sys2)[2]) fdif! = eval(generate_diffusion_function(sys2)[2]) du = similar(u0) fdrift!(du, u0, p, t) @@ -181,11 +181,11 @@ fdif!(du, u0, p, t) # Strat -> Ito sys2 = stochastic_integral_transform(sys, 1 // 2) -fdrift = eval(generate_function(sys2)[1]) +fdrift = eval(generate_rhs(sys2)[1]) fdif = eval(generate_diffusion_function(sys2)[1]) @test fdrift(u0, p, t) ≈ @. sin(t) + cos(u0) + 1 // 2 * 1 / (1 + u0^2) * (pi + atan(u0)) @test fdif(u0, p, t) == pi .+ atan.(u0) -fdrift! = eval(generate_function(sys2)[2]) +fdrift! = eval(generate_rhs(sys2)[2]) fdif! = eval(generate_diffusion_function(sys2)[2]) du = similar(u0) fdrift!(du, u0, p, t) @@ -210,11 +210,11 @@ end prob = SDEProblem(f3!, σ3!, u0, trange, p) # no correction sys = modelingtoolkitize(prob) -fdrift = eval(generate_function(sys)[1]) +fdrift = eval(generate_rhs(sys)[1]) fdif = eval(generate_diffusion_function(sys)[1]) @test fdrift(u0, p, t) == u0 / 2 @test fdif(u0, p, t) == reverse(u0) -fdrift! = eval(generate_function(sys)[2]) +fdrift! = eval(generate_rhs(sys)[2]) fdif! = eval(generate_diffusion_function(sys)[2]) du = similar(u0) fdrift!(du, u0, p, t) @@ -224,11 +224,11 @@ fdif!(du, u0, p, t) # Ito -> Strat sys2 = stochastic_integral_transform(sys, -1 // 2) -fdrift = eval(generate_function(sys2)[1]) +fdrift = eval(generate_rhs(sys2)[1]) fdif = eval(generate_diffusion_function(sys2)[1]) @test fdrift(u0, p, t) == u0 * 0 @test fdif(u0, p, t) == reverse(u0) -fdrift! = eval(generate_function(sys2)[2]) +fdrift! = eval(generate_rhs(sys2)[2]) fdif! = eval(generate_diffusion_function(sys2)[2]) du = similar(u0) fdrift!(du, u0, p, t) @@ -238,11 +238,11 @@ fdif!(du, u0, p, t) # Strat -> Ito sys2 = stochastic_integral_transform(sys, 1 // 2) -fdrift = eval(generate_function(sys2)[1]) +fdrift = eval(generate_rhs(sys2)[1]) fdif = eval(generate_diffusion_function(sys2)[1]) @test fdrift(u0, p, t) == u0 @test fdif(u0, p, t) == reverse(u0) -fdrift! = eval(generate_function(sys2)[2]) +fdrift! = eval(generate_rhs(sys2)[2]) fdif! = eval(generate_diffusion_function(sys2)[2]) du = similar(u0) fdrift!(du, u0, p, t) @@ -260,11 +260,11 @@ f1(u, p, t) = p[1] * u prob = SDEProblem(f1, σ1, u0, trange, p) # no correction sys = modelingtoolkitize(prob) -fdrift = eval(generate_function(sys)[1]) +fdrift = eval(generate_rhs(sys)[1]) fdif = eval(generate_diffusion_function(sys)[1]) @test fdrift(u0, p, t) == p[1] * u0 @test fdif(u0, p, t) == p[2] * u0 -fdrift! = eval(generate_function(sys)[2]) +fdrift! = eval(generate_rhs(sys)[2]) fdif! = eval(generate_diffusion_function(sys)[2]) du = similar(u0) fdrift!(du, u0, p, t) @@ -274,11 +274,11 @@ fdif!(du, u0, p, t) # Ito -> Strat sys2 = stochastic_integral_transform(sys, -1 // 2) -fdrift = eval(generate_function(sys2)[1]) +fdrift = eval(generate_rhs(sys2)[1]) fdif = eval(generate_diffusion_function(sys2)[1]) @test fdrift(u0, p, t) == p[1] * u0 - 1 // 2 * p[2]^2 * u0 @test fdif(u0, p, t) == p[2] * u0 -fdrift! = eval(generate_function(sys2)[2]) +fdrift! = eval(generate_rhs(sys2)[2]) fdif! = eval(generate_diffusion_function(sys2)[2]) du = similar(u0) fdrift!(du, u0, p, t) @@ -288,11 +288,11 @@ fdif!(du, u0, p, t) # Strat -> Ito sys2 = stochastic_integral_transform(sys, 1 // 2) -fdrift = eval(generate_function(sys2)[1]) +fdrift = eval(generate_rhs(sys2)[1]) fdif = eval(generate_diffusion_function(sys2)[1]) @test fdrift(u0, p, t) == p[1] * u0 + 1 // 2 * p[2]^2 * u0 @test fdif(u0, p, t) == p[2] * u0 -fdrift! = eval(generate_function(sys2)[2]) +fdrift! = eval(generate_rhs(sys2)[2]) fdif! = eval(generate_diffusion_function(sys2)[2]) du = similar(u0) fdrift!(du, u0, p, t) @@ -316,12 +316,12 @@ end prob = SDEProblem(f4!, g4!, u0, trange, noise_rate_prototype = zeros(2, 2), p) # no correction sys = modelingtoolkitize(prob) -fdrift = eval(generate_function(sys)[1]) +fdrift = eval(generate_rhs(sys)[1]) fdif = eval(generate_diffusion_function(sys)[1]) @test fdrift(u0, p, t) == p[1] * u0 @test fdif(u0, p, t) == [p[2]*u0[1] p[3]*u0[1] p[4]*u0[1] p[5]*u0[2]] -fdrift! = eval(generate_function(sys)[2]) +fdrift! = eval(generate_rhs(sys)[2]) fdif! = eval(generate_diffusion_function(sys)[2]) du = similar(u0) fdrift!(du, u0, p, t) @@ -333,7 +333,7 @@ fdif!(du, u0, p, t) # Ito -> Strat sys2 = stochastic_integral_transform(sys, -1 // 2) -fdrift = eval(generate_function(sys2)[1]) +fdrift = eval(generate_rhs(sys2)[1]) fdif = eval(generate_diffusion_function(sys2)[1]) @test fdrift(u0, p, t) ≈ [ p[1] * u0[1] - 1 // 2 * (p[2]^2 * u0[1] + p[3]^2 * u0[1]), @@ -341,7 +341,7 @@ fdif = eval(generate_diffusion_function(sys2)[1]) ] @test fdif(u0, p, t) == [p[2]*u0[1] p[3]*u0[1] p[4]*u0[1] p[5]*u0[2]] -fdrift! = eval(generate_function(sys2)[2]) +fdrift! = eval(generate_rhs(sys2)[2]) fdif! = eval(generate_diffusion_function(sys2)[2]) du = similar(u0) fdrift!(du, u0, p, t) @@ -356,7 +356,7 @@ fdif!(du, u0, p, t) # Strat -> Ito sys2 = stochastic_integral_transform(sys, 1 // 2) -fdrift = eval(generate_function(sys2)[1]) +fdrift = eval(generate_rhs(sys2)[1]) fdif = eval(generate_diffusion_function(sys2)[1]) @test fdrift(u0, p, t) ≈ [ p[1] * u0[1] + 1 // 2 * (p[2]^2 * u0[1] + p[3]^2 * u0[1]), @@ -364,7 +364,7 @@ fdif = eval(generate_diffusion_function(sys2)[1]) ] @test fdif(u0, p, t) == [p[2]*u0[1] p[3]*u0[1] p[4]*u0[1] p[5]*u0[2]] -fdrift! = eval(generate_function(sys2)[2]) +fdrift! = eval(generate_rhs(sys2)[2]) fdif! = eval(generate_diffusion_function(sys2)[2]) du = similar(u0) fdrift!(du, u0, p, t) @@ -397,13 +397,13 @@ end prob = SDEProblem(f5!, g5!, u0, trange, noise_rate_prototype = zeros(2, 4), p) # no correction sys = modelingtoolkitize(prob) -fdrift = eval(generate_function(sys)[1]) +fdrift = eval(generate_rhs(sys)[1]) fdif = eval(generate_diffusion_function(sys)[1]) @test fdrift(u0, p, t) == 0 * u0 @test fdif(u0, p, t) == [cos(p[1])*sin(u0[1]) cos(p[1])*cos(u0[1]) -sin(p[1])*sin(u0[2]) -sin(p[1])*cos(u0[2]) sin(p[1])*sin(u0[1]) sin(p[1])*cos(u0[1]) cos(p[1])*sin(u0[2]) cos(p[1])*cos(u0[2])] -fdrift! = eval(generate_function(sys)[2]) +fdrift! = eval(generate_rhs(sys)[2]) fdif! = eval(generate_diffusion_function(sys)[2]) du = similar(u0) fdrift!(du, u0, p, t) @@ -416,13 +416,13 @@ fdif!(du, u0, p, t) # Ito -> Strat sys2 = stochastic_integral_transform(sys, -1 // 2) -fdrift = eval(generate_function(sys2)[1]) +fdrift = eval(generate_rhs(sys2)[1]) fdif = eval(generate_diffusion_function(sys2)[1]) @test fdrift(u0, p, t) == 0 * u0 @test fdif(u0, p, t) == [cos(p[1])*sin(u0[1]) cos(p[1])*cos(u0[1]) -sin(p[1])*sin(u0[2]) -sin(p[1])*cos(u0[2]) sin(p[1])*sin(u0[1]) sin(p[1])*cos(u0[1]) cos(p[1])*sin(u0[2]) cos(p[1])*cos(u0[2])] -fdrift! = eval(generate_function(sys2)[2]) +fdrift! = eval(generate_rhs(sys2)[2]) fdif! = eval(generate_diffusion_function(sys2)[2]) du = similar(u0) fdrift!(du, u0, p, t) @@ -435,13 +435,13 @@ fdif!(du, u0, p, t) # Strat -> Ito sys2 = stochastic_integral_transform(sys, 1 // 2) -fdrift = eval(generate_function(sys2)[1]) +fdrift = eval(generate_rhs(sys2)[1]) fdif = eval(generate_diffusion_function(sys2)[1]) @test fdrift(u0, p, t) == 0 * u0 @test fdif(u0, p, t) == [cos(p[1])*sin(u0[1]) cos(p[1])*cos(u0[1]) -sin(p[1])*sin(u0[2]) -sin(p[1])*cos(u0[2]) sin(p[1])*sin(u0[1]) sin(p[1])*cos(u0[1]) cos(p[1])*sin(u0[2]) cos(p[1])*cos(u0[2])] -fdrift! = eval(generate_function(sys2)[2]) +fdrift! = eval(generate_rhs(sys2)[2]) fdif! = eval(generate_diffusion_function(sys2)[2]) du = similar(u0) fdrift!(du, u0, p, t) @@ -463,7 +463,8 @@ fdif!(du, u0, p, t) x - y] sys1 = SDESystem(eqs_short, noise_eqs, t, [x, y, z], [σ, ρ, β], name = :sys1) sys2 = SDESystem(eqs_short, noise_eqs, t, [x, y, z], [σ, ρ, β], name = :sys1) - @test_throws ArgumentError SDESystem([sys2.y ~ sys1.z], [sys2.y], t, [], [], + @test_throws ModelingToolkit.NonUniqueSubsystemsError SDESystem( + [sys2.y ~ sys1.z], [sys2.y], t, [], [], systems = [sys1, sys2], name = :foo) end @@ -608,6 +609,8 @@ diffusion_eqs = [s*x 0 sys2 = SDESystem(drift_eqs, diffusion_eqs, tt, sts, ps, name = :sys1) sys2 = complete(sys2) +@set! sys1.parent = nothing +@set! sys2.parent = nothing @test sys1 == sys2 prob = SDEProblem(sys1, sts .=> [1.0, 0.0, 0.0], @@ -621,7 +624,8 @@ solve(prob, LambaEulerHeun(), seed = 1) @variables X(t) eqs = [D(X) ~ p - d * X] noise_eqs = [sqrt(p), -sqrt(d * X)] -@test_throws ArgumentError SDESystem(eqs, noise_eqs, t, [X], [p, d]; name = :ssys) +@test_throws ModelingToolkit.IllFormedNoiseEquationsError SDESystem( + eqs, noise_eqs, t, [X], [p, d]; name = :ssys) noise_eqs = reshape([sqrt(p), -sqrt(d * X)], 1, 2) ssys = SDESystem(eqs, noise_eqs, t, [X], [p, d]; name = :ssys) @@ -745,7 +749,7 @@ end @test_throws ErrorException solve(prob, SOSRI()).retcode==ReturnCode.Success # ImplicitEM does work for non-diagonal noise @test solve(prob, ImplicitEM()).retcode == ReturnCode.Success - @test size(ModelingToolkit.get_noiseeqs(de)) == (3, 6) + @test size(ModelingToolkit.get_noise_eqs(de)) == (3, 6) end @testset "Diagonal noise, less brownians than equations" begin @@ -795,7 +799,7 @@ end sys = System(eqs, t, sts, ps; name = :name) sys = structural_simplify(sys) - @test ModelingToolkit.get_noiseeqs(sys) ≈ [1.0] + @test ModelingToolkit.get_noise_eqs(sys) ≈ [1.0] prob = SDEProblem(sys, [], (0.0, 1.0), []) @test_nowarn solve(prob, RKMil()) end @@ -804,7 +808,6 @@ end @variables x(t) y(t) z(t) @brownian a @mtkbuild sys = System([D(x) ~ x + a, D(y) ~ y + a, z ~ x + y], t) - @test sys isa SDESystem @test length(observed(sys)) == 1 prob = SDEProblem(sys, [x => 1.0, y => 1.0], (0.0, 1.0)) @test prob[z] ≈ 2.0 @@ -815,11 +818,11 @@ end @testset "Scalar noise" begin @named sys = SDESystem([D(x) ~ x, D(y) ~ y, z ~ x + y], [x, y, 3], t, [x, y, z], [], is_scalar_noise = true) - odesys = System(sys) - @test odesys isa System + odesys = noise_to_brownians(sys) vs = ModelingToolkit.vars(equations(odesys)) nbrownian = count( v -> ModelingToolkit.getvariabletype(v) == ModelingToolkit.BROWNIAN, vs) + @test length(brownians(odesys)) == 3 @test nbrownian == 3 for eq in equations(odesys) ModelingToolkit.isdiffeq(eq) || continue @@ -828,10 +831,9 @@ end end @testset "Non-scalar vector noise" begin - @named sys = SDESystem([D(x) ~ x, D(y) ~ y, z ~ x + y], [x, y, 0], - t, [x, y, z], [], is_scalar_noise = false) - odesys = System(sys) - @test odesys isa System + @named sys = SDESystem([D(x) ~ x, D(y) ~ y, z ~ x + y], [x; y; 0;;], + t, [x, y, z], []; is_scalar_noise = false) + odesys = noise_to_brownians(sys) vs = ModelingToolkit.vars(equations(odesys)) nbrownian = count( v -> ModelingToolkit.getvariabletype(v) == ModelingToolkit.BROWNIAN, vs) @@ -847,8 +849,7 @@ end 2y 2z 2x z+1 x+1 y+1] @named sys = SDESystem([D(x) ~ x, D(y) ~ y, D(z) ~ z], noiseeqs, t, [x, y, z], []) - odesys = System(sys) - @test odesys isa System + odesys = noise_to_brownians(sys) vs = ModelingToolkit.vars(equations(odesys)) nbrownian = count( v -> ModelingToolkit.getvariabletype(v) == ModelingToolkit.BROWNIAN, vs) @@ -862,10 +863,9 @@ end @testset "`structural_simplify(::SDESystem)`" begin @variables x(t) y(t) @mtkbuild sys = SDESystem( - [D(x) ~ x, y ~ 2x], [x, 0], t, [x, y], []; is_scalar_noise = true) - @test sys isa SDESystem + [D(x) ~ x, y ~ 2x], [x, 0], t, [x, y], []) @test length(equations(sys)) == 1 - @test length(ModelingToolkit.get_noiseeqs(sys)) == 1 + @test length(ModelingToolkit.get_noise_eqs(sys)) == 1 @test length(observed(sys)) == 1 end @@ -875,9 +875,11 @@ end @variables X(t)::Int64 @brownian z eq2 = D(X) ~ p - d * X + z - @test_throws ArgumentError @mtkbuild ssys = System([eq2], t) + @test_throws ModelingToolkit.ContinuousOperatorDiscreteArgumentError @mtkbuild ssys = System( + [eq2], t) noiseeq = [1] - @test_throws ArgumentError @named ssys = SDESystem([eq2], [noiseeq], t) + @test_throws ModelingToolkit.ContinuousOperatorDiscreteArgumentError @named ssys = SDESystem( + [eq2], [noiseeq], t) end @testset "SDEFunctionExpr" begin @@ -894,30 +896,30 @@ end @named sys = System(eqs, tt, [x, y, z], [σ, ρ, β]) - @named de = SDESystem(eqs, noiseeqs, tt, [x, y, z], [σ, ρ, β], tspan = (0.0, 10.0)) + @named de = SDESystem(eqs, noiseeqs, tt, [x, y, z], [σ, ρ, β]) de = complete(de) - f = SDEFunctionExpr(de) + f = SDEFunction(de; expression = Val{true}) @test f isa Expr @testset "Configuration Tests" begin # Test with `tgrad` - f_tgrad = SDEFunctionExpr(de; tgrad = true) + f_tgrad = SDEFunction(de; tgrad = true, expression = Val{true}) @test f_tgrad isa Expr # Test with `jac` - f_jac = SDEFunctionExpr(de; jac = true) + f_jac = SDEFunction(de; jac = true, expression = Val{true}) @test f_jac isa Expr # Test with sparse Jacobian - f_sparse = SDEFunctionExpr(de; sparse = true) + f_sparse = SDEFunction(de; sparse = true, expression = Val{true}) @test f_sparse isa Expr end @testset "Ordering Tests" begin dvs = [z, y, x] ps = [β, ρ, σ] - f_order = SDEFunctionExpr(de, dvs, ps) + f_order = SDEFunction(de; expression = Val{true}) @test f_order isa Expr end end @@ -947,7 +949,7 @@ end @test ssys1 !== ssys2 end -@testset "Error when constructing SDESystem without `structural_simplify`" begin +@testset "Error when constructing SDEProblem without `structural_simplify`" begin @parameters σ ρ β @variables x(tt) y(tt) z(tt) @brownian a @@ -961,7 +963,7 @@ end u0map = [x => 1.0, y => 0.0, z => 0.0] parammap = [σ => 10.0, β => 26.0, ρ => 2.33] - @test_throws ErrorException("SDESystem constructed by defining Brownian variables with @brownian must be simplified by calling `structural_simplify` before a SDEProblem can be constructed.") SDEProblem( + @test_throws ["Brownian", "structural_simplify"] SDEProblem( de, u0map, (0.0, 100.0), parammap) de = structural_simplify(de) @test SDEProblem(de, u0map, (0.0, 100.0), parammap) isa SDEProblem From c871e5221b0094aad46f61fdeab4fa052b3b708e Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 14 May 2025 14:05:54 +0530 Subject: [PATCH 1765/2176] fix: unwrap in `add_toterms!`, don't override existing values --- src/systems/problem_utils.jl | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/systems/problem_utils.jl b/src/systems/problem_utils.jl index 912496fc69..84a77364ae 100644 --- a/src/systems/problem_utils.jl +++ b/src/systems/problem_utils.jl @@ -37,7 +37,8 @@ the old value should be removed. """ function add_toterms!(varmap::AbstractDict; toterm = default_toterm, replace = false) for k in collect(keys(varmap)) - ttk = toterm(k) + ttk = toterm(unwrap(k)) + haskey(varmap, ttk) && continue varmap[ttk] = varmap[k] !isequal(k, ttk) && replace && delete!(varmap, k) end From 192dff72099c6495fd19439152b090f7e39346a3 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 14 May 2025 14:06:13 +0530 Subject: [PATCH 1766/2176] fix: unwrap `varmap` and add toterms in `better_varmap_to_vars` --- src/systems/problem_utils.jl | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/systems/problem_utils.jl b/src/systems/problem_utils.jl index 84a77364ae..8562bb1cfa 100644 --- a/src/systems/problem_utils.jl +++ b/src/systems/problem_utils.jl @@ -352,6 +352,8 @@ function better_varmap_to_vars(varmap::AbstractDict, vars::Vector; allow_symbolic = false, is_initializeprob = false) isempty(vars) && return nothing + varmap = recursive_unwrap(varmap) + add_toterms!(varmap; toterm) if check missing_vars = missingvars(varmap, vars; toterm) isempty(missing_vars) || throw(MissingVariablesError(missing_vars)) From 1e10954b516afdd7c8ca2d8871f1c0f1c28f59d8 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 15 May 2025 00:41:46 +0530 Subject: [PATCH 1767/2176] test: update mtkparameters tests --- test/mtkparameters.jl | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/test/mtkparameters.jl b/test/mtkparameters.jl index 122b7acdd1..347043d437 100644 --- a/test/mtkparameters.jl +++ b/test/mtkparameters.jl @@ -181,8 +181,8 @@ function level1() D(y) ~ -p3 * y + p4 * x * y] sys = structural_simplify(complete(System( - eqs, t, tspan = (0, 3.0), name = :sys, parameter_dependencies = [y0 => 2p4]))) - prob = ODEProblem{true, SciMLBase.FullSpecialize}(sys) + eqs, t, name = :sys, parameter_dependencies = [y0 => 2p4]))) + prob = ODEProblem{true, SciMLBase.FullSpecialize}(sys, [], (0.0, 3.0)) end # scalar and vector parameters @@ -195,8 +195,8 @@ function level2() D(y) ~ -p23[2] * y + p4 * x * y] sys = structural_simplify(complete(System( - eqs, t, tspan = (0, 3.0), name = :sys, parameter_dependencies = [y0 => 2p4]))) - prob = ODEProblem{true, SciMLBase.FullSpecialize}(sys) + eqs, t, name = :sys, parameter_dependencies = [y0 => 2p4]))) + prob = ODEProblem{true, SciMLBase.FullSpecialize}(sys, [], (0.0, 3.0)) end # scalar and vector parameters with different scalar types @@ -209,8 +209,8 @@ function level3() D(y) ~ -p23[2] * y + p4 * x * y] sys = structural_simplify(complete(System( - eqs, t, tspan = (0, 3.0), name = :sys, parameter_dependencies = [y0 => 2p4]))) - prob = ODEProblem{true, SciMLBase.FullSpecialize}(sys) + eqs, t, name = :sys, parameter_dependencies = [y0 => 2p4]))) + prob = ODEProblem{true, SciMLBase.FullSpecialize}(sys, [], (0.0, 3.0)) end @testset "level$i" for (i, prob) in enumerate([level1(), level2(), level3()]) From c2f0424ade618818b9ad436e4422cfab84c7a4eb Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 15 May 2025 00:41:55 +0530 Subject: [PATCH 1768/2176] test: update serialization tests --- test/serialization.jl | 24 +++--------------------- 1 file changed, 3 insertions(+), 21 deletions(-) diff --git a/test/serialization.jl b/test/serialization.jl index cce015c776..1a0105d155 100644 --- a/test/serialization.jl +++ b/test/serialization.jl @@ -8,8 +8,8 @@ sys = complete(sys) for prob in [ eval(ModelingToolkit.ODEProblem{false}(sys, nothing, nothing, SciMLBase.NullParameters())), - eval(ModelingToolkit.ODEProblemExpr{false}(sys, nothing, nothing, - SciMLBase.NullParameters())) + eval(ModelingToolkit.ODEProblem{false}(sys, nothing, nothing, + SciMLBase.NullParameters(); expression = Val{true})) ] _fn = tempname() @@ -47,26 +47,8 @@ sol_ = solve(prob_, ImplicitEuler()) ## Check ODEProblemExpr with Observables ----------- # build the observable function expression -obs_exps = [] -for var in all_obs - f = ModelingToolkit.build_explicit_observed_function(ss, var; expression = true) - sym = ModelingToolkit.getname(var) |> string - ex = :(if name == Symbol($sym) - return $f(u0, p, t) - end) - push!(obs_exps, ex) -end -# observedfun expression for ODEFunctionExpr -observedfun_exp = :(function obs(var, u0, p, t) - if var isa AbstractArray - return obs.(var, (u0,), (p,), (t,)) - end - name = ModelingToolkit.getname(var) - $(obs_exps...) -end) - # ODEProblemExpr with observedfun_exp included -probexpr = ODEProblemExpr{true}(ss, [capacitor.v => 0.0], (0, 0.1); observedfun_exp); +probexpr = ODEProblem{true}(ss, [capacitor.v => 0.0], (0, 0.1); expr = Val{true}); prob_obs = eval(probexpr) sol_obs = solve(prob_obs, ImplicitEuler()) @show all_obs From cf61fe3abba26c8d97dba5b21b1919ecf5116b74 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 15 May 2025 00:42:22 +0530 Subject: [PATCH 1769/2176] test: fix symbolic indexing interface tests --- test/symbolic_indexing_interface.jl | 27 ++++++++++++--------------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/test/symbolic_indexing_interface.jl b/test/symbolic_indexing_interface.jl index 9c478565fa..14cf0c10a7 100644 --- a/test/symbolic_indexing_interface.jl +++ b/test/symbolic_indexing_interface.jl @@ -179,21 +179,18 @@ end @test isequal(parameters(pdesys), [h]) end -# Issue#2767 -using ModelingToolkit -using ModelingToolkit: t_nounits as t, D_nounits as D -using SymbolicIndexingInterface - -@parameters p1[1:2]=[1.0, 2.0] p2[1:2]=[0.0, 0.0] -@variables x(t) = 0 - -@named sys = System( - [D(x) ~ sum(p1) * t + sum(p2)], - t; -) -prob = ODEProblem(complete(sys)) -get_dep = @test_nowarn getu(prob, 2p1) -@test get_dep(prob) == [2.0, 4.0] +@testset "Issue#2767" begin + @parameters p1[1:2]=[1.0, 2.0] p2[1:2]=[0.0, 0.0] + @variables x(t) = 0 + + @named sys = System( + [D(x) ~ sum(p1) * t + sum(p2)], + t; + ) + prob = ODEProblem(complete(sys), [], (0.0, 1.0)) + get_dep = @test_nowarn getu(prob, 2p1) + @test get_dep(prob) == [2.0, 4.0] +end @testset "Observed functions with variables as `Symbol`s" begin @variables x(t) y(t) z(t)[1:2] From 573146e102f21144f5133b00c970fed1841e043f Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 15 May 2025 00:43:02 +0530 Subject: [PATCH 1770/2176] fix: fix `Pre` parameter discovery for `AffectSystem` --- src/systems/callbacks.jl | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/systems/callbacks.jl b/src/systems/callbacks.jl index d0bd18a65a..631e9fe216 100644 --- a/src/systems/callbacks.jl +++ b/src/systems/callbacks.jl @@ -285,12 +285,17 @@ function make_affect(affect::Vector{Equation}; discrete_parameters = Any[], dvs = OrderedSet() params = OrderedSet() + _varsbuf = Set() for eq in affect if !haspre(eq) && !(symbolic_type(eq.rhs) === NotSymbolic() || symbolic_type(eq.lhs) === NotSymbolic()) @warn "Affect equation $eq has no `Pre` operator. As such it will be interpreted as an algebraic equation to be satisfied after the callback. If you intended to use the value of a variable x before the affect, use Pre(x). Errors may be thrown if there is no `Pre` and the algebraic equation is unsatisfiable, such as X ~ X + 1." end collect_vars!(dvs, params, eq, iv; op = Pre) + empty!(_varsbuf) + vars!(_varsbuf, eq; op = Pre) + filter!(x -> iscall(x) && operation(x) isa Pre, _varsbuf) + union!(params, _varsbuf) diffvs = collect_applied_operators(eq, Differential) union!(dvs, diffvs) end From f3d62880fd8a695079db6d23079a90d9ac0484a0 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 15 May 2025 00:43:53 +0530 Subject: [PATCH 1771/2176] fix: fix `compile_condition`, respect `eval_expression` and `eval_module` --- src/systems/callbacks.jl | 52 ++++++++++++++++++++++++++-------------- 1 file changed, 34 insertions(+), 18 deletions(-) diff --git a/src/systems/callbacks.jl b/src/systems/callbacks.jl index 631e9fe216..365f3c4eab 100644 --- a/src/systems/callbacks.jl +++ b/src/systems/callbacks.jl @@ -596,6 +596,33 @@ Base.isempty(cb::AbstractCallback) = isempty(cb.conditions) #################################### ####### Compilation functions ###### #################################### + +struct CompiledCondition{IsDiscrete, F} + f::F +end + +function CompiledCondition{ID}(f::F) where {ID, F} + return CompiledCondition{ID, F}(f) +end + +function (cc::CompiledCondition)(out, u, t, integ) + cc.f(out, u, parameter_values(integ), t) +end + +function (cc::CompiledCondition{false})(u, t, integ) + if DiffEqBase.isinplace(SciMLBase.get_sol(integ).prob) + tmp, = DiffEqBase.get_tmp_cache(integ) + cc.f(tmp, u, parameter_values(integ), t) + tmp[1] + else + cc.f(u, parameter_values(integ), t) + end +end + +function (cc::CompiledCondition{true})(u, t, integ) + cc.f(u, parameter_values(integ), t) +end + """ compile_condition(cb::AbstractCallback, sys, dvs, ps; expression, kwargs...) @@ -615,30 +642,19 @@ function compile_condition( end if !is_discrete(cbs) - condit = reduce(vcat, flatten_equations(condit)) + condit = reduce(vcat, flatten_equations(Vector{Equation}(condit))) condit = condit isa AbstractVector ? [c.lhs - c.rhs for c in condit] : [condit.lhs - condit.rhs] end fs = build_function_wrapper( - sys, condit, u, p..., t; kwargs..., expression = Val{false}, cse = false) - (f_oop, f_iip) = is_discrete(cbs) ? (fs, nothing) : fs - - cond = if cbs isa AbstractVector - (out, u, t, integ) -> f_iip(out, u, parameter_values(integ), t) - elseif is_discrete(cbs) - (u, t, integ) -> f_oop(u, parameter_values(integ), t) - else - function (u, t, integ) - if DiffEqBase.isinplace(SciMLBase.get_sol(integ).prob) - tmp, = DiffEqBase.get_tmp_cache(integ) - f_iip(tmp, u, parameter_values(integ), t) - tmp[1] - else - f_oop(u, parameter_values(integ), t) - end - end + sys, condit, u, p..., t; kwargs..., cse = false) + if is_discrete(cbs) + fs = (fs, nothing) end + fs = GeneratedFunctionWrapper{(2, 3, is_split(sys))}( + Val{false}, fs...; eval_expression, eval_module) + return CompiledCondition{is_discrete(cbs)}(fs) end """ From 0d8fcb8bf49460dba4642e65d752b83ebfa14f7b Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 15 May 2025 00:44:27 +0530 Subject: [PATCH 1772/2176] fix: respect `eval_expression`, `eval_module` in `compile_equational_affect` --- src/systems/callbacks.jl | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/systems/callbacks.jl b/src/systems/callbacks.jl index 365f3c4eab..0bb2318e4b 100644 --- a/src/systems/callbacks.jl +++ b/src/systems/callbacks.jl @@ -895,7 +895,8 @@ end Compile an affect defined by a set of equations. Systems with algebraic equations will solve implicit discrete problems to obtain their next state. Systems without will generate functions that perform explicit updates. """ function compile_equational_affect( - aff::Union{AffectSystem, Vector{Equation}}, sys; reset_jumps = false, kwargs...) + aff::Union{AffectSystem, Vector{Equation}}, sys; reset_jumps = false, + eval_expression = false, eval_module = @__MODULE__, kwargs...) if aff isa AbstractVector aff = make_affect( aff; iv = get_iv(sys), warn_no_algebraic = false) @@ -930,11 +931,13 @@ function compile_equational_affect( integ = gensym(:MTKIntegrator) u_up, u_up! = build_function_wrapper(sys, (@view rhss[is_u]), dvs, _ps..., t; - wrap_code = add_integrator_header(sys, integ, :u), - expression = Val{false}, outputidxs = u_idxs, wrap_mtkparameters, cse = false) + wrap_code = add_integrator_header(sys, integ, :u), expression = Val{false}, + outputidxs = u_idxs, wrap_mtkparameters, cse = false, eval_expression, + eval_module) p_up, p_up! = build_function_wrapper(sys, (@view rhss[is_p]), dvs, _ps..., t; - wrap_code = add_integrator_header(sys, integ, :p), - expression = Val{false}, outputidxs = p_idxs, wrap_mtkparameters, cse = false) + wrap_code = add_integrator_header(sys, integ, :p), expression = Val{false}, + outputidxs = p_idxs, wrap_mtkparameters, cse = false, eval_expression, + eval_module) return let dvs_to_update = dvs_to_update, ps_to_update = ps_to_update, reset_jumps = reset_jumps, u_up! = u_up!, p_up! = p_up! @@ -963,7 +966,8 @@ function compile_equational_affect( affprob = ImplicitDiscreteProblem(affsys, [dv => 0 for dv in unknowns(affsys)], (0, 0), [p => 0.0 for p in parameters(affsys)]; - build_initializeprob = false, check_length = false) + build_initializeprob = false, check_length = false, eval_expression, + eval_module, check_compatibility = false) function implicit_affect!(integ) new_u0 = affu_getter(integ) From 3a7a54ea2aa1663149f586b095c259f62263010a Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 15 May 2025 00:44:34 +0530 Subject: [PATCH 1773/2176] test: fix symbolic event tests --- test/symbolic_events.jl | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/test/symbolic_events.jl b/test/symbolic_events.jl index 1d611360a3..ee0eaf6392 100644 --- a/test/symbolic_events.jl +++ b/test/symbolic_events.jl @@ -238,11 +238,11 @@ end cb = ModelingToolkit.generate_continuous_callbacks(sys) cond = cb.condition out = [0.0] - cond.f_iip(out, [0], p0, t0) + cond.f(out, [0], p0, t0) @test out[] ≈ -1 # signature is u,p,t - cond.f_iip(out, [1], p0, t0) + cond.f(out, [1], p0, t0) @test out[] ≈ 0 # signature is u,p,t - cond.f_iip(out, [2], p0, t0) + cond.f(out, [2], p0, t0) @test out[] ≈ 1 # signature is u,p,t prob = ODEProblem(sys, Pair[], (0.0, 2.0)) @@ -270,20 +270,20 @@ end cond = cb.condition out = [0.0, 0.0] # the root to find is 2 - cond.f_iip(out, [0, 0], p0, t0) + cond.f(out, [0, 0], p0, t0) @test out[1] ≈ -2 # signature is u,p,t - cond.f_iip(out, [1, 0], p0, t0) + cond.f(out, [1, 0], p0, t0) @test out[1] ≈ -1 # signature is u,p,t - cond.f_iip(out, [2, 0], p0, t0) # this should return 0 + cond.f(out, [2, 0], p0, t0) # this should return 0 @test out[1] ≈ 0 # signature is u,p,t # the root to find is 1 out = [0.0, 0.0] - cond.f_iip(out, [0, 0], p0, t0) + cond.f(out, [0, 0], p0, t0) @test out[2] ≈ -1 # signature is u,p,t - cond.f_iip(out, [0, 1], p0, t0) # this should return 0 + cond.f(out, [0, 1], p0, t0) # this should return 0 @test out[2] ≈ 0 # signature is u,p,t - cond.f_iip(out, [0, 2], p0, t0) + cond.f(out, [0, 2], p0, t0) @test out[2] ≈ 1 # signature is u,p,t sol = solve(prob, Tsit5()) @@ -351,7 +351,7 @@ end out = [0.0, 0.0, 0.0] p0 = 0.0 t0 = 0.0 - cond.f_iip(out, [0, 0, 0, 0], p0, t0) + cond.f(out, [0, 0, 0, 0], p0, t0) @test out ≈ [0, 1.5, -1.5] sol = solve(prob, Tsit5()) From e5223b818a5d291c2542f27cac414125e3869928 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 15 May 2025 11:59:56 +0530 Subject: [PATCH 1774/2176] fix: update CasADi extension to new semantics --- ext/MTKCasADiDynamicOptExt.jl | 76 +++++++++++++++++------------------ 1 file changed, 37 insertions(+), 39 deletions(-) diff --git a/ext/MTKCasADiDynamicOptExt.jl b/ext/MTKCasADiDynamicOptExt.jl index 6aa15fb55a..a5400e3c8d 100644 --- a/ext/MTKCasADiDynamicOptExt.jl +++ b/ext/MTKCasADiDynamicOptExt.jl @@ -56,9 +56,9 @@ function (M::MXLinearInterpolation)(τ) end """ - CasADiDynamicOptProblem(sys::ODESystem, u0, tspan, p; dt, steps) + CasADiDynamicOptProblem(sys::System, u0, tspan, p; dt, steps) -Convert an ODESystem representing an optimal control system into a CasADi model +Convert an System representing an optimal control system into a CasADi model for solving using optimization. Must provide either `dt`, the timestep between collocation points (which, along with the timespan, determines the number of points), or directly provide the number of points as `steps`. @@ -68,10 +68,10 @@ The optimization variables: - a vector-of-vectors V representing the controls as an interpolation array The constraints are: -- The set of user constraints passed to the ODESystem via `constraints` +- The set of user constraints passed to the System via `constraints` - The solver constraints that encode the time-stepping used by the solver """ -function MTK.CasADiDynamicOptProblem(sys::ODESystem, u0map, tspan, pmap; +function MTK.CasADiDynamicOptProblem(sys::System, u0map, tspan, pmap; dt = nothing, steps = nothing, guesses = Dict(), kwargs...) @@ -80,7 +80,8 @@ function MTK.CasADiDynamicOptProblem(sys::ODESystem, u0map, tspan, pmap; f, u0, p = MTK.process_SciMLProblem(ODEInputFunction, sys, _u0map, pmap; t = tspan !== nothing ? tspan[1] : tspan, output_type = MX, kwargs...) - pmap = Dict{Any, Any}(pmap) + pmap = MTK.recursive_unwrap(MTK.AnyDict(pmap)) + MTK.evaluate_varmap!(pmap, keys(pmap)) steps, is_free_t = MTK.process_tspan(tspan, dt, steps) model = init_model(sys, tspan, steps, u0map, pmap, u0; is_free_t) @@ -143,15 +144,15 @@ function set_casadi_bounds!(model, sys, pmap) for (i, u) in enumerate(unknowns(sys)) if MTK.hasbounds(u) lo, hi = MTK.getbounds(u) - subject_to!(opti, Symbolics.fixpoint_sub(lo, pmap) <= U.u[i, :]) - subject_to!(opti, U.u[i, :] <= Symbolics.fixpoint_sub(hi, pmap)) + subject_to!(opti, Symbolics.fast_substitute(lo, pmap) <= U.u[i, :]) + subject_to!(opti, U.u[i, :] <= Symbolics.fast_substitute(hi, pmap)) end end for (i, v) in enumerate(MTK.unbound_inputs(sys)) if MTK.hasbounds(v) lo, hi = MTK.getbounds(v) - subject_to!(opti, Symbolics.fixpoint_sub(lo, pmap) <= V.u[i, :]) - subject_to!(opti, V.u[i, :] <= Symbolics.fixpoint_sub(hi, pmap)) + subject_to!(opti, Symbolics.fast_substitute(lo, pmap) <= V.u[i, :]) + subject_to!(opti, V.u[i, :] <= Symbolics.fast_substitute(hi, pmap)) end end end @@ -167,15 +168,15 @@ function add_user_constraints!(model::CasADiModel, sys, tspan, pmap; is_free_t) @unpack opti, U, V, tₛ = model iv = MTK.get_iv(sys) - conssys = MTK.get_constraintsystem(sys) - jconstraints = isnothing(conssys) ? nothing : MTK.get_constraints(conssys) + jconstraints = MTK.get_constraints(sys) (isnothing(jconstraints) || isempty(jconstraints)) && return nothing stidxmap = Dict([v => i for (i, v) in enumerate(unknowns(sys))]) ctidxmap = Dict([v => i for (i, v) in enumerate(MTK.unbound_inputs(sys))]) - cons_unknowns = map(MTK.default_toterm, unknowns(conssys)) + cons_dvs, cons_ps = MTK.process_constraint_system( + jconstraints, Set(unknowns(sys)), parameters(sys), iv; validate = false) - auxmap = Dict([u => MTK.default_toterm(MTK.value(u)) for u in unknowns(conssys)]) + auxmap = Dict([u => MTK.default_toterm(MTK.value(u)) for u in cons_dvs]) jconstraints = substitute_casadi_vars(model, sys, pmap, jconstraints; is_free_t, auxmap) # Manually substitute fixed-t variables for (i, cons) in enumerate(jconstraints) @@ -207,9 +208,8 @@ end function add_cost_function!(model::CasADiModel, sys, tspan, pmap; is_free_t) @unpack opti, U, V, tₛ = model - jcosts = copy(MTK.get_costs(sys)) - consolidate = MTK.get_consolidate(sys) - if isnothing(jcosts) || isempty(jcosts) + jcosts = cost(sys) + if Symbolics._iszero(jcosts) minimize!(opti, MX(0)) return end @@ -218,24 +218,22 @@ function add_cost_function!(model::CasADiModel, sys, tspan, pmap; is_free_t) stidxmap = Dict([v => i for (i, v) in enumerate(unknowns(sys))]) ctidxmap = Dict([v => i for (i, v) in enumerate(MTK.unbound_inputs(sys))]) - jcosts = substitute_casadi_vars(model, sys, pmap, jcosts; is_free_t) + jcosts = substitute_casadi_vars(model, sys, pmap, [jcosts]; is_free_t)[1] # Substitute fixed-time variables. - for i in 1:length(jcosts) - costvars = MTK.vars(jcosts[i]) - for st in costvars - MTK.iscall(st) || continue - x = operation(st) - t = only(arguments(st)) - MTK.symbolic_type(t) === MTK.NotSymbolic() || continue - if haskey(stidxmap, x(iv)) - idx = stidxmap[x(iv)] - cv = U - else - idx = ctidxmap[x(iv)] - cv = V - end - jcosts[i] = Symbolics.substitute(jcosts[i], Dict(x(t) => cv(t)[idx])) + costvars = MTK.vars(jcosts) + for st in costvars + MTK.iscall(st) || continue + x = operation(st) + t = only(arguments(st)) + MTK.symbolic_type(t) === MTK.NotSymbolic() || continue + if haskey(stidxmap, x(iv)) + idx = stidxmap[x(iv)] + cv = U + else + idx = ctidxmap[x(iv)] + cv = V end + jcosts = Symbolics.substitute(jcosts, Dict(x(t) => cv(t)[idx])) end dt = U.t[2] - U.t[1] @@ -249,9 +247,9 @@ function add_cost_function!(model::CasADiModel, sys, tspan, pmap; is_free_t) # Approximate integral as sum. intmap[int] = dt * tₛ * sum(arg) end - jcosts = map(c -> Symbolics.substitute(c, intmap), jcosts) - jcosts = MTK.value.(jcosts) - minimize!(opti, MX(MTK.value(consolidate(jcosts)))) + jcosts = Symbolics.substitute(jcosts, intmap) + jcosts = MTK.value(jcosts) + minimize!(opti, MX(jcosts)) end function substitute_casadi_vars( @@ -264,20 +262,20 @@ function substitute_casadi_vars( x_ops = [MTK.operation(MTK.unwrap(st)) for st in sts] c_ops = [MTK.operation(MTK.unwrap(ct)) for ct in cts] - exprs = map(c -> Symbolics.fixpoint_sub(c, auxmap), exprs) - exprs = map(c -> Symbolics.fixpoint_sub(c, Dict(pmap)), exprs) + exprs = map(c -> Symbolics.fast_substitute(c, auxmap), exprs) + exprs = map(c -> Symbolics.fast_substitute(c, Dict(pmap)), exprs) # tf means different things in different contexts; a [tf] in a cost function # should be tₛ, while a x(tf) should translate to x[1] if is_free_t free_t_map = Dict([[x(tₛ) => U.u[i, end] for (i, x) in enumerate(x_ops)]; [c(tₛ) => V.u[i, end] for (i, c) in enumerate(c_ops)]]) - exprs = map(c -> Symbolics.fixpoint_sub(c, free_t_map), exprs) + exprs = map(c -> Symbolics.fast_substitute(c, free_t_map), exprs) end # for variables like x(t) whole_interval_map = Dict([[v => U.u[i, :] for (i, v) in enumerate(sts)]; [v => V.u[i, :] for (i, v) in enumerate(cts)]]) - exprs = map(c -> Symbolics.fixpoint_sub(c, whole_interval_map), exprs) + exprs = map(c -> Symbolics.fast_substitute(c, whole_interval_map), exprs) exprs end From 96e3d810092297349365194d00259355bed14ac0 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 15 May 2025 12:00:13 +0530 Subject: [PATCH 1775/2176] fix: update InfiniteOpt extension to new semantics --- ext/MTKInfiniteOptExt.jl | 44 +++++++++++++++++++++------------------- 1 file changed, 23 insertions(+), 21 deletions(-) diff --git a/ext/MTKInfiniteOptExt.jl b/ext/MTKInfiniteOptExt.jl index dd7da24672..f4fff61dff 100644 --- a/ext/MTKInfiniteOptExt.jl +++ b/ext/MTKInfiniteOptExt.jl @@ -63,7 +63,8 @@ function MTK.JuMPDynamicOptProblem(sys::System, u0map, tspan, pmap; f, u0, p = MTK.process_SciMLProblem(ODEInputFunction, sys, _u0map, pmap; t = tspan !== nothing ? tspan[1] : tspan, kwargs...) - pmap = Dict{Any, Any}(pmap) + pmap = MTK.recursive_unwrap(MTK.AnyDict(pmap)) + MTK.evaluate_varmap!(pmap, keys(pmap)) steps, is_free_t = MTK.process_tspan(tspan, dt, steps) model = init_model(sys, tspan, steps, u0map, pmap, u0; is_free_t) @@ -89,7 +90,8 @@ function MTK.InfiniteOptDynamicOptProblem(sys::System, u0map, tspan, pmap; f, u0, p = MTK.process_SciMLProblem(ODEInputFunction, sys, _u0map, pmap; t = tspan !== nothing ? tspan[1] : tspan, kwargs...) - pmap = Dict{Any, Any}(pmap) + pmap = MTK.recursive_unwrap(MTK.AnyDict(pmap)) + MTK.evaluate_varmap!(pmap, keys(pmap)) steps, is_free_t = MTK.process_tspan(tspan, dt, steps) model = init_model(sys, tspan, steps, u0map, pmap, u0; is_free_t) @@ -150,8 +152,8 @@ function set_jump_bounds!(model, sys, pmap) for (i, u) in enumerate(unknowns(sys)) if MTK.hasbounds(u) lo, hi = MTK.getbounds(u) - set_lower_bound(U[i], Symbolics.fixpoint_sub(lo, pmap)) - set_upper_bound(U[i], Symbolics.fixpoint_sub(hi, pmap)) + set_lower_bound(U[i], Symbolics.fast_substitute(lo, pmap)) + set_upper_bound(U[i], Symbolics.fast_substitute(hi, pmap)) end end @@ -159,20 +161,19 @@ function set_jump_bounds!(model, sys, pmap) for (i, v) in enumerate(MTK.unbound_inputs(sys)) if MTK.hasbounds(v) lo, hi = MTK.getbounds(v) - set_lower_bound(V[i], Symbolics.fixpoint_sub(lo, pmap)) - set_upper_bound(V[i], Symbolics.fixpoint_sub(hi, pmap)) + set_lower_bound(V[i], Symbolics.fast_substitute(lo, pmap)) + set_upper_bound(V[i], Symbolics.fast_substitute(hi, pmap)) end end end function add_jump_cost_function!(model::InfiniteModel, sys, tspan, pmap; is_free_t = false) - jcosts = MTK.get_costs(sys) - consolidate = MTK.get_consolidate(sys) - if isnothing(jcosts) || isempty(jcosts) + jcosts = cost(sys) + if Symbolics._iszero(jcosts) @objective(model, Min, 0) return end - jcosts = substitute_jump_vars(model, sys, pmap, jcosts; is_free_t) + jcosts = substitute_jump_vars(model, sys, pmap, [jcosts]; is_free_t)[1] tₛ = is_free_t ? model[:tf] : 1 # Substitute integral @@ -187,17 +188,18 @@ function add_jump_cost_function!(model::InfiniteModel, sys, tspan, pmap; is_free hi = haskey(pmap, hi) ? 1 : MTK.value(hi) intmap[int] = tₛ * InfiniteOpt.∫(arg, model[:t], lo, hi) end - jcosts = map(c -> Symbolics.substitute(c, intmap), jcosts) - @objective(model, Min, consolidate(jcosts)) + jcosts = Symbolics.substitute(jcosts, intmap) + @objective(model, Min, MTK.value(jcosts)) end function add_user_constraints!(model::InfiniteModel, sys, pmap; is_free_t = false) - conssys = MTK.get_constraintsystem(sys) - jconstraints = isnothing(conssys) ? nothing : MTK.get_constraints(conssys) + jconstraints = MTK.get_constraints(sys) (isnothing(jconstraints) || isempty(jconstraints)) && return nothing + cons_dvs, cons_ps = MTK.process_constraint_system( + jconstraints, Set(unknowns(sys)), parameters(sys), MTK.get_iv(sys); validate = false) if is_free_t - for u in MTK.get_unknowns(conssys) + for u in cons_dvs x = MTK.operation(u) t = only(arguments(u)) if (MTK.symbolic_type(t) === MTK.NotSymbolic()) @@ -206,7 +208,7 @@ function add_user_constraints!(model::InfiniteModel, sys, pmap; is_free_t = fals end end - auxmap = Dict([u => MTK.default_toterm(MTK.value(u)) for u in unknowns(conssys)]) + auxmap = Dict([u => MTK.default_toterm(MTK.value(u)) for u in cons_dvs]) jconstraints = substitute_jump_vars(model, sys, pmap, jconstraints; auxmap, is_free_t) # Substitute to-term'd variables @@ -235,25 +237,25 @@ function substitute_jump_vars(model, sys, pmap, exprs; auxmap = Dict(), is_free_ x_ops = [MTK.operation(MTK.unwrap(st)) for st in sts] c_ops = [MTK.operation(MTK.unwrap(ct)) for ct in cts] - exprs = map(c -> Symbolics.fixpoint_sub(c, auxmap), exprs) - exprs = map(c -> Symbolics.fixpoint_sub(c, Dict(pmap)), exprs) + exprs = map(c -> Symbolics.fast_substitute(c, auxmap), exprs) + exprs = map(c -> Symbolics.fast_substitute(c, Dict(pmap)), exprs) if is_free_t tf = model[:tf] free_t_map = Dict([[x(tf) => U[i](1) for (i, x) in enumerate(x_ops)]; [c(tf) => V[i](1) for (i, c) in enumerate(c_ops)]]) - exprs = map(c -> Symbolics.fixpoint_sub(c, free_t_map), exprs) + exprs = map(c -> Symbolics.fast_substitute(c, free_t_map), exprs) end # for variables like x(t) whole_interval_map = Dict([[v => U[i] for (i, v) in enumerate(sts)]; [v => V[i] for (i, v) in enumerate(cts)]]) - exprs = map(c -> Symbolics.fixpoint_sub(c, whole_interval_map), exprs) + exprs = map(c -> Symbolics.fast_substitute(c, whole_interval_map), exprs) # for variables like x(1.0) fixed_t_map = Dict([[x_ops[i] => U[i] for i in 1:length(U)]; [c_ops[i] => V[i] for i in 1:length(V)]]) - exprs = map(c -> Symbolics.fixpoint_sub(c, fixed_t_map), exprs) + exprs = map(c -> Symbolics.fast_substitute(c, fixed_t_map), exprs) exprs end From 23ff019a40b6b3b77903736ac748b8b2131864e2 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 15 May 2025 12:01:09 +0530 Subject: [PATCH 1776/2176] fix: update `optimal_control_interface.jl` to new semantics --- src/systems/optimal_control_interface.jl | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/systems/optimal_control_interface.jl b/src/systems/optimal_control_interface.jl index 2ee2d0e9ca..c88aceabff 100644 --- a/src/systems/optimal_control_interface.jl +++ b/src/systems/optimal_control_interface.jl @@ -21,9 +21,9 @@ function InfiniteOptDynamicOptProblem end function CasADiDynamicOptProblem end function warn_overdetermined(sys, u0map) - constraintsys = get_constraintsystem(sys) - if !isnothing(constraintsys) - (length(constraints(constraintsys)) + length(u0map) > length(unknowns(sys))) && + cstrs = constraints(sys) + if !isempty(cstrs) + (length(cstrs) + length(u0map) > length(unknowns(sys))) && @warn "The control problem is overdetermined. The total number of conditions (# constraints + # fixed initial values given by u0map) exceeds the total number of states. The solvers will default to doing a nonlinear least-squares optimization." end end From 17caec8f39c5c9fdf5e27c2b21890647bd3c6bd2 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 15 May 2025 14:09:19 +0530 Subject: [PATCH 1777/2176] fix: recognize delayed derivatives in `isdelay` --- src/systems/codegen_utils.jl | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/systems/codegen_utils.jl b/src/systems/codegen_utils.jl index 9ee8a7fd81..8c2c322e31 100644 --- a/src/systems/codegen_utils.jl +++ b/src/systems/codegen_utils.jl @@ -115,6 +115,9 @@ variable. """ function isdelay(var, iv) iv === nothing && return false + if iscall(var) && ModelingToolkit.isoperator(var, Differential) + return isdelay(arguments(var)[1], iv) + end isvariable(var) || return false isparameter(var) && return false if iscall(var) && !ModelingToolkit.isoperator(var, Symbolics.Operator) From c1c87463b39814f5d6e3b25b8149c4e40f294a60 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 15 May 2025 14:10:04 +0530 Subject: [PATCH 1778/2176] fix: use 2-argument `consolidate` in dynamic optimization tests --- test/extensions/dynamic_optimization.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/extensions/dynamic_optimization.jl b/test/extensions/dynamic_optimization.jl index 1074cae620..0d4e9d94e2 100644 --- a/test/extensions/dynamic_optimization.jl +++ b/test/extensions/dynamic_optimization.jl @@ -259,7 +259,7 @@ end eqs = [D(x(t)) ~ -2 + 0.5 * u(t)] # Integral cost function costs = [-Symbolics.Integral(t in (0, tf))(x(t) - u(t)), -x(tf)] - consolidate(u) = u[1] + u[2] + consolidate(u, sub) = u[1] + u[2] + sum(sub) @named rocket = System(eqs, t; costs, consolidate) rocket = structural_simplify(rocket; inputs = [u(t)]) From 995b8aebd7f987a1ce620afdf2751968d70c64c5 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 16 May 2025 12:03:06 +0530 Subject: [PATCH 1779/2176] build: bump CasADi compat --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index ca03a35ba0..41568022ab 100644 --- a/Project.toml +++ b/Project.toml @@ -89,7 +89,7 @@ BifurcationKit = "0.4" BlockArrays = "1.1" BoundaryValueDiffEqAscher = "1.6.0" BoundaryValueDiffEqMIRK = "1.7.0" -CasADi = "1.0.6" +CasADi = "1.0.7" ChainRulesCore = "1" Combinatorics = "1" CommonSolve = "0.2.4" From 229e8c265973ad595682fe1af3d9207447d1df05 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 20 May 2025 11:33:27 +0530 Subject: [PATCH 1780/2176] fix: fix homotopy continuation --- src/systems/nonlinear/homotopy_continuation.jl | 1 - 1 file changed, 1 deletion(-) diff --git a/src/systems/nonlinear/homotopy_continuation.jl b/src/systems/nonlinear/homotopy_continuation.jl index e68737c19b..103780cb21 100644 --- a/src/systems/nonlinear/homotopy_continuation.jl +++ b/src/systems/nonlinear/homotopy_continuation.jl @@ -394,7 +394,6 @@ function transform_system(sys::System, transformation::PolynomialTransformation; @set! sys2.unknowns = new_dvs # remove observed equations to avoid adding them in codegen @set! sys2.observed = Equation[] - @set! sys2.substitutions = nothing return PolynomialTransformationResult(sys2, denoms) end From 71dd1ca570d0f73db1c6cb467132e04e46b8d0be Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 20 May 2025 11:33:44 +0530 Subject: [PATCH 1781/2176] fix: fix AD on `default_consolidate` --- src/systems/system.jl | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/systems/system.jl b/src/systems/system.jl index a638debfb4..940ca39350 100644 --- a/src/systems/system.jl +++ b/src/systems/system.jl @@ -107,7 +107,9 @@ struct System <: AbstractSystem end function default_consolidate(costs, subcosts) - return sum(costs; init = 0.0) + sum(subcosts; init = 0.0) + # `reduce` instead of `sum` because the rrule for `sum` doesn't + # handle the `init` kwarg. + return reduce(+, costs; init = 0.0) + reduce(+, subcosts; init = 0.0) end function System(eqs::Vector{Equation}, iv, dvs, ps, brownians = []; From 545616f1eb1a4f089dcca13b3e0f8641272804fe Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 20 May 2025 11:33:52 +0530 Subject: [PATCH 1782/2176] test: fix labelledarrays test --- test/labelledarrays.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/labelledarrays.jl b/test/labelledarrays.jl index 59c269dff7..96c9e1c32b 100644 --- a/test/labelledarrays.jl +++ b/test/labelledarrays.jl @@ -14,7 +14,7 @@ eqs = [D(x) ~ σ * (y - x), @named de = System(eqs, t) de = complete(de) -ff = ODEFunction(de, [x, y, z], [σ, ρ, β], jac = true) +ff = ODEFunction(de; jac = true) a = @SVector [1.0, 2.0, 3.0] b = SLVector(x = 1.0, y = 2.0, z = 3.0) From a9e5df57ccd1f009c32222b44b1d950735560c17 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 20 May 2025 13:04:10 +0530 Subject: [PATCH 1783/2176] fix: fix `define_params` in `LabelledArraysExt` --- ext/MTKLabelledArraysExt.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ext/MTKLabelledArraysExt.jl b/ext/MTKLabelledArraysExt.jl index dda04d07da..c10400b109 100644 --- a/ext/MTKLabelledArraysExt.jl +++ b/ext/MTKLabelledArraysExt.jl @@ -6,7 +6,7 @@ function ModelingToolkit.define_vars(u::Union{SLArray, LArray}, t) [ModelingToolkit._defvar(x)(t) for x in LabelledArrays.symnames(typeof(u))] end -function ModelingToolkit.define_params(p::Union{SLArray, LArray}, names = nothing) +function ModelingToolkit.define_params(p::Union{SLArray, LArray}, t, names = nothing) if names === nothing [toparam(variable(x)) for x in LabelledArrays.symnames(typeof(p))] else From 130f98f144779a5f491ad21d84dd461364e8b4a7 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 20 May 2025 14:24:01 +0530 Subject: [PATCH 1784/2176] fix: fix BifurcationKitExt --- ext/MTKBifurcationKitExt.jl | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/ext/MTKBifurcationKitExt.jl b/ext/MTKBifurcationKitExt.jl index bcb3152702..870b523985 100644 --- a/ext/MTKBifurcationKitExt.jl +++ b/ext/MTKBifurcationKitExt.jl @@ -5,6 +5,7 @@ module MTKBifurcationKitExt # Imports using ModelingToolkit, Setfield import BifurcationKit +using SymbolicIndexingInterface: is_time_dependent ### Observable Plotting Handling ### @@ -94,6 +95,14 @@ function BifurcationKit.BifurcationProblem(nsys::System, if !ModelingToolkit.iscomplete(nsys) error("A completed `System` is required. Call `complete` or `structural_simplify` on the system before creating a `BifurcationProblem`") end + if is_time_dependent(nsys) + nsys = System([0 ~ eq.rhs for eq in full_equations(nsys)], + unknowns(nsys), + parameters(nsys); + observed = observed(nsys), + name = nameof(nsys)) + nsys = complete(nsys) + end @set! nsys.index_cache = nothing # force usage of a parameter vector instead of `MTKParameters` # Creates F and J functions. ofun = NonlinearFunction(nsys; jac = jac) @@ -143,17 +152,4 @@ function BifurcationKit.BifurcationProblem(nsys::System, kwargs...) end -# When input is a ODESystem. -function BifurcationKit.BifurcationProblem(osys::System, args...; kwargs...) - if !ModelingToolkit.iscomplete(osys) - error("A completed `ODESystem` is required. Call `complete` or `structural_simplify` on the system before creating a `BifurcationProblem`") - end - nsys = System([0 ~ eq.rhs for eq in full_equations(osys)], - unknowns(osys), - parameters(osys); - observed = observed(osys), - name = nameof(osys)) - return BifurcationKit.BifurcationProblem(complete(nsys), args...; kwargs...) -end - end # module From 98de7ec7d996db7a4291e43f5df671bf7f1272f3 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 20 May 2025 16:21:24 +0530 Subject: [PATCH 1785/2176] refactor: remove `ode_order_lowering` and `dae_order_lowering` --- src/ModelingToolkit.jl | 6 +- src/systems/diffeqs/first_order_transform.jl | 106 ----------------- test/lowering_solving.jl | 76 ------------ test/odesystem.jl | 110 ------------------ test/runtests.jl | 1 - test/state_selection.jl | 1 - .../index_reduction.jl | 90 +------------- 7 files changed, 5 insertions(+), 385 deletions(-) delete mode 100644 src/systems/diffeqs/first_order_transform.jl delete mode 100644 test/lowering_solving.jl diff --git a/src/ModelingToolkit.jl b/src/ModelingToolkit.jl index 37b4629992..bf6dfc3af2 100644 --- a/src/ModelingToolkit.jl +++ b/src/ModelingToolkit.jl @@ -190,7 +190,6 @@ include("modelingtoolkitize/nonlinearproblem.jl") include("systems/nonlinear/homotopy_continuation.jl") include("systems/nonlinear/initializesystem.jl") -include("systems/diffeqs/first_order_transform.jl") include("systems/diffeqs/basic_transformations.jl") include("systems/pde/pdesystem.jl") @@ -295,9 +294,8 @@ export isinput, isoutput, getbounds, hasbounds, getguess, hasguess, isdisturbanc tunable_parameters, isirreducible, getdescription, hasdescription, hasunit, getunit, hasconnect, getconnect, hasmisc, getmisc, state_priority -export ode_order_lowering, dae_order_lowering, liouville_transform, - change_independent_variable, substitute_component, add_accumulations, - noise_to_brownians +export liouville_transform, change_independent_variable, substitute_component, + add_accumulations, noise_to_brownians export PDESystem export Differential, expand_derivatives, @derivatives export Equation, ConstrainedEquation diff --git a/src/systems/diffeqs/first_order_transform.jl b/src/systems/diffeqs/first_order_transform.jl deleted file mode 100644 index d017ea362b..0000000000 --- a/src/systems/diffeqs/first_order_transform.jl +++ /dev/null @@ -1,106 +0,0 @@ -""" -$(TYPEDSIGNATURES) - -Takes a Nth order System and returns a new System written in first order -form by defining new variables which represent the N-1 derivatives. -""" -function ode_order_lowering(sys::System) - iv = get_iv(sys) - eqs_lowered, new_vars = ode_order_lowering(equations(sys), iv, unknowns(sys)) - @set! sys.eqs = eqs_lowered - @set! sys.unknowns = new_vars - return sys -end - -function dae_order_lowering(sys::System) - iv = get_iv(sys) - eqs_lowered, new_vars = dae_order_lowering(equations(sys), iv, unknowns(sys)) - @set! sys.eqs = eqs_lowered - @set! sys.unknowns = new_vars - return sys -end - -function ode_order_lowering(eqs, iv, unknown_vars) - var_order = OrderedDict{Any, Int}() - D = Differential(iv) - diff_eqs = Equation[] - diff_vars = [] - alge_eqs = Equation[] - - for (i, eq) in enumerate(eqs) - if !isdiffeq(eq) - push!(alge_eqs, eq) - else - var, maxorder = var_from_nested_derivative(eq.lhs) - maxorder > get(var_order, var, 1) && (var_order[var] = maxorder) - var′ = lower_varname(var, iv, maxorder - 1) - rhs′ = diff2term_with_unit(eq.rhs, iv) - push!(diff_vars, var′) - push!(diff_eqs, D(var′) ~ rhs′) - end - end - - for (var, order) in var_order - for o in (order - 1):-1:1 - lvar = lower_varname(var, iv, o - 1) - rvar = lower_varname(var, iv, o) - push!(diff_vars, lvar) - - rhs = rvar - eq = Differential(iv)(lvar) ~ rhs - push!(diff_eqs, eq) - end - end - - # we want to order the equations and variables to be `(diff, alge)` - return (vcat(diff_eqs, alge_eqs), vcat(diff_vars, setdiff(unknown_vars, diff_vars))) -end - -function dae_order_lowering(eqs, iv, unknown_vars) - var_order = OrderedDict{Any, Int}() - D = Differential(iv) - diff_eqs = Equation[] - diff_vars = OrderedSet() - alge_eqs = Equation[] - vars = Set() - subs = Dict() - - for (i, eq) in enumerate(eqs) - vars!(vars, eq) - n_diffvars = 0 - for vv in vars - isdifferential(vv) || continue - var, maxorder = var_from_nested_derivative(vv) - isparameter(var) && continue - n_diffvars += 1 - order = get(var_order, var, nothing) - seen = order !== nothing - if !seen - order = 1 - end - maxorder > order && (var_order[var] = maxorder) - var′ = lower_varname(var, iv, maxorder - 1) - subs[vv] = D(var′) - if !seen - push!(diff_vars, var′) - end - end - n_diffvars == 0 && push!(alge_eqs, eq) - empty!(vars) - end - - for (var, order) in var_order - for o in (order - 1):-1:1 - lvar = lower_varname(var, iv, o - 1) - rvar = lower_varname(var, iv, o) - push!(diff_vars, lvar) - - rhs = rvar - eq = Differential(iv)(lvar) ~ rhs - push!(diff_eqs, eq) - end - end - - return ([diff_eqs; substitute.(eqs, (subs,))], - vcat(collect(diff_vars), setdiff(unknown_vars, diff_vars))) -end diff --git a/test/lowering_solving.jl b/test/lowering_solving.jl deleted file mode 100644 index 280848f6ad..0000000000 --- a/test/lowering_solving.jl +++ /dev/null @@ -1,76 +0,0 @@ -using ModelingToolkit, OrdinaryDiffEq, Test, LinearAlgebra -using ModelingToolkit: t_nounits as t, D_nounits as D - -@parameters σ ρ β -@variables x(t) y(t) z(t) k(t) - -eqs = [D(D(x)) ~ σ * (y - x), - D(y) ~ x * (ρ - z) - y, - D(z) ~ x * y - β * z] - -@named sys′ = System(eqs, t) -sys = ode_order_lowering(sys′) - -eqs2 = [0 ~ x * y - k, - D(D(x)) ~ σ * (y - x), - D(y) ~ x * (ρ - z) - y, - D(z) ~ x * y - β * z] -@named sys2 = System(eqs2, t, [x, y, z, k], parameters(sys′)) -sys2 = ode_order_lowering(sys2) -# test equation/variable ordering -ModelingToolkit.calculate_massmatrix(sys2) == Diagonal([1, 1, 1, 1, 0]) - -u0 = [D(x) => 2.0, - x => 1.0, - y => 0.0, - z => 0.0] - -p = [σ => 28.0, - ρ => 10.0, - β => 8 / 3] - -tspan = (0.0, 100.0) - -sys = complete(sys) -prob = ODEProblem(sys, u0, tspan, p, jac = true) -probexpr = ODEProblem(sys, u0, tspan, p; jac = true, expression = Val{true}) -sol = solve(prob, Tsit5()) -solexpr = solve(eval(prob), Tsit5()) -@test all(x -> x == 0, Array(sol - solexpr)) -#using Plots; plot(sol,idxs=(:x,:y)) - -@parameters σ ρ β -@variables x(t) y(t) z(t) - -eqs = [D(x) ~ σ * (y - x), - D(y) ~ x * (ρ - z) - y, - D(z) ~ x * y - β * z] - -lorenz1 = System(eqs, t, name = :lorenz1) -lorenz2 = System(eqs, t, name = :lorenz2) - -@variables α(t) -@parameters γ -connections = [0 ~ lorenz1.x + lorenz2.y + α * γ] -@named connected = System(connections, t, [α], [γ], systems = [lorenz1, lorenz2]) -connected = complete(connected) -u0 = [lorenz1.x => 1.0, - lorenz1.y => 0.0, - lorenz1.z => 0.0, - lorenz2.x => 0.0, - lorenz2.y => 1.0, - lorenz2.z => 0.0] - -p = [lorenz1.σ => 10.0, - lorenz1.ρ => 28.0, - lorenz1.β => 8 / 3, - lorenz2.σ => 10.0, - lorenz2.ρ => 28.0, - lorenz2.β => 8 / 3, - γ => 2.0] - -tspan = (0.0, 100.0) -prob = ODEProblem(connected, u0, tspan, p) -sol = solve(prob, Rodas5()) -@test maximum(sol[lorenz1.x] + sol[lorenz2.y] + 2sol[α]) < 1e-12 -#using Plots; plot(sol,idxs=(:α,Symbol(lorenz1.x),Symbol(lorenz2.y))) diff --git a/test/odesystem.jl b/test/odesystem.jl index 8b24691fea..904ca58d1a 100644 --- a/test/odesystem.jl +++ b/test/odesystem.jl @@ -153,32 +153,6 @@ du = [0.0] f(du, [1.0], [t -> t + 2], 5.0) @test du ≈ [27561] -@testset "Issue#17: Conversion to first order ODEs" begin - D3 = D^3 - D2 = D^2 - @variables u(t) uˍtt(t) uˍt(t) xˍt(t) - eqs = [D3(u) ~ 2(D2(u)) + D(u) + D(x) + 1 - D2(x) ~ D(x) + 2] - @named de = System(eqs, t) - de1 = ode_order_lowering(de) - - @testset "Issue#219: Ordering of equations in `ode_order_lowering`" begin - lowered_eqs = [D(uˍtt) ~ 2uˍtt + uˍt + xˍt + 1 - D(xˍt) ~ xˍt + 2 - D(uˍt) ~ uˍtt - D(u) ~ uˍt - D(x) ~ xˍt] - @test isequal( - [ModelingToolkit.var_from_nested_derivative(eq.lhs)[1] for eq in equations(de1)], - unknowns(@named lowered = System(lowered_eqs, t))) - end - - test_diffeq_inference("first-order transform", de1, t, [uˍtt, xˍt, uˍt, u, x], []) - du = zeros(5) - ODEFunction(complete(de1))(du, ones(5), nothing, 0.1) - @test du == [5.0, 3.0, 1.0, 1.0, 1.0] -end - # Internal calculations @parameters σ a = y - x @@ -348,16 +322,6 @@ eqs = [D(x) ~ σ * (y - x), @test issym(equations(sys)[1].rhs) end -@testset "Issue#708" begin - @parameters a - @variables x(t) y(t) z(t) - @named sys = System([D(x) ~ y, 0 ~ x + z, 0 ~ x - y], t, [z, y, x], []) - - sys2 = ode_order_lowering(sys) - M = ModelingToolkit.calculate_massmatrix(sys2) - @test M == Diagonal([1, 0, 0]) -end - # issue #609 @variables x1(t) x2(t) @@ -416,22 +380,6 @@ eqs = [ ] @test_throws ArgumentError ModelingToolkit.System(eqs, t, vars, pars, name = :foo) -@variables x(t) -@parameters M b k -eqs = [D(D(x)) ~ -b / M * D(x) - k / M * x] -ps = [M, b, k] -default_u0 = [D(x) => 0.0, x => 10.0] -default_p = [M => 1.0, b => 1.0, k => 1.0] -@named sys = System(eqs, t, [x], ps; defaults = [default_u0; default_p]) -sys = ode_order_lowering(sys) -sys = complete(sys) -prob = ODEProblem(sys, nothing, tspan) -sol = solve(prob, Tsit5()) -@test sol.t[end] == tspan[end] -@test sum(abs, sol.u[end]) < 1 -prob = ODEProblem{false}(sys, nothing, tspan; u0_constructor = x -> SVector(x...)) -@test prob.u0 isa SVector - # check_eqs_u0 kwarg test @variables x1(t) x2(t) eqs = [D(x1) ~ -x1] @@ -1530,64 +1478,6 @@ end @test osys1 !== osys2 end -@testset "dae_order_lowering basic test" begin - @parameters a - @variables x(t) y(t) z(t) - @named dae_sys = System([ - D(x) ~ y, - 0 ~ x + z, - 0 ~ x - y + z - ], t, [z, y, x], []) - - lowered_dae_sys = dae_order_lowering(dae_sys) - @variables x1(t) y1(t) z1(t) - expected_eqs = [ - 0 ~ x + z, - 0 ~ x - y + z, - Differential(t)(x) ~ y - ] - lowered_eqs = equations(lowered_dae_sys) - sorted_lowered_eqs = sort(lowered_eqs, by = string) - sorted_expected_eqs = sort(expected_eqs, by = string) - @test sorted_lowered_eqs == sorted_expected_eqs - - expected_vars = Set([z, y, x]) - lowered_vars = Set(unknowns(lowered_dae_sys)) - @test lowered_vars == expected_vars -end - -@testset "dae_order_lowering test with structural_simplify" begin - @variables x(t) y(t) z(t) - @parameters M b k - eqs = [ - D(D(x)) ~ -b / M * D(x) - k / M * x, - 0 ~ y - D(x), - 0 ~ z - x - ] - ps = [M, b, k] - default_u0 = [ - D(x) => 0.0, x => 10.0, y => 0.0, z => 10.0 - ] - default_p = [M => 1.0, b => 1.0, k => 1.0] - @named dae_sys = System(eqs, t, [x, y, z], ps; defaults = [default_u0; default_p]) - - simplified_dae_sys = structural_simplify(dae_sys) - - lowered_dae_sys = dae_order_lowering(simplified_dae_sys) - lowered_dae_sys = complete(lowered_dae_sys) - - tspan = (0.0, 10.0) - prob = ODEProblem(lowered_dae_sys, nothing, tspan) - sol = solve(prob, Tsit5()) - - @test sol.t[end] == tspan[end] - @test sum(abs, sol.u[end]) < 1 - - prob = ODEProblem{false}( - lowered_dae_sys, nothing, tspan; u0_constructor = x -> SVector(x...)) - @test prob.u0 isa SVector -end - @testset "Constraint system construction" begin @variables x(..) y(..) z(..) @parameters a b c d e diff --git a/test/runtests.jl b/test/runtests.jl index 86d69228ce..426993c414 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -51,7 +51,6 @@ end @safetestset "Symbolic Event Test" include("symbolic_events.jl") @safetestset "Stream Connect Test" include("stream_connectors.jl") @safetestset "Domain Connect Test" include("domain_connectors.jl") - @safetestset "Lowering Integration Test" include("lowering_solving.jl") @safetestset "Dependency Graph Test" include("dep_graphs.jl") @safetestset "Function Registration Test" include("function_registration.jl") @safetestset "Precompiled Modules Test" include("precompile_test.jl") diff --git a/test/state_selection.jl b/test/state_selection.jl index 6db8e8c5a0..ba74cc04a2 100644 --- a/test/state_selection.jl +++ b/test/state_selection.jl @@ -23,7 +23,6 @@ end @test_skip let pss = partial_state_selection(sys) @test length(equations(pss)) == 1 @test length(unknowns(pss)) == 2 - @test length(equations(ode_order_lowering(pss))) == 2 end @parameters σ ρ β diff --git a/test/structural_transformation/index_reduction.jl b/test/structural_transformation/index_reduction.jl index 26f73db4e7..0703221418 100644 --- a/test/structural_transformation/index_reduction.jl +++ b/test/structural_transformation/index_reduction.jl @@ -3,26 +3,13 @@ using Graphs using DiffEqBase using Test using UnPack +using OrdinaryDiffEq +using LinearAlgebra using ModelingToolkit: t_nounits as t, D_nounits as D # Define some variables @parameters L g -@variables x(t) y(t) w(t) z(t) T(t) xˍt(t) yˍt(t) xˍˍt(t) yˍˍt(t) - -eqs2 = [D(D(x)) ~ T * x, - D(D(y)) ~ T * y - g, - 0 ~ x^2 + y^2 - L^2] -pendulum2 = System(eqs2, t, [x, y, T], [L, g], name = :pendulum) -lowered_sys = ModelingToolkit.ode_order_lowering(pendulum2) - -lowered_eqs = [D(xˍt) ~ T * x, - D(yˍt) ~ T * y - g, - D(x) ~ xˍt, - D(y) ~ yˍt, - 0 ~ x^2 + y^2 - L^2] -@test System(lowered_eqs, t, [xˍt, yˍt, x, y, T], [L, g], name = :pendulum) == - lowered_sys -@test isequal(equations(lowered_sys), lowered_eqs) +@variables x(t) y(t) z(t) w(t) T(t) # Simple pendulum in cartesian coordinates eqs = [D(x) ~ w, @@ -39,82 +26,11 @@ state = TearingState(pendulum) map(x -> x == 0 ? StructuralTransformations.unassigned : x, [3, 4, 2, 5, 0, 0, 0, 0, 0]) -using ModelingToolkit -@parameters L g -@variables x(t) y(t) w(t) z(t) T(t) xˍt(t) yˍt(t) -idx1_pendulum = [D(x) ~ w, - D(y) ~ z, - #0 ~ x^2 + y^2 - L^2, - D(w) ~ T * x, - D(z) ~ T * y - g, - # intermediate 1: 0 ~ 2x*D(x) + 2y*D(y) - 0, - # intermediate 2(a): 0 ~ 2x*w + 2y*z - 0, (substitute D(x) and D(y)) - #0 ~ 2x*w + 2y*z, - # D(D(x)) ~ D(w) and substitute the rhs - D(xˍt) ~ T * x, - # D(D(y)) ~ D(z) and substitute the rhs - D(yˍt) ~ T * y - g, - # 2x*D(D(x)) + 2*D(x)*D(x) + 2y*D(D(y)) + 2*D(y)*D(y) and - # substitute the rhs - 0 ~ 2x * (T * x) + 2 * xˍt * xˍt + 2y * (T * y - g) + 2 * yˍt * yˍt] -@named idx1_pendulum = System(idx1_pendulum, t, [x, y, w, z, xˍt, yˍt, T], [L, g]) -first_order_idx1_pendulum = complete(ode_order_lowering(idx1_pendulum)) - -using OrdinaryDiffEq -using LinearAlgebra -prob = ODEProblem(first_order_idx1_pendulum, - # [x, y, w, z, xˍt, yˍt, T] - [1, 0, 0, 0, 0, 0, 0.0],# 0, 0, 0, 0], - (0, 10.0), - [1, 9.8]) -sol = solve(prob, Rodas5()); -#plot(sol, idxs=(1, 2)) - -new_sys = complete(dae_index_lowering(ModelingToolkit.ode_order_lowering(pendulum2))) - -prob_auto = ODEProblem(new_sys, - [D(x) => 0, - D(y) => 0, - x => 1, - y => 0, - T => 0.0], - (0, 100.0), - [L => 1, g => 9.8]) -sol = solve(prob_auto, Rodas5()); -#plot(sol, idxs=(x, y)) - -# Define some variables -@parameters L g -@variables x(t) y(t) T(t) - eqs2 = [D(D(x)) ~ T * x, D(D(y)) ~ T * y - g, 0 ~ x^2 + y^2 - L^2] pendulum2 = System(eqs2, t, [x, y, T], [L, g], name = :pendulum) -# Turn into a first order differential equation system -first_order_sys = ModelingToolkit.ode_order_lowering(pendulum2) - -# Perform index reduction to get an Index 1 DAE -new_sys = complete(dae_index_lowering(first_order_sys)) - -u0 = [ - D(x) => 0.0, - D(y) => 0.0, - x => 1.0, - y => 0.0, - T => 0.0 -] - -p = [ - L => 1.0, - g => 9.8 -] - -prob_auto = ODEProblem(new_sys, u0, (0.0, 10.0), p) -sol = solve(prob_auto, Rodas5()); -#plot(sol, idxs=(D(x), y)) - @test_skip begin let pss_pendulum2 = partial_state_selection(pendulum2) length(equations(pss_pendulum2)) <= 6 From abc6abe20fd5d64c2b12a04c1370583c1d902b1c Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 19 May 2025 23:07:50 +0530 Subject: [PATCH 1786/2176] refactor: remove code related to parsing and substitution of constants --- src/inputoutput.jl | 3 +- src/problems/optimizationproblem.jl | 4 +- .../StructuralTransformations.jl | 2 +- src/structural_transformation/codegen.jl | 265 +----------------- src/structural_transformation/utils.jl | 4 +- src/systems/abstractsystem.jl | 12 - src/systems/callbacks.jl | 5 - src/systems/codegen_utils.jl | 12 +- src/systems/nonlinear/initializesystem.jl | 3 +- src/systems/parameter_buffer.jl | 3 +- src/systems/problem_utils.jl | 14 +- src/systems/systemstructure.jl | 6 +- src/utils.jl | 117 +------- 13 files changed, 19 insertions(+), 431 deletions(-) diff --git a/src/inputoutput.jl b/src/inputoutput.jl index 1beb229664..19603b76cd 100644 --- a/src/inputoutput.jl +++ b/src/inputoutput.jl @@ -222,7 +222,7 @@ function generate_control_function(sys::AbstractSystem, inputs = unbound_inputs( disturbance_inputs = unwrap.(disturbance_inputs) eqs = [eq for eq in full_equations(sys)] - eqs = map(subs_constants, eqs) + if disturbance_inputs !== nothing && !disturbance_argument # Set all disturbance *inputs* to zero (we just want to keep the disturbance state) subs = Dict(disturbance_inputs .=> 0) @@ -237,7 +237,6 @@ function generate_control_function(sys::AbstractSystem, inputs = unbound_inputs( p = reorder_parameters(sys, ps) t = get_iv(sys) - # pre = has_difference ? (ex -> ex) : get_postprocess_fbody(sys) if disturbance_argument args = (dvs, inputs, p..., t, disturbance_inputs) else diff --git a/src/problems/optimizationproblem.jl b/src/problems/optimizationproblem.jl index 243d453ada..e0de2f78ff 100644 --- a/src/problems/optimizationproblem.jl +++ b/src/problems/optimizationproblem.jl @@ -56,10 +56,10 @@ function SciMLBase.OptimizationFunction{iip}(sys::System; else _cons_h = cons_hess_prototype = nothing end - cons_expr = subs_constants(cstr) + cons_expr = cstr end - obj_expr = subs_constants(cost(sys)) + obj_expr = cost(sys) observedfun = ObservedFunctionCache( sys; expression, eval_expression, eval_module, checkbounds, cse) diff --git a/src/structural_transformation/StructuralTransformations.jl b/src/structural_transformation/StructuralTransformations.jl index 06d8e440cc..2ba469e26a 100644 --- a/src/structural_transformation/StructuralTransformations.jl +++ b/src/structural_transformation/StructuralTransformations.jl @@ -21,7 +21,7 @@ using ModelingToolkit: System, AbstractSystem, var_from_nested_derivative, Diffe has_tearing_state, defaults, InvalidSystemException, ExtraEquationsSystemException, ExtraVariablesSystemException, - get_postprocess_fbody, vars!, + vars!, IncrementalCycleTracker, add_edge_checked!, topological_sort, invalidate_cache!, Substitutions, get_or_construct_tearing_state, filter_kwargs, lower_varname_with_unit, diff --git a/src/structural_transformation/codegen.jl b/src/structural_transformation/codegen.jl index 144e19aa31..9afe7ec5e7 100644 --- a/src/structural_transformation/codegen.jl +++ b/src/structural_transformation/codegen.jl @@ -1,6 +1,6 @@ using LinearAlgebra -using ModelingToolkit: process_events, get_preprocess_constants +using ModelingToolkit: process_events const MAX_INLINE_NLSOLVE_SIZE = 8 @@ -96,136 +96,6 @@ function torn_system_with_nlsolve_jacobian_sparsity(state, var_eq_matching, var_ sparse(I, J, true, length(eqs_idxs), length(states_idxs)) end -function gen_nlsolve!(is_not_prepended_assignment, eqs, vars, u0map::AbstractDict, - assignments, (deps, invdeps), var2assignment; checkbounds = true) - isempty(vars) && throw(ArgumentError("vars may not be empty")) - length(eqs) == length(vars) || - throw(ArgumentError("vars must be of the same length as the number of equations to find the roots of")) - rhss = map(x -> x.rhs, eqs) - # We use `vars` instead of `graph` to capture parameters, too. - paramset = ModelingToolkit.vars(r for r in rhss) - - # Compute necessary assignments for the nlsolve expr - init_assignments = [var2assignment[p] for p in paramset if haskey(var2assignment, p)] - if isempty(init_assignments) - needed_assignments_idxs = Int[] - needed_assignments = similar(assignments, 0) - else - tmp = [init_assignments] - # `deps[init_assignments]` gives the dependency of `init_assignments` - while true - next_assignments = unique(reduce(vcat, deps[init_assignments])) - isempty(next_assignments) && break - init_assignments = next_assignments - push!(tmp, init_assignments) - end - needed_assignments_idxs = unique(reduce(vcat, reverse(tmp))) - needed_assignments = assignments[needed_assignments_idxs] - end - - # Compute `params`. They are like enclosed variables - rhsvars = [ModelingToolkit.vars(r.rhs) for r in needed_assignments] - vars_set = Set(vars) - outer_set = BitSet() - inner_set = BitSet() - for (i, vs) in enumerate(rhsvars) - j = needed_assignments_idxs[i] - if isdisjoint(vars_set, vs) - push!(outer_set, j) - else - push!(inner_set, j) - end - end - init_refine = BitSet() - for i in inner_set - union!(init_refine, invdeps[i]) - end - intersect!(init_refine, outer_set) - setdiff!(outer_set, init_refine) - union!(inner_set, init_refine) - - next_refine = BitSet() - while true - for i in init_refine - id = invdeps[i] - isempty(id) && break - union!(next_refine, id) - end - intersect!(next_refine, outer_set) - isempty(next_refine) && break - setdiff!(outer_set, next_refine) - union!(inner_set, next_refine) - - init_refine, next_refine = next_refine, init_refine - empty!(next_refine) - end - global2local = Dict(j => i for (i, j) in enumerate(needed_assignments_idxs)) - inner_idxs = [global2local[i] for i in collect(inner_set)] - outer_idxs = [global2local[i] for i in collect(outer_set)] - extravars = reduce(union!, rhsvars[inner_idxs], init = Set()) - union!(paramset, extravars) - setdiff!(paramset, vars) - setdiff!(paramset, [needed_assignments[i].lhs for i in inner_idxs]) - union!(paramset, [needed_assignments[i].lhs for i in outer_idxs]) - params = collect(paramset) - - # splatting to tighten the type - u0 = [] - for v in vars - v in keys(u0map) || (push!(u0, 1e-3); continue) - u = substitute(v, u0map) - for i in 1:length(u0map) - u = substitute(u, u0map) - u isa Number && (push!(u0, u); break) - end - u isa Number || error("$v doesn't have a default.") - end - u0 = [u0...] - # specialize on the scalar case - isscalar = length(u0) == 1 - u0 = isscalar ? u0[1] : SVector(u0...) - - fname = gensym("fun") - # f is the function to find roots on - if isscalar - funex = rhss[1] - pre = get_preprocess_constants(funex) - else - funex = MakeArray(rhss, SVector) - pre = get_preprocess_constants(rhss) - end - f = Func( - [DestructuredArgs(vars, inbounds = !checkbounds) - DestructuredArgs(params, inbounds = !checkbounds)], - [], - pre(Let(needed_assignments[inner_idxs], - funex, - false))) |> SymbolicUtils.Code.toexpr - - # solver call contains code to call the root-finding solver on the function f - solver_call = LiteralExpr(quote - $numerical_nlsolve($fname, - # initial guess - $u0, - # "captured variables" - ($(params...),)) - end) - - preassignments = [] - for i in outer_idxs - ii = needed_assignments_idxs[i] - is_not_prepended_assignment[ii] || continue - is_not_prepended_assignment[ii] = false - push!(preassignments, assignments[ii]) - end - - nlsolve_expr = Assignment[preassignments - fname ← drop_expr(@RuntimeGeneratedFunction(f)) - DestructuredArgs(vars, inbounds = !checkbounds) ← solver_call] - - nlsolve_expr -end - """ find_solve_sequence(sccs, vars) @@ -242,136 +112,3 @@ function find_solve_sequence(sccs, vars) return find_solve_sequence(sccs, vars′) end end - -function build_observed_function(state, ts, var_eq_matching, var_sccs, - is_solver_unknown_idxs, - assignments, - deps, - sol_states, - var2assignment; - expression = false, - output_type = Array, - checkbounds = true) - is_not_prepended_assignment = trues(length(assignments)) - if (isscalar = !(ts isa AbstractVector)) - ts = [ts] - end - ts = unwrap.(Symbolics.scalarize(ts)) - - vars = Set() - sys = state.sys - foreach(Base.Fix1(vars!, vars), ts) - ivs = independent_variables(sys) - dep_vars = collect(setdiff(vars, ivs)) - - fullvars = state.fullvars - s = state.structure - unknown_vars = fullvars[is_solver_unknown_idxs] - algvars = fullvars[.!is_solver_unknown_idxs] - - required_algvars = Set(intersect(algvars, vars)) - obs = observed(sys) - observed_idx = Dict(x.lhs => i for (i, x) in enumerate(obs)) - namespaced_to_obs = Dict(unknowns(sys, x.lhs) => x.lhs for x in obs) - namespaced_to_sts = Dict(unknowns(sys, x) => x for x in unknowns(sys)) - sts = Set(unknowns(sys)) - - # FIXME: This is a rather rough estimate of dependencies. We assume - # the expression depends on everything before the `maxidx`. - subs = Dict() - maxidx = 0 - for (i, s) in enumerate(dep_vars) - idx = get(observed_idx, s, nothing) - if idx !== nothing - idx > maxidx && (maxidx = idx) - else - s′ = get(namespaced_to_obs, s, nothing) - if s′ !== nothing - subs[s] = s′ - s = s′ - idx = get(observed_idx, s, nothing) - end - if idx !== nothing - idx > maxidx && (maxidx = idx) - elseif !(s in sts) - s′ = get(namespaced_to_sts, s, nothing) - if s′ !== nothing - subs[s] = s′ - continue - end - throw(ArgumentError("$s is either an observed nor an unknown variable.")) - end - continue - end - end - ts = map(t -> substitute(t, subs), ts) - vs = Set() - for idx in 1:maxidx - vars!(vs, obs[idx].rhs) - union!(required_algvars, intersect(algvars, vs)) - empty!(vs) - end - for eq in assignments - vars!(vs, eq.rhs) - union!(required_algvars, intersect(algvars, vs)) - empty!(vs) - end - - varidxs = findall(x -> x in required_algvars, fullvars) - subset = find_solve_sequence(var_sccs, varidxs) - if !isempty(subset) - eqs = equations(sys) - - nested_torn_vars_idxs = [] - for iscc in subset - torn_vars_idxs = Int[var - for var in var_sccs[iscc] - if var_eq_matching[var] !== unassigned] - isempty(torn_vars_idxs) || push!(nested_torn_vars_idxs, torn_vars_idxs) - end - torn_eqs = [[eqs[var_eq_matching[i]] for i in idxs] - for idxs in nested_torn_vars_idxs] - torn_vars = [fullvars[idxs] for idxs in nested_torn_vars_idxs] - u0map = defaults(sys) - assignments = copy(assignments) - solves = map(zip(torn_eqs, torn_vars)) do (eqs, vars) - gen_nlsolve!(is_not_prepended_assignment, eqs, vars, - u0map, assignments, deps, var2assignment; - checkbounds = checkbounds) - end - else - solves = [] - end - - subs = [] - for sym in vars - eqidx = get(observed_idx, sym, nothing) - eqidx === nothing && continue - push!(subs, sym ← obs[eqidx].rhs) - end - pre = get_postprocess_fbody(sys) - cpre = get_preprocess_constants([obs[1:maxidx]; - isscalar ? ts[1] : MakeArray(ts, output_type)]) - pre2 = x -> pre(cpre(x)) - ex = Code.toexpr( - Func( - [DestructuredArgs(unknown_vars, inbounds = !checkbounds) - DestructuredArgs(parameters(sys), inbounds = !checkbounds) - independent_variables(sys)], - [], - pre2(Let( - [collect(Iterators.flatten(solves)) - assignments[is_not_prepended_assignment] - map(eq -> eq.lhs ← eq.rhs, obs[1:maxidx]) - subs], - isscalar ? ts[1] : MakeArray(ts, output_type), - false))), - sol_states) - - expression ? ex : drop_expr(@RuntimeGeneratedFunction(ex)) -end - -struct ODAEProblem{iip} end - -@deprecate ODAEProblem(args...; kw...) ODEProblem(args...; kw...) -@deprecate ODAEProblem{iip}(args...; kw...) where {iip} ODEProblem{iip}(args...; kw...) diff --git a/src/structural_transformation/utils.jl b/src/structural_transformation/utils.jl index 191c25ab68..2bf316cfa6 100644 --- a/src/structural_transformation/utils.jl +++ b/src/structural_transformation/utils.jl @@ -224,14 +224,12 @@ function find_eq_solvables!(state::TearingState, ieq, to_rm = Int[], coeffs = no a, b, islinear = linear_expansion(term, var) a, b = unwrap(a), unwrap(b) islinear || (all_int_vars = false; continue) - a = ModelingToolkit.fold_constants(a) - b = ModelingToolkit.fold_constants(b) if a isa Symbolic all_int_vars = false if !allow_symbolic if allow_parameter all( - x -> ModelingToolkit.isparameter(x) || ModelingToolkit.isconstant(x), + x -> ModelingToolkit.isparameter(x), vars(a)) || continue else continue diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index 4021f1fc44..1a90faad92 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -2540,18 +2540,6 @@ function debug_system( return sys end -function eliminate_constants(sys::AbstractSystem) - if has_eqs(sys) - eqs = get_eqs(sys) - eq_cs = collect_constants(eqs) - if !isempty(eq_cs) - new_eqs = eliminate_constants(eqs, eq_cs) - @set! sys.eqs = new_eqs - end - end - return sys -end - @latexrecipe function f(sys::AbstractSystem) return latexify(equations(sys)) end diff --git a/src/systems/callbacks.jl b/src/systems/callbacks.jl index 0bb2318e4b..59ff0e0d98 100644 --- a/src/systems/callbacks.jl +++ b/src/systems/callbacks.jl @@ -635,11 +635,6 @@ function compile_condition( p = map.(value, reorder_parameters(sys, ps)) t = get_iv(sys) condit = conditions(cbs) - cs = collect_constants(condit) - if !isempty(cs) - cmap = map(x -> x => getdefault(x), cs) - condit = substitute(condit, Dict(cmap)) - end if !is_discrete(cbs) condit = reduce(vcat, flatten_equations(Vector{Equation}(condit))) diff --git a/src/systems/codegen_utils.jl b/src/systems/codegen_utils.jl index 8c2c322e31..c3b652740e 100644 --- a/src/systems/codegen_utils.jl +++ b/src/systems/codegen_utils.jl @@ -247,15 +247,7 @@ function build_function_wrapper(sys::AbstractSystem, expr, args...; p_start = 2, p_end += 1 end pdeps = parameter_dependencies(sys) - # get the constants to add to the code - cmap, _ = get_cmap(sys) - extra_constants = collect_constants(expr) - filter!(extra_constants) do c - !any(x -> isequal(c, x.lhs), cmap) - end - for c in extra_constants - push!(cmap, c ~ getdefault(c)) - end + # only get the necessary observed equations, avoiding extra computation if add_observed && !isempty(obs) obsidxs = observed_equations_used_by(sys, expr; obs) @@ -270,7 +262,7 @@ function build_function_wrapper(sys::AbstractSystem, expr, args...; p_start = 2, # assignments for reconstructing scalarized array symbolics assignments = array_variable_assignments(args...) - for eq in Iterators.flatten((cmap, pdeps[pdepidxs], obs[obsidxs])) + for eq in Iterators.flatten((pdeps[pdepidxs], obs[obsidxs])) push!(assignments, eq.lhs ← eq.rhs) end append!(assignments, extra_assignments) diff --git a/src/systems/nonlinear/initializesystem.jl b/src/systems/nonlinear/initializesystem.jl index 646ce81112..a550f9d740 100644 --- a/src/systems/nonlinear/initializesystem.jl +++ b/src/systems/nonlinear/initializesystem.jl @@ -533,7 +533,6 @@ function SciMLBase.remake_initialization_data( symbols_to_symbolics!(sys, pmap) guesses = Dict() defs = defaults(sys) - cmap, cs = get_cmap(sys) use_scc = true initialization_eqs = Equation[] @@ -588,7 +587,7 @@ function SciMLBase.remake_initialization_data( filter_missing_values!(pmap) op, missing_unknowns, missing_pars = build_operating_point!(sys, - u0map, pmap, defs, cmap, dvs, ps) + u0map, pmap, defs, dvs, ps) floatT = float_type_from_varmap(op) u0_constructor = p_constructor = identity if newu0 isa StaticArray diff --git a/src/systems/parameter_buffer.jl b/src/systems/parameter_buffer.jl index c3d2a0e831..83d165eefc 100644 --- a/src/systems/parameter_buffer.jl +++ b/src/systems/parameter_buffer.jl @@ -45,13 +45,12 @@ function MTKParameters( p = to_varmap(p, ps) symbols_to_symbolics!(sys, p) defs = add_toterms(recursive_unwrap(defaults(sys))) - cmap, cs = get_cmap(sys) is_time_dependent(sys) && add_observed!(sys, u0) add_parameter_dependencies!(sys, p) op, missing_unknowns, missing_pars = build_operating_point!(sys, - u0, p, defs, cmap, dvs, ps) + u0, p, defs, dvs, ps) if t0 !== nothing op[get_iv(sys)] = t0 diff --git a/src/systems/problem_utils.jl b/src/systems/problem_utils.jl index 8562bb1cfa..8cff86f9d3 100644 --- a/src/systems/problem_utils.jl +++ b/src/systems/problem_utils.jl @@ -532,15 +532,15 @@ end $(TYPEDSIGNATURES) Construct the operating point of the system from the user-provided `u0map` and `pmap`, system -defaults `defs`, constant equations `cmap` (from `get_cmap(sys)`), unknowns `dvs` and -parameters `ps`. Return the operating point as a dictionary, the list of unknowns for which -no values can be determined, and the list of parameters for which no values can be determined. +defaults `defs`, unknowns `dvs` and parameters `ps`. Return the operating point as a dictionary, +the list of unknowns for which no values can be determined, and the list of parameters for which +no values can be determined. Also updates `u0map` and `pmap` in-place to contain all the initial conditions in `op`, split by unknowns and parameters respectively. """ function build_operating_point!(sys::AbstractSystem, - u0map::AbstractDict, pmap::AbstractDict, defs::AbstractDict, cmap, dvs, ps) + u0map::AbstractDict, pmap::AbstractDict, defs::AbstractDict, dvs, ps) op = add_toterms(u0map) missing_unknowns = add_fallbacks!(op, dvs, defs) for (k, v) in defs @@ -552,9 +552,6 @@ function build_operating_point!(sys::AbstractSystem, merge!(op, pmap) missing_pars = add_fallbacks!(op, ps, defs) filter_missing_values!(op; missing_values = missing_pars) - for eq in cmap - op[eq.lhs] = eq.rhs - end filter!(kvp -> kvp[2] === nothing, u0map) filter!(kvp -> kvp[2] === nothing, pmap) @@ -1254,7 +1251,6 @@ function process_SciMLProblem( check_inputmap_keys(sys, u0map, pmap) defs = add_toterms(recursive_unwrap(defaults(sys))) - cmap, cs = get_cmap(sys) kwargs = NamedTuple(kwargs) if eltype(eqs) <: Equation @@ -1264,7 +1260,7 @@ function process_SciMLProblem( end op, missing_unknowns, missing_pars = build_operating_point!(sys, - u0map, pmap, defs, cmap, dvs, ps) + u0map, pmap, defs, dvs, ps) floatT = Bool if u0Type <: AbstractArray && eltype(u0Type) <: Real && eltype(u0Type) != Union{} diff --git a/src/systems/systemstructure.jl b/src/systems/systemstructure.jl index 144aad148e..c1b2c337ac 100644 --- a/src/systems/systemstructure.jl +++ b/src/systems/systemstructure.jl @@ -5,7 +5,7 @@ using SymbolicUtils: quick_cancel, maketerm using ..ModelingToolkit import ..ModelingToolkit: isdiffeq, var_from_nested_derivative, vars!, flatten, value, InvalidSystemException, isdifferential, _iszero, - isparameter, isconstant, + isparameter, independent_variables, SparseMatrixCLIL, AbstractSystem, equations, isirreducible, input_timedomain, TimeDomain, InferredTimeDomain, @@ -314,7 +314,7 @@ function TearingState(sys; quick_cancel = false, check = true, sort_eqs = true) _var, _ = var_from_nested_derivative(v) any(isequal(_var), ivs) && continue if isparameter(_var) || - (iscall(_var) && isparameter(operation(_var)) || isconstant(_var)) + (iscall(_var) && isparameter(operation(_var))) if is_time_dependent_parameter(_var, iv) && !haskey(param_derivative_map, Differential(iv)(_var)) # Parameter derivatives default to zero - they stay constant @@ -339,7 +339,7 @@ function TearingState(sys; quick_cancel = false, check = true, sort_eqs = true) _var, _ = var_from_nested_derivative(var) any(isequal(_var), ivs) && continue if isparameter(_var) || - (iscall(_var) && isparameter(operation(_var)) || isconstant(_var)) + (iscall(_var) && isparameter(operation(_var))) continue end varidx = addvar!(var) diff --git a/src/utils.jl b/src/utils.jl index efa6196af8..e1e8c50af4 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -708,7 +708,7 @@ function collect_var!(unknowns, parameters, var, iv; depth = 0) collect_vars!(unknowns, parameters, arguments(var), iv) elseif isparameter(var) || (iscall(var) && isparameter(operation(var))) push!(parameters, var) - elseif !isconstant(var) + else push!(unknowns, var) end # Add also any parameters that appear only as defaults in the var @@ -734,90 +734,6 @@ function check_scope_depth(scope, depth) end end -""" -Find all the symbolic constants of some equations or terms and return them as a vector. -""" -function collect_constants(x) - constants = BasicSymbolic[] - collect_constants!(constants, x) - return constants -end - -collect_constants!(::Any, ::Symbol) = nothing - -function collect_constants!(constants, arr::AbstractArray) - for el in arr - collect_constants!(constants, el) - end -end - -function collect_constants!(constants, eq::Equation) - collect_constants!(constants, eq.lhs) - collect_constants!(constants, eq.rhs) -end - -function collect_constants!(constants, eq::Inequality) - collect_constants!(constants, eq.lhs) - collect_constants!(constants, eq.rhs) -end - -collect_constants!(constants, x::Num) = collect_constants!(constants, unwrap(x)) -collect_constants!(constants, x::Real) = nothing -collect_constants(n::Nothing) = BasicSymbolic[] - -function collect_constants!(constants, expr::Symbolic) - if issym(expr) && isconstant(expr) - push!(constants, expr) - else - evars = vars(expr) - if length(evars) == 1 && isequal(only(evars), expr) - return nothing #avoid infinite recursion for vars(x(t)) == [x(t)] - else - for var in evars - collect_constants!(constants, var) - end - end - end -end - -function collect_constants!(constants, expr::Union{ConstantRateJump, VariableRateJump}) - collect_constants!(constants, expr.rate) - collect_constants!(constants, expr.affect!) -end - -function collect_constants!(constants, ::MassActionJump) - return constants -end - -""" -Replace symbolic constants with their literal values -""" -function eliminate_constants(eqs, cs) - cmap = Dict(x => getdefault(x) for x in cs) - return substitute(eqs, cmap) -end - -""" -Create a function preface containing assignments of default values to constants. -""" -function get_preprocess_constants(eqs) - cs = collect_constants(eqs) - pre = ex -> Let(Assignment[Assignment(x, getdefault(x)) for x in cs], - ex, false) - return pre -end - -function get_postprocess_fbody(sys) - if has_preface(sys) && (pre = preface(sys); pre !== nothing) - pre_ = let pre = pre - ex -> Let(pre, ex, false) - end - else - pre_ = ex -> ex - end - return pre_ -end - """ $(SIGNATURES) @@ -838,22 +754,6 @@ end isarray(x) = x isa AbstractArray || x isa Symbolics.Arr -function get_cmap(sys, exprs = nothing) - #Inject substitutions for constants => values - buffer = [] - has_eqs(sys) && append!(buffer, collect(get_eqs(sys))) - has_observed(sys) && append!(buffer, collect(get_observed(sys))) - has_op(sys) && push!(buffer, get_op(sys)) - has_constraints(sys) && append!(buffer, get_constraints(sys)) - cs = collect_constants(buffer) #ctrls? what else? - if exprs !== nothing - cs = [cs; collect_constants(exprs)] - end - # Swap constants for their values - cmap = map(x -> x ~ getdefault(x), cs) - return cmap, cs -end - function empty_substitutions(sys) isempty(observed(sys)) end @@ -1043,21 +943,6 @@ function Base.iterate(it::StatefulBFS, queue = (eltype(it)[(0, it.t)])) return (lv, t), queue end -function fold_constants(ex) - if iscall(ex) - maketerm(typeof(ex), operation(ex), map(fold_constants, arguments(ex)), - metadata(ex)) - elseif issym(ex) && isconstant(ex) - if (unit = getmetadata(ex, VariableUnit, nothing); unit !== nothing) - ex # we cannot fold constant with units - else - getdefault(ex) - end - else - ex - end -end - normalize_to_differential(s) = s function restrict_array_to_union(arr) From 92781396cc22c13d288688a3371458223fd75dd7 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 19 May 2025 23:08:06 +0530 Subject: [PATCH 1787/2176] refactor: make `@constants` create non-tunable parameters --- src/constants.jl | 25 ++++--------------------- 1 file changed, 4 insertions(+), 21 deletions(-) diff --git a/src/constants.jl b/src/constants.jl index a0a38fd057..4113287ad4 100644 --- a/src/constants.jl +++ b/src/constants.jl @@ -1,13 +1,9 @@ -import SymbolicUtils: symtype, term, hasmetadata, issym -struct MTKConstantCtx end - -isconstant(x::Num) = isconstant(unwrap(x)) """ Test whether `x` is a constant-type Sym. """ function isconstant(x) x = unwrap(x) - x isa Symbolic && getmetadata(x, MTKConstantCtx, false) + x isa Symbolic && !getmetadata(x, VariableTunable, true) end """ @@ -16,12 +12,11 @@ end Maps the parameter to a constant. The parameter must have a default. """ function toconstant(s) - hasmetadata(s, Symbolics.VariableDefaultValue) || - throw(ArgumentError("Constant `$(s)` must be assigned a default value.")) - setmetadata(s, MTKConstantCtx, true) + s = toparam(s) + setmetadata(s, VariableTunable, false) end -toconstant(s::Num) = wrap(toconstant(value(s))) +toconstant(s::Union{Num, Symbolics.Arr}) = wrap(toconstant(value(s))) """ $(SIGNATURES) @@ -36,15 +31,3 @@ macro constants(xs...) xs, toconstant) |> esc end - -""" -Substitute all `@constants` in the given expression -""" -function subs_constants(eqs) - consts = collect_constants(eqs) - if !isempty(consts) - csubs = Dict(c => getdefault(c) for c in consts) - eqs = substitute(eqs, csubs) - end - return eqs -end From 2b140de66adea58c5d5a6aa280aa80afc1be3dce Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 19 May 2025 23:08:16 +0530 Subject: [PATCH 1788/2176] refactor: update `@constants` parsing in `@mtkmodel` --- src/systems/model_parsing.jl | 76 +++++++++++++----------------------- 1 file changed, 28 insertions(+), 48 deletions(-) diff --git a/src/systems/model_parsing.jl b/src/systems/model_parsing.jl index 8fe07f7f99..9d293fd40d 100644 --- a/src/systems/model_parsing.jl +++ b/src/systems/model_parsing.jl @@ -53,7 +53,6 @@ function _model_macro(mod, fullname::Union{Expr, Symbol}, expr, isconnector) end exprs = Expr(:block) dict = Dict{Symbol, Any}( - :constants => Dict{Symbol, Dict}(), :defaults => Dict{Symbol, Any}(), :kwargs => Dict{Symbol, Dict}(), :structural_parameters => Dict{Symbol, Dict}() @@ -125,7 +124,7 @@ function _model_macro(mod, fullname::Union{Expr, Symbol}, expr, isconnector) description = get(dict, :description, "") @inline pop_structure_dict!.( - Ref(dict), [:constants, :defaults, :kwargs, :structural_parameters]) + Ref(dict), [:defaults, :kwargs, :structural_parameters]) sys = :($type($(flatten_equations)(equations), $iv, variables, parameters; name, description = $description, systems, gui_metadata = $gui_metadata, @@ -320,6 +319,10 @@ Base.@nospecializeinfer function parse_variable_def!( Meta.isexpr(a, :call) && assert_unique_independent_var(dict, a.args[end]) var = :($varname = $first(@parameters ($a[$(indices...)]::$type = $varval), $meta_val)) + elseif varclass == :constants + Meta.isexpr(a, :call) && assert_unique_independent_var(dict, a.args[end]) + var = :($varname = $first(@constants ($a[$(indices...)]::$type = $varval), + $meta_val)) else Meta.isexpr(a, :call) || throw("$a is not a variable of the independent variable") @@ -351,6 +354,12 @@ Base.@nospecializeinfer function parse_variable_def!( var = :($varname = $varname === $NO_VALUE ? $val : $varname; $varname = $first(@parameters ($a[$(indices...)]::$type = $varval), $(def_n_meta...))) + elseif varclass == :constants + Meta.isexpr(a, :call) && + assert_unique_independent_var(dict, a.args[end]) + var = :($varname = $varname === $NO_VALUE ? $val : $varname; + $varname = $first(@constants ($a[$(indices...)]::$type = $varval), + $(def_n_meta...))) else Meta.isexpr(a, :call) || throw("$a is not a variable of the independent variable") @@ -366,6 +375,11 @@ Base.@nospecializeinfer function parse_variable_def!( assert_unique_independent_var(dict, a.args[end]) var = :($varname = $varname === $NO_VALUE ? $def_n_meta : $varname; $varname = $first(@parameters $a[$(indices...)]::$type = $varname)) + elseif varclass == :constants + Meta.isexpr(a, :call) && + assert_unique_independent_var(dict, a.args[end]) + var = :($varname = $varname === $NO_VALUE ? $def_n_meta : $varname; + $varname = $first(@constants $a[$(indices...)]::$type = $varname)) else Meta.isexpr(a, :call) || throw("$a is not a variable of the independent variable") @@ -393,6 +407,9 @@ Base.@nospecializeinfer function parse_variable_def!( if varclass == :parameters Meta.isexpr(a, :call) && assert_unique_independent_var(dict, a.args[end]) var = :($varname = $first(@parameters $a[$(indices...)]::$type = $varname)) + elseif varclass == :constants + Meta.isexpr(a, :call) && assert_unique_independent_var(dict, a.args[end]) + var = :($varname = $first(@constants $a[$(indices...)]::$type = $varname)) elseif varclass == :variables Meta.isexpr(a, :call) || throw("$a is not a variable of the independent variable") @@ -453,6 +470,8 @@ function generate_var(a, varclass; type = Real) var = Symbolics.variable(a; T = type) if varclass == :parameters var = toparam(var) + elseif varclass == :constants + var = toconstant(var) elseif varclass == :independent_variables var = toiv(var) end @@ -513,6 +532,8 @@ function generate_var!(dict, a, b, varclass, mod; end if varclass == :parameters var = toparam(var) + elseif varclass == :constants + var = toconstant(var) end var end @@ -622,7 +643,7 @@ function parse_model!(exprs, comps, ext, eqs, icon, vs, ps, sps, c_evts, d_evts, elseif mname == Symbol("@equations") parse_equations!(exprs, eqs, dict, body) elseif mname == Symbol("@constants") - parse_constants!(exprs, dict, body, mod) + parse_variables!(exprs, ps, dict, mod, body, :constants, kwargs, where_types) elseif mname == Symbol("@continuous_events") parse_continuous_events!(c_evts, dict, body) elseif mname == Symbol("@discrete_events") @@ -643,49 +664,6 @@ function parse_model!(exprs, comps, ext, eqs, icon, vs, ps, sps, c_evts, d_evts, end end -function parse_constants!(exprs, dict, body, mod) - Base.remove_linenums!(body) - for arg in body.args - MLStyle.@match arg begin - Expr(:(=), Expr(:(::), a, type), Expr(:tuple, b, metadata)) || Expr(:(=), Expr(:(::), a, type), b) => begin - type = getfield(mod, type) - b = _type_check!(get_var(mod, b), a, type, :constants) - push!(exprs, - :($(Symbolics._parse_vars( - :constants, type, [:($a = $b), metadata], toconstant)))) - dict[:constants][a] = Dict(:value => b, :type => type) - if @isdefined metadata - for data in metadata.args - dict[:constants][a][data.args[1]] = data.args[2] - end - end - end - Expr(:(=), a, Expr(:tuple, b, metadata)) => begin - push!(exprs, - :($(Symbolics._parse_vars( - :constants, Real, [:($a = $b), metadata], toconstant)))) - dict[:constants][a] = Dict{Symbol, Any}(:value => get_var(mod, b)) - for data in metadata.args - dict[:constants][a][data.args[1]] = data.args[2] - end - end - Expr(:(=), a, b) => begin - push!(exprs, - :($(Symbolics._parse_vars( - :constants, Real, [:($a = $b)], toconstant)))) - dict[:constants][a] = Dict(:value => get_var(mod, b)) - end - _ => error("""Malformed constant definition `$arg`. Please use the following syntax: - ``` - @constants begin - var = value, [description = "This is an example constant."] - end - ``` - """) - end - end -end - push_additional_defaults!(dict, a, b::Number) = dict[:defaults][a] = b push_additional_defaults!(dict, a, b::QuoteNode) = dict[:defaults][a] = b.value function push_additional_defaults!(dict, a, b::Expr) @@ -950,6 +928,7 @@ function handle_conditional_vars!( arg, conditional_branch, mod, varclass, kwargs, where_types) conditional_dict = Dict(:kwargs => Dict(), :parameters => Any[Dict{Symbol, Dict{Symbol, Any}}()], + :constants => Any[Dict{Symbol, Dict{Symbol, Any}}()], :variables => Any[Dict{Symbol, Dict{Symbol, Any}}()]) for _arg in arg.args name, ex = parse_variable_arg( @@ -964,7 +943,7 @@ function prune_conditional_dict!(conditional_tuple::Tuple) prune_conditional_dict!.(collect(conditional_tuple)) end function prune_conditional_dict!(conditional_dict::Dict) - for k in [:parameters, :variables] + for k in [:parameters, :variables, :constants] length(conditional_dict[k]) == 1 && isempty(first(conditional_dict[k])) && delete!(conditional_dict, k) end @@ -981,7 +960,7 @@ end function get_conditional_dict!(conditional_dict::Dict, conditional_y_tuple::Dict) merge!(conditional_dict[:kwargs], conditional_y_tuple[:kwargs]) - for key in [:parameters, :variables] + for key in [:parameters, :variables, :constants] merge!(conditional_dict[key][1], conditional_y_tuple[key][1]) end conditional_dict @@ -1000,6 +979,7 @@ function push_conditional_dict!(dict, condition, conditional_dict, end conditional_y_dict = Dict(:kwargs => Dict(), :parameters => Any[Dict{Symbol, Dict{Symbol, Any}}()], + :constants => Any[Dict{Symbol, Dict{Symbol, Any}}()], :variables => Any[Dict{Symbol, Dict{Symbol, Any}}()]) get_conditional_dict!(conditional_y_dict, conditional_y_tuple) From 9f39e289a04842d5c810022b4c2cc9396f686181 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 19 May 2025 23:08:36 +0530 Subject: [PATCH 1789/2176] test: account for new `@constants` behavior in tests --- test/components.jl | 2 +- test/constants.jl | 13 +++++-------- test/discrete_system.jl | 2 +- test/dq_units.jl | 3 +-- test/funcaffect.jl | 2 +- test/input_output_handling.jl | 2 +- test/jumpsystem.jl | 13 ++++++++----- test/model_parsing.jl | 2 +- test/nonlinearsystem.jl | 18 +++++++++--------- test/odesystem.jl | 13 +++++++------ test/parameter_dependencies.jl | 8 ++++---- test/structural_transformation/tearing.jl | 12 +++++------- 12 files changed, 44 insertions(+), 46 deletions(-) diff --git a/test/components.jl b/test/components.jl index 7680afc50c..6102762d01 100644 --- a/test/components.jl +++ b/test/components.jl @@ -230,7 +230,7 @@ end eqs = [ v ~ i * R ] - extend(System(eqs, t, [], []; name = name), oneport) + extend(System(eqs, t, [], [R]; name = name), oneport) end capacitor = Capacitor(; name = :c1, C = 1.0) resistor = FixedResistor(; name = :r1) diff --git a/test/constants.jl b/test/constants.jl index 5e97d52d7f..bd28517ae6 100644 --- a/test/constants.jl +++ b/test/constants.jl @@ -4,7 +4,8 @@ MT = ModelingToolkit UMT = ModelingToolkit.UnitfulUnitCheck @constants a = 1 -@test_throws MT.ArgumentError @constants b +@test isconstant(a) +@test !istunable(a) @independent_variables t @variables x(t) w(t) @@ -14,9 +15,6 @@ eqs = [D(x) ~ a] prob = ODEProblem(complete(sys), [0], [0.0, 1.0], []) sol = solve(prob, Tsit5()) -newsys = MT.eliminate_constants(sys) -@test isequal(equations(newsys), [D(x) ~ 1]) - # Test structural_simplify substitutions & observed values eqs = [D(x) ~ 1, w ~ a] @@ -29,6 +27,7 @@ simp = structural_simplify(sys) @constants β=1 [unit = u"m/s"] UMT.get_unit(β) @test MT.isconstant(β) +@test !MT.istunable(β) @independent_variables t [unit = u"s"] @variables x(t) [unit = u"m"] D = Differential(t) @@ -36,17 +35,15 @@ eqs = [D(x) ~ β] @named sys = System(eqs, t) simp = structural_simplify(sys) -@test isempty(MT.collect_constants(nothing)) - @testset "Issue#3044" begin - @constants h = 1 + @constants h @parameters τ = 0.5 * h @variables x(MT.t_nounits) = h eqs = [MT.D_nounits(x) ~ (h - x) / τ] @mtkbuild fol_model = System(eqs, MT.t_nounits) - prob = ODEProblem(fol_model, [], (0.0, 10.0)) + prob = ODEProblem(fol_model, [], (0.0, 10.0), [h => 1]) @test prob[x] ≈ 1 @test prob.ps[τ] ≈ 0.5 end diff --git a/test/discrete_system.jl b/test/discrete_system.jl index 1ed6438d76..f3c5bff496 100644 --- a/test/discrete_system.jl +++ b/test/discrete_system.jl @@ -30,7 +30,7 @@ eqs = [S ~ S(k - 1) - infection * h, R ~ R(k - 1) + recovery] # System -@named sys = System(eqs, t, [S, I, R], [c, nsteps, δt, β, γ]) +@named sys = System(eqs, t, [S, I, R], [c, nsteps, δt, β, γ, h]) syss = structural_simplify(sys) @test syss == syss diff --git a/test/dq_units.jl b/test/dq_units.jl index 4d8c245e06..f0dc2dbe23 100644 --- a/test/dq_units.jl +++ b/test/dq_units.jl @@ -233,8 +233,7 @@ end L(t), [unit = u"m"] L_out(t), [unit = u"1"] end -@test to_m in ModelingToolkit.vars(ModelingToolkit.fold_constants(Symbolics.unwrap(L_out * - -to_m))) +@test to_m in ModelingToolkit.vars(Symbolics.unwrap(L_out * -to_m)) # test units for registered functions let diff --git a/test/funcaffect.jl b/test/funcaffect.jl index 8e73280eb3..b0745c8a9d 100644 --- a/test/funcaffect.jl +++ b/test/funcaffect.jl @@ -282,7 +282,7 @@ function bb_affect!(integ, u, p, ctx) integ.u[u.v] = -integ.u[u.v] end -@named bb_model = System(bb_eqs, t, sts, par, +@named bb_model = System(bb_eqs, t, sts, [par; zr], continuous_events = [ [y ~ zr] => (bb_affect!, [v], [], [], nothing) ]) diff --git a/test/input_output_handling.jl b/test/input_output_handling.jl index 1fd7732c50..1f14bc3814 100644 --- a/test/input_output_handling.jl +++ b/test/input_output_handling.jl @@ -447,7 +447,7 @@ end @constants c = 2.0 @variables x(t) eqs = [D(x) ~ c * x] - @mtkbuild sys = System(eqs, t, [x], []) + @mtkbuild sys = System(eqs, t, [x], [c]) f, dvs, ps, io_sys = ModelingToolkit.generate_control_function(sys) @test f[1]([0.5], nothing, MTKParameters(io_sys, []), 0.0) ≈ [1.0] diff --git a/test/jumpsystem.jl b/test/jumpsystem.jl index 50d59b3313..a42ba5ecb9 100644 --- a/test/jumpsystem.jl +++ b/test/jumpsystem.jl @@ -1,4 +1,5 @@ using ModelingToolkit, DiffEqBase, JumpProcesses, Test, LinearAlgebra +using SymbolicIndexingInterface using Random, StableRNGs, NonlinearSolve using OrdinaryDiffEq using ModelingToolkit: t_nounits as t, D_nounits as D @@ -17,7 +18,7 @@ rate₂ = γ * I + t affect₂ = [I ~ Pre(I) - 1, R ~ Pre(R) + 1] j₁ = ConstantRateJump(rate₁, affect₁) j₂ = VariableRateJump(rate₂, affect₂) -@named js = JumpSystem([j₁, j₂], t, [S, I, R], [β, γ]) +@named js = JumpSystem([j₁, j₂], t, [S, I, R], [β, γ, h]) unknowntoid = Dict(MT.value(unknown) => i for (i, unknown) in enumerate(unknowns(js))) mtjump1 = MT.assemble_crj(js, j₁, unknowntoid) mtjump2 = MT.assemble_vrj(js, j₂, unknowntoid) @@ -38,7 +39,7 @@ jump2 = VariableRateJump(rate2, affect2!) # test crjs u = [100, 9, 5] -p = (0.1 / 1000, 0.01) +p = (0.1 / 1000, 0.01, 1) tf = 1.0 mutable struct TestInt{U, V, T} u::U @@ -62,15 +63,15 @@ jump2.affect!(integrator) rate₃ = γ * I * h affect₃ = [I ~ Pre(I) * h - 1, R ~ Pre(R) + 1] j₃ = ConstantRateJump(rate₃, affect₃) -@named js2 = JumpSystem([j₁, j₃], t, [S, I, R], [β, γ]) +@named js2 = JumpSystem([j₁, j₃], t, [S, I, R], [β, γ, h]) js2 = complete(js2) u₀ = [999, 1, 0]; -p = (0.1 / 1000, 0.01); tspan = (0.0, 250.0); u₀map = [S => 999, I => 1, R => 0] parammap = [β => 0.1 / 1000, γ => 0.01] jprob = JumpProblem(js2, u₀map, tspan, parammap; aggregator = Direct(), save_positions = (false, false), rng) +p = parameter_values(jprob) @test jprob.prob isa DiscreteProblem Nsims = 30000 function getmean(jprob, Nsims; use_stepper = true) @@ -90,7 +91,7 @@ mb = getmean(jprobb, Nsims; use_stepper = false) @variables S2(t) obs = [S2 ~ 2 * S] -@named js2b = JumpSystem([j₁, j₃], t, [S, I, R], [β, γ], observed = obs) +@named js2b = JumpSystem([j₁, j₃], t, [S, I, R], [β, γ, h], observed = obs) js2b = complete(js2b) jprob = JumpProblem(js2b, u₀map, tspan, parammap; aggregator = Direct(), save_positions = (false, false), rng) @@ -110,6 +111,8 @@ jump2 = ConstantRateJump(rate2, affect2!) mtjumps = jprob.discrete_jump_aggregation @test abs(mtjumps.rates[1](u, p, tf) - jump1.rate(u, p, tf)) < 10 * eps() @test abs(mtjumps.rates[2](u, p, tf) - jump2.rate(u, p, tf)) < 10 * eps() + +ModelingToolkit.@set! mtintegrator.p = (mtintegrator.p, (1,)) mtjumps.affects![1](mtintegrator) jump1.affect!(integrator) @test all(integrator.u .== mtintegrator.u) diff --git a/test/model_parsing.jl b/test/model_parsing.jl index 705ec79816..88fa7faac7 100644 --- a/test/model_parsing.jl +++ b/test/model_parsing.jl @@ -511,7 +511,7 @@ using ModelingToolkit: getdefault, scalarize @test eval(ModelWithComponentArray.structure[:parameters][:r][:unit]) == eval(u"Ω") - @test lastindex(parameters(model_with_component_array)) == 3 + @test lastindex(parameters(model_with_component_array)) == 4 # Test the constant `k`. Manually k's value should be kept in sync here # and the ModelParsingPrecompile. diff --git a/test/nonlinearsystem.jl b/test/nonlinearsystem.jl index 3eb141ebc1..313866d1d8 100644 --- a/test/nonlinearsystem.jl +++ b/test/nonlinearsystem.jl @@ -26,13 +26,13 @@ end eqs = [0 ~ σ * (y - x) * h, 0 ~ x * (ρ - z) - y, 0 ~ x * y - β * z] -@named ns = System(eqs, [x, y, z], [σ, ρ, β], defaults = Dict(x => 2)) +@named ns = System(eqs, [x, y, z], [σ, ρ, β, h], defaults = Dict(x => 2)) @test eval(toexpr(ns)) == ns -test_nlsys_inference("standard", ns, (x, y, z), (σ, ρ, β)) +test_nlsys_inference("standard", ns, (x, y, z), (σ, ρ, β, h)) @test begin - f = generate_rhs(ns, [x, y, z], [σ, ρ, β], expression = Val{false})[2] + f = generate_rhs(ns, [x, y, z], [σ, ρ, β, h], expression = Val{false})[2] du = [0.0, 0.0, 0.0] - f(du, [1, 2, 3], [1, 2, 3]) + f(du, [1, 2, 3], [1, 2, 3, 1]) du ≈ [1, -3, -7] end @@ -64,9 +64,9 @@ a = y - x eqs = [0 ~ σ * a * h, 0 ~ x * (ρ - z) - y, 0 ~ x * y - β * z] -@named ns = System(eqs, [x, y, z], [σ, ρ, β]) +@named ns = System(eqs, [x, y, z], [σ, ρ, β, h]) ns = complete(ns) -nlsys_func = generate_rhs(ns, [x, y, z], [σ, ρ, β]) +nlsys_func = generate_rhs(ns, [x, y, z], [σ, ρ, β, h]) nf = NonlinearFunction(ns) jac = calculate_jacobian(ns) @@ -99,7 +99,7 @@ eqs1 = [ 0 ~ x + y - z - u ] -lorenz = name -> System(eqs1, [x, y, z, u, F], [σ, ρ, β], name = name) +lorenz = name -> System(eqs1, [x, y, z, u, F], [σ, ρ, β, h], name = name) lorenz1 = lorenz(:lorenz1) @test_throws ArgumentError NonlinearProblem(complete(lorenz1), zeros(5), zeros(3)) lorenz2 = lorenz(:lorenz2) @@ -132,7 +132,7 @@ sol = solve(prob, FBDF(), reltol = 1e-7, abstol = 1e-7) eqs = [0 ~ σ * (y - x), 0 ~ x * (ρ - z) - y, 0 ~ x * y - β * z * h] -@named ns = System(eqs, [x, y, z], [σ, ρ, β]) +@named ns = System(eqs, [x, y, z], [σ, ρ, β, h]) np = NonlinearProblem( complete(ns), [0, 0, 0], [σ => 1, ρ => 2, β => 3], jac = true, sparse = true) @test calculate_jacobian(ns, sparse = true) isa SparseMatrixCSC @@ -214,7 +214,7 @@ testdict = Dict([:test => 1]) eqs = [0 ~ a * (y - x) * h, 0 ~ x * (b - z) - y, 0 ~ x * y - c * z] - @named sys = System(eqs, [x, y, z], [a, b, c], defaults = Dict(x => 2.0)) + @named sys = System(eqs, [x, y, z], [a, b, c, h], defaults = Dict(x => 2.0)) sys = complete(sys) prob = NonlinearProblem(sys, ones(length(unknowns(sys)))) diff --git a/test/odesystem.jl b/test/odesystem.jl index 904ca58d1a..33267e8c78 100644 --- a/test/odesystem.jl +++ b/test/odesystem.jl @@ -27,7 +27,7 @@ ModelingToolkit.toexpr.(eqs)[1] @named de = System(eqs, t; defaults = Dict(x => 1)) subed = substitute(de, [σ => k]) ssort(eqs) = sort(eqs, by = string) -@test isequal(ssort(parameters(subed)), [k, β, ρ]) +@test isequal(ssort(parameters(subed)), [k, β, κ, ρ]) @test isequal(equations(subed), [D(x) ~ k * (y - x) D(y) ~ (ρ - z) * x - y @@ -47,7 +47,7 @@ function test_diffeq_inference(name, sys, iv, dvs, ps) end end -test_diffeq_inference("standard", de, t, [x, y, z], [ρ, σ, β]) +test_diffeq_inference("standard", de, t, [x, y, z], [ρ, σ, β, κ]) jac_expr = generate_jacobian(de) jac = calculate_jacobian(de) jacfun = eval(jac_expr[2]) @@ -138,11 +138,11 @@ tgrad_iip(du, u, p, t) eqs = [D(x) ~ σ(t - 1) * (y - x), D(y) ~ x * (ρ - z) - y, D(z) ~ x * y - β * z * κ] -@named de = System(eqs, t, [x, y, z], [σ, ρ, β]) -test_diffeq_inference("single internal iv-varying", de, t, (x, y, z), (σ, ρ, β)) +@named de = System(eqs, t, [x, y, z], [σ, ρ, β, κ]) +test_diffeq_inference("single internal iv-varying", de, t, (x, y, z), (σ, ρ, β, κ)) f = generate_rhs(de, expression = Val{false}, wrap_gfw = Val{true}) du = [0.0, 0.0, 0.0] -f(du, [1.0, 2.0, 3.0], [x -> x + 7, 2, 3], 5.0) +f(du, [1.0, 2.0, 3.0], [x -> x + 7, 2, 3, 1], 5.0) @test du ≈ [11, -3, -7] eqs = [D(x) ~ x + 10σ(t - 1) + 100σ(t - 2) + 1000σ(t^2)] @@ -1202,7 +1202,8 @@ end prob = ODEProblem(sys, u0, (0.0, 1.0), p) # evaluate - u0_v, p_v, _ = ModelingToolkit.get_u0_p(sys, u0, p) + u0_v = prob.u0 + p_v = prob.p @test prob.f(u0_v, p_v, 0.0) == [c_b, c_a] end diff --git a/test/parameter_dependencies.jl b/test/parameter_dependencies.jl index 624dbe8b6b..5498ecf4d8 100644 --- a/test/parameter_dependencies.jl +++ b/test/parameter_dependencies.jl @@ -296,9 +296,9 @@ end j₁ = ConstantRateJump(rate₁, affect₁) j₃ = ConstantRateJump(rate₃, affect₃) @named js2 = JumpSystem( - [j₃], t, [S, I, R], [γ]; parameter_dependencies = [β => 0.01γ]) - @test isequal(only(parameters(js2)), γ) - @test Set(full_parameters(js2)) == Set([γ, β]) + [j₃], t, [S, I, R], [γ, h]; parameter_dependencies = [β => 0.01γ]) + @test issetequal(parameters(js2), [γ, h]) + @test Set(full_parameters(js2)) == Set([γ, β, h]) js2 = complete(js2) tspan = (0.0, 250.0) u₀map = [S => 999, I => 1, R => 0] @@ -310,7 +310,7 @@ end @test_nowarn solve(jprob, SSAStepper()) @named js2 = JumpSystem( - [j₁, j₃], t, [S, I, R], [γ]; parameter_dependencies = [β => 0.01γ], + [j₁, j₃], t, [S, I, R], [γ, h]; parameter_dependencies = [β => 0.01γ], discrete_events = [SymbolicDiscreteCallback( [10.0] => [γ ~ 0.02], discrete_parameters = [γ])]) js2 = complete(js2) diff --git a/test/structural_transformation/tearing.jl b/test/structural_transformation/tearing.jl index e91f3fa988..f6c4fe5c44 100644 --- a/test/structural_transformation/tearing.jl +++ b/test/structural_transformation/tearing.jl @@ -18,7 +18,7 @@ eqs = [ 0 ~ u4 - hypot(u2, u3), 0 ~ u5 - hypot(u4, u1) ] -@named sys = System(eqs, [u1, u2, u3, u4, u5], []) +@named sys = System(eqs, [u1, u2, u3, u4, u5], [h]) state = TearingState(sys) StructuralTransformations.find_solvables!(state) @@ -149,19 +149,17 @@ eqs = [D(x) ~ z * h 0 ~ sin(z) + y - p * t] @named daesys = System(eqs, t) newdaesys = structural_simplify(daesys) -@test equations(newdaesys) == [D(x) ~ z; 0 ~ y + sin(z) - p * t] -@test equations(tearing_substitution(newdaesys)) == [D(x) ~ z; 0 ~ x + sin(z) - p * t] +@test equations(newdaesys) == [D(x) ~ h * z; 0 ~ y + sin(z) - p * t] +@test equations(tearing_substitution(newdaesys)) == [D(x) ~ h * z; 0 ~ x + sin(z) - p * t] @test isequal(unknowns(newdaesys), [x, z]) -@test isequal(unknowns(newdaesys), [x, z]) -@test_deprecated ODAEProblem(newdaesys, [x => 1.0, z => -0.5π], (0, 1.0), [p => 0.2]) prob = ODEProblem(newdaesys, [x => 1.0, z => -0.5π], (0, 1.0), [p => 0.2]) du = [0.0, 0.0]; u = [1.0, -0.5π]; -pr = 0.2; +pr = prob.p; tt = 0.1; @test (@ballocated $(prob.f)($du, $u, $pr, $tt)) == 0 prob.f(du, u, pr, tt) -@test du≈[u[2], u[1] + sin(u[2]) - pr * tt] atol=1e-5 +@test du≈[u[2], u[1] + sin(u[2]) - prob.ps[p] * tt] atol=1e-5 # test the initial guess is respected @named sys = System(eqs, t, defaults = Dict(z => NaN)) From 21baf12db5e129a6f401252cd566e7c85faf8b69 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 4 Apr 2025 13:58:01 +0530 Subject: [PATCH 1790/2176] fix: don't `toparam` inside `Initial` --- src/systems/abstractsystem.jl | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index 1a90faad92..248768b8d4 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -648,14 +648,14 @@ function (f::Initial)(x) iscall(x) && operation(x) isa Initial && return x result = if symbolic_type(x) == ArraySymbolic() # create an array for `Initial(array)` - Symbolics.array_term(f, toparam(x)) + Symbolics.array_term(f, x) elseif iscall(x) && operation(x) == getindex # instead of `Initial(x[1])` create `Initial(x)[1]` # which allows parameter indexing to handle this case automatically. arr = arguments(x)[1] - term(getindex, f(toparam(arr)), arguments(x)[2:end]...) + term(getindex, f(arr), arguments(x)[2:end]...) else - term(f, toparam(x)) + term(f, x) end # the result should be a parameter result = toparam(result) From 811196b16128b825ee53833d5903e0b4510a57cd Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 4 Apr 2025 13:58:22 +0530 Subject: [PATCH 1791/2176] feat: reduce reliance on metadata in `structural_simplify` --- src/inputoutput.jl | 2 +- src/systems/systemstructure.jl | 226 +++++++++++++++++++++------------ 2 files changed, 143 insertions(+), 85 deletions(-) diff --git a/src/inputoutput.jl b/src/inputoutput.jl index 19603b76cd..97376a2a44 100644 --- a/src/inputoutput.jl +++ b/src/inputoutput.jl @@ -319,7 +319,7 @@ function inputs_to_parameters!(state::TransformationState, inputsyms) @set! sys.ps = [ps; new_parameters] @set! state.sys = sys - @set! state.fullvars = new_fullvars + @set! state.fullvars = Vector{BasicSymbolic}(new_fullvars) @set! state.structure = structure return state end diff --git a/src/systems/systemstructure.jl b/src/systems/systemstructure.jl index c1b2c337ac..e8d81e0820 100644 --- a/src/systems/systemstructure.jl +++ b/src/systems/systemstructure.jl @@ -204,7 +204,7 @@ mutable struct TearingState{T <: AbstractSystem} <: AbstractTearingState{T} """The system of equations.""" sys::T """The set of variables of the system.""" - fullvars::Vector + fullvars::Vector{BasicSymbolic} structure::SystemStructure extra_eqs::Vector param_derivative_map::Dict{BasicSymbolic, Any} @@ -254,128 +254,164 @@ function Base.push!(ev::EquationsView, eq) push!(ev.ts.extra_eqs, eq) end -function is_time_dependent_parameter(p, iv) - return iv !== nothing && isparameter(p) && iscall(p) && - (operation(p) === getindex && is_time_dependent_parameter(arguments(p)[1], iv) || +function is_time_dependent_parameter(p, allps, iv) + return iv !== nothing && p in allps && iscall(p) && + (operation(p) === getindex && + is_time_dependent_parameter(arguments(p)[1], allps, iv) || (args = arguments(p); length(args)) == 1 && isequal(only(args), iv)) end +function symbolic_contains(var, set) + var in set || + symbolic_type(var) == ArraySymbolic() && + Symbolics.shape(var) != Symbolics.Unknown() && + all(x -> x in set, Symbolics.scalarize(var)) +end + function TearingState(sys; quick_cancel = false, check = true, sort_eqs = true) + # flatten system sys = flatten(sys) ivs = independent_variables(sys) iv = length(ivs) == 1 ? ivs[1] : nothing - # scalarize array equations, without scalarizing arguments to registered functions - eqs = flatten_equations(copy(equations(sys))) + # flatten array equations + eqs = flatten_equations(equations(sys)) neqs = length(eqs) - dervaridxs = OrderedSet{Int}() - var2idx = Dict{Any, Int}() - symbolic_incidence = [] - fullvars = [] param_derivative_map = Dict{BasicSymbolic, Any}() - var_counter = Ref(0) + # * Scalarize unknowns + dvs = Set{BasicSymbolic}() + fullvars = BasicSymbolic[] + for x in unknowns(sys) + push!(dvs, x) + xx = Symbolics.scalarize(x) + if xx isa AbstractArray + union!(dvs, xx) + end + end + ps = Set{Symbolic}() + for x in full_parameters(sys) + push!(ps, x) + if symbolic_type(x) == ArraySymbolic() && Symbolics.shape(x) != Symbolics.Unknown() + xx = Symbolics.scalarize(x) + union!(ps, xx) + end + end + browns = Set{BasicSymbolic}() + for x in brownians(sys) + push!(browns, x) + xx = Symbolics.scalarize(x) + if xx isa AbstractArray + union!(browns, xx) + end + end + var2idx = Dict{BasicSymbolic, Int}() var_types = VariableType[] - addvar! = let fullvars = fullvars, var_counter = var_counter, var_types = var_types - var -> get!(var2idx, var) do + addvar! = let fullvars = fullvars, dvs = dvs, var2idx = var2idx, var_types = var_types + (var, vtype) -> get!(var2idx, var) do + push!(dvs, var) push!(fullvars, var) - push!(var_types, getvariabletype(var)) - var_counter[] += 1 + push!(var_types, vtype) + return length(fullvars) end end - vars = OrderedSet() - varsvec = [] + # build symbolic incidence + symbolic_incidence = Vector{BasicSymbolic}[] + varsbuf = Set() eqs_to_retain = trues(length(eqs)) - for (i, eq′) in enumerate(eqs) - if eq′.lhs isa Connection - check ? error("$(nameof(sys)) has unexpanded `connect` statements") : - return nothing - end - if iscall(eq′.lhs) && (op = operation(eq′.lhs)) isa Differential && - isequal(op.x, iv) && is_time_dependent_parameter(only(arguments(eq′.lhs)), iv) + for (i, eq) in enumerate(eqs) + if iscall(eq.lhs) && (op = operation(eq.lhs)) isa Differential && + isequal(op.x, iv) && is_time_dependent_parameter(only(arguments(eq.lhs)), ps, iv) # parameter derivatives are opted out by specifying `D(p) ~ missing`, but # we want to store `nothing` in the map because that means `fast_substitute` # will ignore the rule. We will this identify the presence of `eq′.lhs` in # the differentiated expression and error. - param_derivative_map[eq′.lhs] = coalesce(eq′.rhs, nothing) + param_derivative_map[eq.lhs] = coalesce(eq.rhs, nothing) eqs_to_retain[i] = false # change the equation if the RHS is `missing` so the rest of this loop works - eq′ = eq′.lhs ~ coalesce(eq′.rhs, 0.0) + eq = 0.0 ~ coalesce(eq.rhs, 0.0) end - if _iszero(eq′.lhs) - rhs = quick_cancel ? quick_cancel_expr(eq′.rhs) : eq′.rhs - eq = eq′ - else - lhs = quick_cancel ? quick_cancel_expr(eq′.lhs) : eq′.lhs - rhs = quick_cancel ? quick_cancel_expr(eq′.rhs) : eq′.rhs + rhs = quick_cancel ? quick_cancel_expr(eq.rhs) : eq.rhs + if !_iszero(eq.lhs) + lhs = quick_cancel ? quick_cancel_expr(eq.lhs) : eq.lhs eq = 0 ~ rhs - lhs end - vars!(vars, eq.rhs, op = Symbolics.Operator) - for v in vars - _var, _ = var_from_nested_derivative(v) - any(isequal(_var), ivs) && continue - if isparameter(_var) || - (iscall(_var) && isparameter(operation(_var))) - if is_time_dependent_parameter(_var, iv) && - !haskey(param_derivative_map, Differential(iv)(_var)) + empty!(varsbuf) + vars!(varsbuf, eq; op = Symbolics.Operator) + incidence = Set{BasicSymbolic}() + isalgeq = true + for v in varsbuf + # additionally track brownians in fullvars + if v in browns + addvar!(v, BROWNIAN) + push!(incidence, v) + end + + # TODO: Can we handle this without `isparameter`? + if symbolic_contains(v, ps) || + getmetadata(v, SymScope, LocalScope()) isa GlobalScope && isparameter(v) + if is_time_dependent_parameter(v, ps, iv) && + !haskey(param_derivative_map, Differential(iv)(v)) # Parameter derivatives default to zero - they stay constant # between callbacks - param_derivative_map[Differential(iv)(_var)] = 0.0 + param_derivative_map[Differential(iv)(v)] = 0.0 end continue end - v = scalarize(v) - if v isa AbstractArray - append!(varsvec, v) - else - push!(varsvec, v) - end - end - isalgeq = true - unknownvars = [] - for var in varsvec - ModelingToolkit.isdelay(var, iv) && continue - set_incidence = true - @label ANOTHER_VAR - _var, _ = var_from_nested_derivative(var) - any(isequal(_var), ivs) && continue - if isparameter(_var) || - (iscall(_var) && isparameter(operation(_var))) - continue - end - varidx = addvar!(var) - set_incidence && push!(unknownvars, var) - - dvar = var - idx = varidx - while isdifferential(dvar) - if !(idx in dervaridxs) - push!(dervaridxs, idx) + + isequal(v, iv) && continue + isdelay(v, iv) && continue + + if !symbolic_contains(v, dvs) + isvalid = iscall(v) && operation(v) isa Union{Shift, Sample, Hold} + v′ = v + while !isvalid && iscall(v′) && operation(v′) isa Union{Differential, Shift} + v′ = arguments(v′)[1] + if v′ in dvs || getmetadata(v′, SymScope, LocalScope()) isa GlobalScope + isvalid = true + break + end + end + if !isvalid + throw(ArgumentError("$v is present in the system but $v′ is not an unknown.")) + end + + addvar!(v, VARIABLE) + if iscall(v) && operation(v) isa Symbolics.Operator && !isdifferential(v) && + (it = input_timedomain(v)) !== nothing + v′ = only(arguments(v)) + addvar!(setmetadata(v′, VariableTimeDomain, it), VARIABLE) end - isalgeq = false - dvar = arguments(dvar)[1] - idx = addvar!(dvar) end - dvar = var - idx = varidx + isalgeq &= !isdifferential(v) - if iscall(var) && operation(var) isa Symbolics.Operator && - !isdifferential(var) && (it = input_timedomain(var)) !== nothing - set_incidence = false - var = only(arguments(var)) - var = setmetadata(var, VariableTimeDomain, it) - @goto ANOTHER_VAR + if symbolic_type(v) == ArraySymbolic() + vv = collect(v) + union!(incidence, vv) + map(vv) do vi + addvar!(vi, VARIABLE) + end + else + push!(incidence, v) + addvar!(v, VARIABLE) end end - push!(symbolic_incidence, copy(unknownvars)) - empty!(unknownvars) - empty!(vars) - empty!(varsvec) + if isalgeq eqs[i] = eq else eqs[i] = eqs[i].lhs ~ rhs end + push!(symbolic_incidence, collect(incidence)) + end + + dervaridxs = OrderedSet{Int}() + for (i, v) in enumerate(fullvars) + while isdifferential(v) + push!(dervaridxs, i) + v = arguments(v)[1] + i = addvar!(v, VARIABLE) + end end eqs = eqs[eqs_to_retain] neqs = length(eqs) @@ -389,6 +425,7 @@ function TearingState(sys; quick_cancel = false, check = true, sort_eqs = true) symbolic_incidence = symbolic_incidence[sortidxs] end + # Handle shifts - find lowest shift and add intermediates with derivative edges ### Handle discrete variables lowest_shift = Dict() for var in fullvars @@ -422,12 +459,13 @@ function TearingState(sys; quick_cancel = false, check = true, sort_eqs = true) for s in (steps - 1):-1:(lshift + 1) sf = Shift(tt, s) dvar = sf(v) - idx = addvar!(dvar) + idx = addvar!(dvar, VARIABLE) if !(idx in dervaridxs) push!(dervaridxs, idx) end end end + # sort `fullvars` such that the mass matrix is as diagonal as possible. dervaridxs = collect(dervaridxs) sorted_fullvars = OrderedSet(fullvars[dervaridxs]) @@ -451,6 +489,7 @@ function TearingState(sys; quick_cancel = false, check = true, sort_eqs = true) var2idx = Dict(fullvars .=> eachindex(fullvars)) dervaridxs = 1:length(dervaridxs) + # build `var_to_diff` nvars = length(fullvars) diffvars = [] var_to_diff = DiffGraph(nvars, true) @@ -462,6 +501,7 @@ function TearingState(sys; quick_cancel = false, check = true, sort_eqs = true) var_to_diff[diffvaridx] = dervaridx end + # build incidence graph graph = BipartiteGraph(neqs, nvars, Val(false)) for (ie, vars) in enumerate(symbolic_incidence), v in vars jv = var2idx[v] @@ -731,3 +771,21 @@ function _structural_simplify!(state::TearingState; simplify = false, ModelingToolkit.invalidate_cache!(sys) end + +struct DifferentiatedVariableNotUnknownError <: Exception + differentiated::Any + undifferentiated::Any +end + +function Base.showerror(io::IO, err::DifferentiatedVariableNotUnknownError) + undiff = err.undifferentiated + diff = err.differentiated + print(io, + "Variable $undiff occurs differentiated as $diff but is not an unknown of the system.") + scope = getmetadata(undiff, SymScope, LocalScope()) + depth = expected_scope_depth(scope) + if depth > 0 + print(io, + "\nVariable $undiff expects $depth more levels in the hierarchy to be an unknown.") + end +end From 3c09dea926acd6f6fe0447be92d23f09fa6b7e6c Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 19 May 2025 18:02:43 +0530 Subject: [PATCH 1792/2176] fix: fix `linearization_function` with analysis points mutating the system --- src/systems/analysis_points.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/systems/analysis_points.jl b/src/systems/analysis_points.jl index 27a0204cb8..2ce6356aa7 100644 --- a/src/systems/analysis_points.jl +++ b/src/systems/analysis_points.jl @@ -950,7 +950,7 @@ function linearization_function(sys::AbstractSystem, if output isa AnalysisPoint sys, (output_var,) = apply_transformation(AddVariable(output), sys) sys, (input_var,) = apply_transformation(GetInput(output), sys) - push!(get_eqs(sys), output_var ~ input_var) + @set! sys.eqs = [get_eqs(sys); output_var ~ input_var] else output_var = output end From 348287f439182d7cf74577b899fec70e80b3bd07 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 19 May 2025 18:03:05 +0530 Subject: [PATCH 1793/2176] fix: properly handle array equations during variable discovery in `System` --- src/systems/system.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/systems/system.jl b/src/systems/system.jl index 940ca39350..c84187feaa 100644 --- a/src/systems/system.jl +++ b/src/systems/system.jl @@ -206,7 +206,7 @@ function System(eqs::Vector{Equation}, iv; kwargs...) diffeqs = Equation[] othereqs = Equation[] for eq in eqs - if !(eq.lhs isa Union{Symbolic, Number}) + if !(eq.lhs isa Union{Symbolic, Number, AbstractArray}) push!(othereqs, eq) continue end From 6d994205699a257f7627bb4f71e7d67f77ea3f90 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 19 May 2025 18:03:42 +0530 Subject: [PATCH 1794/2176] test: fix broken tearing test --- test/structural_transformation/tearing.jl | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/test/structural_transformation/tearing.jl b/test/structural_transformation/tearing.jl index f6c4fe5c44..5bac18a253 100644 --- a/test/structural_transformation/tearing.jl +++ b/test/structural_transformation/tearing.jl @@ -58,11 +58,9 @@ graph2vars(graph) = map(is -> Set(map(i -> int2var[i], is)), graph.fadjlist) Set([u4]) Set([u5])] -state = TearingState(tearing(sys)) -let sss = state.structure - @unpack graph = sss - @test graph2vars(graph) == [Set([u1, u2, u5])] -end +newsys = tearing(sys) +@test length(equations(newsys)) == 1 +@test issetequal(ModelingToolkit.vars(equations(newsys)), [u1, u4, u5]) # Before: # u1 u2 u3 u4 u5 From 9fc141a414c5ab841014fb4143c09e26b0b69ddf Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 19 May 2025 18:04:12 +0530 Subject: [PATCH 1795/2176] test: fix missing subcomponent in components test --- test/components.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/components.jl b/test/components.jl index 6102762d01..5160aad6da 100644 --- a/test/components.jl +++ b/test/components.jl @@ -197,7 +197,7 @@ end connect(resistor.heat_port, heat_capacitor.port)] compose(System(rc_eqs, t, name = Symbol(name, i)), - [resistor, capacitor, source, ground, heat_capacitor]) + [resistor, capacitor, source, ground, shape, heat_capacitor]) end V = 2.0 @named shape = Constant(k = V) From 59e5ad6e759173ef45bb3867caa72192910ed477 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 19 May 2025 18:04:19 +0530 Subject: [PATCH 1796/2176] fix: fix incorrect default in DDE test --- test/dde.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/dde.jl b/test/dde.jl index a5a0f6ee0d..cac207caa3 100644 --- a/test/dde.jl +++ b/test/dde.jl @@ -98,7 +98,7 @@ prob_sa = SDDEProblem( function oscillator(; name, k = 1.0, τ = 0.01) @parameters k=k τ=τ - @variables x(..)=0.1 y(t)=0.1 jcn(t)=0.0 delx(t) + @variables x(..)=0.1 y(t)=0.1 jcn(t) delx(t) eqs = [D(x(t)) ~ y, D(y) ~ -k * x(t - τ) + jcn, delx ~ x(t - τ)] From 678d98f48e5fea98ffcf06365cc3ec9bee21e3d3 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 19 May 2025 18:04:37 +0530 Subject: [PATCH 1797/2176] fix: fix parameter not passed to `System` in units test --- test/dq_units.jl | 2 +- test/units.jl | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/test/dq_units.jl b/test/dq_units.jl index f0dc2dbe23..5a635144f5 100644 --- a/test/dq_units.jl +++ b/test/dq_units.jl @@ -129,7 +129,7 @@ sys_simple = structural_simplify(sys) eqs = [L ~ v * t, V ~ L^3] -@named sys = System(eqs, [V, L], [t, r]) +@named sys = System(eqs, [V, L], [t, r, v]) sys_simple = structural_simplify(sys) #Jump System diff --git a/test/units.jl b/test/units.jl index b7d141f347..f15145ffa9 100644 --- a/test/units.jl +++ b/test/units.jl @@ -156,7 +156,7 @@ sys_simple = structural_simplify(sys) eqs = [L ~ v * t, V ~ L^3] -@named sys = System(eqs, [V, L], [t, r]) +@named sys = System(eqs, [V, L], [v, t, r]) sys_simple = structural_simplify(sys) #Jump System From bd15dfd2563e94c2f2996769c4429c9d3bdd8c1c Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 19 May 2025 18:04:52 +0530 Subject: [PATCH 1798/2176] fix: fix parameter not passed to system in error handling test --- test/error_handling.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/error_handling.jl b/test/error_handling.jl index d6c0fa8caa..6b416d9a6e 100644 --- a/test/error_handling.jl +++ b/test/error_handling.jl @@ -27,7 +27,7 @@ function OverdefinedConstantVoltage(; name, V = 1.0, I = 1.0) # Overdefine p.i and n.i n.i ~ I p.i ~ I] - System(eqs, t, [], [V], systems = [p, n], defaults = Dict(V => val, I => val2), + System(eqs, t, [], [V, I], systems = [p, n], defaults = Dict(V => val, I => val2), name = name) end From 9bd3a06dfd26b26649c62e5d76e4741e5df7ebc4 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 19 May 2025 18:05:05 +0530 Subject: [PATCH 1799/2176] test: update test with new simplification result --- test/nonlinearsystem.jl | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/nonlinearsystem.jl b/test/nonlinearsystem.jl index 313866d1d8..375c8ba503 100644 --- a/test/nonlinearsystem.jl +++ b/test/nonlinearsystem.jl @@ -296,9 +296,9 @@ sys = structural_simplify(ns; conservative = true) # system that contains a chain of observed variables when simplified @variables x y z eqs = [0 ~ x^2 + 2z + y, z ~ y, y ~ x] # analytical solution x = y = z = 0 or -3 - @mtkbuild ns = System(eqs) # solve for y with observed chain z -> x -> y - @test isequal(expand.(calculate_jacobian(ns)), [3 // 2 + y;;]) - @test isequal(calculate_hessian(ns), [[1;;]]) + @mtkbuild ns = System(eqs) # solve for y with observed chain z -> y -> x + @test isequal(expand.(calculate_jacobian(ns)), [-3 // 2 - x;;]) + @test isequal(calculate_hessian(ns), [[-1;;]]) prob = NonlinearProblem(ns, unknowns(ns) .=> -4.0) # give guess < -3 to reach -3 sol = solve(prob, NewtonRaphson()) @test sol[x] ≈ sol[y] ≈ sol[z] ≈ -3 From abfb41b7709062a2c8ac5857f87bc7ecae0db130 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 19 May 2025 18:05:18 +0530 Subject: [PATCH 1800/2176] test: fix variable not passed to `System` in odesystem test --- test/odesystem.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/odesystem.jl b/test/odesystem.jl index 33267e8c78..57948c517d 100644 --- a/test/odesystem.jl +++ b/test/odesystem.jl @@ -1390,7 +1390,7 @@ end @parameters c(t) @mtkbuild sys = System([D(x) ~ c * cos(x), obs ~ c], t, - [x], + [x, obs], [c]; discrete_events = [SymbolicDiscreteCallback( 1.0 => [c ~ Pre(c) + 1], discrete_parameters = [c])]) From 0dedfe9849bbbdb3da955170a938ac4f12d26cb7 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 20 May 2025 12:14:38 +0530 Subject: [PATCH 1801/2176] refactor: don't check `isparameter` in `check_variables` and `check_parameters` --- src/utils.jl | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/utils.jl b/src/utils.jl index e1e8c50af4..13ebb28b6a 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -124,8 +124,6 @@ function check_parameters(ps, iv) for p in ps isequal(iv, p) && throw(ArgumentError("Independent variable $iv not allowed in parameters.")) - isparameter(p) || - throw(ArgumentError("$p is not a parameter.")) end end @@ -153,8 +151,6 @@ function check_variables(dvs, iv) throw(ArgumentError("Independent variable $iv not allowed in dependent variables.")) (is_delay_var(iv, dv) || occursin(iv, dv)) || throw(ArgumentError("Variable $dv is not a function of independent variable $iv.")) - isparameter(dv) && - throw(ArgumentError("$dv is not an unknown. It is a parameter.")) end end From 501dc7faabb2a691c82e2caaa4eacbec5e653077 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 20 May 2025 12:15:05 +0530 Subject: [PATCH 1802/2176] fix: don't use `isparameter` in `find_eq_solvables!` --- .../StructuralTransformations.jl | 1 + src/structural_transformation/utils.jl | 12 +++++++++--- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/structural_transformation/StructuralTransformations.jl b/src/structural_transformation/StructuralTransformations.jl index 2ba469e26a..15f1f7d2db 100644 --- a/src/structural_transformation/StructuralTransformations.jl +++ b/src/structural_transformation/StructuralTransformations.jl @@ -40,6 +40,7 @@ using ModelingToolkit: algeqs, EquationsView, dervars_range, diffvars_range, algvars_range, DiffGraph, complete!, get_fullvars, system_subset +using SymbolicIndexingInterface: symbolic_type, ArraySymbolic using ModelingToolkit.DiffEqBase using ModelingToolkit.StaticArrays diff --git a/src/structural_transformation/utils.jl b/src/structural_transformation/utils.jl index 2bf316cfa6..e1c8a74eca 100644 --- a/src/structural_transformation/utils.jl +++ b/src/structural_transformation/utils.jl @@ -228,9 +228,15 @@ function find_eq_solvables!(state::TearingState, ieq, to_rm = Int[], coeffs = no all_int_vars = false if !allow_symbolic if allow_parameter - all( - x -> ModelingToolkit.isparameter(x), - vars(a)) || continue + # if any of the variables in `a` are present in fullvars (taking into account arrays) + if any( + v -> any(isequal(v), fullvars) || + symbolic_type(v) == ArraySymbolic() && + Symbolics.shape(v) != Symbolics.Unknown() && + any(x -> any(isequal(x), fullvars), collect(v)), + vars(a)) + continue + end else continue end From 21d9cb896f159b41d18456fe79ea7c38289652fb Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 20 May 2025 12:26:52 +0530 Subject: [PATCH 1803/2176] fix: don't use `isparameter` in `generate_initializesystem` --- src/systems/nonlinear/initializesystem.jl | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/systems/nonlinear/initializesystem.jl b/src/systems/nonlinear/initializesystem.jl index a550f9d740..a364caaa18 100644 --- a/src/systems/nonlinear/initializesystem.jl +++ b/src/systems/nonlinear/initializesystem.jl @@ -214,7 +214,15 @@ function generate_initializesystem_timeindependent(sys::AbstractSystem; initialization_eqs = filter(initialization_eqs) do eq empty!(vs) vars!(vs, eq; op = Initial) - non_params = filter(!isparameter, vs) + allpars = full_parameters(sys) + for p in allpars + if symbolic_type(p) == ArraySymbolic() && + Symbolics.shape(p) != Symbolics.Unknown() + append!(allpars, Symbolics.scalarize(p)) + end + end + allpars = Set(allpars) + non_params = filter(!in(allpars), vs) # error if non-parameters are present in the initialization equations if !isempty(non_params) throw(UnknownsInTimeIndependentInitializationError(eq, non_params)) From 77a3f510d03dd840f2d80705b64f364a21dc47df Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 20 May 2025 12:27:08 +0530 Subject: [PATCH 1804/2176] test: test simplification independent of metadata --- test/structural_transformation/utils.jl | 50 +++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/test/structural_transformation/utils.jl b/test/structural_transformation/utils.jl index 7611ab5d33..b228cdbbf7 100644 --- a/test/structural_transformation/utils.jl +++ b/test/structural_transformation/utils.jl @@ -6,6 +6,7 @@ using UnPack using ModelingToolkit: t_nounits as t, D_nounits as D, default_toterm using Symbolics: unwrap using DataInterpolations +using OrdinaryDiffEq, NonlinearSolve, StochasticDiffEq const ST = StructuralTransformations # Define some variables @@ -386,3 +387,52 @@ end @test D(sys.k(t)) in vs end end + +@testset "Don't rely on metadata" begin + @testset "ODESystem" begin + @variables x(t) p + @parameters y(t) q + @mtkbuild sys = System([D(x) ~ x * q, x^2 + y^2 ~ p], t, [x, y], + [p, q]; initialization_eqs = [p + q ~ 3], + defaults = [p => missing], guesses = [p => 1.0, y => 1.0]) + @test length(equations(sys)) == 2 + @test length(parameters(sys)) == 2 + prob = ODEProblem(sys, [x => 1.0], (0.0, 1.0), [q => 2.0]) + integ = init(prob, Rodas5P(); abstol = 1e-10, reltol = 1e-8) + @test integ.ps[p]≈1.0 atol=1e-6 + @test integ[y]≈0.0 atol=1e-5 + end + + @testset "NonlinearSystem" begin + @variables x p + @parameters y q + @mtkbuild sys = System([0 ~ p * x + y, x^3 + y^3 ~ q], [x, y], + [p, q]; initialization_eqs = [p ~ q + 1], + guesses = [p => 1.0], defaults = [p => missing]) + @test length(equations(sys)) == length(unknowns(sys)) == 1 + @test length(observed(sys)) == 1 + @test observed(sys)[1].lhs in Set([x, y]) + @test length(parameters(sys)) == 2 + prob = NonlinearProblem(sys, [x => 1.0, y => 1.0], [q => 1.0]) + integ = init(prob, NewtonRaphson()) + @test prob.ps[p] ≈ 2.0 + end + + @testset "SDESystem" begin + @variables x(t) p a + @parameters y(t) q b + @brownian c + @mtkbuild sys = System([D(x) ~ x + q * a, D(y) ~ y + p * b + c], t, [x, y], + [p, q], [a, b, c]; initialization_eqs = [p + q ~ 4], + guesses = [p => 1.0], defaults = [p => missing]) + @test length(equations(sys)) == 2 + @test issetequal(unknowns(sys), [x, y]) + @test issetequal(parameters(sys), [p, q]) + @test isempty(brownians(sys)) + neqs = ModelingToolkit.get_noise_eqs(sys) + @test issetequal(sum.(eachrow(neqs)), [q, 1 + p]) + prob = SDEProblem(sys, [x => 1.0, y => 1.0], (0.0, 1.0), [q => 1.0]) + integ = init(prob, ImplicitEM()) + @test integ.ps[p] ≈ 3.0 + end +end From 6aa6039e19bdcd461261382212dcbc5c500ca62a Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 21 May 2025 14:05:45 +0530 Subject: [PATCH 1805/2176] fix: unwrap brownians in `noise_to_brownians` --- src/systems/diffeqs/basic_transformations.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/systems/diffeqs/basic_transformations.jl b/src/systems/diffeqs/basic_transformations.jl index 4bf3b40177..6046e5d4be 100644 --- a/src/systems/diffeqs/basic_transformations.jl +++ b/src/systems/diffeqs/basic_transformations.jl @@ -482,7 +482,7 @@ function noise_to_brownians(sys::System; names::Union{Symbol, Vector{Symbol}} = """)) end brownvars = map(names) do name - only(@brownian $name) + unwrap(only(@brownian $name)) end terms = if ndims(neqs) == 1 From 0723b71563d9ed7fb9f17ca318351c37dc451a0a Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 21 May 2025 14:06:28 +0530 Subject: [PATCH 1806/2176] test: pass `@constants` parameter to `System` constructor --- test/nonlinearsystem.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/nonlinearsystem.jl b/test/nonlinearsystem.jl index 375c8ba503..72de46bad0 100644 --- a/test/nonlinearsystem.jl +++ b/test/nonlinearsystem.jl @@ -108,7 +108,7 @@ lorenz2 = lorenz(:lorenz2) lorenz2.y ~ s * h lorenz1.F ~ lorenz2.u lorenz2.F ~ lorenz1.u], - [s, a], [], + [s, a], [h], systems = [lorenz1, lorenz2]) @test_nowarn alias_elimination(connected) From 92e3686eb8081a65c419aa420bdf77e932d9547b Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 21 May 2025 14:06:40 +0530 Subject: [PATCH 1807/2176] test: pass brownians to `System` constructor --- test/sdesystem.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/sdesystem.jl b/test/sdesystem.jl index 5bdc033498..a3e0ff00d0 100644 --- a/test/sdesystem.jl +++ b/test/sdesystem.jl @@ -792,12 +792,12 @@ end [input = true] end ps = @parameters a = 2 - @brownian η + browns = @brownian η eqs = [D(x) ~ -a * x + (input + 1) * η input ~ 0.0] - sys = System(eqs, t, sts, ps; name = :name) + sys = System(eqs, t, sts, ps, browns; name = :name) sys = structural_simplify(sys) @test ModelingToolkit.get_noise_eqs(sys) ≈ [1.0] prob = SDEProblem(sys, [], (0.0, 1.0), []) From 7bceb7ac70509958bb14abf55b7ccb1ec1bbc698 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 21 May 2025 19:00:31 +0530 Subject: [PATCH 1808/2176] fix: unhack observed when compiling callbacks --- src/systems/callbacks.jl | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/systems/callbacks.jl b/src/systems/callbacks.jl index 59ff0e0d98..412c4e9bc7 100644 --- a/src/systems/callbacks.jl +++ b/src/systems/callbacks.jl @@ -902,9 +902,10 @@ function compile_equational_affect( aff_map = aff_to_sys(aff) sys_map = Dict([v => k for (k, v) in aff_map]) + obseqs, eqs = unhack_observed(observed(affsys), equations(affsys)) if isempty(equations(affsys)) update_eqs = Symbolics.fast_substitute( - observed(affsys), Dict([p => unPre(p) for p in parameters(affsys)])) + obseqs, Dict([p => unPre(p) for p in parameters(affsys)])) rhss = map(x -> x.rhs, update_eqs) lhss = map(x -> aff_map[x.lhs], update_eqs) is_p = [lhs ∈ Set(ps_to_update) for lhs in lhss] From d1bffda71626dbe8abd7602efe528da62936bd80 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 22 May 2025 13:48:50 +0530 Subject: [PATCH 1809/2176] refactor: disable using `getproperty` to access system fields --- src/systems/abstractsystem.jl | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index 248768b8d4..90aa14dc28 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -1019,12 +1019,7 @@ function Base.getproperty( end function getvar(sys::AbstractSystem, name::Symbol; namespace = does_namespacing(sys)) systems = get_systems(sys) - if isdefined(sys, name) - Base.depwarn( - "`sys.name` like `sys.$name` is deprecated. Use getters like `get_$name` instead.", - "sys.$name") - return getfield(sys, name) - elseif !isempty(systems) + if !isempty(systems) i = findfirst(x -> nameof(x) == name, systems) if i !== nothing return namespace ? renamespace(sys, systems[i]) : systems[i] From 084fbbf9bb527d507800168974e9d81daa72de3e Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 22 May 2025 13:49:15 +0530 Subject: [PATCH 1810/2176] refactor: disable `setproperty!` on systems --- src/systems/abstractsystem.jl | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index 90aa14dc28..d301f2ae23 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -1074,19 +1074,14 @@ function getvar(sys::AbstractSystem, name::Symbol; namespace = does_namespacing( end function Base.setproperty!(sys::AbstractSystem, prop::Symbol, val) - # We use this weird syntax because `parameters` and `unknowns` calls are - # potentially expensive. - if (params = parameters(sys); - idx = findfirst(s -> getname(s) == prop, params); - idx !== nothing) - get_defaults(sys)[params[idx]] = value(val) - elseif (sts = unknowns(sys); - idx = findfirst(s -> getname(s) == prop, sts); - idx !== nothing) - get_defaults(sys)[sts[idx]] = value(val) - else - setfield!(sys, prop, val) - end + error(""" + `setproperty!` on systems is invalid. Systems are immutable data structures, and \ + modifications to fields should be made by constructing a new system. This can be done \ + easily using packages such as Setfield.jl. + + If you are looking for the old behavior of updating the default of a variable via \ + `setproperty!`, this should now be done by mutating `ModelingToolkit.get_defaults(sys)`. + """) end apply_to_variables(f::F, ex) where {F} = _apply_to_variables(f, ex) From d9ee7c3d9a7f630a8518ff714fdd6bbf8a5cd2d5 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 22 May 2025 00:49:05 +0530 Subject: [PATCH 1811/2176] refactor: change `metadata` field to be like `BasicSymbolic` --- src/systems/abstractsystem.jl | 2 +- src/systems/connectors.jl | 14 +++++++------- src/systems/system.jl | 33 +++++++++++++++++++++++++++++---- 3 files changed, 37 insertions(+), 12 deletions(-) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index 248768b8d4..6ede839e4a 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -2665,7 +2665,7 @@ function extend(sys::AbstractSystem, basesys::AbstractSystem; cevs = union(get_continuous_events(basesys), get_continuous_events(sys)) devs = union(get_discrete_events(basesys), get_discrete_events(sys)) defs = merge(get_defaults(basesys), get_defaults(sys)) # prefer `sys` - meta = union_nothing(get_metadata(basesys), get_metadata(sys)) + meta = merge(get_metadata(basesys), get_metadata(sys)) syss = union(get_systems(basesys), get_systems(sys)) args = length(ivs) == 0 ? (eqs, sts, ps) : (eqs, ivs[1], sts, ps) kwargs = (parameter_dependencies = dep_ps, observed = obs, continuous_events = cevs, diff --git a/src/systems/connectors.jl b/src/systems/connectors.jl index 6b0600fbb7..ba41b2b011 100644 --- a/src/systems/connectors.jl +++ b/src/systems/connectors.jl @@ -298,19 +298,19 @@ end error("Different types of connectors are in one connection statement: <$(map(nameof, ss))>") end +abstract type IsFrame end + "Return true if the system is a 3D multibody frame, otherwise return false." function isframe(sys) - (has_metadata(sys) && (md = get_metadata(sys)) isa Dict) || return false - get(md, :frame, false) + getmetadata(sys, IsFrame, false) end +abstract type FrameOrientation end + "Return orientation object of a multibody frame." function ori(sys) - @assert has_metadata(sys) - md = get_metadata(sys) - if md isa Dict && (O = get(md, :orientation, nothing)) !== nothing - return O - else + val = getmetadata(sys, FrameOrientation, nothing) + if val === nothing error("System $(sys.name) does not have an orientation object.") end end diff --git a/src/systems/system.jl b/src/systems/system.jl index c84187feaa..1587f85b75 100644 --- a/src/systems/system.jl +++ b/src/systems/system.jl @@ -9,6 +9,8 @@ struct Schedule{V <: BipartiteGraphs.Matching} dummy_sub::Dict{Any, Any} end +const MetadataT = Base.ImmutableDict{DataType, Any} + struct System <: AbstractSystem tag::UInt eqs::Vector{Equation} @@ -38,7 +40,7 @@ struct System <: AbstractSystem discrete_events::Vector{SymbolicDiscreteCallback} connector_type::Any assertions::Dict{BasicSymbolic, String} - metadata::Any + metadata::MetadataT gui_metadata::Any # ? is_dde::Bool tstops::Vector{Any} @@ -60,7 +62,7 @@ struct System <: AbstractSystem brownians, iv, observed, parameter_dependencies, var_to_name, name, description, defaults, guesses, systems, initialization_eqs, continuous_events, discrete_events, connector_type, assertions = Dict{BasicSymbolic, String}(), - metadata = nothing, gui_metadata = nothing, + metadata = MetadataT(), gui_metadata = nothing, is_dde = false, tstops = [], tearing_state = nothing, namespacing = true, complete = false, index_cache = nothing, ignored_connections = nothing, preface = nothing, parent = nothing, initializesystem = nothing, @@ -119,8 +121,9 @@ function System(eqs::Vector{Equation}, iv, dvs, ps, brownians = []; guesses = Dict(), systems = System[], initialization_eqs = Equation[], continuous_events = SymbolicContinuousCallback[], discrete_events = SymbolicDiscreteCallback[], connector_type = nothing, assertions = Dict{BasicSymbolic, String}(), - metadata = nothing, gui_metadata = nothing, is_dde = nothing, tstops = [], - tearing_state = nothing, ignored_connections = nothing, parent = nothing, + metadata = MetadataT(), gui_metadata = nothing, + is_dde = nothing, tstops = [], tearing_state = nothing, + ignored_connections = nothing, parent = nothing, description = "", name = nothing, discover_from_metadata = true, initializesystem = nothing, is_initializesystem = false, preface = [], checks = true) @@ -185,6 +188,17 @@ function System(eqs::Vector{Equation}, iv, dvs, ps, brownians = []; assertions = Dict{BasicSymbolic, String}(unwrap(k) => v for (k, v) in assertions) + if isempty(metadata) + metadata = MetadataT() + elseif metadata isa MetadataT + metadata = metadata + else + meta = MetadataT() + for kvp in metadata + meta = Base.ImmutableDict(meta, kvp) + end + metadata = meta + end System(Threads.atomic_add!(SYSTEM_COUNT, UInt(1)), eqs, noise_eqs, jumps, constraints, costs, consolidate, dvs, ps, brownians, iv, observed, parameter_dependencies, var_to_name, name, description, defaults, guesses, systems, initialization_eqs, @@ -621,6 +635,17 @@ function Base.hash(sys::System, h::UInt) return h end +function SymbolicUtils.getmetadata(sys::AbstractSystem, k::DataType, default) + meta = get_metadata(sys) + return get(meta, k, default) +end + +function SymbolicUtils.setmetadata(sys::AbstractSystem, k::DataType, v) + meta = get_metadata(sys) + meta = Base.ImmutableDict(meta, k => v)::MetadataT + @set sys.metadata = meta +end + """ $(TYPEDSIGNATURES) """ From 36255235d8f9c094a85180453c2d56c585b91523 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 22 May 2025 00:49:23 +0530 Subject: [PATCH 1812/2176] test: update tests to account for new `metadata` --- test/components.jl | 7 ++++--- test/discrete_system.jl | 6 ------ test/nonlinearsystem.jl | 9 --------- test/odesystem.jl | 29 +++++++++++++---------------- test/optimizationsystem.jl | 13 ------------- 5 files changed, 17 insertions(+), 47 deletions(-) diff --git a/test/components.jl b/test/components.jl index 5160aad6da..19b601a0be 100644 --- a/test/components.jl +++ b/test/components.jl @@ -8,6 +8,7 @@ using ModelingToolkitStandardLibrary.Electrical using ModelingToolkitStandardLibrary.Blocks using LinearAlgebra using ModelingToolkitStandardLibrary.Thermal +using SymbolicUtils: getmetadata include("common/rc_model.jl") @testset "Basics" begin @@ -328,8 +329,8 @@ end @testset "Issue#3275: Metadata retained on `complete`" begin @variables x(t) y(t) @named inner = System(D(x) ~ x, t) - @named outer = System(D(y) ~ y, t; systems = [inner], metadata = "test") - @test ModelingToolkit.get_metadata(outer) == "test" + @named outer = System(D(y) ~ y, t; systems = [inner], metadata = [Int => "test"]) + @test getmetadata(outer, Int, nothing) == "test" sys = complete(outer) - @test ModelingToolkit.get_metadata(sys) == "test" + @test getmetadata(sys, Int, nothing) == "test" end diff --git a/test/discrete_system.jl b/test/discrete_system.jl index f3c5bff496..2d2682b07a 100644 --- a/test/discrete_system.jl +++ b/test/discrete_system.jl @@ -5,7 +5,6 @@ =# using ModelingToolkit, SymbolicIndexingInterface, Test using ModelingToolkit: t_nounits as t -using ModelingToolkit: get_metadata, MTKParameters # Make sure positive shifts error @variables x(t) @@ -205,11 +204,6 @@ RHS2 = RHS # @test c[1] + 1 == length(sol) # end -@variables x(t) y(t) -testdict = Dict([:test => 1]) -@named sys = System([x(k + 1) ~ 1.0], t, [x], []; metadata = testdict) -@test get_metadata(sys) == testdict - @variables x(t) y(t) u(t) eqs = [u ~ 1 x ~ x(k - 1) + u diff --git a/test/nonlinearsystem.jl b/test/nonlinearsystem.jl index 72de46bad0..d7c8c15f6c 100644 --- a/test/nonlinearsystem.jl +++ b/test/nonlinearsystem.jl @@ -1,5 +1,4 @@ using ModelingToolkit, StaticArrays, LinearAlgebra -using ModelingToolkit: get_metadata using DiffEqBase, SparseArrays using Test using NonlinearSolve @@ -198,14 +197,6 @@ eq = [v1 ~ sin(2pi * t * h) @named sys = System(eq, t) @test length(equations(structural_simplify(sys))) == 0 -@variables x(t) -@parameters a -eqs = [0 ~ a * x] - -testdict = Dict([:test => 1]) -@named sys = System(eqs, [x], [a], metadata = testdict) -@test get_metadata(sys) == testdict - @testset "Remake" begin @parameters a=1.0 b=1.0 c=1.0 @constants h = 1 diff --git a/test/odesystem.jl b/test/odesystem.jl index 57948c517d..ec3e52fe3a 100644 --- a/test/odesystem.jl +++ b/test/odesystem.jl @@ -799,16 +799,6 @@ let @test string.(independent_variables(prob.f.sys)) == ["t"] end -@parameters C L R -@variables q(t) p(t) F(t) - -eqs = [D(q) ~ -p / L - F - D(p) ~ q / C - 0 ~ q / C - R * F] -testdict = Dict([:name => "test"]) -@named sys = System(eqs, t, metadata = testdict) -@test get_metadata(sys) == testdict - @variables P(t)=NaN Q(t)=NaN eqs = [D(Q) ~ 1 / sin(P), D(P) ~ log(-cos(Q))] @named sys = System(eqs, t, [P, Q], []) @@ -1112,16 +1102,23 @@ end # https://github.com/SciML/ModelingToolkit.jl/issues/2502 @testset "Extend systems with a field that can be nothing" begin - A = Dict(:a => 1) - B = Dict(:b => 2) + A = Dict(Int => 1) + B = Dict(String => 2) @named A1 = System(Equation[], t, [], []) @named B1 = System(Equation[], t, [], []) @named A2 = System(Equation[], t, [], []; metadata = A) @named B2 = System(Equation[], t, [], []; metadata = B) - @test ModelingToolkit.get_metadata(extend(A1, B1)) == nothing - @test ModelingToolkit.get_metadata(extend(A1, B2)) == B - @test ModelingToolkit.get_metadata(extend(A2, B1)) == A - @test Set(ModelingToolkit.get_metadata(extend(A2, B2))) == Set(A ∪ B) + @test isempty(ModelingToolkit.get_metadata(extend(A1, B1))) + meta = ModelingToolkit.get_metadata(extend(A1, B2)) + @test length(meta) == 1 + @test meta[String] == 2 + meta = ModelingToolkit.get_metadata(extend(A2, B1)) + @test length(meta) == 1 + @test meta[Int] == 1 + meta = ModelingToolkit.get_metadata(extend(A2, B2)) + @test length(meta) == 2 + @test meta[Int] == 1 + @test meta[String] == 2 end # https://github.com/SciML/ModelingToolkit.jl/issues/2859 diff --git a/test/optimizationsystem.jl b/test/optimizationsystem.jl index a59cb6421b..099d8346ba 100644 --- a/test/optimizationsystem.jl +++ b/test/optimizationsystem.jl @@ -1,7 +1,6 @@ using ModelingToolkit, SparseArrays, Test, Optimization, OptimizationOptimJL, OptimizationMOI, Ipopt, AmplNLWriter, Ipopt_jll, SymbolicIndexingInterface, LinearAlgebra -using ModelingToolkit: get_metadata @testset "basic" begin @variables x y @@ -228,18 +227,6 @@ end =# end -@testset "metadata" begin - @variables x - o1 = (x - 1)^2 - c1 = [ - x ~ 1 - ] - testdict = Dict(["test" => 1]) - sys1 = OptimizationSystem(o1, [x], [], name = :sys1, constraints = c1, - metadata = testdict) - @test get_metadata(sys1) == testdict -end - @testset "non-convex problem with inequalities" begin @variables x[1:2] [bounds = (0.0, Inf)] @named sys = OptimizationSystem(x[1] + x[2], [x...], []; From 9923652a1e1c22afa3eef87d44c44799829b0c7d Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 22 May 2025 02:25:37 +0530 Subject: [PATCH 1813/2176] refactor: remove `FunctionalAffect` --- ext/MTKFMIExt.jl | 14 ++-- src/systems/callbacks.jl | 107 +++---------------------------- src/systems/imperative_affect.jl | 5 +- src/systems/index_cache.jl | 3 +- 4 files changed, 20 insertions(+), 109 deletions(-) diff --git a/ext/MTKFMIExt.jl b/ext/MTKFMIExt.jl index 1c70a385a6..87ed6662d4 100644 --- a/ext/MTKFMIExt.jl +++ b/ext/MTKFMIExt.jl @@ -235,8 +235,8 @@ function MTK.FMIComponent(::Val{Ver}; fmu = nothing, tolerance = 1e-6, # instance management callback which deallocates the instance when # necessary and notifies the FMU of completed integrator steps - finalize_affect = MTK.FunctionalAffect(fmiFinalize!, [], [wrapper], []) - step_affect = MTK.FunctionalAffect(Returns(nothing), [], [], []) + finalize_affect = MTK.ImperativeAffect(fmiFinalize!; observed = (; wrapper)) + step_affect = MTK.ImperativeAffect(Returns((;))) instance_management_callback = MTK.SymbolicDiscreteCallback( (t == t - 1), step_affect; finalize = finalize_affect, reinitializealg = SciMLBase.NoInit()) @@ -273,7 +273,7 @@ function MTK.FMIComponent(::Val{Ver}; fmu = nothing, tolerance = 1e-6, end initialize_affect = MTK.ImperativeAffect(fmiCSInitialize!; observed = cb_observed, modified = cb_modified, ctx = _functor) - finalize_affect = MTK.FunctionalAffect(fmiFinalize!, [], [wrapper], []) + finalize_affect = MTK.ImperativeAffect(fmiFinalize!; observed = (; wrapper)) # the callback affect performs the stepping step_affect = MTK.ImperativeAffect( fmiCSStep!; observed = cb_observed, modified = cb_modified, ctx = _functor) @@ -708,15 +708,15 @@ end """ $(TYPEDSIGNATURES) -An affect function for use inside a `FunctionalAffect`. This should be triggered at the +An affect function for use inside an `ImperativeAffect`. This should be triggered at the end of the solve, regardless of whether it succeeded or failed. Expects `p` to be a 1-length array containing the index of the instance wrapper (`FMI2InstanceWrapper` or `FMI3InstanceWrapper`) in the parameter object. """ -function fmiFinalize!(integrator, u, p, ctx) - wrapper_idx = p[1] - wrapper = integrator.ps[wrapper_idx] +function fmiFinalize!(m, o, ctx, integrator) + wrapper = o.wrapper reset_instance!(wrapper) + return (;) end """ diff --git a/src/systems/callbacks.jl b/src/systems/callbacks.jl index 412c4e9bc7..d94d1c822c 100644 --- a/src/systems/callbacks.jl +++ b/src/systems/callbacks.jl @@ -1,58 +1,7 @@ abstract type AbstractCallback end -struct FunctionalAffect - f::Any - sts::Vector - sts_syms::Vector{Symbol} - pars::Vector - pars_syms::Vector{Symbol} - discretes::Vector - ctx::Any -end - -function FunctionalAffect(f, sts, pars, discretes, ctx = nothing) - # sts & pars contain either pairs: resistor.R => R, or Syms: R - vs = [x isa Pair ? x.first : x for x in sts] - vs_syms = Symbol[x isa Pair ? Symbol(x.second) : getname(x) for x in sts] - length(vs_syms) == length(unique(vs_syms)) || error("Variables are not unique") - - ps = [x isa Pair ? x.first : x for x in pars] - ps_syms = Symbol[x isa Pair ? Symbol(x.second) : getname(x) for x in pars] - length(ps_syms) == length(unique(ps_syms)) || error("Parameters are not unique") - - FunctionalAffect(f, vs, vs_syms, ps, ps_syms, discretes, ctx) -end - -function FunctionalAffect(; f, sts, pars, discretes, ctx = nothing) - FunctionalAffect(f, sts, pars, discretes, ctx) -end - -func(a::FunctionalAffect) = a.f -context(a::FunctionalAffect) = a.ctx -parameters(a::FunctionalAffect) = a.pars -parameters_syms(a::FunctionalAffect) = a.pars_syms -unknowns(a::FunctionalAffect) = a.sts -unknowns_syms(a::FunctionalAffect) = a.sts_syms -discretes(a::FunctionalAffect) = a.discretes - -function Base.:(==)(a1::FunctionalAffect, a2::FunctionalAffect) - isequal(a1.f, a2.f) && isequal(a1.sts, a2.sts) && isequal(a1.pars, a2.pars) && - isequal(a1.sts_syms, a2.sts_syms) && isequal(a1.pars_syms, a2.pars_syms) && - isequal(a1.ctx, a2.ctx) -end - -function Base.hash(a::FunctionalAffect, s::UInt) - s = hash(a.f, s) - s = hash(a.sts, s) - s = hash(a.sts_syms, s) - s = hash(a.pars, s) - s = hash(a.pars_syms, s) - s = hash(a.discretes, s) - hash(a.ctx, s) -end - function has_functional_affect(cb) - (affects(cb) isa FunctionalAffect || affects(cb) isa ImperativeAffect) + affects(cb) isa ImperativeAffect end struct AffectSystem @@ -97,7 +46,7 @@ function Base.hash(a::AffectSystem, s::UInt) hash(aff_to_sys(a), s) end -function vars!(vars, aff::Union{FunctionalAffect, AffectSystem}; op = Differential) +function vars!(vars, aff::AffectSystem; op = Differential) for var in Iterators.flatten((unknowns(aff), parameters(aff), discretes(aff))) vars!(vars, var) end @@ -161,7 +110,7 @@ end ############################### ###### Continuous events ###### ############################### -const Affect = Union{AffectSystem, FunctionalAffect, ImperativeAffect} +const Affect = Union{AffectSystem, ImperativeAffect} """ SymbolicContinuousCallback(eqs::Vector{Equation}, affect = nothing, iv = nothing; @@ -233,7 +182,7 @@ struct SymbolicContinuousCallback <: AbstractCallback conditions = (conditions isa AbstractVector) ? conditions : [conditions] if isnothing(reinitializealg) - if any(a -> (a isa FunctionalAffect || a isa ImperativeAffect), + if any(a -> a isa ImperativeAffect, [affect, affect_neg, initialize, finalize]) reinitializealg = SciMLBase.CheckInit() else @@ -263,8 +212,8 @@ function SymbolicContinuousCallback(cb::Tuple, args...; kwargs...) end make_affect(affect::Nothing; kwargs...) = nothing -make_affect(affect::Tuple; kwargs...) = FunctionalAffect(affect...) -make_affect(affect::NamedTuple; kwargs...) = FunctionalAffect(; affect...) +make_affect(affect::Tuple; kwargs...) = ImperativeAffect(affect...) +make_affect(affect::NamedTuple; kwargs...) = ImperativeAffect(; affect...) make_affect(affect::Affect; kwargs...) = affect function make_affect(affect::Vector{Equation}; discrete_parameters = Any[], @@ -446,7 +395,7 @@ struct SymbolicDiscreteCallback <: AbstractCallback c = is_timed_condition(condition) ? condition : value(scalarize(condition)) if isnothing(reinitializealg) - if any(a -> (a isa FunctionalAffect || a isa ImperativeAffect), + if any(a -> a isa ImperativeAffect, [affect, initialize, finalize]) reinitializealg = SciMLBase.CheckInit() else @@ -498,16 +447,6 @@ end ############################################ ########## Namespacing Utilities ########### ############################################ -function namespace_affects(affect::FunctionalAffect, s) - FunctionalAffect(func(affect), - renamespace.((s,), unknowns(affect)), - unknowns_syms(affect), - renamespace.((s,), parameters(affect)), - parameters_syms(affect), - renamespace.((s,), discretes(affect)), - context(affect)) -end - function namespace_affects(affect::AffectSystem, s) AffectSystem(renamespace(s, system(affect)), renamespace.((s,), unknowns(affect)), @@ -652,36 +591,6 @@ function compile_condition( return CompiledCondition{is_discrete(cbs)}(fs) end -""" -Compile user-defined functional affect. -""" -function compile_functional_affect(affect::FunctionalAffect, sys; kwargs...) - dvs = unknowns(sys) - ps = parameters(sys) - dvs_ind = Dict(reverse(en) for en in enumerate(dvs)) - v_inds = map(sym -> dvs_ind[sym], unknowns(affect)) - - if has_index_cache(sys) && (ic = get_index_cache(sys)) !== nothing - p_inds = [(pind = parameter_index(sys, sym)) === nothing ? sym : pind - for sym in parameters(affect)] - else - ps_ind = Dict(reverse(en) for en in enumerate(ps)) - p_inds = map(sym -> get(ps_ind, sym, sym), parameters(affect)) - end - # HACK: filter out eliminated symbols. Not clear this is the right thing to do - # (MTK should keep these symbols) - u = filter(x -> !isnothing(x[2]), collect(zip(unknowns_syms(affect), v_inds))) |> - NamedTuple - p = filter(x -> !isnothing(x[2]), collect(zip(parameters_syms(affect), p_inds))) |> - NamedTuple - - let u = u, p = p, user_affect = func(affect), ctx = context(affect) - (integ) -> begin - user_affect(integ, u, p, ctx) - end - end -end - is_discrete(cb::AbstractCallback) = cb isa SymbolicDiscreteCallback is_discrete(cb::Vector{<:AbstractCallback}) = eltype(cb) isa SymbolicDiscreteCallback @@ -837,7 +746,7 @@ function compile_affect( elseif aff isa AffectSystem f = compile_equational_affect(aff, sys; kwargs...) wrap_save_discretes(f, save_idxs) - elseif aff isa FunctionalAffect || aff isa ImperativeAffect + elseif aff isa ImperativeAffect f = compile_functional_affect(aff, sys; kwargs...) wrap_save_discretes(f, save_idxs) end diff --git a/src/systems/imperative_affect.jl b/src/systems/imperative_affect.jl index 7b05a3ead2..b05ace4106 100644 --- a/src/systems/imperative_affect.jl +++ b/src/systems/imperative_affect.jl @@ -28,7 +28,7 @@ Where we use Setfield to copy the tuple `m` with a new value for `x`, then retur `modified`; a runtime error will be produced if a value is written that does not appear in `modified`. The user can dynamically decide not to write a value back by not including it in the returned tuple, in which case the associated field will not be updated. """ -@kwdef struct ImperativeAffect +struct ImperativeAffect f::Any obs::Vector obs_syms::Vector{Symbol} @@ -63,6 +63,9 @@ function ImperativeAffect( ImperativeAffect( f, observed = observed, modified = modified, ctx = ctx, skip_checks = skip_checks) end +function ImperativeAffect(; f, kwargs...) + ImperativeAffect(f; kwargs...) +end function Base.show(io::IO, mfa::ImperativeAffect) obs_vals = join(map((ob, nm) -> "$ob => $nm", mfa.obs, mfa.obs_syms), ", ") diff --git a/src/systems/index_cache.jl b/src/systems/index_cache.jl index 948af4dfa5..5ea5fa16a7 100644 --- a/src/systems/index_cache.jl +++ b/src/systems/index_cache.jl @@ -117,8 +117,7 @@ function IndexCache(sys::AbstractSystem) affs = [affs] end for affect in affs - if affect isa AffectSystem || affect isa FunctionalAffect || - affect isa ImperativeAffect + if affect isa AffectSystem || affect isa ImperativeAffect union!(discs, unwrap.(discretes(affect))) elseif isnothing(affect) continue From e1c17a9598bac0711bc637de4849936e7454093e Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 22 May 2025 02:25:53 +0530 Subject: [PATCH 1814/2176] test: remove FuncAffect testset --- test/funcaffect.jl | 300 --------------------------------------------- test/runtests.jl | 1 - 2 files changed, 301 deletions(-) delete mode 100644 test/funcaffect.jl diff --git a/test/funcaffect.jl b/test/funcaffect.jl deleted file mode 100644 index b0745c8a9d..0000000000 --- a/test/funcaffect.jl +++ /dev/null @@ -1,300 +0,0 @@ -using ModelingToolkit, Test, OrdinaryDiffEq -using ModelingToolkitStandardLibrary.Electrical -using ModelingToolkitStandardLibrary.Blocks -using ModelingToolkit: t_nounits as t, D_nounits as D - -@constants h=1 zr=0 -@variables u(t) - -eqs = [D(u) ~ -u] - -affect1!(integ, u, p, ctx) = integ.u[u.u] += 10 - -@named sys = System(eqs, t, [u], [], - discrete_events = [[4.0] => (affect1!, [u], [], [], nothing)]) -prob = ODEProblem(complete(sys), [u => 10.0], (0, 10.0)) -sol = solve(prob, Tsit5()) -i4 = findfirst(==(4.0), sol[:t]) -@test sol.u[i4 + 1][1] > 10.0 - -# callback -cb = ModelingToolkit.SymbolicDiscreteCallback(t == zr, - (f = affect1!, sts = [], pars = [], discretes = [], - ctx = [1])) -cb1 = ModelingToolkit.SymbolicDiscreteCallback(t == zr, (affect1!, [], [], [], [1])) -@test ModelingToolkit.affects(cb) isa ModelingToolkit.FunctionalAffect -@test cb == cb1 -@test ModelingToolkit.SymbolicDiscreteCallback(cb) === cb # passthrough -@test hash(cb) == hash(cb1) -ModelingToolkit.generate_callback(cb, sys); - -cb = ModelingToolkit.SymbolicContinuousCallback([t ~ zr], - (f = affect1!, sts = [], pars = [], discretes = [], - ctx = [1])) -cb1 = ModelingToolkit.SymbolicContinuousCallback([t ~ zr], (affect1!, [], [], [], [1])) -@test cb == cb1 -@test ModelingToolkit.SymbolicContinuousCallback(cb) === cb # passthrough -@test hash(cb) == hash(cb1) - -# named tuple -sys1 = System(eqs, t, [u], [], name = :sys, - discrete_events = [ - [4.0] => (f = affect1!, sts = [u], pars = [], discretes = [], ctx = nothing) - ]) -@test sys == sys1 - -# has_functional_affect -de = ModelingToolkit.get_discrete_events(sys1) -@test length(de) == 1 -de = de[1] -@test ModelingToolkit.conditions(de) == [4.0] -@test ModelingToolkit.has_functional_affect(de) - -sys2 = System(eqs, t, [u], [], name = :sys, - discrete_events = [[4.0] => [u ~ -u * h]]) -@test !ModelingToolkit.has_functional_affect(ModelingToolkit.get_discrete_events(sys2)[1]) - -# context -function affect2!(integ, u, p, ctx) - integ.u[u.u] += ctx[1] - ctx[1] *= 2 -end -ctx1 = [10.0] -@named sys = System(eqs, t, [u], [], - discrete_events = [[4.0, 8.0] => (affect2!, [u], [], [], ctx1)]) -prob = ODEProblem(complete(sys), [u => 10.0], (0, 10.0)) -sol = solve(prob, Tsit5()) -i4 = findfirst(==(4.0), sol[:t]) -@test sol.u[i4 + 1][1] > 10.0 -i8 = findfirst(==(8.0), sol[:t]) -@test sol.u[i8 + 1][1] > 20.0 -@test ctx1[1] == 40.0 - -# parameter -function affect3!(integ, u, p, ctx) - integ.u[u.u] += integ.ps[p.a] - integ.ps[p.a] *= 2 -end - -@parameters a = 10.0 -@named sys = System(eqs, t, [u], [a], - discrete_events = [[4.0, 8.0] => (affect3!, [u], [a], [a], nothing)]) -prob = ODEProblem(complete(sys), [u => 10.0], (0, 10.0)) - -sol = solve(prob, Tsit5()) -i4 = findfirst(==(4.0), sol[:t]) -@test sol.u[i4 + 1][1] > 10.0 -i8 = findfirst(==(8.0), sol[:t]) -@test sol.u[i8 + 1][1] > 20.0 - -# rename parameter -function affect3!(integ, u, p, ctx) - integ.u[u.u] += integ.ps[p.b] - integ.ps[p.b] *= 2 -end - -@named sys = System(eqs, t, [u], [a], - discrete_events = [ - [4.0, 8.0] => (affect3!, [u], [a => :b], [a], nothing) - ]) -prob = ODEProblem(complete(sys), [u => 10.0], (0, 10.0)) - -sol = solve(prob, Tsit5()) -i4 = findfirst(==(4.0), sol[:t]) -@test sol.u[i4 + 1][1] > 10.0 -i8 = findfirst(==(8.0), sol[:t]) -@test sol.u[i8 + 1][1] > 20.0 - -# same name -@variables v(t) -@test_throws ErrorException System(eqs, t, [u], [a], - discrete_events = [ - [4.0, 8.0] => (affect3!, [u, v => :u], [a], [a], - nothing) - ]; name = :sys) - -@test_nowarn System(eqs, t, [u], [a], - discrete_events = [ - [4.0, 8.0] => (affect3!, [u], [a => :u], [a], nothing) - ]; name = :sys) - -@named resistor = System(D(v) ~ v, t, [v], []) - -# nested namespace -ctx = [0] -function affect4!(integ, u, p, ctx) - ctx[1] += 1 - @test u.resistor₊v == 1 -end -s1 = compose( - System(Equation[], t, [], [], name = :s1, - discrete_events = 1.0 => (affect4!, [resistor.v], [], [], ctx)), - resistor) -s2 = structural_simplify(s1) -prob = ODEProblem(s2, [resistor.v => 10.0], (0, 2.01)) -sol = solve(prob, Tsit5()) -@test ctx[1] == 2 - -include("common/rc_model.jl") - -function affect5!(integ, u, p, ctx) - @test integ.u[u.capacitor₊v] ≈ 0.3 - integ.ps[p.C] *= 200 -end - -@unpack capacitor = rc_model -@named event_sys = System(Equation[], t; - continuous_events = [ - [capacitor.v ~ 0.3] => (affect5!, [capacitor.v], - [capacitor.C => :C], [capacitor.C], nothing) - ]) -rc_model = extend(rc_model, event_sys) -# rc_model = compose(rc_model, [resistor, capacitor, source, ground]) - -sys = structural_simplify(rc_model) -u0 = [capacitor.v => 0.0 - capacitor.p.i => 0.0 - resistor.v => 0.0] - -prob = ODEProblem(sys, u0, (0, 10.0)) -sol = solve(prob, Rodas4()) -@test all(sol[rc_model.capacitor.v] .< 0.4) - -# hierarchical - result should be identical - -function affect6!(integ, u, p, ctx) - @test integ.u[u.v] ≈ 0.3 - integ.ps[p.C] *= 200 -end - -function Capacitor2(; name, C = 1.0) - @named oneport = OnePort() - @unpack v, i = oneport - ps = @parameters C = C - eqs = [ - D(v) ~ i / C - ] - extend( - System(eqs, t, [], ps; name = name, - continuous_events = [[v ~ 0.3] => (affect6!, [v], [C], [C], nothing)]), - oneport) -end - -@named begin - capacitor2 = Capacitor2(C = 1.0) - resistor = Resistor(R = 1.0) - capacitor = Capacitor(C = 1.0) - shape = Constant(k = 1.0) - source = Voltage() - ground = Ground() -end - -rc_eqs2 = [connect(shape.output, source.V) - connect(source.p, resistor.p) - connect(resistor.n, capacitor2.p) - connect(capacitor2.n, source.n) - connect(capacitor2.n, ground.g)] - -@named rc_model2 = System(rc_eqs2, t) -rc_model2 = compose(rc_model2, [resistor, capacitor2, shape, source, ground]) - -sys2 = structural_simplify(rc_model2) -u0 = [capacitor2.v => 0.0 - capacitor2.p.i => 0.0 - resistor.v => 0.0] - -prob2 = ODEProblem(sys2, u0, (0, 10.0)) -sol2 = solve(prob2, Rodas4()) -@test all(sol2[rc_model2.capacitor2.v] .== sol[rc_model.capacitor.v]) - -# discrete events - -a7_count = 0 -function affect7!(integ, u, p, ctx) - integ.ps[p.g] = 0 - ctx[1] += 1 - @test ctx[1] <= 2 - @test (ctx[1] == 1 && integ.t == 1.0) || (ctx[1] == 2 && integ.t == 2.0) - global a7_count += 1 -end - -a7_ctx = [0] -function Ball(; name, g = 9.8, anti_gravity_time = 1.0) - pars = @parameters g = g - sts = @variables x(t), v(t) - eqs = [D(x) ~ v, D(v) ~ g] - System(eqs, t, sts, pars; name = name, - discrete_events = [[anti_gravity_time] => (affect7!, [], [g], [g], a7_ctx)]) -end - -@named ball1 = Ball(anti_gravity_time = 1.0) -@named ball2 = Ball(anti_gravity_time = 2.0) - -@named balls = System(Equation[], t) -balls = compose(balls, [ball1, ball2]) - -@test ModelingToolkit.has_discrete_events(balls) -@test length(ModelingToolkit.affects(ModelingToolkit.discrete_events(balls))) == 2 - -prob = ODEProblem(complete(balls), - [ball1.x => 10.0, ball1.v => 0, ball2.x => 10.0, ball2.v => 0], - (0, 3.0)) -sol = solve(prob, Tsit5()) - -@test a7_count == 2 -@test sol(0.99)[1] == sol(0.99)[3] -@test sol(1.01)[4] > sol(1.01)[2] -@test sol(1.99)[2] == sol(1.01)[2] -@test sol(1.99)[4] > sol(1.01)[4] -@test sol(2.5)[4] == sol(3.0)[4] - -# bouncing ball - -# DiffEq implementation -function f_(du, u, p, t) - du[1] = u[2] - du[2] = -p -end - -function condition_(u, t, integrator) # Event when event_f(u,t) == 0 - u[1] -end - -function affect_!(integrator) - integrator.u[2] = -integrator.u[2] -end - -cb_ = ContinuousCallback(condition_, affect_!) - -u0 = [50.0, 0.0] -tspan = (0.0, 15.0) -p = 9.8 -prob_ = ODEProblem(f_, u0, tspan, p) -sol_ = solve(prob_, Tsit5(), callback = cb_) - -# same - with MTK -sts = @variables y(t), v(t) -par = @parameters g = 9.8 -bb_eqs = [D(y) ~ v - D(v) ~ -g] - -function bb_affect!(integ, u, p, ctx) - integ.u[u.v] = -integ.u[u.v] -end - -@named bb_model = System(bb_eqs, t, sts, [par; zr], - continuous_events = [ - [y ~ zr] => (bb_affect!, [v], [], [], nothing) - ]) - -bb_sys = structural_simplify(bb_model) -@test only(ModelingToolkit.affects(ModelingToolkit.continuous_events(bb_sys))) isa - ModelingToolkit.FunctionalAffect - -u0 = [v => 0.0, y => 50.0] - -bb_prob = ODEProblem(bb_sys, u0, (0, 15.0)) -bb_sol = solve(bb_prob, Tsit5()) - -@test bb_sol[y] ≈ map(u -> u[1], sol_.u) -@test bb_sol[v] ≈ map(u -> u[2], sol_.u) diff --git a/test/runtests.jl b/test/runtests.jl index 426993c414..2014162cc3 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -57,7 +57,6 @@ end @safetestset "DAE Jacobians Test" include("dae_jacobian.jl") @safetestset "Jacobian Sparsity" include("jacobiansparsity.jl") @safetestset "Modelingtoolkitize Test" include("modelingtoolkitize.jl") - @safetestset "FuncAffect Test" include("funcaffect.jl") @safetestset "Constants Test" include("constants.jl") @safetestset "Parameter Dependency Test" include("parameter_dependencies.jl") @safetestset "Equation Type Accessors Test" include("equation_type_accessors.jl") From dcddee3b8a03c8e9738e1a88156fb628ca385a90 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 22 May 2025 02:26:02 +0530 Subject: [PATCH 1815/2176] test: update tests to use `ImperativeAffect` --- test/index_cache.jl | 8 ++-- test/initializationsystem.jl | 4 +- test/jumpsystem.jl | 6 +-- test/parameter_dependencies.jl | 6 +-- test/symbolic_events.jl | 76 ++++++++++++++++------------------ 5 files changed, 49 insertions(+), 51 deletions(-) diff --git a/test/index_cache.jl b/test/index_cache.jl index 3048084d96..5573563d32 100644 --- a/test/index_cache.jl +++ b/test/index_cache.jl @@ -98,15 +98,17 @@ mutable struct ParamTest end (pt::ParamTest)(x) = pt.y - x @testset "Issue#3215: Callable discrete parameter" begin - function update_affect!(integ, u, p, ctx) - integ.p[p.p_1].y = integ.t + function update_affect!(mod, obs, ctx, integ) + p_1 = mod.p_1 + p_1.y = integ.t + return (; p_1) end tp1 = typeof(ParamTest(1)) @parameters (p_1::tp1)(..) = ParamTest(1) @variables x(ModelingToolkit.t_nounits) = 0 - event1 = [1.0, 2, 3] => (update_affect!, [], [p_1], [p_1], nothing) + event1 = [1.0, 2, 3] => (f = update_affect!, modified = (; p_1)) @named sys = System([ ModelingToolkit.D_nounits(x) ~ p_1(x) diff --git a/test/initializationsystem.jl b/test/initializationsystem.jl index eda381b5a3..cca4be1f76 100644 --- a/test/initializationsystem.jl +++ b/test/initializationsystem.jl @@ -1367,13 +1367,13 @@ end @testset "Issue#3342" begin @variables x(t) y(t) - stop!(integrator, _, _, _) = terminate!(integrator) + stop!(mod, obs, ctx, integrator) = (terminate!(integrator); return (;)) @named sys = System([D(x) ~ 1.0 D(y) ~ 1.0], t; initialization_eqs = [ y ~ 0.0 ], continuous_events = [ - [y ~ 0.5] => (stop!, [y], [], [], nothing) + [y ~ 0.5] => (; f = stop!) ]) sys = structural_simplify(sys) prob0 = ODEProblem(sys, [x => NaN], (0.0, 1.0), []) diff --git a/test/jumpsystem.jl b/test/jumpsystem.jl index a42ba5ecb9..b48b17ce60 100644 --- a/test/jumpsystem.jl +++ b/test/jumpsystem.jl @@ -516,12 +516,12 @@ end @test all(abs.(Xv .- Xact) .<= 0.05 .* Xv) @test all(abs.(Yv .- Yact) .<= 0.05 .* Yv) - function affect!(integ, u, p, ctx) + function affect!(mod, obs, ctx, integ) savevalues!(integ, true) terminate!(integ) - nothing + (;) end - cevents = [t ~ 0.2] => (affect!, [], [], [], nothing) + cevents = [t ~ 0.2] => (; f = affect!) @named jsys = JumpSystem([maj, crj, vrj, eqs[1]], t, [X, Y], [α, β]; continuous_events = cevents) jsys = complete(jsys) diff --git a/test/parameter_dependencies.jl b/test/parameter_dependencies.jl index 5498ecf4d8..1ea796507d 100644 --- a/test/parameter_dependencies.jl +++ b/test/parameter_dependencies.jl @@ -14,10 +14,10 @@ using NonlinearSolve @parameters p1(t)=1.0 p2 @variables x(t) cb1 = SymbolicContinuousCallback([x ~ 2.0] => [p1 ~ 2.0], discrete_parameters = [p1]) # triggers at t=-2+√6 - function affect1!(integ, u, p, ctx) - integ.ps[p[1]] = integ.ps[p[2]] + function affect1!(mod, obs, ctx, integ) + return (; p1 = obs.p2) end - cb2 = [x ~ 4.0] => (affect1!, [], [p1, p2], [p1]) # triggers at t=-2+√7 + cb2 = [x ~ 4.0] => (f = affect1!, observed = (; p2), modified = (; p1)) # triggers at t=-2+√7 cb3 = SymbolicDiscreteCallback([1.0] => [p1 ~ 5.0], discrete_parameters = [p1]) @mtkbuild sys = System( diff --git a/test/symbolic_events.jl b/test/symbolic_events.jl index ee0eaf6392..d3c767ee0d 100644 --- a/test/symbolic_events.jl +++ b/test/symbolic_events.jl @@ -514,11 +514,10 @@ end testsol(ssys3, SDEProblem, RI5, u0, p, tspan; tstops = [1.0], paramtotest = k) # mixing with a func affect - function affect!(integrator, u, p, ctx) - integrator.ps[p.k] = 1.0 - nothing + function affect!(mod, obs, ctx, integ) + return (; k = 1.0) end - cb2‵‵ = [2.0] => (affect!, [], [k], [k], nothing) + cb2‵‵ = [2.0] => (f = affect!, modified = (; k)) @named osys4 = System(eqs, t, [A], [k, t1], discrete_events = [cb1, cb2‵‵]) @named ssys4 = SDESystem(eqs, [0.0], t, [A], [k, t1], discrete_events = [cb1, cb2‵‵]) @@ -527,7 +526,7 @@ end testsol(ssys4, SDEProblem, RI5, u0, p, tspan; tstops = [1.0], paramtotest = k) # mixing with symbolic condition in the func affect - cb2‵‵‵ = (t == t2) => (affect!, [], [k], [k], nothing) + cb2‵‵‵ = (t == t2) => (f = affect!, modified = (; k)) @named osys5 = System(eqs, t, [A], [k, t1, t2], discrete_events = [cb1, cb2‵‵‵]) @named ssys5 = SDESystem(eqs, [0.0], t, [A], [k, t1, t2], discrete_events = [cb1, cb2‵‵‵]) @@ -605,17 +604,15 @@ end testsol(jsys3, u0, p, tspan; tstops = [1.0], rng, paramtotest = k) # mixing with a func affect - function affect!(integrator, u, p, ctx) - integrator.ps[p.k] = 1.0 - reset_aggregated_jumps!(integrator) - nothing + function affect!(mod, obs, ctx, integrator) + return (; k = 1.0) end - cb2‵‵ = [2.0] => (affect!, [], [k], [k], nothing) + cb2‵‵ = [2.0] => (f = affect!, modified = (; k)) @named jsys4 = JumpSystem(eqs, t, [A], [k, t1], discrete_events = [cb1, cb2‵‵]) testsol(jsys4, u0, p, tspan; tstops = [1.0], rng, paramtotest = k) # mixing with symbolic condition in the func affect - cb2‵‵‵ = (t == t2) => (affect!, [], [k], [k], nothing) + cb2‵‵‵ = (t == t2) => (f = affect!, modified = (; k)) @named jsys5 = JumpSystem(eqs, t, [A], [k, t1, t2], discrete_events = [cb1, cb2‵‵‵]) testsol(jsys5, u0, p, tspan; tstops = [1.0, 2.0], rng, paramtotest = k) @named jsys6 = JumpSystem(eqs, t, [A], [k, t1, t2], discrete_events = [cb2‵‵‵, cb1]) @@ -649,13 +646,16 @@ end # baseline affect (pos + neg + left root find) @variables c1(t)=1.0 c2(t)=1.0 # c1 = cos(t), c2 = cos(3t) eqs = [D(c1) ~ -sin(t); D(c2) ~ -3 * sin(3 * t)] - record_crossings(i, u, _, c) = push!(c, i.t => i.u[u.v]) + function record_crossings(mod, obs, ctx, integ) + push!(ctx, integ.t => obs.v) + return (;) + end cr1 = [] cr2 = [] evt1 = ModelingToolkit.SymbolicContinuousCallback( - [c1 ~ 0], (record_crossings, [c1 => :v], [], [], cr1)) + [c1 ~ 0], (f = record_crossings, observed = (; v = c1), ctx = cr1)) evt2 = ModelingToolkit.SymbolicContinuousCallback( - [c2 ~ 0], (record_crossings, [c2 => :v], [], [], cr2)) + [c2 ~ 0], (f = record_crossings, observed = (; v = c2), ctx = cr2)) @named trigsys = System(eqs, t; continuous_events = [evt1, evt2]) trigsys_ss = structural_simplify(trigsys) prob = ODEProblem(trigsys_ss, [], (0.0, 2π)) @@ -673,11 +673,11 @@ end cr1n = [] cr2n = [] evt1 = ModelingToolkit.SymbolicContinuousCallback( - [c1 ~ 0], (record_crossings, [c1 => :v], [], [], cr1p); - affect_neg = (record_crossings, [c1 => :v], [], [], cr1n)) + [c1 ~ 0], (f = record_crossings, observed = (; v = c1), ctx = cr1p); + affect_neg = (f = record_crossings, observed = (; v = c1), ctx = cr1n)) evt2 = ModelingToolkit.SymbolicContinuousCallback( - [c2 ~ 0], (record_crossings, [c2 => :v], [], [], cr2p); - affect_neg = (record_crossings, [c2 => :v], [], [], cr2n)) + [c2 ~ 0], (f = record_crossings, observed = (; v = c2), ctx = cr2p); + affect_neg = (f = record_crossings, observed = (; v = c2), ctx = cr2n)) @named trigsys = System(eqs, t; continuous_events = [evt1, evt2]) trigsys_ss = structural_simplify(trigsys) prob = ODEProblem(trigsys_ss, [], (0.0, 2π)) @@ -699,9 +699,9 @@ end cr1p = [] cr2p = [] evt1 = ModelingToolkit.SymbolicContinuousCallback( - [c1 ~ 0], (record_crossings, [c1 => :v], [], [], cr1p); affect_neg = nothing) + [c1 ~ 0], (f = record_crossings, observed = (; v = c1), ctx = cr1p); affect_neg = nothing) evt2 = ModelingToolkit.SymbolicContinuousCallback( - [c2 ~ 0], (record_crossings, [c2 => :v], [], [], cr2p); affect_neg = nothing) + [c2 ~ 0], (f = record_crossings, observed = (; v = c2), ctx = cr2p); affect_neg = nothing) @named trigsys = System(eqs, t; continuous_events = [evt1, evt2]) trigsys_ss = structural_simplify(trigsys) prob = ODEProblem(trigsys_ss, [], (0.0, 2π)) @@ -717,10 +717,10 @@ end cr1n = [] cr2n = [] evt1 = ModelingToolkit.SymbolicContinuousCallback( - [c1 ~ 0], (record_crossings, [c1 => :v], [], [], cr1p); affect_neg = nothing) + [c1 ~ 0], (f = record_crossings, observed = (; v = c1), ctx = cr1p); affect_neg = nothing) evt2 = ModelingToolkit.SymbolicContinuousCallback( - [c2 ~ 0], (record_crossings, [c2 => :v], [], [], cr2p); - affect_neg = (record_crossings, [c2 => :v], [], [], cr2n)) + [c2 ~ 0], (f = record_crossings, observed = (; v = c2), ctx = cr2p); + affect_neg = (f = record_crossings, observed = (; v = c2), ctx = cr2n)) @named trigsys = System(eqs, t; continuous_events = [evt1, evt2]) trigsys_ss = structural_simplify(trigsys) prob = ODEProblem(trigsys_ss, [], (0.0, 2π)) @@ -740,10 +740,10 @@ end cr1 = [] cr2 = [] evt1 = ModelingToolkit.SymbolicContinuousCallback( - [c1 ~ 0], (record_crossings, [c1 => :v], [], [], cr1); + [c1 ~ 0], (f = record_crossings, observed = (; v = c1), ctx = cr1); rootfind = SciMLBase.RightRootFind) evt2 = ModelingToolkit.SymbolicContinuousCallback( - [c2 ~ 0], (record_crossings, [c2 => :v], [], [], cr2); + [c2 ~ 0], (f = record_crossings, observed = (; v = c2), ctx = cr2); rootfind = SciMLBase.RightRootFind) @named trigsys = System(eqs, t; continuous_events = [evt1, evt2]) trigsys_ss = structural_simplify(trigsys) @@ -760,10 +760,10 @@ end cr1 = [] cr2 = [] evt1 = ModelingToolkit.SymbolicContinuousCallback( - [c1 ~ 0], (record_crossings, [c1 => :v], [], [], cr1); + [c1 ~ 0], (f = record_crossings, observed = (; v = c1), ctx = cr1); rootfind = SciMLBase.LeftRootFind) evt2 = ModelingToolkit.SymbolicContinuousCallback( - [c2 ~ 0], (record_crossings, [c2 => :v], [], [], cr2); + [c2 ~ 0], (f = record_crossings, observed = (; v = c2), ctx = cr2); rootfind = SciMLBase.RightRootFind) @named trigsys = System(eqs, t; continuous_events = [evt1, evt2]) trigsys_ss = structural_simplify(trigsys) @@ -778,10 +778,10 @@ end cr1 = [] cr2 = [] evt1 = ModelingToolkit.SymbolicContinuousCallback( - [c1 ~ 0], (record_crossings, [c1 => :v], [], [], cr1); + [c1 ~ 0], (f = record_crossings, observed = (; v = c1), ctx = cr1); rootfind = SciMLBase.LeftRootFind) evt2 = ModelingToolkit.SymbolicContinuousCallback( - [c2 ~ 0], (record_crossings, [c2 => :v], [], [], cr2); + [c2 ~ 0], (f = record_crossings, observed = (; v = c2), ctx = cr2); rootfind = SciMLBase.RightRootFind) @named trigsys = System(eqs, t; continuous_events = [evt2, evt1]) trigsys_ss = structural_simplify(trigsys) @@ -879,10 +879,10 @@ end @variables x(t) @parameters a(t) b(t) c(t) cb1 = SymbolicContinuousCallback([x ~ 1.0] => [a ~ -Pre(a)], discrete_parameters = [a]) - function save_affect!(integ, u, p, ctx) - integ.ps[p.b] = 5.0 + function save_affect!(mod, obs, ctx, integ) + return (; b = 5.0) end - cb2 = [x ~ 0.5] => (save_affect!, [], [b], [b], nothing) + cb2 = [x ~ 0.5] => (f = save_affect!, modified = (; b)) cb3 = SymbolicDiscreteCallback(1.0 => [c ~ t], discrete_parameters = [c]) @mtkbuild sys = System(D(x) ~ cos(t), t, [x], [a, b, c]; @@ -1067,8 +1067,7 @@ end @testset "Initialization" begin @variables x(t) seen = false - f = ModelingToolkit.FunctionalAffect( - f = (i, u, p, c) -> seen = true, sts = [], pars = [], discretes = []) + f = ModelingToolkit.ImperativeAffect(f = (m, o, ctx, int) -> (seen = true; return (;))) cb1 = ModelingToolkit.SymbolicContinuousCallback( [x ~ 0], nothing, initialize = [x ~ 1.5], finalize = f) @mtkbuild sys = System(D(x) ~ -1, t, [x], []; continuous_events = [cb1]) @@ -1080,16 +1079,13 @@ end @variables x(t) seen = false - f = ModelingToolkit.FunctionalAffect( - f = (i, u, p, c) -> seen = true, sts = [], pars = [], discretes = []) + f = ModelingToolkit.ImperativeAffect(f = (m, o, ctx, int) -> (seen = true; return (;))) cb1 = ModelingToolkit.SymbolicContinuousCallback( [x ~ 0], nothing, initialize = [x ~ 1.5], finalize = f) inited = false finaled = false - a = ModelingToolkit.FunctionalAffect( - f = (i, u, p, c) -> inited = true, sts = [], pars = [], discretes = []) - b = ModelingToolkit.FunctionalAffect( - f = (i, u, p, c) -> finaled = true, sts = [], pars = [], discretes = []) + a = ModelingToolkit.ImperativeAffect(f = (m, o, ctx, int) -> (inited = true; return (;))) + b = ModelingToolkit.ImperativeAffect(f = (m, o, ctx, int) -> (finaled = true; return (;))) cb2 = ModelingToolkit.SymbolicContinuousCallback( [x ~ 0.1], nothing, initialize = a, finalize = b) @mtkbuild sys = System(D(x) ~ -1, t, [x], []; continuous_events = [cb1, cb2]) From 3f70980f16d57804ad2e764824f4222ef26b9034 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 22 May 2025 12:36:46 +0530 Subject: [PATCH 1816/2176] fix: call `reset_aggregated_jumps!` in compiled equational affect --- src/systems/callbacks.jl | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/systems/callbacks.jl b/src/systems/callbacks.jl index d94d1c822c..7be1397922 100644 --- a/src/systems/callbacks.jl +++ b/src/systems/callbacks.jl @@ -855,7 +855,8 @@ function compile_equational_affect( end else return let dvs_to_update = dvs_to_update, aff_map = aff_map, sys_map = sys_map, - affsys = affsys, ps_to_update = ps_to_update, aff = aff, sys = sys + affsys = affsys, ps_to_update = ps_to_update, aff = aff, sys = sys, + reset_jumps = reset_jumps dvs_to_access = [aff_map[u] for u in unknowns(affsys)] ps_to_access = [unPre(p) for p in parameters(affsys)] @@ -888,6 +889,8 @@ function compile_equational_affect( u_setter!(integ, u_getter(affsol)) p_setter!(integ, p_getter(affsol)) + + reset_jumps && reset_aggregated_jumps!(integ) end end end From 059128159e4d6bb15afe15b76179d15d3b629b72 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 22 May 2025 12:36:58 +0530 Subject: [PATCH 1817/2176] fix: call `reset_aggregated_jumps!` in compiled `ImperativeAffect` --- src/systems/imperative_affect.jl | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/systems/imperative_affect.jl b/src/systems/imperative_affect.jl index b05ace4106..7b1a9fb286 100644 --- a/src/systems/imperative_affect.jl +++ b/src/systems/imperative_affect.jl @@ -167,7 +167,8 @@ function check_assignable(sys, sym) end end -function compile_functional_affect(affect::ImperativeAffect, sys; kwargs...) +function compile_functional_affect( + affect::ImperativeAffect, sys; reset_jumps = false, kwargs...) #= Implementation sketch: generate observed function (oop), should save to a component array under obs_syms @@ -247,7 +248,7 @@ function compile_functional_affect(affect::ImperativeAffect, sys; kwargs...) upd_funs = NamedTuple{mod_names}((setu.((sys,), first.(mod_pairs))...,)) - let user_affect = func(affect), ctx = context(affect) + let user_affect = func(affect), ctx = context(affect), reset_jumps = reset_jumps @inline function (integ) # update the to-be-mutated values; this ensures that if you do a no-op then nothing happens modvals = mod_og_val_fun(integ.u, integ.p, integ.t) @@ -262,6 +263,8 @@ function compile_functional_affect(affect::ImperativeAffect, sys; kwargs...) # write the new values back to the integrator _generated_writeback(integ, upd_funs, upd_vals) + + reset_jumps && reset_aggregated_jumps!(integ) end end end From 148a15486dedb8723a3b9c00e8ab76e78a7462f5 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 22 May 2025 13:25:29 +0530 Subject: [PATCH 1818/2176] refactor: format --- src/linearization.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/linearization.jl b/src/linearization.jl index 8d6edf8f07..25b42dffb3 100644 --- a/src/linearization.jl +++ b/src/linearization.jl @@ -612,7 +612,7 @@ function markio!(state, orig_inputs, inputs, outputs, disturbances; check = true end (all(values(outputset)) || error( "Some specified outputs were not found in system. The following Dict indicates the found variables ", - outputset)) + outputset)) end state, orig_inputs end From 15782f274e62776385334d5adbf23fc699caa3a2 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 22 May 2025 15:55:38 +0530 Subject: [PATCH 1819/2176] refactor: rename `@mtkbuild` to `@mtkcompile` --- src/ModelingToolkit.jl | 2 +- src/problems/bvproblem.jl | 2 +- src/systems/abstractsystem.jl | 4 +- src/systems/parameter_buffer.jl | 2 +- test/bvproblem.jl | 26 +++--- test/clock.jl | 4 +- test/code_generation.jl | 2 +- test/constants.jl | 2 +- test/dde.jl | 6 +- test/debugging.jl | 2 +- test/discrete_system.jl | 22 ++--- test/dq_units.jl | 4 +- test/extensions/ad.jl | 4 +- test/extensions/bifurcationkit.jl | 2 +- test/extensions/dynamic_optimization.jl | 12 +-- test/extensions/homotopy_continuation.jl | 36 ++++---- test/fmi/fmi.jl | 16 ++-- test/guess_propagation.jl | 8 +- test/hierarchical_initialization_eqs.jl | 2 +- test/if_lifting.jl | 8 +- test/implicit_discrete_system.jl | 10 +-- test/initial_values.jl | 44 ++++----- test/initializationsystem.jl | 110 +++++++++++------------ test/input_output_handling.jl | 2 +- test/jacobiansparsity.jl | 4 +- test/jumpsystem.jl | 6 +- test/model_parsing.jl | 4 +- test/modelingtoolkitize.jl | 6 +- test/mtkparameters.jl | 8 +- test/nonlinearsystem.jl | 10 +-- test/odesystem.jl | 58 ++++++------ test/optimizationsystem.jl | 8 +- test/parameter_dependencies.jl | 12 +-- test/problem_validation.jl | 4 +- test/scc_nonlinear_problem.jl | 20 ++--- test/sdesystem.jl | 38 ++++---- test/split_parameters.jl | 4 +- test/structural_transformation/utils.jl | 26 +++--- test/symbolic_events.jl | 36 ++++---- test/symbolic_indexing_interface.jl | 6 +- test/variable_utils.jl | 2 +- 41 files changed, 292 insertions(+), 292 deletions(-) diff --git a/src/ModelingToolkit.jl b/src/ModelingToolkit.jl index bf6dfc3af2..c34e6df3d3 100644 --- a/src/ModelingToolkit.jl +++ b/src/ModelingToolkit.jl @@ -288,7 +288,7 @@ export alias_elimination, flatten export connect, domain_connect, @connector, Connection, AnalysisPoint, Flow, Stream, instream export initial_state, transition, activeState, entry, ticksInState, timeInState -export @component, @mtkmodel, @mtkbuild +export @component, @mtkmodel, @mtkcompile export isinput, isoutput, getbounds, hasbounds, getguess, hasguess, isdisturbance, istunable, getdist, hasdist, tunable_parameters, isirreducible, getdescription, hasdescription, diff --git a/src/problems/bvproblem.jl b/src/problems/bvproblem.jl index b0ad28a842..82783077c1 100644 --- a/src/problems/bvproblem.jl +++ b/src/problems/bvproblem.jl @@ -30,7 +30,7 @@ If a `System` without `constraints` is specified, it will be treated as an initi D(D(y)) ~ λ * y - g x(t)^2 + y^2 ~ 1] cstr = [x(0.5) ~ 1] - @mtkbuild pend = System(eqs, t; constraints = cstrs) + @mtkcompile pend = System(eqs, t; constraints = cstrs) tspan = (0.0, 1.5) u0map = [x(t) => 0.6, y => 0.8] diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index 6ede839e4a..4efc9be0b4 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -146,7 +146,7 @@ may be subsetted using `dvs` and `ps`. All `kwargs` are passed to the internal [`build_function`](@ref) call. The returned function can be called as `f(u, p, t)` or `f(du, u, p, t)` for time-dependent systems and `f(u, p)` or `f(du, u, p)` for time-independent systems. If `split=true` (the default) was passed to [`complete`](@ref), -[`structural_simplify`](@ref) or [`@mtkbuild`](@ref), `p` is expected to be an `MTKParameters` +[`structural_simplify`](@ref) or [`@mtkcompile`](@ref), `p` is expected to be an `MTKParameters` object. """ function generate_custom_function(sys::AbstractSystem, exprs, dvs = unknowns(sys), @@ -2470,7 +2470,7 @@ macro component(expr) esc(component_post_processing(expr, false)) end -macro mtkbuild(exprs...) +macro mtkcompile(exprs...) expr = exprs[1] named_expr = ModelingToolkit.named_expr(expr) name = named_expr.args[1] diff --git a/src/systems/parameter_buffer.jl b/src/systems/parameter_buffer.jl index 83d165eefc..37dc6d1236 100644 --- a/src/systems/parameter_buffer.jl +++ b/src/systems/parameter_buffer.jl @@ -23,7 +23,7 @@ dependent systems. It is only required if the symbolic expressions also use the variable of the system. This requires that `complete` has been called on the system (usually via -`structural_simplify` or `@mtkbuild`) and the keyword `split = true` was passed (which is +`structural_simplify` or `@mtkcompile`) and the keyword `split = true` was passed (which is the default behavior). """ function MTKParameters( diff --git a/test/bvproblem.jl b/test/bvproblem.jl index 0931a0ab6d..bafb3e9674 100644 --- a/test/bvproblem.jl +++ b/test/bvproblem.jl @@ -20,7 +20,7 @@ daesolvers = [Ascher2, Ascher4, Ascher6] parammap = [α => 7.5, β => 4, γ => 8.0, δ => 5.0] tspan = (0.0, 10.0) - @mtkbuild lotkavolterra = System(eqs, t) + @mtkcompile lotkavolterra = System(eqs, t) op = ODEProblem(lotkavolterra, u0map, tspan, parammap) osol = solve(op, Vern9()) @@ -52,7 +52,7 @@ end eqs = [D(θ) ~ θ_t D(θ_t) ~ -(g / L) * sin(θ)] - @mtkbuild pend = System(eqs, t) + @mtkcompile pend = System(eqs, t) u0map = [θ => π / 2, θ_t => π / 2] parammap = [:L => 1.0, :g => 9.81] @@ -91,7 +91,7 @@ end D(y(t)) ~ -γ * y(t) + δ * x(t) * y(t)] tspan = (0.0, 1.0) - @mtkbuild lksys = System(eqs, t) + @mtkcompile lksys = System(eqs, t) function lotkavolterra!(du, u, p, t) du[1] = p[1] * u[1] - p[2] * u[1] * u[2] @@ -104,7 +104,7 @@ end # Test with a constraint. constr = [y(0.5) ~ 2.0] - @mtkbuild lksys = System(eqs, t; constraints = constr) + @mtkcompile lksys = System(eqs, t; constraints = constr) function bc!(resid, u, p, t) resid[1] = u(0.0)[1] - 1.0 @@ -177,7 +177,7 @@ end tspan = (0.0, 1.0) guess = [x(t) => 4.0, y(t) => 2.0] constr = [x(0.6) ~ 3.5, x(0.3) ~ 7.0] - @mtkbuild lksys = System(eqs, t; constraints = constr) + @mtkcompile lksys = System(eqs, t; constraints = constr) bvp = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}( lksys, u0map, tspan; guesses = guess) @@ -185,13 +185,13 @@ end # Testing that more complicated constraints give correct solutions. constr = [y(0.2) + x(0.8) ~ 3.0, y(0.3) ~ 2.0] - @mtkbuild lksys = System(eqs, t; constraints = constr) + @mtkcompile lksys = System(eqs, t; constraints = constr) bvp = SciMLBase.BVProblem{false, SciMLBase.FullSpecialize}( lksys, u0map, tspan; guesses = guess) test_solvers(solvers, bvp, u0map, constr; dt = 0.05) constr = [α * β - x(0.6) ~ 0.0, y(0.2) ~ 3.0] - @mtkbuild lksys = System(eqs, t; constraints = constr) + @mtkcompile lksys = System(eqs, t; constraints = constr) bvp = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}( lksys, u0map, tspan; guesses = guess) test_solvers(solvers, bvp, u0map, constr) @@ -205,7 +205,7 @@ end # eqs = [D(D(x)) ~ λ * x # D(D(y)) ~ λ * y - g # x^2 + y^2 ~ 1] -# @mtkbuild pend = System(eqs, t) +# @mtkcompile pend = System(eqs, t) # # tspan = (0.0, 1.5) # u0map = [x => 1, y => 0] @@ -243,7 +243,7 @@ end # D(D(y)) ~ λ * y - g # x(t)^2 + y^2 ~ 1] # constr = [x(0.5) ~ 1] -# @mtkbuild pend = System(eqs, t; constr) +# @mtkcompile pend = System(eqs, t; constr) # # tspan = (0.0, 1.5) # u0map = [x(t) => 0.6, y => 0.8] @@ -262,13 +262,13 @@ end # # constr = [x(0.5) ~ 1, # x(0.3)^3 + y(0.6)^2 ~ 0.5] -# @mtkbuild pend = System(eqs, t; constr) +# @mtkcompile pend = System(eqs, t; constr) # bvp = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}(pend, u0map, tspan, parammap; guesses, check_length = false) # test_solvers(daesolvers, bvp, u0map, constr, get_alg_eqs(pend)) # # constr = [x(0.4) * g ~ y(0.2), # y(0.7) ~ 0.3] -# @mtkbuild pend = System(eqs, t; constr) +# @mtkcompile pend = System(eqs, t; constr) # bvp = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}(pend, u0map, tspan, parammap; guesses, check_length = false) # test_solvers(daesolvers, bvp, u0map, constr, get_alg_eqs(pend)) # end @@ -286,7 +286,7 @@ end parammap = [α => 7.5, β => 4, γ => 8.0, δ => 5.0] costs = [x(0.6), x(0.3)^2] consolidate(u, sub) = (u[1] + 3)^2 + u[2] + sum(sub; init = 0) - @mtkbuild lksys = System(eqs, t; costs, consolidate) + @mtkcompile lksys = System(eqs, t; costs, consolidate) @test_throws ModelingToolkit.SystemCompatibilityError ODEProblem( lksys, u0map, tspan, parammap) @@ -301,7 +301,7 @@ end @parameters t_c costs = [y(t_c) + x(0.0), x(0.4)^2] consolidate(u, sub) = log(u[1]) - u[2] + sum(sub; init = 0) - @mtkbuild lksys = System(eqs, t; costs, consolidate) + @mtkcompile lksys = System(eqs, t; costs, consolidate) @test t_c ∈ Set(parameters(lksys)) push!(parammap, t_c => 0.56) prob = ODEProblem(lksys, u0map, tspan, parammap; check_compatibility = false) diff --git a/test/clock.jl b/test/clock.jl index 3c5a32c1ce..80f7cd6af1 100644 --- a/test/clock.jl +++ b/test/clock.jl @@ -518,7 +518,7 @@ eqs = [yd ~ Sample(dt)(y) end end - @mtkbuild model = FirstOrderWithStepCounter() + @mtkcompile model = FirstOrderWithStepCounter() prob = ODEProblem(model, [], (0.0, 10.0)) sol = solve(prob, Tsit5(), kwargshandle = KeywordArgSilent) @@ -529,7 +529,7 @@ eqs = [yd ~ Sample(dt)(y) @variables x(t)=1.0 y(t)=1.0 eqs = [D(y) ~ Hold(x) x ~ x(k - 1) + x(k - 2)] - @mtkbuild sys = System(eqs, t) + @mtkcompile sys = System(eqs, t) prob = ODEProblem(sys, [], (0.0, 10.0)) int = init(prob, Tsit5(); kwargshandle = KeywordArgSilent) @test int.ps[x] == 2.0 diff --git a/test/code_generation.jl b/test/code_generation.jl index 0dc86871c5..b5caacbe9a 100644 --- a/test/code_generation.jl +++ b/test/code_generation.jl @@ -58,7 +58,7 @@ end @testset "Non-standard array variables" begin @variables x(t) @parameters p[0:2] (f::Function)(..) - @mtkbuild sys = System(D(x) ~ p[0] * x + p[1] * t + p[2] + f(p), t) + @mtkcompile sys = System(D(x) ~ p[0] * x + p[1] * t + p[2] + f(p), t) prob = ODEProblem(sys, [x => 1.0], (0.0, 1.0), [p => [1.0, 2.0, 3.0], f => sum]) @test prob.ps[p] == [1.0, 2.0, 3.0] @test prob.ps[p[0]] == 1.0 diff --git a/test/constants.jl b/test/constants.jl index bd28517ae6..49181faf57 100644 --- a/test/constants.jl +++ b/test/constants.jl @@ -41,7 +41,7 @@ simp = structural_simplify(sys) @variables x(MT.t_nounits) = h eqs = [MT.D_nounits(x) ~ (h - x) / τ] - @mtkbuild fol_model = System(eqs, MT.t_nounits) + @mtkcompile fol_model = System(eqs, MT.t_nounits) prob = ODEProblem(fol_model, [], (0.0, 10.0), [h => 1]) @test prob[x] ≈ 1 diff --git a/test/dde.jl b/test/dde.jl index cac207caa3..8f26b6c431 100644 --- a/test/dde.jl +++ b/test/dde.jl @@ -38,7 +38,7 @@ eqs = [D(x₀) ~ (v0 / (1 + beta0 * (x₂(t - tau)^2))) * (p0 - q0) * x₀ - d0 D(x₁) ~ (v0 / (1 + beta0 * (x₂(t - tau)^2))) * (1 - p0 + q0) * x₀ + (v1 / (1 + beta1 * (x₂(t - tau)^2))) * (p1 - q1) * x₁ - d1 * x₁ D(x₂(t)) ~ (v1 / (1 + beta1 * (x₂(t - tau)^2))) * (1 - p1 + q1) * x₁ - d2 * x₂(t)] -@mtkbuild sys = System(eqs, t) +@mtkcompile sys = System(eqs, t) @test ModelingToolkit.is_dde(sys) @test !is_markovian(sys) prob = DDEProblem(sys, @@ -81,7 +81,7 @@ sol = solve(prob, RKMil(), seed = 100) @brownian η τ = 1.0 eqs = [D(x(t)) ~ a * x(t) + b * x(t - τ) + c + (α * x(t) + γ) * η, delx ~ x(t - τ)] -@mtkbuild sys = System(eqs, t) +@mtkcompile sys = System(eqs, t) @test ModelingToolkit.has_observed_with_lhs(sys, delx) @test ModelingToolkit.is_dde(sys) @test !is_markovian(sys) @@ -162,7 +162,7 @@ prob_sa = DDEProblem(sys, [], (0.0, 10.0); constant_lags = [sys.osc1.τ, sys.osc return System([D(x) ~ dx], t; name = name) end - @mtkbuild ssys = System( + @mtkcompile ssys = System( Equation[], t; systems = [valve(name = :valve), veccy(name = :vvecs)]) prob = DDEProblem(ssys, [ssys.valve.opening => 1.0], (0.0, 1.0)) sol = solve(prob, MethodOfSteps(Tsit5())) diff --git a/test/debugging.jl b/test/debugging.jl index 615b452b38..6b4eeabca4 100644 --- a/test/debugging.jl +++ b/test/debugging.jl @@ -42,7 +42,7 @@ end (System, ODEProblem, inner_ode, Tsit5()), (System, SDEProblem, inner_sde, ImplicitEM())] kwargs = Problem == SDEProblem ? (; seed = SEED) : (;) - @mtkbuild outer = ctor(Equation[], t; systems = [inner]) + @mtkcompile outer = ctor(Equation[], t; systems = [inner]) dsys = debug_system(outer; functions = []) @test is_parameter(dsys, ASSERTION_LOG_VARIABLE) prob = Problem(dsys, [inner.x => 0.1], (0.0, 5.0); kwargs...) diff --git a/test/discrete_system.jl b/test/discrete_system.jl index 2d2682b07a..da02d7c287 100644 --- a/test/discrete_system.jl +++ b/test/discrete_system.jl @@ -9,7 +9,7 @@ using ModelingToolkit: t_nounits as t # Make sure positive shifts error @variables x(t) k = ShiftIndex(t) -@test_throws ErrorException @mtkbuild sys = System([x(k + 1) ~ x + x(k - 1)], t) +@test_throws ErrorException @mtkcompile sys = System([x(k + 1) ~ x + x(k - 1)], t) @inline function rate_to_proportion(r, t) 1 - exp(-r * t) @@ -72,7 +72,7 @@ eqs2 = [S ~ S(k - 1) - infection2, R ~ R(k - 1) + recovery2, R2 ~ R] -@mtkbuild sys = System( +@mtkcompile sys = System( eqs2, t, [S, I, R, R2], [c, nsteps, δt, β, γ]; controls = [β, γ], tspan) @test ModelingToolkit.defaults(sys) != Dict() @@ -198,7 +198,7 @@ RHS2 = RHS # Δ(us[i]) ~ dummy_identity(buffer[i], us[i]) # end -# @mtkbuild sys = System(eqs, t, us, ps; defaults = defs, preface = preface) +# @mtkcompile sys = System(eqs, t, us, ps; defaults = defs, preface = preface) # prob = DiscreteProblem(sys, [], (0.0, 1.0)) # sol = solve(prob, FunctionMap(); dt = dt) # @test c[1] + 1 == length(sol) @@ -208,7 +208,7 @@ RHS2 = RHS eqs = [u ~ 1 x ~ x(k - 1) + u y ~ x + u] -@mtkbuild de = System(eqs, t) +@mtkcompile de = System(eqs, t) prob = DiscreteProblem(de, [x(k - 1) => 0.0], (0, 10)) sol = solve(prob, FunctionMap()) @@ -243,7 +243,7 @@ function System(; name, buffer) System(eqs, t, vars, pars; systems = [y_sys], name = name) end -@test_nowarn @mtkbuild sys = System(; buffer = ones(10)) +@test_nowarn @mtkcompile sys = System(; buffer = ones(10)) # Ensure discrete systems with algebraic equations throw @variables x(t) y(t) @@ -253,7 +253,7 @@ k = ShiftIndex(t) @testset "Passing `nothing` to `u0`" begin @variables x(t) = 1 k = ShiftIndex() - @mtkbuild sys = System([x(k) ~ x(k - 1) + 1], t) + @mtkcompile sys = System([x(k) ~ x(k - 1) + 1], t) prob = @test_nowarn DiscreteProblem(sys, nothing, (0.0, 1.0)) @test_nowarn solve(prob, FunctionMap()) end @@ -261,7 +261,7 @@ end @testset "Initialization" begin # test that default values apply to the entire history @variables x(t) = 1.0 - @mtkbuild de = System([x ~ x(k - 1) + x(k - 2)], t) + @mtkcompile de = System([x ~ x(k - 1) + x(k - 2)], t) prob = DiscreteProblem(de, [], (0, 10)) @test prob[x] == 2.0 @test prob[x(k - 1)] == 1.0 @@ -284,14 +284,14 @@ end # Test missing initial throws error @variables x(t) - @mtkbuild de = System([x ~ x(k - 1) + x(k - 2) * x(k - 3)], t) + @mtkcompile de = System([x ~ x(k - 1) + x(k - 2) * x(k - 3)], t) @test_throws ErrorException prob=DiscreteProblem(de, [x(k - 3) => 2.0], (0, 10)) @test_throws ErrorException prob=DiscreteProblem( de, [x(k - 3) => 2.0, x(k - 1) => 3.0], (0, 10)) # Test non-assigned initials are given default value @variables x(t) = 2.0 - @mtkbuild de = System([x ~ x(k - 1) + x(k - 2) * x(k - 3)], t) + @mtkcompile de = System([x ~ x(k - 1) + x(k - 2) * x(k - 3)], t) prob = DiscreteProblem(de, [x(k - 3) => 12.0], (0, 10)) @test prob[x] == 26.0 @test prob[x(k - 1)] == 2.0 @@ -301,7 +301,7 @@ end @variables xₜ₋₂(t) zₜ₋₁(t) z(t) eqs = [x ~ x(k - 1) + z(k - 2), z ~ x(k - 2) * x(k - 3) - z(k - 1)^2] - @mtkbuild de = System(eqs, t) + @mtkcompile de = System(eqs, t) u0 = [x(k - 1) => 3, xₜ₋₂(k - 1) => 4, x(k - 2) => 1, @@ -328,7 +328,7 @@ end y[1](k) ~ y[1](k - 1) + y[1](k - 2), y[2](k) ~ y[2](k - 1) + y[2](k - 2) ] - @mtkbuild sys = System(eqs, t) + @mtkcompile sys = System(eqs, t) prob = DiscreteProblem(sys, [x(k - 1) => ones(2), x(k - 2) => zeros(2), y[1](k - 1) => 1.0, y[1](k - 2) => 0.0, y[2](k - 1) => 1.0, y[2](k - 2) => 0.0], diff --git a/test/dq_units.jl b/test/dq_units.jl index 5a635144f5..4689cab859 100644 --- a/test/dq_units.jl +++ b/test/dq_units.jl @@ -211,7 +211,7 @@ end x^2 + y^2 ~ L^2 end end - @mtkbuild pend = PendulumUnits() + @mtkcompile pend = PendulumUnits() u0 = [pend.x => 1.0, pend.y => 0.0] p = [pend.g => 1.0, pend.L => 1.0] guess = [pend.λ => 0.0] @@ -279,7 +279,7 @@ using DynamicQuantities end end -@mtkbuild pend = UnitsExample() +@mtkcompile pend = UnitsExample() @test ModelingToolkit.get_unit.(filter(x -> occursin("ˍt", string(x)), unknowns(pend))) == [u"m/s", u"m/s"] end diff --git a/test/extensions/ad.jl b/test/extensions/ad.jl index 8764200fac..23374bba43 100644 --- a/test/extensions/ad.jl +++ b/test/extensions/ad.jl @@ -21,7 +21,7 @@ u0 = [x => zeros(3), ps = [p => zeros(3, 3), q => 1.0] tspan = (0.0, 10.0) -@mtkbuild sys = System(eqs, t) +@mtkcompile sys = System(eqs, t) prob = ODEProblem(sys, u0, tspan, ps) sol = solve(prob, Tsit5()) @@ -129,7 +129,7 @@ end @testset "`sys.var` is non-differentiable" begin @variables x(t) - @mtkbuild sys = System(D(x) ~ x, t) + @mtkcompile sys = System(D(x) ~ x, t) prob = ODEProblem(sys, [x => 1.0], (0.0, 1.0)) grad = Zygote.gradient(prob) do prob diff --git a/test/extensions/bifurcationkit.jl b/test/extensions/bifurcationkit.jl index 7b95ffd82e..78758ae470 100644 --- a/test/extensions/bifurcationkit.jl +++ b/test/extensions/bifurcationkit.jl @@ -149,7 +149,7 @@ let end end - @mtkbuild fol = FOL() + @mtkcompile fol = FOL() par = [fol.τ => 0.0] u0 = [fol.x => -1.0] diff --git a/test/extensions/dynamic_optimization.jl b/test/extensions/dynamic_optimization.jl index 0d4e9d94e2..7893088e0e 100644 --- a/test/extensions/dynamic_optimization.jl +++ b/test/extensions/dynamic_optimization.jl @@ -20,7 +20,7 @@ const M = ModelingToolkit eqs = [D(x(t)) ~ α * x(t) - β * x(t) * y(t), D(y(t)) ~ -γ * y(t) + δ * x(t) * y(t)] - @mtkbuild sys = System(eqs, t) + @mtkcompile sys = System(eqs, t) tspan = (0.0, 1.0) u0map = [x(t) => 4.0, y(t) => 2.0] parammap = [α => 1.5, β => 1.0, γ => 3.0, δ => 1.0] @@ -53,7 +53,7 @@ const M = ModelingToolkit u0map = Pair[] guess = [x(t) => 4.0, y(t) => 2.0] constr = [x(0.6) ~ 3.5, x(0.3) ~ 7.0] - @mtkbuild lksys = System(eqs, t; constraints = constr) + @mtkcompile lksys = System(eqs, t; constraints = constr) jprob = JuMPDynamicOptProblem(lksys, u0map, tspan, parammap; guesses = guess, dt = 0.01) @test JuMP.num_constraints(jprob.model) == 2 @@ -77,7 +77,7 @@ const M = ModelingToolkit # Test whole-interval constraints constr = [x(t) ≳ 1, y(t) ≳ 1] - @mtkbuild lksys = System(eqs, t; constraints = constr) + @mtkcompile lksys = System(eqs, t; constraints = constr) iprob = InfiniteOptDynamicOptProblem( lksys, u0map, tspan, parammap; guesses = guess, dt = 0.01) isol = solve(iprob, Ipopt.Optimizer, @@ -139,7 +139,7 @@ end # Test dynamics @parameters (u_interp::ConstantInterpolation)(..) - @mtkbuild block_ode = System([D(x(t)) ~ v(t), D(v(t)) ~ u_interp(t)], t) + @mtkcompile block_ode = System([D(x(t)) ~ v(t), D(v(t)) ~ u_interp(t)], t) spline = ctrl_to_spline(jsol.input_sol, ConstantInterpolation) oprob = ODEProblem(block_ode, u0map, tspan, [u_interp => spline]) osol = solve(oprob, Vern8(), dt = 0.01, adaptive = false) @@ -183,7 +183,7 @@ end @parameters (α_interp::LinearInterpolation)(..) eqs = [D(w(t)) ~ -μ * w(t) + b * s * α_interp(t) * w(t), D(q(t)) ~ -ν * q(t) + c * (1 - α_interp(t)) * s * w(t)] - @mtkbuild beesys_ode = System(eqs, t) + @mtkcompile beesys_ode = System(eqs, t) oprob = ODEProblem(beesys_ode, u0map, tspan, @@ -237,7 +237,7 @@ end eqs = [D(h(t)) ~ v(t), D(v(t)) ~ (T_interp(t) - drag(h(t), v(t))) / m(t) - gravity(h(t)), D(m(t)) ~ -T_interp(t) / c] - @mtkbuild rocket_ode = System(eqs, t) + @mtkcompile rocket_ode = System(eqs, t) interpmap = Dict(T_interp => ctrl_to_spline(jsol.input_sol, CubicSpline)) oprob = ODEProblem(rocket_ode, u0map, (ts, te), merge(Dict(pmap), interpmap)) osol = solve(oprob, RadauIIA5(); adaptive = false, dt = 0.001) diff --git a/test/extensions/homotopy_continuation.jl b/test/extensions/homotopy_continuation.jl index 607c27346c..f9abeaa449 100644 --- a/test/extensions/homotopy_continuation.jl +++ b/test/extensions/homotopy_continuation.jl @@ -33,7 +33,7 @@ end eqs = [0 ~ x^2 + y^2 + 2x * y 0 ~ x^2 + 4x + 4 0 ~ y * z + 4x^2] - @mtkbuild sys = System(eqs) + @mtkcompile sys = System(eqs) u0 = [x => 1.0, y => 1.0, z => 1.0] prob = HomotopyContinuationProblem(sys, u0) @test prob isa NonlinearProblem @@ -60,7 +60,7 @@ end 0 ~ x^2 + 4x + q 0 ~ y * z + 4x^2 + wrapper(r)] - @mtkbuild sys = System(eqs) + @mtkcompile sys = System(eqs) prob = HomotopyContinuationProblem(sys, [x => 1.0, y => 1.0, z => 1.0], [p => 2.0, q => 4, r => Wrapper([1.0 1.0; 0.0 0.0])]) @test prob.ps[p] == 2.0 @@ -78,7 +78,7 @@ end @parameters p[1:3] _x = collect(x) eqs = collect(0 .~ vec(sum(_x * _x'; dims = 2)) + collect(p)) - @mtkbuild sys = System(eqs) + @mtkcompile sys = System(eqs) prob = HomotopyContinuationProblem(sys, [x => ones(3)], [p => 1:3]) @test prob[x] == ones(3) @test prob[p + x] == [2, 3, 4] @@ -93,7 +93,7 @@ end @testset "Parametric exponents" begin @variables x = 1.0 @parameters n::Integer = 4 - @mtkbuild sys = System([x^n + x^2 - 1 ~ 0]) + @mtkcompile sys = System([x^n + x^2 - 1 ~ 0]) prob = HomotopyContinuationProblem(sys, []) sol = solve(prob, singlerootalg) test_single_root(sol) @@ -103,34 +103,34 @@ end @testset "Polynomial check and warnings" begin @variables x = 1.0 - @mtkbuild sys = System([x^1.5 + x^2 - 1 ~ 0]) + @mtkcompile sys = System([x^1.5 + x^2 - 1 ~ 0]) @test_throws ["Cannot convert", "Unable", "symbolically solve", "Exponent", "not an integer", "not a polynomial"] HomotopyContinuationProblem( sys, []) - @mtkbuild sys = System([x^x - x ~ 0]) + @mtkcompile sys = System([x^x - x ~ 0]) @test_throws ["Cannot convert", "Unable", "symbolically solve", "Exponent", "unknowns", "not a polynomial"] HomotopyContinuationProblem( sys, []) - @mtkbuild sys = System([((x^2) / sin(x))^2 + x ~ 0]) + @mtkcompile sys = System([((x^2) / sin(x))^2 + x ~ 0]) @test_throws ["Cannot convert", "both polynomial", "non-polynomial", "recognized", "sin", "not a polynomial"] HomotopyContinuationProblem( sys, []) @variables y = 2.0 - @mtkbuild sys = System([x^2 + y^2 + 2 ~ 0, y ~ sin(x)]) + @mtkcompile sys = System([x^2 + y^2 + 2 ~ 0, y ~ sin(x)]) @test_throws ["Cannot convert", "recognized", "sin", "not a polynomial"] HomotopyContinuationProblem( sys, []) - @mtkbuild sys = System([x^2 + y^2 - 2 ~ 0, sin(x + y) ~ 0]) + @mtkcompile sys = System([x^2 + y^2 - 2 ~ 0, sin(x + y) ~ 0]) @test_throws ["Cannot convert", "function of multiple unknowns"] HomotopyContinuationProblem( sys, []) - @mtkbuild sys = System([sin(x)^2 + 1 ~ 0, cos(y) - cos(x) - 1 ~ 0]) + @mtkcompile sys = System([sin(x)^2 + 1 ~ 0, cos(y) - cos(x) - 1 ~ 0]) @test_throws ["Cannot convert", "multiple non-polynomial terms", "same unknown"] HomotopyContinuationProblem( sys, []) - @mtkbuild sys = System([sin(x^2)^2 + sin(x^2) - 1 ~ 0]) + @mtkcompile sys = System([sin(x^2)^2 + sin(x^2) - 1 ~ 0]) @test_throws ["import Nemo"] HomotopyContinuationProblem(sys, []) end @@ -138,7 +138,7 @@ import Nemo @testset "With Nemo" begin @variables x = 2.0 - @mtkbuild sys = System([sin(x^2)^2 + sin(x^2) - 1 ~ 0]) + @mtkcompile sys = System([sin(x^2)^2 + sin(x^2) - 1 ~ 0]) prob = HomotopyContinuationProblem(sys, []) @test prob[1] ≈ 2.0 # singlerootalg doesn't converge @@ -151,8 +151,8 @@ end @variables x=0.25 y=0.125 a = sin(x^2 - 4x + 1) b = cos(3log(y) + 4) - @mtkbuild sys = System([(a^2 - 5a * b + 6b^2) / (a - 0.25) ~ 0 - (a^2 - 0.75a + 0.125) ~ 0]) + @mtkcompile sys = System([(a^2 - 5a * b + 6b^2) / (a - 0.25) ~ 0 + (a^2 - 0.75a + 0.125) ~ 0]) prob = HomotopyContinuationProblem(sys, []) @test prob[x] ≈ 0.25 @test prob[y] ≈ 0.125 @@ -165,7 +165,7 @@ end @testset "Rational functions" begin @variables x=2.0 y=2.0 @parameters n = 5 - @mtkbuild sys = System([ + @mtkcompile sys = System([ 0 ~ (x^2 - n * x + 6) * (x - 1) / (x - 2) / (x - 3) ]) prob = HomotopyContinuationProblem(sys, []) @@ -209,7 +209,7 @@ end @testset "Rational function in observed" begin @variables x=1 y=1 - @mtkbuild sys = System([x^2 + y^2 - 2x - 2 ~ 0, y ~ (x - 1) / (x - 2)]) + @mtkcompile sys = System([x^2 + y^2 - 2x - 2 ~ 0, y ~ (x - 1) / (x - 2)]) prob = HomotopyContinuationProblem(sys, []) @test any(prob.f.denominator([2.0], parameter_values(prob)) .≈ 0.0) @test SciMLBase.successful_retcode(solve(prob, singlerootalg)) @@ -217,7 +217,7 @@ end @testset "Rational function forced to common denominators" begin @variables x = 1 - @mtkbuild sys = System([0 ~ 1 / (1 + x) - x]) + @mtkcompile sys = System([0 ~ 1 / (1 + x) - x]) prob = HomotopyContinuationProblem(sys, []) @test any(prob.f.denominator([-1.0], parameter_values(prob)) .≈ 0.0) sol = solve(prob, singlerootalg) @@ -228,7 +228,7 @@ end @testset "Non-polynomial observed not used in equations" begin @variables x=1 y - @mtkbuild sys = System([x^2 - 2 ~ 0, y ~ sin(x)]) + @mtkcompile sys = System([x^2 - 2 ~ 0, y ~ sin(x)]) prob = HomotopyContinuationProblem(sys, []) sol = @test_nowarn solve(prob, singlerootalg) @test sol[x] ≈ √2.0 diff --git a/test/fmi/fmi.jl b/test/fmi/fmi.jl index b2684b4524..29b2dc1224 100644 --- a/test/fmi/fmi.jl +++ b/test/fmi/fmi.jl @@ -17,7 +17,7 @@ const FMU_DIR = joinpath(@__DIR__, "fmus") end @testset "v2, ME" begin fmu = loadFMU("SpringPendulum1D", "Dymola", "2022x"; type = :ME) - @mtkbuild sys = MTK.FMIComponent(Val(2); fmu, type = :ME) + @mtkcompile sys = MTK.FMIComponent(Val(2); fmu, type = :ME) test_no_inputs_outputs(sys) prob = ODEProblem{true, SciMLBase.FullSpecialize}( sys, [sys.mass__s => 0.5, sys.mass__v => 0.0], (0.0, 8.0)) @@ -34,7 +34,7 @@ const FMU_DIR = joinpath(@__DIR__, "fmus") @named inner = MTK.FMIComponent( Val(2); fmu, communication_step_size = 1e-5, type = :CS) @variables x(t) = 1.0 - @mtkbuild sys = System([D(x) ~ x], t; systems = [inner]) + @mtkcompile sys = System([D(x) ~ x], t; systems = [inner]) test_no_inputs_outputs(sys) prob = ODEProblem{true, SciMLBase.FullSpecialize}( @@ -51,7 +51,7 @@ const FMU_DIR = joinpath(@__DIR__, "fmus") fmu, (0.0, 8.0); saveat = 0.0:0.1:8.0, recordValues = ["mass.s", "mass.v"]) @testset "v3, ME" begin fmu = loadFMU("SpringPendulum1D", "Dymola", "2023x", "3.0"; type = :ME) - @mtkbuild sys = MTK.FMIComponent(Val(3); fmu, type = :ME) + @mtkcompile sys = MTK.FMIComponent(Val(3); fmu, type = :ME) test_no_inputs_outputs(sys) prob = ODEProblem{true, SciMLBase.FullSpecialize}( sys, [sys.mass__s => 0.5, sys.mass__v => 0.0], (0.0, 8.0)) @@ -68,7 +68,7 @@ const FMU_DIR = joinpath(@__DIR__, "fmus") @named inner = MTK.FMIComponent( Val(3); fmu, communication_step_size = 1e-5, type = :CS) @variables x(t) = 1.0 - @mtkbuild sys = System([D(x) ~ x], t; systems = [inner]) + @mtkcompile sys = System([D(x) ~ x], t; systems = [inner]) test_no_inputs_outputs(sys) prob = ODEProblem{true, SciMLBase.FullSpecialize}( @@ -120,7 +120,7 @@ end @testset "IO Model" begin function build_simple_adder(adder) @variables a(t) b(t) c(t) [guess = 1.0] - @mtkbuild sys = System( + @mtkcompile sys = System( [adder.a ~ a, adder.b ~ b, D(a) ~ t, D(b) ~ adder.out + adder.c, c^2 ~ adder.out + adder.value], t; @@ -176,7 +176,7 @@ end function build_sspace_model(sspace) @variables u(t)=1.0 x(t)=1.0 y(t) [guess = 1.0] - @mtkbuild sys = System( + @mtkcompile sys = System( [sspace.u ~ u, D(u) ~ t, D(x) ~ sspace.x + sspace.y, y^2 ~ sspace.y + sspace.x], t; systems = [sspace] ) @@ -229,7 +229,7 @@ end @testset "FMUs in a loop" begin function build_looped_adders(adder1, adder2) @variables x(t) = 1 - @mtkbuild sys = System( + @mtkcompile sys = System( [D(x) ~ x, adder1.a ~ adder2.out2, adder2.a ~ adder1.out2, adder1.b ~ 1.0, adder2.b ~ 2.0], t; @@ -274,7 +274,7 @@ end function build_looped_sspace(sspace1, sspace2) @variables x(t) = 1 - @mtkbuild sys = System([D(x) ~ x, sspace1.u ~ sspace2.x, sspace2.u ~ sspace1.y], + @mtkcompile sys = System([D(x) ~ x, sspace1.u ~ sspace2.x, sspace2.u ~ sspace1.y], t; systems = [sspace1, sspace2]) prob = ODEProblem(sys, [sspace1.x => 1.0, sspace2.x => 1.0], (0.0, 1.0)) return sys, prob diff --git a/test/guess_propagation.jl b/test/guess_propagation.jl index f2131e28cb..20421346a5 100644 --- a/test/guess_propagation.jl +++ b/test/guess_propagation.jl @@ -80,7 +80,7 @@ sol = solve(prob.f.initializeprob; show_trace = Val(true)) @parameters x0 @variables x(t) @variables y(t) = x -@mtkbuild sys = System([x ~ x0, D(y) ~ x], t) +@mtkcompile sys = System([x ~ x0, D(y) ~ x], t) prob = ODEProblem(sys, [], (0.0, 1.0), [x0 => 1.0]) @test prob[x] == 1.0 @test prob[y] == 1.0 @@ -88,7 +88,7 @@ prob = ODEProblem(sys, [], (0.0, 1.0), [x0 => 1.0]) @parameters x0 @variables x(t) @variables y(t) = x0 -@mtkbuild sys = System([x ~ x0, D(y) ~ x], t) +@mtkcompile sys = System([x ~ x0, D(y) ~ x], t) prob = ODEProblem(sys, [], (0.0, 1.0), [x0 => 1.0]) @test prob[x] == 1.0 @test prob[y] == 1.0 @@ -96,7 +96,7 @@ prob = ODEProblem(sys, [], (0.0, 1.0), [x0 => 1.0]) @parameters x0 @variables x(t) @variables y(t) = x0 -@mtkbuild sys = System([x ~ y, D(y) ~ x], t) +@mtkcompile sys = System([x ~ y, D(y) ~ x], t) prob = ODEProblem(sys, [], (0.0, 1.0), [x0 => 1.0]) @test prob[x] == 1.0 @test prob[y] == 1.0 @@ -104,7 +104,7 @@ prob = ODEProblem(sys, [], (0.0, 1.0), [x0 => 1.0]) @parameters x0 @variables x(t) = x0 @variables y(t) = x -@mtkbuild sys = System([x ~ y, D(y) ~ x], t) +@mtkcompile sys = System([x ~ y, D(y) ~ x], t) prob = ODEProblem(sys, [], (0.0, 1.0), [x0 => 1.0]) @test prob[x] == 1.0 @test prob[y] == 1.0 diff --git a/test/hierarchical_initialization_eqs.jl b/test/hierarchical_initialization_eqs.jl index e9ea36947e..229c71f9b7 100644 --- a/test/hierarchical_initialization_eqs.jl +++ b/test/hierarchical_initialization_eqs.jl @@ -122,7 +122,7 @@ HTML as well. end """Run model RLCModel from 0 to 10""" function simple() - @mtkbuild model = RLCModel() + @mtkcompile model = RLCModel() u0 = [] prob = ODEProblem(model, u0, (0.0, 10.0)) sol = solve(prob) diff --git a/test/if_lifting.jl b/test/if_lifting.jl index 085dc600c8..c203df8f22 100644 --- a/test/if_lifting.jl +++ b/test/if_lifting.jl @@ -111,7 +111,7 @@ end end end -@testset "`@mtkbuild` macro accepts `additional_passes`" begin +@testset "`@mtkcompile` macro accepts `additional_passes`" begin @mtkmodel SimpleAbs begin @variables begin x(t) @@ -122,7 +122,7 @@ end y ~ sin(t) end end - @test_nowarn @mtkbuild sys=SimpleAbs() additional_passes=[IfLifting] + @test_nowarn @mtkcompile sys=SimpleAbs() additional_passes=[IfLifting] end @testset "Nested conditions are handled properly" begin @@ -144,8 +144,8 @@ end D(x) ~ y end end - @mtkbuild sys = RampModel() - @mtkbuild sys2=RampModel() additional_passes=[IfLifting] + @mtkcompile sys = RampModel() + @mtkcompile sys2=RampModel() additional_passes=[IfLifting] prob = ODEProblem(sys, [sys.x => 1.0], (0.0, 3.0)) prob2 = ODEProblem(sys2, [sys.x => 1.0], (0.0, 3.0)) sol = solve(prob) diff --git a/test/implicit_discrete_system.jl b/test/implicit_discrete_system.jl index 49ab2a5921..59e4a94977 100644 --- a/test/implicit_discrete_system.jl +++ b/test/implicit_discrete_system.jl @@ -7,7 +7,7 @@ rng = StableRNG(22525) @testset "Correct ImplicitDiscreteFunction" begin @variables x(t) = 1 - @mtkbuild sys = System([x(k) ~ x(k) * x(k - 1) - 3], t) + @mtkcompile sys = System([x(k) ~ x(k) * x(k - 1) - 3], t) tspan = (0, 10) # u[2] - u_next[1] @@ -27,7 +27,7 @@ rng = StableRNG(22525) prob = ImplicitDiscreteProblem(sys, [], tspan) @test prob.u0 == [1.0, 1.0] @variables x(t) - @mtkbuild sys = System([x(k) ~ x(k) * x(k - 1) - 3], t) + @mtkcompile sys = System([x(k) ~ x(k) * x(k - 1) - 3], t) @test_throws ModelingToolkit.MissingVariablesError prob=ImplicitDiscreteProblem( sys, [], tspan) end @@ -36,7 +36,7 @@ end @variables x(t) y(t) eqs = [x(k) ~ x(k - 1) + x(k - 2), x^2 ~ 1 - y^2] - @mtkbuild sys = System(eqs, t) + @mtkcompile sys = System(eqs, t) f = ImplicitDiscreteFunction(sys) function correct_f(u_next, u, p, t) @@ -63,7 +63,7 @@ end eqs = [x(k) ~ x(k - 1) + x(k - 2), y(k) ~ x(k) + x(k - 2) * z(k - 1), x + y + z ~ 2] - @mtkbuild sys = System(eqs, t) + @mtkcompile sys = System(eqs, t) @test length(unknowns(sys)) == length(equations(sys)) == 3 @test occursin( "var\"y(t)\"", string(ImplicitDiscreteFunction(sys; expression = Val{true}))) @@ -72,7 +72,7 @@ end eqs = [z(k) ~ x(k) + sin(x(k)), y(k) ~ x(k - 1) + x(k - 2), z(k) * x(k) ~ 3] - @mtkbuild sys = System(eqs, t) + @mtkcompile sys = System(eqs, t) @test occursin("var\"Shift(t, 1)(z(t))\"", string(ImplicitDiscreteFunction(sys; expression = Val{true}))) end diff --git a/test/initial_values.jl b/test/initial_values.jl index cc75c2e41e..347f794e8d 100644 --- a/test/initial_values.jl +++ b/test/initial_values.jl @@ -7,13 +7,13 @@ using SymbolicIndexingInterface @variables x(t)[1:3]=[1.0, 2.0, 3.0] y(t) z(t)[1:2] -@mtkbuild sys=System([D(x) ~ t * x], t) simplify=false +@mtkcompile sys=System([D(x) ~ t * x], t) simplify=false @test get_u0(sys, [])[1] == [1.0, 2.0, 3.0] @test get_u0(sys, [x => [2.0, 3.0, 4.0]])[1] == [2.0, 3.0, 4.0] @test get_u0(sys, [x[1] => 2.0, x[2] => 3.0, x[3] => 4.0])[1] == [2.0, 3.0, 4.0] @test get_u0(sys, [2.0, 3.0, 4.0])[1] == [2.0, 3.0, 4.0] -@mtkbuild sys=System([ +@mtkcompile sys=System([ D(x) ~ 3x, D(y) ~ t, D(z[1]) ~ z[2] + t, @@ -56,7 +56,7 @@ vals = ModelingToolkit.varmap_to_vars(var_vals, desired_values; defaults = defau @parameters k1 k2 Γ[1:1]=X1 + X2 eq = D(X1) ~ -k1 * X1 + k2 * (-X1 + Γ[1]) obs = X2 ~ Γ[1] - X1 -@mtkbuild osys_m = System([eq], t, [X1], [k1, k2, Γ[1]]; observed = [X2 ~ Γ[1] - X1]) +@mtkcompile osys_m = System([eq], t, [X1], [k1, k2, Γ[1]]; observed = [X2 ~ Γ[1] - X1]) # Creates ODEProblem. u0 = [X1 => 1.0, X2 => 2.0] @@ -77,14 +77,14 @@ target_varmap = Dict(p => ones(3), q => 2ones(3), q[1] => 2.0, q[2] => 2.0, q[3] # Issue#1283 @variables z(t)[1:2, 1:2] eqs = [D(D(z)) ~ ones(2, 2)] -@mtkbuild sys = System(eqs, t) +@mtkcompile sys = System(eqs, t) @test_nowarn ODEProblem(sys, [z => zeros(2, 2), D(z) => ones(2, 2)], (0.0, 10.0)) # Initialization with defaults involving parameters that are not part of the system # Issue#2817 @parameters A1 A2 B1 B2 @variables x1(t) x2(t) -@mtkbuild sys = System( +@mtkcompile sys = System( [ x1 ~ B1, x2 ~ B2 @@ -108,14 +108,14 @@ end # Issue#2799 @variables x(t) @parameters p -@mtkbuild sys = System([D(x) ~ p], t; defaults = [x => t, p => 2t]) +@mtkcompile sys = System([D(x) ~ p], t; defaults = [x => t, p => 2t]) prob = ODEProblem(sys, [], (1.0, 2.0), []) @test prob[x] == 1.0 @test prob.ps[p] == 2.0 @testset "Array of symbolics is unwrapped" begin @variables x(t)[1:2] y(t) - @mtkbuild sys = System([D(x) ~ x, D(y) ~ t], t; defaults = [x => [y, 3.0]]) + @mtkcompile sys = System([D(x) ~ x, D(y) ~ t], t; defaults = [x => [y, 3.0]]) prob = ODEProblem(sys, [y => 1.0], (0.0, 1.0)) @test eltype(prob.u0) <: Float64 prob = ODEProblem(sys, [x => [y, 4.0], y => 2.0], (0.0, 1.0)) @@ -125,7 +125,7 @@ end @testset "split=false systems with all parameter defaults" begin @variables x(t) = 1.0 @parameters p=1.0 q=2.0 r=3.0 - @mtkbuild sys=System(D(x) ~ p * x + q * t + r, t) split=false + @mtkcompile sys=System(D(x) ~ p * x + q * t + r, t) split=false prob = @test_nowarn ODEProblem(sys, [], (0.0, 1.0)) @test prob.p isa Vector{Float64} end @@ -137,7 +137,7 @@ end y ~ ifelse(t < c1, 0.0, (-c1 + t)^(c3))] sps = [x, y] ps = [c1, c2, c3] - @mtkbuild osys = System(eqs, t, sps, ps) + @mtkcompile osys = System(eqs, t, sps, ps) u0map = [x => 1.0] pmap = [c1 => 5.0, c2 => 1.0, c3 => 1.2] oprob = ODEProblem(osys, u0map, (0.0, 10.0), pmap) @@ -145,7 +145,7 @@ end @testset "Cyclic dependency checking and substitution limits" begin @variables x(t) y(t) - @mtkbuild sys = System( + @mtkcompile sys = System( [D(x) ~ x, D(y) ~ y], t; initialization_eqs = [x ~ 2y + 3, y ~ 2x], guesses = [x => 2y, y => 2x]) @test_warn ["Cycle", "unknowns", "x", "y"] try @@ -156,7 +156,7 @@ end sys, [x => 2y + 1, y => 2x], (0.0, 1.0); build_initializeprob = false) @parameters p q - @mtkbuild sys = System( + @mtkcompile sys = System( [D(x) ~ x * p, D(y) ~ y * q], t; guesses = [p => 1.0, q => 2.0]) # "unknowns" because they are initialization unknowns @test_warn ["Cycle", "unknowns", "p", "q"] try @@ -171,7 +171,7 @@ end @testset "`add_fallbacks!` checks scalarized array parameters correctly" begin @variables x(t)[1:2] @parameters p[1:2, 1:2] - @mtkbuild sys = System(D(x) ~ p * x, t) + @mtkcompile sys = System(D(x) ~ p * x, t) # used to throw a `MethodError` complaining about `getindex(::Nothing, ::CartesianIndex{2})` @test_throws ModelingToolkit.MissingParametersError ODEProblem( sys, [x => ones(2)], (0.0, 1.0)) @@ -185,7 +185,7 @@ end y[1] ~ x[3], y[2] ~ x[4] ] - @mtkbuild sys = System(eqs, t; defaults = [x => vcat(ones(2), y), y => x[1:2] ./ 2]) + @mtkcompile sys = System(eqs, t; defaults = [x => vcat(ones(2), y), y => x[1:2] ./ 2]) prob = ODEProblem(sys, [], (0.0, 1.0)) sol = solve(prob) @test SciMLBase.successful_retcode(sol) @@ -198,7 +198,7 @@ end eqs = [D(D(x)) ~ λ * x D(D(y)) ~ λ * y - g x^2 + y^2 ~ 1] - @mtkbuild pend = System(eqs, t) + @mtkcompile pend = System(eqs, t) @test_throws ModelingToolkit.MissingGuessError ODEProblem( pend, [x => 1], (0, 1), [g => 1], guesses = [y => λ, λ => y + 1]) @@ -207,7 +207,7 @@ end # 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 = System(eqs, t) + @mtkcompile sys = System(eqs, t) @test_throws ["a(t)", "c(t)"] ODEProblem( sys, [e => 2, a => b, b => a + 1, c => d, d => c + 1], (0, 1)) end @@ -219,7 +219,7 @@ end @variables x(t) @parameters (interp::Tspline)(..) - @mtkbuild sys = System(D(x) ~ interp(t), t) + @mtkcompile sys = System(D(x) ~ interp(t), t) prob = ODEProblem(sys, [x => 0.0], (0.0, 1.0), [interp => spline]) spline2 = LinearInterpolation(ts .^ 2, ts .^ 2) @@ -248,7 +248,7 @@ end @parameters p d @variables X(t) eqs = [D(X) ~ p - d * X] - @mtkbuild osys = System(eqs, t) + @mtkcompile osys = System(eqs, t) u0 = [X => 1.0f0] ps = [p => 1.0f0, d => 2.0f0] oprob = ODEProblem(osys, u0, (0.0f0, 1.0f0), ps) @@ -260,7 +260,7 @@ end @testset "Array initials and scalar parameters with `split = false`" begin @variables x(t)[1:2] @parameters p - @mtkbuild sys=System([D(x[1]) ~ x[1], D(x[2]) ~ x[2] + p], t) split=false + @mtkcompile sys=System([D(x[1]) ~ x[1], D(x[2]) ~ x[2] + p], t) split=false ps = Set(parameters(sys; initial_parameters = true)) @test length(ps) == 5 for i in 1:2 @@ -284,7 +284,7 @@ end 0 ~ x^2 + y^2 - w2^2 ] - @mtkbuild sys = System(eqs, t) + @mtkcompile sys = System(eqs, t) u0 = [D(x) => 2.0, x => 1.0, @@ -309,7 +309,7 @@ end D(y) ~ x * (ρ - z) - y, D(z) ~ x * y - β * z] - @mtkbuild sys = System(eqs, t) + @mtkcompile sys = System(eqs, t) u0 = SA[D(x) => 2.0f0, x => 1.0f0, @@ -332,7 +332,7 @@ end eqs = [D(D(x)) ~ λ * x D(D(y)) ~ λ * y - g x^2 + y^2 ~ 1] - @mtkbuild pend = System(eqs, t) + @mtkcompile pend = System(eqs, t) u0 = [x => 1.0, D(x) => 0.0] u0_constructor = p_constructor = vals -> SVector{length(vals)}(vals...) @@ -345,7 +345,7 @@ end @test state_values(initdata.initializeprob) isa SVector @test parameter_values(initdata.initializeprob).tunable isa SVector - @mtkbuild pend=System(eqs, t) split=false + @mtkcompile pend=System(eqs, t) split=false prob = ODEProblem(pend, u0, tspan; u0_constructor, p_constructor) @test prob.p isa SVector initdata = prob.f.initialization_data diff --git a/test/initializationsystem.jl b/test/initializationsystem.jl index cca4be1f76..2c4f0f8bf6 100644 --- a/test/initializationsystem.jl +++ b/test/initializationsystem.jl @@ -11,7 +11,7 @@ using DynamicQuantities eqs = [D(D(x)) ~ λ * x D(D(y)) ~ λ * y - g x^2 + y^2 ~ 1] -@mtkbuild pend = System(eqs, t) +@mtkcompile pend = System(eqs, t) initprob = ModelingToolkit.InitializationProblem(pend, 0.0, [], [g => 1]; guesses = [ModelingToolkit.missing_variable_defaults(pend); x => 1; y => 0.2]) @@ -235,7 +235,7 @@ end end end -@mtkbuild sys = HydraulicSystem() +@mtkcompile sys = HydraulicSystem() initprob = ModelingToolkit.InitializationProblem(sys, 0.0) conditions = getfield.(equations(initprob.f.sys), :rhs) @@ -323,7 +323,7 @@ end end end -@mtkbuild sys = MassDamperSystem() +@mtkcompile sys = MassDamperSystem() initprob = ModelingToolkit.InitializationProblem(sys, 0.0) @test initprob isa NonlinearProblem initsol = solve(initprob, reltol = 1e-12, abstol = 1e-12) @@ -348,7 +348,7 @@ eqs = [D(D(x)) ~ σ * (y - x), D(y) ~ x * (ρ - z) - y, D(z) ~ x * y - β * z] -@mtkbuild sys = System(eqs, t) +@mtkcompile sys = System(eqs, t) u0 = [D(x) => 2.0, y => 0.0, @@ -378,7 +378,7 @@ function System2(; name) return System(eqs, t, vars, []; name) end -@mtkbuild sys = System2() +@mtkcompile sys = System2() prob = ODEProblem(sys, [sys.dx => 1], (0, 1)) # OK prob = ODEProblem(sys, [sys.ddx => -2], (0, 1), guesses = [sys.dx => 1]) sol = solve(prob, Tsit5()) @@ -400,7 +400,7 @@ function System3(; name) return System(eqs, t, vars, []; name, initialization_eqs) end -@mtkbuild sys = System3() +@mtkcompile sys = System3() prob = ODEProblem(sys, [], (0, 1), guesses = [sys.dx => 1]) sol = solve(prob, Tsit5()) @test SciMLBase.successful_retcode(sol) @@ -472,7 +472,7 @@ sol = solve(prob, Tsit5()) eqs = [D(D(x)) ~ λ * x D(D(y)) ~ λ * y - g x^2 + y^2 ~ 1] -@mtkbuild pend = System(eqs, t) +@mtkcompile pend = System(eqs, t) prob = ODEProblem(pend, [x => 1], (0.0, 1.5), [g => 1], guesses = [λ => 0, y => 1], initialization_eqs = [y ~ 1]) @@ -556,8 +556,8 @@ eqs_1st_order = [D(Y) + Y - ω ~ 0, X + k1 ~ Y + k2] eqs_2nd_order = [D(D(Y)) + 2ω * D(Y) + (ω^2) * Y ~ 0, X + k1 ~ Y + k2] -@mtkbuild sys_1st_order = System(eqs_1st_order, t) -@mtkbuild sys_2nd_order = System(eqs_2nd_order, t) +@mtkcompile sys_1st_order = System(eqs_1st_order, t) +@mtkcompile sys_2nd_order = System(eqs_2nd_order, t) u0_1st_order_1 = [X => 1.0, Y => 2.0] u0_1st_order_2 = [Y => 2.0] @@ -595,7 +595,7 @@ end @brownian a b x = _x(t) sarray_ctor = splat(SVector) - # `System` constructor creates appropriate type with mtkbuild + # `System` constructor creates appropriate type with mtkcompile # `Problem` and `alg` create the problem to test and allow calling `init` with # the correct solver. # `rhss` allows adding terms to the end of equations (only 2 equations allowed) to influence @@ -635,7 +635,7 @@ end pmap = Dict() pmap[q] = 1.0 # `missing` default, equation from Problem - @mtkbuild sys = System( + @mtkcompile sys = System( [D(x) ~ x * q + rhss[1], D(y) ~ y * p + rhss[2]], t; defaults = [p => missing], guesses = [p => 1.0]) pmap[p] = 2q prob = Problem(sys, u0map, (0.0, 1.0), pmap; u0_constructor, p_constructor) @@ -644,7 +644,7 @@ end prob2 = remake(prob2; p = setp_oop(prob2, p)(prob2, 0.0)) test_parameter(prob2, p, 2.0) # `missing` default, provided guess - @mtkbuild sys = System( + @mtkcompile sys = System( [D(x) ~ x + rhss[1], p ~ x + y + rhss[2]], t; defaults = [p => missing], guesses = [p => 0.0]) prob = Problem(sys, u0map, (0.0, 1.0); u0_constructor, p_constructor) test_parameter(prob, p, 2.0) @@ -654,7 +654,7 @@ end test_parameter(prob2, p, 2.0) # `missing` to Problem, equation from default - @mtkbuild sys = System( + @mtkcompile sys = System( [D(x) ~ x * q + rhss[1], D(y) ~ y * p + rhss[2]], t; defaults = [p => 2q], guesses = [p => 1.0]) pmap[p] = missing prob = Problem(sys, u0map, (0.0, 1.0), pmap; u0_constructor, p_constructor) @@ -664,7 +664,7 @@ end prob2 = remake(prob2; p = setp_oop(prob2, p)(prob2, 0.0)) test_parameter(prob2, p, 2.0) # `missing` to Problem, provided guess - @mtkbuild sys = System( + @mtkcompile sys = System( [D(x) ~ x + rhss[1], p ~ x + y + rhss[2]], t; guesses = [p => 0.0]) prob = Problem(sys, u0map, (0.0, 1.0), pmap; u0_constructor, p_constructor) test_parameter(prob, p, 2.0) @@ -674,7 +674,7 @@ end test_parameter(prob2, p, 2.0) # No `missing`, default and guess - @mtkbuild sys = System( + @mtkcompile sys = System( [D(x) ~ x * q + rhss[1], D(y) ~ y * p + rhss[2]], t; defaults = [p => 2q], guesses = [p => 0.0]) delete!(pmap, p) prob = Problem(sys, u0map, (0.0, 1.0), pmap; u0_constructor, p_constructor) @@ -685,14 +685,14 @@ end test_parameter(prob2, p, 2.0) # Default overridden by Problem, guess provided - @mtkbuild sys = System( + @mtkcompile sys = System( [D(x) ~ q * x + rhss[1], D(y) ~ y * p + rhss[2]], t; defaults = [p => 2q], guesses = [p => 1.0]) _pmap = merge(pmap, Dict(p => q)) prob = Problem(sys, u0map, (0.0, 1.0), _pmap; u0_constructor, p_constructor) test_parameter(prob, p, _pmap[q]) test_initializesystem(prob, p, p ~ q) # Problem dependent value with guess, no `missing` - @mtkbuild sys = System( + @mtkcompile sys = System( [D(x) ~ y * q + p + rhss[1], D(y) ~ x * p + q + rhss[2]], t; guesses = [p => 0.0]) _pmap = merge(pmap, Dict(p => 3q)) prob = Problem(sys, u0map, (0.0, 1.0), _pmap; u0_constructor, p_constructor) @@ -700,7 +700,7 @@ end # Should not be solved for: # Override dependent default with direct value - @mtkbuild sys = System( + @mtkcompile sys = System( [D(x) ~ q * x + rhss[1], D(y) ~ y * p + rhss[2]], t; defaults = [p => 2q], guesses = [p => 1.0]) _pmap = merge(pmap, Dict(p => 1.0)) prob = Problem(sys, u0map, (0.0, 1.0), _pmap; u0_constructor, p_constructor) @@ -710,7 +710,7 @@ end # Non-floating point @parameters r::Int s::Int - @mtkbuild sys = System( + @mtkcompile sys = System( [D(x) ~ s * x + rhss[1], D(y) ~ y * r + rhss[2]], t; defaults = [s => 2r], guesses = [s => 1.0]) prob = Problem(sys, u0map, (0.0, 1.0), [r => 1]; u0_constructor, p_constructor) @test prob.ps[r] == 1 @@ -719,7 +719,7 @@ end @test is_parameter(initsys, r) @test is_parameter(initsys, s) - @mtkbuild sys = System( + @mtkcompile sys = System( [D(x) ~ x + rhss[1], p ~ x + y + rhss[2]], t; guesses = [p => 0.0]) @test_throws ModelingToolkit.MissingParametersError Problem( sys, [x => 1.0, y => 1.0], (0.0, 1.0)) @@ -736,7 +736,7 @@ end @testset "Null system" begin @variables x(t) y(t) s(t) @parameters x0 y0 - @mtkbuild sys = System([x ~ x0, y ~ y0, s ~ x + y], t; guesses = [y0 => 0.0]) + @mtkcompile sys = System([x ~ x0, y ~ y0, s ~ x + y], t; guesses = [y0 => 0.0]) prob = ODEProblem(sys, [s => 1.0], (0.0, 1.0), [x0 => 0.3, y0 => missing]) # trivial initialization run immediately @test prob.ps[y0] ≈ 0.7 @@ -756,7 +756,7 @@ end @named gravity = Force() @named constant = Constant(; k = 9.81) @named damper = TM.Damper(; d = 0.1) - @mtkbuild sys = System( + @mtkcompile sys = System( [connect(fixed.flange, spring.flange_a), connect(spring.flange_b, mass.flange_a), connect(mass.flange_a, gravity.flange), connect(constant.output, gravity.f), connect(fixed.flange, damper.flange_a), connect(damper.flange_b, mass.flange_a)], @@ -782,7 +782,7 @@ end eqs = [0 ~ σ * (y - x), 0 ~ x * (ρ - z) - y, 0 ~ x * y - β * z] - @mtkbuild ns = System(eqs, [x, y, z], [σ, ρ, β]) + @mtkcompile ns = System(eqs, [x, y, z], [σ, ρ, β]) prob = NonlinearProblem(ns, []) @test prob.f.initialization_data.update_initializeprob! === nothing @@ -834,7 +834,7 @@ end 0 ~ p * (-x + (q - z) * x)] @named sys = System(eqs; initialization_eqs = [p^2 + q^2 + 2p * q ~ 0]) sys = complete(sys) - # @mtkbuild sys = NonlinearSystem( + # @mtkcompile sys = NonlinearSystem( # [p * x^2 + q * y^3 ~ 0, x - q ~ 0]; defaults = [q => missing], # guesses = [q => 1.0], initialization_eqs = [p^2 + q^2 + 2p * q ~ 0]) @@ -889,7 +889,7 @@ end (ModelingToolkit.System, DDEProblem, MethodOfSteps(Tsit5()), [_x(t - 0.1), 0.0]), (ModelingToolkit.System, SDDEProblem, ImplicitEM(), [_x(t - 0.1) + a, b]) ] - @mtkbuild sys = System( + @mtkcompile sys = System( [D(x) ~ x + rhss[1], p ~ x + y + rhss[2]], t; guesses = [x => 0.0, p => 0.0]) prob = Problem(sys, [y => 1.0], (0.0, 1.0), [p => 3.0]) @test prob.f.initialization_data.initializeprob.ps[p] ≈ 3.0 @@ -918,7 +918,7 @@ end (ModelingToolkit.System, DDEProblem, MethodOfSteps(Tsit5()), _x(t - 0.1)), (ModelingToolkit.System, SDDEProblem, ImplicitEM(), _x(t - 0.1) + a) ] - @mtkbuild sys = System( + @mtkcompile sys = System( [D(x) ~ 2x + r + rhss], t; parameter_dependencies = [r ~ p + 2q, q ~ p + 3], guesses = [p => 1.0]) prob = Problem(sys, [x => 1.0], (0.0, 1.0), [p => missing]) @@ -941,7 +941,7 @@ end (DDEProblem, MethodOfSteps(Tsit5()), [_x(t - 0.1), 0.0]), (SDDEProblem, ImplicitEM(), [_x(t - 0.1) + a, b]) ] - @mtkbuild sys = System( + @mtkcompile sys = System( [D(x) ~ x + rhss[1], p ~ x + y + rhss[2]], t; defaults = [p => missing], guesses = [ x => 0.0, p => 0.0]) prob = Problem(sys, [x => 1.0, y => 1.0], (0.0, 1.0)) @@ -975,7 +975,7 @@ end ] alge_eqs = [y^2 * q + q^2 * x ~ 0, z * p - p^2 * x * z ~ 0] - @mtkbuild sys = System( + @mtkcompile sys = System( [D(x) ~ x * p + y^2 * q + rhss; alge_eqs], t; guesses = [x => 0.0, y => 0.0, z => 0.0, p => 0.0, q => 0.0]) prob = Problem(sys, [x => 1.0], (0.0, 1.0), [p => 1.0, q => missing]) @@ -1031,7 +1031,7 @@ end (ModelingToolkit.System, SDDEProblem, ImplicitEM(), _x(t - 0.1) + a) ] alge_eqs = [y^2 + 4y * p^2 ~ x^3] - @mtkbuild sys = System( + @mtkcompile sys = System( [D(x) ~ x + p * y^2 + rhss; alge_eqs], t; guesses = [ y => 1.0, p => 1.0]) prob = Problem(sys, [x => 1.0], (0.0, 1.0), [p => 1.0]) @@ -1060,7 +1060,7 @@ end @testset "Nonnumeric parameter dependencies are retained" begin @variables x(t) y(t) @parameters foo(::Real, ::Real) p - @mtkbuild sys = System([D(x) ~ t, 0 ~ foo(x, y)], t; + @mtkcompile sys = System([D(x) ~ t, 0 ~ foo(x, y)], t; parameter_dependencies = [foo ~ Multiplier(p, 2p)], guesses = [y => -1.0]) prob = ODEProblem(sys, [x => 1.0], (0.0, 1.0), [p => 1.0]) integ = init(prob, Rosenbrock23()) @@ -1069,7 +1069,7 @@ end @testset "Use observed equations for guesses of observed variables" begin @variables x(t) y(t) [state_priority = 100] - @mtkbuild sys = System( + @mtkcompile sys = System( [D(x) ~ x + t, y ~ 2x + 1], t; initialization_eqs = [x^3 + y^3 ~ 1]) isys = ModelingToolkit.generate_initializesystem(sys) @test isequal(defaults(isys)[y], 2x + 1) @@ -1077,14 +1077,14 @@ end @testset "Create initializeprob when unknown has dependent value" begin @variables x(t) y(t) - @mtkbuild sys = System([D(x) ~ x, D(y) ~ t * y], t; defaults = [x => 2y]) + @mtkcompile sys = System([D(x) ~ x, D(y) ~ t * y], t; defaults = [x => 2y]) prob = ODEProblem(sys, [y => 1.0], (0.0, 1.0)) @test prob.f.initializeprob !== nothing integ = init(prob) @test integ[x] ≈ 2.0 @variables x(t)[1:2] y(t) - @mtkbuild sys = System([D(x) ~ x, D(y) ~ t], t; defaults = [x => [y, 3.0]]) + @mtkcompile sys = System([D(x) ~ x, D(y) ~ t], t; defaults = [x => [y, 3.0]]) prob = ODEProblem(sys, [y => 1.0], (0.0, 1.0)) @test prob.f.initializeprob !== nothing integ = init(prob) @@ -1099,7 +1099,7 @@ end eqs = [D(D(x)) ~ λ * x D(D(y)) ~ λ * y - g x^2 + y^2 ~ L] - @mtkbuild pend = System(eqs, t) + @mtkcompile pend = System(eqs, t) prob = ODEProblem(pend, [x => 1, y => 0], (0.0, 1.5), [g => 1], guesses = ModelingToolkit.missing_variable_defaults(pend)) @@ -1183,7 +1183,7 @@ end eqs = [D(D(x)) ~ λ * x D(D(y)) ~ λ * y - g x^2 + y^2 ~ 1] - @mtkbuild pend = System(eqs, t) + @mtkcompile pend = System(eqs, t) @test_warn ["structurally singular", "initialization", "Guess", "heuristic"] ODEProblem( pend, [x => 1, y => 0], (0.0, 1.5), [g => 1], guesses = [λ => 1]) end @@ -1191,7 +1191,7 @@ end @testset "DAEProblem initialization" begin @variables x(t) [guess = 1.0] y(t) [guess = 1.0] @parameters p=missing [guess = 1.0] q=missing [guess = 1.0] - @mtkbuild sys = System( + @mtkcompile sys = System( [D(x) ~ p * y + q * t, x^3 + y^3 ~ 5], t; initialization_eqs = [p^2 + q^3 ~ 3]) # FIXME: solve for du0 @@ -1208,7 +1208,7 @@ end @testset "Guesses provided to `ODEProblem` are used in `remake`" begin @variables x(t) y(t)=2x @parameters p q=3x - @mtkbuild sys = System([D(x) ~ x * p + q, x^3 + y^3 ~ 3], t) + @mtkcompile sys = System([D(x) ~ x * p + q, x^3 + y^3 ~ 3], t) prob = ODEProblem( sys, [], (0.0, 1.0), [p => 1.0]; guesses = [x => 1.0, y => 1.0, q => 1.0]) @test prob[x] == 1.0 @@ -1238,7 +1238,7 @@ end @testset "Remake problem with no initializeprob" begin @variables x(t) [guess = 1.0] y(t) [guess = 1.0] @parameters p [guess = 1.0] q [guess = 1.0] - @mtkbuild sys = System( + @mtkcompile sys = System( [D(x) ~ p * x + q * y, y ~ 2x], t; parameter_dependencies = [q ~ 2p]) prob = ODEProblem(sys, [x => 1.0], (0.0, 1.0), [p => 1.0]) test_dummy_initialization_equation(prob, x) @@ -1259,7 +1259,7 @@ end @testset "Variables provided as symbols" begin @variables x(t) [guess = 1.0] y(t) [guess = 1.0] @parameters p [guess = 1.0] q [guess = 1.0] - @mtkbuild sys = System( + @mtkcompile sys = System( [D(x) ~ p * x + q * y, y ~ 2x], t; parameter_dependencies = [q ~ 2p]) prob = ODEProblem(sys, [:x => 1.0], (0.0, 1.0), [p => 1.0]) test_dummy_initialization_equation(prob, x) @@ -1294,7 +1294,7 @@ end D(X) ~ p - d * X, D(Y) ~ p - d * Y ] - @mtkbuild osys = System(eqs, t) + @mtkcompile osys = System(eqs, t) # Make problem. u0_vals = [X => 4, Y => 5.0] @@ -1321,7 +1321,7 @@ end j₁ = ConstantRateJump(rate₁, affect₁) j₂ = ConstantRateJump(rate₂, affect₂) j₃ = MassActionJump(2 * β + γ, [R => 1], [S => 1, R => -1]) - @mtkbuild js = JumpSystem([j₁, j₂, j₃], t, [S, I, R], [β, γ, S0]) + @mtkcompile js = JumpSystem([j₁, j₂, j₃], t, [S, I, R], [β, γ, S0]) u0s = [I => 1, R => 0] ps = [S0 => 999, β => 0.01, γ => 0.001] @@ -1335,7 +1335,7 @@ end @testset "Solvable array parameters with scalarized guesses" begin @variables x(t) @parameters p[1:2] q - @mtkbuild sys = System( + @mtkcompile sys = System( D(x) ~ p[1] + p[2] + q, t; defaults = [p[1] => q, p[2] => 2q], guesses = [p[1] => q, p[2] => 2q]) @test ModelingToolkit.is_parameter_solvable(p, Dict(), defaults(sys), guesses(sys)) @@ -1349,7 +1349,7 @@ end @testset "Issue#3318: Mutating `Initial` parameters works" begin @variables x(t) y(t)[1:2] [guess = ones(2)] @parameters p[1:2, 1:2] - @mtkbuild sys = System( + @mtkcompile sys = System( [D(x) ~ x, D(y) ~ p * y], t; initialization_eqs = [x^2 + y[1]^2 + y[2]^2 ~ 4]) prob = ODEProblem(sys, [x => 1.0, y[1] => 1], (0.0, 1.0), [p => 2ones(2, 2)]) integ = init(prob, Tsit5()) @@ -1390,7 +1390,7 @@ end @testset "Issue#3330: Initialization for unsimplified systems" begin @variables x(t) [guess = 1.0] - @mtkbuild sys = System(D(x) ~ x, t; initialization_eqs = [x^2 ~ 4]) + @mtkcompile sys = System(D(x) ~ x, t; initialization_eqs = [x^2 ~ 4]) prob = ODEProblem(sys, [], (0.0, 1.0)) @test prob.f.initialization_data !== nothing end @@ -1398,7 +1398,7 @@ end @testset "`ReconstructInitializeprob` with `nothing` state" begin @parameters p @variables x(t) - @mtkbuild sys = System(x ~ p * t, t) + @mtkcompile sys = System(x ~ p * t, t) prob = @test_nowarn ODEProblem(sys, [], (0.0, 1.0), [p => 1.0]) @test_nowarn remake(prob, p = [p => 1.0]) @test_nowarn remake(prob, p = [p => ForwardDiff.Dual(1.0)]) @@ -1412,7 +1412,7 @@ end D(X1) ~ k1 * (Γ[1] - X1) - k2 * X1 ] obs = [X2 ~ Γ[1] - X1] - @mtkbuild osys = System(eqs, t, [X1, X2], [k1, k2, Γ]; observed = obs) + @mtkcompile osys = System(eqs, t, [X1, X2], [k1, k2, Γ]; observed = obs) u0 = [X1 => 1.0, X2 => 2.0] ps = [k1 => 0.1, k2 => 0.2] @@ -1433,14 +1433,14 @@ end (DDEProblem, D, _x(t - 0.1)), (SDDEProblem, D, _x(t - 0.1) + a) ] - @mtkbuild sys = ModelingToolkit.System([lhs(x) ~ x + rhs, x + y ~ tot], t; + @mtkcompile sys = ModelingToolkit.System([lhs(x) ~ x + rhs, x + y ~ tot], t; guesses = [tot => 1.0], defaults = [tot => missing]) prob = Problem(sys, [x => 1.0, y => 1.0], (0.0, 1.0)) @test prob.ps[tot] ≈ 2.0 end @testset "$Problem" for Problem in [NonlinearProblem, NonlinearLeastSquaresProblem] @parameters p1 p2 - @mtkbuild sys = System([x^2 + y^2 ~ p1, (x - 1)^2 + (y - 1)^2 ~ p2]; + @mtkcompile sys = System([x^2 + y^2 ~ p1, (x - 1)^2 + (y - 1)^2 ~ p2]; parameter_dependencies = [p2 ~ 2p1], guesses = [p1 => 0.0], defaults = [p1 => missing]) prob = Problem(sys, [x => 1.0, y => 1.0], [p2 => 6.0]) @@ -1459,7 +1459,7 @@ end initialization_eqs = [ X2 ~ Γ[1] - X1 ] - @mtkbuild nlsys = System(eqs, [X1, X2], [k1, k2, Γ]; initialization_eqs) + @mtkcompile nlsys = System(eqs, [X1, X2], [k1, k2, Γ]; initialization_eqs) @testset "throws if initialization_eqs contain unknowns" begin u0 = [X1 => 1.0, X2 => 2.0] @@ -1472,7 +1472,7 @@ end initialization_eqs = [ Initial(X2) ~ Γ[1] - Initial(X1) ] - @mtkbuild nlsys = System(eqs, [X1, X2], [k1, k2, Γ]; initialization_eqs) + @mtkcompile nlsys = System(eqs, [X1, X2], [k1, k2, Γ]; initialization_eqs) @testset "solves initialization" begin u0 = [X1 => 1.0, X2 => 2.0] @@ -1510,7 +1510,7 @@ end @testset "Issue#3504: Update initials when `remake` called with non-symbolic `u0`" begin @variables x(t) y(t) @parameters c1 c2 - @mtkbuild sys = System([D(x) ~ -c1 * x + c2 * y, D(y) ~ c1 * x - c2 * y], t) + @mtkcompile sys = System([D(x) ~ -c1 * x + c2 * y, D(y) ~ c1 * x - c2 * y], t) prob1 = ODEProblem(sys, [1.0, 2.0], (0.0, 1.0), [c1 => 1.0, c2 => 2.0]) prob2 = remake(prob1, u0 = [2.0, 3.0]) prob3 = remake(prob1, u0 = [2.0, 3.0], p = [c1 => 2.0]) @@ -1539,7 +1539,7 @@ end eqs = [D(D(x)) ~ λ * x D(D(y)) ~ λ * y - g x^2 + y^2 ~ 1] - @mtkbuild pend = System(eqs, t) + @mtkcompile pend = System(eqs, t) prob = ODEProblem( pend, [x => (√2 / 2), D(x) => 0.0], (0.0, 1.5), @@ -1594,7 +1594,7 @@ end 0 ~ x^2 + y^2 - w2^2 ] - @mtkbuild sys = System(eqs, t) + @mtkcompile sys = System(eqs, t) u0 = [D(x) => 2.0, x => 1.0, @@ -1625,7 +1625,7 @@ end eqs = [D(D(x)) ~ λ * x D(D(y)) ~ λ * y - g x^2 + y^2 ~ 1] - @mtkbuild pend=System(eqs, t) split=false + @mtkcompile pend=System(eqs, t) split=false prob = ODEProblem(pend, [x => 1.0, D(x) => 0.0], (0.0, 1.0), [g => 1.0]; guesses = [y => 1.0, λ => 1.0]) @test !ModelingToolkit.is_split(prob.f.initialization_data.initializeprob.f.sys) @@ -1637,7 +1637,7 @@ end eqs = [D(D(x)) ~ λ * x D(D(y)) ~ λ * y - g x^2 + y^2 ~ 1] - @mtkbuild pend = System(eqs, t) + @mtkcompile pend = System(eqs, t) prob = ODEProblem(pend, SA[x => 1.0, D(x) => 0.0], (0.0, 1.0), SA[g => 1.0]; guesses = [y => 1.0, λ => 1.0]) @test !SciMLBase.isinplace(prob) diff --git a/test/input_output_handling.jl b/test/input_output_handling.jl index 1f14bc3814..ec71c3e9ff 100644 --- a/test/input_output_handling.jl +++ b/test/input_output_handling.jl @@ -447,7 +447,7 @@ end @constants c = 2.0 @variables x(t) eqs = [D(x) ~ c * x] - @mtkbuild sys = System(eqs, t, [x], [c]) + @mtkcompile sys = System(eqs, t, [x], [c]) f, dvs, ps, io_sys = ModelingToolkit.generate_control_function(sys) @test f[1]([0.5], nothing, MTKParameters(io_sys, []), 0.0) ≈ [1.0] diff --git a/test/jacobiansparsity.jl b/test/jacobiansparsity.jl index d10ec66c44..56e8edc183 100644 --- a/test/jacobiansparsity.jl +++ b/test/jacobiansparsity.jl @@ -91,7 +91,7 @@ prob = ODEProblem(sys, u0, (0, 11.5), sparse = true, jac = true) eqs = [D(D(x)) ~ λ * x D(D(y)) ~ λ * y - g x^2 + y^2 ~ 1] - @mtkbuild pend = System(eqs, t) + @mtkcompile pend = System(eqs, t) u0 = [x => 1, y => 0] prob = ODEProblem( @@ -125,7 +125,7 @@ end eqs = [D(D(x)) ~ λ * x D(D(y)) ~ λ * y - g x^2 + y^2 ~ 1] - @mtkbuild pend = System(eqs, t) + @mtkcompile pend = System(eqs, t) prob = ODEProblem(pend, [x => 0.0, D(x) => 1.0], (0.0, 1.0), [g => 1.0]; guesses = [y => 1.0, λ => 1.0], jac = true, sparse = true) J = deepcopy(prob.f.jac_prototype) diff --git a/test/jumpsystem.jl b/test/jumpsystem.jl index b48b17ce60..fca3d1a981 100644 --- a/test/jumpsystem.jl +++ b/test/jumpsystem.jl @@ -279,7 +279,7 @@ ps = [k => 1.0] @test_nowarn jp3 = JumpProblem(js3, u0, tspan, ps; aggregator = Direct()) @test_nowarn jp4 = JumpProblem(js4, u0, tspan; aggregator = Direct()) -# Ensure `structural_simplify` (and `@mtkbuild`) works on JumpSystem (by doing nothing) +# Ensure `structural_simplify` (and `@mtkcompile`) works on JumpSystem (by doing nothing) # Issue#2558 @parameters k @variables X(t) @@ -287,7 +287,7 @@ rate = k affect = [X ~ Pre(X) - 1] j1 = ConstantRateJump(k, [X ~ Pre(X) - 1]) -@test_nowarn @mtkbuild js1 = JumpSystem([j1], t, [X], [k]) +@test_nowarn @mtkcompile js1 = JumpSystem([j1], t, [X], [k]) # test correct autosolver is selected, which implies appropriate dep graphs are available @testset "Autosolver test" begin @@ -550,7 +550,7 @@ end j2 = ConstantRateJump(rate2, affect2) # Works. - @mtkbuild js = JumpSystem([j1, j2], t, [X], [p, d]) + @mtkcompile js = JumpSystem([j1, j2], t, [X], [p, d]) jprob = JumpProblem( js, [X => 15], (0.0, 10.0), [p => 2.0, d => 0.5]; aggregator = Direct()) sol = solve(jprob, SSAStepper()) diff --git a/test/model_parsing.jl b/test/model_parsing.jl index 88fa7faac7..1da6ac6928 100644 --- a/test/model_parsing.jl +++ b/test/model_parsing.jl @@ -152,7 +152,7 @@ end C_val = 20u"F" R_val = 20u"Ω" res__R = 100u"Ω" -@mtkbuild rc = RC(; C_val, R_val, resistor.R = res__R) +@mtkcompile rc = RC(; C_val, R_val, resistor.R = res__R) prob = ODEProblem(rc, [], (0, 1e9)) sol = solve(prob) defs = ModelingToolkit.defaults(rc) @@ -488,7 +488,7 @@ using ModelingToolkit: D_nounits end end - @mtkbuild model = M() + @mtkcompile model = M() u0 = [model.x => 10, model.y => 0, model.z => 0] prob = ODEProblem(model, u0, (0, 5.0)) diff --git a/test/modelingtoolkitize.jl b/test/modelingtoolkitize.jl index 0157a50acb..837f2fabb6 100644 --- a/test/modelingtoolkitize.jl +++ b/test/modelingtoolkitize.jl @@ -373,7 +373,7 @@ sys = modelingtoolkitize(prob) @testset "ODE" begin @variables x(t)=1.0 y(t)=2.0 @parameters p=3.0 q=4.0 - @mtkbuild sys = System([D(x) ~ p * y, D(y) ~ q * x], t) + @mtkcompile sys = System([D(x) ~ p * y, D(y) ~ q * x], t) prob1 = ODEProblem(sys, [], (0.0, 5.0)) newsys = complete(modelingtoolkitize(prob1)) @test is_variable(newsys, newsys.x) @@ -389,7 +389,7 @@ sys = modelingtoolkitize(prob) @testset "Nonlinear" begin @variables x=1.0 y=2.0 @parameters p=3.0 q=4.0 - @mtkbuild nlsys = System([0 ~ p * y^2 + x, 0 ~ x + exp(x) * q]) + @mtkcompile nlsys = System([0 ~ p * y^2 + x, 0 ~ x + exp(x) * q]) prob1 = NonlinearProblem(nlsys, []) newsys = complete(modelingtoolkitize(prob1)) @test is_variable(newsys, newsys.x) @@ -409,7 +409,7 @@ sys = modelingtoolkitize(prob) end @parameters p=3.0 q=4.0 loss = (p - x)^2 + q * (y - x^2)^2 - @mtkbuild optsys = OptimizationSystem(loss, [x, y], [p, q]) + @mtkcompile optsys = OptimizationSystem(loss, [x, y], [p, q]) prob1 = OptimizationProblem(optsys, [], grad = true, hess = true) newsys = complete(modelingtoolkitize(prob1)) @test is_variable(newsys, newsys.x) diff --git a/test/mtkparameters.jl b/test/mtkparameters.jl index 347043d437..23e4e577b9 100644 --- a/test/mtkparameters.jl +++ b/test/mtkparameters.jl @@ -140,7 +140,7 @@ end @parameters p::Vector{Float64} @variables X(t) eq = D(X) ~ p[1] - p[2] * X -@mtkbuild osys = System([eq], t) +@mtkcompile osys = System([eq], t) u0 = [X => 1.0] ps = [p => [2.0, 0.1]] @@ -160,7 +160,7 @@ newps = remake_buffer(sys, ps, (p,), (1.0f0,)) @parameters p d @variables X(t) eqs = [D(X) ~ p - d * X] -@mtkbuild sys = System(eqs, t) +@mtkcompile sys = System(eqs, t) u0 = [X => 1.0] tspan = (0.0, 100.0) @@ -248,7 +248,7 @@ end @variables x(t) y(t) eqs = [D(x) ~ (α - β * y) * x D(y) ~ (δ * x - γ) * y] -@mtkbuild odesys = System(eqs, t) +@mtkcompile odesys = System(eqs, t) odeprob = ODEProblem( odesys, [x => 1.0, y => 1.0], (0.0, 10.0), [α => 1.5, β => 1.0, γ => 3.0, δ => 1.0]) tunables, _... = canonicalize(Tunable(), odeprob.p) @@ -323,7 +323,7 @@ end D(V[1]) ~ k[1] - k[2] * V[1], D(V[2]) ~ k[3] - k[4] * V[2] ] - @mtkbuild osys_scal = System(eqs, t, [V[1], V[2]], [k[1], k[2], k[3], k[4]]) + @mtkcompile osys_scal = System(eqs, t, [V[1], V[2]], [k[1], k[2], k[3], k[4]]) u0 = [V => [10.0, 20.0]] ps_vec = [k => [2.0, 3.0, 4.0, 5.0]] diff --git a/test/nonlinearsystem.jl b/test/nonlinearsystem.jl index d7c8c15f6c..9580d1ea0a 100644 --- a/test/nonlinearsystem.jl +++ b/test/nonlinearsystem.jl @@ -270,7 +270,7 @@ sys = structural_simplify(ns; conservative = true) 0 ~ x * y - β * z] guesses = [x => 1.0, z => 0.0] ps = [σ => 10.0, ρ => 26.0, β => 8 / 3] - @mtkbuild ns = System(eqs) + @mtkcompile ns = System(eqs) @test isequal(calculate_jacobian(ns), [(-1 - z + ρ)*σ -x*σ 2x*(-z + ρ) -β-(x^2)]) @@ -287,7 +287,7 @@ sys = structural_simplify(ns; conservative = true) # system that contains a chain of observed variables when simplified @variables x y z eqs = [0 ~ x^2 + 2z + y, z ~ y, y ~ x] # analytical solution x = y = z = 0 or -3 - @mtkbuild ns = System(eqs) # solve for y with observed chain z -> y -> x + @mtkcompile ns = System(eqs) # solve for y with observed chain z -> y -> x @test isequal(expand.(calculate_jacobian(ns)), [-3 // 2 - x;;]) @test isequal(calculate_hessian(ns), [[-1;;]]) prob = NonlinearProblem(ns, unknowns(ns) .=> -4.0) # give guess < -3 to reach -3 @@ -297,7 +297,7 @@ end @testset "Passing `nothing` to `u0`" begin @variables x = 1 - @mtkbuild sys = System([0 ~ x^2 - x^3 + 3]) + @mtkcompile sys = System([0 ~ x^2 - x^3 + 3]) prob = @test_nowarn NonlinearProblem(sys, nothing) @test_nowarn solve(prob) end @@ -348,7 +348,7 @@ end end @variables y - @mtkbuild sys = System([0 ~ x * x - p * x + p, 0 ~ x * y + p]) + @mtkcompile sys = System([0 ~ x * x - p * x + p, 0 ~ x * y + p]) @test_throws ["single unknown"] IntervalNonlinearProblem(sys, (0.0, 1.0)) @test_throws ["single unknown"] IntervalNonlinearFunction(sys) @test_throws ["single unknown"] IntervalNonlinearProblem( @@ -361,7 +361,7 @@ end @variables x y @parameters p[1:2] (f::Function)(..) - @mtkbuild sys = System([x^2 - p[1]^2 ~ 0, y^2 ~ f(p)]) + @mtkcompile sys = System([x^2 - p[1]^2 ~ 0, y^2 ~ f(p)]) @test !any(isequal(p[1]), parameters(sys)) @test is_parameter(sys, p) end diff --git a/test/odesystem.jl b/test/odesystem.jl index ec3e52fe3a..b4884bb925 100644 --- a/test/odesystem.jl +++ b/test/odesystem.jl @@ -453,7 +453,7 @@ foo(a, ms::AbstractVector) = a + sum(ms) eqs = [D(x) ~ foo(x, ms); D(ms) ~ ones(3)] @named sys = System(eqs, t, [x; ms], []) @named emptysys = System(Equation[], t) -@mtkbuild outersys = compose(emptysys, sys) +@mtkcompile outersys = compose(emptysys, sys) prob = ODEProblem( outersys, [outersys.sys.x => 1.0; collect(outersys.sys.ms .=> 1:3)], (0, 1.0)) @test_nowarn solve(prob, Tsit5()) @@ -468,7 +468,7 @@ end eqs = [D(x) ~ foo(x, ms); D(ms) ~ bar(ms, p)] @named sys = System(eqs, t) @named emptysys = System(Equation[], t) -@mtkbuild outersys = compose(emptysys, sys) +@mtkcompile outersys = compose(emptysys, sys) prob = ODEProblem( outersys, [sys.x => 1.0, sys.ms => 1:3], (0.0, 1.0), [sys.p => ones(3, 3)]) @test_nowarn solve(prob, Tsit5()) @@ -862,7 +862,7 @@ end # Issue#2599 @variables x(t) y(t) eqs = [D(x) ~ x * t, y ~ 2x] -@mtkbuild sys = System(eqs, t; continuous_events = [[y ~ 3] => [x ~ 2]]) +@mtkcompile sys = System(eqs, t; continuous_events = [[y ~ 3] => [x ~ 2]]) prob = ODEProblem(sys, [x => 1.0], (0.0, 10.0)) @test_nowarn solve(prob, Tsit5()) @@ -873,14 +873,14 @@ prob = ODEProblem(sys, [x => 1.0], (0.0, 10.0)) eqs = [ D(x) ~ p * x ] - @mtkbuild sys = System( + @mtkcompile sys = System( eqs, t; continuous_events = [[norm(x) ~ 3.0] => [x ~ ones(3)]]) # array affect equations used to not work prob1 = @test_nowarn ODEProblem(sys, [x => ones(3)], (0.0, 10.0), [p => ones(3, 3)]) sol1 = @test_nowarn solve(prob1, Tsit5()) # array condition equations also used to not work - @mtkbuild sys = System( + @mtkcompile sys = System( eqs, t; continuous_events = [[x ~ sqrt(3) * ones(3)] => [x ~ ones(3)]]) # array affect equations used to not work prob2 = @test_nowarn ODEProblem(sys, [x => ones(3)], (0.0, 10.0), [p => ones(3, 3)]) @@ -893,7 +893,7 @@ end @test_skip begin @variables x(t)[1:3] y(t) @parameters p[1:3, 1:3] - @test_nowarn @mtkbuild sys = System([D(x) ~ p * x, D(y) ~ x' * p * x], t) + @test_nowarn @mtkcompile sys = System([D(x) ~ p * x, D(y) ~ x' * p * x], t) @test_nowarn ODEProblem(sys, [x => ones(3), y => 2], (0.0, 10.0), [p => ones(3, 3)]) end @@ -917,7 +917,7 @@ eqs = [D(D(x)) ~ σ * (y - x), D(y) ~ x * (ρ - z) - y, D(z) ~ x * y - β * z] -@mtkbuild sys = System(eqs, t) +@mtkcompile sys = System(eqs, t) u0 = [D(x) => 2.0, x => 1.0, @@ -952,7 +952,7 @@ function FML2(; name) System(eqs, t; systems, name) end -@mtkbuild model = FML2() +@mtkcompile model = FML2() @test isequal(ModelingToolkit.defaults(model)[model.constant.k], model.k2[1]) @test_nowarn ODEProblem(model, [], (0.0, 10.0)) @@ -1083,7 +1083,7 @@ end y ~ 0 end end - @test_nowarn @mtkbuild sys = MyModel() + @test_nowarn @mtkcompile sys = MyModel() @variables x y(x) @test_logs (:warn,) @named sys = System([y ~ 0], x) @@ -1273,7 +1273,7 @@ end @testset "Passing `nothing` to `u0`" begin @variables x(t) = 1 - @mtkbuild sys = System(D(x) ~ t, t) + @mtkcompile sys = System(D(x) ~ t, t) prob = @test_nowarn ODEProblem(sys, nothing, (0.0, 1.0)) @test_nowarn solve(prob) end @@ -1335,7 +1335,7 @@ end @testset "Inplace observed" begin @variables x(t) @parameters p[1:2] q - @mtkbuild sys = System(D(x) ~ sum(p) * x + q * t, t) + @mtkcompile sys = System(D(x) ~ sum(p) * x + q * t, t) prob = ODEProblem(sys, [x => 1.0], (0.0, 1.0), [p => ones(2), q => 2]) obsfn = ModelingToolkit.build_explicit_observed_function( sys, [p..., q], return_inplace = true)[2] @@ -1375,7 +1375,7 @@ end @testset "`complete` with `split = false` removes the index cache" begin @variables x(t) @parameters p - @mtkbuild sys = System(D(x) ~ p * t, t) + @mtkcompile sys = System(D(x) ~ p * t, t) @test ModelingToolkit.get_index_cache(sys) !== nothing sys2 = complete(sys; split = false) @test ModelingToolkit.get_index_cache(sys2) === nothing @@ -1385,7 +1385,7 @@ end @testset "Observed variables dependent on discrete parameters" begin @variables x(t) obs(t) @parameters c(t) - @mtkbuild sys = System([D(x) ~ c * cos(x), obs ~ c], + @mtkcompile sys = System([D(x) ~ c * cos(x), obs ~ c], t, [x, obs], [c]; @@ -1399,7 +1399,7 @@ end @testset "DAEProblem with array parameters" begin @variables x(t)=1.0 y(t) [guess = 1.0] @parameters p[1:2] = [1.0, 2.0] - @mtkbuild sys = System([D(x) ~ x, y^2 ~ x + sum(p)], t) + @mtkcompile sys = System([D(x) ~ x, y^2 ~ x + sum(p)], t) prob = DAEProblem(sys, [D(x) => x, D(y) => D(x) / 2y], [], (0.0, 1.0)) sol = solve(prob, DFBDF(), abstol = 1e-8, reltol = 1e-8) @test sol[x]≈sol[y^2 - sum(p)] atol=1e-5 @@ -1408,7 +1408,7 @@ end @testset "Symbolic tstops" begin @variables x(t) = 1.0 @parameters p=0.15 q=0.25 r[1:2]=[0.35, 0.45] - @mtkbuild sys = System( + @mtkcompile sys = System( [D(x) ~ p * x + q * t + sum(r)], t; tstops = [0.5p, [0.1, 0.2], [p + 2q], r]) prob = ODEProblem(sys, [], (0.0, 5.0)) sol = solve(prob) @@ -1420,7 +1420,7 @@ end @test all(x -> any(isapprox(x, atol = 1e-6), sol2.t), expected_tstops) @variables y(t) [guess = 1.0] - @mtkbuild sys = System([D(x) ~ p * x + q * t + sum(r), y^3 ~ 2x + 1], + @mtkcompile sys = System([D(x) ~ p * x + q * t + sum(r), y^3 ~ 2x + 1], t; tstops = [0.5p, [0.1, 0.2], [p + 2q], r]) prob = DAEProblem( sys, [D(y) => 2D(x) / 3y^2, D(x) => p * x + q * t + sum(r)], [], (0.0, 5.0)) @@ -1437,11 +1437,11 @@ end @parameters p d @variables X(t)::Int64 eq = D(X) ~ p - d * X - @test_throws ModelingToolkit.ContinuousOperatorDiscreteArgumentError @mtkbuild osys = System( + @test_throws ModelingToolkit.ContinuousOperatorDiscreteArgumentError @mtkcompile osys = System( [eq], t) @variables Y(t)[1:3]::String eq = D(Y) ~ [p, p, p] - @test_throws ModelingToolkit.ContinuousOperatorDiscreteArgumentError @mtkbuild osys = System( + @test_throws ModelingToolkit.ContinuousOperatorDiscreteArgumentError @mtkcompile osys = System( [eq], t) @variables X(t)::Complex @@ -1483,32 +1483,32 @@ end cons = [x(0.3) ~ c * d, y(0.7) ~ 3] # Test variables + parameters infer correctly. - @mtkbuild sys = System(eqs, t; constraints = cons) + @mtkcompile sys = System(eqs, t; constraints = cons) @test issetequal(parameters(sys), [a, c, d, e]) @test issetequal(unknowns(sys), [x(t), y(t), z(t)]) @parameters t_c cons = [x(t_c) ~ 3] - @mtkbuild sys = System(eqs, t; constraints = cons) + @mtkcompile sys = System(eqs, t; constraints = cons) @test issetequal(parameters(sys), [a, e, t_c]) @parameters g(..) h i cons = [g(h, i) * x(3) ~ c] - @mtkbuild sys = System(eqs, t; constraints = cons) + @mtkcompile sys = System(eqs, t; constraints = cons) @test issetequal(parameters(sys), [g, h, i, a, e, c]) # Test that bad constraints throw errors. cons = [x(3, 4) ~ 3] # unknowns cannot have multiple args. - @test_throws ArgumentError @mtkbuild sys = System(eqs, t; constraints = cons) + @test_throws ArgumentError @mtkcompile sys = System(eqs, t; constraints = cons) cons = [x(y(t)) ~ 2] # unknown arg must be parameter, value, or t - @test_throws ArgumentError @mtkbuild sys = System(eqs, t; constraints = cons) + @test_throws ArgumentError @mtkcompile sys = System(eqs, t; constraints = cons) @variables u(t) v cons = [x(t) * u ~ 3] - @test_throws ArgumentError @mtkbuild sys = System(eqs, t; constraints = cons) + @test_throws ArgumentError @mtkcompile sys = System(eqs, t; constraints = cons) cons = [x(t) * v ~ 3] - @test_throws ArgumentError @mtkbuild sys = System(eqs, t; constraints = cons) # Need time argument. + @test_throws ArgumentError @mtkcompile sys = System(eqs, t; constraints = cons) # Need time argument. # Test array variables @variables x(..)[1:5] @@ -1519,13 +1519,13 @@ end 0 0 2 0 5] eqs = D(x(t)) ~ mat * x(t) cons = [x(3) ~ [2, 3, 3, 5, 4]] - @mtkbuild ode = System(D(x(t)) ~ mat * x(t), t; constraints = cons) + @mtkcompile ode = System(D(x(t)) ~ mat * x(t), t; constraints = cons) @test length(constraints(ode)) == 1 end @testset "`build_explicit_observed_function` with `expression = true` returns `Expr`" begin @variables x(t) - @mtkbuild sys = System(D(x) ~ 2x, t) + @mtkcompile sys = System(D(x) ~ 2x, t) obsfn_expr = ModelingToolkit.build_explicit_observed_function( sys, 2x + 1, expression = true) @test obsfn_expr isa Expr @@ -1543,7 +1543,7 @@ end D(y) ~ x * (ρ - z) - y, D(z) ~ x * y - β * z] - @mtkbuild sys=System(eqs, t) split=false + @mtkcompile sys=System(eqs, t) split=false u0 = SA[D(x) => 2.0f0, x => 1.0f0, @@ -1579,6 +1579,6 @@ end @named subsys = SysB(; var1 = x) return System(D(x) ~ x, t; systems = [subsys], name) end - @mtkbuild sys = SysC() + @mtkcompile sys = SysC() @test length(unknowns(sys)) == 3 end diff --git a/test/optimizationsystem.jl b/test/optimizationsystem.jl index 099d8346ba..46c796ed8d 100644 --- a/test/optimizationsystem.jl +++ b/test/optimizationsystem.jl @@ -323,7 +323,7 @@ end @testset "Passing `nothing` to `u0`" begin @variables x = 1.0 - @mtkbuild sys = OptimizationSystem((x - 3)^2, [x], []) + @mtkcompile sys = OptimizationSystem((x - 3)^2, [x], []) prob = @test_nowarn OptimizationProblem(sys, nothing) @test_nowarn solve(prob, NelderMead()) end @@ -335,14 +335,14 @@ end obj = x^2 + y^2 + z^2 cons = [y ~ 2x z ~ 2y] - @mtkbuild sys = OptimizationSystem(obj, [x, y, z], []; constraints = cons) + @mtkcompile sys = OptimizationSystem(obj, [x, y, z], []; constraints = cons) @test is_variable(sys, z) @test !is_variable(sys, y) @variables x[1:3] [bounds = ([-Inf, -1.0, -2.0], [Inf, 1.0, 2.0])] obj = x[1]^2 + x[2]^2 + x[3]^2 cons = [x[2] ~ 2x[1] + 3, x[3] ~ x[1] + x[2]] - @mtkbuild sys = OptimizationSystem(obj, [x], []; constraints = cons) + @mtkcompile sys = OptimizationSystem(obj, [x], []; constraints = cons) @test length(unknowns(sys)) == 2 @test !is_variable(sys, x[1]) @test is_variable(sys, x[2]) @@ -352,7 +352,7 @@ end @testset "Constraints work with nonnumeric parameters" begin @variables x @parameters p f(::Real) - @mtkbuild sys = OptimizationSystem( + @mtkcompile sys = OptimizationSystem( x^2 + f(x) * p, [x], [f, p]; constraints = [2.0 ≲ f(x) + p]) prob = OptimizationProblem(sys, [x => 1.0], [p => 1.0, f => (x -> 2x)]) @test abs(prob.f.cons(prob.u0, prob.p)[1]) ≈ 1.0 diff --git a/test/parameter_dependencies.jl b/test/parameter_dependencies.jl index 1ea796507d..d4c5e3e2a1 100644 --- a/test/parameter_dependencies.jl +++ b/test/parameter_dependencies.jl @@ -20,7 +20,7 @@ using NonlinearSolve cb2 = [x ~ 4.0] => (f = affect1!, observed = (; p2), modified = (; p1)) # triggers at t=-2+√7 cb3 = SymbolicDiscreteCallback([1.0] => [p1 ~ 5.0], discrete_parameters = [p1]) - @mtkbuild sys = System( + @mtkcompile sys = System( [D(x) ~ p1 * t + p2], t; parameter_dependencies = [p2 => 2p1], @@ -73,7 +73,7 @@ end @parameters p1=1.0 p2=1.0 @variables x(t) = 0 - @mtkbuild sys1 = System( + @mtkcompile sys1 = System( [D(x) ~ p1 * t + p2], t ) @@ -215,7 +215,7 @@ end D(x) ~ -x + u y ~ x z(k) ~ z(k - 2) + yd(k - 2)] - @test_throws ModelingToolkit.HybridSystemNotSupportedException @mtkbuild sys = System( + @test_throws ModelingToolkit.HybridSystemNotSupportedException @mtkcompile sys = System( eqs, t; parameter_dependencies = [kq => 2kp]) @test_skip begin @@ -225,7 +225,7 @@ end yd(k - 2) => 2.0]) @test_nowarn solve(prob, Tsit5()) - @mtkbuild sys = System(eqs, t; parameter_dependencies = [kq => 2kp], + @mtkcompile sys = System(eqs, t; parameter_dependencies = [kq => 2kp], discrete_events = [SymbolicDiscreteCallback( [0.5] => [kp ~ 2.0], discrete_parameters = [kp])]) prob = ODEProblem(sys, [x => 0.0, y => 0.0], (0.0, Tf), @@ -328,7 +328,7 @@ end @parameters p1=1.0 p2=1.0 @variables x(t) eqs = [0 ~ p1 * x * exp(x) + p2] - @mtkbuild sys = System(eqs; parameter_dependencies = [p2 => 2p1]) + @mtkcompile sys = System(eqs; parameter_dependencies = [p2 => 2p1]) @test isequal(only(parameters(sys)), p1) @test Set(full_parameters(sys)) == Set([p1, p2, Initial(p2), Initial(x)]) prob = NonlinearProblem(sys, [x => 1.0]) @@ -350,7 +350,7 @@ end cb2 = [x ~ 4.0] => (affect1!, [], [p1, p2], [p1]) # triggers at t=-2+√7 cb3 = [1.0] => [p1 ~ 5.0] - @mtkbuild sys = System( + @mtkcompile sys = System( [D(x) ~ p1 * t + p2], t; parameter_dependencies = [p2 => 2p1] diff --git a/test/problem_validation.jl b/test/problem_validation.jl index 3db151bda5..e6e7022684 100644 --- a/test/problem_validation.jl +++ b/test/problem_validation.jl @@ -6,7 +6,7 @@ using ModelingToolkit: t_nounits as t, D_nounits as D @variables X(t) @parameters p d eqs = [D(X) ~ p - d * X] - @mtkbuild osys = System(eqs, t) + @mtkcompile osys = System(eqs, t) p = "I accidentally renamed p" u0 = [X => 1.0] @@ -23,7 +23,7 @@ using ModelingToolkit: t_nounits as t, D_nounits as D @variables x(t) y(t) z(t) @parameters a b c d eqs = [D(x) ~ x * a, D(y) ~ y * c, D(z) ~ b + d] - @mtkbuild sys = System(eqs, t) + @mtkcompile sys = System(eqs, t) pmap = [a => 1, b => 2, c => 3, d => 4, "b" => 2] u0map = [x => 1, y => 2, z => 3] @test_throws InvalidKeyError ODEProblem(sys, u0map, (0.0, 1.0), pmap) diff --git a/test/scc_nonlinear_problem.jl b/test/scc_nonlinear_problem.jl index 3759508c0d..51552678b0 100644 --- a/test/scc_nonlinear_problem.jl +++ b/test/scc_nonlinear_problem.jl @@ -85,7 +85,7 @@ end @variables u[1:5] [irreducible = true] @parameters p1[1:6] p2 eqs = 0 .~ collect(nlf(u, (u0, (p1, p2)))) - @mtkbuild sys = System(eqs, [u], [p1, p2]) + @mtkcompile sys = System(eqs, [u], [p1, p2]) sccprob = SCCNonlinearProblem(sys, [u => u0], [p1 => p[1], p2 => p[2][]]) sccsol = solve(sccprob, SimpleNewtonRaphson(); abstol = 1e-9) @test SciMLBase.successful_retcode(sccsol) @@ -141,7 +141,7 @@ end eqs = 0 .~ eqs subrules = Dict(Symbolics.unwrap(D(y[i])) => ((y[i] - u0[i]) / dt) for i in 1:8) eqs = substitute.(eqs, (subrules,)) - @mtkbuild sys = System(eqs) + @mtkcompile sys = System(eqs) prob = NonlinearProblem(sys, [y => u0], [t => t0]) sol = solve(prob, NewtonRaphson(); abstol = 1e-12) @@ -159,10 +159,10 @@ end x + y end @register_symbolic func(x, y) - @mtkbuild sys = System([0 ~ x[1]^3 + x[2]^3 - 5 - 0 ~ sin(x[1] - x[2]) - 0.5 - 0 ~ func(x[1], x[2]) * exp(x[3]) - x[4]^3 - 5 - 0 ~ func(x[1], x[2]) * exp(x[4]) - x[3]^3 - 4]) + @mtkcompile sys = System([0 ~ x[1]^3 + x[2]^3 - 5 + 0 ~ sin(x[1] - x[2]) - 0.5 + 0 ~ func(x[1], x[2]) * exp(x[3]) - x[4]^3 - 5 + 0 ~ func(x[1], x[2]) * exp(x[4]) - x[3]^3 - 4]) sccprob = SCCNonlinearProblem(sys, []) sccsol = solve(sccprob, NewtonRaphson()) @test SciMLBase.successful_retcode(sccsol) @@ -255,7 +255,7 @@ import ModelingToolkitStandardLibrary.Hydraulic.IsothermalCompressible as IC initialization_eqs = [mass.s ~ 0.0 mass.v ~ 0.0] - @mtkbuild sys = System(eqs, t, [], []; systems, initialization_eqs) + @mtkcompile sys = System(eqs, t, [], []; systems, initialization_eqs) prob = ODEProblem(sys, [], (0, 5)) sol = solve(prob) @test SciMLBase.successful_retcode(sol) @@ -264,7 +264,7 @@ end @testset "Array variables split across SCCs" begin @variables x[1:3] @parameters (f::Function)(..) - @mtkbuild sys = System([ + @mtkcompile sys = System([ 0 ~ x[1]^2 - 9, x[2] ~ 2x[1], 0 ~ x[3]^2 - x[1]^2 + f(x)]) prob = SCCNonlinearProblem(sys, [x => ones(3)], [f => sum]) sol = solve(prob, NewtonRaphson()) @@ -274,7 +274,7 @@ end @testset "SCCNonlinearProblem retains parameter order" begin @variables x y z @parameters σ β ρ - @mtkbuild fullsys = System( + @mtkcompile fullsys = System( [0 ~ x^3 * β + y^3 * ρ - σ, 0 ~ x^2 + 2x * y + y^2, 0 ~ z^2 - 4z + 4], [x, y, z], [σ, β, ρ]) @@ -294,7 +294,7 @@ end @variables x y @parameters p[1:2] (f::Function)(..) - @mtkbuild sys = System([x^2 - p[1]^2 ~ 0, y^2 ~ f(p)]) + @mtkcompile sys = System([x^2 - p[1]^2 ~ 0, y^2 ~ f(p)]) prob = SCCNonlinearProblem(sys, [x => 1.0, y => 1.0], [p => ones(2), f => sum]) @test_nowarn solve(prob, NewtonRaphson()) end diff --git a/test/sdesystem.jl b/test/sdesystem.jl index a3e0ff00d0..ead8057bee 100644 --- a/test/sdesystem.jl +++ b/test/sdesystem.jl @@ -636,7 +636,7 @@ ssys = SDESystem(eqs, noise_eqs, t, [X], [p, d]; name = :ssys) @variables x(tt) @brownian a eqs = [D(x) ~ p - d * x + a * sqrt(p)] -@mtkbuild sys = System(eqs, tt) +@mtkcompile sys = System(eqs, tt) u0 = @SVector[x => 10.0] tspan = (0.0, 10.0) ps = @SVector[p => 5.0, d => 0.5] @@ -650,7 +650,7 @@ sprob = SDEProblem(sys, u0, tspan, ps) @brownian b eqs = [D(x) ~ p - d * x + a * sqrt(p) D(y) ~ p - d * y + b * sqrt(d)] -@mtkbuild sys = System(eqs, tt) +@mtkcompile sys = System(eqs, tt) u0 = @SVector[x => 10.0, y => 20.0] tspan = (0.0, 10.0) ps = @SVector[p => 5.0, d => 0.5] @@ -666,7 +666,7 @@ let D(y) ~ x * (ρ - z) - y + 0.1a * y, D(z) ~ x * y - β * z + 0.1a * z] - @mtkbuild de = System(eqs, t) + @mtkcompile de = System(eqs, t) u0map = [ x => 1.0, @@ -690,7 +690,7 @@ let # test to make sure that scalar noise always receive the same kicks eqs = [D(x) ~ a, D(y) ~ a] - @mtkbuild de = System(eqs, t) + @mtkcompile de = System(eqs, t) prob = SDEProblem(de, [x => 0, y => 0], (0.0, 10.0), []) sol = solve(prob, SOSRI()) @test sol.u[end][1] == sol.u[end][2] @@ -704,7 +704,7 @@ let # test that diagonal noise is correctly handled D(y) ~ x * (ρ - z) - y + 0.1b * y, D(z) ~ x * y - β * z + 0.1c * z] - @mtkbuild de = System(eqs, t) + @mtkcompile de = System(eqs, t) u0map = [ x => 1.0, @@ -730,7 +730,7 @@ end eqs = [D(x) ~ σ * (y - x) + 0.1a * x + d, D(y) ~ x * (ρ - z) - y + 0.1b * y + e, D(z) ~ x * y - β * z + 0.1c * z + f] - @mtkbuild de = System(eqs, tt) + @mtkcompile de = System(eqs, tt) u0map = [ x => 1.0, @@ -759,7 +759,7 @@ end eqs = [D(x) ~ σ * (y - x) + 0.1a * x, # One brownian D(y) ~ x * (ρ - z) - y + 0.1b * y, # Another brownian D(z) ~ x * y - β * z] # no brownians -- still diagonal - @mtkbuild de = System(eqs, tt) + @mtkcompile de = System(eqs, tt) u0map = [ x => 1.0, @@ -780,7 +780,7 @@ end @testset "Passing `nothing` to `u0`" begin @variables x(t) = 1 @brownian b - @mtkbuild sys = System([D(x) ~ x + b], t) + @mtkcompile sys = System([D(x) ~ x + b], t) prob = @test_nowarn SDEProblem(sys, nothing, (0.0, 1.0)) @test_nowarn solve(prob, ImplicitEM()) end @@ -807,7 +807,7 @@ end @testset "Observed variables retained after `structural_simplify`" begin @variables x(t) y(t) z(t) @brownian a - @mtkbuild sys = System([D(x) ~ x + a, D(y) ~ y + a, z ~ x + y], t) + @mtkcompile sys = System([D(x) ~ x + a, D(y) ~ y + a, z ~ x + y], t) @test length(observed(sys)) == 1 prob = SDEProblem(sys, [x => 1.0, y => 1.0], (0.0, 1.0)) @test prob[z] ≈ 2.0 @@ -862,7 +862,7 @@ end @testset "`structural_simplify(::SDESystem)`" begin @variables x(t) y(t) - @mtkbuild sys = SDESystem( + @mtkcompile sys = SDESystem( [D(x) ~ x, y ~ 2x], [x, 0], t, [x, y], []) @test length(equations(sys)) == 1 @test length(ModelingToolkit.get_noise_eqs(sys)) == 1 @@ -875,7 +875,7 @@ end @variables X(t)::Int64 @brownian z eq2 = D(X) ~ p - d * X + z - @test_throws ModelingToolkit.ContinuousOperatorDiscreteArgumentError @mtkbuild ssys = System( + @test_throws ModelingToolkit.ContinuousOperatorDiscreteArgumentError @mtkcompile ssys = System( [eq2], t) noiseeq = [1] @test_throws ModelingToolkit.ContinuousOperatorDiscreteArgumentError @named ssys = SDESystem( @@ -929,23 +929,23 @@ end @parameters p d @brownian a seq = D(X) ~ p - d * X + a - @mtkbuild ssys1 = System([seq], t; name = :ssys) - @mtkbuild ssys2 = System([seq], t; name = :ssys) + @mtkcompile ssys1 = System([seq], t; name = :ssys) + @mtkcompile ssys2 = System([seq], t; name = :ssys) @test ssys1 == ssys2 # true continuous_events = [[X ~ 1.0] => [X ~ X + 5.0]] discrete_events = [5.0 => [d ~ d / 2.0]] - @mtkbuild ssys1 = System([seq], t; name = :ssys, continuous_events) - @mtkbuild ssys2 = System([seq], t; name = :ssys) + @mtkcompile ssys1 = System([seq], t; name = :ssys, continuous_events) + @mtkcompile ssys2 = System([seq], t; name = :ssys) @test ssys1 !== ssys2 - @mtkbuild ssys1 = System([seq], t; name = :ssys, discrete_events) - @mtkbuild ssys2 = System([seq], t; name = :ssys) + @mtkcompile ssys1 = System([seq], t; name = :ssys, discrete_events) + @mtkcompile ssys2 = System([seq], t; name = :ssys) @test ssys1 !== ssys2 - @mtkbuild ssys1 = System([seq], t; name = :ssys, continuous_events) - @mtkbuild ssys2 = System([seq], t; name = :ssys, discrete_events) + @mtkcompile ssys1 = System([seq], t; name = :ssys, continuous_events) + @mtkcompile ssys2 = System([seq], t; name = :ssys, discrete_events) @test ssys1 !== ssys2 end diff --git a/test/split_parameters.jl b/test/split_parameters.jl index 39b242db08..d6596f68ae 100644 --- a/test/split_parameters.jl +++ b/test/split_parameters.jl @@ -236,7 +236,7 @@ end (::Foo)(x) = 3x @variables x(t) @parameters fn(::Real) = _f1 - @mtkbuild sys = System(D(x) ~ fn(t), t) + @mtkcompile sys = System(D(x) ~ fn(t), t) @test is_parameter(sys, fn) @test ModelingToolkit.defaults(sys)[fn] == _f1 @@ -260,7 +260,7 @@ end interp = LinearInterpolation(ts .^ 2, ts; extrapolate = true) @variables x(t) @parameters (fn::typeof(interp))(..) - @mtkbuild sys = System(D(x) ~ fn(x), t) + @mtkcompile sys = System(D(x) ~ fn(x), t) @test is_parameter(sys, fn) getter = getp(sys, fn) prob = ODEProblem(sys, [x => 1.0], (0.0, 1.0), [fn => interp]) diff --git a/test/structural_transformation/utils.jl b/test/structural_transformation/utils.jl index b228cdbbf7..83da4df584 100644 --- a/test/structural_transformation/utils.jl +++ b/test/structural_transformation/utils.jl @@ -49,7 +49,7 @@ end @variables x(t) y(t)[1:2] z(t)[1:2] @parameters foo(::AbstractVector)[1:2] _tmp_fn(x) = 2x - @mtkbuild sys = System( + @mtkcompile sys = System( [D(x) ~ z[1] + z[2] + foo(z)[1], y[1] ~ 2t, y[2] ~ 3t, z ~ foo(y)], t) @test length(equations(sys)) == 1 @test length(observed(sys)) == 7 @@ -75,7 +75,7 @@ end val[] += 1 return [x, 2x] end - @mtkbuild sys = System([D(x) ~ y[1] + y[2], y ~ foo(x)], t) + @mtkcompile sys = System([D(x) ~ y[1] + y[2], y ~ foo(x)], t) @test length(equations(sys)) == 1 @test length(observed(sys)) == 4 prob = ODEProblem(sys, [x => 1.0], (0.0, 1.0), [foo => _tmp_fn2]) @@ -94,7 +94,7 @@ end @testset "CSE hack in equations(sys)" begin val[] = 0 @variables z(t)[1:2] - @mtkbuild sys = System( + @mtkcompile sys = System( [D(y) ~ foo(x), D(x) ~ sum(y), zeros(2) ~ foo(prod(z))], t) @test length(equations(sys)) == 5 @test length(observed(sys)) == 2 @@ -200,7 +200,7 @@ end end @testset "`ODESystem`" begin @variables x(t) y(t) z(t) - @mtkbuild sys = System([D(x) ~ 2x + y, y ~ x + z, z^3 + x^3 ~ 12], t) + @mtkcompile sys = System([D(x) ~ 2x + y, y ~ x + z, z^3 + x^3 ~ 12], t) mapping = map_variables_to_equations(sys) @test mapping[x] == (D(x) ~ 2x + y) @test mapping[y] == (y ~ x + z) @@ -213,7 +213,7 @@ end eqs = [D(D(x)) ~ λ * x D(D(y)) ~ λ * y - g x^2 + y^2 ~ 1] - @mtkbuild sys = System(eqs, t) + @mtkcompile sys = System(eqs, t) mapping = map_variables_to_equations(sys) yt = default_toterm(unwrap(D(y))) @@ -254,7 +254,7 @@ end eqs = [osc1.jcn ~ osc2.delx, osc2.jcn ~ osc1.delx] @named coupledOsc = System(eqs, t) - @mtkbuild sys = compose(coupledOsc, systems) + @mtkcompile sys = compose(coupledOsc, systems) mapping = map_variables_to_equations(sys) x1 = operation(unwrap(osc1.x)) x2 = operation(unwrap(osc2.x)) @@ -271,7 +271,7 @@ end end @testset "`NonlinearSystem`" begin @variables x y z - @mtkbuild sys = System([x^2 ~ 2y^2 + 1, sin(z) ~ y, z^3 + 4z + 1 ~ 0]) + @mtkcompile sys = System([x^2 ~ 2y^2 + 1, sin(z) ~ y, z^3 + 4z + 1 ~ 0]) mapping = map_variables_to_equations(sys) @test mapping[x] == (0 ~ 2y^2 + 1 - x^2) @test mapping[y] == (y ~ sin(z)) @@ -337,7 +337,7 @@ end @named sys = FilteredInputErr() @test_throws ["derivative of discrete variable", "k(t)"] structural_simplify(sys) - @mtkbuild sys = FilteredInput() + @mtkcompile sys = FilteredInput() vs = Set() for eq in equations(sys) ModelingToolkit.vars!(vs, eq) @@ -348,7 +348,7 @@ end @test !(D(sys.k) in vs) - @mtkbuild sys = FilteredInputExplicit() + @mtkcompile sys = FilteredInputExplicit() obsfn1 = ModelingToolkit.build_explicit_observed_function(sys, sys.ddx) obsfn2 = ModelingToolkit.build_explicit_observed_function(sys, sys.dx) u = [1.0] @@ -375,7 +375,7 @@ end return System(eqs, t, vars, params; systems, name) end - @mtkbuild sys = FilteredInput2() + @mtkcompile sys = FilteredInput2() vs = Set() for eq in equations(sys) ModelingToolkit.vars!(vs, eq) @@ -392,7 +392,7 @@ end @testset "ODESystem" begin @variables x(t) p @parameters y(t) q - @mtkbuild sys = System([D(x) ~ x * q, x^2 + y^2 ~ p], t, [x, y], + @mtkcompile sys = System([D(x) ~ x * q, x^2 + y^2 ~ p], t, [x, y], [p, q]; initialization_eqs = [p + q ~ 3], defaults = [p => missing], guesses = [p => 1.0, y => 1.0]) @test length(equations(sys)) == 2 @@ -406,7 +406,7 @@ end @testset "NonlinearSystem" begin @variables x p @parameters y q - @mtkbuild sys = System([0 ~ p * x + y, x^3 + y^3 ~ q], [x, y], + @mtkcompile sys = System([0 ~ p * x + y, x^3 + y^3 ~ q], [x, y], [p, q]; initialization_eqs = [p ~ q + 1], guesses = [p => 1.0], defaults = [p => missing]) @test length(equations(sys)) == length(unknowns(sys)) == 1 @@ -422,7 +422,7 @@ end @variables x(t) p a @parameters y(t) q b @brownian c - @mtkbuild sys = System([D(x) ~ x + q * a, D(y) ~ y + p * b + c], t, [x, y], + @mtkcompile sys = System([D(x) ~ x + q * a, D(y) ~ y + p * b + c], t, [x, y], [p, q], [a, b, c]; initialization_eqs = [p + q ~ 4], guesses = [p => 1.0], defaults = [p => missing]) @test length(equations(sys)) == 2 diff --git a/test/symbolic_events.jl b/test/symbolic_events.jl index d3c767ee0d..8f26d12e19 100644 --- a/test/symbolic_events.jl +++ b/test/symbolic_events.jl @@ -867,7 +867,7 @@ end end # Test Simulation - @mtkbuild sys = TestSystem() + @mtkcompile sys = TestSystem() # Test Simulation prob = ODEProblem(sys, [], (0.0, 150.0)) @@ -885,7 +885,7 @@ end cb2 = [x ~ 0.5] => (f = save_affect!, modified = (; b)) cb3 = SymbolicDiscreteCallback(1.0 => [c ~ t], discrete_parameters = [c]) - @mtkbuild sys = System(D(x) ~ cos(t), t, [x], [a, b, c]; + @mtkcompile sys = System(D(x) ~ cos(t), t, [x], [a, b, c]; continuous_events = [cb1, cb2], discrete_events = [cb3]) prob = ODEProblem(sys, [x => 1.0], (0.0, 2pi), [a => 1.0, b => 2.0, c => 0.0]) @test sort(canonicalize(Discrete(), prob.p)[1]) == [0.0, 1.0, 2.0] @@ -1070,7 +1070,7 @@ end f = ModelingToolkit.ImperativeAffect(f = (m, o, ctx, int) -> (seen = true; return (;))) cb1 = ModelingToolkit.SymbolicContinuousCallback( [x ~ 0], nothing, initialize = [x ~ 1.5], finalize = f) - @mtkbuild sys = System(D(x) ~ -1, t, [x], []; continuous_events = [cb1]) + @mtkcompile sys = System(D(x) ~ -1, t, [x], []; continuous_events = [cb1]) prob = ODEProblem(sys, [x => 1.0], (0.0, 2), []) sol = solve(prob, Tsit5(); dtmax = 0.01) @test sol[x][1] ≈ 1.0 @@ -1088,7 +1088,7 @@ end b = ModelingToolkit.ImperativeAffect(f = (m, o, ctx, int) -> (finaled = true; return (;))) cb2 = ModelingToolkit.SymbolicContinuousCallback( [x ~ 0.1], nothing, initialize = a, finalize = b) - @mtkbuild sys = System(D(x) ~ -1, t, [x], []; continuous_events = [cb1, cb2]) + @mtkcompile sys = System(D(x) ~ -1, t, [x], []; continuous_events = [cb1, cb2]) prob = ODEProblem(sys, [x => 1.0], (0.0, 2), []) sol = solve(prob, Tsit5()) @test sol[x][1] ≈ 1.0 @@ -1102,7 +1102,7 @@ end finaled = false cb3 = ModelingToolkit.SymbolicDiscreteCallback( 1.0, [x ~ 2], initialize = a, finalize = b) - @mtkbuild sys = System(D(x) ~ -1, t, [x], []; discrete_events = [cb3]) + @mtkcompile sys = System(D(x) ~ -1, t, [x], []; discrete_events = [cb3]) prob = ODEProblem(sys, [x => 1.0], (0.0, 2), []) sol = solve(prob, Tsit5()) @test inited == true @@ -1115,7 +1115,7 @@ end inited = false finaled = false cb3 = ModelingToolkit.SymbolicDiscreteCallback(1.0, f, initialize = a, finalize = b) - @mtkbuild sys = System(D(x) ~ -1, t, [x], []; discrete_events = [cb3]) + @mtkcompile sys = System(D(x) ~ -1, t, [x], []; discrete_events = [cb3]) prob = ODEProblem(sys, [x => 1.0], (0.0, 2), []) sol = solve(prob, Tsit5()) @test seen == true @@ -1126,7 +1126,7 @@ end inited = false finaled = false cb3 = ModelingToolkit.SymbolicDiscreteCallback([1.0], f, initialize = a, finalize = b) - @mtkbuild sys = System(D(x) ~ -1, t, [x], []; discrete_events = [cb3]) + @mtkcompile sys = System(D(x) ~ -1, t, [x], []; discrete_events = [cb3]) prob = ODEProblem(sys, [x => 1.0], (0.0, 2), []) sol = solve(prob, Tsit5()) @test seen == true @@ -1139,7 +1139,7 @@ end finaled = false cb3 = ModelingToolkit.SymbolicDiscreteCallback( t == 1.0, f, initialize = a, finalize = b) - @mtkbuild sys = System(D(x) ~ -1, t, [x], []; discrete_events = [cb3]) + @mtkcompile sys = System(D(x) ~ -1, t, [x], []; discrete_events = [cb3]) prob = ODEProblem(sys, [x => 1.0], (0.0, 2), []) sol = solve(prob, Tsit5(); tstops = 1.0) @test seen == true @@ -1151,17 +1151,17 @@ end @variables x(t) [irreducible = true] y(t) [irreducible = true] eqs = [x ~ y, D(x) ~ -1] cb = [x ~ 0.0] => [x ~ 0, y ~ 1] - @mtkbuild pend = System(eqs, t; continuous_events = [cb]) + @mtkcompile pend = System(eqs, t; continuous_events = [cb]) prob = ODEProblem(pend, [x => 1], (0.0, 3.0), guesses = [y => x]) @test_broken !SciMLBase.successful_retcode(solve(prob, Rodas5())) cb = [x ~ 0.0] => [y ~ 1] - @mtkbuild pend = System(eqs, t; continuous_events = [cb]) + @mtkcompile pend = System(eqs, t; continuous_events = [cb]) prob = ODEProblem(pend, [x => 1], (0.0, 3.0), guesses = [y => x]) @test_broken !SciMLBase.successful_retcode(solve(prob, Rodas5())) cb = [x ~ 0.0] => [x ~ 1, y ~ 1] - @mtkbuild pend = System(eqs, t; continuous_events = [cb]) + @mtkcompile pend = System(eqs, t; continuous_events = [cb]) prob = ODEProblem(pend, [x => 1], (0.0, 3.0), guesses = [y => x]) @test all(≈(0.0; atol = 1e-9), solve(prob, Rodas5())[[x, y]][end]) end @@ -1182,7 +1182,7 @@ end (t == 1.0) => [k ~ 1.0], [discrete_parameters = k] end end - @mtkbuild decay = DECAY() + @mtkcompile decay = DECAY() prob = ODEProblem(decay, [], (0.0, 10.0), []) @test_nowarn solve(prob, Tsit5(), tstops = [1.0]) end @@ -1252,7 +1252,7 @@ end D(D(y)) ~ λ * y - g x^2 + y^2 ~ 1] c_evt = [t ~ 5.0] => [x ~ Pre(x) + 0.1] - @mtkbuild pend = System(eqs, t, continuous_events = c_evt) + @mtkcompile pend = System(eqs, t, continuous_events = c_evt) prob = ODEProblem(pend, [x => -1, y => 0], (0.0, 10.0), [g => 1], guesses = [λ => 1]) sol = solve(prob, FBDF()) @test ≈(sol(5.000001, idxs = x) - sol(4.999999, idxs = x), 0.1, rtol = 1e-4) @@ -1260,7 +1260,7 @@ end # Implicit affect with Pre c_evt = [t ~ 5.0] => [x ~ Pre(x) + y^2] - @mtkbuild pend = System(eqs, t, continuous_events = c_evt) + @mtkcompile pend = System(eqs, t, continuous_events = c_evt) prob = ODEProblem(pend, [x => 1, y => 0], (0.0, 10.0), [g => 1], guesses = [λ => 1]) sol = solve(prob, FBDF()) @test ≈(sol(5.000001, idxs = y)^2 + sol(4.999999, idxs = x), @@ -1269,7 +1269,7 @@ end # Impossible affect errors c_evt = [t ~ 5.0] => [x ~ Pre(x) + 2] - @mtkbuild pend = System(eqs, t, continuous_events = c_evt) + @mtkcompile pend = System(eqs, t, continuous_events = c_evt) prob = ODEProblem(pend, [x => 1, y => 0], (0.0, 10.0), [g => 1], guesses = [λ => 1]) @test_throws UnsolvableCallbackError sol=solve(prob, FBDF()) @@ -1280,7 +1280,7 @@ end x^2 + y^2 ~ 1] c_evt = SymbolicContinuousCallback( [t ~ 5.0], [x ~ Pre(x) + 0.1, g ~ Pre(g) + 1], discrete_parameters = [g], iv = t) - @mtkbuild pend = System(eqs, t, continuous_events = c_evt) + @mtkcompile pend = System(eqs, t, continuous_events = c_evt) prob = ODEProblem(pend, [x => 1, y => 0], (0.0, 10.0), [g => 1], guesses = [λ => 1]) sol = solve(prob, FBDF()) @test sol.ps[g] ≈ [1, 2] @@ -1290,7 +1290,7 @@ end eqs = [y ~ g^2, D(x) ~ x] c_evt = SymbolicContinuousCallback( [t ~ 5.0], [x ~ Pre(x) + 1, g ~ Pre(g) + 1], discrete_parameters = [g], iv = t) - @mtkbuild sys = System(eqs, t, continuous_events = c_evt) + @mtkcompile sys = System(eqs, t, continuous_events = c_evt) prob = ODEProblem(sys, [x => 1.0], (0.0, 10.0), [g => 2]) sol = solve(prob, FBDF()) @test sol.ps[g] ≈ [2.0, 3.0] @@ -1299,7 +1299,7 @@ end # Parameters that don't appear in affects should not be mutated. c_evt = [t ~ 5.0] => [x ~ Pre(x) + 1] - @mtkbuild sys = System(eqs, t, continuous_events = c_evt) + @mtkcompile sys = System(eqs, t, continuous_events = c_evt) prob = ODEProblem(sys, [x => 0.5], (0.0, 10.0), [g => 2], guesses = [y => 0]) sol = solve(prob, FBDF()) @test prob.ps[g] == sol.ps[g] diff --git a/test/symbolic_indexing_interface.jl b/test/symbolic_indexing_interface.jl index 14cf0c10a7..b3ecd9058b 100644 --- a/test/symbolic_indexing_interface.jl +++ b/test/symbolic_indexing_interface.jl @@ -78,7 +78,7 @@ end # D(x) ~ -x + u # y ~ x] -# @mtkbuild cl = System(eqs, t) +# @mtkcompile cl = System(eqs, t) # partition1_params = [Hold(ud1), Sample(t, dt)(y), ud1, yd1] # partition2_params = [Hold(ud2), Sample(t, dt2)(y), ud2, yd2] # @test all( @@ -195,7 +195,7 @@ end @testset "Observed functions with variables as `Symbol`s" begin @variables x(t) y(t) z(t)[1:2] @parameters p1 p2[1:2, 1:2] - @mtkbuild sys = System([D(x) ~ x * t + p1, y ~ 2x, D(z) ~ p2 * z], t) + @mtkcompile sys = System([D(x) ~ x * t + p1, y ~ 2x, D(z) ~ p2 * z], t) prob = ODEProblem( sys, [x => 1.0, z => ones(2)], (0.0, 1.0), [p1 => 2.0, p2 => ones(2, 2)]) @test getu(prob, x)(prob) == getu(prob, :x)(prob) @@ -230,7 +230,7 @@ end @parameters p(t)[1:2, 1:2] ev = SymbolicContinuousCallback( [x[1] ~ 2.0] => [p ~ -ones(2, 2)], discrete_parameters = [p]) - @mtkbuild sys = System(D(x) ~ p * x, t; continuous_events = [ev]) + @mtkcompile sys = System(D(x) ~ p * x, t; continuous_events = [ev]) p = ModelingToolkit.unwrap(p) @test timeseries_parameter_index(sys, p) === ParameterTimeseriesIndex(1, (1, 1)) @test timeseries_parameter_index(sys, p[1, 1]) === diff --git a/test/variable_utils.jl b/test/variable_utils.jl index 36b80a21dd..c088481925 100644 --- a/test/variable_utils.jl +++ b/test/variable_utils.jl @@ -76,7 +76,7 @@ end sys = System(Equation[], iv; name, systems = [😄, arr]) end - @mtkbuild sys = Outer() + @mtkcompile sys = Outer() for (str, var) in [ # unicode system, scalar variable ("😄.x", sys.😄.x), From 52d3ab8335188d196d1dfe044ca18ebb1095344e Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 22 May 2025 15:56:16 +0530 Subject: [PATCH 1820/2176] refactor: rename `structural_simplify` to `mtkcompile` --- src/ModelingToolkit.jl | 4 +- src/inputoutput.jl | 4 +- src/linearization.jl | 6 +-- src/problems/compatibility.jl | 4 +- src/problems/initializationproblem.jl | 4 +- src/problems/sccnonlinearproblem.jl | 4 +- .../StructuralTransformations.jl | 2 +- src/structural_transformation/pantelides.jl | 2 +- .../symbolics_tearing.jl | 2 +- src/systems/abstractsystem.jl | 16 +++--- src/systems/callbacks.jl | 2 +- src/systems/diffeqs/basic_transformations.jl | 6 +-- src/systems/index_cache.jl | 4 +- .../nonlinear/homotopy_continuation.jl | 4 +- src/systems/parameter_buffer.jl | 2 +- src/systems/system.jl | 2 +- src/systems/systems.jl | 16 +++--- src/systems/systemstructure.jl | 8 +-- src/utils.jl | 8 +-- test/accessor_functions.jl | 10 ++-- test/analysis_points.jl | 2 +- test/basic_transformations.jl | 26 +++++----- test/clock.jl | 14 +++--- test/code_generation.jl | 2 +- test/components.jl | 24 ++++----- test/constants.jl | 6 +-- test/dde.jl | 8 +-- test/debugging.jl | 4 +- test/discrete_system.jl | 2 +- test/domain_connectors.jl | 2 +- test/downstream/analysis_points.jl | 22 ++++---- test/downstream/inversemodel.jl | 2 +- test/downstream/test_disturbance_model.jl | 10 ++-- test/dq_units.jl | 8 +-- test/error_handling.jl | 4 +- test/extensions/ad.jl | 4 +- test/extensions/bifurcationkit.jl | 4 +- test/extensions/dynamic_optimization.jl | 12 ++--- test/extensions/test_infiniteopt.jl | 2 +- test/guess_propagation.jl | 8 +-- test/hierarchical_initialization_eqs.jl | 2 +- test/if_lifting.jl | 6 +-- test/initializationsystem.jl | 26 +++++----- test/input_output_handling.jl | 28 +++++------ test/jumpsystem.jl | 2 +- test/modelingtoolkitize.jl | 2 +- test/mtkparameters.jl | 6 +-- test/nonlinearsystem.jl | 20 ++++---- test/odesystem.jl | 50 +++++++++---------- test/optimizationsystem.jl | 2 +- test/parameter_dependencies.jl | 4 +- test/reduction.jl | 28 +++++------ test/scc_nonlinear_problem.jl | 4 +- test/sciml_problem_inputs.jl | 2 +- test/sdesystem.jl | 14 +++--- test/serialization.jl | 2 +- test/split_parameters.jl | 4 +- test/state_selection.jl | 6 +-- test/static_arrays.jl | 2 +- test/stream_connectors.jl | 12 ++--- .../index_reduction.jl | 4 +- test/structural_transformation/tearing.jl | 6 +-- test/structural_transformation/utils.jl | 12 ++--- test/substitute_component.jl | 4 +- test/symbolic_events.jl | 50 +++++++++---------- test/symbolic_indexing_interface.jl | 2 +- test/units.jl | 8 +-- test/variable_scope.jl | 2 +- 68 files changed, 293 insertions(+), 293 deletions(-) diff --git a/src/ModelingToolkit.jl b/src/ModelingToolkit.jl index c34e6df3d3..bac58df69b 100644 --- a/src/ModelingToolkit.jl +++ b/src/ModelingToolkit.jl @@ -233,7 +233,7 @@ PrecompileTools.@compile_workload begin using ModelingToolkit @variables x(ModelingToolkit.t_nounits) @named sys = System([ModelingToolkit.D_nounits(x) ~ -x], ModelingToolkit.t_nounits) - prob = ODEProblem(structural_simplify(sys), [x => 30.0], (0, 100), [], jac = true) + prob = ODEProblem(mtkcompile(sys), [x => 30.0], (0, 100), [], jac = true) @mtkmodel __testmod__ begin @constants begin c = 1.0 @@ -304,7 +304,7 @@ export SymScope, LocalScope, ParentScope, GlobalScope export independent_variable, equations, controls, observed, full_equations, jumps, cost, brownians export initialization_equations, guesses, defaults, parameter_dependencies, hierarchy -export structural_simplify, expand_connections, linearize, linearization_function, +export mtkcompile, expand_connections, linearize, linearization_function, LinearizationProblem export solve export Pre diff --git a/src/inputoutput.jl b/src/inputoutput.jl index 97376a2a44..b4a6bfd998 100644 --- a/src/inputoutput.jl +++ b/src/inputoutput.jl @@ -202,7 +202,7 @@ function generate_control_function(sys::AbstractSystem, inputs = unbound_inputs( kwargs...) # Remove this when the ControlFunction gets merged. if check_simplified && !iscomplete(sys) - error("A completed `ODESystem` is required. Call `complete` or `structural_simplify` on the system before creating the control function.") + error("A completed `ODESystem` is required. Call `complete` or `mtkcompile` on the system before creating the control function.") end isempty(inputs) && @warn("No unbound inputs were found in system.") if disturbance_inputs !== nothing @@ -417,7 +417,7 @@ function add_input_disturbance(sys, dist::DisturbanceModel, inputs = Any[]; kwar dist.input ~ u + dsys.output.u[1]] augmented_sys = System(eqs, t, systems = [dsys], name = gensym(:outer)) augmented_sys = extend(augmented_sys, sys) - ssys = structural_simplify(augmented_sys, inputs = all_inputs, disturbance_inputs = [d]) + ssys = mtkcompile(augmented_sys, inputs = all_inputs, disturbance_inputs = [d]) f, dvs, p, io_sys = generate_control_function(ssys, all_inputs, [d]; kwargs...) diff --git a/src/linearization.jl b/src/linearization.jl index 25b42dffb3..757d6eece5 100644 --- a/src/linearization.jl +++ b/src/linearization.jl @@ -15,7 +15,7 @@ y &= h(x, z, u) where `x` are differential unknown variables, `z` algebraic variables, `u` inputs and `y` outputs. To obtain a linear statespace representation, see [`linearize`](@ref). The input argument `variables` is a vector defining the operating point, corresponding to `unknowns(simplified_sys)` and `p` is a vector corresponding to the parameters of `simplified_sys`. Note: all variables in `inputs` have been converted to parameters in `simplified_sys`. -The `simplified_sys` has undergone [`structural_simplify`](@ref) and had any occurring input or output variables replaced with the variables provided in arguments `inputs` and `outputs`. The unknowns of this system also indicate the order of the unknowns that holds for the linearized matrices. +The `simplified_sys` has undergone [`mtkcompile`](@ref) and had any occurring input or output variables replaced with the variables provided in arguments `inputs` and `outputs`. The unknowns of this system also indicate the order of the unknowns that holds for the linearized matrices. # Arguments: @@ -58,7 +58,7 @@ function linearization_function(sys::AbstractSystem, inputs, outputs = mapreduce(vcat, outputs; init = []) do var symbolic_type(var) == ArraySymbolic() ? collect(var) : [var] end - ssys = structural_simplify(sys; inputs, outputs, simplify, kwargs...) + ssys = mtkcompile(sys; inputs, outputs, simplify, kwargs...) diff_idxs, alge_idxs = eq_idxs(ssys) if zero_dummy_der dummyder = setdiff(unknowns(ssys), unknowns(sys)) @@ -498,7 +498,7 @@ function linearize_symbolic(sys::AbstractSystem, inputs, outputs; simplify = false, allow_input_derivatives = false, eval_expression = false, eval_module = @__MODULE__, kwargs...) - sys = structural_simplify(sys; inputs, outputs, simplify, kwargs...) + sys = mtkcompile(sys; inputs, outputs, simplify, kwargs...) diff_idxs, alge_idxs = eq_idxs(sys) sts = unknowns(sys) t = get_iv(sys) diff --git a/src/problems/compatibility.jl b/src/problems/compatibility.jl index 84c2eefc3f..36302a9e74 100644 --- a/src/problems/compatibility.jl +++ b/src/problems/compatibility.jl @@ -120,7 +120,7 @@ function check_has_noise(sys::System, T) if !isempty(brownians(sys)) msg = """ Systems constructed by defining Brownian variables with `@brownian` must be \ - simplified by calling `structural_simplify` before a `$T` can be constructed. + simplified by calling `mtkcompile` before a `$T` can be constructed. """ end throw(SystemCompatibilityError(msg)) @@ -131,7 +131,7 @@ function check_is_discrete(sys::System, T) if !is_discrete_system(sys) throw(SystemCompatibilityError(""" `$T` expects a discrete system. Consider an `ODEProblem` instead. If your system \ - is discrete, ensure `structural_simplify` has been run on it. + is discrete, ensure `mtkcompile` has been run on it. """)) end end diff --git a/src/problems/initializationproblem.jl b/src/problems/initializationproblem.jl index 132376e3e6..be535da3e6 100644 --- a/src/problems/initializationproblem.jl +++ b/src/problems/initializationproblem.jl @@ -35,7 +35,7 @@ initial conditions for the given DAE. time_dependent_init = is_time_dependent(sys), kwargs...) where {iip, specialize} if !iscomplete(sys) - error("A completed system is required. Call `complete` or `structural_simplify` on the system before creating an `ODEProblem`") + error("A completed system is required. Call `complete` or `mtkcompile` on the system before creating an `ODEProblem`") end if isempty(u0map) && get_initializesystem(sys) !== nothing isys = get_initializesystem(sys; initialization_eqs, check_units) @@ -60,7 +60,7 @@ initial conditions for the given DAE. end if simplify_system - isys = structural_simplify(isys; fully_determined, split = is_split(sys)) + isys = mtkcompile(isys; fully_determined, split = is_split(sys)) end ts = get_tearing_state(isys) diff --git a/src/problems/sccnonlinearproblem.jl b/src/problems/sccnonlinearproblem.jl index 36330e339f..63058a08f3 100644 --- a/src/problems/sccnonlinearproblem.jl +++ b/src/problems/sccnonlinearproblem.jl @@ -73,11 +73,11 @@ function SciMLBase.SCCNonlinearProblem{iip}(sys::System, u0map, parammap = SciMLBase.NullParameters(); eval_expression = false, eval_module = @__MODULE__, cse = true, kwargs...) where {iip} if !iscomplete(sys) || get_tearing_state(sys) === nothing - error("A simplified `System` is required. Call `structural_simplify` on the system before creating an `SCCNonlinearProblem`.") + error("A simplified `System` is required. Call `mtkcompile` on the system before creating an `SCCNonlinearProblem`.") end if !is_split(sys) - error("The system has been simplified with `split = false`. `SCCNonlinearProblem` is not compatible with this system. Pass `split = true` to `structural_simplify` to use `SCCNonlinearProblem`.") + error("The system has been simplified with `split = false`. `SCCNonlinearProblem` is not compatible with this system. Pass `split = true` to `mtkcompile` to use `SCCNonlinearProblem`.") end ts = get_tearing_state(sys) diff --git a/src/structural_transformation/StructuralTransformations.jl b/src/structural_transformation/StructuralTransformations.jl index 15f1f7d2db..0365d50a84 100644 --- a/src/structural_transformation/StructuralTransformations.jl +++ b/src/structural_transformation/StructuralTransformations.jl @@ -35,7 +35,7 @@ import ModelingToolkit: var_derivative!, var_derivative_graph! using Graphs using ModelingToolkit: algeqs, EquationsView, SystemStructure, TransformationState, TearingState, - structural_simplify!, + mtkcompile!, isdiffvar, isdervar, isalgvar, isdiffeq, algeqs, is_only_discrete, dervars_range, diffvars_range, algvars_range, DiffGraph, complete!, diff --git a/src/structural_transformation/pantelides.jl b/src/structural_transformation/pantelides.jl index 53c790863f..871bd99ef4 100644 --- a/src/structural_transformation/pantelides.jl +++ b/src/structural_transformation/pantelides.jl @@ -210,7 +210,7 @@ end dae_index_lowering(sys::System; kwargs...) -> System Perform the Pantelides algorithm to transform a higher index DAE to an index 1 -DAE. `kwargs` are forwarded to [`pantelides!`](@ref). End users are encouraged to call [`structural_simplify`](@ref) +DAE. `kwargs` are forwarded to [`pantelides!`](@ref). End users are encouraged to call [`mtkcompile`](@ref) instead, which calls this function internally. """ function dae_index_lowering(sys::System; kwargs...) diff --git a/src/structural_transformation/symbolics_tearing.jl b/src/structural_transformation/symbolics_tearing.jl index 43c3e78e32..c3499abebc 100644 --- a/src/structural_transformation/symbolics_tearing.jl +++ b/src/structural_transformation/symbolics_tearing.jl @@ -989,7 +989,7 @@ end tearing(sys; simplify=false) Tear the nonlinear equations in system. When `simplify=true`, we simplify the -new residual equations after tearing. End users are encouraged to call [`structural_simplify`](@ref) +new residual equations after tearing. End users are encouraged to call [`mtkcompile`](@ref) instead, which calls this function internally. """ function tearing(sys::AbstractSystem, state = TearingState(sys); mm = nothing, diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index 4efc9be0b4..e4bc47a4b7 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -146,7 +146,7 @@ may be subsetted using `dvs` and `ps`. All `kwargs` are passed to the internal [`build_function`](@ref) call. The returned function can be called as `f(u, p, t)` or `f(du, u, p, t)` for time-dependent systems and `f(u, p)` or `f(du, u, p)` for time-independent systems. If `split=true` (the default) was passed to [`complete`](@ref), -[`structural_simplify`](@ref) or [`@mtkcompile`](@ref), `p` is expected to be an `MTKParameters` +[`mtkcompile`](@ref) or [`@mtkcompile`](@ref), `p` is expected to be an `MTKParameters` object. """ function generate_custom_function(sys::AbstractSystem, exprs, dvs = unknowns(sys), @@ -154,7 +154,7 @@ function generate_custom_function(sys::AbstractSystem, exprs, dvs = unknowns(sys expression = Val{true}, eval_expression = false, eval_module = @__MODULE__, cachesyms::Tuple = (), kwargs...) if !iscomplete(sys) - error("A completed system is required. Call `complete` or `structural_simplify` on the system.") + error("A completed system is required. Call `complete` or `mtkcompile` on the system.") end p = (reorder_parameters(sys, unwrap.(ps))..., cachesyms...) isscalar = !(exprs isa AbstractArray) @@ -751,7 +751,7 @@ function complete( newparams = OrderedSet() iv = has_iv(sys) ? get_iv(sys) : nothing collect_scoped_vars!(newunknowns, newparams, sys, iv; depth = -1) - # don't update unknowns to not disturb `structural_simplify` order + # don't update unknowns to not disturb `mtkcompile` order # `GlobalScope`d unknowns will be picked up and added there @set! sys.ps = unique!(vcat(get_ps(sys), collect(newparams))) @@ -1178,7 +1178,7 @@ end Denotes that a variable belongs to the root system in the hierarchy, regardless of which equations of subsystems in the hierarchy it is involved in. Variables with this scope are never namespaced and only added to the unknowns/parameters of a system when calling -`complete` or `structural_simplify`. +`complete` or `mtkcompile`. """ struct GlobalScope <: SymScope end @@ -2484,7 +2484,7 @@ macro mtkcompile(exprs...) else kwargs = (Expr(:parameters, kwargs...),) end - call_expr = Expr(:call, structural_simplify, kwargs..., name) + call_expr = Expr(:call, mtkcompile, kwargs..., name) esc(quote $named_expr $name = $call_expr @@ -2523,7 +2523,7 @@ function debug_system( functions = Set(functions) # more efficient "in" lookup end if has_systems(sys) && !isempty(get_systems(sys)) - error("debug_system(sys) only works on systems with no sub-systems! Consider flattening it with flatten(sys) or structural_simplify(sys) first.") + error("debug_system(sys) only works on systems with no sub-systems! Consider flattening it with flatten(sys) or mtkcompile(sys) first.") end if has_eqs(sys) eqs = debug_sub.(equations(sys), Ref(functions); kw...) @@ -2608,10 +2608,10 @@ end function check_array_equations_unknowns(eqs, dvs) if any(eq -> eq isa Equation && Symbolics.isarraysymbolic(eq.lhs), eqs) - throw(ArgumentError("The system has array equations. Call `structural_simplify` to handle such equations or scalarize them manually.")) + throw(ArgumentError("The system has array equations. Call `mtkcompile` to handle such equations or scalarize them manually.")) end if any(x -> Symbolics.isarraysymbolic(x), dvs) - throw(ArgumentError("The system has array unknowns. Call `structural_simplify` to handle this or scalarize them manually.")) + throw(ArgumentError("The system has array unknowns. Call `mtkcompile` to handle this or scalarize them manually.")) end end diff --git a/src/systems/callbacks.jl b/src/systems/callbacks.jl index 7be1397922..f70669eca3 100644 --- a/src/systems/callbacks.jl +++ b/src/systems/callbacks.jl @@ -267,7 +267,7 @@ function make_affect(affect::Vector{Equation}; discrete_parameters = Any[], @named affectsys = System( vcat(affect, alg_eqs), iv, collect(union(_dvs, discretes)), collect(union(pre_params, sys_params))) - affectsys = structural_simplify(affectsys; fully_determined = nothing) + affectsys = mtkcompile(affectsys; fully_determined = nothing) # get accessed parameters p from Pre(p) in the callback parameters accessed_params = filter(isparameter, map(unPre, collect(pre_params))) union!(accessed_params, sys_params) diff --git a/src/systems/diffeqs/basic_transformations.jl b/src/systems/diffeqs/basic_transformations.jl index 6046e5d4be..6243ca2405 100644 --- a/src/systems/diffeqs/basic_transformations.jl +++ b/src/systems/diffeqs/basic_transformations.jl @@ -74,7 +74,7 @@ Any extra equations `eqs` involving the new and old independent variables will b # Usage before structural simplification The variable change must take place before structural simplification. -In following calls to `structural_simplify`, consider passing `allow_symbolic = true` to avoid undesired constraint equations between between dummy variables. +In following calls to `mtkcompile`, consider passing `allow_symbolic = true` to avoid undesired constraint equations between between dummy variables. # Usage with non-autonomous systems @@ -99,7 +99,7 @@ julia> @named M = System([D(D(y)) ~ -9.81, D(D(x)) ~ 0.0], t); julia> M = change_independent_variable(M, x); -julia> M = structural_simplify(M; allow_symbolic = true); +julia> M = mtkcompile(M; allow_symbolic = true); julia> unknowns(M) 3-element Vector{SymbolicUtils.BasicSymbolic{Real}}: @@ -248,7 +248,7 @@ function stochastic_integral_transform(sys::System, correction_factor) throw(ArgumentError(""" `$stochastic_integral_transform` expects a system with noise_eqs. If your \ noise is specified using brownian variables, consider calling \ - `structural_simplify`. + `mtkcompile`. """)) end name = nameof(sys) diff --git a/src/systems/index_cache.jl b/src/systems/index_cache.jl index 5ea5fa16a7..bdb69ae8c6 100644 --- a/src/systems/index_cache.jl +++ b/src/systems/index_cache.jl @@ -653,10 +653,10 @@ See also: [`MTKParameters`](@ref), [`tunable_parameters`](@ref), [`reorder_dimen function reorder_dimension_by_tunables!( dest::AbstractArray, sys::AbstractSystem, arr::AbstractArray, syms; dim = 1) if !iscomplete(sys) - throw(ArgumentError("A completed system is required. Call `complete` or `structural_simplify` on the system.")) + throw(ArgumentError("A completed system is required. Call `complete` or `mtkcompile` on the system.")) end if !has_index_cache(sys) || (ic = get_index_cache(sys)) === nothing - throw(ArgumentError("The system does not have an index cache. Call `complete` or `structural_simplify` on the system with `split = true`.")) + throw(ArgumentError("The system does not have an index cache. Call `complete` or `mtkcompile` on the system with `split = true`.")) end if size(dest) != size(arr) throw(ArgumentError("Source and destination arrays must have the same size. Got source array with size $(size(arr)) and destination with size $(size(dest)).")) diff --git a/src/systems/nonlinear/homotopy_continuation.jl b/src/systems/nonlinear/homotopy_continuation.jl index 103780cb21..ddc0b72aed 100644 --- a/src/systems/nonlinear/homotopy_continuation.jl +++ b/src/systems/nonlinear/homotopy_continuation.jl @@ -491,7 +491,7 @@ function SciMLBase.HomotopyNonlinearFunction{iip, specialize}( p = nothing, fraction_cancel_fn = SymbolicUtils.simplify_fractions, cse = true, kwargs...) where {iip, specialize} if !iscomplete(sys) - error("A completed `System` is required. Call `complete` or `structural_simplify` on the system before creating a `HomotopyContinuationFunction`") + error("A completed `System` is required. Call `complete` or `mtkcompile` on the system before creating a `HomotopyContinuationFunction`") end transformation = PolynomialTransformation(sys) if transformation isa NotPolynomialError @@ -552,7 +552,7 @@ function HomotopyContinuationProblem{iip, spec}( sys::System, u0map, pmap = SciMLBase.NullParameters(); kwargs...) where {iip, spec} if !iscomplete(sys) - error("A completed `System` is required. Call `complete` or `structural_simplify` on the system before creating a `HomotopyContinuationProblem`") + error("A completed `System` is required. Call `complete` or `mtkcompile` on the system before creating a `HomotopyContinuationProblem`") end f, u0, p = process_SciMLProblem( HomotopyNonlinearFunction{iip, spec}, sys, u0map, pmap; kwargs...) diff --git a/src/systems/parameter_buffer.jl b/src/systems/parameter_buffer.jl index 37dc6d1236..9448f1930e 100644 --- a/src/systems/parameter_buffer.jl +++ b/src/systems/parameter_buffer.jl @@ -23,7 +23,7 @@ dependent systems. It is only required if the symbolic expressions also use the variable of the system. This requires that `complete` has been called on the system (usually via -`structural_simplify` or `@mtkcompile`) and the keyword `split = true` was passed (which is +`mtkcompile` or `@mtkcompile`) and the keyword `split = true` was passed (which is the default behavior). """ function MTKParameters( diff --git a/src/systems/system.jl b/src/systems/system.jl index 1587f85b75..5da5d9db79 100644 --- a/src/systems/system.jl +++ b/src/systems/system.jl @@ -739,7 +739,7 @@ end function Base.showerror(io::IO, err::SystemNotCompleteError) print(io, """ - A completed system is required. Call `complete` or `structural_simplify` on the \ + A completed system is required. Call `complete` or `mtkcompile` on the \ system before creating a `$(err.obj)`. """) end diff --git a/src/systems/systems.jl b/src/systems/systems.jl index 13ee0767cf..13a62f7915 100644 --- a/src/systems/systems.jl +++ b/src/systems/systems.jl @@ -19,14 +19,14 @@ topological sort of the observed equations in `sys`. + `inputs`, `outputs` and `disturbance_inputs` are passed as keyword arguments.` All inputs` get converted to parameters and are allowed to be unconnected, allowing models where `n_unknowns = n_equations - n_inputs`. + `sort_eqs=true` controls whether equations are sorted lexicographically before simplification or not. """ -function structural_simplify( +function mtkcompile( sys::AbstractSystem; additional_passes = [], simplify = false, split = true, allow_symbolic = false, allow_parameter = true, conservative = false, fully_determined = true, inputs = Any[], outputs = Any[], disturbance_inputs = Any[], kwargs...) isscheduled(sys) && throw(RepeatedStructuralSimplificationError()) - newsys′ = __structural_simplify(sys; simplify, + newsys′ = __mtkcompile(sys; simplify, allow_symbolic, allow_parameter, conservative, fully_determined, inputs, outputs, disturbance_inputs, kwargs...) @@ -51,7 +51,7 @@ function structural_simplify( end end -function __structural_simplify(sys::AbstractSystem; simplify = false, +function __mtkcompile(sys::AbstractSystem; simplify = false, inputs = Any[], outputs = Any[], disturbance_inputs = Any[], sort_eqs = true, @@ -84,7 +84,7 @@ function __structural_simplify(sys::AbstractSystem; simplify = false, end end if isempty(brown_vars) - return structural_simplify!( + return mtkcompile!( state; simplify, inputs, outputs, disturbance_inputs, kwargs...) else Is = Int[] @@ -117,7 +117,7 @@ function __structural_simplify(sys::AbstractSystem; simplify = false, for (i, v) in enumerate(fullvars) if !iszero(new_idxs[i]) && invview(var_to_diff)[i] === nothing] - ode_sys = structural_simplify( + ode_sys = mtkcompile( sys; simplify, inputs, outputs, disturbance_inputs, kwargs...) eqs = equations(ode_sys) sorted_g_rows = zeros(Num, length(eqs), size(g, 2)) @@ -184,7 +184,7 @@ function simplify_optimization_system(sys::System; split = true, kwargs...) end econs = fast_substitute.(econs, (irreducible_subs,)) nlsys = System(econs, dvs, parameters(sys); name = :___tmp_nlsystem) - snlsys = structural_simplify(nlsys; kwargs..., fully_determined = false) + snlsys = mtkcompile(nlsys; kwargs..., fully_determined = false) obs = observed(snlsys) subs = Dict(eq.lhs => eq.rhs for eq in observed(snlsys)) seqs = equations(snlsys) @@ -231,7 +231,7 @@ end """ $(TYPEDSIGNATURES) -Given a system that has been simplified via `structural_simplify`, return a `Dict` mapping +Given a system that has been simplified via `mtkcompile`, return a `Dict` mapping variables of the system to equations that are used to solve for them. This includes observed variables. @@ -247,7 +247,7 @@ function map_variables_to_equations(sys::AbstractSystem; rename_dummy_derivative end ts = get_tearing_state(sys) if ts === nothing - throw(ArgumentError("`map_variables_to_equations` requires a simplified system. Call `structural_simplify` on the system before calling this function.")) + throw(ArgumentError("`map_variables_to_equations` requires a simplified system. Call `mtkcompile` on the system before calling this function.")) end dummy_sub = Dict() diff --git a/src/systems/systemstructure.jl b/src/systems/systemstructure.jl index e8d81e0820..16551008e9 100644 --- a/src/systems/systemstructure.jl +++ b/src/systems/systemstructure.jl @@ -24,7 +24,7 @@ function quick_cancel_expr(expr) kws...))(expr) end -export SystemStructure, TransformationState, TearingState, structural_simplify! +export SystemStructure, TransformationState, TearingState, mtkcompile! export isdiffvar, isdervar, isalgvar, isdiffeq, algeqs, is_only_discrete export dervars_range, diffvars_range, algvars_range export DiffGraph, complete! @@ -695,7 +695,7 @@ function Base.show(io::IO, mime::MIME"text/plain", ms::MatchedSystemStructure) printstyled(io, " SelectedState") end -function structural_simplify!(state::TearingState; simplify = false, +function mtkcompile!(state::TearingState; simplify = false, check_consistency = true, fully_determined = true, warn_initialize_determined = true, inputs = Any[], outputs = Any[], disturbance_inputs = Any[], @@ -723,13 +723,13 @@ function structural_simplify!(state::TearingState; simplify = false, state.structure.only_discrete = true end - sys = _structural_simplify!(state; simplify, check_consistency, + sys = _mtkcompile!(state; simplify, check_consistency, inputs, outputs, disturbance_inputs, fully_determined, kwargs...) return sys end -function _structural_simplify!(state::TearingState; simplify = false, +function _mtkcompile!(state::TearingState; simplify = false, check_consistency = true, fully_determined = true, warn_initialize_determined = false, dummy_derivative = true, inputs = Any[], outputs = Any[], diff --git a/src/utils.jl b/src/utils.jl index 13ebb28b6a..4a183fd83f 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -158,7 +158,7 @@ function check_lhs(eq::Equation, op, dvs::Set) v = unwrap(eq.lhs) _iszero(v) && return (operation(v) isa op && only(arguments(v)) in dvs) && return - error("$v is not a valid LHS. Please run structural_simplify before simulation.") + error("$v is not a valid LHS. Please run mtkcompile before simulation.") end check_lhs(eqs, op, dvs::Set) = for eq in eqs @@ -322,7 +322,7 @@ Throw error when difference/derivative operation occurs in the R.H.S. optext = "derivative" end msg = "The $optext variable must be isolated to the left-hand " * - "side of the equation like `$opvar ~ ...`. You may want to use `structural_simplify` or the DAE form.\nGot $eq." + "side of the equation like `$opvar ~ ...`. You may want to use `mtkcompile` or the DAE form.\nGot $eq." throw(InvalidSystemException(msg)) end @@ -358,10 +358,10 @@ function check_operator_variables(eqs, op::T) where {T} is_tmp_fine = iszero(nd) end is_tmp_fine || - error("The LHS cannot contain nondifferentiated variables. Please run `structural_simplify` or use the DAE form.\nGot $eq") + error("The LHS cannot contain nondifferentiated variables. Please run `mtkcompile` or use the DAE form.\nGot $eq") for v in tmp v in ops && - error("The LHS operator must be unique. Please run `structural_simplify` or use the DAE form. $v appears in LHS more than once.") + error("The LHS operator must be unique. Please run `mtkcompile` or use the DAE form. $v appears in LHS more than once.") push!(ops, v) end empty!(tmp) diff --git a/test/accessor_functions.jl b/test/accessor_functions.jl index 707b928e75..ba6aae27a0 100644 --- a/test/accessor_functions.jl +++ b/test/accessor_functions.jl @@ -68,10 +68,10 @@ let sys_mid2_comp = complete(sys_mid2) sys_mid1_comp = complete(sys_mid1) sys_top_comp = complete(sys_top) - sys_bot_ss = structural_simplify(sys_bot) - sys_mid2_ss = structural_simplify(sys_mid2) - sys_mid1_ss = structural_simplify(sys_mid1) - sys_top_ss = structural_simplify(sys_top) + sys_bot_ss = mtkcompile(sys_bot) + sys_mid2_ss = mtkcompile(sys_mid2) + sys_mid1_ss = mtkcompile(sys_mid1) + sys_top_ss = mtkcompile(sys_top) # Checks `parameters1. @test all_sets_equal(parameters.([sys_bot, sys_bot_comp, sys_bot_ss])..., [d, p_bot]) @@ -98,7 +98,7 @@ let @test all(sym_issubset(parameters_toplevel(sys), get_ps(sys)) for sys in [sys_bot, sys_mid2, sys_mid1, sys_top]) - # Checks `unknowns`. O(t) is eliminated by `structural_simplify` and + # Checks `unknowns`. O(t) is eliminated by `mtkcompile` and # must be considered separately. @test all_sets_equal(unknowns.([sys_bot, sys_bot_comp])..., [O, Y, X_bot]) @test all_sets_equal(unknowns.([sys_bot_ss])..., [Y, X_bot]) diff --git a/test/analysis_points.jl b/test/analysis_points.jl index 54cf0004c6..bb233db5e8 100644 --- a/test/analysis_points.jl +++ b/test/analysis_points.jl @@ -67,7 +67,7 @@ sys = System(eqs, t, systems = [P, C], name = :hej) nonamespace_sys = toggle_namespacing(nested_sys, false) @testset "simplifies and solves" begin - ssys = structural_simplify(sys) + ssys = mtkcompile(sys) prob = ODEProblem(ssys, [P.x => 1], (0, 10)) sol = solve(prob, Rodas5()) @test norm(sol.u[1]) >= 1 diff --git a/test/basic_transformations.jl b/test/basic_transformations.jl index 50f4c0feaf..661b508861 100644 --- a/test/basic_transformations.jl +++ b/test/basic_transformations.jl @@ -51,8 +51,8 @@ end M1 = System(eqs, t; initialization_eqs, name = :M) M2 = change_independent_variable(M1, s) - M1 = structural_simplify(M1; allow_symbolic = true) - M2 = structural_simplify(M2; allow_symbolic = true) + M1 = mtkcompile(M1; allow_symbolic = true) + M2 = mtkcompile(M2; allow_symbolic = true) prob1 = ODEProblem(M1, [M1.s => 1.0], (1.0, 4.0), []) prob2 = ODEProblem(M2, [], (1.0, 2.0), []) sol1 = solve(prob1, Tsit5(); reltol = 1e-10, abstol = 1e-10) @@ -101,9 +101,9 @@ end extraeqs = [Differential(M2.a)(b) ~ exp(-b), M2.a ~ exp(b)] M3 = change_independent_variable(M2, b, extraeqs) - M1 = structural_simplify(M1) - M2 = structural_simplify(M2; allow_symbolic = true) - M3 = structural_simplify(M3; allow_symbolic = true) + M1 = mtkcompile(M1) + M2 = mtkcompile(M2; allow_symbolic = true) + M3 = mtkcompile(M3; allow_symbolic = true) @test length(unknowns(M3)) == length(unknowns(M2)) == length(unknowns(M1)) - 1 end @@ -121,7 +121,7 @@ end @parameters g=9.81 v # gravitational acceleration and constant horizontal velocity Mt = System([D(D(y)) ~ -g, D(x) ~ v], t; name = :M) # gives (x, y) as function of t, ... Mx = change_independent_variable(Mt, x; add_old_diff = true) # ... but we want y as a function of x - Mx = structural_simplify(Mx; allow_symbolic = true) + Mx = mtkcompile(Mx; allow_symbolic = true) Dx = Differential(Mx.x) u0 = [Mx.y => 0.0, Dx(Mx.y) => 1.0, Mx.t => 0.0] p = [v => 10.0] @@ -135,7 +135,7 @@ end @parameters g = 9.81 # gravitational acceleration Mt = System([D(D(y)) ~ -g, D(D(x)) ~ 0], t; name = :M) # gives (x, y) as function of t, ... Mx = change_independent_variable(Mt, x; add_old_diff = true) # ... but we want y as a function of x - Mx = structural_simplify(Mx; allow_symbolic = true) + Mx = mtkcompile(Mx; allow_symbolic = true) Dx = Differential(Mx.x) u0 = [Mx.y => 0.0, Dx(Mx.y) => 1.0, Mx.t => 0.0, Mx.xˍt => 10.0] prob = ODEProblem(Mx, u0, (0.0, 20.0), []) # 1 = dy/dx = (dy/dt)/(dx/dt) means equal initial horizontal and vertical velocities @@ -153,7 +153,7 @@ end ] M1 = System(eqs, t; name = :M) M2 = change_independent_variable(M1, x; add_old_diff = true) - @test_nowarn structural_simplify(M2) + @test_nowarn mtkcompile(M2) # Compare to pen-and-paper result @variables x xˍt(x) xˍt(x) y(x) t(x) @@ -199,7 +199,7 @@ end ]) _f = LinearInterpolation([1.0, 1.0], [-100.0, +100.0]) # constant value 1 - M2s = structural_simplify(M2; allow_symbolic = true) + M2s = mtkcompile(M2; allow_symbolic = true) prob = ODEProblem(M2s, [M2s.y => 0.0], (1.0, 4.0), [fc => _f, f => _f]) sol = solve(prob, Tsit5(); abstol = 1e-5) @test isapprox(sol(4.0, idxs = M2.y), 12.0; atol = 1e-5) # Anal solution is D(y) ~ 12 => y(t) ~ 12*t + C => y(x) ~ 12*√(x) + C. With y(x=1)=0 => 12*(√(x)-1), so y(x=4) ~ 12 @@ -208,7 +208,7 @@ end @testset "Change independent variable (errors)" begin @variables x(t) y z(y) w(t) v(t) M = System([D(x) ~ 1, v ~ x], t; name = :M) - Ms = structural_simplify(M) + Ms = mtkcompile(M) @test_throws "structurally simplified" change_independent_variable(Ms, y) @test_throws "not a function of" change_independent_variable(M, y) @test_throws "not a function of" change_independent_variable(M, z) @@ -224,7 +224,7 @@ end @parameters g=9.81 [unit = u"m * s^-2"] # gravitational acceleration Mt = System([D_units(D_units(y)) ~ -g, D_units(D_units(x)) ~ 0], t_units; name = :M) # gives (x, y) as function of t, ... Mx = change_independent_variable(Mt, x; add_old_diff = true) # ... but we want y as a function of x - Mx = structural_simplify(Mx; allow_symbolic = true) + Mx = mtkcompile(Mx; allow_symbolic = true) Dx = Differential(Mx.x) u0 = [Mx.y => 0.0, Dx(Mx.y) => 1.0, Mx.t_units => 0.0, Mx.xˍt_units => 10.0] prob = ODEProblem(Mx, u0, (0.0, 20.0), []) # 1 = dy/dx = (dy/dt)/(dx/dt) means equal initial horizontal and vertical velocities @@ -281,7 +281,7 @@ end @named sys = ConnectSys() sys = complete(sys; flatten = false) new_sys = change_independent_variable(sys, sys.y; add_old_diff = true) - ss = structural_simplify(new_sys; allow_symbolic = true) + ss = mtkcompile(new_sys; allow_symbolic = true) prob = ODEProblem(ss, [ss.t => 0.0, ss.x => 1.0], (0.0, 1.0)) sol = solve(prob, Tsit5(); reltol = 1e-5) @test all(isapprox.(sol[ss.t], sol[ss.y]; atol = 1e-10)) @@ -298,7 +298,7 @@ end @named sys = System(eqs, t) sys = complete(sys) new_sys = change_independent_variable(sys, sys.x; add_old_diff = true) - ss_new_sys = structural_simplify(new_sys; allow_symbolic = true) + ss_new_sys = mtkcompile(new_sys; allow_symbolic = true) u0 = [new_sys.y => 0.5, new_sys.t => 0.0] prob = ODEProblem(ss_new_sys, u0, (0.0, 0.5), []) sol = solve(prob, Tsit5(); reltol = 1e-5) diff --git a/test/clock.jl b/test/clock.jl index 80f7cd6af1..7fc9e7ddd2 100644 --- a/test/clock.jl +++ b/test/clock.jl @@ -65,10 +65,10 @@ By inference: ci, varmap = infer_clocks(sys) eqmap = ci.eq_domain tss, inputs, continuous_id = ModelingToolkit.split_system(deepcopy(ci)) -sss = ModelingToolkit._structural_simplify!( +sss = ModelingToolkit._mtkcompile!( deepcopy(tss[continuous_id]), inputs = inputs[continuous_id], outputs = []) @test equations(sss) == [D(x) ~ u - x] -sss = ModelingToolkit._structural_simplify!( +sss = ModelingToolkit._mtkcompile!( deepcopy(tss[1]), inputs = inputs[1], outputs = []) @test isempty(equations(sss)) d = Clock(dt) @@ -116,7 +116,7 @@ eqs = [yd ~ Sample(dt)(y) D(x) ~ -x + u y ~ x] @named sys = System(eqs, t) -@test_throws ModelingToolkit.HybridSystemNotSupportedException ss=structural_simplify(sys); +@test_throws ModelingToolkit.HybridSystemNotSupportedException ss=mtkcompile(sys); @test_skip begin Tf = 1.0 @@ -129,7 +129,7 @@ eqs = [yd ~ Sample(dt)(y) [kp => 1.0; ud(k - 1) => 2.1; ud(k - 2) => 2.0]) # recreate problem to empty saved values sol = solve(prob, Tsit5(), kwargshandle = KeywordArgSilent) - ss_nosplit = structural_simplify(sys; split = false) + ss_nosplit = mtkcompile(sys; split = false) prob_nosplit = ODEProblem(ss_nosplit, [x => 0.1], (0.0, Tf), [kp => 1.0; ud(k - 1) => 2.1; ud(k - 2) => 2.0]) int = init(prob_nosplit, Tsit5(); kwargshandle = KeywordArgSilent) @@ -295,8 +295,8 @@ eqs = [yd ~ Sample(dt)(y) @test varmap[y] == ContinuousClock() @test varmap[u] == ContinuousClock() - ss = structural_simplify(cl) - ss_nosplit = structural_simplify(cl; split = false) + ss = mtkcompile(cl) + ss_nosplit = mtkcompile(cl; split = false) if VERSION >= v"1.7" prob = ODEProblem(ss, [x => 0.0], (0.0, 1.0), [kp => 1.0]) @@ -427,7 +427,7 @@ eqs = [yd ~ Sample(dt)(y) @test varmap[_model.feedback.output.u] == d @test varmap[_model.feedback.input2.u] == d - ssys = structural_simplify(model) + ssys = mtkcompile(model) Tf = 0.2 timevec = 0:(d.dt):Tf diff --git a/test/code_generation.jl b/test/code_generation.jl index b5caacbe9a..9a3805ce21 100644 --- a/test/code_generation.jl +++ b/test/code_generation.jl @@ -70,7 +70,7 @@ end @parameters p[1:2] (f::Function)(..) @named sys = System( [D(x[0]) ~ p[1] * x[0] + x[2], D(x[1]) ~ p[2] * f(x) + x[2]], t) - sys = structural_simplify(sys, inputs = [x[2]], outputs = []) + sys = mtkcompile(sys, inputs = [x[2]], outputs = []) @test is_parameter(sys, x[2]) prob = ODEProblem(sys, [x[0] => 1.0, x[1] => 1.0], (0.0, 1.0), [p => ones(2), f => sum, x[2] => 2.0]) diff --git a/test/components.jl b/test/components.jl index 19b601a0be..689173d61f 100644 --- a/test/components.jl +++ b/test/components.jl @@ -49,9 +49,9 @@ include("common/rc_model.jl") completed_rc_model = complete(rc_model) @test isequal(completed_rc_model.resistor.n.i, resistor.n.i) @test ModelingToolkit.n_expanded_connection_equations(capacitor) == 2 - @test length(equations(structural_simplify(rc_model, allow_parameter = false))) == 2 - sys = structural_simplify(rc_model) - @test_throws ModelingToolkit.RepeatedStructuralSimplificationError structural_simplify(sys) + @test length(equations(mtkcompile(rc_model, allow_parameter = false))) == 2 + sys = mtkcompile(rc_model) + @test_throws ModelingToolkit.RepeatedStructuralSimplificationError mtkcompile(sys) @test length(equations(sys)) == 1 check_contract(sys) @test !isempty(ModelingToolkit.defaults(sys)) @@ -62,7 +62,7 @@ include("common/rc_model.jl") end @testset "Outer/inner connections" begin - sys = structural_simplify(rc_model) + sys = mtkcompile(rc_model) prob = ODEProblem(sys, [sys.capacitor.v => 0.0], (0.0, 10.0)) sol = solve(prob, Rodas4()) @@ -92,7 +92,7 @@ end @named sys_inner_outer = compose(sys′, [ground, shape, source, rc_comp]) @test_nowarn show(IOBuffer(), MIME"text/plain"(), sys_inner_outer) expand_connections(sys_inner_outer, debug = true) - sys_inner_outer = structural_simplify(sys_inner_outer) + sys_inner_outer = mtkcompile(sys_inner_outer) @test !isempty(ModelingToolkit.defaults(sys_inner_outer)) u0 = [rc_comp.capacitor.v => 0.0] prob = ODEProblem(sys_inner_outer, u0, (0, 10.0), sparse = true) @@ -114,7 +114,7 @@ end include("common/serial_inductor.jl") @testset "Serial inductor" begin - sys = structural_simplify(ll_model) + sys = mtkcompile(ll_model) @test length(equations(sys)) == 2 u0 = unknowns(sys) .=> 0 @test_nowarn ODEProblem( @@ -123,7 +123,7 @@ include("common/serial_inductor.jl") sol = solve(prob, DFBDF()) @test sol.retcode == SciMLBase.ReturnCode.Success - sys2 = structural_simplify(ll2_model) + sys2 = mtkcompile(ll2_model) @test length(equations(sys2)) == 3 u0 = unknowns(sys) .=> 0 prob = ODEProblem(sys, u0, (0, 10.0)) @@ -182,7 +182,7 @@ function Circuit(; name) end @named foo = Circuit() -@test structural_simplify(foo) isa ModelingToolkit.AbstractSystem +@test mtkcompile(foo) isa ModelingToolkit.AbstractSystem # BLT tests @testset "BLT ordering" begin @@ -243,7 +243,7 @@ end @named _rc_model = System(rc_eqs, t) @named rc_model = compose(_rc_model, [resistor, capacitor, ground]) - sys = structural_simplify(rc_model) + sys = mtkcompile(rc_model) prob = ODEProblem(sys, [sys.c1.v => 0.0], (0, 10.0)) sol = solve(prob, Tsit5()) end @@ -289,7 +289,7 @@ end end @named outer = Outer() - simp = structural_simplify(outer) + simp = mtkcompile(outer) @test sort(propertynames(outer)) == [:inner, :t, :x] @test propertynames(simp) == propertynames(outer) @@ -306,7 +306,7 @@ end @test_throws ArgumentError outer.inner₊p end -@testset "`getproperty` on `structural_simplify(complete(sys))`" begin +@testset "`getproperty` on `mtkcompile(complete(sys))`" begin @mtkmodel Foo begin @variables begin x(t) @@ -322,7 +322,7 @@ end end @named bar = Bar() cbar = complete(bar) - ss = structural_simplify(cbar) + ss = mtkcompile(cbar) @test isequal(cbar.foo.x, ss.foo.x) end diff --git a/test/constants.jl b/test/constants.jl index 49181faf57..ec98dab9b4 100644 --- a/test/constants.jl +++ b/test/constants.jl @@ -15,12 +15,12 @@ eqs = [D(x) ~ a] prob = ODEProblem(complete(sys), [0], [0.0, 1.0], []) sol = solve(prob, Tsit5()) -# Test structural_simplify substitutions & observed values +# Test mtkcompile substitutions & observed values eqs = [D(x) ~ 1, w ~ a] @named sys = System(eqs, t) # Now eliminate the constants first -simp = structural_simplify(sys) +simp = mtkcompile(sys) @test equations(simp) == [D(x) ~ 1.0] #Constant with units @@ -33,7 +33,7 @@ UMT.get_unit(β) D = Differential(t) eqs = [D(x) ~ β] @named sys = System(eqs, t) -simp = structural_simplify(sys) +simp = mtkcompile(sys) @testset "Issue#3044" begin @constants h diff --git a/test/dde.jl b/test/dde.jl index 8f26b6c431..211b59075c 100644 --- a/test/dde.jl +++ b/test/dde.jl @@ -119,11 +119,11 @@ eqs = [osc1.jcn ~ osc2.delx, @test ModelingToolkit.is_dde(coupledOsc2) @test !is_markovian(coupledOsc2) for coupledOsc in [coupledOsc, coupledOsc2] - local sys = structural_simplify(coupledOsc) + local sys = mtkcompile(coupledOsc) @test length(equations(sys)) == 4 @test length(unknowns(sys)) == 4 end -sys = structural_simplify(coupledOsc) +sys = mtkcompile(coupledOsc) prob = DDEProblem(sys, [], (0.0, 10.0); constant_lags = [sys.osc1.τ, sys.osc2.τ]) sol = solve(prob, MethodOfSteps(Tsit5())) obsfn = ModelingToolkit.build_explicit_observed_function( @@ -178,7 +178,7 @@ end eqs = [D(x(t)) ~ -w * x(t - τ)] @named sys = System(eqs, t) - sys = structural_simplify(sys) + sys = mtkcompile(sys) prob = DDEProblem(sys, [], @@ -191,7 +191,7 @@ end @brownian r eqs = [D(x(t)) ~ -w * x(t - τ) + r] @named sys = System(eqs, t) - sys = structural_simplify(sys) + sys = mtkcompile(sys) prob = SDDEProblem(sys, [], (0.0, 10.0), diff --git a/test/debugging.jl b/test/debugging.jl index 6b4eeabca4..0cf80fd494 100644 --- a/test/debugging.jl +++ b/test/debugging.jl @@ -6,8 +6,8 @@ using ModelingToolkit: t_nounits as t, D_nounits as D, ASSERTION_LOG_VARIABLE @brownian a @named inner_ode = System(D(x) ~ -sqrt(x), t; assertions = [(x > 0) => "ohno"]) @named inner_sde = System([D(x) ~ -10sqrt(x) + 0.01a], t; assertions = [(x > 0) => "ohno"]) -sys_ode = structural_simplify(inner_ode) -sys_sde = structural_simplify(inner_sde) +sys_ode = mtkcompile(inner_ode) +sys_sde = mtkcompile(inner_sde) SEED = 42 @testset "assertions are present in generated `f`" begin diff --git a/test/discrete_system.jl b/test/discrete_system.jl index da02d7c287..2db2aab8d3 100644 --- a/test/discrete_system.jl +++ b/test/discrete_system.jl @@ -30,7 +30,7 @@ eqs = [S ~ S(k - 1) - infection * h, # System @named sys = System(eqs, t, [S, I, R], [c, nsteps, δt, β, γ, h]) -syss = structural_simplify(sys) +syss = mtkcompile(sys) @test syss == syss df = DiscreteFunction(syss) diff --git a/test/domain_connectors.jl b/test/domain_connectors.jl index e35cee8f30..485796d585 100644 --- a/test/domain_connectors.jl +++ b/test/domain_connectors.jl @@ -148,7 +148,7 @@ esys = ModelingToolkit.expand_connections(odesys) csys = complete(odesys) -sys = structural_simplify(odesys) +sys = mtkcompile(odesys) @test length(equations(sys)) == length(unknowns(sys)) sys_defs = ModelingToolkit.defaults(sys) diff --git a/test/downstream/analysis_points.jl b/test/downstream/analysis_points.jl index 5dc0026138..21c33b2068 100644 --- a/test/downstream/analysis_points.jl +++ b/test/downstream/analysis_points.jl @@ -60,7 +60,7 @@ import ControlSystemsBase as CS filt.xd => 0.0 ]) - sys = structural_simplify(closed_loop) + sys = mtkcompile(closed_loop) prob = ODEProblem(sys, unknowns(sys) .=> 0.0, (0.0, 4.0)) sol = solve(prob, Rodas5P(), reltol = 1e-6, abstol = 1e-9) @@ -100,8 +100,8 @@ end connect(F.output, sys_inner.add.input1)] sys_outer = System(eqs, t, systems = [F, sys_inner, r], name = :outer) - # test first that the structural_simplify works correctly - ssys = structural_simplify(sys_outer) + # test first that the mtkcompile works correctly + ssys = mtkcompile(sys_outer) prob = ODEProblem(ssys, Pair[], (0, 10)) @test_nowarn solve(prob, Rodas5()) @@ -136,8 +136,8 @@ end connect(F.output, sys_inner.add.input1)] sys_outer = System(eqs, t, systems = [F, sys_inner, r], name = :outer) - # test first that the structural_simplify works correctly - ssys = structural_simplify(sys_outer) + # test first that the mtkcompile works correctly + ssys = mtkcompile(sys_outer) prob = ODEProblem(ssys, Pair[], (0, 10)) @test_nowarn solve(prob, Rodas5()) @@ -172,8 +172,8 @@ end connect(F.output, sys_inner.add.input1)] sys_outer = System(eqs, t, systems = [F, sys_inner, r], name = :outer) - # test first that the structural_simplify works correctly - ssys = structural_simplify(sys_outer) + # test first that the mtkcompile works correctly + ssys = mtkcompile(sys_outer) prob = ODEProblem(ssys, Pair[], (0, 10)) @test_nowarn solve(prob, Rodas5()) @@ -363,7 +363,7 @@ end sys_normal = normal_test_system() -prob = ODEProblem(structural_simplify(sys_normal), [], (0.0, 10.0)) +prob = ODEProblem(mtkcompile(sys_normal), [], (0.0, 10.0)) @test SciMLBase.successful_retcode(solve(prob, Rodas5P())) matrices_normal, _ = get_sensitivity(sys_normal, sys_normal.normal_inner.ap) @@ -384,7 +384,7 @@ matrices_normal, _ = get_sensitivity(sys_normal, sys_normal.normal_inner.ap) connect(inner.back.output, :ap, inner.F1.input)] @named sys = System(eqs2, t; systems = [inner, step]) - prob = ODEProblem(structural_simplify(sys), [], (0.0, 10.0)) + prob = ODEProblem(mtkcompile(sys), [], (0.0, 10.0)) @test SciMLBase.successful_retcode(solve(prob, Rodas5P())) matrices, _ = get_sensitivity(sys, sys.ap) @@ -408,7 +408,7 @@ end connect(inner.back.output.u, :ap, inner.F1.input.u)] @named sys = System(eqs2, t; systems = [inner, step]) - prob = ODEProblem(structural_simplify(sys), [], (0.0, 10.0)) + prob = ODEProblem(mtkcompile(sys), [], (0.0, 10.0)) @test SciMLBase.successful_retcode(solve(prob, Rodas5P())) matrices, _ = get_sensitivity(sys, sys.ap) @@ -432,7 +432,7 @@ end connect(inner.back.output.u, :ap, inner.F1.input.u)] @named sys = System(eqs2, t; systems = [inner, step]) - prob = ODEProblem(structural_simplify(sys), [], (0.0, 10.0)) + prob = ODEProblem(mtkcompile(sys), [], (0.0, 10.0)) @test SciMLBase.successful_retcode(solve(prob, Rodas5P())) matrices, _ = get_sensitivity(sys, sys.ap) diff --git a/test/downstream/inversemodel.jl b/test/downstream/inversemodel.jl index 0e4b1ea5eb..66da2d2ea9 100644 --- a/test/downstream/inversemodel.jl +++ b/test/downstream/inversemodel.jl @@ -111,7 +111,7 @@ end end end; @named model = InverseControlledTank() -ssys = structural_simplify(model) +ssys = mtkcompile(model) cm = complete(model) op = Dict( diff --git a/test/downstream/test_disturbance_model.jl b/test/downstream/test_disturbance_model.jl index 6da6c249a1..5f5672dc74 100644 --- a/test/downstream/test_disturbance_model.jl +++ b/test/downstream/test_disturbance_model.jl @@ -55,7 +55,7 @@ end end @named model = ModelWithInputs() # Model with load disturbance -ssys = structural_simplify(model) +ssys = mtkcompile(model) prob = ODEProblem(ssys, [], (0.0, 10.0)) sol = solve(prob, Tsit5()) # plot(sol) @@ -94,10 +94,10 @@ dist(; name) = System(1 / s; name) end @named model_with_disturbance = SystemModelWithDisturbanceModel() -# ssys = structural_simplify(open_loop(model_with_disturbance, :d)) # Open loop worked, but it's a bit awkward that we have to use it here +# ssys = mtkcompile(open_loop(model_with_disturbance, :d)) # Open loop worked, but it's a bit awkward that we have to use it here # lsys2 = named_ss(model_with_disturbance, [:u, :d1], # [P.inertia1.phi, P.inertia2.phi, P.inertia1.w, P.inertia2.w]) -ssys = structural_simplify(model_with_disturbance) +ssys = mtkcompile(model_with_disturbance) prob = ODEProblem(ssys, [], (0.0, 10.0)) sol = solve(prob, Tsit5()) @test SciMLBase.successful_retcode(sol) @@ -137,10 +137,10 @@ dist3(; name) = System(ss(1 + 10 / s, balance = false); name) end @named model_with_disturbance = SystemModelWithDisturbanceModel() -# ssys = structural_simplify(open_loop(model_with_disturbance, :d)) # Open loop worked, but it's a bit awkward that we have to use it here +# ssys = mtkcompile(open_loop(model_with_disturbance, :d)) # Open loop worked, but it's a bit awkward that we have to use it here # lsys3 = named_ss(model_with_disturbance, [:u, :d1], # [P.inertia1.phi, P.inertia2.phi, P.inertia1.w, P.inertia2.w]) -ssys = structural_simplify(model_with_disturbance) +ssys = mtkcompile(model_with_disturbance) prob = ODEProblem(ssys, [], (0.0, 10.0)) sol = solve(prob, Tsit5()) @test SciMLBase.successful_retcode(sol) diff --git a/test/dq_units.jl b/test/dq_units.jl index 4689cab859..74209c3584 100644 --- a/test/dq_units.jl +++ b/test/dq_units.jl @@ -113,24 +113,24 @@ noiseeqs = [0.1us"W" 0.1us"W" eqs = [D(L) ~ v, V ~ L^3] @named sys = System(eqs, t) -sys_simple = structural_simplify(sys) +sys_simple = mtkcompile(sys) eqs = [D(V) ~ r, V ~ L^3] @named sys = System(eqs, t) -sys_simple = structural_simplify(sys) +sys_simple = mtkcompile(sys) @variables V [unit = u"m"^3] L [unit = u"m"] @parameters v [unit = u"m/s"] r [unit = u"m"^3 / u"s"] eqs = [V ~ r * t, V ~ L^3] @named sys = System(eqs, [V, L], [t, r]) -sys_simple = structural_simplify(sys) +sys_simple = mtkcompile(sys) eqs = [L ~ v * t, V ~ L^3] @named sys = System(eqs, [V, L], [t, r, v]) -sys_simple = structural_simplify(sys) +sys_simple = mtkcompile(sys) #Jump System @parameters β [unit = u"(mol^2*s)^-1"] γ [unit = u"(mol*s)^-1"] jumpmol [ diff --git a/test/error_handling.jl b/test/error_handling.jl index 6b416d9a6e..6a552ae063 100644 --- a/test/error_handling.jl +++ b/test/error_handling.jl @@ -43,7 +43,7 @@ rc_eqs = [connect(source.p, resistor.p) connect(capacitor.n, source.n)] @named rc_model = System(rc_eqs, t, systems = [resistor, capacitor, source]) -@test_throws ModelingToolkit.ExtraVariablesSystemException structural_simplify(rc_model) +@test_throws ModelingToolkit.ExtraVariablesSystemException mtkcompile(rc_model) @named source2 = OverdefinedConstantVoltage(V = V, I = V / R) rc_eqs2 = [connect(source2.p, resistor.p) @@ -51,4 +51,4 @@ rc_eqs2 = [connect(source2.p, resistor.p) connect(capacitor.n, source2.n)] @named rc_model2 = System(rc_eqs2, t, systems = [resistor, capacitor, source2]) -@test_throws ModelingToolkit.ExtraEquationsSystemException structural_simplify(rc_model2) +@test_throws ModelingToolkit.ExtraEquationsSystemException mtkcompile(rc_model2) diff --git a/test/extensions/ad.jl b/test/extensions/ad.jl index 23374bba43..d6b0cd67e3 100644 --- a/test/extensions/ad.jl +++ b/test/extensions/ad.jl @@ -47,7 +47,7 @@ end Th0 => (4 / 11)^(1 / 3) * Tγ0, Tγ0 => (15 / π^2 * ργ0 * (2 * h)^2 / 7)^(1 / 4) / 5 ]) - sys = structural_simplify(sys) + sys = mtkcompile(sys) function x_at_0(θ) prob = ODEProblem(sys, [sys.x => 1.0], (0.0, 1.0), [sys.ργ0 => θ[1], sys.h => θ[2]]) @@ -113,7 +113,7 @@ fwd, back = ChainRulesCore.rrule(remake_buffer, sys, ps, idxs, vals) eqs = [D(D(y)) ~ -9.81] initialization_eqs = [y^2 ~ 0] # initialize y = 0 in a way that builds an initialization problem @named sys = System(eqs, t; initialization_eqs) - sys = structural_simplify(sys) + sys = mtkcompile(sys) # Find initial throw velocity that reaches exactly 10 m after 1 s dprob0 = ODEProblem(sys, [D(y) => NaN], (0.0, 1.0), []; guesses = [y => 0.0]) diff --git a/test/extensions/bifurcationkit.jl b/test/extensions/bifurcationkit.jl index 78758ae470..65c3f2eb37 100644 --- a/test/extensions/bifurcationkit.jl +++ b/test/extensions/bifurcationkit.jl @@ -97,14 +97,14 @@ end # Checks that default parameter values are accounted for. # Checks that observables (that depend on other observables, as in this case) are accounted for. let - # Creates model, and uses `structural_simplify` to generate observables. + # Creates model, and uses `mtkcompile` to generate observables. @parameters μ p=2 @variables x(t) y(t) z(t) eqs = [0 ~ μ - x^3 + 2x^2, 0 ~ p * μ - y, 0 ~ y - z] @named nsys = System(eqs, [x, y, z], [μ, p]) - nsys = structural_simplify(nsys) + nsys = mtkcompile(nsys) # Creates BifurcationProblem. bif_par = μ diff --git a/test/extensions/dynamic_optimization.jl b/test/extensions/dynamic_optimization.jl index 7893088e0e..b9a512707c 100644 --- a/test/extensions/dynamic_optimization.jl +++ b/test/extensions/dynamic_optimization.jl @@ -118,7 +118,7 @@ end cost = [-x(1.0)] # Maximize the final distance. @named block = System( [D(x(t)) ~ v(t), D(v(t)) ~ u(t)], t; costs = cost, constraints = constr) - block = structural_simplify(block; inputs = [u(t)]) + block = mtkcompile(block; inputs = [u(t)]) u0map = [x(t) => 0.0, v(t) => 0.0] tspan = (0.0, 1.0) @@ -166,7 +166,7 @@ end costs = [-q(tspan[2])] @named beesys = System(eqs, t; costs) - beesys = structural_simplify(beesys; inputs = [α]) + beesys = mtkcompile(beesys; inputs = [α]) u0map = [w(t) => 40, q(t) => 2] pmap = [b => 1, c => 1, μ => 1, s => 1, ν => 1, α => 1] @@ -213,7 +213,7 @@ end costs = [-h(te)] cons = [T(te) ~ 0, m(te) ~ m_c] @named rocket = System(eqs, t; costs, constraints = cons) - rocket = structural_simplify(rocket; inputs = [T(t)]) + rocket = mtkcompile(rocket; inputs = [T(t)]) u0map = [h(t) => h₀, m(t) => m₀, v(t) => 0] pmap = [ @@ -261,7 +261,7 @@ end costs = [-Symbolics.Integral(t in (0, tf))(x(t) - u(t)), -x(tf)] consolidate(u, sub) = u[1] + u[2] + sum(sub) @named rocket = System(eqs, t; costs, consolidate) - rocket = structural_simplify(rocket; inputs = [u(t)]) + rocket = mtkcompile(rocket; inputs = [u(t)]) u0map = [x(t) => 17.5] pmap = [u(t) => 0.0, tf => 8] @@ -285,7 +285,7 @@ end @named block = System( [D(x(t)) ~ v(t), D(v(t)) ~ u(t)], t; costs = cost, constraints = constr) - block = structural_simplify(block, inputs = [u(t)]) + block = mtkcompile(block, inputs = [u(t)]) u0map = [x(t) => 1.0, v(t) => 0.0] tspan = (0.0, tf) @@ -328,7 +328,7 @@ end tspan = (0, tf) @named cartpole = System(eqs, t; costs, constraints = cons) - cartpole = structural_simplify(cartpole; inputs = [u]) + cartpole = mtkcompile(cartpole; inputs = [u]) u0map = [D(x(t)) => 0.0, D(θ(t)) => 0.0, θ(t) => 0.0, x(t) => 0.0] pmap = [mₖ => 1.0, mₚ => 0.2, l => 0.5, g => 9.81, u => 0] diff --git a/test/extensions/test_infiniteopt.jl b/test/extensions/test_infiniteopt.jl index 1a811359d3..6b371a79ae 100644 --- a/test/extensions/test_infiniteopt.jl +++ b/test/extensions/test_infiniteopt.jl @@ -24,7 +24,7 @@ end model = complete(model) inputs = [model.τ] outputs = [model.y] -model = structural_simplify(model; inputs, outputs) +model = mtkcompile(model; inputs, outputs) f, dvs, psym, io_sys = ModelingToolkit.generate_control_function( model, split = false) diff --git a/test/guess_propagation.jl b/test/guess_propagation.jl index 20421346a5..0be9506bb9 100644 --- a/test/guess_propagation.jl +++ b/test/guess_propagation.jl @@ -11,7 +11,7 @@ eqs = [D(x) ~ 1 initialization_eqs = [1 ~ exp(1 + x)] @named sys = System(eqs, t; initialization_eqs) -sys = complete(structural_simplify(sys)) +sys = complete(mtkcompile(sys)) tspan = (0.0, 0.2) prob = ODEProblem(sys, [], tspan, []) @@ -28,7 +28,7 @@ eqs = [D(x) ~ 1 initialization_eqs = [1 ~ exp(1 + x)] @named sys = System(eqs, t; initialization_eqs) -sys = complete(structural_simplify(sys)) +sys = complete(mtkcompile(sys)) tspan = (0.0, 0.2) prob = ODEProblem(sys, [], tspan, []) @@ -46,7 +46,7 @@ eqs = [D(x) ~ a] initialization_eqs = [1 ~ exp(1 + x)] @named sys = System(eqs, t; initialization_eqs) -sys = complete(structural_simplify(sys)) +sys = complete(mtkcompile(sys)) tspan = (0.0, 0.2) prob = ODEProblem(sys, [], tspan, []) @@ -66,7 +66,7 @@ eqs = [D(x) ~ a, initialization_eqs = [1 ~ exp(1 + x)] @named sys = System(eqs, t; initialization_eqs) -sys = complete(structural_simplify(sys)) +sys = complete(mtkcompile(sys)) tspan = (0.0, 0.2) prob = ODEProblem(sys, [], tspan, []) diff --git a/test/hierarchical_initialization_eqs.jl b/test/hierarchical_initialization_eqs.jl index 229c71f9b7..fef9953438 100644 --- a/test/hierarchical_initialization_eqs.jl +++ b/test/hierarchical_initialization_eqs.jl @@ -140,7 +140,7 @@ syslist = ModelingToolkit.get_systems(model) @test length(ModelingToolkit.initialization_equations(model)) == 2 u0 = [] -prob = ODEProblem(structural_simplify(model), u0, (0.0, 10.0)) +prob = ODEProblem(mtkcompile(model), u0, (0.0, 10.0)) sol = solve(prob, Rodas5P()) @test length(sol.u[end]) == 2 @test length(equations(prob.f.initializeprob.f.sys)) == 0 diff --git a/test/if_lifting.jl b/test/if_lifting.jl index c203df8f22..1fcb5947e4 100644 --- a/test/if_lifting.jl +++ b/test/if_lifting.jl @@ -13,9 +13,9 @@ using ModelingToolkit: t_nounits as t, D_nounits as D, IfLifting, no_if_lift end end @named sys = SimpleAbs() - ss1 = structural_simplify(sys) + ss1 = mtkcompile(sys) @test length(equations(ss1)) == 1 - ss2 = structural_simplify(sys, additional_passes = [IfLifting]) + ss2 = mtkcompile(sys, additional_passes = [IfLifting]) @test length(equations(ss2)) == 1 @test length(parameters(ss2)) == 1 @test operation(only(equations(ss2)).rhs) === ifelse @@ -71,7 +71,7 @@ end end @named sys = BigModel() - ss = structural_simplify(sys, additional_passes = [IfLifting]) + ss = mtkcompile(sys, additional_passes = [IfLifting]) ps = parameters(ss) @test length(ps) == 9 diff --git a/test/initializationsystem.jl b/test/initializationsystem.jl index 2c4f0f8bf6..8c1634e4e4 100644 --- a/test/initializationsystem.jl +++ b/test/initializationsystem.jl @@ -416,7 +416,7 @@ sol = solve(prob, Tsit5()) D(z) ~ x * y - β * z] @named sys = System(eqs, t) - sys = structural_simplify(sys) + sys = mtkcompile(sys) u0 = [D(x) => 2.0, x => 1.0, @@ -445,7 +445,7 @@ eqs = [D(x) ~ α * x - β * x * y z ~ x + y] @named sys = System(eqs, t) -simpsys = structural_simplify(sys) +simpsys = mtkcompile(sys) tspan = (0.0, 10.0) prob = ODEProblem(simpsys, [D(x) => 0.0, y => 0.0], tspan, guesses = [x => 0.0]) @@ -478,7 +478,7 @@ prob = ODEProblem(pend, [x => 1], (0.0, 1.5), [g => 1], guesses = [λ => 0, y => 1], initialization_eqs = [y ~ 1]) unsimp = generate_initializesystem(pend; u0map = [x => 1], initialization_eqs = [y ~ 1]) -sys = structural_simplify(unsimp; fully_determined = false) +sys = mtkcompile(unsimp; fully_determined = false) @test length(equations(sys)) in (3, 4) # could be either depending on tearing # Extend two systems with initialization equations and guesses @@ -494,7 +494,7 @@ sys = extend(sysx, sysy) @testset "Error on missing defaults" begin @variables x(t) y(t) @named sys = System([x^2 + y^2 ~ 25, D(x) ~ 1], t) - ssys = structural_simplify(sys) + ssys = mtkcompile(sys) @test_throws ModelingToolkit.MissingVariablesError ODEProblem( ssys, [x => 3], (0, 1), []) # y should have a guess end @@ -505,7 +505,7 @@ end # system 1 should solve to x = 1 ics1 = [x => 1] - sys1 = System([D(x) ~ 0], t; defaults = ics1, name = :sys1) |> structural_simplify + sys1 = System([D(x) ~ 0], t; defaults = ics1, name = :sys1) |> mtkcompile prob1 = ODEProblem(sys1, [], (0.0, 1.0), []) sol1 = solve(prob1, Tsit5()) @test all(sol1[x] .== 1) @@ -514,7 +514,7 @@ end sys2 = extend( sys1, System([D(y) ~ 0], t; initialization_eqs = [y ~ 2], name = :sys2) - ) |> structural_simplify + ) |> mtkcompile ics2 = unknowns(sys1) .=> 2 # should be equivalent to "ics2 = [x => 2]" prob2 = ODEProblem(sys2, ics2, (0.0, 1.0), []; fully_determined = true) sol2 = solve(prob2, Tsit5()) @@ -526,12 +526,12 @@ end @variables x(t) sys = System( [D(D(x)) ~ 0], t; initialization_eqs = [x ~ 0, D(x) ~ 1], name = :sys) |> - structural_simplify + mtkcompile @test_nowarn ODEProblem(sys, [], (0.0, 1.0), []) sys = System( [D(D(x)) ~ 0], t; initialization_eqs = [x ~ 0, D(D(x)) ~ 0], name = :sys) |> - structural_simplify + mtkcompile @test_nowarn ODEProblem(sys, [D(x) => 1.0], (0.0, 1.0), []) end @@ -542,7 +542,7 @@ end sys = System( [D(D(x)) ~ 0], t; initialization_eqs = [D(x)^2 ~ 1, x ~ 0], guesses = [D(x) => sign], name = :sys - ) |> structural_simplify + ) |> mtkcompile prob = ODEProblem(sys, [], (0.0, 1.0), []) sol = solve(prob, Tsit5()) @test sol(1.0, idxs = sys.x) ≈ sign # system with D(x(0)) = ±1 should solve to x(1) = ±1 @@ -583,7 +583,7 @@ sol = solve(oprob_2nd_order_2, Rosenbrock23()) # retcode: Success @testset "Vector in initial conditions" begin @variables x(t)[1:5] y(t)[1:5] @named sys = System([D(x) ~ x, D(y) ~ y], t; initialization_eqs = [y ~ -x]) - sys = structural_simplify(sys) + sys = mtkcompile(sys) prob = ODEProblem(sys, [sys.x => ones(5)], (0.0, 1.0), []) sol = solve(prob, Tsit5(), reltol = 1e-4) @test all(sol(1.0, idxs = sys.x) .≈ +exp(1)) && all(sol(1.0, idxs = sys.y) .≈ -exp(1)) @@ -1170,7 +1170,7 @@ end end model = dc_motor() - sys = structural_simplify(model) + sys = mtkcompile(model) prob = ODEProblem(sys, [sys.L1.i => 0.0], (0, 6.0)) @@ -1275,7 +1275,7 @@ end @parameters a = 1 @named sys = System([D(x) ~ 0, D(y) ~ x + a], t; initialization_eqs = [y ~ a]) - ssys = structural_simplify(sys) + ssys = mtkcompile(sys) prob = ODEProblem(ssys, [], (0, 1), []) @test SciMLBase.successful_retcode(solve(prob)) @@ -1375,7 +1375,7 @@ end continuous_events = [ [y ~ 0.5] => (; f = stop!) ]) - sys = structural_simplify(sys) + sys = mtkcompile(sys) prob0 = ODEProblem(sys, [x => NaN], (0.0, 1.0), []) # final_x(x0) is equivalent to x0 + 0.5 diff --git a/test/input_output_handling.jl b/test/input_output_handling.jl index ec71c3e9ff..c295546248 100644 --- a/test/input_output_handling.jl +++ b/test/input_output_handling.jl @@ -7,10 +7,10 @@ using ModelingToolkit: t_nounits as t, D_nounits as D @variables xx(t) some_input(t) [input = true] eqs = [D(xx) ~ some_input] @named model = System(eqs, t) -@test_throws ExtraVariablesSystemException structural_simplify(model) +@test_throws ExtraVariablesSystemException mtkcompile(model) if VERSION >= v"1.8" err = "In particular, the unset input(s) are:\n some_input(t)" - @test_throws err structural_simplify(model) + @test_throws err mtkcompile(model) end # Test input handling @@ -50,7 +50,7 @@ end @test !is_bound(sys31, sys1.v[2]) # simplification turns input variables into parameters -ssys = structural_simplify(sys, inputs = [u], outputs = []) +ssys = mtkcompile(sys, inputs = [u], outputs = []) @test ModelingToolkit.isparameter(unbound_inputs(ssys)[]) @test !is_bound(ssys, u) @test u ∈ Set(unbound_inputs(ssys)) @@ -88,7 +88,7 @@ fsys4 = flatten(sys4) @variables x(t) y(t) [output = true] @test isoutput(y) @named sys = System([D(x) ~ -x, y ~ x], t) # both y and x are unbound -syss = structural_simplify(sys, outputs = [y]) # This makes y an observed variable +syss = mtkcompile(sys, outputs = [y]) # This makes y an observed variable @named sys2 = System([D(x) ~ -sys.x, y ~ sys.y], t, systems = [sys]) @@ -106,7 +106,7 @@ syss = structural_simplify(sys, outputs = [y]) # This makes y an observed variab @test isequal(unbound_outputs(sys2), [y]) @test isequal(bound_outputs(sys2), [sys.y]) -syss = structural_simplify(sys2, outputs = [sys.y]) +syss = mtkcompile(sys2, outputs = [sys.y]) @test !is_bound(syss, y) @test !is_bound(syss, x) @@ -165,7 +165,7 @@ end ] @named sys = System(eqs, t) - sys = structural_simplify(sys, inputs = [u]) + sys = mtkcompile(sys, inputs = [u]) f, dvs, ps, io_sys = ModelingToolkit.generate_control_function(sys; simplify, split) @test isequal(dvs[], x) @@ -183,7 +183,7 @@ end ] @named sys = System(eqs, t) - sys = structural_simplify(sys, inputs = [u], disturbance_inputs = [d]) + sys = mtkcompile(sys, inputs = [u], disturbance_inputs = [d]) f, dvs, ps, io_sys = ModelingToolkit.generate_control_function(sys; simplify, split) @test isequal(dvs[], x) @@ -201,7 +201,7 @@ end ] @named sys = System(eqs, t) - sys = structural_simplify(sys, inputs = [u], disturbance_inputs = [d]) + sys = mtkcompile(sys, inputs = [u], disturbance_inputs = [d]) f, dvs, ps, io_sys = ModelingToolkit.generate_control_function( sys; simplify, split, disturbance_argument = true) @@ -267,7 +267,7 @@ eqs = [connect_sd(sd, mass1, mass2) @named _model = System(eqs, t) @named model = compose(_model, mass1, mass2, sd); -model = structural_simplify(model, inputs = [u]) +model = mtkcompile(model, inputs = [u]) f, dvs, ps, io_sys = ModelingToolkit.generate_control_function(model, simplify = true) @test length(dvs) == 4 p = MTKParameters(io_sys, [io_sys.u => NaN]) @@ -283,7 +283,7 @@ i = findfirst(isequal(u[1]), out) @variables x(t) u(t) [input = true] eqs = [D(x) ~ u] @named sys = System(eqs, t) -@test_nowarn structural_simplify(sys, inputs = [u], outputs = []) +@test_nowarn mtkcompile(sys, inputs = [u], outputs = []) #= ## Disturbance input handling @@ -368,7 +368,7 @@ eqs = [D(y₁) ~ -k₁ * y₁ + k₃ * y₂ * y₃ + u1 @named sys = System(eqs, t) m_inputs = [u[1], u[2]] m_outputs = [y₂] -sys_simp = structural_simplify(sys, inputs = m_inputs, outputs = m_outputs) +sys_simp = mtkcompile(sys, inputs = m_inputs, outputs = m_outputs) @test isequal(unknowns(sys_simp), collect(x[1:2])) @test length(inputs(sys_simp)) == 2 @@ -386,7 +386,7 @@ sys_simp = structural_simplify(sys, inputs = m_inputs, outputs = m_outputs) ], t, systems = [int, gain, c, fb]) -sys = structural_simplify(model) +sys = mtkcompile(model) @test length(unknowns(sys)) == length(equations(sys)) == 1 ## Disturbance models when plant has multiple inputs @@ -435,7 +435,7 @@ matrices = ModelingToolkit.reorder_unknowns( ] @named sys = System(eqs, t) - sys = structural_simplify(sys, inputs = [u]) + sys = mtkcompile(sys, inputs = [u]) (; io_sys,) = ModelingToolkit.generate_control_function(sys, simplify = true) obsfn = ModelingToolkit.build_explicit_observed_function( io_sys, [x + u * t]; inputs = [u]) @@ -458,7 +458,7 @@ end @parameters p(::Real) = (x -> 2x) eqs = [D(x) ~ -x + p(u)] @named sys = System(eqs, t) - sys = structural_simplify(sys, inputs = [u]) + sys = mtkcompile(sys, inputs = [u]) f, dvs, ps, io_sys = ModelingToolkit.generate_control_function(sys) p = MTKParameters(io_sys, []) u = [1.0] diff --git a/test/jumpsystem.jl b/test/jumpsystem.jl index fca3d1a981..95cc29e7b0 100644 --- a/test/jumpsystem.jl +++ b/test/jumpsystem.jl @@ -279,7 +279,7 @@ ps = [k => 1.0] @test_nowarn jp3 = JumpProblem(js3, u0, tspan, ps; aggregator = Direct()) @test_nowarn jp4 = JumpProblem(js4, u0, tspan; aggregator = Direct()) -# Ensure `structural_simplify` (and `@mtkcompile`) works on JumpSystem (by doing nothing) +# Ensure `mtkcompile` (and `@mtkcompile`) works on JumpSystem (by doing nothing) # Issue#2558 @parameters k @variables X(t) diff --git a/test/modelingtoolkitize.jl b/test/modelingtoolkitize.jl index 837f2fabb6..f6b4dc8a8d 100644 --- a/test/modelingtoolkitize.jl +++ b/test/modelingtoolkitize.jl @@ -438,7 +438,7 @@ prob = NonlinearLeastSquaresProblem( NonlinearFunction(nlls!, resid_prototype = zeros(3)), u0) sys = modelingtoolkitize(prob) @test length(equations(sys)) == 3 -@test length(equations(structural_simplify(sys; fully_determined = false))) == 0 +@test length(equations(mtkcompile(sys; fully_determined = false))) == 0 @testset "`modelingtoolkitize(::SDEProblem)` sets defaults" begin function sdeg!(du, u, p, t) diff --git a/test/mtkparameters.jl b/test/mtkparameters.jl index 23e4e577b9..afd97a5984 100644 --- a/test/mtkparameters.jl +++ b/test/mtkparameters.jl @@ -180,7 +180,7 @@ function level1() eqs = [D(x) ~ p1 * x - p2 * x * y D(y) ~ -p3 * y + p4 * x * y] - sys = structural_simplify(complete(System( + sys = mtkcompile(complete(System( eqs, t, name = :sys, parameter_dependencies = [y0 => 2p4]))) prob = ODEProblem{true, SciMLBase.FullSpecialize}(sys, [], (0.0, 3.0)) end @@ -194,7 +194,7 @@ function level2() eqs = [D(x) ~ p1 * x - p23[1] * x * y D(y) ~ -p23[2] * y + p4 * x * y] - sys = structural_simplify(complete(System( + sys = mtkcompile(complete(System( eqs, t, name = :sys, parameter_dependencies = [y0 => 2p4]))) prob = ODEProblem{true, SciMLBase.FullSpecialize}(sys, [], (0.0, 3.0)) end @@ -208,7 +208,7 @@ function level3() eqs = [D(x) ~ p1 * x - p23[1] * x * y D(y) ~ -p23[2] * y + p4 * x * y] - sys = structural_simplify(complete(System( + sys = mtkcompile(complete(System( eqs, t, name = :sys, parameter_dependencies = [y0 => 2p4]))) prob = ODEProblem{true, SciMLBase.FullSpecialize}(sys, [], (0.0, 3.0)) end diff --git a/test/nonlinearsystem.jl b/test/nonlinearsystem.jl index 9580d1ea0a..8575fbc36a 100644 --- a/test/nonlinearsystem.jl +++ b/test/nonlinearsystem.jl @@ -117,7 +117,7 @@ using OrdinaryDiffEq D = Differential(t) @named subsys = convert_system_indepvar(lorenz1, t) @named sys = System([D(subsys.x) ~ subsys.x + subsys.x], t, systems = [subsys]) -sys = structural_simplify(sys) +sys = mtkcompile(sys) u0 = [subsys.x => 1, subsys.z => 2.0, subsys.y => 1.0] prob = ODEProblem(sys, u0, (0, 1.0), [subsys.σ => 1, subsys.ρ => 2, subsys.β => 3]) sol = solve(prob, FBDF(), reltol = 1e-7, abstol = 1e-7) @@ -195,7 +195,7 @@ eq = [v1 ~ sin(2pi * t * h) v2 ~ i2 i1 ~ i2] @named sys = System(eq, t) -@test length(equations(structural_simplify(sys))) == 0 +@test length(equations(mtkcompile(sys))) == 0 @testset "Remake" begin @parameters a=1.0 b=1.0 c=1.0 @@ -229,7 +229,7 @@ end @named ns = System(eqs, [x, y, z], []) ns = complete(ns) vs = [unknowns(ns); parameters(ns)] - ss_mtk = structural_simplify(ns) + ss_mtk = mtkcompile(ns) prob = NonlinearProblem(ss_mtk, vs .=> 1.0) sol = solve(prob) @test_nowarn sol[unknowns(ns)] @@ -249,16 +249,16 @@ sys = @test_nowarn System(alg_eqs; name = :name) @parameters u3 u4 eqs = [u3 ~ u1 + u2, u4 ~ 2 * (u1 + u2), u3 + u4 ~ 3 * (u1 + u2)] @named ns = System(eqs, [u1, u2], [u3, u4]) -sys = structural_simplify(ns; fully_determined = false) +sys = mtkcompile(ns; fully_determined = false) @test length(unknowns(sys)) == 1 # Conservative @variables X(t) alg_eqs = [1 ~ 2X] @named ns = System(alg_eqs) -sys = structural_simplify(ns) +sys = mtkcompile(ns) @test length(equations(sys)) == 0 -sys = structural_simplify(ns; conservative = true) +sys = mtkcompile(ns; conservative = true) @test length(equations(sys)) == 1 # https://github.com/SciML/ModelingToolkit.jl/issues/2858 @@ -310,7 +310,7 @@ end -1 1/2 -1] b = [1, -2, 0] @named sys = System(A * x ~ b, [x], []) - sys = structural_simplify(sys) + sys = mtkcompile(sys) prob = NonlinearProblem(sys, unknowns(sys) .=> 0.0) sol = solve(prob) @test all(sol[x] .≈ A \ b) @@ -321,8 +321,8 @@ end @parameters p @named sys = System([x ~ 1, x^2 - p ~ 0]) for sys in [ - structural_simplify(sys, fully_determined = false), - structural_simplify(sys, fully_determined = false, split = false) + mtkcompile(sys, fully_determined = false), + mtkcompile(sys, fully_determined = false, split = false) ] @test length(equations(sys)) == 1 @test length(unknowns(sys)) == 0 @@ -397,7 +397,7 @@ end @test ModelingToolkit.iscomplete(nlsys) @test ModelingToolkit.is_split(nlsys) - sys3 = structural_simplify(sys) + sys3 = mtkcompile(sys) nlsys = NonlinearSystem(sys3) @test length(equations(nlsys)) == length(ModelingToolkit.observed(nlsys)) == 1 diff --git a/test/odesystem.jl b/test/odesystem.jl index b4884bb925..463f459df9 100644 --- a/test/odesystem.jl +++ b/test/odesystem.jl @@ -391,7 +391,7 @@ sys = complete(sys) @testset "Issue#1109" begin @variables x(t)[1:3, 1:3] @named sys = System(D(x) ~ x, t) - @test_nowarn structural_simplify(sys) + @test_nowarn mtkcompile(sys) end # Array vars @@ -402,7 +402,7 @@ ps = @parameters p[1:3] = [1, 2, 3] eqs = [collect(D.(x) .~ x) D(y) ~ norm(collect(x)) * y - x[1]] @named sys = System(eqs, t, sts, ps) -sys = structural_simplify(sys) +sys = mtkcompile(sys) @test isequal(@nonamespace(sys.x), x) @test isequal(@nonamespace(sys.y), y) @test isequal(@nonamespace(sys.p), p) @@ -574,7 +574,7 @@ let D(x[2]) ~ -x[1] - 0.5 * x[2] + k y ~ 0.9 * x[1] + x[2]] @named sys = System(eqs, t, vcat(x, [y]), [k], defaults = Dict(x .=> 0)) - sys = structural_simplify(sys) + sys = mtkcompile(sys) u0 = [0.5, 0] du0 = 0 .* copy(u0) @@ -656,7 +656,7 @@ let 0 ~ q / C - R * F] @named sys = System(eqs, t) - @test length(equations(structural_simplify(sys))) == 2 + @test length(equations(mtkcompile(sys))) == 2 end let @@ -669,7 +669,7 @@ let spm ~ 0 sph ~ a] @named sys = System(eqs, t, vars, pars) - @test_throws ModelingToolkit.ExtraEquationsSystemException structural_simplify(sys) + @test_throws ModelingToolkit.ExtraEquationsSystemException mtkcompile(sys) end # 1561 @@ -693,9 +693,9 @@ let ps = [] @named sys = System(eqs, t, u, ps) - @test_nowarn simpsys = structural_simplify(sys) + @test_nowarn simpsys = mtkcompile(sys) - sys = structural_simplify(sys) + sys = mtkcompile(sys) u0 = ModelingToolkit.missing_variable_defaults(sys) u0_expected = Pair[s => 0.0 for s in unknowns(sys)] @@ -781,7 +781,7 @@ let @named connected = System(connections, t) @named sys_con = compose(connected, sys, ctrl) - sys_simp = structural_simplify(sys_con) + sys_simp = mtkcompile(sys_con) true_eqs = [D(sys.x) ~ sys.v D(sys.v) ~ ctrl.kv * sys.v + ctrl.kx * sys.x] @test issetequal(full_equations(sys_simp), true_eqs) @@ -792,7 +792,7 @@ let @variables y(t) = 1 @parameters pp = -1 @named sys4 = System([D(x) ~ -y; D(y) ~ 1 + pp * y + x], t) - sys4s = structural_simplify(sys4) + sys4s = mtkcompile(sys4) prob = ODEProblem(sys4s, [x => 1.0, D(x) => 1.0], (0, 1.0)) @test string.(unknowns(prob.f.sys)) == ["x(t)", "y(t)"] @test string.(parameters(prob.f.sys)) == ["pp"] @@ -813,7 +813,7 @@ let @parameters pp = -1 der = Differential(t) @named sys4 = System([der(x) ~ -y; der(y) ~ 1 + pp * y + x], t) - sys4s = structural_simplify(sys4) + sys4s = mtkcompile(sys4) prob = ODEProblem(sys4s, [x => 1.0, D(x) => 1.0], (0, 1.0)) @test !isnothing(prob.f.sys) end @@ -849,7 +849,7 @@ let # Issue https://github.com/SciML/ModelingToolkit.jl/issues/2322 sys = System(eqs, t; name = :kjshdf) - sys_simp = structural_simplify(sys) + sys_simp = mtkcompile(sys) @test a ∈ keys(ModelingToolkit.defaults(sys_simp)) @@ -993,7 +993,7 @@ orig_vars = unknowns(sys) @named outer = System( [D(y) ~ sys.x + t, 0 ~ t + y - sys.x * y], t, [y], []; systems = [sys]) @test ModelingToolkit.guesses(outer)[sys.x] == 1.0 -outer = structural_simplify(outer) +outer = mtkcompile(outer) @test ModelingToolkit.get_guesses(outer)[sys.x] == 1.0 prob = ODEProblem(outer, [outer.y => 2.0], (0.0, 10.0)) int = init(prob, Rodas4()) @@ -1029,7 +1029,7 @@ end @testset "Non-1-indexed variable array (issue #2670)" begin @variables x(t)[0:1] # 0-indexed variable array @named sys = System([x[0] ~ 0.0, D(x[1]) ~ x[0]], t, [x], []) - @test_nowarn sys = structural_simplify(sys) + @test_nowarn sys = mtkcompile(sys) @test equations(sys) == [D(x[1]) ~ 0.0] end @@ -1045,7 +1045,7 @@ end @testset "ForwardDiff through ODEProblem constructor" begin @parameters P @variables x(t) - sys = structural_simplify(System([D(x) ~ P], t, [x], [P]; name = :sys)) + sys = mtkcompile(System([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]) @@ -1058,7 +1058,7 @@ end @testset "Inplace observed functions" begin @parameters P @variables x(t) - sys = structural_simplify(System([D(x) ~ P], t, [x], [P]; name = :sys)) + sys = mtkcompile(System([D(x) ~ P], t, [x], [P]; name = :sys)) obsfn = ModelingToolkit.build_explicit_observed_function( sys, [x + 1, x + P, x + t], return_inplace = true)[2] ps = ModelingToolkit.MTKParameters(sys, [P => 2.0]) @@ -1095,7 +1095,7 @@ end initialization_eqs = [x ~ T] guesses = [x => 0.0] @named sys2 = System(eqs, T; initialization_eqs, guesses) - prob2 = ODEProblem(structural_simplify(sys2), [], (1.0, 2.0), []) + prob2 = ODEProblem(mtkcompile(sys2), [], (1.0, 2.0), []) sol2 = solve(prob2) @test all(sol2[x] .== 1.0) end @@ -1127,7 +1127,7 @@ end eqs = [D(x) ~ 0, y ~ x, D(z) ~ 0] defaults = [x => 1, z => y] @named sys = System(eqs, t; defaults) - ssys = structural_simplify(sys) + ssys = mtkcompile(sys) prob = ODEProblem(ssys, [], (0.0, 1.0), []) @test prob[x] == prob[y] == prob[z] == 1.0 @@ -1136,7 +1136,7 @@ end eqs = [D(x) ~ 0, y ~ y0 / x, D(z) ~ y] defaults = [y0 => 1, x => 1, z => y] @named sys = System(eqs, t; defaults) - ssys = structural_simplify(sys) + ssys = mtkcompile(sys) prob = ODEProblem(ssys, [], (0.0, 1.0), []) @test prob[x] == prob[y] == prob[z] == 1.0 end @@ -1147,11 +1147,11 @@ end @named sys = System( [D(u) ~ (sum(u) + sum(x) + sum(p) + sum(o)) * x, o ~ prod(u) * x], t, [u..., x..., o...], [p...]) - sys1 = structural_simplify(sys, inputs = [x...], outputs = []) + sys1 = mtkcompile(sys, inputs = [x...], outputs = []) fn1, = ModelingToolkit.generate_rhs(sys1; expression = Val{false}) ps = MTKParameters(sys1, [x => 2ones(2), p => 3ones(2, 2)]) @test_nowarn fn1(ones(4), ps, 4.0) - sys2 = structural_simplify(sys, inputs = [x...], outputs = [], split = false) + sys2 = mtkcompile(sys, inputs = [x...], outputs = [], split = false) fn2, = ModelingToolkit.generate_rhs(sys2; expression = Val{false}) ps = zeros(8) setp(sys2, x)(ps, 2ones(2)) @@ -1236,10 +1236,10 @@ end @named outersys = System( [D(innersys.y) ~ innersys.y + p4], t; parameter_dependencies = [p4 ~ 3p3], defaults = [p3 => 3.0, p4 => 9.0], guesses = [p4 => 10.0], systems = [innersys]) - @test_nowarn structural_simplify(outersys) + @test_nowarn mtkcompile(outersys) @parameters p5 sys2 = substitute(outersys, [p4 => p5]) - @test_nowarn structural_simplify(sys2) + @test_nowarn mtkcompile(sys2) @test length(equations(sys2)) == 2 @test length(parameters(sys2)) == 2 @test length(full_parameters(sys2)) == 4 @@ -1261,7 +1261,7 @@ end o[2] ~ sum(p) * sum(x)] @named sys = System(eqs, t, [u..., x..., o], [p...]) - sys1 = structural_simplify(sys, inputs = [x...], outputs = [o...], split = false) + sys1 = mtkcompile(sys, inputs = [x...], outputs = [o...], split = false) @test_nowarn ModelingToolkit.build_explicit_observed_function(sys1, u; inputs = [x...]) @@ -1299,7 +1299,7 @@ end @test_nowarn ODEProblem(sys, [], (0.0, 1.0)) else @test_throws [ - r"array (equations|unknowns)", "structural_simplify", "scalarize"] ODEProblem( + r"array (equations|unknowns)", "mtkcompile", "scalarize"] ODEProblem( sys, [], (0.0, 1.0)) end end @@ -1312,7 +1312,7 @@ end @test_nowarn ODEProblem(sys, [], (0.0, 1.0)) else @test_throws [ - r"array (equations|unknowns)", "structural_simplify", "scalarize"] ODEProblem( + r"array (equations|unknowns)", "mtkcompile", "scalarize"] ODEProblem( sys, [], (0.0, 1.0)) end end diff --git a/test/optimizationsystem.jl b/test/optimizationsystem.jl index 46c796ed8d..b586d47d4e 100644 --- a/test/optimizationsystem.jl +++ b/test/optimizationsystem.jl @@ -84,7 +84,7 @@ end z ~ y - x^2 z^2 + y^2 ≲ 1.0] @named sys = OptimizationSystem(loss, [x, y, z], [a, b], constraints = cons) - sys = structural_simplify(sys) + sys = mtkcompile(sys) prob = OptimizationProblem(sys, [x => 0.0, y => 0.0, z => 0.0], [a => 1.0, b => 1.0], grad = true, hess = true, cons_j = true, cons_h = true) sol = solve(prob, IPNewton()) diff --git a/test/parameter_dependencies.jl b/test/parameter_dependencies.jl index d4c5e3e2a1..97937b8811 100644 --- a/test/parameter_dependencies.jl +++ b/test/parameter_dependencies.jl @@ -171,7 +171,7 @@ end # (https://github.com/SciML/ModelingToolkit.jl/pull/2978) @inferred ModelingToolkit.parameter_dependencies(sys1) - sys = structural_simplify(sys1) + sys = mtkcompile(sys1) prob = ODEProblem(sys, [], (0.0, 1.0)) sol = solve(prob) @@ -193,7 +193,7 @@ end eqs = [D(y) ~ i(t) + p] @named model = System(eqs, t, [y], [p, i]; parameter_dependencies = [i ~ CallableFoo(p)]) - sys = structural_simplify(model) + sys = mtkcompile(model) prob = ODEProblem(sys, [], (0.0, 1.0)) sol = solve(prob, Tsit5()) diff --git a/test/reduction.jl b/test/reduction.jl index e75b2afdee..af9e510646 100644 --- a/test/reduction.jl +++ b/test/reduction.jl @@ -30,7 +30,7 @@ eqs = [D(x) ~ σ * (y - x) lorenz1 = System(eqs, t, name = :lorenz1) -lorenz1_aliased = structural_simplify(lorenz1) +lorenz1_aliased = mtkcompile(lorenz1) io = IOBuffer(); show(io, MIME("text/plain"), lorenz1_aliased); str = String(take!(io)); @@ -74,8 +74,8 @@ __x = x # Reduced Flattened System -reduced_system = structural_simplify(connected) -reduced_system2 = structural_simplify(tearing_substitution(structural_simplify(tearing_substitution(structural_simplify(connected))))) +reduced_system = mtkcompile(connected) +reduced_system2 = mtkcompile(tearing_substitution(mtkcompile(tearing_substitution(mtkcompile(connected))))) @test isempty(setdiff(unknowns(reduced_system), unknowns(reduced_system2))) @test isequal(equations(tearing_substitution(reduced_system)), equations(reduced_system2)) @@ -133,7 +133,7 @@ let pc.y_c ~ ol.y] @named connected = System(connections, t, systems = [ol, pc]) @test equations(connected) isa Vector{Equation} - reduced_sys = structural_simplify(connected) + reduced_sys = mtkcompile(connected) ref_eqs = [D(ol.x) ~ ol.a * ol.x + ol.b * ol.u 0 ~ pc.k_P * ol.y - ol.u] #@test ref_eqs == equations(reduced_sys) @@ -144,7 +144,7 @@ let @variables x(t) @named sys = System([0 ~ D(x) + x], t, [x], []) #@test_throws ModelingToolkit.InvalidSystemException ODEProblem(sys, [1.0], (0, 10.0)) - sys = structural_simplify(sys) + sys = mtkcompile(sys) #@test_nowarn ODEProblem(sys, [1.0], (0, 10.0)) end @@ -155,7 +155,7 @@ eqs = [u1 ~ u2 u3 ~ u1 + u2 + p u3 ~ hypot(u1, u2) * p] @named sys = System(eqs, [u1, u2, u3], [p]) -reducedsys = structural_simplify(sys) +reducedsys = mtkcompile(sys) @test length(observed(reducedsys)) == 2 u0 = [u2 => 1] @@ -175,7 +175,7 @@ N = 5 A = reshape(1:(N^2), N, N) eqs = xs ~ A * xs @named sys′ = System(eqs, [xs], []) -sys = structural_simplify(sys′) +sys = mtkcompile(sys′) @test length(equations(sys)) == 3 && length(observed(sys)) == 3 # issue 958 @@ -189,7 +189,7 @@ eqs = [D(E) ~ k₋₁ * C - k₁ * E * S E₀ ~ E + C] @named sys = System(eqs, t, [E, C, S, P], [k₁, k₂, k₋₁, E₀]) -@test_throws ModelingToolkit.ExtraEquationsSystemException structural_simplify(sys) +@test_throws ModelingToolkit.ExtraEquationsSystemException mtkcompile(sys) # Example 5 from Pantelides' original paper params = collect(@parameters y1(t) y2(t)) @@ -198,7 +198,7 @@ eqs = [0 ~ x + sin(u1 + u2) D(x) ~ x + y1 cos(x) ~ sin(y2)] @named sys = System(eqs, t, sts, params) -@test_throws ModelingToolkit.InvalidSystemException structural_simplify(sys) +@test_throws ModelingToolkit.InvalidSystemException mtkcompile(sys) # issue #963 @variables v47(t) v57(t) v66(t) v25(t) i74(t) i75(t) i64(t) i71(t) v1(t) v2(t) @@ -215,7 +215,7 @@ eq = [v47 ~ v1 0 ~ i64 + i71] @named sys0 = System(eq, t) -sys = structural_simplify(sys0) +sys = mtkcompile(sys0) @test length(equations(sys)) == 1 eq = equations(tearing_substitution(sys))[1] vv = only(unknowns(sys)) @@ -233,7 +233,7 @@ eqs = [D(x) ~ σ * (y - x) u ~ z + a] lorenz1 = System(eqs, t, name = :lorenz1) -lorenz1_reduced = structural_simplify(lorenz1, inputs = [z], outputs = []) +lorenz1_reduced = mtkcompile(lorenz1, inputs = [z], outputs = []) @test z in Set(parameters(lorenz1_reduced)) # #2064 @@ -242,7 +242,7 @@ eqs = [D(x) ~ x D(y) ~ y D(z) ~ t] @named model = System(eqs, t) -sys = structural_simplify(model) +sys = mtkcompile(model) Js = ModelingToolkit.jacobian_sparsity(sys) @test size(Js) == (3, 3) @test Js == Diagonal([1, 1, 0]) @@ -275,7 +275,7 @@ new_sys = alias_elimination(sys) eqs = [x ~ 0 D(x) ~ x + y] @named sys = System(eqs, t, [x, y], []) -ss = structural_simplify(sys) +ss = mtkcompile(sys) @test isempty(equations(ss)) @test sort(string.(observed(ss))) == ["x(t) ~ 0.0" "xˍt(t) ~ 0.0" @@ -285,5 +285,5 @@ eqs = [D(D(x)) ~ -x] @named sys = System(eqs, t, [x], []) ss = alias_elimination(sys) @test length(equations(ss)) == length(unknowns(ss)) == 1 -ss = structural_simplify(sys) +ss = mtkcompile(sys) @test length(equations(ss)) == length(unknowns(ss)) == 2 diff --git a/test/scc_nonlinear_problem.jl b/test/scc_nonlinear_problem.jl index 51552678b0..278b1a90d1 100644 --- a/test/scc_nonlinear_problem.jl +++ b/test/scc_nonlinear_problem.jl @@ -23,9 +23,9 @@ using ModelingToolkit: t_nounits as t, D_nounits as D eqs = 0 .~ eqs @named model = System(eqs) @test_throws ["simplified", "required"] SCCNonlinearProblem(model, []) - _model = structural_simplify(model; split = false) + _model = mtkcompile(model; split = false) @test_throws ["not compatible"] SCCNonlinearProblem(_model, []) - model = structural_simplify(model) + model = mtkcompile(model) prob = NonlinearProblem(model, [u => zeros(8)]) sccprob = SCCNonlinearProblem(model, [u => zeros(8)]) sol1 = solve(prob, NewtonRaphson()) diff --git a/test/sciml_problem_inputs.jl b/test/sciml_problem_inputs.jl index a1b9d1e416..249991b631 100644 --- a/test/sciml_problem_inputs.jl +++ b/test/sciml_problem_inputs.jl @@ -37,7 +37,7 @@ begin MassActionJump(k2, [Z => 1], [Y => 1, Z => -1]) ] - # Create systems (without structural_simplify, since that might modify systems to affect intended tests). + # Create systems (without mtkcompile, since that might modify systems to affect intended tests). osys = complete(System(diff_eqs, t; name = :osys)) ssys = complete(SDESystem( diff_eqs, noise_eqs, t, [X, Y, Z], [kp, kd, k1, k2]; name = :ssys)) diff --git a/test/sdesystem.jl b/test/sdesystem.jl index ead8057bee..bcec551faf 100644 --- a/test/sdesystem.jl +++ b/test/sdesystem.jl @@ -597,7 +597,7 @@ eqs = [D(x) ~ σ * (y - x) + x * β, D(y) ~ x * (ρ - z) - y + y * β + x * η, D(z) ~ x * y - β * z + (x * z) * β] @named sys1 = System(eqs, tt) -sys1 = structural_simplify(sys1) +sys1 = mtkcompile(sys1) drift_eqs = [D(x) ~ σ * (y - x), D(y) ~ x * (ρ - z) - y, @@ -798,13 +798,13 @@ end input ~ 0.0] sys = System(eqs, t, sts, ps, browns; name = :name) - sys = structural_simplify(sys) + sys = mtkcompile(sys) @test ModelingToolkit.get_noise_eqs(sys) ≈ [1.0] prob = SDEProblem(sys, [], (0.0, 1.0), []) @test_nowarn solve(prob, RKMil()) end -@testset "Observed variables retained after `structural_simplify`" begin +@testset "Observed variables retained after `mtkcompile`" begin @variables x(t) y(t) z(t) @brownian a @mtkcompile sys = System([D(x) ~ x + a, D(y) ~ y + a, z ~ x + y], t) @@ -860,7 +860,7 @@ end end end -@testset "`structural_simplify(::SDESystem)`" begin +@testset "`mtkcompile(::SDESystem)`" begin @variables x(t) y(t) @mtkcompile sys = SDESystem( [D(x) ~ x, y ~ 2x], [x, 0], t, [x, y], []) @@ -949,7 +949,7 @@ end @test ssys1 !== ssys2 end -@testset "Error when constructing SDEProblem without `structural_simplify`" begin +@testset "Error when constructing SDEProblem without `mtkcompile`" begin @parameters σ ρ β @variables x(tt) y(tt) z(tt) @brownian a @@ -963,8 +963,8 @@ end u0map = [x => 1.0, y => 0.0, z => 0.0] parammap = [σ => 10.0, β => 26.0, ρ => 2.33] - @test_throws ["Brownian", "structural_simplify"] SDEProblem( + @test_throws ["Brownian", "mtkcompile"] SDEProblem( de, u0map, (0.0, 100.0), parammap) - de = structural_simplify(de) + de = mtkcompile(de) @test SDEProblem(de, u0map, (0.0, 100.0), parammap) isa SDEProblem end diff --git a/test/serialization.jl b/test/serialization.jl index 1a0105d155..83e68f5770 100644 --- a/test/serialization.jl +++ b/test/serialization.jl @@ -32,7 +32,7 @@ sys = include_string(@__MODULE__, str) @test sys == expand_connections(rc_model) # this actually kind of works, but the variables would have different identities. # check answer -ss = structural_simplify(rc_model) +ss = mtkcompile(rc_model) all_obs = observables(ss) prob = ODEProblem(ss, [capacitor.v => 0.0], (0, 0.1)) sol = solve(prob, ImplicitEuler()) diff --git a/test/split_parameters.jl b/test/split_parameters.jl index d6596f68ae..ac819351b5 100644 --- a/test/split_parameters.jl +++ b/test/split_parameters.jl @@ -82,7 +82,7 @@ eqs = [y ~ src.output.u @named sys = System(eqs, t, vars, []; systems = [int, src]) s = complete(sys) -sys = structural_simplify(sys) +sys = mtkcompile(sys) prob = ODEProblem( sys, [], (0.0, t_end), [s.src.interpolator => Interpolator(x, dt)]; tofloat = false) @@ -108,7 +108,7 @@ eqs = [D(y) ~ dy * a ddy ~ sin(t) * c] @named model = System(eqs, t, vars, pars) -sys = structural_simplify(model; split = false) +sys = mtkcompile(model; split = false) tspan = (0.0, t_end) prob = ODEProblem(sys, [], tspan, []; build_initializeprob = false) diff --git a/test/state_selection.jl b/test/state_selection.jl index ba74cc04a2..f3971bdbe3 100644 --- a/test/state_selection.jl +++ b/test/state_selection.jl @@ -117,7 +117,7 @@ let @named system = HydraulicSystem(L = 10) @unpack supply_pipe, return_pipe = system - sys = structural_simplify(system) + sys = mtkcompile(system) u0 = [ sys.supply_pipe.v => 0.1, sys.return_pipe.v => 0.1, D(supply_pipe.v) => 0.0, D(return_pipe.fluid_port_a.m) => 0.0, @@ -168,7 +168,7 @@ let @named trans = System(eqs, t) - sys = structural_simplify(trans) + sys = mtkcompile(trans) n = 3 u = 0 * ones(n) @@ -273,7 +273,7 @@ let # solution ------------------------------------------------------------------- @named catapult = System(eqs, t, vars, params, defaults = defs) - sys = structural_simplify(catapult) + sys = mtkcompile(catapult) prob = ODEProblem(sys, [], (0.0, 0.1), [l_2f => 0.55, damp => 1e7]; jac = true) @test solve(prob, Rodas4()).retcode == ReturnCode.Success end diff --git a/test/static_arrays.jl b/test/static_arrays.jl index 61b9bb6b25..edb6eeff7d 100644 --- a/test/static_arrays.jl +++ b/test/static_arrays.jl @@ -9,7 +9,7 @@ eqs = [D(D(x)) ~ σ * (y - x), D(z) ~ x * y - β * z] @named sys = System(eqs, t) -sys = structural_simplify(sys) +sys = mtkcompile(sys) u0 = @SVector [D(x) => 2.0, x => 1.0, diff --git a/test/stream_connectors.jl b/test/stream_connectors.jl index 23c648d9e1..6e49cd1fb3 100644 --- a/test/stream_connectors.jl +++ b/test/stream_connectors.jl @@ -134,7 +134,7 @@ eqns = [domain_connect(fluid, n1m1.port_a) @named n1m1Test = System(eqns, t, [], []; systems = [fluid, n1m1, pipe, sink]) -@test_nowarn structural_simplify(n1m1Test) +@test_nowarn mtkcompile(n1m1Test) @unpack source, port_a = n1m1 ssort(eqs) = sort(eqs, by = string) @test ssort(equations(expand_connections(n1m1))) == ssort([0 ~ port_a.m_flow @@ -205,7 +205,7 @@ eqns = [connect(n1m2.port_a, sink1.port) @named sys = System(eqns, t) @named n1m2Test = compose(sys, n1m2, sink1, sink2) -@test_nowarn structural_simplify(n1m2Test) +@test_nowarn mtkcompile(n1m2Test) @named n1m2 = N1M2() @named pipe1 = AdiabaticStraightPipe() @@ -220,7 +220,7 @@ eqns = [connect(n1m2.port_a, pipe1.port_a) @named sys = System(eqns, t) @named n1m2AltTest = compose(sys, n1m2, pipe1, pipe2, sink1, sink2) -@test_nowarn structural_simplify(n1m2AltTest) +@test_nowarn mtkcompile(n1m2AltTest) # N2M2 model and test code. function N2M2(; name, @@ -249,7 +249,7 @@ eqns = [connect(source.port, n2m2.port_a) @named sys = System(eqns, t) @named n2m2Test = compose(sys, n2m2, source, sink) -@test_nowarn structural_simplify(n2m2Test) +@test_nowarn mtkcompile(n2m2Test) # stream var @named sp1 = TwoPhaseFluidPort() @@ -472,7 +472,7 @@ csys = complete(two_fluid_system) @test Symbol(sys_defs[csys.volume_a.H.rho]) == Symbol(csys.fluid_a.rho) @test Symbol(sys_defs[csys.volume_b.H.rho]) == Symbol(csys.fluid_b.rho) -@test_nowarn structural_simplify(two_fluid_system) +@test_nowarn mtkcompile(two_fluid_system) function OneFluidSystem(; name) pars = [] @@ -510,4 +510,4 @@ csys = complete(one_fluid_system) @test Symbol(sys_defs[csys.volume_a.H.rho]) == Symbol(csys.fluid.rho) @test Symbol(sys_defs[csys.volume_b.H.rho]) == Symbol(csys.fluid.rho) -@test_nowarn structural_simplify(one_fluid_system) +@test_nowarn mtkcompile(one_fluid_system) diff --git a/test/structural_transformation/index_reduction.jl b/test/structural_transformation/index_reduction.jl index 0703221418..6ab8dde338 100644 --- a/test/structural_transformation/index_reduction.jl +++ b/test/structural_transformation/index_reduction.jl @@ -49,7 +49,7 @@ let pss_pendulum = partial_state_selection(pendulum) @test_broken length(equations(pss_pendulum)) == 3 end -let sys = structural_simplify(pendulum2) +let sys = mtkcompile(pendulum2) @test length(equations(sys)) == 5 @test length(unknowns(sys)) == 5 @@ -76,7 +76,7 @@ let D(D(y)) ~ λ * y - g x^2 + y^2 ~ 1] @named pend = System(eqs, t) - sys = complete(structural_simplify(pend; dummy_derivative = false)) + sys = complete(mtkcompile(pend; dummy_derivative = false)) prob = ODEProblem( sys, [x => 1, y => 0, D(x) => 0.0], (0.0, 10.0), [g => 1], guesses = [λ => 0.0]) sol = solve(prob, Rodas5P()) diff --git a/test/structural_transformation/tearing.jl b/test/structural_transformation/tearing.jl index 5bac18a253..e9b9909871 100644 --- a/test/structural_transformation/tearing.jl +++ b/test/structural_transformation/tearing.jl @@ -146,7 +146,7 @@ eqs = [D(x) ~ z * h 0 ~ x - y 0 ~ sin(z) + y - p * t] @named daesys = System(eqs, t) -newdaesys = structural_simplify(daesys) +newdaesys = mtkcompile(daesys) @test equations(newdaesys) == [D(x) ~ h * z; 0 ~ y + sin(z) - p * t] @test equations(tearing_substitution(newdaesys)) == [D(x) ~ h * z; 0 ~ x + sin(z) - p * t] @test isequal(unknowns(newdaesys), [x, z]) @@ -161,7 +161,7 @@ prob.f(du, u, pr, tt) # test the initial guess is respected @named sys = System(eqs, t, defaults = Dict(z => NaN)) -infprob = ODEProblem(structural_simplify(sys), [x => 1.0], (0, 1.0), [p => 0.2]) +infprob = ODEProblem(mtkcompile(sys), [x => 1.0], (0, 1.0), [p => 0.2]) infprob.f(du, infprob.u0, pr, tt) @test any(isnan, du) @@ -192,7 +192,7 @@ calculate_tgrad(ms_model) u0 = [mass.s => 0.0 mass.v => 1.0] -sys = structural_simplify(ms_model) +sys = mtkcompile(ms_model) # @test ModelingToolkit.get_jac(sys)[] === ModelingToolkit.EMPTY_JAC # @test ModelingToolkit.get_tgrad(sys)[] === ModelingToolkit.EMPTY_TGRAD prob_complex = ODEProblem(sys, u0, (0, 1.0)) diff --git a/test/structural_transformation/utils.jl b/test/structural_transformation/utils.jl index 83da4df584..8b158b6477 100644 --- a/test/structural_transformation/utils.jl +++ b/test/structural_transformation/utils.jl @@ -123,14 +123,14 @@ end @named sys = System( [D(x) ~ z[1] + z[2] + foo(z)[1], y[1] ~ 2t, y[2] ~ 3t, z ~ foo(y)], t) - sys1 = structural_simplify(sys; cse_hack = false) + sys1 = mtkcompile(sys; cse_hack = false) @test length(observed(sys1)) == 6 @test !any(observed(sys1)) do eq iscall(eq.rhs) && operation(eq.rhs) == StructuralTransformations.getindex_wrapper end - sys2 = structural_simplify(sys; array_hack = false) + sys2 = mtkcompile(sys; array_hack = false) @test length(observed(sys2)) == 5 @test !any(observed(sys2)) do eq iscall(eq.rhs) && operation(eq.rhs) == StructuralTransformations.change_origin @@ -144,14 +144,14 @@ end @named sys = System( [D(x) ~ z[1] + z[2] + foo(z)[1] + w, y[1] ~ 2t, y[2] ~ 3t, z ~ foo(y)], t) - sys1 = structural_simplify(sys; cse_hack = false, fully_determined = false) + sys1 = mtkcompile(sys; cse_hack = false, fully_determined = false) @test length(observed(sys1)) == 6 @test !any(observed(sys1)) do eq iscall(eq.rhs) && operation(eq.rhs) == StructuralTransformations.getindex_wrapper end - sys2 = structural_simplify(sys; array_hack = false, fully_determined = false) + sys2 = mtkcompile(sys; array_hack = false, fully_determined = false) @test length(observed(sys2)) == 5 @test !any(observed(sys2)) do eq iscall(eq.rhs) && operation(eq.rhs) == StructuralTransformations.change_origin @@ -164,7 +164,7 @@ end @named sys = System([D(x) ~ x, y ~ x + t], t) value = Ref(0) pass(sys; kwargs...) = (value[] += 1; return sys) - structural_simplify(sys; additional_passes = [pass]) + mtkcompile(sys; additional_passes = [pass]) @test value[] == 1 end @@ -335,7 +335,7 @@ end end @named sys = FilteredInputErr() - @test_throws ["derivative of discrete variable", "k(t)"] structural_simplify(sys) + @test_throws ["derivative of discrete variable", "k(t)"] mtkcompile(sys) @mtkcompile sys = FilteredInput() vs = Set() diff --git a/test/substitute_component.jl b/test/substitute_component.jl index ad20dbcbc1..e598d86a06 100644 --- a/test/substitute_component.jl +++ b/test/substitute_component.jl @@ -59,8 +59,8 @@ end @named reference = RC() - sys1 = structural_simplify(rcsys) - sys2 = structural_simplify(reference) + sys1 = mtkcompile(rcsys) + sys2 = mtkcompile(reference) @test isequal(unknowns(sys1), unknowns(sys2)) @test isequal(equations(sys1), equations(sys2)) diff --git a/test/symbolic_events.jl b/test/symbolic_events.jl index 8f26d12e19..94a443fc4d 100644 --- a/test/symbolic_events.jl +++ b/test/symbolic_events.jl @@ -312,7 +312,7 @@ end @test only(continuous_events(ball)) == SymbolicContinuousCallback(Equation[x ~ 0], Equation[v ~ -Pre(v)]) - ball = structural_simplify(ball) + ball = mtkcompile(ball) @test length(ModelingToolkit.continuous_events(ball)) == 1 @@ -334,8 +334,8 @@ end D(vy) ~ -0.01vy], t; continuous_events = events) _ball = ball - ball = structural_simplify(_ball) - ball_nosplit = structural_simplify(_ball; split = false) + ball = mtkcompile(_ball) + ball_nosplit = mtkcompile(_ball; split = false) tspan = (0.0, 5.0) prob = ODEProblem(ball, Pair[], tspan) @@ -373,8 +373,8 @@ end D(vx) ~ -1 D(vy) ~ 0], t; continuous_events = events) - ball_nosplit = structural_simplify(ball) - ball = structural_simplify(ball) + ball_nosplit = mtkcompile(ball) + ball = mtkcompile(ball) tspan = (0.0, 5.0) prob = ODEProblem(ball, Pair[], tspan) @@ -396,7 +396,7 @@ end D(vmeasured) ~ 0.0] ev = [sin(20pi * t) ~ 0.0] => [vmeasured ~ Pre(v)] @named sys = System(eq, t, continuous_events = ev) - sys = structural_simplify(sys) + sys = mtkcompile(sys) prob = ODEProblem(sys, zeros(2), (0.0, 5.1)) sol = solve(prob, Tsit5()) @test all(minimum((0:0.1:5) .- sol.t', dims = 2) .< 0.0001) # test that the solver stepped every 0.1s as dictated by event @@ -446,7 +446,7 @@ end @named model = compose(_model, mass1, mass2, sd) end model = Model(sin(30t)) - sys = structural_simplify(model) + sys = mtkcompile(model) @test isempty(ModelingToolkit.continuous_events(sys)) end @@ -632,7 +632,7 @@ end eqs = [oscce.F ~ 0] @named eqs_sys = System(eqs, t) @named oneosc_ce = compose(eqs_sys, oscce) - oneosc_ce_simpl = structural_simplify(oneosc_ce) + oneosc_ce_simpl = mtkcompile(oneosc_ce) prob = ODEProblem(oneosc_ce_simpl, [], (0.0, 2.0), []) sol = solve(prob, Tsit5(), saveat = 0.1) @@ -657,7 +657,7 @@ end evt2 = ModelingToolkit.SymbolicContinuousCallback( [c2 ~ 0], (f = record_crossings, observed = (; v = c2), ctx = cr2)) @named trigsys = System(eqs, t; continuous_events = [evt1, evt2]) - trigsys_ss = structural_simplify(trigsys) + trigsys_ss = mtkcompile(trigsys) prob = ODEProblem(trigsys_ss, [], (0.0, 2π)) sol = solve(prob, Tsit5()) required_crossings_c1 = [π / 2, 3 * π / 2] @@ -679,7 +679,7 @@ end [c2 ~ 0], (f = record_crossings, observed = (; v = c2), ctx = cr2p); affect_neg = (f = record_crossings, observed = (; v = c2), ctx = cr2n)) @named trigsys = System(eqs, t; continuous_events = [evt1, evt2]) - trigsys_ss = structural_simplify(trigsys) + trigsys_ss = mtkcompile(trigsys) prob = ODEProblem(trigsys_ss, [], (0.0, 2π)) sol = solve(prob, Tsit5(); dtmax = 0.01) c1_pc = filter((<=)(0) ∘ sin, required_crossings_c1) @@ -703,7 +703,7 @@ end evt2 = ModelingToolkit.SymbolicContinuousCallback( [c2 ~ 0], (f = record_crossings, observed = (; v = c2), ctx = cr2p); affect_neg = nothing) @named trigsys = System(eqs, t; continuous_events = [evt1, evt2]) - trigsys_ss = structural_simplify(trigsys) + trigsys_ss = mtkcompile(trigsys) prob = ODEProblem(trigsys_ss, [], (0.0, 2π)) sol = solve(prob, Tsit5(); dtmax = 0.01) @test maximum(abs.(c1_pc .- first.(cr1p))) < 1e-5 @@ -722,7 +722,7 @@ end [c2 ~ 0], (f = record_crossings, observed = (; v = c2), ctx = cr2p); affect_neg = (f = record_crossings, observed = (; v = c2), ctx = cr2n)) @named trigsys = System(eqs, t; continuous_events = [evt1, evt2]) - trigsys_ss = structural_simplify(trigsys) + trigsys_ss = mtkcompile(trigsys) prob = ODEProblem(trigsys_ss, [], (0.0, 2π)) sol = solve(prob, Tsit5(); dtmax = 0.01) c1_pc = filter((<=)(0) ∘ sin, required_crossings_c1) @@ -746,7 +746,7 @@ end [c2 ~ 0], (f = record_crossings, observed = (; v = c2), ctx = cr2); rootfind = SciMLBase.RightRootFind) @named trigsys = System(eqs, t; continuous_events = [evt1, evt2]) - trigsys_ss = structural_simplify(trigsys) + trigsys_ss = mtkcompile(trigsys) prob = ODEProblem(trigsys_ss, [], (0.0, 2π)) sol = solve(prob, Tsit5(); dtmax = 0.01) required_crossings_c1 = [π / 2, 3 * π / 2] @@ -766,7 +766,7 @@ end [c2 ~ 0], (f = record_crossings, observed = (; v = c2), ctx = cr2); rootfind = SciMLBase.RightRootFind) @named trigsys = System(eqs, t; continuous_events = [evt1, evt2]) - trigsys_ss = structural_simplify(trigsys) + trigsys_ss = mtkcompile(trigsys) prob = ODEProblem(trigsys_ss, [], (0.0, 2π)) sol = solve(prob, Tsit5()) @test maximum(abs.(first.(cr1) .- required_crossings_c1)) < 1e-4 @@ -784,7 +784,7 @@ end [c2 ~ 0], (f = record_crossings, observed = (; v = c2), ctx = cr2); rootfind = SciMLBase.RightRootFind) @named trigsys = System(eqs, t; continuous_events = [evt2, evt1]) - trigsys_ss = structural_simplify(trigsys) + trigsys_ss = mtkcompile(trigsys) prob = ODEProblem(trigsys_ss, [], (0.0, 2π)) sol = solve(prob, Tsit5()) @test maximum(abs.(first.(cr1) .- required_crossings_c1)) < 1e-4 @@ -915,7 +915,7 @@ end end) @named sys = System( eqs, t, [temp], params; continuous_events = [furnace_off, furnace_enable]) - ss = structural_simplify(sys) + ss = mtkcompile(sys) prob = ODEProblem(ss, [temp => 0.0, furnace_on => true], (0.0, 100.0)) sol = solve(prob, Tsit5(); dtmax = 0.01) @test all(sol[temp][sol.t .> 1.0] .<= 0.79) && all(sol[temp][sol.t .> 1.0] .>= 0.49) @@ -935,7 +935,7 @@ end end) @named sys = System( eqs, t, [temp], params; continuous_events = [furnace_off, furnace_enable]) - ss = structural_simplify(sys) + ss = mtkcompile(sys) prob = ODEProblem(ss, [temp => 0.0, furnace_on => true], (0.0, 100.0)) sol = solve(prob, Tsit5(); dtmax = 0.01) @test all(sol[temp][sol.t .> 1.0] .<= 0.79) && all(sol[temp][sol.t .> 1.0] .>= 0.49) @@ -956,7 +956,7 @@ end @set! x.furnace_on = false end) @named sys = System(eqs, t, [temp], params; continuous_events = [furnace_off]) - ss = structural_simplify(sys) + ss = mtkcompile(sys) @test_logs (:warn, "The symbols Any[:furnace_on] are declared as both observed and modified; this is a code smell because it becomes easy to confuse them and assign/not assign a value.") prob=ODEProblem( ss, [temp => 0.0, furnace_on => true], (0.0, 100.0)) @@ -973,7 +973,7 @@ end end) @named sys = System( eqs, t, [temp, tempsq], params; continuous_events = [furnace_off]) - ss = structural_simplify(sys) + ss = mtkcompile(sys) @test_throws "refers to missing variable(s)" prob=ODEProblem( ss, [temp => 0.0, furnace_on => true], (0.0, 100.0)) @@ -986,7 +986,7 @@ end end) @named sys = System( eqs, t, [temp, tempsq], params; continuous_events = [furnace_off]) - ss = structural_simplify(sys) + ss = mtkcompile(sys) @test_throws "refers to missing variable(s)" prob=ODEProblem( ss, [temp => 0.0, furnace_on => true], (0.0, 100.0)) @@ -998,7 +998,7 @@ end end) @named sys = System( eqs, t, [temp, tempsq], params; continuous_events = [furnace_off]) - ss = structural_simplify(sys) + ss = mtkcompile(sys) prob = ODEProblem( ss, [temp => 0.0, furnace_on => true], (0.0, 100.0)) @test_throws "Tried to write back to" solve(prob, Tsit5()) @@ -1058,7 +1058,7 @@ end end; rootfind = SciMLBase.RightRootFind) @named sys = System( eqs, t, [theta, omega], params; continuous_events = [qAevt, qBevt]) - ss = structural_simplify(sys) + ss = mtkcompile(sys) prob = ODEProblem(ss, [theta => 1e-5], (0.0, pi)) sol = solve(prob, Tsit5(); dtmax = 0.01) @test getp(sol, cnt)(sol) == 198 # we get 2 pulses per phase cycle (cos 0 crossing) and we go to 100 cycles; we miss a few due to the initial state @@ -1224,13 +1224,13 @@ end @named wd1 = weird1(0.021) @named wd2 = weird2(0.021) - sys1 = structural_simplify(System(Equation[], t; name = :parent, + sys1 = mtkcompile(System(Equation[], t; name = :parent, discrete_events = [0.01 => ModelingToolkit.ImperativeAffect( modified = (; θs = reduce(vcat, [[wd1.θ]])), ctx = [1]) do m, o, c, i @set! m.θs[1] = c[] += 1 end], systems = [wd1])) - sys2 = structural_simplify(System(Equation[], t; name = :parent, + sys2 = mtkcompile(System(Equation[], t; name = :parent, discrete_events = [0.01 => ModelingToolkit.ImperativeAffect( modified = (; θs = reduce(vcat, [[wd2.θ]])), ctx = [1]) do m, o, c, i @set! m.θs[1] = c[] += 1 @@ -1328,6 +1328,6 @@ end @named sys = System(Equation[], t, [], Symbolics.scalarize(vals); systems = [child(vals; name = :child)]) - sys = structural_simplify(sys) + sys = mtkcompile(sys) sol = solve(ODEProblem(sys, [], (0.0, 1.0)), Tsit5()) end diff --git a/test/symbolic_indexing_interface.jl b/test/symbolic_indexing_interface.jl index b3ecd9058b..f71fe93842 100644 --- a/test/symbolic_indexing_interface.jl +++ b/test/symbolic_indexing_interface.jl @@ -218,7 +218,7 @@ end @variables x(t) y(t) z(t) @parameters a @named sys = System([D(x) ~ a * x, y ~ 2x, z ~ 0.0], t) - sys = structural_simplify(sys, split = false) + sys = mtkcompile(sys, split = false) for sym in [x, y, z, x + y, x + a, y / x] @test only(get_all_timeseries_indexes(sys, sym)) == ContinuousTimeseries() end diff --git a/test/units.jl b/test/units.jl index f15145ffa9..77f35877e9 100644 --- a/test/units.jl +++ b/test/units.jl @@ -140,24 +140,24 @@ D = Differential(t) eqs = [D(L) ~ v, V ~ L^3] @named sys = System(eqs, t) -sys_simple = structural_simplify(sys) +sys_simple = mtkcompile(sys) eqs = [D(V) ~ r, V ~ L^3] @named sys = System(eqs, t) -sys_simple = structural_simplify(sys) +sys_simple = mtkcompile(sys) @variables V [unit = u"m"^3] L [unit = u"m"] @parameters v [unit = u"m/s"] r [unit = u"m"^3 / u"s"] t [unit = u"s"] eqs = [V ~ r * t, V ~ L^3] @named sys = System(eqs, [V, L], [t, r]) -sys_simple = structural_simplify(sys) +sys_simple = mtkcompile(sys) eqs = [L ~ v * t, V ~ L^3] @named sys = System(eqs, [V, L], [v, t, r]) -sys_simple = structural_simplify(sys) +sys_simple = mtkcompile(sys) #Jump System @parameters β [unit = u"(mol^2*s)^-1"] γ [unit = u"(mol*s)^-1"] t [unit = u"s"] jumpmol [ diff --git a/test/variable_scope.jl b/test/variable_scope.jl index 2ecd62ec1f..080cbcc7c4 100644 --- a/test/variable_scope.jl +++ b/test/variable_scope.jl @@ -125,7 +125,7 @@ defs = ModelingToolkit.defaults(bar) sys4 = complete(sys3) @test length(unknowns(sys4)) == 3 @test length(parameters(sys4)) == 4 - sys5 = structural_simplify(sys3) + sys5 = mtkcompile(sys3) @test length(unknowns(sys5)) == 4 @test any(isequal(x4), unknowns(sys5)) @test length(parameters(sys5)) == 4 From 6307c1de78ee5aff8974c4001fd999e637cb8104 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 22 May 2025 16:08:53 +0530 Subject: [PATCH 1821/2176] test: do not use deprecated `getproperty`/`setproperty!` in tests --- test/model_parsing.jl | 2 +- test/symbolic_parameters.jl | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/test/model_parsing.jl b/test/model_parsing.jl index 88fa7faac7..a615a1d616 100644 --- a/test/model_parsing.jl +++ b/test/model_parsing.jl @@ -977,7 +977,7 @@ end @testset "Multiple extend statements" begin @named multiple_extend = MultipleExtend() - @test collect(nameof.(multiple_extend.systems)) == [:inmodel_b, :inmodel] + @test collect(nameof.(get_systems(multiple_extend))) == [:inmodel_b, :inmodel] @test MultipleExtend.structure[:extend][1] == [:inmodel, :b, :inmodel_b] @test tosymbol.(parameters(multiple_extend)) == [:b, :inmodel_b₊p, :inmodel₊p] end diff --git a/test/symbolic_parameters.jl b/test/symbolic_parameters.jl index 6a6a434ccc..15b19b320a 100644 --- a/test/symbolic_parameters.jl +++ b/test/symbolic_parameters.jl @@ -21,7 +21,7 @@ u0 = [ z => u - 0.1 ] ns = System(eqs, [x, y, z], [σ, ρ, β], name = :ns, defaults = [par; u0]) -ns.y = u * 1.1 +ModelingToolkit.get_defaults(ns)[y] = u * 1.1 resolved = ModelingToolkit.varmap_to_vars(Dict(), parameters(ns), defaults = ModelingToolkit.defaults(ns)) @test resolved == [1, 0.1 + 1, (0.1 + 1) * 1.1] @@ -33,8 +33,8 @@ sol = solve(prob, NewtonRaphson()) @variables a @parameters b top = System([0 ~ -a + ns.x + b], [a], [b], systems = [ns], name = :top) -top.b = ns.σ * 0.5 -top.ns.x = u * 0.5 +ModelingToolkit.get_defaults(top)[b] = ns.σ * 0.5 +ModelingToolkit.get_defaults(top)[ns.x] = unknowns(ns, u) * 0.5 res = ModelingToolkit.varmap_to_vars(Dict(), parameters(top), defaults = ModelingToolkit.defaults(top)) From a45fef80269699fa91506e39e0787271e2ba047b Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 22 May 2025 23:03:56 +0530 Subject: [PATCH 1822/2176] refactor: deprecate `structural_simplify` and `@mtkbuild` --- src/ModelingToolkit.jl | 5 +++-- src/deprecations.jl | 11 +++++++++++ test/structural_transformation/utils.jl | 7 +++++++ 3 files changed, 21 insertions(+), 2 deletions(-) create mode 100644 src/deprecations.jl diff --git a/src/ModelingToolkit.jl b/src/ModelingToolkit.jl index bac58df69b..e94c8bb5af 100644 --- a/src/ModelingToolkit.jl +++ b/src/ModelingToolkit.jl @@ -214,6 +214,7 @@ include("structural_transformation/StructuralTransformations.jl") include("inputoutput.jl") include("adjoints.jl") +include("deprecations.jl") const t_nounits = let only(@independent_variables t) @@ -288,7 +289,7 @@ export alias_elimination, flatten export connect, domain_connect, @connector, Connection, AnalysisPoint, Flow, Stream, instream export initial_state, transition, activeState, entry, ticksInState, timeInState -export @component, @mtkmodel, @mtkcompile +export @component, @mtkmodel, @mtkcompile, @mtkbuild export isinput, isoutput, getbounds, hasbounds, getguess, hasguess, isdisturbance, istunable, getdist, hasdist, tunable_parameters, isirreducible, getdescription, hasdescription, @@ -305,7 +306,7 @@ export independent_variable, equations, controls, observed, full_equations, jump brownians export initialization_equations, guesses, defaults, parameter_dependencies, hierarchy export mtkcompile, expand_connections, linearize, linearization_function, - LinearizationProblem + LinearizationProblem, structural_simplify export solve export Pre diff --git a/src/deprecations.jl b/src/deprecations.jl new file mode 100644 index 0000000000..c8aee32253 --- /dev/null +++ b/src/deprecations.jl @@ -0,0 +1,11 @@ +@deprecate structural_simplify(sys; kwargs...) mtkcompile(sys; kwargs...) +@deprecate structural_simplify(sys, io; kwargs...) mtkcompile( + sys; inputs = io[1], outputs = io[2], kwargs...) + +macro mtkbuild(exprs...) + return quote + Base.depwarn("`@mtkbuild` is deprecated. Use `@mtkcompile` instead.", :mtkbuild) + @mtkcompile $(exprs...) + end |> esc +end + diff --git a/test/structural_transformation/utils.jl b/test/structural_transformation/utils.jl index 8b158b6477..02298eb480 100644 --- a/test/structural_transformation/utils.jl +++ b/test/structural_transformation/utils.jl @@ -436,3 +436,10 @@ end @test integ.ps[p] ≈ 3.0 end end + +@testset "Deprecated `structural_simplify` and `@mtkbuild`" begin + @variables x(t) + @test_deprecated @mtkbuild sys = System([D(x) ~ x], t) + @named sys = System([D(x) ~ x], t) + @test_deprecated structural_simplify(sys) +end From d78b814c5f9f62cd2bd82c53321f7f1e8223acfb Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 21 May 2025 16:47:19 +0530 Subject: [PATCH 1823/2176] refactor: change problem constructors to `XProblem(sys, op[, tspan])` --- src/problems/bvproblem.jl | 13 ++--- src/problems/daeproblem.jl | 6 +-- src/problems/ddeproblem.jl | 4 +- src/problems/discreteproblem.jl | 4 +- src/problems/implicitdiscreteproblem.jl | 8 +-- src/problems/initializationproblem.jl | 5 +- src/problems/intervalnonlinearproblem.jl | 4 +- src/problems/jumpproblem.jl | 10 ++-- src/problems/nonlinearproblem.jl | 8 +-- src/problems/odeproblem.jl | 8 +-- src/problems/optimizationproblem.jl | 6 +-- src/problems/sccnonlinearproblem.jl | 9 ++-- src/problems/sddeproblem.jl | 4 +- src/problems/sdeproblem.jl | 4 +- .../nonlinear/homotopy_continuation.jl | 4 +- src/systems/nonlinear/initializesystem.jl | 6 ++- src/systems/parameter_buffer.jl | 18 +++---- src/systems/problem_utils.jl | 53 ++++++++----------- 18 files changed, 84 insertions(+), 90 deletions(-) diff --git a/src/problems/bvproblem.jl b/src/problems/bvproblem.jl index 82783077c1..30bd7e61c3 100644 --- a/src/problems/bvproblem.jl +++ b/src/problems/bvproblem.jl @@ -44,7 +44,7 @@ If the `System` has algebraic equations, like `x(t)^2 + y(t)^2`, the resulting `BVProblem` must be solved using BVDAE solvers, such as Ascher. """ @fallback_iip_specialize function SciMLBase.BVProblem{iip, spec}( - sys::System, u0map, tspan, parammap = SciMLBase.NullParameters(); + sys::System, op, tspan; check_compatibility = true, cse = true, checkbounds = false, eval_expression = false, eval_module = @__MODULE__, expression = Val{false}, guesses = Dict(), callback = nothing, @@ -55,22 +55,23 @@ If the `System` has algebraic equations, like `x(t)^2 + y(t)^2`, the resulting # Systems without algebraic equations should use both fixed values + guesses # for initialization. - _u0map = has_alg_eqs(sys) ? u0map : merge(Dict(u0map), Dict(guesses)) + _op = has_alg_eqs(sys) ? op : merge(Dict(op), Dict(guesses)) fode, u0, p = process_SciMLProblem( - ODEFunction{iip, spec}, sys, _u0map, parammap; guesses, + ODEFunction{iip, spec}, sys, _op; guesses, t = tspan !== nothing ? tspan[1] : tspan, check_compatibility = false, cse, checkbounds, time_dependent_init = false, expression, kwargs...) dvs = unknowns(sys) stidxmap = Dict([v => i for (i, v) in enumerate(dvs)]) - u0_idxs = has_alg_eqs(sys) ? collect(1:length(dvs)) : [stidxmap[k] for (k, v) in u0map] + u0_idxs = has_alg_eqs(sys) ? collect(1:length(dvs)) : + [stidxmap[k] for (k, v) in op if haskey(stidxmap, k)] fbc = generate_boundary_conditions( sys, u0, u0_idxs, tspan[1]; expression = Val{false}, wrap_gfw = Val{true}, cse, checkbounds) - if (length(constraints(sys)) + length(u0map) > length(dvs)) - @warn "The BVProblem is overdetermined. The total number of conditions (# constraints + # fixed initial values given by u0map) exceeds the total number of states. The BVP solvers will default to doing a nonlinear least-squares optimization." + if (length(constraints(sys)) + length(op) > length(dvs)) + @warn "The BVProblem is overdetermined. The total number of conditions (# constraints + # fixed initial values given by op) exceeds the total number of states. The BVP solvers will default to doing a nonlinear least-squares optimization." end kwargs = process_kwargs(sys; expression, kwargs...) diff --git a/src/problems/daeproblem.jl b/src/problems/daeproblem.jl index d1f8893cd5..860f2d2da2 100644 --- a/src/problems/daeproblem.jl +++ b/src/problems/daeproblem.jl @@ -62,15 +62,15 @@ end @fallback_iip_specialize function SciMLBase.DAEProblem{iip, spec}( - sys::System, du0map, u0map, tspan, parammap = SciMLBase.NullParameters(); + sys::System, op, tspan; callback = nothing, check_length = true, eval_expression = false, eval_module = @__MODULE__, check_compatibility = true, expression = Val{false}, kwargs...) where {iip, spec} check_complete(sys, DAEProblem) check_compatibility && check_compatible_system(DAEProblem, sys) - f, du0, u0, p = process_SciMLProblem(DAEFunction{iip, spec}, sys, u0map, parammap; - du0map, t = tspan !== nothing ? tspan[1] : tspan, check_length, eval_expression, + f, du0, u0, p = process_SciMLProblem(DAEFunction{iip, spec}, sys, op; + t = tspan !== nothing ? tspan[1] : tspan, check_length, eval_expression, eval_module, check_compatibility, implicit_dae = true, expression, kwargs...) kwargs = process_kwargs(sys; expression, callback, eval_expression, eval_module, diff --git a/src/problems/ddeproblem.jl b/src/problems/ddeproblem.jl index dfc660effd..6f072b3d51 100644 --- a/src/problems/ddeproblem.jl +++ b/src/problems/ddeproblem.jl @@ -42,14 +42,14 @@ end @fallback_iip_specialize function SciMLBase.DDEProblem{iip, spec}( - sys::System, u0map, tspan, parammap = SciMLBase.NullParameters(); + sys::System, op, tspan; callback = nothing, check_length = true, cse = true, checkbounds = false, eval_expression = false, eval_module = @__MODULE__, check_compatibility = true, u0_constructor = identity, expression = Val{false}, kwargs...) where {iip, spec} check_complete(sys, DDEProblem) check_compatibility && check_compatible_system(DDEProblem, sys) - f, u0, p = process_SciMLProblem(DDEFunction{iip, spec}, sys, u0map, parammap; + f, u0, p = process_SciMLProblem(DDEFunction{iip, spec}, sys, op; t = tspan !== nothing ? tspan[1] : tspan, check_length, cse, checkbounds, eval_expression, eval_module, check_compatibility, symbolic_u0 = true, expression, u0_constructor, kwargs...) diff --git a/src/problems/discreteproblem.jl b/src/problems/discreteproblem.jl index d77efed225..f640ee9ff5 100644 --- a/src/problems/discreteproblem.jl +++ b/src/problems/discreteproblem.jl @@ -39,7 +39,7 @@ end @fallback_iip_specialize function SciMLBase.DiscreteProblem{iip, spec}( - sys::System, u0map, tspan, parammap = SciMLBase.NullParameters(); + sys::System, op, tspan; check_compatibility = true, expression = Val{false}, kwargs...) where {iip, spec} check_complete(sys, DiscreteProblem) check_compatibility && check_compatible_system(DiscreteProblem, sys) @@ -47,7 +47,7 @@ end dvs = unknowns(sys) u0map = to_varmap(u0map, dvs) add_toterms!(u0map; replace = true) - f, u0, p = process_SciMLProblem(DiscreteFunction{iip, spec}, sys, u0map, parammap; + f, u0, p = process_SciMLProblem(DiscreteFunction{iip, spec}, sys, op; t = tspan !== nothing ? tspan[1] : tspan, check_compatibility, expression, kwargs...) diff --git a/src/problems/implicitdiscreteproblem.jl b/src/problems/implicitdiscreteproblem.jl index 476265e049..5b61e34df5 100644 --- a/src/problems/implicitdiscreteproblem.jl +++ b/src/problems/implicitdiscreteproblem.jl @@ -43,16 +43,16 @@ end @fallback_iip_specialize function SciMLBase.ImplicitDiscreteProblem{iip, spec}( - sys::System, u0map, tspan, parammap = SciMLBase.NullParameters(); + sys::System, op, tspan; check_compatibility = true, expression = Val{false}, kwargs...) where {iip, spec} check_complete(sys, ImplicitDiscreteProblem) check_compatibility && check_compatible_system(ImplicitDiscreteProblem, sys) dvs = unknowns(sys) - u0map = to_varmap(u0map, dvs) - add_toterms!(u0map; replace = true) + op = to_varmap(op, dvs) + add_toterms!(op; replace = true) f, u0, p = process_SciMLProblem( - ImplicitDiscreteFunction{iip, spec}, sys, u0map, parammap; + ImplicitDiscreteFunction{iip, spec}, sys, op; t = tspan !== nothing ? tspan[1] : tspan, check_compatibility, expression, kwargs...) diff --git a/src/problems/initializationproblem.jl b/src/problems/initializationproblem.jl index be535da3e6..e805e4d6aa 100644 --- a/src/problems/initializationproblem.jl +++ b/src/problems/initializationproblem.jl @@ -119,7 +119,7 @@ initial conditions for the given DAE. filter_missing_values!(u0map) filter_missing_values!(parammap) - u0map = merge(ModelingToolkit.guesses(sys), todict(guesses), u0map) + op = merge(ModelingToolkit.guesses(sys), todict(guesses), u0map, parammap) TProb = if neqs == nunknown && isempty(unassigned_vars) if use_scc && neqs > 0 @@ -135,8 +135,7 @@ initial conditions for the given DAE. else NonlinearLeastSquaresProblem end - TProb{iip}(isys, u0map, parammap; kwargs..., - build_initializeprob = false, is_initializeprob = true) + TProb{iip}(isys, op; kwargs..., build_initializeprob = false, is_initializeprob = true) end const INCOMPLETE_INITIALIZATION_MESSAGE = """ diff --git a/src/problems/intervalnonlinearproblem.jl b/src/problems/intervalnonlinearproblem.jl index 9c9e5d8688..65e7f7cb62 100644 --- a/src/problems/intervalnonlinearproblem.jl +++ b/src/problems/intervalnonlinearproblem.jl @@ -33,7 +33,9 @@ function SciMLBase.IntervalNonlinearProblem( check_compatibility && check_compatible_system(IntervalNonlinearProblem, sys) u0map = unknowns(sys) .=> uspan[1] - f, u0, p = process_SciMLProblem(IntervalNonlinearFunction, sys, u0map, parammap; + op = anydict([unknowns(sys)[1] => uspan[1]]) + merge!(op, to_varmap(parammap, parameters(sys))) + f, u0, p = process_SciMLProblem(IntervalNonlinearFunction, sys, op; check_compatibility, expression, kwargs...) kwargs = process_kwargs(sys; kwargs...) diff --git a/src/problems/jumpproblem.jl b/src/problems/jumpproblem.jl index 242b9549fa..8f5f2e8499 100644 --- a/src/problems/jumpproblem.jl +++ b/src/problems/jumpproblem.jl @@ -1,5 +1,5 @@ @fallback_iip_specialize function JumpProcesses.JumpProblem{iip, spec}( - sys::System, u0map, tspan::Union{Tuple, Nothing}, pmap = SciMLBase.NullParameters(); + sys::System, op, tspan::Union{Tuple, Nothing}; check_compatibility = true, eval_expression = false, eval_module = @__MODULE__, checkbounds = false, cse = true, aggregator = JumpProcesses.NullAggregator(), callback = nothing, rng = nothing, kwargs...) where {iip, spec} @@ -13,16 +13,16 @@ if (has_vrjs || has_eqs) if has_eqs && has_noise prob = SDEProblem{iip, spec}( - sys, u0map, tspan, pmap; check_compatibility = false, + sys, op, tspan; check_compatibility = false, build_initializeprob = false, checkbounds, cse, check_length = false, kwargs...) elseif has_eqs prob = ODEProblem{iip, spec}( - sys, u0map, tspan, pmap; check_compatibility = false, + sys, op, tspan; check_compatibility = false, build_initializeprob = false, checkbounds, cse, check_length = false, kwargs...) else - _, u0, p = process_SciMLProblem(EmptySciMLFunction{iip}, sys, u0map, pmap; + _, u0, p = process_SciMLProblem(EmptySciMLFunction{iip}, sys, op; t = tspan === nothing ? nothing : tspan[1], tofloat = false, check_length = false, build_initializeprob = false) observedfun = ObservedFunctionCache(sys; eval_expression, eval_module, @@ -32,7 +32,7 @@ prob = ODEProblem{true}(df, u0, tspan, p; kwargs...) end else - _f, u0, p = process_SciMLProblem(EmptySciMLFunction{iip}, sys, u0map, pmap; + _f, u0, p = process_SciMLProblem(EmptySciMLFunction{iip}, sys, op; t = tspan === nothing ? nothing : tspan[1], tofloat = false, check_length = false, build_initializeprob = false, cse) f = DiffEqBase.DISCRETE_INPLACE_DEFAULT diff --git a/src/problems/nonlinearproblem.jl b/src/problems/nonlinearproblem.jl index 0d3ffad17f..ef36179e06 100644 --- a/src/problems/nonlinearproblem.jl +++ b/src/problems/nonlinearproblem.jl @@ -57,7 +57,7 @@ end @fallback_iip_specialize function SciMLBase.NonlinearProblem{iip, spec}( - sys::System, u0map, parammap = SciMLBase.NullParameters(); expression = Val{false}, + sys::System, op; expression = Val{false}, check_length = true, check_compatibility = true, kwargs...) where {iip, spec} check_complete(sys, NonlinearProblem) if is_time_dependent(sys) @@ -65,7 +65,7 @@ end end check_compatibility && check_compatible_system(NonlinearProblem, sys) - f, u0, p = process_SciMLProblem(NonlinearFunction{iip, spec}, sys, u0map, parammap; + f, u0, p = process_SciMLProblem(NonlinearFunction{iip, spec}, sys, op; check_length, check_compatibility, expression, kwargs...) kwargs = process_kwargs(sys; kwargs...) @@ -75,12 +75,12 @@ end end @fallback_iip_specialize function SciMLBase.NonlinearLeastSquaresProblem{iip, spec}( - sys::System, u0map, parammap = DiffEqBase.NullParameters(); check_length = false, + sys::System, op; check_length = false, check_compatibility = true, expression = Val{false}, kwargs...) where {iip, spec} check_complete(sys, NonlinearLeastSquaresProblem) check_compatibility && check_compatible_system(NonlinearLeastSquaresProblem, sys) - f, u0, p = process_SciMLProblem(NonlinearFunction{iip}, sys, u0map, parammap; + f, u0, p = process_SciMLProblem(NonlinearFunction{iip}, sys, op; check_length, expression, kwargs...) kwargs = process_kwargs(sys; kwargs...) diff --git a/src/problems/odeproblem.jl b/src/problems/odeproblem.jl index 99cff129e0..e2901292ee 100644 --- a/src/problems/odeproblem.jl +++ b/src/problems/odeproblem.jl @@ -65,14 +65,14 @@ end @fallback_iip_specialize function SciMLBase.ODEProblem{iip, spec}( - sys::System, u0map, tspan, parammap = SciMLBase.NullParameters(); + sys::System, op, tspan; callback = nothing, check_length = true, eval_expression = false, expression = Val{false}, eval_module = @__MODULE__, check_compatibility = true, kwargs...) where {iip, spec} check_complete(sys, ODEProblem) check_compatibility && check_compatible_system(ODEProblem, sys) - f, u0, p = process_SciMLProblem(ODEFunction{iip, spec}, sys, u0map, parammap; + f, u0, p = process_SciMLProblem(ODEFunction{iip, spec}, sys, op; t = tspan !== nothing ? tspan[1] : tspan, check_length, eval_expression, eval_module, expression, check_compatibility, kwargs...) @@ -98,12 +98,12 @@ Generates an SteadyStateProblem from a `System` of ODEs and allows for automatic symbolically calculating numerical enhancements. """ @fallback_iip_specialize function DiffEqBase.SteadyStateProblem{iip, spec}( - sys::System, u0map, parammap; check_length = true, check_compatibility = true, + sys::System, op; check_length = true, check_compatibility = true, expression = Val{false}, kwargs...) where {iip, spec} check_complete(sys, SteadyStateProblem) check_compatibility && check_compatible_system(SteadyStateProblem, sys) - f, u0, p = process_SciMLProblem(ODEFunction{iip}, sys, u0map, parammap; + f, u0, p = process_SciMLProblem(ODEFunction{iip}, sys, op; steady_state = true, check_length, check_compatibility, expression, force_initialization_time_independent = true, kwargs...) diff --git a/src/problems/optimizationproblem.jl b/src/problems/optimizationproblem.jl index e0de2f78ff..3f121e3d2a 100644 --- a/src/problems/optimizationproblem.jl +++ b/src/problems/optimizationproblem.jl @@ -87,13 +87,13 @@ function SciMLBase.OptimizationProblem(sys::System, args...; kwargs...) end function SciMLBase.OptimizationProblem{iip}( - sys::System, u0map, parammap = SciMLBase.NullParameters(); lb = nothing, + sys::System, op; lb = nothing, ub = nothing, check_compatibility = true, expression = Val{false}, kwargs...) where {iip} check_complete(sys, OptimizationProblem) check_compatibility && check_compatible_system(OptimizationProblem, sys) - f, u0, p = process_SciMLProblem(OptimizationFunction{iip}, sys, u0map, parammap; + f, u0, p = process_SciMLProblem(OptimizationFunction{iip}, sys, op; check_compatibility, tofloat = false, check_length = false, expression, kwargs...) dvs = unknowns(sys) @@ -114,7 +114,7 @@ function SciMLBase.OptimizationProblem{iip}( end ps = parameters(sys) - defs = merge(defaults(sys), to_varmap(parammap, ps), to_varmap(u0map, dvs)) + defs = merge(defaults(sys), to_varmap(op, dvs)) lb = varmap_to_vars(dvs .=> lb, dvs; defaults = defs, tofloat = false) ub = varmap_to_vars(dvs .=> ub, dvs; defaults = defs, tofloat = false) diff --git a/src/problems/sccnonlinearproblem.jl b/src/problems/sccnonlinearproblem.jl index 63058a08f3..3e00170ad9 100644 --- a/src/problems/sccnonlinearproblem.jl +++ b/src/problems/sccnonlinearproblem.jl @@ -69,9 +69,8 @@ function SciMLBase.SCCNonlinearProblem(sys::System, args...; kwargs...) SCCNonlinearProblem{true}(sys, args...; kwargs...) end -function SciMLBase.SCCNonlinearProblem{iip}(sys::System, u0map, - parammap = SciMLBase.NullParameters(); eval_expression = false, eval_module = @__MODULE__, - cse = true, kwargs...) where {iip} +function SciMLBase.SCCNonlinearProblem{iip}(sys::System, op; eval_expression = false, + eval_module = @__MODULE__, cse = true, kwargs...) where {iip} if !iscomplete(sys) || get_tearing_state(sys) === nothing error("A simplified `System` is required. Call `mtkcompile` on the system before creating an `SCCNonlinearProblem`.") end @@ -85,7 +84,7 @@ function SciMLBase.SCCNonlinearProblem{iip}(sys::System, u0map, if length(var_sccs) == 1 return NonlinearProblem{iip}( - sys, u0map, parammap; eval_expression, eval_module, kwargs...) + sys, op; eval_expression, eval_module, kwargs...) end condensed_graph = MatchedCondensationGraph( @@ -102,7 +101,7 @@ function SciMLBase.SCCNonlinearProblem{iip}(sys::System, u0map, obs = observed(sys) _, u0, p = process_SciMLProblem( - EmptySciMLFunction{iip}, sys, u0map, parammap; eval_expression, eval_module, kwargs...) + EmptySciMLFunction{iip}, sys, op; eval_expression, eval_module, kwargs...) explicitfuns = [] nlfuns = [] diff --git a/src/problems/sddeproblem.jl b/src/problems/sddeproblem.jl index 3bc20c0412..0daa04fcf7 100644 --- a/src/problems/sddeproblem.jl +++ b/src/problems/sddeproblem.jl @@ -43,7 +43,7 @@ end @fallback_iip_specialize function SciMLBase.SDDEProblem{iip, spec}( - sys::System, u0map, tspan, parammap = SciMLBase.NullParameters(); + sys::System, op, tspan; callback = nothing, check_length = true, cse = true, checkbounds = false, eval_expression = false, eval_module = @__MODULE__, check_compatibility = true, u0_constructor = identity, sparse = false, sparsenoise = sparse, @@ -51,7 +51,7 @@ end check_complete(sys, SDDEProblem) check_compatibility && check_compatible_system(SDDEProblem, sys) - f, u0, p = process_SciMLProblem(SDDEFunction{iip, spec}, sys, u0map, parammap; + f, u0, p = process_SciMLProblem(SDDEFunction{iip, spec}, sys, op; t = tspan !== nothing ? tspan[1] : tspan, check_length, cse, checkbounds, eval_expression, eval_module, check_compatibility, sparse, symbolic_u0 = true, expression, u0_constructor, kwargs...) diff --git a/src/problems/sdeproblem.jl b/src/problems/sdeproblem.jl index a6270973f2..57ca120641 100644 --- a/src/problems/sdeproblem.jl +++ b/src/problems/sdeproblem.jl @@ -67,14 +67,14 @@ end @fallback_iip_specialize function SciMLBase.SDEProblem{iip, spec}( - sys::System, u0map, tspan, parammap = SciMLBase.NullParameters(); + sys::System, op, tspan; callback = nothing, check_length = true, eval_expression = false, eval_module = @__MODULE__, check_compatibility = true, sparse = false, sparsenoise = sparse, expression = Val{false}, kwargs...) where {iip, spec} check_complete(sys, SDEProblem) check_compatibility && check_compatible_system(SDEProblem, sys) - f, u0, p = process_SciMLProblem(SDEFunction{iip, spec}, sys, u0map, parammap; + f, u0, p = process_SciMLProblem(SDEFunction{iip, spec}, sys, op; t = tspan !== nothing ? tspan[1] : tspan, check_length, eval_expression, eval_module, check_compatibility, sparse, expression, kwargs...) diff --git a/src/systems/nonlinear/homotopy_continuation.jl b/src/systems/nonlinear/homotopy_continuation.jl index ddc0b72aed..d74e4bd226 100644 --- a/src/systems/nonlinear/homotopy_continuation.jl +++ b/src/systems/nonlinear/homotopy_continuation.jl @@ -549,13 +549,13 @@ function HomotopyContinuationProblem{false}(sys::System, args...; kwargs...) end function HomotopyContinuationProblem{iip, spec}( - sys::System, u0map, pmap = SciMLBase.NullParameters(); + sys::System, op; kwargs...) where {iip, spec} if !iscomplete(sys) error("A completed `System` is required. Call `complete` or `mtkcompile` on the system before creating a `HomotopyContinuationProblem`") end f, u0, p = process_SciMLProblem( - HomotopyNonlinearFunction{iip, spec}, sys, u0map, pmap; kwargs...) + HomotopyNonlinearFunction{iip, spec}, sys, op; kwargs...) kwargs = filter_kwargs(kwargs) return NonlinearProblem{iip}(f, u0, p; kwargs...) diff --git a/src/systems/nonlinear/initializesystem.jl b/src/systems/nonlinear/initializesystem.jl index a364caaa18..2fd0b8097a 100644 --- a/src/systems/nonlinear/initializesystem.jl +++ b/src/systems/nonlinear/initializesystem.jl @@ -594,7 +594,11 @@ function SciMLBase.remake_initialization_data( filter_missing_values!(u0map) filter_missing_values!(pmap) - op, missing_unknowns, missing_pars = build_operating_point!(sys, + merge!(u0map, pmap) + op = u0map + u0map = anydict() + pmap = anydict() + missing_unknowns, missing_pars = build_operating_point!(sys, op, u0map, pmap, defs, dvs, ps) floatT = float_type_from_varmap(op) u0_constructor = p_constructor = identity diff --git a/src/systems/parameter_buffer.jl b/src/systems/parameter_buffer.jl index 9448f1930e..bd77c8c519 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, + sys::AbstractSystem, op; tofloat = false, t0 = nothing, substitution_limit = 1000, floatT = nothing, p_constructor = identity) ic = if has_index_cache(sys) && get_index_cache(sys) !== nothing @@ -40,17 +40,17 @@ function MTKParameters( dvs = unknowns(sys) ps = parameters(sys; initial_parameters = true) - u0 = to_varmap(u0, dvs) - symbols_to_symbolics!(sys, u0) - p = to_varmap(p, ps) - symbols_to_symbolics!(sys, p) + op = to_varmap(op, ps) + symbols_to_symbolics!(sys, op) defs = add_toterms(recursive_unwrap(defaults(sys))) - is_time_dependent(sys) && add_observed!(sys, u0) - add_parameter_dependencies!(sys, p) + is_time_dependent(sys) && add_observed!(sys, op) + add_parameter_dependencies!(sys, op) - op, missing_unknowns, missing_pars = build_operating_point!(sys, - u0, p, defs, dvs, ps) + u0map = anydict() + pmap = anydict() + missing_unknowns, missing_pars = build_operating_point!(sys, op, + u0map, pmap, defs, dvs, ps) if t0 !== nothing op[get_iv(sys)] = t0 diff --git a/src/systems/problem_utils.jl b/src/systems/problem_utils.jl index 8cff86f9d3..1814d50e83 100644 --- a/src/systems/problem_utils.jl +++ b/src/systems/problem_utils.jl @@ -540,8 +540,8 @@ Also updates `u0map` and `pmap` in-place to contain all the initial conditions i by unknowns and parameters respectively. """ function build_operating_point!(sys::AbstractSystem, - u0map::AbstractDict, pmap::AbstractDict, defs::AbstractDict, dvs, ps) - op = add_toterms(u0map) + op::AbstractDict, u0map::AbstractDict, pmap::AbstractDict, defs::AbstractDict, dvs, ps) + add_toterms!(op) missing_unknowns = add_fallbacks!(op, dvs, defs) for (k, v) in defs haskey(op, k) && continue @@ -594,7 +594,7 @@ function build_operating_point!(sys::AbstractSystem, pmap[k] = v end - return op, missing_unknowns, missing_pars + return missing_unknowns, missing_pars end """ @@ -1180,8 +1180,8 @@ Keyword arguments: - `build_initializeprob`: If `false`, avoids building the initialization problem. - `t`: The initial time of the `ODEProblem`. If this is not provided, the initialization problem cannot be built. -- `implicit_dae`: Also build a mapping of derivatives of states to values for implicit DAEs, - using `du0map`. Changes the return value of this function to `(f, du0, u0, p)` instead of +- `implicit_dae`: Also build a mapping of derivatives of states to values for implicit DAEs. + Changes the return value of this function to `(f, du0, u0, p)` instead of `(f, u0, p)`. - `guesses`: The guesses for variables in the system, used as initial values for the initialization problem. @@ -1197,7 +1197,6 @@ Keyword arguments: - `u0_constructor`: A function to apply to the `u0` value returned from `better_varmap_to_vars` to construct the final `u0` value. - `p_constructor`: A function to apply to each array buffer created when constructing the parameter object. -- `du0map`: A map of derivatives to values. See `implicit_dae`. - `check_length`: Whether to check the number of equations along with number of unknowns and length of `u0` vector for consistency. If `false`, do not check with equations. This is forwarded to `check_eqs_u0` @@ -1220,13 +1219,13 @@ Keyword arguments: All other keyword arguments are passed as-is to `constructor`. """ function process_SciMLProblem( - constructor, sys::AbstractSystem, u0map, pmap; + constructor, sys::AbstractSystem, op; build_initializeprob = supports_initialization(sys), 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, - u0_constructor = identity, p_constructor = identity, du0map = nothing, + u0_constructor = identity, p_constructor = identity, check_length = true, symbolic_u0 = false, warn_cyclic_dependency = false, circular_dependency_max_cycle_length = length(all_symbols(sys)), circular_dependency_max_cycles = 10, @@ -1240,15 +1239,12 @@ function process_SciMLProblem( check_array_equations_unknowns(eqs, dvs) - u0Type = typeof(u0map) - pType = typeof(pmap) + u0Type = pType = typeof(op) - u0map = to_varmap(u0map, dvs) - symbols_to_symbolics!(sys, u0map) - pmap = to_varmap(pmap, parameters(sys)) - symbols_to_symbolics!(sys, pmap) + op = to_varmap(op, dvs) + symbols_to_symbolics!(sys, op) - check_inputmap_keys(sys, u0map, pmap) + check_inputmap_keys(sys, op) defs = add_toterms(recursive_unwrap(defaults(sys))) kwargs = NamedTuple(kwargs) @@ -1259,7 +1255,9 @@ function process_SciMLProblem( obs, _ = unhack_observed(observed(sys), Equation[x for x in eqs if x isa Equation]) end - op, missing_unknowns, missing_pars = build_operating_point!(sys, + u0map = anydict() + pmap = anydict() + missing_unknowns, missing_pars = build_operating_point!(sys, op, u0map, pmap, defs, dvs, ps) floatT = Bool @@ -1351,10 +1349,8 @@ function process_SciMLProblem( p = p_constructor(better_varmap_to_vars(op, ps; tofloat, container_type = pType)) end - if implicit_dae && du0map !== nothing + if implicit_dae ddvs = map(Differential(iv), dvs) - du0map = to_varmap(du0map, ddvs) - merge!(op, du0map) du0 = varmap_to_vars(op, ddvs; toterm = identity, tofloat) kwargs = merge(kwargs, (; ddvs)) @@ -1388,22 +1384,17 @@ 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_inputmap_keys(sys, u0map, pmap) +function check_inputmap_keys(sys, op) badvarkeys = Any[] - for k in keys(u0map) + for k in keys(op) if symbolic_type(k) === NotSymbolic() push!(badvarkeys, k) end end - badparamkeys = Any[] - for k in keys(pmap) - if symbolic_type(k) === NotSymbolic() - push!(badparamkeys, k) - end + if !isempty(badvarkeys) + throw(InvalidKeyError(collect(badvarkeys))) end - (isempty(badvarkeys) && isempty(badparamkeys)) || - throw(InvalidKeyError(collect(badvarkeys), collect(badparamkeys))) end const BAD_KEY_MESSAGE = """ @@ -1413,13 +1404,11 @@ const BAD_KEY_MESSAGE = """ struct InvalidKeyError <: Exception vars::Any - params::Any end function Base.showerror(io::IO, e::InvalidKeyError) println(io, BAD_KEY_MESSAGE) - println(io, "u0map: $(join(e.vars, ", "))") - println(io, "pmap: $(join(e.params, ", "))") + println(io, join(e.vars, ", ")) end function SciMLBase.detect_cycles(sys::AbstractSystem, varmap::Dict{Any, Any}, vars) @@ -1566,7 +1555,7 @@ macro fallback_iip_specialize(ex) fn_sarr = nothing if occursin("Problem", string(fnname_name)) # args should at least contain an argument for the `u0map` - @assert length(args) > 3 + @assert length(args) > 2 u0_arg = args[3] # should not have a type-annotation @assert !Meta.isexpr(u0_arg, :(::)) From ff2ed4296b2db4aeca0e66c55093c44ae8fb217a Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 21 May 2025 16:48:33 +0530 Subject: [PATCH 1824/2176] fix: fix `DAEProblem` initialization with new problem syntax --- src/systems/nonlinear/initializesystem.jl | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/systems/nonlinear/initializesystem.jl b/src/systems/nonlinear/initializesystem.jl index 2fd0b8097a..eaf2393e00 100644 --- a/src/systems/nonlinear/initializesystem.jl +++ b/src/systems/nonlinear/initializesystem.jl @@ -65,6 +65,13 @@ function generate_initializesystem_timevarying(sys::AbstractSystem; function process_u0map_with_dummysubs(y, x) y = get(schedule.dummy_sub, y, y) y = fixpoint_sub(y, diffmap) + # FIXME: DAEs provide initial conditions that require reducing the system + # to index zero. If `isdifferential(y)`, an initial condition was given for an + # algebraic variable, so ignore it. Otherwise, the initialization system + # gets a `D(y) ~ ...` equation and errors. This is the same behavior as v9. + if isdifferential(y) + return + end # If we have `D(x) ~ x` and provide [D(x) => x, x => 1.0] to `u0map`, then # without this condition `defs` would get `x => x` instead of retaining # `x => 1.0`. From f663b2e18cb1b53f6b8cfdd601d1b069ea351612 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 21 May 2025 16:50:35 +0530 Subject: [PATCH 1825/2176] fix: use new problem constructor syntax --- src/ModelingToolkit.jl | 2 +- src/linearization.jl | 2 +- src/systems/callbacks.jl | 5 +++-- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/ModelingToolkit.jl b/src/ModelingToolkit.jl index e94c8bb5af..63468917d1 100644 --- a/src/ModelingToolkit.jl +++ b/src/ModelingToolkit.jl @@ -234,7 +234,7 @@ PrecompileTools.@compile_workload begin using ModelingToolkit @variables x(ModelingToolkit.t_nounits) @named sys = System([ModelingToolkit.D_nounits(x) ~ -x], ModelingToolkit.t_nounits) - prob = ODEProblem(mtkcompile(sys), [x => 30.0], (0, 100), [], jac = true) + prob = ODEProblem(mtkcompile(sys), [x => 30.0], (0, 100), jac = true) @mtkmodel __testmod__ begin @constants begin c = 1.0 diff --git a/src/linearization.jl b/src/linearization.jl index 757d6eece5..0c174e4cc3 100644 --- a/src/linearization.jl +++ b/src/linearization.jl @@ -73,7 +73,7 @@ function linearization_function(sys::AbstractSystem, inputs, end prob = ODEProblem{true, SciMLBase.FullSpecialize}( - sys, op, (nothing, nothing), p; allow_incomplete = true, + sys, merge(op, anydict(p)), (nothing, nothing); allow_incomplete = true, algebraic_only = true, guesses) u0 = state_values(prob) diff --git a/src/systems/callbacks.jl b/src/systems/callbacks.jl index f70669eca3..db6e6b9648 100644 --- a/src/systems/callbacks.jl +++ b/src/systems/callbacks.jl @@ -870,8 +870,9 @@ function compile_equational_affect( u_getter = getsym(affsys, [sys_map[u] for u in dvs_to_update]) p_getter = getsym(affsys, [sys_map[p] for p in ps_to_update]) - affprob = ImplicitDiscreteProblem(affsys, [dv => 0 for dv in unknowns(affsys)], - (0, 0), [p => 0.0 for p in parameters(affsys)]; + affprob = ImplicitDiscreteProblem( + affsys, Pair[unknowns(affsys) .=> 0; parameters(affsys) .=> 0], + (0, 0); build_initializeprob = false, check_length = false, eval_expression, eval_module, check_compatibility = false) From 810960441fe89b9570176451045b84a8e2f774d7 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 21 May 2025 16:50:59 +0530 Subject: [PATCH 1826/2176] test: use new problem construction syntax --- test/basic_transformations.jl | 18 +-- test/bvproblem.jl | 19 +-- test/clock.jl | 24 ++-- test/code_generation.jl | 7 +- test/components.jl | 7 +- test/constants.jl | 4 +- test/dae_jacobian.jl | 4 +- test/discrete_system.jl | 2 +- test/distributed.jl | 6 +- test/dq_units.jl | 2 +- test/extensions/dynamic_optimization.jl | 15 +- test/extensions/homotopy_continuation.jl | 6 +- test/fmi/fmi.jl | 7 +- test/guess_propagation.jl | 16 +-- test/initial_values.jl | 30 ++-- test/initializationsystem.jl | 132 +++++++++--------- test/jacobiansparsity.jl | 4 +- test/jumpsystem.jl | 38 ++--- test/mass_matrix.jl | 10 +- test/modelingtoolkitize.jl | 5 +- test/mtkparameters.jl | 12 +- test/nonlinearsystem.jl | 19 ++- test/odesystem.jl | 98 ++++++------- test/optimizationsystem.jl | 31 ++-- test/parameter_dependencies.jl | 35 ++--- test/reduction.jl | 6 +- test/scc_nonlinear_problem.jl | 12 +- test/sciml_problem_inputs.jl | 6 +- test/sdesystem.jl | 49 ++++--- test/serialization.jl | 6 +- test/split_parameters.jl | 10 +- test/state_selection.jl | 8 +- test/static_arrays.jl | 9 +- test/steadystatesystems.jl | 4 +- .../index_reduction.jl | 10 +- test/structural_transformation/tearing.jl | 4 +- test/structural_transformation/utils.jl | 14 +- test/symbolic_events.jl | 36 ++--- test/symbolic_indexing_interface.jl | 6 +- test/symbolic_parameters.jl | 14 +- 40 files changed, 363 insertions(+), 382 deletions(-) diff --git a/test/basic_transformations.jl b/test/basic_transformations.jl index 661b508861..6880f77479 100644 --- a/test/basic_transformations.jl +++ b/test/basic_transformations.jl @@ -14,14 +14,14 @@ D = Differential(t) u0 = [x => 1.0, y => 1.0] p = [α => 1.5, β => 1.0, δ => 3.0, γ => 1.0] tspan = (0.0, 10.0) - prob = ODEProblem(sys, u0, tspan, p) + prob = ODEProblem(sys, [u0; p], tspan) sol = solve(prob, Tsit5()) sys2 = liouville_transform(sys) sys2 = complete(sys2) u0 = [x => 1.0, y => 1.0, sys2.trJ => 1.0] - prob = ODEProblem(sys2, u0, tspan, p, jac = true) + prob = ODEProblem(sys2, [u0; p], tspan, jac = true) sol = solve(prob, Tsit5()) @test sol[end, end] ≈ 1.0742818931017244 end @@ -53,8 +53,8 @@ end M1 = mtkcompile(M1; allow_symbolic = true) M2 = mtkcompile(M2; allow_symbolic = true) - prob1 = ODEProblem(M1, [M1.s => 1.0], (1.0, 4.0), []) - prob2 = ODEProblem(M2, [], (1.0, 2.0), []) + prob1 = ODEProblem(M1, [M1.s => 1.0], (1.0, 4.0)) + prob2 = ODEProblem(M2, [], (1.0, 2.0)) sol1 = solve(prob1, Tsit5(); reltol = 1e-10, abstol = 1e-10) sol2 = solve(prob2, Tsit5(); reltol = 1e-10, abstol = 1e-10) ts = range(0.0, 1.0, length = 50) @@ -125,7 +125,7 @@ end Dx = Differential(Mx.x) u0 = [Mx.y => 0.0, Dx(Mx.y) => 1.0, Mx.t => 0.0] p = [v => 10.0] - prob = ODEProblem(Mx, u0, (0.0, 20.0), p) # 1 = dy/dx = (dy/dt)/(dx/dt) means equal initial horizontal and vertical velocities + prob = ODEProblem(Mx, [u0; p], (0.0, 20.0)) # 1 = dy/dx = (dy/dt)/(dx/dt) means equal initial horizontal and vertical velocities sol = solve(prob, Tsit5(); reltol = 1e-5) @test all(isapprox.(sol[Mx.y], sol[Mx.x - g * (Mx.t)^2 / 2]; atol = 1e-10)) # compare to analytical solution (x(t) = v*t, y(t) = v*t - g*t^2/2) end @@ -138,7 +138,7 @@ end Mx = mtkcompile(Mx; allow_symbolic = true) Dx = Differential(Mx.x) u0 = [Mx.y => 0.0, Dx(Mx.y) => 1.0, Mx.t => 0.0, Mx.xˍt => 10.0] - prob = ODEProblem(Mx, u0, (0.0, 20.0), []) # 1 = dy/dx = (dy/dt)/(dx/dt) means equal initial horizontal and vertical velocities + prob = ODEProblem(Mx, u0, (0.0, 20.0)) # 1 = dy/dx = (dy/dt)/(dx/dt) means equal initial horizontal and vertical velocities sol = solve(prob, Tsit5(); reltol = 1e-5) @test all(isapprox.(sol[Mx.y], sol[Mx.x - g * (Mx.t)^2 / 2]; atol = 1e-10)) # compare to analytical solution (x(t) = v*t, y(t) = v*t - g*t^2/2) end @@ -200,7 +200,7 @@ end _f = LinearInterpolation([1.0, 1.0], [-100.0, +100.0]) # constant value 1 M2s = mtkcompile(M2; allow_symbolic = true) - prob = ODEProblem(M2s, [M2s.y => 0.0], (1.0, 4.0), [fc => _f, f => _f]) + prob = ODEProblem(M2s, [M2s.y => 0.0, fc => _f, f => _f], (1.0, 4.0)) sol = solve(prob, Tsit5(); abstol = 1e-5) @test isapprox(sol(4.0, idxs = M2.y), 12.0; atol = 1e-5) # Anal solution is D(y) ~ 12 => y(t) ~ 12*t + C => y(x) ~ 12*√(x) + C. With y(x=1)=0 => 12*(√(x)-1), so y(x=4) ~ 12 end @@ -227,7 +227,7 @@ end Mx = mtkcompile(Mx; allow_symbolic = true) Dx = Differential(Mx.x) u0 = [Mx.y => 0.0, Dx(Mx.y) => 1.0, Mx.t_units => 0.0, Mx.xˍt_units => 10.0] - prob = ODEProblem(Mx, u0, (0.0, 20.0), []) # 1 = dy/dx = (dy/dt)/(dx/dt) means equal initial horizontal and vertical velocities + prob = ODEProblem(Mx, u0, (0.0, 20.0)) # 1 = dy/dx = (dy/dt)/(dx/dt) means equal initial horizontal and vertical velocities sol = solve(prob, Tsit5(); reltol = 1e-5) # compare to analytical solution (x(t) = v*t, y(t) = v*t - g*t^2/2) @test all(isapprox.(sol[Mx.y], sol[Mx.x - g * (Mx.t_units)^2 / 2]; atol = 1e-10)) @@ -300,7 +300,7 @@ end new_sys = change_independent_variable(sys, sys.x; add_old_diff = true) ss_new_sys = mtkcompile(new_sys; allow_symbolic = true) u0 = [new_sys.y => 0.5, new_sys.t => 0.0] - prob = ODEProblem(ss_new_sys, u0, (0.0, 0.5), []) + prob = ODEProblem(ss_new_sys, u0, (0.0, 0.5)) sol = solve(prob, Tsit5(); reltol = 1e-5) @test sol[new_sys.y][end] ≈ 0.75 end diff --git a/test/bvproblem.jl b/test/bvproblem.jl index bafb3e9674..abc925ccfa 100644 --- a/test/bvproblem.jl +++ b/test/bvproblem.jl @@ -21,11 +21,11 @@ daesolvers = [Ascher2, Ascher4, Ascher6] tspan = (0.0, 10.0) @mtkcompile lotkavolterra = System(eqs, t) - op = ODEProblem(lotkavolterra, u0map, tspan, parammap) + op = ODEProblem(lotkavolterra, [u0map; parammap], tspan) osol = solve(op, Vern9()) bvp = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}( - lotkavolterra, u0map, tspan, parammap) + lotkavolterra, [u0map; parammap], tspan) for solver in solvers sol = solve(bvp, solver(), dt = 0.01) @@ -35,7 +35,7 @@ daesolvers = [Ascher2, Ascher4, Ascher6] # Test out of place bvp2 = SciMLBase.BVProblem{false, SciMLBase.AutoSpecialize}( - lotkavolterra, u0map, tspan, parammap) + lotkavolterra, [u0map; parammap], tspan) for solver in solvers sol = solve(bvp2, solver(), dt = 0.01) @@ -58,10 +58,11 @@ end parammap = [:L => 1.0, :g => 9.81] tspan = (0.0, 6.0) - op = ODEProblem(pend, u0map, tspan, parammap) + op = ODEProblem(pend, [u0map; parammap], tspan) osol = solve(op, Vern9()) - bvp = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}(pend, u0map, tspan, parammap) + bvp = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}( + pend, [u0map; parammap], tspan) for solver in solvers sol = solve(bvp, solver(), dt = 0.01) @test isapprox(sol.u[end], osol.u[end]; atol = 0.01) @@ -70,7 +71,7 @@ end # Test out-of-place bvp2 = SciMLBase.BVProblem{false, SciMLBase.FullSpecialize}( - pend, u0map, tspan, parammap) + pend, [u0map; parammap], tspan) for solver in solvers sol = solve(bvp2, solver(), dt = 0.01) @@ -289,8 +290,8 @@ end @mtkcompile lksys = System(eqs, t; costs, consolidate) @test_throws ModelingToolkit.SystemCompatibilityError ODEProblem( - lksys, u0map, tspan, parammap) - prob = ODEProblem(lksys, u0map, tspan, parammap; check_compatibility = false) + lksys, [u0map; parammap], tspan) + prob = ODEProblem(lksys, [u0map; parammap], tspan; check_compatibility = false) sol = solve(prob, Tsit5()) costfn = ModelingToolkit.generate_cost( lksys; expression = Val{false}, wrap_gfw = Val{true}) @@ -304,7 +305,7 @@ end @mtkcompile lksys = System(eqs, t; costs, consolidate) @test t_c ∈ Set(parameters(lksys)) push!(parammap, t_c => 0.56) - prob = ODEProblem(lksys, u0map, tspan, parammap; check_compatibility = false) + prob = ODEProblem(lksys, [u0map; parammap], tspan; check_compatibility = false) sol = solve(prob, Tsit5()) costfn = ModelingToolkit.generate_cost( lksys; expression = Val{false}, wrap_gfw = Val{true}) diff --git a/test/clock.jl b/test/clock.jl index 7fc9e7ddd2..a50026b38f 100644 --- a/test/clock.jl +++ b/test/clock.jl @@ -120,22 +120,22 @@ eqs = [yd ~ Sample(dt)(y) @test_skip begin Tf = 1.0 - prob = ODEProblem(ss, [x => 0.1], (0.0, Tf), - [kp => 1.0; ud(k - 1) => 2.1; ud(k - 2) => 2.0]) + prob = ODEProblem( + ss, [x => 0.1, kp => 1.0; ud(k - 1) => 2.1; ud(k - 2) => 2.0], (0.0, Tf)) # create integrator so callback is evaluated at t=0 and we can test correct param values int = init(prob, Tsit5(); kwargshandle = KeywordArgSilent) @test sort(vcat(int.p...)) == [0.1, 1.0, 2.1, 2.1, 2.1] # yd, kp, ud(k-1), ud, Hold(ud) - prob = ODEProblem(ss, [x => 0.1], (0.0, Tf), - [kp => 1.0; ud(k - 1) => 2.1; ud(k - 2) => 2.0]) # recreate problem to empty saved values + prob = ODEProblem( + ss, [x => 0.1, kp => 1.0; ud(k - 1) => 2.1; ud(k - 2) => 2.0], (0.0, Tf)) # recreate problem to empty saved values sol = solve(prob, Tsit5(), kwargshandle = KeywordArgSilent) ss_nosplit = mtkcompile(sys; split = false) - prob_nosplit = ODEProblem(ss_nosplit, [x => 0.1], (0.0, Tf), - [kp => 1.0; ud(k - 1) => 2.1; ud(k - 2) => 2.0]) + prob_nosplit = ODEProblem( + ss_nosplit, [x => 0.1, kp => 1.0; ud(k - 1) => 2.1; ud(k - 2) => 2.0], (0.0, Tf)) int = init(prob_nosplit, Tsit5(); kwargshandle = KeywordArgSilent) @test sort(int.p) == [0.1, 1.0, 2.1, 2.1, 2.1] # yd, kp, ud(k-1), ud, Hold(ud) - prob_nosplit = ODEProblem(ss_nosplit, [x => 0.1], (0.0, Tf), - [kp => 1.0; ud(k - 1) => 2.1; ud(k - 2) => 2.0]) # recreate problem to empty saved values + prob_nosplit = ODEProblem( + ss_nosplit, [x => 0.1, kp => 1.0; ud(k - 1) => 2.1; ud(k - 2) => 2.0], (0.0, Tf)) # recreate problem to empty saved values sol_nosplit = solve(prob_nosplit, Tsit5(), kwargshandle = KeywordArgSilent) # For all inputs in parameters, just initialize them to 0.0, and then set them # in the callback. @@ -299,8 +299,8 @@ eqs = [yd ~ Sample(dt)(y) ss_nosplit = mtkcompile(cl; split = false) if VERSION >= v"1.7" - prob = ODEProblem(ss, [x => 0.0], (0.0, 1.0), [kp => 1.0]) - prob_nosplit = ODEProblem(ss_nosplit, [x => 0.0], (0.0, 1.0), [kp => 1.0]) + prob = ODEProblem(ss, [x => 0.0, kp => 1.0], (0.0, 1.0)) + prob_nosplit = ODEProblem(ss_nosplit, [x => 0.0, kp => 1.0], (0.0, 1.0)) sol = solve(prob, Tsit5(), kwargshandle = KeywordArgSilent) sol_nosplit = solve(prob_nosplit, Tsit5(), kwargshandle = KeywordArgSilent) @@ -535,8 +535,8 @@ eqs = [yd ~ Sample(dt)(y) @test int.ps[x] == 2.0 @test int.ps[x(k - 1)] == 1.0 - @test_throws ErrorException ODEProblem(sys, [], (0.0, 10.0), [x => 2.0]) - prob = ODEProblem(sys, [], (0.0, 10.0), [x(k - 1) => 2.0]) + @test_throws ErrorException ODEProblem(sys, [x => 2.0], (0.0, 10.0)) + prob = ODEProblem(sys, [x(k - 1) => 2.0], (0.0, 10.0)) int = init(prob, Tsit5(); kwargshandle = KeywordArgSilent) @test int.ps[x] == 3.0 @test int.ps[x(k - 1)] == 2.0 diff --git a/test/code_generation.jl b/test/code_generation.jl index 9a3805ce21..2d5925cbca 100644 --- a/test/code_generation.jl +++ b/test/code_generation.jl @@ -59,7 +59,7 @@ end @variables x(t) @parameters p[0:2] (f::Function)(..) @mtkcompile sys = System(D(x) ~ p[0] * x + p[1] * t + p[2] + f(p), t) - prob = ODEProblem(sys, [x => 1.0], (0.0, 1.0), [p => [1.0, 2.0, 3.0], f => sum]) + prob = ODEProblem(sys, [x => 1.0, p => [1.0, 2.0, 3.0], f => sum], (0.0, 1.0)) @test prob.ps[p] == [1.0, 2.0, 3.0] @test prob.ps[p[0]] == 1.0 sol = solve(prob, Tsit5()) @@ -72,8 +72,9 @@ end [D(x[0]) ~ p[1] * x[0] + x[2], D(x[1]) ~ p[2] * f(x) + x[2]], t) sys = mtkcompile(sys, inputs = [x[2]], outputs = []) @test is_parameter(sys, x[2]) - prob = ODEProblem(sys, [x[0] => 1.0, x[1] => 1.0], (0.0, 1.0), - [p => ones(2), f => sum, x[2] => 2.0]) + prob = ODEProblem( + sys, [x[0] => 1.0, x[1] => 1.0, x[2] => 2.0, p => ones(2), f => sum], + (0.0, 1.0)) sol = solve(prob, Tsit5()) @test SciMLBase.successful_retcode(sol) end diff --git a/test/components.jl b/test/components.jl index 689173d61f..e71ef3fa45 100644 --- a/test/components.jl +++ b/test/components.jl @@ -119,15 +119,16 @@ include("common/serial_inductor.jl") u0 = unknowns(sys) .=> 0 @test_nowarn ODEProblem( sys, [], (0, 10.0), guesses = u0, warn_initialize_determined = false) - prob = DAEProblem(sys, D.(unknowns(sys)) .=> 0, [], (0, 0.5), guesses = u0) + prob = DAEProblem(sys, D.(unknowns(sys)) .=> 0, (0, 0.5), guesses = u0) sol = solve(prob, DFBDF()) @test sol.retcode == SciMLBase.ReturnCode.Success sys2 = mtkcompile(ll2_model) @test length(equations(sys2)) == 3 - u0 = unknowns(sys) .=> 0 + u0 = [sys.inductor2.i => 0] prob = ODEProblem(sys, u0, (0, 10.0)) - @test_nowarn sol = solve(prob, FBDF()) + sol = solve(prob, FBDF()) + @test SciMLBase.successful_retcode(sol) end @testset "Compose/extend" begin diff --git a/test/constants.jl b/test/constants.jl index ec98dab9b4..ce5c7e6e8e 100644 --- a/test/constants.jl +++ b/test/constants.jl @@ -12,7 +12,7 @@ UMT = ModelingToolkit.UnitfulUnitCheck D = Differential(t) eqs = [D(x) ~ a] @named sys = System(eqs, t) -prob = ODEProblem(complete(sys), [0], [0.0, 1.0], []) +prob = ODEProblem(complete(sys), [0], [0.0, 1.0]) sol = solve(prob, Tsit5()) # Test mtkcompile substitutions & observed values @@ -43,7 +43,7 @@ simp = mtkcompile(sys) @mtkcompile fol_model = System(eqs, MT.t_nounits) - prob = ODEProblem(fol_model, [], (0.0, 10.0), [h => 1]) + prob = ODEProblem(fol_model, [h => 1], (0.0, 10.0)) @test prob[x] ≈ 1 @test prob.ps[τ] ≈ 0.5 end diff --git a/test/dae_jacobian.jl b/test/dae_jacobian.jl index 94f15cbb7c..17b14b39e4 100644 --- a/test/dae_jacobian.jl +++ b/test/dae_jacobian.jl @@ -43,12 +43,12 @@ u0 = [u1 => 1.0, tspan = (0.0, 10.0) -du0 = [0.5, -2.0] +du0 = [D(u1) => 0.5, D(u2) => -2.0] p = [p1 => 1.5, p2 => 3.0] -prob = DAEProblem(complete(sys), du0, u0, tspan, p, jac = true, sparse = true) +prob = DAEProblem(complete(sys), [du0; u0; p], tspan, jac = true, sparse = true) sol = solve(prob, IDA(linear_solver = :KLU)) @test maximum(sol - sol1) < 1e-12 diff --git a/test/discrete_system.jl b/test/discrete_system.jl index 2db2aab8d3..dc0281c8bf 100644 --- a/test/discrete_system.jl +++ b/test/discrete_system.jl @@ -51,7 +51,7 @@ reorderer = getu(syss, [S, I, R]) u0 = [S(k - 1) => 990.0, I(k - 1) => 10.0, R(k - 1) => 0.0] p = [β => 0.05, c => 10.0, γ => 0.25, δt => 0.1, nsteps => 400] tspan = (0.0, ModelingToolkit.value(substitute(nsteps, p))) # value function (from Symbolics) is used to convert a Num to Float64 -prob_map = DiscreteProblem(syss, u0, tspan, p) +prob_map = DiscreteProblem(syss, [u0; p], tspan) @test prob_map.f.sys === syss # Solution diff --git a/test/distributed.jl b/test/distributed.jl index e7edc17a66..8c3ca4dcfa 100644 --- a/test/distributed.jl +++ b/test/distributed.jl @@ -16,10 +16,10 @@ addprocs(2) @everywhere @named de = System(eqs, t) @everywhere de = complete(de) -@everywhere u0 = [19.0, 20.0, 50.0] -@everywhere params = [16.0, 45.92, 4] +@everywhere u0 = unknowns(de) .=> [19.0, 20.0, 50.0] +@everywhere params = parameters(de) .=> [16.0, 45.92, 4] -@everywhere ode_prob = ODEProblem(de, u0, (0.0, 10.0), params) +@everywhere ode_prob = ODEProblem(de, [u0; params], (0.0, 10.0)) @everywhere begin using OrdinaryDiffEq diff --git a/test/dq_units.jl b/test/dq_units.jl index 74209c3584..615de1d642 100644 --- a/test/dq_units.jl +++ b/test/dq_units.jl @@ -216,7 +216,7 @@ end p = [pend.g => 1.0, pend.L => 1.0] guess = [pend.λ => 0.0] @test prob = ODEProblem( - pend, u0, (0.0, 1.0), p; guesses = guess, check_units = false) isa Any + pend, [u0; p], (0.0, 1.0); guesses = guess, check_units = false) isa Any end @parameters p [unit = u"L/s"] d [unit = u"s^(-1)"] diff --git a/test/extensions/dynamic_optimization.jl b/test/extensions/dynamic_optimization.jl index b9a512707c..62553121fe 100644 --- a/test/extensions/dynamic_optimization.jl +++ b/test/extensions/dynamic_optimization.jl @@ -29,7 +29,7 @@ const M = ModelingToolkit jprob = JuMPDynamicOptProblem(sys, u0map, tspan, parammap, dt = 0.01) @test JuMP.num_constraints(jprob.model) == 2 # initials jsol = solve(jprob, Ipopt.Optimizer, constructRK4, silent = true) - oprob = ODEProblem(sys, u0map, tspan, parammap) + oprob = ODEProblem(sys, [u0map; parammap], tspan) osol = solve(oprob, SimpleRK4(), dt = 0.01) cprob = CasADiDynamicOptProblem(sys, u0map, tspan, parammap, dt = 0.01) csol = solve(cprob, "ipopt", constructRK4) @@ -141,7 +141,7 @@ end @parameters (u_interp::ConstantInterpolation)(..) @mtkcompile block_ode = System([D(x(t)) ~ v(t), D(v(t)) ~ u_interp(t)], t) spline = ctrl_to_spline(jsol.input_sol, ConstantInterpolation) - oprob = ODEProblem(block_ode, u0map, tspan, [u_interp => spline]) + oprob = ODEProblem(block_ode, [u0map; [u_interp => spline]], tspan) osol = solve(oprob, Vern8(), dt = 0.01, adaptive = false) @test ≈(jsol.sol.u, osol.u, rtol = 0.05) @test ≈(csol.sol.u, osol.u, rtol = 0.05) @@ -185,10 +185,9 @@ end D(q(t)) ~ -ν * q(t) + c * (1 - α_interp(t)) * s * w(t)] @mtkcompile beesys_ode = System(eqs, t) oprob = ODEProblem(beesys_ode, - u0map, - tspan, - merge(Dict(pmap), - Dict(α_interp => ctrl_to_spline(jsol.input_sol, LinearInterpolation)))) + merge(Dict(u0map), Dict(pmap), + Dict(α_interp => ctrl_to_spline(jsol.input_sol, LinearInterpolation))), + tspan) osol = solve(oprob, Tsit5(); dt = 0.01, adaptive = false) @test ≈(osol.u, jsol.sol.u, rtol = 0.01) @test ≈(osol.u, csol.sol.u, rtol = 0.01) @@ -239,13 +238,13 @@ end D(m(t)) ~ -T_interp(t) / c] @mtkcompile rocket_ode = System(eqs, t) interpmap = Dict(T_interp => ctrl_to_spline(jsol.input_sol, CubicSpline)) - oprob = ODEProblem(rocket_ode, u0map, (ts, te), merge(Dict(pmap), interpmap)) + oprob = ODEProblem(rocket_ode, merge(Dict(u0map), Dict(pmap), interpmap), (ts, te)) osol = solve(oprob, RadauIIA5(); adaptive = false, dt = 0.001) @test ≈(jsol.sol.u, osol.u, rtol = 0.02) @test ≈(csol.sol.u, osol.u, rtol = 0.02) interpmap1 = Dict(T_interp => ctrl_to_spline(isol.input_sol, CubicSpline)) - oprob1 = ODEProblem(rocket_ode, u0map, (ts, te), merge(Dict(pmap), interpmap1)) + oprob1 = ODEProblem(rocket_ode, merge(Dict(u0map), Dict(pmap), interpmap1), (ts, te)) osol1 = solve(oprob1, ImplicitEuler(); adaptive = false, dt = 0.001) @test ≈(isol.sol.u, osol1.u, rtol = 0.01) end diff --git a/test/extensions/homotopy_continuation.jl b/test/extensions/homotopy_continuation.jl index f9abeaa449..a4af57c8fe 100644 --- a/test/extensions/homotopy_continuation.jl +++ b/test/extensions/homotopy_continuation.jl @@ -61,8 +61,8 @@ end 0 ~ y * z + 4x^2 + wrapper(r)] @mtkcompile sys = System(eqs) - prob = HomotopyContinuationProblem(sys, [x => 1.0, y => 1.0, z => 1.0], - [p => 2.0, q => 4, r => Wrapper([1.0 1.0; 0.0 0.0])]) + prob = HomotopyContinuationProblem(sys, + [x => 1.0, y => 1.0, z => 1.0, p => 2.0, q => 4, r => Wrapper([1.0 1.0; 0.0 0.0])]) @test prob.ps[p] == 2.0 @test prob.ps[q] == 4 @test prob.ps[r].x == [1.0 1.0; 0.0 0.0] @@ -79,7 +79,7 @@ end _x = collect(x) eqs = collect(0 .~ vec(sum(_x * _x'; dims = 2)) + collect(p)) @mtkcompile sys = System(eqs) - prob = HomotopyContinuationProblem(sys, [x => ones(3)], [p => 1:3]) + prob = HomotopyContinuationProblem(sys, [x => ones(3), p => 1:3]) @test prob[x] == ones(3) @test prob[p + x] == [2, 3, 4] prob[x] = 2ones(3) diff --git a/test/fmi/fmi.jl b/test/fmi/fmi.jl index 29b2dc1224..e0eaf373d0 100644 --- a/test/fmi/fmi.jl +++ b/test/fmi/fmi.jl @@ -127,8 +127,9 @@ end systems = [adder]) # c will be solved for by initialization # this tests that initialization also works with FMUs - prob = ODEProblem(sys, [sys.adder.c => 2.0, sys.a => 1.0, sys.b => 1.0], - (0.0, 1.0), [sys.adder.value => 2.0]) + prob = ODEProblem( + sys, [sys.adder.c => 2.0, sys.a => 1.0, sys.b => 1.0, sys.adder.value => 2.0], + (0.0, 1.0)) return sys, prob end @@ -182,7 +183,7 @@ end ) prob = ODEProblem( - sys, [sys.sspace.x => 1.0], (0.0, 1.0), [sys.sspace.A => 2.0]; use_scc = false) + sys, [sys.sspace.x => 1.0, sys.sspace.A => 2.0], (0.0, 1.0); use_scc = false) return sys, prob end diff --git a/test/guess_propagation.jl b/test/guess_propagation.jl index 0be9506bb9..62a6ae9871 100644 --- a/test/guess_propagation.jl +++ b/test/guess_propagation.jl @@ -13,7 +13,7 @@ initialization_eqs = [1 ~ exp(1 + x)] @named sys = System(eqs, t; initialization_eqs) sys = complete(mtkcompile(sys)) tspan = (0.0, 0.2) -prob = ODEProblem(sys, [], tspan, []) +prob = ODEProblem(sys, [], tspan) @test prob.f.initializeprob[y] == 2.0 @test prob.f.initializeprob[x] == 2.0 @@ -30,7 +30,7 @@ initialization_eqs = [1 ~ exp(1 + x)] @named sys = System(eqs, t; initialization_eqs) sys = complete(mtkcompile(sys)) tspan = (0.0, 0.2) -prob = ODEProblem(sys, [], tspan, []) +prob = ODEProblem(sys, [], tspan) @test prob.f.initializeprob[x] == 2.0 @test prob.f.initializeprob[y] == 2.0 @@ -49,7 +49,7 @@ initialization_eqs = [1 ~ exp(1 + x)] sys = complete(mtkcompile(sys)) tspan = (0.0, 0.2) -prob = ODEProblem(sys, [], tspan, []) +prob = ODEProblem(sys, [], tspan) @test prob.f.initializeprob[x] == -1.0 sol = solve(prob.f.initializeprob; show_trace = Val(true)) @@ -69,7 +69,7 @@ initialization_eqs = [1 ~ exp(1 + x)] sys = complete(mtkcompile(sys)) tspan = (0.0, 0.2) -prob = ODEProblem(sys, [], tspan, []) +prob = ODEProblem(sys, [], tspan) @test prob.f.initializeprob[x] == -1.0 sol = solve(prob.f.initializeprob; show_trace = Val(true)) @@ -81,7 +81,7 @@ sol = solve(prob.f.initializeprob; show_trace = Val(true)) @variables x(t) @variables y(t) = x @mtkcompile sys = System([x ~ x0, D(y) ~ x], t) -prob = ODEProblem(sys, [], (0.0, 1.0), [x0 => 1.0]) +prob = ODEProblem(sys, [x0 => 1.0], (0.0, 1.0)) @test prob[x] == 1.0 @test prob[y] == 1.0 @@ -89,7 +89,7 @@ prob = ODEProblem(sys, [], (0.0, 1.0), [x0 => 1.0]) @variables x(t) @variables y(t) = x0 @mtkcompile sys = System([x ~ x0, D(y) ~ x], t) -prob = ODEProblem(sys, [], (0.0, 1.0), [x0 => 1.0]) +prob = ODEProblem(sys, [x0 => 1.0], (0.0, 1.0)) @test prob[x] == 1.0 @test prob[y] == 1.0 @@ -97,7 +97,7 @@ prob = ODEProblem(sys, [], (0.0, 1.0), [x0 => 1.0]) @variables x(t) @variables y(t) = x0 @mtkcompile sys = System([x ~ y, D(y) ~ x], t) -prob = ODEProblem(sys, [], (0.0, 1.0), [x0 => 1.0]) +prob = ODEProblem(sys, [x0 => 1.0], (0.0, 1.0)) @test prob[x] == 1.0 @test prob[y] == 1.0 @@ -105,6 +105,6 @@ prob = ODEProblem(sys, [], (0.0, 1.0), [x0 => 1.0]) @variables x(t) = x0 @variables y(t) = x @mtkcompile sys = System([x ~ y, D(y) ~ x], t) -prob = ODEProblem(sys, [], (0.0, 1.0), [x0 => 1.0]) +prob = ODEProblem(sys, [x0 => 1.0], (0.0, 1.0)) @test prob[x] == 1.0 @test prob[y] == 1.0 diff --git a/test/initial_values.jl b/test/initial_values.jl index 347f794e8d..ef6c404440 100644 --- a/test/initial_values.jl +++ b/test/initial_values.jl @@ -64,7 +64,7 @@ tspan = (0.0, 1.0) ps = [k1 => 1.0, k2 => 5.0] # Broken since we need both X1 and X2 to initialize Γ but this makes the initialization system # overdetermined because parameter initialization isn't in yet -@test_warn "overdetermined" oprob=ODEProblem(osys_m, u0, tspan, ps) +@test_warn "overdetermined" oprob=ODEProblem(osys_m, [u0; ps], tspan) # Make sure it doesn't error on array variables with unspecified size @parameters p::Vector{Real} q[1:3] @@ -93,7 +93,7 @@ eqs = [D(D(z)) ~ ones(2, 2)] B1 => A1, B2 => A2 ]) -prob = ODEProblem(sys, [], (0.0, 1.0), [A1 => 0.3]) +prob = ODEProblem(sys, [A1 => 0.3], (0.0, 1.0)) @test prob.ps[B1] == 0.3 @test prob.ps[B2] == 0.7 @@ -109,7 +109,7 @@ end @variables x(t) @parameters p @mtkcompile sys = System([D(x) ~ p], t; defaults = [x => t, p => 2t]) -prob = ODEProblem(sys, [], (1.0, 2.0), []) +prob = ODEProblem(sys, [], (1.0, 2.0)) @test prob[x] == 1.0 @test prob.ps[p] == 2.0 @@ -140,7 +140,7 @@ end @mtkcompile osys = System(eqs, t, sps, ps) u0map = [x => 1.0] pmap = [c1 => 5.0, c2 => 1.0, c3 => 1.2] - oprob = ODEProblem(osys, u0map, (0.0, 10.0), pmap) + oprob = ODEProblem(osys, [u0map; pmap], (0.0, 10.0)) end @testset "Cyclic dependency checking and substitution limits" begin @@ -160,12 +160,12 @@ end [D(x) ~ x * p, D(y) ~ y * q], t; guesses = [p => 1.0, q => 2.0]) # "unknowns" because they are initialization unknowns @test_warn ["Cycle", "unknowns", "p", "q"] try - ODEProblem(sys, [x => 1, y => 2], (0.0, 1.0), - [p => 2q, q => 3p]; warn_cyclic_dependency = true) + ODEProblem(sys, [x => 1, y => 2, p => 2q, q => 3p], + (0.0, 1.0); warn_cyclic_dependency = true) catch end @test_throws ModelingToolkit.MissingGuessError ODEProblem( - sys, [x => 1, y => 2], (0.0, 1.0), [p => 2q, q => 3p]) + sys, [x => 1, y => 2, p => 2q, q => 3p], (0.0, 1.0)) end @testset "`add_fallbacks!` checks scalarized array parameters correctly" begin @@ -201,8 +201,8 @@ end @mtkcompile pend = System(eqs, t) @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]) + pend, [x => 1, g => 1], (0, 1), guesses = [y => λ, λ => y + 1]) + ODEProblem(pend, [x => 1, g => 1], (0, 1), guesses = [y => λ, λ => 0.5]) # Throw multiple if multiple are missing @variables a(t) b(t) c(t) d(t) e(t) @@ -221,7 +221,7 @@ end @mtkcompile sys = System(D(x) ~ interp(t), t) - prob = ODEProblem(sys, [x => 0.0], (0.0, 1.0), [interp => spline]) + prob = ODEProblem(sys, [x => 0.0, interp => spline], (0.0, 1.0)) spline2 = LinearInterpolation(ts .^ 2, ts .^ 2) p_new = [interp => spline2] prob2 = remake(prob; p = p_new) @@ -241,7 +241,7 @@ end # Creates the `NonlinearProblem`. u0 = [X => [1.0, 2.0]] ps = [p => [4.0, 5.0]] - @test_nowarn NonlinearProblem(nlsys, u0, ps) + @test_nowarn NonlinearProblem(nlsys, [u0; ps]) end @testset "Issue#3553: Retain `Float32` initial values" begin @@ -251,7 +251,7 @@ end @mtkcompile osys = System(eqs, t) u0 = [X => 1.0f0] ps = [p => 1.0f0, d => 2.0f0] - oprob = ODEProblem(osys, u0, (0.0f0, 1.0f0), ps) + oprob = ODEProblem(osys, [u0; ps], (0.0f0, 1.0f0)) sol = solve(oprob) @test eltype(oprob.u0) == Float32 @test eltype(eltype(sol.u)) == Float32 @@ -268,7 +268,7 @@ end @test Initial(D(x[i])) in ps end @test p in ps - prob = ODEProblem(sys, [x => ones(2)], (0.0, 1.0), [p => 1.0]) + prob = ODEProblem(sys, [x => ones(2), p => 1.0], (0.0, 1.0)) @test prob.p isa Vector{Float64} @test length(prob.p) == 5 end @@ -295,7 +295,7 @@ end ρ => 10.0] tspan = (0.0, 100.0) - prob = ODEProblem(sys, u0, tspan, p, jac = true, guesses = [w2 => -1.0], + prob = ODEProblem(sys, [u0; p], tspan, jac = true, guesses = [w2 => -1.0], warn_initialize_determined = false) @test prob[w2] ≈ -1.0 @test prob.ps[β] ≈ 8 / 3 @@ -321,7 +321,7 @@ end β => 8.0f0 / 3] tspan = (0.0f0, 100.0f0) - prob = ODEProblem(sys, u0, tspan, p) + prob = ODEProblem(sys, [u0; p], tspan) @test prob.p.tunable isa SVector @test prob.p.initials isa SVector end diff --git a/test/initializationsystem.jl b/test/initializationsystem.jl index 8c1634e4e4..9c9d2a2e7a 100644 --- a/test/initializationsystem.jl +++ b/test/initializationsystem.jl @@ -46,7 +46,7 @@ sol = solve(initprob) pend, 0.0, [], [g => 1]; guesses = ModelingToolkit.missing_variable_defaults(pend), fully_determined = true) -prob = ODEProblem(pend, [x => 1, y => 0], (0.0, 1.5), [g => 1], +prob = ODEProblem(pend, [x => 1, y => 0, g => 1], (0.0, 1.5), guesses = ModelingToolkit.missing_variable_defaults(pend)) prob.f.initializeprob isa NonlinearProblem sol = solve(prob.f.initializeprob) @@ -54,7 +54,7 @@ sol = solve(prob.f.initializeprob) sol = solve(prob, Rodas5P()) @test maximum(abs.(sol[conditions][1])) < 1e-14 -prob = ODEProblem(pend, [x => 1], (0.0, 1.5), [g => 1], +prob = ODEProblem(pend, [x => 1, g => 1], (0.0, 1.5), guesses = ModelingToolkit.missing_variable_defaults(pend)) prob.f.initializeprob isa NonlinearLeastSquaresProblem sol = solve(prob.f.initializeprob) @@ -63,7 +63,7 @@ sol = solve(prob, Rodas5P()) @test maximum(abs.(sol[conditions][1])) < 1e-14 @test_throws ModelingToolkit.ExtraVariablesSystemException ODEProblem( - pend, [x => 1], (0.0, 1.5), [g => 1], + pend, [x => 1, g => 1], (0.0, 1.5), guesses = ModelingToolkit.missing_variable_defaults(pend), fully_determined = true) @@ -360,7 +360,7 @@ p = [σ => 28.0, tspan = (0.0, 100.0) @test_throws ModelingToolkit.IncompleteInitializationError prob=ODEProblem( - sys, u0, tspan, p, jac = true) + sys, [u0; p], tspan, jac = true) # DAE Initialization on ODE with nonlinear system for initial conditions # https://github.com/SciML/ModelingToolkit.jl/issues/2508 @@ -428,7 +428,7 @@ sol = solve(prob, Tsit5()) β => 8 / 3] tspan = (0.0, 0.2) - prob_mtk = ODEProblem(sys, u0, tspan, p) + prob_mtk = ODEProblem(sys, [u0; p], tspan) sol = solve(prob_mtk, Tsit5()) @test sol[x * (ρ - z) - y][1] == 0.0 @@ -474,7 +474,7 @@ eqs = [D(D(x)) ~ λ * x x^2 + y^2 ~ 1] @mtkcompile pend = System(eqs, t) -prob = ODEProblem(pend, [x => 1], (0.0, 1.5), [g => 1], +prob = ODEProblem(pend, [x => 1, g => 1], (0.0, 1.5), guesses = [λ => 0, y => 1], initialization_eqs = [y ~ 1]) unsimp = generate_initializesystem(pend; u0map = [x => 1], initialization_eqs = [y ~ 1]) @@ -496,7 +496,7 @@ sys = extend(sysx, sysy) @named sys = System([x^2 + y^2 ~ 25, D(x) ~ 1], t) ssys = mtkcompile(sys) @test_throws ModelingToolkit.MissingVariablesError ODEProblem( - ssys, [x => 3], (0, 1), []) # y should have a guess + ssys, [x => 3], (0, 1)) # y should have a guess end # https://github.com/SciML/ModelingToolkit.jl/issues/3025 @@ -506,7 +506,7 @@ end # system 1 should solve to x = 1 ics1 = [x => 1] sys1 = System([D(x) ~ 0], t; defaults = ics1, name = :sys1) |> mtkcompile - prob1 = ODEProblem(sys1, [], (0.0, 1.0), []) + prob1 = ODEProblem(sys1, [], (0.0, 1.0)) sol1 = solve(prob1, Tsit5()) @test all(sol1[x] .== 1) @@ -516,7 +516,7 @@ end System([D(y) ~ 0], t; initialization_eqs = [y ~ 2], name = :sys2) ) |> mtkcompile ics2 = unknowns(sys1) .=> 2 # should be equivalent to "ics2 = [x => 2]" - prob2 = ODEProblem(sys2, ics2, (0.0, 1.0), []; fully_determined = true) + prob2 = ODEProblem(sys2, ics2, (0.0, 1.0); fully_determined = true) sol2 = solve(prob2, Tsit5()) @test all(sol2[x] .== 2) && all(sol2[y] .== 2) end @@ -527,12 +527,12 @@ end sys = System( [D(D(x)) ~ 0], t; initialization_eqs = [x ~ 0, D(x) ~ 1], name = :sys) |> mtkcompile - @test_nowarn ODEProblem(sys, [], (0.0, 1.0), []) + @test_nowarn ODEProblem(sys, [], (0.0, 1.0)) sys = System( [D(D(x)) ~ 0], t; initialization_eqs = [x ~ 0, D(D(x)) ~ 0], name = :sys) |> mtkcompile - @test_nowarn ODEProblem(sys, [D(x) => 1.0], (0.0, 1.0), []) + @test_nowarn ODEProblem(sys, [D(x) => 1.0], (0.0, 1.0)) end # https://github.com/SciML/ModelingToolkit.jl/issues/3049 @@ -543,7 +543,7 @@ end [D(D(x)) ~ 0], t; initialization_eqs = [D(x)^2 ~ 1, x ~ 0], guesses = [D(x) => sign], name = :sys ) |> mtkcompile - prob = ODEProblem(sys, [], (0.0, 1.0), []) + prob = ODEProblem(sys, [], (0.0, 1.0)) sol = solve(prob, Tsit5()) @test sol(1.0, idxs = sys.x) ≈ sign # system with D(x(0)) = ±1 should solve to x(1) = ±1 end @@ -566,10 +566,10 @@ u0_2nd_order_2 = [Y => 2.0, D(Y) => 0.5] tspan = (0.0, 10.0) ps = [ω => 0.5, k1 => 2.0, k2 => 3.0] -oprob_1st_order_1 = ODEProblem(sys_1st_order, u0_1st_order_1, tspan, ps) -oprob_1st_order_2 = ODEProblem(sys_1st_order, u0_1st_order_2, tspan, ps) -oprob_2nd_order_1 = ODEProblem(sys_2nd_order, u0_2nd_order_1, tspan, ps) # gives sys_2nd_order -oprob_2nd_order_2 = ODEProblem(sys_2nd_order, u0_2nd_order_2, tspan, ps) +oprob_1st_order_1 = ODEProblem(sys_1st_order, [u0_1st_order_1; ps], tspan) +oprob_1st_order_2 = ODEProblem(sys_1st_order, [u0_1st_order_2; ps], tspan) +oprob_2nd_order_1 = ODEProblem(sys_2nd_order, [u0_2nd_order_1; ps], tspan) # gives sys_2nd_order +oprob_2nd_order_2 = ODEProblem(sys_2nd_order, [u0_2nd_order_2; ps], tspan) @test solve(oprob_1st_order_1, Rosenbrock23()).retcode == SciMLBase.ReturnCode.InitialFailure @@ -584,7 +584,7 @@ sol = solve(oprob_2nd_order_2, Rosenbrock23()) # retcode: Success @variables x(t)[1:5] y(t)[1:5] @named sys = System([D(x) ~ x, D(y) ~ y], t; initialization_eqs = [y ~ -x]) sys = mtkcompile(sys) - prob = ODEProblem(sys, [sys.x => ones(5)], (0.0, 1.0), []) + prob = ODEProblem(sys, [sys.x => ones(5)], (0.0, 1.0)) sol = solve(prob, Tsit5(), reltol = 1e-4) @test all(sol(1.0, idxs = sys.x) .≈ +exp(1)) && all(sol(1.0, idxs = sys.y) .≈ -exp(1)) end @@ -638,7 +638,7 @@ end @mtkcompile sys = System( [D(x) ~ x * q + rhss[1], D(y) ~ y * p + rhss[2]], t; defaults = [p => missing], guesses = [p => 1.0]) pmap[p] = 2q - prob = Problem(sys, u0map, (0.0, 1.0), pmap; u0_constructor, p_constructor) + prob = Problem(sys, merge(u0map, pmap), (0.0, 1.0); u0_constructor, p_constructor) test_parameter(prob, p, 2.0) prob2 = remake(prob; u0 = u0map, p = pmap) prob2 = remake(prob2; p = setp_oop(prob2, p)(prob2, 0.0)) @@ -657,7 +657,7 @@ end @mtkcompile sys = System( [D(x) ~ x * q + rhss[1], D(y) ~ y * p + rhss[2]], t; defaults = [p => 2q], guesses = [p => 1.0]) pmap[p] = missing - prob = Problem(sys, u0map, (0.0, 1.0), pmap; u0_constructor, p_constructor) + prob = Problem(sys, merge(u0map, pmap), (0.0, 1.0); u0_constructor, p_constructor) test_parameter(prob, p, 2.0) test_initializesystem(prob, p, p ~ 2q) prob2 = remake(prob; u0 = u0map, p = pmap) @@ -666,7 +666,7 @@ end # `missing` to Problem, provided guess @mtkcompile sys = System( [D(x) ~ x + rhss[1], p ~ x + y + rhss[2]], t; guesses = [p => 0.0]) - prob = Problem(sys, u0map, (0.0, 1.0), pmap; u0_constructor, p_constructor) + prob = Problem(sys, merge(u0map, pmap), (0.0, 1.0); u0_constructor, p_constructor) test_parameter(prob, p, 2.0) test_initializesystem(prob, p, p ~ x + y) prob2 = remake(prob; u0 = u0map, p = pmap) @@ -677,7 +677,7 @@ end @mtkcompile sys = System( [D(x) ~ x * q + rhss[1], D(y) ~ y * p + rhss[2]], t; defaults = [p => 2q], guesses = [p => 0.0]) delete!(pmap, p) - prob = Problem(sys, u0map, (0.0, 1.0), pmap; u0_constructor, p_constructor) + prob = Problem(sys, merge(u0map, pmap), (0.0, 1.0); u0_constructor, p_constructor) test_parameter(prob, p, 2.0) test_initializesystem(prob, p, p ~ 2q) prob2 = remake(prob; u0 = u0map, p = pmap) @@ -688,14 +688,14 @@ end @mtkcompile sys = System( [D(x) ~ q * x + rhss[1], D(y) ~ y * p + rhss[2]], t; defaults = [p => 2q], guesses = [p => 1.0]) _pmap = merge(pmap, Dict(p => q)) - prob = Problem(sys, u0map, (0.0, 1.0), _pmap; u0_constructor, p_constructor) + prob = Problem(sys, merge(u0map, _pmap), (0.0, 1.0); u0_constructor, p_constructor) test_parameter(prob, p, _pmap[q]) test_initializesystem(prob, p, p ~ q) # Problem dependent value with guess, no `missing` @mtkcompile sys = System( [D(x) ~ y * q + p + rhss[1], D(y) ~ x * p + q + rhss[2]], t; guesses = [p => 0.0]) _pmap = merge(pmap, Dict(p => 3q)) - prob = Problem(sys, u0map, (0.0, 1.0), _pmap; u0_constructor, p_constructor) + prob = Problem(sys, merge(u0map, _pmap), (0.0, 1.0); u0_constructor, p_constructor) test_parameter(prob, p, 3pmap[q]) # Should not be solved for: @@ -703,7 +703,7 @@ end @mtkcompile sys = System( [D(x) ~ q * x + rhss[1], D(y) ~ y * p + rhss[2]], t; defaults = [p => 2q], guesses = [p => 1.0]) _pmap = merge(pmap, Dict(p => 1.0)) - prob = Problem(sys, u0map, (0.0, 1.0), _pmap; u0_constructor, p_constructor) + prob = Problem(sys, merge(u0map, _pmap), (0.0, 1.0); u0_constructor, p_constructor) @test prob.ps[p] ≈ 1.0 initsys = prob.f.initialization_data.initializeprob.f.sys @test is_parameter(initsys, p) @@ -712,7 +712,8 @@ end @parameters r::Int s::Int @mtkcompile sys = System( [D(x) ~ s * x + rhss[1], D(y) ~ y * r + rhss[2]], t; defaults = [s => 2r], guesses = [s => 1.0]) - prob = Problem(sys, u0map, (0.0, 1.0), [r => 1]; u0_constructor, p_constructor) + prob = Problem( + sys, merge(u0map, Dict(r => 1)), (0.0, 1.0); u0_constructor, p_constructor) @test prob.ps[r] == 1 @test prob.ps[s] == 2 initsys = prob.f.initialization_data.initializeprob.f.sys @@ -725,8 +726,8 @@ end sys, [x => 1.0, y => 1.0], (0.0, 1.0)) # Unsatisfiable initialization - prob = Problem(sys, [x => 1.0, y => 1.0], (0.0, 1.0), - [p => 2.0]; initialization_eqs = [x^2 + y^2 ~ 3], u0_constructor, p_constructor) + prob = Problem(sys, [x => 1.0, y => 1.0, p => 2.0], (0.0, 1.0); + initialization_eqs = [x^2 + y^2 ~ 3], u0_constructor, p_constructor) @test prob.f.initialization_data !== nothing @test solve(prob, alg).retcode == ReturnCode.InitialFailure cache = init(prob, alg) @@ -737,7 +738,7 @@ end @variables x(t) y(t) s(t) @parameters x0 y0 @mtkcompile sys = System([x ~ x0, y ~ y0, s ~ x + y], t; guesses = [y0 => 0.0]) - prob = ODEProblem(sys, [s => 1.0], (0.0, 1.0), [x0 => 0.3, y0 => missing]) + prob = ODEProblem(sys, [s => 1.0, x0 => 0.3, y0 => missing], (0.0, 1.0)) # trivial initialization run immediately @test prob.ps[y0] ≈ 0.7 @test init(prob, Tsit5()).ps[y0] ≈ 0.7 @@ -763,7 +764,7 @@ end t; systems = [fixed, spring, mass, gravity, constant, damper], guesses = [spring.s_rel0 => 1.0]) - prob = ODEProblem(sys, [], (0.0, 1.0), [spring.s_rel0 => missing]) + prob = ODEProblem(sys, [spring.s_rel0 => missing], (0.0, 1.0)) # trivial initialization run immediately @test prob.ps[spring.s_rel0] ≈ -3.905 @test init(prob, Tsit5()).ps[spring.s_rel0] ≈ -3.905 @@ -891,7 +892,7 @@ end ] @mtkcompile sys = System( [D(x) ~ x + rhss[1], p ~ x + y + rhss[2]], t; guesses = [x => 0.0, p => 0.0]) - prob = Problem(sys, [y => 1.0], (0.0, 1.0), [p => 3.0]) + prob = Problem(sys, [y => 1.0, p => 3.0], (0.0, 1.0)) @test prob.f.initialization_data.initializeprob.ps[p] ≈ 3.0 @test init(prob, alg)[x] ≈ 2.0 prob.ps[p] = 2.0 @@ -921,7 +922,7 @@ end @mtkcompile sys = System( [D(x) ~ 2x + r + rhss], t; parameter_dependencies = [r ~ p + 2q, q ~ p + 3], guesses = [p => 1.0]) - prob = Problem(sys, [x => 1.0], (0.0, 1.0), [p => missing]) + prob = Problem(sys, [x => 1.0, p => missing], (0.0, 1.0)) @test length(equations(ModelingToolkit.get_parent(prob.f.initialization_data.initializeprob.f.sys))) == 4 integ = init(prob, alg) @@ -978,7 +979,7 @@ end @mtkcompile sys = System( [D(x) ~ x * p + y^2 * q + rhss; alge_eqs], t; guesses = [x => 0.0, y => 0.0, z => 0.0, p => 0.0, q => 0.0]) - prob = Problem(sys, [x => 1.0], (0.0, 1.0), [p => 1.0, q => missing]) + prob = Problem(sys, [x => 1.0, p => 1.0, q => missing], (0.0, 1.0)) @test is_variable(prob.f.initialization_data.initializeprob, q) ps = prob.p newps = SciMLStructures.replace(Tunable(), ps, ForwardDiff.Dual.(ps.tunable)) @@ -1034,12 +1035,12 @@ end @mtkcompile sys = System( [D(x) ~ x + p * y^2 + rhss; alge_eqs], t; guesses = [ y => 1.0, p => 1.0]) - prob = Problem(sys, [x => 1.0], (0.0, 1.0), [p => 1.0]) + prob = Problem(sys, [x => 1.0, p => 1.0], (0.0, 1.0)) @test is_variable(prob.f.initialization_data.initializeprob, y) prob2 = @test_nowarn remake(prob; p = [p => 3.0]) # ensure no over/under-determined warning @test is_variable(prob.f.initialization_data.initializeprob, y) - prob = Problem(sys, [y => 1.0, x => 2.0], (0.0, 1.0), [p => missing]) + prob = Problem(sys, [y => 1.0, x => 2.0, p => missing], (0.0, 1.0)) @test is_variable(prob.f.initialization_data.initializeprob, p) prob2 = @test_nowarn remake(prob; u0 = [y => 0.5]) @test is_variable(prob.f.initialization_data.initializeprob, p) @@ -1062,7 +1063,7 @@ end @parameters foo(::Real, ::Real) p @mtkcompile sys = System([D(x) ~ t, 0 ~ foo(x, y)], t; parameter_dependencies = [foo ~ Multiplier(p, 2p)], guesses = [y => -1.0]) - prob = ODEProblem(sys, [x => 1.0], (0.0, 1.0), [p => 1.0]) + prob = ODEProblem(sys, [x => 1.0, p => 1.0], (0.0, 1.0)) integ = init(prob, Rosenbrock23()) @test integ[y] ≈ -0.5 end @@ -1101,7 +1102,7 @@ end x^2 + y^2 ~ L] @mtkcompile pend = System(eqs, t) - prob = ODEProblem(pend, [x => 1, y => 0], (0.0, 1.5), [g => 1], + prob = ODEProblem(pend, [x => 1, y => 0, g => 1], (0.0, 1.5), guesses = ModelingToolkit.missing_variable_defaults(pend)) sol = solve(prob, Rodas5P()) @test SciMLBase.successful_retcode(sol) @@ -1185,7 +1186,7 @@ end x^2 + y^2 ~ 1] @mtkcompile pend = System(eqs, t) @test_warn ["structurally singular", "initialization", "Guess", "heuristic"] ODEProblem( - pend, [x => 1, y => 0], (0.0, 1.5), [g => 1], guesses = [λ => 1]) + pend, [x => 1, y => 0, g => 1], (0.0, 1.5), guesses = [λ => 1]) end @testset "DAEProblem initialization" begin @@ -1196,7 +1197,7 @@ end # FIXME: solve for du0 prob = DAEProblem( - sys, [D(x) => cbrt(4), D(y) => -1 / cbrt(4)], [x => 1.0], (0.0, 1.0), [p => 1.0]) + sys, [D(x) => cbrt(4), D(y) => -1 / cbrt(4), x => 1.0, p => 1.0], (0.0, 1.0)) integ = init(prob, DImplicitEuler()) @test integ[x] ≈ 1.0 @@ -1210,7 +1211,7 @@ end @parameters p q=3x @mtkcompile sys = System([D(x) ~ x * p + q, x^3 + y^3 ~ 3], t) prob = ODEProblem( - sys, [], (0.0, 1.0), [p => 1.0]; guesses = [x => 1.0, y => 1.0, q => 1.0]) + sys, [p => 1.0], (0.0, 1.0); guesses = [x => 1.0, y => 1.0, q => 1.0]) @test prob[x] == 1.0 @test prob[y] == 2.0 @test prob.ps[p] == 1.0 @@ -1240,7 +1241,7 @@ end @parameters p [guess = 1.0] q [guess = 1.0] @mtkcompile sys = System( [D(x) ~ p * x + q * y, y ~ 2x], t; parameter_dependencies = [q ~ 2p]) - prob = ODEProblem(sys, [x => 1.0], (0.0, 1.0), [p => 1.0]) + prob = ODEProblem(sys, [x => 1.0, p => 1.0], (0.0, 1.0)) test_dummy_initialization_equation(prob, x) prob2 = remake(prob; u0 = [x => 2.0]) @test prob2[x] == 2.0 @@ -1261,7 +1262,7 @@ end @parameters p [guess = 1.0] q [guess = 1.0] @mtkcompile sys = System( [D(x) ~ p * x + q * y, y ~ 2x], t; parameter_dependencies = [q ~ 2p]) - prob = ODEProblem(sys, [:x => 1.0], (0.0, 1.0), [p => 1.0]) + prob = ODEProblem(sys, [:x => 1.0, p => 1.0], (0.0, 1.0)) test_dummy_initialization_equation(prob, x) prob2 = remake(prob; u0 = [:x => 2.0]) test_dummy_initialization_equation(prob2, x) @@ -1276,7 +1277,7 @@ end @named sys = System([D(x) ~ 0, D(y) ~ x + a], t; initialization_eqs = [y ~ a]) ssys = mtkcompile(sys) - prob = ODEProblem(ssys, [], (0, 1), []) + prob = ODEProblem(ssys, [], (0, 1)) @test SciMLBase.successful_retcode(solve(prob)) @@ -1300,7 +1301,7 @@ end u0_vals = [X => 4, Y => 5.0] tspan = (0.0, 10.0) p_vals = [p => 1.0, d => 0.1] - oprob = ODEProblem(osys, u0_vals, tspan, p_vals) + oprob = ODEProblem(osys, [u0_vals; p_vals], tspan) integ = init(oprob) @test integ[X] ≈ 4.0 @test integ[Y] ≈ 5.0 @@ -1326,7 +1327,7 @@ end u0s = [I => 1, R => 0] ps = [S0 => 999, β => 0.01, γ => 0.001] - jprob = JumpProblem(js, u0s, (0.0, 10.0), ps) + jprob = JumpProblem(js, [u0s; ps], (0.0, 10.0)) sol = solve(jprob, SSAStepper()) @test sol[S, 1] ≈ 999 @test SciMLBase.successful_retcode(sol) @@ -1339,7 +1340,7 @@ end D(x) ~ p[1] + p[2] + q, t; defaults = [p[1] => q, p[2] => 2q], guesses = [p[1] => q, p[2] => 2q]) @test ModelingToolkit.is_parameter_solvable(p, Dict(), defaults(sys), guesses(sys)) - prob = ODEProblem(sys, [x => 1.0], (0.0, 1.0), [q => 2.0]) + prob = ODEProblem(sys, [x => 1.0, q => 2.0], (0.0, 1.0)) initsys = prob.f.initialization_data.initializeprob.f.sys @test length(ModelingToolkit.observed(initsys)) == 4 sol = solve(prob, Tsit5()) @@ -1351,7 +1352,7 @@ end @parameters p[1:2, 1:2] @mtkcompile sys = System( [D(x) ~ x, D(y) ~ p * y], t; initialization_eqs = [x^2 + y[1]^2 + y[2]^2 ~ 4]) - prob = ODEProblem(sys, [x => 1.0, y[1] => 1], (0.0, 1.0), [p => 2ones(2, 2)]) + prob = ODEProblem(sys, [x => 1.0, y[1] => 1, p => 2ones(2, 2)], (0.0, 1.0)) integ = init(prob, Tsit5()) @test integ[x] ≈ 1.0 @test integ[y] ≈ [1.0, sqrt(2.0)] @@ -1376,7 +1377,7 @@ end [y ~ 0.5] => (; f = stop!) ]) sys = mtkcompile(sys) - prob0 = ODEProblem(sys, [x => NaN], (0.0, 1.0), []) + prob0 = ODEProblem(sys, [x => NaN], (0.0, 1.0)) # final_x(x0) is equivalent to x0 + 0.5 function final_x(x0) @@ -1399,7 +1400,7 @@ end @parameters p @variables x(t) @mtkcompile sys = System(x ~ p * t, t) - prob = @test_nowarn ODEProblem(sys, [], (0.0, 1.0), [p => 1.0]) + prob = @test_nowarn ODEProblem(sys, [p => 1.0], (0.0, 1.0)) @test_nowarn remake(prob, p = [p => 1.0]) @test_nowarn remake(prob, p = [p => ForwardDiff.Dual(1.0)]) end @@ -1416,7 +1417,7 @@ end u0 = [X1 => 1.0, X2 => 2.0] ps = [k1 => 0.1, k2 => 0.2] - oprob1 = ODEProblem(osys, u0, 1.0, ps) + oprob1 = ODEProblem(osys, [u0; ps], 1.0) oprob2 = remake(oprob1, u0 = [X1 => 10.0]) integ1 = init(oprob1) @test integ1[X1] ≈ 1.0 @@ -1443,7 +1444,7 @@ end @mtkcompile sys = System([x^2 + y^2 ~ p1, (x - 1)^2 + (y - 1)^2 ~ p2]; parameter_dependencies = [p2 ~ 2p1], guesses = [p1 => 0.0], defaults = [p1 => missing]) - prob = Problem(sys, [x => 1.0, y => 1.0], [p2 => 6.0]) + prob = Problem(sys, [x => 1.0, y => 1.0, p2 => 6.0]) @test prob.ps[p1] ≈ 3.0 end end @@ -1464,7 +1465,7 @@ end @testset "throws if initialization_eqs contain unknowns" begin u0 = [X1 => 1.0, X2 => 2.0] ps = [k1 => 0.1, k2 => 0.2] - @test_throws ArgumentError Problem(nlsys, u0, ps) + @test_throws ArgumentError Problem(nlsys, [u0; ps]) end eqs = [0 ~ k1 * (Γ[1] - X1) - k2 * X1 @@ -1477,22 +1478,21 @@ end @testset "solves initialization" begin u0 = [X1 => 1.0, X2 => 2.0] ps = [k1 => 0.1, k2 => 0.2] - prob = Problem(nlsys, u0, ps) + prob = Problem(nlsys, [u0; ps]) @test state_values(prob.f.initialization_data.initializeprob) === nothing @test prob.ps[Γ[1]] ≈ 3.0 end @testset "respects explicitly provided value" begin - u0 = [] ps = [k1 => 0.1, k2 => 0.2, Γ => [5.0]] - prob = Problem(nlsys, u0, ps) + prob = Problem(nlsys, ps) @test prob.ps[Γ[1]] ≈ 5.0 end @testset "fails initialization if inconsistent explicit value" begin u0 = [X1 => 1.0, X2 => 2.0] ps = [k1 => 0.1, k2 => 0.2, Γ => [5.0]] - prob = Problem(nlsys, u0, ps) + prob = Problem(nlsys, [u0; ps]) sol = solve(prob) @test sol.retcode == SciMLBase.ReturnCode.InitialFailure end @@ -1500,7 +1500,7 @@ end @testset "Ignores initial equation if given insufficient u0" begin u0 = [X2 => 2.0] ps = [k1 => 0.1, k2 => 0.2, Γ => [5.0]] - prob = Problem(nlsys, u0, ps) + prob = Problem(nlsys, [u0; ps]) sol = solve(prob) @test SciMLBase.successful_retcode(sol) @test sol.ps[Γ[1]] ≈ 5.0 @@ -1511,7 +1511,7 @@ end @variables x(t) y(t) @parameters c1 c2 @mtkcompile sys = System([D(x) ~ -c1 * x + c2 * y, D(y) ~ c1 * x - c2 * y], t) - prob1 = ODEProblem(sys, [1.0, 2.0], (0.0, 1.0), [c1 => 1.0, c2 => 2.0]) + prob1 = ODEProblem(sys, [x => 1.0, y => 2.0, c1 => 1.0, c2 => 2.0], (0.0, 1.0)) prob2 = remake(prob1, u0 = [2.0, 3.0]) prob3 = remake(prob1, u0 = [2.0, 3.0], p = [c1 => 2.0]) integ1 = init(prob1, Tsit5()) @@ -1542,8 +1542,8 @@ end @mtkcompile pend = System(eqs, t) prob = ODEProblem( - pend, [x => (√2 / 2), D(x) => 0.0], (0.0, 1.5), - [g => 1], guesses = [λ => 1, y => √2 / 2]) + pend, [x => (√2 / 2), D(x) => 0.0, g => 1], (0.0, 1.5), + guesses = [λ => 1, y => √2 / 2]) sol = solve(prob) @testset "Guesses of initialization problem copied to algebraic variables" begin @@ -1555,8 +1555,8 @@ end @testset "Initial values for algebraic variables are retained" begin prob2 = ODEProblem( - pend, [x => (√2 / 2), D(y) => 0.0], (0.0, 1.5), - [g => 1], guesses = [λ => 1, y => √2 / 2]) + pend, [x => (√2 / 2), D(y) => 0.0, g => 1], (0.0, 1.5), + guesses = [λ => 1, y => √2 / 2]) sol = solve(prob) @test SciMLBase.successful_retcode(sol) prob3 = DiffEqBase.get_updated_symbolic_problem( @@ -1607,7 +1607,7 @@ end tspan = (0.0, 100.0) getter = getsym(sys, Initial.(unknowns(sys))) - prob = ODEProblem(sys, u0, tspan, p; guesses = [w2 => 3.0]) + prob = ODEProblem(sys, [u0; p], tspan; guesses = [w2 => 3.0]) new_u0, new_p, _ = SciMLBase.get_initial_values( prob, prob, prob.f, SciMLBase.OverrideInit(), Val(true); nlsolve_alg = NewtonRaphson(), abstol = 1e-6, reltol = 1e-3) @@ -1626,8 +1626,8 @@ end D(D(y)) ~ λ * y - g x^2 + y^2 ~ 1] @mtkcompile pend=System(eqs, t) split=false - prob = ODEProblem(pend, [x => 1.0, D(x) => 0.0], (0.0, 1.0), - [g => 1.0]; guesses = [y => 1.0, λ => 1.0]) + prob = ODEProblem( + pend, [x => 1.0, D(x) => 0.0, g => 1.0], (0.0, 1.0); guesses = [y => 1.0, λ => 1.0]) @test !ModelingToolkit.is_split(prob.f.initialization_data.initializeprob.f.sys) end @@ -1638,8 +1638,8 @@ end D(D(y)) ~ λ * y - g x^2 + y^2 ~ 1] @mtkcompile pend = System(eqs, t) - prob = ODEProblem(pend, SA[x => 1.0, D(x) => 0.0], (0.0, 1.0), - SA[g => 1.0]; guesses = [y => 1.0, λ => 1.0]) + prob = ODEProblem(pend, SA[x => 1.0, D(x) => 0.0, g => 1.0], + (0.0, 1.0); guesses = [y => 1.0, λ => 1.0]) @test !SciMLBase.isinplace(prob) @test !SciMLBase.isinplace(prob.f.initialization_data.initializeprob) end diff --git a/test/jacobiansparsity.jl b/test/jacobiansparsity.jl index 56e8edc183..7173ab9e1a 100644 --- a/test/jacobiansparsity.jl +++ b/test/jacobiansparsity.jl @@ -95,7 +95,7 @@ prob = ODEProblem(sys, u0, (0, 11.5), sparse = true, jac = true) u0 = [x => 1, y => 0] prob = ODEProblem( - pend, u0, (0, 11.5), [g => 1], guesses = [λ => 1], sparse = true, jac = true) + pend, [u0; [g => 1]], (0, 11.5), guesses = [λ => 1], sparse = true, jac = true) jac, jac! = generate_jacobian(pend; expression = Val{false}, sparse = true) jac_prototype = ModelingToolkit.jacobian_sparsity(pend) W_prototype = ModelingToolkit.W_sparsity(pend) @@ -126,7 +126,7 @@ end D(D(y)) ~ λ * y - g x^2 + y^2 ~ 1] @mtkcompile pend = System(eqs, t) - prob = ODEProblem(pend, [x => 0.0, D(x) => 1.0], (0.0, 1.0), [g => 1.0]; + prob = ODEProblem(pend, [x => 0.0, D(x) => 1.0, g => 1.0], (0.0, 1.0); guesses = [y => 1.0, λ => 1.0], jac = true, sparse = true) J = deepcopy(prob.f.jac_prototype) prob.f.jac(J, prob.u0, prob.p, 1.0) diff --git a/test/jumpsystem.jl b/test/jumpsystem.jl index 95cc29e7b0..3eeac9bd5e 100644 --- a/test/jumpsystem.jl +++ b/test/jumpsystem.jl @@ -69,7 +69,7 @@ u₀ = [999, 1, 0]; tspan = (0.0, 250.0); u₀map = [S => 999, I => 1, R => 0] parammap = [β => 0.1 / 1000, γ => 0.01] -jprob = JumpProblem(js2, u₀map, tspan, parammap; aggregator = Direct(), +jprob = JumpProblem(js2, [u₀map; parammap], tspan; aggregator = Direct(), save_positions = (false, false), rng) p = parameter_values(jprob) @test jprob.prob isa DiscreteProblem @@ -85,7 +85,7 @@ end m = getmean(jprob, Nsims) # test auto-alg selection works -jprobb = JumpProblem(js2, u₀map, tspan, parammap; save_positions = (false, false), rng) +jprobb = JumpProblem(js2, [u₀map; parammap], tspan; save_positions = (false, false), rng) mb = getmean(jprobb, Nsims; use_stepper = false) @test abs(m - mb) / m < 0.01 @@ -93,14 +93,14 @@ mb = getmean(jprobb, Nsims; use_stepper = false) obs = [S2 ~ 2 * S] @named js2b = JumpSystem([j₁, j₃], t, [S, I, R], [β, γ, h], observed = obs) js2b = complete(js2b) -jprob = JumpProblem(js2b, u₀map, tspan, parammap; aggregator = Direct(), +jprob = JumpProblem(js2b, [u₀map; parammap], tspan; aggregator = Direct(), save_positions = (false, false), rng) @test jprob.prob isa DiscreteProblem sol = solve(jprob, SSAStepper(); saveat = tspan[2] / 10) @test all(2 .* sol[S] .== sol[S2]) # test save_positions is working -jprob = JumpProblem(js2, u₀map, tspan, parammap; aggregator = Direct(), +jprob = JumpProblem(js2, [u₀map; parammap], tspan; aggregator = Direct(), save_positions = (false, false), rng) sol = solve(jprob, SSAStepper(); saveat = 1.0) @test all((sol.t) .== collect(0.0:tspan[2])) @@ -149,7 +149,7 @@ maj1 = MassActionJump(2 * β / 2, [S => 1, I => 1], [S => -1, I => 1]) maj2 = MassActionJump(γ, [I => 1], [I => -1, R => 1]) @named js3 = JumpSystem([maj1, maj2], t, [S, I, R], [β, γ]) js3 = complete(js3) -jprob = JumpProblem(js3, u₀map, tspan, parammap; aggregator = Direct(), rng) +jprob = JumpProblem(js3, [u₀map; parammap], tspan; aggregator = Direct(), rng) @test jprob.prob isa DiscreteProblem m3 = getmean(jprob, Nsims) @test abs(m - m3) / m < 0.01 @@ -157,11 +157,11 @@ m3 = getmean(jprob, Nsims) # maj jump test with various dep graphs @named js3b = JumpSystem([maj1, maj2], t, [S, I, R], [β, γ]) js3b = complete(js3b) -jprobb = JumpProblem(js3b, u₀map, tspan, parammap; aggregator = NRM(), rng) +jprobb = JumpProblem(js3b, [u₀map; parammap], tspan; aggregator = NRM(), rng) @test jprobb.prob isa DiscreteProblem m4 = getmean(jprobb, Nsims) @test abs(m - m4) / m < 0.01 -jprobc = JumpProblem(js3b, u₀map, tspan, parammap; aggregator = RSSA(), rng) +jprobc = JumpProblem(js3b, [u₀map; parammap], tspan; aggregator = RSSA(), rng) @test jprobc.prob isa DiscreteProblem m4 = getmean(jprobc, Nsims) @test abs(m - m4) / m < 0.01 @@ -172,7 +172,7 @@ maj2 = MassActionJump(γ, [S => 1], [S => -1]) @named js4 = JumpSystem([maj1, maj2], t, [S], [β, γ]) js4 = complete(js4) jprob = JumpProblem( - js4, [S => 999], (0, 1000.0), [β => 100.0, γ => 0.01]; aggregator = Direct(), rng) + js4, [S => 999, β => 100.0, γ => 0.01], (0, 1000.0); aggregator = Direct(), rng) @test jprob.prob isa DiscreteProblem m4 = getmean(jprob, Nsims) @test abs(m4 - 2.0 / 0.01) * 0.01 / 2.0 < 0.01 @@ -183,7 +183,7 @@ maj2 = MassActionJump(γ, [S => 2], [S => -1]) @named js4 = JumpSystem([maj1, maj2], t, [S], [β, γ]) js4 = complete(js4) jprob = JumpProblem( - js4, [S => 999], (0, 1000.0), [β => 100.0, γ => 0.01]; aggregator = Direct(), rng) + js4, [S => 999, β => 100.0, γ => 0.01], (0, 1000.0); aggregator = Direct(), rng) @test jprob.prob isa DiscreteProblem sol = solve(jprob, SSAStepper()); @@ -208,7 +208,7 @@ end u₀ = [A => 100, B => 0] tspan = (0.0, 2000.0) jprob = JumpProblem( - js5, u₀, tspan, p; aggregator = Direct(), save_positions = (false, false), rng) + js5, [u₀; p], tspan; aggregator = Direct(), save_positions = (false, false), rng) @test jprob.prob isa DiscreteProblem @test all(jprob.massaction_jump.scaled_rates .== [1.0, 0.0]) @@ -274,9 +274,9 @@ u0 = [X => 10] tspan = (0.0, 1.0) ps = [k => 1.0] -@test_nowarn jp1 = JumpProblem(js1, u0, tspan, ps; aggregator = Direct()) +@test_nowarn jp1 = JumpProblem(js1, [u0; ps], tspan; aggregator = Direct()) @test_nowarn jp2 = JumpProblem(js2, u0, tspan; aggregator = Direct()) -@test_nowarn jp3 = JumpProblem(js3, u0, tspan, ps; aggregator = Direct()) +@test_nowarn jp3 = JumpProblem(js3, [u0; ps], tspan; aggregator = Direct()) @test_nowarn jp4 = JumpProblem(js4, u0, tspan; aggregator = Direct()) # Ensure `mtkcompile` (and `@mtkcompile`) works on JumpSystem (by doing nothing) @@ -302,7 +302,7 @@ j1 = ConstantRateJump(k, [X ~ Pre(X) - 1]) for (N, algtype) in zip(Nv, algtypes) @named jsys = JumpSystem([deepcopy(j1) for _ in 1:N], t, [X], [k]) jsys = complete(jsys) - jprob = JumpProblem(jsys, [X => 10], (0.0, 10.0), [k => 1]) + jprob = JumpProblem(jsys, [X => 10, k => 1], (0.0, 10.0)) @test jprob.aggregator isa algtype end end @@ -316,7 +316,7 @@ end vrj = VariableRateJump(k * (sin(t) + 1), [A ~ Pre(A) + 1, C ~ Pre(C) + 2]) js = complete(JumpSystem([vrj], t, [A, C], [k]; name = :js, observed = [B ~ C * A])) jprob = JumpProblem( - js, [A => 0, C => 0], (0.0, 10.0), [k => 1.0]; aggregator = Direct(), rng) + js, [A => 0, C => 0, k => 1], (0.0, 10.0); aggregator = Direct(), rng) @test jprob.prob isa ODEProblem sol = solve(jprob, Tsit5()) @@ -449,7 +449,7 @@ end k2val = 20.0 p = [k1 => k1val, k2 => k2val] tspan = (0.0, 10.0) - jprob = JumpProblem(jsys, u0, tspan, p; rng, save_positions = (false, false)) + jprob = JumpProblem(jsys, [u0; p], tspan; rng, save_positions = (false, false)) times = range(0.0, tspan[2], length = 100) Nsims = 4000 @@ -488,7 +488,7 @@ end u0map = [X => p.X₀, Y => p.Y₀] pmap = [α => p.α, β => p.β] tspan = (0.0, 20.0) - jprob = JumpProblem(jsys, u0map, tspan, pmap; rng, save_positions = (false, false)) + jprob = JumpProblem(jsys, [u0map; pmap], tspan; rng, save_positions = (false, false)) times = range(0.0, tspan[2], length = 100) Nsims = 4000 Xv = zeros(length(times)) @@ -526,7 +526,7 @@ end continuous_events = cevents) jsys = complete(jsys) tspan = (0.0, 200.0) - jprob = JumpProblem(jsys, u0map, tspan, pmap; rng, save_positions = (false, false)) + jprob = JumpProblem(jsys, [u0map; pmap], tspan; rng, save_positions = (false, false)) Xsamp = 0.0 Nsims = 4000 for n in 1:Nsims @@ -552,7 +552,7 @@ end # Works. @mtkcompile js = JumpSystem([j1, j2], t, [X], [p, d]) jprob = JumpProblem( - js, [X => 15], (0.0, 10.0), [p => 2.0, d => 0.5]; aggregator = Direct()) + js, [X => 15, p => 2.0, d => 0.5], (0.0, 10.0); aggregator = Direct()) sol = solve(jprob, SSAStepper()) @test eltype(sol[X]) === Int64 end @@ -566,7 +566,7 @@ end crj = ConstantRateJump(rate, affect) @named jsys = JumpSystem([crj, eq], t, [X], [a, b]) jsys = complete(jsys) - jprob = JumpProblem(jsys, [:X => 1.0], (0.0, 10.0), [:a => 1.0, :b => 0.5]) + jprob = JumpProblem(jsys, [:X => 1.0, :a => 1.0, :b => 0.5], (0.0, 10.0)) jprob2 = remake(jprob; u0 = [:X => 10.0]) @test jprob2[X] ≈ 10.0 end diff --git a/test/mass_matrix.jl b/test/mass_matrix.jl index 2e828cda64..82d1cf86a5 100644 --- a/test/mass_matrix.jl +++ b/test/mass_matrix.jl @@ -17,12 +17,10 @@ M = calculate_massmatrix(sys) 0 1 0 0 0 0] -prob_mm = ODEProblem(sys, [y => [1.0, 0.0, 0.0]], (0.0, 1e5), - [k => [0.04, 3e7, 1e4]]) +prob_mm = ODEProblem(sys, [y => [1.0, 0.0, 0.0], k => [0.04, 3e7, 1e4]], (0.0, 1e5)) @test prob_mm.f.mass_matrix isa Diagonal{Float64, Vector{Float64}} sol = solve(prob_mm, Rodas5(), reltol = 1e-8, abstol = 1e-8) -prob_mm = ODEProblem(sys, SA[y => [1.0, 0.0, 0.0]], (0.0, 1e5), - [k => [0.04, 3e7, 1e4]]) +prob_mm = ODEProblem(sys, SA[y => [1.0, 0.0, 0.0], k => [0.04, 3e7, 1e4]], (0.0, 1e5)) @test prob_mm.f.mass_matrix isa Diagonal{Float64, SVector{3, Float64}} function rober(du, u, p, t) @@ -57,8 +55,8 @@ eqs = [D(y[1]) ~ y[1], D(y[2]) ~ y[2], D(y[3]) ~ y[3]] @named sys = System(eqs, t, collect(y), [k]) @named sys = SDESystem(sys, [1, 1, 0]) sys = complete(sys) - prob = SDEProblem(sys, [y => [1.0, 0.0, 0.0]], (0.0, 1e5), [k => [0.04, 3e7, 1e4]]) + prob = SDEProblem(sys, [y => [1.0, 0.0, 0.0], k => [0.04, 3e7, 1e4]], (0.0, 1e5)) @test prob.f.mass_matrix isa Diagonal{Float64, Vector{Float64}} - prob = SDEProblem(sys, SA[y => [1.0, 0.0, 0.0]], (0.0, 1e5), [k => [0.04, 3e7, 1e4]]) + prob = SDEProblem(sys, SA[y => [1.0, 0.0, 0.0], k => [0.04, 3e7, 1e4]], (0.0, 1e5)) @test prob.f.mass_matrix isa Diagonal{Float64, SVector{3, Float64}} end diff --git a/test/modelingtoolkitize.jl b/test/modelingtoolkitize.jl index f6b4dc8a8d..3f2db92344 100644 --- a/test/modelingtoolkitize.jl +++ b/test/modelingtoolkitize.jl @@ -57,7 +57,8 @@ p = [1.0, 100.0] prob = OptimizationProblem(rosenbrock, x0, p) sys = complete(modelingtoolkitize(prob)) # symbolicitize me captain! -prob = OptimizationProblem(sys, x0, p, grad = true, hess = true) +prob = OptimizationProblem( + sys, [unknowns(sys) .=> x0; parameters(sys) .=> p], grad = true, hess = true) sol = solve(prob, NelderMead()) @test sol.objective < 1e-8 @@ -155,7 +156,7 @@ problem = ODEProblem(SIRD_ac!, ℬ, 𝒯, 𝒫) problem = ODEProblem(SIRD_ac!, ℬ, 𝒯, 𝒫) sys = complete(modelingtoolkitize(problem)) -fast_problem = ODEProblem(sys, ℬ, 𝒯, parameters(sys) .=> 𝒫) +fast_problem = ODEProblem(sys, [unknowns(sys) .=> ℬ; parameters(sys) .=> 𝒫], 𝒯) @time solution = solve(fast_problem, Tsit5(), saveat = 1:final_time) ## Issue #778 diff --git a/test/mtkparameters.jl b/test/mtkparameters.jl index afd97a5984..2f52dad30b 100644 --- a/test/mtkparameters.jl +++ b/test/mtkparameters.jl @@ -107,7 +107,7 @@ end @test getp(sys, g)(newps) isa Vector{Float32} @testset "Type-stability of `remake_buffer`" begin - prob = ODEProblem(sys, [], (0.0, 1.0), ivs) + prob = ODEProblem(sys, ivs, (0.0, 1.0)) idxs = (a, c, d, e, f, g, h) vals = (1.0, 2.0, 3, ones(3), ones(Int, 3, 3), ones(2), "a") @@ -144,7 +144,7 @@ eq = D(X) ~ p[1] - p[2] * X u0 = [X => 1.0] ps = [p => [2.0, 0.1]] -p = MTKParameters(osys, ps, u0) +p = MTKParameters(osys, [ps; u0]) @test p.tunable == [2.0, 0.1] # Ensure partial update promotes the buffer @@ -166,8 +166,8 @@ u0 = [X => 1.0] tspan = (0.0, 100.0) ps = [p => 1.0] # Value for `d` is missing -@test_throws ModelingToolkit.MissingParametersError ODEProblem(sys, u0, tspan, ps) -@test_nowarn ODEProblem(sys, u0, tspan, [ps..., d => 1.0]) +@test_throws ModelingToolkit.MissingParametersError ODEProblem(sys, [u0; ps], tspan) +@test_nowarn ODEProblem(sys, [u0; ps; [d => 1.0]], tspan) # JET tests @@ -250,7 +250,7 @@ eqs = [D(x) ~ (α - β * y) * x D(y) ~ (δ * x - γ) * y] @mtkcompile odesys = System(eqs, t) odeprob = ODEProblem( - odesys, [x => 1.0, y => 1.0], (0.0, 10.0), [α => 1.5, β => 1.0, γ => 3.0, δ => 1.0]) + odesys, [x => 1.0, y => 1.0, α => 1.5, β => 1.0, γ => 3.0, δ => 1.0], (0.0, 10.0)) tunables, _... = canonicalize(Tunable(), odeprob.p) @test tunables isa AbstractVector{Float64} @@ -328,7 +328,7 @@ end u0 = [V => [10.0, 20.0]] ps_vec = [k => [2.0, 3.0, 4.0, 5.0]] ps_scal = [k[1] => 1.0, k[2] => 2.0, k[3] => 3.0, k[4] => 4.0] - oprob_scal_scal = ODEProblem(osys_scal, u0, 1.0, ps_scal) + oprob_scal_scal = ODEProblem(osys_scal, [u0; ps_scal], 1.0) newoprob = remake(oprob_scal_scal; p = ps_vec, build_initializeprob = false) @test newoprob.ps[k] == [2.0, 3.0, 4.0, 5.0] end diff --git a/test/nonlinearsystem.jl b/test/nonlinearsystem.jl index 8575fbc36a..bf05e317ce 100644 --- a/test/nonlinearsystem.jl +++ b/test/nonlinearsystem.jl @@ -80,16 +80,15 @@ sH = calculate_hessian(ns) @test getfield.(ModelingToolkit.hessian_sparsity(ns), :rowval) == getfield.(sparse.(sH), :rowval) -prob = NonlinearProblem(ns, ones(3), [σ => 1.0, ρ => 1.0, β => 1.0]) +prob = NonlinearProblem(ns, [x => 1.0, y => 1.0, z => 1.0, σ => 1.0, ρ => 1.0, β => 1.0]) @test prob.f.sys === ns sol = solve(prob, NewtonRaphson()) @test sol.u[1] ≈ sol.u[2] -prob = NonlinearProblem(ns, ones(3), [σ => 1.0, ρ => 1.0, β => 1.0], jac = true) +prob = NonlinearProblem( + ns, [x => 1.0, y => 1.0, z => 1.0, σ => 1.0, ρ => 1.0, β => 1.0], jac = true) @test_nowarn solve(prob, NewtonRaphson()) -@test_throws ArgumentError NonlinearProblem(ns, ones(4), [σ => 1.0, ρ => 1.0, β => 1.0]) - @variables u F s a eqs1 = [ 0 ~ σ * (y - x) * h + F, @@ -100,7 +99,7 @@ eqs1 = [ lorenz = name -> System(eqs1, [x, y, z, u, F], [σ, ρ, β, h], name = name) lorenz1 = lorenz(:lorenz1) -@test_throws ArgumentError NonlinearProblem(complete(lorenz1), zeros(5), zeros(3)) +@test_throws ArgumentError NonlinearProblem(complete(lorenz1), zeros(4)) lorenz2 = lorenz(:lorenz2) @named connected = System( [s ~ a + lorenz1.x @@ -119,7 +118,7 @@ D = Differential(t) @named sys = System([D(subsys.x) ~ subsys.x + subsys.x], t, systems = [subsys]) sys = mtkcompile(sys) u0 = [subsys.x => 1, subsys.z => 2.0, subsys.y => 1.0] -prob = ODEProblem(sys, u0, (0, 1.0), [subsys.σ => 1, subsys.ρ => 2, subsys.β => 3]) +prob = ODEProblem(sys, [u0; [subsys.σ => 1, subsys.ρ => 2, subsys.β => 3]], (0, 1.0)) sol = solve(prob, FBDF(), reltol = 1e-7, abstol = 1e-7) @test sol[subsys.x] + sol[subsys.y] - sol[subsys.z]≈sol[subsys.u] atol=1e-7 @test_throws ArgumentError convert_system_indepvar(sys, t) @@ -133,7 +132,7 @@ eqs = [0 ~ σ * (y - x), 0 ~ x * y - β * z * h] @named ns = System(eqs, [x, y, z], [σ, ρ, β, h]) np = NonlinearProblem( - complete(ns), [0, 0, 0], [σ => 1, ρ => 2, β => 3], jac = true, sparse = true) + complete(ns), [x => 0, y => 0, z => 0, σ => 1, ρ => 2, β => 3], jac = true, sparse = true) @test calculate_jacobian(ns, sparse = true) isa SparseMatrixCSC # issue #819 @@ -275,12 +274,12 @@ sys = mtkcompile(ns; conservative = true) @test isequal(calculate_jacobian(ns), [(-1 - z + ρ)*σ -x*σ 2x*(-z + ρ) -β-(x^2)]) # solve without analytical jacobian - prob = NonlinearProblem(ns, guesses, ps) + prob = NonlinearProblem(ns, [guesses; ps]) sol = solve(prob, NewtonRaphson()) @test sol.retcode == ReturnCode.Success # solve with analytical jacobian - prob = NonlinearProblem(ns, guesses, ps, jac = true) + prob = NonlinearProblem(ns, [guesses; ps], jac = true) sol = solve(prob, NewtonRaphson()) @test sol.retcode == ReturnCode.Success @@ -327,7 +326,7 @@ end @test length(equations(sys)) == 1 @test length(unknowns(sys)) == 0 T = typeof(ForwardDiff.Dual(1.0)) - prob = NonlinearProblem(sys, [], [p => ForwardDiff.Dual(1.0)]; check_length = false) + prob = NonlinearProblem(sys, [p => ForwardDiff.Dual(1.0)]; check_length = false) @test prob.f(Float64[], prob.p) isa Vector{T} @test prob.f.resid_prototype isa Vector{T} @test_nowarn solve(prob) diff --git a/test/odesystem.jl b/test/odesystem.jl index 463f459df9..8d3619b0f9 100644 --- a/test/odesystem.jl +++ b/test/odesystem.jl @@ -217,15 +217,15 @@ push!(u0, y₂ => 0.0) push!(u0, y₃ => 0.0) p = [k₁ => 0.04, k₃ => 1e4] -p2 = (k₁ => 0.04, +p2 = [k₁ => 0.04, k₂ => 3e7, - k₃ => 1e4) + k₃ => 1e4] tspan = (0.0, 100000.0) -prob1 = ODEProblem(sys, u0, tspan, p) +prob1 = ODEProblem(sys, [u0; p], tspan) @test prob1.f.sys == sys -prob12 = ODEProblem(sys, u0, tspan, [k₁ => 0.04, k₂ => 3e7, k₃ => 1e4]) -prob13 = ODEProblem(sys, u0, tspan, (k₁ => 0.04, k₂ => 3e7, k₃ => 1e4)) -prob14 = ODEProblem(sys, u0, tspan, p2) +prob12 = ODEProblem(sys, [u0; [k₁ => 0.04, k₂ => 3e7, k₃ => 1e4]], tspan) +prob13 = ODEProblem(sys, [u0; [k₁ => 0.04, k₂ => 3e7, k₃ => 1e4]], tspan) +prob14 = ODEProblem(sys, [u0; p2], tspan) for p in [prob1, prob14] @test p.p isa MTKParameters p.ps[k₁] ≈ 0.04 @@ -280,12 +280,12 @@ sol_dpmap = solve(prob_dpmap, Rodas5()) end # test kwargs -prob2 = ODEProblem(sys, u0, tspan, p, jac = true) -prob3 = ODEProblem(sys, u0, tspan, p, jac = true, sparse = true) #SparseMatrixCSC need to handle +prob2 = ODEProblem(sys, [u0; p], tspan, jac = true) +prob3 = ODEProblem(sys, [u0; p], tspan, jac = true, sparse = true) #SparseMatrixCSC need to handle @test prob3.f.jac_prototype isa SparseMatrixCSC -prob3 = ODEProblem(sys, u0, tspan, p, jac = true, sparsity = true) +prob3 = ODEProblem(sys, [u0; p], tspan, jac = true, sparsity = true) @test prob3.f.sparsity isa SparseMatrixCSC -@test_throws ArgumentError ODEProblem(sys, zeros(5), tspan, p) +@test_throws ArgumentError ODEProblem(sys, zeros(5), tspan) for (prob, atol) in [(prob1, 1e-12), (prob2, 1e-12), (prob3, 1e-12)] local sol sol = solve(prob, Rodas5()) @@ -295,8 +295,8 @@ end du0 = [D(y₁) => -0.04 D(y₂) => 0.04 D(y₃) => 0.0] -prob4 = DAEProblem(sys, du0, u0, tspan, p2) -prob5 = eval(DAEProblem(sys, du0, u0, tspan, p2; expression = Val{true})) +prob4 = DAEProblem(sys, [du0; u0; p2], tspan) +prob5 = eval(DAEProblem(sys, [du0; u0; p2], tspan; expression = Val{true})) for prob in [prob4, prob5] local sol @test prob.differential_vars == [true, true, false] @@ -470,7 +470,7 @@ eqs = [D(x) ~ foo(x, ms); D(ms) ~ bar(ms, p)] @named emptysys = System(Equation[], t) @mtkcompile outersys = compose(emptysys, sys) prob = ODEProblem( - outersys, [sys.x => 1.0, sys.ms => 1:3], (0.0, 1.0), [sys.p => ones(3, 3)]) + outersys, [sys.x => 1.0, sys.ms => 1:3, sys.p => ones(3, 3)], (0.0, 1.0)) @test_nowarn solve(prob, Tsit5()) obsfn = ModelingToolkit.build_explicit_observed_function( outersys, bar(3outersys.sys.ms, 3outersys.sys.p)) @@ -576,18 +576,18 @@ let @named sys = System(eqs, t, vcat(x, [y]), [k], defaults = Dict(x .=> 0)) sys = mtkcompile(sys) - u0 = [0.5, 0] - du0 = 0 .* copy(u0) - prob = DAEProblem(sys, du0, u0, (0, 50)) - @test prob.u0 ≈ u0 - @test prob.du0 ≈ du0 + u0 = unknowns(sys) .=> [0.5, 0] + du0 = D.(unknowns(sys)) .=> 0.0 + prob = DAEProblem(sys, [du0; u0], (0, 50)) + @test prob.u0 ≈ [0.5, 0.0] + @test prob.du0 ≈ [0.0, 0.0] @test prob.p isa MTKParameters @test prob.ps[k] ≈ 1 sol = solve(prob, IDA()) @test sol[y] ≈ 0.9 * sol[x[1]] + sol[x[2]] @test isapprox(sol[x[1]][end], 1, atol = 1e-3) - prob = DAEProblem(sys, [D(y) => 0, D(x[1]) => 0, D(x[2]) => 0], Pair[x[1] => 0.5], + prob = DAEProblem(sys, [D(y) => 0, D(x[1]) => 0, D(x[2]) => 0, x[1] => 0.5], (0, 50)) @test prob.u0 ≈ [0.5, 0] @test prob.du0 ≈ [0, 0] @@ -596,8 +596,8 @@ let sol = solve(prob, IDA()) @test isapprox(sol[x[1]][end], 1, atol = 1e-3) - prob = DAEProblem(sys, [D(y) => 0, D(x[1]) => 0, D(x[2]) => 0], Pair[x[1] => 0.5], - (0, 50), [k => 2]) + prob = DAEProblem(sys, [D(y) => 0, D(x[1]) => 0, D(x[2]) => 0, x[1] => 0.5, k => 2], + (0, 50)) @test prob.u0 ≈ [0.5, 0] @test prob.du0 ≈ [0, 0] @test prob.p isa MTKParameters @@ -607,7 +607,7 @@ let # no initial conditions for D(x[1]) and D(x[2]) provided @test_throws ModelingToolkit.MissingVariablesError prob=DAEProblem( - sys, Pair[], Pair[], (0, 50)) + sys, Pair[], (0, 50)) prob = ODEProblem(sys, Pair[x[1] => 0], (0, 50)) sol = solve(prob, Rosenbrock23()) @@ -621,30 +621,12 @@ let eqs = [D(A) ~ -k1 * k2 * A] @named sys = System(eqs, t) sys = complete(sys) - u0map = [A => 1.0] - pmap = (k1 => 1.0, k2 => 1) + ivmap = [A => 1.0, k1 => 1.0, k2 => 1.0] tspan = (0.0, 1.0) - prob = ODEProblem(sys, u0map, tspan, pmap; tofloat = false) + prob = ODEProblem(sys, ivmap, tspan; tofloat = false) @test prob.p isa MTKParameters @test prob.ps[k1] ≈ 1.0 @test prob.ps[k2] == 1 && prob.ps[k2] isa Int - - prob = ODEProblem(sys, u0map, tspan, pmap) - @test vcat(prob.p...) isa Vector{Float64} - - pmap = [k1 => 1, k2 => 1] - tspan = (0.0, 1.0) - prob = ODEProblem(sys, u0map, tspan, pmap) - @test eltype(vcat(prob.p...)) === Float64 - - prob = ODEProblem(sys, u0map, tspan, pmap) - @test vcat(prob.p...) isa Vector{Float64} - - # 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) - # @test eltype(prob.p) === Union{Float64, Int} end let @@ -715,7 +697,7 @@ let eqs = [D(A) ~ -k * A] @named osys = System(eqs, t) osys = complete(osys) - oprob = ODEProblem(osys, [A => 1.0], (0.0, 10.0), [k => 1.0]; check_length = false) + oprob = ODEProblem(osys, [A => 1.0, k => 1.0], (0.0, 10.0); check_length = false) @test_nowarn sol = solve(oprob, Tsit5()) end @@ -876,14 +858,14 @@ prob = ODEProblem(sys, [x => 1.0], (0.0, 10.0)) @mtkcompile sys = System( eqs, t; continuous_events = [[norm(x) ~ 3.0] => [x ~ ones(3)]]) # array affect equations used to not work - prob1 = @test_nowarn ODEProblem(sys, [x => ones(3)], (0.0, 10.0), [p => ones(3, 3)]) + prob1 = @test_nowarn ODEProblem(sys, [x => ones(3), p => ones(3, 3)], (0.0, 10.0)) sol1 = @test_nowarn solve(prob1, Tsit5()) # array condition equations also used to not work @mtkcompile sys = System( eqs, t; continuous_events = [[x ~ sqrt(3) * ones(3)] => [x ~ ones(3)]]) # array affect equations used to not work - prob2 = @test_nowarn ODEProblem(sys, [x => ones(3)], (0.0, 10.0), [p => ones(3, 3)]) + prob2 = @test_nowarn ODEProblem(sys, [x => ones(3), p => ones(3, 3)], (0.0, 10.0)) sol2 = @test_nowarn solve(prob2, Tsit5()) @test sol1.u ≈ sol2.u[2:end] @@ -894,7 +876,7 @@ end @variables x(t)[1:3] y(t) @parameters p[1:3, 1:3] @test_nowarn @mtkcompile sys = System([D(x) ~ p * x, D(y) ~ x' * p * x], t) - @test_nowarn ODEProblem(sys, [x => ones(3), y => 2], (0.0, 10.0), [p => ones(3, 3)]) + @test_nowarn ODEProblem(sys, [x => ones(3), y => 2, p => ones(3, 3)], (0.0, 10.0)) end @parameters g L @@ -928,9 +910,9 @@ p = [σ => 28.0, ρ => 10.0, β => 8 / 3] -prob = SteadyStateProblem(sys, u0, p) +prob = SteadyStateProblem(sys, [u0; p]) @test prob isa SteadyStateProblem -prob = SteadyStateProblem(ODEProblem(sys, u0, (0.0, 10.0), p)) +prob = SteadyStateProblem(ODEProblem(sys, [u0; p], (0.0, 10.0))) @test prob isa SteadyStateProblem # Issue#2344 @@ -1048,7 +1030,7 @@ end sys = mtkcompile(System([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]) + prob = ODEProblem(sys, [x => P, sys.P => P], (0.0, 1.0)) return solve(prob, Tsit5())(1.0) end @@ -1095,7 +1077,7 @@ end initialization_eqs = [x ~ T] guesses = [x => 0.0] @named sys2 = System(eqs, T; initialization_eqs, guesses) - prob2 = ODEProblem(mtkcompile(sys2), [], (1.0, 2.0), []) + prob2 = ODEProblem(mtkcompile(sys2), [], (1.0, 2.0)) sol2 = solve(prob2) @test all(sol2[x] .== 1.0) end @@ -1128,7 +1110,7 @@ end defaults = [x => 1, z => y] @named sys = System(eqs, t; defaults) ssys = mtkcompile(sys) - prob = ODEProblem(ssys, [], (0.0, 1.0), []) + prob = ODEProblem(ssys, [], (0.0, 1.0)) @test prob[x] == prob[y] == prob[z] == 1.0 @parameters y0 @@ -1137,7 +1119,7 @@ end defaults = [y0 => 1, x => 1, z => y] @named sys = System(eqs, t; defaults) ssys = mtkcompile(sys) - prob = ODEProblem(ssys, [], (0.0, 1.0), []) + prob = ODEProblem(ssys, [], (0.0, 1.0)) @test prob[x] == prob[y] == prob[z] == 1.0 end @@ -1196,7 +1178,7 @@ end u0 = [sys.y => -1.0, sys.modela.x => -1.0] p = defaults(sys) - prob = ODEProblem(sys, u0, (0.0, 1.0), p) + prob = ODEProblem(sys, merge(p, Dict(u0)), (0.0, 1.0)) # evaluate u0_v = prob.u0 @@ -1336,7 +1318,7 @@ end @variables x(t) @parameters p[1:2] q @mtkcompile sys = System(D(x) ~ sum(p) * x + q * t, t) - prob = ODEProblem(sys, [x => 1.0], (0.0, 1.0), [p => ones(2), q => 2]) + prob = ODEProblem(sys, [x => 1.0, p => ones(2), q => 2], (0.0, 1.0)) obsfn = ModelingToolkit.build_explicit_observed_function( sys, [p..., q], return_inplace = true)[2] buf = zeros(3) @@ -1391,7 +1373,7 @@ end [c]; discrete_events = [SymbolicDiscreteCallback( 1.0 => [c ~ Pre(c) + 1], discrete_parameters = [c])]) - prob = ODEProblem(sys, [x => 0.0], (0.0, 2pi), [c => 1.0]) + prob = ODEProblem(sys, [x => 0.0, c => 1.0], (0.0, 2pi)) sol = solve(prob, Tsit5()) @test sol[obs] ≈ 1:7 end @@ -1400,7 +1382,7 @@ end @variables x(t)=1.0 y(t) [guess = 1.0] @parameters p[1:2] = [1.0, 2.0] @mtkcompile sys = System([D(x) ~ x, y^2 ~ x + sum(p)], t) - prob = DAEProblem(sys, [D(x) => x, D(y) => D(x) / 2y], [], (0.0, 1.0)) + prob = DAEProblem(sys, [D(x) => x, D(y) => D(x) / 2y], (0.0, 1.0)) sol = solve(prob, DFBDF(), abstol = 1e-8, reltol = 1e-8) @test sol[x]≈sol[y^2 - sum(p)] atol=1e-5 end @@ -1423,7 +1405,7 @@ end @mtkcompile sys = System([D(x) ~ p * x + q * t + sum(r), y^3 ~ 2x + 1], t; tstops = [0.5p, [0.1, 0.2], [p + 2q], r]) prob = DAEProblem( - sys, [D(y) => 2D(x) / 3y^2, D(x) => p * x + q * t + sum(r)], [], (0.0, 5.0)) + sys, [D(y) => 2D(x) / 3y^2, D(x) => p * x + q * t + sum(r)], (0.0, 5.0)) sol = solve(prob, DImplicitEuler()) expected_tstops = unique!(sort!(vcat(0.0:0.075:5.0, 0.1, 0.2, 0.65, 0.35, 0.45))) @test all(x -> any(isapprox(x, atol = 1e-6), sol.t), expected_tstops) @@ -1555,7 +1537,7 @@ end β => 8.0f0 / 3.0f0] tspan = (0.0f0, 100.0f0) - prob = ODEProblem{false}(sys, u0, tspan, p) + prob = ODEProblem{false}(sys, [u0; p], tspan) sol = solve(prob, Tsit5()) @test SciMLBase.successful_retcode(sol) end diff --git a/test/optimizationsystem.jl b/test/optimizationsystem.jl index b586d47d4e..d98cd11c69 100644 --- a/test/optimizationsystem.jl +++ b/test/optimizationsystem.jl @@ -28,8 +28,7 @@ using ModelingToolkit, SparseArrays, Test, Optimization, OptimizationOptimJL, generate_cost_hessian(combinedsys) hess_sparsity = ModelingToolkit.cost_hessian_sparsity(sys1) sparse_prob = OptimizationProblem(complete(sys1), - [x => 1, y => 1], - [a => 0.0, b => 0.0], + [x => 1, y => 1, a => 0.0, b => 0.0], grad = true, sparse = true) @test sparse_prob.f.hess_prototype.rowval == hess_sparsity.rowval @@ -46,7 +45,8 @@ using ModelingToolkit, SparseArrays, Test, Optimization, OptimizationOptimJL, sys2.b => 9.0 β => 10.0] - prob = OptimizationProblem(combinedsys, u0, p, grad = true, hess = true, cons_j = true, + prob = OptimizationProblem( + combinedsys, [u0; p], grad = true, hess = true, cons_j = true, cons_h = true) @test prob.f.sys === combinedsys sol = solve(prob, Ipopt.Optimizer(); print_level = 0) @@ -62,7 +62,7 @@ end ] @named sys = OptimizationSystem(loss, [x, y], [a, b], constraints = cons) sys = complete(sys) - prob = OptimizationProblem(sys, [x => 0.0, y => 0.0], [a => 1.0, b => 1.0], + prob = OptimizationProblem(sys, [x => 0.0, y => 0.0, a => 1.0, b => 1.0], grad = true, hess = true, cons_j = true, cons_h = true) @test prob.f.sys === sys sol = solve(prob, IPNewton()) @@ -70,7 +70,7 @@ end sol = solve(prob, Ipopt.Optimizer(); print_level = 0) @test sol.objective < 1.0 - prob = OptimizationProblem(sys, [x => 0.0, y => 0.0], [a => 1.0, b => 1.0], + prob = OptimizationProblem(sys, [x => 0.0, y => 0.0, a => 1.0, b => 1.0], grad = false, hess = false, cons_j = false, cons_h = false) sol = solve(prob, AmplNLWriter.Optimizer(Ipopt_jll.amplexe)) @test_skip sol.objective < 1.0 @@ -85,7 +85,7 @@ end z^2 + y^2 ≲ 1.0] @named sys = OptimizationSystem(loss, [x, y, z], [a, b], constraints = cons) sys = mtkcompile(sys) - prob = OptimizationProblem(sys, [x => 0.0, y => 0.0, z => 0.0], [a => 1.0, b => 1.0], + prob = OptimizationProblem(sys, [x => 0.0, y => 0.0, z => 0.0, a => 1.0, b => 1.0], grad = true, hess = true, cons_j = true, cons_h = true) sol = solve(prob, IPNewton()) @test sol.objective < 1.0 @@ -96,7 +96,7 @@ end @test sol.u≈[0.808, -0.064] atol=1e-3 @test sol[x]^2 + sol[y]^2 ≈ 1.0 - prob = OptimizationProblem(sys, [x => 0.0, y => 0.0, z => 0.0], [a => 1.0, b => 1.0], + prob = OptimizationProblem(sys, [x => 0.0, y => 0.0, z => 0.0, a => 1.0, b => 1.0], grad = false, hess = false, cons_j = false, cons_h = false) sol = solve(prob, AmplNLWriter.Optimizer(Ipopt_jll.amplexe)) @test_skip sol.objective < 1.0 @@ -208,7 +208,8 @@ end sys2.b => 9.0 β => 10.0] - prob = OptimizationProblem(combinedsys, u0, p, grad = true, hess = true, cons_j = true, + prob = OptimizationProblem( + combinedsys, [u0; p], grad = true, hess = true, cons_j = true, cons_h = true) @test prob.f.sys === combinedsys @test_broken SciMLBase.successful_retcode(solve(prob, @@ -235,7 +236,7 @@ end x[1]^2 + x[2]^2 ≲ 2.0 ]) - prob = OptimizationProblem(complete(sys), [x[1] => 2.0, x[2] => 0.0], [], grad = true, + prob = OptimizationProblem(complete(sys), [x[1] => 2.0, x[2] => 0.0], grad = true, hess = true, cons_j = true, cons_h = true) sol = Optimization.solve(prob, Ipopt.Optimizer(); print_level = 0) @test sol.u ≈ [1, 0] @@ -249,7 +250,7 @@ end @parameters a b loss = (a - x)^2 + b * (y - x^2)^2 @named sys = OptimizationSystem(loss, [x, y], [a, b, c]) - prob = OptimizationProblem(complete(sys), [x => 0.0, y => 0.0], [a => 1.0, b => 100.0]) + prob = OptimizationProblem(complete(sys), [x => 0.0, y => 0.0, a => 1.0, b => 100.0]) @test prob.lb == [-Inf, 0.0] @test prob.ub == [Inf, Inf] end @@ -267,11 +268,11 @@ end name = :sys1, constraints = cons)) - prob1 = OptimizationProblem(sys1, [x₁ => 0.0, x₂ => 0.0], [α₁ => 1.0, α₂ => 100.0], + prob1 = OptimizationProblem(sys1, [x₁ => 0.0, x₂ => 0.0, α₁ => 1.0, α₂ => 100.0], grad = true, hess = true, cons_j = true, cons_h = true) sys2 = complete(modelingtoolkitize(prob1)) - prob2 = OptimizationProblem(sys2, [x₁ => 0.0, x₂ => 0.0], [α₁ => 1.0, α₂ => 100.0], + prob2 = OptimizationProblem(sys2, [x₁ => 0.0, x₂ => 0.0, α₁ => 1.0, α₂ => 100.0], grad = true, hess = true, cons_j = true, cons_h = true) sol1 = Optimization.solve(prob1, Ipopt.Optimizer()) @@ -287,7 +288,7 @@ end @named sys = OptimizationSystem(loss, [x, y], [a, b], constraints = [x^2 + y^2 ≲ 0.0]) sys = complete(sys) - prob = OptimizationProblem(sys, [x => 0.0, y => 0.0], [a => 1.0, b => 100.0]) + prob = OptimizationProblem(sys, [x => 0.0, y => 0.0, a => 1.0, b => 100.0]) @test prob.f.expr isa Symbolics.Symbolic @test all(prob.f.cons_expr[i].lhs isa Symbolics.Symbolic for i in 1:length(prob.f.cons_expr)) @@ -300,7 +301,7 @@ end cons2 = [x^2 + y^2 ~ 0, y * sin(x) - x ~ 0] sys = complete(OptimizationSystem( loss, [x, y], [a, b], name = :sys2, constraints = cons2)) - prob = OptimizationProblem(sys, [x => 0.0, y => 0.0], [a => 1.0, b => 100.0], + prob = OptimizationProblem(sys, [x => 0.0, y => 0.0, a => 1.0, b => 100.0], grad = true, hess = true, cons_j = true, cons_h = true) G1 = Array{Float64}(undef, 2) @@ -354,7 +355,7 @@ end @parameters p f(::Real) @mtkcompile sys = OptimizationSystem( x^2 + f(x) * p, [x], [f, p]; constraints = [2.0 ≲ f(x) + p]) - prob = OptimizationProblem(sys, [x => 1.0], [p => 1.0, f => (x -> 2x)]) + prob = OptimizationProblem(sys, [x => 1.0, p => 1.0, f => (x -> 2x)]) @test abs(prob.f.cons(prob.u0, prob.p)[1]) ≈ 1.0 end diff --git a/test/parameter_dependencies.jl b/test/parameter_dependencies.jl index 97937b8811..0abfd2e63e 100644 --- a/test/parameter_dependencies.jl +++ b/test/parameter_dependencies.jl @@ -33,7 +33,7 @@ using NonlinearSolve @test prob.ps[p1] == 1.0 @test prob.ps[p2] == 2.0 @test SciMLBase.successful_retcode(solve(prob, Tsit5())) - prob = ODEProblem(sys, [x => 1.0], (0.0, 1.5), [p1 => 1.0], jac = true) + prob = ODEProblem(sys, [x => 1.0, p1 => 1.0], (0.0, 1.5), jac = true) @test prob.ps[p1] == 1.0 @test prob.ps[p2] == 2.0 integ = init(prob, Tsit5()) @@ -220,23 +220,26 @@ end @test_skip begin Tf = 1.0 - prob = ODEProblem(sys, [x => 0.0, y => 0.0], (0.0, Tf), - [kp => 1.0; z(k - 1) => 3.0; yd(k - 1) => 0.0; z(k - 2) => 4.0; - yd(k - 2) => 2.0]) + prob = ODEProblem(sys, + [x => 0.0, y => 0.0, kp => 1.0, z(k - 1) => 3.0, + yd(k - 1) => 0.0, z(k - 2) => 4.0, yd(k - 2) => 2.0], + (0.0, Tf)) @test_nowarn solve(prob, Tsit5()) @mtkcompile sys = System(eqs, t; parameter_dependencies = [kq => 2kp], discrete_events = [SymbolicDiscreteCallback( [0.5] => [kp ~ 2.0], discrete_parameters = [kp])]) - prob = ODEProblem(sys, [x => 0.0, y => 0.0], (0.0, Tf), - [kp => 1.0; z(k - 1) => 3.0; yd(k - 1) => 0.0; z(k - 2) => 4.0; - yd(k - 2) => 2.0]) + prob = ODEProblem(sys, + [x => 0.0, y => 0.0, kp => 1.0, z(k - 1) => 3.0, + yd(k - 1) => 0.0, z(k - 2) => 4.0, yd(k - 2) => 2.0], + (0.0, Tf)) @test prob.ps[kp] == 1.0 @test prob.ps[kq] == 2.0 @test_nowarn solve(prob, Tsit5()) - prob = ODEProblem(sys, [x => 0.0, y => 0.0], (0.0, Tf), - [kp => 1.0; z(k - 1) => 3.0; yd(k - 1) => 0.0; z(k - 2) => 4.0; - yd(k - 2) => 2.0]) + prob = ODEProblem(sys, + [x => 0.0, y => 0.0, kp => 1.0, z(k - 1) => 3.0, + yd(k - 1) => 0.0, z(k - 2) => 4.0, yd(k - 2) => 2.0], + (0.0, Tf)) integ = init(prob, Tsit5()) @test integ.ps[kp] == 1.0 @test integ.ps[kq] == 2.0 @@ -265,7 +268,7 @@ end @test ρ in Set(full_parameters(sdesys)) prob = SDEProblem( - sdesys, [x => 1.0, y => 0.0, z => 0.0], (0.0, 100.0), [σ => 10.0, β => 2.33]) + sdesys, [x => 1.0, y => 0.0, z => 0.0, σ => 10.0, β => 2.33], (0.0, 100.0)) @test prob.ps[ρ] == 2prob.ps[σ] @test_nowarn solve(prob, SRIW1()) @@ -275,7 +278,7 @@ end [10.0] => [σ ~ 15.0], discrete_parameters = [σ])]) sdesys = complete(sdesys) prob = SDEProblem( - sdesys, [x => 1.0, y => 0.0, z => 0.0], (0.0, 100.0), [σ => 10.0, β => 2.33]) + sdesys, [x => 1.0, y => 0.0, z => 0.0, σ => 10.0, β => 2.33], (0.0, 100.0)) integ = init(prob, SRIW1()) @test integ.ps[σ] == 10.0 @test integ.ps[ρ] == 20.0 @@ -303,7 +306,7 @@ end tspan = (0.0, 250.0) u₀map = [S => 999, I => 1, R => 0] parammap = [γ => 0.01] - jprob = JumpProblem(js2, u₀map, tspan, parammap; aggregator = Direct(), + jprob = JumpProblem(js2, [u₀map; parammap], tspan; aggregator = Direct(), save_positions = (false, false), rng = rng) @test jprob.ps[γ] == 0.01 @test jprob.ps[β] == 0.0001 @@ -314,7 +317,7 @@ end discrete_events = [SymbolicDiscreteCallback( [10.0] => [γ ~ 0.02], discrete_parameters = [γ])]) js2 = complete(js2) - jprob = JumpProblem(js2, u₀map, tspan, parammap; aggregator = Direct(), + jprob = JumpProblem(js2, [u₀map; parammap], tspan; aggregator = Direct(), save_positions = (false, false), rng = rng) integ = init(jprob, SSAStepper()) @test integ.ps[γ] == 0.01 @@ -335,7 +338,7 @@ end @test prob.ps[p1] == 1.0 @test prob.ps[p2] == 2.0 @test_nowarn solve(prob, NewtonRaphson()) - prob = NonlinearProblem(sys, [x => 1.0], [p1 => 2.0]) + prob = NonlinearProblem(sys, [x => 1.0, p1 => 2.0]) @test prob.ps[p1] == 2.0 @test prob.ps[p2] == 4.0 end @@ -355,7 +358,7 @@ end t; parameter_dependencies = [p2 => 2p1] ) - prob = ODEProblem(sys, [x => 1.0], (0.0, 1.5), [p1 => 1.0], jac = true) + prob = ODEProblem(sys, [x => 1.0, p1 => 1.0], (0.0, 1.5), jac = true) prob.ps[p1] = 3.0 @test prob.ps[p1] == 3.0 @test prob.ps[p2] == 6.0 diff --git a/test/reduction.jl b/test/reduction.jl index af9e510646..43fef0fb63 100644 --- a/test/reduction.jl +++ b/test/reduction.jl @@ -115,10 +115,10 @@ u0 = [lorenz1.x => 1.0 lorenz2.x => 1.0 lorenz2.y => 0.0 lorenz2.z => 0.0] -prob1 = ODEProblem(reduced_system, u0, (0.0, 100.0), pp) +prob1 = ODEProblem(reduced_system, [u0; pp], (0.0, 100.0)) solve(prob1, Rodas5()) -prob2 = SteadyStateProblem(reduced_system, u0, pp) +prob2 = SteadyStateProblem(reduced_system, [u0; pp]) @test prob2.f.observed(lorenz2.u, prob2.u0, prob2.p) === 1.0 # issue #724 and #716 @@ -160,7 +160,7 @@ reducedsys = mtkcompile(sys) u0 = [u2 => 1] pp = [2] -nlprob = NonlinearProblem(reducedsys, u0, [p => pp[1]]) +nlprob = NonlinearProblem(reducedsys, [u0; [p => pp[1]]]) reducedsol = solve(nlprob, NewtonRaphson()) residual = fill(100.0, length(unknowns(reducedsys))) nlprob.f(residual, reducedsol.u, pp) diff --git a/test/scc_nonlinear_problem.jl b/test/scc_nonlinear_problem.jl index 278b1a90d1..ded9852670 100644 --- a/test/scc_nonlinear_problem.jl +++ b/test/scc_nonlinear_problem.jl @@ -86,7 +86,7 @@ end @parameters p1[1:6] p2 eqs = 0 .~ collect(nlf(u, (u0, (p1, p2)))) @mtkcompile sys = System(eqs, [u], [p1, p2]) - sccprob = SCCNonlinearProblem(sys, [u => u0], [p1 => p[1], p2 => p[2][]]) + sccprob = SCCNonlinearProblem(sys, [u => u0, p1 => p[1], p2 => p[2][]]) sccsol = solve(sccprob, SimpleNewtonRaphson(); abstol = 1e-9) @test SciMLBase.successful_retcode(sccsol) @test norm(sccsol.resid) < norm(sol.resid) @@ -142,10 +142,10 @@ end subrules = Dict(Symbolics.unwrap(D(y[i])) => ((y[i] - u0[i]) / dt) for i in 1:8) eqs = substitute.(eqs, (subrules,)) @mtkcompile sys = System(eqs) - prob = NonlinearProblem(sys, [y => u0], [t => t0]) + prob = NonlinearProblem(sys, [y => u0, t => t0]) sol = solve(prob, NewtonRaphson(); abstol = 1e-12) - sccprob = SCCNonlinearProblem(sys, [y => u0], [t => t0]) + sccprob = SCCNonlinearProblem(sys, [y => u0, t => t0]) sccsol = solve(sccprob, NewtonRaphson(); abstol = 1e-12) @test sol.u≈sccsol.u atol=1e-10 @@ -266,7 +266,7 @@ end @parameters (f::Function)(..) @mtkcompile sys = System([ 0 ~ x[1]^2 - 9, x[2] ~ 2x[1], 0 ~ x[3]^2 - x[1]^2 + f(x)]) - prob = SCCNonlinearProblem(sys, [x => ones(3)], [f => sum]) + prob = SCCNonlinearProblem(sys, [x => ones(3), f => sum]) sol = solve(prob, NewtonRaphson()) @test SciMLBase.successful_retcode(sol) end @@ -286,7 +286,7 @@ end ρ => 10.0, β => 8 / 3] - sccprob = SCCNonlinearProblem(fullsys, u0, p) + sccprob = SCCNonlinearProblem(fullsys, [u0; p]) @test isequal(parameters(fullsys), parameters(sccprob.f.sys)) end @@ -295,6 +295,6 @@ end @parameters p[1:2] (f::Function)(..) @mtkcompile sys = System([x^2 - p[1]^2 ~ 0, y^2 ~ f(p)]) - prob = SCCNonlinearProblem(sys, [x => 1.0, y => 1.0], [p => ones(2), f => sum]) + prob = SCCNonlinearProblem(sys, [x => 1.0, y => 1.0, p => ones(2), f => sum]) @test_nowarn solve(prob, NewtonRaphson()) end diff --git a/test/sciml_problem_inputs.jl b/test/sciml_problem_inputs.jl index 249991b631..c02bd9dea1 100644 --- a/test/sciml_problem_inputs.jl +++ b/test/sciml_problem_inputs.jl @@ -103,7 +103,7 @@ end # Perform ODE simulations (singular and ensemble). let # Creates normal and ensemble problems. - base_oprob = ODEProblem(osys, u0_alts[1], tspan, p_alts[1]) + base_oprob = ODEProblem(osys, [u0_alts[1]; p_alts[1]], tspan) base_sol = solve(base_oprob, Tsit5(); saveat = 1.0) base_eprob = EnsembleProblem(base_oprob) base_esol = solve(base_eprob, Tsit5(); trajectories = 2, saveat = 1.0) @@ -120,7 +120,7 @@ end # Solves a nonlinear problem (EnsembleProblems are not possible for these). let - base_nlprob = NonlinearProblem(nsys, u0_alts[1], p_alts[1]) + base_nlprob = NonlinearProblem(nsys, [u0_alts[1]; p_alts[1]]) base_sol = solve(base_nlprob, NewtonRaphson()) # Solves problems for all input types, checking that identical solutions are found. for u0 in u0_alts, p in p_alts @@ -132,7 +132,7 @@ end # Perform steady state simulations (singular and ensemble). let # Creates normal and ensemble problems. - base_ssprob = SteadyStateProblem(osys, u0_alts[1], p_alts[1]) + base_ssprob = SteadyStateProblem(osys, [u0_alts[1]; p_alts[1]]) base_sol = solve(base_ssprob, DynamicSS(Tsit5())) base_eprob = EnsembleProblem(base_ssprob) base_esol = solve(base_eprob, DynamicSS(Tsit5()); trajectories = 2) diff --git a/test/sdesystem.jl b/test/sdesystem.jl index bcec551faf..d2a8f77636 100644 --- a/test/sdesystem.jl +++ b/test/sdesystem.jl @@ -27,11 +27,14 @@ f = eval(generate_diffusion_function(de)[1]) @test f(ones(3), rand(3), nothing) == 0.1ones(3) f = SDEFunction(de) -prob = SDEProblem(de, [1.0, 0.0, 0.0], (0.0, 100.0), [10.0, 26.0, 2.33]) +prob = SDEProblem( + de, [unknowns(de) .=> [1.0, 0.0, 0.0]; parameters(de) .=> [10.0, 26.0, 2.33]], + (0.0, 100.0)) sol = solve(prob, SRIW1(), seed = 1) -probexpr = SDEProblem(de, [1.0, 0.0, 0.0], (0.0, 100.0), - [10.0, 26.0, 2.33]) +probexpr = SDEProblem( + de, [unknowns(de) .=> [1.0, 0.0, 0.0]; parameters(de) .=> [10.0, 26.0, 2.33]], + (0.0, 100.0)) solexpr = solve(eval(probexpr), SRIW1(), seed = 1) @test all(x -> x == 0, Array(sol - solexpr)) @@ -54,8 +57,8 @@ f(du, [1, 2, 3.0], p, nothing) 0.1 0.01*2 0.02*1*3 0.2 0.3 0.01*3] -prob = SDEProblem(de, [1.0, 0.0, 0.0], (0.0, 100.0), (σ => 10.0, ρ => 26.0, β => 2.33), - noise_rate_prototype = zeros(3, 3)) +prob = SDEProblem(de, [x => 1.0, y => 0.0, z => 0.0, σ => 10.0, ρ => 26.0, β => 2.33], + (0.0, 100.0), noise_rate_prototype = zeros(3, 3)) sol = solve(prob, EM(), dt = 0.001) u0map = [ @@ -70,13 +73,13 @@ parammap = [ ρ => 2.33 ] -prob = SDEProblem(de, u0map, (0.0, 100.0), parammap) +prob = SDEProblem(de, [u0map; parammap], (0.0, 100.0)) @test prob.f.sys === de @test size(prob.noise_rate_prototype) == (3, 3) @test prob.noise_rate_prototype isa Matrix sol = solve(prob, EM(), dt = 0.001) -prob = SDEProblem(de, u0map, (0.0, 100.0), parammap, sparsenoise = true) +prob = SDEProblem(de, [u0map; parammap], (0.0, 100.0), sparsenoise = true) @test size(prob.noise_rate_prototype) == (3, 3) @test prob.noise_rate_prototype isa SparseMatrixCSC sol = solve(prob, EM(), dt = 0.001) @@ -506,14 +509,14 @@ noiseeqs = [0.1 * x] @named de = SDESystem(eqs, noiseeqs, tt, [x], [α, β], observed = [weight ~ x * 10]) de = complete(de) - prob = SDEProblem(de, u0map, (0.0, 1.0), parammap) + prob = SDEProblem(de, [u0map; parammap], (0.0, 1.0)) sol = solve(prob, EM(), dt = dt) @test observed(de) == [weight ~ x * 10] @test sol[weight] == 10 * sol[x] @named ode = System(eqs, tt, [x], [α, β], observed = [weight ~ x * 10]) ode = complete(ode) - odeprob = ODEProblem(ode, u0map, (0.0, 1.0), parammap) + odeprob = ODEProblem(ode, [u0map; parammap], (0.0, 1.0)) solode = solve(odeprob, Tsit5()) @test observed(ode) == [weight ~ x * 10] @test solode[weight] == 10 * solode[x] @@ -545,7 +548,7 @@ end β => 1.0 ] - prob = SDEProblem(de, u0map, (0.0, 1.0), parammap) + prob = SDEProblem(de, [u0map; parammap], (0.0, 1.0)) function prob_func(prob, i, repeat) remake(prob, seed = seeds[i]) @@ -567,7 +570,7 @@ end u = x demod = complete(ModelingToolkit.Girsanov_transform(de, u; θ0 = 0.1)) - probmod = SDEProblem(demod, u0map, (0.0, 1.0), parammap) + probmod = SDEProblem(demod, [u0map; parammap], (0.0, 1.0)) ensemble_probmod = EnsembleProblem(probmod; output_func = (sol, i) -> (g(sol[x, end]) * @@ -613,8 +616,8 @@ sys2 = complete(sys2) @set! sys2.parent = nothing @test sys1 == sys2 -prob = SDEProblem(sys1, sts .=> [1.0, 0.0, 0.0], - (0.0, 100.0), ps .=> (10.0, 26.0)) +prob = SDEProblem(sys1, [sts .=> [1.0, 0.0, 0.0]; ps .=> [10.0, 26.0]], + (0.0, 100.0)) solve(prob, LambaEulerHeun(), seed = 1) # Test ill-formed due to more equations than states in noise equations @@ -640,7 +643,7 @@ eqs = [D(x) ~ p - d * x + a * sqrt(p)] u0 = @SVector[x => 10.0] tspan = (0.0, 10.0) ps = @SVector[p => 5.0, d => 0.5] -sprob = SDEProblem(sys, u0, tspan, ps) +sprob = SDEProblem(sys, [u0; ps], tspan) @test !isinplace(sprob) @test !isinplace(sprob.f) @test_nowarn solve(sprob, ImplicitEM()) @@ -654,7 +657,7 @@ eqs = [D(x) ~ p - d * x + a * sqrt(p) u0 = @SVector[x => 10.0, y => 20.0] tspan = (0.0, 10.0) ps = @SVector[p => 5.0, d => 0.5] -sprob = SDEProblem(sys, u0, tspan, ps) +sprob = SDEProblem(sys, [u0; ps], tspan) @test sprob.f.g(sprob.u0, sprob.p, sprob.tspan[1]) isa SVector{2, Float64} @test_nowarn solve(sprob, ImplicitEM()) @@ -679,7 +682,7 @@ let β => 26.0, ρ => 2.33 ] - prob = SDEProblem(de, u0map, (0.0, 100.0), parammap) + prob = SDEProblem(de, [u0map; parammap], (0.0, 100.0)) # TODO: re-enable this when we support scalar noise @test solve(prob, SOSRI()).retcode == ReturnCode.Success end @@ -691,7 +694,7 @@ let # test to make sure that scalar noise always receive the same kicks D(y) ~ a] @mtkcompile de = System(eqs, t) - prob = SDEProblem(de, [x => 0, y => 0], (0.0, 10.0), []) + prob = SDEProblem(de, [x => 0, y => 0], (0.0, 10.0)) sol = solve(prob, SOSRI()) @test sol.u[end][1] == sol.u[end][2] end @@ -718,7 +721,7 @@ let # test that diagonal noise is correctly handled ρ => 2.33 ] - prob = SDEProblem(de, u0map, (0.0, 100.0), parammap) + prob = SDEProblem(de, [u0map; parammap], (0.0, 100.0)) # SOSRI only works for diagonal and scalar noise @test solve(prob, SOSRI()).retcode == ReturnCode.Success end @@ -744,7 +747,7 @@ end ρ => 2.33 ] - prob = SDEProblem(de, u0map, (0.0, 100.0), parammap) + prob = SDEProblem(de, [u0map; parammap], (0.0, 100.0)) # SOSRI only works for diagonal and scalar noise @test_throws ErrorException solve(prob, SOSRI()).retcode==ReturnCode.Success # ImplicitEM does work for non-diagonal noise @@ -773,7 +776,7 @@ end ρ => 2.33 ] - prob = SDEProblem(de, u0map, (0.0, 100.0), parammap) + prob = SDEProblem(de, [u0map; parammap], (0.0, 100.0)) @test solve(prob, SOSRI()).retcode == ReturnCode.Success end @@ -800,7 +803,7 @@ end sys = System(eqs, t, sts, ps, browns; name = :name) sys = mtkcompile(sys) @test ModelingToolkit.get_noise_eqs(sys) ≈ [1.0] - prob = SDEProblem(sys, [], (0.0, 1.0), []) + prob = SDEProblem(sys, [], (0.0, 1.0)) @test_nowarn solve(prob, RKMil()) end @@ -964,7 +967,7 @@ end parammap = [σ => 10.0, β => 26.0, ρ => 2.33] @test_throws ["Brownian", "mtkcompile"] SDEProblem( - de, u0map, (0.0, 100.0), parammap) + de, [u0map; parammap], (0.0, 100.0)) de = mtkcompile(de) - @test SDEProblem(de, u0map, (0.0, 100.0), parammap) isa SDEProblem + @test SDEProblem(de, [u0map; parammap], (0.0, 100.0)) isa SDEProblem end diff --git a/test/serialization.jl b/test/serialization.jl index 83e68f5770..41ea9c2c15 100644 --- a/test/serialization.jl +++ b/test/serialization.jl @@ -6,10 +6,8 @@ using ModelingToolkit: t_nounits as t, D_nounits as D @named sys = System([D(x) ~ -0.5 * x], t, defaults = Dict(x => 1.0)) sys = complete(sys) for prob in [ - eval(ModelingToolkit.ODEProblem{false}(sys, nothing, nothing, - SciMLBase.NullParameters())), - eval(ModelingToolkit.ODEProblem{false}(sys, nothing, nothing, - SciMLBase.NullParameters(); expression = Val{true})) + eval(ModelingToolkit.ODEProblem{false}(sys, nothing, nothing)), + eval(ModelingToolkit.ODEProblem{false}(sys, nothing, nothing; expression = Val{true})) ] _fn = tempname() diff --git a/test/split_parameters.jl b/test/split_parameters.jl index ac819351b5..f76ae4f01c 100644 --- a/test/split_parameters.jl +++ b/test/split_parameters.jl @@ -84,7 +84,7 @@ eqs = [y ~ src.output.u s = complete(sys) sys = mtkcompile(sys) prob = ODEProblem( - sys, [], (0.0, t_end), [s.src.interpolator => Interpolator(x, dt)]; + sys, [s.src.interpolator => Interpolator(x, dt)], (0.0, t_end); tofloat = false) sol = solve(prob, ImplicitEuler()); @test sol.retcode == ReturnCode.Success @@ -111,7 +111,7 @@ eqs = [D(y) ~ dy * a sys = mtkcompile(model; split = false) tspan = (0.0, t_end) -prob = ODEProblem(sys, [], tspan, []; build_initializeprob = false) +prob = ODEProblem(sys, [], tspan; build_initializeprob = false) @test prob.p isa Vector{Float64} sol = solve(prob, ImplicitEuler()); @@ -120,7 +120,7 @@ sol = solve(prob, ImplicitEuler()); # ------------------------ Mixed Type Conserved prob = ODEProblem( - sys, [], tspan, []; tofloat = false, build_initializeprob = false) + sys, [], tspan; tofloat = false, build_initializeprob = false) sol = solve(prob, ImplicitEuler()); @test sol.retcode == ReturnCode.Success @@ -248,7 +248,7 @@ end sol = solve(prob, Tsit5(); abstol = 1e-10, reltol = 1e-10) @test sol.u[end][] ≈ 2.0 - prob = ODEProblem(sys, [x => 1.0], (0.0, 1.0), [fn => Foo()]) + prob = ODEProblem(sys, [x => 1.0, fn => Foo()], (0.0, 1.0)) @inferred getter(prob) @inferred Vector{<:Real} prob.f(prob.u0, prob.p, prob.tspan[1]) sol = solve(prob; abstol = 1e-10, reltol = 1e-10) @@ -263,7 +263,7 @@ end @mtkcompile sys = System(D(x) ~ fn(x), t) @test is_parameter(sys, fn) getter = getp(sys, fn) - prob = ODEProblem(sys, [x => 1.0], (0.0, 1.0), [fn => interp]) + prob = ODEProblem(sys, [x => 1.0, fn => interp], (0.0, 1.0)) @inferred getter(prob) @inferred prob.f(prob.u0, prob.p, prob.tspan[1]) @test_nowarn sol = solve(prob, Tsit5()) diff --git a/test/state_selection.jl b/test/state_selection.jl index f3971bdbe3..97741110e2 100644 --- a/test/state_selection.jl +++ b/test/state_selection.jl @@ -122,9 +122,9 @@ let sys.supply_pipe.v => 0.1, sys.return_pipe.v => 0.1, D(supply_pipe.v) => 0.0, D(return_pipe.fluid_port_a.m) => 0.0, D(supply_pipe.fluid_port_a.m) => 0.0] - prob1 = ODEProblem(sys, [], (0.0, 10.0), [], guesses = u0) - prob2 = ODEProblem(sys, [], (0.0, 10.0), [], guesses = u0) - prob3 = DAEProblem(sys, D.(unknowns(sys)) .=> 0.0, [], (0.0, 10.0), guesses = u0) + prob1 = ODEProblem(sys, [], (0.0, 10.0), guesses = u0) + prob2 = ODEProblem(sys, [], (0.0, 10.0), guesses = u0) + prob3 = DAEProblem(sys, D.(unknowns(sys)) .=> 0.0, (0.0, 10.0), guesses = u0) @test solve(prob1, FBDF()).retcode == ReturnCode.Success #@test solve(prob2, FBDF()).retcode == ReturnCode.Success @test solve(prob3, DFBDF()).retcode == ReturnCode.Success @@ -274,6 +274,6 @@ let # solution ------------------------------------------------------------------- @named catapult = System(eqs, t, vars, params, defaults = defs) sys = mtkcompile(catapult) - prob = ODEProblem(sys, [], (0.0, 0.1), [l_2f => 0.55, damp => 1e7]; jac = true) + prob = ODEProblem(sys, [l_2f => 0.55, damp => 1e7], (0.0, 0.1); jac = true) @test solve(prob, Rodas4()).retcode == ReturnCode.Success end diff --git a/test/static_arrays.jl b/test/static_arrays.jl index edb6eeff7d..a23eeddde1 100644 --- a/test/static_arrays.jl +++ b/test/static_arrays.jl @@ -11,17 +11,16 @@ eqs = [D(D(x)) ~ σ * (y - x), @named sys = System(eqs, t) sys = mtkcompile(sys) -u0 = @SVector [D(x) => 2.0, +ivs = @SVector [D(x) => 2.0, x => 1.0, y => 0.0, - z => 0.0] - -p = @SVector [σ => 28.0, + z => 0.0, + σ => 28.0, ρ => 10.0, β => 8 / 3] tspan = (0.0, 100.0) -prob_mtk = ODEProblem(sys, u0, tspan, p) +prob_mtk = ODEProblem(sys, ivs, tspan) @test !SciMLBase.isinplace(prob_mtk) @test prob_mtk.u0 isa SArray diff --git a/test/steadystatesystems.jl b/test/steadystatesystems.jl index a99e84d9d2..505e7da890 100644 --- a/test/steadystatesystems.jl +++ b/test/steadystatesystems.jl @@ -14,10 +14,10 @@ for factor in [1e-1, 1e0, 1e10], u0 = [x => factor * u0_p[1]] p = [r => factor * u0_p[2]] - ss_prob = SteadyStateProblem(de, u0, p) + ss_prob = SteadyStateProblem(de, [u0; p]) sol = solve(ss_prob, SSRootfind()).u[1] @test abs(sol^2 - factor * u0_p[2]) < 1e-8 - ss_prob = SteadyStateProblem(de, u0, p) + ss_prob = SteadyStateProblem(de, [u0; p]) sol_expr = solve(ss_prob, SSRootfind()).u[1] @test all(x -> x == 0, sol - sol_expr) end diff --git a/test/structural_transformation/index_reduction.jl b/test/structural_transformation/index_reduction.jl index 6ab8dde338..94dd1f884b 100644 --- a/test/structural_transformation/index_reduction.jl +++ b/test/structural_transformation/index_reduction.jl @@ -53,16 +53,14 @@ let sys = mtkcompile(pendulum2) @test length(equations(sys)) == 5 @test length(unknowns(sys)) == 5 - u0 = [ + ivs = [ x => sqrt(2) / 2, - y => sqrt(2) / 2 - ] - p = [ + y => sqrt(2) / 2, L => 1.0, g => 9.8 ] - prob_auto = ODEProblem(sys, u0, (0.0, 0.5), p, guesses = [T => 0.0]) + prob_auto = ODEProblem(sys, ivs, (0.0, 0.5), guesses = [T => 0.0]) sol = solve(prob_auto, FBDF()) @test sol.retcode == ReturnCode.Success @test norm(sol[x] .^ 2 + sol[y] .^ 2 .- 1) < 1e-2 @@ -78,7 +76,7 @@ let @named pend = System(eqs, t) sys = complete(mtkcompile(pend; dummy_derivative = false)) prob = ODEProblem( - sys, [x => 1, y => 0, D(x) => 0.0], (0.0, 10.0), [g => 1], guesses = [λ => 0.0]) + sys, [x => 1, y => 0, D(x) => 0.0, g => 1], (0.0, 10.0), guesses = [λ => 0.0]) sol = solve(prob, Rodas5P()) @test SciMLBase.successful_retcode(sol) @test sol[x^2 + y^2][end] < 1.1 diff --git a/test/structural_transformation/tearing.jl b/test/structural_transformation/tearing.jl index e9b9909871..7f18cbb703 100644 --- a/test/structural_transformation/tearing.jl +++ b/test/structural_transformation/tearing.jl @@ -150,7 +150,7 @@ newdaesys = mtkcompile(daesys) @test equations(newdaesys) == [D(x) ~ h * z; 0 ~ y + sin(z) - p * t] @test equations(tearing_substitution(newdaesys)) == [D(x) ~ h * z; 0 ~ x + sin(z) - p * t] @test isequal(unknowns(newdaesys), [x, z]) -prob = ODEProblem(newdaesys, [x => 1.0, z => -0.5π], (0, 1.0), [p => 0.2]) +prob = ODEProblem(newdaesys, [x => 1.0, z => -0.5π, p => 0.2], (0, 1.0)) du = [0.0, 0.0]; u = [1.0, -0.5π]; pr = prob.p; @@ -161,7 +161,7 @@ prob.f(du, u, pr, tt) # test the initial guess is respected @named sys = System(eqs, t, defaults = Dict(z => NaN)) -infprob = ODEProblem(mtkcompile(sys), [x => 1.0], (0, 1.0), [p => 0.2]) +infprob = ODEProblem(mtkcompile(sys), [x => 1.0, p => 0.2], (0, 1.0)) infprob.f(du, infprob.u0, pr, tt) @test any(isnan, du) diff --git a/test/structural_transformation/utils.jl b/test/structural_transformation/utils.jl index 02298eb480..4d5dead59f 100644 --- a/test/structural_transformation/utils.jl +++ b/test/structural_transformation/utils.jl @@ -55,7 +55,7 @@ end @test length(observed(sys)) == 7 @test any(obs -> isequal(obs, y), observables(sys)) @test any(obs -> isequal(obs, z), observables(sys)) - prob = ODEProblem(sys, [x => 1.0], (0.0, 1.0), [foo => _tmp_fn]) + prob = ODEProblem(sys, [x => 1.0, foo => _tmp_fn], (0.0, 1.0)) @test_nowarn prob.f(prob.u0, prob.p, 0.0) isys = ModelingToolkit.generate_initializesystem(sys) @@ -78,7 +78,7 @@ end @mtkcompile sys = System([D(x) ~ y[1] + y[2], y ~ foo(x)], t) @test length(equations(sys)) == 1 @test length(observed(sys)) == 4 - prob = ODEProblem(sys, [x => 1.0], (0.0, 1.0), [foo => _tmp_fn2]) + prob = ODEProblem(sys, [x => 1.0, foo => _tmp_fn2], (0.0, 1.0)) val[] = 0 @test_nowarn prob.f(prob.u0, prob.p, 0.0) @test val[] == 1 @@ -99,7 +99,7 @@ end @test length(equations(sys)) == 5 @test length(observed(sys)) == 2 prob = ODEProblem( - sys, [y => ones(2), z => 2ones(2), x => 3.0], (0.0, 1.0), [foo => _tmp_fn2]) + sys, [y => ones(2), z => 2ones(2), x => 3.0, foo => _tmp_fn2], (0.0, 1.0)) val[] = 0 @test_nowarn prob.f(prob.u0, prob.p, 0.0) @test val[] == 2 @@ -397,7 +397,7 @@ end defaults = [p => missing], guesses = [p => 1.0, y => 1.0]) @test length(equations(sys)) == 2 @test length(parameters(sys)) == 2 - prob = ODEProblem(sys, [x => 1.0], (0.0, 1.0), [q => 2.0]) + prob = ODEProblem(sys, [x => 1.0, q => 2.0], (0.0, 1.0)) integ = init(prob, Rodas5P(); abstol = 1e-10, reltol = 1e-8) @test integ.ps[p]≈1.0 atol=1e-6 @test integ[y]≈0.0 atol=1e-5 @@ -413,7 +413,7 @@ end @test length(observed(sys)) == 1 @test observed(sys)[1].lhs in Set([x, y]) @test length(parameters(sys)) == 2 - prob = NonlinearProblem(sys, [x => 1.0, y => 1.0], [q => 1.0]) + prob = NonlinearProblem(sys, [x => 1.0, y => 1.0, q => 1.0]) integ = init(prob, NewtonRaphson()) @test prob.ps[p] ≈ 2.0 end @@ -431,13 +431,13 @@ end @test isempty(brownians(sys)) neqs = ModelingToolkit.get_noise_eqs(sys) @test issetequal(sum.(eachrow(neqs)), [q, 1 + p]) - prob = SDEProblem(sys, [x => 1.0, y => 1.0], (0.0, 1.0), [q => 1.0]) + prob = SDEProblem(sys, [x => 1.0, y => 1.0, q => 1.0], (0.0, 1.0)) integ = init(prob, ImplicitEM()) @test integ.ps[p] ≈ 3.0 end end -@testset "Deprecated `structural_simplify` and `@mtkbuild`" begin +@testset "Deprecated `mtkcompile` and `@mtkcompile`" begin @variables x(t) @test_deprecated @mtkbuild sys = System([D(x) ~ x], t) @named sys = System([D(x) ~ x], t) diff --git a/test/symbolic_events.jl b/test/symbolic_events.jl index 94a443fc4d..e61ea9779c 100644 --- a/test/symbolic_events.jl +++ b/test/symbolic_events.jl @@ -454,7 +454,7 @@ end function testsol( sys, probtype, solver, u0, p, tspan; tstops = Float64[], paramtotest = nothing, kwargs...) - prob = probtype(complete(sys), u0, tspan, p; kwargs...) + prob = probtype(complete(sys), [u0; p], tspan; kwargs...) sol = solve(prob, solver(); tstops = tstops, abstol = 1e-10, reltol = 1e-10) @test isapprox(sol(1.0000000001)[1] - sol(0.999999999)[1], 1.0; rtol = 1e-6) paramtotest === nothing || (@test sol.ps[paramtotest] == [0.0, 1.0]) @@ -521,7 +521,7 @@ end @named osys4 = System(eqs, t, [A], [k, t1], discrete_events = [cb1, cb2‵‵]) @named ssys4 = SDESystem(eqs, [0.0], t, [A], [k, t1], discrete_events = [cb1, cb2‵‵]) - oprob4 = ODEProblem(complete(osys4), u0, tspan, p) + oprob4 = ODEProblem(complete(osys4), [u0; p], tspan) testsol(osys4, ODEProblem, Tsit5, u0, p, tspan; tstops = [1.0], paramtotest = k) testsol(ssys4, SDEProblem, RI5, u0, p, tspan; tstops = [1.0], paramtotest = k) @@ -558,7 +558,7 @@ end function testsol(jsys, u0, p, tspan; tstops = Float64[], paramtotest = nothing, N = 40000, kwargs...) jsys = complete(jsys) - jprob = JumpProblem(jsys, u0, tspan, p; aggregator = Direct(), kwargs...) + jprob = JumpProblem(jsys, [u0; p], tspan; aggregator = Direct(), kwargs...) sol = solve(jprob, SSAStepper(); tstops = tstops) @test (sol(1.000000000001)[1] - sol(0.99999999999)[1]) == 1 paramtotest === nothing || (@test sol.ps[paramtotest] == [0.0, 1.0]) @@ -634,7 +634,7 @@ end @named oneosc_ce = compose(eqs_sys, oscce) oneosc_ce_simpl = mtkcompile(oneosc_ce) - prob = ODEProblem(oneosc_ce_simpl, [], (0.0, 2.0), []) + prob = ODEProblem(oneosc_ce_simpl, [], (0.0, 2.0)) sol = solve(prob, Tsit5(), saveat = 0.1) @test typeof(oneosc_ce_simpl) == System @@ -887,7 +887,7 @@ end @mtkcompile sys = System(D(x) ~ cos(t), t, [x], [a, b, c]; continuous_events = [cb1, cb2], discrete_events = [cb3]) - prob = ODEProblem(sys, [x => 1.0], (0.0, 2pi), [a => 1.0, b => 2.0, c => 0.0]) + prob = ODEProblem(sys, [x => 1.0, a => 1.0, b => 2.0, c => 0.0], (0.0, 2pi)) @test sort(canonicalize(Discrete(), prob.p)[1]) == [0.0, 1.0, 2.0] sol = solve(prob, Tsit5()) @@ -1071,7 +1071,7 @@ end cb1 = ModelingToolkit.SymbolicContinuousCallback( [x ~ 0], nothing, initialize = [x ~ 1.5], finalize = f) @mtkcompile sys = System(D(x) ~ -1, t, [x], []; continuous_events = [cb1]) - prob = ODEProblem(sys, [x => 1.0], (0.0, 2), []) + prob = ODEProblem(sys, [x => 1.0], (0.0, 2)) sol = solve(prob, Tsit5(); dtmax = 0.01) @test sol[x][1] ≈ 1.0 @test sol[x][2] ≈ 1.5 # the initialize affect has been applied @@ -1089,7 +1089,7 @@ end cb2 = ModelingToolkit.SymbolicContinuousCallback( [x ~ 0.1], nothing, initialize = a, finalize = b) @mtkcompile sys = System(D(x) ~ -1, t, [x], []; continuous_events = [cb1, cb2]) - prob = ODEProblem(sys, [x => 1.0], (0.0, 2), []) + prob = ODEProblem(sys, [x => 1.0], (0.0, 2)) sol = solve(prob, Tsit5()) @test sol[x][1] ≈ 1.0 @test sol[x][2] ≈ 1.5 # the initialize affect has been applied @@ -1103,7 +1103,7 @@ end cb3 = ModelingToolkit.SymbolicDiscreteCallback( 1.0, [x ~ 2], initialize = a, finalize = b) @mtkcompile sys = System(D(x) ~ -1, t, [x], []; discrete_events = [cb3]) - prob = ODEProblem(sys, [x => 1.0], (0.0, 2), []) + prob = ODEProblem(sys, [x => 1.0], (0.0, 2)) sol = solve(prob, Tsit5()) @test inited == true @test finaled == true @@ -1116,7 +1116,7 @@ end finaled = false cb3 = ModelingToolkit.SymbolicDiscreteCallback(1.0, f, initialize = a, finalize = b) @mtkcompile sys = System(D(x) ~ -1, t, [x], []; discrete_events = [cb3]) - prob = ODEProblem(sys, [x => 1.0], (0.0, 2), []) + prob = ODEProblem(sys, [x => 1.0], (0.0, 2)) sol = solve(prob, Tsit5()) @test seen == true @test inited == true @@ -1127,7 +1127,7 @@ end finaled = false cb3 = ModelingToolkit.SymbolicDiscreteCallback([1.0], f, initialize = a, finalize = b) @mtkcompile sys = System(D(x) ~ -1, t, [x], []; discrete_events = [cb3]) - prob = ODEProblem(sys, [x => 1.0], (0.0, 2), []) + prob = ODEProblem(sys, [x => 1.0], (0.0, 2)) sol = solve(prob, Tsit5()) @test seen == true @test inited == true @@ -1140,7 +1140,7 @@ end cb3 = ModelingToolkit.SymbolicDiscreteCallback( t == 1.0, f, initialize = a, finalize = b) @mtkcompile sys = System(D(x) ~ -1, t, [x], []; discrete_events = [cb3]) - prob = ODEProblem(sys, [x => 1.0], (0.0, 2), []) + prob = ODEProblem(sys, [x => 1.0], (0.0, 2)) sol = solve(prob, Tsit5(); tstops = 1.0) @test seen == true @test inited == true @@ -1183,7 +1183,7 @@ end end end @mtkcompile decay = DECAY() - prob = ODEProblem(decay, [], (0.0, 10.0), []) + prob = ODEProblem(decay, [], (0.0, 10.0)) @test_nowarn solve(prob, Tsit5(), tstops = [1.0]) end @@ -1253,7 +1253,7 @@ end x^2 + y^2 ~ 1] c_evt = [t ~ 5.0] => [x ~ Pre(x) + 0.1] @mtkcompile pend = System(eqs, t, continuous_events = c_evt) - prob = ODEProblem(pend, [x => -1, y => 0], (0.0, 10.0), [g => 1], guesses = [λ => 1]) + prob = ODEProblem(pend, [x => -1, y => 0, g => 1], (0.0, 10.0), guesses = [λ => 1]) sol = solve(prob, FBDF()) @test ≈(sol(5.000001, idxs = x) - sol(4.999999, idxs = x), 0.1, rtol = 1e-4) @test ≈(sol(5.000001, idxs = x)^2 + sol(5.000001, idxs = y)^2, 1, rtol = 1e-4) @@ -1261,7 +1261,7 @@ end # Implicit affect with Pre c_evt = [t ~ 5.0] => [x ~ Pre(x) + y^2] @mtkcompile pend = System(eqs, t, continuous_events = c_evt) - prob = ODEProblem(pend, [x => 1, y => 0], (0.0, 10.0), [g => 1], guesses = [λ => 1]) + prob = ODEProblem(pend, [x => 1, y => 0, g => 1], (0.0, 10.0), guesses = [λ => 1]) sol = solve(prob, FBDF()) @test ≈(sol(5.000001, idxs = y)^2 + sol(4.999999, idxs = x), sol(5.000001, idxs = x), rtol = 1e-4) @@ -1270,7 +1270,7 @@ end # Impossible affect errors c_evt = [t ~ 5.0] => [x ~ Pre(x) + 2] @mtkcompile pend = System(eqs, t, continuous_events = c_evt) - prob = ODEProblem(pend, [x => 1, y => 0], (0.0, 10.0), [g => 1], guesses = [λ => 1]) + prob = ODEProblem(pend, [x => 1, y => 0, g => 1], (0.0, 10.0), guesses = [λ => 1]) @test_throws UnsolvableCallbackError sol=solve(prob, FBDF()) # Changing both variables and parameters in the same affect. @@ -1281,7 +1281,7 @@ end c_evt = SymbolicContinuousCallback( [t ~ 5.0], [x ~ Pre(x) + 0.1, g ~ Pre(g) + 1], discrete_parameters = [g], iv = t) @mtkcompile pend = System(eqs, t, continuous_events = c_evt) - prob = ODEProblem(pend, [x => 1, y => 0], (0.0, 10.0), [g => 1], guesses = [λ => 1]) + prob = ODEProblem(pend, [x => 1, y => 0, g => 1], (0.0, 10.0), guesses = [λ => 1]) sol = solve(prob, FBDF()) @test sol.ps[g] ≈ [1, 2] @test ≈(sol(5.0000001, idxs = x) - sol(4.999999, idxs = x), 0.1, rtol = 1e-4) @@ -1291,7 +1291,7 @@ end c_evt = SymbolicContinuousCallback( [t ~ 5.0], [x ~ Pre(x) + 1, g ~ Pre(g) + 1], discrete_parameters = [g], iv = t) @mtkcompile sys = System(eqs, t, continuous_events = c_evt) - prob = ODEProblem(sys, [x => 1.0], (0.0, 10.0), [g => 2]) + prob = ODEProblem(sys, [x => 1.0, g => 2], (0.0, 10.0)) sol = solve(prob, FBDF()) @test sol.ps[g] ≈ [2.0, 3.0] @test ≈(sol(5.00000001, idxs = x) - sol(4.9999999, idxs = x), 1; rtol = 1e-4) @@ -1300,7 +1300,7 @@ end # Parameters that don't appear in affects should not be mutated. c_evt = [t ~ 5.0] => [x ~ Pre(x) + 1] @mtkcompile sys = System(eqs, t, continuous_events = c_evt) - prob = ODEProblem(sys, [x => 0.5], (0.0, 10.0), [g => 2], guesses = [y => 0]) + prob = ODEProblem(sys, [x => 0.5, g => 2], (0.0, 10.0), guesses = [y => 0]) sol = solve(prob, FBDF()) @test prob.ps[g] == sol.ps[g] end diff --git a/test/symbolic_indexing_interface.jl b/test/symbolic_indexing_interface.jl index f71fe93842..81d444668e 100644 --- a/test/symbolic_indexing_interface.jl +++ b/test/symbolic_indexing_interface.jl @@ -37,7 +37,7 @@ using SciMLStructures: Tunable @test default_values(odesys)[y] == 2.0 @test isequal(default_values(odesys)[xy], x + y) - prob = ODEProblem(odesys, [], (0.0, 1.0), [a => 1.0, b => 2.0]) + prob = ODEProblem(odesys, [a => 1.0, b => 2.0], (0.0, 1.0)) getter = getu(odesys, (x + 1, x + 2)) @test getter(prob) isa Tuple @test_nowarn @inferred getter(prob) @@ -122,7 +122,7 @@ end @test pobs(ps) == [3.0, 5.0] prob = NonlinearProblem( - ns, [x => 1.0, y => 2.0, z => 3.0], [σ => 1.0, ρ => 2.0, β => 3.0]) + ns, [x => 1.0, y => 2.0, z => 3.0, σ => 1.0, ρ => 2.0, β => 3.0]) getter = getu(ns, (x + 1, x + 2)) @test getter(prob) isa Tuple @test_nowarn @inferred getter(prob) @@ -197,7 +197,7 @@ end @parameters p1 p2[1:2, 1:2] @mtkcompile sys = System([D(x) ~ x * t + p1, y ~ 2x, D(z) ~ p2 * z], t) prob = ODEProblem( - sys, [x => 1.0, z => ones(2)], (0.0, 1.0), [p1 => 2.0, p2 => ones(2, 2)]) + sys, [x => 1.0, z => ones(2), p1 => 2.0, p2 => ones(2, 2)], (0.0, 1.0)) @test getu(prob, x)(prob) == getu(prob, :x)(prob) @test getu(prob, [x, y])(prob) == getu(prob, [:x, :y])(prob) @test getu(prob, z)(prob) == getu(prob, :z)(prob) diff --git a/test/symbolic_parameters.jl b/test/symbolic_parameters.jl index 15b19b320a..7a2fef3500 100644 --- a/test/symbolic_parameters.jl +++ b/test/symbolic_parameters.jl @@ -26,7 +26,7 @@ resolved = ModelingToolkit.varmap_to_vars(Dict(), parameters(ns), defaults = ModelingToolkit.defaults(ns)) @test resolved == [1, 0.1 + 1, (0.1 + 1) * 1.1] -prob = NonlinearProblem(complete(ns), [u => 1.0], Pair[]) +prob = NonlinearProblem(complete(ns), [u => 1.0]) @test prob.u0 == [1.0, 1.1, 0.9] sol = solve(prob, NewtonRaphson()) @@ -41,7 +41,7 @@ res = ModelingToolkit.varmap_to_vars(Dict(), parameters(top), @test res == [0.5, 1, 0.1 + 1, (0.1 + 1) * 1.1] top = complete(top) -prob = NonlinearProblem(top, [unknowns(ns, u) => 1.0, a => 1.0], []) +prob = NonlinearProblem(top, [unknowns(ns, u) => 1.0, a => 1.0]) @test prob.u0 == [1.0, 0.5, 1.1, 0.9] sol = solve(prob, NewtonRaphson()) @@ -61,12 +61,8 @@ der = Differential(t) eqs = [der(x) ~ x] @named sys = System(eqs, t, vars, [x0]) sys = complete(sys) -pars = [ - x0 => 10.0 -] -initialValues = [ - x => x0 -] +initialValues = [x => x0 + x0 => 10.0] tspan = (0.0, 1.0) -problem = ODEProblem(sys, initialValues, tspan, pars) +problem = ODEProblem(sys, initialValues, tspan) @test problem.u0 isa Vector{Float64} From c1e4dfd7fdc158550c0e435ac71504f494718996 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 23 May 2025 00:34:02 +0530 Subject: [PATCH 1827/2176] refactor: make `better_varmap_to_vars` more predictable --- src/systems/problem_utils.jl | 54 +++++++++++++++++++++--------------- 1 file changed, 31 insertions(+), 23 deletions(-) diff --git a/src/systems/problem_utils.jl b/src/systems/problem_utils.jl index 1814d50e83..c557beaf88 100644 --- a/src/systems/problem_utils.jl +++ b/src/systems/problem_utils.jl @@ -335,28 +335,39 @@ Return an array of values where the `i`th element corresponds to the value of `v in `varmap`. Does not perform symbolic substitution in the values of `varmap`. Keyword arguments: -- `tofloat`: Convert values to floating point numbers using `float`. -- `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`). 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`, - `promotetoconcrete` is set to `false`. -- `is_initializeprob, guesses`: Used to determine whether the system is missing guesses. +- `container_type`: The type of the returned container. +- `allow_symbolic`: Whether the returned container of values can have symbolic expressions. +- `buffer_eltype`: The `eltype` of the returned container if `!allow_symbolic`. If + `Nothing`, automatically promotes the values in the container to a common `eltype`. +- `tofloat`: Whether to promote values to floating point numbers if + `buffer_eltype == Nothing`. +- `use_union`: Whether to allow using a `Union` as the `eltype` if + `buffer_eltype == Nothing`. +- `toterm`: The `toterm` function for canonicalizing keys of `varmap`. A value of `nothing` + disables this process. +- `check`: Whether to check if all of `vars` are keys of `varmap`. +- `is_initializeprob`: Whether an initialization problem is being constructed. Used for + better error messages. """ function better_varmap_to_vars(varmap::AbstractDict, vars::Vector; - tofloat = true, container_type = Array, floatT = Nothing, - toterm = default_toterm, promotetoconcrete = nothing, check = true, - allow_symbolic = false, is_initializeprob = false) + tofloat = true, use_union = false, container_type = Array, buffer_eltype = Nothing, + toterm = default_toterm, check = true, allow_symbolic = false, + is_initializeprob = false) isempty(vars) && return nothing varmap = recursive_unwrap(varmap) - add_toterms!(varmap; toterm) + if toterm !== nothing + add_toterms!(varmap; toterm) + end if check missing_vars = missingvars(varmap, vars; toterm) - isempty(missing_vars) || throw(MissingVariablesError(missing_vars)) + if !isempty(missing_vars) + if is_initializeprob + throw(MissingGuessError(collect(missing_vars), collect(missing_vars))) + else + throw(MissingVariablesError(missing_vars)) + end + end end vals = map(x -> varmap[x], vars) if !allow_symbolic @@ -372,20 +383,17 @@ function better_varmap_to_vars(varmap::AbstractDict, vars::Vector; is_initializeprob ? throw(MissingGuessError(missingsyms, missingvals)) : throw(UnexpectedSymbolicValueInVarmap(missingsyms[1], missingvals[1])) end - if tofloat && !(floatT == Nothing) - vals = floatT.(vals) + if buffer_eltype == Nothing + vals = promote_to_concrete(vals; tofloat, use_union) + else + vals = buffer_eltype.(vals) end end - if container_type <: Union{AbstractDict, Tuple, Nothing, SciMLBase.NullParameters} + if container_type <: Union{AbstractDict, Nothing, SciMLBase.NullParameters} container_type = Array end - promotetoconcrete === nothing && (promotetoconcrete = container_type <: AbstractArray) - if promotetoconcrete && !allow_symbolic - vals = promote_to_concrete(vals; tofloat = tofloat, use_union = false) - end - if isempty(vals) return nothing elseif container_type <: Tuple From 8ef587c5ebe891ea7e03180309c5a389510f3672 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 23 May 2025 00:34:29 +0530 Subject: [PATCH 1828/2176] feat: add `u0_eltype` kwarg to problems, only use `tofloat` for parameter vector --- src/systems/problem_utils.jl | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/systems/problem_utils.jl b/src/systems/problem_utils.jl index c557beaf88..49fff2be51 100644 --- a/src/systems/problem_utils.jl +++ b/src/systems/problem_utils.jl @@ -1201,7 +1201,10 @@ 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. -- `tofloat`, `is_initializeprob`: Passed to [`better_varmap_to_vars`](@ref) for building `u0` (and possibly `p`). +- `tofloat`: Passed to [`better_varmap_to_vars`](@ref) when building the parameter vector of + a non-split system. +- `u0_eltype`: The `eltype` of the `u0` vector. If `nothing`, finds the promoted floating point + type from `op`. - `u0_constructor`: A function to apply to the `u0` value returned from `better_varmap_to_vars` to construct the final `u0` value. - `p_constructor`: A function to apply to each array buffer created when constructing the parameter object. @@ -1232,7 +1235,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, + check_initialization_units = false, u0_eltype = nothing, tofloat = true, u0_constructor = identity, p_constructor = identity, check_length = true, symbolic_u0 = false, warn_cyclic_dependency = false, circular_dependency_max_cycle_length = length(all_symbols(sys)), @@ -1275,6 +1278,8 @@ function process_SciMLProblem( floatT = float_type_from_varmap(op, floatT) end + u0_eltype = something(u0_eltype, floatT) + if !is_time_dependent(sys) || is_initializesystem(sys) add_observed_equations!(u0map, obs) end @@ -1324,7 +1329,7 @@ function process_SciMLProblem( evaluate_varmap!(op, dvs; limit = substitution_limit) u0 = better_varmap_to_vars( - op, dvs; tofloat, floatT, + op, dvs; buffer_eltype = u0_eltype, container_type = u0Type, allow_symbolic = symbolic_u0, is_initializeprob) if u0 !== nothing From 4f67240b7bdcd7550492bf46f03a655a43d27b3b Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 23 May 2025 00:34:47 +0530 Subject: [PATCH 1829/2176] fix: use `default_toterm` when building `du0` for implicit DAEs --- 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 49fff2be51..3875d20be0 100644 --- a/src/systems/problem_utils.jl +++ b/src/systems/problem_utils.jl @@ -1364,7 +1364,7 @@ function process_SciMLProblem( if implicit_dae ddvs = map(Differential(iv), dvs) - du0 = varmap_to_vars(op, ddvs; toterm = identity, + du0 = varmap_to_vars(op, ddvs; toterm = default_toterm, tofloat) kwargs = merge(kwargs, (; ddvs)) else From 20344fa386dc1ecb466b98b8b0086ba2038ecd77 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 23 May 2025 00:35:13 +0530 Subject: [PATCH 1830/2176] fix: propagate kwargs properly in `JumpProblem` --- src/problems/jumpproblem.jl | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/problems/jumpproblem.jl b/src/problems/jumpproblem.jl index 8f5f2e8499..28de46c35f 100644 --- a/src/problems/jumpproblem.jl +++ b/src/problems/jumpproblem.jl @@ -23,8 +23,8 @@ kwargs...) else _, u0, p = process_SciMLProblem(EmptySciMLFunction{iip}, sys, op; - t = tspan === nothing ? nothing : tspan[1], tofloat = false, - check_length = false, build_initializeprob = false) + t = tspan === nothing ? nothing : tspan[1], + check_length = false, build_initializeprob = false, kwargs...) observedfun = ObservedFunctionCache(sys; eval_expression, eval_module, checkbounds, cse) f = (du, u, p, t) -> (du .= 0; nothing) @@ -33,7 +33,7 @@ end else _f, u0, p = process_SciMLProblem(EmptySciMLFunction{iip}, sys, op; - t = tspan === nothing ? nothing : tspan[1], tofloat = false, check_length = false, build_initializeprob = false, cse) + t = tspan === nothing ? nothing : tspan[1], check_length = false, build_initializeprob = false, cse, kwargs...) f = DiffEqBase.DISCRETE_INPLACE_DEFAULT observedfun = ObservedFunctionCache( From 06fda920bfd54d2513914844491c1b684f6bceda Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 23 May 2025 00:37:36 +0530 Subject: [PATCH 1831/2176] test: fix DAEProblem initialization test --- test/initializationsystem.jl | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/test/initializationsystem.jl b/test/initializationsystem.jl index 9c9d2a2e7a..8d5abfc22e 100644 --- a/test/initializationsystem.jl +++ b/test/initializationsystem.jl @@ -1193,11 +1193,12 @@ end @variables x(t) [guess = 1.0] y(t) [guess = 1.0] @parameters p=missing [guess = 1.0] q=missing [guess = 1.0] @mtkcompile sys = System( - [D(x) ~ p * y + q * t, x^3 + y^3 ~ 5], t; initialization_eqs = [p^2 + q^3 ~ 3]) + [D(x) ~ p * y + q, x^3 + y^3 ~ 5], t; initialization_eqs = [p^2 + q^3 ~ 3]) # FIXME: solve for du0 prob = DAEProblem( - sys, [D(x) => cbrt(4), D(y) => -1 / cbrt(4), x => 1.0, p => 1.0], (0.0, 1.0)) + sys, [D(x) => cbrt(4) + cbrt(2), D(y) => -1 / cbrt(4), x => 1.0, p => 1.0], ( + 0.0, 1.0)) integ = init(prob, DImplicitEuler()) @test integ[x] ≈ 1.0 From a8d14e104d6924ac7e660180efdfcdf776bece22 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 23 May 2025 00:38:01 +0530 Subject: [PATCH 1832/2176] test: update error checking tests for improved error in `better_varmap_to_vars` --- test/implicit_discrete_system.jl | 2 +- test/initializationsystem.jl | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/test/implicit_discrete_system.jl b/test/implicit_discrete_system.jl index 59e4a94977..bbfb045b1f 100644 --- a/test/implicit_discrete_system.jl +++ b/test/implicit_discrete_system.jl @@ -28,7 +28,7 @@ rng = StableRNG(22525) @test prob.u0 == [1.0, 1.0] @variables x(t) @mtkcompile sys = System([x(k) ~ x(k) * x(k - 1) - 3], t) - @test_throws ModelingToolkit.MissingVariablesError prob=ImplicitDiscreteProblem( + @test_throws ModelingToolkit.MissingGuessError prob=ImplicitDiscreteProblem( sys, [], tspan) end diff --git a/test/initializationsystem.jl b/test/initializationsystem.jl index 8d5abfc22e..b178bf0d84 100644 --- a/test/initializationsystem.jl +++ b/test/initializationsystem.jl @@ -495,7 +495,7 @@ sys = extend(sysx, sysy) @variables x(t) y(t) @named sys = System([x^2 + y^2 ~ 25, D(x) ~ 1], t) ssys = mtkcompile(sys) - @test_throws ModelingToolkit.MissingVariablesError ODEProblem( + @test_throws ModelingToolkit.MissingGuessError ODEProblem( ssys, [x => 3], (0, 1)) # y should have a guess end From 1550787ca1447042ec309fa79ca29ca0aed93a9d Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 23 May 2025 00:38:13 +0530 Subject: [PATCH 1833/2176] test: use `u0_eltype` in jumpsystem tests --- test/jumpsystem.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/jumpsystem.jl b/test/jumpsystem.jl index 3eeac9bd5e..ca3d56359f 100644 --- a/test/jumpsystem.jl +++ b/test/jumpsystem.jl @@ -552,7 +552,7 @@ end # Works. @mtkcompile js = JumpSystem([j1, j2], t, [X], [p, d]) jprob = JumpProblem( - js, [X => 15, p => 2.0, d => 0.5], (0.0, 10.0); aggregator = Direct()) + js, [X => 15, p => 2.0, d => 0.5], (0.0, 10.0); aggregator = Direct(), u0_eltype = Int) sol = solve(jprob, SSAStepper()) @test eltype(sol[X]) === Int64 end From 800a50937bd2c3bf82ea9fc2782edb800230ea83 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 23 May 2025 00:38:23 +0530 Subject: [PATCH 1834/2176] test: mark appropriate optimization tests as broken --- test/optimizationsystem.jl | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/test/optimizationsystem.jl b/test/optimizationsystem.jl index d98cd11c69..0ff85a518d 100644 --- a/test/optimizationsystem.jl +++ b/test/optimizationsystem.jl @@ -98,7 +98,7 @@ end prob = OptimizationProblem(sys, [x => 0.0, y => 0.0, z => 0.0, a => 1.0, b => 1.0], grad = false, hess = false, cons_j = false, cons_h = false) - sol = solve(prob, AmplNLWriter.Optimizer(Ipopt_jll.amplexe)) + @test_broken sol = solve(prob, AmplNLWriter.Optimizer(Ipopt_jll.amplexe)) @test_skip sol.objective < 1.0 @test_skip sol.u≈[0.808, -0.064] atol=1e-3 @test_skip sol[x]^2 + sol[y]^2 ≈ 1.0 @@ -109,9 +109,11 @@ end x0 = zeros(2) p = [1.0, 100.0] f = OptimizationFunction(rosenbrock, Optimization.AutoSymbolics()) - prob = OptimizationProblem(f, x0, p) - sol = solve(prob, Newton()) - @test sol.u ≈ [1.0, 1.0] + @test_broken begin + prob = OptimizationProblem(f, x0, p) + sol = solve(prob, Newton()) + @test sol.u ≈ [1.0, 1.0] + end end # issue #819 From 868a07d21609e93349318525056af6eb65280785 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 23 May 2025 12:55:29 +0530 Subject: [PATCH 1835/2176] refactor: accept a single `op` in `generate_initializesystem` --- src/systems/nonlinear/initializesystem.jl | 30 +++++++++++++++++------ 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/src/systems/nonlinear/initializesystem.jl b/src/systems/nonlinear/initializesystem.jl index eaf2393e00..09e6f7c6ae 100644 --- a/src/systems/nonlinear/initializesystem.jl +++ b/src/systems/nonlinear/initializesystem.jl @@ -13,8 +13,7 @@ $(TYPEDSIGNATURES) Generate `System` of nonlinear equations which initializes a problem from specified initial conditions of an `AbstractTimeDependentSystem`. """ function generate_initializesystem_timevarying(sys::AbstractSystem; - u0map = Dict(), - pmap = Dict(), + op = Dict(), initialization_eqs = [], guesses = Dict(), default_dd_guess = Bool(0), @@ -40,8 +39,16 @@ function generate_initializesystem_timevarying(sys::AbstractSystem; idxs_diff = isdiffeq.(eqs) # PREPROCESSING - u0map = copy(anydict(u0map)) - pmap = anydict(pmap) + op = anydict(op) + u0map = anydict() + pmap = anydict() + build_operating_point!(sys, op, u0map, pmap, defs, unknowns(sys), + parameters(sys; initial_parameters = true)) + for (k, v) in op + if has_parameter_dependency_with_lhs(sys, k) && is_variable_floatingpoint(k) + pmap[k] = v + end + end initsys_preprocessing!(u0map, defs) # 1) Use algebraic equations of system as initialization constraints @@ -177,8 +184,7 @@ $(TYPEDSIGNATURES) Generate `System` of nonlinear equations which initializes a problem from specified initial conditions of an `AbstractTimeDependentSystem`. """ function generate_initializesystem_timeindependent(sys::AbstractSystem; - u0map = Dict(), - pmap = Dict(), + op = Dict(), initialization_eqs = [], guesses = Dict(), algebraic_only = false, @@ -196,8 +202,16 @@ function generate_initializesystem_timeindependent(sys::AbstractSystem; guesses = merge(get_guesses(sys), additional_guesses) # PREPROCESSING - u0map = copy(anydict(u0map)) - pmap = anydict(pmap) + op = anydict(op) + u0map = anydict() + pmap = anydict() + build_operating_point!(sys, op, u0map, pmap, defs, unknowns(sys), + parameters(sys; initial_parameters = true)) + for (k, v) in op + if has_parameter_dependency_with_lhs(sys, k) && is_variable_floatingpoint(k) + pmap[k] = v + end + end initsys_preprocessing!(u0map, defs) # Calculate valid `Initial` parameters. These are unknowns for From 205a76222774d3f133aac52d21be9cb0a6470db5 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 23 May 2025 12:56:05 +0530 Subject: [PATCH 1836/2176] refactor: accept a single `op` in `InitializationProblem` --- src/problems/initializationproblem.jl | 35 ++++++++++++++------------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/src/problems/initializationproblem.jl b/src/problems/initializationproblem.jl index e805e4d6aa..e957fa7652 100644 --- a/src/problems/initializationproblem.jl +++ b/src/problems/initializationproblem.jl @@ -2,8 +2,7 @@ struct InitializationProblem{iip, specialization} end """ ```julia -InitializationProblem{iip}(sys::AbstractSystem, t, u0map, - parammap = DiffEqBase.NullParameters(); +InitializationProblem{iip}(sys::AbstractSystem, t, op; version = nothing, tgrad = false, jac = false, checkbounds = false, sparse = false, @@ -20,8 +19,7 @@ initial conditions for the given DAE. """ @fallback_iip_specialize function InitializationProblem{iip, specialize}( sys::AbstractSystem, - t, u0map = [], - parammap = DiffEqBase.NullParameters(); + t, op = Dict(); guesses = [], check_length = true, warn_initialize_determined = true, @@ -37,18 +35,24 @@ initial conditions for the given DAE. if !iscomplete(sys) error("A completed system is required. Call `complete` or `mtkcompile` on the system before creating an `ODEProblem`") end - if isempty(u0map) && get_initializesystem(sys) !== nothing + has_u0_ics = false + op = copy(anydict(op)) + for k in keys(op) + has_u0_ics |= is_variable(sys, k) || isdifferential(k) || + symbolic_type(k) == ArraySymbolic() && + is_sized_array_symbolic(k) && is_variable(sys, first(collect(k))) + end + if !has_u0_ics && get_initializesystem(sys) !== nothing isys = get_initializesystem(sys; initialization_eqs, check_units) simplify_system = false - elseif isempty(u0map) && get_initializesystem(sys) === nothing + elseif !has_u0_ics && get_initializesystem(sys) === nothing isys = generate_initializesystem( - sys; initialization_eqs, check_units, pmap = parammap, - guesses, algebraic_only) + sys; initialization_eqs, check_units, op, guesses, algebraic_only) simplify_system = true else isys = generate_initializesystem( - sys; u0map, initialization_eqs, check_units, time_dependent_init, - pmap = parammap, guesses, algebraic_only) + sys; op, initialization_eqs, check_units, time_dependent_init, + guesses, algebraic_only) simplify_system = true end @@ -106,20 +110,17 @@ initial conditions for the given DAE. @warn "Initialization system is underdetermined. $neqs equations for $nunknown unknowns. Initialization will default to using least squares. $(scc_message)To suppress this warning pass warn_initialize_determined = false. To make this warning into an error, pass fully_determined = true" end - parammap = recursive_unwrap(anydict(parammap)) if t !== nothing - parammap[get_iv(sys)] = t + op[get_iv(sys)] = t end - filter!(kvp -> kvp[2] !== missing, parammap) + filter!(kvp -> kvp[2] !== missing, op) - u0map = to_varmap(u0map, unknowns(sys)) if isempty(guesses) guesses = Dict() end - filter_missing_values!(u0map) - filter_missing_values!(parammap) - op = merge(ModelingToolkit.guesses(sys), todict(guesses), u0map, parammap) + filter_missing_values!(op) + op = merge(ModelingToolkit.guesses(sys), todict(guesses), op) TProb = if neqs == nunknown && isempty(unassigned_vars) if use_scc && neqs > 0 From 1b0ce61d0ccf6afab1af15704b29aa703cf7c0d2 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 23 May 2025 12:56:33 +0530 Subject: [PATCH 1837/2176] refactor: update initialization code to account for new `op` syntax --- src/systems/nonlinear/initializesystem.jl | 12 ++++----- src/systems/problem_utils.jl | 30 ++++++++++------------- 2 files changed, 18 insertions(+), 24 deletions(-) diff --git a/src/systems/nonlinear/initializesystem.jl b/src/systems/nonlinear/initializesystem.jl index 09e6f7c6ae..ab09c71280 100644 --- a/src/systems/nonlinear/initializesystem.jl +++ b/src/systems/nonlinear/initializesystem.jl @@ -564,11 +564,11 @@ function SciMLBase.remake_initialization_data( defs = defaults(sys) use_scc = true initialization_eqs = Equation[] + op = anydict() if oldinitdata !== nothing && oldinitdata.metadata isa InitializationMetadata meta = oldinitdata.metadata - u0map = merge(meta.u0map, u0map) - pmap = merge(meta.pmap, pmap) + op = copy(meta.op) merge!(guesses, meta.guesses) use_scc = meta.use_scc initialization_eqs = meta.additional_initialization_eqs @@ -612,11 +612,9 @@ function SciMLBase.remake_initialization_data( if t0 === nothing && is_time_dependent(sys) t0 = 0.0 end - filter_missing_values!(u0map) - filter_missing_values!(pmap) + merge!(op, u0map, pmap) + filter_missing_values!(op) - merge!(u0map, pmap) - op = u0map u0map = anydict() pmap = anydict() missing_unknowns, missing_pars = build_operating_point!(sys, op, @@ -632,7 +630,7 @@ function SciMLBase.remake_initialization_data( typeof(newp.initials), floatT, Val(1), Val(length(vals)), vals...) end kws = maybe_build_initialization_problem( - sys, SciMLBase.isinplace(odefn), op, u0map, pmap, t0, defs, guesses, + sys, SciMLBase.isinplace(odefn), op, t0, defs, guesses, missing_unknowns; time_dependent_init, use_scc, initialization_eqs, floatT, u0_constructor, p_constructor, allow_incomplete = true) diff --git a/src/systems/problem_utils.jl b/src/systems/problem_utils.jl index 3875d20be0..42379a30c1 100644 --- a/src/systems/problem_utils.jl +++ b/src/systems/problem_utils.jl @@ -898,13 +898,9 @@ $(TYPEDFIELDS) """ struct InitializationMetadata{R <: ReconstructInitializeprob, GUU, SIU} """ - The `u0map` used to construct the initialization. + The operating point used to construct the initialization. """ - u0map::Dict{Any, Any} - """ - The `pmap` used to construct the initialization. - """ - pmap::Dict{Any, Any} + op::Dict{Any, Any} """ The `guesses` used to construct the initialization. """ @@ -1019,14 +1015,14 @@ end $(TYPEDSIGNATURES) Build and return the initialization problem and associated data as a `NamedTuple` to be passed -to the `SciMLFunction` constructor. Requires the system `sys`, operating point `op`, -user-provided `u0map` and `pmap`, initial time `t`, system defaults `defs`, user-provided -`guesses`, and list of unknowns which don't have a value in `op`. The keyword `implicit_dae` -denotes whether the `SciMLProblem` being constructed is in implicit DAE form (`DAEProblem`). -All other keyword arguments are forwarded to `InitializationProblem`. +to the `SciMLFunction` constructor. Requires the system `sys`, operating point `op`, initial +time `t`, system defaults `defs`, user-provided `guesses`, and list of unknowns which don't +have a value in `op`. The keyword `implicit_dae` denotes whether the `SciMLProblem` being +constructed is in implicit DAE form (`DAEProblem`). All other keyword arguments are forwarded +to `InitializationProblem`. """ function maybe_build_initialization_problem( - sys::AbstractSystem, iip, op::AbstractDict, u0map, pmap, t, defs, + sys::AbstractSystem, iip, op::AbstractDict, t, defs, guesses, missing_unknowns; implicit_dae = false, time_dependent_init = is_time_dependent(sys), u0_constructor = identity, p_constructor = identity, floatT = Float64, initialization_eqs = [], @@ -1038,7 +1034,7 @@ function maybe_build_initialization_problem( end initializeprob = ModelingToolkit.InitializationProblem{iip}( - sys, t, u0map, pmap; guesses, time_dependent_init, initialization_eqs, + sys, t, op; guesses, time_dependent_init, initialization_eqs, use_scc, u0_constructor, p_constructor, kwargs...) if state_values(initializeprob) !== nothing _u0 = state_values(initializeprob) @@ -1072,7 +1068,7 @@ function maybe_build_initialization_problem( nothing end meta = InitializationMetadata( - u0map, pmap, guesses, Vector{Equation}(initialization_eqs), + copy(op), copy(guesses), Vector{Equation}(initialization_eqs), use_scc, time_dependent_init, ReconstructInitializeprob( sys, initializeprob.f.sys; u0_constructor, p_constructor), @@ -1108,7 +1104,7 @@ function maybe_build_initialization_problem( end for p in punknowns - is_parameter_solvable(p, pmap, defs, guesses) || continue + is_parameter_solvable(p, op, defs, guesses) || continue get(op, p, missing) === missing || continue p = unwrap(p) op[p] = getu(initializeprob, p)(initializeprob) @@ -1281,7 +1277,7 @@ function process_SciMLProblem( u0_eltype = something(u0_eltype, floatT) if !is_time_dependent(sys) || is_initializesystem(sys) - add_observed_equations!(u0map, obs) + add_observed_equations!(op, obs) end if u0_constructor === identity && u0Type <: StaticArray u0_constructor = vals -> SymbolicUtils.Code.create_array( @@ -1295,7 +1291,7 @@ function process_SciMLProblem( if build_initializeprob kws = maybe_build_initialization_problem( sys, constructor <: SciMLBase.AbstractSciMLFunction{true}, - op, u0map, pmap, t, defs, guesses, missing_unknowns; + op, t, defs, guesses, missing_unknowns; implicit_dae, warn_initialize_determined, initialization_eqs, eval_expression, eval_module, fully_determined, warn_cyclic_dependency, check_units = check_initialization_units, From 4ce79ee74a7b680bb774941aa5ed1283d4d42fdf Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 23 May 2025 12:56:57 +0530 Subject: [PATCH 1838/2176] test: update tests to account for new `generate_initializesystem`/`InitializationProblem` --- test/initializationsystem.jl | 12 ++++++------ test/odesystem.jl | 2 +- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/test/initializationsystem.jl b/test/initializationsystem.jl index b178bf0d84..244e4ea4d3 100644 --- a/test/initializationsystem.jl +++ b/test/initializationsystem.jl @@ -13,7 +13,7 @@ eqs = [D(D(x)) ~ λ * x x^2 + y^2 ~ 1] @mtkcompile pend = System(eqs, t) -initprob = ModelingToolkit.InitializationProblem(pend, 0.0, [], [g => 1]; +initprob = ModelingToolkit.InitializationProblem(pend, 0.0, [g => 1]; guesses = [ModelingToolkit.missing_variable_defaults(pend); x => 1; y => 0.2]) conditions = getfield.(equations(initprob.f.sys), :rhs) @@ -23,11 +23,11 @@ sol = solve(initprob) @test maximum(abs.(sol[conditions])) < 1e-14 @test_throws ModelingToolkit.ExtraVariablesSystemException ModelingToolkit.InitializationProblem( - pend, 0.0, [], [g => 1]; + pend, 0.0, [g => 1]; guesses = [ModelingToolkit.missing_variable_defaults(pend); x => 1; y => 0.2], fully_determined = true) -initprob = ModelingToolkit.InitializationProblem(pend, 0.0, [x => 1, y => 0], [g => 1]; +initprob = ModelingToolkit.InitializationProblem(pend, 0.0, [x => 1, y => 0, g => 1]; guesses = ModelingToolkit.missing_variable_defaults(pend)) @test initprob isa NonlinearLeastSquaresProblem sol = solve(initprob) @@ -36,14 +36,14 @@ sol = solve(initprob) @test maximum(abs.(sol[conditions])) < 1e-14 initprob = ModelingToolkit.InitializationProblem( - pend, 0.0, [], [g => 1]; guesses = ModelingToolkit.missing_variable_defaults(pend)) + pend, 0.0, [g => 1]; guesses = ModelingToolkit.missing_variable_defaults(pend)) @test initprob isa NonlinearLeastSquaresProblem sol = solve(initprob) @test !SciMLBase.successful_retcode(sol) || sol.retcode == SciMLBase.ReturnCode.StalledSuccess @test_throws ModelingToolkit.ExtraVariablesSystemException ModelingToolkit.InitializationProblem( - pend, 0.0, [], [g => 1]; guesses = ModelingToolkit.missing_variable_defaults(pend), + pend, 0.0, [g => 1]; guesses = ModelingToolkit.missing_variable_defaults(pend), fully_determined = true) prob = ODEProblem(pend, [x => 1, y => 0, g => 1], (0.0, 1.5), @@ -477,7 +477,7 @@ eqs = [D(D(x)) ~ λ * x prob = ODEProblem(pend, [x => 1, g => 1], (0.0, 1.5), guesses = [λ => 0, y => 1], initialization_eqs = [y ~ 1]) -unsimp = generate_initializesystem(pend; u0map = [x => 1], initialization_eqs = [y ~ 1]) +unsimp = generate_initializesystem(pend; op = [x => 1], initialization_eqs = [y ~ 1]) sys = mtkcompile(unsimp; fully_determined = false) @test length(equations(sys)) in (3, 4) # could be either depending on tearing diff --git a/test/odesystem.jl b/test/odesystem.jl index 8d3619b0f9..c0e11dc718 100644 --- a/test/odesystem.jl +++ b/test/odesystem.jl @@ -889,7 +889,7 @@ eqs = [D(D(q₁)) ~ -λ * q₁, @named pend = System(eqs, t) @test_nowarn generate_initializesystem( - pend, u0map = [q₁ => 1.0, q₂ => 0.0], guesses = [λ => 1]) + pend; op = [q₁ => 1.0, q₂ => 0.0], guesses = [λ => 1]) # https://github.com/SciML/ModelingToolkit.jl/issues/2618 @parameters σ ρ β From 8aff22f77f1de4bb5aecf03995aec9833d49dc93 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 23 May 2025 15:05:11 +0530 Subject: [PATCH 1839/2176] refactor: use new `process_SciMLProblem` in optimal-control constructors --- ext/MTKCasADiDynamicOptExt.jl | 6 ++++-- ext/MTKInfiniteOptExt.jl | 12 ++++++++---- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/ext/MTKCasADiDynamicOptExt.jl b/ext/MTKCasADiDynamicOptExt.jl index a5400e3c8d..8f6b2c345e 100644 --- a/ext/MTKCasADiDynamicOptExt.jl +++ b/ext/MTKCasADiDynamicOptExt.jl @@ -76,8 +76,10 @@ function MTK.CasADiDynamicOptProblem(sys::System, u0map, tspan, pmap; steps = nothing, guesses = Dict(), kwargs...) MTK.warn_overdetermined(sys, u0map) - _u0map = has_alg_eqs(sys) ? u0map : merge(Dict(u0map), Dict(guesses)) - f, u0, p = MTK.process_SciMLProblem(ODEInputFunction, sys, _u0map, pmap; + _u0map = has_alg_eqs(sys) ? MTK.to_varmap(u0map, unknowns(sys)) : + merge(Dict(u0map), Dict(guesses)) + pmap = MTK.to_varmap(pmap, parameters(sys)) + f, u0, p = MTK.process_SciMLProblem(ODEInputFunction, sys, merge(_u0map, pmap); t = tspan !== nothing ? tspan[1] : tspan, output_type = MX, kwargs...) pmap = MTK.recursive_unwrap(MTK.AnyDict(pmap)) diff --git a/ext/MTKInfiniteOptExt.jl b/ext/MTKInfiniteOptExt.jl index f4fff61dff..40eb6f5264 100644 --- a/ext/MTKInfiniteOptExt.jl +++ b/ext/MTKInfiniteOptExt.jl @@ -59,8 +59,10 @@ function MTK.JuMPDynamicOptProblem(sys::System, u0map, tspan, pmap; steps = nothing, guesses = Dict(), kwargs...) MTK.warn_overdetermined(sys, u0map) - _u0map = has_alg_eqs(sys) ? u0map : merge(Dict(u0map), Dict(guesses)) - f, u0, p = MTK.process_SciMLProblem(ODEInputFunction, sys, _u0map, pmap; + _u0map = has_alg_eqs(sys) ? MTK.to_varmap(u0map, unknowns(sys)) : + merge(Dict(u0map), Dict(guesses)) + pmap = MTK.to_varmap(pmap, parameters(sys)) + f, u0, p = MTK.process_SciMLProblem(ODEInputFunction, sys, merge(_u0map, pmap); t = tspan !== nothing ? tspan[1] : tspan, kwargs...) pmap = MTK.recursive_unwrap(MTK.AnyDict(pmap)) @@ -86,8 +88,10 @@ function MTK.InfiniteOptDynamicOptProblem(sys::System, u0map, tspan, pmap; steps = nothing, guesses = Dict(), kwargs...) MTK.warn_overdetermined(sys, u0map) - _u0map = has_alg_eqs(sys) ? u0map : merge(Dict(u0map), Dict(guesses)) - f, u0, p = MTK.process_SciMLProblem(ODEInputFunction, sys, _u0map, pmap; + _u0map = has_alg_eqs(sys) ? MTK.to_varmap(u0map, unknowns(sys)) : + merge(Dict(u0map), Dict(guesses)) + pmap = MTK.to_varmap(pmap, parameters(sys)) + f, u0, p = MTK.process_SciMLProblem(ODEInputFunction, sys, merge(_u0map, pmap); t = tspan !== nothing ? tspan[1] : tspan, kwargs...) pmap = MTK.recursive_unwrap(MTK.AnyDict(pmap)) From 4d79ef4a73c66b41ddf4f7b8c8e229449c083cd0 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 23 May 2025 15:11:26 +0530 Subject: [PATCH 1840/2176] refactor: format --- src/deprecations.jl | 1 - 1 file changed, 1 deletion(-) diff --git a/src/deprecations.jl b/src/deprecations.jl index c8aee32253..73df50ecc4 100644 --- a/src/deprecations.jl +++ b/src/deprecations.jl @@ -8,4 +8,3 @@ macro mtkbuild(exprs...) @mtkcompile $(exprs...) end |> esc end - From e2eb777197448a1eac02ac23bea0ee1387bf8424 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 21 May 2025 23:05:31 +0530 Subject: [PATCH 1841/2176] refactor: remove deprecated items --- src/ModelingToolkit.jl | 33 +++++++++++---------------------- src/domains.jl | 17 ----------------- src/systems/abstractsystem.jl | 29 ----------------------------- src/systems/pde/pdesystem.jl | 4 +--- src/utils.jl | 2 -- 5 files changed, 12 insertions(+), 73 deletions(-) delete mode 100644 src/domains.jl diff --git a/src/ModelingToolkit.jl b/src/ModelingToolkit.jl index 63468917d1..4b0286f218 100644 --- a/src/ModelingToolkit.jl +++ b/src/ModelingToolkit.jl @@ -128,10 +128,6 @@ $(TYPEDEF) TODO """ abstract type AbstractSystem end -abstract type AbstractTimeDependentSystem <: AbstractSystem end -abstract type AbstractTimeIndependentSystem <: AbstractSystem end -abstract type AbstractMultivariateSystem <: AbstractSystem end -abstract type AbstractOptimizationSystem <: AbstractTimeIndependentSystem end function independent_variable end @@ -150,7 +146,6 @@ include("independent_variables.jl") include("constants.jl") include("utils.jl") -include("domains.jl") include("systems/index_cache.jl") include("systems/parameter_buffer.jl") @@ -265,25 +260,19 @@ PrecompileTools.@compile_workload begin end end -export AbstractTimeDependentSystem, - AbstractTimeIndependentSystem, - AbstractMultivariateSystem - -export ODEFunction, ODEFunctionExpr, ODEProblemExpr, convert_system_indepvar, +export ODEFunction, convert_system_indepvar, System, OptimizationSystem, JumpSystem, SDESystem, NonlinearSystem -export DAEFunctionExpr, DAEProblemExpr -export SDEFunction, SDEFunctionExpr, SDEProblemExpr +export SDEFunction export SystemStructure -export DiscreteProblem, DiscreteFunction, DiscreteFunctionExpr -export ImplicitDiscreteProblem, ImplicitDiscreteFunction, - ImplicitDiscreteFunctionExpr +export DiscreteProblem, DiscreteFunction +export ImplicitDiscreteProblem, ImplicitDiscreteFunction export ODEProblem, SDEProblem -export NonlinearFunction, NonlinearFunctionExpr -export NonlinearProblem, NonlinearProblemExpr -export IntervalNonlinearFunction, IntervalNonlinearFunctionExpr -export IntervalNonlinearProblem, IntervalNonlinearProblemExpr -export OptimizationProblem, OptimizationProblemExpr, constraints -export SteadyStateProblem, SteadyStateProblemExpr +export NonlinearFunction +export NonlinearProblem +export IntervalNonlinearFunction +export IntervalNonlinearProblem +export OptimizationProblem, constraints +export SteadyStateProblem export JumpProblem export alias_elimination, flatten export connect, domain_connect, @connector, Connection, AnalysisPoint, Flow, Stream, @@ -315,7 +304,7 @@ export calculate_jacobian, generate_jacobian, generate_rhs, generate_custom_func export calculate_control_jacobian, generate_control_jacobian export calculate_tgrad, generate_tgrad export generate_cost, calculate_cost_gradient, generate_cost_gradient -export calculate_factorized_W, generate_factorized_W +export calculate_factorized_W export calculate_cost_hessian, generate_cost_hessian export calculate_massmatrix, generate_diffusion_function export stochastic_integral_transform diff --git a/src/domains.jl b/src/domains.jl deleted file mode 100644 index 4972e44006..0000000000 --- a/src/domains.jl +++ /dev/null @@ -1,17 +0,0 @@ -import DomainSets: Interval, Ball, infimum, supremum - -@deprecate IntervalDomain(a, b) Interval(a, b) -@deprecate CircleDomain() Ball() - -# type piracy on Interval for downstream compatibility to be reverted once upgrade is complete -function Base.getproperty(domain::Interval, sym::Symbol) - if sym === :lower - @warn "domain.lower is deprecated, use infimum(domain) instead" - return infimum(domain) - elseif sym === :upper - @warn "domain.upper is deprecated, use supremum(domain) instead" - return supremum(domain) - else - return getfield(domain, sym) - end -end diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index a0ae5f685a..1582cddd4b 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -207,24 +207,6 @@ Substitutions(subs, deps) = Substitutions(subs, deps, nothing) Base.nameof(sys::AbstractSystem) = getfield(sys, :name) description(sys::AbstractSystem) = has_description(sys) ? get_description(sys) : "" -#Deprecated -function independent_variable(sys::AbstractSystem) - Base.depwarn( - "`independent_variable` is deprecated. Use `get_iv` or `independent_variables` instead.", - :independent_variable) - isdefined(sys, :iv) ? getfield(sys, :iv) : nothing -end - -function independent_variables(sys::AbstractTimeDependentSystem) - return [getfield(sys, :iv)] -end - -independent_variables(::AbstractTimeIndependentSystem) = [] - -function independent_variables(sys::AbstractMultivariateSystem) - return getfield(sys, :ivs) -end - """ $(TYPEDSIGNATURES) @@ -233,9 +215,6 @@ Get the independent variable(s) of the system `sys`. See also [`@independent_variables`](@ref) and [`ModelingToolkit.get_iv`](@ref). """ function independent_variables(sys::AbstractSystem) - if !(sys isa System) - @warn "Please declare ($(typeof(sys))) as a subtype of `AbstractTimeDependentSystem`, `AbstractTimeIndependentSystem` or `AbstractMultivariateSystem`." - end if isdefined(sys, :iv) && getfield(sys, :iv) !== nothing return [getfield(sys, :iv)] elseif isdefined(sys, :ivs) @@ -309,8 +288,6 @@ function SymbolicIndexingInterface.variable_index(sys::AbstractSystem, sym::Symb return nothing end -SymbolicIndexingInterface.variable_symbols(sys::AbstractMultivariateSystem) = sys.dvs - function SymbolicIndexingInterface.variable_symbols(sys::AbstractSystem) return solved_unknowns(sys) end @@ -561,9 +538,6 @@ function SymbolicIndexingInterface.default_values(sys::AbstractSystem) ) end -SymbolicIndexingInterface.is_time_dependent(::AbstractTimeDependentSystem) = true -SymbolicIndexingInterface.is_time_dependent(::AbstractTimeIndependentSystem) = false - SymbolicIndexingInterface.is_markovian(sys::AbstractSystem) = !is_dde(sys) SymbolicIndexingInterface.constant_structure(::AbstractSystem) = true @@ -1645,9 +1619,6 @@ function observables(sys::AbstractSystem) return map(eq -> eq.lhs, observed(sys)) end -Base.@deprecate default_u0(x) defaults(x) false -Base.@deprecate default_p(x) defaults(x) false - """ $(TYPEDSIGNATURES) diff --git a/src/systems/pde/pdesystem.jl b/src/systems/pde/pdesystem.jl index 96e6a6b276..d44a9cbd3f 100644 --- a/src/systems/pde/pdesystem.jl +++ b/src/systems/pde/pdesystem.jl @@ -34,7 +34,7 @@ domains = [t ∈ (0.0,1.0), @named pde_system = PDESystem(eq,bcs,domains,[t,x],[u]) ``` """ -struct PDESystem <: ModelingToolkit.AbstractMultivariateSystem +struct PDESystem <: AbstractSystem "The equations which define the PDE." eqs::Any "The boundary conditions." @@ -166,5 +166,3 @@ function Base.show(io::IO, ::MIME"text/plain", sys::PDESystem) print(io, "Default Parameter Values", get_defaults(sys)) return nothing end - -SymbolicIndexingInterface.is_time_dependent(::AbstractMultivariateSystem) = true diff --git a/src/utils.jl b/src/utils.jl index 4a183fd83f..bad438d797 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -60,8 +60,6 @@ macro showarr(x) end end -@deprecate substitute_expr!(expr, s) substitute(expr, s) - function todict(d) eltype(d) <: Pair || throw(ArgumentError("The variable-value mapping must be a Dict.")) d isa Dict ? d : Dict(d) From 5c3468c4b7d4b07c9e92b67f0c8cc99efe388c77 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 21 May 2025 23:53:17 +0530 Subject: [PATCH 1842/2176] refactor: remove `Substitutions` --- src/structural_transformation/StructuralTransformations.jl | 1 - src/structural_transformation/symbolics_tearing.jl | 1 - src/systems/abstractsystem.jl | 7 ------- 3 files changed, 9 deletions(-) diff --git a/src/structural_transformation/StructuralTransformations.jl b/src/structural_transformation/StructuralTransformations.jl index 0365d50a84..3cf08448d1 100644 --- a/src/structural_transformation/StructuralTransformations.jl +++ b/src/structural_transformation/StructuralTransformations.jl @@ -23,7 +23,6 @@ using ModelingToolkit: System, AbstractSystem, var_from_nested_derivative, Diffe ExtraVariablesSystemException, vars!, IncrementalCycleTracker, add_edge_checked!, topological_sort, - invalidate_cache!, Substitutions, get_or_construct_tearing_state, filter_kwargs, lower_varname_with_unit, lower_shift_varname_with_unit, setio, SparseMatrixCLIL, get_fullvars, has_equations, observed, diff --git a/src/structural_transformation/symbolics_tearing.jl b/src/structural_transformation/symbolics_tearing.jl index c3499abebc..a608b8b5ee 100644 --- a/src/structural_transformation/symbolics_tearing.jl +++ b/src/structural_transformation/symbolics_tearing.jl @@ -734,7 +734,6 @@ function update_simplified_system!( @set! sys.eqs = neweqs @set! sys.observed = obs - # @set! sys.substitutions = Substitutions(subeqs, deps) # Only makes sense for time-dependent if ModelingToolkit.has_schedule(sys) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index 1582cddd4b..51eb261b62 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -197,13 +197,6 @@ end const MTKPARAMETERS_ARG = Sym{Vector{Vector}}(:___mtkparameters___) -mutable struct Substitutions - subs::Vector{Equation} - deps::Vector{Vector{Int}} - subed_eqs::Union{Nothing, Vector{Equation}} -end -Substitutions(subs, deps) = Substitutions(subs, deps, nothing) - Base.nameof(sys::AbstractSystem) = getfield(sys, :name) description(sys::AbstractSystem) = has_description(sys) ? get_description(sys) : "" From 37ebc8880b09d4f55d399a7eddf9db9b4dd1422e Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 21 May 2025 23:53:38 +0530 Subject: [PATCH 1843/2176] docs: add docstrings for `has_observed_with_lhs`, `has_parameter_dependency_with_lhs` --- src/systems/abstractsystem.jl | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index 51eb261b62..fbd3757b18 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -386,7 +386,12 @@ function SymbolicIndexingInterface.parameter_observed(sys::AbstractSystem, sym) return build_explicit_observed_function(sys, sym; param_only = true) end -function has_observed_with_lhs(sys, sym) +""" + $(TYPEDSIGNATURES) + +Check if the system `sys` contains an observed equation with LHS `sym`. +""" +function has_observed_with_lhs(sys::AbstractSystem, sym) has_observed(sys) || return false if has_index_cache(sys) && (ic = get_index_cache(sys)) !== nothing return haskey(ic.observed_syms_to_timeseries, sym) @@ -395,6 +400,11 @@ function has_observed_with_lhs(sys, sym) end end +""" + $(TYPEDSIGNATURES) + +Check if the system `sys` contains a parameter dependency equation with LHS `sym`. +""" function has_parameter_dependency_with_lhs(sys, sym) has_parameter_dependencies(sys) || return false if has_index_cache(sys) && (ic = get_index_cache(sys)) !== nothing From 9d5529e3719cacb28cc5f527989d52f93e5ad4bc Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 21 May 2025 23:53:57 +0530 Subject: [PATCH 1844/2176] refactor: remove old field `get_` and `has_` functions --- src/systems/abstractsystem.jl | 18 ------------------ src/utils.jl | 3 --- 2 files changed, 21 deletions(-) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index fbd3757b18..74b5b30acd 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -833,7 +833,6 @@ end for prop in [:eqs :tag - :noiseeqs # TODO: remove :noise_eqs :iv :unknowns @@ -844,30 +843,17 @@ for prop in [:eqs :name :description :var_to_name - :ctrls :defaults :guesses :observed - :tgrad - :jac - :ctrl_jac - :Wfact - :Wfact_t :systems - :structure - :op :constraints - :constraintsystem - :controls - :loss :bcs :domain :ivs :dvs :connector_type - :connections :preface - :torn_matching :initializesystem :initialization_eqs :schedule @@ -875,17 +861,13 @@ for prop in [:eqs :metadata :gui_metadata :is_initializesystem - :discrete_subsystems :parameter_dependencies :assertions - :solved_unknowns - :split_idxs :ignored_connections :parent :is_dde :tstops :index_cache - :is_scalar_noise :isscheduled :costs :consolidate] diff --git a/src/utils.jl b/src/utils.jl index bad438d797..0d3c0b482c 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -561,9 +561,6 @@ function collect_scoped_vars!(unknowns, parameters, sys, iv; depth = 1, op = Dif collect_vars!(unknowns, parameters, eq, iv; depth, op) end end - if has_op(sys) - collect_vars!(unknowns, parameters, objective(sys), iv; depth, op) - end end """ From 101097e2e6df9a7c3539612cfa07909cb706f81b Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 21 May 2025 23:54:22 +0530 Subject: [PATCH 1845/2176] fix: remove jac/tgrad cache-related code --- src/systems/abstractsystem.jl | 21 --------------------- 1 file changed, 21 deletions(-) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index 74b5b30acd..b004b1059c 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -899,27 +899,6 @@ end has_equations(::AbstractSystem) = true -const EMPTY_TGRAD = Vector{Num}(undef, 0) -const EMPTY_JAC = Matrix{Num}(undef, 0, 0) -function invalidate_cache!(sys::AbstractSystem) - if has_tgrad(sys) - get_tgrad(sys)[] = EMPTY_TGRAD - end - if has_jac(sys) - get_jac(sys)[] = EMPTY_JAC - end - if has_ctrl_jac(sys) - get_ctrl_jac(sys)[] = EMPTY_JAC - end - if has_Wfact(sys) - get_Wfact(sys)[] = EMPTY_JAC - end - if has_Wfact_t(sys) - get_Wfact_t(sys)[] = EMPTY_JAC - end - return sys -end - function Setfield.get(obj::AbstractSystem, ::Setfield.PropertyLens{field}) where {field} getfield(obj, field) end From dd7175575f014eed9f390476bcf1549661718b55 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 21 May 2025 23:55:05 +0530 Subject: [PATCH 1846/2176] refactor: remove outdated functions refactor: remove `controls(sys)` refactor: remove `solved_unknowns(sys)` --- src/systems/abstractsystem.jl | 43 +---------------------------------- src/systems/index_cache.jl | 2 +- 2 files changed, 2 insertions(+), 43 deletions(-) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index b004b1059c..5d2d0ebc85 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -282,7 +282,7 @@ function SymbolicIndexingInterface.variable_index(sys::AbstractSystem, sym::Symb end function SymbolicIndexingInterface.variable_symbols(sys::AbstractSystem) - return solved_unknowns(sys) + return unknowns(sys) end function SymbolicIndexingInterface.is_parameter(sys::AbstractSystem, sym) @@ -1165,7 +1165,6 @@ end namespace_variables(sys::AbstractSystem) = unknowns(sys, unknowns(sys)) namespace_parameters(sys::AbstractSystem) = parameters(sys, parameters(sys)) -namespace_controls(sys::AbstractSystem) = controls(sys, controls(sys)) function namespace_defaults(sys) defs = defaults(sys) @@ -1547,12 +1546,6 @@ end # required in `src/connectors.jl:437` parameters(_) = [] -function controls(sys::AbstractSystem) - ctrls = get_ctrls(sys) - systems = get_systems(sys) - isempty(systems) ? ctrls : [ctrls; reduce(vcat, namespace_controls.(systems))] -end - """ $(TYPEDSIGNATURES) @@ -1773,19 +1766,6 @@ function isaffine(sys::AbstractSystem) all(isaffine(r, unknowns(sys)) for r in rhs) end -""" -$(SIGNATURES) - -Return a list of actual unknowns needed to be solved by solvers. -""" -function solved_unknowns(sys::AbstractSystem) - sts = unknowns(sys) - if has_solved_unknowns(sys) - sts = something(get_solved_unknowns(sys), sts) - end - return sts -end - ### ### System utils ### @@ -1978,18 +1958,6 @@ end Base.write(io::IO, sys::AbstractSystem) = write(io, readable_code(toexpr(sys))) -function get_or_construct_tearing_state(sys) - if has_tearing_state(sys) - state = get_tearing_state(sys) - if state === nothing - state = TearingState(sys) - end - else - state = nothing - end - state -end - """ n_expanded_connection_equations(sys::AbstractSystem) @@ -2125,15 +2093,6 @@ function Base.show( return nothing end -function Graphs.incidence_matrix(sys) - if has_torn_matching(sys) && has_tearing_state(sys) - state = get_tearing_state(sys) - incidence_matrix(state.structure.graph, Num(Sym{Real}(:×))) - else - return nothing - end -end - function split_assign(expr) if !(expr isa Expr && expr.head === :(=) && expr.args[2].head === :call) throw(ArgumentError("expression should be of the form `sys = foo(a, b)`")) diff --git a/src/systems/index_cache.jl b/src/systems/index_cache.jl index bdb69ae8c6..593de06838 100644 --- a/src/systems/index_cache.jl +++ b/src/systems/index_cache.jl @@ -64,7 +64,7 @@ struct IndexCache end function IndexCache(sys::AbstractSystem) - unks = solved_unknowns(sys) + unks = unknowns(sys) unk_idxs = UnknownIndexMap() symbol_to_variable = Dict{Symbol, SymbolicParam}() From 91822bc64b0bccfdf46d09d336ac4ee218c59e09 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 21 May 2025 23:55:34 +0530 Subject: [PATCH 1847/2176] refactor: remove `varmap_to_vars` and related outdated infrastructure --- src/variables.jl | 127 ----------------------------------------- test/initial_values.jl | 7 --- test/odesystem.jl | 8 --- 3 files changed, 142 deletions(-) diff --git a/src/variables.jl b/src/variables.jl index eb2e54a268..6317ebfa28 100644 --- a/src/variables.jl +++ b/src/variables.jl @@ -149,133 +149,6 @@ function default_toterm(x) end end -""" -$(SIGNATURES) - -Takes a list of pairs of `variables=>values` and an ordered list of variables -and creates the array of values in the correct order with default values when -applicable. -""" -function varmap_to_vars(varmap, varlist; defaults = Dict(), check = true, - toterm = default_toterm, promotetoconcrete = nothing, - tofloat = true, use_union = true) - varlist = collect(map(unwrap, varlist)) - - # Edge cases where one of the arguments is effectively empty. - is_incomplete_initialization = varmap isa DiffEqBase.NullParameters || - varmap === nothing - if is_incomplete_initialization || isempty(varmap) - if isempty(defaults) - if !is_incomplete_initialization && check - isempty(varlist) || throw(MissingVariablesError(varlist)) - end - return nothing - else - varmap = Dict() - end - end - - # We respect the input type if it's a static array - # otherwise canonicalize to a normal array - # container_type = T <: Union{Dict,Tuple} ? Array : T - if varmap isa StaticArray - container_type = typeof(varmap) - else - container_type = Array - end - - vals = if eltype(varmap) <: Pair # `varmap` is a dict or an array of pairs - varmap = todict(varmap) - _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, use_union) - end - - if isempty(vals) - return nothing - elseif container_type <: Tuple - (vals...,) - else - SymbolicUtils.Code.create_array(container_type, eltype(vals), Val{1}(), - Val(length(vals)), vals...) - end -end - -const MISSING_VARIABLES_MESSAGE = """ - Initial condition underdefined. Some are missing from the variable map. - Please provide a default (`u0`), initialization equation, or guess - for the following variables: - """ - -struct MissingVariablesError <: Exception - vars::Any -end - -function Base.showerror(io::IO, e::MissingVariablesError) - println(io, MISSING_VARIABLES_MESSAGE) - println(io, join(e.vars, ", ")) -end - -function _varmap_to_vars(varmap::Dict, varlist; defaults = Dict(), check = false, - toterm = Symbolics.diff2term, initialization_phase = false) - varmap = canonicalize_varmap(varmap; toterm) - defaults = canonicalize_varmap(defaults; toterm) - varmap = merge(defaults, varmap) - values = Dict() - - T = Union{} - for var in varlist - var = unwrap(var) - val = unwrap(fixpoint_sub(var, varmap; operator = Symbolics.Operator)) - if !isequal(val, var) - values[var] = val - end - end - missingvars = setdiff(varlist, collect(keys(values))) - check && (isempty(missingvars) || throw(MissingVariablesError(missingvars))) - return [values[unwrap(var)] for var in varlist] -end - -function varmap_with_toterm(varmap; toterm = Symbolics.diff2term) - return merge(todict(varmap), Dict(toterm(unwrap(k)) => v for (k, v) in varmap)) -end - -function canonicalize_varmap(varmap; toterm = Symbolics.diff2term) - new_varmap = Dict() - for (k, v) in varmap - k = unwrap(k) - v = unwrap(v) - new_varmap[k] = v - new_varmap[toterm(k)] = v - if Symbolics.isarraysymbolic(k) && Symbolics.shape(k) !== Symbolics.Unknown() - for i in eachindex(k) - new_varmap[k[i]] = v[i] - new_varmap[toterm(k[i])] = v[i] - end - end - end - return new_varmap -end - -@noinline function throw_missingvars(vars) - throw(ArgumentError("$vars are missing from the variable map.")) -end - -struct IsHistory end -ishistory(x::Num) = ishistory(unwrap(x)) -ishistory(x::Symbolic) = getmetadata(x, IsHistory, false) -hist(x, t) = wrap(hist(unwrap(x), t)) -function hist(x::Symbolic, t) - setmetadata( - toparam(maketerm(typeof(x), operation(x), [unwrap(t)], metadata(x))), - IsHistory, true) -end - ## Bounds ====================================================================== struct VariableBounds end Symbolics.option_to_metadata_type(::Val{:bounds}) = VariableBounds diff --git a/test/initial_values.jl b/test/initial_values.jl index ef6c404440..8bc052d6a7 100644 --- a/test/initial_values.jl +++ b/test/initial_values.jl @@ -66,13 +66,6 @@ ps = [k1 => 1.0, k2 => 5.0] # overdetermined because parameter initialization isn't in yet @test_warn "overdetermined" oprob=ODEProblem(osys_m, [u0; ps], tspan) -# Make sure it doesn't error on array variables with unspecified size -@parameters p::Vector{Real} q[1:3] -varmap = Dict(p => ones(3), q => 2ones(3)) -cvarmap = ModelingToolkit.canonicalize_varmap(varmap) -target_varmap = Dict(p => ones(3), q => 2ones(3), q[1] => 2.0, q[2] => 2.0, q[3] => 2.0) -@test cvarmap == target_varmap - # Initialization of ODEProblem with dummy derivatives of multidimensional arrays # Issue#1283 @variables z(t)[1:2, 1:2] diff --git a/test/odesystem.jl b/test/odesystem.jl index c0e11dc718..7e38805f2d 100644 --- a/test/odesystem.jl +++ b/test/odesystem.jl @@ -434,14 +434,6 @@ end @named sys = System([0 ~ sys1.y + sys2.y], t; systems = [sys1, sys2]) -# DelayDiffEq -using ModelingToolkit: hist -@variables x(t) y(t) -xₜ₋₁ = hist(x, t - 1) -eqs = [D(x) ~ x * y - D(y) ~ y * x - xₜ₋₁] -@named sys = System(eqs, t) - # register using StaticArrays using SymbolicUtils: term From a5080e171992d2dd7ea9b4e5f02985ee6ce8fcf3 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 21 May 2025 23:55:49 +0530 Subject: [PATCH 1848/2176] refactor: use `add_toterms` instead of `varmap_with_toterm` --- src/systems/abstractsystem.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index 5d2d0ebc85..fe0b47ddd1 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -2792,8 +2792,8 @@ ModelingToolkit.dump_unknowns(sys) See also: [`ModelingToolkit.dump_variable_metadata`](@ref), [`ModelingToolkit.dump_parameters`](@ref) """ function dump_unknowns(sys::AbstractSystem) - defs = varmap_with_toterm(defaults(sys)) - gs = varmap_with_toterm(guesses(sys)) + defs = add_toterms(defaults(sys)) + gs = add_toterms(guesses(sys)) map(dump_variable_metadata.(unknowns(sys))) do meta if haskey(defs, meta.var) meta = merge(meta, (; default = defs[meta.var])) From 75b5457ef6eea822f963ed4307b899bdffbae867 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 21 May 2025 23:56:52 +0530 Subject: [PATCH 1849/2176] feat: evaluate in `better_varmap_to_vars`, allow passing `use_union` --- src/systems/problem_utils.jl | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/src/systems/problem_utils.jl b/src/systems/problem_utils.jl index 42379a30c1..d0da0c5b6d 100644 --- a/src/systems/problem_utils.jl +++ b/src/systems/problem_utils.jl @@ -328,11 +328,26 @@ function Base.showerror(io::IO, err::MissingGuessError) 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 +const MISSING_VARIABLES_MESSAGE = """ + Initial condition underdefined. Some are missing from the variable map. + Please provide a default (`u0`), initialization equation, or guess + for the following variables: + """ + +struct MissingVariablesError <: Exception + vars::Any +end + +function Base.showerror(io::IO, e::MissingVariablesError) + println(io, MISSING_VARIABLES_MESSAGE) + println(io, join(e.vars, ", ")) +end + """ $(TYPEDSIGNATURES) Return an array of values where the `i`th element corresponds to the value of `vars[i]` -in `varmap`. Does not perform symbolic substitution in the values of `varmap`. +in `varmap`. Will mutate `varmap` by symbolically substituting it into itself. Keyword arguments: - `container_type`: The type of the returned container. @@ -348,11 +363,13 @@ Keyword arguments: - `check`: Whether to check if all of `vars` are keys of `varmap`. - `is_initializeprob`: Whether an initialization problem is being constructed. Used for better error messages. +- `substitution_limit`: The maximum number of times to recursively substitute `varmap` into + itself to get a numeric value for each variable in `vars`. """ function better_varmap_to_vars(varmap::AbstractDict, vars::Vector; tofloat = true, use_union = false, container_type = Array, buffer_eltype = Nothing, toterm = default_toterm, check = true, allow_symbolic = false, - is_initializeprob = false) + is_initializeprob = false, substitution_limit = 100) isempty(vars) && return nothing varmap = recursive_unwrap(varmap) @@ -369,6 +386,7 @@ function better_varmap_to_vars(varmap::AbstractDict, vars::Vector; end end end + evaluate_varmap!(varmap, vars; limit = substitution_limit) vals = map(x -> varmap[x], vars) if !allow_symbolic missingsyms = Any[] From 71361ddda6e76be9c806b98dd3093021071c97a5 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 21 May 2025 23:56:32 +0530 Subject: [PATCH 1850/2176] refactor: rename `better_varmap_to_vars` to `varmap_to_vars` --- 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 d0da0c5b6d..dba59e2645 100644 --- a/src/systems/problem_utils.jl +++ b/src/systems/problem_utils.jl @@ -366,7 +366,7 @@ Keyword arguments: - `substitution_limit`: The maximum number of times to recursively substitute `varmap` into itself to get a numeric value for each variable in `vars`. """ -function better_varmap_to_vars(varmap::AbstractDict, vars::Vector; +function varmap_to_vars(varmap::AbstractDict, vars::Vector; tofloat = true, use_union = false, container_type = Array, buffer_eltype = Nothing, toterm = default_toterm, check = true, allow_symbolic = false, is_initializeprob = false, substitution_limit = 100) From c07372dceeb5b15eb93165b6c77c0e00103318ed Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 21 May 2025 23:58:37 +0530 Subject: [PATCH 1851/2176] fix: use new `varmap_to_vars` --- ext/MTKBifurcationKitExt.jl | 11 ++++++----- src/systems/problem_utils.jl | 15 +++++++-------- test/discrete_system.jl | 2 +- test/extensions/test_infiniteopt.jl | 10 +++++----- test/odesystem.jl | 3 ++- test/symbolic_parameters.jl | 6 ++---- 6 files changed, 23 insertions(+), 24 deletions(-) diff --git a/ext/MTKBifurcationKitExt.jl b/ext/MTKBifurcationKitExt.jl index 870b523985..6a85fe66cd 100644 --- a/ext/MTKBifurcationKitExt.jl +++ b/ext/MTKBifurcationKitExt.jl @@ -113,11 +113,12 @@ function BifurcationKit.BifurcationProblem(nsys::System, J = jac ? ofun.jac : nothing # Converts the input state guess. - u0_bif_vals = ModelingToolkit.varmap_to_vars(u0_bif, - unknowns(nsys); - defaults = ModelingToolkit.get_defaults(nsys)) - p_vals = ModelingToolkit.varmap_to_vars( - ps, parameters(nsys); defaults = ModelingToolkit.get_defaults(nsys)) + u0_bif = ModelingToolkit.to_varmap(u0_bif, unknowns(nsys)) + u0_buf = merge(ModelingToolkit.get_defaults(nsys), u0_bif) + u0_bif_vals = ModelingToolkit.varmap_to_vars(u0_bif, unknowns(nsys)) + ps = ModelingToolkit.to_varmap(ps, parameters(nsys)) + ps = merge(ModelingToolkit.get_defaults(nsys), ps) + p_vals = ModelingToolkit.varmap_to_vars(ps, parameters(nsys)) # Computes bifurcation parameter and the plotting function. bif_idx = findfirst(isequal(bif_par), parameters(nsys)) diff --git a/src/systems/problem_utils.jl b/src/systems/problem_utils.jl index dba59e2645..4ba7035457 100644 --- a/src/systems/problem_utils.jl +++ b/src/systems/problem_utils.jl @@ -1215,11 +1215,11 @@ 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. -- `tofloat`: Passed to [`better_varmap_to_vars`](@ref) when building the parameter vector of +- `tofloat`: Passed to [`varmap_to_vars`](@ref) when building the parameter vector of a non-split system. - `u0_eltype`: The `eltype` of the `u0` vector. If `nothing`, finds the promoted floating point type from `op`. -- `u0_constructor`: A function to apply to the `u0` value returned from `better_varmap_to_vars` +- `u0_constructor`: A function to apply to the `u0` value returned from `varmap_to_vars` to construct the final `u0` value. - `p_constructor`: A function to apply to each array buffer created when constructing the parameter object. - `check_length`: Whether to check the number of equations along with number of unknowns and @@ -1340,11 +1340,10 @@ function process_SciMLProblem( @warn "Cycles in unknowns:\n$msg" end end - evaluate_varmap!(op, dvs; limit = substitution_limit) - u0 = better_varmap_to_vars( - op, dvs; buffer_eltype = u0_eltype, - container_type = u0Type, allow_symbolic = symbolic_u0, is_initializeprob) + u0 = varmap_to_vars( + op, dvs; buffer_eltype = u0_eltype, container_type = u0Type, + allow_symbolic = symbolic_u0, is_initializeprob, substitution_limit) if u0 !== nothing u0 = u0_constructor(u0) @@ -1365,7 +1364,7 @@ function process_SciMLProblem( @warn "Cycles in parameters:\n$msg" end end - evaluate_varmap!(op, ps; limit = substitution_limit) + if is_split(sys) # `pType` is usually `Dict` when the user passes key-value pairs. if !(pType <: AbstractArray) @@ -1373,7 +1372,7 @@ function process_SciMLProblem( end p = MTKParameters(sys, op; floatT = floatT, p_constructor) else - p = p_constructor(better_varmap_to_vars(op, ps; tofloat, container_type = pType)) + p = p_constructor(varmap_to_vars(op, ps; tofloat, container_type = pType)) end if implicit_dae diff --git a/test/discrete_system.jl b/test/discrete_system.jl index dc0281c8bf..7a33d89537 100644 --- a/test/discrete_system.jl +++ b/test/discrete_system.jl @@ -36,7 +36,7 @@ syss = mtkcompile(sys) df = DiscreteFunction(syss) # iip du = zeros(3) -u = ModelingToolkit.better_varmap_to_vars( +u = ModelingToolkit.varmap_to_vars( Dict([S(k - 1) => 1, I(k - 1) => 2, R(k - 1) => 3]), unknowns(syss)) p = MTKParameters(syss, [c, nsteps, δt, β, γ] .=> collect(1:5)) df.f(du, u, p, 0) diff --git a/test/extensions/test_infiniteopt.jl b/test/extensions/test_infiniteopt.jl index 6b371a79ae..c732ff5f81 100644 --- a/test/extensions/test_infiniteopt.jl +++ b/test/extensions/test_infiniteopt.jl @@ -35,9 +35,9 @@ permutation = [findfirst(isequal(x), expected_state_order) for x in dvs] # This ## -ub = varmap_to_vars([model.θ => 2pi, model.ω => 10], dvs) -lb = varmap_to_vars([model.θ => -2pi, model.ω => -10], dvs) -xf = varmap_to_vars([model.θ => pi, model.ω => 0], dvs) +ub = varmap_to_vars(Dict{Any, Any}([model.θ => 2pi, model.ω => 10]), dvs) +lb = varmap_to_vars(Dict{Any, Any}([model.θ => -2pi, model.ω => -10]), dvs) +xf = varmap_to_vars(Dict{Any, Any}([model.θ => pi, model.ω => 0]), dvs) nx = length(dvs) nu = length(inputs) ny = length(outputs) @@ -71,8 +71,8 @@ cp = f_obs(x, u, p, τ) # Test that it's possible to trace through an observed f @constraint(m, [i = 1:nx], x[i](0)==x0[i]) # Initial condition @constraint(m, [i = 1:nx], x[i](1)==xf[i]) # Terminal state -x_scale = varmap_to_vars([model.θ => 1 - model.ω => 1], dvs) +x_scale = varmap_to_vars(Dict{Any, Any}([model.θ => 1 + model.ω => 1]), dvs) # Add dynamics constraints @constraint(m, [i = 1:nx], (∂(x[i], τ) - tf * xp[i]) / x_scale[i]==0) diff --git a/test/odesystem.jl b/test/odesystem.jl index 7e38805f2d..6dc8b06eb9 100644 --- a/test/odesystem.jl +++ b/test/odesystem.jl @@ -649,7 +649,8 @@ end # 1561 let vars = @variables x y - arr = ModelingToolkit.varmap_to_vars([x => 0.0, y => [0.0, 1.0]], vars) #error + arr = ModelingToolkit.varmap_to_vars( + Dict([x => 0.0, y => [0.0, 1.0]]), vars; use_union = true) #error sol = Union{Float64, Vector{Float64}}[0.0, [0.0, 1.0]] @test arr == sol @test typeof(arr) == typeof(sol) diff --git a/test/symbolic_parameters.jl b/test/symbolic_parameters.jl index 7a2fef3500..5a01c3d645 100644 --- a/test/symbolic_parameters.jl +++ b/test/symbolic_parameters.jl @@ -22,8 +22,7 @@ u0 = [ ] ns = System(eqs, [x, y, z], [σ, ρ, β], name = :ns, defaults = [par; u0]) ModelingToolkit.get_defaults(ns)[y] = u * 1.1 -resolved = ModelingToolkit.varmap_to_vars(Dict(), parameters(ns), - defaults = ModelingToolkit.defaults(ns)) +resolved = ModelingToolkit.varmap_to_vars(defaults(ns), parameters(ns)) @test resolved == [1, 0.1 + 1, (0.1 + 1) * 1.1] prob = NonlinearProblem(complete(ns), [u => 1.0]) @@ -36,8 +35,7 @@ top = System([0 ~ -a + ns.x + b], [a], [b], systems = [ns], name = :top) ModelingToolkit.get_defaults(top)[b] = ns.σ * 0.5 ModelingToolkit.get_defaults(top)[ns.x] = unknowns(ns, u) * 0.5 -res = ModelingToolkit.varmap_to_vars(Dict(), parameters(top), - defaults = ModelingToolkit.defaults(top)) +res = ModelingToolkit.varmap_to_vars(defaults(top), parameters(top)) @test res == [0.5, 1, 0.1 + 1, (0.1 + 1) * 1.1] top = complete(top) From c5aa808ff162abf218fba6ee9ae7eec682732acd Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 21 May 2025 23:59:19 +0530 Subject: [PATCH 1852/2176] refactor: remove `get_u0_p`, modernize `get_u0` and add `get_p` --- src/systems/problem_utils.jl | 112 ++++++++++------------------------- 1 file changed, 31 insertions(+), 81 deletions(-) diff --git a/src/systems/problem_utils.jl b/src/systems/problem_utils.jl index 4ba7035457..3f73ea42cf 100644 --- a/src/systems/problem_utils.jl +++ b/src/systems/problem_utils.jl @@ -1698,97 +1698,47 @@ function maybe_codegen_scimlproblem(::Type{Val{false}}, T, args::NamedTuple; kwa remake(T(args...; kwargs...)) end -############## -# Legacy functions for backward compatibility -############## - """ - u0, p, defs = get_u0_p(sys, u0map, parammap; use_union=true, tofloat=true) + $(TYPEDSIGNATURES) -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. +Return the `u0` vector for the given system `sys` and variable-value mapping `varmap`. All +keyword arguments are forwarded to [`varmap_to_vars`](@ref). """ -function get_u0_p(sys, - u0map, - parammap = nothing; - t0 = nothing, - tofloat = true, - use_union = true, - symbolic_u0 = false) +function get_u0(sys::AbstractSystem, varmap; kwargs...) dvs = unknowns(sys) ps = parameters(sys; initial_parameters = true) + op = to_varmap(varmap, dvs) + add_observed!(sys, op) + add_parameter_dependencies!(sys, op) + missing_dvs, _ = build_operating_point!( + sys, op, Dict(), Dict(), defaults(sys), dvs, ps) - defs = defaults(sys) - if t0 !== nothing - defs[get_iv(sys)] = t0 - end - if parammap !== nothing - defs = mergedefaults(defs, parammap, ps) - end - if u0map isa Vector && eltype(u0map) <: Pair - u0map = Dict(u0map) - end - if u0map isa Dict - allobs = Set(observables(sys)) - if any(in(allobs), keys(u0map)) - u0s_in_obs = filter(in(allobs), keys(u0map)) - @warn "Observed variables cannot be assigned initial values. Initial values for $u0s_in_obs will be ignored." - end - end - obs = filter!(x -> !(x[1] isa Number), map(x -> x.rhs => x.lhs, observed(sys))) - observedmap = isempty(obs) ? Dict() : todict(obs) - defs = mergedefaults(defs, observedmap, u0map, dvs) - for (k, v) in defs - if Symbolics.isarraysymbolic(k) - ks = scalarize(k) - length(ks) == length(v) || error("$k has default value $v with unmatched size") - for (kk, vv) in zip(ks, v) - if !haskey(defs, kk) - defs[kk] = vv - end - end - end - end + isempty(missing_dvs) || throw(MissingVariablesError(collect(missing_dvs))) - 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, use_union) - end - 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 + return varmap_to_vars(op, dvs; kwargs...) end -function get_u0( - sys, u0map, parammap = nothing; symbolic_u0 = false, - toterm = default_toterm, t0 = nothing, use_union = true) +""" + $(TYPEDSIGNATURES) + +Return the `u0` vector for the given system `sys` and variable-value mapping `varmap`. All +keyword arguments are forwarded to [`MTKParameters`](@ref) for split systems and +[`varmap_to_vars`](@ref) for non-split systems. +""" +function get_p(sys::AbstractSystem, varmap; split = is_split(sys), kwargs...) dvs = unknowns(sys) - ps = parameters(sys) - defs = defaults(sys) - if t0 !== nothing - defs[get_iv(sys)] = t0 - end - if parammap !== nothing - defs = mergedefaults(defs, parammap, ps) - end - - # Convert observed equations "lhs ~ rhs" into defaults. - # Use the order "lhs => rhs" by default, but flip it to "rhs => lhs" - # if "lhs" is known by other means (parameter, another default, ...) - # TODO: Is there a better way to determine which equations to flip? - obs = map(x -> x.lhs => x.rhs, observed(sys)) - obs = map(x -> x[1] in keys(defs) ? reverse(x) : x, obs) - obs = filter!(x -> !(x[1] isa Number), obs) # exclude e.g. "0 => x^2 + y^2 - 25" - obsmap = isempty(obs) ? Dict() : todict(obs) - - defs = mergedefaults(defs, obsmap, u0map, dvs) - if symbolic_u0 - u0 = varmap_to_vars( - u0map, dvs; defaults = defs, tofloat = false, use_union = false, toterm) + ps = parameters(sys; initial_parameters = true) + op = to_varmap(varmap, dvs) + add_observed!(sys, op) + add_parameter_dependencies!(sys, op) + _, missing_ps = build_operating_point!( + sys, op, Dict(), Dict(), defaults(sys), dvs, ps) + + isempty(missing_ps) || throw(MissingParametersError(collect(missing_ps))) + + if split + MTKParameters(sys, op; kwargs...) else - u0 = varmap_to_vars(u0map, dvs; defaults = defs, tofloat = true, use_union, toterm) + varmap_to_vars(op, ps; kwargs...) end - t0 !== nothing && delete!(defs, get_iv(sys)) - return u0, defs end From b7bb11f715ef39d195b12978b51fd29695147a13 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 21 May 2025 23:59:37 +0530 Subject: [PATCH 1853/2176] test: use new `get_u0` --- test/downstream/test_disturbance_model.jl | 3 ++- test/extensions/test_infiniteopt.jl | 3 ++- test/initial_values.jl | 20 ++++++++++---------- 3 files changed, 14 insertions(+), 12 deletions(-) diff --git a/test/downstream/test_disturbance_model.jl b/test/downstream/test_disturbance_model.jl index 5f5672dc74..c7b9d833d0 100644 --- a/test/downstream/test_disturbance_model.jl +++ b/test/downstream/test_disturbance_model.jl @@ -164,7 +164,8 @@ measurement2 = ModelingToolkit.build_explicit_observed_function( disturbance_argument = true) op = ModelingToolkit.inputs(io_sys) .=> 0 -x0, p = ModelingToolkit.get_u0_p(io_sys, op, op) +x0 = ModelingToolkit.get_u0(io_sys, op) +p = ModelingToolkit.get_p(io_sys, op) x = zeros(5) u = zeros(1) d = zeros(3) diff --git a/test/extensions/test_infiniteopt.jl b/test/extensions/test_infiniteopt.jl index c732ff5f81..e1e4143bb7 100644 --- a/test/extensions/test_infiniteopt.jl +++ b/test/extensions/test_infiniteopt.jl @@ -62,7 +62,8 @@ InfiniteOpt.@variables(m, end) # Trace the dynamics -x0, p = ModelingToolkit.get_u0_p(io_sys, [model.θ => 0, model.ω => 0], [model.L => L]) +x0 = ModelingToolkit.get_u0(io_sys, [model.θ => 0, model.ω => 0]) +p = ModelingToolkit.get_p(io_sys, [model.L => L]; split = false, buffer_eltype = Any) xp = f[1](x, u, p, τ) cp = f_obs(x, u, p, τ) # Test that it's possible to trace through an observed function diff --git a/test/initial_values.jl b/test/initial_values.jl index 8bc052d6a7..d15f30c280 100644 --- a/test/initial_values.jl +++ b/test/initial_values.jl @@ -8,10 +8,10 @@ using SymbolicIndexingInterface @variables x(t)[1:3]=[1.0, 2.0, 3.0] y(t) z(t)[1:2] @mtkcompile sys=System([D(x) ~ t * x], t) simplify=false -@test get_u0(sys, [])[1] == [1.0, 2.0, 3.0] -@test get_u0(sys, [x => [2.0, 3.0, 4.0]])[1] == [2.0, 3.0, 4.0] -@test get_u0(sys, [x[1] => 2.0, x[2] => 3.0, x[3] => 4.0])[1] == [2.0, 3.0, 4.0] -@test get_u0(sys, [2.0, 3.0, 4.0])[1] == [2.0, 3.0, 4.0] +@test get_u0(sys, []) == [1.0, 2.0, 3.0] +@test get_u0(sys, [x => [2.0, 3.0, 4.0]]) == [2.0, 3.0, 4.0] +@test get_u0(sys, [x[1] => 2.0, x[2] => 3.0, x[3] => 4.0]) == [2.0, 3.0, 4.0] +@test get_u0(sys, [2.0, 3.0, 4.0]) == [2.0, 3.0, 4.0] @mtkcompile sys=System([ D(x) ~ 3x, @@ -22,19 +22,19 @@ using SymbolicIndexingInterface @test_throws ModelingToolkit.MissingVariablesError get_u0(sys, []) getter = getu(sys, [x..., y, z...]) -@test getter(get_u0(sys, [y => 4.0, z => [5.0, 6.0]])[1]) == collect(1.0:6.0) -@test getter(get_u0(sys, [y => 4.0, z => [3y, 4y]])[1]) == [1.0, 2.0, 3.0, 4.0, 12.0, 16.0] -@test getter(get_u0(sys, [y => 3.0, z[1] => 3y, z[2] => 2x[1]])[1]) == +@test getter(get_u0(sys, [y => 4.0, z => [5.0, 6.0]])) == collect(1.0:6.0) +@test getter(get_u0(sys, [y => 4.0, z => [3y, 4y]])) == [1.0, 2.0, 3.0, 4.0, 12.0, 16.0] +@test getter(get_u0(sys, [y => 3.0, z[1] => 3y, z[2] => 2x[1]])) == [1.0, 2.0, 3.0, 3.0, 9.0, 2.0] @variables w(t) @parameters p1 p2 -@test getter(get_u0(sys, [y => 2p1, z => [3y, 2p2]], [p1 => 5.0, p2 => 6.0])[1]) == +@test getter(get_u0(sys, [y => 2p1, z => [3y, 2p2], p1 => 5.0, p2 => 6.0])) == [1.0, 2.0, 3.0, 10.0, 30.0, 12.0] @test_throws Any getter(get_u0(sys, [y => 2w, w => 3.0, z[1] => 2p1, z[2] => 3p2])) @test getter(get_u0( - sys, [y => 2w, w => 3.0, z[1] => 2p1, z[2] => 3p2], [p1 => 3.0, p2 => 4.0])[1]) == + sys, [y => 2w, w => 3.0, z[1] => 2p1, z[2] => 3p2, p1 => 3.0, p2 => 4.0])) == [1.0, 2.0, 3.0, 6.0, 6.0, 12.0] # Issue#2566 @@ -47,7 +47,7 @@ u_vals = [X => 3.0] var_vals = [p1 => 1.0, p2 => 2.0, X => 3.0] desired_values = [p1, p2, p3] defaults = Dict([p3 => X]) -vals = ModelingToolkit.varmap_to_vars(var_vals, desired_values; defaults = defaults) +vals = ModelingToolkit.varmap_to_vars(merge(defaults, Dict(var_vals)), desired_values) @test vals == [1.0, 2.0, 3.0] # Issue#2565 From 35a06a460210719913a415c5af5e22a22fd3310b Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 22 May 2025 00:14:21 +0530 Subject: [PATCH 1854/2176] refactor: remove dead code --- src/utils.jl | 214 --------------------------------------------------- 1 file changed, 214 deletions(-) diff --git a/src/utils.jl b/src/utils.jl index 0d3c0b482c..3d5b95db41 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -11,20 +11,6 @@ end get_iv(D::Differential) = D.x -function make_operation(@nospecialize(op), args) - if op === (*) - args = filter(!_isone, args) - if isempty(args) - return 1 - end - elseif op === (+) - args = filter(!_iszero, args) - if isempty(args) - return 0 - end - end - return op(args...) -end function detime_dvs(op) if !iscall(op) @@ -49,24 +35,11 @@ function modified_unknowns!(munknowns, e::Equation, unknownlist = nothing) get_variables!(munknowns, e.lhs, unknownlist) end -macro showarr(x) - n = string(x) - quote - y = $(esc(x)) - println($n, " = ", summary(y)) - Base.print_array(stdout, y) - println() - y - end -end - function todict(d) eltype(d) <: Pair || throw(ArgumentError("The variable-value mapping must be a Dict.")) d isa Dict ? d : Dict(d) end -_merge(d1, d2) = merge(todict(d1), todict(d2)) - function _readable_code(ex) ex isa Expr || return ex if ex.head === :call @@ -248,10 +221,6 @@ end hasdefault(v) = hasmetadata(v, Symbolics.VariableDefaultValue) getdefault(v) = value(Symbolics.getdefaultval(v)) -function getdefaulttype(v) - def = value(getmetadata(unwrap(v), Symbolics.VariableDefaultValue, nothing)) - def === nothing ? Float64 : typeof(def) -end function setdefault(v, val) val === nothing ? v : wrap(setdefaultval(unwrap(v), value(val))) end @@ -510,18 +479,6 @@ function collect_applied_operators(x, op) end end -function find_derivatives!(vars, expr::Equation, f = identity) - (find_derivatives!(vars, expr.lhs, f); find_derivatives!(vars, expr.rhs, f); vars) -end -function find_derivatives!(vars, expr, f) - !iscall(O) && return vars - operation(O) isa Differential && push!(vars, f(O)) - for arg in arguments(O) - vars!(vars, arg) - end - return vars -end - """ $(TYPEDSIGNATURES) @@ -725,24 +682,6 @@ function check_scope_depth(scope, depth) end end -""" -$(SIGNATURES) - -find duplicates in an iterable object. -""" -function find_duplicates(xs, ::Val{Ret} = Val(false)) where {Ret} - appeared = Set() - duplicates = Set() - for x in xs - if x in appeared - push!(duplicates, x) - else - push!(appeared, x) - end - end - return Ret ? (appeared, duplicates) : duplicates -end - isarray(x) = x isa AbstractArray || x isa Symbolics.Arr function empty_substitutions(sys) @@ -753,30 +692,6 @@ function get_substitutions(sys) Dict([eq.lhs => eq.rhs for eq in observed(sys)]) end -function mergedefaults(defaults, varmap, vars) - defs = if varmap isa Dict - merge(defaults, varmap) - elseif eltype(varmap) <: Pair - merge(defaults, Dict(varmap)) - elseif eltype(varmap) <: Number - merge(defaults, Dict(zip(vars, varmap))) - else - defaults - end -end - -function mergedefaults(defaults, observedmap, varmap, vars) - defs = if varmap isa Dict - merge(observedmap, defaults, varmap) - elseif eltype(varmap) <: Pair - merge(observedmap, defaults, Dict(varmap)) - elseif eltype(varmap) <: Number - merge(observedmap, defaults, Dict(zip(vars, varmap))) - else - merge(observedmap, defaults) - end -end - @noinline function throw_missingvars_in_sys(vars) throw(ArgumentError("$vars are either missing from the variable map or missing from the system's unknowns/parameters list.")) end @@ -831,119 +746,6 @@ function promote_to_concrete(vs; tofloat = true, use_union = true) return y end -struct BitDict <: AbstractDict{Int, Int} - keys::Vector{Int} - values::Vector{Union{Nothing, Int}} -end -BitDict(n::Integer) = BitDict(Int[], Union{Nothing, Int}[nothing for _ in 1:n]) -struct BitDictKeySet <: AbstractSet{Int} - d::BitDict -end - -Base.keys(d::BitDict) = BitDictKeySet(d) -Base.in(v::Integer, s::BitDictKeySet) = s.d.values[v] !== nothing -Base.iterate(s::BitDictKeySet, state...) = iterate(s.d.keys, state...) -function Base.setindex!(d::BitDict, val::Integer, ind::Integer) - if 1 <= ind <= length(d.values) && d.values[ind] === nothing - push!(d.keys, ind) - end - d.values[ind] = val -end -function Base.getindex(d::BitDict, ind::Integer) - if 1 <= ind <= length(d.values) && d.values[ind] === nothing - return d.values[ind] - else - throw(KeyError(ind)) - end -end -function Base.iterate(d::BitDict, state...) - r = Base.iterate(d.keys, state...) - r === nothing && return nothing - k, state = r - (k => d.values[k]), state -end -function Base.empty!(d::BitDict) - for v in d.keys - d.values[v] = nothing - end - empty!(d.keys) - d -end - -abstract type AbstractSimpleTreeIter{T} end -Base.IteratorSize(::Type{<:AbstractSimpleTreeIter}) = Base.SizeUnknown() -Base.eltype(::Type{<:AbstractSimpleTreeIter{T}}) where {T} = childtype(T) -has_fast_reverse(::Type{<:AbstractSimpleTreeIter}) = true -has_fast_reverse(::T) where {T <: AbstractSimpleTreeIter} = has_fast_reverse(T) -reverse_buffer(it::AbstractSimpleTreeIter) = has_fast_reverse(it) ? nothing : eltype(it)[] -reverse_children!(::Nothing, cs) = Iterators.reverse(cs) -function reverse_children!(rev_buff, cs) - Iterators.reverse(cs) - empty!(rev_buff) - for c in cs - push!(rev_buff, c) - end - Iterators.reverse(rev_buff) -end - -struct StatefulPreOrderDFS{T} <: AbstractSimpleTreeIter{T} - t::T -end -function Base.iterate(it::StatefulPreOrderDFS, - state = (eltype(it)[it.t], reverse_buffer(it))) - stack, rev_buff = state - isempty(stack) && return nothing - t = pop!(stack) - for c in reverse_children!(rev_buff, children(t)) - push!(stack, c) - end - return t, state -end -struct StatefulPostOrderDFS{T} <: AbstractSimpleTreeIter{T} - t::T -end -function Base.iterate(it::StatefulPostOrderDFS, - state = (eltype(it)[it.t], falses(1), reverse_buffer(it))) - isempty(state[2]) && return nothing - vstack, sstack, rev_buff = state - while true - t = pop!(vstack) - isresume = pop!(sstack) - isresume && return t, state - push!(vstack, t) - push!(sstack, true) - for c in reverse_children!(rev_buff, children(t)) - push!(vstack, c) - push!(sstack, false) - end - end -end - -# Note that StatefulBFS also returns the depth. -struct StatefulBFS{T} <: AbstractSimpleTreeIter{T} - t::T -end -Base.eltype(::Type{<:StatefulBFS{T}}) where {T} = Tuple{Int, childtype(T)} -function Base.iterate(it::StatefulBFS, queue = (eltype(it)[(0, it.t)])) - isempty(queue) && return nothing - lv, t = popfirst!(queue) - nextlv = lv + 1 - for c in children(t) - push!(queue, (nextlv, c)) - end - return (lv, t), queue -end - -normalize_to_differential(s) = s - -function restrict_array_to_union(arr) - isempty(arr) && return arr - T = foldl(arr; init = Union{}) do prev, cur - Union{prev, typeof(cur)} - end - return Array{T, ndims(arr)}(arr) -end - function _with_unit(f, x, t, args...) x = f(x, args...) if hasmetadata(x, VariableUnit) && (t isa Symbolic && hasmetadata(t, VariableUnit)) @@ -1139,22 +941,6 @@ function similar_variable(var::BasicSymbolic, name = :anon; use_gensym = true) return sym end -function guesses_from_metadata!(guesses, vars) - varguesses = [getguess(v) for v in vars] - hasaguess = findall(!isnothing, varguesses) - for i in hasaguess - haskey(guesses, vars[i]) && continue - guesses[vars[i]] = varguesses[i] - end -end - -function has_diffvar_type(diffvar) - st = symtype(diffvar) - st === Real || eltype(st) === Real || st === Complex || eltype(st) === Complex || - st === Number || eltype(st) === Number || st === AbstractFloat || - eltype(st) === AbstractFloat -end - """ $(TYPEDSIGNATURES) From b70714d527963bacb0d93d8057690ea2feaade0b Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 22 May 2025 00:14:31 +0530 Subject: [PATCH 1855/2176] docs: add docstrings for `detime_dvs` and `retime_dvs` --- src/utils.jl | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/utils.jl b/src/utils.jl index 3d5b95db41..e2bc68e03f 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -11,7 +11,11 @@ end get_iv(D::Differential) = D.x +""" + $(TYPEDSIGNATURES) +Turn `x(t)` into `x` +""" function detime_dvs(op) if !iscall(op) op @@ -23,6 +27,11 @@ function detime_dvs(op) end end +""" + $(TYPEDSIGNATURES) + +Reverse `detime_dvs` for the given `dvs` using independent variable `iv`. +""" function retime_dvs(op, dvs, iv) issym(op) && return Sym{FnType{Tuple{symtype(iv)}, Real}}(nameof(op))(iv) iscall(op) ? From 569a4573400a33cdca9b66aee6221e23bf29020a Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 22 May 2025 12:48:51 +0530 Subject: [PATCH 1856/2176] fix: re-add `invalidate_cache!` for use with new metadata --- src/structural_transformation/StructuralTransformations.jl | 2 +- src/systems/abstractsystem.jl | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/structural_transformation/StructuralTransformations.jl b/src/structural_transformation/StructuralTransformations.jl index 3cf08448d1..346a800174 100644 --- a/src/structural_transformation/StructuralTransformations.jl +++ b/src/structural_transformation/StructuralTransformations.jl @@ -21,7 +21,7 @@ using ModelingToolkit: System, AbstractSystem, var_from_nested_derivative, Diffe has_tearing_state, defaults, InvalidSystemException, ExtraEquationsSystemException, ExtraVariablesSystemException, - vars!, + vars!, invalidate_cache!, IncrementalCycleTracker, add_edge_checked!, topological_sort, filter_kwargs, lower_varname_with_unit, lower_shift_varname_with_unit, setio, SparseMatrixCLIL, diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index fe0b47ddd1..41cc96bf57 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -899,6 +899,13 @@ end has_equations(::AbstractSystem) = true +""" + $(TYPEDSIGNATURES) + +Invalidate cached jacobians, etc. +""" +invalidate_cache!(sys::AbstractSystem) = sys + function Setfield.get(obj::AbstractSystem, ::Setfield.PropertyLens{field}) where {field} getfield(obj, field) end From be8f6c24d11310196d1f58eb13dc18e65f3b070c Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 22 May 2025 13:21:18 +0530 Subject: [PATCH 1857/2176] fix: re-add `normalize_to_differential` --- src/variables.jl | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/variables.jl b/src/variables.jl index 6317ebfa28..230c98a985 100644 --- a/src/variables.jl +++ b/src/variables.jl @@ -135,6 +135,8 @@ setirreducible(x, v::Bool) = setmetadata(x, VariableIrreducible, v) state_priority(x::Union{Num, Symbolics.Arr}) = state_priority(unwrap(x)) state_priority(x) = convert(Float64, getmetadata(x, VariableStatePriority, 0.0))::Float64 +normalize_to_differential(x) = x + function default_toterm(x) if iscall(x) && (op = operation(x)) isa Operator if !(op isa Differential) From 875738138d15bb171a3ab2255a1daa9a9861d6fd Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 22 May 2025 14:37:03 +0530 Subject: [PATCH 1858/2176] docs: add docstring for `reordered_matrix` --- src/structural_transformation/utils.jl | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/structural_transformation/utils.jl b/src/structural_transformation/utils.jl index e1c8a74eca..0bb7c0e4ea 100644 --- a/src/structural_transformation/utils.jl +++ b/src/structural_transformation/utils.jl @@ -352,8 +352,14 @@ function but_ordered_incidence(ts::TearingState, varmask = highest_order_variabl mm[[var_eq_matching[v] for v in vordering if var_eq_matching[v] isa Int], vordering], bb end -# debugging use -function reordered_matrix(sys, torn_matching) +""" + $(TYPEDSIGNATURES) + +Given a system `sys` and torn variable-equation matching `torn_matching`, return the sparse +incidence matrix of the system with SCCs grouped together, and each SCC sorted to contain +the analytically solved equations/variables before the unsolved ones. +""" +function reordered_matrix(sys::System, torn_matching) s = TearingState(sys) complete!(s.structure) @unpack graph = s.structure From a4c55866d05cc001dfad9d45fbc40c96c9c73e82 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 22 May 2025 14:37:12 +0530 Subject: [PATCH 1859/2176] refactor: remove dead code in structural transformation utils --- src/structural_transformation/utils.jl | 17 ----------------- 1 file changed, 17 deletions(-) diff --git a/src/structural_transformation/utils.jl b/src/structural_transformation/utils.jl index 0bb7c0e4ea..131010550e 100644 --- a/src/structural_transformation/utils.jl +++ b/src/structural_transformation/utils.jl @@ -438,23 +438,6 @@ function torn_system_jacobian_sparsity(sys) return sparse(I, J, true, neqs, neqs) end -### -### Nonlinear equation(s) solving -### - -@noinline function nlsolve_failure(rc) - error("The nonlinear solver failed with the return code $rc.") -end - -function numerical_nlsolve(f, u0, p) - prob = NonlinearProblem{false}(f, u0, p) - sol = solve(prob, SimpleNewtonRaphson()) - rc = sol.retcode - (rc == ReturnCode.Success) || nlsolve_failure(rc) - # TODO: robust initial guess, better debugging info, and residual check - sol.u -end - ### ### Misc ### From 690e5c7e387eb4882f7eb6f66439a2dcbf0a620e Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 22 May 2025 14:37:24 +0530 Subject: [PATCH 1860/2176] fix: fix construction of `lb` and `ub` in `OptimizationProblem` --- src/problems/optimizationproblem.jl | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/problems/optimizationproblem.jl b/src/problems/optimizationproblem.jl index 3f121e3d2a..6d5fb1d79e 100644 --- a/src/problems/optimizationproblem.jl +++ b/src/problems/optimizationproblem.jl @@ -114,9 +114,14 @@ function SciMLBase.OptimizationProblem{iip}( end ps = parameters(sys) - defs = merge(defaults(sys), to_varmap(op, dvs)) - lb = varmap_to_vars(dvs .=> lb, dvs; defaults = defs, tofloat = false) - ub = varmap_to_vars(dvs .=> ub, dvs; defaults = defs, tofloat = false) + defs = defaults(sys) + op = to_varmap(op, dvs) + lbmap = merge(op, AnyDict(dvs .=> lb)) + _, _ = build_operating_point!(sys, lbmap, Dict(), Dict(), defs, dvs, ps) + lb = varmap_to_vars(lbmap, dvs; tofloat = false) + ubmap = merge(op, AnyDict(dvs .=> ub)) + _, _ = build_operating_point!(sys, ubmap, Dict(), Dict(), defs, dvs, ps) + ub = varmap_to_vars(ubmap, dvs; tofloat = false) if !isnothing(lb) && all(lb .== -Inf) && !isnothing(ub) && all(ub .== Inf) lb = nothing From e9a8dfb4fccac25b7989714e34c6689b0cb359c1 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 22 May 2025 14:50:27 +0530 Subject: [PATCH 1861/2176] test: do not test system subtype warning in `independent_variables` --- test/abstractsystem.jl | 3 --- 1 file changed, 3 deletions(-) diff --git a/test/abstractsystem.jl b/test/abstractsystem.jl index bd5b6fe542..09b21ea290 100644 --- a/test/abstractsystem.jl +++ b/test/abstractsystem.jl @@ -8,7 +8,6 @@ struct MyNLS <: MT.AbstractSystem name::Any systems::Any end -@test_logs (:warn,) tmp=independent_variables(MyNLS("sys", [])) tmp = independent_variables(MyNLS("sys", [])) @test tmp == [] @@ -17,7 +16,6 @@ struct MyTDS <: MT.AbstractSystem name::Any systems::Any end -@test_logs (:warn,) iv=independent_variables(MyTDS(t, "sys", [])) iv = independent_variables(MyTDS(t, "sys", [])) @test all(isequal.(iv, [t])) @@ -26,6 +24,5 @@ struct MyMVS <: MT.AbstractSystem name::Any systems::Any end -@test_logs (:warn,) ivs=independent_variables(MyMVS([t, x], "sys", [])) ivs = independent_variables(MyMVS([t, x], "sys", [])) @test all(isequal.(ivs, [t, x])) From f473a43a7c8eaa77d372f4e23d4b3195d8d91b5c Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 26 May 2025 15:41:43 +0530 Subject: [PATCH 1862/2176] fix: fix `buffer_eltype` implementation in `varmap_to_vars` --- 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 3f73ea42cf..76ade8e910 100644 --- a/src/systems/problem_utils.jl +++ b/src/systems/problem_utils.jl @@ -404,7 +404,7 @@ function varmap_to_vars(varmap::AbstractDict, vars::Vector; if buffer_eltype == Nothing vals = promote_to_concrete(vals; tofloat, use_union) else - vals = buffer_eltype.(vals) + vals = Vector{buffer_eltype}(vals) end end From c0052f200f5435bb765151e39fa75e7469c209d8 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 23 May 2025 15:26:01 +0530 Subject: [PATCH 1863/2176] docs: update `basics` docs to new syntax --- docs/src/basics/AbstractSystem.md | 2 +- docs/src/basics/Composition.md | 10 +++++----- docs/src/basics/Debugging.md | 4 ++-- docs/src/basics/Events.md | 29 +++++++++++++++-------------- docs/src/basics/FAQ.md | 8 ++++---- docs/src/basics/InputOutput.md | 2 +- docs/src/basics/Linearization.md | 4 ++-- docs/src/basics/MTKLanguage.md | 18 +++++++++--------- docs/src/basics/Precompilation.md | 2 +- docs/src/basics/Validation.md | 2 +- 10 files changed, 41 insertions(+), 40 deletions(-) diff --git a/docs/src/basics/AbstractSystem.md b/docs/src/basics/AbstractSystem.md index d1707f822f..62f842b2b2 100644 --- a/docs/src/basics/AbstractSystem.md +++ b/docs/src/basics/AbstractSystem.md @@ -152,7 +152,7 @@ a lower level in the system. ## Namespacing By default, unsimplified systems will namespace variables accessed via `getproperty`. -Systems created via `@mtkbuild`, or ones passed through `structural_simplify` or +Systems created via `@mtkcompile`, or ones passed through `mtkcompile` or `complete` will not perform this namespacing. However, all of these processes modify the system in a variety of ways. To toggle namespacing without transforming any other property of the system, use `toggle_namespacing`. diff --git a/docs/src/basics/Composition.md b/docs/src/basics/Composition.md index d3de71d696..f6abe97d38 100644 --- a/docs/src/basics/Composition.md +++ b/docs/src/basics/Composition.md @@ -42,7 +42,7 @@ equations(connected) # Differential(t)(decay1₊x(t)) ~ decay1₊f(t) - (decay1₊a*(decay1₊x(t))) # Differential(t)(decay2₊x(t)) ~ decay2₊f(t) - (decay2₊a*(decay2₊x(t))) -simplified_sys = structural_simplify(connected) +simplified_sys = mtkcompile(connected) equations(simplified_sys) ``` @@ -84,7 +84,7 @@ example, let's say there is a variable `x` in `unknowns` and a variable `x` in `subsys`. We can declare that these two variables are the same by specifying their equality: `x ~ subsys.x` in the `eqs` for `sys`. This algebraic relationship can then be simplified by transformations -like `structural_simplify` which will be described later. +like `mtkcompile` which will be described later. ### Numerics with Composed Models @@ -169,7 +169,7 @@ parameters(level3) In many cases, the nicest way to build a model may leave a lot of unnecessary variables. Thus one may want to remove these equations -before numerically solving. The `structural_simplify` function removes +before numerically solving. The `mtkcompile` function removes these trivial equality relationships and trivial singularity equations, i.e. equations which result in `0~0` expressions, in over-specified systems. @@ -227,7 +227,7 @@ values. The user of this model can then solve this model simply by specifying the values at the highest level: ```@example compose -sireqn_simple = structural_simplify(sir) +sireqn_simple = mtkcompile(sir) equations(sireqn_simple) ``` @@ -251,7 +251,7 @@ sol[reqn.R] ## Tearing Problem Construction Some system types (specifically `NonlinearSystem`) can be further -reduced if `structural_simplify` has already been applied to them. This is done +reduced if `mtkcompile` has already been applied to them. This is done by using the alternative problem constructors (`BlockNonlinearProblem`). In these cases, the constructor uses the knowledge of the strongly connected components calculated during the process of simplification diff --git a/docs/src/basics/Debugging.md b/docs/src/basics/Debugging.md index 6e2d471461..5bc509acfd 100644 --- a/docs/src/basics/Debugging.md +++ b/docs/src/basics/Debugging.md @@ -13,7 +13,7 @@ using ModelingToolkit: t_nounits as t, D_nounits as D eqs = [D(u1) ~ -√(u1), D(u2) ~ -√(u2), D(u3) ~ -√(u3)] defaults = [u1 => 1.0, u2 => 2.0, u3 => 3.0] @named sys = ODESystem(eqs, t; defaults) -sys = structural_simplify(sys) +sys = mtkcompile(sys) ``` This problem causes the ODE solver to crash: @@ -38,7 +38,7 @@ We could have figured that out ourselves, but it is not always so obvious for mo Suppose we also want to validate that `u1 + u2 >= 2.0`. We can do this via the assertions functionality. ```@example debug -@mtkbuild sys = ODESystem(eqs, t; defaults, assertions = [(u1 + u2 >= 2.0) => "Oh no!"]) +@mtkcompile sys = ODESystem(eqs, t; defaults, assertions = [(u1 + u2 >= 2.0) => "Oh no!"]) ``` The assertions must be an iterable of pairs, where the first element is the symbolic condition and diff --git a/docs/src/basics/Events.md b/docs/src/basics/Events.md index 2808204d61..5d5df0a377 100644 --- a/docs/src/basics/Events.md +++ b/docs/src/basics/Events.md @@ -59,7 +59,7 @@ For example, consider the following system. ```julia @variables x(t) y(t) @parameters p(t) -@mtkbuild sys = ODESystem([x * y ~ p, D(x) ~ 0], t) +@mtkcompile sys = ODESystem([x * y ~ p, D(x) ~ 0], t) event = [t == 1] => [x ~ Pre(x) + 1] ``` @@ -134,7 +134,7 @@ function UnitMassWithFriction(k; name) D(v) ~ sin(t) - k * sign(v)] ODESystem(eqs, t; continuous_events = [v ~ 0], name) # when v = 0 there is a discontinuity end -@mtkbuild m = UnitMassWithFriction(0.7) +@mtkcompile m = UnitMassWithFriction(0.7) prob = ODEProblem(m, Pair[], (0, 10pi)) sol = solve(prob, Tsit5()) plot(sol) @@ -154,8 +154,9 @@ like this root_eqs = [x ~ 0] # the event happens at the ground x(t) = 0 affect = [v ~ -Pre(v)] # the effect is that the velocity changes sign -@mtkbuild ball = ODESystem([D(x) ~ v - D(v) ~ -9.8], t; continuous_events = root_eqs => affect) # equation => affect +@mtkcompile ball = ODESystem( + [D(x) ~ v + D(v) ~ -9.8], t; continuous_events = root_eqs => affect) # equation => affect tspan = (0.0, 5.0) prob = ODEProblem(ball, Pair[], tspan) @@ -174,7 +175,7 @@ Multiple events? No problem! This example models a bouncing ball in 2D that is e continuous_events = [[x ~ 0] => [vx ~ -Pre(vx)] [y ~ -1.5, y ~ 1.5] => [vy ~ -Pre(vy)]] -@mtkbuild ball = ODESystem( +@mtkcompile ball = ODESystem( [ D(x) ~ vx, D(y) ~ vy, @@ -254,7 +255,7 @@ end reflect = [x ~ 0] => (bb_affect!, [v], [], [], nothing) -@mtkbuild bb_sys = ODESystem(bb_eqs, t, sts, par, +@mtkcompile bb_sys = ODESystem(bb_eqs, t, sts, par, continuous_events = reflect) u0 = [v => 0.0, x => 1.0] @@ -299,7 +300,7 @@ injection = (t == tinject) => [N ~ Pre(N) + M] u0 = [N => 0.0] tspan = (0.0, 20.0) p = [α => 100.0, tinject => 10.0, M => 50] -@mtkbuild osys = ODESystem(eqs, t, [N], [α, M, tinject]; discrete_events = injection) +@mtkcompile osys = ODESystem(eqs, t, [N], [α, M, tinject]; discrete_events = injection) oprob = ODEProblem(osys, u0, tspan, p) sol = solve(oprob, Tsit5(); tstops = 10.0) plot(sol) @@ -318,7 +319,7 @@ to ```@example events injection = ((t == tinject) & (N < 50)) => [N ~ Pre(N) + M] -@mtkbuild osys = ODESystem(eqs, t, [N], [M, tinject, α]; discrete_events = injection) +@mtkcompile osys = ODESystem(eqs, t, [N], [M, tinject, α]; discrete_events = injection) oprob = ODEProblem(osys, u0, tspan, p) sol = solve(oprob, Tsit5(); tstops = 10.0) plot(sol) @@ -345,7 +346,7 @@ killing = ModelingToolkit.SymbolicDiscreteCallback( tspan = (0.0, 30.0) p = [α => 100.0, tinject => 10.0, M => 50, tkill => 20.0] -@mtkbuild osys = ODESystem(eqs, t, [N], [α, M, tinject, tkill]; +@mtkcompile osys = ODESystem(eqs, t, [N], [α, M, tinject, tkill]; discrete_events = [injection, killing]) oprob = ODEProblem(osys, u0, tspan, p) sol = solve(oprob, Tsit5(); tstops = [10.0, 20.0]) @@ -374,7 +375,7 @@ killing = ModelingToolkit.SymbolicDiscreteCallback( [20.0] => [α ~ 0.0], discrete_parameters = α, iv = t) p = [α => 100.0, M => 50] -@mtkbuild osys = ODESystem(eqs, t, [N], [α, M]; +@mtkcompile osys = ODESystem(eqs, t, [N], [α, M]; discrete_events = [injection, killing]) oprob = ODEProblem(osys, u0, tspan, p) sol = solve(oprob, Tsit5()) @@ -414,7 +415,7 @@ example: ev = ModelingToolkit.SymbolicDiscreteCallback( 1.0 => [c ~ Pre(c) + 1], discrete_parameters = c, iv = t) -@mtkbuild sys = ODESystem( +@mtkcompile sys = ODESystem( D(x) ~ c * cos(x), t, [x], [c]; discrete_events = [ev]) prob = ODEProblem(sys, [x => 0.0], (0.0, 2pi), [c => 1.0]) @@ -435,7 +436,7 @@ will be saved. If we repeat the above example with `c` not a `discrete_parameter @variables x(t) @parameters c(t) -@mtkbuild sys = ODESystem( +@mtkcompile sys = ODESystem( D(x) ~ c * cos(x), t, [x], [c]; discrete_events = [1.0 => [c ~ Pre(c) + 1]]) prob = ODEProblem(sys, [x => 0.0], (0.0, 2pi), [c => 1.0]) @@ -538,7 +539,7 @@ to the system. ```@example events @named sys = ODESystem( eqs, t, [temp], params; continuous_events = [furnace_disable, furnace_enable]) -ss = structural_simplify(sys) +ss = mtkcompile(sys) prob = ODEProblem(ss, [temp => 0.0, furnace_on => true], (0.0, 10.0)) sol = solve(prob, Tsit5()) plot(sol) @@ -651,7 +652,7 @@ We can now simulate the encoder. ```@example events @named sys = ODESystem( eqs, t, [theta, omega], params; continuous_events = [qAevt, qBevt]) -ss = structural_simplify(sys) +ss = mtkcompile(sys) prob = ODEProblem(ss, [theta => 0.0], (0.0, pi)) sol = solve(prob, Tsit5(); dtmax = 0.01) sol.ps[cnt] diff --git a/docs/src/basics/FAQ.md b/docs/src/basics/FAQ.md index 10671299c6..e83b1f1336 100644 --- a/docs/src/basics/FAQ.md +++ b/docs/src/basics/FAQ.md @@ -28,7 +28,7 @@ are similarly undocumented. Following is the list of behaviors that should be re - `setindex!(::MTKParameters, value, ::ParameterIndex)` can be used to set the value of a parameter with the given index. - `parameter_values(sys, sym)` will return a `ParameterIndex` object if `sys` has been - `complete`d (through `structural_simplify`, `complete` or `@mtkbuild`). + `complete`d (through `mtkcompile`, `complete` or `@mtkcompile`). - `copy(::MTKParameters)` is defined and duplicates the parameter object, including the memory used by the underlying buffers. @@ -194,7 +194,7 @@ p, replace, alias = SciMLStructures.canonicalize(Tunable(), prob.p) # ERROR: ArgumentError: SymbolicUtils.BasicSymbolic{Real}[xˍt(t)] are missing from the variable map. -This error can come up after running `structural_simplify` on a system that generates dummy derivatives (i.e. variables with `ˍt`). For example, here even though all the variables are defined with initial values, the `ODEProblem` generation will throw an error that defaults are missing from the variable map. +This error can come up after running `mtkcompile` on a system that generates dummy derivatives (i.e. variables with `ˍt`). For example, here even though all the variables are defined with initial values, the `ODEProblem` generation will throw an error that defaults are missing from the variable map. ```julia using ModelingToolkit @@ -206,7 +206,7 @@ eqs = [x1 + x2 + 1 ~ 0 x1 + D(x3) + x4 + 3 ~ 0 2 * D(D(x1)) + D(D(x2)) + D(D(x3)) + D(x4) + 4 ~ 0] @named sys = ODESystem(eqs, t) -sys = structural_simplify(sys) +sys = mtkcompile(sys) prob = ODEProblem(sys, [], (0, 1)) ``` @@ -237,7 +237,7 @@ using ModelingToolkit: t_nounits as t, D_nounits as D sts = @variables x1(t) = 0.0 eqs = [D(x1) ~ 1.1 * x1] -@mtkbuild sys = ODESystem(eqs, t) +@mtkcompile sys = ODESystem(eqs, t) prob = ODEProblem{false}(sys, [], (0, 1); u0_constructor = x -> SVector(x...)) ``` diff --git a/docs/src/basics/InputOutput.md b/docs/src/basics/InputOutput.md index 4dc5a3d50f..35a3885dbd 100644 --- a/docs/src/basics/InputOutput.md +++ b/docs/src/basics/InputOutput.md @@ -30,7 +30,7 @@ This function takes a vector of variables that are to be considered inputs, i.e. !!! note "Un-simplified system" - This function expects `sys` to be un-simplified, i.e., `structural_simplify` or `@mtkbuild` should not be called on the system before passing it into this function. `generate_control_function` calls a special version of `structural_simplify` internally. + This function expects `sys` to be un-simplified, i.e., `mtkcompile` or `@mtkcompile` should not be called on the system before passing it into this function. `generate_control_function` calls a special version of `mtkcompile` internally. ### Example: diff --git a/docs/src/basics/Linearization.md b/docs/src/basics/Linearization.md index 1c06ce72d4..27b8ec9903 100644 --- a/docs/src/basics/Linearization.md +++ b/docs/src/basics/Linearization.md @@ -29,7 +29,7 @@ eqs = [u ~ kp * (r - y) # P controller D(x) ~ -x + u # First-order plant y ~ x] # Output equation -@named sys = ODESystem(eqs, t) # Do not call @mtkbuild when linearizing +@named sys = ODESystem(eqs, t) # Do not call @mtkcompile when linearizing matrices, simplified_sys = linearize(sys, [r], [y]) # Linearize from r to y matrices ``` @@ -47,7 +47,7 @@ using ModelingToolkit: inputs, outputs !!! note "Un-simplified system" - Linearization expects `sys` to be un-simplified, i.e., `structural_simplify` or `@mtkbuild` should not be called on the system before linearizing. + Linearization expects `sys` to be un-simplified, i.e., `mtkcompile` or `@mtkcompile` should not be called on the system before linearizing. ## Operating point diff --git a/docs/src/basics/MTKLanguage.md b/docs/src/basics/MTKLanguage.md index ba6d2c34b5..05847581f6 100644 --- a/docs/src/basics/MTKLanguage.md +++ b/docs/src/basics/MTKLanguage.md @@ -1,4 +1,4 @@ -# [ModelingToolkit Language: Modeling with `@mtkmodel`, `@connectors` and `@mtkbuild`](@id mtk_language) +# [ModelingToolkit Language: Modeling with `@mtkmodel`, `@connectors` and `@mtkcompile`](@id mtk_language) ## MTK Model @@ -150,7 +150,7 @@ end - Whenever a parameter or variable has initial value, for example `v(t) = 0.0`, a symbolic variable named `v` with initial value 0.0 and a keyword argument `v`, with default value `nothing` are created.
This way, users can optionally pass new value of `v` while creating a component. ```julia -julia> @mtkbuild model_c1 = ModelC(; v = 2.0); +julia> @mtkcompile model_c1 = ModelC(; v = 2.0); julia> ModelingToolkit.getdefault(model_c1.v) 2.0 @@ -182,7 +182,7 @@ One or more partial systems can be extended in a higher system with `@extend` st ```@example mtkmodel-example using ModelingToolkit: getdefault -@mtkbuild model_c3 = ModelC(; model_a.k_array = [1.0, 2.0]) +@mtkcompile model_c3 = ModelC(; model_a.k_array = [1.0, 2.0]) getdefault(model_c3.model_a.k_array[1]) # 1.0 @@ -529,28 +529,28 @@ end ## Build structurally simplified models: -`@mtkbuild` builds an instance of a component and returns a structurally simplied system. +`@mtkcompile` builds an instance of a component and returns a structurally simplied system. ```julia -@mtkbuild sys = CustomModel() +@mtkcompile sys = CustomModel() ``` This is equivalent to: ```julia @named model = CustomModel() -sys = structural_simplify(model) +sys = mtkcompile(model) ``` -Pass keyword arguments to `structural_simplify` using the following syntax: +Pass keyword arguments to `mtkcompile` using the following syntax: ```julia -@mtkbuild sys=CustomModel() fully_determined=false +@mtkcompile sys=CustomModel() fully_determined=false ``` This is equivalent to: ```julia @named model = CustomModel() -sys = structural_simplify(model; fully_determined = false) +sys = mtkcompile(model; fully_determined = false) ``` diff --git a/docs/src/basics/Precompilation.md b/docs/src/basics/Precompilation.md index 97111f0d6b..0bf9a86653 100644 --- a/docs/src/basics/Precompilation.md +++ b/docs/src/basics/Precompilation.md @@ -22,7 +22,7 @@ using ModelingToolkit @variables x(ModelingToolkit.t_nounits) @named sys = ODESystem([ModelingToolkit.D_nounits(x) ~ -x + 1], ModelingToolkit.t_nounits) -prob = ODEProblem(structural_simplify(sys), [x => 30.0], (0, 100), [], +prob = ODEProblem(mtkcompile(sys), [x => 30.0], (0, 100), [], eval_expression = true, eval_module = @__MODULE__) end diff --git a/docs/src/basics/Validation.md b/docs/src/basics/Validation.md index 79c5d0d214..74715d351e 100644 --- a/docs/src/basics/Validation.md +++ b/docs/src/basics/Validation.md @@ -112,7 +112,7 @@ ps = @parameters s=-1 [unit = u"cm"] c=c [unit = u"cm"] eqs = [D(a) ~ dummycomplex(c, s);] sys = ODESystem( eqs, t, [sts...;], [ps...;], name = :sys, checks = ~ModelingToolkit.CheckUnits) -sys_simple = structural_simplify(sys) +sys_simple = mtkcompile(sys) ``` ## `DynamicQuantities` Literals From 750db43807fa40cb734b1253c2560a7dbe272ed0 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 23 May 2025 15:26:12 +0530 Subject: [PATCH 1864/2176] docs: update `examples` docs to new syntax --- docs/src/examples/higher_order.md | 8 +++---- .../modelingtoolkitize_index_reduction.md | 4 ++-- docs/src/examples/perturbation.md | 4 ++-- docs/src/examples/remake.md | 4 ++-- docs/src/examples/sparse_jacobians.md | 2 +- docs/src/examples/spring_mass.md | 24 +++++++++---------- docs/src/examples/tearing_parallelism.md | 24 +++++++++---------- 7 files changed, 35 insertions(+), 35 deletions(-) diff --git a/docs/src/examples/higher_order.md b/docs/src/examples/higher_order.md index fac707525f..95480e283b 100644 --- a/docs/src/examples/higher_order.md +++ b/docs/src/examples/higher_order.md @@ -4,7 +4,7 @@ ModelingToolkit has a system for transformations of mathematical systems. These transformations allow for symbolically changing the representation of the model to problems that are easier to numerically solve. One simple to demonstrate transformation, is -`structural_simplify`, which does a lot of tricks, one being the +`mtkcompile`, which does a lot of tricks, one being the transformation that turns an Nth order ODE into N coupled 1st order ODEs. @@ -32,7 +32,7 @@ using ModelingToolkit: t_nounits as t, D_nounits as D D(z) ~ x * y - β * z end end -@mtkbuild sys = SECOND_ORDER() +@mtkcompile sys = SECOND_ORDER() ``` The second order ODE has been automatically transformed to two first order ODEs. @@ -43,7 +43,7 @@ and this syntax extends to `N`-th order. Also, we can use `*` or `∘` to compos `Differential`s, like `Differential(t) * Differential(x)`. Now let's transform this into the `ODESystem` of first order components. -We do this by calling `structural_simplify`: +We do this by calling `mtkcompile`: Now we can directly numerically solve the lowered system. Note that, following the original problem, the solution requires knowing the @@ -54,7 +54,7 @@ but we still have to provide a value for the latter. ```@example orderlowering u0 = [D(sys.x) => 2.0] tspan = (0.0, 100.0) -prob = ODEProblem(sys, u0, tspan, [], jac = true) +prob = ODEProblem(sys, u0, tspan, jac = true) sol = solve(prob, Tsit5()) using Plots plot(sol, idxs = (sys.x, sys.y)) diff --git a/docs/src/examples/modelingtoolkitize_index_reduction.md b/docs/src/examples/modelingtoolkitize_index_reduction.md index b19ea46701..9759dc2081 100644 --- a/docs/src/examples/modelingtoolkitize_index_reduction.md +++ b/docs/src/examples/modelingtoolkitize_index_reduction.md @@ -29,7 +29,7 @@ p = [9.8, 1] tspan = (0, 10.0) pendulum_prob = ODEProblem(pendulum_fun!, u0, tspan, p) traced_sys = modelingtoolkitize(pendulum_prob) -pendulum_sys = structural_simplify(dae_index_lowering(traced_sys)) +pendulum_sys = mtkcompile(dae_index_lowering(traced_sys)) prob = ODEProblem(pendulum_sys, [], tspan) sol = solve(prob, Rodas5P(), abstol = 1e-8, reltol = 1e-8) plot(sol, idxs = unknowns(traced_sys)) @@ -157,7 +157,7 @@ numerical solver. Let's try that out: ```@example indexred traced_sys = modelingtoolkitize(pendulum_prob) -pendulum_sys = structural_simplify(dae_index_lowering(traced_sys)) +pendulum_sys = mtkcompile(dae_index_lowering(traced_sys)) prob = ODEProblem(pendulum_sys, Pair[], tspan) sol = solve(prob, Rodas5P()) diff --git a/docs/src/examples/perturbation.md b/docs/src/examples/perturbation.md index 0d1a493cb4..20cef4067c 100644 --- a/docs/src/examples/perturbation.md +++ b/docs/src/examples/perturbation.md @@ -43,7 +43,7 @@ eqs_pert = taylor_coeff(eq_pert, ϵ, 0:2) These are the ODEs we want to solve. Now construct an `ODESystem`, which automatically inserts dummy derivatives for the velocities: ```@example perturbation -@mtkbuild sys = ODESystem(eqs_pert, t) +@mtkcompile sys = System(eqs_pert, t) ``` To solve the `ODESystem`, we generate an `ODEProblem` with initial conditions $x(0) = 0$, and $ẋ(0) = 1$, and solve it: @@ -87,7 +87,7 @@ We follow the same steps as in the previous example to construct the `ODESystem` ```@example perturbation eq_pert = substitute(eq, x => x_series) eqs_pert = taylor_coeff(eq_pert, ϵ, 0:2) -@mtkbuild sys = ODESystem(eqs_pert, t) +@mtkcompile sys = System(eqs_pert, t) ``` We solve and plot it as in the previous example, and compare the solution with $ϵ=0.1$ to the exact solution $x(t, ϵ) = e^{-ϵ t} \sin(\sqrt{(1-ϵ^2)}\,t) / \sqrt{1-ϵ^2}$ of the unperturbed equation: diff --git a/docs/src/examples/remake.md b/docs/src/examples/remake.md index 91dba4d7ae..9d40a83dbf 100644 --- a/docs/src/examples/remake.md +++ b/docs/src/examples/remake.md @@ -14,7 +14,7 @@ using ModelingToolkit: t_nounits as t, D_nounits as D @variables x(t) y(t) eqs = [D(x) ~ (α - β * y) * x D(y) ~ (δ * x - γ) * y] -@mtkbuild odesys = ODESystem(eqs, t) +@mtkcompile odesys = System(eqs, t) ``` To create the "data" for optimization, we will solve the system with a known set of @@ -24,7 +24,7 @@ parameters. using OrdinaryDiffEq odeprob = ODEProblem( - odesys, [x => 1.0, y => 1.0], (0.0, 10.0), [α => 1.5, β => 1.0, γ => 3.0, δ => 1.0]) + odesys, [x => 1.0, y => 1.0, α => 1.5, β => 1.0, γ => 3.0, δ => 1.0], (0.0, 10.0)) timesteps = 0.0:0.1:10.0 sol = solve(odeprob, Tsit5(); saveat = timesteps) data = Array(sol) diff --git a/docs/src/examples/sparse_jacobians.md b/docs/src/examples/sparse_jacobians.md index 03bd80d432..79a93c3471 100644 --- a/docs/src/examples/sparse_jacobians.md +++ b/docs/src/examples/sparse_jacobians.md @@ -54,7 +54,7 @@ prob = ODEProblem(brusselator_2d_loop, u0, (0.0, 11.5), p) Now let's use `modelingtoolkitize` to generate the symbolic version: ```@example sparsejac -@mtkbuild sys = modelingtoolkitize(prob); +@mtkcompile sys = modelingtoolkitize(prob); nothing # hide ``` diff --git a/docs/src/examples/spring_mass.md b/docs/src/examples/spring_mass.md index 355e5c20b2..8d42592796 100644 --- a/docs/src/examples/spring_mass.md +++ b/docs/src/examples/spring_mass.md @@ -13,13 +13,13 @@ function Mass(; name, m = 1.0, xy = [0.0, 0.0], u = [0.0, 0.0]) ps = @parameters m = m sts = @variables pos(t)[1:2]=xy v(t)[1:2]=u eqs = scalarize(D.(pos) .~ v) - ODESystem(eqs, t, [pos..., v...], ps; name) + System(eqs, t, [pos..., v...], ps; name) end function Spring(; name, k = 1e4, l = 1.0) ps = @parameters k=k l=l @variables x(t), dir(t)[1:2] - ODESystem(Equation[], t, [x, dir...], ps; name) + System(Equation[], t, [x, dir...], ps; name) end function connect_spring(spring, a, b) @@ -43,9 +43,9 @@ g = [0.0, -9.81] eqs = [connect_spring(spring, mass.pos, center) scalarize(D.(mass.v) .~ spring_force(spring) / mass.m .+ g)] -@named _model = ODESystem(eqs, t, [spring.x; spring.dir; mass.pos], []) +@named _model = System(eqs, t, [spring.x; spring.dir; mass.pos], []) @named model = compose(_model, mass, spring) -sys = structural_simplify(model) +sys = mtkcompile(model) prob = ODEProblem(sys, [], (0.0, 3.0)) sol = solve(prob, Rosenbrock23()) @@ -56,18 +56,18 @@ plot(sol) ### Building the components -For each component, we use a Julia function that returns an `ODESystem`. At the top, we define the fundamental properties of a `Mass`: it has a mass `m`, a position `pos` and a velocity `v`. We also define that the velocity is the rate of change of position with respect to time. +For each component, we use a Julia function that returns an `System`. At the top, we define the fundamental properties of a `Mass`: it has a mass `m`, a position `pos` and a velocity `v`. We also define that the velocity is the rate of change of position with respect to time. ```@example component function Mass(; name, m = 1.0, xy = [0.0, 0.0], u = [0.0, 0.0]) ps = @parameters m = m sts = @variables pos(t)[1:2]=xy v(t)[1:2]=u eqs = scalarize(D.(pos) .~ v) - ODESystem(eqs, t, [pos..., v...], ps; name) + System(eqs, t, [pos..., v...], ps; name) end ``` -Note that this is an incompletely specified `ODESystem`. It cannot be simulated on its own, since the equations for the velocity `v[1:2](t)` are unknown. Notice the addition of a `name` keyword. This allows us to generate different masses with different names. A `Mass` can now be constructed as: +Note that this is an incompletely specified `System`. It cannot be simulated on its own, since the equations for the velocity `v[1:2](t)` are unknown. Notice the addition of a `name` keyword. This allows us to generate different masses with different names. A `Mass` can now be constructed as: ```@example component Mass(name = :mass1) @@ -85,7 +85,7 @@ Next, we build the spring component. It is characterized by the spring constant function Spring(; name, k = 1e4, l = 1.0) ps = @parameters k=k l=l @variables x(t), dir(t)[1:2] - ODESystem(Equation[], t, [x, dir...], ps; name) + System(Equation[], t, [x, dir...], ps; name) end ``` @@ -129,7 +129,7 @@ eqs = [connect_spring(spring, mass.pos, center) Finally, we can build the model using these equations and components. ```@example component -@named _model = ODESystem(eqs, t, [spring.x; spring.dir; mass.pos], []) +@named _model = System(eqs, t, [spring.x; spring.dir; mass.pos], []) @named model = compose(_model, mass, spring) ``` @@ -153,10 +153,10 @@ parameters(model) ### Simplifying and solving this system -This system can be solved directly as a DAE using [one of the DAE solvers from DifferentialEquations.jl](https://docs.sciml.ai/DiffEqDocs/stable/solvers/dae_solve/). However, we can symbolically simplify the system first beforehand. Running `structural_simplify` eliminates unnecessary variables from the model to give the leanest numerical representation of the system. +This system can be solved directly as a DAE using [one of the DAE solvers from DifferentialEquations.jl](https://docs.sciml.ai/DiffEqDocs/stable/solvers/dae_solve/). However, we can symbolically simplify the system first beforehand. Running `mtkcompile` eliminates unnecessary variables from the model to give the leanest numerical representation of the system. ```@example component -sys = structural_simplify(model) +sys = mtkcompile(model) equations(sys) ``` @@ -177,7 +177,7 @@ sol = solve(prob, Rosenbrock23()) plot(sol) ``` -What if we want the timeseries of a different variable? That information is not lost! Instead, `structural_simplify` simply changes unknown variables into `observed` variables. +What if we want the timeseries of a different variable? That information is not lost! Instead, `mtkcompile` simply changes unknown variables into `observed` variables. ```@example component observed(sys) diff --git a/docs/src/examples/tearing_parallelism.md b/docs/src/examples/tearing_parallelism.md index 9540e610bd..4ecc8d2a45 100644 --- a/docs/src/examples/tearing_parallelism.md +++ b/docs/src/examples/tearing_parallelism.md @@ -1,7 +1,7 @@ # Exposing More Parallelism By Tearing Algebraic Equations in ODESystems Sometimes it can be very non-trivial to parallelize a system. In this tutorial, -we will demonstrate how to make use of `structural_simplify` to expose more +we will demonstrate how to make use of `mtkcompile` to expose more parallelism in the solution process and parallelize the resulting simulation. ## The Component Library @@ -16,13 +16,13 @@ using ModelingToolkit: t_nounits as t, D_nounits as D # Basic electric components @connector function Pin(; name) @variables v(t)=1.0 i(t)=1.0 [connect = Flow] - ODESystem(Equation[], t, [v, i], [], name = name) + System(Equation[], t, [v, i], [], name = name) end function Ground(; name) @named g = Pin() eqs = [g.v ~ 0] - compose(ODESystem(eqs, t, [], [], name = name), g) + compose(System(eqs, t, [], [], name = name), g) end function ConstantVoltage(; name, V = 1.0) @@ -32,12 +32,12 @@ function ConstantVoltage(; name, V = 1.0) @parameters V = V eqs = [V ~ p.v - n.v 0 ~ p.i + n.i] - compose(ODESystem(eqs, t, [], [V], name = name), p, n) + compose(System(eqs, t, [], [V], name = name), p, n) end @connector function HeatPort(; name) @variables T(t)=293.15 Q_flow(t)=0.0 [connect = Flow] - ODESystem(Equation[], t, [T, Q_flow], [], name = name) + System(Equation[], t, [T, Q_flow], [], name = name) end function HeatingResistor(; name, R = 1.0, TAmbient = 293.15, alpha = 1.0) @@ -51,7 +51,7 @@ function HeatingResistor(; name, R = 1.0, TAmbient = 293.15, alpha = 1.0) h.Q_flow ~ -v * p.i # -LossPower v ~ p.v - n.v 0 ~ p.i + n.i] - compose(ODESystem(eqs, t, [v, RTherm], [R, TAmbient, alpha], + compose(System(eqs, t, [v, RTherm], [R, TAmbient, alpha], name = name), p, n, h) end @@ -62,7 +62,7 @@ function HeatCapacitor(; name, rho = 8050, V = 1, cp = 460, TAmbient = 293.15) eqs = [ D(h.T) ~ h.Q_flow / C ] - compose(ODESystem(eqs, t, [], [rho, V, cp], + compose(System(eqs, t, [], [rho, V, cp], name = name), h) end @@ -74,7 +74,7 @@ function Capacitor(; name, C = 1.0) eqs = [v ~ p.v - n.v 0 ~ p.i + n.i D(v) ~ p.i / C] - compose(ODESystem(eqs, t, [v], [C], + compose(System(eqs, t, [v], [C], name = name), p, n) end @@ -88,7 +88,7 @@ function parallel_rc_model(i; name, source, ground, R, C) connect(capacitor.n, source.n, ground.g) connect(resistor.h, heat_capacitor.h)] - compose(ODESystem(rc_eqs, t, name = Symbol(name, i)), + compose(System(rc_eqs, t, name = Symbol(name, i)), [resistor, capacitor, source, ground, heat_capacitor]) end ``` @@ -114,7 +114,7 @@ eqs = [ D(E) ~ sum(((i, sys),) -> getproperty(sys, Symbol(:resistor, i)).h.Q_flow, enumerate(rc_systems)) ] -@named _big_rc = ODESystem(eqs, t, [E], []) +@named _big_rc = System(eqs, t, [E], []) @named big_rc = compose(_big_rc, rc_systems) ``` @@ -122,7 +122,7 @@ Now let's say we want to expose a bit more parallelism via running tearing. How do we do that? ```@example tearing -sys = structural_simplify(big_rc) +sys = mtkcompile(big_rc) ``` Done, that's it. There's no more to it. @@ -175,5 +175,5 @@ so this is better than trying to do it by hand. After performing this, you can construct the `ODEProblem` and set `parallel_form` to use the exposed parallelism in multithreaded function -constructions, but this showcases why `structural_simplify` is so important +constructions, but this showcases why `mtkcompile` is so important to that process. From 554305c249b4b43002067feb1cb9e7a05c8dd2db Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 23 May 2025 15:28:52 +0530 Subject: [PATCH 1865/2176] docs: update `tutorials` docs to new syntax --- docs/src/tutorials/SampledData.md | 9 ++--- docs/src/tutorials/acausal_components.md | 6 +-- docs/src/tutorials/attractors.md | 6 +-- .../bifurcation_diagram_computation.md | 4 +- docs/src/tutorials/callable_params.md | 8 ++-- .../tutorials/change_independent_variable.md | 16 ++++---- docs/src/tutorials/discrete_system.md | 9 ++--- docs/src/tutorials/disturbance_modeling.md | 8 ++-- docs/src/tutorials/domain_connections.md | 38 +++++++++---------- docs/src/tutorials/fmi.md | 22 ++++++----- docs/src/tutorials/initialization.md | 32 ++++++++-------- docs/src/tutorials/linear_analysis.md | 6 ++- docs/src/tutorials/modelingtoolkitize.md | 2 +- docs/src/tutorials/nonlinear.md | 6 +-- docs/src/tutorials/ode_modeling.md | 32 ++++++++-------- docs/src/tutorials/optimization.md | 6 +-- .../tutorials/parameter_identifiability.md | 8 ++-- .../tutorials/programmatically_generating.md | 21 +++++----- docs/src/tutorials/stochastic_diffeq.md | 8 ++-- 19 files changed, 125 insertions(+), 122 deletions(-) diff --git a/docs/src/tutorials/SampledData.md b/docs/src/tutorials/SampledData.md index c700bae5c2..c2d6c9308d 100644 --- a/docs/src/tutorials/SampledData.md +++ b/docs/src/tutorials/SampledData.md @@ -155,10 +155,9 @@ k = ShiftIndex(clock) function plant(; name) @variables x(t)=1 u(t)=0 y(t)=0 - D = Differential(t) eqs = [D(x) ~ -x + u y ~ x] - ODESystem(eqs, t; name = name) + System(eqs, t; name = name) end function filt(; name) # Reference filter @@ -166,7 +165,7 @@ function filt(; name) # Reference filter a = 1 / exp(dt) eqs = [x(k) ~ a * x(k - 1) + (1 - a) * u(k) y ~ x] - ODESystem(eqs, t, name = name) + System(eqs, t, name = name) end function controller(kp; name) @@ -174,7 +173,7 @@ function controller(kp; name) @parameters kp = kp eqs = [yd ~ Sample(y) ud ~ kp * (r - yd)] - ODESystem(eqs, t; name = name) + System(eqs, t; name = name) end @named f = filt() @@ -187,7 +186,7 @@ connections = [r ~ sin(t) # reference signal Hold(c.ud) ~ p.u # controller output to plant input (zero-order-hold) p.y ~ c.y] # plant output to controller feedback -@named cl = ODESystem(connections, t, systems = [f, c, p]) +@named cl = System(connections, t, systems = [f, c, p]) ``` ```@docs diff --git a/docs/src/tutorials/acausal_components.md b/docs/src/tutorials/acausal_components.md index 751b678dae..b364be4012 100644 --- a/docs/src/tutorials/acausal_components.md +++ b/docs/src/tutorials/acausal_components.md @@ -99,7 +99,7 @@ end end end -@mtkbuild rc_model = RCModel(resistor.R = 2.0) +@mtkcompile rc_model = RCModel(resistor.R = 2.0) u0 = [ rc_model.capacitor.v => 0.0 ] @@ -272,7 +272,7 @@ We can create a RCModel component with `@named`. And using `subcomponent_name.pa the parameters or defaults values of variables of subcomponents. ```@example acausal -@mtkbuild rc_model = RCModel(resistor.R = 2.0) +@mtkcompile rc_model = RCModel(resistor.R = 2.0) ``` This model is acausal because we have not specified anything about the causality of the model. We have @@ -323,7 +323,7 @@ plot(sol) By default, this plots only the unknown variables that had to be solved for. However, what if we wanted to plot the timeseries of a different variable? Do not worry, that information was not thrown away! Instead, transformations -like `structural_simplify` simply change unknown variables into observables which are +like `mtkcompile` simply change unknown variables into observables which are defined by `observed` equations. ```@example acausal diff --git a/docs/src/tutorials/attractors.md b/docs/src/tutorials/attractors.md index 317384b01a..426551b017 100644 --- a/docs/src/tutorials/attractors.md +++ b/docs/src/tutorials/attractors.md @@ -41,10 +41,10 @@ eqs = [ Because our dynamical system is super simple, we will directly make an `ODESystem` and cast it in an `ODEProblem` as in the [`ODESystems` tutorial](@ref programmatically). Since all state variables and parameters have a default value we can immediately write ```@example Attractors -@named modlorenz = ODESystem(eqs, t) -ssys = structural_simplify(modlorenz) +@named modlorenz = System(eqs, t) +ssys = mtkcompile(modlorenz) # The timespan given to the problem is irrelevant for DynamicalSystems.jl -prob = ODEProblem(ssys, [], (0.0, 1.0), []) +prob = ODEProblem(ssys, [], (0.0, 1.0)) ``` This `prob` can be turned to a dynamical system as simply as diff --git a/docs/src/tutorials/bifurcation_diagram_computation.md b/docs/src/tutorials/bifurcation_diagram_computation.md index f15d46e1e4..811ea89e83 100644 --- a/docs/src/tutorials/bifurcation_diagram_computation.md +++ b/docs/src/tutorials/bifurcation_diagram_computation.md @@ -14,7 +14,7 @@ using ModelingToolkit: t_nounits as t, D_nounits as D @parameters μ α eqs = [0 ~ μ * x - x^3 + α * y, 0 ~ -y] -@mtkbuild nsys = NonlinearSystem(eqs, [x, y], [μ, α]) +@mtkcompile nsys = System(eqs, [x, y], [μ, α]) ``` we wish to compute a bifurcation diagram for this system as we vary the parameter `μ`. For this, we need to provide the following information: @@ -95,7 +95,7 @@ using ModelingToolkit: t_nounits as t, D_nounits as D @parameters μ eqs = [D(x) ~ μ * x - y - x * (x^2 + y^2), D(y) ~ x + μ * y - y * (x^2 + y^2)] -@mtkbuild osys = ODESystem(eqs, t) +@mtkcompile osys = System(eqs, t) bif_par = μ plot_var = x diff --git a/docs/src/tutorials/callable_params.md b/docs/src/tutorials/callable_params.md index 74e7ea87fa..2500c27015 100644 --- a/docs/src/tutorials/callable_params.md +++ b/docs/src/tutorials/callable_params.md @@ -2,7 +2,7 @@ ModelingToolkit.jl allows creating parameters that represent functions to be called. This is especially useful for including interpolants and/or lookup tables inside ODEs. In this -tutorial we will create an `ODESystem` which employs callable parameters to interpolate data +tutorial we will create an `System` which employs callable parameters to interpolate data inside an ODE and go over the various syntax options and their implications. ## Callable parameter syntax @@ -69,7 +69,7 @@ Tspline = typeof(spline) @variables x(t) @parameters (interp::Tspline)(..) -@mtkbuild sys = ODESystem(D(x) ~ interp(t), t) +@mtkcompile sys = System(D(x) ~ interp(t), t) ``` The derivative of `x` is obtained via an interpolation from DataInterpolations.jl. Note @@ -77,7 +77,7 @@ the parameter syntax. The `(..)` marks the parameter as callable. `(interp::Tspl indicates that the parameter is of type `Tspline`. ```@example callable -prob = ODEProblem(sys, [x => 0.0], (0.0, 1.0), [interp => spline]) +prob = ODEProblem(sys, [x => 0.0, interp => spline], (0.0, 1.0)) solve(prob) ``` @@ -85,7 +85,7 @@ Note that the the following will not work: ```julia ODEProblem( - sys; [x => 0.0], (0.0, 1.0), [interp => LinearInterpolation(0.0:0.1:1.0, 0.0:0.1:1.0)]) + sys; [x => 0.0, interp => LinearInterpolation(0.0:0.1:1.0, 0.0:0.1:1.0)], (0.0, 1.0)) ``` Since the type of the spline doesn't match. diff --git a/docs/src/tutorials/change_independent_variable.md b/docs/src/tutorials/change_independent_variable.md index d55639a669..b5b548a73b 100644 --- a/docs/src/tutorials/change_independent_variable.md +++ b/docs/src/tutorials/change_independent_variable.md @@ -24,8 +24,8 @@ using ModelingToolkit: t_nounits as t, D_nounits as D @parameters g=9.81 v # gravitational acceleration and horizontal velocity eqs = [D(D(y)) ~ -g, D(x) ~ v] initialization_eqs = [D(x) ~ D(y)] # 45° initial angle -M1 = ODESystem(eqs, t; initialization_eqs, name = :M) -M1s = structural_simplify(M1) +M1 = System(eqs, t; initialization_eqs, name = :M) +M1s = mtkcompile(M1) @assert length(equations(M1s)) == 3 # hide M1s # hide ``` @@ -44,7 +44,7 @@ This transformation is well-defined for any non-zero horizontal velocity $v$, so ```@example changeivar M2 = change_independent_variable(M1, x) -M2s = structural_simplify(M2; allow_symbolic = true) +M2s = mtkcompile(M2; allow_symbolic = true) # a sanity test on the 10 x/y variables that are accessible to the user # hide @assert allequal([x, M1s.x]) # hide @assert allequal([M2.x, M2s.x]) # hide @@ -67,7 +67,7 @@ It is straightforward to evolve the ODE for 10 meters and plot the resulting tra ```@example changeivar using OrdinaryDiffEq, Plots -prob = ODEProblem(M2s, [M2s.y => 0.0], [0.0, 10.0], [v => 8.0]) # throw 10 meters +prob = ODEProblem(M2s, [M2s.y => 0.0, v => 8.0], [0.0, 10.0]) # throw 10 meters sol = solve(prob, Tsit5()) plot(sol; idxs = M2.y) # must index by M2.y = y(x); not M1.y = y(t)! ``` @@ -88,16 +88,16 @@ In terms of conformal time $t$, they can be written a = GlobalScope(a) # global var needed by all species function species(w; kw...) eqs = [D(Ω) ~ -3(1 + w) * D(a) / a * Ω] - return ODESystem(eqs, t, [Ω], []; kw...) + return System(eqs, t, [Ω], []; kw...) end @named r = species(1 // 3) # radiation @named m = species(0) # matter @named Λ = species(-1) # dark energy / cosmological constant eqs = [Ω ~ r.Ω + m.Ω + Λ.Ω, D(a) ~ √(Ω) * a^2] initialization_eqs = [Λ.Ω + r.Ω + m.Ω ~ 1] -M1 = ODESystem(eqs, t, [Ω, a], []; initialization_eqs, name = :M) +M1 = System(eqs, t, [Ω, a], []; initialization_eqs, name = :M) M1 = compose(M1, r, m, Λ) -M1s = structural_simplify(M1) +M1s = mtkcompile(M1) ``` Of course, we can solve this ODE as it is: @@ -137,7 +137,7 @@ M3 = change_independent_variable(M2, b, [Da(b) ~ exp(-b), a ~ exp(b)]) We can now solve and plot the ODE in terms of $b$: ```@example changeivar -M3s = structural_simplify(M3; allow_symbolic = true) +M3s = mtkcompile(M3; allow_symbolic = true) prob = ODEProblem(M3s, [M3s.r.Ω => 5e-5, M3s.m.Ω => 0.3], (0, -15), []) sol = solve(prob, Tsit5()) @assert Symbol(sol.retcode) == :Success # surrounding text assumes the solution was successful # hide diff --git a/docs/src/tutorials/discrete_system.md b/docs/src/tutorials/discrete_system.md index 8f6828fde7..d070156076 100644 --- a/docs/src/tutorials/discrete_system.md +++ b/docs/src/tutorials/discrete_system.md @@ -1,7 +1,6 @@ # (Experimental) Modeling Discrete Systems -In this example, we will use the new [`DiscreteSystem`](@ref) API -to create an SIR model. +In this example, we will use the [`System`](@ref) API to create an SIR model. ```@example discrete using ModelingToolkit @@ -23,12 +22,12 @@ recovery = rate_to_proportion(γ * h, δt) * I(k - 1) eqs = [S(k) ~ S(k - 1) - infection * h, I(k) ~ I(k - 1) + infection - recovery, R(k) ~ R(k - 1) + recovery] -@mtkbuild sys = DiscreteSystem(eqs, t) +@mtkcompile sys = System(eqs, t) u0 = [S(k - 1) => 990.0, I(k - 1) => 10.0, R(k - 1) => 0.0] p = [β => 0.05, c => 10.0, γ => 0.25, δt => 0.1] tspan = (0.0, 100.0) -prob = DiscreteProblem(sys, u0, tspan, p) +prob = DiscreteProblem(sys, vcat(u0, p), tspan) sol = solve(prob, FunctionMap()) ``` @@ -39,7 +38,7 @@ the Fibonacci series: ```@example discrete @variables x(t) = 1.0 -@mtkbuild sys = DiscreteSystem([x ~ x(k - 1) + x(k - 2)], t) +@mtkcompile sys = System([x ~ x(k - 1) + x(k - 2)], t) ``` The "default value" here should be interpreted as the value of `x` at all past timesteps. diff --git a/docs/src/tutorials/disturbance_modeling.md b/docs/src/tutorials/disturbance_modeling.md index db8d926498..41c7bd86ee 100644 --- a/docs/src/tutorials/disturbance_modeling.md +++ b/docs/src/tutorials/disturbance_modeling.md @@ -67,7 +67,7 @@ This outer model, `ModelWithInputs`, contains two disturbance inputs, both of ty ```@example DISTURBANCE_MODELING @named model = ModelWithInputs() # Model with load disturbance -ssys = structural_simplify(model) +ssys = mtkcompile(model) prob = ODEProblem(ssys, [], (0.0, 6.0)) sol = solve(prob, Tsit5()) using Plots @@ -108,7 +108,7 @@ y &= g(x, p, t) \end{aligned} ``` -where ``x`` is the state, ``y`` are observed variables, ``p`` are parameters, and ``t`` is time. When using MTK, which variables constitute ``x`` and which are considered part of the output, ``y``, is up to the tool rather than the user, this choice is made by MTK during the call to `@mtkbuild` or the lower-level function `structural_simplify`. +where ``x`` is the state, ``y`` are observed variables, ``p`` are parameters, and ``t`` is time. When using MTK, which variables constitute ``x`` and which are considered part of the output, ``y``, is up to the tool rather than the user, this choice is made by MTK during the call to `@mtkcompile` or the lower-level function `mtkcompile`. If we further consider external inputs to the system, such as controlled input signals ``u`` and disturbance inputs ``w``, we can write the system as @@ -169,7 +169,7 @@ end We demonstrate that this model is complete and can be simulated: ```@example DISTURBANCE_MODELING -ssys = structural_simplify(model_with_disturbance) +ssys = mtkcompile(model_with_disturbance) prob = ODEProblem(ssys, [], (0.0, 10.0)) sol = solve(prob, Tsit5()) using Test @@ -191,7 +191,7 @@ g = ModelingToolkit.build_explicit_observed_function( io_sys, outputs; inputs) op = ModelingToolkit.inputs(io_sys) .=> 0 -x0, _ = ModelingToolkit.get_u0_p(io_sys, op, op) +x0 = ModelingToolkit.get_u0(io_sys, op) p = MTKParameters(io_sys, op) u = zeros(1) # Control input w = zeros(length(disturbance_inputs)) # Disturbance input diff --git a/docs/src/tutorials/domain_connections.md b/docs/src/tutorials/domain_connections.md index d6dc2d8781..f9cd3040d2 100644 --- a/docs/src/tutorials/domain_connections.md +++ b/docs/src/tutorials/domain_connections.md @@ -20,7 +20,7 @@ using ModelingToolkit: t_nounits as t, D_nounits as D dm(t), [connect = Flow] end - ODESystem(Equation[], t, vars, pars; name, defaults = [dm => 0]) + System(Equation[], t, vars, pars; name, defaults = [dm => 0]) end nothing #hide ``` @@ -47,7 +47,7 @@ The fluid medium setter for `HydralicPort` may be defined as `HydraulicFluid` wi dm ~ 0 ] - ODESystem(eqs, t, vars, pars; name, defaults = [dm => 0]) + System(eqs, t, vars, pars; name, defaults = [dm => 0]) end nothing #hide ``` @@ -63,7 +63,7 @@ Now, we can connect a `HydraulicFluid` component to any `HydraulicPort` connecto eqs = [port.p ~ p] - ODESystem(eqs, t, [], pars; name, systems) + System(eqs, t, [], pars; name, systems) end @component function FixedVolume(; vol, p_int, name) @@ -89,7 +89,7 @@ end rho ~ port.ρ * (1 + p / port.β) dm ~ drho * vol] - ODESystem(eqs, t, vars, pars; name, systems) + System(eqs, t, vars, pars; name, systems) end nothing #hide ``` @@ -97,7 +97,7 @@ nothing #hide When the system is defined we can generate a fluid component and connect it to the system. Here `fluid` is connected to the `src.port`, but it could also be connected to `vol.port`, any connection in the network is fine. ```@example domain -@component function System(; name) +@component function HydraulicSystem(; name) systems = @named begin src = FixedPressure(; p = 200e5) vol = FixedVolume(; vol = 0.1, p_int = 200e5) @@ -108,17 +108,17 @@ When the system is defined we can generate a fluid component and connect it to t eqs = [connect(fluid, src.port) connect(src.port, vol.port)] - ODESystem(eqs, t, [], []; systems, name) + System(eqs, t, [], []; systems, name) end -@named odesys = System() +@named odesys = HydraulicSystem() nothing #hide ``` -To see how the domain works, we can examine the set parameter values for each of the ports `src.port` and `vol.port`. First we assemble the system using `structural_simplify()` and then check the default value of `vol.port.ρ`, whichs points to the setter value `fluid₊ρ`. Likewise, `src.port.ρ`, will also point to the setter value `fluid₊ρ`. Therefore, there is now only 1 defined density value `fluid₊ρ` which sets the density for the connected network. +To see how the domain works, we can examine the set parameter values for each of the ports `src.port` and `vol.port`. First we assemble the system using `mtkcompile()` and then check the default value of `vol.port.ρ`, whichs points to the setter value `fluid₊ρ`. Likewise, `src.port.ρ`, will also point to the setter value `fluid₊ρ`. Therefore, there is now only 1 defined density value `fluid₊ρ` which sets the density for the connected network. ```@repl domain -sys = structural_simplify(odesys) +sys = mtkcompile(odesys) ModelingToolkit.defaults(sys)[odesys.vol.port.ρ] ``` @@ -151,7 +151,7 @@ If we have a more complicated system, for example a hydraulic actuator, with a s port_a.dm ~ +(port_a.ρ) * dx * area port_b.dm ~ -(port_b.ρ) * dx * area] - ODESystem(eqs, t, vars, pars; name, systems) + System(eqs, t, vars, pars; name, systems) end nothing #hide ``` @@ -174,14 +174,14 @@ A system with 2 different fluids is defined and connected to each separate domai connect(src_a.port, act.port_a) connect(src_b.port, act.port_b)] - ODESystem(eqs, t, [], []; systems, name) + System(eqs, t, [], []; systems, name) end @named actsys2 = ActuatorSystem2() nothing #hide ``` -After running `structural_simplify()` on `actsys2`, the defaults will show that `act.port_a.ρ` points to `fluid_a₊ρ` and `act.port_b.ρ` points to `fluid_b₊ρ`. This is a special case, in most cases a hydraulic system will have only 1 fluid, however this simple system has 2 separate domain networks. Therefore, we can connect a single fluid to both networks. This does not interfere with the mathematical equations of the system, since no unknown variables are connected. +After running `mtkcompile()` on `actsys2`, the defaults will show that `act.port_a.ρ` points to `fluid_a₊ρ` and `act.port_b.ρ` points to `fluid_b₊ρ`. This is a special case, in most cases a hydraulic system will have only 1 fluid, however this simple system has 2 separate domain networks. Therefore, we can connect a single fluid to both networks. This does not interfere with the mathematical equations of the system, since no unknown variables are connected. ```@example domain @component function ActuatorSystem1(; name) @@ -198,7 +198,7 @@ After running `structural_simplify()` on `actsys2`, the defaults will show that connect(src_a.port, act.port_a) connect(src_b.port, act.port_b)] - ODESystem(eqs, t, [], []; systems, name) + System(eqs, t, [], []; systems, name) end @named actsys1 = ActuatorSystem1() @@ -224,7 +224,7 @@ In some cases a component will be defined with 2 connectors of the same domain, eqs = [port_a.dm ~ (port_a.p - port_b.p) * K 0 ~ port_a.dm + port_b.dm] - ODESystem(eqs, t, [], pars; systems, name) + System(eqs, t, [], pars; systems, name) end nothing #hide ``` @@ -245,14 +245,14 @@ Adding the `Restrictor` to the original system example will cause a break in the connect(src.port, res.port_a) connect(res.port_b, vol.port)] - ODESystem(eqs, t, [], []; systems, name) + System(eqs, t, [], []; systems, name) end -@mtkbuild ressys = RestrictorSystem() +@mtkcompile ressys = RestrictorSystem() nothing #hide ``` -When `structural_simplify()` is applied to this system it can be seen that the defaults are missing for `res.port_b` and `vol.port`. +When `mtkcompile()` is applied to this system it can be seen that the defaults are missing for `res.port_b` and `vol.port`. ```@repl domain ModelingToolkit.defaults(ressys)[ressys.res.port_a.ρ] @@ -278,10 +278,10 @@ To ensure that the `Restrictor` component does not disrupt the domain network, t port_a.dm ~ (port_a.p - port_b.p) * K 0 ~ port_a.dm + port_b.dm] - ODESystem(eqs, t, [], pars; systems, name) + System(eqs, t, [], pars; systems, name) end -@mtkbuild ressys = RestrictorSystem() +@mtkcompile ressys = RestrictorSystem() nothing #hide ``` diff --git a/docs/src/tutorials/fmi.md b/docs/src/tutorials/fmi.md index 0e01393652..d17452f612 100644 --- a/docs/src/tutorials/fmi.md +++ b/docs/src/tutorials/fmi.md @@ -76,7 +76,7 @@ initialization semantics. We can simulate this model like any other ModelingToolkit system. ```@repl fmi -sys = structural_simplify(model) +sys = mtkcompile(model) prob = ODEProblem(sys, [sys.mass__s => 0.5, sys.mass__v => 0.0], (0.0, 5.0)) sol = solve(prob, Tsit5()) ``` @@ -105,11 +105,11 @@ constant until the next time the callback triggers. The periodic interval must b `communication_step_size` keyword argument. A smaller step size typically leads to less error but is more computationally expensive. -This model alone does not have any differential variables, and calling `structural_simplify` will lead +This model alone does not have any differential variables, and calling `mtkcompile` will lead to an `ODESystem` with no unknowns. ```@example fmi -structural_simplify(inner) +mtkcompile(inner) ``` Simulating this model will cause the OrdinaryDiffEq integrator to immediately finish, and will not @@ -117,7 +117,7 @@ trigger the callback. Thus, we wrap this system in a trivial system with a diffe ```@example fmi @variables x(t) = 1.0 -@mtkbuild sys = ODESystem([D(x) ~ x], t; systems = [inner]) +@mtkcompile sys = System([D(x) ~ x], t; systems = [inner]) ``` We can now simulate `sys`. @@ -184,7 +184,7 @@ metadata. We can now use this component as a subcomponent of a larger system. ```@repl fmi @variables a(t) b(t) c(t) [guess = 1.0]; -@mtkbuild sys = ODESystem( +@mtkcompile sys = System( [adder.a ~ a, adder.b ~ b, D(a) ~ t, D(b) ~ adder.out + adder.c, c^2 ~ adder.out + adder.value], t; @@ -198,8 +198,9 @@ FMUs in initialization to solve for initial states. As mentioned earlier, we can through an FMU. Thus, automatic differentiation has to be disabled for the solver. ```@example fmi -prob = ODEProblem(sys, [sys.adder.c => 2.0, sys.a => 1.0, sys.b => 1.0], - (0.0, 1.0), [sys.adder.value => 2.0]) +prob = ODEProblem( + sys, [sys.adder.c => 2.0, sys.a => 1.0, sys.b => 1.0, sys.adder.value => 2.0], + (0.0, 1.0)) solve(prob, Rodas5P(autodiff = false)) ``` @@ -217,12 +218,13 @@ fmu = loadFMU( @named adder = ModelingToolkit.FMIComponent( Val(2); fmu, type = :CS, communication_step_size = 1e-3, reinitializealg = BrownFullBasicInit()) -@mtkbuild sys = ODESystem( +@mtkcompile sys = System( [adder.a ~ a, adder.b ~ b, D(a) ~ t, D(b) ~ adder.out + adder.c, c^2 ~ adder.out + adder.value], t; systems = [adder]) -prob = ODEProblem(sys, [sys.adder.c => 2.0, sys.a => 1.0, sys.b => 1.0], - (0.0, 1.0), [sys.adder.value => 2.0]) +prob = ODEProblem( + sys, [sys.adder.c => 2.0, sys.a => 1.0, sys.b => 1.0, sys.adder.value => 2.0], + (0.0, 1.0)) solve(prob, Rodas5P()) ``` diff --git a/docs/src/tutorials/initialization.md b/docs/src/tutorials/initialization.md index ba733e0bfb..cdf6e7e813 100644 --- a/docs/src/tutorials/initialization.md +++ b/docs/src/tutorials/initialization.md @@ -44,7 +44,7 @@ using ModelingToolkit: t_nounits as t, D_nounits as D eqs = [D(D(x)) ~ λ * x D(D(y)) ~ λ * y - g x^2 + y^2 ~ 1] -@mtkbuild pend = ODESystem(eqs, t) +@mtkcompile pend = System(eqs, t) ``` While we defined the system using second derivatives and a length constraint, @@ -59,7 +59,7 @@ To see how the system works, let's start the pendulum in the far right position, i.e. `x(0) = 1` and `y(0) = 0`. We can do this by: ```@example init -prob = ODEProblem(pend, [x => 1, y => 0], (0.0, 1.5), [g => 1], guesses = [λ => 1]) +prob = ODEProblem(pend, [x => 1, y => 0, g => 1], (0.0, 1.5), guesses = [λ => 1]) ``` This solves via: @@ -93,7 +93,7 @@ sol[λ][1] We can similarly choose `λ = 0` and solve for `y` to start the system: ```@example init -prob = ODEProblem(pend, [x => 1, λ => 0], (0.0, 1.5), [g => 1], guesses = [y => 1]) +prob = ODEProblem(pend, [x => 1, λ => 0, g => 1], (0.0, 1.5); guesses = [y => 1]) sol = solve(prob, Rodas5P()) plot(sol, idxs = (x, y)) ``` @@ -102,7 +102,7 @@ or choose to satisfy derivative conditions: ```@example init prob = ODEProblem( - pend, [x => 1, D(y) => 0], (0.0, 1.5), [g => 1], guesses = [λ => 0, y => 1]) + pend, [x => 1, D(y) => 0, g => 1], (0.0, 1.5); guesses = [λ => 0, y => 1]) sol = solve(prob, Rodas5P()) plot(sol, idxs = (x, y)) ``` @@ -114,7 +114,7 @@ We can also directly give equations to be satisfied at the initial point by usin the `initialization_eqs` keyword argument, for example: ```@example init -prob = ODEProblem(pend, [x => 1], (0.0, 1.5), [g => 1], guesses = [λ => 0, y => 1], +prob = ODEProblem(pend, [x => 1, g => 1], (0.0, 1.5); guesses = [λ => 0, y => 1], initialization_eqs = [y ~ 0]) sol = solve(prob, Rodas5P()) plot(sol, idxs = (x, y)) @@ -125,7 +125,7 @@ variables and parameters: ```@example init prob = ODEProblem( - pend, [x => 1, D(y) => g], (0.0, 3.0), [g => 1], guesses = [λ => 0, y => 1]) + pend, [x => 1, D(y) => g, g => 1], (0.0, 3.0); guesses = [λ => 0, y => 1]) sol = solve(prob, Rodas5P()) plot(sol, idxs = (x, y)) ``` @@ -141,7 +141,7 @@ conditions = getfield.(equations(pend)[3:end], :rhs) when we initialize with ```@example init -prob = ODEProblem(pend, [x => 1, y => 0], (0.0, 1.5), [g => 1], guesses = [y => 0, λ => 1]) +prob = ODEProblem(pend, [x => 1, y => 0, g => 1], (0.0, 1.5); guesses = [y => 0, λ => 1]) ``` we have two extra conditions to satisfy, `x ~ 1` and `y ~ 0` at the initial point. That gives @@ -149,7 +149,7 @@ we have two extra conditions to satisfy, `x ~ 1` and `y ~ 0` at the initial poin case? ```@example init -prob = ODEProblem(pend, [x => 1], (0.0, 1.5), [g => 1], guesses = [y => 0, λ => 1]) +prob = ODEProblem(pend, [x => 1, g => 1], (0.0, 1.5); guesses = [y => 0, λ => 1]) ``` Here we have 4 equations for 5 unknowns (note: the warning is post-simplification of the @@ -167,7 +167,7 @@ direction, we may have an overdetermined system: ```@example init prob = ODEProblem( - pend, [x => 1, y => 0.0, D(y) => 0], (0.0, 1.5), [g => 1], guesses = [λ => 1]) + pend, [x => 1, y => 0.0, D(y) => 0, g => 1], (0.0, 1.5); guesses = [λ => 1]) ``` Can that be solved? @@ -184,7 +184,7 @@ a `SciMLBase.ReturnCode.InitialFailure`: ```@example init prob = ODEProblem( - pend, [x => 1, y => 0.0, D(y) => 2.0, λ => 1], (0.0, 1.5), [g => 1], guesses = [λ => 1]) + pend, [x => 1, y => 0.0, D(y) => 2.0, λ => 1, g => 1], (0.0, 1.5); guesses = [λ => 1]) sol = solve(prob, Rodas5P()) ``` @@ -217,7 +217,7 @@ y-velocities from a given position. ```@example init prob = ODEProblem( - pend, [x => 1, D(y) => 0], (0.0, 1.5), [g => 1], guesses = [λ => 0, y => 1, x => 1]) + pend, [x => 1, D(y) => 0, g => 1], (0.0, 1.5); guesses = [λ => 0, y => 1, x => 1]) sol1 = solve(prob, Rodas5P()) ``` @@ -312,7 +312,7 @@ using ModelingToolkit: t_nounits as t, D_nounits as D # hidden @variables x(t) y(t) @parameters total -@mtkbuild sys = ODESystem([D(x) ~ -x, total ~ x + y], t; +@mtkcompile sys = System([D(x) ~ -x, total ~ x + y], t; defaults = [total => missing], guesses = [total => 1.0]) ``` @@ -380,7 +380,7 @@ with observables, those observables are too treated as initial equations. We can resulting simplified system via the command: ```@example init -isys = structural_simplify(isys; fully_determined = false) +isys = mtkcompile(isys; fully_determined = false) ``` Note `fully_determined=false` allows for the simplification to occur when the number of equations @@ -392,7 +392,7 @@ isys = ModelingToolkit.generate_initializesystem( ``` ```@example init -isys = structural_simplify(isys; fully_determined = false) +isys = mtkcompile(isys; fully_determined = false) ``` ```@example init @@ -503,8 +503,8 @@ eqs = [D(x) ~ α * x - β * x * y D(y) ~ -γ * y + δ * x * y z ~ x + y] -@named sys = ODESystem(eqs, t) -simpsys = structural_simplify(sys) +@named sys = System(eqs, t) +simpsys = mtkcompile(sys) tspan = (0.0, 10.0) ``` diff --git a/docs/src/tutorials/linear_analysis.md b/docs/src/tutorials/linear_analysis.md index 7fcb67f9fb..250dbaa6a7 100644 --- a/docs/src/tutorials/linear_analysis.md +++ b/docs/src/tutorials/linear_analysis.md @@ -58,12 +58,14 @@ The following example builds a simple closed-loop system with a plant $P$ and a ```@example LINEAR_ANALYSIS using ModelingToolkitStandardLibrary.Blocks, ModelingToolkit +using ModelingToolkit: t_nounits as t + @named P = FirstOrder(k = 1, T = 1) # A first-order system with pole in -1 @named C = Gain(-1) # A P controller -t = ModelingToolkit.get_iv(P) + eqs = [connect(P.output, :plant_output, C.input) # Connect with an automatically created analysis point called :plant_output connect(C.output, :plant_input, P.input)] -sys = ODESystem(eqs, t, systems = [P, C], name = :feedback_system) +sys = System(eqs, t, systems = [P, C], name = :feedback_system) matrices_S = get_sensitivity(sys, :plant_input)[1] # Compute the matrices of a state-space representation of the (input)sensitivity function. matrices_T = get_comp_sensitivity(sys, :plant_input)[1] diff --git a/docs/src/tutorials/modelingtoolkitize.md b/docs/src/tutorials/modelingtoolkitize.md index 545879f842..fc74578ea4 100644 --- a/docs/src/tutorials/modelingtoolkitize.md +++ b/docs/src/tutorials/modelingtoolkitize.md @@ -52,7 +52,7 @@ If we want to get a symbolic representation, we can simply call `modelingtoolkit on the `prob`, which will return an `ODESystem`: ```@example mtkize -@mtkbuild sys = modelingtoolkitize(prob) +@mtkcompile sys = modelingtoolkitize(prob) ``` Using this, we can symbolically build the Jacobian and then rebuild the ODEProblem: diff --git a/docs/src/tutorials/nonlinear.md b/docs/src/tutorials/nonlinear.md index 057e856229..13caf96231 100644 --- a/docs/src/tutorials/nonlinear.md +++ b/docs/src/tutorials/nonlinear.md @@ -23,12 +23,12 @@ using ModelingToolkit, NonlinearSolve eqs = [0 ~ σ * (y - x) 0 ~ x * (ρ - z) - y 0 ~ x * y - β * z] -@mtkbuild ns = NonlinearSystem(eqs) +@mtkcompile ns = System(eqs) guesses = [x => 1.0, y => 0.0, z => 0.0] ps = [σ => 10.0, ρ => 26.0, β => 8 / 3] -prob = NonlinearProblem(ns, guesses, ps) +prob = NonlinearProblem(ns, vcat(guesses, ps)) sol = solve(prob, NewtonRaphson()) ``` @@ -38,6 +38,6 @@ Just like with `ODEProblem`s we can generate the `NonlinearProblem` with its ana Jacobian function: ```@example nonlinear -prob = NonlinearProblem(ns, guesses, ps, jac = true) +prob = NonlinearProblem(ns, vcat(guesses, ps), jac = true) sol = solve(prob, NewtonRaphson()) ``` diff --git a/docs/src/tutorials/ode_modeling.md b/docs/src/tutorials/ode_modeling.md index 0a4cd80803..3113ab0fc2 100644 --- a/docs/src/tutorials/ode_modeling.md +++ b/docs/src/tutorials/ode_modeling.md @@ -35,7 +35,7 @@ using ModelingToolkit: t_nounits as t, D_nounits as D end using OrdinaryDiffEq -@mtkbuild fol = FOL() +@mtkcompile fol = FOL() prob = ODEProblem(fol, [], (0.0, 10.0), []) sol = solve(prob) @@ -77,12 +77,12 @@ using ModelingToolkit: t_nounits as t, D_nounits as D end end -@mtkbuild fol = FOL() +@mtkcompile fol = FOL() ``` Note that equations in MTK use the tilde character (`~`) as equality sign. -`@mtkbuild` creates an instance of `FOL` named as `fol`. +`@mtkcompile` creates an instance of `FOL` named as `fol`. After construction of the ODE, you can solve it using [OrdinaryDiffEq.jl](https://docs.sciml.ai/DiffEqDocs/stable/): @@ -90,7 +90,7 @@ After construction of the ODE, you can solve it using [OrdinaryDiffEq.jl](https: using OrdinaryDiffEq using Plots -prob = ODEProblem(fol, [], (0.0, 10.0), []) +prob = ODEProblem(fol, [], (0.0, 10.0)) plot(solve(prob)) ``` @@ -105,15 +105,15 @@ you likely do not want to write an entirely new `@mtkmodel`. ModelingToolkit supports overwriting the default values: ```@example ode2 -@mtkbuild fol_different_values = FOL(; τ = 1 / 3, x = 0.5) -prob = ODEProblem(fol_different_values, [], (0.0, 10.0), []) +@mtkcompile fol_different_values = FOL(; τ = 1 / 3, x = 0.5) +prob = ODEProblem(fol_different_values, [], (0.0, 10.0)) plot(solve(prob)) ``` Alternatively, this overwriting could also have occurred at the `ODEProblem` level. ```@example ode2 -prob = ODEProblem(fol, [fol.τ => 1 / 3], (0.0, 10.0), [fol.x => 0.5]) +prob = ODEProblem(fol, [fol.x => 0.5, fol.τ => 1 / 3], (0.0, 10.0)) plot(solve(prob)) ``` @@ -147,7 +147,7 @@ using ModelingToolkit: t_nounits as t, D_nounits as D end end -@mtkbuild fol = FOL() +@mtkcompile fol = FOL() ``` If you copy this block of code to your REPL, you will not see the above LaTeX equations. @@ -175,7 +175,7 @@ in a simulation result. The intermediate variable `RHS` therefore can be plotted along with the unknown variable. Note that this has to be requested explicitly: ```@example ode2 -prob = ODEProblem(fol, [], (0.0, 10.0), []) +prob = ODEProblem(fol, [], (0.0, 10.0)) sol = solve(prob) plot(sol, idxs = [fol.x, fol.RHS]) ``` @@ -221,7 +221,7 @@ Obviously, one could use an explicit, symbolic function of time: end end -@mtkbuild fol_variable_f = FOL() +@mtkcompile fol_variable_f = FOL() ``` However, this function might not be available in an explicit form. @@ -252,11 +252,11 @@ f_fun(t) = t >= 10 ? value_vector[end] : value_vector[Int(floor(t)) + 1] end end -@mtkbuild fol_external_f = FOLExternalFunction() +@mtkcompile fol_external_f = FOLExternalFunction() ``` ```@example ode2 -prob = ODEProblem(fol_external_f, [], (0.0, 10.0), []) +prob = ODEProblem(fol_external_f, [], (0.0, 10.0)) sol = solve(prob) plot(sol, idxs = [fol_external_f.x, fol_external_f.f]) ``` @@ -292,7 +292,7 @@ end fol_2.f ~ fol_1.x end end -@mtkbuild connected = FOLConnected() +@mtkcompile connected = FOLConnected() ``` Here the total model consists of two of the same submodels (components), @@ -316,7 +316,7 @@ initial unknown and the parameter values can be specified accordingly when building the `ODEProblem`: ```@example ode2 -prob = ODEProblem(connected, [], (0.0, 10.0), []) +prob = ODEProblem(connected, [], (0.0, 10.0)) plot(solve(prob)) ``` @@ -344,13 +344,13 @@ Now have MTK provide sparse, analytical derivatives to the solver. This has to be specified during the construction of the `ODEProblem`: ```@example ode2 -prob_an = ODEProblem(connected, [], (0.0, 10.0), []; jac = true) +prob_an = ODEProblem(connected, [], (0.0, 10.0); jac = true) @btime solve(prob_an, Rodas4()); nothing # hide ``` ```@example ode2 -prob_sparse = ODEProblem(connected, [], (0.0, 10.0), []; jac = true, sparse = true) +prob_sparse = ODEProblem(connected, [], (0.0, 10.0); jac = true, sparse = true) @btime solve(prob_sparse, Rodas4()); nothing # hide ``` diff --git a/docs/src/tutorials/optimization.md b/docs/src/tutorials/optimization.md index 20f1079dcd..9eb72b36ea 100644 --- a/docs/src/tutorials/optimization.md +++ b/docs/src/tutorials/optimization.md @@ -24,7 +24,7 @@ using ModelingToolkit, Optimization, OptimizationOptimJL end @parameters a=1.0 b=1.0 rosenbrock = (a - x)^2 + b * (y - x^2)^2 -@mtkbuild sys = OptimizationSystem(rosenbrock, [x, y], [a, b]) +@mtkcompile sys = OptimizationSystem(rosenbrock, [x, y], [a, b]) ``` Every optimization problem consists of a set of optimization variables. @@ -52,7 +52,7 @@ ModelingToolkit is also capable of constructing analytical gradients and Hessian u0 = [y => 2.0] p = [b => 100.0] -prob = OptimizationProblem(sys, u0, p, grad = true, hess = true) +prob = OptimizationProblem(sys, vcat(u0, p), grad = true, hess = true) u_opt = solve(prob, GradientDescent()) ``` @@ -86,7 +86,7 @@ rosenbrock = (a - x)^2 + b * (y - x^2)^2 cons = [ x^2 + y^2 ≲ 1 ] -@mtkbuild sys = OptimizationSystem(rosenbrock, [x, y], [a, b], constraints = cons) +@mtkcompile sys = OptimizationSystem(rosenbrock, [x, y], [a, b], constraints = cons) prob = OptimizationProblem(sys, [], grad = true, hess = true, cons_j = true, cons_h = true) u_opt = solve(prob, IPNewton()) ``` diff --git a/docs/src/tutorials/parameter_identifiability.md b/docs/src/tutorials/parameter_identifiability.md index 0875a7698c..1731d5795f 100644 --- a/docs/src/tutorials/parameter_identifiability.md +++ b/docs/src/tutorials/parameter_identifiability.md @@ -4,7 +4,7 @@ Ordinary differential equations are commonly used for modeling real-world proces We will start by illustrating **local identifiability** in which a parameter is known up to _finitely many values_, and then proceed to determining **global identifiability**, that is, which parameters can be identified _uniquely_. -The package has a standalone data structure for ordinary differential equations, but is also compatible with `ODESystem` type from `ModelingToolkit.jl`. +The package has a standalone data structure for ordinary differential equations, but is also compatible with `System` type from `ModelingToolkit.jl`. ## Local Identifiability @@ -22,7 +22,7 @@ y_2 = x_5\end{cases}$$ This model describes the biohydrogenation[^1] process[^2] with unknown initial conditions. -### Using the `ODESystem` object +### Using the `System` object To define the ode system in Julia, we use `ModelingToolkit.jl`. @@ -61,7 +61,7 @@ using ModelingToolkit: t_nounits as t, D_nounits as D end # define the system -@mtkbuild de = Biohydrogenation() +@mtkcompile de = Biohydrogenation() ``` After that, we are ready to check the system for local identifiability: @@ -179,7 +179,7 @@ using ModelingToolkit: t_nounits as t, D_nounits as D end end -@mtkbuild ode = GoodwinOscillator() +@mtkcompile ode = GoodwinOscillator() # check only 2 parameters to_check = [ode.b, ode.c] diff --git a/docs/src/tutorials/programmatically_generating.md b/docs/src/tutorials/programmatically_generating.md index 9fc1db1834..406f65d8d9 100644 --- a/docs/src/tutorials/programmatically_generating.md +++ b/docs/src/tutorials/programmatically_generating.md @@ -18,7 +18,8 @@ as demonstrated in the Symbolics.jl documentation. This looks like: ```@example scripting using ModelingToolkit # reexports Symbolics -@variables t x(t) y(t) # Define variables +@independent_variables t +@variables x(t) y(t) # Define variables D = Differential(t) eqs = [D(x) ~ y D(y) ~ x] # Define an array of equations @@ -43,11 +44,11 @@ using ModelingToolkit: t_nounits as t, D_nounits as D eqs = [D(x) ~ (h - x) / τ] # create an array of equations # your first ODE, consisting of a single equation, indicated by ~ -@named model = ODESystem(eqs, t) +@named model = System(eqs, t) # Perform the standard transformations and mark the model complete # Note: Complete models cannot be subsystems of other models! -fol = structural_simplify(model) +fol = mtkcompile(model) prob = ODEProblem(fol, [], (0.0, 10.0), []) using OrdinaryDiffEq sol = solve(prob) @@ -56,22 +57,22 @@ using Plots plot(sol) ``` -As you can see, generating an ODESystem is as simple as creating an array of equations -and passing it to the `ODESystem` constructor. +As you can see, generating an `System` is as simple as creating an array of equations +and passing it to the `System` constructor. -`@named` automatically gives a name to the `ODESystem`, and is shorthand for +`@named` automatically gives a name to the `System`, and is shorthand for ```@example scripting -fol_model = ODESystem(eqs, t; name = :fol_model) # @named fol_model = ODESystem(eqs, t) +fol_model = System(eqs, t; name = :fol_model) # @named fol_model = ODESystem(eqs, t) ``` -Thus, if we had read a name from a file and wish to populate an `ODESystem` with said name, we could do: +Thus, if we had read a name from a file and wish to populate an `System` with said name, we could do: ```@example scripting namesym = :name_from_file -fol_model = ODESystem(eqs, t; name = namesym) +fol_model = System(eqs, t; name = namesym) ``` ## Warning About Mutation -Be advsied that it's never a good idea to mutate an `ODESystem`, or any `AbstractSystem`. +Be advsied that it's never a good idea to mutate an `System`, or any `AbstractSystem`. diff --git a/docs/src/tutorials/stochastic_diffeq.md b/docs/src/tutorials/stochastic_diffeq.md index 79c71a2e8d..9bc0086d35 100644 --- a/docs/src/tutorials/stochastic_diffeq.md +++ b/docs/src/tutorials/stochastic_diffeq.md @@ -53,7 +53,7 @@ eqs = [D(x) ~ σ * (y - x) + 0.3x * B, D(y) ~ x * (ρ - z) - y + 0.3y * B, D(z) ~ x * y - β * z + 0.3z * B] -@mtkbuild de = System(eqs, t) +@mtkcompile de = System(eqs, t) ``` Even though we did not explicitly use `SDESystem`, ModelingToolkit can still infer this from the equations. @@ -65,7 +65,7 @@ typeof(de) We continue by solving and plotting the SDE. ```@example SDE -prob = SDEProblem(de, [], (0.0, 100.0), []) +prob = SDEProblem(de, [], (0.0, 100.0)) sol = solve(prob, SRIW1()) plot(sol, idxs = [(1, 2, 3)]) ``` @@ -87,8 +87,8 @@ multiple `@brownian` variables have to be declared. eqs = [D(x) ~ σ * (y - x) + 0.3x * Bx, D(y) ~ x * (ρ - z) - y + 0.3y * By, D(z) ~ x * y - β * z + 0.3z * Bz] -@mtkbuild de = System(eqs, t) -prob = SDEProblem(de, [], (0.0, 100.0), []) +@mtkcompile de = System(eqs, t) +prob = SDEProblem(de, [], (0.0, 100.0)) sol = solve(prob, SRIW1()) plot(sol) ``` From 073403976e93a3d4bfc57ee309fdc4dce5d935a8 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 23 May 2025 15:30:16 +0530 Subject: [PATCH 1866/2176] docs: remove old system docs --- docs/src/systems/DiscreteSystem.md | 33 ---------- docs/src/systems/ImplicitDiscreteSystem.md | 33 ---------- docs/src/systems/JumpSystem.md | 33 ---------- docs/src/systems/NonlinearSystem.md | 57 ----------------- docs/src/systems/ODESystem.md | 74 ---------------------- docs/src/systems/OptimizationSystem.md | 40 ------------ docs/src/systems/SDESystem.md | 70 -------------------- 7 files changed, 340 deletions(-) delete mode 100644 docs/src/systems/DiscreteSystem.md delete mode 100644 docs/src/systems/ImplicitDiscreteSystem.md delete mode 100644 docs/src/systems/JumpSystem.md delete mode 100644 docs/src/systems/NonlinearSystem.md delete mode 100644 docs/src/systems/ODESystem.md delete mode 100644 docs/src/systems/OptimizationSystem.md delete mode 100644 docs/src/systems/SDESystem.md diff --git a/docs/src/systems/DiscreteSystem.md b/docs/src/systems/DiscreteSystem.md deleted file mode 100644 index 55a02e5714..0000000000 --- a/docs/src/systems/DiscreteSystem.md +++ /dev/null @@ -1,33 +0,0 @@ -# DiscreteSystem - -## System Constructors - -```@docs -DiscreteSystem -``` - -## Composition and Accessor Functions - - - `get_eqs(sys)` or `equations(sys)`: The equations that define the discrete system. - - `get_unknowns(sys)` or `unknowns(sys)`: The set of unknowns in the discrete system. - - `get_ps(sys)` or `parameters(sys)`: The parameters of the discrete system. - - `get_iv(sys)`: The independent variable of the discrete system - -## Transformations - -```@docs; canonical=false -structural_simplify -``` - -## Problem Constructors - -```@docs; canonical=false -DiscreteProblem(sys::DiscreteSystem, u0map, tspan) -DiscreteFunction(sys::DiscreteSystem, args...) -``` - -## Discrete Domain - -```@docs; canonical=false -Shift -``` diff --git a/docs/src/systems/ImplicitDiscreteSystem.md b/docs/src/systems/ImplicitDiscreteSystem.md deleted file mode 100644 index d687502b49..0000000000 --- a/docs/src/systems/ImplicitDiscreteSystem.md +++ /dev/null @@ -1,33 +0,0 @@ -# ImplicitDiscreteSystem - -## System Constructors - -```@docs -ImplicitDiscreteSystem -``` - -## Composition and Accessor Functions - - - `get_eqs(sys)` or `equations(sys)`: The equations that define the implicit discrete system. - - `get_unknowns(sys)` or `unknowns(sys)`: The set of unknowns in the implicit discrete system. - - `get_ps(sys)` or `parameters(sys)`: The parameters of the implicit discrete system. - - `get_iv(sys)`: The independent variable of the implicit discrete system - -## Transformations - -```@docs; canonical=false -structural_simplify -``` - -## Problem Constructors - -```@docs; canonical=false -ImplicitDiscreteProblem(sys::ImplicitDiscreteSystem, u0map, tspan) -ImplicitDiscreteFunction(sys::ImplicitDiscreteSystem, args...) -``` - -## Discrete Domain - -```@docs; canonical=false -Shift -``` diff --git a/docs/src/systems/JumpSystem.md b/docs/src/systems/JumpSystem.md deleted file mode 100644 index 5bd0d50602..0000000000 --- a/docs/src/systems/JumpSystem.md +++ /dev/null @@ -1,33 +0,0 @@ -# JumpSystem - -## System Constructors - -```@docs -JumpSystem -``` - -## Composition and Accessor Functions - - - `get_eqs(sys)` or `equations(sys)`: The equations that define the jump system. - - `get_unknowns(sys)` or `unknowns(sys)`: The set of unknowns in the jump system. - - `get_ps(sys)` or `parameters(sys)`: The parameters of the jump system. - - `get_iv(sys)`: The independent variable of the jump system. - - `discrete_events(sys)`: The set of discrete events in the jump system. - -## Transformations - -```@docs; canonical=false -structural_simplify -``` - -## Analyses - -## Problem Constructors - -```@docs; canonical=false -DiscreteProblem(sys::JumpSystem, u0map, tspan) -``` - -```@docs -JumpProblem(sys::JumpSystem, prob, aggregator) -``` diff --git a/docs/src/systems/NonlinearSystem.md b/docs/src/systems/NonlinearSystem.md deleted file mode 100644 index 06d587b1b9..0000000000 --- a/docs/src/systems/NonlinearSystem.md +++ /dev/null @@ -1,57 +0,0 @@ -# NonlinearSystem - -## System Constructors - -```@docs -NonlinearSystem -``` - -## Composition and Accessor Functions - - - `get_eqs(sys)` or `equations(sys)`: The equations that define the nonlinear system. - - `get_unknowns(sys)` or `unknowns(sys)`: The set of unknowns in the nonlinear system. - - `get_ps(sys)` or `parameters(sys)`: The parameters of the nonlinear system. - - `get_u0_p(sys, u0map, parammap)` Numeric arrays for the initial condition and parameters given `var => value` maps. - -## Transformations - -```@docs; canonical=false -structural_simplify -alias_elimination -tearing -``` - -## Analyses - -```@docs; canonical=false -ModelingToolkit.isaffine -ModelingToolkit.islinear -``` - -## Applicable Calculation and Generation Functions - -```@docs; canonical=false -calculate_jacobian -generate_jacobian -jacobian_sparsity -``` - -## Problem Constructors - -```@docs -NonlinearFunction(sys::ModelingToolkit.NonlinearSystem, args...) -NonlinearProblem(sys::ModelingToolkit.NonlinearSystem, args...) -``` - -## Torn Problem Constructors - -```@docs -BlockNonlinearProblem -``` - -## Expression Constructors - -```@docs -NonlinearFunctionExpr -NonlinearProblemExpr -``` diff --git a/docs/src/systems/ODESystem.md b/docs/src/systems/ODESystem.md deleted file mode 100644 index 24e2952fc5..0000000000 --- a/docs/src/systems/ODESystem.md +++ /dev/null @@ -1,74 +0,0 @@ -# ODESystem - -## System Constructors - -```@docs -ODESystem -``` - -## Composition and Accessor Functions - - - `get_eqs(sys)` or `equations(sys)`: The equations that define the ODE. - - `get_unknowns(sys)` or `unknowns(sys)`: The set of unknowns in the ODE. - - `get_ps(sys)` or `parameters(sys)`: The parameters of the ODE. - - `get_iv(sys)`: The independent variable of the ODE. - - `get_u0_p(sys, u0map, parammap)` Numeric arrays for the initial condition and parameters given `var => value` maps. - - `continuous_events(sys)`: The set of continuous events in the ODE. - - `discrete_events(sys)`: The set of discrete events in the ODE. - - `alg_equations(sys)`: The algebraic equations (i.e. that does not contain a differential) that defines the ODE. - - `get_alg_eqs(sys)`: The algebraic equations (i.e. that does not contain a differential) that defines the ODE. Only returns equations of the current-level system. - - `diff_equations(sys)`: The differential equations (i.e. that contain a differential) that defines the ODE. - - `get_diff_eqs(sys)`: The differential equations (i.e. that contain a differential) that defines the ODE. Only returns equations of the current-level system. - - `has_alg_equations(sys)`: Returns `true` if the ODE contains any algebraic equations (i.e. that does not contain a differential). - - `has_alg_eqs(sys)`: Returns `true` if the ODE contains any algebraic equations (i.e. that does not contain a differential). Only considers the current-level system. - - `has_diff_equations(sys)`: Returns `true` if the ODE contains any differential equations (i.e. that does contain a differential). - - `has_diff_eqs(sys)`: Returns `true` if the ODE contains any differential equations (i.e. that does contain a differential). Only considers the current-level system. - -## Transformations - -```@docs -structural_simplify -ode_order_lowering -dae_index_lowering -change_independent_variable -liouville_transform -alias_elimination -tearing -``` - -## Analyses - -```@docs -ModelingToolkit.islinear -ModelingToolkit.isautonomous -ModelingToolkit.isaffine -``` - -## Applicable Calculation and Generation Functions - -```@docs; canonical=false -calculate_jacobian -calculate_tgrad -calculate_factorized_W -generate_jacobian -generate_tgrad -generate_factorized_W -jacobian_sparsity -``` - -## Standard Problem Constructors - -```@docs -ODEFunction(sys::ModelingToolkit.AbstractODESystem, args...) -ODEProblem(sys::ModelingToolkit.AbstractODESystem, args...) -SteadyStateProblem(sys::ModelingToolkit.AbstractODESystem, args...) -DAEProblem(sys::ModelingToolkit.AbstractODESystem, args...) -``` - -## Expression Constructors - -```@docs -ODEFunctionExpr -DAEFunctionExpr -SteadyStateProblemExpr -``` diff --git a/docs/src/systems/OptimizationSystem.md b/docs/src/systems/OptimizationSystem.md deleted file mode 100644 index bcc8b21de7..0000000000 --- a/docs/src/systems/OptimizationSystem.md +++ /dev/null @@ -1,40 +0,0 @@ -# OptimizationSystem - -## System Constructors - -```@docs -OptimizationSystem -``` - -## Composition and Accessor Functions - - - `get_op(sys)`: The objective to be minimized. - - `get_unknowns(sys)` or `unknowns(sys)`: The set of unknowns for the optimization. - - `get_ps(sys)` or `parameters(sys)`: The parameters for the optimization. - - `get_constraints(sys)` or `constraints(sys)`: The constraints for the optimization. - -## Transformations - -## Analyses - -## Applicable Calculation and Generation Functions - -```julia -calculate_gradient -calculate_hessian -generate_gradient -generate_hessian -hessian_sparsity -``` - -## Problem Constructors - -```@docs -OptimizationProblem(sys::ModelingToolkit.OptimizationSystem, args...) -``` - -## Expression Constructors - -```@docs -OptimizationProblemExpr -``` diff --git a/docs/src/systems/SDESystem.md b/docs/src/systems/SDESystem.md deleted file mode 100644 index 5789d2d9cb..0000000000 --- a/docs/src/systems/SDESystem.md +++ /dev/null @@ -1,70 +0,0 @@ -# SDESystem - -## System Constructors - -```@docs -SDESystem -``` - -To convert an `ODESystem` to an `SDESystem` directly: - -``` -ode = ODESystem(eqs,t,[x,y,z],[σ,ρ,β]) -sde = SDESystem(ode, noiseeqs) -``` - -## Composition and Accessor Functions - - - `get_eqs(sys)` or `equations(sys)`: The equations that define the SDE. - - `get_unknowns(sys)` or `unknowns(sys)`: The set of unknowns in the SDE. - - `get_ps(sys)` or `parameters(sys)`: The parameters of the SDE. - - `get_iv(sys)`: The independent variable of the SDE. - - `continuous_events(sys)`: The set of continuous events in the SDE. - - `discrete_events(sys)`: The set of discrete events in the SDE. - - `alg_equations(sys)`: The algebraic equations (i.e. that does not contain a differential) that defines the ODE. - - `get_alg_eqs(sys)`: The algebraic equations (i.e. that does not contain a differential) that defines the ODE. Only returns equations of the current-level system. - - `diff_equations(sys)`: The differential equations (i.e. that contain a differential) that defines the ODE. - - `get_diff_eqs(sys)`: The differential equations (i.e. that contain a differential) that defines the ODE. Only returns equations of the current-level system. - - `has_alg_equations(sys)`: Returns `true` if the ODE contains any algebraic equations (i.e. that does not contain a differential). - - `has_alg_eqs(sys)`: Returns `true` if the ODE contains any algebraic equations (i.e. that does not contain a differential). Only considers the current-level system. - - `has_diff_equations(sys)`: Returns `true` if the ODE contains any differential equations (i.e. that does contain a differential). - - `has_diff_eqs(sys)`: Returns `true` if the ODE contains any differential equations (i.e. that does contain a differential). Only considers the current-level system. - -## Transformations - -```@docs; canonical=false -structural_simplify -alias_elimination -``` - -```@docs -ModelingToolkit.Girsanov_transform -``` - -## Analyses - -## Applicable Calculation and Generation Functions - -```@docs; canonical=false -calculate_jacobian -calculate_tgrad -calculate_factorized_W -generate_jacobian -generate_tgrad -generate_factorized_W -jacobian_sparsity -``` - -## Problem Constructors - -```@docs -SDEFunction(sys::ModelingToolkit.SDESystem, args...) -SDEProblem(sys::ModelingToolkit.SDESystem, args...) -``` - -## Expression Constructors - -```@docs -SDEFunctionExpr -SDEProblemExpr -``` From e61981103e91c33a5af6d71343560ba90d811bd6 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 23 May 2025 15:30:37 +0530 Subject: [PATCH 1867/2176] docs: update more docs to new syntax --- docs/src/comparison.md | 4 ++-- docs/src/internals.md | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/src/comparison.md b/docs/src/comparison.md index 52d5ab2f70..6691b0023a 100644 --- a/docs/src/comparison.md +++ b/docs/src/comparison.md @@ -12,7 +12,7 @@ - All current Modelica compiler implementations are fixed and not extendable by the users from the Modelica language itself. For example, the Dymola compiler [shares its symbolic processing pipeline](https://www.claytex.com/tech-blog/model-translation-and-symbolic-manipulation/), - which is roughly equivalent to the `dae_index_lowering` and `structural_simplify` + which is roughly equivalent to the `dae_index_lowering` and `mtkcompile` of ModelingToolkit.jl. ModelingToolkit.jl is an open and hackable transformation system which allows users to add new non-standard transformations and control the order of application. @@ -90,7 +90,7 @@ [Dymola symbolic processing pipeline](https://www.claytex.com/tech-blog/model-translation-and-symbolic-manipulation/) with some improvements. ModelingToolkit.jl has an open transformation pipeline that allows for users to extend and reorder transformation passes, where - `structural_simplify` is an adaptation of the Modia.jl-improved alias elimination + `mtkcompile` is an adaptation of the Modia.jl-improved alias elimination and tearing algorithms. - Both Modia and ModelingToolkit generate `DAEProblem` and `ODEProblem` forms for solving with [DifferentialEquations.jl](https://docs.sciml.ai/DiffEqDocs/stable/). diff --git a/docs/src/internals.md b/docs/src/internals.md index 00b29f1a64..ed83192f21 100644 --- a/docs/src/internals.md +++ b/docs/src/internals.md @@ -18,7 +18,7 @@ and are then used to generate the `observed` equation found in the variable when necessary. In this sense, there is an equivalence between observables and the variable elimination system. -The procedure for variable elimination inside [`structural_simplify`](@ref) is +The procedure for variable elimination inside [`mtkcompile`](@ref) is 1. [`ModelingToolkit.initialize_system_structure`](@ref). 2. [`ModelingToolkit.alias_elimination`](@ref). This step moves equations into `observed(sys)`. From b4335a7191e4a375abda92d8eeacc1c8624021f1 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 23 May 2025 15:30:53 +0530 Subject: [PATCH 1868/2176] docs: move `PDESystem` docs --- docs/src/{systems => API}/PDESystem.md | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename docs/src/{systems => API}/PDESystem.md (100%) diff --git a/docs/src/systems/PDESystem.md b/docs/src/API/PDESystem.md similarity index 100% rename from docs/src/systems/PDESystem.md rename to docs/src/API/PDESystem.md From 55ea020116bbdcb3e7df405056e73e2af2c12b6f Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 23 May 2025 15:31:03 +0530 Subject: [PATCH 1869/2176] feat: add new API docs --- docs/src/API/System.md | 174 +++++++++++++++++++++++ docs/src/API/model_building.md | 253 +++++++++++++++++++++++++++++++++ docs/src/API/problems.md | 121 ++++++++++++++++ 3 files changed, 548 insertions(+) create mode 100644 docs/src/API/System.md create mode 100644 docs/src/API/model_building.md create mode 100644 docs/src/API/problems.md diff --git a/docs/src/API/System.md b/docs/src/API/System.md new file mode 100644 index 0000000000..33a4d064ab --- /dev/null +++ b/docs/src/API/System.md @@ -0,0 +1,174 @@ +# [The `System` type](@id System_type) + +ModelingToolkit.jl uses `System` to symbolically represent all types of numerical problems. +Users create `System`s representing the problem they want to solve and `mtkcompile` transforms +them into a format ModelingToolkit.jl can generate code for (alongside performing other +optimizations). + +```@docs +System +``` + +## Utility constructors + +Several utility constructors also exist to easily construct alternative system formulations. + +```@docs +NonlinearSystem +SDESystem +JumpSystem +OptimizationSystem +``` + +## Accessor functions + +Several accessor functions exist to query systems for the information they contain. In general, +for every field `x` there exists a `has_x` function which checks if the system contains the +field and a `get_x` function for obtaining the value in the field. Note that fields of a system +cannot be accessed via `getproperty` - that is reserved for accessing variables, subsystems +or analysis points of the hierarchical system. + +```@docs +ModelingToolkit.has_eqs +ModelingToolkit.get_eqs +equations +equations_toplevel +full_equations +ModelingToolkit.has_noise_eqs +ModelingToolkit.get_noise_eqs +ModelingToolkit.has_jumps +ModelingToolkit.get_jumps +jumps +ModelingToolkit.has_constraints +ModelingToolkit.get_constraints +constraints +ModelingToolkit.has_costs +ModelingToolkit.get_costs +costs +ModelingToolkit.has_consolidate +ModelingToolkit.get_consolidate +ModelingToolkit.has_unknowns +ModelingToolkit.get_unknowns +unknowns +unknowns_toplevel +ModelingToolkit.has_ps +ModelingToolkit.get_ps +parameters +parameters_toplevel +tunable_parameters +ModelingToolkit.has_brownians +ModelingToolkit.get_brownians +brownians +ModelingToolkit.has_iv +ModelingToolkit.get_iv +ModelingToolkit.has_observed +ModelingToolkit.get_observed +observed +observables +ModelingToolkit.has_name +ModelingToolkit.get_name +nameof +ModelingToolkit.has_description +ModelingToolkit.get_description +ModelingToolkit.description +ModelingToolkit.has_defaults +ModelingToolkit.get_defaults +defaults +ModelingToolkit.has_guesses +ModelingToolkit.get_guesses +guesses +ModelingToolkit.has_initialization_eqs +ModelingToolkit.get_initialization_eqs +initialization_equations +ModelingToolkit.has_continuous_events +ModelingToolkit.get_continuous_events +continuous_events +continuous_events_toplevel +ModelingToolkit.has_discrete_events +ModelingToolkit.get_discrete_events +discrete_events_toplevel +ModelingToolkit.has_assertions +ModelingToolkit.get_assertions +assertions +ModelingToolkit.has_metadata +ModelingToolkit.get_metadata +SymbolicUtils.getmetadata(::ModelingToolkit.AbstractSystem, ::DataType, ::Any) +SymbolicUtils.setmetadata(::ModelingToolkit.AbstractSystem, ::DataType, ::Any) +ModelingToolkit.has_is_dde +ModelingToolkit.get_is_dde +ModelingToolkit.is_dde +ModelingToolkit.has_tstops +ModelingToolkit.get_tstops +ModelingToolkit.symbolic_tstops +ModelingToolkit.has_tearing_state +ModelingToolkit.get_tearing_state +ModelingToolkit.does_namespacing +toggle_namespacing +ModelingToolkit.iscomplete +ModelingToolkit.has_preface +ModelingToolkit.get_preface +ModelingToolkit.preface +ModelingToolkit.has_parent +ModelingToolkit.get_parent +ModelingToolkit.has_initializesystem +ModelingToolkit.get_initializesystem +ModelingToolkit.is_initializesystem +``` + +## `getproperty` syntax + +ModelingToolkit allows obtaining in a system using `getproperty`. For a system `sys` with a +subcomponent `inner` containing variable `var`, `sys.inner.var` will obtain the appropriately +namespaced version of `var`. Note that this can also be used to access subsystems (`sys.inner`) +or analysis points. + +!!! note + + By default, top-level systems not marked as `complete` will apply their namespace. Systems + marked as `complete` will not do this namespacing. This namespacing behavior can be toggled + independently of whether the system is completed using [`toggle_namespacing`](@ref) and the + current namespacing behavior can be queried via [`ModelingToolkit.does_namespacing`](@ref). + +```@docs +Base.getproperty(::ModelingToolkit.AbstractSystem, ::Symbol) +``` + +## Functions for querying system equations + +```@docs +has_diff_eqs +has_alg_eqs +get_diff_eqs +get_alg_eqs +has_diff_equations +has_alg_equations +diff_equations +alg_equations +is_alg_equation +is_diff_equation +``` + +## String parsing + +ModelingToolkit can parse system variables from strings. + +```@docs +ModelingToolkit.parse_variable +``` + +## Dumping system data + +```@docs +ModelingToolkit.dump_unknowns +ModelingToolkit.dump_parameters +``` + +```@docs; canonical = false +ModelingToolkit.dump_variable_metadata +``` + +## Debugging utilities + +```@docs +debug_system +``` diff --git a/docs/src/API/model_building.md b/docs/src/API/model_building.md new file mode 100644 index 0000000000..40072bc2c4 --- /dev/null +++ b/docs/src/API/model_building.md @@ -0,0 +1,253 @@ +# [Model building reference](@id model_building_api) + +This page lists functionality and utilities related to building hierarchical models. It is +recommended to read the page on the [`System`](@ref System_type) before this. + +## Hierarchical model composition + +The `System` data structure can represent a tree-like hierarchy of systems for building models +from composable blocks. The [`ModelingToolkit.get_systems`](@ref) function can be used for +querying the subsystems of a system. The `@component` macro should be used when writing +building blocks for model composition. + +```@docs +@component +``` + +Every constructor function should build either a component or a connector. Components define +the dynamics of the system. Connectors are used to connect components together and propagate +information between them. See also [`@connector`](@ref). + +### Scoping of variables + +When building hierarchical systems, is is often necessary to pass variables from a parent system +to the subsystems. If done naively, this will result in the child system assuming it "owns" the +variables passed to it and any occurrences of those variables in the child system will be +namespaced. To prevent this, ModelingToolkit has the concept of variable scope. The scope allows +specifying which system a variable belongs to relative to the system in which it is used. + +```@docs +LocalScope +ParentScope +GlobalScope +``` + +Note that the scopes must be applied to _individual variables_ and not expressions. For example, +`ParentScope(x + y)` is incorrect. Instead, `ParentScope(x) + ParentScope(y)` is the correct usage. +Applying the same scope (more generally, the same function) to all variables in an expression is a +common task, and ModelingToolkit exposes a utility for the same: + +```@docs +ModelingToolkit.apply_to_variables +``` + +It is still tedious to manually use `apply_to_variables` on any symbolic expression passed to a +subsystem. The `@named` macro automatically wraps all symbolic arguments in `ParentScope` and +uses the identifier being assigned as the name of the system. + +```@docs +@named +``` + +### Exploring the tree structure + +The `System` type implements the `AbstractTrees` interface. This can be used to explore the +hierarchical structure. + +```@docs +hierarchy +``` + +### [Connection semantics](@id connect_semantics) + +ModelingToolkit implements connection semantics similar to those in the [Modelica specification](https://specification.modelica.org/maint/3.6/connectors-and-connections.html). +We do not support the concept of `inner` and `outer` elements or `expandable` connectors. +Connectors in ModelingToolkit are systems with the appropriate metadata added via the `@connector` +macro. + +```@docs +connect +domain_connect +@connector +``` + +Connections can be expanded using `expand_connections`. + +```@docs +expand_connections +``` + +Similar to the `stream` and `flow` keyword arguments in the specification, ModelingToolkit +allows specifying how variables in a connector behave in a connection. + +```@docs +Equality +Flow +Stream +``` + +These are specified using the `connect` metadata. ModelingToolkit also supports `instream`. +Refer to the Modelica specification on [Stream connectors](https://specification.modelica.org/maint/3.6/stream-connectors.html) +for more information. + +```@docs +instream +``` + +### System composition utilities + +```@docs +extend +compose +substitute_component +``` + +### Flattening systems + +The hierarchical structure can be flattened. This operation is performed during simplification. + +```@docs +flatten +``` + +## System simplification + +`System`s can be simplified to reformulate them in a way that enables it to be solved numerically, +and also perform other optimizations. This is done via the `mtkcompile` function. Connection expansion +and flattening are preprocessing steps of simplification. + +```@docs +mtkcompile +@mtkcompile +``` + +It is also possible (though not always advisable) to build numerical problems from systems without +passing them through `mtkcompile`. To do this, the system must first be marked as "complete" via +the `complete` function. This process is used to indicate that a system will not be modified +further and allows ModelingToolkit to perform any necessary preprocessing to it. `mtkcompile` +calls `complete` internally. + +```@docs +complete +``` + +### Exploring the results of simplification + +Similar to how [`full_equations`](@ref) returns the equations of a system with all variables +eliminated during `mtkcompile` substituted, we can perform this substitution on an arbitrary +expression. + +```@docs +ModelingToolkit.substitute_observed +ModelingToolkit.empty_substitutions +ModelingToolkit.get_substitutions +``` + +### Experimental simplification + +ModelingToolkit may have a variety of experimental simplification passes. These are not +enabled by default, but can be used by passing to the `additional_passes` keyword argument +of `mtkcompile`. + +```@docs +ModelingToolkit.IfLifting +``` + +## Event handling + +Time-dependent systems may have several events. These are used to trigger discontinuities +in the model. They compile to standard callbacks from `DiffEqCallbacks.jl`. + +```@docs +ModelingToolkit.SymbolicContinuousCallback +ModelingToolkit.SymbolicDiscreteCallback +``` + +The affect functions for the above callbacks can be symbolic or user-defined functions. +Symbolic affects are handled using equations as described in the [Events](@ref events) +section of the documentation. User-defined functions can be used via `ImperativeAffect`. + +```@docs +ModelingToolkit.AffectSystem +ModelingToolkit.ImperativeAffect +``` + +## Modelingtoolkitize + +ModelingToolkit can take some numerical problems created non-symbolically and build a +symbolic representation from them. + +```@docs +modelingtoolkitize +``` + +## Using FMUs + +ModelingToolkit is capable of importing FMUs as black-box symbolic models. Currently only +a subset of FMU features are supported. This functionality requires importing `FMI.jl`. + +```@docs +ModelingToolkit.FMIComponent +``` + +## Model transformations + +ModelingToolkit exposes a variety of transformations that can be applied to models to aid in +symbolic analysis. + +```@docs +liouville_transform +stochastic_integral_transform +Girsanov_transform +change_independent_variable +add_accumulations +noise_to_brownians +convert_system_indepvar +``` + +## Hybrid systems + +Hybrid systems are dynamical systems involving one or more discrete-time subsystems. These +discrete time systems follow clock semantics - they are synchronous systems and the relevant +variables are only defined at points where the clock ticks. + +While ModelingToolkit is unable to simplify, compile and solve such systems on its own, it +has the ability to represent them. Compilation strategies can be implemented independently +on top of [`mtkcompile`](@ref) using the `additional_passes` functionality. + +!!! warn + + These operators are considered experimental API. + +```@docs; canonical = false +Sample +Hold +SampleTime +``` + +ModelingToolkit uses the clock definition in SciMLBase + +```@docs +SciMLBase.TimeDomain +SciMLBase.Clock +SciMLBase.SolverStepClock +SciMLBase.Continuous +``` + +### State machines + +While ModelingToolkit has the capability to represent state machines, it lacks the ability +to compile and simulate them. + +!!! warn + + This functionality is considered experimental API + +```@docs +initial_state +transition +activeState +entry +ticksInState +timeInState +``` diff --git a/docs/src/API/problems.md b/docs/src/API/problems.md new file mode 100644 index 0000000000..5549224a09 --- /dev/null +++ b/docs/src/API/problems.md @@ -0,0 +1,121 @@ +# Building and solving numerical problems + +Systems are numerically solved by building and solving the appropriate problem type. +Numerical solvers expect to receive functions taking a predefeined set of arguments +and returning specific values. This format of argument and return value depends on +the function and the problem. ModelingToolkit is capable of compiling and generating +code for a variety of such numerical problems. + +## Dynamical systems + +```@docs +ODEFunction(::System, args...) +ODEProblem(::System, args...) +DAEFunction(::System, args...) +DAEProblem(::System, args...) +SDEFunction(::System, args...) +SDEProblem(::System, args...) +DDEFunction(::System, args...) +DDEProblem(::System, args...) +SDDEFunction(::System, args...) +SDDEProblem(::System, args...) +JumpProblem(::System, args...) +BVProblem(::System, args...) +DiscreteProblem(::System, args...) +ImplicitDiscreteProblem(::System, args...) +``` + +## Nonlinear systems + +```@docs +NonlinearFunction(::System, args...) +NonlinearProblem(::System, args...) +SCCNonlinearProblem(::System, args...) +NonlinearLeastSquaresProblem(::System, args...) +SteadyStateProblem(::System, args...) +IntervalNonlinearFunction(::System, args...) +IntervalNonlinearProblem(::System, args...) +ModelingToolkit.HomotopyContinuationProblem +HomotopyNonlinearFunction(::System, args...) +``` + +## Optimization and optimal control + +```@docs +OptimizationFunction(::System, args...) +OptimizationProblem(::System, args...) +ODEInputFunction(::System, args...) +JuMPDynamicOptProblem(::System, args...) +InfiniteOptDynamicOptProblem,(::System, args...) +CasADiDynamicOptProblem(::System, args...) +DynamicOptSolution +``` + +## The state vector and parameter object + +Typically the unknowns of the system are present as a `Vector` of the appropriate length +in the numerical problem. The state vector can also be constructed manually without building +a problem. + +```@docs +ModelingToolkit.get_u0 +ModelingToolkit.varmap_to_vars +``` + +By default, the parameters of the system are stored in a custom data structure called +`MTKParameters`. The internals of this data structure are undocumented, and it should +only be interacted with through defined public API. SymbolicIndexingInterface.jl contains +functionality useful for this purpose. + +```@docs +MTKParameters +ModelingToolkit.get_p +``` + +The following functions are useful when working with `MTKParameters` objects, and especially +the `Tunables` portion. For more information about the "portions" of `MTKParameters`, refer +to the [`SciMLStructures.jl`](https://docs.sciml.ai/SciMLStructures/stable/) documentation. + +```@docs +reorder_dimension_by_tunables! +reorder_dimension_by_tunables +``` + +## Initialization + +```@docs +generate_initializesystem +InitializationProblem +``` + +## Linear analysis + +```@docs +linearization_function +LinearizationProblem +linearize +CommonSolve.solve(::LinearizationProblem) +linearize_symbolic +``` + +There are also utilities for manipulating the results of these analyses in a symbolic context. + +```@docs +ModelingToolkit.similarity_transform +ModelingToolkit.reorder_unknnowns +``` + +### Analysis point transformations + +Linear analysis can also be done using analysis points to perform several common +workflows. + +```@docs +get_sensitivity_function +get_sensitivity +get_comp_sensitivity_function +get_comp_sensitivity +get_looptransfer_function +get_looptransfer +open_loop +``` From b84017c7835fb308ad0d06e7a74badc6d5c37f91 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 23 May 2025 15:31:28 +0530 Subject: [PATCH 1870/2176] docs: document `getproperty(::AbstractSystem, ::Symbol)` --- src/systems/abstractsystem.jl | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index 41cc96bf57..b997219eba 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -955,6 +955,14 @@ function Base.propertynames(sys::AbstractSystem; private = false) end end +""" + Base.getproperty(sys::AbstractSystem, name::Symbol) + +Access the subsystem, variable or analysis point of `sys` named `name`. To check if `sys` +will namespace the returned value, use `ModelingToolkit.does_namespacing(sys)`. + +See also: [`ModelingToolkit.does_namespacing`](@ref). +""" function Base.getproperty( sys::AbstractSystem, name::Symbol; namespace = does_namespacing(sys)) if has_parent(sys) && (parent = get_parent(sys); parent !== nothing) From b85a193e49863365b451c85392bd248c6fc54c1c Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 23 May 2025 15:32:50 +0530 Subject: [PATCH 1871/2176] docs: add `INTERNAL_FIELD_WARNING` and `INTERNAL_ARGS_WARNING` --- src/ModelingToolkit.jl | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/ModelingToolkit.jl b/src/ModelingToolkit.jl index 4b0286f218..9bb96e1e14 100644 --- a/src/ModelingToolkit.jl +++ b/src/ModelingToolkit.jl @@ -122,6 +122,16 @@ for fun in [:toexpr] end end +const INTERNAL_FIELD_WARNING = """ +This field is internal API. It may be removed or changed without notice in a non-breaking \ +release. Usage of this field is not advised. +""" + +const INTERNAL_ARGS_WARNING = """ +The following arguments are internal API. They may be removed or changed without notice \ +in a non-breaking release. Usage of these arguments is not advised. +""" + """ $(TYPEDEF) From d5dab58bab11ab7aaa62753b2dab3f77992fb5a1 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 23 May 2025 15:31:49 +0530 Subject: [PATCH 1872/2176] docs: document `System` and its fields --- src/systems/system.jl | 187 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 187 insertions(+) diff --git a/src/systems/system.jl b/src/systems/system.jl index 5da5d9db79..f327a9ca9a 100644 --- a/src/systems/system.jl +++ b/src/systems/system.jl @@ -11,50 +11,237 @@ end const MetadataT = Base.ImmutableDict{DataType, Any} +""" + $(TYPEDEF) + +A symbolic representation of a numerical system to be solved. This is a recursive +tree-like data structure - each system can contain additional subsystems. As such, +it implements the `AbstractTrees.jl` interface to enable exploring the hierarchical +structure. + +# Fields + +$(TYPEDFIELDS) +""" struct System <: AbstractSystem + """ + $INTERNAL_FIELD_WARNING + A unique integer tag for the system. + """ tag::UInt + """ + The equations of the system. + """ eqs::Vector{Equation} # nothing - no noise # vector - diagonal noise # matrix - generic form # column matrix - scalar noise + """ + The noise terms for each equation of the system. This field is only used for flattened + systems. To represent noise in a hierarchical system, use brownians. In a system with + `N` equations and `K` independent brownian variables, this should be an `N x K` + matrix. In the special case where `N == K` and each equation has independent noise, + this noise matrix is diagonal. Diagonal noise can be specified by providing an `N` + length vector. If this field is `nothing`, the system does not have noise. + """ noise_eqs::Union{Nothing, AbstractVector, AbstractMatrix} + """ + Jumps associated with the system. Each jump can be a `VariableRateJump`, + `ConstantRateJump` or `MassActionJump`. See `JumpProcesses.jl` for more information. + """ jumps::Vector{JumpType} + """ + The constraints of the system. This can be used to represent the constraints in an + optimal-control problem or boundary-value differential equation, or the constraints + in a constrained optimization. + """ constraints::Vector{Union{Equation, Inequality}} + """ + The costs of the system. This can be the cost in an optimal-control problem, or the + loss of an optimization problem. Scalar loss values must also be provided as a single- + element vector. + """ costs::Vector{<:Union{BasicSymbolic, Real}} + """ + A function which combines costs into a scalar value. This should take two arguments, + the `costs` of this system and the consolidated costs of all subsystems in the order + they are present in the `systems` field. It should return a scalar cost that combines + all of the individual values. This defaults to a function that simply sums all cost + values. + """ consolidate::Any + """ + The variables being solved for by this system. For example, in a differential equation + system, this contains the dependent variables. + """ unknowns::Vector + """ + The parameters of the system. Parameters can either be variables that parameterize the + problem being solved for (e.g. the spring constant of a mass-spring system) or + additional unknowns not part of the main dynamics of the system (e.g. discrete/clocked + variables in a hybrid ODE). + """ ps::Vector + """ + The brownian variables of the system, created via `@brownians`. Each brownian variable + represents an independent noise. A system with brownians cannot be simulated directly. + It needs to be compiled using `mtkcompile` into `noise_eqs`. + """ brownians::Vector + """ + The independent variable for a time-dependent system, or `nothing` for a time-independent + system. + """ iv::Union{Nothing, BasicSymbolic{Real}} + """ + Equations that compute variables of a system that have been eliminated from the set of + unknowns by `mtkcompile`. More generally, this contains all variables that can be + computed from the unknowns and parameters and do not need to be solved for. Such + variables are termed as "observables". Each equation must be of the form + `observable ~ expression` and observables cannot appear on the LHS of multiple + equations. Equations must be sorted such that every observable appears on + the left hand side of an equation before it appears on the right hand side of any other + equation. + """ observed::Vector{Equation} parameter_dependencies::Vector{Equation} + """ + $INTERNAL_FIELD_WARNING + A mapping from the name of a variable to the actual symbolic variable in the system. + This is used to enable `getproperty` syntax to access variables of a system. + """ var_to_name::Dict{Symbol, Any} + """ + The name of the system. + """ name::Symbol + """ + An optional description for the system. + """ description::String + """ + Default values that variables (unknowns/observables/parameters) should take when + constructing a numerical problem from the system. These values can be overridden + by initial values provided to the problem constructor. Defaults of parent systems + take priority over those in child systems. + """ defaults::Dict + """ + Guess values for variables of a system that are solved for during initialization. + """ guesses::Dict + """ + A list of subsystems of this system. Used for hierarchically building models. + """ systems::Vector{System} + """ + Equations that must be satisfied during initialization of the numerical problem created + from this system. For time-dependent systems, these equations are not valid after the + initial time. + """ initialization_eqs::Vector{Equation} + """ + Symbolic representation of continuous events in a dynamical system. See + [`SymbolicContinuousCallback`](@ref). + """ continuous_events::Vector{SymbolicContinuousCallback} + """ + Symbolic representation of discrete events in a dynamica system. See + [`SymbolicDiscreteCallback`](@ref). + """ discrete_events::Vector{SymbolicDiscreteCallback} + """ + $INTERNAL_FIELD_WARNING + If this system is a connector, the type of connector it is. + """ connector_type::Any + """ + A map from expressions that must be through throughout the solution process to an + associated error message. By default these assertions cause the generated code to + output `NaN`s if violated, but can be made to error using `debug_system`. + """ assertions::Dict{BasicSymbolic, String} + """ + The metadata associated with this system, as a `Base.ImmutableDict`. This follows + the same interface as SymbolicUtils.jl. Metadata can be queried and updated using + `SymbolicUtils.getmetadata` and `SymbolicUtils.setmetadata` respectively. + """ metadata::MetadataT + """ + $INTERNAL_FIELD_WARNING + Metadata added by the `@mtkmodel` macro. + """ gui_metadata::Any # ? + """ + Whether the system contains delay terms. This is inferred from the equations, but + can also be provided explicitly. + """ is_dde::Bool + """ + Extra time points for the integrator to stop at. These can be numeric values, + or expressions of parameters and time. + """ tstops::Vector{Any} + """ + The `TearingState` of the system post-simplification with `mtkcompile`. + """ tearing_state::Any + """ + Whether the system namespaces variables accessed via `getproperty`. `complete`d systems + do not namespace, but this flag can be toggled independently of `complete` using + `toggle_namespacing`. + """ namespacing::Bool + """ + Whether the system is marked as "complete". Completed systems cannot be used as + subsystems. + """ complete::Bool + """ + $INTERNAL_FIELD_WARNING + For systems simplified or completed with `split = true` (the default) this contains an + `IndexCache` which aids in symbolic indexing. If this field is `nothing`, the system is + either not completed, or completed with `split = false`. + """ index_cache::Union{Nothing, IndexCache} + """ + $INTERNAL_FIELD_WARNING + Connections that should be ignored because they were removed by an analysis point + transformation. The first element of the tuple contains all such "standard" connections + (ones between connector systems) and the second contains all such causal variable + connections. + """ ignored_connections::Union{ Nothing, Tuple{Vector{IgnoredAnalysisPoint}, Vector{IgnoredAnalysisPoint}}} + """ + `SymbolicUtils.Code.Assignment`s to prepend to all code generated from this system. + """ preface::Any + """ + After simplification with `mtkcompile`, this field contains the unsimplified system + with the hierarchical structure. There may be multiple levels of `parent`s. The root + parent is used for accessing variables via `getproperty` syntax. + """ parent::Union{Nothing, System} + """ + A custom initialization system to use if no initial conditions are provided for the + unknowns or observables of this system. + """ initializesystem::Union{Nothing, System} + """ + Whether the current system is an initialization system. + """ is_initializesystem::Bool + """ + $INTERNAL_FIELD_WARNING + Whether the system has been simplified by `mtkcompile`. + """ isscheduled::Bool + """ + $INTERNAL_FIELD_WARNING + The `Schedule` containing additional information about the simplified system. + """ schedule::Union{Schedule, Nothing} function System( From 3cf5248b7d63323852b403d173d4b57533dc848a Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 23 May 2025 15:32:01 +0530 Subject: [PATCH 1873/2176] docs: document `System` constructors --- src/systems/system.jl | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/src/systems/system.jl b/src/systems/system.jl index f327a9ca9a..4909f5a9d3 100644 --- a/src/systems/system.jl +++ b/src/systems/system.jl @@ -301,6 +301,22 @@ function default_consolidate(costs, subcosts) return reduce(+, costs; init = 0.0) + reduce(+, subcosts; init = 0.0) end +""" + $(TYPEDSIGNATURES) + +Construct a system using the given equations `eqs`, independent variable `iv` (`nothing`) +for time-independent systems, unknowns `dvs`, parameters `ps` and brownian variables +`brownians`. + +## Keyword Arguments + +- `discover_from_metadata`: Whether to parse metadata of unknowns and parameters of the + system to obtain defaults and/or guesses. +- `checks`: Whether to perform sanity checks on the passed values. + +All other keyword arguments are named identically to the corresponding fields in +[`System`](@ref). +""" function System(eqs::Vector{Equation}, iv, dvs, ps, brownians = []; constraints = Union{Equation, Inequality}[], noise_eqs = nothing, jumps = [], costs = BasicSymbolic[], consolidate = default_consolidate, @@ -394,10 +410,23 @@ function System(eqs::Vector{Equation}, iv, dvs, ps, brownians = []; initializesystem, is_initializesystem; checks) end +""" + $(TYPEDSIGNATURES) + +Create a time-independent [`System`](@ref) with the given equations `eqs`, unknowns `dvs` +and parameters `ps`. +""" function System(eqs::Vector{Equation}, dvs, ps; kwargs...) System(eqs, nothing, dvs, ps; kwargs...) end +""" + $(TYPEDSIGNATURES) + +Create a time-dependent system with the given equations `eqs` and independent variable `iv`. +Discover variables, parameters and brownians in the system by parsing the equations and +other symbolic expressions passed to the system. +""" function System(eqs::Vector{Equation}, iv; kwargs...) iv === nothing && return System(eqs; kwargs...) @@ -486,6 +515,13 @@ function System(eqs::Vector{Equation}, iv; kwargs...) eqs, iv, collect(allunknowns), collect(new_ps), collect(brownians); kwargs...) end +""" + $(TYPEDSIGNATURES) + +Create a time-independent system with the given equations `eqs`. Discover variables and +parameters in the system by parsing the equations and other symbolic expressions passed to +the system. +""" function System(eqs::Vector{Equation}; kwargs...) eqs = collect(eqs) @@ -516,6 +552,11 @@ function System(eqs::Vector{Equation}; kwargs...) return System(eqs, nothing, collect(allunknowns), collect(new_ps); kwargs...) end +""" + $(TYPEDSIGNATURES) + +Create a `System` with a single equation `eq`. +""" System(eq::Equation, args...; kwargs...) = System([eq], args...; kwargs...) function gather_array_params(ps) From f0075c105bebea53d83ed0bf20da541fd384d8bc Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 23 May 2025 15:32:25 +0530 Subject: [PATCH 1874/2176] docs: document `flatten`, `getmetadata` and `setmetadata` --- src/systems/system.jl | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/systems/system.jl b/src/systems/system.jl index 4909f5a9d3..9c48375e86 100644 --- a/src/systems/system.jl +++ b/src/systems/system.jl @@ -693,6 +693,12 @@ function _check_if_dde(eqs, iv, subsystems) return is_dde end +""" + $(TYPEDSIGNATURES) + +Flatten the hierarchical structure of a system, collecting all equations, unknowns, etc. +into one top-level system after namespacing appropriately. +""" function flatten(sys::System, noeqs = false) systems = get_systems(sys) isempty(systems) && return sys @@ -863,11 +869,23 @@ function Base.hash(sys::System, h::UInt) return h end +""" + $(TYPEDSIGNATURES) + +Get the metadata associated with key `k` in system `sys` or `default` if it does not exist. +""" function SymbolicUtils.getmetadata(sys::AbstractSystem, k::DataType, default) meta = get_metadata(sys) return get(meta, k, default) end +""" + $(TYPEDSIGNATURES) + +Set the metadata associated with key `k` in system `sys` to value `v`. This is an +out-of-place operation, and will return a shallow copy of `sys` with the appropriate +metadata values. +""" function SymbolicUtils.setmetadata(sys::AbstractSystem, k::DataType, v) meta = get_metadata(sys) meta = Base.ImmutableDict(meta, k => v)::MetadataT From dd432517867891437665eb8022adc36e47c47b0b Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 23 May 2025 15:32:37 +0530 Subject: [PATCH 1875/2176] docs: document `XSystem` utility constructors --- src/systems/system.jl | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/systems/system.jl b/src/systems/system.jl index 9c48375e86..872cf5fc95 100644 --- a/src/systems/system.jl +++ b/src/systems/system.jl @@ -899,6 +899,13 @@ function check_complete(sys::System, obj) iscomplete(sys) || throw(SystemNotCompleteError(obj)) end +""" + $(TYPEDSIGNATURES) + +Convert a time-dependent system `sys` to a time-independent system of nonlinear +equations that solve for the steady state of the system where `D(x)` is zero for +each continuous variable `x`. +""" function NonlinearSystem(sys::System) if !is_time_dependent(sys) throw(ArgumentError("`NonlinearSystem` constructor expects a time-dependent `System`")) @@ -924,6 +931,11 @@ end # Utility constructors ######## +""" + $(METHODLIST) + +Construct a [`System`](@ref) to solve an optimization problem with the given scalar cost. +""" function OptimizationSystem(cost; kwargs...) return System(Equation[]; costs = [cost], kwargs...) end @@ -940,6 +952,11 @@ function OptimizationSystem(cost::Array, dvs, ps; kwargs...) return System(Equation[], nothing, dvs, ps; costs = vec(cost), kwargs...) end +""" + $(METHODLIST) + +Construct a [`System`](@ref) to solve a system of jump equations. +""" function JumpSystem(jumps, iv; kwargs...) mask = isa.(jumps, Equation) eqs = Vector{Equation}(jumps[mask]) @@ -954,6 +971,11 @@ function JumpSystem(jumps, iv, dvs, ps; kwargs...) return System(eqs, iv, dvs, ps; jumps, kwargs...) end +""" + $(METHODLIST) + +Construct a system of equations with associated noise terms. +""" function SDESystem(eqs::Vector{Equation}, noise, iv; is_scalar_noise = false, kwargs...) if is_scalar_noise if !(noise isa Vector) From 9ff28e46c412d6688c36095d6b2176ccfc26cad3 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 23 May 2025 17:23:27 +0530 Subject: [PATCH 1876/2176] docs: move variable metadata docs to `API` section --- .../Variable_metadata.md => API/variables.md} | 218 +++++++++++++----- 1 file changed, 154 insertions(+), 64 deletions(-) rename docs/src/{basics/Variable_metadata.md => API/variables.md} (63%) diff --git a/docs/src/basics/Variable_metadata.md b/docs/src/API/variables.md similarity index 63% rename from docs/src/basics/Variable_metadata.md rename to docs/src/API/variables.md index 43d0a7f2ea..0ff2e7799d 100644 --- a/docs/src/basics/Variable_metadata.md +++ b/docs/src/API/variables.md @@ -1,8 +1,22 @@ -# [Symbolic Metadata](@id symbolic_metadata) +# Symbolic variables and variable metadata -It is possible to add metadata to symbolic variables, the metadata will be displayed when calling help on a variable. +ModelingToolkit uses [Symbolics.jl](https://docs.sciml.ai/Symbolics/stable/) for the symbolic +manipulation infrastructure. In fact, the `@variables` macro is defined in Symbolics.jl. In +addition to `@variables`, ModelingToolkit defines `@parameters`, `@independent_variables`, +`@constants` and `@brownians`. These macros function identically to `@variables` but allow +ModelingToolkit to attach additional metadata. -The following information can be added (note, it's possible to extend this to user-defined metadata as well) +```@docs +Symbolics.@variables +@independent_variables +@parameters +@constants +@brownians +``` + +Symbolic variables can have metadata attached to them. The defaults and guesses assigned +at variable construction time are examples of this metadata. ModelingToolkit also defines +additional types of metadata. ## Variable descriptions @@ -39,6 +53,11 @@ help?> u Symbolics.VariableSource: (:variables, :u) ``` +```@docs +hasdescription +getdescription +``` + ## Connect Variables in connectors can have `connect` metadata which describes the type of connections. @@ -61,6 +80,16 @@ hasconnect(i) getconnect(k) ``` +```@docs +hasconnect +getconnect +``` + +```@docs; canonical = false +Flow +Stream +``` + ## Input or output Designate a variable as either an input or an output using the following @@ -78,16 +107,20 @@ isinput(u) isoutput(y) ``` +```@docs +isinput +isoutput +ModelingToolkit.setinput +ModelingToolkit.setoutput +``` + ## Bounds Bounds are useful when parameters are to be optimized, or to express intervals of uncertainty. -```@example metadata -@variables u [bounds = (-1, 1)] +```@repl metadata +@variables u [bounds = (-1, 1)]; hasbounds(u) -``` - -```@example metadata getbounds(u) ``` @@ -95,51 +128,45 @@ Bounds can also be specified for array variables. A scalar array bound is applie element of the array. A bound may also be specified as an array, in which case the size of the array must match the size of the symbolic variable. -```@example metadata -@variables x[1:2, 1:2] [bounds = (-1, 1)] +```@repl metadata +@variables x[1:2, 1:2] [bounds = (-1, 1)]; hasbounds(x) -``` - -```@example metadata getbounds(x) -``` - -```@example metadata getbounds(x[1, 1]) -``` - -```@example metadata getbounds(x[1:2, 1]) -``` - -```@example metadata -@variables x[1:2] [bounds = (-Inf, [1.0, Inf])] +@variables x[1:2] [bounds = (-Inf, [1.0, Inf])]; hasbounds(x) -``` - -```@example metadata getbounds(x) -``` - -```@example metadata getbounds(x[2]) +hasbounds(x[2]) ``` -```@example metadata -hasbounds(x[2]) +```@docs +hasbounds +getbounds ``` ## Guess -Specify an initial guess for custom initial conditions of an `ODESystem`. +Specify an initial guess for variables of a `System`. This is used when building the +[`InitializationProblem`](@ref). -```@example metadata -@variables u [guess = 1] +```@repl metadata +@variables u [guess = 1]; hasguess(u) +getguess(u) ``` -```@example metadata -getguess(u) +```@docs +hasguess +getguess +``` + +When a system is constructed, the guesses of the involved variables are stored in a `Dict` +in the system. After this point, the guess metadata of the variable is irrelevant. + +```@docs; canonical = false +guesses ``` ## Mark input as a disturbance @@ -151,6 +178,10 @@ Indicate that an input is not available for control, i.e., it's a disturbance in isdisturbance(u) ``` +```@docs +isdisturbance +``` + ## Mark parameter as tunable Indicate that a parameter can be automatically tuned by parameter optimization or automatic control tuning apps. @@ -160,20 +191,32 @@ Indicate that a parameter can be automatically tuned by parameter optimization o istunable(Kp) ``` +```@docs +istunable +ModelingToolkit.isconstant +``` + +!!! note + + [`@constants`](@ref) is a convenient way to create `@parameters` with `tunable = false` + metadata + ## Probability distributions A probability distribution may be associated with a parameter to indicate either uncertainty about its value, or as a prior distribution for Bayesian optimization. -```julia -using Distributions -d = Normal(10, 1) -@parameters m [dist = d] +```@repl metadata +using Distributions; +d = Normal(10, 1); +@parameters m [dist = d]; hasdist(m) +getdist(m) ``` -```julia -getdist(m) +```@docs +hasdist +getdist ``` ## Irreducible @@ -187,6 +230,10 @@ it can be accessed in [callbacks](@ref events) isirreducible(important_value) ``` +```@docs +isirreducible +``` + ## State Priority When a model is structurally simplified, the algorithm will try to ensure that the variables with higher state priority become states of the system. A variable's state priority is a number set using the `state_priority` metadata. @@ -196,18 +243,24 @@ When a model is structurally simplified, the algorithm will try to ensure that t state_priority(important_dof) ``` +```@docs +state_priority +``` + ## Units Units for variables can be designated using symbolic metadata. For more information, please see the [model validation and units](@ref units) section of the docs. Note that `getunit` is not equivalent to `get_unit` - the former is a metadata getter for individual variables (and is provided so the same interface function for `unit` exists like other metadata), while the latter is used to handle more general symbolic expressions. -```@example metadata -using DynamicQuantities -@variables speed [unit = u"m/s"] +```@repl metadata +using DynamicQuantities; +@variables speed [unit = u"m/s"]; hasunit(speed) +getunit(speed) ``` -```@example metadata -getunit(speed) +```@docs +hasunit +getunit ``` ## Miscellaneous metadata @@ -215,13 +268,23 @@ getunit(speed) User-defined metadata can be added using the `misc` metadata. This can be queried using the `hasmisc` and `getmisc` functions. -```@example metadata -@variables u [misc = :conserved_parameter] y [misc = [2, 4, 6]] +```@repl metadata +@variables u [misc = :conserved_parameter] y [misc = [2, 4, 6]]; hasmisc(u) +getmisc(y) ``` -```@example metadata -getmisc(y) +```@docs +hasmisc +getmisc +``` + +## Dumping metadata + +ModelingToolkit allows dumping the metadata of a variable as a `NamedTuple`. + +```@docs +ModelingToolkit.dump_variable_metadata ``` ## Additional functions @@ -250,25 +313,52 @@ lb, ub = getbounds(p) # operating on a vector, we get lower and upper bound vect b = getbounds(sys) # Operating on the system, we get a dict ``` -See also: [`ModelingToolkit.dump_variable_metadata`](@ref), [`ModelingToolkit.dump_parameters`](@ref), -[`ModelingToolkit.dump_unknowns`](@ref). +See also: + +```@docs; canonical = false +tunable_parameters +ModelingToolkit.dump_unknowns +ModelingToolkit.dump_parameters +``` + +## Symbolic operators -## Index +ModelingToolkit makes heavy use of "operators". These are custom functions that are applied +to symbolic variables. The most common operator is the `Differential` operator, defined in +Symbolics.jl. -```@index -Pages = ["Variable_metadata.md"] +```@docs +Symbolics.Differential ``` -## Docstrings +ModelingToolkit also defines a plethora of custom operators. -```@autodocs -Modules = [ModelingToolkit] -Pages = ["variables.jl"] -Private = false +```@docs +Pre +Initial +Shift ``` +While not an operator, `ShiftIndex` is commonly used to use `Shift` operators in a more +convenient way when writing discrete systems. + ```@docs -ModelingToolkit.dump_variable_metadata -ModelingToolkit.dump_parameters -ModelingToolkit.dump_unknowns +ShiftIndex +``` + +### Sampled time operators + +The following operators are used in hybrid ODE systems, where part of the dynamics of the +system happen at discrete intervals on a clock. While ModelingToolkit cannot yet simulate +such systems, it has the capability to represent them. + +!!! warn + + These operators are considered experimental API. + +```@docs +Sample +Hold +SampleTime +sampletime ``` From ebde6245b99fc5e26f8321f0e36eb8f44c06e329 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 23 May 2025 18:18:54 +0530 Subject: [PATCH 1877/2176] docs: add codegen page to `API` docs --- docs/src/API/codegen.md | 45 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 docs/src/API/codegen.md diff --git a/docs/src/API/codegen.md b/docs/src/API/codegen.md new file mode 100644 index 0000000000..b3a9c01e58 --- /dev/null +++ b/docs/src/API/codegen.md @@ -0,0 +1,45 @@ +# Code generation utilities + +These are lower-level functions that ModelingToolkit leverages to generate code for +building numerical problems. + +```@docs +ModelingToolkit.generate_rhs +ModelingToolkit.generate_diffusion_function +ModelingToolkit.generate_jacobian +ModelingToolkit.generate_tgrad +ModelingToolkit.generate_hessian +ModelingToolkit.generate_W +ModelingToolkit.generate_dae_jacobian +ModelingToolkit.generate_history +ModelingToolkit.generate_boundary_conditions +ModelingToolkit.generate_cost +ModelingToolkit.generate_cost_gradient +ModelingToolkit.generate_cost_hessian +ModelingToolkit.generate_cons +ModelingToolkit.generate_constraint_jacobian +ModelingToolkit.generate_constraint_hessian +ModelingToolkit.generate_control_jacobian +ModelingToolkit.build_explicit_observed_function +``` + +For functions such as jacobian calculation which require symbolic computation, there +are `calculate_*` equivalents to obtain the symbolic result without building a function. + +```@docs +ModelingToolkit.calculate_tgrad +ModelingToolkit.calculate_jacobian +ModelingToolkit.jacobian_sparsity +ModelingToolkit.jacobian_dae_sparsity +ModelingToolkit.calculate_hessian +ModelingToolkit.hessian_sparsity +ModelingToolkit.calculate_massmatrix +ModelingToolkit.W_sparsity +ModelingToolkit.calculate_W_prototype +ModelingToolkit.calculate_cost_gradient +ModelingToolkit.calculate_cost_hessian +ModelingToolkit.cost_hessian_sparsity +ModelingToolkit.calculate_constraint_jacobian +ModelingToolkit.calculate_constraint_hessian +ModelingToolkit.calculate_control_jacobian +``` From 5b64a6b7d2aefd3d1b352151a160c2ca3c36f9a0 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Sat, 24 May 2025 11:17:50 +0530 Subject: [PATCH 1878/2176] docs: document `nameof` and `brownian` --- demo.jl | 107 +++++++++++++++++++++++ docs/src/getting_started/odes.md | 145 +++++++++++++++++++++++++++++++ src/systems/abstractsystem.jl | 11 +++ 3 files changed, 263 insertions(+) create mode 100644 demo.jl create mode 100644 docs/src/getting_started/odes.md diff --git a/demo.jl b/demo.jl new file mode 100644 index 0000000000..0a1bea54db --- /dev/null +++ b/demo.jl @@ -0,0 +1,107 @@ +macro mtkcompile(ex...) + quote + @mtkbuild $(ex...) + end +end + +function mtkcompile(args...; kwargs...) + structural_simplify(args...; kwargs...) +end + +################################# + +using ModelingToolkit, OrdinaryDiffEq, StochasticDiffEq +using ModelingToolkit: t_nounits as t, D_nounits as D + +## ODEs + +@parameters g +@variables x(t) y(t) λ(t) +eqs = [D(D(x)) ~ λ * x + D(D(y)) ~ λ * y - g + x^2 + y^2 ~ 1] +@mtkbuild pend = System(eqs, t) +prob = ODEProblem(pend, [x => -1, y => 0], (0.0, 10.0), [g => 1], guesses = [λ => 1]) + +sol = solve(prob, FBDF()) + +## SDEs and unified `System` + +@variables x(t) y(t) z(t) +@parameters σ ρ β +@brownian a + +eqs = [ + D(x) ~ σ * (y - x) + 0.1x * a, + D(y) ~ x * (ρ - z) - y + 0.1y * a, + D(z) ~ x * y - β * z + 0.1z * a +] + +@mtkbuild sys1 = System(eqs, t) + +eqs = [ + D(x) ~ σ * (y - x), + D(y) ~ x * (ρ - z) - y, + D(z) ~ x * y - β * z +] + +noiseeqs = [0.1*x; + 0.1*y; + 0.1*z;;] + +@mtkbuild sys2 = SDESystem(eqs, noiseeqs, t) + +u0 = [ + x => 1.0, + y => 0.0, + z => 0.0] + +p = [σ => 28.0, + ρ => 10.0, + β => 8 / 3] + +sdeprob = SDEProblem(sys1, u0, (0.0, 10.0), p) +sdesol = solve(sdeprob, ImplicitEM()) + +odeprob = ODEProblem(sys1, u0, (0.0, 10.0), p) # error! +odeprob = ODEProblem(sys1, u0, (0.0, 10.0), p; check_compatibility = false) + +@variables x y z +@parameters σ ρ β + +# Define a nonlinear system +eqs = [0 ~ σ * (y - x), + y ~ x * (ρ - z), + β * z ~ x * y] +@mtkbuild sys = System(eqs) + +## ImplicitDiscrete Affects + +@parameters g +@variables x(t) y(t) λ(t) +eqs = [D(D(x)) ~ λ * x + D(D(y)) ~ λ * y - g + x^2 + y^2 ~ 1] +c_evt = [t ~ 5.0] => [x ~ Pre(x) + 0.1] +@mtkbuild pend = System(eqs, t, continuous_events = c_evt) +prob = ODEProblem(pend, [x => -1, y => 0], (0.0, 10.0), [g => 1], guesses = [λ => 1]) + +sol = solve(prob, FBDF()) + +## `@named` and `ParentScope` + +function SysA(; name, var1) + @variables x(t) + return System([D(x) ~ var1], t; name) +end +function SysB(; name, var1) + @variables x(t) + @named subsys = SysA(; var1) + return System([D(x) ~ x], t; systems = [subsys], name) +end +function SysC(; name) + @variables x(t) + @named subsys = SysB(; var1 = x) + return System([D(x) ~ x], t; systems = [subsys], name) +end +@mtkbuild sys = SysC() diff --git a/docs/src/getting_started/odes.md b/docs/src/getting_started/odes.md new file mode 100644 index 0000000000..7f51accc16 --- /dev/null +++ b/docs/src/getting_started/odes.md @@ -0,0 +1,145 @@ +# [Building ODEs and DAEs with ModelingToolkit.jl](@id getting_started_ode) + +This is an introductory tutorial for ModelingToolkit.jl (MTK). We will demonstrate the +basics of the package by demontrating how to build systems of Ordinary Differential +Equations (ODEs) and Differential-Algebraic Equations (DAEs). + +## Installing ModelingToolkit + +To install ModelingToolkit, use the Julia package manager. This can be done as follows: + +```julia +using Pkg +Pkg.add("ModelingToolkit") +``` + +## The end goal + +TODO + +## Basics of MTK + +ModelingToolkit.jl is a symbolic-numeric system. This means it allows specifying a model +(such as an ODE) in a similar way to how it would be written on paper. Let's start with a +simple example. The system to be modeled is a first-order lag element: + +```math +\dot{x} = \frac{f(t) - x(t)}{\tau} +``` + +Here, ``t`` is the independent variable (time), ``x(t)`` is the (scalar) unknown +variable, ``f(t)`` is an external forcing function, and ``\tau`` is a +parameter. + +For simplicity, we will start off by setting the forcing function to a constant `1`. Every +ODE has a single independent variable. MTK has a common definition for time `t` and the +derivative with respect to it. + +```@example ode2 +using ModelingToolkit +using ModelingToolkit: t_nounits as t, D_nounits as D +``` + +Next, we declare the (dependent) variables and the parameters of our model: + +```@example ode2 +@variables x(t) +@parameters τ +``` + +Note the syntax `x(t)`. We must declare that the variable `x` is a function of the independent +variable `t`. Next, we define the equations of the system: + +```@example ode2 +eqs = [D(x) ~ (1 - x) / τ] +``` + +Since `=` is reserved as the assignment operator, MTK uses `~` to denote equality between +expressions. Now we must consolidate all of this information about our system of ODEs into +ModelingToolkit's `System` type. + +```@example ode2 +sys = System(eqs, t, [x], [τ]; name = :sys) +``` + +The `System` constructor accepts a `Vector{Equation}` as the first argument, followed by the +independent variable, a list of dependent variables, and a list of parameters. Every system +must be given a name via the `name` keyword argument. Most of the time, we want to name our +system the same as the variable it is assigned to. The `@named` macro helps with this: + +```@example ode2 +@named sys = System(eqs, t, [x], [τ]) +``` + +Additionally, it may become inconvenient to specify all variables and parameters every time +a system is created. MTK allows omitting these arguments, and will automatically infer them +from the equations. + +```@example ode2 +@named sys = System(eqs, t) +``` + +Our system is not quite ready for simulation yet. First, we must use the `mtkcompile` +function which transforms the system into a form that MTK can handle. For our trivial +system, this does not do much. + +```@example ode2 +simp_sys = mtkcompile(sys) +``` + +Since building and simplifying a system is a common workflow, MTK provides the `@mtkcompile` +macro for convenience. + +```@example ode2 +@mtkcompile sys = System(eqs, t) +``` + +We can now build an `ODEProblem` from the system. ModelingToolkit generates the necessary +code for numerical ODE solvers to solve this system. We need to provide an initial +value for the variable `x` and a value for the parameter `p`, as well as the time span +for which to simulate the system. + +```@example ode2 +prob = ODEProblem(sys, [x => 0.0, τ => 3.0], (0.0, 10.0)) +``` + +Here, we are saying that `x` should start at `0.0`, `τ` should be `3.0` and the system +should be simulated from `t = 0.0` to `t = 10.0`. To solve the system, we must import a +solver. + +```@example ode2 +using OrdinaryDiffEq + +sol = solve(prob) +``` + +[OrdinaryDiffEq.jl](https://docs.sciml.ai/DiffEqDocs/stable/) contains a large number of +numerical solvers. It also comes with a default solver which is used when calling +`solve(prob)` and is capable of handling a large variety of systems. + +We can obtain the timeseries of `x` by indexing the solution with the symbolic variable: + +```@example ode2 +sol[x] +``` + +We can even obtain timeseries of complicated expressions involving the symbolic variables in +the model + +```@example ode2 +sol[(1 - x) / τ] +``` + +Perhaps more interesting is a plot of the solution. This can easily be achieved using Plots.jl. + +```@example ode2 +using Plots + +plot(sol) +``` + +Similarly, we can plot different expressions: + +```@example ode2 +plot(sol; idxs = (1 - x) / τ) +``` diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index b997219eba..549a86afda 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -197,7 +197,18 @@ end const MTKPARAMETERS_ARG = Sym{Vector{Vector}}(:___mtkparameters___) +""" + $(TYPEDSIGNATURES) + +Obtain the name of `sys`. +""" Base.nameof(sys::AbstractSystem) = getfield(sys, :name) +""" + $(TYPEDSIGNATURES) + +Obtain the description associated with `sys` if present, and an empty +string otherwise. +""" description(sys::AbstractSystem) = has_description(sys) ? get_description(sys) : "" """ From 292ff836b6d7accb1b54e4105d5f2d8fb58b059e Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Sat, 24 May 2025 11:19:07 +0530 Subject: [PATCH 1879/2176] docs: add docs for `jumps`, `brownians`, `cost`, `constraints`, `symbolic_tstops`, `preface` --- src/systems/abstractsystem.jl | 36 ++++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index 549a86afda..8f43d2f66f 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -1679,6 +1679,12 @@ function equations_toplevel(sys::AbstractSystem) return get_eqs(sys) end +""" + $(TYPEDSIGNATURES) + +Get the flattened jumps of the system. In other words, obtain all of the jumps in `sys` and +all the subsystems of `sys` (appropriately namespaced). +""" function jumps(sys::AbstractSystem) js = get_jumps(sys) systems = get_systems(sys) @@ -1688,6 +1694,12 @@ function jumps(sys::AbstractSystem) return [js; reduce(vcat, namespace_jumps.(systems); init = [])] end +""" + $(TYPEDSIGNATURES) + +Get all of the brownian variables involved in the system `sys` and all subsystems, +appropriately namespaced. +""" function brownians(sys::AbstractSystem) bs = get_brownians(sys) systems = get_systems(sys) @@ -1697,6 +1709,12 @@ function brownians(sys::AbstractSystem) return [bs; reduce(vcat, namespace_brownians.(systems); init = [])] end +""" + $(TYPEDSIGNATURES) + +Recursively consolidate the cost vector of `sys` and all subsystems of `sys`, returning the +final scalar cost function. +""" function cost(sys::AbstractSystem) cs = get_costs(sys) consolidate = get_consolidate(sys) @@ -1726,7 +1744,12 @@ function namespace_constraints(sys) map(cstr -> namespace_constraint(cstr, sys), cstrs) end -function constraints(sys) +""" + $(TYPEDSIGNATURES) + +Get all constraints in the system `sys` and all of its subsystems, appropriately namespaced. +""" +function constraints(sys::AbstractSystem) cs = get_constraints(sys) systems = get_systems(sys) isempty(systems) ? cs : [cs; reduce(vcat, namespace_constraints.(systems))] @@ -1753,6 +1776,11 @@ function initialization_equations(sys::AbstractSystem) end end +""" + $(TYPEDSIGNATURES) + +Get the tstops present in `sys` and its subsystems, appropriately namespaced. +""" function symbolic_tstops(sys::AbstractSystem) tstops = get_tstops(sys) systems = get_systems(sys) @@ -1761,6 +1789,12 @@ function symbolic_tstops(sys::AbstractSystem) return tstops end +""" + $(TYPEDSIGNATURES) + +Obtain the preface associated with `sys` and all of its subsystems, appropriately +namespaced. +""" function preface(sys::AbstractSystem) has_preface(sys) || return nothing pre = get_preface(sys) From 2f5f27fdf6ca153870952ddffcfd091ef611f9c9 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Sat, 24 May 2025 11:19:47 +0530 Subject: [PATCH 1880/2176] refactor: move `full_equations` to `ModelingToolkit`, don't error on singular systems --- .../symbolics_tearing.jl | 42 --------------- src/systems/abstractsystem.jl | 51 +++++++++++++++++++ 2 files changed, 51 insertions(+), 42 deletions(-) diff --git a/src/structural_transformation/symbolics_tearing.jl b/src/structural_transformation/symbolics_tearing.jl index a608b8b5ee..9eccc309bd 100644 --- a/src/structural_transformation/symbolics_tearing.jl +++ b/src/structural_transformation/symbolics_tearing.jl @@ -99,48 +99,6 @@ function eq_derivative!(ts::TearingState, ieq::Int; kwargs...) return eq_diff end -function tearing_sub(expr, dict, s) - expr = Symbolics.fixpoint_sub(expr, dict; operator = ModelingToolkit.Initial) - s ? simplify(expr) : expr -end - -function tearing_substitute_expr(sys::AbstractSystem, expr; simplify = false) - empty_substitutions(sys) && return expr - substitutions = get_substitutions(sys) - return tearing_sub(expr, substitutions, simplify) -end - -""" -$(TYPEDSIGNATURES) - -Like `equations(sys)`, but includes substitutions done by the tearing process. -These equations matches generated numerical code. - -See also [`equations`](@ref) and [`ModelingToolkit.get_eqs`](@ref). -""" -function full_equations(sys::AbstractSystem; simplify = false) - isempty(observed(sys)) && return equations(sys) - subs = Dict([eq.lhs => eq.rhs for eq in observed(sys)]) - neweqs = map(equations(sys)) do eq - if iscall(eq.lhs) && operation(eq.lhs) isa Union{Shift, Differential} - return tearing_sub(eq.lhs, subs, simplify) ~ tearing_sub(eq.rhs, subs, - simplify) - else - if !(eq.lhs isa Number && eq.lhs == 0) - eq = 0 ~ eq.rhs - eq.lhs - end - rhs = tearing_sub(eq.rhs, subs, simplify) - if rhs isa Symbolic - return 0 ~ rhs - else # a number - error("tearing failed because the system is singular") - end - end - eq - end - return neweqs -end - function tearing_substitution(sys::AbstractSystem; kwargs...) neweqs = full_equations(sys::AbstractSystem; kwargs...) @set! sys.eqs = neweqs diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index 8f43d2f66f..d736edf614 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -1679,6 +1679,57 @@ function equations_toplevel(sys::AbstractSystem) return get_eqs(sys) end +""" + $(TYPEDSIGNATURES) + +Recursively substitute `dict` into `expr`. Use `Symbolics.simplify` on the expression +if `simplify == true`. +""" +function substitute_and_simplify(expr, dict::AbstractDict, simplify::Bool) + expr = Symbolics.fixpoint_sub(expr, dict; operator = ModelingToolkit.Initial) + simplify ? Symbolics.simplify(expr) : expr +end + +""" + $(TYPEDSIGNATURES) + +Recursively substitute the observed equations of `sys` into `expr`. If `simplify`, call +`Symbolics.simplify` on the resultant expression. +""" +function substitute_observed(sys::AbstractSystem, expr; simplify = false) + empty_substitutions(sys) && return expr + substitutions = get_substitutions(sys) + return substitute_and_simplify(expr, substitutions, simplify) +end + +""" +$(TYPEDSIGNATURES) + +Like `equations(sys)`, but also substitutes the observed equations eliminated from the +equations during `mtkcompile`. These equations matches generated numerical code. + +See also [`equations`](@ref) and [`ModelingToolkit.get_eqs`](@ref). +""" +function full_equations(sys::AbstractSystem; simplify = false) + empty_substitutions(sys) && return equations(sys) + subs = get_substitutions(sys) + neweqs = map(equations(sys)) do eq + if iscall(eq.lhs) && operation(eq.lhs) isa Union{Shift, Differential} + return substitute_and_simplify(eq.lhs, subs, simplify) ~ substitute_and_simplify( + eq.rhs, subs, + simplify) + else + if !_iszero(eq.lhs) + eq = 0 ~ eq.rhs - eq.lhs + end + rhs = substitute_and_simplify(eq.rhs, subs, simplify) + return 0 ~ rhs + end + eq + end + return neweqs +end + """ $(TYPEDSIGNATURES) From 04c02bc43903a0737a537850ab6a93f3109299da Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Sat, 24 May 2025 11:20:06 +0530 Subject: [PATCH 1881/2176] refactor: use new `substitute_observed` instead of `tearing_substitute_expr` --- src/systems/systems.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/systems/systems.jl b/src/systems/systems.jl index 13a62f7915..ceab3c6c2f 100644 --- a/src/systems/systems.jl +++ b/src/systems/systems.jl @@ -147,7 +147,7 @@ function __mtkcompile(sys::AbstractSystem; simplify = false, is_scalar_noise = false end - noise_eqs = StructuralTransformations.tearing_substitute_expr(ode_sys, noise_eqs) + noise_eqs = substitute_observed(ode_sys, noise_eqs) ssys = System(Vector{Equation}(full_equations(ode_sys)), get_iv(ode_sys), unknowns(ode_sys), parameters(ode_sys); noise_eqs, name = nameof(ode_sys), observed = observed(ode_sys), defaults = defaults(sys), From d507ff969d568aadcb0d925bec92d0bdfe327f52 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Sat, 24 May 2025 11:20:25 +0530 Subject: [PATCH 1882/2176] docs: add docs for `empty_substitutions` and `get_substitutions` --- src/utils.jl | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/utils.jl b/src/utils.jl index e2bc68e03f..5397b23077 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -693,10 +693,21 @@ end isarray(x) = x isa AbstractArray || x isa Symbolics.Arr +""" + $(TYPEDSIGNATURES) + +Check if any variables were eliminated from the system as part of `mtkcompile`. +""" function empty_substitutions(sys) isempty(observed(sys)) end +""" + $(TYPEDSIGNATURES) + +Get a dictionary mapping variables eliminated from the system during `mtkcompile` to the +expressions used to calculate them. +""" function get_substitutions(sys) Dict([eq.lhs => eq.rhs for eq in observed(sys)]) end From c5cc07af1217ee56afc175fb33aaa72751814f38 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Sat, 24 May 2025 12:40:50 +0530 Subject: [PATCH 1883/2176] docs: add docs for `apply_to_variables` --- src/systems/abstractsystem.jl | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index d736edf614..e2bd39ca5c 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -1048,8 +1048,18 @@ function Base.setproperty!(sys::AbstractSystem, prop::Symbol, val) """) end -apply_to_variables(f::F, ex) where {F} = _apply_to_variables(f, ex) -apply_to_variables(f::F, ex::Num) where {F} = wrap(_apply_to_variables(f, unwrap(ex))) +""" + $(TYPEDSIGNATURES) + +Apply function `f` to each variable in expression `ex`. `f` should be a function that takes +a variable and returns the replacement to use. A "variable" in this context refers to a +symbolic quantity created directly from a variable creation macro such as +[`Symbolics.@variables`](@ref), [`@independent_variables`](@ref), [`@parameters`](@ref), +[`@constants`](@ref) or [`@brownians`](@ref). +""" +apply_to_variables(f, ex) = _apply_to_variables(f, ex) +apply_to_variables(f, ex::Num) = wrap(_apply_to_variables(f, unwrap(ex))) +apply_to_variables(f, ex::Symbolics.Arr) = wrap(_apply_to_variables(f, unwrap(ex))) function _apply_to_variables(f::F, ex) where {F} if isvariable(ex) return f(ex) From e1ca3682cc243b1aa578cf1db6c3ef2bcfe0c82c Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Sat, 24 May 2025 12:41:12 +0530 Subject: [PATCH 1884/2176] docs: add docs for `@component` and `@connector` --- src/systems/abstractsystem.jl | 20 ++++++++++++++++++++ src/systems/connectors.jl | 31 +++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index e2bd39ca5c..1d97417d93 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -2471,6 +2471,26 @@ function component_post_processing(expr, isconnector) end end +""" + $(TYPEDSIGNATURES) + +Mark a system constructor function as building a component. For example, + +```julia +@component function AddOne(; name) + @variables in(t) out(t) + eqs = [out ~ in + 1] + return System(eqs, t, [in, out], []; name) +end +``` + +ModelingToolkit systems are either components or connectors. Components define dynamics of +the model. Connectors are used to connect components together. See the +[Model building reference](@ref model_building_api) section of the documentation for more +information. + +See also: [`@connector`](@ref). +""" macro component(expr) esc(component_post_processing(expr, false)) end diff --git a/src/systems/connectors.jl b/src/systems/connectors.jl index ba41b2b011..b4a3d47711 100644 --- a/src/systems/connectors.jl +++ b/src/systems/connectors.jl @@ -20,6 +20,37 @@ function get_connection_type(s) getmetadata(s, VariableConnectType, Equality) end +""" + $(TYPEDSIGNATURES) + +Mark a system constructor function as building a connector. For example, + +```julia +@connector function ElectricalPin(; name, v = nothing, i = nothing) + @variables begin + v(t) = v, [description = "Potential at the pin [V]"] + i(t) = i, [connect = Flow, description = "Current flowing into the pin [A]"] + end + return System(Equation[], t, [v, i], []; name) +end +``` + +Since connectors only declare variables, the equivalent shorthand syntax can also be used: + +```julia +@connector Pin begin + v(t), [description = "Potential at the pin [V]"] + i(t), [connect = Flow, description = "Current flowing into the pin [A]"] +end +``` + +ModelingToolkit systems are either components or connectors. Components define dynamics of +the model. Connectors are used to connect components together. See the +[Model building reference](@ref model_building_api) section of the documentation for more +information. + +See also: [`@component`](@ref). +""" macro connector(expr) esc(component_post_processing(expr, true)) end From 81a2cd64a0e051cac224467a968821d4261d0378 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Sat, 24 May 2025 12:41:42 +0530 Subject: [PATCH 1885/2176] docs: add docs for `hierarchy`, `extend`, `compose` --- src/systems/abstractsystem.jl | 49 ++++++++++++++++++++++++++++++++--- 1 file changed, 46 insertions(+), 3 deletions(-) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index 1d97417d93..3ad7c35923 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -2619,6 +2619,11 @@ end hierarchy(sys::AbstractSystem; describe = false, bold = describe, kwargs...) Print a tree of a system's hierarchy of subsystems. + +# Keyword arguments + +- `describe`: Whether to also print the description of each subsystem, if present. +- `bold`: Whether to print the name of the system in **bold** font. """ function hierarchy(sys::AbstractSystem; describe = false, bold = describe, kwargs...) print_tree(sys; printnode_kw = (describe = describe, bold = bold), kwargs...) @@ -2661,9 +2666,14 @@ end """ $(TYPEDSIGNATURES) -Extend `basesys` with `sys`. +Extend `basesys` with `sys`. This can be thought of as the `merge` operation on systems. +Values in `sys` take priority over duplicates in `basesys` (for example, defaults). + By default, the resulting system inherits `sys`'s name and description. +The `&` operator can also be used for this purpose. `sys & basesys` is equivalent to +`extend(sys, basesys)`. + See also [`compose`](@ref). """ function extend(sys::AbstractSystem, basesys::AbstractSystem; @@ -2710,14 +2720,31 @@ function extend(sys::AbstractSystem, basesys::AbstractSystem; return T(args...; kwargs...) end +""" + $(TYPEDSIGNATURES) + +Extend `sys` with all systems in `basesys` in order. +""" function extend(sys, basesys::Vector{T}) where {T <: AbstractSystem} foldl(extend, basesys, init = sys) end +""" + $(TYPEDSIGNATURES) + +Syntactic sugar for `extend(sys, basesys)`. + +See also: [`extend`](@ref). +""" function Base.:(&)(sys::AbstractSystem, basesys::AbstractSystem; kwargs...) extend(sys, basesys; kwargs...) end +""" + $(TYPEDSIGNATURES) + +Syntactic sugar for `extend(sys, basesys)`. +""" function Base.:(&)( sys::AbstractSystem, basesys::Vector{T}; kwargs...) where {T <: AbstractSystem} extend(sys, basesys; kwargs...) @@ -2726,8 +2753,11 @@ end """ $(SIGNATURES) -Compose multiple systems together. The resulting system would inherit the first -system's name. +Compose multiple systems together. This adds all of `systems` as subsystems of `sys`. +The resulting system inherits the name of `sys` by default. + +The `∘` operator can also be used for this purpose. `sys ∘ basesys` is equivalent to +`compose(sys, basesys)`. See also [`extend`](@ref). """ @@ -2749,9 +2779,22 @@ function compose(sys::AbstractSystem, systems::AbstractArray; name = nameof(sys) @set! sys.ps = unique!(vcat(get_ps(sys), collect(newparams))) return sys end +""" + $(TYPEDSIGNATURES) + +Syntactic sugar for adding all systems in `syss` as the subsystems of `first(syss)`. +""" function compose(syss...; name = nameof(first(syss))) compose(first(syss), collect(syss[2:end]); name = name) end + +""" + $(TYPEDSIGNATURES) + +Syntactic sugar for `compose(sys1, sys2)`. + +See also: [`compose`](@ref). +""" Base.:(∘)(sys1::AbstractSystem, sys2::AbstractSystem) = compose(sys1, sys2) function UnPack.unpack(sys::ModelingToolkit.AbstractSystem, ::Val{p}) where {p} From 51bd5e8038b9ff4c60d97fe56d97d459477edd96 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Sat, 24 May 2025 12:42:01 +0530 Subject: [PATCH 1886/2176] docs: add docs for `Equality`, `Flow`, `Stream` and `instream` --- src/systems/connectors.jl | 9 +++++++++ src/variables.jl | 42 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+) diff --git a/src/systems/connectors.jl b/src/systems/connectors.jl index b4a3d47711..473b1bd680 100644 --- a/src/systems/connectors.jl +++ b/src/systems/connectors.jl @@ -94,6 +94,15 @@ is_domain_connector(s) = isconnector(s) && get_connector_type(s) === DomainConne get_systems(c::Connection) = c.systems +""" + $(TYPEDSIGNATURES) + +`instream` is used when modeling stream connections. It is only allowed to be used on +`Stream` variables. + +Refer to the [Connection semantics](@ref connect_semantics) section of the docs for more +information. +""" instream(a) = term(instream, unwrap(a), type = symtype(a)) SymbolicUtils.promote_symtype(::typeof(instream), _) = Real diff --git a/src/variables.jl b/src/variables.jl index 230c98a985..746548b432 100644 --- a/src/variables.jl +++ b/src/variables.jl @@ -91,8 +91,50 @@ end ### Connect abstract type AbstractConnectType end +""" + $(TYPEDEF) + +Flag which is meant to be passed to the `connect` metadata of a variable to affect how it +behaves when the connector it is in is part of a `connect` equation. `Equality` is the +default value and such variables when connected are made equal. For example, electric +potential is equated at a junction. + +For more information, refer to the [Connection semantics](@ref connect_semantics) section +of the docs. + +See also: [`Symbolics.connect`](@ref), [`@connector`](@ref), [`Flow`](@ref), +[`Stream`](@ref). +""" struct Equality <: AbstractConnectType end # Equality connection +""" + $(TYPEDEF) + +Flag which is meant to be passed to the `connect` metadata of a variable to affect how it +behaves when the connector it is in is part of a `connect` equation. `Flow` denotes that +the sum of marked variable in all connectors in the connection set must sum to zero. For +example, electric current sums to zero at a junction (assuming appropriate signs are used +for current flowing in and out of the function). + +For more information, refer to the [Connection semantics](@ref connect_semantics) section +of the docs. + +See also: [`Symbolics.connect`](@ref), [`@connector`](@ref), [`Equality`](@ref), +[`Stream`](@ref). +""" struct Flow <: AbstractConnectType end # sum to 0 +""" + $(TYPEDEF) + +Flag which is meant to be passed to the `connect` metadata of a variable to affect how it +behaves when the connector it is in is part of a `connect` equation. `Stream` denotes that +the variable is part of a special stream connector. + +For more information, refer to the [Connection semantics](@ref connect_semantics) section +of the docs. + +See also: [`Symbolics.connect`](@ref), [`@connector`](@ref), [`Equality`](@ref), +[`Flow`](@ref). +""" struct Stream <: AbstractConnectType end # special stream connector """ From 950cede02b8d2aba10083f178748218ac20b9f1f Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Sat, 24 May 2025 12:42:28 +0530 Subject: [PATCH 1887/2176] chore: add SciMLPublic.jl as a dependency --- Project.toml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Project.toml b/Project.toml index 41568022ab..527451bca2 100644 --- a/Project.toml +++ b/Project.toml @@ -51,6 +51,7 @@ Reexport = "189a3867-3050-52da-a836-e630ba90ab69" RuntimeGeneratedFunctions = "7e49a35a-f44a-4d26-94aa-eba1b4ca6b47" SCCNonlinearSolve = "9dfe8606-65a1-4bb3-9748-cb89d1561431" SciMLBase = "0bca4576-84f4-4d90-8ffe-ffa030f20462" +SciMLPublic = "431bcebd-1456-4ced-9d72-93c2757fff0b" SciMLStructures = "53ae85a6-f571-4167-b2af-e1d143709226" Serialization = "9e88b42a-f829-5b0c-bbe9-9e923198166b" Setfield = "efcf1570-3423-57d1-acb7-fd33fddbac46" @@ -145,6 +146,7 @@ Reexport = "0.2, 1" RuntimeGeneratedFunctions = "0.5.9" SCCNonlinearSolve = "1.0.0" SciMLBase = "2.91.1" +SciMLPublic = "1.0.0" SciMLStructures = "1.7" Serialization = "1" Setfield = "0.7, 0.8, 1" From ee8a461a0aabf58dcb05f628b964daa497c95adf Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Sat, 24 May 2025 12:42:39 +0530 Subject: [PATCH 1888/2176] chore: mark `apply_to_variables` as public --- src/ModelingToolkit.jl | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/ModelingToolkit.jl b/src/ModelingToolkit.jl index 9bb96e1e14..79d2afcd67 100644 --- a/src/ModelingToolkit.jl +++ b/src/ModelingToolkit.jl @@ -99,6 +99,7 @@ const DQ = DynamicQuantities import DifferentiationInterface as DI using ADTypes: AutoForwardDiff +import SciMLPublic: @public export @derivatives @@ -358,4 +359,6 @@ export AbstractDynamicOptProblem, JuMPDynamicOptProblem, InfiniteOptDynamicOptPr CasADiDynamicOptProblem export DynamicOptSolution +@public apply_to_variables + end # module From 6367ed389ca8b408d92089983977d8750d8bee3b Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Sat, 24 May 2025 15:29:56 +0530 Subject: [PATCH 1889/2176] docs: improve docstring for `complete` --- src/systems/abstractsystem.jl | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index 3ad7c35923..40029086a1 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -732,6 +732,9 @@ the global structure of the system. One property to note is that if a system is complete, the system will no longer namespace its subsystems or variables, i.e. `isequal(complete(sys).v.i, v.i)`. + +This namespacing functionality can also be toggled independently of `complete` +using [`toggle_namespacing`](@ref). """ function complete( sys::AbstractSystem; split = true, flatten = true, add_initial_parameters = true) @@ -2495,6 +2498,22 @@ macro component(expr) esc(component_post_processing(expr, false)) end +""" + $(TYPEDSIGNATURES) + +Macro shorthand for building and compiling a system in one step. + +```julia +@mtkcompile sys = Constructor(args...; kwargs....) +``` + +Is shorthand for + +```julia +@named sys = Constructor(args...; kwargs...) +sys = mtkcompile(sys) +``` +""" macro mtkcompile(exprs...) expr = exprs[1] named_expr = ModelingToolkit.named_expr(expr) From f4783b027983e9cf8a31781f8a9ac8dc7f5a2919 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Sat, 24 May 2025 15:30:17 +0530 Subject: [PATCH 1890/2176] docs: document `IfLifting` as experimental --- src/systems/if_lifting.jl | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/systems/if_lifting.jl b/src/systems/if_lifting.jl index 130a4bdab4..c2eba035ff 100644 --- a/src/systems/if_lifting.jl +++ b/src/systems/if_lifting.jl @@ -410,6 +410,11 @@ If lifting converts (nested) if statements into a series of continuous events + Lifting proceeds through the following process: * rewrite comparisons to be of the form eqn [op] 0; subtract the RHS from the LHS * replace comparisons with generated parameters; for each comparison eqn [op] 0, generate an event (dependent on op) that sets the parameter + +!!! warn + + This is an experimental simplification pass. It may have bugs. Please open issues with + MWEs for any bugs encountered while using this. """ function IfLifting(sys::System) if !is_time_dependent(sys) From 7769b5ba0d6167a396f34acc4eff8f03672bc4fc Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Sat, 24 May 2025 15:30:33 +0530 Subject: [PATCH 1891/2176] docs: improve docstring for `mtkcompile` --- src/systems/systems.jl | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/systems/systems.jl b/src/systems/systems.jl index ceab3c6c2f..63585541e0 100644 --- a/src/systems/systems.jl +++ b/src/systems/systems.jl @@ -9,10 +9,18 @@ end """ $(SIGNATURES) -Structurally simplify algebraic equations in a system and compute the -topological sort of the observed equations in `sys`. +Compile the given system into a form that ModelingToolkit can generate code for. Also +performs a variety of symbolic-numeric enhancements. For ODEs, this includes processes +such as order reduction, index reduction, alias elimination and tearing. A subset of the +unknowns of the system may be eliminated as observables, eliminating the need for the +numerical solver to solve for these variables. + +Does not rely on metadata to identify variables/parameters/brownians. Instead, queries +the system for which symbolic quantites belong to which category. Any variables not +present in the equations of the system will be removed in this process. + +# Keyword Arguments -### Optional Keyword Arguments: + When `simplify=true`, the `simplify` function will be applied during the tearing process. + `allow_symbolic=false`, `allow_parameter=true`, and `conservative=false` limit the coefficient types during tearing. In particular, `conservative=true` limits tearing to only solve for trivial linear systems where the coefficient has the absolute value of ``1``. + `fully_determined=true` controls whether or not an error will be thrown if the number of equations don't match the number of inputs, outputs, and equations. From ac0ad247a2519fc9c25be5367a5964ac5bc7dc90 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Sat, 24 May 2025 15:30:53 +0530 Subject: [PATCH 1892/2176] refactor: export `Girsanov_transform` --- src/ModelingToolkit.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ModelingToolkit.jl b/src/ModelingToolkit.jl index 79d2afcd67..08a1b8e03e 100644 --- a/src/ModelingToolkit.jl +++ b/src/ModelingToolkit.jl @@ -296,7 +296,7 @@ export isinput, isoutput, getbounds, hasbounds, getguess, hasguess, isdisturbanc hasunit, getunit, hasconnect, getconnect, hasmisc, getmisc, state_priority export liouville_transform, change_independent_variable, substitute_component, - add_accumulations, noise_to_brownians + add_accumulations, noise_to_brownians, Girsanov_transform export PDESystem export Differential, expand_derivatives, @derivatives export Equation, ConstrainedEquation From 62a2bfaff784570cefecfbfeed5f6ce6f41edaf5 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Sat, 24 May 2025 15:31:36 +0530 Subject: [PATCH 1893/2176] refactor: move `Connection` and `connect` here instead of Symbolics --- src/ModelingToolkit.jl | 2 +- src/systems/analysis_points.jl | 6 ++--- src/systems/connectors.jl | 46 +++++++++++++++++++++++++++++++++- src/systems/systemstructure.jl | 4 +-- src/variables.jl | 6 ++--- 5 files changed, 54 insertions(+), 10 deletions(-) diff --git a/src/ModelingToolkit.jl b/src/ModelingToolkit.jl index 08a1b8e03e..c99ac17c0c 100644 --- a/src/ModelingToolkit.jl +++ b/src/ModelingToolkit.jl @@ -72,7 +72,7 @@ using RuntimeGeneratedFunctions: drop_expr using Symbolics: degree using Symbolics: _parse_vars, value, @derivatives, get_variables, exprs_occur_in, symbolic_linear_solve, build_expr, unwrap, wrap, - VariableSource, getname, variable, Connection, connect, + VariableSource, getname, variable, NAMESPACE_SEPARATOR, set_scalar_metadata, setdefaultval, initial_state, transition, activeState, entry, hasnode, ticksInState, timeInState, fixpoint_sub, fast_substitute, diff --git a/src/systems/analysis_points.jl b/src/systems/analysis_points.jl index 2ce6356aa7..12185c943f 100644 --- a/src/systems/analysis_points.jl +++ b/src/systems/analysis_points.jl @@ -176,7 +176,7 @@ function renamespace(sys, ap::AnalysisPoint) end # create analysis points via `connect` -function Symbolics.connect(in, ap::AnalysisPoint, outs...; verbose = true) +function connect(in, ap::AnalysisPoint, outs...; verbose = true) return AnalysisPoint() ~ AnalysisPoint(in, ap.name, collect(outs); verbose) end @@ -217,11 +217,11 @@ typically is not (unless the model is an inverse model). - `verbose`: Warn if an input is connected to an output (reverse causality). Silence this warning if you are analyzing an inverse model. """ -function Symbolics.connect(in::AbstractSystem, name::Symbol, out, outs...; verbose = true) +function connect(in::AbstractSystem, name::Symbol, out, outs...; verbose = true) return AnalysisPoint() ~ AnalysisPoint(in, name, [out; collect(outs)]; verbose) end -function Symbolics.connect( +function connect( in::ConnectableSymbolicT, name::Symbol, out::ConnectableSymbolicT, outs::ConnectableSymbolicT...; verbose = true) allvars = (in, out, outs...) diff --git a/src/systems/connectors.jl b/src/systems/connectors.jl index 473b1bd680..af5be85096 100644 --- a/src/systems/connectors.jl +++ b/src/systems/connectors.jl @@ -1,4 +1,48 @@ using Symbolics: StateMachineOperator + +""" + $(TYPEDEF) + +Struct used to represent a connection equation. A connection equation is an `Equation` +where the LHS is an empty `Connection(nothing)` and the RHS is a `Connection` containing +the connected connectors. + +For special types of connections, the LHS `Connection` can contain relevant metadata. +""" +struct Connection + systems::Any +end + +Base.broadcastable(x::Connection) = Ref(x) +Connection() = Connection(nothing) +Base.hash(c::Connection, seed::UInt) = hash(c.systems, (0xc80093537bdc1311 % UInt) ⊻ seed) +Symbolics.hide_lhs(_::Connection) = true + +""" + $(TYPEDSIGNATURES) + +Connect multiple connectors created via `@connector`. All connected connectors +must be unique. +""" +function connect(sys1::AbstractSystem, sys2::AbstractSystem, syss::AbstractSystem...) + syss = (sys1, sys2, syss...) + length(unique(nameof, syss)) == length(syss) || error("connect takes distinct systems!") + Equation(Connection(), Connection(syss)) # the RHS are connected systems +end + +function Base.show(io::IO, c::Connection) + print(io, "connect(") + if c.systems isa AbstractArray || c.systems isa Tuple + n = length(c.systems) + for (i, s) in enumerate(c.systems) + str = join(split(string(nameof(s)), NAMESPACE_SEPARATOR), '.') + print(io, str) + i != n && print(io, ", ") + end + end + print(io, ")") +end + isconnection(_) = false isconnection(_::Connection) = true """ @@ -188,7 +232,7 @@ var1 ~ var3 # ... ``` """ -function Symbolics.connect(var1::ConnectableSymbolicT, var2::ConnectableSymbolicT, +function connect(var1::ConnectableSymbolicT, var2::ConnectableSymbolicT, vars::ConnectableSymbolicT...) allvars = (var1, var2, vars...) validate_causal_variables_connection(allvars) diff --git a/src/systems/systemstructure.jl b/src/systems/systemstructure.jl index 16551008e9..4b01917bcf 100644 --- a/src/systems/systemstructure.jl +++ b/src/systems/systemstructure.jl @@ -1,11 +1,11 @@ using DataStructures -using Symbolics: linear_expansion, unwrap, Connection +using Symbolics: linear_expansion, unwrap using SymbolicUtils: iscall, operation, arguments, Symbolic using SymbolicUtils: quick_cancel, maketerm using ..ModelingToolkit import ..ModelingToolkit: isdiffeq, var_from_nested_derivative, vars!, flatten, value, InvalidSystemException, isdifferential, _iszero, - isparameter, + isparameter, Connection, independent_variables, SparseMatrixCLIL, AbstractSystem, equations, isirreducible, input_timedomain, TimeDomain, InferredTimeDomain, diff --git a/src/variables.jl b/src/variables.jl index 746548b432..104616f6e4 100644 --- a/src/variables.jl +++ b/src/variables.jl @@ -102,7 +102,7 @@ potential is equated at a junction. For more information, refer to the [Connection semantics](@ref connect_semantics) section of the docs. -See also: [`Symbolics.connect`](@ref), [`@connector`](@ref), [`Flow`](@ref), +See also: [`connect`](@ref), [`@connector`](@ref), [`Flow`](@ref), [`Stream`](@ref). """ struct Equality <: AbstractConnectType end # Equality connection @@ -118,7 +118,7 @@ for current flowing in and out of the function). For more information, refer to the [Connection semantics](@ref connect_semantics) section of the docs. -See also: [`Symbolics.connect`](@ref), [`@connector`](@ref), [`Equality`](@ref), +See also: [`connect`](@ref), [`@connector`](@ref), [`Equality`](@ref), [`Stream`](@ref). """ struct Flow <: AbstractConnectType end # sum to 0 @@ -132,7 +132,7 @@ the variable is part of a special stream connector. For more information, refer to the [Connection semantics](@ref connect_semantics) section of the docs. -See also: [`Symbolics.connect`](@ref), [`@connector`](@ref), [`Equality`](@ref), +See also: [`connect`](@ref), [`@connector`](@ref), [`Equality`](@ref), [`Flow`](@ref). """ struct Stream <: AbstractConnectType end # special stream connector From afc33dfb57cf24a04d1949af14cf0f329d056cba Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Sat, 24 May 2025 19:50:51 +0530 Subject: [PATCH 1894/2176] feat: move statemachine code to MTK --- src/ModelingToolkit.jl | 4 +- src/systems/connectors.jl | 2 - src/systems/state_machines.jl | 155 ++++++++++++++++++++++++++++++++++ 3 files changed, 157 insertions(+), 4 deletions(-) create mode 100644 src/systems/state_machines.jl diff --git a/src/ModelingToolkit.jl b/src/ModelingToolkit.jl index c99ac17c0c..033d83d20b 100644 --- a/src/ModelingToolkit.jl +++ b/src/ModelingToolkit.jl @@ -74,8 +74,7 @@ using Symbolics: _parse_vars, value, @derivatives, get_variables, exprs_occur_in, symbolic_linear_solve, build_expr, unwrap, wrap, VariableSource, getname, variable, NAMESPACE_SEPARATOR, set_scalar_metadata, setdefaultval, - initial_state, transition, activeState, entry, hasnode, - ticksInState, timeInState, fixpoint_sub, fast_substitute, + hasnode, fixpoint_sub, fast_substitute, CallWithMetadata, CallWithParent const NAMESPACE_SEPARATOR_SYMBOL = Symbol(NAMESPACE_SEPARATOR) import Symbolics: rename, get_variables!, _solve, hessian_sparsity, @@ -163,6 +162,7 @@ include("systems/parameter_buffer.jl") include("systems/abstractsystem.jl") include("systems/model_parsing.jl") include("systems/connectors.jl") +include("systems/state_machines.jl") include("systems/analysis_points.jl") include("systems/imperative_affect.jl") include("systems/callbacks.jl") diff --git a/src/systems/connectors.jl b/src/systems/connectors.jl index af5be85096..9761d18493 100644 --- a/src/systems/connectors.jl +++ b/src/systems/connectors.jl @@ -1,5 +1,3 @@ -using Symbolics: StateMachineOperator - """ $(TYPEDEF) diff --git a/src/systems/state_machines.jl b/src/systems/state_machines.jl new file mode 100644 index 0000000000..347f92e6f8 --- /dev/null +++ b/src/systems/state_machines.jl @@ -0,0 +1,155 @@ +_nameof(s) = nameof(s) +_nameof(s::Union{Int, Symbol}) = s +abstract type StateMachineOperator end +Base.broadcastable(x::StateMachineOperator) = Ref(x) +Symbolics.hide_lhs(_::StateMachineOperator) = true +struct InitialState <: StateMachineOperator + s::Any +end +Base.show(io::IO, s::InitialState) = print(io, "initial_state(", _nameof(s.s), ")") +initial_state(s) = Equation(InitialState(nothing), InitialState(s)) + +Base.@kwdef struct Transition{A, B, C} <: StateMachineOperator + from::A = nothing + to::B = nothing + cond::C = nothing + immediate::Bool = true + reset::Bool = true + synchronize::Bool = false + priority::Int = 1 + function Transition(from, to, cond, immediate, reset, synchronize, priority) + cond = unwrap(cond) + new{typeof(from), typeof(to), typeof(cond)}(from, to, cond, immediate, + reset, synchronize, + priority) + end +end +function Base.:(==)(transition1::Transition, transition2::Transition) + transition1.from == transition2.from && + transition1.to == transition2.to && + isequal(transition1.cond, transition2.cond) && + transition1.immediate == transition2.immediate && + transition1.reset == transition2.reset && + transition1.synchronize == transition2.synchronize && + transition1.priority == transition2.priority +end + +""" + transition(from, to, cond; immediate::Bool = true, reset::Bool = true, synchronize::Bool = false, priority::Int = 1) + +Create a transition from state `from` to state `to` that is enabled when transitioncondition `cond` evaluates to `true`. + +# Arguments: +- `from`: The source state of the transition. +- `to`: The target state of the transition. +- `cond`: A transition condition that evaluates to a Bool, such as `ticksInState() >= 2`. +- `immediate`: If `true`, the transition will fire at the same tick as it becomes true, if `false`, the actions of the state are evaluated first, and the transition fires during the next tick. +- `reset`: If true, the destination state `to` is reset to its initial condition when the transition fires. +- `synchronize`: If true, the transition will only fire if all sub-state machines in the source state are in their final (terminal) state. A final state is one that has no outgoing transitions. +- `priority`: If a state has more than one outgoing transition, all outgoing transitions must have a unique priority. The transitions are evaluated in priority order, i.e., the transition with priority 1 is evaluated first. +""" +function transition(from, to, cond; + immediate::Bool = true, reset::Bool = true, synchronize::Bool = false, + priority::Int = 1) + Equation( + Transition(), Transition(; from, to, cond, immediate, reset, + synchronize, priority)) +end +function Base.show(io::IO, s::Transition) + print(io, _nameof(s.from), " → ", _nameof(s.to), " if (", s.cond, ") [") + print(io, "immediate: ", Int(s.immediate), ", ") + print(io, "reset: ", Int(s.reset), ", ") + print(io, "sync: ", Int(s.synchronize), ", ") + print(io, "prio: ", s.priority, "]") +end + +function activeState end +function entry end +function ticksInState end +function timeInState end + +for (s, T) in [(:timeInState, :Real), + (:ticksInState, :Integer), + (:entry, :Bool), + (:activeState, :Bool)] + seed = hash(s) + @eval begin + $s(x) = wrap(term($s, x)) + SymbolicUtils.promote_symtype(::typeof($s), _...) = $T + function SymbolicUtils.show_call(io, ::typeof($s), args) + if isempty(args) + print(io, $s, "()") + else + arg = only(args) + print(io, $s, "(", arg isa Number ? arg : nameof(arg), ")") + end + end + end + if s != :activeState + @eval $s() = wrap(term($s)) + end +end + +@doc """ + timeInState() + timeInState(state) + +Get the time (in seconds) spent in a state in a finite state machine. + +When used to query the time spent in the enclosing state, the method without arguments is used, i.e., +```julia +@mtkmodel FSM begin + ... + @equations begin + var(k+1) ~ timeInState() >= 2 ? 0.0 : var(k) + end +end +``` + +If used to query the residence time of another state, the state is passed as an argument. + +This operator can be used in both equations and transition conditions. + +See also [`ticksInState`](@ref) and [`entry`](@ref) +""" timeInState + +@doc """ + ticksInState() + ticksInState(state) + +Get the number of ticks spent in a state in a finite state machine. + +When used to query the number of ticks spent in the enclosing state, the method without arguments is used, i.e., +```julia +@mtkmodel FSM begin + ... + @equations begin + var(k+1) ~ ticksInState() >= 2 ? 0.0 : var(k) + end +end +``` + +If used to query the number of ticks in another state, the state is passed as an argument. + +This operator can be used in both equations and transition conditions. + +See also [`timeInState`](@ref) and [`entry`](@ref) +""" ticksInState + +@doc """ + entry() + entry(state) + +When used in a finite-state machine, this operator returns true at the first tick when the state is active, and false otherwise. + +When used to query the entry of the enclosing state, the method without arguments is used, when used to query the entry of another state, the state is passed as an argument. + +This can be used to perform a unique action when entering a state. +""" +entry + +@doc """ + activeState(state) + +When used in a finite state machine, this operator returns `true` if the queried state is active and false otherwise. +""" activeState From a6235ea5dfdbd5eba36aaf957534c2ccc7aa1e80 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 26 May 2025 11:47:16 +0530 Subject: [PATCH 1895/2176] docs: remove old `BVProblem`, `SteadyStateProblem` docstrings --- src/problems/bvproblem.jl | 45 -------------------------------------- src/problems/odeproblem.jl | 14 ------------ 2 files changed, 59 deletions(-) diff --git a/src/problems/bvproblem.jl b/src/problems/bvproblem.jl index 30bd7e61c3..7040b32ffd 100644 --- a/src/problems/bvproblem.jl +++ b/src/problems/bvproblem.jl @@ -1,48 +1,3 @@ -""" -```julia -SciMLBase.BVProblem{iip}(sys::AbstractSystem, u0map, tspan, - parammap = DiffEqBase.NullParameters(); - constraints = nothing, guesses = nothing, - version = nothing, tgrad = false, - jac = true, sparse = true, - simplify = false, - kwargs...) where {iip} -``` - -Create a boundary value problem from the [`System`](@ref). - -`u0map` is used to specify fixed initial values for the states. Every variable -must have either an initial guess supplied using `guesses` or a fixed initial -value specified using `u0map`. - -Boundary value conditions are supplied to Systems in the form of a list of constraints. -These equations should specify values that state variables should take at specific points, -as in `x(0.5) ~ 1`). More general constraints that should hold over the entire solution, -such as `x(t)^2 + y(t)^2`, should be specified as one of the equations used to build the -`System`. - -If a `System` without `constraints` is specified, it will be treated as an initial value problem. - -```julia - @parameters g t_c = 0.5 - @variables x(..) y(t) λ(t) - eqs = [D(D(x(t))) ~ λ * x(t) - D(D(y)) ~ λ * y - g - x(t)^2 + y^2 ~ 1] - cstr = [x(0.5) ~ 1] - @mtkcompile pend = System(eqs, t; constraints = cstrs) - - tspan = (0.0, 1.5) - u0map = [x(t) => 0.6, y => 0.8] - parammap = [g => 1] - guesses = [λ => 1] - - bvp = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}(pend, u0map, tspan, parammap; guesses, check_length = false) -``` - -If the `System` has algebraic equations, like `x(t)^2 + y(t)^2`, the resulting -`BVProblem` must be solved using BVDAE solvers, such as Ascher. -""" @fallback_iip_specialize function SciMLBase.BVProblem{iip, spec}( sys::System, op, tspan; check_compatibility = true, cse = true, diff --git a/src/problems/odeproblem.jl b/src/problems/odeproblem.jl index e2901292ee..fbe7af5dec 100644 --- a/src/problems/odeproblem.jl +++ b/src/problems/odeproblem.jl @@ -83,20 +83,6 @@ end maybe_codegen_scimlproblem(expression, ODEProblem{iip}, args; kwargs...) end -""" -```julia -SciMLBase.SteadyStateProblem(sys::System, u0map, - parammap = DiffEqBase.NullParameters(); - version = nothing, tgrad = false, - jac = false, - checkbounds = false, sparse = false, - linenumbers = true, parallel = SerialForm(), - kwargs...) where {iip} -``` - -Generates an SteadyStateProblem from a `System` of ODEs and allows for automatically -symbolically calculating numerical enhancements. -""" @fallback_iip_specialize function DiffEqBase.SteadyStateProblem{iip, spec}( sys::System, op; check_length = true, check_compatibility = true, expression = Val{false}, kwargs...) where {iip, spec} From 228b2629f90b2e45dc451fbe3c3dd2a8dbbfde44 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 26 May 2025 11:47:34 +0530 Subject: [PATCH 1896/2176] fix: fix bugs in `HomotopyNonlinearFunction` --- .../nonlinear/homotopy_continuation.jl | 20 +++---------------- 1 file changed, 3 insertions(+), 17 deletions(-) diff --git a/src/systems/nonlinear/homotopy_continuation.jl b/src/systems/nonlinear/homotopy_continuation.jl index d74e4bd226..41ec50052d 100644 --- a/src/systems/nonlinear/homotopy_continuation.jl +++ b/src/systems/nonlinear/homotopy_continuation.jl @@ -472,22 +472,8 @@ function handle_rational_polynomials(x, wrt; fraction_cancel_fn = simplify_fract return num, den end -function SciMLBase.HomotopyNonlinearFunction(sys::System, args...; kwargs...) - ODEFunction{true}(sys, args...; kwargs...) -end - -function SciMLBase.HomotopyNonlinearFunction{true}(sys::System, args...; - kwargs...) - ODEFunction{true, SciMLBase.AutoSpecialize}(sys, args...; kwargs...) -end - -function SciMLBase.HomotopyNonlinearFunction{false}(sys::System, args...; - kwargs...) - ODEFunction{false, SciMLBase.FullSpecialize}(sys, args...; kwargs...) -end - -function SciMLBase.HomotopyNonlinearFunction{iip, specialize}( - sys::System, args...; eval_expression = false, eval_module = @__MODULE__, +@fallback_iip_specialize function SciMLBase.HomotopyNonlinearFunction{iip, specialize}( + sys::System; eval_expression = false, eval_module = @__MODULE__, p = nothing, fraction_cancel_fn = SymbolicUtils.simplify_fractions, cse = true, kwargs...) where {iip, specialize} if !iscomplete(sys) @@ -510,7 +496,7 @@ function SciMLBase.HomotopyNonlinearFunction{iip, specialize}( # we want to create f, jac etc. according to `sys2` since that will do the solving # but the `sys` inside for symbolic indexing should be the non-polynomial system - fn = NonlinearFunction{iip}(sys2; eval_expression, eval_module, cse, kwargs...) + fn = NonlinearFunction{iip}(sys2; p, eval_expression, eval_module, cse, kwargs...) obsfn = ObservedFunctionCache( sys; eval_expression, eval_module, checkbounds = get(kwargs, :checkbounds, false), cse) fn = remake(fn; sys = sys, observed = obsfn) From 298b694aa7b423f9bdd43e471eb24c9a7b54d0ac Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 26 May 2025 11:48:19 +0530 Subject: [PATCH 1897/2176] docs: add common documentation functionality for problems and functions --- src/ModelingToolkit.jl | 1 + src/problems/docs.jl | 393 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 394 insertions(+) create mode 100644 src/problems/docs.jl diff --git a/src/ModelingToolkit.jl b/src/ModelingToolkit.jl index 033d83d20b..306a1f5bc7 100644 --- a/src/ModelingToolkit.jl +++ b/src/ModelingToolkit.jl @@ -168,6 +168,7 @@ include("systems/imperative_affect.jl") include("systems/callbacks.jl") include("systems/system.jl") include("systems/codegen_utils.jl") +include("problems/docs.jl") include("systems/codegen.jl") include("systems/problem_utils.jl") include("linearization.jl") diff --git a/src/problems/docs.jl b/src/problems/docs.jl new file mode 100644 index 0000000000..94b77b8772 --- /dev/null +++ b/src/problems/docs.jl @@ -0,0 +1,393 @@ +const U0_P_DOCS = """ +The order of unknowns is determined by `unknowns(sys)`. If the system is split +[`is_split`](@ref) create an [`MTKParameters`](@ref) object. Otherwise, a parameter vector. +Initial values provided in terms of other variables will be symbolically evaluated. +The type of `op` will be used to determine the type of the containers. For example, if +given as an `SArray` of key-value pairs, `u0` will be an appropriately sized `SVector` +and the parameter object will be an `MTKParameters` object with `SArray`s inside. +""" + +const EVAL_EXPR_MOD_KWARGS = """ +- `eval_expression`: Whether to compile any functions via `eval` or + `RuntimeGeneratedFunctions`. +- `eval_module`: If `eval_expression == true`, the module to `eval` into. Otherwise, the + module in which to generate the `RuntimeGeneratedFunction`. +""" + +const INITIALIZEPROB_KWARGS = """ +- `guesses`: The guesses for variables in the system, used as initial values for the + initialization problem. +- `warn_initialize_determined`: Warn if the initialization system is under/over-determined. +- `initialization_eqs`: Extra equations to use in the initialization problem. +- `fully_determined`: Override whether the initialization system is fully determined. +- `use_scc`: Whether to use `SCCNonlinearProblem` for initialization if the system is fully + determined. +""" + +const PROBLEM_KWARGS = """ +$EVAL_EXPR_MOD_KWARGS +$INITIALIZEPROB_KWARGS +- `check_initialization_units`: Enable or disable unit checks when constructing the + initialization problem. +- `tofloat`: Passed to [`varmap_to_vars`](@ref) when building the parameter vector of + a non-split system. +- `u0_eltype`: The `eltype` of the `u0` vector. If `nothing`, finds the promoted floating point + type from `op`. +- `u0_constructor`: A function to apply to the `u0` value returned from + [`varmap_to_vars`](@ref). + to construct the final `u0` value. +- `p_constructor`: A function to apply to each array buffer created when constructing the + parameter object. +- `warn_cyclic_dependency`: Whether to emit a warning listing out cycles in initial + conditions provided for unknowns and parameters. +- `circular_dependency_max_cycle_length`: Maximum length of cycle to check for. Only + applicable if `warn_cyclic_dependency == true`. +- `circular_dependency_max_cycles`: Maximum number of cycles to check for. Only applicable + if `warn_cyclic_dependency == true`. +- `substitution_limit`: The number times to substitute initial conditions into each other + to attempt to arrive at a numeric value. +""" + +const TIME_DEPENDENT_PROBLEM_KWARGS = """ +- `callback`: An extra callback or `CallbackSet` to add to the problem, in addition to the + ones defined symbolically in the system. +""" + +const PROBLEM_INTERNALS_HEADER = """ +# Extended docs + +The following API is internal and may change or be removed without notice. Its usage is +highly discouraged. +""" + +const INTERNAL_INITIALIZEPROB_KWARGS = """ +- `time_dependent_init`: Whether to build a time-dependent initialization for the problem. A + time-dependent initialization solves for a consistent `u0`, whereas a time-independent one + only runs parameter initialization. +- `algebraic_only`: Whether to build the initialization problem using only algebraic equations. +- `allow_incomplete`: Whether to allow incomplete initialization problems. +""" + +const PROBLEM_INTERNAL_KWARGS = """ +- `build_initializeprob`: If `false`, avoids building the initialization problem. +- `check_length`: Whether to check the number of equations along with number of unknowns and + length of `u0` vector for consistency. If `false`, do not check with equations. This is + forwarded to `check_eqs_u0`. +$INTERNAL_INITIALIZEPROB_KWARGS +""" + +function problem_ctors(prob, istd) + if istd + """ + SciMLBase.$prob(sys::System, op, tspan::NTuple{2}; kwargs...) + SciMLBase.$prob{iip}(sys::System, op, tspan::NTuple{2}; kwargs...) + SciMLBase.$prob{iip, specialize}(sys::System, op, tspan::NTuple{2}; kwargs...) + """ + else + """ + SciMLBase.$prob(sys::System, op; kwargs...) + SciMLBase.$prob{iip}(sys::System, op; kwargs...) + SciMLBase.$prob{iip, specialize}(sys::System, op; kwargs...) + """ + end +end + +function prob_fun_common_kwargs(T, istd) + return """ + - `check_compatibility`: Whether to check if the given system `sys` contains all the + information necessary to create a `$T` and no more. If disabled, assumes that `sys` + at least contains the necessary information. + - `expression`: `Val{true}` to return an `Expr` that constructs the corresponding + problem instead of the problem itself. `Val{false}` otherwise. + $(istd ? " Constructing the expression does not support callbacks" : "") + """ +end + +function problem_docstring(prob, func, istd; init = true, extra_body = "") + if func isa DataType + func = "`$func`" + end + return """ + $(problem_ctors(prob, istd)) + + Build a `$prob` given a system `sys` and operating point `op` + $(istd ? " and timespan `tspan`" : ""). `iip` is a boolean indicating whether the + problem should be in-place. `specialization` is a `SciMLBase.AbstractSpecalize` subtype + indicating the level of specialization of the $func. The operating point should be an + iterable collection of key-value pairs mapping variables/parameters in the system to the + (initial) values they should take in `$prob`. Any values not provided will fallback to + the corresponding default (if present). + + $(init ? istd ? TIME_DEPENDENT_INIT : TIME_INDEPENDENT_INIT : "") + + $extra_body + + # Keyword arguments + + $PROBLEM_KWARGS + $(istd ? TIME_DEPENDENT_PROBLEM_KWARGS : "") + $(prob_fun_common_kwargs(prob, istd)) + + All other keyword arguments are forwarded to the $func constructor. + + $PROBLEM_INTERNALS_HEADER + + $PROBLEM_INTERNAL_KWARGS + """ +end + +const TIME_DEPENDENT_INIT = """ +ModelingToolkit will build an initialization problem where all initial values for +unknowns or observables of `sys` (either explicitly provided or in defaults) will +be constraints. To remove an initial condition in the defaults (without providing +a replacement) give the corresponding variable a value of `nothing` in the operating +point. The initialization problem will also run parameter initialization. See the +[Initialization](@ref initialization) documentation for more information. +""" + +const TIME_INDEPENDENT_INIT = """ +ModelingToolkit will build an initialization problem that will run parameter +initialization. Since it does not solve for initial values of unknowns, observed +equations will not be initialization constraints. If an initialization equation +of the system must involve the initial value of an unknown `x`, it must be used as +`Initial(x)` in the equation. For example, an equation to be used to solve for parameter +`p` in terms of unknowns `x` and `y` must be provided as `Initial(x) + Initial(y) ~ p` +instead of `x + y ~ p`. See the [Initialization](@ref initialization) documentation +for more information. +""" + +const BV_EXTRA_BODY = """ +Boundary value conditions are supplied to Systems in the form of a list of constraints. +These equations should specify values that state variables should take at specific points, +as in `x(0.5) ~ 1`). More general constraints that should hold over the entire solution, +such as `x(t)^2 + y(t)^2`, should be specified as one of the equations used to build the +`System`. + +If a `System` without `constraints` is specified, it will be treated as an initial value problem. + +```julia + @parameters g t_c = 0.5 + @variables x(..) y(t) λ(t) + eqs = [D(D(x(t))) ~ λ * x(t) + D(D(y)) ~ λ * y - g + x(t)^2 + y^2 ~ 1] + cstr = [x(0.5) ~ 1] + @mtkcompile pend = System(eqs, t; constraints = cstrs) + + tspan = (0.0, 1.5) + u0map = [x(t) => 0.6, y => 0.8] + parammap = [g => 1] + guesses = [λ => 1] + + bvp = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}(pend, u0map, tspan, parammap; guesses, check_length = false) +``` + +If the `System` has algebraic equations, like `x(t)^2 + y(t)^2`, the resulting +`BVProblem` must be solved using BVDAE solvers, such as Ascher. +""" + +for (mod, prob, func, istd, kws) in [ + (SciMLBase, :ODEProblem, ODEFunction, true, (;)), + (SciMLBase, :SteadyStateProblem, ODEFunction, false, (;)), + (SciMLBase, :BVProblem, ODEFunction, true, + (; init = false, extra_body = BV_EXTRA_BODY)), + (SciMLBase, :DAEProblem, DAEFunction, true, (;)), + (SciMLBase, :DDEProblem, DDEFunction, true, (;)), + (SciMLBase, :SDEProblem, SDEFunction, true, (;)), + (SciMLBase, :SDDEProblem, SDDEFunction, true, (;)), + (JumpProcesses, :JumpProblem, "inner SciMLFunction", true, (; init = false)), + (SciMLBase, :DiscreteProblem, DiscreteFunction, true, (;)), + (SciMLBase, :ImplicitDiscreteProblem, ImplicitDiscreteFunction, true, (;)), + (SciMLBase, :NonlinearProblem, NonlinearFunction, false, (;)), + (SciMLBase, :NonlinearLeastSquaresProblem, NonlinearFunction, false, (;)), + (SciMLBase, :SCCNonlinearProblem, NonlinearFunction, false, (; init = false)), + (SciMLBase, :OptimizationProblem, OptimizationFunction, false, (; init = false)) +] + @eval @doc problem_docstring($mod.$prob, $func, $istd) $mod.$prob +end + +function function_docstring(func, istd, optionals) + return """ + $func(sys::System; kwargs...) + $func{iip}(sys::System; kwargs...) + $func{iip, specialize}(sys::System; kwargs...) + + Create a `$func` from the given `sys`. `iip` is a boolean indicating whether the + function should be in-place. `specialization` is a `SciMLBase.AbstractSpecalize` + subtype indicating the level of specialization of the $func. + + # Keyword arguments + + - `u0`: The `u0` vector for the corresponding problem, if available. Can be obtained + using [`ModelingToolkit.get_u0`](@ref). + - `p`: The parameter object for the corresponding problem, if available. Can be obtained + using [`ModelingToolkit.get_p`](@ref). + $(istd ? TIME_DEPENDENT_FUNCTION_KWARGS : "") + $EVAL_EXPR_MOD_KWARGS + - `checkbounds`: Whether to enable bounds checking in the generated code. + - `simplify`: Whether to `simplify` any symbolically computed jacobians/hessians/etc. + - `cse`: Whether to enable Common Subexpression Elimination (CSE) on the generated code. + This typically improves performance of the generated code but reduces readability. + - `sparse`: Whether to generate jacobian/hessian/etc. functions that return/operate on + sparse matrices. Also controls whether the mass matrix is sparse, wherever applicable. + $(prob_fun_common_kwargs(func, istd)) + $(process_optional_function_kwargs(optionals)) + + All other keyword arguments are forwarded to the `$func` struct constructor. + """ +end + +const TIME_DEPENDENT_FUNCTION_KWARGS = """ +- `t`: The initial time for the corresponding problem, if available. +""" + +const JAC_KWARGS = """ +- `jac`: Whether to symbolically compute and generate code for the jacobian function. +""" + +const TGRAD_KWARGS = """ +- `tgrad`: Whether to symbolically compute and generate code for the `tgrad` function. +""" + +const SPARSITY_KWARGS = """ +- `sparsity`: Whether to provide symbolically compute and provide sparsity patterns for the + jacobian/hessian/etc. +""" + +const RESID_PROTOTYPE_KWARGS = """ +- `resid_prototype`: The prototype of the residual function `f` for a problem involving a + nonlinear solve where the residual and `u0` have different sizes. +""" + +const GRAD_KWARGS = """ +- `grad`: Whether the symbolically compute and generate code for the gradient of the cost + function with respect to unknowns. +""" + +const HESS_KWARGS = """ +- `hess`: Whether to symbolically compute and generate code for the hessian function. +""" + +const CONSH_KWARGS = """ +- `cons_h`: Whether to symbolically compute and generate code for the hessian function of + constraints. Since the constraint function is vector-valued, the hessian is a vector + of hessian matrices. +""" + +const CONSJ_KWARGS = """ +- `cons_j`: Whether to symbolically compute and generate code for the jacobian function of + constraints. +""" + +const CONSSPARSE_KWARGS = """ +- `cons_sparse`: Identical to the `sparse` keyword, but specifically for jacobian/hessian + functions of the constraints. +""" + +const INPUTFN_KWARGS = """ +- `inputs`: The variables in the input vector. The system must have been simplified using + `mtkcompile` with these variables passed as `inputs`. +- `disturbance_inputs`: The disturbance input variables. The system must have been + simplified using `mtkcompile` with these variables passed as `disturbance_inputs`. +""" + +const CONTROLJAC_KWARGS = """ +- `controljac`: Whether to symbolically compute and generate code for the jacobian of + the ODE with respect to the inputs. +""" + +const OPTIONAL_FN_KWARGS_DICT = Dict( + :jac => JAC_KWARGS, + :tgrad => TGRAD_KWARGS, + :sparsity => SPARSITY_KWARGS, + :resid_prototype => RESID_PROTOTYPE_KWARGS, + :grad => GRAD_KWARGS, + :hess => HESS_KWARGS, + :cons_h => CONSH_KWARGS, + :cons_j => CONSJ_KWARGS, + :cons_sparse => CONSSPARSE_KWARGS, + :inputfn => INPUTFN_KWARGS, + :controljac => CONTROLJAC_KWARGS +) + +const SPARSITY_OPTIONALS = Set([:jac, :hess, :cons_h, :cons_j, :controljac]) + +const CONS_SPARSITY_OPTIONALS = Set([:cons_h, :cons_j]) + +function process_optional_function_kwargs(choices::Vector{Symbol}) + if !isdisjoint(choices, SPARSITY_OPTIONALS) + push!(choices, :sparsity) + end + if !isdisjoint(choices, CONS_SPARSITY_OPTIONALS) + push!(choices, :cons_sparse) + end + join(map(Base.Fix1(getindex, OPTIONAL_FN_KWARGS_DICT), choices), "\n") +end + +for (mod, func, istd, optionals) in [ + (SciMLBase, :ODEFunction, true, [:jac, :tgrad]), + (SciMLBase, :ODEInputFunction, true, [:inputfn, :jac, :tgrad, :controljac]), + (SciMLBase, :DAEFunction, true, [:jac, :tgrad]), + (SciMLBase, :DDEFunction, true, Symbol[]), + (SciMLBase, :SDEFunction, true, [:jac, :tgrad]), + (SciMLBase, :SDDEFunction, true, Symbol[]), + (SciMLBase, :DiscreteFunction, true, Symbol[]), + (SciMLBase, :ImplicitDiscreteFunction, true, Symbol[]), + (SciMLBase, :NonlinearFunction, false, [:resid_prototype, :jac]), + (SciMLBase, :IntervalNonlinearFunction, false, Symbol[]), + (SciMLBase, :OptimizationFunction, false, [:jac, :grad, :hess, :cons_h, :cons_j]) +] + @eval @doc function_docstring($mod.$func, $istd, $optionals) $mod.$func +end + +@doc """ + SciMLBase.HomotopyNonlinearFunction(sys::System; kwargs...) + SciMLBase.HomotopyNonlinearFunction{iip}(sys::System; kwargs...) + SciMLBase.HomotopyNonlinearFunction{iip, specialize}(sys::System; kwargs...) + +Create a `HomotopyNonlinearFunction` from the given `sys`. `iip` is a boolean indicating +whether the function should be in-place. `specialization` is a `SciMLBase.AbstractSpecalize` +subtype indicating the level of specialization of the $func. + +# Keyword arguments + +- `u0`: The `u0` vector for the corresponding problem, if available. Can be obtained + using [`ModelingToolkit.get_u0`](@ref). +- `p`: The parameter object for the corresponding problem, if available. Can be obtained + using [`ModelingToolkit.get_p`](@ref). +$EVAL_EXPR_MOD_KWARGS +- `checkbounds`: Whether to enable bounds checking in the generated code. +- `simplify`: Whether to `simplify` any symbolically computed jacobians/hessians/etc. +- `cse`: Whether to enable Common Subexpression Elimination (CSE) on the generated code. + This typically improves performance of the generated code but reduces readability. +- `fraction_cancel_fn`: The function to use to simplify fractions in the polynomial + expression. A more powerful function can increase processing time but be able to + eliminate more rational functions, thus improving solve time. Should be a function that + takes a symbolic expression containing zero or more fraction expressions and returns the + simplified expression. While this defaults to `SymbolicUtils.simplify_fractions`, a viable + alternative is `SymbolicUtils.quick_cancel` + +All keyword arguments are forwarded to the wrapped `NonlinearFunction` constructor. +""" SciMLBase.HomotopyNonlinearFunction + +@doc """ + SciMLBase.IntervalNonlinearProblem(sys::System, uspan::NTuple{2}, parammap = SciMLBase.NullParameters(); kwargs...) + +Create an `IntervalNonlinearProblem` from the given `sys`. This is only valid for a system +of nonlinear equations with a single equation and unknown. `uspan` is the interval in which +the root is to be found, and `parammap` is an iterable collection of key-value pairs +providing values for the parameters in the system. + +$TIME_INDEPENDENT_INIT + +# Keyword arguments + +$PROBLEM_KWARGS +$(prob_fun_common_kwargs(IntervalNonlinearProblem, false)) + +All other keyword arguments are forwarded to the `IntervalNonlinearFunction` constructor. + +$PROBLEM_INTERNALS_HEADER + +$PROBLEM_INTERNAL_KWARGS +""" SciMLBase.IntervalNonlinearProblem From 36b49434a6ff49ccd5dfcd889c1d8029e3a2894a Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 26 May 2025 11:48:35 +0530 Subject: [PATCH 1898/2176] docs: use common docs in `process_SciMLProblem` docstring --- src/systems/problem_utils.jl | 52 +++++------------------------------- 1 file changed, 7 insertions(+), 45 deletions(-) diff --git a/src/systems/problem_utils.jl b/src/systems/problem_utils.jl index 76ade8e910..c1ca718b0a 100644 --- a/src/systems/problem_utils.jl +++ b/src/systems/problem_utils.jl @@ -1188,58 +1188,20 @@ Return the SciMLFunction created via calling `constructor`, the initial conditio and parameter object `p` given the system `sys`, and user-provided initial values `u0map` and `pmap`. `u0map` and `pmap` are converted into variable maps via [`to_varmap`](@ref). -The order of unknowns is determined by `unknowns(sys)`. If the system is split -[`is_split`](@ref) create an [`MTKParameters`](@ref) object. Otherwise, a parameter vector. -Initial values provided in terms of other variables will be symbolically evaluated using -[`evaluate_varmap!`](@ref). The type of `u0map` and `pmap` will be used to determine the -type of the containers (if parameters are not in an `MTKParameters` object). `Dict`s will be -turned into `Array`s. +$U0_P_DOCS This will also build the initialization problem and related objects and pass them to the SciMLFunction as keyword arguments. Keyword arguments: -- `build_initializeprob`: If `false`, avoids building the initialization problem. -- `t`: The initial time of the `ODEProblem`. If this is not provided, the initialization - problem cannot be built. +$PROBLEM_KWARGS +$PROBLEM_INTERNAL_KWARGS +- `t`: The initial time of the `SciMLProblem`. This does not need to be provided for time- + independent problems. If not provided for time-dependent problems, will be assumed as + zero. - `implicit_dae`: Also build a mapping of derivatives of states to values for implicit DAEs. - Changes the return value of this function to `(f, du0, u0, p)` instead of - `(f, u0, p)`. -- `guesses`: The guesses for variables in the system, used as initial values for the - initialization problem. -- `warn_initialize_determined`: Warn if the initialization system is under/over-determined. -- `initialization_eqs`: Extra equations to use in the initialization problem. -- `eval_expression`: Whether to compile any functions via `eval` or `RuntimeGeneratedFunctions`. -- `eval_module`: If `eval_expression == true`, the module to `eval` into. Otherwise, the module - in which to generate the `RuntimeGeneratedFunction`. -- `fully_determined`: Override whether the initialization system is fully determined. -- `check_initialization_units`: Enable or disable unit checks when constructing the - initialization problem. -- `tofloat`: Passed to [`varmap_to_vars`](@ref) when building the parameter vector of - a non-split system. -- `u0_eltype`: The `eltype` of the `u0` vector. If `nothing`, finds the promoted floating point - type from `op`. -- `u0_constructor`: A function to apply to the `u0` value returned from `varmap_to_vars` - to construct the final `u0` value. -- `p_constructor`: A function to apply to each array buffer created when constructing the parameter object. -- `check_length`: Whether to check the number of equations along with number of unknowns and - length of `u0` vector for consistency. If `false`, do not check with equations. This is - forwarded to `check_eqs_u0` + Changes the return value of this function to `(f, du0, u0, p)` instead of `(f, u0, p)`. - `symbolic_u0` allows the returned `u0` to be an array of symbolics. -- `warn_cyclic_dependency`: Whether to emit a warning listing out cycles in initial - conditions provided for unknowns and parameters. -- `circular_dependency_max_cycle_length`: Maximum length of cycle to check for. - Only applicable if `warn_cyclic_dependency == true`. -- `circular_dependency_max_cycles`: Maximum number of cycles to check for. - Only applicable if `warn_cyclic_dependency == true`. -- `substitution_limit`: The number times to substitute initial conditions into each - other to attempt to arrive at a numeric value. -- `use_scc`: Whether to use `SCCNonlinearProblem` for initialization if the system is fully - determined. -- `force_initialization_time_independent`: Whether to force the initialization to not use - the independent variable of `sys`. -- `algebraic_only`: Whether to build the initialization problem using only algebraic equations. -- `allow_incomplete`: Whether to allow incomplete initialization problems. All other keyword arguments are passed as-is to `constructor`. """ From 1897db8ad30f741a18cd627022f32657c1e919c0 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 26 May 2025 11:48:46 +0530 Subject: [PATCH 1899/2176] docs: tag the initialization docs --- docs/src/tutorials/initialization.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/src/tutorials/initialization.md b/docs/src/tutorials/initialization.md index cdf6e7e813..fedb8b9a22 100644 --- a/docs/src/tutorials/initialization.md +++ b/docs/src/tutorials/initialization.md @@ -1,4 +1,4 @@ -# Initialization of ODESystems +# [Initialization of Systems](@id initialization) While for simple numerical ODEs choosing an initial condition can be an easy affair, with ModelingToolkit's more general differential-algebraic equation From 1ff060aefa34aec087dc07129a5687075aba2b3b Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 26 May 2025 12:28:44 +0530 Subject: [PATCH 1900/2176] refactor: modernize `ODEInputFunction` --- src/systems/optimal_control_interface.jl | 59 ++++++------------------ 1 file changed, 13 insertions(+), 46 deletions(-) diff --git a/src/systems/optimal_control_interface.jl b/src/systems/optimal_control_interface.jl index c88aceabff..2b460f77a2 100644 --- a/src/systems/optimal_control_interface.jl +++ b/src/systems/optimal_control_interface.jl @@ -47,16 +47,10 @@ end is_explicit(tableau) = tableau isa DiffEqBase.ExplicitRKTableau -""" -Generate the control function f(x, u, p, t) from the ODESystem. -Input variables are automatically inferred but can be manually specified. -""" -function SciMLBase.ODEInputFunction{iip, specialize}(sys::System, - dvs = unknowns(sys), - ps = parameters(sys), u0 = nothing, +@fallback_iip_specialize function SciMLBase.ODEInputFunction{iip, specialize}(sys::System; inputs = unbound_inputs(sys), - disturbance_inputs = disturbances(sys); - version = nothing, tgrad = false, + disturbance_inputs = disturbances(sys), + u0 = nothing, tgrad = false, jac = false, controljac = false, p = nothing, t = nothing, eval_expression = false, @@ -66,7 +60,6 @@ function SciMLBase.ODEInputFunction{iip, specialize}(sys::System, checkbounds = false, sparsity = false, analytic = nothing, - split_idxs = nothing, initialization_data = nothing, cse = true, kwargs...) where {iip, specialize} @@ -75,61 +68,49 @@ function SciMLBase.ODEInputFunction{iip, specialize}(sys::System, f = f[1] if tgrad - tgrad_gen = generate_tgrad(sys, dvs, ps; + _tgrad = generate_tgrad(sys; simplify = simplify, expression = Val{true}, + wrap_gfw = Val{true}, expression_module = eval_module, cse, checkbounds = checkbounds, kwargs...) - tgrad_oop, tgrad_iip = eval_or_rgf.(tgrad_gen; eval_expression, eval_module) - _tgrad = GeneratedFunctionWrapper{(2, 3, is_split(sys))}(tgrad_oop, tgrad_iip) else _tgrad = nothing end if jac - jac_gen = generate_jacobian(sys, dvs, ps; + _jac = generate_jacobian(sys; simplify = simplify, sparse = sparse, expression = Val{true}, + wrap_gfw = Val{true}, expression_module = eval_module, cse, checkbounds = checkbounds, kwargs...) - jac_oop, jac_iip = eval_or_rgf.(jac_gen; eval_expression, eval_module) - - _jac = GeneratedFunctionWrapper{(2, 3, is_split(sys))}(jac_oop, jac_iip) else _jac = nothing end if controljac - cjac_gen = generate_control_jacobian(sys, dvs, ps; + _cjac = generate_control_jacobian(sys; simplify = simplify, sparse = sparse, - expression = Val{true}, + expression = Val{true}, wrap_gfw = Val{true}, expression_module = eval_module, cse, checkbounds = checkbounds, kwargs...) - cjac_oop, cjac_iip = eval_or_rgf.(cjac_gen; eval_expression, eval_module) - - _cjac = GeneratedFunctionWrapper{(2, 3, is_split(sys))}(cjac_oop, cjac_iip) else _cjac = nothing end M = calculate_massmatrix(sys) - _M = if sparse && !(u0 === nothing || M === I) - SparseArrays.sparse(M) - elseif u0 === nothing || M === I - M - else - ArrayInterface.restructure(u0 .* u0', M) - end + _M = concrete_massmatrix(M; sparse, u0) observedfun = ObservedFunctionCache( sys; steady_state, eval_expression, eval_module, checkbounds, cse) + _W_sparsity = W_sparsity(sys) + W_prototype = calculate_W_prototype(_W_sparsity; u0, sparse) if sparse uElType = u0 === nothing ? Float64 : eltype(u0) - W_prototype = similar(W_sparsity(sys), uElType) controljac_prototype = similar(calculate_control_jacobian(sys), uElType) else - W_prototype = nothing controljac_prototype = nothing end @@ -142,25 +123,11 @@ function SciMLBase.ODEInputFunction{iip, specialize}(sys::System, jac_prototype = W_prototype, controljac_prototype = controljac_prototype, observed = observedfun, - sparsity = sparsity ? W_sparsity(sys) : nothing, + sparsity = sparsity ? _W_sparsity : nothing, analytic = analytic, initialization_data) end -function SciMLBase.ODEInputFunction(sys::System, args...; kwargs...) - ODEInputFunction{true}(sys, args...; kwargs...) -end - -function SciMLBase.ODEInputFunction{true}(sys::System, args...; - kwargs...) - ODEInputFunction{true, SciMLBase.AutoSpecialize}(sys, args...; kwargs...) -end - -function SciMLBase.ODEInputFunction{false}(sys::System, args...; - kwargs...) - ODEInputFunction{false, SciMLBase.FullSpecialize}(sys, args...; kwargs...) -end - # returns the JuMP timespan, the number of steps, and whether it is a free time problem. function process_tspan(tspan, dt, steps) is_free_time = false From 6518c996928c831332f9d69b822b7550e1890f28 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 26 May 2025 12:52:19 +0530 Subject: [PATCH 1901/2176] refactor: remove old `force_initialization_time_independent` kwarg --- src/problems/initializationproblem.jl | 3 +-- src/problems/odeproblem.jl | 2 +- src/systems/problem_utils.jl | 6 +++--- 3 files changed, 5 insertions(+), 6 deletions(-) diff --git a/src/problems/initializationproblem.jl b/src/problems/initializationproblem.jl index e957fa7652..5d6c77b9a6 100644 --- a/src/problems/initializationproblem.jl +++ b/src/problems/initializationproblem.jl @@ -28,7 +28,6 @@ initial conditions for the given DAE. check_units = true, use_scc = true, allow_incomplete = false, - force_time_independent = false, algebraic_only = false, time_dependent_init = is_time_dependent(sys), kwargs...) where {iip, specialize} @@ -58,7 +57,7 @@ initial conditions for the given DAE. # useful for `SteadyStateProblem` since `f` has to be autonomous and the # initialization should be too - if force_time_independent + if !time_dependent_init idx = findfirst(isequal(get_iv(sys)), get_ps(isys)) idx === nothing || deleteat!(get_ps(isys), idx) end diff --git a/src/problems/odeproblem.jl b/src/problems/odeproblem.jl index fbe7af5dec..e2c8f9cd8a 100644 --- a/src/problems/odeproblem.jl +++ b/src/problems/odeproblem.jl @@ -91,7 +91,7 @@ end f, u0, p = process_SciMLProblem(ODEFunction{iip}, sys, op; steady_state = true, check_length, check_compatibility, expression, - force_initialization_time_independent = true, kwargs...) + time_dependent_init = false, kwargs...) kwargs = process_kwargs(sys; expression, kwargs...) args = (; f, u0, p) diff --git a/src/systems/problem_utils.jl b/src/systems/problem_utils.jl index c1ca718b0a..6eb44893e9 100644 --- a/src/systems/problem_utils.jl +++ b/src/systems/problem_utils.jl @@ -1217,7 +1217,7 @@ function process_SciMLProblem( circular_dependency_max_cycle_length = length(all_symbols(sys)), circular_dependency_max_cycles = 10, substitution_limit = 100, use_scc = true, time_dependent_init = is_time_dependent(sys), - force_initialization_time_independent = false, algebraic_only = false, + algebraic_only = false, allow_incomplete = false, is_initializeprob = false, kwargs...) dvs = unknowns(sys) ps = parameters(sys; initial_parameters = true) @@ -1276,8 +1276,8 @@ function process_SciMLProblem( eval_expression, eval_module, fully_determined, warn_cyclic_dependency, check_units = check_initialization_units, circular_dependency_max_cycle_length, circular_dependency_max_cycles, use_scc, - force_time_independent = force_initialization_time_independent, algebraic_only, allow_incomplete, - u0_constructor, p_constructor, floatT, time_dependent_init) + algebraic_only, allow_incomplete, u0_constructor, p_constructor, floatT, + time_dependent_init) kwargs = merge(kwargs, kws) end From bf24fdf8528296d94e5ef045fcd0b1b037ed714b Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 26 May 2025 12:52:37 +0530 Subject: [PATCH 1902/2176] docs: add docstring for `HomotopyContinuationProblem` --- src/systems/nonlinear/homotopy_continuation.jl | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/systems/nonlinear/homotopy_continuation.jl b/src/systems/nonlinear/homotopy_continuation.jl index 41ec50052d..77e39e4839 100644 --- a/src/systems/nonlinear/homotopy_continuation.jl +++ b/src/systems/nonlinear/homotopy_continuation.jl @@ -514,6 +514,9 @@ end struct HomotopyContinuationProblem{iip, specialization} end +@doc problem_docstring( + HomotopyContinuationProblem, HomotopyNonlinearFunction, false; init = false) HomotopyContinuationProblem + function HomotopyContinuationProblem(sys::System, args...; kwargs...) HomotopyContinuationProblem{true}(sys, args...; kwargs...) end From 613a9b48285333dbd661fbc452dca8d06526c396 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 26 May 2025 12:52:54 +0530 Subject: [PATCH 1903/2176] docs: improve docstring for `InitializationProblem` --- src/problems/initializationproblem.jl | 34 +++++++++++++-------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/src/problems/initializationproblem.jl b/src/problems/initializationproblem.jl index 5d6c77b9a6..8b30e449b0 100644 --- a/src/problems/initializationproblem.jl +++ b/src/problems/initializationproblem.jl @@ -1,22 +1,22 @@ struct InitializationProblem{iip, specialization} end -""" -```julia -InitializationProblem{iip}(sys::AbstractSystem, t, op; - version = nothing, tgrad = false, - jac = false, - checkbounds = false, sparse = false, - simplify = false, - linenumbers = true, parallel = SerialForm(), - initialization_eqs = [], - fully_determined = false, - kwargs...) where {iip} -``` - -Generates a NonlinearProblem or NonlinearLeastSquaresProblem from a System -which represents the initialization, i.e. the calculation of the consistent -initial conditions for the given DAE. -""" +@doc """ + InitializationProblem(sys::AbstractSystem, t, op = Dict(); kwargs...) + InitializationProblem{iip}(sys::AbstractSystem, t, op = Dict(); kwargs...) + InitializationProblem{iip, specialize}(sys::AbstractSystem, t, op = Dict(); kwargs...) + +Generate a `NonlinearProblem`, `SCCNonlinearProblem` or `NonlinearLeastSquaresProblem` to +represent a consistent initialization of `sys` given the initial time `t` and operating +point `op`. The initial time can be `nothing` for time-independent systems. + +# Keyword arguments + +$INITIALIZEPROB_KWARGS +$INTERNAL_INITIALIZEPROB_KWARGS + +All other keyword arguments are forwarded to the wrapped nonlinear problem constructor. +""" InitializationProblem + @fallback_iip_specialize function InitializationProblem{iip, specialize}( sys::AbstractSystem, t, op = Dict(); From 170cf53302518f2c28cf221ecfaa58430c0f2afd Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 26 May 2025 14:14:55 +0530 Subject: [PATCH 1904/2176] refactor: disallow passing `dvs`, `ps` to `generate_*` functions, add docstrings --- src/systems/codegen.jl | 353 ++++++++++++++++++++++++++++++++++++++--- 1 file changed, 327 insertions(+), 26 deletions(-) diff --git a/src/systems/codegen.jl b/src/systems/codegen.jl index 064be6fdfb..1c4e780e64 100644 --- a/src/systems/codegen.jl +++ b/src/systems/codegen.jl @@ -1,18 +1,38 @@ +const GENERATE_X_KWARGS = """ +- `expression`: `Val{true}` if this should return an `Expr` (or tuple of `Expr`s) of the + generated code. `Val{false}` otherwise. +- `wrap_gfw`: `Val{true}` if the returned functions should be wrapped in a callable + struct to make them callable using the expected syntax. The callable struct itself is + internal API. If `expression == Val{true}`, the returned expression will construct the + callable struct. If this function returns a tuple of functions/expressions, both will + be identical if `wrap_gfw == Val{true}`. +$EVAL_EXPR_MOD_KWARGS """ - $(TYPEDSIGNATURES) -Generate the RHS function for the `equations` of a `System`. +""" + $(TYPEDSIGNATURES) -# Arguments +Generate the RHS function for the [`equations`](@ref) of a [`System`](@ref). # Keyword Arguments +$GENERATE_X_KWARGS +- `implicit_dae`: Whether the generated function should be in the implicit form. Applicable + only for ODEs/DAEs or discrete systems. Instead of `f(u, p, t)` (`f(du, u, p, t)` for the + in-place form) the function is `f(du, u, p, t)` (respectively `f(resid, du, u, p, t)`). +- `override_discrete`: Whether to assume the system is discrete regardless of + `is_discrete_system(sys)`. +- `scalar`: Whether to generate a single-out-of-place function that returns a scalar for + the only equation in the system. + +All other keyword arguments are forwarded to [`build_function_wrapper`](@ref). """ -function generate_rhs(sys::System, dvs = unknowns(sys), - ps = parameters(sys; initial_parameters = true); implicit_dae = false, +function generate_rhs(sys::System; implicit_dae = false, scalar = false, expression = Val{true}, wrap_gfw = Val{false}, eval_expression = false, eval_module = @__MODULE__, override_discrete = false, kwargs...) + dvs = unknowns(sys) + ps = parameters(sys; initial_parameters = true) eqs = equations(sys) obs = observed(sys) u = dvs @@ -81,10 +101,22 @@ function generate_rhs(sys::System, dvs = unknowns(sys), res; eval_expression, eval_module) end -function generate_diffusion_function(sys::System, dvs = unknowns(sys), - ps = parameters(sys; initial_parameters = true); expression = Val{true}, +""" + $(TYPEDSIGNATURES) + +Generate the diffusion function for the noise equations of a [`System`](@ref). + +# Keyword Arguments + +$GENERATE_X_KWARGS + +All other keyword arguments are forwarded to [`build_function_wrapper`](@ref). +""" +function generate_diffusion_function(sys::System; expression = Val{true}, wrap_gfw = Val{false}, eval_expression = false, eval_module = @__MODULE__, kwargs...) + dvs = unknowns(sys) + ps = parameters(sys; initial_parameters = true) eqs = get_noise_eqs(sys) if ndims(eqs) == 2 && size(eqs, 2) == 1 # scalar noise @@ -106,6 +138,12 @@ function generate_diffusion_function(sys::System, dvs = unknowns(sys), expression, wrap_gfw, (p_start, nargs, is_split(sys)), res; eval_expression, eval_module) end +""" + $(TYPEDSIGNATURES) + +Calculate the gradient of the equations of `sys` with respect to the independent variable. +`simplify` is forwarded to `Symbolics.expand_derivatives`. +""" function calculate_tgrad(sys::System; simplify = false) # We need to remove explicit time dependence on the unknown because when we # have `u(t) * t` we want to have the tgrad to be `u(t)` instead of `u'(t) * @@ -121,6 +159,16 @@ function calculate_tgrad(sys::System; simplify = false) return tgrad end +""" + $(TYPEDSIGNATURES) + +Calculate the jacobian of the equations of `sys`. + +# Keyword arguments + +- `simplify`, `sparse`: Forwarded to `Symbolics.jacobian`. +- `dvs`: The variables with respect to which the jacobian should be computed. +""" function calculate_jacobian(sys::System; sparse = false, simplify = false, dvs = unknowns(sys)) obs = Dict(eq.lhs => eq.rhs for eq in observed(sys)) @@ -147,6 +195,18 @@ function calculate_jacobian(sys::System; return jac end +""" + $(TYPEDSIGNATURES) + +Generate the jacobian function for the equations of a [`System`](@ref). + +# Keyword Arguments + +$GENERATE_X_KWARGS +- `simplify`, `sparse`: Forwarded to [`calculate_jacobian`](@ref). + +All other keyword arguments are forwarded to [`build_function_wrapper`](@ref). +""" function generate_jacobian(sys::System; simplify = false, sparse = false, eval_expression = false, eval_module = @__MODULE__, expression = Val{true}, wrap_gfw = Val{false}, @@ -182,11 +242,24 @@ function assert_jac_length_header(sys) end end +""" + $(TYPEDSIGNATURES) + +Generate the tgrad function for the equations of a [`System`](@ref). + +# Keyword Arguments + +$GENERATE_X_KWARGS +- `simplify`: Forwarded to [`calculate_tgrad`](@ref). + +All other keyword arguments are forwarded to [`build_function_wrapper`](@ref). +""" function generate_tgrad( - sys::System, dvs = unknowns(sys), ps = parameters( - sys; initial_parameters = true); + sys::System; simplify = false, eval_expression = false, eval_module = @__MODULE__, expression = Val{true}, wrap_gfw = Val{false}, kwargs...) + dvs = unknowns(sys) + ps = parameters(sys; initial_parameters = true) tgrad = calculate_tgrad(sys, simplify = simplify) p = reorder_parameters(sys, ps) res = build_function_wrapper(sys, tgrad, @@ -215,6 +288,11 @@ function calculate_hessian(sys::System; simplify = false, sparse = false) return hess end +""" + $(TYPEDSIGNATURES) + +Return the sparsity pattern of the hessian of the equations of `sys`. +""" function Symbolics.hessian_sparsity(sys::System) hess = calculate_hessian(sys; sparse = true) return similar.(hess, Float64) @@ -222,10 +300,23 @@ end const W_GAMMA = only(@variables ˍ₋gamma) -function generate_W(sys::System, γ = 1.0, dvs = unknowns(sys), - ps = parameters(sys; initial_parameters = true); +""" + $(TYPEDSIGNATURES) + +Generate the `W = γ * M + J` function for the equations of a [`System`](@ref). + +# Keyword Arguments + +$GENERATE_X_KWARGS +- `simplify`, `sparse`: Forwarded to [`calculate_jacobian`](@ref). + +All other keyword arguments are forwarded to [`build_function_wrapper`](@ref). +""" +function generate_W(sys::System; simplify = false, sparse = false, expression = Val{true}, wrap_gfw = Val{false}, eval_expression = false, eval_module = @__MODULE__, kwargs...) + dvs = unknowns(sys) + ps = parameters(sys; initial_parameters = true) M = calculate_massmatrix(sys; simplify) if sparse M = SparseArrays.sparse(M) @@ -244,10 +335,25 @@ function generate_W(sys::System, γ = 1.0, dvs = unknowns(sys), expression, wrap_gfw, (2, 4, is_split(sys)), res; eval_expression, eval_module) end -function generate_dae_jacobian(sys::System, dvs = unknowns(sys), - ps = parameters(sys; initial_parameters = true); simplify = false, sparse = false, +""" + $(TYPEDSIGNATURES) + +Generate the DAE jacobian `γ * J′ + J` function for the equations of a [`System`](@ref). +`J′` is the jacobian of the equations with respect to the `du` vector, and `J` is the +standard jacobian. + +# Keyword Arguments + +$GENERATE_X_KWARGS +- `simplify`, `sparse`: Forwarded to [`calculate_jacobian`](@ref). + +All other keyword arguments are forwarded to [`build_function_wrapper`](@ref). +""" +function generate_dae_jacobian(sys::System; simplify = false, sparse = false, expression = Val{true}, wrap_gfw = Val{false}, eval_expression = false, eval_module = @__MODULE__, kwargs...) + dvs = unknowns(sys) + ps = parameters(sys; initial_parameters = true) jac_u = calculate_jacobian(sys; simplify = simplify, sparse = sparse) t = get_iv(sys) derivatives = Differential(t).(unknowns(sys)) @@ -262,6 +368,18 @@ function generate_dae_jacobian(sys::System, dvs = unknowns(sys), expression, wrap_gfw, (3, 5, is_split(sys)), res; eval_expression, eval_module) end +""" + $(TYPEDSIGNATURES) + +Generate the history function for a [`System`](@ref), given a symbolic representation of +the `u0` vector prior to the initial time. + +# Keyword Arguments + +$GENERATE_X_KWARGS + +All other keyword arguments are forwarded to [`build_function_wrapper`](@ref). +""" function generate_history(sys::System, u0; expression = Val{true}, wrap_gfw = Val{false}, eval_expression = false, eval_module = @__MODULE__, kwargs...) p = reorder_parameters(sys) @@ -272,6 +390,13 @@ function generate_history(sys::System, u0; expression = Val{true}, wrap_gfw = Va expression, wrap_gfw, (1, 2, is_split(sys)), res; eval_expression, eval_module) end +""" + $(TYPEDSIGNATURES) + +Calculate the mass matrix of `sys`. `simplify` controls whether `Symbolics.simplify` is +applied to the symbolic mass matrix. Returns a `Diagonal` or `LinearAlgebra.I` wherever +possible. +""" function calculate_massmatrix(sys::System; simplify = false) eqs = [eq for eq in equations(sys)] M = zeros(length(eqs), length(eqs)) @@ -285,7 +410,7 @@ function calculate_massmatrix(sys::System; simplify = false) error("Only semi-explicit constant mass matrices are currently supported. Faulty equation: $eq.") end end - M = simplify ? simplify.(M) : M + M = simplify ? Symbolics.simplify.(M) : M if isdiag(M) M = Diagonal(M) end @@ -293,6 +418,12 @@ function calculate_massmatrix(sys::System; simplify = false) M == I ? I : M end +""" + $(TYPEDSIGNATURES) + +Return a modified version of mass matrix `M` which is of a similar type to `u0`. `sparse` +controls whether the mass matrix should be a sparse matrix. +""" function concrete_massmatrix(M; sparse = false, u0 = nothing) if sparse && !(u0 === nothing || M === I) SparseArrays.sparse(M) @@ -305,6 +436,11 @@ function concrete_massmatrix(M; sparse = false, u0 = nothing) end end +""" + $(TYPEDSIGNATURES) + +Return the sparsity pattern of the jacobian of `sys` as a matrix. +""" function jacobian_sparsity(sys::System) sparsity = torn_system_jacobian_sparsity(sys) sparsity === nothing || return sparsity @@ -313,6 +449,13 @@ function jacobian_sparsity(sys::System) [dv for dv in unknowns(sys)]) end +""" + $(TYPEDSIGNATURES) + +Return the sparsity pattern of the DAE jacobian of `sys` as a matrix. + +See also: [`generate_dae_jacobian`](@ref). +""" function jacobian_dae_sparsity(sys::System) J1 = jacobian_sparsity([eq.rhs for eq in full_equations(sys)], [dv for dv in unknowns(sys)]) @@ -322,6 +465,13 @@ function jacobian_dae_sparsity(sys::System) J1 + J2 end +""" + $(TYPEDSIGNATURES) + +Return the sparsity pattern of the `W` matrix of `sys`. + +See also: [`generate_W`](@ref). +""" function W_sparsity(sys::System) jac_sparsity = jacobian_sparsity(sys) (n, n) = size(jac_sparsity) @@ -359,6 +509,18 @@ function get_constraint_unknown_subs!(subs::Dict, cons::Vector, stidxmap::Dict, end end +""" + $(TYPEDSIGNATURES) + +Generate the boundary condition function for a [`System`](@ref) given the state vector `u0`, +the indexes of `u0` to consider as hard constraints `u0_idxs` and the initial time `t0`. + +# Keyword Arguments + +$GENERATE_X_KWARGS + +All other keyword arguments are forwarded to [`build_function_wrapper`](@ref). +""" function generate_boundary_conditions(sys::System, u0, u0_idxs, t0; expression = Val{true}, wrap_gfw = Val{false}, eval_expression = false, eval_module = @__MODULE__, kwargs...) @@ -393,6 +555,17 @@ function generate_boundary_conditions(sys::System, u0, u0_idxs, t0; expression = expression, wrap_gfw, (2, 3, is_split(sys)), res; eval_expression, eval_module) end +""" + $(TYPEDSIGNATURES) + +Generate the cost function for a [`System`](@ref). + +# Keyword Arguments + +$GENERATE_X_KWARGS + +All other keyword arguments are forwarded to [`build_function_wrapper`](@ref). +""" function generate_cost(sys::System; expression = Val{true}, wrap_gfw = Val{false}, eval_expression = false, eval_module = @__MODULE__, kwargs...) obj = cost(sys) @@ -429,6 +602,18 @@ function calculate_cost_gradient(sys::System; simplify = false) return Symbolics.gradient(obj, dvs; simplify) end +""" + $(TYPEDSIGNATURES) + +Generate the gradient of the cost function with respect to unknowns for a [`System`](@ref). + +# Keyword Arguments + +$GENERATE_X_KWARGS +- `simplify`: Forwarded to [`calculate_cost_gradient`](@ref). + +All other keyword arguments are forwarded to [`build_function_wrapper`](@ref). +""" function generate_cost_gradient( sys::System; expression = Val{true}, wrap_gfw = Val{false}, eval_expression = false, eval_module = @__MODULE__, simplify = false, kwargs...) @@ -452,10 +637,29 @@ function calculate_cost_hessian(sys::System; sparse = false, simplify = false) end end +""" + $(TYPEDSIGNATURES) + +Return the sparsity pattern for the hessian of the cost function of `sys`. +""" function cost_hessian_sparsity(sys::System) return similar(calculate_cost_hessian(sys; sparse = true), Float64) end +""" + $(TYPEDSIGNATURES) + +Generate the hessian of the cost function for a [`System`](@ref). + +# Keyword Arguments + +$GENERATE_X_KWARGS +- `simplify`, `sparse`: Forwarded to [`calculate_cost_hessian`](@ref). +- `return_sparsity`: Whether to also return the sparsity pattern of the hessian as the + second return value. + +All other keyword arguments are forwarded to [`build_function_wrapper`](@ref). +""" function generate_cost_hessian( sys::System; expression = Val{true}, wrap_gfw = Val{false}, eval_expression = false, eval_module = @__MODULE__, simplify = false, @@ -481,6 +685,17 @@ function canonical_constraints(sys::System) end end +""" + $(TYPEDSIGNATURES) + +Generate the constraint function for a [`System`](@ref). + +# Keyword Arguments + +$GENERATE_X_KWARGS + +All other keyword arguments are forwarded to [`build_function_wrapper`](@ref). +""" function generate_cons(sys::System; expression = Val{true}, wrap_gfw = Val{false}, eval_expression = false, eval_module = @__MODULE__, kwargs...) cons = canonical_constraints(sys) @@ -491,13 +706,20 @@ function generate_cons(sys::System; expression = Val{true}, wrap_gfw = Val{false expression, wrap_gfw, (2, 2, is_split(sys)), res; eval_expression, eval_module) end -function generate_constraint_jacobian( - sys::System; expression = Val{true}, wrap_gfw = Val{false}, - eval_expression = false, eval_module = @__MODULE__, return_sparsity = false, - simplify = false, sparse = false, kwargs...) +""" + $(TYPEDSIGNATURES) + +Return the jacobian of the constraints of `sys` with respect to unknowns. + +# Keyword arguments + +- `simplify`, `sparse`: Forwarded to `Symbolics.jacobian`. +- `return_sparsity`: Whether to also return the sparsity pattern of the jacobian. +""" +function calculate_constraint_jacobian(sys::System; simplify = false, sparse = false, + return_sparsity = false) cons = canonical_constraints(sys) dvs = unknowns(sys) - ps = reorder_parameters(sys) sparsity = nothing if sparse jac = Symbolics.sparsejacobian(cons, dvs; simplify)::AbstractSparseArray @@ -505,19 +727,51 @@ function generate_constraint_jacobian( else jac = Symbolics.jacobian(cons, dvs; simplify) end + return return_sparsity ? (jac, sparsity) : jac +end + +""" + $(TYPEDSIGNATURES) + +Generate the jacobian of the constraint function for a [`System`](@ref). + +# Keyword Arguments + +$GENERATE_X_KWARGS +- `simplify`, `sparse`: Forwarded to [`calculate_constraint_jacobian`](@ref). +- `return_sparsity`: Whether to also return the sparsity pattern of the jacobian as the + second return value. + +All other keyword arguments are forwarded to [`build_function_wrapper`](@ref). +""" +function generate_constraint_jacobian( + sys::System; expression = Val{true}, wrap_gfw = Val{false}, + eval_expression = false, eval_module = @__MODULE__, return_sparsity = false, + simplify = false, sparse = false, kwargs...) + dvs = unknowns(sys) + ps = reorder_parameters(sys) + jac, sparsity = calculate_constraint_jacobian( + sys; simplify, sparse, return_sparsity = true) res = build_function_wrapper(sys, jac, dvs, ps...; expression = Val{true}, kwargs...) fn = maybe_compile_function( expression, wrap_gfw, (2, 2, is_split(sys)), res; eval_expression, eval_module) return return_sparsity ? (fn, sparsity) : fn end -function generate_constraint_hessian( - sys::System; expression = Val{true}, wrap_gfw = Val{false}, - eval_expression = false, eval_module = @__MODULE__, return_sparsity = false, - simplify = false, sparse = false, kwargs...) +""" + $(TYPEDSIGNATURES) + +Return the hessian of the constraints of `sys` with respect to unknowns. + +# Keyword arguments + +- `simplify`, `sparse`: Forwarded to `Symbolics.hessian`. +- `return_sparsity`: Whether to also return the sparsity pattern of the hessian. +""" +function calculate_constraint_hessian( + sys::System; simplify = false, sparse = false, return_sparsity = false) cons = canonical_constraints(sys) dvs = unknowns(sys) - ps = reorder_parameters(sys) sparsity = nothing if sparse hess = map(cons) do cstr @@ -527,12 +781,46 @@ function generate_constraint_hessian( else hess = [Symbolics.hessian(cstr, dvs; simplify) for cstr in cons] end + return return_sparsity ? (hess, sparsity) : hess +end + +""" + $(TYPEDSIGNATURES) + +Generate the hessian of the constraint function for a [`System`](@ref). + +# Keyword Arguments + +$GENERATE_X_KWARGS +- `simplify`, `sparse`: Forwarded to [`calculate_constraint_hessian`](@ref). +- `return_sparsity`: Whether to also return the sparsity pattern of the hessian as the + second return value. + +All other keyword arguments are forwarded to [`build_function_wrapper`](@ref). +""" +function generate_constraint_hessian( + sys::System; expression = Val{true}, wrap_gfw = Val{false}, + eval_expression = false, eval_module = @__MODULE__, return_sparsity = false, + simplify = false, sparse = false, kwargs...) + dvs = unknowns(sys) + ps = reorder_parameters(sys) + hess, sparsity = calculate_constraint_hessian( + sys; simplify, sparse, return_sparsity = true) res = build_function_wrapper(sys, hess, dvs, ps...; expression = Val{true}, kwargs...) fn = maybe_compile_function( expression, wrap_gfw, (2, 2, is_split(sys)), res; eval_expression, eval_module) return return_sparsity ? (fn, sparsity) : fn end +""" + $(TYPEDSIGNATURES) + +Calculate the jacobian of the equations of `sys` with respect to the inputs. + +# Keyword arguments + +- `simplify`, `sparse`: Forwarded to `Symbolics.jacobian`. +""" function calculate_control_jacobian(sys::AbstractSystem; sparse = false, simplify = false) rhs = [eq.rhs for eq in full_equations(sys)] @@ -547,10 +835,23 @@ function calculate_control_jacobian(sys::AbstractSystem; return jac end -function generate_control_jacobian(sys::AbstractSystem, dvs = unknowns(sys), - ps = parameters(sys; initial_parameters = true); +""" + $(TYPEDSIGNATURES) + +Generate the jacobian function of the equations of `sys` with respect to the inputs. + +# Keyword arguments + +$GENERATE_X_KWARGS +- `simplify`, `sparse`: Forwarded to [`calculate_constraint_hessian`](@ref). + +All other keyword arguments are forwarded to [`build_function_wrapper`](@ref). +""" +function generate_control_jacobian(sys::AbstractSystem; expression = Val{true}, wrap_gfw = Val{false}, eval_expression = false, eval_module = @__MODULE__, simplify = false, sparse = false, kwargs...) + dvs = unknowns(sys) + ps = parameters(sys; initial_parameters = true) jac = calculate_control_jacobian(sys; simplify = simplify, sparse = sparse) p = reorder_parameters(sys, ps) res = build_function_wrapper(sys, jac, dvs, p..., get_iv(sys); kwargs...) From 2f0acc1ff8b5d0c033bd68f9f4ff98e3f46e46f9 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 26 May 2025 14:15:25 +0530 Subject: [PATCH 1905/2176] refactor: remove old `calculate_*` and `generate_*` function stubs --- src/systems/abstractsystem.jl | 126 ---------------------------------- 1 file changed, 126 deletions(-) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index 40029086a1..33c4d72c00 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -8,132 +8,6 @@ end GUIMetadata(type) = GUIMetadata(type, nothing) -""" -```julia -calculate_tgrad(sys::AbstractTimeDependentSystem) -``` - -Calculate the time gradient of a system. - -Returns a vector of [`Num`](@ref) instances. The result from the first -call will be cached in the system object. -""" -function calculate_tgrad end - -""" -```julia -calculate_gradient(sys::AbstractSystem) -``` - -Calculate the gradient of a scalar system. - -Returns a vector of [`Num`](@ref) instances. The result from the first -call will be cached in the system object. -""" -function calculate_gradient end - -""" -```julia -calculate_jacobian(sys::AbstractSystem) -``` - -Calculate the Jacobian matrix of a system. - -Returns a matrix of [`Num`](@ref) instances. The result from the first -call will be cached in the system object. -""" -function calculate_jacobian end - -""" -```julia -calculate_control_jacobian(sys::AbstractSystem) -``` - -Calculate the Jacobian matrix of a system with respect to the system's controls. - -Returns a matrix of [`Num`](@ref) instances. The result from the first -call will be cached in the system object. -""" -function calculate_control_jacobian end - -""" -```julia -calculate_factorized_W(sys::AbstractSystem) -``` - -Calculate the factorized W-matrix of a system. - -Returns a matrix of [`Num`](@ref) instances. The result from the first -call will be cached in the system object. -""" -function calculate_factorized_W end - -""" -```julia -calculate_hessian(sys::AbstractSystem) -``` - -Calculate the hessian matrix of a scalar system. - -Returns a matrix of [`Num`](@ref) instances. The result from the first -call will be cached in the system object. -""" -function calculate_hessian end - -""" -```julia -generate_tgrad(sys::AbstractTimeDependentSystem, dvs = unknowns(sys), ps = parameters(sys), - expression = Val{true}; kwargs...) -``` - -Generates a function for the time gradient of a system. Extra arguments control -the arguments to the internal [`build_function`](@ref) call. -""" -function generate_tgrad end - -""" -```julia -generate_gradient(sys::AbstractSystem, dvs = unknowns(sys), ps = parameters(sys), - expression = Val{true}; kwargs...) -``` - -Generates a function for the gradient of a system. Extra arguments control -the arguments to the internal [`build_function`](@ref) call. -""" -function generate_gradient end - -""" -```julia -generate_jacobian(sys::AbstractSystem, dvs = unknowns(sys), ps = parameters(sys), - expression = Val{true}; sparse = false, kwargs...) -``` - -Generates a function for the Jacobian matrix of a system. Extra arguments control -the arguments to the internal [`build_function`](@ref) call. -""" -function generate_jacobian end - -""" -```julia -generate_hessian(sys::AbstractSystem, dvs = unknowns(sys), ps = parameters(sys), - expression = Val{true}; sparse = false, kwargs...) -``` - -Generates a function for the hessian matrix of a system. Extra arguments control -the arguments to the internal [`build_function`](@ref) call. -""" -function generate_hessian end - -""" -```julia -generate_function(sys::AbstractSystem, dvs = unknowns(sys), ps = parameters(sys), - expression = Val{true}; kwargs...) -``` - -Generate a function to evaluate the system's equations. -""" -function generate_rhs end - """ ```julia generate_custom_function(sys::AbstractSystem, exprs, dvs = unknowns(sys), From 8ed2e02d93b5caf6014119275a825ef6713c352e Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 26 May 2025 14:16:02 +0530 Subject: [PATCH 1906/2176] refactor: update from old `generate_*` syntax --- src/problems/daeproblem.jl | 6 ++---- src/problems/ddeproblem.jl | 5 +---- src/problems/discreteproblem.jl | 6 ++---- src/problems/implicitdiscreteproblem.jl | 3 +-- src/problems/intervalnonlinearproblem.jl | 4 +--- src/problems/nonlinearproblem.jl | 4 +--- src/problems/odeproblem.jl | 6 ++---- src/problems/optimizationproblem.jl | 3 +-- src/problems/sddeproblem.jl | 7 ++----- src/problems/sdeproblem.jl | 8 +++----- test/nonlinearsystem.jl | 4 ++-- test/odesystem.jl | 4 ++-- 12 files changed, 20 insertions(+), 40 deletions(-) diff --git a/src/problems/daeproblem.jl b/src/problems/daeproblem.jl index 860f2d2da2..6134923d4d 100644 --- a/src/problems/daeproblem.jl +++ b/src/problems/daeproblem.jl @@ -7,9 +7,7 @@ check_complete(sys, DAEFunction) check_compatibility && check_compatible_system(DAEFunction, sys) - dvs = unknowns(sys) - ps = parameters(sys) - f = generate_rhs(sys, dvs, ps; expression, wrap_gfw = Val{true}, + f = generate_rhs(sys; expression, wrap_gfw = Val{true}, implicit_dae = true, eval_expression, eval_module, checkbounds = checkbounds, cse, kwargs...) @@ -25,7 +23,7 @@ end if jac - _jac = generate_dae_jacobian(sys, dvs, ps; expression, + _jac = generate_dae_jacobian(sys; expression, wrap_gfw = Val{true}, simplify, sparse, cse, eval_expression, eval_module, checkbounds, kwargs...) else diff --git a/src/problems/ddeproblem.jl b/src/problems/ddeproblem.jl index 6f072b3d51..45af3edddb 100644 --- a/src/problems/ddeproblem.jl +++ b/src/problems/ddeproblem.jl @@ -6,10 +6,7 @@ check_complete(sys, DDEFunction) check_compatibility && check_compatible_system(DDEFunction, sys) - dvs = unknowns(sys) - ps = parameters(sys) - - f = generate_rhs(sys, dvs, ps; expression, wrap_gfw = Val{true}, + f = generate_rhs(sys; expression, wrap_gfw = Val{true}, eval_expression, eval_module, checkbounds = checkbounds, cse, kwargs...) diff --git a/src/problems/discreteproblem.jl b/src/problems/discreteproblem.jl index f640ee9ff5..ff0500ccf3 100644 --- a/src/problems/discreteproblem.jl +++ b/src/problems/discreteproblem.jl @@ -7,9 +7,7 @@ check_complete(sys, DiscreteFunction) check_compatibility && check_compatible_system(DiscreteFunction, sys) - dvs = unknowns(sys) - ps = parameters(sys) - f = generate_rhs(sys, dvs, ps; expression, wrap_gfw = Val{true}, + f = generate_rhs(sys; expression, wrap_gfw = Val{true}, eval_expression, eval_module, checkbounds = checkbounds, cse, kwargs...) @@ -45,7 +43,7 @@ end check_compatibility && check_compatible_system(DiscreteProblem, sys) dvs = unknowns(sys) - u0map = to_varmap(u0map, dvs) + u0map = to_varmap(op, dvs) add_toterms!(u0map; replace = true) f, u0, p = process_SciMLProblem(DiscreteFunction{iip, spec}, sys, op; t = tspan !== nothing ? tspan[1] : tspan, check_compatibility, expression, diff --git a/src/problems/implicitdiscreteproblem.jl b/src/problems/implicitdiscreteproblem.jl index 5b61e34df5..9e500d7354 100644 --- a/src/problems/implicitdiscreteproblem.jl +++ b/src/problems/implicitdiscreteproblem.jl @@ -9,8 +9,7 @@ iv = get_iv(sys) dvs = unknowns(sys) - ps = parameters(sys) - f = generate_rhs(sys, dvs, ps; expression, wrap_gfw = Val{true}, + f = generate_rhs(sys; expression, wrap_gfw = Val{true}, implicit_dae = true, eval_expression, eval_module, checkbounds = checkbounds, cse, override_discrete = true, kwargs...) diff --git a/src/problems/intervalnonlinearproblem.jl b/src/problems/intervalnonlinearproblem.jl index 65e7f7cb62..2dc929fe25 100644 --- a/src/problems/intervalnonlinearproblem.jl +++ b/src/problems/intervalnonlinearproblem.jl @@ -6,9 +6,7 @@ function SciMLBase.IntervalNonlinearFunction( check_complete(sys, IntervalNonlinearFunction) check_compatibility && check_compatible_system(IntervalNonlinearFunction, sys) - dvs = unknowns(sys) - ps = parameters(sys) - f = generate_rhs(sys, dvs, ps; expression, wrap_gfw = Val{true}, + f = generate_rhs(sys; expression, wrap_gfw = Val{true}, scalar = true, eval_expression, eval_module, checkbounds, cse, kwargs...) observedfun = ObservedFunctionCache( diff --git a/src/problems/nonlinearproblem.jl b/src/problems/nonlinearproblem.jl index ef36179e06..9986ee665b 100644 --- a/src/problems/nonlinearproblem.jl +++ b/src/problems/nonlinearproblem.jl @@ -8,9 +8,7 @@ check_complete(sys, NonlinearFunction) check_compatibility && check_compatible_system(NonlinearFunction, sys) - dvs = unknowns(sys) - ps = parameters(sys) - f = generate_rhs(sys, dvs, ps; expression, wrap_gfw = Val{true}, + f = generate_rhs(sys; expression, wrap_gfw = Val{true}, eval_expression, eval_module, checkbounds = checkbounds, cse, kwargs...) diff --git a/src/problems/odeproblem.jl b/src/problems/odeproblem.jl index e2c8f9cd8a..e78ff00525 100644 --- a/src/problems/odeproblem.jl +++ b/src/problems/odeproblem.jl @@ -7,9 +7,7 @@ check_complete(sys, ODEFunction) check_compatibility && check_compatible_system(ODEFunction, sys) - dvs = unknowns(sys) - ps = parameters(sys) - f = generate_rhs(sys, dvs, ps; expression, wrap_gfw = Val{true}, + f = generate_rhs(sys; expression, wrap_gfw = Val{true}, eval_expression, eval_module, checkbounds = checkbounds, cse, kwargs...) @@ -26,7 +24,7 @@ if tgrad _tgrad = generate_tgrad( - sys, dvs, ps; expression, wrap_gfw = Val{true}, + sys; expression, wrap_gfw = Val{true}, simplify, cse, eval_expression, eval_module, checkbounds, kwargs...) else _tgrad = nothing diff --git a/src/problems/optimizationproblem.jl b/src/problems/optimizationproblem.jl index 6d5fb1d79e..36b34a8867 100644 --- a/src/problems/optimizationproblem.jl +++ b/src/problems/optimizationproblem.jl @@ -10,8 +10,7 @@ function SciMLBase.OptimizationFunction{iip}(sys::System; expression = Val{false}, kwargs...) where {iip} check_complete(sys, OptimizationFunction) check_compatibility && check_compatible_system(OptimizationFunction, sys) - dvs = unknowns(sys) - ps = parameters(sys) + cstr = constraints(sys) f = generate_cost(sys; expression, wrap_gfw = Val{true}, eval_expression, diff --git a/src/problems/sddeproblem.jl b/src/problems/sddeproblem.jl index 0daa04fcf7..e1cc00b2a7 100644 --- a/src/problems/sddeproblem.jl +++ b/src/problems/sddeproblem.jl @@ -6,12 +6,9 @@ check_complete(sys, SDDEFunction) check_compatibility && check_compatible_system(SDDEFunction, sys) - dvs = unknowns(sys) - ps = parameters(sys) - - f = generate_rhs(sys, dvs, ps; expression, wrap_gfw = Val{true}, + f = generate_rhs(sys; expression, wrap_gfw = Val{true}, eval_expression, eval_module, checkbounds = checkbounds, cse, kwargs...) - g = generate_diffusion_function(sys, dvs, ps; expression, + g = generate_diffusion_function(sys; expression, wrap_gfw = Val{true}, eval_expression, eval_module, checkbounds, cse, kwargs...) if spec === SciMLBase.FunctionWrapperSpecialize && iip diff --git a/src/problems/sdeproblem.jl b/src/problems/sdeproblem.jl index 57ca120641..1bc47118ff 100644 --- a/src/problems/sdeproblem.jl +++ b/src/problems/sdeproblem.jl @@ -7,12 +7,10 @@ check_complete(sys, SDEFunction) check_compatibility && check_compatible_system(SDEFunction, sys) - dvs = unknowns(sys) - ps = parameters(sys) - f = generate_rhs(sys, dvs, ps; expression, wrap_gfw = Val{true}, + f = generate_rhs(sys; expression, wrap_gfw = Val{true}, eval_expression, eval_module, checkbounds = checkbounds, cse, kwargs...) - g = generate_diffusion_function(sys, dvs, ps; expression, + g = generate_diffusion_function(sys; expression, wrap_gfw = Val{true}, eval_expression, eval_module, checkbounds, cse, kwargs...) if spec === SciMLBase.FunctionWrapperSpecialize && iip @@ -27,7 +25,7 @@ end if tgrad - _tgrad = generate_tgrad(sys, dvs, ps; expression, + _tgrad = generate_tgrad(sys; expression, wrap_gfw = Val{true}, simplify, cse, eval_expression, eval_module, checkbounds, kwargs...) else diff --git a/test/nonlinearsystem.jl b/test/nonlinearsystem.jl index bf05e317ce..e4a5b418b9 100644 --- a/test/nonlinearsystem.jl +++ b/test/nonlinearsystem.jl @@ -29,7 +29,7 @@ eqs = [0 ~ σ * (y - x) * h, @test eval(toexpr(ns)) == ns test_nlsys_inference("standard", ns, (x, y, z), (σ, ρ, β, h)) @test begin - f = generate_rhs(ns, [x, y, z], [σ, ρ, β, h], expression = Val{false})[2] + f = generate_rhs(ns, expression = Val{false})[2] du = [0.0, 0.0, 0.0] f(du, [1, 2, 3], [1, 2, 3, 1]) du ≈ [1, -3, -7] @@ -65,7 +65,7 @@ eqs = [0 ~ σ * a * h, 0 ~ x * y - β * z] @named ns = System(eqs, [x, y, z], [σ, ρ, β, h]) ns = complete(ns) -nlsys_func = generate_rhs(ns, [x, y, z], [σ, ρ, β, h]) +nlsys_func = generate_rhs(ns) nf = NonlinearFunction(ns) jac = calculate_jacobian(ns) diff --git a/test/odesystem.jl b/test/odesystem.jl index 6dc8b06eb9..7bf4dfebb0 100644 --- a/test/odesystem.jl +++ b/test/odesystem.jl @@ -148,7 +148,7 @@ f(du, [1.0, 2.0, 3.0], [x -> x + 7, 2, 3, 1], 5.0) eqs = [D(x) ~ x + 10σ(t - 1) + 100σ(t - 2) + 1000σ(t^2)] @named de = System(eqs, t) test_diffeq_inference("many internal iv-varying", de, t, (x,), (σ,)) -f = generate_rhs(de, [x], [σ], expression = Val{false}, wrap_gfw = Val{true}) +f = generate_rhs(de, expression = Val{false}, wrap_gfw = Val{true}) du = [0.0] f(du, [1.0], [t -> t + 2], 5.0) @test du ≈ [27561] @@ -173,7 +173,7 @@ eqs = [D(x) ~ -A * x, @named de = System(eqs, t) @test begin local f, du - f = generate_rhs(de, [x, y], [A, B, C], expression = Val{false}, wrap_gfw = Val{true}) + f = generate_rhs(de, expression = Val{false}, wrap_gfw = Val{true}) du = [0.0, 0.0] f(du, [1.0, 2.0], [1, 2, 3], 0.0) du ≈ [-1, -1 / 3] From 6874e2b7b2757f4f0532d4f92962ffc37078a1d3 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 26 May 2025 14:23:46 +0530 Subject: [PATCH 1907/2176] docs: add new doc pages, remove outdated pages --- docs/pages.jl | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/docs/pages.jl b/docs/pages.jl index 0177947ed2..6c0806b15a 100644 --- a/docs/pages.jl +++ b/docs/pages.jl @@ -27,9 +27,13 @@ pages = [ "Advanced Examples" => Any["examples/tearing_parallelism.md", "examples/sparse_jacobians.md", "examples/perturbation.md"]], - "Basics" => Any["basics/AbstractSystem.md", - "basics/ContextualVariables.md", - "basics/Variable_metadata.md", + "API" => Any["API/System.md", + "API/variables.md", + "API/model_building.md", + "API/problems.md", + "API/codegen.md", + "API/PDESystem.md"], + "Basics" => Any[ "basics/Composition.md", "basics/Events.md", "basics/Linearization.md", @@ -40,14 +44,6 @@ pages = [ "basics/DependencyGraphs.md", "basics/Precompilation.md", "basics/FAQ.md"], - "System Types" => Any["systems/ODESystem.md", - "systems/SDESystem.md", - "systems/JumpSystem.md", - "systems/NonlinearSystem.md", - "systems/OptimizationSystem.md", - "systems/PDESystem.md", - "systems/DiscreteSystem.md", - "systems/ImplicitDiscreteSystem.md"], "comparison.md", "internals.md" ] From 175b9cd4ce10a01a4ba098ed3189a23afd480746 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 26 May 2025 14:24:18 +0530 Subject: [PATCH 1908/2176] docs: add sources to `docs/Project.toml` to enable doc builds --- docs/Project.toml | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/docs/Project.toml b/docs/Project.toml index ae0d89dd3a..f9cf903ed7 100644 --- a/docs/Project.toml +++ b/docs/Project.toml @@ -17,6 +17,7 @@ ModelingToolkitStandardLibrary = "16a59e39-deab-5bd0-87e4-056b12336739" NonlinearSolve = "8913a72c-1f9b-4ce2-8d82-65094dcecaec" Optim = "429524aa-4258-5aef-a3af-852621145aeb" Optimization = "7f7a1694-90dd-40f0-9382-eb1efda571ba" +OptimizationBase = "bca83a33-5cc9-4baa-983d-23429ab6bcbb" OptimizationOptimJL = "36348300-93cb-4f02-beb5-3c3902f8871e" OrdinaryDiffEq = "1dea7af3-3e70-54e6-95c3-0bf5283fa5ed" Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80" @@ -29,6 +30,10 @@ SymbolicUtils = "d1185830-fcd6-423d-90d6-eec64667417b" Symbolics = "0c5d862f-8b57-4792-8d23-62f2024744c7" Unitful = "1986cc42-f94f-5a68-af5c-568840ba703d" +[sources] +ModelingToolkitStandardLibrary = {rev = "mtk-v10", url = "https://github.com/SciML/ModelingToolkitStandardLibrary.jl/"} +OptimizationBase = {rev = "as/mtk-v10", url = "https://github.com/AayushSabharwal/OptimizationBase.jl"} + [compat] Attractors = "1.24" BenchmarkTools = "1.3" From 276aef366b83c6d22f53356bc47eb2194a44ea91 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 27 May 2025 12:14:11 +0530 Subject: [PATCH 1909/2176] fix: use `time_dependent_init` instead of `is_time_dependent(sys)` where applicable --- src/systems/problem_utils.jl | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/systems/problem_utils.jl b/src/systems/problem_utils.jl index 6eb44893e9..2641d92108 100644 --- a/src/systems/problem_utils.jl +++ b/src/systems/problem_utils.jl @@ -1080,7 +1080,7 @@ function maybe_build_initialization_problem( end initializeprob = remake(initializeprob; p = initp) - get_initial_unknowns = if is_time_dependent(sys) + get_initial_unknowns = if time_dependent_init GetUpdatedU0(sys, initializeprob.f.sys, op) else nothing @@ -1092,9 +1092,6 @@ function maybe_build_initialization_problem( sys, initializeprob.f.sys; u0_constructor, p_constructor), get_initial_unknowns, SetInitialUnknowns(sys)) - if time_dependent_init === nothing - time_dependent_init = is_time_dependent(sys) - end if time_dependent_init all_init_syms = Set(all_symbols(initializeprob)) solved_unknowns = filter(var -> var in all_init_syms, unknowns(sys)) From 03dc7992ded37b48d07f08e04a1cf98ff8bfda27 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 27 May 2025 12:14:41 +0530 Subject: [PATCH 1910/2176] feat: implement latexification for `Connector` --- src/systems/analysis_points.jl | 2 ++ src/systems/connectors.jl | 9 +++++++++ 2 files changed, 11 insertions(+) diff --git a/src/systems/analysis_points.jl b/src/systems/analysis_points.jl index 12185c943f..bbd23b9a50 100644 --- a/src/systems/analysis_points.jl +++ b/src/systems/analysis_points.jl @@ -140,6 +140,8 @@ function Base.show(io::IO, ::MIME"text/plain", ap::AnalysisPoint) end end +Symbolics.hide_lhs(::AnalysisPoint) = true + @latexrecipe function f(ap::AnalysisPoint) index --> :subscript snakecase --> true diff --git a/src/systems/connectors.jl b/src/systems/connectors.jl index 9761d18493..d771445074 100644 --- a/src/systems/connectors.jl +++ b/src/systems/connectors.jl @@ -41,6 +41,15 @@ function Base.show(io::IO, c::Connection) print(io, ")") end +@latexrecipe function f(c::Connection) + index --> :subscript + return Expr(:call, :connect, map(nameof, c.systems)...) +end + +function Base.show(io::IO, ::MIME"text/latex", ap::Connection) + print(io, latexify(ap)) +end + isconnection(_) = false isconnection(_::Connection) = true """ From 1daaa32e2c9d1f2936fae30bbc769b123cffc614 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 27 May 2025 22:34:13 +0530 Subject: [PATCH 1911/2176] build: bump Symbolics compat --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 527451bca2..e77aa6377c 100644 --- a/Project.toml +++ b/Project.toml @@ -158,7 +158,7 @@ StochasticDelayDiffEq = "1.10" StochasticDiffEq = "6.72.1" SymbolicIndexingInterface = "0.3.39" SymbolicUtils = "3.26.1" -Symbolics = "6.37" +Symbolics = "6.40" URIs = "1" UnPack = "0.1, 1.0" Unitful = "1.1" From 3e3b5f05c33ef6da4e441ac2d67ee8b4520edabc Mon Sep 17 00:00:00 2001 From: Fredrik Bagge Carlson Date: Wed, 28 May 2025 06:59:03 +0200 Subject: [PATCH 1912/2176] refactor AP transform into exported API --- src/ModelingToolkit.jl | 2 +- src/systems/analysis_points.jl | 23 +++++++++++++++++++---- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/src/ModelingToolkit.jl b/src/ModelingToolkit.jl index 306a1f5bc7..5a869ce27a 100644 --- a/src/ModelingToolkit.jl +++ b/src/ModelingToolkit.jl @@ -307,7 +307,7 @@ export independent_variable, equations, controls, observed, full_equations, jump brownians export initialization_equations, guesses, defaults, parameter_dependencies, hierarchy export mtkcompile, expand_connections, linearize, linearization_function, - LinearizationProblem, structural_simplify + LinearizationProblem, linearization_ap_transform, structural_simplify export solve export Pre diff --git a/src/systems/analysis_points.jl b/src/systems/analysis_points.jl index bbd23b9a50..1a21a3f7b3 100644 --- a/src/systems/analysis_points.jl +++ b/src/systems/analysis_points.jl @@ -930,13 +930,22 @@ function open_loop(sys, ap::Union{Symbol, AnalysisPoint}; system_modifier = iden return system_modifier(sys), vars end -function linearization_function(sys::AbstractSystem, +""" + sys, input_vars, output_vars = $(TYPEDSIGNATURES) + +Apply analysis-point transformations to prepare a system for linearization. + +Returns +- `sys`: The transformed system. +- `input_vars`: A vector of input variables corresponding to the input analysis points. +- `output_vars`: A vector of output variables corresponding to the output analysis points. +""" +function linearization_ap_transform(sys, inputs::Union{Symbol, Vector{Symbol}, AnalysisPoint, Vector{AnalysisPoint}}, - outputs; loop_openings = [], system_modifier = identity, kwargs...) + outputs, loop_openings) loop_openings = Set(map(nameof, canonicalize_ap(sys, loop_openings))) inputs = canonicalize_ap(sys, inputs) outputs = canonicalize_ap(sys, outputs) - input_vars = [] for input in inputs if nameof(input) in loop_openings @@ -958,9 +967,15 @@ function linearization_function(sys::AbstractSystem, end push!(output_vars, output_var) end - sys = handle_loop_openings(sys, map(AnalysisPoint, collect(loop_openings))) + return sys, input_vars, output_vars +end + +function linearization_function(sys::AbstractSystem, + inputs::Union{Symbol, Vector{Symbol}, AnalysisPoint, Vector{AnalysisPoint}}, + outputs; loop_openings = [], system_modifier = identity, kwargs...) + sys, input_vars, output_vars = linearization_ap_transform(sys, inputs, outputs, loop_openings) return linearization_function(system_modifier(sys), input_vars, output_vars; kwargs...) end From 81fc4d81e6256bcd10cb7acb9ba4ece1b26e633d Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 28 May 2025 18:18:13 +0530 Subject: [PATCH 1913/2176] docs: document `parameter_dependencies` field of `System` --- src/systems/system.jl | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/systems/system.jl b/src/systems/system.jl index 872cf5fc95..9238a7708c 100644 --- a/src/systems/system.jl +++ b/src/systems/system.jl @@ -105,6 +105,11 @@ struct System <: AbstractSystem equation. """ observed::Vector{Equation} + """ + $INTERNAL_FIELD_WARNING + All the explicit equations relating parameters. Equations here only contain parameters + and are in the same format as `observed`. + """ parameter_dependencies::Vector{Equation} """ $INTERNAL_FIELD_WARNING From 317c2f7e7ed24799c19ea66ad00f8fbd71e43bfb Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 28 May 2025 18:19:35 +0530 Subject: [PATCH 1914/2176] refactor: disable passing `parameter_dependencies` as kwarg to `System` --- src/systems/system.jl | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/systems/system.jl b/src/systems/system.jl index 9238a7708c..6f6298c70d 100644 --- a/src/systems/system.jl +++ b/src/systems/system.jl @@ -336,6 +336,13 @@ function System(eqs::Vector{Equation}, iv, dvs, ps, brownians = []; initializesystem = nothing, is_initializesystem = false, preface = [], checks = true) name === nothing && throw(NoNameError()) + if !isempty(parameter_dependencies) + @warn """ + The `parameter_dependencies` keyword argument is deprecated. Please provide all + such equations as part of the normal equations of the system. + """ + eqs = Equation[eqs; parameter_dependencies] + end iv = unwrap(iv) ps = unwrap.(ps) @@ -356,7 +363,6 @@ function System(eqs::Vector{Equation}, iv, dvs, ps, brownians = []; costs = Union{BasicSymbolic, Real}[] end - parameter_dependencies, ps = process_parameter_dependencies(parameter_dependencies, ps) defaults = anydict(defaults) guesses = anydict(guesses) var_to_name = anydict() @@ -366,10 +372,6 @@ function System(eqs::Vector{Equation}, iv, dvs, ps, brownians = []; process_variables!(var_to_name, defaults, guesses, dvs) process_variables!(var_to_name, defaults, guesses, ps) - process_variables!( - var_to_name, defaults, guesses, [eq.lhs for eq in parameter_dependencies]) - process_variables!( - var_to_name, defaults, guesses, [eq.rhs for eq in parameter_dependencies]) process_variables!(var_to_name, defaults, guesses, [eq.lhs for eq in observed]) process_variables!(var_to_name, defaults, guesses, [eq.rhs for eq in observed]) end @@ -408,7 +410,7 @@ function System(eqs::Vector{Equation}, iv, dvs, ps, brownians = []; metadata = meta end System(Threads.atomic_add!(SYSTEM_COUNT, UInt(1)), eqs, noise_eqs, jumps, constraints, - costs, consolidate, dvs, ps, brownians, iv, observed, parameter_dependencies, + costs, consolidate, dvs, ps, brownians, iv, observed, Equation[], var_to_name, name, description, defaults, guesses, systems, initialization_eqs, continuous_events, discrete_events, connector_type, assertions, metadata, gui_metadata, is_dde, tstops, tearing_state, true, false, nothing, ignored_connections, preface, parent, From 38a6df7d3e6b068ae9c6a333ea9f00b906b3aa64 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 28 May 2025 18:21:00 +0530 Subject: [PATCH 1915/2176] feat: separate parameter-only equations to `parameter_dependencies` in `complete` --- src/systems/abstractsystem.jl | 88 ++++++++++++++++++----------------- 1 file changed, 45 insertions(+), 43 deletions(-) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index 33c4d72c00..512442078c 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -632,6 +632,7 @@ function complete( @set! newsys.parent = complete(sys; split = false, flatten = false) end sys = newsys + sys = process_parameter_equations(sys) if add_initial_parameters sys = add_initialization_parameters(sys; split) end @@ -2760,52 +2761,53 @@ function Symbolics.substitute(sys::AbstractSystem, rules::Union{Vector{<:Pair}, end end -struct InvalidParameterDependenciesType - got::Any -end +""" + $(TYPEDSIGNATURES) -function Base.showerror(io::IO, err::InvalidParameterDependenciesType) - print( - io, "Parameter dependencies must be a `Dict`, or an array of `Pair` or `Equation`.") - if err.got !== nothing - print(io, " Got ", err.got) +Find equations of `sys` involving only parameters and separate them out into the +`parameter_dependencies` field. Relative ordering of equations is maintained. +Parameter-only equations are validated to be explicit and sorted topologically. All such +explicitly determined parameters are removed from the parameters of `sys`. Return the new +system. +""" +function process_parameter_equations(sys::AbstractSystem) + if !isempty(get_systems(sys)) + throw(ArgumentError("Expected flattened system")) end -end - -function process_parameter_dependencies(pdeps, ps) - if pdeps === nothing || isempty(pdeps) - return Equation[], ps + varsbuf = Set() + pareq_idxs = Int[] + eqs = equations(sys) + for (i, eq) in enumerate(eqs) + empty!(varsbuf) + vars!(varsbuf, eq; op = Union{Differential, Initial, Pre}) + # singular equations + isempty(varsbuf) && continue + if all(varsbuf) do sym + is_parameter(sys, sym) || + symbolic_type(sym) == ArraySymbolic() && + is_sized_array_symbolic(sym) && + all(Base.Fix1(is_parameter, sys), collect(sym)) + end + if !isparameter(eq.lhs) + throw(ArgumentError(""" + LHS of parameter dependency equation must be a single parameter. Found \ + $(eq.lhs). + """)) + end + push!(pareq_idxs, i) + end end - if pdeps isa Dict - pdeps = [k ~ v for (k, v) in pdeps] - else - pdeps isa AbstractArray || throw(InvalidParameterDependenciesType(pdeps)) - pdeps = [if p isa Pair - p[1] ~ p[2] - elseif p isa Equation - p - else - error("Parameter dependencies must be a `Dict`, `Vector{Pair}` or `Vector{Equation}`") - end - for p in pdeps] - end - lhss = [] - for p in pdeps - if !isparameter(p.lhs) - error("LHS of parameter dependency must be a single parameter. Found $(p.lhs).") - end - syms = vars(p.rhs) - if !all(isparameter, syms) - error("RHS of parameter dependency must only include parameters. Found $(p.rhs)") - end - push!(lhss, p.lhs) - end - lhss = map(identity, lhss) - pdeps = topsort_equations(pdeps, union(ps, lhss)) - ps = filter(ps) do p - !any(isequal(p), lhss) - end - return pdeps, ps + + pareqs = [get_parameter_dependencies(sys); eqs[pareq_idxs]] + explicitpars = [eq.lhs for eq in pareqs] + pareqs = topsort_equations(pareqs, explicitpars) + + eqs = eqs[setdiff(eachindex(eqs), pareq_idxs)] + + @set! sys.eqs = eqs + @set! sys.parameter_dependencies = pareqs + @set! sys.ps = setdiff(get_ps(sys), explicitpars) + return sys end """ From 8b5270bbd11f7f2d812c92f52721701843b85752 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 28 May 2025 18:22:01 +0530 Subject: [PATCH 1916/2176] refactor: separate out `GlobalScope` discovery in `complete` --- src/systems/abstractsystem.jl | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index 512442078c..2049c221a4 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -596,6 +596,22 @@ function isinitial(p) operation(p) === getindex && isinitial(arguments(p)[1])) end +""" + $(TYPEDSIGNATURES) + +Find [`GlobalScope`](@ref)d variables in `sys` and add them to the unknowns/parameters. +""" +function discover_globalscoped(sys::AbstractSystem) + newunknowns = OrderedSet() + newparams = OrderedSet() + iv = has_iv(sys) ? get_iv(sys) : nothing + collect_scoped_vars!(newunknowns, newparams, sys, iv; depth = -1) + setdiff!(newunknowns, observables(sys)) + @set! sys.ps = unique!(vcat(get_ps(sys), collect(newparams))) + @set! sys.unknowns = unique!(vcat(get_unknowns(sys), collect(newunknowns))) + return sys +end + """ $(TYPEDSIGNATURES) @@ -612,13 +628,7 @@ using [`toggle_namespacing`](@ref). """ function complete( sys::AbstractSystem; split = true, flatten = true, add_initial_parameters = true) - newunknowns = OrderedSet() - newparams = OrderedSet() - iv = has_iv(sys) ? get_iv(sys) : nothing - collect_scoped_vars!(newunknowns, newparams, sys, iv; depth = -1) - # don't update unknowns to not disturb `mtkcompile` order - # `GlobalScope`d unknowns will be picked up and added there - @set! sys.ps = unique!(vcat(get_ps(sys), collect(newparams))) + sys = discover_globalscoped(sys) if flatten eqs = equations(sys) From 19d970cd8e61d4c7af7376cd4ee591037edfc204 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 28 May 2025 18:22:59 +0530 Subject: [PATCH 1917/2176] refactor: require `complete` system in `dependent_parameters`, `parameter_dependencies` --- src/systems/abstractsystem.jl | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index 2049c221a4..06728289c4 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -1274,6 +1274,12 @@ function parameters(sys::AbstractSystem; initial_parameters = false) end function dependent_parameters(sys::AbstractSystem) + if !iscomplete(sys) + throw(ArgumentError(""" + `dependent_parameters` requires that the system is marked as complete. Call + `complete` or `mtkcompile` on the system. + """)) + end return map(eq -> eq.lhs, parameter_dependencies(sys)) end @@ -1290,23 +1296,22 @@ function parameters_toplevel(sys::AbstractSystem) end """ -$(TYPEDSIGNATURES) -Get the parameter dependencies of the system `sys` and its subsystems. + $(TYPEDSIGNATURES) -See also [`defaults`](@ref) and [`ModelingToolkit.get_parameter_dependencies`](@ref). +Get the parameter dependencies of the system `sys` and its subsystems. Requires that the +system is `complete`d. """ function parameter_dependencies(sys::AbstractSystem) + if !iscomplete(sys) + throw(ArgumentError(""" + `parameter_dependencies` requires that the system is marked as complete. Call \ + `complete` or `mtkcompile` on the system. + """)) + end if !has_parameter_dependencies(sys) return Equation[] end - pdeps = get_parameter_dependencies(sys) - systems = get_systems(sys) - # put pdeps after those of subsystems to maintain topological sorted order - namespaced_deps = mapreduce( - s -> map(eq -> namespace_equation(eq, s), parameter_dependencies(s)), vcat, - systems; init = Equation[]) - - return vcat(namespaced_deps, pdeps) + get_parameter_dependencies(sys) end function full_parameters(sys::AbstractSystem) From f5015936167b79fe0f8adec1d543f6d68a7863ff Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 28 May 2025 18:23:58 +0530 Subject: [PATCH 1918/2176] refactor: account for new parameter dependencies --- src/systems/abstractsystem.jl | 31 +++++++++++++------- src/systems/codegen_utils.jl | 2 +- src/systems/diffeqs/basic_transformations.jl | 3 +- src/systems/index_cache.jl | 2 +- src/systems/nonlinear/initializesystem.jl | 8 ++--- src/systems/system.jl | 20 ++++++++----- src/systems/systems.jl | 4 ++- src/utils.jl | 9 ------ 8 files changed, 44 insertions(+), 35 deletions(-) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index 06728289c4..533206883f 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -295,7 +295,7 @@ function has_parameter_dependency_with_lhs(sys, sym) if has_index_cache(sys) && (ic = get_index_cache(sys)) !== nothing return haskey(ic.dependent_pars_to_timeseries, unwrap(sym)) else - return any(isequal(sym), [eq.lhs for eq in parameter_dependencies(sys)]) + return any(isequal(sym), [eq.lhs for eq in get_parameter_dependencies(sys)]) end end @@ -565,7 +565,7 @@ function add_initialization_parameters(sys::AbstractSystem; split = true) D = Differential(get_iv(sys)) union!(all_initialvars, [D(v) for v in all_initialvars if iscall(v)]) end - for eq in parameter_dependencies(sys) + for eq in get_parameter_dependencies(sys) is_variable_floatingpoint(eq.lhs) || continue push!(all_initialvars, eq.lhs) end @@ -1314,8 +1314,15 @@ function parameter_dependencies(sys::AbstractSystem) get_parameter_dependencies(sys) end +""" + $(TYPEDSIGNATURES) + +Return all of the parameters of the system, including hidden initial parameters and ones +eliminated via `parameter_dependencies`. +""" function full_parameters(sys::AbstractSystem) - vcat(parameters(sys; initial_parameters = true), dependent_parameters(sys)) + dep_ps = [eq.lhs for eq in get_parameter_dependencies(sys)] + vcat(parameters(sys; initial_parameters = true), dep_ps) end """ @@ -2095,7 +2102,7 @@ function Base.show( end # Print parameter dependencies - npdeps = has_parameter_dependencies(sys) ? length(parameter_dependencies(sys)) : 0 + npdeps = has_parameter_dependencies(sys) ? length(get_parameter_dependencies(sys)) : 0 npdeps > 0 && printstyled(io, "\nParameter dependencies ($npdeps):"; bold) npdeps > 0 && hint && print(io, " see parameter_dependencies($name)") @@ -2604,7 +2611,7 @@ function extend(sys::AbstractSystem, basesys::AbstractSystem; eqs = union(get_eqs(basesys), get_eqs(sys)) sts = union(get_unknowns(basesys), get_unknowns(sys)) ps = union(get_ps(basesys), get_ps(sys)) - dep_ps = union(parameter_dependencies(basesys), parameter_dependencies(sys)) + dep_ps = union(get_parameter_dependencies(basesys), get_parameter_dependencies(sys)) obs = union(get_observed(basesys), get_observed(sys)) cevs = union(get_continuous_events(basesys), get_continuous_events(sys)) devs = union(get_discrete_events(basesys), get_discrete_events(sys)) @@ -2612,7 +2619,7 @@ function extend(sys::AbstractSystem, basesys::AbstractSystem; meta = merge(get_metadata(basesys), get_metadata(sys)) syss = union(get_systems(basesys), get_systems(sys)) args = length(ivs) == 0 ? (eqs, sts, ps) : (eqs, ivs[1], sts, ps) - kwargs = (parameter_dependencies = dep_ps, observed = obs, continuous_events = cevs, + kwargs = (observed = obs, continuous_events = cevs, discrete_events = devs, defaults = defs, systems = syss, metadata = meta, name = name, description = description, gui_metadata = gui_metadata) @@ -2626,7 +2633,10 @@ function extend(sys::AbstractSystem, basesys::AbstractSystem; kwargs, (; assertions = merge(get_assertions(basesys), get_assertions(sys)))) end - return T(args...; kwargs...) + newsys = T(args...; kwargs...) + @set! newsys.parameter_dependencies = dep_ps + + return newsys end """ @@ -2768,9 +2778,10 @@ function Symbolics.substitute(sys::AbstractSystem, rules::Union{Vector{<:Pair}, initialization_eqs = fast_substitute(get_initialization_eqs(sys), rules) cstrs = fast_substitute(get_constraints(sys), rules) subsys = map(s -> substitute(s, rules), get_systems(sys)) - System(eqs, get_iv(sys); name = nameof(sys), defaults = defs, - guesses = guess, parameter_dependencies = pdeps, systems = subsys, noise_eqs, + newsys = System(eqs, get_iv(sys); name = nameof(sys), defaults = defs, + guesses = guess, systems = subsys, noise_eqs, observed, initialization_eqs, constraints = cstrs) + @set! newsys.parameter_dependencies = pdeps else error("substituting symbols is not supported for $(typeof(sys))") end @@ -2846,7 +2857,7 @@ See also: [`ModelingToolkit.dump_variable_metadata`](@ref), [`ModelingToolkit.du """ function dump_parameters(sys::AbstractSystem) defs = defaults(sys) - pdeps = parameter_dependencies(sys) + pdeps = get_parameter_dependencies(sys) metas = map(dump_variable_metadata.(parameters(sys))) do meta if haskey(defs, meta.var) meta = merge(meta, (; default = defs[meta.var])) diff --git a/src/systems/codegen_utils.jl b/src/systems/codegen_utils.jl index c3b652740e..d6bcd06d07 100644 --- a/src/systems/codegen_utils.jl +++ b/src/systems/codegen_utils.jl @@ -246,7 +246,7 @@ function build_function_wrapper(sys::AbstractSystem, expr, args...; p_start = 2, p_start += 1 p_end += 1 end - pdeps = parameter_dependencies(sys) + pdeps = get_parameter_dependencies(sys) # only get the necessary observed equations, avoiding extra computation if add_observed && !isempty(obs) diff --git a/src/systems/diffeqs/basic_transformations.jl b/src/systems/diffeqs/basic_transformations.jl index 6243ca2405..430260f60a 100644 --- a/src/systems/diffeqs/basic_transformations.jl +++ b/src/systems/diffeqs/basic_transformations.jl @@ -223,12 +223,13 @@ function change_independent_variable( wasflat = isempty(systems) sys = typeof(sys)( # recreate system with transformed fields eqs, iv2, unknowns, ps; observed, initialization_eqs, - parameter_dependencies, defaults, guesses, connector_type, + defaults, guesses, connector_type, assertions, name = nameof(sys), description = description(sys) ) sys = compose(sys, systems) # rebuild hierarchical system if wascomplete sys = complete(sys; split = wassplit, flatten = wasflat) # complete output if input was complete + @set! sys.parameter_dependencies = parameter_dependencies end return sys end diff --git a/src/systems/index_cache.jl b/src/systems/index_cache.jl index 593de06838..c66c562e9c 100644 --- a/src/systems/index_cache.jl +++ b/src/systems/index_cache.jl @@ -315,7 +315,7 @@ function IndexCache(sys::AbstractSystem) dependent_pars_to_timeseries = Dict{ Union{BasicSymbolic, CallWithMetadata}, TimeseriesSetType}() - for eq in parameter_dependencies(sys) + for eq in get_parameter_dependencies(sys) sym = eq.lhs vs = vars(eq.rhs) timeseries = TimeseriesSetType() diff --git a/src/systems/nonlinear/initializesystem.jl b/src/systems/nonlinear/initializesystem.jl index ab09c71280..7769f20837 100644 --- a/src/systems/nonlinear/initializesystem.jl +++ b/src/systems/nonlinear/initializesystem.jl @@ -167,15 +167,15 @@ function generate_initializesystem_timevarying(sys::AbstractSystem; for k in keys(defs) defs[k] = substitute(defs[k], paramsubs) end - return System(eqs_ics, + isys = System(eqs_ics, vars, pars; defaults = defs, checks = check_units, - parameter_dependencies = new_parameter_deps, name, is_initializesystem = true, kwargs...) + @set isys.parameter_dependencies = new_parameter_deps end """ @@ -280,15 +280,15 @@ function generate_initializesystem_timeindependent(sys::AbstractSystem; for k in keys(defs) defs[k] = substitute(defs[k], paramsubs) end - return System(eqs_ics, + isys = System(eqs_ics, vars, pars; defaults = defs, checks = check_units, - parameter_dependencies = new_parameter_deps, name, is_initializesystem = true, kwargs...) + @set isys.parameter_dependencies = new_parameter_deps end """ diff --git a/src/systems/system.jl b/src/systems/system.jl index 6f6298c70d..1b0fa940f6 100644 --- a/src/systems/system.jl +++ b/src/systems/system.jl @@ -723,8 +723,8 @@ function flatten(sys::System, noeqs = false) parameters(sys; initial_parameters = true), brownians(sys); jumps = jumps(sys), constraints = constraints(sys), costs = costs, consolidate = default_consolidate, observed = observed(sys), - parameter_dependencies = parameter_dependencies(sys), defaults = defaults(sys), - guesses = guesses(sys), continuous_events = continuous_events(sys), + defaults = defaults(sys), guesses = guesses(sys), + continuous_events = continuous_events(sys), discrete_events = discrete_events(sys), assertions = assertions(sys), is_dde = is_dde(sys), tstops = symbolic_tstops(sys), initialization_eqs = initialization_equations(sys), @@ -924,12 +924,12 @@ function NonlinearSystem(sys::System) fast_substitute(eq, subrules) end nsys = System(eqs, unknowns(sys), [parameters(sys); get_iv(sys)]; - parameter_dependencies = parameter_dependencies(sys), defaults = merge(defaults(sys), Dict(get_iv(sys) => Inf)), guesses = guesses(sys), initialization_eqs = initialization_equations(sys), name = nameof(sys), - observed = obs) + observed = obs, systems = map(NonlinearSystem, get_systems(sys))) if iscomplete(sys) nsys = complete(nsys; split = is_split(sys)) + @set! nsys.parameter_dependencies = get_parameter_dependencies(sys) end return nsys end @@ -983,25 +983,29 @@ end Construct a system of equations with associated noise terms. """ -function SDESystem(eqs::Vector{Equation}, noise, iv; is_scalar_noise = false, kwargs...) +function SDESystem(eqs::Vector{Equation}, noise, iv; is_scalar_noise = false, + parameter_dependencies = Equation[], kwargs...) if is_scalar_noise if !(noise isa Vector) throw(ArgumentError("Expected noise to be a vector if `is_scalar_noise`")) end noise = repeat(reshape(noise, (1, :)), length(eqs)) end - return System(eqs, iv; noise_eqs = noise, kwargs...) + sys = System(eqs, iv; noise_eqs = noise, kwargs...) + @set sys.parameter_dependencies = parameter_dependencies end function SDESystem( - eqs::Vector{Equation}, noise, iv, dvs, ps; is_scalar_noise = false, kwargs...) + eqs::Vector{Equation}, noise, iv, dvs, ps; is_scalar_noise = false, + parameter_dependencies = Equation[], kwargs...) if is_scalar_noise if !(noise isa Vector) throw(ArgumentError("Expected noise to be a vector if `is_scalar_noise`")) end noise = repeat(reshape(noise, (1, :)), length(eqs)) end - return System(eqs, iv, dvs, ps; noise_eqs = noise, kwargs...) + sys = System(eqs, iv, dvs, ps; noise_eqs = noise, kwargs...) + @set sys.parameter_dependencies = parameter_dependencies end function SDESystem(sys::System, noise; kwargs...) diff --git a/src/systems/systems.jl b/src/systems/systems.jl index 63585541e0..4a6c1ccbb4 100644 --- a/src/systems/systems.jl +++ b/src/systems/systems.jl @@ -159,10 +159,12 @@ function __mtkcompile(sys::AbstractSystem; simplify = false, ssys = System(Vector{Equation}(full_equations(ode_sys)), get_iv(ode_sys), unknowns(ode_sys), parameters(ode_sys); noise_eqs, name = nameof(ode_sys), observed = observed(ode_sys), defaults = defaults(sys), - parameter_dependencies = parameter_dependencies(sys), assertions = assertions(sys), + assertions = assertions(sys), guesses = guesses(sys), initialization_eqs = initialization_equations(sys), continuous_events = continuous_events(sys), discrete_events = discrete_events(sys)) + @set! ssys.parameter_dependencies = get_parameter_dependencies(sys) + return ssys end end diff --git a/src/utils.jl b/src/utils.jl index 5397b23077..8fcf8d7a25 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -512,15 +512,6 @@ function collect_scoped_vars!(unknowns, parameters, sys, iv; depth = 1, op = Dif collect_vars!(unknowns, parameters, eq, iv; depth, op) end end - if has_parameter_dependencies(sys) - for eq in parameter_dependencies(sys) - if eq isa Pair - collect_vars!(unknowns, parameters, eq, iv; depth, op) - else - collect_vars!(unknowns, parameters, eq, iv; depth, op) - end - end - end if has_constraints(sys) for eq in constraints(sys) eqtype_supports_collect_vars(eq) || continue From 048de4e505268faf87f9727fa35182bd2a9a95a4 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 28 May 2025 18:24:11 +0530 Subject: [PATCH 1919/2176] feat: separate out parameter dependencies in `TearingState` --- src/systems/systemstructure.jl | 1 + 1 file changed, 1 insertion(+) diff --git a/src/systems/systemstructure.jl b/src/systems/systemstructure.jl index 4b01917bcf..32da7a81bf 100644 --- a/src/systems/systemstructure.jl +++ b/src/systems/systemstructure.jl @@ -271,6 +271,7 @@ end function TearingState(sys; quick_cancel = false, check = true, sort_eqs = true) # flatten system sys = flatten(sys) + sys = process_parameter_equations(sys) ivs = independent_variables(sys) iv = length(ivs) == 1 ? ivs[1] : nothing # flatten array equations From f26064fbff9d150b615c8dadc82a40894b27f0f9 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 28 May 2025 18:24:25 +0530 Subject: [PATCH 1920/2176] test: account for new parameter dependencies in tests --- test/initializationsystem.jl | 15 +++--- test/mtkparameters.jl | 17 ++++--- test/nonlinearsystem.jl | 9 ++-- test/odesystem.jl | 17 ++++--- test/parameter_dependencies.jl | 78 ++++++++++++++--------------- test/sdesystem.jl | 4 +- test/symbolic_indexing_interface.jl | 2 +- test/test_variable_metadata.jl | 5 +- test/variable_scope.jl | 2 +- 9 files changed, 76 insertions(+), 73 deletions(-) diff --git a/test/initializationsystem.jl b/test/initializationsystem.jl index 244e4ea4d3..1e83ec579f 100644 --- a/test/initializationsystem.jl +++ b/test/initializationsystem.jl @@ -486,7 +486,7 @@ sys = mtkcompile(unsimp; fully_determined = false) @variables x(t) y(t) @named sysx = System([D(x) ~ 0], t; initialization_eqs = [x ~ 1]) @named sysy = System([D(y) ~ 0], t; initialization_eqs = [y^2 ~ 2], guesses = [y => 1]) -sys = extend(sysx, sysy) +sys = complete(extend(sysx, sysy)) @test length(equations(generate_initializesystem(sys))) == 2 @test length(ModelingToolkit.guesses(sys)) == 1 @@ -920,7 +920,7 @@ end (ModelingToolkit.System, SDDEProblem, ImplicitEM(), _x(t - 0.1) + a) ] @mtkcompile sys = System( - [D(x) ~ 2x + r + rhss], t; parameter_dependencies = [r ~ p + 2q, q ~ p + 3], + [D(x) ~ 2x + r + rhss, r ~ p + 2q, q ~ p + 3], t; guesses = [p => 1.0]) prob = Problem(sys, [x => 1.0, p => missing], (0.0, 1.0)) @test length(equations(ModelingToolkit.get_parent(prob.f.initialization_data.initializeprob.f.sys))) == @@ -1061,8 +1061,8 @@ end @testset "Nonnumeric parameter dependencies are retained" begin @variables x(t) y(t) @parameters foo(::Real, ::Real) p - @mtkcompile sys = System([D(x) ~ t, 0 ~ foo(x, y)], t; - parameter_dependencies = [foo ~ Multiplier(p, 2p)], guesses = [y => -1.0]) + @mtkcompile sys = System([D(x) ~ t, 0 ~ foo(x, y), foo ~ Multiplier(p, 2p)], t; + guesses = [y => -1.0]) prob = ODEProblem(sys, [x => 1.0, p => 1.0], (0.0, 1.0)) integ = init(prob, Rosenbrock23()) @test integ[y] ≈ -0.5 @@ -1241,7 +1241,7 @@ end @variables x(t) [guess = 1.0] y(t) [guess = 1.0] @parameters p [guess = 1.0] q [guess = 1.0] @mtkcompile sys = System( - [D(x) ~ p * x + q * y, y ~ 2x], t; parameter_dependencies = [q ~ 2p]) + [D(x) ~ p * x + q * y, y ~ 2x, q ~ 2p], t) prob = ODEProblem(sys, [x => 1.0, p => 1.0], (0.0, 1.0)) test_dummy_initialization_equation(prob, x) prob2 = remake(prob; u0 = [x => 2.0]) @@ -1262,7 +1262,7 @@ end @variables x(t) [guess = 1.0] y(t) [guess = 1.0] @parameters p [guess = 1.0] q [guess = 1.0] @mtkcompile sys = System( - [D(x) ~ p * x + q * y, y ~ 2x], t; parameter_dependencies = [q ~ 2p]) + [D(x) ~ p * x + q * y, y ~ 2x, q ~ 2p], t) prob = ODEProblem(sys, [:x => 1.0, p => 1.0], (0.0, 1.0)) test_dummy_initialization_equation(prob, x) prob2 = remake(prob; u0 = [:x => 2.0]) @@ -1442,8 +1442,7 @@ end end @testset "$Problem" for Problem in [NonlinearProblem, NonlinearLeastSquaresProblem] @parameters p1 p2 - @mtkcompile sys = System([x^2 + y^2 ~ p1, (x - 1)^2 + (y - 1)^2 ~ p2]; - parameter_dependencies = [p2 ~ 2p1], + @mtkcompile sys = System([x^2 + y^2 ~ p1, (x - 1)^2 + (y - 1)^2 ~ p2, p2 ~ 2p1]; guesses = [p1 => 0.0], defaults = [p1 => missing]) prob = Problem(sys, [x => 1.0, y => 1.0, p2 => 6.0]) @test prob.ps[p1] ≈ 3.0 diff --git a/test/mtkparameters.jl b/test/mtkparameters.jl index 2f52dad30b..1508d95a74 100644 --- a/test/mtkparameters.jl +++ b/test/mtkparameters.jl @@ -9,7 +9,7 @@ using JET @parameters a b c(t) d::Integer e[1:3] f[1:3, 1:3]::Int g::Vector{AbstractFloat} h::String @named sys = System( - Equation[], t, [], [a, c, d, e, f, g, h], parameter_dependencies = [b ~ 2a], + [b ~ 2a], t, [], [a, b, c, d, e, f, g, h]; continuous_events = [ModelingToolkit.SymbolicContinuousCallback( [a ~ 0] => [c ~ 0], discrete_parameters = c)], defaults = Dict(a => 0.0)) sys = complete(sys) @@ -178,10 +178,11 @@ function level1() D = Differential(t) eqs = [D(x) ~ p1 * x - p2 * x * y - D(y) ~ -p3 * y + p4 * x * y] + D(y) ~ -p3 * y + p4 * x * y + y0 ~ 2p4] sys = mtkcompile(complete(System( - eqs, t, name = :sys, parameter_dependencies = [y0 => 2p4]))) + eqs, t, name = :sys))) prob = ODEProblem{true, SciMLBase.FullSpecialize}(sys, [], (0.0, 3.0)) end @@ -192,10 +193,11 @@ function level2() D = Differential(t) eqs = [D(x) ~ p1 * x - p23[1] * x * y - D(y) ~ -p23[2] * y + p4 * x * y] + D(y) ~ -p23[2] * y + p4 * x * y + y0 ~ 2p4] sys = mtkcompile(complete(System( - eqs, t, name = :sys, parameter_dependencies = [y0 => 2p4]))) + eqs, t, name = :sys))) prob = ODEProblem{true, SciMLBase.FullSpecialize}(sys, [], (0.0, 3.0)) end @@ -206,10 +208,11 @@ function level3() D = Differential(t) eqs = [D(x) ~ p1 * x - p23[1] * x * y - D(y) ~ -p23[2] * y + p4 * x * y] + D(y) ~ -p23[2] * y + p4 * x * y + y0 ~ 2p4] sys = mtkcompile(complete(System( - eqs, t, name = :sys, parameter_dependencies = [y0 => 2p4]))) + eqs, t, name = :sys))) prob = ODEProblem{true, SciMLBase.FullSpecialize}(sys, [], (0.0, 3.0)) end diff --git a/test/nonlinearsystem.jl b/test/nonlinearsystem.jl index e4a5b418b9..704d2de5de 100644 --- a/test/nonlinearsystem.jl +++ b/test/nonlinearsystem.jl @@ -368,12 +368,13 @@ end @testset "Can convert from `System`" begin @variables x(t) y(t) @parameters p q r - @named sys = System([D(x) ~ p * x^3 + q, 0 ~ -y + q * x - r], t; + @named sys = System([D(x) ~ p * x^3 + q, 0 ~ -y + q * x - r, r ~ 3p], t; defaults = [x => 1.0, p => missing], guesses = [p => 1.0], - initialization_eqs = [p^3 + q^3 ~ 4r], parameter_dependencies = [r ~ 3p]) + initialization_eqs = [p^3 + q^3 ~ 4r]) nlsys = NonlinearSystem(sys) + nlsys = complete(nlsys) defs = defaults(nlsys) - @test length(defs) == 3 + @test length(defs) == 6 @test defs[x] == 1.0 @test defs[p] === missing @test isinf(defs[t]) @@ -384,7 +385,7 @@ end @test length(equations(nlsys)) == 2 @test all(iszero, [eq.lhs for eq in equations(nlsys)]) @test nameof(nlsys) == nameof(sys) - @test !ModelingToolkit.iscomplete(nlsys) + @test ModelingToolkit.iscomplete(nlsys) sys1 = complete(sys; split = false) nlsys = NonlinearSystem(sys1) diff --git a/test/odesystem.jl b/test/odesystem.jl index 7bf4dfebb0..be301f34f1 100644 --- a/test/odesystem.jl +++ b/test/odesystem.jl @@ -881,6 +881,7 @@ eqs = [D(D(q₁)) ~ -λ * q₁, q₂ ~ L * cos(θ)] @named pend = System(eqs, t) +pend = complete(pend) @test_nowarn generate_initializesystem( pend; op = [q₁ => 1.0, q₂ => 0.0], guesses = [λ => 1]) @@ -1189,14 +1190,15 @@ end @testset "Substituting preserves parameter dependencies, defaults, guesses" begin @parameters p1 p2 @variables x(t) y(t) - @named sys = System([D(x) ~ y + p2], t; parameter_dependencies = [p2 ~ 2p1], + @named sys = System([D(x) ~ y + p2, p2 ~ 2p1], t; defaults = [p1 => 1.0, p2 => 2.0], guesses = [p1 => 2.0, p2 => 3.0]) @parameters p3 sys2 = substitute(sys, [p1 => p3]) + sys2 = complete(sys2) @test length(parameters(sys2)) == 1 @test is_parameter(sys2, p3) @test !is_parameter(sys2, p1) - @test length(ModelingToolkit.defaults(sys2)) == 2 + @test length(ModelingToolkit.defaults(sys2)) == 7 @test ModelingToolkit.defaults(sys2)[p3] == 1.0 @test length(ModelingToolkit.guesses(sys2)) == 2 @test ModelingToolkit.guesses(sys2)[p3] == 2.0 @@ -1205,22 +1207,23 @@ end @testset "Substituting with nested systems" begin @parameters p1 p2 @variables x(t) y(t) - @named innersys = System([D(x) ~ y + p2], t; parameter_dependencies = [p2 ~ 2p1], + @named innersys = System([D(x) ~ y + p2; p2 ~ 2p1], t; defaults = [p1 => 1.0, p2 => 2.0], guesses = [p1 => 2.0, p2 => 3.0]) @parameters p3 p4 @named outersys = System( - [D(innersys.y) ~ innersys.y + p4], t; parameter_dependencies = [p4 ~ 3p3], + [D(innersys.y) ~ innersys.y + p4, p4 ~ 3p3], t; defaults = [p3 => 3.0, p4 => 9.0], guesses = [p4 => 10.0], systems = [innersys]) @test_nowarn mtkcompile(outersys) @parameters p5 sys2 = substitute(outersys, [p4 => p5]) + sys2 = complete(sys2) @test_nowarn mtkcompile(sys2) @test length(equations(sys2)) == 2 @test length(parameters(sys2)) == 2 - @test length(full_parameters(sys2)) == 4 + @test length(full_parameters(sys2)) == 10 @test all(!isequal(p4), full_parameters(sys2)) @test any(isequal(p5), full_parameters(sys2)) - @test length(ModelingToolkit.defaults(sys2)) == 4 + @test length(ModelingToolkit.defaults(sys2)) == 10 @test ModelingToolkit.defaults(sys2)[p5] == 9.0 @test length(ModelingToolkit.guesses(sys2)) == 3 @test ModelingToolkit.guesses(sys2)[p5] == 10.0 @@ -1296,7 +1299,7 @@ end @testset "Parameter dependencies with constant RHS" begin @parameters p - @test_nowarn System(Equation[], t; parameter_dependencies = [p ~ 1.0], name = :a) + @test_nowarn System([p ~ 1.0], t; name = :a) end @testset "Variable discovery in arrays of `Num` inside callable symbolic" begin diff --git a/test/parameter_dependencies.jl b/test/parameter_dependencies.jl index 0abfd2e63e..debdeac6e5 100644 --- a/test/parameter_dependencies.jl +++ b/test/parameter_dependencies.jl @@ -21,9 +21,8 @@ using NonlinearSolve cb3 = SymbolicDiscreteCallback([1.0] => [p1 ~ 5.0], discrete_parameters = [p1]) @mtkcompile sys = System( - [D(x) ~ p1 * t + p2], + [D(x) ~ p1 * t + p2, p2 ~ 2p1], t; - parameter_dependencies = [p2 => 2p1], continuous_events = [cb1, cb2], discrete_events = [cb3] ) @@ -55,9 +54,8 @@ end @variables x(t) = 0 @named sys = System( - [D(x) ~ sum(p1) * t + sum(p2)], - t; - parameter_dependencies = [p2 => 2p1] + [D(x) ~ sum(p1) * t + sum(p2), p2 ~ 2p1], + t ) prob = ODEProblem(complete(sys), [], (0.0, 1.0)) setp1! = setp(prob, p1) @@ -78,11 +76,10 @@ end t ) @named sys2 = System( - Equation[], - t; - parameter_dependencies = [p2 => 2p1] + [p2 ~ 2p1], + t ) - sys = extend(sys2, sys1) + sys = complete(extend(sys2, sys1)) @test !(p2 in Set(parameters(sys))) @test p2 in Set(full_parameters(sys)) prob = ODEProblem(complete(sys), nothing, (0.0, 1.0)) @@ -95,9 +92,8 @@ end @variables x(t) = 0 @named sys = System( - [D(x) ~ p1 * t + p2], - t; - parameter_dependencies = [p2 => 2p1] + [D(x) ~ p1 * t + p2, p2 ~ 2p1], + t ) prob = ODEProblem(complete(sys), nothing, (0.0, 1.0)) get_dep = getu(prob, 2p2) @@ -109,9 +105,8 @@ end @variables x(t) = 0 @named sys = System( - [D(x) ~ sum(p1) * t + sum(p2)], - t; - parameter_dependencies = [p2 => 2p1] + [D(x) ~ sum(p1) * t + sum(p2), p2 ~ 2p1], + t ) prob = ODEProblem(complete(sys), [], (0.0, 1.0)) get_dep = getu(prob, 2p1) @@ -127,9 +122,8 @@ end t ) @named sys2 = System( - [D(x) ~ p1 * t - p2], - t; - parameter_dependencies = [p2 => 2p1] + [D(x) ~ p1 * t - p2, p2 ~ 2p1], + t ) sys = complete(System(Equation[], t, systems = [sys1, sys2], name = :sys)) @@ -149,7 +143,7 @@ end new_prob = remake(prob, p = [sys2.p1 => 1.5]) - @test !isempty(ModelingToolkit.parameter_dependencies(sys2)) + @test !isempty(ModelingToolkit.parameter_dependencies(sys)) @test new_prob.ps[sys2.p1] == 1.5 @test new_prob.ps[sys2.p2] == 3.0 end @@ -163,13 +157,14 @@ end end @parameters p1 = 1.0 - parameter_dependencies = [sys2.p2 ~ p1 * 2.0] + parameter_dependencies = [] sys1 = System( - Equation[], t, [], [p1]; parameter_dependencies, name = :sys1, systems = [sys2]) + [sys2.p2 ~ p1 * 2.0], t, [], [p1]; name = :sys1, systems = [sys2]) # ensure that parameter_dependencies is type stable # (https://github.com/SciML/ModelingToolkit.jl/pull/2978) - @inferred ModelingToolkit.parameter_dependencies(sys1) + sys = complete(sys1) + @inferred ModelingToolkit.parameter_dependencies(sys) sys = mtkcompile(sys1) @@ -190,9 +185,8 @@ end @variables y(t) = 1 @parameters p=2 (i::CallableFoo)(..) - eqs = [D(y) ~ i(t) + p] - @named model = System(eqs, t, [y], [p, i]; - parameter_dependencies = [i ~ CallableFoo(p)]) + eqs = [D(y) ~ i(t) + p, i ~ CallableFoo(p)] + @named model = System(eqs, t, [y], [p, i]) sys = mtkcompile(model) prob = ODEProblem(sys, [], (0.0, 1.0)) @@ -214,9 +208,10 @@ end u ~ Hold(ud) D(x) ~ -x + u y ~ x - z(k) ~ z(k - 2) + yd(k - 2)] + z(k) ~ z(k - 2) + yd(k - 2) + kq ~ 2kp] @test_throws ModelingToolkit.HybridSystemNotSupportedException @mtkcompile sys = System( - eqs, t; parameter_dependencies = [kq => 2kp]) + eqs, t) @test_skip begin Tf = 1.0 @@ -226,7 +221,7 @@ end (0.0, Tf)) @test_nowarn solve(prob, Tsit5()) - @mtkcompile sys = System(eqs, t; parameter_dependencies = [kq => 2kp], + @mtkcompile sys = System(eqs, t; discrete_events = [SymbolicDiscreteCallback( [0.5] => [kp ~ 2.0], discrete_parameters = [kp])]) prob = ODEProblem(sys, @@ -262,7 +257,7 @@ end 0.1 * z] @named sys = System(eqs, t) - @named sdesys = SDESystem(sys, noiseeqs; parameter_dependencies = [ρ => 2σ]) + @named sdesys = SDESystem(sys, noiseeqs; parameter_dependencies = [ρ ~ 2σ]) sdesys = complete(sdesys) @test !(ρ in Set(parameters(sdesys))) @test ρ in Set(full_parameters(sdesys)) @@ -273,7 +268,7 @@ end @test_nowarn solve(prob, SRIW1()) @named sys = System(eqs, t) - @named sdesys = SDESystem(sys, noiseeqs; parameter_dependencies = [ρ => 2σ], + @named sdesys = SDESystem(sys, noiseeqs; parameter_dependencies = [ρ ~ 2σ], discrete_events = [SymbolicDiscreteCallback( [10.0] => [σ ~ 15.0], discrete_parameters = [σ])]) sdesys = complete(sdesys) @@ -299,10 +294,12 @@ end j₁ = ConstantRateJump(rate₁, affect₁) j₃ = ConstantRateJump(rate₃, affect₃) @named js2 = JumpSystem( - [j₃], t, [S, I, R], [γ, h]; parameter_dependencies = [β => 0.01γ]) - @test issetequal(parameters(js2), [γ, h]) + [j₃, β ~ 0.01γ], t, [S, I, R], [β, γ, h]) + @test issetequal(parameters(js2), [β, γ, h]) @test Set(full_parameters(js2)) == Set([γ, β, h]) js2 = complete(js2) + @test issetequal(parameters(js2), [γ, h]) + @test Set(full_parameters(js2)) == Set([γ, β, h]) tspan = (0.0, 250.0) u₀map = [S => 999, I => 1, R => 0] parammap = [γ => 0.01] @@ -313,7 +310,7 @@ end @test_nowarn solve(jprob, SSAStepper()) @named js2 = JumpSystem( - [j₁, j₃], t, [S, I, R], [γ, h]; parameter_dependencies = [β => 0.01γ], + [j₁, j₃, β ~ 0.01γ], t, [S, I, R], [β, γ, h]; discrete_events = [SymbolicDiscreteCallback( [10.0] => [γ ~ 0.02], discrete_parameters = [γ])]) js2 = complete(js2) @@ -330,8 +327,8 @@ end @testset "NonlinearSystem" begin @parameters p1=1.0 p2=1.0 @variables x(t) - eqs = [0 ~ p1 * x * exp(x) + p2] - @mtkcompile sys = System(eqs; parameter_dependencies = [p2 => 2p1]) + eqs = [0 ~ p1 * x * exp(x) + p2, p2 ~ 2p1] + @mtkcompile sys = System(eqs; parameter_dependencies = [p2 ~ 2p1]) @test isequal(only(parameters(sys)), p1) @test Set(full_parameters(sys)) == Set([p1, p2, Initial(p2), Initial(x)]) prob = NonlinearProblem(sys, [x => 1.0]) @@ -354,9 +351,8 @@ end cb3 = [1.0] => [p1 ~ 5.0] @mtkcompile sys = System( - [D(x) ~ p1 * t + p2], - t; - parameter_dependencies = [p2 => 2p1] + [D(x) ~ p1 * t + p2, p2 ~ 2p1], + t ) prob = ODEProblem(sys, [x => 1.0, p1 => 1.0], (0.0, 1.5), jac = true) prob.ps[p1] = 3.0 @@ -384,12 +380,12 @@ end @testset "Discovery of parameters from dependencies" begin @parameters p1 p2 @variables x(t) y(t) - @named sys = System([D(x) ~ y + p2], t; parameter_dependencies = [p2 ~ 2p1]) + @named sys = System([D(x) ~ y + p2, p2 ~ 2p1], t) @test is_parameter(sys, p1) - @named sys = System([x * y^2 ~ y + p2]; parameter_dependencies = [p2 ~ 2p1]) + @named sys = System([x * y^2 ~ y + p2, p2 ~ 2p1]) @test is_parameter(sys, p1) k = ShiftIndex(t) @named sys = System( - [x(k - 1) ~ x(k) + y(k) + p2], t; parameter_dependencies = [p2 ~ 2p1]) + [x(k - 1) ~ x(k) + y(k) + p2, p2 ~ 2p1], t) @test is_parameter(sys, p1) end diff --git a/test/sdesystem.jl b/test/sdesystem.jl index d2a8f77636..74c05d7e43 100644 --- a/test/sdesystem.jl +++ b/test/sdesystem.jl @@ -936,8 +936,8 @@ end @mtkcompile ssys2 = System([seq], t; name = :ssys) @test ssys1 == ssys2 # true - continuous_events = [[X ~ 1.0] => [X ~ X + 5.0]] - discrete_events = [5.0 => [d ~ d / 2.0]] + continuous_events = [[X ~ 1.0] => [X ~ Pre(X) + 5.0]] + discrete_events = [5.0 => [d ~ Pre(d) / 2.0]] @mtkcompile ssys1 = System([seq], t; name = :ssys, continuous_events) @mtkcompile ssys2 = System([seq], t; name = :ssys) diff --git a/test/symbolic_indexing_interface.jl b/test/symbolic_indexing_interface.jl index 81d444668e..89b6907f4b 100644 --- a/test/symbolic_indexing_interface.jl +++ b/test/symbolic_indexing_interface.jl @@ -208,7 +208,7 @@ end @testset "Parameter dependencies as symbols" begin @variables x(t) = 1.0 @parameters a=1 b - @named model = System(D(x) ~ x + a - b, t, parameter_dependencies = [b ~ a + 1]) + @named model = System([D(x) ~ x + a - b, b ~ a + 1], t) sys = complete(model) prob = ODEProblem(sys, [], (0.0, 1.0)) @test prob.ps[b] == prob.ps[:b] diff --git a/test/test_variable_metadata.jl b/test/test_variable_metadata.jl index 7ce45bb211..482ebf927c 100644 --- a/test/test_variable_metadata.jl +++ b/test/test_variable_metadata.jl @@ -176,8 +176,9 @@ sp = Set(p) # Defaults, guesses overridden by system, parameter dependencies @variables x(t)=1.0 y(t) [guess = 1.0] @parameters p=2.0 q -@named sys = System(Equation[], t, [x, y], [p]; defaults = Dict(x => 2.0, p => 3.0), - guesses = Dict(y => 2.0), parameter_dependencies = [q => 2p]) +@named sys = System([q ~ 2p], t, [x, y], [p, q]; defaults = Dict(x => 2.0, p => 3.0), + guesses = Dict(y => 2.0)) +sys = complete(sys) unks_meta = ModelingToolkit.dump_unknowns(sys) unks_meta = Dict([ModelingToolkit.getname(meta.var) => meta for meta in unks_meta]) @test unks_meta[:x].default == 2.0 diff --git a/test/variable_scope.jl b/test/variable_scope.jl index 080cbcc7c4..13b813122e 100644 --- a/test/variable_scope.jl +++ b/test/variable_scope.jl @@ -123,7 +123,7 @@ defs = ModelingToolkit.defaults(bar) @test length(parameters(sys3)) == 3 @test any(isequal(p3), parameters(sys3)) sys4 = complete(sys3) - @test length(unknowns(sys4)) == 3 + @test length(unknowns(sys4)) == 4 @test length(parameters(sys4)) == 4 sys5 = mtkcompile(sys3) @test length(unknowns(sys5)) == 4 From 0111aa6f65139e2a48fc68fec8fb3585a300b714 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 28 May 2025 23:22:32 +0530 Subject: [PATCH 1921/2176] docs: add v10 release notes to `NEWS.md` --- NEWS.md | 114 +++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 113 insertions(+), 1 deletion(-) diff --git a/NEWS.md b/NEWS.md index d316ac23fb..726bf1628f 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,6 +1,6 @@ # ModelingToolkit v10 Release Notes -### Callbacks +## Callbacks Callback semantics have changed. @@ -17,6 +17,118 @@ event = SymbolicDiscreteCallback( [t == 1] => [p ~ Pre(p) + 1], discrete_parameters = [p]) ``` +## New `mtkcompile` and `@mtkcompile` + +`structural_simplify` is now renamed to `mtkcompile`. `@mtkbuild` is renamed to +`@mtkcompile`. Their functionality remains the same. However, instead of a second +positional argument `structural_simplify(sys, (inputs, outputs))` the inputs and outputs +should be specified via keyword arguments as `mtkcompile(sys; inputs, outputs, disturbance_inputs)`. + +## Reduce reliance on metadata in `mtkcompile` + +Previously, `mtkcompile` (formerly `structural_simplify`) used to rely on the metadata of +symbolic variables to identify variables/parameters/brownians. This was regardless of +what the system expected the variable to be. Now, it respects the information in the system. + +## Unified `System` type + +There is now a single common `System` type for all types of models except PDEs, for which +`PDESystem` still exists. It follows the same syntax as `ODESystem` and `NonlinearSystem` +did. `System(equations, t[, vars, pars])` will construct a time-dependent system. +`System(equations[, vars, pars])` will construct a time-independent system. Refer to the +docstring for `System` for further information. + +Utility constructors are defined for: + + - `NonlinearSystem(sys)` to convert a time-dependent system to a time-independent one for + its steady state. + - `SDESystem(sys, noise_eqs)` to add noise to a system + - `JumpSystem(jumps, ...)` to define a system with jumps. Note that normal equations can + also be passed to `jumps`. + - `OptimizationSystem(cost, ...)` to define a system for optimization. + +All problem constructors validate that the system matches the expected structure for +that problem. + +## No more `parameter_dependencies` + +The `parameter_dependencies` keyword is deprecated. All equations previously passed here +should now be provided as part of the standard equations of the system. If passing parameters +explicitly to the `System` constructor, the dependent parameters (on the left hand side of +parameter dependencies) should also be provided. These will be separated out when calling +`complete` or `mtkcompile`. Calling `parameter_dependencies` or `dependent_parameters` now +requires that the system is completed. The new `SDESystem` constructor still retains the +`parameter_dependencies` keyword argument since the number of equations has to match the +number of columns in `noise_eqs`. + +ModelingToolkit now has discretion of what parameters are eliminated using the parameter +equations during `complete` or `mtkcompile`. + +## New problem and constructors + +Instead of `XProblem(sys, u0map, tspan, pmap)` for time-dependent problems and +`XProblem(sys, u0map, pmap)` for time-independent problems, the syntax has changed to +`XProblem(sys, op, tspan)` and `XProblem(sys, op)` respectively. `op` refers to the +operating point, and is a variable-value mapping containing both unknowns and parameters. + +`XFunction` constructors also no longer accept the list of unknowns and parameters as +positional arguments. + +## Removed `DelayParentScope` + +The outdated `DelayParentScope` has been removed. + +## Removed `XProblemExpr` and `XFunctionExpr` + +The old `XProblemExpr` and `XFunctionExpr` constructors used to build an `Expr` that +constructs `XProblem` and `XFunction` respectively are now removed. This functionality +is now available by passing `expression = Val{true}` to any problem or function constructor. + +## Renaming of `generate_*` and `calculate_*` methods + +Several `generate_*` methods have been renamed, along with some `calculate_*` methods. +The `generate_*` methods also no longer accept a list of unknowns and/or parameters. Refer +to the documentation for more information. + +## New behavior of `getproperty` and `setproperty!` + +Using `getproperty` to access fields of a system has been deprecated for a long time, and +this functionality is now removed. `setproperty!` previously used to update the default +of the accessed symbolic variable. This is not supported anymore. Defaults can be updated by +mutating `ModelingToolkit.get_defaults(sys)`. + +## New behavior of `@constants` + +`@constants` now creates parameters with the `tunable = false` metadata by default. + +## Removed `FunctionalAffect` + +`FunctionalAffect` is now removed in favor of the new `ImperativeAffect`. Refer to the +documentation for more information. + +## Improved system metadata + +Instead of an empty field that can contain arbitrary data, the `System` type stores metadata +identically to `SymbolicUtils.BasicSymbolic`. Metadata is stored in an immutable dictionary +keyed by a user-provided `DataType` and containing arbitrary values. `System` supports the +same `SymbolicUtils.getmetadata` and `SymbolicUtils.setmetadata` API as symbolic variables. +Refer to the documentation of `System` and the aforementioned functions for more information. + +## Moved `connect` and `Connector` to ModelingToolkit + +Previously ModelingToolkit used the `connect` function and `Connector` type defined in +Symbolics.jl. These have now been moved to ModelingToolkit along with the experimental +state machine API. If you imported them from Symbolics.jl, it is recommended to import from +ModelingToolkit instead. + +## Always wrap with `ParentScope` in `@named` + +When creating a system using `@named`, any symbolic quantities passed as keyword arguments +to the subsystem are wrapped in `ParentScope`. Previously, this would only happen if the +variable wasn't already wrapped in a `ParentScope`. However, the old behavior had issues +when passing symbolic quantities down multiple levels of the hierarchy. The `@named` macro +now always performs this wrapping. + # ModelingToolkit v9 Release Notes ### Upgrade guide From 0a4c160fea8c3e616d59ed0c69e2a5b8c8137c1b Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 28 May 2025 23:26:21 +0530 Subject: [PATCH 1922/2176] docs: mention v10 in docs --- docs/src/index.md | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/docs/src/index.md b/docs/src/index.md index 3f079fa1d8..f742975802 100644 --- a/docs/src/index.md +++ b/docs/src/index.md @@ -36,6 +36,19 @@ If you use ModelingToolkit in your work, please cite the following: ## Feature Summary +!!! danger "ModelingToolkit version 10" + + ModelingToolkit version 10 just released. Please refer to the [changelog](https://github.com/SciML/ModelingToolkit.jl/blob/master/NEWS.md) + for a summary of the changes. Some documentation pages may be broken while downstram + packages update to the new version. + +!!! danger "Temporarily broken discrete systems" + + ModelingToolkit's support for purely explicit systems of discrete update equations + (ones solved via `OrdinaryDiffEqFunctionMap.jl`) is temporarily broken. While such + systems can be created, simplfied and solved there are issues with the naming of + simplified unknowns and symbolic indexing of the problem/solution. + ModelingToolkit.jl is a symbolic-numeric modeling package. Thus it combines some of the features from symbolic computing packages like SymPy or Mathematica with the ideas of equation-based modeling systems like the causal Simulink and the From 294f1847cd05a22a51533d5a7681ebef7d557419 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 28 May 2025 23:26:50 +0530 Subject: [PATCH 1923/2176] chore: remove `sources` section from Project.toml --- Project.toml | 5 ----- 1 file changed, 5 deletions(-) diff --git a/Project.toml b/Project.toml index e77aa6377c..1f51599a51 100644 --- a/Project.toml +++ b/Project.toml @@ -201,10 +201,5 @@ StochasticDiffEq = "789caeaf-c7a9-5a7d-9973-96adeb23e2a0" Sundials = "c3572dad-4567-51f8-b174-8c6c989267f4" Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" -[sources] -ModelingToolkitStandardLibrary = { url = "https://github.com/SciML/ModelingToolkitStandardLibrary.jl/", rev = "mtk-v10" } -OptimizationBase = { url = "https://github.com/AayushSabharwal/OptimizationBase.jl", rev = "as/mtk-v10" } -OptimizationMOI = { url = "https://github.com/AayushSabharwal/Optimization.jl", subdir = "lib/OptimizationMOI", rev = "as/mtk-v10" } - [targets] test = ["AmplNLWriter", "BenchmarkTools", "BoundaryValueDiffEqMIRK", "BoundaryValueDiffEqAscher", "ControlSystemsBase", "DataInterpolations", "DelayDiffEq", "NonlinearSolve", "ForwardDiff", "Ipopt", "Ipopt_jll", "ModelingToolkitStandardLibrary", "Optimization", "OptimizationOptimJL", "OptimizationMOI", "OrdinaryDiffEq", "OrdinaryDiffEqCore", "OrdinaryDiffEqDefault", "REPL", "Random", "ReferenceTests", "SafeTestsets", "StableRNGs", "Statistics", "SteadyStateDiffEq", "Test", "StochasticDiffEq", "Sundials", "StochasticDelayDiffEq", "Pkg", "JET", "OrdinaryDiffEqNonlinearSolve", "Logging", "OptimizationBase"] From f5f6fb91bb2f0f1451a20c9582bf5f584f2d1f15 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 28 May 2025 23:53:21 +0530 Subject: [PATCH 1924/2176] docs: temporarily remove downstream libs from docs, mark some pages as draft --- docs/Project.toml | 10 ---------- docs/src/examples/remake.md | 4 ++++ docs/src/tutorials/disturbance_modeling.md | 4 ++++ docs/src/tutorials/optimization.md | 4 ++++ 4 files changed, 12 insertions(+), 10 deletions(-) diff --git a/docs/Project.toml b/docs/Project.toml index f9cf903ed7..ef6af51d79 100644 --- a/docs/Project.toml +++ b/docs/Project.toml @@ -4,7 +4,6 @@ BenchmarkTools = "6e4b80f9-dd63-53aa-95a3-0cdb28fa8baf" BifurcationKit = "0f109fa4-8a5d-4b75-95aa-f515264e7665" CairoMakie = "13f3f980-e62b-5c42-98c6-ff1f3baf88f0" ControlSystemsBase = "aaaaaaaa-a6ca-5380-bf3e-84a91bcd477e" -ControlSystemsMTK = "687d7614-c7e5-45fc-bfc3-9ee385575c88" DataInterpolations = "82cc6244-b520-54b8-b5a6-8a565e85f1d0" Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f" Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4" @@ -16,9 +15,6 @@ ModelingToolkit = "961ee093-0014-501f-94e3-6117800e7a78" ModelingToolkitStandardLibrary = "16a59e39-deab-5bd0-87e4-056b12336739" NonlinearSolve = "8913a72c-1f9b-4ce2-8d82-65094dcecaec" Optim = "429524aa-4258-5aef-a3af-852621145aeb" -Optimization = "7f7a1694-90dd-40f0-9382-eb1efda571ba" -OptimizationBase = "bca83a33-5cc9-4baa-983d-23429ab6bcbb" -OptimizationOptimJL = "36348300-93cb-4f02-beb5-3c3902f8871e" OrdinaryDiffEq = "1dea7af3-3e70-54e6-95c3-0bf5283fa5ed" Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80" PreallocationTools = "d236fae5-4411-538c-8e31-a6e3d9e00b46" @@ -30,10 +26,6 @@ SymbolicUtils = "d1185830-fcd6-423d-90d6-eec64667417b" Symbolics = "0c5d862f-8b57-4792-8d23-62f2024744c7" Unitful = "1986cc42-f94f-5a68-af5c-568840ba703d" -[sources] -ModelingToolkitStandardLibrary = {rev = "mtk-v10", url = "https://github.com/SciML/ModelingToolkitStandardLibrary.jl/"} -OptimizationBase = {rev = "as/mtk-v10", url = "https://github.com/AayushSabharwal/OptimizationBase.jl"} - [compat] Attractors = "1.24" BenchmarkTools = "1.3" @@ -49,8 +41,6 @@ ModelingToolkit = "10" ModelingToolkitStandardLibrary = "2.19" NonlinearSolve = "3, 4" Optim = "1.7" -Optimization = "3.9, 4" -OptimizationOptimJL = "0.1, 0.4" OrdinaryDiffEq = "6.31" Plots = "1.36" PreallocationTools = "0.4" diff --git a/docs/src/examples/remake.md b/docs/src/examples/remake.md index 9d40a83dbf..b42315304e 100644 --- a/docs/src/examples/remake.md +++ b/docs/src/examples/remake.md @@ -1,3 +1,7 @@ +```@meta +Draft = true +``` + # Optimizing through an ODE solve and re-creating MTK Problems Solving an ODE as part of an `OptimizationProblem`'s loss function is a common scenario. diff --git a/docs/src/tutorials/disturbance_modeling.md b/docs/src/tutorials/disturbance_modeling.md index 41c7bd86ee..341077b76b 100644 --- a/docs/src/tutorials/disturbance_modeling.md +++ b/docs/src/tutorials/disturbance_modeling.md @@ -1,3 +1,7 @@ +```@meta +Draft = true +``` + # Disturbance and input modeling modeling Disturbances are often seen as external factors that influence a system. Modeling and simulation of such external influences is common in order to ensure that the plant and or control system can adequately handle or suppress these disturbances. Disturbance modeling is also integral to the problem of state estimation, indeed, modeling how disturbances affect the evolution of the state of the system is crucial in order to accurately estimate this state. diff --git a/docs/src/tutorials/optimization.md b/docs/src/tutorials/optimization.md index 9eb72b36ea..d299416412 100644 --- a/docs/src/tutorials/optimization.md +++ b/docs/src/tutorials/optimization.md @@ -1,3 +1,7 @@ +```@meta +Draft = true +``` + # Modeling Optimization Problems ModelingToolkit.jl is not only useful for generating initial value problems (`ODEProblem`). From 58b37665b10f56c94ef71cc323638ca95ecd4288 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 28 May 2025 23:56:08 +0530 Subject: [PATCH 1925/2176] refactor: remove old `find/replace` in `expand_connections` --- src/systems/connectors.jl | 48 ++++++++++++++++----------------------- 1 file changed, 19 insertions(+), 29 deletions(-) diff --git a/src/systems/connectors.jl b/src/systems/connectors.jl index d771445074..cf2f24a7d1 100644 --- a/src/systems/connectors.jl +++ b/src/systems/connectors.jl @@ -519,11 +519,11 @@ function connection2set!(connectionsets, namespace, ss, isouter; end function generate_connection_set( - sys::AbstractSystem, find = nothing, replace = nothing; scalarize = false) + sys::AbstractSystem; scalarize = false) connectionsets = ConnectionSet[] domain_csets = ConnectionSet[] sys = generate_connection_set!( - connectionsets, domain_csets, sys, find, replace, scalarize, nothing, ignored_connections(sys)) + connectionsets, domain_csets, sys, scalarize, nothing, ignored_connections(sys)) csets = merge(connectionsets) domain_csets = merge([csets; domain_csets], true) @@ -627,7 +627,7 @@ Generate connection sets from `connect` equations. for no namespace (if `sys` is top-level). """ function generate_connection_set!(connectionsets, domain_csets, - sys::AbstractSystem, find, replace, scalarize, namespace = nothing, + sys::AbstractSystem, scalarize, namespace = nothing, ignored_connects = (HierarchyAnalysisPointT[], HierarchyAnalysisPointT[])) subsys = get_systems(sys) ignored_system_aps, ignored_variable_aps = ignored_connects @@ -646,31 +646,21 @@ function generate_connection_set!(connectionsets, domain_csets, # causal variable connections will be expanded before we get here, # but this guard is useful for `n_expanded_connection_equations`. is_causal_variable_connection(rhs) && continue - if find !== nothing && find(rhs, _getname(namespace)) - neweq, extra_unknown = replace(rhs, _getname(namespace)) - if extra_unknown isa AbstractArray - append!(extra_unknowns, unwrap.(extra_unknown)) - elseif extra_unknown !== nothing - push!(extra_unknowns, extra_unknown) - end - neweq isa AbstractArray ? append!(eqs, neweq) : push!(eqs, neweq) + if lhs isa Connection && get_systems(lhs) === :domain + connected_systems = get_systems(rhs) + connection2set!(domain_csets, namespace, connected_systems, isouter; + ignored_systems = systems_to_ignore( + ignored_system_aps, connected_systems), + ignored_variables = variables_to_ignore( + ignored_variable_aps, connected_systems)) + elseif isconnection(rhs) + push!(cts, get_systems(rhs)) else - if lhs isa Connection && get_systems(lhs) === :domain - connected_systems = get_systems(rhs) - connection2set!(domain_csets, namespace, connected_systems, isouter; - ignored_systems = systems_to_ignore( - ignored_system_aps, connected_systems), - ignored_variables = variables_to_ignore( - ignored_variable_aps, connected_systems)) - elseif isconnection(rhs) - push!(cts, get_systems(rhs)) + # split connections and equations + if eq.lhs isa AbstractArray || eq.rhs isa AbstractArray + append!(eqs, Symbolics.scalarize(eq)) else - # split connections and equations - if eq.lhs isa AbstractArray || eq.rhs isa AbstractArray - append!(eqs, Symbolics.scalarize(eq)) - else - push!(eqs, eq) - end + push!(eqs, eq) end end end @@ -699,7 +689,7 @@ function generate_connection_set!(connectionsets, domain_csets, end @set! sys.systems = map( s -> generate_connection_set!(connectionsets, domain_csets, s, - find, replace, scalarize, renamespace(namespace, s), + scalarize, renamespace(namespace, s), ignored_systems_for_subsystem.((s,), ignored_connects)), subsys) @set! sys.eqs = eqs @@ -870,11 +860,11 @@ function expand_variable_connections(sys::AbstractSystem; ignored_variables = no return sys end -function expand_connections(sys::AbstractSystem, find = nothing, replace = nothing; +function expand_connections(sys::AbstractSystem; debug = false, tol = 1e-10, scalarize = true) sys = remove_analysis_points(sys) sys = expand_variable_connections(sys) - sys, (csets, domain_csets) = generate_connection_set(sys, find, replace; scalarize) + sys, (csets, domain_csets) = generate_connection_set(sys; scalarize) ceqs, instream_csets = generate_connection_equations_and_stream_connections(csets) _sys = expand_instream(instream_csets, sys; debug = debug, tol = tol) sys = flatten(sys, true) From 716c8aa5bfda3ac369404dbadd3fc98bd88e8064 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 29 May 2025 13:42:35 +0530 Subject: [PATCH 1926/2176] docs: rename `ODESystem` to `System` --- docs/src/API/variables.md | 4 +-- docs/src/basics/AbstractSystem.md | 2 +- docs/src/basics/Composition.md | 26 ++++++++--------- docs/src/basics/Debugging.md | 4 +-- docs/src/basics/Events.md | 28 +++++++++---------- docs/src/basics/FAQ.md | 8 +++--- docs/src/basics/InputOutput.md | 2 +- docs/src/basics/Linearization.md | 6 ++-- docs/src/basics/MTKLanguage.md | 20 ++----------- docs/src/basics/Precompilation.md | 2 +- docs/src/basics/Validation.md | 2 +- docs/src/examples/higher_order.md | 2 +- docs/src/examples/perturbation.md | 6 ++-- docs/src/examples/tearing_parallelism.md | 2 +- docs/src/index.md | 6 ++-- docs/src/internals.md | 4 +-- docs/src/tutorials/acausal_components.md | 6 ++-- docs/src/tutorials/attractors.md | 2 +- .../bifurcation_diagram_computation.md | 6 ++-- docs/src/tutorials/fmi.md | 2 +- docs/src/tutorials/initialization.md | 2 +- docs/src/tutorials/modelingtoolkitize.md | 2 +- docs/src/tutorials/nonlinear.md | 2 +- docs/src/tutorials/ode_modeling.md | 6 ++-- docs/src/tutorials/optimization.md | 2 +- .../tutorials/programmatically_generating.md | 16 +++++------ docs/src/tutorials/stochastic_diffeq.md | 4 +-- 27 files changed, 80 insertions(+), 94 deletions(-) diff --git a/docs/src/API/variables.md b/docs/src/API/variables.md index 0ff2e7799d..04d85e06b9 100644 --- a/docs/src/API/variables.md +++ b/docs/src/API/variables.md @@ -34,7 +34,7 @@ When variables with descriptions are present in systems, they will be printed wh ```@example metadata @variables u(t) [description = "A short description of u"] @parameters p [description = "A description of p"] -@named sys = ODESystem([u ~ p], t) +@named sys = System([u ~ p], t) show(stdout, "text/plain", sys) # hide ``` @@ -298,7 +298,7 @@ In the example below, we define a system with tunable parameters and extract bou @parameters k [tunable = true, bounds = (0, Inf)] eqs = [D(x) ~ (-x + k * u) / T # A first-order system with time constant T and gain k y ~ x] -sys = ODESystem(eqs, t, name = :tunable_first_order) +sys = System(eqs, t, name = :tunable_first_order) ``` ```@example metadata diff --git a/docs/src/basics/AbstractSystem.md b/docs/src/basics/AbstractSystem.md index 62f842b2b2..61b6ef4fff 100644 --- a/docs/src/basics/AbstractSystem.md +++ b/docs/src/basics/AbstractSystem.md @@ -12,7 +12,7 @@ model manipulation and compilation. There are three immediate subtypes of `AbstractSystem`, classified by how many independent variables each type has: - `AbstractTimeIndependentSystem`: has no independent variable (e.g.: `NonlinearSystem`) - - `AbstractTimeDependentSystem`: has a single independent variable (e.g.: `ODESystem`) + - `AbstractTimeDependentSystem`: has a single independent variable (e.g.: `System`) - `AbstractMultivariateSystem`: may have multiple independent variables (e.g.: `PDESystem`) ## Constructors and Naming diff --git a/docs/src/basics/Composition.md b/docs/src/basics/Composition.md index f6abe97d38..01a755e15c 100644 --- a/docs/src/basics/Composition.md +++ b/docs/src/basics/Composition.md @@ -21,7 +21,7 @@ using ModelingToolkit: t_nounits as t, D_nounits as D function decay(; name) @parameters a @variables x(t) f(t) - ODESystem([ + System([ D(x) ~ -a * x + f ], t; name = name) @@ -31,7 +31,7 @@ end @named decay2 = decay() connected = compose( - ODESystem([decay2.f ~ decay1.x + System([decay2.f ~ decay1.x D(decay1.f) ~ 0], t; name = :connected), decay1, decay2) equations(connected) @@ -69,7 +69,7 @@ subsystems. A model is the composition of itself and its subsystems. For example, if we have: ```julia -@named sys = compose(ODESystem(eqs, indepvar, unknowns, ps), subsys) +@named sys = compose(System(eqs, indepvar, unknowns, ps), subsys) ``` the `equations` of `sys` is the concatenation of `get_eqs(sys)` and @@ -122,7 +122,7 @@ With symbolic parameters, it is possible to set the default value of a parameter ```julia # ... -sys = ODESystem( +sys = System( # ... # directly in the defaults argument defaults = Pair{Num, Any}[x => u, @@ -144,20 +144,20 @@ d = GlobalScope(d) p = [a, b, c, d] -level0 = ODESystem(Equation[], t, [], p; name = :level0) -level1 = ODESystem(Equation[], t, [], []; name = :level1) ∘ level0 +level0 = System(Equation[], t, [], p; name = :level0) +level1 = System(Equation[], t, [], []; name = :level1) ∘ level0 parameters(level1) #level0₊a #b #c #d -level2 = ODESystem(Equation[], t, [], []; name = :level2) ∘ level1 +level2 = System(Equation[], t, [], []; name = :level2) ∘ level1 parameters(level2) #level1₊level0₊a #level1₊b #c #d -level3 = ODESystem(Equation[], t, [], []; name = :level3) ∘ level2 +level3 = System(Equation[], t, [], []; name = :level3) ∘ level2 parameters(level3) #level2₊level1₊level0₊a #level2₊level1₊b @@ -194,12 +194,12 @@ using ModelingToolkit: t_nounits as t, D_nounits as D N = S + I + R @parameters β, γ -@named seqn = ODESystem([D(S) ~ -β * S * I / N], t) -@named ieqn = ODESystem([D(I) ~ β * S * I / N - γ * I], t) -@named reqn = ODESystem([D(R) ~ γ * I], t) +@named seqn = System([D(S) ~ -β * S * I / N], t) +@named ieqn = System([D(I) ~ β * S * I / N - γ * I], t) +@named reqn = System([D(R) ~ γ * I], t) sir = compose( - ODESystem( + System( [ S ~ ieqn.S, I ~ seqn.I, @@ -266,6 +266,6 @@ equations are discontinuous in either the unknown or one of its derivatives. Thi causes the solver to take very small steps around the discontinuity, and sometimes leads to early stopping due to `dt <= dt_min`. The correct way to handle such dynamics is to tell the solver about the discontinuity by a -root-finding equation, which can be modeling using [`ODESystem`](@ref)'s event +root-finding equation, which can be modeling using [`System`](@ref)'s event support. Please see the tutorial on [Callbacks and Events](@ref events) for details and examples. diff --git a/docs/src/basics/Debugging.md b/docs/src/basics/Debugging.md index 5bc509acfd..4f9c2c07d7 100644 --- a/docs/src/basics/Debugging.md +++ b/docs/src/basics/Debugging.md @@ -12,7 +12,7 @@ using ModelingToolkit: t_nounits as t, D_nounits as D @variables u1(t) u2(t) u3(t) eqs = [D(u1) ~ -√(u1), D(u2) ~ -√(u2), D(u3) ~ -√(u3)] defaults = [u1 => 1.0, u2 => 2.0, u3 => 3.0] -@named sys = ODESystem(eqs, t; defaults) +@named sys = System(eqs, t; defaults) sys = mtkcompile(sys) ``` @@ -38,7 +38,7 @@ We could have figured that out ourselves, but it is not always so obvious for mo Suppose we also want to validate that `u1 + u2 >= 2.0`. We can do this via the assertions functionality. ```@example debug -@mtkcompile sys = ODESystem(eqs, t; defaults, assertions = [(u1 + u2 >= 2.0) => "Oh no!"]) +@mtkcompile sys = System(eqs, t; defaults, assertions = [(u1 + u2 >= 2.0) => "Oh no!"]) ``` The assertions must be an iterable of pairs, where the first element is the symbolic condition and diff --git a/docs/src/basics/Events.md b/docs/src/basics/Events.md index 5d5df0a377..89f874b08b 100644 --- a/docs/src/basics/Events.md +++ b/docs/src/basics/Events.md @@ -9,7 +9,7 @@ or into more specialized callback types from the [DiffEqCallbacks.jl](https://docs.sciml.ai/DiffEqDocs/stable/features/callback_library/) library. -[`ODESystem`](@ref)s and [`SDESystem`](@ref)s accept keyword arguments +[`System`](@ref)s and [`SDESystem`](@ref)s accept keyword arguments `continuous_events` and `discrete_events` to symbolically encode continuous or discrete callbacks. [`JumpSystem`](@ref)s currently support only `discrete_events`. Continuous events are applied when a given condition becomes @@ -59,7 +59,7 @@ For example, consider the following system. ```julia @variables x(t) y(t) @parameters p(t) -@mtkcompile sys = ODESystem([x * y ~ p, D(x) ~ 0], t) +@mtkcompile sys = System([x * y ~ p, D(x) ~ 0], t) event = [t == 1] => [x ~ Pre(x) + 1] ``` @@ -132,7 +132,7 @@ function UnitMassWithFriction(k; name) @variables x(t)=0 v(t)=0 eqs = [D(x) ~ v D(v) ~ sin(t) - k * sign(v)] - ODESystem(eqs, t; continuous_events = [v ~ 0], name) # when v = 0 there is a discontinuity + System(eqs, t; continuous_events = [v ~ 0], name) # when v = 0 there is a discontinuity end @mtkcompile m = UnitMassWithFriction(0.7) prob = ODEProblem(m, Pair[], (0, 10pi)) @@ -154,7 +154,7 @@ like this root_eqs = [x ~ 0] # the event happens at the ground x(t) = 0 affect = [v ~ -Pre(v)] # the effect is that the velocity changes sign -@mtkcompile ball = ODESystem( +@mtkcompile ball = System( [D(x) ~ v D(v) ~ -9.8], t; continuous_events = root_eqs => affect) # equation => affect @@ -175,7 +175,7 @@ Multiple events? No problem! This example models a bouncing ball in 2D that is e continuous_events = [[x ~ 0] => [vx ~ -Pre(vx)] [y ~ -1.5, y ~ 1.5] => [vy ~ -Pre(vy)]] -@mtkcompile ball = ODESystem( +@mtkcompile ball = System( [ D(x) ~ vx, D(y) ~ vy, @@ -255,7 +255,7 @@ end reflect = [x ~ 0] => (bb_affect!, [v], [], [], nothing) -@mtkcompile bb_sys = ODESystem(bb_eqs, t, sts, par, +@mtkcompile bb_sys = System(bb_eqs, t, sts, par, continuous_events = reflect) u0 = [v => 0.0, x => 1.0] @@ -300,7 +300,7 @@ injection = (t == tinject) => [N ~ Pre(N) + M] u0 = [N => 0.0] tspan = (0.0, 20.0) p = [α => 100.0, tinject => 10.0, M => 50] -@mtkcompile osys = ODESystem(eqs, t, [N], [α, M, tinject]; discrete_events = injection) +@mtkcompile osys = System(eqs, t, [N], [α, M, tinject]; discrete_events = injection) oprob = ODEProblem(osys, u0, tspan, p) sol = solve(oprob, Tsit5(); tstops = 10.0) plot(sol) @@ -319,7 +319,7 @@ to ```@example events injection = ((t == tinject) & (N < 50)) => [N ~ Pre(N) + M] -@mtkcompile osys = ODESystem(eqs, t, [N], [M, tinject, α]; discrete_events = injection) +@mtkcompile osys = System(eqs, t, [N], [M, tinject, α]; discrete_events = injection) oprob = ODEProblem(osys, u0, tspan, p) sol = solve(oprob, Tsit5(); tstops = 10.0) plot(sol) @@ -346,7 +346,7 @@ killing = ModelingToolkit.SymbolicDiscreteCallback( tspan = (0.0, 30.0) p = [α => 100.0, tinject => 10.0, M => 50, tkill => 20.0] -@mtkcompile osys = ODESystem(eqs, t, [N], [α, M, tinject, tkill]; +@mtkcompile osys = System(eqs, t, [N], [α, M, tinject, tkill]; discrete_events = [injection, killing]) oprob = ODEProblem(osys, u0, tspan, p) sol = solve(oprob, Tsit5(); tstops = [10.0, 20.0]) @@ -375,7 +375,7 @@ killing = ModelingToolkit.SymbolicDiscreteCallback( [20.0] => [α ~ 0.0], discrete_parameters = α, iv = t) p = [α => 100.0, M => 50] -@mtkcompile osys = ODESystem(eqs, t, [N], [α, M]; +@mtkcompile osys = System(eqs, t, [N], [α, M]; discrete_events = [injection, killing]) oprob = ODEProblem(osys, u0, tspan, p) sol = solve(oprob, Tsit5()) @@ -415,7 +415,7 @@ example: ev = ModelingToolkit.SymbolicDiscreteCallback( 1.0 => [c ~ Pre(c) + 1], discrete_parameters = c, iv = t) -@mtkcompile sys = ODESystem( +@mtkcompile sys = System( D(x) ~ c * cos(x), t, [x], [c]; discrete_events = [ev]) prob = ODEProblem(sys, [x => 0.0], (0.0, 2pi), [c => 1.0]) @@ -436,7 +436,7 @@ will be saved. If we repeat the above example with `c` not a `discrete_parameter @variables x(t) @parameters c(t) -@mtkcompile sys = ODESystem( +@mtkcompile sys = System( D(x) ~ c * cos(x), t, [x], [c]; discrete_events = [1.0 => [c ~ Pre(c) + 1]]) prob = ODEProblem(sys, [x => 0.0], (0.0, 2pi), [c => 1.0]) @@ -537,7 +537,7 @@ will write `furnace_on = false` back to the system, and when `temp = furnace_on_ to the system. ```@example events -@named sys = ODESystem( +@named sys = System( eqs, t, [temp], params; continuous_events = [furnace_disable, furnace_enable]) ss = mtkcompile(sys) prob = ODEProblem(ss, [temp => 0.0, furnace_on => true], (0.0, 10.0)) @@ -650,7 +650,7 @@ affect activation point, with -1 mapped to 0. We can now simulate the encoder. ```@example events -@named sys = ODESystem( +@named sys = System( eqs, t, [theta, omega], params; continuous_events = [qAevt, qBevt]) ss = mtkcompile(sys) prob = ODEProblem(ss, [theta => 0.0], (0.0, pi)) diff --git a/docs/src/basics/FAQ.md b/docs/src/basics/FAQ.md index e83b1f1336..e3b12b46ab 100644 --- a/docs/src/basics/FAQ.md +++ b/docs/src/basics/FAQ.md @@ -205,7 +205,7 @@ eqs = [x1 + x2 + 1 ~ 0 x1 + x2 + x3 + 2 ~ 0 x1 + D(x3) + x4 + 3 ~ 0 2 * D(D(x1)) + D(D(x2)) + D(D(x3)) + D(x4) + 4 ~ 0] -@named sys = ODESystem(eqs, t) +@named sys = System(eqs, t) sys = mtkcompile(sys) prob = ODEProblem(sys, [], (0, 1)) ``` @@ -237,7 +237,7 @@ using ModelingToolkit: t_nounits as t, D_nounits as D sts = @variables x1(t) = 0.0 eqs = [D(x1) ~ 1.1 * x1] -@mtkcompile sys = ODESystem(eqs, t) +@mtkcompile sys = System(eqs, t) prob = ODEProblem{false}(sys, [], (0, 1); u0_constructor = x -> SVector(x...)) ``` @@ -252,7 +252,7 @@ using ModelingToolkit @independent_variables x D = Differential(x) @variables y(x) -@named sys = ODESystem([D(y) ~ x], x) +@named sys = System([D(y) ~ x], x) ``` ## Ordering of tunable parameters @@ -279,7 +279,7 @@ using ModelingToolkit @parameters p q[1:3] r[1:2, 1:2] -@named sys = ODESystem(Equation[], ModelingToolkit.t_nounits, [], [p, q, r]) +@named sys = System(Equation[], ModelingToolkit.t_nounits, [], [p, q, r]) sys = complete(sys) ``` diff --git a/docs/src/basics/InputOutput.md b/docs/src/basics/InputOutput.md index 35a3885dbd..2e9da1c2db 100644 --- a/docs/src/basics/InputOutput.md +++ b/docs/src/basics/InputOutput.md @@ -44,7 +44,7 @@ import ModelingToolkit: t_nounits as t, D_nounits as D eqs = [D(x) ~ -k * (x + u) y ~ x] -@named sys = ODESystem(eqs, t) +@named sys = System(eqs, t) f, x_sym, ps = ModelingToolkit.generate_control_function(sys, [u], simplify = true); nothing # hide ``` diff --git a/docs/src/basics/Linearization.md b/docs/src/basics/Linearization.md index 27b8ec9903..3951aab28a 100644 --- a/docs/src/basics/Linearization.md +++ b/docs/src/basics/Linearization.md @@ -29,12 +29,12 @@ eqs = [u ~ kp * (r - y) # P controller D(x) ~ -x + u # First-order plant y ~ x] # Output equation -@named sys = ODESystem(eqs, t) # Do not call @mtkcompile when linearizing +@named sys = System(eqs, t) # Do not call @mtkcompile when linearizing matrices, simplified_sys = linearize(sys, [r], [y]) # Linearize from r to y matrices ``` -The named tuple `matrices` contains the matrices of the linear statespace representation, while `simplified_sys` is an `ODESystem` that, among other things, indicates the unknown variable order in the linear system through +The named tuple `matrices` contains the matrices of the linear statespace representation, while `simplified_sys` is an `System` that, among other things, indicates the unknown variable order in the linear system through ```@example LINEARIZE using ModelingToolkit: inputs, outputs @@ -78,7 +78,7 @@ eqs = [D(x) ~ v D(v) ~ -k * x - k3 * x^3 - c * v + 10u.u y.u ~ x] -@named duffing = ODESystem(eqs, t, systems = [y, u], defaults = [u.u => 0]) +@named duffing = System(eqs, t, systems = [y, u], defaults = [u.u => 0]) # pass a constant value for `x`, since it is the variable we will change in operating points linfun, simplified_sys = linearization_function(duffing, [u.u], [y.u]; op = Dict(x => NaN)); diff --git a/docs/src/basics/MTKLanguage.md b/docs/src/basics/MTKLanguage.md index 05847581f6..09fa6d2daa 100644 --- a/docs/src/basics/MTKLanguage.md +++ b/docs/src/basics/MTKLanguage.md @@ -16,7 +16,7 @@ equations. ### [Defining components with `@mtkmodel`](@id mtkmodel) `@mtkmodel` is a convenience macro to define components. It returns -`ModelingToolkit.Model`, which includes a system constructor (`ODESystem` by +`ModelingToolkit.Model`, which includes a system constructor (`System` by default), a `structure` dictionary with metadata, and flag `isconnector` which is set to `false`. @@ -263,7 +263,7 @@ end ### Setting the type of system: -By default `@mtkmodel` returns an ODESystem. Different types of system can be +By default `@mtkmodel` returns an System. Different types of system can be defined with the following syntax: ``` @@ -273,20 +273,6 @@ end ``` -Example: - -```@example mtkmodel-example -@mtkmodel Float2Bool::DiscreteSystem begin - @variables begin - u(t)::Float64 - y(t)::Bool - end - @equations begin - y ~ u != 0 - end -end -``` - ## Connectors Connectors are special models that can be used to connect different components together. @@ -301,7 +287,7 @@ MTK provides 3 distinct connectors: ### [Defining connectors with `@connector`](@id connector) `@connector` returns `ModelingToolkit.Model`. It includes a constructor that returns -a connector system (`ODESystem` by default), a `structure` dictionary with metadata, and flag `isconnector` +a connector system (`System` by default), a `structure` dictionary with metadata, and flag `isconnector` which is set to `true`. A simple connector can be defined with syntax similar to following example: diff --git a/docs/src/basics/Precompilation.md b/docs/src/basics/Precompilation.md index 0bf9a86653..3bac7fcc31 100644 --- a/docs/src/basics/Precompilation.md +++ b/docs/src/basics/Precompilation.md @@ -21,7 +21,7 @@ module PrecompilationMWE using ModelingToolkit @variables x(ModelingToolkit.t_nounits) -@named sys = ODESystem([ModelingToolkit.D_nounits(x) ~ -x + 1], ModelingToolkit.t_nounits) +@named sys = System([ModelingToolkit.D_nounits(x) ~ -x + 1], ModelingToolkit.t_nounits) prob = ODEProblem(mtkcompile(sys), [x => 30.0], (0, 100), [], eval_expression = true, eval_module = @__MODULE__) diff --git a/docs/src/basics/Validation.md b/docs/src/basics/Validation.md index 74715d351e..6e17beeded 100644 --- a/docs/src/basics/Validation.md +++ b/docs/src/basics/Validation.md @@ -110,7 +110,7 @@ end sts = @variables a(t)=0 [unit = u"cm"] ps = @parameters s=-1 [unit = u"cm"] c=c [unit = u"cm"] eqs = [D(a) ~ dummycomplex(c, s);] -sys = ODESystem( +sys = System( eqs, t, [sts...;], [ps...;], name = :sys, checks = ~ModelingToolkit.CheckUnits) sys_simple = mtkcompile(sys) ``` diff --git a/docs/src/examples/higher_order.md b/docs/src/examples/higher_order.md index 95480e283b..e8dda823e0 100644 --- a/docs/src/examples/higher_order.md +++ b/docs/src/examples/higher_order.md @@ -42,7 +42,7 @@ Note that we could've used an alternative syntax for 2nd order, i.e. and this syntax extends to `N`-th order. Also, we can use `*` or `∘` to compose `Differential`s, like `Differential(t) * Differential(x)`. -Now let's transform this into the `ODESystem` of first order components. +Now let's transform this into the `System` of first order components. We do this by calling `mtkcompile`: Now we can directly numerically solve the lowered system. Note that, diff --git a/docs/src/examples/perturbation.md b/docs/src/examples/perturbation.md index 20cef4067c..5dfe84c600 100644 --- a/docs/src/examples/perturbation.md +++ b/docs/src/examples/perturbation.md @@ -40,13 +40,13 @@ eqs_pert = taylor_coeff(eq_pert, ϵ, 0:2) The 0-th order equation can be solved analytically, but ModelingToolkit does currently not feature automatic analytical solution of ODEs, so we proceed with solving it numerically. -These are the ODEs we want to solve. Now construct an `ODESystem`, which automatically inserts dummy derivatives for the velocities: +These are the ODEs we want to solve. Now construct an `System`, which automatically inserts dummy derivatives for the velocities: ```@example perturbation @mtkcompile sys = System(eqs_pert, t) ``` -To solve the `ODESystem`, we generate an `ODEProblem` with initial conditions $x(0) = 0$, and $ẋ(0) = 1$, and solve it: +To solve the `System`, we generate an `ODEProblem` with initial conditions $x(0) = 0$, and $ẋ(0) = 1$, and solve it: ```@example perturbation using OrdinaryDiffEq @@ -82,7 +82,7 @@ eq = D(D(x)) + 2 * ϵ * D(x) + x ~ 0 with initial conditions $x(0) = 0$ and $ẋ(0) = 1$. With $ϵ = 0$, the problem reduces to the simple linear harmonic oscillator with the exact solution $x(t) = \sin(t)$. -We follow the same steps as in the previous example to construct the `ODESystem`: +We follow the same steps as in the previous example to construct the `System`: ```@example perturbation eq_pert = substitute(eq, x => x_series) diff --git a/docs/src/examples/tearing_parallelism.md b/docs/src/examples/tearing_parallelism.md index 4ecc8d2a45..f123f8b7b3 100644 --- a/docs/src/examples/tearing_parallelism.md +++ b/docs/src/examples/tearing_parallelism.md @@ -1,4 +1,4 @@ -# Exposing More Parallelism By Tearing Algebraic Equations in ODESystems +# Exposing More Parallelism By Tearing Algebraic Equations in Systems Sometimes it can be very non-trivial to parallelize a system. In this tutorial, we will demonstrate how to make use of `mtkcompile` to expose more diff --git a/docs/src/index.md b/docs/src/index.md index f742975802..6050af538e 100644 --- a/docs/src/index.md +++ b/docs/src/index.md @@ -135,7 +135,7 @@ Below is an incomplete list of extension libraries one may want to be aware of: - [MomentClosure.jl](https://augustinas1.github.io/MomentClosure.jl/dev/): Automatic transformation of ReactionSystems into deterministic systems - + Generates ODESystems for the moment closures + + Generates Systems for the moment closures + Allows for geometrically-distributed random reaction rates - [ReactionMechanismSimulator.jl](https://github.com/ReactionMechanismGenerator/ReactionMechanismSimulator.jl): Simulating and analyzing large chemical reaction mechanisms @@ -159,14 +159,14 @@ Below is an incomplete list of extension libraries one may want to be aware of: All of the symbolic systems have a direct conversion to a numerical system, which can then be handled through the SciML interfaces. For example, after building a -model and performing symbolic manipulations, an `ODESystem` can be converted into +model and performing symbolic manipulations, an `System` can be converted into an `ODEProblem` to then be solved by a numerical ODE solver. Below is a list of the solver libraries which are the numerical targets of the ModelingToolkit system: - [DifferentialEquations.jl](https://docs.sciml.ai/DiffEqDocs/stable/) - + Multi-package interface of high performance numerical solvers for `ODESystem`, + + Multi-package interface of high performance numerical solvers for `System`, `SDESystem`, and `JumpSystem` - [NonlinearSolve.jl](https://docs.sciml.ai/NonlinearSolve/stable/) diff --git a/docs/src/internals.md b/docs/src/internals.md index ed83192f21..0381e854b0 100644 --- a/docs/src/internals.md +++ b/docs/src/internals.md @@ -22,13 +22,13 @@ The procedure for variable elimination inside [`mtkcompile`](@ref) is 1. [`ModelingToolkit.initialize_system_structure`](@ref). 2. [`ModelingToolkit.alias_elimination`](@ref). This step moves equations into `observed(sys)`. - 3. [`ModelingToolkit.dae_index_lowering`](@ref) by means of [`pantelides!`](@ref) (if the system is an [`ODESystem`](@ref)). + 3. [`ModelingToolkit.dae_index_lowering`](@ref) by means of [`pantelides!`](@ref) (if the system is an [`System`](@ref)). 4. [`ModelingToolkit.tearing`](@ref). ## Preparing a system for simulation Before a simulation or optimization can be performed, the symbolic equations stored in an [`AbstractSystem`](@ref) must be converted into executable code. This step typically occurs after the simplification explained above, and is performed when an instance of a [`SciMLBase.AbstractSciMLProblem`](@ref), such as a [`ODEProblem`](@ref), is constructed. -The call chain typically looks like this, with the function names in the case of an `ODESystem` indicated in parentheses +The call chain typically looks like this, with the function names in the case of an `System` indicated in parentheses 1. Problem constructor ([`ODEProblem`](@ref)) 2. Build an `DEFunction` ([`process_DEProblem`](@ref) -> [`ODEFunction`](@ref) diff --git a/docs/src/tutorials/acausal_components.md b/docs/src/tutorials/acausal_components.md index b364be4012..46da36caa4 100644 --- a/docs/src/tutorials/acausal_components.md +++ b/docs/src/tutorials/acausal_components.md @@ -116,7 +116,7 @@ We wish to build the following RC circuit by building individual components and ### Building the Component Library -For each of our components, we use ModelingToolkit `Model` that emits an `ODESystem`. +For each of our components, we use ModelingToolkit `Model` that emits an `System`. At the top, we start with defining the fundamental qualities of an electric circuit component. At every input and output pin, a circuit component has two values: the current at the pin and the voltage. Thus we define the `Pin` @@ -133,7 +133,7 @@ default, variables are equal in a connection. end ``` -Note that this is an incompletely specified ODESystem: it cannot be simulated +Note that this is an incompletely specified System: it cannot be simulated on its own because the equations for `v(t)` and `i(t)` are unknown. Instead, this just gives a common syntax for receiving this pair with some default values. @@ -145,7 +145,7 @@ One can then construct a `Pin` using the `@named` helper macro: Next, we build our ground node. A ground node is just a pin that is connected to a constant voltage reservoir, typically taken to be `V = 0`. Thus to define -this component, we generate an `ODESystem` with a `Pin` subcomponent and specify +this component, we generate an `System` with a `Pin` subcomponent and specify that the voltage in such a `Pin` is equal to zero. This gives: ```@example acausal diff --git a/docs/src/tutorials/attractors.md b/docs/src/tutorials/attractors.md index 426551b017..8b5fecbef9 100644 --- a/docs/src/tutorials/attractors.md +++ b/docs/src/tutorials/attractors.md @@ -38,7 +38,7 @@ eqs = [ ] ``` -Because our dynamical system is super simple, we will directly make an `ODESystem` and cast it in an `ODEProblem` as in the [`ODESystems` tutorial](@ref programmatically). Since all state variables and parameters have a default value we can immediately write +Because our dynamical system is super simple, we will directly make an `System` and cast it in an `ODEProblem` as in the [`Systems` tutorial](@ref programmatically). Since all state variables and parameters have a default value we can immediately write ```@example Attractors @named modlorenz = System(eqs, t) diff --git a/docs/src/tutorials/bifurcation_diagram_computation.md b/docs/src/tutorials/bifurcation_diagram_computation.md index 811ea89e83..3ceb5474ab 100644 --- a/docs/src/tutorials/bifurcation_diagram_computation.md +++ b/docs/src/tutorials/bifurcation_diagram_computation.md @@ -1,6 +1,6 @@ # [Bifurcation Diagrams](@id bifurcation_diagrams) -Bifurcation diagrams describes how, for a dynamic system, the quantity and quality of its steady states changes with a parameter's value. These can be computed through the [BifurcationKit.jl](https://github.com/bifurcationkit/BifurcationKit.jl) package. ModelingToolkit provides a simple interface for creating BifurcationKit compatible `BifurcationProblem`s from `NonlinearSystem`s and `ODESystem`s. All the features provided by BifurcationKit can then be applied to these systems. This tutorial provides a brief introduction for these features, with BifurcationKit.jl providing [a more extensive documentation](https://bifurcationkit.github.io/BifurcationKitDocs.jl/stable/). +Bifurcation diagrams describes how, for a dynamic system, the quantity and quality of its steady states changes with a parameter's value. These can be computed through the [BifurcationKit.jl](https://github.com/bifurcationkit/BifurcationKit.jl) package. ModelingToolkit provides a simple interface for creating BifurcationKit compatible `BifurcationProblem`s from `NonlinearSystem`s and `System`s. All the features provided by BifurcationKit can then be applied to these systems. This tutorial provides a brief introduction for these features, with BifurcationKit.jl providing [a more extensive documentation](https://bifurcationkit.github.io/BifurcationKitDocs.jl/stable/). ### Creating a `BifurcationProblem` @@ -83,9 +83,9 @@ plot(bf; Here, the system exhibits a pitchfork bifurcation at *μ=0.0*. -### Using `ODESystem` inputs +### Using `System` inputs -It is also possible to use `ODESystem`s (rather than `NonlinearSystem`s) as input to `BifurcationProblem`. Here follows a brief such example. +It is also possible to use `System`s (rather than `NonlinearSystem`s) as input to `BifurcationProblem`. Here follows a brief such example. ```@example Bif2 using BifurcationKit, ModelingToolkit, Plots diff --git a/docs/src/tutorials/fmi.md b/docs/src/tutorials/fmi.md index d17452f612..7e949839ef 100644 --- a/docs/src/tutorials/fmi.md +++ b/docs/src/tutorials/fmi.md @@ -106,7 +106,7 @@ constant until the next time the callback triggers. The periodic interval must b more computationally expensive. This model alone does not have any differential variables, and calling `mtkcompile` will lead -to an `ODESystem` with no unknowns. +to an `System` with no unknowns. ```@example fmi mtkcompile(inner) diff --git a/docs/src/tutorials/initialization.md b/docs/src/tutorials/initialization.md index fedb8b9a22..90d86f1521 100644 --- a/docs/src/tutorials/initialization.md +++ b/docs/src/tutorials/initialization.md @@ -4,7 +4,7 @@ While for simple numerical ODEs choosing an initial condition can be an easy affair, with ModelingToolkit's more general differential-algebraic equation (DAE) system there is more care needed due to the flexibility of the solver state. In this tutorial we will walk through the functionality involved in -initialization of ODESystem and the diagnostics to better understand and +initialization of System and the diagnostics to better understand and debug the initialization problem. ## Primer on Initialization of Differential-Algebraic Equations diff --git a/docs/src/tutorials/modelingtoolkitize.md b/docs/src/tutorials/modelingtoolkitize.md index fc74578ea4..8cd8015630 100644 --- a/docs/src/tutorials/modelingtoolkitize.md +++ b/docs/src/tutorials/modelingtoolkitize.md @@ -49,7 +49,7 @@ prob = ODEProblem(rober, [1.0, 0.0, 0.0], (0.0, 1e5), (0.04, 3e7, 1e4)) ``` If we want to get a symbolic representation, we can simply call `modelingtoolkitize` -on the `prob`, which will return an `ODESystem`: +on the `prob`, which will return an `System`: ```@example mtkize @mtkcompile sys = modelingtoolkitize(prob) diff --git a/docs/src/tutorials/nonlinear.md b/docs/src/tutorials/nonlinear.md index 13caf96231..8342eb788b 100644 --- a/docs/src/tutorials/nonlinear.md +++ b/docs/src/tutorials/nonlinear.md @@ -12,7 +12,7 @@ This steady state is reached when the nonlinear system of differential equations is not yet compatible with `NonlinearSystem`. We thus have to use a lower level interface to define nonlinear systems. For an introduction to this interface, read the - [programmatically generating ODESystems tutorial](@ref programmatically). + [programmatically generating Systems tutorial](@ref programmatically). ```@example nonlinear using ModelingToolkit, NonlinearSolve diff --git a/docs/src/tutorials/ode_modeling.md b/docs/src/tutorials/ode_modeling.md index 3113ab0fc2..6da2524140 100644 --- a/docs/src/tutorials/ode_modeling.md +++ b/docs/src/tutorials/ode_modeling.md @@ -362,15 +362,15 @@ memory allocations. For large, hierarchically built models, which tend to be sparse, speedup and the reduction of memory allocation can also be expected to be substantial. In addition, these problem builders allow for automatic parallelism by exploiting the structural information. For more information, see the -[ODESystem](@ref ODESystem) page. +[System](@ref System) page. ## Notes and pointers how to go on Here are some notes that may be helpful during your initial steps with MTK: - The `@mtkmodel` macro is for high-level usage of MTK. However, in many cases you - may need to programmatically generate `ODESystem`s. If that's the case, check out - the [Programmatically Generating and Scripting ODESystems Tutorial](@ref programmatically). + may need to programmatically generate `System`s. If that's the case, check out + the [Programmatically Generating and Scripting Systems Tutorial](@ref programmatically). - Vector-valued parameters and variables are possible. A cleaner, more consistent treatment of these is still a work in progress, however. Once finished, this introductory tutorial will also cover this feature. diff --git a/docs/src/tutorials/optimization.md b/docs/src/tutorials/optimization.md index d299416412..89237e01e6 100644 --- a/docs/src/tutorials/optimization.md +++ b/docs/src/tutorials/optimization.md @@ -14,7 +14,7 @@ The package can also build optimization systems. is not yet compatible with `OptimizationSystem`. We thus have to use a lower level interface to define optimization systems. For an introduction to this interface, read the - [programmatically generating ODESystems tutorial](@ref programmatically). + [programmatically generating Systems tutorial](@ref programmatically). ## Unconstrained Rosenbrock Function diff --git a/docs/src/tutorials/programmatically_generating.md b/docs/src/tutorials/programmatically_generating.md index 406f65d8d9..93a9543818 100644 --- a/docs/src/tutorials/programmatically_generating.md +++ b/docs/src/tutorials/programmatically_generating.md @@ -1,10 +1,10 @@ -# [Programmatically Generating and Scripting ODESystems](@id programmatically) +# [Programmatically Generating and Scripting Systems](@id programmatically) -In the following tutorial, we will discuss how to programmatically generate `ODESystem`s. -This is useful for functions that generate `ODESystem`s, for example -when you implement a reader that parses some file format, such as SBML, to generate an `ODESystem`. -It is also useful for functions that transform an `ODESystem`, for example -when you write a function that log-transforms a variable in an `ODESystem`. +In the following tutorial, we will discuss how to programmatically generate `System`s. +This is useful for functions that generate `System`s, for example +when you implement a reader that parses some file format, such as SBML, to generate an `System`. +It is also useful for functions that transform an `System`, for example +when you write a function that log-transforms a variable in an `System`. ## The Representation of a ModelingToolkit System @@ -28,7 +28,7 @@ eqs = [D(x) ~ y However, ModelingToolkit has many higher-level features which will make scripting ModelingToolkit systems more convenient. For example, as shown in the next section, defining your own independent variables and differentials is rarely needed. -## The Non-DSL (non-`@mtkmodel`) Way of Defining an ODESystem +## The Non-DSL (non-`@mtkmodel`) Way of Defining an System Using `@mtkmodel`, like in the [getting started tutorial](@ref getting_started), is the preferred way of defining ODEs with MTK. @@ -63,7 +63,7 @@ and passing it to the `System` constructor. `@named` automatically gives a name to the `System`, and is shorthand for ```@example scripting -fol_model = System(eqs, t; name = :fol_model) # @named fol_model = ODESystem(eqs, t) +fol_model = System(eqs, t; name = :fol_model) # @named fol_model = System(eqs, t) ``` Thus, if we had read a name from a file and wish to populate an `System` with said name, we could do: diff --git a/docs/src/tutorials/stochastic_diffeq.md b/docs/src/tutorials/stochastic_diffeq.md index 9bc0086d35..72a77eda05 100644 --- a/docs/src/tutorials/stochastic_diffeq.md +++ b/docs/src/tutorials/stochastic_diffeq.md @@ -1,6 +1,6 @@ # Modeling with Stochasticity -All previous differential equations tutorials deal with deterministic `ODESystem`s. +All previous differential equations tutorials deal with deterministic `System`s. In this tutorial, we add randomness. In particular, we show how to represent a [stochastic differential equation](https://en.wikipedia.org/wiki/Stochastic_differential_equation) @@ -13,7 +13,7 @@ as a `SDESystem`. is not yet compatible with `SDESystem`. We thus have to use a lower level interface to define stochastic differential equations. For an introduction to this interface, read the - [programmatically generating ODESystems tutorial](@ref programmatically). + [programmatically generating Systems tutorial](@ref programmatically). Let's take the Lorenz equation and add noise to each of the states. To show the flexibility of ModelingToolkit, From f215ec083a439be63603b83bc42178db24ce685e Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 29 May 2025 18:17:55 +0530 Subject: [PATCH 1927/2176] fix: fix infinite recursion in `full_equations` --- .../symbolics_tearing.jl | 21 +++----------- src/utils.jl | 3 +- test/odesystem.jl | 29 +++++++++++++++++++ 3 files changed, 35 insertions(+), 18 deletions(-) diff --git a/src/structural_transformation/symbolics_tearing.jl b/src/structural_transformation/symbolics_tearing.jl index 9eccc309bd..f6c394ee1c 100644 --- a/src/structural_transformation/symbolics_tearing.jl +++ b/src/structural_transformation/symbolics_tearing.jl @@ -687,8 +687,8 @@ function update_simplified_system!( unknowns = [unknowns; extra_unknowns] @set! sys.unknowns = unknowns - obs, subeqs, deps = cse_and_array_hacks( - sys, obs, solved_eqs, unknowns, neweqs; cse = cse_hack, array = array_hack) + obs = cse_and_array_hacks( + sys, obs, unknowns, neweqs; cse = cse_hack, array = array_hack) @set! sys.eqs = neweqs @set! sys.observed = obs @@ -790,7 +790,7 @@ if all `p[i]` are present and the unscalarized form is used in any equation (obs not) we first count the number of times the scalarized form of each observed variable occurs in observed equations (and unknowns if it's split). """ -function cse_and_array_hacks(sys, obs, subeqs, unknowns, neweqs; cse = true, array = true) +function cse_and_array_hacks(sys, obs, unknowns, neweqs; cse = true, array = true) # HACK 1 # mapping of rhs to temporary CSE variable # `f(...) => tmpvar` in above example @@ -818,7 +818,6 @@ function cse_and_array_hacks(sys, obs, subeqs, unknowns, neweqs; cse = true, arr tempeq = tempvar ~ rhs_arr rhs_to_tempvar[rhs_arr] = tempvar push!(obs, tempeq) - push!(subeqs, tempeq) end # getindex_wrapper is used because `observed2graph` treats `x` and `x[i]` as different, @@ -827,10 +826,6 @@ function cse_and_array_hacks(sys, obs, subeqs, unknowns, neweqs; cse = true, arr neweq = lhs ~ getindex_wrapper( rhs_to_tempvar[rhs_arr], Tuple(arguments(rhs)[2:end])) obs[i] = neweq - subeqi = findfirst(isequal(eq), subeqs) - if subeqi !== nothing - subeqs[subeqi] = neweq - end end # end HACK 1 @@ -860,7 +855,6 @@ function cse_and_array_hacks(sys, obs, subeqs, unknowns, neweqs; cse = true, arr tempeq = tempvar ~ rhs_arr rhs_to_tempvar[rhs_arr] = tempvar push!(obs, tempeq) - push!(subeqs, tempeq) end # don't need getindex_wrapper, but do it anyway to know that this # hack took place @@ -900,15 +894,8 @@ function cse_and_array_hacks(sys, obs, subeqs, unknowns, neweqs; cse = true, arr push!(obs_arr_eqs, arrvar ~ rhs) end append!(obs, obs_arr_eqs) - append!(subeqs, obs_arr_eqs) - - # need to re-sort subeqs - subeqs = ModelingToolkit.topsort_equations(subeqs, [eq.lhs for eq in subeqs]) - - deps = Vector{Int}[i == 1 ? Int[] : collect(1:(i - 1)) - for i in 1:length(subeqs)] - return obs, subeqs, deps + return obs end function is_getindexed_array(rhs) diff --git a/src/utils.jl b/src/utils.jl index 8fcf8d7a25..dd6971dbe1 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -700,7 +700,8 @@ Get a dictionary mapping variables eliminated from the system during `mtkcompile expressions used to calculate them. """ function get_substitutions(sys) - Dict([eq.lhs => eq.rhs for eq in observed(sys)]) + obs, _ = unhack_observed(observed(sys), equations(sys)) + Dict([eq.lhs => eq.rhs for eq in obs]) end @noinline function throw_missingvars_in_sys(vars) diff --git a/test/odesystem.jl b/test/odesystem.jl index be301f34f1..58315f0cff 100644 --- a/test/odesystem.jl +++ b/test/odesystem.jl @@ -1560,3 +1560,32 @@ end @mtkcompile sys = SysC() @test length(unknowns(sys)) == 3 end + +@testset "`full_equations` doesn't recurse infinitely" begin + code = """ + using ModelingToolkit + using ModelingToolkit: t_nounits as t, D_nounits as D + @variables x(t)[1:3]=[0,0,1] + @variables u1(t)=0 u2(t)=0 + y₁, y₂, y₃ = x + k₁, k₂, k₃ = 1,1,1 + eqs = [ + D(y₁) ~ -k₁*y₁ + k₃*y₂*y₃ + u1 + D(y₂) ~ k₁*y₁ - k₃*y₂*y₃ - k₂*y₂^2 + u2 + y₁ + y₂ + y₃ ~ 1 + ] + + @named sys = System(eqs, t) + + inputs = [u1, u2] + outputs = [y₁, y₂, y₃] + ss = mtkcompile(sys; inputs) + full_equations(ss) + """ + + cmd = `$(Base.julia_cmd()) --project=$(@__DIR__) -e $code` + proc = run(cmd, stdin, stdout, stderr; wait = false) + sleep(120) + @test !process_running(proc) + kill(proc, Base.SIGKILL) +end From 6b0f03c215aea4e938078dd122f4340674176465 Mon Sep 17 00:00:00 2001 From: fchen121 Date: Thu, 29 May 2025 12:36:31 -0400 Subject: [PATCH 1928/2176] Created change of variable for SDE --- src/ModelingToolkit.jl | 2 +- src/systems/diffeqs/basic_transformations.jl | 114 +++++++++++++++++++ test/changeofvariables.jl | 96 ++++++++++++++++ 3 files changed, 211 insertions(+), 1 deletion(-) create mode 100644 test/changeofvariables.jl diff --git a/src/ModelingToolkit.jl b/src/ModelingToolkit.jl index 63468917d1..84547e5698 100644 --- a/src/ModelingToolkit.jl +++ b/src/ModelingToolkit.jl @@ -296,7 +296,7 @@ export isinput, isoutput, getbounds, hasbounds, getguess, hasguess, isdisturbanc hasunit, getunit, hasconnect, getconnect, hasmisc, getmisc, state_priority export liouville_transform, change_independent_variable, substitute_component, - add_accumulations, noise_to_brownians + add_accumulations, noise_to_brownians, changeofvariables, change_of_variable_SDE export PDESystem export Differential, expand_derivatives, @derivatives export Equation, ConstrainedEquation diff --git a/src/systems/diffeqs/basic_transformations.jl b/src/systems/diffeqs/basic_transformations.jl index 6243ca2405..b19b6911e5 100644 --- a/src/systems/diffeqs/basic_transformations.jl +++ b/src/systems/diffeqs/basic_transformations.jl @@ -53,6 +53,120 @@ function liouville_transform(sys::System; kwargs...) ) end +""" +$(TYPEDSIGNATURES) + +Generates the set of ODEs after change of variables. + + +Example: + +```julia +using ModelingToolkit, OrdinaryDiffEq, Test + +# Change of variables: z = log(x) +# (this implies that x = exp(z) is automatically non-negative) + +@parameters t α +@variables x(t) +D = Differential(t) +eqs = [D(x) ~ α*x] + +tspan = (0., 1.) +u0 = [x => 1.0] +p = [α => -0.5] + +@named sys = ODESystem(eqs; defaults=u0) +prob = ODEProblem(sys, [], tspan, p) +sol = solve(prob, Tsit5()) + +@variables z(t) +forward_subs = [log(x) => z] +backward_subs = [x => exp(z)] + +@named new_sys = changeofvariables(sys, forward_subs, backward_subs) +@test equations(new_sys)[1] == (D(z) ~ α) + +new_prob = ODEProblem(new_sys, [], tspan, p) +new_sol = solve(new_prob, Tsit5()) + +@test isapprox(new_sol[x][end], sol[x][end], atol=1e-4) +``` + +""" +function changeofvariables(sys::System, forward_subs, backward_subs; simplify=false, t0=missing) + t = independent_variable(sys) + + old_vars = first.(backward_subs) + new_vars = last.(forward_subs) + kept_vars = setdiff(states(sys), old_vars) + rhs = [eq.rhs for eq in equations(sys)] + + # use: dz/dt = ∂z/∂x dx/dt + ∂z/∂t + dzdt = Symbolics.derivative( first.(forward_subs), t ) + new_eqs = Equation[] + for (new_var, ex) in zip(new_vars, dzdt) + for ode_eq in equations(sys) + ex = substitute(ex, ode_eq.lhs => ode_eq.rhs) + end + ex = substitute(ex, Dict(forward_subs)) + ex = substitute(ex, Dict(backward_subs)) + if simplify + ex = Symbolics.simplify(ex, expand=true) + end + push!(new_eqs, Differential(t)(new_var) ~ ex) + end + + defs = get_defaults(sys) + new_defs = Dict() + for f_sub in forward_subs + #TODO call value(...)? + ex = substitute(first(f_sub), defs) + if !ismissing(t0) + ex = substitute(ex, t => t0) + end + new_defs[last(f_sub)] = ex + end + return ODESystem(new_eqs; + defaults=new_defs, + observed=vcat(observed(sys),first.(backward_subs) .~ last.(backward_subs)) + ) +end + +function change_of_variable_SDE(sys::System, forward_subs, backward_subs, iv; simplify=false, t0=missing) + t = independent_variable(sys) + + old_vars = first.(backward_subs) + new_vars = last.(forward_subs) + + # use: f = Y(t, X) + # use: dY = (∂f/∂t + μ∂f/∂x + (1/2)*σ^2*∂2f/∂x2)dt + σ∂f/∂xdW + old_eqs = equations(sys) + old_noise = get_noiseeqs(sys) + ∂f∂t = Symbolics.derivative( first.(forward_subs), t ) + ∂f∂x = Symbolics.derivative( first.(forward_subs), old_vars ) + ∂2f∂x2 = Symbolics.derivative( ∂f∂x, old_vars ) + new_eqs = Equation[] + + for (new_var, eq, noise, first, second, third) in zip(new_vars, old_eqs, old_noise, ∂f∂t, ∂f∂x, ∂2f∂x2) + ex = first + eq.rhs * second + 1/2 * noise^2 * third + for eqs in old_eqs + ex = substitute(ex, eqs.lhs => eqs.rhs) + end + ex = substitute(ex, Dict(forward_subs)) + ex = substitute(ex, Dict(backward_subs)) + if simplify + ex = Symbolics.simplify(ex, expand=true) + end + push!(new_eqs, Differential(t)(new_var) ~ ex) + end + new_noise = [noise * div for (noise, div) in zip(old_noise, ∂f∂x)] + + return SDESystem(new_eqs, new_noise; + observed=vcat(observed(sys),first.(backward_subs) .~ last.(backward_subs)) + ) +end + """ change_independent_variable( sys::System, iv, eqs = []; diff --git a/test/changeofvariables.jl b/test/changeofvariables.jl new file mode 100644 index 0000000000..f9a4568fa0 --- /dev/null +++ b/test/changeofvariables.jl @@ -0,0 +1,96 @@ +using ModelingToolkit, OrdinaryDiffEq +using Test, LinearAlgebra + + +# Change of variables: z = log(x) +# (this implies that x = exp(z) is automatically non-negative) + +@parameters t α +@variables x(t) +D = Differential(t) +eqs = [D(x) ~ α*x] + +tspan = (0., 1.) +u0 = [x => 1.0] +p = [α => -0.5] + +sys = ODESystem(eqs; defaults=u0) +prob = ODEProblem(sys, [], tspan, p) +sol = solve(prob, Tsit5()) + +@variables z(t) +forward_subs = [log(x) => z] +backward_subs = [x => exp(z)] +new_sys = changeofvariables(sys, forward_subs, backward_subs) +@test equations(new_sys)[1] == (D(z) ~ α) + +new_prob = ODEProblem(new_sys, [], tspan, p) +new_sol = solve(new_prob, Tsit5()) + +@test isapprox(new_sol[x][end], sol[x][end], atol=1e-4) + + + +# Riccati equation +@parameters t α +@variables x(t) +D = Differential(t) +eqs = [D(x) ~ t^2 + α - x^2] +sys = ODESystem(eqs, defaults=[x=>1.]) + +@variables z(t) +forward_subs = [t + α/(x+t) => z ] +backward_subs = [ x => α/(z-t) - t] + +new_sys = changeofvariables(sys, forward_subs, backward_subs; simplify=true, t0=0.) +# output should be equivalent to +# t^2 + α - z^2 + 2 (but this simplification is not found automatically) + +tspan = (0., 1.) +p = [α => 1.] +prob = ODEProblem(sys,[],tspan,p) +new_prob = ODEProblem(new_sys,[],tspan,p) + +sol = solve(prob, Tsit5()) +new_sol = solve(new_prob, Tsit5()) + +@test isapprox(sol[x][end], new_sol[x][end], rtol=1e-4) + + +# Linear transformation to diagonal system +@parameters t +@variables x[1:3](t) +D = Differential(t) +A = [0. -1. 0.; -0.5 0.5 0.; 0. 0. -1.] +eqs = D.(x) .~ A*x + +tspan = (0., 10.) +u0 = x .=> [1.0, 2.0, -1.0] + +sys = ODESystem(eqs; defaults=u0) +prob = ODEProblem(sys,[],tspan) +sol = solve(prob, Tsit5()) + +T = eigen(A).vectors + +@variables z[1:3](t) +forward_subs = T \ x .=> z +backward_subs = x .=> T*z + +new_sys = changeofvariables(sys, forward_subs, backward_subs; simplify=true) + +new_prob = ODEProblem(new_sys, [], tspan, p) +new_sol = solve(new_prob, Tsit5()) + +# test RHS +new_rhs = [eq.rhs for eq in equations(new_sys)] +new_A = Symbolics.value.(Symbolics.jacobian(new_rhs, z)) +@test isapprox(diagm(eigen(A).values), new_A, rtol = 1e-10) +@test isapprox( new_sol[x[1],end], sol[x[1],end], rtol=1e-4) + +# Change of variables for sde +@Browian B +@parameters μ σ +@variables x(t) y(t) + + From fe7dffbd7cabc9d5a54e934e9bd045261e8a9bce Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 30 May 2025 10:33:56 +0530 Subject: [PATCH 1929/2176] chore: remove outdated exports --- src/ModelingToolkit.jl | 3 +-- src/structural_transformation/StructuralTransformations.jl | 1 - 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/src/ModelingToolkit.jl b/src/ModelingToolkit.jl index 306a1f5bc7..9937417e0f 100644 --- a/src/ModelingToolkit.jl +++ b/src/ModelingToolkit.jl @@ -303,7 +303,7 @@ export Differential, expand_derivatives, @derivatives export Equation, ConstrainedEquation export Term, Sym export SymScope, LocalScope, ParentScope, GlobalScope -export independent_variable, equations, controls, observed, full_equations, jumps, cost, +export independent_variable, equations, observed, full_equations, jumps, cost, brownians export initialization_equations, guesses, defaults, parameter_dependencies, hierarchy export mtkcompile, expand_connections, linearize, linearization_function, @@ -316,7 +316,6 @@ export calculate_jacobian, generate_jacobian, generate_rhs, generate_custom_func export calculate_control_jacobian, generate_control_jacobian export calculate_tgrad, generate_tgrad export generate_cost, calculate_cost_gradient, generate_cost_gradient -export calculate_factorized_W export calculate_cost_hessian, generate_cost_hessian export calculate_massmatrix, generate_diffusion_function export stochastic_integral_transform diff --git a/src/structural_transformation/StructuralTransformations.jl b/src/structural_transformation/StructuralTransformations.jl index 346a800174..33269f88a7 100644 --- a/src/structural_transformation/StructuralTransformations.jl +++ b/src/structural_transformation/StructuralTransformations.jl @@ -57,7 +57,6 @@ using DocStringExtensions export tearing, partial_state_selection, dae_index_lowering, check_consistency export dummy_derivative -export build_observed_function, ODAEProblem export sorted_incidence_matrix, pantelides!, pantelides_reassemble, tearing_reassemble, find_solvables!, linear_subsys_adjmat! From 7ad96ee3165897eae5ca1e2ade58bed671648cfb Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 30 May 2025 10:33:59 +0530 Subject: [PATCH 1930/2176] refactor: format --- docs/src/basics/Composition.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/src/basics/Composition.md b/docs/src/basics/Composition.md index 01a755e15c..3b8b59937a 100644 --- a/docs/src/basics/Composition.md +++ b/docs/src/basics/Composition.md @@ -32,7 +32,7 @@ end connected = compose( System([decay2.f ~ decay1.x - D(decay1.f) ~ 0], t; name = :connected), decay1, decay2) + D(decay1.f) ~ 0], t; name = :connected), decay1, decay2) equations(connected) From 77f85ea38609bb7be60fb1e794c3a9dd4e64ecf9 Mon Sep 17 00:00:00 2001 From: Christopher Rackauckas Date: Fri, 30 May 2025 07:52:42 +0000 Subject: [PATCH 1931/2176] Update Project.toml --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 1f51599a51..903debe297 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "ModelingToolkit" uuid = "961ee093-0014-501f-94e3-6117800e7a78" authors = ["Yingbo Ma ", "Chris Rackauckas and contributors"] -version = "10.0.0" +version = "10.0.1" [deps] ADTypes = "47edcb42-4c32-4615-8424-f2b9edc5f35b" From 3c7ef9de67540608cd352df1ebe7912a261c01bb Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 30 May 2025 14:58:07 +0530 Subject: [PATCH 1932/2176] fix: fix namespacing of `AffectSystem` --- src/systems/callbacks.jl | 48 ++++++++++++++++++++-------------------- test/symbolic_events.jl | 16 ++++++++++++++ 2 files changed, 40 insertions(+), 24 deletions(-) diff --git a/src/systems/callbacks.jl b/src/systems/callbacks.jl index db6e6b9648..a9069e575c 100644 --- a/src/systems/callbacks.jl +++ b/src/systems/callbacks.jl @@ -13,15 +13,12 @@ struct AffectSystem parameters::Vector """Parameters of the parent ODESystem whose values are modified by the affect.""" discretes::Vector - """Maps the symbols of unknowns/observed in the ImplicitDiscreteSystem to its corresponding unknown/parameter in the parent system.""" - aff_to_sys::Dict end system(a::AffectSystem) = a.system discretes(a::AffectSystem) = a.discretes unknowns(a::AffectSystem) = a.unknowns parameters(a::AffectSystem) = a.parameters -aff_to_sys(a::AffectSystem) = a.aff_to_sys all_equations(a::AffectSystem) = vcat(equations(system(a)), observed(system(a))) function Base.show(iio::IO, aff::AffectSystem) @@ -34,16 +31,14 @@ function Base.:(==)(a1::AffectSystem, a2::AffectSystem) isequal(system(a1), system(a2)) && isequal(discretes(a1), discretes(a2)) && isequal(unknowns(a1), unknowns(a2)) && - isequal(parameters(a1), parameters(a2)) && - isequal(aff_to_sys(a1), aff_to_sys(a2)) + isequal(parameters(a1), parameters(a2)) end function Base.hash(a::AffectSystem, s::UInt) s = hash(system(a), s) s = hash(unknowns(a), s) s = hash(parameters(a), s) - s = hash(discretes(a), s) - hash(aff_to_sys(a), s) + hash(discretes(a), s) end function vars!(vars, aff::AffectSystem; op = Differential) @@ -251,14 +246,12 @@ function make_affect(affect::Vector{Equation}; discrete_parameters = Any[], for eq in alg_eqs collect_vars!(dvs, params, eq, iv) end - pre_params = filter(haspre ∘ value, params) sys_params = collect(setdiff(params, union(discrete_parameters, pre_params))) discretes = map(tovar, discrete_parameters) dvs = collect(dvs) _dvs = map(default_toterm, dvs) - aff_map = Dict(zip(discretes, discrete_parameters)) rev_map = Dict(zip(discrete_parameters, discretes)) subs = merge(rev_map, Dict(zip(dvs, _dvs))) affect = Symbolics.fast_substitute(affect, subs) @@ -269,17 +262,14 @@ function make_affect(affect::Vector{Equation}; discrete_parameters = Any[], collect(union(pre_params, sys_params))) affectsys = mtkcompile(affectsys; fully_determined = nothing) # get accessed parameters p from Pre(p) in the callback parameters - accessed_params = filter(isparameter, map(unPre, collect(pre_params))) + accessed_params = Vector{Any}(filter(isparameter, map(unPre, collect(pre_params)))) union!(accessed_params, sys_params) # add scalarized unknowns to the map. _dvs = reduce(vcat, map(scalarize, _dvs), init = Any[]) - for u in _dvs - aff_map[u] = u - end AffectSystem(affectsys, collect(_dvs), collect(accessed_params), - collect(discrete_parameters), aff_map) + collect(discrete_parameters)) end function make_affect(affect; kwargs...) @@ -448,11 +438,23 @@ end ########## Namespacing Utilities ########### ############################################ function namespace_affects(affect::AffectSystem, s) - AffectSystem(renamespace(s, system(affect)), + affsys = system(affect) + old_ts = get_tearing_state(affsys) + # if we just `renamespace` the system, it updates the name. However, this doesn't + # namespace the returned values from `equations(affsys)`, etc. which we need. So we + # need to manually namespace everything. This is done by renaming the system to the + # namespace, putting it as a subsystem of an empty system called `affectsys`, and then + # flatten the system. The resultant system has everything namespaced, and is still + # called `affectsys` for further namespacing + affsys = rename(affsys, nameof(s)) + affsys = toggle_namespacing(affsys, true) + affsys = System(Equation[], get_iv(affsys); systems = [affsys], name = :affectsys) + affsys = complete(affsys) + @set! affsys.tearing_state = old_ts + AffectSystem(affsys, renamespace.((s,), unknowns(affect)), renamespace.((s,), parameters(affect)), - renamespace.((s,), discretes(affect)), - Dict([k => renamespace(s, v) for (k, v) in aff_to_sys(affect)])) + renamespace.((s,), discretes(affect))) end namespace_affects(af::Nothing, s) = nothing @@ -808,15 +810,13 @@ function compile_equational_affect( affsys = system(aff) ps_to_update = discretes(aff) dvs_to_update = setdiff(unknowns(aff), getfield.(observed(sys), :lhs)) - aff_map = aff_to_sys(aff) - sys_map = Dict([v => k for (k, v) in aff_map]) obseqs, eqs = unhack_observed(observed(affsys), equations(affsys)) if isempty(equations(affsys)) update_eqs = Symbolics.fast_substitute( obseqs, Dict([p => unPre(p) for p in parameters(affsys)])) rhss = map(x -> x.rhs, update_eqs) - lhss = map(x -> aff_map[x.lhs], update_eqs) + lhss = map(x -> x.lhs, update_eqs) is_p = [lhs ∈ Set(ps_to_update) for lhs in lhss] is_u = [lhs ∈ Set(dvs_to_update) for lhs in lhss] dvs = unknowns(sys) @@ -854,11 +854,11 @@ function compile_equational_affect( end end else - return let dvs_to_update = dvs_to_update, aff_map = aff_map, sys_map = sys_map, + return let dvs_to_update = dvs_to_update, affsys = affsys, ps_to_update = ps_to_update, aff = aff, sys = sys, reset_jumps = reset_jumps - dvs_to_access = [aff_map[u] for u in unknowns(affsys)] + dvs_to_access = unknowns(affsys) ps_to_access = [unPre(p) for p in parameters(affsys)] affu_getter = getsym(sys, dvs_to_access) @@ -867,8 +867,8 @@ function compile_equational_affect( affp_setter! = setsym(affsys, parameters(affsys)) u_setter! = setsym(sys, dvs_to_update) p_setter! = setsym(sys, ps_to_update) - u_getter = getsym(affsys, [sys_map[u] for u in dvs_to_update]) - p_getter = getsym(affsys, [sys_map[p] for p in ps_to_update]) + u_getter = getsym(affsys, dvs_to_update) + p_getter = getsym(affsys, ps_to_update) affprob = ImplicitDiscreteProblem( affsys, Pair[unknowns(affsys) .=> 0; parameters(affsys) .=> 0], diff --git a/test/symbolic_events.jl b/test/symbolic_events.jl index e61ea9779c..2c6253e70d 100644 --- a/test/symbolic_events.jl +++ b/test/symbolic_events.jl @@ -1331,3 +1331,19 @@ end sys = mtkcompile(sys) sol = solve(ODEProblem(sys, [], (0.0, 1.0)), Tsit5()) end + +@testset "non-floating-point discretes and namespaced affects" begin + function Inner(; name) + @parameters p(t)::Int + @variables x(t) + cevs = ModelingToolkit.SymbolicContinuousCallback( + [x ~ 1.0], [p ~ Pre(p) + 1]; iv = t, discrete_parameters = [p]) + System([D(x) ~ 1], t, [x], [p]; continuous_events = [cevs], name) + end + @named inner = Inner() + @mtkcompile sys = System(Equation[], t; systems = [inner]) + prob = ODEProblem(sys, [inner.x => 0.0, inner.p => 0], (0.0, 5.0)) + sol = solve(prob, Tsit5()) + @test SciMLBase.successful_retcode(sol) + @test sol[inner.p][end] ≈ 1.0 +end From 9834baafc594071bb81f44640d43327a62767ec0 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 2 Jun 2025 16:50:18 +0530 Subject: [PATCH 1933/2176] fix: fix BVProblem construction --- src/problems/bvproblem.jl | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/problems/bvproblem.jl b/src/problems/bvproblem.jl index 7040b32ffd..6344dfaa0b 100644 --- a/src/problems/bvproblem.jl +++ b/src/problems/bvproblem.jl @@ -8,6 +8,8 @@ check_compatibility && check_compatible_system(BVProblem, sys) isnothing(callback) || error("BVP solvers do not support callbacks.") + dvs = unknowns(sys) + op = to_varmap(op, dvs) # Systems without algebraic equations should use both fixed values + guesses # for initialization. _op = has_alg_eqs(sys) ? op : merge(Dict(op), Dict(guesses)) @@ -17,7 +19,6 @@ t = tspan !== nothing ? tspan[1] : tspan, check_compatibility = false, cse, checkbounds, time_dependent_init = false, expression, kwargs...) - dvs = unknowns(sys) stidxmap = Dict([v => i for (i, v) in enumerate(dvs)]) u0_idxs = has_alg_eqs(sys) ? collect(1:length(dvs)) : [stidxmap[k] for (k, v) in op if haskey(stidxmap, k)] From 36295d0334c13cb7b4fb7e3c44c141c783a76340 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 2 Jun 2025 16:50:36 +0530 Subject: [PATCH 1934/2176] fix: handle empty equations in `generate_initializesystem` --- src/systems/nonlinear/initializesystem.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/systems/nonlinear/initializesystem.jl b/src/systems/nonlinear/initializesystem.jl index 7769f20837..beb2719c2c 100644 --- a/src/systems/nonlinear/initializesystem.jl +++ b/src/systems/nonlinear/initializesystem.jl @@ -167,7 +167,7 @@ function generate_initializesystem_timevarying(sys::AbstractSystem; for k in keys(defs) defs[k] = substitute(defs[k], paramsubs) end - isys = System(eqs_ics, + isys = System(Vector{Equation}(eqs_ics), vars, pars; defaults = defs, @@ -280,7 +280,7 @@ function generate_initializesystem_timeindependent(sys::AbstractSystem; for k in keys(defs) defs[k] = substitute(defs[k], paramsubs) end - isys = System(eqs_ics, + isys = System(Vector{Equation}(eqs_ics), vars, pars; defaults = defs, From 3703a1ec57dcdb6efe2f03e3803f6c95f80f1934 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 2 Jun 2025 16:50:46 +0530 Subject: [PATCH 1935/2176] fix: fix parsing of costs in time-independent systems --- src/systems/system.jl | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/systems/system.jl b/src/systems/system.jl index 1b0fa940f6..5c01557cb5 100644 --- a/src/systems/system.jl +++ b/src/systems/system.jl @@ -543,12 +543,11 @@ function System(eqs::Vector{Equation}; kwargs...) for ssys in get(kwargs, :systems, System[]) collect_scoped_vars!(allunknowns, ps, ssys, nothing) end - costs = get(kwargs, :costs, nothing) - if costs !== nothing - costunknowns, costps = process_costs(costs, allunknowns, ps, nothing) - union!(allunknowns, costunknowns) - union!(ps, costps) + costs = get(kwargs, :costs, []) + for val in costs + collect_vars!(allunknowns, ps, val, nothing) end + cstrs = Vector{Union{Equation, Inequality}}(get(kwargs, :constraints, [])) for eq in cstrs collect_vars!(allunknowns, ps, eq, nothing) From 32cc70d38452cb2974567661dc26d0adb93368d1 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 2 Jun 2025 17:21:09 +0530 Subject: [PATCH 1936/2176] refactor: remove old `==` and `hash` implementation for `System` --- src/systems/system.jl | 91 ------------------------------------------- 1 file changed, 91 deletions(-) diff --git a/src/systems/system.jl b/src/systems/system.jl index 1b0fa940f6..5429664555 100644 --- a/src/systems/system.jl +++ b/src/systems/system.jl @@ -785,97 +785,6 @@ function ignored_connections_equal(sys1::System, sys2::System) return _eq_unordered(ic1[1], ic2[1]) && _eq_unordered(ic1[2], ic2[2]) end -function Base.:(==)(sys1::System, sys2::System) - sys1 === sys2 && return true - iv1 = get_iv(sys1) - iv2 = get_iv(sys2) - isequal(iv1, iv2) && - isequal(nameof(sys1), nameof(sys2)) && - _eq_unordered(get_eqs(sys1), get_eqs(sys2)) && - noise_equations_equal(sys1, sys2) && - _eq_unordered(get_jumps(sys1), get_jumps(sys2)) && - _eq_unordered(get_constraints(sys1), get_constraints(sys2)) && - _eq_unordered(get_costs(sys1), get_costs(sys2)) && - isequal(get_consolidate(sys1), get_consolidate(sys2)) && - _eq_unordered(get_unknowns(sys1), get_unknowns(sys2)) && - _eq_unordered(get_ps(sys1), get_ps(sys2)) && - _eq_unordered(get_brownians(sys1), get_brownians(sys2)) && - _eq_unordered(get_observed(sys1), get_observed(sys2)) && - _eq_unordered(get_parameter_dependencies(sys1), get_parameter_dependencies(sys2)) && - isequal(get_description(sys1), get_description(sys2)) && - isequal(get_defaults(sys1), get_defaults(sys2)) && - isequal(get_guesses(sys1), get_guesses(sys2)) && - _eq_unordered(get_initialization_eqs(sys1), get_initialization_eqs(sys2)) && - _eq_unordered(get_continuous_events(sys1), get_continuous_events(sys2)) && - _eq_unordered(get_discrete_events(sys1), get_discrete_events(sys2)) && - isequal(get_connector_type(sys1), get_connector_type(sys2)) && - isequal(get_assertions(sys1), get_assertions(sys2)) && - isequal(get_metadata(sys1), get_metadata(sys2)) && - isequal(get_gui_metadata(sys1), get_gui_metadata(sys2)) && - get_is_dde(sys1) == get_is_dde(sys2) && - _eq_unordered(get_tstops(sys1), get_tstops(sys2)) && - # not comparing tearing states because checking if they're equal up to ordering - # is difficult - getfield(sys1, :namespacing) == getfield(sys2, :namespacing) && - getfield(sys1, :complete) == getfield(sys2, :complete) && - ignored_connections_equal(sys1, sys2) && - get_parent(sys1) == get_parent(sys2) && - get_isscheduled(sys1) == get_isscheduled(sys2) && - all(s1 == s2 for (s1, s2) in zip(get_systems(sys1), get_systems(sys2))) -end - -function Base.hash(sys::System, h::UInt) - h = hash(nameof(sys), h) - h = hash(get_iv(sys), h) - # be considerate of things compared using `_eq_unordered` in `==` - eqs = get_eqs(sys) - eq_sortperm = sortperm(eqs; by = string) - h = hash(@view(eqs[eq_sortperm]), h) - neqs = get_noise_eqs(sys) - if neqs === nothing - h = hash(nothing, h) - elseif neqs isa Vector - h = hash(@view(neqs[eq_sortperm]), h) - else - h = hash(@view(neqs[eq_sortperm, :]), h) - end - h = hash(Set(get_jumps(sys)), h) - h = hash(Set(get_constraints(sys)), h) - h = hash(Set(get_costs(sys)), h) - h = hash(get_consolidate(sys), h) - h = hash(Set(get_unknowns(sys)), h) - h = hash(Set(get_ps(sys)), h) - h = hash(Set(get_brownians(sys)), h) - h = hash(Set(get_observed(sys)), h) - h = hash(Set(get_parameter_dependencies(sys)), h) - h = hash(get_description(sys), h) - h = hash(get_defaults(sys), h) - h = hash(get_guesses(sys), h) - h = hash(Set(get_initialization_eqs(sys)), h) - h = hash(Set(get_continuous_events(sys)), h) - h = hash(Set(get_discrete_events(sys)), h) - h = hash(get_connector_type(sys), h) - h = hash(get_assertions(sys), h) - h = hash(get_metadata(sys), h) - h = hash(get_gui_metadata(sys), h) - h = hash(get_is_dde(sys), h) - h = hash(Set(get_tstops(sys)), h) - h = hash(Set(getfield(sys, :namespacing)), h) - h = hash(Set(getfield(sys, :complete)), h) - ics = get_ignored_connections(sys) - if ics === nothing - h = hash(ics, h) - else - h = hash(Set(ics[1]), hash(Set(ics[2]), h), h) - end - h = hash(get_parent(sys), h) - h = hash(get_isscheduled(sys), h) - for s in get_systems(sys) - h = hash(s, h) - end - return h -end - """ $(TYPEDSIGNATURES) From 90c7c66264cd45220e0f077d41b22c5d7e7d0a52 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 2 Jun 2025 20:40:32 +0530 Subject: [PATCH 1937/2176] test: do not rely on system equality in tests --- test/accessor_functions.jl | 10 ++-------- test/analysis_points.jl | 2 +- test/nonlinearsystem.jl | 5 ++++- test/odesystem.jl | 37 +++++++------------------------------ test/sdesystem.jl | 30 ++---------------------------- test/serialization.jl | 5 ++++- test/symbolic_events.jl | 14 ++++++++------ 7 files changed, 28 insertions(+), 75 deletions(-) diff --git a/test/accessor_functions.jl b/test/accessor_functions.jl index ba6aae27a0..c54fb4c4ca 100644 --- a/test/accessor_functions.jl +++ b/test/accessor_functions.jl @@ -151,17 +151,11 @@ let # Checks `continuous_events_toplevel` and `discrete_events_toplevel` (straightforward # as I stored the same single event in all systems). Don't check for non-toplevel cases as # technically not needed for these tests and name spacing the events is a mess. - bot_cev = ModelingToolkit.SymbolicContinuousCallback( - cevs[1], alg_eqs = [O ~ (d + p_bot) * X_bot + Y]) - mid_dev = ModelingToolkit.SymbolicDiscreteCallback( - devs[1], alg_eqs = [O ~ (d + p_mid1) * X_mid1 + Y]) @test all_sets_equal( - continuous_events_toplevel.([sys_bot, sys_bot_comp, sys_bot_ss])..., - [bot_cev]) + continuous_events_toplevel.([sys_bot, sys_bot_comp, sys_bot_ss])...) @test all_sets_equal( discrete_events_toplevel.( - [sys_mid1, sys_mid1_comp, sys_mid1_ss])..., - [mid_dev]) + [sys_mid1, sys_mid1_comp, sys_mid1_ss])...) @test all(sym_issubset( continuous_events_toplevel(sys), get_continuous_events(sys)) for sys in [sys_bot, sys_mid2, sys_mid1, sys_top]) diff --git a/test/analysis_points.jl b/test/analysis_points.jl index bb233db5e8..903a74cc84 100644 --- a/test/analysis_points.jl +++ b/test/analysis_points.jl @@ -22,7 +22,7 @@ using Symbolics: NAMESPACE_SEPARATOR sys_normal = System(eqs, t, systems = [P, C], name = :hej) sys_normal2 = @test_nowarn expand_connections(sys_normal) - @test isequal(sys_ap2, sys_normal2) + @test issetequal(equations(sys_ap2), equations(sys_normal2)) end @testset "Inverse causality throws a warning" begin diff --git a/test/nonlinearsystem.jl b/test/nonlinearsystem.jl index 704d2de5de..4bacd6a50d 100644 --- a/test/nonlinearsystem.jl +++ b/test/nonlinearsystem.jl @@ -26,7 +26,10 @@ eqs = [0 ~ σ * (y - x) * h, 0 ~ x * (ρ - z) - y, 0 ~ x * y - β * z] @named ns = System(eqs, [x, y, z], [σ, ρ, β, h], defaults = Dict(x => 2)) -@test eval(toexpr(ns)) == ns +ns2 = eval(toexpr(ns)) +@test issetequal(equations(ns), equations(ns2)) +@test issetequal(unknowns(ns), unknowns(ns2)) +@test issetequal(parameters(ns), parameters(ns2)) test_nlsys_inference("standard", ns, (x, y, z), (σ, ρ, β, h)) @test begin f = generate_rhs(ns, expression = Val{false})[2] diff --git a/test/odesystem.jl b/test/odesystem.jl index 58315f0cff..073dbde5b2 100644 --- a/test/odesystem.jl +++ b/test/odesystem.jl @@ -35,8 +35,10 @@ ssort(eqs) = sort(eqs, by = string) @named des[1:3] = System(eqs, t) @test length(unique(x -> ModelingToolkit.get_tag(x), des)) == 1 -@test eval(toexpr(de)) == de -@test hash(deepcopy(de)) == hash(de) +de2 = eval(toexpr(de)) +@test issetequal(equations(de2), eqs) +@test issetequal(unknowns(de2), unknowns(de)) +@test issetequal(parameters(de2), parameters(de)) function test_diffeq_inference(name, sys, iv, dvs, ps) @testset "System construction: $name" begin @@ -710,7 +712,9 @@ let s1′ = sys1(; name = :s1) @named s2 = sys2() @unpack s1 = s2 - @test isequal(s1, s1′) + @test isequal(unknowns(s1), unknowns(s1′)) + @test isequal(parameters(s1), parameters(s1′)) + @test isequal(equations(s1), equations(s1′)) defs = Dict(s1.dx => 0.0, D(s1.x) => s1.x, s1.x => 0.0) @test isequal(ModelingToolkit.defaults(s2), defs) @@ -1427,33 +1431,6 @@ end @test_nowarn @named osys = System(eqs, t) end -# Test `isequal` -@testset "`isequal`" begin - @variables X(t) - @parameters p d(t) - eq = D(X) ~ p - d * X - - osys1 = complete(System([eq], t; name = :osys)) - osys2 = complete(System([eq], t; name = :osys)) - @test osys1 == osys2 # true - - continuous_events = [[X ~ 1.0] => [X ~ Pre(X) + 5.0]] - discrete_events = [SymbolicDiscreteCallback( - 5.0 => [d ~ d / 2.0], discrete_parameters = [d])] - - osys1 = complete(System([eq], t; name = :osys, continuous_events)) - osys2 = complete(System([eq], t; name = :osys)) - @test osys1 !== osys2 - - osys1 = complete(System([eq], t; name = :osys, discrete_events)) - osys2 = complete(System([eq], t; name = :osys)) - @test osys1 !== osys2 - - osys1 = complete(System([eq], t; name = :osys, continuous_events)) - osys2 = complete(System([eq], t; name = :osys, discrete_events)) - @test osys1 !== osys2 -end - @testset "Constraint system construction" begin @variables x(..) y(..) z(..) @parameters a b c d e diff --git a/test/sdesystem.jl b/test/sdesystem.jl index 74c05d7e43..20f62c402c 100644 --- a/test/sdesystem.jl +++ b/test/sdesystem.jl @@ -612,9 +612,8 @@ diffusion_eqs = [s*x 0 sys2 = SDESystem(drift_eqs, diffusion_eqs, tt, sts, ps, name = :sys1) sys2 = complete(sys2) -@set! sys1.parent = nothing -@set! sys2.parent = nothing -@test sys1 == sys2 + +@test issetequal(ModelingToolkit.get_noise_eqs(sys1), ModelingToolkit.get_noise_eqs(sys2)) prob = SDEProblem(sys1, [sts .=> [1.0, 0.0, 0.0]; ps .=> [10.0, 26.0]], (0.0, 100.0)) @@ -927,31 +926,6 @@ end end end -@testset "SDESystem Equality with events" begin - @variables X(t) - @parameters p d - @brownian a - seq = D(X) ~ p - d * X + a - @mtkcompile ssys1 = System([seq], t; name = :ssys) - @mtkcompile ssys2 = System([seq], t; name = :ssys) - @test ssys1 == ssys2 # true - - continuous_events = [[X ~ 1.0] => [X ~ Pre(X) + 5.0]] - discrete_events = [5.0 => [d ~ Pre(d) / 2.0]] - - @mtkcompile ssys1 = System([seq], t; name = :ssys, continuous_events) - @mtkcompile ssys2 = System([seq], t; name = :ssys) - @test ssys1 !== ssys2 - - @mtkcompile ssys1 = System([seq], t; name = :ssys, discrete_events) - @mtkcompile ssys2 = System([seq], t; name = :ssys) - @test ssys1 !== ssys2 - - @mtkcompile ssys1 = System([seq], t; name = :ssys, continuous_events) - @mtkcompile ssys2 = System([seq], t; name = :ssys, discrete_events) - @test ssys1 !== ssys2 -end - @testset "Error when constructing SDEProblem without `mtkcompile`" begin @parameters σ ρ β @variables x(tt) y(tt) z(tt) diff --git a/test/serialization.jl b/test/serialization.jl index 41ea9c2c15..2c754694b4 100644 --- a/test/serialization.jl +++ b/test/serialization.jl @@ -27,7 +27,10 @@ write(io, expand_connections(rc_model)) str = String(take!(io)) sys = include_string(@__MODULE__, str) -@test sys == expand_connections(rc_model) # this actually kind of works, but the variables would have different identities. +rc2 = expand_connections(rc_model) +@test issetequal(equations(sys), equations(rc2)) +@test issetequal(unknowns(sys), unknowns(rc2)) +@test issetequal(parameters(sys), parameters(rc2)) # check answer ss = mtkcompile(rc_model) diff --git a/test/symbolic_events.jl b/test/symbolic_events.jl index 2c6253e70d..b69346ca4d 100644 --- a/test/symbolic_events.jl +++ b/test/symbolic_events.jl @@ -310,8 +310,9 @@ end [D(x) ~ v D(v) ~ -9.8], t, continuous_events = root_eqs => affect) - @test only(continuous_events(ball)) == - SymbolicContinuousCallback(Equation[x ~ 0], Equation[v ~ -Pre(v)]) + cev = only(continuous_events(ball)) + @test isequal(only(equations(cev)), x ~ 0) + @test isequal(only(observed(cev.affect.system)), v ~ -Pre(v)) ball = mtkcompile(ball) @test length(ModelingToolkit.continuous_events(ball)) == 1 @@ -343,10 +344,11 @@ end cb = get_callback(prob) @test cb isa ModelingToolkit.DiffEqCallbacks.VectorContinuousCallback - @test getfield(ball, :continuous_events)[1] == - SymbolicContinuousCallback(Equation[x ~ 0], Equation[vx ~ -Pre(vx)]) - @test getfield(ball, :continuous_events)[2] == - SymbolicContinuousCallback(Equation[y ~ -1.5, y ~ 1.5], Equation[vy ~ -Pre(vy)]) + _cevs = getfield(ball, :continuous_events) + @test isequal(only(equations(_cevs[1])), x ~ 0) + @test isequal(only(observed(_cevs[1].affect.system)), vx ~ -Pre(vx)) + @test issetequal(equations(_cevs[2]), [y ~ -1.5, y ~ 1.5]) + @test isequal(only(observed(_cevs[2].affect.system)), vy ~ -Pre(vy)) cond = cb.condition out = [0.0, 0.0, 0.0] p0 = 0.0 From 018e2d631d24ca0d418bceef6b215ee927f160d1 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 2 Jun 2025 16:51:00 +0530 Subject: [PATCH 1938/2176] refactor: add depwarns for old system and problem constructors --- src/deprecations.jl | 162 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 162 insertions(+) diff --git a/src/deprecations.jl b/src/deprecations.jl index 73df50ecc4..aaafa0d283 100644 --- a/src/deprecations.jl +++ b/src/deprecations.jl @@ -8,3 +8,165 @@ macro mtkbuild(exprs...) @mtkcompile $(exprs...) end |> esc end + +for T in [:ODESystem, :NonlinearSystem, :DiscreteSystem, :ImplicitDiscreteSystem] + @eval @deprecate $T(args...; kwargs...) System(args...; kwargs...) +end + +for T in [:ODEProblem, :DDEProblem, :SDEProblem, :SDDEProblem, :DAEProblem, + :BVProblem, :DiscreteProblem, :ImplicitDiscreteProblem] + for (pType, pCanonical) in [ + (AbstractDict, :p), + (AbstractArray{<:Pair}, :(Dict(p))), + (AbstractArray, :(isempty(p) ? Dict() : Dict(parameters(sys) .=> p))) + ], + (uType, uCanonical) in [ + (Nothing, :(Dict())), + (AbstractDict, :u0), + (AbstractArray{<:Pair}, :(Dict(u0))), + (AbstractArray, :(isempty(u0) ? Dict() : Dict(unknowns(sys) .=> u0))) + ] + + @eval function SciMLBase.$T(sys::System, u0::$uType, tspan, p::$pType; kw...) + ctor = string($T) + uCan = string($(QuoteNode(uCanonical))) + pCan = string($(QuoteNode(pCanonical))) + @warn """ + `$ctor(sys, u0, tspan, p; kw...)` is deprecated. Use + `$ctor(sys, merge($uCan, $pCan), tspan)` instead. + """ + SciMLBase.$T(sys, merge($uCanonical, $pCanonical), tspan; kw...) + end + @eval function SciMLBase.$T{iip}( + sys::System, u0::$uType, tspan, p::$pType; kw...) where {iip} + ctor = string($T{iip}) + uCan = string($(QuoteNode(uCanonical))) + pCan = string($(QuoteNode(pCanonical))) + @warn """ + `$ctor(sys, u0, tspan, p; kw...)` is deprecated. Use + `$ctor(sys, merge($uCan, $pCan), tspan)` instead. + """ + return SciMLBase.$T{iip}(sys, merge($uCanonical, $pCanonical), tspan; kw...) + end + @eval function SciMLBase.$T{iip, spec}( + sys::System, u0::$uType, tspan, p::$pType; kw...) where {iip, spec} + ctor = string($T{iip, spec}) + uCan = string($(QuoteNode(uCanonical))) + pCan = string($(QuoteNode(pCanonical))) + @warn """ + `$ctor(sys, u0, tspan, p; kw...)` is deprecated. Use + `$ctor(sys, merge($uCan, $pCan), tspan)` instead. + """ + return $T{iip, spec}(sys, merge($uCanonical, $pCanonical), tspan; kw...) + end + end + + for pType in [SciMLBase.NullParameters, Nothing], uType in [Any, Nothing] + @eval function SciMLBase.$T(sys::System, u0::$uType, tspan, p::$pType; kw...) + ctor = string($T) + pT = string($(QuoteNode(pType))) + @warn """ + `$ctor(sys, u0, tspan, p::$pT; kw...)` is deprecated. Use + `$ctor(sys, u0, tspan)` instead. + """ + $T(sys, u0, tspan; kw...) + end + @eval function SciMLBase.$T{iip}( + sys::System, u0::$uType, tspan, p::$pType; kw...) where {iip} + ctor = string($T{iip}) + pT = string($(QuoteNode(pType))) + @warn """ + `$ctor(sys, u0, tspan, p::$pT; kw...)` is deprecated. Use + `$ctor(sys, u0, tspan)` instead. + """ + return $T{iip}(sys, u0, tspan; kw...) + end + @eval function SciMLBase.$T{iip, spec}( + sys::System, u0::$uType, tspan, p::$pType; kw...) where {iip, spec} + ctor = string($T{iip, spec}) + pT = string($(QuoteNode(pType))) + @warn """ + `$ctor(sys, u0, tspan, p::$pT; kw...)` is deprecated. Use + `$ctor(sys, u0, tspan)` instead. + """ + return $T{iip, spec}(sys, u0, tspan; kw...) + end + end +end + +for T in [:NonlinearProblem, :NonlinearLeastSquaresProblem, + :SCCNonlinearProblem, :OptimizationProblem, :SteadyStateProblem] + for (pType, pCanonical) in [ + (AbstractDict, :p), + (AbstractArray{<:Pair}, :(Dict(p))), + (AbstractArray, :(isempty(p) ? Dict() : Dict(parameters(sys) .=> p))) + ], + (uType, uCanonical) in [ + (Nothing, :(Dict())), + (AbstractDict, :u0), + (AbstractArray{<:Pair}, :(Dict(u0))), + (AbstractArray, :(isempty(u0) ? Dict() : Dict(unknowns(sys) .=> u0))) + ] + + @eval function SciMLBase.$T(sys::System, u0::$uType, p::$pType; kw...) + ctor = string($T) + uCan = string($(QuoteNode(uCanonical))) + pCan = string($(QuoteNode(pCanonical))) + @warn """ + `$ctor(sys, u0, p; kw...)` is deprecated. Use `$ctor(sys, merge($uCan, $pCan))` + instead. + """ + $T(sys, merge($uCanonical, $pCanonical); kw...) + end + @eval function SciMLBase.$T{iip}( + sys::System, u0::$uType, p::$pType; kw...) where {iip} + ctor = string($T{iip}) + uCan = string($(QuoteNode(uCanonical))) + pCan = string($(QuoteNode(pCanonical))) + @warn """ + `$ctor(sys, u0, p; kw...)` is deprecated. Use `$ctor(sys, merge($uCan, $pCan))` + instead. + """ + return $T{iip}(sys, merge($uCanonical, $pCanonical); kw...) + end + @eval function SciMLBase.$T{iip, spec}( + sys::System, u0::$uType, p::$pType; kw...) where {iip, spec} + ctor = string($T{iip, spec}) + uCan = string($(QuoteNode(uCanonical))) + pCan = string($(QuoteNode(pCanonical))) + @warn """ + `$ctor(sys, u0, p; kw...)` is deprecated. Use `$ctor(sys, merge($uCan, $pCan))` + instead. + """ + return $T{iip, spec}(sys, merge($uCanonical, $pCanonical); kw...) + end + end + for pType in [SciMLBase.NullParameters, Nothing], uType in [Any, Nothing] + @eval function SciMLBase.$T(sys::System, u0::$uType, p::$pType; kw...) + ctor = string($T) + pT = string($(QuoteNode(pType))) + @warn """ + `$ctor(sys, u0, p::$pT; kw...)` is deprecated. Use `$ctor(sys, u0)` instead + """ + $T(sys, u0; kw...) + end + @eval function SciMLBase.$T{iip}( + sys::System, u0::$uType, p::$pType; kw...) where {iip} + ctor = string($T{iip}) + pT = string($(QuoteNode(pType))) + @warn """ + `$ctor(sys, u0, p::$pT; kw...)` is deprecated. Use `$ctor(sys, u0)` instead + """ + return $T{iip}(sys, u0; kw...) + end + @eval function SciMLBase.$T{iip, spec}( + sys::System, u0::$uType, p::$pType; kw...) where {iip, spec} + ctor = string($T{iip, spec}) + pT = string($(QuoteNode(pType))) + @warn """ + `$ctor(sys, u0, p::$pT; kw...)` is deprecated. Use `$ctor(sys, u0)` instead + """ + return $T{iip, spec}(sys, u0; kw...) + end + end +end From f6148795250826994c929010a7ca6a4c06ceebef Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 2 Jun 2025 16:51:18 +0530 Subject: [PATCH 1939/2176] fix: use `vars` instead of `get_variables` in `Shift` implementation --- src/discretedomain.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/discretedomain.jl b/src/discretedomain.jl index f8a1a17009..73e6b8b7fa 100644 --- a/src/discretedomain.jl +++ b/src/discretedomain.jl @@ -231,7 +231,7 @@ function (xn::Num)(k::ShiftIndex) @unpack clock, steps = k x = value(xn) # Verify that the independent variables of k and x match and that the expression doesn't have multiple variables - vars = Symbolics.get_variables(x) + vars = ModelingToolkit.vars(x) if length(vars) != 1 error("Cannot shift a multivariate expression $x. Either create a new unknown and shift this, or shift the individual variables in the expression.") end From 1d446ca24903d694f872fd01c0d042ad8d2a6ab9 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 2 Jun 2025 16:51:30 +0530 Subject: [PATCH 1940/2176] test: test new deprecations --- test/sciml_problem_inputs.jl | 90 ++++++++++++++++++++++++++++++++++-- 1 file changed, 86 insertions(+), 4 deletions(-) diff --git a/test/sciml_problem_inputs.jl b/test/sciml_problem_inputs.jl index c02bd9dea1..cd081118fb 100644 --- a/test/sciml_problem_inputs.jl +++ b/test/sciml_problem_inputs.jl @@ -2,7 +2,7 @@ # Fetch packages using ModelingToolkit, JumpProcesses, NonlinearSolve, OrdinaryDiffEq, StaticArrays, - SteadyStateDiffEq, StochasticDiffEq, Test + SteadyStateDiffEq, StochasticDiffEq, SciMLBase, Test using ModelingToolkit: t_nounits as t, D_nounits as D # Sets rnd number. @@ -101,7 +101,7 @@ begin end # Perform ODE simulations (singular and ensemble). -let +@testset "ODE" begin # Creates normal and ensemble problems. base_oprob = ODEProblem(osys, [u0_alts[1]; p_alts[1]], tspan) base_sol = solve(base_oprob, Tsit5(); saveat = 1.0) @@ -119,7 +119,7 @@ let end # Solves a nonlinear problem (EnsembleProblems are not possible for these). -let +@testset "Nonlinear" begin base_nlprob = NonlinearProblem(nsys, [u0_alts[1]; p_alts[1]]) base_sol = solve(base_nlprob, NewtonRaphson()) # Solves problems for all input types, checking that identical solutions are found. @@ -130,7 +130,7 @@ let end # Perform steady state simulations (singular and ensemble). -let +@testset "SteadyState" begin # Creates normal and ensemble problems. base_ssprob = SteadyStateProblem(osys, [u0_alts[1]; p_alts[1]]) base_sol = solve(base_ssprob, DynamicSS(Tsit5())) @@ -146,3 +146,85 @@ let @test base_esol == solve(eprob, DynamicSS(Tsit5()); trajectories = 2) end end + +@testset "Deprecations" begin + @variables _x(..) = 1.0 + @parameters p = 1.0 + @brownian a + x = _x(t) + k = ShiftIndex(t) + + @test_deprecated ODESystem([D(x) ~ x * p], t; name = :a) + @mtkcompile odesys = System([D(x) ~ x * p], t) + @mtkcompile sdesys = System([D(x) ~ x * p + a], t) + @test_deprecated NonlinearSystem([0 ~ x^3 + p]; name = :a) + @mtkcompile nlsys = System([0 ~ x^3 + p]) + @mtkcompile ddesys = System([D(x) ~ x * p + _x(t - 0.1)], t) + @mtkcompile sddesys = System([D(x) ~ x * p + _x(t - 0.1) + a], t) + @test_deprecated DiscreteSystem([x ~ x(k - 1) + x(k - 2)], t; name = :a) + @mtkcompile discsys = System([x ~ x(k - 1) * p], t) + @test_deprecated ImplicitDiscreteSystem([x ~ x(k - 1) + x(k - 2) * p * x], t; name = :a) + @mtkcompile idiscsys = System([x ~ x(k - 1) * p * x], t) + @mtkcompile optsys = OptimizationSystem(x^2 + p) + + u0s = [ + Dict(x => 1.0), + [x => 1.0], + [1.0], + [], + nothing + ] + ps = [ + Dict(p => 1.0), + [p => 1.0], + [1.0], + [], + nothing, + SciMLBase.NullParameters() + ] + tspan = (0.0, 1.0) + + @testset "$ctor" for (sys, ctor) in [ + (odesys, ODEProblem), + (odesys, ODEProblem{true}), + (odesys, ODEProblem{true, SciMLBase.FullSpecialize}), (odesys, BVProblem), + (odesys, BVProblem{true}), + (odesys, BVProblem{true, SciMLBase.FullSpecialize}), (sdesys, SDEProblem), + (sdesys, SDEProblem{true}), + (sdesys, SDEProblem{true, SciMLBase.FullSpecialize}), (ddesys, DDEProblem), + (ddesys, DDEProblem{true}), + (ddesys, DDEProblem{true, SciMLBase.FullSpecialize}), (sddesys, SDDEProblem), + (sddesys, SDDEProblem{true}), + (sddesys, SDDEProblem{true, SciMLBase.FullSpecialize}), + + # (discsys, DiscreteProblem), + # (discsys, DiscreteProblem{true}), + # (discsys, DiscreteProblem{true, SciMLBase.FullSpecialize}), + + (idiscsys, ImplicitDiscreteProblem), + (idiscsys, ImplicitDiscreteProblem{true}), + (idiscsys, ImplicitDiscreteProblem{true, SciMLBase.FullSpecialize}) + ] + @testset "$(typeof(u0)) - $(typeof(p))" for u0 in u0s, p in ps + if u0 isa Vector{Float64} && ctor <: ImplicitDiscreteProblem + u0 = ones(2) + end + @test_warn ["deprecated"] ctor(sys, u0, tspan, p) + end + end + @testset "$ctor" for (sys, ctor) in [ + (nlsys, NonlinearProblem), + (nlsys, NonlinearProblem{true}), + (nlsys, NonlinearProblem{true, SciMLBase.FullSpecialize}), ( + nlsys, NonlinearLeastSquaresProblem), + (nlsys, NonlinearLeastSquaresProblem{true}), + (nlsys, NonlinearLeastSquaresProblem{true, SciMLBase.FullSpecialize}), ( + nlsys, SCCNonlinearProblem), + (nlsys, SCCNonlinearProblem{true}), (optsys, OptimizationProblem), + (optsys, OptimizationProblem{true}) + ] + @testset "$(typeof(u0)) - $(typeof(p))" for u0 in u0s, p in ps + @test_warn ["deprecated"] ctor(sys, u0, p) + end + end +end From 24226f474a67a7e0f07d0720471c750a019cb379 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 3 Jun 2025 11:38:38 +0530 Subject: [PATCH 1941/2176] test: move linearize testset to InterfaceII --- test/{downstream => }/linearize.jl | 44 ++++++++++++------------------ test/runtests.jl | 2 +- 2 files changed, 18 insertions(+), 28 deletions(-) rename test/{downstream => }/linearize.jl (91%) diff --git a/test/downstream/linearize.jl b/test/linearize.jl similarity index 91% rename from test/downstream/linearize.jl rename to test/linearize.jl index f86d7c937e..ddb90f3eac 100644 --- a/test/downstream/linearize.jl +++ b/test/linearize.jl @@ -87,10 +87,10 @@ connections = [f.y ~ c.r # filtered reference to controller reference @named cl = System(connections, t, systems = [f, c, p]) -lsys0, ssys = linearize(cl) +lsys0, ssys = linearize(cl, [f.u], [p.x]) desired_order = [f.x, p.x] lsys = ModelingToolkit.reorder_unknowns(lsys0, unknowns(ssys), desired_order) -lsys1, ssys = linearize(cl; autodiff = AutoFiniteDiff()) +lsys1, ssys = linearize(cl, [f.u], [p.x]; autodiff = AutoFiniteDiff()) lsys2 = ModelingToolkit.reorder_unknowns(lsys1, unknowns(ssys), desired_order) @test lsys.A == lsys2.A == [-2 0; 1 -2] @@ -99,8 +99,9 @@ lsys2 = ModelingToolkit.reorder_unknowns(lsys1, unknowns(ssys), desired_order) @test lsys.D[] == lsys2.D[] == 0 ## Symbolic linearization -lsyss, _ = ModelingToolkit.linearize_symbolic(cl, [f.u], [p.x]) +lsyss, ssys = ModelingToolkit.linearize_symbolic(cl, [f.u], [p.x]) +lsyss = ModelingToolkit.reorder_unknowns(lsyss, unknowns(ssys), [f.x, p.x]) @test ModelingToolkit.fixpoint_sub(lsyss.A, ModelingToolkit.defaults(cl)) == lsys.A @test ModelingToolkit.fixpoint_sub(lsyss.B, ModelingToolkit.defaults(cl)) == lsys.B @test ModelingToolkit.fixpoint_sub(lsyss.C, ModelingToolkit.defaults(cl)) == lsys.C @@ -147,28 +148,17 @@ lsys = ModelingToolkit.reorder_unknowns(lsys, desired_order, reverse(desired_ord @test lsys.D == [4400 -4400] ## Test that there is a warning when input is misspecified -if VERSION >= v"1.8" - @test_throws "Some specified inputs were not found" linearize(pid, - [ - pid.reference.u, - pid.measurement.u - ], [ctr_output.u]) - @test_throws "Some specified outputs were not found" linearize(pid, - [ - reference.u, - measurement.u - ], - [pid.ctr_output.u]) -else # v1.6 does not have the feature to match error message - @test_throws ErrorException linearize(pid, - [ - pid.reference.u, - pid.measurement.u - ], [ctr_output.u]) - @test_throws ErrorException linearize(pid, - [reference.u, measurement.u], - [pid.ctr_output.u]) -end +@test_throws "Some specified inputs were not found" linearize(pid, + [ + pid.reference.u, + pid.measurement.u + ], [ctr_output.u]) +@test_throws "Some specified outputs were not found" linearize(pid, + [ + reference.u, + measurement.u + ], + [pid.ctr_output.u]) ## Test operating points @@ -266,7 +256,7 @@ closed_loop = System(connections, t, systems = [model, pid, filt, sensor, r, er] filt.xd => 0.0 ]) -@test_nowarn linearize(closed_loop; warn_empty_op = false) +@test_nowarn linearize(closed_loop, :r, :y; warn_empty_op = false) # https://discourse.julialang.org/t/mtk-change-in-linearize/115760/3 @mtkmodel Tank_noi begin @@ -327,7 +317,7 @@ end eqs = [0 ~ x * log(y) - p] @named sys = System(eqs, t; defaults = [p => 1.0]) sys = complete(sys) - @test_throws ModelingToolkit.MissingVariablesError linearize( + @test_throws ModelingToolkit.MissingGuessError linearize( sys, [x], []; op = Dict(x => 1.0), allow_input_derivatives = true) @test_nowarn linearize( sys, [x], []; op = Dict(x => 1.0), guesses = Dict(y => 1.0), diff --git a/test/runtests.jl b/test/runtests.jl index 2014162cc3..31e4cc609f 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -97,6 +97,7 @@ end @safetestset "Debugging Test" include("debugging.jl") @safetestset "Namespacing test" include("namespacing.jl") @safetestset "Subsystem replacement" include("substitute_component.jl") + @safetestset "Linearization Tests" include("linearize.jl") end end @@ -120,7 +121,6 @@ end if GROUP == "All" || GROUP == "Downstream" activate_downstream_env() - @safetestset "Linearization Tests" include("downstream/linearize.jl") @safetestset "Linearization Dummy Derivative Tests" include("downstream/linearization_dd.jl") @safetestset "Inverse Models Test" include("downstream/inversemodel.jl") @safetestset "Analysis Points Test" include("downstream/analysis_points.jl") From b173d9ca1c3125c9c436e2cf1c6c26a28c8b6e6d Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 3 Jun 2025 11:38:45 +0530 Subject: [PATCH 1942/2176] fix: fix `linearize_symbolic` --- src/linearization.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/linearization.jl b/src/linearization.jl index 0c174e4cc3..88fe1d39d3 100644 --- a/src/linearization.jl +++ b/src/linearization.jl @@ -505,7 +505,7 @@ function linearize_symbolic(sys::AbstractSystem, inputs, ps = parameters(sys; initial_parameters = true) p = reorder_parameters(sys, ps) - fun_expr = generate_function(sys, sts, ps; expression = Val{true})[1] + fun_expr = generate_rhs(sys; expression = Val{true})[1] fun = eval_or_rgf(fun_expr; eval_expression, eval_module) dx = fun(sts, p, t) From e2ddd787b6293f7de57a1c872c4ec51bc6e16d90 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 3 Jun 2025 11:39:12 +0530 Subject: [PATCH 1943/2176] refactor: format --- src/systems/analysis_points.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/systems/analysis_points.jl b/src/systems/analysis_points.jl index 1a21a3f7b3..3b65dd6669 100644 --- a/src/systems/analysis_points.jl +++ b/src/systems/analysis_points.jl @@ -974,8 +974,8 @@ end function linearization_function(sys::AbstractSystem, inputs::Union{Symbol, Vector{Symbol}, AnalysisPoint, Vector{AnalysisPoint}}, outputs; loop_openings = [], system_modifier = identity, kwargs...) - - sys, input_vars, output_vars = linearization_ap_transform(sys, inputs, outputs, loop_openings) + sys, input_vars, output_vars = linearization_ap_transform( + sys, inputs, outputs, loop_openings) return linearization_function(system_modifier(sys), input_vars, output_vars; kwargs...) end From ab25ef57e3ac4f165801faf5d490ffc670ca588e Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Sun, 1 Jun 2025 13:32:23 +0530 Subject: [PATCH 1944/2176] refactor: remove `partial_state_selection` --- .../StructuralTransformations.jl | 2 +- .../partial_state_selection.jl | 169 ------------------ test/state_selection.jl | 21 --- .../index_reduction.jl | 11 -- 4 files changed, 1 insertion(+), 202 deletions(-) diff --git a/src/structural_transformation/StructuralTransformations.jl b/src/structural_transformation/StructuralTransformations.jl index 33269f88a7..df30f7d2d2 100644 --- a/src/structural_transformation/StructuralTransformations.jl +++ b/src/structural_transformation/StructuralTransformations.jl @@ -55,7 +55,7 @@ using SimpleNonlinearSolve using DocStringExtensions -export tearing, partial_state_selection, dae_index_lowering, check_consistency +export tearing, dae_index_lowering, check_consistency export dummy_derivative export sorted_incidence_matrix, pantelides!, pantelides_reassemble, tearing_reassemble, find_solvables!, diff --git a/src/structural_transformation/partial_state_selection.jl b/src/structural_transformation/partial_state_selection.jl index 8a0ae5276e..887571ceb1 100644 --- a/src/structural_transformation/partial_state_selection.jl +++ b/src/structural_transformation/partial_state_selection.jl @@ -1,173 +1,4 @@ -function partial_state_selection_graph!(state::TransformationState) - find_solvables!(state; allow_symbolic = true) - var_eq_matching = complete(pantelides!(state)) - complete!(state.structure) - partial_state_selection_graph!(state.structure, var_eq_matching) -end - -function ascend_dg(xs, dg, level) - while level > 0 - xs = Int[dg[x] for x in xs] - level -= 1 - end - return xs -end - -function ascend_dg_all(xs, dg, level, maxlevel) - r = Int[] - while true - if level <= 0 - append!(r, xs) - end - maxlevel <= 0 && break - xs = Int[dg[x] for x in xs if dg[x] !== nothing] - level -= 1 - maxlevel -= 1 - end - return r -end - -function pss_graph_modia!(structure::SystemStructure, maximal_top_matching, varlevel, - inv_varlevel, inv_eqlevel) - @unpack eq_to_diff, var_to_diff, graph, solvable_graph = structure - - # var_eq_matching is a maximal matching on the top-differentiated variables. - # Find Strongly connected components. Note that after pantelides, we expect - # a balanced system, so a maximal matching should be possible. - var_sccs::Vector{Union{Vector{Int}, Int}} = find_var_sccs(graph, maximal_top_matching) - var_eq_matching = Matching{Union{Unassigned, SelectedState}}(ndsts(graph)) - for vars in var_sccs - # TODO: We should have a way to not have the scc code look at unassigned vars. - if length(vars) == 1 && maximal_top_matching[vars[1]] === unassigned - continue - end - - # Now proceed level by level from lowest to highest and tear the graph. - eqs = [maximal_top_matching[var] - for var in vars if maximal_top_matching[var] !== unassigned] - isempty(eqs) && continue - maxeqlevel = maximum(map(x -> inv_eqlevel[x], eqs)) - maxvarlevel = level = maximum(map(x -> inv_varlevel[x], vars)) - old_level_vars = () - ict = IncrementalCycleTracker( - DiCMOBiGraph{true}(graph, - complete(Matching(ndsts(graph)), nsrcs(graph))), - dir = :in) - - while level >= 0 - to_tear_eqs_toplevel = filter(eq -> inv_eqlevel[eq] >= level, eqs) - to_tear_eqs = ascend_dg(to_tear_eqs_toplevel, invview(eq_to_diff), level) - - to_tear_vars_toplevel = filter(var -> inv_varlevel[var] >= level, vars) - to_tear_vars = ascend_dg(to_tear_vars_toplevel, invview(var_to_diff), level) - - assigned_eqs = Int[] - - if old_level_vars !== () - # Inherit constraints from previous level. - # TODO: Is this actually a good idea or do we want full freedom - # to tear differently on each level? Does it make a difference - # whether we're using heuristic or optimal tearing? - removed_eqs = Int[] - removed_vars = Int[] - for var in old_level_vars - old_assign = var_eq_matching[var] - if isa(old_assign, SelectedState) - push!(removed_vars, var) - continue - elseif !isa(old_assign, Int) || - ict.graph.matching[var_to_diff[var]] !== unassigned - continue - end - # Make sure the ict knows about this edge, so it doesn't accidentally introduce - # a cycle. - assgned_eq = eq_to_diff[old_assign] - ok = try_assign_eq!(ict, var_to_diff[var], assgned_eq) - @assert ok - var_eq_matching[var_to_diff[var]] = assgned_eq - push!(removed_eqs, eq_to_diff[ict.graph.matching[var]]) - push!(removed_vars, var_to_diff[var]) - push!(removed_vars, var) - end - to_tear_eqs = setdiff(to_tear_eqs, removed_eqs) - to_tear_vars = setdiff(to_tear_vars, removed_vars) - end - tearEquations!(ict, solvable_graph.fadjlist, to_tear_eqs, BitSet(to_tear_vars), - nothing) - - for var in to_tear_vars - @assert var_eq_matching[var] === unassigned - assgned_eq = ict.graph.matching[var] - var_eq_matching[var] = assgned_eq - isa(assgned_eq, Int) && push!(assigned_eqs, assgned_eq) - end - - if level != 0 - remaining_vars = collect(v - for v in to_tear_vars - if var_eq_matching[v] === unassigned) - if !isempty(remaining_vars) - remaining_eqs = setdiff(to_tear_eqs, assigned_eqs) - nlsolve_matching = maximal_matching(graph, - Base.Fix2(in, remaining_eqs), - Base.Fix2(in, remaining_vars)) - for var in remaining_vars - if nlsolve_matching[var] === unassigned && - var_eq_matching[var] === unassigned - var_eq_matching[var] = SelectedState() - end - end - end - end - - old_level_vars = to_tear_vars - level -= 1 - end - end - return complete(var_eq_matching, nsrcs(graph)) -end - struct SelectedState end -function partial_state_selection_graph!(structure::SystemStructure, var_eq_matching) - @unpack eq_to_diff, var_to_diff, graph, solvable_graph = structure - eq_to_diff = complete(eq_to_diff) - - inv_eqlevel = map(1:nsrcs(graph)) do eq - level = 0 - while invview(eq_to_diff)[eq] !== nothing - eq = invview(eq_to_diff)[eq] - level += 1 - end - level - end - - varlevel = map(1:ndsts(graph)) do var - graph_level = level = 0 - while var_to_diff[var] !== nothing - var = var_to_diff[var] - level += 1 - if !isempty(𝑑neighbors(graph, var)) - graph_level = level - end - end - graph_level - end - - inv_varlevel = map(1:ndsts(graph)) do var - level = 0 - while invview(var_to_diff)[var] !== nothing - var = invview(var_to_diff)[var] - level += 1 - end - level - end - - var_eq_matching = pss_graph_modia!(structure, - complete(var_eq_matching), varlevel, inv_varlevel, - inv_eqlevel) - - var_eq_matching -end function dummy_derivative_graph!(state::TransformationState, jac = nothing; state_priority = nothing, log = Val(false), kwargs...) diff --git a/test/state_selection.jl b/test/state_selection.jl index 97741110e2..fd7a840798 100644 --- a/test/state_selection.jl +++ b/test/state_selection.jl @@ -20,27 +20,6 @@ let dd = dummy_derivative(sys) @test length(unknowns(dd)) == length(equations(dd)) < 9 end -@test_skip let pss = partial_state_selection(sys) - @test length(equations(pss)) == 1 - @test length(unknowns(pss)) == 2 -end - -@parameters σ ρ β -@variables x(t) y(t) z(t) a(t) u(t) F(t) - -eqs = [D(x) ~ σ * (y - x) - D(y) ~ x * (ρ - z) - y + β - 0 ~ z - x + y - 0 ~ a + z - u ~ z + a] - -lorenz1 = System(eqs, t, name = :lorenz1) -let al1 = alias_elimination(lorenz1) - let lss = partial_state_selection(al1) - @test length(equations(lss)) == 2 - end -end - # 1516 let @connector function Fluid_port(; name, p = 101325.0, m = 0.0, T = 293.15) diff --git a/test/structural_transformation/index_reduction.jl b/test/structural_transformation/index_reduction.jl index 94dd1f884b..229b1f5736 100644 --- a/test/structural_transformation/index_reduction.jl +++ b/test/structural_transformation/index_reduction.jl @@ -31,12 +31,6 @@ eqs2 = [D(D(x)) ~ T * x, 0 ~ x^2 + y^2 - L^2] pendulum2 = System(eqs2, t, [x, y, T], [L, g], name = :pendulum) -@test_skip begin - let pss_pendulum2 = partial_state_selection(pendulum2) - length(equations(pss_pendulum2)) <= 6 - end -end - eqs = [D(x) ~ w, D(y) ~ z, D(w) ~ T * x, @@ -44,11 +38,6 @@ eqs = [D(x) ~ w, 0 ~ x^2 + y^2 - L^2] pendulum = System(eqs, t, [x, y, w, z, T], [L, g], name = :pendulum) -let pss_pendulum = partial_state_selection(pendulum) - # This currently selects `T` rather than `x` at top level. Needs tearing priorities to fix. - @test_broken length(equations(pss_pendulum)) == 3 -end - let sys = mtkcompile(pendulum2) @test length(equations(sys)) == 5 @test length(unknowns(sys)) == 5 From caf6f2ea3e99fd8740d6dd249306da72f1fa16e2 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Sun, 1 Jun 2025 13:32:50 +0530 Subject: [PATCH 1945/2176] refactor: always return all information from `dummy_derivative_graph!` --- src/structural_transformation/partial_state_selection.jl | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/structural_transformation/partial_state_selection.jl b/src/structural_transformation/partial_state_selection.jl index 887571ceb1..89447a4e16 100644 --- a/src/structural_transformation/partial_state_selection.jl +++ b/src/structural_transformation/partial_state_selection.jl @@ -174,11 +174,7 @@ function dummy_derivative_graph!( end ret = tearing_with_dummy_derivatives(structure, BitSet(dummy_derivatives)) - if log - (ret..., DummyDerivativeSummary(var_dummy_scc, var_state_priority)) - else - ret[1] - end + (ret..., DummyDerivativeSummary(var_dummy_scc, var_state_priority)) end function is_present(structure, v)::Bool From 9db7e6aa0d898f59ee3554d24388e266f25b40fa Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Sun, 1 Jun 2025 13:33:25 +0530 Subject: [PATCH 1946/2176] refactor: sort SCCs in `find_var_sccs` --- src/structural_transformation/utils.jl | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/structural_transformation/utils.jl b/src/structural_transformation/utils.jl index 131010550e..f36584744a 100644 --- a/src/structural_transformation/utils.jl +++ b/src/structural_transformation/utils.jl @@ -153,6 +153,9 @@ function find_var_sccs(g::BipartiteGraph, assign = nothing) cmog = DiCMOBiGraph{true}(g, Matching(assign === nothing ? Base.OneTo(nsrcs(g)) : assign)) sccs = Graphs.strongly_connected_components(cmog) + cgraph = MatchedCondensationGraph(cmog, sccs) + toporder = topological_sort(cgraph) + permute!(sccs, toporder) foreach(sort!, sccs) return sccs end From eac59a14d384f19c95fda130ccfd244cd5c339b5 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Sun, 1 Jun 2025 13:33:42 +0530 Subject: [PATCH 1947/2176] fix: fix `but_sorted_incidence` --- src/structural_transformation/utils.jl | 1 + 1 file changed, 1 insertion(+) diff --git a/src/structural_transformation/utils.jl b/src/structural_transformation/utils.jl index f36584744a..e900764698 100644 --- a/src/structural_transformation/utils.jl +++ b/src/structural_transformation/utils.jl @@ -352,6 +352,7 @@ function but_ordered_incidence(ts::TearingState, varmask = highest_order_variabl isemptyc || push!(bb, l) end mm = incidence_matrix(graph) + reverse!(vordering) mm[[var_eq_matching[v] for v in vordering if var_eq_matching[v] isa Int], vordering], bb end From 4ecbc2c8a3bdddc40ba5ebd5f335a85992c4a9ac Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Sun, 1 Jun 2025 13:37:03 +0530 Subject: [PATCH 1948/2176] refactor: BLT sort equations and variables in tearing --- .../symbolics_tearing.jl | 537 +++++++++++++----- src/systems/system.jl | 7 +- src/systems/systemstructure.jl | 4 +- 3 files changed, 404 insertions(+), 144 deletions(-) diff --git a/src/structural_transformation/symbolics_tearing.jl b/src/structural_transformation/symbolics_tearing.jl index f6c394ee1c..0014a947c8 100644 --- a/src/structural_transformation/symbolics_tearing.jl +++ b/src/structural_transformation/symbolics_tearing.jl @@ -317,11 +317,16 @@ Effects on the system structure: - fullvars: add the new derivative variables x_t - neweqs: add the identity equations for the new variables, D(x) ~ x_t - graph: update graph with the new equations and variables, and their connections -- solvable_graph: -- var_eq_matching: match D(x) to the added identity equation D(x) ~ x_t +- solvable_graph: mark the new equation as solvable for `D(x)` +- var_eq_matching: match D(x) to the added identity equation `D(x) ~ x_t` +- full_var_eq_matching: match `x_t` to the equation that `D(x)` used to match to, and + match `D(x)` to `D(x) ~ x_t` +- var_sccs: Replace `D(x)` in its SCC by `x_t`, and add `D(x)` in its own SCC. Return + the new list of SCCs. """ function generate_derivative_variables!( - ts::TearingState, neweqs, var_eq_matching; mm = nothing, iv = nothing, D = nothing) + ts::TearingState, neweqs, var_eq_matching, full_var_eq_matching, + var_sccs; mm, iv = nothing, D = nothing) @unpack fullvars, sys, structure = ts @unpack solvable_graph, var_to_diff, eq_to_diff, graph = structure eq_var_matching = invview(var_eq_matching) @@ -330,44 +335,117 @@ function generate_derivative_variables!( linear_eqs = mm === nothing ? Dict{Int, Int}() : Dict(reverse(en) for en in enumerate(mm.nzrows)) + # We need the inverse mapping of `var_sccs` to update it efficiently later. + v_to_scc = Vector{NTuple{2, Int}}(undef, ndsts(graph)) + for (i, scc) in enumerate(var_sccs), (j, v) in enumerate(scc) + v_to_scc[v] = (i, j) + end + # Pairs of `(x_t, dx)` added below + v_t_dvs = NTuple{2, Int}[] + # For variable x, make dummy derivative x_t if the # derivative is in the system for v in 1:length(var_to_diff) dv = var_to_diff[v] + # if the variable is not differentiated, there is nothing to do dv isa Int || continue + # if we will solve for the differentiated variable, there is nothing to do solved = var_eq_matching[dv] isa Int solved && continue # If there's `D(x) = x_t` already, update mappings and continue without # adding new equations/variables dd = find_duplicate_dd(dv, solvable_graph, diff_to_var, linear_eqs, mm) - if !isnothing(dd) - dummy_eq, v_t = dd - var_to_diff[v_t] = var_to_diff[dv] - var_eq_matching[dv] = unassigned - eq_var_matching[dummy_eq] = dv - continue + if dd === nothing + # there is no such pre-existing equation + # generate the dummy derivative variable + dx = fullvars[dv] + order, lv = var_order(dv, diff_to_var) + x_t = is_discrete ? lower_shift_varname_with_unit(fullvars[dv], iv) : + lower_varname_with_unit(fullvars[lv], iv, order) + + # Add `x_t` to the graph + v_t = add_dd_variable!(structure, fullvars, x_t, dv) + # Add `D(x) - x_t ~ 0` to the graph + dummy_eq = add_dd_equation!(structure, neweqs, 0 ~ dx - x_t, dv, v_t) + # Update graph to say, all the equations featuring D(x) also feature x_t + for e in 𝑑neighbors(graph, dv) + add_edge!(graph, e, v_t) + end + # Update matching + push!(var_eq_matching, unassigned) + push!(full_var_eq_matching, unassigned) + + # We also need to substitute all occurrences of `D(x)` with `x_t` in all equations + # except `dummy_eq`, but that is handled in `generate_system_equations!` since + # we will solve for `D(x) ~ x_t` and add it to the substitution map. + dd = dummy_eq, v_t end + # there is a duplicate `D(x) ~ x_t` equation + # `dummy_eq` is the index of the equation + # `v_t` is the dummy derivative variable + dummy_eq, v_t = dd + var_to_diff[v_t] = var_to_diff[dv] + old_matched_eq = full_var_eq_matching[dv] + full_var_eq_matching[dv] = var_eq_matching[dv] = dummy_eq + full_var_eq_matching[v_t] = old_matched_eq + eq_var_matching[dummy_eq] = dv + push!(v_t_dvs, (v_t, dv)) + end - dx = fullvars[dv] - order, lv = var_order(dv, diff_to_var) - x_t = is_discrete ? lower_shift_varname_with_unit(fullvars[dv], iv) : - lower_varname_with_unit(fullvars[lv], iv, order) - - # Add `x_t` to the graph - v_t = add_dd_variable!(structure, fullvars, x_t, dv) - # Add `D(x) - x_t ~ 0` to the graph - dummy_eq = add_dd_equation!(structure, neweqs, 0 ~ dx - x_t, dv, v_t) - # Update graph to say, all the equations featuring D(x) also feature x_t - for e in 𝑑neighbors(graph, dv) - add_edge!(graph, e, v_t) + # tuples of (index, scc) indicating that `scc` has to be inserted at + # index `index` in `var_sccs`. Same length as `v_t_dvs` because we will + # have one new SCC per new variable. + sccs_to_insert = similar(v_t_dvs, Tuple{Int, Vector{Int}}) + # mapping of SCC index to indexes in the SCC to delete + idxs_to_remove = Dict{Int, Vector{Int}}() + for (k, (v_t, dv)) in enumerate(v_t_dvs) + # replace `dv` with `v_t` + i, j = v_to_scc[dv] + var_sccs[i][j] = v_t + if v_t <= length(v_to_scc) + # v_t wasn't added by this process, it was already present. Which + # means we need to remove it from whatever SCC it is in, since it is + # now in this one + i_, j_ = v_to_scc[v_t] + scc_del_idxs = get!(() -> Int[], idxs_to_remove, i_) + push!(scc_del_idxs, j_) end + # `dv` still needs to be present in some SCC. Since we solve for `dv` from + # `0 ~ D(x) - x_t`, it is in its own SCC. This new singleton SCC is solved + # immediately before the one that `dv` used to be in (`i`) + sccs_to_insert[k] = (i, [dv]) + end + sort!(sccs_to_insert, by = first) + # remove the idxs we need to remove + for (i, idxs) in idxs_to_remove + deleteat!(var_sccs[i], idxs) + end + # insert the new SCCs, accounting for the fact that we might have multiple entries + # in `sccs_to_insert` to be inserted at the same index. + old_idx = 1 + insert_idx = 1 + new_sccs = similar(var_sccs, length(var_sccs) + length(sccs_to_insert)) + for i in eachindex(new_sccs) + # if we have SCCs to insert, and the index we have to insert them at is the current + # one in the old list of SCCs + if insert_idx <= length(sccs_to_insert) && sccs_to_insert[insert_idx][1] == old_idx + # insert it + new_sccs[i] = sccs_to_insert[insert_idx][2] + insert_idx += 1 + else + # otherwise, insert the old SCC + new_sccs[i] = copy(var_sccs[old_idx]) + old_idx += 1 + end + end - # Update matching - push!(var_eq_matching, unassigned) - var_eq_matching[dv] = unassigned - eq_var_matching[dummy_eq] = dv + filter!(!isempty, new_sccs) + if mm !== nothing + @set! mm.ncols = ndsts(graph) end + + return new_sccs end """ @@ -442,23 +520,20 @@ or solved variables on the RHS of the final equations will get substituted. The topological sort of the equations ensures that variables are solved for before they appear in equations. -Reorder the equations and unknowns to be: - [diffeqs; ...] - [diffvars; ...] -such that the mass matrix is: - [I 0 - 0 0]. +Reorder the equations and unknowns to be in the BLT sorted form. -Order the new equations and variables such that the differential equations -and variables come first. Return the new equations, the solved equations, +Return the new equations, the solved equations, the new orderings, and the number of solved variables and equations. """ -function generate_system_equations!(state::TearingState, neweqs, var_eq_matching; +function generate_system_equations!(state::TearingState, neweqs, var_eq_matching, + full_var_eq_matching, var_sccs, extra_eqs_vars; simplify = false, iv = nothing, D = nothing) @unpack fullvars, sys, structure = state @unpack solvable_graph, var_to_diff, eq_to_diff, graph = structure eq_var_matching = invview(var_eq_matching) + full_eq_var_matching = invview(full_var_eq_matching) diff_to_var = invview(var_to_diff) + extra_eqs, extra_vars = extra_eqs_vars total_sub = Dict() if is_only_discrete(structure) @@ -473,88 +548,236 @@ function generate_system_equations!(state::TearingState, neweqs, var_eq_matching end end - # if var is like D(x) or Shift(t, 1)(x) - isdervar = let diff_to_var = diff_to_var - var -> diff_to_var[var] !== nothing - end + eq_generator = EquationGenerator(state, total_sub, D, iv) - # Extract partition information - is_solvable = let solvable_graph = solvable_graph - (eq, iv) -> eq isa Int && iv isa Int && BipartiteEdge(eq, iv) in solvable_graph + # We need to solve extra equations before everything to repsect + # topological order. + for eq in extra_eqs + var = eq_var_matching[eq] + var isa Int || continue + codegen_equation!(eq_generator, neweqs[eq], eq, var; simplify) end - diff_eqs = Equation[] - diffeq_idxs = Int[] - diff_vars = Int[] - alge_eqs = Equation[] - algeeq_idxs = Int[] - solved_eqs = Equation[] - solvedeq_idxs = Int[] - solved_vars = Int[] + # if the variable is present in the equations either as-is or differentiated + ispresent = let var_to_diff = var_to_diff, graph = graph + i -> (!isempty(𝑑neighbors(graph, i)) || + (var_to_diff[i] !== nothing && !isempty(𝑑neighbors(graph, var_to_diff[i])))) + end - toporder = topological_sort(DiCMOBiGraph{false}(graph, var_eq_matching)) - eqs = Iterators.reverse(toporder) + digraph = DiCMOBiGraph{false}(graph, var_eq_matching) idep = iv + for (i, scc) in enumerate(var_sccs) + # note that the `vscc <-> escc` relation is a set-to-set mapping, and not + # point-to-point. + vscc, escc = get_sorted_scc(digraph, full_var_eq_matching, var_eq_matching, scc) + var_sccs[i] = vscc + + if length(escc) != length(vscc) + isempty(escc) && continue + escc = setdiff(escc, extra_eqs) + isempty(escc) && continue + vscc = setdiff(vscc, extra_vars) + isempty(vscc) && continue + end - # Generate equations. - # Solvable equations of differential variables D(x) become differential equations - # Solvable equations of non-differential variables become observable equations - # Non-solvable equations become algebraic equations. - for ieq in eqs - iv = eq_var_matching[ieq] - eq = neweqs[ieq] - - if is_solvable(ieq, iv) && isdervar(iv) - var = fullvars[iv] - isnothing(D) && throw(UnexpectedDifferentialError(equations(sys)[ieq])) - order, lv = var_order(iv, diff_to_var) - dx = D(simplify_shifts(fullvars[lv])) - - neweq = make_differential_equation(var, dx, eq, total_sub) - for e in 𝑑neighbors(graph, iv) - e == ieq && continue - rem_edge!(graph, e, iv) - end - - total_sub[simplify_shifts(neweq.lhs)] = neweq.rhs - # Substitute unshifted variables x(k), y(k) on RHS of implicit equations - if is_only_discrete(structure) - var_to_diff[iv] === nothing && (total_sub[var] = neweq.rhs) - end - push!(diff_eqs, neweq) - push!(diffeq_idxs, ieq) - push!(diff_vars, diff_to_var[iv]) - elseif is_solvable(ieq, iv) - var = fullvars[iv] - neweq = make_solved_equation(var, eq, total_sub; simplify) - !isnothing(neweq) && begin - push!(solved_eqs, neweq) - push!(solvedeq_idxs, ieq) - push!(solved_vars, iv) - end - else - neweq = make_algebraic_equation(eq, total_sub) - push!(alge_eqs, neweq) - push!(algeeq_idxs, ieq) + offset = 1 + for ieq in escc + iv = eq_var_matching[ieq] + eq = neweqs[ieq] + codegen_equation!(eq_generator, neweqs[ieq], ieq, iv; simplify) end end + for eq in extra_eqs + var = eq_var_matching[eq] + var isa Int && continue + codegen_equation!(eq_generator, neweqs[eq], eq, var; simplify) + end + + @unpack neweqs′, eq_ordering, var_ordering, solved_eqs, solved_vars = eq_generator + + is_diff_eq = .!iszero.(var_ordering) # Generate new equations and orderings - neweqs = [diff_eqs; alge_eqs] - eq_ordering = [diffeq_idxs; algeeq_idxs] + diff_vars = var_ordering[is_diff_eq] diff_vars_set = BitSet(diff_vars) if length(diff_vars_set) != length(diff_vars) error("Tearing internal error: lowering DAE into semi-implicit ODE failed!") end solved_vars_set = BitSet(solved_vars) - var_ordering = [diff_vars; - setdiff!(setdiff(1:ndsts(graph), diff_vars_set), - solved_vars_set)] - + # We filled zeros for algebraic variables, so fill them properly here + offset = 1 + for (i, v) in enumerate(var_ordering) + v == 0 || continue + # find the next variable which is not differential or solved, is not the + # derivative of another variable and is present in the equations + index = findnext(1:ndsts(graph), offset) do j + !(j in diff_vars_set || j in solved_vars_set) && diff_to_var[j] === nothing && + ispresent(j) + end + # in case of overdetermined systems, this may not be present + index === nothing && break + var_ordering[i] = index + offset = index + 1 + end + filter!(!iszero, var_ordering) + var_ordering = [var_ordering; setdiff(1:ndsts(graph), var_ordering, solved_vars_set)] + neweqs = neweqs′ return neweqs, solved_eqs, eq_ordering, var_ordering, length(solved_vars), length(solved_vars_set) end +""" + $(TYPEDSIGNATURES) + +Sort the provided SCC `scc`, given the `digraph` of the system constructed using +`var_eq_matching` along with both the matchings of the system. +""" +function get_sorted_scc( + digraph::DiCMOBiGraph, full_var_eq_matching::Matching, var_eq_matching::Matching, scc::Vector{Int}) + eq_var_matching = invview(var_eq_matching) + full_eq_var_matching = invview(full_var_eq_matching) + # obtain the matched equations in the SCC + scc_eqs = Int[full_var_eq_matching[v] for v in scc if full_var_eq_matching[v] isa Int] + # obtain the equations in the SCC that are linearly solvable + scc_solved_eqs = Int[var_eq_matching[v] for v in scc if var_eq_matching[v] isa Int] + # obtain the subgraph of the contracted graph involving the solved equations + subgraph, varmap = Graphs.induced_subgraph(digraph, scc_solved_eqs) + # topologically sort the solved equations and append the remainder + scc_eqs = [varmap[reverse(topological_sort(subgraph))]; + setdiff(scc_eqs, scc_solved_eqs)] + # the variables of the SCC are obtained by inverse mapping the sorted equations + # and appending the rest + scc_vars = [eq_var_matching[e] for e in scc_eqs if eq_var_matching[e] isa Int] + append!(scc_vars, setdiff(scc, scc_vars)) + return scc_vars, scc_eqs +end + +""" + $(TYPEDSIGNATURES) + +Struct containing the information required to generate equations of a system, as well as +the generated equations and associated metadata. +""" +struct EquationGenerator{S, D, I} + """ + `TearingState` of the system. + """ + state::S + """ + Substitutions to perform in all subsequent equations. For each differential equation + `D(x) ~ f(..)`, the substitution `D(x) => f(..)` is added to the rules. + """ + total_sub::Dict{Any, Any} + """ + The differential operator, or `nothing` if not applicable. + """ + D::D + """ + The independent variable, or `nothing` if not applicable. + """ + idep::I + """ + The new generated equations of the system. + """ + neweqs′::Vector{Equation} + """ + `eq_ordering[i]` is the index `neweqs′[i]` was originally at in the untorn equations of + the system. This is used to permute the state of the system into BLT sorted form. + """ + eq_ordering::Vector{Int} + """ + `var_ordering[i]` is the index in `state.fullvars` of the variable at the `i`th index in + the BLT sorted form. + """ + var_ordering::Vector{Int} + """ + List of linearly solved (observed) equations. + """ + solved_eqs::Vector{Equation} + """ + `eq_ordering` for `solved_eqs`. + """ + solved_vars::Vector{Int} +end + +function EquationGenerator(state, total_sub, D, idep) + EquationGenerator( + state, total_sub, D, idep, Equation[], Int[], Int[], Equation[], Int[]) +end + +""" + $(TYPEDSIGNATURES) + +Check if equation at index `ieq` is linearly solvable for variable at index `iv`. +""" +function is_solvable(eg::EquationGenerator, ieq, iv) + solvable_graph = eg.state.structure.solvable_graph + return ieq isa Int && iv isa Int && BipartiteEdge(ieq, iv) in solvable_graph +end + +""" + $(TYPEDSIGNATURES) + + If `iv` is like D(x) or Shift(t, 1)(x) +""" +function is_dervar(eg::EquationGenerator, iv::Int) + diff_to_var = invview(eg.state.structure.var_to_diff) + diff_to_var[iv] !== nothing +end + +""" + $(TYPEDSIGNATURES) + +Appropriately codegen the given equation `eq`, which occurs at index `ieq` in the untorn +list of equations and is matched to variable at index `iv`. +""" +function codegen_equation!(eg::EquationGenerator, + eq::Equation, ieq::Int, iv::Union{Int, Unassigned}; simplify = false) + # We generate equations ordered by the matched variables + # Solvable equations of differential variables D(x) become differential equations + # Solvable equations of non-differential variables become observable equations + # Non-solvable equations become algebraic equations. + @unpack state, total_sub, neweqs′, eq_ordering, var_ordering = eg + @unpack solved_eqs, solved_vars, D, idep = eg + @unpack fullvars, sys, structure = state + @unpack solvable_graph, var_to_diff, eq_to_diff, graph = structure + diff_to_var = invview(var_to_diff) + if is_solvable(eg, ieq, iv) && is_dervar(eg, iv) + var = fullvars[iv] + isnothing(D) && throw(UnexpectedDifferentialError(equations(sys)[ieq])) + order, lv = var_order(iv, diff_to_var) + dx = D(simplify_shifts(fullvars[lv])) + + neweq = make_differential_equation(var, dx, eq, total_sub) + for e in 𝑑neighbors(graph, iv) + e == ieq && continue + rem_edge!(graph, e, iv) + end + + total_sub[simplify_shifts(neweq.lhs)] = neweq.rhs + # Substitute unshifted variables x(k), y(k) on RHS of implicit equations + if is_only_discrete(structure) + var_to_diff[iv] === nothing && (total_sub[var] = neweq.rhs) + end + push!(neweqs′, neweq) + push!(eq_ordering, ieq) + push!(var_ordering, diff_to_var[iv]) + elseif is_solvable(eg, ieq, iv) + var = fullvars[iv] + neweq = make_solved_equation(var, eq, total_sub; simplify) + if neweq !== nothing + push!(solved_eqs, neweq) + push!(solved_vars, iv) + end + else + neweq = make_algebraic_equation(eq, total_sub) + push!(neweqs′, neweq) + push!(eq_ordering, ieq) + # we push a dummy to `var_ordering` here because `iv` is `unassigned` + push!(var_ordering, 0) + end +end + """ Occurs when a variable D(x) occurs in a non-differential system. """ @@ -615,8 +838,7 @@ tearing state to account for the new order. Permute the variables and equations. Eliminate the solved variables and equations from the graph and permute the graph's vertices to account for the new variable/equation ordering. """ -# TODO: BLT sorting -function reorder_vars!(state::TearingState, var_eq_matching, eq_ordering, +function reorder_vars!(state::TearingState, var_eq_matching, var_sccs, eq_ordering, var_ordering, nsolved_eq, nsolved_var) @unpack solvable_graph, var_to_diff, eq_to_diff, graph = state.structure @@ -650,6 +872,17 @@ function reorder_vars!(state::TearingState, var_eq_matching, eq_ordering, end new_fullvars = state.fullvars[var_ordering] + # Update the SCCs + var_ordering_set = BitSet(var_ordering) + for scc in var_sccs + # Map variables to their new indices + map!(v -> varsperm[v], scc, scc) + # Remove variables not in the reduced set + filter!(!iszero, scc) + end + # Remove empty SCCs + filter!(!isempty, var_sccs) + # Update system structure @set! state.structure.graph = complete(new_graph) @set! state.structure.var_to_diff = new_var_to_diff @@ -662,7 +895,7 @@ end Update the system equations, unknowns, and observables after simplification. """ function update_simplified_system!( - state::TearingState, neweqs, solved_eqs, dummy_sub, var_eq_matching, extra_unknowns; + state::TearingState, neweqs, solved_eqs, dummy_sub, var_sccs, extra_unknowns; cse_hack = true, array_hack = true) @unpack solvable_graph, var_to_diff, eq_to_diff, graph = state.structure diff_to_var = invview(var_to_diff) @@ -681,9 +914,9 @@ function update_simplified_system!( # TODO: compute the dependency correctly so that we don't have to do this obs = [fast_substitute(observed(sys), obs_sub); solved_eqs] - unknowns = Any[v - for (i, v) in enumerate(state.fullvars) - if diff_to_var[i] === nothing && ispresent(i)] + unknown_idxs = filter( + i -> diff_to_var[i] === nothing && ispresent(i), eachindex(state.fullvars)) + unknowns = state.fullvars[unknown_idxs] unknowns = [unknowns; extra_unknowns] @set! sys.unknowns = unknowns @@ -695,7 +928,12 @@ function update_simplified_system!( # Only makes sense for time-dependent if ModelingToolkit.has_schedule(sys) - @set! sys.schedule = Schedule(var_eq_matching, dummy_sub) + unknowns_set = BitSet(unknown_idxs) + for scc in var_sccs + intersect!(scc, unknowns_set) + end + filter!(!isempty, var_sccs) + @set! sys.schedule = Schedule(var_sccs, dummy_sub) end if ModelingToolkit.has_isscheduled(sys) @set! sys.isscheduled = true @@ -729,18 +967,19 @@ variables are marked as `SelectedState` and they are differentiated in the DAE system, i.e. `v'(t)` are all the variables in `u'(t)` that actually appear in the system. Algebraic variables are variables that are not differential variables. + +# Arguments + +- `state`: The `TearingState` of the system. +- `var_eq_matching`: The maximal matching after state selection. +- `full_var_eq_matching`: The maximal matching prior to state selection. +- `var_sccs`: The topologically sorted strongly connected components of the system + according to `full_var_eq_matching`. """ -function tearing_reassemble(state::TearingState, var_eq_matching, - full_var_eq_matching = nothing; simplify = false, mm = nothing, cse_hack = true, array_hack = true) - extra_vars = Int[] - if full_var_eq_matching !== nothing - for v in 𝑑vertices(state.structure.graph) - eq = full_var_eq_matching[v] - eq isa Int && continue - push!(extra_vars, v) - end - end - extra_unknowns = state.fullvars[extra_vars] +function tearing_reassemble(state::TearingState, var_eq_matching::Matching, + full_var_eq_matching::Matching, var_sccs::Vector{Vector{Int}}; simplify = false, mm, cse_hack = true, + array_hack = true, fully_determined = true) + extra_eqs_vars = get_extra_eqs_vars(state, full_var_eq_matching, fully_determined) neweqs = collect(equations(state)) dummy_sub = Dict() @@ -755,18 +994,22 @@ function tearing_reassemble(state::TearingState, var_eq_matching, iv = D = nothing end + extra_unknowns = state.fullvars[extra_eqs_vars[2]] # Structural simplification substitute_derivatives_algevars!(state, neweqs, var_eq_matching, dummy_sub; iv, D) - generate_derivative_variables!(state, neweqs, var_eq_matching; mm, iv, D) + var_sccs = generate_derivative_variables!( + state, neweqs, var_eq_matching, full_var_eq_matching, var_sccs; mm, iv, D) neweqs, solved_eqs, eq_ordering, var_ordering, nelim_eq, nelim_var = generate_system_equations!( - state, neweqs, var_eq_matching; simplify, iv, D) + state, neweqs, var_eq_matching, full_var_eq_matching, + var_sccs, extra_eqs_vars; simplify, iv, D) state = reorder_vars!( - state, var_eq_matching, eq_ordering, var_ordering, nelim_eq, nelim_var) + state, var_eq_matching, var_sccs, eq_ordering, var_ordering, nelim_eq, nelim_var) + # var_eq_matching and full_var_eq_matching are now invalidated - sys = update_simplified_system!(state, neweqs, solved_eqs, dummy_sub, var_eq_matching, + sys = update_simplified_system!(state, neweqs, solved_eqs, dummy_sub, var_sccs, extra_unknowns; cse_hack, array_hack) @set! state.sys = sys @@ -774,6 +1017,35 @@ function tearing_reassemble(state::TearingState, var_eq_matching, return invalidate_cache!(sys) end +""" + $(TYPEDSIGNATURES) + +Return a 2-tuple of integer vectors containing indices of extra equations and variables +respectively. For fully-determined systems, both of these are empty. Overdetermined systems +have extra equations, and underdetermined systems have extra variables. +""" +function get_extra_eqs_vars( + state::TearingState, full_var_eq_matching::Matching, fully_determined::Bool) + fully_determined && return Int[], Int[] + + extra_eqs = Int[] + extra_vars = Int[] + full_eq_var_matching = invview(full_var_eq_matching) + + for v in 𝑑vertices(state.structure.graph) + eq = full_var_eq_matching[v] + eq isa Int && continue + push!(extra_vars, v) + end + for eq in 𝑠vertices(state.structure.graph) + v = full_eq_var_matching[eq] + v isa Int && continue + push!(extra_eqs, eq) + end + + return extra_eqs, extra_vars +end + """ # HACK 1 @@ -937,22 +1209,11 @@ new residual equations after tearing. End users are encouraged to call [`mtkcomp instead, which calls this function internally. """ function tearing(sys::AbstractSystem, state = TearingState(sys); mm = nothing, - simplify = false, cse_hack = true, array_hack = true, kwargs...) - var_eq_matching, full_var_eq_matching = tearing(state) + simplify = false, cse_hack = true, array_hack = true, fully_determined = true, kwargs...) + var_eq_matching, full_var_eq_matching, var_sccs, can_eliminate = tearing(state) invalidate_cache!(tearing_reassemble( - state, var_eq_matching, full_var_eq_matching; mm, simplify, cse_hack, array_hack)) -end - -""" - partial_state_selection(sys; simplify=false) - -Perform partial state selection and tearing. -""" -function partial_state_selection(sys; simplify = false) - state = TearingState(sys) - var_eq_matching = partial_state_selection_graph!(state) - - tearing_reassemble(state, var_eq_matching; simplify = simplify) + state, var_eq_matching, full_var_eq_matching, var_sccs; mm, + simplify, cse_hack, array_hack, fully_determined)) end """ @@ -962,7 +1223,7 @@ Perform index reduction and use the dummy derivative technique to ensure that the system is balanced. """ function dummy_derivative(sys, state = TearingState(sys); simplify = false, - mm = nothing, cse_hack = true, array_hack = true, kwargs...) + mm = nothing, cse_hack = true, array_hack = true, fully_determined = true, kwargs...) jac = let state = state (eqs, vars) -> begin symeqs = EquationsView(state)[eqs] @@ -984,7 +1245,9 @@ function dummy_derivative(sys, state = TearingState(sys); simplify = false, p end end - var_eq_matching = dummy_derivative_graph!(state, jac; state_priority, + var_eq_matching, full_var_eq_matching, var_sccs, can_eliminate, summary = dummy_derivative_graph!( + state, jac; state_priority, kwargs...) - tearing_reassemble(state, var_eq_matching; simplify, mm, cse_hack, array_hack) + tearing_reassemble(state, var_eq_matching, full_var_eq_matching, var_sccs; + simplify, mm, cse_hack, array_hack, fully_determined) end diff --git a/src/systems/system.jl b/src/systems/system.jl index 5c01557cb5..7cab43156b 100644 --- a/src/systems/system.jl +++ b/src/systems/system.jl @@ -1,8 +1,5 @@ -struct Schedule{V <: BipartiteGraphs.Matching} - """ - Maximal matching of variables to equations calculated during structural simplification. - """ - var_eq_matching::V +struct Schedule + var_sccs::Vector{Vector{Int}} """ Mapping of `Differential`s of variables to corresponding derivative expressions. """ diff --git a/src/systems/systemstructure.jl b/src/systems/systemstructure.jl index 32da7a81bf..8b0e020cc4 100644 --- a/src/systems/systemstructure.jl +++ b/src/systems/systemstructure.jl @@ -762,10 +762,10 @@ function _mtkcompile!(state::TearingState; simplify = false, state = TearingState(sys) sys, mm = ModelingToolkit.alias_elimination!(state; kwargs...) sys = ModelingToolkit.dummy_derivative( - sys, state; simplify, mm, check_consistency, kwargs...) + sys, state; simplify, mm, check_consistency, fully_determined, kwargs...) else sys = ModelingToolkit.tearing( - sys, state; simplify, mm, check_consistency, kwargs...) + sys, state; simplify, mm, check_consistency, fully_determined, kwargs...) end fullunknowns = [observables(sys); unknowns(sys)] @set! sys.observed = ModelingToolkit.topsort_equations(observed(sys), fullunknowns) From 6d4edba320f9bde164643b5efc53f68e8cf3eb27 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Sun, 1 Jun 2025 13:37:53 +0530 Subject: [PATCH 1949/2176] test: update tests to account for new tearing --- test/bvproblem.jl | 13 +++++++----- test/dde.jl | 4 ++-- test/extensions/dynamic_optimization.jl | 24 +++++++++++------------ test/fmi/fmi.jl | 3 ++- test/initial_values.jl | 9 +++++---- test/initializationsystem.jl | 2 +- test/input_output_handling.jl | 4 ++-- test/odesystem.jl | 13 ++++++------ test/reduction.jl | 2 +- test/structural_transformation/tearing.jl | 13 ++++++++---- 10 files changed, 49 insertions(+), 38 deletions(-) diff --git a/test/bvproblem.jl b/test/bvproblem.jl index abc925ccfa..d86ba251c7 100644 --- a/test/bvproblem.jl +++ b/test/bvproblem.jl @@ -30,7 +30,7 @@ daesolvers = [Ascher2, Ascher4, Ascher6] for solver in solvers sol = solve(bvp, solver(), dt = 0.01) @test isapprox(sol.u[end], osol.u[end]; atol = 0.01) - @test sol.u[1] == [1.0, 2.0] + @test sol[[x, y], 1] == [1.0, 2.0] end # Test out of place @@ -40,7 +40,7 @@ daesolvers = [Ascher2, Ascher4, Ascher6] for solver in solvers sol = solve(bvp2, solver(), dt = 0.01) @test isapprox(sol.u[end], osol.u[end]; atol = 0.01) - @test sol.u[1] == [1.0, 2.0] + @test sol[[x, y], 1] == [1.0, 2.0] end end @@ -129,7 +129,9 @@ end sol2 = solve(bvpi2, MIRK4(), dt = 0.01) sol3 = solve(bvpi3, MIRK4(), dt = 0.01) sol4 = solve(bvpi4, MIRK4(), dt = 0.01) - @test sol1 ≈ sol2 ≈ sol3 ≈ sol4 # don't get true equality here, not sure why + @test sol1.t ≈ sol2.t ≈ sol3.t ≈ sol4.t + @test sol1.u ≈ sol2.u ≈ sol3[[x(t), y(t)]] ≈ sol4[[x(t), y(t)]] + # @test sol1 ≈ sol2 ≈ sol3 ≈ sol4 # don't get true equality here, not sure why end function test_solvers( @@ -296,7 +298,7 @@ end costfn = ModelingToolkit.generate_cost( lksys; expression = Val{false}, wrap_gfw = Val{true}) _t = tspan[2] - @test costfn(sol, prob.p, _t) ≈ (sol(0.6)[1] + 3)^2 + sol(0.3)[1]^2 + @test costfn(sol, prob.p, _t) ≈ (sol(0.6; idxs = x(t)) + 3)^2 + sol(0.3; idxs = x(t))^2 ### With a parameter @parameters t_c @@ -309,5 +311,6 @@ end sol = solve(prob, Tsit5()) costfn = ModelingToolkit.generate_cost( lksys; expression = Val{false}, wrap_gfw = Val{true}) - @test costfn(sol, prob.p, _t) ≈ log(sol(0.56)[2] + sol(0.0)[1]) - sol(0.4)[1]^2 + @test costfn(sol, prob.p, _t) ≈ + log(sol(0.56; idxs = y(t)) + sol(0.0; idxs = x(t))) - sol(0.4; idxs = x(t))^2 end diff --git a/test/dde.jl b/test/dde.jl index 211b59075c..fb92bf61eb 100644 --- a/test/dde.jl +++ b/test/dde.jl @@ -46,13 +46,13 @@ prob = DDEProblem(sys, tspan, constant_lags = [tau]) sol_mtk = solve(prob, alg, reltol = 1e-7, abstol = 1e-10) -@test sol_mtk.u[end] ≈ sol.u[end] +@test sol_mtk[[x₀, x₁, x₂(t)]][end] ≈ sol.u[end] prob2 = DDEProblem(sys, [x₀ => 1.0 - t * q1 * 10, x₁ => 1.0 - t * q1 * 10, x₂(t) => 1.0 - t * q1 * 10], tspan, constant_lags = [tau]) sol2_mtk = solve(prob2, alg, reltol = 1e-7, abstol = 1e-10) -@test sol2_mtk.u[end] ≈ sol2.u[end] +@test sol2_mtk[[x₀, x₁, x₂(t)]][end] ≈ sol2.u[end] @test_nowarn sol2_mtk[[x₀, x₁, x₂(t)]] @test_nowarn sol2_mtk[[x₀, x₁, x₂(t - 0.1)]] diff --git a/test/extensions/dynamic_optimization.jl b/test/extensions/dynamic_optimization.jl index 62553121fe..dd2368b558 100644 --- a/test/extensions/dynamic_optimization.jl +++ b/test/extensions/dynamic_optimization.jl @@ -58,22 +58,22 @@ const M = ModelingToolkit jprob = JuMPDynamicOptProblem(lksys, u0map, tspan, parammap; guesses = guess, dt = 0.01) @test JuMP.num_constraints(jprob.model) == 2 jsol = solve(jprob, Ipopt.Optimizer, constructTsitouras5, silent = true) # 12.190 s, 9.68 GiB - @test jsol.sol(0.6)[1] ≈ 3.5 - @test jsol.sol(0.3)[1] ≈ 7.0 + @test jsol.sol(0.6; idxs = x(t)) ≈ 3.5 + @test jsol.sol(0.3; idxs = x(t)) ≈ 7.0 cprob = CasADiDynamicOptProblem( lksys, u0map, tspan, parammap; guesses = guess, dt = 0.01) csol = solve(cprob, "ipopt", constructTsitouras5, silent = true) - @test csol.sol(0.6)[1] ≈ 3.5 - @test csol.sol(0.3)[1] ≈ 7.0 + @test csol.sol(0.6; idxs = x(t)) ≈ 3.5 + @test csol.sol(0.3; idxs = x(t)) ≈ 7.0 iprob = InfiniteOptDynamicOptProblem( lksys, u0map, tspan, parammap; guesses = guess, dt = 0.01) isol = solve(iprob, Ipopt.Optimizer, derivative_method = InfiniteOpt.OrthogonalCollocation(3), silent = true) # 48.564 ms, 9.58 MiB sol = isol.sol - @test sol(0.6)[1] ≈ 3.5 - @test sol(0.3)[1] ≈ 7.0 + @test sol(0.6; idxs = x(t)) ≈ 3.5 + @test sol(0.3; idxs = x(t)) ≈ 7.0 # Test whole-interval constraints constr = [x(t) ≳ 1, y(t) ≳ 1] @@ -128,14 +128,14 @@ end # Linear systems have bang-bang controls @test is_bangbang(jsol.input_sol, [-1.0], [1.0]) # Test reached final position. - @test ≈(jsol.sol.u[end][2], 0.25, rtol = 1e-5) + @test ≈(jsol.sol[x(t)][end], 0.25, rtol = 1e-5) cprob = CasADiDynamicOptProblem(block, u0map, tspan, parammap; dt = 0.01) csol = solve(cprob, "ipopt", constructVerner8, silent = true) # Linear systems have bang-bang controls @test is_bangbang(csol.input_sol, [-1.0], [1.0]) # Test reached final position. - @test ≈(csol.sol.u[end][2], 0.25, rtol = 1e-5) + @test ≈(csol.sol[x(t)][end], 0.25, rtol = 1e-5) # Test dynamics @parameters (u_interp::ConstantInterpolation)(..) @@ -149,7 +149,7 @@ end iprob = InfiniteOptDynamicOptProblem(block, u0map, tspan, parammap; dt = 0.01) isol = solve(iprob, Ipopt.Optimizer; silent = true) @test is_bangbang(isol.input_sol, [-1.0], [1.0]) - @test ≈(isol.sol.u[end][2], 0.25, rtol = 1e-5) + @test ≈(isol.sol[x(t)][end], 0.25, rtol = 1e-5) osol = solve(oprob, ImplicitEuler(); dt = 0.01, adaptive = false) @test ≈(isol.sol.u, osol.u, rtol = 0.05) @@ -220,16 +220,16 @@ end Tₘ => 3.5 * g₀ * m₀, T(t) => 0.0, h₀ => 1, m_c => 0.6] jprob = JuMPDynamicOptProblem(rocket, u0map, (ts, te), pmap; dt = 0.001, cse = false) jsol = solve(jprob, Ipopt.Optimizer, constructRadauIIA5, silent = true) - @test jsol.sol.u[end][1] > 1.012 + @test jsol.sol[h(t)][end] > 1.012 cprob = CasADiDynamicOptProblem(rocket, u0map, (ts, te), pmap; dt = 0.001, cse = false) csol = solve(cprob, "ipopt"; silent = true) - @test csol.sol.u[end][1] > 1.012 + @test csol.sol[h(t)][end] > 1.012 iprob = InfiniteOptDynamicOptProblem( rocket, u0map, (ts, te), pmap; dt = 0.001) isol = solve(iprob, Ipopt.Optimizer, silent = true) - @test isol.sol.u[end][1] > 1.012 + @test isol.sol[h(t)][end] > 1.012 # Test solution @parameters (T_interp::CubicSpline)(..) diff --git a/test/fmi/fmi.jl b/test/fmi/fmi.jl index e0eaf373d0..85e1830650 100644 --- a/test/fmi/fmi.jl +++ b/test/fmi/fmi.jl @@ -170,7 +170,8 @@ end sol = solve(prob, Rodas5P(autodiff = false), abstol = 1e-8, reltol = 1e-8) @test SciMLBase.successful_retcode(sol) - @test truesol(sol.t; idxs = [truesys.a, truesys.b, truesys.c]).u≈sol.u rtol=4e-2 + @test truesol(sol.t; + idxs = [truesys.a, truesys.b, truesys.c]).u≈sol[[sys.a, sys.b, sys.c]] rtol=4e-2 # sys.adder.c is a discrete variable @test truesol(sol.t; idxs = truesys.adder.c).u≈sol(sol.t; idxs = sys.adder.c).u rtol=4e-2 end diff --git a/test/initial_values.jl b/test/initial_values.jl index d15f30c280..1b32dc7b51 100644 --- a/test/initial_values.jl +++ b/test/initial_values.jl @@ -8,9 +8,10 @@ using SymbolicIndexingInterface @variables x(t)[1:3]=[1.0, 2.0, 3.0] y(t) z(t)[1:2] @mtkcompile sys=System([D(x) ~ t * x], t) simplify=false -@test get_u0(sys, []) == [1.0, 2.0, 3.0] -@test get_u0(sys, [x => [2.0, 3.0, 4.0]]) == [2.0, 3.0, 4.0] -@test get_u0(sys, [x[1] => 2.0, x[2] => 3.0, x[3] => 4.0]) == [2.0, 3.0, 4.0] +reorderer = getsym(sys, x) +@test reorderer(get_u0(sys, [])) == [1.0, 2.0, 3.0] +@test reorderer(get_u0(sys, [x => [2.0, 3.0, 4.0]])) == [2.0, 3.0, 4.0] +@test reorderer(get_u0(sys, [x[1] => 2.0, x[2] => 3.0, x[3] => 4.0])) == [2.0, 3.0, 4.0] @test get_u0(sys, [2.0, 3.0, 4.0]) == [2.0, 3.0, 4.0] @mtkcompile sys=System([ @@ -182,7 +183,7 @@ end prob = ODEProblem(sys, [], (0.0, 1.0)) sol = solve(prob) @test SciMLBase.successful_retcode(sol) - @test sol.u[1] ≈ [1.0, 1.0, 0.5, 0.5] + @test sol[x, 1] ≈ [1.0, 1.0, 0.5, 0.5] end @testset "Missing/cyclic guesses throws error" begin diff --git a/test/initializationsystem.jl b/test/initializationsystem.jl index 1e83ec579f..fc01e9e2e6 100644 --- a/test/initializationsystem.jl +++ b/test/initializationsystem.jl @@ -459,7 +459,7 @@ sol = solve(prob, Tsit5()) prob = ODEProblem(simpsys, [z => 1.0, y => 1.0], tspan, guesses = [x => 2.0]) sol = solve(prob, Tsit5()) -@test sol.u[1] == [0.0, 1.0] +@test sol[[x, y], 1] == [0.0, 1.0] # This should warn, but logging tests can't be marked as broken @test_logs prob = ODEProblem(simpsys, [], tspan, guesses = [x => 2.0]) diff --git a/test/input_output_handling.jl b/test/input_output_handling.jl index c295546248..f30949ed1b 100644 --- a/test/input_output_handling.jl +++ b/test/input_output_handling.jl @@ -369,7 +369,7 @@ eqs = [D(y₁) ~ -k₁ * y₁ + k₃ * y₂ * y₃ + u1 m_inputs = [u[1], u[2]] m_outputs = [y₂] sys_simp = mtkcompile(sys, inputs = m_inputs, outputs = m_outputs) -@test isequal(unknowns(sys_simp), collect(x[1:2])) +@test issetequal(unknowns(sys_simp), collect(x[1:2])) @test length(inputs(sys_simp)) == 2 # https://github.com/SciML/ModelingToolkit.jl/issues/1577 @@ -417,7 +417,7 @@ matrices, ssys = linearize(augmented_sys, ], outs; op = [augmented_sys.u => 0.0, augmented_sys.input.u[2] => 0.0, augmented_sys.d => 0.0]) matrices = ModelingToolkit.reorder_unknowns( - matrices, unknowns(ssys), [ssys.x[2], ssys.integrator.x[1], ssys.x[1]]) + matrices, unknowns(ssys), [ssys.x[1], ssys.x[2], ssys.integrator.x[1]]) @test matrices.A ≈ [A [1; 0]; zeros(1, 2) -0.001] @test matrices.B == I @test matrices.C == [C zeros(2)] diff --git a/test/odesystem.jl b/test/odesystem.jl index 58315f0cff..fed0ac28fe 100644 --- a/test/odesystem.jl +++ b/test/odesystem.jl @@ -568,10 +568,10 @@ let @named sys = System(eqs, t, vcat(x, [y]), [k], defaults = Dict(x .=> 0)) sys = mtkcompile(sys) - u0 = unknowns(sys) .=> [0.5, 0] - du0 = D.(unknowns(sys)) .=> 0.0 + u0 = x .=> [0.5, 0] + du0 = D.(x) .=> 0.0 prob = DAEProblem(sys, [du0; u0], (0, 50)) - @test prob.u0 ≈ [0.5, 0.0] + @test prob[x] ≈ [0.5, 0.0] @test prob.du0 ≈ [0.0, 0.0] @test prob.p isa MTKParameters @test prob.ps[k] ≈ 1 @@ -581,7 +581,8 @@ let prob = DAEProblem(sys, [D(y) => 0, D(x[1]) => 0, D(x[2]) => 0, x[1] => 0.5], (0, 50)) - @test prob.u0 ≈ [0.5, 0] + + @test prob[x] ≈ [0.5, 0] @test prob.du0 ≈ [0, 0] @test prob.p isa MTKParameters @test prob.ps[k] ≈ 1 @@ -590,7 +591,7 @@ let prob = DAEProblem(sys, [D(y) => 0, D(x[1]) => 0, D(x[2]) => 0, x[1] => 0.5, k => 2], (0, 50)) - @test prob.u0 ≈ [0.5, 0] + @test prob[x] ≈ [0.5, 0] @test prob.du0 ≈ [0, 0] @test prob.p isa MTKParameters @test prob.ps[k] ≈ 2 @@ -769,7 +770,7 @@ let @named sys4 = System([D(x) ~ -y; D(y) ~ 1 + pp * y + x], t) sys4s = mtkcompile(sys4) prob = ODEProblem(sys4s, [x => 1.0, D(x) => 1.0], (0, 1.0)) - @test string.(unknowns(prob.f.sys)) == ["x(t)", "y(t)"] + @test issetequal(string.(unknowns(prob.f.sys)), ["x(t)", "y(t)"]) @test string.(parameters(prob.f.sys)) == ["pp"] @test string.(independent_variables(prob.f.sys)) == ["t"] end diff --git a/test/reduction.jl b/test/reduction.jl index 43fef0fb63..97d360fd67 100644 --- a/test/reduction.jl +++ b/test/reduction.jl @@ -245,7 +245,7 @@ eqs = [D(x) ~ x sys = mtkcompile(model) Js = ModelingToolkit.jacobian_sparsity(sys) @test size(Js) == (3, 3) -@test Js == Diagonal([1, 1, 0]) +@test Js == Diagonal([0, 1, 1]) # MWE for #1722 vars = @variables a(t) w(t) phi(t) diff --git a/test/structural_transformation/tearing.jl b/test/structural_transformation/tearing.jl index 7f18cbb703..4025f7a298 100644 --- a/test/structural_transformation/tearing.jl +++ b/test/structural_transformation/tearing.jl @@ -5,6 +5,7 @@ using ModelingToolkit.StructuralTransformations: SystemStructure, find_solvables using NonlinearSolve using LinearAlgebra using UnPack +using SymbolicIndexingInterface using ModelingToolkit: t_nounits as t, D_nounits as D ### ### Nonlinear system @@ -147,9 +148,10 @@ eqs = [D(x) ~ z * h 0 ~ sin(z) + y - p * t] @named daesys = System(eqs, t) newdaesys = mtkcompile(daesys) -@test equations(newdaesys) == [D(x) ~ h * z; 0 ~ y + sin(z) - p * t] -@test equations(tearing_substitution(newdaesys)) == [D(x) ~ h * z; 0 ~ x + sin(z) - p * t] -@test isequal(unknowns(newdaesys), [x, z]) +@test issetequal(equations(newdaesys), [D(x) ~ h * z; 0 ~ y + sin(z) - p * t]) +@test issetequal( + equations(tearing_substitution(newdaesys)), [D(x) ~ h * z; 0 ~ x + sin(z) - p * t]) +@test issetequal(unknowns(newdaesys), [x, z]) prob = ODEProblem(newdaesys, [x => 1.0, z => -0.5π, p => 0.2], (0, 1.0)) du = [0.0, 0.0]; u = [1.0, -0.5π]; @@ -157,7 +159,10 @@ pr = prob.p; tt = 0.1; @test (@ballocated $(prob.f)($du, $u, $pr, $tt)) == 0 prob.f(du, u, pr, tt) -@test du≈[u[2], u[1] + sin(u[2]) - prob.ps[p] * tt] atol=1e-5 +xgetter = getsym(prob, x) +zgetter = getsym(prob, z) +@test xgetter(du)≈zgetter(u) atol=1e-5 +@test zgetter(du)≈xgetter(u) + sin(zgetter(u)) - prob.ps[p] * tt atol=1e-5 # test the initial guess is respected @named sys = System(eqs, t, defaults = Dict(z => NaN)) From bea3914be5f8afb6f5bb3d8d1ce4b88c72ecc66c Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 2 Jun 2025 11:46:59 +0530 Subject: [PATCH 1950/2176] test: optimization test is no longer broken --- test/optimizationsystem.jl | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/test/optimizationsystem.jl b/test/optimizationsystem.jl index 0ff85a518d..ad77dbc416 100644 --- a/test/optimizationsystem.jl +++ b/test/optimizationsystem.jl @@ -109,11 +109,9 @@ end x0 = zeros(2) p = [1.0, 100.0] f = OptimizationFunction(rosenbrock, Optimization.AutoSymbolics()) - @test_broken begin - prob = OptimizationProblem(f, x0, p) - sol = solve(prob, Newton()) - @test sol.u ≈ [1.0, 1.0] - end + prob = OptimizationProblem(f, x0, p) + sol = solve(prob, Newton()) + @test sol.u ≈ [1.0, 1.0] end # issue #819 From a6ced9a1ee2b2d9c4f479069b78cfa3f639d05f6 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 2 Jun 2025 13:16:48 +0530 Subject: [PATCH 1951/2176] refactor: use SCC information calculated during `mtkcompile` in `SCCNonlinearProblem` --- src/problems/sccnonlinearproblem.jl | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/src/problems/sccnonlinearproblem.jl b/src/problems/sccnonlinearproblem.jl index 3e00170ad9..d2afb7b315 100644 --- a/src/problems/sccnonlinearproblem.jl +++ b/src/problems/sccnonlinearproblem.jl @@ -80,21 +80,32 @@ function SciMLBase.SCCNonlinearProblem{iip}(sys::System, op; eval_expression = f end ts = get_tearing_state(sys) - var_eq_matching, var_sccs = StructuralTransformations.algebraic_variables_scc(ts) + sched = get_schedule(sys) + if sched === nothing + @warn "System is simplified but does not have a schedule. This should not happen." + var_eq_matching, var_sccs = StructuralTransformations.algebraic_variables_scc(ts) + condensed_graph = MatchedCondensationGraph( + DiCMOBiGraph{true}(complete(ts.structure.graph), + complete(var_eq_matching)), + var_sccs) + toporder = topological_sort_by_dfs(condensed_graph) + var_sccs = var_sccs[toporder] + eq_sccs = map(Base.Fix1(getindex, var_eq_matching), var_sccs) + else + var_sccs = sched.var_sccs + # Equations are already in the order of SCCs + eq_sccs = length.(var_sccs) + cumsum!(eq_sccs, eq_sccs) + eq_sccs = map(enumerate(eq_sccs)) do (i, lasti) + i == 1 ? (1:lasti) : ((eq_sccs[i - 1] + 1):lasti) + end + end if length(var_sccs) == 1 return NonlinearProblem{iip}( sys, op; eval_expression, eval_module, kwargs...) end - condensed_graph = MatchedCondensationGraph( - DiCMOBiGraph{true}(complete(ts.structure.graph), - complete(var_eq_matching)), - var_sccs) - toporder = topological_sort_by_dfs(condensed_graph) - var_sccs = var_sccs[toporder] - eq_sccs = map(Base.Fix1(getindex, var_eq_matching), var_sccs) - dvs = unknowns(sys) ps = parameters(sys) eqs = equations(sys) From be49462e0547a7fdf38e90f7fa79b9286817ebdf Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 2 Jun 2025 13:17:01 +0530 Subject: [PATCH 1952/2176] test: test SCC sorted order --- test/scc_nonlinear_problem.jl | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/test/scc_nonlinear_problem.jl b/test/scc_nonlinear_problem.jl index ded9852670..fad30dbcea 100644 --- a/test/scc_nonlinear_problem.jl +++ b/test/scc_nonlinear_problem.jl @@ -39,6 +39,9 @@ using ModelingToolkit: t_nounits as t, D_nounits as D @test prob.u0 isa SVector @test !SciMLBase.isinplace(prob) end + + # Test BLT sorted + @test istril(StructuralTransformations.sorted_incidence_matrix(model), 2) end @testset "With parameters" begin @@ -90,6 +93,9 @@ end sccsol = solve(sccprob, SimpleNewtonRaphson(); abstol = 1e-9) @test SciMLBase.successful_retcode(sccsol) @test norm(sccsol.resid) < norm(sol.resid) + + # Test BLT sorted + @test istril(StructuralTransformations.sorted_incidence_matrix(sys), 1) end @testset "Transistor amplifier" begin @@ -149,6 +155,9 @@ end sccsol = solve(sccprob, NewtonRaphson(); abstol = 1e-12) @test sol.u≈sccsol.u atol=1e-10 + + # Test BLT sorted + @test istril(StructuralTransformations.sorted_incidence_matrix(sys), 1) end @testset "Expression caching" begin From 1e564207241814265fc9870a100625f382f84a60 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 2 Jun 2025 14:50:09 +0530 Subject: [PATCH 1953/2176] feat: implement `sorted_incidence_matrix(sys)` --- .../StructuralTransformations.jl | 2 +- src/structural_transformation/utils.jl | 21 +++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/structural_transformation/StructuralTransformations.jl b/src/structural_transformation/StructuralTransformations.jl index df30f7d2d2..eeb480e0f7 100644 --- a/src/structural_transformation/StructuralTransformations.jl +++ b/src/structural_transformation/StructuralTransformations.jl @@ -26,7 +26,7 @@ using ModelingToolkit: System, AbstractSystem, var_from_nested_derivative, Diffe filter_kwargs, lower_varname_with_unit, lower_shift_varname_with_unit, setio, SparseMatrixCLIL, get_fullvars, has_equations, observed, - Schedule, schedule + Schedule, schedule, iscomplete, get_schedule using ModelingToolkit.BipartiteGraphs import .BipartiteGraphs: invview, complete diff --git a/src/structural_transformation/utils.jl b/src/structural_transformation/utils.jl index e900764698..50f9aaf887 100644 --- a/src/structural_transformation/utils.jl +++ b/src/structural_transformation/utils.jl @@ -205,6 +205,27 @@ function sorted_incidence_matrix(ts::TransformationState, val = true; only_algeq sparse(I, J, val, nsrcs(graph), ndsts(graph)) end +""" + $(TYPEDSIGNATURES) + +Obtain the incidence matrix of the system sorted by the SCCs. Requires that the system is +simplified and has a `schedule`. +""" +function sorted_incidence_matrix(sys::AbstractSystem) + if !iscomplete(sys) || get_tearing_state(sys) === nothing || + get_schedule(sys) === nothing + error("A simplified `System` is required. Call `mtkcompile` on the system before creating an `SCCNonlinearProblem`.") + end + sched = get_schedule(sys) + var_sccs = sched.var_sccs + + ts = get_tearing_state(sys) + imat = Graphs.incidence_matrix(ts.structure.graph) + buffer = similar(imat) + permute!(buffer, imat, 1:size(imat, 2), reduce(vcat, var_sccs)) + buffer +end + ### ### Structural and symbolic utilities ### From 26437e211b80588e5a7b2b6bcb9ed6584e0853da Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 2 Jun 2025 17:12:18 +0530 Subject: [PATCH 1954/2176] test: fix undefined import in tests --- test/symbolic_events.jl | 1 - 1 file changed, 1 deletion(-) diff --git a/test/symbolic_events.jl b/test/symbolic_events.jl index 2c6253e70d..5968976653 100644 --- a/test/symbolic_events.jl +++ b/test/symbolic_events.jl @@ -2,7 +2,6 @@ using ModelingToolkit, OrdinaryDiffEq, StochasticDiffEq, JumpProcesses, Test using SciMLStructures: canonicalize, Discrete using ModelingToolkit: SymbolicContinuousCallback, SymbolicDiscreteCallback, - get_callback, t_nounits as t, D_nounits as D, affects, affect_negs, system, observed, AffectSystem From 3d83e08c217a85a748e4558c54d89d64de50e749 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 29 May 2025 18:49:46 +0530 Subject: [PATCH 1955/2176] fix: fix type promotion in `late_binding_update_u0_p` with non-dual types --- src/systems/nonlinear/initializesystem.jl | 46 ++++++++++++++++------- test/initial_values.jl | 10 +++++ 2 files changed, 43 insertions(+), 13 deletions(-) diff --git a/src/systems/nonlinear/initializesystem.jl b/src/systems/nonlinear/initializesystem.jl index beb2719c2c..b1ca187e95 100644 --- a/src/systems/nonlinear/initializesystem.jl +++ b/src/systems/nonlinear/initializesystem.jl @@ -638,24 +638,44 @@ function SciMLBase.remake_initialization_data( return SciMLBase.remake_initialization_data(sys, odefn, newu0, t0, newp, newu0, newp) end -function promote_u0_p(u0, p::MTKParameters, t0) - u0 = DiffEqBase.promote_u0(u0, p.tunable, t0) - u0 = DiffEqBase.promote_u0(u0, p.initials, t0) +promote_type_with_nothing(::Type{T}, ::Nothing) where {T} = T +promote_type_with_nothing(::Type{T}, ::SizedVector{0}) where {T} = T +function promote_type_with_nothing(::Type{T}, ::AbstractArray{T2}) where {T, T2} + promote_type(T, T2) +end +function promote_type_with_nothing(::Type{T}, p::MTKParameters) where {T} + promote_type_with_nothing(promote_type_with_nothing(T, p.tunable), p.initials) +end - if !isempty(p.tunable) - tunables = DiffEqBase.promote_u0(p.tunable, u0, t0) - p = SciMLStructures.replace(SciMLStructures.Tunable(), p, tunables) - end - if !isempty(p.initials) - initials = DiffEqBase.promote_u0(p.initials, u0, t0) - p = SciMLStructures.replace(SciMLStructures.Initials(), p, initials) +promote_with_nothing(::Type, ::Nothing) = nothing +promote_with_nothing(::Type, x::SizedVector{0}) = x +promote_with_nothing(::Type{T}, x::AbstractArray{T}) where {T} = x +function promote_with_nothing(::Type{T}, x::AbstractArray{T2}) where {T, T2} + if ArrayInterface.ismutable(x) + y = similar(x, T) + copyto!(y, x) + return y + else + yT = similar_type(x, T) + return yT(x) end - - return u0, p +end +function promote_with_nothing(::Type{T}, p::MTKParameters) where {T} + tunables = promote_with_nothing(T, p.tunable) + p = SciMLStructures.replace(SciMLStructures.Tunable(), p, tunables) + initials = promote_with_nothing(T, p.initials) + p = SciMLStructures.replace(SciMLStructures.Initials(), p, initials) + return p end function promote_u0_p(u0, p, t0) - return DiffEqBase.promote_u0(u0, p, t0), DiffEqBase.promote_u0(p, u0, t0) + T = Union{} + T = promote_type_with_nothing(T, u0) + T = promote_type_with_nothing(T, p) + + u0 = promote_with_nothing(T, u0) + p = promote_with_nothing(T, p) + return u0, p end function SciMLBase.late_binding_update_u0_p( diff --git a/test/initial_values.jl b/test/initial_values.jl index d15f30c280..a2fe0075dc 100644 --- a/test/initial_values.jl +++ b/test/initial_values.jl @@ -345,3 +345,13 @@ end @test state_values(initdata.initializeprob) isa SVector @test parameter_values(initdata.initializeprob) isa SVector end + +@testset "Type promotion of `p` works with non-dual types" begin + @variables x(t) y(t) + @mtkcompile sys = System([D(x) ~ x + y, x^3 + y^3 ~ 5], t; guesses = [y => 1.0]) + prob = ODEProblem(sys, [x => 1.0], (0.0, 1.0)) + prob2 = remake(prob; u0 = BigFloat.(prob.u0)) + @test prob2.p.initials isa Vector{BigFloat} + sol = solve(prob2) + @test SciMLBase.successful_retcode(sol) +end From 5a4bbc14f6ede9d77761285fac4960976a7d3a40 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 3 Jun 2025 16:13:48 +0530 Subject: [PATCH 1956/2176] feat: allow specifying `problem_type` using system metadata --- src/problems/nonlinearproblem.jl | 3 ++- src/problems/odeproblem.jl | 3 ++- src/systems/system.jl | 10 ++++++++++ 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/problems/nonlinearproblem.jl b/src/problems/nonlinearproblem.jl index 9986ee665b..1ed7cda750 100644 --- a/src/problems/nonlinearproblem.jl +++ b/src/problems/nonlinearproblem.jl @@ -67,7 +67,8 @@ end check_length, check_compatibility, expression, kwargs...) kwargs = process_kwargs(sys; kwargs...) - args = (; f, u0, p, ptype = StandardNonlinearProblem()) + ptype = getmetadata(sys, ProblemTypeCtx, StandardNonlinearProblem()) + args = (; f, u0, p, ptype) return maybe_codegen_scimlproblem(expression, NonlinearProblem{iip}, args; kwargs...) end diff --git a/src/problems/odeproblem.jl b/src/problems/odeproblem.jl index e78ff00525..6726322907 100644 --- a/src/problems/odeproblem.jl +++ b/src/problems/odeproblem.jl @@ -77,7 +77,8 @@ end kwargs = process_kwargs( sys; expression, callback, eval_expression, eval_module, kwargs...) - args = (; f, u0, tspan, p, ptype = StandardODEProblem()) + ptype = getmetadata(sys, ProblemTypeCtx, StandardODEProblem()) + args = (; f, u0, tspan, p, ptype) maybe_codegen_scimlproblem(expression, ODEProblem{iip}, args; kwargs...) end diff --git a/src/systems/system.jl b/src/systems/system.jl index b30c046edb..cd8d711742 100644 --- a/src/systems/system.jl +++ b/src/systems/system.jl @@ -807,6 +807,16 @@ function SymbolicUtils.setmetadata(sys::AbstractSystem, k::DataType, v) @set sys.metadata = meta end +""" + $(TYPEDSIGNATURES) + +Metadata key for systems containing the `problem_type` to be passed to the problem +constructor, where applicable. For example, if `getmetadata(sys, ProblemTypeCtx, nothing)` +is `CustomType()` then `ODEProblem(sys, ...).problem_type` will be `CustomType()` instead +of `StandardODEProblem`. +""" +struct ProblemTypeCtx end + """ $(TYPEDSIGNATURES) """ From 9c47eaa08ba5a52da1211fb71b909cd276ee3748 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 3 Jun 2025 16:14:00 +0530 Subject: [PATCH 1957/2176] test: test application of `problem_type` via system metadata --- test/nonlinearsystem.jl | 8 ++++++++ test/odesystem.jl | 8 ++++++++ 2 files changed, 16 insertions(+) diff --git a/test/nonlinearsystem.jl b/test/nonlinearsystem.jl index 4bacd6a50d..73cbbe9639 100644 --- a/test/nonlinearsystem.jl +++ b/test/nonlinearsystem.jl @@ -434,3 +434,11 @@ end @test resid == [0.0] @test resid isa SVector end + +@testset "`ProblemTypeCtx`" begin + @variables x + @mtkcompile sys = System( + [0 ~ x^2 - 4x + 4]; metadata = [ModelingToolkit.ProblemTypeCtx => "A"]) + prob = NonlinearProblem(sys, [x => 1.0]) + @test prob.problem_type == "A" +end diff --git a/test/odesystem.jl b/test/odesystem.jl index 073dbde5b2..c510b98429 100644 --- a/test/odesystem.jl +++ b/test/odesystem.jl @@ -1566,3 +1566,11 @@ end @test !process_running(proc) kill(proc, Base.SIGKILL) end + +@testset "`ProblemTypeCtx`" begin + @variables x(t) + @mtkcompile sys = System( + [D(x) ~ x], t; metadata = [ModelingToolkit.ProblemTypeCtx => "A"]) + prob = ODEProblem(sys, [x => 1.0], (0.0, 1.0)) + @test prob.problem_type == "A" +end From 3ca1c6321aeee6269303217b5ce2427a755e6d31 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 3 Jun 2025 18:19:34 +0530 Subject: [PATCH 1958/2176] refactor: do not build initialization for discrete systems --- src/problems/discreteproblem.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/problems/discreteproblem.jl b/src/problems/discreteproblem.jl index ff0500ccf3..8e1abc46e4 100644 --- a/src/problems/discreteproblem.jl +++ b/src/problems/discreteproblem.jl @@ -47,7 +47,7 @@ end add_toterms!(u0map; replace = true) f, u0, p = process_SciMLProblem(DiscreteFunction{iip, spec}, sys, op; t = tspan !== nothing ? tspan[1] : tspan, check_compatibility, expression, - kwargs...) + build_initializeprob = false, kwargs...) if expression == Val{true} u0 = :(f($u0, p, tspan[1])) From 714d82376f09b941d69e04ffe0a54225d58bf2c4 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 3 Jun 2025 18:19:43 +0530 Subject: [PATCH 1959/2176] test: mark appropriate discrete system tests as broken --- test/discrete_system.jl | 161 +++++++++++++++++++++------------------- 1 file changed, 83 insertions(+), 78 deletions(-) diff --git a/test/discrete_system.jl b/test/discrete_system.jl index 7a33d89537..ccebe5d346 100644 --- a/test/discrete_system.jl +++ b/test/discrete_system.jl @@ -40,7 +40,8 @@ u = ModelingToolkit.varmap_to_vars( Dict([S(k - 1) => 1, I(k - 1) => 2, R(k - 1) => 3]), unknowns(syss)) p = MTKParameters(syss, [c, nsteps, δt, β, γ] .=> collect(1:5)) df.f(du, u, p, 0) -reorderer = getu(syss, [S, I, R]) +@test_broken getu(syss, [S, I, R]) +reorderer = getu(syss, [S(k - 1), I(k - 1), R(k - 1)]) @test reorderer(du) ≈ [0.01831563888873422, 0.9816849729159067, 4.999999388195359] # oop @@ -57,7 +58,8 @@ prob_map = DiscreteProblem(syss, [u0; p], tspan) # Solution using OrdinaryDiffEq sol_map = solve(prob_map, FunctionMap()); -@test sol_map[S] isa Vector +@test_broken sol_map[S] isa Vector +@test sol_map[S(k - 1)] isa Vector # Using defaults constructor @parameters c=10.0 nsteps=400 δt=0.1 β=0.05 γ=0.25 @@ -73,18 +75,19 @@ eqs2 = [S ~ S(k - 1) - infection2, R2 ~ R] @mtkcompile sys = System( - eqs2, t, [S, I, R, R2], [c, nsteps, δt, β, γ]; controls = [β, γ], tspan) + eqs2, t, [S, I, R, R2], [c, nsteps, δt, β, γ]) @test ModelingToolkit.defaults(sys) != Dict() -prob_map2 = DiscreteProblem(sys) +@test_broken DiscreteProblem(sys, [], tspan) +prob_map2 = DiscreteProblem(sys, [S(k - 1) => S, I(k - 1) => I, R(k - 1) => R], tspan) sol_map2 = solve(prob_map2, FunctionMap()); @test sol_map.u ≈ sol_map2.u for p in parameters(sys) @test sol_map.prob.ps[p] ≈ sol_map2.prob.ps[p] end -@test_throws Any sol_map2[R2] -@test sol_map2[R2(k + 1)][begin:(end - 1)] == sol_map2[R][(begin + 1):end] +@test sol_map2[R2][begin:(end - 1)] == sol_map2[R(k - 1)][(begin + 1):end] +@test_broken sol_map2[R2(k + 1)][begin:(end - 1)] == sol_map2[R][(begin + 1):end] # Direct Implementation function sir_map!(u_diff, u, p, t) @@ -100,12 +103,14 @@ function sir_map!(u_diff, u, p, t) end nothing end; -u0 = prob_map2[[S, I, R]]; +@test_broken prob_map2[[S, I, R]] +u0 = prob_map2[[S(k - 1), I(k - 1), R(k - 1)]]; p = [0.05, 10.0, 0.25, 0.1]; prob_map = DiscreteProblem(sir_map!, u0, tspan, p); sol_map2 = solve(prob_map, FunctionMap()); -@test reduce(hcat, sol_map[[S, I, R]]) ≈ Array(sol_map2) +@test_broken reduce(hcat, sol_map[[S, I, R]]) ≈ Array(sol_map2) +@test reduce(hcat, sol_map[[S(k - 1), I(k - 1), R(k - 1)]]) ≈ Array(sol_map2) # Delayed difference equation # @variables x(..) y(..) z(t) @@ -245,79 +250,78 @@ end @test_nowarn @mtkcompile sys = System(; buffer = ones(10)) -# Ensure discrete systems with algebraic equations throw -@variables x(t) y(t) -k = ShiftIndex(t) -@named sys = System([x ~ x^2 + y^2, y ~ x(k - 1) + y(k - 1)], t) - @testset "Passing `nothing` to `u0`" begin - @variables x(t) = 1 - k = ShiftIndex() - @mtkcompile sys = System([x(k) ~ x(k - 1) + 1], t) - prob = @test_nowarn DiscreteProblem(sys, nothing, (0.0, 1.0)) - @test_nowarn solve(prob, FunctionMap()) + @test_broken begin + @variables x(t) = 1 + k = ShiftIndex() + @mtkcompile sys = System([x(k) ~ x(k - 1) + 1], t) + prob = @test_nowarn DiscreteProblem(sys, nothing, (0.0, 1.0)) + @test_nowarn solve(prob, FunctionMap()) + end end @testset "Initialization" begin - # test that default values apply to the entire history - @variables x(t) = 1.0 - @mtkcompile de = System([x ~ x(k - 1) + x(k - 2)], t) - prob = DiscreteProblem(de, [], (0, 10)) - @test prob[x] == 2.0 - @test prob[x(k - 1)] == 1.0 - - # must provide initial conditions for history - @test_throws ErrorException DiscreteProblem(de, [x => 2.0], (0, 10)) - @test_throws ErrorException DiscreteProblem(de, [x(k + 1) => 2.0], (0, 10)) - - # initial values only affect _that timestep_, not the entire history - prob = DiscreteProblem(de, [x(k - 1) => 2.0], (0, 10)) - @test prob[x] == 3.0 - @test prob[x(k - 1)] == 2.0 - @variables xₜ₋₁(t) - @test prob[xₜ₋₁] == 2.0 - - # Test initial assignment with lowered variable - prob = DiscreteProblem(de, [xₜ₋₁(k - 1) => 4.0], (0, 10)) - @test prob[x(k - 1)] == prob[xₜ₋₁] == 1.0 - @test prob[x] == 5.0 - - # Test missing initial throws error - @variables x(t) - @mtkcompile de = System([x ~ x(k - 1) + x(k - 2) * x(k - 3)], t) - @test_throws ErrorException prob=DiscreteProblem(de, [x(k - 3) => 2.0], (0, 10)) - @test_throws ErrorException prob=DiscreteProblem( - de, [x(k - 3) => 2.0, x(k - 1) => 3.0], (0, 10)) - - # Test non-assigned initials are given default value - @variables x(t) = 2.0 - @mtkcompile de = System([x ~ x(k - 1) + x(k - 2) * x(k - 3)], t) - prob = DiscreteProblem(de, [x(k - 3) => 12.0], (0, 10)) - @test prob[x] == 26.0 - @test prob[x(k - 1)] == 2.0 - @test prob[x(k - 2)] == 2.0 - - # Elaborate test - @variables xₜ₋₂(t) zₜ₋₁(t) z(t) - eqs = [x ~ x(k - 1) + z(k - 2), - z ~ x(k - 2) * x(k - 3) - z(k - 1)^2] - @mtkcompile de = System(eqs, t) - u0 = [x(k - 1) => 3, - xₜ₋₂(k - 1) => 4, - x(k - 2) => 1, - z(k - 1) => 5, - zₜ₋₁(k - 1) => 12] - prob = DiscreteProblem(de, u0, (0, 10)) - @test prob[x] == 15 - @test prob[z] == -21 - - import ModelingToolkit: shift2term - # unknowns(de) = xₜ₋₁, x, zₜ₋₁, xₜ₋₂, z - vars = sort(ModelingToolkit.value.(unknowns(de)); by = string) - @test isequal(shift2term(Shift(t, 1)(vars[2])), vars[1]) - @test isequal(shift2term(Shift(t, 1)(vars[3])), vars[2]) - @test isequal(shift2term(Shift(t, -1)(vars[4])), vars[5]) - @test isequal(shift2term(Shift(t, -2)(vars[1])), vars[3]) + @test_broken begin + # test that default values apply to the entire history + @variables x(t) = 1.0 + @mtkcompile de = System([x ~ x(k - 1) + x(k - 2)], t) + prob = DiscreteProblem(de, [], (0, 10)) + @test prob[x] == 2.0 + @test prob[x(k - 1)] == 1.0 + + # must provide initial conditions for history + @test_throws ErrorException DiscreteProblem(de, [x => 2.0], (0, 10)) + @test_throws ErrorException DiscreteProblem(de, [x(k + 1) => 2.0], (0, 10)) + + # initial values only affect _that timestep_, not the entire history + prob = DiscreteProblem(de, [x(k - 1) => 2.0], (0, 10)) + @test prob[x] == 3.0 + @test prob[x(k - 1)] == 2.0 + @variables xₜ₋₁(t) + @test prob[xₜ₋₁] == 2.0 + + # Test initial assignment with lowered variable + prob = DiscreteProblem(de, [xₜ₋₁(k - 1) => 4.0], (0, 10)) + @test prob[x(k - 1)] == prob[xₜ₋₁] == 1.0 + @test prob[x] == 5.0 + + # Test missing initial throws error + @variables x(t) + @mtkcompile de = System([x ~ x(k - 1) + x(k - 2) * x(k - 3)], t) + @test_throws ErrorException prob=DiscreteProblem(de, [x(k - 3) => 2.0], (0, 10)) + @test_throws ErrorException prob=DiscreteProblem( + de, [x(k - 3) => 2.0, x(k - 1) => 3.0], (0, 10)) + + # Test non-assigned initials are given default value + @variables x(t) = 2.0 + @mtkcompile de = System([x ~ x(k - 1) + x(k - 2) * x(k - 3)], t) + prob = DiscreteProblem(de, [x(k - 3) => 12.0], (0, 10)) + @test prob[x] == 26.0 + @test prob[x(k - 1)] == 2.0 + @test prob[x(k - 2)] == 2.0 + + # Elaborate test + @variables xₜ₋₂(t) zₜ₋₁(t) z(t) + eqs = [x ~ x(k - 1) + z(k - 2), + z ~ x(k - 2) * x(k - 3) - z(k - 1)^2] + @mtkcompile de = System(eqs, t) + u0 = [x(k - 1) => 3, + xₜ₋₂(k - 1) => 4, + x(k - 2) => 1, + z(k - 1) => 5, + zₜ₋₁(k - 1) => 12] + prob = DiscreteProblem(de, u0, (0, 10)) + @test prob[x] == 15 + @test prob[z] == -21 + + import ModelingToolkit: shift2term + # unknowns(de) = xₜ₋₁, x, zₜ₋₁, xₜ₋₂, z + vars = sort(ModelingToolkit.value.(unknowns(de)); by = string) + @test isequal(shift2term(Shift(t, 1)(vars[2])), vars[1]) + @test isequal(shift2term(Shift(t, 1)(vars[3])), vars[2]) + @test isequal(shift2term(Shift(t, -1)(vars[4])), vars[5]) + @test isequal(shift2term(Shift(t, -2)(vars[1])), vars[3]) + end end @testset "Shifted array variables" begin @@ -335,5 +339,6 @@ end (0, 4)) @test all(isone, prob.u0) sol = solve(prob, FunctionMap()) - @test sol[[x..., y...], end] == 8ones(4) + @test_broken sol[[x..., y...], end] + @test sol[[x(k - 1)..., y(k - 1)...], end] == 8ones(4) end From f382bbbbc35fbc3adfb8802aa0362e299e0e763f Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 3 Jun 2025 18:27:55 +0530 Subject: [PATCH 1960/2176] test: update AD tests, mark some as broken --- test/extensions/ad.jl | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/test/extensions/ad.jl b/test/extensions/ad.jl index d6b0cd67e3..2efc9781cf 100644 --- a/test/extensions/ad.jl +++ b/test/extensions/ad.jl @@ -22,16 +22,18 @@ ps = [p => zeros(3, 3), q => 1.0] tspan = (0.0, 10.0) @mtkcompile sys = System(eqs, t) -prob = ODEProblem(sys, u0, tspan, ps) +prob = ODEProblem(sys, [u0; ps], tspan) sol = solve(prob, Tsit5()) mtkparams = parameter_values(prob) new_p = rand(14) -gs = gradient(new_p) do new_p - new_params = SciMLStructures.replace(SciMLStructures.Tunable(), mtkparams, new_p) - new_prob = remake(prob, p = new_params) - new_sol = solve(new_prob, Tsit5()) - sum(new_sol) +@test_broken begin + gs = gradient(new_p) do new_p + new_params = SciMLStructures.replace(SciMLStructures.Tunable(), mtkparams, new_p) + new_prob = remake(prob, p = new_params) + new_sol = solve(new_prob, Tsit5()) + sum(new_sol) + end end @testset "Issue#2997" begin @@ -50,7 +52,7 @@ end sys = mtkcompile(sys) function x_at_0(θ) - prob = ODEProblem(sys, [sys.x => 1.0], (0.0, 1.0), [sys.ργ0 => θ[1], sys.h => θ[2]]) + prob = ODEProblem(sys, [sys.x => 1.0, sys.ργ0 => θ[1], sys.h => θ[2]], (0.0, 1.0)) return prob.u0[1] end @@ -61,7 +63,7 @@ end @named sys = System( Equation[], t, [], [a, b, c, d, e, f, g, h], continuous_events = [ModelingToolkit.SymbolicContinuousCallback( - [a ~ 0] => [c ~ 0], discrete_parameters = c)]) + [a ~ 0] => [c ~ 0], discrete_parameters = c, iv = t)]) sys = complete(sys) ivs = Dict(c => 3a, b => ones(3), a => 1.0, d => 4, e => [5.0, 6.0, 7.0], @@ -116,7 +118,7 @@ fwd, back = ChainRulesCore.rrule(remake_buffer, sys, ps, idxs, vals) sys = mtkcompile(sys) # Find initial throw velocity that reaches exactly 10 m after 1 s - dprob0 = ODEProblem(sys, [D(y) => NaN], (0.0, 1.0), []; guesses = [y => 0.0]) + dprob0 = ODEProblem(sys, [D(y) => NaN], (0.0, 1.0); guesses = [y => 0.0]) function f(ics, _) dprob = remake(dprob0, u0 = Dict(D(y) => ics[1])) dsol = solve(dprob, Tsit5()) From e0bb5d20c4a7ff7615b62442465fd6093dfb9648 Mon Sep 17 00:00:00 2001 From: fchen121 Date: Tue, 3 Jun 2025 15:54:55 -0400 Subject: [PATCH 1961/2176] Implement and fix change of variable for ODE --- Project.toml | 5 + src/systems/diffeqs/basic_transformations.jl | 36 +++++--- test/changeofvariables.jl | 96 +++++++++++--------- 3 files changed, 82 insertions(+), 55 deletions(-) diff --git a/Project.toml b/Project.toml index 1f51599a51..a6620c2f28 100644 --- a/Project.toml +++ b/Project.toml @@ -44,7 +44,9 @@ NaNMath = "77ba4419-2d1f-58cd-9bb1-8ffee604a2e3" NonlinearSolve = "8913a72c-1f9b-4ce2-8d82-65094dcecaec" OffsetArrays = "6fe1bfb0-de20-5000-8ca7-80f57d26f881" OrderedCollections = "bac558e1-5e72-5ebc-8fee-abe8a469f55d" +OrdinaryDiffEq = "1dea7af3-3e70-54e6-95c3-0bf5283fa5ed" OrdinaryDiffEqCore = "bbf590c4-e513-4bbe-9b18-05decba2e5d8" +OrdinaryDiffEqDefault = "50262376-6c5a-4cf5-baba-aaf4f84d72d7" PrecompileTools = "aea7be01-6a6a-4083-8856-8a6e6704d82a" RecursiveArrayTools = "731186ca-8d62-57ce-b412-fbd966d074cd" Reexport = "189a3867-3050-52da-a836-e630ba90ab69" @@ -59,9 +61,11 @@ SimpleNonlinearSolve = "727e6d20-b764-4bd8-a329-72de5adea6c7" SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf" SpecialFunctions = "276daf66-3868-5448-9aa4-cd146d93841b" StaticArrays = "90137ffa-7385-5640-81b9-e52037218182" +StochasticDiffEq = "789caeaf-c7a9-5a7d-9973-96adeb23e2a0" SymbolicIndexingInterface = "2efcf032-c050-4f8e-a9bb-153293bab1f5" SymbolicUtils = "d1185830-fcd6-423d-90d6-eec64667417b" Symbolics = "0c5d862f-8b57-4792-8d23-62f2024744c7" +Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" URIs = "5c2747f8-b7ea-4ff2-ba2e-563bfd36b1d4" UnPack = "3a884ed6-31ef-47d7-9d2a-63182c4928ed" Unitful = "1986cc42-f94f-5a68-af5c-568840ba703d" @@ -159,6 +163,7 @@ StochasticDiffEq = "6.72.1" SymbolicIndexingInterface = "0.3.39" SymbolicUtils = "3.26.1" Symbolics = "6.40" +Test = "1.11.0" URIs = "1" UnPack = "0.1, 1.0" Unitful = "1.1" diff --git a/src/systems/diffeqs/basic_transformations.jl b/src/systems/diffeqs/basic_transformations.jl index dedc7bc7a7..a0102891a5 100644 --- a/src/systems/diffeqs/basic_transformations.jl +++ b/src/systems/diffeqs/basic_transformations.jl @@ -94,13 +94,13 @@ new_sol = solve(new_prob, Tsit5()) ``` """ -function changeofvariables(sys::System, forward_subs, backward_subs; simplify=false, t0=missing) - t = independent_variable(sys) +function changeofvariables(sys::System, iv, forward_subs, backward_subs; simplify=true, t0=missing) + t = iv old_vars = first.(backward_subs) new_vars = last.(forward_subs) - kept_vars = setdiff(states(sys), old_vars) - rhs = [eq.rhs for eq in equations(sys)] + # kept_vars = setdiff(states(sys), old_vars) + # rhs = [eq.rhs for eq in equations(sys)] # use: dz/dt = ∂z/∂x dx/dt + ∂z/∂t dzdt = Symbolics.derivative( first.(forward_subs), t ) @@ -120,21 +120,29 @@ function changeofvariables(sys::System, forward_subs, backward_subs; simplify=fa defs = get_defaults(sys) new_defs = Dict() for f_sub in forward_subs - #TODO call value(...)? ex = substitute(first(f_sub), defs) if !ismissing(t0) ex = substitute(ex, t => t0) end new_defs[last(f_sub)] = ex end - return ODESystem(new_eqs; + for para in parameters(sys) + if haskey(defs, para) + new_defs[para] = defs[para] + end + end + @named new_sys = System(new_eqs, t; defaults=new_defs, observed=vcat(observed(sys),first.(backward_subs) .~ last.(backward_subs)) ) + if simplify + return mtkcompile(new_sys) + end + return new_sys end -function change_of_variable_SDE(sys::System, forward_subs, backward_subs, iv; simplify=false, t0=missing) - t = independent_variable(sys) +function change_of_variable_SDE(sys::System, iv, nvs, forward_subs, backward_subs; simplify=false, t0=missing) + t = iv old_vars = first.(backward_subs) new_vars = last.(forward_subs) @@ -142,14 +150,14 @@ function change_of_variable_SDE(sys::System, forward_subs, backward_subs, iv; si # use: f = Y(t, X) # use: dY = (∂f/∂t + μ∂f/∂x + (1/2)*σ^2*∂2f/∂x2)dt + σ∂f/∂xdW old_eqs = equations(sys) - old_noise = get_noiseeqs(sys) + old_noise = ModelingToolkit.get_noise_eqs(sys) ∂f∂t = Symbolics.derivative( first.(forward_subs), t ) - ∂f∂x = Symbolics.derivative( first.(forward_subs), old_vars ) + ∂f∂x = [Symbolics.derivative( first(f_sub), old_var )] ∂2f∂x2 = Symbolics.derivative( ∂f∂x, old_vars ) new_eqs = Equation[] - for (new_var, eq, noise, first, second, third) in zip(new_vars, old_eqs, old_noise, ∂f∂t, ∂f∂x, ∂2f∂x2) - ex = first + eq.rhs * second + 1/2 * noise^2 * third + for (new_var, eq, noise, nv, first, second, third) in zip(new_vars, old_eqs, old_noise, nvs, ∂f∂t, ∂f∂x, ∂2f∂x2) + ex = first + eq.rhs * second + 1/2 * noise^2 * third + noise*second*nv for eqs in old_eqs ex = substitute(ex, eqs.lhs => eqs.rhs) end @@ -160,11 +168,11 @@ function change_of_variable_SDE(sys::System, forward_subs, backward_subs, iv; si end push!(new_eqs, Differential(t)(new_var) ~ ex) end - new_noise = [noise * div for (noise, div) in zip(old_noise, ∂f∂x)] - return SDESystem(new_eqs, new_noise; + @named new_sys = System(new_eqs; observed=vcat(observed(sys),first.(backward_subs) .~ last.(backward_subs)) ) + return new_sys end """ diff --git a/test/changeofvariables.jl b/test/changeofvariables.jl index f9a4568fa0..1ce05c0ec5 100644 --- a/test/changeofvariables.jl +++ b/test/changeofvariables.jl @@ -1,30 +1,35 @@ -using ModelingToolkit, OrdinaryDiffEq +using ModelingToolkit, OrdinaryDiffEq, StochasticDiffEq using Test, LinearAlgebra # Change of variables: z = log(x) # (this implies that x = exp(z) is automatically non-negative) - -@parameters t α +@independent_variables t +# @variables z(t)[1:2, 1:2] +# D = Differential(t) +# eqs = [D(D(z)) ~ ones(2, 2)] +# @mtkcompile sys = System(eqs, t) +# @test_nowarn ODEProblem(sys, [z => zeros(2, 2), D(z) => ones(2, 2)], (0.0, 10.0)) + +@parameters α @variables x(t) D = Differential(t) eqs = [D(x) ~ α*x] tspan = (0., 1.) -u0 = [x => 1.0] -p = [α => -0.5] +def = [x => 1.0, α => -0.5] -sys = ODESystem(eqs; defaults=u0) -prob = ODEProblem(sys, [], tspan, p) +@mtkcompile sys = System(eqs, t;defaults=def) +prob = ODEProblem(sys, [], tspan) sol = solve(prob, Tsit5()) @variables z(t) forward_subs = [log(x) => z] backward_subs = [x => exp(z)] -new_sys = changeofvariables(sys, forward_subs, backward_subs) +new_sys = changeofvariables(sys, t, forward_subs, backward_subs) @test equations(new_sys)[1] == (D(z) ~ α) -new_prob = ODEProblem(new_sys, [], tspan, p) +new_prob = ODEProblem(new_sys, [], tspan) new_sol = solve(new_prob, Tsit5()) @test isapprox(new_sol[x][end], sol[x][end], atol=1e-4) @@ -32,24 +37,24 @@ new_sol = solve(new_prob, Tsit5()) # Riccati equation -@parameters t α +@parameters α @variables x(t) D = Differential(t) eqs = [D(x) ~ t^2 + α - x^2] -sys = ODESystem(eqs, defaults=[x=>1.]) +def = [x=>1., α => 1.] +@mtkcompile sys = System(eqs, t; defaults=def) @variables z(t) forward_subs = [t + α/(x+t) => z ] backward_subs = [ x => α/(z-t) - t] -new_sys = changeofvariables(sys, forward_subs, backward_subs; simplify=true, t0=0.) +new_sys = changeofvariables(sys, t, forward_subs, backward_subs; simplify=true, t0=0.) # output should be equivalent to # t^2 + α - z^2 + 2 (but this simplification is not found automatically) tspan = (0., 1.) -p = [α => 1.] -prob = ODEProblem(sys,[],tspan,p) -new_prob = ODEProblem(new_sys,[],tspan,p) +prob = ODEProblem(sys,[],tspan) +new_prob = ODEProblem(new_sys,[],tspan) sol = solve(prob, Tsit5()) new_sol = solve(new_prob, Tsit5()) @@ -57,40 +62,49 @@ new_sol = solve(new_prob, Tsit5()) @test isapprox(sol[x][end], new_sol[x][end], rtol=1e-4) -# Linear transformation to diagonal system -@parameters t -@variables x[1:3](t) -D = Differential(t) -A = [0. -1. 0.; -0.5 0.5 0.; 0. 0. -1.] -eqs = D.(x) .~ A*x +# # Linear transformation to diagonal system +# @variables x(t)[1:3] +# D = Differential(t) +# A = [0. -1. 0.; -0.5 0.5 0.; 0. 0. -1.] +# right = A.*transpose(x) +# eqs = [D(x[1]) ~ sum(right[1, 1:3]), D(x[2]) ~ sum(right[2, 1:3]), D(x[3]) ~ sum(right[3, 1:3])] -tspan = (0., 10.) -u0 = x .=> [1.0, 2.0, -1.0] +# tspan = (0., 10.) +# u0 = [x[1] => 1.0, x[2] => 2.0, x[3] => -1.0] -sys = ODESystem(eqs; defaults=u0) -prob = ODEProblem(sys,[],tspan) -sol = solve(prob, Tsit5()) +# @mtkcompile sys = System(eqs, t; defaults=u0) +# prob = ODEProblem(sys,[],tspan) +# sol = solve(prob, Tsit5()) -T = eigen(A).vectors +# T = eigen(A).vectors -@variables z[1:3](t) -forward_subs = T \ x .=> z -backward_subs = x .=> T*z +# @variables z(t)[1:3] +# forward_subs = T \ x .=> z +# backward_subs = x .=> T*z -new_sys = changeofvariables(sys, forward_subs, backward_subs; simplify=true) +# new_sys = changeofvariables(sys, t, forward_subs, backward_subs; simplify=true) -new_prob = ODEProblem(new_sys, [], tspan, p) -new_sol = solve(new_prob, Tsit5()) +# new_prob = ODEProblem(new_sys, [], tspan) +# new_sol = solve(new_prob, Tsit5()) -# test RHS -new_rhs = [eq.rhs for eq in equations(new_sys)] -new_A = Symbolics.value.(Symbolics.jacobian(new_rhs, z)) -@test isapprox(diagm(eigen(A).values), new_A, rtol = 1e-10) -@test isapprox( new_sol[x[1],end], sol[x[1],end], rtol=1e-4) +# # test RHS +# new_rhs = [eq.rhs for eq in equations(new_sys)] +# new_A = Symbolics.value.(Symbolics.jacobian(new_rhs, z)) +# @test isapprox(diagm(eigen(A).values), new_A, rtol = 1e-10) +# @test isapprox( new_sol[x[1],end], sol[x[1],end], rtol=1e-4) # Change of variables for sde -@Browian B -@parameters μ σ -@variables x(t) y(t) +# @independent_variables t +# @brownian B +# @parameters μ σ +# @variables x(t) y(t) +# D = Differential(t) +# eqs = [D(x) ~ μ*x + σ*x*B] + +# def = [x=>0., μ => 2., σ=>1.] +# @mtkcompile sys = System(eqs, t; defaults=def) +# forward_subs = [log(x) => y] +# backward_subs = [x => exp(y)] +# new_sys = change_of_variable_SDE(sys, t, [B], forward_subs, backward_subs) From 34c8a48c72655e885337bfc95b7d6ba150518d1e Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 4 Jun 2025 11:33:27 +0530 Subject: [PATCH 1962/2176] chore: bump minor version --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 903debe297..9645a412b0 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "ModelingToolkit" uuid = "961ee093-0014-501f-94e3-6117800e7a78" authors = ["Yingbo Ma ", "Chris Rackauckas and contributors"] -version = "10.0.1" +version = "10.1.0" [deps] ADTypes = "47edcb42-4c32-4615-8424-f2b9edc5f35b" From 27120aa8027c594f6286530d91ee44d359c73289 Mon Sep 17 00:00:00 2001 From: Fredrik Bagge Carlson Date: Wed, 4 Jun 2025 11:03:56 +0200 Subject: [PATCH 1963/2176] Fix docstring of `get_p` --- 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 2641d92108..61d2292925 100644 --- a/src/systems/problem_utils.jl +++ b/src/systems/problem_utils.jl @@ -1680,7 +1680,7 @@ end """ $(TYPEDSIGNATURES) -Return the `u0` vector for the given system `sys` and variable-value mapping `varmap`. All +Return the `p` object for the given system `sys` and variable-value mapping `varmap`. All keyword arguments are forwarded to [`MTKParameters`](@ref) for split systems and [`varmap_to_vars`](@ref) for non-split systems. """ From 22ab62432b85a20de0c56f1b6025d50488f3c6f8 Mon Sep 17 00:00:00 2001 From: fchen121 Date: Wed, 4 Jun 2025 17:44:24 -0400 Subject: [PATCH 1964/2176] Implement change of variables for sde --- src/systems/diffeqs/basic_transformations.jl | 37 ++++++++++++++++---- test/changeofvariables.jl | 34 +++++++++--------- 2 files changed, 49 insertions(+), 22 deletions(-) diff --git a/src/systems/diffeqs/basic_transformations.jl b/src/systems/diffeqs/basic_transformations.jl index a0102891a5..fbf03eacda 100644 --- a/src/systems/diffeqs/basic_transformations.jl +++ b/src/systems/diffeqs/basic_transformations.jl @@ -141,8 +141,10 @@ function changeofvariables(sys::System, iv, forward_subs, backward_subs; simplif return new_sys end -function change_of_variable_SDE(sys::System, iv, nvs, forward_subs, backward_subs; simplify=false, t0=missing) +function change_of_variable_SDE(sys::System, iv, forward_subs, backward_subs; simplify=true, t0=missing) + sys = mtkcompile(sys) t = iv + @brownian B old_vars = first.(backward_subs) new_vars = last.(forward_subs) @@ -151,13 +153,17 @@ function change_of_variable_SDE(sys::System, iv, nvs, forward_subs, backward_sub # use: dY = (∂f/∂t + μ∂f/∂x + (1/2)*σ^2*∂2f/∂x2)dt + σ∂f/∂xdW old_eqs = equations(sys) old_noise = ModelingToolkit.get_noise_eqs(sys) + + # Is there a function to find partial derivative? ∂f∂t = Symbolics.derivative( first.(forward_subs), t ) - ∂f∂x = [Symbolics.derivative( first(f_sub), old_var )] - ∂2f∂x2 = Symbolics.derivative( ∂f∂x, old_vars ) + ∂f∂t = [substitute(f_t, Differential(t)(old_var) => 0) for (f_t, old_var) in zip(∂f∂t, old_vars)] + + ∂f∂x = [Symbolics.derivative( first(f_sub), old_var ) for (f_sub, old_var) in zip(forward_subs, old_vars)] + ∂2f∂x2 = Symbolics.derivative.( ∂f∂x, old_vars ) new_eqs = Equation[] - for (new_var, eq, noise, nv, first, second, third) in zip(new_vars, old_eqs, old_noise, nvs, ∂f∂t, ∂f∂x, ∂2f∂x2) - ex = first + eq.rhs * second + 1/2 * noise^2 * third + noise*second*nv + for (new_var, eq, noise, first, second, third) in zip(new_vars, old_eqs, old_noise, ∂f∂t, ∂f∂x, ∂2f∂x2) + ex = first + eq.rhs * second + 1/2 * noise^2 * third + noise*second*B for eqs in old_eqs ex = substitute(ex, eqs.lhs => eqs.rhs) end @@ -169,9 +175,28 @@ function change_of_variable_SDE(sys::System, iv, nvs, forward_subs, backward_sub push!(new_eqs, Differential(t)(new_var) ~ ex) end - @named new_sys = System(new_eqs; + defs = get_defaults(sys) + new_defs = Dict() + for f_sub in forward_subs + ex = substitute(first(f_sub), defs) + if !ismissing(t0) + ex = substitute(ex, t => t0) + end + new_defs[last(f_sub)] = ex + end + for para in parameters(sys) + if haskey(defs, para) + new_defs[para] = defs[para] + end + end + + @named new_sys = System(new_eqs, t; + defaults=new_defs, observed=vcat(observed(sys),first.(backward_subs) .~ last.(backward_subs)) ) + if simplify + return mtkcompile(new_sys) + end return new_sys end diff --git a/test/changeofvariables.jl b/test/changeofvariables.jl index 1ce05c0ec5..2667e143f2 100644 --- a/test/changeofvariables.jl +++ b/test/changeofvariables.jl @@ -5,11 +5,11 @@ using Test, LinearAlgebra # Change of variables: z = log(x) # (this implies that x = exp(z) is automatically non-negative) @independent_variables t -# @variables z(t)[1:2, 1:2] -# D = Differential(t) -# eqs = [D(D(z)) ~ ones(2, 2)] -# @mtkcompile sys = System(eqs, t) -# @test_nowarn ODEProblem(sys, [z => zeros(2, 2), D(z) => ones(2, 2)], (0.0, 10.0)) +@variables z(t)[1:2, 1:2] +D = Differential(t) +eqs = [D(D(z)) ~ ones(2, 2)] +@mtkcompile sys = System(eqs, t) +@test_nowarn ODEProblem(sys, [z => zeros(2, 2), D(z) => ones(2, 2)], (0.0, 10.0)) @parameters α @variables x(t) @@ -94,17 +94,19 @@ new_sol = solve(new_prob, Tsit5()) # @test isapprox( new_sol[x[1],end], sol[x[1],end], rtol=1e-4) # Change of variables for sde -# @independent_variables t -# @brownian B -# @parameters μ σ -# @variables x(t) y(t) -# D = Differential(t) -# eqs = [D(x) ~ μ*x + σ*x*B] +@independent_variables t +@brownian B +@parameters μ σ +@variables x(t) y(t) +D = Differential(t) +eqs = [D(x) ~ μ*x + σ*x*B] -# def = [x=>0., μ => 2., σ=>1.] -# @mtkcompile sys = System(eqs, t; defaults=def) -# forward_subs = [log(x) => y] -# backward_subs = [x => exp(y)] -# new_sys = change_of_variable_SDE(sys, t, [B], forward_subs, backward_subs) +def = [x=>0., μ => 2., σ=>1.] +@mtkcompile sys = System(eqs, t; defaults=def) +forward_subs = [log(x) => y] +backward_subs = [x => exp(y)] +new_sys = change_of_variable_SDE(sys, t, forward_subs, backward_subs) +@test equations(new_sys)[1] == (D(y) ~ μ - 1/2*σ^2) +@test ModelingToolkit.get_noise_eqs(new_sys)[1] === ModelingToolkit.value(σ) From 064efafeb16f108eb66df16530542db4873b50f9 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 5 Jun 2025 13:04:31 +0530 Subject: [PATCH 1965/2176] feat: allow passing `wrap_delays` to `build_explicit_observed_function` --- src/systems/codegen.jl | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/systems/codegen.jl b/src/systems/codegen.jl index 1c4e780e64..2a5bc1a728 100644 --- a/src/systems/codegen.jl +++ b/src/systems/codegen.jl @@ -943,6 +943,8 @@ Generates a function that computes the observed value(s) `ts` in the system `sys - `throw = true` if true, throw an error when generating a function for `ts` that reference variables that do not exist. - `mkarray`: only used if the output is an array (that is, `!isscalar(ts)` and `ts` is not a tuple, in which case the result will always be a tuple). Called as `mkarray(ts, output_type)` where `ts` are the expressions to put in the array and `output_type` is the argument of the same name passed to build_explicit_observed_function. - `cse = true`: Whether to use Common Subexpression Elimination (CSE) to generate a more efficient function. +- `wrap_delays = is_dde(sys)`: Whether to add an argument for the history function and use + it to calculate all delayed variables. ## Returns @@ -981,7 +983,8 @@ function build_explicit_observed_function(sys, ts; op = Operator, throw = true, cse = true, - mkarray = nothing) + mkarray = nothing, + wrap_delays = is_dde(sys)) # TODO: cleanup is_tuple = ts isa Tuple if is_tuple @@ -1068,14 +1071,15 @@ function build_explicit_observed_function(sys, ts; p_end = length(dvs) + length(inputs) + length(ps) fns = build_function_wrapper( sys, ts, args...; p_start, p_end, filter_observed = obsfilter, - output_type, mkarray, try_namespaced = true, expression = Val{true}, cse) + output_type, mkarray, try_namespaced = true, expression = Val{true}, cse, + wrap_delays) if fns isa Tuple if expression return return_inplace ? fns : fns[1] end oop, iip = eval_or_rgf.(fns; eval_expression, eval_module) f = GeneratedFunctionWrapper{( - p_start + is_dde(sys), length(args) - length(ps) + 1 + is_dde(sys), is_split(sys))}( + p_start + wrap_delays, length(args) - length(ps) + 1 + wrap_delays, is_split(sys))}( oop, iip) return return_inplace ? (f, f) : f else @@ -1084,7 +1088,7 @@ function build_explicit_observed_function(sys, ts; end f = eval_or_rgf(fns; eval_expression, eval_module) f = GeneratedFunctionWrapper{( - p_start + is_dde(sys), length(args) - length(ps) + 1 + is_dde(sys), is_split(sys))}( + p_start + wrap_delays, length(args) - length(ps) + 1 + wrap_delays, is_split(sys))}( f, nothing) return f end From 6c57f82dd6b88402481c06110b039030c023a24a Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 4 Jun 2025 18:48:33 +0530 Subject: [PATCH 1966/2176] fix: fix major compile time regression due to `concrete_getu` --- src/systems/problem_utils.jl | 50 +++++++++++++++++++++--------------- 1 file changed, 30 insertions(+), 20 deletions(-) diff --git a/src/systems/problem_utils.jl b/src/systems/problem_utils.jl index 61d2292925..2016b1efd8 100644 --- a/src/systems/problem_utils.jl +++ b/src/systems/problem_utils.jl @@ -646,30 +646,40 @@ struct ReconstructInitializeprob{GP, GU} ugetter::GU end +""" + $(TYPEDEF) + +A wrapper over an observed function which allows calling it on a problem-like object. +`TD` determines whether the getter function is `(u, p, t)` (if `true`) or `(u, p)` (if +`false`). +""" +struct ObservedWrapper{TD, F} + f::F +end + +ObservedWrapper{TD}(f::F) where {TD, F} = ObservedWrapper{TD, F}(f) + +function (ow::ObservedWrapper{true})(prob) + ow.f(state_values(prob), parameter_values(prob), current_time(prob)) +end + +function (ow::ObservedWrapper{false})(prob) + ow.f(state_values(prob), parameter_values(prob)) +end + """ $(TYPEDSIGNATURES) Given an index provider `indp` and a vector of symbols `syms` return a type-stable getter -function by splitting `syms` into contiguous buffers where the getter of each buffer -is type-stable and constructing a function that calls and concatenates the results. -""" -function concrete_getu(indp, syms::AbstractVector) - # a list of contiguous buffer - split_syms = [Any[syms[1]]] - # the type of the getter of the last buffer - current = typeof(getu(indp, syms[1])) - for sym in syms[2:end] - getter = getu(indp, sym) - if typeof(getter) != current - # if types don't match, build a new buffer - push!(split_syms, []) - current = typeof(getter) - end - push!(split_syms[end], sym) - end - split_syms = Tuple(split_syms) - # the getter is now type-stable, and we can vcat it to get the full buffer - return Base.Fix1(reduce, vcat) ∘ getu(indp, split_syms) +function. + +Note that the getter ONLY works for problem-like objects, since it generates an observed +function. It does NOT work for solutions. +""" +Base.@nospecializeinfer function concrete_getu(indp, syms::AbstractVector) + @nospecialize + obsfn = build_explicit_observed_function(indp, syms; wrap_delays = false) + return ObservedWrapper{is_time_dependent(indp)}(obsfn) end """ From 93658219166a6f3d70144efe4188634b1c8bb492 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 5 Jun 2025 12:57:11 +0530 Subject: [PATCH 1967/2176] test: fix preface test --- test/odesystem.jl | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/test/odesystem.jl b/test/odesystem.jl index dc8f3f8349..28bc1d7db0 100644 --- a/test/odesystem.jl +++ b/test/odesystem.jl @@ -6,11 +6,14 @@ using OrdinaryDiffEq, Sundials using DiffEqBase, SparseArrays using StaticArrays using Test -using SymbolicUtils: issym +using SymbolicUtils.Code +using SymbolicUtils: Sym, issym using ForwardDiff using ModelingToolkit: value using ModelingToolkit: t_nounits as t, D_nounits as D +using Symbolics using Symbolics: unwrap +using DiffEqBase: isinplace # Define some variables @parameters σ ρ β @@ -505,13 +508,6 @@ sys = complete(sys) @test_throws Any ODEFunction(sys) @testset "Preface tests" begin - using OrdinaryDiffEq - using Symbolics - using DiffEqBase: isinplace - using ModelingToolkit - using SymbolicUtils.Code - using SymbolicUtils: Sym - c = [0] function f(c, du::AbstractVector{Float64}, u::AbstractVector{Float64}, p, t::Float64) c .= [c[1] + 1] @@ -554,7 +550,9 @@ sys = complete(sys) @named sys = System(eqs, t, us, ps; defaults = defs, preface = preface) sys = complete(sys) - prob = ODEProblem(sys, [], (0.0, 1.0)) + # don't build initializeprob because it will use preface in other functions and + # affect `c` + prob = ODEProblem(sys, [], (0.0, 1.0); build_initializeprob = false) sol = solve(prob, Euler(); dt = 0.1) @test c[1] == length(sol) From c6eec57218f2cbca54bffd5b1110e84498c17cd6 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 5 Jun 2025 13:07:39 +0530 Subject: [PATCH 1968/2176] test: AD test is no longer broken --- test/extensions/ad.jl | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/test/extensions/ad.jl b/test/extensions/ad.jl index 2efc9781cf..7e9cbdd740 100644 --- a/test/extensions/ad.jl +++ b/test/extensions/ad.jl @@ -27,13 +27,11 @@ sol = solve(prob, Tsit5()) mtkparams = parameter_values(prob) new_p = rand(14) -@test_broken begin - gs = gradient(new_p) do new_p - new_params = SciMLStructures.replace(SciMLStructures.Tunable(), mtkparams, new_p) - new_prob = remake(prob, p = new_params) - new_sol = solve(new_prob, Tsit5()) - sum(new_sol) - end +gs = gradient(new_p) do new_p + new_params = SciMLStructures.replace(SciMLStructures.Tunable(), mtkparams, new_p) + new_prob = remake(prob, p = new_params) + new_sol = solve(new_prob, Tsit5()) + sum(new_sol) end @testset "Issue#2997" begin From 53fb05fce25ad230bbfde83ec78d307ce1a8885b Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 5 Jun 2025 14:58:25 +0530 Subject: [PATCH 1969/2176] fix: fix `observed_equations_used_by` for `Initial`s --- src/utils.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utils.jl b/src/utils.jl index dd6971dbe1..2b8ec4a7a0 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -830,7 +830,7 @@ Keyword arguments: `available_vars` will not be searched for in the observed equations. """ function observed_equations_used_by(sys::AbstractSystem, exprs; - involved_vars = vars(exprs; op = Union{Shift, Differential}), obs = observed(sys), available_vars = []) + involved_vars = vars(exprs; op = Union{Shift, Differential, Initial}), obs = observed(sys), available_vars = []) obsvars = getproperty.(obs, :lhs) graph = observed_dependency_graph(obs) if !(available_vars isa Set) From adf609859b8d3b5d385602a21194cbb23e59e6b5 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 5 Jun 2025 17:32:38 +0530 Subject: [PATCH 1970/2176] ci: add benchmark CI --- .github/workflows/benchmark.yml | 24 +++++++++++++++ benchmark/Project.toml | 3 ++ benchmark/benchmarks.jl | 54 +++++++++++++++++++++++++++++++++ 3 files changed, 81 insertions(+) create mode 100644 .github/workflows/benchmark.yml create mode 100644 benchmark/Project.toml create mode 100644 benchmark/benchmarks.jl diff --git a/.github/workflows/benchmark.yml b/.github/workflows/benchmark.yml new file mode 100644 index 0000000000..c025bf4907 --- /dev/null +++ b/.github/workflows/benchmark.yml @@ -0,0 +1,24 @@ +name: Benchmark this PR +on: + pull_request: + branches: + - master + paths-ignore: + - 'docs/**' + +permissions: + pull-requests: write # needed to post comments + +jobs: + bench: + name: "Benchmarks" + strategy: + matrix: + version: + - "1" + - "lts" + runs-on: ubuntu-latest + steps: + - uses: MilesCranmer/AirspeedVelocity.jl@action-v1 + with: + julia-version: "${{ matrix.version }}" diff --git a/benchmark/Project.toml b/benchmark/Project.toml new file mode 100644 index 0000000000..666ceb7abb --- /dev/null +++ b/benchmark/Project.toml @@ -0,0 +1,3 @@ +[deps] +ModelingToolkitStandardLibrary = "16a59e39-deab-5bd0-87e4-056b12336739" +OrdinaryDiffEqDefault = "50262376-6c5a-4cf5-baba-aaf4f84d72d7" diff --git a/benchmark/benchmarks.jl b/benchmark/benchmarks.jl new file mode 100644 index 0000000000..dc7487b18c --- /dev/null +++ b/benchmark/benchmarks.jl @@ -0,0 +1,54 @@ +using ModelingToolkit, BenchmarkTools +using ModelingToolkitStandardLibrary +using ModelingToolkitStandardLibrary.Thermal +using OrdinaryDiffEqDefault + +const SUITE = BenchmarkGroup() + +@mtkmodel DCMotor begin + @structural_parameters begin + R = 0.5 + L = 4.5e-3 + k = 0.5 + J = 0.02 + f = 0.01 + V_step = 10 + tau_L_step = -3 + end + @components begin + ground = Ground() + source = Voltage() + voltage_step = Blocks.Step(height = V_step, start_time = 0) + R1 = Resistor(R = R) + L1 = Inductor(L = L, i = 0.0) + emf = EMF(k = k) + fixed = Fixed() + load = Torque() + load_step = Blocks.Step(height = tau_L_step, start_time = 3) + inertia = Inertia(J = J) + friction = Damper(d = f) + end + @equations begin + connect(fixed.flange, emf.support, friction.flange_b) + connect(emf.flange, friction.flange_a, inertia.flange_a) + connect(inertia.flange_b, load.flange) + connect(load_step.output, load.tau) + connect(voltage_step.output, source.V) + connect(source.p, R1.p) + connect(R1.n, L1.p) + connect(L1.n, emf.p) + connect(emf.n, source.n, ground.g) + end +end + +@named model = DCMotor() + +SUITE["mtkcompile"] = @benchmarkable mtkcompile($model) + +model = mtkcompile(model) +u0 = unknowns(model) .=> 0.0 +tspan = (0.0, 6.0) +SUITE["ODEProblem"] = @benchmarkable ODEProblem($model, $u0, $tspan) + +prob = ODEProblem(model, u0, tspan) +SUITE["init"] = init($prob) From 2ccdbbaf09bf051d14713dab75a949a3402222c7 Mon Sep 17 00:00:00 2001 From: vyudu Date: Thu, 15 May 2025 12:39:14 -0400 Subject: [PATCH 1971/2176] refactor: add definitions of shared functions to optimal_control_interface.jl --- ext/MTKCasADiDynamicOptExt.jl | 5 + src/systems/optimal_control_interface.jl | 147 +++++++++++++++++++++++ 2 files changed, 152 insertions(+) diff --git a/ext/MTKCasADiDynamicOptExt.jl b/ext/MTKCasADiDynamicOptExt.jl index 8f6b2c345e..e07791f4ab 100644 --- a/ext/MTKCasADiDynamicOptExt.jl +++ b/ext/MTKCasADiDynamicOptExt.jl @@ -90,6 +90,11 @@ function MTK.CasADiDynamicOptProblem(sys::System, u0map, tspan, pmap; CasADiDynamicOptProblem(f, u0, tspan, p, model, kwargs...) end +MTK.generate_U(model, dims) = 1 +MTK.generate_V(model, dims) = 1 +MTK.generate_timescale(model, dims) = 1 +MTK.generate_internal_model(::Type{CasADiModel}) = CasADi.opti() + function init_model(sys, tspan, steps, u0map, pmap, u0; is_free_t = false) ctrls = MTK.unbound_inputs(sys) states = unknowns(sys) diff --git a/src/systems/optimal_control_interface.jl b/src/systems/optimal_control_interface.jl index 2b460f77a2..04809f5686 100644 --- a/src/systems/optimal_control_interface.jl +++ b/src/systems/optimal_control_interface.jl @@ -1,6 +1,8 @@ abstract type AbstractDynamicOptProblem{uType, tType, isinplace} <: SciMLBase.AbstractODEProblem{uType, tType, isinplace} end +abstract type AbstractCollocation end + struct DynamicOptSolution model::Any sol::ODESolution @@ -148,3 +150,148 @@ function process_tspan(tspan, dt, steps) return length(tspan[1]:dt:tspan[2]), false end end + +function process_DynamicOptProblem(prob_type::AbstractDynamicOptProblem, model_type, sys::ODESystem, u0map, tspan, pmap; + dt = nothing, + steps = nothing, + guesses = Dict(), kwargs...) + + MTK.warn_overdetermined(sys, u0map) + _u0map = has_alg_eqs(sys) ? u0map : merge(Dict(u0map), Dict(guesses)) + f, u0, p = MTK.process_SciMLProblem(ODEInputFunction, sys, _u0map, pmap; + t = tspan !== nothing ? tspan[1] : tspan, kwargs...) + + stidxmap = Dict([v => i for (i, v) in enumerate(states)]) + u0map = Dict([MTK.default_toterm(MTK.value(k)) => v for (k, v) in u0map]) + u0_idxs = has_alg_eqs(sys) ? collect(1:length(states)) : + [stidxmap[MTK.default_toterm(k)] for (k, v) in u0map] + pmap = Dict{Any, Any}(pmap) + steps, is_free_t = MTK.process_tspan(tspan, dt, steps) + + ctrls = MTK.unbound_inputs(sys) + states = unknowns(sys) + + model = generate_internal_model(model_type) + U = generate_U(model, u0) + V = generate_V() + tₛ = generate_timescale() + fullmodel = model_type(model, U, V, tₛ) + + set_variable_bounds!(fullmodel, sys, pmap) + add_cost_function!(fullmodel, sys, tspan, pmap; is_free_t) + add_user_constraints!(fullmodel, sys, tspan, pmap; is_free_t) + add_initial_constraints!(fullmodel, u0, u0_idxs) + + prob_type(f, u0, tspan, p, fullmodel, kwargs...) +end + +function add_cost_function!() + jcosts = copy(MTK.get_costs(sys)) + consolidate = MTK.get_consolidate(sys) + if isnothing(jcosts) || isempty(jcosts) + minimize!(opti, MX(0)) + return + end + + jcosts = substitute_model_vars(model, sys, pmap, jcosts; is_free_t) + jcosts = substitute_free_final_vars(model, sys, pmap, jcosts; is_free_t) + jcosts = substitute_fixed_t_vars(model, sys, pmap, jcosts; is_free_t) + jcosts = substitute_integral() +end + +function add_user_constraints!() + conssys = MTK.get_constraintsystem(sys) + jconstraints = isnothing(conssys) ? nothing : MTK.get_constraints(conssys) + (isnothing(jconstraints) || isempty(jconstraints)) && return nothing + + auxmap = Dict([u => MTK.default_toterm(MTK.value(u)) for u in unknowns(conssys)]) + jconstraints = substitute_model_vars(model, sys, pmap, jconstraints; auxmap, is_free_t) + + for c in jconstraints + if cons isa Equation + add_constraint!() + elseif cons.relational_op === Symbolics.geq + add_constraint!() + else + add_constraint!() + end + end +end + +function generate_U end +function generate_V end +function generate_timescale end + +function add_initial_constraints! end +function add_constraint! end + +function add_collocation_solve_constraints!(prob, tableau) + nᵤ = size(U.u, 1) + nᵥ = size(V.u, 1) + + if is_explicit(tableau) + K = MX[] + for k in 1:(length(tsteps) - 1) + τ = tsteps[k] + for (i, h) in enumerate(c) + ΔU = sum([A[i, j] * K[j] for j in 1:(i - 1)], init = MX(zeros(nᵤ))) + Uₙ = U.u[:, k] + ΔU * dt + Vₙ = V.u[:, k] + Kₙ = tₛ * f(Uₙ, Vₙ, p, τ + h * dt) # scale the time + push!(K, Kₙ) + end + ΔU = dt * sum([α[i] * K[i] for i in 1:length(α)]) + subject_to!(solver_opti, U.u[:, k] + ΔU == U.u[:, k + 1]) + empty!(K) + end + else + for k in 1:(length(tsteps) - 1) + τ = tsteps[k] + # Kᵢ = generate_K() + Kᵢ = variable!(solver_opti, nᵤ, length(α)) + ΔUs = A * Kᵢ' # the stepsize at each stage of the implicit method + for (i, h) in enumerate(c) + ΔU = ΔUs[i, :]' + Uₙ = U.u[:, k] + ΔU * dt + Vₙ = V.u[:, k] + subject_to!(solver_opti, Kᵢ[:, i] == tₛ * f(Uₙ, Vₙ, p, τ + h * dt)) + end + ΔU_tot = dt * (Kᵢ * α) + subject_to!(solver_opti, U.u[:, k] + ΔU_tot == U.u[:, k + 1]) + end + end +end + +function add_equational_solve_constraints!() + diff_eqs = substitute_differentials() + add_constraint!() + + alg_eqs = substitute_model_vars() + add_constraint!() +end + +""" +Add the solve constraints, set the solver (Ipopt, e.g.) +""" +function prepare_solver end + +function DiffEqBase.solve(prob::AbstractDynamicOptProblem, solver::AbstractCollocation) + #add_solve_constraints!(prob, solver) + solver = prepare_solver(prob, solver) + sol = solve_prob(prob, solver) + + ts = get_t_values(sol) + Us = get_U_values(sol) + Vs = get_V_values(sol) + + ode_sol = DiffEqBase.build_solution(prob, solver, ts, Us) + input_sol = DiffEqBase.build_solution(prob, solver, ts, Vs) + + if successful_solve(model) + ode_sol = SciMLBase.solution_new_retcode( + ode_sol, SciMLBase.ReturnCode.ConvergenceFailure) + !isnothing(input_sol) && (input_sol = SciMLBase.solution_new_retcode( + input_sol, SciMLBase.ReturnCode.ConvergenceFailure)) + end + DynamicOptSolution(model, ode_sol, input_sol) +end From df1a0e496fb0d8e7afb8fd034595bcbf251bb591 Mon Sep 17 00:00:00 2001 From: vyudu Date: Fri, 16 May 2025 17:05:45 -0400 Subject: [PATCH 1972/2176] refactor: aduse optimal control itnerface --- ext/MTKCasADiDynamicOptExt.jl | 9 +- ext/MTKInfiniteOptExt.jl | 342 ++++++++--------------- src/systems/optimal_control_interface.jl | 181 ++++++------ test/extensions/dynamic_optimization.jl | 6 +- 4 files changed, 223 insertions(+), 315 deletions(-) diff --git a/ext/MTKCasADiDynamicOptExt.jl b/ext/MTKCasADiDynamicOptExt.jl index e07791f4ab..e0c8fbb5ea 100644 --- a/ext/MTKCasADiDynamicOptExt.jl +++ b/ext/MTKCasADiDynamicOptExt.jl @@ -25,6 +25,7 @@ struct CasADiModel U::MXLinearInterpolation V::MXLinearInterpolation tₛ::MX + is_free_final::Bool end struct CasADiDynamicOptProblem{uType, tType, isinplace, P, F, K} <: @@ -90,10 +91,10 @@ function MTK.CasADiDynamicOptProblem(sys::System, u0map, tspan, pmap; CasADiDynamicOptProblem(f, u0, tspan, p, model, kwargs...) end -MTK.generate_U(model, dims) = 1 -MTK.generate_V(model, dims) = 1 -MTK.generate_timescale(model, dims) = 1 MTK.generate_internal_model(::Type{CasADiModel}) = CasADi.opti() +MTK.generate_state_variable(model, u0, ns, nt) +MTK.generate_input_variable(model, c0, nc, nt) = 1 +MTK.generate_timescale(model, dims) = 1 function init_model(sys, tspan, steps, u0map, pmap, u0; is_free_t = false) ctrls = MTK.unbound_inputs(sys) @@ -317,7 +318,7 @@ function add_solve_constraints(prob, tableau) for k in 1:(length(tsteps) - 1) τ = tsteps[k] Kᵢ = variable!(solver_opti, nᵤ, length(α)) - ΔUs = A * Kᵢ' # the stepsize at each stage of the implicit method + ΔUs = A * Kᵢ' for (i, h) in enumerate(c) ΔU = ΔUs[i, :]' Uₙ = U.u[:, k] + ΔU * dt diff --git a/ext/MTKInfiniteOptExt.jl b/ext/MTKInfiniteOptExt.jl index 40eb6f5264..6dd80232f8 100644 --- a/ext/MTKInfiniteOptExt.jl +++ b/ext/MTKInfiniteOptExt.jl @@ -4,6 +4,7 @@ using InfiniteOpt using DiffEqBase using LinearAlgebra using StaticArrays +using UnPack import SymbolicUtils import NaNMath const MTK = ModelingToolkit @@ -38,6 +39,29 @@ struct InfiniteOptDynamicOptProblem{uType, tType, isinplace, P, F, K} <: end end +struct InfiniteOptModel + model::InfiniteModel + U::AbstractVariableRef + V::AbstractVariableRef + tₛ::Union + is_free_final::Bool +end + +MTK.generate_internal_model(m::Type{InfiniteOptModel}) = InfiniteModel() +MTK.generate_state_variable!(m::InfiniteModel, u0::Vector, ns, nt) = @variable(m, U[i = 1:nt], Infinite(m[:t]), start=u0[i]) +MTK.generate_input_variable!(m::InfiniteModel, c0, nc, nt) = @variable(m, V[i = 1:nc], Infinite(m[:t]), start=c0[i]) + +function MTK.generate_timescale!(m::InfiniteModel, guess, is_free_t) + @variable(m, tₛ ≥ 0, start = guess) + if !is_free_t + fix(m[:tₛ], 1) + set_start_value(m[:tₛ], 1) + end +end + +MTK.add_constraint!(m::InfiniteOptModel, expr) = @constraint(m.model, expr) +MTK.set_objective!(m::InfiniteOptModel, expr) = @objective(m.model, Min, expr) + """ JuMPDynamicOptProblem(sys::System, u0, tspan, p; dt) @@ -58,19 +82,7 @@ function MTK.JuMPDynamicOptProblem(sys::System, u0map, tspan, pmap; dt = nothing, steps = nothing, guesses = Dict(), kwargs...) - MTK.warn_overdetermined(sys, u0map) - _u0map = has_alg_eqs(sys) ? MTK.to_varmap(u0map, unknowns(sys)) : - merge(Dict(u0map), Dict(guesses)) - pmap = MTK.to_varmap(pmap, parameters(sys)) - f, u0, p = MTK.process_SciMLProblem(ODEInputFunction, sys, merge(_u0map, pmap); - t = tspan !== nothing ? tspan[1] : tspan, kwargs...) - - pmap = MTK.recursive_unwrap(MTK.AnyDict(pmap)) - MTK.evaluate_varmap!(pmap, keys(pmap)) - steps, is_free_t = MTK.process_tspan(tspan, dt, steps) - model = init_model(sys, tspan, steps, u0map, pmap, u0; is_free_t) - - JuMPDynamicOptProblem(f, u0, tspan, p, model, kwargs...) + process_DynamicOptProblem(JuMPDynamicOptProblem, InfiniteOptModel, u0map, tspan, pmap; dt, steps, guesses, kwargs...) end """ @@ -87,216 +99,87 @@ function MTK.InfiniteOptDynamicOptProblem(sys::System, u0map, tspan, pmap; dt = nothing, steps = nothing, guesses = Dict(), kwargs...) - MTK.warn_overdetermined(sys, u0map) - _u0map = has_alg_eqs(sys) ? MTK.to_varmap(u0map, unknowns(sys)) : - merge(Dict(u0map), Dict(guesses)) - pmap = MTK.to_varmap(pmap, parameters(sys)) - f, u0, p = MTK.process_SciMLProblem(ODEInputFunction, sys, merge(_u0map, pmap); - t = tspan !== nothing ? tspan[1] : tspan, kwargs...) - - pmap = MTK.recursive_unwrap(MTK.AnyDict(pmap)) - MTK.evaluate_varmap!(pmap, keys(pmap)) - steps, is_free_t = MTK.process_tspan(tspan, dt, steps) - model = init_model(sys, tspan, steps, u0map, pmap, u0; is_free_t) - - add_infopt_solve_constraints!(model, sys, pmap; is_free_t) - InfiniteOptDynamicOptProblem(f, u0, tspan, p, model, kwargs...) -end - -# Initialize InfiniteOpt model. -function init_model(sys, tspan, steps, u0map, pmap, u0; is_free_t = false) - ctrls = MTK.unbound_inputs(sys) - states = unknowns(sys) - model = InfiniteModel() - - if is_free_t - (ts_sym, te_sym) = tspan - MTK.symbolic_type(ts_sym) !== MTK.NotSymbolic() && - error("Free initial time problems are not currently supported.") - @variable(model, tf, start=pmap[te_sym]) - set_lower_bound(tf, ts_sym) - hasbounds(te_sym) && begin - lo, hi = getbounds(te_sym) - set_lower_bound(tf, lo) - set_upper_bound(tf, hi) - end - pmap[te_sym] = model[:tf] - tspan = (0, 1) - end - - @infinite_parameter(model, t in [tspan[1], tspan[2]], num_supports=steps) - @variable(model, U[i = 1:length(states)], Infinite(t), start=u0[i]) - c0 = MTK.value.([pmap[c] for c in ctrls]) - @variable(model, V[i = 1:length(ctrls)], Infinite(t), start=c0[i]) - for (i, ct) in enumerate(ctrls) - pmap[ct] = model[:V][i] - end - - set_jump_bounds!(model, sys, pmap) - add_jump_cost_function!(model, sys, (tspan[1], tspan[2]), pmap; is_free_t) - add_user_constraints!(model, sys, pmap; is_free_t) - - stidxmap = Dict([v => i for (i, v) in enumerate(states)]) - u0map = Dict([MTK.default_toterm(MTK.value(k)) => v for (k, v) in u0map]) - u0_idxs = has_alg_eqs(sys) ? collect(1:length(states)) : - [stidxmap[MTK.default_toterm(k)] for (k, v) in u0map] - add_initial_constraints!(model, u0, u0_idxs, tspan[1]) - return model + prob = process_DynamicOptProblem(InfiniteOptDynamicOptProblem, InfiniteOptModel, u0map, tspan, pmap; dt, steps, guesses, kwargs...) end -""" -Modify the pmap by replacing controls with V[i](t), and tf with the model's final time variable for free final time problems. -""" -function modified_pmap(model, sys, pmap) - pmap = Dict{Any, Any}(pmap) -end - -function set_jump_bounds!(model, sys, pmap) - U = model[:U] +function MTK.set_variable_bounds!(model, sys, pmap, tf) for (i, u) in enumerate(unknowns(sys)) if MTK.hasbounds(u) lo, hi = MTK.getbounds(u) - set_lower_bound(U[i], Symbolics.fast_substitute(lo, pmap)) - set_upper_bound(U[i], Symbolics.fast_substitute(hi, pmap)) + set_lower_bound(model.U[i], Symbolics.fixpoint_sub(lo, pmap)) + set_upper_bound(model.U[i], Symbolics.fixpoint_sub(hi, pmap)) end end - V = model[:V] for (i, v) in enumerate(MTK.unbound_inputs(sys)) if MTK.hasbounds(v) lo, hi = MTK.getbounds(v) - set_lower_bound(V[i], Symbolics.fast_substitute(lo, pmap)) - set_upper_bound(V[i], Symbolics.fast_substitute(hi, pmap)) + set_lower_bound(model.V[i], Symbolics.fixpoint_sub(lo, pmap)) + set_upper_bound(model.V[i], Symbolics.fixpoint_sub(hi, pmap)) end end -end -function add_jump_cost_function!(model::InfiniteModel, sys, tspan, pmap; is_free_t = false) - jcosts = cost(sys) - if Symbolics._iszero(jcosts) - @objective(model, Min, 0) - return + if symbolic_type(tf) === ScalarSymbolic() && hasbounds(tf) + lo, hi = MTK.getbounds(tf) + set_lower_bound(model.tₛ, lo) + set_upper_bound(model.tₛ, hi) end - jcosts = substitute_jump_vars(model, sys, pmap, [jcosts]; is_free_t)[1] - tₛ = is_free_t ? model[:tf] : 1 - - # Substitute integral - iv = MTK.get_iv(sys) +end - intmap = Dict() +function MTK.substitute_integral(model, jcosts) for int in MTK.collect_applied_operators(jcosts, Symbolics.Integral) op = MTK.operation(int) arg = only(arguments(MTK.value(int))) lo, hi = (op.domain.domain.left, op.domain.domain.right) lo = MTK.value(lo) hi = haskey(pmap, hi) ? 1 : MTK.value(hi) - intmap[int] = tₛ * InfiniteOpt.∫(arg, model[:t], lo, hi) - end - jcosts = Symbolics.substitute(jcosts, intmap) - @objective(model, Min, MTK.value(jcosts)) -end - -function add_user_constraints!(model::InfiniteModel, sys, pmap; is_free_t = false) - jconstraints = MTK.get_constraints(sys) - (isnothing(jconstraints) || isempty(jconstraints)) && return nothing - cons_dvs, cons_ps = MTK.process_constraint_system( - jconstraints, Set(unknowns(sys)), parameters(sys), MTK.get_iv(sys); validate = false) - - if is_free_t - for u in cons_dvs - x = MTK.operation(u) - t = only(arguments(u)) - if (MTK.symbolic_type(t) === MTK.NotSymbolic()) - error("Provided specific time constraint in a free final time problem. This is not supported by the JuMP/InfiniteOpt collocation solvers. The offending variable is $u. Specific-time constraints can only be specified at the beginning or end of the timespan.") - end - end - end - - auxmap = Dict([u => MTK.default_toterm(MTK.value(u)) for u in cons_dvs]) - jconstraints = substitute_jump_vars(model, sys, pmap, jconstraints; auxmap, is_free_t) - - # Substitute to-term'd variables - for (i, cons) in enumerate(jconstraints) - if cons isa Equation - @constraint(model, cons.lhs - cons.rhs==0, base_name="user[$i]") - elseif cons.relational_op === Symbolics.geq - @constraint(model, cons.lhs - cons.rhs≥0, base_name="user[$i]") - else - @constraint(model, cons.lhs - cons.rhs≤0, base_name="user[$i]") - end + intmap[int] = model.tₛ * InfiniteOpt.∫(arg, model.model.t, lo, hi) end + jcosts = map(c -> Symbolics.substitute(c, intmap), jcosts) end -function add_initial_constraints!(model::InfiniteModel, u0, u0_idxs, ts) - U = model[:U] +function MTK.add_initial_constraints!(model::InfiniteOptModel, u0, u0_idxs, ts) + U = model.U @constraint(model, initial[i in u0_idxs], U[i](ts)==u0[i]) end -function substitute_jump_vars(model, sys, pmap, exprs; auxmap = Dict(), is_free_t = false) - iv = MTK.get_iv(sys) - sts = unknowns(sys) - cts = MTK.unbound_inputs(sys) - U = model[:U] - V = model[:V] - x_ops = [MTK.operation(MTK.unwrap(st)) for st in sts] - c_ops = [MTK.operation(MTK.unwrap(ct)) for ct in cts] - - exprs = map(c -> Symbolics.fast_substitute(c, auxmap), exprs) - exprs = map(c -> Symbolics.fast_substitute(c, Dict(pmap)), exprs) - if is_free_t - tf = model[:tf] +function MTK.substitute_model_vars(model, sys, exprs; tf = nothing) + whole_interval_map = Dict([[v => model.U[i] for (i, v) in enumerate(unknowns(sys))]; + [v => model.V[i] for (i, v) in enumerate(MTK.unbound_inputs(sys))]]) + exprs = map(c -> Symbolics.fast_substitute(c, whole_interval_map), exprs) + + x_ops = [MTK.operation(MTK.unwrap(st)) for st in unknowns(sys)] + c_ops = [MTK.operation(MTK.unwrap(ct)) for ct in MTK.unbound_inputs(sys)] + + if symbolic_type(tf) === ScalarSymbolic() free_t_map = Dict([[x(tf) => U[i](1) for (i, x) in enumerate(x_ops)]; [c(tf) => V[i](1) for (i, c) in enumerate(c_ops)]]) exprs = map(c -> Symbolics.fast_substitute(c, free_t_map), exprs) end - # for variables like x(t) - whole_interval_map = Dict([[v => U[i] for (i, v) in enumerate(sts)]; - [v => V[i] for (i, v) in enumerate(cts)]]) - exprs = map(c -> Symbolics.fast_substitute(c, whole_interval_map), exprs) - # for variables like x(1.0) fixed_t_map = Dict([[x_ops[i] => U[i] for i in 1:length(U)]; [c_ops[i] => V[i] for i in 1:length(V)]]) - exprs = map(c -> Symbolics.fast_substitute(c, fixed_t_map), exprs) - exprs end -function add_infopt_solve_constraints!(model::InfiniteModel, sys, pmap; is_free_t = false) - # Differential equations - U = model[:U] - t = model[:t] +function MTK.substitute_differentials(model::InfiniteOptModel, eqs) + U = model.U + t = model.model[:t] D = Differential(MTK.get_iv(sys)) diffsubmap = Dict([D(U[i]) => ∂(U[i], t) for i in 1:length(U)]) - tₛ = is_free_t ? model[:tf] : 1 - - diff_eqs = substitute_jump_vars(model, sys, pmap, diff_equations(sys)) - diff_eqs = map(e -> Symbolics.substitute(e, diffsubmap), diff_eqs) - @constraint(model, D[i = 1:length(diff_eqs)], diff_eqs[i].lhs==tₛ * diff_eqs[i].rhs) - - # Algebraic equations - alg_eqs = substitute_jump_vars(model, sys, pmap, alg_equations(sys)) - @constraint(model, A[i = 1:length(alg_eqs)], alg_eqs[i].lhs==alg_eqs[i].rhs) + map(e -> Symbolics.substitute(e, diffsubmap), diff_eqs) end -function add_jump_solve_constraints!(prob, tableau; is_free_t = false) - A = tableau.A - α = tableau.α - c = tableau.c - model = prob.model - f = prob.f - p = prob.p - - t = model[:t] - tsteps = supports(t) - tmax = tsteps[end] - pop!(tsteps) - tₛ = is_free_t ? model[:tf] : 1 +function add_solve_constraints!(prob::JuMPDynamicOptProblem, solver) + @unpack A, α, c = solver.tableau + @unpack model, f, p = prob + tsteps = supports(model.model[:t]) dt = tsteps[2] - tsteps[1] - U = model[:U] - V = model[:V] + tₛ = model.tₛ + U = model.U + V = model.V nᵤ = length(U) nᵥ = length(V) if MTK.is_explicit(tableau) @@ -306,7 +189,7 @@ function add_jump_solve_constraints!(prob, tableau; is_free_t = false) ΔU = sum([A[i, j] * K[j] for j in 1:(i - 1)], init = zeros(nᵤ)) Uₙ = [U[i](τ) + ΔU[i] * dt for i in 1:nᵤ] Vₙ = [V[i](τ) for i in 1:nᵥ] - Kₙ = tₛ * f(Uₙ, Vₙ, p, τ + h * dt) # scale the time + Kₙ = tₛ * f(Uₙ, Vₙ, p, τ + h * dt) push!(K, Kₙ) end ΔU = dt * sum([α[i] * K[i] for i in 1:length(α)]) @@ -325,27 +208,39 @@ function add_jump_solve_constraints!(prob, tableau; is_free_t = false) @constraint(model, [j = 1:nᵤ], K[i, j]==(tₛ * f(Uₙ, V, p, τ + h * dt)[j]), DomainRestrictions(t => τ), base_name="solve_K$i($τ)") end - @constraint(model, [n = 1:nᵤ], U[n](τ) + ΔU_tot[n]==U[n](min(τ + dt, tmax)), + @constraint(model, [n = 1:nᵤ], U[n](τ) + ΔU_tot[n]==U[n](min(τ + dt, tsteps[end])), DomainRestrictions(t => τ), base_name="solve_U($τ)") end end end """ -Solve JuMPDynamicOptProblem. Arguments: -- prob: a JumpDynamicOptProblem -- jump_solver: a LP solver such as HiGHS -- tableau_getter: Takes in a function to fetch a tableau. Tableau loaders look like `constructRK4` and may be found at https://docs.sciml.ai/DiffEqDevDocs/stable/internals/tableaus/. If this argument is not passed in, the solver will default to Radau second order. -- silent: set the model silent (suppress model output) +JuMP Collocation solver. +- solver: a optimization solver such as Ipopt +- tableau: An ODE RK tableau. Load a tableau by calling a function like `constructRK4` and may be found at https://docs.sciml.ai/DiffEqDevDocs/stable/internals/tableaus/. If this argument is not passed in, the solver will default to Radau second order. Returns a DynamicOptSolution, which contains both the model and the ODE solution. """ -function DiffEqBase.solve( - prob::JuMPDynamicOptProblem, jump_solver, tableau_getter = MTK.constructDefault; silent = false) - model = prob.model - tableau = tableau_getter() - silent && set_silent(model) +struct JuMPCollocation + solver::Any + tableau::DiffEqBase.ODERKTableau +end +JuMPCollocation(solver; tableau = MTK.constructDefault()) = JuMPCollocation(solver, tableau) + +""" +InfiniteOpt Collocation solver. +- solver: an optimization solver such as Ipopt +- `derivative_method` kwarg refers to the method used by InfiniteOpt to compute derivatives. The list of possible options can be found at https://infiniteopt.github.io/InfiniteOpt.jl/stable/guide/derivative/. Defaults to FiniteDifference(Backward()). +""" +struct InfiniteOptCollocation + solver::Any + derivative_method::InfiniteOpt.AbstractDerivativeMethod +end +InfiniteOptCollocation(solver; derivative_method = InfiniteOpt.FiniteDifference(InfiniteOpt.Backward())) = InfiniteOptCollocation(solver, derivative_method) +function MTK.prepare_solver!(prob::JuMPDynamicOptProblem, solver::JuMPCollocation; verbose = false, kwargs...) + model = prob.model.model + verbose || set_silent(model) # Unregister current solver constraints for con in all_constraints(model) if occursin("solve", JuMP.name(con)) @@ -360,53 +255,48 @@ function DiffEqBase.solve( delete(model, var) end end - add_jump_solve_constraints!(prob, tableau; is_free_t = haskey(model, :tf)) - _solve(prob, jump_solver, tableau_getter) + add_collocation_solve_constraints!(model, solver.tableau) + set_optimizer(model, solver.solver) end -""" -`derivative_method` kwarg refers to the method used by InfiniteOpt to compute derivatives. The list of possible options can be found at https://infiniteopt.github.io/InfiniteOpt.jl/stable/guide/derivative/. Defaults to FiniteDifference(Backward()). -""" -function DiffEqBase.solve(prob::InfiniteOptDynamicOptProblem, jump_solver; - derivative_method = InfiniteOpt.FiniteDifference(Backward()), silent = false) - model = prob.model - silent && set_silent(model) - set_derivative_method(model[:t], derivative_method) - _solve(prob, jump_solver, derivative_method) +function MTK.prepare_solver!(prob::InfiniteOptDynamicOptProblem, solver::InfiniteOptCollocation; verbose = false, kwargs...) + model = prob.model.model + verbose || set_silent(model) + add_equational_constraints!(model, prob.f.sys, prob.tspan) + set_derivative_method(model[:t], solver.derivative_method) + set_optimizer(model, solver.solver) end -function _solve(prob::AbstractDynamicOptProblem, jump_solver, solver) - model = prob.model - set_optimizer(model, jump_solver) - optimize!(model) +function MTK.optimize_model!(prob::Union{InfiniteOptDynamicOptProblem, JuMPDynamicOptProblem}, solver) + optimize!(prob.model.model) + prob.model +end +function MTK.get_V_values(m::InfiniteOptModel) + if !isempty(m.V) + V_vals = value.(m.V) + V_vals = [[V_vals[i][j] for i in 1:length(V_vals)] for j in 1:length(ts)] + else + nothing + end +end +function MTK.get_U_values(m::InfiniteOptModel) + U_vals = value.(m.U) + U_vals = [[U_vals[i][j] for i in 1:length(U_vals)] for j in 1:length(ts)] +end + +MTK.get_t_values(model) = prob.tspan[1] .+ model.tₛ * supports(model.model[:t]) + +function MTK.successful_solve(m::InfiniteOptModel) + model = m.model tstatus = termination_status(model) pstatus = primal_status(model) !has_values(model) && error("Model not solvable; please report this to github.com/SciML/ModelingToolkit.jl with a MWE.") - tf = haskey(model, :tf) ? value(model[:tf]) : 1 - ts = tf * supports(model[:t]) - U_vals = value.(model[:U]) - U_vals = [[U_vals[i][j] for i in 1:length(U_vals)] for j in 1:length(ts)] - sol = DiffEqBase.build_solution(prob, solver, ts, U_vals) - - input_sol = nothing - if !isempty(model[:V]) - V_vals = value.(model[:V]) - V_vals = [[V_vals[i][j] for i in 1:length(V_vals)] for j in 1:length(ts)] - input_sol = DiffEqBase.build_solution(prob, solver, ts, V_vals) - end - - if !(pstatus === FEASIBLE_POINT && + pstatus === FEASIBLE_POINT && (tstatus === OPTIMAL || tstatus === LOCALLY_SOLVED || tstatus === ALMOST_OPTIMAL || - tstatus === ALMOST_LOCALLY_SOLVED)) - sol = SciMLBase.solution_new_retcode(sol, SciMLBase.ReturnCode.ConvergenceFailure) - !isnothing(input_sol) && (input_sol = SciMLBase.solution_new_retcode( - input_sol, SciMLBase.ReturnCode.ConvergenceFailure)) - end - - DynamicOptSolution(model, sol, input_sol) + tstatus === ALMOST_LOCALLY_SOLVED) end import InfiniteOpt: JuMP, GeneralVariableRef diff --git a/src/systems/optimal_control_interface.jl b/src/systems/optimal_control_interface.jl index 04809f5686..04c1a0c74b 100644 --- a/src/systems/optimal_control_interface.jl +++ b/src/systems/optimal_control_interface.jl @@ -22,6 +22,10 @@ function JuMPDynamicOptProblem end function InfiniteOptDynamicOptProblem end function CasADiDynamicOptProblem end +function JuMPCollocation end +function InfiniteOptCollocation end +function CasADiCollocation end + function warn_overdetermined(sys, u0map) cstrs = constraints(sys) if !isempty(cstrs) @@ -133,6 +137,9 @@ end # returns the JuMP timespan, the number of steps, and whether it is a free time problem. function process_tspan(tspan, dt, steps) is_free_time = false + symbolic_type(tspan[1]) !== NotSymbolic() && + error("Free initial time problems are not currently supported by the collocation solvers.") + if isnothing(dt) && isnothing(steps) error("Must provide either the dt or the number of intervals to the collocation solvers (JuMP, InfiniteOpt, CasADi).") elseif symbolic_type(tspan[1]) === ScalarSymbolic() || @@ -142,16 +149,19 @@ function process_tspan(tspan, dt, steps) isnothing(dt) || @warn "Specified dt for free final time problem. This will be ignored; dt will be determined by the number of timesteps." - return steps, true + return (0, 1), steps, true else isnothing(steps) || @warn "Specified number of steps for problem with concrete tspan. This will be ignored; number of steps will be determined by dt." - return length(tspan[1]:dt:tspan[2]), false + return tspan, length(tspan[1]:dt:tspan[2]), false end end -function process_DynamicOptProblem(prob_type::AbstractDynamicOptProblem, model_type, sys::ODESystem, u0map, tspan, pmap; +########################## +### MODEL CONSTRUCTION ### +########################## +function process_DynamicOptProblem(prob_type::Type{<:AbstractDynamicOptProblem}, model_type, sys::ODESystem, u0map, tspan, pmap; dt = nothing, steps = nothing, guesses = Dict(), kwargs...) @@ -166,128 +176,135 @@ function process_DynamicOptProblem(prob_type::AbstractDynamicOptProblem, model_t u0_idxs = has_alg_eqs(sys) ? collect(1:length(states)) : [stidxmap[MTK.default_toterm(k)] for (k, v) in u0map] pmap = Dict{Any, Any}(pmap) - steps, is_free_t = MTK.process_tspan(tspan, dt, steps) + model_tspan, steps, is_free_t = MTK.process_tspan(tspan, dt, steps) ctrls = MTK.unbound_inputs(sys) states = unknowns(sys) + c0 = MTK.value.([pmap[c] for c in ctrls]) model = generate_internal_model(model_type) - U = generate_U(model, u0) - V = generate_V() - tₛ = generate_timescale() - fullmodel = model_type(model, U, V, tₛ) + generate_time_variable!(model, model_tspan, steps) + U = generate_state_variable!(model, u0, length(states), length(steps)) + V = generate_input_variable!(model, c0, length(ctrls), length(steps)) + tₛ = generate_timescale!(model, get(pmap, tspan[2], tspan[2]), is_free_t) + fullmodel = model_type(model, U, V, tₛ, is_free_t) - set_variable_bounds!(fullmodel, sys, pmap) - add_cost_function!(fullmodel, sys, tspan, pmap; is_free_t) - add_user_constraints!(fullmodel, sys, tspan, pmap; is_free_t) - add_initial_constraints!(fullmodel, u0, u0_idxs) + set_variable_bounds!(fullmodel, sys, pmap, tspan[2]) + add_cost_function!(fullmodel, sys, tspan, pmap) + add_user_constraints!(fullmodel, sys, tspan, pmap) + add_initial_constraints!(fullmodel, u0, u0_idxs, model_tspan[1]) prob_type(f, u0, tspan, p, fullmodel, kwargs...) end -function add_cost_function!() +function generate_internal_model end +function generate_state_variable! end +function generate_input_variable! end +function generate_timescale! end +function set_variable_bounds! end +function add_initial_constraints! end +function add_constraint! end +is_free_final(model) = model.is_free_final + +function add_cost_function!(model, sys, tspan, pmap) jcosts = copy(MTK.get_costs(sys)) consolidate = MTK.get_consolidate(sys) if isnothing(jcosts) || isempty(jcosts) - minimize!(opti, MX(0)) + set_objective!(model, 0) return end - - jcosts = substitute_model_vars(model, sys, pmap, jcosts; is_free_t) - jcosts = substitute_free_final_vars(model, sys, pmap, jcosts; is_free_t) - jcosts = substitute_fixed_t_vars(model, sys, pmap, jcosts; is_free_t) - jcosts = substitute_integral() + jcosts = substitute_model_vars(model, sys, jcosts; tf = tspan[2]) + jcosts = substitute_params(pmap, jcosts) + jcosts = substitute_integral(model, jcosts) + set_objective!(model, consolidate(jcosts)) end -function add_user_constraints!() +function add_user_constraints!(model, sys, tspan, pmap) conssys = MTK.get_constraintsystem(sys) jconstraints = isnothing(conssys) ? nothing : MTK.get_constraints(conssys) (isnothing(jconstraints) || isempty(jconstraints)) && return nothing + consvars = MTK.get_unknowns(conssys) + is_free_final(model) && check_constraint_vars(consvars) - auxmap = Dict([u => MTK.default_toterm(MTK.value(u)) for u in unknowns(conssys)]) - jconstraints = substitute_model_vars(model, sys, pmap, jconstraints; auxmap, is_free_t) + jconstraints = substitute_model_vars(model, sys, jcosts; tf = tspan[2]) + jconstraints = substitute_toterm(consvars, jconstraints) + jconstraints = substitute_params(pmap, jconstraints) for c in jconstraints if cons isa Equation - add_constraint!() + add_constraint!(model, c.lhs - c.rhs == 0) elseif cons.relational_op === Symbolics.geq - add_constraint!() + add_constraint!(model, c.lhs - c.rhs ≥ 0) else - add_constraint!() + add_constraint!(model, c.lhs - c.rhs ≤ 0) end end end -function generate_U end -function generate_V end -function generate_timescale end - -function add_initial_constraints! end -function add_constraint! end +function add_equational_constraints!(model, sys, tspan) + model = model.model + diff_eqs = substitute_model_vars(model, sys, diff_equations(sys); tf = tspan[2]) + diff_eqs = substitute_differentials(model, sys, diff_eqs) + for eq in diff_eqs + add_constraint!(model, eq.lhs == eq.rhs * model.tₛ) + end -function add_collocation_solve_constraints!(prob, tableau) - nᵤ = size(U.u, 1) - nᵥ = size(V.u, 1) - - if is_explicit(tableau) - K = MX[] - for k in 1:(length(tsteps) - 1) - τ = tsteps[k] - for (i, h) in enumerate(c) - ΔU = sum([A[i, j] * K[j] for j in 1:(i - 1)], init = MX(zeros(nᵤ))) - Uₙ = U.u[:, k] + ΔU * dt - Vₙ = V.u[:, k] - Kₙ = tₛ * f(Uₙ, Vₙ, p, τ + h * dt) # scale the time - push!(K, Kₙ) - end - ΔU = dt * sum([α[i] * K[i] for i in 1:length(α)]) - subject_to!(solver_opti, U.u[:, k] + ΔU == U.u[:, k + 1]) - empty!(K) - end - else - for k in 1:(length(tsteps) - 1) - τ = tsteps[k] - # Kᵢ = generate_K() - Kᵢ = variable!(solver_opti, nᵤ, length(α)) - ΔUs = A * Kᵢ' # the stepsize at each stage of the implicit method - for (i, h) in enumerate(c) - ΔU = ΔUs[i, :]' - Uₙ = U.u[:, k] + ΔU * dt - Vₙ = V.u[:, k] - subject_to!(solver_opti, Kᵢ[:, i] == tₛ * f(Uₙ, Vₙ, p, τ + h * dt)) - end - ΔU_tot = dt * (Kᵢ * α) - subject_to!(solver_opti, U.u[:, k] + ΔU_tot == U.u[:, k + 1]) - end + alg_eqs = substitute_model_vars(model, sys, alg_equations(sys); tf = tspan[2]) + for eq in alg_eqs + add_constraint!(model, eq.lhs == eq.rhs * model.tₛ) end end -function add_equational_solve_constraints!() - diff_eqs = substitute_differentials() - add_constraint!() +function set_objective! end +"""Substitute variables like x(1.5) with the corresponding model variables.""" +function substitute_model_vars end +function substitute_integral end +function substitute_differentials end + +function substitute_toterm(vars, exprs) + toterm_map = Dict([u => MTK.default_toterm(MTK.value(u)) for u in vars]) + exprs = map(c -> Symbolics.fast_substitute(c, toterm_map), exprs) +end + +function substitute_params(pmap, exprs) + exprs = map(c -> Symbolics.fast_substitute(c, Dict(pmap)), exprs) +end - alg_eqs = substitute_model_vars() - add_constraint!() +function check_constraint_vars(vars) + for u in vars + x = operation(u) + t = only(arguments(u)) + if (symbolic_type(t) === NotSymbolic()) + error("Provided specific time constraint in a free final time problem. This is not supported by the collocation solvers at the moment. The offending variable is $u. Specific-time user constraints can only be specified at the end of the timespan.") + end + end end +######################## +### SOLVER UTILITIES ### +######################## """ -Add the solve constraints, set the solver (Ipopt, e.g.) +Add the solve constraints, set the solver (Ipopt, e.g.) and solver options. """ -function prepare_solver end - -function DiffEqBase.solve(prob::AbstractDynamicOptProblem, solver::AbstractCollocation) - #add_solve_constraints!(prob, solver) - solver = prepare_solver(prob, solver) - sol = solve_prob(prob, solver) +function prepare_solver! end +function optimize_model! end +function get_t_values end +function get_U_values end +function get_V_values end +function successful_solve end + +function DiffEqBase.solve(prob::AbstractDynamicOptProblem, solver::AbstractCollocation; verbose = false, kwargs...) + solver = prepare_solver!(prob, solver) + model = optimize_model!(prob, solver) - ts = get_t_values(sol) - Us = get_U_values(sol) - Vs = get_V_values(sol) + ts = get_t_values(model) + Us = get_U_values(model) + Vs = get_V_values(model) ode_sol = DiffEqBase.build_solution(prob, solver, ts, Us) - input_sol = DiffEqBase.build_solution(prob, solver, ts, Vs) + input_sol = isnothing(Vs) ? nothing : DiffEqBase.build_solution(prob, solver, ts, Vs) - if successful_solve(model) + if !successful_solve(model) ode_sol = SciMLBase.solution_new_retcode( ode_sol, SciMLBase.ReturnCode.ConvergenceFailure) !isnothing(input_sol) && (input_sol = SciMLBase.solution_new_retcode( diff --git a/test/extensions/dynamic_optimization.jl b/test/extensions/dynamic_optimization.jl index dd2368b558..03aeebf1bc 100644 --- a/test/extensions/dynamic_optimization.jl +++ b/test/extensions/dynamic_optimization.jl @@ -1,5 +1,5 @@ using ModelingToolkit -import JuMP, InfiniteOpt +import InfiniteOpt using DiffEqDevTools, DiffEqBase using SimpleDiffEq using OrdinaryDiffEqSDIRK, OrdinaryDiffEqVerner, OrdinaryDiffEqTsit5, OrdinaryDiffEqFIRK @@ -27,7 +27,7 @@ const M = ModelingToolkit # Test explicit method. jprob = JuMPDynamicOptProblem(sys, u0map, tspan, parammap, dt = 0.01) - @test JuMP.num_constraints(jprob.model) == 2 # initials + @test InfiniteOpt.num_constraints(jprob.model) == 2 # initials jsol = solve(jprob, Ipopt.Optimizer, constructRK4, silent = true) oprob = ODEProblem(sys, [u0map; parammap], tspan) osol = solve(oprob, SimpleRK4(), dt = 0.01) @@ -56,7 +56,7 @@ const M = ModelingToolkit @mtkcompile lksys = System(eqs, t; constraints = constr) jprob = JuMPDynamicOptProblem(lksys, u0map, tspan, parammap; guesses = guess, dt = 0.01) - @test JuMP.num_constraints(jprob.model) == 2 + @test InfiniteOpt.num_constraints(jprob.model) == 2 jsol = solve(jprob, Ipopt.Optimizer, constructTsitouras5, silent = true) # 12.190 s, 9.68 GiB @test jsol.sol(0.6; idxs = x(t)) ≈ 3.5 @test jsol.sol(0.3; idxs = x(t)) ≈ 7.0 From 912d71b318424c0d3737cb22d75d3048c4c89097 Mon Sep 17 00:00:00 2001 From: vyudu Date: Fri, 16 May 2025 19:05:05 -0400 Subject: [PATCH 1973/2176] refactor: add interface functions for CasADi --- ext/MTKCasADiDynamicOptExt.jl | 284 ++++++++--------------- ext/MTKInfiniteOptExt.jl | 107 +++++---- src/ModelingToolkit.jl | 2 + src/systems/optimal_control_interface.jl | 62 ++--- test/extensions/dynamic_optimization.jl | 58 +++-- 5 files changed, 211 insertions(+), 302 deletions(-) diff --git a/ext/MTKCasADiDynamicOptExt.jl b/ext/MTKCasADiDynamicOptExt.jl index e0c8fbb5ea..554d9faec4 100644 --- a/ext/MTKCasADiDynamicOptExt.jl +++ b/ext/MTKCasADiDynamicOptExt.jl @@ -6,10 +6,8 @@ using UnPack using NaNMath const MTK = ModelingToolkit -# NaNMath for ff in [acos, log1p, acosh, log2, asin, tan, atanh, cos, log, sin, log10, sqrt] f = nameof(ff) - # These need to be defined so that JuMP can trace through functions built by Symbolics @eval NaNMath.$f(x::CasadiSymbolicObject) = Base.$f(x) end @@ -76,78 +74,47 @@ function MTK.CasADiDynamicOptProblem(sys::System, u0map, tspan, pmap; dt = nothing, steps = nothing, guesses = Dict(), kwargs...) - MTK.warn_overdetermined(sys, u0map) - _u0map = has_alg_eqs(sys) ? MTK.to_varmap(u0map, unknowns(sys)) : - merge(Dict(u0map), Dict(guesses)) - pmap = MTK.to_varmap(pmap, parameters(sys)) - f, u0, p = MTK.process_SciMLProblem(ODEInputFunction, sys, merge(_u0map, pmap); - t = tspan !== nothing ? tspan[1] : tspan, output_type = MX, kwargs...) - - pmap = MTK.recursive_unwrap(MTK.AnyDict(pmap)) - MTK.evaluate_varmap!(pmap, keys(pmap)) - steps, is_free_t = MTK.process_tspan(tspan, dt, steps) - model = init_model(sys, tspan, steps, u0map, pmap, u0; is_free_t) - - CasADiDynamicOptProblem(f, u0, tspan, p, model, kwargs...) + process_DynamicOptProblem(CasADiDynamicOptProblem, CasADiModel, sys, u0map, tspan, pmap; dt, steps, guesses, kwargs...) end MTK.generate_internal_model(::Type{CasADiModel}) = CasADi.opti() -MTK.generate_state_variable(model, u0, ns, nt) -MTK.generate_input_variable(model, c0, nc, nt) = 1 -MTK.generate_timescale(model, dims) = 1 -function init_model(sys, tspan, steps, u0map, pmap, u0; is_free_t = false) - ctrls = MTK.unbound_inputs(sys) - states = unknowns(sys) - opti = CasADi.Opti() +function MTK.generate_state_variable(model::Opti, u0, ns, nt, tsteps) + U = CasADi.variable!(model, ns, nt) + set_initial!(opti, U, DM(repeat(u0, 1, steps))) + MXLinearInterpolation(U, tsteps, tsteps[2] - tsteps[1]) +end +function MTK.generate_input_variable(model::Opti, c0, nc, nt, tsteps) + V = CasADi.variable!(model, nc, nt) + !isempty(c0) && set_initial!(opti, V, DM(repeat(c0, 1, steps))) + MXLinearInterpolation(V, tsteps, tsteps[2] - tsteps[1]) +end + +function MTK.generate_timescale(model::Opti, guess, is_free_t) if is_free_t - (ts_sym, te_sym) = tspan - MTK.symbolic_type(ts_sym) !== MTK.NotSymbolic() && - error("Free initial time problems are not currently supported in CasADiDynamicOptProblem.") - tₛ = variable!(opti) - set_initial!(opti, tₛ, pmap[te_sym]) - subject_to!(opti, tₛ >= ts_sym) - hasbounds(te_sym) && begin - lo, hi = getbounds(te_sym) - subject_to!(opti, tₛ >= lo) - subject_to!(opti, tₛ >= hi) - end - pmap[te_sym] = tₛ - tsteps = LinRange(0, 1, steps) + tₛ = variable!(model) + set_initial!(model, tₛ, guess) + subject_to!(model, tₛ >= 0) + tₛ else - tₛ = MX(1) - tsteps = LinRange(tspan[1], tspan[2], steps) + MX(1) end +end - U = CasADi.variable!(opti, length(states), steps) - V = CasADi.variable!(opti, length(ctrls), steps) - set_initial!(opti, U, DM(repeat(u0, 1, steps))) - c0 = MTK.value.([pmap[c] for c in ctrls]) - !isempty(c0) && set_initial!(opti, V, DM(repeat(c0, 1, steps))) - - U_interp = MXLinearInterpolation(U, tsteps, tsteps[2] - tsteps[1]) - V_interp = MXLinearInterpolation(V, tsteps, tsteps[2] - tsteps[1]) - for (i, ct) in enumerate(ctrls) - pmap[ct] = V[i, :] +function MTK.add_constraint!(model::CasADiModel, expr) + @unpack opti = model + if cons isa Equation + subject_to!(opti, expr.lhs - expr.rhs == 0) + elseif cons.relational_op === Symbolics.geq + subject_to!(opti, expr.lhs - expr.rhs ≥ 0) + else + subject_to!(opti, expr.lhs - expr.rhs ≤ 0) end - - model = CasADiModel(opti, U_interp, V_interp, tₛ) - - set_casadi_bounds!(model, sys, pmap) - add_cost_function!(model, sys, tspan, pmap; is_free_t) - add_user_constraints!(model, sys, tspan, pmap; is_free_t) - - stidxmap = Dict([v => i for (i, v) in enumerate(states)]) - u0map = Dict([MTK.default_toterm(MTK.value(k)) => v for (k, v) in u0map]) - u0_idxs = has_alg_eqs(sys) ? collect(1:length(states)) : - [stidxmap[MTK.default_toterm(k)] for (k, v) in u0map] - add_initial_constraints!(model, u0, u0_idxs) - - model end +MTK.set_objective!(model::CasADiModel, expr) = minimize!(model.opti, MX(expr)) -function set_casadi_bounds!(model, sys, pmap) +function MTK.set_variable_bounds!(model, sys, pmap, tf) @unpack opti, U, V = model for (i, u) in enumerate(unknowns(sys)) if MTK.hasbounds(u) @@ -163,36 +130,56 @@ function set_casadi_bounds!(model, sys, pmap) subject_to!(opti, V.u[i, :] <= Symbolics.fast_substitute(hi, pmap)) end end + if MTK.symbolic_type(tf) === MTK.ScalarSymbolic() && hasbounds(tf) + lo, hi = MTK.getbounds(tf) + subject_to!(opti, model.tₛ >= lo) + subject_to!(opti, model.tₛ <= hi) + end end -function add_initial_constraints!(model::CasADiModel, u0, u0_idxs) +function MTK.add_initial_constraints!(model::CasADiModel, u0, u0_idxs) @unpack opti, U = model for i in u0_idxs subject_to!(opti, U.u[i, 1] == u0[i]) end end -function add_user_constraints!(model::CasADiModel, sys, tspan, pmap; is_free_t) +function MTK.substitute_model_vars( + model::CasADiModel, sys, pmap, exprs; auxmap::Dict = Dict(), is_free_t) @unpack opti, U, V, tₛ = model - iv = MTK.get_iv(sys) - jconstraints = MTK.get_constraints(sys) - (isnothing(jconstraints) || isempty(jconstraints)) && return nothing - - stidxmap = Dict([v => i for (i, v) in enumerate(unknowns(sys))]) - ctidxmap = Dict([v => i for (i, v) in enumerate(MTK.unbound_inputs(sys))]) - cons_dvs, cons_ps = MTK.process_constraint_system( - jconstraints, Set(unknowns(sys)), parameters(sys), iv; validate = false) - - auxmap = Dict([u => MTK.default_toterm(MTK.value(u)) for u in cons_dvs]) - jconstraints = substitute_casadi_vars(model, sys, pmap, jconstraints; is_free_t, auxmap) - # Manually substitute fixed-t variables - for (i, cons) in enumerate(jconstraints) - consvars = MTK.vars(cons) - for st in consvars + sts = unknowns(sys) + cts = MTK.unbound_inputs(sys) + + x_ops = [MTK.operation(MTK.unwrap(st)) for st in sts] + c_ops = [MTK.operation(MTK.unwrap(ct)) for ct in cts] + + exprs = map(c -> Symbolics.fast_substitute(c, auxmap), exprs) + exprs = map(c -> Symbolics.fast_substitute(c, Dict(pmap)), exprs) + # tf means different things in different contexts; a [tf] in a cost function + # should be tₛ, while a x(tf) should translate to x[1] + if is_free_t + free_t_map = Dict([[x(tₛ) => U.u[i, end] for (i, x) in enumerate(x_ops)]; + [c(tₛ) => V.u[i, end] for (i, c) in enumerate(c_ops)]]) + exprs = map(c -> Symbolics.fast_substitute(c, free_t_map), exprs) + end + + exprs = substitute_fixed_t_vars(exprs) + + # for variables like x(t) + whole_interval_map = Dict([[v => U.u[i, :] for (i, v) in enumerate(sts)]; + [v => V.u[i, :] for (i, v) in enumerate(cts)]]) + exprs = map(c -> Symbolics.fast_substitute(c, whole_interval_map), exprs) + exprs +end + +function substitute_fixed_t_vars(exprs) + for i in 1:length(exprs) + subvars = MTK.vars(exprs[i]) + for st in subvars MTK.iscall(st) || continue - x = MTK.operation(st) - t = only(MTK.arguments(st)) + x = operation(st) + t = only(arguments(st)) MTK.symbolic_type(t) === MTK.NotSymbolic() || continue if haskey(stidxmap, x(iv)) idx = stidxmap[x(iv)] @@ -201,52 +188,19 @@ function add_user_constraints!(model::CasADiModel, sys, tspan, pmap; is_free_t) idx = ctidxmap[x(iv)] cv = V end - cons = Symbolics.substitute(cons, Dict(x(t) => cv(t)[idx])) - end - - if cons isa Equation - subject_to!(opti, cons.lhs - cons.rhs == 0) - elseif cons.relational_op === Symbolics.geq - subject_to!(opti, cons.lhs - cons.rhs ≥ 0) - else - subject_to!(opti, cons.lhs - cons.rhs ≤ 0) + exprs[i] = Symbolics.fast_substitute(exprs[i], Dict(x(t) => cv(t)[idx])) end + jcosts = Symbolics.substitute(jcosts, Dict(x(t) => cv(t)[idx])) end end -function add_cost_function!(model::CasADiModel, sys, tspan, pmap; is_free_t) - @unpack opti, U, V, tₛ = model - jcosts = cost(sys) - if Symbolics._iszero(jcosts) - minimize!(opti, MX(0)) - return - end - - iv = MTK.get_iv(sys) - stidxmap = Dict([v => i for (i, v) in enumerate(unknowns(sys))]) - ctidxmap = Dict([v => i for (i, v) in enumerate(MTK.unbound_inputs(sys))]) - - jcosts = substitute_casadi_vars(model, sys, pmap, [jcosts]; is_free_t)[1] - # Substitute fixed-time variables. - costvars = MTK.vars(jcosts) - for st in costvars - MTK.iscall(st) || continue - x = operation(st) - t = only(arguments(st)) - MTK.symbolic_type(t) === MTK.NotSymbolic() || continue - if haskey(stidxmap, x(iv)) - idx = stidxmap[x(iv)] - cv = U - else - idx = ctidxmap[x(iv)] - cv = V - end - jcosts = Symbolics.substitute(jcosts, Dict(x(t) => cv(t)[idx])) - end +MTK.substitute_differentials(model::CasADiModel, exprs, args...) = exprs +function MTK.substitute_integral(model::CasADiModel, exprs) + @unpack U, opti = model dt = U.t[2] - U.t[1] intmap = Dict() - for int in MTK.collect_applied_operators(jcosts, Symbolics.Integral) + for int in MTK.collect_applied_operators(exprs, Symbolics.Integral) op = MTK.operation(int) arg = only(arguments(MTK.value(int))) lo, hi = (op.domain.domain.left, op.domain.domain.right) @@ -255,39 +209,11 @@ function add_cost_function!(model::CasADiModel, sys, tspan, pmap; is_free_t) # Approximate integral as sum. intmap[int] = dt * tₛ * sum(arg) end - jcosts = Symbolics.substitute(jcosts, intmap) - jcosts = MTK.value(jcosts) - minimize!(opti, MX(jcosts)) -end - -function substitute_casadi_vars( - model::CasADiModel, sys, pmap, exprs; auxmap::Dict = Dict(), is_free_t) - @unpack opti, U, V, tₛ = model - iv = MTK.get_iv(sys) - sts = unknowns(sys) - cts = MTK.unbound_inputs(sys) - - x_ops = [MTK.operation(MTK.unwrap(st)) for st in sts] - c_ops = [MTK.operation(MTK.unwrap(ct)) for ct in cts] - - exprs = map(c -> Symbolics.fast_substitute(c, auxmap), exprs) - exprs = map(c -> Symbolics.fast_substitute(c, Dict(pmap)), exprs) - # tf means different things in different contexts; a [tf] in a cost function - # should be tₛ, while a x(tf) should translate to x[1] - if is_free_t - free_t_map = Dict([[x(tₛ) => U.u[i, end] for (i, x) in enumerate(x_ops)]; - [c(tₛ) => V.u[i, end] for (i, c) in enumerate(c_ops)]]) - exprs = map(c -> Symbolics.fast_substitute(c, free_t_map), exprs) - end - - # for variables like x(t) - whole_interval_map = Dict([[v => U.u[i, :] for (i, v) in enumerate(sts)]; - [v => V.u[i, :] for (i, v) in enumerate(cts)]]) - exprs = map(c -> Symbolics.fast_substitute(c, whole_interval_map), exprs) - exprs + exprs = map(c -> Symbolics.substitute(c, intmap), exprs) + exprs = MTK.value.(exprs) end -function add_solve_constraints(prob, tableau) +function add_solve_constraints!(prob, tableau) @unpack A, α, c = tableau @unpack model, f, p = prob @unpack opti, U, V, tₛ = model @@ -332,28 +258,22 @@ function add_solve_constraints(prob, tableau) solver_opti end -""" - solve(prob::CasADiDynamicOptProblem, casadi_solver, ode_solver; plugin_options, solver_options, silent) - -`plugin_options` and `solver_options` get propagated to the Opti object in CasADi. - -NOTE: the solver should be passed in as a string to CasADi. "ipopt" -""" -function DiffEqBase.solve( - prob::CasADiDynamicOptProblem, solver::Union{String, Symbol} = "ipopt", - tableau_getter = MTK.constructDefault; plugin_options::Dict = Dict(), - solver_options::Dict = Dict(), silent = false) - @unpack model, u0, p, tspan, f = prob - tableau = tableau_getter() - @unpack opti, U, V, tₛ = model - +function MTK.prepare_solver() opti = add_solve_constraints(prob, tableau) - silent && (solver_options["print_level"] = 0) solver!(opti, "$solver", plugin_options, solver_options) +end +function MTK.get_U_values() + U_vals = value_getter(U.u) + size(U_vals, 2) == 1 && (U_vals = U_vals') + U_vals = [[U_vals[i, j] for i in 1:size(U_vals, 1)] for j in 1:length(ts)] +end +function MTK.get_V_values() +end +function MTK.get_t_values() + ts = value_getter(tₛ) * U.t +end - failed = false - value_getter = nothing - sol = nothing +function MTK.optimize_model!() try sol = CasADi.solve!(opti) value_getter = x -> CasADi.value(sol, x) @@ -361,28 +281,6 @@ function DiffEqBase.solve( value_getter = x -> CasADi.debug_value(opti, x) failed = true end - - ts = value_getter(tₛ) * U.t - U_vals = value_getter(U.u) - size(U_vals, 2) == 1 && (U_vals = U_vals') - U_vals = [[U_vals[i, j] for i in 1:size(U_vals, 1)] for j in 1:length(ts)] - ode_sol = DiffEqBase.build_solution(prob, tableau_getter, ts, U_vals) - - input_sol = nothing - if prod(size(V.u)) != 0 - V_vals = value_getter(V.u) - size(V_vals, 2) == 1 && (V_vals = V_vals') - V_vals = [[V_vals[i, j] for i in 1:size(V_vals, 1)] for j in 1:length(ts)] - input_sol = DiffEqBase.build_solution(prob, tableau_getter, ts, V_vals) - end - - if failed - ode_sol = SciMLBase.solution_new_retcode( - ode_sol, SciMLBase.ReturnCode.ConvergenceFailure) - !isnothing(input_sol) && (input_sol = SciMLBase.solution_new_retcode( - input_sol, SciMLBase.ReturnCode.ConvergenceFailure)) - end - - DynamicOptSolution(model, ode_sol, input_sol) end +MTK.successful_solve() = true end diff --git a/ext/MTKInfiniteOptExt.jl b/ext/MTKInfiniteOptExt.jl index 6dd80232f8..6e4a24ecae 100644 --- a/ext/MTKInfiniteOptExt.jl +++ b/ext/MTKInfiniteOptExt.jl @@ -9,13 +9,21 @@ import SymbolicUtils import NaNMath const MTK = ModelingToolkit +struct InfiniteOptModel + model::InfiniteModel + U::Vector{<:AbstractVariableRef} + V::Vector{<:AbstractVariableRef} + tₛ::AbstractVariableRef + is_free_final::Bool +end + struct JuMPDynamicOptProblem{uType, tType, isinplace, P, F, K} <: AbstractDynamicOptProblem{uType, tType, isinplace} f::F u0::uType tspan::tType p::P - model::InfiniteModel + model::InfiniteOptModel kwargs::K function JuMPDynamicOptProblem(f, u0, tspan, p, model, kwargs...) @@ -30,7 +38,7 @@ struct InfiniteOptDynamicOptProblem{uType, tType, isinplace, P, F, K} <: u0::uType tspan::tType p::P - model::InfiniteModel + model::InfiniteOptModel kwargs::K function InfiniteOptDynamicOptProblem(f, u0, tspan, p, model, kwargs...) @@ -39,27 +47,29 @@ struct InfiniteOptDynamicOptProblem{uType, tType, isinplace, P, F, K} <: end end -struct InfiniteOptModel - model::InfiniteModel - U::AbstractVariableRef - V::AbstractVariableRef - tₛ::Union - is_free_final::Bool -end - MTK.generate_internal_model(m::Type{InfiniteOptModel}) = InfiniteModel() -MTK.generate_state_variable!(m::InfiniteModel, u0::Vector, ns, nt) = @variable(m, U[i = 1:nt], Infinite(m[:t]), start=u0[i]) +MTK.generate_time_variable!(m::InfiniteModel, tspan, steps) = @infinite_parameter(m, t in [tspan[1], tspan[2]], num_supports = steps) +MTK.generate_state_variable!(m::InfiniteModel, u0::Vector, ns, nt) = @variable(m, U[i = 1:ns], Infinite(m[:t]), start=u0[i]) MTK.generate_input_variable!(m::InfiniteModel, c0, nc, nt) = @variable(m, V[i = 1:nc], Infinite(m[:t]), start=c0[i]) function MTK.generate_timescale!(m::InfiniteModel, guess, is_free_t) @variable(m, tₛ ≥ 0, start = guess) if !is_free_t - fix(m[:tₛ], 1) - set_start_value(m[:tₛ], 1) + fix(tₛ, 1, force=true) + set_start_value(tₛ, 1) end + tₛ end -MTK.add_constraint!(m::InfiniteOptModel, expr) = @constraint(m.model, expr) +function MTK.add_constraint!(m::InfiniteOptModel, expr::Union{Equation, Inequality}) + if expr isa Equation + @constraint(m.model, expr.lhs - expr.rhs == 0) + elseif expr.relational_op === Symbolics.geq + @constraint(m.model, expr.lhs - eq.rhs ≥ 0) + else + @constraint(m.model, expr.lhs - eq.rhs ≤ 0) + end +end MTK.set_objective!(m::InfiniteOptModel, expr) = @objective(m.model, Min, expr) """ @@ -82,7 +92,7 @@ function MTK.JuMPDynamicOptProblem(sys::System, u0map, tspan, pmap; dt = nothing, steps = nothing, guesses = Dict(), kwargs...) - process_DynamicOptProblem(JuMPDynamicOptProblem, InfiniteOptModel, u0map, tspan, pmap; dt, steps, guesses, kwargs...) + MTK.process_DynamicOptProblem(JuMPDynamicOptProblem, InfiniteOptModel, sys, u0map, tspan, pmap; dt, steps, guesses, kwargs...) end """ @@ -99,7 +109,7 @@ function MTK.InfiniteOptDynamicOptProblem(sys::System, u0map, tspan, pmap; dt = nothing, steps = nothing, guesses = Dict(), kwargs...) - prob = process_DynamicOptProblem(InfiniteOptDynamicOptProblem, InfiniteOptModel, u0map, tspan, pmap; dt, steps, guesses, kwargs...) + MTK.process_DynamicOptProblem(InfiniteOptDynamicOptProblem, InfiniteOptModel, sys, u0map, tspan, pmap; dt, steps, guesses, kwargs...) end function MTK.set_variable_bounds!(model, sys, pmap, tf) @@ -119,28 +129,28 @@ function MTK.set_variable_bounds!(model, sys, pmap, tf) end end - if symbolic_type(tf) === ScalarSymbolic() && hasbounds(tf) + if MTK.symbolic_type(tf) === MTK.ScalarSymbolic() && hasbounds(tf) lo, hi = MTK.getbounds(tf) set_lower_bound(model.tₛ, lo) set_upper_bound(model.tₛ, hi) end end -function MTK.substitute_integral(model, jcosts) - for int in MTK.collect_applied_operators(jcosts, Symbolics.Integral) +function MTK.substitute_integral(model, exprs) + intmap = Dict() + for int in MTK.collect_applied_operators(exprs, Symbolics.Integral) op = MTK.operation(int) arg = only(arguments(MTK.value(int))) - lo, hi = (op.domain.domain.left, op.domain.domain.right) - lo = MTK.value(lo) - hi = haskey(pmap, hi) ? 1 : MTK.value(hi) - intmap[int] = model.tₛ * InfiniteOpt.∫(arg, model.model.t, lo, hi) + lo, hi = MTK.value.((op.domain.domain.left, op.domain.domain.right)) + hi = (MTK.symbolic_type(hi) === MTK.ScalarSymbolic()) ? 1 : hi + intmap[int] = model.tₛ * InfiniteOpt.∫(arg, model.model[:t], lo, hi) end - jcosts = map(c -> Symbolics.substitute(c, intmap), jcosts) + exprs = map(c -> Symbolics.substitute(c, intmap), exprs) end -function MTK.add_initial_constraints!(model::InfiniteOptModel, u0, u0_idxs, ts) - U = model.U - @constraint(model, initial[i in u0_idxs], U[i](ts)==u0[i]) +function MTK.add_initial_constraints!(m::InfiniteOptModel, u0, u0_idxs, ts) + @show m.U + @constraint(m.model, initial[i in u0_idxs], m.U[i](ts)==u0[i]) end function MTK.substitute_model_vars(model, sys, exprs; tf = nothing) @@ -151,15 +161,15 @@ function MTK.substitute_model_vars(model, sys, exprs; tf = nothing) x_ops = [MTK.operation(MTK.unwrap(st)) for st in unknowns(sys)] c_ops = [MTK.operation(MTK.unwrap(ct)) for ct in MTK.unbound_inputs(sys)] - if symbolic_type(tf) === ScalarSymbolic() - free_t_map = Dict([[x(tf) => U[i](1) for (i, x) in enumerate(x_ops)]; - [c(tf) => V[i](1) for (i, c) in enumerate(c_ops)]]) + if MTK.symbolic_type(tf) === MTK.ScalarSymbolic() + free_t_map = Dict([[x(tf) => model.U[i](1) for (i, x) in enumerate(x_ops)]; + [c(tf) => model.V[i](1) for (i, c) in enumerate(c_ops)]]) exprs = map(c -> Symbolics.fast_substitute(c, free_t_map), exprs) end # for variables like x(1.0) - fixed_t_map = Dict([[x_ops[i] => U[i] for i in 1:length(U)]; - [c_ops[i] => V[i] for i in 1:length(V)]]) + fixed_t_map = Dict([[x_ops[i] => model.U[i] for i in 1:length(model.U)]; + [c_ops[i] => model.V[i] for i in 1:length(model.V)]]) exprs = map(c -> Symbolics.fast_substitute(c, fixed_t_map), exprs) end @@ -171,8 +181,8 @@ function MTK.substitute_differentials(model::InfiniteOptModel, eqs) map(e -> Symbolics.substitute(e, diffsubmap), diff_eqs) end -function add_solve_constraints!(prob::JuMPDynamicOptProblem, solver) - @unpack A, α, c = solver.tableau +function add_solve_constraints!(prob::JuMPDynamicOptProblem, tableau) + @unpack A, α, c = tableau @unpack model, f, p = prob tsteps = supports(model.model[:t]) dt = tsteps[2] - tsteps[1] @@ -184,7 +194,7 @@ function add_solve_constraints!(prob::JuMPDynamicOptProblem, solver) nᵥ = length(V) if MTK.is_explicit(tableau) K = Any[] - for τ in tsteps + for τ in tsteps[1:end-1] for (i, h) in enumerate(c) ΔU = sum([A[i, j] * K[j] for j in 1:(i - 1)], init = zeros(nᵤ)) Uₙ = [U[i](τ) + ΔU[i] * dt for i in 1:nᵤ] @@ -193,7 +203,7 @@ function add_solve_constraints!(prob::JuMPDynamicOptProblem, solver) push!(K, Kₙ) end ΔU = dt * sum([α[i] * K[i] for i in 1:length(α)]) - @constraint(model, [n = 1:nᵤ], U[n](τ) + ΔU[n]==U[n](τ + dt), + @constraint(model.model, [n = 1:nᵤ], U[n](τ) + ΔU[n]==U[n](τ + dt), base_name="solve_time_$τ") empty!(K) end @@ -201,14 +211,14 @@ function add_solve_constraints!(prob::JuMPDynamicOptProblem, solver) @variable(model, K[1:length(α), 1:nᵤ], Infinite(t)) ΔUs = A * K ΔU_tot = dt * (K' * α) - for τ in tsteps + for τ in tsteps[1:end-1] for (i, h) in enumerate(c) ΔU = @view ΔUs[i, :] Uₙ = U + ΔU * h * dt - @constraint(model, [j = 1:nᵤ], K[i, j]==(tₛ * f(Uₙ, V, p, τ + h * dt)[j]), + @constraint(model.model, [j = 1:nᵤ], K[i, j]==(tₛ * f(Uₙ, V, p, τ + h * dt)[j]), DomainRestrictions(t => τ), base_name="solve_K$i($τ)") end - @constraint(model, [n = 1:nᵤ], U[n](τ) + ΔU_tot[n]==U[n](min(τ + dt, tsteps[end])), + @constraint(model.model, [n = 1:nᵤ], U[n](τ) + ΔU_tot[n]==U[n](min(τ + dt, tsteps[end])), DomainRestrictions(t => τ), base_name="solve_U($τ)") end end @@ -221,22 +231,22 @@ JuMP Collocation solver. Returns a DynamicOptSolution, which contains both the model and the ODE solution. """ -struct JuMPCollocation +struct JuMPCollocation <: AbstractCollocation solver::Any tableau::DiffEqBase.ODERKTableau end -JuMPCollocation(solver; tableau = MTK.constructDefault()) = JuMPCollocation(solver, tableau) +MTK.JuMPCollocation(solver, tableau = MTK.constructDefault()) = JuMPCollocation(solver, tableau) """ InfiniteOpt Collocation solver. - solver: an optimization solver such as Ipopt - `derivative_method` kwarg refers to the method used by InfiniteOpt to compute derivatives. The list of possible options can be found at https://infiniteopt.github.io/InfiniteOpt.jl/stable/guide/derivative/. Defaults to FiniteDifference(Backward()). """ -struct InfiniteOptCollocation +struct InfiniteOptCollocation <: AbstractCollocation solver::Any derivative_method::InfiniteOpt.AbstractDerivativeMethod end -InfiniteOptCollocation(solver; derivative_method = InfiniteOpt.FiniteDifference(InfiniteOpt.Backward())) = InfiniteOptCollocation(solver, derivative_method) +MTK.InfiniteOptCollocation(solver, derivative_method = InfiniteOpt.FiniteDifference(InfiniteOpt.Backward())) = InfiniteOptCollocation(solver, derivative_method) function MTK.prepare_solver!(prob::JuMPDynamicOptProblem, solver::JuMPCollocation; verbose = false, kwargs...) model = prob.model.model @@ -255,7 +265,7 @@ function MTK.prepare_solver!(prob::JuMPDynamicOptProblem, solver::JuMPCollocatio delete(model, var) end end - add_collocation_solve_constraints!(model, solver.tableau) + add_solve_constraints!(prob, solver.tableau) set_optimizer(model, solver.solver) end @@ -273,19 +283,20 @@ function MTK.optimize_model!(prob::Union{InfiniteOptDynamicOptProblem, JuMPDynam end function MTK.get_V_values(m::InfiniteOptModel) + nt = length(supports(m.model[:t])) if !isempty(m.V) V_vals = value.(m.V) - V_vals = [[V_vals[i][j] for i in 1:length(V_vals)] for j in 1:length(ts)] + V_vals = [[V_vals[i][j] for i in 1:length(V_vals)] for j in 1:nt] else nothing end end function MTK.get_U_values(m::InfiniteOptModel) + nt = length(supports(m.model[:t])) U_vals = value.(m.U) - U_vals = [[U_vals[i][j] for i in 1:length(U_vals)] for j in 1:length(ts)] + U_vals = [[U_vals[i][j] for i in 1:length(U_vals)] for j in 1:nt] end - -MTK.get_t_values(model) = prob.tspan[1] .+ model.tₛ * supports(model.model[:t]) +MTK.get_t_values(model) = model.tₛ * supports(model.model[:t]) function MTK.successful_solve(m::InfiniteOptModel) model = m.model diff --git a/src/ModelingToolkit.jl b/src/ModelingToolkit.jl index 0e9962c241..9608c6083d 100644 --- a/src/ModelingToolkit.jl +++ b/src/ModelingToolkit.jl @@ -357,6 +357,8 @@ function FMIComponent end include("systems/optimal_control_interface.jl") export AbstractDynamicOptProblem, JuMPDynamicOptProblem, InfiniteOptDynamicOptProblem, CasADiDynamicOptProblem +export AbstractCollocation, JuMPCollocation, InfiniteOptCollocation, + CasADiCollocation export DynamicOptSolution @public apply_to_variables diff --git a/src/systems/optimal_control_interface.jl b/src/systems/optimal_control_interface.jl index 04c1a0c74b..3fb0fc91d9 100644 --- a/src/systems/optimal_control_interface.jl +++ b/src/systems/optimal_control_interface.jl @@ -165,23 +165,25 @@ function process_DynamicOptProblem(prob_type::Type{<:AbstractDynamicOptProblem}, dt = nothing, steps = nothing, guesses = Dict(), kwargs...) + warn_overdetermined(sys, u0map) + ctrls = unbound_inputs(sys) + states = unknowns(sys) - MTK.warn_overdetermined(sys, u0map) _u0map = has_alg_eqs(sys) ? u0map : merge(Dict(u0map), Dict(guesses)) - f, u0, p = MTK.process_SciMLProblem(ODEInputFunction, sys, _u0map, pmap; - t = tspan !== nothing ? tspan[1] : tspan, kwargs...) - stidxmap = Dict([v => i for (i, v) in enumerate(states)]) - u0map = Dict([MTK.default_toterm(MTK.value(k)) => v for (k, v) in u0map]) + u0map = Dict([default_toterm(value(k)) => v for (k, v) in u0map]) u0_idxs = has_alg_eqs(sys) ? collect(1:length(states)) : - [stidxmap[MTK.default_toterm(k)] for (k, v) in u0map] - pmap = Dict{Any, Any}(pmap) - model_tspan, steps, is_free_t = MTK.process_tspan(tspan, dt, steps) + [stidxmap[default_toterm(k)] for (k, v) in u0map] - ctrls = MTK.unbound_inputs(sys) - states = unknowns(sys) - c0 = MTK.value.([pmap[c] for c in ctrls]) + f, u0, p = process_SciMLProblem(ODEInputFunction, sys, _u0map, pmap; + t = tspan !== nothing ? tspan[1] : tspan, kwargs...) + model_tspan, steps, is_free_t = process_tspan(tspan, dt, steps) + pmap = recursive_unwrap(AnyDict(pmap)) + evaluate_varmap!(pmap, keys(pmap)) + c0 = value.([pmap[c] for c in ctrls]) + + tsteps = LinRange(model_tspan[1], model_tspan[2], steps) model = generate_internal_model(model_type) generate_time_variable!(model, model_tspan, steps) U = generate_state_variable!(model, u0, length(states), length(steps)) @@ -197,6 +199,7 @@ function process_DynamicOptProblem(prob_type::Type{<:AbstractDynamicOptProblem}, prob_type(f, u0, tspan, p, fullmodel, kwargs...) end +function generate_time_variable! end function generate_internal_model end function generate_state_variable! end function generate_input_variable! end @@ -207,8 +210,8 @@ function add_constraint! end is_free_final(model) = model.is_free_final function add_cost_function!(model, sys, tspan, pmap) - jcosts = copy(MTK.get_costs(sys)) - consolidate = MTK.get_consolidate(sys) + jcosts = copy(get_costs(sys)) + consolidate = get_consolidate(sys) if isnothing(jcosts) || isempty(jcosts) set_objective!(model, 0) return @@ -220,24 +223,19 @@ function add_cost_function!(model, sys, tspan, pmap) end function add_user_constraints!(model, sys, tspan, pmap) - conssys = MTK.get_constraintsystem(sys) - jconstraints = isnothing(conssys) ? nothing : MTK.get_constraints(conssys) + conssys = get_constraintsystem(sys) + jconstraints = isnothing(conssys) ? nothing : get_constraints(conssys) (isnothing(jconstraints) || isempty(jconstraints)) && return nothing - consvars = MTK.get_unknowns(conssys) + consvars = get_unknowns(conssys) is_free_final(model) && check_constraint_vars(consvars) - jconstraints = substitute_model_vars(model, sys, jcosts; tf = tspan[2]) jconstraints = substitute_toterm(consvars, jconstraints) jconstraints = substitute_params(pmap, jconstraints) + jconstraints = substitute_model_vars(model, sys, jconstraints; tf = tspan[2]) for c in jconstraints - if cons isa Equation - add_constraint!(model, c.lhs - c.rhs == 0) - elseif cons.relational_op === Symbolics.geq - add_constraint!(model, c.lhs - c.rhs ≥ 0) - else - add_constraint!(model, c.lhs - c.rhs ≤ 0) - end + @show c + add_constraint!(model, c) end end @@ -246,12 +244,12 @@ function add_equational_constraints!(model, sys, tspan) diff_eqs = substitute_model_vars(model, sys, diff_equations(sys); tf = tspan[2]) diff_eqs = substitute_differentials(model, sys, diff_eqs) for eq in diff_eqs - add_constraint!(model, eq.lhs == eq.rhs * model.tₛ) + add_constraint!(model, eq.lhs ~ eq.rhs * model.tₛ) end alg_eqs = substitute_model_vars(model, sys, alg_equations(sys); tf = tspan[2]) for eq in alg_eqs - add_constraint!(model, eq.lhs == eq.rhs * model.tₛ) + add_constraint!(model, eq.lhs ~ eq.rhs * model.tₛ) end end @@ -262,7 +260,7 @@ function substitute_integral end function substitute_differentials end function substitute_toterm(vars, exprs) - toterm_map = Dict([u => MTK.default_toterm(MTK.value(u)) for u in vars]) + toterm_map = Dict([u => default_toterm(value(u)) for u in vars]) exprs = map(c -> Symbolics.fast_substitute(c, toterm_map), exprs) end @@ -293,18 +291,24 @@ function get_U_values end function get_V_values end function successful_solve end +""" + solve(prob::AbstractDynamicOptProblem, solver::AbstractCollocation; verbose = false, kwargs...) + +- kwargs are used for other options. For example, the `plugin_options` and `solver_options` will propagated to the Opti object in CasADi. +""" function DiffEqBase.solve(prob::AbstractDynamicOptProblem, solver::AbstractCollocation; verbose = false, kwargs...) - solver = prepare_solver!(prob, solver) + solver = prepare_solver!(prob, solver; verbose, kwargs...) model = optimize_model!(prob, solver) ts = get_t_values(model) Us = get_U_values(model) Vs = get_V_values(model) + is_free_final(model) && (ts .+ tspan[1]) ode_sol = DiffEqBase.build_solution(prob, solver, ts, Us) input_sol = isnothing(Vs) ? nothing : DiffEqBase.build_solution(prob, solver, ts, Vs) - if !successful_solve(model) + if !successful_solve(model) ode_sol = SciMLBase.solution_new_retcode( ode_sol, SciMLBase.ReturnCode.ConvergenceFailure) !isnothing(input_sol) && (input_sol = SciMLBase.solution_new_retcode( diff --git a/test/extensions/dynamic_optimization.jl b/test/extensions/dynamic_optimization.jl index 03aeebf1bc..0d8ad71931 100644 --- a/test/extensions/dynamic_optimization.jl +++ b/test/extensions/dynamic_optimization.jl @@ -27,26 +27,22 @@ const M = ModelingToolkit # Test explicit method. jprob = JuMPDynamicOptProblem(sys, u0map, tspan, parammap, dt = 0.01) - @test InfiniteOpt.num_constraints(jprob.model) == 2 # initials - jsol = solve(jprob, Ipopt.Optimizer, constructRK4, silent = true) + jsol = solve(jprob, JuMPCollocation(Ipopt.Optimizer, constructRK4())) oprob = ODEProblem(sys, [u0map; parammap], tspan) osol = solve(oprob, SimpleRK4(), dt = 0.01) cprob = CasADiDynamicOptProblem(sys, u0map, tspan, parammap, dt = 0.01) - csol = solve(cprob, "ipopt", constructRK4) + csol = solve(cprob, CasADiCollocation("ipopt", constructRK4())) @test jsol.sol.u ≈ osol.u @test csol.sol.u ≈ osol.u # Implicit method. - jsol2 = solve(jprob, Ipopt.Optimizer, constructImplicitEuler, silent = true) # 63.031 ms, 26.49 MiB - osol2 = solve(oprob, ImplicitEuler(), dt = 0.01, adaptive = false) # 129.375 μs, 61.91 KiB - jsol2 = solve(jprob, Ipopt.Optimizer, constructImplicitEuler, silent = true) # 63.031 ms, 26.49 MiB + osol2 = solve(oprob, ImplicitEuler(), dt = 0.01, adaptive = false) + jsol2 = solve(jprob, JuMPCollocation(Ipopt.Optimizer, constructImplicitEuler())) @test ≈(jsol2.sol.u, osol2.u, rtol = 0.001) iprob = InfiniteOptDynamicOptProblem(sys, u0map, tspan, parammap, dt = 0.01) - isol = solve(iprob, Ipopt.Optimizer, - derivative_method = InfiniteOpt.FiniteDifference(InfiniteOpt.Backward()), - silent = true) # 11.540 ms, 4.00 MiB + isol = solve(iprob, InfiniteOptCollocation(Ipopt.Optimizer)) @test ≈(isol.sol.u, osol2.u, rtol = 0.001) - csol2 = solve(cprob, "ipopt", constructImplicitEuler, silent = true) + csol2 = solve(cprob, CasADiCollocation("ipopt", constructImplicitEuler())) @test ≈(csol2.sol.u, osol2.u, rtol = 0.001) # With a constraint @@ -57,20 +53,19 @@ const M = ModelingToolkit jprob = JuMPDynamicOptProblem(lksys, u0map, tspan, parammap; guesses = guess, dt = 0.01) @test InfiniteOpt.num_constraints(jprob.model) == 2 - jsol = solve(jprob, Ipopt.Optimizer, constructTsitouras5, silent = true) # 12.190 s, 9.68 GiB + jsol = solve(jprob, JuMPCollocation(Ipopt.Optimizer, constructTsitouras5())) @test jsol.sol(0.6; idxs = x(t)) ≈ 3.5 @test jsol.sol(0.3; idxs = x(t)) ≈ 7.0 cprob = CasADiDynamicOptProblem( lksys, u0map, tspan, parammap; guesses = guess, dt = 0.01) - csol = solve(cprob, "ipopt", constructTsitouras5, silent = true) + csol = solve(cprob, CasADiCollocation("ipopt", constructTsitouras5())) @test csol.sol(0.6; idxs = x(t)) ≈ 3.5 @test csol.sol(0.3; idxs = x(t)) ≈ 7.0 iprob = InfiniteOptDynamicOptProblem( lksys, u0map, tspan, parammap; guesses = guess, dt = 0.01) - isol = solve(iprob, Ipopt.Optimizer, - derivative_method = InfiniteOpt.OrthogonalCollocation(3), silent = true) # 48.564 ms, 9.58 MiB + isol = solve(iprob, InfiniteOptCollocation(Ipopt.Optimizer, InfiniteOpt.OrthogonalCollocation(3))) # 48.564 ms, 9.58 MiB sol = isol.sol @test sol(0.6; idxs = x(t)) ≈ 3.5 @test sol(0.3; idxs = x(t)) ≈ 7.0 @@ -80,17 +75,16 @@ const M = ModelingToolkit @mtkcompile lksys = System(eqs, t; constraints = constr) iprob = InfiniteOptDynamicOptProblem( lksys, u0map, tspan, parammap; guesses = guess, dt = 0.01) - isol = solve(iprob, Ipopt.Optimizer, - derivative_method = InfiniteOpt.OrthogonalCollocation(3), silent = true) + isol = solve(iprob, InfiniteOptCollocation(Ipopt.Optimizer, InfiniteOpt.OrthogonalCollocation(3))) @test all(u -> u > [1, 1], isol.sol.u) jprob = JuMPDynamicOptProblem(lksys, u0map, tspan, parammap; guesses = guess, dt = 0.01) - jsol = solve(jprob, Ipopt.Optimizer, constructRadauIA3, silent = true) # 12.190 s, 9.68 GiB + jsol = solve(jprob, JuMPCollocation(Ipopt.Optimizer, constructRadauIA3())) @test all(u -> u > [1, 1], jsol.sol.u) cprob = CasADiDynamicOptProblem( lksys, u0map, tspan, parammap; guesses = guess, dt = 0.01) - csol = solve(cprob, "ipopt", constructRadauIA3, silent = true) + csol = solve(cprob, CasADiCollocation("ipopt", constructRadauIA3())) @test all(u -> u > [1, 1], csol.sol.u) end @@ -124,14 +118,14 @@ end tspan = (0.0, 1.0) parammap = [u(t) => 0.0] jprob = JuMPDynamicOptProblem(block, u0map, tspan, parammap; dt = 0.01) - jsol = solve(jprob, Ipopt.Optimizer, constructVerner8, silent = true) + jsol = solve(jprob, JuMPCollocation(Ipopt.Optimizer, constructVerner8())) # Linear systems have bang-bang controls @test is_bangbang(jsol.input_sol, [-1.0], [1.0]) # Test reached final position. @test ≈(jsol.sol[x(t)][end], 0.25, rtol = 1e-5) cprob = CasADiDynamicOptProblem(block, u0map, tspan, parammap; dt = 0.01) - csol = solve(cprob, "ipopt", constructVerner8, silent = true) + csol = solve(cprob, CasADiCollocation("ipopt", constructVerner8())) # Linear systems have bang-bang controls @test is_bangbang(csol.input_sol, [-1.0], [1.0]) # Test reached final position. @@ -147,7 +141,7 @@ end @test ≈(csol.sol.u, osol.u, rtol = 0.05) iprob = InfiniteOptDynamicOptProblem(block, u0map, tspan, parammap; dt = 0.01) - isol = solve(iprob, Ipopt.Optimizer; silent = true) + isol = solve(iprob, InfiniteOptCollocation(Ipopt.Optimizer)) @test is_bangbang(isol.input_sol, [-1.0], [1.0]) @test ≈(isol.sol[x(t)][end], 0.25, rtol = 1e-5) osol = solve(oprob, ImplicitEuler(); dt = 0.01, adaptive = false) @@ -171,13 +165,13 @@ end pmap = [b => 1, c => 1, μ => 1, s => 1, ν => 1, α => 1] jprob = JuMPDynamicOptProblem(beesys, u0map, tspan, pmap, dt = 0.01) - jsol = solve(jprob, Ipopt.Optimizer, constructTsitouras5, silent = true) + jsol = solve(jprob, JuMPCollocation(Ipopt.Optimizer, constructTsitouras5())) @test is_bangbang(jsol.input_sol, [0.0], [1.0]) iprob = InfiniteOptDynamicOptProblem(beesys, u0map, tspan, pmap, dt = 0.01) - isol = solve(iprob, Ipopt.Optimizer; silent = true) + isol = solve(iprob, InfiniteOptCollocation(Ipopt.Optimizer)) @test is_bangbang(isol.input_sol, [0.0], [1.0]) cprob = CasADiDynamicOptProblem(beesys, u0map, tspan, pmap; dt = 0.01) - csol = solve(cprob, "ipopt", constructTsitouras5, silent = true) + csol = solve(cprob, CasADiCollocation("ipopt", constructTsitouras5())) @test is_bangbang(csol.input_sol, [0.0], [1.0]) @parameters (α_interp::LinearInterpolation)(..) @@ -265,15 +259,15 @@ end u0map = [x(t) => 17.5] pmap = [u(t) => 0.0, tf => 8] jprob = JuMPDynamicOptProblem(rocket, u0map, (0, tf), pmap; steps = 201) - jsol = solve(jprob, Ipopt.Optimizer, constructTsitouras5, silent = true) + jsol = solve(jprob, JuMPCollocation(Ipopt.Optimizer, constructTsitouras5())) @test isapprox(jsol.sol.t[end], 10.0, rtol = 1e-3) cprob = CasADiDynamicOptProblem(rocket, u0map, (0, tf), pmap; steps = 201) - csol = solve(cprob, "ipopt", constructTsitouras5, silent = true) + csol = solve(cprob, CasADiCollocation("ipopt", constructTsitouras5())) @test isapprox(csol.sol.t[end], 10.0, rtol = 1e-3) iprob = InfiniteOptDynamicOptProblem(rocket, u0map, (0, tf), pmap; steps = 200) - isol = solve(iprob, Ipopt.Optimizer, silent = true) + isol = solve(iprob, InfiniteOptCollocation(Ipopt.Optimizer)) @test isapprox(isol.sol.t[end], 10.0, rtol = 1e-3) @variables x(..) v(..) @@ -290,15 +284,15 @@ end tspan = (0.0, tf) parammap = [u(t) => 0.0, tf => 1.0] jprob = JuMPDynamicOptProblem(block, u0map, tspan, parammap; steps = 51) - jsol = solve(jprob, Ipopt.Optimizer, constructVerner8, silent = true) + jsol = solve(jprob, JuMPCollocation(Ipopt.Optimizer, constructVerner8())) @test isapprox(jsol.sol.t[end], 2.0, atol = 1e-5) cprob = CasADiDynamicOptProblem(block, u0map, (0, tf), parammap; steps = 51) - csol = solve(cprob, "ipopt", constructVerner8, silent = true) + csol = solve(cprob, CasADiCollocation("ipopt", constructVerner8())) @test isapprox(csol.sol.t[end], 2.0, atol = 1e-5) iprob = InfiniteOptDynamicOptProblem(block, u0map, tspan, parammap; steps = 51) - isol = solve(iprob, Ipopt.Optimizer, silent = true) + isol = solve(iprob, InfiniteOptCollocation(Ipopt.Optimizer)) @test isapprox(isol.sol.t[end], 2.0, atol = 1e-5) end @@ -332,7 +326,7 @@ end u0map = [D(x(t)) => 0.0, D(θ(t)) => 0.0, θ(t) => 0.0, x(t) => 0.0] pmap = [mₖ => 1.0, mₚ => 0.2, l => 0.5, g => 9.81, u => 0] jprob = JuMPDynamicOptProblem(cartpole, u0map, tspan, pmap; dt = 0.04) - jsol = solve(jprob, Ipopt.Optimizer, constructRK4, silent = true) + jsol = solve(jprob, JuMPCollocation(Ipopt.Optimizer, constructRK4())) @test jsol.sol.u[end] ≈ [π, 0, 0, 0] cprob = CasADiDynamicOptProblem(cartpole, u0map, tspan, pmap; dt = 0.04) @@ -340,6 +334,6 @@ end @test csol.sol.u[end] ≈ [π, 0, 0, 0] iprob = InfiniteOptDynamicOptProblem(cartpole, u0map, tspan, pmap; dt = 0.04) - isol = solve(iprob, Ipopt.Optimizer, silent = true) + isol = solve(iprob, InfiniteOptCollocation(Ipopt.Optimizer)) @test isol.sol.u[end] ≈ [π, 0, 0, 0] end From 4ca744a72b52485c728d1d0fc84add4294b2ba58 Mon Sep 17 00:00:00 2001 From: vyudu Date: Sat, 17 May 2025 17:16:34 -0400 Subject: [PATCH 1974/2176] correctly implement interface --- ext/MTKCasADiDynamicOptExt.jl | 175 +++++++++++++---------- ext/MTKInfiniteOptExt.jl | 59 ++++---- src/systems/optimal_control_interface.jl | 48 ++++--- test/extensions/dynamic_optimization.jl | 9 +- 4 files changed, 164 insertions(+), 127 deletions(-) diff --git a/ext/MTKCasADiDynamicOptExt.jl b/ext/MTKCasADiDynamicOptExt.jl index 554d9faec4..1dbb776615 100644 --- a/ext/MTKCasADiDynamicOptExt.jl +++ b/ext/MTKCasADiDynamicOptExt.jl @@ -18,12 +18,17 @@ struct MXLinearInterpolation dt::Float64 end -struct CasADiModel - opti::Opti +mutable struct CasADiModel + model::Opti U::MXLinearInterpolation V::MXLinearInterpolation tₛ::MX is_free_final::Bool + solver_opti::Union{Nothing, Opti} + + function CasADiModel(opti, U, V, tₛ, is_free_final, solver_opti = nothing) + new(opti, U, V, tₛ, is_free_final, solver_opti) + end end struct CasADiDynamicOptProblem{uType, tType, isinplace, P, F, K} <: @@ -74,24 +79,27 @@ function MTK.CasADiDynamicOptProblem(sys::System, u0map, tspan, pmap; dt = nothing, steps = nothing, guesses = Dict(), kwargs...) - process_DynamicOptProblem(CasADiDynamicOptProblem, CasADiModel, sys, u0map, tspan, pmap; dt, steps, guesses, kwargs...) + MTK.process_DynamicOptProblem(CasADiDynamicOptProblem, CasADiModel, sys, u0map, tspan, pmap; dt, steps, guesses, kwargs...) end -MTK.generate_internal_model(::Type{CasADiModel}) = CasADi.opti() +MTK.generate_internal_model(::Type{CasADiModel}) = CasADi.Opti() +MTK.generate_time_variable!(opti::Opti, args...) = nothing -function MTK.generate_state_variable(model::Opti, u0, ns, nt, tsteps) +function MTK.generate_state_variable!(model::Opti, u0, ns, tsteps) + nt = length(tsteps) U = CasADi.variable!(model, ns, nt) - set_initial!(opti, U, DM(repeat(u0, 1, steps))) + set_initial!(model, U, DM(repeat(u0, 1, nt))) MXLinearInterpolation(U, tsteps, tsteps[2] - tsteps[1]) end -function MTK.generate_input_variable(model::Opti, c0, nc, nt, tsteps) +function MTK.generate_input_variable!(model::Opti, c0, nc, tsteps) + nt = length(tsteps) V = CasADi.variable!(model, nc, nt) - !isempty(c0) && set_initial!(opti, V, DM(repeat(c0, 1, steps))) + !isempty(c0) && set_initial!(model, V, DM(repeat(c0, 1, nt))) MXLinearInterpolation(V, tsteps, tsteps[2] - tsteps[1]) end -function MTK.generate_timescale(model::Opti, guess, is_free_t) +function MTK.generate_timescale!(model::Opti, guess, is_free_t) if is_free_t tₛ = variable!(model) set_initial!(model, tₛ, guess) @@ -102,78 +110,73 @@ function MTK.generate_timescale(model::Opti, guess, is_free_t) end end -function MTK.add_constraint!(model::CasADiModel, expr) - @unpack opti = model - if cons isa Equation - subject_to!(opti, expr.lhs - expr.rhs == 0) - elseif cons.relational_op === Symbolics.geq - subject_to!(opti, expr.lhs - expr.rhs ≥ 0) +function MTK.add_constraint!(m::CasADiModel, expr) + if expr isa Equation + subject_to!(m.model, expr.lhs - expr.rhs == 0) + elseif expr.relational_op === Symbolics.geq + subject_to!(m.model, expr.lhs - expr.rhs ≥ 0) else - subject_to!(opti, expr.lhs - expr.rhs ≤ 0) + subject_to!(m.model, expr.lhs - expr.rhs ≤ 0) end end -MTK.set_objective!(model::CasADiModel, expr) = minimize!(model.opti, MX(expr)) +MTK.set_objective!(m::CasADiModel, expr) = minimize!(m.model, MX(expr)) -function MTK.set_variable_bounds!(model, sys, pmap, tf) - @unpack opti, U, V = model +function MTK.set_variable_bounds!(m::CasADiModel, sys, pmap, tf) + @unpack model, U, tₛ, V = m for (i, u) in enumerate(unknowns(sys)) if MTK.hasbounds(u) lo, hi = MTK.getbounds(u) - subject_to!(opti, Symbolics.fast_substitute(lo, pmap) <= U.u[i, :]) - subject_to!(opti, U.u[i, :] <= Symbolics.fast_substitute(hi, pmap)) + subject_to!(model, Symbolics.fixpoint_sub(lo, pmap) <= U.u[i, :]) + subject_to!(model, U.u[i, :] <= Symbolics.fixpoint_sub(hi, pmap)) end end for (i, v) in enumerate(MTK.unbound_inputs(sys)) if MTK.hasbounds(v) lo, hi = MTK.getbounds(v) - subject_to!(opti, Symbolics.fast_substitute(lo, pmap) <= V.u[i, :]) - subject_to!(opti, V.u[i, :] <= Symbolics.fast_substitute(hi, pmap)) + subject_to!(model, Symbolics.fixpoint_sub(lo, pmap) <= V.u[i, :]) + subject_to!(model, V.u[i, :] <= Symbolics.fixpoint_sub(hi, pmap)) end end if MTK.symbolic_type(tf) === MTK.ScalarSymbolic() && hasbounds(tf) lo, hi = MTK.getbounds(tf) - subject_to!(opti, model.tₛ >= lo) - subject_to!(opti, model.tₛ <= hi) + subject_to!(model, tₛ >= lo) + subject_to!(model, tₛ <= hi) end end -function MTK.add_initial_constraints!(model::CasADiModel, u0, u0_idxs) - @unpack opti, U = model +function MTK.add_initial_constraints!(m::CasADiModel, u0, u0_idxs, args...) + @unpack model, U = m for i in u0_idxs - subject_to!(opti, U.u[i, 1] == u0[i]) + subject_to!(model, U.u[i, 1] == u0[i]) end end -function MTK.substitute_model_vars( - model::CasADiModel, sys, pmap, exprs; auxmap::Dict = Dict(), is_free_t) - @unpack opti, U, V, tₛ = model +function MTK.substitute_model_vars(m::CasADiModel, sys, exprs, tspan) + @unpack model, U, V, tₛ = m iv = MTK.get_iv(sys) sts = unknowns(sys) cts = MTK.unbound_inputs(sys) - x_ops = [MTK.operation(MTK.unwrap(st)) for st in sts] c_ops = [MTK.operation(MTK.unwrap(ct)) for ct in cts] - - exprs = map(c -> Symbolics.fast_substitute(c, auxmap), exprs) - exprs = map(c -> Symbolics.fast_substitute(c, Dict(pmap)), exprs) - # tf means different things in different contexts; a [tf] in a cost function - # should be tₛ, while a x(tf) should translate to x[1] - if is_free_t - free_t_map = Dict([[x(tₛ) => U.u[i, end] for (i, x) in enumerate(x_ops)]; - [c(tₛ) => V.u[i, end] for (i, c) in enumerate(c_ops)]]) + (ti, tf) = tspan + if MTK.is_free_final(m) + _tf = tₛ + ti + exprs = map(c -> Symbolics.fast_substitute(c, Dict(tf => _tf)), exprs) + free_t_map = Dict([[x(_tf) => U.u[i, end] for (i, x) in enumerate(x_ops)]; + [c(_tf) => V.u[i, end] for (i, c) in enumerate(c_ops)]]) exprs = map(c -> Symbolics.fast_substitute(c, free_t_map), exprs) end - exprs = substitute_fixed_t_vars(exprs) - - # for variables like x(t) + exprs = substitute_fixed_t_vars(m, sys, exprs) whole_interval_map = Dict([[v => U.u[i, :] for (i, v) in enumerate(sts)]; [v => V.u[i, :] for (i, v) in enumerate(cts)]]) exprs = map(c -> Symbolics.fast_substitute(c, whole_interval_map), exprs) - exprs end -function substitute_fixed_t_vars(exprs) +function substitute_fixed_t_vars(model::CasADiModel, sys, exprs) + stidxmap = Dict([v => i for (i, v) in enumerate(unknowns(sys))]) + ctidxmap = Dict([v => i for (i, v) in enumerate(MTK.unbound_inputs(sys))]) + iv = MTK.get_iv(sys) for i in 1:length(exprs) subvars = MTK.vars(exprs[i]) for st in subvars @@ -183,27 +186,28 @@ function substitute_fixed_t_vars(exprs) MTK.symbolic_type(t) === MTK.NotSymbolic() || continue if haskey(stidxmap, x(iv)) idx = stidxmap[x(iv)] - cv = U + cv = model.U else idx = ctidxmap[x(iv)] - cv = V + cv = model.V end exprs[i] = Symbolics.fast_substitute(exprs[i], Dict(x(t) => cv(t)[idx])) end jcosts = Symbolics.substitute(jcosts, Dict(x(t) => cv(t)[idx])) end + exprs end -MTK.substitute_differentials(model::CasADiModel, exprs, args...) = exprs +MTK.substitute_differentials(model::CasADiModel, sys, eqs) = exprs -function MTK.substitute_integral(model::CasADiModel, exprs) - @unpack U, opti = model +function MTK.substitute_integral(m::CasADiModel, exprs, tspan) + @unpack U, model, tₛ = m dt = U.t[2] - U.t[1] intmap = Dict() for int in MTK.collect_applied_operators(exprs, Symbolics.Integral) op = MTK.operation(int) arg = only(arguments(MTK.value(int))) - lo, hi = (op.domain.domain.left, op.domain.domain.right) + lo, hi = MTK.value.((op.domain.domain.left, op.domain.domain.right)) !isequal((lo, hi), tspan) && error("Non-whole interval bounds for integrals are not currently supported for CasADiDynamicOptProblem.") # Approximate integral as sum. @@ -213,11 +217,11 @@ function MTK.substitute_integral(model::CasADiModel, exprs) exprs = MTK.value.(exprs) end -function add_solve_constraints!(prob, tableau) +function add_solve_constraints!(prob::CasADiDynamicOptProblem, tableau) @unpack A, α, c = tableau @unpack model, f, p = prob - @unpack opti, U, V, tₛ = model - solver_opti = copy(opti) + @unpack model, U, V, tₛ = model + solver_opti = copy(model) tsteps = U.t dt = tsteps[2] - tsteps[1] @@ -258,29 +262,56 @@ function add_solve_constraints!(prob, tableau) solver_opti end -function MTK.prepare_solver() - opti = add_solve_constraints(prob, tableau) - solver!(opti, "$solver", plugin_options, solver_options) +""" +CasADi Collocation solver. +- solver: an optimization solver such as Ipopt. Should be given as a string or symbol in all lowercase, e.g. "ipopt" +- tableau: An ODE RK tableau. Load a tableau by calling a function like `constructRK4` and may be found at https://docs.sciml.ai/DiffEqDevDocs/stable/internals/tableaus/. If this argument is not passed in, the solver will default to Radau second order. +""" +struct CasADiCollocation <: AbstractCollocation + solver::Union{String, Symbol} + tableau::DiffEqBase.ODERKTableau +end +MTK.CasADiCollocation(solver, tableau = MTK.constructDefault()) = CasADiCollocation(solver, tableau) + +function MTK.prepare_and_optimize!(prob::CasADiDynamicOptProblem, solver::CasADiCollocation; verbose = false, solver_options = Dict(), plugin_options = Dict(), kwargs...) + solver_opti = add_solve_constraints!(prob, solver.tableau) + verbose || (solver_options["print_level"] = 0) + solver!(solver_opti, "$(solver.solver)", plugin_options, solver_options) + try + CasADi.solve!(solver_opti) + catch ErrorException + end + prob.model.solver_opti = solver_opti end -function MTK.get_U_values() - U_vals = value_getter(U.u) + +function MTK.get_U_values(model::CasADiModel) + value_getter = MTK.successful_solve(model) ? CasADi.debug_value : CasADi.value + (nu, nt) = size(model.U.u) + U_vals = value_getter(model.solver_opti, model.U.u) size(U_vals, 2) == 1 && (U_vals = U_vals') - U_vals = [[U_vals[i, j] for i in 1:size(U_vals, 1)] for j in 1:length(ts)] + U_vals = [[U_vals[i, j] for i in 1:nu] for j in 1:nt] end -function MTK.get_V_values() + +function MTK.get_V_values(model::CasADiModel) + value_getter = MTK.successful_solve(model) ? CasADi.debug_value : CasADi.value + (nu, nt) = size(model.V.u) + if nu*nt != 0 + V_vals = value_getter(model.solver_opti, model.V.u) + size(V_vals, 2) == 1 && (V_vals = V_vals') + V_vals = [[V_vals[i, j] for i in 1:nu] for j in 1:nt] + else + nothing + end end -function MTK.get_t_values() - ts = value_getter(tₛ) * U.t + +function MTK.get_t_values(model::CasADiModel) + value_getter = MTK.successful_solve(model) ? CasADi.debug_value : CasADi.value + ts = value_getter(model.solver_opti, model.tₛ) .* model.U.t end -function MTK.optimize_model!() - try - sol = CasADi.solve!(opti) - value_getter = x -> CasADi.value(sol, x) - catch ErrorException - value_getter = x -> CasADi.debug_value(opti, x) - failed = true - end +function MTK.successful_solve(m::CasADiModel) + isnothing(m.solver_opti) && return false + retcode = CasADi.return_status(m.solver_opti) + retcode == "Solve_Succeeded" || retcode == "Solved_To_Acceptable_Level" end -MTK.successful_solve() = true end diff --git a/ext/MTKInfiniteOptExt.jl b/ext/MTKInfiniteOptExt.jl index 6e4a24ecae..4a2f025f00 100644 --- a/ext/MTKInfiniteOptExt.jl +++ b/ext/MTKInfiniteOptExt.jl @@ -49,8 +49,8 @@ end MTK.generate_internal_model(m::Type{InfiniteOptModel}) = InfiniteModel() MTK.generate_time_variable!(m::InfiniteModel, tspan, steps) = @infinite_parameter(m, t in [tspan[1], tspan[2]], num_supports = steps) -MTK.generate_state_variable!(m::InfiniteModel, u0::Vector, ns, nt) = @variable(m, U[i = 1:ns], Infinite(m[:t]), start=u0[i]) -MTK.generate_input_variable!(m::InfiniteModel, c0, nc, nt) = @variable(m, V[i = 1:nc], Infinite(m[:t]), start=c0[i]) +MTK.generate_state_variable!(m::InfiniteModel, u0::Vector, ns, ts) = @variable(m, U[i = 1:ns], Infinite(m[:t]), start=u0[i]) +MTK.generate_input_variable!(m::InfiniteModel, c0, nc, ts) = @variable(m, V[i = 1:nc], Infinite(m[:t]), start=c0[i]) function MTK.generate_timescale!(m::InfiniteModel, guess, is_free_t) @variable(m, tₛ ≥ 0, start = guess) @@ -65,9 +65,9 @@ function MTK.add_constraint!(m::InfiniteOptModel, expr::Union{Equation, Inequali if expr isa Equation @constraint(m.model, expr.lhs - expr.rhs == 0) elseif expr.relational_op === Symbolics.geq - @constraint(m.model, expr.lhs - eq.rhs ≥ 0) + @constraint(m.model, expr.lhs - expr.rhs ≥ 0) else - @constraint(m.model, expr.lhs - eq.rhs ≤ 0) + @constraint(m.model, expr.lhs - expr.rhs ≤ 0) end end MTK.set_objective!(m::InfiniteOptModel, expr) = @objective(m.model, Min, expr) @@ -109,10 +109,12 @@ function MTK.InfiniteOptDynamicOptProblem(sys::System, u0map, tspan, pmap; dt = nothing, steps = nothing, guesses = Dict(), kwargs...) - MTK.process_DynamicOptProblem(InfiniteOptDynamicOptProblem, InfiniteOptModel, sys, u0map, tspan, pmap; dt, steps, guesses, kwargs...) + prob = MTK.process_DynamicOptProblem(InfiniteOptDynamicOptProblem, InfiniteOptModel, sys, u0map, tspan, pmap; dt, steps, guesses, kwargs...) + MTK.add_equational_constraints!(prob.model, sys, pmap, tspan) + prob end -function MTK.set_variable_bounds!(model, sys, pmap, tf) +function MTK.set_variable_bounds!(model::InfiniteOptModel, sys, pmap, tf) for (i, u) in enumerate(unknowns(sys)) if MTK.hasbounds(u) lo, hi = MTK.getbounds(u) @@ -136,24 +138,27 @@ function MTK.set_variable_bounds!(model, sys, pmap, tf) end end -function MTK.substitute_integral(model, exprs) +function MTK.substitute_integral(model, exprs, tspan) intmap = Dict() for int in MTK.collect_applied_operators(exprs, Symbolics.Integral) op = MTK.operation(int) arg = only(arguments(MTK.value(int))) lo, hi = MTK.value.((op.domain.domain.left, op.domain.domain.right)) - hi = (MTK.symbolic_type(hi) === MTK.ScalarSymbolic()) ? 1 : hi + if MTK.is_free_final(model) && isequal((lo, hi), tspan) + (lo, hi) = (0, 1) + elseif MTK.is_free_final(model) + error("Free final time problems cannot handle partial timespans.") + end intmap[int] = model.tₛ * InfiniteOpt.∫(arg, model.model[:t], lo, hi) end exprs = map(c -> Symbolics.substitute(c, intmap), exprs) end function MTK.add_initial_constraints!(m::InfiniteOptModel, u0, u0_idxs, ts) - @show m.U @constraint(m.model, initial[i in u0_idxs], m.U[i](ts)==u0[i]) end -function MTK.substitute_model_vars(model, sys, exprs; tf = nothing) +function MTK.substitute_model_vars(model::InfiniteOptModel, sys, exprs, tspan) whole_interval_map = Dict([[v => model.U[i] for (i, v) in enumerate(unknowns(sys))]; [v => model.V[i] for (i, v) in enumerate(MTK.unbound_inputs(sys))]]) exprs = map(c -> Symbolics.fast_substitute(c, whole_interval_map), exprs) @@ -161,9 +166,12 @@ function MTK.substitute_model_vars(model, sys, exprs; tf = nothing) x_ops = [MTK.operation(MTK.unwrap(st)) for st in unknowns(sys)] c_ops = [MTK.operation(MTK.unwrap(ct)) for ct in MTK.unbound_inputs(sys)] + (ti, tf) = tspan if MTK.symbolic_type(tf) === MTK.ScalarSymbolic() - free_t_map = Dict([[x(tf) => model.U[i](1) for (i, x) in enumerate(x_ops)]; - [c(tf) => model.V[i](1) for (i, c) in enumerate(c_ops)]]) + _tf = model.tₛ + ti + exprs = map(c -> Symbolics.fast_substitute(c, Dict(tf => _tf)), exprs) + free_t_map = Dict([[x(_tf) => model.U[i](1) for (i, x) in enumerate(x_ops)]; + [c(_tf) => model.V[i](1) for (i, c) in enumerate(c_ops)]]) exprs = map(c -> Symbolics.fast_substitute(c, free_t_map), exprs) end @@ -173,18 +181,19 @@ function MTK.substitute_model_vars(model, sys, exprs; tf = nothing) exprs = map(c -> Symbolics.fast_substitute(c, fixed_t_map), exprs) end -function MTK.substitute_differentials(model::InfiniteOptModel, eqs) +function MTK.substitute_differentials(model::InfiniteOptModel, sys, eqs) U = model.U t = model.model[:t] D = Differential(MTK.get_iv(sys)) diffsubmap = Dict([D(U[i]) => ∂(U[i], t) for i in 1:length(U)]) - map(e -> Symbolics.substitute(e, diffsubmap), diff_eqs) + map(e -> Symbolics.substitute(e, diffsubmap), eqs) end function add_solve_constraints!(prob::JuMPDynamicOptProblem, tableau) @unpack A, α, c = tableau @unpack model, f, p = prob - tsteps = supports(model.model[:t]) + t = model.model[:t] + tsteps = supports(t) dt = tsteps[2] - tsteps[1] tₛ = model.tₛ @@ -208,7 +217,7 @@ function add_solve_constraints!(prob::JuMPDynamicOptProblem, tableau) empty!(K) end else - @variable(model, K[1:length(α), 1:nᵤ], Infinite(t)) + @variable(model.model, K[1:length(α), 1:nᵤ], Infinite(t)) ΔUs = A * K ΔU_tot = dt * (K' * α) for τ in tsteps[1:end-1] @@ -228,8 +237,6 @@ end JuMP Collocation solver. - solver: a optimization solver such as Ipopt - tableau: An ODE RK tableau. Load a tableau by calling a function like `constructRK4` and may be found at https://docs.sciml.ai/DiffEqDevDocs/stable/internals/tableaus/. If this argument is not passed in, the solver will default to Radau second order. - -Returns a DynamicOptSolution, which contains both the model and the ODE solution. """ struct JuMPCollocation <: AbstractCollocation solver::Any @@ -240,7 +247,7 @@ MTK.JuMPCollocation(solver, tableau = MTK.constructDefault()) = JuMPCollocation( """ InfiniteOpt Collocation solver. - solver: an optimization solver such as Ipopt -- `derivative_method` kwarg refers to the method used by InfiniteOpt to compute derivatives. The list of possible options can be found at https://infiniteopt.github.io/InfiniteOpt.jl/stable/guide/derivative/. Defaults to FiniteDifference(Backward()). +- `derivative_method`: the method used by InfiniteOpt to compute derivatives. The list of possible options can be found at https://infiniteopt.github.io/InfiniteOpt.jl/stable/guide/derivative/. Defaults to FiniteDifference(Backward()). """ struct InfiniteOptCollocation <: AbstractCollocation solver::Any @@ -248,7 +255,7 @@ struct InfiniteOptCollocation <: AbstractCollocation end MTK.InfiniteOptCollocation(solver, derivative_method = InfiniteOpt.FiniteDifference(InfiniteOpt.Backward())) = InfiniteOptCollocation(solver, derivative_method) -function MTK.prepare_solver!(prob::JuMPDynamicOptProblem, solver::JuMPCollocation; verbose = false, kwargs...) +function MTK.prepare_and_optimize!(prob::JuMPDynamicOptProblem, solver::JuMPCollocation; verbose = false, kwargs...) model = prob.model.model verbose || set_silent(model) # Unregister current solver constraints @@ -267,19 +274,15 @@ function MTK.prepare_solver!(prob::JuMPDynamicOptProblem, solver::JuMPCollocatio end add_solve_constraints!(prob, solver.tableau) set_optimizer(model, solver.solver) + optimize!(model) end -function MTK.prepare_solver!(prob::InfiniteOptDynamicOptProblem, solver::InfiniteOptCollocation; verbose = false, kwargs...) +function MTK.prepare_and_optimize!(prob::InfiniteOptDynamicOptProblem, solver::InfiniteOptCollocation; verbose = false, kwargs...) model = prob.model.model verbose || set_silent(model) - add_equational_constraints!(model, prob.f.sys, prob.tspan) set_derivative_method(model[:t], solver.derivative_method) set_optimizer(model, solver.solver) -end - -function MTK.optimize_model!(prob::Union{InfiniteOptDynamicOptProblem, JuMPDynamicOptProblem}, solver) - optimize!(prob.model.model) - prob.model + optimize!(model) end function MTK.get_V_values(m::InfiniteOptModel) @@ -296,7 +299,7 @@ function MTK.get_U_values(m::InfiniteOptModel) U_vals = value.(m.U) U_vals = [[U_vals[i][j] for i in 1:length(U_vals)] for j in 1:nt] end -MTK.get_t_values(model) = model.tₛ * supports(model.model[:t]) +MTK.get_t_values(model) = value(model.tₛ) * supports(model.model[:t]) function MTK.successful_solve(m::InfiniteOptModel) model = m.model diff --git a/src/systems/optimal_control_interface.jl b/src/systems/optimal_control_interface.jl index 3fb0fc91d9..92e5467980 100644 --- a/src/systems/optimal_control_interface.jl +++ b/src/systems/optimal_control_interface.jl @@ -186,8 +186,8 @@ function process_DynamicOptProblem(prob_type::Type{<:AbstractDynamicOptProblem}, tsteps = LinRange(model_tspan[1], model_tspan[2], steps) model = generate_internal_model(model_type) generate_time_variable!(model, model_tspan, steps) - U = generate_state_variable!(model, u0, length(states), length(steps)) - V = generate_input_variable!(model, c0, length(ctrls), length(steps)) + U = generate_state_variable!(model, u0, length(states), tsteps) + V = generate_input_variable!(model, c0, length(ctrls), tsteps) tₛ = generate_timescale!(model, get(pmap, tspan[2], tspan[2]), is_free_t) fullmodel = model_type(model, U, V, tₛ, is_free_t) @@ -216,9 +216,9 @@ function add_cost_function!(model, sys, tspan, pmap) set_objective!(model, 0) return end - jcosts = substitute_model_vars(model, sys, jcosts; tf = tspan[2]) + jcosts = substitute_model_vars(model, sys, jcosts, tspan) jcosts = substitute_params(pmap, jcosts) - jcosts = substitute_integral(model, jcosts) + jcosts = substitute_integral(model, jcosts, tspan) set_objective!(model, consolidate(jcosts)) end @@ -230,24 +230,24 @@ function add_user_constraints!(model, sys, tspan, pmap) is_free_final(model) && check_constraint_vars(consvars) jconstraints = substitute_toterm(consvars, jconstraints) + jconstraints = substitute_model_vars(model, sys, jconstraints, tspan) jconstraints = substitute_params(pmap, jconstraints) - jconstraints = substitute_model_vars(model, sys, jconstraints; tf = tspan[2]) for c in jconstraints - @show c add_constraint!(model, c) end end -function add_equational_constraints!(model, sys, tspan) - model = model.model - diff_eqs = substitute_model_vars(model, sys, diff_equations(sys); tf = tspan[2]) +function add_equational_constraints!(model, sys, pmap, tspan) + diff_eqs = substitute_model_vars(model, sys, diff_equations(sys), tspan) + diff_eqs = substitute_params(pmap, diff_eqs) diff_eqs = substitute_differentials(model, sys, diff_eqs) for eq in diff_eqs add_constraint!(model, eq.lhs ~ eq.rhs * model.tₛ) end - alg_eqs = substitute_model_vars(model, sys, alg_equations(sys); tf = tspan[2]) + alg_eqs = substitute_model_vars(model, sys, alg_equations(sys), tspan) + alg_eqs = substitute_params(pmap, alg_eqs) for eq in alg_eqs add_constraint!(model, eq.lhs ~ eq.rhs * model.tₛ) end @@ -256,6 +256,12 @@ end function set_objective! end """Substitute variables like x(1.5) with the corresponding model variables.""" function substitute_model_vars end +""" +Substitute integrals. For an integral from (ts, te): +- Free final time problems should transcribe this to (0, 1) in the case that (ts, te) is the original timespan. Free final time problems cannot handle partial timespans. +- CasADi cannot handle partial timespans, even for non-free-final time problems. +time problems and unchanged otherwise. +""" function substitute_integral end function substitute_differentials end @@ -265,7 +271,7 @@ function substitute_toterm(vars, exprs) end function substitute_params(pmap, exprs) - exprs = map(c -> Symbolics.fast_substitute(c, Dict(pmap)), exprs) + exprs = map(c -> Symbolics.fixpoint_sub(c, Dict(pmap)), exprs) end function check_constraint_vars(vars) @@ -282,10 +288,9 @@ end ### SOLVER UTILITIES ### ######################## """ -Add the solve constraints, set the solver (Ipopt, e.g.) and solver options. +Add the solve constraints, set the solver (Ipopt, e.g.) and solver options, optimize the model. """ -function prepare_solver! end -function optimize_model! end +function prepare_and_optimize! end function get_t_values end function get_U_values end function get_V_values end @@ -297,22 +302,21 @@ function successful_solve end - kwargs are used for other options. For example, the `plugin_options` and `solver_options` will propagated to the Opti object in CasADi. """ function DiffEqBase.solve(prob::AbstractDynamicOptProblem, solver::AbstractCollocation; verbose = false, kwargs...) - solver = prepare_solver!(prob, solver; verbose, kwargs...) - model = optimize_model!(prob, solver) + prepare_and_optimize!(prob, solver; verbose, kwargs...) - ts = get_t_values(model) - Us = get_U_values(model) - Vs = get_V_values(model) - is_free_final(model) && (ts .+ tspan[1]) + ts = get_t_values(prob.model) + Us = get_U_values(prob.model) + Vs = get_V_values(prob.model) + is_free_final(prob.model) && (ts .+ prob.tspan[1]) ode_sol = DiffEqBase.build_solution(prob, solver, ts, Us) input_sol = isnothing(Vs) ? nothing : DiffEqBase.build_solution(prob, solver, ts, Vs) - if !successful_solve(model) + if !successful_solve(prob.model) ode_sol = SciMLBase.solution_new_retcode( ode_sol, SciMLBase.ReturnCode.ConvergenceFailure) !isnothing(input_sol) && (input_sol = SciMLBase.solution_new_retcode( input_sol, SciMLBase.ReturnCode.ConvergenceFailure)) end - DynamicOptSolution(model, ode_sol, input_sol) + DynamicOptSolution(prob.model.model, ode_sol, input_sol) end diff --git a/test/extensions/dynamic_optimization.jl b/test/extensions/dynamic_optimization.jl index 0d8ad71931..2d24f92d1f 100644 --- a/test/extensions/dynamic_optimization.jl +++ b/test/extensions/dynamic_optimization.jl @@ -52,7 +52,6 @@ const M = ModelingToolkit @mtkcompile lksys = System(eqs, t; constraints = constr) jprob = JuMPDynamicOptProblem(lksys, u0map, tspan, parammap; guesses = guess, dt = 0.01) - @test InfiniteOpt.num_constraints(jprob.model) == 2 jsol = solve(jprob, JuMPCollocation(Ipopt.Optimizer, constructTsitouras5())) @test jsol.sol(0.6; idxs = x(t)) ≈ 3.5 @test jsol.sol(0.3; idxs = x(t)) ≈ 7.0 @@ -213,16 +212,16 @@ end g₀ => 1, m₀ => 1.0, h_c => 500, c => 0.5 * √(g₀ * h₀), D_c => 0.5 * 620 * m₀ / g₀, Tₘ => 3.5 * g₀ * m₀, T(t) => 0.0, h₀ => 1, m_c => 0.6] jprob = JuMPDynamicOptProblem(rocket, u0map, (ts, te), pmap; dt = 0.001, cse = false) - jsol = solve(jprob, Ipopt.Optimizer, constructRadauIIA5, silent = true) + jsol = solve(jprob, JuMPCollocation(Ipopt.Optimizer, constructRadauIIA5())) @test jsol.sol[h(t)][end] > 1.012 cprob = CasADiDynamicOptProblem(rocket, u0map, (ts, te), pmap; dt = 0.001, cse = false) - csol = solve(cprob, "ipopt"; silent = true) + csol = solve(cprob, CasADiCollocation("ipopt")) @test csol.sol[h(t)][end] > 1.012 iprob = InfiniteOptDynamicOptProblem( rocket, u0map, (ts, te), pmap; dt = 0.001) - isol = solve(iprob, Ipopt.Optimizer, silent = true) + isol = solve(iprob, InfiniteOptCollocation(Ipopt.Optimizer)) @test isol.sol[h(t)][end] > 1.012 # Test solution @@ -330,7 +329,7 @@ end @test jsol.sol.u[end] ≈ [π, 0, 0, 0] cprob = CasADiDynamicOptProblem(cartpole, u0map, tspan, pmap; dt = 0.04) - csol = solve(cprob, "ipopt", constructRK4, silent = true) + csol = solve(cprob, CasADiCollocation("ipopt", constructRK4())) @test csol.sol.u[end] ≈ [π, 0, 0, 0] iprob = InfiniteOptDynamicOptProblem(cartpole, u0map, tspan, pmap; dt = 0.04) From 8d268e9f1064f92f415a3a5426b9c9ec677bd442 Mon Sep 17 00:00:00 2001 From: vyudu Date: Mon, 19 May 2025 14:54:06 -0400 Subject: [PATCH 1975/2176] refactor: move set_variable_bounds to interface function --- ext/MTKCasADiDynamicOptExt.jl | 37 ++++------------- ext/MTKInfiniteOptExt.jl | 52 ++++++------------------ src/systems/optimal_control_interface.jl | 36 +++++++++++++--- 3 files changed, 51 insertions(+), 74 deletions(-) diff --git a/ext/MTKCasADiDynamicOptExt.jl b/ext/MTKCasADiDynamicOptExt.jl index 1dbb776615..2dbd184683 100644 --- a/ext/MTKCasADiDynamicOptExt.jl +++ b/ext/MTKCasADiDynamicOptExt.jl @@ -17,6 +17,7 @@ struct MXLinearInterpolation t::Vector{Float64} dt::Float64 end +Base.getindex(m::MXLinearInterpolation, i...) = length(i) == length(size(m.u)) ? m.u[i...] : m.u[i..., :] mutable struct CasADiModel model::Opti @@ -37,7 +38,7 @@ struct CasADiDynamicOptProblem{uType, tType, isinplace, P, F, K} <: u0::uType tspan::tType p::P - model::CasADiModel + wrapped_model::CasADiModel kwargs::K function CasADiDynamicOptProblem(f, u0, tspan, p, model, kwargs...) @@ -52,10 +53,11 @@ function (M::MXLinearInterpolation)(τ) Δ = nt - i + 1 (i > length(M.t) || i < 1) && error("Cannot extrapolate past the tspan.") + colons = ntuple(_ -> (:), length(size(M.u)) - 1) if i < length(M.t) - M.u[:, i] + Δ * (M.u[:, i + 1] - M.u[:, i]) + M.u[colons..., i] + Δ*(M.u[colons..., i+1] - M.u[colons..., i]) else - M.u[:, i] + M.u[colons..., i] end end @@ -121,29 +123,6 @@ function MTK.add_constraint!(m::CasADiModel, expr) end MTK.set_objective!(m::CasADiModel, expr) = minimize!(m.model, MX(expr)) -function MTK.set_variable_bounds!(m::CasADiModel, sys, pmap, tf) - @unpack model, U, tₛ, V = m - for (i, u) in enumerate(unknowns(sys)) - if MTK.hasbounds(u) - lo, hi = MTK.getbounds(u) - subject_to!(model, Symbolics.fixpoint_sub(lo, pmap) <= U.u[i, :]) - subject_to!(model, U.u[i, :] <= Symbolics.fixpoint_sub(hi, pmap)) - end - end - for (i, v) in enumerate(MTK.unbound_inputs(sys)) - if MTK.hasbounds(v) - lo, hi = MTK.getbounds(v) - subject_to!(model, Symbolics.fixpoint_sub(lo, pmap) <= V.u[i, :]) - subject_to!(model, V.u[i, :] <= Symbolics.fixpoint_sub(hi, pmap)) - end - end - if MTK.symbolic_type(tf) === MTK.ScalarSymbolic() && hasbounds(tf) - lo, hi = MTK.getbounds(tf) - subject_to!(model, tₛ >= lo) - subject_to!(model, tₛ <= hi) - end -end - function MTK.add_initial_constraints!(m::CasADiModel, u0, u0_idxs, args...) @unpack model, U = m for i in u0_idxs @@ -219,8 +198,8 @@ end function add_solve_constraints!(prob::CasADiDynamicOptProblem, tableau) @unpack A, α, c = tableau - @unpack model, f, p = prob - @unpack model, U, V, tₛ = model + @unpack wrapped_model, f, p = prob + @unpack model, U, V, tₛ = wrapped_model solver_opti = copy(model) tsteps = U.t @@ -281,7 +260,7 @@ function MTK.prepare_and_optimize!(prob::CasADiDynamicOptProblem, solver::CasADi CasADi.solve!(solver_opti) catch ErrorException end - prob.model.solver_opti = solver_opti + prob.wrapped_model.solver_opti = solver_opti end function MTK.get_U_values(model::CasADiModel) diff --git a/ext/MTKInfiniteOptExt.jl b/ext/MTKInfiniteOptExt.jl index 4a2f025f00..db9eb3f3a0 100644 --- a/ext/MTKInfiniteOptExt.jl +++ b/ext/MTKInfiniteOptExt.jl @@ -23,7 +23,7 @@ struct JuMPDynamicOptProblem{uType, tType, isinplace, P, F, K} <: u0::uType tspan::tType p::P - model::InfiniteOptModel + wrapped_model::InfiniteOptModel kwargs::K function JuMPDynamicOptProblem(f, u0, tspan, p, model, kwargs...) @@ -38,7 +38,7 @@ struct InfiniteOptDynamicOptProblem{uType, tType, isinplace, P, F, K} <: u0::uType tspan::tType p::P - model::InfiniteOptModel + wrapped_model::InfiniteOptModel kwargs::K function InfiniteOptDynamicOptProblem(f, u0, tspan, p, model, kwargs...) @@ -110,34 +110,10 @@ function MTK.InfiniteOptDynamicOptProblem(sys::System, u0map, tspan, pmap; steps = nothing, guesses = Dict(), kwargs...) prob = MTK.process_DynamicOptProblem(InfiniteOptDynamicOptProblem, InfiniteOptModel, sys, u0map, tspan, pmap; dt, steps, guesses, kwargs...) - MTK.add_equational_constraints!(prob.model, sys, pmap, tspan) + MTK.add_equational_constraints!(prob.wrapped_model, sys, pmap, tspan) prob end -function MTK.set_variable_bounds!(model::InfiniteOptModel, sys, pmap, tf) - for (i, u) in enumerate(unknowns(sys)) - if MTK.hasbounds(u) - lo, hi = MTK.getbounds(u) - set_lower_bound(model.U[i], Symbolics.fixpoint_sub(lo, pmap)) - set_upper_bound(model.U[i], Symbolics.fixpoint_sub(hi, pmap)) - end - end - - for (i, v) in enumerate(MTK.unbound_inputs(sys)) - if MTK.hasbounds(v) - lo, hi = MTK.getbounds(v) - set_lower_bound(model.V[i], Symbolics.fixpoint_sub(lo, pmap)) - set_upper_bound(model.V[i], Symbolics.fixpoint_sub(hi, pmap)) - end - end - - if MTK.symbolic_type(tf) === MTK.ScalarSymbolic() && hasbounds(tf) - lo, hi = MTK.getbounds(tf) - set_lower_bound(model.tₛ, lo) - set_upper_bound(model.tₛ, hi) - end -end - function MTK.substitute_integral(model, exprs, tspan) intmap = Dict() for int in MTK.collect_applied_operators(exprs, Symbolics.Integral) @@ -191,14 +167,12 @@ end function add_solve_constraints!(prob::JuMPDynamicOptProblem, tableau) @unpack A, α, c = tableau - @unpack model, f, p = prob - t = model.model[:t] + @unpack wrapped_model, f, p = prob + @unpack tₛ, U, V, model = wrapped_model + t = model[:t] tsteps = supports(t) dt = tsteps[2] - tsteps[1] - tₛ = model.tₛ - U = model.U - V = model.V nᵤ = length(U) nᵥ = length(V) if MTK.is_explicit(tableau) @@ -212,22 +186,22 @@ function add_solve_constraints!(prob::JuMPDynamicOptProblem, tableau) push!(K, Kₙ) end ΔU = dt * sum([α[i] * K[i] for i in 1:length(α)]) - @constraint(model.model, [n = 1:nᵤ], U[n](τ) + ΔU[n]==U[n](τ + dt), + @constraint(model, [n = 1:nᵤ], U[n](τ) + ΔU[n]==U[n](τ + dt), base_name="solve_time_$τ") empty!(K) end else - @variable(model.model, K[1:length(α), 1:nᵤ], Infinite(t)) + K = @variable(model, K[1:length(α), 1:nᵤ], Infinite(model[:t])) ΔUs = A * K ΔU_tot = dt * (K' * α) for τ in tsteps[1:end-1] for (i, h) in enumerate(c) ΔU = @view ΔUs[i, :] - Uₙ = U + ΔU * h * dt - @constraint(model.model, [j = 1:nᵤ], K[i, j]==(tₛ * f(Uₙ, V, p, τ + h * dt)[j]), + Uₙ = U + ΔU * dt + @constraint(model, [j = 1:nᵤ], K[i, j]==(tₛ * f(Uₙ, V, p, τ + h * dt)[j]), DomainRestrictions(t => τ), base_name="solve_K$i($τ)") end - @constraint(model.model, [n = 1:nᵤ], U[n](τ) + ΔU_tot[n]==U[n](min(τ + dt, tsteps[end])), + @constraint(model, [n = 1:nᵤ], U[n](τ) + ΔU_tot[n]==U[n](min(τ + dt, tsteps[end])), DomainRestrictions(t => τ), base_name="solve_U($τ)") end end @@ -256,7 +230,7 @@ end MTK.InfiniteOptCollocation(solver, derivative_method = InfiniteOpt.FiniteDifference(InfiniteOpt.Backward())) = InfiniteOptCollocation(solver, derivative_method) function MTK.prepare_and_optimize!(prob::JuMPDynamicOptProblem, solver::JuMPCollocation; verbose = false, kwargs...) - model = prob.model.model + model = prob.wrapped_model.model verbose || set_silent(model) # Unregister current solver constraints for con in all_constraints(model) @@ -278,7 +252,7 @@ function MTK.prepare_and_optimize!(prob::JuMPDynamicOptProblem, solver::JuMPColl end function MTK.prepare_and_optimize!(prob::InfiniteOptDynamicOptProblem, solver::InfiniteOptCollocation; verbose = false, kwargs...) - model = prob.model.model + model = prob.wrapped_model.model verbose || set_silent(model) set_derivative_method(model[:t], solver.derivative_method) set_optimizer(model, solver.solver) diff --git a/src/systems/optimal_control_interface.jl b/src/systems/optimal_control_interface.jl index 92e5467980..b91c76182f 100644 --- a/src/systems/optimal_control_interface.jl +++ b/src/systems/optimal_control_interface.jl @@ -207,6 +207,30 @@ function generate_timescale! end function set_variable_bounds! end function add_initial_constraints! end function add_constraint! end + +function set_variable_bounds!(m, sys, pmap, tf) + @unpack model, U, V, tₛ = m + for (i, u) in enumerate(unknowns(sys)) + if hasbounds(u) + lo, hi = getbounds(u) + add_constraint!(m, U[i] ≳ Symbolics.fixpoint_sub(lo, pmap)) + add_constraint!(m, U[i] ≲ Symbolics.fixpoint_sub(hi, pmap)) + end + end + for (i, v) in enumerate(unbound_inputs(sys)) + if hasbounds(v) + lo, hi = getbounds(v) + add_constraint!(m, V[i] ≳ Symbolics.fixpoint_sub(lo, pmap)) + add_constraint!(m, V[i] ≲ Symbolics.fixpoint_sub(hi, pmap)) + end + end + if symbolic_type(tf) === ScalarSymbolic() && hasbounds(tf) + lo, hi = getbounds(tf) + set_lower_bound(tₛ, Symbolics.fixpoint_sub(lo, pmap)) + set_upper_bound(tₛ, Symbolics.fixpoint_sub(hi, pmap)) + end +end + is_free_final(model) = model.is_free_final function add_cost_function!(model, sys, tspan, pmap) @@ -304,19 +328,19 @@ function successful_solve end function DiffEqBase.solve(prob::AbstractDynamicOptProblem, solver::AbstractCollocation; verbose = false, kwargs...) prepare_and_optimize!(prob, solver; verbose, kwargs...) - ts = get_t_values(prob.model) - Us = get_U_values(prob.model) - Vs = get_V_values(prob.model) - is_free_final(prob.model) && (ts .+ prob.tspan[1]) + ts = get_t_values(prob.wrapped_model) + Us = get_U_values(prob.wrapped_model) + Vs = get_V_values(prob.wrapped_model) + is_free_final(prob.wrapped_model) && (ts .+ prob.tspan[1]) ode_sol = DiffEqBase.build_solution(prob, solver, ts, Us) input_sol = isnothing(Vs) ? nothing : DiffEqBase.build_solution(prob, solver, ts, Vs) - if !successful_solve(prob.model) + if !successful_solve(prob.wrapped_model) ode_sol = SciMLBase.solution_new_retcode( ode_sol, SciMLBase.ReturnCode.ConvergenceFailure) !isnothing(input_sol) && (input_sol = SciMLBase.solution_new_retcode( input_sol, SciMLBase.ReturnCode.ConvergenceFailure)) end - DynamicOptSolution(prob.model.model, ode_sol, input_sol) + DynamicOptSolution(prob.wrapped_model.model, ode_sol, input_sol) end From b5b0f4456da373a2c864279ccc170684225469de Mon Sep 17 00:00:00 2001 From: vyudu Date: Mon, 19 May 2025 18:00:34 -0400 Subject: [PATCH 1976/2176] refactor: centralize substitute_differentiasl and substitute_integral --- ext/MTKCasADiDynamicOptExt.jl | 59 ++----- ext/MTKInfiniteOptExt.jl | 66 +++---- ext/MTKPyomoDynamicOptExt.jl | 209 +++++++++++++++++++++++ src/systems/optimal_control_interface.jl | 61 +++++-- 4 files changed, 299 insertions(+), 96 deletions(-) create mode 100644 ext/MTKPyomoDynamicOptExt.jl diff --git a/ext/MTKCasADiDynamicOptExt.jl b/ext/MTKCasADiDynamicOptExt.jl index 2dbd184683..f70b69a59d 100644 --- a/ext/MTKCasADiDynamicOptExt.jl +++ b/ext/MTKCasADiDynamicOptExt.jl @@ -130,32 +130,20 @@ function MTK.add_initial_constraints!(m::CasADiModel, u0, u0_idxs, args...) end end -function MTK.substitute_model_vars(m::CasADiModel, sys, exprs, tspan) - @unpack model, U, V, tₛ = m - iv = MTK.get_iv(sys) - sts = unknowns(sys) - cts = MTK.unbound_inputs(sys) - x_ops = [MTK.operation(MTK.unwrap(st)) for st in sts] - c_ops = [MTK.operation(MTK.unwrap(ct)) for ct in cts] - (ti, tf) = tspan - if MTK.is_free_final(m) - _tf = tₛ + ti - exprs = map(c -> Symbolics.fast_substitute(c, Dict(tf => _tf)), exprs) - free_t_map = Dict([[x(_tf) => U.u[i, end] for (i, x) in enumerate(x_ops)]; - [c(_tf) => V.u[i, end] for (i, c) in enumerate(c_ops)]]) - exprs = map(c -> Symbolics.fast_substitute(c, free_t_map), exprs) - end +function free_t_map(model, tf, x_ops, c_ops) + Dict([[x(tf) => model.U.u[i, end] for (i, x) in enumerate(x_ops)]; + [c(tf) => model.V.u[i, end] for (i, c) in enumerate(c_ops)]]) +end + +function whole_t_map(model, sys) + Dict([[v => model.U.u[i, :] for (i, v) in enumerate(unknowns(sys))]; + [v => model.V.u[i, :] for (i, v) in enumerate(MTK.unbound_inputs(sys))]]) - exprs = substitute_fixed_t_vars(m, sys, exprs) - whole_interval_map = Dict([[v => U.u[i, :] for (i, v) in enumerate(sts)]; - [v => V.u[i, :] for (i, v) in enumerate(cts)]]) - exprs = map(c -> Symbolics.fast_substitute(c, whole_interval_map), exprs) end -function substitute_fixed_t_vars(model::CasADiModel, sys, exprs) - stidxmap = Dict([v => i for (i, v) in enumerate(unknowns(sys))]) - ctidxmap = Dict([v => i for (i, v) in enumerate(MTK.unbound_inputs(sys))]) - iv = MTK.get_iv(sys) +function fixed_t_map(model::CasADiModel, x_ops, c_ops, exprs) + stidxmap = Dict([v => i for (i, v) in x_ops]) + ctidxmap = Dict([v => i for (i, v) in c_ops]) for i in 1:length(exprs) subvars = MTK.vars(exprs[i]) for st in subvars @@ -163,11 +151,11 @@ function substitute_fixed_t_vars(model::CasADiModel, sys, exprs) x = operation(st) t = only(arguments(st)) MTK.symbolic_type(t) === MTK.NotSymbolic() || continue - if haskey(stidxmap, x(iv)) - idx = stidxmap[x(iv)] + if haskey(stidxmap, x) + idx = stidxmap[x] cv = model.U else - idx = ctidxmap[x(iv)] + idx = ctidxmap[x] cv = model.V end exprs[i] = Symbolics.fast_substitute(exprs[i], Dict(x(t) => cv(t)[idx])) @@ -177,24 +165,7 @@ function substitute_fixed_t_vars(model::CasADiModel, sys, exprs) exprs end -MTK.substitute_differentials(model::CasADiModel, sys, eqs) = exprs - -function MTK.substitute_integral(m::CasADiModel, exprs, tspan) - @unpack U, model, tₛ = m - dt = U.t[2] - U.t[1] - intmap = Dict() - for int in MTK.collect_applied_operators(exprs, Symbolics.Integral) - op = MTK.operation(int) - arg = only(arguments(MTK.value(int))) - lo, hi = MTK.value.((op.domain.domain.left, op.domain.domain.right)) - !isequal((lo, hi), tspan) && - error("Non-whole interval bounds for integrals are not currently supported for CasADiDynamicOptProblem.") - # Approximate integral as sum. - intmap[int] = dt * tₛ * sum(arg) - end - exprs = map(c -> Symbolics.substitute(c, intmap), exprs) - exprs = MTK.value.(exprs) -end +MTK.lowered_integral(model, expr, args...) = model.tₛ * (model.U.t[2] - model.U.t[1]) * expr function add_solve_constraints!(prob::CasADiDynamicOptProblem, tableau) @unpack A, α, c = tableau diff --git a/ext/MTKInfiniteOptExt.jl b/ext/MTKInfiniteOptExt.jl index db9eb3f3a0..61f4ec70b3 100644 --- a/ext/MTKInfiniteOptExt.jl +++ b/ext/MTKInfiniteOptExt.jl @@ -48,7 +48,7 @@ struct InfiniteOptDynamicOptProblem{uType, tType, isinplace, P, F, K} <: end MTK.generate_internal_model(m::Type{InfiniteOptModel}) = InfiniteModel() -MTK.generate_time_variable!(m::InfiniteModel, tspan, steps) = @infinite_parameter(m, t in [tspan[1], tspan[2]], num_supports = steps) +MTK.generate_time_variable!(m::InfiniteModel, tspan, steps) = @infinite_parameter(m, t in [tspan[1], tspan[2]], num_supports = length(tsteps)) MTK.generate_state_variable!(m::InfiniteModel, u0::Vector, ns, ts) = @variable(m, U[i = 1:ns], Infinite(m[:t]), start=u0[i]) MTK.generate_input_variable!(m::InfiniteModel, c0, nc, ts) = @variable(m, V[i = 1:nc], Infinite(m[:t]), start=c0[i]) @@ -114,55 +114,39 @@ function MTK.InfiniteOptDynamicOptProblem(sys::System, u0map, tspan, pmap; prob end -function MTK.substitute_integral(model, exprs, tspan) - intmap = Dict() - for int in MTK.collect_applied_operators(exprs, Symbolics.Integral) - op = MTK.operation(int) - arg = only(arguments(MTK.value(int))) - lo, hi = MTK.value.((op.domain.domain.left, op.domain.domain.right)) - if MTK.is_free_final(model) && isequal((lo, hi), tspan) - (lo, hi) = (0, 1) - elseif MTK.is_free_final(model) - error("Free final time problems cannot handle partial timespans.") - end - intmap[int] = model.tₛ * InfiniteOpt.∫(arg, model.model[:t], lo, hi) +MTK.lowered_integral(model, expr, lo, hi) = model.tₛ * InfiniteOpt.∫(arg, model.model[:t], lo, hi) + +function MTK.process_integral_bounds(model, integral_span, tspan) + if MTK.is_free_final(model) && isequal(integral_span, tspan) + integral_span = (0, 1) + elseif MTK.is_free_final(model) + error("Free final time problems cannot handle partial timespans.") + else + integral_span end - exprs = map(c -> Symbolics.substitute(c, intmap), exprs) end function MTK.add_initial_constraints!(m::InfiniteOptModel, u0, u0_idxs, ts) - @constraint(m.model, initial[i in u0_idxs], m.U[i](ts)==u0[i]) + for i in u0_idxs + fix(m.U[i], u0[i], force = true) + end end -function MTK.substitute_model_vars(model::InfiniteOptModel, sys, exprs, tspan) - whole_interval_map = Dict([[v => model.U[i] for (i, v) in enumerate(unknowns(sys))]; - [v => model.V[i] for (i, v) in enumerate(MTK.unbound_inputs(sys))]]) - exprs = map(c -> Symbolics.fast_substitute(c, whole_interval_map), exprs) - - x_ops = [MTK.operation(MTK.unwrap(st)) for st in unknowns(sys)] - c_ops = [MTK.operation(MTK.unwrap(ct)) for ct in MTK.unbound_inputs(sys)] - - (ti, tf) = tspan - if MTK.symbolic_type(tf) === MTK.ScalarSymbolic() - _tf = model.tₛ + ti - exprs = map(c -> Symbolics.fast_substitute(c, Dict(tf => _tf)), exprs) - free_t_map = Dict([[x(_tf) => model.U[i](1) for (i, x) in enumerate(x_ops)]; - [c(_tf) => model.V[i](1) for (i, c) in enumerate(c_ops)]]) - exprs = map(c -> Symbolics.fast_substitute(c, free_t_map), exprs) - end +MTK.lowered_derivative(model, i) = ∂(model.U[i], model.model[:t]) + +function MTK.fixed_t_map(model::InfiniteOptModel, x_ops, c_ops, exprs) + Dict([[x_ops[i] => model.U[i] for i in 1:length(model.U)]; + [c_ops[i] => model.V[i] for i in 1:length(model.V)]]) +end - # for variables like x(1.0) - fixed_t_map = Dict([[x_ops[i] => model.U[i] for i in 1:length(model.U)]; - [c_ops[i] => model.V[i] for i in 1:length(model.V)]]) - exprs = map(c -> Symbolics.fast_substitute(c, fixed_t_map), exprs) +function MTK.free_t_map(model::InfiniteOptModel, tf, x_ops, c_ops) + Dict([[x(tf) => model.U[i](1) for (i, x) in enumerate(x_ops)]; + [c(tf) => model.V[i](1) for (i, c) in enumerate(c_ops)]]) end -function MTK.substitute_differentials(model::InfiniteOptModel, sys, eqs) - U = model.U - t = model.model[:t] - D = Differential(MTK.get_iv(sys)) - diffsubmap = Dict([D(U[i]) => ∂(U[i], t) for i in 1:length(U)]) - map(e -> Symbolics.substitute(e, diffsubmap), eqs) +function MTK.whole_t_map(model::InfiniteOptModel, sys) + whole_interval_map = Dict([[v => model.U[i] for (i, v) in enumerate(unknowns(sys))]; + [v => model.V[i] for (i, v) in enumerate(MTK.unbound_inputs(sys))]]) end function add_solve_constraints!(prob::JuMPDynamicOptProblem, tableau) diff --git a/ext/MTKPyomoDynamicOptExt.jl b/ext/MTKPyomoDynamicOptExt.jl new file mode 100644 index 0000000000..859c17d72a --- /dev/null +++ b/ext/MTKPyomoDynamicOptExt.jl @@ -0,0 +1,209 @@ +module MTKPyomoDynamicOptExt +using ModelingToolkit +using PythonCall +using DiffEqBase +using UnPack +using NaNMath +const MTK = ModelingToolkit + +# import pyomo +const pyomo = PythonCall.pynew() +PythonCall.pycopy!(pyomo, pyimport("pyomo.environ")) + +struct PyomoDAEVar + v::Py +end +(v::PyomoDAEVar)(t) = v.v[:, t] +getindex(v::PyomoDAEVar, i::Union{Num, Symbolic}, t::Union{Num, Symbolic}) = wrap(Term{symeltype(A)}(getindex, [A, unwrap(i), unwrap(t)])) + +for ff in [acos, log1p, acosh, log2, asin, tan, atanh, cos, log, sin, log10, sqrt] + f = nameof(ff) + @eval NaNMath.$f(x::PyomoDAEVar) = Base.$f(x) +end + +const SymbolicConcreteModel = Symbolics.symstruct(ConcreteModel) + +struct PyomoModel + model::ConcreteModel + U + V + tₛ::Union{Int} + is_free_final::Bool + model_sym::SymbolicConcreteModel + t_sym::Union{Num, BasicSymbolic} + idx_sym::Union{Num, BasicSymbolic} + + function PyomoModel(model, U, V, tₛ, is_free_final) + @variables MODEL_SYM::SymbolicConcreteModel IDX_SYM::Int T_SYM + PyomoModel(model, U, V, tₛ, is_free_final, MODEL_SYM, T_SYM, INDEX_SYM) + end +end + +struct PyomoDynamicOptProblem{uType, tType, isinplace, P, F, K} <: + AbstractDynamicOptProblem{uType, tType, isinplace} + f::F + u0::uType + tspan::tType + p::P + wrapped_model::ConcreteModel + kwargs::K + + function PyomoDynamicOptProblem(f, u0, tspan, p, model, kwargs...) + new{typeof(u0), typeof(tspan), SciMLBase.isinplace(f, 5), + typeof(p), typeof(f), typeof(kwargs)}(f, u0, tspan, p, model, kwargs) + end +end + +""" + PyomoDynamicOptProblem(sys::ODESystem, u0, tspan, p; dt, steps) + +Convert an ODESystem representing an optimal control system into a Pyomo model +for solving using optimization. Must provide either `dt`, the timestep between collocation +points (which, along with the timespan, determines the number of points), or directly +provide the number of points as `steps`. +""" +function MTK.PyomoDynamicOptProblem(sys::ODESystem, u0map, tspan, pmap; + dt = nothing, + steps = nothing, + guesses = Dict(), kwargs...) + prob = MTK.process_DynamicOptProblem(PyomoDynamicOptProblem, PyomoModel, sys, u0map, tspan, pmap; dt, steps, guesses, kwargs...) + MTK.add_equational_constraints!(prob.wrapped_model, sys, pmap, tspan) + prob +end + +MTK.generate_internal_model(m::Type{PyomoModel}) = pyomo.ConcreteModel() +function MTK.generate_time_variable!(m::ConcreteModel, tspan, tsteps) + m.t = pyomo.ContinuousSet(initialize = collect(tsteps), bounds = tspan) +end + +function MTK.generate_state_variable!(m::ConcreteModel, u0, ns, ts) + m.u_idxs = pyomo.RangeSet(1, ns) + pyomo.Var(m.u_idxs, m.t) +end + +function MTK.generate_input_variable!(m::ConcreteModel, u0, nc, ts) + m.v_idxs = pyomo.RangeSet(1, nc) + pyomo.Var(m.v_idxs, m.t) +end + +function MTK.generate_timescale(m::ConcreteModel, guess, is_free_t) + m.tₛ = is_free_t ? pyomo.Var(initialize = guess, bounds = (0, Inf)) : 1 +end + +function MTK.add_constraint!(pmodel::PyomoModel, cons) + @unpack model, model_sym, idx_sym, t_sym = pmodel + expr = if cons isa Equation + cons.lhs - cons.rhs == 0 + elseif cons.relational_op === Symbolics.geq + cons.lhs - cons.rhs ≥ 0 + else + cons.lhs - cons.rhs ≤ 0 + end + constraint_f = Symbolics.build_function(expr, model_sym, idx_sym, t_sym) + pyomo.Constraint(rule = constraint_f) +end + +function MTK.set_objective!(m::PyomoModel, expr) = pyomo.Objective(expr = expr) + +function add_initial_constraints!(model::PyomoModel, u0, u0_idxs) + for i in u0_idxs + model.U[i, 0].fix(u0[i]) + end +end + +function substitute_fixed_t_vars!(model::PyomoModel, sys, exprs) + stidxmap = Dict([v => i for (i, v) in enumerate(unknowns(sys))]) + ctidxmap = Dict([v => i for (i, v) in enumerate(MTK.unbound_inputs(sys))]) + iv = MTK.get_iv(sys) + + for cons in jconstraints + consvars = MTK.vars(cons) + for st in consvars + MTK.iscall(st) || continue + x = MTK.operation(st) + t = only(MTK.arguments(st)) + MTK.symbolic_type(t) === MTK.NotSymbolic() || continue + if haskey(stidxmap, x(iv)) + idx = stidxmap[x(iv)] + cv = :U + else + idx = ctidxmap[x(iv)] + cv = :V + end + model.t.add(t) + cons = Symbolics.substitute(cons, Dict(x(t) => model.cv[idx, t])) + end + end +end + +function MTK.substitute_model_vars(pmodel::PyomoModel, sys, pmap, exprs, tspan) + @unwrap model, model_sym, idx_sym, t_sym = pmodel + x_ops = [MTK.operation(MTK.unwrap(st)) for st in unknowns(sys)] + c_ops = [MTK.operation(MTK.unwrap(ct)) for ct in MTK.unbound_inputs(sys)] + mU = Symbolics.symbolic_getproperty(model_sym, :U) + mV = Symbolics.symbolic_getproperty(model_sym, :V) + + (ti, tf) = tspan + if MTK.symbolic_type(tf) === MTK.ScalarSymbolic() + _tf = model.tₛ + ti + exprs = map(c -> Symbolics.fast_substitute(c, Dict(tf => _tf)), exprs) + free_t_map = Dict([[x(tₛ) => mU[i, end] for (i, x) in enumerate(x_ops)]; + [c(tₛ) => mV[i, end] for (i, c) in enumerate(c_ops)]]) + exprs = map(c -> Symbolics.fixpoint_sub(c, free_t_map), exprs) + end + + whole_interval_map = Dict([[v => mU[i, t_sym] for (i, v) in enumerate(sts)]; + [v => mV[i, t_sym] for (i, v) in enumerate(cts)]]) + exprs = map(c -> Symbolics.fixpoint_sub(c, whole_interval_map), exprs) + exprs +end + +function MTK.substitute_integral!(model::PyomoModel, exprs, tspan) + intmap = Dict() + for int in MTK.collect_applied_operators(exprs, Symbolics.Integral) + op = MTK.operation(int) + arg = only(arguments(MTK.value(int))) + lo, hi = MTK.value.((op.domain.domain.left, op.domain.domain.right)) + if MTK.is_free_final(model) && isequal((lo, hi), tspan) + (lo, hi) = (0, 1) + elseif MTK.is_free_final(model) + error("Free final time problems cannot handle partial timespans.") + end + intmap[int] = model.tₛ * InfiniteOpt.∫(arg, model.model[:t], lo, hi) + end + exprs = map(c -> Symbolics.substitute(c, intmap), exprs) +end + +function MTK.substitute_differentials(model::PyomoModel, sys, eqs) + pmodel = prob.model + @unpack model, model_sym, t_sym, idx_sym = pmodel + model.dU = pyomo.DerivativeVar(model.U, wrt = model.t) + + mdU = Symbolics.symbolic_getproperty(model_sym, :dU) + mU = Symbolics.symbolic_getproperty(model_sym, :U) + mtₛ = Symbolics.symbolic_getproperty(model_sym, :tₛ) + diffsubmap = Dict([D(mU[i, t_sym]) => mdU[i, t_sym] for i in 1:length(unknowns(sys))]) + + diff_eqs = substitute_model_vars(model, sys, pmap, diff_equations(sys)) + diff_eqs = map(e -> Symbolics.substitute(e, diffsubmap), diff_eqs) + [mtₛ * eq.rhs - eq.lhs == 0 for eq in diff_eqs] +end + +struct PyomoCollocation <: AbstractCollocation + solver::Any + derivative_method +end +MTK.PyomoCollocation(solver, derivative_method = 1) = PyomoCollocation(solver, derivative_method) + +function MTK.prepare_and_optimize!() +end +function MTK.get_U_values() +end +function MTK.get_V_values() +end +function MTK.get_t_values() +end +function MTK.successful_solve() +end + +end diff --git a/src/systems/optimal_control_interface.jl b/src/systems/optimal_control_interface.jl index b91c76182f..04d508bc11 100644 --- a/src/systems/optimal_control_interface.jl +++ b/src/systems/optimal_control_interface.jl @@ -243,9 +243,52 @@ function add_cost_function!(model, sys, tspan, pmap) jcosts = substitute_model_vars(model, sys, jcosts, tspan) jcosts = substitute_params(pmap, jcosts) jcosts = substitute_integral(model, jcosts, tspan) + set_objective!(model, consolidate(jcosts)) end +""" +Substitute integrals. For an integral from (ts, te): +- Free final time problems should transcribe this to (0, 1) in the case that (ts, te) is the original timespan. Free final time problems cannot handle partial timespans. +- CasADi cannot handle partial timespans, even for non-free-final time problems. +time problems and unchanged otherwise. +""" +function substitute_integral(model, exprs, tspan) + intmap = Dict() + for int in MTK.collect_applied_operators(exprs, Symbolics.Integral) + op = MTK.operation(int) + arg = only(arguments(MTK.value(int))) + lo, hi = MTK.value.((op.domain.domain.left, op.domain.domain.right)) + lo, hi = process_integral_bounds(model, (lo, hi), tspan) + intmap[int] = lowered_integral(model, expr, lo, hi) + end + expr = map(c -> Symbolics.substitute(c, intmap), expr) + expr = value.(expr) +end + +"""Substitute variables like x(1.5) with the corresponding model variables.""" +function substitute_model_vars(model, sys, exprs) + x_ops = [MTK.operation(MTK.unwrap(st)) for st in unknowns(sys)] + c_ops = [MTK.operation(MTK.unwrap(ct)) for ct in MTK.unbound_inputs(sys)] + + exprs = map(c -> Symbolics.fast_substitute(c, fixed_t_map(model, x_ops, c_ops, exprs)), exprs) + exprs = map(c -> Symbolics.fast_substitute(c, whole_t_map(model, sys)), exprs) + + (ti, tf) = tspan + if MTK.symbolic_type(tf) === MTK.ScalarSymbolic() + _tf = model.tₛ + ti + exprs = map(c -> Symbolics.fast_substitute(c, Dict(tf => _tf)), exprs) + exprs = map(c -> Symbolics.fast_substitute(c, free_t_map(model, _tf, x_ops, c_ops)), exprs) + end +end + +function process_integral_bounds end +function lowered_integral end +function lowered_derivative end +function free_t_map end +function fixed_t_map end +function whole_t_map end + function add_user_constraints!(model, sys, tspan, pmap) conssys = get_constraintsystem(sys) jconstraints = isnothing(conssys) ? nothing : get_constraints(conssys) @@ -265,7 +308,7 @@ end function add_equational_constraints!(model, sys, pmap, tspan) diff_eqs = substitute_model_vars(model, sys, diff_equations(sys), tspan) diff_eqs = substitute_params(pmap, diff_eqs) - diff_eqs = substitute_differentials(model, sys, diff_eqs) + diff_eqs = substitute_differentials(model, sys, diff_eqs, tspan) for eq in diff_eqs add_constraint!(model, eq.lhs ~ eq.rhs * model.tₛ) end @@ -278,16 +321,12 @@ function add_equational_constraints!(model, sys, pmap, tspan) end function set_objective! end -"""Substitute variables like x(1.5) with the corresponding model variables.""" -function substitute_model_vars end -""" -Substitute integrals. For an integral from (ts, te): -- Free final time problems should transcribe this to (0, 1) in the case that (ts, te) is the original timespan. Free final time problems cannot handle partial timespans. -- CasADi cannot handle partial timespans, even for non-free-final time problems. -time problems and unchanged otherwise. -""" -function substitute_integral end -function substitute_differentials end + +function substitute_differentials(model, sys, eqs) + D = Differential(MTK.get_iv(sys)) + diffsubmap = Dict([D(model.U[i]) => lowered_derivative(model, i) for i in 1:length(U)]) + diff_eqs = map(c -> Symbolics.substitute(c, diffsubmap), diff_eqs) +end function substitute_toterm(vars, exprs) toterm_map = Dict([u => default_toterm(value(u)) for u in vars]) From 072824cf99562f94e8c01a6a53743e6b04cebacd Mon Sep 17 00:00:00 2001 From: vyudu Date: Mon, 19 May 2025 21:58:00 -0400 Subject: [PATCH 1977/2176] chore: move the docstrings to the interface file --- ext/MTKCasADiDynamicOptExt.jl | 24 +--- ext/MTKInfiniteOptExt.jl | 43 +------ ext/MTKPyomoDynamicOptExt.jl | 155 ++++++++++------------- src/systems/optimal_control_interface.jl | 68 +++++++++- 4 files changed, 141 insertions(+), 149 deletions(-) diff --git a/ext/MTKCasADiDynamicOptExt.jl b/ext/MTKCasADiDynamicOptExt.jl index f70b69a59d..d780472540 100644 --- a/ext/MTKCasADiDynamicOptExt.jl +++ b/ext/MTKCasADiDynamicOptExt.jl @@ -61,23 +61,7 @@ function (M::MXLinearInterpolation)(τ) end end -""" - CasADiDynamicOptProblem(sys::System, u0, tspan, p; dt, steps) - -Convert an System representing an optimal control system into a CasADi model -for solving using optimization. Must provide either `dt`, the timestep between collocation -points (which, along with the timespan, determines the number of points), or directly -provide the number of points as `steps`. - -The optimization variables: -- a vector-of-vectors U representing the unknowns as an interpolation array -- a vector-of-vectors V representing the controls as an interpolation array - -The constraints are: -- The set of user constraints passed to the System via `constraints` -- The solver constraints that encode the time-stepping used by the solver -""" -function MTK.CasADiDynamicOptProblem(sys::System, u0map, tspan, pmap; +function MTK.CasADiDynamicOptProblem(sys::ODESystem, u0map, tspan, pmap; dt = nothing, steps = nothing, guesses = Dict(), kwargs...) @@ -212,15 +196,11 @@ function add_solve_constraints!(prob::CasADiDynamicOptProblem, tableau) solver_opti end -""" -CasADi Collocation solver. -- solver: an optimization solver such as Ipopt. Should be given as a string or symbol in all lowercase, e.g. "ipopt" -- tableau: An ODE RK tableau. Load a tableau by calling a function like `constructRK4` and may be found at https://docs.sciml.ai/DiffEqDevDocs/stable/internals/tableaus/. If this argument is not passed in, the solver will default to Radau second order. -""" struct CasADiCollocation <: AbstractCollocation solver::Union{String, Symbol} tableau::DiffEqBase.ODERKTableau end + MTK.CasADiCollocation(solver, tableau = MTK.constructDefault()) = CasADiCollocation(solver, tableau) function MTK.prepare_and_optimize!(prob::CasADiDynamicOptProblem, solver::CasADiCollocation; verbose = false, solver_options = Dict(), plugin_options = Dict(), kwargs...) diff --git a/ext/MTKInfiniteOptExt.jl b/ext/MTKInfiniteOptExt.jl index 61f4ec70b3..4b59714e35 100644 --- a/ext/MTKInfiniteOptExt.jl +++ b/ext/MTKInfiniteOptExt.jl @@ -72,40 +72,14 @@ function MTK.add_constraint!(m::InfiniteOptModel, expr::Union{Equation, Inequali end MTK.set_objective!(m::InfiniteOptModel, expr) = @objective(m.model, Min, expr) -""" - JuMPDynamicOptProblem(sys::System, u0, tspan, p; dt) - -Convert a System representing an optimal control system into a JuMP model -for solving using optimization. Must provide either `dt`, the timestep between collocation -points (which, along with the timespan, determines the number of points), or directly -provide the number of points as `steps`. - -The optimization variables: -- a vector-of-vectors U representing the unknowns as an interpolation array -- a vector-of-vectors V representing the controls as an interpolation array - -The constraints are: -- The set of user constraints passed to the System via `constraints` -- The solver constraints that encode the time-stepping used by the solver -""" -function MTK.JuMPDynamicOptProblem(sys::System, u0map, tspan, pmap; +function MTK.JuMPDynamicOptProblem(sys::ODESystem, u0map, tspan, pmap; dt = nothing, steps = nothing, guesses = Dict(), kwargs...) MTK.process_DynamicOptProblem(JuMPDynamicOptProblem, InfiniteOptModel, sys, u0map, tspan, pmap; dt, steps, guesses, kwargs...) end -""" - InfiniteOptDynamicOptProblem(sys::System, u0map, tspan, pmap; dt) - -Convert System representing an optimal control system into a InfiniteOpt model -for solving using optimization. Must provide `dt` for determining the length -of the interpolation arrays. - -Related to `JuMPDynamicOptProblem`, but directly adds the differential equations -of the system as derivative constraints, rather than using a solver tableau. -""" -function MTK.InfiniteOptDynamicOptProblem(sys::System, u0map, tspan, pmap; +function MTK.InfiniteOptDynamicOptProblem(sys::ODESystem, u0map, tspan, pmap; dt = nothing, steps = nothing, guesses = Dict(), kwargs...) @@ -115,6 +89,7 @@ function MTK.InfiniteOptDynamicOptProblem(sys::System, u0map, tspan, pmap; end MTK.lowered_integral(model, expr, lo, hi) = model.tₛ * InfiniteOpt.∫(arg, model.model[:t], lo, hi) +MTK.lowered_derivative(model, i) = ∂(model.U[i], model.model[:t]) function MTK.process_integral_bounds(model, integral_span, tspan) if MTK.is_free_final(model) && isequal(integral_span, tspan) @@ -132,8 +107,6 @@ function MTK.add_initial_constraints!(m::InfiniteOptModel, u0, u0_idxs, ts) end end -MTK.lowered_derivative(model, i) = ∂(model.U[i], model.model[:t]) - function MTK.fixed_t_map(model::InfiniteOptModel, x_ops, c_ops, exprs) Dict([[x_ops[i] => model.U[i] for i in 1:length(model.U)]; [c_ops[i] => model.V[i] for i in 1:length(model.V)]]) @@ -191,22 +164,12 @@ function add_solve_constraints!(prob::JuMPDynamicOptProblem, tableau) end end -""" -JuMP Collocation solver. -- solver: a optimization solver such as Ipopt -- tableau: An ODE RK tableau. Load a tableau by calling a function like `constructRK4` and may be found at https://docs.sciml.ai/DiffEqDevDocs/stable/internals/tableaus/. If this argument is not passed in, the solver will default to Radau second order. -""" struct JuMPCollocation <: AbstractCollocation solver::Any tableau::DiffEqBase.ODERKTableau end MTK.JuMPCollocation(solver, tableau = MTK.constructDefault()) = JuMPCollocation(solver, tableau) -""" -InfiniteOpt Collocation solver. -- solver: an optimization solver such as Ipopt -- `derivative_method`: the method used by InfiniteOpt to compute derivatives. The list of possible options can be found at https://infiniteopt.github.io/InfiniteOpt.jl/stable/guide/derivative/. Defaults to FiniteDifference(Backward()). -""" struct InfiniteOptCollocation <: AbstractCollocation solver::Any derivative_method::InfiniteOpt.AbstractDerivativeMethod diff --git a/ext/MTKPyomoDynamicOptExt.jl b/ext/MTKPyomoDynamicOptExt.jl index 859c17d72a..b822ca0203 100644 --- a/ext/MTKPyomoDynamicOptExt.jl +++ b/ext/MTKPyomoDynamicOptExt.jl @@ -14,7 +14,8 @@ struct PyomoDAEVar v::Py end (v::PyomoDAEVar)(t) = v.v[:, t] -getindex(v::PyomoDAEVar, i::Union{Num, Symbolic}, t::Union{Num, Symbolic}) = wrap(Term{symeltype(A)}(getindex, [A, unwrap(i), unwrap(t)])) +getindex(v::PyomoDAEVar, i::Union{Num, Symbolic}, t::Union{Num, Symbolic}) = wrap(Term{symeltype(v)}(getindex, [v, unwrap(i), unwrap(t)])) +getindex(v::PyomoDAEVar, i::Int) = wrap(Term{symeltype(v)}(getindex, [v, unwrap(i), Colon()])) for ff in [acos, log1p, acosh, log2, asin, tan, atanh, cos, log, sin, log10, sqrt] f = nameof(ff) @@ -25,9 +26,9 @@ const SymbolicConcreteModel = Symbolics.symstruct(ConcreteModel) struct PyomoModel model::ConcreteModel - U - V - tₛ::Union{Int} + U::PyomoDAEVar + V::PyomoDAEVar + tₛ::Union{Int, Py} is_free_final::Bool model_sym::SymbolicConcreteModel t_sym::Union{Num, BasicSymbolic} @@ -54,36 +55,30 @@ struct PyomoDynamicOptProblem{uType, tType, isinplace, P, F, K} <: end end -""" - PyomoDynamicOptProblem(sys::ODESystem, u0, tspan, p; dt, steps) - -Convert an ODESystem representing an optimal control system into a Pyomo model -for solving using optimization. Must provide either `dt`, the timestep between collocation -points (which, along with the timespan, determines the number of points), or directly -provide the number of points as `steps`. -""" function MTK.PyomoDynamicOptProblem(sys::ODESystem, u0map, tspan, pmap; - dt = nothing, - steps = nothing, + dt = nothing, steps = nothing, guesses = Dict(), kwargs...) prob = MTK.process_DynamicOptProblem(PyomoDynamicOptProblem, PyomoModel, sys, u0map, tspan, pmap; dt, steps, guesses, kwargs...) + prob.wrapped_model.model.dU = pyomo.DerivativeVar(prob.wrapped_model.model.U, wrt = model.t) MTK.add_equational_constraints!(prob.wrapped_model, sys, pmap, tspan) prob end MTK.generate_internal_model(m::Type{PyomoModel}) = pyomo.ConcreteModel() + function MTK.generate_time_variable!(m::ConcreteModel, tspan, tsteps) + m.steps = length(tsteps) m.t = pyomo.ContinuousSet(initialize = collect(tsteps), bounds = tspan) end function MTK.generate_state_variable!(m::ConcreteModel, u0, ns, ts) m.u_idxs = pyomo.RangeSet(1, ns) - pyomo.Var(m.u_idxs, m.t) + PyomoDAEVar(pyomo.Var(m.u_idxs, m.t)) end function MTK.generate_input_variable!(m::ConcreteModel, u0, nc, ts) m.v_idxs = pyomo.RangeSet(1, nc) - pyomo.Var(m.v_idxs, m.t) + PyomoDAEVar(pyomo.Var(m.v_idxs, m.t)) end function MTK.generate_timescale(m::ConcreteModel, guess, is_free_t) @@ -111,99 +106,89 @@ function add_initial_constraints!(model::PyomoModel, u0, u0_idxs) end end -function substitute_fixed_t_vars!(model::PyomoModel, sys, exprs) - stidxmap = Dict([v => i for (i, v) in enumerate(unknowns(sys))]) - ctidxmap = Dict([v => i for (i, v) in enumerate(MTK.unbound_inputs(sys))]) - iv = MTK.get_iv(sys) - - for cons in jconstraints - consvars = MTK.vars(cons) - for st in consvars +function fixed_t_map(m::PyomoModel, sys, exprs) + stidxmap = Dict([v => i for (i, v) in x_ops]) + ctidxmap = Dict([v => i for (i, v) in c_ops]) + mU = Symbolics.symbolic_getproperty(model_sym, :U) + mV = Symbolics.symbolic_getproperty(model_sym, :V) + fixed_t_map = Dict() + for expr in exprs + vars = MTK.vars(exprs) + for st in vars MTK.iscall(st) || continue x = MTK.operation(st) t = only(MTK.arguments(st)) MTK.symbolic_type(t) === MTK.NotSymbolic() || continue - if haskey(stidxmap, x(iv)) - idx = stidxmap[x(iv)] - cv = :U - else - idx = ctidxmap[x(iv)] - cv = :V - end - model.t.add(t) - cons = Symbolics.substitute(cons, Dict(x(t) => model.cv[idx, t])) + m.model.t.add(t) + fixed_t_map[x(t)] = haskey(stidxmap, x) ? mU[stidxmap[x], t] : mV[ctidxmap[x], t] end end + fixed_t_map end -function MTK.substitute_model_vars(pmodel::PyomoModel, sys, pmap, exprs, tspan) - @unwrap model, model_sym, idx_sym, t_sym = pmodel - x_ops = [MTK.operation(MTK.unwrap(st)) for st in unknowns(sys)] - c_ops = [MTK.operation(MTK.unwrap(ct)) for ct in MTK.unbound_inputs(sys)] +function MTK.free_t_map(model, x_ops, c_ops) mU = Symbolics.symbolic_getproperty(model_sym, :U) mV = Symbolics.symbolic_getproperty(model_sym, :V) + Dict([[x(tₛ) => mU[i, end] for (i, x) in enumerate(x_ops)]; + [c(tₛ) => mV[i, end] for (i, c) in enumerate(c_ops)]]) +end - (ti, tf) = tspan - if MTK.symbolic_type(tf) === MTK.ScalarSymbolic() - _tf = model.tₛ + ti - exprs = map(c -> Symbolics.fast_substitute(c, Dict(tf => _tf)), exprs) - free_t_map = Dict([[x(tₛ) => mU[i, end] for (i, x) in enumerate(x_ops)]; - [c(tₛ) => mV[i, end] for (i, c) in enumerate(c_ops)]]) - exprs = map(c -> Symbolics.fixpoint_sub(c, free_t_map), exprs) - end +function MTK.whole_t_map(model) + mU = Symbolics.symbolic_getproperty(model_sym, :U) + mV = Symbolics.symbolic_getproperty(model_sym, :V) + Dict([[v => mU[i, t_sym] for (i, v) in enumerate(sts)]; + [v => mV[i, t_sym] for (i, v) in enumerate(cts)]]) +end - whole_interval_map = Dict([[v => mU[i, t_sym] for (i, v) in enumerate(sts)]; - [v => mV[i, t_sym] for (i, v) in enumerate(cts)]]) - exprs = map(c -> Symbolics.fixpoint_sub(c, whole_interval_map), exprs) - exprs -end - -function MTK.substitute_integral!(model::PyomoModel, exprs, tspan) - intmap = Dict() - for int in MTK.collect_applied_operators(exprs, Symbolics.Integral) - op = MTK.operation(int) - arg = only(arguments(MTK.value(int))) - lo, hi = MTK.value.((op.domain.domain.left, op.domain.domain.right)) - if MTK.is_free_final(model) && isequal((lo, hi), tspan) - (lo, hi) = (0, 1) - elseif MTK.is_free_final(model) - error("Free final time problems cannot handle partial timespans.") - end - intmap[int] = model.tₛ * InfiniteOpt.∫(arg, model.model[:t], lo, hi) - end - exprs = map(c -> Symbolics.substitute(c, intmap), exprs) +function MTK.lowered_integral(m::PyomoModel, arg) + @unpack model, model_sym, t_sym = m + arg_f = Symbolics.build_function(arg, model_sym, t_sym) + Integral(model.t, wrt = model.t, rule=arg_f) end -function MTK.substitute_differentials(model::PyomoModel, sys, eqs) - pmodel = prob.model - @unpack model, model_sym, t_sym, idx_sym = pmodel - model.dU = pyomo.DerivativeVar(model.U, wrt = model.t) +MTK.process_integral_bounds(model, integral_span, tspan) = integral_span +function MTK.lowered_derivative(m::PyomoModel, i) mdU = Symbolics.symbolic_getproperty(model_sym, :dU) - mU = Symbolics.symbolic_getproperty(model_sym, :U) - mtₛ = Symbolics.symbolic_getproperty(model_sym, :tₛ) - diffsubmap = Dict([D(mU[i, t_sym]) => mdU[i, t_sym] for i in 1:length(unknowns(sys))]) - - diff_eqs = substitute_model_vars(model, sys, pmap, diff_equations(sys)) - diff_eqs = map(e -> Symbolics.substitute(e, diffsubmap), diff_eqs) - [mtₛ * eq.rhs - eq.lhs == 0 for eq in diff_eqs] + mdU[i, t_sym] end struct PyomoCollocation <: AbstractCollocation - solver::Any - derivative_method + solver::Union{String, Symbol} + derivative_method::Pyomo.DiscretizationMethod end -MTK.PyomoCollocation(solver, derivative_method = 1) = PyomoCollocation(solver, derivative_method) -function MTK.prepare_and_optimize!() -end -function MTK.get_U_values() +MTK.PyomoCollocation(solver, derivative_method = LagrangeRadau(5)) = PyomoCollocation(solver, derivative_method) + +function MTK.prepare_and_optimize!(prob::PyomoDynamicOptProblem, collocation; verbose, kwargs...) + m = prob.wrapped_model.model + dm = collocation.derivative_method + discretizer = TransformationFactory(dm) + ncp = is_finite_difference(dm) ? 1 : dm.np + discretizer.apply_to(model, wrt = m.t, nfe = m.steps, ncp = ncp, scheme = scheme_string(dm)) + solver = SolverFactory(string(collocation.solver)) + solver.solve(m) end -function MTK.get_V_values() + +function MTK.get_U_values(m::PyomoModel) + [pyomo.value(model.U[i]) for i in model.U] end -function MTK.get_t_values() + +function MTK.get_V_values(m::PyomoModel) + [pyomo.value(model.V[i]) for i in model.V] end -function MTK.successful_solve() + +function MTK.get_t_values(m::PyomoModel) + [pyomo.value(model.t[i]) for i in model.t] end +function MTK.successful_solve(m::PyomoModel) + ss = m.solver.status + tc = m.solver.termination_condition + if ss == opt.SolverStatus.ok && (tc == opt.TerminationStatus.optimal || tc == opt.TerminationStatus.locallyOptimal) + return true + else + return false + end +end end diff --git a/src/systems/optimal_control_interface.jl b/src/systems/optimal_control_interface.jl index 04d508bc11..31dbf5e5c1 100644 --- a/src/systems/optimal_control_interface.jl +++ b/src/systems/optimal_control_interface.jl @@ -18,13 +18,78 @@ function Base.show(io::IO, sol::DynamicOptSolution) print("\n\nPlease query the model using sol.model, the solution trajectory for the system using sol.sol, or the solution trajectory for the controllers using sol.input_sol.") end +""" + JuMPDynamicOptProblem(sys::ODESystem, u0, tspan, p; dt, steps, guesses, kwargs...) + +Convert an ODESystem representing an optimal control system into a JuMP model +for solving using optimization. Must provide either `dt`, the timestep between collocation +points (which, along with the timespan, determines the number of points), or directly +provide the number of points as `steps`. + +To construct the problem, please load InfiniteOpt along with ModelingToolkit. +""" function JuMPDynamicOptProblem end +""" + InfiniteOptDynamicOptProblem(sys::ODESystem, u0map, tspan, pmap; dt) + +Convert an ODESystem representing an optimal control system into a InfiniteOpt model +for solving using optimization. Must provide `dt` for determining the length +of the interpolation arrays. + +Related to `JuMPDynamicOptProblem`, but directly adds the differential equations +of the system as derivative constraints, rather than using a solver tableau. + +To construct the problem, please load InfiniteOpt along with ModelingToolkit. +""" function InfiniteOptDynamicOptProblem end +""" + CasADiDynamicOptProblem(sys::ODESystem, u0, tspan, p; dt, steps, guesses, kwargs...) + +Convert an ODESystem representing an optimal control system into a CasADi model +for solving using optimization. Must provide either `dt`, the timestep between collocation +points (which, along with the timespan, determines the number of points), or directly +provide the number of points as `steps`. + +To construct the problem, please load CasADi along with ModelingToolkit. +""" function CasADiDynamicOptProblem end +""" + PyomoDynamicOptProblem(sys::ODESystem, u0, tspan, p; dt, steps) +Convert an ODESystem representing an optimal control system into a Pyomo model +for solving using optimization. Must provide either `dt`, the timestep between collocation +points (which, along with the timespan, determines the number of points), or directly +provide the number of points as `steps`. + +To construct the problem, please load Pyomo along with ModelingToolkit. +""" +function PyomoDynamicOptProblem end + +### Collocations +""" +JuMP Collocation solver. +- solver: a optimization solver such as Ipopt +- tableau: An ODE RK tableau. Load a tableau by calling a function like `constructRK4` and may be found at https://docs.sciml.ai/DiffEqDevDocs/stable/internals/tableaus/. If this argument is not passed in, the solver will default to Radau second order. +""" function JuMPCollocation end +""" +InfiniteOpt Collocation solver. +- solver: an optimization solver such as Ipopt +- `derivative_method`: the method used by InfiniteOpt to compute derivatives. The list of possible options can be found at https://infiniteopt.github.io/InfiniteOpt.jl/stable/guide/derivative/. Defaults to FiniteDifference(Backward()). +""" function InfiniteOptCollocation end +""" +CasADi Collocation solver. +- solver: an optimization solver such as Ipopt. Should be given as a string or symbol in all lowercase, e.g. "ipopt" +- tableau: An ODE RK tableau. Load a tableau by calling a function like `constructRK4` and may be found at https://docs.sciml.ai/DiffEqDevDocs/stable/internals/tableaus/. If this argument is not passed in, the solver will default to Radau second order. +""" function CasADiCollocation end +""" +Pyomo Collocation solver. +- solver: an optimization solver such as Ipopt. Should be given as a string or symbol in all lowercase, e.g. "ipopt" +- derivative_method: a derivative method from Pyomo. The choices here are ForwardEuler, BackwardEuler, MidpointEuler, LagrangeRadau, or LagrangeLegendre. The last two should additionally have a number indicating the number of collocation points per timestep, e.g. PyomoCollocation("ipopt", LagrangeRadau(3)). Defaults to LagrangeRadau(5). +""" +function PyomoCollocation end function warn_overdetermined(sys, u0map) cstrs = constraints(sys) @@ -243,7 +308,6 @@ function add_cost_function!(model, sys, tspan, pmap) jcosts = substitute_model_vars(model, sys, jcosts, tspan) jcosts = substitute_params(pmap, jcosts) jcosts = substitute_integral(model, jcosts, tspan) - set_objective!(model, consolidate(jcosts)) end @@ -316,7 +380,7 @@ function add_equational_constraints!(model, sys, pmap, tspan) alg_eqs = substitute_model_vars(model, sys, alg_equations(sys), tspan) alg_eqs = substitute_params(pmap, alg_eqs) for eq in alg_eqs - add_constraint!(model, eq.lhs ~ eq.rhs * model.tₛ) + add_constraint!(model, eq.lhs ~ eq.rhs) end end From 7fda5b0f13a6ad689afc24a652377a97243b84ad Mon Sep 17 00:00:00 2001 From: vyudu Date: Mon, 19 May 2025 22:08:15 -0400 Subject: [PATCH 1978/2176] test: add Pyomo tests --- test/extensions/dynamic_optimization.jl | 48 +++++++++++++++++++++++-- 1 file changed, 46 insertions(+), 2 deletions(-) diff --git a/test/extensions/dynamic_optimization.jl b/test/extensions/dynamic_optimization.jl index 2d24f92d1f..d476b7a066 100644 --- a/test/extensions/dynamic_optimization.jl +++ b/test/extensions/dynamic_optimization.jl @@ -44,6 +44,9 @@ const M = ModelingToolkit @test ≈(isol.sol.u, osol2.u, rtol = 0.001) csol2 = solve(cprob, CasADiCollocation("ipopt", constructImplicitEuler())) @test ≈(csol2.sol.u, osol2.u, rtol = 0.001) + pprob = PyomoDynamicOptProblem(sys, u0map, tspan, parammap, dt = 0.01) + psol = solve(cprob, PyomoCollocation("ipopt", BackwardEuler())) + @test psol.sol.u ≈ osol2.u # With a constraint u0map = Pair[] @@ -62,6 +65,12 @@ const M = ModelingToolkit @test csol.sol(0.6; idxs = x(t)) ≈ 3.5 @test csol.sol(0.3; idxs = x(t)) ≈ 7.0 + pprob = PyomoDynamicOptProblem( + lksys, u0map, tspan, parammap; guesses = guess, dt = 0.01) + psol = solve(pprob, PyomoCollocation("ipopt", LagrangeLegendre(3))) + @test psol.sol(0.6)[1] ≈ 3.5 + @test psol.sol(0.3)[1] ≈ 7.0 + iprob = InfiniteOptDynamicOptProblem( lksys, u0map, tspan, parammap; guesses = guess, dt = 0.01) isol = solve(iprob, InfiniteOptCollocation(Ipopt.Optimizer, InfiniteOpt.OrthogonalCollocation(3))) # 48.564 ms, 9.58 MiB @@ -77,10 +86,14 @@ const M = ModelingToolkit isol = solve(iprob, InfiniteOptCollocation(Ipopt.Optimizer, InfiniteOpt.OrthogonalCollocation(3))) @test all(u -> u > [1, 1], isol.sol.u) - jprob = JuMPDynamicOptProblem(lksys, u0map, tspan, parammap; guesses = guess, dt = 0.01) + jprob = PyoDynamicOptProblem(lksys, u0map, tspan, parammap; guesses = guess, dt = 0.01) jsol = solve(jprob, JuMPCollocation(Ipopt.Optimizer, constructRadauIA3())) @test all(u -> u > [1, 1], jsol.sol.u) + pprob = PyomoDynamicOptProblem(lksys, u0map, tspan, parammap; guesses = guess, dt = 0.01) + psol = solve(pprob, PyomoCollocation("ipopt", MidpointEuler())) + @test all(u -> u > [1, 1], psol.sol.u) + cprob = CasADiDynamicOptProblem( lksys, u0map, tspan, parammap; guesses = guess, dt = 0.01) csol = solve(cprob, CasADiCollocation("ipopt", constructRadauIA3())) @@ -125,7 +138,6 @@ end cprob = CasADiDynamicOptProblem(block, u0map, tspan, parammap; dt = 0.01) csol = solve(cprob, CasADiCollocation("ipopt", constructVerner8())) - # Linear systems have bang-bang controls @test is_bangbang(csol.input_sol, [-1.0], [1.0]) # Test reached final position. @test ≈(csol.sol[x(t)][end], 0.25, rtol = 1e-5) @@ -143,8 +155,15 @@ end isol = solve(iprob, InfiniteOptCollocation(Ipopt.Optimizer)) @test is_bangbang(isol.input_sol, [-1.0], [1.0]) @test ≈(isol.sol[x(t)][end], 0.25, rtol = 1e-5) + + pprob = PyomoDynamicOptProblem(block, u0map, tspan, parammap; dt = 0.01) + psol = solve(pprob, PyomoCollocation("ipopt", BackwardEuler())) + @test is_bangbang(psol.input_sol, [-1.0], [1.0]) + @test ≈(psol.sol.u[end][2], 0.25, rtol = 1e-5) + osol = solve(oprob, ImplicitEuler(); dt = 0.01, adaptive = false) @test ≈(isol.sol.u, osol.u, rtol = 0.05) + @test ≈(psol.sol.u, osol.u, rtol = 0.05) ################### ### Bee example ### @@ -172,6 +191,9 @@ end cprob = CasADiDynamicOptProblem(beesys, u0map, tspan, pmap; dt = 0.01) csol = solve(cprob, CasADiCollocation("ipopt", constructTsitouras5())) @test is_bangbang(csol.input_sol, [0.0], [1.0]) + pprob = PyomoDynamicOptProblem(beesys, u0map, tspan, pmap, dt = 0.01) + psol = solve(pprob, PyomoCollocation("ipopt", BackwardEuler())) + @test is_bangbang(psol.input_sol, [0.0], [1.0]) @parameters (α_interp::LinearInterpolation)(..) eqs = [D(w(t)) ~ -μ * w(t) + b * s * α_interp(t) * w(t), @@ -186,6 +208,7 @@ end @test ≈(osol.u, csol.sol.u, rtol = 0.01) osol2 = solve(oprob, ImplicitEuler(); dt = 0.01, adaptive = false) @test ≈(osol2.u, isol.sol.u, rtol = 0.01) + @test ≈(osol2.u, psol.sol.u, rtol = 0.01) end @testset "Rocket launch" begin @@ -224,6 +247,10 @@ end isol = solve(iprob, InfiniteOptCollocation(Ipopt.Optimizer)) @test isol.sol[h(t)][end] > 1.012 + pprob = PyomoCollocationDynamicOptProblem(rocket, u0map, (ts, te), pmap; dt = 0.001, cse = false) + psol = solve(pprob, PyomoCollocation("ipopt")) + @test psol.sol.u[end][1] > 1.012 + # Test solution @parameters (T_interp::CubicSpline)(..) eqs = [D(h(t)) ~ v(t), @@ -240,6 +267,11 @@ end oprob1 = ODEProblem(rocket_ode, merge(Dict(u0map), Dict(pmap), interpmap1), (ts, te)) osol1 = solve(oprob1, ImplicitEuler(); adaptive = false, dt = 0.001) @test ≈(isol.sol.u, osol1.u, rtol = 0.01) + + interpmap2 = Dict(T_interp => ctrl_to_spline(psol.input_sol, CubicSpline)) + oprob2 = ODEProblem(rocket_ode, u0map, (ts, te), merge(Dict(pmap), interpmap2)) + osol2 = solve(oprob2, RadauIIA5(); adaptive = false, dt = 0.001) + @test ≈(psol.sol.u, osol2.u, rtol = 0.01) end @testset "Free final time problems" begin @@ -269,6 +301,10 @@ end isol = solve(iprob, InfiniteOptCollocation(Ipopt.Optimizer)) @test isapprox(isol.sol.t[end], 10.0, rtol = 1e-3) + pprob = PyomoDynamicOptProblem(rocket, u0map, (0, tf), pmap; steps = 201) + psol = solve(pprob, PyomoCollocation("ipopt")) + @test isapprox(psol.sol.t[end], 10.0, rtol = 1e-3) + @variables x(..) v(..) @variables u(..) [bounds = (-1.0, 1.0), input = true] @parameters tf @@ -293,6 +329,10 @@ end iprob = InfiniteOptDynamicOptProblem(block, u0map, tspan, parammap; steps = 51) isol = solve(iprob, InfiniteOptCollocation(Ipopt.Optimizer)) @test isapprox(isol.sol.t[end], 2.0, atol = 1e-5) + + pprob = PyomoDynamicOptProblem(block, u0map, tspan, parammap; steps = 51) + psol = solve(pprob, PyomoCollocation("ipopt")) + @test isapprox(psol.sol.t[end], 2.0, atol = 1e-5) end @testset "Cart-pole problem" begin @@ -335,4 +375,8 @@ end iprob = InfiniteOptDynamicOptProblem(cartpole, u0map, tspan, pmap; dt = 0.04) isol = solve(iprob, InfiniteOptCollocation(Ipopt.Optimizer)) @test isol.sol.u[end] ≈ [π, 0, 0, 0] + + pprob = PyomoDynamicOptProblem(cartpole, u0map, tspan, pmap; dt = 0.04) + psol = solve(pprob, PyomoCollocation("ipopt", LagrangeLegendre(4))) + @test psol.sol.u[end] ≈ [π, 0, 0, 0] end From a275b339569a76f184fbfaa61c3c203588d7a268 Mon Sep 17 00:00:00 2001 From: vyudu Date: Thu, 22 May 2025 09:26:41 -0400 Subject: [PATCH 1979/2176] refactor: use lowered_var instead of manual substitution --- Project.toml | 2 + ext/MTKCasADiDynamicOptExt.jl | 39 +----- ext/MTKInfiniteOptExt.jl | 24 +--- ext/MTKPyomoDynamicOptExt.jl | 155 ++++++++--------------- src/ModelingToolkit.jl | 4 +- src/systems/optimal_control_interface.jl | 74 +++++++---- test/extensions/dynamic_optimization.jl | 6 +- 7 files changed, 123 insertions(+), 181 deletions(-) diff --git a/Project.toml b/Project.toml index 9645a412b0..215c0bcd1b 100644 --- a/Project.toml +++ b/Project.toml @@ -46,6 +46,7 @@ OffsetArrays = "6fe1bfb0-de20-5000-8ca7-80f57d26f881" OrderedCollections = "bac558e1-5e72-5ebc-8fee-abe8a469f55d" OrdinaryDiffEqCore = "bbf590c4-e513-4bbe-9b18-05decba2e5d8" PrecompileTools = "aea7be01-6a6a-4083-8856-8a6e6704d82a" +Pyomo = "0e8e1daf-01b5-4eba-a626-3897743a3816" RecursiveArrayTools = "731186ca-8d62-57ce-b412-fbd966d074cd" Reexport = "189a3867-3050-52da-a836-e630ba90ab69" RuntimeGeneratedFunctions = "7e49a35a-f44a-4d26-94aa-eba1b4ca6b47" @@ -81,6 +82,7 @@ MTKDeepDiffsExt = "DeepDiffs" MTKFMIExt = "FMI" MTKInfiniteOptExt = "InfiniteOpt" MTKLabelledArraysExt = "LabelledArrays" +MTKPyomoDynamicOptExt = "Pyomo" [compat] ADTypes = "1.14.0" diff --git a/ext/MTKCasADiDynamicOptExt.jl b/ext/MTKCasADiDynamicOptExt.jl index d780472540..d678e853e6 100644 --- a/ext/MTKCasADiDynamicOptExt.jl +++ b/ext/MTKCasADiDynamicOptExt.jl @@ -105,6 +105,7 @@ function MTK.add_constraint!(m::CasADiModel, expr) subject_to!(m.model, expr.lhs - expr.rhs ≤ 0) end end + MTK.set_objective!(m::CasADiModel, expr) = minimize!(m.model, MX(expr)) function MTK.add_initial_constraints!(m::CasADiModel, u0, u0_idxs, args...) @@ -114,42 +115,12 @@ function MTK.add_initial_constraints!(m::CasADiModel, u0, u0_idxs, args...) end end -function free_t_map(model, tf, x_ops, c_ops) - Dict([[x(tf) => model.U.u[i, end] for (i, x) in enumerate(x_ops)]; - [c(tf) => model.V.u[i, end] for (i, c) in enumerate(c_ops)]]) -end - -function whole_t_map(model, sys) - Dict([[v => model.U.u[i, :] for (i, v) in enumerate(unknowns(sys))]; - [v => model.V.u[i, :] for (i, v) in enumerate(MTK.unbound_inputs(sys))]]) - -end - -function fixed_t_map(model::CasADiModel, x_ops, c_ops, exprs) - stidxmap = Dict([v => i for (i, v) in x_ops]) - ctidxmap = Dict([v => i for (i, v) in c_ops]) - for i in 1:length(exprs) - subvars = MTK.vars(exprs[i]) - for st in subvars - MTK.iscall(st) || continue - x = operation(st) - t = only(arguments(st)) - MTK.symbolic_type(t) === MTK.NotSymbolic() || continue - if haskey(stidxmap, x) - idx = stidxmap[x] - cv = model.U - else - idx = ctidxmap[x] - cv = model.V - end - exprs[i] = Symbolics.fast_substitute(exprs[i], Dict(x(t) => cv(t)[idx])) - end - jcosts = Symbolics.substitute(jcosts, Dict(x(t) => cv(t)[idx])) - end - exprs +function MTK.lowered_var(m::CasADiModel, uv, i, t) + X = getfield(m, uv) + t isa Union{Num, Symbolics.Symbolic} ? X.u[i, :] : X(t)[i] end -MTK.lowered_integral(model, expr, args...) = model.tₛ * (model.U.t[2] - model.U.t[1]) * expr +MTK.lowered_integral(model::CasADiModel, expr, args...) = model.tₛ * (model.U.t[2] - model.U.t[1]) * sum(expr) function add_solve_constraints!(prob::CasADiDynamicOptProblem, tableau) @unpack A, α, c = tableau diff --git a/ext/MTKInfiniteOptExt.jl b/ext/MTKInfiniteOptExt.jl index 4b59714e35..8cd83e4323 100644 --- a/ext/MTKInfiniteOptExt.jl +++ b/ext/MTKInfiniteOptExt.jl @@ -48,7 +48,7 @@ struct InfiniteOptDynamicOptProblem{uType, tType, isinplace, P, F, K} <: end MTK.generate_internal_model(m::Type{InfiniteOptModel}) = InfiniteModel() -MTK.generate_time_variable!(m::InfiniteModel, tspan, steps) = @infinite_parameter(m, t in [tspan[1], tspan[2]], num_supports = length(tsteps)) +MTK.generate_time_variable!(m::InfiniteModel, tspan, tsteps) = @infinite_parameter(m, t in [tspan[1], tspan[2]], num_supports = length(tsteps)) MTK.generate_state_variable!(m::InfiniteModel, u0::Vector, ns, ts) = @variable(m, U[i = 1:ns], Infinite(m[:t]), start=u0[i]) MTK.generate_input_variable!(m::InfiniteModel, c0, nc, ts) = @variable(m, V[i = 1:nc], Infinite(m[:t]), start=c0[i]) @@ -88,8 +88,8 @@ function MTK.InfiniteOptDynamicOptProblem(sys::ODESystem, u0map, tspan, pmap; prob end -MTK.lowered_integral(model, expr, lo, hi) = model.tₛ * InfiniteOpt.∫(arg, model.model[:t], lo, hi) -MTK.lowered_derivative(model, i) = ∂(model.U[i], model.model[:t]) +MTK.lowered_integral(model::InfiniteOptModel, expr, lo, hi) = model.tₛ * InfiniteOpt.∫(expr, model.model[:t], lo, hi) +MTK.lowered_derivative(model::InfiniteOptModel, i) = ∂(model.U[i], model.model[:t]) function MTK.process_integral_bounds(model, integral_span, tspan) if MTK.is_free_final(model) && isequal(integral_span, tspan) @@ -103,23 +103,13 @@ end function MTK.add_initial_constraints!(m::InfiniteOptModel, u0, u0_idxs, ts) for i in u0_idxs - fix(m.U[i], u0[i], force = true) + fix(m.U[i](0), u0[i], force = true) end end -function MTK.fixed_t_map(model::InfiniteOptModel, x_ops, c_ops, exprs) - Dict([[x_ops[i] => model.U[i] for i in 1:length(model.U)]; - [c_ops[i] => model.V[i] for i in 1:length(model.V)]]) -end - -function MTK.free_t_map(model::InfiniteOptModel, tf, x_ops, c_ops) - Dict([[x(tf) => model.U[i](1) for (i, x) in enumerate(x_ops)]; - [c(tf) => model.V[i](1) for (i, c) in enumerate(c_ops)]]) -end - -function MTK.whole_t_map(model::InfiniteOptModel, sys) - whole_interval_map = Dict([[v => model.U[i] for (i, v) in enumerate(unknowns(sys))]; - [v => model.V[i] for (i, v) in enumerate(MTK.unbound_inputs(sys))]]) +function MTK.lowered_var(m::InfiniteOptModel, uv, i, t) + X = getfield(m, uv) + t isa Union{Num, Symbolics.Symbolic} ? X[i] : X[i](t) end function add_solve_constraints!(prob::JuMPDynamicOptProblem, tableau) diff --git a/ext/MTKPyomoDynamicOptExt.jl b/ext/MTKPyomoDynamicOptExt.jl index b822ca0203..5e5087fd9f 100644 --- a/ext/MTKPyomoDynamicOptExt.jl +++ b/ext/MTKPyomoDynamicOptExt.jl @@ -1,42 +1,26 @@ module MTKPyomoDynamicOptExt using ModelingToolkit -using PythonCall +using Pyomo using DiffEqBase using UnPack using NaNMath const MTK = ModelingToolkit -# import pyomo -const pyomo = PythonCall.pynew() -PythonCall.pycopy!(pyomo, pyimport("pyomo.environ")) - -struct PyomoDAEVar - v::Py -end -(v::PyomoDAEVar)(t) = v.v[:, t] -getindex(v::PyomoDAEVar, i::Union{Num, Symbolic}, t::Union{Num, Symbolic}) = wrap(Term{symeltype(v)}(getindex, [v, unwrap(i), unwrap(t)])) -getindex(v::PyomoDAEVar, i::Int) = wrap(Term{symeltype(v)}(getindex, [v, unwrap(i), Colon()])) - -for ff in [acos, log1p, acosh, log2, asin, tan, atanh, cos, log, sin, log10, sqrt] - f = nameof(ff) - @eval NaNMath.$f(x::PyomoDAEVar) = Base.$f(x) -end - -const SymbolicConcreteModel = Symbolics.symstruct(ConcreteModel) - -struct PyomoModel +struct PyomoDynamicOptModel model::ConcreteModel - U::PyomoDAEVar - V::PyomoDAEVar - tₛ::Union{Int, Py} + U::PyomoVar + V::PyomoVar + tₛ::Union{Int, PyomoVar} is_free_final::Bool - model_sym::SymbolicConcreteModel - t_sym::Union{Num, BasicSymbolic} - idx_sym::Union{Num, BasicSymbolic} - - function PyomoModel(model, U, V, tₛ, is_free_final) - @variables MODEL_SYM::SymbolicConcreteModel IDX_SYM::Int T_SYM - PyomoModel(model, U, V, tₛ, is_free_final, MODEL_SYM, T_SYM, INDEX_SYM) + dU::PyomoVar + model_sym::Union{Num, Symbolics.BasicSymbolic} + t_sym::Union{Num, Symbolics.BasicSymbolic} + idx_sym::Union{Num, Symbolics.BasicSymbolic} + + function PyomoDynamicOptModel(model, U, V, tₛ, is_free_final) + @variables MODEL_SYM::Symbolics.symstruct(PyomoDynamicOptModel) IDX_SYM::Int T_SYM + model.dU = dae.DerivativeVar(U, wrt = model.t, initialize = 0) + new(model, U, V, tₛ, is_free_final, PyomoVar(model.dU), MODEL_SYM, T_SYM, IDX_SYM) end end @@ -46,7 +30,7 @@ struct PyomoDynamicOptProblem{uType, tType, isinplace, P, F, K} <: u0::uType tspan::tType p::P - wrapped_model::ConcreteModel + wrapped_model::PyomoDynamicOptModel kwargs::K function PyomoDynamicOptProblem(f, u0, tspan, p, model, kwargs...) @@ -58,35 +42,38 @@ end function MTK.PyomoDynamicOptProblem(sys::ODESystem, u0map, tspan, pmap; dt = nothing, steps = nothing, guesses = Dict(), kwargs...) - prob = MTK.process_DynamicOptProblem(PyomoDynamicOptProblem, PyomoModel, sys, u0map, tspan, pmap; dt, steps, guesses, kwargs...) - prob.wrapped_model.model.dU = pyomo.DerivativeVar(prob.wrapped_model.model.U, wrt = model.t) + prob = MTK.process_DynamicOptProblem(PyomoDynamicOptProblem, PyomoDynamicOptModel, sys, u0map, tspan, pmap; dt, steps, guesses, kwargs...) + conc_model = prob.wrapped_model.model MTK.add_equational_constraints!(prob.wrapped_model, sys, pmap, tspan) prob end -MTK.generate_internal_model(m::Type{PyomoModel}) = pyomo.ConcreteModel() +MTK.generate_internal_model(m::Type{PyomoDynamicOptModel}) = ConcreteModel(pyomo.ConcreteModel()) function MTK.generate_time_variable!(m::ConcreteModel, tspan, tsteps) m.steps = length(tsteps) - m.t = pyomo.ContinuousSet(initialize = collect(tsteps), bounds = tspan) + m.t = dae.ContinuousSet(initialize = tsteps, bounds = tspan) end function MTK.generate_state_variable!(m::ConcreteModel, u0, ns, ts) m.u_idxs = pyomo.RangeSet(1, ns) - PyomoDAEVar(pyomo.Var(m.u_idxs, m.t)) + m.U = pyomo.Var(m.u_idxs, m.t, initialize = 0) + PyomoVar(m.U) end -function MTK.generate_input_variable!(m::ConcreteModel, u0, nc, ts) +function MTK.generate_input_variable!(m::ConcreteModel, c0, nc, ts) m.v_idxs = pyomo.RangeSet(1, nc) - PyomoDAEVar(pyomo.Var(m.v_idxs, m.t)) + m.V = pyomo.Var(m.v_idxs, m.t, initialize = 0) + PyomoVar(m.V) end -function MTK.generate_timescale(m::ConcreteModel, guess, is_free_t) - m.tₛ = is_free_t ? pyomo.Var(initialize = guess, bounds = (0, Inf)) : 1 +function MTK.generate_timescale!(m::ConcreteModel, guess, is_free_t) + m.tₛ = is_free_t ? PyomoVar(pyomo.Var(initialize = guess, bounds = (0, Inf))) : 1 end -function MTK.add_constraint!(pmodel::PyomoModel, cons) +function MTK.add_constraint!(pmodel::PyomoDynamicOptModel, cons) @unpack model, model_sym, idx_sym, t_sym = pmodel + @show model.dU expr = if cons isa Equation cons.lhs - cons.rhs == 0 elseif cons.relational_op === Symbolics.geq @@ -94,63 +81,40 @@ function MTK.add_constraint!(pmodel::PyomoModel, cons) else cons.lhs - cons.rhs ≤ 0 end - constraint_f = Symbolics.build_function(expr, model_sym, idx_sym, t_sym) - pyomo.Constraint(rule = constraint_f) + constraint_f = Symbolics.build_function(expr, model_sym, idx_sym, t_sym, expression = Val{false}) + @show typeof(constraint_f) + @show typeof(Pyomo.pyfunc(constraint_f)) + cons_sym = gensym() + setproperty!(model, cons_sym, pyomo.Constraint(model.u_idxs, model.t, rule = Pyomo.pyfunc(constraint_f))) end -function MTK.set_objective!(m::PyomoModel, expr) = pyomo.Objective(expr = expr) +function MTK.set_objective!(m::PyomoDynamicOptModel, expr) + m.model.obj = pyomo.Objective(expr = expr) +end -function add_initial_constraints!(model::PyomoModel, u0, u0_idxs) +function MTK.add_initial_constraints!(model::PyomoDynamicOptModel, u0, u0_idxs, ts) for i in u0_idxs model.U[i, 0].fix(u0[i]) end end -function fixed_t_map(m::PyomoModel, sys, exprs) - stidxmap = Dict([v => i for (i, v) in x_ops]) - ctidxmap = Dict([v => i for (i, v) in c_ops]) - mU = Symbolics.symbolic_getproperty(model_sym, :U) - mV = Symbolics.symbolic_getproperty(model_sym, :V) - fixed_t_map = Dict() - for expr in exprs - vars = MTK.vars(exprs) - for st in vars - MTK.iscall(st) || continue - x = MTK.operation(st) - t = only(MTK.arguments(st)) - MTK.symbolic_type(t) === MTK.NotSymbolic() || continue - m.model.t.add(t) - fixed_t_map[x(t)] = haskey(stidxmap, x) ? mU[stidxmap[x], t] : mV[ctidxmap[x], t] - end - end - fixed_t_map -end - -function MTK.free_t_map(model, x_ops, c_ops) - mU = Symbolics.symbolic_getproperty(model_sym, :U) - mV = Symbolics.symbolic_getproperty(model_sym, :V) - Dict([[x(tₛ) => mU[i, end] for (i, x) in enumerate(x_ops)]; - [c(tₛ) => mV[i, end] for (i, c) in enumerate(c_ops)]]) -end - -function MTK.whole_t_map(model) - mU = Symbolics.symbolic_getproperty(model_sym, :U) - mV = Symbolics.symbolic_getproperty(model_sym, :V) - Dict([[v => mU[i, t_sym] for (i, v) in enumerate(sts)]; - [v => mV[i, t_sym] for (i, v) in enumerate(cts)]]) -end - -function MTK.lowered_integral(m::PyomoModel, arg) +function MTK.lowered_integral(m::PyomoDynamicOptModel, arg, lo, hi) @unpack model, model_sym, t_sym = m arg_f = Symbolics.build_function(arg, model_sym, t_sym) - Integral(model.t, wrt = model.t, rule=arg_f) + dae.Integral(model.t, wrt = model.t, rule=arg_f) end MTK.process_integral_bounds(model, integral_span, tspan) = integral_span -function MTK.lowered_derivative(m::PyomoModel, i) - mdU = Symbolics.symbolic_getproperty(model_sym, :dU) - mdU[i, t_sym] +function MTK.lowered_derivative(m::PyomoDynamicOptModel, i) + mdU = Symbolics.symbolic_getproperty(m.model_sym, :dU).val + Symbolics.unwrap(mdU[i, m.t_sym]) +end + +function MTK.lowered_var(m::PyomoDynamicOptModel, uv, i, t) + X = Symbolics.symbolic_getproperty(m.model_sym, uv).val + var = t isa Union{Num, Symbolics.Symbolic} ? X[i, m.t_sym] : X[i, t] + Symbolics.unwrap(var) end struct PyomoCollocation <: AbstractCollocation @@ -164,25 +128,18 @@ function MTK.prepare_and_optimize!(prob::PyomoDynamicOptProblem, collocation; ve m = prob.wrapped_model.model dm = collocation.derivative_method discretizer = TransformationFactory(dm) - ncp = is_finite_difference(dm) ? 1 : dm.np - discretizer.apply_to(model, wrt = m.t, nfe = m.steps, ncp = ncp, scheme = scheme_string(dm)) + ncp = Pyomo.is_finite_difference(dm) ? 1 : dm.np + discretizer.apply_to(m, wrt = m.t, nfe = m.steps, scheme = Pyomo.scheme_string(dm)) solver = SolverFactory(string(collocation.solver)) - solver.solve(m) + solver.solve(m, tee = true) + Main.xx[] = solver end -function MTK.get_U_values(m::PyomoModel) - [pyomo.value(model.U[i]) for i in model.U] -end - -function MTK.get_V_values(m::PyomoModel) - [pyomo.value(model.V[i]) for i in model.V] -end - -function MTK.get_t_values(m::PyomoModel) - [pyomo.value(model.t[i]) for i in model.t] -end +MTK.get_U_values(m::PyomoDynamicOptModel) = [pyomo.value(m.model.U[i]) for i in m.model.U.index_set()] +MTK.get_V_values(m::PyomoDynamicOptModel) = [pyomo.value(m.model.V[i]) for i in m.model.V.index_set()] +MTK.get_t_values(m::PyomoDynamicOptModel) = Pyomo.get_results(m.model, :t) -function MTK.successful_solve(m::PyomoModel) +function MTK.successful_solve(m::PyomoDynamicOptModel) ss = m.solver.status tc = m.solver.termination_condition if ss == opt.SolverStatus.ok && (tc == opt.TerminationStatus.optimal || tc == opt.TerminationStatus.locallyOptimal) diff --git a/src/ModelingToolkit.jl b/src/ModelingToolkit.jl index 9608c6083d..255c5ade48 100644 --- a/src/ModelingToolkit.jl +++ b/src/ModelingToolkit.jl @@ -356,9 +356,9 @@ function FMIComponent end include("systems/optimal_control_interface.jl") export AbstractDynamicOptProblem, JuMPDynamicOptProblem, InfiniteOptDynamicOptProblem, - CasADiDynamicOptProblem + CasADiDynamicOptProblem, PyomoDynamicOptProblem export AbstractCollocation, JuMPCollocation, InfiniteOptCollocation, - CasADiCollocation + CasADiCollocation, PyomoCollocation export DynamicOptSolution @public apply_to_variables diff --git a/src/systems/optimal_control_interface.jl b/src/systems/optimal_control_interface.jl index 31dbf5e5c1..b094c4a24f 100644 --- a/src/systems/optimal_control_interface.jl +++ b/src/systems/optimal_control_interface.jl @@ -250,7 +250,7 @@ function process_DynamicOptProblem(prob_type::Type{<:AbstractDynamicOptProblem}, tsteps = LinRange(model_tspan[1], model_tspan[2], steps) model = generate_internal_model(model_type) - generate_time_variable!(model, model_tspan, steps) + generate_time_variable!(model, model_tspan, tsteps) U = generate_state_variable!(model, u0, length(states), tsteps) V = generate_input_variable!(model, c0, length(ctrls), tsteps) tₛ = generate_timescale!(model, get(pmap, tspan[2], tspan[2]), is_free_t) @@ -269,24 +269,26 @@ function generate_internal_model end function generate_state_variable! end function generate_input_variable! end function generate_timescale! end -function set_variable_bounds! end function add_initial_constraints! end function add_constraint! end function set_variable_bounds!(m, sys, pmap, tf) @unpack model, U, V, tₛ = m + t = get_iv(sys) for (i, u) in enumerate(unknowns(sys)) + var = lowered_var(m, :U, i, t) if hasbounds(u) lo, hi = getbounds(u) - add_constraint!(m, U[i] ≳ Symbolics.fixpoint_sub(lo, pmap)) - add_constraint!(m, U[i] ≲ Symbolics.fixpoint_sub(hi, pmap)) + add_constraint!(m, var ≳ Symbolics.fixpoint_sub(lo, pmap)) + add_constraint!(m, var ≲ Symbolics.fixpoint_sub(hi, pmap)) end end for (i, v) in enumerate(unbound_inputs(sys)) + var = lowered_var(m, :V, i, t) if hasbounds(v) lo, hi = getbounds(v) - add_constraint!(m, V[i] ≳ Symbolics.fixpoint_sub(lo, pmap)) - add_constraint!(m, V[i] ≲ Symbolics.fixpoint_sub(hi, pmap)) + add_constraint!(m, var ≳ Symbolics.fixpoint_sub(lo, pmap)) + add_constraint!(m, var ≲ Symbolics.fixpoint_sub(hi, pmap)) end end if symbolic_type(tf) === ScalarSymbolic() && hasbounds(tf) @@ -319,39 +321,58 @@ time problems and unchanged otherwise. """ function substitute_integral(model, exprs, tspan) intmap = Dict() - for int in MTK.collect_applied_operators(exprs, Symbolics.Integral) - op = MTK.operation(int) - arg = only(arguments(MTK.value(int))) - lo, hi = MTK.value.((op.domain.domain.left, op.domain.domain.right)) + for int in collect_applied_operators(exprs, Symbolics.Integral) + op = operation(int) + arg = only(arguments(value(int))) + lo, hi = value.((op.domain.domain.left, op.domain.domain.right)) lo, hi = process_integral_bounds(model, (lo, hi), tspan) - intmap[int] = lowered_integral(model, expr, lo, hi) + intmap[int] = lowered_integral(model, arg, lo, hi) end - expr = map(c -> Symbolics.substitute(c, intmap), expr) - expr = value.(expr) + exprs = map(c -> Symbolics.substitute(c, intmap), exprs) + value.(exprs) end -"""Substitute variables like x(1.5) with the corresponding model variables.""" -function substitute_model_vars(model, sys, exprs) - x_ops = [MTK.operation(MTK.unwrap(st)) for st in unknowns(sys)] - c_ops = [MTK.operation(MTK.unwrap(ct)) for ct in MTK.unbound_inputs(sys)] +"""Substitute variables like x(1.5), x(t), etc. with the corresponding model variables.""" +function substitute_model_vars(model, sys, exprs, tspan) + x_ops = [operation(unwrap(st)) for st in unknowns(sys)] + c_ops = [operation(unwrap(ct)) for ct in unbound_inputs(sys)] + t = get_iv(sys) - exprs = map(c -> Symbolics.fast_substitute(c, fixed_t_map(model, x_ops, c_ops, exprs)), exprs) - exprs = map(c -> Symbolics.fast_substitute(c, whole_t_map(model, sys)), exprs) + exprs = map(c -> Symbolics.fast_substitute(c, whole_t_map(model, t, x_ops, c_ops)), exprs) + exprs = map(c -> Symbolics.fast_substitute(c, fixed_t_map(model, x_ops, c_ops)), exprs) (ti, tf) = tspan - if MTK.symbolic_type(tf) === MTK.ScalarSymbolic() + if symbolic_type(tf) === ScalarSymbolic() _tf = model.tₛ + ti exprs = map(c -> Symbolics.fast_substitute(c, Dict(tf => _tf)), exprs) exprs = map(c -> Symbolics.fast_substitute(c, free_t_map(model, _tf, x_ops, c_ops)), exprs) end + exprs +end + +"""Mappings for variables that depend on the final time parameter, x(tf).""" +function free_t_map(m, tf, x_ops, c_ops) + Dict([[x(tf) => lowered_var(m, :U, i, 1) for (i, x) in enumerate(x_ops)]; + [c(tf) => lowered_var(m, :V, i, 1) for (i, c) in enumerate(c_ops)]]) +end + +"""Mappings for variables that cover the whole timespan, x(t).""" +function whole_t_map(m, t, x_ops, c_ops) + Dict([[v(t) => lowered_var(m, :U, i, t) for (i, v) in enumerate(x_ops)]; + [v(t) => lowered_var(m, :V, i, t) for (i, v) in enumerate(c_ops)]]) +end + +"""Mappings for variables that cover the whole timespan, x(t).""" +function fixed_t_map(m, x_ops, c_ops) + Dict([[v => (t -> lowered_var(m, :U, i, t)) for (i, v) in enumerate(x_ops)]; + [v => (t -> lowered_var(m, :V, i, t)) for (i, v) in enumerate(c_ops)]]) end function process_integral_bounds end function lowered_integral end function lowered_derivative end -function free_t_map end +function lowered_var end function fixed_t_map end -function whole_t_map end function add_user_constraints!(model, sys, tspan, pmap) conssys = get_constraintsystem(sys) @@ -372,7 +393,7 @@ end function add_equational_constraints!(model, sys, pmap, tspan) diff_eqs = substitute_model_vars(model, sys, diff_equations(sys), tspan) diff_eqs = substitute_params(pmap, diff_eqs) - diff_eqs = substitute_differentials(model, sys, diff_eqs, tspan) + diff_eqs = substitute_differentials(model, sys, diff_eqs) for eq in diff_eqs add_constraint!(model, eq.lhs ~ eq.rhs * model.tₛ) end @@ -387,9 +408,10 @@ end function set_objective! end function substitute_differentials(model, sys, eqs) - D = Differential(MTK.get_iv(sys)) - diffsubmap = Dict([D(model.U[i]) => lowered_derivative(model, i) for i in 1:length(U)]) - diff_eqs = map(c -> Symbolics.substitute(c, diffsubmap), diff_eqs) + t = get_iv(sys) + D = Differential(t) + diffsubmap = Dict([D(lowered_var(model, :U, i, t)) => lowered_derivative(model, i) for i in 1:length(unknowns(sys))]) + map(c -> Symbolics.substitute(c, diffsubmap), eqs) end function substitute_toterm(vars, exprs) diff --git a/test/extensions/dynamic_optimization.jl b/test/extensions/dynamic_optimization.jl index d476b7a066..706eb11789 100644 --- a/test/extensions/dynamic_optimization.jl +++ b/test/extensions/dynamic_optimization.jl @@ -45,7 +45,7 @@ const M = ModelingToolkit csol2 = solve(cprob, CasADiCollocation("ipopt", constructImplicitEuler())) @test ≈(csol2.sol.u, osol2.u, rtol = 0.001) pprob = PyomoDynamicOptProblem(sys, u0map, tspan, parammap, dt = 0.01) - psol = solve(cprob, PyomoCollocation("ipopt", BackwardEuler())) + psol = solve(pprob, PyomoCollocation("ipopt", BackwardEuler())) @test psol.sol.u ≈ osol2.u # With a constraint @@ -118,7 +118,7 @@ end # Double integrator t = M.t_nounits D = M.D_nounits - @variables x(..) [bounds = (0.0, 0.25)] v(..) + @variables x(..) v(..) @variables u(..) [bounds = (-1.0, 1.0), input = true] constr = [v(1.0) ~ 0.0] cost = [-x(1.0)] # Maximize the final distance. @@ -130,7 +130,7 @@ end tspan = (0.0, 1.0) parammap = [u(t) => 0.0] jprob = JuMPDynamicOptProblem(block, u0map, tspan, parammap; dt = 0.01) - jsol = solve(jprob, JuMPCollocation(Ipopt.Optimizer, constructVerner8())) + jsol = solve(jprob, JuMPCollocation(Ipopt.Optimizer, constructVerner8()), verbose = true) # Linear systems have bang-bang controls @test is_bangbang(jsol.input_sol, [-1.0], [1.0]) # Test reached final position. From 3c591c057f5b75c7d4a0585650df427bef4e2cf9 Mon Sep 17 00:00:00 2001 From: vyudu Date: Mon, 26 May 2025 14:22:22 -0400 Subject: [PATCH 1980/2176] feat: PyomoDynamicOtpPRoblem --- ext/MTKInfiniteOptExt.jl | 21 +++++----- ext/MTKPyomoDynamicOptExt.jl | 50 +++++++++++++----------- src/systems/optimal_control_interface.jl | 15 +++---- test/extensions/dynamic_optimization.jl | 1 + 4 files changed, 47 insertions(+), 40 deletions(-) diff --git a/ext/MTKInfiniteOptExt.jl b/ext/MTKInfiniteOptExt.jl index 8cd83e4323..21cf117ab3 100644 --- a/ext/MTKInfiniteOptExt.jl +++ b/ext/MTKInfiniteOptExt.jl @@ -186,6 +186,7 @@ function MTK.prepare_and_optimize!(prob::JuMPDynamicOptProblem, solver::JuMPColl add_solve_constraints!(prob, solver.tableau) set_optimizer(model, solver.solver) optimize!(model) + model end function MTK.prepare_and_optimize!(prob::InfiniteOptDynamicOptProblem, solver::InfiniteOptCollocation; verbose = false, kwargs...) @@ -194,26 +195,26 @@ function MTK.prepare_and_optimize!(prob::InfiniteOptDynamicOptProblem, solver::I set_derivative_method(model[:t], solver.derivative_method) set_optimizer(model, solver.solver) optimize!(model) + model end -function MTK.get_V_values(m::InfiniteOptModel) - nt = length(supports(m.model[:t])) - if !isempty(m.V) - V_vals = value.(m.V) +function MTK.get_V_values(m::InfiniteModel) + nt = length(supports(m[:t])) + if !isempty(m[:V]) + V_vals = value.(m[:V]) V_vals = [[V_vals[i][j] for i in 1:length(V_vals)] for j in 1:nt] else nothing end end -function MTK.get_U_values(m::InfiniteOptModel) - nt = length(supports(m.model[:t])) - U_vals = value.(m.U) +function MTK.get_U_values(m::InfiniteModel) + nt = length(supports(m[:t])) + U_vals = value.(m[:U]) U_vals = [[U_vals[i][j] for i in 1:length(U_vals)] for j in 1:nt] end -MTK.get_t_values(model) = value(model.tₛ) * supports(model.model[:t]) +MTK.get_t_values(m::InfiniteModel) = value(m[:tₛ]) * supports(m[:t]) -function MTK.successful_solve(m::InfiniteOptModel) - model = m.model +function MTK.successful_solve(model::InfiniteModel) tstatus = termination_status(model) pstatus = primal_status(model) !has_values(model) && diff --git a/ext/MTKPyomoDynamicOptExt.jl b/ext/MTKPyomoDynamicOptExt.jl index 5e5087fd9f..69c2bdaffd 100644 --- a/ext/MTKPyomoDynamicOptExt.jl +++ b/ext/MTKPyomoDynamicOptExt.jl @@ -4,23 +4,26 @@ using Pyomo using DiffEqBase using UnPack using NaNMath +using Setfield const MTK = ModelingToolkit struct PyomoDynamicOptModel model::ConcreteModel U::PyomoVar V::PyomoVar - tₛ::Union{Int, PyomoVar} + tₛ::PyomoVar is_free_final::Bool + solver_model::Union{Nothing, ConcreteModel} dU::PyomoVar model_sym::Union{Num, Symbolics.BasicSymbolic} t_sym::Union{Num, Symbolics.BasicSymbolic} - idx_sym::Union{Num, Symbolics.BasicSymbolic} + uidx_sym::Union{Num, Symbolics.BasicSymbolic} + vidx_sym::Union{Num, Symbolics.BasicSymbolic} function PyomoDynamicOptModel(model, U, V, tₛ, is_free_final) - @variables MODEL_SYM::Symbolics.symstruct(PyomoDynamicOptModel) IDX_SYM::Int T_SYM + @variables MODEL_SYM::Symbolics.symstruct(ConcreteModel) U_IDX_SYM::Int V_IDX_SYM::Int T_SYM model.dU = dae.DerivativeVar(U, wrt = model.t, initialize = 0) - new(model, U, V, tₛ, is_free_final, PyomoVar(model.dU), MODEL_SYM, T_SYM, IDX_SYM) + new(model, U, V, tₛ, is_free_final, nothing, PyomoVar(model.dU), MODEL_SYM, T_SYM, U_IDX_SYM, V_IDX_SYM) end end @@ -39,6 +42,9 @@ struct PyomoDynamicOptProblem{uType, tType, isinplace, P, F, K} <: end end +pysym_getproperty(s, name::Symbol) = Symbolics.wrap(SymbolicUtils.term(_getproperty, s, Val{name}(), type = Symbolics.Struct{PyomoVar})) +_getproperty(s, name::Val{fieldname}) where fieldname = getproperty(s, fieldname) + function MTK.PyomoDynamicOptProblem(sys::ODESystem, u0map, tspan, pmap; dt = nothing, steps = nothing, guesses = Dict(), kwargs...) @@ -68,12 +74,11 @@ function MTK.generate_input_variable!(m::ConcreteModel, c0, nc, ts) end function MTK.generate_timescale!(m::ConcreteModel, guess, is_free_t) - m.tₛ = is_free_t ? PyomoVar(pyomo.Var(initialize = guess, bounds = (0, Inf))) : 1 + m.tₛ = is_free_t ? PyomoVar(pyomo.Var(initialize = guess, bounds = (0, Inf))) : PyomoVar(Pyomo.Py(1)) end -function MTK.add_constraint!(pmodel::PyomoDynamicOptModel, cons) - @unpack model, model_sym, idx_sym, t_sym = pmodel - @show model.dU +function MTK.add_constraint!(pmodel::PyomoDynamicOptModel, cons; n_idxs = 1) + @unpack model, model_sym, t_sym = pmodel expr = if cons isa Equation cons.lhs - cons.rhs == 0 elseif cons.relational_op === Symbolics.geq @@ -81,11 +86,10 @@ function MTK.add_constraint!(pmodel::PyomoDynamicOptModel, cons) else cons.lhs - cons.rhs ≤ 0 end - constraint_f = Symbolics.build_function(expr, model_sym, idx_sym, t_sym, expression = Val{false}) - @show typeof(constraint_f) - @show typeof(Pyomo.pyfunc(constraint_f)) - cons_sym = gensym() - setproperty!(model, cons_sym, pyomo.Constraint(model.u_idxs, model.t, rule = Pyomo.pyfunc(constraint_f))) + f_expr = Symbolics.build_function(expr, model_sym, t_sym) + cons_sym = Symbol("cons", hash(cons)) + constraint_f = eval(:(cons_sym = $f_expr)) + setproperty!(model, cons_sym, pyomo.Constraint(model.t, rule = Pyomo.pyfunc(constraint_f))) end function MTK.set_objective!(m::PyomoDynamicOptModel, expr) @@ -107,12 +111,12 @@ end MTK.process_integral_bounds(model, integral_span, tspan) = integral_span function MTK.lowered_derivative(m::PyomoDynamicOptModel, i) - mdU = Symbolics.symbolic_getproperty(m.model_sym, :dU).val + mdU = Symbolics.value(pysym_getproperty(m.model_sym, :dU)) Symbolics.unwrap(mdU[i, m.t_sym]) end function MTK.lowered_var(m::PyomoDynamicOptModel, uv, i, t) - X = Symbolics.symbolic_getproperty(m.model_sym, uv).val + X = Symbolics.value(pysym_getproperty(m.model_sym, uv)) var = t isa Union{Num, Symbolics.Symbolic} ? X[i, m.t_sym] : X[i, t] Symbolics.unwrap(var) end @@ -125,21 +129,21 @@ end MTK.PyomoCollocation(solver, derivative_method = LagrangeRadau(5)) = PyomoCollocation(solver, derivative_method) function MTK.prepare_and_optimize!(prob::PyomoDynamicOptProblem, collocation; verbose, kwargs...) - m = prob.wrapped_model.model + solver_m = prob.wrapped_model.model.clone() dm = collocation.derivative_method discretizer = TransformationFactory(dm) ncp = Pyomo.is_finite_difference(dm) ? 1 : dm.np - discretizer.apply_to(m, wrt = m.t, nfe = m.steps, scheme = Pyomo.scheme_string(dm)) + discretizer.apply_to(solver_m, wrt = solver_m.t, nfe = solver_m.steps, scheme = Pyomo.scheme_string(dm)) solver = SolverFactory(string(collocation.solver)) - solver.solve(m, tee = true) - Main.xx[] = solver + solver.solve(solver_m, tee = true) + solver_m end -MTK.get_U_values(m::PyomoDynamicOptModel) = [pyomo.value(m.model.U[i]) for i in m.model.U.index_set()] -MTK.get_V_values(m::PyomoDynamicOptModel) = [pyomo.value(m.model.V[i]) for i in m.model.V.index_set()] -MTK.get_t_values(m::PyomoDynamicOptModel) = Pyomo.get_results(m.model, :t) +MTK.get_U_values(m::ConcreteModel) = [[pyomo.value(m.U[i, t]) for i in m.u_idxs] for t in m.t] +MTK.get_V_values(m::ConcreteModel) = [[pyomo.value(m.V[i, t]) for i in m.v_idxs] for t in m.t] +MTK.get_t_values(m::ConcreteModel) = [t for t in m.t] -function MTK.successful_solve(m::PyomoDynamicOptModel) +function MTK.successful_solve(m::ConcreteModel) ss = m.solver.status tc = m.solver.termination_condition if ss == opt.SolverStatus.ok && (tc == opt.TerminationStatus.optimal || tc == opt.TerminationStatus.locallyOptimal) diff --git a/src/systems/optimal_control_interface.jl b/src/systems/optimal_control_interface.jl index b094c4a24f..7e59d893cc 100644 --- a/src/systems/optimal_control_interface.jl +++ b/src/systems/optimal_control_interface.jl @@ -395,6 +395,7 @@ function add_equational_constraints!(model, sys, pmap, tspan) diff_eqs = substitute_params(pmap, diff_eqs) diff_eqs = substitute_differentials(model, sys, diff_eqs) for eq in diff_eqs + @show typeof(eq.lhs) add_constraint!(model, eq.lhs ~ eq.rhs * model.tₛ) end @@ -411,7 +412,7 @@ function substitute_differentials(model, sys, eqs) t = get_iv(sys) D = Differential(t) diffsubmap = Dict([D(lowered_var(model, :U, i, t)) => lowered_derivative(model, i) for i in 1:length(unknowns(sys))]) - map(c -> Symbolics.substitute(c, diffsubmap), eqs) + eqs = map(c -> Symbolics.substitute(c, diffsubmap), eqs) end function substitute_toterm(vars, exprs) @@ -451,21 +452,21 @@ function successful_solve end - kwargs are used for other options. For example, the `plugin_options` and `solver_options` will propagated to the Opti object in CasADi. """ function DiffEqBase.solve(prob::AbstractDynamicOptProblem, solver::AbstractCollocation; verbose = false, kwargs...) - prepare_and_optimize!(prob, solver; verbose, kwargs...) + solved_model = prepare_and_optimize!(prob, solver; verbose, kwargs...) - ts = get_t_values(prob.wrapped_model) - Us = get_U_values(prob.wrapped_model) - Vs = get_V_values(prob.wrapped_model) + ts = get_t_values(solved_model) + Us = get_U_values(solved_model) + Vs = get_V_values(solved_model) is_free_final(prob.wrapped_model) && (ts .+ prob.tspan[1]) ode_sol = DiffEqBase.build_solution(prob, solver, ts, Us) input_sol = isnothing(Vs) ? nothing : DiffEqBase.build_solution(prob, solver, ts, Vs) - if !successful_solve(prob.wrapped_model) + if !successful_solve(solved_model) ode_sol = SciMLBase.solution_new_retcode( ode_sol, SciMLBase.ReturnCode.ConvergenceFailure) !isnothing(input_sol) && (input_sol = SciMLBase.solution_new_retcode( input_sol, SciMLBase.ReturnCode.ConvergenceFailure)) end - DynamicOptSolution(prob.wrapped_model.model, ode_sol, input_sol) + DynamicOptSolution(solved_model, ode_sol, input_sol) end diff --git a/test/extensions/dynamic_optimization.jl b/test/extensions/dynamic_optimization.jl index 706eb11789..e7056b2399 100644 --- a/test/extensions/dynamic_optimization.jl +++ b/test/extensions/dynamic_optimization.jl @@ -6,6 +6,7 @@ using OrdinaryDiffEqSDIRK, OrdinaryDiffEqVerner, OrdinaryDiffEqTsit5, OrdinaryDi using Ipopt using DataInterpolations using CasADi +using Pyomo import DiffEqBase: solve const M = ModelingToolkit From 33ad60a68cb92f7285055d342e70e9fefed19ee2 Mon Sep 17 00:00:00 2001 From: vyudu Date: Tue, 27 May 2025 01:34:16 -0400 Subject: [PATCH 1981/2176] fix: fix constraint and cost transcription for pyomo --- ext/MTKCasADiDynamicOptExt.jl | 1 + ext/MTKPyomoDynamicOptExt.jl | 72 ++++++++++++++++++------ src/systems/optimal_control_interface.jl | 8 +-- test/extensions/dynamic_optimization.jl | 13 +++-- 4 files changed, 65 insertions(+), 29 deletions(-) diff --git a/ext/MTKCasADiDynamicOptExt.jl b/ext/MTKCasADiDynamicOptExt.jl index d678e853e6..12ecf31196 100644 --- a/ext/MTKCasADiDynamicOptExt.jl +++ b/ext/MTKCasADiDynamicOptExt.jl @@ -183,6 +183,7 @@ function MTK.prepare_and_optimize!(prob::CasADiDynamicOptProblem, solver::CasADi catch ErrorException end prob.wrapped_model.solver_opti = solver_opti + prob.wrapped_model end function MTK.get_U_values(model::CasADiModel) diff --git a/ext/MTKPyomoDynamicOptExt.jl b/ext/MTKPyomoDynamicOptExt.jl index 69c2bdaffd..bff1460ceb 100644 --- a/ext/MTKPyomoDynamicOptExt.jl +++ b/ext/MTKPyomoDynamicOptExt.jl @@ -7,6 +7,20 @@ using NaNMath using Setfield const MTK = ModelingToolkit +SPECIAL_FUNCTIONS_DICT = Dict([acos => Pyomo.py_acos, + log1p => Pyomo.py_log1p, + acosh => Pyomo.py_acosh, + log2 => Pyomo.py_log2, + asin => Pyomo.py_asin, + tan => Pyomo.py_tan, + atanh => Pyomo.py_atanh, + cos => Pyomo.py_cos, + log => Pyomo.py_log, + sin => Pyomo.py_sin, + log10 => Pyomo.py_log10, + sqrt => Pyomo.py_sqrt, + exp => Pyomo.py_exp]) + struct PyomoDynamicOptModel model::ConcreteModel U::PyomoVar @@ -17,13 +31,12 @@ struct PyomoDynamicOptModel dU::PyomoVar model_sym::Union{Num, Symbolics.BasicSymbolic} t_sym::Union{Num, Symbolics.BasicSymbolic} - uidx_sym::Union{Num, Symbolics.BasicSymbolic} - vidx_sym::Union{Num, Symbolics.BasicSymbolic} + dummy_sym::Union{Num, Symbolics.BasicSymbolic} function PyomoDynamicOptModel(model, U, V, tₛ, is_free_final) - @variables MODEL_SYM::Symbolics.symstruct(ConcreteModel) U_IDX_SYM::Int V_IDX_SYM::Int T_SYM + @variables MODEL_SYM::Symbolics.symstruct(ConcreteModel) T_SYM DUMMY_SYM model.dU = dae.DerivativeVar(U, wrt = model.t, initialize = 0) - new(model, U, V, tₛ, is_free_final, nothing, PyomoVar(model.dU), MODEL_SYM, T_SYM, U_IDX_SYM, V_IDX_SYM) + new(model, U, V, tₛ, is_free_final, nothing, PyomoVar(model.dU), MODEL_SYM, T_SYM, DUMMY_SYM) end end @@ -42,7 +55,7 @@ struct PyomoDynamicOptProblem{uType, tType, isinplace, P, F, K} <: end end -pysym_getproperty(s, name::Symbol) = Symbolics.wrap(SymbolicUtils.term(_getproperty, s, Val{name}(), type = Symbolics.Struct{PyomoVar})) +pysym_getproperty(s::Union{Num, Symbolics.Symbolic}, name::Symbol) = Symbolics.wrap(SymbolicUtils.term(_getproperty, Symbolics.unwrap(s), Val{name}(), type = Symbolics.Struct{PyomoVar})) _getproperty(s, name::Val{fieldname}) where fieldname = getproperty(s, fieldname) function MTK.PyomoDynamicOptProblem(sys::ODESystem, u0map, tspan, pmap; @@ -78,7 +91,7 @@ function MTK.generate_timescale!(m::ConcreteModel, guess, is_free_t) end function MTK.add_constraint!(pmodel::PyomoDynamicOptModel, cons; n_idxs = 1) - @unpack model, model_sym, t_sym = pmodel + @unpack model, model_sym, t_sym, dummy_sym = pmodel expr = if cons isa Equation cons.lhs - cons.rhs == 0 elseif cons.relational_op === Symbolics.geq @@ -86,14 +99,30 @@ function MTK.add_constraint!(pmodel::PyomoDynamicOptModel, cons; n_idxs = 1) else cons.lhs - cons.rhs ≤ 0 end - f_expr = Symbolics.build_function(expr, model_sym, t_sym) + expr = Symbolics.substitute(expr, SPECIAL_FUNCTIONS_DICT) + @show expr + cons_sym = Symbol("cons", hash(cons)) - constraint_f = eval(:(cons_sym = $f_expr)) - setproperty!(model, cons_sym, pyomo.Constraint(model.t, rule = Pyomo.pyfunc(constraint_f))) + if occursin(Symbolics.unwrap(t_sym), expr) + f = eval(Symbolics.build_function(expr, model_sym, t_sym)) + setproperty!(model, cons_sym, pyomo.Constraint(model.t, rule = Pyomo.pyfunc(f))) + else + f = eval(Symbolics.build_function(expr, model_sym, dummy_sym)) + setproperty!(model, cons_sym, pyomo.Constraint(rule = Pyomo.pyfunc(f))) + end end -function MTK.set_objective!(m::PyomoDynamicOptModel, expr) - m.model.obj = pyomo.Objective(expr = expr) +function MTK.set_objective!(pmodel::PyomoDynamicOptModel, expr) + @unpack model, model_sym, t_sym, dummy_sym = pmodel + @show expr + expr = Symbolics.substitute(expr, SPECIAL_FUNCTIONS_DICT) + if occursin(Symbolics.unwrap(t_sym), expr) + f = eval(Symbolics.build_function(expr, model_sym, t_sym)) + model.obj = pyomo.Objective(model.t, rule = Pyomo.pyfunc(f)) + else + f = eval(Symbolics.build_function(expr, model_sym, dummy_sym)) + model.obj = pyomo.Objective(rule = Pyomo.pyfunc(f)) + end end function MTK.add_initial_constraints!(model::PyomoDynamicOptModel, u0, u0_idxs, ts) @@ -104,8 +133,14 @@ end function MTK.lowered_integral(m::PyomoDynamicOptModel, arg, lo, hi) @unpack model, model_sym, t_sym = m - arg_f = Symbolics.build_function(arg, model_sym, t_sym) - dae.Integral(model.t, wrt = model.t, rule=arg_f) + @show arg + @show Symbolics.build_function(arg, model_sym, t_sym) + arg_f = eval(Symbolics.build_function(arg, model_sym, t_sym)) + @show arg_f + int_sym = Symbol(:int, hash(arg)) + @show int_sym + setproperty!(model, int_sym, dae.Integral(model.t, wrt = model.t, rule=Pyomo.pyfunc(arg_f))) + PyomoVar(model.tₛ * model.int_sym) end MTK.process_integral_bounds(model, integral_span, tspan) = integral_span @@ -135,15 +170,16 @@ function MTK.prepare_and_optimize!(prob::PyomoDynamicOptProblem, collocation; ve ncp = Pyomo.is_finite_difference(dm) ? 1 : dm.np discretizer.apply_to(solver_m, wrt = solver_m.t, nfe = solver_m.steps, scheme = Pyomo.scheme_string(dm)) solver = SolverFactory(string(collocation.solver)) - solver.solve(solver_m, tee = true) - solver_m + results = solver.solve(solver_m, tee = true) + ConcreteModel(solver_m) end -MTK.get_U_values(m::ConcreteModel) = [[pyomo.value(m.U[i, t]) for i in m.u_idxs] for t in m.t] -MTK.get_V_values(m::ConcreteModel) = [[pyomo.value(m.V[i, t]) for i in m.v_idxs] for t in m.t] -MTK.get_t_values(m::ConcreteModel) = [t for t in m.t] +MTK.get_U_values(m::ConcreteModel) = [[Pyomo.pyconvert(Float64, pyomo.value(m.U[i, t])) for i in m.u_idxs] for t in m.t] +MTK.get_V_values(m::ConcreteModel) = [[Pyomo.pyconvert(Float64, pyomo.value(m.V[i, t])) for i in m.v_idxs] for t in m.t] +MTK.get_t_values(m::ConcreteModel) = [Pyomo.pyconvert(Float64, t) for t in m.t] function MTK.successful_solve(m::ConcreteModel) + return true ss = m.solver.status tc = m.solver.termination_condition if ss == opt.SolverStatus.ok && (tc == opt.TerminationStatus.optimal || tc == opt.TerminationStatus.locallyOptimal) diff --git a/src/systems/optimal_control_interface.jl b/src/systems/optimal_control_interface.jl index 7e59d893cc..f6cb499e91 100644 --- a/src/systems/optimal_control_interface.jl +++ b/src/systems/optimal_control_interface.jl @@ -328,8 +328,7 @@ function substitute_integral(model, exprs, tspan) lo, hi = process_integral_bounds(model, (lo, hi), tspan) intmap[int] = lowered_integral(model, arg, lo, hi) end - exprs = map(c -> Symbolics.substitute(c, intmap), exprs) - value.(exprs) + exprs = map(c -> Symbolics.substitute(c, intmap), value.(exprs)) end """Substitute variables like x(1.5), x(t), etc. with the corresponding model variables.""" @@ -339,14 +338,14 @@ function substitute_model_vars(model, sys, exprs, tspan) t = get_iv(sys) exprs = map(c -> Symbolics.fast_substitute(c, whole_t_map(model, t, x_ops, c_ops)), exprs) - exprs = map(c -> Symbolics.fast_substitute(c, fixed_t_map(model, x_ops, c_ops)), exprs) (ti, tf) = tspan if symbolic_type(tf) === ScalarSymbolic() _tf = model.tₛ + ti + exprs = map(c -> Symbolics.fast_substitute(c, free_t_map(model, tf, x_ops, c_ops)), exprs) exprs = map(c -> Symbolics.fast_substitute(c, Dict(tf => _tf)), exprs) - exprs = map(c -> Symbolics.fast_substitute(c, free_t_map(model, _tf, x_ops, c_ops)), exprs) end + exprs = map(c -> Symbolics.fast_substitute(c, fixed_t_map(model, x_ops, c_ops)), exprs) exprs end @@ -395,7 +394,6 @@ function add_equational_constraints!(model, sys, pmap, tspan) diff_eqs = substitute_params(pmap, diff_eqs) diff_eqs = substitute_differentials(model, sys, diff_eqs) for eq in diff_eqs - @show typeof(eq.lhs) add_constraint!(model, eq.lhs ~ eq.rhs * model.tₛ) end diff --git a/test/extensions/dynamic_optimization.jl b/test/extensions/dynamic_optimization.jl index e7056b2399..4f52b08d67 100644 --- a/test/extensions/dynamic_optimization.jl +++ b/test/extensions/dynamic_optimization.jl @@ -47,7 +47,7 @@ const M = ModelingToolkit @test ≈(csol2.sol.u, osol2.u, rtol = 0.001) pprob = PyomoDynamicOptProblem(sys, u0map, tspan, parammap, dt = 0.01) psol = solve(pprob, PyomoCollocation("ipopt", BackwardEuler())) - @test psol.sol.u ≈ osol2.u + @test all([≈(psol.sol(t), osol2(t), rtol = 1e-3) for t in 0.:0.01:1.]) # With a constraint u0map = Pair[] @@ -69,6 +69,7 @@ const M = ModelingToolkit pprob = PyomoDynamicOptProblem( lksys, u0map, tspan, parammap; guesses = guess, dt = 0.01) psol = solve(pprob, PyomoCollocation("ipopt", LagrangeLegendre(3))) + @show psol.sol @test psol.sol(0.6)[1] ≈ 3.5 @test psol.sol(0.3)[1] ≈ 7.0 @@ -87,7 +88,7 @@ const M = ModelingToolkit isol = solve(iprob, InfiniteOptCollocation(Ipopt.Optimizer, InfiniteOpt.OrthogonalCollocation(3))) @test all(u -> u > [1, 1], isol.sol.u) - jprob = PyoDynamicOptProblem(lksys, u0map, tspan, parammap; guesses = guess, dt = 0.01) + jprob = JuMPDynamicOptProblem(lksys, u0map, tspan, parammap; guesses = guess, dt = 0.01) jsol = solve(jprob, JuMPCollocation(Ipopt.Optimizer, constructRadauIA3())) @test all(u -> u > [1, 1], jsol.sol.u) @@ -160,11 +161,11 @@ end pprob = PyomoDynamicOptProblem(block, u0map, tspan, parammap; dt = 0.01) psol = solve(pprob, PyomoCollocation("ipopt", BackwardEuler())) @test is_bangbang(psol.input_sol, [-1.0], [1.0]) - @test ≈(psol.sol.u[end][2], 0.25, rtol = 1e-5) + @test ≈(psol.sol.u[end][2], 0.25, rtol = 1e-3) osol = solve(oprob, ImplicitEuler(); dt = 0.01, adaptive = false) @test ≈(isol.sol.u, osol.u, rtol = 0.05) - @test ≈(psol.sol.u, osol.u, rtol = 0.05) + @test all([≈(psol.sol(t), osol(t), rtol = 0.05) for t in 0.:0.01:1.]) ################### ### Bee example ### @@ -209,7 +210,7 @@ end @test ≈(osol.u, csol.sol.u, rtol = 0.01) osol2 = solve(oprob, ImplicitEuler(); dt = 0.01, adaptive = false) @test ≈(osol2.u, isol.sol.u, rtol = 0.01) - @test ≈(osol2.u, psol.sol.u, rtol = 0.01) + @test all([≈(psol.sol(t), osol2(t), rtol = 0.01) for t in 0.:0.01:4.]) end @testset "Rocket launch" begin @@ -248,7 +249,7 @@ end isol = solve(iprob, InfiniteOptCollocation(Ipopt.Optimizer)) @test isol.sol[h(t)][end] > 1.012 - pprob = PyomoCollocationDynamicOptProblem(rocket, u0map, (ts, te), pmap; dt = 0.001, cse = false) + pprob = PyomoDynamicOptProblem(rocket, u0map, (ts, te), pmap; dt = 0.001, cse = false) psol = solve(pprob, PyomoCollocation("ipopt")) @test psol.sol.u[end][1] > 1.012 From 5988c8123be5b259c64824fdb00766561a98cb4d Mon Sep 17 00:00:00 2001 From: vyudu Date: Wed, 28 May 2025 12:07:19 -0400 Subject: [PATCH 1982/2176] fix: pyomo needs one fewer finite element than # tsteps --- ext/MTKPyomoDynamicOptExt.jl | 49 ++++++++++++++++--------- test/extensions/dynamic_optimization.jl | 8 ++-- 2 files changed, 35 insertions(+), 22 deletions(-) diff --git a/ext/MTKPyomoDynamicOptExt.jl b/ext/MTKPyomoDynamicOptExt.jl index bff1460ceb..bbd6dba115 100644 --- a/ext/MTKPyomoDynamicOptExt.jl +++ b/ext/MTKPyomoDynamicOptExt.jl @@ -76,18 +76,21 @@ end function MTK.generate_state_variable!(m::ConcreteModel, u0, ns, ts) m.u_idxs = pyomo.RangeSet(1, ns) - m.U = pyomo.Var(m.u_idxs, m.t, initialize = 0) + init_f = Pyomo.pyfunc((m, i, t) -> (u0[Pyomo.pyconvert(Int, i)])) + m.U = pyomo.Var(m.u_idxs, m.t, initialize = init_f) PyomoVar(m.U) end function MTK.generate_input_variable!(m::ConcreteModel, c0, nc, ts) m.v_idxs = pyomo.RangeSet(1, nc) - m.V = pyomo.Var(m.v_idxs, m.t, initialize = 0) + init_f = Pyomo.pyfunc((m, i, t) -> (c0[Pyomo.pyconvert(Int, i)])) + m.V = pyomo.Var(m.v_idxs, m.t, initialize = init_f) PyomoVar(m.V) end function MTK.generate_timescale!(m::ConcreteModel, guess, is_free_t) - m.tₛ = is_free_t ? PyomoVar(pyomo.Var(initialize = guess, bounds = (0, Inf))) : PyomoVar(Pyomo.Py(1)) + m.tₛ = is_free_t ? pyomo.Var(initialize = guess, bounds = (0, Inf)) : Pyomo.Py(1) + PyomoVar(m.tₛ) end function MTK.add_constraint!(pmodel::PyomoDynamicOptModel, cons; n_idxs = 1) @@ -100,7 +103,6 @@ function MTK.add_constraint!(pmodel::PyomoDynamicOptModel, cons; n_idxs = 1) cons.lhs - cons.rhs ≤ 0 end expr = Symbolics.substitute(expr, SPECIAL_FUNCTIONS_DICT) - @show expr cons_sym = Symbol("cons", hash(cons)) if occursin(Symbolics.unwrap(t_sym), expr) @@ -133,12 +135,8 @@ end function MTK.lowered_integral(m::PyomoDynamicOptModel, arg, lo, hi) @unpack model, model_sym, t_sym = m - @show arg - @show Symbolics.build_function(arg, model_sym, t_sym) arg_f = eval(Symbolics.build_function(arg, model_sym, t_sym)) - @show arg_f int_sym = Symbol(:int, hash(arg)) - @show int_sym setproperty!(model, int_sym, dae.Integral(model.t, wrt = model.t, rule=Pyomo.pyfunc(arg_f))) PyomoVar(model.tₛ * model.int_sym) end @@ -168,21 +166,36 @@ function MTK.prepare_and_optimize!(prob::PyomoDynamicOptProblem, collocation; ve dm = collocation.derivative_method discretizer = TransformationFactory(dm) ncp = Pyomo.is_finite_difference(dm) ? 1 : dm.np - discretizer.apply_to(solver_m, wrt = solver_m.t, nfe = solver_m.steps, scheme = Pyomo.scheme_string(dm)) + discretizer.apply_to(solver_m, wrt = solver_m.t, nfe = solver_m.steps - 1, scheme = Pyomo.scheme_string(dm)) solver = SolverFactory(string(collocation.solver)) results = solver.solve(solver_m, tee = true) - ConcreteModel(solver_m) + PyomoOutput(results, solver_m) +end + +struct PyomoOutput + result::Pyomo.Py + model::Pyomo.Py end -MTK.get_U_values(m::ConcreteModel) = [[Pyomo.pyconvert(Float64, pyomo.value(m.U[i, t])) for i in m.u_idxs] for t in m.t] -MTK.get_V_values(m::ConcreteModel) = [[Pyomo.pyconvert(Float64, pyomo.value(m.V[i, t])) for i in m.v_idxs] for t in m.t] -MTK.get_t_values(m::ConcreteModel) = [Pyomo.pyconvert(Float64, t) for t in m.t] +function MTK.get_U_values(output::PyomoOutput) + m = output.model + [[Pyomo.pyconvert(Float64, pyomo.value(m.U[i, t])) for i in m.u_idxs] for t in m.t] +end +function MTK.get_V_values(output::PyomoOutput) + m = output.model + [[Pyomo.pyconvert(Float64, pyomo.value(m.V[i, t])) for i in m.v_idxs] for t in m.t] +end +function MTK.get_t_values(output::PyomoOutput) + m = output.model + Pyomo.pyconvert(Float64, pyomo.value(m.tₛ)) * [Pyomo.pyconvert(Float64, t) for t in m.t] +end -function MTK.successful_solve(m::ConcreteModel) - return true - ss = m.solver.status - tc = m.solver.termination_condition - if ss == opt.SolverStatus.ok && (tc == opt.TerminationStatus.optimal || tc == opt.TerminationStatus.locallyOptimal) +function MTK.successful_solve(output::PyomoOutput) + r = output.result + ss = r.solver.status + Main.xx[] = ss + tc = r.solver.termination_condition + if Bool(ss == opt.SolverStatus.ok) && (Bool(tc == opt.TerminationCondition.optimal) || Bool(tc == opt.TerminationCondition.locallyOptimal)) return true else return false diff --git a/test/extensions/dynamic_optimization.jl b/test/extensions/dynamic_optimization.jl index 4f52b08d67..85b06eee6f 100644 --- a/test/extensions/dynamic_optimization.jl +++ b/test/extensions/dynamic_optimization.jl @@ -250,7 +250,7 @@ end @test isol.sol[h(t)][end] > 1.012 pprob = PyomoDynamicOptProblem(rocket, u0map, (ts, te), pmap; dt = 0.001, cse = false) - psol = solve(pprob, PyomoCollocation("ipopt")) + psol = solve(pprob, PyomoCollocation("ipopt", MidpointEuler())) @test psol.sol.u[end][1] > 1.012 # Test solution @@ -319,7 +319,7 @@ end u0map = [x(t) => 1.0, v(t) => 0.0] tspan = (0.0, tf) - parammap = [u(t) => 0.0, tf => 1.0] + parammap = [u(t) => 1.0, tf => 1.0] jprob = JuMPDynamicOptProblem(block, u0map, tspan, parammap; steps = 51) jsol = solve(jprob, JuMPCollocation(Ipopt.Optimizer, constructVerner8())) @test isapprox(jsol.sol.t[end], 2.0, atol = 1e-5) @@ -329,11 +329,11 @@ end @test isapprox(csol.sol.t[end], 2.0, atol = 1e-5) iprob = InfiniteOptDynamicOptProblem(block, u0map, tspan, parammap; steps = 51) - isol = solve(iprob, InfiniteOptCollocation(Ipopt.Optimizer)) + isol = solve(iprob, InfiniteOptCollocation(Ipopt.Optimizer), verbose = true) @test isapprox(isol.sol.t[end], 2.0, atol = 1e-5) pprob = PyomoDynamicOptProblem(block, u0map, tspan, parammap; steps = 51) - psol = solve(pprob, PyomoCollocation("ipopt")) + psol = solve(pprob, PyomoCollocation("ipopt", BackwardEuler())) @test isapprox(psol.sol.t[end], 2.0, atol = 1e-5) end From 5f33573f83dc8df1de3fcaf8ab29ce10e9dfacb7 Mon Sep 17 00:00:00 2001 From: vyudu Date: Thu, 29 May 2025 03:29:07 -0400 Subject: [PATCH 1983/2176] docs: dynamic optimization docs --- docs/src/tutorials/dynamic_optimization.md | 105 +++++++++++++++++++++ ext/MTKCasADiDynamicOptExt.jl | 16 +++- ext/MTKInfiniteOptExt.jl | 3 +- ext/MTKPyomoDynamicOptExt.jl | 43 ++++++--- src/systems/optimal_control_interface.jl | 13 +++ test/extensions/dynamic_optimization.jl | 23 +++-- 6 files changed, 180 insertions(+), 23 deletions(-) create mode 100644 docs/src/tutorials/dynamic_optimization.md diff --git a/docs/src/tutorials/dynamic_optimization.md b/docs/src/tutorials/dynamic_optimization.md new file mode 100644 index 0000000000..a1a1a44384 --- /dev/null +++ b/docs/src/tutorials/dynamic_optimization.md @@ -0,0 +1,105 @@ +# Solving Dynamic Optimization Problems +Systems in ModelingToolkit.jl can be directly converted to dynamic optimization or optimal control problems. In such systems, one has one or more input variables that are externally controlled to control the dynamics of the system. A dynamic optimization solves for the optimal time trajectory of the input variables in order to maximize or minimize a desired objective function. For example, a car driver might like to know how to step on the accelerator if the goal is to finish a race while using the least gas. + +To begin, let us take a rocket launch example. The input variable here is the thrust exerted by the engine. The rocket state is described by its current height and velocity. +```julia +using ModelingToolkit +t = ModelingToolkit.t_nounits +D = ModelingToolkit.D_nounits + +@parameters h_c m₀ h₀ g₀ D_c c Tₘ m_c +@variables begin + h(..) + v(..) + m(..) [bounds = (m_c, 1)] + T(..) [input = true, bounds = (0, Tₘ)] +end + +drag(h, v) = D_c * v^2 * exp(-h_c * (h - h₀) / h₀) +gravity(h) = g₀ * (h₀ / h) + +eqs = [D(h(t)) ~ v(t), + D(v(t)) ~ (T(t) - drag(h(t), v(t))) / m(t) - gravity(h(t)), + D(m(t)) ~ -T(t) / c] + +(ts, te) = (0.0, 0.2) +costs = [-h(te)] +cons = [T(te) ~ 0, m(te) ~ m_c] + +@named rocket = ODESystem(eqs, t; costs, constraints = cons) +rocket, input_idxs = structural_simplify(rocket, ([T(t)], [])) + +u0map = [h(t) => h₀, m(t) => m₀, v(t) => 0] +pmap = [ + g₀ => 1, m₀ => 1.0, h_c => 500, c => 0.5 * √(g₀ * h₀), D_c => 0.5 * 620 * m₀ / g₀, + Tₘ => 3.5 * g₀ * m₀, T(t) => 0.0, h₀ => 1, m_c => 0.6] +``` +What we would like to optimize here is the final height of the rocket. We do this by providing a vector of expressions corresponding to the costs. By default, the sense of the optimization is to minimize the provided cost. So to maximize the rocket height at the final time, we write `-h(te)` as the cost. + +Now we can construct a problem and solve it. Let us use JuMP as our backend here. +```julia +jprob = JuMPDynamicOptProblem(rocket, u0map, (ts, te), pmap; dt = 0.001, cse = false) +jsol = solve(jprob, JuMPCollocation(Ipopt.Optimizer, constructRadauIIA5())) +``` + +Let's plot our final solution and the controller here: +```julia +``` + +###### Free final time problems +There are additionally a class of dynamic optimization problems where we would like to know how to control our system to achieve something in the least time. Such problems are called free final time problems, since the final time is unknown. To model these problems in ModelingToolkit, we declare the final time as a parameter. + +```julia +@variables x(..) v(..) +@variables u(..) [bounds = (-1.0, 1.0), input = true] +@parameters tf + +constr = [v(tf) ~ 0, x(tf) ~ 0] +cost = [tf] # Minimize time + +@named block = ODESystem( + [D(x(t)) ~ v(t), D(v(t)) ~ u(t)], t; costs = cost, constraints = constr) + +block, input_idxs = structural_simplify(block, ([u(t)], [])) + +u0map = [x(t) => 1.0, v(t) => 0.0] +tspan = (0.0, tf) +parammap = [u(t) => 0.0, tf => 1.0] +``` + +Please note that, at the moment, free final time problems cannot support constraints defined at definite time values, like `x(3) ~ 2`. + +Let's plot our final solution and the controller for the block: +```julia +``` + +### Solvers +Currently 4 backends are exposed for solving dynamic optimization problems using collocation: JuMP, InfiniteOpt, CasADi, and Pyomo. + +Please note that there are differences in how to construct the collocation solver for the different cases. For example, the Python based ones, CasADi and Pyomo, expect the solver to be passed in as a string (CasADi and Pyomo come pre-loaded with certain solvers, but other solvers may need to be manually installed using `pip` or `conda`), while JuMP/InfiniteOpt expect the optimizer object to be passed in directly: +``` +JuMPCollocation(Ipopt.Optimizer, constructRK4()) +CasADiCollocation("ipopt", constructRK4()) +``` + +**JuMP** and **CasADi** collocation require an ODE tableau to be passed in. These can be constructed by calling the `constructX()` functions from DiffEqDevTools. If none is passed in, both solvers will default to using Radau second-order with five collocation points. + +**Pyomo** and **InfiniteOpt** each have their own built-in collocation methods. +1. **InfiniteOpt**: The list of InfiniteOpt collocation methods can be found [in the table on this page](https://infiniteopt.github.io/InfiniteOpt.jl/stable/guide/derivative/). If none is passed in, the solver defaults to `FiniteDifference(Backward())`, which is effectively implicit Euler. +2. **Pyomo**: The list of Pyomo collocation methods can be found [here](). If none is passed in, the solver defaults to a `LagrangeRadau(3)`. + +```@docs; canonical = false +JuMPCollocation +InfiniteOptCollocation +CasADiCollocation +PyomoCollocation +solve(::AbstractDynamicOptProblem) +``` + +### Problem constructors +```@docs; canonical = false +JuMPDynamicOptProblem +InfiniteOptDynamicOptProblem +CasADiDynamicOptProblem +PyomoDynamicOptProblem +``` diff --git a/ext/MTKCasADiDynamicOptExt.jl b/ext/MTKCasADiDynamicOptExt.jl index 12ecf31196..2b4494863c 100644 --- a/ext/MTKCasADiDynamicOptExt.jl +++ b/ext/MTKCasADiDynamicOptExt.jl @@ -120,7 +120,20 @@ function MTK.lowered_var(m::CasADiModel, uv, i, t) t isa Union{Num, Symbolics.Symbolic} ? X.u[i, :] : X(t)[i] end -MTK.lowered_integral(model::CasADiModel, expr, args...) = model.tₛ * (model.U.t[2] - model.U.t[1]) * sum(expr) +function MTK.lowered_integral(model::CasADiModel, expr, lo, hi) + total = MX(0) + dt = model.U.t[2] - model.U.t[1] + for (i, t) in enumerate(model.U.t) + if lo < t < hi + Δt = min(dt, t - lo) + total += (0.5*Δt*(expr[i] + expr[i-1])) + elseif t >= hi && (t - dt < hi) + Δt = hi - t + dt + total += (0.5*Δt*(expr[i] + expr[i-1])) + end + end + model.tₛ * total +end function add_solve_constraints!(prob::CasADiDynamicOptProblem, tableau) @unpack A, α, c = tableau @@ -210,6 +223,7 @@ function MTK.get_t_values(model::CasADiModel) value_getter = MTK.successful_solve(model) ? CasADi.debug_value : CasADi.value ts = value_getter(model.solver_opti, model.tₛ) .* model.U.t end +MTK.objective_value(model::CasADiModel) = CasADi.pyconvert(Float64, model.solver_opti.py.value(model.solver_opti.py.f)) function MTK.successful_solve(m::CasADiModel) isnothing(m.solver_opti) && return false diff --git a/ext/MTKInfiniteOptExt.jl b/ext/MTKInfiniteOptExt.jl index 21cf117ab3..31183e3761 100644 --- a/ext/MTKInfiniteOptExt.jl +++ b/ext/MTKInfiniteOptExt.jl @@ -91,7 +91,7 @@ end MTK.lowered_integral(model::InfiniteOptModel, expr, lo, hi) = model.tₛ * InfiniteOpt.∫(expr, model.model[:t], lo, hi) MTK.lowered_derivative(model::InfiniteOptModel, i) = ∂(model.U[i], model.model[:t]) -function MTK.process_integral_bounds(model, integral_span, tspan) +function MTK.process_integral_bounds(model::InfiniteOptModel, integral_span, tspan) if MTK.is_free_final(model) && isequal(integral_span, tspan) integral_span = (0, 1) elseif MTK.is_free_final(model) @@ -213,6 +213,7 @@ function MTK.get_U_values(m::InfiniteModel) U_vals = [[U_vals[i][j] for i in 1:length(U_vals)] for j in 1:nt] end MTK.get_t_values(m::InfiniteModel) = value(m[:tₛ]) * supports(m[:t]) +MTK.objective_value(m::InfiniteModel) = InfiniteOpt.objective_value(m) function MTK.successful_solve(model::InfiniteModel) tstatus = termination_status(model) diff --git a/ext/MTKPyomoDynamicOptExt.jl b/ext/MTKPyomoDynamicOptExt.jl index bbd6dba115..8b668001f0 100644 --- a/ext/MTKPyomoDynamicOptExt.jl +++ b/ext/MTKPyomoDynamicOptExt.jl @@ -8,16 +8,13 @@ using Setfield const MTK = ModelingToolkit SPECIAL_FUNCTIONS_DICT = Dict([acos => Pyomo.py_acos, - log1p => Pyomo.py_log1p, acosh => Pyomo.py_acosh, - log2 => Pyomo.py_log2, asin => Pyomo.py_asin, tan => Pyomo.py_tan, atanh => Pyomo.py_atanh, cos => Pyomo.py_cos, log => Pyomo.py_log, sin => Pyomo.py_sin, - log10 => Pyomo.py_log10, sqrt => Pyomo.py_sqrt, exp => Pyomo.py_exp]) @@ -36,10 +33,21 @@ struct PyomoDynamicOptModel function PyomoDynamicOptModel(model, U, V, tₛ, is_free_final) @variables MODEL_SYM::Symbolics.symstruct(ConcreteModel) T_SYM DUMMY_SYM model.dU = dae.DerivativeVar(U, wrt = model.t, initialize = 0) + #add_time_equation!(model, MODEL_SYM, T_SYM) new(model, U, V, tₛ, is_free_final, nothing, PyomoVar(model.dU), MODEL_SYM, T_SYM, DUMMY_SYM) end end +function add_time_equation!(model::ConcreteModel, model_sym, t_sym) + model.dtime = dae.DerivativeVar(model.time) + + mdt = Symbolics.value(pysym_getproperty(model_sym, :dtime)) + mts = Symbolics.value(pysym_getproperty(model_sym, :tₛ)) + expr = mdt[t_sym] - mts == 0 + f = Pyomo.pyfunc(eval(Symbolics.build_function(expr, model_sym, t_sym))) + model.time_eq = pyomo.Constraint(model.t, rule = f) +end + struct PyomoDynamicOptProblem{uType, tType, isinplace, P, F, K} <: AbstractDynamicOptProblem{uType, tType, isinplace} f::F @@ -72,6 +80,7 @@ MTK.generate_internal_model(m::Type{PyomoDynamicOptModel}) = ConcreteModel(pyomo function MTK.generate_time_variable!(m::ConcreteModel, tspan, tsteps) m.steps = length(tsteps) m.t = dae.ContinuousSet(initialize = tsteps, bounds = tspan) + m.time = pyomo.Var(m.t) end function MTK.generate_state_variable!(m::ConcreteModel, u0, ns, ts) @@ -116,7 +125,6 @@ end function MTK.set_objective!(pmodel::PyomoDynamicOptModel, expr) @unpack model, model_sym, t_sym, dummy_sym = pmodel - @show expr expr = Symbolics.substitute(expr, SPECIAL_FUNCTIONS_DICT) if occursin(Symbolics.unwrap(t_sym), expr) f = eval(Symbolics.build_function(expr, model_sym, t_sym)) @@ -134,15 +142,24 @@ function MTK.add_initial_constraints!(model::PyomoDynamicOptModel, u0, u0_idxs, end function MTK.lowered_integral(m::PyomoDynamicOptModel, arg, lo, hi) - @unpack model, model_sym, t_sym = m - arg_f = eval(Symbolics.build_function(arg, model_sym, t_sym)) - int_sym = Symbol(:int, hash(arg)) - setproperty!(model, int_sym, dae.Integral(model.t, wrt = model.t, rule=Pyomo.pyfunc(arg_f))) - PyomoVar(model.tₛ * model.int_sym) + @unpack model, model_sym, t_sym, dummy_sym = m + total = 0 + dt = Pyomo.pyconvert(Float64, (model.t.at(-1) - model.t.at(1))/(model.steps - 1)) + f = Symbolics.build_function(arg, model_sym, t_sym, expression = false) + for (i, t) in enumerate(model.t) + if Bool(lo < t) && Bool(t < hi) + t_p = model.t.at(i-1) + Δt = min(t - lo, t - t_p) + total += 0.5*Δt*(f(model, t) + f(model, t_p)) + elseif Bool(t >= hi) && Bool(t - dt < hi) + t_p = model.t.at(i-1) + Δt = hi - t + dt + total += 0.5*Δt*(f(model, t) + f(model, t_p)) + end + end + PyomoVar(model.tₛ * total) end -MTK.process_integral_bounds(model, integral_span, tspan) = integral_span - function MTK.lowered_derivative(m::PyomoDynamicOptModel, i) mdU = Symbolics.value(pysym_getproperty(m.model_sym, :dU)) Symbolics.unwrap(mdU[i, m.t_sym]) @@ -167,6 +184,7 @@ function MTK.prepare_and_optimize!(prob::PyomoDynamicOptProblem, collocation; ve discretizer = TransformationFactory(dm) ncp = Pyomo.is_finite_difference(dm) ? 1 : dm.np discretizer.apply_to(solver_m, wrt = solver_m.t, nfe = solver_m.steps - 1, scheme = Pyomo.scheme_string(dm)) + solver = SolverFactory(string(collocation.solver)) results = solver.solve(solver_m, tee = true) PyomoOutput(results, solver_m) @@ -190,10 +208,11 @@ function MTK.get_t_values(output::PyomoOutput) Pyomo.pyconvert(Float64, pyomo.value(m.tₛ)) * [Pyomo.pyconvert(Float64, t) for t in m.t] end +MTK.objective_value(output::PyomoOutput) = Pyomo.pyconvert(Float64, pyomo.value(output.model.obj)) + function MTK.successful_solve(output::PyomoOutput) r = output.result ss = r.solver.status - Main.xx[] = ss tc = r.solver.termination_condition if Bool(ss == opt.SolverStatus.ok) && (Bool(tc == opt.TerminationCondition.optimal) || Bool(tc == opt.TerminationCondition.locallyOptimal)) return true diff --git a/src/systems/optimal_control_interface.jl b/src/systems/optimal_control_interface.jl index f6cb499e91..d0668a98ef 100644 --- a/src/systems/optimal_control_interface.jl +++ b/src/systems/optimal_control_interface.jl @@ -331,6 +331,18 @@ function substitute_integral(model, exprs, tspan) exprs = map(c -> Symbolics.substitute(c, intmap), value.(exprs)) end +function process_integral_bounds(model, integral_span, tspan) + if is_free_final(model) && isequal(integral_span, tspan) + integral_span = (0, 1) + elseif is_free_final(model) + error("Free final time problems cannot handle partial timespans.") + else + (lo, hi) = integral_span + (lo < tspan[1] || hi > tspan[2]) && error("Integral bounds are beyond the timespan.") + integral_span + end +end + """Substitute variables like x(1.5), x(t), etc. with the corresponding model variables.""" function substitute_model_vars(model, sys, exprs, tspan) x_ops = [operation(unwrap(st)) for st in unknowns(sys)] @@ -405,6 +417,7 @@ function add_equational_constraints!(model, sys, pmap, tspan) end function set_objective! end +objective_value(sol::DynamicOptSolution) = objective_value(sol.model) function substitute_differentials(model, sys, eqs) t = get_iv(sys) diff --git a/test/extensions/dynamic_optimization.jl b/test/extensions/dynamic_optimization.jl index 85b06eee6f..feb8ddfba8 100644 --- a/test/extensions/dynamic_optimization.jl +++ b/test/extensions/dynamic_optimization.jl @@ -47,7 +47,7 @@ const M = ModelingToolkit @test ≈(csol2.sol.u, osol2.u, rtol = 0.001) pprob = PyomoDynamicOptProblem(sys, u0map, tspan, parammap, dt = 0.01) psol = solve(pprob, PyomoCollocation("ipopt", BackwardEuler())) - @test all([≈(psol.sol(t), osol2(t), rtol = 1e-3) for t in 0.:0.01:1.]) + @test all([≈(psol.sol(t), osol2(t), rtol = 1e-2) for t in 0.:0.01:1.]) # With a constraint u0map = Pair[] @@ -111,9 +111,9 @@ function is_bangbang(input_sol, lbounds, ubounds, rtol = 1e-4) end function ctrl_to_spline(inputsol, splineType) - us = reduce(vcat, inputsol.u) - ts = reduce(vcat, inputsol.t) - splineType(us, ts) + us = reduce(vcat, inputsol.u) + ts = reduce(vcat, inputsol.t) + splineType(us, ts) end @testset "Linear systems" begin @@ -163,7 +163,8 @@ end @test is_bangbang(psol.input_sol, [-1.0], [1.0]) @test ≈(psol.sol.u[end][2], 0.25, rtol = 1e-3) - osol = solve(oprob, ImplicitEuler(); dt = 0.01, adaptive = false) + spline = ctrl_to_spline(isol.input_sol, ConstantInterpolation) + oprob = ODEProblem(block_ode, u0map, tspan, [u_interp => spline]) @test ≈(isol.sol.u, osol.u, rtol = 0.05) @test all([≈(psol.sol(t), osol(t), rtol = 0.05) for t in 0.:0.01:1.]) @@ -250,7 +251,7 @@ end @test isol.sol[h(t)][end] > 1.012 pprob = PyomoDynamicOptProblem(rocket, u0map, (ts, te), pmap; dt = 0.001, cse = false) - psol = solve(pprob, PyomoCollocation("ipopt", MidpointEuler())) + psol = solve(pprob, PyomoCollocation("ipopt", LagrangeRadau(4))) @test psol.sol.u[end][1] > 1.012 # Test solution @@ -273,7 +274,7 @@ end interpmap2 = Dict(T_interp => ctrl_to_spline(psol.input_sol, CubicSpline)) oprob2 = ODEProblem(rocket_ode, u0map, (ts, te), merge(Dict(pmap), interpmap2)) osol2 = solve(oprob2, RadauIIA5(); adaptive = false, dt = 0.001) - @test ≈(psol.sol.u, osol2.u, rtol = 0.01) + @test all([≈(psol.sol(t), osol2(t), rtol = 0.01) for t in 0:0.001:0.2]) end @testset "Free final time problems" begin @@ -294,18 +295,22 @@ end jprob = JuMPDynamicOptProblem(rocket, u0map, (0, tf), pmap; steps = 201) jsol = solve(jprob, JuMPCollocation(Ipopt.Optimizer, constructTsitouras5())) @test isapprox(jsol.sol.t[end], 10.0, rtol = 1e-3) + @test ≈(M.objective_value(jsol), -92.75, atol = 0.25) cprob = CasADiDynamicOptProblem(rocket, u0map, (0, tf), pmap; steps = 201) csol = solve(cprob, CasADiCollocation("ipopt", constructTsitouras5())) @test isapprox(csol.sol.t[end], 10.0, rtol = 1e-3) + @test ≈(M.objective_value(csol), -92.75, atol = 0.25) iprob = InfiniteOptDynamicOptProblem(rocket, u0map, (0, tf), pmap; steps = 200) isol = solve(iprob, InfiniteOptCollocation(Ipopt.Optimizer)) @test isapprox(isol.sol.t[end], 10.0, rtol = 1e-3) + @test ≈(M.objective_value(isol), -92.75, atol = 0.25) pprob = PyomoDynamicOptProblem(rocket, u0map, (0, tf), pmap; steps = 201) - psol = solve(pprob, PyomoCollocation("ipopt")) + psol = solve(pprob, PyomoCollocation("ipopt", BackwardEuler())) @test isapprox(psol.sol.t[end], 10.0, rtol = 1e-3) + @test ≈(M.objective_value(psol), -92.75, atol = 0.1) @variables x(..) v(..) @variables u(..) [bounds = (-1.0, 1.0), input = true] @@ -375,7 +380,7 @@ end @test csol.sol.u[end] ≈ [π, 0, 0, 0] iprob = InfiniteOptDynamicOptProblem(cartpole, u0map, tspan, pmap; dt = 0.04) - isol = solve(iprob, InfiniteOptCollocation(Ipopt.Optimizer)) + isol = solve(iprob, InfiniteOptCollocation(Ipopt.Optimizer, InfiniteOpt.OrthogonalCollocation(2))) @test isol.sol.u[end] ≈ [π, 0, 0, 0] pprob = PyomoDynamicOptProblem(cartpole, u0map, tspan, pmap; dt = 0.04) From f7d246f5e082f20329f162fcfe0c15d9d85af97e Mon Sep 17 00:00:00 2001 From: vyudu Date: Thu, 29 May 2025 03:36:31 -0400 Subject: [PATCH 1984/2176] docs: make examples dynamic --- docs/src/tutorials/dynamic_optimization.md | 27 ++++++++++++++++------ 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/docs/src/tutorials/dynamic_optimization.md b/docs/src/tutorials/dynamic_optimization.md index a1a1a44384..8cad7746c4 100644 --- a/docs/src/tutorials/dynamic_optimization.md +++ b/docs/src/tutorials/dynamic_optimization.md @@ -2,7 +2,8 @@ Systems in ModelingToolkit.jl can be directly converted to dynamic optimization or optimal control problems. In such systems, one has one or more input variables that are externally controlled to control the dynamics of the system. A dynamic optimization solves for the optimal time trajectory of the input variables in order to maximize or minimize a desired objective function. For example, a car driver might like to know how to step on the accelerator if the goal is to finish a race while using the least gas. To begin, let us take a rocket launch example. The input variable here is the thrust exerted by the engine. The rocket state is described by its current height and velocity. -```julia + +```@example dynamic_opt using ModelingToolkit t = ModelingToolkit.t_nounits D = ModelingToolkit.D_nounits @@ -37,19 +38,25 @@ pmap = [ What we would like to optimize here is the final height of the rocket. We do this by providing a vector of expressions corresponding to the costs. By default, the sense of the optimization is to minimize the provided cost. So to maximize the rocket height at the final time, we write `-h(te)` as the cost. Now we can construct a problem and solve it. Let us use JuMP as our backend here. -```julia +```@example dynamic_opt +using InfiniteOpt jprob = JuMPDynamicOptProblem(rocket, u0map, (ts, te), pmap; dt = 0.001, cse = false) jsol = solve(jprob, JuMPCollocation(Ipopt.Optimizer, constructRadauIIA5())) ``` Let's plot our final solution and the controller here: -```julia +```@example dynamic_opt +using CairoMakie +plot( + plot(jsol.sol, title = "Rocket trajectory"), + plot(jsol.input_sol, title = "Control trajectory") +) ``` ###### Free final time problems There are additionally a class of dynamic optimization problems where we would like to know how to control our system to achieve something in the least time. Such problems are called free final time problems, since the final time is unknown. To model these problems in ModelingToolkit, we declare the final time as a parameter. -```julia +```@example dynamic_opt @variables x(..) v(..) @variables u(..) [bounds = (-1.0, 1.0), input = true] @parameters tf @@ -69,8 +76,14 @@ parammap = [u(t) => 0.0, tf => 1.0] Please note that, at the moment, free final time problems cannot support constraints defined at definite time values, like `x(3) ~ 2`. -Let's plot our final solution and the controller for the block: -```julia +!!! warning + + The Pyomo collocation methods (LagrangeRadau, LagrangeLegendre) currently are bugged for free final time problems. Strongly suggest using BackwardEuler() for such problems. + +Let's solve plot our final solution and the controller for the block, using InfiniteOpt as the backend: +```@example dynamic_opt +iprob = InfiniteOptDynamicOptProblem(rocket, u0map, (ts, te), pmap; dt = 0.001, cse = false) +isol = solve(iprob, InfiniteOptCollocation(Ipopt.Optimizer)) ``` ### Solvers @@ -86,7 +99,7 @@ CasADiCollocation("ipopt", constructRK4()) **Pyomo** and **InfiniteOpt** each have their own built-in collocation methods. 1. **InfiniteOpt**: The list of InfiniteOpt collocation methods can be found [in the table on this page](https://infiniteopt.github.io/InfiniteOpt.jl/stable/guide/derivative/). If none is passed in, the solver defaults to `FiniteDifference(Backward())`, which is effectively implicit Euler. -2. **Pyomo**: The list of Pyomo collocation methods can be found [here](). If none is passed in, the solver defaults to a `LagrangeRadau(3)`. +2. **Pyomo**: The list of Pyomo collocation methods can be found [at the bottom of this page](https://github.com/SciML/Pyomo.jl). If none is passed in, the solver defaults to a `LagrangeRadau(3)`. ```@docs; canonical = false JuMPCollocation From 10719ae0465300490a7f2ee2a13f146853e2f4e3 Mon Sep 17 00:00:00 2001 From: vyudu Date: Thu, 29 May 2025 12:20:57 -0400 Subject: [PATCH 1985/2176] fix: update ext/tests to v10 --- ext/MTKCasADiDynamicOptExt.jl | 5 +- ext/MTKInfiniteOptExt.jl | 9 +-- ext/MTKPyomoDynamicOptExt.jl | 4 +- src/systems/optimal_control_interface.jl | 68 +++++++++++---------- test/extensions/dynamic_optimization.jl | 77 ++++++++++++------------ 5 files changed, 84 insertions(+), 79 deletions(-) diff --git a/ext/MTKCasADiDynamicOptExt.jl b/ext/MTKCasADiDynamicOptExt.jl index 2b4494863c..73e4a77ed4 100644 --- a/ext/MTKCasADiDynamicOptExt.jl +++ b/ext/MTKCasADiDynamicOptExt.jl @@ -61,11 +61,12 @@ function (M::MXLinearInterpolation)(τ) end end -function MTK.CasADiDynamicOptProblem(sys::ODESystem, u0map, tspan, pmap; +function MTK.CasADiDynamicOptProblem(sys::System, op, tspan; dt = nothing, steps = nothing, guesses = Dict(), kwargs...) - MTK.process_DynamicOptProblem(CasADiDynamicOptProblem, CasADiModel, sys, u0map, tspan, pmap; dt, steps, guesses, kwargs...) + prob, _ = MTK.process_DynamicOptProblem(CasADiDynamicOptProblem, CasADiModel, sys, op, tspan; dt, steps, guesses, kwargs...) + prob end MTK.generate_internal_model(::Type{CasADiModel}) = CasADi.Opti() diff --git a/ext/MTKInfiniteOptExt.jl b/ext/MTKInfiniteOptExt.jl index 31183e3761..8150cd06f7 100644 --- a/ext/MTKInfiniteOptExt.jl +++ b/ext/MTKInfiniteOptExt.jl @@ -72,18 +72,19 @@ function MTK.add_constraint!(m::InfiniteOptModel, expr::Union{Equation, Inequali end MTK.set_objective!(m::InfiniteOptModel, expr) = @objective(m.model, Min, expr) -function MTK.JuMPDynamicOptProblem(sys::ODESystem, u0map, tspan, pmap; +function MTK.JuMPDynamicOptProblem(sys::System, op, tspan; dt = nothing, steps = nothing, guesses = Dict(), kwargs...) - MTK.process_DynamicOptProblem(JuMPDynamicOptProblem, InfiniteOptModel, sys, u0map, tspan, pmap; dt, steps, guesses, kwargs...) + prob, _ = MTK.process_DynamicOptProblem(JuMPDynamicOptProblem, InfiniteOptModel, sys, op, tspan; dt, steps, guesses, kwargs...) + prob end -function MTK.InfiniteOptDynamicOptProblem(sys::ODESystem, u0map, tspan, pmap; +function MTK.InfiniteOptDynamicOptProblem(sys::System, op, tspan; dt = nothing, steps = nothing, guesses = Dict(), kwargs...) - prob = MTK.process_DynamicOptProblem(InfiniteOptDynamicOptProblem, InfiniteOptModel, sys, u0map, tspan, pmap; dt, steps, guesses, kwargs...) + prob, pmap = MTK.process_DynamicOptProblem(InfiniteOptDynamicOptProblem, InfiniteOptModel, sys, op, tspan; dt, steps, guesses, kwargs...) MTK.add_equational_constraints!(prob.wrapped_model, sys, pmap, tspan) prob end diff --git a/ext/MTKPyomoDynamicOptExt.jl b/ext/MTKPyomoDynamicOptExt.jl index 8b668001f0..2301a86b68 100644 --- a/ext/MTKPyomoDynamicOptExt.jl +++ b/ext/MTKPyomoDynamicOptExt.jl @@ -66,10 +66,10 @@ end pysym_getproperty(s::Union{Num, Symbolics.Symbolic}, name::Symbol) = Symbolics.wrap(SymbolicUtils.term(_getproperty, Symbolics.unwrap(s), Val{name}(), type = Symbolics.Struct{PyomoVar})) _getproperty(s, name::Val{fieldname}) where fieldname = getproperty(s, fieldname) -function MTK.PyomoDynamicOptProblem(sys::ODESystem, u0map, tspan, pmap; +function MTK.PyomoDynamicOptProblem(sys::System, op, tspan; dt = nothing, steps = nothing, guesses = Dict(), kwargs...) - prob = MTK.process_DynamicOptProblem(PyomoDynamicOptProblem, PyomoDynamicOptModel, sys, u0map, tspan, pmap; dt, steps, guesses, kwargs...) + prob, pmap = MTK.process_DynamicOptProblem(PyomoDynamicOptProblem, PyomoDynamicOptModel, sys, op, tspan; dt, steps, guesses, kwargs...) conc_model = prob.wrapped_model.model MTK.add_equational_constraints!(prob.wrapped_model, sys, pmap, tspan) prob diff --git a/src/systems/optimal_control_interface.jl b/src/systems/optimal_control_interface.jl index d0668a98ef..ea19afea86 100644 --- a/src/systems/optimal_control_interface.jl +++ b/src/systems/optimal_control_interface.jl @@ -19,9 +19,9 @@ function Base.show(io::IO, sol::DynamicOptSolution) end """ - JuMPDynamicOptProblem(sys::ODESystem, u0, tspan, p; dt, steps, guesses, kwargs...) + JuMPDynamicOptProblem(sys::System, u0, tspan, p; dt, steps, guesses, kwargs...) -Convert an ODESystem representing an optimal control system into a JuMP model +Convert an System representing an optimal control system into a JuMP model for solving using optimization. Must provide either `dt`, the timestep between collocation points (which, along with the timespan, determines the number of points), or directly provide the number of points as `steps`. @@ -30,9 +30,9 @@ To construct the problem, please load InfiniteOpt along with ModelingToolkit. """ function JuMPDynamicOptProblem end """ - InfiniteOptDynamicOptProblem(sys::ODESystem, u0map, tspan, pmap; dt) + InfiniteOptDynamicOptProblem(sys::System, op, tspan; dt) -Convert an ODESystem representing an optimal control system into a InfiniteOpt model +Convert an System representing an optimal control system into a InfiniteOpt model for solving using optimization. Must provide `dt` for determining the length of the interpolation arrays. @@ -43,9 +43,9 @@ To construct the problem, please load InfiniteOpt along with ModelingToolkit. """ function InfiniteOptDynamicOptProblem end """ - CasADiDynamicOptProblem(sys::ODESystem, u0, tspan, p; dt, steps, guesses, kwargs...) + CasADiDynamicOptProblem(sys::System, u0, tspan, p; dt, steps, guesses, kwargs...) -Convert an ODESystem representing an optimal control system into a CasADi model +Convert an System representing an optimal control system into a CasADi model for solving using optimization. Must provide either `dt`, the timestep between collocation points (which, along with the timespan, determines the number of points), or directly provide the number of points as `steps`. @@ -54,9 +54,9 @@ To construct the problem, please load CasADi along with ModelingToolkit. """ function CasADiDynamicOptProblem end """ - PyomoDynamicOptProblem(sys::ODESystem, u0, tspan, p; dt, steps) + PyomoDynamicOptProblem(sys::System, u0, tspan, p; dt, steps) -Convert an ODESystem representing an optimal control system into a Pyomo model +Convert an System representing an optimal control system into a Pyomo model for solving using optimization. Must provide either `dt`, the timestep between collocation points (which, along with the timespan, determines the number of points), or directly provide the number of points as `steps`. @@ -91,11 +91,12 @@ Pyomo Collocation solver. """ function PyomoCollocation end -function warn_overdetermined(sys, u0map) +function warn_overdetermined(sys, op) cstrs = constraints(sys) + init_conds = filter(x -> value(x) ∈ Set(unknowns(sys)), [k for (k,v) in op]) if !isempty(cstrs) - (length(cstrs) + length(u0map) > length(unknowns(sys))) && - @warn "The control problem is overdetermined. The total number of conditions (# constraints + # fixed initial values given by u0map) exceeds the total number of states. The solvers will default to doing a nonlinear least-squares optimization." + (length(cstrs) + length(init_conds) > length(unknowns(sys))) && + @warn "The control problem is overdetermined. The total number of conditions (# constraints + # fixed initial values given by op) exceeds the total number of states. The solvers will default to doing a nonlinear least-squares optimization." end end @@ -226,24 +227,27 @@ end ########################## ### MODEL CONSTRUCTION ### ########################## -function process_DynamicOptProblem(prob_type::Type{<:AbstractDynamicOptProblem}, model_type, sys::ODESystem, u0map, tspan, pmap; +function process_DynamicOptProblem(prob_type::Type{<:AbstractDynamicOptProblem}, model_type, sys::System, op, tspan; dt = nothing, steps = nothing, guesses = Dict(), kwargs...) - warn_overdetermined(sys, u0map) + + warn_overdetermined(sys, op) ctrls = unbound_inputs(sys) states = unknowns(sys) - _u0map = has_alg_eqs(sys) ? u0map : merge(Dict(u0map), Dict(guesses)) stidxmap = Dict([v => i for (i, v) in enumerate(states)]) - u0map = Dict([default_toterm(value(k)) => v for (k, v) in u0map]) + op = Dict([default_toterm(value(k)) => v for (k, v) in op]) u0_idxs = has_alg_eqs(sys) ? collect(1:length(states)) : - [stidxmap[default_toterm(k)] for (k, v) in u0map] + [stidxmap[default_toterm(k)] for (k, v) in op if haskey(stidxmap, k)] - f, u0, p = process_SciMLProblem(ODEInputFunction, sys, _u0map, pmap; + _op = has_alg_eqs(sys) ? op : merge(Dict(op), Dict(guesses)) + f, u0, p = process_SciMLProblem(ODEInputFunction, sys, _op; t = tspan !== nothing ? tspan[1] : tspan, kwargs...) model_tspan, steps, is_free_t = process_tspan(tspan, dt, steps) + warn_overdetermined(sys, op) + pmap = filter(p -> (first(p) ∉ Set(unknowns(sys))), op) pmap = recursive_unwrap(AnyDict(pmap)) evaluate_varmap!(pmap, keys(pmap)) c0 = value.([pmap[c] for c in ctrls]) @@ -261,7 +265,7 @@ function process_DynamicOptProblem(prob_type::Type{<:AbstractDynamicOptProblem}, add_user_constraints!(fullmodel, sys, tspan, pmap) add_initial_constraints!(fullmodel, u0, u0_idxs, model_tspan[1]) - prob_type(f, u0, tspan, p, fullmodel, kwargs...) + prob_type(f, u0, tspan, p, fullmodel, kwargs...), pmap end function generate_time_variable! end @@ -301,16 +305,16 @@ end is_free_final(model) = model.is_free_final function add_cost_function!(model, sys, tspan, pmap) - jcosts = copy(get_costs(sys)) - consolidate = get_consolidate(sys) - if isnothing(jcosts) || isempty(jcosts) + jcosts = cost(sys) + if Symbolics._iszero(jcosts) set_objective!(model, 0) return end - jcosts = substitute_model_vars(model, sys, jcosts, tspan) + + jcosts = substitute_model_vars(model, sys, [jcosts], tspan) jcosts = substitute_params(pmap, jcosts) - jcosts = substitute_integral(model, jcosts, tspan) - set_objective!(model, consolidate(jcosts)) + jcosts = substitute_integral(model, only(jcosts), tspan) + set_objective!(model, value(jcosts)) end """ @@ -319,16 +323,16 @@ Substitute integrals. For an integral from (ts, te): - CasADi cannot handle partial timespans, even for non-free-final time problems. time problems and unchanged otherwise. """ -function substitute_integral(model, exprs, tspan) +function substitute_integral(model, expr, tspan) intmap = Dict() - for int in collect_applied_operators(exprs, Symbolics.Integral) + for int in collect_applied_operators(expr, Symbolics.Integral) op = operation(int) arg = only(arguments(value(int))) lo, hi = value.((op.domain.domain.left, op.domain.domain.right)) lo, hi = process_integral_bounds(model, (lo, hi), tspan) intmap[int] = lowered_integral(model, arg, lo, hi) end - exprs = map(c -> Symbolics.substitute(c, intmap), value.(exprs)) + Symbolics.substitute(expr, intmap) end function process_integral_bounds(model, integral_span, tspan) @@ -386,13 +390,13 @@ function lowered_var end function fixed_t_map end function add_user_constraints!(model, sys, tspan, pmap) - conssys = get_constraintsystem(sys) - jconstraints = isnothing(conssys) ? nothing : get_constraints(conssys) + jconstraints = get_constraints(sys) (isnothing(jconstraints) || isempty(jconstraints)) && return nothing - consvars = get_unknowns(conssys) - is_free_final(model) && check_constraint_vars(consvars) + cons_dvs, cons_ps = process_constraint_system(jconstraints, Set(unknowns(sys)), parameters(sys), get_iv(sys); validate = false) + + is_free_final(model) && check_constraint_vars(cons_dvs) - jconstraints = substitute_toterm(consvars, jconstraints) + jconstraints = substitute_toterm(cons_dvs, jconstraints) jconstraints = substitute_model_vars(model, sys, jconstraints, tspan) jconstraints = substitute_params(pmap, jconstraints) diff --git a/test/extensions/dynamic_optimization.jl b/test/extensions/dynamic_optimization.jl index feb8ddfba8..0243b298d6 100644 --- a/test/extensions/dynamic_optimization.jl +++ b/test/extensions/dynamic_optimization.jl @@ -27,11 +27,11 @@ const M = ModelingToolkit parammap = [α => 1.5, β => 1.0, γ => 3.0, δ => 1.0] # Test explicit method. - jprob = JuMPDynamicOptProblem(sys, u0map, tspan, parammap, dt = 0.01) + jprob = JuMPDynamicOptProblem(sys, [u0map; parammap], tspan, dt = 0.01) jsol = solve(jprob, JuMPCollocation(Ipopt.Optimizer, constructRK4())) oprob = ODEProblem(sys, [u0map; parammap], tspan) osol = solve(oprob, SimpleRK4(), dt = 0.01) - cprob = CasADiDynamicOptProblem(sys, u0map, tspan, parammap, dt = 0.01) + cprob = CasADiDynamicOptProblem(sys, [u0map; parammap], tspan, dt = 0.01) csol = solve(cprob, CasADiCollocation("ipopt", constructRK4())) @test jsol.sol.u ≈ osol.u @test csol.sol.u ≈ osol.u @@ -40,12 +40,12 @@ const M = ModelingToolkit osol2 = solve(oprob, ImplicitEuler(), dt = 0.01, adaptive = false) jsol2 = solve(jprob, JuMPCollocation(Ipopt.Optimizer, constructImplicitEuler())) @test ≈(jsol2.sol.u, osol2.u, rtol = 0.001) - iprob = InfiniteOptDynamicOptProblem(sys, u0map, tspan, parammap, dt = 0.01) + iprob = InfiniteOptDynamicOptProblem(sys, [u0map; parammap], tspan, dt = 0.01) isol = solve(iprob, InfiniteOptCollocation(Ipopt.Optimizer)) @test ≈(isol.sol.u, osol2.u, rtol = 0.001) csol2 = solve(cprob, CasADiCollocation("ipopt", constructImplicitEuler())) @test ≈(csol2.sol.u, osol2.u, rtol = 0.001) - pprob = PyomoDynamicOptProblem(sys, u0map, tspan, parammap, dt = 0.01) + pprob = PyomoDynamicOptProblem(sys, [u0map; parammap], tspan, dt = 0.01) psol = solve(pprob, PyomoCollocation("ipopt", BackwardEuler())) @test all([≈(psol.sol(t), osol2(t), rtol = 1e-2) for t in 0.:0.01:1.]) @@ -55,26 +55,26 @@ const M = ModelingToolkit constr = [x(0.6) ~ 3.5, x(0.3) ~ 7.0] @mtkcompile lksys = System(eqs, t; constraints = constr) - jprob = JuMPDynamicOptProblem(lksys, u0map, tspan, parammap; guesses = guess, dt = 0.01) + jprob = JuMPDynamicOptProblem(lksys, [u0map; parammap], tspan; guesses = guess, dt = 0.01) jsol = solve(jprob, JuMPCollocation(Ipopt.Optimizer, constructTsitouras5())) @test jsol.sol(0.6; idxs = x(t)) ≈ 3.5 @test jsol.sol(0.3; idxs = x(t)) ≈ 7.0 cprob = CasADiDynamicOptProblem( - lksys, u0map, tspan, parammap; guesses = guess, dt = 0.01) + lksys, [u0map; parammap], tspan; guesses = guess, dt = 0.01) csol = solve(cprob, CasADiCollocation("ipopt", constructTsitouras5())) @test csol.sol(0.6; idxs = x(t)) ≈ 3.5 @test csol.sol(0.3; idxs = x(t)) ≈ 7.0 pprob = PyomoDynamicOptProblem( - lksys, u0map, tspan, parammap; guesses = guess, dt = 0.01) + lksys, [u0map; parammap], tspan; guesses = guess, dt = 0.01) psol = solve(pprob, PyomoCollocation("ipopt", LagrangeLegendre(3))) @show psol.sol @test psol.sol(0.6)[1] ≈ 3.5 @test psol.sol(0.3)[1] ≈ 7.0 iprob = InfiniteOptDynamicOptProblem( - lksys, u0map, tspan, parammap; guesses = guess, dt = 0.01) + lksys, [u0map; parammap], tspan; guesses = guess, dt = 0.01) isol = solve(iprob, InfiniteOptCollocation(Ipopt.Optimizer, InfiniteOpt.OrthogonalCollocation(3))) # 48.564 ms, 9.58 MiB sol = isol.sol @test sol(0.6; idxs = x(t)) ≈ 3.5 @@ -84,20 +84,20 @@ const M = ModelingToolkit constr = [x(t) ≳ 1, y(t) ≳ 1] @mtkcompile lksys = System(eqs, t; constraints = constr) iprob = InfiniteOptDynamicOptProblem( - lksys, u0map, tspan, parammap; guesses = guess, dt = 0.01) + lksys, [u0map; parammap], tspan; guesses = guess, dt = 0.01) isol = solve(iprob, InfiniteOptCollocation(Ipopt.Optimizer, InfiniteOpt.OrthogonalCollocation(3))) @test all(u -> u > [1, 1], isol.sol.u) - jprob = JuMPDynamicOptProblem(lksys, u0map, tspan, parammap; guesses = guess, dt = 0.01) + jprob = JuMPDynamicOptProblem(lksys, [u0map; parammap], tspan; guesses = guess, dt = 0.01) jsol = solve(jprob, JuMPCollocation(Ipopt.Optimizer, constructRadauIA3())) @test all(u -> u > [1, 1], jsol.sol.u) - pprob = PyomoDynamicOptProblem(lksys, u0map, tspan, parammap; guesses = guess, dt = 0.01) + pprob = PyomoDynamicOptProblem(lksys, [u0map; parammap], tspan; guesses = guess, dt = 0.01) psol = solve(pprob, PyomoCollocation("ipopt", MidpointEuler())) @test all(u -> u > [1, 1], psol.sol.u) cprob = CasADiDynamicOptProblem( - lksys, u0map, tspan, parammap; guesses = guess, dt = 0.01) + lksys, [u0map; parammap], tspan; guesses = guess, dt = 0.01) csol = solve(cprob, CasADiCollocation("ipopt", constructRadauIA3())) @test all(u -> u > [1, 1], csol.sol.u) end @@ -131,14 +131,14 @@ end u0map = [x(t) => 0.0, v(t) => 0.0] tspan = (0.0, 1.0) parammap = [u(t) => 0.0] - jprob = JuMPDynamicOptProblem(block, u0map, tspan, parammap; dt = 0.01) + jprob = JuMPDynamicOptProblem(block, [u0map; parammap], tspan; dt = 0.01) jsol = solve(jprob, JuMPCollocation(Ipopt.Optimizer, constructVerner8()), verbose = true) # Linear systems have bang-bang controls @test is_bangbang(jsol.input_sol, [-1.0], [1.0]) # Test reached final position. @test ≈(jsol.sol[x(t)][end], 0.25, rtol = 1e-5) - cprob = CasADiDynamicOptProblem(block, u0map, tspan, parammap; dt = 0.01) + cprob = CasADiDynamicOptProblem(block, [u0map; parammap], tspan; dt = 0.01) csol = solve(cprob, CasADiCollocation("ipopt", constructVerner8())) @test is_bangbang(csol.input_sol, [-1.0], [1.0]) # Test reached final position. @@ -153,18 +153,18 @@ end @test ≈(jsol.sol.u, osol.u, rtol = 0.05) @test ≈(csol.sol.u, osol.u, rtol = 0.05) - iprob = InfiniteOptDynamicOptProblem(block, u0map, tspan, parammap; dt = 0.01) + iprob = InfiniteOptDynamicOptProblem(block, [u0map; parammap], tspan; dt = 0.01) isol = solve(iprob, InfiniteOptCollocation(Ipopt.Optimizer)) @test is_bangbang(isol.input_sol, [-1.0], [1.0]) @test ≈(isol.sol[x(t)][end], 0.25, rtol = 1e-5) - pprob = PyomoDynamicOptProblem(block, u0map, tspan, parammap; dt = 0.01) + pprob = PyomoDynamicOptProblem(block, [u0map; parammap], tspan; dt = 0.01) psol = solve(pprob, PyomoCollocation("ipopt", BackwardEuler())) @test is_bangbang(psol.input_sol, [-1.0], [1.0]) @test ≈(psol.sol.u[end][2], 0.25, rtol = 1e-3) spline = ctrl_to_spline(isol.input_sol, ConstantInterpolation) - oprob = ODEProblem(block_ode, u0map, tspan, [u_interp => spline]) + oprob = ODEProblem(block_ode, [u0map; u_interp => spline], tspan) @test ≈(isol.sol.u, osol.u, rtol = 0.05) @test all([≈(psol.sol(t), osol(t), rtol = 0.05) for t in 0.:0.01:1.]) @@ -185,16 +185,16 @@ end u0map = [w(t) => 40, q(t) => 2] pmap = [b => 1, c => 1, μ => 1, s => 1, ν => 1, α => 1] - jprob = JuMPDynamicOptProblem(beesys, u0map, tspan, pmap, dt = 0.01) + jprob = JuMPDynamicOptProblem(beesys, [u0map; pmap], tspan, dt = 0.01) jsol = solve(jprob, JuMPCollocation(Ipopt.Optimizer, constructTsitouras5())) @test is_bangbang(jsol.input_sol, [0.0], [1.0]) - iprob = InfiniteOptDynamicOptProblem(beesys, u0map, tspan, pmap, dt = 0.01) + iprob = InfiniteOptDynamicOptProblem(beesys, [u0map; pmap], tspan, dt = 0.01) isol = solve(iprob, InfiniteOptCollocation(Ipopt.Optimizer)) @test is_bangbang(isol.input_sol, [0.0], [1.0]) - cprob = CasADiDynamicOptProblem(beesys, u0map, tspan, pmap; dt = 0.01) + cprob = CasADiDynamicOptProblem(beesys, [u0map; pmap], tspan; dt = 0.01) csol = solve(cprob, CasADiCollocation("ipopt", constructTsitouras5())) @test is_bangbang(csol.input_sol, [0.0], [1.0]) - pprob = PyomoDynamicOptProblem(beesys, u0map, tspan, pmap, dt = 0.01) + pprob = PyomoDynamicOptProblem(beesys, [u0map; pmap], tspan, dt = 0.01) psol = solve(pprob, PyomoCollocation("ipopt", BackwardEuler())) @test is_bangbang(psol.input_sol, [0.0], [1.0]) @@ -237,20 +237,19 @@ end pmap = [ g₀ => 1, m₀ => 1.0, h_c => 500, c => 0.5 * √(g₀ * h₀), D_c => 0.5 * 620 * m₀ / g₀, Tₘ => 3.5 * g₀ * m₀, T(t) => 0.0, h₀ => 1, m_c => 0.6] - jprob = JuMPDynamicOptProblem(rocket, u0map, (ts, te), pmap; dt = 0.001, cse = false) + jprob = JuMPDynamicOptProblem(rocket, [u0map; pmap], (ts, te); dt = 0.001, cse = false) jsol = solve(jprob, JuMPCollocation(Ipopt.Optimizer, constructRadauIIA5())) @test jsol.sol[h(t)][end] > 1.012 - cprob = CasADiDynamicOptProblem(rocket, u0map, (ts, te), pmap; dt = 0.001, cse = false) + cprob = CasADiDynamicOptProblem(rocket, [u0map; pmap], (ts, te); dt = 0.001, cse = false) csol = solve(cprob, CasADiCollocation("ipopt")) @test csol.sol[h(t)][end] > 1.012 - iprob = InfiniteOptDynamicOptProblem( - rocket, u0map, (ts, te), pmap; dt = 0.001) + iprob = InfiniteOptDynamicOptProblem(rocket, [u0map; pmap], (ts, te); dt = 0.001) isol = solve(iprob, InfiniteOptCollocation(Ipopt.Optimizer)) @test isol.sol[h(t)][end] > 1.012 - pprob = PyomoDynamicOptProblem(rocket, u0map, (ts, te), pmap; dt = 0.001, cse = false) + pprob = PyomoDynamicOptProblem(rocket, [u0map; pmap], (ts, te); dt = 0.001, cse = false) psol = solve(pprob, PyomoCollocation("ipopt", LagrangeRadau(4))) @test psol.sol.u[end][1] > 1.012 @@ -272,7 +271,7 @@ end @test ≈(isol.sol.u, osol1.u, rtol = 0.01) interpmap2 = Dict(T_interp => ctrl_to_spline(psol.input_sol, CubicSpline)) - oprob2 = ODEProblem(rocket_ode, u0map, (ts, te), merge(Dict(pmap), interpmap2)) + oprob2 = ODEProblem(rocket_ode, merge(Dict(u0map), Dict(pmap), interpmap2), (ts, te)) osol2 = solve(oprob2, RadauIIA5(); adaptive = false, dt = 0.001) @test all([≈(psol.sol(t), osol2(t), rtol = 0.01) for t in 0:0.001:0.2]) end @@ -292,22 +291,22 @@ end u0map = [x(t) => 17.5] pmap = [u(t) => 0.0, tf => 8] - jprob = JuMPDynamicOptProblem(rocket, u0map, (0, tf), pmap; steps = 201) + jprob = JuMPDynamicOptProblem(rocket, [u0map; pmap], (0, tf); steps = 201) jsol = solve(jprob, JuMPCollocation(Ipopt.Optimizer, constructTsitouras5())) @test isapprox(jsol.sol.t[end], 10.0, rtol = 1e-3) @test ≈(M.objective_value(jsol), -92.75, atol = 0.25) - cprob = CasADiDynamicOptProblem(rocket, u0map, (0, tf), pmap; steps = 201) + cprob = CasADiDynamicOptProblem(rocket, [u0map; pmap], (0, tf); steps = 201) csol = solve(cprob, CasADiCollocation("ipopt", constructTsitouras5())) @test isapprox(csol.sol.t[end], 10.0, rtol = 1e-3) @test ≈(M.objective_value(csol), -92.75, atol = 0.25) - iprob = InfiniteOptDynamicOptProblem(rocket, u0map, (0, tf), pmap; steps = 200) + iprob = InfiniteOptDynamicOptProblem(rocket, [u0map; pmap], (0, tf); steps = 200) isol = solve(iprob, InfiniteOptCollocation(Ipopt.Optimizer)) @test isapprox(isol.sol.t[end], 10.0, rtol = 1e-3) @test ≈(M.objective_value(isol), -92.75, atol = 0.25) - pprob = PyomoDynamicOptProblem(rocket, u0map, (0, tf), pmap; steps = 201) + pprob = PyomoDynamicOptProblem(rocket, [u0map; pmap], (0, tf); steps = 201) psol = solve(pprob, PyomoCollocation("ipopt", BackwardEuler())) @test isapprox(psol.sol.t[end], 10.0, rtol = 1e-3) @test ≈(M.objective_value(psol), -92.75, atol = 0.1) @@ -325,19 +324,19 @@ end u0map = [x(t) => 1.0, v(t) => 0.0] tspan = (0.0, tf) parammap = [u(t) => 1.0, tf => 1.0] - jprob = JuMPDynamicOptProblem(block, u0map, tspan, parammap; steps = 51) + jprob = JuMPDynamicOptProblem(block, [u0map; parammap], tspan; steps = 51) jsol = solve(jprob, JuMPCollocation(Ipopt.Optimizer, constructVerner8())) @test isapprox(jsol.sol.t[end], 2.0, atol = 1e-5) - cprob = CasADiDynamicOptProblem(block, u0map, (0, tf), parammap; steps = 51) + cprob = CasADiDynamicOptProblem(block, [u0map; parammap], (0, tf); steps = 51) csol = solve(cprob, CasADiCollocation("ipopt", constructVerner8())) @test isapprox(csol.sol.t[end], 2.0, atol = 1e-5) - iprob = InfiniteOptDynamicOptProblem(block, u0map, tspan, parammap; steps = 51) + iprob = InfiniteOptDynamicOptProblem(block, [u0map; parammap], tspan; steps = 51) isol = solve(iprob, InfiniteOptCollocation(Ipopt.Optimizer), verbose = true) @test isapprox(isol.sol.t[end], 2.0, atol = 1e-5) - pprob = PyomoDynamicOptProblem(block, u0map, tspan, parammap; steps = 51) + pprob = PyomoDynamicOptProblem(block, [u0map; parammap], tspan; steps = 51) psol = solve(pprob, PyomoCollocation("ipopt", BackwardEuler())) @test isapprox(psol.sol.t[end], 2.0, atol = 1e-5) end @@ -371,19 +370,19 @@ end u0map = [D(x(t)) => 0.0, D(θ(t)) => 0.0, θ(t) => 0.0, x(t) => 0.0] pmap = [mₖ => 1.0, mₚ => 0.2, l => 0.5, g => 9.81, u => 0] - jprob = JuMPDynamicOptProblem(cartpole, u0map, tspan, pmap; dt = 0.04) + jprob = JuMPDynamicOptProblem(cartpole, [u0map; pmap], tspan; dt = 0.04) jsol = solve(jprob, JuMPCollocation(Ipopt.Optimizer, constructRK4())) @test jsol.sol.u[end] ≈ [π, 0, 0, 0] - cprob = CasADiDynamicOptProblem(cartpole, u0map, tspan, pmap; dt = 0.04) + cprob = CasADiDynamicOptProblem(cartpole, [u0map; pmap], tspan; dt = 0.04) csol = solve(cprob, CasADiCollocation("ipopt", constructRK4())) @test csol.sol.u[end] ≈ [π, 0, 0, 0] - iprob = InfiniteOptDynamicOptProblem(cartpole, u0map, tspan, pmap; dt = 0.04) + iprob = InfiniteOptDynamicOptProblem(cartpole, [u0map; pmap], tspan; dt = 0.04) isol = solve(iprob, InfiniteOptCollocation(Ipopt.Optimizer, InfiniteOpt.OrthogonalCollocation(2))) @test isol.sol.u[end] ≈ [π, 0, 0, 0] - pprob = PyomoDynamicOptProblem(cartpole, u0map, tspan, pmap; dt = 0.04) + pprob = PyomoDynamicOptProblem(cartpole, [u0map; pmap], tspan; dt = 0.04) psol = solve(pprob, PyomoCollocation("ipopt", LagrangeLegendre(4))) @test psol.sol.u[end] ≈ [π, 0, 0, 0] end From 7daecfbf3c88f4179791fbd94b39dcc11e9511b8 Mon Sep 17 00:00:00 2001 From: vyudu Date: Thu, 29 May 2025 18:32:40 -0400 Subject: [PATCH 1986/2176] docs: update docs to v10 --- docs/Project.toml | 3 + docs/src/tutorials/dynamic_optimization.md | 87 +++++++++++++++------- 2 files changed, 65 insertions(+), 25 deletions(-) diff --git a/docs/Project.toml b/docs/Project.toml index ef6af51d79..314a1d7f84 100644 --- a/docs/Project.toml +++ b/docs/Project.toml @@ -5,11 +5,14 @@ BifurcationKit = "0f109fa4-8a5d-4b75-95aa-f515264e7665" CairoMakie = "13f3f980-e62b-5c42-98c6-ff1f3baf88f0" ControlSystemsBase = "aaaaaaaa-a6ca-5380-bf3e-84a91bcd477e" DataInterpolations = "82cc6244-b520-54b8-b5a6-8a565e85f1d0" +DiffEqDevTools = "f3b72e0c-5b89-59e1-b016-84e28bfd966d" Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f" Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4" DynamicQuantities = "06fc5a27-2a28-4c7c-a15d-362465fb6821" FMI = "14a09403-18e3-468f-ad8a-74f8dda2d9ac" FMIZoo = "724179cf-c260-40a9-bd27-cccc6fe2f195" +InfiniteOpt = "20393b10-9daf-11e9-18c9-8db751c92c57" +Ipopt = "b6b21f68-93f8-5de0-b562-5493be1d77c9" LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" ModelingToolkit = "961ee093-0014-501f-94e3-6117800e7a78" ModelingToolkitStandardLibrary = "16a59e39-deab-5bd0-87e4-056b12336739" diff --git a/docs/src/tutorials/dynamic_optimization.md b/docs/src/tutorials/dynamic_optimization.md index 8cad7746c4..7753516795 100644 --- a/docs/src/tutorials/dynamic_optimization.md +++ b/docs/src/tutorials/dynamic_optimization.md @@ -1,7 +1,7 @@ # Solving Dynamic Optimization Problems Systems in ModelingToolkit.jl can be directly converted to dynamic optimization or optimal control problems. In such systems, one has one or more input variables that are externally controlled to control the dynamics of the system. A dynamic optimization solves for the optimal time trajectory of the input variables in order to maximize or minimize a desired objective function. For example, a car driver might like to know how to step on the accelerator if the goal is to finish a race while using the least gas. -To begin, let us take a rocket launch example. The input variable here is the thrust exerted by the engine. The rocket state is described by its current height and velocity. +To begin, let us take a rocket launch example. The input variable here is the thrust exerted by the engine. The rocket state is described by its current height, mass, and velocity. The mass decreases as the rocket loses fuel while thrusting. ```@example dynamic_opt using ModelingToolkit @@ -12,8 +12,8 @@ D = ModelingToolkit.D_nounits @variables begin h(..) v(..) - m(..) [bounds = (m_c, 1)] - T(..) [input = true, bounds = (0, Tₘ)] + m(..), [bounds = (m_c, 1)] + T(..), [input = true, bounds = (0, Tₘ)] end drag(h, v) = D_c * v^2 * exp(-h_c * (h - h₀) / h₀) @@ -27,8 +27,8 @@ eqs = [D(h(t)) ~ v(t), costs = [-h(te)] cons = [T(te) ~ 0, m(te) ~ m_c] -@named rocket = ODESystem(eqs, t; costs, constraints = cons) -rocket, input_idxs = structural_simplify(rocket, ([T(t)], [])) +@named rocket = System(eqs, t; costs, constraints = cons) +rocket = mtkcompile(rocket, inputs = [T(t)]) u0map = [h(t) => h₀, m(t) => m₀, v(t) => 0] pmap = [ @@ -37,70 +37,107 @@ pmap = [ ``` What we would like to optimize here is the final height of the rocket. We do this by providing a vector of expressions corresponding to the costs. By default, the sense of the optimization is to minimize the provided cost. So to maximize the rocket height at the final time, we write `-h(te)` as the cost. -Now we can construct a problem and solve it. Let us use JuMP as our backend here. +Now we can construct a problem and solve it. Let us use JuMP as our backend here. Note that the package trigger is actually [InfiniteOpt](https://infiniteopt.github.io/InfiniteOpt.jl/stable/), and not JuMP - this package includes JuMP but is designed for optimization on function spaces. Additionally we need to load the solver package - we will use [Ipopt](https://github.com/jump-dev/Ipopt.jl) here (a good choice in general). + +Here we have also loaded DiffEqDevTools because we will need to construct the ODE tableau. This is only needed if one desires a custom ODE tableau for the collocation - by default the solver will use RadauIIA5. ```@example dynamic_opt -using InfiniteOpt -jprob = JuMPDynamicOptProblem(rocket, u0map, (ts, te), pmap; dt = 0.001, cse = false) -jsol = solve(jprob, JuMPCollocation(Ipopt.Optimizer, constructRadauIIA5())) +using InfiniteOpt, Ipopt, DiffEqDevTools +jprob = JuMPDynamicOptProblem(rocket, [u0map; pmap], (ts, te); dt = 0.001) +jsol = solve(jprob, JuMPCollocation(Ipopt.Optimizer, constructRadauIIA5())); ``` +The solution has three fields: `jsol.sol` is the ODE solution for the states, `jsol.input_sol` is the ODE solution for the inputs, and `jsol.model` is the wrapped model that we can use to query things like objective and constraint residuals. -Let's plot our final solution and the controller here: +Let's plot the final solution and the controller here: ```@example dynamic_opt using CairoMakie -plot( - plot(jsol.sol, title = "Rocket trajectory"), - plot(jsol.input_sol, title = "Control trajectory") -) +fig = Figure(resolution = (800, 400)) +ax1 = Axis(fig[1,1], title = "Rocket trajectory", xlabel = "Time") +ax2 = Axis(fig[1,2], title = "Control trajectory", xlabel = "Time") + +for u in unknowns(rocket) + lines!(ax1, jsol.sol.t, jsol.sol[u], label = string(u)) +end +lines!(ax2, jsol.input_sol, label = "Thrust") +axislegend(ax1) +axislegend(ax2) +fig ``` -###### Free final time problems +### Free final time problems There are additionally a class of dynamic optimization problems where we would like to know how to control our system to achieve something in the least time. Such problems are called free final time problems, since the final time is unknown. To model these problems in ModelingToolkit, we declare the final time as a parameter. +Below we have a model system called the double integrator. We control the acceleration of a block in order to reach a desired destination in the least time. ```@example dynamic_opt -@variables x(..) v(..) -@variables u(..) [bounds = (-1.0, 1.0), input = true] +@variables begin + x(..) + v(..) + u(..), [bounds = (-1.0, 1.0), input = true] +end + @parameters tf constr = [v(tf) ~ 0, x(tf) ~ 0] cost = [tf] # Minimize time -@named block = ODESystem( +@named block = System( [D(x(t)) ~ v(t), D(v(t)) ~ u(t)], t; costs = cost, constraints = constr) -block, input_idxs = structural_simplify(block, ([u(t)], [])) +block = mtkcompile(block; inputs = [u(t)]) u0map = [x(t) => 1.0, v(t) => 0.0] tspan = (0.0, tf) parammap = [u(t) => 0.0, tf => 1.0] ``` +The `tf` mapping in the parameter map is treated as an initial guess. Please note that, at the moment, free final time problems cannot support constraints defined at definite time values, like `x(3) ~ 2`. !!! warning - The Pyomo collocation methods (LagrangeRadau, LagrangeLegendre) currently are bugged for free final time problems. Strongly suggest using BackwardEuler() for such problems. + The Pyomo collocation methods (LagrangeRadau, LagrangeLegendre) currently are bugged for free final time problems. Strongly suggest using BackwardEuler() for such problems when using Pyomo as the backend. -Let's solve plot our final solution and the controller for the block, using InfiniteOpt as the backend: +When declaring the problem in this case we need to provide the number of steps, since dt can't be known in advanced. Let's solve plot our final solution and the controller for the block, using InfiniteOpt as the backend: ```@example dynamic_opt -iprob = InfiniteOptDynamicOptProblem(rocket, u0map, (ts, te), pmap; dt = 0.001, cse = false) -isol = solve(iprob, InfiniteOptCollocation(Ipopt.Optimizer)) +iprob = InfiniteOptDynamicOptProblem(block, [u0map; parammap], tspan; steps = 100) +isol = solve(iprob, InfiniteOptCollocation(Ipopt.Optimizer)); +``` + +Let's plot the final solution and the controller here: +```@example dynamic_opt +fig = Figure(resolution = (800, 400)) +ax1 = Axis(fig[1,1], title = "Block trajectory", xlabel = "Time") +ax2 = Axis(fig[1,2], title = "Control trajectory", xlabel = "Time") + +for u in unknowns(block) + lines!(ax1, isol.sol.t, isol.sol[u], label = string(u)) +end +lines!(ax2, isol.input_sol, label = "Acceleration") +axislegend(ax1) +axislegend(ax2) +fig ``` ### Solvers Currently 4 backends are exposed for solving dynamic optimization problems using collocation: JuMP, InfiniteOpt, CasADi, and Pyomo. -Please note that there are differences in how to construct the collocation solver for the different cases. For example, the Python based ones, CasADi and Pyomo, expect the solver to be passed in as a string (CasADi and Pyomo come pre-loaded with certain solvers, but other solvers may need to be manually installed using `pip` or `conda`), while JuMP/InfiniteOpt expect the optimizer object to be passed in directly: +Please note that there are differences in how to construct the collocation solver for the different cases. For example, the Python based ones, CasADi and Pyomo, expect the solver to be passed in as a string (CasADi and Pyomo come pre-loaded with Ipopt, but other solvers may need to be manually installed using `pip` or `conda`), while JuMP/InfiniteOpt expect the optimizer object to be passed in directly: ``` JuMPCollocation(Ipopt.Optimizer, constructRK4()) CasADiCollocation("ipopt", constructRK4()) ``` -**JuMP** and **CasADi** collocation require an ODE tableau to be passed in. These can be constructed by calling the `constructX()` functions from DiffEqDevTools. If none is passed in, both solvers will default to using Radau second-order with five collocation points. +**JuMP** and **CasADi** collocation require an ODE tableau to be passed in. These can be constructed by calling the `constructX()` functions from DiffEqDevTools. The list of tableaus can be found [here](https://docs.sciml.ai/DiffEqDevDocs/dev/internals/tableaus/). If none is passed in, both solvers will default to using Radau second-order with five collocation points. **Pyomo** and **InfiniteOpt** each have their own built-in collocation methods. 1. **InfiniteOpt**: The list of InfiniteOpt collocation methods can be found [in the table on this page](https://infiniteopt.github.io/InfiniteOpt.jl/stable/guide/derivative/). If none is passed in, the solver defaults to `FiniteDifference(Backward())`, which is effectively implicit Euler. 2. **Pyomo**: The list of Pyomo collocation methods can be found [at the bottom of this page](https://github.com/SciML/Pyomo.jl). If none is passed in, the solver defaults to a `LagrangeRadau(3)`. +Some examples of the latter two collocations: +```julia +PyomoCollocation("ipopt", LagrangeRadau(2)) +InfiniteOptCollocation(Ipopt.Optimizer, OrthogonalCollocation(3)) +``` + ```@docs; canonical = false JuMPCollocation InfiniteOptCollocation From f33f8c70b429db71eb0ab9ce168f75557ecb72db Mon Sep 17 00:00:00 2001 From: vyudu Date: Mon, 2 Jun 2025 10:02:36 -0400 Subject: [PATCH 1987/2176] fix: error when using collocation on fft problems --- docs/src/API/dynamic_opt.md | 41 ++++++++++++++++++++++++ ext/MTKPyomoDynamicOptExt.jl | 60 +++++++++++++++++++++++------------- 2 files changed, 80 insertions(+), 21 deletions(-) create mode 100644 docs/src/API/dynamic_opt.md diff --git a/docs/src/API/dynamic_opt.md b/docs/src/API/dynamic_opt.md new file mode 100644 index 0000000000..1a7081154c --- /dev/null +++ b/docs/src/API/dynamic_opt.md @@ -0,0 +1,41 @@ +### Solvers + +Currently 4 backends are exposed for solving dynamic optimization problems using collocation: JuMP, InfiniteOpt, CasADi, and Pyomo. + +Please note that there are differences in how to construct the collocation solver for the different cases. For example, the Python based ones, CasADi and Pyomo, expect the solver to be passed in as a string (CasADi and Pyomo come pre-loaded with Ipopt, but other solvers may need to be manually installed using `pip` or `conda`), while JuMP/InfiniteOpt expect the optimizer object to be passed in directly: + +``` +JuMPCollocation(Ipopt.Optimizer, constructRK4()) +CasADiCollocation("ipopt", constructRK4()) +``` + +**JuMP** and **CasADi** collocation require an ODE tableau to be passed in. These can be constructed by calling the `constructX()` functions from DiffEqDevTools. The list of tableaus can be found [here](https://docs.sciml.ai/DiffEqDevDocs/dev/internals/tableaus/). If none is passed in, both solvers will default to using Radau second-order with five collocation points. + +**Pyomo** and **InfiniteOpt** each have their own built-in collocation methods. + + 1. **InfiniteOpt**: The list of InfiniteOpt collocation methods can be found [in the table on this page](https://infiniteopt.github.io/InfiniteOpt.jl/stable/guide/derivative/). If none is passed in, the solver defaults to `FiniteDifference(Backward())`, which is effectively implicit Euler. + 2. **Pyomo**: The list of Pyomo collocation methods can be found [at the bottom of this page](https://github.com/SciML/Pyomo.jl). If none is passed in, the solver defaults to a `LagrangeRadau(3)`. + +Some examples of the latter two collocations: + +```julia +PyomoCollocation("ipopt", LagrangeRadau(2)) +InfiniteOptCollocation(Ipopt.Optimizer, OrthogonalCollocation(3)) +``` + +```@docs; canonical = false +JuMPCollocation +InfiniteOptCollocation +CasADiCollocation +PyomoCollocation +solve(::AbstractDynamicOptProblem) +``` + +### Problem constructors + +```@docs; canonical = false +JuMPDynamicOptProblem +InfiniteOptDynamicOptProblem +CasADiDynamicOptProblem +PyomoDynamicOptProblem +``` diff --git a/ext/MTKPyomoDynamicOptExt.jl b/ext/MTKPyomoDynamicOptExt.jl index 2301a86b68..01a7fa726b 100644 --- a/ext/MTKPyomoDynamicOptExt.jl +++ b/ext/MTKPyomoDynamicOptExt.jl @@ -8,15 +8,15 @@ using Setfield const MTK = ModelingToolkit SPECIAL_FUNCTIONS_DICT = Dict([acos => Pyomo.py_acos, - acosh => Pyomo.py_acosh, - asin => Pyomo.py_asin, - tan => Pyomo.py_tan, - atanh => Pyomo.py_atanh, - cos => Pyomo.py_cos, - log => Pyomo.py_log, - sin => Pyomo.py_sin, - sqrt => Pyomo.py_sqrt, - exp => Pyomo.py_exp]) + acosh => Pyomo.py_acosh, + asin => Pyomo.py_asin, + tan => Pyomo.py_tan, + atanh => Pyomo.py_atanh, + cos => Pyomo.py_cos, + log => Pyomo.py_log, + sin => Pyomo.py_sin, + sqrt => Pyomo.py_sqrt, + exp => Pyomo.py_exp]) struct PyomoDynamicOptModel model::ConcreteModel @@ -34,7 +34,8 @@ struct PyomoDynamicOptModel @variables MODEL_SYM::Symbolics.symstruct(ConcreteModel) T_SYM DUMMY_SYM model.dU = dae.DerivativeVar(U, wrt = model.t, initialize = 0) #add_time_equation!(model, MODEL_SYM, T_SYM) - new(model, U, V, tₛ, is_free_final, nothing, PyomoVar(model.dU), MODEL_SYM, T_SYM, DUMMY_SYM) + new(model, U, V, tₛ, is_free_final, nothing, + PyomoVar(model.dU), MODEL_SYM, T_SYM, DUMMY_SYM) end end @@ -63,19 +64,26 @@ struct PyomoDynamicOptProblem{uType, tType, isinplace, P, F, K} <: end end -pysym_getproperty(s::Union{Num, Symbolics.Symbolic}, name::Symbol) = Symbolics.wrap(SymbolicUtils.term(_getproperty, Symbolics.unwrap(s), Val{name}(), type = Symbolics.Struct{PyomoVar})) -_getproperty(s, name::Val{fieldname}) where fieldname = getproperty(s, fieldname) +function pysym_getproperty(s::Union{Num, Symbolics.Symbolic}, name::Symbol) + Symbolics.wrap(SymbolicUtils.term( + _getproperty, Symbolics.unwrap(s), Val{name}(), type = Symbolics.Struct{PyomoVar})) +end +_getproperty(s, name::Val{fieldname}) where {fieldname} = getproperty(s, fieldname) function MTK.PyomoDynamicOptProblem(sys::System, op, tspan; dt = nothing, steps = nothing, guesses = Dict(), kwargs...) - prob, pmap = MTK.process_DynamicOptProblem(PyomoDynamicOptProblem, PyomoDynamicOptModel, sys, op, tspan; dt, steps, guesses, kwargs...) + prob, + pmap = MTK.process_DynamicOptProblem(PyomoDynamicOptProblem, PyomoDynamicOptModel, + sys, op, tspan; dt, steps, guesses, kwargs...) conc_model = prob.wrapped_model.model MTK.add_equational_constraints!(prob.wrapped_model, sys, pmap, tspan) prob end -MTK.generate_internal_model(m::Type{PyomoDynamicOptModel}) = ConcreteModel(pyomo.ConcreteModel()) +function MTK.generate_internal_model(m::Type{PyomoDynamicOptModel}) + ConcreteModel(pyomo.ConcreteModel()) +end function MTK.generate_time_variable!(m::ConcreteModel, tspan, tsteps) m.steps = length(tsteps) @@ -83,7 +91,7 @@ function MTK.generate_time_variable!(m::ConcreteModel, tspan, tsteps) m.time = pyomo.Var(m.t) end -function MTK.generate_state_variable!(m::ConcreteModel, u0, ns, ts) +function MTK.generate_state_variable!(m::ConcreteModel, u0, ns, ts) m.u_idxs = pyomo.RangeSet(1, ns) init_f = Pyomo.pyfunc((m, i, t) -> (u0[Pyomo.pyconvert(Int, i)])) m.U = pyomo.Var(m.u_idxs, m.t, initialize = init_f) @@ -113,7 +121,7 @@ function MTK.add_constraint!(pmodel::PyomoDynamicOptModel, cons; n_idxs = 1) end expr = Symbolics.substitute(expr, SPECIAL_FUNCTIONS_DICT) - cons_sym = Symbol("cons", hash(cons)) + cons_sym = Symbol("cons", hash(cons)) if occursin(Symbolics.unwrap(t_sym), expr) f = eval(Symbolics.build_function(expr, model_sym, t_sym)) setproperty!(model, cons_sym, pyomo.Constraint(model.t, rule = Pyomo.pyfunc(f))) @@ -176,14 +184,21 @@ struct PyomoCollocation <: AbstractCollocation derivative_method::Pyomo.DiscretizationMethod end -MTK.PyomoCollocation(solver, derivative_method = LagrangeRadau(5)) = PyomoCollocation(solver, derivative_method) +function MTK.PyomoCollocation(solver, derivative_method = LagrangeRadau(5)) + PyomoCollocation(solver, derivative_method) +end -function MTK.prepare_and_optimize!(prob::PyomoDynamicOptProblem, collocation; verbose, kwargs...) +function MTK.prepare_and_optimize!( + prob::PyomoDynamicOptProblem, collocation; verbose, kwargs...) solver_m = prob.wrapped_model.model.clone() dm = collocation.derivative_method discretizer = TransformationFactory(dm) + if MTK.is_free_final(prob.wrapped_model) && !Pyomo.is_finite_difference(dm) + error("The Lagrange-Radau and Lagrange-Legendre collocations currently cannot be used for free final problems.") + end ncp = Pyomo.is_finite_difference(dm) ? 1 : dm.np - discretizer.apply_to(solver_m, wrt = solver_m.t, nfe = solver_m.steps - 1, scheme = Pyomo.scheme_string(dm)) + discretizer.apply_to(solver_m, wrt = solver_m.t, nfe = solver_m.steps - 1, + scheme = Pyomo.scheme_string(dm)) solver = SolverFactory(string(collocation.solver)) results = solver.solve(solver_m, tee = true) @@ -208,13 +223,16 @@ function MTK.get_t_values(output::PyomoOutput) Pyomo.pyconvert(Float64, pyomo.value(m.tₛ)) * [Pyomo.pyconvert(Float64, t) for t in m.t] end -MTK.objective_value(output::PyomoOutput) = Pyomo.pyconvert(Float64, pyomo.value(output.model.obj)) +function MTK.objective_value(output::PyomoOutput) + Pyomo.pyconvert(Float64, pyomo.value(output.model.obj)) +end function MTK.successful_solve(output::PyomoOutput) r = output.result ss = r.solver.status tc = r.solver.termination_condition - if Bool(ss == opt.SolverStatus.ok) && (Bool(tc == opt.TerminationCondition.optimal) || Bool(tc == opt.TerminationCondition.locallyOptimal)) + if Bool(ss == opt.SolverStatus.ok) && (Bool(tc == opt.TerminationCondition.optimal) || + Bool(tc == opt.TerminationCondition.locallyOptimal)) return true else return false From d5a49b0668e4170affbd028fe88b9fe456f467c0 Mon Sep 17 00:00:00 2001 From: vyudu Date: Mon, 2 Jun 2025 10:03:12 -0400 Subject: [PATCH 1988/2176] reemove solver info from tutorial --- docs/src/tutorials/dynamic_optimization.md | 65 +++++++--------------- 1 file changed, 19 insertions(+), 46 deletions(-) diff --git a/docs/src/tutorials/dynamic_optimization.md b/docs/src/tutorials/dynamic_optimization.md index 7753516795..ff1ffab3dc 100644 --- a/docs/src/tutorials/dynamic_optimization.md +++ b/docs/src/tutorials/dynamic_optimization.md @@ -1,4 +1,5 @@ # Solving Dynamic Optimization Problems + Systems in ModelingToolkit.jl can be directly converted to dynamic optimization or optimal control problems. In such systems, one has one or more input variables that are externally controlled to control the dynamics of the system. A dynamic optimization solves for the optimal time trajectory of the input variables in order to maximize or minimize a desired objective function. For example, a car driver might like to know how to step on the accelerator if the goal is to finish a race while using the least gas. To begin, let us take a rocket launch example. The input variable here is the thrust exerted by the engine. The rocket state is described by its current height, mass, and velocity. The mass decreases as the rocket loses fuel while thrusting. @@ -10,8 +11,8 @@ D = ModelingToolkit.D_nounits @parameters h_c m₀ h₀ g₀ D_c c Tₘ m_c @variables begin - h(..) - v(..) + h(..) + v(..) m(..), [bounds = (m_c, 1)] T(..), [input = true, bounds = (0, Tₘ)] end @@ -20,8 +21,8 @@ drag(h, v) = D_c * v^2 * exp(-h_c * (h - h₀) / h₀) gravity(h) = g₀ * (h₀ / h) eqs = [D(h(t)) ~ v(t), - D(v(t)) ~ (T(t) - drag(h(t), v(t))) / m(t) - gravity(h(t)), - D(m(t)) ~ -T(t) / c] + D(v(t)) ~ (T(t) - drag(h(t), v(t))) / m(t) - gravity(h(t)), + D(m(t)) ~ -T(t) / c] (ts, te) = (0.0, 0.2) costs = [-h(te)] @@ -35,24 +36,28 @@ pmap = [ g₀ => 1, m₀ => 1.0, h_c => 500, c => 0.5 * √(g₀ * h₀), D_c => 0.5 * 620 * m₀ / g₀, Tₘ => 3.5 * g₀ * m₀, T(t) => 0.0, h₀ => 1, m_c => 0.6] ``` + What we would like to optimize here is the final height of the rocket. We do this by providing a vector of expressions corresponding to the costs. By default, the sense of the optimization is to minimize the provided cost. So to maximize the rocket height at the final time, we write `-h(te)` as the cost. Now we can construct a problem and solve it. Let us use JuMP as our backend here. Note that the package trigger is actually [InfiniteOpt](https://infiniteopt.github.io/InfiniteOpt.jl/stable/), and not JuMP - this package includes JuMP but is designed for optimization on function spaces. Additionally we need to load the solver package - we will use [Ipopt](https://github.com/jump-dev/Ipopt.jl) here (a good choice in general). Here we have also loaded DiffEqDevTools because we will need to construct the ODE tableau. This is only needed if one desires a custom ODE tableau for the collocation - by default the solver will use RadauIIA5. + ```@example dynamic_opt using InfiniteOpt, Ipopt, DiffEqDevTools jprob = JuMPDynamicOptProblem(rocket, [u0map; pmap], (ts, te); dt = 0.001) jsol = solve(jprob, JuMPCollocation(Ipopt.Optimizer, constructRadauIIA5())); ``` + The solution has three fields: `jsol.sol` is the ODE solution for the states, `jsol.input_sol` is the ODE solution for the inputs, and `jsol.model` is the wrapped model that we can use to query things like objective and constraint residuals. Let's plot the final solution and the controller here: + ```@example dynamic_opt using CairoMakie fig = Figure(resolution = (800, 400)) -ax1 = Axis(fig[1,1], title = "Rocket trajectory", xlabel = "Time") -ax2 = Axis(fig[1,2], title = "Control trajectory", xlabel = "Time") +ax1 = Axis(fig[1, 1], title = "Rocket trajectory", xlabel = "Time") +ax2 = Axis(fig[1, 2], title = "Control trajectory", xlabel = "Time") for u in unknowns(rocket) lines!(ax1, jsol.sol.t, jsol.sol[u], label = string(u)) @@ -64,12 +69,14 @@ fig ``` ### Free final time problems + There are additionally a class of dynamic optimization problems where we would like to know how to control our system to achieve something in the least time. Such problems are called free final time problems, since the final time is unknown. To model these problems in ModelingToolkit, we declare the final time as a parameter. Below we have a model system called the double integrator. We control the acceleration of a block in order to reach a desired destination in the least time. + ```@example dynamic_opt @variables begin - x(..) + x(..) v(..) u(..), [bounds = (-1.0, 1.0), input = true] end @@ -88,6 +95,7 @@ u0map = [x(t) => 1.0, v(t) => 0.0] tspan = (0.0, tf) parammap = [u(t) => 0.0, tf => 1.0] ``` + The `tf` mapping in the parameter map is treated as an initial guess. Please note that, at the moment, free final time problems cannot support constraints defined at definite time values, like `x(3) ~ 2`. @@ -97,16 +105,18 @@ Please note that, at the moment, free final time problems cannot support constra The Pyomo collocation methods (LagrangeRadau, LagrangeLegendre) currently are bugged for free final time problems. Strongly suggest using BackwardEuler() for such problems when using Pyomo as the backend. When declaring the problem in this case we need to provide the number of steps, since dt can't be known in advanced. Let's solve plot our final solution and the controller for the block, using InfiniteOpt as the backend: + ```@example dynamic_opt iprob = InfiniteOptDynamicOptProblem(block, [u0map; parammap], tspan; steps = 100) isol = solve(iprob, InfiniteOptCollocation(Ipopt.Optimizer)); ``` Let's plot the final solution and the controller here: + ```@example dynamic_opt fig = Figure(resolution = (800, 400)) -ax1 = Axis(fig[1,1], title = "Block trajectory", xlabel = "Time") -ax2 = Axis(fig[1,2], title = "Control trajectory", xlabel = "Time") +ax1 = Axis(fig[1, 1], title = "Block trajectory", xlabel = "Time") +ax2 = Axis(fig[1, 2], title = "Control trajectory", xlabel = "Time") for u in unknowns(block) lines!(ax1, isol.sol.t, isol.sol[u], label = string(u)) @@ -116,40 +126,3 @@ axislegend(ax1) axislegend(ax2) fig ``` - -### Solvers -Currently 4 backends are exposed for solving dynamic optimization problems using collocation: JuMP, InfiniteOpt, CasADi, and Pyomo. - -Please note that there are differences in how to construct the collocation solver for the different cases. For example, the Python based ones, CasADi and Pyomo, expect the solver to be passed in as a string (CasADi and Pyomo come pre-loaded with Ipopt, but other solvers may need to be manually installed using `pip` or `conda`), while JuMP/InfiniteOpt expect the optimizer object to be passed in directly: -``` -JuMPCollocation(Ipopt.Optimizer, constructRK4()) -CasADiCollocation("ipopt", constructRK4()) -``` - -**JuMP** and **CasADi** collocation require an ODE tableau to be passed in. These can be constructed by calling the `constructX()` functions from DiffEqDevTools. The list of tableaus can be found [here](https://docs.sciml.ai/DiffEqDevDocs/dev/internals/tableaus/). If none is passed in, both solvers will default to using Radau second-order with five collocation points. - -**Pyomo** and **InfiniteOpt** each have their own built-in collocation methods. -1. **InfiniteOpt**: The list of InfiniteOpt collocation methods can be found [in the table on this page](https://infiniteopt.github.io/InfiniteOpt.jl/stable/guide/derivative/). If none is passed in, the solver defaults to `FiniteDifference(Backward())`, which is effectively implicit Euler. -2. **Pyomo**: The list of Pyomo collocation methods can be found [at the bottom of this page](https://github.com/SciML/Pyomo.jl). If none is passed in, the solver defaults to a `LagrangeRadau(3)`. - -Some examples of the latter two collocations: -```julia -PyomoCollocation("ipopt", LagrangeRadau(2)) -InfiniteOptCollocation(Ipopt.Optimizer, OrthogonalCollocation(3)) -``` - -```@docs; canonical = false -JuMPCollocation -InfiniteOptCollocation -CasADiCollocation -PyomoCollocation -solve(::AbstractDynamicOptProblem) -``` - -### Problem constructors -```@docs; canonical = false -JuMPDynamicOptProblem -InfiniteOptDynamicOptProblem -CasADiDynamicOptProblem -PyomoDynamicOptProblem -``` From 8750986b434cb9f24baa22d3702d6886d0d6081e Mon Sep 17 00:00:00 2001 From: vyudu Date: Mon, 2 Jun 2025 11:04:17 -0400 Subject: [PATCH 1989/2176] update Project.toml, fix substitution bug --- Project.toml | 3 ++- ext/MTKPyomoDynamicOptExt.jl | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/Project.toml b/Project.toml index 215c0bcd1b..098757e3b9 100644 --- a/Project.toml +++ b/Project.toml @@ -46,7 +46,6 @@ OffsetArrays = "6fe1bfb0-de20-5000-8ca7-80f57d26f881" OrderedCollections = "bac558e1-5e72-5ebc-8fee-abe8a469f55d" OrdinaryDiffEqCore = "bbf590c4-e513-4bbe-9b18-05decba2e5d8" PrecompileTools = "aea7be01-6a6a-4083-8856-8a6e6704d82a" -Pyomo = "0e8e1daf-01b5-4eba-a626-3897743a3816" RecursiveArrayTools = "731186ca-8d62-57ce-b412-fbd966d074cd" Reexport = "189a3867-3050-52da-a836-e630ba90ab69" RuntimeGeneratedFunctions = "7e49a35a-f44a-4d26-94aa-eba1b4ca6b47" @@ -74,6 +73,7 @@ DeepDiffs = "ab62b9b5-e342-54a8-a765-a90f495de1a6" FMI = "14a09403-18e3-468f-ad8a-74f8dda2d9ac" InfiniteOpt = "20393b10-9daf-11e9-18c9-8db751c92c57" LabelledArrays = "2ee39098-c373-598a-b85f-a56591580800" +Pyomo = "0e8e1daf-01b5-4eba-a626-3897743a3816" [extensions] MTKBifurcationKitExt = "BifurcationKit" @@ -142,6 +142,7 @@ OrdinaryDiffEqCore = "1.15.0" OrdinaryDiffEqDefault = "1.2" OrdinaryDiffEqNonlinearSolve = "1.5.0" PrecompileTools = "1" +Pyomo = "0.1.0" REPL = "1" RecursiveArrayTools = "3.26" Reexport = "0.2, 1" diff --git a/ext/MTKPyomoDynamicOptExt.jl b/ext/MTKPyomoDynamicOptExt.jl index 01a7fa726b..ce667cde6d 100644 --- a/ext/MTKPyomoDynamicOptExt.jl +++ b/ext/MTKPyomoDynamicOptExt.jl @@ -119,7 +119,7 @@ function MTK.add_constraint!(pmodel::PyomoDynamicOptModel, cons; n_idxs = 1) else cons.lhs - cons.rhs ≤ 0 end - expr = Symbolics.substitute(expr, SPECIAL_FUNCTIONS_DICT) + expr = Symbolics.substitute(Symbolics.unwrap(expr), SPECIAL_FUNCTIONS_DICT) cons_sym = Symbol("cons", hash(cons)) if occursin(Symbolics.unwrap(t_sym), expr) From 97bb8ffacb4000819de1a3aea9745261e2d45ba9 Mon Sep 17 00:00:00 2001 From: vyudu Date: Mon, 2 Jun 2025 13:44:09 -0400 Subject: [PATCH 1990/2176] add Pyomo to extensions --- test/extensions/Project.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/test/extensions/Project.toml b/test/extensions/Project.toml index 9f43e6f4a4..6a8d01a7b8 100644 --- a/test/extensions/Project.toml +++ b/test/extensions/Project.toml @@ -20,6 +20,7 @@ OrdinaryDiffEqNonlinearSolve = "127b3ac7-2247-4354-8eb6-78cf4e7c58e8" OrdinaryDiffEqSDIRK = "2d112036-d095-4a1e-ab9a-08536f3ecdbf" OrdinaryDiffEqTsit5 = "b1df2697-797e-41e3-8120-5422d3b24e4a" OrdinaryDiffEqVerner = "79d7bb75-1356-48c1-b8c0-6832512096c2" +Pyomo = "0e8e1daf-01b5-4eba-a626-3897743a3816" SciMLSensitivity = "1ed8b502-d754-442c-8d5d-10ac956f44a1" SciMLStructures = "53ae85a6-f571-4167-b2af-e1d143709226" SimpleDiffEq = "05bca326-078c-5bf0-a5bf-ce7c7982d7fd" From a8181789e5cac6e2cb34fcbcb7d9400af5a88683 Mon Sep 17 00:00:00 2001 From: vyudu Date: Thu, 5 Jun 2025 10:18:56 -0400 Subject: [PATCH 1991/2176] fix: fix pyomo problems on lts --- ext/MTKPyomoDynamicOptExt.jl | 17 +++-------------- 1 file changed, 3 insertions(+), 14 deletions(-) diff --git a/ext/MTKPyomoDynamicOptExt.jl b/ext/MTKPyomoDynamicOptExt.jl index ce667cde6d..cb0aafc432 100644 --- a/ext/MTKPyomoDynamicOptExt.jl +++ b/ext/MTKPyomoDynamicOptExt.jl @@ -7,7 +7,7 @@ using NaNMath using Setfield const MTK = ModelingToolkit -SPECIAL_FUNCTIONS_DICT = Dict([acos => Pyomo.py_acos, +const SPECIAL_FUNCTIONS_DICT = Dict([acos => Pyomo.py_acos, acosh => Pyomo.py_acosh, asin => Pyomo.py_asin, tan => Pyomo.py_tan, @@ -33,22 +33,11 @@ struct PyomoDynamicOptModel function PyomoDynamicOptModel(model, U, V, tₛ, is_free_final) @variables MODEL_SYM::Symbolics.symstruct(ConcreteModel) T_SYM DUMMY_SYM model.dU = dae.DerivativeVar(U, wrt = model.t, initialize = 0) - #add_time_equation!(model, MODEL_SYM, T_SYM) new(model, U, V, tₛ, is_free_final, nothing, PyomoVar(model.dU), MODEL_SYM, T_SYM, DUMMY_SYM) end end -function add_time_equation!(model::ConcreteModel, model_sym, t_sym) - model.dtime = dae.DerivativeVar(model.time) - - mdt = Symbolics.value(pysym_getproperty(model_sym, :dtime)) - mts = Symbolics.value(pysym_getproperty(model_sym, :tₛ)) - expr = mdt[t_sym] - mts == 0 - f = Pyomo.pyfunc(eval(Symbolics.build_function(expr, model_sym, t_sym))) - model.time_eq = pyomo.Constraint(model.t, rule = f) -end - struct PyomoDynamicOptProblem{uType, tType, isinplace, P, F, K} <: AbstractDynamicOptProblem{uType, tType, isinplace} f::F @@ -119,7 +108,7 @@ function MTK.add_constraint!(pmodel::PyomoDynamicOptModel, cons; n_idxs = 1) else cons.lhs - cons.rhs ≤ 0 end - expr = Symbolics.substitute(Symbolics.unwrap(expr), SPECIAL_FUNCTIONS_DICT) + expr = Symbolics.substitute(Symbolics.unwrap(expr), SPECIAL_FUNCTIONS_DICT, fold = false) cons_sym = Symbol("cons", hash(cons)) if occursin(Symbolics.unwrap(t_sym), expr) @@ -133,7 +122,7 @@ end function MTK.set_objective!(pmodel::PyomoDynamicOptModel, expr) @unpack model, model_sym, t_sym, dummy_sym = pmodel - expr = Symbolics.substitute(expr, SPECIAL_FUNCTIONS_DICT) + expr = Symbolics.substitute(expr, SPECIAL_FUNCTIONS_DICT, fold = false) if occursin(Symbolics.unwrap(t_sym), expr) f = eval(Symbolics.build_function(expr, model_sym, t_sym)) model.obj = pyomo.Objective(model.t, rule = Pyomo.pyfunc(f)) From 639ed6a1c0374687ffd25f2efa0ed5b26d6a21da Mon Sep 17 00:00:00 2001 From: vyudu Date: Thu, 5 Jun 2025 10:48:03 -0400 Subject: [PATCH 1992/2176] fix: use symbolic indexing for solutions of pprobs --- test/extensions/dynamic_optimization.jl | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/test/extensions/dynamic_optimization.jl b/test/extensions/dynamic_optimization.jl index 0243b298d6..3884bc1aa9 100644 --- a/test/extensions/dynamic_optimization.jl +++ b/test/extensions/dynamic_optimization.jl @@ -69,9 +69,8 @@ const M = ModelingToolkit pprob = PyomoDynamicOptProblem( lksys, [u0map; parammap], tspan; guesses = guess, dt = 0.01) psol = solve(pprob, PyomoCollocation("ipopt", LagrangeLegendre(3))) - @show psol.sol - @test psol.sol(0.6)[1] ≈ 3.5 - @test psol.sol(0.3)[1] ≈ 7.0 + @test psol.sol(0.6; idxs = x(t)) ≈ 3.5 + @test psol.sol(0.3; idxs = x(t)) ≈ 7.0 iprob = InfiniteOptDynamicOptProblem( lksys, [u0map; parammap], tspan; guesses = guess, dt = 0.01) @@ -161,7 +160,7 @@ end pprob = PyomoDynamicOptProblem(block, [u0map; parammap], tspan; dt = 0.01) psol = solve(pprob, PyomoCollocation("ipopt", BackwardEuler())) @test is_bangbang(psol.input_sol, [-1.0], [1.0]) - @test ≈(psol.sol.u[end][2], 0.25, rtol = 1e-3) + @test ≈(psol.sol[x(t)][end], 0.25, rtol = 1e-3) spline = ctrl_to_spline(isol.input_sol, ConstantInterpolation) oprob = ODEProblem(block_ode, [u0map; u_interp => spline], tspan) @@ -251,7 +250,7 @@ end pprob = PyomoDynamicOptProblem(rocket, [u0map; pmap], (ts, te); dt = 0.001, cse = false) psol = solve(pprob, PyomoCollocation("ipopt", LagrangeRadau(4))) - @test psol.sol.u[end][1] > 1.012 + @test psol.sol[h(t)][end] > 1.012 # Test solution @parameters (T_interp::CubicSpline)(..) From 50a4fc3d5b493584d7db42f746824ba2eae589b2 Mon Sep 17 00:00:00 2001 From: fchen121 Date: Thu, 5 Jun 2025 12:33:07 -0400 Subject: [PATCH 1993/2176] Fix Linear transformation to diagonal system test --- test/changeofvariables.jl | 53 ++++++++++++++++++++++----------------- 1 file changed, 30 insertions(+), 23 deletions(-) diff --git a/test/changeofvariables.jl b/test/changeofvariables.jl index 2667e143f2..69f79768e3 100644 --- a/test/changeofvariables.jl +++ b/test/changeofvariables.jl @@ -62,36 +62,43 @@ new_sol = solve(new_prob, Tsit5()) @test isapprox(sol[x][end], new_sol[x][end], rtol=1e-4) -# # Linear transformation to diagonal system -# @variables x(t)[1:3] -# D = Differential(t) -# A = [0. -1. 0.; -0.5 0.5 0.; 0. 0. -1.] -# right = A.*transpose(x) -# eqs = [D(x[1]) ~ sum(right[1, 1:3]), D(x[2]) ~ sum(right[2, 1:3]), D(x[3]) ~ sum(right[3, 1:3])] +# Linear transformation to diagonal system +@independent_variables t +@variables x(t)[1:3] +x = reshape(x, 3, 1) +D = Differential(t) +A = [0. -1. 0.; -0.5 0.5 0.; 0. 0. -1.] +right = A*x +eqs = vec(D.(x) .~ right) -# tspan = (0., 10.) -# u0 = [x[1] => 1.0, x[2] => 2.0, x[3] => -1.0] +tspan = (0., 10.) +u0 = [x[1] => 1.0, x[2] => 2.0, x[3] => -1.0] -# @mtkcompile sys = System(eqs, t; defaults=u0) -# prob = ODEProblem(sys,[],tspan) -# sol = solve(prob, Tsit5()) +@mtkcompile sys = System(eqs, t; defaults=u0) +prob = ODEProblem(sys,[],tspan) +sol = solve(prob, Tsit5()) -# T = eigen(A).vectors +T = eigen(A).vectors +T_inv = inv(T) -# @variables z(t)[1:3] -# forward_subs = T \ x .=> z -# backward_subs = x .=> T*z +@variables z(t)[1:3] +z = reshape(z, 3, 1) +forward_subs = vec(T_inv*x .=> z) +backward_subs = vec(x .=> T*z) -# new_sys = changeofvariables(sys, t, forward_subs, backward_subs; simplify=true) +new_sys = changeofvariables(sys, t, forward_subs, backward_subs; simplify=true) -# new_prob = ODEProblem(new_sys, [], tspan) -# new_sol = solve(new_prob, Tsit5()) +new_prob = ODEProblem(new_sys, [], tspan) +new_sol = solve(new_prob, Tsit5()) -# # test RHS -# new_rhs = [eq.rhs for eq in equations(new_sys)] -# new_A = Symbolics.value.(Symbolics.jacobian(new_rhs, z)) -# @test isapprox(diagm(eigen(A).values), new_A, rtol = 1e-10) -# @test isapprox( new_sol[x[1],end], sol[x[1],end], rtol=1e-4) +# test RHS +new_rhs = [eq.rhs for eq in equations(new_sys)] +new_A = Symbolics.value.(Symbolics.jacobian(new_rhs, z)) +A = diagm(eigen(A).values) +A = sortslices(A, dims=1) +new_A = sortslices(new_A, dims=1) +@test isapprox(A, new_A, rtol = 1e-10) +@test isapprox( new_sol[x[1],end], sol[x[1],end], rtol=1e-4) # Change of variables for sde @independent_variables t From a1e99446edd556f92923c1c353675adb6a36b1c2 Mon Sep 17 00:00:00 2001 From: fchen121 Date: Thu, 5 Jun 2025 12:36:16 -0400 Subject: [PATCH 1994/2176] Update Project.toml --- Project.toml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Project.toml b/Project.toml index 6632f626a2..525e14534b 100644 --- a/Project.toml +++ b/Project.toml @@ -65,6 +65,7 @@ StochasticDiffEq = "789caeaf-c7a9-5a7d-9973-96adeb23e2a0" SymbolicIndexingInterface = "2efcf032-c050-4f8e-a9bb-153293bab1f5" SymbolicUtils = "d1185830-fcd6-423d-90d6-eec64667417b" Symbolics = "0c5d862f-8b57-4792-8d23-62f2024744c7" +TermInterface = "8ea1fca8-c5ef-4a55-8b96-4e9afe9c9a3c" Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" URIs = "5c2747f8-b7ea-4ff2-ba2e-563bfd36b1d4" UnPack = "3a884ed6-31ef-47d7-9d2a-63182c4928ed" @@ -163,6 +164,7 @@ StochasticDiffEq = "6.72.1" SymbolicIndexingInterface = "0.3.39" SymbolicUtils = "3.26.1" Symbolics = "6.40" +TermInterface = "2.0.0" Test = "1.11.0" URIs = "1" UnPack = "0.1, 1.0" From e33bcc1f6ac5baa94278ca1bf520f9224977e49b Mon Sep 17 00:00:00 2001 From: fchen121 Date: Thu, 5 Jun 2025 13:39:14 -0400 Subject: [PATCH 1995/2176] Change backward subs from observed to equations --- src/systems/diffeqs/basic_transformations.jl | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/systems/diffeqs/basic_transformations.jl b/src/systems/diffeqs/basic_transformations.jl index fbf03eacda..3061ae20ea 100644 --- a/src/systems/diffeqs/basic_transformations.jl +++ b/src/systems/diffeqs/basic_transformations.jl @@ -131,9 +131,9 @@ function changeofvariables(sys::System, iv, forward_subs, backward_subs; simplif new_defs[para] = defs[para] end end - @named new_sys = System(new_eqs, t; + @named new_sys = System(vcat(new_eqs, first.(backward_subs) .~ last.(backward_subs)), t; defaults=new_defs, - observed=vcat(observed(sys),first.(backward_subs) .~ last.(backward_subs)) + observed=observed(sys) ) if simplify return mtkcompile(new_sys) @@ -190,9 +190,9 @@ function change_of_variable_SDE(sys::System, iv, forward_subs, backward_subs; si end end - @named new_sys = System(new_eqs, t; + @named new_sys = System(vcat(new_eqs, first.(backward_subs) .~ last.(backward_subs)), t; defaults=new_defs, - observed=vcat(observed(sys),first.(backward_subs) .~ last.(backward_subs)) + observed=observed(sys) ) if simplify return mtkcompile(new_sys) From 192872ce19f3fc0cb6e2af2c99b9be2bef2ca939 Mon Sep 17 00:00:00 2001 From: fchen121 Date: Thu, 5 Jun 2025 17:54:27 -0400 Subject: [PATCH 1996/2176] Change of variables for multiple Brownian SDE --- src/systems/diffeqs/basic_transformations.jl | 24 +-- test/changeofvariables.jl | 157 ++++++++++--------- 2 files changed, 99 insertions(+), 82 deletions(-) diff --git a/src/systems/diffeqs/basic_transformations.jl b/src/systems/diffeqs/basic_transformations.jl index 3061ae20ea..26131554ed 100644 --- a/src/systems/diffeqs/basic_transformations.jl +++ b/src/systems/diffeqs/basic_transformations.jl @@ -144,7 +144,6 @@ end function change_of_variable_SDE(sys::System, iv, forward_subs, backward_subs; simplify=true, t0=missing) sys = mtkcompile(sys) t = iv - @brownian B old_vars = first.(backward_subs) new_vars = last.(forward_subs) @@ -152,20 +151,27 @@ function change_of_variable_SDE(sys::System, iv, forward_subs, backward_subs; si # use: f = Y(t, X) # use: dY = (∂f/∂t + μ∂f/∂x + (1/2)*σ^2*∂2f/∂x2)dt + σ∂f/∂xdW old_eqs = equations(sys) - old_noise = ModelingToolkit.get_noise_eqs(sys) + neqs = get_noise_eqs(sys) + neqs = [neqs[i,:] for i in 1:size(neqs,1)] - # Is there a function to find partial derivative? - ∂f∂t = Symbolics.derivative( first.(forward_subs), t ) - ∂f∂t = [substitute(f_t, Differential(t)(old_var) => 0) for (f_t, old_var) in zip(∂f∂t, old_vars)] + brownvars = map([Symbol(:B, :_, i) for i in 1:length(neqs[1])]) do name + unwrap(only(@brownian $name)) + end + # df/dt = ∂f/∂x dx/dt + ∂f/∂t + dfdt = Symbolics.derivative( first.(forward_subs), t ) ∂f∂x = [Symbolics.derivative( first(f_sub), old_var ) for (f_sub, old_var) in zip(forward_subs, old_vars)] ∂2f∂x2 = Symbolics.derivative.( ∂f∂x, old_vars ) new_eqs = Equation[] - for (new_var, eq, noise, first, second, third) in zip(new_vars, old_eqs, old_noise, ∂f∂t, ∂f∂x, ∂2f∂x2) - ex = first + eq.rhs * second + 1/2 * noise^2 * third + noise*second*B - for eqs in old_eqs - ex = substitute(ex, eqs.lhs => eqs.rhs) + for (new_var, ex, first, second) in zip(new_vars, dfdt, ∂f∂x, ∂2f∂x2) + for (eqs, neq) in zip(old_eqs, neqs) + if occursin(value(eqs.lhs), value(ex)) + ex = substitute(ex, eqs.lhs => eqs.rhs) + for (noise, B) in zip(neq, brownvars) + ex = ex + 1/2 * noise^2 * second + noise*first*B + end + end end ex = substitute(ex, Dict(forward_subs)) ex = substitute(ex, Dict(backward_subs)) diff --git a/test/changeofvariables.jl b/test/changeofvariables.jl index 69f79768e3..d5eb7d8dd4 100644 --- a/test/changeofvariables.jl +++ b/test/changeofvariables.jl @@ -4,101 +4,101 @@ using Test, LinearAlgebra # Change of variables: z = log(x) # (this implies that x = exp(z) is automatically non-negative) -@independent_variables t -@variables z(t)[1:2, 1:2] -D = Differential(t) -eqs = [D(D(z)) ~ ones(2, 2)] -@mtkcompile sys = System(eqs, t) -@test_nowarn ODEProblem(sys, [z => zeros(2, 2), D(z) => ones(2, 2)], (0.0, 10.0)) +# @independent_variables t +# @variables z(t)[1:2, 1:2] +# D = Differential(t) +# eqs = [D(D(z)) ~ ones(2, 2)] +# @mtkcompile sys = System(eqs, t) +# @test_nowarn ODEProblem(sys, [z => zeros(2, 2), D(z) => ones(2, 2)], (0.0, 10.0)) -@parameters α -@variables x(t) -D = Differential(t) -eqs = [D(x) ~ α*x] +# @parameters α +# @variables x(t) +# D = Differential(t) +# eqs = [D(x) ~ α*x] -tspan = (0., 1.) -def = [x => 1.0, α => -0.5] +# tspan = (0., 1.) +# def = [x => 1.0, α => -0.5] -@mtkcompile sys = System(eqs, t;defaults=def) -prob = ODEProblem(sys, [], tspan) -sol = solve(prob, Tsit5()) +# @mtkcompile sys = System(eqs, t;defaults=def) +# prob = ODEProblem(sys, [], tspan) +# sol = solve(prob, Tsit5()) -@variables z(t) -forward_subs = [log(x) => z] -backward_subs = [x => exp(z)] -new_sys = changeofvariables(sys, t, forward_subs, backward_subs) -@test equations(new_sys)[1] == (D(z) ~ α) +# @variables z(t) +# forward_subs = [log(x) => z] +# backward_subs = [x => exp(z)] +# new_sys = changeofvariables(sys, t, forward_subs, backward_subs) +# @test equations(new_sys)[1] == (D(z) ~ α) -new_prob = ODEProblem(new_sys, [], tspan) -new_sol = solve(new_prob, Tsit5()) +# new_prob = ODEProblem(new_sys, [], tspan) +# new_sol = solve(new_prob, Tsit5()) -@test isapprox(new_sol[x][end], sol[x][end], atol=1e-4) +# @test isapprox(new_sol[x][end], sol[x][end], atol=1e-4) -# Riccati equation -@parameters α -@variables x(t) -D = Differential(t) -eqs = [D(x) ~ t^2 + α - x^2] -def = [x=>1., α => 1.] -@mtkcompile sys = System(eqs, t; defaults=def) +# # Riccati equation +# @parameters α +# @variables x(t) +# D = Differential(t) +# eqs = [D(x) ~ t^2 + α - x^2] +# def = [x=>1., α => 1.] +# @mtkcompile sys = System(eqs, t; defaults=def) -@variables z(t) -forward_subs = [t + α/(x+t) => z ] -backward_subs = [ x => α/(z-t) - t] +# @variables z(t) +# forward_subs = [t + α/(x+t) => z ] +# backward_subs = [ x => α/(z-t) - t] -new_sys = changeofvariables(sys, t, forward_subs, backward_subs; simplify=true, t0=0.) -# output should be equivalent to -# t^2 + α - z^2 + 2 (but this simplification is not found automatically) +# new_sys = changeofvariables(sys, t, forward_subs, backward_subs; simplify=true, t0=0.) +# # output should be equivalent to +# # t^2 + α - z^2 + 2 (but this simplification is not found automatically) -tspan = (0., 1.) -prob = ODEProblem(sys,[],tspan) -new_prob = ODEProblem(new_sys,[],tspan) +# tspan = (0., 1.) +# prob = ODEProblem(sys,[],tspan) +# new_prob = ODEProblem(new_sys,[],tspan) -sol = solve(prob, Tsit5()) -new_sol = solve(new_prob, Tsit5()) +# sol = solve(prob, Tsit5()) +# new_sol = solve(new_prob, Tsit5()) -@test isapprox(sol[x][end], new_sol[x][end], rtol=1e-4) +# @test isapprox(sol[x][end], new_sol[x][end], rtol=1e-4) -# Linear transformation to diagonal system -@independent_variables t -@variables x(t)[1:3] -x = reshape(x, 3, 1) -D = Differential(t) -A = [0. -1. 0.; -0.5 0.5 0.; 0. 0. -1.] -right = A*x -eqs = vec(D.(x) .~ right) +# # Linear transformation to diagonal system +# @independent_variables t +# @variables x(t)[1:3] +# x = reshape(x, 3, 1) +# D = Differential(t) +# A = [0. -1. 0.; -0.5 0.5 0.; 0. 0. -1.] +# right = A*x +# eqs = vec(D.(x) .~ right) -tspan = (0., 10.) -u0 = [x[1] => 1.0, x[2] => 2.0, x[3] => -1.0] +# tspan = (0., 10.) +# u0 = [x[1] => 1.0, x[2] => 2.0, x[3] => -1.0] -@mtkcompile sys = System(eqs, t; defaults=u0) -prob = ODEProblem(sys,[],tspan) -sol = solve(prob, Tsit5()) +# @mtkcompile sys = System(eqs, t; defaults=u0) +# prob = ODEProblem(sys,[],tspan) +# sol = solve(prob, Tsit5()) -T = eigen(A).vectors -T_inv = inv(T) +# T = eigen(A).vectors +# T_inv = inv(T) -@variables z(t)[1:3] -z = reshape(z, 3, 1) -forward_subs = vec(T_inv*x .=> z) -backward_subs = vec(x .=> T*z) +# @variables z(t)[1:3] +# z = reshape(z, 3, 1) +# forward_subs = vec(T_inv*x .=> z) +# backward_subs = vec(x .=> T*z) -new_sys = changeofvariables(sys, t, forward_subs, backward_subs; simplify=true) +# new_sys = changeofvariables(sys, t, forward_subs, backward_subs; simplify=true) -new_prob = ODEProblem(new_sys, [], tspan) -new_sol = solve(new_prob, Tsit5()) +# new_prob = ODEProblem(new_sys, [], tspan) +# new_sol = solve(new_prob, Tsit5()) -# test RHS -new_rhs = [eq.rhs for eq in equations(new_sys)] -new_A = Symbolics.value.(Symbolics.jacobian(new_rhs, z)) -A = diagm(eigen(A).values) -A = sortslices(A, dims=1) -new_A = sortslices(new_A, dims=1) -@test isapprox(A, new_A, rtol = 1e-10) -@test isapprox( new_sol[x[1],end], sol[x[1],end], rtol=1e-4) +# # test RHS +# new_rhs = [eq.rhs for eq in equations(new_sys)] +# new_A = Symbolics.value.(Symbolics.jacobian(new_rhs, z)) +# A = diagm(eigen(A).values) +# A = sortslices(A, dims=1) +# new_A = sortslices(new_A, dims=1) +# @test isapprox(A, new_A, rtol = 1e-10) +# @test isapprox( new_sol[x[1],end], sol[x[1],end], rtol=1e-4) # Change of variables for sde @independent_variables t @@ -116,4 +116,15 @@ new_sys = change_of_variable_SDE(sys, t, forward_subs, backward_subs) @test equations(new_sys)[1] == (D(y) ~ μ - 1/2*σ^2) @test ModelingToolkit.get_noise_eqs(new_sys)[1] === ModelingToolkit.value(σ) - +#Multiple Brownian and equations +@independent_variables t +@brownian Bx By +@parameters μ σ α +@variables x(t) y(t) z(t) w(t) u(t) v(t) +D = Differential(t) +eqs = [D(x) ~ μ*x + σ*x*Bx, D(y) ~ α*By, D(u) ~ μ*u + σ*u*Bx + α*u*By] +def = [x=>0., y=> 0., u=>0., μ => 2., σ=>1., α=>3.] +@mtkcompile sys = System(eqs, t; defaults=def) +forward_subs = [log(x) => z, y^2 => w, log(u) => v] +backward_subs = [x => exp(z), y => w^.5, u => exp(v)] +new_sys = change_of_variable_SDE(sys, t, forward_subs, backward_subs) \ No newline at end of file From 9a5312189533952593cc29d5bd40a5d19480022a Mon Sep 17 00:00:00 2001 From: fchen121 Date: Thu, 5 Jun 2025 18:21:53 -0400 Subject: [PATCH 1997/2176] Improve change of variables test --- test/changeofvariables.jl | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/test/changeofvariables.jl b/test/changeofvariables.jl index d5eb7d8dd4..68a8a143b6 100644 --- a/test/changeofvariables.jl +++ b/test/changeofvariables.jl @@ -101,6 +101,9 @@ using Test, LinearAlgebra # @test isapprox( new_sol[x[1],end], sol[x[1],end], rtol=1e-4) # Change of variables for sde +noise_eqs = ModelingToolkit.get_noise_eqs +value = ModelingToolkit.value + @independent_variables t @brownian B @parameters μ σ @@ -114,7 +117,7 @@ forward_subs = [log(x) => y] backward_subs = [x => exp(y)] new_sys = change_of_variable_SDE(sys, t, forward_subs, backward_subs) @test equations(new_sys)[1] == (D(y) ~ μ - 1/2*σ^2) -@test ModelingToolkit.get_noise_eqs(new_sys)[1] === ModelingToolkit.value(σ) +@test noise_eqs(new_sys)[1] === value(σ) #Multiple Brownian and equations @independent_variables t @@ -127,4 +130,13 @@ def = [x=>0., y=> 0., u=>0., μ => 2., σ=>1., α=>3.] @mtkcompile sys = System(eqs, t; defaults=def) forward_subs = [log(x) => z, y^2 => w, log(u) => v] backward_subs = [x => exp(z), y => w^.5, u => exp(v)] -new_sys = change_of_variable_SDE(sys, t, forward_subs, backward_subs) \ No newline at end of file +new_sys = change_of_variable_SDE(sys, t, forward_subs, backward_subs) +@test equations(new_sys)[1] == (D(z) ~ μ - 1/2*σ^2) +@test equations(new_sys)[2] == (D(w) ~ α^2) +@test equations(new_sys)[3] == (D(v) ~ μ - 1/2*(α^2 + σ^2)) +@test noise_eqs(new_sys)[1,1] === value(σ) +@test noise_eqs(new_sys)[1,2] === value(0) +@test noise_eqs(new_sys)[2,1] === value(0) +@test noise_eqs(new_sys)[2,2] === value(substitute(2*α*y, backward_subs[2])) +@test noise_eqs(new_sys)[3,1] === value(σ) +@test noise_eqs(new_sys)[3,2] === value(α) \ No newline at end of file From 3c6f7036312f874f5dabe3cf92a92ae5ae5b2de8 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 6 Jun 2025 17:14:32 +0530 Subject: [PATCH 1998/2176] feat: add `is_discrete` flag to systems --- src/systems/abstractsystem.jl | 1 + src/systems/system.jl | 16 +++++++++------- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index 533206883f..fb69d8b702 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -760,6 +760,7 @@ for prop in [:eqs :metadata :gui_metadata :is_initializesystem + :is_discrete :parameter_dependencies :assertions :ignored_connections diff --git a/src/systems/system.jl b/src/systems/system.jl index 7db44c7c97..cb330b382f 100644 --- a/src/systems/system.jl +++ b/src/systems/system.jl @@ -235,6 +235,7 @@ struct System <: AbstractSystem Whether the current system is an initialization system. """ is_initializesystem::Bool + is_discrete::Bool """ $INTERNAL_FIELD_WARNING Whether the system has been simplified by `mtkcompile`. @@ -255,8 +256,8 @@ struct System <: AbstractSystem is_dde = false, tstops = [], tearing_state = nothing, namespacing = true, complete = false, index_cache = nothing, ignored_connections = nothing, preface = nothing, parent = nothing, initializesystem = nothing, - is_initializesystem = false, isscheduled = false, schedule = nothing; - checks::Union{Bool, Int} = true) + is_initializesystem = false, is_discrete = false, isscheduled = false, + schedule = nothing; checks::Union{Bool, Int} = true) if is_initializesystem && iv !== nothing throw(ArgumentError(""" Expected initialization system to be time-independent. Found independent @@ -293,7 +294,8 @@ struct System <: AbstractSystem guesses, systems, initialization_eqs, continuous_events, discrete_events, connector_type, assertions, metadata, gui_metadata, is_dde, tstops, tearing_state, namespacing, complete, index_cache, ignored_connections, - preface, parent, initializesystem, is_initializesystem, isscheduled, schedule) + preface, parent, initializesystem, is_initializesystem, is_discrete, + isscheduled, schedule) end end @@ -330,8 +332,8 @@ function System(eqs::Vector{Equation}, iv, dvs, ps, brownians = []; is_dde = nothing, tstops = [], tearing_state = nothing, ignored_connections = nothing, parent = nothing, description = "", name = nothing, discover_from_metadata = true, - initializesystem = nothing, is_initializesystem = false, preface = [], - checks = true) + initializesystem = nothing, is_initializesystem = false, is_discrete = false, + preface = [], checks = true) name === nothing && throw(NoNameError()) if !isempty(parameter_dependencies) @warn """ @@ -411,7 +413,7 @@ function System(eqs::Vector{Equation}, iv, dvs, ps, brownians = []; var_to_name, name, description, defaults, guesses, systems, initialization_eqs, continuous_events, discrete_events, connector_type, assertions, metadata, gui_metadata, is_dde, tstops, tearing_state, true, false, nothing, ignored_connections, preface, parent, - initializesystem, is_initializesystem; checks) + initializesystem, is_initializesystem, is_discrete; checks) end """ @@ -668,7 +670,7 @@ callbacks, so checking if any LHS is shifted is sufficient. If a variable is shi the input equations there _will_ be a `Shift` equation in the simplified system. """ function is_discrete_system(sys::System) - any(eq -> isoperator(eq.lhs, Shift), equations(sys)) + get_is_discrete(sys) || any(eq -> isoperator(eq.lhs, Shift), equations(sys)) end SymbolicIndexingInterface.is_time_dependent(sys::System) = get_iv(sys) !== nothing From 60439b136d844b7f10ca329013cc01521300201b Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 6 Jun 2025 17:14:45 +0530 Subject: [PATCH 1999/2176] fix: explicitly mark callback systems as discrete --- src/systems/callbacks.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/systems/callbacks.jl b/src/systems/callbacks.jl index a9069e575c..a12dad9d72 100644 --- a/src/systems/callbacks.jl +++ b/src/systems/callbacks.jl @@ -259,7 +259,7 @@ function make_affect(affect::Vector{Equation}; discrete_parameters = Any[], @named affectsys = System( vcat(affect, alg_eqs), iv, collect(union(_dvs, discretes)), - collect(union(pre_params, sys_params))) + collect(union(pre_params, sys_params)); is_discrete = true) affectsys = mtkcompile(affectsys; fully_determined = nothing) # get accessed parameters p from Pre(p) in the callback parameters accessed_params = Vector{Any}(filter(isparameter, map(unPre, collect(pre_params)))) From d584eab3c823774da464752626419318c08321d1 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 6 Jun 2025 17:15:21 +0530 Subject: [PATCH 2000/2176] fix: check and update `is_discrete` flag in simplification --- src/systems/systemstructure.jl | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/systems/systemstructure.jl b/src/systems/systemstructure.jl index 8b0e020cc4..a2e021c66e 100644 --- a/src/systems/systemstructure.jl +++ b/src/systems/systemstructure.jl @@ -720,8 +720,13 @@ function mtkcompile!(state::TearingState; simplify = false, """)) end end - if continuous_id == 1 && any(Base.Fix2(isoperator, Shift), state.fullvars) + if get_is_discrete(state.sys) || + continuous_id == 1 && any(Base.Fix2(isoperator, Shift), state.fullvars) state.structure.only_discrete = true + state = shift_discrete_system(state) + sys = state.sys + @set! sys.is_discrete = true + state.sys = sys end sys = _mtkcompile!(state; simplify, check_consistency, From 57f96fc024480f4d9adbe140f835706d19e060af Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 6 Jun 2025 17:15:46 +0530 Subject: [PATCH 2001/2176] feat: add `descend_lower_shift_varname` and `_with_unit` variant --- src/structural_transformation/utils.jl | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/structural_transformation/utils.jl b/src/structural_transformation/utils.jl index 50f9aaf887..a5a8febd18 100644 --- a/src/structural_transformation/utils.jl +++ b/src/structural_transformation/utils.jl @@ -482,11 +482,29 @@ function lower_shift_varname(var, iv) end end +function descend_lower_shift_varname_with_unit(var, iv) + symbolic_type(var) == NotSymbolic() && return var + ModelingToolkit._with_unit(descend_lower_shift_varname, var, iv, iv) +end +function descend_lower_shift_varname(var, iv) + iscall(var) || return var + op = operation(var) + if op isa Shift + return shift2term(var) + else + args = arguments(var) + args = map(Base.Fix2(descend_lower_shift_varname, iv), args) + return maketerm(typeof(var), op, args, Symbolics.metadata(var)) + end +end + """ Rename a Shift variable with negative shift, Shift(t, k)(x(t)) to xₜ₋ₖ(t). """ function shift2term(var) + iscall(var) || return var op = operation(var) + op isa Shift || return var iv = op.t arg = only(arguments(var)) if operation(arg) === getindex From 224948df5b7215d8e884399eb9dc9ac3556a0e69 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 6 Jun 2025 17:16:04 +0530 Subject: [PATCH 2002/2176] feat: properly handle simplification of (implicit) discrete systems --- .../StructuralTransformations.jl | 3 +- .../symbolics_tearing.jl | 200 ++++++++++++++++-- 2 files changed, 190 insertions(+), 13 deletions(-) diff --git a/src/structural_transformation/StructuralTransformations.jl b/src/structural_transformation/StructuralTransformations.jl index eeb480e0f7..681025cb81 100644 --- a/src/structural_transformation/StructuralTransformations.jl +++ b/src/structural_transformation/StructuralTransformations.jl @@ -22,6 +22,7 @@ using ModelingToolkit: System, AbstractSystem, var_from_nested_derivative, Diffe ExtraEquationsSystemException, ExtraVariablesSystemException, vars!, invalidate_cache!, + vars!, invalidate_cache!, Shift, IncrementalCycleTracker, add_edge_checked!, topological_sort, filter_kwargs, lower_varname_with_unit, lower_shift_varname_with_unit, setio, SparseMatrixCLIL, @@ -39,7 +40,7 @@ using ModelingToolkit: algeqs, EquationsView, dervars_range, diffvars_range, algvars_range, DiffGraph, complete!, get_fullvars, system_subset -using SymbolicIndexingInterface: symbolic_type, ArraySymbolic +using SymbolicIndexingInterface: symbolic_type, ArraySymbolic, NotSymbolic using ModelingToolkit.DiffEqBase using ModelingToolkit.StaticArrays diff --git a/src/structural_transformation/symbolics_tearing.jl b/src/structural_transformation/symbolics_tearing.jl index 0014a947c8..c5c0757295 100644 --- a/src/structural_transformation/symbolics_tearing.jl +++ b/src/structural_transformation/symbolics_tearing.jl @@ -421,6 +421,23 @@ function generate_derivative_variables!( for (i, idxs) in idxs_to_remove deleteat!(var_sccs[i], idxs) end + new_sccs = insert_sccs(var_sccs, sccs_to_insert) + + if mm !== nothing + @set! mm.ncols = ndsts(graph) + end + + return new_sccs +end + +""" + $(TYPEDSIGNATURES) + +Given a list of SCCs and a list of SCCs to insert at specific indices, insert them and +return the new SCC vector. +""" +function insert_sccs( + var_sccs::Vector{Vector{Int}}, sccs_to_insert::Vector{Tuple{Int, Vector{Int}}}) # insert the new SCCs, accounting for the fact that we might have multiple entries # in `sccs_to_insert` to be inserted at the same index. old_idx = 1 @@ -441,10 +458,6 @@ function generate_derivative_variables!( end filter!(!isempty, new_sccs) - if mm !== nothing - @set! mm.ncols = ndsts(graph) - end - return new_sccs end @@ -742,7 +755,17 @@ function codegen_equation!(eg::EquationGenerator, @unpack fullvars, sys, structure = state @unpack solvable_graph, var_to_diff, eq_to_diff, graph = structure diff_to_var = invview(var_to_diff) - if is_solvable(eg, ieq, iv) && is_dervar(eg, iv) + + issolvable = is_solvable(eg, ieq, iv) + isdervar = issolvable && is_dervar(eg, iv) + isdisc = is_only_discrete(structure) + # The variable is derivative variable and the "most differentiated" + # This is only used for discrete systems, and basically refers to + # `Shift(t, 1)(x(k))` in `Shift(t, 1)(x(k)) ~ x(k) + x(k-1)`. As illustrated in + # the docstring for `add_additional_history!`, this is an exception and needs to be + # treated like a solved equation rather than a differential equation. + is_highest_diff = iv isa Int && isdervar && var_to_diff[iv] === nothing + if issolvable && isdervar && (!isdisc || !is_highest_diff) var = fullvars[iv] isnothing(D) && throw(UnexpectedDifferentialError(equations(sys)[ieq])) order, lv = var_order(iv, diff_to_var) @@ -762,15 +785,25 @@ function codegen_equation!(eg::EquationGenerator, push!(neweqs′, neweq) push!(eq_ordering, ieq) push!(var_ordering, diff_to_var[iv]) - elseif is_solvable(eg, ieq, iv) + elseif issolvable var = fullvars[iv] neweq = make_solved_equation(var, eq, total_sub; simplify) if neweq !== nothing + # backshift solved equations to calculate the value of the variable at the + # current time. This works because we added one additional history element + # in `add_additional_history!`. + if isdisc + neweq = backshift_expr(neweq, idep) + end push!(solved_eqs, neweq) push!(solved_vars, iv) end else neweq = make_algebraic_equation(eq, total_sub) + # For the same reason as solved equations (they are effectively the same) + if isdisc + neweq = backshift_expr(neweq, idep) + end push!(neweqs′, neweq) push!(eq_ordering, ieq) # we push a dummy to `var_ordering` here because `iv` is `unassigned` @@ -896,9 +929,24 @@ Update the system equations, unknowns, and observables after simplification. """ function update_simplified_system!( state::TearingState, neweqs, solved_eqs, dummy_sub, var_sccs, extra_unknowns; - cse_hack = true, array_hack = true) - @unpack solvable_graph, var_to_diff, eq_to_diff, graph = state.structure + cse_hack = true, array_hack = true, D = nothing, iv = nothing) + @unpack fullvars, structure = state + @unpack solvable_graph, var_to_diff, eq_to_diff, graph = structure diff_to_var = invview(var_to_diff) + # Since we solved the highest order derivative varible in discrete systems, + # we make a list of the solved variables and avoid including them in the + # unknowns. + solved_vars = Set() + if is_only_discrete(structure) + for eq in solved_eqs + var = eq.lhs + if isequal(eq.lhs, eq.rhs) + var = lower_shift_varname_with_unit(D(eq.lhs), iv) + end + push!(solved_vars, var) + end + filter!(eq -> !isequal(eq.lhs, eq.rhs), solved_eqs) + end ispresent = let var_to_diff = var_to_diff, graph = graph i -> (!isempty(𝑑neighbors(graph, i)) || @@ -915,9 +963,19 @@ function update_simplified_system!( obs = [fast_substitute(observed(sys), obs_sub); solved_eqs] unknown_idxs = filter( - i -> diff_to_var[i] === nothing && ispresent(i), eachindex(state.fullvars)) + i -> diff_to_var[i] === nothing && ispresent(i) && !(fullvars[i] in solved_vars), eachindex(state.fullvars)) unknowns = state.fullvars[unknown_idxs] unknowns = [unknowns; extra_unknowns] + if is_only_discrete(structure) + # Algebraic variables are shifted forward by one, so we backshift them. + unknowns = map(enumerate(unknowns)) do (i, var) + if iscall(var) && operation(var) isa Shift && operation(var).steps == 1 + backshift_expr(var, iv) + else + var + end + end + end @set! sys.unknowns = unknowns obs = cse_and_array_hacks( @@ -979,7 +1037,8 @@ differential variables. function tearing_reassemble(state::TearingState, var_eq_matching::Matching, full_var_eq_matching::Matching, var_sccs::Vector{Vector{Int}}; simplify = false, mm, cse_hack = true, array_hack = true, fully_determined = true) - extra_eqs_vars = get_extra_eqs_vars(state, full_var_eq_matching, fully_determined) + extra_eqs_vars = get_extra_eqs_vars( + state, var_eq_matching, full_var_eq_matching, fully_determined) neweqs = collect(equations(state)) dummy_sub = Dict() @@ -995,6 +1054,11 @@ function tearing_reassemble(state::TearingState, var_eq_matching::Matching, end extra_unknowns = state.fullvars[extra_eqs_vars[2]] + if is_only_discrete(state.structure) + var_sccs = add_additional_history!( + state, neweqs, var_eq_matching, full_var_eq_matching, var_sccs; iv, D) + end + # Structural simplification substitute_derivatives_algevars!(state, neweqs, var_eq_matching, dummy_sub; iv, D) @@ -1010,13 +1074,121 @@ function tearing_reassemble(state::TearingState, var_eq_matching::Matching, # var_eq_matching and full_var_eq_matching are now invalidated sys = update_simplified_system!(state, neweqs, solved_eqs, dummy_sub, var_sccs, - extra_unknowns; cse_hack, array_hack) + extra_unknowns; cse_hack, array_hack, iv, D) @set! state.sys = sys @set! sys.tearing_state = state return invalidate_cache!(sys) end +""" + $(TYPEDSIGNATURES) + +Add one more history equation for discrete systems. For example, if we have + +```julia +Shift(t, 1)(x(k-1)) ~ x(k) +Shift(t, 1)(x(k)) ~ x(k) + x(k-1) +``` + +This turns it into + +```julia +Shift(t, 1)(x(k-2)) ~ x(k-1) +Shift(t, 1)(x(k-1)) ~ x(k) +Shift(t, 1)(x(k)) ~ x(k) + x(k-1) +``` + +Thus adding an additional unknown as well. Later, the highest derivative equation will +be backshifted by one and turned into an observed equation, resulting in: + +```julia +Shift(t, 1)(x(k-2)) ~ x(k-1) +Shift(t, 1)(x(k-1)) ~ x(k) + +x(k) ~ x(k-1) + x(k-2) +``` + +Where the last equation is the observed equation. +""" +function add_additional_history!( + state::TearingState, neweqs::Vector, var_eq_matching::Matching, + full_var_eq_matching::Matching, var_sccs::Vector{Vector{Int}}; iv, D) + @unpack fullvars, sys, structure = state + @unpack solvable_graph, var_to_diff, eq_to_diff, graph = structure + eq_var_matching = invview(var_eq_matching) + diff_to_var = invview(var_to_diff) + is_discrete = is_only_discrete(structure) + digraph = DiCMOBiGraph{false}(graph, var_eq_matching) + + # We need the inverse mapping of `var_sccs` to update it efficiently later. + v_to_scc = Vector{NTuple{2, Int}}(undef, ndsts(graph)) + for (i, scc) in enumerate(var_sccs), (j, v) in enumerate(scc) + v_to_scc[v] = (i, j) + end + + vars_to_backshift = BitSet() + eqs_to_backshift = BitSet() + # add history for differential variables + for ivar in 1:length(fullvars) + ieq = var_eq_matching[ivar] + # the variable to backshift is a state variable which is not the + # derivative of any other one. + ieq isa SelectedState || continue + diff_to_var[ivar] === nothing || continue + push!(vars_to_backshift, ivar) + end + + inserts = Tuple{Int, Vector{Int}}[] + + for var in vars_to_backshift + add_backshifted_var!(state, var, iv) + # all backshifted vars are differential vars, hence SelectedState + push!(var_eq_matching, SelectedState()) + push!(full_var_eq_matching, unassigned) + # add to the SCCs right before the variable that was backshifted + push!(inserts, (v_to_scc[var][1], [length(fullvars)])) + end + + sort!(inserts, by = first) + new_sccs = insert_sccs(var_sccs, inserts) + return new_sccs +end + +""" + $(TYPEDSIGNATURES) + +Add the backshifted version of variable `ivar` to the system. +""" +function add_backshifted_var!(state::TearingState, ivar::Int, iv) + @unpack fullvars, structure = state + @unpack var_to_diff, graph, solvable_graph = structure + + var = fullvars[ivar] + newvar = simplify_shifts(Shift(iv, -1)(var)) + push!(fullvars, newvar) + inewvar = add_vertex!(var_to_diff) + add_edge!(var_to_diff, inewvar, ivar) + add_vertex!(graph, DST) + add_vertex!(solvable_graph, DST) + return inewvar +end + +""" + $(TYPEDSIGNATURES) + +Backshift the given expression `ex`. +""" +function backshift_expr(ex, iv) + ex isa Symbolic || return ex + return descend_lower_shift_varname_with_unit( + simplify_shifts(distribute_shift(Shift(iv, -1)(ex))), iv) +end + +function backshift_expr(ex::Equation, iv) + return backshift_expr(ex.lhs, iv) ~ backshift_expr(ex.rhs, iv) +end + """ $(TYPEDSIGNATURES) @@ -1025,7 +1197,7 @@ respectively. For fully-determined systems, both of these are empty. Overdetermi have extra equations, and underdetermined systems have extra variables. """ function get_extra_eqs_vars( - state::TearingState, full_var_eq_matching::Matching, fully_determined::Bool) + state::TearingState, var_eq_matching::Matching, full_var_eq_matching::Matching, fully_determined::Bool) fully_determined && return Int[], Int[] extra_eqs = Int[] @@ -1035,6 +1207,10 @@ function get_extra_eqs_vars( for v in 𝑑vertices(state.structure.graph) eq = full_var_eq_matching[v] eq isa Int && continue + # Only if the variable is also unmatched in `var_eq_matching`. + # Otherwise, `SelectedState` differential variables from order lowering + # are also considered "extra" + var_eq_matching[v] === unassigned || continue push!(extra_vars, v) end for eq in 𝑠vertices(state.structure.graph) From 190fa108bc47e4a3182c8c85ef8b115122d5f762 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 6 Jun 2025 17:16:20 +0530 Subject: [PATCH 2003/2176] fix: fix codegen of implicit discrete systems --- src/systems/codegen.jl | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/systems/codegen.jl b/src/systems/codegen.jl index 2a5bc1a728..17ff652c41 100644 --- a/src/systems/codegen.jl +++ b/src/systems/codegen.jl @@ -54,13 +54,16 @@ function generate_rhs(sys::System; implicit_dae = false, # Handle observables in algebraic equations, since they are shifted shifted_obs = Equation[distribute_shift(D(eq)) for eq in obs] obsidxs = observed_equations_used_by(sys, rhss; obs = shifted_obs) - extra_assignments = [Assignment(shifted_obs[i].lhs, shifted_obs[i].rhs) - for i in obsidxs] + ddvs = map(D, dvs) + + append!(extra_assignments, + [Assignment(shifted_obs[i].lhs, shifted_obs[i].rhs) + for i in obsidxs]) else D = Differential(t) + ddvs = map(D, dvs) rhss = [_iszero(eq.lhs) ? eq.rhs : eq.rhs - eq.lhs for eq in eqs] end - ddvs = map(D, dvs) else if !override_discrete && !is_discrete_system(sys) check_operator_variables(eqs, Differential) From 44f71a8cc4247d0a04e74eab232740892c3a53e3 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 6 Jun 2025 18:30:22 +0530 Subject: [PATCH 2004/2176] fix: fix toterm handling in `DiscreteProblem` --- src/problems/discreteproblem.jl | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/problems/discreteproblem.jl b/src/problems/discreteproblem.jl index 8e1abc46e4..c4ab069ebe 100644 --- a/src/problems/discreteproblem.jl +++ b/src/problems/discreteproblem.jl @@ -43,11 +43,11 @@ end check_compatibility && check_compatible_system(DiscreteProblem, sys) dvs = unknowns(sys) - u0map = to_varmap(op, dvs) - add_toterms!(u0map; replace = true) + op = to_varmap(op, dvs) + add_toterms!(op; replace = true) f, u0, p = process_SciMLProblem(DiscreteFunction{iip, spec}, sys, op; t = tspan !== nothing ? tspan[1] : tspan, check_compatibility, expression, - build_initializeprob = false, kwargs...) + kwargs...) if expression == Val{true} u0 = :(f($u0, p, tspan[1])) From 9b69e0d02cf711960c9ed18cb42fddd0cc148d3a Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 6 Jun 2025 18:31:15 +0530 Subject: [PATCH 2005/2176] fix: handle scalarized array symbolics in `distribute_shift` --- src/structural_transformation/utils.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/structural_transformation/utils.jl b/src/structural_transformation/utils.jl index a5a8febd18..c462013923 100644 --- a/src/structural_transformation/utils.jl +++ b/src/structural_transformation/utils.jl @@ -596,7 +596,7 @@ function _distribute_shift(expr, shift) (op isa Pre || op isa Initial) && return expr args = arguments(expr) - if ModelingToolkit.isvariable(expr) + if ModelingToolkit.isvariable(expr) && operation(expr) !== getindex (length(args) == 1 && isequal(shift.t, only(args))) ? (return shift(expr)) : (return expr) elseif op isa Shift From 8747976430354aeb2c93a6ae7dd03feccb9889e2 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 6 Jun 2025 18:31:32 +0530 Subject: [PATCH 2006/2176] fix: perform `toterm` in `Initial` for `Shift`ed variables --- src/systems/abstractsystem.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index fb69d8b702..941739f127 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -503,7 +503,7 @@ function (f::Initial)(x) return x end # differential variables are default-toterm-ed - if iscall(x) && operation(x) isa Differential + if iscall(x) && operation(x) isa Union{Differential, Shift} x = default_toterm(x) end # don't double wrap From 8140b6254c38219e2bd0ab1195c805818103c04d Mon Sep 17 00:00:00 2001 From: fchen121 Date: Fri, 6 Jun 2025 18:49:11 -0400 Subject: [PATCH 2007/2176] Combined change of variable function for ODE and SDE --- src/systems/diffeqs/basic_transformations.jl | 71 +++------ test/changeofvariables.jl | 148 +++++++++---------- 2 files changed, 92 insertions(+), 127 deletions(-) diff --git a/src/systems/diffeqs/basic_transformations.jl b/src/systems/diffeqs/basic_transformations.jl index 26131554ed..e28149819e 100644 --- a/src/systems/diffeqs/basic_transformations.jl +++ b/src/systems/diffeqs/basic_transformations.jl @@ -94,55 +94,13 @@ new_sol = solve(new_prob, Tsit5()) ``` """ -function changeofvariables(sys::System, iv, forward_subs, backward_subs; simplify=true, t0=missing) - t = iv - - old_vars = first.(backward_subs) - new_vars = last.(forward_subs) - # kept_vars = setdiff(states(sys), old_vars) - # rhs = [eq.rhs for eq in equations(sys)] - - # use: dz/dt = ∂z/∂x dx/dt + ∂z/∂t - dzdt = Symbolics.derivative( first.(forward_subs), t ) - new_eqs = Equation[] - for (new_var, ex) in zip(new_vars, dzdt) - for ode_eq in equations(sys) - ex = substitute(ex, ode_eq.lhs => ode_eq.rhs) - end - ex = substitute(ex, Dict(forward_subs)) - ex = substitute(ex, Dict(backward_subs)) - if simplify - ex = Symbolics.simplify(ex, expand=true) - end - push!(new_eqs, Differential(t)(new_var) ~ ex) - end - - defs = get_defaults(sys) - new_defs = Dict() - for f_sub in forward_subs - ex = substitute(first(f_sub), defs) - if !ismissing(t0) - ex = substitute(ex, t => t0) - end - new_defs[last(f_sub)] = ex - end - for para in parameters(sys) - if haskey(defs, para) - new_defs[para] = defs[para] - end - end - @named new_sys = System(vcat(new_eqs, first.(backward_subs) .~ last.(backward_subs)), t; - defaults=new_defs, - observed=observed(sys) - ) - if simplify - return mtkcompile(new_sys) +function changeofvariables( + sys::System, iv, forward_subs, backward_subs; + simplify=true, t0=missing, isSDE=false +) + if !iscomplete(sys) + sys = mtkcompile(sys) end - return new_sys -end - -function change_of_variable_SDE(sys::System, iv, forward_subs, backward_subs; simplify=true, t0=missing) - sys = mtkcompile(sys) t = iv old_vars = first.(backward_subs) @@ -152,10 +110,15 @@ function change_of_variable_SDE(sys::System, iv, forward_subs, backward_subs; si # use: dY = (∂f/∂t + μ∂f/∂x + (1/2)*σ^2*∂2f/∂x2)dt + σ∂f/∂xdW old_eqs = equations(sys) neqs = get_noise_eqs(sys) - neqs = [neqs[i,:] for i in 1:size(neqs,1)] + if neqs !== nothing + isSDE = true + neqs = [neqs[i,:] for i in 1:size(neqs,1)] - brownvars = map([Symbol(:B, :_, i) for i in 1:length(neqs[1])]) do name - unwrap(only(@brownian $name)) + brownvars = map([Symbol(:B, :_, i) for i in 1:length(neqs[1])]) do name + unwrap(only(@brownian $name)) + end + else + neqs = ones(1, length(old_eqs)) end # df/dt = ∂f/∂x dx/dt + ∂f/∂t @@ -168,8 +131,10 @@ function change_of_variable_SDE(sys::System, iv, forward_subs, backward_subs; si for (eqs, neq) in zip(old_eqs, neqs) if occursin(value(eqs.lhs), value(ex)) ex = substitute(ex, eqs.lhs => eqs.rhs) - for (noise, B) in zip(neq, brownvars) - ex = ex + 1/2 * noise^2 * second + noise*first*B + if isSDE + for (noise, B) in zip(neq, brownvars) + ex = ex + 1/2 * noise^2 * second + noise*first*B + end end end end diff --git a/test/changeofvariables.jl b/test/changeofvariables.jl index 68a8a143b6..1c0204e8fd 100644 --- a/test/changeofvariables.jl +++ b/test/changeofvariables.jl @@ -4,101 +4,101 @@ using Test, LinearAlgebra # Change of variables: z = log(x) # (this implies that x = exp(z) is automatically non-negative) -# @independent_variables t -# @variables z(t)[1:2, 1:2] -# D = Differential(t) -# eqs = [D(D(z)) ~ ones(2, 2)] -# @mtkcompile sys = System(eqs, t) -# @test_nowarn ODEProblem(sys, [z => zeros(2, 2), D(z) => ones(2, 2)], (0.0, 10.0)) +@independent_variables t +@variables z(t)[1:2, 1:2] +D = Differential(t) +eqs = [D(D(z)) ~ ones(2, 2)] +@mtkcompile sys = System(eqs, t) +@test_nowarn ODEProblem(sys, [z => zeros(2, 2), D(z) => ones(2, 2)], (0.0, 10.0)) -# @parameters α -# @variables x(t) -# D = Differential(t) -# eqs = [D(x) ~ α*x] +@parameters α +@variables x(t) +D = Differential(t) +eqs = [D(x) ~ α*x] -# tspan = (0., 1.) -# def = [x => 1.0, α => -0.5] +tspan = (0., 1.) +def = [x => 1.0, α => -0.5] -# @mtkcompile sys = System(eqs, t;defaults=def) -# prob = ODEProblem(sys, [], tspan) -# sol = solve(prob, Tsit5()) +@mtkcompile sys = System(eqs, t;defaults=def) +prob = ODEProblem(sys, [], tspan) +sol = solve(prob, Tsit5()) -# @variables z(t) -# forward_subs = [log(x) => z] -# backward_subs = [x => exp(z)] -# new_sys = changeofvariables(sys, t, forward_subs, backward_subs) -# @test equations(new_sys)[1] == (D(z) ~ α) +@variables z(t) +forward_subs = [log(x) => z] +backward_subs = [x => exp(z)] +new_sys = changeofvariables(sys, t, forward_subs, backward_subs) +@test equations(new_sys)[1] == (D(z) ~ α) -# new_prob = ODEProblem(new_sys, [], tspan) -# new_sol = solve(new_prob, Tsit5()) +new_prob = ODEProblem(new_sys, [], tspan) +new_sol = solve(new_prob, Tsit5()) -# @test isapprox(new_sol[x][end], sol[x][end], atol=1e-4) +@test isapprox(new_sol[x][end], sol[x][end], atol=1e-4) -# # Riccati equation -# @parameters α -# @variables x(t) -# D = Differential(t) -# eqs = [D(x) ~ t^2 + α - x^2] -# def = [x=>1., α => 1.] -# @mtkcompile sys = System(eqs, t; defaults=def) +# Riccati equation +@parameters α +@variables x(t) +D = Differential(t) +eqs = [D(x) ~ t^2 + α - x^2] +def = [x=>1., α => 1.] +@mtkcompile sys = System(eqs, t; defaults=def) -# @variables z(t) -# forward_subs = [t + α/(x+t) => z ] -# backward_subs = [ x => α/(z-t) - t] +@variables z(t) +forward_subs = [t + α/(x+t) => z ] +backward_subs = [ x => α/(z-t) - t] -# new_sys = changeofvariables(sys, t, forward_subs, backward_subs; simplify=true, t0=0.) -# # output should be equivalent to -# # t^2 + α - z^2 + 2 (but this simplification is not found automatically) +new_sys = changeofvariables(sys, t, forward_subs, backward_subs; simplify=true, t0=0.) +# output should be equivalent to +# t^2 + α - z^2 + 2 (but this simplification is not found automatically) -# tspan = (0., 1.) -# prob = ODEProblem(sys,[],tspan) -# new_prob = ODEProblem(new_sys,[],tspan) +tspan = (0., 1.) +prob = ODEProblem(sys,[],tspan) +new_prob = ODEProblem(new_sys,[],tspan) -# sol = solve(prob, Tsit5()) -# new_sol = solve(new_prob, Tsit5()) +sol = solve(prob, Tsit5()) +new_sol = solve(new_prob, Tsit5()) -# @test isapprox(sol[x][end], new_sol[x][end], rtol=1e-4) +@test isapprox(sol[x][end], new_sol[x][end], rtol=1e-4) -# # Linear transformation to diagonal system -# @independent_variables t -# @variables x(t)[1:3] -# x = reshape(x, 3, 1) -# D = Differential(t) -# A = [0. -1. 0.; -0.5 0.5 0.; 0. 0. -1.] -# right = A*x -# eqs = vec(D.(x) .~ right) +# Linear transformation to diagonal system +@independent_variables t +@variables x(t)[1:3] +x = reshape(x, 3, 1) +D = Differential(t) +A = [0. -1. 0.; -0.5 0.5 0.; 0. 0. -1.] +right = A*x +eqs = vec(D.(x) .~ right) -# tspan = (0., 10.) -# u0 = [x[1] => 1.0, x[2] => 2.0, x[3] => -1.0] +tspan = (0., 10.) +u0 = [x[1] => 1.0, x[2] => 2.0, x[3] => -1.0] -# @mtkcompile sys = System(eqs, t; defaults=u0) -# prob = ODEProblem(sys,[],tspan) -# sol = solve(prob, Tsit5()) +@mtkcompile sys = System(eqs, t; defaults=u0) +prob = ODEProblem(sys,[],tspan) +sol = solve(prob, Tsit5()) -# T = eigen(A).vectors -# T_inv = inv(T) +T = eigen(A).vectors +T_inv = inv(T) -# @variables z(t)[1:3] -# z = reshape(z, 3, 1) -# forward_subs = vec(T_inv*x .=> z) -# backward_subs = vec(x .=> T*z) +@variables z(t)[1:3] +z = reshape(z, 3, 1) +forward_subs = vec(T_inv*x .=> z) +backward_subs = vec(x .=> T*z) -# new_sys = changeofvariables(sys, t, forward_subs, backward_subs; simplify=true) +new_sys = changeofvariables(sys, t, forward_subs, backward_subs; simplify=true) -# new_prob = ODEProblem(new_sys, [], tspan) -# new_sol = solve(new_prob, Tsit5()) +new_prob = ODEProblem(new_sys, [], tspan) +new_sol = solve(new_prob, Tsit5()) -# # test RHS -# new_rhs = [eq.rhs for eq in equations(new_sys)] -# new_A = Symbolics.value.(Symbolics.jacobian(new_rhs, z)) -# A = diagm(eigen(A).values) -# A = sortslices(A, dims=1) -# new_A = sortslices(new_A, dims=1) -# @test isapprox(A, new_A, rtol = 1e-10) -# @test isapprox( new_sol[x[1],end], sol[x[1],end], rtol=1e-4) +# test RHS +new_rhs = [eq.rhs for eq in equations(new_sys)] +new_A = Symbolics.value.(Symbolics.jacobian(new_rhs, z)) +A = diagm(eigen(A).values) +A = sortslices(A, dims=1) +new_A = sortslices(new_A, dims=1) +@test isapprox(A, new_A, rtol = 1e-10) +@test isapprox( new_sol[x[1],end], sol[x[1],end], rtol=1e-4) # Change of variables for sde noise_eqs = ModelingToolkit.get_noise_eqs @@ -115,7 +115,7 @@ def = [x=>0., μ => 2., σ=>1.] @mtkcompile sys = System(eqs, t; defaults=def) forward_subs = [log(x) => y] backward_subs = [x => exp(y)] -new_sys = change_of_variable_SDE(sys, t, forward_subs, backward_subs) +new_sys = changeofvariables(sys, t, forward_subs, backward_subs) @test equations(new_sys)[1] == (D(y) ~ μ - 1/2*σ^2) @test noise_eqs(new_sys)[1] === value(σ) @@ -130,7 +130,7 @@ def = [x=>0., y=> 0., u=>0., μ => 2., σ=>1., α=>3.] @mtkcompile sys = System(eqs, t; defaults=def) forward_subs = [log(x) => z, y^2 => w, log(u) => v] backward_subs = [x => exp(z), y => w^.5, u => exp(v)] -new_sys = change_of_variable_SDE(sys, t, forward_subs, backward_subs) +new_sys = changeofvariables(sys, t, forward_subs, backward_subs) @test equations(new_sys)[1] == (D(z) ~ μ - 1/2*σ^2) @test equations(new_sys)[2] == (D(w) ~ α^2) @test equations(new_sys)[3] == (D(v) ~ μ - 1/2*(α^2 + σ^2)) From 0facf9b17a7ae1323b98a1f5a85b016ae0c46d9e Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 6 Jun 2025 18:31:49 +0530 Subject: [PATCH 2008/2176] test: update discrete system tests --- test/discrete_system.jl | 104 ++++++------------------------- test/implicit_discrete_system.jl | 6 +- 2 files changed, 22 insertions(+), 88 deletions(-) diff --git a/test/discrete_system.jl b/test/discrete_system.jl index ccebe5d346..d3db4df5f2 100644 --- a/test/discrete_system.jl +++ b/test/discrete_system.jl @@ -40,7 +40,6 @@ u = ModelingToolkit.varmap_to_vars( Dict([S(k - 1) => 1, I(k - 1) => 2, R(k - 1) => 3]), unknowns(syss)) p = MTKParameters(syss, [c, nsteps, δt, β, γ] .=> collect(1:5)) df.f(du, u, p, 0) -@test_broken getu(syss, [S, I, R]) reorderer = getu(syss, [S(k - 1), I(k - 1), R(k - 1)]) @test reorderer(du) ≈ [0.01831563888873422, 0.9816849729159067, 4.999999388195359] @@ -49,16 +48,17 @@ reorderer = getu(syss, [S(k - 1), I(k - 1), R(k - 1)]) [0.01831563888873422, 0.9816849729159067, 4.999999388195359] # Problem -u0 = [S(k - 1) => 990.0, I(k - 1) => 10.0, R(k - 1) => 0.0] +u0 = [S => 990.0, I => 10.0, R => 0.0] p = [β => 0.05, c => 10.0, γ => 0.25, δt => 0.1, nsteps => 400] tspan = (0.0, ModelingToolkit.value(substitute(nsteps, p))) # value function (from Symbolics) is used to convert a Num to Float64 -prob_map = DiscreteProblem(syss, [u0; p], tspan) +prob_map = DiscreteProblem( + syss, [u0; p], tspan; guesses = [S(k - 1) => 1.0, I(k - 1) => 1.0, R(k - 1) => 1.0]) @test prob_map.f.sys === syss # Solution using OrdinaryDiffEq sol_map = solve(prob_map, FunctionMap()); -@test_broken sol_map[S] isa Vector +@test sol_map[S] isa Vector @test sol_map[S(k - 1)] isa Vector # Using defaults constructor @@ -78,16 +78,16 @@ eqs2 = [S ~ S(k - 1) - infection2, eqs2, t, [S, I, R, R2], [c, nsteps, δt, β, γ]) @test ModelingToolkit.defaults(sys) != Dict() -@test_broken DiscreteProblem(sys, [], tspan) -prob_map2 = DiscreteProblem(sys, [S(k - 1) => S, I(k - 1) => I, R(k - 1) => R], tspan) +prob_map2 = DiscreteProblem(sys, [], tspan) +# prob_map2 = DiscreteProblem(sys, [S(k - 1) => S, I(k - 1) => I, R(k - 1) => R], tspan) sol_map2 = solve(prob_map2, FunctionMap()); @test sol_map.u ≈ sol_map2.u for p in parameters(sys) @test sol_map.prob.ps[p] ≈ sol_map2.prob.ps[p] end -@test sol_map2[R2][begin:(end - 1)] == sol_map2[R(k - 1)][(begin + 1):end] -@test_broken sol_map2[R2(k + 1)][begin:(end - 1)] == sol_map2[R][(begin + 1):end] +@test sol_map2[R2][begin:(end - 1)] == sol_map2[R(k - 1)][(begin + 1):end] == + sol_map2[R][begin:(end - 1)] # Direct Implementation function sir_map!(u_diff, u, p, t) @@ -103,14 +103,12 @@ function sir_map!(u_diff, u, p, t) end nothing end; -@test_broken prob_map2[[S, I, R]] -u0 = prob_map2[[S(k - 1), I(k - 1), R(k - 1)]]; +u0 = sol_map2[[S, I, R], 1]; p = [0.05, 10.0, 0.25, 0.1]; prob_map = DiscreteProblem(sir_map!, u0, tspan, p); sol_map2 = solve(prob_map, FunctionMap()); -@test_broken reduce(hcat, sol_map[[S, I, R]]) ≈ Array(sol_map2) -@test reduce(hcat, sol_map[[S(k - 1), I(k - 1), R(k - 1)]]) ≈ Array(sol_map2) +@test reduce(hcat, sol_map[[S, I, R]]) ≈ Array(sol_map2) # Delayed difference equation # @variables x(..) y(..) z(t) @@ -217,7 +215,7 @@ eqs = [u ~ 1 prob = DiscreteProblem(de, [x(k - 1) => 0.0], (0, 10)) sol = solve(prob, FunctionMap()) -@test reduce(vcat, sol.u) == 1:11 +@test sol[x] == 1:11 # Issue#2585 getdata(buffer, t) = buffer[mod1(Int(t), length(buffer))] @@ -251,77 +249,12 @@ end @test_nowarn @mtkcompile sys = System(; buffer = ones(10)) @testset "Passing `nothing` to `u0`" begin - @test_broken begin - @variables x(t) = 1 - k = ShiftIndex() - @mtkcompile sys = System([x(k) ~ x(k - 1) + 1], t) - prob = @test_nowarn DiscreteProblem(sys, nothing, (0.0, 1.0)) - @test_nowarn solve(prob, FunctionMap()) - end -end - -@testset "Initialization" begin - @test_broken begin - # test that default values apply to the entire history - @variables x(t) = 1.0 - @mtkcompile de = System([x ~ x(k - 1) + x(k - 2)], t) - prob = DiscreteProblem(de, [], (0, 10)) - @test prob[x] == 2.0 - @test prob[x(k - 1)] == 1.0 - - # must provide initial conditions for history - @test_throws ErrorException DiscreteProblem(de, [x => 2.0], (0, 10)) - @test_throws ErrorException DiscreteProblem(de, [x(k + 1) => 2.0], (0, 10)) - - # initial values only affect _that timestep_, not the entire history - prob = DiscreteProblem(de, [x(k - 1) => 2.0], (0, 10)) - @test prob[x] == 3.0 - @test prob[x(k - 1)] == 2.0 - @variables xₜ₋₁(t) - @test prob[xₜ₋₁] == 2.0 - - # Test initial assignment with lowered variable - prob = DiscreteProblem(de, [xₜ₋₁(k - 1) => 4.0], (0, 10)) - @test prob[x(k - 1)] == prob[xₜ₋₁] == 1.0 - @test prob[x] == 5.0 - - # Test missing initial throws error - @variables x(t) - @mtkcompile de = System([x ~ x(k - 1) + x(k - 2) * x(k - 3)], t) - @test_throws ErrorException prob=DiscreteProblem(de, [x(k - 3) => 2.0], (0, 10)) - @test_throws ErrorException prob=DiscreteProblem( - de, [x(k - 3) => 2.0, x(k - 1) => 3.0], (0, 10)) - - # Test non-assigned initials are given default value - @variables x(t) = 2.0 - @mtkcompile de = System([x ~ x(k - 1) + x(k - 2) * x(k - 3)], t) - prob = DiscreteProblem(de, [x(k - 3) => 12.0], (0, 10)) - @test prob[x] == 26.0 - @test prob[x(k - 1)] == 2.0 - @test prob[x(k - 2)] == 2.0 - - # Elaborate test - @variables xₜ₋₂(t) zₜ₋₁(t) z(t) - eqs = [x ~ x(k - 1) + z(k - 2), - z ~ x(k - 2) * x(k - 3) - z(k - 1)^2] - @mtkcompile de = System(eqs, t) - u0 = [x(k - 1) => 3, - xₜ₋₂(k - 1) => 4, - x(k - 2) => 1, - z(k - 1) => 5, - zₜ₋₁(k - 1) => 12] - prob = DiscreteProblem(de, u0, (0, 10)) - @test prob[x] == 15 - @test prob[z] == -21 - - import ModelingToolkit: shift2term - # unknowns(de) = xₜ₋₁, x, zₜ₋₁, xₜ₋₂, z - vars = sort(ModelingToolkit.value.(unknowns(de)); by = string) - @test isequal(shift2term(Shift(t, 1)(vars[2])), vars[1]) - @test isequal(shift2term(Shift(t, 1)(vars[3])), vars[2]) - @test isequal(shift2term(Shift(t, -1)(vars[4])), vars[5]) - @test isequal(shift2term(Shift(t, -2)(vars[1])), vars[3]) - end + @variables x(t) = 1 + k = ShiftIndex() + @mtkcompile sys = System([x(k) ~ x(k - 1) + 1], t) + prob = @test_nowarn DiscreteProblem(sys, nothing, (0.0, 1.0)) + sol = solve(prob, FunctionMap()) + @test SciMLBase.successful_retcode(sol) end @testset "Shifted array variables" begin @@ -339,6 +272,5 @@ end (0, 4)) @test all(isone, prob.u0) sol = solve(prob, FunctionMap()) - @test_broken sol[[x..., y...], end] - @test sol[[x(k - 1)..., y(k - 1)...], end] == 8ones(4) + @test sol[[x..., y...], end] == 8ones(4) end diff --git a/test/implicit_discrete_system.jl b/test/implicit_discrete_system.jl index bbfb045b1f..57c67116d1 100644 --- a/test/implicit_discrete_system.jl +++ b/test/implicit_discrete_system.jl @@ -1,4 +1,4 @@ -using ModelingToolkit, Test +using ModelingToolkit, SymbolicIndexingInterface, Test using ModelingToolkit: t_nounits as t using StableRNGs @@ -45,6 +45,8 @@ end 1 - (u_next[1] + u_next[2])^2 - u_next[3]^2] end + reorderer = getu(sys, [x(k - 2), x(k - 1), y]) + for _ in 1:10 u_next = rand(rng, 3) u = rand(rng, 3) @@ -73,6 +75,6 @@ end y(k) ~ x(k - 1) + x(k - 2), z(k) * x(k) ~ 3] @mtkcompile sys = System(eqs, t) - @test occursin("var\"Shift(t, 1)(z(t))\"", + @test occursin("var\"Shift(t, 1)(x(t))\"", string(ImplicitDiscreteFunction(sys; expression = Val{true}))) end From aaf7c579e1a1bbc44532b3910938eef59b14f471 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Sat, 7 Jun 2025 01:08:32 +0530 Subject: [PATCH 2009/2176] fix: do not shift inside `Pre` in `shift_discrete_system` --- src/systems/systemstructure.jl | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/systems/systemstructure.jl b/src/systems/systemstructure.jl index a2e021c66e..7e00f6ef95 100644 --- a/src/systems/systemstructure.jl +++ b/src/systems/systemstructure.jl @@ -544,21 +544,21 @@ function shift_discrete_system(ts::TearingState) discvars = OrderedSet() eqs = equations(sys) for eq in eqs - vars!(discvars, eq; op = Union{Sample, Hold}) + vars!(discvars, eq; op = Union{Sample, Hold, Pre}) end iv = get_iv(sys) discmap = Dict(k => StructuralTransformations.simplify_shifts(Shift(iv, 1)(k)) for k in discvars - if any(isequal(k), fullvars) && !isa(operation(k), Union{Sample, Hold})) + if any(isequal(k), fullvars) && !isa(operation(k), Union{Sample, Hold, Pre})) for i in eachindex(fullvars) fullvars[i] = StructuralTransformations.simplify_shifts(fast_substitute( - fullvars[i], discmap; operator = Union{Sample, Hold})) + fullvars[i], discmap; operator = Union{Sample, Hold, Pre})) end for i in eachindex(eqs) eqs[i] = StructuralTransformations.simplify_shifts(fast_substitute( - eqs[i], discmap; operator = Union{Sample, Hold})) + eqs[i], discmap; operator = Union{Sample, Hold, Pre})) end @set! ts.sys.eqs = eqs @set! ts.fullvars = fullvars From da27d6999bbe9530fb1f11574e754fe60136ddc0 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Sat, 7 Jun 2025 11:14:18 +0530 Subject: [PATCH 2010/2176] fix: do not `distribute_shift` inside `Sample` and `Hold` --- src/structural_transformation/utils.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/structural_transformation/utils.jl b/src/structural_transformation/utils.jl index c462013923..02a9763226 100644 --- a/src/structural_transformation/utils.jl +++ b/src/structural_transformation/utils.jl @@ -593,7 +593,7 @@ end function _distribute_shift(expr, shift) if iscall(expr) op = operation(expr) - (op isa Pre || op isa Initial) && return expr + (op isa Union{Pre, Initial, Sample, Hold}) && return expr args = arguments(expr) if ModelingToolkit.isvariable(expr) && operation(expr) !== getindex From adfc91cf43e4f67ceb87863ea7e5c2d7ce9d7e08 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Sat, 7 Jun 2025 11:14:24 +0530 Subject: [PATCH 2011/2176] test: update clock tests --- test/clock.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/clock.jl b/test/clock.jl index a50026b38f..7afd7572fb 100644 --- a/test/clock.jl +++ b/test/clock.jl @@ -74,8 +74,8 @@ sss = ModelingToolkit._mtkcompile!( d = Clock(dt) k = ShiftIndex(d) @test issetequal(observed(sss), - [yd(k + 1) ~ Sample(dt)(y); r(k + 1) ~ 1.0; - ud(k + 1) ~ kp * (r(k + 1) - yd(k + 1))]) + [yd ~ Sample(dt)(y); r ~ 1.0; + ud ~ kp * (r - yd)]) canonical_eqs = map(eqs) do eq if iscall(eq.lhs) && operation(eq.lhs) isa Differential From 80a25104b1c2548223f6304b739412f231aee114 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Sat, 7 Jun 2025 11:30:05 +0530 Subject: [PATCH 2012/2176] ci: fix benchmark script --- benchmark/benchmarks.jl | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/benchmark/benchmarks.jl b/benchmark/benchmarks.jl index dc7487b18c..861d7b0722 100644 --- a/benchmark/benchmarks.jl +++ b/benchmark/benchmarks.jl @@ -1,6 +1,8 @@ using ModelingToolkit, BenchmarkTools using ModelingToolkitStandardLibrary -using ModelingToolkitStandardLibrary.Thermal +using ModelingToolkitStandardLibrary.Electrical +using ModelingToolkitStandardLibrary.Mechanical.Rotational +using ModelingToolkitStandardLibrary.Blocks using OrdinaryDiffEqDefault const SUITE = BenchmarkGroup() @@ -51,4 +53,4 @@ tspan = (0.0, 6.0) SUITE["ODEProblem"] = @benchmarkable ODEProblem($model, $u0, $tspan) prob = ODEProblem(model, u0, tspan) -SUITE["init"] = init($prob) +SUITE["init"] = @benchmarkable init($prob) From fea5333cf493a23cac23f5910b35cce5fd894d74 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Sat, 7 Jun 2025 11:32:35 +0530 Subject: [PATCH 2013/2176] ci: don't run tests when benchmarks change --- .github/workflows/Downstream.yml | 1 + .github/workflows/ReleaseTest.yml | 1 + .github/workflows/Tests.yml | 1 + 3 files changed, 3 insertions(+) diff --git a/.github/workflows/Downstream.yml b/.github/workflows/Downstream.yml index 6d79ee746a..7c536bbead 100644 --- a/.github/workflows/Downstream.yml +++ b/.github/workflows/Downstream.yml @@ -6,6 +6,7 @@ on: pull_request: paths-ignore: - 'docs/**' + - 'benchmark/**' concurrency: # Skip intermediate builds: always, but for the master branch and tags. diff --git a/.github/workflows/ReleaseTest.yml b/.github/workflows/ReleaseTest.yml index f8b592e9d0..a9e1ee1821 100644 --- a/.github/workflows/ReleaseTest.yml +++ b/.github/workflows/ReleaseTest.yml @@ -6,6 +6,7 @@ on: pull_request: paths-ignore: - 'docs/**' + - 'benchmark/**' concurrency: # Skip intermediate builds: always, but for the master branch and tags. diff --git a/.github/workflows/Tests.yml b/.github/workflows/Tests.yml index a62f7f272d..5b2bca6289 100644 --- a/.github/workflows/Tests.yml +++ b/.github/workflows/Tests.yml @@ -13,6 +13,7 @@ on: - master paths-ignore: - 'docs/**' + - 'benchmark/**' concurrency: # Skip intermediate builds: always, but for the master branch. From 5eae6a2c8dfb4b75899dadb4f3e7674c60ecf1e5 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Sat, 7 Jun 2025 12:53:08 +0530 Subject: [PATCH 2014/2176] ci: fix benchmark CI script --- .github/workflows/benchmark.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/benchmark.yml b/.github/workflows/benchmark.yml index c025bf4907..6fa719dcdb 100644 --- a/.github/workflows/benchmark.yml +++ b/.github/workflows/benchmark.yml @@ -22,3 +22,5 @@ jobs: - uses: MilesCranmer/AirspeedVelocity.jl@action-v1 with: julia-version: "${{ matrix.version }}" + script: "benchmark/benchmarks.jl" + extra-pkgs: "ModelingToolkitStandardLibrary,OrdinaryDiffEqDefault" From 70902a6849ab076ca8f5732132a146dea8138527 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Sat, 7 Jun 2025 12:55:26 +0530 Subject: [PATCH 2015/2176] ci: remove redundant `benchmark/Project.toml` --- benchmark/Project.toml | 3 --- 1 file changed, 3 deletions(-) delete mode 100644 benchmark/Project.toml diff --git a/benchmark/Project.toml b/benchmark/Project.toml deleted file mode 100644 index 666ceb7abb..0000000000 --- a/benchmark/Project.toml +++ /dev/null @@ -1,3 +0,0 @@ -[deps] -ModelingToolkitStandardLibrary = "16a59e39-deab-5bd0-87e4-056b12336739" -OrdinaryDiffEqDefault = "50262376-6c5a-4cf5-baba-aaf4f84d72d7" From e56493d71c855e532d0bb47191c5a703521226a5 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Sat, 7 Jun 2025 13:42:47 +0530 Subject: [PATCH 2016/2176] fix: properly retain defaults in `generate_initializesystem` --- src/systems/nonlinear/initializesystem.jl | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/systems/nonlinear/initializesystem.jl b/src/systems/nonlinear/initializesystem.jl index b1ca187e95..f6fdd6eeda 100644 --- a/src/systems/nonlinear/initializesystem.jl +++ b/src/systems/nonlinear/initializesystem.jl @@ -167,6 +167,8 @@ function generate_initializesystem_timevarying(sys::AbstractSystem; for k in keys(defs) defs[k] = substitute(defs[k], paramsubs) end + initials = Dict(k => v for (k, v) in pmap if isinitial(k)) + merge!(defs, initials) isys = System(Vector{Equation}(eqs_ics), vars, pars; @@ -280,6 +282,8 @@ function generate_initializesystem_timeindependent(sys::AbstractSystem; for k in keys(defs) defs[k] = substitute(defs[k], paramsubs) end + initials = Dict(k => v for (k, v) in pmap if isinitial(k)) + merge!(defs, initials) isys = System(Vector{Equation}(eqs_ics), vars, pars; From c58b67fc7b9ca0939d7ab472cd77fcee75d097ab Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 5 Jun 2025 12:27:37 +0530 Subject: [PATCH 2017/2176] refactor: remove CSE hack --- .../symbolics_tearing.jl | 97 +++---------------- src/systems/nonlinear/initializesystem.jl | 14 --- test/code_generation.jl | 31 ++++++ test/structural_transformation/utils.jl | 73 +------------- 4 files changed, 47 insertions(+), 168 deletions(-) diff --git a/src/structural_transformation/symbolics_tearing.jl b/src/structural_transformation/symbolics_tearing.jl index c5c0757295..af0973f1dc 100644 --- a/src/structural_transformation/symbolics_tearing.jl +++ b/src/structural_transformation/symbolics_tearing.jl @@ -929,7 +929,7 @@ Update the system equations, unknowns, and observables after simplification. """ function update_simplified_system!( state::TearingState, neweqs, solved_eqs, dummy_sub, var_sccs, extra_unknowns; - cse_hack = true, array_hack = true, D = nothing, iv = nothing) + array_hack = true, D = nothing, iv = nothing) @unpack fullvars, structure = state @unpack solvable_graph, var_to_diff, eq_to_diff, graph = structure diff_to_var = invview(var_to_diff) @@ -978,8 +978,7 @@ function update_simplified_system!( end @set! sys.unknowns = unknowns - obs = cse_and_array_hacks( - sys, obs, unknowns, neweqs; cse = cse_hack, array = array_hack) + obs = tearing_hacks(sys, obs, unknowns, neweqs; array = array_hack) @set! sys.eqs = neweqs @set! sys.observed = obs @@ -1035,7 +1034,7 @@ differential variables. according to `full_var_eq_matching`. """ function tearing_reassemble(state::TearingState, var_eq_matching::Matching, - full_var_eq_matching::Matching, var_sccs::Vector{Vector{Int}}; simplify = false, mm, cse_hack = true, + full_var_eq_matching::Matching, var_sccs::Vector{Vector{Int}}; simplify = false, mm, array_hack = true, fully_determined = true) extra_eqs_vars = get_extra_eqs_vars( state, var_eq_matching, full_var_eq_matching, fully_determined) @@ -1074,7 +1073,7 @@ function tearing_reassemble(state::TearingState, var_eq_matching::Matching, # var_eq_matching and full_var_eq_matching are now invalidated sys = update_simplified_system!(state, neweqs, solved_eqs, dummy_sub, var_sccs, - extra_unknowns; cse_hack, array_hack, iv, D) + extra_unknowns; array_hack, iv, D) @set! state.sys = sys @set! sys.tearing_state = state @@ -1223,14 +1222,7 @@ function get_extra_eqs_vars( end """ -# HACK 1 - -Since we don't support array equations, any equation of the sort `x[1:n] ~ f(...)[1:n]` -gets turned into `x[1] ~ f(...)[1], x[2] ~ f(...)[2]`. Repeatedly calling `f` gets -_very_ expensive. this hack performs a limited form of CSE specifically for this case to -avoid the unnecessary cost. This and the below hack are implemented simultaneously - -# HACK 2 +# HACK Add equations for array observed variables. If `p[i] ~ (...)` are equations, add an equation `p ~ [p[1], p[2], ...]` allow topsort to reorder them only add the new equation @@ -1238,13 +1230,7 @@ if all `p[i]` are present and the unscalarized form is used in any equation (obs not) we first count the number of times the scalarized form of each observed variable occurs in observed equations (and unknowns if it's split). """ -function cse_and_array_hacks(sys, obs, unknowns, neweqs; cse = true, array = true) - # HACK 1 - # mapping of rhs to temporary CSE variable - # `f(...) => tmpvar` in above example - rhs_to_tempvar = Dict() - - # HACK 2 +function tearing_hacks(sys, obs, unknowns, neweqs; array = true) # map of array observed variable (unscalarized) to number of its # scalarized terms that appear in observed equations arr_obs_occurrences = Dict() @@ -1252,31 +1238,6 @@ function cse_and_array_hacks(sys, obs, unknowns, neweqs; cse = true, array = tru lhs = eq.lhs rhs = eq.rhs - # HACK 1 - if cse && is_getindexed_array(rhs) - rhs_arr = arguments(rhs)[1] - iscall(rhs_arr) && operation(rhs_arr) isa Symbolics.Operator && continue - if !haskey(rhs_to_tempvar, rhs_arr) - tempvar = gensym(Symbol(lhs)) - N = length(rhs_arr) - tempvar = unwrap(Symbolics.variable( - tempvar; T = Symbolics.symtype(rhs_arr))) - tempvar = setmetadata( - tempvar, Symbolics.ArrayShapeCtx, Symbolics.shape(rhs_arr)) - tempeq = tempvar ~ rhs_arr - rhs_to_tempvar[rhs_arr] = tempvar - push!(obs, tempeq) - end - - # getindex_wrapper is used because `observed2graph` treats `x` and `x[i]` as different, - # so it doesn't find a dependency between this equation and `tempvar ~ rhs_arr` - # which fails the topological sort - neweq = lhs ~ getindex_wrapper( - rhs_to_tempvar[rhs_arr], Tuple(arguments(rhs)[2:end])) - obs[i] = neweq - end - # end HACK 1 - array || continue iscall(lhs) || continue operation(lhs) === getindex || continue @@ -1287,31 +1248,6 @@ function cse_and_array_hacks(sys, obs, unknowns, neweqs; cse = true, array = tru continue end - # Also do CSE for `equations(sys)` - if cse - for (i, eq) in enumerate(neweqs) - (; lhs, rhs) = eq - is_getindexed_array(rhs) || continue - rhs_arr = arguments(rhs)[1] - if !haskey(rhs_to_tempvar, rhs_arr) - tempvar = gensym(Symbol(lhs)) - N = length(rhs_arr) - tempvar = unwrap(Symbolics.variable( - tempvar; T = Symbolics.symtype(rhs_arr))) - tempvar = setmetadata( - tempvar, Symbolics.ArrayShapeCtx, Symbolics.shape(rhs_arr)) - tempeq = tempvar ~ rhs_arr - rhs_to_tempvar[rhs_arr] = tempvar - push!(obs, tempeq) - end - # don't need getindex_wrapper, but do it anyway to know that this - # hack took place - neweq = lhs ~ getindex_wrapper( - rhs_to_tempvar[rhs_arr], Tuple(arguments(rhs)[2:end])) - neweqs[i] = neweq - end - end - # count variables in unknowns if they are scalarized forms of variables # also present as observed. e.g. if `x[1]` is an unknown and `x[2] ~ (..)` # is an observed equation. @@ -1346,18 +1282,7 @@ function cse_and_array_hacks(sys, obs, unknowns, neweqs; cse = true, array = tru return obs end -function is_getindexed_array(rhs) - (!ModelingToolkit.isvariable(rhs) || ModelingToolkit.iscalledparameter(rhs)) && - iscall(rhs) && operation(rhs) === getindex && - Symbolics.shape(rhs) != Symbolics.Unknown() -end - -# PART OF HACK 1 -getindex_wrapper(x, i) = x[i...] - -@register_symbolic getindex_wrapper(x::AbstractArray, i::Tuple{Vararg{Int}}) - -# PART OF HACK 2 +# PART OF HACK function change_origin(origin, arr) if all(isone, Tuple(origin)) return arr @@ -1385,11 +1310,11 @@ new residual equations after tearing. End users are encouraged to call [`mtkcomp instead, which calls this function internally. """ function tearing(sys::AbstractSystem, state = TearingState(sys); mm = nothing, - simplify = false, cse_hack = true, array_hack = true, fully_determined = true, kwargs...) + simplify = false, array_hack = true, fully_determined = true, kwargs...) var_eq_matching, full_var_eq_matching, var_sccs, can_eliminate = tearing(state) invalidate_cache!(tearing_reassemble( state, var_eq_matching, full_var_eq_matching, var_sccs; mm, - simplify, cse_hack, array_hack, fully_determined)) + simplify, array_hack, fully_determined)) end """ @@ -1399,7 +1324,7 @@ Perform index reduction and use the dummy derivative technique to ensure that the system is balanced. """ function dummy_derivative(sys, state = TearingState(sys); simplify = false, - mm = nothing, cse_hack = true, array_hack = true, fully_determined = true, kwargs...) + mm = nothing, array_hack = true, fully_determined = true, kwargs...) jac = let state = state (eqs, vars) -> begin symeqs = EquationsView(state)[eqs] @@ -1425,5 +1350,5 @@ function dummy_derivative(sys, state = TearingState(sys); simplify = false, state, jac; state_priority, kwargs...) tearing_reassemble(state, var_eq_matching, full_var_eq_matching, var_sccs; - simplify, mm, cse_hack, array_hack, fully_determined) + simplify, mm, array_hack, fully_determined) end diff --git a/src/systems/nonlinear/initializesystem.jl b/src/systems/nonlinear/initializesystem.jl index b1ca187e95..df97adfdb3 100644 --- a/src/systems/nonlinear/initializesystem.jl +++ b/src/systems/nonlinear/initializesystem.jl @@ -780,20 +780,6 @@ function unhack_observed(obseqs::Vector{Equation}, eqs::Vector{Equation}) push!(rm_idxs, i) continue end - if operation(eq.rhs) == StructuralTransformations.getindex_wrapper - var, idxs = arguments(eq.rhs) - subs[eq.rhs] = var[idxs...] - push!(tempvars, var) - end - end - - for (i, eq) in enumerate(eqs) - iscall(eq.rhs) || continue - if operation(eq.rhs) == StructuralTransformations.getindex_wrapper - var, idxs = arguments(eq.rhs) - subs[eq.rhs] = var[idxs...] - push!(tempvars, var) - end end for (i, eq) in enumerate(obseqs) diff --git a/test/code_generation.jl b/test/code_generation.jl index 2d5925cbca..15de194fd8 100644 --- a/test/code_generation.jl +++ b/test/code_generation.jl @@ -79,3 +79,34 @@ end @test SciMLBase.successful_retcode(sol) end end + +@testset "scalarized array observed calling same function multiple times" begin + @variables x(t) y(t)[1:2] + @parameters foo(::Real)[1:2] + val = Ref(0) + function _tmp_fn2(x) + val[] += 1 + return [x, 2x] + end + @mtkcompile sys = System([D(x) ~ y[1] + y[2], y ~ foo(x)], t) + @test length(equations(sys)) == 1 + @test length(ModelingToolkit.observed(sys)) == 3 + prob = ODEProblem(sys, [x => 1.0, foo => _tmp_fn2], (0.0, 1.0)) + val[] = 0 + @test_nowarn prob.f(prob.u0, prob.p, 0.0) + @test val[] == 1 + + @testset "CSE in equations(sys)" begin + val[] = 0 + @variables z(t)[1:2] + @mtkcompile sys = System( + [D(y) ~ foo(x), D(x) ~ sum(y), zeros(2) ~ foo(prod(z))], t) + @test length(equations(sys)) == 5 + @test length(ModelingToolkit.observed(sys)) == 0 + prob = ODEProblem( + sys, [y => ones(2), z => 2ones(2), x => 3.0, foo => _tmp_fn2], (0.0, 1.0)) + val[] = 0 + @test_nowarn prob.f(prob.u0, prob.p, 0.0) + @test val[] == 2 + end +end diff --git a/test/structural_transformation/utils.jl b/test/structural_transformation/utils.jl index 4d5dead59f..4c1ba1e807 100644 --- a/test/structural_transformation/utils.jl +++ b/test/structural_transformation/utils.jl @@ -52,7 +52,7 @@ end @mtkcompile sys = System( [D(x) ~ z[1] + z[2] + foo(z)[1], y[1] ~ 2t, y[2] ~ 3t, z ~ foo(y)], t) @test length(equations(sys)) == 1 - @test length(observed(sys)) == 7 + @test length(observed(sys)) == 6 @test any(obs -> isequal(obs, y), observables(sys)) @test any(obs -> isequal(obs, z), observables(sys)) prob = ODEProblem(sys, [x => 1.0, foo => _tmp_fn], (0.0, 1.0)) @@ -62,60 +62,11 @@ end @test length(unknowns(isys)) == 5 @test length(equations(isys)) == 4 @test !any(equations(isys)) do eq - iscall(eq.rhs) && operation(eq.rhs) in [StructuralTransformations.getindex_wrapper, - StructuralTransformations.change_origin] + iscall(eq.rhs) && operation(eq.rhs) in [StructuralTransformations.change_origin] end end -@testset "scalarized array observed calling same function multiple times" begin - @variables x(t) y(t)[1:2] - @parameters foo(::Real)[1:2] - val = Ref(0) - function _tmp_fn2(x) - val[] += 1 - return [x, 2x] - end - @mtkcompile sys = System([D(x) ~ y[1] + y[2], y ~ foo(x)], t) - @test length(equations(sys)) == 1 - @test length(observed(sys)) == 4 - prob = ODEProblem(sys, [x => 1.0, foo => _tmp_fn2], (0.0, 1.0)) - val[] = 0 - @test_nowarn prob.f(prob.u0, prob.p, 0.0) - @test val[] == 1 - - isys = ModelingToolkit.generate_initializesystem(sys) - @test length(unknowns(isys)) == 3 - @test length(equations(isys)) == 2 - @test !any(equations(isys)) do eq - iscall(eq.rhs) && operation(eq.rhs) in [StructuralTransformations.getindex_wrapper, - StructuralTransformations.change_origin] - end - - @testset "CSE hack in equations(sys)" begin - val[] = 0 - @variables z(t)[1:2] - @mtkcompile sys = System( - [D(y) ~ foo(x), D(x) ~ sum(y), zeros(2) ~ foo(prod(z))], t) - @test length(equations(sys)) == 5 - @test length(observed(sys)) == 2 - prob = ODEProblem( - sys, [y => ones(2), z => 2ones(2), x => 3.0, foo => _tmp_fn2], (0.0, 1.0)) - val[] = 0 - @test_nowarn prob.f(prob.u0, prob.p, 0.0) - @test val[] == 2 - - isys = ModelingToolkit.generate_initializesystem(sys) - @test length(unknowns(isys)) == 5 - @test length(equations(isys)) == 2 - @test !any(equations(isys)) do eq - iscall(eq.rhs) && - operation(eq.rhs) in [StructuralTransformations.getindex_wrapper, - StructuralTransformations.change_origin] - end - end -end - -@testset "array and cse hacks can be disabled" begin +@testset "array hack can be disabled" begin @testset "fully_determined = true" begin @variables x(t) y(t)[1:2] z(t)[1:2] @parameters foo(::AbstractVector)[1:2] @@ -123,15 +74,8 @@ end @named sys = System( [D(x) ~ z[1] + z[2] + foo(z)[1], y[1] ~ 2t, y[2] ~ 3t, z ~ foo(y)], t) - sys1 = mtkcompile(sys; cse_hack = false) - @test length(observed(sys1)) == 6 - @test !any(observed(sys1)) do eq - iscall(eq.rhs) && - operation(eq.rhs) == StructuralTransformations.getindex_wrapper - end - sys2 = mtkcompile(sys; array_hack = false) - @test length(observed(sys2)) == 5 + @test length(observed(sys2)) == 4 @test !any(observed(sys2)) do eq iscall(eq.rhs) && operation(eq.rhs) == StructuralTransformations.change_origin end @@ -144,15 +88,8 @@ end @named sys = System( [D(x) ~ z[1] + z[2] + foo(z)[1] + w, y[1] ~ 2t, y[2] ~ 3t, z ~ foo(y)], t) - sys1 = mtkcompile(sys; cse_hack = false, fully_determined = false) - @test length(observed(sys1)) == 6 - @test !any(observed(sys1)) do eq - iscall(eq.rhs) && - operation(eq.rhs) == StructuralTransformations.getindex_wrapper - end - sys2 = mtkcompile(sys; array_hack = false, fully_determined = false) - @test length(observed(sys2)) == 5 + @test length(observed(sys2)) == 4 @test !any(observed(sys2)) do eq iscall(eq.rhs) && operation(eq.rhs) == StructuralTransformations.change_origin end From aff913f468f5144ca7de377ff53a569c20b0d649 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 5 Jun 2025 12:32:06 +0530 Subject: [PATCH 2018/2176] test: test array unknowns occurring unscalarized in initializeprobpmap --- test/initializationsystem.jl | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/test/initializationsystem.jl b/test/initializationsystem.jl index fc01e9e2e6..99e73e5b17 100644 --- a/test/initializationsystem.jl +++ b/test/initializationsystem.jl @@ -1643,3 +1643,24 @@ end @test !SciMLBase.isinplace(prob) @test !SciMLBase.isinplace(prob.f.initialization_data.initializeprob) end + +@testset "Array unknowns occurring unscalarized in initializeprobpmap" begin + @variables begin + u(t)[1:2] = 0.9ones(2) + x(t)[1:2], [guess = 0.01ones(2)] + o(t)[1:2] + end + @parameters p[1:4] = [2.0, 1.875, 2.0, 1.875] + + eqs = [D(u[1]) ~ p[1] * u[1] - p[2] * u[1] * u[2] + x[1] + 0.1 + D(u[2]) ~ p[4] * u[1] * u[2] - p[3] * u[2] - x[2] + o[1] ~ sum(p) * sum(u) + o[2] ~ sum(p) * sum(x) + x[1] ~ 0.01exp(-1) + x[2] ~ 0.01cos(t)] + + @mtkbuild sys = ODESystem(eqs, t) + prob = ODEProblem(sys, [], (0.0, 1.0)) + sol = solve(prob, Tsit5()) + @test SciMLBase.successful_retcode(sol) +end From fdbf149b0cf339f807ec4eaf08d7b042c04ff900 Mon Sep 17 00:00:00 2001 From: Christopher Rackauckas Date: Sat, 7 Jun 2025 18:39:13 +0000 Subject: [PATCH 2019/2176] Update Project.toml --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 9645a412b0..b1f3b7e43f 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "ModelingToolkit" uuid = "961ee093-0014-501f-94e3-6117800e7a78" authors = ["Yingbo Ma ", "Chris Rackauckas and contributors"] -version = "10.1.0" +version = "10.2.0" [deps] ADTypes = "47edcb42-4c32-4615-8424-f2b9edc5f35b" From 7b870477ae0dd26cd6314c8195568f718989a818 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 5 Jun 2025 16:40:24 +0530 Subject: [PATCH 2020/2176] feat: allow calling `generate_control_function` with unsimplified system Co-authored-by: Fredrik Bagge Carlson --- src/inputoutput.jl | 8 +++----- test/input_output_handling.jl | 23 +++++++++++------------ 2 files changed, 14 insertions(+), 17 deletions(-) diff --git a/src/inputoutput.jl b/src/inputoutput.jl index b4a6bfd998..ac022cbe2f 100644 --- a/src/inputoutput.jl +++ b/src/inputoutput.jl @@ -198,13 +198,11 @@ function generate_control_function(sys::AbstractSystem, inputs = unbound_inputs( simplify = false, eval_expression = false, eval_module = @__MODULE__, - check_simplified = true, kwargs...) - # Remove this when the ControlFunction gets merged. - if check_simplified && !iscomplete(sys) - error("A completed `ODESystem` is required. Call `complete` or `mtkcompile` on the system before creating the control function.") - end isempty(inputs) && @warn("No unbound inputs were found in system.") + if !iscomplete(sys) + sys = mtkcompile(sys; inputs, disturbance_inputs) + end if disturbance_inputs !== nothing # add to inputs for the purposes of io processing inputs = [inputs; disturbance_inputs] diff --git a/test/input_output_handling.jl b/test/input_output_handling.jl index f30949ed1b..ed1fb5fc69 100644 --- a/test/input_output_handling.jl +++ b/test/input_output_handling.jl @@ -165,8 +165,8 @@ end ] @named sys = System(eqs, t) - sys = mtkcompile(sys, inputs = [u]) - f, dvs, ps, io_sys = ModelingToolkit.generate_control_function(sys; simplify, split) + f, dvs, ps, io_sys = ModelingToolkit.generate_control_function( + sys, [u]; simplify, split) @test isequal(dvs[], x) @test isempty(ps) @@ -183,8 +183,8 @@ end ] @named sys = System(eqs, t) - sys = mtkcompile(sys, inputs = [u], disturbance_inputs = [d]) - f, dvs, ps, io_sys = ModelingToolkit.generate_control_function(sys; simplify, split) + f, dvs, ps, io_sys = ModelingToolkit.generate_control_function( + sys, [u], [d]; simplify, split) @test isequal(dvs[], x) @test isempty(ps) @@ -201,9 +201,9 @@ end ] @named sys = System(eqs, t) - sys = mtkcompile(sys, inputs = [u], disturbance_inputs = [d]) f, dvs, ps, io_sys = ModelingToolkit.generate_control_function( - sys; simplify, split, disturbance_argument = true) + sys, [u], [d]; + simplify, split, disturbance_argument = true) @test isequal(dvs[], x) @test isempty(ps) @@ -267,8 +267,8 @@ eqs = [connect_sd(sd, mass1, mass2) @named _model = System(eqs, t) @named model = compose(_model, mass1, mass2, sd); -model = mtkcompile(model, inputs = [u]) -f, dvs, ps, io_sys = ModelingToolkit.generate_control_function(model, simplify = true) +f, dvs, ps, io_sys = ModelingToolkit.generate_control_function( + model, [u]; simplify = true) @test length(dvs) == 4 p = MTKParameters(io_sys, [io_sys.u => NaN]) x = ModelingToolkit.varmap_to_vars( @@ -435,8 +435,8 @@ matrices = ModelingToolkit.reorder_unknowns( ] @named sys = System(eqs, t) - sys = mtkcompile(sys, inputs = [u]) - (; io_sys,) = ModelingToolkit.generate_control_function(sys, simplify = true) + (; io_sys,) = ModelingToolkit.generate_control_function( + sys, [u]; simplify = true) obsfn = ModelingToolkit.build_explicit_observed_function( io_sys, [x + u * t]; inputs = [u]) @test obsfn([1.0], [2.0], MTKParameters(io_sys, []), 3.0) ≈ [7.0] @@ -458,8 +458,7 @@ end @parameters p(::Real) = (x -> 2x) eqs = [D(x) ~ -x + p(u)] @named sys = System(eqs, t) - sys = mtkcompile(sys, inputs = [u]) - f, dvs, ps, io_sys = ModelingToolkit.generate_control_function(sys) + f, dvs, ps, io_sys = ModelingToolkit.generate_control_function(sys, [u]) p = MTKParameters(io_sys, []) u = [1.0] x = [1.0] From 973e9ebbe9bec385eca899e4a8f88420e55f0d72 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 9 Jun 2025 14:45:35 +0530 Subject: [PATCH 2021/2176] docs: add docstring for `generate_initializesystem` --- src/systems/nonlinear/initializesystem.jl | 31 +++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/src/systems/nonlinear/initializesystem.jl b/src/systems/nonlinear/initializesystem.jl index 5dfd69b72f..4f7bc7771f 100644 --- a/src/systems/nonlinear/initializesystem.jl +++ b/src/systems/nonlinear/initializesystem.jl @@ -1,3 +1,34 @@ +""" + $(TYPEDSIGNATURES) + +Generate the initialization system for `sys`. The initialization system is a system of +nonlinear equations that solve for the full set of initial conditions of `sys` given +specified constraints. + +The initialization system can be of two types: time-dependent and time-independent. +Time-dependent initialization systems solve for the initial values of unknowns as well as +the values of solvable parameters of the system. Time-independent initialization systems +only solve for solvable parameters of the system. + +# Keyword arguments + +- `time_dependent_init`: Whether to create an initialization system for a time-dependent + system. A time-dependent initialization requires a time-dependent `sys`, but a time- + independent initialization can be created regardless. +- `op`: The operating point of user-specified initial conditions of variables in `sys`. +- `initialization_eqs`: Additional initialization equations to use apart from those in + `initialization_equations(sys)`. +- `guesses`: Additional guesses to use apart from those in `guesses(sys)`. +- `default_dd_guess`: Default guess for dummy derivative variables in time-dependent + initialization. +- `algebraic_only`: If `false`, does not use initialization equations (provided via the + keyword or part of the system) to construct initialization. +- `check_defguess`: Whether to error when a variable does not have a default or guess + despite ModelingToolkit expecting it to. +- `name`: The name of the initialization system. + +All other keyword arguments are forwarded to the [`System`](@ref) constructor. +""" function generate_initializesystem( sys::AbstractSystem; time_dependent_init = is_time_dependent(sys), kwargs...) if time_dependent_init From 4bcd491b6a309de1d9c98a884cbc0ace5c195004 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 9 Jun 2025 14:46:03 +0530 Subject: [PATCH 2022/2176] docs: add docstrings for `get_`/`has_` event functions --- src/systems/callbacks.jl | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/src/systems/callbacks.jl b/src/systems/callbacks.jl index a12dad9d72..cf9c53e610 100644 --- a/src/systems/callbacks.jl +++ b/src/systems/callbacks.jl @@ -941,7 +941,23 @@ function discrete_events(sys::AbstractSystem) cbs end +""" + $(TYPEDSIGNATURES) + +Returns whether the system `sys` has the internal field `discrete_events`. + +See also [`get_discrete_events`](@ref). +""" has_discrete_events(sys::AbstractSystem) = isdefined(sys, :discrete_events) +""" + $(TYPEDSIGNATURES) + +Get the internal field `discrete_events` of a system `sys`. +It only includes `discrete_events` local to `sys`; not those of its subsystems, +like `unknowns(sys)`, `parameters(sys)` and `equations(sys)` does. + +See also [`has_discrete_events`](@ref). +""" function get_discrete_events(sys::AbstractSystem) has_discrete_events(sys) || return SymbolicDiscreteCallback[] getfield(sys, :discrete_events) @@ -984,7 +1000,23 @@ function continuous_events(sys::AbstractSystem) filter(!isempty, cbs) end +""" + $(TYPEDSIGNATURES) + +Returns whether the system `sys` has the internal field `continuous_events`. + +See also [`get_continuous_events`](@ref). +""" has_continuous_events(sys::AbstractSystem) = isdefined(sys, :continuous_events) +""" + $(TYPEDSIGNATURES) + +Get the internal field `continuous_events` of a system `sys`. +It only includes `continuous_events` local to `sys`; not those of its subsystems, +like `unknowns(sys)`, `parameters(sys)` and `equations(sys)` does. + +See also [`has_continuous_events`](@ref). +""" function get_continuous_events(sys::AbstractSystem) has_continuous_events(sys) || return SymbolicContinuousCallback[] getfield(sys, :continuous_events) From 7e7b2b4c077d0dd7a4fa792149a67b3c8fc5438d Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 9 Jun 2025 14:46:14 +0530 Subject: [PATCH 2023/2176] docs: add docstrings for codegen functions --- src/systems/codegen.jl | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/src/systems/codegen.jl b/src/systems/codegen.jl index 17ff652c41..96ff14f58a 100644 --- a/src/systems/codegen.jl +++ b/src/systems/codegen.jl @@ -277,6 +277,16 @@ function generate_tgrad( expression, wrap_gfw, (2, 3, is_split(sys)), res; eval_expression, eval_module) end +""" + $(TYPEDSIGNATURES) + +Return an array of symbolic hessians corresponding to the equations of the system. + +# Keyword Arguments + +- `sparse`: Controls whether the symbolic hessians are sparse matrices +- `simplify`: Forwarded to `Symbolics.hessian` +""" function calculate_hessian(sys::System; simplify = false, sparse = false) rhs = [eq.rhs - eq.lhs for eq in full_equations(sys)] dvs = unknowns(sys) @@ -484,6 +494,17 @@ function W_sparsity(sys::System) jac_sparsity .| M_sparsity end +""" + $(TYPEDSIGNATURES) + +Return the matrix to use as the jacobian prototype given the W-sparsity matrix of the +system. This is not the same as the jacobian sparsity pattern. + +# Keyword arguments + +- `u0`: The `u0` vector for the problem. +- `sparse`: The prototype is `nothing` for non-sparse matrices. +""" function calculate_W_prototype(W_sparsity; u0 = nothing, sparse = false) sparse || return nothing uElType = u0 === nothing ? Float64 : eltype(u0) @@ -599,6 +620,12 @@ function generate_cost(sys::System; expression = Val{true}, wrap_gfw = Val{false expression, wrap_gfw, (2, nargs, is_split(sys)), res; eval_expression, eval_module) end +""" + $(TYPEDSIGNATURES) + +Calculate the gradient of the consolidated cost of `sys` with respect to the unknowns. +`simplify` is forwarded to `Symbolics.gradient`. +""" function calculate_cost_gradient(sys::System; simplify = false) obj = cost(sys) dvs = unknowns(sys) @@ -629,6 +656,13 @@ function generate_cost_gradient( expression, wrap_gfw, (2, 2, is_split(sys)), res; eval_expression, eval_module) end +""" + $(TYPEDSIGNATURES) + +Calculate the hessian of the consolidated cost of `sys` with respect to the unknowns. +`simplify` is forwarded to `Symbolics.hessian`. `sparse` controls whether a sparse +matrix is returned. +""" function calculate_cost_hessian(sys::System; sparse = false, simplify = false) obj = cost(sys) dvs = unknowns(sys) From 3032d2fa8a3805c3a44258aace7c58a40ae5f5e7 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 9 Jun 2025 14:46:21 +0530 Subject: [PATCH 2024/2176] docs: add docstring for `expand_connections` --- src/systems/connectors.jl | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/systems/connectors.jl b/src/systems/connectors.jl index cf2f24a7d1..5d6227d4c1 100644 --- a/src/systems/connectors.jl +++ b/src/systems/connectors.jl @@ -860,6 +860,12 @@ function expand_variable_connections(sys::AbstractSystem; ignored_variables = no return sys end +""" + function expand_connections(sys::AbstractSystem) + +Given a hierarchical system with [`connect`](@ref) equations, expand the connection +equations and return the new system. +""" function expand_connections(sys::AbstractSystem; debug = false, tol = 1e-10, scalarize = true) sys = remove_analysis_points(sys) From c7be89d94a93b54bb0560e3bcbbd0b9178c81628 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 9 Jun 2025 14:46:32 +0530 Subject: [PATCH 2025/2176] docs: add docstring for `SampleTime` --- src/discretedomain.jl | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/discretedomain.jl b/src/discretedomain.jl index 73e6b8b7fa..a10fec81b4 100644 --- a/src/discretedomain.jl +++ b/src/discretedomain.jl @@ -1,5 +1,11 @@ using Symbolics: Operator, Num, Term, value, recursive_hasoperator +""" + function SampleTime() + +`SampleTime()` can be used in the equations of a hybrid system to represent time sampled +at the inferred clock for that equation. +""" struct SampleTime <: Operator SampleTime() = SymbolicUtils.term(SampleTime, type = Real) end From ae2a7ecabbd0eaad0cbd36242fb24a9fd009aff7 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 9 Jun 2025 14:47:41 +0530 Subject: [PATCH 2026/2176] docs: add docstrings for variable metadata functions --- src/variables.jl | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/src/variables.jl b/src/variables.jl index 104616f6e4..f9de12dae0 100644 --- a/src/variables.jl +++ b/src/variables.jl @@ -163,18 +163,50 @@ function isvarkind(m, x) getmetadata(x, m, false) end +""" + $(TYPEDSIGNATURES) + +Set the `input` metadata of variable `x` to `v`. +""" setinput(x, v::Bool) = setmetadata(x, VariableInput, v) +""" + $(TYPEDSIGNATURES) + +Set the `output` metadata of variable `x` to `v`. +""" setoutput(x, v::Bool) = setmetadata(x, VariableOutput, v) setio(x, i::Bool, o::Bool) = setoutput(setinput(x, i), o) +""" + $(TYPEDSIGNATURES) + +Check if variable `x` is marked as an input. +""" isinput(x) = isvarkind(VariableInput, x) +""" + $(TYPEDSIGNATURES) + +Check if variable `x` is marked as an output. +""" isoutput(x) = isvarkind(VariableOutput, x) # Before the solvability check, we already have handled IO variables, so # irreducibility is independent from IO. +""" + $(TYPEDSIGNATURES) + +Check if `x` is marked as irreducible. This prevents it from being eliminated as an +observed variable in `mtkcompile`. +""" isirreducible(x) = isvarkind(VariableIrreducible, x) setirreducible(x, v::Bool) = setmetadata(x, VariableIrreducible, v) state_priority(x::Union{Num, Symbolics.Arr}) = state_priority(unwrap(x)) +""" + $(TYPEDSIGNATURES) + +Return the `state_priority` metadata of variable `x`. This influences its priority to be +chosen as a state in `mtkcompile`. +""" state_priority(x) = convert(Float64, getmetadata(x, VariableStatePriority, 0.0))::Float64 normalize_to_differential(x) = x @@ -419,6 +451,11 @@ function getdescription(x) Symbolics.getmetadata(x, VariableDescription, "") end +""" + $(TYPEDSIGNATURES) + +Check if variable `x` has a non-empty attached description. +""" function hasdescription(x) getdescription(x) != "" end From 545eba2b7c01fb2b568aa8d601098b3212e16565 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 9 Jun 2025 14:47:07 +0530 Subject: [PATCH 2027/2176] fix: allow latexificaton of `change_origin` from array hack --- src/structural_transformation/symbolics_tearing.jl | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/structural_transformation/symbolics_tearing.jl b/src/structural_transformation/symbolics_tearing.jl index af0973f1dc..f8f05ffb7f 100644 --- a/src/structural_transformation/symbolics_tearing.jl +++ b/src/structural_transformation/symbolics_tearing.jl @@ -1265,7 +1265,7 @@ function tearing_hacks(sys, obs, unknowns, neweqs; array = true) for (arrvar, cnt) in arr_obs_occurrences cnt == length(arrvar) || continue # firstindex returns 1 for multidimensional array symbolics - firstind = first(eachindex(arrvar)) + firstind = Tuple(first(eachindex(arrvar))) scal = [arrvar[i] for i in eachindex(arrvar)] # respect non-1-indexed arrays # TODO: get rid of this hack together with the above hack, then remove OffsetArrays dependency @@ -1273,8 +1273,7 @@ function tearing_hacks(sys, obs, unknowns, neweqs; array = true) # try to `create_array(OffsetArray{...}, ...)` which errors. # `term(Origin(firstind), scal)` doesn't retain the `symtype` and `size` # of `scal`. - rhs = scal - rhs = change_origin(firstind, rhs) + rhs = change_origin(firstind, scal) push!(obs_arr_eqs, arrvar ~ rhs) end append!(obs, obs_arr_eqs) @@ -1284,7 +1283,7 @@ end # PART OF HACK function change_origin(origin, arr) - if all(isone, Tuple(origin)) + if all(isone, origin) return arr end return Origin(origin)(arr) From 66b8c5cb277b2bf9ad67b401d5eb880cb5b772ab Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 9 Jun 2025 14:48:25 +0530 Subject: [PATCH 2028/2176] refactor: deprecate `@brownian` in favor of `@brownians` --- src/ModelingToolkit.jl | 2 +- src/deprecations.jl | 7 +++++ src/problems/compatibility.jl | 2 +- src/systems/diffeqs/basic_transformations.jl | 2 +- src/variables.jl | 4 +-- test/dde.jl | 4 +-- test/debugging.jl | 2 +- test/initializationsystem.jl | 14 ++++----- test/sciml_problem_inputs.jl | 2 +- test/sdesystem.jl | 30 +++++++++++--------- test/structural_transformation/utils.jl | 2 +- test/test_variable_metadata.jl | 2 +- 12 files changed, 42 insertions(+), 31 deletions(-) diff --git a/src/ModelingToolkit.jl b/src/ModelingToolkit.jl index 255c5ade48..0133ab80d5 100644 --- a/src/ModelingToolkit.jl +++ b/src/ModelingToolkit.jl @@ -335,7 +335,7 @@ export generate_initializesystem, Initial, isinitial export alg_equations, diff_equations, has_alg_equations, has_diff_equations export get_alg_eqs, get_diff_eqs, has_alg_eqs, has_diff_eqs -export @variables, @parameters, @independent_variables, @constants, @brownian +export @variables, @parameters, @independent_variables, @constants, @brownians, @brownian export @named, @nonamespace, @namespace, extend, compose, complete, toggle_namespacing export debug_system diff --git a/src/deprecations.jl b/src/deprecations.jl index aaafa0d283..56c166f575 100644 --- a/src/deprecations.jl +++ b/src/deprecations.jl @@ -170,3 +170,10 @@ for T in [:NonlinearProblem, :NonlinearLeastSquaresProblem, end end end + +macro brownian(xs...) + return quote + Base.depwarn("`@brownian` is deprecated. Use `@brownians` instead", :brownian_macro) + $(@__MODULE__).@brownians $(xs...) + end +end diff --git a/src/problems/compatibility.jl b/src/problems/compatibility.jl index 36302a9e74..e5608ce4f1 100644 --- a/src/problems/compatibility.jl +++ b/src/problems/compatibility.jl @@ -119,7 +119,7 @@ function check_has_noise(sys::System, T) """ if !isempty(brownians(sys)) msg = """ - Systems constructed by defining Brownian variables with `@brownian` must be \ + Systems constructed by defining Brownian variables with `@brownians` must be \ simplified by calling `mtkcompile` before a `$T` can be constructed. """ end diff --git a/src/systems/diffeqs/basic_transformations.jl b/src/systems/diffeqs/basic_transformations.jl index 430260f60a..2eeb1a05d5 100644 --- a/src/systems/diffeqs/basic_transformations.jl +++ b/src/systems/diffeqs/basic_transformations.jl @@ -483,7 +483,7 @@ function noise_to_brownians(sys::System; names::Union{Symbol, Vector{Symbol}} = """)) end brownvars = map(names) do name - unwrap(only(@brownian $name)) + unwrap(only(@brownians $name)) end terms = if ndims(neqs) == 1 diff --git a/src/variables.jl b/src/variables.jl index f9de12dae0..38faf6dbda 100644 --- a/src/variables.jl +++ b/src/variables.jl @@ -475,11 +475,11 @@ $(SIGNATURES) Define one or more Brownian variables. """ -macro brownian(xs...) +macro brownians(xs...) all( x -> x isa Symbol || Meta.isexpr(x, :call) && x.args[1] == :$ || Meta.isexpr(x, :$), xs) || - error("@brownian only takes scalar expressions!") + error("@brownians only takes scalar expressions!") Symbolics._parse_vars(:brownian, Real, xs, diff --git a/test/dde.jl b/test/dde.jl index fb92bf61eb..df4acf377a 100644 --- a/test/dde.jl +++ b/test/dde.jl @@ -78,7 +78,7 @@ sol = solve(prob, RKMil(), seed = 100) @variables x(..) delx(t) @parameters a=-4.0 b=-2.0 c=10.0 α=-1.3 β=-1.2 γ=1.1 -@brownian η +@brownians η τ = 1.0 eqs = [D(x(t)) ~ a * x(t) + b * x(t - τ) + c + (α * x(t) + γ) * η, delx ~ x(t - τ)] @mtkcompile sys = System(eqs, t) @@ -188,7 +188,7 @@ end alg = MethodOfSteps(Vern7()) @test_nowarn solve(prob, alg) - @brownian r + @brownians r eqs = [D(x(t)) ~ -w * x(t - τ) + r] @named sys = System(eqs, t) sys = mtkcompile(sys) diff --git a/test/debugging.jl b/test/debugging.jl index 0cf80fd494..0ff7a0fb4d 100644 --- a/test/debugging.jl +++ b/test/debugging.jl @@ -3,7 +3,7 @@ import Logging using ModelingToolkit: t_nounits as t, D_nounits as D, ASSERTION_LOG_VARIABLE @variables x(t) -@brownian a +@brownians a @named inner_ode = System(D(x) ~ -sqrt(x), t; assertions = [(x > 0) => "ohno"]) @named inner_sde = System([D(x) ~ -10sqrt(x) + 0.01a], t; assertions = [(x > 0) => "ohno"]) sys_ode = mtkcompile(inner_ode) diff --git a/test/initializationsystem.jl b/test/initializationsystem.jl index 99e73e5b17..e12d05479f 100644 --- a/test/initializationsystem.jl +++ b/test/initializationsystem.jl @@ -592,7 +592,7 @@ end @testset "Initialization of parameters" begin @variables _x(..) y(t) @parameters p q - @brownian a b + @brownians a b x = _x(t) sarray_ctor = splat(SVector) # `System` constructor creates appropriate type with mtkcompile @@ -881,7 +881,7 @@ end @testset "Update initializeprob parameters" begin @variables _x(..) y(t) @parameters p q - @brownian a b + @brownians a b x = _x(t) @testset "$Problem with $(SciMLBase.parameterless_type(typeof(alg)))" for (System, Problem, alg, rhss) in [ @@ -910,7 +910,7 @@ end @testset "Equations for dependent parameters" begin @variables _x(..) @parameters p q=5 r - @brownian a + @brownians a x = _x(t) @testset "$Problem with $(SciMLBase.parameterless_type(typeof(alg)))" for (System, Problem, alg, rhss) in [ @@ -933,7 +933,7 @@ end @testset "Re-creating initialization problem on remake" begin @variables _x(..) y(t) @parameters p q - @brownian a b + @brownians a b x = _x(t) @testset "$Problem with $(SciMLBase.parameterless_type(typeof(alg)))" for (Problem, alg, rhss) in [ @@ -965,7 +965,7 @@ end @testset "`remake` changes initialization problem types" begin @variables _x(..) y(t) z(t) @parameters p q - @brownian a + @brownians a x = _x(t) @testset "$Problem with $(SciMLBase.parameterless_type(typeof(alg)))" for (System, Problem, alg, rhss) in [ @@ -1022,7 +1022,7 @@ end @testset "`remake` preserves old u0map and pmap" begin @variables _x(..) y(t) @parameters p - @brownian a + @brownians a x = _x(t) @testset "$Problem with $(SciMLBase.parameterless_type(typeof(alg)))" for (System, Problem, alg, rhss) in [ @@ -1426,7 +1426,7 @@ end @testset "Trivial initialization is run on problem construction" begin @variables _x(..) y(t) - @brownian a + @brownians a @parameters tot x = _x(t) @testset "$Problem" for (Problem, lhs, rhs) in [ diff --git a/test/sciml_problem_inputs.jl b/test/sciml_problem_inputs.jl index cd081118fb..46dfbbed21 100644 --- a/test/sciml_problem_inputs.jl +++ b/test/sciml_problem_inputs.jl @@ -150,7 +150,7 @@ end @testset "Deprecations" begin @variables _x(..) = 1.0 @parameters p = 1.0 - @brownian a + @brownians a x = _x(t) k = ShiftIndex(t) diff --git a/test/sdesystem.jl b/test/sdesystem.jl index 20f62c402c..f60e3a9777 100644 --- a/test/sdesystem.jl +++ b/test/sdesystem.jl @@ -591,7 +591,7 @@ end sts = @variables x(tt) y(tt) z(tt) ps = @parameters σ ρ -@brownian β η +@brownians β η s = 0.001 β *= s η *= s @@ -636,7 +636,7 @@ ssys = SDESystem(eqs, noise_eqs, t, [X], [p, d]; name = :ssys) # Issue#2814 @parameters p d @variables x(tt) -@brownian a +@brownians a eqs = [D(x) ~ p - d * x + a * sqrt(p)] @mtkcompile sys = System(eqs, tt) u0 = @SVector[x => 10.0] @@ -649,7 +649,7 @@ sprob = SDEProblem(sys, [u0; ps], tspan) # Ensure diagonal noise generates vector noise function @variables y(tt) -@brownian b +@brownians b eqs = [D(x) ~ p - d * x + a * sqrt(p) D(y) ~ p - d * y + b * sqrt(d)] @mtkcompile sys = System(eqs, tt) @@ -663,7 +663,7 @@ sprob = SDEProblem(sys, [u0; ps], tspan) let @parameters σ ρ β @variables x(t) y(t) z(t) - @brownian a + @brownians a eqs = [D(x) ~ σ * (y - x) + 0.1a * x, D(y) ~ x * (ρ - z) - y + 0.1a * y, D(z) ~ x * y - β * z + 0.1a * z] @@ -688,7 +688,7 @@ end let # test to make sure that scalar noise always receive the same kicks @variables x(t) y(t) - @brownian a + @brownians a eqs = [D(x) ~ a, D(y) ~ a] @@ -701,7 +701,7 @@ end let # test that diagonal noise is correctly handled @parameters σ ρ β @variables x(t) y(t) z(t) - @brownian a b c + @brownians a b c eqs = [D(x) ~ σ * (y - x) + 0.1a * x, D(y) ~ x * (ρ - z) - y + 0.1b * y, D(z) ~ x * y - β * z + 0.1c * z] @@ -728,7 +728,7 @@ end @testset "Non-diagonal noise check" begin @parameters σ ρ β @variables x(tt) y(tt) z(tt) - @brownian a b c d e f + @brownians a b c d e f eqs = [D(x) ~ σ * (y - x) + 0.1a * x + d, D(y) ~ x * (ρ - z) - y + 0.1b * y + e, D(z) ~ x * y - β * z + 0.1c * z + f] @@ -757,7 +757,7 @@ end @testset "Diagonal noise, less brownians than equations" begin @parameters σ ρ β @variables x(tt) y(tt) z(tt) - @brownian a b + @brownians a b eqs = [D(x) ~ σ * (y - x) + 0.1a * x, # One brownian D(y) ~ x * (ρ - z) - y + 0.1b * y, # Another brownian D(z) ~ x * y - β * z] # no brownians -- still diagonal @@ -781,7 +781,7 @@ end @testset "Passing `nothing` to `u0`" begin @variables x(t) = 1 - @brownian b + @brownians b @mtkcompile sys = System([D(x) ~ x + b], t) prob = @test_nowarn SDEProblem(sys, nothing, (0.0, 1.0)) @test_nowarn solve(prob, ImplicitEM()) @@ -794,7 +794,7 @@ end [input = true] end ps = @parameters a = 2 - browns = @brownian η + browns = @brownians η eqs = [D(x) ~ -a * x + (input + 1) * η input ~ 0.0] @@ -808,7 +808,7 @@ end @testset "Observed variables retained after `mtkcompile`" begin @variables x(t) y(t) z(t) - @brownian a + @brownians a @mtkcompile sys = System([D(x) ~ x + a, D(y) ~ y + a, z ~ x + y], t) @test length(observed(sys)) == 1 prob = SDEProblem(sys, [x => 1.0, y => 1.0], (0.0, 1.0)) @@ -875,7 +875,7 @@ end @testset "Validate input types" begin @parameters p d @variables X(t)::Int64 - @brownian z + @brownians z eq2 = D(X) ~ p - d * X + z @test_throws ModelingToolkit.ContinuousOperatorDiscreteArgumentError @mtkcompile ssys = System( [eq2], t) @@ -929,7 +929,7 @@ end @testset "Error when constructing SDEProblem without `mtkcompile`" begin @parameters σ ρ β @variables x(tt) y(tt) z(tt) - @brownian a + @brownians a eqs = [D(x) ~ σ * (y - x) + 0.1a * x, D(y) ~ x * (ρ - z) - y + 0.1a * y, D(z) ~ x * y - β * z + 0.1a * z] @@ -945,3 +945,7 @@ end de = mtkcompile(de) @test SDEProblem(de, [u0map; parammap], (0.0, 100.0)) isa SDEProblem end + +@testset "`@brownian` is deprecated" begin + @test_deprecated @brownian a b c +end diff --git a/test/structural_transformation/utils.jl b/test/structural_transformation/utils.jl index 4c1ba1e807..4a2df411a6 100644 --- a/test/structural_transformation/utils.jl +++ b/test/structural_transformation/utils.jl @@ -358,7 +358,7 @@ end @testset "SDESystem" begin @variables x(t) p a @parameters y(t) q b - @brownian c + @brownians c @mtkcompile sys = System([D(x) ~ x + q * a, D(y) ~ y + p * b + c], t, [x, y], [p, q], [a, b, c]; initialization_eqs = [p + q ~ 4], guesses = [p => 1.0], defaults = [p => missing]) diff --git a/test/test_variable_metadata.jl b/test/test_variable_metadata.jl index 482ebf927c..596372d45b 100644 --- a/test/test_variable_metadata.jl +++ b/test/test_variable_metadata.jl @@ -224,5 +224,5 @@ x = ModelingToolkit.toparam(x) @parameters y @test ModelingToolkit.getvariabletype(y) == ModelingToolkit.PARAMETER -@brownian z +@brownians z @test ModelingToolkit.getvariabletype(z) == ModelingToolkit.BROWNIAN From 8b10cd7938b4ae150fa228b180657c4b2418f5dc Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 9 Jun 2025 14:48:44 +0530 Subject: [PATCH 2029/2176] chore: mark unexported public functions with `@public` --- src/ModelingToolkit.jl | 11 ++++- src/systems/abstractsystem.jl | 85 ++++++++++++++++++----------------- 2 files changed, 53 insertions(+), 43 deletions(-) diff --git a/src/ModelingToolkit.jl b/src/ModelingToolkit.jl index 0133ab80d5..7a95cf4321 100644 --- a/src/ModelingToolkit.jl +++ b/src/ModelingToolkit.jl @@ -361,6 +361,15 @@ export AbstractCollocation, JuMPCollocation, InfiniteOptCollocation, CasADiCollocation, PyomoCollocation export DynamicOptSolution -@public apply_to_variables +@public apply_to_variables, equations_toplevel, unknowns_toplevel, parameters_toplevel +@public continuous_events_toplevel, discrete_events_toplevel, assertions, is_alg_equation +@public is_diff_equation, Equality, linearize_symbolic, reorder_unknowns +@public similarity_transform + +for prop in [SYS_PROPS; [:continuous_events, :discrete_events]] + getter = Symbol(:get_, prop) + hasfn = Symbol(:has_, prop) + @eval @public $getter, $hasfn +end end # module diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index 941739f127..6e3782f938 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -730,47 +730,49 @@ function unflatten_parameters!(buffer, params, all_ps) end end -for prop in [:eqs - :tag - :noise_eqs - :iv - :unknowns - :ps - :tspan - :brownians - :jumps - :name - :description - :var_to_name - :defaults - :guesses - :observed - :systems - :constraints - :bcs - :domain - :ivs - :dvs - :connector_type - :preface - :initializesystem - :initialization_eqs - :schedule - :tearing_state - :metadata - :gui_metadata - :is_initializesystem - :is_discrete - :parameter_dependencies - :assertions - :ignored_connections - :parent - :is_dde - :tstops - :index_cache - :isscheduled - :costs - :consolidate] +const SYS_PROPS = [:eqs + :tag + :noise_eqs + :iv + :unknowns + :ps + :tspan + :brownians + :jumps + :name + :description + :var_to_name + :defaults + :guesses + :observed + :systems + :constraints + :bcs + :domain + :ivs + :dvs + :connector_type + :preface + :initializesystem + :initialization_eqs + :schedule + :tearing_state + :metadata + :gui_metadata + :is_initializesystem + :is_discrete + :parameter_dependencies + :assertions + :ignored_connections + :parent + :is_dde + :tstops + :index_cache + :isscheduled + :costs + :consolidate] + +for prop in SYS_PROPS fname_get = Symbol(:get_, prop) fname_has = Symbol(:has_, prop) @eval begin @@ -780,7 +782,6 @@ for prop in [:eqs Get the internal field `$($(QuoteNode(prop)))` of a system `sys`. It only includes `$($(QuoteNode(prop)))` local to `sys`; not those of its subsystems, like `unknowns(sys)`, `parameters(sys)` and `equations(sys)` does. - This is equivalent to, but preferred over `sys.$($(QuoteNode(prop)))`. See also [`has_$($(QuoteNode(prop)))`](@ref). """ From dc355cd884cf1c9625e06410251c6fd4c7cdaac0 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 9 Jun 2025 14:49:05 +0530 Subject: [PATCH 2030/2176] docs: remove old `AbstractSystem.md` --- docs/src/basics/AbstractSystem.md | 162 ------------------------------ 1 file changed, 162 deletions(-) delete mode 100644 docs/src/basics/AbstractSystem.md diff --git a/docs/src/basics/AbstractSystem.md b/docs/src/basics/AbstractSystem.md deleted file mode 100644 index 61b6ef4fff..0000000000 --- a/docs/src/basics/AbstractSystem.md +++ /dev/null @@ -1,162 +0,0 @@ -# The AbstractSystem Interface - -## Overview - -The `AbstractSystem` interface is the core of the system level of ModelingToolkit.jl. -It establishes a common set of functionality that is used between systems -representing ODEs, PDEs, SDEs and more, allowing users to have a common framework for -model manipulation and compilation. - -### Subtypes - -There are three immediate subtypes of `AbstractSystem`, classified by how many independent variables each type has: - - - `AbstractTimeIndependentSystem`: has no independent variable (e.g.: `NonlinearSystem`) - - `AbstractTimeDependentSystem`: has a single independent variable (e.g.: `System`) - - `AbstractMultivariateSystem`: may have multiple independent variables (e.g.: `PDESystem`) - -## Constructors and Naming - -The `AbstractSystem` interface has a consistent method for constructing systems. -Generally, it follows the order of: - - 1. Equations - 2. Independent Variables - 3. Dependent Variables (or Unknowns) - 4. Parameters - -All other pieces are handled via keyword arguments. `AbstractSystem`s share the -same keyword arguments, which are: - - - `system`: This is used for specifying subsystems for hierarchical modeling with - reusable components. For more information, see the [components page](@ref components). - - Defaults: Keyword arguments like `defaults` are used for specifying default - values which are used. If a value is not given at the `SciMLProblem` construction - time, its numerical value will be the default. - -## Composition and Accessor Functions - -Each `AbstractSystem` has lists of variables in context, such as distinguishing -parameters vs unknowns. In addition, an `AbstractSystem` can also hold other -`AbstractSystem` types. Direct accessing of the values, such as `sys.unknowns`, -gives the immediate list, while the accessor functions `unknowns(sys)` gives the -total set, which includes that of all systems held inside. - -The values which are common to all `AbstractSystem`s are: - - - `equations(sys)`: All equations that define the system and its subsystems. - - `unknowns(sys)`: All the unknowns in the system and its subsystems. - - `parameters(sys)`: All parameters of the system and its subsystems. - - `nameof(sys)`: The name of the current-level system. - - `get_eqs(sys)`: Equations that define the current-level system. - - `get_unknowns(sys)`: Unknowns that are in the current-level system. - - `get_ps(sys)`: Parameters that are in the current-level system. - - `get_systems(sys)`: Subsystems of the current-level system. - -Optionally, a system could have: - - - `observed(sys)`: All observed equations of the system and its subsystems. - - `independent_variables(sys)`: The independent variables of a system. - - `defaults(sys)`: A `Dict` that maps variables/parameters into their default values for the system and its subsystems. - - `get_observed(sys)`: Observed equations of the current-level system. - - `get_continuous_events(sys)`: `SymbolicContinuousCallback`s of the current-level system. - - `get_defaults(sys)`: A `Dict` that maps variables into their default values - for the current-level system. - - `get_noiseeqs(sys)`: Noise equations of the current-level system. - - `get_description(sys)`: A string that describes what a system represents. - - `get_metadata(sys)`: Any metadata about the system or its origin to be used by downstream packages. - -Note that if you know a system is an `AbstractTimeDependentSystem` you could use `get_iv` to get the -unique independent variable directly, rather than using `independent_variables(sys)[1]`, which is clunky and may cause problems if `sys` is an `AbstractMultivariateSystem` because there may be more than one independent variable. `AbstractTimeIndependentSystem`s do not have a method `get_iv`, and `independent_variables(sys)` will return a size-zero result for such. For an `AbstractMultivariateSystem`, `get_ivs` is equivalent. - -For the `parameters`, `unknowns`, `continuous_events`, and `discrete_events` accessors there are corresponding `parameters_toplevel`, `unknowns_toplevel`, `continuous_events_toplevel`, and `discrete_events_toplevel` accessors which work similarly, but ignore the content of subsystems. Furthermore, a `equations_toplevel` version of `equations` exists as well, however, it can only be applied to non-complete systems. - -A system could also have caches: - - - `get_jac(sys)`: The Jacobian of a system. - - `get_tgrad(sys)`: The gradient with respect to time of a system. - -## Transformations - -Transformations are functions which send a valid `AbstractSystem` definition to -another `AbstractSystem`. These are passes, like optimizations (e.g., Block-Lower -Triangle transformations), or changes to the representation, which allow for -alternative numerical methods to be utilized on the model (e.g., DAE index reduction). - -## Analyses - -Analyses are functions on a system which return information about the corresponding -properties, like whether its parameters are structurally identifiable, or whether -it's linear. - -## Function Calculation and Generation - -The calculation and generation functions allow for calculating additional -quantities to enhance the numerical methods applied to the resulting system. -The calculations, like `calculate_jacobian`, generate ModelingToolkit IR for -the Jacobian of the system, while the generations, like `generate_jacobian`, -generate compiled output for the numerical solvers by applying `build_function` -to the generated code. Additionally, many systems have function-type outputs, -which cobble together the generation functionality for a system, for example, -`ODEFunction` can be used to generate a DifferentialEquations-based `ODEFunction` -with compiled version of the ODE itself, the Jacobian, the mass matrix, etc. - -Below are the possible calculation and generation functions: - -```@docs -calculate_tgrad -calculate_gradient -calculate_jacobian -calculate_factorized_W -calculate_hessian -generate_tgrad -generate_gradient -generate_jacobian -generate_factorized_W -generate_hessian -``` - -Additionally, `jacobian_sparsity(sys)` and `hessian_sparsity(sys)` -exist on the appropriate systems for fast generation of the sparsity -patterns via an abstract interpretation without requiring differentiation. - -## Problem Constructors - -At the end, the system types have `DEProblem` constructors, like `ODEProblem`, -which allow for directly generating the problem types required for numerical -methods. The first argument is always the `AbstractSystem`, and the next -arguments match the argument order of their original constructors. Whenever an -array would normally be provided, such as `u0` the initial condition of an -`ODEProblem`, it is instead replaced with a variable map, i.e., an array of -pairs `var=>value`, which allows the user to designate the values without having -to know the order that ModelingToolkit is internally using. - -For the value maps, the parameters are allowed to be functions of each other, -and value maps of unknowns can be functions of the parameters, i.e. you can do: - -``` -u0 = [ - lorenz1.x => 2.0 - lorenz2.x => lorenz1.x * lorenz1.p -] -``` - -## Default Value Handling - -The `AbstractSystem` types allow for specifying default values, for example -`defaults` inside of them. At problem construction time, these values are merged -into the value maps, where for any repeats the value maps override the default. -In addition, defaults of a higher level in the system override the defaults of -a lower level in the system. - -## Namespacing - -By default, unsimplified systems will namespace variables accessed via `getproperty`. -Systems created via `@mtkcompile`, or ones passed through `mtkcompile` or -`complete` will not perform this namespacing. However, all of these processes modify -the system in a variety of ways. To toggle namespacing without transforming any other -property of the system, use `toggle_namespacing`. - -```@docs -toggle_namespacing -``` From c84b996482e1a117665e66e68092ef15542623de Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 9 Jun 2025 15:13:28 +0530 Subject: [PATCH 2031/2176] refactor: export `InitializationProblem` --- src/ModelingToolkit.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ModelingToolkit.jl b/src/ModelingToolkit.jl index 7a95cf4321..fd352eaf52 100644 --- a/src/ModelingToolkit.jl +++ b/src/ModelingToolkit.jl @@ -330,7 +330,7 @@ export toexpr, get_variables export simplify, substitute export build_function export modelingtoolkitize -export generate_initializesystem, Initial, isinitial +export generate_initializesystem, Initial, isinitial, InitializationProblem export alg_equations, diff_equations, has_alg_equations, has_diff_equations export get_alg_eqs, get_diff_eqs, has_alg_eqs, has_diff_eqs From 5125ae6b60f2043b8e1720999098716fcbef3e0d Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 9 Jun 2025 15:13:51 +0530 Subject: [PATCH 2032/2176] chore: mark input/output-related functions as public --- src/ModelingToolkit.jl | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/ModelingToolkit.jl b/src/ModelingToolkit.jl index fd352eaf52..0a7fdd5209 100644 --- a/src/ModelingToolkit.jl +++ b/src/ModelingToolkit.jl @@ -364,7 +364,8 @@ export DynamicOptSolution @public apply_to_variables, equations_toplevel, unknowns_toplevel, parameters_toplevel @public continuous_events_toplevel, discrete_events_toplevel, assertions, is_alg_equation @public is_diff_equation, Equality, linearize_symbolic, reorder_unknowns -@public similarity_transform +@public similarity_transform, inputs, outputs, bound_inputs, unbound_inputs, bound_outputs +@public unbound_outputs, is_bound for prop in [SYS_PROPS; [:continuous_events, :discrete_events]] getter = Symbol(:get_, prop) From b370df7549c98704d473ea611bdb4d329d606ddb Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 9 Jun 2025 15:14:01 +0530 Subject: [PATCH 2033/2176] docs: remove reference to `parameter_dependencies` --- src/systems/abstractsystem.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index 6e3782f938..386dd2f2fd 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -1520,7 +1520,7 @@ $(TYPEDSIGNATURES) Get the default values of the system sys and its subsystems. If they are not explicitly provided, variables and parameters are initialized to these values. -See also [`initialization_equations`](@ref), [`parameter_dependencies`](@ref) and [`ModelingToolkit.get_defaults`](@ref). +See also [`initialization_equations`](@ref) and [`ModelingToolkit.get_defaults`](@ref). """ function defaults(sys::AbstractSystem) systems = get_systems(sys) @@ -1723,7 +1723,7 @@ $(TYPEDSIGNATURES) Get the initialization equations of the system `sys` and its subsystems. -See also [`guesses`](@ref), [`defaults`](@ref), [`parameter_dependencies`](@ref) and [`ModelingToolkit.get_initialization_eqs`](@ref). +See also [`guesses`](@ref), [`defaults`](@ref) and [`ModelingToolkit.get_initialization_eqs`](@ref). """ function initialization_equations(sys::AbstractSystem) eqs = get_initialization_eqs(sys) From 012056c4f07e3d0f594aab75461c64b6a5d838c2 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 9 Jun 2025 14:49:31 +0530 Subject: [PATCH 2034/2176] docs: fix doc pages --- docs/Project.toml | 2 + docs/make.jl | 2 + docs/src/API/System.md | 31 +++++++---- docs/src/API/codegen.md | 8 ++- docs/src/API/dynamic_opt.md | 2 +- docs/src/API/model_building.md | 3 +- docs/src/API/problems.md | 60 +++++++++++----------- docs/src/API/variables.md | 8 +-- docs/src/basics/Debugging.md | 2 +- docs/src/basics/Events.md | 6 +-- docs/src/basics/FAQ.md | 6 +-- docs/src/basics/InputOutput.md | 4 +- docs/src/basics/Linearization.md | 2 +- docs/src/internals.md | 42 --------------- docs/src/tutorials/SampledData.md | 2 +- docs/src/tutorials/disturbance_modeling.md | 2 +- docs/src/tutorials/initialization.md | 10 ++-- docs/src/tutorials/linear_analysis.md | 2 +- docs/src/tutorials/stochastic_diffeq.md | 8 +-- 19 files changed, 91 insertions(+), 111 deletions(-) diff --git a/docs/Project.toml b/docs/Project.toml index 314a1d7f84..746d6448b6 100644 --- a/docs/Project.toml +++ b/docs/Project.toml @@ -3,6 +3,7 @@ Attractors = "f3fd9213-ca85-4dba-9dfd-7fc91308fec7" BenchmarkTools = "6e4b80f9-dd63-53aa-95a3-0cdb28fa8baf" BifurcationKit = "0f109fa4-8a5d-4b75-95aa-f515264e7665" CairoMakie = "13f3f980-e62b-5c42-98c6-ff1f3baf88f0" +CommonSolve = "38540f10-b2f7-11e9-35d8-d573e4eb0ff2" ControlSystemsBase = "aaaaaaaa-a6ca-5380-bf3e-84a91bcd477e" DataInterpolations = "82cc6244-b520-54b8-b5a6-8a565e85f1d0" DiffEqDevTools = "f3b72e0c-5b89-59e1-b016-84e28bfd966d" @@ -13,6 +14,7 @@ FMI = "14a09403-18e3-468f-ad8a-74f8dda2d9ac" FMIZoo = "724179cf-c260-40a9-bd27-cccc6fe2f195" InfiniteOpt = "20393b10-9daf-11e9-18c9-8db751c92c57" Ipopt = "b6b21f68-93f8-5de0-b562-5493be1d77c9" +JumpProcesses = "ccbc3e58-028d-4f4c-8cd5-9ae44345cda5" LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" ModelingToolkit = "961ee093-0014-501f-94e3-6117800e7a78" ModelingToolkitStandardLibrary = "16a59e39-deab-5bd0-87e4-056b12336739" diff --git a/docs/make.jl b/docs/make.jl index 36a27bf598..4f3b616f0b 100644 --- a/docs/make.jl +++ b/docs/make.jl @@ -1,5 +1,7 @@ using Documenter, ModelingToolkit using ModelingToolkit: SciMLBase +# To load docstring from extension +import FMI, CommonSolve, JumpProcesses # Make sure that plots don't throw a bunch of warnings / errors! ENV["GKSwstype"] = "100" diff --git a/docs/src/API/System.md b/docs/src/API/System.md index 33a4d064ab..379e7fa19b 100644 --- a/docs/src/API/System.md +++ b/docs/src/API/System.md @@ -32,7 +32,7 @@ or analysis points of the hierarchical system. ModelingToolkit.has_eqs ModelingToolkit.get_eqs equations -equations_toplevel +ModelingToolkit.equations_toplevel full_equations ModelingToolkit.has_noise_eqs ModelingToolkit.get_noise_eqs @@ -44,17 +44,17 @@ ModelingToolkit.get_constraints constraints ModelingToolkit.has_costs ModelingToolkit.get_costs -costs +cost ModelingToolkit.has_consolidate ModelingToolkit.get_consolidate ModelingToolkit.has_unknowns ModelingToolkit.get_unknowns unknowns -unknowns_toplevel +ModelingToolkit.unknowns_toplevel ModelingToolkit.has_ps ModelingToolkit.get_ps parameters -parameters_toplevel +ModelingToolkit.parameters_toplevel tunable_parameters ModelingToolkit.has_brownians ModelingToolkit.get_brownians @@ -77,19 +77,20 @@ defaults ModelingToolkit.has_guesses ModelingToolkit.get_guesses guesses +ModelingToolkit.get_systems ModelingToolkit.has_initialization_eqs ModelingToolkit.get_initialization_eqs initialization_equations ModelingToolkit.has_continuous_events ModelingToolkit.get_continuous_events continuous_events -continuous_events_toplevel +ModelingToolkit.continuous_events_toplevel ModelingToolkit.has_discrete_events ModelingToolkit.get_discrete_events -discrete_events_toplevel +ModelingToolkit.discrete_events_toplevel ModelingToolkit.has_assertions ModelingToolkit.get_assertions -assertions +ModelingToolkit.assertions ModelingToolkit.has_metadata ModelingToolkit.get_metadata SymbolicUtils.getmetadata(::ModelingToolkit.AbstractSystem, ::DataType, ::Any) @@ -144,8 +145,8 @@ has_diff_equations has_alg_equations diff_equations alg_equations -is_alg_equation -is_diff_equation +ModelingToolkit.is_alg_equation +ModelingToolkit.is_diff_equation ``` ## String parsing @@ -167,6 +168,18 @@ ModelingToolkit.dump_parameters ModelingToolkit.dump_variable_metadata ``` +## Inputs and outputs + +```@docs +ModelingToolkit.inputs +ModelingToolkit.outputs +ModelingToolkit.bound_inputs +ModelingToolkit.unbound_inputs +ModelingToolkit.bound_outputs +ModelingToolkit.unbound_outputs +ModelingToolkit.is_bound +``` + ## Debugging utilities ```@docs diff --git a/docs/src/API/codegen.md b/docs/src/API/codegen.md index b3a9c01e58..cd76d34522 100644 --- a/docs/src/API/codegen.md +++ b/docs/src/API/codegen.md @@ -8,7 +8,6 @@ ModelingToolkit.generate_rhs ModelingToolkit.generate_diffusion_function ModelingToolkit.generate_jacobian ModelingToolkit.generate_tgrad -ModelingToolkit.generate_hessian ModelingToolkit.generate_W ModelingToolkit.generate_dae_jacobian ModelingToolkit.generate_history @@ -21,6 +20,7 @@ ModelingToolkit.generate_constraint_jacobian ModelingToolkit.generate_constraint_hessian ModelingToolkit.generate_control_jacobian ModelingToolkit.build_explicit_observed_function +ModelingToolkit.generate_control_function ``` For functions such as jacobian calculation which require symbolic computation, there @@ -43,3 +43,9 @@ ModelingToolkit.calculate_constraint_jacobian ModelingToolkit.calculate_constraint_hessian ModelingToolkit.calculate_control_jacobian ``` + +All code generation eventually calls `build_function_wrapper`. + +```@docs +build_function_wrapper +``` diff --git a/docs/src/API/dynamic_opt.md b/docs/src/API/dynamic_opt.md index 1a7081154c..b086fca522 100644 --- a/docs/src/API/dynamic_opt.md +++ b/docs/src/API/dynamic_opt.md @@ -28,7 +28,7 @@ JuMPCollocation InfiniteOptCollocation CasADiCollocation PyomoCollocation -solve(::AbstractDynamicOptProblem) +CommonSolve.solve(::AbstractDynamicOptProblem) ``` ### Problem constructors diff --git a/docs/src/API/model_building.md b/docs/src/API/model_building.md index 40072bc2c4..a72be8a4c6 100644 --- a/docs/src/API/model_building.md +++ b/docs/src/API/model_building.md @@ -81,7 +81,7 @@ Similar to the `stream` and `flow` keyword arguments in the specification, Model allows specifying how variables in a connector behave in a connection. ```@docs -Equality +ModelingToolkit.Equality Flow Stream ``` @@ -168,7 +168,6 @@ Symbolic affects are handled using equations as described in the [Events](@ref e section of the documentation. User-defined functions can be used via `ImperativeAffect`. ```@docs -ModelingToolkit.AffectSystem ModelingToolkit.ImperativeAffect ``` diff --git a/docs/src/API/problems.md b/docs/src/API/problems.md index 5549224a09..1e3591ea26 100644 --- a/docs/src/API/problems.md +++ b/docs/src/API/problems.md @@ -9,46 +9,46 @@ code for a variety of such numerical problems. ## Dynamical systems ```@docs -ODEFunction(::System, args...) -ODEProblem(::System, args...) -DAEFunction(::System, args...) -DAEProblem(::System, args...) -SDEFunction(::System, args...) -SDEProblem(::System, args...) -DDEFunction(::System, args...) -DDEProblem(::System, args...) -SDDEFunction(::System, args...) -SDDEProblem(::System, args...) -JumpProblem(::System, args...) -BVProblem(::System, args...) -DiscreteProblem(::System, args...) -ImplicitDiscreteProblem(::System, args...) +SciMLBase.ODEFunction +SciMLBase.ODEProblem +SciMLBase.DAEFunction +SciMLBase.DAEProblem +SciMLBase.SDEFunction +SciMLBase.SDEProblem +SciMLBase.DDEFunction +SciMLBase.DDEProblem +SciMLBase.SDDEFunction +SciMLBase.SDDEProblem +JumpProcesses.JumpProblem +SciMLBase.BVProblem +SciMLBase.DiscreteProblem +SciMLBase.ImplicitDiscreteProblem ``` ## Nonlinear systems ```@docs -NonlinearFunction(::System, args...) -NonlinearProblem(::System, args...) -SCCNonlinearProblem(::System, args...) -NonlinearLeastSquaresProblem(::System, args...) -SteadyStateProblem(::System, args...) -IntervalNonlinearFunction(::System, args...) -IntervalNonlinearProblem(::System, args...) +SciMLBase.NonlinearFunction +SciMLBase.NonlinearProblem +SciMLBase.SCCNonlinearProblem +SciMLBase.NonlinearLeastSquaresProblem +SciMLBase.SteadyStateProblem +SciMLBase.IntervalNonlinearFunction +SciMLBase.IntervalNonlinearProblem ModelingToolkit.HomotopyContinuationProblem -HomotopyNonlinearFunction(::System, args...) +SciMLBase.HomotopyNonlinearFunction ``` ## Optimization and optimal control ```@docs -OptimizationFunction(::System, args...) -OptimizationProblem(::System, args...) -ODEInputFunction(::System, args...) -JuMPDynamicOptProblem(::System, args...) -InfiniteOptDynamicOptProblem,(::System, args...) -CasADiDynamicOptProblem(::System, args...) -DynamicOptSolution +SciMLBase.OptimizationFunction +SciMLBase.OptimizationProblem +SciMLBase.ODEInputFunction +ModelingToolkit.JuMPDynamicOptProblem +ModelingToolkit.InfiniteOptDynamicOptProblem +ModelingToolkit.CasADiDynamicOptProblem +ModelingToolkit.DynamicOptSolution ``` ## The state vector and parameter object @@ -102,7 +102,7 @@ There are also utilities for manipulating the results of these analyses in a sym ```@docs ModelingToolkit.similarity_transform -ModelingToolkit.reorder_unknnowns +ModelingToolkit.reorder_unknowns ``` ### Analysis point transformations diff --git a/docs/src/API/variables.md b/docs/src/API/variables.md index 04d85e06b9..6f48c9a5f8 100644 --- a/docs/src/API/variables.md +++ b/docs/src/API/variables.md @@ -1,4 +1,4 @@ -# Symbolic variables and variable metadata +# [Symbolic variables and variable metadata](@id symbolic_metadata) ModelingToolkit uses [Symbolics.jl](https://docs.sciml.ai/Symbolics/stable/) for the symbolic manipulation infrastructure. In fact, the `@variables` macro is defined in Symbolics.jl. In @@ -85,7 +85,7 @@ hasconnect getconnect ``` -```@docs; canonical = false +```@docs; canonical=false Flow Stream ``` @@ -165,7 +165,7 @@ getguess When a system is constructed, the guesses of the involved variables are stored in a `Dict` in the system. After this point, the guess metadata of the variable is irrelevant. -```@docs; canonical = false +```@docs; canonical=false guesses ``` @@ -315,7 +315,7 @@ b = getbounds(sys) # Operating on the system, we get a dict See also: -```@docs; canonical = false +```@docs; canonical=false tunable_parameters ModelingToolkit.dump_unknowns ModelingToolkit.dump_parameters diff --git a/docs/src/basics/Debugging.md b/docs/src/basics/Debugging.md index 4f9c2c07d7..ccf1de263a 100644 --- a/docs/src/basics/Debugging.md +++ b/docs/src/basics/Debugging.md @@ -68,6 +68,6 @@ dprob[ModelingToolkit.ASSERTION_LOG_VARIABLE] = false; solve(dprob, Tsit5()); ``` -```@docs +```@docs; canonical = false debug_system ``` diff --git a/docs/src/basics/Events.md b/docs/src/basics/Events.md index 89f874b08b..aca8eb4b68 100644 --- a/docs/src/basics/Events.md +++ b/docs/src/basics/Events.md @@ -249,11 +249,11 @@ par = @parameters g = 9.8 bb_eqs = [D(x) ~ v D(v) ~ -g] -function bb_affect!(integ, u, p, ctx) - integ.u[u.v] = -integ.u[u.v] +function bb_affect!(mod, obs, integ, ctx) + return (; v = -mod.v) end -reflect = [x ~ 0] => (bb_affect!, [v], [], [], nothing) +reflect = [x ~ 0] => (bb_affect!, (; v)) @mtkcompile bb_sys = System(bb_eqs, t, sts, par, continuous_events = reflect) diff --git a/docs/src/basics/FAQ.md b/docs/src/basics/FAQ.md index e3b12b46ab..1a1ffe75ca 100644 --- a/docs/src/basics/FAQ.md +++ b/docs/src/basics/FAQ.md @@ -88,7 +88,7 @@ Strings are not considered symbolic variables, and thus cannot directly be used indexing. However, ModelingToolkit does provide a method to parse the string representation of a variable, given the system in which that variable exists. -```@docs +```@docs; canonical = false ModelingToolkit.parse_variable ``` @@ -260,14 +260,14 @@ D = Differential(x) Tunable parameters are floating point parameters, not used in callbacks and not marked with `tunable = false` in their metadata. These are expected to be used with AD and optimization libraries. As such, they are stored together in one `Vector{T}`. To obtain the ordering of tunable parameters in this buffer, use: -```@docs +```@docs; canonical = false tunable_parameters ``` If you have an array in which a particular dimension is in the order of tunable parameters (e.g. the jacobian with respect to tunables) then that dimension of the array can be reordered into the required permutation using the symbolic variables: -```@docs +```@docs; canonical = false reorder_dimension_by_tunables! reorder_dimension_by_tunables ``` diff --git a/docs/src/basics/InputOutput.md b/docs/src/basics/InputOutput.md index 2e9da1c2db..b1eb2905df 100644 --- a/docs/src/basics/InputOutput.md +++ b/docs/src/basics/InputOutput.md @@ -75,7 +75,7 @@ u = [rand()] ## Generating an output function, ``g`` -ModelingToolkit can also generate a function that computes a specified output of a system, the function ``y = g(x, u, p, t)`` above. This is done using the function [`build_explicit_observed_function`](@ref). When generating an output function, the user must specify the output variable(s) of interest, as well as any inputs if inputs are relevant to compute the output. +ModelingToolkit can also generate a function that computes a specified output of a system, the function ``y = g(x, u, p, t)`` above. This is done using the function [`ModelingToolkit.build_explicit_observed_function`](@ref). When generating an output function, the user must specify the output variable(s) of interest, as well as any inputs if inputs are relevant to compute the output. The order of the user-specified output variables determines the order of the output vector ``y``. @@ -93,7 +93,7 @@ See [Linearization](@ref linearization). Pages = ["InputOutput.md"] ``` -```@docs +```@docs; canonical=false ModelingToolkit.generate_control_function ModelingToolkit.build_explicit_observed_function ``` diff --git a/docs/src/basics/Linearization.md b/docs/src/basics/Linearization.md index 3951aab28a..0d219d35a5 100644 --- a/docs/src/basics/Linearization.md +++ b/docs/src/basics/Linearization.md @@ -153,7 +153,7 @@ Also see [ControlSystemsMTK.jl](https://juliacontrol.github.io/ControlSystemsMTK Pages = ["Linearization.md"] ``` -```@docs +```@docs; canonical = false linearize ModelingToolkit.linearize_symbolic ModelingToolkit.linearization_function diff --git a/docs/src/internals.md b/docs/src/internals.md index 0381e854b0..409ca51571 100644 --- a/docs/src/internals.md +++ b/docs/src/internals.md @@ -2,45 +2,3 @@ This is a page for detailing some of the inner workings to help future contributors to the library. - -## Observables and Variable Elimination - -In the variable “elimination” algorithms, what is actually done is that variables -are removed from being unknowns and equations are moved into the `observed` category -of the system. The `observed` equations are explicit algebraic equations which -are then substituted out to completely eliminate these variables from the other -equations, allowing the system to act as though these variables no longer exist. - -However, a user may want to interact with such variables, for example, -plotting their output. For this reason, these relationships are stored, -and are then used to generate the `observed` equation found in the -`SciMLFunction` interface, so that `sol[x]` lazily reconstructs the observed -variable when necessary. In this sense, there is an equivalence between -observables and the variable elimination system. - -The procedure for variable elimination inside [`mtkcompile`](@ref) is - - 1. [`ModelingToolkit.initialize_system_structure`](@ref). - 2. [`ModelingToolkit.alias_elimination`](@ref). This step moves equations into `observed(sys)`. - 3. [`ModelingToolkit.dae_index_lowering`](@ref) by means of [`pantelides!`](@ref) (if the system is an [`System`](@ref)). - 4. [`ModelingToolkit.tearing`](@ref). - -## Preparing a system for simulation - -Before a simulation or optimization can be performed, the symbolic equations stored in an [`AbstractSystem`](@ref) must be converted into executable code. This step typically occurs after the simplification explained above, and is performed when an instance of a [`SciMLBase.AbstractSciMLProblem`](@ref), such as a [`ODEProblem`](@ref), is constructed. -The call chain typically looks like this, with the function names in the case of an `System` indicated in parentheses - - 1. Problem constructor ([`ODEProblem`](@ref)) - 2. Build an `DEFunction` ([`process_DEProblem`](@ref) -> [`ODEFunction`](@ref) - 3. Write actual executable code ([`generate_function`](@ref) or [`generate_custom_function`](@ref)) - -Apart from [`generate_function`](@ref), which generates the dynamics function, `ODEFunction` also builds functions for observed equations (`build_explicit_observed_function`) and Jacobians (`generate_jacobian`) etc. These are all stored in the `ODEFunction`. - -## Creating an `MTKParameters` object - -It may be useful to create a parameter object without creating the problem. For this -purpose, the `MTKParameters` constructor is exposed as public API. - -```@docs -MTKParameters -``` diff --git a/docs/src/tutorials/SampledData.md b/docs/src/tutorials/SampledData.md index c2d6c9308d..9f3f340f46 100644 --- a/docs/src/tutorials/SampledData.md +++ b/docs/src/tutorials/SampledData.md @@ -189,7 +189,7 @@ connections = [r ~ sin(t) # reference signal @named cl = System(connections, t, systems = [f, c, p]) ``` -```@docs +```@docs; canonical = false Sample Hold ShiftIndex diff --git a/docs/src/tutorials/disturbance_modeling.md b/docs/src/tutorials/disturbance_modeling.md index 341077b76b..cdbfa25b40 100644 --- a/docs/src/tutorials/disturbance_modeling.md +++ b/docs/src/tutorials/disturbance_modeling.md @@ -123,7 +123,7 @@ y &= g(x, u, p, t) \end{aligned} ``` -To make use of the model defined above for state estimation, we may want to generate a Julia function for the dynamics ``f`` and the output equations ``g`` that we can plug into, e.g., a nonlinear version of a Kalman filter or a particle filter, etc. MTK contains utilities to do this, namely, [`generate_control_function`](@ref) and [`build_explicit_observed_function`](@ref) (described in more details in ["Input output"](@ref inputoutput)). These functions take keyword arguments `disturbance_inputs` and `disturbance_argument`, that indicate which variables in the model are considered part of ``w``, and whether or not these variables are to be added as function arguments to ``f``, i.e., whether we have ``f(x, u, p, t)`` or ``f(x, u, p, t, w)``. If we do not include the disturbance inputs as function arguments, MTK will assume that the ``w`` variables are all zero, but any dynamics associated with these variables, such as disturbance models, will be included in the generated function. This allows a state estimator to estimate the state of the disturbance model, provided that this state is [observable](https://en.wikipedia.org/wiki/Observability) from the measured outputs of the system. +To make use of the model defined above for state estimation, we may want to generate a Julia function for the dynamics ``f`` and the output equations ``g`` that we can plug into, e.g., a nonlinear version of a Kalman filter or a particle filter, etc. MTK contains utilities to do this, namely, [`ModelingToolkit.generate_control_function`](@ref) and [`ModelingToolkit.build_explicit_observed_function`](@ref) (described in more details in ["Input output"](@ref inputoutput)). These functions take keyword arguments `disturbance_inputs` and `disturbance_argument`, that indicate which variables in the model are considered part of ``w``, and whether or not these variables are to be added as function arguments to ``f``, i.e., whether we have ``f(x, u, p, t)`` or ``f(x, u, p, t, w)``. If we do not include the disturbance inputs as function arguments, MTK will assume that the ``w`` variables are all zero, but any dynamics associated with these variables, such as disturbance models, will be included in the generated function. This allows a state estimator to estimate the state of the disturbance model, provided that this state is [observable](https://en.wikipedia.org/wiki/Observability) from the measured outputs of the system. Below, we demonstrate diff --git a/docs/src/tutorials/initialization.md b/docs/src/tutorials/initialization.md index 90d86f1521..a804ca1b3e 100644 --- a/docs/src/tutorials/initialization.md +++ b/docs/src/tutorials/initialization.md @@ -362,7 +362,7 @@ which requires the system-level information and the additional nonlinear equatio tagged to the system. ```@example init -isys = generate_initializesystem(pend, u0map = [x => 1.0, y => 0.0], guesses = [λ => 1]) +isys = generate_initializesystem(pend; op = [x => 1.0, y => 0.0], guesses = [λ => 1]) ``` We can inspect what its equations and unknown values are: @@ -388,7 +388,7 @@ does not match the number of unknowns, which we can use to investigate our overd ```@example init isys = ModelingToolkit.generate_initializesystem( - pend, u0map = [x => 1, y => 0.0, D(y) => 2.0, λ => 1], guesses = [λ => 1]) + pend; op = [x => 1, y => 0.0, D(y) => 2.0, λ => 1], guesses = [λ => 1]) ``` ```@example init @@ -418,7 +418,7 @@ creates the special initialization system for a given `sys`. This is done as fol ```@example init iprob = ModelingToolkit.InitializationProblem(pend, 0.0, - [x => 1, y => 0.0, D(y) => 2.0, λ => 1], [g => 1], guesses = [λ => 1]) + [x => 1, y => 0.0, D(y) => 2.0, λ => 1, g => 1], guesses = [λ => 1]) ``` We can see that because the system is overdetermined we receive a NonlinearLeastSquaresProblem, @@ -460,7 +460,7 @@ some of the conditions: ```@example init iprob = ModelingToolkit.InitializationProblem(pend, 0.0, - [x => 1, y => 0.0, D(y) => 0.0, λ => 0], [g => 1], guesses = [λ => 1]) + [x => 1, y => 0.0, D(y) => 0.0, λ => 0, g => 1], guesses = [λ => 1]) ``` gives a NonlinearLeastSquaresProblem which can be solved: @@ -477,7 +477,7 @@ In comparison, if we have a well-conditioned system: ```@example init iprob = ModelingToolkit.InitializationProblem(pend, 0.0, - [x => 1, y => 0.0], [g => 1], guesses = [λ => 1]) + [x => 1, y => 0.0, g => 1], guesses = [λ => 1]) ``` notice that we instead obtained a NonlinearSystem. In this case we have to use diff --git a/docs/src/tutorials/linear_analysis.md b/docs/src/tutorials/linear_analysis.md index 250dbaa6a7..2119ea9ad1 100644 --- a/docs/src/tutorials/linear_analysis.md +++ b/docs/src/tutorials/linear_analysis.md @@ -146,7 +146,7 @@ nyquistplot(P) Pages = ["linear_analysis.md"] ``` -```@autodocs +```@autodocs; canonical = false Modules = [ModelingToolkit] Pages = ["systems/analysis_points.jl"] Order = [:function, :type] diff --git a/docs/src/tutorials/stochastic_diffeq.md b/docs/src/tutorials/stochastic_diffeq.md index 72a77eda05..d7326c36c6 100644 --- a/docs/src/tutorials/stochastic_diffeq.md +++ b/docs/src/tutorials/stochastic_diffeq.md @@ -39,7 +39,7 @@ By "multiplying" the equations by $dt$, the notation used in can be recovered. We use this Langevin-like notation because it allows us to extend MTK modeling capacity from ODEs to SDEs, -using only a single new concept, `@brownian` variables, which represent $\frac{dB}{dt}$ in the above equation. +using only a single new concept, `@brownians` variables, which represent $\frac{dB}{dt}$ in the above equation. ```@example SDE using ModelingToolkit, StochasticDiffEq @@ -48,7 +48,7 @@ using Plots @parameters σ=10.0 ρ=2.33 β=26.0 @variables x(t)=5.0 y(t)=5.0 z(t)=1.0 -@brownian B +@brownians B eqs = [D(x) ~ σ * (y - x) + 0.3x * B, D(y) ~ x * (ρ - z) - y + 0.3y * B, D(z) ~ x * y - β * z + 0.3z * B] @@ -80,10 +80,10 @@ plot(sol) ``` If you want uncorrelated noise for each equation, -multiple `@brownian` variables have to be declared. +multiple `@brownians` variables have to be declared. ```@example SDE -@brownian Bx By Bz +@brownians Bx By Bz eqs = [D(x) ~ σ * (y - x) + 0.3x * Bx, D(y) ~ x * (ρ - z) - y + 0.3y * By, D(z) ~ x * y - β * z + 0.3z * Bz] From 7de4f39816b03e41ccf90481c64474ef31a2d687 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 9 Jun 2025 17:13:38 +0530 Subject: [PATCH 2035/2176] docs: ignore HTML size threshold for `API/problems.md` --- docs/make.jl | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/docs/make.jl b/docs/make.jl index 4f3b616f0b..7cc7022adb 100644 --- a/docs/make.jl +++ b/docs/make.jl @@ -37,7 +37,9 @@ makedocs(sitename = "ModelingToolkit.jl", assets = ["assets/favicon.ico"], mathengine, canonical = "https://docs.sciml.ai/ModelingToolkit/stable/", - prettyurls = (get(ENV, "CI", nothing) == "true")), + prettyurls = (get(ENV, "CI", nothing) == "true"), + # This page gets especially big with all the problem docstrings + size_threshold_ignore = ["API/problems.md"]), pages = pages) deploydocs(repo = "github.com/SciML/ModelingToolkit.jl.git"; From cf816c46b23ba36b996ecc10024c55497e3b0132 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 9 Jun 2025 17:16:47 +0530 Subject: [PATCH 2036/2176] docs: add `MTKFMIExt` to modules for doc build --- docs/make.jl | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/docs/make.jl b/docs/make.jl index 7cc7022adb..248b9e35bc 100644 --- a/docs/make.jl +++ b/docs/make.jl @@ -3,6 +3,8 @@ using ModelingToolkit: SciMLBase # To load docstring from extension import FMI, CommonSolve, JumpProcesses +MTKFMIExt = Base.get_extension(ModelingToolkit, :MTKFMIExt) + # Make sure that plots don't throw a bunch of warnings / errors! ENV["GKSwstype"] = "100" using Plots @@ -24,7 +26,7 @@ mathengine = MathJax3(Dict(:loader => Dict("load" => ["[tex]/require", "[tex]/ma makedocs(sitename = "ModelingToolkit.jl", authors = "Chris Rackauckas", - modules = [ModelingToolkit], + modules = [ModelingToolkit, MTKFMIExt], clean = true, doctest = false, linkcheck = true, warnonly = [:docs_block, :missing_docs, :cross_references], linkcheck_ignore = [ From 2f6755950641b865c42a45d912888bc63e87bc8f Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 9 Jun 2025 13:41:19 +0530 Subject: [PATCH 2037/2176] fix: use `namespace_expr` in two-argument `unknowns` --- src/systems/abstractsystem.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index 386dd2f2fd..ae42ac7ba6 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -1538,9 +1538,9 @@ function defaults_and_guesses(sys::AbstractSystem) merge(guesses(sys), defaults(sys)) end -unknowns(sys::Union{AbstractSystem, Nothing}, v) = renamespace(sys, v) +unknowns(sys::Union{AbstractSystem, Nothing}, v) = namespace_expr(v, sys) for vType in [Symbolics.Arr, Symbolics.Symbolic{<:AbstractArray}] - @eval unknowns(sys::AbstractSystem, v::$vType) = renamespace(sys, v) + @eval unknowns(sys::AbstractSystem, v::$vType) = namespace_expr(v, sys) @eval parameters(sys::AbstractSystem, v::$vType) = toparam(unknowns(sys, v)) end parameters(sys::Union{AbstractSystem, Nothing}, v) = toparam(unknowns(sys, v)) From 4801a9e4e4ec347ada9d2326f0d6ab48f807ae74 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 9 Jun 2025 13:41:31 +0530 Subject: [PATCH 2038/2176] fix: do not consider variables of variables as delayed --- src/systems/codegen_utils.jl | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/systems/codegen_utils.jl b/src/systems/codegen_utils.jl index d6bcd06d07..dbbd7f85a8 100644 --- a/src/systems/codegen_utils.jl +++ b/src/systems/codegen_utils.jl @@ -123,7 +123,11 @@ function isdelay(var, iv) if iscall(var) && !ModelingToolkit.isoperator(var, Symbolics.Operator) args = arguments(var) length(args) == 1 || return false - isequal(args[1], iv) || return true + arg = args[1] + isequal(arg, iv) && return false + iscall(arg) || return true + issym(operation(arg)) && !iscalledparameter(arg) && return false + return true end return false end From 2ecbdb2dd6620a6a1785f47aa0afcf65e114f592 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 9 Jun 2025 13:41:41 +0530 Subject: [PATCH 2039/2176] test: test namespacing of variables of variables --- test/namespacing.jl | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/test/namespacing.jl b/test/namespacing.jl index 6a8a654ecf..4cc8ea7296 100644 --- a/test/namespacing.jl +++ b/test/namespacing.jl @@ -1,5 +1,6 @@ using ModelingToolkit -using ModelingToolkit: t_nounits as t, D_nounits as D, iscomplete, does_namespacing +using ModelingToolkit: t_nounits as t, D_nounits as D, iscomplete, does_namespacing, + renamespace @variables x(t) @parameters p @@ -24,3 +25,23 @@ nsys = toggle_namespacing(sys, false) @test_throws ["namespacing", "inner"] System( Equation[], t; systems = [nsys], name = :a) + +@testset "Variables of variables" begin + @variables x(t) y(x) + @named inner = System([D(x) ~ x, y ~ 2x + 1], t) + @test issetequal(unknowns(inner), [x, y]) + ss = mtkcompile(inner) + @test isequal(only(unknowns(ss)), x) + @test isequal(only(observed(ss)), y ~ 2x + 1) + + @named sys = System(Equation[], t; systems = [inner]) + xx, yy = let sys = inner + xx = renamespace(sys, x) + yy = only(@variables y(xx)) + xx, renamespace(sys, yy) + end + @test issetequal(unknowns(sys), [xx, yy]) + ss = mtkcompile(sys) + @test isequal(only(unknowns(ss)), xx) + @test isequal(only(observed(ss)), yy ~ 2xx + 1) +end From 18cc95179af8b9e1fef75fce65f5689299d21a65 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 9 Jun 2025 16:54:18 +0530 Subject: [PATCH 2040/2176] fix: handle `nothing` sys in `namespace_expr` --- src/systems/abstractsystem.jl | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index ae42ac7ba6..1d92416455 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -1169,7 +1169,9 @@ function is_array_of_symbolics(x) end function namespace_expr( - O, sys, n = nameof(sys); ivs = independent_variables(sys)) + O, sys, n = (sys === nothing ? nothing : nameof(sys)); + ivs = sys === nothing ? nothing : independent_variables(sys)) + sys === nothing && return O O = unwrap(O) # Exceptions for arrays of symbolic and Ref of a symbolic, the latter # of which shows up in broadcasts From 64829833fc6c502a7425a11e2d76ac6e91b087a2 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 9 Jun 2025 16:54:35 +0530 Subject: [PATCH 2041/2176] feat: `GlobalScope` variables in `@independent_variables` --- src/independent_variables.jl | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/independent_variables.jl b/src/independent_variables.jl index 94d792a11e..d1f2ab4210 100644 --- a/src/independent_variables.jl +++ b/src/independent_variables.jl @@ -7,8 +7,12 @@ Define one or more independent variables. For example: @variables x(t) """ macro independent_variables(ts...) - :(@parameters $(ts...)) |> esc # TODO: treat independent variables separately from variables and parameters + Symbolics._parse_vars(:independent_variables, + Real, + ts, + toiv) |> esc end -toiv(s::Symbolic) = setmetadata(s, MTKVariableTypeCtx, PARAMETER) +toiv(s::Symbolic) = GlobalScope(setmetadata(s, MTKVariableTypeCtx, PARAMETER)) +toiv(s::Symbolics.Arr) = wrap(toiv(value(s))) toiv(s::Num) = Num(toiv(value(s))) From c0453f470f4f22af219d296ce5787a2c3bbe2c94 Mon Sep 17 00:00:00 2001 From: fchen121 Date: Mon, 9 Jun 2025 16:29:29 -0400 Subject: [PATCH 2042/2176] Change of variable for non-simplified SDE --- src/systems/diffeqs/basic_transformations.jl | 24 ++++++++++++++++---- test/changeofvariables.jl | 12 ++++++++-- 2 files changed, 29 insertions(+), 7 deletions(-) diff --git a/src/systems/diffeqs/basic_transformations.jl b/src/systems/diffeqs/basic_transformations.jl index e28149819e..c779d1b64b 100644 --- a/src/systems/diffeqs/basic_transformations.jl +++ b/src/systems/diffeqs/basic_transformations.jl @@ -98,9 +98,6 @@ function changeofvariables( sys::System, iv, forward_subs, backward_subs; simplify=true, t0=missing, isSDE=false ) - if !iscomplete(sys) - sys = mtkcompile(sys) - end t = iv old_vars = first.(backward_subs) @@ -110,7 +107,12 @@ function changeofvariables( # use: dY = (∂f/∂t + μ∂f/∂x + (1/2)*σ^2*∂2f/∂x2)dt + σ∂f/∂xdW old_eqs = equations(sys) neqs = get_noise_eqs(sys) - if neqs !== nothing + brownvars = brownians(sys) + + + if neqs === nothing && length(brownvars) === 0 + neqs = ones(1, length(old_eqs)) + elseif neqs !== nothing isSDE = true neqs = [neqs[i,:] for i in 1:size(neqs,1)] @@ -118,7 +120,19 @@ function changeofvariables( unwrap(only(@brownian $name)) end else - neqs = ones(1, length(old_eqs)) + isSDE = true + neqs = Vector{Any}[] + for (i, eq) in enumerate(old_eqs) + neq = Any[] + right = eq.rhs + for Bv in brownvars + lin_exp = linear_expansion(right, Bv) + right = lin_exp[2] + push!(neq, lin_exp[1]) + end + push!(neqs, neq) + old_eqs[i] = eq.lhs ~ right + end end # df/dt = ∂f/∂x dx/dt + ∂f/∂t diff --git a/test/changeofvariables.jl b/test/changeofvariables.jl index 1c0204e8fd..7f3ee5eb0b 100644 --- a/test/changeofvariables.jl +++ b/test/changeofvariables.jl @@ -127,9 +127,10 @@ new_sys = changeofvariables(sys, t, forward_subs, backward_subs) D = Differential(t) eqs = [D(x) ~ μ*x + σ*x*Bx, D(y) ~ α*By, D(u) ~ μ*u + σ*u*Bx + α*u*By] def = [x=>0., y=> 0., u=>0., μ => 2., σ=>1., α=>3.] -@mtkcompile sys = System(eqs, t; defaults=def) forward_subs = [log(x) => z, y^2 => w, log(u) => v] backward_subs = [x => exp(z), y => w^.5, u => exp(v)] + +@mtkcompile sys = System(eqs, t; defaults=def) new_sys = changeofvariables(sys, t, forward_subs, backward_subs) @test equations(new_sys)[1] == (D(z) ~ μ - 1/2*σ^2) @test equations(new_sys)[2] == (D(w) ~ α^2) @@ -139,4 +140,11 @@ new_sys = changeofvariables(sys, t, forward_subs, backward_subs) @test noise_eqs(new_sys)[2,1] === value(0) @test noise_eqs(new_sys)[2,2] === value(substitute(2*α*y, backward_subs[2])) @test noise_eqs(new_sys)[3,1] === value(σ) -@test noise_eqs(new_sys)[3,2] === value(α) \ No newline at end of file +@test noise_eqs(new_sys)[3,2] === value(α) + +# Test for Brownian instead of noise +@named sys = System(eqs, t; defaults=def) +new_sys = changeofvariables(sys, t, forward_subs, backward_subs; simplify=false) +@test simplify(equations(new_sys)[1]) == simplify((D(z) ~ μ - 1/2*σ^2 + σ*Bx)) +@test simplify(equations(new_sys)[2]) == simplify((D(w) ~ α^2 + 2*α*w^.5*By)) +@test simplify(equations(new_sys)[3]) == simplify((D(v) ~ μ - 1/2*(α^2 + σ^2) + σ*Bx + α*By)) \ No newline at end of file From e7927f528fb3cc3b56bfdf38f3691195a09ef15b Mon Sep 17 00:00:00 2001 From: CompatHelper Julia Date: Tue, 10 Jun 2025 00:28:06 +0000 Subject: [PATCH 2043/2176] CompatHelper: add new compat entry for InfiniteOpt at version 0.5 for package docs, (keep existing compat) --- docs/Project.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/Project.toml b/docs/Project.toml index 746d6448b6..9971021dcb 100644 --- a/docs/Project.toml +++ b/docs/Project.toml @@ -42,6 +42,7 @@ Documenter = "1" DynamicQuantities = "^0.11.2, 0.12, 1" FMI = "0.14" FMIZoo = "1" +InfiniteOpt = "0.5" ModelingToolkit = "10" ModelingToolkitStandardLibrary = "2.19" NonlinearSolve = "3, 4" From 278f8f32962667ce88d075ae9f6c9871ffe7a1b8 Mon Sep 17 00:00:00 2001 From: CompatHelper Julia Date: Tue, 10 Jun 2025 00:28:10 +0000 Subject: [PATCH 2044/2176] CompatHelper: add new compat entry for Ipopt at version 1 for package docs, (keep existing compat) --- docs/Project.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/Project.toml b/docs/Project.toml index 746d6448b6..e4095cd15c 100644 --- a/docs/Project.toml +++ b/docs/Project.toml @@ -42,6 +42,7 @@ Documenter = "1" DynamicQuantities = "^0.11.2, 0.12, 1" FMI = "0.14" FMIZoo = "1" +Ipopt = "1" ModelingToolkit = "10" ModelingToolkitStandardLibrary = "2.19" NonlinearSolve = "3, 4" From 707b0039f68374e3d97b4a6e677f946c416ddcc5 Mon Sep 17 00:00:00 2001 From: CompatHelper Julia Date: Tue, 10 Jun 2025 00:28:12 +0000 Subject: [PATCH 2045/2176] CompatHelper: add new compat entry for CommonSolve at version 0.2 for package docs, (keep existing compat) --- docs/Project.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/Project.toml b/docs/Project.toml index 746d6448b6..a604d222c4 100644 --- a/docs/Project.toml +++ b/docs/Project.toml @@ -36,6 +36,7 @@ Attractors = "1.24" BenchmarkTools = "1.3" BifurcationKit = "0.4" CairoMakie = "0.13" +CommonSolve = "0.2" DataInterpolations = "6.5, 8" Distributions = "0.25" Documenter = "1" From f99df25124344a2589b0126ea85b347d19471561 Mon Sep 17 00:00:00 2001 From: CompatHelper Julia Date: Tue, 10 Jun 2025 00:28:14 +0000 Subject: [PATCH 2046/2176] CompatHelper: add new compat entry for DiffEqDevTools at version 2 for package docs, (keep existing compat) --- docs/Project.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/Project.toml b/docs/Project.toml index 746d6448b6..2dcd4137df 100644 --- a/docs/Project.toml +++ b/docs/Project.toml @@ -37,6 +37,7 @@ BenchmarkTools = "1.3" BifurcationKit = "0.4" CairoMakie = "0.13" DataInterpolations = "6.5, 8" +DiffEqDevTools = "2" Distributions = "0.25" Documenter = "1" DynamicQuantities = "^0.11.2, 0.12, 1" From a481c615783b2d1c16388f59be441e3b1ab0874c Mon Sep 17 00:00:00 2001 From: CompatHelper Julia Date: Tue, 10 Jun 2025 00:28:16 +0000 Subject: [PATCH 2047/2176] CompatHelper: add new compat entry for JumpProcesses at version 9 for package docs, (keep existing compat) --- docs/Project.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/Project.toml b/docs/Project.toml index 746d6448b6..8f6e7c1412 100644 --- a/docs/Project.toml +++ b/docs/Project.toml @@ -42,6 +42,7 @@ Documenter = "1" DynamicQuantities = "^0.11.2, 0.12, 1" FMI = "0.14" FMIZoo = "1" +JumpProcesses = "9" ModelingToolkit = "10" ModelingToolkitStandardLibrary = "2.19" NonlinearSolve = "3, 4" From 7c3886efcc5cf95a955c54b2d751a77bcbb9f1a0 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 9 Jun 2025 16:57:17 +0530 Subject: [PATCH 2048/2176] fix: ensure `=> nothing` overrides defaults --- src/systems/nonlinear/initializesystem.jl | 6 +++--- test/initializationsystem.jl | 7 +++++++ 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/systems/nonlinear/initializesystem.jl b/src/systems/nonlinear/initializesystem.jl index 4f7bc7771f..9b6e38e338 100644 --- a/src/systems/nonlinear/initializesystem.jl +++ b/src/systems/nonlinear/initializesystem.jl @@ -73,7 +73,7 @@ function generate_initializesystem_timevarying(sys::AbstractSystem; op = anydict(op) u0map = anydict() pmap = anydict() - build_operating_point!(sys, op, u0map, pmap, defs, unknowns(sys), + build_operating_point!(sys, op, u0map, pmap, Dict(), unknowns(sys), parameters(sys; initial_parameters = true)) for (k, v) in op if has_parameter_dependency_with_lhs(sys, k) && is_variable_floatingpoint(k) @@ -144,7 +144,7 @@ function generate_initializesystem_timevarying(sys::AbstractSystem; # 3) process other variables for var in vars - if var ∈ keys(defs) + if var ∈ keys(op) push!(eqs_ics, var ~ defs[var]) elseif var ∈ keys(guesses) push!(defs, var => guesses[var]) @@ -238,7 +238,7 @@ function generate_initializesystem_timeindependent(sys::AbstractSystem; op = anydict(op) u0map = anydict() pmap = anydict() - build_operating_point!(sys, op, u0map, pmap, defs, unknowns(sys), + build_operating_point!(sys, op, u0map, pmap, Dict(), unknowns(sys), parameters(sys; initial_parameters = true)) for (k, v) in op if has_parameter_dependency_with_lhs(sys, k) && is_variable_floatingpoint(k) diff --git a/test/initializationsystem.jl b/test/initializationsystem.jl index e12d05479f..06d0752076 100644 --- a/test/initializationsystem.jl +++ b/test/initializationsystem.jl @@ -1664,3 +1664,10 @@ end sol = solve(prob, Tsit5()) @test SciMLBase.successful_retcode(sol) end + +@testset "Defaults removed with ` => nothing` aren't retained" begin + @variables x(t)[1:2] + @mtkbuild sys = System([D(x[1]) ~ -x[1], x[1] + x[2] ~ 3], t; defaults = [x[1] => 1]) + prob = ODEProblem(sys, [x[1] => nothing, x[2] => 1], (0.0, 1.0)) + @test SciMLBase.initialization_status(prob) == SciMLBase.FULLY_DETERMINED +end From 44857dde76c67358d5a71276f9382a50ee384c41 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 9 Jun 2025 18:45:35 +0530 Subject: [PATCH 2049/2176] fix: fallback `op` to `defs` if empty --- src/systems/nonlinear/initializesystem.jl | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/systems/nonlinear/initializesystem.jl b/src/systems/nonlinear/initializesystem.jl index 9b6e38e338..2e56f1f2ba 100644 --- a/src/systems/nonlinear/initializesystem.jl +++ b/src/systems/nonlinear/initializesystem.jl @@ -71,6 +71,9 @@ function generate_initializesystem_timevarying(sys::AbstractSystem; # PREPROCESSING op = anydict(op) + if isempty(op) + op = copy(defs) + end u0map = anydict() pmap = anydict() build_operating_point!(sys, op, u0map, pmap, Dict(), unknowns(sys), From 166b0f82c479cb856846c7653bf7ffcfdf843414 Mon Sep 17 00:00:00 2001 From: Christopher Rackauckas Date: Tue, 10 Jun 2025 09:43:05 +0000 Subject: [PATCH 2050/2176] Update test/changeofvariables.jl --- test/changeofvariables.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/changeofvariables.jl b/test/changeofvariables.jl index 7f3ee5eb0b..776d44f7e7 100644 --- a/test/changeofvariables.jl +++ b/test/changeofvariables.jl @@ -105,7 +105,7 @@ noise_eqs = ModelingToolkit.get_noise_eqs value = ModelingToolkit.value @independent_variables t -@brownian B +@brownians B @parameters μ σ @variables x(t) y(t) D = Differential(t) @@ -121,7 +121,7 @@ new_sys = changeofvariables(sys, t, forward_subs, backward_subs) #Multiple Brownian and equations @independent_variables t -@brownian Bx By +@brownians Bx By @parameters μ σ α @variables x(t) y(t) z(t) w(t) u(t) v(t) D = Differential(t) From 3224c338d8818ac6fcf09c75671c7db122dd3368 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 10 Jun 2025 16:38:59 +0530 Subject: [PATCH 2051/2176] fix: handle unscalarized observed variable defaults in initialization --- src/systems/nonlinear/initializesystem.jl | 7 +++++++ src/systems/problem_utils.jl | 18 ++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/src/systems/nonlinear/initializesystem.jl b/src/systems/nonlinear/initializesystem.jl index 2e56f1f2ba..eab33e00b3 100644 --- a/src/systems/nonlinear/initializesystem.jl +++ b/src/systems/nonlinear/initializesystem.jl @@ -61,6 +61,12 @@ function generate_initializesystem_timevarying(sys::AbstractSystem; isempty(trueobs) || filter_delay_equations_variables!(sys, trueobs) vars = unique([unknowns(sys); getfield.(trueobs, :lhs)]) vars_set = Set(vars) # for efficient in-lookup + arrvars = Set() + for var in vars + if iscall(var) && operation(var) === getindex + push!(arrvars, first(arguments(var))) + end + end eqs_ics = Equation[] defs = copy(defaults(sys)) # copy so we don't modify sys.defaults @@ -74,6 +80,7 @@ function generate_initializesystem_timevarying(sys::AbstractSystem; if isempty(op) op = copy(defs) end + scalarize_vars_in_varmap!(op, arrvars) u0map = anydict() pmap = anydict() build_operating_point!(sys, op, u0map, pmap, Dict(), unknowns(sys), diff --git a/src/systems/problem_utils.jl b/src/systems/problem_utils.jl index 2016b1efd8..65b1e4ba18 100644 --- a/src/systems/problem_utils.jl +++ b/src/systems/problem_utils.jl @@ -523,6 +523,24 @@ function scalarize_varmap!(varmap::AbstractDict) return varmap end +""" + $(TYPEDSIGNATURES) + +For each array variable in `vars`, scalarize the corresponding entry in `varmap`. +If a scalarized entry already exists, it is not overridden. +""" +function scalarize_vars_in_varmap!(varmap::AbstractDict, vars) + for var in vars + symbolic_type(var) == ArraySymbolic() || continue + is_sized_array_symbolic(var) || continue + haskey(varmap, var) || continue + for i in eachindex(var) + haskey(varmap, var[i]) && continue + varmap[var[i]] = varmap[var][i] + end + end +end + function get_temporary_value(p, floatT = Float64) stype = symtype(unwrap(p)) return if stype == Real From 629c26b2538547e310d8f4463fd6c9697926d17a Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 10 Jun 2025 20:37:11 +0530 Subject: [PATCH 2052/2176] fix: do not unwrap initials in `initializeprobpmap` --- src/systems/problem_utils.jl | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/systems/problem_utils.jl b/src/systems/problem_utils.jl index 2016b1efd8..3954d8f1e0 100644 --- a/src/systems/problem_utils.jl +++ b/src/systems/problem_utils.jl @@ -718,6 +718,8 @@ takes a value provider of `srcsys` and a value provider of `dstsys` and returns # Keyword Arguments - `initials`: Whether to include the `Initial` parameters of `dstsys` among the values to be transferred. +- `unwrap_initials`: Whether initials in `dstsys` corresponding to unknowns in `srcsys` are + unwrapped. - `p_constructor`: The `p_constructor` argument to `process_SciMLProblem`. """ function get_mtkparameters_reconstructor(srcsys::AbstractSystem, dstsys::AbstractSystem; @@ -740,7 +742,7 @@ function get_mtkparameters_reconstructor(srcsys::AbstractSystem, dstsys::Abstrac end initials_getter = if initials && !isempty(syms[2]) initsyms = Vector{Any}(syms[2]) - allsyms = Set(all_symbols(srcsys)) + allsyms = Set(variable_symbols(srcsys)) if unwrap_initials for i in eachindex(initsyms) sym = initsyms[i] From 3518b395182b7cbb6997f818fa0d387e93cf8f74 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 10 Jun 2025 20:37:22 +0530 Subject: [PATCH 2053/2176] test: test that redundant observed expressions are not evaluated --- test/code_generation.jl | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/test/code_generation.jl b/test/code_generation.jl index 15de194fd8..158ee64fbe 100644 --- a/test/code_generation.jl +++ b/test/code_generation.jl @@ -110,3 +110,33 @@ end @test val[] == 2 end end + +@testset "Do not codegen redundant expressions" begin + @variables v1(t) = 1 + @variables v2(t) [guess = 0] + + mutable struct Data + count::Int + end + function update!(d::Data, t) + d.count += 1 # Count the number of times the data gets updated. + end + function (d::Data)(t) + update!(d, t) + rand(1:10) + end + + @parameters (d1::Data)(..) = Data(0) + @parameters (d2::Data)(..) = Data(0) + + eqs = [ + D(v1) ~ d1(t), + v2 ~ d2(t) # Some of the data parameters are not actually needed to solve the system. + ] + + @mtkbuild sys = System(eqs, t) + prob = ODEProblem(sys, [], (0.0, 1.0)) + sol = solve(prob, Tsit5()) + + @test sol.ps[d2].count == 0 +end From 2641ab37a8ce58782c6804201995ddbcf55eab8e Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 11 Jun 2025 14:01:46 +0530 Subject: [PATCH 2054/2176] docs: collapse docstrings by default in `API/problems.md` --- docs/src/API/problems.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/docs/src/API/problems.md b/docs/src/API/problems.md index 1e3591ea26..d308785d77 100644 --- a/docs/src/API/problems.md +++ b/docs/src/API/problems.md @@ -1,3 +1,7 @@ +```@meta +CollapsedDocStrings = true +``` + # Building and solving numerical problems Systems are numerically solved by building and solving the appropriate problem type. From 4631a3593853b2f75411b2fd06ff9e6173fb7e17 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 11 Jun 2025 14:02:00 +0530 Subject: [PATCH 2055/2176] docs: remove front-page warning about broken discrete systems --- docs/src/index.md | 7 ------- 1 file changed, 7 deletions(-) diff --git a/docs/src/index.md b/docs/src/index.md index 6050af538e..77eb4d5423 100644 --- a/docs/src/index.md +++ b/docs/src/index.md @@ -42,13 +42,6 @@ If you use ModelingToolkit in your work, please cite the following: for a summary of the changes. Some documentation pages may be broken while downstram packages update to the new version. -!!! danger "Temporarily broken discrete systems" - - ModelingToolkit's support for purely explicit systems of discrete update equations - (ones solved via `OrdinaryDiffEqFunctionMap.jl`) is temporarily broken. While such - systems can be created, simplfied and solved there are issues with the naming of - simplified unknowns and symbolic indexing of the problem/solution. - ModelingToolkit.jl is a symbolic-numeric modeling package. Thus it combines some of the features from symbolic computing packages like SymPy or Mathematica with the ideas of equation-based modeling systems like the causal Simulink and the From 2a71e8f361f041001044874d3da38bfbedced2f2 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 11 Jun 2025 14:02:14 +0530 Subject: [PATCH 2056/2176] docs: improve documentation for utility system constructors --- src/systems/system.jl | 108 ++++++++++++++++++++++++++++++++++++++---- 1 file changed, 98 insertions(+), 10 deletions(-) diff --git a/src/systems/system.jl b/src/systems/system.jl index cb330b382f..af721a6ff3 100644 --- a/src/systems/system.jl +++ b/src/systems/system.jl @@ -826,9 +826,19 @@ end """ $(TYPEDSIGNATURES) -Convert a time-dependent system `sys` to a time-independent system of nonlinear -equations that solve for the steady state of the system where `D(x)` is zero for -each continuous variable `x`. +Given a time-dependent system `sys` of ODEs, convert it to a time-independent system of +nonlinear equations that solve for the steady-state of the unknowns. This is done by +replacing every derivative `D(x)` of an unknown `x` with zero. Note that this process +does not retain noise equations, brownian terms, jumps or costs associated with `sys`. +All other information such as defaults, guesses, observed and initialization equations +are retained. The independent variable of `sys` becomes a parameter of the returned system. + +If `sys` is hierarchical (it contains subsystems) this transformation will be applied +recursively to all subsystems. The output system will be marked as `complete` if and only +if the input system is also `complete`. This also retains the `split` flag passed to +`complete`. + +See also: [`complete`](@ref). """ function NonlinearSystem(sys::System) if !is_time_dependent(sys) @@ -837,6 +847,9 @@ function NonlinearSystem(sys::System) eqs = equations(sys) obs = observed(sys) subrules = Dict([D(x) => 0.0 for x in unknowns(sys)]) + for var in brownians(sys) + subrules[var] = 0.0 + end eqs = map(eqs) do eq fast_substitute(eq, subrules) end @@ -856,30 +869,68 @@ end ######## """ - $(METHODLIST) + $(TYPEDSIGNATURES) + +Construct a time-independent [`System`](@ref) for optimizing the specified scalar `cost`. +The system will have no equations. -Construct a [`System`](@ref) to solve an optimization problem with the given scalar cost. +Unknowns and parameters of the system are inferred from the cost and other values (such as +defaults) passed to it. + +All keyword arguments are the same as those of the [`System`](@ref) constructor. """ function OptimizationSystem(cost; kwargs...) return System(Equation[]; costs = [cost], kwargs...) end +""" + $(TYPEDSIGNATURES) + +Identical to the corresponding single-argument `OptimizationSystem` constructor, except +the unknowns and parameters are specified by passing arrays of symbolic variables to `dvs` +and `ps` respectively. +""" function OptimizationSystem(cost, dvs, ps; kwargs...) return System(Equation[], nothing, dvs, ps; costs = [cost], kwargs...) end +""" + $(TYPEDSIGNATURES) + +Construct a time-independent [`System`](@ref) for optimizing the specified multi-objective +`cost`. The cost will be reduced to a scalar using the `consolidate` function. This +defaults to summing the specified cost and that of all subsystems. The system will have no +equations. + +Unknowns and parameters of the system are inferred from the cost and other values (such as +defaults) passed to it. + +All keyword arguments are the same as those of the [`System`](@ref) constructor. +""" function OptimizationSystem(cost::Array; kwargs...) return System(Equation[]; costs = vec(cost), kwargs...) end +""" + $(TYPEDSIGNATURES) + +Identical to the corresponding single-argument `OptimizationSystem` constructor, except +the unknowns and parameters are specified by passing arrays of symbolic variables to `dvs` +and `ps` respectively. +""" function OptimizationSystem(cost::Array, dvs, ps; kwargs...) return System(Equation[], nothing, dvs, ps; costs = vec(cost), kwargs...) end """ - $(METHODLIST) + $(TYPEDSIGNATURES) -Construct a [`System`](@ref) to solve a system of jump equations. +Construct a [`System`](@ref) to solve a system of jump equations. `jumps` is an array of +jumps, expressed using `JumpProcesses.MassActionJump`, `JumpProcesses.ConstantRateJump` +and `JumpProcesses.VariableRateJump`. It can also include standard equations to simulate +jump-diffusion processes. `iv` should be the independent variable of the system. + +All keyword arguments are the same as those of the [`System`](@ref) constructor. """ function JumpSystem(jumps, iv; kwargs...) mask = isa.(jumps, Equation) @@ -888,6 +939,12 @@ function JumpSystem(jumps, iv; kwargs...) return System(eqs, iv; jumps, kwargs...) end +""" + $(TYPEDSIGNATURES) + +Identical to the 2-argument `JumpSystem` constructor, but uses the explicitly provided +`dvs` and `ps` for unknowns and parameters of the system. +""" function JumpSystem(jumps, iv, dvs, ps; kwargs...) mask = isa.(jumps, Equation) eqs = Vector{Equation}(jumps[mask]) @@ -895,10 +952,29 @@ function JumpSystem(jumps, iv, dvs, ps; kwargs...) return System(eqs, iv, dvs, ps; jumps, kwargs...) end +# explicitly write the docstring to avoid mentioning `parameter_dependencies`. """ - $(METHODLIST) - -Construct a system of equations with associated noise terms. + SDESystem(eqs::Vector{Equation}, noise, iv; is_scalar_noise = false, kwargs...) + +Construct a system of equations with associated noise terms. Instead of specifying noise +using [`@brownians`](@ref) variables, it is specified using a noise matrix `noise`. `iv` is +the independent variable of the system. + +In the general case, `noise` should be a `N x M` matrix where `N` is the number of +equations (`length(eqs)`) and `M` is the number of independent random variables. +`noise[i, j]` is the diffusion term for equation `i` and random variable `j`. If the noise +is diagonal (`N == M` and `noise[i, j] == 0` for all `i != j`) it can be specified as a +`Vector` of length `N` corresponding to the diagonal of the noise matrix. As a special +case, if all equations have the same noise then all rows of `noise` are identical. This +is known as "scalar noise". In this case, `noise` can be a `Vector` corresponding to the +repeated row and `is_scalar_noise` must be `true`. + +Note that systems created in this manner cannot be used hierarchically. This should only +be used to construct flattened systems. To use such a system hierarchically, it must be +converted to use brownian variables using [`noise_to_brownians`](@ref). [`mtkcompile`](@ref) +will automatically perform this conversion. + +All keyword arguments are the same as those of the [`System`](@ref) constructor. """ function SDESystem(eqs::Vector{Equation}, noise, iv; is_scalar_noise = false, parameter_dependencies = Equation[], kwargs...) @@ -912,6 +988,13 @@ function SDESystem(eqs::Vector{Equation}, noise, iv; is_scalar_noise = false, @set sys.parameter_dependencies = parameter_dependencies end +""" + SDESystem(eqs::Vector{Equation}, noise, iv, dvs, ps; is_scalar_noise = false, kwargs...) + + +Identical to the 3-argument `SDESystem` constructor, but uses the explicitly provided +`dvs` and `ps` for unknowns and parameters of the system. +""" function SDESystem( eqs::Vector{Equation}, noise, iv, dvs, ps; is_scalar_noise = false, parameter_dependencies = Equation[], kwargs...) @@ -925,6 +1008,11 @@ function SDESystem( @set sys.parameter_dependencies = parameter_dependencies end +""" + $(TYPEDSIGNATURES) + +Attach the given noise matrix `noise` to the system `sys`. +""" function SDESystem(sys::System, noise; kwargs...) SDESystem(equations(sys), noise, get_iv(sys); kwargs...) end From e38a335df4904faaf064d9d2615058d7bdec184b Mon Sep 17 00:00:00 2001 From: Christopher Rackauckas Date: Wed, 11 Jun 2025 09:03:51 +0000 Subject: [PATCH 2057/2176] Add Dynamic Optimization to the pages --- docs/pages.jl | 2 ++ 1 file changed, 2 insertions(+) diff --git a/docs/pages.jl b/docs/pages.jl index 6c0806b15a..e280834cfe 100644 --- a/docs/pages.jl +++ b/docs/pages.jl @@ -8,6 +8,7 @@ pages = [ "tutorials/modelingtoolkitize.md", "tutorials/programmatically_generating.md", "tutorials/stochastic_diffeq.md", + "tutorials/dynamic_optimization.md", "tutorials/discrete_system.md", "tutorials/parameter_identifiability.md", "tutorials/change_independent_variable.md", @@ -31,6 +32,7 @@ pages = [ "API/variables.md", "API/model_building.md", "API/problems.md", + "API/dynamic_opt.md", "API/codegen.md", "API/PDESystem.md"], "Basics" => Any[ From 6706407eb87c442cc1a4089e319fab4c756a8638 Mon Sep 17 00:00:00 2001 From: Fredrik Bagge Carlson Date: Thu, 27 Feb 2025 14:28:46 +0100 Subject: [PATCH 2058/2176] output extra information from linearization do not output success --- src/linearization.jl | 22 ++++++++++++++-------- test/linearize.jl | 5 +++-- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/src/linearization.jl b/src/linearization.jl index 88fe1d39d3..ca816f4ae8 100644 --- a/src/linearization.jl +++ b/src/linearization.jl @@ -275,7 +275,7 @@ end """ $(TYPEDSIGNATURES) -Linearize the wrapped system at the point given by `(u, p, t)`. +Linearize the wrapped system at the point given by `(unknowns, p, t)`. """ function (linfun::LinearizationFunction)(u, p, t) if eltype(p) <: Pair @@ -301,7 +301,7 @@ function (linfun::LinearizationFunction)(u, p, t) linfun.prob, integ, fun, linfun.initializealg, Val(true); linfun.initialize_kwargs...) if !success - error("Initialization algorithm $(linfun.initializealg) failed with `u = $u` and `p = $p`.") + error("Initialization algorithm $(linfun.initializealg) failed with `unknowns = $u` and `p = $p`.") end fg_xz = linfun.uf_jac(u, DI.Constant(p), DI.Constant(t)) h_xz = linfun.h_jac(u, DI.Constant(p), DI.Constant(t)) @@ -323,7 +323,10 @@ function (linfun::LinearizationFunction)(u, p, t) g_u = fg_u[linfun.alge_idxs, :], h_x = h_xz[:, linfun.diff_idxs], h_z = h_xz[:, linfun.alge_idxs], - h_u = h_u) + h_u = h_u, + x = u, + p, + t) end """ @@ -436,7 +439,7 @@ function CommonSolve.solve(prob::LinearizationProblem; allow_input_derivatives = p = parameter_values(prob) t = current_time(prob) linres = prob.f(u0, p, t) - f_x, f_z, g_x, g_z, f_u, g_u, h_x, h_z, h_u = linres + f_x, f_z, g_x, g_z, f_u, g_u, h_x, h_z, h_u, x, p, t = linres nx, nu = size(f_u) nz = size(f_z, 2) @@ -473,7 +476,7 @@ function CommonSolve.solve(prob::LinearizationProblem; allow_input_derivatives = end end - (; A, B, C, D) + (; A, B, C, D), (; x, p, t) end """ @@ -618,8 +621,8 @@ function markio!(state, orig_inputs, inputs, outputs, disturbances; check = true end """ - (; A, B, C, D), simplified_sys = linearize(sys, inputs, outputs; t=0.0, op = Dict(), allow_input_derivatives = false, zero_dummy_der=false, kwargs...) - (; A, B, C, D) = linearize(simplified_sys, lin_fun; t=0.0, op = Dict(), allow_input_derivatives = false, zero_dummy_der=false) + (; A, B, C, D), simplified_sys, extras = linearize(sys, inputs, outputs; t=0.0, op = Dict(), allow_input_derivatives = false, zero_dummy_der=false, kwargs...) + (; A, B, C, D), extras = linearize(simplified_sys, lin_fun; t=0.0, op = Dict(), allow_input_derivatives = false, zero_dummy_der=false) Linearize `sys` between `inputs` and `outputs`, both vectors of variables. Return a NamedTuple with the matrices of a linear statespace representation on the form @@ -641,6 +644,8 @@ If `allow_input_derivatives = false`, an error will be thrown if input derivativ `zero_dummy_der` can be set to automatically set the operating point to zero for all dummy derivatives. +The return value `extras` is a NamedTuple `(; x, p, t)` containing the result of the initialization problem that was solved to determine the operating point. + See also [`linearization_function`](@ref) which provides a lower-level interface, [`linearize_symbolic`](@ref) and [`ModelingToolkit.reorder_unknowns`](@ref). See extended help for an example. @@ -750,7 +755,8 @@ function linearize(sys, inputs, outputs; op = Dict(), t = 0.0, zero_dummy_der, op, kwargs...) - linearize(ssys, lin_fun; op, t, allow_input_derivatives), ssys + mats, extras = linearize(ssys, lin_fun; op, t, allow_input_derivatives) + mats, ssys, extras end """ diff --git a/test/linearize.jl b/test/linearize.jl index ddb90f3eac..f9b050c4d3 100644 --- a/test/linearize.jl +++ b/test/linearize.jl @@ -14,15 +14,16 @@ eqs = [u ~ kp * (r - y) @named sys = System(eqs, t) -lsys, ssys = linearize(sys, [r], [y]) +lsys, ssys, extras = linearize(sys, [r], [y]) lprob = LinearizationProblem(sys, [r], [y]) -lsys2 = solve(lprob) +lsys2, extras2 = solve(lprob) lsys3, _ = linearize(sys, [r], [y]; autodiff = AutoFiniteDiff()) @test lsys.A[] == lsys2.A[] == lsys3.A[] == -2 @test lsys.B[] == lsys2.B[] == lsys3.B[] == 1 @test lsys.C[] == lsys2.C[] == lsys3.C[] == 1 @test lsys.D[] == lsys2.D[] == lsys3.D[] == 0 +@test extras == extras2 lsys, ssys = linearize(sys, [r], [r]) From 2eedd0a233b3fa632aafefe735f15273eefe125a Mon Sep 17 00:00:00 2001 From: Fredrik Bagge Carlson Date: Wed, 12 Mar 2025 06:29:48 +0100 Subject: [PATCH 2059/2176] handle extra arg in analysis functions --- src/systems/analysis_points.jl | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/systems/analysis_points.jl b/src/systems/analysis_points.jl index 3b65dd6669..caa51f0740 100644 --- a/src/systems/analysis_points.jl +++ b/src/systems/analysis_points.jl @@ -908,7 +908,8 @@ for f in [:get_sensitivity, :get_comp_sensitivity, :get_looptransfer] sys, ap, args...; loop_openings = [], system_modifier = identity, kwargs...) lin_fun, ssys = $(utility_fun)( sys, ap, args...; loop_openings, system_modifier, kwargs...) - ModelingToolkit.linearize(ssys, lin_fun), ssys + mats, extras = ModelingToolkit.linearize(ssys, lin_fun) + mats, ssys, extras end end From 77dad69cd7d4fc75509169bb69a89008f0456e43 Mon Sep 17 00:00:00 2001 From: Fredrik Bagge Carlson Date: Wed, 12 Mar 2025 12:12:20 +0100 Subject: [PATCH 2060/2176] test op change --- test/downstream/inversemodel.jl | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/test/downstream/inversemodel.jl b/test/downstream/inversemodel.jl index 66da2d2ea9..2b1e067847 100644 --- a/test/downstream/inversemodel.jl +++ b/test/downstream/inversemodel.jl @@ -132,13 +132,13 @@ sol = solve(prob, Rodas5P()) # we need to provide `op` so the initialization system knows what to hold constant # the values don't matter -Sf, simplified_sys = Blocks.get_sensitivity_function(model, :y; op); # This should work without providing an operating opint containing a dummy derivative +Sf, simplified_sys = get_sensitivity_function(model, :y; op); # This should work without providing an operating opint containing a dummy derivative x = state_values(Sf) p = parameter_values(Sf) # If this somehow passes, mention it on # https://github.com/SciML/ModelingToolkit.jl/issues/2786 matrices1 = Sf(x, p, 0) -matrices2, _ = Blocks.get_sensitivity(model, :y; op); # Test that we get the same result when calling the higher-level API +matrices2, _ = get_sensitivity(model, :y; op); # Test that we get the same result when calling the higher-level API @test matrices1.f_x ≈ matrices2.A[1:6, 1:6] nsys = get_named_sensitivity(model, :y; op) # Test that we get the same result when calling an even higher-level API @test matrices2.A ≈ nsys.A @@ -146,14 +146,14 @@ nsys = get_named_sensitivity(model, :y; op) # Test that we get the same result w # Test the same thing for comp sensitivities # This should work without providing an operating opint containing a dummy derivative -Sf, simplified_sys = Blocks.get_comp_sensitivity_function(model, :y; op); +Sf, simplified_sys = get_comp_sensitivity_function(model, :y; op); x = state_values(Sf) p = parameter_values(Sf) # If this somehow passes, mention it on # https://github.com/SciML/ModelingToolkit.jl/issues/2786 matrices1 = Sf(x, p, 0) # Test that we get the same result when calling the higher-level API -matrices2, _ = Blocks.get_comp_sensitivity(model, :y; op) +matrices2, _ = get_comp_sensitivity(model, :y; op) @test matrices1.f_x ≈ matrices2.A[1:6, 1:6] # Test that we get the same result when calling an even higher-level API nsys = get_named_comp_sensitivity(model, :y; op) @@ -173,15 +173,16 @@ nsys = get_named_comp_sensitivity(model, :y; op) output = :y # we need to provide `op` so the initialization system knows which # values to hold constant - lin_fun, ssys = Blocks.get_sensitivity_function(model, output; op = op1) - matrices1 = linearize(ssys, lin_fun, op = op1) - matrices2 = linearize(ssys, lin_fun, op = op2) + lin_fun, ssys = get_sensitivity_function(model, output; op = op1) + matrices1, extras1 = linearize(ssys, lin_fun, op = op1) + matrices2, extras2 = linearize(ssys, lin_fun, op = op2) + @test extras1.x != extras2.x S1f = ss(matrices1...) S2f = ss(matrices2...) @test S1f != S2f - matrices1, ssys = Blocks.get_sensitivity(model, output; op = op1) - matrices2, ssys = Blocks.get_sensitivity(model, output; op = op2) + matrices1, ssys = get_sensitivity(model, output; op = op1) + matrices2, ssys = get_sensitivity(model, output; op = op2) S1 = ss(matrices1...) S2 = ss(matrices2...) @test S1 != S2 From 0884e59cc855691601be01da8d5d82a964843773 Mon Sep 17 00:00:00 2001 From: Christopher Rackauckas Date: Wed, 11 Jun 2025 13:12:31 +0000 Subject: [PATCH 2061/2176] Update src/systems/diffeqs/basic_transformations.jl --- src/systems/diffeqs/basic_transformations.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/systems/diffeqs/basic_transformations.jl b/src/systems/diffeqs/basic_transformations.jl index c779d1b64b..e2b49808be 100644 --- a/src/systems/diffeqs/basic_transformations.jl +++ b/src/systems/diffeqs/basic_transformations.jl @@ -117,7 +117,7 @@ function changeofvariables( neqs = [neqs[i,:] for i in 1:size(neqs,1)] brownvars = map([Symbol(:B, :_, i) for i in 1:length(neqs[1])]) do name - unwrap(only(@brownian $name)) + unwrap(only(@brownians $name)) end else isSDE = true From 70a5b3975cde4279f16dadb713531af58180032c Mon Sep 17 00:00:00 2001 From: Christopher Rackauckas Date: Wed, 11 Jun 2025 22:40:53 +0000 Subject: [PATCH 2062/2176] Update Project.toml --- Project.toml | 2 -- 1 file changed, 2 deletions(-) diff --git a/Project.toml b/Project.toml index e9ea4cd50e..0f9789472b 100644 --- a/Project.toml +++ b/Project.toml @@ -164,8 +164,6 @@ StochasticDiffEq = "6.72.1" SymbolicIndexingInterface = "0.3.39" SymbolicUtils = "3.26.1" Symbolics = "6.40" -TermInterface = "2.0.0" -Test = "1.11.0" URIs = "1" UnPack = "0.1, 1.0" Unitful = "1.1" From 2d5c9e7128f904fe6d710177580be07ee1e7114b Mon Sep 17 00:00:00 2001 From: Christopher Rackauckas Date: Wed, 11 Jun 2025 22:41:04 +0000 Subject: [PATCH 2063/2176] Update Project.toml --- Project.toml | 2 -- 1 file changed, 2 deletions(-) diff --git a/Project.toml b/Project.toml index 0f9789472b..67d54f1757 100644 --- a/Project.toml +++ b/Project.toml @@ -65,8 +65,6 @@ StochasticDiffEq = "789caeaf-c7a9-5a7d-9973-96adeb23e2a0" SymbolicIndexingInterface = "2efcf032-c050-4f8e-a9bb-153293bab1f5" SymbolicUtils = "d1185830-fcd6-423d-90d6-eec64667417b" Symbolics = "0c5d862f-8b57-4792-8d23-62f2024744c7" -TermInterface = "8ea1fca8-c5ef-4a55-8b96-4e9afe9c9a3c" -Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" URIs = "5c2747f8-b7ea-4ff2-ba2e-563bfd36b1d4" UnPack = "3a884ed6-31ef-47d7-9d2a-63182c4928ed" Unitful = "1986cc42-f94f-5a68-af5c-568840ba703d" From 2b32ad48e15e15abd408383f4d3b9c0afec6dbaf Mon Sep 17 00:00:00 2001 From: Christopher Rackauckas Date: Wed, 11 Jun 2025 22:41:11 +0000 Subject: [PATCH 2064/2176] Update Project.toml --- Project.toml | 1 - 1 file changed, 1 deletion(-) diff --git a/Project.toml b/Project.toml index 67d54f1757..37bf660098 100644 --- a/Project.toml +++ b/Project.toml @@ -46,7 +46,6 @@ OffsetArrays = "6fe1bfb0-de20-5000-8ca7-80f57d26f881" OrderedCollections = "bac558e1-5e72-5ebc-8fee-abe8a469f55d" OrdinaryDiffEq = "1dea7af3-3e70-54e6-95c3-0bf5283fa5ed" OrdinaryDiffEqCore = "bbf590c4-e513-4bbe-9b18-05decba2e5d8" -OrdinaryDiffEqDefault = "50262376-6c5a-4cf5-baba-aaf4f84d72d7" PrecompileTools = "aea7be01-6a6a-4083-8856-8a6e6704d82a" RecursiveArrayTools = "731186ca-8d62-57ce-b412-fbd966d074cd" Reexport = "189a3867-3050-52da-a836-e630ba90ab69" From 2b4864858aa9ba28059e9dabbb8bcbbd5a511537 Mon Sep 17 00:00:00 2001 From: Christopher Rackauckas Date: Wed, 11 Jun 2025 22:41:24 +0000 Subject: [PATCH 2065/2176] Update Project.toml --- Project.toml | 1 - 1 file changed, 1 deletion(-) diff --git a/Project.toml b/Project.toml index 37bf660098..201436fa19 100644 --- a/Project.toml +++ b/Project.toml @@ -60,7 +60,6 @@ SimpleNonlinearSolve = "727e6d20-b764-4bd8-a329-72de5adea6c7" SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf" SpecialFunctions = "276daf66-3868-5448-9aa4-cd146d93841b" StaticArrays = "90137ffa-7385-5640-81b9-e52037218182" -StochasticDiffEq = "789caeaf-c7a9-5a7d-9973-96adeb23e2a0" SymbolicIndexingInterface = "2efcf032-c050-4f8e-a9bb-153293bab1f5" SymbolicUtils = "d1185830-fcd6-423d-90d6-eec64667417b" Symbolics = "0c5d862f-8b57-4792-8d23-62f2024744c7" From 07affe6eec93afa8ff255280c8f3f782be4e08af Mon Sep 17 00:00:00 2001 From: Christopher Rackauckas Date: Wed, 11 Jun 2025 22:41:32 +0000 Subject: [PATCH 2066/2176] Update Project.toml --- Project.toml | 1 - 1 file changed, 1 deletion(-) diff --git a/Project.toml b/Project.toml index 201436fa19..9645a412b0 100644 --- a/Project.toml +++ b/Project.toml @@ -44,7 +44,6 @@ NaNMath = "77ba4419-2d1f-58cd-9bb1-8ffee604a2e3" NonlinearSolve = "8913a72c-1f9b-4ce2-8d82-65094dcecaec" OffsetArrays = "6fe1bfb0-de20-5000-8ca7-80f57d26f881" OrderedCollections = "bac558e1-5e72-5ebc-8fee-abe8a469f55d" -OrdinaryDiffEq = "1dea7af3-3e70-54e6-95c3-0bf5283fa5ed" OrdinaryDiffEqCore = "bbf590c4-e513-4bbe-9b18-05decba2e5d8" PrecompileTools = "aea7be01-6a6a-4083-8856-8a6e6704d82a" RecursiveArrayTools = "731186ca-8d62-57ce-b412-fbd966d074cd" From 7d5e9155d40f57dcd5bf4b72cfcca5efc21890ad Mon Sep 17 00:00:00 2001 From: Christopher Rackauckas Date: Wed, 11 Jun 2025 22:42:17 +0000 Subject: [PATCH 2067/2176] Update src/ModelingToolkit.jl --- src/ModelingToolkit.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ModelingToolkit.jl b/src/ModelingToolkit.jl index 294010e831..8aaecaa6a3 100644 --- a/src/ModelingToolkit.jl +++ b/src/ModelingToolkit.jl @@ -297,7 +297,7 @@ export isinput, isoutput, getbounds, hasbounds, getguess, hasguess, isdisturbanc hasunit, getunit, hasconnect, getconnect, hasmisc, getmisc, state_priority export liouville_transform, change_independent_variable, substitute_component, - add_accumulations, noise_to_brownians, Girsanov_transform, changeofvariables, change_of_variable_SDE + add_accumulations, noise_to_brownians, Girsanov_transform, changeofvariables export PDESystem export Differential, expand_derivatives, @derivatives export Equation, ConstrainedEquation From 4d46b155e634ae9007aae3d8eae6da7a818d1b31 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 9 Jun 2025 23:43:31 +0530 Subject: [PATCH 2068/2176] fix: use `full_equations` in `islinear` and `isaffine` --- src/systems/abstractsystem.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index 1d92416455..e643be904d 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -1780,13 +1780,13 @@ function preface(sys::AbstractSystem) end function islinear(sys::AbstractSystem) - rhs = [eq.rhs for eq in equations(sys)] + rhs = [eq.rhs for eq in full_equations(sys)] all(islinear(r, unknowns(sys)) for r in rhs) end function isaffine(sys::AbstractSystem) - rhs = [eq.rhs for eq in equations(sys)] + rhs = [eq.rhs for eq in full_equations(sys)] all(isaffine(r, unknowns(sys)) for r in rhs) end From 31d9cdbae7e58f6b6c0a1ded78b17b60996bbc99 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 9 Jun 2025 23:43:49 +0530 Subject: [PATCH 2069/2176] fix: fix support for symbolic `u0` in `varmap_to_vars` --- src/systems/problem_utils.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/systems/problem_utils.jl b/src/systems/problem_utils.jl index 4ec875c83e..0aeb6dd048 100644 --- a/src/systems/problem_utils.jl +++ b/src/systems/problem_utils.jl @@ -376,7 +376,7 @@ function varmap_to_vars(varmap::AbstractDict, vars::Vector; if toterm !== nothing add_toterms!(varmap; toterm) end - if check + if check && !allow_symbolic missing_vars = missingvars(varmap, vars; toterm) if !isempty(missing_vars) if is_initializeprob @@ -387,7 +387,7 @@ function varmap_to_vars(varmap::AbstractDict, vars::Vector; end end evaluate_varmap!(varmap, vars; limit = substitution_limit) - vals = map(x -> varmap[x], vars) + vals = map(x -> get(varmap, x, x), vars) if !allow_symbolic missingsyms = Any[] missingvals = Any[] From 3719c68345afb096e7178804262885360d92a1e8 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 10 Jun 2025 16:33:54 +0530 Subject: [PATCH 2070/2176] refactor: modularize `process_SciMLProblem` a bit more --- src/systems/problem_utils.jl | 68 ++++++++++++++++++++++++++++-------- 1 file changed, 53 insertions(+), 15 deletions(-) diff --git a/src/systems/problem_utils.jl b/src/systems/problem_utils.jl index 0aeb6dd048..e24f37331f 100644 --- a/src/systems/problem_utils.jl +++ b/src/systems/problem_utils.jl @@ -1192,6 +1192,20 @@ function float_type_from_varmap(varmap, floatT = Bool) return float(floatT) end +""" + $(TYPEDSIGNATURES) + +Calculate the floating point type to use from the given `varmap` by looking at variables +with a constant value. `u0Type` takes priority if it is a real-valued array type. +""" +function calculate_float_type(varmap, u0Type::Type, floatT = Bool) + if u0Type <: AbstractArray && eltype(u0Type) <: Real && eltype(u0Type) != Union{} + return float(eltype(u0Type)) + else + return float_type_from_varmap(varmap, floatT) + end +end + """ $(TYPEDSIGNATURES) @@ -1208,6 +1222,41 @@ function calculate_resid_prototype(N::Int, u0, p) return zeros(u0ElType, N) end +""" + $(TYPEDSIGNATURES) + +Given the user-provided value of `u0_constructor`, the container type of user-provided +`op`, the desired floating point type and whether a symbolic `u0` is allowed, return the +updated `u0_constructor`. +""" +function get_u0_constructor(u0_constructor, u0Type::Type, floatT::Type, symbolic_u0::Bool) + u0_constructor === identity || return u0_constructor + u0Type <: StaticArray || return u0_constructor + return function (vals) + elT = if symbolic_u0 && any(x -> symbolic_type(x) != NotSymbolic(), vals) + nothing + else + floatT + end + SymbolicUtils.Code.create_array(u0Type, elT, Val(1), Val(length(vals)), vals...) + end +end + +""" + $(TYPEDSIGNATURES) + +Given the user-provided value of `p_constructor`, the container type of user-provided `op`, +ans the desired floating point type, return the updated `p_constructor`. +""" +function get_p_constructor(p_constructor, pType::Type, floatT::Type) + p_constructor === identity || return p_constructor + pType <: StaticArray || return p_constructor + return function (vals) + SymbolicUtils.Code.create_array( + pType, floatT, Val(ndims(vals)), Val(size(vals)), vals...) + end +end + """ $(TYPEDSIGNATURES) @@ -1274,26 +1323,15 @@ function process_SciMLProblem( missing_unknowns, missing_pars = build_operating_point!(sys, op, u0map, pmap, defs, dvs, ps) - floatT = Bool - if u0Type <: AbstractArray && eltype(u0Type) <: Real && eltype(u0Type) != Union{} - floatT = float(eltype(u0Type)) - else - floatT = float_type_from_varmap(op, floatT) - end - + floatT = calculate_float_type(op, u0Type) u0_eltype = something(u0_eltype, floatT) if !is_time_dependent(sys) || is_initializesystem(sys) add_observed_equations!(op, obs) end - if u0_constructor === identity && u0Type <: StaticArray - u0_constructor = vals -> SymbolicUtils.Code.create_array( - u0Type, floatT, Val(1), Val(length(vals)), vals...) - end - if p_constructor === identity && pType <: StaticArray - p_constructor = vals -> SymbolicUtils.Code.create_array( - pType, floatT, Val(1), Val(length(vals)), vals...) - end + + u0_constructor = get_u0_constructor(u0_constructor, u0Type, u0_eltype, symbolic_u0) + p_constructor = get_p_constructor(p_constructor, pType, floatT) if build_initializeprob kws = maybe_build_initialization_problem( From 696bab0bb3ade76398750c743fb693691a244dd4 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 9 Jun 2025 23:44:01 +0530 Subject: [PATCH 2071/2176] feat: add `LinearProblem` codegen --- src/ModelingToolkit.jl | 1 + src/problems/compatibility.jl | 9 ++++ src/problems/docs.jl | 29 +++++++++++ src/problems/linearproblem.jl | 98 +++++++++++++++++++++++++++++++++++ src/systems/codegen.jl | 87 +++++++++++++++++++++++++++++++ 5 files changed, 224 insertions(+) create mode 100644 src/problems/linearproblem.jl diff --git a/src/ModelingToolkit.jl b/src/ModelingToolkit.jl index 0a7fdd5209..de360d624d 100644 --- a/src/ModelingToolkit.jl +++ b/src/ModelingToolkit.jl @@ -188,6 +188,7 @@ include("problems/jumpproblem.jl") include("problems/initializationproblem.jl") include("problems/sccnonlinearproblem.jl") include("problems/bvproblem.jl") +include("problems/linearproblem.jl") include("modelingtoolkitize/common.jl") include("modelingtoolkitize/odeproblem.jl") diff --git a/src/problems/compatibility.jl b/src/problems/compatibility.jl index e5608ce4f1..9d5abf926e 100644 --- a/src/problems/compatibility.jl +++ b/src/problems/compatibility.jl @@ -169,3 +169,12 @@ function check_no_equations(sys::System, T) """)) end end + +function check_affine(sys::System, T) + if !isaffine(sys) + throw(SystemCompatibilityError(""" + A non-affine system cannot be used to construct a `$T`. Consider a + `NonlinearProblem` instead. + """)) + end +end diff --git a/src/problems/docs.jl b/src/problems/docs.jl index 94b77b8772..17bc2c83c6 100644 --- a/src/problems/docs.jl +++ b/src/problems/docs.jl @@ -391,3 +391,32 @@ $PROBLEM_INTERNALS_HEADER $PROBLEM_INTERNAL_KWARGS """ SciMLBase.IntervalNonlinearProblem + +@doc """ + SciMLBase.LinearProblem(sys::System, op; kwargs...) + SciMLBase.LinearProblem{iip}(sys::System, op; kwargs...) + +Build a `LinearProblem` given a system `sys` and operating point `op`. `iip` is a boolean +indicating whether the problem should be in-place. The operating point should be an +iterable collection of key-value pairs mapping variables/parameters in the system to the +(initial) values they should take in `LinearProblem`. Any values not provided will +fallback to the corresponding default (if present). + +Note that since `u0` is optional for `LinearProblem`, values of unknowns do not need to be +specified in `op` to create a `LinearProblem`. In such a case, `prob.u0` will be `nothing` +and attempting to symbolically index the problem with an unknown, observable, or expression +depending on unknowns/observables will error. + +Updating the parameters automatically updates the `A` and `b` arrays. + +# Keyword arguments + +$PROBLEM_KWARGS +$(prob_fun_common_kwargs(LinearProblem, false)) + +All other keyword arguments are forwarded to the $func constructor. + +$PROBLEM_INTERNALS_HEADER + +$PROBLEM_INTERNAL_KWARGS +""" SciMLBase.LinearProblem diff --git a/src/problems/linearproblem.jl b/src/problems/linearproblem.jl new file mode 100644 index 0000000000..26d5c932bd --- /dev/null +++ b/src/problems/linearproblem.jl @@ -0,0 +1,98 @@ +function SciMLBase.LinearProblem(sys::System, op; kwargs...) + SciMLBase.LinearProblem{true}(sys, op; kwargs...) +end + +function SciMLBase.LinearProblem(sys::System, op::StaticArray; kwargs...) + SciMLBase.LinearProblem{false}(sys, op; kwargs...) +end + +function SciMLBase.LinearProblem{iip}( + sys::System, op; check_length = true, expression = Val{false}, + check_compatibility = true, sparse = false, eval_expression = false, + eval_module = @__MODULE__, checkbounds = false, cse = true, + u0_constructor = identity, u0_eltype = nothing, kwargs...) where {iip} + check_complete(sys, LinearProblem) + check_compatibility && check_compatible_system(LinearProblem, sys) + + _, u0, p = process_SciMLProblem( + EmptySciMLFunction{iip}, sys, op; check_length, expression, + build_initializeprob = false, symbolic_u0 = true, u0_constructor, u0_eltype, + kwargs...) + + if any(x -> symbolic_type(x) != NotSymbolic(), u0) + u0 = nothing + end + + u0Type = typeof(op) + floatT = if u0 === nothing + calculate_float_type(op, u0Type) + else + eltype(u0) + end + u0_eltype = something(u0_eltype, floatT) + + u0_constructor = get_p_constructor(u0_constructor, u0Type, u0_eltype) + + A, b = calculate_A_b(sys; sparse) + update_A = generate_update_A(sys, A; expression, wrap_gfw = Val{true}, eval_expression, + eval_module, checkbounds, cse, kwargs...) + update_b = generate_update_b(sys, b; expression, wrap_gfw = Val{true}, eval_expression, + eval_module, checkbounds, cse, kwargs...) + observedfun = ObservedFunctionCache( + sys; steady_state = false, expression, eval_expression, eval_module, checkbounds, + cse) + + if expression == Val{true} + symbolic_interface = quote + update_A = $update_A + update_b = $update_b + sys = $sys + observedfun = $observedfun + $(SciMLBase.SymbolicLinearInterface)( + update_A, update_b, sys, observedfun, nothing) + end + get_A = build_explicit_observed_function( + sys, A; param_only = true, eval_expression, eval_module) + if sparse + get_A = SparseArrays.sparse ∘ get_A + end + get_b = build_explicit_observed_function( + sys, b; param_only = true, eval_expression, eval_module) + A = u0_constructor(get_A(p)) + b = u0_constructor(get_b(p)) + else + symbolic_interface = SciMLBase.SymbolicLinearInterface( + update_A, update_b, sys, observedfun, nothing) + A = u0_constructor(update_A(p)) + b = u0_constructor(update_b(p)) + end + + kwargs = (; u0, process_kwargs(sys; kwargs...)..., f = symbolic_interface) + args = (; A, b, p) + + return maybe_codegen_scimlproblem(expression, LinearProblem{iip}, args; kwargs...) +end + +# For remake +function SciMLBase.get_new_A_b( + sys::AbstractSystem, f::SciMLBase.SymbolicLinearInterface, p, A, b; kw...) + if ArrayInterface.ismutable(A) + f.update_A!(A, p) + f.update_b!(b, p) + else + # The generated function has both IIP and OOP variants + A = StaticArraysCore.similar_type(A)(f.update_A!(p)) + b = StaticArraysCore.similar_type(b)(f.update_b!(p)) + end + return A, b +end + +function check_compatible_system(T::Type{LinearProblem}, sys::System) + check_time_independent(sys, T) + check_affine(sys, T) + check_not_dde(sys) + check_no_cost(sys, T) + check_no_constraints(sys, T) + check_no_jumps(sys, T) + check_no_noise(sys, T) +end diff --git a/src/systems/codegen.jl b/src/systems/codegen.jl index 96ff14f58a..4a68f935e8 100644 --- a/src/systems/codegen.jl +++ b/src/systems/codegen.jl @@ -1130,3 +1130,90 @@ function build_explicit_observed_function(sys, ts; return f end end + +""" + $(TYPEDSIGNATURES) + +Return matrix `A` and vector `b` such that the system `sys` can be represented as +`A * x = b` where `x` is `unknowns(sys)`. Errors if the system is not affine. + +# Keyword arguments + +- `sparse`: return a sparse `A`. +""" +function calculate_A_b(sys::System; sparse = false) + rhss = [eq.rhs for eq in full_equations(sys)] + dvs = unknowns(sys) + + A = Matrix{Any}(undef, length(rhss), length(dvs)) + b = Vector{Any}(undef, length(rhss)) + for (i, rhs) in enumerate(rhss) + # mtkcompile makes this `0 ~ rhs` which typically ends up giving + # unknowns negative coefficients. If given the equations `A * x ~ b` + # it will simplify to `0 ~ b - A * x`. Thus this negation usually leads + # to more comprehensible user API. + resid = -rhs + for (j, var) in enumerate(dvs) + p, q, islinear = Symbolics.linear_expansion(resid, var) + if !islinear + throw(ArgumentError("System is not linear. Equation $((0 ~ rhs)) is not linear in unknown $var.")) + end + A[i, j] = p + resid = q + end + # negate beucause `resid` is the residual on the LHS + b[i] = -resid + end + + @assert all(Base.Fix1(isassigned, A), eachindex(A)) + @assert all(Base.Fix1(isassigned, A), eachindex(b)) + + if sparse + A = SparseArrays.sparse(A) + end + return A, b +end + +""" + $(TYPEDSIGNATURES) + +Given a system `sys` and the `A` from [`calculate_A_b`](@ref) generate the function that +updates `A` given the parameter object. + +# Keyword arguments + +$GENERATE_X_KWARGS + +All other keyword arguments are forwarded to [`build_function_wrapper`](@ref). +""" +function generate_update_A(sys::System, A::AbstractMatrix; expression = Val{true}, + wrap_gfw = Val{false}, eval_expression = false, eval_module = @__MODULE__, kwargs...) + ps = reorder_parameters(sys) + + res = build_function_wrapper(sys, A, ps...; p_start = 1, expression = Val{true}, + similarto = typeof(A), kwargs...) + return maybe_compile_function(expression, wrap_gfw, (1, 1, is_split(sys)), res; + eval_expression, eval_module) +end + +""" + $(TYPEDSIGNATURES) + +Given a system `sys` and the `b` from [`calculate_A_b`](@ref) generate the function that +updates `b` given the parameter object. + +# Keyword arguments + +$GENERATE_X_KWARGS + +All other keyword arguments are forwarded to [`build_function_wrapper`](@ref). +""" +function generate_update_b(sys::System, b::AbstractVector; expression = Val{true}, + wrap_gfw = Val{false}, eval_expression = false, eval_module = @__MODULE__, kwargs...) + ps = reorder_parameters(sys) + + res = build_function_wrapper(sys, b, ps...; p_start = 1, expression = Val{true}, + similarto = typeof(b), kwargs...) + return maybe_compile_function(expression, wrap_gfw, (1, 1, is_split(sys)), res; + eval_expression, eval_module) +end From c2849c422ad0d4f986a3c6637ab918c50b4ca5fd Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 9 Jun 2025 23:44:10 +0530 Subject: [PATCH 2072/2176] docs: add `LinearProblem` and related codegen to docs --- docs/src/API/codegen.md | 3 +++ docs/src/API/problems.md | 3 ++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/docs/src/API/codegen.md b/docs/src/API/codegen.md index cd76d34522..4f31405174 100644 --- a/docs/src/API/codegen.md +++ b/docs/src/API/codegen.md @@ -21,6 +21,8 @@ ModelingToolkit.generate_constraint_hessian ModelingToolkit.generate_control_jacobian ModelingToolkit.build_explicit_observed_function ModelingToolkit.generate_control_function +ModelingToolkit.generate_update_A +ModelingToolkit.generate_update_b ``` For functions such as jacobian calculation which require symbolic computation, there @@ -42,6 +44,7 @@ ModelingToolkit.cost_hessian_sparsity ModelingToolkit.calculate_constraint_jacobian ModelingToolkit.calculate_constraint_hessian ModelingToolkit.calculate_control_jacobian +ModelingToolkit.calculate_A_b ``` All code generation eventually calls `build_function_wrapper`. diff --git a/docs/src/API/problems.md b/docs/src/API/problems.md index d308785d77..72147a7e09 100644 --- a/docs/src/API/problems.md +++ b/docs/src/API/problems.md @@ -29,7 +29,7 @@ SciMLBase.DiscreteProblem SciMLBase.ImplicitDiscreteProblem ``` -## Nonlinear systems +## Linear and Nonlinear systems ```@docs SciMLBase.NonlinearFunction @@ -41,6 +41,7 @@ SciMLBase.IntervalNonlinearFunction SciMLBase.IntervalNonlinearProblem ModelingToolkit.HomotopyContinuationProblem SciMLBase.HomotopyNonlinearFunction +SciMLBase.LinearProblem ``` ## Optimization and optimal control From 1c6188112c68d610f2b3cbc99208d751e5e27926 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 10 Jun 2025 16:33:01 +0530 Subject: [PATCH 2073/2176] refactor: early exit `late_binding_update_u0_p` for `LinearProblem` --- src/systems/nonlinear/initializesystem.jl | 1 + 1 file changed, 1 insertion(+) diff --git a/src/systems/nonlinear/initializesystem.jl b/src/systems/nonlinear/initializesystem.jl index eab33e00b3..93fa988b7d 100644 --- a/src/systems/nonlinear/initializesystem.jl +++ b/src/systems/nonlinear/initializesystem.jl @@ -727,6 +727,7 @@ function SciMLBase.late_binding_update_u0_p( prob, sys::AbstractSystem, u0, p, t0, newu0, newp) supports_initialization(sys) || return newu0, newp prob isa IntervalNonlinearProblem && return newu0, newp + prob isa LinearProblem && return newu0, newp initdata = prob.f.initialization_data meta = initdata === nothing ? nothing : initdata.metadata From 9fb0d828bf87d83e5bcb1631ce2dbf9655fd2630 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 10 Jun 2025 16:34:20 +0530 Subject: [PATCH 2074/2176] test: test `LinearProblem` codegen --- test/linearproblem.jl | 189 ++++++++++++++++++++++++++++++++++++++++++ test/runtests.jl | 1 + 2 files changed, 190 insertions(+) create mode 100644 test/linearproblem.jl diff --git a/test/linearproblem.jl b/test/linearproblem.jl new file mode 100644 index 0000000000..2c5c130a1b --- /dev/null +++ b/test/linearproblem.jl @@ -0,0 +1,189 @@ +using ModelingToolkit +using LinearSolve +using SciMLBase +using StaticArrays +using SparseArrays +using Test +using ModelingToolkit: t_nounits as t, D_nounits as D, SystemCompatibilityError + +@testset "Rejects non-affine systems" begin + @variables x y + @mtkbuild sys = System([0 ~ x^2 + y, 0 ~ x - y]) + @test_throws SystemCompatibilityError LinearProblem(sys, nothing) +end + +@variables x[1:3] [irreducible = true] +@parameters p[1:3, 1:3] q[1:3] + +@mtkbuild sys = System([p * x ~ q]) +# sanity check +@test length(unknowns(sys)) == length(equations(sys)) == 3 +A = Float64[1 2 3; 4 3.5 1.7; 5.2 1.8 9.7] +b = Float64[2, 5, 8] +ps = [p => A, q => b] + +@testset "Basics" begin + # Ensure it works without providing `u0` + prob = LinearProblem(sys, ps) + @test prob.u0 === nothing + @test SciMLBase.isinplace(prob) + @test prob.A ≈ A + @test prob.b ≈ b + @test eltype(prob.A) == Float64 + @test eltype(prob.b) == Float64 + + @test prob.ps[p * q] ≈ A * b + + sol = solve(prob) + # https://github.com/SciML/LinearSolve.jl/issues/532 + @test_broken SciMLBase.successful_retcode(sol) + @test prob.A * sol.u - prob.b≈zeros(3) atol=1e-10 + + A2 = rand(3, 3) + b2 = rand(3) + @testset "remake" begin + prob2 = remake(prob; p = [p => A2, q => b2]) + @test prob2.ps[p] ≈ A2 + @test prob2.ps[q] ≈ b2 + @test prob2.A ≈ A2 + @test prob2.b ≈ b2 + end + + prob.ps[p] = A2 + @test prob.A ≈ A2 + prob.ps[q] = b2 + @test prob.b ≈ b2 + A2[1, 1] = prob.ps[p[1, 1]] = 1.5 + @test prob.A ≈ A2 + b2[1] = prob.ps[q[1]] = 2.5 + @test prob.b ≈ b2 + + @testset "expression = Val{true}" begin + prob3e = LinearProblem(sys, ps; expression = Val{true}) + @test prob3e isa Expr + prob3 = eval(prob3e) + + @test prob3.u0 === nothing + @test SciMLBase.isinplace(prob3) + @test prob3.A ≈ A + @test prob3.b ≈ b + @test eltype(prob3.A) == Float64 + @test eltype(prob3.b) == Float64 + + @test prob3.ps[p * q] ≈ A * b + + sol = solve(prob3) + # https://github.com/SciML/LinearSolve.jl/issues/532 + @test_broken SciMLBase.successful_retcode(sol) + @test prob3.A * sol.u - prob3.b≈zeros(3) atol=1e-10 + end +end + +@testset "With `u0`" begin + prob = LinearProblem(sys, [x => ones(3); ps]) + @test prob.u0 ≈ ones(3) + @test SciMLBase.isinplace(prob) + @test eltype(prob.u0) == Float64 + + # Observed should work + @test prob[x[1] + x[2]] ≈ 2.0 + + @testset "expression = Val{true}" begin + prob3e = LinearProblem(sys, [x => ones(3); ps]; expression = Val{true}) + @test prob3e isa Expr + prob3 = eval(prob3e) + @test prob3.u0 ≈ ones(3) + @test eltype(prob3.u0) == Float64 + end +end + +@testset "SArray OOP form" begin + prob = LinearProblem(sys, SVector{2}(ps)) + @test prob.A isa SMatrix{3, 3, Float64} + @test prob.b isa SVector{3, Float64} + @test !SciMLBase.isinplace(prob) + @test prob.ps[p * q] ≈ A * b + + sol = solve(prob) + # https://github.com/SciML/LinearSolve.jl/issues/532 + @test_broken SciMLBase.successful_retcode(sol) + @test prob.A * sol.u - prob.b≈zeros(3) atol=1e-10 + + A2 = rand(3, 3) + b2 = rand(3) + @testset "remake" begin + prob2 = remake(prob; p = [p => A2, q => b2]) + # Despite passing `Array` to `remake` + @test prob2.A isa SMatrix{3, 3, Float64} + @test prob2.b isa SVector{3, Float64} + @test prob2.ps[p] ≈ A2 + @test prob2.ps[q] ≈ b2 + @test prob2.A ≈ A2 + @test prob2.b ≈ b2 + end + + @testset "expression = Val{true}" begin + prob3e = LinearProblem(sys, SVector{2}(ps); expression = Val{true}) + @test prob3e isa Expr + prob3 = eval(prob3e) + @test prob3.A isa SMatrix{3, 3, Float64} + @test prob3.b isa SVector{3, Float64} + @test !SciMLBase.isinplace(prob3) + @test prob3.ps[p * q] ≈ A * b + + sol = solve(prob3) + # https://github.com/SciML/LinearSolve.jl/issues/532 + @test_broken SciMLBase.successful_retcode(sol) + @test prob3.A * sol.u - prob3.b≈zeros(3) atol=1e-10 + end +end + +@testset "u0_constructor" begin + prob = LinearProblem{false}(sys, ps; u0_constructor = x -> SArray{Tuple{size(x)...}}(x)) + @test prob.A isa SMatrix{3, 3, Float64} + @test prob.b isa SVector{3, Float64} + @test prob.ps[p * q] ≈ A * b +end + +@testset "sparse form" begin + prob = LinearProblem(sys, ps; sparse = true) + @test issparse(prob.A) + @test !issparse(prob.b) + + sol = solve(prob) + # This might end up failing because of + # https://github.com/SciML/LinearSolve.jl/issues/532 + @test SciMLBase.successful_retcode(sol) + + A2 = rand(3, 3) + prob.ps[p] = A2 + @test prob.A ≈ A2 + b2 = rand(3) + prob.ps[q] = b2 + @test prob.b ≈ b2 + + A2 = rand(3, 3) + b2 = rand(3) + @testset "remake" begin + prob2 = remake(prob; p = [p => A2, q => b2]) + @test issparse(prob2.A) + @test !issparse(prob2.b) + @test prob2.ps[p] ≈ A2 + @test prob2.ps[q] ≈ b2 + @test prob2.A ≈ A2 + @test prob2.b ≈ b2 + end + + @testset "expression = Val{true}" begin + prob3e = LinearProblem(sys, ps; sparse = true, expression = Val{true}) + @test prob3e isa Expr + prob3 = eval(prob3e) + @test issparse(prob3.A) + @test !issparse(prob3.b) + + sol = solve(prob3) + # This might end up failing because of + # https://github.com/SciML/LinearSolve.jl/issues/532 + @test SciMLBase.successful_retcode(sol) + end +end diff --git a/test/runtests.jl b/test/runtests.jl index 31e4cc609f..c108c7898d 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -98,6 +98,7 @@ end @safetestset "Namespacing test" include("namespacing.jl") @safetestset "Subsystem replacement" include("substitute_component.jl") @safetestset "Linearization Tests" include("linearize.jl") + @safetestset "LinearProblem Tests" include("linearproblem.jl") end end From 0ed1b5f52449581c7105fe516c77d98a65f2ec16 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 10 Jun 2025 16:35:04 +0530 Subject: [PATCH 2075/2176] build: bump SciMLBase compat --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index eb7333ae47..836e11bc28 100644 --- a/Project.toml +++ b/Project.toml @@ -148,7 +148,7 @@ RecursiveArrayTools = "3.26" Reexport = "0.2, 1" RuntimeGeneratedFunctions = "0.5.9" SCCNonlinearSolve = "1.0.0" -SciMLBase = "2.91.1" +SciMLBase = "2.100.0" SciMLPublic = "1.0.0" SciMLStructures = "1.7" Serialization = "1" From 96609b33c00da03cfc9f508aba03ed783fa5ef5e Mon Sep 17 00:00:00 2001 From: Christopher Rackauckas Date: Thu, 12 Jun 2025 10:13:47 +0000 Subject: [PATCH 2076/2176] Update runtests.jl --- test/runtests.jl | 1 + 1 file changed, 1 insertion(+) diff --git a/test/runtests.jl b/test/runtests.jl index 31e4cc609f..75b23a8cda 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -47,6 +47,7 @@ end @safetestset "Error Handling" include("error_handling.jl") @safetestset "StructuralTransformations" include("structural_transformation/runtests.jl") @safetestset "Basic transformations" include("basic_transformations.jl") + @safetestset "Change of variables" include("changeofvariables.jl") @safetestset "State Selection Test" include("state_selection.jl") @safetestset "Symbolic Event Test" include("symbolic_events.jl") @safetestset "Stream Connect Test" include("stream_connectors.jl") From 8a299e01a9d022c908215fb770379bedbf3985a3 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 12 Jun 2025 15:51:56 +0530 Subject: [PATCH 2077/2176] build: add LinearSolve as test dependency --- Project.toml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 836e11bc28..faf9e4eaf9 100644 --- a/Project.toml +++ b/Project.toml @@ -129,6 +129,7 @@ LabelledArrays = "1.3" Latexify = "0.11, 0.12, 0.13, 0.14, 0.15, 0.16" Libdl = "1" LinearAlgebra = "1" +LinearSolve = "3" Logging = "1" MLStyle = "0.4.17" ModelingToolkitStandardLibrary = "2.20" @@ -180,6 +181,7 @@ ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210" Ipopt = "b6b21f68-93f8-5de0-b562-5493be1d77c9" Ipopt_jll = "9cc047cb-c261-5740-88fc-0cf96f7bdcc7" JET = "c3a54625-cd67-489e-a8e7-0a5a0ff4e31b" +LinearSolve = "7ed4a6bd-45f5-4d41-b270-4a48e9bafcae" Logging = "56ddb016-857b-54e1-b83d-db4d58db5568" ModelingToolkitStandardLibrary = "16a59e39-deab-5bd0-87e4-056b12336739" NonlinearSolve = "8913a72c-1f9b-4ce2-8d82-65094dcecaec" @@ -205,4 +207,4 @@ Sundials = "c3572dad-4567-51f8-b174-8c6c989267f4" Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" [targets] -test = ["AmplNLWriter", "BenchmarkTools", "BoundaryValueDiffEqMIRK", "BoundaryValueDiffEqAscher", "ControlSystemsBase", "DataInterpolations", "DelayDiffEq", "NonlinearSolve", "ForwardDiff", "Ipopt", "Ipopt_jll", "ModelingToolkitStandardLibrary", "Optimization", "OptimizationOptimJL", "OptimizationMOI", "OrdinaryDiffEq", "OrdinaryDiffEqCore", "OrdinaryDiffEqDefault", "REPL", "Random", "ReferenceTests", "SafeTestsets", "StableRNGs", "Statistics", "SteadyStateDiffEq", "Test", "StochasticDiffEq", "Sundials", "StochasticDelayDiffEq", "Pkg", "JET", "OrdinaryDiffEqNonlinearSolve", "Logging", "OptimizationBase"] +test = ["AmplNLWriter", "BenchmarkTools", "BoundaryValueDiffEqMIRK", "BoundaryValueDiffEqAscher", "ControlSystemsBase", "DataInterpolations", "DelayDiffEq", "NonlinearSolve", "ForwardDiff", "Ipopt", "Ipopt_jll", "ModelingToolkitStandardLibrary", "Optimization", "OptimizationOptimJL", "OptimizationMOI", "OrdinaryDiffEq", "OrdinaryDiffEqCore", "OrdinaryDiffEqDefault", "REPL", "Random", "ReferenceTests", "SafeTestsets", "StableRNGs", "Statistics", "SteadyStateDiffEq", "Test", "StochasticDiffEq", "Sundials", "StochasticDelayDiffEq", "Pkg", "JET", "OrdinaryDiffEqNonlinearSolve", "Logging", "OptimizationBase", "LinearSolve"] From c09883e5c5b02af0fb152f510959af4f84ccdfd9 Mon Sep 17 00:00:00 2001 From: fchen121 Date: Thu, 12 Jun 2025 10:46:52 -0400 Subject: [PATCH 2078/2176] Add change_of_variables function to documents --- docs/src/API/model_building.md | 1 + src/ModelingToolkit.jl | 2 +- src/systems/diffeqs/basic_transformations.jl | 17 ++++++++--------- test/changeofvariables.jl | 12 ++++++------ 4 files changed, 16 insertions(+), 16 deletions(-) diff --git a/docs/src/API/model_building.md b/docs/src/API/model_building.md index a72be8a4c6..cbdf72edae 100644 --- a/docs/src/API/model_building.md +++ b/docs/src/API/model_building.md @@ -196,6 +196,7 @@ symbolic analysis. ```@docs liouville_transform +change_of_variables stochastic_integral_transform Girsanov_transform change_independent_variable diff --git a/src/ModelingToolkit.jl b/src/ModelingToolkit.jl index 3b86a55548..fb21494bc4 100644 --- a/src/ModelingToolkit.jl +++ b/src/ModelingToolkit.jl @@ -297,7 +297,7 @@ export isinput, isoutput, getbounds, hasbounds, getguess, hasguess, isdisturbanc hasunit, getunit, hasconnect, getconnect, hasmisc, getmisc, state_priority export liouville_transform, change_independent_variable, substitute_component, - add_accumulations, noise_to_brownians, Girsanov_transform, changeofvariables + add_accumulations, noise_to_brownians, Girsanov_transform, change_of_variables export PDESystem export Differential, expand_derivatives, @derivatives export Equation, ConstrainedEquation diff --git a/src/systems/diffeqs/basic_transformations.jl b/src/systems/diffeqs/basic_transformations.jl index 65dc4177a0..9312966528 100644 --- a/src/systems/diffeqs/basic_transformations.jl +++ b/src/systems/diffeqs/basic_transformations.jl @@ -67,34 +67,33 @@ using ModelingToolkit, OrdinaryDiffEq, Test # Change of variables: z = log(x) # (this implies that x = exp(z) is automatically non-negative) -@parameters t α +@independent_variables t +@parameters α @variables x(t) D = Differential(t) eqs = [D(x) ~ α*x] tspan = (0., 1.) -u0 = [x => 1.0] -p = [α => -0.5] +def = [x => 1.0, α => -0.5] -@named sys = ODESystem(eqs; defaults=u0) -prob = ODEProblem(sys, [], tspan, p) +@mtkcompile sys = System(eqs, t;defaults=def) +prob = ODEProblem(sys, [], tspan) sol = solve(prob, Tsit5()) @variables z(t) forward_subs = [log(x) => z] backward_subs = [x => exp(z)] - -@named new_sys = changeofvariables(sys, forward_subs, backward_subs) +new_sys = change_of_variables(sys, t, forward_subs, backward_subs) @test equations(new_sys)[1] == (D(z) ~ α) -new_prob = ODEProblem(new_sys, [], tspan, p) +new_prob = ODEProblem(new_sys, [], tspan) new_sol = solve(new_prob, Tsit5()) @test isapprox(new_sol[x][end], sol[x][end], atol=1e-4) ``` """ -function changeofvariables( +function change_of_variables( sys::System, iv, forward_subs, backward_subs; simplify=true, t0=missing, isSDE=false ) diff --git a/test/changeofvariables.jl b/test/changeofvariables.jl index 776d44f7e7..abc773ed21 100644 --- a/test/changeofvariables.jl +++ b/test/changeofvariables.jl @@ -26,7 +26,7 @@ sol = solve(prob, Tsit5()) @variables z(t) forward_subs = [log(x) => z] backward_subs = [x => exp(z)] -new_sys = changeofvariables(sys, t, forward_subs, backward_subs) +new_sys = change_of_variables(sys, t, forward_subs, backward_subs) @test equations(new_sys)[1] == (D(z) ~ α) new_prob = ODEProblem(new_sys, [], tspan) @@ -48,7 +48,7 @@ def = [x=>1., α => 1.] forward_subs = [t + α/(x+t) => z ] backward_subs = [ x => α/(z-t) - t] -new_sys = changeofvariables(sys, t, forward_subs, backward_subs; simplify=true, t0=0.) +new_sys = change_of_variables(sys, t, forward_subs, backward_subs; simplify=true, t0=0.) # output should be equivalent to # t^2 + α - z^2 + 2 (but this simplification is not found automatically) @@ -86,7 +86,7 @@ z = reshape(z, 3, 1) forward_subs = vec(T_inv*x .=> z) backward_subs = vec(x .=> T*z) -new_sys = changeofvariables(sys, t, forward_subs, backward_subs; simplify=true) +new_sys = change_of_variables(sys, t, forward_subs, backward_subs; simplify=true) new_prob = ODEProblem(new_sys, [], tspan) new_sol = solve(new_prob, Tsit5()) @@ -115,7 +115,7 @@ def = [x=>0., μ => 2., σ=>1.] @mtkcompile sys = System(eqs, t; defaults=def) forward_subs = [log(x) => y] backward_subs = [x => exp(y)] -new_sys = changeofvariables(sys, t, forward_subs, backward_subs) +new_sys = change_of_variables(sys, t, forward_subs, backward_subs) @test equations(new_sys)[1] == (D(y) ~ μ - 1/2*σ^2) @test noise_eqs(new_sys)[1] === value(σ) @@ -131,7 +131,7 @@ forward_subs = [log(x) => z, y^2 => w, log(u) => v] backward_subs = [x => exp(z), y => w^.5, u => exp(v)] @mtkcompile sys = System(eqs, t; defaults=def) -new_sys = changeofvariables(sys, t, forward_subs, backward_subs) +new_sys = change_of_variables(sys, t, forward_subs, backward_subs) @test equations(new_sys)[1] == (D(z) ~ μ - 1/2*σ^2) @test equations(new_sys)[2] == (D(w) ~ α^2) @test equations(new_sys)[3] == (D(v) ~ μ - 1/2*(α^2 + σ^2)) @@ -144,7 +144,7 @@ new_sys = changeofvariables(sys, t, forward_subs, backward_subs) # Test for Brownian instead of noise @named sys = System(eqs, t; defaults=def) -new_sys = changeofvariables(sys, t, forward_subs, backward_subs; simplify=false) +new_sys = change_of_variables(sys, t, forward_subs, backward_subs; simplify=false) @test simplify(equations(new_sys)[1]) == simplify((D(z) ~ μ - 1/2*σ^2 + σ*Bx)) @test simplify(equations(new_sys)[2]) == simplify((D(w) ~ α^2 + 2*α*w^.5*By)) @test simplify(equations(new_sys)[3]) == simplify((D(v) ~ μ - 1/2*(α^2 + σ^2) + σ*Bx + α*By)) \ No newline at end of file From 3bcc420a598613b5a60f3f62b7d555c41ca96e94 Mon Sep 17 00:00:00 2001 From: Christopher Tessum Date: Fri, 13 Jun 2025 11:25:16 +0800 Subject: [PATCH 2079/2176] switch parse_costs! and parse_constraints It seems like they are currently backward --- src/systems/model_parsing.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/systems/model_parsing.jl b/src/systems/model_parsing.jl index 9d293fd40d..67df29fae3 100644 --- a/src/systems/model_parsing.jl +++ b/src/systems/model_parsing.jl @@ -654,9 +654,9 @@ function parse_model!(exprs, comps, ext, eqs, icon, vs, ps, sps, c_evts, d_evts, elseif mname == Symbol("@defaults") parse_system_defaults!(exprs, arg, dict) elseif mname == Symbol("@constraints") - parse_costs!(cons, dict, body) + parse_constraints!(cons, dict, body) elseif mname == Symbol("@costs") - parse_constraints!(costs, dict, body) + parse_costs!(costs, dict, body) elseif mname == Symbol("@consolidate") parse_consolidate!(body, dict) else From f1fe0caa176dabed63d58d0ba3263abdd2ba12ae Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 13 Jun 2025 15:27:09 +0530 Subject: [PATCH 2080/2176] fix: fix parsing of costs and constraints for `@mtkmodel` metadata --- src/systems/model_parsing.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/systems/model_parsing.jl b/src/systems/model_parsing.jl index 67df29fae3..18c1b15922 100644 --- a/src/systems/model_parsing.jl +++ b/src/systems/model_parsing.jl @@ -1160,7 +1160,7 @@ function parse_constraints!(cons, dict, body) Base.remove_linenums!(body) for arg in body.args push!(cons, arg) - push!(dict[:constraints], readable_code.(cons)...) + push!(dict[:constraints], readable_code(arg)) end end @@ -1169,7 +1169,7 @@ function parse_costs!(costs, dict, body) Base.remove_linenums!(body) for arg in body.args push!(costs, arg) - push!(dict[:costs], readable_code.(costs)...) + push!(dict[:costs], readable_code(arg)) end end From 821b4c45c216b72dc66e3d9dac15722fec74735d Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 13 Jun 2025 15:27:25 +0530 Subject: [PATCH 2081/2176] test: test correct costs and constraints in `@mtkmodel` metadata --- test/model_parsing.jl | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/model_parsing.jl b/test/model_parsing.jl index fbbca9c7ea..c48628b007 100644 --- a/test/model_parsing.jl +++ b/test/model_parsing.jl @@ -1042,4 +1042,6 @@ end @test isequal(constrs[1], EvalAt(0.3)(ex.x) ~ 3) @test isequal(constrs[2], ex.y ≲ 4) @test ModelingToolkit.get_consolidate(ex)([1, 2], [3, 4]) ≈ 8 + log(2) + @test Example.structure[:constraints] == ["(EvalAt(0.3))(x) ~ 3", "y ≲ 4"] + @test Example.structure[:costs] == ["x + y", "(EvalAt(1))(y) ^ 2"] end From a1766c00fcd1da63289a81f879332b06cb198038 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 13 Jun 2025 18:05:25 +0530 Subject: [PATCH 2082/2176] refactor: store all ignored connections together in `System` --- src/systems/abstractsystem.jl | 98 ---------------------------------- src/systems/analysis_points.jl | 10 ++-- src/systems/system.jl | 3 +- 3 files changed, 4 insertions(+), 107 deletions(-) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index e643be904d..de62858760 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -1368,104 +1368,6 @@ struct IgnoredAnalysisPoint outputs::Vector{Union{BasicSymbolic, AbstractSystem}} end -const HierarchyVariableT = Vector{Union{BasicSymbolic, Symbol}} -const HierarchySystemT = Vector{Union{AbstractSystem, Symbol}} -""" -The type returned from `analysis_point_common_hierarchy`. -""" -const HierarchyAnalysisPointT = Vector{Union{IgnoredAnalysisPoint, Symbol}} -""" -The type returned from `as_hierarchy`. -""" -const HierarchyT = Union{HierarchyVariableT, HierarchySystemT} - -""" - $(TYPEDSIGNATURES) - -The inverse operation of `as_hierarchy`. -""" -function from_hierarchy(hierarchy::HierarchyT) - namefn = hierarchy[1] isa AbstractSystem ? nameof : getname - foldl(@view hierarchy[2:end]; init = hierarchy[1]) do sys, name - rename(sys, Symbol(name, NAMESPACE_SEPARATOR, namefn(sys))) - end -end - -""" - $(TYPEDSIGNATURES) - -Represent an ignored analysis point as a namespaced hierarchy. The hierarchy is built -using the common hierarchy of all involved systems/variables. -""" -function analysis_point_common_hierarchy(ap::IgnoredAnalysisPoint)::HierarchyAnalysisPointT - isys = as_hierarchy(ap.input) - osyss = as_hierarchy.(ap.outputs) - suffix = Symbol[] - while isys[end] == osyss[1][end] && allequal(last.(osyss)) - push!(suffix, isys[end]) - pop!(isys) - pop!.(osyss) - end - isys = from_hierarchy(isys) - osyss = from_hierarchy.(osyss) - newap = IgnoredAnalysisPoint(isys, osyss) - hierarchy = HierarchyAnalysisPointT([suffix; newap]) - reverse!(hierarchy) - return hierarchy -end - -""" - $(TYPEDSIGNATURES) - -Represent a namespaced system (or variable) `sys` as a hierarchy. Return a vector, where -the first element is the unnamespaced system (variable) and subsequent elements are -`Symbol`s representing the parents of the unnamespaced system (variable) in order from -inner to outer. -""" -function as_hierarchy(sys::Union{AbstractSystem, BasicSymbolic})::HierarchyT - namefn = sys isa AbstractSystem ? nameof : getname - # get the hierarchy - hierarchy = namespace_hierarchy(namefn(sys)) - # rename the system with unnamespaced name - newsys = rename(sys, hierarchy[end]) - # and remove it from the list - pop!(hierarchy) - # reverse it to go from inner to outer - reverse!(hierarchy) - # concatenate - T = sys isa AbstractSystem ? AbstractSystem : BasicSymbolic - return Union{Symbol, T}[newsys; hierarchy] -end - -""" - $(TYPEDSIGNATURES) - -Get the analysis points to ignore for `sys` and its subsystems. The returned value is a -`Tuple` similar in structure to the `ignored_connections` field. -""" -function ignored_connections(sys::AbstractSystem) - has_ignored_connections(sys) || - return (HierarchyAnalysisPointT[], HierarchyAnalysisPointT[]) - - ics = get_ignored_connections(sys) - if ics === nothing - ics = (HierarchyAnalysisPointT[], HierarchyAnalysisPointT[]) - end - # turn into hierarchies - ics = (map(analysis_point_common_hierarchy, ics[1]), - map(analysis_point_common_hierarchy, ics[2])) - systems = get_systems(sys) - # for each subsystem, get its ignored connections, add the name of the subsystem - # to the hierarchy and concatenate corresponding buffers of the result - result = mapreduce(Broadcast.BroadcastFunction(vcat), systems; init = ics) do subsys - sub_ics = ignored_connections(subsys) - (map(Base.Fix2(push!, nameof(subsys)), sub_ics[1]), - map(Base.Fix2(push!, nameof(subsys)), sub_ics[2])) - end - return (Vector{HierarchyAnalysisPointT}(result[1]), - Vector{HierarchyAnalysisPointT}(result[2])) -end - """ $(TYPEDSIGNATURES) diff --git a/src/systems/analysis_points.jl b/src/systems/analysis_points.jl index caa51f0740..4171042908 100644 --- a/src/systems/analysis_points.jl +++ b/src/systems/analysis_points.jl @@ -432,19 +432,15 @@ function with_analysis_point_ignored(sys::AbstractSystem, ap::AnalysisPoint) has_ignored_connections(sys) || return sys ignored = get_ignored_connections(sys) if ignored === nothing - ignored = (IgnoredAnalysisPoint[], IgnoredAnalysisPoint[]) + ignored = Connection[] else - ignored = copy.(ignored) + ignored = copy(ignored) end if ap.outputs === nothing error("Empty analysis point") end - if ap.input isa AbstractSystem && all(x -> x isa AbstractSystem, ap.outputs) - push!(ignored[1], IgnoredAnalysisPoint(ap.input, ap.outputs)) - else - push!(ignored[2], IgnoredAnalysisPoint(unwrap(ap.input), unwrap.(ap.outputs))) - end + push!(ignored, Connection([unwrap(ap.input); unwrap.(ap.outputs)])) return @set sys.ignored_connections = ignored end diff --git a/src/systems/system.jl b/src/systems/system.jl index af721a6ff3..863b840b37 100644 --- a/src/systems/system.jl +++ b/src/systems/system.jl @@ -214,8 +214,7 @@ struct System <: AbstractSystem (ones between connector systems) and the second contains all such causal variable connections. """ - ignored_connections::Union{ - Nothing, Tuple{Vector{IgnoredAnalysisPoint}, Vector{IgnoredAnalysisPoint}}} + ignored_connections::Union{Nothing, Vector{Connection}} """ `SymbolicUtils.Code.Assignment`s to prepend to all code generated from this system. """ From 8483672e97889b08c7af069fa6011d513ac43d25 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 13 Jun 2025 18:06:16 +0530 Subject: [PATCH 2083/2176] feat: add connection graph utilities --- src/ModelingToolkit.jl | 1 + src/systems/connectiongraph.jl | 446 +++++++++++++++++++++++++++++++++ 2 files changed, 447 insertions(+) create mode 100644 src/systems/connectiongraph.jl diff --git a/src/ModelingToolkit.jl b/src/ModelingToolkit.jl index 0ad1080965..d23d173b9a 100644 --- a/src/ModelingToolkit.jl +++ b/src/ModelingToolkit.jl @@ -161,6 +161,7 @@ include("systems/index_cache.jl") include("systems/parameter_buffer.jl") include("systems/abstractsystem.jl") include("systems/model_parsing.jl") +include("systems/connectiongraph.jl") include("systems/connectors.jl") include("systems/state_machines.jl") include("systems/analysis_points.jl") diff --git a/src/systems/connectiongraph.jl b/src/systems/connectiongraph.jl new file mode 100644 index 0000000000..70b8e2a72b --- /dev/null +++ b/src/systems/connectiongraph.jl @@ -0,0 +1,446 @@ +""" + $(TYPEDEF) + +A vertex in the connection hypergraph. + +## Fields + +$(TYPEDFIELDS) +""" +struct ConnectionVertex + """ + The name of the variable or subsystem represented by this connection vertex. Stored as + a list of names denoting the path from the root system to this variable/subsystem. The + name of the root system is not included. + """ + name::Vector{Symbol} + """ + Boolean indicating whether this is an outside connector. + """ + isouter::Bool + """ + A type indicating what kind of connector it is. One of: + - `Stream` + - `Equality` + - `Flow` + - `InputVar` + - `OutputVar` + """ + type::DataType + """ + The cached hash value of this struct. Should never be passed manually. + """ + hash::UInt +end + +""" + $(TYPEDSIGNATURES) + +Create a `ConnectionVertex` given +- `namespace`: the path from the root to the variable/subsystem. Does not include the root + system. +- `var`: the variable/subsystem. + +`isouter` is the same as the struct field. Uses `get_connection_type` to find the type to +use for this connection. +""" +function ConnectionVertex( + namespace::Vector{Symbol}, var::Union{BasicSymbolic, AbstractSystem}, isouter::Bool) + if var isa BasicSymbolic + name = getname(var) + else + name = nameof(var) + end + var_ns = namespace_hierarchy(name) + type = get_connection_type(var) + name = vcat(namespace, var_ns) + return ConnectionVertex(name, isouter, type; alias = true) +end + +""" + $(TYPEDSIGNATURES) + +Create a connection vertex for the given path. Typically used for domain connection graphs, +where the type of connection doesn't matter. Uses `isouter = true` and `type = Flow`. +""" +function ConnectionVertex(name::Vector{Symbol}) + return ConnectionVertex(name, true, Flow) +end + +""" + $(TYPEDSIGNATURES) + +Create a connection vertex for the given path `name` using the provided `isouter` and +`type`. `alias` denotes whether `name` can be stored by this vertex without copying. +""" +function ConnectionVertex( + name::Vector{Symbol}, isouter::Bool, type::DataType; alias = false) + if !alias + name = copy(name) + end + h = foldr(hash, name; init = zero(UInt)) + h = hash(type, h) + h = hash(isouter, h) + return ConnectionVertex(name, isouter, type, h) +end + +Base.hash(x::ConnectionVertex, h::UInt) = h ⊻ x.hash + +function Base.:(==)(a::ConnectionVertex, b::ConnectionVertex) + length(a.name) == length(b.name) || return false + for (x, y) in zip(a.name, b.name) + x == y || return false + end + a.isouter == b.isouter || return false + a.type == b.type || return false + if a.hash != b.hash + error(""" + This should never happen. Please open an issue in ModelingToolkit.jl with an MWE. + """) + end + return true +end + +""" + $(TYPEDEF) + +A hypergraph used to represent the connection sets in a system. Vertices of this graph are +of type `ConnectionVertex`. The connected components of a connection graph are the merged +connection sets. + +## Fields + +$(TYPEDFIELDS) +""" +struct ConnectionGraph + """ + Mapping from vertices to their integer ID. + """ + labels::Dict{ConnectionVertex, Int} + """ + Reverse mapping from integer ID to vertices. + """ + invmap::Vector{ConnectionVertex} + """ + Core data structure for storing the hypergraph. Each hyperedge is a source vertex and + has bipartite edges to the connection vertices it is incident on. + """ + graph::BipartiteGraph{Int, Nothing} +end + +""" + $(TYPEDSIGNATURES) + +Create an empty `ConnectionGraph`. +""" +function ConnectionGraph() + graph = BipartiteGraph(0, 0, Val(true)) + return ConnectionGraph(Dict{ConnectionVertex, Int}(), ConnectionVertex[], graph) +end + +""" + $(TYPEDSIGNATURES) + +Add the given vertex to the connection graph. Return the integer ID of the added vertex. +No-op if the vertex already exists. +""" +function Graphs.add_vertex!(graph::ConnectionGraph, dst::ConnectionVertex) + j = get(graph.labels, dst, 0) + iszero(j) || return j + j = Graphs.add_vertex!(graph.graph, DST) + push!(graph.invmap, dst) + @assert length(graph.invmap) == j + graph.labels[dst] = j + return j +end + +const ConnectionGraphEdge = Union{Vector{ConnectionVertex}, Tuple{Vararg{ConnectionVertex}}} + +""" + $(TYPEDSIGNATURES) + +Add the given hyperedge to the connection graph. Adds all vertices in the given edge if +they do not exist. Returns the integer ID of the added edge. +""" +function Graphs.add_edge!(graph::ConnectionGraph, src::ConnectionGraphEdge) + i = Graphs.add_vertex!(graph.graph, SRC) + for vert in src + j = Graphs.add_vertex!(graph, vert) + Graphs.add_edge!(graph.graph, i, j) + end + return i +end + +""" + $(TYPEDEF) + +A connection state is a combination of two `ConnectionGraph`s, one for the connection sets +and the other for the domain network. The domain network is a graph of connected +subsystems. The connected components of the domain network denote connected domains that +share properties. +""" +abstract type AbstractConnectionState end + +""" + $(TYPEDEF) + +The most trivial `AbstractConnectionState`. + +## Fields + +$(TYPEDFIELDS) +""" +struct ConnectionState <: AbstractConnectionState + """ + The connection graph for connection sets. + """ + connection_graph::ConnectionGraph + """ + The connection graph for the domain network. + """ + domain_connection_graph::ConnectionGraph +end + +""" + $(TYPEDSIGNATURES) + +Create an empty `ConnectionState` with empty graphs. +""" +ConnectionState() = ConnectionState(ConnectionGraph(), ConnectionGraph()) + +""" + $(TYPEDSIGNATURES) + +Add the given edge to the connection network. Does not affect the domain network. +""" +function add_connection_edge!(state::ConnectionState, edge::ConnectionGraphEdge) + Graphs.add_edge!(state.connection_graph, edge) + return nothing +end + +""" + $(TYPEDSIGNATURES) + +Add the given edge to the domain network. Does not affect the connection network. +""" +function add_domain_connection_edge!(state::ConnectionState, edge::ConnectionGraphEdge) + Graphs.add_edge!(state.domain_connection_graph, edge) + return nothing +end + +""" + $(TYPEDSIGNATURES) + +An `AbstractConnectionState` that is used to remove edges from the main connection state. +Transformed analysis points add to the list of removed connections, and the list of removed +connections builds this connection state. This allows ensuring that the removed connections +are not present in the final network even if they are connected multiple times. This state +also tracks which vertex in each hyperedge is the input, since the removed connections are +causal. + +## Fields + +$(TYPEDFIELDS) +""" +struct NegativeConnectionState <: AbstractConnectionState + """ + The connection graph for connection sets. + """ + connection_graph::ConnectionGraph + """ + The connection graph for the domain network. + """ + domain_connection_graph::ConnectionGraph + """ + Mapping from the integer ID of each hyperedge in `connection_graph` to the integer ID + of the "input" in that hyperedge. + """ + connection_hyperedge_inputs::Vector{Int} + """ + Mapping from the integer ID of each hyperedge in `domain_connection_graph` to the + integer ID of the "input" in that hyperedge. + """ + domain_hyperedge_inputs::Vector{Int} +end + +""" + $(TYPEDSIGNATURES) + +Create an empty `NegativeConnectionState` with empty graphs. +""" +function NegativeConnectionState() + NegativeConnectionState(ConnectionGraph(), ConnectionGraph(), Int[], Int[]) +end + +""" + $(TYPEDSIGNATURES) + +Add the given edge to the connection network. Does not affect the domain network. Assumes +that the first vertex in `edge` is the input. +""" +function add_connection_edge!(state::NegativeConnectionState, edge::ConnectionGraphEdge) + i = Graphs.add_edge!(state.connection_graph, edge) + j = state.connection_graph.labels[first(edge)] + push!(state.connection_hyperedge_inputs, j) + @assert length(state.connection_hyperedge_inputs) == i + return nothing +end + +""" + $(TYPEDSIGNATURES) + +Add the given edge to the domain network. Does not affect the connection network. Assumes +that the first vertex in `edge` is the input. +""" +function add_domain_connection_edge!( + state::NegativeConnectionState, edge::ConnectionGraphEdge) + i = Graphs.add_edge!(state.domain_connection_graph, edge) + j = state.domain_connection_graph.labels[first(edge)] + push!(state.domain_hyperedge_inputs, j) + @assert length(state.domain_hyperedge_inputs) == i + return nothing +end + +""" + $(TYPEDSIGNATURES) + +Modify `graph` such that no hyperedge is a superset of any (causal) hyerpedge in `neg_graph`. + +For each "negative" hyperedge in `neg_graph` with integer ID `neg_edge_id`, +`neg_edge_inputs[neg_edge_id]` denotes the vertex the negative hyperedge is incident on +which is considered the input of the negative hyperedge. If any hyperedge in `graph` +contains this input as well as at least one other vertex in the negative hyperedge, all +vertices common between the hyperedge and negative hyperedge are removed from the hyperedge. + +`graph` is modified in-place. Note that `graph` and `neg_graph` may not have the same +ordering of vertices, and thus all comparisons should be done by comparing the +`ConnectionVertex`. +""" +function remove_negative_connections!( + graph::ConnectionGraph, neg_graph::ConnectionGraph, neg_edge_inputs::Vector{Int}) + # _i means index in neg_graph + # _j means index in graph + + # get all edges in `graph` as bitsets + graph_hyperedgesets = map(𝑠vertices(graph.graph)) do edge_j + hyperedge_jdxs = 𝑠neighbors(graph.graph, edge_j) + return BitSet(hyperedge_jdxs) + end + + # indexes in each hyperedge to remove + idxs_to_rm = [BitSet() for _ in graph_hyperedgesets] + # iterate over negative edges and the corresponding input vertex in each edge + for (input_i, edge_i) in zip(neg_edge_inputs, 𝑠vertices(neg_graph.graph)) + # get the hyperedge as integer indexes in `neg_graph` + neg_hyperedge_idxs = 𝑠neighbors(neg_graph.graph, edge_i) + # the hyperedge as `ConnectionVar`s + neg_hyperedge = map(Base.Fix1(getindex, neg_graph.invmap), neg_hyperedge_idxs) + # The hyperedge as integer indexes in `graph` + # *j*dxs. See what I did there? + neg_hyperedge_jdxs = map(cvar -> get(graph.labels, cvar, 0), neg_hyperedge) + # the edge to remove is between variables that aren't connected, so ignore it + if any(iszero, neg_hyperedge_jdxs) + continue + end + + # The input vertex as a `ConnectionVar` + input_v = neg_graph.invmap[input_i] + # The input vertex as an index in `graph` + input_j = graph.labels[input_v] + # Iterate over hyperedges in `graph` + for edge_j in 𝑠vertices(graph.graph) + # The bitset of nodes this edge is incident on + edgeset = graph_hyperedgesets[edge_j] + # the input must be in this hyperedge + input_j in edgeset || continue + # now, if any other vertex apart from this input is also in the hyperedge + # we remove all the indices in `neg_hyperedge_jdxs` also present in this + # hyperedge + + # should_rm tracks if any other vertex apart from `input_j` is in the hyperedge + should_rm = false + # iterate over the negative hyperedge + for var_j in neg_hyperedge_jdxs + var_j == input_j && continue + # check if the variable which is not `input_j` is in the hyperedge + should_rm |= var_j in edgeset + should_rm || continue + # if there is any other variable, start removing + push!(idxs_to_rm[edge_j], var_j) + end + if should_rm + # if there was any other variable, also remove `input_j` + push!(idxs_to_rm, input_j) + end + end + end + + # for each edge and list of vertices to remove from the edge + for (edge_j, neg_vertices) in enumerate(idxs_to_rm) + for vert_j in neg_vertices + # remove those vertices + Graphs.rem_edge!(graph.graph, edge_j, vert_j) + end + end +end + +""" + $(TYPEDSIGNATURES) + +Remove negative hyperedges given by `neg_state` from the connection and domain networks of +`state`. +""" +function remove_negative_connections!( + state::ConnectionState, neg_state::NegativeConnectionState) + remove_negative_connections!(state.connection_graph, neg_state.connection_graph, + neg_state.connection_hyperedge_inputs) + remove_negative_connections!( + state.domain_connection_graph, neg_state.domain_connection_graph, + neg_state.domain_hyperedge_inputs) +end + +""" + $(TYPEDSIGNATURES) + +Return the merged connection sets in `graph` as a `Vector{Vector{ConnectionVertex}}`. These +are equivalent to the connected components of `graph`. +""" +function connectionsets(graph::ConnectionGraph) + bigraph = graph.graph + invmap = graph.invmap + + # union all of the hyperedges + disjoint_sets = IntDisjointSets(length(invmap)) + for edge_i in 𝑠vertices(bigraph) + hyperedge = 𝑠neighbors(bigraph, edge_i) + root, rest = Iterators.peel(hyperedge) + for vert in rest + union!(disjoint_sets, root, vert) + end + end + + # maps the root of a vertex in `disjoint_sets` to the index of the corresponding set + # in `vertex_sets` + root_to_set = Dict{Int, Int}() + vertex_sets = Vector{ConnectionVertex}[] + for (vert_i, vert) in enumerate(invmap) + root = find_root!(disjoint_sets, vert_i) + set_i = get!(root_to_set, root) do + push!(vertex_sets, ConnectionVertex[]) + return length(vertex_sets) + end + push!(vertex_sets[set_i], vert) + end + + return vertex_sets +end + +""" + $(TYPEDSIGNATURES) + +Return the connection sets of the connection graph and domain network. +""" +function connectionsets(state::ConnectionState) + return connectionsets(state.connection_graph), + connectionsets(state.domain_connection_graph) +end From f6af87fb1e7d547ce7b0a4a3b4ff9510599aeeb7 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 13 Jun 2025 18:06:22 +0530 Subject: [PATCH 2084/2176] feat: add `NotPossibleError` --- src/utils.jl | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/utils.jl b/src/utils.jl index 2b8ec4a7a0..48a4e57d71 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -1050,3 +1050,11 @@ function flatten_equations(eqs::Vector{Equation}) end const JumpType = Union{VariableRateJump, ConstantRateJump, MassActionJump} + +struct NotPossibleError <: Exception end + +function Base.showerror(io::IO, ::NotPossibleError) + print(io, """ + This should not be possible. Please open an issue in ModelingToolkit.jl with an MWE. + """) +end From 8bc75d41b13dd87994ce5030cb95adcbc6edfb28 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 13 Jun 2025 18:06:41 +0530 Subject: [PATCH 2085/2176] refactor: rewrite connection expansion implementation --- src/systems/abstractsystem.jl | 41 +- src/systems/connectors.jl | 1220 ++++++++++++++++----------------- 2 files changed, 591 insertions(+), 670 deletions(-) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index de62858760..f95cf90eea 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -1895,35 +1895,20 @@ function n_expanded_connection_equations(sys::AbstractSystem) # TODO: what about inputs? isconnector(sys) && return length(get_unknowns(sys)) sys = remove_analysis_points(sys) - n_variable_connect_eqs = 0 - for eq in equations(sys) - is_causal_variable_connection(eq.rhs) || continue - n_variable_connect_eqs += length(get_systems(eq.rhs)) - 1 - end - sys, (csets, _) = generate_connection_set(sys) - ceqs, instream_csets = generate_connection_equations_and_stream_connections(csets) - n_outer_stream_variables = 0 - for cset in instream_csets - n_outer_stream_variables += count(x -> x.isouter, cset.set) - end - - #n_toplevel_unused_flows = 0 - #toplevel_flows = Set() - #for cset in csets - # e1 = first(cset.set) - # e1.sys.namespace === nothing || continue - # for e in cset.set - # get_connection_type(e.v) === Flow || continue - # push!(toplevel_flows, e.v) - # end - #end - #for m in get_systems(sys) - # isconnector(m) || continue - # n_toplevel_unused_flows += count(x->get_connection_type(x) === Flow && !(x in toplevel_flows), get_unknowns(m)) - #end - - nextras = n_outer_stream_variables + length(ceqs) + n_variable_connect_eqs + + n_extras = 0 + for cset in csets + rep = cset[1] + if rep.type <: Union{InputVar, OutputVar, Equality} + n_extras += length(cset) - 1 + elseif rep.type == Flow + n_extras += 1 + elseif rep.type == Stream + n_extras += count(x -> x.isouter, cset) + end + end + return n_extras end Base.show(io::IO, sys::AbstractSystem; kws...) = show(io, MIME"text/plain"(), sys; kws...) diff --git a/src/systems/connectors.jl b/src/systems/connectors.jl index 5d6227d4c1..14f90ff14e 100644 --- a/src/systems/connectors.jl +++ b/src/systems/connectors.jl @@ -28,6 +28,8 @@ function connect(sys1::AbstractSystem, sys2::AbstractSystem, syss::AbstractSyste Equation(Connection(), Connection(syss)) # the RHS are connected systems end +const _debug_mode = Base.JLOptions().check_bounds == 1 + function Base.show(io::IO, c::Connection) print(io, "connect(") if c.systems isa AbstractArray || c.systems isa Tuple @@ -52,18 +54,25 @@ end isconnection(_) = false isconnection(_::Connection) = true + """ - domain_connect(sys1, sys2, syss...) + $(TYPEDSIGNATURES) Adds a domain only connection equation, through and across state equations are not generated. """ -function domain_connect(sys1, sys2, syss...) +function domain_connect(sys1::AbstractSystem, sys2::AbstractSystem, syss::AbstractSystem...) syss = (sys1, sys2, syss...) length(unique(nameof, syss)) == length(syss) || error("connect takes distinct systems!") Equation(Connection(:domain), Connection(syss)) # the RHS are connected systems end -function get_connection_type(s) +""" + $(TYPEDSIGNATURES) + +Get the connection type of symbolic variable `s` from the `VariableConnectType` metadata. +Defaults to `Equality` if not present. +""" +function get_connection_type(s::Symbolic) s = unwrap(s) if iscall(s) && operation(s) === getindex s = arguments(s)[1] @@ -111,6 +120,14 @@ struct StreamConnector <: AbstractConnectorType end struct RegularConnector <: AbstractConnectorType end struct DomainConnector <: AbstractConnectorType end +""" + $(TYPEDSIGNATURES) + +Return an `AbstractConnectorType` denoting the type of connector that `sys` is. +Domain connectors have a single `Flow` unknown. Stream connectors have a single +`Flow` variable and multiple `Stream` variables. Any other type of connector is +a "regular" connector. +""" function connector_type(sys::AbstractSystem) unkvars = get_unknowns(sys) n_stream = 0 @@ -180,22 +197,15 @@ end const ConnectableSymbolicT = Union{BasicSymbolic, Num, Symbolics.Arr} -const CAUSAL_CONNECTION_ERR = """ -Only causal variables can be used in a `connect` statement. The first argument must \ -be a single output variable and all subsequent variables must be input variables. -""" - -function VariableNotOutputError(var) +function NonCausalVariableError(vars) + names = join(map(var -> " " * string(var), vars), "\n") ArgumentError(""" - $CAUSAL_CONNECTION_ERR Expected $var to be marked as an output with `[output = true]` \ - in the variable metadata. - """) -end + Only causal variables can be used in a `connect` statement. Each variable must be \ + either an input or an output. Mark a variable as input using the `[input = true]` \ + variable metadata or as an output using the `[output = true]` variable metadata. -function VariableNotInputError(var) - ArgumentError(""" - $CAUSAL_CONNECTION_ERR Expected $var to be marked an input with `[input = true]` \ - in the variable metadata. + The following variables were found to be non-causal: + $names """) end @@ -220,11 +230,10 @@ function validate_causal_variables_connection(allvars) if !allequal(allsizes) throw(ArgumentError("Expected all connection variables to have the same size. Got variables $allvars with sizes $allsizes respectively.")) end - isoutput(var1) || throw(VariableNotOutputError(var1)) - isinput(var2) || throw(VariableNotInputError(var2)) - for var in vars - isinput(var) || throw(VariableNotInputError(var)) + non_causal_variables = filter(allvars) do var + !isinput(var) && !isoutput(var) end + isempty(non_causal_variables) || throw(NonCausalVariableError(non_causal_variables)) end """ @@ -246,15 +255,11 @@ function connect(var1::ConnectableSymbolicT, var2::ConnectableSymbolicT, return Equation(Connection(), Connection(map(SymbolicWithNameof, unwrap.(allvars)))) end -function flowvar(sys::AbstractSystem) - sts = get_unknowns(sys) - for s in sts - vtype = get_connection_type(s) - vtype === Flow && return s - end - error("There in no flow variable in $(nameof(sys))") -end +""" + $(METHODLIST) +Add all `instream(..)` expressions to `set`. +""" function collect_instream!(set, eq::Equation) collect_instream!(set, eq.lhs) | collect_instream!(set, eq.rhs) end @@ -297,6 +302,12 @@ mydiv(num, den) = end @register_symbolic mydiv(n, d) +""" + $(TYPEDSIGNATURES) + +Return a function which checks whether the connector (system) passed to it is an outside +connector of `sys`. The function can also be given the name of a system as a `Symbol`. +""" function generate_isouter(sys::AbstractSystem) outer_connectors = Symbol[] for s in get_systems(sys) @@ -309,82 +320,14 @@ function generate_isouter(sys::AbstractSystem) isconnector(sys) || error("$s is not a connector!") idx = findfirst(isequal(NAMESPACE_SEPARATOR), s) parent_name = Symbol(idx === nothing ? s : s[1:prevind(s, idx)]) - parent_name in outer_connectors + isouter(parent_name) + end + function isouter(name::Symbol)::Bool + return name in outer_connectors end end end -struct LazyNamespace - namespace::Union{Nothing, AbstractSystem} - sys::Any -end - -_getname(::Nothing) = nothing -_getname(sys) = nameof(sys) -Base.copy(l::LazyNamespace) = renamespace(_getname(l.namespace), l.sys) -Base.nameof(l::LazyNamespace) = renamespace(_getname(l.namespace), nameof(l.sys)) - -struct ConnectionElement - sys::LazyNamespace - v::Any - isouter::Bool - h::UInt -end -function _hash_impl(sys, v, isouter) - hashcore = hash(nameof(sys)::Symbol) ⊻ hash(getname(v)::Symbol) - hashouter = isouter ? hash(true) : hash(false) - hashcore ⊻ hashouter -end -function ConnectionElement(sys::LazyNamespace, v, isouter::Bool) - ConnectionElement(sys, v, isouter, _hash_impl(sys, v, isouter)) -end -Base.nameof(l::ConnectionElement) = renamespace(nameof(l.sys), getname(l.v)) -Base.isequal(l1::ConnectionElement, l2::ConnectionElement) = l1 == l2 -function Base.:(==)(l1::ConnectionElement, l2::ConnectionElement) - l1.isouter == l2.isouter && nameof(l1.sys) == nameof(l2.sys) && isequal(l1.v, l2.v) -end - -const _debug_mode = Base.JLOptions().check_bounds == 1 - -function Base.show(io::IO, c::ConnectionElement) - @unpack sys, v, isouter = c - print(io, nameof(sys), ".", v, "::", isouter ? "outer" : "inner") -end - -function Base.hash(e::ConnectionElement, salt::UInt) - if _debug_mode - @assert e.h === _hash_impl(e.sys, e.v, e.isouter) - end - e.h ⊻ salt -end -namespaced_var(l::ConnectionElement) = unknowns(l, l.v) -unknowns(l::ConnectionElement, v) = unknowns(copy(l.sys), v) - -function withtrueouter(e::ConnectionElement) - e.isouter && return e - # we undo the xor - newhash = (e.h ⊻ hash(false)) ⊻ hash(true) - ConnectionElement(e.sys, e.v, true, newhash) -end - -struct ConnectionSet - set::Vector{ConnectionElement} # namespace.sys, var, isouter -end -ConnectionSet() = ConnectionSet(ConnectionElement[]) -Base.copy(c::ConnectionSet) = ConnectionSet(copy(c.set)) -Base.:(==)(a::ConnectionSet, b::ConnectionSet) = a.set == b.set -Base.sort(a::ConnectionSet) = ConnectionSet(sort(a.set, by = string)) - -function Base.show(io::IO, c::ConnectionSet) - print(io, "<") - for i in 1:(length(c.set) - 1) - @unpack sys, v, isouter = c.set[i] - print(io, nameof(sys), ".", v, "::", isouter ? "outer" : "inner", ", ") - end - @unpack sys, v, isouter = last(c.set) - print(io, nameof(sys), ".", v, "::", isouter ? "outer" : "inner", ">") -end - @noinline function connection_error(ss) error("Different types of connectors are in one connection statement: <$(map(nameof, ss))>") end @@ -406,384 +349,460 @@ function ori(sys) end end +""" +Connection type used in `ConnectionVertex` for a causal input variable. `I` is an object +that can be passed to `getindex` as an index denoting the index in the variable for +causal array variables. For non-array variables this should be `1`. +""" +abstract type InputVar{I} end +""" +Connection type used in `ConnectionVertex` for a causal output variable. `I` is an object +that can be passed to `getindex` as an index denoting the index in the variable for +causal array variables. For non-array variables this should be `1`. +""" +abstract type OutputVar{I} end + +""" + $(METHODLIST) + +Get the contained index in an `InputVar` or `OutputVar` type. +""" +index_from_type(::Type{InputVar{I}}) where {I} = I +index_from_type(::Type{OutputVar{I}}) where {I} = I + +""" + $(TYPEDSIGNATURES) + +Chain `getproperty` calls on sys in the order given by `names` and return the unwrapped +result. +""" +function iterative_getproperty(sys::AbstractSystem, names::AbstractVector{Symbol}) + # we don't want to namespace the first time + result = toggle_namespacing(sys, false) + for name in names + result = getproperty(result, name) + end + return unwrap(result) +end + """ $(TYPEDSIGNATURES) -Populate `connectionsets` with connections between the connectors `ss`, all of which are -namespaced by `namespace`. +Return the variable/subsystem of `sys` referred to by vertex `vert`. +""" +function variable_from_vertex(sys::AbstractSystem, vert::ConnectionVertex) + value = iterative_getproperty(sys, vert.name) + value isa AbstractSystem && return value + vert.type <: Union{InputVar, OutputVar} || return value + # index possibly array causal variable + unwrap(wrap(value)[index_from_type(vert.type)]) +end + +""" + $(TYPEDSIGNATURES) + +Given `connected`, the list of connected variables/systems, generate the appropriate +connection sets and add them to `connection_state`. Update both the connection network and +domain network as necessary. `namespace` is the path from the root system to the system in +which the [`connect`](@ref) equation containing `connected` is located. `isouter` is the +function returned from [`generate_isouter`](@ref) for the system referred to by +`namespace`. -# Keyword Arguments -- `ignored_connects`: A tuple of the systems and variables for which connections should be - ignored. Of the format returned from `as_hierarchy`. -- `namespaced_ignored_systems`: The `from_hierarchy` versions of entries in - `ignored_connects[1]`, purely to avoid unnecessary recomputation. +`namespace` must not contain the name of the root system. """ -function connection2set!(connectionsets, namespace, ss, isouter; - ignored_systems = HierarchySystemT[], ignored_variables = HierarchyVariableT[]) - ns_ignored_systems = from_hierarchy.(ignored_systems) - ns_ignored_variables = from_hierarchy.(ignored_variables) - # ignore specified systems - ss = filter(ss) do s - !any(x -> nameof(x) == nameof(s), ns_ignored_systems) +function generate_connectionsets!(connection_state::AbstractConnectionState, + namespace::Vector{Symbol}, connected, isouter) + initial_len = length(namespace) + _generate_connectionsets!(connection_state, namespace, connected, isouter) + # Enforce postcondition as a sanity check that the namespacing is implemented correctly + length(namespace) == initial_len || throw(NotPossibleError()) + return nothing +end + +function _generate_connectionsets!(connection_state::AbstractConnectionState, + namespace::Vector{Symbol}, + connected_vars::Union{ + AbstractVector{SymbolicWithNameof}, Tuple{Vararg{SymbolicWithNameof}}}, + isouter) + # unwrap the `SymbolicWithNameof` into the contained symbolic variables. + connected_vars = map(x -> x.var, connected_vars) + _generate_connectionsets!(connection_state, namespace, connected_vars, isouter) +end + +function _generate_connectionsets!(connection_state::AbstractConnectionState, + namespace::Vector{Symbol}, + connected_vars::Union{ + AbstractVector{<:BasicSymbolic}, Tuple{Vararg{BasicSymbolic}}}, + isouter) + # NOTE: variable connections don't populate the domain network + + # wrap to be able to call `eachindex` on a non-array variable + representative = wrap(first(connected_vars)) + # all of them have the same size, but may have different axes/shape + # so we iterate over `eachindex(eachindex(..))` since that is identical for all + for sz_i in eachindex(eachindex(representative)) + hyperedge = map(connected_vars) do var + var = unwrap(var) + var_ns = namespace_hierarchy(getname(var)) + i = eachindex(wrap(var))[sz_i] + + is_input = isinput(var) + is_output = isoutput(var) + if is_input && is_output + names = join(string.(connected_vars), ", ") + throw(ArgumentError(""" + Variable $var in connection `connect($names)` is both input and output. + """)) + elseif is_input + type = InputVar{i} + elseif is_output + type = OutputVar{i} + else + names = join(string.(connected_vars), ", ") + throw(ArgumentError(""" + Variable $var in connection `connect($names)` is neither input nor output. + """)) + end + + return ConnectionVertex( + [namespace; var_ns], length(var_ns) == 1 || isouter(var_ns[1]), type) + end + add_connection_edge!(connection_state, hyperedge) end - # `ignored_variables` for each `s` in `ss` - corresponding_ignored_variables = map( - Base.Fix2(ignored_systems_for_subsystem, ignored_variables), ss) - corresponding_namespaced_ignored_variables = map( - Broadcast.BroadcastFunction(from_hierarchy), corresponding_ignored_variables) - - regular_ss = [] - domain_ss = nothing - for s in ss +end + +function _generate_connectionsets!(connection_state::AbstractConnectionState, + namespace::Vector{Symbol}, + systems::Union{AbstractVector{<:AbstractSystem}, Tuple{Vararg{AbstractSystem}}}, + isouter) + regular_systems = System[] + domain_system = nothing + for s in systems if is_domain_connector(s) - if domain_ss === nothing - domain_ss = s + if domain_system === nothing + domain_system = s else - names = join(map(string ∘ nameof, ss), ",") + names = join(map(string ∘ nameof, systems), ",") error("connect($names) contains multiple source domain connectors. There can only be one!") end else - push!(regular_ss, s) + push!(regular_systems, s) end end - T = ConnectionElement - @assert !isempty(regular_ss) - ss = regular_ss - # domain connections don't generate any equations - if domain_ss !== nothing - cset = ConnectionElement[] - dv = only(unknowns(domain_ss)) - for (i, s) in enumerate(ss) - sts = unknowns(s) - io = isouter(s) - _ignored_variables = corresponding_ignored_variables[i] - _namespaced_ignored_variables = corresponding_namespaced_ignored_variables[i] + + @assert !isempty(regular_systems) + + systems = regular_systems + # There is a domain being connected here. In such a case, we only connect the + # flow variable common between the domain setter and all other connectors in the + # normal connection graph. The domain graph connects all these subsystems. + if domain_system !== nothing + hyperedge = ConnectionVertex[] + domain_hyperedge = ConnectionVertex[] + sizehint!(hyperedge, length(systems) + 1) + sizehint!(domain_hyperedge, length(systems) + 1) + + dv = only(unknowns(domain_system)) + push!(namespace, nameof(domain_system)) + dv_vertex = ConnectionVertex(namespace, dv, false) + domain_vertex = ConnectionVertex(namespace) + pop!(namespace) + + push!(domain_hyperedge, domain_vertex) + push!(hyperedge, dv_vertex) + + for (i, sys) in enumerate(systems) + sts = unknowns(sys) + sys_is_outer = isouter(sys) + + # add this system to the namespace so all vertices created from its unknowns + # are properly namespaced + sysname = nameof(sys) + sys_ns = namespace_hierarchy(sysname) + append!(namespace, sys_ns) for v in sts vtype = get_connection_type(v) + # ignore all non-flow vertices in connectors (vtype === Flow && isequal(v, dv)) || continue - any(isequal(v), _namespaced_ignored_variables) && continue - push!(cset, T(LazyNamespace(namespace, domain_ss), dv, false)) - push!(cset, T(LazyNamespace(namespace, s), v, io)) + + vertex = ConnectionVertex(namespace, v, sys_is_outer) + # vertices in the domain graph are systems with isouter=true and type=Flow + sys_vertex = ConnectionVertex(namespace) + push!(hyperedge, vertex) + push!(domain_hyperedge, sys_vertex) end + # remember to remove the added namespace! + foreach(_ -> pop!(namespace), sys_ns) end - @assert length(cset) > 0 - push!(connectionsets, ConnectionSet(cset)) - return connectionsets + @assert length(hyperedge) > 1 + @assert length(domain_hyperedge) == length(hyperedge) + + add_connection_edge!(connection_state, hyperedge) + add_domain_connection_edge!(connection_state, domain_hyperedge) + return end - s1 = first(ss) - sts1v = unknowns(s1) - if isframe(s1) # Multibody - O = ori(s1) + sys1 = first(systems) + sys1_dvs = unknowns(sys1) + # Add 9 orientation variables if connection is between multibody frames + if isframe(sys1) # Multibody + O = ori(sys1) orientation_vars = Symbolics.unwrap.(collect(vec(O.R))) - sts1v = [sts1v; orientation_vars] + sys1_dvs = [sys1_dvs; orientation_vars] end - sts1 = Set(sts1v) - num_unknowns = length(sts1) - - # we don't filter here because `csets` should include the full set of unknowns. - # not all of `ss` will have the same (or any) variables filtered so the ones - # that aren't should still go in the right cset. Since `sts1` is only used for - # validating that all systems being connected are of the same type, it has - # unfiltered entries. - csets = [T[] for _ in 1:num_unknowns] # Add 9 orientation variables if connection is between multibody frames - for (i, s) in enumerate(ss) - unknown_vars = unknowns(s) - if isframe(s) # Multibody - O = ori(s) + sys1_dvs_set = Set(sys1_dvs) + num_unknowns = length(sys1_dvs) + + # We first build sets of all vertices that are connected together + var_sets = [ConnectionVertex[] for _ in 1:num_unknowns] + domain_hyperedge = ConnectionVertex[] + for (i, sys) in enumerate(systems) + unknown_vars = unknowns(sys) + # Add 9 orientation variables if connection is between multibody frames + if isframe(sys) # Multibody + O = ori(sys) orientation_vars = Symbolics.unwrap.(vec(O.R)) unknown_vars = [unknown_vars; orientation_vars] end - i != 1 && ((num_unknowns == length(unknown_vars) && - all(Base.Fix2(in, sts1), unknown_vars)) || - connection_error(ss)) - io = isouter(s) - # don't `filter!` here so that `j` points to the correct cset regardless of - # which variables are filtered. + # Error if any subsequent systems do not have the same number of unknowns + # or have unknowns not in the others. + if i != 1 && + (num_unknowns != length(unknown_vars) || any(!in(sys1_dvs_set), unknown_vars)) + connection_error(systems) + end + # add this system to the namespace so all vertices created from its unknowns + # are properly namespaced + sysname = nameof(sys) + sys_ns = namespace_hierarchy(sysname) + append!(namespace, sys_ns) + sys_is_outer = isouter(sys) for (j, v) in enumerate(unknown_vars) - any(isequal(v), corresponding_namespaced_ignored_variables[i]) && continue - push!(csets[j], T(LazyNamespace(namespace, s), v, io)) + push!(var_sets[j], ConnectionVertex(namespace, v, sys_is_outer)) end + domain_vertex = ConnectionVertex(namespace) + push!(domain_hyperedge, domain_vertex) + # remember to remove the added namespace! + foreach(_ -> pop!(namespace), sys_ns) end - for cset in csets - v = first(cset).v - vtype = get_connection_type(v) - if domain_ss !== nothing && vtype === Flow && - (dv = only(unknowns(domain_ss)); isequal(v, dv)) - push!(cset, T(LazyNamespace(namespace, domain_ss), dv, false)) - end - for k in 2:length(cset) - vtype === get_connection_type(cset[k].v) || connection_error(ss) + for var_set in var_sets + # all connected variables should have the same type + if !allequal(Iterators.map(cvert -> cvert.type, var_set)) + connection_error(systems) end - push!(connectionsets, ConnectionSet(cset)) + # add edges + add_connection_edge!(connection_state, var_set) end -end - -function generate_connection_set( - sys::AbstractSystem; scalarize = false) - connectionsets = ConnectionSet[] - domain_csets = ConnectionSet[] - sys = generate_connection_set!( - connectionsets, domain_csets, sys, scalarize, nothing, ignored_connections(sys)) - csets = merge(connectionsets) - domain_csets = merge([csets; domain_csets], true) - - sys, (csets, domain_csets) + add_domain_connection_edge!(connection_state, domain_hyperedge) end """ $(TYPEDSIGNATURES) -For a list of `systems` in a connect equation, return the subset of it to ignore (as a -list of hierarchical systems) based on `ignored_system_aps`, the analysis points to be -ignored. All analysis points in `ignored_system_aps` must contain systems (connectors) -as their input/outputs. +Generate the merged connection sets and connected domain sets for system `sys`. Also +removes all `connect` equations in `sys`. Return the modified system and a tuple of the +connection sets and domain sets. Also scalarizes array equations in the system. """ -function systems_to_ignore(ignored_system_aps::Vector{HierarchyAnalysisPointT}, - systems::Union{Vector{S}, Tuple{Vararg{S}}}) where {S <: AbstractSystem} - to_ignore = HierarchySystemT[] - for ap in ignored_system_aps - # if `systems` contains the input of the AP, ignore any outputs of the AP present in it. - isys_hierarchy = HierarchySystemT([ap[1].input; @view ap[2:end]]) - isys = from_hierarchy(isys_hierarchy) - any(x -> nameof(x) == nameof(isys), systems) || continue - - for outsys in ap[1].outputs - osys_hierarchy = HierarchySystemT([outsys; @view ap[2:end]]) - osys = from_hierarchy(osys_hierarchy) - any(x -> nameof(x) == nameof(osys), systems) || continue - push!(to_ignore, HierarchySystemT(osys_hierarchy)) - end - end +function generate_connection_set(sys::AbstractSystem) + # generate the states + connection_state = ConnectionState() + negative_connection_state = NegativeConnectionState() + # the root system isn't added to the namespace, which we handle by not namespacing it + sys = toggle_namespacing(sys, false) + sys = generate_connection_set!( + connection_state, negative_connection_state, sys, Symbol[]) + remove_negative_connections!(connection_state, negative_connection_state) - return to_ignore + return sys, connectionsets(connection_state) end """ $(TYPEDSIGNATURES) -For a list of `systems` in a connect equation, return the subset of their variables to -ignore (as a list of hierarchical variables) based on `ignored_system_aps`, the analysis -points to be ignored. All analysis points in `ignored_system_aps` must contain variables -as their input/outputs. +Appropriately handle the equation `eq` depending on whether it is a normal or connection +equation. For normal equations, it is expected that `eqs` is a buffer to which the equation +can be pushed, unmodified. Connection equations update the given `state`. The equation is +present at the path in the hierarchical system given by `namespace`. `isouter` is the +function returned from `generate_isouter`. """ -function variables_to_ignore(ignored_variable_aps::Vector{HierarchyAnalysisPointT}, - systems::Union{Vector{S}, Tuple{Vararg{S}}}) where {S <: AbstractSystem} - to_ignore = HierarchyVariableT[] - for ap in ignored_variable_aps - ivar_hierarchy = HierarchyVariableT([ap[1].input; @view ap[2:end]]) - ivar = from_hierarchy(ivar_hierarchy) - any(x -> any(isequal(ivar), renamespace.((x,), unknowns(x))), systems) || continue - - for outvar in ap[1].outputs - ovar_hierarchy = HierarchyVariableT([as_hierarchy(outvar); @view ap[2:end]]) - ovar = from_hierarchy(ovar_hierarchy) - any(x -> any(isequal(ovar), renamespace.((x,), unknowns(x))), systems) || - continue - push!(to_ignore, HierarchyVariableT(ovar_hierarchy)) +function handle_maybe_connect_equation!(eqs, state::AbstractConnectionState, + eq::Equation, namespace::Vector{Symbol}, isouter) + lhs = eq.lhs + rhs = eq.rhs + + if !(lhs isa Connection) + # split connections and equations + if eq.lhs isa AbstractArray || eq.rhs isa AbstractArray + append!(eqs, Symbolics.scalarize(eq)) + else + push!(eqs, eq) end + return end - return to_ignore -end -""" - $(TYPEDSIGNATURES) - -For a list of variables `vars` in a connect equation, return the subset of them ignore -(as a list of symbolic variables) based on `ignored_system_aps`, the analysis points to -be ignored. All analysis points in `ignored_system_aps` must contain variables as their -input/outputs. -""" -function variables_to_ignore(ignored_variable_aps::Vector{HierarchyAnalysisPointT}, - vars::Union{Vector{S}, Tuple{Vararg{S}}}) where {S <: BasicSymbolic} - to_ignore = eltype(vars)[] - for ap in ignored_variable_aps - ivar_hierarchy = HierarchyVariableT([ap[1].input; @view ap[2:end]]) - ivar = from_hierarchy(ivar_hierarchy) - any(isequal(ivar), vars) || continue - - for outvar in ap[1].outputs - ovar_hierarchy = HierarchyVariableT([outvar; @view ap[2:end]]) - ovar = from_hierarchy(ovar_hierarchy) - any(isequal(ovar), vars) || continue - push!(to_ignore, ovar) + if get_systems(lhs) === :domain + # This is a domain connection, so we only update the domain connection graph + hyperedge = map(get_systems(rhs)) do sys + sys isa AbstractSystem || error("Domain connections can only connect systems!") + sysname = nameof(sys) + sys_ns = namespace_hierarchy(sysname) + append!(namespace, sys_ns) + vertex = ConnectionVertex(namespace) + foreach(_ -> pop!(namespace), sys_ns) + return vertex end + add_domain_connection_edge!(state, hyperedge) + else + connected_systems = get_systems(rhs) + generate_connectionsets!(state, namespace, connected_systems, isouter) end - - return to_ignore + return nothing end """ $(TYPEDSIGNATURES) -Generate connection sets from `connect` equations. +Generate the appropriate connection sets from `connect` equations present in the +hierarchical system `sys`. This is a recursive function that descends the hierarchy. If +`sys` is the root system, then `does_namespacing(sys)` must be `false` and `namespace` +should be empty. It is essential that the traversal is preorder. -# Arguments +## Arguments -- `connectionsets` is the list of connection sets to be populated by recursively - descending `sys`. -- `domain_csets` is the list of connection sets for domain connections. -- `sys` is the system whose equations are to be searched. -- `namespace` is a system representing the namespace in which `sys` exists, or `nothing` - for no namespace (if `sys` is top-level). +- `connection_state`: The connection state keeping track of the connection network and the + domain network. +- `negative_connection_state`: The connection state that tracks connections removed by + analysis point transformations. These removed connections are stored in the + `ignored_connections` field of the system. +- `namespace`: The path of names from the root system to the current system. This should + not include the name of the root system. """ -function generate_connection_set!(connectionsets, domain_csets, - sys::AbstractSystem, scalarize, namespace = nothing, - ignored_connects = (HierarchyAnalysisPointT[], HierarchyAnalysisPointT[])) +function generate_connection_set!(connection_state::ConnectionState, + negative_connection_state::NegativeConnectionState, + sys::AbstractSystem, namespace::Vector{Symbol}) + initial_len = length(namespace) + res = _generate_connection_set!( + connection_state, negative_connection_state, sys, namespace) + # Enforce postcondition as a sanity check that the recursion is implemented correctly + length(namespace) == initial_len || throw(NotPossibleError()) + return res +end + +function _generate_connection_set!(connection_state::ConnectionState, + negative_connection_state::NegativeConnectionState, + sys::AbstractSystem, namespace::Vector{Symbol}) + # This function recurses down the system tree. Each system adds its name and pops + # it before returning. We don't add the root system, which is handled by assuming + # it doesn't do namespacing. + does_namespacing(sys) && push!(namespace, nameof(sys)) subsys = get_systems(sys) - ignored_system_aps, ignored_variable_aps = ignored_connects isouter = generate_isouter(sys) eqs′ = get_eqs(sys) eqs = Equation[] - cts = [] # connections - domain_cts = [] # connections - extra_unknowns = [] + # generate connection equations and separate out non-connection equations for eq in eqs′ - lhs = eq.lhs - rhs = eq.rhs - - # causal variable connections will be expanded before we get here, - # but this guard is useful for `n_expanded_connection_equations`. - is_causal_variable_connection(rhs) && continue - if lhs isa Connection && get_systems(lhs) === :domain - connected_systems = get_systems(rhs) - connection2set!(domain_csets, namespace, connected_systems, isouter; - ignored_systems = systems_to_ignore( - ignored_system_aps, connected_systems), - ignored_variables = variables_to_ignore( - ignored_variable_aps, connected_systems)) - elseif isconnection(rhs) - push!(cts, get_systems(rhs)) - else - # split connections and equations - if eq.lhs isa AbstractArray || eq.rhs isa AbstractArray - append!(eqs, Symbolics.scalarize(eq)) - else - push!(eqs, eq) - end - end + handle_maybe_connect_equation!(eqs, connection_state, eq, namespace, isouter) end - # all connectors are eventually inside connectors. - T = ConnectionElement - # only generate connection sets for systems that are not ignored + # go through the removed connections and update the negative graph + for conn in something(get_ignored_connections(sys), ()) + eq = Equation(Connection(), conn) + # there won't be any standard equations, so we can pass `nothing` instead of + # `eqs`. + handle_maybe_connect_equation!( + nothing, negative_connection_state, eq, namespace, isouter) + end + + # all connectors are eventually inside connectors, and all flow variables + # need at least a singleton connectionset (hyperedge) with the inside variant for s in subsys isconnector(s) || continue is_domain_connector(s) && continue + push!(namespace, nameof(s)) for v in unknowns(s) Flow === get_connection_type(v) || continue - push!(connectionsets, ConnectionSet([T(LazyNamespace(namespace, s), v, false)])) + add_connection_edge!(connection_state, (ConnectionVertex(namespace, v, false),)) end + pop!(namespace) end - for ct in cts - connection2set!(connectionsets, namespace, ct, isouter; - ignored_systems = systems_to_ignore(ignored_system_aps, ct), - ignored_variables = variables_to_ignore(ignored_variable_aps, ct)) - end - - # pre order traversal - if !isempty(extra_unknowns) - @set! sys.unknowns = [get_unknowns(sys); extra_unknowns] + # recurse down the hierarchy + @set! sys.systems = map(subsys) do s + generate_connection_set!(connection_state, negative_connection_state, s, namespace) end - @set! sys.systems = map( - s -> generate_connection_set!(connectionsets, domain_csets, s, - scalarize, renamespace(namespace, s), - ignored_systems_for_subsystem.((s,), ignored_connects)), - subsys) @set! sys.eqs = eqs + # Remember to pop the name at the end! + does_namespacing(sys) && pop!(namespace) + return sys end """ $(TYPEDSIGNATURES) -Given a subsystem `subsys` of a parent system and a list of systems (variables) to be -ignored by `generate_connection_set!` (`expand_variable_connections`), filter -`ignored_systems` to only include those present in the subtree of `subsys` and update -their hierarchy to not include `subsys`. +Generate connection equations for the connection sets given by `csets`. This does not +handle stream connections. Return the generated equations and the stream connection sets. """ -function ignored_systems_for_subsystem( - subsys::AbstractSystem, ignored_systems::Vector{<:Union{ - HierarchyT, HierarchyAnalysisPointT}}) - result = eltype(ignored_systems)[] - # in case `subsys` is namespaced, get its hierarchy and compare suffixes - # instead of the just the last element - suffix = reverse!(namespace_hierarchy(nameof(subsys))) - N = length(suffix) - for igsys in ignored_systems - if length(igsys) > N && igsys[(end - N + 1):end] == suffix - push!(result, copy(igsys)) - for i in 1:N - pop!(result[end]) - end - end - end - return result -end - -function Base.merge(csets::AbstractVector{<:ConnectionSet}, allouter = false) - ele2idx = Dict{ConnectionElement, Int}() - idx2ele = ConnectionElement[] - union_find = IntDisjointSets(0) - prev_id = Ref(-1) - for cset in csets, (j, s) in enumerate(cset.set) - v = allouter ? withtrueouter(s) : s - id = let ele2idx = ele2idx, idx2ele = idx2ele - get!(ele2idx, v) do - push!(idx2ele, v) - id = length(idx2ele) - id′ = push!(union_find) - @assert id == id′ - id - end - end - # isequal might not be equal? lol - if v.sys.namespace !== nothing - idx2ele[id] = v - end - if j > 1 - union!(union_find, prev_id[], id) - end - prev_id[] = id - end - id2set = Dict{Int, Int}() - merged_set = ConnectionSet[] - for (id, ele) in enumerate(idx2ele) - rid = find_root!(union_find, id) - set_idx = get!(id2set, rid) do - set = ConnectionSet() - push!(merged_set, set) - length(merged_set) - end - push!(merged_set[set_idx].set, ele) - end - merged_set -end - -function generate_connection_equations_and_stream_connections(csets::AbstractVector{ - <:ConnectionSet, -}) +function generate_connection_equations_and_stream_connections( + sys::AbstractSystem, csets::Vector{Vector{ConnectionVertex}}) eqs = Equation[] - stream_connections = ConnectionSet[] + stream_connections = Vector{ConnectionVertex}[] for cset in csets - v = cset.set[1].v - v = getparent(v, v) - vtype = get_connection_type(v) - if vtype === Stream + cvert = cset[1] + var = variable_from_vertex(sys, cvert)::BasicSymbolic + vtype = cvert.type + if vtype <: Union{InputVar, OutputVar} + inner_output = nothing + outer_input = nothing + for cvert in cset + if cvert.isouter && cvert.type <: InputVar + if outer_input !== nothing + error(""" + Found two outer input connectors `$outer_input` and `$cvert` in the + same connection set. + """) + end + outer_input = cvert + elseif !cvert.isouter && cvert.type <: OutputVar + if inner_output !== nothing + error(""" + Found two inner output connectors `$inner_output` and `$cvert` in + the same connection set. + """) + end + inner_output = cvert + end + end + root, rest = Iterators.peel(cset) + root_var = variable_from_vertex(sys, root) + for cvert in rest + var = variable_from_vertex(sys, cvert) + push!(eqs, root_var ~ var) + end + elseif vtype === Stream push!(stream_connections, cset) elseif vtype === Flow - rhs = 0 - for ele in cset.set - v = namespaced_var(ele) - rhs += ele.isouter ? -v : v + # arrays have to be broadcasted to be added/subtracted/negated which leads + # to bad-looking equations. Just generate scalar equations instead since + # mtkcompile will scalarize anyway. + representative = variable_from_vertex(sys, cset[1]) + # each variable can have different axes, but they all have the same size + for sz_i in eachindex(eachindex(wrap(representative))) + rhs = 0 + for cvert in cset + # all of this wrapping/unwrapping is necessary because the relevant + # methods are defined on `Arr/Num` and not `BasicSymbolic`. + v = variable_from_vertex(sys, cvert)::BasicSymbolic + idxs = eachindex(wrap(v)) + v = unwrap(wrap(v)[idxs[sz_i]]) + rhs += cvert.isouter ? unwrap(-wrap(v)) : v + end + push!(eqs, 0 ~ rhs) end - push!(eqs, 0 ~ rhs) else # Equality - base = namespaced_var(cset.set[1]) - for i in 2:length(cset.set) - v = namespaced_var(cset.set[i]) + base = variable_from_vertex(sys, cset[1]) + for i in 2:length(cset) + v = variable_from_vertex(sys, cset[i]) push!(eqs, base ~ v) end end @@ -791,295 +810,212 @@ function generate_connection_equations_and_stream_connections(csets::AbstractVec eqs, stream_connections end -function domain_defaults(sys, domain_csets) - def = Dict() - for c in domain_csets - cset = c.set - idx = findfirst(s -> is_domain_connector(s.sys.sys), cset) - idx === nothing && continue - s = cset[idx] - root = s.sys - s_def = defaults(root.sys) - for (j, m) in enumerate(cset) - if j == idx - continue - elseif is_domain_connector(m.sys.sys) - error("Domain sources $(nameof(root)) and $(nameof(m)) are connected!") - else - ns_s_def = Dict(unknowns(m.sys.sys, n) => n for (n, v) in s_def) - for p in parameters(m.sys.namespace) - d_p = get(ns_s_def, p, nothing) - if d_p !== nothing - def[parameters(m.sys.namespace, p)] = parameters(s.sys.namespace, - parameters(s.sys.sys, - d_p)) - end - end - end - end - end - def -end - """ $(TYPEDSIGNATURES) -Recursively descend through the hierarchy of `sys` and expand all connection equations -of causal variables. Return the modified system. +Generate the defaults for parameters in the domain sets given by `domain_csets`. """ -function expand_variable_connections(sys::AbstractSystem; ignored_variables = nothing) - if ignored_variables === nothing - ignored_variables = ignored_connections(sys)[2] - end - eqs = copy(get_eqs(sys)) - valid_idxs = trues(length(eqs)) - additional_eqs = Equation[] - - for (i, eq) in enumerate(eqs) - eq.lhs isa Connection || continue - connection = eq.rhs - elements = get_systems(connection) - is_causal_variable_connection(connection) || continue - - valid_idxs[i] = false - elements = map(x -> x.var, elements) - to_ignore = variables_to_ignore(ignored_variables, elements) - elements = setdiff(elements, to_ignore) - outvar = first(elements) - for invar in Iterators.drop(elements, 1) - push!(additional_eqs, outvar ~ invar) +function domain_defaults( + sys::AbstractSystem, domain_csets::Vector{Vector{ConnectionVertex}}) + defs = Dict() + for cset in domain_csets + systems = map(Base.Fix1(variable_from_vertex, sys), cset) + @assert all(x -> x isa AbstractSystem, systems) + idx = findfirst(is_domain_connector, systems) + idx === nothing && continue + domain_sys = systems[idx] + # note that these will not be namespaced with `domain_sys`. + domain_defs = defaults(domain_sys) + for (j, csys) in enumerate(systems) + j == idx && continue + if is_domain_connector(csys) + throw(ArgumentError(""" + Domain sources $(nameof(domain_sys)) and $(nameof(csys)) are connected! + """)) + end + for par in parameters(csys) + defval = get(domain_defs, par, nothing) + defval === nothing && continue + defs[parameters(csys, par)] = parameters(domain_sys, par) + end end end - eqs = [eqs[valid_idxs]; additional_eqs] - subsystems = map(get_systems(sys)) do subsys - expand_variable_connections(subsys; - ignored_variables = ignored_systems_for_subsystem(subsys, ignored_variables)) - end - @set! sys.eqs = eqs - @set! sys.systems = subsystems - return sys + return defs end """ - function expand_connections(sys::AbstractSystem) + $(TYPEDSIGNATURES) Given a hierarchical system with [`connect`](@ref) equations, expand the connection -equations and return the new system. +equations and return the new system. `tol` is the tolerance for handling the singularities +in stream connection equations that happen when a flow variable approaches zero. """ -function expand_connections(sys::AbstractSystem; - debug = false, tol = 1e-10, scalarize = true) +function expand_connections(sys::AbstractSystem; tol = 1e-10) + # turn analysis points into standard connection equations sys = remove_analysis_points(sys) - sys = expand_variable_connections(sys) - sys, (csets, domain_csets) = generate_connection_set(sys; scalarize) - ceqs, instream_csets = generate_connection_equations_and_stream_connections(csets) - _sys = expand_instream(instream_csets, sys; debug = debug, tol = tol) - sys = flatten(sys, true) - @set! sys.eqs = [equations(_sys); ceqs] + # generate the connection sets + sys, (csets, domain_csets) = generate_connection_set(sys) + # generate equations, and stream equations + ceqs, instream_csets = generate_connection_equations_and_stream_connections(sys, csets) + stream_eqs, instream_subs = expand_instream(instream_csets, sys; tol = tol) + + eqs = [equations(sys); ceqs; stream_eqs] + # substitute `instream(..)` expressions with their new values + for i in eachindex(eqs) + eqs[i] = fixpoint_sub(eqs[i], instream_subs; maxiters = length(instream_subs)) + end + # get the defaults for domain networks d_defs = domain_defaults(sys, domain_csets) + # build the new system + sys = flatten(sys, true) + @set! sys.eqs = eqs @set! sys.defaults = merge(get_defaults(sys), d_defs) end -function unnamespace(root, namespace) - root === nothing && return namespace - root = string(root) - namespace = string(namespace) - if length(namespace) > length(root) - @assert root == namespace[1:length(root)] - Symbol(namespace[nextind(namespace, length(root)):end]) - else - @assert root == namespace - nothing +""" + $(TYPEDSIGNATURES) + +Given a connection vertex `cvert` referring to a variable in a connector in `sys`, return +the flow variable in that connector. +""" +function get_flowvar(sys::AbstractSystem, cvert::ConnectionVertex) + parent_names = @view cvert.name[1:(end - 1)] + parent_sys = iterative_getproperty(sys, parent_names) + for var in unknowns(parent_sys) + type = get_connection_type(var) + type == Flow || continue + return unwrap(unknowns(parent_sys, var)) end + throw(ArgumentError("There is no flow variable in system `$(nameof(parent_sys))`")) end -function expand_instream(csets::AbstractVector{<:ConnectionSet}, sys::AbstractSystem, - namespace = nothing, prevnamespace = nothing; debug = false, - tol = 1e-8) - subsys = get_systems(sys) - # post order traversal - @set! sys.systems = map( - s -> expand_instream(csets, s, - renamespace(namespace, nameof(s)), - namespace; debug, tol), - subsys) - subsys = get_systems(sys) +""" + $(TYPEDSIGNATURES) - if debug - @info "Expanding" namespace +Given connection sets of stream variables in `sys`, return the additional equations to add +to the system and the substitutions to make to handle `instream(..)` expressions. `tol` is +the tolerance for handling singularities in stream connection equations when the flow +variable approaches zero. +""" +function expand_instream(csets::Vector{Vector{ConnectionVertex}}, sys::AbstractSystem; + tol = 1e-8) + eqs = equations(sys) + # collect all `instream` terms in the equations + instream_exprs = Set{BasicSymbolic}() + for eq in eqs + collect_instream!(instream_exprs, eq) end - sub = Dict() - eqs = Equation[] - instream_eqs = Equation[] - instream_exprs = Set() - for s in subsys - for eq in get_eqs(s) - eq = namespace_equation(eq, s) - if collect_instream!(instream_exprs, eq) - push!(instream_eqs, eq) - else - push!(eqs, eq) - end - end + # specifically substitute `instream(x[i]) => instream(x)[i]` + instream_subs = Dict{BasicSymbolic, BasicSymbolic}() + for expr in instream_exprs + stream_var = only(arguments(expr)) + iscall(stream_var) && operation(stream_var) === getindex || continue + args = arguments(stream_var) + new_expr = Symbolics.array_term( + instream, args[1]; size = size(args[1]), ndims = ndims(args[1]))[args[2:end]...] + instream_subs[expr] = new_expr end - if !isempty(instream_exprs) - # map from a namespaced stream variable to a ConnectionSet - expr_cset = Dict() - for cset in csets - crep = first(cset.set) - current = namespace == _getname(crep.sys.namespace) - for v in cset.set - if (current || !v.isouter) - expr_cset[namespaced_var(v)] = cset.set - end - end - end + # for all the newly added `instream(x)[i]`, add `instream(x)` to `instream_exprs` + # also remove all `instream(x[i])` + for (k, v) in instream_subs + push!(instream_exprs, arguments(v)[1]) + delete!(instream_exprs, k) end - for ex in instream_exprs - ns_sv = only(arguments(ex)) - full_name_sv = renamespace(namespace, ns_sv) - cset = get(expr_cset, full_name_sv, nothing) - cset === nothing && error("$ns_sv is not a variable inside stream connectors") - idx_in_set, sv = get_cset_sv(full_name_sv, cset) - - n_inners = n_outers = 0 - for (i, e) in enumerate(cset) - if e.isouter - n_outers += 1 - else - n_inners += 1 - end - end - if debug - @info "Expanding at [$idx_in_set]" ex ConnectionSet(cset) n_inners n_outers - end - if n_inners == 1 && n_outers == 0 - sub[ex] = sv - elseif n_inners == 2 && n_outers == 0 - other = idx_in_set == 1 ? 2 : 1 - sub[ex] = get_current_var(namespace, cset[other], sv) - elseif n_inners == 1 && n_outers == 1 - if !cset[idx_in_set].isouter - other = idx_in_set == 1 ? 2 : 1 - outerstream = get_current_var(namespace, cset[other], sv) - sub[ex] = instream(outerstream) + # This is an implementation of the modelica spec + # https://specification.modelica.org/maint/3.6/stream-connectors.html + additional_eqs = Equation[] + for cset in csets + n_outer = count(cvert -> cvert.isouter, cset) + n_inner = length(cset) - n_outer + if n_inner == 1 && n_outer == 0 + cvert = only(cset) + stream_var = variable_from_vertex(sys, cvert)::BasicSymbolic + instream_subs[instream(stream_var)] = stream_var + elseif n_inner == 2 && n_outer == 0 + cvert1, cvert2 = cset + stream_var1 = variable_from_vertex(sys, cvert1)::BasicSymbolic + stream_var2 = variable_from_vertex(sys, cvert2)::BasicSymbolic + instream_subs[instream(stream_var1)] = stream_var2 + instream_subs[instream(stream_var2)] = stream_var1 + elseif n_inner == 1 && n_outer == 1 + cvert_inner, cvert_outer = cset + if cvert_inner.isouter + cvert_inner, cvert_outer = cvert_outer, cvert_inner end + streamvar_inner = variable_from_vertex(sys, cvert_inner)::BasicSymbolic + streamvar_outer = variable_from_vertex(sys, cvert_outer)::BasicSymbolic + instream_subs[instream(streamvar_inner)] = instream(streamvar_outer) + push!(additional_eqs, (streamvar_outer ~ streamvar_inner)) + elseif n_inner == 0 && n_outer == 2 + cvert1, cvert2 = cset + stream_var1 = variable_from_vertex(sys, cvert1)::BasicSymbolic + stream_var2 = variable_from_vertex(sys, cvert2)::BasicSymbolic + push!(additional_eqs, (stream_var1 ~ instream(stream_var2)), + (stream_var2 ~ instream(stream_var1))) else - if !cset[idx_in_set].isouter - fv = flowvar(first(cset).sys.sys) - # mj.c.m_flow - innerfvs = [get_current_var(namespace, s, fv) - for (j, s) in enumerate(cset) if j != idx_in_set && !s.isouter] - innersvs = [get_current_var(namespace, s, sv) - for (j, s) in enumerate(cset) if j != idx_in_set && !s.isouter] - # ck.m_flow - outerfvs = [get_current_var(namespace, s, fv) for s in cset if s.isouter] - outersvs = [get_current_var(namespace, s, sv) for s in cset if s.isouter] - - sub[ex] = term(instream_rt, Val(length(innerfvs)), Val(length(outerfvs)), - innerfvs..., innersvs..., outerfvs..., outersvs...) + # Currently just implements the "else" case for `instream(..)` in the suggested + # implementation of stream connectors in the Modelica spec v3.6 section 15.2. + # https://specification.modelica.org/maint/3.6/stream-connectors.html#instream-and-connection-equations + # We could implement the "if" case using variable bounds? It would be nice to + # move that metadata to the system (storing it similar to `defaults`). + outer_cverts = filter(cvert -> cvert.isouter, cset) + inner_cverts = filter(cvert -> !cvert.isouter, cset) + + outer_streamvars = map(Base.Fix1(variable_from_vertex, sys), outer_cverts) + inner_streamvars = map(Base.Fix1(variable_from_vertex, sys), inner_cverts) + + outer_flowvars = map(Base.Fix1(get_flowvar, sys), outer_cverts) + inner_flowvars = map(Base.Fix1(get_flowvar, sys), inner_cverts) + + mask = trues(length(inner_cverts)) + for inner_i in eachindex(inner_cverts) + # mask out the current variable + mask[inner_i] = false + svar = inner_streamvars[inner_i] + instream_subs[instream(svar)] = term( + instream_rt, Val(n_inner - 1), Val(n_outer), inner_flowvars[mask]..., + inner_streamvars[mask]..., outer_flowvars..., outer_streamvars...) + # make sure to reset the mask + mask[inner_i] = true end - end - end - # additional equations - additional_eqs = Equation[] - csets = filter(cset -> any(e -> _getname(e.sys.namespace) === namespace, cset.set), - csets) - for cset′ in csets - cset = cset′.set - connectors = Vector{Any}(undef, length(cset)) - n_inners = n_outers = 0 - for (i, e) in enumerate(cset) - connectors[i] = e.sys.sys - if e.isouter - n_outers += 1 - else - n_inners += 1 - end - end - iszero(n_outers) && continue - connector_representative = first(cset).sys.sys - fv = flowvar(connector_representative) - sv = first(cset).v - vtype = get_connection_type(sv) - vtype === Stream || continue - if n_inners == 1 && n_outers == 1 - push!(additional_eqs, - unknowns(cset[1].sys.sys, sv) ~ unknowns(cset[2].sys.sys, sv)) - elseif n_inners == 0 && n_outers == 2 - # we don't expand `instream` in this case. - v1 = unknowns(cset[1].sys.sys, sv) - v2 = unknowns(cset[2].sys.sys, sv) - push!(additional_eqs, v1 ~ instream(v2)) - push!(additional_eqs, v2 ~ instream(v1)) - else - sq = 0 - s_inners = (s for s in cset if !s.isouter) - s_outers = (s for s in cset if s.isouter) - for (q, oscq) in enumerate(s_outers) - sq += sum(s -> max(-unknowns(s, fv), 0), s_inners, init = 0) - for (k, s) in enumerate(s_outers) - k == q && continue - f = unknowns(s.sys.sys, fv) - sq += max(f, 0) + for q in 1:n_outer + sq = mapreduce(+, inner_flowvars) do fvar + max(-fvar, 0) end + sq += mapreduce(+, enumerate(outer_flowvars)) do (outer_i, fvar) + outer_i == q && return 0 + max(fvar, 0) + end + # sanity check to make sure it isn't going to codegen a `mapreduce` + @assert operation(sq) == (+) - num = 0 - den = 0 - for s in s_inners - f = unknowns(s.sys.sys, fv) - tmp = positivemax(-f, sq; tol = tol) - den += tmp - num += tmp * unknowns(s.sys.sys, sv) + num = mapreduce(+, inner_flowvars, inner_streamvars) do fvar, svar + positivemax(-fvar, sq; tol) * svar end - for (k, s) in enumerate(s_outers) - k == q && continue - f = unknowns(s.sys.sys, fv) - tmp = positivemax(f, sq; tol = tol) - den += tmp - num += tmp * instream(unknowns(s.sys.sys, sv)) + num += mapreduce( + +, enumerate(outer_flowvars), outer_streamvars) do (outer_i, fvar), svar + outer_i == q && return 0 + positivemax(fvar, sq; tol) * instream(svar) end - push!(additional_eqs, unknowns(oscq.sys.sys, sv) ~ num / den) - end - end - end - - subed_eqs = substitute(instream_eqs, sub) - if debug && !(isempty(csets) && isempty(additional_eqs) && isempty(instream_eqs)) - println("======================================") - @info "Additional equations" csets - display(additional_eqs) - println("======================================") - println("Substitutions") - display(sub) - println("======================================") - println("Substituted equations") - foreach(i -> println(instream_eqs[i] => subed_eqs[i]), eachindex(subed_eqs)) - println("======================================") - end + @assert operation(num) == (+) - @set! sys.systems = [] - @set! sys.eqs = [get_eqs(sys); eqs; subed_eqs; additional_eqs] - sys -end - -function get_current_var(namespace, cele, sv) - unknowns( - renamespace(unnamespace(namespace, _getname(cele.sys.namespace)), - cele.sys.sys), - sv) -end + den = mapreduce(+, inner_flowvars) do fvar + positivemax(-fvar, sq; tol) + end + den += mapreduce(+, enumerate(outer_flowvars)) do (outer_i, fvar) + outer_i == q && return 0 + positivemax(fvar, sq; tol) + end -function get_cset_sv(full_name_sv, cset) - for (idx_in_set, v) in enumerate(cset) - if isequal(namespaced_var(v), full_name_sv) - return idx_in_set, v.v + push!(additional_eqs, (outer_streamvars[q] ~ num / den)) + end end end - error("$ns_sv is not a variable inside stream connectors") + return additional_eqs, instream_subs end # instream runtime From 0a5dfee7e40a8861b95030a5ef09c2689bf36a3e Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 13 Jun 2025 18:06:53 +0530 Subject: [PATCH 2086/2176] test: minor updates to stream connector tests --- test/stream_connectors.jl | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/test/stream_connectors.jl b/test/stream_connectors.jl index 6e49cd1fb3..dfca3306f4 100644 --- a/test/stream_connectors.jl +++ b/test/stream_connectors.jl @@ -126,7 +126,7 @@ end eqns = [connect(n1m1.port_a, pipe.port_a) connect(pipe.port_b, sink.port)] -@named sys = System(eqns, t) +@named sys = System(eqns, t; systems = [n1m1, pipe, sink]) eqns = [domain_connect(fluid, n1m1.port_a) connect(n1m1.port_a, pipe.port_a) @@ -141,7 +141,7 @@ ssort(eqs) = sort(eqs, by = string) 0 ~ source.port1.m_flow - port_a.m_flow source.port1.P ~ port_a.P source.port1.P ~ source.P - source.port1.h_outflow ~ port_a.h_outflow + port_a.h_outflow ~ source.port1.h_outflow source.port1.h_outflow ~ source.h]) @unpack port_a, port_b = pipe @test ssort(equations(expand_connections(pipe))) == @@ -151,11 +151,11 @@ ssort(eqs) = sort(eqs, by = string) port_a.P ~ port_b.P port_a.h_outflow ~ instream(port_b.h_outflow) port_b.h_outflow ~ instream(port_a.h_outflow)]) -@test ssort(equations(expand_connections(sys))) == - ssort([0 ~ n1m1.port_a.m_flow + pipe.port_a.m_flow - 0 ~ pipe.port_b.m_flow + sink.port.m_flow - n1m1.port_a.P ~ pipe.port_a.P - pipe.port_b.P ~ sink.port.P]) +@test equations(expand_connections(sys)) ⊇ + [0 ~ n1m1.port_a.m_flow + pipe.port_a.m_flow + 0 ~ pipe.port_b.m_flow + sink.port.m_flow + n1m1.port_a.P ~ pipe.port_a.P + pipe.port_b.P ~ sink.port.P] @test ssort(equations(expand_connections(n1m1Test))) == ssort([0 ~ -pipe.port_a.m_flow - pipe.port_b.m_flow 0 ~ n1m1.source.port1.m_flow - n1m1.port_a.m_flow @@ -165,7 +165,7 @@ ssort(eqs) = sort(eqs, by = string) n1m1.port_a.P ~ pipe.port_a.P n1m1.source.port1.P ~ n1m1.port_a.P n1m1.source.port1.P ~ n1m1.source.P - n1m1.source.port1.h_outflow ~ n1m1.port_a.h_outflow + n1m1.port_a.h_outflow ~ n1m1.source.port1.h_outflow n1m1.source.port1.h_outflow ~ n1m1.source.h pipe.port_a.P ~ pipe.port_b.P pipe.port_a.h_outflow ~ sink.port.h_outflow @@ -278,10 +278,8 @@ sys = expand_connections(compose(simple, [vp1, vp2, vp3])) @test ssort(equations(sys)) == ssort([0 .~ collect(vp1.i) 0 .~ collect(vp2.i) 0 .~ collect(vp3.i) - vp1.v[1] ~ vp2.v[1] - vp1.v[2] ~ vp2.v[2] - vp1.v[1] ~ vp3.v[1] - vp1.v[2] ~ vp3.v[2] + vp1.v ~ vp2.v + vp1.v ~ vp3.v 0 ~ -vp1.i[1] - vp2.i[1] - vp3.i[1] 0 ~ -vp1.i[2] - vp2.i[2] - vp3.i[2]]) From 144144849c46c578fbe5e26614b064bb6099a888 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 13 Jun 2025 19:55:44 +0530 Subject: [PATCH 2087/2176] test: remove outdated causal connection validation --- test/causal_variables_connection.jl | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/test/causal_variables_connection.jl b/test/causal_variables_connection.jl index 222db540de..74e0e9dd0c 100644 --- a/test/causal_variables_connection.jl +++ b/test/causal_variables_connection.jl @@ -25,13 +25,7 @@ using ModelingToolkit: t_nounits as t, D_nounits as D @test_throws ["same size"] connect(xarr, yarr) - @test_throws ["Expected", "x", "output = true", "metadata"] connect(x, y) - @test_throws ["Expected", "y", "output = true", "metadata"] connect(y, v) - - @test_throws ["Expected", "x", "input = true", "metadata"] connect(z, x) - @test_throws ["Expected", "x", "input = true", "metadata"] connect(z, y, x) - @test_throws ["Expected", "u", "input = true", "metadata"] connect(z, u) - @test_throws ["Expected", "u", "input = true", "metadata"] connect(z, y, u) + @test_throws ArgumentError connect(x, y) end @testset "Connection expansion" begin From b295cfe360d3f7fcd6a3b8351a9591e5f81919b0 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 13 Jun 2025 19:55:55 +0530 Subject: [PATCH 2088/2176] test: remove outdated `debug` kwarg to `expand_connections` --- test/components.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/components.jl b/test/components.jl index e71ef3fa45..8e5747c750 100644 --- a/test/components.jl +++ b/test/components.jl @@ -91,7 +91,7 @@ end @named sys′ = System(eqs, t) @named sys_inner_outer = compose(sys′, [ground, shape, source, rc_comp]) @test_nowarn show(IOBuffer(), MIME"text/plain"(), sys_inner_outer) - expand_connections(sys_inner_outer, debug = true) + expand_connections(sys_inner_outer) sys_inner_outer = mtkcompile(sys_inner_outer) @test !isempty(ModelingToolkit.defaults(sys_inner_outer)) u0 = [rc_comp.capacitor.v => 0.0] From a4bc034677e1cb2a7d82a65acbe6aa4781767153 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 13 Jun 2025 23:31:49 +0530 Subject: [PATCH 2089/2176] refactor: add pretty printing for connection graph utilities --- src/systems/connectiongraph.jl | 46 ++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/src/systems/connectiongraph.jl b/src/systems/connectiongraph.jl index 70b8e2a72b..e99f200732 100644 --- a/src/systems/connectiongraph.jl +++ b/src/systems/connectiongraph.jl @@ -101,6 +101,13 @@ function Base.:(==)(a::ConnectionVertex, b::ConnectionVertex) return true end +function Base.show(io::IO, vert::ConnectionVertex) + for name in @view(vert.name[1:(end - 1)]) + print(io, name, ".") + end + print(io, vert.name[end], "::", vert.isouter ? "outer" : "inner") +end + """ $(TYPEDEF) @@ -138,6 +145,33 @@ function ConnectionGraph() return ConnectionGraph(Dict{ConnectionVertex, Int}(), ConnectionVertex[], graph) end +function Base.show(io::IO, graph::ConnectionGraph) + printstyled(io, get(io, :cgraph_name, "ConnectionGraph"); color = :blue, bold = true) + println(io, " with ", length(graph.labels), + " vertices and ", nsrcs(graph.graph), " hyperedges") + compact = get(io, :compact, false) + for edge_i in 𝑠vertices(graph.graph) + if compact && edge_i > 5 + println(io, "⋮") + break + end + edge_idxs = 𝑠neighbors(graph.graph, edge_i) + type = graph.invmap[edge_idxs[1]].type + if type <: Union{InputVar, OutputVar} + type = "Causal" + elseif type == Equality + # otherwise it prints `ModelingToolkit.Equality` + type = "Equality" + end + printstyled(io, " ", type; bold = true, color = :yellow) + print(io, "<") + for vi in @view(edge_idxs[1:(end - 1)]) + print(io, graph.invmap[vi], ", ") + end + println(io, graph.invmap[edge_idxs[end]], ">") + end +end + """ $(TYPEDSIGNATURES) @@ -208,6 +242,18 @@ Create an empty `ConnectionState` with empty graphs. """ ConnectionState() = ConnectionState(ConnectionGraph(), ConnectionGraph()) +function Base.show(io::IO, state::AbstractConnectionState) + printstyled(io, typeof(state); bold = true, color = :green) + println(io, " comprising of") + ctx1 = IOContext(io, :cgraph_name => "Connection Network", :compact => true) + show(ctx1, state.connection_graph) + println(io) + println(io, "And") + println(io) + ctx2 = IOContext(io, :cgraph_name => "Domain Network", :compact => true) + show(ctx2, state.domain_connection_graph) +end + """ $(TYPEDSIGNATURES) From c66a92f8718d10eca015d2109184803c6635b86d Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 13 Jun 2025 23:43:22 +0530 Subject: [PATCH 2090/2176] test: test improved causal variable connection semantics --- test/causal_variables_connection.jl | 31 +++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/test/causal_variables_connection.jl b/test/causal_variables_connection.jl index 74e0e9dd0c..eb922879e1 100644 --- a/test/causal_variables_connection.jl +++ b/test/causal_variables_connection.jl @@ -90,3 +90,34 @@ end @test matrices.D[] == 0 end end + +@testset "Outside input to inside input connection" begin + @mtkmodel Inner begin + @variables begin + x(t), [input = true] + y(t), [output = true] + end + @equations begin + y ~ x + end + end + @mtkmodel Outer begin + @variables begin + u(t), [input = true] + v(t), [output = true] + end + @components begin + inner = Inner() + end + @equations begin + connect(u, inner.x) + connect(inner.y, v) + end + end + @named sys = Outer() + ss = toggle_namespacing(sys, false) + eqs = equations(expand_connections(sys)) + @test issetequal(eqs, [ss.u ~ ss.inner.x + ss.inner.y ~ ss.inner.x + ss.inner.y ~ ss.v]) +end From 1f55c5c2d44cedcddcc4a7a50a8f9730167d7f53 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 16 Jun 2025 14:53:24 +0530 Subject: [PATCH 2091/2176] fix: properly distribute shifts inside called parameters --- src/structural_transformation/utils.jl | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/structural_transformation/utils.jl b/src/structural_transformation/utils.jl index 02a9763226..67eb5b0c73 100644 --- a/src/structural_transformation/utils.jl +++ b/src/structural_transformation/utils.jl @@ -596,7 +596,8 @@ function _distribute_shift(expr, shift) (op isa Union{Pre, Initial, Sample, Hold}) && return expr args = arguments(expr) - if ModelingToolkit.isvariable(expr) && operation(expr) !== getindex + if ModelingToolkit.isvariable(expr) && operation(expr) !== getindex && + !ModelingToolkit.iscalledparameter(expr) (length(args) == 1 && isequal(shift.t, only(args))) ? (return shift(expr)) : (return expr) elseif op isa Shift From 58e917fda64f7e471362a3c9ab6a553b6887a374 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 16 Jun 2025 14:53:38 +0530 Subject: [PATCH 2092/2176] fix: fix `unPre` for arrays and non-`BasicSymbolic` parameters --- src/systems/callbacks.jl | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/systems/callbacks.jl b/src/systems/callbacks.jl index cf9c53e610..745c8f382d 100644 --- a/src/systems/callbacks.jl +++ b/src/systems/callbacks.jl @@ -63,7 +63,8 @@ Base.show(io::IO, x::Pre) = print(io, "Pre") input_timedomain(::Pre, _ = nothing) = ContinuousClock() output_timedomain(::Pre, _ = nothing) = ContinuousClock() unPre(x::Num) = unPre(unwrap(x)) -unPre(x::BasicSymbolic) = (iscall(x) && operation(x) isa Pre) ? only(arguments(x)) : x +unPre(x::Symbolics.Arr) = unPre(unwrap(x)) +unPre(x::Symbolic) = (iscall(x) && operation(x) isa Pre) ? only(arguments(x)) : x function (p::Pre)(x) iw = Symbolics.iswrapped(x) From a1ee397b69df0e868bd187630ee5022704ea083b Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 16 Jun 2025 17:00:58 +0530 Subject: [PATCH 2093/2176] build: bump minor version --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index faf9e4eaf9..e23664450c 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "ModelingToolkit" uuid = "961ee093-0014-501f-94e3-6117800e7a78" authors = ["Yingbo Ma ", "Chris Rackauckas and contributors"] -version = "10.2.0" +version = "10.3.0" [deps] ADTypes = "47edcb42-4c32-4615-8424-f2b9edc5f35b" From 4004dcb3214513a76e053e88280a8e010629a0b8 Mon Sep 17 00:00:00 2001 From: vyudu Date: Mon, 16 Jun 2025 11:43:07 -0400 Subject: [PATCH 2094/2176] fix some docstrings, fix dynamic opt api page --- docs/src/API/dynamic_opt.md | 2 +- src/systems/optimal_control_interface.jl | 22 +++++++++++----------- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/docs/src/API/dynamic_opt.md b/docs/src/API/dynamic_opt.md index b086fca522..1d94bc2108 100644 --- a/docs/src/API/dynamic_opt.md +++ b/docs/src/API/dynamic_opt.md @@ -1,4 +1,4 @@ -### Solvers +# [Dynamic Optimization Solvers](@id dynamic_opt_api) Currently 4 backends are exposed for solving dynamic optimization problems using collocation: JuMP, InfiniteOpt, CasADi, and Pyomo. diff --git a/src/systems/optimal_control_interface.jl b/src/systems/optimal_control_interface.jl index ea19afea86..7651c9a235 100644 --- a/src/systems/optimal_control_interface.jl +++ b/src/systems/optimal_control_interface.jl @@ -19,7 +19,7 @@ function Base.show(io::IO, sol::DynamicOptSolution) end """ - JuMPDynamicOptProblem(sys::System, u0, tspan, p; dt, steps, guesses, kwargs...) + JuMPDynamicOptProblem(sys::System, op, tspan; dt, steps, guesses, kwargs...) Convert an System representing an optimal control system into a JuMP model for solving using optimization. Must provide either `dt`, the timestep between collocation @@ -43,7 +43,7 @@ To construct the problem, please load InfiniteOpt along with ModelingToolkit. """ function InfiniteOptDynamicOptProblem end """ - CasADiDynamicOptProblem(sys::System, u0, tspan, p; dt, steps, guesses, kwargs...) + CasADiDynamicOptProblem(sys::System, op, tspan; dt, steps, guesses, kwargs...) Convert an System representing an optimal control system into a CasADi model for solving using optimization. Must provide either `dt`, the timestep between collocation @@ -54,7 +54,7 @@ To construct the problem, please load CasADi along with ModelingToolkit. """ function CasADiDynamicOptProblem end """ - PyomoDynamicOptProblem(sys::System, u0, tspan, p; dt, steps) + PyomoDynamicOptProblem(sys::System, op, tspan; dt, steps) Convert an System representing an optimal control system into a Pyomo model for solving using optimization. Must provide either `dt`, the timestep between collocation @@ -67,27 +67,27 @@ function PyomoDynamicOptProblem end ### Collocations """ -JuMP Collocation solver. -- solver: a optimization solver such as Ipopt -- tableau: An ODE RK tableau. Load a tableau by calling a function like `constructRK4` and may be found at https://docs.sciml.ai/DiffEqDevDocs/stable/internals/tableaus/. If this argument is not passed in, the solver will default to Radau second order. +JuMP Collocation solver. Takes two arguments: +- `solver`: a optimization solver such as Ipopt +- `tableau`: An ODE RK tableau. Load a tableau by calling a function like `constructRK4` and may be found at https://docs.sciml.ai/DiffEqDevDocs/stable/internals/tableaus/. If this argument is not passed in, the solver will default to Radau second order. """ function JuMPCollocation end """ InfiniteOpt Collocation solver. -- solver: an optimization solver such as Ipopt +- `solver`: an optimization solver such as Ipopt - `derivative_method`: the method used by InfiniteOpt to compute derivatives. The list of possible options can be found at https://infiniteopt.github.io/InfiniteOpt.jl/stable/guide/derivative/. Defaults to FiniteDifference(Backward()). """ function InfiniteOptCollocation end """ CasADi Collocation solver. -- solver: an optimization solver such as Ipopt. Should be given as a string or symbol in all lowercase, e.g. "ipopt" -- tableau: An ODE RK tableau. Load a tableau by calling a function like `constructRK4` and may be found at https://docs.sciml.ai/DiffEqDevDocs/stable/internals/tableaus/. If this argument is not passed in, the solver will default to Radau second order. +- `solver`: an optimization solver such as Ipopt. Should be given as a string or symbol in all lowercase, e.g. "ipopt" +- `tableau`: An ODE RK tableau. Load a tableau by calling a function like `constructRK4` and may be found at https://docs.sciml.ai/DiffEqDevDocs/stable/internals/tableaus/. If this argument is not passed in, the solver will default to Radau second order. """ function CasADiCollocation end """ Pyomo Collocation solver. -- solver: an optimization solver such as Ipopt. Should be given as a string or symbol in all lowercase, e.g. "ipopt" -- derivative_method: a derivative method from Pyomo. The choices here are ForwardEuler, BackwardEuler, MidpointEuler, LagrangeRadau, or LagrangeLegendre. The last two should additionally have a number indicating the number of collocation points per timestep, e.g. PyomoCollocation("ipopt", LagrangeRadau(3)). Defaults to LagrangeRadau(5). +- `solver`: an optimization solver such as Ipopt. Should be given as a string or symbol in all lowercase, e.g. "ipopt" +- `derivative_method`: a derivative method from Pyomo. The choices here are ForwardEuler, BackwardEuler, MidpointEuler, LagrangeRadau, or LagrangeLegendre. The last two should additionally have a number indicating the number of collocation points per timestep, e.g. PyomoCollocation("ipopt", LagrangeRadau(3)). Defaults to LagrangeRadau(5). """ function PyomoCollocation end From 1843ac12fbe04dc14d81823d1909b3cc75b04dd4 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 17 Jun 2025 13:04:33 +0530 Subject: [PATCH 2095/2176] docs: fix incorrect FAQ section --- docs/src/basics/FAQ.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/src/basics/FAQ.md b/docs/src/basics/FAQ.md index 1a1ffe75ca..96a65095cf 100644 --- a/docs/src/basics/FAQ.md +++ b/docs/src/basics/FAQ.md @@ -27,7 +27,7 @@ are similarly undocumented. Following is the list of behaviors that should be re parameter with the given index. - `setindex!(::MTKParameters, value, ::ParameterIndex)` can be used to set the value of a parameter with the given index. - - `parameter_values(sys, sym)` will return a `ParameterIndex` object if `sys` has been + - `parameter_index(sys, sym)` will return a `ParameterIndex` object if `sys` has been `complete`d (through `mtkcompile`, `complete` or `@mtkcompile`). - `copy(::MTKParameters)` is defined and duplicates the parameter object, including the memory used by the underlying buffers. From d0765de9c9681175085846950f877b163317aa4b Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 17 Jun 2025 13:04:45 +0530 Subject: [PATCH 2096/2176] refactor: improve error message for incorrectly named analysis point --- src/systems/analysis_points.jl | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/systems/analysis_points.jl b/src/systems/analysis_points.jl index 4171042908..fa0809dd3c 100644 --- a/src/systems/analysis_points.jl +++ b/src/systems/analysis_points.jl @@ -348,7 +348,12 @@ function modify_nested_subsystem( end # ignore the name of the root if nameof(root) != hierarchy[1] - error("The name of the root system $(nameof(root)) must be included in the name passed to `modify_nested_subsystem`") + error(""" + Invalid analysis point name `$(join(hierarchy, NAMESPACE_SEPARATOR))`. The name + must include the name of the root system `$(nameof(root))`. This typically happens + when using an analysis point obtained by calling `getproperty` on a system marked + as `complete` to linearize a system that is not marked as `complete`. + """) end hierarchy = @view hierarchy[2:end] From 621e7ac88da5ce1d26344a304a184c7609a1bcb2 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 17 Jun 2025 14:33:24 +0530 Subject: [PATCH 2097/2176] fix: fix deprecated `@brownian` not working properly --- src/deprecations.jl | 2 +- test/sdesystem.jl | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/src/deprecations.jl b/src/deprecations.jl index 56c166f575..7ccd323060 100644 --- a/src/deprecations.jl +++ b/src/deprecations.jl @@ -175,5 +175,5 @@ macro brownian(xs...) return quote Base.depwarn("`@brownian` is deprecated. Use `@brownians` instead", :brownian_macro) $(@__MODULE__).@brownians $(xs...) - end + end |> esc end diff --git a/test/sdesystem.jl b/test/sdesystem.jl index f60e3a9777..f3bb3ba9c9 100644 --- a/test/sdesystem.jl +++ b/test/sdesystem.jl @@ -948,4 +948,8 @@ end @testset "`@brownian` is deprecated" begin @test_deprecated @brownian a b c + + @brownian p q + @test ModelingToolkit.isbrownian(p) + @test ModelingToolkit.isbrownian(q) end From da13368b5efecac98fdbbc5dbb08a958bb381358 Mon Sep 17 00:00:00 2001 From: Herman Sletmoen Date: Tue, 17 Jun 2025 14:37:58 +0200 Subject: [PATCH 2098/2176] Ensure DDEs are univariate --- src/systems/abstractsystem.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index f95cf90eea..4fb2bdfd4c 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -320,7 +320,7 @@ for traitT in [ elseif is_timeseries_parameter(sys, s) push!(ts_idxs, timeseries_parameter_index(sys, s).timeseries_idx) elseif is_time_dependent(sys) && iscall(s) && issym(operation(s)) && - is_variable(sys, operation(s)(get_iv(sys))) + length(arguments(s)) == 1 && is_variable(sys, operation(s)(get_iv(sys))) # DDEs case, to detect x(t - k) push!(ts_idxs, ContinuousTimeseries()) else From 977ceb604fa57946502c1f44b6d23e948bac5b3e Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 18 Jun 2025 11:55:27 +0530 Subject: [PATCH 2099/2176] feat: add `is_transparent_operator` trait --- src/discretedomain.jl | 15 +++++++++++++++ src/systems/systemstructure.jl | 2 +- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/discretedomain.jl b/src/discretedomain.jl index a10fec81b4..da8417de4e 100644 --- a/src/discretedomain.jl +++ b/src/discretedomain.jl @@ -1,5 +1,15 @@ using Symbolics: Operator, Num, Term, value, recursive_hasoperator +""" + $(TYPEDSIGNATURES) + +Trait to be implemented for operators which determines whether application of the operator +generates a semantically different variable or not. For example, `Differential` and `Shift` +are not transparent but `Sample` and `Hold` are. Defaults to `false` if not implemented. +""" +is_transparent_operator(x) = is_transparent_operator(typeof(x)) +is_transparent_operator(::Type) = false + """ function SampleTime() @@ -128,6 +138,8 @@ struct Sample <: Operator Sample(clock::Union{TimeDomain, InferredTimeDomain} = InferredDiscrete()) = new(clock) end +is_transparent_operator(::Type{Sample}) = true + function Sample(arg::Real) arg = unwrap(arg) if symbolic_type(arg) == NotSymbolic() @@ -179,6 +191,9 @@ cont_x = Hold()(disc_x) """ struct Hold <: Operator end + +is_transparent_operator(::Type{Hold}) = true + (D::Hold)(x) = Term{symtype(x)}(D, Any[x]) (D::Hold)(x::Num) = Num(D(value(x))) SymbolicUtils.promote_symtype(::Hold, x) = x diff --git a/src/systems/systemstructure.jl b/src/systems/systemstructure.jl index 7e00f6ef95..e368641350 100644 --- a/src/systems/systemstructure.jl +++ b/src/systems/systemstructure.jl @@ -363,7 +363,7 @@ function TearingState(sys; quick_cancel = false, check = true, sort_eqs = true) isdelay(v, iv) && continue if !symbolic_contains(v, dvs) - isvalid = iscall(v) && operation(v) isa Union{Shift, Sample, Hold} + isvalid = iscall(v) && (operation(v) isa Shift || is_transparent_operator(operation(v))) v′ = v while !isvalid && iscall(v′) && operation(v′) isa Union{Differential, Shift} v′ = arguments(v′)[1] From 829a8b8603d01439d9dfe18002e88b3727f0481c Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 17 Jun 2025 19:38:27 +0530 Subject: [PATCH 2100/2176] fix: appropriately toterm defaults when creating `DiscreteProblem` --- src/systems/problem_utils.jl | 2 +- test/discrete_system.jl | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/systems/problem_utils.jl b/src/systems/problem_utils.jl index e24f37331f..f785de798f 100644 --- a/src/systems/problem_utils.jl +++ b/src/systems/problem_utils.jl @@ -1309,7 +1309,7 @@ function process_SciMLProblem( check_inputmap_keys(sys, op) - defs = add_toterms(recursive_unwrap(defaults(sys))) + defs = add_toterms(recursive_unwrap(defaults(sys)); replace = is_discrete_system(sys)) kwargs = NamedTuple(kwargs) if eltype(eqs) <: Equation diff --git a/test/discrete_system.jl b/test/discrete_system.jl index d3db4df5f2..ccd5b2c0a9 100644 --- a/test/discrete_system.jl +++ b/test/discrete_system.jl @@ -274,3 +274,21 @@ end sol = solve(prob, FunctionMap()) @test sol[[x..., y...], end] == 8ones(4) end + +@testset "Defaults are totermed appropriately" begin + @parameters σ ρ β q + @variables x(t) y(t) z(t) + k = ShiftIndex(t) + p = [σ => 28.0, + ρ => 10.0, + β => 8 / 3] + + @mtkcompile discsys = System( + [x ~ x(k - 1) * ρ + y(k - 2), y ~ y(k - 1) * σ - z(k - 2), + z ~ z(k - 1) * β + x(k - 2)], + t; defaults = [x => 1.0, y => 1.0, z => 1.0, x(k - 1) => 1.0, + y(k - 1) => 1.0, z(k - 1) => 1.0]) + discprob = DiscreteProblem(discsys, p, (0, 10)) + sol = solve(discprob, FunctionMap()) + @test SciMLBase.successful_retcode(sol) +end From 84dbc1c56db3e71e72edc5212267710b61fdd168 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 16 Jun 2025 14:54:02 +0530 Subject: [PATCH 2101/2176] fix: pass operating point to `ImplicitDiscreteProblem` in `generate_equational_affect` --- src/problems/daeproblem.jl | 2 +- src/problems/ddeproblem.jl | 2 +- src/problems/jumpproblem.jl | 3 ++- src/problems/odeproblem.jl | 2 +- src/problems/sddeproblem.jl | 2 +- src/problems/sdeproblem.jl | 2 +- src/systems/callbacks.jl | 24 +++++++++++++++++++++--- test/symbolic_events.jl | 27 +++++++++++++++++++++++++++ 8 files changed, 55 insertions(+), 9 deletions(-) diff --git a/src/problems/daeproblem.jl b/src/problems/daeproblem.jl index 6134923d4d..d98e275f17 100644 --- a/src/problems/daeproblem.jl +++ b/src/problems/daeproblem.jl @@ -72,7 +72,7 @@ end eval_module, check_compatibility, implicit_dae = true, expression, kwargs...) kwargs = process_kwargs(sys; expression, callback, eval_expression, eval_module, - kwargs...) + op, kwargs...) diffvars = collect_differential_variables(sys) sts = unknowns(sys) diff --git a/src/problems/ddeproblem.jl b/src/problems/ddeproblem.jl index 45af3edddb..4fb9b3f37b 100644 --- a/src/problems/ddeproblem.jl +++ b/src/problems/ddeproblem.jl @@ -66,7 +66,7 @@ end end kwargs = process_kwargs( - sys; expression, callback, eval_expression, eval_module, kwargs...) + sys; expression, callback, eval_expression, eval_module, op, kwargs...) args = (; f, u0, h, tspan, p) return maybe_codegen_scimlproblem(expression, DDEProblem{iip}, args; kwargs...) diff --git a/src/problems/jumpproblem.jl b/src/problems/jumpproblem.jl index 28de46c35f..dd3fd9ba1c 100644 --- a/src/problems/jumpproblem.jl +++ b/src/problems/jumpproblem.jl @@ -80,7 +80,8 @@ end # handle events, making sure to reset aggregators in the generated affect functions - cbs = process_events(sys; callback, eval_expression, eval_module, reset_jumps = true) + cbs = process_events( + sys; callback, eval_expression, eval_module, op, reset_jumps = true) if rng !== nothing kwargs = (; kwargs..., rng) diff --git a/src/problems/odeproblem.jl b/src/problems/odeproblem.jl index 6726322907..bc8b9cf701 100644 --- a/src/problems/odeproblem.jl +++ b/src/problems/odeproblem.jl @@ -75,7 +75,7 @@ end eval_module, expression, check_compatibility, kwargs...) kwargs = process_kwargs( - sys; expression, callback, eval_expression, eval_module, kwargs...) + sys; expression, callback, eval_expression, eval_module, op, kwargs...) ptype = getmetadata(sys, ProblemTypeCtx, StandardODEProblem()) args = (; f, u0, tspan, p, ptype) diff --git a/src/problems/sddeproblem.jl b/src/problems/sddeproblem.jl index e1cc00b2a7..0e3201c1d1 100644 --- a/src/problems/sddeproblem.jl +++ b/src/problems/sddeproblem.jl @@ -68,7 +68,7 @@ end end noise, noise_rate_prototype = calculate_noise_and_rate_prototype(sys, u0; sparsenoise) - kwargs = process_kwargs(sys; callback, eval_expression, eval_module, kwargs...) + kwargs = process_kwargs(sys; callback, eval_expression, eval_module, op, kwargs...) if expression == Val{true} g = :(f.g) diff --git a/src/problems/sdeproblem.jl b/src/problems/sdeproblem.jl index 1bc47118ff..83a050f7e4 100644 --- a/src/problems/sdeproblem.jl +++ b/src/problems/sdeproblem.jl @@ -78,7 +78,7 @@ end noise, noise_rate_prototype = calculate_noise_and_rate_prototype(sys, u0; sparsenoise) kwargs = process_kwargs(sys; expression, callback, eval_expression, eval_module, - kwargs...) + op, kwargs...) args = (; f, u0, tspan, p) kwargs = (; noise, noise_rate_prototype, kwargs...) diff --git a/src/systems/callbacks.jl b/src/systems/callbacks.jl index 745c8f382d..a4f39243d9 100644 --- a/src/systems/callbacks.jl +++ b/src/systems/callbacks.jl @@ -798,16 +798,34 @@ function add_integrator_header( expr.body) end +function default_operating_point(affsys::AffectSystem) + sys = system(affsys) + + op = Dict(unknowns(sys) .=> 0.0) + for p in parameters(sys) + T = symtype(p) + if T <: Number + op[p] = false + elseif T <: Array{<:Real} && is_sized_array_symbolic(p) + op[p] = zeros(size(p)) + end + end + return op +end + """ Compile an affect defined by a set of equations. Systems with algebraic equations will solve implicit discrete problems to obtain their next state. Systems without will generate functions that perform explicit updates. """ function compile_equational_affect( aff::Union{AffectSystem, Vector{Equation}}, sys; reset_jumps = false, - eval_expression = false, eval_module = @__MODULE__, kwargs...) + eval_expression = false, eval_module = @__MODULE__, op = nothing, kwargs...) if aff isa AbstractVector aff = make_affect( aff; iv = get_iv(sys), warn_no_algebraic = false) end + if op === nothing + op = default_operating_point(aff) + end affsys = system(aff) ps_to_update = discretes(aff) dvs_to_update = setdiff(unknowns(aff), getfield.(observed(sys), :lhs)) @@ -872,10 +890,10 @@ function compile_equational_affect( p_getter = getsym(affsys, ps_to_update) affprob = ImplicitDiscreteProblem( - affsys, Pair[unknowns(affsys) .=> 0; parameters(affsys) .=> 0], + affsys, op, (0, 0); build_initializeprob = false, check_length = false, eval_expression, - eval_module, check_compatibility = false) + eval_module, check_compatibility = false, kwargs...) function implicit_affect!(integ) new_u0 = affu_getter(integ) diff --git a/test/symbolic_events.jl b/test/symbolic_events.jl index 856a97484f..d82bf8a5c1 100644 --- a/test/symbolic_events.jl +++ b/test/symbolic_events.jl @@ -1348,3 +1348,30 @@ end @test SciMLBase.successful_retcode(sol) @test sol[inner.p][end] ≈ 1.0 end + +mutable struct ParamTest + y::Any +end + +@testset "callable parameter and symbolic affect" begin + (pt::ParamTest)(x) = pt.y - x + + p1 = ParamTest(1) + tp1 = typeof(p1) + @parameters (p_1::tp1)(..) = p1 + @parameters p2(t) = 1.0 + @variables x(t) = 0.0 + @variables x2(t) + event = [0.5] => [p2 ~ Pre(t)] + + eq = [ + D(x) ~ p2, + x2 ~ p_1(x) + ] + @mtkcompile sys = ODESystem(eq, t, [x, x2], [p_1, p2], discrete_events = [event]) + + prob = ODEProblem(sys, [], (0.0, 1.0)) + sol = solve(prob) + @test SciMLBase.successful_retcode(sol) + @test sol[x, end]≈1.0 atol=1e-6 +end From e494ca2f408d281d88d7ae4c974c660084863f7a Mon Sep 17 00:00:00 2001 From: Christopher Rackauckas Date: Wed, 18 Jun 2025 11:31:01 +0000 Subject: [PATCH 2102/2176] Update Downstream.yml --- .github/workflows/Downstream.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/Downstream.yml b/.github/workflows/Downstream.yml index 6d79ee746a..ec79fa994f 100644 --- a/.github/workflows/Downstream.yml +++ b/.github/workflows/Downstream.yml @@ -26,6 +26,7 @@ jobs: os: [ubuntu-latest] package: - {user: SciML, repo: SciMLBase.jl, group: Downstream} + - {user: SciML, repo: SciMLBase.jl, group: SymbolicIndexingInterface} - {user: SciML, repo: Catalyst.jl, group: All} - {user: SciML, repo: CellMLToolkit.jl, group: Core} - {user: SciML, repo: SBMLToolkit.jl, group: All} From ba5e20b2b7fcde5889f629f60b843072295349c4 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 18 Jun 2025 12:51:39 +0530 Subject: [PATCH 2103/2176] feat: better handle loop opening defaults --- src/systems/analysis_points.jl | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/src/systems/analysis_points.jl b/src/systems/analysis_points.jl index fa0809dd3c..392fda03ad 100644 --- a/src/systems/analysis_points.jl +++ b/src/systems/analysis_points.jl @@ -486,14 +486,19 @@ struct Break <: AnalysisPointTransformation Whether to add a new input variable connected to all the outputs of `ap`. """ add_input::Bool + """ + Whether the default of the added input variable should be the input of `ap`. Only + applicable if `add_input == true`. + """ + default_outputs_to_input::Bool end """ $(TYPEDSIGNATURES) -`Break` the given analysis point `ap` without adding an input. +`Break` the given analysis point `ap`. """ -Break(ap::AnalysisPoint) = Break(ap, false) +Break(ap::AnalysisPoint, add_input::Bool = false) = Break(ap, add_input, false) function apply_transformation(tf::Break, sys::AbstractSystem) modify_nested_subsystem(sys, tf.ap) do breaksys @@ -517,7 +522,11 @@ function apply_transformation(tf::Break, sys::AbstractSystem) push!(breaksys_eqs, ap_var(outsys) ~ new_var) end defs = copy(get_defaults(breaksys)) - defs[new_var] = new_def + defs[new_var] = if tf.default_outputs_to_input + ap_ivar + else + new_def + end @set! breaksys.defaults = defs unks = copy(get_unknowns(breaksys)) push!(unks, new_var) @@ -803,7 +812,7 @@ Given a list of analysis points, break the connection for each and set the outpu """ function handle_loop_openings(sys::AbstractSystem, aps) for ap in canonicalize_ap(sys, aps) - sys, (outvar,) = apply_transformation(Break(ap, true), sys) + sys, (outvar,) = apply_transformation(Break(ap, true, true), sys) if Symbolics.isarraysymbolic(outvar) push!(get_eqs(sys), outvar ~ zeros(size(outvar))) else @@ -815,7 +824,7 @@ end const DOC_LOOP_OPENINGS = """ - `loop_openings`: A list of analysis points whose connections should be removed and - the outputs set to zero as a part of the linear analysis. + the outputs set to the input as a part of the linear analysis. """ const DOC_SYS_MODIFIER = """ @@ -952,7 +961,7 @@ function linearization_ap_transform(sys, for input in inputs if nameof(input) in loop_openings delete!(loop_openings, nameof(input)) - sys, (input_var,) = apply_transformation(Break(input, true), sys) + sys, (input_var,) = apply_transformation(Break(input, true, true), sys) else sys, (input_var,) = apply_transformation(PerturbOutput(input), sys) end From 8f17cdbd8da54c5f80ee6f44e8f47f5a6e04969d Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 18 Jun 2025 12:51:53 +0530 Subject: [PATCH 2104/2176] test: move downstream AP tests to main testset --- test/analysis_points.jl | 498 ++++++++++++++++++++++++++++- test/downstream/analysis_points.jl | 496 ---------------------------- test/runtests.jl | 3 +- 3 files changed, 498 insertions(+), 499 deletions(-) delete mode 100644 test/downstream/analysis_points.jl diff --git a/test/analysis_points.jl b/test/analysis_points.jl index 903a74cc84..505e5ca813 100644 --- a/test/analysis_points.jl +++ b/test/analysis_points.jl @@ -1,8 +1,11 @@ -using ModelingToolkit, ModelingToolkitStandardLibrary.Blocks +using ModelingToolkit, ModelingToolkitStandardLibrary.Blocks, ControlSystemsBase +using ModelingToolkitStandardLibrary.Mechanical.Rotational +using ModelingToolkitStandardLibrary.Blocks using OrdinaryDiffEq, LinearAlgebra using Test using ModelingToolkit: t_nounits as t, D_nounits as D, AnalysisPoint, AbstractSystem import ModelingToolkit as MTK +import ControlSystemsBase as CS using Symbolics: NAMESPACE_SEPARATOR @testset "AnalysisPoint is lowered to `connect`" begin @@ -184,3 +187,496 @@ end @test matrices.B[] * matrices.C[] == 1 # both positive @test matrices.D[] == 0 end + +@testset "Complicated model" begin + # Parameters + m1 = 1 + m2 = 1 + k = 1000 # Spring stiffness + c = 10 # Damping coefficient + @named inertia1 = Inertia(; J = m1) + @named inertia2 = Inertia(; J = m2) + @named spring = Spring(; c = k) + @named damper = Damper(; d = c) + @named torque = Torque() + + function SystemModel(u = nothing; name = :model) + eqs = [connect(torque.flange, inertia1.flange_a) + connect(inertia1.flange_b, spring.flange_a, damper.flange_a) + connect(inertia2.flange_a, spring.flange_b, damper.flange_b)] + if u !== nothing + push!(eqs, connect(torque.tau, u.output)) + return System(eqs, t; + systems = [ + torque, + inertia1, + inertia2, + spring, + damper, + u + ], + name) + end + System(eqs, t; systems = [torque, inertia1, inertia2, spring, damper], name) + end + + @named r = Step(start_time = 0) + model = SystemModel() + @named pid = PID(k = 100, Ti = 0.5, Td = 1) + @named filt = SecondOrder(d = 0.9, w = 10) + @named sensor = AngleSensor() + @named er = Add(k2 = -1) + + connections = [connect(r.output, :r, filt.input) + connect(filt.output, er.input1) + connect(pid.ctr_output, :u, model.torque.tau) + connect(model.inertia2.flange_b, sensor.flange) + connect(sensor.phi, :y, er.input2) + connect(er.output, :e, pid.err_input)] + + closed_loop = System(connections, t, systems = [model, pid, filt, sensor, r, er], + name = :closed_loop, defaults = [ + model.inertia1.phi => 0.0, + model.inertia2.phi => 0.0, + model.inertia1.w => 0.0, + model.inertia2.w => 0.0, + filt.x => 0.0, + filt.xd => 0.0 + ]) + + sys = mtkcompile(closed_loop) + prob = ODEProblem(sys, unknowns(sys) .=> 0.0, (0.0, 4.0)) + sol = solve(prob, Rodas5P(), reltol = 1e-6, abstol = 1e-9) + + matrices, ssys = linearize(closed_loop, :r, :y) + lsys = ss(matrices...) |> sminreal + @test lsys.nx == 8 + + stepres = ControlSystemsBase.step(c2d(lsys, 0.001), 4) + @test Array(stepres.y[:])≈Array(sol(0:0.001:4, idxs = model.inertia2.phi)) rtol=1e-4 + + matrices, ssys = get_sensitivity(closed_loop, :y) + So = ss(matrices...) + + matrices, ssys = get_sensitivity(closed_loop, :u) + Si = ss(matrices...) + + @test tf(So) ≈ tf(Si) +end + +@testset "Duplicate `connect` statements across subsystems with AP transforms - standard `connect`" begin + @named P = FirstOrder(k = 1, T = 1) + @named C = Gain(; k = 1) + @named add = Blocks.Add(k2 = -1) + + eqs = [connect(P.output, :plant_output, add.input2) + connect(add.output, C.input) + connect(C.output, P.input)] + + sys_inner = System(eqs, t, systems = [P, C, add], name = :inner) + + @named r = Constant(k = 1) + @named F = FirstOrder(k = 1, T = 3) + + eqs = [connect(r.output, F.input) + connect(sys_inner.P.output, sys_inner.add.input2) + connect(sys_inner.C.output, :plant_input, sys_inner.P.input) + connect(F.output, sys_inner.add.input1)] + sys_outer = System(eqs, t, systems = [F, sys_inner, r], name = :outer) + + # test first that the mtkcompile works correctly + ssys = mtkcompile(sys_outer) + prob = ODEProblem(ssys, Pair[], (0, 10)) + @test_nowarn solve(prob, Rodas5()) + + matrices, _ = get_sensitivity(sys_outer, sys_outer.plant_input) + lsys = sminreal(ss(matrices...)) + @test lsys.A[] == -2 + @test lsys.B[] * lsys.C[] == -1 # either one negative + @test lsys.D[] == 1 + + matrices_So, _ = get_sensitivity(sys_outer, sys_outer.inner.plant_output) + lsyso = sminreal(ss(matrices_So...)) + @test lsys == lsyso || lsys == -1 * lsyso * (-1) # Output and input sensitivities are equal for SISO systems +end + +@testset "Duplicate `connect` statements across subsystems with AP transforms - causal variable `connect`" begin + @named P = FirstOrder(k = 1, T = 1) + @named C = Gain(; k = 1) + @named add = Blocks.Add(k2 = -1) + + eqs = [connect(P.output.u, :plant_output, add.input2.u) + connect(add.output, C.input) + connect(C.output.u, P.input.u)] + + sys_inner = System(eqs, t, systems = [P, C, add], name = :inner) + + @named r = Constant(k = 1) + @named F = FirstOrder(k = 1, T = 3) + + eqs = [connect(r.output, F.input) + connect(sys_inner.P.output.u, sys_inner.add.input2.u) + connect(sys_inner.C.output.u, :plant_input, sys_inner.P.input.u) + connect(F.output, sys_inner.add.input1)] + sys_outer = System(eqs, t, systems = [F, sys_inner, r], name = :outer) + + # test first that the mtkcompile works correctly + ssys = mtkcompile(sys_outer) + prob = ODEProblem(ssys, Pair[], (0, 10)) + @test_nowarn solve(prob, Rodas5()) + + matrices, _ = get_sensitivity(sys_outer, sys_outer.plant_input) + lsys = sminreal(ss(matrices...)) + @test lsys.A[] == -2 + @test lsys.B[] * lsys.C[] == -1 # either one negative + @test lsys.D[] == 1 + + matrices_So, _ = get_sensitivity(sys_outer, sys_outer.inner.plant_output) + lsyso = sminreal(ss(matrices_So...)) + @test lsys == lsyso || lsys == -1 * lsyso * (-1) # Output and input sensitivities are equal for SISO systems +end + +@testset "Duplicate `connect` statements across subsystems with AP transforms - mixed `connect`" begin + @named P = FirstOrder(k = 1, T = 1) + @named C = Gain(; k = 1) + @named add = Blocks.Add(k2 = -1) + + eqs = [connect(P.output.u, :plant_output, add.input2.u) + connect(add.output, C.input) + connect(C.output, P.input)] + + sys_inner = System(eqs, t, systems = [P, C, add], name = :inner) + + @named r = Constant(k = 1) + @named F = FirstOrder(k = 1, T = 3) + + eqs = [connect(r.output, F.input) + connect(sys_inner.P.output, sys_inner.add.input2) + connect(sys_inner.C.output.u, :plant_input, sys_inner.P.input.u) + connect(F.output, sys_inner.add.input1)] + sys_outer = System(eqs, t, systems = [F, sys_inner, r], name = :outer) + + # test first that the mtkcompile works correctly + ssys = mtkcompile(sys_outer) + prob = ODEProblem(ssys, Pair[], (0, 10)) + @test_nowarn solve(prob, Rodas5()) + + matrices, _ = get_sensitivity(sys_outer, sys_outer.plant_input) + lsys = sminreal(ss(matrices...)) + @test lsys.A[] == -2 + @test lsys.B[] * lsys.C[] == -1 # either one negative + @test lsys.D[] == 1 + + matrices_So, _ = get_sensitivity(sys_outer, sys_outer.inner.plant_output) + lsyso = sminreal(ss(matrices_So...)) + @test lsys == lsyso || lsys == -1 * lsyso * (-1) # Output and input sensitivities are equal for SISO systems +end + +@testset "multilevel system with loop openings" begin + @named P_inner = FirstOrder(k = 1, T = 1) + @named feedback = Feedback() + @named ref = Step() + @named sys_inner = System( + [connect(P_inner.output, :y, feedback.input2) + connect(feedback.output, :u, P_inner.input) + connect(ref.output, :r, feedback.input1)], + t, + systems = [P_inner, feedback, ref]) + + P_not_broken, _ = linearize(sys_inner, :u, :y) + @test P_not_broken.A[] == -2 + P_broken, ssys = linearize(sys_inner, :u, :y, loop_openings = [:u]) + @test isequal(defaults(ssys)[ssys.d_u], ssys.feedback.output.u) + @test P_broken.A[] == -1 + P_broken, ssys = linearize(sys_inner, :u, :y, loop_openings = [:y]) + @test isequal(defaults(ssys)[ssys.d_y], ssys.P_inner.output.u) + @test P_broken.A[] == -1 + + Sinner = sminreal(ss(get_sensitivity(sys_inner, :u)[1]...)) + + @named sys_inner = System( + [connect(P_inner.output, :y, feedback.input2) + connect(feedback.output, :u, P_inner.input)], + t, + systems = [P_inner, feedback]) + + @named P_outer = FirstOrder(k = rand(), T = rand()) + + @named sys_outer = System( + [connect(sys_inner.P_inner.output, :y2, P_outer.input) + connect(P_outer.output, :u2, sys_inner.feedback.input1)], + t, + systems = [P_outer, sys_inner]) + + Souter = sminreal(ss(get_sensitivity(sys_outer, sys_outer.sys_inner.u)[1]...)) + + Sinner2 = sminreal(ss(get_sensitivity( + sys_outer, sys_outer.sys_inner.u, loop_openings = [:y2])[1]...)) + + @test Sinner.nx == 1 + @test Sinner == Sinner2 + @test Souter.nx == 2 +end + +@testset "sensitivities in multivariate signals" begin + A = [-0.994 -0.0794; -0.006242 -0.0134] + B = [-0.181 -0.389; 1.1 1.12] + C = [1.74 0.72; -0.33 0.33] + D = [0.0 0.0; 0.0 0.0] + @named P = Blocks.StateSpace(A, B, C, D) + Pss = CS.ss(A, B, C, D) + + A = [-0.097;;] + B = [-0.138 -1.02] + C = [-0.076; 0.09;;] + D = [0.0 0.0; 0.0 0.0] + @named K = Blocks.StateSpace(A, B, C, D) + Kss = CS.ss(A, B, C, D) + + eqs = [connect(P.output, :plant_output, K.input) + connect(K.output, :plant_input, P.input)] + sys = System(eqs, t, systems = [P, K], name = :hej) + + matrices, _ = get_sensitivity(sys, :plant_input) + S = CS.feedback(I(2), Kss * Pss, pos_feedback = true) + + @test CS.tf(CS.ss(matrices...)) ≈ CS.tf(S) + + matrices, _ = get_comp_sensitivity(sys, :plant_input) + T = -CS.feedback(Kss * Pss, I(2), pos_feedback = true) + + # bodeplot([ss(matrices...), T]) + @test CS.tf(CS.ss(matrices...)) ≈ CS.tf(T) + + matrices, _ = get_looptransfer( + sys, :plant_input) + L = Kss * Pss + @test CS.tf(CS.ss(matrices...)) ≈ CS.tf(L) + + matrices, _ = linearize(sys, AnalysisPoint(:plant_input), :plant_output) + G = CS.feedback(Pss, Kss, pos_feedback = true) + @test CS.tf(CS.ss(matrices...)) ≈ CS.tf(G) +end + +@testset "multiple analysis points" begin + @named P = FirstOrder(k = 1, T = 1) + @named C = Gain(; k = 1) + @named add = Blocks.Add(k2 = -1) + + eqs = [connect(P.output, :plant_output, add.input2) + connect(add.output, C.input) + connect(C.output, :plant_input, P.input)] + + sys_inner = System(eqs, t, systems = [P, C, add], name = :inner) + + @named r = Constant(k = 1) + @named F = FirstOrder(k = 1, T = 3) + + eqs = [connect(r.output, F.input) + connect(F.output, sys_inner.add.input1)] + sys_outer = System(eqs, t, systems = [F, sys_inner, r], name = :outer) + + matrices, _ = get_sensitivity( + sys_outer, [sys_outer.inner.plant_input, sys_outer.inner.plant_output]) + + Ps = tf(1, [1, 1]) |> ss + Cs = tf(1) |> ss + + G = CS.ss(matrices...) |> sminreal + Si = CS.feedback(1, Cs * Ps) + @test tf(G[1, 1]) ≈ tf(Si) + + So = CS.feedback(1, Ps * Cs) + @test tf(G[2, 2]) ≈ tf(So) + @test tf(G[1, 2]) ≈ tf(-CS.feedback(Cs, Ps)) + @test tf(G[2, 1]) ≈ tf(CS.feedback(Ps, Cs)) + + matrices, _ = get_comp_sensitivity( + sys_outer, [sys_outer.inner.plant_input, sys_outer.inner.plant_output]) + + G = CS.ss(matrices...) |> sminreal + Ti = CS.feedback(Cs * Ps) + @test tf(G[1, 1]) ≈ tf(Ti) + + To = CS.feedback(Ps * Cs) + @test tf(G[2, 2]) ≈ tf(To) + @test tf(G[1, 2]) ≈ tf(CS.feedback(Cs, Ps)) # The negative sign appears in a confusing place due to negative feedback not happening through Ps + @test tf(G[2, 1]) ≈ tf(-CS.feedback(Ps, Cs)) + + # matrices, _ = get_looptransfer(sys_outer, [:inner_plant_input, :inner_plant_output]) + matrices, _ = get_looptransfer(sys_outer, sys_outer.inner.plant_input) + L = CS.ss(matrices...) |> sminreal + @test tf(L) ≈ -tf(Cs * Ps) + + matrices, _ = get_looptransfer(sys_outer, sys_outer.inner.plant_output) + L = CS.ss(matrices...) |> sminreal + @test tf(L[1, 1]) ≈ -tf(Ps * Cs) + + # Calling looptransfer like below is not the intended way, but we can work out what it should return if we did so it remains a valid test + matrices, _ = get_looptransfer( + sys_outer, [sys_outer.inner.plant_input, sys_outer.inner.plant_output]) + L = CS.ss(matrices...) |> sminreal + @test tf(L[1, 1]) ≈ tf(0) + @test tf(L[2, 2]) ≈ tf(0) + @test sminreal(L[1, 2]) ≈ ss(-1) + @test tf(L[2, 1]) ≈ tf(Ps) + + matrices, _ = linearize( + sys_outer, [sys_outer.inner.plant_input], [nameof(sys_inner.plant_output)]) + G = CS.ss(matrices...) |> sminreal + @test tf(G) ≈ tf(CS.feedback(Ps, Cs)) +end + +function normal_test_system() + @named F1 = FirstOrder(k = 1, T = 1) + @named F2 = FirstOrder(k = 1, T = 1) + @named add = Blocks.Add(k1 = 1, k2 = 2) + @named back = Feedback() + + eqs_normal = [connect(back.output, :ap, F1.input) + connect(back.output, F2.input) + connect(F1.output, add.input1) + connect(F2.output, add.input2) + connect(add.output, back.input2)] + @named normal_inner = System(eqs_normal, t; systems = [F1, F2, add, back]) + + @named step = Step() + eqs2_normal = [ + connect(step.output, normal_inner.back.input1) + ] + @named sys_normal = System(eqs2_normal, t; systems = [normal_inner, step]) +end + +sys_normal = normal_test_system() + +prob = ODEProblem(mtkcompile(sys_normal), [], (0.0, 10.0)) +@test SciMLBase.successful_retcode(solve(prob, Rodas5P())) +matrices_normal, _ = get_sensitivity(sys_normal, sys_normal.normal_inner.ap) + +@testset "Analysis point overriding part of connection - normal connect" begin + @named F1 = FirstOrder(k = 1, T = 1) + @named F2 = FirstOrder(k = 1, T = 1) + @named add = Blocks.Add(k1 = 1, k2 = 2) + @named back = Feedback() + + eqs = [connect(back.output, F1.input, F2.input) + connect(F1.output, add.input1) + connect(F2.output, add.input2) + connect(add.output, back.input2)] + @named inner = System(eqs, t; systems = [F1, F2, add, back]) + + @named step = Step() + eqs2 = [connect(step.output, inner.back.input1) + connect(inner.back.output, :ap, inner.F1.input)] + @named sys = System(eqs2, t; systems = [inner, step]) + + prob = ODEProblem(mtkcompile(sys), [], (0.0, 10.0)) + @test SciMLBase.successful_retcode(solve(prob, Rodas5P())) + + matrices, _ = get_sensitivity(sys, sys.ap) + @test matrices == matrices_normal +end + +@testset "Analysis point overriding part of connection - variable connect" begin + @named F1 = FirstOrder(k = 1, T = 1) + @named F2 = FirstOrder(k = 1, T = 1) + @named add = Blocks.Add(k1 = 1, k2 = 2) + @named back = Feedback() + + eqs = [connect(back.output.u, F1.input.u, F2.input.u) + connect(F1.output, add.input1) + connect(F2.output, add.input2) + connect(add.output, back.input2)] + @named inner = System(eqs, t; systems = [F1, F2, add, back]) + + @named step = Step() + eqs2 = [connect(step.output, inner.back.input1) + connect(inner.back.output.u, :ap, inner.F1.input.u)] + @named sys = System(eqs2, t; systems = [inner, step]) + + prob = ODEProblem(mtkcompile(sys), [], (0.0, 10.0)) + @test SciMLBase.successful_retcode(solve(prob, Rodas5P())) + + matrices, _ = get_sensitivity(sys, sys.ap) + @test matrices == matrices_normal +end + +@testset "Analysis point overriding part of connection - mixed connect" begin + @named F1 = FirstOrder(k = 1, T = 1) + @named F2 = FirstOrder(k = 1, T = 1) + @named add = Blocks.Add(k1 = 1, k2 = 2) + @named back = Feedback() + + eqs = [connect(back.output, F1.input, F2.input) + connect(F1.output, add.input1) + connect(F2.output, add.input2) + connect(add.output, back.input2)] + @named inner = System(eqs, t; systems = [F1, F2, add, back]) + + @named step = Step() + eqs2 = [connect(step.output, inner.back.input1) + connect(inner.back.output.u, :ap, inner.F1.input.u)] + @named sys = System(eqs2, t; systems = [inner, step]) + + prob = ODEProblem(mtkcompile(sys), [], (0.0, 10.0)) + @test SciMLBase.successful_retcode(solve(prob, Rodas5P())) + + matrices, _ = get_sensitivity(sys, sys.ap) + @test matrices == matrices_normal +end + +@testset "Ignored analysis points only affect relevant connection sets" begin + m1 = 1 + m2 = 1 + k = 1000 # Spring stiffness + c = 10 # Damping coefficient + + @named inertia1 = Inertia(; J = m1, phi = 0, w = 0) + @named inertia2 = Inertia(; J = m2, phi = 0, w = 0) + + @named spring = Spring(; c = k) + @named damper = Damper(; d = c) + + @named torque = Torque(use_support = false) + + function SystemModel(u = nothing; name = :model) + eqs = [connect(torque.flange, inertia1.flange_a) + connect(inertia1.flange_b, spring.flange_a, damper.flange_a) + connect(inertia2.flange_a, spring.flange_b, damper.flange_b)] + if u !== nothing + push!(eqs, connect(torque.tau, u.output)) + return @named model = System( + eqs, t; systems = [torque, inertia1, inertia2, spring, damper, u]) + end + System(eqs, t; systems = [torque, inertia1, inertia2, spring, damper], name) + end + + @named r = Step(start_time = 1) + @named pid = LimPID(k = 400, Ti = 0.5, Td = 1, u_max = 350) + @named filt = SecondOrder(d = 0.9, w = 10, x = 0, xd = 0) + @named sensor = AngleSensor() + @named add = Add() # To add the feedback and feedforward control signals + model = SystemModel() + @named inverse_model = SystemModel() + @named inverse_sensor = AngleSensor() + connections = [connect(r.output, :r, filt.input) # Name connection r to form an analysis point + connect(inverse_model.inertia1.flange_b, inverse_sensor.flange) # Attach the inverse sensor to the inverse model + connect(filt.output, pid.reference, inverse_sensor.phi) # the filtered reference now goes to both the PID controller and the inverse model input + connect(inverse_model.torque.tau, add.input1) + connect(pid.ctr_output, add.input2) + connect(add.output, :u, model.torque.tau) # Name connection u to form an analysis point + connect(model.inertia1.flange_b, sensor.flange) + connect(sensor.phi, :y, pid.measurement)] + closed_loop = System(connections, t, + systems = [model, inverse_model, pid, filt, sensor, inverse_sensor, r, add], + name = :closed_loop) + # just ensure the system simplifies + mats, _ = get_sensitivity(closed_loop, :y) + S = CS.ss(mats...) + fr = CS.freqrespv(S, [0.01, 1, 100]) + # https://github.com/SciML/ModelingToolkit.jl/pull/3469 + reference_fr = ComplexF64[-1.2505330104772838e-11 - 2.500062613816021e-9im, + -0.0024688370221621625 - 0.002279011866413123im, + 1.8100018764334602 + 0.3623845793211718im] + @test isapprox(fr, reference_fr) +end diff --git a/test/downstream/analysis_points.jl b/test/downstream/analysis_points.jl deleted file mode 100644 index 21c33b2068..0000000000 --- a/test/downstream/analysis_points.jl +++ /dev/null @@ -1,496 +0,0 @@ -using ModelingToolkit, OrdinaryDiffEqRosenbrock, LinearAlgebra, ControlSystemsBase -using ModelingToolkitStandardLibrary.Mechanical.Rotational -using ModelingToolkitStandardLibrary.Blocks -using ModelingToolkit: connect, t_nounits as t, D_nounits as D -import ControlSystemsBase as CS - -@testset "Complicated model" begin - # Parameters - m1 = 1 - m2 = 1 - k = 1000 # Spring stiffness - c = 10 # Damping coefficient - @named inertia1 = Inertia(; J = m1) - @named inertia2 = Inertia(; J = m2) - @named spring = Spring(; c = k) - @named damper = Damper(; d = c) - @named torque = Torque() - - function SystemModel(u = nothing; name = :model) - eqs = [connect(torque.flange, inertia1.flange_a) - connect(inertia1.flange_b, spring.flange_a, damper.flange_a) - connect(inertia2.flange_a, spring.flange_b, damper.flange_b)] - if u !== nothing - push!(eqs, connect(torque.tau, u.output)) - return System(eqs, t; - systems = [ - torque, - inertia1, - inertia2, - spring, - damper, - u - ], - name) - end - System(eqs, t; systems = [torque, inertia1, inertia2, spring, damper], name) - end - - @named r = Step(start_time = 0) - model = SystemModel() - @named pid = PID(k = 100, Ti = 0.5, Td = 1) - @named filt = SecondOrder(d = 0.9, w = 10) - @named sensor = AngleSensor() - @named er = Add(k2 = -1) - - connections = [connect(r.output, :r, filt.input) - connect(filt.output, er.input1) - connect(pid.ctr_output, :u, model.torque.tau) - connect(model.inertia2.flange_b, sensor.flange) - connect(sensor.phi, :y, er.input2) - connect(er.output, :e, pid.err_input)] - - closed_loop = System(connections, t, systems = [model, pid, filt, sensor, r, er], - name = :closed_loop, defaults = [ - model.inertia1.phi => 0.0, - model.inertia2.phi => 0.0, - model.inertia1.w => 0.0, - model.inertia2.w => 0.0, - filt.x => 0.0, - filt.xd => 0.0 - ]) - - sys = mtkcompile(closed_loop) - prob = ODEProblem(sys, unknowns(sys) .=> 0.0, (0.0, 4.0)) - sol = solve(prob, Rodas5P(), reltol = 1e-6, abstol = 1e-9) - - matrices, ssys = linearize(closed_loop, :r, :y) - lsys = ss(matrices...) |> sminreal - @test lsys.nx == 8 - - stepres = ControlSystemsBase.step(c2d(lsys, 0.001), 4) - @test Array(stepres.y[:])≈Array(sol(0:0.001:4, idxs = model.inertia2.phi)) rtol=1e-4 - - matrices, ssys = get_sensitivity(closed_loop, :y) - So = ss(matrices...) - - matrices, ssys = get_sensitivity(closed_loop, :u) - Si = ss(matrices...) - - @test tf(So) ≈ tf(Si) -end - -@testset "Duplicate `connect` statements across subsystems with AP transforms - standard `connect`" begin - @named P = FirstOrder(k = 1, T = 1) - @named C = Gain(; k = 1) - @named add = Blocks.Add(k2 = -1) - - eqs = [connect(P.output, :plant_output, add.input2) - connect(add.output, C.input) - connect(C.output, P.input)] - - sys_inner = System(eqs, t, systems = [P, C, add], name = :inner) - - @named r = Constant(k = 1) - @named F = FirstOrder(k = 1, T = 3) - - eqs = [connect(r.output, F.input) - connect(sys_inner.P.output, sys_inner.add.input2) - connect(sys_inner.C.output, :plant_input, sys_inner.P.input) - connect(F.output, sys_inner.add.input1)] - sys_outer = System(eqs, t, systems = [F, sys_inner, r], name = :outer) - - # test first that the mtkcompile works correctly - ssys = mtkcompile(sys_outer) - prob = ODEProblem(ssys, Pair[], (0, 10)) - @test_nowarn solve(prob, Rodas5()) - - matrices, _ = get_sensitivity(sys_outer, sys_outer.plant_input) - lsys = sminreal(ss(matrices...)) - @test lsys.A[] == -2 - @test lsys.B[] * lsys.C[] == -1 # either one negative - @test lsys.D[] == 1 - - matrices_So, _ = get_sensitivity(sys_outer, sys_outer.inner.plant_output) - lsyso = sminreal(ss(matrices_So...)) - @test lsys == lsyso || lsys == -1 * lsyso * (-1) # Output and input sensitivities are equal for SISO systems -end - -@testset "Duplicate `connect` statements across subsystems with AP transforms - causal variable `connect`" begin - @named P = FirstOrder(k = 1, T = 1) - @named C = Gain(; k = 1) - @named add = Blocks.Add(k2 = -1) - - eqs = [connect(P.output.u, :plant_output, add.input2.u) - connect(add.output, C.input) - connect(C.output.u, P.input.u)] - - sys_inner = System(eqs, t, systems = [P, C, add], name = :inner) - - @named r = Constant(k = 1) - @named F = FirstOrder(k = 1, T = 3) - - eqs = [connect(r.output, F.input) - connect(sys_inner.P.output.u, sys_inner.add.input2.u) - connect(sys_inner.C.output.u, :plant_input, sys_inner.P.input.u) - connect(F.output, sys_inner.add.input1)] - sys_outer = System(eqs, t, systems = [F, sys_inner, r], name = :outer) - - # test first that the mtkcompile works correctly - ssys = mtkcompile(sys_outer) - prob = ODEProblem(ssys, Pair[], (0, 10)) - @test_nowarn solve(prob, Rodas5()) - - matrices, _ = get_sensitivity(sys_outer, sys_outer.plant_input) - lsys = sminreal(ss(matrices...)) - @test lsys.A[] == -2 - @test lsys.B[] * lsys.C[] == -1 # either one negative - @test lsys.D[] == 1 - - matrices_So, _ = get_sensitivity(sys_outer, sys_outer.inner.plant_output) - lsyso = sminreal(ss(matrices_So...)) - @test lsys == lsyso || lsys == -1 * lsyso * (-1) # Output and input sensitivities are equal for SISO systems -end - -@testset "Duplicate `connect` statements across subsystems with AP transforms - mixed `connect`" begin - @named P = FirstOrder(k = 1, T = 1) - @named C = Gain(; k = 1) - @named add = Blocks.Add(k2 = -1) - - eqs = [connect(P.output.u, :plant_output, add.input2.u) - connect(add.output, C.input) - connect(C.output, P.input)] - - sys_inner = System(eqs, t, systems = [P, C, add], name = :inner) - - @named r = Constant(k = 1) - @named F = FirstOrder(k = 1, T = 3) - - eqs = [connect(r.output, F.input) - connect(sys_inner.P.output, sys_inner.add.input2) - connect(sys_inner.C.output.u, :plant_input, sys_inner.P.input.u) - connect(F.output, sys_inner.add.input1)] - sys_outer = System(eqs, t, systems = [F, sys_inner, r], name = :outer) - - # test first that the mtkcompile works correctly - ssys = mtkcompile(sys_outer) - prob = ODEProblem(ssys, Pair[], (0, 10)) - @test_nowarn solve(prob, Rodas5()) - - matrices, _ = get_sensitivity(sys_outer, sys_outer.plant_input) - lsys = sminreal(ss(matrices...)) - @test lsys.A[] == -2 - @test lsys.B[] * lsys.C[] == -1 # either one negative - @test lsys.D[] == 1 - - matrices_So, _ = get_sensitivity(sys_outer, sys_outer.inner.plant_output) - lsyso = sminreal(ss(matrices_So...)) - @test lsys == lsyso || lsys == -1 * lsyso * (-1) # Output and input sensitivities are equal for SISO systems -end - -@testset "multilevel system with loop openings" begin - @named P_inner = FirstOrder(k = 1, T = 1) - @named feedback = Feedback() - @named ref = Step() - @named sys_inner = System( - [connect(P_inner.output, :y, feedback.input2) - connect(feedback.output, :u, P_inner.input) - connect(ref.output, :r, feedback.input1)], - t, - systems = [P_inner, feedback, ref]) - - P_not_broken, _ = linearize(sys_inner, :u, :y) - @test P_not_broken.A[] == -2 - P_broken, _ = linearize(sys_inner, :u, :y, loop_openings = [:u]) - @test P_broken.A[] == -1 - P_broken, _ = linearize(sys_inner, :u, :y, loop_openings = [:y]) - @test P_broken.A[] == -1 - - Sinner = sminreal(ss(get_sensitivity(sys_inner, :u)[1]...)) - - @named sys_inner = System( - [connect(P_inner.output, :y, feedback.input2) - connect(feedback.output, :u, P_inner.input)], - t, - systems = [P_inner, feedback]) - - @named P_outer = FirstOrder(k = rand(), T = rand()) - - @named sys_outer = System( - [connect(sys_inner.P_inner.output, :y2, P_outer.input) - connect(P_outer.output, :u2, sys_inner.feedback.input1)], - t, - systems = [P_outer, sys_inner]) - - Souter = sminreal(ss(get_sensitivity(sys_outer, sys_outer.sys_inner.u)[1]...)) - - Sinner2 = sminreal(ss(get_sensitivity( - sys_outer, sys_outer.sys_inner.u, loop_openings = [:y2])[1]...)) - - @test Sinner.nx == 1 - @test Sinner == Sinner2 - @test Souter.nx == 2 -end - -@testset "sensitivities in multivariate signals" begin - A = [-0.994 -0.0794; -0.006242 -0.0134] - B = [-0.181 -0.389; 1.1 1.12] - C = [1.74 0.72; -0.33 0.33] - D = [0.0 0.0; 0.0 0.0] - @named P = Blocks.StateSpace(A, B, C, D) - Pss = CS.ss(A, B, C, D) - - A = [-0.097;;] - B = [-0.138 -1.02] - C = [-0.076; 0.09;;] - D = [0.0 0.0; 0.0 0.0] - @named K = Blocks.StateSpace(A, B, C, D) - Kss = CS.ss(A, B, C, D) - - eqs = [connect(P.output, :plant_output, K.input) - connect(K.output, :plant_input, P.input)] - sys = System(eqs, t, systems = [P, K], name = :hej) - - matrices, _ = get_sensitivity(sys, :plant_input) - S = CS.feedback(I(2), Kss * Pss, pos_feedback = true) - - @test CS.tf(CS.ss(matrices...)) ≈ CS.tf(S) - - matrices, _ = get_comp_sensitivity(sys, :plant_input) - T = -CS.feedback(Kss * Pss, I(2), pos_feedback = true) - - # bodeplot([ss(matrices...), T]) - @test CS.tf(CS.ss(matrices...)) ≈ CS.tf(T) - - matrices, _ = get_looptransfer( - sys, :plant_input) - L = Kss * Pss - @test CS.tf(CS.ss(matrices...)) ≈ CS.tf(L) - - matrices, _ = linearize(sys, AnalysisPoint(:plant_input), :plant_output) - G = CS.feedback(Pss, Kss, pos_feedback = true) - @test CS.tf(CS.ss(matrices...)) ≈ CS.tf(G) -end - -@testset "multiple analysis points" begin - @named P = FirstOrder(k = 1, T = 1) - @named C = Gain(; k = 1) - @named add = Blocks.Add(k2 = -1) - - eqs = [connect(P.output, :plant_output, add.input2) - connect(add.output, C.input) - connect(C.output, :plant_input, P.input)] - - sys_inner = System(eqs, t, systems = [P, C, add], name = :inner) - - @named r = Constant(k = 1) - @named F = FirstOrder(k = 1, T = 3) - - eqs = [connect(r.output, F.input) - connect(F.output, sys_inner.add.input1)] - sys_outer = System(eqs, t, systems = [F, sys_inner, r], name = :outer) - - matrices, _ = get_sensitivity( - sys_outer, [sys_outer.inner.plant_input, sys_outer.inner.plant_output]) - - Ps = tf(1, [1, 1]) |> ss - Cs = tf(1) |> ss - - G = CS.ss(matrices...) |> sminreal - Si = CS.feedback(1, Cs * Ps) - @test tf(G[1, 1]) ≈ tf(Si) - - So = CS.feedback(1, Ps * Cs) - @test tf(G[2, 2]) ≈ tf(So) - @test tf(G[1, 2]) ≈ tf(-CS.feedback(Cs, Ps)) - @test tf(G[2, 1]) ≈ tf(CS.feedback(Ps, Cs)) - - matrices, _ = get_comp_sensitivity( - sys_outer, [sys_outer.inner.plant_input, sys_outer.inner.plant_output]) - - G = CS.ss(matrices...) |> sminreal - Ti = CS.feedback(Cs * Ps) - @test tf(G[1, 1]) ≈ tf(Ti) - - To = CS.feedback(Ps * Cs) - @test tf(G[2, 2]) ≈ tf(To) - @test tf(G[1, 2]) ≈ tf(CS.feedback(Cs, Ps)) # The negative sign appears in a confusing place due to negative feedback not happening through Ps - @test tf(G[2, 1]) ≈ tf(-CS.feedback(Ps, Cs)) - - # matrices, _ = get_looptransfer(sys_outer, [:inner_plant_input, :inner_plant_output]) - matrices, _ = get_looptransfer(sys_outer, sys_outer.inner.plant_input) - L = CS.ss(matrices...) |> sminreal - @test tf(L) ≈ -tf(Cs * Ps) - - matrices, _ = get_looptransfer(sys_outer, sys_outer.inner.plant_output) - L = CS.ss(matrices...) |> sminreal - @test tf(L[1, 1]) ≈ -tf(Ps * Cs) - - # Calling looptransfer like below is not the intended way, but we can work out what it should return if we did so it remains a valid test - matrices, _ = get_looptransfer( - sys_outer, [sys_outer.inner.plant_input, sys_outer.inner.plant_output]) - L = CS.ss(matrices...) |> sminreal - @test tf(L[1, 1]) ≈ tf(0) - @test tf(L[2, 2]) ≈ tf(0) - @test sminreal(L[1, 2]) ≈ ss(-1) - @test tf(L[2, 1]) ≈ tf(Ps) - - matrices, _ = linearize( - sys_outer, [sys_outer.inner.plant_input], [nameof(sys_inner.plant_output)]) - G = CS.ss(matrices...) |> sminreal - @test tf(G) ≈ tf(CS.feedback(Ps, Cs)) -end - -function normal_test_system() - @named F1 = FirstOrder(k = 1, T = 1) - @named F2 = FirstOrder(k = 1, T = 1) - @named add = Blocks.Add(k1 = 1, k2 = 2) - @named back = Feedback() - - eqs_normal = [connect(back.output, :ap, F1.input) - connect(back.output, F2.input) - connect(F1.output, add.input1) - connect(F2.output, add.input2) - connect(add.output, back.input2)] - @named normal_inner = System(eqs_normal, t; systems = [F1, F2, add, back]) - - @named step = Step() - eqs2_normal = [ - connect(step.output, normal_inner.back.input1) - ] - @named sys_normal = System(eqs2_normal, t; systems = [normal_inner, step]) -end - -sys_normal = normal_test_system() - -prob = ODEProblem(mtkcompile(sys_normal), [], (0.0, 10.0)) -@test SciMLBase.successful_retcode(solve(prob, Rodas5P())) -matrices_normal, _ = get_sensitivity(sys_normal, sys_normal.normal_inner.ap) - -@testset "Analysis point overriding part of connection - normal connect" begin - @named F1 = FirstOrder(k = 1, T = 1) - @named F2 = FirstOrder(k = 1, T = 1) - @named add = Blocks.Add(k1 = 1, k2 = 2) - @named back = Feedback() - - eqs = [connect(back.output, F1.input, F2.input) - connect(F1.output, add.input1) - connect(F2.output, add.input2) - connect(add.output, back.input2)] - @named inner = System(eqs, t; systems = [F1, F2, add, back]) - - @named step = Step() - eqs2 = [connect(step.output, inner.back.input1) - connect(inner.back.output, :ap, inner.F1.input)] - @named sys = System(eqs2, t; systems = [inner, step]) - - prob = ODEProblem(mtkcompile(sys), [], (0.0, 10.0)) - @test SciMLBase.successful_retcode(solve(prob, Rodas5P())) - - matrices, _ = get_sensitivity(sys, sys.ap) - @test matrices == matrices_normal -end - -@testset "Analysis point overriding part of connection - variable connect" begin - @named F1 = FirstOrder(k = 1, T = 1) - @named F2 = FirstOrder(k = 1, T = 1) - @named add = Blocks.Add(k1 = 1, k2 = 2) - @named back = Feedback() - - eqs = [connect(back.output.u, F1.input.u, F2.input.u) - connect(F1.output, add.input1) - connect(F2.output, add.input2) - connect(add.output, back.input2)] - @named inner = System(eqs, t; systems = [F1, F2, add, back]) - - @named step = Step() - eqs2 = [connect(step.output, inner.back.input1) - connect(inner.back.output.u, :ap, inner.F1.input.u)] - @named sys = System(eqs2, t; systems = [inner, step]) - - prob = ODEProblem(mtkcompile(sys), [], (0.0, 10.0)) - @test SciMLBase.successful_retcode(solve(prob, Rodas5P())) - - matrices, _ = get_sensitivity(sys, sys.ap) - @test matrices == matrices_normal -end - -@testset "Analysis point overriding part of connection - mixed connect" begin - @named F1 = FirstOrder(k = 1, T = 1) - @named F2 = FirstOrder(k = 1, T = 1) - @named add = Blocks.Add(k1 = 1, k2 = 2) - @named back = Feedback() - - eqs = [connect(back.output, F1.input, F2.input) - connect(F1.output, add.input1) - connect(F2.output, add.input2) - connect(add.output, back.input2)] - @named inner = System(eqs, t; systems = [F1, F2, add, back]) - - @named step = Step() - eqs2 = [connect(step.output, inner.back.input1) - connect(inner.back.output.u, :ap, inner.F1.input.u)] - @named sys = System(eqs2, t; systems = [inner, step]) - - prob = ODEProblem(mtkcompile(sys), [], (0.0, 10.0)) - @test SciMLBase.successful_retcode(solve(prob, Rodas5P())) - - matrices, _ = get_sensitivity(sys, sys.ap) - @test matrices == matrices_normal -end - -@testset "Ignored analysis points only affect relevant connection sets" begin - m1 = 1 - m2 = 1 - k = 1000 # Spring stiffness - c = 10 # Damping coefficient - - @named inertia1 = Inertia(; J = m1, phi = 0, w = 0) - @named inertia2 = Inertia(; J = m2, phi = 0, w = 0) - - @named spring = Spring(; c = k) - @named damper = Damper(; d = c) - - @named torque = Torque(use_support = false) - - function SystemModel(u = nothing; name = :model) - eqs = [connect(torque.flange, inertia1.flange_a) - connect(inertia1.flange_b, spring.flange_a, damper.flange_a) - connect(inertia2.flange_a, spring.flange_b, damper.flange_b)] - if u !== nothing - push!(eqs, connect(torque.tau, u.output)) - return @named model = System( - eqs, t; systems = [torque, inertia1, inertia2, spring, damper, u]) - end - System(eqs, t; systems = [torque, inertia1, inertia2, spring, damper], name) - end - - @named r = Step(start_time = 1) - @named pid = LimPID(k = 400, Ti = 0.5, Td = 1, u_max = 350) - @named filt = SecondOrder(d = 0.9, w = 10, x = 0, xd = 0) - @named sensor = AngleSensor() - @named add = Add() # To add the feedback and feedforward control signals - model = SystemModel() - @named inverse_model = SystemModel() - @named inverse_sensor = AngleSensor() - connections = [connect(r.output, :r, filt.input) # Name connection r to form an analysis point - connect(inverse_model.inertia1.flange_b, inverse_sensor.flange) # Attach the inverse sensor to the inverse model - connect(filt.output, pid.reference, inverse_sensor.phi) # the filtered reference now goes to both the PID controller and the inverse model input - connect(inverse_model.torque.tau, add.input1) - connect(pid.ctr_output, add.input2) - connect(add.output, :u, model.torque.tau) # Name connection u to form an analysis point - connect(model.inertia1.flange_b, sensor.flange) - connect(sensor.phi, :y, pid.measurement)] - closed_loop = System(connections, t, - systems = [model, inverse_model, pid, filt, sensor, inverse_sensor, r, add], - name = :closed_loop) - # just ensure the system simplifies - mats, _ = get_sensitivity(closed_loop, :y) - S = CS.ss(mats...) - fr = CS.freqrespv(S, [0.01, 1, 100]) - # https://github.com/SciML/ModelingToolkit.jl/pull/3469 - reference_fr = ComplexF64[-1.2505330104772838e-11 - 2.500062613816021e-9im, - -0.0024688370221621625 - 0.002279011866413123im, - 1.8100018764334602 + 0.3623845793211718im] - @test isapprox(fr, reference_fr) -end diff --git a/test/runtests.jl b/test/runtests.jl index 89701342d0..47230c9539 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -125,8 +125,7 @@ end activate_downstream_env() @safetestset "Linearization Dummy Derivative Tests" include("downstream/linearization_dd.jl") @safetestset "Inverse Models Test" include("downstream/inversemodel.jl") - @safetestset "Analysis Points Test" include("downstream/analysis_points.jl") - @safetestset "Analysis Points Test" include("downstream/test_disturbance_model.jl") + @safetestset "Disturbance model Test" include("downstream/test_disturbance_model.jl") end if GROUP == "All" || GROUP == "FMI" From 39ad303647cc556bb70a377edd42b64e6b9343e8 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 18 Jun 2025 17:18:34 +0530 Subject: [PATCH 2105/2176] fix: do not remove input of negative connection in ignored connect handling --- src/systems/connectiongraph.jl | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/systems/connectiongraph.jl b/src/systems/connectiongraph.jl index e99f200732..5147748d67 100644 --- a/src/systems/connectiongraph.jl +++ b/src/systems/connectiongraph.jl @@ -414,10 +414,6 @@ function remove_negative_connections!( # if there is any other variable, start removing push!(idxs_to_rm[edge_j], var_j) end - if should_rm - # if there was any other variable, also remove `input_j` - push!(idxs_to_rm, input_j) - end end end From 9153443c39abc24d3020fe70739f5f968442a129 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 18 Jun 2025 17:18:51 +0530 Subject: [PATCH 2106/2176] fix: ignore empty hyperedges during connectionset generation --- src/systems/connectiongraph.jl | 1 + 1 file changed, 1 insertion(+) diff --git a/src/systems/connectiongraph.jl b/src/systems/connectiongraph.jl index 5147748d67..5c5e8716c6 100644 --- a/src/systems/connectiongraph.jl +++ b/src/systems/connectiongraph.jl @@ -455,6 +455,7 @@ function connectionsets(graph::ConnectionGraph) disjoint_sets = IntDisjointSets(length(invmap)) for edge_i in 𝑠vertices(bigraph) hyperedge = 𝑠neighbors(bigraph, edge_i) + isempty(hyperedge) && continue root, rest = Iterators.peel(hyperedge) for vert in rest union!(disjoint_sets, root, vert) From c6fe424a79ff3234f39b3759e88da460d74dcdfb Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 18 Jun 2025 17:19:28 +0530 Subject: [PATCH 2107/2176] fix: make ignored causal connections affect equivalent `Equality` connections --- src/systems/connectors.jl | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/systems/connectors.jl b/src/systems/connectors.jl index 14f90ff14e..226534b82a 100644 --- a/src/systems/connectors.jl +++ b/src/systems/connectors.jl @@ -468,6 +468,16 @@ function _generate_connectionsets!(connection_state::AbstractConnectionState, [namespace; var_ns], length(var_ns) == 1 || isouter(var_ns[1]), type) end add_connection_edge!(connection_state, hyperedge) + + # Removed analysis points generate causal connections in the negative graph. These + # should also remove `Equality` connections involving the same variables, so also + # add an `Equality` variant of the edge. + if connection_state isa NegativeConnectionState + hyperedge = map(hyperedge) do cvert + ConnectionVertex(cvert.name, cvert.isouter, Equality) + end + add_connection_edge!(connection_state, hyperedge) + end end end From 92b395acf86c4248c07230d8401e77e9655d8c09 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 18 Jun 2025 17:19:57 +0530 Subject: [PATCH 2108/2176] fix: allow passing `t` to `linearization_function` --- src/linearization.jl | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/linearization.jl b/src/linearization.jl index ca816f4ae8..b0254490dd 100644 --- a/src/linearization.jl +++ b/src/linearization.jl @@ -45,6 +45,7 @@ function linearization_function(sys::AbstractSystem, inputs, warn_initialize_determined = true, guesses = Dict(), warn_empty_op = true, + t = 0.0, kwargs...) op = Dict(op) if isempty(op) && warn_empty_op @@ -73,7 +74,7 @@ function linearization_function(sys::AbstractSystem, inputs, end prob = ODEProblem{true, SciMLBase.FullSpecialize}( - sys, merge(op, anydict(p)), (nothing, nothing); allow_incomplete = true, + sys, merge(op, anydict(p)), (t, t); allow_incomplete = true, algebraic_only = true, guesses) u0 = state_values(prob) @@ -753,7 +754,7 @@ function linearize(sys, inputs, outputs; op = Dict(), t = 0.0, inputs, outputs; zero_dummy_der, - op, + op, t, kwargs...) mats, extras = linearize(ssys, lin_fun; op, t, allow_input_derivatives) mats, ssys, extras From 32ad4d6aaadf4aaffe89b4363450e28ecca3f17e Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 18 Jun 2025 19:40:18 +0530 Subject: [PATCH 2109/2176] build: bump minor version --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index e23664450c..951daa0af3 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "ModelingToolkit" uuid = "961ee093-0014-501f-94e3-6117800e7a78" authors = ["Yingbo Ma ", "Chris Rackauckas and contributors"] -version = "10.3.0" +version = "10.4.0" [deps] ADTypes = "47edcb42-4c32-4615-8424-f2b9edc5f35b" From 904a35e51f25a06b3a97e97a660c60fe38b82826 Mon Sep 17 00:00:00 2001 From: Fredrik Bagge Carlson Date: Thu, 19 Jun 2025 10:36:55 +0200 Subject: [PATCH 2110/2176] Pass along `allow_input_derivatives` --- src/systems/analysis_points.jl | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/systems/analysis_points.jl b/src/systems/analysis_points.jl index 392fda03ad..dbee166344 100644 --- a/src/systems/analysis_points.jl +++ b/src/systems/analysis_points.jl @@ -915,10 +915,11 @@ end for f in [:get_sensitivity, :get_comp_sensitivity, :get_looptransfer] utility_fun = Symbol(f, :_function) @eval function $f( - sys, ap, args...; loop_openings = [], system_modifier = identity, kwargs...) + sys, ap, args...; loop_openings = [], system_modifier = identity, + allow_input_derivatives = true, kwargs...) lin_fun, ssys = $(utility_fun)( sys, ap, args...; loop_openings, system_modifier, kwargs...) - mats, extras = ModelingToolkit.linearize(ssys, lin_fun) + mats, extras = ModelingToolkit.linearize(ssys, lin_fun; allow_input_derivatives) mats, ssys, extras end end From 5ec1b700fb1979c48ea336e15643aecf802ae66c Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 20 Jun 2025 16:07:55 +0530 Subject: [PATCH 2111/2176] fix: fix `get_updated_symbolic_problem` Respect `u0` and `p` passed as kwargs --- src/systems/nonlinear/initializesystem.jl | 10 +++--- test/extensions/Project.toml | 1 + test/extensions/ad.jl | 44 +++++++++++++++++++++++ 3 files changed, 50 insertions(+), 5 deletions(-) diff --git a/src/systems/nonlinear/initializesystem.jl b/src/systems/nonlinear/initializesystem.jl index 93fa988b7d..9c8ec73de2 100644 --- a/src/systems/nonlinear/initializesystem.jl +++ b/src/systems/nonlinear/initializesystem.jl @@ -772,7 +772,9 @@ function SciMLBase.late_binding_update_u0_p( return newu0, newp end -function DiffEqBase.get_updated_symbolic_problem(sys::AbstractSystem, prob; kw...) +function DiffEqBase.get_updated_symbolic_problem( + sys::AbstractSystem, prob; u0 = state_values(prob), + p = parameter_values(prob), kw...) supports_initialization(sys) || return prob initdata = prob.f.initialization_data initdata isa SciMLBase.OverrideInitData || return prob @@ -780,10 +782,8 @@ function DiffEqBase.get_updated_symbolic_problem(sys::AbstractSystem, prob; kw.. meta isa InitializationMetadata || return prob meta.get_updated_u0 === nothing && return prob - u0 = state_values(prob) - u0 === nothing && return prob + u0 === nothing && return remake(prob; p) - p = parameter_values(prob) t0 = is_time_dependent(prob) ? current_time(prob) : nothing if p isa MTKParameters @@ -800,7 +800,7 @@ function DiffEqBase.get_updated_symbolic_problem(sys::AbstractSystem, prob; kw.. T = StaticArrays.similar_type(u0) end - return remake(prob; u0 = T(meta.get_updated_u0(prob, initdata.initializeprob))) + return remake(prob; u0 = T(meta.get_updated_u0(prob, initdata.initializeprob)), p) end """ diff --git a/test/extensions/Project.toml b/test/extensions/Project.toml index 6a8d01a7b8..a5ab9a8d0c 100644 --- a/test/extensions/Project.toml +++ b/test/extensions/Project.toml @@ -24,6 +24,7 @@ Pyomo = "0e8e1daf-01b5-4eba-a626-3897743a3816" SciMLSensitivity = "1ed8b502-d754-442c-8d5d-10ac956f44a1" SciMLStructures = "53ae85a6-f571-4167-b2af-e1d143709226" SimpleDiffEq = "05bca326-078c-5bf0-a5bf-ce7c7982d7fd" +StableRNGs = "860ef19b-820b-49d6-a774-d7a799459cd3" SymbolicIndexingInterface = "2efcf032-c050-4f8e-a9bb-153293bab1f5" Symbolics = "0c5d862f-8b57-4792-8d23-62f2024744c7" Zygote = "e88e6eb3-aa80-5325-afca-941959d7151f" diff --git a/test/extensions/ad.jl b/test/extensions/ad.jl index 7e9cbdd740..53210b66a8 100644 --- a/test/extensions/ad.jl +++ b/test/extensions/ad.jl @@ -8,6 +8,7 @@ using OrdinaryDiffEqNonlinearSolve using NonlinearSolve using SciMLSensitivity using ForwardDiff +using StableRNGs using ChainRulesCore using ChainRulesCore: NoTangent using ChainRulesTestUtils: test_rrule, rand_tangent @@ -136,3 +137,46 @@ end prob[sys.x] end end + +@testset "`p` provided to `solve` is respected" begin + @mtkmodel Linear begin + @variables begin + x(t) = 1.0, [description = "Prey"] + end + @parameters begin + α = 1.5 + end + @equations begin + D(x) ~ -α * x + end + end + + @mtkcompile linear = Linear() + problem = ODEProblem(linear, [], (0.0, 1.0)) + solution = solve(problem, Tsit5(), saveat = 0.1) + rng = StableRNG(42) + data = (; + t = solution.t, + # [[y, x], :] + measurements = Array(solution) + ) + data.measurements .+= 0.05 * randn(rng, size(data.measurements)) + + p0, repack, _ = SciMLStructures.canonicalize(SciMLStructures.Tunable(), problem.p) + + objective = let repack = repack, problem = problem + (p, data) -> begin + pnew = repack(p) + sol = solve(problem, Tsit5(), p = pnew, saveat = data.t) + sum(abs2, sol .- data.measurements) / size(data.t, 1) + end + end + + # Check 0.0031677344878386607 + @test_nowarn objective(p0, data) + + fd = ForwardDiff.gradient(Base.Fix2(objective, data), p0) + zg = Zygote.gradient(Base.Fix2(objective, data), p0) + + @test fd≈zg[1] atol=1e-6 +end From 786a4f4c08c4c57601f597af3d269d1dfa1ba432 Mon Sep 17 00:00:00 2001 From: CompatHelper Julia Date: Sun, 22 Jun 2025 00:30:51 +0000 Subject: [PATCH 2112/2176] CompatHelper: bump compat for CairoMakie to 0.15 for package docs, (keep existing compat) --- docs/Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/Project.toml b/docs/Project.toml index 58fd2ec21c..7264f41851 100644 --- a/docs/Project.toml +++ b/docs/Project.toml @@ -35,7 +35,7 @@ Unitful = "1986cc42-f94f-5a68-af5c-568840ba703d" Attractors = "1.24" BenchmarkTools = "1.3" BifurcationKit = "0.4" -CairoMakie = "0.13" +CairoMakie = "0.13, 0.15" CommonSolve = "0.2" DataInterpolations = "6.5, 8" DiffEqDevTools = "2" From 4a1ea7154af613fb2bffc217f4b7d6537f8b0d2a Mon Sep 17 00:00:00 2001 From: Herman Sletmoen Date: Sun, 22 Jun 2025 18:04:39 +0200 Subject: [PATCH 2113/2176] Fix plot in perturbation example --- docs/src/examples/perturbation.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/docs/src/examples/perturbation.md b/docs/src/examples/perturbation.md index 5dfe84c600..fa01edb70f 100644 --- a/docs/src/examples/perturbation.md +++ b/docs/src/examples/perturbation.md @@ -93,12 +93,13 @@ eqs_pert = taylor_coeff(eq_pert, ϵ, 0:2) We solve and plot it as in the previous example, and compare the solution with $ϵ=0.1$ to the exact solution $x(t, ϵ) = e^{-ϵ t} \sin(\sqrt{(1-ϵ^2)}\,t) / \sqrt{1-ϵ^2}$ of the unperturbed equation: ```@example perturbation -u0 = Dict([unknowns(sys) .=> 0.0; D(y[0]) => 1.0]) # nonzero initial velocity +u0 = [y[0] => 0.0, y[1] => 0.0, y[2] => 0.0, D(y[0]) => 1.0, D(y[1]) => 0.0, D(y[2]) => 0.0] # nonzero initial velocity prob = ODEProblem(sys, u0, (0.0, 50.0)) sol = solve(prob) plot(sol, idxs = substitute(x_series, ϵ => 0.1); label = "Perturbative (ϵ=0.1)") x_exact(t, ϵ) = exp(-ϵ * t) * sin(√(1 - ϵ^2) * t) / √(1 - ϵ^2) +@assert isapprox(sol(π/2; idxs = substitute(x_series, ϵ => 0.1)), x_exact(π/2, 0.1); atol = 1e-2) # compare around 1st peak # hide plot!(sol.t, x_exact.(sol.t, 0.1); label = "Exact (ϵ=0.1)") ``` From 7d32637b16a973835f185da1d058e4d395a7714b Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 24 Jun 2025 15:00:34 +0530 Subject: [PATCH 2114/2176] fix: do not add redundant `var ~ 0` equations in underdetermined systems --- src/systems/alias_elimination.jl | 32 +++++++++++++++++--------------- src/systems/systemstructure.jl | 4 ++-- 2 files changed, 19 insertions(+), 17 deletions(-) diff --git a/src/systems/alias_elimination.jl b/src/systems/alias_elimination.jl index fb4fedc920..e0bfde2268 100644 --- a/src/systems/alias_elimination.jl +++ b/src/systems/alias_elimination.jl @@ -8,7 +8,7 @@ function alias_eliminate_graph!(state::TransformationState; kwargs...) end @unpack graph, var_to_diff, solvable_graph = state.structure - mm = alias_eliminate_graph!(state, mm) + mm = alias_eliminate_graph!(state, mm; kwargs...) s = state.structure for g in (s.graph, s.solvable_graph) g === nothing && continue @@ -347,7 +347,7 @@ function do_bareiss!(M, Mold, is_linear_variables, is_highest_diff) (rank1, rank2, rank3, pivots) end -function alias_eliminate_graph!(state::TransformationState, ils::SparseMatrixCLIL) +function alias_eliminate_graph!(state::TransformationState, ils::SparseMatrixCLIL; fully_determined = true, kwargs...) @unpack structure = state @unpack graph, solvable_graph, var_to_diff, eq_to_diff = state.structure # Step 1: Perform Bareiss factorization on the adjacency matrix of the linear @@ -355,19 +355,21 @@ function alias_eliminate_graph!(state::TransformationState, ils::SparseMatrixCLI # ils, solvable_variables, (rank1, rank2, rank3, pivots) = aag_bareiss!(structure, ils) - ## Step 2: Simplify the system using the Bareiss factorization - rk1vars = BitSet(@view pivots[1:rank1]) - for v in solvable_variables - v in rk1vars && continue - @set! ils.nparentrows += 1 - push!(ils.nzrows, ils.nparentrows) - push!(ils.row_cols, [v]) - push!(ils.row_vals, [convert(eltype(ils), 1)]) - add_vertex!(graph, SRC) - add_vertex!(solvable_graph, SRC) - add_edge!(graph, ils.nparentrows, v) - add_edge!(solvable_graph, ils.nparentrows, v) - add_vertex!(eq_to_diff) + if fully_determined == true + ## Step 2: Simplify the system using the Bareiss factorization + rk1vars = BitSet(@view pivots[1:rank1]) + for v in solvable_variables + v in rk1vars && continue + @set! ils.nparentrows += 1 + push!(ils.nzrows, ils.nparentrows) + push!(ils.row_cols, [v]) + push!(ils.row_vals, [convert(eltype(ils), 1)]) + add_vertex!(graph, SRC) + add_vertex!(solvable_graph, SRC) + add_edge!(graph, ils.nparentrows, v) + add_edge!(solvable_graph, ils.nparentrows, v) + add_vertex!(eq_to_diff) + end end return ils diff --git a/src/systems/systemstructure.jl b/src/systems/systemstructure.jl index e368641350..71f996f251 100644 --- a/src/systems/systemstructure.jl +++ b/src/systems/systemstructure.jl @@ -753,7 +753,7 @@ function _mtkcompile!(state::TearingState; simplify = false, ModelingToolkit.markio!(state, orig_inputs, inputs, outputs, disturbance_inputs) state = ModelingToolkit.inputs_to_parameters!(state, [inputs; disturbance_inputs]) end - sys, mm = ModelingToolkit.alias_elimination!(state; kwargs...) + sys, mm = ModelingToolkit.alias_elimination!(state; fully_determined, kwargs...) if check_consistency fully_determined = ModelingToolkit.check_consistency( state, orig_inputs; nothrow = fully_determined === nothing) @@ -765,7 +765,7 @@ function _mtkcompile!(state::TearingState; simplify = false, var_eq_matching = pantelides!(state; finalize = false, kwargs...) sys = pantelides_reassemble(state, var_eq_matching) state = TearingState(sys) - sys, mm = ModelingToolkit.alias_elimination!(state; kwargs...) + sys, mm = ModelingToolkit.alias_elimination!(state; fully_determined, kwargs...) sys = ModelingToolkit.dummy_derivative( sys, state; simplify, mm, check_consistency, fully_determined, kwargs...) else From 6dcbab09489648029a8656113238e73ad050365e Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 24 Jun 2025 15:01:02 +0530 Subject: [PATCH 2115/2176] test: update tests to account for new alias elimination behavior --- test/initializationsystem.jl | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/test/initializationsystem.jl b/test/initializationsystem.jl index 06d0752076..e34641bbb2 100644 --- a/test/initializationsystem.jl +++ b/test/initializationsystem.jl @@ -455,14 +455,13 @@ sol = solve(prob, Tsit5()) # Initialize with an observed variable prob = ODEProblem(simpsys, [z => 0.0], tspan, guesses = [x => 2.0, y => 4.0]) sol = solve(prob, Tsit5()) -@test sol.u[1] == [0.0, 0.0] +@test sol[z, 1] == 0.0 prob = ODEProblem(simpsys, [z => 1.0, y => 1.0], tspan, guesses = [x => 2.0]) sol = solve(prob, Tsit5()) @test sol[[x, y], 1] == [0.0, 1.0] -# This should warn, but logging tests can't be marked as broken -@test_logs prob = ODEProblem(simpsys, [], tspan, guesses = [x => 2.0]) +@test_warn "underdetermined" prob = ODEProblem(simpsys, [], tspan, guesses = [x => 2.0, y => 1.0]) # Late Binding initialization_eqs # https://github.com/SciML/ModelingToolkit.jl/issues/2787 From d0bffc10d29766867ecf2a65f8061984e36f330b Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 24 Jun 2025 15:01:09 +0530 Subject: [PATCH 2116/2176] test: do not use `ODESystem` in tests --- test/symbolic_events.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/symbolic_events.jl b/test/symbolic_events.jl index d82bf8a5c1..e48f793a70 100644 --- a/test/symbolic_events.jl +++ b/test/symbolic_events.jl @@ -1368,7 +1368,7 @@ end D(x) ~ p2, x2 ~ p_1(x) ] - @mtkcompile sys = ODESystem(eq, t, [x, x2], [p_1, p2], discrete_events = [event]) + @mtkcompile sys = System(eq, t, [x, x2], [p_1, p2], discrete_events = [event]) prob = ODEProblem(sys, [], (0.0, 1.0)) sol = solve(prob) From 8e50f184d1653fd5ba621ba5bc1e7bfc86a478bf Mon Sep 17 00:00:00 2001 From: Oscar Smith Date: Tue, 24 Jun 2025 11:14:53 -0400 Subject: [PATCH 2117/2176] allow DataInterpolations@7 --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 951daa0af3..4befe6079c 100644 --- a/Project.toml +++ b/Project.toml @@ -98,7 +98,7 @@ Combinatorics = "1" CommonSolve = "0.2.4" Compat = "3.42, 4" ConstructionBase = "1" -DataInterpolations = "6.4" +DataInterpolations = "6.4, 7, 8" DataStructures = "0.17, 0.18" DeepDiffs = "1" DelayDiffEq = "5.50" From e74ef672297dacb229eaf04da091cb1c95a97ce5 Mon Sep 17 00:00:00 2001 From: oscarddssmith Date: Tue, 24 Jun 2025 14:21:30 -0400 Subject: [PATCH 2118/2176] remove DataInterpolationsv6 and update tests --- Project.toml | 2 +- test/split_parameters.jl | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Project.toml b/Project.toml index 4befe6079c..5f099a3e9d 100644 --- a/Project.toml +++ b/Project.toml @@ -98,7 +98,7 @@ Combinatorics = "1" CommonSolve = "0.2.4" Compat = "3.42, 4" ConstructionBase = "1" -DataInterpolations = "6.4, 7, 8" +DataInterpolations = "7, 8" DataStructures = "0.17, 0.18" DeepDiffs = "1" DelayDiffEq = "5.50" diff --git a/test/split_parameters.jl b/test/split_parameters.jl index f76ae4f01c..9b5ed3424a 100644 --- a/test/split_parameters.jl +++ b/test/split_parameters.jl @@ -257,7 +257,7 @@ end @testset "Concrete function type" begin ts = 0.0:0.1:1.0 - interp = LinearInterpolation(ts .^ 2, ts; extrapolate = true) + interp = LinearInterpolation(ts .^ 2, ts; extrapolation = ExtrapolationType.Extension) @variables x(t) @parameters (fn::typeof(interp))(..) @mtkcompile sys = System(D(x) ~ fn(x), t) @@ -267,7 +267,7 @@ end @inferred getter(prob) @inferred prob.f(prob.u0, prob.p, prob.tspan[1]) @test_nowarn sol = solve(prob, Tsit5()) - @test_nowarn prob.ps[fn] = LinearInterpolation(ts .^ 3, ts; extrapolate = true) + @test_nowarn prob.ps[fn] = LinearInterpolation(ts .^ 3, ts; extrapolation = ExtrapolationType.Extension) @test_nowarn sol = solve(prob) end end From d90ad907758357ed71aff4b5b1819f8aaa373206 Mon Sep 17 00:00:00 2001 From: Orjan Ameye Date: Tue, 24 Jun 2025 22:08:54 +0200 Subject: [PATCH 2119/2176] fix: enable support for complex ODEProblem again --- src/systems/index_cache.jl | 4 ++-- src/systems/problem_utils.jl | 6 +++--- test/complex.jl | 28 ++++++++++++++++++++++++++++ 3 files changed, 33 insertions(+), 5 deletions(-) diff --git a/src/systems/index_cache.jl b/src/systems/index_cache.jl index c66c562e9c..2ce1c7cffa 100644 --- a/src/systems/index_cache.jl +++ b/src/systems/index_cache.jl @@ -388,8 +388,8 @@ function IndexCache(sys::AbstractSystem) observed_syms_to_timeseries, dependent_pars_to_timeseries, disc_buffer_templates, - BufferTemplate(Real, tunable_buffer_size), - BufferTemplate(Real, initials_buffer_size), + BufferTemplate(Number, tunable_buffer_size), + BufferTemplate(Number, initials_buffer_size), const_buffer_sizes, nonnumeric_buffer_sizes, symbol_to_variable diff --git a/src/systems/problem_utils.jl b/src/systems/problem_utils.jl index f785de798f..5f5e63aa70 100644 --- a/src/systems/problem_utils.jl +++ b/src/systems/problem_utils.jl @@ -985,7 +985,7 @@ end $(TYPEDEF) A callable struct to use as the `get_updated_u0` field of `InitializationMetadata`. -Returns the value to use for the `u0` of the problem. +Returns the value to use for the `u0` of the problem. # Fields @@ -1185,7 +1185,7 @@ function float_type_from_varmap(varmap, floatT = Bool) if v isa AbstractArray floatT = promote_type(floatT, eltype(v)) - elseif v isa Real + elseif v isa Number floatT = promote_type(floatT, typeof(v)) end end @@ -1451,7 +1451,7 @@ function check_inputmap_keys(sys, op) end const BAD_KEY_MESSAGE = """ - Undefined keys found in the parameter or initial condition maps. Check if symbolic variable names have been reassigned. + Undefined keys found in the parameter or initial condition maps. Check if symbolic variable names have been reassigned. The following keys are invalid: """ diff --git a/test/complex.jl b/test/complex.jl index 69cc22c985..e30ebb177e 100644 --- a/test/complex.jl +++ b/test/complex.jl @@ -14,3 +14,31 @@ using Test end @named mixed = ComplexModel() @test length(equations(mixed)) == 2 + +@testset "Complex ODEProblem" begin + using ModelingToolkit: t_nounits as t, D_nounits as D + + vars = @variables x(t) y(t) z(t) + pars = @parameters a b + + eqs = [ + D(x) ~ y - x, + D(y) ~ -x * z + b * abs(z), + D(z) ~ x * y - a + ] + @named modlorenz = System(eqs, t) + sys = mtkcompile(modlorenz) + + ic = ModelingToolkit.get_index_cache(sys) + @test ic.tunable_buffer_size.type == Number + + u0 = ComplexF64[-4.0, 5.0, 0.0] .+ randn(ComplexF64, 3) + p = ComplexF64[5.0, 0.1] + dict = merge(Dict(unknowns(sys) .=> u0), Dict(parameters(sys) .=> p)) + prob = ODEProblem(sys, dict, (0.0, 1.0)) + + using OrdinaryDiffEq + sol = solve(prob, Tsit5(), saveat = 0.1) + + @test sol.u[1] isa Vector{ComplexF64} +end From d64051768384ddedbed4d511658aab9595ba1f9c Mon Sep 17 00:00:00 2001 From: Orjan Ameye Date: Tue, 24 Jun 2025 22:30:45 +0200 Subject: [PATCH 2120/2176] fix: spelling typos --- src/modelingtoolkitize/common.jl | 6 +-- .../symbolics_tearing.jl | 50 +++++++++---------- src/systems/problem_utils.jl | 6 +-- src/systems/system.jl | 8 +-- 4 files changed, 35 insertions(+), 35 deletions(-) diff --git a/src/modelingtoolkitize/common.jl b/src/modelingtoolkitize/common.jl index 8291fd5710..ffddca2f4c 100644 --- a/src/modelingtoolkitize/common.jl +++ b/src/modelingtoolkitize/common.jl @@ -46,7 +46,7 @@ end """ $(TYPEDSIGNATURES) -Return a symbolic state for the given proble `prob.`. `t` is the independent variable. +Return a symbolic state for the given problem `prob.`. `t` is the independent variable. `u_names` optionally contains the names to use for the created symbolic variables. """ function construct_vars(prob, t, u_names = nothing) @@ -287,7 +287,7 @@ end """ $(TYPEDSIGNATURES) -Return a symbolic parameter object for the given proble `prob.`. `t` is the independent +Return a symbolic parameter object for the given problem `prob.`. `t` is the independent variable. `p_names` optionally contains the names to use for the created symbolic variables. """ @@ -319,7 +319,7 @@ end $(TYPEDSIGNATURES) Given the differential operator `D`, mass matrix `mm` and ordered list of unknowns `vars`, -return the list of +return the list of """ function lhs_from_mass_matrix(D, mm, vars) var_set = Set(vars) diff --git a/src/structural_transformation/symbolics_tearing.jl b/src/structural_transformation/symbolics_tearing.jl index f8f05ffb7f..00982cc9d8 100644 --- a/src/structural_transformation/symbolics_tearing.jl +++ b/src/structural_transformation/symbolics_tearing.jl @@ -193,14 +193,14 @@ end =# """ -Replace derivatives of non-selected unknown variables by dummy derivatives. +Replace derivatives of non-selected unknown variables by dummy derivatives. State selection may determine that some differential variables are algebraic variables in disguise. The derivative of such variables are called dummy derivatives. -`SelectedState` information is no longer needed after this function is called. -State selection is done. All non-differentiated variables are algebraic +`SelectedState` information is no longer needed after this function is called. +State selection is done. All non-differentiated variables are algebraic variables, and all variables that appear differentiated are differential variables. """ function substitute_derivatives_algevars!( @@ -241,7 +241,7 @@ function substitute_derivatives_algevars!( end end -#= +#= There are three cases where we want to generate new variables to convert the system into first order (semi-implicit) ODEs. @@ -288,32 +288,32 @@ where `:=` denotes assignment. As a final note, in all the above cases where we need to introduce new variables and equations, don't add them when they already exist. -###### DISCRETE SYSTEMS ####### +###### DISCRETE SYSTEMS ####### Documenting the differences to structural simplification for discrete systems: In discrete systems everything gets shifted forward a timestep by `shift_discrete_system` -in order to properly generate the difference equations. +in order to properly generate the difference equations. In the system x(k) ~ x(k-1) + x(k-2), becomes Shift(t, 1)(x(t)) ~ x(t) + Shift(t, -1)(x(t)) -The lowest-order term is Shift(t, k)(x(t)), instead of x(t). As such we actually want +The lowest-order term is Shift(t, k)(x(t)), instead of x(t). As such we actually want dummy variables for the k-1 lowest order terms instead of the k-1 highest order terms. Shift(t, -1)(x(t)) -> x\_{t-1}(t) -Since Shift(t, -1)(x) is not a derivative, it is directly substituted in `fullvars`. -No equation or variable is added for it. +Since Shift(t, -1)(x) is not a derivative, it is directly substituted in `fullvars`. +No equation or variable is added for it. -For ODESystems D(D(D(x))) in equations is recursively substituted as D(x) ~ x_t, D(x_t) ~ x_tt, etc. -The analogue for discrete systems, Shift(t, 1)(Shift(t,1)(Shift(t,1)(Shift(t, -3)(x(t))))) -does not actually appear. So `total_sub` in generate_system_equations` is directly -initialized with all of the lowered variables `Shift(t, -3)(x) -> x_t-3(t)`, etc. +For ODESystems D(D(D(x))) in equations is recursively substituted as D(x) ~ x_t, D(x_t) ~ x_tt, etc. +The analogue for discrete systems, Shift(t, 1)(Shift(t,1)(Shift(t,1)(Shift(t, -3)(x(t))))) +does not actually appear. So `total_sub` in generate_system_equations` is directly +initialized with all of the lowered variables `Shift(t, -3)(x) -> x_t-3(t)`, etc. =# """ Generate new derivative variables for the system. -Effects on the system structure: +Effects on the system structure: - fullvars: add the new derivative variables x_t - neweqs: add the identity equations for the new variables, D(x) ~ x_t - graph: update graph with the new equations and variables, and their connections @@ -484,7 +484,7 @@ function find_duplicate_dd(dv, solvable_graph, diff_to_var, linear_eqs, mm) end """ -Add a dummy derivative variable x_t corresponding to symbolic variable D(x) +Add a dummy derivative variable x_t corresponding to symbolic variable D(x) which has index dv in `fullvars`. Return the new index of x_t. """ function add_dd_variable!(s::SystemStructure, fullvars, x_t, dv) @@ -516,14 +516,14 @@ function add_dd_equation!(s::SystemStructure, neweqs, eq, dv, v_t) end """ -Solve the equations in `neweqs` to obtain the final equations of the +Solve the equations in `neweqs` to obtain the final equations of the system. -For each equation of `neweqs`, do one of the following: +For each equation of `neweqs`, do one of the following: 1. If the equation is solvable for a differentiated variable D(x), then solve for D(x), and add D(x) ~ sol as a differential equation of the system. - 2. If the equation is solvable for an un-differentiated variable x, + 2. If the equation is solvable for an un-differentiated variable x, solve for x and then add x ~ sol as a solved equation. These will become observables. 3. If the equation is not solvable, add it as an algebraic equation. @@ -531,7 +531,7 @@ For each equation of `neweqs`, do one of the following: Solved equations are added to `total_sub`. Occurrences of differential or solved variables on the RHS of the final equations will get substituted. The topological sort of the equations ensures that variables are solved for -before they appear in equations. +before they appear in equations. Reorder the equations and unknowns to be in the BLT sorted form. @@ -610,7 +610,7 @@ function generate_system_equations!(state::TearingState, neweqs, var_eq_matching @unpack neweqs′, eq_ordering, var_ordering, solved_eqs, solved_vars = eq_generator is_diff_eq = .!iszero.(var_ordering) - # Generate new equations and orderings + # Generate new equations and orderings diff_vars = var_ordering[is_diff_eq] diff_vars_set = BitSet(diff_vars) if length(diff_vars_set) != length(diff_vars) @@ -695,7 +695,7 @@ struct EquationGenerator{S, D, I} neweqs′::Vector{Equation} """ `eq_ordering[i]` is the index `neweqs′[i]` was originally at in the untorn equations of - the system. This is used to permute the state of the system into BLT sorted form. + the system. This is used to permute the state of the system into BLT sorted form. """ eq_ordering::Vector{Int} """ @@ -866,7 +866,7 @@ function make_solved_equation(var, eq, total_sub; simplify = false) end """ -Given the ordering returned by `generate_system_equations!`, update the +Given the ordering returned by `generate_system_equations!`, update the tearing state to account for the new order. Permute the variables and equations. Eliminate the solved variables and equations from the graph and permute the graph's vertices to account for the new variable/equation ordering. @@ -916,7 +916,7 @@ function reorder_vars!(state::TearingState, var_eq_matching, var_sccs, eq_orderi # Remove empty SCCs filter!(!isempty, var_sccs) - # Update system structure + # Update system structure @set! state.structure.graph = complete(new_graph) @set! state.structure.var_to_diff = new_var_to_diff @set! state.structure.eq_to_diff = new_eq_to_diff @@ -933,7 +933,7 @@ function update_simplified_system!( @unpack fullvars, structure = state @unpack solvable_graph, var_to_diff, eq_to_diff, graph = structure diff_to_var = invview(var_to_diff) - # Since we solved the highest order derivative varible in discrete systems, + # Since we solved the highest order derivative variable in discrete systems, # we make a list of the solved variables and avoid including them in the # unknowns. solved_vars = Set() @@ -1058,7 +1058,7 @@ function tearing_reassemble(state::TearingState, var_eq_matching::Matching, state, neweqs, var_eq_matching, full_var_eq_matching, var_sccs; iv, D) end - # Structural simplification + # Structural simplification substitute_derivatives_algevars!(state, neweqs, var_eq_matching, dummy_sub; iv, D) var_sccs = generate_derivative_variables!( diff --git a/src/systems/problem_utils.jl b/src/systems/problem_utils.jl index f785de798f..24e9c76054 100644 --- a/src/systems/problem_utils.jl +++ b/src/systems/problem_utils.jl @@ -985,7 +985,7 @@ end $(TYPEDEF) A callable struct to use as the `get_updated_u0` field of `InitializationMetadata`. -Returns the value to use for the `u0` of the problem. +Returns the value to use for the `u0` of the problem. # Fields @@ -1451,7 +1451,7 @@ function check_inputmap_keys(sys, op) end const BAD_KEY_MESSAGE = """ - Undefined keys found in the parameter or initial condition maps. Check if symbolic variable names have been reassigned. + Undefined keys found in the parameter or initial condition maps. Check if symbolic variable names have been reassigned. The following keys are invalid: """ @@ -1634,7 +1634,7 @@ end """ $(TYPEDSIGNATURES) -Turn key-value pairs in `kws` into assignments and appent them to `block.args`. `head` is +Turn key-value pairs in `kws` into assignments and append them to `block.args`. `head` is the head of the `Expr` used to create the assignment. `filter` is a function that takes the key and returns whether or not to include it in the assignments. """ diff --git a/src/systems/system.jl b/src/systems/system.jl index 863b840b37..69e61739a2 100644 --- a/src/systems/system.jl +++ b/src/systems/system.jl @@ -624,12 +624,12 @@ function process_costs(costs::Vector, sts, ps, iv) end """ -Validate that all the variables in an auxiliary system of the (ODE) System (constraint or costs) are +Validate that all the variables in an auxiliary system of the (ODE) System (constraint or costs) are well-formed states or parameters. - Callable/delay variables (e.g. of the form x(0.6) should be unknowns of the system (and have one arg, etc.) - Callable/delay parameters should be parameters of the system -Return the set of additional parameters found in the system, e.g. in x(p) ~ 3 then p should be added as a +Return the set of additional parameters found in the system, e.g. in x(p) ~ 3 then p should be added as a parameter of the system. """ function validate_vars_and_find_ps!(auxvars, auxps, sysvars, iv) @@ -1072,14 +1072,14 @@ end function Base.showerror(io::IO, err::EventsInTimeIndependentSystemError) println(io, """ - Events are not supported in time-indepent systems. Provide an independent variable to \ + Events are not supported in time-independent systems. Provide an independent variable to \ make the system time-dependent or remove the events. The following continuous events were provided: $(err.cevents) The following discrete events were provided: - $(err.devents) + $(err.devents) """) end From 0948934cd23a21487bf93298a8104976e5d45c9f Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 17 Jun 2025 16:48:12 +0530 Subject: [PATCH 2121/2176] refactor: do not build and use `paramsubs` in `generate_initializesystem` Simplification doesn't care for metadata, so this is unnecessary and expensive. --- src/systems/nonlinear/initializesystem.jl | 50 ++++++++--------------- 1 file changed, 17 insertions(+), 33 deletions(-) diff --git a/src/systems/nonlinear/initializesystem.jl b/src/systems/nonlinear/initializesystem.jl index 9c8ec73de2..67cc025644 100644 --- a/src/systems/nonlinear/initializesystem.jl +++ b/src/systems/nonlinear/initializesystem.jl @@ -173,20 +173,20 @@ function generate_initializesystem_timevarying(sys::AbstractSystem; end # 5) process parameters as initialization unknowns - paramsubs = setup_parameter_initialization!( + solved_params = setup_parameter_initialization!( sys, pmap, defs, guesses, eqs_ics; check_defguess) # 6) parameter dependencies become equations, their LHS become unknowns # non-numeric dependent parameters stay as parameter dependencies new_parameter_deps = solve_parameter_dependencies!( - sys, paramsubs, eqs_ics, defs, guesses) + sys, solved_params, eqs_ics, defs, guesses) # 7) handle values provided for dependent parameters similar to values for observed variables - handle_dependent_parameter_constraints!(sys, pmap, eqs_ics, paramsubs) + handle_dependent_parameter_constraints!(sys, pmap, eqs_ics) # parameters do not include ones that became initialization unknowns pars = Vector{SymbolicParam}(filter( - p -> !haskey(paramsubs, p), parameters(sys; initial_parameters = true))) + !in(solved_params), parameters(sys; initial_parameters = true))) push!(pars, get_iv(sys)) # 8) use observed equations for guesses of observed variables if not provided @@ -198,16 +198,8 @@ function generate_initializesystem_timevarying(sys::AbstractSystem; end append!(eqs_ics, trueobs) - vars = [vars; collect(values(paramsubs))] + vars = [vars; collect(solved_params)] - # even if `p => tovar(p)` is in `paramsubs`, `isparameter(p[1]) === true` after substitution - # so add scalarized versions as well - scalarize_varmap!(paramsubs) - - eqs_ics = Symbolics.substitute.(eqs_ics, (paramsubs,)) - for k in keys(defs) - defs[k] = substitute(defs[k], paramsubs) - end initials = Dict(k => v for (k, v) in pmap if isinitial(k)) merge!(defs, initials) isys = System(Vector{Equation}(eqs_ics), @@ -299,30 +291,22 @@ function generate_initializesystem_timeindependent(sys::AbstractSystem; append!(eqs_ics, initialization_eqs) # process parameters as initialization unknowns - paramsubs = setup_parameter_initialization!( + solved_params = setup_parameter_initialization!( sys, pmap, defs, guesses, eqs_ics; check_defguess) # parameter dependencies become equations, their LHS become unknowns # non-numeric dependent parameters stay as parameter dependencies new_parameter_deps = solve_parameter_dependencies!( - sys, paramsubs, eqs_ics, defs, guesses) + sys, solved_params, eqs_ics, defs, guesses) # handle values provided for dependent parameters similar to values for observed variables - handle_dependent_parameter_constraints!(sys, pmap, eqs_ics, paramsubs) + handle_dependent_parameter_constraints!(sys, pmap, eqs_ics) # parameters do not include ones that became initialization unknowns pars = Vector{SymbolicParam}(filter( - p -> !haskey(paramsubs, p), parameters(sys; initial_parameters = true))) - vars = collect(values(paramsubs)) + !in(solved_params), parameters(sys; initial_parameters = true))) + vars = collect(solved_params) - # even if `p => tovar(p)` is in `paramsubs`, `isparameter(p[1]) === true` after substitution - # so add scalarized versions as well - scalarize_varmap!(paramsubs) - - eqs_ics = Vector{Equation}(Symbolics.substitute.(eqs_ics, (paramsubs,))) - for k in keys(defs) - defs[k] = substitute(defs[k], paramsubs) - end initials = Dict(k => v for (k, v) in pmap if isinitial(k)) merge!(defs, initials) isys = System(Vector{Equation}(eqs_ics), @@ -359,7 +343,7 @@ mapping solvable parameters to their `tovar` variants. function setup_parameter_initialization!( sys::AbstractSystem, pmap::AbstractDict, defs::AbstractDict, guesses::AbstractDict, eqs_ics::Vector{Equation}; check_defguess = false) - paramsubs = Dict() + solved_params = Set() for p in parameters(sys) if is_parameter_solvable(p, pmap, defs, guesses) # If either of them are `missing` the parameter is an unknown @@ -369,7 +353,7 @@ function setup_parameter_initialization!( _val2 = get_possibly_array_fallback_singletons(defs, p) _val3 = get_possibly_array_fallback_singletons(guesses, p) varp = tovar(p) - paramsubs[p] = varp + push!(solved_params, p) # Has a default of `missing`, and (either an equation using the value passed to `ODEProblem` or a guess) if _val2 === missing if _val1 !== nothing && _val1 !== missing @@ -409,7 +393,7 @@ function setup_parameter_initialization!( end end - return paramsubs + return solved_params end """ @@ -418,7 +402,7 @@ end Add appropriate parameter dependencies as initialization equations. Return the new list of parameter dependencies for the initialization system. """ -function solve_parameter_dependencies!(sys::AbstractSystem, paramsubs::AbstractDict, +function solve_parameter_dependencies!(sys::AbstractSystem, solved_params::AbstractSet, eqs_ics::Vector{Equation}, defs::AbstractDict, guesses::AbstractDict) new_parameter_deps = Equation[] for eq in parameter_dependencies(sys) @@ -427,7 +411,7 @@ function solve_parameter_dependencies!(sys::AbstractSystem, paramsubs::AbstractD continue end varp = tovar(eq.lhs) - paramsubs[eq.lhs] = varp + push!(solved_params, eq.lhs) push!(eqs_ics, eq) guessval = get(guesses, eq.lhs, eq.rhs) push!(defs, varp => guessval) @@ -442,10 +426,10 @@ end Turn values provided for parameter dependencies into initialization equations. """ function handle_dependent_parameter_constraints!(sys::AbstractSystem, pmap::AbstractDict, - eqs_ics::Vector{Equation}, paramsubs::AbstractDict) + eqs_ics::Vector{Equation}) for (k, v) in merge(defaults(sys), pmap) if is_variable_floatingpoint(k) && has_parameter_dependency_with_lhs(sys, k) - push!(eqs_ics, paramsubs[k] ~ v) + push!(eqs_ics, k ~ v) end end From 58a47ab166c1d5b6a2ad9ca727a923529a8bfb40 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 17 Jun 2025 16:57:17 +0530 Subject: [PATCH 2122/2176] feat: add fast path for constructing `MTKParameters` in `process_SciMLProblem` --- src/systems/parameter_buffer.jl | 11 +++++++---- src/systems/problem_utils.jl | 2 +- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/src/systems/parameter_buffer.jl b/src/systems/parameter_buffer.jl index bd77c8c519..226b629680 100644 --- a/src/systems/parameter_buffer.jl +++ b/src/systems/parameter_buffer.jl @@ -29,7 +29,7 @@ the default behavior). function MTKParameters( sys::AbstractSystem, op; tofloat = false, t0 = nothing, substitution_limit = 1000, floatT = nothing, - p_constructor = identity) + p_constructor = identity, fast_path = false) ic = if has_index_cache(sys) && get_index_cache(sys) !== nothing get_index_cache(sys) else @@ -49,9 +49,12 @@ function MTKParameters( u0map = anydict() pmap = anydict() - missing_unknowns, missing_pars = build_operating_point!(sys, op, - u0map, pmap, defs, dvs, ps) - + if fast_path + missing_pars = missingvars(op, ps) + else + _, missing_pars = build_operating_point!(sys, op, + u0map, pmap, defs, dvs, ps) + end if t0 !== nothing op[get_iv(sys)] = t0 end diff --git a/src/systems/problem_utils.jl b/src/systems/problem_utils.jl index 3db092f117..7db3c65503 100644 --- a/src/systems/problem_utils.jl +++ b/src/systems/problem_utils.jl @@ -1397,7 +1397,7 @@ function process_SciMLProblem( if !(pType <: AbstractArray) pType = Array end - p = MTKParameters(sys, op; floatT = floatT, p_constructor) + p = MTKParameters(sys, op; floatT = floatT, p_constructor, fast_path = true) else p = p_constructor(varmap_to_vars(op, ps; tofloat, container_type = pType)) end From c8618c0709941dd976300c1a497fb7768a08e379 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 17 Jun 2025 16:57:38 +0530 Subject: [PATCH 2123/2176] refactor: avoid unnecessary computation in `evaluate_varmap!` --- src/systems/problem_utils.jl | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/systems/problem_utils.jl b/src/systems/problem_utils.jl index 7db3c65503..3dfcffef13 100644 --- a/src/systems/problem_utils.jl +++ b/src/systems/problem_utils.jl @@ -482,8 +482,11 @@ in `varmap`, it is ignored. """ function evaluate_varmap!(varmap::AbstractDict, vars; limit = 100) for k in vars + v = get(varmap, k, nothing) + v === nothing && continue + symbolic_type(v) == NotSymbolic() && !is_array_of_symbolics(v) && continue haskey(varmap, k) || continue - varmap[k] = fixpoint_sub(varmap[k], varmap; maxiters = limit) + varmap[k] = fixpoint_sub(v, varmap; maxiters = limit) end end From 9982ed9b170df2d493d491db448a7d0215474e1f Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 17 Jun 2025 16:58:00 +0530 Subject: [PATCH 2124/2176] refactor: avoid unnecesary computation in `build_operating_point!` --- src/systems/problem_utils.jl | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/systems/problem_utils.jl b/src/systems/problem_utils.jl index 3dfcffef13..39f69ef0f9 100644 --- a/src/systems/problem_utils.jl +++ b/src/systems/problem_utils.jl @@ -630,13 +630,15 @@ function build_operating_point!(sys::AbstractSystem, end end - for k in keys(u0map) - v = fixpoint_sub(u0map[k], neithermap; operator = Symbolics.Operator) + for (k, v) in u0map + symbolic_type(v) == NotSymbolic() && !is_array_of_symbolics(v) && continue + v = fixpoint_sub(v, neithermap; operator = Symbolics.Operator) isequal(k, v) && continue u0map[k] = v end - for k in keys(pmap) - v = fixpoint_sub(pmap[k], neithermap; operator = Symbolics.Operator) + for (k, v) in pmap + symbolic_type(v) == NotSymbolic() && !is_array_of_symbolics(v) && continue + v = fixpoint_sub(v, neithermap; operator = Symbolics.Operator) isequal(k, v) && continue pmap[k] = v end From dd8b43214a19399d1bdbb4d32dda3ed4aba0ff19 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 17 Jun 2025 16:58:29 +0530 Subject: [PATCH 2125/2176] refactor: batch computation of temporary values in `maybe_build_initialization_problem` --- src/systems/problem_utils.jl | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/systems/problem_utils.jl b/src/systems/problem_utils.jl index 39f69ef0f9..a8716aa586 100644 --- a/src/systems/problem_utils.jl +++ b/src/systems/problem_utils.jl @@ -1153,20 +1153,24 @@ function maybe_build_initialization_problem( update_initializeprob! = ModelingToolkit.update_initializeprob! end - for p in punknowns - is_parameter_solvable(p, op, defs, guesses) || continue - get(op, p, missing) === missing || continue + filter!(punknowns) do p + is_parameter_solvable(p, op, defs, guesses) && get(op, p, missing) === missing + end + pvals = getu(initializeprob, punknowns)(initializeprob) + for (p, pval) in zip(punknowns, pvals) p = unwrap(p) - op[p] = getu(initializeprob, p)(initializeprob) + op[p] = pval if iscall(p) && operation(p) === getindex arrp = arguments(p)[1] + get(op, arrp, nothing) !== missing && continue op[arrp] = collect(arrp) end end if time_dependent_init - for v in missing_unknowns - op[v] = getu(initializeprob, v)(initializeprob) + uvals = getu(initializeprob, collect(missing_unknowns))(initializeprob) + for (v, val) in zip(missing_unknowns, uvals) + op[v] = val end empty!(missing_unknowns) end From 29eee6d650e5f83d8c929d127866d2ef667cf9f9 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 19 Jun 2025 11:52:43 +0530 Subject: [PATCH 2126/2176] feat: add an always-present mutable cache key to the system --- src/systems/abstractsystem.jl | 6 +++++- src/systems/system.jl | 5 +++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index 4fb2bdfd4c..6e2e4dd044 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -805,7 +805,11 @@ has_equations(::AbstractSystem) = true Invalidate cached jacobians, etc. """ -invalidate_cache!(sys::AbstractSystem) = sys +function invalidate_cache!(sys::AbstractSystem) + has_metadata(sys) || return sys + empty!(getmetadata(sys, MutableCacheKey, nothing)) + return sys +end function Setfield.get(obj::AbstractSystem, ::Setfield.PropertyLens{field}) where {field} getfield(obj, field) diff --git a/src/systems/system.jl b/src/systems/system.jl index 69e61739a2..c9c44da702 100644 --- a/src/systems/system.jl +++ b/src/systems/system.jl @@ -8,6 +8,10 @@ end const MetadataT = Base.ImmutableDict{DataType, Any} +abstract type MutableCacheKey end + +const MutableCacheT = Dict{DataType, Any} + """ $(TYPEDEF) @@ -407,6 +411,7 @@ function System(eqs::Vector{Equation}, iv, dvs, ps, brownians = []; end metadata = meta end + metadata = Base.ImmutableDict(metadata, MutableCacheKey => MutableCacheT()) System(Threads.atomic_add!(SYSTEM_COUNT, UInt(1)), eqs, noise_eqs, jumps, constraints, costs, consolidate, dvs, ps, brownians, iv, observed, Equation[], var_to_name, name, description, defaults, guesses, systems, initialization_eqs, From b86cc5203a9448d97f009a6ead589cbb3acfb704 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 19 Jun 2025 11:53:26 +0530 Subject: [PATCH 2127/2176] fix: fix unnecessary warnings when no stream connections in `expand_connections` --- src/systems/connectors.jl | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/systems/connectors.jl b/src/systems/connectors.jl index 226534b82a..f63c10e8a1 100644 --- a/src/systems/connectors.jl +++ b/src/systems/connectors.jl @@ -872,7 +872,8 @@ function expand_connections(sys::AbstractSystem; tol = 1e-10) eqs = [equations(sys); ceqs; stream_eqs] # substitute `instream(..)` expressions with their new values for i in eachindex(eqs) - eqs[i] = fixpoint_sub(eqs[i], instream_subs; maxiters = length(instream_subs)) + eqs[i] = fixpoint_sub( + eqs[i], instream_subs; maxiters = max(length(instream_subs), 10)) end # get the defaults for domain networks d_defs = domain_defaults(sys, domain_csets) From d3a7e7184e236170a0d1040f265962ce3812d84c Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 19 Jun 2025 11:53:49 +0530 Subject: [PATCH 2128/2176] feat: cache intermediate results for `observed_equations_used_by` --- src/utils.jl | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/src/utils.jl b/src/utils.jl index 48a4e57d71..e3e5e4de9f 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -811,6 +811,14 @@ function observed_dependency_graph(eqs::Vector{Equation}) return DiCMOBiGraph{false}(graph, matching) end +abstract type ObservedGraphCacheKey end + +struct ObservedGraphCache + graph::DiCMOBiGraph{false, Int, BipartiteGraph{Int, Nothing}, + Matching{Unassigned, Vector{Union{Unassigned, Int}}}} + obsvar_to_idx::Dict{Any, Int} +end + """ $(TYPEDSIGNATURES) @@ -831,8 +839,19 @@ Keyword arguments: """ function observed_equations_used_by(sys::AbstractSystem, exprs; involved_vars = vars(exprs; op = Union{Shift, Differential, Initial}), obs = observed(sys), available_vars = []) - obsvars = getproperty.(obs, :lhs) - graph = observed_dependency_graph(obs) + if iscomplete(sys) && obs == observed(sys) + cache = getmetadata(sys, MutableCacheKey, nothing) + obs_graph_cache = get!(cache, ObservedGraphCacheKey) do + obsvar_to_idx = Dict{Any, Int}([eq.lhs => i for (i, eq) in enumerate(obs)]) + graph = observed_dependency_graph(obs) + return ObservedGraphCache(graph, obsvar_to_idx) + end + @unpack obsvar_to_idx, graph = obs_graph_cache + else + obsvar_to_idx = Dict([eq.lhs => i for (i, eq) in enumerate(obs)]) + graph = observed_dependency_graph(obs) + end + if !(available_vars isa Set) available_vars = Set(available_vars) end @@ -841,7 +860,9 @@ function observed_equations_used_by(sys::AbstractSystem, exprs; for sym in involved_vars sym in available_vars && continue arrsym = iscall(sym) && operation(sym) === getindex ? arguments(sym)[1] : nothing - idx = findfirst(v -> isequal(v, sym) || isequal(v, arrsym), obsvars) + idx = @something(get(obsvar_to_idx, sym, nothing), + get(obsvar_to_idx, arrsym, nothing), + Some(nothing)) idx === nothing && continue idx in obsidxs && continue parents = dfs_parents(graph, idx) From 3a0abbbe0cb18c9e5415d81e90f8001dfae102fb Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 19 Jun 2025 15:40:11 +0530 Subject: [PATCH 2129/2176] fix: remove unnecessary scalarization in `InitializationProblem` --- src/problems/initializationproblem.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/problems/initializationproblem.jl b/src/problems/initializationproblem.jl index 8b30e449b0..323c65dc2e 100644 --- a/src/problems/initializationproblem.jl +++ b/src/problems/initializationproblem.jl @@ -39,7 +39,7 @@ All other keyword arguments are forwarded to the wrapped nonlinear problem const for k in keys(op) has_u0_ics |= is_variable(sys, k) || isdifferential(k) || symbolic_type(k) == ArraySymbolic() && - is_sized_array_symbolic(k) && is_variable(sys, first(collect(k))) + is_sized_array_symbolic(k) && is_variable(sys, unwrap(first(wrap(k)))) end if !has_u0_ics && get_initializesystem(sys) !== nothing isys = get_initializesystem(sys; initialization_eqs, check_units) From 27702b6cabdac30e1ad093c12997550b015bc2f5 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 19 Jun 2025 15:40:49 +0530 Subject: [PATCH 2130/2176] refactor: add fast path in `build_operating_point!` --- src/systems/problem_utils.jl | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/src/systems/problem_utils.jl b/src/systems/problem_utils.jl index a8716aa586..3167d2e015 100644 --- a/src/systems/problem_utils.jl +++ b/src/systems/problem_utils.jl @@ -630,17 +630,19 @@ function build_operating_point!(sys::AbstractSystem, end end - for (k, v) in u0map - symbolic_type(v) == NotSymbolic() && !is_array_of_symbolics(v) && continue - v = fixpoint_sub(v, neithermap; operator = Symbolics.Operator) - isequal(k, v) && continue - u0map[k] = v - end - for (k, v) in pmap - symbolic_type(v) == NotSymbolic() && !is_array_of_symbolics(v) && continue - v = fixpoint_sub(v, neithermap; operator = Symbolics.Operator) - isequal(k, v) && continue - pmap[k] = v + if !isempty(neithermap) + for (k, v) in u0map + symbolic_type(v) == NotSymbolic() && !is_array_of_symbolics(v) && continue + v = fixpoint_sub(v, neithermap; operator = Symbolics.Operator) + isequal(k, v) && continue + u0map[k] = v + end + for (k, v) in pmap + symbolic_type(v) == NotSymbolic() && !is_array_of_symbolics(v) && continue + v = fixpoint_sub(v, neithermap; operator = Symbolics.Operator) + isequal(k, v) && continue + pmap[k] = v + end end return missing_unknowns, missing_pars From 7db8deceb990b60380f2b89ea275181df054dc7b Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 19 Jun 2025 23:51:32 +0530 Subject: [PATCH 2131/2176] fix: do not rely on metadata in `process_parameter_equations` --- src/systems/abstractsystem.jl | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index 6e2e4dd044..72a623293b 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -2709,7 +2709,9 @@ function process_parameter_equations(sys::AbstractSystem) is_sized_array_symbolic(sym) && all(Base.Fix1(is_parameter, sys), collect(sym)) end - if !isparameter(eq.lhs) + # Everything in `varsbuf` is a parameter, so this is a cheap `is_parameter` + # check. + if !(eq.lhs in varsbuf) throw(ArgumentError(""" LHS of parameter dependency equation must be a single parameter. Found \ $(eq.lhs). From 5bfb2a9a1a4ffe296c4b1f4fe66f9649c56035d1 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 20 Jun 2025 14:44:03 +0530 Subject: [PATCH 2132/2176] refactor: remove source of allocations in `InitializationProblem` --- src/problems/initializationproblem.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/problems/initializationproblem.jl b/src/problems/initializationproblem.jl index 323c65dc2e..b379215e39 100644 --- a/src/problems/initializationproblem.jl +++ b/src/problems/initializationproblem.jl @@ -79,7 +79,7 @@ All other keyword arguments are forwarded to the wrapped nonlinear problem const @warn errmsg end - uninit = setdiff(unknowns(sys), [unknowns(isys); observables(isys)]) + uninit = setdiff(unknowns(sys), unknowns(isys), observables(isys)) # TODO: throw on uninitialized arrays filter!(x -> !(x isa Symbolics.Arr), uninit) From fffc9839b43273ad6976b31f8db93effcc7beacf Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 20 Jun 2025 14:45:14 +0530 Subject: [PATCH 2133/2176] fix: properly handle values given to parameter dependencies in `late_binding_update_u0_p` --- src/systems/nonlinear/initializesystem.jl | 56 +++++++++++++++-------- test/initializationsystem.jl | 4 +- 2 files changed, 39 insertions(+), 21 deletions(-) diff --git a/src/systems/nonlinear/initializesystem.jl b/src/systems/nonlinear/initializesystem.jl index 67cc025644..6ad029559d 100644 --- a/src/systems/nonlinear/initializesystem.jl +++ b/src/systems/nonlinear/initializesystem.jl @@ -719,7 +719,25 @@ function SciMLBase.late_binding_update_u0_p( newu0, newp = promote_u0_p(newu0, newp, t0) # non-symbolic u0 updates initials... - if !(eltype(u0) <: Pair) + if eltype(u0) <: Pair + syms = [] + vals = [] + allsyms = all_symbols(sys) + for (k, v) in u0 + v === nothing && continue + (symbolic_type(v) == NotSymbolic() && !is_array_of_symbolics(v)) || continue + if k isa Symbol + k2 = symbol_to_symbolic(sys, k; allsyms) + # if it is returned as-is, there is no match so skip it + k2 === k && continue + k = k2 + end + is_parameter(sys, Initial(k)) || continue + push!(syms, Initial(k)) + push!(vals, v) + end + newp = setp_oop(sys, syms)(newp, vals) + else # if `p` is not provided or is symbolic p === missing || eltype(p) <: Pair || return newu0, newp (newu0 === nothing || isempty(newu0)) && return newu0, newp @@ -732,27 +750,27 @@ function SciMLBase.late_binding_update_u0_p( throw(ArgumentError("Expected `newu0` to be of same length as unknowns ($(length(prob.u0))). Got $(typeof(newu0)) of length $(length(newu0))")) end newp = meta.set_initial_unknowns!(newp, newu0) - return newu0, newp - end - - syms = [] - vals = [] - allsyms = all_symbols(sys) - for (k, v) in u0 - v === nothing && continue - (symbolic_type(v) == NotSymbolic() && !is_array_of_symbolics(v)) || continue - if k isa Symbol - k2 = symbol_to_symbolic(sys, k; allsyms) - # if it is returned as-is, there is no match so skip it - k2 === k && continue - k = k2 + end + + if eltype(p) <: Pair + syms = [] + vals = [] + for (k, v) in p + v === nothing && continue + (symbolic_type(v) == NotSymbolic() && !is_array_of_symbolics(v)) || continue + if k isa Symbol + k2 = symbol_to_symbolic(sys, k; allsyms) + # if it is returned as-is, there is no match so skip it + k2 === k && continue + k = k2 + end + is_parameter(sys, Initial(k)) || continue + push!(syms, Initial(k)) + push!(vals, v) end - is_parameter(sys, Initial(k)) || continue - push!(syms, Initial(k)) - push!(vals, v) + newp = setp_oop(sys, syms)(newp, vals) end - newp = setp_oop(sys, syms)(newp, vals) return newu0, newp end diff --git a/test/initializationsystem.jl b/test/initializationsystem.jl index e34641bbb2..173fce397f 100644 --- a/test/initializationsystem.jl +++ b/test/initializationsystem.jl @@ -1252,9 +1252,9 @@ end @test init(prob3)[x] ≈ 1.0 prob4 = remake(prob; p = [p => 1.0]) test_dummy_initialization_equation(prob4, x) - prob5 = remake(prob; p = [p => missing, q => 2.0]) + prob5 = remake(prob; p = [p => missing, q => 4.0]) @test prob5.f.initialization_data !== nothing - @test init(prob5).ps[p] ≈ 1.0 + @test init(prob5).ps[p] ≈ 2.0 end @testset "Variables provided as symbols" begin From 7ec630d87a7ab1982d1beacd12f3b19c317ba1ee Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 20 Jun 2025 14:45:39 +0530 Subject: [PATCH 2134/2176] feat: invalidate cache in `@set!` --- src/systems/abstractsystem.jl | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index 72a623293b..665c0044cd 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -811,6 +811,18 @@ function invalidate_cache!(sys::AbstractSystem) return sys end +# `::MetadataT` but that is defined later +function refreshed_metadata(meta::Base.ImmutableDict) + newmeta = MetadataT() + for (k, v) in meta + if k === MutableCacheKey + v = MutableCacheT() + end + newmeta = Base.ImmutableDict(newmeta, k => v) + end + return newmeta +end + function Setfield.get(obj::AbstractSystem, ::Setfield.PropertyLens{field}) where {field} getfield(obj, field) end @@ -819,6 +831,8 @@ end args = map(fieldnames(obj)) do fn if fn in fieldnames(patch) :(patch.$fn) + elseif fn == :metadata + :($refreshed_metadata(getfield(obj, $(Meta.quot(fn))))) else :(getfield(obj, $(Meta.quot(fn)))) end From d4079a80e7c4f453d4527873d7b25352d2e06a2e Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 20 Jun 2025 14:45:54 +0530 Subject: [PATCH 2135/2176] refactor: update tests to account for new initsys generation --- test/initializationsystem.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/initializationsystem.jl b/test/initializationsystem.jl index 173fce397f..3be3e400c3 100644 --- a/test/initializationsystem.jl +++ b/test/initializationsystem.jl @@ -922,8 +922,8 @@ end [D(x) ~ 2x + r + rhss, r ~ p + 2q, q ~ p + 3], t; guesses = [p => 1.0]) prob = Problem(sys, [x => 1.0, p => missing], (0.0, 1.0)) - @test length(equations(ModelingToolkit.get_parent(prob.f.initialization_data.initializeprob.f.sys))) == - 4 + parent_isys = ModelingToolkit.get_parent(prob.f.initialization_data.initializeprob.f.sys) + @test length(equations(parent_isys)) == 4 integ = init(prob, alg) @test integ.ps[p] ≈ 2 end From 7709760e1e57dc5522a068414ebb93b91aa03981 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 24 Jun 2025 12:00:23 +0530 Subject: [PATCH 2136/2176] fix: handle metadata merging in `extend` --- src/systems/abstractsystem.jl | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index 665c0044cd..bac04bf153 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -2525,7 +2525,15 @@ function extend(sys::AbstractSystem, basesys::AbstractSystem; cevs = union(get_continuous_events(basesys), get_continuous_events(sys)) devs = union(get_discrete_events(basesys), get_discrete_events(sys)) defs = merge(get_defaults(basesys), get_defaults(sys)) # prefer `sys` - meta = merge(get_metadata(basesys), get_metadata(sys)) + meta = MetadataT() + for kvp in get_metadata(basesys) + kvp[1] == MutableCacheKey && continue + meta = Base.ImmutableDict(meta, kvp) + end + for kvp in get_metadata(sys) + kvp[1] == MutableCacheKey && continue + meta = Base.ImmutableDict(meta, kvp) + end syss = union(get_systems(basesys), get_systems(sys)) args = length(ivs) == 0 ? (eqs, sts, ps) : (eqs, ivs[1], sts, ps) kwargs = (observed = obs, continuous_events = cevs, From d9758615ef69dab39c768ad9338d2df1a48d719b Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 24 Jun 2025 12:00:40 +0530 Subject: [PATCH 2137/2176] test: update metadata tests --- test/odesystem.jl | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/test/odesystem.jl b/test/odesystem.jl index 28bc1d7db0..e1e18c3b17 100644 --- a/test/odesystem.jl +++ b/test/odesystem.jl @@ -1087,15 +1087,16 @@ end @named B1 = System(Equation[], t, [], []) @named A2 = System(Equation[], t, [], []; metadata = A) @named B2 = System(Equation[], t, [], []; metadata = B) - @test isempty(ModelingToolkit.get_metadata(extend(A1, B1))) + n_core_metadata = length(ModelingToolkit.get_metadata(A1)) + @test length(ModelingToolkit.get_metadata(extend(A1, B1))) == n_core_metadata meta = ModelingToolkit.get_metadata(extend(A1, B2)) - @test length(meta) == 1 + @test length(meta) == n_core_metadata + 1 @test meta[String] == 2 meta = ModelingToolkit.get_metadata(extend(A2, B1)) - @test length(meta) == 1 + @test length(meta) == n_core_metadata + 1 @test meta[Int] == 1 meta = ModelingToolkit.get_metadata(extend(A2, B2)) - @test length(meta) == 2 + @test length(meta) == n_core_metadata + 2 @test meta[Int] == 1 @test meta[String] == 2 end From a501375f02e696d068d2644befad3960f97efbea Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 24 Jun 2025 23:26:37 +0530 Subject: [PATCH 2138/2176] feat: add the ability to completely remove vertices from `BipartiteGraph` --- src/bipartite_graph.jl | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/src/bipartite_graph.jl b/src/bipartite_graph.jl index b6665646c9..8cdb76cca0 100644 --- a/src/bipartite_graph.jl +++ b/src/bipartite_graph.jl @@ -535,13 +535,39 @@ function set_neighbors!(g::BipartiteGraph, i::Integer, new_neighbors) end end -function delete_srcs!(g::BipartiteGraph, srcs) +function delete_srcs!(g::BipartiteGraph{I}, srcs; rm_verts = false) where {I} for s in srcs set_neighbors!(g, s, ()) end + if rm_verts + old_to_new_idxs = collect(one(I):I(nsrcs(g))) + for s in srcs + old_to_new_idxs[s] = zero(I) + end + offset = zero(I) + for i in eachindex(old_to_new_idxs) + if iszero(old_to_new_idxs[i]) + offset += one(I) + continue + end + old_to_new_idxs[i] -= offset + end + + if g.badjlist isa AbstractVector + for i in 1:ndsts(g) + for j in eachindex(g.badjlist[i]) + g.badjlist[i][j] = old_to_new_idxs[g.badjlist[i][j]] + end + filter!(!iszero, g.badjlist[i]) + end + end + deleteat!(g.fadjlist, srcs) + end g end -delete_dsts!(g::BipartiteGraph, srcs) = delete_srcs!(invview(g), srcs) +function delete_dsts!(g::BipartiteGraph, srcs; rm_verts = false) + delete_srcs!(invview(g), srcs; rm_verts) +end ### ### Edges iteration From 4ecd1555a8a24fb22fbc2397c7e4fe18445d62bc Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 25 Jun 2025 17:09:36 +0530 Subject: [PATCH 2139/2176] build: bump minor version --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 5f099a3e9d..9a369fc2bd 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "ModelingToolkit" uuid = "961ee093-0014-501f-94e3-6117800e7a78" authors = ["Yingbo Ma ", "Chris Rackauckas and contributors"] -version = "10.4.0" +version = "10.5.0" [deps] ADTypes = "47edcb42-4c32-4615-8424-f2b9edc5f35b" From 2c04b7ba23a4b18d07d4f77049bbae4cf989feb9 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 19 Jun 2025 11:51:25 +0530 Subject: [PATCH 2140/2176] feat: preemptively tear some trivial equations in `mtkcompile` --- .../symbolics_tearing.jl | 3 +- src/systems/systemstructure.jl | 117 +++++++++++++++++- 2 files changed, 118 insertions(+), 2 deletions(-) diff --git a/src/structural_transformation/symbolics_tearing.jl b/src/structural_transformation/symbolics_tearing.jl index 00982cc9d8..c91fedb281 100644 --- a/src/structural_transformation/symbolics_tearing.jl +++ b/src/structural_transformation/symbolics_tearing.jl @@ -960,7 +960,8 @@ function update_simplified_system!( obs_sub[eq.lhs] = eq.rhs end # TODO: compute the dependency correctly so that we don't have to do this - obs = [fast_substitute(observed(sys), obs_sub); solved_eqs] + obs = [fast_substitute(observed(sys), obs_sub); solved_eqs; + fast_substitute(state.additional_observed, obs_sub)] unknown_idxs = filter( i -> diff_to_var[i] === nothing && ispresent(i) && !(fullvars[i] in solved_vars), eachindex(state.fullvars)) diff --git a/src/systems/systemstructure.jl b/src/systems/systemstructure.jl index 71f996f251..935982e0ff 100644 --- a/src/systems/systemstructure.jl +++ b/src/systems/systemstructure.jl @@ -208,12 +208,19 @@ mutable struct TearingState{T <: AbstractSystem} <: AbstractTearingState{T} structure::SystemStructure extra_eqs::Vector param_derivative_map::Dict{BasicSymbolic, Any} + original_eqs::Vector{Equation} + """ + Additional user-provided observed equations. The variables calculated here + are not used in the rest of the system. + """ + additional_observed::Vector{Equation} end TransformationState(sys::AbstractSystem) = TearingState(sys) function system_subset(ts::TearingState, ieqs::Vector{Int}) eqs = equations(ts) @set! ts.sys.eqs = eqs[ieqs] + @set! ts.original_eqs = ts.original_eqs[ieqs] @set! ts.structure = system_subset(ts.structure, ieqs) ts end @@ -276,6 +283,7 @@ function TearingState(sys; quick_cancel = false, check = true, sort_eqs = true) iv = length(ivs) == 1 ? ivs[1] : nothing # flatten array equations eqs = flatten_equations(equations(sys)) + original_eqs = copy(eqs) neqs = length(eqs) param_derivative_map = Dict{BasicSymbolic, Any}() # * Scalarize unknowns @@ -320,6 +328,7 @@ function TearingState(sys; quick_cancel = false, check = true, sort_eqs = true) varsbuf = Set() eqs_to_retain = trues(length(eqs)) for (i, eq) in enumerate(eqs) + _eq = eq if iscall(eq.lhs) && (op = operation(eq.lhs)) isa Differential && isequal(op.x, iv) && is_time_dependent_parameter(only(arguments(eq.lhs)), ps, iv) # parameter derivatives are opted out by specifying `D(p) ~ missing`, but @@ -415,6 +424,7 @@ function TearingState(sys; quick_cancel = false, check = true, sort_eqs = true) end end eqs = eqs[eqs_to_retain] + original_eqs = original_eqs[eqs_to_retain] neqs = length(eqs) symbolic_incidence = symbolic_incidence[eqs_to_retain] @@ -423,6 +433,7 @@ function TearingState(sys; quick_cancel = false, check = true, sort_eqs = true) # depending on order due to NP-completeness of tearing. sortidxs = Base.sortperm(eqs, by = string) eqs = eqs[sortidxs] + original_eqs = original_eqs[sortidxs] symbolic_incidence = symbolic_incidence[sortidxs] end @@ -516,11 +527,114 @@ function TearingState(sys; quick_cancel = false, check = true, sort_eqs = true) ts = TearingState(sys, fullvars, SystemStructure(complete(var_to_diff), complete(eq_to_diff), complete(graph), nothing, var_types, false), - Any[], param_derivative_map) + Any[], param_derivative_map, original_eqs, Equation[]) return ts end +""" + $(TYPEDSIGNATURES) + +Preemptively identify observed equations in the system and tear them. This happens before +any simplification. The equations torn by this process are ones that are already given in +an explicit form in the system and where the LHS is not present in any other equation of +the system except for other such preempitvely torn equations. +""" +function trivial_tearing!(ts::TearingState) + @assert length(ts.original_eqs) == length(equations(ts)) + # equations that can be trivially torn an observed equations + trivial_idxs = BitSet() + # equations to never check + blacklist = BitSet() + torn_eqs = Equation[] + # variables that have been matched to trivially torn equations + matched_vars = BitSet() + # variable to index in fullvars + var_to_idx = Dict{Any, Int}(ts.fullvars .=> eachindex(ts.fullvars)) + + complete!(ts.structure) + var_to_diff = ts.structure.var_to_diff + graph = ts.structure.graph + while true + # track whether we added an equation to the trivial list this iteration + added_equation = false + for (i, eq) in enumerate(ts.original_eqs) + # don't check already torn equations + i in trivial_idxs && continue + i in blacklist && continue + # ensure it is an observed equation matched to a variable in fullvars + vari = get(var_to_idx, eq.lhs, 0) + iszero(vari) && continue + # don't tear irreducible variables + if isirreducible(eq.lhs) + push!(blacklist, i) + continue + end + # if a variable was the LHS of two trivial observed equations, we wouldn't have + # included it in the list. Error if somehow it made it through. + @assert !(vari in matched_vars) + # don't tear differential/shift equations (or differentiated/shifted variables) + var_to_diff[vari] === nothing || continue + invview(var_to_diff)[vari] === nothing || continue + # get the equations that the candidate matched variable is present in, except + # those equations which have already been torn as observed + eqidxs = setdiff(𝑑neighbors(graph, vari), trivial_idxs) + # it should only be present in this equation + length(eqidxs) == 1 || continue + eqi = only(eqidxs) + @assert eqi == i + + # for every variable present in this equation, make sure it isn't _only_ + # present in trivial equations + isvalid = true + for v in 𝑠neighbors(graph, eqi) + v == vari && continue + v in matched_vars && continue + # `> 1` and not `0` because one entry will be this equation (`eqi`) + isvalid &= count(!in(trivial_idxs), 𝑑neighbors(graph, v)) > 1 + isvalid || break + end + isvalid || continue + # skip if the LHS is present in the RHS, since then this isn't explicit + if occursin(eq.lhs, eq.rhs) + push!(blacklist, i) + continue + end + + added_equation = true + push!(trivial_idxs, eqi) + push!(torn_eqs, eq) + push!(matched_vars, vari) + end + + # if we didn't add an equation this iteration, we won't add one next iteration + added_equation || break + end + + deleteat!(var_to_diff.primal_to_diff, matched_vars) + deleteat!(var_to_diff.diff_to_primal, matched_vars) + deleteat!(ts.structure.eq_to_diff.primal_to_diff, trivial_idxs) + deleteat!(ts.structure.eq_to_diff.diff_to_primal, trivial_idxs) + delete_srcs!(ts.structure.graph, trivial_idxs; rm_verts = true) + delete_dsts!(ts.structure.graph, matched_vars; rm_verts = true) + if ts.structure.solvable_graph !== nothing + delete_srcs!(ts.structure.solvable_graph, trivial_idxs; rm_verts = true) + delete_dsts!(ts.structure.solvable_graph, matched_vars; rm_verts = true) + end + if ts.structure.var_types !== nothing + deleteat!(ts.structure.var_types, matched_vars) + end + deleteat!(ts.fullvars, matched_vars) + deleteat!(ts.original_eqs, trivial_idxs) + ts.additional_observed = torn_eqs + sys = ts.sys + eqs = copy(get_eqs(sys)) + deleteat!(eqs, trivial_idxs) + @set! sys.eqs = eqs + ts.sys = sys + return ts +end + function lower_order_var(dervar, t) if isdifferential(dervar) diffvar = arguments(dervar)[1] @@ -753,6 +867,7 @@ function _mtkcompile!(state::TearingState; simplify = false, ModelingToolkit.markio!(state, orig_inputs, inputs, outputs, disturbance_inputs) state = ModelingToolkit.inputs_to_parameters!(state, [inputs; disturbance_inputs]) end + trivial_tearing!(state) sys, mm = ModelingToolkit.alias_elimination!(state; fully_determined, kwargs...) if check_consistency fully_determined = ModelingToolkit.check_consistency( From b98e12f6ae0f648102bb5fa8a25aff1d62317d71 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 24 Jun 2025 18:20:51 +0530 Subject: [PATCH 2141/2176] ci: better handle compile time in benchmarks --- benchmark/benchmarks.jl | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/benchmark/benchmarks.jl b/benchmark/benchmarks.jl index 861d7b0722..bc309f63ee 100644 --- a/benchmark/benchmarks.jl +++ b/benchmark/benchmarks.jl @@ -45,12 +45,17 @@ end @named model = DCMotor() +# first call +mtkcompile(model) SUITE["mtkcompile"] = @benchmarkable mtkcompile($model) model = mtkcompile(model) u0 = unknowns(model) .=> 0.0 tspan = (0.0, 6.0) -SUITE["ODEProblem"] = @benchmarkable ODEProblem($model, $u0, $tspan) prob = ODEProblem(model, u0, tspan) +SUITE["ODEProblem"] = @benchmarkable ODEProblem($model, $u0, $tspan) + +# first call +init(prob) SUITE["init"] = @benchmarkable init($prob) From faf8bceb61ca51ca19d8ec425732b8d3d48bf7f5 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 24 Jun 2025 18:21:08 +0530 Subject: [PATCH 2142/2176] ci: add benchmark for large parameter initialization model --- benchmark/benchmarks.jl | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/benchmark/benchmarks.jl b/benchmark/benchmarks.jl index bc309f63ee..ae62f6ea0a 100644 --- a/benchmark/benchmarks.jl +++ b/benchmark/benchmarks.jl @@ -4,6 +4,7 @@ using ModelingToolkitStandardLibrary.Electrical using ModelingToolkitStandardLibrary.Mechanical.Rotational using ModelingToolkitStandardLibrary.Blocks using OrdinaryDiffEqDefault +using ModelingToolkit: t_nounits as t, D_nounits as D const SUITE = BenchmarkGroup() @@ -59,3 +60,19 @@ SUITE["ODEProblem"] = @benchmarkable ODEProblem($model, $u0, $tspan) # first call init(prob) SUITE["init"] = @benchmarkable init($prob) + +large_param_init = SUITE["large_parameter_init"] = BenchmarkGroup() + +N = 25 +@variables x(t)[1:N] +@parameters A[1:N, 1:N] + +defval = collect(x) * collect(x)' +@mtkcompile model = System( + [D(x) ~ x], t, [x], [A]; defaults = [A => defval], guesses = [A => fill(NaN, N, N)]) + +u0 = [x => rand(N)] +prob = ODEProblem(model, u0, tspan) +large_param_init["ODEProblem"] = @benchmarkable ODEProblem($model, $u0, $tspan) + +large_param_init["init"] = @benchmarkable init($prob) From a6df4cf515f8c7855dfef04c7f9ca495a470175b Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 25 Jun 2025 11:58:07 +0530 Subject: [PATCH 2143/2176] fix: ensure `initializeprobpmap` returns floats --- src/systems/problem_utils.jl | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/systems/problem_utils.jl b/src/systems/problem_utils.jl index 3167d2e015..ecba542fd0 100644 --- a/src/systems/problem_utils.jl +++ b/src/systems/problem_utils.jl @@ -1066,6 +1066,9 @@ function (siu::SetInitialUnknowns)(p::AbstractVector, u0) return p end +safe_float(x) = x +safe_float(x::AbstractArray) = isempty(x) ? x : float(x) + """ $(TYPEDSIGNATURES) @@ -1132,7 +1135,8 @@ function maybe_build_initialization_problem( if time_dependent_init all_init_syms = Set(all_symbols(initializeprob)) solved_unknowns = filter(var -> var in all_init_syms, unknowns(sys)) - initializeprobmap = u0_constructor ∘ getu(initializeprob, solved_unknowns) + initializeprobmap = u0_constructor ∘ safe_float ∘ + getu(initializeprob, solved_unknowns) else initializeprobmap = nothing end From dc0975f17acd7dcfe960e6f375210c622b77ff57 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 25 Jun 2025 13:16:09 +0530 Subject: [PATCH 2144/2176] feat: implement `SymbolicUtils.hasmetadata` for `AbstractSystem` --- src/systems/system.jl | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/systems/system.jl b/src/systems/system.jl index c9c44da702..38fd8fc756 100644 --- a/src/systems/system.jl +++ b/src/systems/system.jl @@ -810,6 +810,11 @@ function SymbolicUtils.setmetadata(sys::AbstractSystem, k::DataType, v) @set sys.metadata = meta end +function SymbolicUtils.hasmetadata(sys::AbstractSystem, k::DataType) + meta = get_metadata(sys) + haskey(meta, k) +end + """ $(TYPEDSIGNATURES) From f4e81dea24880e584b50b5cf2e8258a86bf45abf Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 25 Jun 2025 17:05:40 +0530 Subject: [PATCH 2145/2176] fix: fix potential infinite recursion in `simplify_optimization_system` --- src/systems/systems.jl | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/systems/systems.jl b/src/systems/systems.jl index 4a6c1ccbb4..ff455fb811 100644 --- a/src/systems/systems.jl +++ b/src/systems/systems.jl @@ -196,8 +196,9 @@ function simplify_optimization_system(sys::System; split = true, kwargs...) nlsys = System(econs, dvs, parameters(sys); name = :___tmp_nlsystem) snlsys = mtkcompile(nlsys; kwargs..., fully_determined = false) obs = observed(snlsys) - subs = Dict(eq.lhs => eq.rhs for eq in observed(snlsys)) seqs = equations(snlsys) + trueobs, _ = unhack_observed(obs, seqs) + subs = Dict(eq.lhs => eq.rhs for eq in trueobs) cons_simplified = similar(cons, length(icons) + length(seqs)) for (i, eq) in enumerate(Iterators.flatten((seqs, icons))) cons_simplified[i] = fixpoint_sub(eq, subs) From b0da47859ae1a141945b5c3a3fa5857156c0f48b Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 25 Jun 2025 17:06:02 +0530 Subject: [PATCH 2146/2176] test: mark test optimization test as no longer broken --- test/optimizationsystem.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/optimizationsystem.jl b/test/optimizationsystem.jl index ad77dbc416..3d6720fb73 100644 --- a/test/optimizationsystem.jl +++ b/test/optimizationsystem.jl @@ -73,7 +73,7 @@ end prob = OptimizationProblem(sys, [x => 0.0, y => 0.0, a => 1.0, b => 1.0], grad = false, hess = false, cons_j = false, cons_h = false) sol = solve(prob, AmplNLWriter.Optimizer(Ipopt_jll.amplexe)) - @test_skip sol.objective < 1.0 + @test sol.objective < 1.0 end @testset "equality constraint" begin From 2efd5582a2d539bfe2f3364088dc31b3fc9b0c30 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 25 Jun 2025 17:06:21 +0530 Subject: [PATCH 2147/2176] test: make optimization test robust to simplification --- test/optimizationsystem.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/optimizationsystem.jl b/test/optimizationsystem.jl index 3d6720fb73..e78ebfe9ee 100644 --- a/test/optimizationsystem.jl +++ b/test/optimizationsystem.jl @@ -89,11 +89,11 @@ end grad = true, hess = true, cons_j = true, cons_h = true) sol = solve(prob, IPNewton()) @test sol.objective < 1.0 - @test sol.u≈[0.808, -0.064] atol=1e-3 + @test sol[[x, z]]≈[0.808, -0.064] atol=1e-3 @test sol[x]^2 + sol[y]^2 ≈ 1.0 sol = solve(prob, Ipopt.Optimizer(); print_level = 0) @test sol.objective < 1.0 - @test sol.u≈[0.808, -0.064] atol=1e-3 + @test sol[[x, z]]≈[0.808, -0.064] atol=1e-3 @test sol[x]^2 + sol[y]^2 ≈ 1.0 prob = OptimizationProblem(sys, [x => 0.0, y => 0.0, z => 0.0, a => 1.0, b => 1.0], From 9e0db44aa885f8b221e3803030834757454466f2 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 25 Jun 2025 23:17:45 +0530 Subject: [PATCH 2148/2176] fix: retain events and metadata in `substitute` --- src/systems/abstractsystem.jl | 33 +++++++++++++++++++-------------- test/odesystem.jl | 25 +++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 14 deletions(-) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index bac04bf153..9d6bb1a410 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -2683,22 +2683,27 @@ function Symbolics.substitute(sys::AbstractSystem, rules::Union{Vector{<:Pair}, elseif sys isa System rules = todict(map(r -> Symbolics.unwrap(r[1]) => Symbolics.unwrap(r[2]), collect(rules))) - eqs = fast_substitute(get_eqs(sys), rules) - pdeps = fast_substitute(get_parameter_dependencies(sys), rules) - defs = Dict(fast_substitute(k, rules) => fast_substitute(v, rules) + newsys = @set sys.eqs = fast_substitute(get_eqs(sys), rules) + @set! newsys.unknowns = map(get_unknowns(sys)) do var + get(rules, var, var) + end + @set! newsys.ps = map(get_ps(sys)) do var + get(rules, var, var) + end + @set! newsys.parameter_dependencies = fast_substitute( + get_parameter_dependencies(sys), rules) + @set! newsys.defaults = Dict(fast_substitute(k, rules) => fast_substitute(v, rules) for (k, v) in get_defaults(sys)) - guess = Dict(fast_substitute(k, rules) => fast_substitute(v, rules) + @set! newsys.guesses = Dict(fast_substitute(k, rules) => fast_substitute(v, rules) for (k, v) in get_guesses(sys)) - noise_eqs = fast_substitute(get_noise_eqs(sys), rules) - costs = fast_substitute(get_costs(sys), rules) - observed = fast_substitute(get_observed(sys), rules) - initialization_eqs = fast_substitute(get_initialization_eqs(sys), rules) - cstrs = fast_substitute(get_constraints(sys), rules) - subsys = map(s -> substitute(s, rules), get_systems(sys)) - newsys = System(eqs, get_iv(sys); name = nameof(sys), defaults = defs, - guesses = guess, systems = subsys, noise_eqs, - observed, initialization_eqs, constraints = cstrs) - @set! newsys.parameter_dependencies = pdeps + @set! newsys.noise_eqs = fast_substitute(get_noise_eqs(sys), rules) + @set! newsys.costs = Vector{Union{Real, BasicSymbolic}}(fast_substitute( + get_costs(sys), rules)) + @set! newsys.observed = fast_substitute(get_observed(sys), rules) + @set! newsys.initialization_eqs = fast_substitute( + get_initialization_eqs(sys), rules) + @set! newsys.constraints = fast_substitute(get_constraints(sys), rules) + @set! newsys.systems = map(s -> substitute(s, rules), get_systems(sys)) else error("substituting symbols is not supported for $(typeof(sys))") end diff --git a/test/odesystem.jl b/test/odesystem.jl index e1e18c3b17..a65b096e2b 100644 --- a/test/odesystem.jl +++ b/test/odesystem.jl @@ -1574,3 +1574,28 @@ end prob = ODEProblem(sys, [x => 1.0], (0.0, 1.0)) @test prob.problem_type == "A" end + +@testset "`substitute` retains events and metadata" begin + @parameters p(t) = 1.0 + @variables x(t) = 0.0 + event = [0.5] => [p ~ Pre(t)] + event2 = [x ~ 0.75] => [p ~ 2 * Pre(t)] + + struct TestMeta end + + eq = [ + D(x) ~ p + ] + @named sys = System(eq, t, [x], [p], discrete_events = [event], + continuous_events = [event2], metadata = Dict(TestMeta => "test")) + + @variables x2(t) = 0.0 + sys2 = substitute(sys, [x => x2]) + + @test length(ModelingToolkit.get_discrete_events(sys)) == 1 + @test length(ModelingToolkit.get_discrete_events(sys2)) == 1 + @test length(ModelingToolkit.get_continuous_events(sys)) == 1 + @test length(ModelingToolkit.get_continuous_events(sys2)) == 1 + @test getmetadata(sys, TestMeta, nothing) == "test" + @test getmetadata(sys2, TestMeta, nothing) == "test" +end From 5a0c95939077177a081499d677ee1bc4dda936e0 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 26 Jun 2025 11:51:06 +0530 Subject: [PATCH 2149/2176] build: bump minor version --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 9a369fc2bd..ec7a5fd10f 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "ModelingToolkit" uuid = "961ee093-0014-501f-94e3-6117800e7a78" authors = ["Yingbo Ma ", "Chris Rackauckas and contributors"] -version = "10.5.0" +version = "10.6.0" [deps] ADTypes = "47edcb42-4c32-4615-8424-f2b9edc5f35b" From 8bb4eb6f8d0e4a1d7ba30cc067990aa35c9f74be Mon Sep 17 00:00:00 2001 From: Herman Sletmoen Date: Fri, 27 Jun 2025 22:39:39 +0200 Subject: [PATCH 2150/2176] Call default_toterm on each default only once --- src/systems/nonlinear/initializesystem.jl | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/systems/nonlinear/initializesystem.jl b/src/systems/nonlinear/initializesystem.jl index 6ad029559d..34c8ec3be6 100644 --- a/src/systems/nonlinear/initializesystem.jl +++ b/src/systems/nonlinear/initializesystem.jl @@ -190,11 +190,13 @@ function generate_initializesystem_timevarying(sys::AbstractSystem; push!(pars, get_iv(sys)) # 8) use observed equations for guesses of observed variables if not provided + guessed = Set(keys(defs)) # x(t), D(x(t)), ... + guessed = union(guessed, Set(default_toterm.(guessed))) # x(t), D(x(t)), xˍt(t), ... for eq in trueobs - haskey(defs, eq.lhs) && continue - any(x -> isequal(default_toterm(x), eq.lhs), keys(defs)) && continue - - defs[eq.lhs] = eq.rhs + if !(eq.lhs in guessed) + defs[eq.lhs] = eq.rhs + #push!(guessed, eq.lhs) # should not encounter eq.lhs twice, so don't need to track it + end end append!(eqs_ics, trueobs) From c2bd333d558cc1be9f2643ed90f978d6b8b2a60d Mon Sep 17 00:00:00 2001 From: Herman Sletmoen Date: Fri, 27 Jun 2025 22:43:35 +0200 Subject: [PATCH 2151/2176] Don't refer to non-existent AbstractTimeDependentSystem --- src/systems/nonlinear/initializesystem.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/systems/nonlinear/initializesystem.jl b/src/systems/nonlinear/initializesystem.jl index 34c8ec3be6..fe3014d3d8 100644 --- a/src/systems/nonlinear/initializesystem.jl +++ b/src/systems/nonlinear/initializesystem.jl @@ -41,7 +41,7 @@ end """ $(TYPEDSIGNATURES) -Generate `System` of nonlinear equations which initializes a problem from specified initial conditions of an `AbstractTimeDependentSystem`. +Generate `System` of nonlinear equations which initializes a problem from specified initial conditions of a time-dependent `AbstractSystem`. """ function generate_initializesystem_timevarying(sys::AbstractSystem; op = Dict(), @@ -218,7 +218,7 @@ end """ $(TYPEDSIGNATURES) -Generate `System` of nonlinear equations which initializes a problem from specified initial conditions of an `AbstractTimeDependentSystem`. +Generate `System` of nonlinear equations which initializes a problem from specified initial conditions of a time-independent `AbstractSystem`. """ function generate_initializesystem_timeindependent(sys::AbstractSystem; op = Dict(), From 2597d6a600ace5a10c32b06df7994d04d98e5989 Mon Sep 17 00:00:00 2001 From: Herman Sletmoen Date: Fri, 27 Jun 2025 22:45:23 +0200 Subject: [PATCH 2152/2176] Remove unused variables --- src/systems/nonlinear/initializesystem.jl | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/systems/nonlinear/initializesystem.jl b/src/systems/nonlinear/initializesystem.jl index fe3014d3d8..9ad70f6992 100644 --- a/src/systems/nonlinear/initializesystem.jl +++ b/src/systems/nonlinear/initializesystem.jl @@ -71,7 +71,6 @@ function generate_initializesystem_timevarying(sys::AbstractSystem; eqs_ics = Equation[] defs = copy(defaults(sys)) # copy so we don't modify sys.defaults additional_guesses = anydict(guesses) - additional_initialization_eqs = Vector{Equation}(initialization_eqs) guesses = merge(get_guesses(sys), additional_guesses) idxs_diff = isdiffeq.(eqs) @@ -230,12 +229,10 @@ function generate_initializesystem_timeindependent(sys::AbstractSystem; eqs = equations(sys) trueobs, eqs = unhack_observed(observed(sys), eqs) vars = unique([unknowns(sys); getfield.(trueobs, :lhs)]) - vars_set = Set(vars) # for efficient in-lookup eqs_ics = Equation[] defs = copy(defaults(sys)) # copy so we don't modify sys.defaults additional_guesses = anydict(guesses) - additional_initialization_eqs = Vector{Equation}(initialization_eqs) guesses = merge(get_guesses(sys), additional_guesses) # PREPROCESSING From 26255ab2ef4895457bdc0fed9566d88d73605a4b Mon Sep 17 00:00:00 2001 From: Herman Sletmoen Date: Sat, 28 Jun 2025 11:15:19 +0200 Subject: [PATCH 2153/2176] Convert each equation to string only once --- src/systems/systemstructure.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/systems/systemstructure.jl b/src/systems/systemstructure.jl index 935982e0ff..5dfd36a6fc 100644 --- a/src/systems/systemstructure.jl +++ b/src/systems/systemstructure.jl @@ -431,7 +431,7 @@ function TearingState(sys; quick_cancel = false, check = true, sort_eqs = true) if sort_eqs # sort equations lexicographically to reduce simplification issues # depending on order due to NP-completeness of tearing. - sortidxs = Base.sortperm(eqs, by = string) + sortidxs = Base.sortperm(string.(eqs)) # "by = string" creates more strings eqs = eqs[sortidxs] original_eqs = original_eqs[sortidxs] symbolic_incidence = symbolic_incidence[sortidxs] From 1517da77e483caa1830dca63fb1f1dec50a4bbe8 Mon Sep 17 00:00:00 2001 From: Orjan Ameye Date: Sun, 29 Jun 2025 13:10:17 +0200 Subject: [PATCH 2154/2176] fix: add Complex to check floating point symbolic sym --- src/utils.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/utils.jl b/src/utils.jl index e3e5e4de9f..e96f31f533 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -606,7 +606,7 @@ end """ $(TYPEDSIGNATURES) -Indicate whether the given equation type (Equation, Pair, etc) supports `collect_vars!`. +Indicate whether the given equation type (Equation, Pair, etc) supports `collect_vars!`. Can be dispatched by higher-level libraries to indicate support. """ eqtype_supports_collect_vars(eq) = false @@ -791,7 +791,7 @@ Check if `T` is an appropriate symtype for a symbolic variable representing a fl point number or array of such numbers. """ function is_floatingpoint_symtype(T::Type) - return T == Real || T == Number || T <: AbstractFloat || + return T == Real || T == Number || T == Complex || T <: AbstractFloat || T <: AbstractArray && is_floatingpoint_symtype(eltype(T)) end From c5f1fea7be817aa46fac4416a5508667659a26c3 Mon Sep 17 00:00:00 2001 From: Maysam Gholampour Date: Mon, 30 Jun 2025 18:32:15 +0800 Subject: [PATCH 2155/2176] correct `sys.mass_a` to ``sys.mass_a`` in FMU example correct `sys.mass_a` to ``sys.mass_a`` in FMU example --- docs/src/tutorials/fmi.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/src/tutorials/fmi.md b/docs/src/tutorials/fmi.md index 7e949839ef..a468f30f8f 100644 --- a/docs/src/tutorials/fmi.md +++ b/docs/src/tutorials/fmi.md @@ -85,7 +85,7 @@ We can interpolate the solution object to obtain values at arbitrary time points just like a normal solution. ```@repl fmi -sol(0.0:0.1:1.0; idxs = sys.mass_a) +sol(0.0:0.1:1.0; idxs = sys.mass__a) ``` FMUs following version 3 of the specification can be simulated with almost the same process. This time, From 2afea211ede506628dbd5602fd3a58c501b9b324 Mon Sep 17 00:00:00 2001 From: CompatHelper Julia Date: Tue, 1 Jul 2025 00:31:36 +0000 Subject: [PATCH 2156/2176] CompatHelper: bump compat for BifurcationKit in [weakdeps] to 0.5, (keep existing compat) --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index ec7a5fd10f..e71836c302 100644 --- a/Project.toml +++ b/Project.toml @@ -88,7 +88,7 @@ MTKPyomoDynamicOptExt = "Pyomo" ADTypes = "1.14.0" AbstractTrees = "0.3, 0.4" ArrayInterface = "6, 7" -BifurcationKit = "0.4" +BifurcationKit = "0.4, 0.5" BlockArrays = "1.1" BoundaryValueDiffEqAscher = "1.6.0" BoundaryValueDiffEqMIRK = "1.7.0" From eaaf79a3440d580423202fa312336074b923cbd9 Mon Sep 17 00:00:00 2001 From: CompatHelper Julia Date: Tue, 1 Jul 2025 00:32:17 +0000 Subject: [PATCH 2157/2176] CompatHelper: bump compat for BifurcationKit to 0.5 for package docs, (keep existing compat) --- docs/Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/Project.toml b/docs/Project.toml index 7264f41851..b51530c9d0 100644 --- a/docs/Project.toml +++ b/docs/Project.toml @@ -34,7 +34,7 @@ Unitful = "1986cc42-f94f-5a68-af5c-568840ba703d" [compat] Attractors = "1.24" BenchmarkTools = "1.3" -BifurcationKit = "0.4" +BifurcationKit = "0.4, 0.5" CairoMakie = "0.13, 0.15" CommonSolve = "0.2" DataInterpolations = "6.5, 8" From 3987cdb25f5f7558f36d22b9ef310c414d7d2aa8 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 27 Jun 2025 18:02:09 +0530 Subject: [PATCH 2158/2176] feat: implement `isapprox` for systems --- src/systems/system.jl | 49 +++++++++++++++++++++++++++++++++++++++++++ test/serialization.jl | 1 + 2 files changed, 50 insertions(+) diff --git a/src/systems/system.jl b/src/systems/system.jl index 38fd8fc756..28285acb30 100644 --- a/src/systems/system.jl +++ b/src/systems/system.jl @@ -1097,3 +1097,52 @@ function supports_initialization(sys::System) return isempty(jumps(sys)) && _iszero(cost(sys)) && isempty(constraints(sys)) end + +safe_eachrow(::Nothing) = nothing +safe_eachrow(x::AbstractArray) = eachrow(x) + +safe_issetequal(::Nothing, ::Nothing) = true +safe_issetequal(::Nothing, x) = false +safe_issetequal(x, ::Nothing) = false +safe_issetequal(x, y) = issetequal(x, y) + +""" + $(TYPEDSIGNATURES) + +Check if two systems are about equal, to the extent that ModelingToolkit.jl supports. Note +that if this returns `true`, the systems are not guaranteed to be exactly equivalent +(unless `sysa === sysb`) but are highly likely to represent a similar mathematical problem. +If this returns `false`, the systems are very likely to be different. +""" +function Base.isapprox(sysa::System, sysb::System) + sysa === sysb && return true + return nameof(sysa) == nameof(sysb) && + isequal(get_iv(sysa), get_iv(sysb)) && + issetequal(get_eqs(sysa), get_eqs(sysb)) && + safe_issetequal( + safe_eachrow(get_noise_eqs(sysa)), safe_eachrow(get_noise_eqs(sysb))) && + issetequal(get_jumps(sysa), get_jumps(sysb)) && + issetequal(get_constraints(sysa), get_constraints(sysb)) && + issetequal(get_costs(sysa), get_costs(sysb)) && + isequal(get_consolidate(sysa), get_consolidate(sysb)) && + issetequal(get_unknowns(sysa), get_unknowns(sysb)) && + issetequal(get_ps(sysa), get_ps(sysb)) && + issetequal(get_brownians(sysa), get_brownians(sysb)) && + issetequal(get_observed(sysa), get_observed(sysb)) && + issetequal(get_parameter_dependencies(sysa), get_parameter_dependencies(sysb)) && + isequal(get_description(sysa), get_description(sysb)) && + isequal(get_defaults(sysa), get_defaults(sysb)) && + isequal(get_guesses(sysa), get_guesses(sysb)) && + issetequal(get_initialization_eqs(sysa), get_initialization_eqs(sysb)) && + issetequal(get_continuous_events(sysa), get_continuous_events(sysb)) && + issetequal(get_discrete_events(sysa), get_discrete_events(sysb)) && + isequal(get_connector_type(sysa), get_connector_type(sysb)) && + isequal(get_assertions(sysa), get_assertions(sysb)) && + isequal(get_metadata(sysa), get_metadata(sysb)) && + isequal(get_is_dde(sysa), get_is_dde(sysb)) && + issetequal(get_tstops(sysa), get_tstops(sysb)) && + safe_issetequal(get_ignored_connections(sysa), get_ignored_connections(sysb)) && + isequal(get_is_initializesystem(sysa), get_is_initializesystem(sysb)) && + isequal(get_is_discrete(sysa), get_is_discrete(sysb)) && + isequal(get_isscheduled(sysa), get_isscheduled(sysb)) +end diff --git a/test/serialization.jl b/test/serialization.jl index 2c754694b4..43f2cabb6e 100644 --- a/test/serialization.jl +++ b/test/serialization.jl @@ -28,6 +28,7 @@ str = String(take!(io)) sys = include_string(@__MODULE__, str) rc2 = expand_connections(rc_model) +@test isapprox(sys, rc2) @test issetequal(equations(sys), equations(rc2)) @test issetequal(unknowns(sys), unknowns(rc2)) @test issetequal(parameters(sys), parameters(rc2)) From 0701a63812d9ab79dd4b66c009ca0da9b997756a Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 1 Jul 2025 12:08:48 +0530 Subject: [PATCH 2159/2176] fix: fix cache invalidation in `@set!` --- src/systems/abstractsystem.jl | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index bac04bf153..818933d5aa 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -820,6 +820,9 @@ function refreshed_metadata(meta::Base.ImmutableDict) end newmeta = Base.ImmutableDict(newmeta, k => v) end + if !haskey(newmeta, MutableCacheKey) + newmeta = Base.ImmutableDict(newmeta, MutableCacheKey => MutableCacheT()) + end return newmeta end From 23bf46ac62d279fb45d55d000b212268bf93066b Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 1 Jul 2025 12:09:07 +0530 Subject: [PATCH 2160/2176] fix: remove duplicate keys in metadata --- src/systems/system.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/systems/system.jl b/src/systems/system.jl index 28285acb30..2d38ab2dce 100644 --- a/src/systems/system.jl +++ b/src/systems/system.jl @@ -411,7 +411,7 @@ function System(eqs::Vector{Equation}, iv, dvs, ps, brownians = []; end metadata = meta end - metadata = Base.ImmutableDict(metadata, MutableCacheKey => MutableCacheT()) + metadata = refreshed_metadata(metadata) System(Threads.atomic_add!(SYSTEM_COUNT, UInt(1)), eqs, noise_eqs, jumps, constraints, costs, consolidate, dvs, ps, brownians, iv, observed, Equation[], var_to_name, name, description, defaults, guesses, systems, initialization_eqs, From 8fd7ac9a59f3e11b5a2292f66fc881dd497970c9 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 1 Jul 2025 13:55:25 +0530 Subject: [PATCH 2161/2176] 4fix: fix `late_binding_update_u0-p` --- src/systems/nonlinear/initializesystem.jl | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/systems/nonlinear/initializesystem.jl b/src/systems/nonlinear/initializesystem.jl index 6ad029559d..1bb8bfb3d9 100644 --- a/src/systems/nonlinear/initializesystem.jl +++ b/src/systems/nonlinear/initializesystem.jl @@ -738,6 +738,7 @@ function SciMLBase.late_binding_update_u0_p( end newp = setp_oop(sys, syms)(newp, vals) else + allsyms = nothing # if `p` is not provided or is symbolic p === missing || eltype(p) <: Pair || return newu0, newp (newu0 === nothing || isempty(newu0)) && return newu0, newp @@ -755,6 +756,9 @@ function SciMLBase.late_binding_update_u0_p( if eltype(p) <: Pair syms = [] vals = [] + if allsyms === nothing + allsyms = all_symbols(sys) + end for (k, v) in p v === nothing && continue (symbolic_type(v) == NotSymbolic() && !is_array_of_symbolics(v)) || continue From 0c9a31bca37bb2aef4b5a6004998b0df2cb0568d Mon Sep 17 00:00:00 2001 From: oscarddssmith Date: Thu, 12 Jun 2025 16:23:31 -0400 Subject: [PATCH 2162/2176] fix linearization t0 address review --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index ec7a5fd10f..0a6aa1ff23 100644 --- a/Project.toml +++ b/Project.toml @@ -106,7 +106,7 @@ DiffEqBase = "6.170.1" DiffEqCallbacks = "2.16, 3, 4" DiffEqNoiseProcess = "5" DiffRules = "0.1, 1.0" -DifferentiationInterface = "0.6.47" +DifferentiationInterface = "0.6.47, 0.7" Distributed = "1" Distributions = "0.23, 0.24, 0.25" DocStringExtensions = "0.7, 0.8, 0.9" From 948ef56a47d65cb8e43712148e75545025e95177 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 1 Jul 2025 14:57:57 +0530 Subject: [PATCH 2163/2176] fix: pass `strict = Val(false)` to `DI.prepare_jacobian` --- src/linearization.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/linearization.jl b/src/linearization.jl index b0254490dd..e1fa62e377 100644 --- a/src/linearization.jl +++ b/src/linearization.jl @@ -174,13 +174,13 @@ struct PreparedJacobian{iip, P, F, B, A} end function PreparedJacobian{true}(f, buf, autodiff, args...) - prep = DI.prepare_jacobian(f, buf, autodiff, args...) + prep = DI.prepare_jacobian(f, buf, autodiff, args...; strict = Val(false)) return PreparedJacobian{true, typeof(prep), typeof(f), typeof(buf), typeof(autodiff)}( prep, f, buf, autodiff) end function PreparedJacobian{false}(f, autodiff, args...) - prep = DI.prepare_jacobian(f, autodiff, args...) + prep = DI.prepare_jacobian(f, autodiff, args...; strict = Val(false)) return PreparedJacobian{true, typeof(prep), typeof(f), Nothing, typeof(autodiff)}( prep, f, nothing) end From e0e4d00f512d901166d9511ebd564a7919914f7b Mon Sep 17 00:00:00 2001 From: Anshul Singhvi Date: Tue, 1 Jul 2025 08:38:45 -0400 Subject: [PATCH 2164/2176] build: bump minor version --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index ec7a5fd10f..b51d9bccef 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "ModelingToolkit" uuid = "961ee093-0014-501f-94e3-6117800e7a78" authors = ["Yingbo Ma ", "Chris Rackauckas and contributors"] -version = "10.6.0" +version = "10.7.0" [deps] ADTypes = "47edcb42-4c32-4615-8424-f2b9edc5f35b" From 728055caf51239dcc951858cdf7ec71a4c8e2417 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 26 Jun 2025 14:42:10 +0530 Subject: [PATCH 2165/2176] fix: fix handling of loop openings for systems that are nonlinear in the inputs --- src/systems/analysis_points.jl | 31 +++++++++++++++++++------------ 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/src/systems/analysis_points.jl b/src/systems/analysis_points.jl index dbee166344..04f6baae49 100644 --- a/src/systems/analysis_points.jl +++ b/src/systems/analysis_points.jl @@ -491,6 +491,10 @@ struct Break <: AnalysisPointTransformation applicable if `add_input == true`. """ default_outputs_to_input::Bool + """ + Whether the added input is a parameter. Only applicable if `add_input == true`. + """ + added_input_is_param::Bool end """ @@ -498,7 +502,9 @@ end `Break` the given analysis point `ap`. """ -Break(ap::AnalysisPoint, add_input::Bool = false) = Break(ap, add_input, false) +function Break(ap::AnalysisPoint, add_input::Bool = false, default_outputs_to_input = false) + Break(ap, add_input, default_outputs_to_input, false) +end function apply_transformation(tf::Break, sys::AbstractSystem) modify_nested_subsystem(sys, tf.ap) do breaksys @@ -528,9 +534,15 @@ function apply_transformation(tf::Break, sys::AbstractSystem) new_def end @set! breaksys.defaults = defs - unks = copy(get_unknowns(breaksys)) - push!(unks, new_var) - @set! breaksys.unknowns = unks + if tf.added_input_is_param + ps = copy(get_ps(breaksys)) + push!(ps, new_var) + @set! breaksys.ps = ps + else + unks = copy(get_unknowns(breaksys)) + push!(unks, new_var) + @set! breaksys.unknowns = unks + end return breaksys, (new_var,) end @@ -812,12 +824,7 @@ Given a list of analysis points, break the connection for each and set the outpu """ function handle_loop_openings(sys::AbstractSystem, aps) for ap in canonicalize_ap(sys, aps) - sys, (outvar,) = apply_transformation(Break(ap, true, true), sys) - if Symbolics.isarraysymbolic(outvar) - push!(get_eqs(sys), outvar ~ zeros(size(outvar))) - else - push!(get_eqs(sys), outvar ~ 0) - end + sys, _ = apply_transformation(Break(ap, true, true, true), sys) end return sys end @@ -849,10 +856,10 @@ All other keyword arguments are forwarded to `linearization_function`. """ function get_linear_analysis_function( sys::AbstractSystem, transform, aps; system_modifier = identity, loop_openings = [], kwargs...) - sys = handle_loop_openings(sys, loop_openings) - aps = canonicalize_ap(sys, aps) dus = [] us = [] + sys = handle_loop_openings(sys, loop_openings) + aps = canonicalize_ap(sys, aps) for ap in aps sys, (du, u) = apply_transformation(transform(ap), sys) push!(dus, du) From 561b14db9015a808d238d6fad4bf47f41f692838 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 4 Jul 2025 17:22:41 +0530 Subject: [PATCH 2166/2176] fix: make loop opening parameters solvable --- src/systems/analysis_points.jl | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/systems/analysis_points.jl b/src/systems/analysis_points.jl index 04f6baae49..3fc7f445fb 100644 --- a/src/systems/analysis_points.jl +++ b/src/systems/analysis_points.jl @@ -824,7 +824,14 @@ Given a list of analysis points, break the connection for each and set the outpu """ function handle_loop_openings(sys::AbstractSystem, aps) for ap in canonicalize_ap(sys, aps) - sys, _ = apply_transformation(Break(ap, true, true, true), sys) + sys, (d_v,) = apply_transformation(Break(ap, true, true, true), sys) + guesses = copy(get_guesses(sys)) + guesses[d_v] = if symbolic_type(d_v) == ArraySymbolic() + fill(NaN, size(d_v)) + else + NaN + end + @set! sys.guesses = guesses end return sys end From 700717a5ca351476b0a8dbcb56db6eb1225501b6 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 4 Jul 2025 20:21:10 +0530 Subject: [PATCH 2167/2176] build: bump minor version --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 845724964d..b904650026 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "ModelingToolkit" uuid = "961ee093-0014-501f-94e3-6117800e7a78" authors = ["Yingbo Ma ", "Chris Rackauckas and contributors"] -version = "10.7.0" +version = "10.8.0" [deps] ADTypes = "47edcb42-4c32-4615-8424-f2b9edc5f35b" From dfeb5a52357400a2c3a53e093b2ec2df45f54fe5 Mon Sep 17 00:00:00 2001 From: contradict Date: Tue, 8 Jul 2025 13:47:50 -0700 Subject: [PATCH 2168/2176] Fix formatter crash due to bipartite_graph.jl --- src/bipartite_graph.jl | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/bipartite_graph.jl b/src/bipartite_graph.jl index 8cdb76cca0..a4d4fecc2a 100644 --- a/src/bipartite_graph.jl +++ b/src/bipartite_graph.jl @@ -29,7 +29,8 @@ Base.iterate(u::Unassigned, state) = nothing Base.show(io::IO, ::Unassigned) = printstyled(io, "u"; color = :light_black) -struct Matching{U, V <: AbstractVector} <: AbstractVector{Union{U, Int}} #=> :Unassigned =# +#U=> :Unassigned =# +struct Matching{U, V <: AbstractVector} <: AbstractVector{Union{U, Int}} match::V inv_match::Union{Nothing, V} end From 83135cbca6c0525faaa34b8facac8264867f7c90 Mon Sep 17 00:00:00 2001 From: contradict Date: Tue, 8 Jul 2025 13:51:39 -0700 Subject: [PATCH 2169/2176] Run formatter on all files --- docs/src/API/variables.md | 2 +- docs/src/basics/MTKLanguage.md | 3 +- docs/src/basics/Validation.md | 2 +- docs/src/examples/perturbation.md | 3 +- docs/src/examples/sparse_jacobians.md | 21 +++-- docs/src/examples/tearing_parallelism.md | 4 +- docs/src/tutorials/disturbance_modeling.md | 4 +- ext/MTKCasADiDynamicOptExt.jl | 28 +++++-- ext/MTKInfiniteOptExt.jl | 54 ++++++++---- ext/MTKPyomoDynamicOptExt.jl | 3 +- src/bipartite_graph.jl | 1 + src/deprecations.jl | 5 +- src/inputoutput.jl | 4 +- src/linearization.jl | 6 +- src/modelingtoolkitize/sdeproblem.jl | 3 +- src/problems/bvproblem.jl | 3 +- src/problems/daeproblem.jl | 4 +- src/problems/ddeproblem.jl | 3 +- src/problems/discreteproblem.jl | 3 +- src/problems/implicitdiscreteproblem.jl | 3 +- src/problems/intervalnonlinearproblem.jl | 3 +- src/problems/jumpproblem.jl | 6 +- src/problems/linearproblem.jl | 3 +- src/problems/nonlinearproblem.jl | 6 +- src/problems/odeproblem.jl | 6 +- src/problems/optimizationproblem.jl | 12 ++- src/problems/sccnonlinearproblem.jl | 3 +- src/problems/sddeproblem.jl | 3 +- src/problems/sdeproblem.jl | 3 +- src/structural_transformation/bareiss.jl | 2 + src/structural_transformation/codegen.jl | 1 + .../partial_state_selection.jl | 3 +- .../symbolics_tearing.jl | 13 ++- src/structural_transformation/tearing.jl | 1 + src/structural_transformation/utils.jl | 7 +- src/systems/abstractsystem.jl | 10 ++- src/systems/alias_elimination.jl | 5 +- src/systems/analysis_points.jl | 45 ++++++---- src/systems/callbacks.jl | 17 ++-- src/systems/clock_inference.jl | 3 +- src/systems/codegen.jl | 6 +- src/systems/if_lifting.jl | 84 +++++++++++++------ src/systems/index_cache.jl | 6 +- .../nonlinear/homotopy_continuation.jl | 3 +- src/systems/nonlinear/initializesystem.jl | 12 ++- src/systems/optimal_control_interface.jl | 47 ++++++----- src/systems/parameter_buffer.jl | 33 +++++--- src/systems/problem_utils.jl | 12 +-- src/systems/system.jl | 20 +++-- src/systems/systems.jl | 1 + src/systems/systemstructure.jl | 11 ++- src/variables.jl | 3 +- test/analysis_points.jl | 12 ++- test/basic_transformations.jl | 16 ++-- test/changeofvariables.jl | 80 +++++++++--------- test/code_generation.jl | 6 +- test/dde.jl | 7 +- test/dep_graphs.jl | 30 +++---- test/direct.jl | 6 +- test/downstream/linearization_dd.jl | 6 +- test/downstream/test_disturbance_model.jl | 8 +- test/dq_units.jl | 12 +-- test/extensions/dynamic_optimization.jl | 38 +++++---- test/extensions/homotopy_continuation.jl | 2 +- test/extensions/test_infiniteopt.jl | 14 ++-- test/fmi/fmi.jl | 2 +- test/initial_values.jl | 14 ++-- test/initializationsystem.jl | 42 +++++++--- test/input_output_handling.jl | 24 ++++-- test/inputoutput.jl | 4 +- test/jacobiansparsity.jl | 18 ++-- test/linearize.jl | 8 +- test/modelingtoolkitize.jl | 27 +++--- test/mtkparameters.jl | 10 +-- test/nonlinearsystem.jl | 2 +- test/odesystem.jl | 7 +- test/reduction.jl | 2 +- test/scc_nonlinear_problem.jl | 6 +- test/sciml_problem_inputs.jl | 5 ++ test/split_parameters.jl | 6 +- test/stream_connectors.jl | 4 +- .../index_reduction.jl | 2 +- test/symbolic_events.jl | 9 +- test/test_variable_metadata.jl | 2 +- test/units.jl | 14 ++-- test/variable_parsing.jl | 2 +- 86 files changed, 626 insertions(+), 370 deletions(-) diff --git a/docs/src/API/variables.md b/docs/src/API/variables.md index 6f48c9a5f8..e4db36d316 100644 --- a/docs/src/API/variables.md +++ b/docs/src/API/variables.md @@ -293,7 +293,7 @@ For systems that contain parameters with metadata like described above, have som In the example below, we define a system with tunable parameters and extract bounds vectors ```@example metadata -@variables x(t)=0 u(t)=0 [input = true] y(t)=0 [output = true] +@variables x(t)=0 u(t)=0 [input=true] y(t)=0 [output=true] @parameters T [tunable = true, bounds = (0, Inf)] @parameters k [tunable = true, bounds = (0, Inf)] eqs = [D(x) ~ (-x + k * u) / T # A first-order system with time constant T and gain k diff --git a/docs/src/basics/MTKLanguage.md b/docs/src/basics/MTKLanguage.md index 09fa6d2daa..b15eec126f 100644 --- a/docs/src/basics/MTKLanguage.md +++ b/docs/src/basics/MTKLanguage.md @@ -381,7 +381,8 @@ Refer the following example for different ways to define symbolic arrays. @parameters begin p1[1:4] p2[1:N] - p3[1:N, 1:M] = 10, + p3[1:N, + 1:M] = 10, [description = "A multi-dimensional array of arbitrary length with description"] (p4[1:N, 1:M] = 10), [description = "An alternate syntax for p3 to match the syntax of vanilla parameters macro"] diff --git a/docs/src/basics/Validation.md b/docs/src/basics/Validation.md index 6e17beeded..3f36a06e5e 100644 --- a/docs/src/basics/Validation.md +++ b/docs/src/basics/Validation.md @@ -108,7 +108,7 @@ function ModelingToolkit.get_unit(op::typeof(dummycomplex), args) end sts = @variables a(t)=0 [unit = u"cm"] -ps = @parameters s=-1 [unit = u"cm"] c=c [unit = u"cm"] +ps = @parameters s=-1 [unit=u"cm"] c=c [unit=u"cm"] eqs = [D(a) ~ dummycomplex(c, s);] sys = System( eqs, t, [sts...;], [ps...;], name = :sys, checks = ~ModelingToolkit.CheckUnits) diff --git a/docs/src/examples/perturbation.md b/docs/src/examples/perturbation.md index fa01edb70f..b80016a43f 100644 --- a/docs/src/examples/perturbation.md +++ b/docs/src/examples/perturbation.md @@ -99,7 +99,8 @@ sol = solve(prob) plot(sol, idxs = substitute(x_series, ϵ => 0.1); label = "Perturbative (ϵ=0.1)") x_exact(t, ϵ) = exp(-ϵ * t) * sin(√(1 - ϵ^2) * t) / √(1 - ϵ^2) -@assert isapprox(sol(π/2; idxs = substitute(x_series, ϵ => 0.1)), x_exact(π/2, 0.1); atol = 1e-2) # compare around 1st peak # hide +@assert isapprox( + sol(π/2; idxs = substitute(x_series, ϵ => 0.1)), x_exact(π/2, 0.1); atol = 1e-2) # compare around 1st peak # hide plot!(sol.t, x_exact.(sol.t, 0.1); label = "Exact (ϵ=0.1)") ``` diff --git a/docs/src/examples/sparse_jacobians.md b/docs/src/examples/sparse_jacobians.md index 79a93c3471..a87f824d8d 100644 --- a/docs/src/examples/sparse_jacobians.md +++ b/docs/src/examples/sparse_jacobians.md @@ -23,15 +23,20 @@ function brusselator_2d_loop(du, u, p, t) @inbounds for I in CartesianIndices((N, N)) i, j = Tuple(I) x, y = xyd_brusselator[I[1]], xyd_brusselator[I[2]] - ip1, im1, jp1, jm1 = limit(i + 1, N), limit(i - 1, N), limit(j + 1, N), + ip1, im1, jp1, + jm1 = limit(i + 1, N), limit(i - 1, N), limit(j + 1, N), limit(j - 1, N) - du[i, j, 1] = alpha * (u[im1, j, 1] + u[ip1, j, 1] + u[i, jp1, 1] + u[i, jm1, 1] - - 4u[i, j, 1]) + - B + u[i, j, 1]^2 * u[i, j, 2] - (A + 1) * u[i, j, 1] + - brusselator_f(x, y, t) - du[i, j, 2] = alpha * (u[im1, j, 2] + u[ip1, j, 2] + u[i, jp1, 2] + u[i, jm1, 2] - - 4u[i, j, 2]) + - A * u[i, j, 1] - u[i, j, 1]^2 * u[i, j, 2] + du[i, + j, + 1] = alpha * (u[im1, j, 1] + u[ip1, j, 1] + u[i, jp1, 1] + u[i, jm1, 1] - + 4u[i, j, 1]) + + B + u[i, j, 1]^2 * u[i, j, 2] - (A + 1) * u[i, j, 1] + + brusselator_f(x, y, t) + du[i, + j, + 2] = alpha * (u[im1, j, 2] + u[ip1, j, 2] + u[i, jp1, 2] + u[i, jm1, 2] - + 4u[i, j, 2]) + + A * u[i, j, 1] - u[i, j, 1]^2 * u[i, j, 2] end end p = (3.4, 1.0, 10.0, step(xyd_brusselator)) diff --git a/docs/src/examples/tearing_parallelism.md b/docs/src/examples/tearing_parallelism.md index f123f8b7b3..924102eff0 100644 --- a/docs/src/examples/tearing_parallelism.md +++ b/docs/src/examples/tearing_parallelism.md @@ -15,7 +15,7 @@ using ModelingToolkit: t_nounits as t, D_nounits as D # Basic electric components @connector function Pin(; name) - @variables v(t)=1.0 i(t)=1.0 [connect = Flow] + @variables v(t)=1.0 i(t)=1.0 [connect=Flow] System(Equation[], t, [v, i], [], name = name) end @@ -36,7 +36,7 @@ function ConstantVoltage(; name, V = 1.0) end @connector function HeatPort(; name) - @variables T(t)=293.15 Q_flow(t)=0.0 [connect = Flow] + @variables T(t)=293.15 Q_flow(t)=0.0 [connect=Flow] System(Equation[], t, [T, Q_flow], [], name = name) end diff --git a/docs/src/tutorials/disturbance_modeling.md b/docs/src/tutorials/disturbance_modeling.md index cdbfa25b40..e4b980a707 100644 --- a/docs/src/tutorials/disturbance_modeling.md +++ b/docs/src/tutorials/disturbance_modeling.md @@ -188,7 +188,9 @@ disturbance_inputs = [ssys.d1, ssys.d2] P = ssys.system_model outputs = [P.inertia1.phi, P.inertia2.phi, P.inertia1.w, P.inertia2.w] -(f_oop, f_ip), x_sym, p_sym, io_sys = ModelingToolkit.generate_control_function( +(f_oop, f_ip), x_sym, +p_sym, +io_sys = ModelingToolkit.generate_control_function( model_with_disturbance, inputs, disturbance_inputs; disturbance_argument = true) g = ModelingToolkit.build_explicit_observed_function( diff --git a/ext/MTKCasADiDynamicOptExt.jl b/ext/MTKCasADiDynamicOptExt.jl index 73e4a77ed4..addc478d98 100644 --- a/ext/MTKCasADiDynamicOptExt.jl +++ b/ext/MTKCasADiDynamicOptExt.jl @@ -17,7 +17,9 @@ struct MXLinearInterpolation t::Vector{Float64} dt::Float64 end -Base.getindex(m::MXLinearInterpolation, i...) = length(i) == length(size(m.u)) ? m.u[i...] : m.u[i..., :] +function Base.getindex(m::MXLinearInterpolation, i...) + length(i) == length(size(m.u)) ? m.u[i...] : m.u[i..., :] +end mutable struct CasADiModel model::Opti @@ -55,7 +57,7 @@ function (M::MXLinearInterpolation)(τ) (i > length(M.t) || i < 1) && error("Cannot extrapolate past the tspan.") colons = ntuple(_ -> (:), length(size(M.u)) - 1) if i < length(M.t) - M.u[colons..., i] + Δ*(M.u[colons..., i+1] - M.u[colons..., i]) + M.u[colons..., i] + Δ*(M.u[colons..., i + 1] - M.u[colons..., i]) else M.u[colons..., i] end @@ -65,7 +67,9 @@ function MTK.CasADiDynamicOptProblem(sys::System, op, tspan; dt = nothing, steps = nothing, guesses = Dict(), kwargs...) - prob, _ = MTK.process_DynamicOptProblem(CasADiDynamicOptProblem, CasADiModel, sys, op, tspan; dt, steps, guesses, kwargs...) + prob, + _ = MTK.process_DynamicOptProblem( + CasADiDynamicOptProblem, CasADiModel, sys, op, tspan; dt, steps, guesses, kwargs...) prob end @@ -127,10 +131,10 @@ function MTK.lowered_integral(model::CasADiModel, expr, lo, hi) for (i, t) in enumerate(model.U.t) if lo < t < hi Δt = min(dt, t - lo) - total += (0.5*Δt*(expr[i] + expr[i-1])) + total += (0.5*Δt*(expr[i] + expr[i - 1])) elseif t >= hi && (t - dt < hi) Δt = hi - t + dt - total += (0.5*Δt*(expr[i] + expr[i-1])) + total += (0.5*Δt*(expr[i] + expr[i - 1])) end end model.tₛ * total @@ -186,9 +190,13 @@ struct CasADiCollocation <: AbstractCollocation tableau::DiffEqBase.ODERKTableau end -MTK.CasADiCollocation(solver, tableau = MTK.constructDefault()) = CasADiCollocation(solver, tableau) +function MTK.CasADiCollocation(solver, tableau = MTK.constructDefault()) + CasADiCollocation(solver, tableau) +end -function MTK.prepare_and_optimize!(prob::CasADiDynamicOptProblem, solver::CasADiCollocation; verbose = false, solver_options = Dict(), plugin_options = Dict(), kwargs...) +function MTK.prepare_and_optimize!( + prob::CasADiDynamicOptProblem, solver::CasADiCollocation; verbose = false, + solver_options = Dict(), plugin_options = Dict(), kwargs...) solver_opti = add_solve_constraints!(prob, solver.tableau) verbose || (solver_options["print_level"] = 0) solver!(solver_opti, "$(solver.solver)", plugin_options, solver_options) @@ -224,9 +232,11 @@ function MTK.get_t_values(model::CasADiModel) value_getter = MTK.successful_solve(model) ? CasADi.debug_value : CasADi.value ts = value_getter(model.solver_opti, model.tₛ) .* model.U.t end -MTK.objective_value(model::CasADiModel) = CasADi.pyconvert(Float64, model.solver_opti.py.value(model.solver_opti.py.f)) +function MTK.objective_value(model::CasADiModel) + CasADi.pyconvert(Float64, model.solver_opti.py.value(model.solver_opti.py.f)) +end -function MTK.successful_solve(m::CasADiModel) +function MTK.successful_solve(m::CasADiModel) isnothing(m.solver_opti) && return false retcode = CasADi.return_status(m.solver_opti) retcode == "Solve_Succeeded" || retcode == "Solved_To_Acceptable_Level" diff --git a/ext/MTKInfiniteOptExt.jl b/ext/MTKInfiniteOptExt.jl index 8150cd06f7..e0f02c0436 100644 --- a/ext/MTKInfiniteOptExt.jl +++ b/ext/MTKInfiniteOptExt.jl @@ -48,20 +48,26 @@ struct InfiniteOptDynamicOptProblem{uType, tType, isinplace, P, F, K} <: end MTK.generate_internal_model(m::Type{InfiniteOptModel}) = InfiniteModel() -MTK.generate_time_variable!(m::InfiniteModel, tspan, tsteps) = @infinite_parameter(m, t in [tspan[1], tspan[2]], num_supports = length(tsteps)) -MTK.generate_state_variable!(m::InfiniteModel, u0::Vector, ns, ts) = @variable(m, U[i = 1:ns], Infinite(m[:t]), start=u0[i]) -MTK.generate_input_variable!(m::InfiniteModel, c0, nc, ts) = @variable(m, V[i = 1:nc], Infinite(m[:t]), start=c0[i]) +function MTK.generate_time_variable!(m::InfiniteModel, tspan, tsteps) + @infinite_parameter(m, t in [tspan[1], tspan[2]], num_supports = length(tsteps)) +end +function MTK.generate_state_variable!(m::InfiniteModel, u0::Vector, ns, ts) + @variable(m, U[i = 1:ns], Infinite(m[:t]), start=u0[i]) +end +function MTK.generate_input_variable!(m::InfiniteModel, c0, nc, ts) + @variable(m, V[i = 1:nc], Infinite(m[:t]), start=c0[i]) +end function MTK.generate_timescale!(m::InfiniteModel, guess, is_free_t) @variable(m, tₛ ≥ 0, start = guess) if !is_free_t - fix(tₛ, 1, force=true) + fix(tₛ, 1, force = true) set_start_value(tₛ, 1) end tₛ end -function MTK.add_constraint!(m::InfiniteOptModel, expr::Union{Equation, Inequality}) +function MTK.add_constraint!(m::InfiniteOptModel, expr::Union{Equation, Inequality}) if expr isa Equation @constraint(m.model, expr.lhs - expr.rhs == 0) elseif expr.relational_op === Symbolics.geq @@ -76,7 +82,9 @@ function MTK.JuMPDynamicOptProblem(sys::System, op, tspan; dt = nothing, steps = nothing, guesses = Dict(), kwargs...) - prob, _ = MTK.process_DynamicOptProblem(JuMPDynamicOptProblem, InfiniteOptModel, sys, op, tspan; dt, steps, guesses, kwargs...) + prob, + _ = MTK.process_DynamicOptProblem(JuMPDynamicOptProblem, InfiniteOptModel, sys, + op, tspan; dt, steps, guesses, kwargs...) prob end @@ -84,12 +92,16 @@ function MTK.InfiniteOptDynamicOptProblem(sys::System, op, tspan; dt = nothing, steps = nothing, guesses = Dict(), kwargs...) - prob, pmap = MTK.process_DynamicOptProblem(InfiniteOptDynamicOptProblem, InfiniteOptModel, sys, op, tspan; dt, steps, guesses, kwargs...) + prob, + pmap = MTK.process_DynamicOptProblem(InfiniteOptDynamicOptProblem, InfiniteOptModel, + sys, op, tspan; dt, steps, guesses, kwargs...) MTK.add_equational_constraints!(prob.wrapped_model, sys, pmap, tspan) prob end -MTK.lowered_integral(model::InfiniteOptModel, expr, lo, hi) = model.tₛ * InfiniteOpt.∫(expr, model.model[:t], lo, hi) +function MTK.lowered_integral(model::InfiniteOptModel, expr, lo, hi) + model.tₛ * InfiniteOpt.∫(expr, model.model[:t], lo, hi) +end MTK.lowered_derivative(model::InfiniteOptModel, i) = ∂(model.U[i], model.model[:t]) function MTK.process_integral_bounds(model::InfiniteOptModel, integral_span, tspan) @@ -125,7 +137,7 @@ function add_solve_constraints!(prob::JuMPDynamicOptProblem, tableau) nᵥ = length(V) if MTK.is_explicit(tableau) K = Any[] - for τ in tsteps[1:end-1] + for τ in tsteps[1:(end - 1)] for (i, h) in enumerate(c) ΔU = sum([A[i, j] * K[j] for j in 1:(i - 1)], init = zeros(nᵤ)) Uₙ = [U[i](τ) + ΔU[i] * dt for i in 1:nᵤ] @@ -142,14 +154,15 @@ function add_solve_constraints!(prob::JuMPDynamicOptProblem, tableau) K = @variable(model, K[1:length(α), 1:nᵤ], Infinite(model[:t])) ΔUs = A * K ΔU_tot = dt * (K' * α) - for τ in tsteps[1:end-1] + for τ in tsteps[1:(end - 1)] for (i, h) in enumerate(c) ΔU = @view ΔUs[i, :] Uₙ = U + ΔU * dt @constraint(model, [j = 1:nᵤ], K[i, j]==(tₛ * f(Uₙ, V, p, τ + h * dt)[j]), DomainRestrictions(t => τ), base_name="solve_K$i($τ)") end - @constraint(model, [n = 1:nᵤ], U[n](τ) + ΔU_tot[n]==U[n](min(τ + dt, tsteps[end])), + @constraint(model, + [n = 1:nᵤ], U[n](τ) + ΔU_tot[n]==U[n](min(τ + dt, tsteps[end])), DomainRestrictions(t => τ), base_name="solve_U($τ)") end end @@ -159,15 +172,21 @@ struct JuMPCollocation <: AbstractCollocation solver::Any tableau::DiffEqBase.ODERKTableau end -MTK.JuMPCollocation(solver, tableau = MTK.constructDefault()) = JuMPCollocation(solver, tableau) +function MTK.JuMPCollocation(solver, tableau = MTK.constructDefault()) + JuMPCollocation(solver, tableau) +end struct InfiniteOptCollocation <: AbstractCollocation solver::Any derivative_method::InfiniteOpt.AbstractDerivativeMethod end -MTK.InfiniteOptCollocation(solver, derivative_method = InfiniteOpt.FiniteDifference(InfiniteOpt.Backward())) = InfiniteOptCollocation(solver, derivative_method) +function MTK.InfiniteOptCollocation( + solver, derivative_method = InfiniteOpt.FiniteDifference(InfiniteOpt.Backward())) + InfiniteOptCollocation(solver, derivative_method) +end -function MTK.prepare_and_optimize!(prob::JuMPDynamicOptProblem, solver::JuMPCollocation; verbose = false, kwargs...) +function MTK.prepare_and_optimize!( + prob::JuMPDynamicOptProblem, solver::JuMPCollocation; verbose = false, kwargs...) model = prob.wrapped_model.model verbose || set_silent(model) # Unregister current solver constraints @@ -190,7 +209,8 @@ function MTK.prepare_and_optimize!(prob::JuMPDynamicOptProblem, solver::JuMPColl model end -function MTK.prepare_and_optimize!(prob::InfiniteOptDynamicOptProblem, solver::InfiniteOptCollocation; verbose = false, kwargs...) +function MTK.prepare_and_optimize!(prob::InfiniteOptDynamicOptProblem, + solver::InfiniteOptCollocation; verbose = false, kwargs...) model = prob.wrapped_model.model verbose || set_silent(model) set_derivative_method(model[:t], solver.derivative_method) @@ -223,8 +243,8 @@ function MTK.successful_solve(model::InfiniteModel) error("Model not solvable; please report this to github.com/SciML/ModelingToolkit.jl with a MWE.") pstatus === FEASIBLE_POINT && - (tstatus === OPTIMAL || tstatus === LOCALLY_SOLVED || tstatus === ALMOST_OPTIMAL || - tstatus === ALMOST_LOCALLY_SOLVED) + (tstatus === OPTIMAL || tstatus === LOCALLY_SOLVED || tstatus === ALMOST_OPTIMAL || + tstatus === ALMOST_LOCALLY_SOLVED) end import InfiniteOpt: JuMP, GeneralVariableRef diff --git a/ext/MTKPyomoDynamicOptExt.jl b/ext/MTKPyomoDynamicOptExt.jl index cb0aafc432..5b4e9e7a1c 100644 --- a/ext/MTKPyomoDynamicOptExt.jl +++ b/ext/MTKPyomoDynamicOptExt.jl @@ -108,7 +108,8 @@ function MTK.add_constraint!(pmodel::PyomoDynamicOptModel, cons; n_idxs = 1) else cons.lhs - cons.rhs ≤ 0 end - expr = Symbolics.substitute(Symbolics.unwrap(expr), SPECIAL_FUNCTIONS_DICT, fold = false) + expr = Symbolics.substitute( + Symbolics.unwrap(expr), SPECIAL_FUNCTIONS_DICT, fold = false) cons_sym = Symbol("cons", hash(cons)) if occursin(Symbolics.unwrap(t_sym), expr) diff --git a/src/bipartite_graph.jl b/src/bipartite_graph.jl index a4d4fecc2a..6e4f359617 100644 --- a/src/bipartite_graph.jl +++ b/src/bipartite_graph.jl @@ -635,6 +635,7 @@ function Graphs.incidence_matrix(g::BipartiteGraph, val = true) I = Int[] J = Int[] for i in 𝑠vertices(g), n in 𝑠neighbors(g, i) + push!(I, i) push!(J, n) end diff --git a/src/deprecations.jl b/src/deprecations.jl index 7ccd323060..bbd266fa0c 100644 --- a/src/deprecations.jl +++ b/src/deprecations.jl @@ -62,6 +62,7 @@ for T in [:ODEProblem, :DDEProblem, :SDEProblem, :SDDEProblem, :DAEProblem, end for pType in [SciMLBase.NullParameters, Nothing], uType in [Any, Nothing] + @eval function SciMLBase.$T(sys::System, u0::$uType, tspan, p::$pType; kw...) ctor = string($T) pT = string($(QuoteNode(pType))) @@ -142,6 +143,7 @@ for T in [:NonlinearProblem, :NonlinearLeastSquaresProblem, end end for pType in [SciMLBase.NullParameters, Nothing], uType in [Any, Nothing] + @eval function SciMLBase.$T(sys::System, u0::$uType, p::$pType; kw...) ctor = string($T) pT = string($(QuoteNode(pType))) @@ -173,7 +175,8 @@ end macro brownian(xs...) return quote - Base.depwarn("`@brownian` is deprecated. Use `@brownians` instead", :brownian_macro) + Base.depwarn( + "`@brownian` is deprecated. Use `@brownians` instead", :brownian_macro) $(@__MODULE__).@brownians $(xs...) end |> esc end diff --git a/src/inputoutput.jl b/src/inputoutput.jl index ac022cbe2f..6a17f76d80 100644 --- a/src/inputoutput.jl +++ b/src/inputoutput.jl @@ -120,7 +120,7 @@ function same_or_inner_namespace(u, var) nu == nv || # namespaces are the same startswith(nv, nu) || # or nv starts with nu, i.e., nv is an inner namespace to nu occursin(NAMESPACE_SEPARATOR, string(getname(var))) && - !occursin(NAMESPACE_SEPARATOR, string(getname(u))) # or u is top level but var is internal + !occursin(NAMESPACE_SEPARATOR, string(getname(u))) # or u is top level but var is internal end function inner_namespace(u, var) @@ -129,7 +129,7 @@ function inner_namespace(u, var) nu == nv && return false startswith(nv, nu) || # or nv starts with nu, i.e., nv is an inner namespace to nu occursin(NAMESPACE_SEPARATOR, string(getname(var))) && - !occursin(NAMESPACE_SEPARATOR, string(getname(u))) # or u is top level but var is internal + !occursin(NAMESPACE_SEPARATOR, string(getname(u))) # or u is top level but var is internal end """ diff --git a/src/linearization.jl b/src/linearization.jl index e1fa62e377..327381d611 100644 --- a/src/linearization.jl +++ b/src/linearization.jl @@ -298,7 +298,8 @@ function (linfun::LinearizationFunction)(u, p, t) error("Number of unknown variables ($(linfun.num_states)) does not match the number of input unknowns ($(length(u)))") integ_cache = (linfun.caches,) integ = MockIntegrator{true}(u, p, t, fun, integ_cache, nothing) - u, p, success = SciMLBase.get_initial_values( + u, p, + success = SciMLBase.get_initial_values( linfun.prob, integ, fun, linfun.initializealg, Val(true); linfun.initialize_kwargs...) if !success @@ -750,7 +751,8 @@ function linearize(sys, inputs, outputs; op = Dict(), t = 0.0, allow_input_derivatives = false, zero_dummy_der = false, kwargs...) - lin_fun, ssys = linearization_function(sys, + lin_fun, + ssys = linearization_function(sys, inputs, outputs; zero_dummy_der, diff --git a/src/modelingtoolkitize/sdeproblem.jl b/src/modelingtoolkitize/sdeproblem.jl index ff8c238559..0f63a35fc1 100644 --- a/src/modelingtoolkitize/sdeproblem.jl +++ b/src/modelingtoolkitize/sdeproblem.jl @@ -26,7 +26,8 @@ function modelingtoolkitize( odefn = ODEFunction{SciMLBase.isinplace(prob)}( prob.f.f; mass_matrix = prob.f.mass_matrix, sys = prob.f.sys) odeprob = ODEProblem(odefn, prob.u0, prob.tspan, prob.p) - sys, vars, params = modelingtoolkitize( + sys, vars, + params = modelingtoolkitize( odeprob; u_names, p_names, return_symbolic_u0_p = true, name = gensym(:MTKizedSDE), kwargs...) t = get_iv(sys) diff --git a/src/problems/bvproblem.jl b/src/problems/bvproblem.jl index 6344dfaa0b..1c193bca51 100644 --- a/src/problems/bvproblem.jl +++ b/src/problems/bvproblem.jl @@ -14,7 +14,8 @@ # for initialization. _op = has_alg_eqs(sys) ? op : merge(Dict(op), Dict(guesses)) - fode, u0, p = process_SciMLProblem( + fode, u0, + p = process_SciMLProblem( ODEFunction{iip, spec}, sys, _op; guesses, t = tspan !== nothing ? tspan[1] : tspan, check_compatibility = false, cse, checkbounds, time_dependent_init = false, expression, kwargs...) diff --git a/src/problems/daeproblem.jl b/src/problems/daeproblem.jl index d98e275f17..e2de96b7f5 100644 --- a/src/problems/daeproblem.jl +++ b/src/problems/daeproblem.jl @@ -67,7 +67,9 @@ end check_complete(sys, DAEProblem) check_compatibility && check_compatible_system(DAEProblem, sys) - f, du0, u0, p = process_SciMLProblem(DAEFunction{iip, spec}, sys, op; + f, du0, + u0, + p = process_SciMLProblem(DAEFunction{iip, spec}, sys, op; t = tspan !== nothing ? tspan[1] : tspan, check_length, eval_expression, eval_module, check_compatibility, implicit_dae = true, expression, kwargs...) diff --git a/src/problems/ddeproblem.jl b/src/problems/ddeproblem.jl index 4fb9b3f37b..fb4634f418 100644 --- a/src/problems/ddeproblem.jl +++ b/src/problems/ddeproblem.jl @@ -46,7 +46,8 @@ end check_complete(sys, DDEProblem) check_compatibility && check_compatible_system(DDEProblem, sys) - f, u0, p = process_SciMLProblem(DDEFunction{iip, spec}, sys, op; + f, u0, + p = process_SciMLProblem(DDEFunction{iip, spec}, sys, op; t = tspan !== nothing ? tspan[1] : tspan, check_length, cse, checkbounds, eval_expression, eval_module, check_compatibility, symbolic_u0 = true, expression, u0_constructor, kwargs...) diff --git a/src/problems/discreteproblem.jl b/src/problems/discreteproblem.jl index c4ab069ebe..820819d282 100644 --- a/src/problems/discreteproblem.jl +++ b/src/problems/discreteproblem.jl @@ -45,7 +45,8 @@ end dvs = unknowns(sys) op = to_varmap(op, dvs) add_toterms!(op; replace = true) - f, u0, p = process_SciMLProblem(DiscreteFunction{iip, spec}, sys, op; + f, u0, + p = process_SciMLProblem(DiscreteFunction{iip, spec}, sys, op; t = tspan !== nothing ? tspan[1] : tspan, check_compatibility, expression, kwargs...) diff --git a/src/problems/implicitdiscreteproblem.jl b/src/problems/implicitdiscreteproblem.jl index 9e500d7354..9c85153c4f 100644 --- a/src/problems/implicitdiscreteproblem.jl +++ b/src/problems/implicitdiscreteproblem.jl @@ -50,7 +50,8 @@ end dvs = unknowns(sys) op = to_varmap(op, dvs) add_toterms!(op; replace = true) - f, u0, p = process_SciMLProblem( + f, u0, + p = process_SciMLProblem( ImplicitDiscreteFunction{iip, spec}, sys, op; t = tspan !== nothing ? tspan[1] : tspan, check_compatibility, expression, kwargs...) diff --git a/src/problems/intervalnonlinearproblem.jl b/src/problems/intervalnonlinearproblem.jl index 2dc929fe25..d7221632d3 100644 --- a/src/problems/intervalnonlinearproblem.jl +++ b/src/problems/intervalnonlinearproblem.jl @@ -33,7 +33,8 @@ function SciMLBase.IntervalNonlinearProblem( u0map = unknowns(sys) .=> uspan[1] op = anydict([unknowns(sys)[1] => uspan[1]]) merge!(op, to_varmap(parammap, parameters(sys))) - f, u0, p = process_SciMLProblem(IntervalNonlinearFunction, sys, op; + f, u0, + p = process_SciMLProblem(IntervalNonlinearFunction, sys, op; check_compatibility, expression, kwargs...) kwargs = process_kwargs(sys; kwargs...) diff --git a/src/problems/jumpproblem.jl b/src/problems/jumpproblem.jl index dd3fd9ba1c..3bc0e11f61 100644 --- a/src/problems/jumpproblem.jl +++ b/src/problems/jumpproblem.jl @@ -22,7 +22,8 @@ build_initializeprob = false, checkbounds, cse, check_length = false, kwargs...) else - _, u0, p = process_SciMLProblem(EmptySciMLFunction{iip}, sys, op; + _, u0, + p = process_SciMLProblem(EmptySciMLFunction{iip}, sys, op; t = tspan === nothing ? nothing : tspan[1], check_length = false, build_initializeprob = false, kwargs...) observedfun = ObservedFunctionCache(sys; eval_expression, eval_module, @@ -32,7 +33,8 @@ prob = ODEProblem{true}(df, u0, tspan, p; kwargs...) end else - _f, u0, p = process_SciMLProblem(EmptySciMLFunction{iip}, sys, op; + _f, u0, + p = process_SciMLProblem(EmptySciMLFunction{iip}, sys, op; t = tspan === nothing ? nothing : tspan[1], check_length = false, build_initializeprob = false, cse, kwargs...) f = DiffEqBase.DISCRETE_INPLACE_DEFAULT diff --git a/src/problems/linearproblem.jl b/src/problems/linearproblem.jl index 26d5c932bd..4244e462c6 100644 --- a/src/problems/linearproblem.jl +++ b/src/problems/linearproblem.jl @@ -14,7 +14,8 @@ function SciMLBase.LinearProblem{iip}( check_complete(sys, LinearProblem) check_compatibility && check_compatible_system(LinearProblem, sys) - _, u0, p = process_SciMLProblem( + _, u0, + p = process_SciMLProblem( EmptySciMLFunction{iip}, sys, op; check_length, expression, build_initializeprob = false, symbolic_u0 = true, u0_constructor, u0_eltype, kwargs...) diff --git a/src/problems/nonlinearproblem.jl b/src/problems/nonlinearproblem.jl index 1ed7cda750..093e8db762 100644 --- a/src/problems/nonlinearproblem.jl +++ b/src/problems/nonlinearproblem.jl @@ -63,7 +63,8 @@ end end check_compatibility && check_compatible_system(NonlinearProblem, sys) - f, u0, p = process_SciMLProblem(NonlinearFunction{iip, spec}, sys, op; + f, u0, + p = process_SciMLProblem(NonlinearFunction{iip, spec}, sys, op; check_length, check_compatibility, expression, kwargs...) kwargs = process_kwargs(sys; kwargs...) @@ -79,7 +80,8 @@ end check_complete(sys, NonlinearLeastSquaresProblem) check_compatibility && check_compatible_system(NonlinearLeastSquaresProblem, sys) - f, u0, p = process_SciMLProblem(NonlinearFunction{iip}, sys, op; + f, u0, + p = process_SciMLProblem(NonlinearFunction{iip}, sys, op; check_length, expression, kwargs...) kwargs = process_kwargs(sys; kwargs...) diff --git a/src/problems/odeproblem.jl b/src/problems/odeproblem.jl index bc8b9cf701..da33963be6 100644 --- a/src/problems/odeproblem.jl +++ b/src/problems/odeproblem.jl @@ -70,7 +70,8 @@ end check_complete(sys, ODEProblem) check_compatibility && check_compatible_system(ODEProblem, sys) - f, u0, p = process_SciMLProblem(ODEFunction{iip, spec}, sys, op; + f, u0, + p = process_SciMLProblem(ODEFunction{iip, spec}, sys, op; t = tspan !== nothing ? tspan[1] : tspan, check_length, eval_expression, eval_module, expression, check_compatibility, kwargs...) @@ -88,7 +89,8 @@ end check_complete(sys, SteadyStateProblem) check_compatibility && check_compatible_system(SteadyStateProblem, sys) - f, u0, p = process_SciMLProblem(ODEFunction{iip}, sys, op; + f, u0, + p = process_SciMLProblem(ODEFunction{iip}, sys, op; steady_state = true, check_length, check_compatibility, expression, time_dependent_init = false, kwargs...) diff --git a/src/problems/optimizationproblem.jl b/src/problems/optimizationproblem.jl index 36b34a8867..978f973d83 100644 --- a/src/problems/optimizationproblem.jl +++ b/src/problems/optimizationproblem.jl @@ -23,7 +23,8 @@ function SciMLBase.OptimizationFunction{iip}(sys::System; _grad = nothing end if hess - _hess, hess_prototype = generate_cost_hessian( + _hess, + hess_prototype = generate_cost_hessian( sys; expression, wrap_gfw = Val{true}, eval_expression, eval_module, checkbounds, cse, sparse, simplify, return_sparsity = true, kwargs...) @@ -40,7 +41,8 @@ function SciMLBase.OptimizationFunction{iip}(sys::System; cons = generate_cons(sys; expression, wrap_gfw = Val{true}, eval_expression, eval_module, checkbounds, cse, kwargs...) if cons_j - _cons_j, cons_jac_prototype = generate_constraint_jacobian( + _cons_j, + cons_jac_prototype = generate_constraint_jacobian( sys; expression, wrap_gfw = Val{true}, eval_expression, eval_module, checkbounds, cse, simplify, sparse = cons_sparse, return_sparsity = true, kwargs...) @@ -48,7 +50,8 @@ function SciMLBase.OptimizationFunction{iip}(sys::System; _cons_j = cons_jac_prototype = nothing end if cons_h - _cons_h, cons_hess_prototype = generate_constraint_hessian( + _cons_h, + cons_hess_prototype = generate_constraint_hessian( sys; expression, wrap_gfw = Val{true}, eval_expression, eval_module, checkbounds, cse, simplify, sparse = cons_sparse, return_sparsity = true, kwargs...) @@ -92,7 +95,8 @@ function SciMLBase.OptimizationProblem{iip}( check_complete(sys, OptimizationProblem) check_compatibility && check_compatible_system(OptimizationProblem, sys) - f, u0, p = process_SciMLProblem(OptimizationFunction{iip}, sys, op; + f, u0, + p = process_SciMLProblem(OptimizationFunction{iip}, sys, op; check_compatibility, tofloat = false, check_length = false, expression, kwargs...) dvs = unknowns(sys) diff --git a/src/problems/sccnonlinearproblem.jl b/src/problems/sccnonlinearproblem.jl index d2afb7b315..2a44e3de4e 100644 --- a/src/problems/sccnonlinearproblem.jl +++ b/src/problems/sccnonlinearproblem.jl @@ -111,7 +111,8 @@ function SciMLBase.SCCNonlinearProblem{iip}(sys::System, op; eval_expression = f eqs = equations(sys) obs = observed(sys) - _, u0, p = process_SciMLProblem( + _, u0, + p = process_SciMLProblem( EmptySciMLFunction{iip}, sys, op; eval_expression, eval_module, kwargs...) explicitfuns = [] diff --git a/src/problems/sddeproblem.jl b/src/problems/sddeproblem.jl index 0e3201c1d1..f0e8e354aa 100644 --- a/src/problems/sddeproblem.jl +++ b/src/problems/sddeproblem.jl @@ -48,7 +48,8 @@ end check_complete(sys, SDDEProblem) check_compatibility && check_compatible_system(SDDEProblem, sys) - f, u0, p = process_SciMLProblem(SDDEFunction{iip, spec}, sys, op; + f, u0, + p = process_SciMLProblem(SDDEFunction{iip, spec}, sys, op; t = tspan !== nothing ? tspan[1] : tspan, check_length, cse, checkbounds, eval_expression, eval_module, check_compatibility, sparse, symbolic_u0 = true, expression, u0_constructor, kwargs...) diff --git a/src/problems/sdeproblem.jl b/src/problems/sdeproblem.jl index 83a050f7e4..e322775d2b 100644 --- a/src/problems/sdeproblem.jl +++ b/src/problems/sdeproblem.jl @@ -72,7 +72,8 @@ end check_complete(sys, SDEProblem) check_compatibility && check_compatible_system(SDEProblem, sys) - f, u0, p = process_SciMLProblem(SDEFunction{iip, spec}, sys, op; + f, u0, + p = process_SciMLProblem(SDEFunction{iip, spec}, sys, op; t = tspan !== nothing ? tspan[1] : tspan, check_length, eval_expression, eval_module, check_compatibility, sparse, expression, kwargs...) diff --git a/src/structural_transformation/bareiss.jl b/src/structural_transformation/bareiss.jl index 7957427e5d..602f656c27 100644 --- a/src/structural_transformation/bareiss.jl +++ b/src/structural_transformation/bareiss.jl @@ -131,6 +131,7 @@ end function bareiss_update!(zero!, M::StridedMatrix, k, swapto, pivot, prev_pivot) @inbounds for i in (k + 1):size(M, 2), j in (k + 1):size(M, 1) + M[j, i] = exactdiv(M[j, i] * pivot - M[j, k] * M[k, i], prev_pivot) end zero!(M, (k + 1):size(M, 1), k) @@ -269,6 +270,7 @@ function reduce_echelon!(A::AbstractMatrix{T}, rank, d, end @label out @inbounds for i in (rank + 1):m, j in 1:n + A[i, j] = zero(T) end isreduced && return A diff --git a/src/structural_transformation/codegen.jl b/src/structural_transformation/codegen.jl index 9afe7ec5e7..37a5b380ac 100644 --- a/src/structural_transformation/codegen.jl +++ b/src/structural_transformation/codegen.jl @@ -45,6 +45,7 @@ function torn_system_with_nlsolve_jacobian_sparsity(state, var_eq_matching, var_ var_rename = ones(Int64, ndsts(graph)) nlsolve_vars = Int[] for i in nlsolve_scc_idxs, c in var_sccs[i] + append!(nlsolve_vars, c) for v in c var_rename[v] = 0 diff --git a/src/structural_transformation/partial_state_selection.jl b/src/structural_transformation/partial_state_selection.jl index 89447a4e16..ab8e7f0f3d 100644 --- a/src/structural_transformation/partial_state_selection.jl +++ b/src/structural_transformation/partial_state_selection.jl @@ -213,7 +213,8 @@ function tearing_with_dummy_derivatives(structure, dummy_derivatives) can_eliminate[v] = true end end - var_eq_matching, full_var_eq_matching, var_sccs = tear_graph_modia(structure, + var_eq_matching, full_var_eq_matching, + var_sccs = tear_graph_modia(structure, Base.Fix1(isdiffed, (structure, dummy_derivatives)), Union{Unassigned, SelectedState}; varfilter = Base.Fix1(getindex, can_eliminate)) diff --git a/src/structural_transformation/symbolics_tearing.jl b/src/structural_transformation/symbolics_tearing.jl index c91fedb281..8f87f6d31f 100644 --- a/src/structural_transformation/symbolics_tearing.jl +++ b/src/structural_transformation/symbolics_tearing.jl @@ -338,6 +338,7 @@ function generate_derivative_variables!( # We need the inverse mapping of `var_sccs` to update it efficiently later. v_to_scc = Vector{NTuple{2, Int}}(undef, ndsts(graph)) for (i, scc) in enumerate(var_sccs), (j, v) in enumerate(scc) + v_to_scc[v] = (i, j) end # Pairs of `(x_t, dx)` added below @@ -475,7 +476,7 @@ function find_duplicate_dd(dv, solvable_graph, diff_to_var, linear_eqs, mm) if length(nzs) == 2 && (abs(nzs[1]) == 1 && nzs[1] == -nzs[2]) && (v_t = rvs[1] == dv ? rvs[2] : rvs[1]; - diff_to_var[v_t] === nothing) + diff_to_var[v_t] === nothing) @assert dv in rvs return eq, v_t end @@ -1065,7 +1066,11 @@ function tearing_reassemble(state::TearingState, var_eq_matching::Matching, var_sccs = generate_derivative_variables!( state, neweqs, var_eq_matching, full_var_eq_matching, var_sccs; mm, iv, D) - neweqs, solved_eqs, eq_ordering, var_ordering, nelim_eq, nelim_var = generate_system_equations!( + neweqs, solved_eqs, + eq_ordering, + var_ordering, + nelim_eq, + nelim_var = generate_system_equations!( state, neweqs, var_eq_matching, full_var_eq_matching, var_sccs, extra_eqs_vars; simplify, iv, D) @@ -1124,6 +1129,7 @@ function add_additional_history!( # We need the inverse mapping of `var_sccs` to update it efficiently later. v_to_scc = Vector{NTuple{2, Int}}(undef, ndsts(graph)) for (i, scc) in enumerate(var_sccs), (j, v) in enumerate(scc) + v_to_scc[v] = (i, j) end @@ -1346,7 +1352,8 @@ function dummy_derivative(sys, state = TearingState(sys); simplify = false, p end end - var_eq_matching, full_var_eq_matching, var_sccs, can_eliminate, summary = dummy_derivative_graph!( + var_eq_matching, full_var_eq_matching, var_sccs, + can_eliminate, summary = dummy_derivative_graph!( state, jac; state_priority, kwargs...) tearing_reassemble(state, var_eq_matching, full_var_eq_matching, var_sccs; diff --git a/src/structural_transformation/tearing.jl b/src/structural_transformation/tearing.jl index 31ebb8370a..67933ffe0e 100644 --- a/src/structural_transformation/tearing.jl +++ b/src/structural_transformation/tearing.jl @@ -74,6 +74,7 @@ function free_equations(graph, vars_scc, var_eq_matching, varfilter::F) where {F ne = nsrcs(graph) seen_eqs = falses(ne) for vars in vars_scc, var in vars + varfilter(var) || continue ieq = var_eq_matching[var] if ieq isa Int diff --git a/src/structural_transformation/utils.jl b/src/structural_transformation/utils.jl index 67eb5b0c73..032006b006 100644 --- a/src/structural_transformation/utils.jl +++ b/src/structural_transformation/utils.jl @@ -170,6 +170,7 @@ function sorted_incidence_matrix(ts::TransformationState, val = true; only_algeq varidx = 0 eqidx = 0 for vs in var_scc, v in vs + eq = var_eq_matching[v] if eq !== unassigned eqsmap[eq] = (eqidx += 1) @@ -255,9 +256,9 @@ function find_eq_solvables!(state::TearingState, ieq, to_rm = Int[], coeffs = no # if any of the variables in `a` are present in fullvars (taking into account arrays) if any( v -> any(isequal(v), fullvars) || - symbolic_type(v) == ArraySymbolic() && - Symbolics.shape(v) != Symbolics.Unknown() && - any(x -> any(isequal(x), fullvars), collect(v)), + symbolic_type(v) == ArraySymbolic() && + Symbolics.shape(v) != Symbolics.Unknown() && + any(x -> any(isequal(x), fullvars), collect(v)), vars(a)) continue end diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index 70e7b06bfe..e4e696934c 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -1552,7 +1552,8 @@ function full_equations(sys::AbstractSystem; simplify = false) subs = get_substitutions(sys) neweqs = map(equations(sys)) do eq if iscall(eq.lhs) && operation(eq.lhs) isa Union{Shift, Differential} - return substitute_and_simplify(eq.lhs, subs, simplify) ~ substitute_and_simplify( + return substitute_and_simplify(eq.lhs, subs, simplify) ~ + substitute_and_simplify( eq.rhs, subs, simplify) else @@ -2275,7 +2276,8 @@ function component_post_processing(expr, isconnector) if $isconnector $Setfield.@set!(res.connector_type=$connector_type(res)) end - $Setfield.@set!(res.gui_metadata=$GUIMetadata($GlobalRef(@__MODULE__, name))) + $Setfield.@set!(res.gui_metadata=$GUIMetadata($GlobalRef( + @__MODULE__, name))) else res end @@ -2736,8 +2738,8 @@ function process_parameter_equations(sys::AbstractSystem) if all(varsbuf) do sym is_parameter(sys, sym) || symbolic_type(sym) == ArraySymbolic() && - is_sized_array_symbolic(sym) && - all(Base.Fix1(is_parameter, sys), collect(sym)) + is_sized_array_symbolic(sym) && + all(Base.Fix1(is_parameter, sys), collect(sym)) end # Everything in `varsbuf` is a parameter, so this is a cheap `is_parameter` # check. diff --git a/src/systems/alias_elimination.jl b/src/systems/alias_elimination.jl index e0bfde2268..f24a2562fe 100644 --- a/src/systems/alias_elimination.jl +++ b/src/systems/alias_elimination.jl @@ -215,6 +215,7 @@ function find_linear_variables(graph, linear_equations, var_to_diff, irreducible eqs = get(var_to_lineq, v, nothing) eqs === nothing && continue for eq in eqs, v′ in 𝑠neighbors(graph, eq) + if linear_variables[v′] linear_variables[v′] = false push!(stack, v′) @@ -224,6 +225,7 @@ function find_linear_variables(graph, linear_equations, var_to_diff, irreducible end end for eq in linear_equations, v in 𝑠neighbors(graph, eq) + linear_variables[v] = true vlineqs = get!(() -> BitSet(), var_to_lineq, v) push!(vlineqs, eq) @@ -347,7 +349,8 @@ function do_bareiss!(M, Mold, is_linear_variables, is_highest_diff) (rank1, rank2, rank3, pivots) end -function alias_eliminate_graph!(state::TransformationState, ils::SparseMatrixCLIL; fully_determined = true, kwargs...) +function alias_eliminate_graph!(state::TransformationState, ils::SparseMatrixCLIL; + fully_determined = true, kwargs...) @unpack structure = state @unpack graph, solvable_graph, var_to_diff, eq_to_diff = state.structure # Step 1: Perform Bareiss factorization on the adjacency matrix of the linear diff --git a/src/systems/analysis_points.jl b/src/systems/analysis_points.jl index 3fc7f445fb..a5a612b9ca 100644 --- a/src/systems/analysis_points.jl +++ b/src/systems/analysis_points.jl @@ -238,8 +238,10 @@ end Return all the namespaces in `name`. Namespaces should be separated by `.` or `$NAMESPACE_SEPARATOR`. """ -namespace_hierarchy(name::Symbol) = map( - Symbol, split(string(name), ('.', NAMESPACE_SEPARATOR))) +function namespace_hierarchy(name::Symbol) + map( + Symbol, split(string(name), ('.', NAMESPACE_SEPARATOR))) +end """ $(TYPEDSIGNATURES) @@ -311,15 +313,19 @@ by `fn`. `root`, and returns a 2-tuple consisting of the modified version of `target` and a tuple of the extra variables added. """ -modify_nested_subsystem(fn, root::AbstractSystem, target::AbstractSystem) = modify_nested_subsystem( - fn, root, nameof(target)) +function modify_nested_subsystem(fn, root::AbstractSystem, target::AbstractSystem) + modify_nested_subsystem( + fn, root, nameof(target)) +end """ $(TYPEDSIGNATURES) Apply the modification to the system containing the namespaced analysis point `target`. """ -modify_nested_subsystem(fn, root::AbstractSystem, target::AnalysisPoint) = modify_nested_subsystem( - fn, root, @view namespace_hierarchy(nameof(target))[1:(end - 1)]) +function modify_nested_subsystem(fn, root::AbstractSystem, target::AnalysisPoint) + modify_nested_subsystem( + fn, root, @view namespace_hierarchy(nameof(target))[1:(end - 1)]) +end """ $(TYPEDSIGNATURES) @@ -327,8 +333,10 @@ Apply the modification to the nested subsystem of `root` whose namespaced name m the provided name `target`. The namespace separator in `target` should be `.` or `$NAMESPACE_SEPARATOR`. The `target` may include `nameof(root)` as the first namespace. """ -modify_nested_subsystem(fn, root::AbstractSystem, target::Symbol) = modify_nested_subsystem( - fn, root, namespace_hierarchy(target)) +function modify_nested_subsystem(fn, root::AbstractSystem, target::Symbol) + modify_nested_subsystem( + fn, root, namespace_hierarchy(target)) +end """ $(TYPEDSIGNATURES) @@ -393,8 +401,10 @@ end Given a system `sys` and analysis point `ap`, return the index in `get_eqs(sys)` containing an equation which has as it's RHS an analysis point with name `nameof(ap)`. """ -analysis_point_index(sys::AbstractSystem, ap::AnalysisPoint) = analysis_point_index( - sys, nameof(ap)) +function analysis_point_index(sys::AbstractSystem, ap::AnalysisPoint) + analysis_point_index( + sys, nameof(ap)) +end """ $(TYPEDSIGNATURES) @@ -651,7 +661,8 @@ function apply_transformation(tf::PerturbOutput, sys::AbstractSystem) tf.with_output || return ap_sys, (new_var,) # add output variable, equation, default - out_var, out_def = get_analysis_variable( + out_var, + out_def = get_analysis_variable( ap_ivar, nameof(ap), get_iv(sys); perturb = false) push!(ap_sys_eqs, out_var ~ ap_ivar + wrap(new_var)) push!(unks, out_var) @@ -701,7 +712,8 @@ function apply_transformation(tf::AddVariable, sys::AbstractSystem) # add equations involving new variable ap_ivar = ap_var(ap.input) - new_var, new_def = get_analysis_variable( + new_var, + new_def = get_analysis_variable( ap_ivar, tf.name, get_iv(sys); perturb = false) # add variable unks = copy(get_unknowns(ap_sys)) @@ -750,7 +762,8 @@ end function apply_transformation(cst::ComplementarySensitivityTransform, sys::AbstractSystem) sys, (u,) = apply_transformation(GetInput(cst.ap), sys) - sys, (du,) = apply_transformation( + sys, + (du,) = apply_transformation( AddVariable( cst.ap, Symbol(namespace_hierarchy(nameof(cst.ap))[end], :_comp_sens_du)), sys) @@ -931,7 +944,8 @@ for f in [:get_sensitivity, :get_comp_sensitivity, :get_looptransfer] @eval function $f( sys, ap, args...; loop_openings = [], system_modifier = identity, allow_input_derivatives = true, kwargs...) - lin_fun, ssys = $(utility_fun)( + lin_fun, + ssys = $(utility_fun)( sys, ap, args...; loop_openings, system_modifier, kwargs...) mats, extras = ModelingToolkit.linearize(ssys, lin_fun; allow_input_derivatives) mats, ssys, extras @@ -1000,7 +1014,8 @@ end function linearization_function(sys::AbstractSystem, inputs::Union{Symbol, Vector{Symbol}, AnalysisPoint, Vector{AnalysisPoint}}, outputs; loop_openings = [], system_modifier = identity, kwargs...) - sys, input_vars, output_vars = linearization_ap_transform( + sys, input_vars, + output_vars = linearization_ap_transform( sys, inputs, outputs, loop_openings) return linearization_function(system_modifier(sys), input_vars, output_vars; kwargs...) end diff --git a/src/systems/callbacks.jl b/src/systems/callbacks.jl index a4f39243d9..37e4c2c19a 100644 --- a/src/systems/callbacks.jl +++ b/src/systems/callbacks.jl @@ -527,7 +527,7 @@ function Base.:(==)(e1::AbstractCallback, e2::AbstractCallback) (is_discrete(e1) === is_discrete(e2)) || return false (isequal(e1.conditions, e2.conditions) && isequal(e1.affect, e2.affect) && isequal(e1.initialize, e2.initialize) && isequal(e1.finalize, e2.finalize)) && - isequal(e1.reinitializealg, e2.reinitializealg) || + isequal(e1.reinitializealg, e2.reinitializealg) || return false is_discrete(e1) || (isequal(e1.affect_neg, e2.affect_neg) && isequal(e1.rootfind, e2.rootfind)) @@ -836,8 +836,8 @@ function compile_equational_affect( obseqs, Dict([p => unPre(p) for p in parameters(affsys)])) rhss = map(x -> x.rhs, update_eqs) lhss = map(x -> x.lhs, update_eqs) - is_p = [lhs ∈ Set(ps_to_update) for lhs in lhss] - is_u = [lhs ∈ Set(dvs_to_update) for lhs in lhss] + is_p = [lhs in Set(ps_to_update) for lhs in lhss] + is_u = [lhs in Set(dvs_to_update) for lhs in lhss] dvs = unknowns(sys) ps = parameters(sys) t = get_iv(sys) @@ -854,11 +854,13 @@ function compile_equational_affect( _ps = reorder_parameters(sys, ps) integ = gensym(:MTKIntegrator) - u_up, u_up! = build_function_wrapper(sys, (@view rhss[is_u]), dvs, _ps..., t; + u_up, + u_up! = build_function_wrapper(sys, (@view rhss[is_u]), dvs, _ps..., t; wrap_code = add_integrator_header(sys, integ, :u), expression = Val{false}, outputidxs = u_idxs, wrap_mtkparameters, cse = false, eval_expression, eval_module) - p_up, p_up! = build_function_wrapper(sys, (@view rhss[is_p]), dvs, _ps..., t; + p_up, + p_up! = build_function_wrapper(sys, (@view rhss[is_p]), dvs, _ps..., t; wrap_code = add_integrator_header(sys, integ, :p), expression = Val{false}, outputidxs = p_idxs, wrap_mtkparameters, cse = false, eval_expression, eval_module) @@ -873,9 +875,8 @@ function compile_equational_affect( end end else - return let dvs_to_update = dvs_to_update, - affsys = affsys, ps_to_update = ps_to_update, aff = aff, sys = sys, - reset_jumps = reset_jumps + return let dvs_to_update = dvs_to_update, affsys = affsys, + ps_to_update = ps_to_update, aff = aff, sys = sys, reset_jumps = reset_jumps dvs_to_access = unknowns(affsys) ps_to_access = [unPre(p) for p in parameters(affsys)] diff --git a/src/systems/clock_inference.jl b/src/systems/clock_inference.jl index 42fe28f7c7..97b6be27ab 100644 --- a/src/systems/clock_inference.jl +++ b/src/systems/clock_inference.jl @@ -202,7 +202,8 @@ function split_system(ci::ClockInference{S}) where {S} if continuous_id != 0 tss[continuous_id], tss[end] = tss[end], tss[continuous_id] inputs[continuous_id], inputs[end] = inputs[end], inputs[continuous_id] - id_to_clock[continuous_id], id_to_clock[end] = id_to_clock[end], + id_to_clock[continuous_id], + id_to_clock[end] = id_to_clock[end], id_to_clock[continuous_id] continuous_id = lastindex(tss) end diff --git a/src/systems/codegen.jl b/src/systems/codegen.jl index 4a68f935e8..490e2892c1 100644 --- a/src/systems/codegen.jl +++ b/src/systems/codegen.jl @@ -787,7 +787,8 @@ function generate_constraint_jacobian( simplify = false, sparse = false, kwargs...) dvs = unknowns(sys) ps = reorder_parameters(sys) - jac, sparsity = calculate_constraint_jacobian( + jac, + sparsity = calculate_constraint_jacobian( sys; simplify, sparse, return_sparsity = true) res = build_function_wrapper(sys, jac, dvs, ps...; expression = Val{true}, kwargs...) fn = maybe_compile_function( @@ -841,7 +842,8 @@ function generate_constraint_hessian( simplify = false, sparse = false, kwargs...) dvs = unknowns(sys) ps = reorder_parameters(sys) - hess, sparsity = calculate_constraint_hessian( + hess, + sparsity = calculate_constraint_hessian( sys; simplify, sparse, return_sparsity = true) res = build_function_wrapper(sys, hess, dvs, ps...; expression = Val{true}, kwargs...) fn = maybe_compile_function( diff --git a/src/systems/if_lifting.jl b/src/systems/if_lifting.jl index c2eba035ff..aba9dbdcbf 100644 --- a/src/systems/if_lifting.jl +++ b/src/systems/if_lifting.jl @@ -375,34 +375,69 @@ end const CONDITION_SIMPLIFIER = Rewriters.Fixpoint(Rewriters.Postwalk(Rewriters.Chain([ # simple boolean laws (@rule (!!(~x)) => (~x)) - (@rule ((~x) & true) => (~x)) - (@rule ((~x) & false) => false) - (@rule ((~x) | true) => true) - (@rule ((~x) | false) => (~x)) - (@rule ((~x) & !(~x)) => false) - (@rule ((~x) | !(~x)) => true) + (@rule ((~x) & + true) => (~x)) + (@rule ((~x) & + false) => false) + (@rule ((~x) | + true) => true) + (@rule ((~x) | + false) => (~x)) + (@rule ((~x) & + !(~x)) => false) + (@rule ((~x) | + !(~x)) => true) # reversed order of the above, because it matters and `@acrule` refuses to do its job - (@rule (true & (~x)) => (~x)) - (@rule (false & (~x)) => false) - (@rule (true | (~x)) => true) - (@rule (false | (~x)) => (~x)) - (@rule (!(~x) & (~x)) => false) - (@rule (!(~x) | (~x)) => true) + (@rule (true & + (~x)) => (~x)) + (@rule (false & + (~x)) => false) + (@rule (true | + (~x)) => true) + (@rule (false | + (~x)) => (~x)) + (@rule (!(~x) & + (~x)) => false) + (@rule (!(~x) | + (~x)) => true) # idempotent - (@rule ((~x) & (~x)) => (~x)) - (@rule ((~x) | (~x)) => (~x)) + (@rule ((~x) & + (~x)) => (~x)) + (@rule ((~x) | + (~x)) => (~x)) # ifelse with determined branches - (@rule ifelse((~x), true, false) => (~x)) - (@rule ifelse((~x), false, true) => !(~x)) + (@rule ifelse( + (~x), + true, + false) => (~x)) + (@rule ifelse( + (~x), + false, + true) => !(~x)) # ifelse with identical branches - (@rule ifelse((~x), (~y), (~y)) => (~y)) - (@rule ifelse((~x), (~y), !(~y)) => ((~x) & - (~y))) - (@rule ifelse((~x), !(~y), (~y)) => ((~x) & - !(~y))) + (@rule ifelse( + (~x), + (~y), + (~y)) => (~y)) + (@rule ifelse( + (~x), + (~y), + !(~y)) => ((~x) & + (~y))) + (@rule ifelse( + (~x), + !(~y), + (~y)) => ((~x) & + !(~y))) # ifelse with determined condition - (@rule ifelse(true, (~x), (~y)) => (~x)) - (@rule ifelse(false, (~x), (~y)) => (~y))]))) + (@rule ifelse( + true, + (~x), + (~y)) => (~x)) + (@rule ifelse( + false, + (~x), + (~y)) => (~y))]))) """ If lifting converts (nested) if statements into a series of continuous events + a logically equivalent if statement + parameters. @@ -499,7 +534,8 @@ function IfLifting(sys::System) for var in new_cond_vars condition = generate_condition(cw, var) - up_affect, down_affect = generate_affects( + up_affect, + down_affect = generate_affects( cw, var, new_cond_vars, new_cond_vars_graph) cb = SymbolicContinuousCallback([condition], up_affect; affect_neg = down_affect, initialize = up_affect, rootfind = SciMLBase.RightRootFind) diff --git a/src/systems/index_cache.jl b/src/systems/index_cache.jl index 2ce1c7cffa..e006da3cb5 100644 --- a/src/systems/index_cache.jl +++ b/src/systems/index_cache.jl @@ -246,9 +246,11 @@ function IndexCache(sys::AbstractSystem) return idxs, buffer_sizes end - const_idxs, const_buffer_sizes = get_buffer_sizes_and_idxs( + const_idxs, + const_buffer_sizes = get_buffer_sizes_and_idxs( ParamIndexMap, constant_buffers) - nonnumeric_idxs, nonnumeric_buffer_sizes = get_buffer_sizes_and_idxs( + nonnumeric_idxs, + nonnumeric_buffer_sizes = get_buffer_sizes_and_idxs( NonnumericMap, nonnumeric_buffers) tunable_idxs = TunableIndexMap() diff --git a/src/systems/nonlinear/homotopy_continuation.jl b/src/systems/nonlinear/homotopy_continuation.jl index 77e39e4839..96c00411ad 100644 --- a/src/systems/nonlinear/homotopy_continuation.jl +++ b/src/systems/nonlinear/homotopy_continuation.jl @@ -543,7 +543,8 @@ function HomotopyContinuationProblem{iip, spec}( if !iscomplete(sys) error("A completed `System` is required. Call `complete` or `mtkcompile` on the system before creating a `HomotopyContinuationProblem`") end - f, u0, p = process_SciMLProblem( + f, u0, + p = process_SciMLProblem( HomotopyNonlinearFunction{iip, spec}, sys, op; kwargs...) kwargs = filter_kwargs(kwargs) diff --git a/src/systems/nonlinear/initializesystem.jl b/src/systems/nonlinear/initializesystem.jl index bc7f9c6df4..e5cdcac53d 100644 --- a/src/systems/nonlinear/initializesystem.jl +++ b/src/systems/nonlinear/initializesystem.jl @@ -568,7 +568,8 @@ function SciMLBase.remake_initialization_data( # is called is because `Initial` parameters are calculated from the corresponding # state values. history_fn = is_time_dependent(sys) && !is_markovian(sys) ? Returns(newu0) : nothing - new_initu0, new_initp = reconstruct_fn( + new_initu0, + new_initp = reconstruct_fn( ProblemState(; u = newu0, p = newp, t = t0, h = history_fn), oldinitprob) if oldinitprob.f.resid_prototype === nothing newf = oldinitprob.f @@ -645,7 +646,8 @@ function SciMLBase.remake_initialization_data( u0map = anydict() pmap = anydict() - missing_unknowns, missing_pars = build_operating_point!(sys, op, + missing_unknowns, + missing_pars = build_operating_point!(sys, op, u0map, pmap, defs, dvs, ps) floatT = float_type_from_varmap(op) u0_constructor = p_constructor = identity @@ -660,7 +662,13 @@ function SciMLBase.remake_initialization_data( kws = maybe_build_initialization_problem( sys, SciMLBase.isinplace(odefn), op, t0, defs, guesses, missing_unknowns; time_dependent_init, use_scc, initialization_eqs, floatT, +<<<<<<< Updated upstream u0_constructor, p_constructor, allow_incomplete = true) +||||||| Stash base + u0_constructor, p_constructor, allow_incomplete = true, check_units=false) +======= + u0_constructor, p_constructor, allow_incomplete = true, check_units = false) +>>>>>>> Stashed changes odefn = remake(odefn; kws...) return SciMLBase.remake_initialization_data(sys, odefn, newu0, t0, newp, newu0, newp) diff --git a/src/systems/optimal_control_interface.jl b/src/systems/optimal_control_interface.jl index 7651c9a235..5a0ddbf8d5 100644 --- a/src/systems/optimal_control_interface.jl +++ b/src/systems/optimal_control_interface.jl @@ -93,7 +93,7 @@ function PyomoCollocation end function warn_overdetermined(sys, op) cstrs = constraints(sys) - init_conds = filter(x -> value(x) ∈ Set(unknowns(sys)), [k for (k,v) in op]) + init_conds = filter(x -> value(x) ∈ Set(unknowns(sys)), [k for (k, v) in op]) if !isempty(cstrs) (length(cstrs) + length(init_conds) > length(unknowns(sys))) && @warn "The control problem is overdetermined. The total number of conditions (# constraints + # fixed initial values given by op) exceeds the total number of states. The solvers will default to doing a nonlinear least-squares optimization." @@ -135,7 +135,8 @@ is_explicit(tableau) = tableau isa DiffEqBase.ExplicitRKTableau initialization_data = nothing, cse = true, kwargs...) where {iip, specialize} - f, _, _ = generate_control_function( + f, _, + _ = generate_control_function( sys, inputs, disturbance_inputs; eval_module, cse, kwargs...) f = f[1] @@ -227,11 +228,11 @@ end ########################## ### MODEL CONSTRUCTION ### ########################## -function process_DynamicOptProblem(prob_type::Type{<:AbstractDynamicOptProblem}, model_type, sys::System, op, tspan; - dt = nothing, - steps = nothing, - guesses = Dict(), kwargs...) - +function process_DynamicOptProblem( + prob_type::Type{<:AbstractDynamicOptProblem}, model_type, sys::System, op, tspan; + dt = nothing, + steps = nothing, + guesses = Dict(), kwargs...) warn_overdetermined(sys, op) ctrls = unbound_inputs(sys) states = unknowns(sys) @@ -239,10 +240,11 @@ function process_DynamicOptProblem(prob_type::Type{<:AbstractDynamicOptProblem}, stidxmap = Dict([v => i for (i, v) in enumerate(states)]) op = Dict([default_toterm(value(k)) => v for (k, v) in op]) u0_idxs = has_alg_eqs(sys) ? collect(1:length(states)) : - [stidxmap[default_toterm(k)] for (k, v) in op if haskey(stidxmap, k)] + [stidxmap[default_toterm(k)] for (k, v) in op if haskey(stidxmap, k)] _op = has_alg_eqs(sys) ? op : merge(Dict(op), Dict(guesses)) - f, u0, p = process_SciMLProblem(ODEInputFunction, sys, _op; + f, u0, + p = process_SciMLProblem(ODEInputFunction, sys, _op; t = tspan !== nothing ? tspan[1] : tspan, kwargs...) model_tspan, steps, is_free_t = process_tspan(tspan, dt, steps) warn_overdetermined(sys, op) @@ -302,7 +304,7 @@ function set_variable_bounds!(m, sys, pmap, tf) end end -is_free_final(model) = model.is_free_final +is_free_final(model) = model.is_free_final function add_cost_function!(model, sys, tspan, pmap) jcosts = cost(sys) @@ -335,14 +337,15 @@ function substitute_integral(model, expr, tspan) Symbolics.substitute(expr, intmap) end -function process_integral_bounds(model, integral_span, tspan) +function process_integral_bounds(model, integral_span, tspan) if is_free_final(model) && isequal(integral_span, tspan) integral_span = (0, 1) elseif is_free_final(model) error("Free final time problems cannot handle partial timespans.") else (lo, hi) = integral_span - (lo < tspan[1] || hi > tspan[2]) && error("Integral bounds are beyond the timespan.") + (lo < tspan[1] || hi > tspan[2]) && + error("Integral bounds are beyond the timespan.") integral_span end end @@ -353,12 +356,14 @@ function substitute_model_vars(model, sys, exprs, tspan) c_ops = [operation(unwrap(ct)) for ct in unbound_inputs(sys)] t = get_iv(sys) - exprs = map(c -> Symbolics.fast_substitute(c, whole_t_map(model, t, x_ops, c_ops)), exprs) + exprs = map( + c -> Symbolics.fast_substitute(c, whole_t_map(model, t, x_ops, c_ops)), exprs) (ti, tf) = tspan if symbolic_type(tf) === ScalarSymbolic() _tf = model.tₛ + ti - exprs = map(c -> Symbolics.fast_substitute(c, free_t_map(model, tf, x_ops, c_ops)), exprs) + exprs = map( + c -> Symbolics.fast_substitute(c, free_t_map(model, tf, x_ops, c_ops)), exprs) exprs = map(c -> Symbolics.fast_substitute(c, Dict(tf => _tf)), exprs) end exprs = map(c -> Symbolics.fast_substitute(c, fixed_t_map(model, x_ops, c_ops)), exprs) @@ -392,7 +397,9 @@ function fixed_t_map end function add_user_constraints!(model, sys, tspan, pmap) jconstraints = get_constraints(sys) (isnothing(jconstraints) || isempty(jconstraints)) && return nothing - cons_dvs, cons_ps = process_constraint_system(jconstraints, Set(unknowns(sys)), parameters(sys), get_iv(sys); validate = false) + cons_dvs, + cons_ps = process_constraint_system( + jconstraints, Set(unknowns(sys)), parameters(sys), get_iv(sys); validate = false) is_free_final(model) && check_constraint_vars(cons_dvs) @@ -421,12 +428,13 @@ function add_equational_constraints!(model, sys, pmap, tspan) end function set_objective! end -objective_value(sol::DynamicOptSolution) = objective_value(sol.model) +objective_value(sol::DynamicOptSolution) = objective_value(sol.model) function substitute_differentials(model, sys, eqs) t = get_iv(sys) D = Differential(t) - diffsubmap = Dict([D(lowered_var(model, :U, i, t)) => lowered_derivative(model, i) for i in 1:length(unknowns(sys))]) + diffsubmap = Dict([D(lowered_var(model, :U, i, t)) => lowered_derivative(model, i) + for i in 1:length(unknowns(sys))]) eqs = map(c -> Symbolics.substitute(c, diffsubmap), eqs) end @@ -466,9 +474,10 @@ function successful_solve end - kwargs are used for other options. For example, the `plugin_options` and `solver_options` will propagated to the Opti object in CasADi. """ -function DiffEqBase.solve(prob::AbstractDynamicOptProblem, solver::AbstractCollocation; verbose = false, kwargs...) +function DiffEqBase.solve(prob::AbstractDynamicOptProblem, + solver::AbstractCollocation; verbose = false, kwargs...) solved_model = prepare_and_optimize!(prob, solver; verbose, kwargs...) - + ts = get_t_values(solved_model) Us = get_U_values(solved_model) Vs = get_V_values(solved_model) diff --git a/src/systems/parameter_buffer.jl b/src/systems/parameter_buffer.jl index 226b629680..637ed674ae 100644 --- a/src/systems/parameter_buffer.jl +++ b/src/systems/parameter_buffer.jl @@ -316,9 +316,9 @@ function SciMLStructures.replace!(::SciMLStructures.Initials, p::MTKParameters, end for (Portion, field, recurse) in [(SciMLStructures.Discrete, :discrete, 1) - (SciMLStructures.Constants, :constant, 1) - (Nonnumeric, :nonnumeric, 1) - (SciMLStructures.Caches, :caches, 1)] + (SciMLStructures.Constants, :constant, 1) + (Nonnumeric, :nonnumeric, 1) + (SciMLStructures.Caches, :caches, 1)] @eval function SciMLStructures.canonicalize(::$Portion, p::MTKParameters) as_vector = buffer_to_arraypartition(p.$field) repack = let p = p @@ -620,14 +620,20 @@ end initials = $similar(oldbuf.initials, $initialsT) copyto!(initials, oldbuf.initials) discretes = $(Expr(:tuple, - (:($similar(oldbuf.discrete[$i], $(discretesT[i]))) for i in 1:length(discretesT))...)) - $((:($copyto!(discretes[$i], oldbuf.discrete[$i])) for i in 1:length(discretesT))...) + (:($similar(oldbuf.discrete[$i], $(discretesT[i]))) + for i in 1:length(discretesT))...)) + $((:($copyto!(discretes[$i], oldbuf.discrete[$i])) + for i in 1:length(discretesT))...) constants = $(Expr(:tuple, - (:($similar(oldbuf.constant[$i], $(constantsT[i]))) for i in 1:length(constantsT))...)) - $((:($copyto!(constants[$i], oldbuf.constant[$i])) for i in 1:length(constantsT))...) + (:($similar(oldbuf.constant[$i], $(constantsT[i]))) + for i in 1:length(constantsT))...)) + $((:($copyto!(constants[$i], oldbuf.constant[$i])) + for i in 1:length(constantsT))...) nonnumerics = $(Expr(:tuple, - (:($similar(oldbuf.nonnumeric[$i], $(nonnumericT[i]))) for i in 1:length(nonnumericT))...)) - $((:($copyto!(nonnumerics[$i], oldbuf.nonnumeric[$i])) for i in 1:length(nonnumericT))...) + (:($similar(oldbuf.nonnumeric[$i], $(nonnumericT[i]))) + for i in 1:length(nonnumericT))...)) + $((:($copyto!(nonnumerics[$i], oldbuf.nonnumeric[$i])) + for i in 1:length(nonnumericT))...) caches = copy.(oldbuf.caches) newbuf = MTKParameters( tunables, initials, discretes, constants, nonnumerics, caches) @@ -646,13 +652,16 @@ end push!(expr.args, :(initials = $similar_type($I, $initialsT)(initials))) push!(expr.args, :(discretes = $(Expr(:tuple, - (:($similar_type($(fieldtype(D, i)), $(discretesT[i]))(discretes[$i])) for i in 1:length(discretesT))...)))) + (:($similar_type($(fieldtype(D, i)), $(discretesT[i]))(discretes[$i])) + for i in 1:length(discretesT))...)))) push!(expr.args, :(constants = $(Expr(:tuple, - (:($similar_type($(fieldtype(C, i)), $(constantsT[i]))(constants[$i])) for i in 1:length(constantsT))...)))) + (:($similar_type($(fieldtype(C, i)), $(constantsT[i]))(constants[$i])) + for i in 1:length(constantsT))...)))) push!(expr.args, :(nonnumerics = $(Expr(:tuple, - (:($similar_type($(fieldtype(C, i)), $(nonnumericT[i]))(nonnumerics[$i])) for i in 1:length(nonnumericT))...)))) + (:($similar_type($(fieldtype(C, i)), $(nonnumericT[i]))(nonnumerics[$i])) + for i in 1:length(nonnumericT))...)))) push!(expr.args, :(newbuf = MTKParameters( tunables, initials, discretes, constants, nonnumerics, caches))) diff --git a/src/systems/problem_utils.jl b/src/systems/problem_utils.jl index ecba542fd0..2f42dbb761 100644 --- a/src/systems/problem_utils.jl +++ b/src/systems/problem_utils.jl @@ -1183,8 +1183,8 @@ function maybe_build_initialization_problem( return (; initialization_data = SciMLBase.OverrideInitData( - initializeprob, update_initializeprob!, initializeprobmap, - initializeprobpmap; metadata = meta, is_update_oop = Val(true))) + initializeprob, update_initializeprob!, initializeprobmap, + initializeprobpmap; metadata = meta, is_update_oop = Val(true))) end """ @@ -1335,7 +1335,8 @@ function process_SciMLProblem( u0map = anydict() pmap = anydict() - missing_unknowns, missing_pars = build_operating_point!(sys, op, + missing_unknowns, + missing_pars = build_operating_point!(sys, op, u0map, pmap, defs, dvs, ps) floatT = calculate_float_type(op, u0Type) @@ -1440,7 +1441,7 @@ function process_SciMLProblem( kwargs = merge(kwargs, (; resid_prototype = u0_constructor(calculate_resid_prototype( - length(eqs), u0, p)))) + length(eqs), u0, p)))) end f = constructor(sys; u0 = u0, p = p, @@ -1539,7 +1540,8 @@ function SymbolicTstops( end end rps = reorder_parameters(sys) - tstops, _ = build_function_wrapper(sys, tstops, + tstops, + _ = build_function_wrapper(sys, tstops, rps..., t0, t1; diff --git a/src/systems/system.jl b/src/systems/system.jl index 2d38ab2dce..a43e4f5c65 100644 --- a/src/systems/system.jl +++ b/src/systems/system.jl @@ -387,7 +387,8 @@ function System(eqs::Vector{Equation}, iv, dvs, ps, brownians = []; if length(unique_sysnames) != length(sysnames) throw(NonUniqueSubsystemsError(sysnames, unique_sysnames)) end - continuous_events, discrete_events = create_symbolic_events( + continuous_events, + discrete_events = create_symbolic_events( continuous_events, discrete_events, eqs, iv) if iv === nothing && (!isempty(continuous_events) || !isempty(discrete_events)) @@ -1081,16 +1082,17 @@ struct EventsInTimeIndependentSystemError <: Exception end function Base.showerror(io::IO, err::EventsInTimeIndependentSystemError) - println(io, """ - Events are not supported in time-independent systems. Provide an independent variable to \ - make the system time-dependent or remove the events. + println( + io, """ +Events are not supported in time-independent systems. Provide an independent variable to \ +make the system time-dependent or remove the events. - The following continuous events were provided: - $(err.cevents) +The following continuous events were provided: +$(err.cevents) - The following discrete events were provided: - $(err.devents) - """) +The following discrete events were provided: +$(err.devents) +""") end function supports_initialization(sys::System) diff --git a/src/systems/systems.jl b/src/systems/systems.jl index ff455fb811..891714dce6 100644 --- a/src/systems/systems.jl +++ b/src/systems/systems.jl @@ -109,6 +109,7 @@ function __mtkcompile(sys::AbstractSystem; simplify = false, dvar2eq[fullvars[dv]] = only(deqs) end for (j, bj) in enumerate(brown_vars), i in 𝑑neighbors(graph, bj) + push!(Is, i) push!(Js, j) eq = new_eqs[i] diff --git a/src/systems/systemstructure.jl b/src/systems/systemstructure.jl index 5dfd36a6fc..4fdb96c789 100644 --- a/src/systems/systemstructure.jl +++ b/src/systems/systemstructure.jl @@ -19,7 +19,8 @@ using SparseArrays function quick_cancel_expr(expr) Rewriters.Postwalk(quick_cancel, - similarterm = (x, f, args; kws...) -> maketerm(typeof(x), f, args, + similarterm = (x, f, args; + kws...) -> maketerm(typeof(x), f, args, SymbolicUtils.metadata(x), kws...))(expr) end @@ -271,8 +272,8 @@ end function symbolic_contains(var, set) var in set || symbolic_type(var) == ArraySymbolic() && - Symbolics.shape(var) != Symbolics.Unknown() && - all(x -> x in set, Symbolics.scalarize(var)) + Symbolics.shape(var) != Symbolics.Unknown() && + all(x -> x in set, Symbolics.scalarize(var)) end function TearingState(sys; quick_cancel = false, check = true, sort_eqs = true) @@ -372,7 +373,8 @@ function TearingState(sys; quick_cancel = false, check = true, sort_eqs = true) isdelay(v, iv) && continue if !symbolic_contains(v, dvs) - isvalid = iscall(v) && (operation(v) isa Shift || is_transparent_operator(operation(v))) + isvalid = iscall(v) && + (operation(v) isa Shift || is_transparent_operator(operation(v))) v′ = v while !isvalid && iscall(v′) && operation(v′) isa Union{Differential, Shift} v′ = arguments(v′)[1] @@ -516,6 +518,7 @@ function TearingState(sys; quick_cancel = false, check = true, sort_eqs = true) # build incidence graph graph = BipartiteGraph(neqs, nvars, Val(false)) for (ie, vars) in enumerate(symbolic_incidence), v in vars + jv = var2idx[v] add_edge!(graph, ie, jv) end diff --git a/src/variables.jl b/src/variables.jl index 38faf6dbda..a4f0b5532f 100644 --- a/src/variables.jl +++ b/src/variables.jl @@ -33,7 +33,8 @@ ModelingToolkit.dump_variable_metadata(p) """ function dump_variable_metadata(var) uvar = unwrap(var) - variable_source, name = Symbolics.getmetadata( + variable_source, + name = Symbolics.getmetadata( uvar, VariableSource, (:unknown, :unknown)) type = symtype(uvar) if type <: AbstractArray diff --git a/test/analysis_points.jl b/test/analysis_points.jl index 505e5ca813..008be3a615 100644 --- a/test/analysis_points.jl +++ b/test/analysis_points.jl @@ -476,7 +476,8 @@ end connect(F.output, sys_inner.add.input1)] sys_outer = System(eqs, t, systems = [F, sys_inner, r], name = :outer) - matrices, _ = get_sensitivity( + matrices, + _ = get_sensitivity( sys_outer, [sys_outer.inner.plant_input, sys_outer.inner.plant_output]) Ps = tf(1, [1, 1]) |> ss @@ -491,7 +492,8 @@ end @test tf(G[1, 2]) ≈ tf(-CS.feedback(Cs, Ps)) @test tf(G[2, 1]) ≈ tf(CS.feedback(Ps, Cs)) - matrices, _ = get_comp_sensitivity( + matrices, + _ = get_comp_sensitivity( sys_outer, [sys_outer.inner.plant_input, sys_outer.inner.plant_output]) G = CS.ss(matrices...) |> sminreal @@ -513,7 +515,8 @@ end @test tf(L[1, 1]) ≈ -tf(Ps * Cs) # Calling looptransfer like below is not the intended way, but we can work out what it should return if we did so it remains a valid test - matrices, _ = get_looptransfer( + matrices, + _ = get_looptransfer( sys_outer, [sys_outer.inner.plant_input, sys_outer.inner.plant_output]) L = CS.ss(matrices...) |> sminreal @test tf(L[1, 1]) ≈ tf(0) @@ -521,7 +524,8 @@ end @test sminreal(L[1, 2]) ≈ ss(-1) @test tf(L[2, 1]) ≈ tf(Ps) - matrices, _ = linearize( + matrices, + _ = linearize( sys_outer, [sys_outer.inner.plant_input], [nameof(sys_inner.plant_output)]) G = CS.ss(matrices...) |> sminreal @test tf(G) ≈ tf(CS.feedback(Ps, Cs)) diff --git a/test/basic_transformations.jl b/test/basic_transformations.jl index 6880f77479..bafb5cf9e2 100644 --- a/test/basic_transformations.jl +++ b/test/basic_transformations.jl @@ -127,7 +127,7 @@ end p = [v => 10.0] prob = ODEProblem(Mx, [u0; p], (0.0, 20.0)) # 1 = dy/dx = (dy/dt)/(dx/dt) means equal initial horizontal and vertical velocities sol = solve(prob, Tsit5(); reltol = 1e-5) - @test all(isapprox.(sol[Mx.y], sol[Mx.x - g * (Mx.t)^2 / 2]; atol = 1e-10)) # compare to analytical solution (x(t) = v*t, y(t) = v*t - g*t^2/2) + @test all(isapprox.(sol[Mx.y], sol[Mx.x - g * (Mx.t) ^ 2 / 2]; atol = 1e-10)) # compare to analytical solution (x(t) = v*t, y(t) = v*t - g*t^2/2) end @testset "Change independent variable (free fall with 2nd order horizontal equation)" begin @@ -140,7 +140,7 @@ end u0 = [Mx.y => 0.0, Dx(Mx.y) => 1.0, Mx.t => 0.0, Mx.xˍt => 10.0] prob = ODEProblem(Mx, u0, (0.0, 20.0)) # 1 = dy/dx = (dy/dt)/(dx/dt) means equal initial horizontal and vertical velocities sol = solve(prob, Tsit5(); reltol = 1e-5) - @test all(isapprox.(sol[Mx.y], sol[Mx.x - g * (Mx.t)^2 / 2]; atol = 1e-10)) # compare to analytical solution (x(t) = v*t, y(t) = v*t - g*t^2/2) + @test all(isapprox.(sol[Mx.y], sol[Mx.x - g * (Mx.t) ^ 2 / 2]; atol = 1e-10)) # compare to analytical solution (x(t) = v*t, y(t) = v*t - g*t^2/2) end @testset "Change independent variable (crazy 3rd order nonlinear system)" begin @@ -158,8 +158,9 @@ end # Compare to pen-and-paper result @variables x xˍt(x) xˍt(x) y(x) t(x) Dx = Differential(x) - areequivalent(eq1, eq2) = isequal(expand(eq1.lhs - eq2.lhs), 0) && - isequal(expand(eq1.rhs - eq2.rhs), 0) + areequivalent(eq1, + eq2) = isequal(expand(eq1.lhs - eq2.lhs), 0) && + isequal(expand(eq1.rhs - eq2.rhs), 0) eq1lhs = xˍt^3 * (Dx^3)(y) + xˍt^2 * Dx(y) * (Dx^2)(xˍt) + xˍt * Dx(y) * (Dx(xˍt))^2 + 3 * xˍt^2 * (Dx^2)(y) * Dx(xˍt) @@ -194,8 +195,9 @@ end @test Set(equations(M2)) == Set([ t ~ √(x), xˍt ~ 2t, - xˍt * Dx(y) ~ 1fc(t) + 2fc(x) + 3fc(y) + - 1callme(f, t) + 2callme(f, x) + 3callme(f, y) + xˍt * Dx(y) ~ + 1fc(t) + 2fc(x) + 3fc(y) + + 1callme(f, t) + 2callme(f, x) + 3callme(f, y) ]) _f = LinearInterpolation([1.0, 1.0], [-100.0, +100.0]) # constant value 1 @@ -230,7 +232,7 @@ end prob = ODEProblem(Mx, u0, (0.0, 20.0)) # 1 = dy/dx = (dy/dt)/(dx/dt) means equal initial horizontal and vertical velocities sol = solve(prob, Tsit5(); reltol = 1e-5) # compare to analytical solution (x(t) = v*t, y(t) = v*t - g*t^2/2) - @test all(isapprox.(sol[Mx.y], sol[Mx.x - g * (Mx.t_units)^2 / 2]; atol = 1e-10)) + @test all(isapprox.(sol[Mx.y], sol[Mx.x - g * (Mx.t_units) ^ 2 / 2]; atol = 1e-10)) end @testset "Change independent variable, no equations" begin diff --git a/test/changeofvariables.jl b/test/changeofvariables.jl index abc773ed21..1dccfaec28 100644 --- a/test/changeofvariables.jl +++ b/test/changeofvariables.jl @@ -1,7 +1,6 @@ using ModelingToolkit, OrdinaryDiffEq, StochasticDiffEq using Test, LinearAlgebra - # Change of variables: z = log(x) # (this implies that x = exp(z) is automatically non-negative) @independent_variables t @@ -16,15 +15,15 @@ eqs = [D(D(z)) ~ ones(2, 2)] D = Differential(t) eqs = [D(x) ~ α*x] -tspan = (0., 1.) +tspan = (0.0, 1.0) def = [x => 1.0, α => -0.5] -@mtkcompile sys = System(eqs, t;defaults=def) +@mtkcompile sys = System(eqs, t; defaults = def) prob = ODEProblem(sys, [], tspan) sol = solve(prob, Tsit5()) @variables z(t) -forward_subs = [log(x) => z] +forward_subs = [log(x) => z] backward_subs = [x => exp(z)] new_sys = change_of_variables(sys, t, forward_subs, backward_subs) @test equations(new_sys)[1] == (D(z) ~ α) @@ -32,50 +31,48 @@ new_sys = change_of_variables(sys, t, forward_subs, backward_subs) new_prob = ODEProblem(new_sys, [], tspan) new_sol = solve(new_prob, Tsit5()) -@test isapprox(new_sol[x][end], sol[x][end], atol=1e-4) - - +@test isapprox(new_sol[x][end], sol[x][end], atol = 1e-4) # Riccati equation @parameters α @variables x(t) D = Differential(t) eqs = [D(x) ~ t^2 + α - x^2] -def = [x=>1., α => 1.] -@mtkcompile sys = System(eqs, t; defaults=def) +def = [x=>1.0, α => 1.0] +@mtkcompile sys = System(eqs, t; defaults = def) @variables z(t) -forward_subs = [t + α/(x+t) => z ] -backward_subs = [ x => α/(z-t) - t] +forward_subs = [t + α/(x+t) => z] +backward_subs = [x => α/(z-t) - t] -new_sys = change_of_variables(sys, t, forward_subs, backward_subs; simplify=true, t0=0.) +new_sys = change_of_variables( + sys, t, forward_subs, backward_subs; simplify = true, t0 = 0.0) # output should be equivalent to # t^2 + α - z^2 + 2 (but this simplification is not found automatically) -tspan = (0., 1.) -prob = ODEProblem(sys,[],tspan) -new_prob = ODEProblem(new_sys,[],tspan) +tspan = (0.0, 1.0) +prob = ODEProblem(sys, [], tspan) +new_prob = ODEProblem(new_sys, [], tspan) sol = solve(prob, Tsit5()) new_sol = solve(new_prob, Tsit5()) -@test isapprox(sol[x][end], new_sol[x][end], rtol=1e-4) - +@test isapprox(sol[x][end], new_sol[x][end], rtol = 1e-4) # Linear transformation to diagonal system @independent_variables t @variables x(t)[1:3] x = reshape(x, 3, 1) D = Differential(t) -A = [0. -1. 0.; -0.5 0.5 0.; 0. 0. -1.] +A = [0.0 -1.0 0.0; -0.5 0.5 0.0; 0.0 0.0 -1.0] right = A*x eqs = vec(D.(x) .~ right) -tspan = (0., 10.) +tspan = (0.0, 10.0) u0 = [x[1] => 1.0, x[2] => 2.0, x[3] => -1.0] -@mtkcompile sys = System(eqs, t; defaults=u0) -prob = ODEProblem(sys,[],tspan) +@mtkcompile sys = System(eqs, t; defaults = u0) +prob = ODEProblem(sys, [], tspan) sol = solve(prob, Tsit5()) T = eigen(A).vectors @@ -83,10 +80,10 @@ T_inv = inv(T) @variables z(t)[1:3] z = reshape(z, 3, 1) -forward_subs = vec(T_inv*x .=> z) +forward_subs = vec(T_inv*x .=> z) backward_subs = vec(x .=> T*z) -new_sys = change_of_variables(sys, t, forward_subs, backward_subs; simplify=true) +new_sys = change_of_variables(sys, t, forward_subs, backward_subs; simplify = true) new_prob = ODEProblem(new_sys, [], tspan) new_sol = solve(new_prob, Tsit5()) @@ -95,10 +92,10 @@ new_sol = solve(new_prob, Tsit5()) new_rhs = [eq.rhs for eq in equations(new_sys)] new_A = Symbolics.value.(Symbolics.jacobian(new_rhs, z)) A = diagm(eigen(A).values) -A = sortslices(A, dims=1) -new_A = sortslices(new_A, dims=1) +A = sortslices(A, dims = 1) +new_A = sortslices(new_A, dims = 1) @test isapprox(A, new_A, rtol = 1e-10) -@test isapprox( new_sol[x[1],end], sol[x[1],end], rtol=1e-4) +@test isapprox(new_sol[x[1], end], sol[x[1], end], rtol = 1e-4) # Change of variables for sde noise_eqs = ModelingToolkit.get_noise_eqs @@ -111,8 +108,8 @@ value = ModelingToolkit.value D = Differential(t) eqs = [D(x) ~ μ*x + σ*x*B] -def = [x=>0., μ => 2., σ=>1.] -@mtkcompile sys = System(eqs, t; defaults=def) +def = [x=>0.0, μ => 2.0, σ=>1.0] +@mtkcompile sys = System(eqs, t; defaults = def) forward_subs = [log(x) => y] backward_subs = [x => exp(y)] new_sys = change_of_variables(sys, t, forward_subs, backward_subs) @@ -126,25 +123,26 @@ new_sys = change_of_variables(sys, t, forward_subs, backward_subs) @variables x(t) y(t) z(t) w(t) u(t) v(t) D = Differential(t) eqs = [D(x) ~ μ*x + σ*x*Bx, D(y) ~ α*By, D(u) ~ μ*u + σ*u*Bx + α*u*By] -def = [x=>0., y=> 0., u=>0., μ => 2., σ=>1., α=>3.] +def = [x=>0.0, y => 0.0, u=>0.0, μ => 2.0, σ=>1.0, α=>3.0] forward_subs = [log(x) => z, y^2 => w, log(u) => v] -backward_subs = [x => exp(z), y => w^.5, u => exp(v)] +backward_subs = [x => exp(z), y => w^0.5, u => exp(v)] -@mtkcompile sys = System(eqs, t; defaults=def) +@mtkcompile sys = System(eqs, t; defaults = def) new_sys = change_of_variables(sys, t, forward_subs, backward_subs) @test equations(new_sys)[1] == (D(z) ~ μ - 1/2*σ^2) @test equations(new_sys)[2] == (D(w) ~ α^2) @test equations(new_sys)[3] == (D(v) ~ μ - 1/2*(α^2 + σ^2)) -@test noise_eqs(new_sys)[1,1] === value(σ) -@test noise_eqs(new_sys)[1,2] === value(0) -@test noise_eqs(new_sys)[2,1] === value(0) -@test noise_eqs(new_sys)[2,2] === value(substitute(2*α*y, backward_subs[2])) -@test noise_eqs(new_sys)[3,1] === value(σ) -@test noise_eqs(new_sys)[3,2] === value(α) +@test noise_eqs(new_sys)[1, 1] === value(σ) +@test noise_eqs(new_sys)[1, 2] === value(0) +@test noise_eqs(new_sys)[2, 1] === value(0) +@test noise_eqs(new_sys)[2, 2] === value(substitute(2*α*y, backward_subs[2])) +@test noise_eqs(new_sys)[3, 1] === value(σ) +@test noise_eqs(new_sys)[3, 2] === value(α) # Test for Brownian instead of noise -@named sys = System(eqs, t; defaults=def) -new_sys = change_of_variables(sys, t, forward_subs, backward_subs; simplify=false) +@named sys = System(eqs, t; defaults = def) +new_sys = change_of_variables(sys, t, forward_subs, backward_subs; simplify = false) @test simplify(equations(new_sys)[1]) == simplify((D(z) ~ μ - 1/2*σ^2 + σ*Bx)) -@test simplify(equations(new_sys)[2]) == simplify((D(w) ~ α^2 + 2*α*w^.5*By)) -@test simplify(equations(new_sys)[3]) == simplify((D(v) ~ μ - 1/2*(α^2 + σ^2) + σ*Bx + α*By)) \ No newline at end of file +@test simplify(equations(new_sys)[2]) == simplify((D(w) ~ α^2 + 2*α*w^0.5*By)) +@test simplify(equations(new_sys)[3]) == + simplify((D(v) ~ μ - 1/2*(α^2 + σ^2) + σ*Bx + α*By)) diff --git a/test/code_generation.jl b/test/code_generation.jl index 158ee64fbe..5fb1edb3c2 100644 --- a/test/code_generation.jl +++ b/test/code_generation.jl @@ -17,7 +17,8 @@ using ModelingToolkit: t_nounits as t, D_nounits as D sys, x + y[1] + p1 + p2[1] + p3 * t, [x], [p1, p2, p3]; expression = Val(false)) @test fn1(u0, p, 0.0) == 5.0 - fn3_oop, fn3_iip = generate_custom_function( + fn3_oop, + fn3_iip = generate_custom_function( sys, [x + y[2], y[3] + p2[2], p1 + p3, 3t]; expression = Val(false)) buffer = zeros(4) @@ -41,7 +42,8 @@ using ModelingToolkit: t_nounits as t, D_nounits as D sys, x + y[1] + p1 + p2[1] + p3, [x], [p1, p2, p3]; expression = Val(false)) @test fn1(u0, p) == 6.0 - fn3_oop, fn3_iip = generate_custom_function( + fn3_oop, + fn3_iip = generate_custom_function( sys, [x + y[2], y[3] + p2[2], p1 + p3]; expression = Val(false)) buffer = zeros(3) diff --git a/test/dde.jl b/test/dde.jl index df4acf377a..4af5f1ad31 100644 --- a/test/dde.jl +++ b/test/dde.jl @@ -35,8 +35,9 @@ sol2 = solve(prob2, alg, reltol = 1e-7, abstol = 1e-10) @variables x₀(t) x₁(t) x₂(..) tau = 1 eqs = [D(x₀) ~ (v0 / (1 + beta0 * (x₂(t - tau)^2))) * (p0 - q0) * x₀ - d0 * x₀ - D(x₁) ~ (v0 / (1 + beta0 * (x₂(t - tau)^2))) * (1 - p0 + q0) * x₀ + - (v1 / (1 + beta1 * (x₂(t - tau)^2))) * (p1 - q1) * x₁ - d1 * x₁ + D(x₁) ~ + (v0 / (1 + beta0 * (x₂(t - tau)^2))) * (1 - p0 + q0) * x₀ + + (v1 / (1 + beta1 * (x₂(t - tau)^2))) * (p1 - q1) * x₁ - d1 * x₁ D(x₂(t)) ~ (v1 / (1 + beta1 * (x₂(t - tau)^2))) * (1 - p1 + q1) * x₁ - d2 * x₂(t)] @mtkcompile sys = System(eqs, t) @test ModelingToolkit.is_dde(sys) @@ -174,7 +175,7 @@ end @testset "Issue#3165 DDEs with non-tunables" begin @variables x(..) = 1.0 - @parameters w=1.0 [tunable = false] τ=0.5 + @parameters w=1.0 [tunable=false] τ=0.5 eqs = [D(x(t)) ~ -w * x(t - τ)] @named sys = System(eqs, t) diff --git a/test/dep_graphs.jl b/test/dep_graphs.jl index 04e6bf159a..1fa166f1b7 100644 --- a/test/dep_graphs.jl +++ b/test/dep_graphs.jl @@ -70,21 +70,21 @@ import ModelingToolkit: value @testset "Case $i" for (i, test_case) in enumerate([test_case_1, test_case_2]) (; # filter out vrjs in making graphs - eqs, # eq to vars they depend on - eq_sdeps, - eq_sidepsf, - eq_sidepsb, # eq to params they depend on - eq_pdeps, - eq_pidepsf, - eq_pidepsb, # var to eqs that modify them - s_eqdepsf, - s_eqdepsb, - var_eq_ne, # eq to eqs that depend on them - eq_eqdeps, - eq_eq_ne, # var to vars that depend on them - var_vardeps, - var_var_ne -) = test_case + eqs, # eq to vars they depend on + eq_sdeps, + eq_sidepsf, + eq_sidepsb, # eq to params they depend on + eq_pdeps, + eq_pidepsf, + eq_pidepsb, # var to eqs that modify them + s_eqdepsf, + s_eqdepsb, + var_eq_ne, # eq to eqs that depend on them + eq_eqdeps, + eq_eq_ne, # var to vars that depend on them + var_vardeps, + var_var_ne + ) = test_case deps = equation_dependencies(js; eqs) @test length(deps) == length(eq_sdeps) @test all([issetequal(a, b) for (a, b) in zip(eq_sdeps, deps)]) diff --git a/test/direct.jl b/test/direct.jl index 70e1babe3f..ce2a3f5785 100644 --- a/test/direct.jl +++ b/test/direct.jl @@ -118,7 +118,8 @@ Jiip(J2, [1.0, 2.0, 3.0], [1.0, 2.0, 3.0], 1.0) s∂ = sparse(∂) @test nnz(s∂) == 8 -Joop, Jiip = eval.(ModelingToolkit.build_function(s∂, [x, y, z], [σ, ρ, β], t, +Joop, +Jiip = eval.(ModelingToolkit.build_function(s∂, [x, y, z], [σ, ρ, β], t, linenumbers = true)) J = Joop([1.0, 2.0, 3.0], [1.0, 2.0, 3.0], 1.0) @test length(nonzeros(s∂)) == 8 @@ -147,7 +148,8 @@ function test_worldage() eqs = [σ * (y - x), x * (ρ - z) - y, x * y - β * z] - f, f_iip = ModelingToolkit.build_function(eqs, [x, y, z], [σ, ρ, β]; + f, + f_iip = ModelingToolkit.build_function(eqs, [x, y, z], [σ, ρ, β]; expression = Val{false}) out = [1.0, 2, 3] o1 = f([1.0, 2, 3], [1.0, 2, 3]) diff --git a/test/downstream/linearization_dd.jl b/test/downstream/linearization_dd.jl index 3e94a02469..44659f03ff 100644 --- a/test/downstream/linearization_dd.jl +++ b/test/downstream/linearization_dd.jl @@ -43,9 +43,11 @@ ps = poles(G) @test minimum(abs, ps) < 1e-6 @test minimum(abs, complex(0, 1.3777260367206716) .- ps) < 1e-10 -lsys, syss = linearize(model, lin_inputs, lin_outputs, allow_symbolic = true, op = op, +lsys, +syss = linearize(model, lin_inputs, lin_outputs, allow_symbolic = true, op = op, allow_input_derivatives = true, zero_dummy_der = true) -lsyss, sysss = ModelingToolkit.linearize_symbolic(model, lin_inputs, lin_outputs; +lsyss, +sysss = ModelingToolkit.linearize_symbolic(model, lin_inputs, lin_outputs; allow_input_derivatives = true) dummyder = setdiff(unknowns(sysss), unknowns(model)) diff --git a/test/downstream/test_disturbance_model.jl b/test/downstream/test_disturbance_model.jl index c7b9d833d0..642ff85f99 100644 --- a/test/downstream/test_disturbance_model.jl +++ b/test/downstream/test_disturbance_model.jl @@ -149,10 +149,14 @@ sol = solve(prob, Tsit5()) ## Generate function for an augmented Unscented Kalman Filter ===================== # temp = open_loop(model_with_disturbance, :d) outputs = [P.inertia1.phi, P.inertia2.phi, P.inertia1.w, P.inertia2.w] -f, x_sym, p_sym, io_sys = ModelingToolkit.generate_control_function( +f, x_sym, +p_sym, +io_sys = ModelingToolkit.generate_control_function( model_with_disturbance, [:u], [:d1, :d2, :dy], split = false) -f, x_sym, p_sym, io_sys = ModelingToolkit.generate_control_function( +f, x_sym, +p_sym, +io_sys = ModelingToolkit.generate_control_function( model_with_disturbance, [:u], [:d1, :d2, :dy], disturbance_argument = true, split = false) diff --git a/test/dq_units.jl b/test/dq_units.jl index 615de1d642..ea1103db57 100644 --- a/test/dq_units.jl +++ b/test/dq_units.jl @@ -39,18 +39,18 @@ System(eqs, t, name = :sys, checks = false) # connection validation @connector function Pin(; name) - sts = @variables(v(t)=1.0, [unit = u"V"], - i(t)=1.0, [unit = u"A", connect = Flow]) + sts = @variables(v(t)=1.0, [unit=u"V"], + i(t)=1.0, [unit=u"A", connect=Flow]) System(Equation[], t, sts, []; name = name) end @connector function OtherPin(; name) - sts = @variables(v(t)=1.0, [unit = u"mV"], - i(t)=1.0, [unit = u"mA", connect = Flow]) + sts = @variables(v(t)=1.0, [unit=u"mV"], + i(t)=1.0, [unit=u"mA", connect=Flow]) System(Equation[], t, sts, []; name = name) end @connector function LongPin(; name) - sts = @variables(v(t)=1.0, [unit = u"V"], - i(t)=1.0, [unit = u"A", connect = Flow], + sts = @variables(v(t)=1.0, [unit=u"V"], + i(t)=1.0, [unit=u"A", connect=Flow], x(t)=1.0) System(Equation[], t, sts, []; name = name) end diff --git a/test/extensions/dynamic_optimization.jl b/test/extensions/dynamic_optimization.jl index 3884bc1aa9..f9c6a4277f 100644 --- a/test/extensions/dynamic_optimization.jl +++ b/test/extensions/dynamic_optimization.jl @@ -47,7 +47,7 @@ const M = ModelingToolkit @test ≈(csol2.sol.u, osol2.u, rtol = 0.001) pprob = PyomoDynamicOptProblem(sys, [u0map; parammap], tspan, dt = 0.01) psol = solve(pprob, PyomoCollocation("ipopt", BackwardEuler())) - @test all([≈(psol.sol(t), osol2(t), rtol = 1e-2) for t in 0.:0.01:1.]) + @test all([≈(psol.sol(t), osol2(t), rtol = 1e-2) for t in 0.0:0.01:1.0]) # With a constraint u0map = Pair[] @@ -55,7 +55,8 @@ const M = ModelingToolkit constr = [x(0.6) ~ 3.5, x(0.3) ~ 7.0] @mtkcompile lksys = System(eqs, t; constraints = constr) - jprob = JuMPDynamicOptProblem(lksys, [u0map; parammap], tspan; guesses = guess, dt = 0.01) + jprob = JuMPDynamicOptProblem( + lksys, [u0map; parammap], tspan; guesses = guess, dt = 0.01) jsol = solve(jprob, JuMPCollocation(Ipopt.Optimizer, constructTsitouras5())) @test jsol.sol(0.6; idxs = x(t)) ≈ 3.5 @test jsol.sol(0.3; idxs = x(t)) ≈ 7.0 @@ -74,7 +75,8 @@ const M = ModelingToolkit iprob = InfiniteOptDynamicOptProblem( lksys, [u0map; parammap], tspan; guesses = guess, dt = 0.01) - isol = solve(iprob, InfiniteOptCollocation(Ipopt.Optimizer, InfiniteOpt.OrthogonalCollocation(3))) # 48.564 ms, 9.58 MiB + isol = solve(iprob, + InfiniteOptCollocation(Ipopt.Optimizer, InfiniteOpt.OrthogonalCollocation(3))) # 48.564 ms, 9.58 MiB sol = isol.sol @test sol(0.6; idxs = x(t)) ≈ 3.5 @test sol(0.3; idxs = x(t)) ≈ 7.0 @@ -84,14 +86,17 @@ const M = ModelingToolkit @mtkcompile lksys = System(eqs, t; constraints = constr) iprob = InfiniteOptDynamicOptProblem( lksys, [u0map; parammap], tspan; guesses = guess, dt = 0.01) - isol = solve(iprob, InfiniteOptCollocation(Ipopt.Optimizer, InfiniteOpt.OrthogonalCollocation(3))) + isol = solve(iprob, + InfiniteOptCollocation(Ipopt.Optimizer, InfiniteOpt.OrthogonalCollocation(3))) @test all(u -> u > [1, 1], isol.sol.u) - jprob = JuMPDynamicOptProblem(lksys, [u0map; parammap], tspan; guesses = guess, dt = 0.01) + jprob = JuMPDynamicOptProblem( + lksys, [u0map; parammap], tspan; guesses = guess, dt = 0.01) jsol = solve(jprob, JuMPCollocation(Ipopt.Optimizer, constructRadauIA3())) @test all(u -> u > [1, 1], jsol.sol.u) - pprob = PyomoDynamicOptProblem(lksys, [u0map; parammap], tspan; guesses = guess, dt = 0.01) + pprob = PyomoDynamicOptProblem( + lksys, [u0map; parammap], tspan; guesses = guess, dt = 0.01) psol = solve(pprob, PyomoCollocation("ipopt", MidpointEuler())) @test all(u -> u > [1, 1], psol.sol.u) @@ -110,9 +115,9 @@ function is_bangbang(input_sol, lbounds, ubounds, rtol = 1e-4) end function ctrl_to_spline(inputsol, splineType) - us = reduce(vcat, inputsol.u) - ts = reduce(vcat, inputsol.t) - splineType(us, ts) + us = reduce(vcat, inputsol.u) + ts = reduce(vcat, inputsol.t) + splineType(us, ts) end @testset "Linear systems" begin @@ -131,7 +136,8 @@ end tspan = (0.0, 1.0) parammap = [u(t) => 0.0] jprob = JuMPDynamicOptProblem(block, [u0map; parammap], tspan; dt = 0.01) - jsol = solve(jprob, JuMPCollocation(Ipopt.Optimizer, constructVerner8()), verbose = true) + jsol = solve( + jprob, JuMPCollocation(Ipopt.Optimizer, constructVerner8()), verbose = true) # Linear systems have bang-bang controls @test is_bangbang(jsol.input_sol, [-1.0], [1.0]) # Test reached final position. @@ -156,7 +162,7 @@ end isol = solve(iprob, InfiniteOptCollocation(Ipopt.Optimizer)) @test is_bangbang(isol.input_sol, [-1.0], [1.0]) @test ≈(isol.sol[x(t)][end], 0.25, rtol = 1e-5) - + pprob = PyomoDynamicOptProblem(block, [u0map; parammap], tspan; dt = 0.01) psol = solve(pprob, PyomoCollocation("ipopt", BackwardEuler())) @test is_bangbang(psol.input_sol, [-1.0], [1.0]) @@ -165,7 +171,7 @@ end spline = ctrl_to_spline(isol.input_sol, ConstantInterpolation) oprob = ODEProblem(block_ode, [u0map; u_interp => spline], tspan) @test ≈(isol.sol.u, osol.u, rtol = 0.05) - @test all([≈(psol.sol(t), osol(t), rtol = 0.05) for t in 0.:0.01:1.]) + @test all([≈(psol.sol(t), osol(t), rtol = 0.05) for t in 0.0:0.01:1.0]) ################### ### Bee example ### @@ -210,7 +216,7 @@ end @test ≈(osol.u, csol.sol.u, rtol = 0.01) osol2 = solve(oprob, ImplicitEuler(); dt = 0.01, adaptive = false) @test ≈(osol2.u, isol.sol.u, rtol = 0.01) - @test all([≈(psol.sol(t), osol2(t), rtol = 0.01) for t in 0.:0.01:4.]) + @test all([≈(psol.sol(t), osol2(t), rtol = 0.01) for t in 0.0:0.01:4.0]) end @testset "Rocket launch" begin @@ -240,7 +246,8 @@ end jsol = solve(jprob, JuMPCollocation(Ipopt.Optimizer, constructRadauIIA5())) @test jsol.sol[h(t)][end] > 1.012 - cprob = CasADiDynamicOptProblem(rocket, [u0map; pmap], (ts, te); dt = 0.001, cse = false) + cprob = CasADiDynamicOptProblem( + rocket, [u0map; pmap], (ts, te); dt = 0.001, cse = false) csol = solve(cprob, CasADiCollocation("ipopt")) @test csol.sol[h(t)][end] > 1.012 @@ -378,7 +385,8 @@ end @test csol.sol.u[end] ≈ [π, 0, 0, 0] iprob = InfiniteOptDynamicOptProblem(cartpole, [u0map; pmap], tspan; dt = 0.04) - isol = solve(iprob, InfiniteOptCollocation(Ipopt.Optimizer, InfiniteOpt.OrthogonalCollocation(2))) + isol = solve(iprob, + InfiniteOptCollocation(Ipopt.Optimizer, InfiniteOpt.OrthogonalCollocation(2))) @test isol.sol.u[end] ≈ [π, 0, 0, 0] pprob = PyomoDynamicOptProblem(cartpole, [u0map; pmap], tspan; dt = 0.04) diff --git a/test/extensions/homotopy_continuation.jl b/test/extensions/homotopy_continuation.jl index a4af57c8fe..20ff262132 100644 --- a/test/extensions/homotopy_continuation.jl +++ b/test/extensions/homotopy_continuation.jl @@ -192,7 +192,7 @@ end disallowed_y = [7, 5, 4] @test all(!isapprox(sol[x]; atol = 1e-8), disallowed_x) @test all(!isapprox(sol[y]; atol = 1e-8), disallowed_y) - @test abs(sol[x^2 - 4x + y]) >= 1e-8 + @test abs(sol[x ^ 2 - 4x + y]) >= 1e-8 p = parameter_values(prob) for val in disallowed_x diff --git a/test/extensions/test_infiniteopt.jl b/test/extensions/test_infiniteopt.jl index e1e4143bb7..fab44d0cf9 100644 --- a/test/extensions/test_infiniteopt.jl +++ b/test/extensions/test_infiniteopt.jl @@ -53,13 +53,13 @@ m = InfiniteModel(optimizer_with_attributes(Ipopt.Optimizer, guess_xs = [t -> pi, t -> 0.1][permutation] guess_us = [t -> 0.1] InfiniteOpt.@variables(m, - begin - # state variables - (lb[i] <= x[i = 1:nx] <= ub[i], Infinite(τ), start = guess_xs[i]) # state variables - -10 <= u[i = 1:nu] <= 10, Infinite(τ), (start = guess_us[i]) # control variables - 0 <= tf <= 10, (start = 5) # Final time - 0.2 <= L <= 0.6, (start = 0.4) # Length parameter - end) +begin + # state variables + (lb[i] <= x[i = 1:nx] <= ub[i], Infinite(τ), start = guess_xs[i]) # state variables + -10 <= u[i = 1:nu] <= 10, Infinite(τ), (start = guess_us[i]) # control variables + 0 <= tf <= 10, (start = 5) # Final time + 0.2 <= L <= 0.6, (start = 0.4) # Length parameter +end) # Trace the dynamics x0 = ModelingToolkit.get_u0(io_sys, [model.θ => 0, model.ω => 0]) diff --git a/test/fmi/fmi.jl b/test/fmi/fmi.jl index 85e1830650..de5b8a1dac 100644 --- a/test/fmi/fmi.jl +++ b/test/fmi/fmi.jl @@ -177,7 +177,7 @@ end end function build_sspace_model(sspace) - @variables u(t)=1.0 x(t)=1.0 y(t) [guess = 1.0] + @variables u(t)=1.0 x(t)=1.0 y(t) [guess=1.0] @mtkcompile sys = System( [sspace.u ~ u, D(u) ~ t, D(x) ~ sspace.x + sspace.y, y^2 ~ sspace.y + sspace.x], t; systems = [sspace] diff --git a/test/initial_values.jl b/test/initial_values.jl index d4d768c0fe..5412d4d58a 100644 --- a/test/initial_values.jl +++ b/test/initial_values.jl @@ -7,7 +7,7 @@ using SymbolicIndexingInterface @variables x(t)[1:3]=[1.0, 2.0, 3.0] y(t) z(t)[1:2] -@mtkcompile sys=System([D(x) ~ t * x], t) simplify=false +@mtkcompile sys=System([D(x)~t*x], t) simplify=false reorderer = getsym(sys, x) @test reorderer(get_u0(sys, [])) == [1.0, 2.0, 3.0] @test reorderer(get_u0(sys, [x => [2.0, 3.0, 4.0]])) == [2.0, 3.0, 4.0] @@ -15,10 +15,10 @@ reorderer = getsym(sys, x) @test get_u0(sys, [2.0, 3.0, 4.0]) == [2.0, 3.0, 4.0] @mtkcompile sys=System([ - D(x) ~ 3x, - D(y) ~ t, - D(z[1]) ~ z[2] + t, - D(z[2]) ~ y + z[1] + D(x)~3x, + D(y)~t, + D(z[1])~z[2]+t, + D(z[2])~y+z[1] ], t) simplify=false @test_throws ModelingToolkit.MissingVariablesError get_u0(sys, []) @@ -119,7 +119,7 @@ end @testset "split=false systems with all parameter defaults" begin @variables x(t) = 1.0 @parameters p=1.0 q=2.0 r=3.0 - @mtkcompile sys=System(D(x) ~ p * x + q * t + r, t) split=false + @mtkcompile sys=System(D(x)~p*x+q*t+r, t) split=false prob = @test_nowarn ODEProblem(sys, [], (0.0, 1.0)) @test prob.p isa Vector{Float64} end @@ -254,7 +254,7 @@ end @testset "Array initials and scalar parameters with `split = false`" begin @variables x(t)[1:2] @parameters p - @mtkcompile sys=System([D(x[1]) ~ x[1], D(x[2]) ~ x[2] + p], t) split=false + @mtkcompile sys=System([D(x[1])~x[1], D(x[2])~x[2]+p], t) split=false ps = Set(parameters(sys; initial_parameters = true)) @test length(ps) == 5 for i in 1:2 diff --git a/test/initializationsystem.jl b/test/initializationsystem.jl index 3be3e400c3..ec34253fd5 100644 --- a/test/initializationsystem.jl +++ b/test/initializationsystem.jl @@ -461,7 +461,8 @@ prob = ODEProblem(simpsys, [z => 1.0, y => 1.0], tspan, guesses = [x => 2.0]) sol = solve(prob, Tsit5()) @test sol[[x, y], 1] == [0.0, 1.0] -@test_warn "underdetermined" prob = ODEProblem(simpsys, [], tspan, guesses = [x => 2.0, y => 1.0]) +@test_warn "underdetermined" prob = ODEProblem( + simpsys, [], tspan, guesses = [x => 2.0, y => 1.0]) # Late Binding initialization_eqs # https://github.com/SciML/ModelingToolkit.jl/issues/2787 @@ -599,7 +600,8 @@ end # the correct solver. # `rhss` allows adding terms to the end of equations (only 2 equations allowed) to influence # the system type (brownian vars to turn it into an SDE). - @testset "$Problem with $(SciMLBase.parameterless_type(alg)) and $ctor ctor" for ((Problem, alg, rhss), (ctor, expectedT)) in Iterators.product( + @testset "$Problem with $(SciMLBase.parameterless_type(alg)) and $ctor ctor" for ( + (Problem, alg, rhss), (ctor, expectedT)) in Iterators.product( [ (ODEProblem, Tsit5(), zeros(2)), (SDEProblem, ImplicitEM(), [a, b]), @@ -777,7 +779,7 @@ end @testset "No initialization for variables" begin @variables x=1.0 y=0.0 z=0.0 - @parameters σ=10.0 ρ=26.0 β=8 / 3 + @parameters σ=10.0 ρ=26.0 β=8/3 eqs = [0 ~ σ * (y - x), 0 ~ x * (ρ - z) - y, @@ -822,7 +824,7 @@ end @test SciMLBase.successful_retcode(sol) end - @parameters p=2.0 q=missing [guess = 1.0] c=1.0 + @parameters p=2.0 q=missing [guess=1.0] c=1.0 @variables x=1.0 z=3.0 # eqs = [0 ~ p * (y - x), @@ -883,7 +885,8 @@ end @brownians a b x = _x(t) - @testset "$Problem with $(SciMLBase.parameterless_type(typeof(alg)))" for (System, Problem, alg, rhss) in [ + @testset "$Problem with $(SciMLBase.parameterless_type(typeof(alg)))" for ( + System, Problem, alg, rhss) in [ (ModelingToolkit.System, ODEProblem, Tsit5(), zeros(2)), (ModelingToolkit.System, SDEProblem, ImplicitEM(), [a, b]), (ModelingToolkit.System, DDEProblem, MethodOfSteps(Tsit5()), [_x(t - 0.1), 0.0]), @@ -912,7 +915,8 @@ end @brownians a x = _x(t) - @testset "$Problem with $(SciMLBase.parameterless_type(typeof(alg)))" for (System, Problem, alg, rhss) in [ + @testset "$Problem with $(SciMLBase.parameterless_type(typeof(alg)))" for ( + System, Problem, alg, rhss) in [ (ModelingToolkit.System, ODEProblem, Tsit5(), 0), (ModelingToolkit.System, SDEProblem, ImplicitEM(), a), (ModelingToolkit.System, DDEProblem, MethodOfSteps(Tsit5()), _x(t - 0.1)), @@ -935,7 +939,8 @@ end @brownians a b x = _x(t) - @testset "$Problem with $(SciMLBase.parameterless_type(typeof(alg)))" for (Problem, alg, rhss) in [ + @testset "$Problem with $(SciMLBase.parameterless_type(typeof(alg)))" for ( + Problem, alg, rhss) in [ (ODEProblem, Tsit5(), zeros(2)), (SDEProblem, ImplicitEM(), [a, b]), (DDEProblem, MethodOfSteps(Tsit5()), [_x(t - 0.1), 0.0]), @@ -967,7 +972,8 @@ end @brownians a x = _x(t) - @testset "$Problem with $(SciMLBase.parameterless_type(typeof(alg)))" for (System, Problem, alg, rhss) in [ + @testset "$Problem with $(SciMLBase.parameterless_type(typeof(alg)))" for ( + System, Problem, alg, rhss) in [ (ModelingToolkit.System, ODEProblem, Tsit5(), 0), (ModelingToolkit.System, SDEProblem, ImplicitEM(), a), (ModelingToolkit.System, DDEProblem, MethodOfSteps(Tsit5()), _x(t - 0.1)), @@ -1024,7 +1030,8 @@ end @brownians a x = _x(t) - @testset "$Problem with $(SciMLBase.parameterless_type(typeof(alg)))" for (System, Problem, alg, rhss) in [ + @testset "$Problem with $(SciMLBase.parameterless_type(typeof(alg)))" for ( + System, Problem, alg, rhss) in [ (ModelingToolkit.System, ODEProblem, Tsit5(), 0), (ModelingToolkit.System, SDEProblem, ImplicitEM(), a), (ModelingToolkit.System, DDEProblem, MethodOfSteps(Tsit5()), _x(t - 0.1)), @@ -1105,6 +1112,18 @@ end guesses = ModelingToolkit.missing_variable_defaults(pend)) sol = solve(prob, Rodas5P()) @test SciMLBase.successful_retcode(sol) +<<<<<<< Updated upstream +||||||| Stash base + + prob2 = remake(prob, u0=[x => 0.5, y=>nothing]) + sol2 = solve(prob2, Rodas5P()) + @test SciMLBase.successful_retcode(sol2) +======= + + prob2 = remake(prob, u0 = [x => 0.5, y=>nothing]) + sol2 = solve(prob2, Rodas5P()) + @test SciMLBase.successful_retcode(sol2) +>>>>>>> Stashed changes end @testset "Issue#3205" begin @@ -1190,7 +1209,7 @@ end @testset "DAEProblem initialization" begin @variables x(t) [guess = 1.0] y(t) [guess = 1.0] - @parameters p=missing [guess = 1.0] q=missing [guess = 1.0] + @parameters p=missing [guess=1.0] q=missing [guess=1.0] @mtkcompile sys = System( [D(x) ~ p * y + q, x^3 + y^3 ~ 5], t; initialization_eqs = [p^2 + q^3 ~ 3]) @@ -1607,7 +1626,8 @@ end tspan = (0.0, 100.0) getter = getsym(sys, Initial.(unknowns(sys))) prob = ODEProblem(sys, [u0; p], tspan; guesses = [w2 => 3.0]) - new_u0, new_p, _ = SciMLBase.get_initial_values( + new_u0, new_p, + _ = SciMLBase.get_initial_values( prob, prob, prob.f, SciMLBase.OverrideInit(), Val(true); nlsolve_alg = NewtonRaphson(), abstol = 1e-6, reltol = 1e-3) @test getter(prob) != getter(new_p) diff --git a/test/input_output_handling.jl b/test/input_output_handling.jl index ed1fb5fc69..8b966ca814 100644 --- a/test/input_output_handling.jl +++ b/test/input_output_handling.jl @@ -159,13 +159,14 @@ end for split in [true, false] simplify = true - @variables x(t)=0 u(t)=0 [input = true] + @variables x(t)=0 u(t)=0 [input=true] eqs = [ D(x) ~ -x + u ] @named sys = System(eqs, t) - f, dvs, ps, io_sys = ModelingToolkit.generate_control_function( + f, dvs, + ps, io_sys = ModelingToolkit.generate_control_function( sys, [u]; simplify, split) @test isequal(dvs[], x) @@ -177,13 +178,15 @@ end @test f[1](x, u, p, 1) ≈ -x + u # With disturbance inputs - @variables x(t)=0 u(t)=0 [input = true] d(t)=0 + @variables x(t)=0 u(t)=0 [input=true] d(t)=0 eqs = [ D(x) ~ -x + u + d^2 ] @named sys = System(eqs, t) - f, dvs, ps, io_sys = ModelingToolkit.generate_control_function( + f, dvs, + ps, + io_sys = ModelingToolkit.generate_control_function( sys, [u], [d]; simplify, split) @test isequal(dvs[], x) @@ -195,13 +198,15 @@ end @test f[1](x, u, p, 1) ≈ -x + u ## With added d argument - @variables x(t)=0 u(t)=0 [input = true] d(t)=0 + @variables x(t)=0 u(t)=0 [input=true] d(t)=0 eqs = [ D(x) ~ -x + u + d^2 ] @named sys = System(eqs, t) - f, dvs, ps, io_sys = ModelingToolkit.generate_control_function( + f, dvs, + ps, + io_sys = ModelingToolkit.generate_control_function( sys, [u], [d]; simplify, split, disturbance_argument = true) @@ -409,7 +414,8 @@ f, augmented_sys, dvs, p = ModelingToolkit.add_input_disturbance(model, ins) augmented_sys = complete(augmented_sys) -matrices, ssys = linearize(augmented_sys, +matrices, +ssys = linearize(augmented_sys, [ augmented_sys.u, augmented_sys.input.u[2], @@ -429,7 +435,7 @@ matrices = ModelingToolkit.reorder_unknowns( # @test sminreal(G[1, 3]) ≈ sminreal(P[1,1])*dist @testset "Observed functions with inputs" begin - @variables x(t)=0 u(t)=0 [input = true] + @variables x(t)=0 u(t)=0 [input=true] eqs = [ D(x) ~ -x + u ] @@ -454,7 +460,7 @@ end end @testset "With callable symbolic" begin - @variables x(t)=0 u(t)=0 [input = true] + @variables x(t)=0 u(t)=0 [input=true] @parameters p(::Real) = (x -> 2x) eqs = [D(x) ~ -x + p(u)] @named sys = System(eqs, t) diff --git a/test/inputoutput.jl b/test/inputoutput.jl index 28f112425f..5a503be9e4 100644 --- a/test/inputoutput.jl +++ b/test/inputoutput.jl @@ -29,11 +29,11 @@ sys = connected collapsed_eqs = [ D(lorenz1.x) ~ (lorenz1.σ * (lorenz1.y - lorenz1.x) + - (lorenz2.x + lorenz2.y - lorenz2.z)), + (lorenz2.x + lorenz2.y - lorenz2.z)), D(lorenz1.y) ~ lorenz1.x * (lorenz1.ρ - lorenz1.z) - lorenz1.y, D(lorenz1.z) ~ lorenz1.x * lorenz1.y - (lorenz1.β * lorenz1.z), D(lorenz2.x) ~ (lorenz2.σ * (lorenz2.y - lorenz2.x) + - (lorenz1.x + lorenz1.y - lorenz1.z)), + (lorenz1.x + lorenz1.y - lorenz1.z)), D(lorenz2.y) ~ lorenz2.x * (lorenz2.ρ - lorenz2.z) - lorenz2.y, D(lorenz2.z) ~ lorenz2.x * lorenz2.y - (lorenz2.β * lorenz2.z)] diff --git a/test/jacobiansparsity.jl b/test/jacobiansparsity.jl index 7173ab9e1a..4c8c47feab 100644 --- a/test/jacobiansparsity.jl +++ b/test/jacobiansparsity.jl @@ -12,13 +12,17 @@ function brusselator_2d_loop(du, u, p, t) x, y = xyd_brusselator[I[1]], xyd_brusselator[I[2]] ip1, im1, jp1, jm1 = lim(i + 1, N), lim(i - 1, N), lim(j + 1, N), lim(j - 1, N) - du[i, j, 1] = alpha * (u[im1, j, 1] + u[ip1, j, 1] + u[i, jp1, 1] + u[i, jm1, 1] - - 4u[i, j, 1]) + - B + u[i, j, 1]^2 * u[i, j, 2] - (A + 1) * u[i, j, 1] + - brusselator_f(x, y, t) - du[i, j, 2] = alpha * (u[im1, j, 2] + u[ip1, j, 2] + u[i, jp1, 2] + u[i, jm1, 2] - - 4u[i, j, 2]) + - A * u[i, j, 1] - u[i, j, 1]^2 * u[i, j, 2] + du[i, + j, + 1] = alpha * (u[im1, j, 1] + u[ip1, j, 1] + u[i, jp1, 1] + u[i, jm1, 1] - + 4u[i, j, 1]) + + B + u[i, j, 1]^2 * u[i, j, 2] - (A + 1) * u[i, j, 1] + + brusselator_f(x, y, t) + du[i, + j, + 2] = alpha * (u[im1, j, 2] + u[ip1, j, 2] + u[i, jp1, 2] + u[i, jm1, 2] - + 4u[i, j, 2]) + + A * u[i, j, 1] - u[i, j, 1]^2 * u[i, j, 2] end end diff --git a/test/linearize.jl b/test/linearize.jl index f9b050c4d3..668ba68c13 100644 --- a/test/linearize.jl +++ b/test/linearize.jl @@ -4,7 +4,7 @@ using CommonSolve: solve # r is an input, and y is an output. @independent_variables t @variables x(t)=0 y(t)=0 u(t)=0 r(t)=0 -@variables x(t)=0 y(t)=0 u(t)=0 r(t)=0 [input = true] +@variables x(t)=0 y(t)=0 u(t)=0 r(t)=0 [input=true] @parameters kp = 1 D = Differential(t) @@ -116,7 +116,8 @@ Nd = 10 @named pid = LimPID(; k, Ti, Td, Nd) @unpack reference, measurement, ctr_output = pid -lsys0, ssys = linearize(pid, [reference.u, measurement.u], [ctr_output.u]; +lsys0, +ssys = linearize(pid, [reference.u, measurement.u], [ctr_output.u]; op = Dict(reference.u => 0.0, measurement.u => 0.0)) @unpack int, der = pid desired_order = [int.x, der.x] @@ -127,7 +128,8 @@ lsys = ModelingToolkit.reorder_unknowns(lsys0, unknowns(ssys), desired_order) @test lsys.C == [400 -4000] @test lsys.D == [4400 -4400] -lsyss0, ssys2 = ModelingToolkit.linearize_symbolic(pid, [reference.u, measurement.u], +lsyss0, +ssys2 = ModelingToolkit.linearize_symbolic(pid, [reference.u, measurement.u], [ctr_output.u]) lsyss = ModelingToolkit.reorder_unknowns(lsyss0, unknowns(ssys2), desired_order) diff --git a/test/modelingtoolkitize.jl b/test/modelingtoolkitize.jl index 3f2db92344..30cde7c3b7 100644 --- a/test/modelingtoolkitize.jl +++ b/test/modelingtoolkitize.jl @@ -14,15 +14,20 @@ function brusselator_2d_loop(du, u, p, t) @inbounds for I in CartesianIndices((N, N)) i, j = Tuple(I) x, y = xyd_brusselator[I[1]], xyd_brusselator[I[2]] - ip1, im1, jp1, jm1 = limit(i + 1, N), limit(i - 1, N), limit(j + 1, N), + ip1, im1, jp1, + jm1 = limit(i + 1, N), limit(i - 1, N), limit(j + 1, N), limit(j - 1, N) - du[i, j, 1] = alpha * (u[im1, j, 1] + u[ip1, j, 1] + u[i, jp1, 1] + u[i, jm1, 1] - - 4u[i, j, 1]) + - B + u[i, j, 1]^2 * u[i, j, 2] - (A + 1) * u[i, j, 1] + - brusselator_f(x, y, t) - du[i, j, 2] = alpha * (u[im1, j, 2] + u[ip1, j, 2] + u[i, jp1, 2] + u[i, jm1, 2] - - 4u[i, j, 2]) + - A * u[i, j, 1] - u[i, j, 1]^2 * u[i, j, 2] + du[i, + j, + 1] = alpha * (u[im1, j, 1] + u[ip1, j, 1] + u[i, jp1, 1] + u[i, jm1, 1] - + 4u[i, j, 1]) + + B + u[i, j, 1]^2 * u[i, j, 2] - (A + 1) * u[i, j, 1] + + brusselator_f(x, y, t) + du[i, + j, + 2] = alpha * (u[im1, j, 2] + u[ip1, j, 2] + u[i, jp1, 2] + u[i, jm1, 2] - + 4u[i, j, 2]) + + A * u[i, j, 1] - u[i, j, 1]^2 * u[i, j, 2] end end @@ -115,7 +120,9 @@ function SIRD_ac!(du, u, p, t) # initialize this parameter (death probability stratified by age, taken from literature) - δ₁, δ₂, δ₃, δ₄ = [ + δ₁, δ₂, + δ₃, + δ₄ = [ 0.003 / 100, 0.004 / 100, (0.015 + 0.030 + 0.064 + 0.213 + 0.718) / (5 * 100), @@ -255,7 +262,7 @@ sys = modelingtoolkitize(prob) @test [ModelingToolkit.defaults(sys)[s] for s in unknowns(sys)] == u0 @test [ModelingToolkit.defaults(sys)[s] for s in parameters(sys)] == [10, 20] -@parameters sig=10 rho=28.0 beta=8 / 3 +@parameters sig=10 rho=28.0 beta=8/3 @variables x(t)=100 y(t)=1.0 z(t)=1 eqs = [D(x) ~ sig * (y - x), diff --git a/test/mtkparameters.jl b/test/mtkparameters.jl index 1508d95a74..28ab3759ef 100644 --- a/test/mtkparameters.jl +++ b/test/mtkparameters.jl @@ -56,8 +56,8 @@ setp(sys, a)(ps, 1.0) @test getp(sys, a)(ps) == getp(sys, b)(ps) / 2 == getp(sys, c)(ps) / 3 == 1.0 for (portion, values) in [(Tunable(), [1.0, 5.0, 6.0, 7.0]) - (Discrete(), [3.0]) - (Constants(), vcat([0.1, 0.2, 0.3], ones(9), [4.0]))] + (Discrete(), [3.0]) + (Constants(), vcat([0.1, 0.2, 0.3], ones(9), [4.0]))] buffer, repack, alias = canonicalize(portion, ps) @test alias @test sort(collect(buffer)) == values @@ -173,7 +173,7 @@ ps = [p => 1.0] # Value for `d` is missing # scalar parameters only function level1() - @parameters p1=0.5 [tunable = true] p2=1 [tunable = true] p3=3 [tunable = false] p4=3 [tunable = true] y0=1 + @parameters p1=0.5 [tunable=true] p2=1 [tunable=true] p3=3 [tunable=false] p4=3 [tunable=true] y0=1 @variables x(t)=2 y(t)=y0 D = Differential(t) @@ -188,7 +188,7 @@ end # scalar and vector parameters function level2() - @parameters p1=0.5 [tunable = true] (p23[1:2]=[1, 3.0]) [tunable = true] p4=3 [tunable = false] y0=1 + @parameters p1=0.5 [tunable=true] (p23[1:2]=[1, 3.0]) [tunable=true] p4=3 [tunable=false] y0=1 @variables x(t)=2 y(t)=y0 D = Differential(t) @@ -203,7 +203,7 @@ end # scalar and vector parameters with different scalar types function level3() - @parameters p1=0.5 [tunable = true] (p23[1:2]=[1, 3.0]) [tunable = true] p4::Int=3 [tunable = true] y0::Int=1 + @parameters p1=0.5 [tunable=true] (p23[1:2]=[1, 3.0]) [tunable=true] p4::Int=3 [tunable=true] y0::Int=1 @variables x(t)=2 y(t)=y0 D = Differential(t) diff --git a/test/nonlinearsystem.jl b/test/nonlinearsystem.jl index 73cbbe9639..4c887f5740 100644 --- a/test/nonlinearsystem.jl +++ b/test/nonlinearsystem.jl @@ -408,7 +408,7 @@ end @test prob.f.initialization_data.initializeprobmap === nothing sol = solve(prob) @test SciMLBase.successful_retcode(sol) - @test sol.ps[p^3 + q^3]≈sol.ps[4r] atol=1e-10 + @test sol.ps[p ^ 3 + q ^ 3]≈sol.ps[4r] atol=1e-10 @testset "Differential inside expression also substituted" begin @named sys = System([0 ~ y * D(x) + x^2 - p, 0 ~ x * D(y) + y * p], t) diff --git a/test/odesystem.jl b/test/odesystem.jl index a65b096e2b..3f00120487 100644 --- a/test/odesystem.jl +++ b/test/odesystem.jl @@ -816,7 +816,7 @@ Set(unknowns(new_sys2)) == Set([new_sys2.x1, new_sys2.sys1.x1, new_sys2.sub.s1, new_sys2.sub.s2]) let # Issue https://github.com/SciML/ModelingToolkit.jl/issues/2322 - @parameters a=10 b=a / 10 c=a / 20 + @parameters a=10 b=a/10 c=a/20 Dt = D @@ -1384,7 +1384,7 @@ end @mtkcompile sys = System([D(x) ~ x, y^2 ~ x + sum(p)], t) prob = DAEProblem(sys, [D(x) => x, D(y) => D(x) / 2y], (0.0, 1.0)) sol = solve(prob, DFBDF(), abstol = 1e-8, reltol = 1e-8) - @test sol[x]≈sol[y^2 - sum(p)] atol=1e-5 + @test sol[x]≈sol[y ^ 2 - sum(p)] atol=1e-5 end @testset "Symbolic tstops" begin @@ -1484,7 +1484,8 @@ end obsfn_expr = ModelingToolkit.build_explicit_observed_function( sys, 2x + 1, expression = true) @test obsfn_expr isa Expr - obsfn_expr_oop, obsfn_expr_iip = ModelingToolkit.build_explicit_observed_function( + obsfn_expr_oop, + obsfn_expr_iip = ModelingToolkit.build_explicit_observed_function( sys, [x + 1, x + 2, x + t], return_inplace = true, expression = true) @test obsfn_expr_oop isa Expr @test obsfn_expr_iip isa Expr diff --git a/test/reduction.jl b/test/reduction.jl index 97d360fd67..6fee37d806 100644 --- a/test/reduction.jl +++ b/test/reduction.jl @@ -172,7 +172,7 @@ nlprob.f(residual, reducedsol.u, pp) N = 5 @variables xs[1:N] -A = reshape(1:(N^2), N, N) +A = reshape(1:(N ^ 2), N, N) eqs = xs ~ A * xs @named sys′ = System(eqs, [xs], []) sys = mtkcompile(sys′) diff --git a/test/scc_nonlinear_problem.jl b/test/scc_nonlinear_problem.jl index fad30dbcea..70031ff228 100644 --- a/test/scc_nonlinear_problem.jl +++ b/test/scc_nonlinear_problem.jl @@ -185,7 +185,11 @@ import ModelingToolkitStandardLibrary.Hydraulic.IsothermalCompressible as IC @testset "Caching of subexpressions of different types" begin liquid_pressure(rho, rho_0, bulk) = (rho / rho_0 - 1) * bulk gas_pressure(rho, rho_0, p_gas, rho_gas) = rho * ((0 - p_gas) / (rho_0 - rho_gas)) - full_pressure(rho, rho_0, bulk, p_gas, rho_gas) = ifelse( + full_pressure(rho, + rho_0, + bulk, + p_gas, + rho_gas) = ifelse( rho >= rho_0, liquid_pressure(rho, rho_0, bulk), gas_pressure(rho, rho_0, p_gas, rho_gas)) diff --git a/test/sciml_problem_inputs.jl b/test/sciml_problem_inputs.jl index 46dfbbed21..a91a8d8c7c 100644 --- a/test/sciml_problem_inputs.jl +++ b/test/sciml_problem_inputs.jl @@ -111,6 +111,7 @@ end # Simulates problems for all input types, checking that identical solutions are found. # test failure. for u0 in u0_alts, p in p_alts + oprob = remake(base_oprob; u0, p) @test base_sol == solve(oprob, Tsit5(); saveat = 1.0) eprob = remake(base_eprob; u0, p) @@ -124,6 +125,7 @@ end base_sol = solve(base_nlprob, NewtonRaphson()) # Solves problems for all input types, checking that identical solutions are found. for u0 in u0_alts, p in p_alts + nlprob = remake(base_nlprob; u0, p) @test base_sol == solve(nlprob, NewtonRaphson()) end @@ -140,6 +142,7 @@ end # Simulates problems for all input types, checking that identical solutions are found. # test failure. for u0 in u0_alts, p in p_alts + ssprob = remake(base_ssprob; u0, p) @test base_sol == solve(ssprob, DynamicSS(Tsit5())) eprob = remake(base_eprob; u0, p) @@ -206,6 +209,7 @@ end (idiscsys, ImplicitDiscreteProblem{true, SciMLBase.FullSpecialize}) ] @testset "$(typeof(u0)) - $(typeof(p))" for u0 in u0s, p in ps + if u0 isa Vector{Float64} && ctor <: ImplicitDiscreteProblem u0 = ones(2) end @@ -224,6 +228,7 @@ end (optsys, OptimizationProblem{true}) ] @testset "$(typeof(u0)) - $(typeof(p))" for u0 in u0s, p in ps + @test_warn ["deprecated"] ctor(sys, u0, p) end end diff --git a/test/split_parameters.jl b/test/split_parameters.jl index 9b5ed3424a..a92d9f8350 100644 --- a/test/split_parameters.jl +++ b/test/split_parameters.jl @@ -257,7 +257,8 @@ end @testset "Concrete function type" begin ts = 0.0:0.1:1.0 - interp = LinearInterpolation(ts .^ 2, ts; extrapolation = ExtrapolationType.Extension) + interp = LinearInterpolation( + ts .^ 2, ts; extrapolation = ExtrapolationType.Extension) @variables x(t) @parameters (fn::typeof(interp))(..) @mtkcompile sys = System(D(x) ~ fn(x), t) @@ -267,7 +268,8 @@ end @inferred getter(prob) @inferred prob.f(prob.u0, prob.p, prob.tspan[1]) @test_nowarn sol = solve(prob, Tsit5()) - @test_nowarn prob.ps[fn] = LinearInterpolation(ts .^ 3, ts; extrapolation = ExtrapolationType.Extension) + @test_nowarn prob.ps[fn] = LinearInterpolation( + ts .^ 3, ts; extrapolation = ExtrapolationType.Extension) @test_nowarn sol = solve(prob) end end diff --git a/test/stream_connectors.jl b/test/stream_connectors.jl index dfca3306f4..493d9996e3 100644 --- a/test/stream_connectors.jl +++ b/test/stream_connectors.jl @@ -265,7 +265,7 @@ sys_exp = expand_connections(compose(sys, [sp1, sp2])) # array var @connector function VecPin(; name) - sts = @variables v(t)[1:2]=[1.0, 0.0] i(t)[1:2]=1.0 [connect = Flow] + sts = @variables v(t)[1:2]=[1.0, 0.0] i(t)[1:2]=1.0 [connect=Flow] System(Equation[], t, [sts...;], []; name = name) end @@ -284,7 +284,7 @@ sys = expand_connections(compose(simple, [vp1, vp2, vp3])) 0 ~ -vp1.i[2] - vp2.i[2] - vp3.i[2]]) @connector function VectorHeatPort(; name, N = 100, T0 = 0.0, Q0 = 0.0) - @variables (T(t))[1:N]=T0 (Q(t))[1:N]=Q0 [connect = Flow] + @variables (T(t))[1:N]=T0 (Q(t))[1:N]=Q0 [connect=Flow] System(Equation[], t, [T; Q], []; name = name) end diff --git a/test/structural_transformation/index_reduction.jl b/test/structural_transformation/index_reduction.jl index 229b1f5736..2118c8441d 100644 --- a/test/structural_transformation/index_reduction.jl +++ b/test/structural_transformation/index_reduction.jl @@ -68,5 +68,5 @@ let sys, [x => 1, y => 0, D(x) => 0.0, g => 1], (0.0, 10.0), guesses = [λ => 0.0]) sol = solve(prob, Rodas5P()) @test SciMLBase.successful_retcode(sol) - @test sol[x^2 + y^2][end] < 1.1 + @test sol[x ^ 2 + y ^ 2][end] < 1.1 end diff --git a/test/symbolic_events.jl b/test/symbolic_events.jl index e48f793a70..53af99f1da 100644 --- a/test/symbolic_events.jl +++ b/test/symbolic_events.jl @@ -433,7 +433,8 @@ end compose(System(Equation[], t; name), spring, damper) end - connect_sd(sd, m1, m2) = [ + connect_sd( + sd, m1, m2) = [ sd.spring.x ~ m1.pos - m2.pos, sd.damper.vel ~ m1.vel - m2.vel] sd_force(sd) = -sd.spring.k * sd.spring.x - sd.damper.c * sd.damper.vel @named mass1 = Mass(; m = 1) @@ -1085,8 +1086,10 @@ end [x ~ 0], nothing, initialize = [x ~ 1.5], finalize = f) inited = false finaled = false - a = ModelingToolkit.ImperativeAffect(f = (m, o, ctx, int) -> (inited = true; return (;))) - b = ModelingToolkit.ImperativeAffect(f = (m, o, ctx, int) -> (finaled = true; return (;))) + a = ModelingToolkit.ImperativeAffect(f = ( + m, o, ctx, int) -> (inited = true; return (;))) + b = ModelingToolkit.ImperativeAffect(f = ( + m, o, ctx, int) -> (finaled = true; return (;))) cb2 = ModelingToolkit.SymbolicContinuousCallback( [x ~ 0.1], nothing, initialize = a, finalize = b) @mtkcompile sys = System(D(x) ~ -1, t, [x], []; continuous_events = [cb1, cb2]) diff --git a/test/test_variable_metadata.jl b/test/test_variable_metadata.jl index 596372d45b..d10e0fdc17 100644 --- a/test/test_variable_metadata.jl +++ b/test/test_variable_metadata.jl @@ -117,7 +117,7 @@ d = FakeNormal() ## System interface @independent_variables t Dₜ = Differential(t) -@variables x(t)=0 [bounds = (-10, 10)] u(t)=0 [input = true] y(t)=0 [output = true] +@variables x(t)=0 [bounds=(-10, 10)] u(t)=0 [input=true] y(t)=0 [output=true] @parameters T [bounds = (0, Inf)] @parameters k [tunable = true, bounds = (0, Inf)] @parameters k2 [tunable = false] diff --git a/test/units.jl b/test/units.jl index 77f35877e9..a17dd90575 100644 --- a/test/units.jl +++ b/test/units.jl @@ -66,19 +66,19 @@ System(eqs, t, name = :sys, checks = false) # connection validation @connector function Pin(; name) - sts = @variables(v(t)=1.0, [unit = u"V"], - i(t)=1.0, [unit = u"A", connect = Flow]) + sts = @variables(v(t)=1.0, [unit=u"V"], + i(t)=1.0, [unit=u"A", connect=Flow]) System(Equation[], t, sts, []; name = name) end @connector function OtherPin(; name) - sts = @variables(v(t)=1.0, [unit = u"mV"], - i(t)=1.0, [unit = u"mA", connect = Flow]) + sts = @variables(v(t)=1.0, [unit=u"mV"], + i(t)=1.0, [unit=u"mA", connect=Flow]) System(Equation[], t, sts, []; name = name) end @connector function LongPin(; name) - sts = @variables(v(t)=1.0, [unit = u"V"], - i(t)=1.0, [unit = u"A", connect = Flow], - x(t)=1.0, [unit = NoUnits]) + sts = @variables(v(t)=1.0, [unit=u"V"], + i(t)=1.0, [unit=u"A", connect=Flow], + x(t)=1.0, [unit=NoUnits]) System(Equation[], t, sts, []; name = name) end @named p1 = Pin() diff --git a/test/variable_parsing.jl b/test/variable_parsing.jl index 1ea366d045..60b4e24d64 100644 --- a/test/variable_parsing.jl +++ b/test/variable_parsing.jl @@ -88,7 +88,7 @@ end @test getmetadata(x, VariableUnit) == u @test getmetadata(y, VariableDefaultValue) === 2 -@variables x=[1, 2] [connect = Flow, unit = u] y=2 +@variables x=[1, 2] [connect=Flow, unit=u] y=2 @test getmetadata(x, VariableDefaultValue) == [1, 2] @test getmetadata(x, VariableConnectType) == Flow From 272b2a1084d1787ae257506e02551c3b3cd81c9f Mon Sep 17 00:00:00 2001 From: contradict Date: Tue, 8 Jul 2025 14:12:34 -0700 Subject: [PATCH 2170/2176] Oops, didn't mean to include these changes. --- src/systems/nonlinear/initializesystem.jl | 6 ------ test/initializationsystem.jl | 12 ------------ 2 files changed, 18 deletions(-) diff --git a/src/systems/nonlinear/initializesystem.jl b/src/systems/nonlinear/initializesystem.jl index e5cdcac53d..25dc5ded0d 100644 --- a/src/systems/nonlinear/initializesystem.jl +++ b/src/systems/nonlinear/initializesystem.jl @@ -662,13 +662,7 @@ function SciMLBase.remake_initialization_data( kws = maybe_build_initialization_problem( sys, SciMLBase.isinplace(odefn), op, t0, defs, guesses, missing_unknowns; time_dependent_init, use_scc, initialization_eqs, floatT, -<<<<<<< Updated upstream u0_constructor, p_constructor, allow_incomplete = true) -||||||| Stash base - u0_constructor, p_constructor, allow_incomplete = true, check_units=false) -======= - u0_constructor, p_constructor, allow_incomplete = true, check_units = false) ->>>>>>> Stashed changes odefn = remake(odefn; kws...) return SciMLBase.remake_initialization_data(sys, odefn, newu0, t0, newp, newu0, newp) diff --git a/test/initializationsystem.jl b/test/initializationsystem.jl index ec34253fd5..51119a5347 100644 --- a/test/initializationsystem.jl +++ b/test/initializationsystem.jl @@ -1112,18 +1112,6 @@ end guesses = ModelingToolkit.missing_variable_defaults(pend)) sol = solve(prob, Rodas5P()) @test SciMLBase.successful_retcode(sol) -<<<<<<< Updated upstream -||||||| Stash base - - prob2 = remake(prob, u0=[x => 0.5, y=>nothing]) - sol2 = solve(prob2, Rodas5P()) - @test SciMLBase.successful_retcode(sol2) -======= - - prob2 = remake(prob, u0 = [x => 0.5, y=>nothing]) - sol2 = solve(prob2, Rodas5P()) - @test SciMLBase.successful_retcode(sol2) ->>>>>>> Stashed changes end @testset "Issue#3205" begin From dfdf4913bae0e6fd42fff4fba91ce50362a434f8 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 9 Jul 2025 12:19:01 +0530 Subject: [PATCH 2171/2176] refactor: format --- docs/src/basics/Events.md | 9 +- docs/src/basics/FAQ.md | 2 +- docs/src/tutorials/linear_analysis.md | 2 +- src/systems/diffeqs/basic_transformations.jl | 32 ++--- src/systems/model_parsing.jl | 132 ++++++++++++++----- 5 files changed, 120 insertions(+), 57 deletions(-) diff --git a/docs/src/basics/Events.md b/docs/src/basics/Events.md index aca8eb4b68..bfeac35526 100644 --- a/docs/src/basics/Events.md +++ b/docs/src/basics/Events.md @@ -92,8 +92,8 @@ The basic purely symbolic continuous event interface to encode *one* continuous event is ```julia -AbstractSystem(eqs, ...; continuous_events::Vector{Equation}) -AbstractSystem(eqs, ...; continuous_events::Pair{Vector{Equation}, Vector{Equation}}) +AbstractSystem(eqs, _...; continuous_events::Vector{Equation}) +AbstractSystem(eqs, _...; continuous_events::Pair{Vector{Equation}, Vector{Equation}}) ``` In the former, equations that evaluate to 0 will represent conditions that should @@ -272,7 +272,7 @@ In addition to continuous events, discrete events are also supported. The general interface to represent a collection of discrete events is ```julia -AbstractSystem(eqs, ...; discrete_events = [condition1 => affect1, condition2 => affect2]) +AbstractSystem(eqs, _...; discrete_events = [condition1 => affect1, condition2 => affect2]) ``` where conditions are symbolic expressions that should evaluate to `true` when an @@ -497,7 +497,8 @@ so far we aren't using anything that's not possible with the implicit interface. You can also write ```julia -[temp ~ furnace_off_threshold] => ModelingToolkit.ImperativeAffect(modified = (; +[temp ~ + furnace_off_threshold] => ModelingToolkit.ImperativeAffect(modified = (; furnace_on)) do x, o, i, c @set! x.furnace_on = false end diff --git a/docs/src/basics/FAQ.md b/docs/src/basics/FAQ.md index 96a65095cf..3f09ab8b13 100644 --- a/docs/src/basics/FAQ.md +++ b/docs/src/basics/FAQ.md @@ -63,7 +63,7 @@ The same principle applies to any parameter type that is not `Float64`. @parameters p1::Int # integer-valued @parameters p2::Bool # boolean-valued @parameters p3::MyCustomStructType # non-numeric -@parameters p4::ComponentArray{...} # non-standard array +@parameters p4::ComponentArray{_...} # non-standard array ``` ## Getting the index for a symbol diff --git a/docs/src/tutorials/linear_analysis.md b/docs/src/tutorials/linear_analysis.md index 2119ea9ad1..5317b45fc9 100644 --- a/docs/src/tutorials/linear_analysis.md +++ b/docs/src/tutorials/linear_analysis.md @@ -39,7 +39,7 @@ This is signified by the name being the middle argument to `connect`. Of the above mentioned functions, all except for [`open_loop`](@ref) return the output of [`ModelingToolkit.linearize`](@ref), which is ```julia -matrices, simplified_sys = linearize(...) +matrices, simplified_sys = linearize(_...) # matrices = (; A, B, C, D) ``` diff --git a/src/systems/diffeqs/basic_transformations.jl b/src/systems/diffeqs/basic_transformations.jl index 9312966528..b8268e884e 100644 --- a/src/systems/diffeqs/basic_transformations.jl +++ b/src/systems/diffeqs/basic_transformations.jl @@ -94,26 +94,25 @@ new_sol = solve(new_prob, Tsit5()) """ function change_of_variables( - sys::System, iv, forward_subs, backward_subs; - simplify=true, t0=missing, isSDE=false + sys::System, iv, forward_subs, backward_subs; + simplify = true, t0 = missing, isSDE = false ) t = iv old_vars = first.(backward_subs) new_vars = last.(forward_subs) - + # use: f = Y(t, X) # use: dY = (∂f/∂t + μ∂f/∂x + (1/2)*σ^2*∂2f/∂x2)dt + σ∂f/∂xdW old_eqs = equations(sys) neqs = get_noise_eqs(sys) brownvars = brownians(sys) - - + if neqs === nothing && length(brownvars) === 0 neqs = ones(1, length(old_eqs)) elseif neqs !== nothing isSDE = true - neqs = [neqs[i,:] for i in 1:size(neqs,1)] + neqs = [neqs[i, :] for i in 1:size(neqs, 1)] brownvars = map([Symbol(:B, :_, i) for i in 1:length(neqs[1])]) do name unwrap(only(@brownians $name)) @@ -135,9 +134,10 @@ function change_of_variables( end # df/dt = ∂f/∂x dx/dt + ∂f/∂t - dfdt = Symbolics.derivative( first.(forward_subs), t ) - ∂f∂x = [Symbolics.derivative( first(f_sub), old_var ) for (f_sub, old_var) in zip(forward_subs, old_vars)] - ∂2f∂x2 = Symbolics.derivative.( ∂f∂x, old_vars ) + dfdt = Symbolics.derivative(first.(forward_subs), t) + ∂f∂x = [Symbolics.derivative(first(f_sub), old_var) + for (f_sub, old_var) in zip(forward_subs, old_vars)] + ∂2f∂x2 = Symbolics.derivative.(∂f∂x, old_vars) new_eqs = Equation[] for (new_var, ex, first, second) in zip(new_vars, dfdt, ∂f∂x, ∂2f∂x2) @@ -154,7 +154,7 @@ function change_of_variables( ex = substitute(ex, Dict(forward_subs)) ex = substitute(ex, Dict(backward_subs)) if simplify - ex = Symbolics.simplify(ex, expand=true) + ex = Symbolics.simplify(ex, expand = true) end push!(new_eqs, Differential(t)(new_var) ~ ex) end @@ -174,10 +174,11 @@ function change_of_variables( end end - @named new_sys = System(vcat(new_eqs, first.(backward_subs) .~ last.(backward_subs)), t; - defaults=new_defs, - observed=observed(sys) - ) + @named new_sys = System( + vcat(new_eqs, first.(backward_subs) .~ last.(backward_subs)), t; + defaults = new_defs, + observed = observed(sys) + ) if simplify return mtkcompile(new_sys) end @@ -570,7 +571,8 @@ All accumulation variables have a default of zero. function add_accumulations(sys::System, vars::Vector{<:Pair}) eqs = get_eqs(sys) avars = map(first, vars) - if (ints = intersect(avars, unknowns(sys)); !isempty(ints)) + ints = intersect(avars, unknowns(sys)) + if !isempty(ints) error("$ints already exist in the system!") end D = Differential(get_iv(sys)) diff --git a/src/systems/model_parsing.jl b/src/systems/model_parsing.jl index 18c1b15922..c24c063ee0 100644 --- a/src/systems/model_parsing.jl +++ b/src/systems/model_parsing.jl @@ -85,12 +85,17 @@ function _model_macro(mod, fullname::Union{Expr, Symbol}, expr, isconnector) push!(exprs.args, arg) elseif arg.head == :if MLStyle.@match arg begin - Expr(:if, condition, x) => begin + Expr(:if, + condition, + x) => begin parse_conditional_model_statements(comps, dict, eqs, exprs, kwargs, mod, ps, vs, where_types, parse_top_level_branch(condition, x.args)...) end - Expr(:if, condition, x, y) => begin + Expr(:if, + condition, + x, + y) => begin parse_conditional_model_statements(comps, dict, eqs, exprs, kwargs, mod, ps, vs, where_types, parse_top_level_branch(condition, x.args, y)...) @@ -273,7 +278,9 @@ Base.@nospecializeinfer function parse_variable_def!( # Parses: `par6(t)[1:3]::BigFloat` # Recursively called by: `par2(t)::Int` # Recursively called by: `par3(t)::BigFloat = 1.0` - Expr(:(::), a, type) => begin + Expr(:(::), + a, + type) => begin type = getfield(mod, type) parse_variable_def!( dict, mod, a, varclass, kwargs, where_types; def, type, meta) @@ -283,7 +290,9 @@ Base.@nospecializeinfer function parse_variable_def!( # Recursively called by: `j(t) = jval, [description = "j(t)"]` # Recursively called by: `par2(t)::Int` # Recursively called by: `par3(t)::BigFloat = 1.0` - Expr(:call, a, b) => begin + Expr(:call, + a, + b) => begin var = generate_var!(dict, a, b, varclass, mod; type) update_kwargs_and_metadata!(dict, kwargs, a, def, type, varclass, where_types, meta) @@ -307,7 +316,8 @@ Base.@nospecializeinfer function parse_variable_def!( # `e2[1:2], [description = "e2"]` # `h2(t)[1:2], [description = "h2(t)"]` Expr(:tuple, Expr(:(=), Expr(:ref, a, indices...), default_val), meta_val) || - Expr(:tuple, Expr(:(=), Expr(:(::), Expr(:ref, a, indices...), type), default_val), meta_val) || + Expr(:tuple, Expr(:(=), Expr(:(::), Expr(:ref, a, indices...), type), default_val), + meta_val) || Expr(:tuple, Expr(:(::), Expr(:ref, a, indices...), type), meta_val) || Expr(:tuple, Expr(:ref, a, indices...), meta_val) => begin (@isdefined type) || (type = Real) @@ -341,7 +351,9 @@ Base.@nospecializeinfer function parse_variable_def!( # `d2[1:2] = 2` # `l(t)[1:2, 1:3] = 2, [description = "l is more than 1D"]` Expr(:(=), Expr(:(::), Expr(:ref, a, indices...), type), def_n_meta) || - Expr(:(=), Expr(:ref, a, indices...), def_n_meta) => begin + Expr(:(=), + Expr(:ref, a, indices...), + def_n_meta) => begin (@isdefined type) || (type = Real) varname = Meta.isexpr(a, :call) ? a.args[1] : a if Meta.isexpr(def_n_meta, :tuple) @@ -401,7 +413,9 @@ Base.@nospecializeinfer function parse_variable_def!( # `b2(t)[1:2]` # `a2[1:2]` Expr(:(::), Expr(:ref, a, indices...), type) || - Expr(:ref, a, indices...) => begin + Expr(:ref, + a, + indices...) => begin (@isdefined type) || (type = Real) varname = a isa Expr && a.head == :call ? a.args[1] : a if varclass == :parameters @@ -431,10 +445,13 @@ Base.@nospecializeinfer function parse_variable_def!( # Parses: `k = kval, [description = "k"]` # Parses: `par0::Bool = true` # Parses: `par3(t)::BigFloat = 1.0` - Expr(:(=), a, b) => begin + Expr(:(=), + a, + b) => begin Base.remove_linenums!(b) def, meta = parse_default(mod, b) - var, def, _ = parse_variable_def!( + var, def, + _ = parse_variable_def!( dict, mod, a, varclass, kwargs, where_types; def, type, meta) varclass_dict = dict[varclass] isa Vector ? Ref(dict[varclass][1]) : Ref(dict[varclass]) @@ -449,9 +466,12 @@ Base.@nospecializeinfer function parse_variable_def!( # Parses: `e, [description = "e"]` # Parses: `h(t), [description = "h(t)"]` # Parses: `par2(t)::Int` - Expr(:tuple, a, b) => begin + Expr(:tuple, + a, + b) => begin meta = parse_metadata(mod, b) - var, def, _ = parse_variable_def!( + var, def, + _ = parse_variable_def!( dict, mod, a, varclass, kwargs, where_types; type, meta) varclass_dict = dict[varclass] isa Vector ? Ref(dict[varclass][1]) : Ref(dict[varclass]) @@ -690,7 +710,9 @@ function parse_structural_parameters!(exprs, sps, dict, mod, body, kwargs) Base.remove_linenums!(body) for arg in body.args MLStyle.@match arg begin - Expr(:(=), Expr(:(::), a, type), b) => begin + Expr(:(=), + Expr(:(::), a, type), + b) => begin type = getfield(mod, type) b = _type_check!(get_var(mod, b), a, type, :structural_parameters) push!(sps, a) @@ -698,7 +720,9 @@ function parse_structural_parameters!(exprs, sps, dict, mod, body, kwargs) dict[:structural_parameters][a] = dict[:kwargs][a] = Dict( :value => b, :type => type) end - Expr(:(=), a, b) => begin + Expr(:(=), + a, + b) => begin push!(sps, a) push!(kwargs, Expr(:kw, a, b)) dict[:structural_parameters][a] = dict[:kwargs][a] = Dict(:value => b) @@ -742,7 +766,8 @@ function extend_args!(a, b, dict, expr, kwargs, has_param = false) push!(kwargs, Expr(:kw, x, y)) dict[:kwargs][x] = Dict(:value => y) end - Expr(:parameters, x...) => begin + Expr(:parameters, + x...) => begin has_param = true extend_args!(a, arg, dict, expr, kwargs, has_param) end @@ -806,7 +831,9 @@ function parse_extend!(exprs, ext, dict, mod, body, kwargs) push!(exprs, expr) body = deepcopy(body) MLStyle.@match body begin - Expr(:(=), a, b) => begin + Expr(:(=), + a, + b) => begin if Meta.isexpr(b, :(=)) vars = a if !Meta.isexpr(vars, :tuple) @@ -820,7 +847,9 @@ function parse_extend!(exprs, ext, dict, mod, body, kwargs) error("When explicitly destructing in `@extend` please use the syntax: `@extend a, b = oneport = OnePort()`.") end end - Expr(:call, a′, _...) => begin + Expr(:call, + a′, + _...) => begin a = Symbol(Symbol("#mtkmodel"), :__anonymous__, a′) b = body if (model = getproperty(mod, b.args[1])) isa Model @@ -876,7 +905,8 @@ convert_units(::Unitful.FreeUnits, value::Num) = value convert_units(::DynamicQuantities.Quantity, value::Num) = value function parse_variable_arg(dict, mod, arg, varclass, kwargs, where_types) - vv, def, metadata_with_exprs = parse_variable_def!( + vv, def, + metadata_with_exprs = parse_variable_def!( dict, mod, arg, varclass, kwargs, where_types) if !(vv isa Tuple) name = getname(vv) @@ -931,7 +961,8 @@ function handle_conditional_vars!( :constants => Any[Dict{Symbol, Dict{Symbol, Any}}()], :variables => Any[Dict{Symbol, Dict{Symbol, Any}}()]) for _arg in arg.args - name, ex = parse_variable_arg( + name, + ex = parse_variable_arg( conditional_dict, mod, _arg, varclass, kwargs, where_types) push!(conditional_branch.args, ex) push!(conditional_branch.args, :(push!($varclass, $name))) @@ -997,7 +1028,9 @@ function parse_variables!(exprs, vs, dict, mod, body, varclass, kwargs, where_ty for arg in body.args arg isa LineNumberNode && continue MLStyle.@match arg begin - Expr(:if, condition, x) => begin + Expr(:if, + condition, + x) => begin conditional_expr = Expr(:if, condition, Expr(:block)) conditional_dict = handle_conditional_vars!(x, conditional_expr.args[2], @@ -1008,7 +1041,10 @@ function parse_variables!(exprs, vs, dict, mod, body, varclass, kwargs, where_ty push!(expr.args, conditional_expr) push_conditional_dict!(dict, condition, conditional_dict, nothing, varclass) end - Expr(:if, condition, x, y) => begin + Expr(:if, + condition, + x, + y) => begin conditional_expr = Expr(:if, condition, Expr(:block)) conditional_dict = handle_conditional_vars!(x, conditional_expr.args[2], @@ -1016,7 +1052,8 @@ function parse_variables!(exprs, vs, dict, mod, body, varclass, kwargs, where_ty varclass, kwargs, where_types) - conditional_y_expr, conditional_y_tuple = handle_y_vars(y, + conditional_y_expr, + conditional_y_tuple = handle_y_vars(y, conditional_dict, mod, varclass, @@ -1044,7 +1081,8 @@ function handle_y_vars(y, dict, mod, varclass, kwargs, where_types) varclass, kwargs, where_types) - _y_expr, _conditional_dict = handle_y_vars( + _y_expr, + _conditional_dict = handle_y_vars( y.args[end], dict, mod, varclass, kwargs, where_types) push!(conditional_y_expr.args, _y_expr) (:elseif, y.args[1], conditional_dict, _conditional_dict) @@ -1090,13 +1128,15 @@ function parse_equations!(exprs, eqs, dict, body) Base.remove_linenums!(body) for arg in body.args MLStyle.@match arg begin - Expr(:if, condition, x) => begin + Expr(:if, condition, + x) => begin ifexpr = Expr(:if) eq_entry = handle_if_x_equations!(condition, dict, ifexpr, x) push!(exprs, ifexpr) push!(dict[:equations], (:if, condition, eq_entry)) end - Expr(:if, condition, x, y) => begin + Expr(:if, condition, x, + y) => begin ifexpr = Expr(:if) xeq_entry = handle_if_x_equations!(condition, dict, ifexpr, x) yeq_entry = handle_if_y_equations!(ifexpr, y, dict) @@ -1183,9 +1223,11 @@ end function parse_icon!(body::String, dict, icon, mod) icon_dir = get(ENV, "MTK_ICONS_DIR", joinpath(DEPOT_PATH[1], "mtk_icons")) + iconpath = abspath(joinpath(icon_dir, body)) + _body = lstrip(body) dict[:icon] = icon[] = if isfile(body) URI("file:///" * abspath(body)) - elseif (iconpath = abspath(joinpath(icon_dir, body)); isfile(iconpath)) + elseif isfile(iconpath) URI("file:///" * abspath(iconpath)) elseif try Base.isvalid(URI(body)) @@ -1193,7 +1235,7 @@ function parse_icon!(body::String, dict, icon, mod) false end URI(body) - elseif (_body = lstrip(body); startswith(_body, r"<\?xml| begin + x::Symbol || + Expr(:kw, x) => begin varname, _varname = _rename(a, x) b.args[i] = Expr(:kw, x, _varname) push!(varexpr.args, :((if $varname !== nothing @@ -1240,7 +1283,9 @@ function component_args!(a, b, varexpr, kwargs; index_name = nothing) Expr(:parameters, x...) => begin component_args!(a, arg, varexpr, kwargs) end - Expr(:kw, x, y) => begin + Expr(:kw, + x, + y) => begin varname, _varname = _rename(a, x) b.args[i] = Expr(:kw, x, _varname) if isnothing(index_name) @@ -1269,7 +1314,9 @@ function _parse_components!(body, kwargs) arg = body.args[end] MLStyle.@match arg begin - Expr(:(=), a, Expr(:comprehension, Expr(:generator, b, Expr(:(=), c, d)))) => begin + Expr(:(=), + a, + Expr(:comprehension, Expr(:generator, b, Expr(:(=), c, d)))) => begin array_varexpr = Expr(:block) push!(comp_names, :($a...)) @@ -1280,10 +1327,14 @@ function _parse_components!(body, kwargs) expr = _named_idxs(a, d, :($c -> $b); extra_args = array_varexpr) end - Expr(:(=), a, Expr(:comprehension, Expr(:generator, b, Expr(:filter, e, Expr(:(=), c, d))))) => begin + Expr(:(=), + a, + Expr(:comprehension, Expr(:generator, b, Expr(:filter, e, Expr(:(=), c, d))))) => begin error("List comprehensions with conditional statements aren't supported.") end - Expr(:(=), a, Expr(:comprehension, Expr(:generator, b, Expr(:(=), c, d), e...))) => begin + Expr(:(=), + a, + Expr(:comprehension, Expr(:generator, b, Expr(:(=), c, d), e...))) => begin # Note that `e` is of the form `Tuple{Expr(:(=), c, d)}` error("More than one index isn't supported while building component array") end @@ -1291,7 +1342,9 @@ function _parse_components!(body, kwargs) # TODO: Do we need this? error("Multiple `@components` block detected within a single block") end - Expr(:(=), a, Expr(:for, Expr(:(=), c, d), b)) => begin + Expr(:(=), + a, + Expr(:for, Expr(:(=), c, d), b)) => begin Base.remove_linenums!(b) array_varexpr = Expr(:block) push!(array_varexpr.args, b.args[1:(end - 1)]...) @@ -1367,15 +1420,20 @@ function parse_components!(exprs, cs, dict, compbody, kwargs) Base.remove_linenums!(compbody) for arg in compbody.args MLStyle.@match arg begin - Expr(:if, condition, x) => begin + Expr(:if, condition, + x) => begin handle_conditional_components(condition, dict, exprs, kwargs, x) end - Expr(:if, condition, x, y) => begin + Expr(:if, + condition, + x, + y) => begin handle_conditional_components(condition, dict, exprs, kwargs, x, y) end # Either the arg is top level component declaration or an invalid cause - both are handled by `_parse_components` _ => begin - comp_names, comps, expr_vec, varexpr = _parse_components!(:(begin + comp_names, comps, expr_vec, + varexpr = _parse_components!(:(begin $arg end), kwargs) @@ -1403,7 +1461,9 @@ define_blocks(branch) = [Expr(branch), Expr(branch), Expr(branch), Expr(branch)] Base.@nospecializeinfer function parse_top_level_branch( condition, x, y = nothing, branch::Symbol = :if) @nospecialize - blocks::Vector{Union{Expr, Nothing}} = component_blk, equations_blk, parameter_blk, variable_blk = define_blocks(branch) + blocks::Vector{Union{Expr, + Nothing}} = component_blk, equations_blk, parameter_blk, + variable_blk = define_blocks(branch) for arg in x if arg.args[1] == Symbol("@components") From 211d6ffdfb98146cab9b5f8c93d22f6090bc8e90 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 9 Jul 2025 18:15:58 +0530 Subject: [PATCH 2172/2176] build: bump minor version --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index b904650026..cfebdc4225 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "ModelingToolkit" uuid = "961ee093-0014-501f-94e3-6117800e7a78" authors = ["Yingbo Ma ", "Chris Rackauckas and contributors"] -version = "10.8.0" +version = "10.9.0" [deps] ADTypes = "47edcb42-4c32-4615-8424-f2b9edc5f35b" From 2a3a9ab7692bfd5eef0f64ad74a3445306b80597 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 9 Jul 2025 19:48:53 +0530 Subject: [PATCH 2173/2176] feat: make deprecated `ODESystem` alias `System` type --- src/ModelingToolkit.jl | 5 ++++- src/deprecations.jl | 10 +++++++++- src/systems/system.jl | 2 +- test/odesystem.jl | 14 ++++++++++++++ 4 files changed, 28 insertions(+), 3 deletions(-) diff --git a/src/ModelingToolkit.jl b/src/ModelingToolkit.jl index d23d173b9a..46c6892607 100644 --- a/src/ModelingToolkit.jl +++ b/src/ModelingToolkit.jl @@ -138,6 +138,9 @@ $(TYPEDEF) TODO """ abstract type AbstractSystem end +# Solely so that `ODESystem` can be deprecated and still act as a valid type. +# See `deprecations.jl`. +abstract type IntermediateDeprecationSystem <: AbstractSystem end function independent_variable end @@ -275,7 +278,7 @@ PrecompileTools.@compile_workload begin end export ODEFunction, convert_system_indepvar, - System, OptimizationSystem, JumpSystem, SDESystem, NonlinearSystem + System, OptimizationSystem, JumpSystem, SDESystem, NonlinearSystem, ODESystem export SDEFunction export SystemStructure export DiscreteProblem, DiscreteFunction diff --git a/src/deprecations.jl b/src/deprecations.jl index bbd266fa0c..3735d8f6f4 100644 --- a/src/deprecations.jl +++ b/src/deprecations.jl @@ -9,7 +9,15 @@ macro mtkbuild(exprs...) end |> esc end -for T in [:ODESystem, :NonlinearSystem, :DiscreteSystem, :ImplicitDiscreteSystem] +const ODESystem = IntermediateDeprecationSystem + +function IntermediateDeprecationSystem(args...; kwargs...) + Base.depwarn("`ODESystem(args...; kwargs...)` is deprecated. Use `System(args...; kwargs...) instead`.", :ODESystem) + + return System(args...; kwargs...) +end + +for T in [:NonlinearSystem, :DiscreteSystem, :ImplicitDiscreteSystem] @eval @deprecate $T(args...; kwargs...) System(args...; kwargs...) end diff --git a/src/systems/system.jl b/src/systems/system.jl index a43e4f5c65..61c3821b9f 100644 --- a/src/systems/system.jl +++ b/src/systems/system.jl @@ -24,7 +24,7 @@ structure. $(TYPEDFIELDS) """ -struct System <: AbstractSystem +struct System <: IntermediateDeprecationSystem """ $INTERNAL_FIELD_WARNING A unique integer tag for the system. diff --git a/test/odesystem.jl b/test/odesystem.jl index 3f00120487..9d1b2fc1e8 100644 --- a/test/odesystem.jl +++ b/test/odesystem.jl @@ -1600,3 +1600,17 @@ end @test getmetadata(sys, TestMeta, nothing) == "test" @test getmetadata(sys2, TestMeta, nothing) == "test" end + +struct TestWrapper + sys::ODESystem +end + +@testset "`ODESystem` is a type" begin + @variables x(t) + @named sys = ODESystem(D(x) ~ x, t) + @test sys isa ODESystem + @test sys isa System + arr = ODESystem[] + @test_nowarn push!(arr, sys) + @test_nowarn TestWrapper(sys) +end From 6b5dd8901b9a0c4fb3913a4a0f3b1f62b7ebefc3 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 9 Jul 2025 23:22:32 +0530 Subject: [PATCH 2174/2176] build: bump minor version --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index cfebdc4225..d92500f7e2 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "ModelingToolkit" uuid = "961ee093-0014-501f-94e3-6117800e7a78" authors = ["Yingbo Ma ", "Chris Rackauckas and contributors"] -version = "10.9.0" +version = "10.10.0" [deps] ADTypes = "47edcb42-4c32-4615-8424-f2b9edc5f35b" From 4651b505c0e466cac883a0fe00083c938e5639f7 Mon Sep 17 00:00:00 2001 From: contradict Date: Thu, 10 Jul 2025 10:20:46 -0700 Subject: [PATCH 2175/2176] Add guesses as advised by error message. --- test/downstream/linearization_dd.jl | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/test/downstream/linearization_dd.jl b/test/downstream/linearization_dd.jl index 44659f03ff..d42e642915 100644 --- a/test/downstream/linearization_dd.jl +++ b/test/downstream/linearization_dd.jl @@ -31,9 +31,10 @@ lin_inputs = [force.f.u] # => nothing to remove extra defaults op = Dict(cart.s => 10, cart.v => 0, link1.A => -pi / 2, link1.dA => 0, force.f.u => 0, link1.x1 => nothing, link1.y1 => nothing, link1.x2 => nothing, link1.x_cm => nothing) +guesses = [link1.fx1 => 0] @info "named_ss" G = named_ss(model, lin_inputs, lin_outputs; allow_symbolic = true, op, - allow_input_derivatives = true, zero_dummy_der = true) + allow_input_derivatives = true, zero_dummy_der = true, guesses) G = sminreal(G) @info "minreal" G = minreal(G) @@ -45,7 +46,7 @@ ps = poles(G) lsys, syss = linearize(model, lin_inputs, lin_outputs, allow_symbolic = true, op = op, - allow_input_derivatives = true, zero_dummy_der = true) + allow_input_derivatives = true, zero_dummy_der = true, guesses = guesses) lsyss, sysss = ModelingToolkit.linearize_symbolic(model, lin_inputs, lin_outputs; allow_input_derivatives = true) From 935a86515b162f4ab703d96740fa208de760d0bf Mon Sep 17 00:00:00 2001 From: Anshul Singhvi Date: Thu, 10 Jul 2025 17:10:36 -0400 Subject: [PATCH 2176/2176] Fix typo --- src/systems/system.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/systems/system.jl b/src/systems/system.jl index 61c3821b9f..729e8c4219 100644 --- a/src/systems/system.jl +++ b/src/systems/system.jl @@ -1045,7 +1045,7 @@ end function Base.showerror(io::IO, err::IllFormedNoiseEquationsError) print(io, """ - Noise equations are ill-formed. The number of rows much must number of drift \ + Noise equations are ill-formed. The number of rows must match the number of drift \ equations. `size(neqs, 1) == $(err.noise_eqs_rows) != length(eqs) == \ $(err.eqs_length)`. """)

M;WbV4y zYj3eQ7&WtW`4}PNN)V8SE}&S%*rB|@EMsDrtWn(IfGRNr@@+-Wq=Mw~D9P?apmzZz z1UeG{69Ok*@gQczp4T@6*NHlO0(1h|k<^uRoH#lNa0wGW+S|@VQ_+2|Du?Th_sOUj zhmSwe@;}*cNaes7C@d`!Z6$?bo$o0}R28xcIvJ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O?=4;9Us z?@S;l07ZIYPB32a_5>0T%z+=O1mu=~ArJ&l3jK><=cKkp0JuO%9o&`RA6aud7BR{` z4G01TzQ%$s0iV@hBoG2OA^#=8HUalQrjt3)|9j9KZAk$B7f7(zZZ`sqC5V-7E{X<- zQ-AE_3j4nh2!jI=%q3Xn{#1_x2yFjw(?o~e2r!lau5pre7!X*1nFgwg<{bY*AmV~e zsL-bEcts9Yf9ekv(K`QyKomSjg1L(J1)l$!4vabsey9>$aQO>?7z=M?OdJ93prr9(YIA#^*_Pd{^5xa(T;_`~F-& z(-${BbOM|n{Mpb*ZcxwAAIL~=TTF4INfX9IK$Ll&jtkj(Gu{U7LeSN{ME~oHH&^WK zx#=7jEEn~=EZfUn2%^s2hW-63ExV~<=h|Rv# Date: Tue, 24 Dec 2024 20:26:00 +0530 Subject: [PATCH 0635/2176] feat: allow `ImperativeAffect` to accept callable structs --- src/systems/imperative_affect.jl | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/systems/imperative_affect.jl b/src/systems/imperative_affect.jl index 2f489913ad..7580580add 100644 --- a/src/systems/imperative_affect.jl +++ b/src/systems/imperative_affect.jl @@ -38,7 +38,7 @@ in the returned tuple, in which case the associated field will not be updated. skip_checks::Bool end -function ImperativeAffect(f::Function; +function ImperativeAffect(f; observed::NamedTuple = NamedTuple{()}(()), modified::NamedTuple = NamedTuple{()}(()), ctx = nothing, @@ -48,18 +48,18 @@ function ImperativeAffect(f::Function; collect(values(modified)), collect(keys(modified)), ctx, skip_checks) end -function ImperativeAffect(f::Function, modified::NamedTuple; +function ImperativeAffect(f, modified::NamedTuple; observed::NamedTuple = NamedTuple{()}(()), ctx = nothing, skip_checks = false) ImperativeAffect( f, observed = observed, modified = modified, ctx = ctx, skip_checks = skip_checks) end function ImperativeAffect( - f::Function, modified::NamedTuple, observed::NamedTuple; ctx = nothing, skip_checks = false) + f, modified::NamedTuple, observed::NamedTuple; ctx = nothing, skip_checks = false) ImperativeAffect( f, observed = observed, modified = modified, ctx = ctx, skip_checks = skip_checks) end function ImperativeAffect( - f::Function, modified::NamedTuple, observed::NamedTuple, ctx; skip_checks = false) + f, modified::NamedTuple, observed::NamedTuple, ctx; skip_checks = false) ImperativeAffect( f, observed = observed, modified = modified, ctx = ctx, skip_checks = skip_checks) end From 81ff95a04a79cde91460c6d9440b4b9264450005 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 24 Dec 2024 20:26:23 +0530 Subject: [PATCH 0636/2176] feat: add `vars!` for callbacks and affects --- src/systems/callbacks.jl | 45 ++++++++++++++++++++++++++++++++ src/systems/imperative_affect.jl | 17 ++++++++++++ 2 files changed, 62 insertions(+) diff --git a/src/systems/callbacks.jl b/src/systems/callbacks.jl index eaf31a9c5f..492b5dac8c 100644 --- a/src/systems/callbacks.jl +++ b/src/systems/callbacks.jl @@ -77,6 +77,13 @@ function has_functional_affect(cb) (affects(cb) isa FunctionalAffect || affects(cb) isa ImperativeAffect) end +function vars!(vars, aff::FunctionalAffect; op = Differential) + for var in Iterators.flatten((unknowns(aff), parameters(aff), discretes(aff))) + vars!(vars, var) + end + return vars +end + #################################### continuous events ##################################### const NULL_AFFECT = Equation[] @@ -333,6 +340,22 @@ function continuous_events(sys::AbstractSystem) filter(!isempty, cbs) end +function vars!(vars, cb::SymbolicContinuousCallback; op = Differential) + for eq in equations(cb) + vars!(vars, eq; op) + end + for aff in (affects(cb), affect_negs(cb), initialize_affects(cb), finalize_affects(cb)) + if aff isa Vector{Equation} + for eq in aff + vars!(vars, eq; op) + end + elseif aff !== nothing + vars!(vars, aff; op) + end + end + return vars +end + #################################### discrete events ##################################### struct SymbolicDiscreteCallback @@ -469,6 +492,28 @@ function discrete_events(sys::AbstractSystem) cbs end +function vars!(vars, cb::SymbolicDiscreteCallback; op = Differential) + if symbolic_type(cb.condition) == NotSymbolic + if cb.condition isa AbstractArray + for eq in cb.condition + vars!(vars, eq; op) + end + end + else + vars!(vars, cb.condition; op) + end + for aff in (cb.affects, cb.initialize, cb.finalize) + if aff isa Vector{Equation} + for eq in aff + vars!(vars, eq; op) + end + elseif aff !== nothing + vars!(vars, aff; op) + end + end + return vars +end + ################################# compilation functions #################################### # handles ensuring that affect! functions work with integrator arguments diff --git a/src/systems/imperative_affect.jl b/src/systems/imperative_affect.jl index 7580580add..aee95fd051 100644 --- a/src/systems/imperative_affect.jl +++ b/src/systems/imperative_affect.jl @@ -216,3 +216,20 @@ function compile_user_affect(affect::ImperativeAffect, cb, sys, dvs, ps; kwargs. end scalarize_affects(affects::ImperativeAffect) = affects + +function vars!(vars, aff::ImperativeAffect; op = Differential) + for var in Iterators.flatten((observed(aff), modified(aff))) + if symbolic_type(var) == NotSymbolic() + if var isa AbstractArray + for v in var + v = unwrap(v) + vars!(vars, v) + end + end + else + var = unwrap(var) + vars!(vars, var) + end + end + return vars +end From 363a30c13bd1e90417cb08a0f690cd4f73a3892f Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 24 Dec 2024 20:27:09 +0530 Subject: [PATCH 0637/2176] fix: fix removal of `missing` defaults for solved array parameters --- src/systems/problem_utils.jl | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/systems/problem_utils.jl b/src/systems/problem_utils.jl index bf3d72e1e5..81be9c3d9e 100644 --- a/src/systems/problem_utils.jl +++ b/src/systems/problem_utils.jl @@ -591,6 +591,10 @@ function maybe_build_initialization_problem( p = unwrap(p) stype = symtype(p) op[p] = get_temporary_value(p) + if iscall(p) && operation(p) === getindex + arrp = arguments(p)[1] + op[arrp] = collect(arrp) + end end if is_time_dependent(sys) From 2f0f894d14bb8e242d55b9c58a7d657e501d0430 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 24 Dec 2024 20:27:28 +0530 Subject: [PATCH 0638/2176] fix: avoid trying to scalarize `missing` default of array parameters --- src/systems/systems.jl | 1 + 1 file changed, 1 insertion(+) diff --git a/src/systems/systems.jl b/src/systems/systems.jl index 04c50bc766..9c8c272c5c 100644 --- a/src/systems/systems.jl +++ b/src/systems/systems.jl @@ -57,6 +57,7 @@ function structural_simplify( ks = collect(keys(defs)) # take copy to avoid mutating defs while iterating. for k in ks if Symbolics.isarraysymbolic(k) && Symbolics.shape(k) !== Symbolics.Unknown() + defs[k] === missing && continue for i in eachindex(k) defs[k[i]] = defs[k][i] end From ec4c32ef304cb1397abf3a6a8807f4b8fb42f452 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 24 Dec 2024 20:27:56 +0530 Subject: [PATCH 0639/2176] fix: fix observed timeseries detection --- src/systems/index_cache.jl | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/systems/index_cache.jl b/src/systems/index_cache.jl index 623623da42..e4ca0b6b48 100644 --- a/src/systems/index_cache.jl +++ b/src/systems/index_cache.jl @@ -299,6 +299,9 @@ function IndexCache(sys::AbstractSystem) for v in vs if (idx = get(disc_idxs, v, nothing)) !== nothing push!(timeseries, idx.clock_idx) + elseif iscall(v) && operation(v) === getindex && + (idx = get(disc_idxs, arguments(v)[1], nothing)) !== nothing + push!(timeseries, idx.clock_idx) elseif haskey(observed_syms_to_timeseries, v) union!(timeseries, observed_syms_to_timeseries[v]) elseif haskey(dependent_pars_to_timeseries, v) From 924169b2166beeba7fbc180d8988aa2005277706 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 24 Dec 2024 20:31:26 +0530 Subject: [PATCH 0640/2176] fix: fix conversion of solved array parameters to variables in initialization --- src/systems/nonlinear/initializesystem.jl | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/systems/nonlinear/initializesystem.jl b/src/systems/nonlinear/initializesystem.jl index a7f98d79b1..87be4338b8 100644 --- a/src/systems/nonlinear/initializesystem.jl +++ b/src/systems/nonlinear/initializesystem.jl @@ -193,6 +193,15 @@ function generate_initializesystem(sys::AbstractSystem; append!(eqs_ics, trueobs) end + # even if `p => tovar(p)` is in `paramsubs`, `isparameter(p[1]) === true` after substitution + # so add scalarized versions as well + for k in collect(keys(paramsubs)) + symbolic_type(k) == ArraySymbolic() || continue + for i in eachindex(k) + paramsubs[k[i]] = paramsubs[k][i] + end + end + eqs_ics = Symbolics.substitute.(eqs_ics, (paramsubs,)) if is_time_dependent(sys) vars = [vars; collect(values(paramsubs))] From 00cc0755a90986f4cff4d8e3d41ff4aec4bae979 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 24 Dec 2024 20:32:07 +0530 Subject: [PATCH 0641/2176] fix: make array hack also search callbacks --- src/structural_transformation/symbolics_tearing.jl | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/structural_transformation/symbolics_tearing.jl b/src/structural_transformation/symbolics_tearing.jl index 4382ad6284..173f430c04 100644 --- a/src/structural_transformation/symbolics_tearing.jl +++ b/src/structural_transformation/symbolics_tearing.jl @@ -593,7 +593,7 @@ function tearing_reassemble(state::TearingState, var_eq_matching, @set! sys.unknowns = unknowns obs, subeqs, deps = cse_and_array_hacks( - obs, subeqs, unknowns, neweqs; cse = cse_hack, array = array_hack) + sys, obs, subeqs, unknowns, neweqs; cse = cse_hack, array = array_hack) @set! sys.eqs = neweqs @set! sys.observed = obs @@ -627,7 +627,7 @@ if all `p[i]` are present and the unscalarized form is used in any equation (obs not) we first count the number of times the scalarized form of each observed variable occurs in observed equations (and unknowns if it's split). """ -function cse_and_array_hacks(obs, subeqs, unknowns, neweqs; cse = true, array = true) +function cse_and_array_hacks(sys, obs, subeqs, unknowns, neweqs; cse = true, array = true) # HACK 1 # mapping of rhs to temporary CSE variable # `f(...) => tmpvar` in above example @@ -725,6 +725,11 @@ function cse_and_array_hacks(obs, subeqs, unknowns, neweqs; cse = true, array = for eq in neweqs vars!(all_vars, eq.rhs) end + + # also count unscalarized variables used in callbacks + for ev in Iterators.flatten((continuous_events(sys), discrete_events(sys))) + vars!(all_vars, ev) + end obs_arr_eqs = Equation[] for (arrvar, cnt) in arr_obs_occurrences cnt == length(arrvar) || continue From 4c394c5a577adee3e000dfe4a3d49ec0264ee553 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 24 Dec 2024 20:32:35 +0530 Subject: [PATCH 0642/2176] feat: support building v2 Co-Simulation FMU components --- ext/MTKFMIExt.jl | 238 +++++++++++++++++++++++++++++++++++++---------- 1 file changed, 191 insertions(+), 47 deletions(-) diff --git a/ext/MTKFMIExt.jl b/ext/MTKFMIExt.jl index 8b6f294601..f3ebf6d970 100644 --- a/ext/MTKFMIExt.jl +++ b/ext/MTKFMIExt.jl @@ -1,6 +1,7 @@ module MTKFMIExt using ModelingToolkit +using SymbolicIndexingInterface using ModelingToolkit: t_nounits as t, D_nounits as D import ModelingToolkit as MTK import FMI @@ -17,20 +18,24 @@ macro statuscheck(expr) return quote status = $expr fnname = $fnname - if (status isa Tuple && status[1] == FMI.fmi2True) || - (!(status isa Tuple) && status != FMI.fmi2StatusOK && - status != FMI.fmi2StatusWarning) + if status !== nothing && ((status isa Tuple && status[1] == FMI.fmi2True) || + (!(status isa Tuple) && status != FMI.fmi2StatusOK && + status != FMI.fmi2StatusWarning)) if status != FMI.fmi2StatusFatal FMI.fmi2Terminate(wrapper.instance) end FMI.fmi2FreeInstance!(wrapper.instance) wrapper.instance = nothing - error("FMU Error: status $status") + error("FMU Error in $fnname: status $status") end end |> esc end -function MTK.FMIComponent(::Val{2}, ::Val{:ME}; fmu = nothing, tolerance = 1e-6, name) +function MTK.FMIComponent(::Val{2}; fmu = nothing, tolerance = 1e-6, + communication_step_size = nothing, type, name) + if type == :CS && communication_step_size === nothing + throw(ArgumentError("`communication_step_size` must be specified for Co-Simulation FMUs.")) + end value_references = Dict() defs = Dict() states = [] @@ -40,10 +45,12 @@ function MTK.FMIComponent(::Val{2}, ::Val{:ME}; fmu = nothing, tolerance = 1e-6, value_references, diffvars, states, observed) if isempty(diffvars) __mtk_internal_u = [] - else - @variables __mtk_internal_u(t)[1:length(diffvars)] + elseif type == :ME + @variables __mtk_internal_u(t)[1:length(diffvars)] [guess = diffvars] + push!(observed, __mtk_internal_u ~ copy(diffvars)) + elseif type == :CS + @parameters __mtk_internal_u(t)[1:length(diffvars)]=missing [guess = diffvars] push!(observed, __mtk_internal_u ~ copy(diffvars)) - push!(states, __mtk_internal_u) end inputs = [] @@ -52,7 +59,7 @@ function MTK.FMIComponent(::Val{2}, ::Val{:ME}; fmu = nothing, tolerance = 1e-6, if isempty(inputs) __mtk_internal_x = [] else - @variables __mtk_internal_x(t)[1:length(inputs)] + @variables __mtk_internal_x(t)[1:length(inputs)] [guess = inputs] push!(observed, __mtk_internal_x ~ copy(inputs)) push!(states, __mtk_internal_x) end @@ -60,8 +67,14 @@ function MTK.FMIComponent(::Val{2}, ::Val{:ME}; fmu = nothing, tolerance = 1e-6, outputs = [] fmi_variables_to_mtk_variables!(fmu, FMI.getOutputValueReferencesAndNames(fmu), value_references, outputs, states, observed) - # @variables __mtk_internal_o(t)[1:length(outputs)] - # push!(observed, __mtk_internal_o ~ outputs) + if type == :CS + if isempty(outputs) + __mtk_internal_o = [] + else + @parameters __mtk_internal_o(t)[1:length(outputs)]=missing [guess = zeros(length(outputs))] + push!(observed, __mtk_internal_o ~ outputs) + end + end params = [] parameter_dependencies = Equation[] @@ -82,24 +95,69 @@ function MTK.FMIComponent(::Val{2}, ::Val{:ME}; fmu = nothing, tolerance = 1e-6, output_value_references = UInt32[value_references[var] for var in outputs] buffer_length = length(diffvars) + length(outputs) - _functor = FMI2MEFunctor(zeros(buffer_length), output_value_references) - @parameters (functor::(typeof(_functor)))(..)[1:buffer_length] = _functor - call_expr = functor(wrapper, __mtk_internal_u, __mtk_internal_x, __mtk_internal_p, t) - diffeqs = Equation[] - for (i, var) in enumerate([D.(diffvars); outputs]) - push!(diffeqs, var ~ call_expr[i]) - end + initialization_eqs = Equation[] + + if type == :ME + _functor = FMI2MEFunctor(zeros(buffer_length), output_value_references) + @parameters (functor::(typeof(_functor)))(..)[1:buffer_length] = _functor + call_expr = functor( + wrapper, __mtk_internal_u, __mtk_internal_x, __mtk_internal_p, t) + + diffeqs = Equation[] + for (i, var) in enumerate([D.(diffvars); outputs]) + push!(diffeqs, var ~ call_expr[i]) + end + + finalize_affect = MTK.FunctionalAffect(fmi2Finalize!, [], [wrapper], []) + step_affect = MTK.FunctionalAffect(fmi2MEStep!, [], [wrapper], []) + instance_management_callback = MTK.SymbolicDiscreteCallback( + (t != t - 1), step_affect; finalize = finalize_affect) + + push!(params, wrapper, functor) + push!(states, __mtk_internal_u) + elseif type == :CS + state_value_references = UInt32[value_references[var] for var in diffvars] + state_and_output_value_references = vcat( + state_value_references, output_value_references) + _functor = FMI2CSFunctor(state_and_output_value_references, + state_value_references, output_value_references) + @parameters (functor::(typeof(_functor)))(..)[1:(length(__mtk_internal_u) + length(__mtk_internal_o))] = _functor + for (i, x) in enumerate(collect(__mtk_internal_o)) + push!(initialization_eqs, + x ~ functor( + wrapper, __mtk_internal_u, __mtk_internal_x, __mtk_internal_p, t)[i]) + end - finalize_affect = MTK.FunctionalAffect(fmi2MEFinalize!, [], [wrapper], []) - step_affect = MTK.FunctionalAffect(fmi2MEStep!, [], [wrapper], []) - instance_management_callback = MTK.SymbolicDiscreteCallback( - (t != t - 1), step_affect; finalize = finalize_affect) + diffeqs = Equation[] + + cb_observed = (; inputs = __mtk_internal_x, params = copy(params), + t, wrapper, dt = communication_step_size) + cb_modified = (;) + if symbolic_type(__mtk_internal_o) != NotSymbolic() + cb_modified = (cb_modified..., outputs = __mtk_internal_o) + end + if symbolic_type(__mtk_internal_u) != NotSymbolic() + cb_modified = (cb_modified..., states = __mtk_internal_u) + end + initialize_affect = MTK.ImperativeAffect(fmi2CSInitialize!; observed = cb_observed, + modified = cb_modified, ctx = _functor) + finalize_affect = MTK.FunctionalAffect(fmi2Finalize!, [], [wrapper], []) + step_affect = MTK.ImperativeAffect( + fmi2CSStep!; observed = cb_observed, modified = cb_modified, ctx = _functor) + instance_management_callback = MTK.SymbolicDiscreteCallback( + communication_step_size, step_affect; initialize = initialize_affect, finalize = finalize_affect + ) + + symbolic_type(__mtk_internal_o) == NotSymbolic() || push!(params, __mtk_internal_o) + symbolic_type(__mtk_internal_u) == NotSymbolic() || push!(params, __mtk_internal_u) + + push!(params, wrapper, functor) + end - push!(params, wrapper, functor) eqs = [observed; diffeqs] return ODESystem(eqs, t, states, params; parameter_dependencies, defaults = defs, - discrete_events = [instance_management_callback], name) + discrete_events = [instance_management_callback], name, initialization_eqs) end function fmi_variables_to_mtk_variables!(fmu, varmap, value_references, truevars, allvars, @@ -142,35 +200,42 @@ function FMI2InstanceWrapper(fmu, params, inputs, tolerance) FMI2InstanceWrapper(fmu, params, inputs, tolerance, nothing) end -function get_instance!(wrapper::FMI2InstanceWrapper, states, inputs, params, t) +function get_instance_common!(wrapper::FMI2InstanceWrapper, states, inputs, params, t) + wrapper.instance = FMI.fmi2Instantiate!(wrapper.fmu)::FMI.FMU2Component + if !isempty(params) + @statuscheck FMI.fmi2SetReal(wrapper.instance, wrapper.param_value_references, + Csize_t(length(wrapper.param_value_references)), params) + end + @statuscheck FMI.fmi2SetupExperiment( + wrapper.instance, FMI.fmi2True, wrapper.tolerance, t, FMI.fmi2False, t) + @statuscheck FMI.fmi2EnterInitializationMode(wrapper.instance) + if !isempty(inputs) + @statuscheck FMI.fmi2SetReal(wrapper.instance, wrapper.input_value_references, + Csize_t(length(wrapper.param_value_references)), inputs) + end + + return wrapper.instance +end + +function get_instance_ME!(wrapper::FMI2InstanceWrapper, states, inputs, params, t) if wrapper.instance === nothing - wrapper.instance = FMI.fmi2Instantiate!(wrapper.fmu)::FMI.FMU2Component - if !isempty(params) - @statuscheck FMI.fmi2SetReal(wrapper.instance, wrapper.param_value_references, - Csize_t(length(wrapper.param_value_references)), params) - end - @statuscheck FMI.fmi2SetupExperiment( - wrapper.instance, FMI.fmi2True, wrapper.tolerance, t, FMI.fmi2False, t) - @statuscheck FMI.fmi2EnterInitializationMode(wrapper.instance) - if !isempty(inputs) - @statuscheck FMI.fmi2SetReal(wrapper.instance, wrapper.input_value_references, - Csize_t(length(wrapper.param_value_references)), inputs) - end + get_instance_common!(wrapper, states, inputs, params, t) @statuscheck FMI.fmi2ExitInitializationMode(wrapper.instance) eventInfo = FMI.fmi2NewDiscreteStates(wrapper.instance) @assert eventInfo.newDiscreteStatesNeeded == FMI.fmi2False # TODO: Support FMU events @statuscheck FMI.fmi2EnterContinuousTimeMode(wrapper.instance) end - instance = wrapper.instance - @statuscheck FMI.fmi2SetTime(instance, t) - @statuscheck FMI.fmi2SetContinuousStates(instance, states) - if !isempty(inputs) - @statuscheck FMI.fmi2SetReal(instance, wrapper.input_value_references, - Csize_t(length(wrapper.param_value_references)), inputs) - end - return instance + return wrapper.instance +end + +function get_instance_CS!(wrapper::FMI2InstanceWrapper, states, inputs, params, t) + if wrapper.instance === nothing + get_instance_common!(wrapper, states, inputs, params, t) + @statuscheck FMI.fmi2ExitInitializationMode(wrapper.instance) + end + return wrapper.instance end function complete_step!(wrapper::FMI2InstanceWrapper) @@ -198,8 +263,19 @@ end ndims = 1 end +function update_instance_ME!(wrapper::FMI2InstanceWrapper, states, inputs, t) + instance = wrapper.instance + @statuscheck FMI.fmi2SetTime(instance, t) + @statuscheck FMI.fmi2SetContinuousStates(instance, states) + if !isempty(inputs) + @statuscheck FMI.fmi2SetReal(instance, wrapper.input_value_references, + Csize_t(length(wrapper.param_value_references)), inputs) + end +end + function (fn::FMI2MEFunctor)(wrapper::FMI2InstanceWrapper, states, inputs, params, t) - instance = get_instance!(wrapper, states, inputs, params, t) + instance = get_instance_ME!(wrapper, states, inputs, params, t) + update_instance_ME!(wrapper, states, inputs, t) states_buffer = zeros(length(states)) @statuscheck FMI.fmi2GetDerivatives!(instance, states_buffer) @@ -214,10 +290,78 @@ function fmi2MEStep!(integrator, u, p, ctx) complete_step!(wrapper) end -function fmi2MEFinalize!(integrator, u, p, ctx) +function fmi2Finalize!(integrator, u, p, ctx) wrapper_idx = p[1] wrapper = integrator.ps[wrapper_idx] reset_instance!(wrapper) end +struct FMI2CSFunctor + state_and_output_value_references::Vector{UInt32} + state_value_references::Vector{UInt32} + output_value_references::Vector{UInt32} +end + +function (fn::FMI2CSFunctor)(wrapper::FMI2InstanceWrapper, states, inputs, params, t) + states = states isa SubArray ? copy(states) : states + inputs = inputs isa SubArray ? copy(inputs) : inputs + params = params isa SubArray ? copy(params) : params + instance = get_instance_CS!(wrapper, states, inputs, params, t) + if isempty(fn.output_value_references) + return eltype(states)[] + else + return FMI.fmi2GetReal(instance, fn.output_value_references) + end +end + +@register_array_symbolic (fn::FMI2CSFunctor)( + wrapper::FMI2InstanceWrapper, states::Vector{<:Real}, + inputs::Vector{<:Real}, params::Vector{<:Real}, t::Real) begin + size = (length(states) + length(fn.output_value_references),) + eltype = eltype(states) + ndims = 1 +end + +function fmi2CSInitialize!(m, o, ctx::FMI2CSFunctor, integrator) + states = isdefined(m, :states) ? m.states : () + inputs = o.inputs + params = o.params + t = o.t + wrapper = o.wrapper + if wrapper.instance !== nothing + reset_instance!(wrapper) + end + instance = get_instance_common!(wrapper, states, inputs, params, t) + @statuscheck FMI.fmi2ExitInitializationMode(instance) + if isdefined(m, :states) + @statuscheck FMI.fmi2GetReal!(instance, ctx.state_value_references, m.states) + end + if isdefined(m, :outputs) + @statuscheck FMI.fmi2GetReal!(instance, ctx.output_value_references, m.outputs) + end + + return m +end + +function fmi2CSStep!(m, o, ctx::FMI2CSFunctor, integrator) + wrapper = o.wrapper + states = isdefined(m, :states) ? m.states : () + inputs = o.inputs + params = o.params + t = o.t + dt = o.dt + + instance = get_instance_CS!(wrapper, states, inputs, params, integrator.t) + @statuscheck FMI.fmi2DoStep(instance, integrator.t - dt, dt, FMI.fmi2True) + + if isdefined(m, :states) + @statuscheck FMI.fmi2GetReal!(instance, ctx.state_value_references, m.states) + end + if isdefined(m, :outputs) + @statuscheck FMI.fmi2GetReal!(instance, ctx.output_value_references, m.outputs) + end + + return m +end + end # module From cdb2e1f9fec7ad9603acbbce59c845c3bc1463e7 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 24 Dec 2024 20:32:44 +0530 Subject: [PATCH 0643/2176] test: test v2 Co-Simulation FMU components --- test/extensions/fmi.jl | 73 ++++++++++++++++++++++++++++++++---------- 1 file changed, 56 insertions(+), 17 deletions(-) diff --git a/test/extensions/fmi.jl b/test/extensions/fmi.jl index 7e5e01f76a..2fc385934f 100644 --- a/test/extensions/fmi.jl +++ b/test/extensions/fmi.jl @@ -3,34 +3,73 @@ using ModelingToolkit: t_nounits as t, D_nounits as D import ModelingToolkit as MTK @testset "Standalone pendulum model" begin + fmu = loadFMU("SpringPendulum1D", "Dymola", "2022x"; type = :ME) + truesol = FMI.simulate( + fmu, (0.0, 8.0); saveat = 0.0:0.1:8.0, recordValues = ["mass.s", "mass.v"]) + @testset "v2, ME" begin fmu = loadFMU("SpringPendulum1D", "Dymola", "2022x"; type = :ME) - @mtkbuild sys = MTK.FMIComponent(Val(2), Val(:ME); fmu) + @mtkbuild sys = MTK.FMIComponent(Val(2); fmu, type = :ME) prob = ODEProblem{true, SciMLBase.FullSpecialize}( sys, [sys.mass__s => 0.5, sys.mass__v => 0.0], (0.0, 8.0)) sol = solve(prob, Tsit5(); reltol = 1e-8, abstol = 1e-8) @test SciMLBase.successful_retcode(sol) - truesol = FMI.simulate(fmu, (0.0, 8.0); saveat = 0.0:0.1:8.0) - @test sol(0.0:0.1:8.0).u≈truesol.states.u atol=1e-4 + @test sol(0.0:0.1:8.0; + idxs = [sys.mass__s, sys.mass__v]).u≈collect.(truesol.values.saveval) atol=1e-4 # repeated solve works @test_nowarn solve(prob, Tsit5()) end + @testset "v2, CS" begin + fmu = loadFMU("SpringPendulum1D", "Dymola", "2022x"; type = :CS) + @named inner = MTK.FMIComponent( + Val(2); fmu, communication_step_size = 0.001, type = :CS) + @variables x(t) = 1.0 + @mtkbuild sys = ODESystem([D(x) ~ x], t; systems = [inner]) + + prob = ODEProblem{true, SciMLBase.FullSpecialize}( + sys, [sys.inner.mass__s => 0.5, sys.inner.mass__v => 0.0], (0.0, 8.0)) + sol = solve(prob, Tsit5(); reltol = 1e-8, abstol = 1e-8) + @test SciMLBase.successful_retcode(sol) + + @test sol(0.0:0.1:8.0; + idxs = [sys.inner.mass__s, sys.inner.mass__v]).u≈collect.(truesol.values.saveval) rtol=1e-2 + end end @testset "IO Model" begin - fmu = loadFMU("./fmus/SimpleAdder.fmu"; type = :ME) - @named adder = MTK.FMIComponent(Val(2), Val(:ME); fmu) - @variables a(t) b(t) c(t) [guess = 1.0] - @mtkbuild sys = ODESystem( - [adder.a ~ a, adder.b ~ b, D(a) ~ t, - D(b) ~ adder.out + adder.c, c^2 ~ adder.out + adder.value], - t; - systems = [adder]) - - # c will be solved for by initialization - # this tests that initialization also works with FMUs - prob = ODEProblem(sys, [sys.adder.c => 1.0, sys.a => 1.0, sys.b => 1.0], (0.0, 1.0)) - sol = solve(prob, Rodas5P(autodiff = false)) - @test SciMLBase.successful_retcode(sol) + @testset "v2, ME" begin + fmu = loadFMU("../../omc-fmus/SimpleAdder.fmu"; type = :ME) + @named adder = MTK.FMIComponent(Val(2); fmu, type = :ME) + @variables a(t) b(t) c(t) [guess = 1.0] + @mtkbuild sys = ODESystem( + [adder.a ~ a, adder.b ~ b, D(a) ~ t, + D(b) ~ adder.out + adder.c, c^2 ~ adder.out + adder.value], + t; + systems = [adder]) + + # c will be solved for by initialization + # this tests that initialization also works with FMUs + prob = ODEProblem(sys, [sys.adder.c => 1.0, sys.a => 1.0, sys.b => 1.0], (0.0, 1.0)) + sol = solve(prob, Rodas5P(autodiff = false)) + @test SciMLBase.successful_retcode(sol) + end + @testset "v2, CS" begin + fmu = loadFMU("../../omc-fmus/SimpleAdder.fmu"; type = :CS) + @named adder = MTK.FMIComponent( + Val(2); fmu, type = :CS, communication_step_size = 0.001) + @variables a(t) b(t) c(t) [guess = 1.0] + @mtkbuild sys = ODESystem( + [adder.a ~ a, adder.b ~ b, D(a) ~ t, + D(b) ~ adder.out + adder.c, c^2 ~ adder.out + adder.value], + t; + systems = [adder]) + + # c will be solved for by initialization + # this tests that initialization also works with FMUs + prob = ODEProblem(sys, [sys.adder.c => 1.0, sys.a => 1.0, sys.b => 1.0], + (0.0, 1.0); use_scc = false) + sol = solve(prob, Rodas5P(autodiff = false)) + @test SciMLBase.successful_retcode(sol) + end end From 88e99b15ec40f6d96eb61ed4fedaee7db9b50f67 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 2 Jan 2025 17:19:16 +0530 Subject: [PATCH 0644/2176] feat: support v3 ME FMUs --- ext/MTKFMIExt.jl | 154 ++++++++++++++++++++++++++++++++++++----- test/extensions/fmi.jl | 17 +++++ 2 files changed, 153 insertions(+), 18 deletions(-) diff --git a/ext/MTKFMIExt.jl b/ext/MTKFMIExt.jl index f3ebf6d970..45412af957 100644 --- a/ext/MTKFMIExt.jl +++ b/ext/MTKFMIExt.jl @@ -14,25 +14,46 @@ macro statuscheck(expr) fnname = fn.args[2] instance = expr.args[2] - + is_v2 = startswith("fmi2", string(fnname)) + + fmiTrue = is_v2 ? FMI.fmi2True : FMI.fmi3True + fmiStatusOK = is_v2 ? FMI.fmi2StatusOK : FMI.fmi3StatusOK + fmiStatusWarning = is_v2 ? FMI.fmi2StatusWarning : FMI.fmi3StatusWarning + fmiStatusFatal = is_v2 ? FMI.fmi2StatusFatal : FMI.fmi3StatusFatal + fmiTerminate = is_v2 ? FMI.fmi2Terminate : FMI.fmi3Terminate + fmiFreeInstance! = is_v2 ? FMI.fmi2FreeInstance! : FMI.fmi3FreeInstance! return quote status = $expr fnname = $fnname - if status !== nothing && ((status isa Tuple && status[1] == FMI.fmi2True) || - (!(status isa Tuple) && status != FMI.fmi2StatusOK && - status != FMI.fmi2StatusWarning)) - if status != FMI.fmi2StatusFatal - FMI.fmi2Terminate(wrapper.instance) + if status !== nothing && ((status isa Tuple && status[1] == $fmiTrue) || + (!(status isa Tuple) && status != $fmiStatusOK && + status != $fmiStatusWarning)) + if status != $fmiStatusFatal + $fmiTerminate(wrapper.instance) end - FMI.fmi2FreeInstance!(wrapper.instance) + $fmiFreeInstance!(wrapper.instance) wrapper.instance = nothing error("FMU Error in $fnname: status $status") end end |> esc end -function MTK.FMIComponent(::Val{2}; fmu = nothing, tolerance = 1e-6, - communication_step_size = nothing, type, name) +@static if !hasmethod(FMI.getValueReferencesAndNames, Tuple{FMI.fmi3ModelDescription}) + function FMI.getValueReferencesAndNames( + md::FMI.fmi3ModelDescription; vrs = md.valueReferences) + dict = Dict{FMI.fmi3ValueReference, Array{String}}() + for vr in vrs + dict[vr] = FMI.valueReferenceToString(md, vr) + end + return dict + end +end + +function MTK.FMIComponent(::Val{Ver}; fmu = nothing, tolerance = 1e-6, + communication_step_size = nothing, type, name) where {Ver} + if Ver != 2 && Ver != 3 + throw(ArgumentError("FMI Version must be `2` or `3`")) + end if type == :CS && communication_step_size === nothing throw(ArgumentError("`communication_step_size` must be specified for Co-Simulation FMUs.")) end @@ -90,8 +111,14 @@ function MTK.FMIComponent(::Val{2}; fmu = nothing, tolerance = 1e-6, input_value_references = UInt32[value_references[var] for var in inputs] param_value_references = UInt32[value_references[var] for var in params] - @parameters wrapper::FMI2InstanceWrapper = FMI2InstanceWrapper( - fmu, param_value_references, input_value_references, tolerance) + + if Ver == 2 + @parameters wrapper::FMI2InstanceWrapper = FMI2InstanceWrapper( + fmu, param_value_references, input_value_references, tolerance) + else + @parameters wrapper::FMI3InstanceWrapper = FMI3InstanceWrapper( + fmu, param_value_references, input_value_references) + end output_value_references = UInt32[value_references[var] for var in outputs] buffer_length = length(diffvars) + length(outputs) @@ -99,7 +126,8 @@ function MTK.FMIComponent(::Val{2}; fmu = nothing, tolerance = 1e-6, initialization_eqs = Equation[] if type == :ME - _functor = FMI2MEFunctor(zeros(buffer_length), output_value_references) + FunctorT = Ver == 2 ? FMI2MEFunctor : FMI3MEFunctor + _functor = FunctorT(zeros(buffer_length), output_value_references) @parameters (functor::(typeof(_functor)))(..)[1:buffer_length] = _functor call_expr = functor( wrapper, __mtk_internal_u, __mtk_internal_x, __mtk_internal_p, t) @@ -109,14 +137,14 @@ function MTK.FMIComponent(::Val{2}; fmu = nothing, tolerance = 1e-6, push!(diffeqs, var ~ call_expr[i]) end - finalize_affect = MTK.FunctionalAffect(fmi2Finalize!, [], [wrapper], []) - step_affect = MTK.FunctionalAffect(fmi2MEStep!, [], [wrapper], []) + finalize_affect = MTK.FunctionalAffect(fmiFinalize!, [], [wrapper], []) + step_affect = MTK.FunctionalAffect(fmiMEStep!, [], [wrapper], []) instance_management_callback = MTK.SymbolicDiscreteCallback( (t != t - 1), step_affect; finalize = finalize_affect) push!(params, wrapper, functor) push!(states, __mtk_internal_u) - elseif type == :CS + elseif type == :CS && Ver == 2 state_value_references = UInt32[value_references[var] for var in diffvars] state_and_output_value_references = vcat( state_value_references, output_value_references) @@ -142,7 +170,7 @@ function MTK.FMIComponent(::Val{2}; fmu = nothing, tolerance = 1e-6, end initialize_affect = MTK.ImperativeAffect(fmi2CSInitialize!; observed = cb_observed, modified = cb_modified, ctx = _functor) - finalize_affect = MTK.FunctionalAffect(fmi2Finalize!, [], [wrapper], []) + finalize_affect = MTK.FunctionalAffect(fmiFinalize!, [], [wrapper], []) step_affect = MTK.ImperativeAffect( fmi2CSStep!; observed = cb_observed, modified = cb_modified, ctx = _functor) instance_management_callback = MTK.SymbolicDiscreteCallback( @@ -250,6 +278,63 @@ function reset_instance!(wrapper::FMI2InstanceWrapper) wrapper.instance = nothing end +mutable struct FMI3InstanceWrapper + const fmu::FMI.FMU3 + const param_value_references::Vector{UInt32} + const input_value_references::Vector{UInt32} + instance::Union{FMI.FMU3Instance{FMI.FMU3}, Nothing} +end + +function FMI3InstanceWrapper(fmu, params, inputs) + FMI3InstanceWrapper(fmu, params, inputs, nothing) +end + +function get_instance_common!(wrapper::FMI3InstanceWrapper, states, inputs, params, t) + if !isempty(params) + @statuscheck FMI.fmi3SetFloat64(wrapper.instance, wrapper.param_value_references, + params) + end + @statuscheck FMI.fmi3EnterInitializationMode( + wrapper.instance, FMI.fmi3False, zero(FMI.fmi3Float64), t, FMI.fmi3False, t) + if !isempty(inputs) + @statuscheck FMI.fmi3SetFloat64( + wrapper.instance, wrapper.input_value_references, inputs) + end + + return wrapper.instance +end + +function get_instance_ME!(wrapper::FMI3InstanceWrapper, states, inputs, params, t) + if wrapper.instance === nothing + wrapper.instance = FMI.fmi3InstantiateModelExchange!(wrapper.fmu)::FMI.FMU3Instance + get_instance_common!(wrapper, states, inputs, params, t) + @statuscheck FMI.fmi3ExitInitializationMode(wrapper.instance) + eventInfo = FMI.fmi3UpdateDiscreteStates(wrapper.instance) + @assert eventInfo[1] == FMI.fmi2False + # TODO: Support FMU events + @statuscheck FMI.fmi3EnterContinuousTimeMode(wrapper.instance) + end + + return wrapper.instance +end + +function complete_step!(wrapper::FMI3InstanceWrapper) + wrapper.instance === nothing && return + enterEventMode = Ref(FMI.fmi3False) + terminateSimulation = Ref(FMI.fmi3False) + @statuscheck FMI.fmi3CompletedIntegratorStep!( + wrapper.instance, FMI.fmi3True, enterEventMode, terminateSimulation) + @assert enterEventMode[] == FMI.fmi3False + @assert terminateSimulation[] == FMI.fmi3False +end + +function reset_instance!(wrapper::FMI3InstanceWrapper) + wrapper.instance === nothing && return + FMI.fmi3Terminate(wrapper.instance) + FMI.fmi3FreeInstance!(wrapper.instance) + wrapper.instance = nothing +end + struct FMI2MEFunctor{T} return_buffer::Vector{T} output_value_references::Vector{UInt32} @@ -284,13 +369,46 @@ function (fn::FMI2MEFunctor)(wrapper::FMI2InstanceWrapper, states, inputs, param return [states_buffer; outputs_buffer] end -function fmi2MEStep!(integrator, u, p, ctx) +struct FMI3MEFunctor{T} + return_buffer::Vector{T} + output_value_references::Vector{UInt32} +end + +@register_array_symbolic (fn::FMI3MEFunctor)( + wrapper::FMI3InstanceWrapper, states::Vector{<:Real}, + inputs::Vector{<:Real}, params::Vector{<:Real}, t::Real) begin + size = (length(states) + length(fn.output_value_references),) + eltype = eltype(states) + ndims = 1 +end + +function update_instance_ME!(wrapper::FMI3InstanceWrapper, states, inputs, t) + instance = wrapper.instance + @statuscheck FMI.fmi3SetTime(instance, t) + @statuscheck FMI.fmi3SetContinuousStates(instance, states) + if !isempty(inputs) + @statuscheck FMI.fmi3SetFloat64(instance, wrapper.input_value_references, inputs) + end +end + +function (fn::FMI3MEFunctor)(wrapper::FMI3InstanceWrapper, states, inputs, params, t) + instance = get_instance_ME!(wrapper, states, inputs, params, t) + update_instance_ME!(wrapper, states, inputs, t) + + states_buffer = zeros(length(states)) + @statuscheck FMI.fmi3GetContinuousStateDerivatives!(instance, states_buffer) + outputs_buffer = zeros(length(fn.output_value_references)) + FMI.fmi3GetFloat64!(instance, fn.output_value_references, outputs_buffer) + return [states_buffer; outputs_buffer] +end + +function fmiMEStep!(integrator, u, p, ctx) wrapper_idx = p[1] wrapper = integrator.ps[wrapper_idx] complete_step!(wrapper) end -function fmi2Finalize!(integrator, u, p, ctx) +function fmiFinalize!(integrator, u, p, ctx) wrapper_idx = p[1] wrapper = integrator.ps[wrapper_idx] reset_instance!(wrapper) diff --git a/test/extensions/fmi.jl b/test/extensions/fmi.jl index 2fc385934f..374d340e80 100644 --- a/test/extensions/fmi.jl +++ b/test/extensions/fmi.jl @@ -35,6 +35,23 @@ import ModelingToolkit as MTK @test sol(0.0:0.1:8.0; idxs = [sys.inner.mass__s, sys.inner.mass__v]).u≈collect.(truesol.values.saveval) rtol=1e-2 end + + fmu = loadFMU("SpringPendulum1D", "Dymola", "2023x", "3.0"; type = :ME) + truesol = FMI.simulate( + fmu, (0.0, 8.0); saveat = 0.0:0.1:8.0, recordValues = ["mass.s", "mass.v"]) + @testset "v3, ME" begin + fmu = loadFMU("SpringPendulum1D", "Dymola", "2023x", "3.0"; type = :ME) + @mtkbuild sys = MTK.FMIComponent(Val(3); fmu, type = :ME) + prob = ODEProblem{true, SciMLBase.FullSpecialize}( + sys, [sys.mass__s => 0.5, sys.mass__v => 0.0], (0.0, 8.0)) + sol = solve(prob, Tsit5(); reltol = 1e-8, abstol = 1e-8) + @test SciMLBase.successful_retcode(sol) + + @test sol(0.0:0.1:8.0; + idxs = [sys.mass__s, sys.mass__v]).u≈collect.(truesol.values.saveval) atol=1e-4 + # repeated solve works + @test_nowarn solve(prob, Tsit5()) + end end @testset "IO Model" begin From 5e3e75d6c89059112dac3bc0d063567161c3cbd9 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 2 Jan 2025 18:27:45 +0530 Subject: [PATCH 0645/2176] feat: support v3 CS FMUs --- ext/MTKFMIExt.jl | 104 ++++++++++++++++++++++++++++++++++++++--- test/extensions/fmi.jl | 15 ++++++ 2 files changed, 112 insertions(+), 7 deletions(-) diff --git a/ext/MTKFMIExt.jl b/ext/MTKFMIExt.jl index 45412af957..15ef376769 100644 --- a/ext/MTKFMIExt.jl +++ b/ext/MTKFMIExt.jl @@ -144,12 +144,16 @@ function MTK.FMIComponent(::Val{Ver}; fmu = nothing, tolerance = 1e-6, push!(params, wrapper, functor) push!(states, __mtk_internal_u) - elseif type == :CS && Ver == 2 + elseif type == :CS state_value_references = UInt32[value_references[var] for var in diffvars] state_and_output_value_references = vcat( state_value_references, output_value_references) - _functor = FMI2CSFunctor(state_and_output_value_references, - state_value_references, output_value_references) + _functor = if Ver == 2 + FMI2CSFunctor(state_and_output_value_references, + state_value_references, output_value_references) + else + FMI3CSFunctor(state_value_references, output_value_references) + end @parameters (functor::(typeof(_functor)))(..)[1:(length(__mtk_internal_u) + length(__mtk_internal_o))] = _functor for (i, x) in enumerate(collect(__mtk_internal_o)) push!(initialization_eqs, @@ -168,11 +172,11 @@ function MTK.FMIComponent(::Val{Ver}; fmu = nothing, tolerance = 1e-6, if symbolic_type(__mtk_internal_u) != NotSymbolic() cb_modified = (cb_modified..., states = __mtk_internal_u) end - initialize_affect = MTK.ImperativeAffect(fmi2CSInitialize!; observed = cb_observed, + initialize_affect = MTK.ImperativeAffect(fmiCSInitialize!; observed = cb_observed, modified = cb_modified, ctx = _functor) finalize_affect = MTK.FunctionalAffect(fmiFinalize!, [], [wrapper], []) step_affect = MTK.ImperativeAffect( - fmi2CSStep!; observed = cb_observed, modified = cb_modified, ctx = _functor) + fmiCSStep!; observed = cb_observed, modified = cb_modified, ctx = _functor) instance_management_callback = MTK.SymbolicDiscreteCallback( communication_step_size, step_affect; initialize = initialize_affect, finalize = finalize_affect ) @@ -318,6 +322,16 @@ function get_instance_ME!(wrapper::FMI3InstanceWrapper, states, inputs, params, return wrapper.instance end +function get_instance_CS!(wrapper::FMI3InstanceWrapper, states, inputs, params, t) + if wrapper.instance === nothing + wrapper.instance = FMI.fmi3InstantiateCoSimulation!( + wrapper.fmu; eventModeUsed = false)::FMI.FMU3Instance + get_instance_common!(wrapper, states, inputs, params, t) + @statuscheck FMI.fmi3ExitInitializationMode(wrapper.instance) + end + return wrapper.instance +end + function complete_step!(wrapper::FMI3InstanceWrapper) wrapper.instance === nothing && return enterEventMode = Ref(FMI.fmi3False) @@ -440,7 +454,7 @@ end ndims = 1 end -function fmi2CSInitialize!(m, o, ctx::FMI2CSFunctor, integrator) +function fmiCSInitialize!(m, o, ctx::FMI2CSFunctor, integrator) states = isdefined(m, :states) ? m.states : () inputs = o.inputs params = o.params @@ -449,6 +463,7 @@ function fmi2CSInitialize!(m, o, ctx::FMI2CSFunctor, integrator) if wrapper.instance !== nothing reset_instance!(wrapper) end + instance = get_instance_common!(wrapper, states, inputs, params, t) @statuscheck FMI.fmi2ExitInitializationMode(instance) if isdefined(m, :states) @@ -461,7 +476,7 @@ function fmi2CSInitialize!(m, o, ctx::FMI2CSFunctor, integrator) return m end -function fmi2CSStep!(m, o, ctx::FMI2CSFunctor, integrator) +function fmiCSStep!(m, o, ctx::FMI2CSFunctor, integrator) wrapper = o.wrapper states = isdefined(m, :states) ? m.states : () inputs = o.inputs @@ -482,4 +497,79 @@ function fmi2CSStep!(m, o, ctx::FMI2CSFunctor, integrator) return m end +struct FMI3CSFunctor + state_value_references::Vector{UInt32} + output_value_references::Vector{UInt32} +end + +function (fn::FMI3CSFunctor)(wrapper::FMI3InstanceWrapper, states, inputs, params, t) + states = states isa SubArray ? copy(states) : states + inputs = inputs isa SubArray ? copy(inputs) : inputs + params = params isa SubArray ? copy(params) : params + instance = get_instance_CS!(wrapper, states, inputs, params, t) + if isempty(fn.output_value_references) + return eltype(states)[] + else + return FMI.fmi3GetFloat64(instance, fn.output_value_references) + end +end + +@register_array_symbolic (fn::FMI3CSFunctor)( + wrapper::FMI3InstanceWrapper, states::Vector{<:Real}, + inputs::Vector{<:Real}, params::Vector{<:Real}, t::Real) begin + size = (length(states) + length(fn.output_value_references),) + eltype = eltype(states) + ndims = 1 +end + +function fmiCSInitialize!(m, o, ctx::FMI3CSFunctor, integrator) + states = isdefined(m, :states) ? m.states : () + inputs = o.inputs + params = o.params + t = o.t + wrapper = o.wrapper + if wrapper.instance !== nothing + reset_instance!(wrapper) + end + instance = get_instance_CS!(wrapper, states, inputs, params, t) + if isdefined(m, :states) + @statuscheck FMI.fmi3GetFloat64!(instance, ctx.state_value_references, m.states) + end + if isdefined(m, :outputs) + @statuscheck FMI.fmi3GetFloat64!(instance, ctx.output_value_references, m.outputs) + end + + return m +end + +function fmiCSStep!(m, o, ctx::FMI3CSFunctor, integrator) + wrapper = o.wrapper + states = isdefined(m, :states) ? m.states : () + inputs = o.inputs + params = o.params + t = o.t + dt = o.dt + + instance = get_instance_CS!(wrapper, states, inputs, params, integrator.t) + eventEncountered = Ref(FMI.fmi3False) + terminateSimulation = Ref(FMI.fmi3False) + earlyReturn = Ref(FMI.fmi3False) + lastSuccessfulTime = Ref(zero(FMI.fmi3Float64)) + @statuscheck FMI.fmi3DoStep!( + instance, integrator.t - dt, dt, FMI.fmi3True, eventEncountered, + terminateSimulation, earlyReturn, lastSuccessfulTime) + @assert eventEncountered[] == FMI.fmi3False + @assert terminateSimulation[] == FMI.fmi3False + @assert earlyReturn[] == FMI.fmi3False + + if isdefined(m, :states) + @statuscheck FMI.fmi3GetFloat64!(instance, ctx.state_value_references, m.states) + end + if isdefined(m, :outputs) + @statuscheck FMI.fmi3GetFloat64!(instance, ctx.output_value_references, m.outputs) + end + + return m +end + end # module diff --git a/test/extensions/fmi.jl b/test/extensions/fmi.jl index 374d340e80..743b65cae6 100644 --- a/test/extensions/fmi.jl +++ b/test/extensions/fmi.jl @@ -52,6 +52,21 @@ import ModelingToolkit as MTK # repeated solve works @test_nowarn solve(prob, Tsit5()) end + @testset "v3, CS" begin + fmu = loadFMU("SpringPendulum1D", "Dymola", "2023x", "3.0"; type = :CS) + @named inner = MTK.FMIComponent( + Val(3); fmu, communication_step_size = 0.001, type = :CS) + @variables x(t) = 1.0 + @mtkbuild sys = ODESystem([D(x) ~ x], t; systems = [inner]) + + prob = ODEProblem{true, SciMLBase.FullSpecialize}( + sys, [sys.inner.mass__s => 0.5, sys.inner.mass__v => 0.0], (0.0, 8.0)) + sol = solve(prob, Tsit5(); reltol = 1e-8, abstol = 1e-8) + @test SciMLBase.successful_retcode(sol) + + @test sol(0.0:0.1:8.0; + idxs = [sys.inner.mass__s, sys.inner.mass__v]).u≈collect.(truesol.values.saveval) rtol=1e-2 + end end @testset "IO Model" begin From 8b066e41de9618560756bf728e348fd5d4f85710 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 3 Jan 2025 14:05:22 +0530 Subject: [PATCH 0646/2176] fix: use `NoInit` for FMI callback reinitialization --- ext/MTKFMIExt.jl | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/ext/MTKFMIExt.jl b/ext/MTKFMIExt.jl index 15ef376769..e0d59c4032 100644 --- a/ext/MTKFMIExt.jl +++ b/ext/MTKFMIExt.jl @@ -4,6 +4,7 @@ using ModelingToolkit using SymbolicIndexingInterface using ModelingToolkit: t_nounits as t, D_nounits as D import ModelingToolkit as MTK +import SciMLBase import FMI macro statuscheck(expr) @@ -140,7 +141,7 @@ function MTK.FMIComponent(::Val{Ver}; fmu = nothing, tolerance = 1e-6, finalize_affect = MTK.FunctionalAffect(fmiFinalize!, [], [wrapper], []) step_affect = MTK.FunctionalAffect(fmiMEStep!, [], [wrapper], []) instance_management_callback = MTK.SymbolicDiscreteCallback( - (t != t - 1), step_affect; finalize = finalize_affect) + (t != t - 1), step_affect; finalize = finalize_affect, reinitializealg = SciMLBase.NoInit()) push!(params, wrapper, functor) push!(states, __mtk_internal_u) @@ -178,7 +179,8 @@ function MTK.FMIComponent(::Val{Ver}; fmu = nothing, tolerance = 1e-6, step_affect = MTK.ImperativeAffect( fmiCSStep!; observed = cb_observed, modified = cb_modified, ctx = _functor) instance_management_callback = MTK.SymbolicDiscreteCallback( - communication_step_size, step_affect; initialize = initialize_affect, finalize = finalize_affect + communication_step_size, step_affect; initialize = initialize_affect, + finalize = finalize_affect, reinitializealg = SciMLBase.NoInit() ) symbolic_type(__mtk_internal_o) == NotSymbolic() || push!(params, __mtk_internal_o) From 2676553d058e4f8075bb830ddafd934dd8f08488 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 3 Jan 2025 14:05:43 +0530 Subject: [PATCH 0647/2176] fix: handle array of symbolics in `InitializationProblem` type promotion --- src/systems/diffeqs/abstractodesystem.jl | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/systems/diffeqs/abstractodesystem.jl b/src/systems/diffeqs/abstractodesystem.jl index ac28d41dd4..921825dac8 100644 --- a/src/systems/diffeqs/abstractodesystem.jl +++ b/src/systems/diffeqs/abstractodesystem.jl @@ -1393,11 +1393,13 @@ function InitializationProblem{iip, specialize}(sys::AbstractSystem, for sym in unknowns(isys) haskey(fullmap, sym) || continue symbolic_type(fullmap[sym]) == NotSymbolic() || continue + is_array_of_symbolics(fullmap[sym]) && continue u0T = promote_type(u0T, typeof(fullmap[sym])) end for eq in observed(isys) haskey(fullmap, eq.lhs) || continue symbolic_type(fullmap[eq.lhs]) == NotSymbolic() || continue + is_array_of_symbolics(fullmap[eq.lhs]) && continue u0T = promote_type(u0T, typeof(fullmap[eq.lhs])) end if u0T != Union{} From 6dacd6e64d73f91a0c83b4ef56e10e5b87140535 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 3 Jan 2025 14:06:00 +0530 Subject: [PATCH 0648/2176] test: test v3 FMUs as subcomponents --- test/extensions/fmi.jl | 35 ++++++++++++++++++++++++++-- test/extensions/fmus/StateSpace.fmu | Bin 0 -> 34391 bytes 2 files changed, 33 insertions(+), 2 deletions(-) create mode 100644 test/extensions/fmus/StateSpace.fmu diff --git a/test/extensions/fmi.jl b/test/extensions/fmi.jl index 743b65cae6..868d60a9bb 100644 --- a/test/extensions/fmi.jl +++ b/test/extensions/fmi.jl @@ -2,6 +2,8 @@ using ModelingToolkit, FMI, FMIZoo, OrdinaryDiffEq using ModelingToolkit: t_nounits as t, D_nounits as D import ModelingToolkit as MTK +const FMU_DIR = joinpath(@__DIR__, "fmus") + @testset "Standalone pendulum model" begin fmu = loadFMU("SpringPendulum1D", "Dymola", "2022x"; type = :ME) truesol = FMI.simulate( @@ -71,7 +73,7 @@ end @testset "IO Model" begin @testset "v2, ME" begin - fmu = loadFMU("../../omc-fmus/SimpleAdder.fmu"; type = :ME) + fmu = loadFMU(joinpath(FMU_DIR, "SimpleAdder.fmu"); type = :ME) @named adder = MTK.FMIComponent(Val(2); fmu, type = :ME) @variables a(t) b(t) c(t) [guess = 1.0] @mtkbuild sys = ODESystem( @@ -87,7 +89,7 @@ end @test SciMLBase.successful_retcode(sol) end @testset "v2, CS" begin - fmu = loadFMU("../../omc-fmus/SimpleAdder.fmu"; type = :CS) + fmu = loadFMU(joinpath(FMU_DIR, "SimpleAdder.fmu"); type = :CS) @named adder = MTK.FMIComponent( Val(2); fmu, type = :CS, communication_step_size = 0.001) @variables a(t) b(t) c(t) [guess = 1.0] @@ -104,4 +106,33 @@ end sol = solve(prob, Rodas5P(autodiff = false)) @test SciMLBase.successful_retcode(sol) end + + @testset "v3, ME" begin + fmu = loadFMU(joinpath(FMU_DIR, "StateSpace.fmu"); type = :ME) + @named sspace = MTK.FMIComponent(Val(3); fmu, type = :ME) + @variables u(t)=1.0 x(t)=1.0 y(t) [guess = 1.0] + @mtkbuild sys = ODESystem( + [sspace.u ~ u, D(u) ~ t, D(x) ~ sspace.x + sspace.y, y^2 ~ sspace.y + x], t; + systems = [sspace] + ) + + prob = ODEProblem(sys, [sys.sspace.x => 1.0], (0.0, 1.0); use_scc = false) + sol = solve(prob, Rodas5P(autodiff = false)) + @test SciMLBase.successful_retcode(sol) + end + + @testset "v3, CS" begin + fmu = loadFMU(joinpath(FMU_DIR, "StateSpace.fmu"); type = :CS) + @named sspace = MTK.FMIComponent( + Val(3); fmu, communication_step_size = 1e-3, type = :CS) + @variables u(t)=1.0 x(t)=1.0 y(t) [guess = 1.0] + @mtkbuild sys = ODESystem( + [sspace.u ~ u, D(u) ~ t, D(x) ~ sspace.x + sspace.y, y^2 ~ sspace.y + x], t; + systems = [sspace] + ) + + prob = ODEProblem(sys, [sys.sspace.x => 1.0], (0.0, 1.0); use_scc = false) + sol = solve(prob, Rodas5P(autodiff = false)) + @test SciMLBase.successful_retcode(sol) + end end diff --git a/test/extensions/fmus/StateSpace.fmu b/test/extensions/fmus/StateSpace.fmu new file mode 100644 index 0000000000000000000000000000000000000000..c181081ca4222af0c306205c1d721c06905dd3a1 GIT binary patch literal 34391 zcmagF1yEc;(>6+QcXuZQ4;q{h+#wL$-Q8hv2*KSY0fIwtcPF?7T?mW&q6-WAllS}O zyMNtVcehTTIkVl*JZE}(s zzVTdMi6NMu$3RWHDkDFR(|rQ8?Bv44%5fjyl#BEn=Q=*3){55|<{ri>4d{t!zM-0F zT@0gHfZLF5yE^sV{bK!IYv9vwsgR~9^?_`}%CA=NmCDoQ)}Qs=-6eWNn5@m%QAzC4 z@%g-xM-;CT?h)~9OOW6DXOX(&4`8;%8mWU*xTy<-xtQaJZ5@vukjJh9T>VxZ3-a$P zH@Cd1);esLtGefw{BFN5QQl4K{|LSSA1C@~NdZ2tRUi)fT`KMo&X?|mUVQvLOph!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